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 31 39 2e 20 20 ersion 3.6.19.
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 2a 0a 2a 2a 20 54 68 69 73 rary..**.** This
04e0: 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e 20 77 61 amalgamation wa
04f0: 73 20 67 65 6e 65 72 61 74 65 64 20 6f 6e 20 32 s generated on 2
0500: 30 30 39 2d 31 30 2d 31 33 20 31 36 3a 32 32 3a 009-10-13 16:22:
0510: 30 30 20 55 54 43 2e 0a 2a 2f 0a 23 64 65 66 69 00 UTC..*/.#defi
0520: 6e 65 20 53 51 4c 49 54 45 5f 43 4f 52 45 20 31 ne SQLITE_CORE 1
0530: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
0540: 41 4d 41 4c 47 41 4d 41 54 49 4f 4e 20 31 0a 23 AMALGAMATION 1.#
0550: 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 50 52 ifndef SQLITE_PR
0560: 49 56 41 54 45 0a 23 20 64 65 66 69 6e 65 20 53 IVATE.# define S
0570: 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 74 QLITE_PRIVATE st
0580: 61 74 69 63 0a 23 65 6e 64 69 66 0a 23 69 66 6e atic.#endif.#ifn
0590: 64 65 66 20 53 51 4c 49 54 45 5f 41 50 49 0a 23 def SQLITE_API.#
05a0: 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 define SQLITE_A
05b0: 50 49 0a 23 65 6e 64 69 66 0a 2f 2a 2a 2a 2a 2a PI.#endif./*****
05c0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 ********* Begin
05d0: 66 69 6c 65 20 73 71 6c 69 74 65 49 6e 74 2e 68 file sqliteInt.h
05e0: 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
05f0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0600: 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 ********/./*.**
0610: 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 2001 September 1
0620: 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 5.**.** The auth
0630: 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
0640: 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
0650: 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
0660: 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
0670: 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
0680: 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
0690: 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
06a0: 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
06b0: 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
06c0: 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
06d0: 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
06e0: 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
06f0: 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
0700: 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
0710: 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
0720: 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
0730: 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
0740: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0750: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0760: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0770: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0780: 0a 2a 2a 20 49 6e 74 65 72 6e 61 6c 20 69 6e 74 .** Internal int
0790: 65 72 66 61 63 65 20 64 65 66 69 6e 69 74 69 6f erface definitio
07a0: 6e 73 20 66 6f 72 20 53 51 4c 69 74 65 2e 0a 2a ns for SQLite..*
07b0: 2a 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 53 51 *.*/.#ifndef _SQ
07c0: 4c 49 54 45 49 4e 54 5f 48 5f 0a 23 64 65 66 69 LITEINT_H_.#defi
07d0: 6e 65 20 5f 53 51 4c 49 54 45 49 4e 54 5f 48 5f ne _SQLITEINT_H_
07e0: 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 23 64 ../*.** These #d
07f0: 65 66 69 6e 65 73 20 73 68 6f 75 6c 64 20 65 6e efines should en
0800: 61 62 6c 65 20 3e 32 47 42 20 66 69 6c 65 20 73 able >2GB file s
0810: 75 70 70 6f 72 74 20 6f 6e 20 50 4f 53 49 58 20 upport on POSIX
0820: 69 66 20 74 68 65 0a 2a 2a 20 75 6e 64 65 72 6c if the.** underl
0830: 79 69 6e 67 20 6f 70 65 72 61 74 69 6e 67 20 73 ying operating s
0840: 79 73 74 65 6d 20 73 75 70 70 6f 72 74 73 20 69 ystem supports i
0850: 74 2e 20 20 49 66 20 74 68 65 20 4f 53 20 6c 61 t. If the OS la
0860: 63 6b 73 0a 2a 2a 20 6c 61 72 67 65 20 66 69 6c cks.** large fil
0870: 65 20 73 75 70 70 6f 72 74 2c 20 6f 72 20 69 66 e support, or if
0880: 20 74 68 65 20 4f 53 20 69 73 20 77 69 6e 64 6f the OS is windo
0890: 77 73 2c 20 74 68 65 73 65 20 73 68 6f 75 6c 64 ws, these should
08a0: 20 62 65 20 6e 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a be no-ops..**.*
08b0: 2a 20 54 69 63 6b 65 74 20 23 32 37 33 39 3a 20 * Ticket #2739:
08c0: 20 54 68 65 20 5f 4c 41 52 47 45 46 49 4c 45 5f The _LARGEFILE_
08d0: 53 4f 55 52 43 45 20 6d 61 63 72 6f 20 6d 75 73 SOURCE macro mus
08e0: 74 20 61 70 70 65 61 72 20 62 65 66 6f 72 65 20 t appear before
08f0: 61 6e 79 0a 2a 2a 20 73 79 73 74 65 6d 20 23 69 any.** system #i
0900: 6e 63 6c 75 64 65 73 2e 20 20 48 65 6e 63 65 2c ncludes. Hence,
0910: 20 74 68 69 73 20 62 6c 6f 63 6b 20 6f 66 20 63 this block of c
0920: 6f 64 65 20 6d 75 73 74 20 62 65 20 74 68 65 20 ode must be the
0930: 76 65 72 79 20 66 69 72 73 74 0a 2a 2a 20 63 6f very first.** co
0940: 64 65 20 69 6e 20 61 6c 6c 20 73 6f 75 72 63 65 de in all source
0950: 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 4c 61 files..**.** La
0960: 72 67 65 20 66 69 6c 65 20 73 75 70 70 6f 72 74 rge file support
0970: 20 63 61 6e 20 62 65 20 64 69 73 61 62 6c 65 64 can be disabled
0980: 20 75 73 69 6e 67 20 74 68 65 20 2d 44 53 51 4c using the -DSQL
0990: 49 54 45 5f 44 49 53 41 42 4c 45 5f 4c 46 53 20 ITE_DISABLE_LFS
09a0: 73 77 69 74 63 68 0a 2a 2a 20 6f 6e 20 74 68 65 switch.** on the
09b0: 20 63 6f 6d 70 69 6c 65 72 20 63 6f 6d 6d 61 6e compiler comman
09c0: 64 20 6c 69 6e 65 2e 20 20 54 68 69 73 20 69 73 d line. This is
09d0: 20 6e 65 63 65 73 73 61 72 79 20 69 66 20 79 6f necessary if yo
09e0: 75 20 61 72 65 20 63 6f 6d 70 69 6c 69 6e 67 0a u are compiling.
09f0: 2a 2a 20 6f 6e 20 61 20 72 65 63 65 6e 74 20 6d ** on a recent m
0a00: 61 63 68 69 6e 65 20 28 65 78 3a 20 52 65 64 20 achine (ex: Red
0a10: 48 61 74 20 37 2e 32 29 20 62 75 74 20 79 6f 75 Hat 7.2) but you
0a20: 20 77 61 6e 74 20 79 6f 75 72 20 63 6f 64 65 20 want your code
0a30: 74 6f 20 77 6f 72 6b 0a 2a 2a 20 6f 6e 20 61 6e to work.** on an
0a40: 20 6f 6c 64 65 72 20 6d 61 63 68 69 6e 65 20 28 older machine (
0a50: 65 78 3a 20 52 65 64 20 48 61 74 20 36 2e 30 29 ex: Red Hat 6.0)
0a60: 2e 20 20 49 66 20 79 6f 75 20 63 6f 6d 70 69 6c . If you compil
0a70: 65 20 6f 6e 20 52 65 64 20 48 61 74 20 37 2e 32 e on Red Hat 7.2
0a80: 0a 2a 2a 20 77 69 74 68 6f 75 74 20 74 68 69 73 .** without this
0a90: 20 6f 70 74 69 6f 6e 2c 20 4c 46 53 20 69 73 20 option, LFS is
0aa0: 65 6e 61 62 6c 65 2e 20 20 42 75 74 20 4c 46 53 enable. But LFS
0ab0: 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 20 does not exist
0ac0: 69 6e 20 74 68 65 20 6b 65 72 6e 65 6c 0a 2a 2a in the kernel.**
0ad0: 20 69 6e 20 52 65 64 20 48 61 74 20 36 2e 30 2c in Red Hat 6.0,
0ae0: 20 73 6f 20 74 68 65 20 63 6f 64 65 20 77 6f 6e so the code won
0af0: 27 74 20 77 6f 72 6b 2e 20 20 48 65 6e 63 65 2c 't work. Hence,
0b00: 20 66 6f 72 20 6d 61 78 69 6d 75 6d 20 62 69 6e for maximum bin
0b10: 61 72 79 0a 2a 2a 20 70 6f 72 74 61 62 69 6c 69 ary.** portabili
0b20: 74 79 20 79 6f 75 20 73 68 6f 75 6c 64 20 6f 6d ty you should om
0b30: 69 74 20 4c 46 53 2e 0a 2a 2a 0a 2a 2a 20 53 69 it LFS..**.** Si
0b40: 6d 69 6c 61 72 20 69 73 20 74 72 75 65 20 66 6f milar is true fo
0b50: 72 20 4d 61 63 20 4f 53 20 58 2e 20 20 4c 46 53 r Mac OS X. LFS
0b60: 20 69 73 20 6f 6e 6c 79 20 73 75 70 70 6f 72 74 is only support
0b70: 65 64 20 6f 6e 20 4d 61 63 20 4f 53 20 58 20 39 ed on Mac OS X 9
0b80: 20 61 6e 64 20 6c 61 74 65 72 2e 0a 2a 2f 0a 23 and later..*/.#
0b90: 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 49 ifndef SQLITE_DI
0ba0: 53 41 42 4c 45 5f 4c 46 53 0a 23 20 64 65 66 69 SABLE_LFS.# defi
0bb0: 6e 65 20 5f 4c 41 52 47 45 5f 46 49 4c 45 20 20 ne _LARGE_FILE
0bc0: 20 20 20 20 20 31 0a 23 20 69 66 6e 64 65 66 20 1.# ifndef
0bd0: 5f 46 49 4c 45 5f 4f 46 46 53 45 54 5f 42 49 54 _FILE_OFFSET_BIT
0be0: 53 0a 23 20 20 20 64 65 66 69 6e 65 20 5f 46 49 S.# define _FI
0bf0: 4c 45 5f 4f 46 46 53 45 54 5f 42 49 54 53 20 36 LE_OFFSET_BITS 6
0c00: 34 0a 23 20 65 6e 64 69 66 0a 23 20 64 65 66 69 4.# endif.# defi
0c10: 6e 65 20 5f 4c 41 52 47 45 46 49 4c 45 5f 53 4f ne _LARGEFILE_SO
0c20: 55 52 43 45 20 31 0a 23 65 6e 64 69 66 0a 0a 2f URCE 1.#endif../
0c30: 2a 0a 2a 2a 20 49 6e 63 6c 75 64 65 20 74 68 65 *.** Include the
0c40: 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 68 configuration h
0c50: 65 61 64 65 72 20 6f 75 74 70 75 74 20 62 79 20 eader output by
0c60: 27 63 6f 6e 66 69 67 75 72 65 27 20 69 66 20 77 'configure' if w
0c70: 65 27 72 65 20 75 73 69 6e 67 20 74 68 65 0a 2a e're using the.*
0c80: 2a 20 61 75 74 6f 63 6f 6e 66 2d 62 61 73 65 64 * autoconf-based
0c90: 20 62 75 69 6c 64 0a 2a 2f 0a 23 69 66 64 65 66 build.*/.#ifdef
0ca0: 20 5f 48 41 56 45 5f 53 51 4c 49 54 45 5f 43 4f _HAVE_SQLITE_CO
0cb0: 4e 46 49 47 5f 48 0a 23 69 6e 63 6c 75 64 65 20 NFIG_H.#include
0cc0: 22 63 6f 6e 66 69 67 2e 68 22 0a 23 65 6e 64 69 "config.h".#endi
0cd0: 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a f../************
0ce0: 2a 2a 20 49 6e 63 6c 75 64 65 20 73 71 6c 69 74 ** Include sqlit
0cf0: 65 4c 69 6d 69 74 2e 68 20 69 6e 20 74 68 65 20 eLimit.h in the
0d00: 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 middle of sqlite
0d10: 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a Int.h **********
0d20: 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
0d30: 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 73 71 ** Begin file sq
0d40: 6c 69 74 65 4c 69 6d 69 74 2e 68 20 2a 2a 2a 2a liteLimit.h ****
0d50: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0d60: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0d70: 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 4d 61 */./*.** 2007 Ma
0d80: 79 20 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 y 7.**.** The au
0d90: 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 thor disclaims c
0da0: 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 opyright to this
0db0: 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 source code. I
0dc0: 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 n place of.** a
0dd0: 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 legal notice, he
0de0: 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 re is a blessing
0df0: 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 :.**.** May y
0e00: 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e ou do good and n
0e10: 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d ot evil..** M
0e20: 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 ay you find forg
0e30: 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 iveness for your
0e40: 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 self and forgive
0e50: 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d others..** M
0e60: 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 ay you share fre
0e70: 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e ely, never takin
0e80: 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 g more than you
0e90: 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a give..**.*******
0ea0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0eb0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0ec0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0ed0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0ee0: 2a 2a 0a 2a 2a 20 0a 2a 2a 20 54 68 69 73 20 66 **.** .** This f
0ef0: 69 6c 65 20 64 65 66 69 6e 65 73 20 76 61 72 69 ile defines vari
0f00: 6f 75 73 20 6c 69 6d 69 74 73 20 6f 66 20 77 68 ous limits of wh
0f10: 61 74 20 53 51 4c 69 74 65 20 63 61 6e 20 70 72 at SQLite can pr
0f20: 6f 63 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 ocess..**.** @(#
0f30: 29 20 24 49 64 3a 20 73 71 6c 69 74 65 4c 69 6d ) $Id: sqliteLim
0f40: 69 74 2e 68 2c 76 20 31 2e 31 30 20 32 30 30 39 it.h,v 1.10 2009
0f50: 2f 30 31 2f 31 30 20 31 36 3a 31 35 3a 30 39 20 /01/10 16:15:09
0f60: 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 danielk1977 Exp
0f70: 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 $.*/../*.** The
0f80: 6d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f maximum length o
0f90: 66 20 61 20 54 45 58 54 20 6f 72 20 42 4c 4f 42 f a TEXT or BLOB
0fa0: 20 69 6e 20 62 79 74 65 73 2e 20 20 20 54 68 69 in bytes. Thi
0fb0: 73 20 61 6c 73 6f 0a 2a 2a 20 6c 69 6d 69 74 73 s also.** limits
0fc0: 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 20 72 the size of a r
0fd0: 6f 77 20 69 6e 20 61 20 74 61 62 6c 65 20 6f 72 ow in a table or
0fe0: 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 54 68 index..**.** Th
0ff0: 65 20 68 61 72 64 20 6c 69 6d 69 74 20 69 73 20 e hard limit is
1000: 74 68 65 20 61 62 69 6c 69 74 79 20 6f 66 20 61 the ability of a
1010: 20 33 32 2d 62 69 74 20 73 69 67 6e 65 64 20 69 32-bit signed i
1020: 6e 74 65 67 65 72 0a 2a 2a 20 74 6f 20 63 6f 75 nteger.** to cou
1030: 6e 74 20 74 68 65 20 73 69 7a 65 3a 20 32 5e 33 nt the size: 2^3
1040: 31 2d 31 20 6f 72 20 32 31 34 37 34 38 33 36 34 1-1 or 214748364
1050: 37 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 7..*/.#ifndef SQ
1060: 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 0a LITE_MAX_LENGTH.
1070: 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f # define SQLITE_
1080: 4d 41 58 5f 4c 45 4e 47 54 48 20 31 30 30 30 30 MAX_LENGTH 10000
1090: 30 30 30 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 00000.#endif../*
10a0: 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 .** This is the
10b0: 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f maximum number o
10c0: 66 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20 43 6f 6c f.**.** * Col
10d0: 75 6d 6e 73 20 69 6e 20 61 20 74 61 62 6c 65 0a umns in a table.
10e0: 2a 2a 20 20 20 20 2a 20 43 6f 6c 75 6d 6e 73 20 ** * Columns
10f0: 69 6e 20 61 6e 20 69 6e 64 65 78 0a 2a 2a 20 20 in an index.**
1100: 20 20 2a 20 43 6f 6c 75 6d 6e 73 20 69 6e 20 61 * Columns in a
1110: 20 76 69 65 77 0a 2a 2a 20 20 20 20 2a 20 54 65 view.** * Te
1120: 72 6d 73 20 69 6e 20 74 68 65 20 53 45 54 20 63 rms in the SET c
1130: 6c 61 75 73 65 20 6f 66 20 61 6e 20 55 50 44 41 lause of an UPDA
1140: 54 45 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 TE statement.**
1150: 20 20 20 2a 20 54 65 72 6d 73 20 69 6e 20 74 68 * Terms in th
1160: 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20 e result set of
1170: 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 a SELECT stateme
1180: 6e 74 0a 2a 2a 20 20 20 20 2a 20 54 65 72 6d 73 nt.** * Terms
1190: 20 69 6e 20 74 68 65 20 47 52 4f 55 50 20 42 59 in the GROUP BY
11a0: 20 6f 72 20 4f 52 44 45 52 20 42 59 20 63 6c 61 or ORDER BY cla
11b0: 75 73 65 73 20 6f 66 20 61 20 53 45 4c 45 43 54 uses of a SELECT
11c0: 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 20 statement..**
11d0: 20 20 2a 20 54 65 72 6d 73 20 69 6e 20 74 68 65 * Terms in the
11e0: 20 56 41 4c 55 45 53 20 63 6c 61 75 73 65 20 6f VALUES clause o
11f0: 66 20 61 6e 20 49 4e 53 45 52 54 20 73 74 61 74 f an INSERT stat
1200: 65 6d 65 6e 74 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ement.**.** The
1210: 68 61 72 64 20 75 70 70 65 72 20 6c 69 6d 69 74 hard upper limit
1220: 20 68 65 72 65 20 69 73 20 33 32 36 37 36 2e 20 here is 32676.
1230: 20 4d 6f 73 74 20 64 61 74 61 62 61 73 65 20 70 Most database p
1240: 65 6f 70 6c 65 20 77 69 6c 6c 0a 2a 2a 20 74 65 eople will.** te
1250: 6c 6c 20 79 6f 75 20 74 68 61 74 20 69 6e 20 61 ll you that in a
1260: 20 77 65 6c 6c 2d 6e 6f 72 6d 61 6c 69 7a 65 64 well-normalized
1270: 20 64 61 74 61 62 61 73 65 2c 20 79 6f 75 20 75 database, you u
1280: 73 75 61 6c 6c 79 20 73 68 6f 75 6c 64 0a 2a 2a sually should.**
1290: 20 6e 6f 74 20 68 61 76 65 20 6d 6f 72 65 20 74 not have more t
12a0: 68 61 6e 20 61 20 64 6f 7a 65 6e 20 6f 72 20 73 han a dozen or s
12b0: 6f 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 61 6e 79 o columns in any
12c0: 20 74 61 62 6c 65 2e 20 20 41 6e 64 20 69 66 0a table. And if.
12d0: 2a 2a 20 74 68 61 74 20 69 73 20 74 68 65 20 63 ** that is the c
12e0: 61 73 65 2c 20 74 68 65 72 65 20 69 73 20 6e 6f ase, there is no
12f0: 20 70 6f 69 6e 74 20 69 6e 20 68 61 76 69 6e 67 point in having
1300: 20 6d 6f 72 65 20 74 68 61 6e 20 61 20 66 65 77 more than a few
1310: 0a 2a 2a 20 64 6f 7a 65 6e 20 76 61 6c 75 65 73 .** dozen values
1320: 20 69 6e 20 61 6e 79 20 6f 66 20 74 68 65 20 6f in any of the o
1330: 74 68 65 72 20 73 69 74 75 61 74 69 6f 6e 73 20 ther situations
1340: 64 65 73 63 72 69 62 65 64 20 61 62 6f 76 65 2e described above.
1350: 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 .*/.#ifndef SQLI
1360: 54 45 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 0a 23 20 TE_MAX_COLUMN.#
1370: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 define SQLITE_MA
1380: 58 5f 43 4f 4c 55 4d 4e 20 32 30 30 30 0a 23 65 X_COLUMN 2000.#e
1390: 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 ndif../*.** The
13a0: 6d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f maximum length o
13b0: 66 20 61 20 73 69 6e 67 6c 65 20 53 51 4c 20 73 f a single SQL s
13c0: 74 61 74 65 6d 65 6e 74 20 69 6e 20 62 79 74 65 tatement in byte
13d0: 73 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 75 73 65 64 s..**.** It used
13e0: 20 74 6f 20 62 65 20 74 68 65 20 63 61 73 65 20 to be the case
13f0: 74 68 61 74 20 73 65 74 74 69 6e 67 20 74 68 69 that setting thi
1400: 73 20 76 61 6c 75 65 20 74 6f 20 7a 65 72 6f 20 s value to zero
1410: 77 6f 75 6c 64 0a 2a 2a 20 74 75 72 6e 20 74 68 would.** turn th
1420: 65 20 6c 69 6d 69 74 20 6f 66 66 2e 20 20 54 68 e limit off. Th
1430: 61 74 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 at is no longer
1440: 74 72 75 65 2e 20 20 49 74 20 69 73 20 6e 6f 74 true. It is not
1450: 20 70 6f 73 73 69 62 6c 65 0a 2a 2a 20 74 6f 20 possible.** to
1460: 74 75 72 6e 20 74 68 69 73 20 6c 69 6d 69 74 20 turn this limit
1470: 6f 66 66 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 off..*/.#ifndef
1480: 53 51 4c 49 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c SQLITE_MAX_SQL_L
1490: 45 4e 47 54 48 0a 23 20 64 65 66 69 6e 65 20 53 ENGTH.# define S
14a0: 51 4c 49 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c 45 QLITE_MAX_SQL_LE
14b0: 4e 47 54 48 20 31 30 30 30 30 30 30 30 30 30 0a NGTH 1000000000.
14c0: 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 #endif../*.** Th
14d0: 65 20 6d 61 78 69 6d 75 6d 20 64 65 70 74 68 20 e maximum depth
14e0: 6f 66 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e of an expression
14f0: 20 74 72 65 65 2e 20 54 68 69 73 20 69 73 20 6c tree. This is l
1500: 69 6d 69 74 65 64 20 74 6f 20 0a 2a 2a 20 73 6f imited to .** so
1510: 6d 65 20 65 78 74 65 6e 74 20 62 79 20 53 51 4c me extent by SQL
1520: 49 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c 45 4e 47 ITE_MAX_SQL_LENG
1530: 54 48 2e 20 42 75 74 20 73 6f 6d 65 74 69 6d 65 TH. But sometime
1540: 20 79 6f 75 20 6d 69 67 68 74 20 0a 2a 2a 20 77 you might .** w
1550: 61 6e 74 20 74 6f 20 70 6c 61 63 65 20 6d 6f 72 ant to place mor
1560: 65 20 73 65 76 65 72 65 20 6c 69 6d 69 74 73 20 e severe limits
1570: 6f 6e 20 74 68 65 20 63 6f 6d 70 6c 65 78 69 74 on the complexit
1580: 79 20 6f 66 20 61 6e 20 0a 2a 2a 20 65 78 70 72 y of an .** expr
1590: 65 73 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 41 20 ession..**.** A
15a0: 76 61 6c 75 65 20 6f 66 20 30 20 75 73 65 64 20 value of 0 used
15b0: 74 6f 20 6d 65 61 6e 20 74 68 61 74 20 74 68 65 to mean that the
15c0: 20 6c 69 6d 69 74 20 77 61 73 20 6e 6f 74 20 65 limit was not e
15d0: 6e 66 6f 72 63 65 64 2e 0a 2a 2a 20 42 75 74 20 nforced..** But
15e0: 74 68 61 74 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 that is no longe
15f0: 72 20 74 72 75 65 2e 20 20 54 68 65 20 6c 69 6d r true. The lim
1600: 69 74 20 69 73 20 6e 6f 77 20 73 74 72 69 63 74 it is now strict
1610: 6c 79 20 65 6e 66 6f 72 63 65 64 0a 2a 2a 20 61 ly enforced.** a
1620: 74 20 61 6c 6c 20 74 69 6d 65 73 2e 0a 2a 2f 0a t all times..*/.
1630: 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d #ifndef SQLITE_M
1640: 41 58 5f 45 58 50 52 5f 44 45 50 54 48 0a 23 20 AX_EXPR_DEPTH.#
1650: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 define SQLITE_MA
1660: 58 5f 45 58 50 52 5f 44 45 50 54 48 20 31 30 30 X_EXPR_DEPTH 100
1670: 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 0.#endif../*.**
1680: 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 The maximum numb
1690: 65 72 20 6f 66 20 74 65 72 6d 73 20 69 6e 20 61 er of terms in a
16a0: 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 compound SELECT
16b0: 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 54 statement..** T
16c0: 68 65 20 63 6f 64 65 20 67 65 6e 65 72 61 74 6f he code generato
16d0: 72 20 66 6f 72 20 63 6f 6d 70 6f 75 6e 64 20 53 r for compound S
16e0: 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 ELECT statements
16f0: 20 64 6f 65 73 20 6f 6e 65 0a 2a 2a 20 6c 65 76 does one.** lev
1700: 65 6c 20 6f 66 20 72 65 63 75 72 73 69 6f 6e 20 el of recursion
1710: 66 6f 72 20 65 61 63 68 20 74 65 72 6d 2e 20 20 for each term.
1720: 41 20 73 74 61 63 6b 20 6f 76 65 72 66 6c 6f 77 A stack overflow
1730: 20 63 61 6e 20 72 65 73 75 6c 74 0a 2a 2a 20 69 can result.** i
1740: 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 f the number of
1750: 74 65 72 6d 73 20 69 73 20 74 6f 6f 20 6c 61 72 terms is too lar
1760: 67 65 2e 20 20 49 6e 20 70 72 61 63 74 69 63 65 ge. In practice
1770: 2c 20 6d 6f 73 74 20 53 51 4c 0a 2a 2a 20 6e 65 , most SQL.** ne
1780: 76 65 72 20 68 61 73 20 6d 6f 72 65 20 74 68 61 ver has more tha
1790: 6e 20 33 20 6f 72 20 34 20 74 65 72 6d 73 2e 20 n 3 or 4 terms.
17a0: 20 55 73 65 20 61 20 76 61 6c 75 65 20 6f 66 20 Use a value of
17b0: 30 20 74 6f 20 64 69 73 61 62 6c 65 0a 2a 2a 20 0 to disable.**
17c0: 61 6e 79 20 6c 69 6d 69 74 20 6f 6e 20 74 68 65 any limit on the
17d0: 20 6e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d 73 number of terms
17e0: 20 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 74 20 53 in a compount S
17f0: 45 4c 45 43 54 2e 0a 2a 2f 0a 23 69 66 6e 64 65 ELECT..*/.#ifnde
1800: 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4d f SQLITE_MAX_COM
1810: 50 4f 55 4e 44 5f 53 45 4c 45 43 54 0a 23 20 64 POUND_SELECT.# d
1820: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 efine SQLITE_MAX
1830: 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 _COMPOUND_SELECT
1840: 20 35 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 500.#endif../*.
1850: 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e ** The maximum n
1860: 75 6d 62 65 72 20 6f 66 20 6f 70 63 6f 64 65 73 umber of opcodes
1870: 20 69 6e 20 61 20 56 44 42 45 20 70 72 6f 67 72 in a VDBE progr
1880: 61 6d 2e 0a 2a 2a 20 4e 6f 74 20 63 75 72 72 65 am..** Not curre
1890: 6e 74 6c 79 20 65 6e 66 6f 72 63 65 64 2e 0a 2a ntly enforced..*
18a0: 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
18b0: 5f 4d 41 58 5f 56 44 42 45 5f 4f 50 0a 23 20 64 _MAX_VDBE_OP.# d
18c0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 efine SQLITE_MAX
18d0: 5f 56 44 42 45 5f 4f 50 20 32 35 30 30 30 0a 23 _VDBE_OP 25000.#
18e0: 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 endif../*.** The
18f0: 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 maximum number
1900: 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 of arguments to
1910: 61 6e 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 2e an SQL function.
1920: 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 .*/.#ifndef SQLI
1930: 54 45 5f 4d 41 58 5f 46 55 4e 43 54 49 4f 4e 5f TE_MAX_FUNCTION_
1940: 41 52 47 0a 23 20 64 65 66 69 6e 65 20 53 51 4c ARG.# define SQL
1950: 49 54 45 5f 4d 41 58 5f 46 55 4e 43 54 49 4f 4e ITE_MAX_FUNCTION
1960: 5f 41 52 47 20 31 32 37 0a 23 65 6e 64 69 66 0a _ARG 127.#endif.
1970: 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d ./*.** The maxim
1980: 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 69 6e 2d um number of in-
1990: 6d 65 6d 6f 72 79 20 70 61 67 65 73 20 74 6f 20 memory pages to
19a0: 75 73 65 20 66 6f 72 20 74 68 65 20 6d 61 69 6e use for the main
19b0: 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 74 61 62 database.** tab
19c0: 6c 65 20 61 6e 64 20 66 6f 72 20 74 65 6d 70 6f le and for tempo
19d0: 72 61 72 79 20 74 61 62 6c 65 73 2e 20 20 54 68 rary tables. Th
19e0: 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 e SQLITE_DEFAULT
19f0: 5f 43 41 43 48 45 5f 53 49 5a 45 0a 2a 2f 0a 23 _CACHE_SIZE.*/.#
1a00: 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45 ifndef SQLITE_DE
1a10: 46 41 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45 FAULT_CACHE_SIZE
1a20: 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 .# define SQLITE
1a30: 5f 44 45 46 41 55 4c 54 5f 43 41 43 48 45 5f 53 _DEFAULT_CACHE_S
1a40: 49 5a 45 20 20 32 30 30 30 0a 23 65 6e 64 69 66 IZE 2000.#endif
1a50: 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1a60: 44 45 46 41 55 4c 54 5f 54 45 4d 50 5f 43 41 43 DEFAULT_TEMP_CAC
1a70: 48 45 5f 53 49 5a 45 0a 23 20 64 65 66 69 6e 65 HE_SIZE.# define
1a80: 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f SQLITE_DEFAULT_
1a90: 54 45 4d 50 5f 43 41 43 48 45 5f 53 49 5a 45 20 TEMP_CACHE_SIZE
1aa0: 20 35 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 500.#endif../*.
1ab0: 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e ** The maximum n
1ac0: 75 6d 62 65 72 20 6f 66 20 61 74 74 61 63 68 65 umber of attache
1ad0: 64 20 64 61 74 61 62 61 73 65 73 2e 20 20 54 68 d databases. Th
1ae0: 69 73 20 6d 75 73 74 20 62 65 20 62 65 74 77 65 is must be betwe
1af0: 65 6e 20 30 0a 2a 2a 20 61 6e 64 20 33 30 2e 20 en 0.** and 30.
1b00: 20 54 68 65 20 75 70 70 65 72 20 62 6f 75 6e 64 The upper bound
1b10: 20 6f 6e 20 33 30 20 69 73 20 62 65 63 61 75 73 on 30 is becaus
1b20: 65 20 61 20 33 32 2d 62 69 74 20 69 6e 74 65 67 e a 32-bit integ
1b30: 65 72 20 62 69 74 6d 61 70 0a 2a 2a 20 69 73 20 er bitmap.** is
1b40: 75 73 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 20 used internally
1b50: 74 6f 20 74 72 61 63 6b 20 61 74 74 61 63 68 65 to track attache
1b60: 64 20 64 61 74 61 62 61 73 65 73 2e 0a 2a 2f 0a d databases..*/.
1b70: 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d #ifndef SQLITE_M
1b80: 41 58 5f 41 54 54 41 43 48 45 44 0a 23 20 64 65 AX_ATTACHED.# de
1b90: 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f fine SQLITE_MAX_
1ba0: 41 54 54 41 43 48 45 44 20 31 30 0a 23 65 6e 64 ATTACHED 10.#end
1bb0: 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d if.../*.** The m
1bc0: 61 78 69 6d 75 6d 20 76 61 6c 75 65 20 6f 66 20 aximum value of
1bd0: 61 20 3f 6e 6e 6e 20 77 69 6c 64 63 61 72 64 20 a ?nnn wildcard
1be0: 74 68 61 74 20 74 68 65 20 70 61 72 73 65 72 20 that the parser
1bf0: 77 69 6c 6c 20 61 63 63 65 70 74 2e 0a 2a 2f 0a will accept..*/.
1c00: 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d #ifndef SQLITE_M
1c10: 41 58 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 AX_VARIABLE_NUMB
1c20: 45 52 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 ER.# define SQLI
1c30: 54 45 5f 4d 41 58 5f 56 41 52 49 41 42 4c 45 5f TE_MAX_VARIABLE_
1c40: 4e 55 4d 42 45 52 20 39 39 39 0a 23 65 6e 64 69 NUMBER 999.#endi
1c50: 66 0a 0a 2f 2a 20 4d 61 78 69 6d 75 6d 20 70 61 f../* Maximum pa
1c60: 67 65 20 73 69 7a 65 2e 20 20 54 68 65 20 75 70 ge size. The up
1c70: 70 65 72 20 62 6f 75 6e 64 20 6f 6e 20 74 68 69 per bound on thi
1c80: 73 20 76 61 6c 75 65 20 69 73 20 33 32 37 36 38 s value is 32768
1c90: 2e 20 20 54 68 69 73 20 61 20 6c 69 6d 69 74 0a . This a limit.
1ca0: 2a 2a 20 69 6d 70 6f 73 65 64 20 62 79 20 74 68 ** imposed by th
1cb0: 65 20 6e 65 63 65 73 73 69 74 79 20 6f 66 20 73 e necessity of s
1cc0: 74 6f 72 69 6e 67 20 74 68 65 20 76 61 6c 75 65 toring the value
1cd0: 20 69 6e 20 61 20 32 2d 62 79 74 65 20 75 6e 73 in a 2-byte uns
1ce0: 69 67 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a igned integer.**
1cf0: 20 61 6e 64 20 74 68 65 20 66 61 63 74 20 74 68 and the fact th
1d00: 61 74 20 74 68 65 20 70 61 67 65 20 73 69 7a 65 at the page size
1d10: 20 6d 75 73 74 20 62 65 20 61 20 70 6f 77 65 72 must be a power
1d20: 20 6f 66 20 32 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 of 2..**.** If
1d30: 74 68 69 73 20 6c 69 6d 69 74 20 69 73 20 63 68 this limit is ch
1d40: 61 6e 67 65 64 2c 20 74 68 65 6e 20 74 68 65 20 anged, then the
1d50: 63 6f 6d 70 69 6c 65 64 20 6c 69 62 72 61 72 79 compiled library
1d60: 20 69 73 20 74 65 63 68 6e 69 63 61 6c 6c 79 0a is technically.
1d70: 2a 2a 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65 20 ** incompatible
1d80: 77 69 74 68 20 61 6e 20 53 51 4c 69 74 65 20 6c with an SQLite l
1d90: 69 62 72 61 72 79 20 63 6f 6d 70 69 6c 65 64 20 ibrary compiled
1da0: 77 69 74 68 20 61 20 64 69 66 66 65 72 65 6e 74 with a different
1db0: 20 6c 69 6d 69 74 2e 20 49 66 0a 2a 2a 20 61 20 limit. If.** a
1dc0: 70 72 6f 63 65 73 73 20 6f 70 65 72 61 74 69 6e process operatin
1dd0: 67 20 6f 6e 20 61 20 64 61 74 61 62 61 73 65 20 g on a database
1de0: 77 69 74 68 20 61 20 70 61 67 65 2d 73 69 7a 65 with a page-size
1df0: 20 6f 66 20 36 35 35 33 36 20 62 79 74 65 73 20 of 65536 bytes
1e00: 0a 2a 2a 20 63 72 61 73 68 65 73 2c 20 74 68 65 .** crashes, the
1e10: 6e 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 n an instance of
1e20: 20 53 51 4c 69 74 65 20 63 6f 6d 70 69 6c 65 64 SQLite compiled
1e30: 20 77 69 74 68 20 74 68 65 20 64 65 66 61 75 6c with the defaul
1e40: 74 20 70 61 67 65 2d 73 69 7a 65 20 0a 2a 2a 20 t page-size .**
1e50: 6c 69 6d 69 74 20 77 69 6c 6c 20 6e 6f 74 20 62 limit will not b
1e60: 65 20 61 62 6c 65 20 74 6f 20 72 6f 6c 6c 62 61 e able to rollba
1e70: 63 6b 20 74 68 65 20 61 62 6f 72 74 65 64 20 74 ck the aborted t
1e80: 72 61 6e 73 61 63 74 69 6f 6e 2e 20 54 68 69 73 ransaction. This
1e90: 20 63 6f 75 6c 64 0a 2a 2a 20 6c 65 61 64 20 74 could.** lead t
1ea0: 6f 20 64 61 74 61 62 61 73 65 20 63 6f 72 72 75 o database corru
1eb0: 70 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 ption..*/.#ifnde
1ec0: 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 f SQLITE_MAX_PAG
1ed0: 45 5f 53 49 5a 45 0a 23 20 64 65 66 69 6e 65 20 E_SIZE.# define
1ee0: 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f SQLITE_MAX_PAGE_
1ef0: 53 49 5a 45 20 33 32 37 36 38 0a 23 65 6e 64 69 SIZE 32768.#endi
1f00: 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 64 65 f.../*.** The de
1f10: 66 61 75 6c 74 20 73 69 7a 65 20 6f 66 20 61 20 fault size of a
1f20: 64 61 74 61 62 61 73 65 20 70 61 67 65 2e 0a 2a database page..*
1f30: 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
1f40: 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 _DEFAULT_PAGE_SI
1f50: 5a 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 ZE.# define SQLI
1f60: 54 45 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f TE_DEFAULT_PAGE_
1f70: 53 49 5a 45 20 31 30 32 34 0a 23 65 6e 64 69 66 SIZE 1024.#endif
1f80: 0a 23 69 66 20 53 51 4c 49 54 45 5f 44 45 46 41 .#if SQLITE_DEFA
1f90: 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 3e 53 51 ULT_PAGE_SIZE>SQ
1fa0: 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 LITE_MAX_PAGE_SI
1fb0: 5a 45 0a 23 20 75 6e 64 65 66 20 53 51 4c 49 54 ZE.# undef SQLIT
1fc0: 45 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 E_DEFAULT_PAGE_S
1fd0: 49 5a 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c IZE.# define SQL
1fe0: 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 ITE_DEFAULT_PAGE
1ff0: 5f 53 49 5a 45 20 53 51 4c 49 54 45 5f 4d 41 58 _SIZE SQLITE_MAX
2000: 5f 50 41 47 45 5f 53 49 5a 45 0a 23 65 6e 64 69 _PAGE_SIZE.#endi
2010: 66 0a 0a 2f 2a 0a 2a 2a 20 4f 72 64 69 6e 61 72 f../*.** Ordinar
2020: 69 6c 79 2c 20 69 66 20 6e 6f 20 76 61 6c 75 65 ily, if no value
2030: 20 69 73 20 65 78 70 6c 69 63 69 74 6c 79 20 70 is explicitly p
2040: 72 6f 76 69 64 65 64 2c 20 53 51 4c 69 74 65 20 rovided, SQLite
2050: 63 72 65 61 74 65 73 20 64 61 74 61 62 61 73 65 creates database
2060: 73 0a 2a 2a 20 77 69 74 68 20 70 61 67 65 20 73 s.** with page s
2070: 69 7a 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55 ize SQLITE_DEFAU
2080: 4c 54 5f 50 41 47 45 5f 53 49 5a 45 2e 20 48 6f LT_PAGE_SIZE. Ho
2090: 77 65 76 65 72 2c 20 62 61 73 65 64 20 6f 6e 20 wever, based on
20a0: 63 65 72 74 61 69 6e 0a 2a 2a 20 64 65 76 69 63 certain.** devic
20b0: 65 20 63 68 61 72 61 63 74 65 72 69 73 74 69 63 e characteristic
20c0: 73 20 28 73 65 63 74 6f 72 2d 73 69 7a 65 20 61 s (sector-size a
20d0: 6e 64 20 61 74 6f 6d 69 63 20 77 72 69 74 65 28 nd atomic write(
20e0: 29 20 73 75 70 70 6f 72 74 29 2c 0a 2a 2a 20 53 ) support),.** S
20f0: 51 4c 69 74 65 20 6d 61 79 20 63 68 6f 6f 73 65 QLite may choose
2100: 20 61 20 6c 61 72 67 65 72 20 76 61 6c 75 65 2e a larger value.
2110: 20 54 68 69 73 20 63 6f 6e 73 74 61 6e 74 20 69 This constant i
2120: 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 76 61 s the maximum va
2130: 6c 75 65 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 lue.** SQLite wi
2140: 6c 6c 20 63 68 6f 6f 73 65 20 6f 6e 20 69 74 73 ll choose on its
2150: 20 6f 77 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 own..*/.#ifndef
2160: 20 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41 SQLITE_MAX_DEFA
2170: 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 0a 23 20 ULT_PAGE_SIZE.#
2180: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 define SQLITE_MA
2190: 58 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 X_DEFAULT_PAGE_S
21a0: 49 5a 45 20 38 31 39 32 0a 23 65 6e 64 69 66 0a IZE 8192.#endif.
21b0: 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 44 #if SQLITE_MAX_D
21c0: 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 EFAULT_PAGE_SIZE
21d0: 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 >SQLITE_MAX_PAGE
21e0: 5f 53 49 5a 45 0a 23 20 75 6e 64 65 66 20 53 51 _SIZE.# undef SQ
21f0: 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54 LITE_MAX_DEFAULT
2200: 5f 50 41 47 45 5f 53 49 5a 45 0a 23 20 64 65 66 _PAGE_SIZE.# def
2210: 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 44 ine SQLITE_MAX_D
2220: 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 EFAULT_PAGE_SIZE
2230: 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 SQLITE_MAX_PAGE
2240: 5f 53 49 5a 45 0a 23 65 6e 64 69 66 0a 0a 0a 2f _SIZE.#endif.../
2250: 2a 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20 6e 75 6d *.** Maximum num
2260: 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 ber of pages in
2270: 6f 6e 65 20 64 61 74 61 62 61 73 65 20 66 69 6c one database fil
2280: 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 e..**.** This is
2290: 20 72 65 61 6c 6c 79 20 6a 75 73 74 20 74 68 65 really just the
22a0: 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 66 default value f
22b0: 6f 72 20 74 68 65 20 6d 61 78 5f 70 61 67 65 5f or the max_page_
22c0: 63 6f 75 6e 74 20 70 72 61 67 6d 61 2e 0a 2a 2a count pragma..**
22d0: 20 54 68 69 73 20 76 61 6c 75 65 20 63 61 6e 20 This value can
22e0: 62 65 20 6c 6f 77 65 72 65 64 20 28 6f 72 20 72 be lowered (or r
22f0: 61 69 73 65 64 29 20 61 74 20 72 75 6e 2d 74 69 aised) at run-ti
2300: 6d 65 20 75 73 69 6e 67 20 74 68 61 74 20 74 68 me using that th
2310: 65 0a 2a 2a 20 6d 61 78 5f 70 61 67 65 5f 63 6f e.** max_page_co
2320: 75 6e 74 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 23 69 unt macro..*/.#i
2330: 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 fndef SQLITE_MAX
2340: 5f 50 41 47 45 5f 43 4f 55 4e 54 0a 23 20 64 65 _PAGE_COUNT.# de
2350: 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f fine SQLITE_MAX_
2360: 50 41 47 45 5f 43 4f 55 4e 54 20 31 30 37 33 37 PAGE_COUNT 10737
2370: 34 31 38 32 33 0a 23 65 6e 64 69 66 0a 0a 2f 2a 41823.#endif../*
2380: 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 .** Maximum leng
2390: 74 68 20 28 69 6e 20 62 79 74 65 73 29 20 6f 66 th (in bytes) of
23a0: 20 74 68 65 20 70 61 74 74 65 72 6e 20 69 6e 20 the pattern in
23b0: 61 20 4c 49 4b 45 20 6f 72 20 47 4c 4f 42 0a 2a a LIKE or GLOB.*
23c0: 2a 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2f 0a 23 * operator..*/.#
23d0: 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 ifndef SQLITE_MA
23e0: 58 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f 4c X_LIKE_PATTERN_L
23f0: 45 4e 47 54 48 0a 23 20 64 65 66 69 6e 65 20 53 ENGTH.# define S
2400: 51 4c 49 54 45 5f 4d 41 58 5f 4c 49 4b 45 5f 50 QLITE_MAX_LIKE_P
2410: 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48 20 35 30 ATTERN_LENGTH 50
2420: 30 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 000.#endif../*.*
2430: 2a 20 4d 61 78 69 6d 75 6d 20 64 65 70 74 68 20 * Maximum depth
2440: 6f 66 20 72 65 63 75 72 73 69 6f 6e 20 66 6f 72 of recursion for
2450: 20 74 72 69 67 67 65 72 73 2e 0a 2a 2a 0a 2a 2a triggers..**.**
2460: 20 41 20 76 61 6c 75 65 20 6f 66 20 31 20 6d 65 A value of 1 me
2470: 61 6e 73 20 74 68 61 74 20 61 20 74 72 69 67 67 ans that a trigg
2480: 65 72 20 70 72 6f 67 72 61 6d 20 77 69 6c 6c 20 er program will
2490: 6e 6f 74 20 62 65 20 61 62 6c 65 20 74 6f 20 69 not be able to i
24a0: 74 73 65 6c 66 0a 2a 2a 20 66 69 72 65 20 61 6e tself.** fire an
24b0: 79 20 74 72 69 67 67 65 72 73 2e 20 41 20 76 61 y triggers. A va
24c0: 6c 75 65 20 6f 66 20 30 20 6d 65 61 6e 73 20 74 lue of 0 means t
24d0: 68 61 74 20 6e 6f 20 74 72 69 67 67 65 72 20 70 hat no trigger p
24e0: 72 6f 67 72 61 6d 73 20 61 74 20 61 6c 6c 20 0a rograms at all .
24f0: 2a 2a 20 6d 61 79 20 62 65 20 65 78 65 63 75 74 ** may be execut
2500: 65 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 ed..*/.#ifndef 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 0a 23 69 66 20 64 65 66 69 R_DEPTH.#if defi
2530: 6e 65 64 28 53 51 4c 49 54 45 5f 53 4d 41 4c 4c ned(SQLITE_SMALL
2540: 5f 53 54 41 43 4b 29 0a 23 20 64 65 66 69 6e 65 _STACK).# define
2550: 20 53 51 4c 49 54 45 5f 4d 41 58 5f 54 52 49 47 SQLITE_MAX_TRIG
2560: 47 45 52 5f 44 45 50 54 48 20 31 30 0a 23 65 6c GER_DEPTH 10.#el
2570: 73 65 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 se.# define SQLI
2580: 54 45 5f 4d 41 58 5f 54 52 49 47 47 45 52 5f 44 TE_MAX_TRIGGER_D
2590: 45 50 54 48 20 31 30 30 30 0a 23 65 6e 64 69 66 EPTH 1000.#endif
25a0: 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a .#endif../******
25b0: 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 ******** End of
25c0: 73 71 6c 69 74 65 4c 69 6d 69 74 2e 68 20 2a 2a sqliteLimit.h **
25d0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
25e0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
25f0: 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
2600: 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 ******** Continu
2610: 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 ing where we lef
2620: 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 49 t off in sqliteI
2630: 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a nt.h ***********
2640: 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 20 44 69 73 *******/../* Dis
2650: 61 62 6c 65 20 6e 75 69 73 61 6e 63 65 20 77 61 able nuisance wa
2660: 72 6e 69 6e 67 73 20 6f 6e 20 42 6f 72 6c 61 6e rnings on Borlan
2670: 64 20 63 6f 6d 70 69 6c 65 72 73 20 2a 2f 0a 23 d compilers */.#
2680: 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 42 4f 52 if defined(__BOR
2690: 4c 41 4e 44 43 5f 5f 29 0a 23 70 72 61 67 6d 61 LANDC__).#pragma
26a0: 20 77 61 72 6e 20 2d 72 63 68 20 2f 2a 20 75 6e warn -rch /* un
26b0: 72 65 61 63 68 61 62 6c 65 20 63 6f 64 65 20 2a reachable code *
26c0: 2f 0a 23 70 72 61 67 6d 61 20 77 61 72 6e 20 2d /.#pragma warn -
26d0: 63 63 63 20 2f 2a 20 43 6f 6e 64 69 74 69 6f 6e ccc /* Condition
26e0: 20 69 73 20 61 6c 77 61 79 73 20 74 72 75 65 20 is always true
26f0: 6f 72 20 66 61 6c 73 65 20 2a 2f 0a 23 70 72 61 or false */.#pra
2700: 67 6d 61 20 77 61 72 6e 20 2d 61 75 73 20 2f 2a gma warn -aus /*
2710: 20 41 73 73 69 67 6e 65 64 20 76 61 6c 75 65 20 Assigned value
2720: 69 73 20 6e 65 76 65 72 20 75 73 65 64 20 2a 2f is never used */
2730: 0a 23 70 72 61 67 6d 61 20 77 61 72 6e 20 2d 63 .#pragma warn -c
2740: 73 75 20 2f 2a 20 43 6f 6d 70 61 72 69 6e 67 20 su /* Comparing
2750: 73 69 67 6e 65 64 20 61 6e 64 20 75 6e 73 69 67 signed and unsig
2760: 6e 65 64 20 2a 2f 0a 23 70 72 61 67 6d 61 20 77 ned */.#pragma w
2770: 61 72 6e 20 2d 73 70 61 20 2f 2a 20 53 75 73 70 arn -spa /* Susp
2780: 69 63 69 6f 75 73 20 70 6f 69 6e 74 65 72 20 61 icious pointer a
2790: 72 69 74 68 6d 65 74 69 63 20 2a 2f 0a 23 65 6e rithmetic */.#en
27a0: 64 69 66 0a 0a 2f 2a 20 4e 65 65 64 65 64 20 66 dif../* Needed f
27b0: 6f 72 20 76 61 72 69 6f 75 73 20 64 65 66 69 6e or various defin
27c0: 69 74 69 6f 6e 73 2e 2e 2e 20 2a 2f 0a 23 69 66 itions... */.#if
27d0: 6e 64 65 66 20 5f 47 4e 55 5f 53 4f 55 52 43 45 ndef _GNU_SOURCE
27e0: 0a 23 20 64 65 66 69 6e 65 20 5f 47 4e 55 5f 53 .# define _GNU_S
27f0: 4f 55 52 43 45 0a 23 65 6e 64 69 66 0a 0a 2f 2a OURCE.#endif../*
2800: 0a 2a 2a 20 49 6e 63 6c 75 64 65 20 73 74 61 6e .** Include stan
2810: 64 61 72 64 20 68 65 61 64 65 72 20 66 69 6c 65 dard header file
2820: 73 20 61 73 20 6e 65 63 65 73 73 61 72 79 0a 2a s as necessary.*
2830: 2f 0a 23 69 66 64 65 66 20 48 41 56 45 5f 53 54 /.#ifdef HAVE_ST
2840: 44 49 4e 54 5f 48 0a 23 69 6e 63 6c 75 64 65 20 DINT_H.#include
2850: 3c 73 74 64 69 6e 74 2e 68 3e 0a 23 65 6e 64 69 <stdint.h>.#endi
2860: 66 0a 23 69 66 64 65 66 20 48 41 56 45 5f 49 4e f.#ifdef HAVE_IN
2870: 54 54 59 50 45 53 5f 48 0a 23 69 6e 63 6c 75 64 TTYPES_H.#includ
2880: 65 20 3c 69 6e 74 74 79 70 65 73 2e 68 3e 0a 23 e <inttypes.h>.#
2890: 65 6e 64 69 66 0a 0a 23 64 65 66 69 6e 65 20 53 endif..#define S
28a0: 51 4c 49 54 45 5f 49 4e 44 45 58 5f 53 41 4d 50 QLITE_INDEX_SAMP
28b0: 4c 45 53 20 31 30 0a 0a 2f 2a 0a 2a 2a 20 54 68 LES 10../*.** Th
28c0: 69 73 20 6d 61 63 72 6f 20 69 73 20 75 73 65 64 is macro is used
28d0: 20 74 6f 20 22 68 69 64 65 22 20 73 6f 6d 65 20 to "hide" some
28e0: 75 67 6c 69 6e 65 73 73 20 69 6e 20 63 61 73 74 ugliness in cast
28f0: 69 6e 67 20 61 6e 20 69 6e 74 0a 2a 2a 20 76 61 ing an int.** va
2900: 6c 75 65 20 74 6f 20 61 20 70 74 72 20 76 61 6c lue to a ptr val
2910: 75 65 20 75 6e 64 65 72 20 74 68 65 20 4d 53 56 ue under the MSV
2920: 43 20 36 34 2d 62 69 74 20 63 6f 6d 70 69 6c 65 C 64-bit compile
2930: 72 2e 20 20 20 43 61 73 74 69 6e 67 0a 2a 2a 20 r. Casting.**
2940: 6e 6f 6e 20 36 34 2d 62 69 74 20 76 61 6c 75 65 non 64-bit value
2950: 73 20 74 6f 20 70 74 72 20 74 79 70 65 73 20 72 s to ptr types r
2960: 65 73 75 6c 74 73 20 69 6e 20 61 20 22 68 61 72 esults in a "har
2970: 64 22 20 65 72 72 6f 72 20 77 69 74 68 20 0a 2a d" error with .*
2980: 2a 20 74 68 65 20 4d 53 56 43 20 36 34 2d 62 69 * the MSVC 64-bi
2990: 74 20 63 6f 6d 70 69 6c 65 72 20 77 68 69 63 68 t compiler which
29a0: 20 74 68 69 73 20 61 74 74 65 6d 70 74 73 20 74 this attempts t
29b0: 6f 20 61 76 6f 69 64 2e 20 20 0a 2a 2a 0a 2a 2a o avoid. .**.**
29c0: 20 41 20 73 69 6d 70 6c 65 20 63 6f 6d 70 69 6c A simple compil
29d0: 65 72 20 70 72 61 67 6d 61 20 6f 72 20 63 61 73 er pragma or cas
29e0: 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 63 6f ting sequence co
29f0: 75 6c 64 20 6e 6f 74 20 62 65 20 66 6f 75 6e 64 uld not be found
2a00: 0a 2a 2a 20 74 6f 20 63 6f 72 72 65 63 74 20 74 .** to correct t
2a10: 68 69 73 20 69 6e 20 61 6c 6c 20 73 69 74 75 61 his in all situa
2a20: 74 69 6f 6e 73 2c 20 73 6f 20 74 68 69 73 20 6d tions, so this m
2a30: 61 63 72 6f 20 77 61 73 20 69 6e 74 72 6f 64 75 acro was introdu
2a40: 63 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 63 6f ced..**.** It co
2a50: 75 6c 64 20 62 65 20 61 72 67 75 65 64 20 74 68 uld be argued th
2a60: 61 74 20 74 68 65 20 69 6e 74 70 74 72 5f 74 20 at the intptr_t
2a70: 74 79 70 65 20 63 6f 75 6c 64 20 62 65 20 75 73 type could be us
2a80: 65 64 20 69 6e 20 74 68 69 73 0a 2a 2a 20 63 61 ed in this.** ca
2a90: 73 65 2c 20 62 75 74 20 74 68 61 74 20 74 79 70 se, but that typ
2aa0: 65 20 69 73 20 6e 6f 74 20 61 76 61 69 6c 61 62 e is not availab
2ab0: 6c 65 20 6f 6e 20 61 6c 6c 20 63 6f 6d 70 69 6c le on all compil
2ac0: 65 72 73 2c 20 6f 72 20 0a 2a 2a 20 72 65 71 75 ers, or .** requ
2ad0: 69 72 65 73 20 74 68 65 20 23 69 6e 63 6c 75 64 ires the #includ
2ae0: 65 20 6f 66 20 73 70 65 63 69 66 69 63 20 68 65 e of specific he
2af0: 61 64 65 72 73 20 77 68 69 63 68 20 64 69 66 66 aders which diff
2b00: 65 72 73 20 62 65 74 77 65 65 6e 0a 2a 2a 20 70 ers between.** p
2b10: 6c 61 74 66 6f 72 6d 73 2e 0a 2a 2a 0a 2a 2a 20 latforms..**.**
2b20: 54 69 63 6b 65 74 20 23 33 38 36 30 3a 20 20 54 Ticket #3860: T
2b30: 68 65 20 6c 6c 76 6d 2d 67 63 63 2d 34 2e 32 20 he llvm-gcc-4.2
2b40: 63 6f 6d 70 69 6c 65 72 20 66 72 6f 6d 20 41 70 compiler from Ap
2b50: 70 6c 65 20 63 68 6f 6b 65 73 20 6f 6e 0a 2a 2a ple chokes on.**
2b60: 20 74 68 65 20 28 28 76 6f 69 64 2a 29 26 28 28 the ((void*)&((
2b70: 63 68 61 72 2a 29 30 29 5b 58 5d 29 20 63 6f 6e char*)0)[X]) con
2b80: 73 74 72 75 63 74 2e 20 20 42 75 74 20 4d 53 56 struct. But MSV
2b90: 43 20 63 68 6f 6b 65 73 20 6f 6e 20 28 28 76 6f C chokes on ((vo
2ba0: 69 64 2a 29 28 58 29 29 2e 0a 2a 2a 20 53 6f 20 id*)(X))..** So
2bb0: 77 65 20 68 61 76 65 20 74 6f 20 64 65 66 69 6e we have to defin
2bc0: 65 20 74 68 65 20 6d 61 63 72 6f 73 20 69 6e 20 e the macros in
2bd0: 64 69 66 66 65 72 65 6e 74 20 77 61 79 73 20 64 different ways d
2be0: 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 0a epending on the.
2bf0: 2a 2a 20 63 6f 6d 70 69 6c 65 72 2e 0a 2a 2f 0a ** compiler..*/.
2c00: 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 47 4e #if defined(__GN
2c10: 55 43 5f 5f 29 0a 23 20 69 66 20 64 65 66 69 6e UC__).# if defin
2c20: 65 64 28 48 41 56 45 5f 53 54 44 49 4e 54 5f 48 ed(HAVE_STDINT_H
2c30: 29 0a 23 20 20 20 64 65 66 69 6e 65 20 53 51 4c ).# define SQL
2c40: 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 52 28 58 ITE_INT_TO_PTR(X
2c50: 29 20 20 28 28 76 6f 69 64 2a 29 28 69 6e 74 70 ) ((void*)(intp
2c60: 74 72 5f 74 29 28 58 29 29 0a 23 20 20 20 64 65 tr_t)(X)).# de
2c70: 66 69 6e 65 20 53 51 4c 49 54 45 5f 50 54 52 5f fine SQLITE_PTR_
2c80: 54 4f 5f 49 4e 54 28 58 29 20 20 28 28 69 6e 74 TO_INT(X) ((int
2c90: 29 28 69 6e 74 70 74 72 5f 74 29 28 58 29 29 0a )(intptr_t)(X)).
2ca0: 23 20 65 6c 73 65 0a 23 20 20 20 64 65 66 69 6e # else.# defin
2cb0: 65 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f e SQLITE_INT_TO_
2cc0: 50 54 52 28 58 29 20 20 28 28 76 6f 69 64 2a 29 PTR(X) ((void*)
2cd0: 28 58 29 29 0a 23 20 20 20 64 65 66 69 6e 65 20 (X)).# define
2ce0: 53 51 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e SQLITE_PTR_TO_IN
2cf0: 54 28 58 29 20 20 28 28 69 6e 74 29 28 58 29 29 T(X) ((int)(X))
2d00: 0a 23 20 65 6e 64 69 66 0a 23 65 6c 73 65 0a 23 .# endif.#else.#
2d10: 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 define SQLITE_I
2d20: 4e 54 5f 54 4f 5f 50 54 52 28 58 29 20 20 20 28 NT_TO_PTR(X) (
2d30: 28 76 6f 69 64 2a 29 26 28 28 63 68 61 72 2a 29 (void*)&((char*)
2d40: 30 29 5b 58 5d 29 0a 23 20 64 65 66 69 6e 65 20 0)[X]).# define
2d50: 53 51 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e SQLITE_PTR_TO_IN
2d60: 54 28 58 29 20 20 20 28 28 69 6e 74 29 28 28 28 T(X) ((int)(((
2d70: 63 68 61 72 2a 29 58 29 2d 28 63 68 61 72 2a 29 char*)X)-(char*)
2d80: 30 29 29 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 0)).#endif.../*.
2d90: 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 54 48 ** The SQLITE_TH
2da0: 52 45 41 44 53 41 46 45 20 6d 61 63 72 6f 20 6d READSAFE macro m
2db0: 75 73 74 20 62 65 20 64 65 66 69 6e 65 64 20 61 ust be defined a
2dc0: 73 20 65 69 74 68 65 72 20 30 20 6f 72 20 31 2e s either 0 or 1.
2dd0: 0a 2a 2a 20 4f 6c 64 65 72 20 76 65 72 73 69 6f .** Older versio
2de0: 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 75 73 65 ns of SQLite use
2df0: 64 20 61 6e 20 6f 70 74 69 6f 6e 61 6c 20 54 48 d an optional TH
2e00: 52 45 41 44 53 41 46 45 20 6d 61 63 72 6f 2e 0a READSAFE macro..
2e10: 2a 2a 20 57 65 20 73 75 70 70 6f 72 74 20 74 68 ** We support th
2e20: 61 74 20 66 6f 72 20 6c 65 67 61 63 79 0a 2a 2f at for legacy.*/
2e30: 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 .#if !defined(SQ
2e40: 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 29 LITE_THREADSAFE)
2e50: 0a 23 69 66 20 64 65 66 69 6e 65 64 28 54 48 52 .#if defined(THR
2e60: 45 41 44 53 41 46 45 29 0a 23 20 64 65 66 69 6e EADSAFE).# defin
2e70: 65 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 e SQLITE_THREADS
2e80: 41 46 45 20 54 48 52 45 41 44 53 41 46 45 0a 23 AFE THREADSAFE.#
2e90: 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 53 51 else.# define SQ
2ea0: 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 LITE_THREADSAFE
2eb0: 31 0a 23 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 1.#endif.#endif.
2ec0: 0a 2f 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 ./*.** The SQLIT
2ed0: 45 5f 44 45 46 41 55 4c 54 5f 4d 45 4d 53 54 41 E_DEFAULT_MEMSTA
2ee0: 54 55 53 20 6d 61 63 72 6f 20 6d 75 73 74 20 62 TUS macro must b
2ef0: 65 20 64 65 66 69 6e 65 64 20 61 73 20 65 69 74 e defined as eit
2f00: 68 65 72 20 30 20 6f 72 20 31 2e 0a 2a 2a 20 49 her 0 or 1..** I
2f10: 74 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68 65 t determines whe
2f20: 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 ther or not the
2f30: 66 65 61 74 75 72 65 73 20 72 65 6c 61 74 65 64 features related
2f40: 20 74 6f 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 43 to .** SQLITE_C
2f50: 4f 4e 46 49 47 5f 4d 45 4d 53 54 41 54 55 53 20 ONFIG_MEMSTATUS
2f60: 61 72 65 20 61 76 61 69 6c 61 62 6c 65 20 62 79 are available by
2f70: 20 64 65 66 61 75 6c 74 20 6f 72 20 6e 6f 74 2e default or not.
2f80: 20 54 68 69 73 20 76 61 6c 75 65 20 63 61 6e 0a This value can.
2f90: 2a 2a 20 62 65 20 6f 76 65 72 72 69 64 64 65 6e ** be overridden
2fa0: 20 61 74 20 72 75 6e 74 69 6d 65 20 75 73 69 6e at runtime usin
2fb0: 67 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63 6f g the sqlite3_co
2fc0: 6e 66 69 67 28 29 20 41 50 49 2e 0a 2a 2f 0a 23 nfig() API..*/.#
2fd0: 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 if !defined(SQLI
2fe0: 54 45 5f 44 45 46 41 55 4c 54 5f 4d 45 4d 53 54 TE_DEFAULT_MEMST
2ff0: 41 54 55 53 29 0a 23 20 64 65 66 69 6e 65 20 53 ATUS).# define S
3000: 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 4d 45 QLITE_DEFAULT_ME
3010: 4d 53 54 41 54 55 53 20 31 0a 23 65 6e 64 69 66 MSTATUS 1.#endif
3020: 0a 0a 2f 2a 0a 2a 2a 20 45 78 61 63 74 6c 79 20 ../*.** Exactly
3030: 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f one of the follo
3040: 77 69 6e 67 20 6d 61 63 72 6f 73 20 6d 75 73 74 wing macros must
3050: 20 62 65 20 64 65 66 69 6e 65 64 20 69 6e 20 6f be defined in o
3060: 72 64 65 72 20 74 6f 0a 2a 2a 20 73 70 65 63 69 rder to.** speci
3070: 66 79 20 77 68 69 63 68 20 6d 65 6d 6f 72 79 20 fy which memory
3080: 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 allocation subsy
3090: 73 74 65 6d 20 74 6f 20 75 73 65 2e 0a 2a 2a 0a stem to use..**.
30a0: 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 53 59 ** SQLITE_SY
30b0: 53 54 45 4d 5f 4d 41 4c 4c 4f 43 20 20 20 20 20 STEM_MALLOC
30c0: 20 20 20 20 20 2f 2f 20 55 73 65 20 6e 6f 72 6d // Use norm
30d0: 61 6c 20 73 79 73 74 65 6d 20 6d 61 6c 6c 6f 63 al system malloc
30e0: 28 29 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 ().** SQLITE
30f0: 5f 4d 45 4d 44 45 42 55 47 20 20 20 20 20 20 20 _MEMDEBUG
3100: 20 20 20 20 20 20 20 20 2f 2f 20 44 65 62 75 67 // Debug
3110: 67 69 6e 67 20 76 65 72 73 69 6f 6e 20 6f 66 20 ging version of
3120: 73 79 73 74 65 6d 20 6d 61 6c 6c 6f 63 28 29 0a system malloc().
3130: 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 4d 45 ** SQLITE_ME
3140: 4d 4f 52 59 5f 53 49 5a 45 20 20 20 20 20 20 20 MORY_SIZE
3150: 20 20 20 20 20 2f 2f 20 69 6e 74 65 72 6e 61 6c // internal
3160: 20 61 6c 6c 6f 63 61 74 6f 72 20 23 31 0a 2a 2a allocator #1.**
3170: 20 20 20 20 20 53 51 4c 49 54 45 5f 4d 4d 41 50 SQLITE_MMAP
3180: 5f 48 45 41 50 5f 53 49 5a 45 20 20 20 20 20 20 _HEAP_SIZE
3190: 20 20 20 2f 2f 20 69 6e 74 65 72 6e 61 6c 20 6d // internal m
31a0: 6d 61 70 28 29 20 61 6c 6c 6f 63 61 74 6f 72 0a map() allocator.
31b0: 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 50 4f ** SQLITE_PO
31c0: 57 32 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 20 20 W2_MEMORY_SIZE
31d0: 20 20 20 20 20 2f 2f 20 69 6e 74 65 72 6e 61 6c // internal
31e0: 20 70 6f 77 65 72 2d 6f 66 2d 74 77 6f 20 61 6c power-of-two al
31f0: 6c 6f 63 61 74 6f 72 0a 2a 2a 0a 2a 2a 20 49 66 locator.**.** If
3200: 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 61 62 6f none of the abo
3210: 76 65 20 61 72 65 20 64 65 66 69 6e 65 64 2c 20 ve are defined,
3220: 74 68 65 6e 20 73 65 74 20 53 51 4c 49 54 45 5f then set SQLITE_
3230: 53 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43 20 61 73 SYSTEM_MALLOC as
3240: 0a 2a 2a 20 74 68 65 20 64 65 66 61 75 6c 74 2e .** the default.
3250: 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 .*/.#if defined(
3260: 53 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41 SQLITE_SYSTEM_MA
3270: 4c 4c 4f 43 29 2b 64 65 66 69 6e 65 64 28 53 51 LLOC)+defined(SQ
3280: 4c 49 54 45 5f 4d 45 4d 44 45 42 55 47 29 2b 5c LITE_MEMDEBUG)+\
3290: 0a 20 20 20 20 64 65 66 69 6e 65 64 28 53 51 4c . defined(SQL
32a0: 49 54 45 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 29 ITE_MEMORY_SIZE)
32b0: 2b 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f +defined(SQLITE_
32c0: 4d 4d 41 50 5f 48 45 41 50 5f 53 49 5a 45 29 2b MMAP_HEAP_SIZE)+
32d0: 5c 0a 20 20 20 20 64 65 66 69 6e 65 64 28 53 51 \. defined(SQ
32e0: 4c 49 54 45 5f 50 4f 57 32 5f 4d 45 4d 4f 52 59 LITE_POW2_MEMORY
32f0: 5f 53 49 5a 45 29 3e 31 0a 23 20 65 72 72 6f 72 _SIZE)>1.# error
3300: 20 22 41 74 20 6d 6f 73 74 20 6f 6e 65 20 6f 66 "At most one of
3310: 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 the following c
3320: 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 63 6f 6e 66 ompile-time conf
3330: 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e iguration option
3340: 73 5c 0a 20 69 73 20 61 6c 6c 6f 77 73 3a 20 53 s\. is allows: S
3350: 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41 4c QLITE_SYSTEM_MAL
3360: 4c 4f 43 2c 20 53 51 4c 49 54 45 5f 4d 45 4d 44 LOC, SQLITE_MEMD
3370: 45 42 55 47 2c 20 53 51 4c 49 54 45 5f 4d 45 4d EBUG, SQLITE_MEM
3380: 4f 52 59 5f 53 49 5a 45 2c 5c 0a 20 53 51 4c 49 ORY_SIZE,\. SQLI
3390: 54 45 5f 4d 4d 41 50 5f 48 45 41 50 5f 53 49 5a TE_MMAP_HEAP_SIZ
33a0: 45 2c 20 53 51 4c 49 54 45 5f 50 4f 57 32 5f 4d E, SQLITE_POW2_M
33b0: 45 4d 4f 52 59 5f 53 49 5a 45 22 0a 23 65 6e 64 EMORY_SIZE".#end
33c0: 69 66 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 if.#if defined(S
33d0: 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41 4c QLITE_SYSTEM_MAL
33e0: 4c 4f 43 29 2b 64 65 66 69 6e 65 64 28 53 51 4c LOC)+defined(SQL
33f0: 49 54 45 5f 4d 45 4d 44 45 42 55 47 29 2b 5c 0a ITE_MEMDEBUG)+\.
3400: 20 20 20 20 64 65 66 69 6e 65 64 28 53 51 4c 49 defined(SQLI
3410: 54 45 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 29 2b TE_MEMORY_SIZE)+
3420: 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d defined(SQLITE_M
3430: 4d 41 50 5f 48 45 41 50 5f 53 49 5a 45 29 2b 5c MAP_HEAP_SIZE)+\
3440: 0a 20 20 20 20 64 65 66 69 6e 65 64 28 53 51 4c . defined(SQL
3450: 49 54 45 5f 50 4f 57 32 5f 4d 45 4d 4f 52 59 5f ITE_POW2_MEMORY_
3460: 53 49 5a 45 29 3d 3d 30 0a 23 20 64 65 66 69 6e SIZE)==0.# defin
3470: 65 20 53 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f e SQLITE_SYSTEM_
3480: 4d 41 4c 4c 4f 43 20 31 0a 23 65 6e 64 69 66 0a MALLOC 1.#endif.
3490: 0a 2f 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 ./*.** If SQLITE
34a0: 5f 4d 41 4c 4c 4f 43 5f 53 4f 46 54 5f 4c 49 4d _MALLOC_SOFT_LIM
34b0: 49 54 20 69 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 IT is not zero,
34c0: 74 68 65 6e 20 74 72 79 20 74 6f 20 6b 65 65 70 then try to keep
34d0: 20 74 68 65 0a 2a 2a 20 73 69 7a 65 73 20 6f 66 the.** sizes of
34e0: 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
34f0: 6f 6e 73 20 62 65 6c 6f 77 20 74 68 69 73 20 76 ons below this v
3500: 61 6c 75 65 20 77 68 65 72 65 20 70 6f 73 73 69 alue where possi
3510: 62 6c 65 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 ble..*/.#if !def
3520: 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d 41 4c 4c ined(SQLITE_MALL
3530: 4f 43 5f 53 4f 46 54 5f 4c 49 4d 49 54 29 0a 23 OC_SOFT_LIMIT).#
3540: 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d define SQLITE_M
3550: 41 4c 4c 4f 43 5f 53 4f 46 54 5f 4c 49 4d 49 54 ALLOC_SOFT_LIMIT
3560: 20 31 30 32 34 0a 23 65 6e 64 69 66 0a 0a 2f 2a 1024.#endif../*
3570: 0a 2a 2a 20 57 65 20 6e 65 65 64 20 74 6f 20 64 .** We need to d
3580: 65 66 69 6e 65 20 5f 58 4f 50 45 4e 5f 53 4f 55 efine _XOPEN_SOU
3590: 52 43 45 20 61 73 20 66 6f 6c 6c 6f 77 73 20 69 RCE as follows i
35a0: 6e 20 6f 72 64 65 72 20 74 6f 20 65 6e 61 62 6c n order to enabl
35b0: 65 0a 2a 2a 20 72 65 63 75 72 73 69 76 65 20 6d e.** recursive m
35c0: 75 74 65 78 65 73 20 6f 6e 20 6d 6f 73 74 20 55 utexes on most U
35d0: 6e 69 78 20 73 79 73 74 65 6d 73 2e 20 20 42 75 nix systems. Bu
35e0: 74 20 4d 61 63 20 4f 53 20 58 20 69 73 20 64 69 t Mac OS X is di
35f0: 66 66 65 72 65 6e 74 2e 0a 2a 2a 20 54 68 65 20 fferent..** The
3600: 5f 58 4f 50 45 4e 5f 53 4f 55 52 43 45 20 64 65 _XOPEN_SOURCE de
3610: 66 69 6e 65 20 63 61 75 73 65 73 20 70 72 6f 62 fine causes prob
3620: 6c 65 6d 73 20 66 6f 72 20 4d 61 63 20 4f 53 20 lems for Mac OS
3630: 58 20 77 65 20 61 72 65 20 74 6f 6c 64 2c 0a 2a X we are told,.*
3640: 2a 20 73 6f 20 69 74 20 69 73 20 6f 6d 69 74 74 * so it is omitt
3650: 65 64 20 74 68 65 72 65 2e 20 20 53 65 65 20 74 ed there. See t
3660: 69 63 6b 65 74 20 23 32 36 37 33 2e 0a 2a 2a 0a icket #2673..**.
3670: 2a 2a 20 4c 61 74 65 72 20 77 65 20 6c 65 61 72 ** Later we lear
3680: 6e 20 74 68 61 74 20 5f 58 4f 50 45 4e 5f 53 4f n that _XOPEN_SO
3690: 55 52 43 45 20 69 73 20 70 6f 6f 72 6c 79 20 6f URCE is poorly o
36a0: 72 20 69 6e 63 6f 72 72 65 63 74 6c 79 0a 2a 2a r incorrectly.**
36b0: 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 6f 6e 20 implemented on
36c0: 73 6f 6d 65 20 73 79 73 74 65 6d 73 2e 20 20 53 some systems. S
36d0: 6f 20 77 65 20 61 76 6f 69 64 20 64 65 66 69 6e o we avoid defin
36e0: 69 6e 67 20 69 74 20 61 74 20 61 6c 6c 0a 2a 2a ing it at all.**
36f0: 20 69 66 20 69 74 20 69 73 20 61 6c 72 65 61 64 if it is alread
3700: 79 20 64 65 66 69 6e 65 64 20 6f 72 20 69 66 20 y defined or if
3710: 69 74 20 69 73 20 75 6e 6e 65 65 64 65 64 20 62 it is unneeded b
3720: 65 63 61 75 73 65 20 77 65 20 61 72 65 0a 2a 2a ecause we are.**
3730: 20 6e 6f 74 20 64 6f 69 6e 67 20 61 20 74 68 72 not doing a thr
3740: 65 61 64 73 61 66 65 20 62 75 69 6c 64 2e 20 20 eadsafe build.
3750: 54 69 63 6b 65 74 20 23 32 36 38 31 2e 0a 2a 2a Ticket #2681..**
3760: 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 74 69 63 .** See also tic
3770: 6b 65 74 20 23 32 37 34 31 2e 0a 2a 2f 0a 23 69 ket #2741..*/.#i
3780: 66 20 21 64 65 66 69 6e 65 64 28 5f 58 4f 50 45 f !defined(_XOPE
3790: 4e 5f 53 4f 55 52 43 45 29 20 26 26 20 21 64 65 N_SOURCE) && !de
37a0: 66 69 6e 65 64 28 5f 5f 44 41 52 57 49 4e 5f 5f fined(__DARWIN__
37b0: 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 5f 5f ) && !defined(__
37c0: 41 50 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c 49 APPLE__) && SQLI
37d0: 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a 23 20 TE_THREADSAFE.#
37e0: 20 64 65 66 69 6e 65 20 5f 58 4f 50 45 4e 5f 53 define _XOPEN_S
37f0: 4f 55 52 43 45 20 35 30 30 20 20 2f 2a 20 4e 65 OURCE 500 /* Ne
3800: 65 64 65 64 20 74 6f 20 65 6e 61 62 6c 65 20 70 eded to enable p
3810: 74 68 72 65 61 64 20 72 65 63 75 72 73 69 76 65 thread recursive
3820: 20 6d 75 74 65 78 65 73 20 2a 2f 0a 23 65 6e 64 mutexes */.#end
3830: 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 54 43 if../*.** The TC
3840: 4c 20 68 65 61 64 65 72 73 20 61 72 65 20 6f 6e L headers are on
3850: 6c 79 20 6e 65 65 64 65 64 20 77 68 65 6e 20 63 ly needed when c
3860: 6f 6d 70 69 6c 69 6e 67 20 74 68 65 20 54 43 4c ompiling the TCL
3870: 20 62 69 6e 64 69 6e 67 73 2e 0a 2a 2f 0a 23 69 bindings..*/.#i
3880: 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 f defined(SQLITE
3890: 5f 54 43 4c 29 20 7c 7c 20 64 65 66 69 6e 65 64 _TCL) || defined
38a0: 28 54 43 4c 53 48 29 0a 23 20 69 6e 63 6c 75 64 (TCLSH).# includ
38b0: 65 20 3c 74 63 6c 2e 68 3e 0a 23 65 6e 64 69 66 e <tcl.h>.#endif
38c0: 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6e 79 20 70 65 6f ../*.** Many peo
38d0: 70 6c 65 20 61 72 65 20 66 61 69 6c 69 6e 67 20 ple are failing
38e0: 74 6f 20 73 65 74 20 2d 44 4e 44 45 42 55 47 3d to set -DNDEBUG=
38f0: 31 20 77 68 65 6e 20 63 6f 6d 70 69 6c 69 6e 67 1 when compiling
3900: 20 53 51 4c 69 74 65 2e 0a 2a 2a 20 53 65 74 74 SQLite..** Sett
3910: 69 6e 67 20 4e 44 45 42 55 47 20 6d 61 6b 65 73 ing NDEBUG makes
3920: 20 74 68 65 20 63 6f 64 65 20 73 6d 61 6c 6c 65 the code smalle
3930: 72 20 61 6e 64 20 72 75 6e 20 66 61 73 74 65 72 r and run faster
3940: 2e 20 20 53 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 . So the follow
3950: 69 6e 67 0a 2a 2a 20 6c 69 6e 65 73 20 61 72 65 ing.** lines are
3960: 20 61 64 64 65 64 20 74 6f 20 61 75 74 6f 6d 61 added to automa
3970: 74 69 63 61 6c 6c 79 20 73 65 74 20 4e 44 45 42 tically set NDEB
3980: 55 47 20 75 6e 6c 65 73 73 20 74 68 65 20 2d 44 UG unless the -D
3990: 53 51 4c 49 54 45 5f 44 45 42 55 47 3d 31 0a 2a SQLITE_DEBUG=1.*
39a0: 2a 20 6f 70 74 69 6f 6e 20 69 73 20 73 65 74 2e * option is set.
39b0: 20 20 54 68 75 73 20 4e 44 45 42 55 47 20 62 65 Thus NDEBUG be
39c0: 63 6f 6d 65 73 20 61 6e 20 6f 70 74 2d 69 6e 20 comes an opt-in
39d0: 72 61 74 68 65 72 20 74 68 61 6e 20 61 6e 20 6f rather than an o
39e0: 70 74 2d 6f 75 74 0a 2a 2a 20 66 65 61 74 75 72 pt-out.** featur
39f0: 65 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e e..*/.#if !defin
3a00: 65 64 28 4e 44 45 42 55 47 29 20 26 26 20 21 64 ed(NDEBUG) && !d
3a10: 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 efined(SQLITE_DE
3a20: 42 55 47 29 20 0a 23 20 64 65 66 69 6e 65 20 4e BUG) .# define N
3a30: 44 45 42 55 47 20 31 0a 23 65 6e 64 69 66 0a 0a DEBUG 1.#endif..
3a40: 2f 2a 0a 2a 2a 20 54 68 65 20 74 65 73 74 63 61 /*.** The testca
3a50: 73 65 28 29 20 6d 61 63 72 6f 20 69 73 20 75 73 se() macro is us
3a60: 65 64 20 74 6f 20 61 69 64 20 69 6e 20 63 6f 76 ed to aid in cov
3a70: 65 72 61 67 65 20 74 65 73 74 69 6e 67 2e 20 20 erage testing.
3a80: 57 68 65 6e 20 0a 2a 2a 20 64 6f 69 6e 67 20 63 When .** doing c
3a90: 6f 76 65 72 61 67 65 20 74 65 73 74 69 6e 67 2c overage testing,
3aa0: 20 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e 20 69 the condition i
3ab0: 6e 73 69 64 65 20 74 68 65 20 61 72 67 75 6d 65 nside the argume
3ac0: 6e 74 20 74 6f 0a 2a 2a 20 74 65 73 74 63 61 73 nt to.** testcas
3ad0: 65 28 29 20 6d 75 73 74 20 62 65 20 65 76 61 6c e() must be eval
3ae0: 75 61 74 65 64 20 62 6f 74 68 20 74 72 75 65 20 uated both true
3af0: 61 6e 64 20 66 61 6c 73 65 20 69 6e 20 6f 72 64 and false in ord
3b00: 65 72 20 74 6f 0a 2a 2a 20 67 65 74 20 66 75 6c er to.** get ful
3b10: 6c 20 62 72 61 6e 63 68 20 63 6f 76 65 72 61 67 l branch coverag
3b20: 65 2e 20 20 54 68 65 20 74 65 73 74 63 61 73 65 e. The testcase
3b30: 28 29 20 6d 61 63 72 6f 20 69 73 20 69 6e 73 65 () macro is inse
3b40: 72 74 65 64 0a 2a 2a 20 74 6f 20 68 65 6c 70 20 rted.** to help
3b50: 65 6e 73 75 72 65 20 61 64 65 71 75 61 74 65 20 ensure adequate
3b60: 74 65 73 74 20 63 6f 76 65 72 61 67 65 20 69 6e test coverage in
3b70: 20 70 6c 61 63 65 73 20 77 68 65 72 65 20 73 69 places where si
3b80: 6d 70 6c 65 0a 2a 2a 20 63 6f 6e 64 69 74 69 6f mple.** conditio
3b90: 6e 2f 64 65 63 69 73 69 6f 6e 20 63 6f 76 65 72 n/decision cover
3ba0: 61 67 65 20 69 73 20 69 6e 61 64 65 71 75 61 74 age is inadequat
3bb0: 65 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c e. For example,
3bc0: 20 74 65 73 74 63 61 73 65 28 29 0a 2a 2a 20 63 testcase().** c
3bd0: 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 6d 61 an be used to ma
3be0: 6b 65 20 73 75 72 65 20 62 6f 75 6e 64 61 72 79 ke sure boundary
3bf0: 20 76 61 6c 75 65 73 20 61 72 65 20 74 65 73 74 values are test
3c00: 65 64 2e 20 20 46 6f 72 0a 2a 2a 20 62 69 74 6d ed. For.** bitm
3c10: 61 73 6b 20 74 65 73 74 73 2c 20 74 65 73 74 63 ask tests, testc
3c20: 61 73 65 28 29 20 63 61 6e 20 62 65 20 75 73 65 ase() can be use
3c30: 64 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 65 d to make sure e
3c40: 61 63 68 20 62 69 74 0a 2a 2a 20 69 73 20 73 69 ach bit.** is si
3c50: 67 6e 69 66 69 63 61 6e 74 20 61 6e 64 20 75 73 gnificant and us
3c60: 65 64 20 61 74 20 6c 65 61 73 74 20 6f 6e 63 65 ed at least once
3c70: 2e 20 20 4f 6e 20 73 77 69 74 63 68 20 73 74 61 . On switch sta
3c80: 74 65 6d 65 6e 74 73 0a 2a 2a 20 77 68 65 72 65 tements.** where
3c90: 20 6d 75 6c 74 69 70 6c 65 20 63 61 73 65 73 20 multiple cases
3ca0: 67 6f 20 74 6f 20 74 68 65 20 73 61 6d 65 20 62 go to the same b
3cb0: 6c 6f 63 6b 20 6f 66 20 63 6f 64 65 2c 20 74 65 lock of code, te
3cc0: 73 74 63 61 73 65 28 29 0a 2a 2a 20 63 61 6e 20 stcase().** can
3cd0: 69 6e 73 75 72 65 20 74 68 61 74 20 61 6c 6c 20 insure that all
3ce0: 63 61 73 65 73 20 61 72 65 20 65 76 61 6c 75 61 cases are evalua
3cf0: 74 65 64 2e 0a 2a 2a 0a 2a 2f 0a 23 69 66 64 65 ted..**.*/.#ifde
3d00: 66 20 53 51 4c 49 54 45 5f 43 4f 56 45 52 41 47 f SQLITE_COVERAG
3d10: 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 52 E_TEST.SQLITE_PR
3d20: 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c IVATE void sql
3d30: 69 74 65 33 43 6f 76 65 72 61 67 65 28 69 6e 74 ite3Coverage(int
3d40: 29 3b 0a 23 20 64 65 66 69 6e 65 20 74 65 73 74 );.# define test
3d50: 63 61 73 65 28 58 29 20 20 69 66 28 20 58 20 29 case(X) if( X )
3d60: 7b 20 73 71 6c 69 74 65 33 43 6f 76 65 72 61 67 { sqlite3Coverag
3d70: 65 28 5f 5f 4c 49 4e 45 5f 5f 29 3b 20 7d 0a 23 e(__LINE__); }.#
3d80: 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 74 65 else.# define te
3d90: 73 74 63 61 73 65 28 58 29 0a 23 65 6e 64 69 66 stcase(X).#endif
3da0: 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 54 45 53 54 ../*.** The TEST
3db0: 4f 4e 4c 59 20 6d 61 63 72 6f 20 69 73 20 75 73 ONLY macro is us
3dc0: 65 64 20 74 6f 20 65 6e 63 6c 6f 73 65 20 76 61 ed to enclose va
3dd0: 72 69 61 62 6c 65 20 64 65 63 6c 61 72 61 74 69 riable declarati
3de0: 6f 6e 73 20 6f 72 0a 2a 2a 20 6f 74 68 65 72 20 ons or.** other
3df0: 62 69 74 73 20 6f 66 20 63 6f 64 65 20 74 68 61 bits of code tha
3e00: 74 20 61 72 65 20 6e 65 65 64 65 64 20 74 6f 20 t are needed to
3e10: 73 75 70 70 6f 72 74 20 74 68 65 20 61 72 67 75 support the argu
3e20: 6d 65 6e 74 73 0a 2a 2a 20 77 69 74 68 69 6e 20 ments.** within
3e30: 74 65 73 74 63 61 73 65 28 29 20 61 6e 64 20 61 testcase() and a
3e40: 73 73 65 72 74 28 29 20 6d 61 63 72 6f 73 2e 0a ssert() macros..
3e50: 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 */.#if !defined(
3e60: 4e 44 45 42 55 47 29 20 7c 7c 20 64 65 66 69 6e NDEBUG) || defin
3e70: 65 64 28 53 51 4c 49 54 45 5f 43 4f 56 45 52 41 ed(SQLITE_COVERA
3e80: 47 45 5f 54 45 53 54 29 0a 23 20 64 65 66 69 6e GE_TEST).# defin
3e90: 65 20 54 45 53 54 4f 4e 4c 59 28 58 29 20 20 58 e TESTONLY(X) X
3ea0: 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 .#else.# define
3eb0: 54 45 53 54 4f 4e 4c 59 28 58 29 0a 23 65 6e 64 TESTONLY(X).#end
3ec0: 69 66 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 74 69 if../*.** Someti
3ed0: 6d 65 73 20 77 65 20 6e 65 65 64 20 61 20 73 6d mes we need a sm
3ee0: 61 6c 6c 20 61 6d 6f 75 6e 74 20 6f 66 20 63 6f all amount of co
3ef0: 64 65 20 73 75 63 68 20 61 73 20 61 20 76 61 72 de such as a var
3f00: 69 61 62 6c 65 20 69 6e 69 74 69 61 6c 69 7a 61 iable initializa
3f10: 74 69 6f 6e 0a 2a 2a 20 74 6f 20 73 65 74 75 70 tion.** to setup
3f20: 20 66 6f 72 20 61 20 6c 61 74 65 72 20 61 73 73 for a later ass
3f30: 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 2e ert() statement.
3f40: 20 20 57 65 20 64 6f 20 6e 6f 74 20 77 61 6e 74 We do not want
3f50: 20 74 68 69 73 20 63 6f 64 65 20 74 6f 0a 2a 2a this code to.**
3f60: 20 61 70 70 65 61 72 20 77 68 65 6e 20 61 73 73 appear when ass
3f70: 65 72 74 28 29 20 69 73 20 64 69 73 61 62 6c 65 ert() is disable
3f80: 64 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e d. The followin
3f90: 67 20 6d 61 63 72 6f 20 69 73 20 74 68 65 72 65 g macro is there
3fa0: 66 6f 72 65 0a 2a 2a 20 75 73 65 64 20 74 6f 20 fore.** used to
3fb0: 63 6f 6e 74 61 69 6e 20 74 68 61 74 20 73 65 74 contain that set
3fc0: 75 70 20 63 6f 64 65 2e 20 20 54 68 65 20 22 56 up code. The "V
3fd0: 56 41 22 20 61 63 72 6f 6e 79 6d 20 73 74 61 6e VA" acronym stan
3fe0: 64 73 20 66 6f 72 0a 2a 2a 20 22 56 65 72 69 66 ds for.** "Verif
3ff0: 69 63 61 74 69 6f 6e 2c 20 56 61 6c 69 64 61 74 ication, Validat
4000: 69 6f 6e 2c 20 61 6e 64 20 41 63 63 72 65 64 69 ion, and Accredi
4010: 74 61 74 69 6f 6e 22 2e 20 20 49 6e 20 6f 74 68 tation". In oth
4020: 65 72 20 77 6f 72 64 73 2c 20 74 68 65 0a 2a 2a er words, the.**
4030: 20 63 6f 64 65 20 77 69 74 68 69 6e 20 56 56 41 code within VVA
4040: 5f 4f 4e 4c 59 28 29 20 77 69 6c 6c 20 6f 6e 6c _ONLY() will onl
4050: 79 20 72 75 6e 20 64 75 72 69 6e 67 20 76 65 72 y run during ver
4060: 69 66 69 63 61 74 69 6f 6e 20 70 72 6f 63 65 73 ification proces
4070: 73 65 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 ses..*/.#ifndef
4080: 4e 44 45 42 55 47 0a 23 20 64 65 66 69 6e 65 20 NDEBUG.# define
4090: 56 56 41 5f 4f 4e 4c 59 28 58 29 20 20 58 0a 23 VVA_ONLY(X) X.#
40a0: 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 56 56 else.# define VV
40b0: 41 5f 4f 4e 4c 59 28 58 29 0a 23 65 6e 64 69 66 A_ONLY(X).#endif
40c0: 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 41 4c 57 41 ../*.** The ALWA
40d0: 59 53 20 61 6e 64 20 4e 45 56 45 52 20 6d 61 63 YS and NEVER mac
40e0: 72 6f 73 20 73 75 72 72 6f 75 6e 64 20 62 6f 6f ros surround boo
40f0: 6c 65 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 73 lean expressions
4100: 20 77 68 69 63 68 20 0a 2a 2a 20 61 72 65 20 69 which .** are i
4110: 6e 74 65 6e 64 65 64 20 74 6f 20 61 6c 77 61 79 ntended to alway
4120: 73 20 62 65 20 74 72 75 65 20 6f 72 20 66 61 6c s be true or fal
4130: 73 65 2c 20 72 65 73 70 65 63 74 69 76 65 6c 79 se, respectively
4140: 2e 20 20 53 75 63 68 0a 2a 2a 20 65 78 70 72 65 . Such.** expre
4150: 73 73 69 6f 6e 73 20 63 6f 75 6c 64 20 62 65 20 ssions could be
4160: 6f 6d 69 74 74 65 64 20 66 72 6f 6d 20 74 68 65 omitted from the
4170: 20 63 6f 64 65 20 63 6f 6d 70 6c 65 74 65 6c 79 code completely
4180: 2e 20 20 42 75 74 20 74 68 65 79 0a 2a 2a 20 61 . But they.** a
4190: 72 65 20 69 6e 63 6c 75 64 65 64 20 69 6e 20 61 re included in a
41a0: 20 66 65 77 20 63 61 73 65 73 20 69 6e 20 6f 72 few cases in or
41b0: 64 65 72 20 74 6f 20 65 6e 68 61 6e 63 65 20 74 der to enhance t
41c0: 68 65 20 72 65 73 69 6c 69 65 6e 63 65 0a 2a 2a he resilience.**
41d0: 20 6f 66 20 53 51 4c 69 74 65 20 74 6f 20 75 6e of SQLite to un
41e0: 65 78 70 65 63 74 65 64 20 62 65 68 61 76 69 6f expected behavio
41f0: 72 20 2d 20 74 6f 20 6d 61 6b 65 20 74 68 65 20 r - to make the
4200: 63 6f 64 65 20 22 73 65 6c 66 2d 68 65 61 6c 69 code "self-heali
4210: 6e 67 22 0a 2a 2a 20 6f 72 20 22 64 75 63 74 69 ng".** or "ducti
4220: 6c 65 22 20 72 61 74 68 65 72 20 74 68 61 6e 20 le" rather than
4230: 62 65 69 6e 67 20 22 62 72 69 74 74 6c 65 22 20 being "brittle"
4240: 61 6e 64 20 63 72 61 73 68 69 6e 67 20 61 74 20 and crashing at
4250: 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 68 69 6e the first.** hin
4260: 74 20 6f 66 20 75 6e 70 6c 61 6e 6e 65 64 20 62 t of unplanned b
4270: 65 68 61 76 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20 49 ehavior..**.** I
4280: 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 41 n other words, A
4290: 4c 57 41 59 53 20 61 6e 64 20 4e 45 56 45 52 20 LWAYS and NEVER
42a0: 61 72 65 20 61 64 64 65 64 20 66 6f 72 20 64 65 are added for de
42b0: 66 65 6e 73 69 76 65 20 63 6f 64 65 2e 0a 2a 2a fensive code..**
42c0: 0a 2a 2a 20 57 68 65 6e 20 64 6f 69 6e 67 20 63 .** When doing c
42d0: 6f 76 65 72 61 67 65 20 74 65 73 74 69 6e 67 20 overage testing
42e0: 41 4c 57 41 59 53 20 61 6e 64 20 4e 45 56 45 52 ALWAYS and NEVER
42f0: 20 61 72 65 20 68 61 72 64 2d 63 6f 64 65 64 20 are hard-coded
4300: 74 6f 0a 2a 2a 20 62 65 20 74 72 75 65 20 61 6e to.** be true an
4310: 64 20 66 61 6c 73 65 20 73 6f 20 74 68 61 74 20 d false so that
4320: 74 68 65 20 75 6e 72 65 61 63 68 61 62 6c 65 20 the unreachable
4330: 63 6f 64 65 20 74 68 65 6e 20 73 70 65 63 69 66 code then specif
4340: 79 20 77 69 6c 6c 0a 2a 2a 20 6e 6f 74 20 62 65 y will.** not be
4350: 20 63 6f 75 6e 74 65 64 20 61 73 20 75 6e 74 65 counted as unte
4360: 73 74 65 64 20 63 6f 64 65 2e 0a 2a 2f 0a 23 69 sted code..*/.#i
4370: 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 f defined(SQLITE
4380: 5f 43 4f 56 45 52 41 47 45 5f 54 45 53 54 29 0a _COVERAGE_TEST).
4390: 23 20 64 65 66 69 6e 65 20 41 4c 57 41 59 53 28 # define ALWAYS(
43a0: 58 29 20 20 20 20 20 20 28 31 29 0a 23 20 64 65 X) (1).# de
43b0: 66 69 6e 65 20 4e 45 56 45 52 28 58 29 20 20 20 fine NEVER(X)
43c0: 20 20 20 20 28 30 29 0a 23 65 6c 69 66 20 21 64 (0).#elif !d
43d0: 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 0a 23 efined(NDEBUG).#
43e0: 20 64 65 66 69 6e 65 20 41 4c 57 41 59 53 28 58 define ALWAYS(X
43f0: 29 20 20 20 20 20 20 28 28 58 29 3f 31 3a 28 61 ) ((X)?1:(a
4400: 73 73 65 72 74 28 30 29 2c 30 29 29 0a 23 20 64 ssert(0),0)).# d
4410: 65 66 69 6e 65 20 4e 45 56 45 52 28 58 29 20 20 efine NEVER(X)
4420: 20 20 20 20 20 28 28 58 29 3f 28 61 73 73 65 72 ((X)?(asser
4430: 74 28 30 29 2c 31 29 3a 30 29 0a 23 65 6c 73 65 t(0),1):0).#else
4440: 0a 23 20 64 65 66 69 6e 65 20 41 4c 57 41 59 53 .# define ALWAYS
4450: 28 58 29 20 20 20 20 20 20 28 58 29 0a 23 20 64 (X) (X).# d
4460: 65 66 69 6e 65 20 4e 45 56 45 52 28 58 29 20 20 efine NEVER(X)
4470: 20 20 20 20 20 28 58 29 0a 23 65 6e 64 69 66 0a (X).#endif.
4480: 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 63 72 6f ./*.** The macro
4490: 20 75 6e 6c 69 6b 65 6c 79 28 29 20 69 73 20 61 unlikely() is a
44a0: 20 68 69 6e 74 20 74 68 61 74 20 73 75 72 72 6f hint that surro
44b0: 75 6e 64 73 20 61 20 62 6f 6f 6c 65 61 6e 0a 2a unds a boolean.*
44c0: 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61 * expression tha
44d0: 74 20 69 73 20 75 73 75 61 6c 6c 79 20 66 61 6c t is usually fal
44e0: 73 65 2e 20 20 4d 61 63 72 6f 20 6c 69 6b 65 6c se. Macro likel
44f0: 79 28 29 20 73 75 72 72 6f 75 6e 64 73 0a 2a 2a y() surrounds.**
4500: 20 61 20 62 6f 6f 6c 65 61 6e 20 65 78 70 72 65 a boolean expre
4510: 73 73 69 6f 6e 20 74 68 61 74 20 69 73 20 75 73 ssion that is us
4520: 75 61 6c 6c 79 20 74 72 75 65 2e 20 20 47 43 43 ually true. GCC
4530: 20 69 73 20 61 62 6c 65 20 74 6f 0a 2a 2a 20 75 is able to.** u
4540: 73 65 20 74 68 65 73 65 20 68 69 6e 74 73 20 74 se these hints t
4550: 6f 20 67 65 6e 65 72 61 74 65 20 62 65 74 74 65 o generate bette
4560: 72 20 63 6f 64 65 2c 20 73 6f 6d 65 74 69 6d 65 r code, sometime
4570: 73 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 s..*/.#if define
4580: 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20 30 d(__GNUC__) && 0
4590: 0a 23 20 64 65 66 69 6e 65 20 6c 69 6b 65 6c 79 .# define likely
45a0: 28 58 29 20 20 20 20 5f 5f 62 75 69 6c 74 69 6e (X) __builtin
45b0: 5f 65 78 70 65 63 74 28 28 58 29 2c 31 29 0a 23 _expect((X),1).#
45c0: 20 64 65 66 69 6e 65 20 75 6e 6c 69 6b 65 6c 79 define unlikely
45d0: 28 58 29 20 20 5f 5f 62 75 69 6c 74 69 6e 5f 65 (X) __builtin_e
45e0: 78 70 65 63 74 28 28 58 29 2c 30 29 0a 23 65 6c xpect((X),0).#el
45f0: 73 65 0a 23 20 64 65 66 69 6e 65 20 6c 69 6b 65 se.# define like
4600: 6c 79 28 58 29 20 20 20 20 21 21 28 58 29 0a 23 ly(X) !!(X).#
4610: 20 64 65 66 69 6e 65 20 75 6e 6c 69 6b 65 6c 79 define unlikely
4620: 28 58 29 20 20 21 21 28 58 29 0a 23 65 6e 64 69 (X) !!(X).#endi
4630: 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a f../************
4640: 2a 2a 20 49 6e 63 6c 75 64 65 20 73 71 6c 69 74 ** Include sqlit
4650: 65 33 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 e3.h in the midd
4660: 6c 65 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e le of sqliteInt.
4670: 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a h **************
4680: 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
4690: 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 73 71 ** Begin file sq
46a0: 6c 69 74 65 33 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a lite3.h ********
46b0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
46c0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
46d0: 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 */./*.** 2001 Se
46e0: 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a ptember 15.**.**
46f0: 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 The author disc
4700: 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 laims copyright
4710: 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 to this source c
4720: 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f ode. In place o
4730: 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 f.** a legal not
4740: 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 ice, here is a b
4750: 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 lessing:.**.**
4760: 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f May you do goo
4770: 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a d and not evil..
4780: 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 ** May you fi
4790: 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 nd forgiveness f
47a0: 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 or yourself and
47b0: 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a forgive others..
47c0: 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 ** May you sh
47d0: 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 are freely, neve
47e0: 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 r taking more th
47f0: 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a an you give..**.
4800: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4810: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4820: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4830: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4840: 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 *********.** Thi
4850: 73 20 68 65 61 64 65 72 20 66 69 6c 65 20 64 65 s header file de
4860: 66 69 6e 65 73 20 74 68 65 20 69 6e 74 65 72 66 fines the interf
4870: 61 63 65 20 74 68 61 74 20 74 68 65 20 53 51 4c ace that the SQL
4880: 69 74 65 20 6c 69 62 72 61 72 79 0a 2a 2a 20 70 ite library.** p
4890: 72 65 73 65 6e 74 73 20 74 6f 20 63 6c 69 65 6e resents to clien
48a0: 74 20 70 72 6f 67 72 61 6d 73 2e 20 20 49 66 20 t programs. If
48b0: 61 20 43 2d 66 75 6e 63 74 69 6f 6e 2c 20 73 74 a C-function, st
48c0: 72 75 63 74 75 72 65 2c 20 64 61 74 61 74 79 70 ructure, datatyp
48d0: 65 2c 0a 2a 2a 20 6f 72 20 63 6f 6e 73 74 61 6e e,.** or constan
48e0: 74 20 64 65 66 69 6e 69 74 69 6f 6e 20 64 6f 65 t definition doe
48f0: 73 20 6e 6f 74 20 61 70 70 65 61 72 20 69 6e 20 s not appear in
4900: 74 68 69 73 20 66 69 6c 65 2c 20 74 68 65 6e 20 this file, then
4910: 69 74 20 69 73 0a 2a 2a 20 6e 6f 74 20 61 20 70 it is.** not a p
4920: 75 62 6c 69 73 68 65 64 20 41 50 49 20 6f 66 20 ublished API of
4930: 53 51 4c 69 74 65 2c 20 69 73 20 73 75 62 6a 65 SQLite, is subje
4940: 63 74 20 74 6f 20 63 68 61 6e 67 65 20 77 69 74 ct to change wit
4950: 68 6f 75 74 0a 2a 2a 20 6e 6f 74 69 63 65 2c 20 hout.** notice,
4960: 61 6e 64 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 and should not b
4970: 65 20 72 65 66 65 72 65 6e 63 65 64 20 62 79 20 e referenced by
4980: 70 72 6f 67 72 61 6d 73 20 74 68 61 74 20 75 73 programs that us
4990: 65 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 e SQLite..**.**
49a0: 53 6f 6d 65 20 6f 66 20 74 68 65 20 64 65 66 69 Some of the defi
49b0: 6e 69 74 69 6f 6e 73 20 74 68 61 74 20 61 72 65 nitions that are
49c0: 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 61 72 in this file ar
49d0: 65 20 6d 61 72 6b 65 64 20 61 73 0a 2a 2a 20 22 e marked as.** "
49e0: 65 78 70 65 72 69 6d 65 6e 74 61 6c 22 2e 20 20 experimental".
49f0: 45 78 70 65 72 69 6d 65 6e 74 61 6c 20 69 6e 74 Experimental int
4a00: 65 72 66 61 63 65 73 20 61 72 65 20 6e 6f 72 6d erfaces are norm
4a10: 61 6c 6c 79 20 6e 65 77 0a 2a 2a 20 66 65 61 74 ally new.** feat
4a20: 75 72 65 73 20 72 65 63 65 6e 74 6c 79 20 61 64 ures recently ad
4a30: 64 65 64 20 74 6f 20 53 51 4c 69 74 65 2e 20 20 ded to SQLite.
4a40: 57 65 20 64 6f 20 6e 6f 74 20 61 6e 74 69 63 69 We do not antici
4a50: 70 61 74 65 20 63 68 61 6e 67 65 73 0a 2a 2a 20 pate changes.**
4a60: 74 6f 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 20 to experimental
4a70: 69 6e 74 65 72 66 61 63 65 73 20 62 75 74 20 72 interfaces but r
4a80: 65 73 65 72 76 65 20 74 68 65 20 72 69 67 68 74 eserve the right
4a90: 20 74 6f 20 6d 61 6b 65 20 6d 69 6e 6f 72 20 63 to make minor c
4aa0: 68 61 6e 67 65 73 0a 2a 2a 20 69 66 20 65 78 70 hanges.** if exp
4ab0: 65 72 69 65 6e 63 65 20 66 72 6f 6d 20 75 73 65 erience from use
4ac0: 20 22 69 6e 20 74 68 65 20 77 69 6c 64 22 20 73 "in the wild" s
4ad0: 75 67 67 65 73 74 20 73 75 63 68 20 63 68 61 6e uggest such chan
4ae0: 67 65 73 20 61 72 65 20 70 72 75 64 65 6e 74 2e ges are prudent.
4af0: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 66 66 69 63 .**.** The offic
4b00: 69 61 6c 20 43 2d 6c 61 6e 67 75 61 67 65 20 41 ial C-language A
4b10: 50 49 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e PI documentation
4b20: 20 66 6f 72 20 53 51 4c 69 74 65 20 69 73 20 64 for SQLite is d
4b30: 65 72 69 76 65 64 0a 2a 2a 20 66 72 6f 6d 20 63 erived.** from c
4b40: 6f 6d 6d 65 6e 74 73 20 69 6e 20 74 68 69 73 20 omments in this
4b50: 66 69 6c 65 2e 20 20 54 68 69 73 20 66 69 6c 65 file. This file
4b60: 20 69 73 20 74 68 65 20 61 75 74 68 6f 72 69 74 is the authorit
4b70: 61 74 69 76 65 20 73 6f 75 72 63 65 0a 2a 2a 20 ative source.**
4b80: 6f 6e 20 68 6f 77 20 53 51 4c 69 74 65 20 69 6e on how SQLite in
4b90: 74 65 72 66 61 63 65 73 20 61 72 65 20 73 75 70 terfaces are sup
4ba0: 70 6f 73 65 20 74 6f 20 6f 70 65 72 61 74 65 2e pose to operate.
4bb0: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 61 6d 65 20 .**.** The name
4bc0: 6f 66 20 74 68 69 73 20 66 69 6c 65 20 75 6e 64 of this file und
4bd0: 65 72 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e er configuration
4be0: 20 6d 61 6e 61 67 65 6d 65 6e 74 20 69 73 20 22 management is "
4bf0: 73 71 6c 69 74 65 2e 68 2e 69 6e 22 2e 0a 2a 2a sqlite.h.in"..**
4c00: 20 54 68 65 20 6d 61 6b 65 66 69 6c 65 20 6d 61 The makefile ma
4c10: 6b 65 73 20 73 6f 6d 65 20 6d 69 6e 6f 72 20 63 kes some minor c
4c20: 68 61 6e 67 65 73 20 74 6f 20 74 68 69 73 20 66 hanges to this f
4c30: 69 6c 65 20 28 73 75 63 68 20 61 73 20 69 6e 73 ile (such as ins
4c40: 65 72 74 69 6e 67 0a 2a 2a 20 74 68 65 20 76 65 erting.** the ve
4c50: 72 73 69 6f 6e 20 6e 75 6d 62 65 72 29 20 61 6e rsion number) an
4c60: 64 20 63 68 61 6e 67 65 73 20 69 74 73 20 6e 61 d changes its na
4c70: 6d 65 20 74 6f 20 22 73 71 6c 69 74 65 33 2e 68 me to "sqlite3.h
4c80: 22 20 61 73 0a 2a 2a 20 70 61 72 74 20 6f 66 20 " as.** part of
4c90: 74 68 65 20 62 75 69 6c 64 20 70 72 6f 63 65 73 the build proces
4ca0: 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 53 s..*/.#ifndef _S
4cb0: 51 4c 49 54 45 33 5f 48 5f 0a 23 64 65 66 69 6e QLITE3_H_.#defin
4cc0: 65 20 5f 53 51 4c 49 54 45 33 5f 48 5f 0a 23 69 e _SQLITE3_H_.#i
4cd0: 6e 63 6c 75 64 65 20 3c 73 74 64 61 72 67 2e 68 nclude <stdarg.h
4ce0: 3e 20 20 20 20 20 2f 2a 20 4e 65 65 64 65 64 20 > /* Needed
4cf0: 66 6f 72 20 74 68 65 20 64 65 66 69 6e 69 74 69 for the definiti
4d00: 6f 6e 20 6f 66 20 76 61 5f 6c 69 73 74 20 2a 2f on of va_list */
4d10: 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 ../*.** Make sur
4d20: 65 20 77 65 20 63 61 6e 20 63 61 6c 6c 20 74 68 e we can call th
4d30: 69 73 20 73 74 75 66 66 20 66 72 6f 6d 20 43 2b is stuff from C+
4d40: 2b 2e 0a 2a 2f 0a 23 69 66 20 30 0a 65 78 74 65 +..*/.#if 0.exte
4d50: 72 6e 20 22 43 22 20 7b 0a 23 65 6e 64 69 66 0a rn "C" {.#endif.
4d60: 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 74 68 65 20 ../*.** Add the
4d70: 61 62 69 6c 69 74 79 20 74 6f 20 6f 76 65 72 72 ability to overr
4d80: 69 64 65 20 27 65 78 74 65 72 6e 27 0a 2a 2f 0a ide 'extern'.*/.
4d90: 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 45 #ifndef SQLITE_E
4da0: 58 54 45 52 4e 0a 23 20 64 65 66 69 6e 65 20 53 XTERN.# define S
4db0: 51 4c 49 54 45 5f 45 58 54 45 52 4e 20 65 78 74 QLITE_EXTERN ext
4dc0: 65 72 6e 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e ern.#endif..#ifn
4dd0: 64 65 66 20 53 51 4c 49 54 45 5f 41 50 49 0a 23 def SQLITE_API.#
4de0: 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 define SQLITE_A
4df0: 50 49 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a PI.#endif.../*.*
4e00: 2a 20 54 68 65 73 65 20 6e 6f 2d 6f 70 20 6d 61 * These no-op ma
4e10: 63 72 6f 73 20 61 72 65 20 75 73 65 64 20 69 6e cros are used in
4e20: 20 66 72 6f 6e 74 20 6f 66 20 69 6e 74 65 72 66 front of interf
4e30: 61 63 65 73 20 74 6f 20 6d 61 72 6b 20 74 68 6f aces to mark tho
4e40: 73 65 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 73 se.** interfaces
4e50: 20 61 73 20 65 69 74 68 65 72 20 64 65 70 72 65 as either depre
4e60: 63 61 74 65 64 20 6f 72 20 65 78 70 65 72 69 6d cated or experim
4e70: 65 6e 74 61 6c 2e 20 20 4e 65 77 20 61 70 70 6c ental. New appl
4e80: 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 73 68 6f 75 ications.** shou
4e90: 6c 64 20 6e 6f 74 20 75 73 65 20 64 65 70 72 65 ld not use depre
4ea0: 63 61 74 65 64 20 69 6e 74 65 72 66 61 63 65 73 cated interfaces
4eb0: 20 2d 20 74 68 65 79 20 61 72 65 20 73 75 70 70 - they are supp
4ec0: 6f 72 74 20 66 6f 72 20 62 61 63 6b 77 61 72 64 ort for backward
4ed0: 73 0a 2a 2a 20 63 6f 6d 70 61 74 69 62 69 6c 69 s.** compatibili
4ee0: 74 79 20 6f 6e 6c 79 2e 20 20 41 70 70 6c 69 63 ty only. Applic
4ef0: 61 74 69 6f 6e 20 77 72 69 74 65 72 73 20 73 68 ation writers sh
4f00: 6f 75 6c 64 20 62 65 20 61 77 61 72 65 20 74 68 ould be aware th
4f10: 61 74 0a 2a 2a 20 65 78 70 65 72 69 6d 65 6e 74 at.** experiment
4f20: 61 6c 20 69 6e 74 65 72 66 61 63 65 73 20 61 72 al interfaces ar
4f30: 65 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68 61 e subject to cha
4f40: 6e 67 65 20 69 6e 20 70 6f 69 6e 74 20 72 65 6c nge in point rel
4f50: 65 61 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 eases..**.** The
4f60: 73 65 20 6d 61 63 72 6f 73 20 75 73 65 64 20 74 se macros used t
4f70: 6f 20 72 65 73 6f 6c 76 65 20 74 6f 20 76 61 72 o resolve to var
4f80: 69 6f 75 73 20 6b 69 6e 64 73 20 6f 66 20 63 6f ious kinds of co
4f90: 6d 70 69 6c 65 72 20 6d 61 67 69 63 20 74 68 61 mpiler magic tha
4fa0: 74 0a 2a 2a 20 77 6f 75 6c 64 20 67 65 6e 65 72 t.** would gener
4fb0: 61 74 65 20 77 61 72 6e 69 6e 67 20 6d 65 73 73 ate warning mess
4fc0: 61 67 65 73 20 77 68 65 6e 20 74 68 65 79 20 77 ages when they w
4fd0: 65 72 65 20 75 73 65 64 2e 20 20 42 75 74 20 74 ere used. But t
4fe0: 68 61 74 0a 2a 2a 20 63 6f 6d 70 69 6c 65 72 20 hat.** compiler
4ff0: 6d 61 67 69 63 20 65 6e 64 65 64 20 75 70 20 67 magic ended up g
5000: 65 6e 65 72 61 74 69 6e 67 20 73 75 63 68 20 61 enerating such a
5010: 20 66 6c 75 72 72 79 20 6f 66 20 62 75 67 20 72 flurry of bug r
5020: 65 70 6f 72 74 73 0a 2a 2a 20 74 68 61 74 20 77 eports.** that w
5030: 65 20 68 61 76 65 20 74 61 6b 65 6e 20 69 74 20 e have taken it
5040: 61 6c 6c 20 6f 75 74 20 61 6e 64 20 67 6f 6e 65 all out and gone
5050: 20 62 61 63 6b 20 74 6f 20 75 73 69 6e 67 20 73 back to using s
5060: 69 6d 70 6c 65 0a 2a 2a 20 6e 6f 6f 70 20 6d 61 imple.** noop ma
5070: 63 72 6f 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 cros..*/.#define
5080: 20 53 51 4c 49 54 45 5f 44 45 50 52 45 43 41 54 SQLITE_DEPRECAT
5090: 45 44 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 ED.#define SQLIT
50a0: 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 0a E_EXPERIMENTAL..
50b0: 2f 2a 0a 2a 2a 20 45 6e 73 75 72 65 20 74 68 65 /*.** Ensure the
50c0: 73 65 20 73 79 6d 62 6f 6c 73 20 77 65 72 65 20 se symbols were
50d0: 6e 6f 74 20 64 65 66 69 6e 65 64 20 62 79 20 73 not defined by s
50e0: 6f 6d 65 20 70 72 65 76 69 6f 75 73 20 68 65 61 ome previous hea
50f0: 64 65 72 20 66 69 6c 65 2e 0a 2a 2f 0a 23 69 66 der file..*/.#if
5100: 64 65 66 20 53 51 4c 49 54 45 5f 56 45 52 53 49 def SQLITE_VERSI
5110: 4f 4e 0a 23 20 75 6e 64 65 66 20 53 51 4c 49 54 ON.# undef SQLIT
5120: 45 5f 56 45 52 53 49 4f 4e 0a 23 65 6e 64 69 66 E_VERSION.#endif
5130: 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 56 .#ifdef SQLITE_V
5140: 45 52 53 49 4f 4e 5f 4e 55 4d 42 45 52 0a 23 20 ERSION_NUMBER.#
5150: 75 6e 64 65 66 20 53 51 4c 49 54 45 5f 56 45 52 undef SQLITE_VER
5160: 53 49 4f 4e 5f 4e 55 4d 42 45 52 0a 23 65 6e 64 SION_NUMBER.#end
5170: 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 if../*.** CAPI3R
5180: 45 46 3a 20 43 6f 6d 70 69 6c 65 2d 54 69 6d 65 EF: Compile-Time
5190: 20 4c 69 62 72 61 72 79 20 56 65 72 73 69 6f 6e Library Version
51a0: 20 4e 75 6d 62 65 72 73 20 7b 48 31 30 30 31 30 Numbers {H10010
51b0: 7d 20 3c 53 36 30 31 30 30 3e 0a 2a 2a 0a 2a 2a } <S60100>.**.**
51c0: 20 54 68 65 20 53 51 4c 49 54 45 5f 56 45 52 53 The SQLITE_VERS
51d0: 49 4f 4e 20 61 6e 64 20 53 51 4c 49 54 45 5f 56 ION and SQLITE_V
51e0: 45 52 53 49 4f 4e 5f 4e 55 4d 42 45 52 20 23 64 ERSION_NUMBER #d
51f0: 65 66 69 6e 65 73 20 69 6e 0a 2a 2a 20 74 68 65 efines in.** the
5200: 20 73 71 6c 69 74 65 33 2e 68 20 66 69 6c 65 20 sqlite3.h file
5210: 73 70 65 63 69 66 79 20 74 68 65 20 76 65 72 73 specify the vers
5220: 69 6f 6e 20 6f 66 20 53 51 4c 69 74 65 20 77 69 ion of SQLite wi
5230: 74 68 20 77 68 69 63 68 0a 2a 2a 20 74 68 61 74 th which.** that
5240: 20 68 65 61 64 65 72 20 66 69 6c 65 20 69 73 20 header file is
5250: 61 73 73 6f 63 69 61 74 65 64 2e 0a 2a 2a 0a 2a associated..**.*
5260: 2a 20 54 68 65 20 22 76 65 72 73 69 6f 6e 22 20 * The "version"
5270: 6f 66 20 53 51 4c 69 74 65 20 69 73 20 61 20 73 of SQLite is a s
5280: 74 72 69 6e 67 20 6f 66 20 74 68 65 20 66 6f 72 tring of the for
5290: 6d 20 22 57 2e 58 2e 59 22 20 6f 72 20 22 57 2e m "W.X.Y" or "W.
52a0: 58 2e 59 2e 5a 22 2e 0a 2a 2a 20 54 68 65 20 57 X.Y.Z"..** The W
52b0: 20 76 61 6c 75 65 20 69 73 20 6d 61 6a 6f 72 20 value is major
52c0: 76 65 72 73 69 6f 6e 20 6e 75 6d 62 65 72 20 61 version number a
52d0: 6e 64 20 69 73 20 61 6c 77 61 79 73 20 33 20 69 nd is always 3 i
52e0: 6e 20 53 51 4c 69 74 65 33 2e 0a 2a 2a 20 54 68 n SQLite3..** Th
52f0: 65 20 57 20 76 61 6c 75 65 20 6f 6e 6c 79 20 63 e W value only c
5300: 68 61 6e 67 65 73 20 77 68 65 6e 20 62 61 63 6b hanges when back
5310: 77 61 72 64 73 20 63 6f 6d 70 61 74 69 62 69 6c wards compatibil
5320: 69 74 79 20 69 73 0a 2a 2a 20 62 72 6f 6b 65 6e ity is.** broken
5330: 20 61 6e 64 20 77 65 20 69 6e 74 65 6e 64 20 74 and we intend t
5340: 6f 20 6e 65 76 65 72 20 62 72 65 61 6b 20 62 61 o never break ba
5350: 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 69 62 ckwards compatib
5360: 69 6c 69 74 79 2e 0a 2a 2a 20 54 68 65 20 58 20 ility..** The X
5370: 76 61 6c 75 65 20 69 73 20 74 68 65 20 6d 69 6e value is the min
5380: 6f 72 20 76 65 72 73 69 6f 6e 20 6e 75 6d 62 65 or version numbe
5390: 72 20 61 6e 64 20 6f 6e 6c 79 20 63 68 61 6e 67 r and only chang
53a0: 65 73 20 77 68 65 6e 0a 2a 2a 20 74 68 65 72 65 es when.** there
53b0: 20 61 72 65 20 6d 61 6a 6f 72 20 66 65 61 74 75 are major featu
53c0: 72 65 20 65 6e 68 61 6e 63 65 6d 65 6e 74 73 20 re enhancements
53d0: 74 68 61 74 20 61 72 65 20 66 6f 72 77 61 72 64 that are forward
53e0: 73 20 63 6f 6d 70 61 74 69 62 6c 65 0a 2a 2a 20 s compatible.**
53f0: 62 75 74 20 6e 6f 74 20 62 61 63 6b 77 61 72 64 but not backward
5400: 73 20 63 6f 6d 70 61 74 69 62 6c 65 2e 0a 2a 2a s compatible..**
5410: 20 54 68 65 20 59 20 76 61 6c 75 65 20 69 73 20 The Y value is
5420: 74 68 65 20 72 65 6c 65 61 73 65 20 6e 75 6d 62 the release numb
5430: 65 72 20 61 6e 64 20 69 73 20 69 6e 63 72 65 6d er and is increm
5440: 65 6e 74 65 64 20 77 69 74 68 0a 2a 2a 20 65 61 ented with.** ea
5450: 63 68 20 72 65 6c 65 61 73 65 20 62 75 74 20 72 ch release but r
5460: 65 73 65 74 73 20 62 61 63 6b 20 74 6f 20 30 20 esets back to 0
5470: 77 68 65 6e 65 76 65 72 20 58 20 69 73 20 69 6e whenever X is in
5480: 63 72 65 6d 65 6e 74 65 64 2e 0a 2a 2a 20 54 68 cremented..** Th
5490: 65 20 5a 20 76 61 6c 75 65 20 6f 6e 6c 79 20 61 e Z value only a
54a0: 70 70 65 61 72 73 20 6f 6e 20 62 72 61 6e 63 68 ppears on branch
54b0: 20 72 65 6c 65 61 73 65 73 2e 0a 2a 2a 0a 2a 2a releases..**.**
54c0: 20 54 68 65 20 53 51 4c 49 54 45 5f 56 45 52 53 The SQLITE_VERS
54d0: 49 4f 4e 5f 4e 55 4d 42 45 52 20 69 73 20 61 6e ION_NUMBER is an
54e0: 20 69 6e 74 65 67 65 72 20 74 68 61 74 20 69 73 integer that is
54f0: 20 63 6f 6d 70 75 74 65 64 20 61 73 0a 2a 2a 20 computed as.**
5500: 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c follows:.**.** <
5510: 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e blockquote><pre>
5520: 0a 2a 2a 20 53 51 4c 49 54 45 5f 56 45 52 53 49 .** SQLITE_VERSI
5530: 4f 4e 5f 4e 55 4d 42 45 52 20 3d 20 57 2a 31 30 ON_NUMBER = W*10
5540: 30 30 30 30 30 20 2b 20 58 2a 31 30 30 30 20 2b 00000 + X*1000 +
5550: 20 59 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c Y.** </pre></bl
5560: 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 ockquote>.**.**
5570: 53 69 6e 63 65 20 76 65 72 73 69 6f 6e 20 33 2e Since version 3.
5580: 36 2e 31 38 2c 20 53 51 4c 69 74 65 20 73 6f 75 6.18, SQLite sou
5590: 72 63 65 20 63 6f 64 65 20 68 61 73 20 62 65 65 rce code has bee
55a0: 6e 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 0a n stored in the.
55b0: 2a 2a 20 3c 61 20 68 72 65 66 3d 22 68 74 74 70 ** <a href="http
55c0: 3a 2f 2f 77 77 77 2e 66 6f 73 73 69 6c 2d 73 63 ://www.fossil-sc
55d0: 6d 2e 6f 72 67 2f 22 3e 66 6f 73 73 69 6c 20 63 m.org/">fossil c
55e0: 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6d 61 6e onfiguration man
55f0: 61 67 65 6d 65 6e 74 0a 2a 2a 20 73 79 73 74 65 agement.** syste
5600: 6d 3c 2f 61 3e 2e 20 20 54 68 65 20 53 51 4c 49 m</a>. The SQLI
5610: 54 45 5f 53 4f 55 52 43 45 5f 49 44 0a 2a 2a 20 TE_SOURCE_ID.**
5620: 6d 61 63 72 6f 20 69 73 20 61 20 73 74 72 69 6e macro is a strin
5630: 67 20 77 68 69 63 68 20 69 64 65 6e 74 69 66 69 g which identifi
5640: 65 73 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 es a particular
5650: 63 68 65 63 6b 2d 69 6e 20 6f 66 20 53 51 4c 69 check-in of SQLi
5660: 74 65 0a 2a 2a 20 77 69 74 68 69 6e 20 69 74 73 te.** within its
5670: 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6d configuration m
5680: 61 6e 61 67 65 6d 65 6e 74 20 73 79 73 74 65 6d anagement system
5690: 2e 20 20 54 68 65 20 73 74 72 69 6e 67 20 63 6f . The string co
56a0: 6e 74 61 69 6e 73 20 74 68 65 0a 2a 2a 20 64 61 ntains the.** da
56b0: 74 65 20 61 6e 64 20 74 69 6d 65 20 6f 66 20 74 te and time of t
56c0: 68 65 20 63 68 65 63 6b 2d 69 6e 20 28 55 54 43 he check-in (UTC
56d0: 29 20 61 6e 64 20 61 6e 20 53 48 41 31 20 68 61 ) and an SHA1 ha
56e0: 73 68 20 6f 66 20 74 68 65 20 65 6e 74 69 72 65 sh of the entire
56f0: 0a 2a 2a 20 73 6f 75 72 63 65 20 74 72 65 65 2e .** source tree.
5700: 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a .**.** See also:
5710: 20 5b 73 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 [sqlite3_libver
5720: 73 69 6f 6e 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c sion()],.** [sql
5730: 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 5f ite3_libversion_
5740: 6e 75 6d 62 65 72 28 29 5d 2c 20 5b 73 71 6c 69 number()], [sqli
5750: 74 65 33 5f 73 6f 75 72 63 65 69 64 28 29 5d 2c te3_sourceid()],
5760: 0a 2a 2a 20 5b 73 71 6c 69 74 65 5f 76 65 72 73 .** [sqlite_vers
5770: 69 6f 6e 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 ion()] and [sqli
5780: 74 65 5f 73 6f 75 72 63 65 5f 69 64 28 29 5d 2e te_source_id()].
5790: 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 .**.** Requireme
57a0: 6e 74 73 3a 20 5b 48 31 30 30 31 31 5d 20 5b 48 nts: [H10011] [H
57b0: 31 30 30 31 34 5d 0a 2a 2f 0a 23 64 65 66 69 6e 10014].*/.#defin
57c0: 65 20 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e e SQLITE_VERSION
57d0: 20 20 20 20 20 20 20 20 22 33 2e 36 2e 31 39 22 "3.6.19"
57e0: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
57f0: 56 45 52 53 49 4f 4e 5f 4e 55 4d 42 45 52 20 33 VERSION_NUMBER 3
5800: 30 30 36 30 31 39 0a 23 64 65 66 69 6e 65 20 53 006019.#define S
5810: 51 4c 49 54 45 5f 53 4f 55 52 43 45 5f 49 44 20 QLITE_SOURCE_ID
5820: 20 20 20 20 20 22 32 30 30 39 2d 31 30 2d 31 33 "2009-10-13
5830: 20 31 35 3a 34 32 3a 34 39 20 66 38 39 34 65 62 15:42:49 f894eb
5840: 66 38 36 64 36 62 61 66 63 64 31 34 36 31 66 31 f86d6bafcd1461f1
5850: 30 34 66 35 66 36 37 37 62 33 62 36 61 33 61 61 04f5f677b3b6a3aa
5860: 31 61 22 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 1a"../*.** CAPI3
5870: 52 45 46 3a 20 52 75 6e 2d 54 69 6d 65 20 4c 69 REF: Run-Time Li
5880: 62 72 61 72 79 20 56 65 72 73 69 6f 6e 20 4e 75 brary Version Nu
5890: 6d 62 65 72 73 20 7b 48 31 30 30 32 30 7d 20 3c mbers {H10020} <
58a0: 53 36 30 31 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f S60100>.** KEYWO
58b0: 52 44 53 3a 20 73 71 6c 69 74 65 33 5f 76 65 72 RDS: sqlite3_ver
58c0: 73 69 6f 6e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 sion.**.** These
58d0: 20 69 6e 74 65 72 66 61 63 65 73 20 70 72 6f 76 interfaces prov
58e0: 69 64 65 20 74 68 65 20 73 61 6d 65 20 69 6e 66 ide the same inf
58f0: 6f 72 6d 61 74 69 6f 6e 20 61 73 20 74 68 65 20 ormation as the
5900: 5b 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5d [SQLITE_VERSION]
5910: 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 56 45 52 ,.** [SQLITE_VER
5920: 53 49 4f 4e 5f 4e 55 4d 42 45 52 5d 2c 20 61 6e SION_NUMBER], an
5930: 64 20 5b 53 51 4c 49 54 45 5f 53 4f 55 52 43 45 d [SQLITE_SOURCE
5940: 5f 49 44 5d 20 23 64 65 66 69 6e 65 73 20 69 6e _ID] #defines in
5950: 20 74 68 65 20 68 65 61 64 65 72 2c 0a 2a 2a 20 the header,.**
5960: 62 75 74 20 61 72 65 20 61 73 73 6f 63 69 61 74 but are associat
5970: 65 64 20 77 69 74 68 20 74 68 65 20 6c 69 62 72 ed with the libr
5980: 61 72 79 20 69 6e 73 74 65 61 64 20 6f 66 20 74 ary instead of t
5990: 68 65 20 68 65 61 64 65 72 20 66 69 6c 65 2e 20 he header file.
59a0: 20 43 61 75 74 69 6f 75 73 0a 2a 2a 20 70 72 6f Cautious.** pro
59b0: 67 72 61 6d 6d 65 72 73 20 6d 69 67 68 74 20 69 grammers might i
59c0: 6e 63 6c 75 64 65 20 61 73 73 65 72 74 28 29 20 nclude assert()
59d0: 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 20 74 68 statements in th
59e0: 65 69 72 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 eir application
59f0: 74 6f 0a 2a 2a 20 76 65 72 69 66 79 20 74 68 61 to.** verify tha
5a00: 74 20 76 61 6c 75 65 73 20 72 65 74 75 72 6e 65 t values returne
5a10: 64 20 62 79 20 74 68 65 73 65 20 69 6e 74 65 72 d by these inter
5a20: 66 61 63 65 73 20 6d 61 74 63 68 20 74 68 65 20 faces match the
5a30: 6d 61 63 72 6f 73 20 69 6e 0a 2a 2a 20 74 68 65 macros in.** the
5a40: 20 68 65 61 64 65 72 2c 20 61 6e 64 20 74 68 75 header, and thu
5a50: 73 20 69 6e 73 75 72 65 20 74 68 61 74 20 74 68 s insure that th
5a60: 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 69 73 e application is
5a70: 0a 2a 2a 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 .** compiled wit
5a80: 68 20 6d 61 74 63 68 69 6e 67 20 6c 69 62 72 61 h matching libra
5a90: 72 79 20 61 6e 64 20 68 65 61 64 65 72 20 66 69 ry and header fi
5aa0: 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 les..**.** <bloc
5ab0: 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 kquote><pre>.**
5ac0: 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
5ad0: 6c 69 62 76 65 72 73 69 6f 6e 5f 6e 75 6d 62 65 libversion_numbe
5ae0: 72 28 29 3d 3d 53 51 4c 49 54 45 5f 56 45 52 53 r()==SQLITE_VERS
5af0: 49 4f 4e 5f 4e 55 4d 42 45 52 20 29 3b 0a 2a 2a ION_NUMBER );.**
5b00: 20 61 73 73 65 72 74 28 20 73 74 72 63 6d 70 28 assert( strcmp(
5b10: 73 71 6c 69 74 65 33 5f 73 6f 75 72 63 65 69 64 sqlite3_sourceid
5b20: 28 29 2c 53 51 4c 49 54 45 5f 53 4f 55 52 43 45 (),SQLITE_SOURCE
5b30: 5f 49 44 29 3d 3d 30 20 29 3b 0a 2a 2a 20 61 73 _ID)==0 );.** as
5b40: 73 65 72 74 28 20 73 74 72 63 6d 70 28 73 71 6c sert( strcmp(sql
5b50: 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 2c ite3_libversion,
5b60: 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 29 3d SQLITE_VERSION)=
5b70: 3d 30 20 29 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 3c =0 );.** </pre><
5b80: 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a /blockquote>.**.
5b90: 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6c ** The sqlite3_l
5ba0: 69 62 76 65 72 73 69 6f 6e 28 29 20 66 75 6e 63 ibversion() func
5bb0: 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65 tion returns the
5bc0: 20 73 61 6d 65 20 69 6e 66 6f 72 6d 61 74 69 6f same informatio
5bd0: 6e 20 61 73 20 69 73 0a 2a 2a 20 69 6e 20 74 68 n as is.** in th
5be0: 65 20 73 71 6c 69 74 65 33 5f 76 65 72 73 69 6f e sqlite3_versio
5bf0: 6e 5b 5d 20 73 74 72 69 6e 67 20 63 6f 6e 73 74 n[] string const
5c00: 61 6e 74 2e 20 20 54 68 65 20 66 75 6e 63 74 69 ant. The functi
5c10: 6f 6e 20 69 73 20 70 72 6f 76 69 64 65 64 0a 2a on is provided.*
5c20: 2a 20 66 6f 72 20 75 73 65 20 69 6e 20 44 4c 4c * for use in DLL
5c30: 73 20 73 69 6e 63 65 20 44 4c 4c 20 75 73 65 72 s since DLL user
5c40: 73 20 75 73 75 61 6c 6c 79 20 64 6f 20 6e 6f 74 s usually do not
5c50: 20 68 61 76 65 20 64 69 72 65 63 74 20 61 63 63 have direct acc
5c60: 65 73 73 20 74 6f 20 73 74 72 69 6e 67 0a 2a 2a ess to string.**
5c70: 20 63 6f 6e 73 74 61 6e 74 73 20 77 69 74 68 69 constants withi
5c80: 6e 20 74 68 65 20 44 4c 4c 2e 20 20 53 69 6d 69 n the DLL. Simi
5c90: 6c 61 72 6c 79 2c 20 74 68 65 20 73 71 6c 69 74 larly, the sqlit
5ca0: 65 33 5f 73 6f 75 72 63 65 69 64 28 29 20 66 75 e3_sourceid() fu
5cb0: 6e 63 74 69 6f 6e 0a 2a 2a 20 72 65 74 75 72 6e nction.** return
5cc0: 73 20 74 68 65 20 73 61 6d 65 20 69 6e 66 6f 72 s the same infor
5cd0: 6d 61 74 69 6f 6e 20 61 73 20 69 73 20 69 6e 20 mation as is in
5ce0: 74 68 65 20 5b 53 51 4c 49 54 45 5f 53 4f 55 52 the [SQLITE_SOUR
5cf0: 43 45 5f 49 44 5d 20 23 64 65 66 69 6e 65 20 6f CE_ID] #define o
5d00: 66 0a 2a 2a 20 74 68 65 20 68 65 61 64 65 72 20 f.** the header
5d10: 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 file..**.** See
5d20: 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 5f 76 65 also: [sqlite_ve
5d30: 72 73 69 6f 6e 28 29 5d 20 61 6e 64 20 5b 73 71 rsion()] and [sq
5d40: 6c 69 74 65 5f 73 6f 75 72 63 65 5f 69 64 28 29 lite_source_id()
5d50: 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 ]..**.** Require
5d60: 6d 65 6e 74 73 3a 20 5b 48 31 30 30 32 31 5d 20 ments: [H10021]
5d70: 5b 48 31 30 30 32 32 5d 20 5b 48 31 30 30 32 33 [H10022] [H10023
5d80: 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ].*/.SQLITE_API
5d90: 63 6f 6e 73 74 20 63 68 61 72 20 73 71 6c 69 74 const char sqlit
5da0: 65 33 5f 76 65 72 73 69 6f 6e 5b 5d 20 3d 20 53 e3_version[] = S
5db0: 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 3b 0a 53 QLITE_VERSION;.S
5dc0: 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 QLITE_API const
5dd0: 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 6c 69 char *sqlite3_li
5de0: 62 76 65 72 73 69 6f 6e 28 76 6f 69 64 29 3b 0a bversion(void);.
5df0: 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 SQLITE_API const
5e00: 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 73 char *sqlite3_s
5e10: 6f 75 72 63 65 69 64 28 76 6f 69 64 29 3b 0a 53 ourceid(void);.S
5e20: 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
5e30: 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e lite3_libversion
5e40: 5f 6e 75 6d 62 65 72 28 76 6f 69 64 29 3b 0a 0a _number(void);..
5e50: 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
5e60: 54 65 73 74 20 54 6f 20 53 65 65 20 49 66 20 54 Test To See If T
5e70: 68 65 20 4c 69 62 72 61 72 79 20 49 73 20 54 68 he Library Is Th
5e80: 72 65 61 64 73 61 66 65 20 7b 48 31 30 31 30 30 readsafe {H10100
5e90: 7d 20 3c 53 36 30 31 30 30 3e 0a 2a 2a 0a 2a 2a } <S60100>.**.**
5ea0: 20 53 51 4c 69 74 65 20 63 61 6e 20 62 65 20 63 SQLite can be c
5eb0: 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 6f 72 20 ompiled with or
5ec0: 77 69 74 68 6f 75 74 20 6d 75 74 65 78 65 73 2e without mutexes.
5ed0: 20 20 57 68 65 6e 0a 2a 2a 20 74 68 65 20 5b 53 When.** the [S
5ee0: 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 QLITE_THREADSAFE
5ef0: 5d 20 43 20 70 72 65 70 72 6f 63 65 73 73 6f 72 ] C preprocessor
5f00: 20 6d 61 63 72 6f 20 69 73 20 31 20 6f 72 20 32 macro is 1 or 2
5f10: 2c 20 6d 75 74 65 78 65 73 0a 2a 2a 20 61 72 65 , mutexes.** are
5f20: 20 65 6e 61 62 6c 65 64 20 61 6e 64 20 53 51 4c enabled and SQL
5f30: 69 74 65 20 69 73 20 74 68 72 65 61 64 73 61 66 ite is threadsaf
5f40: 65 2e 20 20 57 68 65 6e 20 74 68 65 0a 2a 2a 20 e. When the.**
5f50: 5b 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 [SQLITE_THREADSA
5f60: 46 45 5d 20 6d 61 63 72 6f 20 69 73 20 30 2c 20 FE] macro is 0,
5f70: 0a 2a 2a 20 74 68 65 20 6d 75 74 65 78 65 73 20 .** the mutexes
5f80: 61 72 65 20 6f 6d 69 74 74 65 64 2e 20 20 57 69 are omitted. Wi
5f90: 74 68 6f 75 74 20 74 68 65 20 6d 75 74 65 78 65 thout the mutexe
5fa0: 73 2c 20 69 74 20 69 73 20 6e 6f 74 20 73 61 66 s, it is not saf
5fb0: 65 0a 2a 2a 20 74 6f 20 75 73 65 20 53 51 4c 69 e.** to use SQLi
5fc0: 74 65 20 63 6f 6e 63 75 72 72 65 6e 74 6c 79 20 te concurrently
5fd0: 66 72 6f 6d 20 6d 6f 72 65 20 74 68 61 6e 20 6f from more than o
5fe0: 6e 65 20 74 68 72 65 61 64 2e 0a 2a 2a 0a 2a 2a ne thread..**.**
5ff0: 20 45 6e 61 62 6c 69 6e 67 20 6d 75 74 65 78 65 Enabling mutexe
6000: 73 20 69 6e 63 75 72 73 20 61 20 6d 65 61 73 75 s incurs a measu
6010: 72 61 62 6c 65 20 70 65 72 66 6f 72 6d 61 6e 63 rable performanc
6020: 65 20 70 65 6e 61 6c 74 79 2e 0a 2a 2a 20 53 6f e penalty..** So
6030: 20 69 66 20 73 70 65 65 64 20 69 73 20 6f 66 20 if speed is of
6040: 75 74 6d 6f 73 74 20 69 6d 70 6f 72 74 61 6e 63 utmost importanc
6050: 65 2c 20 69 74 20 6d 61 6b 65 73 20 73 65 6e 73 e, it makes sens
6060: 65 20 74 6f 20 64 69 73 61 62 6c 65 0a 2a 2a 20 e to disable.**
6070: 74 68 65 20 6d 75 74 65 78 65 73 2e 20 20 42 75 the mutexes. Bu
6080: 74 20 66 6f 72 20 6d 61 78 69 6d 75 6d 20 73 61 t for maximum sa
6090: 66 65 74 79 2c 20 6d 75 74 65 78 65 73 20 73 68 fety, mutexes sh
60a0: 6f 75 6c 64 20 62 65 20 65 6e 61 62 6c 65 64 2e ould be enabled.
60b0: 0a 2a 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 .** The default
60c0: 62 65 68 61 76 69 6f 72 20 69 73 20 66 6f 72 20 behavior is for
60d0: 6d 75 74 65 78 65 73 20 74 6f 20 62 65 20 65 6e mutexes to be en
60e0: 61 62 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 abled..**.** Thi
60f0: 73 20 69 6e 74 65 72 66 61 63 65 20 63 61 6e 20 s interface can
6100: 62 65 20 75 73 65 64 20 62 79 20 61 6e 20 61 70 be used by an ap
6110: 70 6c 69 63 61 74 69 6f 6e 20 74 6f 20 6d 61 6b plication to mak
6120: 65 20 73 75 72 65 20 74 68 61 74 20 74 68 65 0a e sure that the.
6130: 2a 2a 20 76 65 72 73 69 6f 6e 20 6f 66 20 53 51 ** version of SQ
6140: 4c 69 74 65 20 74 68 61 74 20 69 74 20 69 73 20 Lite that it is
6150: 6c 69 6e 6b 69 6e 67 20 61 67 61 69 6e 73 74 20 linking against
6160: 77 61 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 was compiled wit
6170: 68 0a 2a 2a 20 74 68 65 20 64 65 73 69 72 65 64 h.** the desired
6180: 20 73 65 74 74 69 6e 67 20 6f 66 20 74 68 65 20 setting of the
6190: 5b 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 [SQLITE_THREADSA
61a0: 46 45 5d 20 6d 61 63 72 6f 2e 0a 2a 2a 0a 2a 2a FE] macro..**.**
61b0: 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 This interface
61c0: 6f 6e 6c 79 20 72 65 70 6f 72 74 73 20 6f 6e 20 only reports on
61d0: 74 68 65 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 the compile-time
61e0: 20 6d 75 74 65 78 20 73 65 74 74 69 6e 67 0a 2a mutex setting.*
61f0: 2a 20 6f 66 20 74 68 65 20 5b 53 51 4c 49 54 45 * of the [SQLITE
6200: 5f 54 48 52 45 41 44 53 41 46 45 5d 20 66 6c 61 _THREADSAFE] fla
6210: 67 2e 20 20 49 66 20 53 51 4c 69 74 65 20 69 73 g. If SQLite is
6220: 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 0a 2a compiled with.*
6230: 2a 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 * SQLITE_THREADS
6240: 41 46 45 3d 31 20 74 68 65 6e 20 6d 75 74 65 78 AFE=1 then mutex
6250: 65 73 20 61 72 65 20 65 6e 61 62 6c 65 64 20 62 es are enabled b
6260: 79 20 64 65 66 61 75 6c 74 20 62 75 74 0a 2a 2a y default but.**
6270: 20 63 61 6e 20 62 65 20 66 75 6c 6c 79 20 6f 72 can be fully or
6280: 20 70 61 72 74 69 61 6c 6c 79 20 64 69 73 61 62 partially disab
6290: 6c 65 64 20 75 73 69 6e 67 20 61 20 63 61 6c 6c led using a call
62a0: 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e to [sqlite3_con
62b0: 66 69 67 28 29 5d 0a 2a 2a 20 77 69 74 68 20 74 fig()].** with t
62c0: 68 65 20 76 65 72 62 73 20 5b 53 51 4c 49 54 45 he verbs [SQLITE
62d0: 5f 43 4f 4e 46 49 47 5f 53 49 4e 47 4c 45 54 48 _CONFIG_SINGLETH
62e0: 52 45 41 44 5d 2c 20 5b 53 51 4c 49 54 45 5f 43 READ], [SQLITE_C
62f0: 4f 4e 46 49 47 5f 4d 55 4c 54 49 54 48 52 45 41 ONFIG_MULTITHREA
6300: 44 5d 2c 0a 2a 2a 20 6f 72 20 5b 53 51 4c 49 54 D],.** or [SQLIT
6310: 45 5f 43 4f 4e 46 49 47 5f 4d 55 54 45 58 5d 2e E_CONFIG_MUTEX].
6320: 20 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c The return val
6330: 75 65 20 6f 66 20 74 68 69 73 20 66 75 6e 63 74 ue of this funct
6340: 69 6f 6e 20 73 68 6f 77 73 0a 2a 2a 20 6f 6e 6c ion shows.** onl
6350: 79 20 74 68 65 20 64 65 66 61 75 6c 74 20 63 6f y the default co
6360: 6d 70 69 6c 65 2d 74 69 6d 65 20 73 65 74 74 69 mpile-time setti
6370: 6e 67 2c 20 6e 6f 74 20 61 6e 79 20 72 75 6e 2d ng, not any run-
6380: 74 69 6d 65 20 63 68 61 6e 67 65 73 0a 2a 2a 20 time changes.**
6390: 74 6f 20 74 68 61 74 20 73 65 74 74 69 6e 67 2e to that setting.
63a0: 0a 2a 2a 0a 2a 2a 20 53 65 65 20 74 68 65 20 5b .**.** See the [
63b0: 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 65 5d 20 threading mode]
63c0: 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 66 6f documentation fo
63d0: 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 r additional inf
63e0: 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 ormation..**.**
63f0: 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 20 5b 48 Requirements: [H
6400: 31 30 31 30 31 5d 20 5b 48 31 30 31 30 32 5d 0a 10101] [H10102].
6410: 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
6420: 74 20 73 71 6c 69 74 65 33 5f 74 68 72 65 61 64 t sqlite3_thread
6430: 73 61 66 65 28 76 6f 69 64 29 3b 0a 0a 2f 2a 0a safe(void);../*.
6440: 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44 61 74 ** CAPI3REF: Dat
6450: 61 62 61 73 65 20 43 6f 6e 6e 65 63 74 69 6f 6e abase Connection
6460: 20 48 61 6e 64 6c 65 20 7b 48 31 32 30 30 30 7d Handle {H12000}
6470: 20 3c 53 34 30 32 30 30 3e 0a 2a 2a 20 4b 45 59 <S40200>.** KEY
6480: 57 4f 52 44 53 3a 20 7b 64 61 74 61 62 61 73 65 WORDS: {database
6490: 20 63 6f 6e 6e 65 63 74 69 6f 6e 7d 20 7b 64 61 connection} {da
64a0: 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
64b0: 6e 73 7d 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 6f ns}.**.** Each o
64c0: 70 65 6e 20 53 51 4c 69 74 65 20 64 61 74 61 62 pen SQLite datab
64d0: 61 73 65 20 69 73 20 72 65 70 72 65 73 65 6e 74 ase is represent
64e0: 65 64 20 62 79 20 61 20 70 6f 69 6e 74 65 72 20 ed by a pointer
64f0: 74 6f 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f to an instance o
6500: 66 0a 2a 2a 20 74 68 65 20 6f 70 61 71 75 65 20 f.** the opaque
6510: 73 74 72 75 63 74 75 72 65 20 6e 61 6d 65 64 20 structure named
6520: 22 73 71 6c 69 74 65 33 22 2e 20 20 49 74 20 69 "sqlite3". It i
6530: 73 20 75 73 65 66 75 6c 20 74 6f 20 74 68 69 6e s useful to thin
6540: 6b 20 6f 66 20 61 6e 20 73 71 6c 69 74 65 33 0a k of an sqlite3.
6550: 2a 2a 20 70 6f 69 6e 74 65 72 20 61 73 20 61 6e ** pointer as an
6560: 20 6f 62 6a 65 63 74 2e 20 20 54 68 65 20 5b 73 object. The [s
6570: 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 2c 20 qlite3_open()],
6580: 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 [sqlite3_open16(
6590: 29 5d 2c 20 61 6e 64 0a 2a 2a 20 5b 73 71 6c 69 )], and.** [sqli
65a0: 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d 20 69 te3_open_v2()] i
65b0: 6e 74 65 72 66 61 63 65 73 20 61 72 65 20 69 74 nterfaces are it
65c0: 73 20 63 6f 6e 73 74 72 75 63 74 6f 72 73 2c 20 s constructors,
65d0: 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f and [sqlite3_clo
65e0: 73 65 28 29 5d 0a 2a 2a 20 69 73 20 69 74 73 20 se()].** is its
65f0: 64 65 73 74 72 75 63 74 6f 72 2e 20 20 54 68 65 destructor. The
6600: 72 65 20 61 72 65 20 6d 61 6e 79 20 6f 74 68 65 re are many othe
6610: 72 20 69 6e 74 65 72 66 61 63 65 73 20 28 73 75 r interfaces (su
6620: 63 68 20 61 73 0a 2a 2a 20 5b 73 71 6c 69 74 65 ch as.** [sqlite
6630: 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d 2c 3_prepare_v2()],
6640: 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 [sqlite3_create
6650: 5f 66 75 6e 63 74 69 6f 6e 28 29 5d 2c 20 61 6e _function()], an
6660: 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 75 d.** [sqlite3_bu
6670: 73 79 5f 74 69 6d 65 6f 75 74 28 29 5d 20 74 6f sy_timeout()] to
6680: 20 6e 61 6d 65 20 62 75 74 20 74 68 72 65 65 29 name but three)
6690: 20 74 68 61 74 20 61 72 65 20 6d 65 74 68 6f 64 that are method
66a0: 73 20 6f 6e 20 61 6e 0a 2a 2a 20 73 71 6c 69 74 s on an.** sqlit
66b0: 65 33 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 74 79 e3 object..*/.ty
66c0: 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c pedef struct sql
66d0: 69 74 65 33 20 73 71 6c 69 74 65 33 3b 0a 0a 2f ite3 sqlite3;../
66e0: 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 36 *.** CAPI3REF: 6
66f0: 34 2d 42 69 74 20 49 6e 74 65 67 65 72 20 54 79 4-Bit Integer Ty
6700: 70 65 73 20 7b 48 31 30 32 30 30 7d 20 3c 53 31 pes {H10200} <S1
6710: 30 31 31 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 0110>.** KEYWORD
6720: 53 3a 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 S: sqlite_int64
6730: 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 0a 2a 2a sqlite_uint64.**
6740: 0a 2a 2a 20 42 65 63 61 75 73 65 20 74 68 65 72 .** Because ther
6750: 65 20 69 73 20 6e 6f 20 63 72 6f 73 73 2d 70 6c e is no cross-pl
6760: 61 74 66 6f 72 6d 20 77 61 79 20 74 6f 20 73 70 atform way to sp
6770: 65 63 69 66 79 20 36 34 2d 62 69 74 20 69 6e 74 ecify 64-bit int
6780: 65 67 65 72 20 74 79 70 65 73 0a 2a 2a 20 53 51 eger types.** SQ
6790: 4c 69 74 65 20 69 6e 63 6c 75 64 65 73 20 74 79 Lite includes ty
67a0: 70 65 64 65 66 73 20 66 6f 72 20 36 34 2d 62 69 pedefs for 64-bi
67b0: 74 20 73 69 67 6e 65 64 20 61 6e 64 20 75 6e 73 t signed and uns
67c0: 69 67 6e 65 64 20 69 6e 74 65 67 65 72 73 2e 0a igned integers..
67d0: 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 **.** The sqlite
67e0: 33 5f 69 6e 74 36 34 20 61 6e 64 20 73 71 6c 69 3_int64 and sqli
67f0: 74 65 33 5f 75 69 6e 74 36 34 20 61 72 65 20 74 te3_uint64 are t
6800: 68 65 20 70 72 65 66 65 72 72 65 64 20 74 79 70 he preferred typ
6810: 65 20 64 65 66 69 6e 69 74 69 6f 6e 73 2e 0a 2a e definitions..*
6820: 2a 20 54 68 65 20 73 71 6c 69 74 65 5f 69 6e 74 * The sqlite_int
6830: 36 34 20 61 6e 64 20 73 71 6c 69 74 65 5f 75 69 64 and sqlite_ui
6840: 6e 74 36 34 20 74 79 70 65 73 20 61 72 65 20 73 nt64 types are s
6850: 75 70 70 6f 72 74 65 64 20 66 6f 72 20 62 61 63 upported for bac
6860: 6b 77 61 72 64 73 0a 2a 2a 20 63 6f 6d 70 61 74 kwards.** compat
6870: 69 62 69 6c 69 74 79 20 6f 6e 6c 79 2e 0a 2a 2a ibility only..**
6880: 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 .** Requirements
6890: 3a 20 5b 48 31 30 32 30 31 5d 20 5b 48 31 30 32 : [H10201] [H102
68a0: 30 32 5d 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 02].*/.#ifdef SQ
68b0: 4c 49 54 45 5f 49 4e 54 36 34 5f 54 59 50 45 0a LITE_INT64_TYPE.
68c0: 20 20 74 79 70 65 64 65 66 20 53 51 4c 49 54 45 typedef SQLITE
68d0: 5f 49 4e 54 36 34 5f 54 59 50 45 20 73 71 6c 69 _INT64_TYPE sqli
68e0: 74 65 5f 69 6e 74 36 34 3b 0a 20 20 74 79 70 65 te_int64;. type
68f0: 64 65 66 20 75 6e 73 69 67 6e 65 64 20 53 51 4c def unsigned SQL
6900: 49 54 45 5f 49 4e 54 36 34 5f 54 59 50 45 20 73 ITE_INT64_TYPE s
6910: 71 6c 69 74 65 5f 75 69 6e 74 36 34 3b 0a 23 65 qlite_uint64;.#e
6920: 6c 69 66 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 lif defined(_MSC
6930: 5f 56 45 52 29 20 7c 7c 20 64 65 66 69 6e 65 64 _VER) || defined
6940: 28 5f 5f 42 4f 52 4c 41 4e 44 43 5f 5f 29 0a 20 (__BORLANDC__).
6950: 20 74 79 70 65 64 65 66 20 5f 5f 69 6e 74 36 34 typedef __int64
6960: 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 3b 0a 20 sqlite_int64;.
6970: 20 74 79 70 65 64 65 66 20 75 6e 73 69 67 6e 65 typedef unsigne
6980: 64 20 5f 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 d __int64 sqlite
6990: 5f 75 69 6e 74 36 34 3b 0a 23 65 6c 73 65 0a 20 _uint64;.#else.
69a0: 20 74 79 70 65 64 65 66 20 6c 6f 6e 67 20 6c 6f typedef long lo
69b0: 6e 67 20 69 6e 74 20 73 71 6c 69 74 65 5f 69 6e ng int sqlite_in
69c0: 74 36 34 3b 0a 20 20 74 79 70 65 64 65 66 20 75 t64;. typedef u
69d0: 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e nsigned long lon
69e0: 67 20 69 6e 74 20 73 71 6c 69 74 65 5f 75 69 6e g int sqlite_uin
69f0: 74 36 34 3b 0a 23 65 6e 64 69 66 0a 74 79 70 65 t64;.#endif.type
6a00: 64 65 66 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 def sqlite_int64
6a10: 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 3b 0a sqlite3_int64;.
6a20: 74 79 70 65 64 65 66 20 73 71 6c 69 74 65 5f 75 typedef sqlite_u
6a30: 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 75 69 int64 sqlite3_ui
6a40: 6e 74 36 34 3b 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 nt64;../*.** If
6a50: 63 6f 6d 70 69 6c 69 6e 67 20 66 6f 72 20 61 20 compiling for a
6a60: 70 72 6f 63 65 73 73 6f 72 20 74 68 61 74 20 6c processor that l
6a70: 61 63 6b 73 20 66 6c 6f 61 74 69 6e 67 20 70 6f acks floating po
6a80: 69 6e 74 20 73 75 70 70 6f 72 74 2c 0a 2a 2a 20 int support,.**
6a90: 73 75 62 73 74 69 74 75 74 65 20 69 6e 74 65 67 substitute integ
6aa0: 65 72 20 66 6f 72 20 66 6c 6f 61 74 69 6e 67 2d er for floating-
6ab0: 70 6f 69 6e 74 2e 0a 2a 2f 0a 23 69 66 64 65 66 point..*/.#ifdef
6ac0: 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f SQLITE_OMIT_FLO
6ad0: 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 23 20 64 65 ATING_POINT.# de
6ae0: 66 69 6e 65 20 64 6f 75 62 6c 65 20 73 71 6c 69 fine double sqli
6af0: 74 65 33 5f 69 6e 74 36 34 0a 23 65 6e 64 69 66 te3_int64.#endif
6b00: 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
6b10: 3a 20 43 6c 6f 73 69 6e 67 20 41 20 44 61 74 61 : Closing A Data
6b20: 62 61 73 65 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 base Connection
6b30: 7b 48 31 32 30 31 30 7d 20 3c 53 33 30 31 30 30 {H12010} <S30100
6b40: 3e 3c 53 34 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 ><S40200>.**.**
6b50: 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 This routine is
6b60: 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 66 the destructor f
6b70: 6f 72 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5d or the [sqlite3]
6b80: 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 41 object..**.** A
6b90: 70 70 6c 69 63 61 74 69 6f 6e 73 20 73 68 6f 75 pplications shou
6ba0: 6c 64 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 ld [sqlite3_fina
6bb0: 6c 69 7a 65 20 7c 20 66 69 6e 61 6c 69 7a 65 5d lize | finalize]
6bc0: 20 61 6c 6c 20 5b 70 72 65 70 61 72 65 64 20 73 all [prepared s
6bd0: 74 61 74 65 6d 65 6e 74 73 5d 0a 2a 2a 20 61 6e tatements].** an
6be0: 64 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f d [sqlite3_blob_
6bf0: 63 6c 6f 73 65 20 7c 20 63 6c 6f 73 65 5d 20 61 close | close] a
6c00: 6c 6c 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 73 ll [BLOB handles
6c10: 5d 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 ] associated wit
6c20: 68 0a 2a 2a 20 74 68 65 20 5b 73 71 6c 69 74 65 h.** the [sqlite
6c30: 33 5d 20 6f 62 6a 65 63 74 20 70 72 69 6f 72 20 3] object prior
6c40: 74 6f 20 61 74 74 65 6d 70 74 69 6e 67 20 74 6f to attempting to
6c50: 20 63 6c 6f 73 65 20 74 68 65 20 6f 62 6a 65 63 close the objec
6c60: 74 2e 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74 t..** The [sqlit
6c70: 65 33 5f 6e 65 78 74 5f 73 74 6d 74 28 29 5d 20 e3_next_stmt()]
6c80: 69 6e 74 65 72 66 61 63 65 20 63 61 6e 20 62 65 interface can be
6c90: 20 75 73 65 64 20 74 6f 20 6c 6f 63 61 74 65 20 used to locate
6ca0: 61 6c 6c 0a 2a 2a 20 5b 70 72 65 70 61 72 65 64 all.** [prepared
6cb0: 20 73 74 61 74 65 6d 65 6e 74 73 5d 20 61 73 73 statements] ass
6cc0: 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 20 5b ociated with a [
6cd0: 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
6ce0: 69 6f 6e 5d 20 69 66 20 64 65 73 69 72 65 64 2e ion] if desired.
6cf0: 0a 2a 2a 20 54 79 70 69 63 61 6c 20 63 6f 64 65 .** Typical code
6d00: 20 6d 69 67 68 74 20 6c 6f 6f 6b 20 6c 69 6b 65 might look like
6d10: 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c this:.**.** <bl
6d20: 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a ockquote><pre>.*
6d30: 2a 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a * sqlite3_stmt *
6d40: 70 53 74 6d 74 3b 0a 2a 2a 20 77 68 69 6c 65 28 pStmt;.** while(
6d50: 20 28 70 53 74 6d 74 20 3d 20 73 71 6c 69 74 65 (pStmt = sqlite
6d60: 33 5f 6e 65 78 74 5f 73 74 6d 74 28 64 62 2c 20 3_next_stmt(db,
6d70: 30 29 29 21 3d 30 20 29 7b 0a 2a 2a 20 26 6e 62 0))!=0 ){.** &nb
6d80: 73 70 3b 20 20 20 73 71 6c 69 74 65 33 5f 66 69 sp; sqlite3_fi
6d90: 6e 61 6c 69 7a 65 28 70 53 74 6d 74 29 3b 0a 2a nalize(pStmt);.*
6da0: 2a 20 7d 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 * }.** </pre></b
6db0: 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a lockquote>.**.**
6dc0: 20 49 66 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f If [sqlite3_clo
6dd0: 73 65 28 29 5d 20 69 73 20 69 6e 76 6f 6b 65 64 se()] is invoked
6de0: 20 77 68 69 6c 65 20 61 20 74 72 61 6e 73 61 63 while a transac
6df0: 74 69 6f 6e 20 69 73 20 6f 70 65 6e 2c 0a 2a 2a tion is open,.**
6e00: 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e the transaction
6e10: 20 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c is automaticall
6e20: 79 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a y rolled back..*
6e30: 2a 0a 2a 2a 20 54 68 65 20 43 20 70 61 72 61 6d *.** The C param
6e40: 65 74 65 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 eter to [sqlite3
6e50: 5f 63 6c 6f 73 65 28 43 29 5d 20 6d 75 73 74 20 _close(C)] must
6e60: 62 65 20 65 69 74 68 65 72 20 61 20 4e 55 4c 4c be either a NULL
6e70: 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 6f 72 20 61 .** pointer or a
6e80: 6e 20 5b 73 71 6c 69 74 65 33 5d 20 6f 62 6a 65 n [sqlite3] obje
6e90: 63 74 20 70 6f 69 6e 74 65 72 20 6f 62 74 61 69 ct pointer obtai
6ea0: 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 5b 73 71 6c ned.** from [sql
6eb0: 69 74 65 33 5f 6f 70 65 6e 28 29 5d 2c 20 5b 73 ite3_open()], [s
6ec0: 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 5d qlite3_open16()]
6ed0: 2c 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 , or.** [sqlite3
6ee0: 5f 6f 70 65 6e 5f 76 32 28 29 5d 2c 20 61 6e 64 _open_v2()], and
6ef0: 20 6e 6f 74 20 70 72 65 76 69 6f 75 73 6c 79 20 not previously
6f00: 63 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 closed..**.** Re
6f10: 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b quirements:.** [
6f20: 48 31 32 30 31 31 5d 20 5b 48 31 32 30 31 32 5d H12011] [H12012]
6f30: 20 5b 48 31 32 30 31 33 5d 20 5b 48 31 32 30 31 [H12013] [H1201
6f40: 34 5d 20 5b 48 31 32 30 31 35 5d 20 5b 48 31 32 4] [H12015] [H12
6f50: 30 31 39 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 019].*/.SQLITE_A
6f60: 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 PI int sqlite3_c
6f70: 6c 6f 73 65 28 73 71 6c 69 74 65 33 20 2a 29 3b lose(sqlite3 *);
6f80: 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 74 79 70 65 ../*.** The type
6f90: 20 66 6f 72 20 61 20 63 61 6c 6c 62 61 63 6b 20 for a callback
6fa0: 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69 function..** Thi
6fb0: 73 20 69 73 20 6c 65 67 61 63 79 20 61 6e 64 20 s is legacy and
6fc0: 64 65 70 72 65 63 61 74 65 64 2e 20 20 49 74 20 deprecated. It
6fd0: 69 73 20 69 6e 63 6c 75 64 65 64 20 66 6f 72 20 is included for
6fe0: 68 69 73 74 6f 72 69 63 61 6c 0a 2a 2a 20 63 6f historical.** co
6ff0: 6d 70 61 74 69 62 69 6c 69 74 79 20 61 6e 64 20 mpatibility and
7000: 69 73 20 6e 6f 74 20 64 6f 63 75 6d 65 6e 74 65 is not documente
7010: 64 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 69 6e d..*/.typedef in
7020: 74 20 28 2a 73 71 6c 69 74 65 33 5f 63 61 6c 6c t (*sqlite3_call
7030: 62 61 63 6b 29 28 76 6f 69 64 2a 2c 69 6e 74 2c back)(void*,int,
7040: 63 68 61 72 2a 2a 2c 20 63 68 61 72 2a 2a 29 3b char**, char**);
7050: 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
7060: 3a 20 4f 6e 65 2d 53 74 65 70 20 51 75 65 72 79 : One-Step Query
7070: 20 45 78 65 63 75 74 69 6f 6e 20 49 6e 74 65 72 Execution Inter
7080: 66 61 63 65 20 7b 48 31 32 31 30 30 7d 20 3c 53 face {H12100} <S
7090: 31 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 10000>.**.** The
70a0: 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20 sqlite3_exec()
70b0: 69 6e 74 65 72 66 61 63 65 20 69 73 20 61 20 63 interface is a c
70c0: 6f 6e 76 65 6e 69 65 6e 74 20 77 61 79 20 6f 66 onvenient way of
70d0: 20 72 75 6e 6e 69 6e 67 20 6f 6e 65 20 6f 72 20 running one or
70e0: 6d 6f 72 65 0a 2a 2a 20 53 51 4c 20 73 74 61 74 more.** SQL stat
70f0: 65 6d 65 6e 74 73 20 77 69 74 68 6f 75 74 20 68 ements without h
7100: 61 76 69 6e 67 20 74 6f 20 77 72 69 74 65 20 61 aving to write a
7110: 20 6c 6f 74 20 6f 66 20 43 20 63 6f 64 65 2e 20 lot of C code.
7120: 20 54 68 65 20 55 54 46 2d 38 20 65 6e 63 6f 64 The UTF-8 encod
7130: 65 64 0a 2a 2a 20 53 51 4c 20 73 74 61 74 65 6d ed.** SQL statem
7140: 65 6e 74 73 20 61 72 65 20 70 61 73 73 65 64 20 ents are passed
7150: 69 6e 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 in as the second
7160: 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 parameter to sq
7170: 6c 69 74 65 33 5f 65 78 65 63 28 29 2e 0a 2a 2a lite3_exec()..**
7180: 20 54 68 65 20 73 74 61 74 65 6d 65 6e 74 73 20 The statements
7190: 61 72 65 20 65 76 61 6c 75 61 74 65 64 20 6f 6e are evaluated on
71a0: 65 20 62 79 20 6f 6e 65 20 75 6e 74 69 6c 20 65 e by one until e
71b0: 69 74 68 65 72 20 61 6e 20 65 72 72 6f 72 20 6f ither an error o
71c0: 72 0a 2a 2a 20 61 6e 20 69 6e 74 65 72 72 75 70 r.** an interrup
71d0: 74 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 t is encountered
71e0: 2c 20 6f 72 20 75 6e 74 69 6c 20 74 68 65 79 20 , or until they
71f0: 61 72 65 20 61 6c 6c 20 64 6f 6e 65 2e 20 20 54 are all done. T
7200: 68 65 20 33 72 64 20 70 61 72 61 6d 65 74 65 72 he 3rd parameter
7210: 0a 2a 2a 20 69 73 20 61 6e 20 6f 70 74 69 6f 6e .** is an option
7220: 61 6c 20 63 61 6c 6c 62 61 63 6b 20 74 68 61 74 al callback that
7230: 20 69 73 20 69 6e 76 6f 6b 65 64 20 6f 6e 63 65 is invoked once
7240: 20 66 6f 72 20 65 61 63 68 20 72 6f 77 20 6f 66 for each row of
7250: 20 61 6e 79 20 71 75 65 72 79 0a 2a 2a 20 72 65 any query.** re
7260: 73 75 6c 74 73 20 70 72 6f 64 75 63 65 64 20 62 sults produced b
7270: 79 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d y the SQL statem
7280: 65 6e 74 73 2e 20 20 54 68 65 20 35 74 68 20 70 ents. The 5th p
7290: 61 72 61 6d 65 74 65 72 20 74 65 6c 6c 73 20 77 arameter tells w
72a0: 68 65 72 65 0a 2a 2a 20 74 6f 20 77 72 69 74 65 here.** to write
72b0: 20 61 6e 79 20 65 72 72 6f 72 20 6d 65 73 73 61 any error messa
72c0: 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 65 ges..**.** The e
72d0: 72 72 6f 72 20 6d 65 73 73 61 67 65 20 70 61 73 rror message pas
72e0: 73 65 64 20 62 61 63 6b 20 74 68 72 6f 75 67 68 sed back through
72f0: 20 74 68 65 20 35 74 68 20 70 61 72 61 6d 65 74 the 5th paramet
7300: 65 72 20 69 73 20 68 65 6c 64 0a 2a 2a 20 69 6e er is held.** in
7310: 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 memory obtained
7320: 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d from [sqlite3_m
7330: 61 6c 6c 6f 63 28 29 5d 2e 20 20 54 6f 20 61 76 alloc()]. To av
7340: 6f 69 64 20 61 20 6d 65 6d 6f 72 79 20 6c 65 61 oid a memory lea
7350: 6b 2c 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 69 6e k,.** the callin
7360: 67 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 73 68 g application sh
7370: 6f 75 6c 64 20 63 61 6c 6c 20 5b 73 71 6c 69 74 ould call [sqlit
7380: 65 33 5f 66 72 65 65 28 29 5d 20 6f 6e 20 61 6e e3_free()] on an
7390: 79 20 65 72 72 6f 72 0a 2a 2a 20 6d 65 73 73 61 y error.** messa
73a0: 67 65 20 72 65 74 75 72 6e 65 64 20 74 68 72 6f ge returned thro
73b0: 75 67 68 20 74 68 65 20 35 74 68 20 70 61 72 61 ugh the 5th para
73c0: 6d 65 74 65 72 20 77 68 65 6e 20 69 74 20 68 61 meter when it ha
73d0: 73 20 66 69 6e 69 73 68 65 64 20 75 73 69 6e 67 s finished using
73e0: 0a 2a 2a 20 74 68 65 20 65 72 72 6f 72 20 6d 65 .** the error me
73f0: 73 73 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 ssage..**.** If
7400: 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e the SQL statemen
7410: 74 20 69 6e 20 74 68 65 20 32 6e 64 20 70 61 72 t in the 2nd par
7420: 61 6d 65 74 65 72 20 69 73 20 4e 55 4c 4c 20 6f ameter is NULL o
7430: 72 20 61 6e 20 65 6d 70 74 79 20 73 74 72 69 6e r an empty strin
7440: 67 0a 2a 2a 20 6f 72 20 61 20 73 74 72 69 6e 67 g.** or a string
7450: 20 63 6f 6e 74 61 69 6e 69 6e 67 20 6f 6e 6c 79 containing only
7460: 20 77 68 69 74 65 73 70 61 63 65 20 61 6e 64 20 whitespace and
7470: 63 6f 6d 6d 65 6e 74 73 2c 20 74 68 65 6e 20 6e comments, then n
7480: 6f 20 53 51 4c 0a 2a 2a 20 73 74 61 74 65 6d 65 o SQL.** stateme
7490: 6e 74 73 20 61 72 65 20 65 76 61 6c 75 61 74 65 nts are evaluate
74a0: 64 20 61 6e 64 20 74 68 65 20 64 61 74 61 62 61 d and the databa
74b0: 73 65 20 69 73 20 6e 6f 74 20 63 68 61 6e 67 65 se is not change
74c0: 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c d..**.** The sql
74d0: 69 74 65 33 5f 65 78 65 63 28 29 20 69 6e 74 65 ite3_exec() inte
74e0: 72 66 61 63 65 20 69 73 20 69 6d 70 6c 65 6d 65 rface is impleme
74f0: 6e 74 65 64 20 69 6e 20 74 65 72 6d 73 20 6f 66 nted in terms of
7500: 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 .** [sqlite3_pre
7510: 70 61 72 65 5f 76 32 28 29 5d 2c 20 5b 73 71 6c pare_v2()], [sql
7520: 69 74 65 33 5f 73 74 65 70 28 29 5d 2c 20 61 6e ite3_step()], an
7530: 64 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c d [sqlite3_final
7540: 69 7a 65 28 29 5d 2e 0a 2a 2a 20 54 68 65 20 73 ize()]..** The s
7550: 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20 72 6f qlite3_exec() ro
7560: 75 74 69 6e 65 20 64 6f 65 73 20 6e 6f 74 68 69 utine does nothi
7570: 6e 67 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 ng to the databa
7580: 73 65 20 74 68 61 74 20 63 61 6e 6e 6f 74 20 62 se that cannot b
7590: 65 20 64 6f 6e 65 0a 2a 2a 20 62 79 20 5b 73 71 e done.** by [sq
75a0: 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 lite3_prepare_v2
75b0: 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 73 74 ()], [sqlite3_st
75c0: 65 70 28 29 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 ep()], and [sqli
75d0: 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 2e te3_finalize()].
75e0: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 .**.** The first
75f0: 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 parameter to [s
7600: 71 6c 69 74 65 33 5f 65 78 65 63 28 29 5d 20 6d qlite3_exec()] m
7610: 75 73 74 20 62 65 20 61 6e 20 76 61 6c 69 64 20 ust be an valid
7620: 61 6e 64 20 6f 70 65 6e 0a 2a 2a 20 5b 64 61 74 and open.** [dat
7630: 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
7640: 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 61 74 ]..**.** The dat
7650: 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
7660: 20 6d 75 73 74 20 6e 6f 74 20 62 65 20 63 6c 6f must not be clo
7670: 73 65 64 20 77 68 69 6c 65 0a 2a 2a 20 5b 73 71 sed while.** [sq
7680: 6c 69 74 65 33 5f 65 78 65 63 28 29 5d 20 69 73 lite3_exec()] is
7690: 20 72 75 6e 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 running..**.**
76a0: 54 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 The calling func
76b0: 74 69 6f 6e 20 73 68 6f 75 6c 64 20 75 73 65 20 tion should use
76c0: 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d [sqlite3_free()]
76d0: 20 74 6f 20 66 72 65 65 0a 2a 2a 20 74 68 65 20 to free.** the
76e0: 6d 65 6d 6f 72 79 20 74 68 61 74 20 2a 65 72 72 memory that *err
76f0: 6d 73 67 20 69 73 20 6c 65 66 74 20 70 6f 69 6e msg is left poin
7700: 74 69 6e 67 20 61 74 20 6f 6e 63 65 20 74 68 65 ting at once the
7710: 20 65 72 72 6f 72 0a 2a 2a 20 6d 65 73 73 61 67 error.** messag
7720: 65 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 6e e is no longer n
7730: 65 65 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 eeded..**.** The
7740: 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 74 SQL statement t
7750: 65 78 74 20 69 6e 20 74 68 65 20 32 6e 64 20 70 ext in the 2nd p
7760: 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c arameter to [sql
7770: 69 74 65 33 5f 65 78 65 63 28 29 5d 0a 2a 2a 20 ite3_exec()].**
7780: 6d 75 73 74 20 72 65 6d 61 69 6e 20 75 6e 63 68 must remain unch
7790: 61 6e 67 65 64 20 77 68 69 6c 65 20 5b 73 71 6c anged while [sql
77a0: 69 74 65 33 5f 65 78 65 63 28 29 5d 20 69 73 20 ite3_exec()] is
77b0: 72 75 6e 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 52 running..**.** R
77c0: 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 equirements:.**
77d0: 5b 48 31 32 31 30 31 5d 20 5b 48 31 32 31 30 32 [H12101] [H12102
77e0: 5d 20 5b 48 31 32 31 30 34 5d 20 5b 48 31 32 31 ] [H12104] [H121
77f0: 30 35 5d 20 5b 48 31 32 31 30 37 5d 20 5b 48 31 05] [H12107] [H1
7800: 32 31 31 30 5d 20 5b 48 31 32 31 31 33 5d 20 5b 2110] [H12113] [
7810: 48 31 32 31 31 36 5d 0a 2a 2a 20 5b 48 31 32 31 H12116].** [H121
7820: 31 39 5d 20 5b 48 31 32 31 32 32 5d 20 5b 48 31 19] [H12122] [H1
7830: 32 31 32 35 5d 20 5b 48 31 32 31 33 31 5d 20 5b 2125] [H12131] [
7840: 48 31 32 31 33 34 5d 20 5b 48 31 32 31 33 37 5d H12134] [H12137]
7850: 20 5b 48 31 32 31 33 38 5d 0a 2a 2f 0a 53 51 4c [H12138].*/.SQL
7860: 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
7870: 74 65 33 5f 65 78 65 63 28 0a 20 20 73 71 6c 69 te3_exec(. sqli
7880: 74 65 33 2a 2c 20 20 20 20 20 20 20 20 20 20 20 te3*,
7890: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
78a0: 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 6f 70 65 /* An ope
78b0: 6e 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 n database */.
78c0: 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 2c const char *sql,
78d0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
78e0: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 51 /* SQ
78f0: 4c 20 74 6f 20 62 65 20 65 76 61 6c 75 61 74 65 L to be evaluate
7900: 64 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 63 61 6c d */. int (*cal
7910: 6c 62 61 63 6b 29 28 76 6f 69 64 2a 2c 69 6e 74 lback)(void*,int
7920: 2c 63 68 61 72 2a 2a 2c 63 68 61 72 2a 2a 29 2c ,char**,char**),
7930: 20 20 2f 2a 20 43 61 6c 6c 62 61 63 6b 20 66 75 /* Callback fu
7940: 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 nction */. void
7950: 20 2a 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 *,
7960: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7970: 20 20 20 20 20 20 20 2f 2a 20 31 73 74 20 61 72 /* 1st ar
7980: 67 75 6d 65 6e 74 20 74 6f 20 63 61 6c 6c 62 61 gument to callba
7990: 63 6b 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 65 ck */. char **e
79a0: 72 72 6d 73 67 20 20 20 20 20 20 20 20 20 20 20 rrmsg
79b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
79c0: 20 20 20 2f 2a 20 45 72 72 6f 72 20 6d 73 67 20 /* Error msg
79d0: 77 72 69 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a written here */.
79e0: 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
79f0: 45 46 3a 20 52 65 73 75 6c 74 20 43 6f 64 65 73 EF: Result Codes
7a00: 20 7b 48 31 30 32 31 30 7d 20 3c 53 31 30 37 30 {H10210} <S1070
7a10: 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 0>.** KEYWORDS:
7a20: 53 51 4c 49 54 45 5f 4f 4b 20 7b 65 72 72 6f 72 SQLITE_OK {error
7a30: 20 63 6f 64 65 7d 20 7b 65 72 72 6f 72 20 63 6f code} {error co
7a40: 64 65 73 7d 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 des}.** KEYWORDS
7a50: 3a 20 7b 72 65 73 75 6c 74 20 63 6f 64 65 7d 20 : {result code}
7a60: 7b 72 65 73 75 6c 74 20 63 6f 64 65 73 7d 0a 2a {result codes}.*
7a70: 2a 0a 2a 2a 20 4d 61 6e 79 20 53 51 4c 69 74 65 *.** Many SQLite
7a80: 20 66 75 6e 63 74 69 6f 6e 73 20 72 65 74 75 72 functions retur
7a90: 6e 20 61 6e 20 69 6e 74 65 67 65 72 20 72 65 73 n an integer res
7aa0: 75 6c 74 20 63 6f 64 65 20 66 72 6f 6d 20 74 68 ult code from th
7ab0: 65 20 73 65 74 20 73 68 6f 77 6e 0a 2a 2a 20 68 e set shown.** h
7ac0: 65 72 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 ere in order to
7ad0: 69 6e 64 69 63 61 74 65 73 20 73 75 63 63 65 73 indicates succes
7ae0: 73 20 6f 72 20 66 61 69 6c 75 72 65 2e 0a 2a 2a s or failure..**
7af0: 0a 2a 2a 20 4e 65 77 20 65 72 72 6f 72 20 63 6f .** New error co
7b00: 64 65 73 20 6d 61 79 20 62 65 20 61 64 64 65 64 des may be added
7b10: 20 69 6e 20 66 75 74 75 72 65 20 76 65 72 73 69 in future versi
7b20: 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a ons of SQLite..*
7b30: 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b *.** See also: [
7b40: 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 41 SQLITE_IOERR_REA
7b50: 44 20 7c 20 65 78 74 65 6e 64 65 64 20 72 65 73 D | extended res
7b60: 75 6c 74 20 63 6f 64 65 73 5d 0a 2a 2f 0a 23 64 ult codes].*/.#d
7b70: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 4b 20 efine SQLITE_OK
7b80: 20 20 20 20 20 20 20 20 20 20 30 20 20 20 2f 2a 0 /*
7b90: 20 53 75 63 63 65 73 73 66 75 6c 20 72 65 73 75 Successful resu
7ba0: 6c 74 20 2a 2f 0a 2f 2a 20 62 65 67 69 6e 6e 69 lt */./* beginni
7bb0: 6e 67 2d 6f 66 2d 65 72 72 6f 72 2d 63 6f 64 65 ng-of-error-code
7bc0: 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c s */.#define SQL
7bd0: 49 54 45 5f 45 52 52 4f 52 20 20 20 20 20 20 20 ITE_ERROR
7be0: 20 31 20 20 20 2f 2a 20 53 51 4c 20 65 72 72 6f 1 /* SQL erro
7bf0: 72 20 6f 72 20 6d 69 73 73 69 6e 67 20 64 61 74 r or missing dat
7c00: 61 62 61 73 65 20 2a 2f 0a 23 64 65 66 69 6e 65 abase */.#define
7c10: 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 4e 41 4c SQLITE_INTERNAL
7c20: 20 20 20 20 20 32 20 20 20 2f 2a 20 49 6e 74 65 2 /* Inte
7c30: 72 6e 61 6c 20 6c 6f 67 69 63 20 65 72 72 6f 72 rnal logic error
7c40: 20 69 6e 20 53 51 4c 69 74 65 20 2a 2f 0a 23 64 in SQLite */.#d
7c50: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 50 45 52 efine SQLITE_PER
7c60: 4d 20 20 20 20 20 20 20 20 20 33 20 20 20 2f 2a M 3 /*
7c70: 20 41 63 63 65 73 73 20 70 65 72 6d 69 73 73 69 Access permissi
7c80: 6f 6e 20 64 65 6e 69 65 64 20 2a 2f 0a 23 64 65 on denied */.#de
7c90: 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 42 4f 52 fine SQLITE_ABOR
7ca0: 54 20 20 20 20 20 20 20 20 34 20 20 20 2f 2a 20 T 4 /*
7cb0: 43 61 6c 6c 62 61 63 6b 20 72 6f 75 74 69 6e 65 Callback routine
7cc0: 20 72 65 71 75 65 73 74 65 64 20 61 6e 20 61 62 requested an ab
7cd0: 6f 72 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 ort */.#define S
7ce0: 51 4c 49 54 45 5f 42 55 53 59 20 20 20 20 20 20 QLITE_BUSY
7cf0: 20 20 20 35 20 20 20 2f 2a 20 54 68 65 20 64 61 5 /* The da
7d00: 74 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 6c tabase file is l
7d10: 6f 63 6b 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 ocked */.#define
7d20: 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 20 SQLITE_LOCKED
7d30: 20 20 20 20 20 36 20 20 20 2f 2a 20 41 20 74 61 6 /* A ta
7d40: 62 6c 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 ble in the datab
7d50: 61 73 65 20 69 73 20 6c 6f 63 6b 65 64 20 2a 2f ase is locked */
7d60: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
7d70: 4e 4f 4d 45 4d 20 20 20 20 20 20 20 20 37 20 20 NOMEM 7
7d80: 20 2f 2a 20 41 20 6d 61 6c 6c 6f 63 28 29 20 66 /* A malloc() f
7d90: 61 69 6c 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 ailed */.#define
7da0: 20 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 SQLITE_READONLY
7db0: 20 20 20 20 20 38 20 20 20 2f 2a 20 41 74 74 65 8 /* Atte
7dc0: 6d 70 74 20 74 6f 20 77 72 69 74 65 20 61 20 72 mpt to write a r
7dd0: 65 61 64 6f 6e 6c 79 20 64 61 74 61 62 61 73 65 eadonly database
7de0: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
7df0: 54 45 5f 49 4e 54 45 52 52 55 50 54 20 20 20 20 TE_INTERRUPT
7e00: 39 20 20 20 2f 2a 20 4f 70 65 72 61 74 69 6f 6e 9 /* Operation
7e10: 20 74 65 72 6d 69 6e 61 74 65 64 20 62 79 20 73 terminated by s
7e20: 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 qlite3_interrupt
7e30: 28 29 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c ()*/.#define SQL
7e40: 49 54 45 5f 49 4f 45 52 52 20 20 20 20 20 20 20 ITE_IOERR
7e50: 31 30 20 20 20 2f 2a 20 53 6f 6d 65 20 6b 69 6e 10 /* Some kin
7e60: 64 20 6f 66 20 64 69 73 6b 20 49 2f 4f 20 65 72 d of disk I/O er
7e70: 72 6f 72 20 6f 63 63 75 72 72 65 64 20 2a 2f 0a ror occurred */.
7e80: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 #define SQLITE_C
7e90: 4f 52 52 55 50 54 20 20 20 20 20 31 31 20 20 20 ORRUPT 11
7ea0: 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 /* The database
7eb0: 64 69 73 6b 20 69 6d 61 67 65 20 69 73 20 6d 61 disk image is ma
7ec0: 6c 66 6f 72 6d 65 64 20 2a 2f 0a 23 64 65 66 69 lformed */.#defi
7ed0: 6e 65 20 53 51 4c 49 54 45 5f 4e 4f 54 46 4f 55 ne SQLITE_NOTFOU
7ee0: 4e 44 20 20 20 20 31 32 20 20 20 2f 2a 20 4e 4f ND 12 /* NO
7ef0: 54 20 55 53 45 44 2e 20 54 61 62 6c 65 20 6f 72 T USED. Table or
7f00: 20 72 65 63 6f 72 64 20 6e 6f 74 20 66 6f 75 6e record not foun
7f10: 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c d */.#define SQL
7f20: 49 54 45 5f 46 55 4c 4c 20 20 20 20 20 20 20 20 ITE_FULL
7f30: 31 33 20 20 20 2f 2a 20 49 6e 73 65 72 74 69 6f 13 /* Insertio
7f40: 6e 20 66 61 69 6c 65 64 20 62 65 63 61 75 73 65 n failed because
7f50: 20 64 61 74 61 62 61 73 65 20 69 73 20 66 75 6c database is ful
7f60: 6c 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c l */.#define SQL
7f70: 49 54 45 5f 43 41 4e 54 4f 50 45 4e 20 20 20 20 ITE_CANTOPEN
7f80: 31 34 20 20 20 2f 2a 20 55 6e 61 62 6c 65 20 74 14 /* Unable t
7f90: 6f 20 6f 70 65 6e 20 74 68 65 20 64 61 74 61 62 o open the datab
7fa0: 61 73 65 20 66 69 6c 65 20 2a 2f 0a 23 64 65 66 ase file */.#def
7fb0: 69 6e 65 20 53 51 4c 49 54 45 5f 50 52 4f 54 4f ine SQLITE_PROTO
7fc0: 43 4f 4c 20 20 20 20 31 35 20 20 20 2f 2a 20 4e COL 15 /* N
7fd0: 4f 54 20 55 53 45 44 2e 20 44 61 74 61 62 61 73 OT USED. Databas
7fe0: 65 20 6c 6f 63 6b 20 70 72 6f 74 6f 63 6f 6c 20 e lock protocol
7ff0: 65 72 72 6f 72 20 2a 2f 0a 23 64 65 66 69 6e 65 error */.#define
8000: 20 53 51 4c 49 54 45 5f 45 4d 50 54 59 20 20 20 SQLITE_EMPTY
8010: 20 20 20 20 31 36 20 20 20 2f 2a 20 44 61 74 61 16 /* Data
8020: 62 61 73 65 20 69 73 20 65 6d 70 74 79 20 2a 2f base is empty */
8030: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
8040: 53 43 48 45 4d 41 20 20 20 20 20 20 31 37 20 20 SCHEMA 17
8050: 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 /* The database
8060: 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 64 20 schema changed
8070: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
8080: 45 5f 54 4f 4f 42 49 47 20 20 20 20 20 20 31 38 E_TOOBIG 18
8090: 20 20 20 2f 2a 20 53 74 72 69 6e 67 20 6f 72 20 /* String or
80a0: 42 4c 4f 42 20 65 78 63 65 65 64 73 20 73 69 7a BLOB exceeds siz
80b0: 65 20 6c 69 6d 69 74 20 2a 2f 0a 23 64 65 66 69 e limit */.#defi
80c0: 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 ne SQLITE_CONSTR
80d0: 41 49 4e 54 20 20 31 39 20 20 20 2f 2a 20 41 62 AINT 19 /* Ab
80e0: 6f 72 74 20 64 75 65 20 74 6f 20 63 6f 6e 73 74 ort due to const
80f0: 72 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 20 raint violation
8100: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
8110: 45 5f 4d 49 53 4d 41 54 43 48 20 20 20 20 32 30 E_MISMATCH 20
8120: 20 20 20 2f 2a 20 44 61 74 61 20 74 79 70 65 20 /* Data type
8130: 6d 69 73 6d 61 74 63 68 20 2a 2f 0a 23 64 65 66 mismatch */.#def
8140: 69 6e 65 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 ine SQLITE_MISUS
8150: 45 20 20 20 20 20 20 32 31 20 20 20 2f 2a 20 4c E 21 /* L
8160: 69 62 72 61 72 79 20 75 73 65 64 20 69 6e 63 6f ibrary used inco
8170: 72 72 65 63 74 6c 79 20 2a 2f 0a 23 64 65 66 69 rrectly */.#defi
8180: 6e 65 20 53 51 4c 49 54 45 5f 4e 4f 4c 46 53 20 ne SQLITE_NOLFS
8190: 20 20 20 20 20 20 32 32 20 20 20 2f 2a 20 55 73 22 /* Us
81a0: 65 73 20 4f 53 20 66 65 61 74 75 72 65 73 20 6e es OS features n
81b0: 6f 74 20 73 75 70 70 6f 72 74 65 64 20 6f 6e 20 ot supported on
81c0: 68 6f 73 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 host */.#define
81d0: 53 51 4c 49 54 45 5f 41 55 54 48 20 20 20 20 20 SQLITE_AUTH
81e0: 20 20 20 32 33 20 20 20 2f 2a 20 41 75 74 68 6f 23 /* Autho
81f0: 72 69 7a 61 74 69 6f 6e 20 64 65 6e 69 65 64 20 rization denied
8200: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
8210: 45 5f 46 4f 52 4d 41 54 20 20 20 20 20 20 32 34 E_FORMAT 24
8220: 20 20 20 2f 2a 20 41 75 78 69 6c 69 61 72 79 20 /* Auxiliary
8230: 64 61 74 61 62 61 73 65 20 66 6f 72 6d 61 74 20 database format
8240: 65 72 72 6f 72 20 2a 2f 0a 23 64 65 66 69 6e 65 error */.#define
8250: 20 53 51 4c 49 54 45 5f 52 41 4e 47 45 20 20 20 SQLITE_RANGE
8260: 20 20 20 20 32 35 20 20 20 2f 2a 20 32 6e 64 20 25 /* 2nd
8270: 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c parameter to sql
8280: 69 74 65 33 5f 62 69 6e 64 20 6f 75 74 20 6f 66 ite3_bind out of
8290: 20 72 61 6e 67 65 20 2a 2f 0a 23 64 65 66 69 6e range */.#defin
82a0: 65 20 53 51 4c 49 54 45 5f 4e 4f 54 41 44 42 20 e SQLITE_NOTADB
82b0: 20 20 20 20 20 32 36 20 20 20 2f 2a 20 46 69 6c 26 /* Fil
82c0: 65 20 6f 70 65 6e 65 64 20 74 68 61 74 20 69 73 e opened that is
82d0: 20 6e 6f 74 20 61 20 64 61 74 61 62 61 73 65 20 not a database
82e0: 66 69 6c 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 file */.#define
82f0: 53 51 4c 49 54 45 5f 52 4f 57 20 20 20 20 20 20 SQLITE_ROW
8300: 20 20 20 31 30 30 20 20 2f 2a 20 73 71 6c 69 74 100 /* sqlit
8310: 65 33 5f 73 74 65 70 28 29 20 68 61 73 20 61 6e e3_step() has an
8320: 6f 74 68 65 72 20 72 6f 77 20 72 65 61 64 79 20 other row ready
8330: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
8340: 45 5f 44 4f 4e 45 20 20 20 20 20 20 20 20 31 30 E_DONE 10
8350: 31 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 73 74 1 /* sqlite3_st
8360: 65 70 28 29 20 68 61 73 20 66 69 6e 69 73 68 65 ep() has finishe
8370: 64 20 65 78 65 63 75 74 69 6e 67 20 2a 2f 0a 2f d executing */./
8380: 2a 20 65 6e 64 2d 6f 66 2d 65 72 72 6f 72 2d 63 * end-of-error-c
8390: 6f 64 65 73 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 odes */../*.** C
83a0: 41 50 49 33 52 45 46 3a 20 45 78 74 65 6e 64 65 API3REF: Extende
83b0: 64 20 52 65 73 75 6c 74 20 43 6f 64 65 73 20 7b d Result Codes {
83c0: 48 31 30 32 32 30 7d 20 3c 53 31 30 37 30 30 3e H10220} <S10700>
83d0: 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 65 .** KEYWORDS: {e
83e0: 78 74 65 6e 64 65 64 20 65 72 72 6f 72 20 63 6f xtended error co
83f0: 64 65 7d 20 7b 65 78 74 65 6e 64 65 64 20 65 72 de} {extended er
8400: 72 6f 72 20 63 6f 64 65 73 7d 0a 2a 2a 20 4b 45 ror codes}.** KE
8410: 59 57 4f 52 44 53 3a 20 7b 65 78 74 65 6e 64 65 YWORDS: {extende
8420: 64 20 72 65 73 75 6c 74 20 63 6f 64 65 7d 20 7b d result code} {
8430: 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 extended result
8440: 63 6f 64 65 73 7d 0a 2a 2a 0a 2a 2a 20 49 6e 20 codes}.**.** In
8450: 69 74 73 20 64 65 66 61 75 6c 74 20 63 6f 6e 66 its default conf
8460: 69 67 75 72 61 74 69 6f 6e 2c 20 53 51 4c 69 74 iguration, SQLit
8470: 65 20 41 50 49 20 72 6f 75 74 69 6e 65 73 20 72 e API routines r
8480: 65 74 75 72 6e 20 6f 6e 65 20 6f 66 20 32 36 20 eturn one of 26
8490: 69 6e 74 65 67 65 72 0a 2a 2a 20 5b 53 51 4c 49 integer.** [SQLI
84a0: 54 45 5f 4f 4b 20 7c 20 72 65 73 75 6c 74 20 63 TE_OK | result c
84b0: 6f 64 65 73 5d 2e 20 20 48 6f 77 65 76 65 72 2c odes]. However,
84c0: 20 65 78 70 65 72 69 65 6e 63 65 20 68 61 73 20 experience has
84d0: 73 68 6f 77 6e 20 74 68 61 74 20 6d 61 6e 79 20 shown that many
84e0: 6f 66 0a 2a 2a 20 74 68 65 73 65 20 72 65 73 75 of.** these resu
84f0: 6c 74 20 63 6f 64 65 73 20 61 72 65 20 74 6f 6f lt codes are too
8500: 20 63 6f 61 72 73 65 2d 67 72 61 69 6e 65 64 2e coarse-grained.
8510: 20 20 54 68 65 79 20 64 6f 20 6e 6f 74 20 70 72 They do not pr
8520: 6f 76 69 64 65 20 61 73 0a 2a 2a 20 6d 75 63 68 ovide as.** much
8530: 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f information abo
8540: 75 74 20 70 72 6f 62 6c 65 6d 73 20 61 73 20 70 ut problems as p
8550: 72 6f 67 72 61 6d 6d 65 72 73 20 6d 69 67 68 74 rogrammers might
8560: 20 6c 69 6b 65 2e 20 20 49 6e 20 61 6e 20 65 66 like. In an ef
8570: 66 6f 72 74 20 74 6f 0a 2a 2a 20 61 64 64 72 65 fort to.** addre
8580: 73 73 20 74 68 69 73 2c 20 6e 65 77 65 72 20 76 ss this, newer v
8590: 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 ersions of SQLit
85a0: 65 20 28 76 65 72 73 69 6f 6e 20 33 2e 33 2e 38 e (version 3.3.8
85b0: 20 61 6e 64 20 6c 61 74 65 72 29 20 69 6e 63 6c and later) incl
85c0: 75 64 65 0a 2a 2a 20 73 75 70 70 6f 72 74 20 66 ude.** support f
85d0: 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 72 65 or additional re
85e0: 73 75 6c 74 20 63 6f 64 65 73 20 74 68 61 74 20 sult codes that
85f0: 70 72 6f 76 69 64 65 20 6d 6f 72 65 20 64 65 74 provide more det
8600: 61 69 6c 65 64 20 69 6e 66 6f 72 6d 61 74 69 6f ailed informatio
8610: 6e 0a 2a 2a 20 61 62 6f 75 74 20 65 72 72 6f 72 n.** about error
8620: 73 2e 20 54 68 65 20 65 78 74 65 6e 64 65 64 20 s. The extended
8630: 72 65 73 75 6c 74 20 63 6f 64 65 73 20 61 72 65 result codes are
8640: 20 65 6e 61 62 6c 65 64 20 6f 72 20 64 69 73 61 enabled or disa
8650: 62 6c 65 64 0a 2a 2a 20 6f 6e 20 61 20 70 65 72 bled.** on a per
8660: 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
8670: 74 69 6f 6e 20 62 61 73 69 73 20 75 73 69 6e 67 tion basis using
8680: 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 the.** [sqlite3
8690: 5f 65 78 74 65 6e 64 65 64 5f 72 65 73 75 6c 74 _extended_result
86a0: 5f 63 6f 64 65 73 28 29 5d 20 41 50 49 2e 0a 2a _codes()] API..*
86b0: 2a 0a 2a 2a 20 53 6f 6d 65 20 6f 66 20 74 68 65 *.** Some of the
86c0: 20 61 76 61 69 6c 61 62 6c 65 20 65 78 74 65 6e available exten
86d0: 64 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73 ded result codes
86e0: 20 61 72 65 20 6c 69 73 74 65 64 20 68 65 72 65 are listed here
86f0: 2e 0a 2a 2a 20 4f 6e 65 20 6d 61 79 20 65 78 70 ..** One may exp
8700: 65 63 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f ect the number o
8710: 66 20 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c f extended resul
8720: 74 20 63 6f 64 65 73 20 77 69 6c 6c 20 62 65 20 t codes will be
8730: 65 78 70 61 6e 64 0a 2a 2a 20 6f 76 65 72 20 74 expand.** over t
8740: 69 6d 65 2e 20 20 53 6f 66 74 77 61 72 65 20 74 ime. Software t
8750: 68 61 74 20 75 73 65 73 20 65 78 74 65 6e 64 65 hat uses extende
8760: 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73 20 73 d result codes s
8770: 68 6f 75 6c 64 20 65 78 70 65 63 74 0a 2a 2a 20 hould expect.**
8780: 74 6f 20 73 65 65 20 6e 65 77 20 72 65 73 75 6c to see new resul
8790: 74 20 63 6f 64 65 73 20 69 6e 20 66 75 74 75 72 t codes in futur
87a0: 65 20 72 65 6c 65 61 73 65 73 20 6f 66 20 53 51 e releases of SQ
87b0: 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 Lite..**.** The
87c0: 53 51 4c 49 54 45 5f 4f 4b 20 72 65 73 75 6c 74 SQLITE_OK result
87d0: 20 63 6f 64 65 20 77 69 6c 6c 20 6e 65 76 65 72 code will never
87e0: 20 62 65 20 65 78 74 65 6e 64 65 64 2e 20 20 49 be extended. I
87f0: 74 20 77 69 6c 6c 20 61 6c 77 61 79 73 0a 2a 2a t will always.**
8800: 20 62 65 20 65 78 61 63 74 6c 79 20 7a 65 72 6f be exactly zero
8810: 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c ..*/.#define SQL
8820: 49 54 45 5f 49 4f 45 52 52 5f 52 45 41 44 20 20 ITE_IOERR_READ
8830: 20 20 20 20 20 20 20 20 20 20 20 20 28 53 51 4c (SQL
8840: 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 31 3c 3c ITE_IOERR | (1<<
8850: 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 8)).#define SQLI
8860: 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 TE_IOERR_SHORT_R
8870: 45 41 44 20 20 20 20 20 20 20 20 28 53 51 4c 49 EAD (SQLI
8880: 54 45 5f 49 4f 45 52 52 20 7c 20 28 32 3c 3c 38 TE_IOERR | (2<<8
8890: 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 )).#define SQLIT
88a0: 45 5f 49 4f 45 52 52 5f 57 52 49 54 45 20 20 20 E_IOERR_WRITE
88b0: 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 (SQLIT
88c0: 45 5f 49 4f 45 52 52 20 7c 20 28 33 3c 3c 38 29 E_IOERR | (3<<8)
88d0: 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 ).#define SQLITE
88e0: 5f 49 4f 45 52 52 5f 46 53 59 4e 43 20 20 20 20 _IOERR_FSYNC
88f0: 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 (SQLITE
8900: 5f 49 4f 45 52 52 20 7c 20 28 34 3c 3c 38 29 29 _IOERR | (4<<8))
8910: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
8920: 49 4f 45 52 52 5f 44 49 52 5f 46 53 59 4e 43 20 IOERR_DIR_FSYNC
8930: 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f (SQLITE_
8940: 49 4f 45 52 52 20 7c 20 28 35 3c 3c 38 29 29 0a IOERR | (5<<8)).
8950: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 #define SQLITE_I
8960: 4f 45 52 52 5f 54 52 55 4e 43 41 54 45 20 20 20 OERR_TRUNCATE
8970: 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 (SQLITE_I
8980: 4f 45 52 52 20 7c 20 28 36 3c 3c 38 29 29 0a 23 OERR | (6<<8)).#
8990: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f define SQLITE_IO
89a0: 45 52 52 5f 46 53 54 41 54 20 20 20 20 20 20 20 ERR_FSTAT
89b0: 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f (SQLITE_IO
89c0: 45 52 52 20 7c 20 28 37 3c 3c 38 29 29 0a 23 64 ERR | (7<<8)).#d
89d0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 efine SQLITE_IOE
89e0: 52 52 5f 55 4e 4c 4f 43 4b 20 20 20 20 20 20 20 RR_UNLOCK
89f0: 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 (SQLITE_IOE
8a00: 52 52 20 7c 20 28 38 3c 3c 38 29 29 0a 23 64 65 RR | (8<<8)).#de
8a10: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 fine SQLITE_IOER
8a20: 52 5f 52 44 4c 4f 43 4b 20 20 20 20 20 20 20 20 R_RDLOCK
8a30: 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 (SQLITE_IOER
8a40: 52 20 7c 20 28 39 3c 3c 38 29 29 0a 23 64 65 66 R | (9<<8)).#def
8a50: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 ine SQLITE_IOERR
8a60: 5f 44 45 4c 45 54 45 20 20 20 20 20 20 20 20 20 _DELETE
8a70: 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 (SQLITE_IOERR
8a80: 20 7c 20 28 31 30 3c 3c 38 29 29 0a 23 64 65 66 | (10<<8)).#def
8a90: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 ine SQLITE_IOERR
8aa0: 5f 42 4c 4f 43 4b 45 44 20 20 20 20 20 20 20 20 _BLOCKED
8ab0: 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 (SQLITE_IOERR
8ac0: 20 7c 20 28 31 31 3c 3c 38 29 29 0a 23 64 65 66 | (11<<8)).#def
8ad0: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 ine SQLITE_IOERR
8ae0: 5f 4e 4f 4d 45 4d 20 20 20 20 20 20 20 20 20 20 _NOMEM
8af0: 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 (SQLITE_IOERR
8b00: 20 7c 20 28 31 32 3c 3c 38 29 29 0a 23 64 65 66 | (12<<8)).#def
8b10: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 ine SQLITE_IOERR
8b20: 5f 41 43 43 45 53 53 20 20 20 20 20 20 20 20 20 _ACCESS
8b30: 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 (SQLITE_IOERR
8b40: 20 7c 20 28 31 33 3c 3c 38 29 29 0a 23 64 65 66 | (13<<8)).#def
8b50: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 ine SQLITE_IOERR
8b60: 5f 43 48 45 43 4b 52 45 53 45 52 56 45 44 4c 4f _CHECKRESERVEDLO
8b70: 43 4b 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 CK (SQLITE_IOERR
8b80: 20 7c 20 28 31 34 3c 3c 38 29 29 0a 23 64 65 66 | (14<<8)).#def
8b90: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 ine SQLITE_IOERR
8ba0: 5f 4c 4f 43 4b 20 20 20 20 20 20 20 20 20 20 20 _LOCK
8bb0: 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 (SQLITE_IOERR
8bc0: 20 7c 20 28 31 35 3c 3c 38 29 29 0a 23 64 65 66 | (15<<8)).#def
8bd0: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 ine SQLITE_IOERR
8be0: 5f 43 4c 4f 53 45 20 20 20 20 20 20 20 20 20 20 _CLOSE
8bf0: 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 (SQLITE_IOERR
8c00: 20 7c 20 28 31 36 3c 3c 38 29 29 0a 23 64 65 66 | (16<<8)).#def
8c10: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 ine SQLITE_IOERR
8c20: 5f 44 49 52 5f 43 4c 4f 53 45 20 20 20 20 20 20 _DIR_CLOSE
8c30: 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 (SQLITE_IOERR
8c40: 20 7c 20 28 31 37 3c 3c 38 29 29 0a 23 64 65 66 | (17<<8)).#def
8c50: 69 6e 65 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 ine SQLITE_LOCKE
8c60: 44 5f 53 48 41 52 45 44 43 41 43 48 45 20 20 20 D_SHAREDCACHE
8c70: 20 20 20 28 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 (SQLITE_LOCKE
8c80: 44 20 7c 20 28 31 3c 3c 38 29 20 29 0a 0a 2f 2a D | (1<<8) )../*
8c90: 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 46 6c .** CAPI3REF: Fl
8ca0: 61 67 73 20 46 6f 72 20 46 69 6c 65 20 4f 70 65 ags For File Ope
8cb0: 6e 20 4f 70 65 72 61 74 69 6f 6e 73 20 7b 48 31 n Operations {H1
8cc0: 30 32 33 30 7d 20 3c 48 31 31 31 32 30 3e 20 3c 0230} <H11120> <
8cd0: 48 31 32 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 H12700>.**.** Th
8ce0: 65 73 65 20 62 69 74 20 76 61 6c 75 65 73 20 61 ese bit values a
8cf0: 72 65 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 re intended for
8d00: 75 73 65 20 69 6e 20 74 68 65 0a 2a 2a 20 33 72 use in the.** 3r
8d10: 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 d parameter to t
8d20: 68 65 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e he [sqlite3_open
8d30: 5f 76 32 28 29 5d 20 69 6e 74 65 72 66 61 63 65 _v2()] interface
8d40: 20 61 6e 64 0a 2a 2a 20 69 6e 20 74 68 65 20 34 and.** in the 4
8d50: 74 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 th parameter to
8d60: 74 68 65 20 78 4f 70 65 6e 20 6d 65 74 68 6f 64 the xOpen method
8d70: 20 6f 66 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 of the.** [sqli
8d80: 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63 74 2e te3_vfs] object.
8d90: 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 .*/.#define SQLI
8da0: 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 TE_OPEN_READONLY
8db0: 20 20 20 20 20 20 20 20 20 30 78 30 30 30 30 30 0x00000
8dc0: 30 30 31 20 20 2f 2a 20 4f 6b 20 66 6f 72 20 73 001 /* Ok for s
8dd0: 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 qlite3_open_v2()
8de0: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
8df0: 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 TE_OPEN_READWRIT
8e00: 45 20 20 20 20 20 20 20 20 30 78 30 30 30 30 30 E 0x00000
8e10: 30 30 32 20 20 2f 2a 20 4f 6b 20 66 6f 72 20 73 002 /* Ok for s
8e20: 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 qlite3_open_v2()
8e30: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
8e40: 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 20 20 TE_OPEN_CREATE
8e50: 20 20 20 20 20 20 20 20 20 30 78 30 30 30 30 30 0x00000
8e60: 30 30 34 20 20 2f 2a 20 4f 6b 20 66 6f 72 20 73 004 /* Ok for s
8e70: 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 qlite3_open_v2()
8e80: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
8e90: 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e TE_OPEN_DELETEON
8ea0: 43 4c 4f 53 45 20 20 20 20 30 78 30 30 30 30 30 CLOSE 0x00000
8eb0: 30 30 38 20 20 2f 2a 20 56 46 53 20 6f 6e 6c 79 008 /* VFS only
8ec0: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
8ed0: 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 TE_OPEN_EXCLUSIV
8ee0: 45 20 20 20 20 20 20 20 20 30 78 30 30 30 30 30 E 0x00000
8ef0: 30 31 30 20 20 2f 2a 20 56 46 53 20 6f 6e 6c 79 010 /* VFS only
8f00: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
8f10: 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 20 TE_OPEN_MAIN_DB
8f20: 20 20 20 20 20 20 20 20 20 30 78 30 30 30 30 30 0x00000
8f30: 31 30 30 20 20 2f 2a 20 56 46 53 20 6f 6e 6c 79 100 /* VFS only
8f40: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
8f50: 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 44 42 20 TE_OPEN_TEMP_DB
8f60: 20 20 20 20 20 20 20 20 20 30 78 30 30 30 30 30 0x00000
8f70: 32 30 30 20 20 2f 2a 20 56 46 53 20 6f 6e 6c 79 200 /* VFS only
8f80: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
8f90: 54 45 5f 4f 50 45 4e 5f 54 52 41 4e 53 49 45 4e TE_OPEN_TRANSIEN
8fa0: 54 5f 44 42 20 20 20 20 20 30 78 30 30 30 30 30 T_DB 0x00000
8fb0: 34 30 30 20 20 2f 2a 20 56 46 53 20 6f 6e 6c 79 400 /* VFS only
8fc0: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
8fd0: 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 TE_OPEN_MAIN_JOU
8fe0: 52 4e 41 4c 20 20 20 20 20 30 78 30 30 30 30 30 RNAL 0x00000
8ff0: 38 30 30 20 20 2f 2a 20 56 46 53 20 6f 6e 6c 79 800 /* VFS only
9000: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
9010: 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 4a 4f 55 TE_OPEN_TEMP_JOU
9020: 52 4e 41 4c 20 20 20 20 20 30 78 30 30 30 30 31 RNAL 0x00001
9030: 30 30 30 20 20 2f 2a 20 56 46 53 20 6f 6e 6c 79 000 /* VFS only
9040: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
9050: 54 45 5f 4f 50 45 4e 5f 53 55 42 4a 4f 55 52 4e TE_OPEN_SUBJOURN
9060: 41 4c 20 20 20 20 20 20 20 30 78 30 30 30 30 32 AL 0x00002
9070: 30 30 30 20 20 2f 2a 20 56 46 53 20 6f 6e 6c 79 000 /* VFS only
9080: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
9090: 54 45 5f 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a TE_OPEN_MASTER_J
90a0: 4f 55 52 4e 41 4c 20 20 20 30 78 30 30 30 30 34 OURNAL 0x00004
90b0: 30 30 30 20 20 2f 2a 20 56 46 53 20 6f 6e 6c 79 000 /* VFS only
90c0: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
90d0: 54 45 5f 4f 50 45 4e 5f 4e 4f 4d 55 54 45 58 20 TE_OPEN_NOMUTEX
90e0: 20 20 20 20 20 20 20 20 20 30 78 30 30 30 30 38 0x00008
90f0: 30 30 30 20 20 2f 2a 20 4f 6b 20 66 6f 72 20 73 000 /* Ok for s
9100: 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 qlite3_open_v2()
9110: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
9120: 54 45 5f 4f 50 45 4e 5f 46 55 4c 4c 4d 55 54 45 TE_OPEN_FULLMUTE
9130: 58 20 20 20 20 20 20 20 20 30 78 30 30 30 31 30 X 0x00010
9140: 30 30 30 20 20 2f 2a 20 4f 6b 20 66 6f 72 20 73 000 /* Ok for s
9150: 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 qlite3_open_v2()
9160: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
9170: 54 45 5f 4f 50 45 4e 5f 53 48 41 52 45 44 43 41 TE_OPEN_SHAREDCA
9180: 43 48 45 20 20 20 20 20 20 30 78 30 30 30 32 30 CHE 0x00020
9190: 30 30 30 20 20 2f 2a 20 4f 6b 20 66 6f 72 20 73 000 /* Ok for s
91a0: 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 qlite3_open_v2()
91b0: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
91c0: 54 45 5f 4f 50 45 4e 5f 50 52 49 56 41 54 45 43 TE_OPEN_PRIVATEC
91d0: 41 43 48 45 20 20 20 20 20 30 78 30 30 30 34 30 ACHE 0x00040
91e0: 30 30 30 20 20 2f 2a 20 4f 6b 20 66 6f 72 20 73 000 /* Ok for s
91f0: 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 qlite3_open_v2()
9200: 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 */../*.** CAPI3
9210: 52 45 46 3a 20 44 65 76 69 63 65 20 43 68 61 72 REF: Device Char
9220: 61 63 74 65 72 69 73 74 69 63 73 20 7b 48 31 30 acteristics {H10
9230: 32 34 30 7d 20 3c 48 31 31 31 32 30 3e 0a 2a 2a 240} <H11120>.**
9240: 0a 2a 2a 20 54 68 65 20 78 44 65 76 69 63 65 43 .** The xDeviceC
9250: 61 70 61 62 69 6c 69 74 69 65 73 20 6d 65 74 68 apabilities meth
9260: 6f 64 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 od of the [sqlit
9270: 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d 0a 2a e3_io_methods].*
9280: 2a 20 6f 62 6a 65 63 74 20 72 65 74 75 72 6e 73 * object returns
9290: 20 61 6e 20 69 6e 74 65 67 65 72 20 77 68 69 63 an integer whic
92a0: 68 20 69 73 20 61 20 76 65 63 74 6f 72 20 6f 66 h is a vector of
92b0: 20 74 68 65 20 74 68 65 73 65 0a 2a 2a 20 62 69 the these.** bi
92c0: 74 20 76 61 6c 75 65 73 20 65 78 70 72 65 73 73 t values express
92d0: 69 6e 67 20 49 2f 4f 20 63 68 61 72 61 63 74 65 ing I/O characte
92e0: 72 69 73 74 69 63 73 20 6f 66 20 74 68 65 20 6d ristics of the m
92f0: 61 73 73 20 73 74 6f 72 61 67 65 0a 2a 2a 20 64 ass storage.** d
9300: 65 76 69 63 65 20 74 68 61 74 20 68 6f 6c 64 73 evice that holds
9310: 20 74 68 65 20 66 69 6c 65 20 74 68 61 74 20 74 the file that t
9320: 68 65 20 5b 73 71 6c 69 74 65 33 5f 69 6f 5f 6d he [sqlite3_io_m
9330: 65 74 68 6f 64 73 5d 0a 2a 2a 20 72 65 66 65 72 ethods].** refer
9340: 73 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 s to..**.** The
9350: 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f SQLITE_IOCAP_ATO
9360: 4d 49 43 20 70 72 6f 70 65 72 74 79 20 6d 65 61 MIC property mea
9370: 6e 73 20 74 68 61 74 20 61 6c 6c 20 77 72 69 74 ns that all writ
9380: 65 73 20 6f 66 0a 2a 2a 20 61 6e 79 20 73 69 7a es of.** any siz
9390: 65 20 61 72 65 20 61 74 6f 6d 69 63 2e 20 20 54 e are atomic. T
93a0: 68 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f he SQLITE_IOCAP_
93b0: 41 54 4f 4d 49 43 6e 6e 6e 20 76 61 6c 75 65 73 ATOMICnnn values
93c0: 0a 2a 2a 20 6d 65 61 6e 20 74 68 61 74 20 77 72 .** mean that wr
93d0: 69 74 65 73 20 6f 66 20 62 6c 6f 63 6b 73 20 74 ites of blocks t
93e0: 68 61 74 20 61 72 65 20 6e 6e 6e 20 62 79 74 65 hat are nnn byte
93f0: 73 20 69 6e 20 73 69 7a 65 20 61 6e 64 0a 2a 2a s in size and.**
9400: 20 61 72 65 20 61 6c 69 67 6e 65 64 20 74 6f 20 are aligned to
9410: 61 6e 20 61 64 64 72 65 73 73 20 77 68 69 63 68 an address which
9420: 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 6d is an integer m
9430: 75 6c 74 69 70 6c 65 20 6f 66 0a 2a 2a 20 6e 6e ultiple of.** nn
9440: 6e 20 61 72 65 20 61 74 6f 6d 69 63 2e 20 20 54 n are atomic. T
9450: 68 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f he SQLITE_IOCAP_
9460: 53 41 46 45 5f 41 50 50 45 4e 44 20 76 61 6c 75 SAFE_APPEND valu
9470: 65 20 6d 65 61 6e 73 0a 2a 2a 20 74 68 61 74 20 e means.** that
9480: 77 68 65 6e 20 64 61 74 61 20 69 73 20 61 70 70 when data is app
9490: 65 6e 64 65 64 20 74 6f 20 61 20 66 69 6c 65 2c ended to a file,
94a0: 20 74 68 65 20 64 61 74 61 20 69 73 20 61 70 70 the data is app
94b0: 65 6e 64 65 64 0a 2a 2a 20 66 69 72 73 74 20 74 ended.** first t
94c0: 68 65 6e 20 74 68 65 20 73 69 7a 65 20 6f 66 20 hen the size of
94d0: 74 68 65 20 66 69 6c 65 20 69 73 20 65 78 74 65 the file is exte
94e0: 6e 64 65 64 2c 20 6e 65 76 65 72 20 74 68 65 20 nded, never the
94f0: 6f 74 68 65 72 0a 2a 2a 20 77 61 79 20 61 72 6f other.** way aro
9500: 75 6e 64 2e 20 20 54 68 65 20 53 51 4c 49 54 45 und. The SQLITE
9510: 5f 49 4f 43 41 50 5f 53 45 51 55 45 4e 54 49 41 _IOCAP_SEQUENTIA
9520: 4c 20 70 72 6f 70 65 72 74 79 20 6d 65 61 6e 73 L property means
9530: 20 74 68 61 74 0a 2a 2a 20 69 6e 66 6f 72 6d 61 that.** informa
9540: 74 69 6f 6e 20 69 73 20 77 72 69 74 74 65 6e 20 tion is written
9550: 74 6f 20 64 69 73 6b 20 69 6e 20 74 68 65 20 73 to disk in the s
9560: 61 6d 65 20 6f 72 64 65 72 20 61 73 20 63 61 6c ame order as cal
9570: 6c 73 0a 2a 2a 20 74 6f 20 78 57 72 69 74 65 28 ls.** to xWrite(
9580: 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 )..*/.#define SQ
9590: 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 LITE_IOCAP_ATOMI
95a0: 43 20 20 20 20 20 20 20 20 20 20 30 78 30 30 30 C 0x000
95b0: 30 30 30 30 31 0a 23 64 65 66 69 6e 65 20 53 51 00001.#define SQ
95c0: 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 LITE_IOCAP_ATOMI
95d0: 43 35 31 32 20 20 20 20 20 20 20 30 78 30 30 30 C512 0x000
95e0: 30 30 30 30 32 0a 23 64 65 66 69 6e 65 20 53 51 00002.#define SQ
95f0: 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 LITE_IOCAP_ATOMI
9600: 43 31 4b 20 20 20 20 20 20 20 20 30 78 30 30 30 C1K 0x000
9610: 30 30 30 30 34 0a 23 64 65 66 69 6e 65 20 53 51 00004.#define SQ
9620: 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 LITE_IOCAP_ATOMI
9630: 43 32 4b 20 20 20 20 20 20 20 20 30 78 30 30 30 C2K 0x000
9640: 30 30 30 30 38 0a 23 64 65 66 69 6e 65 20 53 51 00008.#define SQ
9650: 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 LITE_IOCAP_ATOMI
9660: 43 34 4b 20 20 20 20 20 20 20 20 30 78 30 30 30 C4K 0x000
9670: 30 30 30 31 30 0a 23 64 65 66 69 6e 65 20 53 51 00010.#define SQ
9680: 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 LITE_IOCAP_ATOMI
9690: 43 38 4b 20 20 20 20 20 20 20 20 30 78 30 30 30 C8K 0x000
96a0: 30 30 30 32 30 0a 23 64 65 66 69 6e 65 20 53 51 00020.#define SQ
96b0: 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 LITE_IOCAP_ATOMI
96c0: 43 31 36 4b 20 20 20 20 20 20 20 30 78 30 30 30 C16K 0x000
96d0: 30 30 30 34 30 0a 23 64 65 66 69 6e 65 20 53 51 00040.#define SQ
96e0: 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 LITE_IOCAP_ATOMI
96f0: 43 33 32 4b 20 20 20 20 20 20 20 30 78 30 30 30 C32K 0x000
9700: 30 30 30 38 30 0a 23 64 65 66 69 6e 65 20 53 51 00080.#define SQ
9710: 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 LITE_IOCAP_ATOMI
9720: 43 36 34 4b 20 20 20 20 20 20 20 30 78 30 30 30 C64K 0x000
9730: 30 30 31 30 30 0a 23 64 65 66 69 6e 65 20 53 51 00100.#define SQ
9740: 4c 49 54 45 5f 49 4f 43 41 50 5f 53 41 46 45 5f LITE_IOCAP_SAFE_
9750: 41 50 50 45 4e 44 20 20 20 20 20 30 78 30 30 30 APPEND 0x000
9760: 30 30 32 30 30 0a 23 64 65 66 69 6e 65 20 53 51 00200.#define SQ
9770: 4c 49 54 45 5f 49 4f 43 41 50 5f 53 45 51 55 45 LITE_IOCAP_SEQUE
9780: 4e 54 49 41 4c 20 20 20 20 20 20 30 78 30 30 30 NTIAL 0x000
9790: 30 30 34 30 30 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 00400../*.** CAP
97a0: 49 33 52 45 46 3a 20 46 69 6c 65 20 4c 6f 63 6b I3REF: File Lock
97b0: 69 6e 67 20 4c 65 76 65 6c 73 20 7b 48 31 30 32 ing Levels {H102
97c0: 35 30 7d 20 3c 48 31 31 31 32 30 3e 20 3c 48 31 50} <H11120> <H1
97d0: 31 33 31 30 3e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 1310>.**.** SQLi
97e0: 74 65 20 75 73 65 73 20 6f 6e 65 20 6f 66 20 74 te uses one of t
97f0: 68 65 73 65 20 69 6e 74 65 67 65 72 20 76 61 6c hese integer val
9800: 75 65 73 20 61 73 20 74 68 65 20 73 65 63 6f 6e ues as the secon
9810: 64 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 74 6f d.** argument to
9820: 20 63 61 6c 6c 73 20 69 74 20 6d 61 6b 65 73 20 calls it makes
9830: 74 6f 20 74 68 65 20 78 4c 6f 63 6b 28 29 20 61 to the xLock() a
9840: 6e 64 20 78 55 6e 6c 6f 63 6b 28 29 20 6d 65 74 nd xUnlock() met
9850: 68 6f 64 73 0a 2a 2a 20 6f 66 20 61 6e 20 5b 73 hods.** of an [s
9860: 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 qlite3_io_method
9870: 73 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 23 64 s] object..*/.#d
9880: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 4f 43 efine SQLITE_LOC
9890: 4b 5f 4e 4f 4e 45 20 20 20 20 20 20 20 20 20 20 K_NONE
98a0: 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 0.#define SQLITE
98b0: 5f 4c 4f 43 4b 5f 53 48 41 52 45 44 20 20 20 20 _LOCK_SHARED
98c0: 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 53 51 1.#define SQ
98d0: 4c 49 54 45 5f 4c 4f 43 4b 5f 52 45 53 45 52 56 LITE_LOCK_RESERV
98e0: 45 44 20 20 20 20 20 20 32 0a 23 64 65 66 69 6e ED 2.#defin
98f0: 65 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 50 45 e SQLITE_LOCK_PE
9900: 4e 44 49 4e 47 20 20 20 20 20 20 20 33 0a 23 64 NDING 3.#d
9910: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 4f 43 efine SQLITE_LOC
9920: 4b 5f 45 58 43 4c 55 53 49 56 45 20 20 20 20 20 K_EXCLUSIVE
9930: 34 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 4../*.** CAPI3RE
9940: 46 3a 20 53 79 6e 63 68 72 6f 6e 69 7a 61 74 69 F: Synchronizati
9950: 6f 6e 20 54 79 70 65 20 46 6c 61 67 73 20 7b 48 on Type Flags {H
9960: 31 30 32 36 30 7d 20 3c 48 31 31 31 32 30 3e 0a 10260} <H11120>.
9970: 2a 2a 0a 2a 2a 20 57 68 65 6e 20 53 51 4c 69 74 **.** When SQLit
9980: 65 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 78 53 e invokes the xS
9990: 79 6e 63 28 29 20 6d 65 74 68 6f 64 20 6f 66 20 ync() method of
99a0: 61 6e 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 69 an.** [sqlite3_i
99b0: 6f 5f 6d 65 74 68 6f 64 73 5d 20 6f 62 6a 65 63 o_methods] objec
99c0: 74 20 69 74 20 75 73 65 73 20 61 20 63 6f 6d 62 t it uses a comb
99d0: 69 6e 61 74 69 6f 6e 20 6f 66 0a 2a 2a 20 74 68 ination of.** th
99e0: 65 73 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75 ese integer valu
99f0: 65 73 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 es as the second
9a00: 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a argument..**.**
9a10: 20 57 68 65 6e 20 74 68 65 20 53 51 4c 49 54 45 When the SQLITE
9a20: 5f 53 59 4e 43 5f 44 41 54 41 4f 4e 4c 59 20 66 _SYNC_DATAONLY f
9a30: 6c 61 67 20 69 73 20 75 73 65 64 2c 20 69 74 20 lag is used, it
9a40: 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 0a 2a means that the.*
9a50: 2a 20 73 79 6e 63 20 6f 70 65 72 61 74 69 6f 6e * sync operation
9a60: 20 6f 6e 6c 79 20 6e 65 65 64 73 20 74 6f 20 66 only needs to f
9a70: 6c 75 73 68 20 64 61 74 61 20 74 6f 20 6d 61 73 lush data to mas
9a80: 73 20 73 74 6f 72 61 67 65 2e 20 20 49 6e 6f 64 s storage. Inod
9a90: 65 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e e.** information
9aa0: 20 6e 65 65 64 20 6e 6f 74 20 62 65 20 66 6c 75 need not be flu
9ab0: 73 68 65 64 2e 20 49 66 20 74 68 65 20 6c 6f 77 shed. If the low
9ac0: 65 72 20 66 6f 75 72 20 62 69 74 73 20 6f 66 20 er four bits of
9ad0: 74 68 65 20 66 6c 61 67 0a 2a 2a 20 65 71 75 61 the flag.** equa
9ae0: 6c 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f l SQLITE_SYNC_NO
9af0: 52 4d 41 4c 2c 20 74 68 61 74 20 6d 65 61 6e 73 RMAL, that means
9b00: 20 74 6f 20 75 73 65 20 6e 6f 72 6d 61 6c 20 66 to use normal f
9b10: 73 79 6e 63 28 29 20 73 65 6d 61 6e 74 69 63 73 sync() semantics
9b20: 2e 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f 77 65 ..** If the lowe
9b30: 72 20 66 6f 75 72 20 62 69 74 73 20 65 71 75 61 r four bits equa
9b40: 6c 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55 l SQLITE_SYNC_FU
9b50: 4c 4c 2c 20 74 68 61 74 20 6d 65 61 6e 73 0a 2a LL, that means.*
9b60: 2a 20 74 6f 20 75 73 65 20 4d 61 63 20 4f 53 20 * to use Mac OS
9b70: 58 20 73 74 79 6c 65 20 66 75 6c 6c 73 79 6e 63 X style fullsync
9b80: 20 69 6e 73 74 65 61 64 20 6f 66 20 66 73 79 6e instead of fsyn
9b90: 63 28 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 c()..*/.#define
9ba0: 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d SQLITE_SYNC_NORM
9bb0: 41 4c 20 20 20 20 20 20 20 20 30 78 30 30 30 30 AL 0x0000
9bc0: 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 2.#define SQLITE
9bd0: 5f 53 59 4e 43 5f 46 55 4c 4c 20 20 20 20 20 20 _SYNC_FULL
9be0: 20 20 20 20 30 78 30 30 30 30 33 0a 23 64 65 66 0x00003.#def
9bf0: 69 6e 65 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f ine SQLITE_SYNC_
9c00: 44 41 54 41 4f 4e 4c 59 20 20 20 20 20 20 30 78 DATAONLY 0x
9c10: 30 30 30 31 30 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 00010../*.** CAP
9c20: 49 33 52 45 46 3a 20 4f 53 20 49 6e 74 65 72 66 I3REF: OS Interf
9c30: 61 63 65 20 4f 70 65 6e 20 46 69 6c 65 20 48 61 ace Open File Ha
9c40: 6e 64 6c 65 20 7b 48 31 31 31 31 30 7d 20 3c 53 ndle {H11110} <S
9c50: 32 30 31 31 30 3e 0a 2a 2a 0a 2a 2a 20 41 6e 20 20110>.**.** An
9c60: 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 5d 20 6f [sqlite3_file] o
9c70: 62 6a 65 63 74 20 72 65 70 72 65 73 65 6e 74 73 bject represents
9c80: 20 61 6e 20 6f 70 65 6e 20 66 69 6c 65 20 69 6e an open file in
9c90: 20 74 68 65 20 0a 2a 2a 20 5b 73 71 6c 69 74 65 the .** [sqlite
9ca0: 33 5f 76 66 73 20 7c 20 4f 53 20 69 6e 74 65 72 3_vfs | OS inter
9cb0: 66 61 63 65 20 6c 61 79 65 72 5d 2e 20 20 49 6e face layer]. In
9cc0: 64 69 76 69 64 75 61 6c 20 4f 53 20 69 6e 74 65 dividual OS inte
9cd0: 72 66 61 63 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 rface.** impleme
9ce0: 6e 74 61 74 69 6f 6e 73 20 77 69 6c 6c 0a 2a 2a ntations will.**
9cf0: 20 77 61 6e 74 20 74 6f 20 73 75 62 63 6c 61 73 want to subclas
9d00: 73 20 74 68 69 73 20 6f 62 6a 65 63 74 20 62 79 s this object by
9d10: 20 61 70 70 65 6e 64 69 6e 67 20 61 64 64 69 74 appending addit
9d20: 69 6f 6e 61 6c 20 66 69 65 6c 64 73 0a 2a 2a 20 ional fields.**
9d30: 66 6f 72 20 74 68 65 69 72 20 6f 77 6e 20 75 73 for their own us
9d40: 65 2e 20 20 54 68 65 20 70 4d 65 74 68 6f 64 73 e. The pMethods
9d50: 20 65 6e 74 72 79 20 69 73 20 61 20 70 6f 69 6e entry is a poin
9d60: 74 65 72 20 74 6f 20 61 6e 0a 2a 2a 20 5b 73 71 ter to an.** [sq
9d70: 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 lite3_io_methods
9d80: 5d 20 6f 62 6a 65 63 74 20 74 68 61 74 20 64 65 ] object that de
9d90: 66 69 6e 65 73 20 6d 65 74 68 6f 64 73 20 66 6f fines methods fo
9da0: 72 20 70 65 72 66 6f 72 6d 69 6e 67 0a 2a 2a 20 r performing.**
9db0: 49 2f 4f 20 6f 70 65 72 61 74 69 6f 6e 73 20 6f I/O operations o
9dc0: 6e 20 74 68 65 20 6f 70 65 6e 20 66 69 6c 65 2e n the open file.
9dd0: 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 .*/.typedef stru
9de0: 63 74 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 ct sqlite3_file
9df0: 73 71 6c 69 74 65 33 5f 66 69 6c 65 3b 0a 73 74 sqlite3_file;.st
9e00: 72 75 63 74 20 73 71 6c 69 74 65 33 5f 66 69 6c ruct sqlite3_fil
9e10: 65 20 7b 0a 20 20 63 6f 6e 73 74 20 73 74 72 75 e {. const stru
9e20: 63 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 ct sqlite3_io_me
9e30: 74 68 6f 64 73 20 2a 70 4d 65 74 68 6f 64 73 3b thods *pMethods;
9e40: 20 20 2f 2a 20 4d 65 74 68 6f 64 73 20 66 6f 72 /* Methods for
9e50: 20 61 6e 20 6f 70 65 6e 20 66 69 6c 65 20 2a 2f an open file */
9e60: 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 .};../*.** CAPI3
9e70: 52 45 46 3a 20 4f 53 20 49 6e 74 65 72 66 61 63 REF: OS Interfac
9e80: 65 20 46 69 6c 65 20 56 69 72 74 75 61 6c 20 4d e File Virtual M
9e90: 65 74 68 6f 64 73 20 4f 62 6a 65 63 74 20 7b 48 ethods Object {H
9ea0: 31 31 31 32 30 7d 20 3c 53 32 30 31 31 30 3e 0a 11120} <S20110>.
9eb0: 2a 2a 0a 2a 2a 20 45 76 65 72 79 20 66 69 6c 65 **.** Every file
9ec0: 20 6f 70 65 6e 65 64 20 62 79 20 74 68 65 20 5b opened by the [
9ed0: 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 78 4f 70 sqlite3_vfs] xOp
9ee0: 65 6e 20 6d 65 74 68 6f 64 20 70 6f 70 75 6c 61 en method popula
9ef0: 74 65 73 20 61 6e 0a 2a 2a 20 5b 73 71 6c 69 74 tes an.** [sqlit
9f00: 65 33 5f 66 69 6c 65 5d 20 6f 62 6a 65 63 74 20 e3_file] object
9f10: 28 6f 72 2c 20 6d 6f 72 65 20 63 6f 6d 6d 6f 6e (or, more common
9f20: 6c 79 2c 20 61 20 73 75 62 63 6c 61 73 73 20 6f ly, a subclass o
9f30: 66 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 f the.** [sqlite
9f40: 33 5f 66 69 6c 65 5d 20 6f 62 6a 65 63 74 29 20 3_file] object)
9f50: 77 69 74 68 20 61 20 70 6f 69 6e 74 65 72 20 74 with a pointer t
9f60: 6f 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 o an instance of
9f70: 20 74 68 69 73 20 6f 62 6a 65 63 74 2e 0a 2a 2a this object..**
9f80: 20 54 68 69 73 20 6f 62 6a 65 63 74 20 64 65 66 This object def
9f90: 69 6e 65 73 20 74 68 65 20 6d 65 74 68 6f 64 73 ines the methods
9fa0: 20 75 73 65 64 20 74 6f 20 70 65 72 66 6f 72 6d used to perform
9fb0: 20 76 61 72 69 6f 75 73 20 6f 70 65 72 61 74 69 various operati
9fc0: 6f 6e 73 0a 2a 2a 20 61 67 61 69 6e 73 74 20 74 ons.** against t
9fd0: 68 65 20 6f 70 65 6e 20 66 69 6c 65 20 72 65 70 he open file rep
9fe0: 72 65 73 65 6e 74 65 64 20 62 79 20 74 68 65 20 resented by the
9ff0: 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 5d 20 6f [sqlite3_file] o
a000: 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 bject..**.** If
a010: 74 68 65 20 78 4f 70 65 6e 20 6d 65 74 68 6f 64 the xOpen method
a020: 20 73 65 74 73 20 74 68 65 20 73 71 6c 69 74 65 sets the sqlite
a030: 33 5f 66 69 6c 65 2e 70 4d 65 74 68 6f 64 73 20 3_file.pMethods
a040: 65 6c 65 6d 65 6e 74 20 0a 2a 2a 20 74 6f 20 61 element .** to a
a050: 20 6e 6f 6e 2d 4e 55 4c 4c 20 70 6f 69 6e 74 65 non-NULL pointe
a060: 72 2c 20 74 68 65 6e 20 74 68 65 20 73 71 6c 69 r, then the sqli
a070: 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 2e 78 te3_io_methods.x
a080: 43 6c 6f 73 65 20 6d 65 74 68 6f 64 0a 2a 2a 20 Close method.**
a090: 6d 61 79 20 62 65 20 69 6e 76 6f 6b 65 64 20 65 may be invoked e
a0a0: 76 65 6e 20 69 66 20 74 68 65 20 78 4f 70 65 6e ven if the xOpen
a0b0: 20 72 65 70 6f 72 74 65 64 20 74 68 61 74 20 69 reported that i
a0c0: 74 20 66 61 69 6c 65 64 2e 20 20 54 68 65 0a 2a t failed. The.*
a0d0: 2a 20 6f 6e 6c 79 20 77 61 79 20 74 6f 20 70 72 * only way to pr
a0e0: 65 76 65 6e 74 20 61 20 63 61 6c 6c 20 74 6f 20 event a call to
a0f0: 78 43 6c 6f 73 65 20 66 6f 6c 6c 6f 77 69 6e 67 xClose following
a100: 20 61 20 66 61 69 6c 65 64 20 78 4f 70 65 6e 0a a failed xOpen.
a110: 2a 2a 20 69 73 20 66 6f 72 20 74 68 65 20 78 4f ** is for the xO
a120: 70 65 6e 20 74 6f 20 73 65 74 20 74 68 65 20 73 pen to set the s
a130: 71 6c 69 74 65 33 5f 66 69 6c 65 2e 70 4d 65 74 qlite3_file.pMet
a140: 68 6f 64 73 20 65 6c 65 6d 65 6e 74 20 74 6f 20 hods element to
a150: 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 NULL..**.** The
a160: 66 6c 61 67 73 20 61 72 67 75 6d 65 6e 74 20 74 flags argument t
a170: 6f 20 78 53 79 6e 63 20 6d 61 79 20 62 65 20 6f o xSync may be o
a180: 6e 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 53 59 ne of [SQLITE_SY
a190: 4e 43 5f 4e 4f 52 4d 41 4c 5d 20 6f 72 0a 2a 2a NC_NORMAL] or.**
a1a0: 20 5b 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55 [SQLITE_SYNC_FU
a1b0: 4c 4c 5d 2e 20 20 54 68 65 20 66 69 72 73 74 20 LL]. The first
a1c0: 63 68 6f 69 63 65 20 69 73 20 74 68 65 20 6e 6f choice is the no
a1d0: 72 6d 61 6c 20 66 73 79 6e 63 28 29 2e 0a 2a 2a rmal fsync()..**
a1e0: 20 54 68 65 20 73 65 63 6f 6e 64 20 63 68 6f 69 The second choi
a1f0: 63 65 20 69 73 20 61 20 4d 61 63 20 4f 53 20 58 ce is a Mac OS X
a200: 20 73 74 79 6c 65 20 66 75 6c 6c 73 79 6e 63 2e style fullsync.
a210: 20 20 54 68 65 20 5b 53 51 4c 49 54 45 5f 53 59 The [SQLITE_SY
a220: 4e 43 5f 44 41 54 41 4f 4e 4c 59 5d 0a 2a 2a 20 NC_DATAONLY].**
a230: 66 6c 61 67 20 6d 61 79 20 62 65 20 4f 52 65 64 flag may be ORed
a240: 20 69 6e 20 74 6f 20 69 6e 64 69 63 61 74 65 20 in to indicate
a250: 74 68 61 74 20 6f 6e 6c 79 20 74 68 65 20 64 61 that only the da
a260: 74 61 20 6f 66 20 74 68 65 20 66 69 6c 65 0a 2a ta of the file.*
a270: 2a 20 61 6e 64 20 6e 6f 74 20 69 74 73 20 69 6e * and not its in
a280: 6f 64 65 20 6e 65 65 64 73 20 74 6f 20 62 65 20 ode needs to be
a290: 73 79 6e 63 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 synced..**.** Th
a2a0: 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 73 e integer values
a2b0: 20 74 6f 20 78 4c 6f 63 6b 28 29 20 61 6e 64 20 to xLock() and
a2c0: 78 55 6e 6c 6f 63 6b 28 29 20 61 72 65 20 6f 6e xUnlock() are on
a2d0: 65 20 6f 66 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 e of.** <ul>.**
a2e0: 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 <li> [SQLITE_LOC
a2f0: 4b 5f 4e 4f 4e 45 5d 2c 0a 2a 2a 20 3c 6c 69 3e K_NONE],.** <li>
a300: 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 53 48 [SQLITE_LOCK_SH
a310: 41 52 45 44 5d 2c 0a 2a 2a 20 3c 6c 69 3e 20 5b ARED],.** <li> [
a320: 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 52 45 53 45 SQLITE_LOCK_RESE
a330: 52 56 45 44 5d 2c 0a 2a 2a 20 3c 6c 69 3e 20 5b RVED],.** <li> [
a340: 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 50 45 4e 44 SQLITE_LOCK_PEND
a350: 49 4e 47 5d 2c 20 6f 72 0a 2a 2a 20 3c 6c 69 3e ING], or.** <li>
a360: 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 45 58 [SQLITE_LOCK_EX
a370: 43 4c 55 53 49 56 45 5d 2e 0a 2a 2a 20 3c 2f 75 CLUSIVE]..** </u
a380: 6c 3e 0a 2a 2a 20 78 4c 6f 63 6b 28 29 20 69 6e l>.** xLock() in
a390: 63 72 65 61 73 65 73 20 74 68 65 20 6c 6f 63 6b creases the lock
a3a0: 2e 20 78 55 6e 6c 6f 63 6b 28 29 20 64 65 63 72 . xUnlock() decr
a3b0: 65 61 73 65 73 20 74 68 65 20 6c 6f 63 6b 2e 0a eases the lock..
a3c0: 2a 2a 20 54 68 65 20 78 43 68 65 63 6b 52 65 73 ** The xCheckRes
a3d0: 65 72 76 65 64 4c 6f 63 6b 28 29 20 6d 65 74 68 ervedLock() meth
a3e0: 6f 64 20 63 68 65 63 6b 73 20 77 68 65 74 68 65 od checks whethe
a3f0: 72 20 61 6e 79 20 64 61 74 61 62 61 73 65 20 63 r any database c
a400: 6f 6e 6e 65 63 74 69 6f 6e 2c 0a 2a 2a 20 65 69 onnection,.** ei
a410: 74 68 65 72 20 69 6e 20 74 68 69 73 20 70 72 6f ther in this pro
a420: 63 65 73 73 20 6f 72 20 69 6e 20 73 6f 6d 65 20 cess or in some
a430: 6f 74 68 65 72 20 70 72 6f 63 65 73 73 2c 20 69 other process, i
a440: 73 20 68 6f 6c 64 69 6e 67 20 61 20 52 45 53 45 s holding a RESE
a450: 52 56 45 44 2c 0a 2a 2a 20 50 45 4e 44 49 4e 47 RVED,.** PENDING
a460: 2c 20 6f 72 20 45 58 43 4c 55 53 49 56 45 20 6c , or EXCLUSIVE l
a470: 6f 63 6b 20 6f 6e 20 74 68 65 20 66 69 6c 65 2e ock on the file.
a480: 20 20 49 74 20 72 65 74 75 72 6e 73 20 74 72 75 It returns tru
a490: 65 0a 2a 2a 20 69 66 20 73 75 63 68 20 61 20 6c e.** if such a l
a4a0: 6f 63 6b 20 65 78 69 73 74 73 20 61 6e 64 20 66 ock exists and f
a4b0: 61 6c 73 65 20 6f 74 68 65 72 77 69 73 65 2e 0a alse otherwise..
a4c0: 2a 2a 0a 2a 2a 20 54 68 65 20 78 46 69 6c 65 43 **.** The xFileC
a4d0: 6f 6e 74 72 6f 6c 28 29 20 6d 65 74 68 6f 64 20 ontrol() method
a4e0: 69 73 20 61 20 67 65 6e 65 72 69 63 20 69 6e 74 is a generic int
a4f0: 65 72 66 61 63 65 20 74 68 61 74 20 61 6c 6c 6f erface that allo
a500: 77 73 20 63 75 73 74 6f 6d 0a 2a 2a 20 56 46 53 ws custom.** VFS
a510: 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 implementations
a520: 20 74 6f 20 64 69 72 65 63 74 6c 79 20 63 6f 6e to directly con
a530: 74 72 6f 6c 20 61 6e 20 6f 70 65 6e 20 66 69 6c trol an open fil
a540: 65 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 5b e using the.** [
a550: 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e sqlite3_file_con
a560: 74 72 6f 6c 28 29 5d 20 69 6e 74 65 72 66 61 63 trol()] interfac
a570: 65 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 20 22 e. The second "
a580: 6f 70 22 20 61 72 67 75 6d 65 6e 74 20 69 73 20 op" argument is
a590: 61 6e 0a 2a 2a 20 69 6e 74 65 67 65 72 20 6f 70 an.** integer op
a5a0: 63 6f 64 65 2e 20 20 54 68 65 20 74 68 69 72 64 code. The third
a5b0: 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 20 67 argument is a g
a5c0: 65 6e 65 72 69 63 20 70 6f 69 6e 74 65 72 20 69 eneric pointer i
a5d0: 6e 74 65 6e 64 65 64 20 74 6f 0a 2a 2a 20 70 6f ntended to.** po
a5e0: 69 6e 74 20 74 6f 20 61 20 73 74 72 75 63 74 75 int to a structu
a5f0: 72 65 20 74 68 61 74 20 6d 61 79 20 63 6f 6e 74 re that may cont
a600: 61 69 6e 20 61 72 67 75 6d 65 6e 74 73 20 6f 72 ain arguments or
a610: 20 73 70 61 63 65 20 69 6e 20 77 68 69 63 68 20 space in which
a620: 74 6f 0a 2a 2a 20 77 72 69 74 65 20 72 65 74 75 to.** write retu
a630: 72 6e 20 76 61 6c 75 65 73 2e 20 20 50 6f 74 65 rn values. Pote
a640: 6e 74 69 61 6c 20 75 73 65 73 20 66 6f 72 20 78 ntial uses for x
a650: 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 29 20 6d 69 FileControl() mi
a660: 67 68 74 20 62 65 0a 2a 2a 20 66 75 6e 63 74 69 ght be.** functi
a670: 6f 6e 73 20 74 6f 20 65 6e 61 62 6c 65 20 62 6c ons to enable bl
a680: 6f 63 6b 69 6e 67 20 6c 6f 63 6b 73 20 77 69 74 ocking locks wit
a690: 68 20 74 69 6d 65 6f 75 74 73 2c 20 74 6f 20 63 h timeouts, to c
a6a0: 68 61 6e 67 65 20 74 68 65 0a 2a 2a 20 6c 6f 63 hange the.** loc
a6b0: 6b 69 6e 67 20 73 74 72 61 74 65 67 79 20 28 66 king strategy (f
a6c0: 6f 72 20 65 78 61 6d 70 6c 65 20 74 6f 20 75 73 or example to us
a6d0: 65 20 64 6f 74 2d 66 69 6c 65 20 6c 6f 63 6b 73 e dot-file locks
a6e0: 29 2c 20 74 6f 20 69 6e 71 75 69 72 65 0a 2a 2a ), to inquire.**
a6f0: 20 61 62 6f 75 74 20 74 68 65 20 73 74 61 74 75 about the statu
a700: 73 20 6f 66 20 61 20 6c 6f 63 6b 2c 20 6f 72 20 s of a lock, or
a710: 74 6f 20 62 72 65 61 6b 20 73 74 61 6c 65 20 6c to break stale l
a720: 6f 63 6b 73 2e 20 20 54 68 65 20 53 51 4c 69 74 ocks. The SQLit
a730: 65 0a 2a 2a 20 63 6f 72 65 20 72 65 73 65 72 76 e.** core reserv
a740: 65 73 20 61 6c 6c 20 6f 70 63 6f 64 65 73 20 6c es all opcodes l
a750: 65 73 73 20 74 68 61 6e 20 31 30 30 20 66 6f 72 ess than 100 for
a760: 20 69 74 73 20 6f 77 6e 20 75 73 65 2e 0a 2a 2a its own use..**
a770: 20 41 20 5b 53 51 4c 49 54 45 5f 46 43 4e 54 4c A [SQLITE_FCNTL
a780: 5f 4c 4f 43 4b 53 54 41 54 45 20 7c 20 6c 69 73 _LOCKSTATE | lis
a790: 74 20 6f 66 20 6f 70 63 6f 64 65 73 5d 20 6c 65 t of opcodes] le
a7a0: 73 73 20 74 68 61 6e 20 31 30 30 20 69 73 20 61 ss than 100 is a
a7b0: 76 61 69 6c 61 62 6c 65 2e 0a 2a 2a 20 41 70 70 vailable..** App
a7c0: 6c 69 63 61 74 69 6f 6e 73 20 74 68 61 74 20 64 lications that d
a7d0: 65 66 69 6e 65 20 61 20 63 75 73 74 6f 6d 20 78 efine a custom x
a7e0: 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 6d 65 74 68 FileControl meth
a7f0: 6f 64 20 73 68 6f 75 6c 64 20 75 73 65 20 6f 70 od should use op
a800: 63 6f 64 65 73 0a 2a 2a 20 67 72 65 61 74 65 72 codes.** greater
a810: 20 74 68 61 6e 20 31 30 30 20 74 6f 20 61 76 6f than 100 to avo
a820: 69 64 20 63 6f 6e 66 6c 69 63 74 73 2e 0a 2a 2a id conflicts..**
a830: 0a 2a 2a 20 54 68 65 20 78 53 65 63 74 6f 72 53 .** The xSectorS
a840: 69 7a 65 28 29 20 6d 65 74 68 6f 64 20 72 65 74 ize() method ret
a850: 75 72 6e 73 20 74 68 65 20 73 65 63 74 6f 72 20 urns the sector
a860: 73 69 7a 65 20 6f 66 20 74 68 65 0a 2a 2a 20 64 size of the.** d
a870: 65 76 69 63 65 20 74 68 61 74 20 75 6e 64 65 72 evice that under
a880: 6c 69 65 73 20 74 68 65 20 66 69 6c 65 2e 20 20 lies the file.
a890: 54 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20 The sector size
a8a0: 69 73 20 74 68 65 0a 2a 2a 20 6d 69 6e 69 6d 75 is the.** minimu
a8b0: 6d 20 77 72 69 74 65 20 74 68 61 74 20 63 61 6e m write that can
a8c0: 20 62 65 20 70 65 72 66 6f 72 6d 65 64 20 77 69 be performed wi
a8d0: 74 68 6f 75 74 20 64 69 73 74 75 72 62 69 6e 67 thout disturbing
a8e0: 0a 2a 2a 20 6f 74 68 65 72 20 62 79 74 65 73 20 .** other bytes
a8f0: 69 6e 20 74 68 65 20 66 69 6c 65 2e 20 20 54 68 in the file. Th
a900: 65 20 78 44 65 76 69 63 65 43 68 61 72 61 63 74 e xDeviceCharact
a910: 65 72 69 73 74 69 63 73 28 29 0a 2a 2a 20 6d 65 eristics().** me
a920: 74 68 6f 64 20 72 65 74 75 72 6e 73 20 61 20 62 thod returns a b
a930: 69 74 20 76 65 63 74 6f 72 20 64 65 73 63 72 69 it vector descri
a940: 62 69 6e 67 20 62 65 68 61 76 69 6f 72 73 20 6f bing behaviors o
a950: 66 20 74 68 65 0a 2a 2a 20 75 6e 64 65 72 6c 79 f the.** underly
a960: 69 6e 67 20 64 65 76 69 63 65 3a 0a 2a 2a 0a 2a ing device:.**.*
a970: 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 5b * <ul>.** <li> [
a980: 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f SQLITE_IOCAP_ATO
a990: 4d 49 43 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 MIC].** <li> [SQ
a9a0: 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 LITE_IOCAP_ATOMI
a9b0: 43 35 31 32 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 C512].** <li> [S
a9c0: 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d QLITE_IOCAP_ATOM
a9d0: 49 43 31 4b 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 IC1K].** <li> [S
a9e0: 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d QLITE_IOCAP_ATOM
a9f0: 49 43 32 4b 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 IC2K].** <li> [S
aa00: 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d QLITE_IOCAP_ATOM
aa10: 49 43 34 4b 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 IC4K].** <li> [S
aa20: 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d QLITE_IOCAP_ATOM
aa30: 49 43 38 4b 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 IC8K].** <li> [S
aa40: 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d QLITE_IOCAP_ATOM
aa50: 49 43 31 36 4b 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b IC16K].** <li> [
aa60: 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f SQLITE_IOCAP_ATO
aa70: 4d 49 43 33 32 4b 5d 0a 2a 2a 20 3c 6c 69 3e 20 MIC32K].** <li>
aa80: 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 [SQLITE_IOCAP_AT
aa90: 4f 4d 49 43 36 34 4b 5d 0a 2a 2a 20 3c 6c 69 3e OMIC64K].** <li>
aaa0: 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 [SQLITE_IOCAP_S
aab0: 41 46 45 5f 41 50 50 45 4e 44 5d 0a 2a 2a 20 3c AFE_APPEND].** <
aac0: 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 li> [SQLITE_IOCA
aad0: 50 5f 53 45 51 55 45 4e 54 49 41 4c 5d 0a 2a 2a P_SEQUENTIAL].**
aae0: 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 </ul>.**.** The
aaf0: 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 SQLITE_IOCAP_AT
ab00: 4f 4d 49 43 20 70 72 6f 70 65 72 74 79 20 6d 65 OMIC property me
ab10: 61 6e 73 20 74 68 61 74 20 61 6c 6c 20 77 72 69 ans that all wri
ab20: 74 65 73 20 6f 66 0a 2a 2a 20 61 6e 79 20 73 69 tes of.** any si
ab30: 7a 65 20 61 72 65 20 61 74 6f 6d 69 63 2e 20 20 ze are atomic.
ab40: 54 68 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 The SQLITE_IOCAP
ab50: 5f 41 54 4f 4d 49 43 6e 6e 6e 20 76 61 6c 75 65 _ATOMICnnn value
ab60: 73 0a 2a 2a 20 6d 65 61 6e 20 74 68 61 74 20 77 s.** mean that w
ab70: 72 69 74 65 73 20 6f 66 20 62 6c 6f 63 6b 73 20 rites of blocks
ab80: 74 68 61 74 20 61 72 65 20 6e 6e 6e 20 62 79 74 that are nnn byt
ab90: 65 73 20 69 6e 20 73 69 7a 65 20 61 6e 64 0a 2a es in size and.*
aba0: 2a 20 61 72 65 20 61 6c 69 67 6e 65 64 20 74 6f * are aligned to
abb0: 20 61 6e 20 61 64 64 72 65 73 73 20 77 68 69 63 an address whic
abc0: 68 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 h is an integer
abd0: 6d 75 6c 74 69 70 6c 65 20 6f 66 0a 2a 2a 20 6e multiple of.** n
abe0: 6e 6e 20 61 72 65 20 61 74 6f 6d 69 63 2e 20 20 nn are atomic.
abf0: 54 68 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 The SQLITE_IOCAP
ac00: 5f 53 41 46 45 5f 41 50 50 45 4e 44 20 76 61 6c _SAFE_APPEND val
ac10: 75 65 20 6d 65 61 6e 73 0a 2a 2a 20 74 68 61 74 ue means.** that
ac20: 20 77 68 65 6e 20 64 61 74 61 20 69 73 20 61 70 when data is ap
ac30: 70 65 6e 64 65 64 20 74 6f 20 61 20 66 69 6c 65 pended to a file
ac40: 2c 20 74 68 65 20 64 61 74 61 20 69 73 20 61 70 , the data is ap
ac50: 70 65 6e 64 65 64 0a 2a 2a 20 66 69 72 73 74 20 pended.** first
ac60: 74 68 65 6e 20 74 68 65 20 73 69 7a 65 20 6f 66 then the size of
ac70: 20 74 68 65 20 66 69 6c 65 20 69 73 20 65 78 74 the file is ext
ac80: 65 6e 64 65 64 2c 20 6e 65 76 65 72 20 74 68 65 ended, never the
ac90: 20 6f 74 68 65 72 0a 2a 2a 20 77 61 79 20 61 72 other.** way ar
aca0: 6f 75 6e 64 2e 20 20 54 68 65 20 53 51 4c 49 54 ound. The SQLIT
acb0: 45 5f 49 4f 43 41 50 5f 53 45 51 55 45 4e 54 49 E_IOCAP_SEQUENTI
acc0: 41 4c 20 70 72 6f 70 65 72 74 79 20 6d 65 61 6e AL property mean
acd0: 73 20 74 68 61 74 0a 2a 2a 20 69 6e 66 6f 72 6d s that.** inform
ace0: 61 74 69 6f 6e 20 69 73 20 77 72 69 74 74 65 6e ation is written
acf0: 20 74 6f 20 64 69 73 6b 20 69 6e 20 74 68 65 20 to disk in the
ad00: 73 61 6d 65 20 6f 72 64 65 72 20 61 73 20 63 61 same order as ca
ad10: 6c 6c 73 0a 2a 2a 20 74 6f 20 78 57 72 69 74 65 lls.** to xWrite
ad20: 28 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 78 52 65 ()..**.** If xRe
ad30: 61 64 28 29 20 72 65 74 75 72 6e 73 20 53 51 4c ad() returns SQL
ad40: 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f ITE_IOERR_SHORT_
ad50: 52 45 41 44 20 69 74 20 6d 75 73 74 20 61 6c 73 READ it must als
ad60: 6f 20 66 69 6c 6c 0a 2a 2a 20 69 6e 20 74 68 65 o fill.** in the
ad70: 20 75 6e 72 65 61 64 20 70 6f 72 74 69 6f 6e 73 unread portions
ad80: 20 6f 66 20 74 68 65 20 62 75 66 66 65 72 20 77 of the buffer w
ad90: 69 74 68 20 7a 65 72 6f 73 2e 20 20 41 20 56 46 ith zeros. A VF
ada0: 53 20 74 68 61 74 0a 2a 2a 20 66 61 69 6c 73 20 S that.** fails
adb0: 74 6f 20 7a 65 72 6f 2d 66 69 6c 6c 20 73 68 6f to zero-fill sho
adc0: 72 74 20 72 65 61 64 73 20 6d 69 67 68 74 20 73 rt reads might s
add0: 65 65 6d 20 74 6f 20 77 6f 72 6b 2e 20 20 48 6f eem to work. Ho
ade0: 77 65 76 65 72 2c 0a 2a 2a 20 66 61 69 6c 75 72 wever,.** failur
adf0: 65 20 74 6f 20 7a 65 72 6f 2d 66 69 6c 6c 20 73 e to zero-fill s
ae00: 68 6f 72 74 20 72 65 61 64 73 20 77 69 6c 6c 20 hort reads will
ae10: 65 76 65 6e 74 75 61 6c 6c 79 20 6c 65 61 64 20 eventually lead
ae20: 74 6f 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 to.** database c
ae30: 6f 72 72 75 70 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 orruption..*/.ty
ae40: 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c pedef struct sql
ae50: 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 ite3_io_methods
ae60: 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f sqlite3_io_metho
ae70: 64 73 3b 0a 73 74 72 75 63 74 20 73 71 6c 69 74 ds;.struct sqlit
ae80: 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 7b 0a e3_io_methods {.
ae90: 20 20 69 6e 74 20 69 56 65 72 73 69 6f 6e 3b 0a int iVersion;.
aea0: 20 20 69 6e 74 20 28 2a 78 43 6c 6f 73 65 29 28 int (*xClose)(
aeb0: 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 3b 0a sqlite3_file*);.
aec0: 20 20 69 6e 74 20 28 2a 78 52 65 61 64 29 28 73 int (*xRead)(s
aed0: 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 76 6f qlite3_file*, vo
aee0: 69 64 2a 2c 20 69 6e 74 20 69 41 6d 74 2c 20 73 id*, int iAmt, s
aef0: 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 4f 66 qlite3_int64 iOf
af00: 73 74 29 3b 0a 20 20 69 6e 74 20 28 2a 78 57 72 st);. int (*xWr
af10: 69 74 65 29 28 73 71 6c 69 74 65 33 5f 66 69 6c ite)(sqlite3_fil
af20: 65 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c e*, const void*,
af30: 20 69 6e 74 20 69 41 6d 74 2c 20 73 71 6c 69 74 int iAmt, sqlit
af40: 65 33 5f 69 6e 74 36 34 20 69 4f 66 73 74 29 3b e3_int64 iOfst);
af50: 0a 20 20 69 6e 74 20 28 2a 78 54 72 75 6e 63 61 . int (*xTrunca
af60: 74 65 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 te)(sqlite3_file
af70: 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 *, sqlite3_int64
af80: 20 73 69 7a 65 29 3b 0a 20 20 69 6e 74 20 28 2a size);. int (*
af90: 78 53 79 6e 63 29 28 73 71 6c 69 74 65 33 5f 66 xSync)(sqlite3_f
afa0: 69 6c 65 2a 2c 20 69 6e 74 20 66 6c 61 67 73 29 ile*, int flags)
afb0: 3b 0a 20 20 69 6e 74 20 28 2a 78 46 69 6c 65 53 ;. int (*xFileS
afc0: 69 7a 65 29 28 73 71 6c 69 74 65 33 5f 66 69 6c ize)(sqlite3_fil
afd0: 65 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 e*, sqlite3_int6
afe0: 34 20 2a 70 53 69 7a 65 29 3b 0a 20 20 69 6e 74 4 *pSize);. int
aff0: 20 28 2a 78 4c 6f 63 6b 29 28 73 71 6c 69 74 65 (*xLock)(sqlite
b000: 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a 20 3_file*, int);.
b010: 20 69 6e 74 20 28 2a 78 55 6e 6c 6f 63 6b 29 28 int (*xUnlock)(
b020: 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 sqlite3_file*, i
b030: 6e 74 29 3b 0a 20 20 69 6e 74 20 28 2a 78 43 68 nt);. int (*xCh
b040: 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 29 eckReservedLock)
b050: 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 (sqlite3_file*,
b060: 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 3b 0a 20 int *pResOut);.
b070: 20 69 6e 74 20 28 2a 78 46 69 6c 65 43 6f 6e 74 int (*xFileCont
b080: 72 6f 6c 29 28 73 71 6c 69 74 65 33 5f 66 69 6c rol)(sqlite3_fil
b090: 65 2a 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69 64 e*, int op, void
b0a0: 20 2a 70 41 72 67 29 3b 0a 20 20 69 6e 74 20 28 *pArg);. int (
b0b0: 2a 78 53 65 63 74 6f 72 53 69 7a 65 29 28 73 71 *xSectorSize)(sq
b0c0: 6c 69 74 65 33 5f 66 69 6c 65 2a 29 3b 0a 20 20 lite3_file*);.
b0d0: 69 6e 74 20 28 2a 78 44 65 76 69 63 65 43 68 61 int (*xDeviceCha
b0e0: 72 61 63 74 65 72 69 73 74 69 63 73 29 28 73 71 racteristics)(sq
b0f0: 6c 69 74 65 33 5f 66 69 6c 65 2a 29 3b 0a 20 20 lite3_file*);.
b100: 2f 2a 20 41 64 64 69 74 69 6f 6e 61 6c 20 6d 65 /* Additional me
b110: 74 68 6f 64 73 20 6d 61 79 20 62 65 20 61 64 64 thods may be add
b120: 65 64 20 69 6e 20 66 75 74 75 72 65 20 72 65 6c ed in future rel
b130: 65 61 73 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a eases */.};../*.
b140: 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 53 74 61 ** CAPI3REF: Sta
b150: 6e 64 61 72 64 20 46 69 6c 65 20 43 6f 6e 74 72 ndard File Contr
b160: 6f 6c 20 4f 70 63 6f 64 65 73 20 7b 48 31 31 33 ol Opcodes {H113
b170: 31 30 7d 20 3c 53 33 30 38 30 30 3e 0a 2a 2a 0a 10} <S30800>.**.
b180: 2a 2a 20 54 68 65 73 65 20 69 6e 74 65 67 65 72 ** These integer
b190: 20 63 6f 6e 73 74 61 6e 74 73 20 61 72 65 20 6f constants are o
b1a0: 70 63 6f 64 65 73 20 66 6f 72 20 74 68 65 20 78 pcodes for the x
b1b0: 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 6d 65 74 68 FileControl meth
b1c0: 6f 64 0a 2a 2a 20 6f 66 20 74 68 65 20 5b 73 71 od.** of the [sq
b1d0: 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 lite3_io_methods
b1e0: 5d 20 6f 62 6a 65 63 74 20 61 6e 64 20 66 6f 72 ] object and for
b1f0: 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 66 69 the [sqlite3_fi
b200: 6c 65 5f 63 6f 6e 74 72 6f 6c 28 29 5d 0a 2a 2a le_control()].**
b210: 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a interface..**.*
b220: 2a 20 54 68 65 20 5b 53 51 4c 49 54 45 5f 46 43 * The [SQLITE_FC
b230: 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 5d 20 6f NTL_LOCKSTATE] o
b240: 70 63 6f 64 65 20 69 73 20 75 73 65 64 20 66 6f pcode is used fo
b250: 72 20 64 65 62 75 67 67 69 6e 67 2e 20 20 54 68 r debugging. Th
b260: 69 73 0a 2a 2a 20 6f 70 63 6f 64 65 20 63 61 75 is.** opcode cau
b270: 73 65 73 20 74 68 65 20 78 46 69 6c 65 43 6f 6e ses the xFileCon
b280: 74 72 6f 6c 20 6d 65 74 68 6f 64 20 74 6f 20 77 trol method to w
b290: 72 69 74 65 20 74 68 65 20 63 75 72 72 65 6e 74 rite the current
b2a0: 20 73 74 61 74 65 20 6f 66 0a 2a 2a 20 74 68 65 state of.** the
b2b0: 20 6c 6f 63 6b 20 28 6f 6e 65 20 6f 66 20 5b 53 lock (one of [S
b2c0: 51 4c 49 54 45 5f 4c 4f 43 4b 5f 4e 4f 4e 45 5d QLITE_LOCK_NONE]
b2d0: 2c 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 53 , [SQLITE_LOCK_S
b2e0: 48 41 52 45 44 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 HARED],.** [SQLI
b2f0: 54 45 5f 4c 4f 43 4b 5f 52 45 53 45 52 56 45 44 TE_LOCK_RESERVED
b300: 5d 2c 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f ], [SQLITE_LOCK_
b310: 50 45 4e 44 49 4e 47 5d 2c 20 6f 72 20 5b 53 51 PENDING], or [SQ
b320: 4c 49 54 45 5f 4c 4f 43 4b 5f 45 58 43 4c 55 53 LITE_LOCK_EXCLUS
b330: 49 56 45 5d 29 0a 2a 2a 20 69 6e 74 6f 20 61 6e IVE]).** into an
b340: 20 69 6e 74 65 67 65 72 20 74 68 61 74 20 74 68 integer that th
b350: 65 20 70 41 72 67 20 61 72 67 75 6d 65 6e 74 20 e pArg argument
b360: 70 6f 69 6e 74 73 20 74 6f 2e 20 54 68 69 73 20 points to. This
b370: 63 61 70 61 62 69 6c 69 74 79 0a 2a 2a 20 69 73 capability.** is
b380: 20 75 73 65 64 20 64 75 72 69 6e 67 20 74 65 73 used during tes
b390: 74 69 6e 67 20 61 6e 64 20 6f 6e 6c 79 20 6e 65 ting and only ne
b3a0: 65 64 73 20 74 6f 20 62 65 20 73 75 70 70 6f 72 eds to be suppor
b3b0: 74 65 64 20 77 68 65 6e 20 53 51 4c 49 54 45 5f ted when SQLITE_
b3c0: 54 45 53 54 0a 2a 2a 20 69 73 20 64 65 66 69 6e TEST.** is defin
b3d0: 65 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 ed..*/.#define S
b3e0: 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b QLITE_FCNTL_LOCK
b3f0: 53 54 41 54 45 20 20 20 20 20 20 20 20 31 0a 23 STATE 1.#
b400: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 47 45 define SQLITE_GE
b410: 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45 20 T_LOCKPROXYFILE
b420: 20 20 20 20 20 32 0a 23 64 65 66 69 6e 65 20 53 2.#define S
b430: 51 4c 49 54 45 5f 53 45 54 5f 4c 4f 43 4b 50 52 QLITE_SET_LOCKPR
b440: 4f 58 59 46 49 4c 45 20 20 20 20 20 20 33 0a 23 OXYFILE 3.#
b450: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 41 define SQLITE_LA
b460: 53 54 5f 45 52 52 4e 4f 20 20 20 20 20 20 20 20 ST_ERRNO
b470: 20 20 20 20 20 34 0a 0a 2f 2a 0a 2a 2a 20 43 41 4../*.** CA
b480: 50 49 33 52 45 46 3a 20 4d 75 74 65 78 20 48 61 PI3REF: Mutex Ha
b490: 6e 64 6c 65 20 7b 48 31 37 31 31 30 7d 20 3c 53 ndle {H17110} <S
b4a0: 32 30 31 33 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20130>.**.** The
b4b0: 20 6d 75 74 65 78 20 6d 6f 64 75 6c 65 20 77 69 mutex module wi
b4c0: 74 68 69 6e 20 53 51 4c 69 74 65 20 64 65 66 69 thin SQLite defi
b4d0: 6e 65 73 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 nes [sqlite3_mut
b4e0: 65 78 5d 20 74 6f 20 62 65 20 61 6e 0a 2a 2a 20 ex] to be an.**
b4f0: 61 62 73 74 72 61 63 74 20 74 79 70 65 20 66 6f abstract type fo
b500: 72 20 61 20 6d 75 74 65 78 20 6f 62 6a 65 63 74 r a mutex object
b510: 2e 20 20 54 68 65 20 53 51 4c 69 74 65 20 63 6f . The SQLite co
b520: 72 65 20 6e 65 76 65 72 20 6c 6f 6f 6b 73 0a 2a re never looks.*
b530: 2a 20 61 74 20 74 68 65 20 69 6e 74 65 72 6e 61 * at the interna
b540: 6c 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e l representation
b550: 20 6f 66 20 61 6e 20 5b 73 71 6c 69 74 65 33 5f of an [sqlite3_
b560: 6d 75 74 65 78 5d 2e 20 20 49 74 20 6f 6e 6c 79 mutex]. It only
b570: 0a 2a 2a 20 64 65 61 6c 73 20 77 69 74 68 20 70 .** deals with p
b580: 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20 5b ointers to the [
b590: 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5d 20 6f sqlite3_mutex] o
b5a0: 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 4d 75 74 bject..**.** Mut
b5b0: 65 78 65 73 20 61 72 65 20 63 72 65 61 74 65 64 exes are created
b5c0: 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f using [sqlite3_
b5d0: 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 5d 2e 0a mutex_alloc()]..
b5e0: 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 */.typedef struc
b5f0: 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 t sqlite3_mutex
b600: 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 3b 0a 0a sqlite3_mutex;..
b610: 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
b620: 4f 53 20 49 6e 74 65 72 66 61 63 65 20 4f 62 6a OS Interface Obj
b630: 65 63 74 20 7b 48 31 31 31 34 30 7d 20 3c 53 32 ect {H11140} <S2
b640: 30 31 30 30 3e 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 0100>.**.** An i
b650: 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 73 nstance of the s
b660: 71 6c 69 74 65 33 5f 76 66 73 20 6f 62 6a 65 63 qlite3_vfs objec
b670: 74 20 64 65 66 69 6e 65 73 20 74 68 65 20 69 6e t defines the in
b680: 74 65 72 66 61 63 65 20 62 65 74 77 65 65 6e 0a terface between.
b690: 2a 2a 20 74 68 65 20 53 51 4c 69 74 65 20 63 6f ** the SQLite co
b6a0: 72 65 20 61 6e 64 20 74 68 65 20 75 6e 64 65 72 re and the under
b6b0: 6c 79 69 6e 67 20 6f 70 65 72 61 74 69 6e 67 20 lying operating
b6c0: 73 79 73 74 65 6d 2e 20 20 54 68 65 20 22 76 66 system. The "vf
b6d0: 73 22 0a 2a 2a 20 69 6e 20 74 68 65 20 6e 61 6d s".** in the nam
b6e0: 65 20 6f 66 20 74 68 65 20 6f 62 6a 65 63 74 20 e of the object
b6f0: 73 74 61 6e 64 73 20 66 6f 72 20 22 76 69 72 74 stands for "virt
b700: 75 61 6c 20 66 69 6c 65 20 73 79 73 74 65 6d 22 ual file system"
b710: 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 76 61 6c 75 ..**.** The valu
b720: 65 20 6f 66 20 74 68 65 20 69 56 65 72 73 69 6f e of the iVersio
b730: 6e 20 66 69 65 6c 64 20 69 73 20 69 6e 69 74 69 n field is initi
b740: 61 6c 6c 79 20 31 20 62 75 74 20 6d 61 79 20 62 ally 1 but may b
b750: 65 20 6c 61 72 67 65 72 20 69 6e 0a 2a 2a 20 66 e larger in.** f
b760: 75 74 75 72 65 20 76 65 72 73 69 6f 6e 73 20 6f uture versions o
b770: 66 20 53 51 4c 69 74 65 2e 20 20 41 64 64 69 74 f SQLite. Addit
b780: 69 6f 6e 61 6c 20 66 69 65 6c 64 73 20 6d 61 79 ional fields may
b790: 20 62 65 20 61 70 70 65 6e 64 65 64 20 74 6f 20 be appended to
b7a0: 74 68 69 73 0a 2a 2a 20 6f 62 6a 65 63 74 20 77 this.** object w
b7b0: 68 65 6e 20 74 68 65 20 69 56 65 72 73 69 6f 6e hen the iVersion
b7c0: 20 76 61 6c 75 65 20 69 73 20 69 6e 63 72 65 61 value is increa
b7d0: 73 65 64 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 sed. Note that
b7e0: 74 68 65 20 73 74 72 75 63 74 75 72 65 0a 2a 2a the structure.**
b7f0: 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f of the sqlite3_
b800: 76 66 73 20 6f 62 6a 65 63 74 20 63 68 61 6e 67 vfs object chang
b810: 65 73 20 69 6e 20 74 68 65 20 74 72 61 6e 73 61 es in the transa
b820: 63 74 69 6f 6e 20 62 65 74 77 65 65 6e 0a 2a 2a ction between.**
b830: 20 53 51 4c 69 74 65 20 76 65 72 73 69 6f 6e 20 SQLite version
b840: 33 2e 35 2e 39 20 61 6e 64 20 33 2e 36 2e 30 20 3.5.9 and 3.6.0
b850: 61 6e 64 20 79 65 74 20 74 68 65 20 69 56 65 72 and yet the iVer
b860: 73 69 6f 6e 20 66 69 65 6c 64 20 77 61 73 20 6e sion field was n
b870: 6f 74 0a 2a 2a 20 6d 6f 64 69 66 69 65 64 2e 0a ot.** modified..
b880: 2a 2a 0a 2a 2a 20 54 68 65 20 73 7a 4f 73 46 69 **.** The szOsFi
b890: 6c 65 20 66 69 65 6c 64 20 69 73 20 74 68 65 20 le field is the
b8a0: 73 69 7a 65 20 6f 66 20 74 68 65 20 73 75 62 63 size of the subc
b8b0: 6c 61 73 73 65 64 20 5b 73 71 6c 69 74 65 33 5f lassed [sqlite3_
b8c0: 66 69 6c 65 5d 0a 2a 2a 20 73 74 72 75 63 74 75 file].** structu
b8d0: 72 65 20 75 73 65 64 20 62 79 20 74 68 69 73 20 re used by this
b8e0: 56 46 53 2e 20 20 6d 78 50 61 74 68 6e 61 6d 65 VFS. mxPathname
b8f0: 20 69 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 is the maximum
b900: 6c 65 6e 67 74 68 20 6f 66 0a 2a 2a 20 61 20 70 length of.** a p
b910: 61 74 68 6e 61 6d 65 20 69 6e 20 74 68 69 73 20 athname in this
b920: 56 46 53 2e 0a 2a 2a 0a 2a 2a 20 52 65 67 69 73 VFS..**.** Regis
b930: 74 65 72 65 64 20 73 71 6c 69 74 65 33 5f 76 66 tered sqlite3_vf
b940: 73 20 6f 62 6a 65 63 74 73 20 61 72 65 20 6b 65 s objects are ke
b950: 70 74 20 6f 6e 20 61 20 6c 69 6e 6b 65 64 20 6c pt on a linked l
b960: 69 73 74 20 66 6f 72 6d 65 64 20 62 79 0a 2a 2a ist formed by.**
b970: 20 74 68 65 20 70 4e 65 78 74 20 70 6f 69 6e 74 the pNext point
b980: 65 72 2e 20 20 54 68 65 20 5b 73 71 6c 69 74 65 er. The [sqlite
b990: 33 5f 76 66 73 5f 72 65 67 69 73 74 65 72 28 29 3_vfs_register()
b9a0: 5d 0a 2a 2a 20 61 6e 64 20 5b 73 71 6c 69 74 65 ].** and [sqlite
b9b0: 33 5f 76 66 73 5f 75 6e 72 65 67 69 73 74 65 72 3_vfs_unregister
b9c0: 28 29 5d 20 69 6e 74 65 72 66 61 63 65 73 20 6d ()] interfaces m
b9d0: 61 6e 61 67 65 20 74 68 69 73 20 6c 69 73 74 0a anage this list.
b9e0: 2a 2a 20 69 6e 20 61 20 74 68 72 65 61 64 2d 73 ** in a thread-s
b9f0: 61 66 65 20 77 61 79 2e 20 20 54 68 65 20 5b 73 afe way. The [s
ba00: 71 6c 69 74 65 33 5f 76 66 73 5f 66 69 6e 64 28 qlite3_vfs_find(
ba10: 29 5d 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20 )] interface.**
ba20: 73 65 61 72 63 68 65 73 20 74 68 65 20 6c 69 73 searches the lis
ba30: 74 2e 20 20 4e 65 69 74 68 65 72 20 74 68 65 20 t. Neither the
ba40: 61 70 70 6c 69 63 61 74 69 6f 6e 20 63 6f 64 65 application code
ba50: 20 6e 6f 72 20 74 68 65 20 56 46 53 0a 2a 2a 20 nor the VFS.**
ba60: 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 73 implementation s
ba70: 68 6f 75 6c 64 20 75 73 65 20 74 68 65 20 70 4e hould use the pN
ba80: 65 78 74 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a ext pointer..**.
ba90: 2a 2a 20 54 68 65 20 70 4e 65 78 74 20 66 69 65 ** The pNext fie
baa0: 6c 64 20 69 73 20 74 68 65 20 6f 6e 6c 79 20 66 ld is the only f
bab0: 69 65 6c 64 20 69 6e 20 74 68 65 20 73 71 6c 69 ield in the sqli
bac0: 74 65 33 5f 76 66 73 0a 2a 2a 20 73 74 72 75 63 te3_vfs.** struc
bad0: 74 75 72 65 20 74 68 61 74 20 53 51 4c 69 74 65 ture that SQLite
bae0: 20 77 69 6c 6c 20 65 76 65 72 20 6d 6f 64 69 66 will ever modif
baf0: 79 2e 20 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 y. SQLite will
bb00: 6f 6e 6c 79 20 61 63 63 65 73 73 0a 2a 2a 20 6f only access.** o
bb10: 72 20 6d 6f 64 69 66 79 20 74 68 69 73 20 66 69 r modify this fi
bb20: 65 6c 64 20 77 68 69 6c 65 20 68 6f 6c 64 69 6e eld while holdin
bb30: 67 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 73 g a particular s
bb40: 74 61 74 69 63 20 6d 75 74 65 78 2e 0a 2a 2a 20 tatic mutex..**
bb50: 54 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 The application
bb60: 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 6d 6f 64 should never mod
bb70: 69 66 79 20 61 6e 79 74 68 69 6e 67 20 77 69 74 ify anything wit
bb80: 68 69 6e 20 74 68 65 20 73 71 6c 69 74 65 33 5f hin the sqlite3_
bb90: 76 66 73 0a 2a 2a 20 6f 62 6a 65 63 74 20 6f 6e vfs.** object on
bba0: 63 65 20 74 68 65 20 6f 62 6a 65 63 74 20 68 61 ce the object ha
bbb0: 73 20 62 65 65 6e 20 72 65 67 69 73 74 65 72 65 s been registere
bbc0: 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 7a 4e 61 d..**.** The zNa
bbd0: 6d 65 20 66 69 65 6c 64 20 68 6f 6c 64 73 20 74 me field holds t
bbe0: 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 56 he name of the V
bbf0: 46 53 20 6d 6f 64 75 6c 65 2e 20 20 54 68 65 20 FS module. The
bc00: 6e 61 6d 65 20 6d 75 73 74 0a 2a 2a 20 62 65 20 name must.** be
bc10: 75 6e 69 71 75 65 20 61 63 72 6f 73 73 20 61 6c unique across al
bc20: 6c 20 56 46 53 20 6d 6f 64 75 6c 65 73 2e 0a 2a l VFS modules..*
bc30: 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c *.** SQLite will
bc40: 20 67 75 61 72 61 6e 74 65 65 20 74 68 61 74 20 guarantee that
bc50: 74 68 65 20 7a 46 69 6c 65 6e 61 6d 65 20 70 61 the zFilename pa
bc60: 72 61 6d 65 74 65 72 20 74 6f 20 78 4f 70 65 6e rameter to xOpen
bc70: 0a 2a 2a 20 69 73 20 65 69 74 68 65 72 20 61 20 .** is either a
bc80: 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 6f 72 20 NULL pointer or
bc90: 73 74 72 69 6e 67 20 6f 62 74 61 69 6e 65 64 0a string obtained.
bca0: 2a 2a 20 66 72 6f 6d 20 78 46 75 6c 6c 50 61 74 ** from xFullPat
bcb0: 68 6e 61 6d 65 28 29 2e 20 20 53 51 4c 69 74 65 hname(). SQLite
bcc0: 20 66 75 72 74 68 65 72 20 67 75 61 72 61 6e 74 further guarant
bcd0: 65 65 73 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 ees that.** the
bce0: 73 74 72 69 6e 67 20 77 69 6c 6c 20 62 65 20 76 string will be v
bcf0: 61 6c 69 64 20 61 6e 64 20 75 6e 63 68 61 6e 67 alid and unchang
bd00: 65 64 20 75 6e 74 69 6c 20 78 43 6c 6f 73 65 28 ed until xClose(
bd10: 29 20 69 73 0a 2a 2a 20 63 61 6c 6c 65 64 2e 20 ) is.** called.
bd20: 42 65 63 61 75 73 65 20 6f 66 20 74 68 65 20 70 Because of the p
bd30: 72 65 76 69 6f 75 73 20 73 65 6e 74 65 6e 63 65 revious sentence
bd40: 2c 0a 2a 2a 20 74 68 65 20 5b 73 71 6c 69 74 65 ,.** the [sqlite
bd50: 33 5f 66 69 6c 65 5d 20 63 61 6e 20 73 61 66 65 3_file] can safe
bd60: 6c 79 20 73 74 6f 72 65 20 61 20 70 6f 69 6e 74 ly store a point
bd70: 65 72 20 74 6f 20 74 68 65 0a 2a 2a 20 66 69 6c er to the.** fil
bd80: 65 6e 61 6d 65 20 69 66 20 69 74 20 6e 65 65 64 ename if it need
bd90: 73 20 74 6f 20 72 65 6d 65 6d 62 65 72 20 74 68 s to remember th
bda0: 65 20 66 69 6c 65 6e 61 6d 65 20 66 6f 72 20 73 e filename for s
bdb0: 6f 6d 65 20 72 65 61 73 6f 6e 2e 0a 2a 2a 20 49 ome reason..** I
bdc0: 66 20 74 68 65 20 7a 46 69 6c 65 6e 61 6d 65 20 f the zFilename
bdd0: 70 61 72 61 6d 65 74 65 72 20 69 73 20 78 4f 70 parameter is xOp
bde0: 65 6e 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 en is a NULL poi
bdf0: 6e 74 65 72 20 74 68 65 6e 20 78 4f 70 65 6e 0a nter then xOpen.
be00: 2a 2a 20 6d 75 73 74 20 69 6e 76 65 6e 74 20 69 ** must invent i
be10: 74 73 20 6f 77 6e 20 74 65 6d 70 6f 72 61 72 79 ts own temporary
be20: 20 6e 61 6d 65 20 66 6f 72 20 74 68 65 20 66 69 name for the fi
be30: 6c 65 2e 20 20 57 68 65 6e 65 76 65 72 20 74 68 le. Whenever th
be40: 65 20 0a 2a 2a 20 78 46 69 6c 65 6e 61 6d 65 20 e .** xFilename
be50: 70 61 72 61 6d 65 74 65 72 20 69 73 20 4e 55 4c parameter is NUL
be60: 4c 20 69 74 20 77 69 6c 6c 20 61 6c 73 6f 20 62 L it will also b
be70: 65 20 74 68 65 20 63 61 73 65 20 74 68 61 74 20 e the case that
be80: 74 68 65 0a 2a 2a 20 66 6c 61 67 73 20 70 61 72 the.** flags par
be90: 61 6d 65 74 65 72 20 77 69 6c 6c 20 69 6e 63 6c ameter will incl
bea0: 75 64 65 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e ude [SQLITE_OPEN
beb0: 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 5d 2e _DELETEONCLOSE].
bec0: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6c 61 67 73 .**.** The flags
bed0: 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 78 4f 70 argument to xOp
bee0: 65 6e 28 29 20 69 6e 63 6c 75 64 65 73 20 61 6c en() includes al
bef0: 6c 20 62 69 74 73 20 73 65 74 20 69 6e 0a 2a 2a l bits set in.**
bf00: 20 74 68 65 20 66 6c 61 67 73 20 61 72 67 75 6d the flags argum
bf10: 65 6e 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f ent to [sqlite3_
bf20: 6f 70 65 6e 5f 76 32 28 29 5d 2e 20 20 4f 72 20 open_v2()]. Or
bf30: 69 66 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e if [sqlite3_open
bf40: 28 29 5d 0a 2a 2a 20 6f 72 20 5b 73 71 6c 69 74 ()].** or [sqlit
bf50: 65 33 5f 6f 70 65 6e 31 36 28 29 5d 20 69 73 20 e3_open16()] is
bf60: 75 73 65 64 2c 20 74 68 65 6e 20 66 6c 61 67 73 used, then flags
bf70: 20 69 6e 63 6c 75 64 65 73 20 61 74 20 6c 65 61 includes at lea
bf80: 73 74 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f 50 st.** [SQLITE_OP
bf90: 45 4e 5f 52 45 41 44 57 52 49 54 45 5d 20 7c 20 EN_READWRITE] |
bfa0: 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 [SQLITE_OPEN_CRE
bfb0: 41 54 45 5d 2e 20 0a 2a 2a 20 49 66 20 78 4f 70 ATE]. .** If xOp
bfc0: 65 6e 28 29 20 6f 70 65 6e 73 20 61 20 66 69 6c en() opens a fil
bfd0: 65 20 72 65 61 64 2d 6f 6e 6c 79 20 74 68 65 6e e read-only then
bfe0: 20 69 74 20 73 65 74 73 20 2a 70 4f 75 74 46 6c it sets *pOutFl
bff0: 61 67 73 20 74 6f 0a 2a 2a 20 69 6e 63 6c 75 64 ags to.** includ
c000: 65 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 e [SQLITE_OPEN_R
c010: 45 41 44 4f 4e 4c 59 5d 2e 20 20 4f 74 68 65 72 EADONLY]. Other
c020: 20 62 69 74 73 20 69 6e 20 2a 70 4f 75 74 46 6c bits in *pOutFl
c030: 61 67 73 20 6d 61 79 20 62 65 20 73 65 74 2e 0a ags may be set..
c040: 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c **.** SQLite wil
c050: 6c 20 61 6c 73 6f 20 61 64 64 20 6f 6e 65 20 6f l also add one o
c060: 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
c070: 66 6c 61 67 73 20 74 6f 20 74 68 65 20 78 4f 70 flags to the xOp
c080: 65 6e 28 29 0a 2a 2a 20 63 61 6c 6c 2c 20 64 65 en().** call, de
c090: 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 6f pending on the o
c0a0: 62 6a 65 63 74 20 62 65 69 6e 67 20 6f 70 65 6e bject being open
c0b0: 65 64 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a ed:.**.** <ul>.*
c0c0: 2a 20 3c 6c 69 3e 20 20 5b 53 51 4c 49 54 45 5f * <li> [SQLITE_
c0d0: 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 5d 0a 2a 2a OPEN_MAIN_DB].**
c0e0: 20 3c 6c 69 3e 20 20 5b 53 51 4c 49 54 45 5f 4f <li> [SQLITE_O
c0f0: 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c PEN_MAIN_JOURNAL
c100: 5d 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53 51 4c 49 ].** <li> [SQLI
c110: 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 44 42 5d TE_OPEN_TEMP_DB]
c120: 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53 51 4c 49 54 .** <li> [SQLIT
c130: 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 4a 4f 55 52 E_OPEN_TEMP_JOUR
c140: 4e 41 4c 5d 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53 NAL].** <li> [S
c150: 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 52 41 4e 53 QLITE_OPEN_TRANS
c160: 49 45 4e 54 5f 44 42 5d 0a 2a 2a 20 3c 6c 69 3e IENT_DB].** <li>
c170: 20 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 [SQLITE_OPEN_S
c180: 55 42 4a 4f 55 52 4e 41 4c 5d 0a 2a 2a 20 3c 6c UBJOURNAL].** <l
c190: 69 3e 20 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e i> [SQLITE_OPEN
c1a0: 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 5d _MASTER_JOURNAL]
c1b0: 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 .** </ul>.**.**
c1c0: 54 68 65 20 66 69 6c 65 20 49 2f 4f 20 69 6d 70 The file I/O imp
c1d0: 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 63 61 6e 20 lementation can
c1e0: 75 73 65 20 74 68 65 20 6f 62 6a 65 63 74 20 74 use the object t
c1f0: 79 70 65 20 66 6c 61 67 73 20 74 6f 0a 2a 2a 20 ype flags to.**
c200: 63 68 61 6e 67 65 20 74 68 65 20 77 61 79 20 69 change the way i
c210: 74 20 64 65 61 6c 73 20 77 69 74 68 20 66 69 6c t deals with fil
c220: 65 73 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 es. For example
c230: 2c 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e , an application
c240: 0a 2a 2a 20 74 68 61 74 20 64 6f 65 73 20 6e 6f .** that does no
c250: 74 20 63 61 72 65 20 61 62 6f 75 74 20 63 72 61 t care about cra
c260: 73 68 20 72 65 63 6f 76 65 72 79 20 6f 72 20 72 sh recovery or r
c270: 6f 6c 6c 62 61 63 6b 20 6d 69 67 68 74 20 6d 61 ollback might ma
c280: 6b 65 0a 2a 2a 20 74 68 65 20 6f 70 65 6e 20 6f ke.** the open o
c290: 66 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 f a journal file
c2a0: 20 61 20 6e 6f 2d 6f 70 2e 20 20 57 72 69 74 65 a no-op. Write
c2b0: 73 20 74 6f 20 74 68 69 73 20 6a 6f 75 72 6e 61 s to this journa
c2c0: 6c 20 77 6f 75 6c 64 0a 2a 2a 20 61 6c 73 6f 20 l would.** also
c2d0: 62 65 20 6e 6f 2d 6f 70 73 2c 20 61 6e 64 20 61 be no-ops, and a
c2e0: 6e 79 20 61 74 74 65 6d 70 74 20 74 6f 20 72 65 ny attempt to re
c2f0: 61 64 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 77 ad the journal w
c300: 6f 75 6c 64 20 72 65 74 75 72 6e 0a 2a 2a 20 53 ould return.** S
c310: 51 4c 49 54 45 5f 49 4f 45 52 52 2e 20 20 4f 72 QLITE_IOERR. Or
c320: 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 the implementat
c330: 69 6f 6e 20 6d 69 67 68 74 20 72 65 63 6f 67 6e ion might recogn
c340: 69 7a 65 20 74 68 61 74 20 61 20 64 61 74 61 62 ize that a datab
c350: 61 73 65 0a 2a 2a 20 66 69 6c 65 20 77 69 6c 6c ase.** file will
c360: 20 62 65 20 64 6f 69 6e 67 20 70 61 67 65 2d 61 be doing page-a
c370: 6c 69 67 6e 65 64 20 73 65 63 74 6f 72 20 72 65 ligned sector re
c380: 61 64 73 20 61 6e 64 20 77 72 69 74 65 73 20 69 ads and writes i
c390: 6e 20 61 20 72 61 6e 64 6f 6d 0a 2a 2a 20 6f 72 n a random.** or
c3a0: 64 65 72 20 61 6e 64 20 73 65 74 20 75 70 20 69 der and set up i
c3b0: 74 73 20 49 2f 4f 20 73 75 62 73 79 73 74 65 6d ts I/O subsystem
c3c0: 20 61 63 63 6f 72 64 69 6e 67 6c 79 2e 0a 2a 2a accordingly..**
c3d0: 0a 2a 2a 20 53 51 4c 69 74 65 20 6d 69 67 68 74 .** SQLite might
c3e0: 20 61 6c 73 6f 20 61 64 64 20 6f 6e 65 20 6f 66 also add one of
c3f0: 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 the following f
c400: 6c 61 67 73 20 74 6f 20 74 68 65 20 78 4f 70 65 lags to the xOpe
c410: 6e 20 6d 65 74 68 6f 64 3a 0a 2a 2a 0a 2a 2a 20 n method:.**.**
c420: 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 <ul>.** <li> [SQ
c430: 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 LITE_OPEN_DELETE
c440: 4f 4e 43 4c 4f 53 45 5d 0a 2a 2a 20 3c 6c 69 3e ONCLOSE].** <li>
c450: 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 [SQLITE_OPEN_EX
c460: 43 4c 55 53 49 56 45 5d 0a 2a 2a 20 3c 2f 75 6c CLUSIVE].** </ul
c470: 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 53 51 4c >.**.** The [SQL
c480: 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f ITE_OPEN_DELETEO
c490: 4e 43 4c 4f 53 45 5d 20 66 6c 61 67 20 6d 65 61 NCLOSE] flag mea
c4a0: 6e 73 20 74 68 65 20 66 69 6c 65 20 73 68 6f 75 ns the file shou
c4b0: 6c 64 20 62 65 0a 2a 2a 20 64 65 6c 65 74 65 64 ld be.** deleted
c4c0: 20 77 68 65 6e 20 69 74 20 69 73 20 63 6c 6f 73 when it is clos
c4d0: 65 64 2e 20 20 54 68 65 20 5b 53 51 4c 49 54 45 ed. The [SQLITE
c4e0: 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c _OPEN_DELETEONCL
c4f0: 4f 53 45 5d 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 OSE].** will be
c500: 73 65 74 20 66 6f 72 20 54 45 4d 50 20 20 64 61 set for TEMP da
c510: 74 61 62 61 73 65 73 2c 20 6a 6f 75 72 6e 61 6c tabases, journal
c520: 73 20 61 6e 64 20 66 6f 72 20 73 75 62 6a 6f 75 s and for subjou
c530: 72 6e 61 6c 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 rnals..**.** The
c540: 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 [SQLITE_OPEN_EX
c550: 43 4c 55 53 49 56 45 5d 20 66 6c 61 67 20 69 73 CLUSIVE] flag is
c560: 20 61 6c 77 61 79 73 20 75 73 65 64 20 69 6e 20 always used in
c570: 63 6f 6e 6a 75 6e 63 74 69 6f 6e 0a 2a 2a 20 77 conjunction.** w
c580: 69 74 68 20 74 68 65 20 5b 53 51 4c 49 54 45 5f ith the [SQLITE_
c590: 4f 50 45 4e 5f 43 52 45 41 54 45 5d 20 66 6c 61 OPEN_CREATE] fla
c5a0: 67 2c 20 77 68 69 63 68 20 61 72 65 20 62 6f 74 g, which are bot
c5b0: 68 20 64 69 72 65 63 74 6c 79 0a 2a 2a 20 61 6e h directly.** an
c5c0: 61 6c 6f 67 6f 75 73 20 74 6f 20 74 68 65 20 4f alogous to the O
c5d0: 5f 45 58 43 4c 20 61 6e 64 20 4f 5f 43 52 45 41 _EXCL and O_CREA
c5e0: 54 20 66 6c 61 67 73 20 6f 66 20 74 68 65 20 50 T flags of the P
c5f0: 4f 53 49 58 20 6f 70 65 6e 28 29 0a 2a 2a 20 41 OSIX open().** A
c600: 50 49 2e 20 20 54 68 65 20 53 51 4c 49 54 45 5f PI. The SQLITE_
c610: 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 20 66 OPEN_EXCLUSIVE f
c620: 6c 61 67 2c 20 77 68 65 6e 20 70 61 69 72 65 64 lag, when paired
c630: 20 77 69 74 68 20 74 68 65 20 0a 2a 2a 20 53 51 with the .** SQ
c640: 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 LITE_OPEN_CREATE
c650: 2c 20 69 73 20 75 73 65 64 20 74 6f 20 69 6e 64 , is used to ind
c660: 69 63 61 74 65 20 74 68 61 74 20 66 69 6c 65 20 icate that file
c670: 73 68 6f 75 6c 64 20 61 6c 77 61 79 73 0a 2a 2a should always.**
c680: 20 62 65 20 63 72 65 61 74 65 64 2c 20 61 6e 64 be created, and
c690: 20 74 68 61 74 20 69 74 20 69 73 20 61 6e 20 65 that it is an e
c6a0: 72 72 6f 72 20 69 66 20 69 74 20 61 6c 72 65 61 rror if it alrea
c6b0: 64 79 20 65 78 69 73 74 73 2e 0a 2a 2a 20 49 74 dy exists..** It
c6c0: 20 69 73 20 3c 69 3e 6e 6f 74 3c 2f 69 3e 20 75 is <i>not</i> u
c6d0: 73 65 64 20 74 6f 20 69 6e 64 69 63 61 74 65 20 sed to indicate
c6e0: 74 68 65 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 the file should
c6f0: 62 65 20 6f 70 65 6e 65 64 20 0a 2a 2a 20 66 6f be opened .** fo
c700: 72 20 65 78 63 6c 75 73 69 76 65 20 61 63 63 65 r exclusive acce
c710: 73 73 2e 0a 2a 2a 0a 2a 2a 20 41 74 20 6c 65 61 ss..**.** At lea
c720: 73 74 20 73 7a 4f 73 46 69 6c 65 20 62 79 74 65 st szOsFile byte
c730: 73 20 6f 66 20 6d 65 6d 6f 72 79 20 61 72 65 20 s of memory are
c740: 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 53 51 4c allocated by SQL
c750: 69 74 65 0a 2a 2a 20 74 6f 20 68 6f 6c 64 20 74 ite.** to hold t
c760: 68 65 20 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c he [sqlite3_fil
c770: 65 5d 20 73 74 72 75 63 74 75 72 65 20 70 61 73 e] structure pas
c780: 73 65 64 20 61 73 20 74 68 65 20 74 68 69 72 64 sed as the third
c790: 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 .** argument to
c7a0: 78 4f 70 65 6e 2e 20 20 54 68 65 20 78 4f 70 65 xOpen. The xOpe
c7b0: 6e 20 6d 65 74 68 6f 64 20 64 6f 65 73 20 6e 6f n method does no
c7c0: 74 20 68 61 76 65 20 74 6f 0a 2a 2a 20 61 6c 6c t have to.** all
c7d0: 6f 63 61 74 65 20 74 68 65 20 73 74 72 75 63 74 ocate the struct
c7e0: 75 72 65 3b 20 69 74 20 73 68 6f 75 6c 64 20 6a ure; it should j
c7f0: 75 73 74 20 66 69 6c 6c 20 69 74 20 69 6e 2e 20 ust fill it in.
c800: 20 4e 6f 74 65 20 74 68 61 74 0a 2a 2a 20 74 68 Note that.** th
c810: 65 20 78 4f 70 65 6e 20 6d 65 74 68 6f 64 20 6d e xOpen method m
c820: 75 73 74 20 73 65 74 20 74 68 65 20 73 71 6c 69 ust set the sqli
c830: 74 65 33 5f 66 69 6c 65 2e 70 4d 65 74 68 6f 64 te3_file.pMethod
c840: 73 20 74 6f 20 65 69 74 68 65 72 0a 2a 2a 20 61 s to either.** a
c850: 20 76 61 6c 69 64 20 5b 73 71 6c 69 74 65 33 5f valid [sqlite3_
c860: 69 6f 5f 6d 65 74 68 6f 64 73 5d 20 6f 62 6a 65 io_methods] obje
c870: 63 74 20 6f 72 20 74 6f 20 4e 55 4c 4c 2e 20 20 ct or to NULL.
c880: 78 4f 70 65 6e 20 6d 75 73 74 20 64 6f 0a 2a 2a xOpen must do.**
c890: 20 74 68 69 73 20 65 76 65 6e 20 69 66 20 74 68 this even if th
c8a0: 65 20 6f 70 65 6e 20 66 61 69 6c 73 2e 20 20 53 e open fails. S
c8b0: 51 4c 69 74 65 20 65 78 70 65 63 74 73 20 74 68 QLite expects th
c8c0: 61 74 20 74 68 65 20 73 71 6c 69 74 65 33 5f 66 at the sqlite3_f
c8d0: 69 6c 65 2e 70 4d 65 74 68 6f 64 73 0a 2a 2a 20 ile.pMethods.**
c8e0: 65 6c 65 6d 65 6e 74 20 77 69 6c 6c 20 62 65 20 element will be
c8f0: 76 61 6c 69 64 20 61 66 74 65 72 20 78 4f 70 65 valid after xOpe
c900: 6e 20 72 65 74 75 72 6e 73 20 72 65 67 61 72 64 n returns regard
c910: 6c 65 73 73 20 6f 66 20 74 68 65 20 73 75 63 63 less of the succ
c920: 65 73 73 0a 2a 2a 20 6f 72 20 66 61 69 6c 75 72 ess.** or failur
c930: 65 20 6f 66 20 74 68 65 20 78 4f 70 65 6e 20 63 e of the xOpen c
c940: 61 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 all..**.** The f
c950: 6c 61 67 73 20 61 72 67 75 6d 65 6e 74 20 74 6f lags argument to
c960: 20 78 41 63 63 65 73 73 28 29 20 6d 61 79 20 62 xAccess() may b
c970: 65 20 5b 53 51 4c 49 54 45 5f 41 43 43 45 53 53 e [SQLITE_ACCESS
c980: 5f 45 58 49 53 54 53 5d 0a 2a 2a 20 74 6f 20 74 _EXISTS].** to t
c990: 65 73 74 20 66 6f 72 20 74 68 65 20 65 78 69 73 est for the exis
c9a0: 74 65 6e 63 65 20 6f 66 20 61 20 66 69 6c 65 2c tence of a file,
c9b0: 20 6f 72 20 5b 53 51 4c 49 54 45 5f 41 43 43 45 or [SQLITE_ACCE
c9c0: 53 53 5f 52 45 41 44 57 52 49 54 45 5d 20 74 6f SS_READWRITE] to
c9d0: 0a 2a 2a 20 74 65 73 74 20 77 68 65 74 68 65 72 .** test whether
c9e0: 20 61 20 66 69 6c 65 20 69 73 20 72 65 61 64 61 a file is reada
c9f0: 62 6c 65 20 61 6e 64 20 77 72 69 74 61 62 6c 65 ble and writable
ca00: 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f 41 43 43 , or [SQLITE_ACC
ca10: 45 53 53 5f 52 45 41 44 5d 0a 2a 2a 20 74 6f 20 ESS_READ].** to
ca20: 74 65 73 74 20 77 68 65 74 68 65 72 20 61 20 66 test whether a f
ca30: 69 6c 65 20 69 73 20 61 74 20 6c 65 61 73 74 20 ile is at least
ca40: 72 65 61 64 61 62 6c 65 2e 20 20 20 54 68 65 20 readable. The
ca50: 66 69 6c 65 20 63 61 6e 20 62 65 20 61 0a 2a 2a file can be a.**
ca60: 20 64 69 72 65 63 74 6f 72 79 2e 0a 2a 2a 0a 2a directory..**.*
ca70: 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 61 6c * SQLite will al
ca80: 77 61 79 73 20 61 6c 6c 6f 63 61 74 65 20 61 74 ways allocate at
ca90: 20 6c 65 61 73 74 20 6d 78 50 61 74 68 6e 61 6d least mxPathnam
caa0: 65 2b 31 20 62 79 74 65 73 20 66 6f 72 20 74 68 e+1 bytes for th
cab0: 65 0a 2a 2a 20 6f 75 74 70 75 74 20 62 75 66 66 e.** output buff
cac0: 65 72 20 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65 er xFullPathname
cad0: 2e 20 20 54 68 65 20 65 78 61 63 74 20 73 69 7a . The exact siz
cae0: 65 20 6f 66 20 74 68 65 20 6f 75 74 70 75 74 20 e of the output
caf0: 62 75 66 66 65 72 0a 2a 2a 20 69 73 20 61 6c 73 buffer.** is als
cb00: 6f 20 70 61 73 73 65 64 20 61 73 20 61 20 70 61 o passed as a pa
cb10: 72 61 6d 65 74 65 72 20 74 6f 20 62 6f 74 68 20 rameter to both
cb20: 20 6d 65 74 68 6f 64 73 2e 20 49 66 20 74 68 65 methods. If the
cb30: 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 0a 2a output buffer.*
cb40: 2a 20 69 73 20 6e 6f 74 20 6c 61 72 67 65 20 65 * is not large e
cb50: 6e 6f 75 67 68 2c 20 5b 53 51 4c 49 54 45 5f 43 nough, [SQLITE_C
cb60: 41 4e 54 4f 50 45 4e 5d 20 73 68 6f 75 6c 64 20 ANTOPEN] should
cb70: 62 65 20 72 65 74 75 72 6e 65 64 2e 20 53 69 6e be returned. Sin
cb80: 63 65 20 74 68 69 73 20 69 73 0a 2a 2a 20 68 61 ce this is.** ha
cb90: 6e 64 6c 65 64 20 61 73 20 61 20 66 61 74 61 6c ndled as a fatal
cba0: 20 65 72 72 6f 72 20 62 79 20 53 51 4c 69 74 65 error by SQLite
cbb0: 2c 20 76 66 73 20 69 6d 70 6c 65 6d 65 6e 74 61 , vfs implementa
cbc0: 74 69 6f 6e 73 20 73 68 6f 75 6c 64 20 65 6e 64 tions should end
cbd0: 65 61 76 6f 72 0a 2a 2a 20 74 6f 20 70 72 65 76 eavor.** to prev
cbe0: 65 6e 74 20 74 68 69 73 20 62 79 20 73 65 74 74 ent this by sett
cbf0: 69 6e 67 20 6d 78 50 61 74 68 6e 61 6d 65 20 74 ing mxPathname t
cc00: 6f 20 61 20 73 75 66 66 69 63 69 65 6e 74 6c 79 o a sufficiently
cc10: 20 6c 61 72 67 65 20 76 61 6c 75 65 2e 0a 2a 2a large value..**
cc20: 0a 2a 2a 20 54 68 65 20 78 52 61 6e 64 6f 6d 6e .** The xRandomn
cc30: 65 73 73 28 29 2c 20 78 53 6c 65 65 70 28 29 2c ess(), xSleep(),
cc40: 20 61 6e 64 20 78 43 75 72 72 65 6e 74 54 69 6d and xCurrentTim
cc50: 65 28 29 20 69 6e 74 65 72 66 61 63 65 73 0a 2a e() interfaces.*
cc60: 2a 20 61 72 65 20 6e 6f 74 20 73 74 72 69 63 74 * are not strict
cc70: 6c 79 20 61 20 70 61 72 74 20 6f 66 20 74 68 65 ly a part of the
cc80: 20 66 69 6c 65 73 79 73 74 65 6d 2c 20 62 75 74 filesystem, but
cc90: 20 74 68 65 79 20 61 72 65 0a 2a 2a 20 69 6e 63 they are.** inc
cca0: 6c 75 64 65 64 20 69 6e 20 74 68 65 20 56 46 53 luded in the VFS
ccb0: 20 73 74 72 75 63 74 75 72 65 20 66 6f 72 20 63 structure for c
ccc0: 6f 6d 70 6c 65 74 65 6e 65 73 73 2e 0a 2a 2a 20 ompleteness..**
ccd0: 54 68 65 20 78 52 61 6e 64 6f 6d 6e 65 73 73 28 The xRandomness(
cce0: 29 20 66 75 6e 63 74 69 6f 6e 20 61 74 74 65 6d ) function attem
ccf0: 70 74 73 20 74 6f 20 72 65 74 75 72 6e 20 6e 42 pts to return nB
cd00: 79 74 65 73 20 62 79 74 65 73 0a 2a 2a 20 6f 66 ytes bytes.** of
cd10: 20 67 6f 6f 64 2d 71 75 61 6c 69 74 79 20 72 61 good-quality ra
cd20: 6e 64 6f 6d 6e 65 73 73 20 69 6e 74 6f 20 7a 4f ndomness into zO
cd30: 75 74 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20 ut. The return
cd40: 76 61 6c 75 65 20 69 73 0a 2a 2a 20 74 68 65 20 value is.** the
cd50: 61 63 74 75 61 6c 20 6e 75 6d 62 65 72 20 6f 66 actual number of
cd60: 20 62 79 74 65 73 20 6f 66 20 72 61 6e 64 6f 6d bytes of random
cd70: 6e 65 73 73 20 6f 62 74 61 69 6e 65 64 2e 0a 2a ness obtained..*
cd80: 2a 20 54 68 65 20 78 53 6c 65 65 70 28 29 20 6d * The xSleep() m
cd90: 65 74 68 6f 64 20 63 61 75 73 65 73 20 74 68 65 ethod causes the
cda0: 20 63 61 6c 6c 69 6e 67 20 74 68 72 65 61 64 20 calling thread
cdb0: 74 6f 20 73 6c 65 65 70 20 66 6f 72 20 61 74 0a to sleep for at.
cdc0: 2a 2a 20 6c 65 61 73 74 20 74 68 65 20 6e 75 6d ** least the num
cdd0: 62 65 72 20 6f 66 20 6d 69 63 72 6f 73 65 63 6f ber of microseco
cde0: 6e 64 73 20 67 69 76 65 6e 2e 20 20 54 68 65 20 nds given. The
cdf0: 78 43 75 72 72 65 6e 74 54 69 6d 65 28 29 0a 2a xCurrentTime().*
ce00: 2a 20 6d 65 74 68 6f 64 20 72 65 74 75 72 6e 73 * method returns
ce10: 20 61 20 4a 75 6c 69 61 6e 20 44 61 79 20 4e 75 a Julian Day Nu
ce20: 6d 62 65 72 20 66 6f 72 20 74 68 65 20 63 75 72 mber for the cur
ce30: 72 65 6e 74 20 64 61 74 65 20 61 6e 64 20 74 69 rent date and ti
ce40: 6d 65 2e 0a 2a 2a 0a 2a 2f 0a 74 79 70 65 64 65 me..**.*/.typede
ce50: 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 f struct sqlite3
ce60: 5f 76 66 73 20 73 71 6c 69 74 65 33 5f 76 66 73 _vfs sqlite3_vfs
ce70: 3b 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 ;.struct sqlite3
ce80: 5f 76 66 73 20 7b 0a 20 20 69 6e 74 20 69 56 65 _vfs {. int iVe
ce90: 72 73 69 6f 6e 3b 20 20 20 20 20 20 20 20 20 20 rsion;
cea0: 20 20 2f 2a 20 53 74 72 75 63 74 75 72 65 20 76 /* Structure v
ceb0: 65 72 73 69 6f 6e 20 6e 75 6d 62 65 72 20 2a 2f ersion number */
cec0: 0a 20 20 69 6e 74 20 73 7a 4f 73 46 69 6c 65 3b . int szOsFile;
ced0: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 /* S
cee0: 69 7a 65 20 6f 66 20 73 75 62 63 6c 61 73 73 65 ize of subclasse
cef0: 64 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a d sqlite3_file *
cf00: 2f 0a 20 20 69 6e 74 20 6d 78 50 61 74 68 6e 61 /. int mxPathna
cf10: 6d 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 me; /*
cf20: 4d 61 78 69 6d 75 6d 20 66 69 6c 65 20 70 61 74 Maximum file pat
cf30: 68 6e 61 6d 65 20 6c 65 6e 67 74 68 20 2a 2f 0a hname length */.
cf40: 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 sqlite3_vfs *p
cf50: 4e 65 78 74 3b 20 20 20 20 20 20 2f 2a 20 4e 65 Next; /* Ne
cf60: 78 74 20 72 65 67 69 73 74 65 72 65 64 20 56 46 xt registered VF
cf70: 53 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 S */. const cha
cf80: 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 r *zName;
cf90: 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 69 73 20 /* Name of this
cfa0: 76 69 72 74 75 61 6c 20 66 69 6c 65 20 73 79 73 virtual file sys
cfb0: 74 65 6d 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 tem */. void *p
cfc0: 41 70 70 44 61 74 61 3b 20 20 20 20 20 20 20 20 AppData;
cfd0: 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 /* Pointer to
cfe0: 61 70 70 6c 69 63 61 74 69 6f 6e 2d 73 70 65 63 application-spec
cff0: 69 66 69 63 20 64 61 74 61 20 2a 2f 0a 20 20 69 ific data */. i
d000: 6e 74 20 28 2a 78 4f 70 65 6e 29 28 73 71 6c 69 nt (*xOpen)(sqli
d010: 74 65 33 5f 76 66 73 2a 2c 20 63 6f 6e 73 74 20 te3_vfs*, const
d020: 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 73 71 6c char *zName, sql
d030: 69 74 65 33 5f 66 69 6c 65 2a 2c 0a 20 20 20 20 ite3_file*,.
d040: 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 66 int f
d050: 6c 61 67 73 2c 20 69 6e 74 20 2a 70 4f 75 74 46 lags, int *pOutF
d060: 6c 61 67 73 29 3b 0a 20 20 69 6e 74 20 28 2a 78 lags);. int (*x
d070: 44 65 6c 65 74 65 29 28 73 71 6c 69 74 65 33 5f Delete)(sqlite3_
d080: 76 66 73 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 vfs*, const char
d090: 20 2a 7a 4e 61 6d 65 2c 20 69 6e 74 20 73 79 6e *zName, int syn
d0a0: 63 44 69 72 29 3b 0a 20 20 69 6e 74 20 28 2a 78 cDir);. int (*x
d0b0: 41 63 63 65 73 73 29 28 73 71 6c 69 74 65 33 5f Access)(sqlite3_
d0c0: 76 66 73 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 vfs*, const char
d0d0: 20 2a 7a 4e 61 6d 65 2c 20 69 6e 74 20 66 6c 61 *zName, int fla
d0e0: 67 73 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 gs, int *pResOut
d0f0: 29 3b 0a 20 20 69 6e 74 20 28 2a 78 46 75 6c 6c );. int (*xFull
d100: 50 61 74 68 6e 61 6d 65 29 28 73 71 6c 69 74 65 Pathname)(sqlite
d110: 33 5f 76 66 73 2a 2c 20 63 6f 6e 73 74 20 63 68 3_vfs*, const ch
d120: 61 72 20 2a 7a 4e 61 6d 65 2c 20 69 6e 74 20 6e ar *zName, int n
d130: 4f 75 74 2c 20 63 68 61 72 20 2a 7a 4f 75 74 29 Out, char *zOut)
d140: 3b 0a 20 20 76 6f 69 64 20 2a 28 2a 78 44 6c 4f ;. void *(*xDlO
d150: 70 65 6e 29 28 73 71 6c 69 74 65 33 5f 76 66 73 pen)(sqlite3_vfs
d160: 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a *, const char *z
d170: 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 76 6f 69 Filename);. voi
d180: 64 20 28 2a 78 44 6c 45 72 72 6f 72 29 28 73 71 d (*xDlError)(sq
d190: 6c 69 74 65 33 5f 76 66 73 2a 2c 20 69 6e 74 20 lite3_vfs*, int
d1a0: 6e 42 79 74 65 2c 20 63 68 61 72 20 2a 7a 45 72 nByte, char *zEr
d1b0: 72 4d 73 67 29 3b 0a 20 20 76 6f 69 64 20 28 2a rMsg);. void (*
d1c0: 28 2a 78 44 6c 53 79 6d 29 28 73 71 6c 69 74 65 (*xDlSym)(sqlite
d1d0: 33 5f 76 66 73 2a 2c 76 6f 69 64 2a 2c 20 63 6f 3_vfs*,void*, co
d1e0: 6e 73 74 20 63 68 61 72 20 2a 7a 53 79 6d 62 6f nst char *zSymbo
d1f0: 6c 29 29 28 76 6f 69 64 29 3b 0a 20 20 76 6f 69 l))(void);. voi
d200: 64 20 28 2a 78 44 6c 43 6c 6f 73 65 29 28 73 71 d (*xDlClose)(sq
d210: 6c 69 74 65 33 5f 76 66 73 2a 2c 20 76 6f 69 64 lite3_vfs*, void
d220: 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 52 61 6e *);. int (*xRan
d230: 64 6f 6d 6e 65 73 73 29 28 73 71 6c 69 74 65 33 domness)(sqlite3
d240: 5f 76 66 73 2a 2c 20 69 6e 74 20 6e 42 79 74 65 _vfs*, int nByte
d250: 2c 20 63 68 61 72 20 2a 7a 4f 75 74 29 3b 0a 20 , char *zOut);.
d260: 20 69 6e 74 20 28 2a 78 53 6c 65 65 70 29 28 73 int (*xSleep)(s
d270: 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 69 6e 74 qlite3_vfs*, int
d280: 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73 29 3b 0a microseconds);.
d290: 20 20 69 6e 74 20 28 2a 78 43 75 72 72 65 6e 74 int (*xCurrent
d2a0: 54 69 6d 65 29 28 73 71 6c 69 74 65 33 5f 76 66 Time)(sqlite3_vf
d2b0: 73 2a 2c 20 64 6f 75 62 6c 65 2a 29 3b 0a 20 20 s*, double*);.
d2c0: 69 6e 74 20 28 2a 78 47 65 74 4c 61 73 74 45 72 int (*xGetLastEr
d2d0: 72 6f 72 29 28 73 71 6c 69 74 65 33 5f 76 66 73 ror)(sqlite3_vfs
d2e0: 2a 2c 20 69 6e 74 2c 20 63 68 61 72 20 2a 29 3b *, int, char *);
d2f0: 0a 20 20 2f 2a 20 4e 65 77 20 66 69 65 6c 64 73 . /* New fields
d300: 20 6d 61 79 20 62 65 20 61 70 70 65 6e 64 65 64 may be appended
d310: 20 69 6e 20 66 69 67 75 72 65 20 76 65 72 73 69 in figure versi
d320: 6f 6e 73 2e 20 20 54 68 65 20 69 56 65 72 73 69 ons. The iVersi
d330: 6f 6e 0a 20 20 2a 2a 20 76 61 6c 75 65 20 77 69 on. ** value wi
d340: 6c 6c 20 69 6e 63 72 65 6d 65 6e 74 20 77 68 65 ll increment whe
d350: 6e 65 76 65 72 20 74 68 69 73 20 68 61 70 70 65 never this happe
d360: 6e 73 2e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a ns. */.};../*.**
d370: 20 43 41 50 49 33 52 45 46 3a 20 46 6c 61 67 73 CAPI3REF: Flags
d380: 20 66 6f 72 20 74 68 65 20 78 41 63 63 65 73 73 for the xAccess
d390: 20 56 46 53 20 6d 65 74 68 6f 64 20 7b 48 31 31 VFS method {H11
d3a0: 31 39 30 7d 20 3c 48 31 31 31 34 30 3e 0a 2a 2a 190} <H11140>.**
d3b0: 0a 2a 2a 20 54 68 65 73 65 20 69 6e 74 65 67 65 .** These intege
d3c0: 72 20 63 6f 6e 73 74 61 6e 74 73 20 63 61 6e 20 r constants can
d3d0: 62 65 20 75 73 65 64 20 61 73 20 74 68 65 20 74 be used as the t
d3e0: 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 20 74 hird parameter t
d3f0: 6f 0a 2a 2a 20 74 68 65 20 78 41 63 63 65 73 73 o.** the xAccess
d400: 20 6d 65 74 68 6f 64 20 6f 66 20 61 6e 20 5b 73 method of an [s
d410: 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65 qlite3_vfs] obje
d420: 63 74 2e 20 7b 45 4e 44 7d 20 20 54 68 65 79 20 ct. {END} They
d430: 64 65 74 65 72 6d 69 6e 65 0a 2a 2a 20 77 68 61 determine.** wha
d440: 74 20 6b 69 6e 64 20 6f 66 20 70 65 72 6d 69 73 t kind of permis
d450: 73 69 6f 6e 73 20 74 68 65 20 78 41 63 63 65 73 sions the xAcces
d460: 73 20 6d 65 74 68 6f 64 20 69 73 20 6c 6f 6f 6b s method is look
d470: 69 6e 67 20 66 6f 72 2e 0a 2a 2a 20 57 69 74 68 ing for..** With
d480: 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 SQLITE_ACCESS_E
d490: 58 49 53 54 53 2c 20 74 68 65 20 78 41 63 63 65 XISTS, the xAcce
d4a0: 73 73 20 6d 65 74 68 6f 64 0a 2a 2a 20 73 69 6d ss method.** sim
d4b0: 70 6c 79 20 63 68 65 63 6b 73 20 77 68 65 74 68 ply checks wheth
d4c0: 65 72 20 74 68 65 20 66 69 6c 65 20 65 78 69 73 er the file exis
d4d0: 74 73 2e 0a 2a 2a 20 57 69 74 68 20 53 51 4c 49 ts..** With SQLI
d4e0: 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 57 52 TE_ACCESS_READWR
d4f0: 49 54 45 2c 20 74 68 65 20 78 41 63 63 65 73 73 ITE, the xAccess
d500: 20 6d 65 74 68 6f 64 0a 2a 2a 20 63 68 65 63 6b method.** check
d510: 73 20 77 68 65 74 68 65 72 20 74 68 65 20 66 69 s whether the fi
d520: 6c 65 20 69 73 20 62 6f 74 68 20 72 65 61 64 61 le is both reada
d530: 62 6c 65 20 61 6e 64 20 77 72 69 74 61 62 6c 65 ble and writable
d540: 2e 0a 2a 2a 20 57 69 74 68 20 53 51 4c 49 54 45 ..** With SQLITE
d550: 5f 41 43 43 45 53 53 5f 52 45 41 44 2c 20 74 68 _ACCESS_READ, th
d560: 65 20 78 41 63 63 65 73 73 20 6d 65 74 68 6f 64 e xAccess method
d570: 0a 2a 2a 20 63 68 65 63 6b 73 20 77 68 65 74 68 .** checks wheth
d580: 65 72 20 74 68 65 20 66 69 6c 65 20 69 73 20 72 er the file is r
d590: 65 61 64 61 62 6c 65 2e 0a 2a 2f 0a 23 64 65 66 eadable..*/.#def
d5a0: 69 6e 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 ine SQLITE_ACCES
d5b0: 53 5f 45 58 49 53 54 53 20 20 20 20 30 0a 23 64 S_EXISTS 0.#d
d5c0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 43 43 efine SQLITE_ACC
d5d0: 45 53 53 5f 52 45 41 44 57 52 49 54 45 20 31 0a ESS_READWRITE 1.
d5e0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 #define SQLITE_A
d5f0: 43 43 45 53 53 5f 52 45 41 44 20 20 20 20 20 20 CCESS_READ
d600: 32 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 2../*.** CAPI3RE
d610: 46 3a 20 49 6e 69 74 69 61 6c 69 7a 65 20 54 68 F: Initialize Th
d620: 65 20 53 51 4c 69 74 65 20 4c 69 62 72 61 72 79 e SQLite Library
d630: 20 7b 48 31 30 31 33 30 7d 20 3c 53 32 30 30 30 {H10130} <S2000
d640: 30 3e 3c 53 33 30 31 30 30 3e 0a 2a 2a 0a 2a 2a 0><S30100>.**.**
d650: 20 54 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 69 The sqlite3_ini
d660: 74 69 61 6c 69 7a 65 28 29 20 72 6f 75 74 69 6e tialize() routin
d670: 65 20 69 6e 69 74 69 61 6c 69 7a 65 73 20 74 68 e initializes th
d680: 65 0a 2a 2a 20 53 51 4c 69 74 65 20 6c 69 62 72 e.** SQLite libr
d690: 61 72 79 2e 20 20 54 68 65 20 73 71 6c 69 74 65 ary. The sqlite
d6a0: 33 5f 73 68 75 74 64 6f 77 6e 28 29 20 72 6f 75 3_shutdown() rou
d6b0: 74 69 6e 65 0a 2a 2a 20 64 65 61 6c 6c 6f 63 61 tine.** dealloca
d6c0: 74 65 73 20 61 6e 79 20 72 65 73 6f 75 72 63 65 tes any resource
d6d0: 73 20 74 68 61 74 20 77 65 72 65 20 61 6c 6c 6f s that were allo
d6e0: 63 61 74 65 64 20 62 79 20 73 71 6c 69 74 65 33 cated by sqlite3
d6f0: 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 2e 0a 2a _initialize()..*
d700: 2a 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 6f 20 73 *.** A call to s
d710: 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a qlite3_initializ
d720: 65 28 29 20 69 73 20 61 6e 20 22 65 66 66 65 63 e() is an "effec
d730: 74 69 76 65 22 20 63 61 6c 6c 20 69 66 20 69 74 tive" call if it
d740: 20 69 73 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 is.** the first
d750: 20 74 69 6d 65 20 73 71 6c 69 74 65 33 5f 69 6e time sqlite3_in
d760: 69 74 69 61 6c 69 7a 65 28 29 20 69 73 20 69 6e itialize() is in
d770: 76 6f 6b 65 64 20 64 75 72 69 6e 67 20 74 68 65 voked during the
d780: 20 6c 69 66 65 74 69 6d 65 20 6f 66 0a 2a 2a 20 lifetime of.**
d790: 74 68 65 20 70 72 6f 63 65 73 73 2c 20 6f 72 20 the process, or
d7a0: 69 66 20 69 74 20 69 73 20 74 68 65 20 66 69 72 if it is the fir
d7b0: 73 74 20 74 69 6d 65 20 73 71 6c 69 74 65 33 5f st time sqlite3_
d7c0: 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 69 73 20 initialize() is
d7d0: 69 6e 76 6f 6b 65 64 0a 2a 2a 20 66 6f 6c 6c 6f invoked.** follo
d7e0: 77 69 6e 67 20 61 20 63 61 6c 6c 20 74 6f 20 73 wing a call to s
d7f0: 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 qlite3_shutdown(
d800: 29 2e 20 20 4f 6e 6c 79 20 61 6e 20 65 66 66 65 ). Only an effe
d810: 63 74 69 76 65 20 63 61 6c 6c 0a 2a 2a 20 6f 66 ctive call.** of
d820: 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c sqlite3_initial
d830: 69 7a 65 28 29 20 64 6f 65 73 20 61 6e 79 20 69 ize() does any i
d840: 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 2e 20 20 nitialization.
d850: 41 6c 6c 20 6f 74 68 65 72 20 63 61 6c 6c 73 0a All other calls.
d860: 2a 2a 20 61 72 65 20 68 61 72 6d 6c 65 73 73 20 ** are harmless
d870: 6e 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 no-ops..**.** A
d880: 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f call to sqlite3_
d890: 73 68 75 74 64 6f 77 6e 28 29 20 69 73 20 61 6e shutdown() is an
d8a0: 20 22 65 66 66 65 63 74 69 76 65 22 20 63 61 6c "effective" cal
d8b0: 6c 20 69 66 20 69 74 20 69 73 20 74 68 65 20 66 l if it is the f
d8c0: 69 72 73 74 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 irst.** call to
d8d0: 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e sqlite3_shutdown
d8e0: 28 29 20 73 69 6e 63 65 20 74 68 65 20 6c 61 73 () since the las
d8f0: 74 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 t sqlite3_initia
d900: 6c 69 7a 65 28 29 2e 20 20 4f 6e 6c 79 0a 2a 2a lize(). Only.**
d910: 20 61 6e 20 65 66 66 65 63 74 69 76 65 20 63 61 an effective ca
d920: 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 73 68 ll to sqlite3_sh
d930: 75 74 64 6f 77 6e 28 29 20 64 6f 65 73 20 61 6e utdown() does an
d940: 79 20 64 65 69 6e 69 74 69 61 6c 69 7a 61 74 69 y deinitializati
d950: 6f 6e 2e 0a 2a 2a 20 41 6c 6c 20 6f 74 68 65 72 on..** All other
d960: 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 calls to sqlite
d970: 33 5f 73 68 75 74 64 6f 77 6e 28 29 20 61 72 65 3_shutdown() are
d980: 20 68 61 72 6d 6c 65 73 73 20 6e 6f 2d 6f 70 73 harmless no-ops
d990: 2e 0a 2a 2a 0a 2a 2a 20 41 6d 6f 6e 67 20 6f 74 ..**.** Among ot
d9a0: 68 65 72 20 74 68 69 6e 67 73 2c 20 73 71 6c 69 her things, sqli
d9b0: 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 te3_initialize()
d9c0: 20 73 68 61 6c 6c 20 69 6e 76 6f 6b 65 0a 2a 2a shall invoke.**
d9d0: 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 sqlite3_os_init
d9e0: 28 29 2e 20 20 53 69 6d 69 6c 61 72 6c 79 2c 20 (). Similarly,
d9f0: 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e sqlite3_shutdown
da00: 28 29 0a 2a 2a 20 73 68 61 6c 6c 20 69 6e 76 6f ().** shall invo
da10: 6b 65 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e ke sqlite3_os_en
da20: 64 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 d()..**.** The s
da30: 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a qlite3_initializ
da40: 65 28 29 20 72 6f 75 74 69 6e 65 20 72 65 74 75 e() routine retu
da50: 72 6e 73 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 rns [SQLITE_OK]
da60: 6f 6e 20 73 75 63 63 65 73 73 2e 0a 2a 2a 20 49 on success..** I
da70: 66 20 66 6f 72 20 73 6f 6d 65 20 72 65 61 73 6f f for some reaso
da80: 6e 2c 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 n, sqlite3_initi
da90: 61 6c 69 7a 65 28 29 20 69 73 20 75 6e 61 62 6c alize() is unabl
daa0: 65 20 74 6f 20 69 6e 69 74 69 61 6c 69 7a 65 0a e to initialize.
dab0: 2a 2a 20 74 68 65 20 6c 69 62 72 61 72 79 20 28 ** the library (
dac0: 70 65 72 68 61 70 73 20 69 74 20 69 73 20 75 6e perhaps it is un
dad0: 61 62 6c 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65 able to allocate
dae0: 20 61 20 6e 65 65 64 65 64 20 72 65 73 6f 75 72 a needed resour
daf0: 63 65 20 73 75 63 68 0a 2a 2a 20 61 73 20 61 20 ce such.** as a
db00: 6d 75 74 65 78 29 20 69 74 20 72 65 74 75 72 6e mutex) it return
db10: 73 20 61 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65 s an [error code
db20: 5d 20 6f 74 68 65 72 20 74 68 61 6e 20 5b 53 51 ] other than [SQ
db30: 4c 49 54 45 5f 4f 4b 5d 2e 0a 2a 2a 0a 2a 2a 20 LITE_OK]..**.**
db40: 54 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 The sqlite3_init
db50: 69 61 6c 69 7a 65 28 29 20 72 6f 75 74 69 6e 65 ialize() routine
db60: 20 69 73 20 63 61 6c 6c 65 64 20 69 6e 74 65 72 is called inter
db70: 6e 61 6c 6c 79 20 62 79 20 6d 61 6e 79 20 6f 74 nally by many ot
db80: 68 65 72 0a 2a 2a 20 53 51 4c 69 74 65 20 69 6e her.** SQLite in
db90: 74 65 72 66 61 63 65 73 20 73 6f 20 74 68 61 74 terfaces so that
dba0: 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 an application
dbb0: 75 73 75 61 6c 6c 79 20 64 6f 65 73 20 6e 6f 74 usually does not
dbc0: 20 6e 65 65 64 20 74 6f 0a 2a 2a 20 69 6e 76 6f need to.** invo
dbd0: 6b 65 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 ke sqlite3_initi
dbe0: 61 6c 69 7a 65 28 29 20 64 69 72 65 63 74 6c 79 alize() directly
dbf0: 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 . For example,
dc00: 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d [sqlite3_open()]
dc10: 0a 2a 2a 20 63 61 6c 6c 73 20 73 71 6c 69 74 65 .** calls sqlite
dc20: 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 73 3_initialize() s
dc30: 6f 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 o the SQLite lib
dc40: 72 61 72 79 20 77 69 6c 6c 20 62 65 20 61 75 74 rary will be aut
dc50: 6f 6d 61 74 69 63 61 6c 6c 79 0a 2a 2a 20 69 6e omatically.** in
dc60: 69 74 69 61 6c 69 7a 65 64 20 77 68 65 6e 20 5b itialized when [
dc70: 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 20 sqlite3_open()]
dc80: 69 73 20 63 61 6c 6c 65 64 20 69 66 20 69 74 20 is called if it
dc90: 68 61 73 20 6e 6f 74 20 62 65 20 69 6e 69 74 69 has not be initi
dca0: 61 6c 69 7a 65 64 0a 2a 2a 20 61 6c 72 65 61 64 alized.** alread
dcb0: 79 2e 20 20 48 6f 77 65 76 65 72 2c 20 69 66 20 y. However, if
dcc0: 53 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c SQLite is compil
dcd0: 65 64 20 77 69 74 68 20 74 68 65 20 5b 53 51 4c ed with the [SQL
dce0: 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 ITE_OMIT_AUTOINI
dcf0: 54 5d 0a 2a 2a 20 63 6f 6d 70 69 6c 65 2d 74 69 T].** compile-ti
dd00: 6d 65 20 6f 70 74 69 6f 6e 2c 20 74 68 65 6e 20 me option, then
dd10: 74 68 65 20 61 75 74 6f 6d 61 74 69 63 20 63 61 the automatic ca
dd20: 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 5f 69 lls to sqlite3_i
dd30: 6e 69 74 69 61 6c 69 7a 65 28 29 0a 2a 2a 20 61 nitialize().** a
dd40: 72 65 20 6f 6d 69 74 74 65 64 20 61 6e 64 20 74 re omitted and t
dd50: 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d he application m
dd60: 75 73 74 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 ust call sqlite3
dd70: 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 64 69 _initialize() di
dd80: 72 65 63 74 6c 79 0a 2a 2a 20 70 72 69 6f 72 20 rectly.** prior
dd90: 74 6f 20 75 73 69 6e 67 20 61 6e 79 20 6f 74 68 to using any oth
dda0: 65 72 20 53 51 4c 69 74 65 20 69 6e 74 65 72 66 er SQLite interf
ddb0: 61 63 65 2e 20 20 46 6f 72 20 6d 61 78 69 6d 75 ace. For maximu
ddc0: 6d 20 70 6f 72 74 61 62 69 6c 69 74 79 2c 0a 2a m portability,.*
ddd0: 2a 20 69 74 20 69 73 20 72 65 63 6f 6d 6d 65 6e * it is recommen
dde0: 64 65 64 20 74 68 61 74 20 61 70 70 6c 69 63 61 ded that applica
ddf0: 74 69 6f 6e 73 20 61 6c 77 61 79 73 20 69 6e 76 tions always inv
de00: 6f 6b 65 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 oke sqlite3_init
de10: 69 61 6c 69 7a 65 28 29 0a 2a 2a 20 64 69 72 65 ialize().** dire
de20: 63 74 6c 79 20 70 72 69 6f 72 20 74 6f 20 75 73 ctly prior to us
de30: 69 6e 67 20 61 6e 79 20 6f 74 68 65 72 20 53 51 ing any other SQ
de40: 4c 69 74 65 20 69 6e 74 65 72 66 61 63 65 2e 20 Lite interface.
de50: 20 46 75 74 75 72 65 20 72 65 6c 65 61 73 65 73 Future releases
de60: 0a 2a 2a 20 6f 66 20 53 51 4c 69 74 65 20 6d 61 .** of SQLite ma
de70: 79 20 72 65 71 75 69 72 65 20 74 68 69 73 2e 20 y require this.
de80: 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c In other words,
de90: 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 65 78 the behavior ex
dea0: 68 69 62 69 74 65 64 0a 2a 2a 20 77 68 65 6e 20 hibited.** when
deb0: 53 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c SQLite is compil
dec0: 65 64 20 77 69 74 68 20 5b 53 51 4c 49 54 45 5f ed with [SQLITE_
ded0: 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 5d 20 6d OMIT_AUTOINIT] m
dee0: 69 67 68 74 20 62 65 63 6f 6d 65 20 74 68 65 0a ight become the.
def0: 2a 2a 20 64 65 66 61 75 6c 74 20 62 65 68 61 76 ** default behav
df00: 69 6f 72 20 69 6e 20 73 6f 6d 65 20 66 75 74 75 ior in some futu
df10: 72 65 20 72 65 6c 65 61 73 65 20 6f 66 20 53 51 re release of SQ
df20: 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 Lite..**.** The
df30: 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 sqlite3_os_init(
df40: 29 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 6f ) routine does o
df50: 70 65 72 61 74 69 6e 67 2d 73 79 73 74 65 6d 20 perating-system
df60: 73 70 65 63 69 66 69 63 0a 2a 2a 20 69 6e 69 74 specific.** init
df70: 69 61 6c 69 7a 61 74 69 6f 6e 20 6f 66 20 74 68 ialization of th
df80: 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 e SQLite library
df90: 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6f . The sqlite3_o
dfa0: 73 5f 65 6e 64 28 29 0a 2a 2a 20 72 6f 75 74 69 s_end().** routi
dfb0: 6e 65 20 75 6e 64 6f 65 73 20 74 68 65 20 65 66 ne undoes the ef
dfc0: 66 65 63 74 20 6f 66 20 73 71 6c 69 74 65 33 5f fect of sqlite3_
dfd0: 6f 73 5f 69 6e 69 74 28 29 2e 20 20 54 79 70 69 os_init(). Typi
dfe0: 63 61 6c 20 74 61 73 6b 73 0a 2a 2a 20 70 65 72 cal tasks.** per
dff0: 66 6f 72 6d 65 64 20 62 79 20 74 68 65 73 65 20 formed by these
e000: 72 6f 75 74 69 6e 65 73 20 69 6e 63 6c 75 64 65 routines include
e010: 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 72 20 64 allocation or d
e020: 65 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 6f eallocation.** o
e030: 66 20 73 74 61 74 69 63 20 72 65 73 6f 75 72 63 f static resourc
e040: 65 73 2c 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 es, initializati
e050: 6f 6e 20 6f 66 20 67 6c 6f 62 61 6c 20 76 61 72 on of global var
e060: 69 61 62 6c 65 73 2c 0a 2a 2a 20 73 65 74 74 69 iables,.** setti
e070: 6e 67 20 75 70 20 61 20 64 65 66 61 75 6c 74 20 ng up a default
e080: 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 6d 6f [sqlite3_vfs] mo
e090: 64 75 6c 65 2c 20 6f 72 20 73 65 74 74 69 6e 67 dule, or setting
e0a0: 20 75 70 0a 2a 2a 20 61 20 64 65 66 61 75 6c 74 up.** a default
e0b0: 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 75 configuration u
e0c0: 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 63 6f sing [sqlite3_co
e0d0: 6e 66 69 67 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 nfig()]..**.** T
e0e0: 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 73 he application s
e0f0: 68 6f 75 6c 64 20 6e 65 76 65 72 20 69 6e 76 6f hould never invo
e100: 6b 65 20 65 69 74 68 65 72 20 73 71 6c 69 74 65 ke either sqlite
e110: 33 5f 6f 73 5f 69 6e 69 74 28 29 0a 2a 2a 20 6f 3_os_init().** o
e120: 72 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 r sqlite3_os_end
e130: 28 29 20 64 69 72 65 63 74 6c 79 2e 20 20 54 68 () directly. Th
e140: 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 73 68 e application sh
e150: 6f 75 6c 64 20 6f 6e 6c 79 20 69 6e 76 6f 6b 65 ould only invoke
e160: 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 .** sqlite3_init
e170: 69 61 6c 69 7a 65 28 29 20 61 6e 64 20 73 71 6c ialize() and sql
e180: 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 2e ite3_shutdown().
e190: 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6f 73 The sqlite3_os
e1a0: 5f 69 6e 69 74 28 29 0a 2a 2a 20 69 6e 74 65 72 _init().** inter
e1b0: 66 61 63 65 20 69 73 20 63 61 6c 6c 65 64 20 61 face is called a
e1c0: 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 62 79 20 utomatically by
e1d0: 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 sqlite3_initiali
e1e0: 7a 65 28 29 20 61 6e 64 0a 2a 2a 20 73 71 6c 69 ze() and.** sqli
e1f0: 74 65 33 5f 6f 73 5f 65 6e 64 28 29 20 69 73 20 te3_os_end() is
e200: 63 61 6c 6c 65 64 20 62 79 20 73 71 6c 69 74 65 called by sqlite
e210: 33 5f 73 68 75 74 64 6f 77 6e 28 29 2e 20 20 41 3_shutdown(). A
e220: 70 70 72 6f 70 72 69 61 74 65 0a 2a 2a 20 69 6d ppropriate.** im
e230: 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 66 6f plementations fo
e240: 72 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 r sqlite3_os_ini
e250: 74 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f t() and sqlite3_
e260: 6f 73 5f 65 6e 64 28 29 0a 2a 2a 20 61 72 65 20 os_end().** are
e270: 62 75 69 6c 74 20 69 6e 74 6f 20 53 51 4c 69 74 built into SQLit
e280: 65 20 77 68 65 6e 20 69 74 20 69 73 20 63 6f 6d e when it is com
e290: 70 69 6c 65 64 20 66 6f 72 20 55 6e 69 78 2c 20 piled for Unix,
e2a0: 57 69 6e 64 6f 77 73 2c 20 6f 72 20 4f 53 2f 32 Windows, or OS/2
e2b0: 2e 0a 2a 2a 20 57 68 65 6e 20 5b 63 75 73 74 6f ..** When [custo
e2c0: 6d 20 62 75 69 6c 64 73 20 7c 20 62 75 69 6c 74 m builds | built
e2d0: 20 66 6f 72 20 6f 74 68 65 72 20 70 6c 61 74 66 for other platf
e2e0: 6f 72 6d 73 5d 0a 2a 2a 20 28 75 73 69 6e 67 20 orms].** (using
e2f0: 74 68 65 20 5b 53 51 4c 49 54 45 5f 4f 53 5f 4f the [SQLITE_OS_O
e300: 54 48 45 52 3d 31 5d 20 63 6f 6d 70 69 6c 65 2d THER=1] compile-
e310: 74 69 6d 65 0a 2a 2a 20 6f 70 74 69 6f 6e 29 20 time.** option)
e320: 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 the application
e330: 6d 75 73 74 20 73 75 70 70 6c 79 20 61 20 73 75 must supply a su
e340: 69 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 itable implement
e350: 61 74 69 6f 6e 20 66 6f 72 0a 2a 2a 20 73 71 6c ation for.** sql
e360: 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 20 61 ite3_os_init() a
e370: 6e 64 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e nd sqlite3_os_en
e380: 64 28 29 2e 20 20 41 6e 20 61 70 70 6c 69 63 61 d(). An applica
e390: 74 69 6f 6e 2d 73 75 70 70 6c 69 65 64 0a 2a 2a tion-supplied.**
e3a0: 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
e3b0: 6f 66 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e of sqlite3_os_in
e3c0: 69 74 28 29 20 6f 72 20 73 71 6c 69 74 65 33 5f it() or sqlite3_
e3d0: 6f 73 5f 65 6e 64 28 29 0a 2a 2a 20 6d 75 73 74 os_end().** must
e3e0: 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f return [SQLITE_
e3f0: 4f 4b 5d 20 6f 6e 20 73 75 63 63 65 73 73 20 61 OK] on success a
e400: 6e 64 20 73 6f 6d 65 20 6f 74 68 65 72 20 5b 65 nd some other [e
e410: 72 72 6f 72 20 63 6f 64 65 5d 20 75 70 6f 6e 0a rror code] upon.
e420: 2a 2a 20 66 61 69 6c 75 72 65 2e 0a 2a 2f 0a 53 ** failure..*/.S
e430: 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
e440: 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 lite3_initialize
e450: 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 41 (void);.SQLITE_A
e460: 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 PI int sqlite3_s
e470: 68 75 74 64 6f 77 6e 28 76 6f 69 64 29 3b 0a 53 hutdown(void);.S
e480: 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
e490: 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 76 6f lite3_os_init(vo
e4a0: 69 64 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 id);.SQLITE_API
e4b0: 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 int sqlite3_os_e
e4c0: 6e 64 28 76 6f 69 64 29 3b 0a 0a 2f 2a 0a 2a 2a nd(void);../*.**
e4d0: 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6e 66 69 CAPI3REF: Confi
e4e0: 67 75 72 69 6e 67 20 54 68 65 20 53 51 4c 69 74 guring The SQLit
e4f0: 65 20 4c 69 62 72 61 72 79 20 7b 48 31 34 31 30 e Library {H1410
e500: 30 7d 20 3c 53 32 30 30 30 30 3e 3c 53 33 30 32 0} <S20000><S302
e510: 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 00>.** EXPERIMEN
e520: 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 TAL.**.** The sq
e530: 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 69 lite3_config() i
e540: 6e 74 65 72 66 61 63 65 20 69 73 20 75 73 65 64 nterface is used
e550: 20 74 6f 20 6d 61 6b 65 20 67 6c 6f 62 61 6c 20 to make global
e560: 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 0a 2a 2a configuration.**
e570: 20 63 68 61 6e 67 65 73 20 74 6f 20 53 51 4c 69 changes to SQLi
e580: 74 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 74 te in order to t
e590: 75 6e 65 20 53 51 4c 69 74 65 20 74 6f 20 74 68 une SQLite to th
e5a0: 65 20 73 70 65 63 69 66 69 63 20 6e 65 65 64 73 e specific needs
e5b0: 20 6f 66 0a 2a 2a 20 74 68 65 20 61 70 70 6c 69 of.** the appli
e5c0: 63 61 74 69 6f 6e 2e 20 20 54 68 65 20 64 65 66 cation. The def
e5d0: 61 75 6c 74 20 63 6f 6e 66 69 67 75 72 61 74 69 ault configurati
e5e0: 6f 6e 20 69 73 20 72 65 63 6f 6d 6d 65 6e 64 65 on is recommende
e5f0: 64 20 66 6f 72 20 6d 6f 73 74 0a 2a 2a 20 61 70 d for most.** ap
e600: 70 6c 69 63 61 74 69 6f 6e 73 20 61 6e 64 20 73 plications and s
e610: 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 o this routine i
e620: 73 20 75 73 75 61 6c 6c 79 20 6e 6f 74 20 6e 65 s usually not ne
e630: 63 65 73 73 61 72 79 2e 20 20 49 74 20 69 73 0a cessary. It is.
e640: 2a 2a 20 70 72 6f 76 69 64 65 64 20 74 6f 20 73 ** provided to s
e650: 75 70 70 6f 72 74 20 72 61 72 65 20 61 70 70 6c upport rare appl
e660: 69 63 61 74 69 6f 6e 73 20 77 69 74 68 20 75 6e ications with un
e670: 75 73 75 61 6c 20 6e 65 65 64 73 2e 0a 2a 2a 0a usual needs..**.
e680: 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 63 ** The sqlite3_c
e690: 6f 6e 66 69 67 28 29 20 69 6e 74 65 72 66 61 63 onfig() interfac
e6a0: 65 20 69 73 20 6e 6f 74 20 74 68 72 65 61 64 73 e is not threads
e6b0: 61 66 65 2e 20 20 54 68 65 20 61 70 70 6c 69 63 afe. The applic
e6c0: 61 74 69 6f 6e 0a 2a 2a 20 6d 75 73 74 20 69 6e ation.** must in
e6d0: 73 75 72 65 20 74 68 61 74 20 6e 6f 20 6f 74 68 sure that no oth
e6e0: 65 72 20 53 51 4c 69 74 65 20 69 6e 74 65 72 66 er SQLite interf
e6f0: 61 63 65 73 20 61 72 65 20 69 6e 76 6f 6b 65 64 aces are invoked
e700: 20 62 79 20 6f 74 68 65 72 0a 2a 2a 20 74 68 72 by other.** thr
e710: 65 61 64 73 20 77 68 69 6c 65 20 73 71 6c 69 74 eads while sqlit
e720: 65 33 5f 63 6f 6e 66 69 67 28 29 20 69 73 20 72 e3_config() is r
e730: 75 6e 6e 69 6e 67 2e 20 20 46 75 72 74 68 65 72 unning. Further
e740: 6d 6f 72 65 2c 20 73 71 6c 69 74 65 33 5f 63 6f more, sqlite3_co
e750: 6e 66 69 67 28 29 0a 2a 2a 20 6d 61 79 20 6f 6e nfig().** may on
e760: 6c 79 20 62 65 20 69 6e 76 6f 6b 65 64 20 70 72 ly be invoked pr
e770: 69 6f 72 20 74 6f 20 6c 69 62 72 61 72 79 20 69 ior to library i
e780: 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 75 73 nitialization us
e790: 69 6e 67 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f ing.** [sqlite3_
e7a0: 69 6e 69 74 69 61 6c 69 7a 65 28 29 5d 20 6f 72 initialize()] or
e7b0: 20 61 66 74 65 72 20 73 68 75 74 64 6f 77 6e 20 after shutdown
e7c0: 62 79 20 5b 73 71 6c 69 74 65 33 5f 73 68 75 74 by [sqlite3_shut
e7d0: 64 6f 77 6e 28 29 5d 2e 0a 2a 2a 20 4e 6f 74 65 down()]..** Note
e7e0: 2c 20 68 6f 77 65 76 65 72 2c 20 74 68 61 74 20 , however, that
e7f0: 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 sqlite3_config()
e800: 20 63 61 6e 20 62 65 20 63 61 6c 6c 65 64 20 61 can be called a
e810: 73 20 70 61 72 74 20 6f 66 20 74 68 65 0a 2a 2a s part of the.**
e820: 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
e830: 6f 66 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f of an applicatio
e840: 6e 2d 64 65 66 69 6e 65 64 20 5b 73 71 6c 69 74 n-defined [sqlit
e850: 65 33 5f 6f 73 5f 69 6e 69 74 28 29 5d 2e 0a 2a e3_os_init()]..*
e860: 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61 *.** The first a
e870: 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 rgument to sqlit
e880: 65 33 5f 63 6f 6e 66 69 67 28 29 20 69 73 20 61 e3_config() is a
e890: 6e 20 69 6e 74 65 67 65 72 0a 2a 2a 20 5b 53 51 n integer.** [SQ
e8a0: 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 49 4e 47 LITE_CONFIG_SING
e8b0: 4c 45 54 48 52 45 41 44 20 7c 20 63 6f 6e 66 69 LETHREAD | confi
e8c0: 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 5d guration option]
e8d0: 20 74 68 61 74 20 64 65 74 65 72 6d 69 6e 65 73 that determines
e8e0: 0a 2a 2a 20 77 68 61 74 20 70 72 6f 70 65 72 74 .** what propert
e8f0: 79 20 6f 66 20 53 51 4c 69 74 65 20 69 73 20 74 y of SQLite is t
e900: 6f 20 62 65 20 63 6f 6e 66 69 67 75 72 65 64 2e o be configured.
e910: 20 20 53 75 62 73 65 71 75 65 6e 74 20 61 72 67 Subsequent arg
e920: 75 6d 65 6e 74 73 0a 2a 2a 20 76 61 72 79 20 64 uments.** vary d
e930: 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 epending on the
e940: 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 [SQLITE_CONFIG_S
e950: 49 4e 47 4c 45 54 48 52 45 41 44 20 7c 20 63 6f INGLETHREAD | co
e960: 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 nfiguration opti
e970: 6f 6e 5d 0a 2a 2a 20 69 6e 20 74 68 65 20 66 69 on].** in the fi
e980: 72 73 74 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a rst argument..**
e990: 0a 2a 2a 20 57 68 65 6e 20 61 20 63 6f 6e 66 69 .** When a confi
e9a0: 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 20 guration option
e9b0: 69 73 20 73 65 74 2c 20 73 71 6c 69 74 65 33 5f is set, sqlite3_
e9c0: 63 6f 6e 66 69 67 28 29 20 72 65 74 75 72 6e 73 config() returns
e9d0: 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 2e 0a 2a 2a [SQLITE_OK]..**
e9e0: 20 49 66 20 74 68 65 20 6f 70 74 69 6f 6e 20 69 If the option i
e9f0: 73 20 75 6e 6b 6e 6f 77 6e 20 6f 72 20 53 51 4c s unknown or SQL
ea00: 69 74 65 20 69 73 20 75 6e 61 62 6c 65 20 74 6f ite is unable to
ea10: 20 73 65 74 20 74 68 65 20 6f 70 74 69 6f 6e 0a set the option.
ea20: 2a 2a 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 ** then this rou
ea30: 74 69 6e 65 20 72 65 74 75 72 6e 73 20 61 20 6e tine returns a n
ea40: 6f 6e 2d 7a 65 72 6f 20 5b 65 72 72 6f 72 20 63 on-zero [error c
ea50: 6f 64 65 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 ode]..**.** Requ
ea60: 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 irements:.** [H1
ea70: 34 31 30 33 5d 20 5b 48 31 34 31 30 36 5d 20 5b 4103] [H14106] [
ea80: 48 31 34 31 32 30 5d 20 5b 48 31 34 31 32 33 5d H14120] [H14123]
ea90: 20 5b 48 31 34 31 32 36 5d 20 5b 48 31 34 31 32 [H14126] [H1412
eaa0: 39 5d 20 5b 48 31 34 31 33 32 5d 20 5b 48 31 34 9] [H14132] [H14
eab0: 31 33 35 5d 0a 2a 2a 20 5b 48 31 34 31 33 38 5d 135].** [H14138]
eac0: 20 5b 48 31 34 31 34 31 5d 20 5b 48 31 34 31 34 [H14141] [H1414
ead0: 34 5d 20 5b 48 31 34 31 34 37 5d 20 5b 48 31 34 4] [H14147] [H14
eae0: 31 35 30 5d 20 5b 48 31 34 31 35 33 5d 20 5b 48 150] [H14153] [H
eaf0: 31 34 31 35 36 5d 20 5b 48 31 34 31 35 39 5d 0a 14156] [H14159].
eb00: 2a 2a 20 5b 48 31 34 31 36 32 5d 20 5b 48 31 34 ** [H14162] [H14
eb10: 31 36 35 5d 20 5b 48 31 34 31 36 38 5d 0a 2a 2f 165] [H14168].*/
eb20: 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 .SQLITE_API SQLI
eb30: 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 TE_EXPERIMENTAL
eb40: 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 int sqlite3_conf
eb50: 69 67 28 69 6e 74 2c 20 2e 2e 2e 29 3b 0a 0a 2f ig(int, ...);../
eb60: 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 *.** CAPI3REF: C
eb70: 6f 6e 66 69 67 75 72 65 20 64 61 74 61 62 61 73 onfigure databas
eb80: 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 20 7b e connections {
eb90: 48 31 34 32 30 30 7d 20 3c 53 32 30 30 30 30 3e H14200} <S20000>
eba0: 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c .** EXPERIMENTAL
ebb0: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 .**.** The sqlit
ebc0: 65 33 5f 64 62 5f 63 6f 6e 66 69 67 28 29 20 69 e3_db_config() i
ebd0: 6e 74 65 72 66 61 63 65 20 69 73 20 75 73 65 64 nterface is used
ebe0: 20 74 6f 20 6d 61 6b 65 20 63 6f 6e 66 69 67 75 to make configu
ebf0: 72 61 74 69 6f 6e 0a 2a 2a 20 63 68 61 6e 67 65 ration.** change
ec00: 73 20 74 6f 20 61 20 5b 64 61 74 61 62 61 73 65 s to a [database
ec10: 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e 20 20 54 connection]. T
ec20: 68 65 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 he interface is
ec30: 73 69 6d 69 6c 61 72 20 74 6f 0a 2a 2a 20 5b 73 similar to.** [s
ec40: 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d qlite3_config()]
ec50: 20 65 78 63 65 70 74 20 74 68 61 74 20 74 68 65 except that the
ec60: 20 63 68 61 6e 67 65 73 20 61 70 70 6c 79 20 74 changes apply t
ec70: 6f 20 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 5b 64 o a single.** [d
ec80: 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
ec90: 6f 6e 5d 20 28 73 70 65 63 69 66 69 65 64 20 69 on] (specified i
eca0: 6e 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 n the first argu
ecb0: 6d 65 6e 74 29 2e 20 20 54 68 65 0a 2a 2a 20 73 ment). The.** s
ecc0: 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67 qlite3_db_config
ecd0: 28 29 20 69 6e 74 65 72 66 61 63 65 20 63 61 6e () interface can
ece0: 20 6f 6e 6c 79 20 62 65 20 75 73 65 64 20 69 6d only be used im
ecf0: 6d 65 64 69 61 74 65 6c 79 20 61 66 74 65 72 0a mediately after.
ed00: 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 ** the database
ed10: 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 63 72 connection is cr
ed20: 65 61 74 65 64 20 75 73 69 6e 67 20 5b 73 71 6c eated using [sql
ed30: 69 74 65 33 5f 6f 70 65 6e 28 29 5d 2c 0a 2a 2a ite3_open()],.**
ed40: 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 [sqlite3_open16
ed50: 28 29 5d 2c 20 6f 72 20 5b 73 71 6c 69 74 65 33 ()], or [sqlite3
ed60: 5f 6f 70 65 6e 5f 76 32 28 29 5d 2e 20 20 0a 2a _open_v2()]. .*
ed70: 2a 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 *.** The second
ed80: 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 argument to sqli
ed90: 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67 28 44 2c te3_db_config(D,
eda0: 56 2c 2e 2e 2e 29 20 20 69 73 20 74 68 65 0a 2a V,...) is the.*
edb0: 2a 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 * configuration
edc0: 76 65 72 62 20 2d 20 61 6e 20 69 6e 74 65 67 65 verb - an intege
edd0: 72 20 63 6f 64 65 20 74 68 61 74 20 69 6e 64 69 r code that indi
ede0: 63 61 74 65 73 20 77 68 61 74 0a 2a 2a 20 61 73 cates what.** as
edf0: 70 65 63 74 20 6f 66 20 74 68 65 20 5b 64 61 74 pect of the [dat
ee00: 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
ee10: 5d 20 69 73 20 62 65 69 6e 67 20 63 6f 6e 66 69 ] is being confi
ee20: 67 75 72 65 64 2e 0a 2a 2a 20 54 68 65 20 6f 6e gured..** The on
ee30: 6c 79 20 63 68 6f 69 63 65 20 66 6f 72 20 74 68 ly choice for th
ee40: 69 73 20 76 61 6c 75 65 20 69 73 20 5b 53 51 4c is value is [SQL
ee50: 49 54 45 5f 44 42 43 4f 4e 46 49 47 5f 4c 4f 4f ITE_DBCONFIG_LOO
ee60: 4b 41 53 49 44 45 5d 2e 0a 2a 2a 20 4e 65 77 20 KASIDE]..** New
ee70: 76 65 72 62 73 20 61 72 65 20 6c 69 6b 65 6c 79 verbs are likely
ee80: 20 74 6f 20 62 65 20 61 64 64 65 64 20 69 6e 20 to be added in
ee90: 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 73 20 future releases
eea0: 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2a 20 41 64 of SQLite..** Ad
eeb0: 64 69 74 69 6f 6e 61 6c 20 61 72 67 75 6d 65 6e ditional argumen
eec0: 74 73 20 64 65 70 65 6e 64 20 6f 6e 20 74 68 65 ts depend on the
eed0: 20 76 65 72 62 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 verb..**.** Req
eee0: 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 uirements:.** [H
eef0: 31 34 32 30 33 5d 20 5b 48 31 34 32 30 36 5d 20 14203] [H14206]
ef00: 5b 48 31 34 32 30 39 5d 20 5b 48 31 34 32 31 32 [H14209] [H14212
ef10: 5d 20 5b 48 31 34 32 31 35 5d 0a 2a 2f 0a 53 51 ] [H14215].*/.SQ
ef20: 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f LITE_API SQLITE_
ef30: 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 69 6e 74 EXPERIMENTAL int
ef40: 20 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e 66 sqlite3_db_conf
ef50: 69 67 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 ig(sqlite3*, int
ef60: 20 6f 70 2c 20 2e 2e 2e 29 3b 0a 0a 2f 2a 0a 2a op, ...);../*.*
ef70: 2a 20 43 41 50 49 33 52 45 46 3a 20 4d 65 6d 6f * CAPI3REF: Memo
ef80: 72 79 20 41 6c 6c 6f 63 61 74 69 6f 6e 20 52 6f ry Allocation Ro
ef90: 75 74 69 6e 65 73 20 7b 48 31 30 31 35 35 7d 20 utines {H10155}
efa0: 3c 53 32 30 31 32 30 3e 0a 2a 2a 20 45 58 50 45 <S20120>.** EXPE
efb0: 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 41 RIMENTAL.**.** A
efc0: 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 n instance of th
efd0: 69 73 20 6f 62 6a 65 63 74 20 64 65 66 69 6e 65 is object define
efe0: 73 20 74 68 65 20 69 6e 74 65 72 66 61 63 65 20 s the interface
eff0: 62 65 74 77 65 65 6e 20 53 51 4c 69 74 65 0a 2a between SQLite.*
f000: 2a 20 61 6e 64 20 6c 6f 77 2d 6c 65 76 65 6c 20 * and low-level
f010: 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
f020: 6e 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 0a 2a n routines..**.*
f030: 2a 20 54 68 69 73 20 6f 62 6a 65 63 74 20 69 73 * This object is
f040: 20 75 73 65 64 20 69 6e 20 6f 6e 6c 79 20 6f 6e used in only on
f050: 65 20 70 6c 61 63 65 20 69 6e 20 74 68 65 20 53 e place in the S
f060: 51 4c 69 74 65 20 69 6e 74 65 72 66 61 63 65 2e QLite interface.
f070: 0a 2a 2a 20 41 20 70 6f 69 6e 74 65 72 20 74 6f .** A pointer to
f080: 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 an instance of
f090: 74 68 69 73 20 6f 62 6a 65 63 74 20 69 73 20 74 this object is t
f0a0: 68 65 20 61 72 67 75 6d 65 6e 74 20 74 6f 0a 2a he argument to.*
f0b0: 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 * [sqlite3_confi
f0c0: 67 28 29 5d 20 77 68 65 6e 20 74 68 65 20 63 6f g()] when the co
f0d0: 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 nfiguration opti
f0e0: 6f 6e 20 69 73 0a 2a 2a 20 5b 53 51 4c 49 54 45 on is.** [SQLITE
f0f0: 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 5d 20 _CONFIG_MALLOC]
f100: 6f 72 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 or [SQLITE_CONFI
f110: 47 5f 47 45 54 4d 41 4c 4c 4f 43 5d 2e 20 20 0a G_GETMALLOC]. .
f120: 2a 2a 20 42 79 20 63 72 65 61 74 69 6e 67 20 61 ** By creating a
f130: 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 n instance of th
f140: 69 73 20 6f 62 6a 65 63 74 0a 2a 2a 20 61 6e 64 is object.** and
f150: 20 70 61 73 73 69 6e 67 20 69 74 20 74 6f 20 5b passing it to [
f160: 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 5d 28 sqlite3_config](
f170: 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d [SQLITE_CONFIG_M
f180: 41 4c 4c 4f 43 5d 29 0a 2a 2a 20 64 75 72 69 6e ALLOC]).** durin
f190: 67 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 2c g configuration,
f1a0: 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 an application
f1b0: 63 61 6e 20 73 70 65 63 69 66 79 20 61 6e 20 61 can specify an a
f1c0: 6c 74 65 72 6e 61 74 69 76 65 0a 2a 2a 20 6d 65 lternative.** me
f1d0: 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
f1e0: 73 75 62 73 79 73 74 65 6d 20 66 6f 72 20 53 51 subsystem for SQ
f1f0: 4c 69 74 65 20 74 6f 20 75 73 65 20 66 6f 72 20 Lite to use for
f200: 61 6c 6c 20 6f 66 20 69 74 73 0a 2a 2a 20 64 79 all of its.** dy
f210: 6e 61 6d 69 63 20 6d 65 6d 6f 72 79 20 6e 65 65 namic memory nee
f220: 64 73 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 ds..**.** Note t
f230: 68 61 74 20 53 51 4c 69 74 65 20 63 6f 6d 65 73 hat SQLite comes
f240: 20 77 69 74 68 20 73 65 76 65 72 61 6c 20 5b 62 with several [b
f250: 75 69 6c 74 2d 69 6e 20 6d 65 6d 6f 72 79 20 61 uilt-in memory a
f260: 6c 6c 6f 63 61 74 6f 72 73 5d 0a 2a 2a 20 74 68 llocators].** th
f270: 61 74 20 61 72 65 20 70 65 72 66 65 63 74 6c 79 at are perfectly
f280: 20 61 64 65 71 75 61 74 65 20 66 6f 72 20 74 68 adequate for th
f290: 65 20 6f 76 65 72 77 68 65 6c 6d 69 6e 67 20 6d e overwhelming m
f2a0: 61 6a 6f 72 69 74 79 20 6f 66 20 61 70 70 6c 69 ajority of appli
f2b0: 63 61 74 69 6f 6e 73 0a 2a 2a 20 61 6e 64 20 74 cations.** and t
f2c0: 68 61 74 20 74 68 69 73 20 6f 62 6a 65 63 74 20 hat this object
f2d0: 69 73 20 6f 6e 6c 79 20 75 73 65 66 75 6c 20 74 is only useful t
f2e0: 6f 20 61 20 74 69 6e 79 20 6d 69 6e 6f 72 69 74 o a tiny minorit
f2f0: 79 20 6f 66 20 61 70 70 6c 69 63 61 74 69 6f 6e y of application
f300: 73 0a 2a 2a 20 77 69 74 68 20 73 70 65 63 69 61 s.** with specia
f310: 6c 69 7a 65 64 20 6d 65 6d 6f 72 79 20 61 6c 6c lized memory all
f320: 6f 63 61 74 69 6f 6e 20 72 65 71 75 69 72 65 6d ocation requirem
f330: 65 6e 74 73 2e 20 20 54 68 69 73 20 6f 62 6a 65 ents. This obje
f340: 63 74 20 69 73 0a 2a 2a 20 61 6c 73 6f 20 75 73 ct is.** also us
f350: 65 64 20 64 75 72 69 6e 67 20 74 65 73 74 69 6e ed during testin
f360: 67 20 6f 66 20 53 51 4c 69 74 65 20 69 6e 20 6f g of SQLite in o
f370: 72 64 65 72 20 74 6f 20 73 70 65 63 69 66 79 20 rder to specify
f380: 61 6e 20 61 6c 74 65 72 6e 61 74 69 76 65 0a 2a an alternative.*
f390: 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 * memory allocat
f3a0: 6f 72 20 74 68 61 74 20 73 69 6d 75 6c 61 74 65 or that simulate
f3b0: 73 20 6d 65 6d 6f 72 79 20 6f 75 74 2d 6f 66 2d s memory out-of-
f3c0: 6d 65 6d 6f 72 79 20 63 6f 6e 64 69 74 69 6f 6e memory condition
f3d0: 73 20 69 6e 0a 2a 2a 20 6f 72 64 65 72 20 74 6f s in.** order to
f3e0: 20 76 65 72 69 66 79 20 74 68 61 74 20 53 51 4c verify that SQL
f3f0: 69 74 65 20 72 65 63 6f 76 65 72 73 20 67 72 61 ite recovers gra
f400: 63 65 66 75 6c 6c 79 20 66 72 6f 6d 20 73 75 63 cefully from suc
f410: 68 0a 2a 2a 20 63 6f 6e 64 69 74 69 6f 6e 73 2e h.** conditions.
f420: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 4d 61 6c 6c .**.** The xMall
f430: 6f 63 20 61 6e 64 20 78 46 72 65 65 20 6d 65 74 oc and xFree met
f440: 68 6f 64 73 20 6d 75 73 74 20 77 6f 72 6b 20 6c hods must work l
f450: 69 6b 65 20 74 68 65 0a 2a 2a 20 6d 61 6c 6c 6f ike the.** mallo
f460: 63 28 29 20 61 6e 64 20 66 72 65 65 28 29 20 66 c() and free() f
f470: 75 6e 63 74 69 6f 6e 73 20 66 72 6f 6d 20 74 68 unctions from th
f480: 65 20 73 74 61 6e 64 61 72 64 20 43 20 6c 69 62 e standard C lib
f490: 72 61 72 79 2e 0a 2a 2a 20 54 68 65 20 78 52 65 rary..** The xRe
f4a0: 61 6c 6c 6f 63 20 6d 65 74 68 6f 64 20 6d 75 73 alloc method mus
f4b0: 74 20 77 6f 72 6b 20 6c 69 6b 65 20 72 65 61 6c t work like real
f4c0: 6c 6f 63 28 29 20 66 72 6f 6d 20 74 68 65 20 73 loc() from the s
f4d0: 74 61 6e 64 61 72 64 20 43 20 6c 69 62 72 61 72 tandard C librar
f4e0: 79 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 65 78 y.** with the ex
f4f0: 63 65 70 74 69 6f 6e 20 74 68 61 74 20 69 66 20 ception that if
f500: 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d the second argum
f510: 65 6e 74 20 74 6f 20 78 52 65 61 6c 6c 6f 63 20 ent to xRealloc
f520: 69 73 20 7a 65 72 6f 2c 0a 2a 2a 20 78 52 65 61 is zero,.** xRea
f530: 6c 6c 6f 63 20 6d 75 73 74 20 62 65 20 61 20 6e lloc must be a n
f540: 6f 2d 6f 70 20 2d 20 69 74 20 6d 75 73 74 20 6e o-op - it must n
f550: 6f 74 20 70 65 72 66 6f 72 6d 20 61 6e 79 20 61 ot perform any a
f560: 6c 6c 6f 63 61 74 69 6f 6e 20 6f 72 0a 2a 2a 20 llocation or.**
f570: 64 65 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20 53 deallocation. S
f580: 51 4c 69 74 65 20 67 75 61 72 61 6e 74 65 65 64 QLite guaranteed
f590: 73 20 74 68 61 74 20 74 68 65 20 73 65 63 6f 6e s that the secon
f5a0: 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 0a 2a 2a d argument to.**
f5b0: 20 78 52 65 61 6c 6c 6f 63 20 69 73 20 61 6c 77 xRealloc is alw
f5c0: 61 79 73 20 61 20 76 61 6c 75 65 20 72 65 74 75 ays a value retu
f5d0: 72 6e 65 64 20 62 79 20 61 20 70 72 69 6f 72 20 rned by a prior
f5e0: 63 61 6c 6c 20 74 6f 20 78 52 6f 75 6e 64 75 70 call to xRoundup
f5f0: 2e 0a 2a 2a 20 41 6e 64 20 73 6f 20 69 6e 20 63 ..** And so in c
f600: 61 73 65 73 20 77 68 65 72 65 20 78 52 6f 75 6e ases where xRoun
f610: 64 75 70 20 61 6c 77 61 79 73 20 72 65 74 75 72 dup always retur
f620: 6e 73 20 61 20 70 6f 73 69 74 69 76 65 20 6e 75 ns a positive nu
f630: 6d 62 65 72 2c 0a 2a 2a 20 78 52 65 61 6c 6c 6f mber,.** xReallo
f640: 63 20 63 61 6e 20 70 65 72 66 6f 72 6d 20 65 78 c can perform ex
f650: 61 63 74 6c 79 20 61 73 20 74 68 65 20 73 74 61 actly as the sta
f660: 6e 64 61 72 64 20 6c 69 62 72 61 72 79 20 72 65 ndard library re
f670: 61 6c 6c 6f 63 28 29 20 61 6e 64 0a 2a 2a 20 73 alloc() and.** s
f680: 74 69 6c 6c 20 62 65 20 69 6e 20 63 6f 6d 70 6c till be in compl
f690: 69 61 6e 63 65 20 77 69 74 68 20 74 68 69 73 20 iance with this
f6a0: 73 70 65 63 69 66 69 63 61 74 69 6f 6e 2e 0a 2a specification..*
f6b0: 2a 0a 2a 2a 20 78 53 69 7a 65 20 73 68 6f 75 6c *.** xSize shoul
f6c0: 64 20 72 65 74 75 72 6e 20 74 68 65 20 61 6c 6c d return the all
f6d0: 6f 63 61 74 65 64 20 73 69 7a 65 20 6f 66 20 61 ocated size of a
f6e0: 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
f6f0: 6f 6e 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 on.** previously
f700: 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 78 obtained from x
f710: 4d 61 6c 6c 6f 63 20 6f 72 20 78 52 65 61 6c 6c Malloc or xReall
f720: 6f 63 2e 20 20 54 68 65 20 61 6c 6c 6f 63 61 74 oc. The allocat
f730: 65 64 20 73 69 7a 65 0a 2a 2a 20 69 73 20 61 6c ed size.** is al
f740: 77 61 79 73 20 61 74 20 6c 65 61 73 74 20 61 73 ways at least as
f750: 20 62 69 67 20 61 73 20 74 68 65 20 72 65 71 75 big as the requ
f760: 65 73 74 65 64 20 73 69 7a 65 20 62 75 74 20 6d ested size but m
f770: 61 79 20 62 65 20 6c 61 72 67 65 72 2e 0a 2a 2a ay be larger..**
f780: 0a 2a 2a 20 54 68 65 20 78 52 6f 75 6e 64 75 70 .** The xRoundup
f790: 20 6d 65 74 68 6f 64 20 72 65 74 75 72 6e 73 20 method returns
f7a0: 77 68 61 74 20 77 6f 75 6c 64 20 62 65 20 74 68 what would be th
f7b0: 65 20 61 6c 6c 6f 63 61 74 65 64 20 73 69 7a 65 e allocated size
f7c0: 20 6f 66 0a 2a 2a 20 61 20 6d 65 6d 6f 72 79 20 of.** a memory
f7d0: 61 6c 6c 6f 63 61 74 69 6f 6e 20 67 69 76 65 6e allocation given
f7e0: 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 72 65 a particular re
f7f0: 71 75 65 73 74 65 64 20 73 69 7a 65 2e 20 20 4d quested size. M
f800: 6f 73 74 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c ost memory.** al
f810: 6c 6f 63 61 74 6f 72 73 20 72 6f 75 6e 64 20 75 locators round u
f820: 70 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 p memory allocat
f830: 69 6f 6e 73 20 61 74 20 6c 65 61 73 74 20 74 6f ions at least to
f840: 20 74 68 65 20 6e 65 78 74 20 6d 75 6c 74 69 70 the next multip
f850: 6c 65 0a 2a 2a 20 6f 66 20 38 2e 20 20 53 6f 6d le.** of 8. Som
f860: 65 20 61 6c 6c 6f 63 61 74 6f 72 73 20 72 6f 75 e allocators rou
f870: 6e 64 20 75 70 20 74 6f 20 61 20 6c 61 72 67 65 nd up to a large
f880: 72 20 6d 75 6c 74 69 70 6c 65 20 6f 72 20 74 6f r multiple or to
f890: 20 61 20 70 6f 77 65 72 20 6f 66 20 32 2e 0a 2a a power of 2..*
f8a0: 2a 20 45 76 65 72 79 20 6d 65 6d 6f 72 79 20 61 * Every memory a
f8b0: 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 71 75 65 73 llocation reques
f8c0: 74 20 63 6f 6d 69 6e 67 20 69 6e 20 74 68 72 6f t coming in thro
f8d0: 75 67 68 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c ugh [sqlite3_mal
f8e0: 6c 6f 63 28 29 5d 0a 2a 2a 20 6f 72 20 5b 73 71 loc()].** or [sq
f8f0: 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 5d lite3_realloc()]
f900: 20 66 69 72 73 74 20 63 61 6c 6c 73 20 78 52 6f first calls xRo
f910: 75 6e 64 75 70 2e 20 20 49 66 20 78 52 6f 75 6e undup. If xRoun
f920: 64 75 70 20 72 65 74 75 72 6e 73 20 30 2c 20 0a dup returns 0, .
f930: 2a 2a 20 74 68 61 74 20 63 61 75 73 65 73 20 74 ** that causes t
f940: 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 he corresponding
f950: 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
f960: 6f 6e 20 74 6f 20 66 61 69 6c 2e 0a 2a 2a 0a 2a on to fail..**.*
f970: 2a 20 54 68 65 20 78 49 6e 69 74 20 6d 65 74 68 * The xInit meth
f980: 6f 64 20 69 6e 69 74 69 61 6c 69 7a 65 73 20 74 od initializes t
f990: 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 he memory alloca
f9a0: 74 6f 72 2e 20 20 28 46 6f 72 20 65 78 61 6d 70 tor. (For examp
f9b0: 6c 65 2c 0a 2a 2a 20 69 74 20 6d 69 67 68 74 20 le,.** it might
f9c0: 61 6c 6c 6f 63 61 74 65 20 61 6e 79 20 72 65 71 allocate any req
f9d0: 75 69 72 65 20 6d 75 74 65 78 65 73 20 6f 72 20 uire mutexes or
f9e0: 69 6e 69 74 69 61 6c 69 7a 65 20 69 6e 74 65 72 initialize inter
f9f0: 6e 61 6c 20 64 61 74 61 0a 2a 2a 20 73 74 72 75 nal data.** stru
fa00: 63 74 75 72 65 73 2e 20 20 54 68 65 20 78 53 68 ctures. The xSh
fa10: 75 74 64 6f 77 6e 20 6d 65 74 68 6f 64 20 69 73 utdown method is
fa20: 20 69 6e 76 6f 6b 65 64 20 28 69 6e 64 69 72 65 invoked (indire
fa30: 63 74 6c 79 29 20 62 79 0a 2a 2a 20 5b 73 71 6c ctly) by.** [sql
fa40: 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 5d ite3_shutdown()]
fa50: 20 61 6e 64 20 73 68 6f 75 6c 64 20 64 65 61 6c and should deal
fa60: 6c 6f 63 61 74 65 20 61 6e 79 20 72 65 73 6f 75 locate any resou
fa70: 72 63 65 73 20 61 63 71 75 69 72 65 64 0a 2a 2a rces acquired.**
fa80: 20 62 79 20 78 49 6e 69 74 2e 20 20 54 68 65 20 by xInit. The
fa90: 70 41 70 70 44 61 74 61 20 70 6f 69 6e 74 65 72 pAppData pointer
faa0: 20 69 73 20 75 73 65 64 20 61 73 20 74 68 65 20 is used as the
fab0: 6f 6e 6c 79 20 70 61 72 61 6d 65 74 65 72 20 74 only parameter t
fac0: 6f 0a 2a 2a 20 78 49 6e 69 74 20 61 6e 64 20 78 o.** xInit and x
fad0: 53 68 75 74 64 6f 77 6e 2e 0a 2a 2a 0a 2a 2a 20 Shutdown..**.**
fae0: 53 51 4c 69 74 65 20 68 6f 6c 64 73 20 74 68 65 SQLite holds the
faf0: 20 5b 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 [SQLITE_MUTEX_S
fb00: 54 41 54 49 43 5f 4d 41 53 54 45 52 5d 20 6d 75 TATIC_MASTER] mu
fb10: 74 65 78 20 77 68 65 6e 20 69 74 20 69 6e 76 6f tex when it invo
fb20: 6b 65 73 0a 2a 2a 20 74 68 65 20 78 49 6e 69 74 kes.** the xInit
fb30: 20 6d 65 74 68 6f 64 2c 20 73 6f 20 74 68 65 20 method, so the
fb40: 78 49 6e 69 74 20 6d 65 74 68 6f 64 20 6e 65 65 xInit method nee
fb50: 64 20 6e 6f 74 20 62 65 20 74 68 72 65 61 64 73 d not be threads
fb60: 61 66 65 2e 20 20 54 68 65 0a 2a 2a 20 78 53 68 afe. The.** xSh
fb70: 75 74 64 6f 77 6e 20 6d 65 74 68 6f 64 20 69 73 utdown method is
fb80: 20 6f 6e 6c 79 20 63 61 6c 6c 65 64 20 66 72 6f only called fro
fb90: 6d 20 5b 73 71 6c 69 74 65 33 5f 73 68 75 74 64 m [sqlite3_shutd
fba0: 6f 77 6e 28 29 5d 20 73 6f 20 69 74 20 64 6f 65 own()] so it doe
fbb0: 73 0a 2a 2a 20 6e 6f 74 20 6e 65 65 64 20 74 6f s.** not need to
fbc0: 20 62 65 20 74 68 72 65 61 64 73 61 66 65 20 65 be threadsafe e
fbd0: 69 74 68 65 72 2e 20 20 46 6f 72 20 61 6c 6c 20 ither. For all
fbe0: 6f 74 68 65 72 20 6d 65 74 68 6f 64 73 2c 20 53 other methods, S
fbf0: 51 4c 69 74 65 0a 2a 2a 20 68 6f 6c 64 73 20 74 QLite.** holds t
fc00: 68 65 20 5b 53 51 4c 49 54 45 5f 4d 55 54 45 58 he [SQLITE_MUTEX
fc10: 5f 53 54 41 54 49 43 5f 4d 45 4d 5d 20 6d 75 74 _STATIC_MEM] mut
fc20: 65 78 20 61 73 20 6c 6f 6e 67 20 61 73 20 74 68 ex as long as th
fc30: 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 4f 4e e.** [SQLITE_CON
fc40: 46 49 47 5f 4d 45 4d 53 54 41 54 55 53 5d 20 63 FIG_MEMSTATUS] c
fc50: 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 onfiguration opt
fc60: 69 6f 6e 20 69 73 20 74 75 72 6e 65 64 20 6f 6e ion is turned on
fc70: 20 28 77 68 69 63 68 0a 2a 2a 20 69 74 20 69 73 (which.** it is
fc80: 20 62 79 20 64 65 66 61 75 6c 74 29 20 61 6e 64 by default) and
fc90: 20 73 6f 20 74 68 65 20 6d 65 74 68 6f 64 73 20 so the methods
fca0: 61 72 65 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c are automaticall
fcb0: 79 20 73 65 72 69 61 6c 69 7a 65 64 2e 0a 2a 2a y serialized..**
fcc0: 20 48 6f 77 65 76 65 72 2c 20 69 66 20 5b 53 51 However, if [SQ
fcd0: 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 45 4d 53 LITE_CONFIG_MEMS
fce0: 54 41 54 55 53 5d 20 69 73 20 64 69 73 61 62 6c TATUS] is disabl
fcf0: 65 64 2c 20 74 68 65 6e 20 74 68 65 20 6f 74 68 ed, then the oth
fd00: 65 72 0a 2a 2a 20 6d 65 74 68 6f 64 73 20 6d 75 er.** methods mu
fd10: 73 74 20 62 65 20 74 68 72 65 61 64 73 61 66 65 st be threadsafe
fd20: 20 6f 72 20 65 6c 73 65 20 6d 61 6b 65 20 74 68 or else make th
fd30: 65 69 72 20 6f 77 6e 20 61 72 72 61 6e 67 65 6d eir own arrangem
fd40: 65 6e 74 73 20 66 6f 72 0a 2a 2a 20 73 65 72 69 ents for.** seri
fd50: 61 6c 69 7a 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a alization..**.**
fd60: 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 65 76 SQLite will nev
fd70: 65 72 20 69 6e 76 6f 6b 65 20 78 49 6e 69 74 28 er invoke xInit(
fd80: 29 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65 ) more than once
fd90: 20 77 69 74 68 6f 75 74 20 61 6e 20 69 6e 74 65 without an inte
fda0: 72 76 65 6e 69 6e 67 0a 2a 2a 20 63 61 6c 6c 20 rvening.** call
fdb0: 74 6f 20 78 53 68 75 74 64 6f 77 6e 28 29 2e 0a to xShutdown()..
fdc0: 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 */.typedef struc
fdd0: 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 t sqlite3_mem_me
fde0: 74 68 6f 64 73 20 73 71 6c 69 74 65 33 5f 6d 65 thods sqlite3_me
fdf0: 6d 5f 6d 65 74 68 6f 64 73 3b 0a 73 74 72 75 63 m_methods;.struc
fe00: 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 t sqlite3_mem_me
fe10: 74 68 6f 64 73 20 7b 0a 20 20 76 6f 69 64 20 2a thods {. void *
fe20: 28 2a 78 4d 61 6c 6c 6f 63 29 28 69 6e 74 29 3b (*xMalloc)(int);
fe30: 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f /* Memo
fe40: 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 ry allocation fu
fe50: 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 nction */. void
fe60: 20 28 2a 78 46 72 65 65 29 28 76 6f 69 64 2a 29 (*xFree)(void*)
fe70: 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 72 ; /* Fr
fe80: 65 65 20 61 20 70 72 69 6f 72 20 61 6c 6c 6f 63 ee a prior alloc
fe90: 61 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 ation */. void
fea0: 2a 28 2a 78 52 65 61 6c 6c 6f 63 29 28 76 6f 69 *(*xRealloc)(voi
feb0: 64 2a 2c 69 6e 74 29 3b 20 20 2f 2a 20 52 65 73 d*,int); /* Res
fec0: 69 7a 65 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f ize an allocatio
fed0: 6e 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 53 69 n */. int (*xSi
fee0: 7a 65 29 28 76 6f 69 64 2a 29 3b 20 20 20 20 20 ze)(void*);
fef0: 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 /* Return
ff00: 74 68 65 20 73 69 7a 65 20 6f 66 20 61 6e 20 61 the size of an a
ff10: 6c 6c 6f 63 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 llocation */. i
ff20: 6e 74 20 28 2a 78 52 6f 75 6e 64 75 70 29 28 69 nt (*xRoundup)(i
ff30: 6e 74 29 3b 20 20 20 20 20 20 20 20 20 20 2f 2a nt); /*
ff40: 20 52 6f 75 6e 64 20 75 70 20 72 65 71 75 65 73 Round up reques
ff50: 74 20 73 69 7a 65 20 74 6f 20 61 6c 6c 6f 63 61 t size to alloca
ff60: 74 69 6f 6e 20 73 69 7a 65 20 2a 2f 0a 20 20 69 tion size */. i
ff70: 6e 74 20 28 2a 78 49 6e 69 74 29 28 76 6f 69 64 nt (*xInit)(void
ff80: 2a 29 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a *); /*
ff90: 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 Initialize the
ffa0: 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 memory allocator
ffb0: 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 53 68 */. void (*xSh
ffc0: 75 74 64 6f 77 6e 29 28 76 6f 69 64 2a 29 3b 20 utdown)(void*);
ffd0: 20 20 20 20 20 2f 2a 20 44 65 69 6e 69 74 69 61 /* Deinitia
ffe0: 6c 69 7a 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 lize the memory
fff0: 61 6c 6c 6f 63 61 74 6f 72 20 2a 2f 0a 20 20 76 allocator */. v
10000 6f 69 64 20 2a 70 41 70 70 44 61 74 61 3b 20 20 oid *pAppData;
10010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
10020 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 78 49 6e Argument to xIn
10030 69 74 28 29 20 61 6e 64 20 78 53 68 75 74 64 6f it() and xShutdo
10040 77 6e 28 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a wn() */.};../*.*
10050 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6e 66 * CAPI3REF: Conf
10060 69 67 75 72 61 74 69 6f 6e 20 4f 70 74 69 6f 6e iguration Option
10070 73 20 7b 48 31 30 31 36 30 7d 20 3c 53 32 30 30 s {H10160} <S200
10080 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 00>.** EXPERIMEN
10090 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 TAL.**.** These
100a0 63 6f 6e 73 74 61 6e 74 73 20 61 72 65 20 74 68 constants are th
100b0 65 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 74 65 e available inte
100c0 67 65 72 20 63 6f 6e 66 69 67 75 72 61 74 69 6f ger configuratio
100d0 6e 20 6f 70 74 69 6f 6e 73 20 74 68 61 74 0a 2a n options that.*
100e0 2a 20 63 61 6e 20 62 65 20 70 61 73 73 65 64 20 * can be passed
100f0 61 73 20 74 68 65 20 66 69 72 73 74 20 61 72 67 as the first arg
10100 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 5b 73 71 ument to the [sq
10110 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d 20 lite3_config()]
10120 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a interface..**.**
10130 20 4e 65 77 20 63 6f 6e 66 69 67 75 72 61 74 69 New configurati
10140 6f 6e 20 6f 70 74 69 6f 6e 73 20 6d 61 79 20 62 on options may b
10150 65 20 61 64 64 65 64 20 69 6e 20 66 75 74 75 72 e added in futur
10160 65 20 72 65 6c 65 61 73 65 73 20 6f 66 20 53 51 e releases of SQ
10170 4c 69 74 65 2e 0a 2a 2a 20 45 78 69 73 74 69 6e Lite..** Existin
10180 67 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 g configuration
10190 6f 70 74 69 6f 6e 73 20 6d 69 67 68 74 20 62 65 options might be
101a0 20 64 69 73 63 6f 6e 74 69 6e 75 65 64 2e 20 20 discontinued.
101b0 41 70 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 Applications.**
101c0 73 68 6f 75 6c 64 20 63 68 65 63 6b 20 74 68 65 should check the
101d0 20 72 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f return code fro
101e0 6d 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 m [sqlite3_confi
101f0 67 28 29 5d 20 74 6f 20 6d 61 6b 65 20 73 75 72 g()] to make sur
10200 65 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 63 61 e that.** the ca
10210 6c 6c 20 77 6f 72 6b 65 64 2e 20 20 54 68 65 20 ll worked. The
10220 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 [sqlite3_config(
10230 29 5d 20 69 6e 74 65 72 66 61 63 65 20 77 69 6c )] interface wil
10240 6c 20 72 65 74 75 72 6e 20 61 0a 2a 2a 20 6e 6f l return a.** no
10250 6e 2d 7a 65 72 6f 20 5b 65 72 72 6f 72 20 63 6f n-zero [error co
10260 64 65 5d 20 69 66 20 61 20 64 69 73 63 6f 6e 74 de] if a discont
10270 69 6e 75 65 64 20 6f 72 20 75 6e 73 75 70 70 6f inued or unsuppo
10280 72 74 65 64 20 63 6f 6e 66 69 67 75 72 61 74 69 rted configurati
10290 6f 6e 20 6f 70 74 69 6f 6e 0a 2a 2a 20 69 73 20 on option.** is
102a0 69 6e 76 6f 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20 3c invoked..**.** <
102b0 64 6c 3e 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 dl>.** <dt>SQLIT
102c0 45 5f 43 4f 4e 46 49 47 5f 53 49 4e 47 4c 45 54 E_CONFIG_SINGLET
102d0 48 52 45 41 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 HREAD</dt>.** <d
102e0 64 3e 54 68 65 72 65 20 61 72 65 20 6e 6f 20 61 d>There are no a
102f0 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 69 73 rguments to this
10300 20 6f 70 74 69 6f 6e 2e 20 20 54 68 69 73 20 6f option. This o
10310 70 74 69 6f 6e 20 64 69 73 61 62 6c 65 73 0a 2a ption disables.*
10320 2a 20 61 6c 6c 20 6d 75 74 65 78 69 6e 67 20 61 * all mutexing a
10330 6e 64 20 70 75 74 73 20 53 51 4c 69 74 65 20 69 nd puts SQLite i
10340 6e 74 6f 20 61 20 6d 6f 64 65 20 77 68 65 72 65 nto a mode where
10350 20 69 74 20 63 61 6e 20 6f 6e 6c 79 20 62 65 20 it can only be
10360 75 73 65 64 0a 2a 2a 20 62 79 20 61 20 73 69 6e used.** by a sin
10370 67 6c 65 20 74 68 72 65 61 64 2e 3c 2f 64 64 3e gle thread.</dd>
10380 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 .**.** <dt>SQLIT
10390 45 5f 43 4f 4e 46 49 47 5f 4d 55 4c 54 49 54 48 E_CONFIG_MULTITH
103a0 52 45 41 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 READ</dt>.** <dd
103b0 3e 54 68 65 72 65 20 61 72 65 20 6e 6f 20 61 72 >There are no ar
103c0 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 69 73 20 guments to this
103d0 6f 70 74 69 6f 6e 2e 20 20 54 68 69 73 20 6f 70 option. This op
103e0 74 69 6f 6e 20 64 69 73 61 62 6c 65 73 0a 2a 2a tion disables.**
103f0 20 6d 75 74 65 78 69 6e 67 20 6f 6e 20 5b 64 61 mutexing on [da
10400 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
10410 6e 5d 20 61 6e 64 20 5b 70 72 65 70 61 72 65 64 n] and [prepared
10420 20 73 74 61 74 65 6d 65 6e 74 5d 20 6f 62 6a 65 statement] obje
10430 63 74 73 2e 0a 2a 2a 20 54 68 65 20 61 70 70 6c cts..** The appl
10440 69 63 61 74 69 6f 6e 20 69 73 20 72 65 73 70 6f ication is respo
10450 6e 73 69 62 6c 65 20 66 6f 72 20 73 65 72 69 61 nsible for seria
10460 6c 69 7a 69 6e 67 20 61 63 63 65 73 73 20 74 6f lizing access to
10470 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 63 6f .** [database co
10480 6e 6e 65 63 74 69 6f 6e 73 5d 20 61 6e 64 20 5b nnections] and [
10490 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
104a0 6e 74 73 5d 2e 20 20 42 75 74 20 6f 74 68 65 72 nts]. But other
104b0 20 6d 75 74 65 78 65 73 0a 2a 2a 20 61 72 65 20 mutexes.** are
104c0 65 6e 61 62 6c 65 64 20 73 6f 20 74 68 61 74 20 enabled so that
104d0 53 51 4c 69 74 65 20 77 69 6c 6c 20 62 65 20 73 SQLite will be s
104e0 61 66 65 20 74 6f 20 75 73 65 20 69 6e 20 61 20 afe to use in a
104f0 6d 75 6c 74 69 2d 74 68 72 65 61 64 65 64 0a 2a multi-threaded.*
10500 2a 20 65 6e 76 69 72 6f 6e 6d 65 6e 74 20 61 73 * environment as
10510 20 6c 6f 6e 67 20 61 73 20 6e 6f 20 74 77 6f 20 long as no two
10520 74 68 72 65 61 64 73 20 61 74 74 65 6d 70 74 20 threads attempt
10530 74 6f 20 75 73 65 20 74 68 65 20 73 61 6d 65 0a to use the same.
10540 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e ** [database con
10550 6e 65 63 74 69 6f 6e 5d 20 61 74 20 74 68 65 20 nection] at the
10560 73 61 6d 65 20 74 69 6d 65 2e 20 20 53 65 65 20 same time. See
10570 74 68 65 20 5b 74 68 72 65 61 64 69 6e 67 20 6d the [threading m
10580 6f 64 65 5d 0a 2a 2a 20 64 6f 63 75 6d 65 6e 74 ode].** document
10590 61 74 69 6f 6e 20 66 6f 72 20 61 64 64 69 74 69 ation for additi
105a0 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e onal information
105b0 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 .</dd>.**.** <dt
105c0 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 >SQLITE_CONFIG_S
105d0 45 52 49 41 4c 49 5a 45 44 3c 2f 64 74 3e 0a 2a ERIALIZED</dt>.*
105e0 2a 20 3c 64 64 3e 54 68 65 72 65 20 61 72 65 20 * <dd>There are
105f0 6e 6f 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 no arguments to
10600 74 68 69 73 20 6f 70 74 69 6f 6e 2e 20 20 54 68 this option. Th
10610 69 73 20 6f 70 74 69 6f 6e 20 65 6e 61 62 6c 65 is option enable
10620 73 0a 2a 2a 20 61 6c 6c 20 6d 75 74 65 78 65 73 s.** all mutexes
10630 20 69 6e 63 6c 75 64 69 6e 67 20 74 68 65 20 72 including the r
10640 65 63 75 72 73 69 76 65 0a 2a 2a 20 6d 75 74 65 ecursive.** mute
10650 78 65 73 20 6f 6e 20 5b 64 61 74 61 62 61 73 65 xes on [database
10660 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 61 6e 64 connection] and
10670 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 [prepared state
10680 6d 65 6e 74 5d 20 6f 62 6a 65 63 74 73 2e 0a 2a ment] objects..*
10690 2a 20 49 6e 20 74 68 69 73 20 6d 6f 64 65 20 28 * In this mode (
106a0 77 68 69 63 68 20 69 73 20 74 68 65 20 64 65 66 which is the def
106b0 61 75 6c 74 20 77 68 65 6e 20 53 51 4c 69 74 65 ault when SQLite
106c0 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 is compiled wit
106d0 68 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 54 48 52 h.** [SQLITE_THR
106e0 45 41 44 53 41 46 45 3d 31 5d 29 20 74 68 65 20 EADSAFE=1]) the
106f0 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 20 77 SQLite library w
10700 69 6c 6c 20 69 74 73 65 6c 66 20 73 65 72 69 61 ill itself seria
10710 6c 69 7a 65 20 61 63 63 65 73 73 0a 2a 2a 20 74 lize access.** t
10720 6f 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e o [database conn
10730 65 63 74 69 6f 6e 73 5d 20 61 6e 64 20 5b 70 72 ections] and [pr
10740 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 epared statement
10750 73 5d 20 73 6f 20 74 68 61 74 20 74 68 65 0a 2a s] so that the.*
10760 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 69 73 * application is
10770 20 66 72 65 65 20 74 6f 20 75 73 65 20 74 68 65 free to use the
10780 20 73 61 6d 65 20 5b 64 61 74 61 62 61 73 65 20 same [database
10790 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 6f 72 20 74 connection] or t
107a0 68 65 0a 2a 2a 20 73 61 6d 65 20 5b 70 72 65 70 he.** same [prep
107b0 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 ared statement]
107c0 69 6e 20 64 69 66 66 65 72 65 6e 74 20 74 68 72 in different thr
107d0 65 61 64 73 20 61 74 20 74 68 65 20 73 61 6d 65 eads at the same
107e0 20 74 69 6d 65 2e 0a 2a 2a 20 53 65 65 20 74 68 time..** See th
107f0 65 20 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 e [threading mod
10800 65 5d 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e e] documentation
10810 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 for additional
10820 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 3c 2f 64 64 information.</dd
10830 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 >.**.** <dt>SQLI
10840 54 45 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 TE_CONFIG_MALLOC
10850 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 </dt>.** <dd>Thi
10860 73 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73 20 61 s option takes a
10870 20 73 69 6e 67 6c 65 20 61 72 67 75 6d 65 6e 74 single argument
10880 20 77 68 69 63 68 20 69 73 20 61 20 70 6f 69 6e which is a poin
10890 74 65 72 20 74 6f 20 61 6e 0a 2a 2a 20 69 6e 73 ter to an.** ins
108a0 74 61 6e 63 65 20 6f 66 20 74 68 65 20 5b 73 71 tance of the [sq
108b0 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 lite3_mem_method
108c0 73 5d 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 s] structure. T
108d0 68 65 20 61 72 67 75 6d 65 6e 74 20 73 70 65 63 he argument spec
108e0 69 66 69 65 73 0a 2a 2a 20 61 6c 74 65 72 6e 61 ifies.** alterna
108f0 74 69 76 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d tive low-level m
10900 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
10910 20 72 6f 75 74 69 6e 65 73 20 74 6f 20 62 65 20 routines to be
10920 75 73 65 64 20 69 6e 20 70 6c 61 63 65 20 6f 66 used in place of
10930 0a 2a 2a 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 .** the memory a
10940 6c 6c 6f 63 61 74 69 6f 6e 20 72 6f 75 74 69 6e llocation routin
10950 65 73 20 62 75 69 6c 74 20 69 6e 74 6f 20 53 51 es built into SQ
10960 4c 69 74 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a Lite.</dd>.**.**
10970 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 <dt>SQLITE_CONF
10980 49 47 5f 47 45 54 4d 41 4c 4c 4f 43 3c 2f 64 74 IG_GETMALLOC</dt
10990 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70 >.** <dd>This op
109a0 74 69 6f 6e 20 74 61 6b 65 73 20 61 20 73 69 6e tion takes a sin
109b0 67 6c 65 20 61 72 67 75 6d 65 6e 74 20 77 68 69 gle argument whi
109c0 63 68 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 ch is a pointer
109d0 74 6f 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 to an.** instanc
109e0 65 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65 e of the [sqlite
109f0 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 5d 20 73 3_mem_methods] s
10a00 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 20 5b tructure. The [
10a10 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 sqlite3_mem_meth
10a20 6f 64 73 5d 0a 2a 2a 20 73 74 72 75 63 74 75 72 ods].** structur
10a30 65 20 69 73 20 66 69 6c 6c 65 64 20 77 69 74 68 e is filled with
10a40 20 74 68 65 20 63 75 72 72 65 6e 74 6c 79 20 64 the currently d
10a50 65 66 69 6e 65 64 20 6d 65 6d 6f 72 79 20 61 6c efined memory al
10a60 6c 6f 63 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65 location routine
10a70 73 2e 0a 2a 2a 20 54 68 69 73 20 6f 70 74 69 6f s..** This optio
10a80 6e 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f n can be used to
10a90 20 6f 76 65 72 6c 6f 61 64 20 74 68 65 20 64 65 overload the de
10aa0 66 61 75 6c 74 20 6d 65 6d 6f 72 79 20 61 6c 6c fault memory all
10ab0 6f 63 61 74 69 6f 6e 0a 2a 2a 20 72 6f 75 74 69 ocation.** routi
10ac0 6e 65 73 20 77 69 74 68 20 61 20 77 72 61 70 70 nes with a wrapp
10ad0 65 72 20 74 68 61 74 20 73 69 6d 75 6c 61 74 69 er that simulati
10ae0 6f 6e 73 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 ons memory alloc
10af0 61 74 69 6f 6e 20 66 61 69 6c 75 72 65 20 6f 72 ation failure or
10b00 0a 2a 2a 20 74 72 61 63 6b 73 20 6d 65 6d 6f 72 .** tracks memor
10b10 79 20 75 73 61 67 65 2c 20 66 6f 72 20 65 78 61 y usage, for exa
10b20 6d 70 6c 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a mple.</dd>.**.**
10b30 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 <dt>SQLITE_CONF
10b40 49 47 5f 4d 45 4d 53 54 41 54 55 53 3c 2f 64 74 IG_MEMSTATUS</dt
10b50 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70 >.** <dd>This op
10b60 74 69 6f 6e 20 74 61 6b 65 73 20 73 69 6e 67 6c tion takes singl
10b70 65 20 61 72 67 75 6d 65 6e 74 20 6f 66 20 74 79 e argument of ty
10b80 70 65 20 69 6e 74 2c 20 69 6e 74 65 72 70 72 65 pe int, interpre
10b90 74 65 64 20 61 73 20 61 20 0a 2a 2a 20 62 6f 6f ted as a .** boo
10ba0 6c 65 61 6e 2c 20 77 68 69 63 68 20 65 6e 61 62 lean, which enab
10bb0 6c 65 73 20 6f 72 20 64 69 73 61 62 6c 65 73 20 les or disables
10bc0 74 68 65 20 63 6f 6c 6c 65 63 74 69 6f 6e 20 6f the collection o
10bd0 66 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 f memory allocat
10be0 69 6f 6e 20 0a 2a 2a 20 73 74 61 74 69 73 74 69 ion .** statisti
10bf0 63 73 2e 20 57 68 65 6e 20 64 69 73 61 62 6c 65 cs. When disable
10c00 64 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 d, the following
10c10 20 53 51 4c 69 74 65 20 69 6e 74 65 72 66 61 63 SQLite interfac
10c20 65 73 20 62 65 63 6f 6d 65 20 0a 2a 2a 20 6e 6f es become .** no
10c30 6e 2d 6f 70 65 72 61 74 69 6f 6e 61 6c 3a 0a 2a n-operational:.*
10c40 2a 20 20 20 3c 75 6c 3e 0a 2a 2a 20 20 20 3c 6c * <ul>.** <l
10c50 69 3e 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d 6f i> [sqlite3_memo
10c60 72 79 5f 75 73 65 64 28 29 5d 0a 2a 2a 20 20 20 ry_used()].**
10c70 3c 6c 69 3e 20 5b 73 71 6c 69 74 65 33 5f 6d 65 <li> [sqlite3_me
10c80 6d 6f 72 79 5f 68 69 67 68 77 61 74 65 72 28 29 mory_highwater()
10c90 5d 0a 2a 2a 20 20 20 3c 6c 69 3e 20 5b 73 71 6c ].** <li> [sql
10ca0 69 74 65 33 5f 73 6f 66 74 5f 68 65 61 70 5f 6c ite3_soft_heap_l
10cb0 69 6d 69 74 28 29 5d 0a 2a 2a 20 20 20 3c 6c 69 imit()].** <li
10cc0 3e 20 5b 73 71 6c 69 74 65 33 5f 73 74 61 74 75 > [sqlite3_statu
10cd0 73 28 29 5d 0a 2a 2a 20 20 20 3c 2f 75 6c 3e 0a s()].** </ul>.
10ce0 2a 2a 20 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c ** </dd>.**.** <
10cf0 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 dt>SQLITE_CONFIG
10d00 5f 53 43 52 41 54 43 48 3c 2f 64 74 3e 0a 2a 2a _SCRATCH</dt>.**
10d10 20 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e <dd>This option
10d20 20 73 70 65 63 69 66 69 65 73 20 61 20 73 74 61 specifies a sta
10d30 74 69 63 20 6d 65 6d 6f 72 79 20 62 75 66 66 65 tic memory buffe
10d40 72 20 74 68 61 74 20 53 51 4c 69 74 65 20 63 61 r that SQLite ca
10d50 6e 20 75 73 65 20 66 6f 72 0a 2a 2a 20 73 63 72 n use for.** scr
10d60 61 74 63 68 20 6d 65 6d 6f 72 79 2e 20 20 54 68 atch memory. Th
10d70 65 72 65 20 61 72 65 20 74 68 72 65 65 20 61 72 ere are three ar
10d80 67 75 6d 65 6e 74 73 3a 20 20 41 20 70 6f 69 6e guments: A poin
10d90 74 65 72 20 61 6e 20 38 2d 62 79 74 65 0a 2a 2a ter an 8-byte.**
10da0 20 61 6c 69 67 6e 65 64 20 6d 65 6d 6f 72 79 20 aligned memory
10db0 62 75 66 66 65 72 20 66 72 6f 6d 20 77 68 69 63 buffer from whic
10dc0 68 20 74 68 65 20 73 63 72 61 63 68 20 61 6c 6c h the scrach all
10dd0 6f 63 61 74 69 6f 6e 73 20 77 69 6c 6c 20 62 65 ocations will be
10de0 0a 2a 2a 20 64 72 61 77 6e 2c 20 74 68 65 20 73 .** drawn, the s
10df0 69 7a 65 20 6f 66 20 65 61 63 68 20 73 63 72 61 ize of each scra
10e00 74 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 28 tch allocation (
10e10 73 7a 29 2c 0a 2a 2a 20 61 6e 64 20 74 68 65 20 sz),.** and the
10e20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f maximum number o
10e30 66 20 73 63 72 61 74 63 68 20 61 6c 6c 6f 63 61 f scratch alloca
10e40 74 69 6f 6e 73 20 28 4e 29 2e 20 20 54 68 65 20 tions (N). The
10e50 73 7a 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 6d sz.** argument m
10e60 75 73 74 20 62 65 20 61 20 6d 75 6c 74 69 70 6c ust be a multipl
10e70 65 20 6f 66 20 31 36 2e 20 54 68 65 20 73 7a 20 e of 16. The sz
10e80 70 61 72 61 6d 65 74 65 72 20 73 68 6f 75 6c 64 parameter should
10e90 20 62 65 20 61 20 66 65 77 20 62 79 74 65 73 0a be a few bytes.
10ea0 2a 2a 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 ** larger than t
10eb0 68 65 20 61 63 74 75 61 6c 20 73 63 72 61 74 63 he actual scratc
10ec0 68 20 73 70 61 63 65 20 72 65 71 75 69 72 65 64 h space required
10ed0 20 64 75 65 20 74 6f 20 69 6e 74 65 72 6e 61 6c due to internal
10ee0 20 6f 76 65 72 68 65 61 64 2e 0a 2a 2a 20 54 68 overhead..** Th
10ef0 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 e first argument
10f00 20 73 68 6f 75 6c 64 20 70 6f 69 6e 74 65 72 20 should pointer
10f10 74 6f 20 61 6e 20 38 2d 62 79 74 65 20 61 6c 69 to an 8-byte ali
10f20 67 6e 65 64 20 62 75 66 66 65 72 0a 2a 2a 20 6f gned buffer.** o
10f30 66 20 61 74 20 6c 65 61 73 74 20 73 7a 2a 4e 20 f at least sz*N
10f40 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 2e bytes of memory.
10f50 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 .** SQLite will
10f60 75 73 65 20 6e 6f 20 6d 6f 72 65 20 74 68 61 6e use no more than
10f70 20 6f 6e 65 20 73 63 72 61 74 63 68 20 62 75 66 one scratch buf
10f80 66 65 72 20 61 74 20 6f 6e 63 65 20 70 65 72 20 fer at once per
10f90 74 68 72 65 61 64 2c 20 73 6f 0a 2a 2a 20 4e 20 thread, so.** N
10fa0 73 68 6f 75 6c 64 20 62 65 20 73 65 74 20 74 6f should be set to
10fb0 20 74 68 65 20 65 78 70 65 63 74 65 64 20 6d 61 the expected ma
10fc0 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 ximum number of
10fd0 74 68 72 65 61 64 73 2e 20 20 54 68 65 20 73 7a threads. The sz
10fe0 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 73 68 .** parameter sh
10ff0 6f 75 6c 64 20 62 65 20 36 20 74 69 6d 65 73 20 ould be 6 times
11000 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 the size of the
11010 6c 61 72 67 65 73 74 20 64 61 74 61 62 61 73 65 largest database
11020 20 70 61 67 65 20 73 69 7a 65 2e 0a 2a 2a 20 53 page size..** S
11030 63 72 61 74 63 68 20 62 75 66 66 65 72 73 20 61 cratch buffers a
11040 72 65 20 75 73 65 64 20 61 73 20 70 61 72 74 20 re used as part
11050 6f 66 20 74 68 65 20 62 74 72 65 65 20 62 61 6c of the btree bal
11060 61 6e 63 65 20 6f 70 65 72 61 74 69 6f 6e 2e 20 ance operation.
11070 20 49 66 0a 2a 2a 20 54 68 65 20 62 74 72 65 65 If.** The btree
11080 20 62 61 6c 61 6e 63 65 72 20 6e 65 65 64 73 20 balancer needs
11090 61 64 64 69 74 69 6f 6e 61 6c 20 6d 65 6d 6f 72 additional memor
110a0 79 20 62 65 79 6f 6e 64 20 77 68 61 74 20 69 73 y beyond what is
110b0 20 70 72 6f 76 69 64 65 64 20 62 79 0a 2a 2a 20 provided by.**
110c0 73 63 72 61 74 63 68 20 62 75 66 66 65 72 73 20 scratch buffers
110d0 6f 72 20 69 66 20 6e 6f 20 73 63 72 61 74 63 68 or if no scratch
110e0 20 62 75 66 66 65 72 20 73 70 61 63 65 20 69 73 buffer space is
110f0 20 73 70 65 63 69 66 69 65 64 2c 20 74 68 65 6e specified, then
11100 20 53 51 4c 69 74 65 0a 2a 2a 20 67 6f 65 73 20 SQLite.** goes
11110 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c to [sqlite3_mall
11120 6f 63 28 29 5d 20 74 6f 20 6f 62 74 61 69 6e 20 oc()] to obtain
11130 74 68 65 20 6d 65 6d 6f 72 79 20 69 74 20 6e 65 the memory it ne
11140 65 64 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 eds.</dd>.**.**
11150 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 <dt>SQLITE_CONFI
11160 47 5f 50 41 47 45 43 41 43 48 45 3c 2f 64 74 3e G_PAGECACHE</dt>
11170 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70 74 .** <dd>This opt
11180 69 6f 6e 20 73 70 65 63 69 66 69 65 73 20 61 20 ion specifies a
11190 73 74 61 74 69 63 20 6d 65 6d 6f 72 79 20 62 75 static memory bu
111a0 66 66 65 72 20 74 68 61 74 20 53 51 4c 69 74 65 ffer that SQLite
111b0 20 63 61 6e 20 75 73 65 20 66 6f 72 0a 2a 2a 20 can use for.**
111c0 74 68 65 20 64 61 74 61 62 61 73 65 20 70 61 67 the database pag
111d0 65 20 63 61 63 68 65 20 77 69 74 68 20 74 68 65 e cache with the
111e0 20 64 65 66 61 75 6c 74 20 70 61 67 65 20 63 61 default page ca
111f0 63 68 65 20 69 6d 70 6c 65 6d 65 6e 61 74 69 6f che implemenatio
11200 6e 2e 20 20 0a 2a 2a 20 54 68 69 73 20 63 6f 6e n. .** This con
11210 66 69 67 75 72 61 74 69 6f 6e 20 73 68 6f 75 6c figuration shoul
11220 64 20 6e 6f 74 20 62 65 20 75 73 65 64 20 69 66 d not be used if
11230 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d an application-
11240 64 65 66 69 6e 65 20 70 61 67 65 0a 2a 2a 20 63 define page.** c
11250 61 63 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 ache implementat
11260 69 6f 6e 20 69 73 20 6c 6f 61 64 65 64 20 75 73 ion is loaded us
11270 69 6e 67 20 74 68 65 20 53 51 4c 49 54 45 5f 43 ing the SQLITE_C
11280 4f 4e 46 49 47 5f 50 43 41 43 48 45 20 6f 70 74 ONFIG_PCACHE opt
11290 69 6f 6e 2e 0a 2a 2a 20 54 68 65 72 65 20 61 72 ion..** There ar
112a0 65 20 74 68 72 65 65 20 61 72 67 75 6d 65 6e 74 e three argument
112b0 73 20 74 6f 20 74 68 69 73 20 6f 70 74 69 6f 6e s to this option
112c0 3a 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 38 : A pointer to 8
112d0 2d 62 79 74 65 20 61 6c 69 67 6e 65 64 0a 2a 2a -byte aligned.**
112e0 20 6d 65 6d 6f 72 79 2c 20 74 68 65 20 73 69 7a memory, the siz
112f0 65 20 6f 66 20 65 61 63 68 20 70 61 67 65 20 62 e of each page b
11300 75 66 66 65 72 20 28 73 7a 29 2c 20 61 6e 64 20 uffer (sz), and
11310 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 the number of pa
11320 67 65 73 20 28 4e 29 2e 0a 2a 2a 20 54 68 65 20 ges (N)..** The
11330 73 7a 20 61 72 67 75 6d 65 6e 74 20 73 68 6f 75 sz argument shou
11340 6c 64 20 62 65 20 74 68 65 20 73 69 7a 65 20 6f ld be the size o
11350 66 20 74 68 65 20 6c 61 72 67 65 73 74 20 64 61 f the largest da
11360 74 61 62 61 73 65 20 70 61 67 65 0a 2a 2a 20 28 tabase page.** (
11370 61 20 70 6f 77 65 72 20 6f 66 20 74 77 6f 20 62 a power of two b
11380 65 74 77 65 65 6e 20 35 31 32 20 61 6e 64 20 33 etween 512 and 3
11390 32 37 36 38 29 20 70 6c 75 73 20 61 20 6c 69 74 2768) plus a lit
113a0 74 6c 65 20 65 78 74 72 61 20 66 6f 72 20 65 61 tle extra for ea
113b0 63 68 0a 2a 2a 20 70 61 67 65 20 68 65 61 64 65 ch.** page heade
113c0 72 2e 20 20 54 68 65 20 70 61 67 65 20 68 65 61 r. The page hea
113d0 64 65 72 20 73 69 7a 65 20 69 73 20 32 30 20 74 der size is 20 t
113e0 6f 20 34 30 20 62 79 74 65 73 20 64 65 70 65 6e o 40 bytes depen
113f0 64 69 6e 67 20 6f 6e 0a 2a 2a 20 74 68 65 20 68 ding on.** the h
11400 6f 73 74 20 61 72 63 68 69 74 65 63 74 75 72 65 ost architecture
11410 2e 20 20 49 74 20 69 73 20 68 61 72 6d 6c 65 73 . It is harmles
11420 73 2c 20 61 70 61 72 74 20 66 72 6f 6d 20 74 68 s, apart from th
11430 65 20 77 61 73 74 65 64 20 6d 65 6d 6f 72 79 2c e wasted memory,
11440 0a 2a 2a 20 74 6f 20 6d 61 6b 65 20 73 7a 20 61 .** to make sz a
11450 20 6c 69 74 74 6c 65 20 74 6f 6f 20 6c 61 72 67 little too larg
11460 65 2e 20 20 54 68 65 20 66 69 72 73 74 0a 2a 2a e. The first.**
11470 20 61 72 67 75 6d 65 6e 74 20 73 68 6f 75 6c 64 argument should
11480 20 70 6f 69 6e 74 20 74 6f 20 61 6e 20 61 6c 6c point to an all
11490 6f 63 61 74 69 6f 6e 20 6f 66 20 61 74 20 6c 65 ocation of at le
114a0 61 73 74 20 73 7a 2a 4e 20 62 79 74 65 73 20 6f ast sz*N bytes o
114b0 66 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 20 53 51 4c f memory..** SQL
114c0 69 74 65 20 77 69 6c 6c 20 75 73 65 20 74 68 65 ite will use the
114d0 20 6d 65 6d 6f 72 79 20 70 72 6f 76 69 64 65 64 memory provided
114e0 20 62 79 20 74 68 65 20 66 69 72 73 74 20 61 72 by the first ar
114f0 67 75 6d 65 6e 74 20 74 6f 20 73 61 74 69 73 66 gument to satisf
11500 79 20 69 74 73 0a 2a 2a 20 6d 65 6d 6f 72 79 20 y its.** memory
11510 6e 65 65 64 73 20 66 6f 72 20 74 68 65 20 66 69 needs for the fi
11520 72 73 74 20 4e 20 70 61 67 65 73 20 74 68 61 74 rst N pages that
11530 20 69 74 20 61 64 64 73 20 74 6f 20 63 61 63 68 it adds to cach
11540 65 2e 20 20 49 66 20 61 64 64 69 74 69 6f 6e 61 e. If additiona
11550 6c 0a 2a 2a 20 70 61 67 65 20 63 61 63 68 65 20 l.** page cache
11560 6d 65 6d 6f 72 79 20 69 73 20 6e 65 65 64 65 64 memory is needed
11570 20 62 65 79 6f 6e 64 20 77 68 61 74 20 69 73 20 beyond what is
11580 70 72 6f 76 69 64 65 64 20 62 79 20 74 68 69 73 provided by this
11590 20 6f 70 74 69 6f 6e 2c 20 74 68 65 6e 0a 2a 2a option, then.**
115a0 20 53 51 4c 69 74 65 20 67 6f 65 73 20 74 6f 20 SQLite goes to
115b0 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 [sqlite3_malloc(
115c0 29 5d 20 66 6f 72 20 74 68 65 20 61 64 64 69 74 )] for the addit
115d0 69 6f 6e 61 6c 20 73 74 6f 72 61 67 65 20 73 70 ional storage sp
115e0 61 63 65 2e 0a 2a 2a 20 54 68 65 20 69 6d 70 6c ace..** The impl
115f0 65 6d 65 6e 74 61 74 69 6f 6e 20 6d 69 67 68 74 ementation might
11600 20 75 73 65 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 use one or more
11610 20 6f 66 20 74 68 65 20 4e 20 62 75 66 66 65 72 of the N buffer
11620 73 20 74 6f 20 68 6f 6c 64 20 0a 2a 2a 20 6d 65 s to hold .** me
11630 6d 6f 72 79 20 61 63 63 6f 75 6e 74 69 6e 67 20 mory accounting
11640 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 54 68 65 information. The
11650 20 70 6f 69 6e 74 65 72 20 69 6e 20 74 68 65 20 pointer in the
11660 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 6d first argument m
11670 75 73 74 0a 2a 2a 20 62 65 20 61 6c 69 67 6e 65 ust.** be aligne
11680 64 20 74 6f 20 61 6e 20 38 2d 62 79 74 65 20 62 d to an 8-byte b
11690 6f 75 6e 64 61 72 79 20 6f 72 20 73 75 62 73 65 oundary or subse
116a0 71 75 65 6e 74 20 62 65 68 61 76 69 6f 72 20 6f quent behavior o
116b0 66 20 53 51 4c 69 74 65 0a 2a 2a 20 77 69 6c 6c f SQLite.** will
116c0 20 62 65 20 75 6e 64 65 66 69 6e 65 64 2e 3c 2f be undefined.</
116d0 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 dd>.**.** <dt>SQ
116e0 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 48 45 41 50 LITE_CONFIG_HEAP
116f0 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 </dt>.** <dd>Thi
11700 73 20 6f 70 74 69 6f 6e 20 73 70 65 63 69 66 69 s option specifi
11710 65 73 20 61 20 73 74 61 74 69 63 20 6d 65 6d 6f es a static memo
11720 72 79 20 62 75 66 66 65 72 20 74 68 61 74 20 53 ry buffer that S
11730 51 4c 69 74 65 20 77 69 6c 6c 20 75 73 65 0a 2a QLite will use.*
11740 2a 20 66 6f 72 20 61 6c 6c 20 6f 66 20 69 74 73 * for all of its
11750 20 64 79 6e 61 6d 69 63 20 6d 65 6d 6f 72 79 20 dynamic memory
11760 61 6c 6c 6f 63 61 74 69 6f 6e 20 6e 65 65 64 73 allocation needs
11770 20 62 65 79 6f 6e 64 20 74 68 6f 73 65 20 70 72 beyond those pr
11780 6f 76 69 64 65 64 0a 2a 2a 20 66 6f 72 20 62 79 ovided.** for by
11790 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f [SQLITE_CONFIG_
117a0 53 43 52 41 54 43 48 5d 20 61 6e 64 20 5b 53 51 SCRATCH] and [SQ
117b0 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 LITE_CONFIG_PAGE
117c0 43 41 43 48 45 5d 2e 0a 2a 2a 20 54 68 65 72 65 CACHE]..** There
117d0 20 61 72 65 20 74 68 72 65 65 20 61 72 67 75 6d are three argum
117e0 65 6e 74 73 3a 20 41 6e 20 38 2d 62 79 74 65 20 ents: An 8-byte
117f0 61 6c 69 67 6e 65 64 20 70 6f 69 6e 74 65 72 20 aligned pointer
11800 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 2c 0a 2a to the memory,.*
11810 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 * the number of
11820 62 79 74 65 73 20 69 6e 20 74 68 65 20 6d 65 6d bytes in the mem
11830 6f 72 79 20 62 75 66 66 65 72 2c 20 61 6e 64 20 ory buffer, and
11840 74 68 65 20 6d 69 6e 69 6d 75 6d 20 61 6c 6c 6f the minimum allo
11850 63 61 74 69 6f 6e 20 73 69 7a 65 2e 0a 2a 2a 20 cation size..**
11860 49 66 20 74 68 65 20 66 69 72 73 74 20 70 6f 69 If the first poi
11870 6e 74 65 72 20 28 74 68 65 20 6d 65 6d 6f 72 79 nter (the memory
11880 20 70 6f 69 6e 74 65 72 29 20 69 73 20 4e 55 4c pointer) is NUL
11890 4c 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 20 72 L, then SQLite r
118a0 65 76 65 72 74 73 0a 2a 2a 20 74 6f 20 75 73 69 everts.** to usi
118b0 6e 67 20 69 74 73 20 64 65 66 61 75 6c 74 20 6d ng its default m
118c0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 emory allocator
118d0 28 74 68 65 20 73 79 73 74 65 6d 20 6d 61 6c 6c (the system mall
118e0 6f 63 28 29 20 69 6d 70 6c 65 6d 65 6e 74 61 74 oc() implementat
118f0 69 6f 6e 29 2c 0a 2a 2a 20 75 6e 64 6f 69 6e 67 ion),.** undoing
11900 20 61 6e 79 20 70 72 69 6f 72 20 69 6e 76 6f 63 any prior invoc
11910 61 74 69 6f 6e 20 6f 66 20 5b 53 51 4c 49 54 45 ation of [SQLITE
11920 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 5d 2e _CONFIG_MALLOC].
11930 20 20 49 66 20 74 68 65 0a 2a 2a 20 6d 65 6d 6f If the.** memo
11940 72 79 20 70 6f 69 6e 74 65 72 20 69 73 20 6e 6f ry pointer is no
11950 74 20 4e 55 4c 4c 20 61 6e 64 20 65 69 74 68 65 t NULL and eithe
11960 72 20 5b 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 r [SQLITE_ENABLE
11970 5f 4d 45 4d 53 59 53 33 5d 20 6f 72 0a 2a 2a 20 _MEMSYS3] or.**
11980 5b 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d [SQLITE_ENABLE_M
11990 45 4d 53 59 53 35 5d 20 61 72 65 20 64 65 66 69 EMSYS5] are defi
119a0 6e 65 64 2c 20 74 68 65 6e 20 74 68 65 20 61 6c ned, then the al
119b0 74 65 72 6e 61 74 69 76 65 20 6d 65 6d 6f 72 79 ternative memory
119c0 0a 2a 2a 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 .** allocator is
119d0 20 65 6e 67 61 67 65 64 20 74 6f 20 68 61 6e 64 engaged to hand
119e0 6c 65 20 61 6c 6c 20 6f 66 20 53 51 4c 69 74 65 le all of SQLite
119f0 73 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 s memory allocat
11a00 69 6f 6e 20 6e 65 65 64 73 2e 0a 2a 2a 20 54 68 ion needs..** Th
11a10 65 20 66 69 72 73 74 20 70 6f 69 6e 74 65 72 20 e first pointer
11a20 28 74 68 65 20 6d 65 6d 6f 72 79 20 70 6f 69 6e (the memory poin
11a30 74 65 72 29 20 6d 75 73 74 20 62 65 20 61 6c 69 ter) must be ali
11a40 67 6e 65 64 20 74 6f 20 61 6e 20 38 2d 62 79 74 gned to an 8-byt
11a50 65 0a 2a 2a 20 62 6f 75 6e 64 61 72 79 20 6f 72 e.** boundary or
11a60 20 73 75 62 73 65 71 75 65 6e 74 20 62 65 68 61 subsequent beha
11a70 76 69 6f 72 20 6f 66 20 53 51 4c 69 74 65 20 77 vior of SQLite w
11a80 69 6c 6c 20 62 65 20 75 6e 64 65 66 69 6e 65 64 ill be undefined
11a90 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 .</dd>.**.** <dt
11aa0 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d >SQLITE_CONFIG_M
11ab0 55 54 45 58 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 UTEX</dt>.** <dd
11ac0 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 74 61 6b >This option tak
11ad0 65 73 20 61 20 73 69 6e 67 6c 65 20 61 72 67 75 es a single argu
11ae0 6d 65 6e 74 20 77 68 69 63 68 20 69 73 20 61 20 ment which is a
11af0 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 0a 2a 2a pointer to an.**
11b00 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 instance of the
11b10 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f [sqlite3_mutex_
11b20 6d 65 74 68 6f 64 73 5d 20 73 74 72 75 63 74 75 methods] structu
11b30 72 65 2e 20 20 54 68 65 20 61 72 67 75 6d 65 6e re. The argumen
11b40 74 20 73 70 65 63 69 66 69 65 73 0a 2a 2a 20 61 t specifies.** a
11b50 6c 74 65 72 6e 61 74 69 76 65 20 6c 6f 77 2d 6c lternative low-l
11b60 65 76 65 6c 20 6d 75 74 65 78 20 72 6f 75 74 69 evel mutex routi
11b70 6e 65 73 20 74 6f 20 62 65 20 75 73 65 64 20 69 nes to be used i
11b80 6e 20 70 6c 61 63 65 0a 2a 2a 20 74 68 65 20 6d n place.** the m
11b90 75 74 65 78 20 72 6f 75 74 69 6e 65 73 20 62 75 utex routines bu
11ba0 69 6c 74 20 69 6e 74 6f 20 53 51 4c 69 74 65 2e ilt into SQLite.
11bb0 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e </dd>.**.** <dt>
11bc0 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45 SQLITE_CONFIG_GE
11bd0 54 4d 55 54 45 58 3c 2f 64 74 3e 0a 2a 2a 20 3c TMUTEX</dt>.** <
11be0 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 74 dd>This option t
11bf0 61 6b 65 73 20 61 20 73 69 6e 67 6c 65 20 61 72 akes a single ar
11c00 67 75 6d 65 6e 74 20 77 68 69 63 68 20 69 73 20 gument which is
11c10 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 0a a pointer to an.
11c20 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 ** instance of t
11c30 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 he [sqlite3_mute
11c40 78 5f 6d 65 74 68 6f 64 73 5d 20 73 74 72 75 63 x_methods] struc
11c50 74 75 72 65 2e 20 20 54 68 65 0a 2a 2a 20 5b 73 ture. The.** [s
11c60 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 qlite3_mutex_met
11c70 68 6f 64 73 5d 0a 2a 2a 20 73 74 72 75 63 74 75 hods].** structu
11c80 72 65 20 69 73 20 66 69 6c 6c 65 64 20 77 69 74 re is filled wit
11c90 68 20 74 68 65 20 63 75 72 72 65 6e 74 6c 79 20 h the currently
11ca0 64 65 66 69 6e 65 64 20 6d 75 74 65 78 20 72 6f defined mutex ro
11cb0 75 74 69 6e 65 73 2e 0a 2a 2a 20 54 68 69 73 20 utines..** This
11cc0 6f 70 74 69 6f 6e 20 63 61 6e 20 62 65 20 75 73 option can be us
11cd0 65 64 20 74 6f 20 6f 76 65 72 6c 6f 61 64 20 74 ed to overload t
11ce0 68 65 20 64 65 66 61 75 6c 74 20 6d 75 74 65 78 he default mutex
11cf0 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 72 allocation.** r
11d00 6f 75 74 69 6e 65 73 20 77 69 74 68 20 61 20 77 outines with a w
11d10 72 61 70 70 65 72 20 75 73 65 64 20 74 6f 20 74 rapper used to t
11d20 72 61 63 6b 20 6d 75 74 65 78 20 75 73 61 67 65 rack mutex usage
11d30 20 66 6f 72 20 70 65 72 66 6f 72 6d 61 6e 63 65 for performance
11d40 0a 2a 2a 20 70 72 6f 66 69 6c 69 6e 67 20 6f 72 .** profiling or
11d50 20 74 65 73 74 69 6e 67 2c 20 66 6f 72 20 65 78 testing, for ex
11d60 61 6d 70 6c 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a ample.</dd>.**.*
11d70 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e * <dt>SQLITE_CON
11d80 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 45 3c 2f 64 FIG_LOOKASIDE</d
11d90 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f t>.** <dd>This o
11da0 70 74 69 6f 6e 20 74 61 6b 65 73 20 74 77 6f 20 ption takes two
11db0 61 72 67 75 6d 65 6e 74 73 20 74 68 61 74 20 64 arguments that d
11dc0 65 74 65 72 6d 69 6e 65 20 74 68 65 20 64 65 66 etermine the def
11dd0 61 75 6c 74 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 ault.** memory a
11de0 6c 6c 6f 63 61 74 69 6f 6e 20 6c 6f 6f 6b 61 73 llocation lookas
11df0 69 64 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e ide optimization
11e00 2e 20 20 54 68 65 20 66 69 72 73 74 20 61 72 67 . The first arg
11e10 75 6d 65 6e 74 20 69 73 20 74 68 65 0a 2a 2a 20 ument is the.**
11e20 73 69 7a 65 20 6f 66 20 65 61 63 68 20 6c 6f 6f size of each loo
11e30 6b 61 73 69 64 65 20 62 75 66 66 65 72 20 73 6c kaside buffer sl
11e40 6f 74 20 61 6e 64 20 74 68 65 20 73 65 63 6f 6e ot and the secon
11e50 64 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 d is the number
11e60 6f 66 0a 2a 2a 20 73 6c 6f 74 73 20 61 6c 6c 6f of.** slots allo
11e70 63 61 74 65 64 20 74 6f 20 65 61 63 68 20 64 61 cated to each da
11e80 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
11e90 6e 2e 20 20 54 68 69 73 20 6f 70 74 69 6f 6e 20 n. This option
11ea0 73 65 74 73 20 74 68 65 0a 2a 2a 20 3c 69 3e 64 sets the.** <i>d
11eb0 65 66 61 75 6c 74 3c 2f 69 3e 20 6c 6f 6f 6b 61 efault</i> looka
11ec0 73 69 64 65 20 73 69 7a 65 2e 20 20 54 68 65 20 side size. The
11ed0 5b 53 51 4c 49 54 45 5f 44 42 43 4f 4e 46 49 47 [SQLITE_DBCONFIG
11ee0 5f 4c 4f 4f 4b 41 53 49 44 45 5d 0a 2a 2a 20 76 _LOOKASIDE].** v
11ef0 65 72 62 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f erb to [sqlite3_
11f00 64 62 5f 63 6f 6e 66 69 67 28 29 5d 20 63 61 6e db_config()] can
11f10 20 62 65 20 75 73 65 64 20 74 6f 20 63 68 61 6e be used to chan
11f20 67 65 20 74 68 65 20 6c 6f 6f 6b 61 73 69 64 65 ge the lookaside
11f30 0a 2a 2a 20 63 6f 6e 66 69 67 75 72 61 74 69 6f .** configuratio
11f40 6e 20 6f 6e 20 69 6e 64 69 76 69 64 75 61 6c 20 n on individual
11f50 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 3c 2f 64 64 connections.</dd
11f60 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 >.**.** <dt>SQLI
11f70 54 45 5f 43 4f 4e 46 49 47 5f 50 43 41 43 48 45 TE_CONFIG_PCACHE
11f80 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 </dt>.** <dd>Thi
11f90 73 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73 20 61 s option takes a
11fa0 20 73 69 6e 67 6c 65 20 61 72 67 75 6d 65 6e 74 single argument
11fb0 20 77 68 69 63 68 20 69 73 20 61 20 70 6f 69 6e which is a poin
11fc0 74 65 72 20 74 6f 0a 2a 2a 20 61 6e 20 5b 73 71 ter to.** an [sq
11fd0 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 lite3_pcache_met
11fe0 68 6f 64 73 5d 20 6f 62 6a 65 63 74 2e 20 20 54 hods] object. T
11ff0 68 69 73 20 6f 62 6a 65 63 74 20 73 70 65 63 69 his object speci
12000 66 69 65 73 20 74 68 65 20 69 6e 74 65 72 66 61 fies the interfa
12010 63 65 0a 2a 2a 20 74 6f 20 61 20 63 75 73 74 6f ce.** to a custo
12020 6d 20 70 61 67 65 20 63 61 63 68 65 20 69 6d 70 m page cache imp
12030 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 20 53 51 lementation. SQ
12040 4c 69 74 65 20 6d 61 6b 65 73 20 61 20 63 6f 70 Lite makes a cop
12050 79 20 6f 66 20 74 68 65 0a 2a 2a 20 6f 62 6a 65 y of the.** obje
12060 63 74 20 61 6e 64 20 75 73 65 73 20 69 74 20 66 ct and uses it f
12070 6f 72 20 70 61 67 65 20 63 61 63 68 65 20 6d 65 or page cache me
12080 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 mory allocations
12090 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 .</dd>.**.** <dt
120a0 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 >SQLITE_CONFIG_G
120b0 45 54 50 43 41 43 48 45 3c 2f 64 74 3e 0a 2a 2a ETPCACHE</dt>.**
120c0 20 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e <dd>This option
120d0 20 74 61 6b 65 73 20 61 20 73 69 6e 67 6c 65 20 takes a single
120e0 61 72 67 75 6d 65 6e 74 20 77 68 69 63 68 20 69 argument which i
120f0 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 s a pointer to a
12100 6e 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 63 n.** [sqlite3_pc
12110 61 63 68 65 5f 6d 65 74 68 6f 64 73 5d 20 6f 62 ache_methods] ob
12120 6a 65 63 74 2e 20 20 53 51 4c 69 74 65 20 63 6f ject. SQLite co
12130 70 69 65 73 20 6f 66 20 74 68 65 20 63 75 72 72 pies of the curr
12140 65 6e 74 0a 2a 2a 20 70 61 67 65 20 63 61 63 68 ent.** page cach
12150 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e e implementation
12160 20 69 6e 74 6f 20 74 68 61 74 20 6f 62 6a 65 63 into that objec
12170 74 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 2f t.</dd>.**.** </
12180 64 6c 3e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 dl>.*/.#define S
12190 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 49 4e QLITE_CONFIG_SIN
121a0 47 4c 45 54 48 52 45 41 44 20 20 31 20 20 2f 2a GLETHREAD 1 /*
121b0 20 6e 69 6c 20 2a 2f 0a 23 64 65 66 69 6e 65 20 nil */.#define
121c0 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 SQLITE_CONFIG_MU
121d0 4c 54 49 54 48 52 45 41 44 20 20 20 32 20 20 2f LTITHREAD 2 /
121e0 2a 20 6e 69 6c 20 2a 2f 0a 23 64 65 66 69 6e 65 * nil */.#define
121f0 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 SQLITE_CONFIG_S
12200 45 52 49 41 4c 49 5a 45 44 20 20 20 20 33 20 20 ERIALIZED 3
12210 2f 2a 20 6e 69 6c 20 2a 2f 0a 23 64 65 66 69 6e /* nil */.#defin
12220 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f e SQLITE_CONFIG_
12230 4d 41 4c 4c 4f 43 20 20 20 20 20 20 20 20 34 20 MALLOC 4
12240 20 2f 2a 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f /* sqlite3_mem_
12250 6d 65 74 68 6f 64 73 2a 20 2a 2f 0a 23 64 65 66 methods* */.#def
12260 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 ine SQLITE_CONFI
12270 47 5f 47 45 54 4d 41 4c 4c 4f 43 20 20 20 20 20 G_GETMALLOC
12280 35 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 6d 65 5 /* sqlite3_me
12290 6d 5f 6d 65 74 68 6f 64 73 2a 20 2a 2f 0a 23 64 m_methods* */.#d
122a0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e efine SQLITE_CON
122b0 46 49 47 5f 53 43 52 41 54 43 48 20 20 20 20 20 FIG_SCRATCH
122c0 20 20 36 20 20 2f 2a 20 76 6f 69 64 2a 2c 20 69 6 /* void*, i
122d0 6e 74 20 73 7a 2c 20 69 6e 74 20 4e 20 2a 2f 0a nt sz, int N */.
122e0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 #define SQLITE_C
122f0 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 20 ONFIG_PAGECACHE
12300 20 20 20 20 37 20 20 2f 2a 20 76 6f 69 64 2a 2c 7 /* void*,
12310 20 69 6e 74 20 73 7a 2c 20 69 6e 74 20 4e 20 2a int sz, int N *
12320 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
12330 5f 43 4f 4e 46 49 47 5f 48 45 41 50 20 20 20 20 _CONFIG_HEAP
12340 20 20 20 20 20 20 38 20 20 2f 2a 20 76 6f 69 64 8 /* void
12350 2a 2c 20 69 6e 74 20 6e 42 79 74 65 2c 20 69 6e *, int nByte, in
12360 74 20 6d 69 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 t min */.#define
12370 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d SQLITE_CONFIG_M
12380 45 4d 53 54 41 54 55 53 20 20 20 20 20 39 20 20 EMSTATUS 9
12390 2f 2a 20 62 6f 6f 6c 65 61 6e 20 2a 2f 0a 23 64 /* boolean */.#d
123a0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e efine SQLITE_CON
123b0 46 49 47 5f 4d 55 54 45 58 20 20 20 20 20 20 20 FIG_MUTEX
123c0 20 31 30 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 10 /* sqlite3_
123d0 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 2a 20 2a mutex_methods* *
123e0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
123f0 5f 43 4f 4e 46 49 47 5f 47 45 54 4d 55 54 45 58 _CONFIG_GETMUTEX
12400 20 20 20 20 20 31 31 20 20 2f 2a 20 73 71 6c 69 11 /* sqli
12410 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 te3_mutex_method
12420 73 2a 20 2a 2f 0a 2f 2a 20 70 72 65 76 69 6f 75 s* */./* previou
12430 73 6c 79 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 sly SQLITE_CONFI
12440 47 5f 43 48 55 4e 4b 41 4c 4c 4f 43 20 31 32 20 G_CHUNKALLOC 12
12450 77 68 69 63 68 20 69 73 20 6e 6f 77 20 75 6e 75 which is now unu
12460 73 65 64 2e 20 2a 2f 20 0a 23 64 65 66 69 6e 65 sed. */ .#define
12470 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4c SQLITE_CONFIG_L
12480 4f 4f 4b 41 53 49 44 45 20 20 20 20 31 33 20 20 OOKASIDE 13
12490 2f 2a 20 69 6e 74 20 69 6e 74 20 2a 2f 0a 23 64 /* int int */.#d
124a0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e efine SQLITE_CON
124b0 46 49 47 5f 50 43 41 43 48 45 20 20 20 20 20 20 FIG_PCACHE
124c0 20 31 34 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 14 /* sqlite3_
124d0 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 2a 20 pcache_methods*
124e0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
124f0 45 5f 43 4f 4e 46 49 47 5f 47 45 54 50 43 41 43 E_CONFIG_GETPCAC
12500 48 45 20 20 20 20 31 35 20 20 2f 2a 20 73 71 6c HE 15 /* sql
12510 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 ite3_pcache_meth
12520 6f 64 73 2a 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 ods* */../*.** C
12530 41 50 49 33 52 45 46 3a 20 43 6f 6e 66 69 67 75 API3REF: Configu
12540 72 61 74 69 6f 6e 20 4f 70 74 69 6f 6e 73 20 7b ration Options {
12550 48 31 30 31 37 30 7d 20 3c 53 32 30 30 30 30 3e H10170} <S20000>
12560 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c .** EXPERIMENTAL
12570 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 63 6f 6e .**.** These con
12580 73 74 61 6e 74 73 20 61 72 65 20 74 68 65 20 61 stants are the a
12590 76 61 69 6c 61 62 6c 65 20 69 6e 74 65 67 65 72 vailable integer
125a0 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f configuration o
125b0 70 74 69 6f 6e 73 20 74 68 61 74 0a 2a 2a 20 63 ptions that.** c
125c0 61 6e 20 62 65 20 70 61 73 73 65 64 20 61 73 20 an be passed as
125d0 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d the second argum
125e0 65 6e 74 20 74 6f 20 74 68 65 20 5b 73 71 6c 69 ent to the [sqli
125f0 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67 28 29 5d te3_db_config()]
12600 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a interface..**.*
12610 2a 20 4e 65 77 20 63 6f 6e 66 69 67 75 72 61 74 * New configurat
12620 69 6f 6e 20 6f 70 74 69 6f 6e 73 20 6d 61 79 20 ion options may
12630 62 65 20 61 64 64 65 64 20 69 6e 20 66 75 74 75 be added in futu
12640 72 65 20 72 65 6c 65 61 73 65 73 20 6f 66 20 53 re releases of S
12650 51 4c 69 74 65 2e 0a 2a 2a 20 45 78 69 73 74 69 QLite..** Existi
12660 6e 67 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e ng configuration
12670 20 6f 70 74 69 6f 6e 73 20 6d 69 67 68 74 20 62 options might b
12680 65 20 64 69 73 63 6f 6e 74 69 6e 75 65 64 2e 20 e discontinued.
12690 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a Applications.**
126a0 20 73 68 6f 75 6c 64 20 63 68 65 63 6b 20 74 68 should check th
126b0 65 20 72 65 74 75 72 6e 20 63 6f 64 65 20 66 72 e return code fr
126c0 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f 63 om [sqlite3_db_c
126d0 6f 6e 66 69 67 28 29 5d 20 74 6f 20 6d 61 6b 65 onfig()] to make
126e0 20 73 75 72 65 20 74 68 61 74 0a 2a 2a 20 74 68 sure that.** th
126f0 65 20 63 61 6c 6c 20 77 6f 72 6b 65 64 2e 20 20 e call worked.
12700 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f The [sqlite3_db_
12710 63 6f 6e 66 69 67 28 29 5d 20 69 6e 74 65 72 66 config()] interf
12720 61 63 65 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 ace will return
12730 61 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 20 5b 65 a.** non-zero [e
12740 72 72 6f 72 20 63 6f 64 65 5d 20 69 66 20 61 20 rror code] if a
12750 64 69 73 63 6f 6e 74 69 6e 75 65 64 20 6f 72 20 discontinued or
12760 75 6e 73 75 70 70 6f 72 74 65 64 20 63 6f 6e 66 unsupported conf
12770 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e iguration option
12780 0a 2a 2a 20 69 73 20 69 6e 76 6f 6b 65 64 2e 0a .** is invoked..
12790 2a 2a 0a 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 **.** <dl>.** <d
127a0 74 3e 53 51 4c 49 54 45 5f 44 42 43 4f 4e 46 49 t>SQLITE_DBCONFI
127b0 47 5f 4c 4f 4f 4b 41 53 49 44 45 3c 2f 64 74 3e G_LOOKASIDE</dt>
127c0 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70 74 .** <dd>This opt
127d0 69 6f 6e 20 74 61 6b 65 73 20 74 68 72 65 65 20 ion takes three
127e0 61 64 64 69 74 69 6f 6e 61 6c 20 61 72 67 75 6d additional argum
127f0 65 6e 74 73 20 74 68 61 74 20 64 65 74 65 72 6d ents that determ
12800 69 6e 65 20 74 68 65 20 0a 2a 2a 20 5b 6c 6f 6f ine the .** [loo
12810 6b 61 73 69 64 65 20 6d 65 6d 6f 72 79 20 61 6c kaside memory al
12820 6c 6f 63 61 74 6f 72 5d 20 63 6f 6e 66 69 67 75 locator] configu
12830 72 61 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 5b ration for the [
12840 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
12850 69 6f 6e 5d 2e 0a 2a 2a 20 54 68 65 20 66 69 72 ion]..** The fir
12860 73 74 20 61 72 67 75 6d 65 6e 74 20 28 74 68 65 st argument (the
12870 20 74 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 third parameter
12880 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f to [sqlite3_db_
12890 63 6f 6e 66 69 67 28 29 5d 20 69 73 20 61 0a 2a config()] is a.*
128a0 2a 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 * pointer to an
128b0 6d 65 6d 6f 72 79 20 62 75 66 66 65 72 20 74 6f memory buffer to
128c0 20 75 73 65 20 66 6f 72 20 6c 6f 6f 6b 61 73 69 use for lookasi
128d0 64 65 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 20 54 68 de memory..** Th
128e0 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 e first argument
128f0 20 6d 61 79 20 62 65 20 4e 55 4c 4c 20 69 6e 20 may be NULL in
12900 77 68 69 63 68 20 63 61 73 65 20 53 51 4c 69 74 which case SQLit
12910 65 20 77 69 6c 6c 20 61 6c 6c 6f 63 61 74 65 20 e will allocate
12920 74 68 65 0a 2a 2a 20 6c 6f 6f 6b 61 73 69 64 65 the.** lookaside
12930 20 62 75 66 66 65 72 20 69 74 73 65 6c 66 20 75 buffer itself u
12940 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 6d 61 sing [sqlite3_ma
12950 6c 6c 6f 63 28 29 5d 2e 20 20 54 68 65 20 73 65 lloc()]. The se
12960 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 cond argument is
12970 20 74 68 65 0a 2a 2a 20 73 69 7a 65 20 6f 66 20 the.** size of
12980 65 61 63 68 20 6c 6f 6f 6b 61 73 69 64 65 20 62 each lookaside b
12990 75 66 66 65 72 20 73 6c 6f 74 20 61 6e 64 20 74 uffer slot and t
129a0 68 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e he third argumen
129b0 74 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 t is the number
129c0 6f 66 0a 2a 2a 20 73 6c 6f 74 73 2e 20 20 54 68 of.** slots. Th
129d0 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 62 75 e size of the bu
129e0 66 66 65 72 20 69 6e 20 74 68 65 20 66 69 72 73 ffer in the firs
129f0 74 20 61 72 67 75 6d 65 6e 74 20 6d 75 73 74 20 t argument must
12a00 62 65 20 67 72 65 61 74 65 72 20 74 68 61 6e 0a be greater than.
12a10 2a 2a 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 ** or equal to t
12a20 68 65 20 70 72 6f 64 75 63 74 20 6f 66 20 74 68 he product of th
12a30 65 20 73 65 63 6f 6e 64 20 61 6e 64 20 74 68 69 e second and thi
12a40 72 64 20 61 72 67 75 6d 65 6e 74 73 2e 20 20 54 rd arguments. T
12a50 68 65 20 62 75 66 66 65 72 0a 2a 2a 20 6d 75 73 he buffer.** mus
12a60 74 20 62 65 20 61 6c 69 67 6e 65 64 20 74 6f 20 t be aligned to
12a70 61 6e 20 38 2d 62 79 74 65 20 62 6f 75 6e 64 61 an 8-byte bounda
12a80 72 79 2e 20 20 49 66 20 74 68 65 20 73 65 63 6f ry. If the seco
12a90 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6e nd argument is n
12aa0 6f 74 0a 2a 2a 20 61 20 6d 75 6c 74 69 70 6c 65 ot.** a multiple
12ab0 20 6f 66 20 38 2c 20 69 74 20 69 73 20 69 6e 74 of 8, it is int
12ac0 65 72 6e 61 6c 6c 79 20 72 6f 75 6e 64 65 64 20 ernally rounded
12ad0 64 6f 77 6e 20 74 6f 20 74 68 65 20 6e 65 78 74 down to the next
12ae0 20 73 6d 61 6c 6c 65 72 0a 2a 2a 20 6d 75 6c 74 smaller.** mult
12af0 69 70 6c 65 20 6f 66 20 38 2e 20 20 53 65 65 20 iple of 8. See
12b00 61 6c 73 6f 3a 20 5b 53 51 4c 49 54 45 5f 43 4f also: [SQLITE_CO
12b10 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 45 5d 3c NFIG_LOOKASIDE]<
12b20 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 2f 64 6c 3e /dd>.**.** </dl>
12b30 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 .*/.#define SQLI
12b40 54 45 5f 44 42 43 4f 4e 46 49 47 5f 4c 4f 4f 4b TE_DBCONFIG_LOOK
12b50 41 53 49 44 45 20 20 20 20 31 30 30 31 20 20 2f ASIDE 1001 /
12b60 2a 20 76 6f 69 64 2a 20 69 6e 74 20 69 6e 74 20 * void* int int
12b70 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 */.../*.** CAPI3
12b80 52 45 46 3a 20 45 6e 61 62 6c 65 20 4f 72 20 44 REF: Enable Or D
12b90 69 73 61 62 6c 65 20 45 78 74 65 6e 64 65 64 20 isable Extended
12ba0 52 65 73 75 6c 74 20 43 6f 64 65 73 20 7b 48 31 Result Codes {H1
12bb0 32 32 30 30 7d 20 3c 53 31 30 37 30 30 3e 0a 2a 2200} <S10700>.*
12bc0 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
12bd0 5f 65 78 74 65 6e 64 65 64 5f 72 65 73 75 6c 74 _extended_result
12be0 5f 63 6f 64 65 73 28 29 20 72 6f 75 74 69 6e 65 _codes() routine
12bf0 20 65 6e 61 62 6c 65 73 20 6f 72 20 64 69 73 61 enables or disa
12c00 62 6c 65 73 20 74 68 65 0a 2a 2a 20 5b 65 78 74 bles the.** [ext
12c10 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63 6f 64 ended result cod
12c20 65 73 5d 20 66 65 61 74 75 72 65 20 6f 66 20 53 es] feature of S
12c30 51 4c 69 74 65 2e 20 54 68 65 20 65 78 74 65 6e QLite. The exten
12c40 64 65 64 20 72 65 73 75 6c 74 0a 2a 2a 20 63 6f ded result.** co
12c50 64 65 73 20 61 72 65 20 64 69 73 61 62 6c 65 64 des are disabled
12c60 20 62 79 20 64 65 66 61 75 6c 74 20 66 6f 72 20 by default for
12c70 68 69 73 74 6f 72 69 63 61 6c 20 63 6f 6d 70 61 historical compa
12c80 74 69 62 69 6c 69 74 79 20 63 6f 6e 73 69 64 65 tibility conside
12c90 72 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 52 rations..**.** R
12ca0 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 equirements:.**
12cb0 5b 48 31 32 32 30 31 5d 20 5b 48 31 32 32 30 32 [H12201] [H12202
12cc0 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ].*/.SQLITE_API
12cd0 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 78 74 65 int sqlite3_exte
12ce0 6e 64 65 64 5f 72 65 73 75 6c 74 5f 63 6f 64 65 nded_result_code
12cf0 73 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 20 s(sqlite3*, int
12d00 6f 6e 6f 66 66 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 onoff);../*.** C
12d10 41 50 49 33 52 45 46 3a 20 4c 61 73 74 20 49 6e API3REF: Last In
12d20 73 65 72 74 20 52 6f 77 69 64 20 7b 48 31 32 32 sert Rowid {H122
12d30 32 30 7d 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 0a 20} <S10700>.**.
12d40 2a 2a 20 45 61 63 68 20 65 6e 74 72 79 20 69 6e ** Each entry in
12d50 20 61 6e 20 53 51 4c 69 74 65 20 74 61 62 6c 65 an SQLite table
12d60 20 68 61 73 20 61 20 75 6e 69 71 75 65 20 36 34 has a unique 64
12d70 2d 62 69 74 20 73 69 67 6e 65 64 0a 2a 2a 20 69 -bit signed.** i
12d80 6e 74 65 67 65 72 20 6b 65 79 20 63 61 6c 6c 65 nteger key calle
12d90 64 20 74 68 65 20 5b 52 4f 57 49 44 20 7c 20 22 d the [ROWID | "
12da0 72 6f 77 69 64 22 5d 2e 20 54 68 65 20 72 6f 77 rowid"]. The row
12db0 69 64 20 69 73 20 61 6c 77 61 79 73 20 61 76 61 id is always ava
12dc0 69 6c 61 62 6c 65 0a 2a 2a 20 61 73 20 61 6e 20 ilable.** as an
12dd0 75 6e 64 65 63 6c 61 72 65 64 20 63 6f 6c 75 6d undeclared colum
12de0 6e 20 6e 61 6d 65 64 20 52 4f 57 49 44 2c 20 4f n named ROWID, O
12df0 49 44 2c 20 6f 72 20 5f 52 4f 57 49 44 5f 20 61 ID, or _ROWID_ a
12e00 73 20 6c 6f 6e 67 20 61 73 20 74 68 6f 73 65 0a s long as those.
12e10 2a 2a 20 6e 61 6d 65 73 20 61 72 65 20 6e 6f 74 ** names are not
12e20 20 61 6c 73 6f 20 75 73 65 64 20 62 79 20 65 78 also used by ex
12e30 70 6c 69 63 69 74 6c 79 20 64 65 63 6c 61 72 65 plicitly declare
12e40 64 20 63 6f 6c 75 6d 6e 73 2e 20 49 66 0a 2a 2a d columns. If.**
12e50 20 74 68 65 20 74 61 62 6c 65 20 68 61 73 20 61 the table has a
12e60 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 79 70 65 20 column of type
12e70 5b 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 [INTEGER PRIMARY
12e80 20 4b 45 59 5d 20 74 68 65 6e 20 74 68 61 74 20 KEY] then that
12e90 63 6f 6c 75 6d 6e 0a 2a 2a 20 69 73 20 61 6e 6f column.** is ano
12ea0 74 68 65 72 20 61 6c 69 61 73 20 66 6f 72 20 74 ther alias for t
12eb0 68 65 20 72 6f 77 69 64 2e 0a 2a 2a 0a 2a 2a 20 he rowid..**.**
12ec0 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 This routine ret
12ed0 75 72 6e 73 20 74 68 65 20 5b 72 6f 77 69 64 5d urns the [rowid]
12ee0 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63 of the most rec
12ef0 65 6e 74 0a 2a 2a 20 73 75 63 63 65 73 73 66 75 ent.** successfu
12f00 6c 20 5b 49 4e 53 45 52 54 5d 20 69 6e 74 6f 20 l [INSERT] into
12f10 74 68 65 20 64 61 74 61 62 61 73 65 20 66 72 6f the database fro
12f20 6d 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 m the [database
12f30 63 6f 6e 6e 65 63 74 69 6f 6e 5d 0a 2a 2a 20 69 connection].** i
12f40 6e 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 n the first argu
12f50 6d 65 6e 74 2e 20 20 49 66 20 6e 6f 20 73 75 63 ment. If no suc
12f60 63 65 73 73 66 75 6c 20 5b 49 4e 53 45 52 54 5d cessful [INSERT]
12f70 73 0a 2a 2a 20 68 61 76 65 20 65 76 65 72 20 6f s.** have ever o
12f80 63 63 75 72 72 65 64 20 6f 6e 20 74 68 61 74 20 ccurred on that
12f90 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
12fa0 69 6f 6e 2c 20 7a 65 72 6f 20 69 73 20 72 65 74 ion, zero is ret
12fb0 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 urned..**.** If
12fc0 61 6e 20 5b 49 4e 53 45 52 54 5d 20 6f 63 63 75 an [INSERT] occu
12fd0 72 73 20 77 69 74 68 69 6e 20 61 20 74 72 69 67 rs within a trig
12fe0 67 65 72 2c 20 74 68 65 6e 20 74 68 65 20 5b 72 ger, then the [r
12ff0 6f 77 69 64 5d 20 6f 66 20 74 68 65 20 69 6e 73 owid] of the ins
13000 65 72 74 65 64 0a 2a 2a 20 72 6f 77 20 69 73 20 erted.** row is
13010 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 69 73 returned by this
13020 20 72 6f 75 74 69 6e 65 20 61 73 20 6c 6f 6e 67 routine as long
13030 20 61 73 20 74 68 65 20 74 72 69 67 67 65 72 20 as the trigger
13040 69 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a 2a 20 42 is running..** B
13050 75 74 20 6f 6e 63 65 20 74 68 65 20 74 72 69 67 ut once the trig
13060 67 65 72 20 74 65 72 6d 69 6e 61 74 65 73 2c 20 ger terminates,
13070 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e the value return
13080 65 64 20 62 79 20 74 68 69 73 20 72 6f 75 74 69 ed by this routi
13090 6e 65 0a 2a 2a 20 72 65 76 65 72 74 73 20 74 6f ne.** reverts to
130a0 20 74 68 65 20 6c 61 73 74 20 76 61 6c 75 65 20 the last value
130b0 69 6e 73 65 72 74 65 64 20 62 65 66 6f 72 65 20 inserted before
130c0 74 68 65 20 74 72 69 67 67 65 72 20 66 69 72 65 the trigger fire
130d0 64 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 5b 49 4e 53 d..**.** An [INS
130e0 45 52 54 5d 20 74 68 61 74 20 66 61 69 6c 73 20 ERT] that fails
130f0 64 75 65 20 74 6f 20 61 20 63 6f 6e 73 74 72 61 due to a constra
13100 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 20 69 73 int violation is
13110 20 6e 6f 74 20 61 0a 2a 2a 20 73 75 63 63 65 73 not a.** succes
13120 73 66 75 6c 20 5b 49 4e 53 45 52 54 5d 20 61 6e sful [INSERT] an
13130 64 20 64 6f 65 73 20 6e 6f 74 20 63 68 61 6e 67 d does not chang
13140 65 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 e the value retu
13150 72 6e 65 64 20 62 79 20 74 68 69 73 0a 2a 2a 20 rned by this.**
13160 72 6f 75 74 69 6e 65 2e 20 20 54 68 75 73 20 49 routine. Thus I
13170 4e 53 45 52 54 20 4f 52 20 46 41 49 4c 2c 20 49 NSERT OR FAIL, I
13180 4e 53 45 52 54 20 4f 52 20 49 47 4e 4f 52 45 2c NSERT OR IGNORE,
13190 20 49 4e 53 45 52 54 20 4f 52 20 52 4f 4c 4c 42 INSERT OR ROLLB
131a0 41 43 4b 2c 0a 2a 2a 20 61 6e 64 20 49 4e 53 45 ACK,.** and INSE
131b0 52 54 20 4f 52 20 41 42 4f 52 54 20 6d 61 6b 65 RT OR ABORT make
131c0 20 6e 6f 20 63 68 61 6e 67 65 73 20 74 6f 20 74 no changes to t
131d0 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 he return value
131e0 6f 66 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 of this.** routi
131f0 6e 65 20 77 68 65 6e 20 74 68 65 69 72 20 69 6e ne when their in
13200 73 65 72 74 69 6f 6e 20 66 61 69 6c 73 2e 20 20 sertion fails.
13210 57 68 65 6e 20 49 4e 53 45 52 54 20 4f 52 20 52 When INSERT OR R
13220 45 50 4c 41 43 45 0a 2a 2a 20 65 6e 63 6f 75 6e EPLACE.** encoun
13230 74 65 72 73 20 61 20 63 6f 6e 73 74 72 61 69 6e ters a constrain
13240 74 20 76 69 6f 6c 61 74 69 6f 6e 2c 20 69 74 20 t violation, it
13250 64 6f 65 73 20 6e 6f 74 20 66 61 69 6c 2e 20 20 does not fail.
13260 54 68 65 0a 2a 2a 20 49 4e 53 45 52 54 20 63 6f The.** INSERT co
13270 6e 74 69 6e 75 65 73 20 74 6f 20 63 6f 6d 70 6c ntinues to compl
13280 65 74 69 6f 6e 20 61 66 74 65 72 20 64 65 6c 65 etion after dele
13290 74 69 6e 67 20 72 6f 77 73 20 74 68 61 74 20 63 ting rows that c
132a0 61 75 73 65 64 0a 2a 2a 20 74 68 65 20 63 6f 6e aused.** the con
132b0 73 74 72 61 69 6e 74 20 70 72 6f 62 6c 65 6d 20 straint problem
132c0 73 6f 20 49 4e 53 45 52 54 20 4f 52 20 52 45 50 so INSERT OR REP
132d0 4c 41 43 45 20 77 69 6c 6c 20 61 6c 77 61 79 73 LACE will always
132e0 20 63 68 61 6e 67 65 0a 2a 2a 20 74 68 65 20 72 change.** the r
132f0 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 74 eturn value of t
13300 68 69 73 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a his interface..*
13310 2a 0a 2a 2a 20 46 6f 72 20 74 68 65 20 70 75 72 *.** For the pur
13320 70 6f 73 65 73 20 6f 66 20 74 68 69 73 20 72 6f poses of this ro
13330 75 74 69 6e 65 2c 20 61 6e 20 5b 49 4e 53 45 52 utine, an [INSER
13340 54 5d 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64 T] is considered
13350 20 74 6f 0a 2a 2a 20 62 65 20 73 75 63 63 65 73 to.** be succes
13360 73 66 75 6c 20 65 76 65 6e 20 69 66 20 69 74 20 sful even if it
13370 69 73 20 73 75 62 73 65 71 75 65 6e 74 6c 79 20 is subsequently
13380 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a 0a rolled back..**.
13390 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a ** Requirements:
133a0 0a 2a 2a 20 5b 48 31 32 32 32 31 5d 20 5b 48 31 .** [H12221] [H1
133b0 32 32 32 33 5d 0a 2a 2a 0a 2a 2a 20 49 66 20 61 2223].**.** If a
133c0 20 73 65 70 61 72 61 74 65 20 74 68 72 65 61 64 separate thread
133d0 20 70 65 72 66 6f 72 6d 73 20 61 20 6e 65 77 20 performs a new
133e0 5b 49 4e 53 45 52 54 5d 20 6f 6e 20 74 68 65 20 [INSERT] on the
133f0 73 61 6d 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 same.** database
13400 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 68 69 6c connection whil
13410 65 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6c e the [sqlite3_l
13420 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 ast_insert_rowid
13430 28 29 5d 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 ()].** function
13440 69 73 20 72 75 6e 6e 69 6e 67 20 61 6e 64 20 74 is running and t
13450 68 75 73 20 63 68 61 6e 67 65 73 20 74 68 65 20 hus changes the
13460 6c 61 73 74 20 69 6e 73 65 72 74 20 5b 72 6f 77 last insert [row
13470 69 64 5d 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 id],.** then the
13480 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 value returned
13490 62 79 20 5b 73 71 6c 69 74 65 33 5f 6c 61 73 74 by [sqlite3_last
134a0 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 29 5d _insert_rowid()]
134b0 20 69 73 0a 2a 2a 20 75 6e 70 72 65 64 69 63 74 is.** unpredict
134c0 61 62 6c 65 20 61 6e 64 20 6d 69 67 68 74 20 6e able and might n
134d0 6f 74 20 65 71 75 61 6c 20 65 69 74 68 65 72 20 ot equal either
134e0 74 68 65 20 6f 6c 64 20 6f 72 20 74 68 65 20 6e the old or the n
134f0 65 77 0a 2a 2a 20 6c 61 73 74 20 69 6e 73 65 72 ew.** last inser
13500 74 20 5b 72 6f 77 69 64 5d 2e 0a 2a 2f 0a 53 51 t [rowid]..*/.SQ
13510 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 LITE_API sqlite3
13520 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 6c _int64 sqlite3_l
13530 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 ast_insert_rowid
13540 28 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a (sqlite3*);../*.
13550 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6f 75 ** CAPI3REF: Cou
13560 6e 74 20 54 68 65 20 4e 75 6d 62 65 72 20 4f 66 nt The Number Of
13570 20 52 6f 77 73 20 4d 6f 64 69 66 69 65 64 20 7b Rows Modified {
13580 48 31 32 32 34 30 7d 20 3c 53 31 30 36 30 30 3e H12240} <S10600>
13590 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 .**.** This func
135a0 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65 tion returns the
135b0 20 6e 75 6d 62 65 72 20 6f 66 20 64 61 74 61 62 number of datab
135c0 61 73 65 20 72 6f 77 73 20 74 68 61 74 20 77 65 ase rows that we
135d0 72 65 20 63 68 61 6e 67 65 64 0a 2a 2a 20 6f 72 re changed.** or
135e0 20 69 6e 73 65 72 74 65 64 20 6f 72 20 64 65 6c inserted or del
135f0 65 74 65 64 20 62 79 20 74 68 65 20 6d 6f 73 74 eted by the most
13600 20 72 65 63 65 6e 74 6c 79 20 63 6f 6d 70 6c 65 recently comple
13610 74 65 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e ted SQL statemen
13620 74 0a 2a 2a 20 6f 6e 20 74 68 65 20 5b 64 61 74 t.** on the [dat
13630 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
13640 5d 20 73 70 65 63 69 66 69 65 64 20 62 79 20 74 ] specified by t
13650 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 he first paramet
13660 65 72 2e 0a 2a 2a 20 4f 6e 6c 79 20 63 68 61 6e er..** Only chan
13670 67 65 73 20 74 68 61 74 20 61 72 65 20 64 69 72 ges that are dir
13680 65 63 74 6c 79 20 73 70 65 63 69 66 69 65 64 20 ectly specified
13690 62 79 20 74 68 65 20 5b 49 4e 53 45 52 54 5d 2c by the [INSERT],
136a0 20 5b 55 50 44 41 54 45 5d 2c 0a 2a 2a 20 6f 72 [UPDATE],.** or
136b0 20 5b 44 45 4c 45 54 45 5d 20 73 74 61 74 65 6d [DELETE] statem
136c0 65 6e 74 20 61 72 65 20 63 6f 75 6e 74 65 64 2e ent are counted.
136d0 20 20 41 75 78 69 6c 69 61 72 79 20 63 68 61 6e Auxiliary chan
136e0 67 65 73 20 63 61 75 73 65 64 20 62 79 0a 2a 2a ges caused by.**
136f0 20 74 72 69 67 67 65 72 73 20 6f 72 20 5b 66 6f triggers or [fo
13700 72 65 69 67 6e 20 6b 65 79 20 61 63 74 69 6f 6e reign key action
13710 73 5d 20 61 72 65 20 6e 6f 74 20 63 6f 75 6e 74 s] are not count
13720 65 64 2e 20 55 73 65 20 74 68 65 0a 2a 2a 20 5b ed. Use the.** [
13730 73 71 6c 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68 sqlite3_total_ch
13740 61 6e 67 65 73 28 29 5d 20 66 75 6e 63 74 69 6f anges()] functio
13750 6e 20 74 6f 20 66 69 6e 64 20 74 68 65 20 74 6f n to find the to
13760 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 tal number of ch
13770 61 6e 67 65 73 0a 2a 2a 20 69 6e 63 6c 75 64 69 anges.** includi
13780 6e 67 20 63 68 61 6e 67 65 73 20 63 61 75 73 65 ng changes cause
13790 64 20 62 79 20 74 72 69 67 67 65 72 73 20 61 6e d by triggers an
137a0 64 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 61 63 d foreign key ac
137b0 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 43 68 61 tions..**.** Cha
137c0 6e 67 65 73 20 74 6f 20 61 20 76 69 65 77 20 74 nges to a view t
137d0 68 61 74 20 61 72 65 20 73 69 6d 75 6c 61 74 65 hat are simulate
137e0 64 20 62 79 20 61 6e 20 5b 49 4e 53 54 45 41 44 d by an [INSTEAD
137f0 20 4f 46 20 74 72 69 67 67 65 72 5d 0a 2a 2a 20 OF trigger].**
13800 61 72 65 20 6e 6f 74 20 63 6f 75 6e 74 65 64 2e are not counted.
13810 20 20 4f 6e 6c 79 20 72 65 61 6c 20 74 61 62 6c Only real tabl
13820 65 20 63 68 61 6e 67 65 73 20 61 72 65 20 63 6f e changes are co
13830 75 6e 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 20 22 unted..**.** A "
13840 72 6f 77 20 63 68 61 6e 67 65 22 20 69 73 20 61 row change" is a
13850 20 63 68 61 6e 67 65 20 74 6f 20 61 20 73 69 6e change to a sin
13860 67 6c 65 20 72 6f 77 20 6f 66 20 61 20 73 69 6e gle row of a sin
13870 67 6c 65 20 74 61 62 6c 65 0a 2a 2a 20 63 61 75 gle table.** cau
13880 73 65 64 20 62 79 20 61 6e 20 49 4e 53 45 52 54 sed by an INSERT
13890 2c 20 44 45 4c 45 54 45 2c 20 6f 72 20 55 50 44 , DELETE, or UPD
138a0 41 54 45 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 ATE statement.
138b0 52 6f 77 73 20 74 68 61 74 0a 2a 2a 20 61 72 65 Rows that.** are
138c0 20 63 68 61 6e 67 65 64 20 61 73 20 73 69 64 65 changed as side
138d0 20 65 66 66 65 63 74 73 20 6f 66 20 5b 52 45 50 effects of [REP
138e0 4c 41 43 45 5d 20 63 6f 6e 73 74 72 61 69 6e 74 LACE] constraint
138f0 20 72 65 73 6f 6c 75 74 69 6f 6e 2c 0a 2a 2a 20 resolution,.**
13900 72 6f 6c 6c 62 61 63 6b 2c 20 41 42 4f 52 54 20 rollback, ABORT
13910 70 72 6f 63 65 73 73 69 6e 67 2c 20 5b 44 52 4f processing, [DRO
13920 50 20 54 41 42 4c 45 5d 2c 20 6f 72 20 62 79 20 P TABLE], or by
13930 61 6e 79 20 6f 74 68 65 72 0a 2a 2a 20 6d 65 63 any other.** mec
13940 68 61 6e 69 73 6d 73 20 64 6f 20 6e 6f 74 20 63 hanisms do not c
13950 6f 75 6e 74 20 61 73 20 64 69 72 65 63 74 20 72 ount as direct r
13960 6f 77 20 63 68 61 6e 67 65 73 2e 0a 2a 2a 0a 2a ow changes..**.*
13970 2a 20 41 20 22 74 72 69 67 67 65 72 20 63 6f 6e * A "trigger con
13980 74 65 78 74 22 20 69 73 20 61 20 73 63 6f 70 65 text" is a scope
13990 20 6f 66 20 65 78 65 63 75 74 69 6f 6e 20 74 68 of execution th
139a0 61 74 20 62 65 67 69 6e 73 20 61 6e 64 0a 2a 2a at begins and.**
139b0 20 65 6e 64 73 20 77 69 74 68 20 74 68 65 20 73 ends with the s
139c0 63 72 69 70 74 20 6f 66 20 61 20 5b 43 52 45 41 cript of a [CREA
139d0 54 45 20 54 52 49 47 47 45 52 20 7c 20 74 72 69 TE TRIGGER | tri
139e0 67 67 65 72 5d 2e 20 0a 2a 2a 20 4d 6f 73 74 20 gger]. .** Most
139f0 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 61 SQL statements a
13a00 72 65 0a 2a 2a 20 65 76 61 6c 75 61 74 65 64 20 re.** evaluated
13a10 6f 75 74 73 69 64 65 20 6f 66 20 61 6e 79 20 74 outside of any t
13a20 72 69 67 67 65 72 2e 20 20 54 68 69 73 20 69 73 rigger. This is
13a30 20 74 68 65 20 22 74 6f 70 20 6c 65 76 65 6c 22 the "top level"
13a40 0a 2a 2a 20 74 72 69 67 67 65 72 20 63 6f 6e 74 .** trigger cont
13a50 65 78 74 2e 20 20 49 66 20 61 20 74 72 69 67 67 ext. If a trigg
13a60 65 72 20 66 69 72 65 73 20 66 72 6f 6d 20 74 68 er fires from th
13a70 65 20 74 6f 70 20 6c 65 76 65 6c 2c 20 61 0a 2a e top level, a.*
13a80 2a 20 6e 65 77 20 74 72 69 67 67 65 72 20 63 6f * new trigger co
13a90 6e 74 65 78 74 20 69 73 20 65 6e 74 65 72 65 64 ntext is entered
13aa0 20 66 6f 72 20 74 68 65 20 64 75 72 61 74 69 6f for the duratio
13ab0 6e 20 6f 66 20 74 68 61 74 20 6f 6e 65 0a 2a 2a n of that one.**
13ac0 20 74 72 69 67 67 65 72 2e 20 20 53 75 62 74 72 trigger. Subtr
13ad0 69 67 67 65 72 73 20 63 72 65 61 74 65 20 73 75 iggers create su
13ae0 62 63 6f 6e 74 65 78 74 73 20 66 6f 72 20 74 68 bcontexts for th
13af0 65 69 72 20 64 75 72 61 74 69 6f 6e 2e 0a 2a 2a eir duration..**
13b00 0a 2a 2a 20 43 61 6c 6c 69 6e 67 20 5b 73 71 6c .** Calling [sql
13b10 69 74 65 33 5f 65 78 65 63 28 29 5d 20 6f 72 20 ite3_exec()] or
13b20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d [sqlite3_step()]
13b30 20 72 65 63 75 72 73 69 76 65 6c 79 20 64 6f 65 recursively doe
13b40 73 0a 2a 2a 20 6e 6f 74 20 63 72 65 61 74 65 20 s.** not create
13b50 61 20 6e 65 77 20 74 72 69 67 67 65 72 20 63 6f a new trigger co
13b60 6e 74 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 ntext..**.** Thi
13b70 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 s function retur
13b80 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 ns the number of
13b90 20 64 69 72 65 63 74 20 72 6f 77 20 63 68 61 6e direct row chan
13ba0 67 65 73 20 69 6e 20 74 68 65 0a 2a 2a 20 6d 6f ges in the.** mo
13bb0 73 74 20 72 65 63 65 6e 74 20 49 4e 53 45 52 54 st recent INSERT
13bc0 2c 20 55 50 44 41 54 45 2c 20 6f 72 20 44 45 4c , UPDATE, or DEL
13bd0 45 54 45 20 73 74 61 74 65 6d 65 6e 74 20 77 69 ETE statement wi
13be0 74 68 69 6e 20 74 68 65 20 73 61 6d 65 0a 2a 2a thin the same.**
13bf0 20 74 72 69 67 67 65 72 20 63 6f 6e 74 65 78 74 trigger context
13c00 2e 0a 2a 2a 0a 2a 2a 20 54 68 75 73 2c 20 77 68 ..**.** Thus, wh
13c10 65 6e 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 74 en called from t
13c20 68 65 20 74 6f 70 20 6c 65 76 65 6c 2c 20 74 68 he top level, th
13c30 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 is function retu
13c40 72 6e 73 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 rns the.** numbe
13c50 72 20 6f 66 20 63 68 61 6e 67 65 73 20 69 6e 20 r of changes in
13c60 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 the most recent
13c70 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45 2c 20 INSERT, UPDATE,
13c80 6f 72 20 44 45 4c 45 54 45 0a 2a 2a 20 74 68 61 or DELETE.** tha
13c90 74 20 61 6c 73 6f 20 6f 63 63 75 72 72 65 64 20 t also occurred
13ca0 61 74 20 74 68 65 20 74 6f 70 20 6c 65 76 65 6c at the top level
13cb0 2e 20 20 57 69 74 68 69 6e 20 74 68 65 20 62 6f . Within the bo
13cc0 64 79 20 6f 66 20 61 20 74 72 69 67 67 65 72 2c dy of a trigger,
13cd0 0a 2a 2a 20 74 68 65 20 73 71 6c 69 74 65 33 5f .** the sqlite3_
13ce0 63 68 61 6e 67 65 73 28 29 20 69 6e 74 65 72 66 changes() interf
13cf0 61 63 65 20 63 61 6e 20 62 65 20 63 61 6c 6c 65 ace can be calle
13d00 64 20 74 6f 20 66 69 6e 64 20 74 68 65 20 6e 75 d to find the nu
13d10 6d 62 65 72 20 6f 66 0a 2a 2a 20 63 68 61 6e 67 mber of.** chang
13d20 65 73 20 69 6e 20 74 68 65 20 6d 6f 73 74 20 72 es in the most r
13d30 65 63 65 6e 74 6c 79 20 63 6f 6d 70 6c 65 74 65 ecently complete
13d40 64 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45 d INSERT, UPDATE
13d50 2c 20 6f 72 20 44 45 4c 45 54 45 0a 2a 2a 20 73 , or DELETE.** s
13d60 74 61 74 65 6d 65 6e 74 20 77 69 74 68 69 6e 20 tatement within
13d70 74 68 65 20 62 6f 64 79 20 6f 66 20 74 68 65 20 the body of the
13d80 73 61 6d 65 20 74 72 69 67 67 65 72 2e 0a 2a 2a same trigger..**
13d90 20 48 6f 77 65 76 65 72 2c 20 74 68 65 20 6e 75 However, the nu
13da0 6d 62 65 72 20 72 65 74 75 72 6e 65 64 20 64 6f mber returned do
13db0 65 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20 63 es not include c
13dc0 68 61 6e 67 65 73 0a 2a 2a 20 63 61 75 73 65 64 hanges.** caused
13dd0 20 62 79 20 73 75 62 74 72 69 67 67 65 72 73 20 by subtriggers
13de0 73 69 6e 63 65 20 74 68 6f 73 65 20 68 61 76 65 since those have
13df0 20 74 68 65 69 72 20 6f 77 6e 20 63 6f 6e 74 65 their own conte
13e00 78 74 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c xt..**.** See al
13e10 73 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f so the [sqlite3_
13e20 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28 29 5d total_changes()]
13e30 20 69 6e 74 65 72 66 61 63 65 20 61 6e 64 20 74 interface and t
13e40 68 65 0a 2a 2a 20 5b 63 6f 75 6e 74 5f 63 68 61 he.** [count_cha
13e50 6e 67 65 73 20 70 72 61 67 6d 61 5d 2e 0a 2a 2a nges pragma]..**
13e60 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 .** Requirements
13e70 3a 0a 2a 2a 20 5b 48 31 32 32 34 31 5d 20 5b 48 :.** [H12241] [H
13e80 31 32 32 34 33 5d 0a 2a 2a 0a 2a 2a 20 49 66 20 12243].**.** If
13e90 61 20 73 65 70 61 72 61 74 65 20 74 68 72 65 61 a separate threa
13ea0 64 20 6d 61 6b 65 73 20 63 68 61 6e 67 65 73 20 d makes changes
13eb0 6f 6e 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 on the same data
13ec0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a base connection.
13ed0 2a 2a 20 77 68 69 6c 65 20 5b 73 71 6c 69 74 65 ** while [sqlite
13ee0 33 5f 63 68 61 6e 67 65 73 28 29 5d 20 69 73 20 3_changes()] is
13ef0 72 75 6e 6e 69 6e 67 20 74 68 65 6e 20 74 68 65 running then the
13f00 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 0a value returned.
13f10 2a 2a 20 69 73 20 75 6e 70 72 65 64 69 63 74 61 ** is unpredicta
13f20 62 6c 65 20 61 6e 64 20 6e 6f 74 20 6d 65 61 6e ble and not mean
13f30 69 6e 67 66 75 6c 2e 0a 2a 2f 0a 53 51 4c 49 54 ingful..*/.SQLIT
13f40 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
13f50 33 5f 63 68 61 6e 67 65 73 28 73 71 6c 69 74 65 3_changes(sqlite
13f60 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 3*);../*.** CAPI
13f70 33 52 45 46 3a 20 54 6f 74 61 6c 20 4e 75 6d 62 3REF: Total Numb
13f80 65 72 20 4f 66 20 52 6f 77 73 20 4d 6f 64 69 66 er Of Rows Modif
13f90 69 65 64 20 7b 48 31 32 32 36 30 7d 20 3c 53 31 ied {H12260} <S1
13fa0 30 36 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 0600>.**.** This
13fb0 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e function return
13fc0 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 s the number of
13fd0 72 6f 77 20 63 68 61 6e 67 65 73 20 63 61 75 73 row changes caus
13fe0 65 64 20 62 79 20 5b 49 4e 53 45 52 54 5d 2c 0a ed by [INSERT],.
13ff0 2a 2a 20 5b 55 50 44 41 54 45 5d 20 6f 72 20 5b ** [UPDATE] or [
14000 44 45 4c 45 54 45 5d 20 73 74 61 74 65 6d 65 6e DELETE] statemen
14010 74 73 20 73 69 6e 63 65 20 74 68 65 20 5b 64 61 ts since the [da
14020 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
14030 6e 5d 20 77 61 73 20 6f 70 65 6e 65 64 2e 0a 2a n] was opened..*
14040 2a 20 54 68 65 20 63 6f 75 6e 74 20 69 6e 63 6c * The count incl
14050 75 64 65 73 20 61 6c 6c 20 63 68 61 6e 67 65 73 udes all changes
14060 20 66 72 6f 6d 20 61 6c 6c 20 5b 43 52 45 41 54 from all [CREAT
14070 45 20 54 52 49 47 47 45 52 20 7c 20 74 72 69 67 E TRIGGER | trig
14080 67 65 72 5d 20 0a 2a 2a 20 63 6f 6e 74 65 78 74 ger] .** context
14090 73 20 61 6e 64 20 63 68 61 6e 67 65 73 20 6d 61 s and changes ma
140a0 64 65 20 62 79 20 5b 66 6f 72 65 69 67 6e 20 6b de by [foreign k
140b0 65 79 20 61 63 74 69 6f 6e 73 5d 2e 20 48 6f 77 ey actions]. How
140c0 65 76 65 72 2c 0a 2a 2a 20 74 68 65 20 63 6f 75 ever,.** the cou
140d0 6e 74 20 64 6f 65 73 20 6e 6f 74 20 69 6e 63 6c nt does not incl
140e0 75 64 65 20 63 68 61 6e 67 65 73 20 75 73 65 64 ude changes used
140f0 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 5b 52 to implement [R
14100 45 50 4c 41 43 45 5d 20 63 6f 6e 73 74 72 61 69 EPLACE] constrai
14110 6e 74 73 2c 0a 2a 2a 20 64 6f 20 72 6f 6c 6c 62 nts,.** do rollb
14120 61 63 6b 73 20 6f 72 20 41 42 4f 52 54 20 70 72 acks or ABORT pr
14130 6f 63 65 73 73 69 6e 67 2c 20 6f 72 20 5b 44 52 ocessing, or [DR
14140 4f 50 20 54 41 42 4c 45 5d 20 70 72 6f 63 65 73 OP TABLE] proces
14150 73 69 6e 67 2e 20 20 54 68 65 0a 2a 2a 20 63 6f sing. The.** co
14160 75 6e 74 20 64 6f 65 73 20 6e 6f 74 20 69 6e 63 unt does not inc
14170 6c 75 64 65 20 72 6f 77 73 20 6f 66 20 76 69 65 lude rows of vie
14180 77 73 20 74 68 61 74 20 66 69 72 65 20 61 6e 20 ws that fire an
14190 5b 49 4e 53 54 45 41 44 20 4f 46 20 74 72 69 67 [INSTEAD OF trig
141a0 67 65 72 5d 2c 0a 2a 2a 20 74 68 6f 75 67 68 20 ger],.** though
141b0 69 66 20 74 68 65 20 49 4e 53 54 45 41 44 20 4f if the INSTEAD O
141c0 46 20 74 72 69 67 67 65 72 20 6d 61 6b 65 73 20 F trigger makes
141d0 63 68 61 6e 67 65 73 20 6f 66 20 69 74 73 20 6f changes of its o
141e0 77 6e 2c 20 74 68 6f 73 65 20 63 68 61 6e 67 65 wn, those change
141f0 73 20 0a 2a 2a 20 61 72 65 20 63 6f 75 6e 74 65 s .** are counte
14200 64 2e 0a 2a 2a 20 54 68 65 20 63 68 61 6e 67 65 d..** The change
14210 73 20 61 72 65 20 63 6f 75 6e 74 65 64 20 61 73 s are counted as
14220 20 73 6f 6f 6e 20 61 73 20 74 68 65 20 73 74 61 soon as the sta
14230 74 65 6d 65 6e 74 20 74 68 61 74 20 6d 61 6b 65 tement that make
14240 73 20 74 68 65 6d 20 69 73 0a 2a 2a 20 63 6f 6d s them is.** com
14250 70 6c 65 74 65 64 20 28 77 68 65 6e 20 74 68 65 pleted (when the
14260 20 73 74 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c statement handl
14270 65 20 69 73 20 70 61 73 73 65 64 20 74 6f 20 5b e is passed to [
14280 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d sqlite3_reset()]
14290 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f or.** [sqlite3_
142a0 66 69 6e 61 6c 69 7a 65 28 29 5d 29 2e 0a 2a 2a finalize()])..**
142b0 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 74 68 65 .** See also the
142c0 20 5b 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 [sqlite3_change
142d0 73 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20 61 s()] interface a
142e0 6e 64 20 74 68 65 0a 2a 2a 20 5b 63 6f 75 6e 74 nd the.** [count
142f0 5f 63 68 61 6e 67 65 73 20 70 72 61 67 6d 61 5d _changes pragma]
14300 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d ..**.** Requirem
14310 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 32 36 31 ents:.** [H12261
14320 5d 20 5b 48 31 32 32 36 33 5d 0a 2a 2a 0a 2a 2a ] [H12263].**.**
14330 20 49 66 20 61 20 73 65 70 61 72 61 74 65 20 74 If a separate t
14340 68 72 65 61 64 20 6d 61 6b 65 73 20 63 68 61 6e hread makes chan
14350 67 65 73 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 ges on the same
14360 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
14370 69 6f 6e 0a 2a 2a 20 77 68 69 6c 65 20 5b 73 71 ion.** while [sq
14380 6c 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e lite3_total_chan
14390 67 65 73 28 29 5d 20 69 73 20 72 75 6e 6e 69 6e ges()] is runnin
143a0 67 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65 g then the value
143b0 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 69 73 20 .** returned is
143c0 75 6e 70 72 65 64 69 63 74 61 62 6c 65 20 61 6e unpredictable an
143d0 64 20 6e 6f 74 20 6d 65 61 6e 69 6e 67 66 75 6c d not meaningful
143e0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
143f0 69 6e 74 20 73 71 6c 69 74 65 33 5f 74 6f 74 61 int sqlite3_tota
14400 6c 5f 63 68 61 6e 67 65 73 28 73 71 6c 69 74 65 l_changes(sqlite
14410 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 3*);../*.** CAPI
14420 33 52 45 46 3a 20 49 6e 74 65 72 72 75 70 74 20 3REF: Interrupt
14430 41 20 4c 6f 6e 67 2d 52 75 6e 6e 69 6e 67 20 51 A Long-Running Q
14440 75 65 72 79 20 7b 48 31 32 32 37 30 7d 20 3c 53 uery {H12270} <S
14450 33 30 35 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 30500>.**.** Thi
14460 73 20 66 75 6e 63 74 69 6f 6e 20 63 61 75 73 65 s function cause
14470 73 20 61 6e 79 20 70 65 6e 64 69 6e 67 20 64 61 s any pending da
14480 74 61 62 61 73 65 20 6f 70 65 72 61 74 69 6f 6e tabase operation
14490 20 74 6f 20 61 62 6f 72 74 20 61 6e 64 0a 2a 2a to abort and.**
144a0 20 72 65 74 75 72 6e 20 61 74 20 69 74 73 20 65 return at its e
144b0 61 72 6c 69 65 73 74 20 6f 70 70 6f 72 74 75 6e arliest opportun
144c0 69 74 79 2e 20 54 68 69 73 20 72 6f 75 74 69 6e ity. This routin
144d0 65 20 69 73 20 74 79 70 69 63 61 6c 6c 79 0a 2a e is typically.*
144e0 2a 20 63 61 6c 6c 65 64 20 69 6e 20 72 65 73 70 * called in resp
144f0 6f 6e 73 65 20 74 6f 20 61 20 75 73 65 72 20 61 onse to a user a
14500 63 74 69 6f 6e 20 73 75 63 68 20 61 73 20 70 72 ction such as pr
14510 65 73 73 69 6e 67 20 22 43 61 6e 63 65 6c 22 0a essing "Cancel".
14520 2a 2a 20 6f 72 20 43 74 72 6c 2d 43 20 77 68 65 ** or Ctrl-C whe
14530 72 65 20 74 68 65 20 75 73 65 72 20 77 61 6e 74 re the user want
14540 73 20 61 20 6c 6f 6e 67 20 71 75 65 72 79 20 6f s a long query o
14550 70 65 72 61 74 69 6f 6e 20 74 6f 20 68 61 6c 74 peration to halt
14560 0a 2a 2a 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e .** immediately.
14570 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 73 61 66 .**.** It is saf
14580 65 20 74 6f 20 63 61 6c 6c 20 74 68 69 73 20 72 e to call this r
14590 6f 75 74 69 6e 65 20 66 72 6f 6d 20 61 20 74 68 outine from a th
145a0 72 65 61 64 20 64 69 66 66 65 72 65 6e 74 20 66 read different f
145b0 72 6f 6d 20 74 68 65 0a 2a 2a 20 74 68 72 65 61 rom the.** threa
145c0 64 20 74 68 61 74 20 69 73 20 63 75 72 72 65 6e d that is curren
145d0 74 6c 79 20 72 75 6e 6e 69 6e 67 20 74 68 65 20 tly running the
145e0 64 61 74 61 62 61 73 65 20 6f 70 65 72 61 74 69 database operati
145f0 6f 6e 2e 20 20 42 75 74 20 69 74 0a 2a 2a 20 69 on. But it.** i
14600 73 20 6e 6f 74 20 73 61 66 65 20 74 6f 20 63 61 s not safe to ca
14610 6c 6c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 ll this routine
14620 77 69 74 68 20 61 20 5b 64 61 74 61 62 61 73 65 with a [database
14630 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 74 68 61 connection] tha
14640 74 0a 2a 2a 20 69 73 20 63 6c 6f 73 65 64 20 6f t.** is closed o
14650 72 20 6d 69 67 68 74 20 63 6c 6f 73 65 20 62 65 r might close be
14660 66 6f 72 65 20 73 71 6c 69 74 65 33 5f 69 6e 74 fore sqlite3_int
14670 65 72 72 75 70 74 28 29 20 72 65 74 75 72 6e 73 errupt() returns
14680 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 53 51 ..**.** If an SQ
14690 4c 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 76 L operation is v
146a0 65 72 79 20 6e 65 61 72 6c 79 20 66 69 6e 69 73 ery nearly finis
146b0 68 65 64 20 61 74 20 74 68 65 20 74 69 6d 65 20 hed at the time
146c0 77 68 65 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 5f when.** sqlite3_
146d0 69 6e 74 65 72 72 75 70 74 28 29 20 69 73 20 63 interrupt() is c
146e0 61 6c 6c 65 64 2c 20 74 68 65 6e 20 69 74 20 6d alled, then it m
146f0 69 67 68 74 20 6e 6f 74 20 68 61 76 65 20 61 6e ight not have an
14700 20 6f 70 70 6f 72 74 75 6e 69 74 79 0a 2a 2a 20 opportunity.**
14710 74 6f 20 62 65 20 69 6e 74 65 72 72 75 70 74 65 to be interrupte
14720 64 20 61 6e 64 20 6d 69 67 68 74 20 63 6f 6e 74 d and might cont
14730 69 6e 75 65 20 74 6f 20 63 6f 6d 70 6c 65 74 69 inue to completi
14740 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 53 51 4c on..**.** An SQL
14750 20 6f 70 65 72 61 74 69 6f 6e 20 74 68 61 74 20 operation that
14760 69 73 20 69 6e 74 65 72 72 75 70 74 65 64 20 77 is interrupted w
14770 69 6c 6c 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 ill return [SQLI
14780 54 45 5f 49 4e 54 45 52 52 55 50 54 5d 2e 0a 2a TE_INTERRUPT]..*
14790 2a 20 49 66 20 74 68 65 20 69 6e 74 65 72 72 75 * If the interru
147a0 70 74 65 64 20 53 51 4c 20 6f 70 65 72 61 74 69 pted SQL operati
147b0 6f 6e 20 69 73 20 61 6e 20 49 4e 53 45 52 54 2c on is an INSERT,
147c0 20 55 50 44 41 54 45 2c 20 6f 72 20 44 45 4c 45 UPDATE, or DELE
147d0 54 45 0a 2a 2a 20 74 68 61 74 20 69 73 20 69 6e TE.** that is in
147e0 73 69 64 65 20 61 6e 20 65 78 70 6c 69 63 69 74 side an explicit
147f0 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 transaction, th
14800 65 6e 20 74 68 65 20 65 6e 74 69 72 65 20 74 72 en the entire tr
14810 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 77 69 6c ansaction.** wil
14820 6c 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b l be rolled back
14830 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 2e 0a automatically..
14840 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 **.** The sqlite
14850 33 5f 69 6e 74 65 72 72 75 70 74 28 44 29 20 63 3_interrupt(D) c
14860 61 6c 6c 20 69 73 20 69 6e 20 65 66 66 65 63 74 all is in effect
14870 20 75 6e 74 69 6c 20 61 6c 6c 20 63 75 72 72 65 until all curre
14880 6e 74 6c 79 20 72 75 6e 6e 69 6e 67 0a 2a 2a 20 ntly running.**
14890 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 6f SQL statements o
148a0 6e 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e n [database conn
148b0 65 63 74 69 6f 6e 5d 20 44 20 63 6f 6d 70 6c 65 ection] D comple
148c0 74 65 2e 20 20 41 6e 79 20 6e 65 77 20 53 51 4c te. Any new SQL
148d0 20 73 74 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 74 statements.** t
148e0 68 61 74 20 61 72 65 20 73 74 61 72 74 65 64 20 hat are started
148f0 61 66 74 65 72 20 74 68 65 20 73 71 6c 69 74 65 after the sqlite
14900 33 5f 69 6e 74 65 72 72 75 70 74 28 29 20 63 61 3_interrupt() ca
14910 6c 6c 20 61 6e 64 20 62 65 66 6f 72 65 20 74 68 ll and before th
14920 65 20 0a 2a 2a 20 72 75 6e 6e 69 6e 67 20 73 74 e .** running st
14930 61 74 65 6d 65 6e 74 73 20 72 65 61 63 68 65 73 atements reaches
14940 20 7a 65 72 6f 20 61 72 65 20 69 6e 74 65 72 72 zero are interr
14950 75 70 74 65 64 20 61 73 20 69 66 20 74 68 65 79 upted as if they
14960 20 68 61 64 20 62 65 65 6e 0a 2a 2a 20 72 75 6e had been.** run
14970 6e 69 6e 67 20 70 72 69 6f 72 20 74 6f 20 74 68 ning prior to th
14980 65 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 e sqlite3_interr
14990 75 70 74 28 29 20 63 61 6c 6c 2e 20 20 4e 65 77 upt() call. New
149a0 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 0a SQL statements.
149b0 2a 2a 20 74 68 61 74 20 61 72 65 20 73 74 61 72 ** that are star
149c0 74 65 64 20 61 66 74 65 72 20 74 68 65 20 72 75 ted after the ru
149d0 6e 6e 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 20 nning statement
149e0 63 6f 75 6e 74 20 72 65 61 63 68 65 73 20 7a 65 count reaches ze
149f0 72 6f 20 61 72 65 0a 2a 2a 20 6e 6f 74 20 65 66 ro are.** not ef
14a00 66 65 63 74 65 64 20 62 79 20 74 68 65 20 73 71 fected by the sq
14a10 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28 lite3_interrupt(
14a20 29 2e 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 6f 20 )..** A call to
14a30 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 sqlite3_interrup
14a40 74 28 44 29 20 74 68 61 74 20 6f 63 63 75 72 73 t(D) that occurs
14a50 20 77 68 65 6e 20 74 68 65 72 65 20 61 72 65 20 when there are
14a60 6e 6f 20 72 75 6e 6e 69 6e 67 0a 2a 2a 20 53 51 no running.** SQ
14a70 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 69 73 20 L statements is
14a80 61 20 6e 6f 2d 6f 70 20 61 6e 64 20 68 61 73 20 a no-op and has
14a90 6e 6f 20 65 66 66 65 63 74 20 6f 6e 20 53 51 4c no effect on SQL
14aa0 20 73 74 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 74 statements.** t
14ab0 68 61 74 20 61 72 65 20 73 74 61 72 74 65 64 20 hat are started
14ac0 61 66 74 65 72 20 74 68 65 20 73 71 6c 69 74 65 after the sqlite
14ad0 33 5f 69 6e 74 65 72 72 75 70 74 28 29 20 63 61 3_interrupt() ca
14ae0 6c 6c 20 72 65 74 75 72 6e 73 2e 0a 2a 2a 0a 2a ll returns..**.*
14af0 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a * Requirements:.
14b00 2a 2a 20 5b 48 31 32 32 37 31 5d 20 5b 48 31 32 ** [H12271] [H12
14b10 32 37 32 5d 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 272].**.** If th
14b20 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 e database conne
14b30 63 74 69 6f 6e 20 63 6c 6f 73 65 73 20 77 68 69 ction closes whi
14b40 6c 65 20 5b 73 71 6c 69 74 65 33 5f 69 6e 74 65 le [sqlite3_inte
14b50 72 72 75 70 74 28 29 5d 0a 2a 2a 20 69 73 20 72 rrupt()].** is r
14b60 75 6e 6e 69 6e 67 20 74 68 65 6e 20 62 61 64 20 unning then bad
14b70 74 68 69 6e 67 73 20 77 69 6c 6c 20 6c 69 6b 65 things will like
14b80 6c 79 20 68 61 70 70 65 6e 2e 0a 2a 2f 0a 53 51 ly happen..*/.SQ
14b90 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 LITE_API void sq
14ba0 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28 lite3_interrupt(
14bb0 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a sqlite3*);../*.*
14bc0 2a 20 43 41 50 49 33 52 45 46 3a 20 44 65 74 65 * CAPI3REF: Dete
14bd0 72 6d 69 6e 65 20 49 66 20 41 6e 20 53 51 4c 20 rmine If An SQL
14be0 53 74 61 74 65 6d 65 6e 74 20 49 73 20 43 6f 6d Statement Is Com
14bf0 70 6c 65 74 65 20 7b 48 31 30 35 31 30 7d 20 3c plete {H10510} <
14c00 53 37 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 S70200>.**.** Th
14c10 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65 ese routines are
14c20 20 75 73 65 66 75 6c 20 64 75 72 69 6e 67 20 63 useful during c
14c30 6f 6d 6d 61 6e 64 2d 6c 69 6e 65 20 69 6e 70 75 ommand-line inpu
14c40 74 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 69 t to determine i
14c50 66 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 f the.** current
14c60 6c 79 20 65 6e 74 65 72 65 64 20 74 65 78 74 20 ly entered text
14c70 73 65 65 6d 73 20 74 6f 20 66 6f 72 6d 20 61 20 seems to form a
14c80 63 6f 6d 70 6c 65 74 65 20 53 51 4c 20 73 74 61 complete SQL sta
14c90 74 65 6d 65 6e 74 20 6f 72 0a 2a 2a 20 69 66 20 tement or.** if
14ca0 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 70 75 74 additional input
14cb0 20 69 73 20 6e 65 65 64 65 64 20 62 65 66 6f 72 is needed befor
14cc0 65 20 73 65 6e 64 69 6e 67 20 74 68 65 20 74 65 e sending the te
14cd0 78 74 20 69 6e 74 6f 0a 2a 2a 20 53 51 4c 69 74 xt into.** SQLit
14ce0 65 20 66 6f 72 20 70 61 72 73 69 6e 67 2e 20 20 e for parsing.
14cf0 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 72 These routines r
14d00 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 20 69 eturn 1 if the i
14d10 6e 70 75 74 20 73 74 72 69 6e 67 0a 2a 2a 20 61 nput string.** a
14d20 70 70 65 61 72 73 20 74 6f 20 62 65 20 61 20 63 ppears to be a c
14d30 6f 6d 70 6c 65 74 65 20 53 51 4c 20 73 74 61 74 omplete SQL stat
14d40 65 6d 65 6e 74 2e 20 20 41 20 73 74 61 74 65 6d ement. A statem
14d50 65 6e 74 20 69 73 20 6a 75 64 67 65 64 20 74 6f ent is judged to
14d60 20 62 65 0a 2a 2a 20 63 6f 6d 70 6c 65 74 65 20 be.** complete
14d70 69 66 20 69 74 20 65 6e 64 73 20 77 69 74 68 20 if it ends with
14d80 61 20 73 65 6d 69 63 6f 6c 6f 6e 20 74 6f 6b 65 a semicolon toke
14d90 6e 20 61 6e 64 20 69 73 20 6e 6f 74 20 61 20 70 n and is not a p
14da0 72 65 66 69 78 20 6f 66 20 61 0a 2a 2a 20 77 65 refix of a.** we
14db0 6c 6c 2d 66 6f 72 6d 65 64 20 43 52 45 41 54 45 ll-formed CREATE
14dc0 20 54 52 49 47 47 45 52 20 73 74 61 74 65 6d 65 TRIGGER stateme
14dd0 6e 74 2e 20 20 53 65 6d 69 63 6f 6c 6f 6e 73 20 nt. Semicolons
14de0 74 68 61 74 20 61 72 65 20 65 6d 62 65 64 64 65 that are embedde
14df0 64 20 77 69 74 68 69 6e 0a 2a 2a 20 73 74 72 69 d within.** stri
14e00 6e 67 20 6c 69 74 65 72 61 6c 73 20 6f 72 20 71 ng literals or q
14e10 75 6f 74 65 64 20 69 64 65 6e 74 69 66 69 65 72 uoted identifier
14e20 20 6e 61 6d 65 73 20 6f 72 20 63 6f 6d 6d 65 6e names or commen
14e30 74 73 20 61 72 65 20 6e 6f 74 0a 2a 2a 20 69 6e ts are not.** in
14e40 64 65 70 65 6e 64 65 6e 74 20 74 6f 6b 65 6e 73 dependent tokens
14e50 20 28 74 68 65 79 20 61 72 65 20 70 61 72 74 20 (they are part
14e60 6f 66 20 74 68 65 20 74 6f 6b 65 6e 20 69 6e 20 of the token in
14e70 77 68 69 63 68 20 74 68 65 79 20 61 72 65 0a 2a which they are.*
14e80 2a 20 65 6d 62 65 64 64 65 64 29 20 61 6e 64 20 * embedded) and
14e90 74 68 75 73 20 64 6f 20 6e 6f 74 20 63 6f 75 6e thus do not coun
14ea0 74 20 61 73 20 61 20 73 74 61 74 65 6d 65 6e 74 t as a statement
14eb0 20 74 65 72 6d 69 6e 61 74 6f 72 2e 20 20 57 68 terminator. Wh
14ec0 69 74 65 73 70 61 63 65 0a 2a 2a 20 61 6e 64 20 itespace.** and
14ed0 63 6f 6d 6d 65 6e 74 73 20 74 68 61 74 20 66 6f comments that fo
14ee0 6c 6c 6f 77 20 74 68 65 20 66 69 6e 61 6c 20 73 llow the final s
14ef0 65 6d 69 63 6f 6c 6f 6e 20 61 72 65 20 69 67 6e emicolon are ign
14f00 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 ored..**.** Thes
14f10 65 20 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72 e routines retur
14f20 6e 20 30 20 69 66 20 74 68 65 20 73 74 61 74 65 n 0 if the state
14f30 6d 65 6e 74 20 69 73 20 69 6e 63 6f 6d 70 6c 65 ment is incomple
14f40 74 65 2e 20 20 49 66 20 61 0a 2a 2a 20 6d 65 6d te. If a.** mem
14f50 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 ory allocation f
14f60 61 69 6c 73 2c 20 74 68 65 6e 20 53 51 4c 49 54 ails, then SQLIT
14f70 45 5f 4e 4f 4d 45 4d 20 69 73 20 72 65 74 75 72 E_NOMEM is retur
14f80 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 ned..**.** These
14f90 20 72 6f 75 74 69 6e 65 73 20 64 6f 20 6e 6f 74 routines do not
14fa0 20 70 61 72 73 65 20 74 68 65 20 53 51 4c 20 73 parse the SQL s
14fb0 74 61 74 65 6d 65 6e 74 73 20 74 68 75 73 0a 2a tatements thus.*
14fc0 2a 20 77 69 6c 6c 20 6e 6f 74 20 64 65 74 65 63 * will not detec
14fd0 74 20 73 79 6e 74 61 63 74 69 63 61 6c 6c 79 20 t syntactically
14fe0 69 6e 63 6f 72 72 65 63 74 20 53 51 4c 2e 0a 2a incorrect SQL..*
14ff0 2a 0a 2a 2a 20 49 66 20 53 51 4c 69 74 65 20 68 *.** If SQLite h
15000 61 73 20 6e 6f 74 20 62 65 65 6e 20 69 6e 69 74 as not been init
15010 69 61 6c 69 7a 65 64 20 75 73 69 6e 67 20 5b 73 ialized using [s
15020 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a qlite3_initializ
15030 65 28 29 5d 20 70 72 69 6f 72 20 0a 2a 2a 20 74 e()] prior .** t
15040 6f 20 69 6e 76 6f 6b 69 6e 67 20 73 71 6c 69 74 o invoking sqlit
15050 65 33 5f 63 6f 6d 70 6c 65 74 65 31 36 28 29 20 e3_complete16()
15060 74 68 65 6e 20 73 71 6c 69 74 65 33 5f 69 6e 69 then sqlite3_ini
15070 74 69 61 6c 69 7a 65 28 29 20 69 73 20 69 6e 76 tialize() is inv
15080 6f 6b 65 64 0a 2a 2a 20 61 75 74 6f 6d 61 74 69 oked.** automati
15090 63 61 6c 6c 79 20 62 79 20 73 71 6c 69 74 65 33 cally by sqlite3
150a0 5f 63 6f 6d 70 6c 65 74 65 31 36 28 29 2e 20 20 _complete16().
150b0 49 66 20 74 68 61 74 20 69 6e 69 74 69 61 6c 69 If that initiali
150c0 7a 61 74 69 6f 6e 20 66 61 69 6c 73 2c 0a 2a 2a zation fails,.**
150d0 20 74 68 65 6e 20 74 68 65 20 72 65 74 75 72 6e then the return
150e0 20 76 61 6c 75 65 20 66 72 6f 6d 20 73 71 6c 69 value from sqli
150f0 74 65 33 5f 63 6f 6d 70 6c 65 74 65 31 36 28 29 te3_complete16()
15100 20 77 69 6c 6c 20 62 65 20 6e 6f 6e 2d 7a 65 72 will be non-zer
15110 6f 0a 2a 2a 20 72 65 67 61 72 64 6c 65 73 73 20 o.** regardless
15120 6f 66 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f of whether or no
15130 74 20 74 68 65 20 69 6e 70 75 74 20 53 51 4c 20 t the input SQL
15140 69 73 20 63 6f 6d 70 6c 65 74 65 2e 0a 2a 2a 0a is complete..**.
15150 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a ** Requirements:
15160 20 5b 48 31 30 35 31 31 5d 20 5b 48 31 30 35 31 [H10511] [H1051
15170 32 5d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 70 2].**.** The inp
15180 75 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 ut to [sqlite3_c
15190 6f 6d 70 6c 65 74 65 28 29 5d 20 6d 75 73 74 20 omplete()] must
151a0 62 65 20 61 20 7a 65 72 6f 2d 74 65 72 6d 69 6e be a zero-termin
151b0 61 74 65 64 0a 2a 2a 20 55 54 46 2d 38 20 73 74 ated.** UTF-8 st
151c0 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ring..**.** The
151d0 69 6e 70 75 74 20 74 6f 20 5b 73 71 6c 69 74 65 input to [sqlite
151e0 33 5f 63 6f 6d 70 6c 65 74 65 31 36 28 29 5d 20 3_complete16()]
151f0 6d 75 73 74 20 62 65 20 61 20 7a 65 72 6f 2d 74 must be a zero-t
15200 65 72 6d 69 6e 61 74 65 64 0a 2a 2a 20 55 54 46 erminated.** UTF
15210 2d 31 36 20 73 74 72 69 6e 67 20 69 6e 20 6e 61 -16 string in na
15220 74 69 76 65 20 62 79 74 65 20 6f 72 64 65 72 2e tive byte order.
15230 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
15240 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c nt sqlite3_compl
15250 65 74 65 28 63 6f 6e 73 74 20 63 68 61 72 20 2a ete(const char *
15260 73 71 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 sql);.SQLITE_API
15270 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6d int sqlite3_com
15280 70 6c 65 74 65 31 36 28 63 6f 6e 73 74 20 76 6f plete16(const vo
15290 69 64 20 2a 73 71 6c 29 3b 0a 0a 2f 2a 0a 2a 2a id *sql);../*.**
152a0 20 43 41 50 49 33 52 45 46 3a 20 52 65 67 69 73 CAPI3REF: Regis
152b0 74 65 72 20 41 20 43 61 6c 6c 62 61 63 6b 20 54 ter A Callback T
152c0 6f 20 48 61 6e 64 6c 65 20 53 51 4c 49 54 45 5f o Handle SQLITE_
152d0 42 55 53 59 20 45 72 72 6f 72 73 20 7b 48 31 32 BUSY Errors {H12
152e0 33 31 30 7d 20 3c 53 34 30 34 30 30 3e 0a 2a 2a 310} <S40400>.**
152f0 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
15300 20 73 65 74 73 20 61 20 63 61 6c 6c 62 61 63 6b sets a callback
15310 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 6d function that m
15320 69 67 68 74 20 62 65 20 69 6e 76 6f 6b 65 64 20 ight be invoked
15330 77 68 65 6e 65 76 65 72 0a 2a 2a 20 61 6e 20 61 whenever.** an a
15340 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65 20 74 ttempt is made t
15350 6f 20 6f 70 65 6e 20 61 20 64 61 74 61 62 61 73 o open a databas
15360 65 20 74 61 62 6c 65 20 74 68 61 74 20 61 6e 6f e table that ano
15370 74 68 65 72 20 74 68 72 65 61 64 0a 2a 2a 20 6f ther thread.** o
15380 72 20 70 72 6f 63 65 73 73 20 68 61 73 20 6c 6f r process has lo
15390 63 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 cked..**.** If t
153a0 68 65 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b he busy callback
153b0 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 5b is NULL, then [
153c0 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 6f 72 20 SQLITE_BUSY] or
153d0 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 42 4c [SQLITE_IOERR_BL
153e0 4f 43 4b 45 44 5d 0a 2a 2a 20 69 73 20 72 65 74 OCKED].** is ret
153f0 75 72 6e 65 64 20 69 6d 6d 65 64 69 61 74 65 6c urned immediatel
15400 79 20 75 70 6f 6e 20 65 6e 63 6f 75 6e 74 65 72 y upon encounter
15410 69 6e 67 20 74 68 65 20 6c 6f 63 6b 2e 20 49 66 ing the lock. If
15420 20 74 68 65 20 62 75 73 79 20 63 61 6c 6c 62 61 the busy callba
15430 63 6b 0a 2a 2a 20 69 73 20 6e 6f 74 20 4e 55 4c ck.** is not NUL
15440 4c 2c 20 74 68 65 6e 20 74 68 65 20 63 61 6c 6c L, then the call
15450 62 61 63 6b 20 77 69 6c 6c 20 62 65 20 69 6e 76 back will be inv
15460 6f 6b 65 64 20 77 69 74 68 20 74 77 6f 20 61 72 oked with two ar
15470 67 75 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 guments..**.** T
15480 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e he first argumen
15490 74 20 74 6f 20 74 68 65 20 68 61 6e 64 6c 65 72 t to the handler
154a0 20 69 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 is a copy of th
154b0 65 20 76 6f 69 64 2a 20 70 6f 69 6e 74 65 72 20 e void* pointer
154c0 77 68 69 63 68 0a 2a 2a 20 69 73 20 74 68 65 20 which.** is the
154d0 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74 20 74 third argument t
154e0 6f 20 73 71 6c 69 74 65 33 5f 62 75 73 79 5f 68 o sqlite3_busy_h
154f0 61 6e 64 6c 65 72 28 29 2e 20 20 54 68 65 20 73 andler(). The s
15500 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 econd argument t
15510 6f 0a 2a 2a 20 74 68 65 20 68 61 6e 64 6c 65 72 o.** the handler
15520 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 74 68 65 callback is the
15530 20 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 number of times
15540 20 74 68 61 74 20 74 68 65 20 62 75 73 79 20 68 that the busy h
15550 61 6e 64 6c 65 72 20 68 61 73 0a 2a 2a 20 62 65 andler has.** be
15560 65 6e 20 69 6e 76 6f 6b 65 64 20 66 6f 72 20 74 en invoked for t
15570 68 69 73 20 6c 6f 63 6b 69 6e 67 20 65 76 65 6e his locking even
15580 74 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20 62 75 t. If the.** bu
15590 73 79 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 sy callback retu
155a0 72 6e 73 20 30 2c 20 74 68 65 6e 20 6e 6f 20 61 rns 0, then no a
155b0 64 64 69 74 69 6f 6e 61 6c 20 61 74 74 65 6d 70 dditional attemp
155c0 74 73 20 61 72 65 20 6d 61 64 65 20 74 6f 0a 2a ts are made to.*
155d0 2a 20 61 63 63 65 73 73 20 74 68 65 20 64 61 74 * access the dat
155e0 61 62 61 73 65 20 61 6e 64 20 5b 53 51 4c 49 54 abase and [SQLIT
155f0 45 5f 42 55 53 59 5d 20 6f 72 20 5b 53 51 4c 49 E_BUSY] or [SQLI
15600 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 TE_IOERR_BLOCKED
15610 5d 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a ] is returned..*
15620 2a 20 49 66 20 74 68 65 20 63 61 6c 6c 62 61 63 * If the callbac
15630 6b 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 k returns non-ze
15640 72 6f 2c 20 74 68 65 6e 20 61 6e 6f 74 68 65 72 ro, then another
15650 20 61 74 74 65 6d 70 74 0a 2a 2a 20 69 73 20 6d attempt.** is m
15660 61 64 65 20 74 6f 20 6f 70 65 6e 20 74 68 65 20 ade to open the
15670 64 61 74 61 62 61 73 65 20 66 6f 72 20 72 65 61 database for rea
15680 64 69 6e 67 20 61 6e 64 20 74 68 65 20 63 79 63 ding and the cyc
15690 6c 65 20 72 65 70 65 61 74 73 2e 0a 2a 2a 0a 2a le repeats..**.*
156a0 2a 20 54 68 65 20 70 72 65 73 65 6e 63 65 20 6f * The presence o
156b0 66 20 61 20 62 75 73 79 20 68 61 6e 64 6c 65 72 f a busy handler
156c0 20 64 6f 65 73 20 6e 6f 74 20 67 75 61 72 61 6e does not guaran
156d0 74 65 65 20 74 68 61 74 20 69 74 20 77 69 6c 6c tee that it will
156e0 20 62 65 20 69 6e 76 6f 6b 65 64 0a 2a 2a 20 77 be invoked.** w
156f0 68 65 6e 20 74 68 65 72 65 20 69 73 20 6c 6f 63 hen there is loc
15700 6b 20 63 6f 6e 74 65 6e 74 69 6f 6e 2e 20 49 66 k contention. If
15710 20 53 51 4c 69 74 65 20 64 65 74 65 72 6d 69 6e SQLite determin
15720 65 73 20 74 68 61 74 20 69 6e 76 6f 6b 69 6e 67 es that invoking
15730 20 74 68 65 20 62 75 73 79 0a 2a 2a 20 68 61 6e the busy.** han
15740 64 6c 65 72 20 63 6f 75 6c 64 20 72 65 73 75 6c dler could resul
15750 74 20 69 6e 20 61 20 64 65 61 64 6c 6f 63 6b 2c t in a deadlock,
15760 20 69 74 20 77 69 6c 6c 20 67 6f 20 61 68 65 61 it will go ahea
15770 64 20 61 6e 64 20 72 65 74 75 72 6e 20 5b 53 51 d and return [SQ
15780 4c 49 54 45 5f 42 55 53 59 5d 0a 2a 2a 20 6f 72 LITE_BUSY].** or
15790 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 42 [SQLITE_IOERR_B
157a0 4c 4f 43 4b 45 44 5d 20 69 6e 73 74 65 61 64 20 LOCKED] instead
157b0 6f 66 20 69 6e 76 6f 6b 69 6e 67 20 74 68 65 20 of invoking the
157c0 62 75 73 79 20 68 61 6e 64 6c 65 72 2e 0a 2a 2a busy handler..**
157d0 20 43 6f 6e 73 69 64 65 72 20 61 20 73 63 65 6e Consider a scen
157e0 61 72 69 6f 20 77 68 65 72 65 20 6f 6e 65 20 70 ario where one p
157f0 72 6f 63 65 73 73 20 69 73 20 68 6f 6c 64 69 6e rocess is holdin
15800 67 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 74 68 g a read lock th
15810 61 74 0a 2a 2a 20 69 74 20 69 73 20 74 72 79 69 at.** it is tryi
15820 6e 67 20 74 6f 20 70 72 6f 6d 6f 74 65 20 74 6f ng to promote to
15830 20 61 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b a reserved lock
15840 20 61 6e 64 0a 2a 2a 20 61 20 73 65 63 6f 6e 64 and.** a second
15850 20 70 72 6f 63 65 73 73 20 69 73 20 68 6f 6c 64 process is hold
15860 69 6e 67 20 61 20 72 65 73 65 72 76 65 64 20 6c ing a reserved l
15870 6f 63 6b 20 74 68 61 74 20 69 74 20 69 73 20 74 ock that it is t
15880 72 79 69 6e 67 0a 2a 2a 20 74 6f 20 70 72 6f 6d rying.** to prom
15890 6f 74 65 20 74 6f 20 61 6e 20 65 78 63 6c 75 73 ote to an exclus
158a0 69 76 65 20 6c 6f 63 6b 2e 20 20 54 68 65 20 66 ive lock. The f
158b0 69 72 73 74 20 70 72 6f 63 65 73 73 20 63 61 6e irst process can
158c0 6e 6f 74 20 70 72 6f 63 65 65 64 0a 2a 2a 20 62 not proceed.** b
158d0 65 63 61 75 73 65 20 69 74 20 69 73 20 62 6c 6f ecause it is blo
158e0 63 6b 65 64 20 62 79 20 74 68 65 20 73 65 63 6f cked by the seco
158f0 6e 64 20 61 6e 64 20 74 68 65 20 73 65 63 6f 6e nd and the secon
15900 64 20 70 72 6f 63 65 73 73 20 63 61 6e 6e 6f 74 d process cannot
15910 0a 2a 2a 20 70 72 6f 63 65 65 64 20 62 65 63 61 .** proceed beca
15920 75 73 65 20 69 74 20 69 73 20 62 6c 6f 63 6b 65 use it is blocke
15930 64 20 62 79 20 74 68 65 20 66 69 72 73 74 2e 20 d by the first.
15940 20 49 66 20 62 6f 74 68 20 70 72 6f 63 65 73 73 If both process
15950 65 73 0a 2a 2a 20 69 6e 76 6f 6b 65 20 74 68 65 es.** invoke the
15960 20 62 75 73 79 20 68 61 6e 64 6c 65 72 73 2c 20 busy handlers,
15970 6e 65 69 74 68 65 72 20 77 69 6c 6c 20 6d 61 6b neither will mak
15980 65 20 61 6e 79 20 70 72 6f 67 72 65 73 73 2e 20 e any progress.
15990 20 54 68 65 72 65 66 6f 72 65 2c 0a 2a 2a 20 53 Therefore,.** S
159a0 51 4c 69 74 65 20 72 65 74 75 72 6e 73 20 5b 53 QLite returns [S
159b0 51 4c 49 54 45 5f 42 55 53 59 5d 20 66 6f 72 20 QLITE_BUSY] for
159c0 74 68 65 20 66 69 72 73 74 20 70 72 6f 63 65 73 the first proces
159d0 73 2c 20 68 6f 70 69 6e 67 20 74 68 61 74 20 74 s, hoping that t
159e0 68 69 73 0a 2a 2a 20 77 69 6c 6c 20 69 6e 64 75 his.** will indu
159f0 63 65 20 74 68 65 20 66 69 72 73 74 20 70 72 6f ce the first pro
15a00 63 65 73 73 20 74 6f 20 72 65 6c 65 61 73 65 20 cess to release
15a10 69 74 73 20 72 65 61 64 20 6c 6f 63 6b 20 61 6e its read lock an
15a20 64 20 61 6c 6c 6f 77 0a 2a 2a 20 74 68 65 20 73 d allow.** the s
15a30 65 63 6f 6e 64 20 70 72 6f 63 65 73 73 20 74 6f econd process to
15a40 20 70 72 6f 63 65 65 64 2e 0a 2a 2a 0a 2a 2a 20 proceed..**.**
15a50 54 68 65 20 64 65 66 61 75 6c 74 20 62 75 73 79 The default busy
15a60 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 4e 55 4c callback is NUL
15a70 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 53 51 L..**.** The [SQ
15a80 4c 49 54 45 5f 42 55 53 59 5d 20 65 72 72 6f 72 LITE_BUSY] error
15a90 20 69 73 20 63 6f 6e 76 65 72 74 65 64 20 74 6f is converted to
15aa0 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 42 [SQLITE_IOERR_B
15ab0 4c 4f 43 4b 45 44 5d 0a 2a 2a 20 77 68 65 6e 20 LOCKED].** when
15ac0 53 51 4c 69 74 65 20 69 73 20 69 6e 20 74 68 65 SQLite is in the
15ad0 20 6d 69 64 64 6c 65 20 6f 66 20 61 20 6c 61 72 middle of a lar
15ae0 67 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 ge transaction w
15af0 68 65 72 65 20 61 6c 6c 20 74 68 65 0a 2a 2a 20 here all the.**
15b00 63 68 61 6e 67 65 73 20 77 69 6c 6c 20 6e 6f 74 changes will not
15b10 20 66 69 74 20 69 6e 74 6f 20 74 68 65 20 69 6e fit into the in
15b20 2d 6d 65 6d 6f 72 79 20 63 61 63 68 65 2e 20 20 -memory cache.
15b30 53 51 4c 69 74 65 20 77 69 6c 6c 0a 2a 2a 20 61 SQLite will.** a
15b40 6c 72 65 61 64 79 20 68 6f 6c 64 20 61 20 52 45 lready hold a RE
15b50 53 45 52 56 45 44 20 6c 6f 63 6b 20 6f 6e 20 74 SERVED lock on t
15b60 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
15b70 2c 20 62 75 74 20 69 74 20 6e 65 65 64 73 0a 2a , but it needs.*
15b80 2a 20 74 6f 20 70 72 6f 6d 6f 74 65 20 74 68 69 * to promote thi
15b90 73 20 6c 6f 63 6b 20 74 6f 20 45 58 43 4c 55 53 s lock to EXCLUS
15ba0 49 56 45 20 73 6f 20 74 68 61 74 20 69 74 20 63 IVE so that it c
15bb0 61 6e 20 73 70 69 6c 6c 20 63 61 63 68 65 0a 2a an spill cache.*
15bc0 2a 20 70 61 67 65 73 20 69 6e 74 6f 20 74 68 65 * pages into the
15bd0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 77 database file w
15be0 69 74 68 6f 75 74 20 68 61 72 6d 20 74 6f 20 63 ithout harm to c
15bf0 6f 6e 63 75 72 72 65 6e 74 0a 2a 2a 20 72 65 61 oncurrent.** rea
15c00 64 65 72 73 2e 20 20 49 66 20 69 74 20 69 73 20 ders. If it is
15c10 75 6e 61 62 6c 65 20 74 6f 20 70 72 6f 6d 6f 74 unable to promot
15c20 65 20 74 68 65 20 6c 6f 63 6b 2c 20 74 68 65 6e e the lock, then
15c30 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 0a 2a the in-memory.*
15c40 2a 20 63 61 63 68 65 20 77 69 6c 6c 20 62 65 20 * cache will be
15c50 6c 65 66 74 20 69 6e 20 61 6e 20 69 6e 63 6f 6e left in an incon
15c60 73 69 73 74 65 6e 74 20 73 74 61 74 65 20 61 6e sistent state an
15c70 64 20 73 6f 20 74 68 65 20 65 72 72 6f 72 0a 2a d so the error.*
15c80 2a 20 63 6f 64 65 20 69 73 20 70 72 6f 6d 6f 74 * code is promot
15c90 65 64 20 66 72 6f 6d 20 74 68 65 20 72 65 6c 61 ed from the rela
15ca0 74 69 76 65 6c 79 20 62 65 6e 69 67 6e 20 5b 53 tively benign [S
15cb0 51 4c 49 54 45 5f 42 55 53 59 5d 20 74 6f 0a 2a QLITE_BUSY] to.*
15cc0 2a 20 74 68 65 20 6d 6f 72 65 20 73 65 76 65 72 * the more sever
15cd0 65 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f e [SQLITE_IOERR_
15ce0 42 4c 4f 43 4b 45 44 5d 2e 20 20 54 68 69 73 20 BLOCKED]. This
15cf0 65 72 72 6f 72 20 63 6f 64 65 20 70 72 6f 6d 6f error code promo
15d00 74 69 6f 6e 0a 2a 2a 20 66 6f 72 63 65 73 20 61 tion.** forces a
15d10 6e 20 61 75 74 6f 6d 61 74 69 63 20 72 6f 6c 6c n automatic roll
15d20 62 61 63 6b 20 6f 66 20 74 68 65 20 63 68 61 6e back of the chan
15d30 67 65 73 2e 20 20 53 65 65 20 74 68 65 0a 2a 2a ges. See the.**
15d40 20 3c 61 20 68 72 65 66 3d 22 2f 63 76 73 74 72 <a href="/cvstr
15d50 61 63 2f 77 69 6b 69 3f 70 3d 43 6f 72 72 75 70 ac/wiki?p=Corrup
15d60 74 69 6f 6e 46 6f 6c 6c 6f 77 69 6e 67 42 75 73 tionFollowingBus
15d70 79 45 72 72 6f 72 22 3e 0a 2a 2a 20 43 6f 72 72 yError">.** Corr
15d80 75 70 74 69 6f 6e 46 6f 6c 6c 6f 77 69 6e 67 42 uptionFollowingB
15d90 75 73 79 45 72 72 6f 72 3c 2f 61 3e 20 77 69 6b usyError</a> wik
15da0 69 20 70 61 67 65 20 66 6f 72 20 61 20 64 69 73 i page for a dis
15db0 63 75 73 73 69 6f 6e 20 6f 66 20 77 68 79 0a 2a cussion of why.*
15dc0 2a 20 74 68 69 73 20 69 73 20 69 6d 70 6f 72 74 * this is import
15dd0 61 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 ant..**.** There
15de0 20 63 61 6e 20 6f 6e 6c 79 20 62 65 20 61 20 73 can only be a s
15df0 69 6e 67 6c 65 20 62 75 73 79 20 68 61 6e 64 6c ingle busy handl
15e00 65 72 20 64 65 66 69 6e 65 64 20 66 6f 72 20 65 er defined for e
15e10 61 63 68 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65 ach.** [database
15e20 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e 20 20 53 connection]. S
15e30 65 74 74 69 6e 67 20 61 20 6e 65 77 20 62 75 73 etting a new bus
15e40 79 20 68 61 6e 64 6c 65 72 20 63 6c 65 61 72 73 y handler clears
15e50 20 61 6e 79 0a 2a 2a 20 70 72 65 76 69 6f 75 73 any.** previous
15e60 6c 79 20 73 65 74 20 68 61 6e 64 6c 65 72 2e 20 ly set handler.
15e70 20 4e 6f 74 65 20 74 68 61 74 20 63 61 6c 6c 69 Note that calli
15e80 6e 67 20 5b 73 71 6c 69 74 65 33 5f 62 75 73 79 ng [sqlite3_busy
15e90 5f 74 69 6d 65 6f 75 74 28 29 5d 0a 2a 2a 20 77 _timeout()].** w
15ea0 69 6c 6c 20 61 6c 73 6f 20 73 65 74 20 6f 72 20 ill also set or
15eb0 63 6c 65 61 72 20 74 68 65 20 62 75 73 79 20 68 clear the busy h
15ec0 61 6e 64 6c 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 andler..**.** Th
15ed0 65 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 20 e busy callback
15ee0 73 68 6f 75 6c 64 20 6e 6f 74 20 74 61 6b 65 20 should not take
15ef0 61 6e 79 20 61 63 74 69 6f 6e 73 20 77 68 69 63 any actions whic
15f00 68 20 6d 6f 64 69 66 79 20 74 68 65 0a 2a 2a 20 h modify the.**
15f10 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
15f20 69 6f 6e 20 74 68 61 74 20 69 6e 76 6f 6b 65 64 ion that invoked
15f30 20 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65 the busy handle
15f40 72 2e 20 20 41 6e 79 20 73 75 63 68 20 61 63 74 r. Any such act
15f50 69 6f 6e 73 0a 2a 2a 20 72 65 73 75 6c 74 20 69 ions.** result i
15f60 6e 20 75 6e 64 65 66 69 6e 65 64 20 62 65 68 61 n undefined beha
15f70 76 69 6f 72 2e 0a 2a 2a 20 0a 2a 2a 20 52 65 71 vior..** .** Req
15f80 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 uirements:.** [H
15f90 31 32 33 31 31 5d 20 5b 48 31 32 33 31 32 5d 20 12311] [H12312]
15fa0 5b 48 31 32 33 31 34 5d 20 5b 48 31 32 33 31 36 [H12314] [H12316
15fb0 5d 20 5b 48 31 32 33 31 38 5d 0a 2a 2a 0a 2a 2a ] [H12318].**.**
15fc0 20 41 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 A busy handler
15fd0 6d 75 73 74 20 6e 6f 74 20 63 6c 6f 73 65 20 74 must not close t
15fe0 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e he database conn
15ff0 65 63 74 69 6f 6e 0a 2a 2a 20 6f 72 20 5b 70 72 ection.** or [pr
16000 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 epared statement
16010 5d 20 74 68 61 74 20 69 6e 76 6f 6b 65 64 20 74 ] that invoked t
16020 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 2e he busy handler.
16030 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
16040 6e 74 20 73 71 6c 69 74 65 33 5f 62 75 73 79 5f nt sqlite3_busy_
16050 68 61 6e 64 6c 65 72 28 73 71 6c 69 74 65 33 2a handler(sqlite3*
16060 2c 20 69 6e 74 28 2a 29 28 76 6f 69 64 2a 2c 69 , int(*)(void*,i
16070 6e 74 29 2c 20 76 6f 69 64 2a 29 3b 0a 0a 2f 2a nt), void*);../*
16080 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 53 65 .** CAPI3REF: Se
16090 74 20 41 20 42 75 73 79 20 54 69 6d 65 6f 75 74 t A Busy Timeout
160a0 20 7b 48 31 32 33 34 30 7d 20 3c 53 34 30 34 31 {H12340} <S4041
160b0 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 0>.**.** This ro
160c0 75 74 69 6e 65 20 73 65 74 73 20 61 20 5b 73 71 utine sets a [sq
160d0 6c 69 74 65 33 5f 62 75 73 79 5f 68 61 6e 64 6c lite3_busy_handl
160e0 65 72 20 7c 20 62 75 73 79 20 68 61 6e 64 6c 65 er | busy handle
160f0 72 5d 20 74 68 61 74 20 73 6c 65 65 70 73 0a 2a r] that sleeps.*
16100 2a 20 66 6f 72 20 61 20 73 70 65 63 69 66 69 65 * for a specifie
16110 64 20 61 6d 6f 75 6e 74 20 6f 66 20 74 69 6d 65 d amount of time
16120 20 77 68 65 6e 20 61 20 74 61 62 6c 65 20 69 73 when a table is
16130 20 6c 6f 63 6b 65 64 2e 20 20 54 68 65 20 68 61 locked. The ha
16140 6e 64 6c 65 72 0a 2a 2a 20 77 69 6c 6c 20 73 6c ndler.** will sl
16150 65 65 70 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d eep multiple tim
16160 65 73 20 75 6e 74 69 6c 20 61 74 20 6c 65 61 73 es until at leas
16170 74 20 22 6d 73 22 20 6d 69 6c 6c 69 73 65 63 6f t "ms" milliseco
16180 6e 64 73 20 6f 66 20 73 6c 65 65 70 69 6e 67 0a nds of sleeping.
16190 2a 2a 20 68 61 76 65 20 61 63 63 75 6d 75 6c 61 ** have accumula
161a0 74 65 64 2e 20 7b 48 31 32 33 34 33 7d 20 41 66 ted. {H12343} Af
161b0 74 65 72 20 22 6d 73 22 20 6d 69 6c 6c 69 73 65 ter "ms" millise
161c0 63 6f 6e 64 73 20 6f 66 20 73 6c 65 65 70 69 6e conds of sleepin
161d0 67 2c 0a 2a 2a 20 74 68 65 20 68 61 6e 64 6c 65 g,.** the handle
161e0 72 20 72 65 74 75 72 6e 73 20 30 20 77 68 69 63 r returns 0 whic
161f0 68 20 63 61 75 73 65 73 20 5b 73 71 6c 69 74 65 h causes [sqlite
16200 33 5f 73 74 65 70 28 29 5d 20 74 6f 20 72 65 74 3_step()] to ret
16210 75 72 6e 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 42 urn.** [SQLITE_B
16220 55 53 59 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f USY] or [SQLITE_
16230 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 5d 2e 0a IOERR_BLOCKED]..
16240 2a 2a 0a 2a 2a 20 43 61 6c 6c 69 6e 67 20 74 68 **.** Calling th
16250 69 73 20 72 6f 75 74 69 6e 65 20 77 69 74 68 20 is routine with
16260 61 6e 20 61 72 67 75 6d 65 6e 74 20 6c 65 73 73 an argument less
16270 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 than or equal t
16280 6f 20 7a 65 72 6f 0a 2a 2a 20 74 75 72 6e 73 20 o zero.** turns
16290 6f 66 66 20 61 6c 6c 20 62 75 73 79 20 68 61 6e off all busy han
162a0 64 6c 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 dlers..**.** The
162b0 72 65 20 63 61 6e 20 6f 6e 6c 79 20 62 65 20 61 re can only be a
162c0 20 73 69 6e 67 6c 65 20 62 75 73 79 20 68 61 6e single busy han
162d0 64 6c 65 72 20 66 6f 72 20 61 20 70 61 72 74 69 dler for a parti
162e0 63 75 6c 61 72 0a 2a 2a 20 5b 64 61 74 61 62 61 cular.** [databa
162f0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 61 se connection] a
16300 6e 79 20 61 6e 79 20 67 69 76 65 6e 20 6d 6f 6d ny any given mom
16310 65 6e 74 2e 20 20 49 66 20 61 6e 6f 74 68 65 72 ent. If another
16320 20 62 75 73 79 20 68 61 6e 64 6c 65 72 0a 2a 2a busy handler.**
16330 20 77 61 73 20 64 65 66 69 6e 65 64 20 20 28 75 was defined (u
16340 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 62 75 sing [sqlite3_bu
16350 73 79 5f 68 61 6e 64 6c 65 72 28 29 5d 29 20 70 sy_handler()]) p
16360 72 69 6f 72 20 74 6f 20 63 61 6c 6c 69 6e 67 0a rior to calling.
16370 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2c ** this routine,
16380 20 74 68 61 74 20 6f 74 68 65 72 20 62 75 73 79 that other busy
16390 20 68 61 6e 64 6c 65 72 20 69 73 20 63 6c 65 61 handler is clea
163a0 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 red..**.** Requi
163b0 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 rements:.** [H12
163c0 33 34 31 5d 20 5b 48 31 32 33 34 33 5d 20 5b 48 341] [H12343] [H
163d0 31 32 33 34 34 5d 0a 2a 2f 0a 53 51 4c 49 54 45 12344].*/.SQLITE
163e0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
163f0 5f 62 75 73 79 5f 74 69 6d 65 6f 75 74 28 73 71 _busy_timeout(sq
16400 6c 69 74 65 33 2a 2c 20 69 6e 74 20 6d 73 29 3b lite3*, int ms);
16410 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
16420 3a 20 43 6f 6e 76 65 6e 69 65 6e 63 65 20 52 6f : Convenience Ro
16430 75 74 69 6e 65 73 20 46 6f 72 20 52 75 6e 6e 69 utines For Runni
16440 6e 67 20 51 75 65 72 69 65 73 20 7b 48 31 32 33 ng Queries {H123
16450 37 30 7d 20 3c 53 31 30 30 30 30 3e 0a 2a 2a 0a 70} <S10000>.**.
16460 2a 2a 20 44 65 66 69 6e 69 74 69 6f 6e 3a 20 41 ** Definition: A
16470 20 3c 62 3e 72 65 73 75 6c 74 20 74 61 62 6c 65 <b>result table
16480 3c 2f 62 3e 20 69 73 20 6d 65 6d 6f 72 79 20 64 </b> is memory d
16490 61 74 61 20 73 74 72 75 63 74 75 72 65 20 63 72 ata structure cr
164a0 65 61 74 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 eated by the.**
164b0 5b 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 [sqlite3_get_tab
164c0 6c 65 28 29 5d 20 69 6e 74 65 72 66 61 63 65 2e le()] interface.
164d0 20 20 41 20 72 65 73 75 6c 74 20 74 61 62 6c 65 A result table
164e0 20 72 65 63 6f 72 64 73 20 74 68 65 0a 2a 2a 20 records the.**
164f0 63 6f 6d 70 6c 65 74 65 20 71 75 65 72 79 20 72 complete query r
16500 65 73 75 6c 74 73 20 66 72 6f 6d 20 6f 6e 65 20 esults from one
16510 6f 72 20 6d 6f 72 65 20 71 75 65 72 69 65 73 2e or more queries.
16520 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 61 62 6c 65 .**.** The table
16530 20 63 6f 6e 63 65 70 74 75 61 6c 6c 79 20 68 61 conceptually ha
16540 73 20 61 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f s a number of ro
16550 77 73 20 61 6e 64 20 63 6f 6c 75 6d 6e 73 2e 20 ws and columns.
16560 20 42 75 74 0a 2a 2a 20 74 68 65 73 65 20 6e 75 But.** these nu
16570 6d 62 65 72 73 20 61 72 65 20 6e 6f 74 20 70 61 mbers are not pa
16580 72 74 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 rt of the result
16590 20 74 61 62 6c 65 20 69 74 73 65 6c 66 2e 20 20 table itself.
165a0 54 68 65 73 65 0a 2a 2a 20 6e 75 6d 62 65 72 73 These.** numbers
165b0 20 61 72 65 20 6f 62 74 61 69 6e 65 64 20 73 65 are obtained se
165c0 70 61 72 61 74 65 6c 79 2e 20 20 4c 65 74 20 4e parately. Let N
165d0 20 62 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f be the number o
165e0 66 20 72 6f 77 73 0a 2a 2a 20 61 6e 64 20 4d 20 f rows.** and M
165f0 62 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 be the number of
16600 20 63 6f 6c 75 6d 6e 73 2e 0a 2a 2a 0a 2a 2a 20 columns..**.**
16610 41 20 72 65 73 75 6c 74 20 74 61 62 6c 65 20 69 A result table i
16620 73 20 61 6e 20 61 72 72 61 79 20 6f 66 20 70 6f s an array of po
16630 69 6e 74 65 72 73 20 74 6f 20 7a 65 72 6f 2d 74 inters to zero-t
16640 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 20 erminated UTF-8
16650 73 74 72 69 6e 67 73 2e 0a 2a 2a 20 54 68 65 72 strings..** Ther
16660 65 20 61 72 65 20 28 4e 2b 31 29 2a 4d 20 65 6c e are (N+1)*M el
16670 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 61 72 ements in the ar
16680 72 61 79 2e 20 20 54 68 65 20 66 69 72 73 74 20 ray. The first
16690 4d 20 70 6f 69 6e 74 65 72 73 20 70 6f 69 6e 74 M pointers point
166a0 0a 2a 2a 20 74 6f 20 7a 65 72 6f 2d 74 65 72 6d .** to zero-term
166b0 69 6e 61 74 65 64 20 73 74 72 69 6e 67 73 20 74 inated strings t
166c0 68 61 74 20 20 63 6f 6e 74 61 69 6e 20 74 68 65 hat contain the
166d0 20 6e 61 6d 65 73 20 6f 66 20 74 68 65 20 63 6f names of the co
166e0 6c 75 6d 6e 73 2e 0a 2a 2a 20 54 68 65 20 72 65 lumns..** The re
166f0 6d 61 69 6e 69 6e 67 20 65 6e 74 72 69 65 73 20 maining entries
16700 61 6c 6c 20 70 6f 69 6e 74 20 74 6f 20 71 75 65 all point to que
16710 72 79 20 72 65 73 75 6c 74 73 2e 20 20 4e 55 4c ry results. NUL
16720 4c 20 76 61 6c 75 65 73 20 72 65 73 75 6c 74 0a L values result.
16730 2a 2a 20 69 6e 20 4e 55 4c 4c 20 70 6f 69 6e 74 ** in NULL point
16740 65 72 73 2e 20 20 41 6c 6c 20 6f 74 68 65 72 20 ers. All other
16750 76 61 6c 75 65 73 20 61 72 65 20 69 6e 20 74 68 values are in th
16760 65 69 72 20 55 54 46 2d 38 20 7a 65 72 6f 2d 74 eir UTF-8 zero-t
16770 65 72 6d 69 6e 61 74 65 64 0a 2a 2a 20 73 74 72 erminated.** str
16780 69 6e 67 20 72 65 70 72 65 73 65 6e 74 61 74 69 ing representati
16790 6f 6e 20 61 73 20 72 65 74 75 72 6e 65 64 20 62 on as returned b
167a0 79 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d y [sqlite3_colum
167b0 6e 5f 74 65 78 74 28 29 5d 2e 0a 2a 2a 0a 2a 2a n_text()]..**.**
167c0 20 41 20 72 65 73 75 6c 74 20 74 61 62 6c 65 20 A result table
167d0 6d 69 67 68 74 20 63 6f 6e 73 69 73 74 20 6f 66 might consist of
167e0 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6d 65 6d one or more mem
167f0 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e ory allocations.
16800 0a 2a 2a 20 49 74 20 69 73 20 6e 6f 74 20 73 61 .** It is not sa
16810 66 65 20 74 6f 20 70 61 73 73 20 61 20 72 65 73 fe to pass a res
16820 75 6c 74 20 74 61 62 6c 65 20 64 69 72 65 63 74 ult table direct
16830 6c 79 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 66 ly to [sqlite3_f
16840 72 65 65 28 29 5d 2e 0a 2a 2a 20 41 20 72 65 73 ree()]..** A res
16850 75 6c 74 20 74 61 62 6c 65 20 73 68 6f 75 6c 64 ult table should
16860 20 62 65 20 64 65 61 6c 6c 6f 63 61 74 65 64 20 be deallocated
16870 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 66 using [sqlite3_f
16880 72 65 65 5f 74 61 62 6c 65 28 29 5d 2e 0a 2a 2a ree_table()]..**
16890 0a 2a 2a 20 41 73 20 61 6e 20 65 78 61 6d 70 6c .** As an exampl
168a0 65 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 e of the result
168b0 74 61 62 6c 65 20 66 6f 72 6d 61 74 2c 20 73 75 table format, su
168c0 70 70 6f 73 65 20 61 20 71 75 65 72 79 20 72 65 ppose a query re
168d0 73 75 6c 74 0a 2a 2a 20 69 73 20 61 73 20 66 6f sult.** is as fo
168e0 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c llows:.**.** <bl
168f0 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a ockquote><pre>.*
16900 2a 20 20 20 20 20 20 20 20 4e 61 6d 65 20 20 20 * Name
16910 20 20 20 20 20 7c 20 41 67 65 0a 2a 2a 20 20 20 | Age.**
16920 20 20 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d -----------
16930 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 ------------.**
16940 20 20 20 20 20 20 20 41 6c 69 63 65 20 20 20 20 Alice
16950 20 20 20 7c 20 34 33 0a 2a 2a 20 20 20 20 20 20 | 43.**
16960 20 20 42 6f 62 20 20 20 20 20 20 20 20 20 7c 20 Bob |
16970 32 38 0a 2a 2a 20 20 20 20 20 20 20 20 43 69 6e 28.** Cin
16980 64 79 20 20 20 20 20 20 20 7c 20 32 31 0a 2a 2a dy | 21.**
16990 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 </pre></blockqu
169a0 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 ote>.**.** There
169b0 20 61 72 65 20 74 77 6f 20 63 6f 6c 75 6d 6e 20 are two column
169c0 28 4d 3d 3d 32 29 20 61 6e 64 20 74 68 72 65 65 (M==2) and three
169d0 20 72 6f 77 73 20 28 4e 3d 3d 33 29 2e 20 20 54 rows (N==3). T
169e0 68 75 73 20 74 68 65 0a 2a 2a 20 72 65 73 75 6c hus the.** resul
169f0 74 20 74 61 62 6c 65 20 68 61 73 20 38 20 65 6e t table has 8 en
16a00 74 72 69 65 73 2e 20 20 53 75 70 70 6f 73 65 20 tries. Suppose
16a10 74 68 65 20 72 65 73 75 6c 74 20 74 61 62 6c 65 the result table
16a20 20 69 73 20 73 74 6f 72 65 64 0a 2a 2a 20 69 6e is stored.** in
16a30 20 61 6e 20 61 72 72 61 79 20 6e 61 6d 65 73 20 an array names
16a40 61 7a 52 65 73 75 6c 74 2e 20 20 54 68 65 6e 20 azResult. Then
16a50 61 7a 52 65 73 75 6c 74 20 68 6f 6c 64 73 20 74 azResult holds t
16a60 68 69 73 20 63 6f 6e 74 65 6e 74 3a 0a 2a 2a 0a his content:.**.
16a70 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c ** <blockquote><
16a80 70 72 65 3e 0a 2a 2a 20 20 20 20 20 20 20 20 61 pre>.** a
16a90 7a 52 65 73 75 6c 74 26 23 39 31 3b 30 5d 20 3d zResult[0] =
16aa0 20 22 4e 61 6d 65 22 3b 0a 2a 2a 20 20 20 20 20 "Name";.**
16ab0 20 20 20 61 7a 52 65 73 75 6c 74 26 23 39 31 3b azResult[
16ac0 31 5d 20 3d 20 22 41 67 65 22 3b 0a 2a 2a 20 20 1] = "Age";.**
16ad0 20 20 20 20 20 20 61 7a 52 65 73 75 6c 74 26 23 azResult&#
16ae0 39 31 3b 32 5d 20 3d 20 22 41 6c 69 63 65 22 3b 91;2] = "Alice";
16af0 0a 2a 2a 20 20 20 20 20 20 20 20 61 7a 52 65 73 .** azRes
16b00 75 6c 74 26 23 39 31 3b 33 5d 20 3d 20 22 34 33 ult[3] = "43
16b10 22 3b 0a 2a 2a 20 20 20 20 20 20 20 20 61 7a 52 ";.** azR
16b20 65 73 75 6c 74 26 23 39 31 3b 34 5d 20 3d 20 22 esult[4] = "
16b30 42 6f 62 22 3b 0a 2a 2a 20 20 20 20 20 20 20 20 Bob";.**
16b40 61 7a 52 65 73 75 6c 74 26 23 39 31 3b 35 5d 20 azResult[5]
16b50 3d 20 22 32 38 22 3b 0a 2a 2a 20 20 20 20 20 20 = "28";.**
16b60 20 20 61 7a 52 65 73 75 6c 74 26 23 39 31 3b 36 azResult[6
16b70 5d 20 3d 20 22 43 69 6e 64 79 22 3b 0a 2a 2a 20 ] = "Cindy";.**
16b80 20 20 20 20 20 20 20 61 7a 52 65 73 75 6c 74 26 azResult&
16b90 23 39 31 3b 37 5d 20 3d 20 22 32 31 22 3b 0a 2a #91;7] = "21";.*
16ba0 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71 * </pre></blockq
16bb0 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 uote>.**.** The
16bc0 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c sqlite3_get_tabl
16bd0 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 65 76 61 e() function eva
16be0 6c 75 61 74 65 73 20 6f 6e 65 20 6f 72 20 6d 6f luates one or mo
16bf0 72 65 0a 2a 2a 20 73 65 6d 69 63 6f 6c 6f 6e 2d re.** semicolon-
16c00 73 65 70 61 72 61 74 65 64 20 53 51 4c 20 73 74 separated SQL st
16c10 61 74 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 atements in the
16c20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 zero-terminated
16c30 55 54 46 2d 38 0a 2a 2a 20 73 74 72 69 6e 67 20 UTF-8.** string
16c40 6f 66 20 69 74 73 20 32 6e 64 20 70 61 72 61 6d of its 2nd param
16c50 65 74 65 72 2e 20 20 49 74 20 72 65 74 75 72 6e eter. It return
16c60 73 20 61 20 72 65 73 75 6c 74 20 74 61 62 6c 65 s a result table
16c70 20 74 6f 20 74 68 65 0a 2a 2a 20 70 6f 69 6e 74 to the.** point
16c80 65 72 20 67 69 76 65 6e 20 69 6e 20 69 74 73 20 er given in its
16c90 33 72 64 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 3rd parameter..*
16ca0 2a 0a 2a 2a 20 41 66 74 65 72 20 74 68 65 20 63 *.** After the c
16cb0 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 alling function
16cc0 68 61 73 20 66 69 6e 69 73 68 65 64 20 75 73 69 has finished usi
16cd0 6e 67 20 74 68 65 20 72 65 73 75 6c 74 2c 20 69 ng the result, i
16ce0 74 20 73 68 6f 75 6c 64 0a 2a 2a 20 70 61 73 73 t should.** pass
16cf0 20 74 68 65 20 70 6f 69 6e 74 65 72 20 74 6f 20 the pointer to
16d00 74 68 65 20 72 65 73 75 6c 74 20 74 61 62 6c 65 the result table
16d10 20 74 6f 20 73 71 6c 69 74 65 33 5f 66 72 65 65 to sqlite3_free
16d20 5f 74 61 62 6c 65 28 29 20 69 6e 20 6f 72 64 65 _table() in orde
16d30 72 20 74 6f 0a 2a 2a 20 72 65 6c 65 61 73 65 20 r to.** release
16d40 74 68 65 20 6d 65 6d 6f 72 79 20 74 68 61 74 20 the memory that
16d50 77 61 73 20 6d 61 6c 6c 6f 63 65 64 2e 20 20 42 was malloced. B
16d60 65 63 61 75 73 65 20 6f 66 20 74 68 65 20 77 61 ecause of the wa
16d70 79 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 y the.** [sqlite
16d80 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 68 61 70 70 3_malloc()] happ
16d90 65 6e 73 20 77 69 74 68 69 6e 20 73 71 6c 69 74 ens within sqlit
16da0 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 2c 20 e3_get_table(),
16db0 74 68 65 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 66 the calling.** f
16dc0 75 6e 63 74 69 6f 6e 20 6d 75 73 74 20 6e 6f 74 unction must not
16dd0 20 74 72 79 20 74 6f 20 63 61 6c 6c 20 5b 73 71 try to call [sq
16de0 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 20 64 69 lite3_free()] di
16df0 72 65 63 74 6c 79 2e 20 20 4f 6e 6c 79 0a 2a 2a rectly. Only.**
16e00 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 5f 74 [sqlite3_free_t
16e10 61 62 6c 65 28 29 5d 20 69 73 20 61 62 6c 65 20 able()] is able
16e20 74 6f 20 72 65 6c 65 61 73 65 20 74 68 65 20 6d to release the m
16e30 65 6d 6f 72 79 20 70 72 6f 70 65 72 6c 79 20 61 emory properly a
16e40 6e 64 20 73 61 66 65 6c 79 2e 0a 2a 2a 0a 2a 2a nd safely..**.**
16e50 20 54 68 65 20 73 71 6c 69 74 65 33 5f 67 65 74 The sqlite3_get
16e60 5f 74 61 62 6c 65 28 29 20 69 6e 74 65 72 66 61 _table() interfa
16e70 63 65 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 ce is implemente
16e80 64 20 61 73 20 61 20 77 72 61 70 70 65 72 20 61 d as a wrapper a
16e90 72 6f 75 6e 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 round.** [sqlite
16ea0 33 5f 65 78 65 63 28 29 5d 2e 20 20 54 68 65 20 3_exec()]. The
16eb0 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c sqlite3_get_tabl
16ec0 65 28 29 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 e() routine does
16ed0 20 6e 6f 74 20 68 61 76 65 20 61 63 63 65 73 73 not have access
16ee0 0a 2a 2a 20 74 6f 20 61 6e 79 20 69 6e 74 65 72 .** to any inter
16ef0 6e 61 6c 20 64 61 74 61 20 73 74 72 75 63 74 75 nal data structu
16f00 72 65 73 20 6f 66 20 53 51 4c 69 74 65 2e 20 20 res of SQLite.
16f10 49 74 20 75 73 65 73 20 6f 6e 6c 79 20 74 68 65 It uses only the
16f20 20 70 75 62 6c 69 63 0a 2a 2a 20 69 6e 74 65 72 public.** inter
16f30 66 61 63 65 20 64 65 66 69 6e 65 64 20 68 65 72 face defined her
16f40 65 2e 20 20 41 73 20 61 20 63 6f 6e 73 65 71 75 e. As a consequ
16f50 65 6e 63 65 2c 20 65 72 72 6f 72 73 20 74 68 61 ence, errors tha
16f60 74 20 6f 63 63 75 72 20 69 6e 20 74 68 65 0a 2a t occur in the.*
16f70 2a 20 77 72 61 70 70 65 72 20 6c 61 79 65 72 20 * wrapper layer
16f80 6f 75 74 73 69 64 65 20 6f 66 20 74 68 65 20 69 outside of the i
16f90 6e 74 65 72 6e 61 6c 20 5b 73 71 6c 69 74 65 33 nternal [sqlite3
16fa0 5f 65 78 65 63 28 29 5d 20 63 61 6c 6c 20 61 72 _exec()] call ar
16fb0 65 20 6e 6f 74 0a 2a 2a 20 72 65 66 6c 65 63 74 e not.** reflect
16fc0 65 64 20 69 6e 20 73 75 62 73 65 71 75 65 6e 74 ed in subsequent
16fd0 20 63 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 calls to [sqlit
16fe0 65 33 5f 65 72 72 63 6f 64 65 28 29 5d 20 6f 72 e3_errcode()] or
16ff0 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 [sqlite3_errmsg
17000 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 ()]..**.** Requi
17010 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 rements:.** [H12
17020 33 37 31 5d 20 5b 48 31 32 33 37 33 5d 20 5b 48 371] [H12373] [H
17030 31 32 33 37 34 5d 20 5b 48 31 32 33 37 36 5d 20 12374] [H12376]
17040 5b 48 31 32 33 37 39 5d 20 5b 48 31 32 33 38 32 [H12379] [H12382
17050 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ].*/.SQLITE_API
17060 69 6e 74 20 73 71 6c 69 74 65 33 5f 67 65 74 5f int sqlite3_get_
17070 74 61 62 6c 65 28 0a 20 20 73 71 6c 69 74 65 33 table(. sqlite3
17080 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 2f *db, /
17090 2a 20 41 6e 20 6f 70 65 6e 20 64 61 74 61 62 61 * An open databa
170a0 73 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 se */. const ch
170b0 61 72 20 2a 7a 53 71 6c 2c 20 20 20 20 20 2f 2a ar *zSql, /*
170c0 20 53 51 4c 20 74 6f 20 62 65 20 65 76 61 6c 75 SQL to be evalu
170d0 61 74 65 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a ated */. char *
170e0 2a 2a 70 61 7a 52 65 73 75 6c 74 2c 20 20 20 20 **pazResult,
170f0 2f 2a 20 52 65 73 75 6c 74 73 20 6f 66 20 74 68 /* Results of th
17100 65 20 71 75 65 72 79 20 2a 2f 0a 20 20 69 6e 74 e query */. int
17110 20 2a 70 6e 52 6f 77 2c 20 20 20 20 20 20 20 20 *pnRow,
17120 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
17130 72 65 73 75 6c 74 20 72 6f 77 73 20 77 72 69 74 result rows writ
17140 74 65 6e 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e ten here */. in
17150 74 20 2a 70 6e 43 6f 6c 75 6d 6e 2c 20 20 20 20 t *pnColumn,
17160 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
17170 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 73 20 result columns
17180 77 72 69 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a written here */.
17190 20 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 6d 73 char **pzErrms
171a0 67 20 20 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 g /* Error
171b0 20 6d 73 67 20 77 72 69 74 74 65 6e 20 68 65 72 msg written her
171c0 65 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 e */.);.SQLITE_A
171d0 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f PI void sqlite3_
171e0 66 72 65 65 5f 74 61 62 6c 65 28 63 68 61 72 20 free_table(char
171f0 2a 2a 72 65 73 75 6c 74 29 3b 0a 0a 2f 2a 0a 2a **result);../*.*
17200 2a 20 43 41 50 49 33 52 45 46 3a 20 46 6f 72 6d * CAPI3REF: Form
17210 61 74 74 65 64 20 53 74 72 69 6e 67 20 50 72 69 atted String Pri
17220 6e 74 69 6e 67 20 46 75 6e 63 74 69 6f 6e 73 20 nting Functions
17230 7b 48 31 37 34 30 30 7d 20 3c 53 37 30 30 30 30 {H17400} <S70000
17240 3e 3c 53 32 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 ><S20000>.**.**
17250 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 These routines a
17260 72 65 20 77 6f 72 6b 2d 61 6c 69 6b 65 73 20 6f re work-alikes o
17270 66 20 74 68 65 20 22 70 72 69 6e 74 66 28 29 22 f the "printf()"
17280 20 66 61 6d 69 6c 79 20 6f 66 20 66 75 6e 63 74 family of funct
17290 69 6f 6e 73 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 ions.** from the
172a0 20 73 74 61 6e 64 61 72 64 20 43 20 6c 69 62 72 standard C libr
172b0 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 ary..**.** The s
172c0 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 29 qlite3_mprintf()
172d0 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 76 6d 70 and sqlite3_vmp
172e0 72 69 6e 74 66 28 29 20 72 6f 75 74 69 6e 65 73 rintf() routines
172f0 20 77 72 69 74 65 20 74 68 65 69 72 0a 2a 2a 20 write their.**
17300 72 65 73 75 6c 74 73 20 69 6e 74 6f 20 6d 65 6d results into mem
17310 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f ory obtained fro
17320 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f m [sqlite3_mallo
17330 63 28 29 5d 2e 0a 2a 2a 20 54 68 65 20 73 74 72 c()]..** The str
17340 69 6e 67 73 20 72 65 74 75 72 6e 65 64 20 62 79 ings returned by
17350 20 74 68 65 73 65 20 74 77 6f 20 72 6f 75 74 69 these two routi
17360 6e 65 73 20 73 68 6f 75 6c 64 20 62 65 0a 2a 2a nes should be.**
17370 20 72 65 6c 65 61 73 65 64 20 62 79 20 5b 73 71 released by [sq
17380 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 2e 20 20 lite3_free()].
17390 42 6f 74 68 20 72 6f 75 74 69 6e 65 73 20 72 65 Both routines re
173a0 74 75 72 6e 20 61 0a 2a 2a 20 4e 55 4c 4c 20 70 turn a.** NULL p
173b0 6f 69 6e 74 65 72 20 69 66 20 5b 73 71 6c 69 74 ointer if [sqlit
173c0 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 69 73 20 e3_malloc()] is
173d0 75 6e 61 62 6c 65 20 74 6f 20 61 6c 6c 6f 63 61 unable to alloca
173e0 74 65 20 65 6e 6f 75 67 68 0a 2a 2a 20 6d 65 6d te enough.** mem
173f0 6f 72 79 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 ory to hold the
17400 72 65 73 75 6c 74 69 6e 67 20 73 74 72 69 6e 67 resulting string
17410 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 73 71 6c 69 74 ..**.** In sqlit
17420 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 20 72 6f e3_snprintf() ro
17430 75 74 69 6e 65 20 69 73 20 73 69 6d 69 6c 61 72 utine is similar
17440 20 74 6f 20 22 73 6e 70 72 69 6e 74 66 28 29 22 to "snprintf()"
17450 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 73 74 61 from.** the sta
17460 6e 64 61 72 64 20 43 20 6c 69 62 72 61 72 79 2e ndard C library.
17470 20 20 54 68 65 20 72 65 73 75 6c 74 20 69 73 20 The result is
17480 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 written into the
17490 0a 2a 2a 20 62 75 66 66 65 72 20 73 75 70 70 6c .** buffer suppl
174a0 69 65 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e ied as the secon
174b0 64 20 70 61 72 61 6d 65 74 65 72 20 77 68 6f 73 d parameter whos
174c0 65 20 73 69 7a 65 20 69 73 20 67 69 76 65 6e 20 e size is given
174d0 62 79 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 by.** the first
174e0 70 61 72 61 6d 65 74 65 72 2e 20 4e 6f 74 65 20 parameter. Note
174f0 74 68 61 74 20 74 68 65 20 6f 72 64 65 72 20 6f that the order o
17500 66 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 74 f the.** first t
17510 77 6f 20 70 61 72 61 6d 65 74 65 72 73 20 69 73 wo parameters is
17520 20 72 65 76 65 72 73 65 64 20 66 72 6f 6d 20 73 reversed from s
17530 6e 70 72 69 6e 74 66 28 29 2e 20 20 54 68 69 73 nprintf(). This
17540 20 69 73 20 61 6e 0a 2a 2a 20 68 69 73 74 6f 72 is an.** histor
17550 69 63 61 6c 20 61 63 63 69 64 65 6e 74 20 74 68 ical accident th
17560 61 74 20 63 61 6e 6e 6f 74 20 62 65 20 66 69 78 at cannot be fix
17570 65 64 20 77 69 74 68 6f 75 74 20 62 72 65 61 6b ed without break
17580 69 6e 67 0a 2a 2a 20 62 61 63 6b 77 61 72 64 73 ing.** backwards
17590 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 2e 20 compatibility.
175a0 20 4e 6f 74 65 20 61 6c 73 6f 20 74 68 61 74 20 Note also that
175b0 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
175c0 28 29 0a 2a 2a 20 72 65 74 75 72 6e 73 20 61 20 ().** returns a
175d0 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 73 20 62 pointer to its b
175e0 75 66 66 65 72 20 69 6e 73 74 65 61 64 20 6f 66 uffer instead of
175f0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a the number of.*
17600 2a 20 63 68 61 72 61 63 74 65 72 73 20 61 63 74 * characters act
17610 75 61 6c 6c 79 20 77 72 69 74 74 65 6e 20 69 6e ually written in
17620 74 6f 20 74 68 65 20 62 75 66 66 65 72 2e 20 20 to the buffer.
17630 57 65 20 61 64 6d 69 74 20 74 68 61 74 0a 2a 2a We admit that.**
17640 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 the number of c
17650 68 61 72 61 63 74 65 72 73 20 77 72 69 74 74 65 haracters writte
17660 6e 20 77 6f 75 6c 64 20 62 65 20 61 20 6d 6f 72 n would be a mor
17670 65 20 75 73 65 66 75 6c 20 72 65 74 75 72 6e 0a e useful return.
17680 2a 2a 20 76 61 6c 75 65 20 62 75 74 20 77 65 20 ** value but we
17690 63 61 6e 6e 6f 74 20 63 68 61 6e 67 65 20 74 68 cannot change th
176a0 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e e implementation
176b0 20 6f 66 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 of sqlite3_snpr
176c0 69 6e 74 66 28 29 0a 2a 2a 20 6e 6f 77 20 77 69 intf().** now wi
176d0 74 68 6f 75 74 20 62 72 65 61 6b 69 6e 67 20 63 thout breaking c
176e0 6f 6d 70 61 74 69 62 69 6c 69 74 79 2e 0a 2a 2a ompatibility..**
176f0 0a 2a 2a 20 41 73 20 6c 6f 6e 67 20 61 73 20 74 .** As long as t
17700 68 65 20 62 75 66 66 65 72 20 73 69 7a 65 20 69 he buffer size i
17710 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 7a s greater than z
17720 65 72 6f 2c 20 73 71 6c 69 74 65 33 5f 73 6e 70 ero, sqlite3_snp
17730 72 69 6e 74 66 28 29 0a 2a 2a 20 67 75 61 72 61 rintf().** guara
17740 6e 74 65 65 73 20 74 68 61 74 20 74 68 65 20 62 ntees that the b
17750 75 66 66 65 72 20 69 73 20 61 6c 77 61 79 73 20 uffer is always
17760 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 2e zero-terminated.
17770 20 20 54 68 65 20 66 69 72 73 74 0a 2a 2a 20 70 The first.** p
17780 61 72 61 6d 65 74 65 72 20 22 6e 22 20 69 73 20 arameter "n" is
17790 74 68 65 20 74 6f 74 61 6c 20 73 69 7a 65 20 6f the total size o
177a0 66 20 74 68 65 20 62 75 66 66 65 72 2c 20 69 6e f the buffer, in
177b0 63 6c 75 64 69 6e 67 20 73 70 61 63 65 20 66 6f cluding space fo
177c0 72 0a 2a 2a 20 74 68 65 20 7a 65 72 6f 20 74 65 r.** the zero te
177d0 72 6d 69 6e 61 74 6f 72 2e 20 20 53 6f 20 74 68 rminator. So th
177e0 65 20 6c 6f 6e 67 65 73 74 20 73 74 72 69 6e 67 e longest string
177f0 20 74 68 61 74 20 63 61 6e 20 62 65 20 63 6f 6d that can be com
17800 70 6c 65 74 65 6c 79 0a 2a 2a 20 77 72 69 74 74 pletely.** writt
17810 65 6e 20 77 69 6c 6c 20 62 65 20 6e 2d 31 20 63 en will be n-1 c
17820 68 61 72 61 63 74 65 72 73 2e 0a 2a 2a 0a 2a 2a haracters..**.**
17830 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 These routines
17840 61 6c 6c 20 69 6d 70 6c 65 6d 65 6e 74 20 73 6f all implement so
17850 6d 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 66 6f me additional fo
17860 72 6d 61 74 74 69 6e 67 0a 2a 2a 20 6f 70 74 69 rmatting.** opti
17870 6f 6e 73 20 74 68 61 74 20 61 72 65 20 75 73 65 ons that are use
17880 66 75 6c 20 66 6f 72 20 63 6f 6e 73 74 72 75 63 ful for construc
17890 74 69 6e 67 20 53 51 4c 20 73 74 61 74 65 6d 65 ting SQL stateme
178a0 6e 74 73 2e 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 nts..** All of t
178b0 68 65 20 75 73 75 61 6c 20 70 72 69 6e 74 66 28 he usual printf(
178c0 29 20 66 6f 72 6d 61 74 74 69 6e 67 20 6f 70 74 ) formatting opt
178d0 69 6f 6e 73 20 61 70 70 6c 79 2e 20 20 49 6e 20 ions apply. In
178e0 61 64 64 69 74 69 6f 6e 2c 20 74 68 65 72 65 0a addition, there.
178f0 2a 2a 20 69 73 20 61 72 65 20 22 25 71 22 2c 20 ** is are "%q",
17900 22 25 51 22 2c 20 61 6e 64 20 22 25 7a 22 20 6f "%Q", and "%z" o
17910 70 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 ptions..**.** Th
17920 65 20 25 71 20 6f 70 74 69 6f 6e 20 77 6f 72 6b e %q option work
17930 73 20 6c 69 6b 65 20 25 73 20 69 6e 20 74 68 61 s like %s in tha
17940 74 20 69 74 20 73 75 62 73 74 69 74 75 74 65 73 t it substitutes
17950 20 61 20 6e 75 6c 6c 2d 74 65 72 6d 69 6e 61 74 a null-terminat
17960 65 64 0a 2a 2a 20 73 74 72 69 6e 67 20 66 72 6f ed.** string fro
17970 6d 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 6c m the argument l
17980 69 73 74 2e 20 20 42 75 74 20 25 71 20 61 6c 73 ist. But %q als
17990 6f 20 64 6f 75 62 6c 65 73 20 65 76 65 72 79 20 o doubles every
179a0 27 5c 27 27 20 63 68 61 72 61 63 74 65 72 2e 0a '\'' character..
179b0 2a 2a 20 25 71 20 69 73 20 64 65 73 69 67 6e 65 ** %q is designe
179c0 64 20 66 6f 72 20 75 73 65 20 69 6e 73 69 64 65 d for use inside
179d0 20 61 20 73 74 72 69 6e 67 20 6c 69 74 65 72 61 a string litera
179e0 6c 2e 20 20 42 79 20 64 6f 75 62 6c 69 6e 67 20 l. By doubling
179f0 65 61 63 68 20 27 5c 27 27 0a 2a 2a 20 63 68 61 each '\''.** cha
17a00 72 61 63 74 65 72 20 69 74 20 65 73 63 61 70 65 racter it escape
17a10 73 20 74 68 61 74 20 63 68 61 72 61 63 74 65 72 s that character
17a20 20 61 6e 64 20 61 6c 6c 6f 77 73 20 69 74 20 74 and allows it t
17a30 6f 20 62 65 20 69 6e 73 65 72 74 65 64 20 69 6e o be inserted in
17a40 74 6f 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e 67 to.** the string
17a50 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 65 78 61 6d ..**.** For exam
17a60 70 6c 65 2c 20 61 73 73 75 6d 65 20 74 68 65 20 ple, assume the
17a70 73 74 72 69 6e 67 20 76 61 72 69 61 62 6c 65 20 string variable
17a80 7a 54 65 78 74 20 63 6f 6e 74 61 69 6e 73 20 74 zText contains t
17a90 65 78 74 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a ext as follows:.
17aa0 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 **.** <blockquot
17ab0 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20 63 68 61 72 e><pre>.** char
17ac0 20 2a 7a 54 65 78 74 20 3d 20 22 49 74 27 73 20 *zText = "It's
17ad0 61 20 68 61 70 70 79 20 64 61 79 21 22 3b 0a 2a a happy day!";.*
17ae0 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71 * </pre></blockq
17af0 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 4f 6e 65 20 uote>.**.** One
17b00 63 61 6e 20 75 73 65 20 74 68 69 73 20 74 65 78 can use this tex
17b10 74 20 69 6e 20 61 6e 20 53 51 4c 20 73 74 61 74 t in an SQL stat
17b20 65 6d 65 6e 74 20 61 73 20 66 6f 6c 6c 6f 77 73 ement as follows
17b30 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 :.**.** <blockqu
17b40 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20 63 68 ote><pre>.** ch
17b50 61 72 20 2a 7a 53 51 4c 20 3d 20 73 71 6c 69 74 ar *zSQL = sqlit
17b60 65 33 5f 6d 70 72 69 6e 74 66 28 22 49 4e 53 45 e3_mprintf("INSE
17b70 52 54 20 49 4e 54 4f 20 74 61 62 6c 65 20 56 41 RT INTO table VA
17b80 4c 55 45 53 28 27 25 71 27 29 22 2c 20 7a 54 65 LUES('%q')", zTe
17b90 78 74 29 3b 0a 2a 2a 20 20 73 71 6c 69 74 65 33 xt);.** sqlite3
17ba0 5f 65 78 65 63 28 64 62 2c 20 7a 53 51 4c 2c 20 _exec(db, zSQL,
17bb0 30 2c 20 30 2c 20 30 29 3b 0a 2a 2a 20 20 73 71 0, 0, 0);.** sq
17bc0 6c 69 74 65 33 5f 66 72 65 65 28 7a 53 51 4c 29 lite3_free(zSQL)
17bd0 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f ;.** </pre></blo
17be0 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 42 ckquote>.**.** B
17bf0 65 63 61 75 73 65 20 74 68 65 20 25 71 20 66 6f ecause the %q fo
17c00 72 6d 61 74 20 73 74 72 69 6e 67 20 69 73 20 75 rmat string is u
17c10 73 65 64 2c 20 74 68 65 20 27 5c 27 27 20 63 68 sed, the '\'' ch
17c20 61 72 61 63 74 65 72 20 69 6e 20 7a 54 65 78 74 aracter in zText
17c30 0a 2a 2a 20 69 73 20 65 73 63 61 70 65 64 20 61 .** is escaped a
17c40 6e 64 20 74 68 65 20 53 51 4c 20 67 65 6e 65 72 nd the SQL gener
17c50 61 74 65 64 20 69 73 20 61 73 20 66 6f 6c 6c 6f ated is as follo
17c60 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b ws:.**.** <block
17c70 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20 quote><pre>.**
17c80 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 61 62 6c INSERT INTO tabl
17c90 65 31 20 56 41 4c 55 45 53 28 27 49 74 27 27 73 e1 VALUES('It''s
17ca0 20 61 20 68 61 70 70 79 20 64 61 79 21 27 29 0a a happy day!').
17cb0 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b ** </pre></block
17cc0 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 quote>.**.** Thi
17cd0 73 20 69 73 20 63 6f 72 72 65 63 74 2e 20 20 48 s is correct. H
17ce0 61 64 20 77 65 20 75 73 65 64 20 25 73 20 69 6e ad we used %s in
17cf0 73 74 65 61 64 20 6f 66 20 25 71 2c 20 74 68 65 stead of %q, the
17d00 20 67 65 6e 65 72 61 74 65 64 20 53 51 4c 0a 2a generated SQL.*
17d10 2a 20 77 6f 75 6c 64 20 68 61 76 65 20 6c 6f 6f * would have loo
17d20 6b 65 64 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a ked like this:.*
17d30 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 *.** <blockquote
17d40 3e 3c 70 72 65 3e 0a 2a 2a 20 20 49 4e 53 45 52 ><pre>.** INSER
17d50 54 20 49 4e 54 4f 20 74 61 62 6c 65 31 20 56 41 T INTO table1 VA
17d60 4c 55 45 53 28 27 49 74 27 73 20 61 20 68 61 70 LUES('It's a hap
17d70 70 79 20 64 61 79 21 27 29 3b 0a 2a 2a 20 3c 2f py day!');.** </
17d80 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 pre></blockquote
17d90 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 65 63 >.**.** This sec
17da0 6f 6e 64 20 65 78 61 6d 70 6c 65 20 69 73 20 61 ond example is a
17db0 6e 20 53 51 4c 20 73 79 6e 74 61 78 20 65 72 72 n SQL syntax err
17dc0 6f 72 2e 20 20 41 73 20 61 20 67 65 6e 65 72 61 or. As a genera
17dd0 6c 20 72 75 6c 65 20 79 6f 75 20 73 68 6f 75 6c l rule you shoul
17de0 64 0a 2a 2a 20 61 6c 77 61 79 73 20 75 73 65 20 d.** always use
17df0 25 71 20 69 6e 73 74 65 61 64 20 6f 66 20 25 73 %q instead of %s
17e00 20 77 68 65 6e 20 69 6e 73 65 72 74 69 6e 67 20 when inserting
17e10 74 65 78 74 20 69 6e 74 6f 20 61 20 73 74 72 69 text into a stri
17e20 6e 67 20 6c 69 74 65 72 61 6c 2e 0a 2a 2a 0a 2a ng literal..**.*
17e30 2a 20 54 68 65 20 25 51 20 6f 70 74 69 6f 6e 20 * The %Q option
17e40 77 6f 72 6b 73 20 6c 69 6b 65 20 25 71 20 65 78 works like %q ex
17e50 63 65 70 74 20 69 74 20 61 6c 73 6f 20 61 64 64 cept it also add
17e60 73 20 73 69 6e 67 6c 65 20 71 75 6f 74 65 73 20 s single quotes
17e70 61 72 6f 75 6e 64 0a 2a 2a 20 74 68 65 20 6f 75 around.** the ou
17e80 74 73 69 64 65 20 6f 66 20 74 68 65 20 74 6f 74 tside of the tot
17e90 61 6c 20 73 74 72 69 6e 67 2e 20 20 41 64 64 69 al string. Addi
17ea0 74 69 6f 6e 61 6c 6c 79 2c 20 69 66 20 74 68 65 tionally, if the
17eb0 20 70 61 72 61 6d 65 74 65 72 20 69 6e 20 74 68 parameter in th
17ec0 65 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 6c 69 e.** argument li
17ed0 73 74 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 st is a NULL poi
17ee0 6e 74 65 72 2c 20 25 51 20 73 75 62 73 74 69 74 nter, %Q substit
17ef0 75 74 65 73 20 74 68 65 20 74 65 78 74 20 22 4e utes the text "N
17f00 55 4c 4c 22 20 28 77 69 74 68 6f 75 74 0a 2a 2a ULL" (without.**
17f10 20 73 69 6e 67 6c 65 20 71 75 6f 74 65 73 29 20 single quotes)
17f20 69 6e 20 70 6c 61 63 65 20 6f 66 20 74 68 65 20 in place of the
17f30 25 51 20 6f 70 74 69 6f 6e 2e 20 20 53 6f 2c 20 %Q option. So,
17f40 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 6f 6e 65 for example, one
17f50 20 63 6f 75 6c 64 20 73 61 79 3a 0a 2a 2a 0a 2a could say:.**.*
17f60 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 * <blockquote><p
17f70 72 65 3e 0a 2a 2a 20 20 63 68 61 72 20 2a 7a 53 re>.** char *zS
17f80 51 4c 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 QL = sqlite3_mpr
17f90 69 6e 74 66 28 22 49 4e 53 45 52 54 20 49 4e 54 intf("INSERT INT
17fa0 4f 20 74 61 62 6c 65 20 56 41 4c 55 45 53 28 25 O table VALUES(%
17fb0 51 29 22 2c 20 7a 54 65 78 74 29 3b 0a 2a 2a 20 Q)", zText);.**
17fc0 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 64 62 sqlite3_exec(db
17fd0 2c 20 7a 53 51 4c 2c 20 30 2c 20 30 2c 20 30 29 , zSQL, 0, 0, 0)
17fe0 3b 0a 2a 2a 20 20 73 71 6c 69 74 65 33 5f 66 72 ;.** sqlite3_fr
17ff0 65 65 28 7a 53 51 4c 29 3b 0a 2a 2a 20 3c 2f 70 ee(zSQL);.** </p
18000 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e re></blockquote>
18010 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 .**.** The code
18020 61 62 6f 76 65 20 77 69 6c 6c 20 72 65 6e 64 65 above will rende
18030 72 20 61 20 63 6f 72 72 65 63 74 20 53 51 4c 20 r a correct SQL
18040 73 74 61 74 65 6d 65 6e 74 20 69 6e 20 74 68 65 statement in the
18050 20 7a 53 51 4c 0a 2a 2a 20 76 61 72 69 61 62 6c zSQL.** variabl
18060 65 20 65 76 65 6e 20 69 66 20 74 68 65 20 7a 54 e even if the zT
18070 65 78 74 20 76 61 72 69 61 62 6c 65 20 69 73 20 ext variable is
18080 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a a NULL pointer..
18090 2a 2a 0a 2a 2a 20 54 68 65 20 22 25 7a 22 20 66 **.** The "%z" f
180a0 6f 72 6d 61 74 74 69 6e 67 20 6f 70 74 69 6f 6e ormatting option
180b0 20 77 6f 72 6b 73 20 65 78 61 63 74 6c 79 20 6c works exactly l
180c0 69 6b 65 20 22 25 73 22 20 77 69 74 68 20 74 68 ike "%s" with th
180d0 65 0a 2a 2a 20 61 64 64 69 74 69 6f 6e 20 74 68 e.** addition th
180e0 61 74 20 61 66 74 65 72 20 74 68 65 20 73 74 72 at after the str
180f0 69 6e 67 20 68 61 73 20 62 65 65 6e 20 72 65 61 ing has been rea
18100 64 20 61 6e 64 20 63 6f 70 69 65 64 20 69 6e 74 d and copied int
18110 6f 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 2c o.** the result,
18120 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 [sqlite3_free()
18130 5d 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 20 74 ] is called on t
18140 68 65 20 69 6e 70 75 74 20 73 74 72 69 6e 67 2e he input string.
18150 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 52 65 71 {END}.**.** Req
18160 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 uirements:.** [H
18170 31 37 34 30 33 5d 20 5b 48 31 37 34 30 36 5d 20 17403] [H17406]
18180 5b 48 31 37 34 30 37 5d 0a 2a 2f 0a 53 51 4c 49 [H17407].*/.SQLI
18190 54 45 5f 41 50 49 20 63 68 61 72 20 2a 73 71 6c TE_API char *sql
181a0 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 63 6f 6e ite3_mprintf(con
181b0 73 74 20 63 68 61 72 2a 2c 2e 2e 2e 29 3b 0a 53 st char*,...);.S
181c0 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 2a QLITE_API char *
181d0 73 71 6c 69 74 65 33 5f 76 6d 70 72 69 6e 74 66 sqlite3_vmprintf
181e0 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 76 61 (const char*, va
181f0 5f 6c 69 73 74 29 3b 0a 53 51 4c 49 54 45 5f 41 _list);.SQLITE_A
18200 50 49 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 PI char *sqlite3
18210 5f 73 6e 70 72 69 6e 74 66 28 69 6e 74 2c 63 68 _snprintf(int,ch
18220 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c ar*,const char*,
18230 20 2e 2e 2e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 ...);../*.** CA
18240 50 49 33 52 45 46 3a 20 4d 65 6d 6f 72 79 20 41 PI3REF: Memory A
18250 6c 6c 6f 63 61 74 69 6f 6e 20 53 75 62 73 79 73 llocation Subsys
18260 74 65 6d 20 7b 48 31 37 33 30 30 7d 20 3c 53 32 tem {H17300} <S2
18270 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 0000>.**.** The
18280 53 51 4c 69 74 65 20 63 6f 72 65 20 20 75 73 65 SQLite core use
18290 73 20 74 68 65 73 65 20 74 68 72 65 65 20 72 6f s these three ro
182a0 75 74 69 6e 65 73 20 66 6f 72 20 61 6c 6c 20 6f utines for all o
182b0 66 20 69 74 73 20 6f 77 6e 0a 2a 2a 20 69 6e 74 f its own.** int
182c0 65 72 6e 61 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c ernal memory all
182d0 6f 63 61 74 69 6f 6e 20 6e 65 65 64 73 2e 20 22 ocation needs. "
182e0 43 6f 72 65 22 20 69 6e 20 74 68 65 20 70 72 65 Core" in the pre
182f0 76 69 6f 75 73 20 73 65 6e 74 65 6e 63 65 0a 2a vious sentence.*
18300 2a 20 64 6f 65 73 20 6e 6f 74 20 69 6e 63 6c 75 * does not inclu
18310 64 65 20 6f 70 65 72 61 74 69 6e 67 2d 73 79 73 de operating-sys
18320 74 65 6d 20 73 70 65 63 69 66 69 63 20 56 46 53 tem specific VFS
18330 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e implementation.
18340 20 20 54 68 65 0a 2a 2a 20 57 69 6e 64 6f 77 73 The.** Windows
18350 20 56 46 53 20 75 73 65 73 20 6e 61 74 69 76 65 VFS uses native
18360 20 6d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 66 72 malloc() and fr
18370 65 65 28 29 20 66 6f 72 20 73 6f 6d 65 20 6f 70 ee() for some op
18380 65 72 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 erations..**.**
18390 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c The sqlite3_mall
183a0 6f 63 28 29 20 72 6f 75 74 69 6e 65 20 72 65 74 oc() routine ret
183b0 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 urns a pointer t
183c0 6f 20 61 20 62 6c 6f 63 6b 0a 2a 2a 20 6f 66 20 o a block.** of
183d0 6d 65 6d 6f 72 79 20 61 74 20 6c 65 61 73 74 20 memory at least
183e0 4e 20 62 79 74 65 73 20 69 6e 20 6c 65 6e 67 74 N bytes in lengt
183f0 68 2c 20 77 68 65 72 65 20 4e 20 69 73 20 74 68 h, where N is th
18400 65 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 20 e parameter..**
18410 49 66 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f If sqlite3_mallo
18420 63 28 29 20 69 73 20 75 6e 61 62 6c 65 20 74 6f c() is unable to
18430 20 6f 62 74 61 69 6e 20 73 75 66 66 69 63 69 65 obtain sufficie
18440 6e 74 20 66 72 65 65 0a 2a 2a 20 6d 65 6d 6f 72 nt free.** memor
18450 79 2c 20 69 74 20 72 65 74 75 72 6e 73 20 61 20 y, it returns a
18460 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 20 20 49 NULL pointer. I
18470 66 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 20 f the parameter
18480 4e 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f N to.** sqlite3_
18490 6d 61 6c 6c 6f 63 28 29 20 69 73 20 7a 65 72 6f malloc() is zero
184a0 20 6f 72 20 6e 65 67 61 74 69 76 65 20 74 68 65 or negative the
184b0 6e 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 n sqlite3_malloc
184c0 28 29 20 72 65 74 75 72 6e 73 0a 2a 2a 20 61 20 () returns.** a
184d0 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a NULL pointer..**
184e0 0a 2a 2a 20 43 61 6c 6c 69 6e 67 20 73 71 6c 69 .** Calling sqli
184f0 74 65 33 5f 66 72 65 65 28 29 20 77 69 74 68 20 te3_free() with
18500 61 20 70 6f 69 6e 74 65 72 20 70 72 65 76 69 6f a pointer previo
18510 75 73 6c 79 20 72 65 74 75 72 6e 65 64 0a 2a 2a usly returned.**
18520 20 62 79 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c by sqlite3_mall
18530 6f 63 28 29 20 6f 72 20 73 71 6c 69 74 65 33 5f oc() or sqlite3_
18540 72 65 61 6c 6c 6f 63 28 29 20 72 65 6c 65 61 73 realloc() releas
18550 65 73 20 74 68 61 74 20 6d 65 6d 6f 72 79 20 73 es that memory s
18560 6f 0a 2a 2a 20 74 68 61 74 20 69 74 20 6d 69 67 o.** that it mig
18570 68 74 20 62 65 20 72 65 75 73 65 64 2e 20 20 54 ht be reused. T
18580 68 65 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 he sqlite3_free(
18590 29 20 72 6f 75 74 69 6e 65 20 69 73 0a 2a 2a 20 ) routine is.**
185a0 61 20 6e 6f 2d 6f 70 20 69 66 20 69 73 20 63 61 a no-op if is ca
185b0 6c 6c 65 64 20 77 69 74 68 20 61 20 4e 55 4c 4c lled with a NULL
185c0 20 70 6f 69 6e 74 65 72 2e 20 20 50 61 73 73 69 pointer. Passi
185d0 6e 67 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 ng a NULL pointe
185e0 72 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f r.** to sqlite3_
185f0 66 72 65 65 28 29 20 69 73 20 68 61 72 6d 6c 65 free() is harmle
18600 73 73 2e 20 20 41 66 74 65 72 20 62 65 69 6e 67 ss. After being
18610 20 66 72 65 65 64 2c 20 6d 65 6d 6f 72 79 0a 2a freed, memory.*
18620 2a 20 73 68 6f 75 6c 64 20 6e 65 69 74 68 65 72 * should neither
18630 20 62 65 20 72 65 61 64 20 6e 6f 72 20 77 72 69 be read nor wri
18640 74 74 65 6e 2e 20 20 45 76 65 6e 20 72 65 61 64 tten. Even read
18650 69 6e 67 20 70 72 65 76 69 6f 75 73 6c 79 20 66 ing previously f
18660 72 65 65 64 0a 2a 2a 20 6d 65 6d 6f 72 79 20 6d reed.** memory m
18670 69 67 68 74 20 72 65 73 75 6c 74 20 69 6e 20 61 ight result in a
18680 20 73 65 67 6d 65 6e 74 61 74 69 6f 6e 20 66 61 segmentation fa
18690 75 6c 74 20 6f 72 20 6f 74 68 65 72 20 73 65 76 ult or other sev
186a0 65 72 65 20 65 72 72 6f 72 2e 0a 2a 2a 20 4d 65 ere error..** Me
186b0 6d 6f 72 79 20 63 6f 72 72 75 70 74 69 6f 6e 2c mory corruption,
186c0 20 61 20 73 65 67 6d 65 6e 74 61 74 69 6f 6e 20 a segmentation
186d0 66 61 75 6c 74 2c 20 6f 72 20 6f 74 68 65 72 20 fault, or other
186e0 73 65 76 65 72 65 20 65 72 72 6f 72 0a 2a 2a 20 severe error.**
186f0 6d 69 67 68 74 20 72 65 73 75 6c 74 20 69 66 20 might result if
18700 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 20 69 sqlite3_free() i
18710 73 20 63 61 6c 6c 65 64 20 77 69 74 68 20 61 20 s called with a
18720 6e 6f 6e 2d 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 non-NULL pointer
18730 20 74 68 61 74 0a 2a 2a 20 77 61 73 20 6e 6f 74 that.** was not
18740 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 obtained from s
18750 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 qlite3_malloc()
18760 6f 72 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c or sqlite3_reall
18770 6f 63 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 oc()..**.** The
18780 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 sqlite3_realloc(
18790 29 20 69 6e 74 65 72 66 61 63 65 20 61 74 74 65 ) interface atte
187a0 6d 70 74 73 20 74 6f 20 72 65 73 69 7a 65 20 61 mpts to resize a
187b0 0a 2a 2a 20 70 72 69 6f 72 20 6d 65 6d 6f 72 79 .** prior memory
187c0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 74 6f 20 62 allocation to b
187d0 65 20 61 74 20 6c 65 61 73 74 20 4e 20 62 79 74 e at least N byt
187e0 65 73 2c 20 77 68 65 72 65 20 4e 20 69 73 20 74 es, where N is t
187f0 68 65 0a 2a 2a 20 73 65 63 6f 6e 64 20 70 61 72 he.** second par
18800 61 6d 65 74 65 72 2e 20 20 54 68 65 20 6d 65 6d ameter. The mem
18810 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 74 ory allocation t
18820 6f 20 62 65 20 72 65 73 69 7a 65 64 20 69 73 20 o be resized is
18830 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 70 61 72 the first.** par
18840 61 6d 65 74 65 72 2e 20 20 49 66 20 74 68 65 20 ameter. If the
18850 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 20 first parameter
18860 74 6f 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c to sqlite3_reall
18870 6f 63 28 29 0a 2a 2a 20 69 73 20 61 20 4e 55 4c oc().** is a NUL
18880 4c 20 70 6f 69 6e 74 65 72 20 74 68 65 6e 20 69 L pointer then i
18890 74 73 20 62 65 68 61 76 69 6f 72 20 69 73 20 69 ts behavior is i
188a0 64 65 6e 74 69 63 61 6c 20 74 6f 20 63 61 6c 6c dentical to call
188b0 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d ing.** sqlite3_m
188c0 61 6c 6c 6f 63 28 4e 29 20 77 68 65 72 65 20 4e alloc(N) where N
188d0 20 69 73 20 74 68 65 20 73 65 63 6f 6e 64 20 70 is the second p
188e0 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 arameter to sqli
188f0 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 2e 0a 2a te3_realloc()..*
18900 2a 20 49 66 20 74 68 65 20 73 65 63 6f 6e 64 20 * If the second
18910 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c parameter to sql
18920 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 20 69 ite3_realloc() i
18930 73 20 7a 65 72 6f 20 6f 72 0a 2a 2a 20 6e 65 67 s zero or.** neg
18940 61 74 69 76 65 20 74 68 65 6e 20 74 68 65 20 62 ative then the b
18950 65 68 61 76 69 6f 72 20 69 73 20 65 78 61 63 74 ehavior is exact
18960 6c 79 20 74 68 65 20 73 61 6d 65 20 61 73 20 63 ly the same as c
18970 61 6c 6c 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 alling.** sqlite
18980 33 5f 66 72 65 65 28 50 29 20 77 68 65 72 65 20 3_free(P) where
18990 50 20 69 73 20 74 68 65 20 66 69 72 73 74 20 70 P is the first p
189a0 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 arameter to sqli
189b0 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 2e 0a 2a te3_realloc()..*
189c0 2a 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f * sqlite3_reallo
189d0 63 28 29 20 72 65 74 75 72 6e 73 20 61 20 70 6f c() returns a po
189e0 69 6e 74 65 72 20 74 6f 20 61 20 6d 65 6d 6f 72 inter to a memor
189f0 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 y allocation.**
18a00 6f 66 20 61 74 20 6c 65 61 73 74 20 4e 20 62 79 of at least N by
18a10 74 65 73 20 69 6e 20 73 69 7a 65 20 6f 72 20 4e tes in size or N
18a20 55 4c 4c 20 69 66 20 73 75 66 66 69 63 69 65 6e ULL if sufficien
18a30 74 20 6d 65 6d 6f 72 79 20 69 73 20 75 6e 61 76 t memory is unav
18a40 61 69 6c 61 62 6c 65 2e 0a 2a 2a 20 49 66 20 4d ailable..** If M
18a50 20 69 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20 is the size of
18a60 74 68 65 20 70 72 69 6f 72 20 61 6c 6c 6f 63 61 the prior alloca
18a70 74 69 6f 6e 2c 20 74 68 65 6e 20 6d 69 6e 28 4e tion, then min(N
18a80 2c 4d 29 20 62 79 74 65 73 0a 2a 2a 20 6f 66 20 ,M) bytes.** of
18a90 74 68 65 20 70 72 69 6f 72 20 61 6c 6c 6f 63 61 the prior alloca
18aa0 74 69 6f 6e 20 61 72 65 20 63 6f 70 69 65 64 20 tion are copied
18ab0 69 6e 74 6f 20 74 68 65 20 62 65 67 69 6e 6e 69 into the beginni
18ac0 6e 67 20 6f 66 20 62 75 66 66 65 72 20 72 65 74 ng of buffer ret
18ad0 75 72 6e 65 64 0a 2a 2a 20 62 79 20 73 71 6c 69 urned.** by sqli
18ae0 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 20 61 6e te3_realloc() an
18af0 64 20 74 68 65 20 70 72 69 6f 72 20 61 6c 6c 6f d the prior allo
18b00 63 61 74 69 6f 6e 20 69 73 20 66 72 65 65 64 2e cation is freed.
18b10 0a 2a 2a 20 49 66 20 73 71 6c 69 74 65 33 5f 72 .** If sqlite3_r
18b20 65 61 6c 6c 6f 63 28 29 20 72 65 74 75 72 6e 73 ealloc() returns
18b30 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65 20 NULL, then the
18b40 70 72 69 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e prior allocation
18b50 0a 2a 2a 20 69 73 20 6e 6f 74 20 66 72 65 65 64 .** is not freed
18b60 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 65 6d 6f ..**.** The memo
18b70 72 79 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 ry returned by s
18b80 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 qlite3_malloc()
18b90 61 6e 64 20 73 71 6c 69 74 65 33 5f 72 65 61 6c and sqlite3_real
18ba0 6c 6f 63 28 29 0a 2a 2a 20 69 73 20 61 6c 77 61 loc().** is alwa
18bb0 79 73 20 61 6c 69 67 6e 65 64 20 74 6f 20 61 74 ys aligned to at
18bc0 20 6c 65 61 73 74 20 61 6e 20 38 20 62 79 74 65 least an 8 byte
18bd0 20 62 6f 75 6e 64 61 72 79 2e 20 7b 45 4e 44 7d boundary. {END}
18be0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 65 66 61 75 .**.** The defau
18bf0 6c 74 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f lt implementatio
18c00 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 n of the memory
18c10 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 allocation subsy
18c20 73 74 65 6d 20 75 73 65 73 0a 2a 2a 20 74 68 65 stem uses.** the
18c30 20 6d 61 6c 6c 6f 63 28 29 2c 20 72 65 61 6c 6c malloc(), reall
18c40 6f 63 28 29 20 61 6e 64 20 66 72 65 65 28 29 20 oc() and free()
18c50 70 72 6f 76 69 64 65 64 20 62 79 20 74 68 65 20 provided by the
18c60 73 74 61 6e 64 61 72 64 20 43 20 6c 69 62 72 61 standard C libra
18c70 72 79 2e 0a 2a 2a 20 7b 48 31 37 33 38 32 7d 20 ry..** {H17382}
18c80 48 6f 77 65 76 65 72 2c 20 69 66 20 53 51 4c 69 However, if SQLi
18c90 74 65 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 77 te is compiled w
18ca0 69 74 68 20 74 68 65 0a 2a 2a 20 53 51 4c 49 54 ith the.** SQLIT
18cb0 45 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 3d 3c 69 E_MEMORY_SIZE=<i
18cc0 3e 4e 4e 4e 3c 2f 69 3e 20 43 20 70 72 65 70 72 >NNN</i> C prepr
18cd0 6f 63 65 73 73 6f 72 20 6d 61 63 72 6f 20 28 77 ocessor macro (w
18ce0 68 65 72 65 20 3c 69 3e 4e 4e 4e 3c 2f 69 3e 0a here <i>NNN</i>.
18cf0 2a 2a 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 ** is an integer
18d00 29 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 20 63 ), then SQLite c
18d10 72 65 61 74 65 20 61 20 73 74 61 74 69 63 20 61 reate a static a
18d20 72 72 61 79 20 6f 66 20 61 74 20 6c 65 61 73 74 rray of at least
18d30 0a 2a 2a 20 3c 69 3e 4e 4e 4e 3c 2f 69 3e 20 62 .** <i>NNN</i> b
18d40 79 74 65 73 20 69 6e 20 73 69 7a 65 20 61 6e 64 ytes in size and
18d50 20 75 73 65 73 20 74 68 61 74 20 61 72 72 61 79 uses that array
18d60 20 66 6f 72 20 61 6c 6c 20 6f 66 20 69 74 73 20 for all of its
18d70 64 79 6e 61 6d 69 63 0a 2a 2a 20 6d 65 6d 6f 72 dynamic.** memor
18d80 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6e 65 65 y allocation nee
18d90 64 73 2e 20 7b 45 4e 44 7d 20 20 41 64 64 69 74 ds. {END} Addit
18da0 69 6f 6e 61 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c ional memory all
18db0 6f 63 61 74 6f 72 20 6f 70 74 69 6f 6e 73 0a 2a ocator options.*
18dc0 2a 20 6d 61 79 20 62 65 20 61 64 64 65 64 20 69 * may be added i
18dd0 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 n future release
18de0 73 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 53 51 4c 69 s..**.** In SQLi
18df0 74 65 20 76 65 72 73 69 6f 6e 20 33 2e 35 2e 30 te version 3.5.0
18e00 20 61 6e 64 20 33 2e 35 2e 31 2c 20 69 74 20 77 and 3.5.1, it w
18e10 61 73 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 64 as possible to d
18e20 65 66 69 6e 65 0a 2a 2a 20 74 68 65 20 53 51 4c efine.** the SQL
18e30 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59 5f ITE_OMIT_MEMORY_
18e40 41 4c 4c 4f 43 41 54 49 4f 4e 20 77 68 69 63 68 ALLOCATION which
18e50 20 77 6f 75 6c 64 20 63 61 75 73 65 20 74 68 65 would cause the
18e60 20 62 75 69 6c 74 2d 69 6e 0a 2a 2a 20 69 6d 70 built-in.** imp
18e70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 lementation of t
18e80 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 74 6f hese routines to
18e90 20 62 65 20 6f 6d 69 74 74 65 64 2e 20 20 54 68 be omitted. Th
18ea0 61 74 20 63 61 70 61 62 69 6c 69 74 79 0a 2a 2a at capability.**
18eb0 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 70 72 is no longer pr
18ec0 6f 76 69 64 65 64 2e 20 20 4f 6e 6c 79 20 62 75 ovided. Only bu
18ed0 69 6c 74 2d 69 6e 20 6d 65 6d 6f 72 79 20 61 6c ilt-in memory al
18ee0 6c 6f 63 61 74 6f 72 73 20 63 61 6e 20 62 65 20 locators can be
18ef0 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 used..**.** The
18f00 57 69 6e 64 6f 77 73 20 4f 53 20 69 6e 74 65 72 Windows OS inter
18f10 66 61 63 65 20 6c 61 79 65 72 20 63 61 6c 6c 73 face layer calls
18f20 0a 2a 2a 20 74 68 65 20 73 79 73 74 65 6d 20 6d .** the system m
18f30 61 6c 6c 6f 63 28 29 20 61 6e 64 20 66 72 65 65 alloc() and free
18f40 28 29 20 64 69 72 65 63 74 6c 79 20 77 68 65 6e () directly when
18f50 20 63 6f 6e 76 65 72 74 69 6e 67 0a 2a 2a 20 66 converting.** f
18f60 69 6c 65 6e 61 6d 65 73 20 62 65 74 77 65 65 6e ilenames between
18f70 20 74 68 65 20 55 54 46 2d 38 20 65 6e 63 6f 64 the UTF-8 encod
18f80 69 6e 67 20 75 73 65 64 20 62 79 20 53 51 4c 69 ing used by SQLi
18f90 74 65 0a 2a 2a 20 61 6e 64 20 77 68 61 74 65 76 te.** and whatev
18fa0 65 72 20 66 69 6c 65 6e 61 6d 65 20 65 6e 63 6f er filename enco
18fb0 64 69 6e 67 20 69 73 20 75 73 65 64 20 62 79 20 ding is used by
18fc0 74 68 65 20 70 61 72 74 69 63 75 6c 61 72 20 57 the particular W
18fd0 69 6e 64 6f 77 73 0a 2a 2a 20 69 6e 73 74 61 6c indows.** instal
18fe0 6c 61 74 69 6f 6e 2e 20 20 4d 65 6d 6f 72 79 20 lation. Memory
18ff0 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 allocation error
19000 73 20 61 72 65 20 64 65 74 65 63 74 65 64 2c 20 s are detected,
19010 62 75 74 0a 2a 2a 20 74 68 65 79 20 61 72 65 20 but.** they are
19020 72 65 70 6f 72 74 65 64 20 62 61 63 6b 20 61 73 reported back as
19030 20 5b 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 [SQLITE_CANTOPE
19040 4e 5d 20 6f 72 0a 2a 2a 20 5b 53 51 4c 49 54 45 N] or.** [SQLITE
19050 5f 49 4f 45 52 52 5d 20 72 61 74 68 65 72 20 74 _IOERR] rather t
19060 68 61 6e 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d 45 han [SQLITE_NOME
19070 4d 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 M]..**.** Requir
19080 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 37 33 ements:.** [H173
19090 30 33 5d 20 5b 48 31 37 33 30 34 5d 20 5b 48 31 03] [H17304] [H1
190a0 37 33 30 35 5d 20 5b 48 31 37 33 30 36 5d 20 5b 7305] [H17306] [
190b0 48 31 37 33 31 30 5d 20 5b 48 31 37 33 31 32 5d H17310] [H17312]
190c0 20 5b 48 31 37 33 31 35 5d 20 5b 48 31 37 33 31 [H17315] [H1731
190d0 38 5d 0a 2a 2a 20 5b 48 31 37 33 32 31 5d 20 5b 8].** [H17321] [
190e0 48 31 37 33 32 32 5d 20 5b 48 31 37 33 32 33 5d H17322] [H17323]
190f0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 6e 74 .**.** The point
19100 65 72 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 er arguments to
19110 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d [sqlite3_free()]
19120 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 72 65 and [sqlite3_re
19130 61 6c 6c 6f 63 28 29 5d 0a 2a 2a 20 6d 75 73 74 alloc()].** must
19140 20 62 65 20 65 69 74 68 65 72 20 4e 55 4c 4c 20 be either NULL
19150 6f 72 20 65 6c 73 65 20 70 6f 69 6e 74 65 72 73 or else pointers
19160 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 61 obtained from a
19170 20 70 72 69 6f 72 0a 2a 2a 20 69 6e 76 6f 63 61 prior.** invoca
19180 74 69 6f 6e 20 6f 66 20 5b 73 71 6c 69 74 65 33 tion of [sqlite3
19190 5f 6d 61 6c 6c 6f 63 28 29 5d 20 6f 72 20 5b 73 _malloc()] or [s
191a0 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 qlite3_realloc()
191b0 5d 20 74 68 61 74 20 68 61 76 65 0a 2a 2a 20 6e ] that have.** n
191c0 6f 74 20 79 65 74 20 62 65 65 6e 20 72 65 6c 65 ot yet been rele
191d0 61 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ased..**.** The
191e0 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d 75 73 74 application must
191f0 20 6e 6f 74 20 72 65 61 64 20 6f 72 20 77 72 69 not read or wri
19200 74 65 20 61 6e 79 20 70 61 72 74 20 6f 66 0a 2a te any part of.*
19210 2a 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d * a block of mem
19220 6f 72 79 20 61 66 74 65 72 20 69 74 20 68 61 73 ory after it has
19230 20 62 65 65 6e 20 72 65 6c 65 61 73 65 64 20 75 been released u
19240 73 69 6e 67 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 sing.** [sqlite3
19250 5f 66 72 65 65 28 29 5d 20 6f 72 20 5b 73 71 6c _free()] or [sql
19260 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 5d 2e ite3_realloc()].
19270 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 .*/.SQLITE_API v
19280 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 6d 61 6c oid *sqlite3_mal
19290 6c 6f 63 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45 loc(int);.SQLITE
192a0 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 _API void *sqlit
192b0 65 33 5f 72 65 61 6c 6c 6f 63 28 76 6f 69 64 2a e3_realloc(void*
192c0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 , int);.SQLITE_A
192d0 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f PI void sqlite3_
192e0 66 72 65 65 28 76 6f 69 64 2a 29 3b 0a 0a 2f 2a free(void*);../*
192f0 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4d 65 .** CAPI3REF: Me
19300 6d 6f 72 79 20 41 6c 6c 6f 63 61 74 6f 72 20 53 mory Allocator S
19310 74 61 74 69 73 74 69 63 73 20 7b 48 31 37 33 37 tatistics {H1737
19320 30 7d 20 3c 53 33 30 32 31 30 3e 0a 2a 2a 0a 2a 0} <S30210>.**.*
19330 2a 20 53 51 4c 69 74 65 20 70 72 6f 76 69 64 65 * SQLite provide
19340 73 20 74 68 65 73 65 20 74 77 6f 20 69 6e 74 65 s these two inte
19350 72 66 61 63 65 73 20 66 6f 72 20 72 65 70 6f 72 rfaces for repor
19360 74 69 6e 67 20 6f 6e 20 74 68 65 20 73 74 61 74 ting on the stat
19370 75 73 0a 2a 2a 20 6f 66 20 74 68 65 20 5b 73 71 us.** of the [sq
19380 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2c lite3_malloc()],
19390 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 [sqlite3_free()
193a0 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f ], and [sqlite3_
193b0 72 65 61 6c 6c 6f 63 28 29 5d 0a 2a 2a 20 72 6f realloc()].** ro
193c0 75 74 69 6e 65 73 2c 20 77 68 69 63 68 20 66 6f utines, which fo
193d0 72 6d 20 74 68 65 20 62 75 69 6c 74 2d 69 6e 20 rm the built-in
193e0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
193f0 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2a 0a n subsystem..**.
19400 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a ** Requirements:
19410 0a 2a 2a 20 5b 48 31 37 33 37 31 5d 20 5b 48 31 .** [H17371] [H1
19420 37 33 37 33 5d 20 5b 48 31 37 33 37 34 5d 20 5b 7373] [H17374] [
19430 48 31 37 33 37 35 5d 0a 2a 2f 0a 53 51 4c 49 54 H17375].*/.SQLIT
19440 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 69 6e E_API sqlite3_in
19450 74 36 34 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f t64 sqlite3_memo
19460 72 79 5f 75 73 65 64 28 76 6f 69 64 29 3b 0a 53 ry_used(void);.S
19470 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 QLITE_API sqlite
19480 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 3_int64 sqlite3_
19490 6d 65 6d 6f 72 79 5f 68 69 67 68 77 61 74 65 72 memory_highwater
194a0 28 69 6e 74 20 72 65 73 65 74 46 6c 61 67 29 3b (int resetFlag);
194b0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
194c0 3a 20 50 73 65 75 64 6f 2d 52 61 6e 64 6f 6d 20 : Pseudo-Random
194d0 4e 75 6d 62 65 72 20 47 65 6e 65 72 61 74 6f 72 Number Generator
194e0 20 7b 48 31 37 33 39 30 7d 20 3c 53 32 30 30 30 {H17390} <S2000
194f0 30 3e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 0>.**.** SQLite
19500 63 6f 6e 74 61 69 6e 73 20 61 20 68 69 67 68 2d contains a high-
19510 71 75 61 6c 69 74 79 20 70 73 65 75 64 6f 2d 72 quality pseudo-r
19520 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 67 65 6e andom number gen
19530 65 72 61 74 6f 72 20 28 50 52 4e 47 29 20 75 73 erator (PRNG) us
19540 65 64 20 74 6f 0a 2a 2a 20 73 65 6c 65 63 74 20 ed to.** select
19550 72 61 6e 64 6f 6d 20 5b 52 4f 57 49 44 20 7c 20 random [ROWID |
19560 52 4f 57 49 44 73 5d 20 77 68 65 6e 20 69 6e 73 ROWIDs] when ins
19570 65 72 74 69 6e 67 20 6e 65 77 20 72 65 63 6f 72 erting new recor
19580 64 73 20 69 6e 74 6f 20 61 20 74 61 62 6c 65 20 ds into a table
19590 74 68 61 74 0a 2a 2a 20 61 6c 72 65 61 64 79 20 that.** already
195a0 75 73 65 73 20 74 68 65 20 6c 61 72 67 65 73 74 uses the largest
195b0 20 70 6f 73 73 69 62 6c 65 20 5b 52 4f 57 49 44 possible [ROWID
195c0 5d 2e 20 20 54 68 65 20 50 52 4e 47 20 69 73 20 ]. The PRNG is
195d0 61 6c 73 6f 20 75 73 65 64 20 66 6f 72 0a 2a 2a also used for.**
195e0 20 74 68 65 20 62 75 69 6c 64 2d 69 6e 20 72 61 the build-in ra
195f0 6e 64 6f 6d 28 29 20 61 6e 64 20 72 61 6e 64 6f ndom() and rando
19600 6d 62 6c 6f 62 28 29 20 53 51 4c 20 66 75 6e 63 mblob() SQL func
19610 74 69 6f 6e 73 2e 20 20 54 68 69 73 20 69 6e 74 tions. This int
19620 65 72 66 61 63 65 20 61 6c 6c 6f 77 73 0a 2a 2a erface allows.**
19630 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 6f applications to
19640 20 61 63 63 65 73 73 20 74 68 65 20 73 61 6d 65 access the same
19650 20 50 52 4e 47 20 66 6f 72 20 6f 74 68 65 72 20 PRNG for other
19660 70 75 72 70 6f 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 purposes..**.**
19670 41 20 63 61 6c 6c 20 74 6f 20 74 68 69 73 20 72 A call to this r
19680 6f 75 74 69 6e 65 20 73 74 6f 72 65 73 20 4e 20 outine stores N
19690 62 79 74 65 73 20 6f 66 20 72 61 6e 64 6f 6d 6e bytes of randomn
196a0 65 73 73 20 69 6e 74 6f 20 62 75 66 66 65 72 20 ess into buffer
196b0 50 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 P..**.** The fir
196c0 73 74 20 74 69 6d 65 20 74 68 69 73 20 72 6f 75 st time this rou
196d0 74 69 6e 65 20 69 73 20 69 6e 76 6f 6b 65 64 20 tine is invoked
196e0 28 65 69 74 68 65 72 20 69 6e 74 65 72 6e 61 6c (either internal
196f0 6c 79 20 6f 72 20 62 79 0a 2a 2a 20 74 68 65 20 ly or by.** the
19700 61 70 70 6c 69 63 61 74 69 6f 6e 29 20 74 68 65 application) the
19710 20 50 52 4e 47 20 69 73 20 73 65 65 64 65 64 20 PRNG is seeded
19720 75 73 69 6e 67 20 72 61 6e 64 6f 6d 6e 65 73 73 using randomness
19730 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66 72 6f obtained.** fro
19740 6d 20 74 68 65 20 78 52 61 6e 64 6f 6d 6e 65 73 m the xRandomnes
19750 73 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20 s method of the
19760 64 65 66 61 75 6c 74 20 5b 73 71 6c 69 74 65 33 default [sqlite3
19770 5f 76 66 73 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2a _vfs] object..**
19780 20 4f 6e 20 61 6c 6c 20 73 75 62 73 65 71 75 65 On all subseque
19790 6e 74 20 69 6e 76 6f 63 61 74 69 6f 6e 73 2c 20 nt invocations,
197a0 74 68 65 20 70 73 65 75 64 6f 2d 72 61 6e 64 6f the pseudo-rando
197b0 6d 6e 65 73 73 20 69 73 20 67 65 6e 65 72 61 74 mness is generat
197c0 65 64 0a 2a 2a 20 69 6e 74 65 72 6e 61 6c 6c 79 ed.** internally
197d0 20 61 6e 64 20 77 69 74 68 6f 75 74 20 72 65 63 and without rec
197e0 6f 75 72 73 65 20 74 6f 20 74 68 65 20 5b 73 71 ourse to the [sq
197f0 6c 69 74 65 33 5f 76 66 73 5d 20 78 52 61 6e 64 lite3_vfs] xRand
19800 6f 6d 6e 65 73 73 0a 2a 2a 20 6d 65 74 68 6f 64 omness.** method
19810 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d ..**.** Requirem
19820 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 37 33 39 32 ents:.** [H17392
19830 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ].*/.SQLITE_API
19840 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 61 6e void sqlite3_ran
19850 64 6f 6d 6e 65 73 73 28 69 6e 74 20 4e 2c 20 76 domness(int N, v
19860 6f 69 64 20 2a 50 29 3b 0a 0a 2f 2a 0a 2a 2a 20 oid *P);../*.**
19870 43 41 50 49 33 52 45 46 3a 20 43 6f 6d 70 69 6c CAPI3REF: Compil
19880 65 2d 54 69 6d 65 20 41 75 74 68 6f 72 69 7a 61 e-Time Authoriza
19890 74 69 6f 6e 20 43 61 6c 6c 62 61 63 6b 73 20 7b tion Callbacks {
198a0 48 31 32 35 30 30 7d 20 3c 53 37 30 31 30 30 3e H12500} <S70100>
198b0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
198c0 69 6e 65 20 72 65 67 69 73 74 65 72 73 20 61 20 ine registers a
198d0 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 authorizer callb
198e0 61 63 6b 20 77 69 74 68 20 61 20 70 61 72 74 69 ack with a parti
198f0 63 75 6c 61 72 0a 2a 2a 20 5b 64 61 74 61 62 61 cular.** [databa
19900 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2c 20 se connection],
19910 73 75 70 70 6c 69 65 64 20 69 6e 20 74 68 65 20 supplied in the
19920 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e 0a first argument..
19930 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 69 7a 65 ** The authorize
19940 72 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e r callback is in
19950 76 6f 6b 65 64 20 61 73 20 53 51 4c 20 73 74 61 voked as SQL sta
19960 74 65 6d 65 6e 74 73 20 61 72 65 20 62 65 69 6e tements are bein
19970 67 20 63 6f 6d 70 69 6c 65 64 0a 2a 2a 20 62 79 g compiled.** by
19980 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 [sqlite3_prepar
19990 65 28 29 5d 20 6f 72 20 69 74 73 20 76 61 72 69 e()] or its vari
199a0 61 6e 74 73 20 5b 73 71 6c 69 74 65 33 5f 70 72 ants [sqlite3_pr
199b0 65 70 61 72 65 5f 76 32 28 29 5d 2c 0a 2a 2a 20 epare_v2()],.**
199c0 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 [sqlite3_prepare
199d0 31 36 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 16()] and [sqlit
199e0 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76 32 28 e3_prepare16_v2(
199f0 29 5d 2e 20 20 41 74 20 76 61 72 69 6f 75 73 0a )]. At various.
19a00 2a 2a 20 70 6f 69 6e 74 73 20 64 75 72 69 6e 67 ** points during
19a10 20 74 68 65 20 63 6f 6d 70 69 6c 61 74 69 6f 6e the compilation
19a20 20 70 72 6f 63 65 73 73 2c 20 61 73 20 6c 6f 67 process, as log
19a30 69 63 20 69 73 20 62 65 69 6e 67 20 63 72 65 61 ic is being crea
19a40 74 65 64 0a 2a 2a 20 74 6f 20 70 65 72 66 6f 72 ted.** to perfor
19a50 6d 20 76 61 72 69 6f 75 73 20 61 63 74 69 6f 6e m various action
19a60 73 2c 20 74 68 65 20 61 75 74 68 6f 72 69 7a 65 s, the authorize
19a70 72 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e r callback is in
19a80 76 6f 6b 65 64 20 74 6f 0a 2a 2a 20 73 65 65 20 voked to.** see
19a90 69 66 20 74 68 6f 73 65 20 61 63 74 69 6f 6e 73 if those actions
19aa0 20 61 72 65 20 61 6c 6c 6f 77 65 64 2e 20 20 54 are allowed. T
19ab0 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 he authorizer ca
19ac0 6c 6c 62 61 63 6b 20 73 68 6f 75 6c 64 0a 2a 2a llback should.**
19ad0 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f return [SQLITE_
19ae0 4f 4b 5d 20 74 6f 20 61 6c 6c 6f 77 20 74 68 65 OK] to allow the
19af0 20 61 63 74 69 6f 6e 2c 20 5b 53 51 4c 49 54 45 action, [SQLITE
19b00 5f 49 47 4e 4f 52 45 5d 20 74 6f 20 64 69 73 61 _IGNORE] to disa
19b10 6c 6c 6f 77 20 74 68 65 0a 2a 2a 20 73 70 65 63 llow the.** spec
19b20 69 66 69 63 20 61 63 74 69 6f 6e 20 62 75 74 20 ific action but
19b30 61 6c 6c 6f 77 20 74 68 65 20 53 51 4c 20 73 74 allow the SQL st
19b40 61 74 65 6d 65 6e 74 20 74 6f 20 63 6f 6e 74 69 atement to conti
19b50 6e 75 65 20 74 6f 20 62 65 0a 2a 2a 20 63 6f 6d nue to be.** com
19b60 70 69 6c 65 64 2c 20 6f 72 20 5b 53 51 4c 49 54 piled, or [SQLIT
19b70 45 5f 44 45 4e 59 5d 20 74 6f 20 63 61 75 73 65 E_DENY] to cause
19b80 20 74 68 65 20 65 6e 74 69 72 65 20 53 51 4c 20 the entire SQL
19b90 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 62 65 0a statement to be.
19ba0 2a 2a 20 72 65 6a 65 63 74 65 64 20 77 69 74 68 ** rejected with
19bb0 20 61 6e 20 65 72 72 6f 72 2e 20 20 49 66 20 74 an error. If t
19bc0 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 he authorizer ca
19bd0 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 0a 2a llback returns.*
19be0 2a 20 61 6e 79 20 76 61 6c 75 65 20 6f 74 68 65 * any value othe
19bf0 72 20 74 68 61 6e 20 5b 53 51 4c 49 54 45 5f 49 r than [SQLITE_I
19c00 47 4e 4f 52 45 5d 2c 20 5b 53 51 4c 49 54 45 5f GNORE], [SQLITE_
19c10 4f 4b 5d 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f OK], or [SQLITE_
19c20 44 45 4e 59 5d 0a 2a 2a 20 74 68 65 6e 20 74 68 DENY].** then th
19c30 65 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 e [sqlite3_prepa
19c40 72 65 5f 76 32 28 29 5d 20 6f 72 20 65 71 75 69 re_v2()] or equi
19c50 76 61 6c 65 6e 74 20 63 61 6c 6c 20 74 68 61 74 valent call that
19c60 20 74 72 69 67 67 65 72 65 64 0a 2a 2a 20 74 68 triggered.** th
19c70 65 20 61 75 74 68 6f 72 69 7a 65 72 20 77 69 6c e authorizer wil
19c80 6c 20 66 61 69 6c 20 77 69 74 68 20 61 6e 20 65 l fail with an e
19c90 72 72 6f 72 20 6d 65 73 73 61 67 65 2e 0a 2a 2a rror message..**
19ca0 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 63 61 6c .** When the cal
19cb0 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20 5b 53 lback returns [S
19cc0 51 4c 49 54 45 5f 4f 4b 5d 2c 20 74 68 61 74 20 QLITE_OK], that
19cd0 6d 65 61 6e 73 20 74 68 65 20 6f 70 65 72 61 74 means the operat
19ce0 69 6f 6e 0a 2a 2a 20 72 65 71 75 65 73 74 65 64 ion.** requested
19cf0 20 69 73 20 6f 6b 2e 20 20 57 68 65 6e 20 74 68 is ok. When th
19d00 65 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 e callback retur
19d10 6e 73 20 5b 53 51 4c 49 54 45 5f 44 45 4e 59 5d ns [SQLITE_DENY]
19d20 2c 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 , the.** [sqlite
19d30 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20 3_prepare_v2()]
19d40 6f 72 20 65 71 75 69 76 61 6c 65 6e 74 20 63 61 or equivalent ca
19d50 6c 6c 20 74 68 61 74 20 74 72 69 67 67 65 72 65 ll that triggere
19d60 64 20 74 68 65 0a 2a 2a 20 61 75 74 68 6f 72 69 d the.** authori
19d70 7a 65 72 20 77 69 6c 6c 20 66 61 69 6c 20 77 69 zer will fail wi
19d80 74 68 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 th an error mess
19d90 61 67 65 20 65 78 70 6c 61 69 6e 69 6e 67 20 74 age explaining t
19da0 68 61 74 0a 2a 2a 20 61 63 63 65 73 73 20 69 73 hat.** access is
19db0 20 64 65 6e 69 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 denied. .**.**
19dc0 54 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 The first parame
19dd0 74 65 72 20 74 6f 20 74 68 65 20 61 75 74 68 6f ter to the autho
19de0 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 69 rizer callback i
19df0 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 s a copy of the
19e00 74 68 69 72 64 0a 2a 2a 20 70 61 72 61 6d 65 74 third.** paramet
19e10 65 72 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65 er to the sqlite
19e20 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 3_set_authorizer
19e30 28 29 20 69 6e 74 65 72 66 61 63 65 2e 20 54 68 () interface. Th
19e40 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 e second paramet
19e50 65 72 0a 2a 2a 20 74 6f 20 74 68 65 20 63 61 6c er.** to the cal
19e60 6c 62 61 63 6b 20 69 73 20 61 6e 20 69 6e 74 65 lback is an inte
19e70 67 65 72 20 5b 53 51 4c 49 54 45 5f 43 4f 50 59 ger [SQLITE_COPY
19e80 20 7c 20 61 63 74 69 6f 6e 20 63 6f 64 65 5d 20 | action code]
19e90 74 68 61 74 20 73 70 65 63 69 66 69 65 73 0a 2a that specifies.*
19ea0 2a 20 74 68 65 20 70 61 72 74 69 63 75 6c 61 72 * the particular
19eb0 20 61 63 74 69 6f 6e 20 74 6f 20 62 65 20 61 75 action to be au
19ec0 74 68 6f 72 69 7a 65 64 2e 20 54 68 65 20 74 68 thorized. The th
19ed0 69 72 64 20 74 68 72 6f 75 67 68 20 73 69 78 74 ird through sixt
19ee0 68 20 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20 h parameters.**
19ef0 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 to the callback
19f00 61 72 65 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 are zero-termina
19f10 74 65 64 20 73 74 72 69 6e 67 73 20 74 68 61 74 ted strings that
19f20 20 63 6f 6e 74 61 69 6e 20 61 64 64 69 74 69 6f contain additio
19f30 6e 61 6c 0a 2a 2a 20 64 65 74 61 69 6c 73 20 61 nal.** details a
19f40 62 6f 75 74 20 74 68 65 20 61 63 74 69 6f 6e 20 bout the action
19f50 74 6f 20 62 65 20 61 75 74 68 6f 72 69 7a 65 64 to be authorized
19f60 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 61 ..**.** If the a
19f70 63 74 69 6f 6e 20 63 6f 64 65 20 69 73 20 5b 53 ction code is [S
19f80 51 4c 49 54 45 5f 52 45 41 44 5d 0a 2a 2a 20 61 QLITE_READ].** a
19f90 6e 64 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 nd the callback
19fa0 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f returns [SQLITE_
19fb0 49 47 4e 4f 52 45 5d 20 74 68 65 6e 20 74 68 65 IGNORE] then the
19fc0 0a 2a 2a 20 5b 70 72 65 70 61 72 65 64 20 73 74 .** [prepared st
19fd0 61 74 65 6d 65 6e 74 5d 20 73 74 61 74 65 6d 65 atement] stateme
19fe0 6e 74 20 69 73 20 63 6f 6e 73 74 72 75 63 74 65 nt is constructe
19ff0 64 20 74 6f 20 73 75 62 73 74 69 74 75 74 65 0a d to substitute.
1a000 2a 2a 20 61 20 4e 55 4c 4c 20 76 61 6c 75 65 20 ** a NULL value
1a010 69 6e 20 70 6c 61 63 65 20 6f 66 20 74 68 65 20 in place of the
1a020 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 74 68 61 table column tha
1a030 74 20 77 6f 75 6c 64 20 68 61 76 65 0a 2a 2a 20 t would have.**
1a040 62 65 65 6e 20 72 65 61 64 20 69 66 20 5b 53 51 been read if [SQ
1a050 4c 49 54 45 5f 4f 4b 5d 20 68 61 64 20 62 65 65 LITE_OK] had bee
1a060 6e 20 72 65 74 75 72 6e 65 64 2e 20 20 54 68 65 n returned. The
1a070 20 5b 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5d [SQLITE_IGNORE]
1a080 0a 2a 2a 20 72 65 74 75 72 6e 20 63 61 6e 20 62 .** return can b
1a090 65 20 75 73 65 64 20 74 6f 20 64 65 6e 79 20 61 e used to deny a
1a0a0 6e 20 75 6e 74 72 75 73 74 65 64 20 75 73 65 72 n untrusted user
1a0b0 20 61 63 63 65 73 73 20 74 6f 20 69 6e 64 69 76 access to indiv
1a0c0 69 64 75 61 6c 0a 2a 2a 20 63 6f 6c 75 6d 6e 73 idual.** columns
1a0d0 20 6f 66 20 61 20 74 61 62 6c 65 2e 0a 2a 2a 20 of a table..**
1a0e0 49 66 20 74 68 65 20 61 63 74 69 6f 6e 20 63 6f If the action co
1a0f0 64 65 20 69 73 20 5b 53 51 4c 49 54 45 5f 44 45 de is [SQLITE_DE
1a100 4c 45 54 45 5d 20 61 6e 64 20 74 68 65 20 63 61 LETE] and the ca
1a110 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 0a 2a llback returns.*
1a120 2a 20 5b 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 * [SQLITE_IGNORE
1a130 5d 20 74 68 65 6e 20 74 68 65 20 5b 44 45 4c 45 ] then the [DELE
1a140 54 45 5d 20 6f 70 65 72 61 74 69 6f 6e 20 70 72 TE] operation pr
1a150 6f 63 65 65 64 73 20 62 75 74 20 74 68 65 0a 2a oceeds but the.*
1a160 2a 20 5b 74 72 75 6e 63 61 74 65 20 6f 70 74 69 * [truncate opti
1a170 6d 69 7a 61 74 69 6f 6e 5d 20 69 73 20 64 69 73 mization] is dis
1a180 61 62 6c 65 64 20 61 6e 64 20 61 6c 6c 20 72 6f abled and all ro
1a190 77 73 20 61 72 65 20 64 65 6c 65 74 65 64 20 69 ws are deleted i
1a1a0 6e 64 69 76 69 64 75 61 6c 6c 79 2e 0a 2a 2a 0a ndividually..**.
1a1b0 2a 2a 20 41 6e 20 61 75 74 68 6f 72 69 7a 65 72 ** An authorizer
1a1c0 20 69 73 20 75 73 65 64 20 77 68 65 6e 20 5b 73 is used when [s
1a1d0 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 20 7c qlite3_prepare |
1a1e0 20 70 72 65 70 61 72 69 6e 67 5d 0a 2a 2a 20 53 preparing].** S
1a1f0 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 66 72 QL statements fr
1a200 6f 6d 20 61 6e 20 75 6e 74 72 75 73 74 65 64 20 om an untrusted
1a210 73 6f 75 72 63 65 2c 20 74 6f 20 65 6e 73 75 72 source, to ensur
1a220 65 20 74 68 61 74 20 74 68 65 20 53 51 4c 20 73 e that the SQL s
1a230 74 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 64 6f 20 tatements.** do
1a240 6e 6f 74 20 74 72 79 20 74 6f 20 61 63 63 65 73 not try to acces
1a250 73 20 64 61 74 61 20 74 68 65 79 20 61 72 65 20 s data they are
1a260 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 74 6f 20 73 not allowed to s
1a270 65 65 2c 20 6f 72 20 74 68 61 74 20 74 68 65 79 ee, or that they
1a280 20 64 6f 20 6e 6f 74 0a 2a 2a 20 74 72 79 20 74 do not.** try t
1a290 6f 20 65 78 65 63 75 74 65 20 6d 61 6c 69 63 69 o execute malici
1a2a0 6f 75 73 20 73 74 61 74 65 6d 65 6e 74 73 20 74 ous statements t
1a2b0 68 61 74 20 64 61 6d 61 67 65 20 74 68 65 20 64 hat damage the d
1a2c0 61 74 61 62 61 73 65 2e 20 20 46 6f 72 0a 2a 2a atabase. For.**
1a2d0 20 65 78 61 6d 70 6c 65 2c 20 61 6e 20 61 70 70 example, an app
1a2e0 6c 69 63 61 74 69 6f 6e 20 6d 61 79 20 61 6c 6c lication may all
1a2f0 6f 77 20 61 20 75 73 65 72 20 74 6f 20 65 6e 74 ow a user to ent
1a300 65 72 20 61 72 62 69 74 72 61 72 79 0a 2a 2a 20 er arbitrary.**
1a310 53 51 4c 20 71 75 65 72 69 65 73 20 66 6f 72 20 SQL queries for
1a320 65 76 61 6c 75 61 74 69 6f 6e 20 62 79 20 61 20 evaluation by a
1a330 64 61 74 61 62 61 73 65 2e 20 20 42 75 74 20 74 database. But t
1a340 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 64 he application d
1a350 6f 65 73 0a 2a 2a 20 6e 6f 74 20 77 61 6e 74 20 oes.** not want
1a360 74 68 65 20 75 73 65 72 20 74 6f 20 62 65 20 61 the user to be a
1a370 62 6c 65 20 74 6f 20 6d 61 6b 65 20 61 72 62 69 ble to make arbi
1a380 74 72 61 72 79 20 63 68 61 6e 67 65 73 20 74 6f trary changes to
1a390 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 the.** database
1a3a0 2e 20 20 41 6e 20 61 75 74 68 6f 72 69 7a 65 72 . An authorizer
1a3b0 20 63 6f 75 6c 64 20 74 68 65 6e 20 62 65 20 70 could then be p
1a3c0 75 74 20 69 6e 20 70 6c 61 63 65 20 77 68 69 6c ut in place whil
1a3d0 65 20 74 68 65 0a 2a 2a 20 75 73 65 72 2d 65 6e e the.** user-en
1a3e0 74 65 72 65 64 20 53 51 4c 20 69 73 20 62 65 69 tered SQL is bei
1a3f0 6e 67 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 ng [sqlite3_prep
1a400 61 72 65 20 7c 20 70 72 65 70 61 72 65 64 5d 20 are | prepared]
1a410 74 68 61 74 0a 2a 2a 20 64 69 73 61 6c 6c 6f 77 that.** disallow
1a420 73 20 65 76 65 72 79 74 68 69 6e 67 20 65 78 63 s everything exc
1a430 65 70 74 20 5b 53 45 4c 45 43 54 5d 20 73 74 61 ept [SELECT] sta
1a440 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 41 tements..**.** A
1a450 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 68 61 74 pplications that
1a460 20 6e 65 65 64 20 74 6f 20 70 72 6f 63 65 73 73 need to process
1a470 20 53 51 4c 20 66 72 6f 6d 20 75 6e 74 72 75 73 SQL from untrus
1a480 74 65 64 20 73 6f 75 72 63 65 73 0a 2a 2a 20 6d ted sources.** m
1a490 69 67 68 74 20 61 6c 73 6f 20 63 6f 6e 73 69 64 ight also consid
1a4a0 65 72 20 6c 6f 77 65 72 69 6e 67 20 72 65 73 6f er lowering reso
1a4b0 75 72 63 65 20 6c 69 6d 69 74 73 20 75 73 69 6e urce limits usin
1a4c0 67 20 5b 73 71 6c 69 74 65 33 5f 6c 69 6d 69 74 g [sqlite3_limit
1a4d0 28 29 5d 0a 2a 2a 20 61 6e 64 20 6c 69 6d 69 74 ()].** and limit
1a4e0 69 6e 67 20 64 61 74 61 62 61 73 65 20 73 69 7a ing database siz
1a4f0 65 20 75 73 69 6e 67 20 74 68 65 20 5b 6d 61 78 e using the [max
1a500 5f 70 61 67 65 5f 63 6f 75 6e 74 5d 20 5b 50 52 _page_count] [PR
1a510 41 47 4d 41 5d 0a 2a 2a 20 69 6e 20 61 64 64 69 AGMA].** in addi
1a520 74 69 6f 6e 20 74 6f 20 75 73 69 6e 67 20 61 6e tion to using an
1a530 20 61 75 74 68 6f 72 69 7a 65 72 2e 0a 2a 2a 0a authorizer..**.
1a540 2a 2a 20 4f 6e 6c 79 20 61 20 73 69 6e 67 6c 65 ** Only a single
1a550 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6e 20 authorizer can
1a560 62 65 20 69 6e 20 70 6c 61 63 65 20 6f 6e 20 61 be in place on a
1a570 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
1a580 74 69 6f 6e 0a 2a 2a 20 61 74 20 61 20 74 69 6d tion.** at a tim
1a590 65 2e 20 20 45 61 63 68 20 63 61 6c 6c 20 74 6f e. Each call to
1a5a0 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 sqlite3_set_aut
1a5b0 68 6f 72 69 7a 65 72 20 6f 76 65 72 72 69 64 65 horizer override
1a5c0 73 20 74 68 65 0a 2a 2a 20 70 72 65 76 69 6f 75 s the.** previou
1a5d0 73 20 63 61 6c 6c 2e 20 20 44 69 73 61 62 6c 65 s call. Disable
1a5e0 20 74 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20 the authorizer
1a5f0 62 79 20 69 6e 73 74 61 6c 6c 69 6e 67 20 61 20 by installing a
1a600 4e 55 4c 4c 20 63 61 6c 6c 62 61 63 6b 2e 0a 2a NULL callback..*
1a610 2a 20 54 68 65 20 61 75 74 68 6f 72 69 7a 65 72 * The authorizer
1a620 20 69 73 20 64 69 73 61 62 6c 65 64 20 62 79 20 is disabled by
1a630 64 65 66 61 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 54 default..**.** T
1a640 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 he authorizer ca
1a650 6c 6c 62 61 63 6b 20 6d 75 73 74 20 6e 6f 74 20 llback must not
1a660 64 6f 20 61 6e 79 74 68 69 6e 67 20 74 68 61 74 do anything that
1a670 20 77 69 6c 6c 20 6d 6f 64 69 66 79 0a 2a 2a 20 will modify.**
1a680 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e the database con
1a690 6e 65 63 74 69 6f 6e 20 74 68 61 74 20 69 6e 76 nection that inv
1a6a0 6f 6b 65 64 20 74 68 65 20 61 75 74 68 6f 72 69 oked the authori
1a6b0 7a 65 72 20 63 61 6c 6c 62 61 63 6b 2e 0a 2a 2a zer callback..**
1a6c0 20 4e 6f 74 65 20 74 68 61 74 20 5b 73 71 6c 69 Note that [sqli
1a6d0 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 te3_prepare_v2()
1a6e0 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 73 ] and [sqlite3_s
1a6f0 74 65 70 28 29 5d 20 62 6f 74 68 20 6d 6f 64 69 tep()] both modi
1a700 66 79 20 74 68 65 69 72 0a 2a 2a 20 64 61 74 61 fy their.** data
1a710 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 base connections
1a720 20 66 6f 72 20 74 68 65 20 6d 65 61 6e 69 6e 67 for the meaning
1a730 20 6f 66 20 22 6d 6f 64 69 66 79 22 20 69 6e 20 of "modify" in
1a740 74 68 69 73 20 70 61 72 61 67 72 61 70 68 2e 0a this paragraph..
1a750 2a 2a 0a 2a 2a 20 57 68 65 6e 20 5b 73 71 6c 69 **.** When [sqli
1a760 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 te3_prepare_v2()
1a770 5d 20 69 73 20 75 73 65 64 20 74 6f 20 70 72 65 ] is used to pre
1a780 70 61 72 65 20 61 20 73 74 61 74 65 6d 65 6e 74 pare a statement
1a790 2c 20 74 68 65 0a 2a 2a 20 73 74 61 74 65 6d 65 , the.** stateme
1a7a0 6e 74 20 6d 69 67 68 74 20 62 65 20 72 65 2d 70 nt might be re-p
1a7b0 72 65 70 61 72 65 64 20 64 75 72 69 6e 67 20 5b repared during [
1a7c0 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 sqlite3_step()]
1a7d0 64 75 65 20 74 6f 20 61 20 0a 2a 2a 20 73 63 68 due to a .** sch
1a7e0 65 6d 61 20 63 68 61 6e 67 65 2e 20 20 48 65 6e ema change. Hen
1a7f0 63 65 2c 20 74 68 65 20 61 70 70 6c 69 63 61 74 ce, the applicat
1a800 69 6f 6e 20 73 68 6f 75 6c 64 20 65 6e 73 75 72 ion should ensur
1a810 65 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 63 6f e that the.** co
1a820 72 72 65 63 74 20 61 75 74 68 6f 72 69 7a 65 72 rrect authorizer
1a830 20 63 61 6c 6c 62 61 63 6b 20 72 65 6d 61 69 6e callback remain
1a840 73 20 69 6e 20 70 6c 61 63 65 20 64 75 72 69 6e s in place durin
1a850 67 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 g the [sqlite3_s
1a860 74 65 70 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 4e 6f tep()]..**.** No
1a870 74 65 20 74 68 61 74 20 74 68 65 20 61 75 74 68 te that the auth
1a880 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 orizer callback
1a890 69 73 20 69 6e 76 6f 6b 65 64 20 6f 6e 6c 79 20 is invoked only
1a8a0 64 75 72 69 6e 67 0a 2a 2a 20 5b 73 71 6c 69 74 during.** [sqlit
1a8b0 65 33 5f 70 72 65 70 61 72 65 28 29 5d 20 6f 72 e3_prepare()] or
1a8c0 20 69 74 73 20 76 61 72 69 61 6e 74 73 2e 20 20 its variants.
1a8d0 41 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 69 73 Authorization is
1a8e0 20 6e 6f 74 0a 2a 2a 20 70 65 72 66 6f 72 6d 65 not.** performe
1a8f0 64 20 64 75 72 69 6e 67 20 73 74 61 74 65 6d 65 d during stateme
1a900 6e 74 20 65 76 61 6c 75 61 74 69 6f 6e 20 69 6e nt evaluation in
1a910 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 [sqlite3_step()
1a920 5d 2c 20 75 6e 6c 65 73 73 0a 2a 2a 20 61 73 20 ], unless.** as
1a930 73 74 61 74 65 64 20 69 6e 20 74 68 65 20 70 72 stated in the pr
1a940 65 76 69 6f 75 73 20 70 61 72 61 67 72 61 70 68 evious paragraph
1a950 2c 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 , sqlite3_step()
1a960 20 69 6e 76 6f 6b 65 73 0a 2a 2a 20 73 71 6c 69 invokes.** sqli
1a970 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 te3_prepare_v2()
1a980 20 74 6f 20 72 65 70 72 65 70 61 72 65 20 61 20 to reprepare a
1a990 73 74 61 74 65 6d 65 6e 74 20 61 66 74 65 72 20 statement after
1a9a0 61 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 2e a schema change.
1a9b0 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 .**.** Requireme
1a9c0 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 35 30 31 5d nts:.** [H12501]
1a9d0 20 5b 48 31 32 35 30 32 5d 20 5b 48 31 32 35 30 [H12502] [H1250
1a9e0 33 5d 20 5b 48 31 32 35 30 34 5d 20 5b 48 31 32 3] [H12504] [H12
1a9f0 35 30 35 5d 20 5b 48 31 32 35 30 36 5d 20 5b 48 505] [H12506] [H
1aa00 31 32 35 30 37 5d 20 5b 48 31 32 35 31 30 5d 0a 12507] [H12510].
1aa10 2a 2a 20 5b 48 31 32 35 31 31 5d 20 5b 48 31 32 ** [H12511] [H12
1aa20 35 31 32 5d 20 5b 48 31 32 35 32 30 5d 20 5b 48 512] [H12520] [H
1aa30 31 32 35 32 31 5d 20 5b 48 31 32 35 32 32 5d 0a 12521] [H12522].
1aa40 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
1aa50 74 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 t sqlite3_set_au
1aa60 74 68 6f 72 69 7a 65 72 28 0a 20 20 73 71 6c 69 thorizer(. sqli
1aa70 74 65 33 2a 2c 0a 20 20 69 6e 74 20 28 2a 78 41 te3*,. int (*xA
1aa80 75 74 68 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 uth)(void*,int,c
1aa90 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 onst char*,const
1aaa0 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 char*,const cha
1aab0 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 2c r*,const char*),
1aac0 0a 20 20 76 6f 69 64 20 2a 70 55 73 65 72 44 61 . void *pUserDa
1aad0 74 61 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 ta.);../*.** CAP
1aae0 49 33 52 45 46 3a 20 41 75 74 68 6f 72 69 7a 65 I3REF: Authorize
1aaf0 72 20 52 65 74 75 72 6e 20 43 6f 64 65 73 20 7b r Return Codes {
1ab00 48 31 32 35 39 30 7d 20 3c 48 31 32 35 30 30 3e H12590} <H12500>
1ab10 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69 .**.** The [sqli
1ab20 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a te3_set_authoriz
1ab30 65 72 20 7c 20 61 75 74 68 6f 72 69 7a 65 72 20 er | authorizer
1ab40 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f callback functio
1ab50 6e 5d 20 6d 75 73 74 0a 2a 2a 20 72 65 74 75 72 n] must.** retur
1ab60 6e 20 65 69 74 68 65 72 20 5b 53 51 4c 49 54 45 n either [SQLITE
1ab70 5f 4f 4b 5d 20 6f 72 20 6f 6e 65 20 6f 66 20 74 _OK] or one of t
1ab80 68 65 73 65 20 74 77 6f 20 63 6f 6e 73 74 61 6e hese two constan
1ab90 74 73 20 69 6e 20 6f 72 64 65 72 0a 2a 2a 20 74 ts in order.** t
1aba0 6f 20 73 69 67 6e 61 6c 20 53 51 4c 69 74 65 20 o signal SQLite
1abb0 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 whether or not t
1abc0 68 65 20 61 63 74 69 6f 6e 20 69 73 20 70 65 72 he action is per
1abd0 6d 69 74 74 65 64 2e 20 20 53 65 65 20 74 68 65 mitted. See the
1abe0 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 73 65 74 .** [sqlite3_set
1abf0 5f 61 75 74 68 6f 72 69 7a 65 72 20 7c 20 61 75 _authorizer | au
1ac00 74 68 6f 72 69 7a 65 72 20 64 6f 63 75 6d 65 6e thorizer documen
1ac10 74 61 74 69 6f 6e 5d 20 66 6f 72 20 61 64 64 69 tation] for addi
1ac20 74 69 6f 6e 61 6c 0a 2a 2a 20 69 6e 66 6f 72 6d tional.** inform
1ac30 61 74 69 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69 6e ation..*/.#defin
1ac40 65 20 53 51 4c 49 54 45 5f 44 45 4e 59 20 20 20 e SQLITE_DENY
1ac50 31 20 20 20 2f 2a 20 41 62 6f 72 74 20 74 68 65 1 /* Abort the
1ac60 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 77 SQL statement w
1ac70 69 74 68 20 61 6e 20 65 72 72 6f 72 20 2a 2f 0a ith an error */.
1ac80 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 #define SQLITE_I
1ac90 47 4e 4f 52 45 20 32 20 20 20 2f 2a 20 44 6f 6e GNORE 2 /* Don
1aca0 27 74 20 61 6c 6c 6f 77 20 61 63 63 65 73 73 2c 't allow access,
1acb0 20 62 75 74 20 64 6f 6e 27 74 20 67 65 6e 65 72 but don't gener
1acc0 61 74 65 20 61 6e 20 65 72 72 6f 72 20 2a 2f 0a ate an error */.
1acd0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
1ace0 20 41 75 74 68 6f 72 69 7a 65 72 20 41 63 74 69 Authorizer Acti
1acf0 6f 6e 20 43 6f 64 65 73 20 7b 48 31 32 35 35 30 on Codes {H12550
1ad00 7d 20 3c 48 31 32 35 30 30 3e 0a 2a 2a 0a 2a 2a } <H12500>.**.**
1ad10 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 65 The [sqlite3_se
1ad20 74 5f 61 75 74 68 6f 72 69 7a 65 72 28 29 5d 20 t_authorizer()]
1ad30 69 6e 74 65 72 66 61 63 65 20 72 65 67 69 73 74 interface regist
1ad40 65 72 73 20 61 20 63 61 6c 6c 62 61 63 6b 20 66 ers a callback f
1ad50 75 6e 63 74 69 6f 6e 0a 2a 2a 20 74 68 61 74 20 unction.** that
1ad60 69 73 20 69 6e 76 6f 6b 65 64 20 74 6f 20 61 75 is invoked to au
1ad70 74 68 6f 72 69 7a 65 20 63 65 72 74 61 69 6e 20 thorize certain
1ad80 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 61 63 SQL statement ac
1ad90 74 69 6f 6e 73 2e 20 20 54 68 65 0a 2a 2a 20 73 tions. The.** s
1ada0 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 econd parameter
1adb0 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 to the callback
1adc0 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 63 6f is an integer co
1add0 64 65 20 74 68 61 74 20 73 70 65 63 69 66 69 65 de that specifie
1ade0 73 0a 2a 2a 20 77 68 61 74 20 61 63 74 69 6f 6e s.** what action
1adf0 20 69 73 20 62 65 69 6e 67 20 61 75 74 68 6f 72 is being author
1ae00 69 7a 65 64 2e 20 20 54 68 65 73 65 20 61 72 65 ized. These are
1ae10 20 74 68 65 20 69 6e 74 65 67 65 72 20 61 63 74 the integer act
1ae20 69 6f 6e 20 63 6f 64 65 73 20 74 68 61 74 0a 2a ion codes that.*
1ae30 2a 20 74 68 65 20 61 75 74 68 6f 72 69 7a 65 72 * the authorizer
1ae40 20 63 61 6c 6c 62 61 63 6b 20 6d 61 79 20 62 65 callback may be
1ae50 20 70 61 73 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 passed..**.** T
1ae60 68 65 73 65 20 61 63 74 69 6f 6e 20 63 6f 64 65 hese action code
1ae70 20 76 61 6c 75 65 73 20 73 69 67 6e 69 66 79 20 values signify
1ae80 77 68 61 74 20 6b 69 6e 64 20 6f 66 20 6f 70 65 what kind of ope
1ae90 72 61 74 69 6f 6e 20 69 73 20 74 6f 20 62 65 0a ration is to be.
1aea0 2a 2a 20 61 75 74 68 6f 72 69 7a 65 64 2e 20 20 ** authorized.
1aeb0 54 68 65 20 33 72 64 20 61 6e 64 20 34 74 68 20 The 3rd and 4th
1aec0 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 74 68 parameters to th
1aed0 65 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 0a e authorization.
1aee0 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 ** callback func
1aef0 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 70 61 72 tion will be par
1af00 61 6d 65 74 65 72 73 20 6f 72 20 4e 55 4c 4c 20 ameters or NULL
1af10 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 77 68 69 depending on whi
1af20 63 68 20 6f 66 20 74 68 65 73 65 0a 2a 2a 20 63 ch of these.** c
1af30 6f 64 65 73 20 69 73 20 75 73 65 64 20 61 73 20 odes is used as
1af40 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d the second param
1af50 65 74 65 72 2e 20 20 54 68 65 20 35 74 68 20 70 eter. The 5th p
1af60 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 0a arameter to the.
1af70 2a 2a 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 ** authorizer ca
1af80 6c 6c 62 61 63 6b 20 69 73 20 74 68 65 20 6e 61 llback is the na
1af90 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 me of the databa
1afa0 73 65 20 28 22 6d 61 69 6e 22 2c 20 22 74 65 6d se ("main", "tem
1afb0 70 22 2c 0a 2a 2a 20 65 74 63 2e 29 20 69 66 20 p",.** etc.) if
1afc0 61 70 70 6c 69 63 61 62 6c 65 2e 20 20 54 68 65 applicable. The
1afd0 20 36 74 68 20 70 61 72 61 6d 65 74 65 72 20 74 6th parameter t
1afe0 6f 20 74 68 65 20 61 75 74 68 6f 72 69 7a 65 72 o the authorizer
1aff0 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 69 73 20 callback.** is
1b000 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 the name of the
1b010 69 6e 6e 65 72 2d 6d 6f 73 74 20 74 72 69 67 67 inner-most trigg
1b020 65 72 20 6f 72 20 76 69 65 77 20 74 68 61 74 20 er or view that
1b030 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 is responsible f
1b040 6f 72 0a 2a 2a 20 74 68 65 20 61 63 63 65 73 73 or.** the access
1b050 20 61 74 74 65 6d 70 74 20 6f 72 20 4e 55 4c 4c attempt or NULL
1b060 20 69 66 20 74 68 69 73 20 61 63 63 65 73 73 20 if this access
1b070 61 74 74 65 6d 70 74 20 69 73 20 64 69 72 65 63 attempt is direc
1b080 74 6c 79 20 66 72 6f 6d 0a 2a 2a 20 74 6f 70 2d tly from.** top-
1b090 6c 65 76 65 6c 20 53 51 4c 20 63 6f 64 65 2e 0a level SQL code..
1b0a0 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e **.** Requiremen
1b0b0 74 73 3a 0a 2a 2a 20 5b 48 31 32 35 35 31 5d 20 ts:.** [H12551]
1b0c0 5b 48 31 32 35 35 32 5d 20 5b 48 31 32 35 35 33 [H12552] [H12553
1b0d0 5d 20 5b 48 31 32 35 35 34 5d 0a 2a 2f 0a 2f 2a ] [H12554].*/./*
1b0e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b0f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 33 72 64 20 2a ********** 3rd *
1b110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 34 74 68 20 *********** 4th
1b120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 64 65 ***********/.#de
1b130 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 52 45 41 fine SQLITE_CREA
1b140 54 45 5f 49 4e 44 45 58 20 20 20 20 20 20 20 20 TE_INDEX
1b150 20 20 31 20 20 20 2f 2a 20 49 6e 64 65 78 20 4e 1 /* Index N
1b160 61 6d 65 20 20 20 20 20 20 54 61 62 6c 65 20 4e ame Table N
1b170 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 ame */.#def
1b180 69 6e 65 20 53 51 4c 49 54 45 5f 43 52 45 41 54 ine SQLITE_CREAT
1b190 45 5f 54 41 42 4c 45 20 20 20 20 20 20 20 20 20 E_TABLE
1b1a0 20 32 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 2 /* Table Na
1b1b0 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 me NULL
1b1c0 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 */.#defi
1b1d0 6e 65 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 ne SQLITE_CREATE
1b1e0 5f 54 45 4d 50 5f 49 4e 44 45 58 20 20 20 20 20 _TEMP_INDEX
1b1f0 33 20 20 20 2f 2a 20 49 6e 64 65 78 20 4e 61 6d 3 /* Index Nam
1b200 65 20 20 20 20 20 20 54 61 62 6c 65 20 4e 61 6d e Table Nam
1b210 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e e */.#defin
1b220 65 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f e SQLITE_CREATE_
1b230 54 45 4d 50 5f 54 41 42 4c 45 20 20 20 20 20 34 TEMP_TABLE 4
1b240 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65 /* Table Name
1b250 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 NULL
1b260 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 */.#define
1b270 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 SQLITE_CREATE_T
1b280 45 4d 50 5f 54 52 49 47 47 45 52 20 20 20 35 20 EMP_TRIGGER 5
1b290 20 20 2f 2a 20 54 72 69 67 67 65 72 20 4e 61 6d /* Trigger Nam
1b2a0 65 20 20 20 20 54 61 62 6c 65 20 4e 61 6d 65 20 e Table Name
1b2b0 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 */.#define
1b2c0 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 45 SQLITE_CREATE_TE
1b2d0 4d 50 5f 56 49 45 57 20 20 20 20 20 20 36 20 20 MP_VIEW 6
1b2e0 20 2f 2a 20 56 69 65 77 20 4e 61 6d 65 20 20 20 /* View Name
1b2f0 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 NULL
1b300 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 */.#define S
1b310 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 52 49 QLITE_CREATE_TRI
1b320 47 47 45 52 20 20 20 20 20 20 20 20 37 20 20 20 GGER 7
1b330 2f 2a 20 54 72 69 67 67 65 72 20 4e 61 6d 65 20 /* Trigger Name
1b340 20 20 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 Table Name
1b350 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 */.#define SQ
1b360 4c 49 54 45 5f 43 52 45 41 54 45 5f 56 49 45 57 LITE_CREATE_VIEW
1b370 20 20 20 20 20 20 20 20 20 20 20 38 20 20 20 2f 8 /
1b380 2a 20 56 69 65 77 20 4e 61 6d 65 20 20 20 20 20 * View Name
1b390 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 NULL
1b3a0 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c */.#define SQL
1b3b0 49 54 45 5f 44 45 4c 45 54 45 20 20 20 20 20 20 ITE_DELETE
1b3c0 20 20 20 20 20 20 20 20 20 20 39 20 20 20 2f 2a 9 /*
1b3d0 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 Table Name
1b3e0 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 NULL
1b3f0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
1b400 54 45 5f 44 52 4f 50 5f 49 4e 44 45 58 20 20 20 TE_DROP_INDEX
1b410 20 20 20 20 20 20 20 20 31 30 20 20 20 2f 2a 20 10 /*
1b420 49 6e 64 65 78 20 4e 61 6d 65 20 20 20 20 20 20 Index Name
1b430 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 Table Name
1b440 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
1b450 45 5f 44 52 4f 50 5f 54 41 42 4c 45 20 20 20 20 E_DROP_TABLE
1b460 20 20 20 20 20 20 20 31 31 20 20 20 2f 2a 20 54 11 /* T
1b470 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 4e able Name N
1b480 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a ULL *
1b490 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
1b4a0 5f 44 52 4f 50 5f 54 45 4d 50 5f 49 4e 44 45 58 _DROP_TEMP_INDEX
1b4b0 20 20 20 20 20 20 31 32 20 20 20 2f 2a 20 49 6e 12 /* In
1b4c0 64 65 78 20 4e 61 6d 65 20 20 20 20 20 20 54 61 dex Name Ta
1b4d0 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f ble Name */
1b4e0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
1b4f0 44 52 4f 50 5f 54 45 4d 50 5f 54 41 42 4c 45 20 DROP_TEMP_TABLE
1b500 20 20 20 20 20 31 33 20 20 20 2f 2a 20 54 61 62 13 /* Tab
1b510 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 4e 55 4c le Name NUL
1b520 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a L */.
1b530 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 #define SQLITE_D
1b540 52 4f 50 5f 54 45 4d 50 5f 54 52 49 47 47 45 52 ROP_TEMP_TRIGGER
1b550 20 20 20 20 31 34 20 20 20 2f 2a 20 54 72 69 67 14 /* Trig
1b560 67 65 72 20 4e 61 6d 65 20 20 20 20 54 61 62 6c ger Name Tabl
1b570 65 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23 e Name */.#
1b580 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 52 define SQLITE_DR
1b590 4f 50 5f 54 45 4d 50 5f 56 49 45 57 20 20 20 20 OP_TEMP_VIEW
1b5a0 20 20 20 31 35 20 20 20 2f 2a 20 56 69 65 77 20 15 /* View
1b5b0 4e 61 6d 65 20 20 20 20 20 20 20 4e 55 4c 4c 20 Name NULL
1b5c0 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 */.#d
1b5d0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f efine SQLITE_DRO
1b5e0 50 5f 54 52 49 47 47 45 52 20 20 20 20 20 20 20 P_TRIGGER
1b5f0 20 20 31 36 20 20 20 2f 2a 20 54 72 69 67 67 65 16 /* Trigge
1b600 72 20 4e 61 6d 65 20 20 20 20 54 61 62 6c 65 20 r Name Table
1b610 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 Name */.#de
1b620 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 fine SQLITE_DROP
1b630 5f 56 49 45 57 20 20 20 20 20 20 20 20 20 20 20 _VIEW
1b640 20 31 37 20 20 20 2f 2a 20 56 69 65 77 20 4e 61 17 /* View Na
1b650 6d 65 20 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 me NULL
1b660 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 */.#def
1b670 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 53 45 52 ine SQLITE_INSER
1b680 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 T
1b690 31 38 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 18 /* Table Na
1b6a0 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 me NULL
1b6b0 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 */.#defi
1b6c0 6e 65 20 53 51 4c 49 54 45 5f 50 52 41 47 4d 41 ne SQLITE_PRAGMA
1b6d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
1b6e0 39 20 20 20 2f 2a 20 50 72 61 67 6d 61 20 4e 61 9 /* Pragma Na
1b6f0 6d 65 20 20 20 20 20 31 73 74 20 61 72 67 20 6f me 1st arg o
1b700 72 20 4e 55 4c 4c 20 2a 2f 0a 23 64 65 66 69 6e r NULL */.#defin
1b710 65 20 53 51 4c 49 54 45 5f 52 45 41 44 20 20 20 e SQLITE_READ
1b720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 30 20
1b730 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65 /* Table Name
1b740 20 20 20 20 20 20 43 6f 6c 75 6d 6e 20 4e 61 6d Column Nam
1b750 65 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 e */.#define
1b760 20 53 51 4c 49 54 45 5f 53 45 4c 45 43 54 20 20 SQLITE_SELECT
1b770 20 20 20 20 20 20 20 20 20 20 20 20 20 32 31 20 21
1b780 20 20 2f 2a 20 4e 55 4c 4c 20 20 20 20 20 20 20 /* NULL
1b790 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 NULL
1b7a0 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 */.#define
1b7b0 53 51 4c 49 54 45 5f 54 52 41 4e 53 41 43 54 49 SQLITE_TRANSACTI
1b7c0 4f 4e 20 20 20 20 20 20 20 20 20 20 32 32 20 20 ON 22
1b7d0 20 2f 2a 20 4f 70 65 72 61 74 69 6f 6e 20 20 20 /* Operation
1b7e0 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 NULL
1b7f0 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 */.#define S
1b800 51 4c 49 54 45 5f 55 50 44 41 54 45 20 20 20 20 QLITE_UPDATE
1b810 20 20 20 20 20 20 20 20 20 20 20 32 33 20 20 20 23
1b820 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 /* Table Name
1b830 20 20 20 43 6f 6c 75 6d 6e 20 4e 61 6d 65 20 20 Column Name
1b840 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 */.#define SQ
1b850 4c 49 54 45 5f 41 54 54 41 43 48 20 20 20 20 20 LITE_ATTACH
1b860 20 20 20 20 20 20 20 20 20 20 32 34 20 20 20 2f 24 /
1b870 2a 20 46 69 6c 65 6e 61 6d 65 20 20 20 20 20 20 * Filename
1b880 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 NULL
1b890 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c */.#define SQL
1b8a0 49 54 45 5f 44 45 54 41 43 48 20 20 20 20 20 20 ITE_DETACH
1b8b0 20 20 20 20 20 20 20 20 20 32 35 20 20 20 2f 2a 25 /*
1b8c0 20 44 61 74 61 62 61 73 65 20 4e 61 6d 65 20 20 Database Name
1b8d0 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 NULL
1b8e0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
1b8f0 54 45 5f 41 4c 54 45 52 5f 54 41 42 4c 45 20 20 TE_ALTER_TABLE
1b900 20 20 20 20 20 20 20 20 32 36 20 20 20 2f 2a 20 26 /*
1b910 44 61 74 61 62 61 73 65 20 4e 61 6d 65 20 20 20 Database Name
1b920 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 Table Name
1b930 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
1b940 45 5f 52 45 49 4e 44 45 58 20 20 20 20 20 20 20 E_REINDEX
1b950 20 20 20 20 20 20 20 32 37 20 20 20 2f 2a 20 49 27 /* I
1b960 6e 64 65 78 20 4e 61 6d 65 20 20 20 20 20 20 4e ndex Name N
1b970 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a ULL *
1b980 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
1b990 5f 41 4e 41 4c 59 5a 45 20 20 20 20 20 20 20 20 _ANALYZE
1b9a0 20 20 20 20 20 20 32 38 20 20 20 2f 2a 20 54 61 28 /* Ta
1b9b0 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 4e 55 ble Name NU
1b9c0 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f LL */
1b9d0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
1b9e0 43 52 45 41 54 45 5f 56 54 41 42 4c 45 20 20 20 CREATE_VTABLE
1b9f0 20 20 20 20 20 32 39 20 20 20 2f 2a 20 54 61 62 29 /* Tab
1ba00 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 4d 6f 64 le Name Mod
1ba10 75 6c 65 20 4e 61 6d 65 20 20 20 20 20 2a 2f 0a ule Name */.
1ba20 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 #define SQLITE_D
1ba30 52 4f 50 5f 56 54 41 42 4c 45 20 20 20 20 20 20 ROP_VTABLE
1ba40 20 20 20 20 33 30 20 20 20 2f 2a 20 54 61 62 6c 30 /* Tabl
1ba50 65 20 4e 61 6d 65 20 20 20 20 20 20 4d 6f 64 75 e Name Modu
1ba60 6c 65 20 4e 61 6d 65 20 20 20 20 20 2a 2f 0a 23 le Name */.#
1ba70 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 55 define SQLITE_FU
1ba80 4e 43 54 49 4f 4e 20 20 20 20 20 20 20 20 20 20 NCTION
1ba90 20 20 20 33 31 20 20 20 2f 2a 20 4e 55 4c 4c 20 31 /* NULL
1baa0 20 20 20 20 20 20 20 20 20 20 20 46 75 6e 63 74 Funct
1bab0 69 6f 6e 20 4e 61 6d 65 20 20 20 2a 2f 0a 23 64 ion Name */.#d
1bac0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 41 56 efine SQLITE_SAV
1bad0 45 50 4f 49 4e 54 20 20 20 20 20 20 20 20 20 20 EPOINT
1bae0 20 20 33 32 20 20 20 2f 2a 20 4f 70 65 72 61 74 32 /* Operat
1baf0 69 6f 6e 20 20 20 20 20 20 20 53 61 76 65 70 6f ion Savepo
1bb00 69 6e 74 20 4e 61 6d 65 20 20 2a 2f 0a 23 64 65 int Name */.#de
1bb10 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 50 59 fine SQLITE_COPY
1bb20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1bb30 20 20 30 20 20 20 2f 2a 20 4e 6f 20 6c 6f 6e 67 0 /* No long
1bb40 65 72 20 75 73 65 64 20 2a 2f 0a 0a 2f 2a 0a 2a er used */../*.*
1bb50 2a 20 43 41 50 49 33 52 45 46 3a 20 54 72 61 63 * CAPI3REF: Trac
1bb60 69 6e 67 20 41 6e 64 20 50 72 6f 66 69 6c 69 6e ing And Profilin
1bb70 67 20 46 75 6e 63 74 69 6f 6e 73 20 7b 48 31 32 g Functions {H12
1bb80 32 38 30 7d 20 3c 53 36 30 34 30 30 3e 0a 2a 2a 280} <S60400>.**
1bb90 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a EXPERIMENTAL.**
1bba0 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e .** These routin
1bbb0 65 73 20 72 65 67 69 73 74 65 72 20 63 61 6c 6c es register call
1bbc0 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 73 20 74 back functions t
1bbd0 68 61 74 20 63 61 6e 20 62 65 20 75 73 65 64 20 hat can be used
1bbe0 66 6f 72 0a 2a 2a 20 74 72 61 63 69 6e 67 20 61 for.** tracing a
1bbf0 6e 64 20 70 72 6f 66 69 6c 69 6e 67 20 74 68 65 nd profiling the
1bc00 20 65 78 65 63 75 74 69 6f 6e 20 6f 66 20 53 51 execution of SQ
1bc10 4c 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a L statements..**
1bc20 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 62 61 63 6b .** The callback
1bc30 20 66 75 6e 63 74 69 6f 6e 20 72 65 67 69 73 74 function regist
1bc40 65 72 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f ered by sqlite3_
1bc50 74 72 61 63 65 28 29 20 69 73 20 69 6e 76 6f 6b trace() is invok
1bc60 65 64 20 61 74 0a 2a 2a 20 76 61 72 69 6f 75 73 ed at.** various
1bc70 20 74 69 6d 65 73 20 77 68 65 6e 20 61 6e 20 53 times when an S
1bc80 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 QL statement is
1bc90 62 65 69 6e 67 20 72 75 6e 20 62 79 20 5b 73 71 being run by [sq
1bca0 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 2e 0a 2a lite3_step()]..*
1bcb0 2a 20 54 68 65 20 63 61 6c 6c 62 61 63 6b 20 72 * The callback r
1bcc0 65 74 75 72 6e 73 20 61 20 55 54 46 2d 38 20 72 eturns a UTF-8 r
1bcd0 65 6e 64 65 72 69 6e 67 20 6f 66 20 74 68 65 20 endering of the
1bce0 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 74 65 SQL statement te
1bcf0 78 74 0a 2a 2a 20 61 73 20 74 68 65 20 73 74 61 xt.** as the sta
1bd00 74 65 6d 65 6e 74 20 66 69 72 73 74 20 62 65 67 tement first beg
1bd10 69 6e 73 20 65 78 65 63 75 74 69 6e 67 2e 20 20 ins executing.
1bd20 41 64 64 69 74 69 6f 6e 61 6c 20 63 61 6c 6c 62 Additional callb
1bd30 61 63 6b 73 20 6f 63 63 75 72 0a 2a 2a 20 61 73 acks occur.** as
1bd40 20 65 61 63 68 20 74 72 69 67 67 65 72 65 64 20 each triggered
1bd50 73 75 62 70 72 6f 67 72 61 6d 20 69 73 20 65 6e subprogram is en
1bd60 74 65 72 65 64 2e 20 20 54 68 65 20 63 61 6c 6c tered. The call
1bd70 62 61 63 6b 73 20 66 6f 72 20 74 72 69 67 67 65 backs for trigge
1bd80 72 73 0a 2a 2a 20 63 6f 6e 74 61 69 6e 20 61 20 rs.** contain a
1bd90 55 54 46 2d 38 20 53 51 4c 20 63 6f 6d 6d 65 6e UTF-8 SQL commen
1bda0 74 20 74 68 61 74 20 69 64 65 6e 74 69 66 69 65 t that identifie
1bdb0 73 20 74 68 65 20 74 72 69 67 67 65 72 2e 0a 2a s the trigger..*
1bdc0 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 62 61 63 *.** The callbac
1bdd0 6b 20 66 75 6e 63 74 69 6f 6e 20 72 65 67 69 73 k function regis
1bde0 74 65 72 65 64 20 62 79 20 73 71 6c 69 74 65 33 tered by sqlite3
1bdf0 5f 70 72 6f 66 69 6c 65 28 29 20 69 73 20 69 6e _profile() is in
1be00 76 6f 6b 65 64 0a 2a 2a 20 61 73 20 65 61 63 68 voked.** as each
1be10 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 66 SQL statement f
1be20 69 6e 69 73 68 65 73 2e 20 20 54 68 65 20 70 72 inishes. The pr
1be30 6f 66 69 6c 65 20 63 61 6c 6c 62 61 63 6b 20 63 ofile callback c
1be40 6f 6e 74 61 69 6e 73 0a 2a 2a 20 74 68 65 20 6f ontains.** the o
1be50 72 69 67 69 6e 61 6c 20 73 74 61 74 65 6d 65 6e riginal statemen
1be60 74 20 74 65 78 74 20 61 6e 64 20 61 6e 20 65 73 t text and an es
1be70 74 69 6d 61 74 65 20 6f 66 20 77 61 6c 6c 2d 63 timate of wall-c
1be80 6c 6f 63 6b 20 74 69 6d 65 0a 2a 2a 20 6f 66 20 lock time.** of
1be90 68 6f 77 20 6c 6f 6e 67 20 74 68 61 74 20 73 74 how long that st
1bea0 61 74 65 6d 65 6e 74 20 74 6f 6f 6b 20 74 6f 20 atement took to
1beb0 72 75 6e 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 run..**.** Requi
1bec0 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 rements:.** [H12
1bed0 32 38 31 5d 20 5b 48 31 32 32 38 32 5d 20 5b 48 281] [H12282] [H
1bee0 31 32 32 38 33 5d 20 5b 48 31 32 32 38 34 5d 20 12283] [H12284]
1bef0 5b 48 31 32 32 38 35 5d 20 5b 48 31 32 32 38 37 [H12285] [H12287
1bf00 5d 20 5b 48 31 32 32 38 38 5d 20 5b 48 31 32 32 ] [H12288] [H122
1bf10 38 39 5d 0a 2a 2a 20 5b 48 31 32 32 39 30 5d 0a 89].** [H12290].
1bf20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 */.SQLITE_API SQ
1bf30 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 LITE_EXPERIMENTA
1bf40 4c 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f L void *sqlite3_
1bf50 74 72 61 63 65 28 73 71 6c 69 74 65 33 2a 2c 20 trace(sqlite3*,
1bf60 76 6f 69 64 28 2a 78 54 72 61 63 65 29 28 76 6f void(*xTrace)(vo
1bf70 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 id*,const char*)
1bf80 2c 20 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45 , void*);.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 70 72 6f 66 69 6c 65 28 73 qlite3_profile(s
1bfc0 71 6c 69 74 65 33 2a 2c 0a 20 20 20 76 6f 69 64 qlite3*,. void
1bfd0 28 2a 78 50 72 6f 66 69 6c 65 29 28 76 6f 69 64 (*xProfile)(void
1bfe0 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 73 71 *,const char*,sq
1bff0 6c 69 74 65 33 5f 75 69 6e 74 36 34 29 2c 20 76 lite3_uint64), v
1c000 6f 69 64 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 oid*);../*.** CA
1c010 50 49 33 52 45 46 3a 20 51 75 65 72 79 20 50 72 PI3REF: Query Pr
1c020 6f 67 72 65 73 73 20 43 61 6c 6c 62 61 63 6b 73 ogress Callbacks
1c030 20 7b 48 31 32 39 31 30 7d 20 3c 53 36 30 34 30 {H12910} <S6040
1c040 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 0>.**.** This ro
1c050 75 74 69 6e 65 20 63 6f 6e 66 69 67 75 72 65 73 utine configures
1c060 20 61 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 a callback func
1c070 74 69 6f 6e 20 2d 20 74 68 65 0a 2a 2a 20 70 72 tion - the.** pr
1c080 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 ogress callback
1c090 2d 20 74 68 61 74 20 69 73 20 69 6e 76 6f 6b 65 - that is invoke
1c0a0 64 20 70 65 72 69 6f 64 69 63 61 6c 6c 79 20 64 d periodically d
1c0b0 75 72 69 6e 67 20 6c 6f 6e 67 0a 2a 2a 20 72 75 uring long.** ru
1c0c0 6e 6e 69 6e 67 20 63 61 6c 6c 73 20 74 6f 20 5b nning calls to [
1c0d0 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 5d 2c sqlite3_exec()],
1c0e0 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 [sqlite3_step()
1c0f0 5d 20 61 6e 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 ] and.** [sqlite
1c100 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 5d 2e 20 3_get_table()].
1c110 20 41 6e 20 65 78 61 6d 70 6c 65 20 75 73 65 20 An example use
1c120 66 6f 72 20 74 68 69 73 0a 2a 2a 20 69 6e 74 65 for this.** inte
1c130 72 66 61 63 65 20 69 73 20 74 6f 20 6b 65 65 70 rface is to keep
1c140 20 61 20 47 55 49 20 75 70 64 61 74 65 64 20 64 a GUI updated d
1c150 75 72 69 6e 67 20 61 20 6c 61 72 67 65 20 71 75 uring a large qu
1c160 65 72 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 ery..**.** If th
1c170 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 e progress callb
1c180 61 63 6b 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d ack returns non-
1c190 7a 65 72 6f 2c 20 74 68 65 20 6f 70 65 72 61 74 zero, the operat
1c1a0 69 6f 6e 20 69 73 0a 2a 2a 20 69 6e 74 65 72 72 ion is.** interr
1c1b0 75 70 74 65 64 2e 20 20 54 68 69 73 20 66 65 61 upted. This fea
1c1c0 74 75 72 65 20 63 61 6e 20 62 65 20 75 73 65 64 ture can be used
1c1d0 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 61 0a to implement a.
1c1e0 2a 2a 20 22 43 61 6e 63 65 6c 22 20 62 75 74 74 ** "Cancel" butt
1c1f0 6f 6e 20 6f 6e 20 61 20 47 55 49 20 70 72 6f 67 on on a GUI prog
1c200 72 65 73 73 20 64 69 61 6c 6f 67 20 62 6f 78 2e ress dialog box.
1c210 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 72 6f 67 72 .**.** The progr
1c220 65 73 73 20 68 61 6e 64 6c 65 72 20 6d 75 73 74 ess handler must
1c230 20 6e 6f 74 20 64 6f 20 61 6e 79 74 68 69 6e 67 not do anything
1c240 20 74 68 61 74 20 77 69 6c 6c 20 6d 6f 64 69 66 that will modif
1c250 79 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 y.** the databas
1c260 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61 e connection tha
1c270 74 20 69 6e 76 6f 6b 65 64 20 74 68 65 20 70 72 t invoked the pr
1c280 6f 67 72 65 73 73 20 68 61 6e 64 6c 65 72 2e 0a ogress handler..
1c290 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 5b 73 71 ** Note that [sq
1c2a0 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 lite3_prepare_v2
1c2b0 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 ()] and [sqlite3
1c2c0 5f 73 74 65 70 28 29 5d 20 62 6f 74 68 20 6d 6f _step()] both mo
1c2d0 64 69 66 79 20 74 68 65 69 72 0a 2a 2a 20 64 61 dify their.** da
1c2e0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
1c2f0 6e 73 20 66 6f 72 20 74 68 65 20 6d 65 61 6e 69 ns for the meani
1c300 6e 67 20 6f 66 20 22 6d 6f 64 69 66 79 22 20 69 ng of "modify" i
1c310 6e 20 74 68 69 73 20 70 61 72 61 67 72 61 70 68 n this paragraph
1c320 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d ..**.** Requirem
1c330 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 39 31 31 ents:.** [H12911
1c340 5d 20 5b 48 31 32 39 31 32 5d 20 5b 48 31 32 39 ] [H12912] [H129
1c350 31 33 5d 20 5b 48 31 32 39 31 34 5d 20 5b 48 31 13] [H12914] [H1
1c360 32 39 31 35 5d 20 5b 48 31 32 39 31 36 5d 20 5b 2915] [H12916] [
1c370 48 31 32 39 31 37 5d 20 5b 48 31 32 39 31 38 5d H12917] [H12918]
1c380 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 .**.*/.SQLITE_AP
1c390 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 70 I void sqlite3_p
1c3a0 72 6f 67 72 65 73 73 5f 68 61 6e 64 6c 65 72 28 rogress_handler(
1c3b0 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 2c 20 69 sqlite3*, int, i
1c3c0 6e 74 28 2a 29 28 76 6f 69 64 2a 29 2c 20 76 6f nt(*)(void*), vo
1c3d0 69 64 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 id*);../*.** CAP
1c3e0 49 33 52 45 46 3a 20 4f 70 65 6e 69 6e 67 20 41 I3REF: Opening A
1c3f0 20 4e 65 77 20 44 61 74 61 62 61 73 65 20 43 6f New Database Co
1c400 6e 6e 65 63 74 69 6f 6e 20 7b 48 31 32 37 30 30 nnection {H12700
1c410 7d 20 3c 53 34 30 32 30 30 3e 0a 2a 2a 0a 2a 2a } <S40200>.**.**
1c420 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 These routines
1c430 6f 70 65 6e 20 61 6e 20 53 51 4c 69 74 65 20 64 open an SQLite d
1c440 61 74 61 62 61 73 65 20 66 69 6c 65 20 77 68 6f atabase file who
1c450 73 65 20 6e 61 6d 65 20 69 73 20 67 69 76 65 6e se name is given
1c460 20 62 79 20 74 68 65 0a 2a 2a 20 66 69 6c 65 6e by the.** filen
1c470 61 6d 65 20 61 72 67 75 6d 65 6e 74 2e 20 54 68 ame argument. Th
1c480 65 20 66 69 6c 65 6e 61 6d 65 20 61 72 67 75 6d e filename argum
1c490 65 6e 74 20 69 73 20 69 6e 74 65 72 70 72 65 74 ent is interpret
1c4a0 65 64 20 61 73 20 55 54 46 2d 38 20 66 6f 72 0a ed as UTF-8 for.
1c4b0 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 ** sqlite3_open(
1c4c0 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6f 70 ) and sqlite3_op
1c4d0 65 6e 5f 76 32 28 29 20 61 6e 64 20 61 73 20 55 en_v2() and as U
1c4e0 54 46 2d 31 36 20 69 6e 20 74 68 65 20 6e 61 74 TF-16 in the nat
1c4f0 69 76 65 20 62 79 74 65 0a 2a 2a 20 6f 72 64 65 ive byte.** orde
1c500 72 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 r for sqlite3_op
1c510 65 6e 31 36 28 29 2e 20 41 20 5b 64 61 74 61 62 en16(). A [datab
1c520 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 ase connection]
1c530 68 61 6e 64 6c 65 20 69 73 20 75 73 75 61 6c 6c handle is usuall
1c540 79 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 69 6e y.** returned in
1c550 20 2a 70 70 44 62 2c 20 65 76 65 6e 20 69 66 20 *ppDb, even if
1c560 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e an error occurs.
1c570 20 20 54 68 65 20 6f 6e 6c 79 20 65 78 63 65 70 The only excep
1c580 74 69 6f 6e 20 69 73 20 74 68 61 74 0a 2a 2a 20 tion is that.**
1c590 69 66 20 53 51 4c 69 74 65 20 69 73 20 75 6e 61 if SQLite is una
1c5a0 62 6c 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 ble to allocate
1c5b0 6d 65 6d 6f 72 79 20 74 6f 20 68 6f 6c 64 20 74 memory to hold t
1c5c0 68 65 20 5b 73 71 6c 69 74 65 33 5d 20 6f 62 6a he [sqlite3] obj
1c5d0 65 63 74 2c 0a 2a 2a 20 61 20 4e 55 4c 4c 20 77 ect,.** a NULL w
1c5e0 69 6c 6c 20 62 65 20 77 72 69 74 74 65 6e 20 69 ill be written i
1c5f0 6e 74 6f 20 2a 70 70 44 62 20 69 6e 73 74 65 61 nto *ppDb instea
1c600 64 20 6f 66 20 61 20 70 6f 69 6e 74 65 72 20 74 d of a pointer t
1c610 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5d 0a o the [sqlite3].
1c620 2a 2a 20 6f 62 6a 65 63 74 2e 20 49 66 20 74 68 ** object. If th
1c630 65 20 64 61 74 61 62 61 73 65 20 69 73 20 6f 70 e database is op
1c640 65 6e 65 64 20 28 61 6e 64 2f 6f 72 20 63 72 65 ened (and/or cre
1c650 61 74 65 64 29 20 73 75 63 63 65 73 73 66 75 6c ated) successful
1c660 6c 79 2c 20 74 68 65 6e 0a 2a 2a 20 5b 53 51 4c ly, then.** [SQL
1c670 49 54 45 5f 4f 4b 5d 20 69 73 20 72 65 74 75 72 ITE_OK] is retur
1c680 6e 65 64 2e 20 20 4f 74 68 65 72 77 69 73 65 20 ned. Otherwise
1c690 61 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 an [error code]
1c6a0 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 54 68 is returned. Th
1c6b0 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 65 72 e.** [sqlite3_er
1c6c0 72 6d 73 67 28 29 5d 20 6f 72 20 5b 73 71 6c 69 rmsg()] or [sqli
1c6d0 74 65 33 5f 65 72 72 6d 73 67 31 36 28 29 5d 20 te3_errmsg16()]
1c6e0 72 6f 75 74 69 6e 65 73 20 63 61 6e 20 62 65 20 routines can be
1c6f0 75 73 65 64 20 74 6f 20 6f 62 74 61 69 6e 0a 2a used to obtain.*
1c700 2a 20 61 6e 20 45 6e 67 6c 69 73 68 20 6c 61 6e * an English lan
1c710 67 75 61 67 65 20 64 65 73 63 72 69 70 74 69 6f guage descriptio
1c720 6e 20 6f 66 20 74 68 65 20 65 72 72 6f 72 2e 0a n of the error..
1c730 2a 2a 0a 2a 2a 20 54 68 65 20 64 65 66 61 75 6c **.** The defaul
1c740 74 20 65 6e 63 6f 64 69 6e 67 20 66 6f 72 20 74 t encoding for t
1c750 68 65 20 64 61 74 61 62 61 73 65 20 77 69 6c 6c he database will
1c760 20 62 65 20 55 54 46 2d 38 20 69 66 0a 2a 2a 20 be UTF-8 if.**
1c770 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 20 6f sqlite3_open() o
1c780 72 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 r sqlite3_open_v
1c790 32 28 29 20 69 73 20 63 61 6c 6c 65 64 20 61 6e 2() is called an
1c7a0 64 0a 2a 2a 20 55 54 46 2d 31 36 20 69 6e 20 74 d.** UTF-16 in t
1c7b0 68 65 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f he native byte o
1c7c0 72 64 65 72 20 69 66 20 73 71 6c 69 74 65 33 5f rder if sqlite3_
1c7d0 6f 70 65 6e 31 36 28 29 20 69 73 20 75 73 65 64 open16() is used
1c7e0 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 74 68 65 72 20 ..**.** Whether
1c7f0 6f 72 20 6e 6f 74 20 61 6e 20 65 72 72 6f 72 20 or not an error
1c800 6f 63 63 75 72 73 20 77 68 65 6e 20 69 74 20 69 occurs when it i
1c810 73 20 6f 70 65 6e 65 64 2c 20 72 65 73 6f 75 72 s opened, resour
1c820 63 65 73 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 ces.** associate
1c830 64 20 77 69 74 68 20 74 68 65 20 5b 64 61 74 61 d with the [data
1c840 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d base connection]
1c850 20 68 61 6e 64 6c 65 20 73 68 6f 75 6c 64 20 62 handle should b
1c860 65 20 72 65 6c 65 61 73 65 64 20 62 79 0a 2a 2a e released by.**
1c870 20 70 61 73 73 69 6e 67 20 69 74 20 74 6f 20 5b passing it to [
1c880 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 29 5d sqlite3_close()]
1c890 20 77 68 65 6e 20 69 74 20 69 73 20 6e 6f 20 6c when it is no l
1c8a0 6f 6e 67 65 72 20 72 65 71 75 69 72 65 64 2e 0a onger required..
1c8b0 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 **.** The sqlite
1c8c0 33 5f 6f 70 65 6e 5f 76 32 28 29 20 69 6e 74 65 3_open_v2() inte
1c8d0 72 66 61 63 65 20 77 6f 72 6b 73 20 6c 69 6b 65 rface works like
1c8e0 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 0a sqlite3_open().
1c8f0 2a 2a 20 65 78 63 65 70 74 20 74 68 61 74 20 69 ** except that i
1c900 74 20 61 63 63 65 70 74 73 20 74 77 6f 20 61 64 t accepts two ad
1c910 64 69 74 69 6f 6e 61 6c 20 70 61 72 61 6d 65 74 ditional paramet
1c920 65 72 73 20 66 6f 72 20 61 64 64 69 74 69 6f 6e ers for addition
1c930 61 6c 20 63 6f 6e 74 72 6f 6c 0a 2a 2a 20 6f 76 al control.** ov
1c940 65 72 20 74 68 65 20 6e 65 77 20 64 61 74 61 62 er the new datab
1c950 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 ase connection.
1c960 20 54 68 65 20 66 6c 61 67 73 20 70 61 72 61 6d The flags param
1c970 65 74 65 72 20 63 61 6e 20 74 61 6b 65 20 6f 6e eter can take on
1c980 65 20 6f 66 0a 2a 2a 20 74 68 65 20 66 6f 6c 6c e of.** the foll
1c990 6f 77 69 6e 67 20 74 68 72 65 65 20 76 61 6c 75 owing three valu
1c9a0 65 73 2c 20 6f 70 74 69 6f 6e 61 6c 6c 79 20 63 es, optionally c
1c9b0 6f 6d 62 69 6e 65 64 20 77 69 74 68 20 74 68 65 ombined with the
1c9c0 20 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f 50 45 .** [SQLITE_OPE
1c9d0 4e 5f 4e 4f 4d 55 54 45 58 5d 2c 20 5b 53 51 4c N_NOMUTEX], [SQL
1c9e0 49 54 45 5f 4f 50 45 4e 5f 46 55 4c 4c 4d 55 54 ITE_OPEN_FULLMUT
1c9f0 45 58 5d 2c 20 5b 53 51 4c 49 54 45 5f 4f 50 45 EX], [SQLITE_OPE
1ca00 4e 5f 53 48 41 52 45 44 43 41 43 48 45 5d 2c 0a N_SHAREDCACHE],.
1ca10 2a 2a 20 61 6e 64 2f 6f 72 20 5b 53 51 4c 49 54 ** and/or [SQLIT
1ca20 45 5f 4f 50 45 4e 5f 50 52 49 56 41 54 45 43 41 E_OPEN_PRIVATECA
1ca30 43 48 45 5d 20 66 6c 61 67 73 3a 0a 2a 2a 0a 2a CHE] flags:.**.*
1ca40 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 3e 5b 53 * <dl>.** <dt>[S
1ca50 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f QLITE_OPEN_READO
1ca60 4e 4c 59 5d 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 NLY]</dt>.** <dd
1ca70 3e 54 68 65 20 64 61 74 61 62 61 73 65 20 69 73 >The database is
1ca80 20 6f 70 65 6e 65 64 20 69 6e 20 72 65 61 64 2d opened in read-
1ca90 6f 6e 6c 79 20 6d 6f 64 65 2e 20 20 49 66 20 74 only mode. If t
1caa0 68 65 20 64 61 74 61 62 61 73 65 20 64 6f 65 73 he database does
1cab0 20 6e 6f 74 0a 2a 2a 20 61 6c 72 65 61 64 79 20 not.** already
1cac0 65 78 69 73 74 2c 20 61 6e 20 65 72 72 6f 72 20 exist, an error
1cad0 69 73 20 72 65 74 75 72 6e 65 64 2e 3c 2f 64 64 is returned.</dd
1cae0 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 5b 53 51 4c >.**.** <dt>[SQL
1caf0 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 ITE_OPEN_READWRI
1cb00 54 45 5d 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e TE]</dt>.** <dd>
1cb10 54 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 The database is
1cb20 6f 70 65 6e 65 64 20 66 6f 72 20 72 65 61 64 69 opened for readi
1cb30 6e 67 20 61 6e 64 20 77 72 69 74 69 6e 67 20 69 ng and writing i
1cb40 66 20 70 6f 73 73 69 62 6c 65 2c 20 6f 72 20 72 f possible, or r
1cb50 65 61 64 69 6e 67 0a 2a 2a 20 6f 6e 6c 79 20 69 eading.** only i
1cb60 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 77 72 f the file is wr
1cb70 69 74 65 20 70 72 6f 74 65 63 74 65 64 20 62 79 ite protected by
1cb80 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73 the operating s
1cb90 79 73 74 65 6d 2e 20 20 49 6e 20 65 69 74 68 65 ystem. In eithe
1cba0 72 0a 2a 2a 20 63 61 73 65 20 74 68 65 20 64 61 r.** case the da
1cbb0 74 61 62 61 73 65 20 6d 75 73 74 20 61 6c 72 65 tabase must alre
1cbc0 61 64 79 20 65 78 69 73 74 2c 20 6f 74 68 65 72 ady exist, other
1cbd0 77 69 73 65 20 61 6e 20 65 72 72 6f 72 20 69 73 wise an error is
1cbe0 20 72 65 74 75 72 6e 65 64 2e 3c 2f 64 64 3e 0a returned.</dd>.
1cbf0 2a 2a 0a 2a 2a 20 3c 64 74 3e 5b 53 51 4c 49 54 **.** <dt>[SQLIT
1cc00 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 E_OPEN_READWRITE
1cc10 5d 20 7c 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e ] | [SQLITE_OPEN
1cc20 5f 43 52 45 41 54 45 5d 3c 2f 64 74 3e 0a 2a 2a _CREATE]</dt>.**
1cc30 20 3c 64 64 3e 54 68 65 20 64 61 74 61 62 61 73 <dd>The databas
1cc40 65 20 69 73 20 6f 70 65 6e 65 64 20 66 6f 72 20 e is opened for
1cc50 72 65 61 64 69 6e 67 20 61 6e 64 20 77 72 69 74 reading and writ
1cc60 69 6e 67 2c 20 61 6e 64 20 69 73 20 63 72 65 61 ing, and is crea
1cc70 74 65 73 20 69 74 20 69 66 0a 2a 2a 20 69 74 20 tes it if.** it
1cc80 64 6f 65 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 does not already
1cc90 20 65 78 69 73 74 2e 20 54 68 69 73 20 69 73 20 exist. This is
1cca0 74 68 65 20 62 65 68 61 76 69 6f 72 20 74 68 61 the behavior tha
1ccb0 74 20 69 73 20 61 6c 77 61 79 73 20 75 73 65 64 t is always used
1ccc0 20 66 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f for.** sqlite3_
1ccd0 6f 70 65 6e 28 29 20 61 6e 64 20 73 71 6c 69 74 open() and sqlit
1cce0 65 33 5f 6f 70 65 6e 31 36 28 29 2e 3c 2f 64 64 e3_open16().</dd
1ccf0 3e 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2a 0a 2a 2a >.** </dl>.**.**
1cd00 20 49 66 20 74 68 65 20 33 72 64 20 70 61 72 61 If the 3rd para
1cd10 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 meter to sqlite3
1cd20 5f 6f 70 65 6e 5f 76 32 28 29 20 69 73 20 6e 6f _open_v2() is no
1cd30 74 20 6f 6e 65 20 6f 66 20 74 68 65 0a 2a 2a 20 t one of the.**
1cd40 63 6f 6d 62 69 6e 61 74 69 6f 6e 73 20 73 68 6f combinations sho
1cd50 77 6e 20 61 62 6f 76 65 20 6f 72 20 6f 6e 65 20 wn above or one
1cd60 6f 66 20 74 68 65 20 63 6f 6d 62 69 6e 61 74 69 of the combinati
1cd70 6f 6e 73 20 73 68 6f 77 6e 20 61 62 6f 76 65 20 ons shown above
1cd80 63 6f 6d 62 69 6e 65 64 0a 2a 2a 20 77 69 74 68 combined.** with
1cd90 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 4f 50 45 the [SQLITE_OPE
1cda0 4e 5f 4e 4f 4d 55 54 45 58 5d 2c 20 5b 53 51 4c N_NOMUTEX], [SQL
1cdb0 49 54 45 5f 4f 50 45 4e 5f 46 55 4c 4c 4d 55 54 ITE_OPEN_FULLMUT
1cdc0 45 58 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f EX],.** [SQLITE_
1cdd0 4f 50 45 4e 5f 53 48 41 52 45 44 43 41 43 48 45 OPEN_SHAREDCACHE
1cde0 5d 20 61 6e 64 2f 6f 72 20 5b 53 51 4c 49 54 45 ] and/or [SQLITE
1cdf0 5f 4f 50 45 4e 5f 53 48 41 52 45 44 43 41 43 48 _OPEN_SHAREDCACH
1ce00 45 5d 20 66 6c 61 67 73 2c 0a 2a 2a 20 74 68 65 E] flags,.** the
1ce10 6e 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 69 n the behavior i
1ce20 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a s undefined..**.
1ce30 2a 2a 20 49 66 20 74 68 65 20 5b 53 51 4c 49 54 ** If the [SQLIT
1ce40 45 5f 4f 50 45 4e 5f 4e 4f 4d 55 54 45 58 5d 20 E_OPEN_NOMUTEX]
1ce50 66 6c 61 67 20 69 73 20 73 65 74 2c 20 74 68 65 flag is set, the
1ce60 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 n the database c
1ce70 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 6f 70 65 onnection.** ope
1ce80 6e 73 20 69 6e 20 74 68 65 20 6d 75 6c 74 69 2d ns in the multi-
1ce90 74 68 72 65 61 64 20 5b 74 68 72 65 61 64 69 6e thread [threadin
1cea0 67 20 6d 6f 64 65 5d 20 61 73 20 6c 6f 6e 67 20 g mode] as long
1ceb0 61 73 20 74 68 65 20 73 69 6e 67 6c 65 2d 74 68 as the single-th
1cec0 72 65 61 64 0a 2a 2a 20 6d 6f 64 65 20 68 61 73 read.** mode has
1ced0 20 6e 6f 74 20 62 65 65 6e 20 73 65 74 20 61 74 not been set at
1cee0 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 72 compile-time or
1cef0 20 73 74 61 72 74 2d 74 69 6d 65 2e 20 20 49 66 start-time. If
1cf00 20 74 68 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f the.** [SQLITE_
1cf10 4f 50 45 4e 5f 46 55 4c 4c 4d 55 54 45 58 5d 20 OPEN_FULLMUTEX]
1cf20 66 6c 61 67 20 69 73 20 73 65 74 20 74 68 65 6e flag is set then
1cf30 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f the database co
1cf40 6e 6e 65 63 74 69 6f 6e 20 6f 70 65 6e 73 0a 2a nnection opens.*
1cf50 2a 20 69 6e 20 74 68 65 20 73 65 72 69 61 6c 69 * in the seriali
1cf60 7a 65 64 20 5b 74 68 72 65 61 64 69 6e 67 20 6d zed [threading m
1cf70 6f 64 65 5d 20 75 6e 6c 65 73 73 20 73 69 6e 67 ode] unless sing
1cf80 6c 65 2d 74 68 72 65 61 64 20 77 61 73 0a 2a 2a le-thread was.**
1cf90 20 70 72 65 76 69 6f 75 73 6c 79 20 73 65 6c 65 previously sele
1cfa0 63 74 65 64 20 61 74 20 63 6f 6d 70 69 6c 65 2d cted at compile-
1cfb0 74 69 6d 65 20 6f 72 20 73 74 61 72 74 2d 74 69 time or start-ti
1cfc0 6d 65 2e 0a 2a 2a 20 54 68 65 20 5b 53 51 4c 49 me..** The [SQLI
1cfd0 54 45 5f 4f 50 45 4e 5f 53 48 41 52 45 44 43 41 TE_OPEN_SHAREDCA
1cfe0 43 48 45 5d 20 66 6c 61 67 20 63 61 75 73 65 73 CHE] flag causes
1cff0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f the database co
1d000 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 62 65 0a 2a nnection to be.*
1d010 2a 20 65 6c 69 67 69 62 6c 65 20 74 6f 20 75 73 * eligible to us
1d020 65 20 5b 73 68 61 72 65 64 20 63 61 63 68 65 20 e [shared cache
1d030 6d 6f 64 65 5d 2c 20 72 65 67 61 72 64 6c 65 73 mode], regardles
1d040 73 20 6f 66 20 77 68 65 74 68 65 72 20 6f 72 20 s of whether or
1d050 6e 6f 74 20 73 68 61 72 65 64 0a 2a 2a 20 63 61 not shared.** ca
1d060 63 68 65 20 69 73 20 65 6e 61 62 6c 65 64 20 75 che is enabled u
1d070 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 65 6e sing [sqlite3_en
1d080 61 62 6c 65 5f 73 68 61 72 65 64 5f 63 61 63 68 able_shared_cach
1d090 65 28 29 5d 2e 20 20 54 68 65 0a 2a 2a 20 5b 53 e()]. The.** [S
1d0a0 51 4c 49 54 45 5f 4f 50 45 4e 5f 50 52 49 56 41 QLITE_OPEN_PRIVA
1d0b0 54 45 43 41 43 48 45 5d 20 66 6c 61 67 20 63 61 TECACHE] flag ca
1d0c0 75 73 65 73 20 74 68 65 20 64 61 74 61 62 61 73 uses the databas
1d0d0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 e connection to
1d0e0 6e 6f 74 0a 2a 2a 20 70 61 72 74 69 63 69 70 61 not.** participa
1d0f0 74 65 20 69 6e 20 5b 73 68 61 72 65 64 20 63 61 te in [shared ca
1d100 63 68 65 20 6d 6f 64 65 5d 20 65 76 65 6e 20 69 che mode] even i
1d110 66 20 69 74 20 69 73 20 65 6e 61 62 6c 65 64 2e f it is enabled.
1d120 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 69 .**.** If the fi
1d130 6c 65 6e 61 6d 65 20 69 73 20 22 3a 6d 65 6d 6f lename is ":memo
1d140 72 79 3a 22 2c 20 74 68 65 6e 20 61 20 70 72 69 ry:", then a pri
1d150 76 61 74 65 2c 20 74 65 6d 70 6f 72 61 72 79 20 vate, temporary
1d160 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 in-memory databa
1d170 73 65 0a 2a 2a 20 69 73 20 63 72 65 61 74 65 64 se.** is created
1d180 20 66 6f 72 20 74 68 65 20 63 6f 6e 6e 65 63 74 for the connect
1d190 69 6f 6e 2e 20 20 54 68 69 73 20 69 6e 2d 6d 65 ion. This in-me
1d1a0 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20 77 69 mory database wi
1d1b0 6c 6c 20 76 61 6e 69 73 68 20 77 68 65 6e 0a 2a ll vanish when.*
1d1c0 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 * the database c
1d1d0 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 63 6c 6f onnection is clo
1d1e0 73 65 64 2e 20 20 46 75 74 75 72 65 20 76 65 72 sed. Future ver
1d1f0 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 sions of SQLite
1d200 6d 69 67 68 74 0a 2a 2a 20 6d 61 6b 65 20 75 73 might.** make us
1d210 65 20 6f 66 20 61 64 64 69 74 69 6f 6e 61 6c 20 e of additional
1d220 73 70 65 63 69 61 6c 20 66 69 6c 65 6e 61 6d 65 special filename
1d230 73 20 74 68 61 74 20 62 65 67 69 6e 20 77 69 74 s that begin wit
1d240 68 20 74 68 65 20 22 3a 22 20 63 68 61 72 61 63 h the ":" charac
1d250 74 65 72 2e 0a 2a 2a 20 49 74 20 69 73 20 72 65 ter..** It is re
1d260 63 6f 6d 6d 65 6e 64 65 64 20 74 68 61 74 20 77 commended that w
1d270 68 65 6e 20 61 20 64 61 74 61 62 61 73 65 20 66 hen a database f
1d280 69 6c 65 6e 61 6d 65 20 61 63 74 75 61 6c 6c 79 ilename actually
1d290 20 64 6f 65 73 20 62 65 67 69 6e 20 77 69 74 68 does begin with
1d2a0 0a 2a 2a 20 61 20 22 3a 22 20 63 68 61 72 61 63 .** a ":" charac
1d2b0 74 65 72 20 79 6f 75 20 73 68 6f 75 6c 64 20 70 ter you should p
1d2c0 72 65 66 69 78 20 74 68 65 20 66 69 6c 65 6e 61 refix the filena
1d2d0 6d 65 20 77 69 74 68 20 61 20 70 61 74 68 6e 61 me with a pathna
1d2e0 6d 65 20 73 75 63 68 20 61 73 0a 2a 2a 20 22 2e me such as.** ".
1d2f0 2f 22 20 74 6f 20 61 76 6f 69 64 20 61 6d 62 69 /" to avoid ambi
1d300 67 75 69 74 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 guity..**.** If
1d310 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 69 73 20 the filename is
1d320 61 6e 20 65 6d 70 74 79 20 73 74 72 69 6e 67 2c an empty string,
1d330 20 74 68 65 6e 20 61 20 70 72 69 76 61 74 65 2c then a private,
1d340 20 74 65 6d 70 6f 72 61 72 79 0a 2a 2a 20 6f 6e temporary.** on
1d350 2d 64 69 73 6b 20 64 61 74 61 62 61 73 65 20 77 -disk database w
1d360 69 6c 6c 20 62 65 20 63 72 65 61 74 65 64 2e 20 ill be created.
1d370 20 54 68 69 73 20 70 72 69 76 61 74 65 20 64 61 This private da
1d380 74 61 62 61 73 65 20 77 69 6c 6c 20 62 65 0a 2a tabase will be.*
1d390 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 * automatically
1d3a0 64 65 6c 65 74 65 64 20 61 73 20 73 6f 6f 6e 20 deleted as soon
1d3b0 61 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 as the database
1d3c0 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 63 6c connection is cl
1d3d0 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 osed..**.** The
1d3e0 66 6f 75 72 74 68 20 70 61 72 61 6d 65 74 65 72 fourth parameter
1d3f0 20 74 6f 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e to sqlite3_open
1d400 5f 76 32 28 29 20 69 73 20 74 68 65 20 6e 61 6d _v2() is the nam
1d410 65 20 6f 66 20 74 68 65 0a 2a 2a 20 5b 73 71 6c e of the.** [sql
1d420 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63 74 ite3_vfs] object
1d430 20 74 68 61 74 20 64 65 66 69 6e 65 73 20 74 68 that defines th
1d440 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 e operating syst
1d450 65 6d 20 69 6e 74 65 72 66 61 63 65 20 74 68 61 em interface tha
1d460 74 0a 2a 2a 20 74 68 65 20 6e 65 77 20 64 61 74 t.** the new dat
1d470 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
1d480 20 73 68 6f 75 6c 64 20 75 73 65 2e 20 20 49 66 should use. If
1d490 20 74 68 65 20 66 6f 75 72 74 68 20 70 61 72 61 the fourth para
1d4a0 6d 65 74 65 72 20 69 73 0a 2a 2a 20 61 20 4e 55 meter is.** a NU
1d4b0 4c 4c 20 70 6f 69 6e 74 65 72 20 74 68 65 6e 20 LL pointer then
1d4c0 74 68 65 20 64 65 66 61 75 6c 74 20 5b 73 71 6c the default [sql
1d4d0 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63 74 ite3_vfs] object
1d4e0 20 69 73 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 is used..**.**
1d4f0 3c 62 3e 4e 6f 74 65 20 74 6f 20 57 69 6e 64 6f <b>Note to Windo
1d500 77 73 20 75 73 65 72 73 3a 3c 2f 62 3e 20 20 54 ws users:</b> T
1d510 68 65 20 65 6e 63 6f 64 69 6e 67 20 75 73 65 64 he encoding used
1d520 20 66 6f 72 20 74 68 65 20 66 69 6c 65 6e 61 6d for the filenam
1d530 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 6f 66 e argument.** of
1d540 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 20 sqlite3_open()
1d550 61 6e 64 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e and sqlite3_open
1d560 5f 76 32 28 29 20 6d 75 73 74 20 62 65 20 55 54 _v2() must be UT
1d570 46 2d 38 2c 20 6e 6f 74 20 77 68 61 74 65 76 65 F-8, not whateve
1d580 72 0a 2a 2a 20 63 6f 64 65 70 61 67 65 20 69 73 r.** codepage is
1d590 20 63 75 72 72 65 6e 74 6c 79 20 64 65 66 69 6e currently defin
1d5a0 65 64 2e 20 20 46 69 6c 65 6e 61 6d 65 73 20 63 ed. Filenames c
1d5b0 6f 6e 74 61 69 6e 69 6e 67 20 69 6e 74 65 72 6e ontaining intern
1d5c0 61 74 69 6f 6e 61 6c 0a 2a 2a 20 63 68 61 72 61 ational.** chara
1d5d0 63 74 65 72 73 20 6d 75 73 74 20 62 65 20 63 6f cters must be co
1d5e0 6e 76 65 72 74 65 64 20 74 6f 20 55 54 46 2d 38 nverted to UTF-8
1d5f0 20 70 72 69 6f 72 20 74 6f 20 70 61 73 73 69 6e prior to passin
1d600 67 20 74 68 65 6d 20 69 6e 74 6f 0a 2a 2a 20 73 g them into.** s
1d610 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 20 6f 72 qlite3_open() or
1d620 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 sqlite3_open_v2
1d630 28 29 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 ()..**.** Requir
1d640 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 37 ements:.** [H127
1d650 30 31 5d 20 5b 48 31 32 37 30 32 5d 20 5b 48 31 01] [H12702] [H1
1d660 32 37 30 33 5d 20 5b 48 31 32 37 30 34 5d 20 5b 2703] [H12704] [
1d670 48 31 32 37 30 36 5d 20 5b 48 31 32 37 30 37 5d H12706] [H12707]
1d680 20 5b 48 31 32 37 30 39 5d 20 5b 48 31 32 37 31 [H12709] [H1271
1d690 31 5d 0a 2a 2a 20 5b 48 31 32 37 31 32 5d 20 5b 1].** [H12712] [
1d6a0 48 31 32 37 31 33 5d 20 5b 48 31 32 37 31 34 5d H12713] [H12714]
1d6b0 20 5b 48 31 32 37 31 37 5d 20 5b 48 31 32 37 31 [H12717] [H1271
1d6c0 39 5d 20 5b 48 31 32 37 32 31 5d 20 5b 48 31 32 9] [H12721] [H12
1d6d0 37 32 33 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 723].*/.SQLITE_A
1d6e0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f PI int sqlite3_o
1d6f0 70 65 6e 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 pen(. const cha
1d700 72 20 2a 66 69 6c 65 6e 61 6d 65 2c 20 20 20 2f r *filename, /
1d710 2a 20 44 61 74 61 62 61 73 65 20 66 69 6c 65 6e * Database filen
1d720 61 6d 65 20 28 55 54 46 2d 38 29 20 2a 2f 0a 20 ame (UTF-8) */.
1d730 20 73 71 6c 69 74 65 33 20 2a 2a 70 70 44 62 20 sqlite3 **ppDb
1d740 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a /* OUT:
1d750 20 53 51 4c 69 74 65 20 64 62 20 68 61 6e 64 6c SQLite db handl
1d760 65 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 e */.);.SQLITE_A
1d770 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f PI int sqlite3_o
1d780 70 65 6e 31 36 28 0a 20 20 63 6f 6e 73 74 20 76 pen16(. const v
1d790 6f 69 64 20 2a 66 69 6c 65 6e 61 6d 65 2c 20 20 oid *filename,
1d7a0 20 2f 2a 20 44 61 74 61 62 61 73 65 20 66 69 6c /* Database fil
1d7b0 65 6e 61 6d 65 20 28 55 54 46 2d 31 36 29 20 2a ename (UTF-16) *
1d7c0 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 2a 70 70 /. sqlite3 **pp
1d7d0 44 62 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f Db /* O
1d7e0 55 54 3a 20 53 51 4c 69 74 65 20 64 62 20 68 61 UT: SQLite db ha
1d7f0 6e 64 6c 65 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54 ndle */.);.SQLIT
1d800 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
1d810 33 5f 6f 70 65 6e 5f 76 32 28 0a 20 20 63 6f 6e 3_open_v2(. con
1d820 73 74 20 63 68 61 72 20 2a 66 69 6c 65 6e 61 6d st char *filenam
1d830 65 2c 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 e, /* Database
1d840 20 66 69 6c 65 6e 61 6d 65 20 28 55 54 46 2d 38 filename (UTF-8
1d850 29 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a ) */. sqlite3 *
1d860 2a 70 70 44 62 2c 20 20 20 20 20 20 20 20 20 2f *ppDb, /
1d870 2a 20 4f 55 54 3a 20 53 51 4c 69 74 65 20 64 62 * OUT: SQLite db
1d880 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 69 6e 74 handle */. int
1d890 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 flags,
1d8a0 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 2a 2f /* Flags */
1d8b0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
1d8c0 56 66 73 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 Vfs /* Na
1d8d0 6d 65 20 6f 66 20 56 46 53 20 6d 6f 64 75 6c 65 me of VFS module
1d8e0 20 74 6f 20 75 73 65 20 2a 2f 0a 29 3b 0a 0a 2f to use */.);../
1d8f0 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 45 *.** CAPI3REF: E
1d900 72 72 6f 72 20 43 6f 64 65 73 20 41 6e 64 20 4d rror Codes And M
1d910 65 73 73 61 67 65 73 20 7b 48 31 32 38 30 30 7d essages {H12800}
1d920 20 3c 53 36 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 <S60200>.**.**
1d930 54 68 65 20 73 71 6c 69 74 65 33 5f 65 72 72 63 The sqlite3_errc
1d940 6f 64 65 28 29 20 69 6e 74 65 72 66 61 63 65 20 ode() interface
1d950 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 65 returns the nume
1d960 72 69 63 20 5b 72 65 73 75 6c 74 20 63 6f 64 65 ric [result code
1d970 5d 20 6f 72 0a 2a 2a 20 5b 65 78 74 65 6e 64 65 ] or.** [extende
1d980 64 20 72 65 73 75 6c 74 20 63 6f 64 65 5d 20 66 d result code] f
1d990 6f 72 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 or the most rece
1d9a0 6e 74 20 66 61 69 6c 65 64 20 73 71 6c 69 74 65 nt failed sqlite
1d9b0 33 5f 2a 20 41 50 49 20 63 61 6c 6c 0a 2a 2a 20 3_* API call.**
1d9c0 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
1d9d0 61 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e a [database conn
1d9e0 65 63 74 69 6f 6e 5d 2e 20 49 66 20 61 20 70 72 ection]. If a pr
1d9f0 69 6f 72 20 41 50 49 20 63 61 6c 6c 20 66 61 69 ior API call fai
1da00 6c 65 64 0a 2a 2a 20 62 75 74 20 74 68 65 20 6d led.** but the m
1da10 6f 73 74 20 72 65 63 65 6e 74 20 41 50 49 20 63 ost recent API c
1da20 61 6c 6c 20 73 75 63 63 65 65 64 65 64 2c 20 74 all succeeded, t
1da30 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 he return value
1da40 66 72 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65 33 5f from.** sqlite3_
1da50 65 72 72 63 6f 64 65 28 29 20 69 73 20 75 6e 64 errcode() is und
1da60 65 66 69 6e 65 64 2e 20 20 54 68 65 20 73 71 6c efined. The sql
1da70 69 74 65 33 5f 65 78 74 65 6e 64 65 64 5f 65 72 ite3_extended_er
1da80 72 63 6f 64 65 28 29 0a 2a 2a 20 69 6e 74 65 72 rcode().** inter
1da90 66 61 63 65 20 69 73 20 74 68 65 20 73 61 6d 65 face is the same
1daa0 20 65 78 63 65 70 74 20 74 68 61 74 20 69 74 20 except that it
1dab0 61 6c 77 61 79 73 20 72 65 74 75 72 6e 73 20 74 always returns t
1dac0 68 65 20 0a 2a 2a 20 5b 65 78 74 65 6e 64 65 64 he .** [extended
1dad0 20 72 65 73 75 6c 74 20 63 6f 64 65 5d 20 65 76 result code] ev
1dae0 65 6e 20 77 68 65 6e 20 65 78 74 65 6e 64 65 64 en when extended
1daf0 20 72 65 73 75 6c 74 20 63 6f 64 65 73 20 61 72 result codes ar
1db00 65 0a 2a 2a 20 64 69 73 61 62 6c 65 64 2e 0a 2a e.** disabled..*
1db10 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
1db20 5f 65 72 72 6d 73 67 28 29 20 61 6e 64 20 73 71 _errmsg() and sq
1db30 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36 28 29 lite3_errmsg16()
1db40 20 72 65 74 75 72 6e 20 45 6e 67 6c 69 73 68 2d return English-
1db50 6c 61 6e 67 75 61 67 65 0a 2a 2a 20 74 65 78 74 language.** text
1db60 20 74 68 61 74 20 64 65 73 63 72 69 62 65 73 20 that describes
1db70 74 68 65 20 65 72 72 6f 72 2c 20 61 73 20 65 69 the error, as ei
1db80 74 68 65 72 20 55 54 46 2d 38 20 6f 72 20 55 54 ther UTF-8 or UT
1db90 46 2d 31 36 20 72 65 73 70 65 63 74 69 76 65 6c F-16 respectivel
1dba0 79 2e 0a 2a 2a 20 4d 65 6d 6f 72 79 20 74 6f 20 y..** Memory to
1dbb0 68 6f 6c 64 20 74 68 65 20 65 72 72 6f 72 20 6d hold the error m
1dbc0 65 73 73 61 67 65 20 73 74 72 69 6e 67 20 69 73 essage string is
1dbd0 20 6d 61 6e 61 67 65 64 20 69 6e 74 65 72 6e 61 managed interna
1dbe0 6c 6c 79 2e 0a 2a 2a 20 54 68 65 20 61 70 70 6c lly..** The appl
1dbf0 69 63 61 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 ication does not
1dc00 20 6e 65 65 64 20 74 6f 20 77 6f 72 72 79 20 61 need to worry a
1dc10 62 6f 75 74 20 66 72 65 65 69 6e 67 20 74 68 65 bout freeing the
1dc20 20 72 65 73 75 6c 74 2e 0a 2a 2a 20 48 6f 77 65 result..** Howe
1dc30 76 65 72 2c 20 74 68 65 20 65 72 72 6f 72 20 73 ver, the error s
1dc40 74 72 69 6e 67 20 6d 69 67 68 74 20 62 65 20 6f tring might be o
1dc50 76 65 72 77 72 69 74 74 65 6e 20 6f 72 20 64 65 verwritten or de
1dc60 61 6c 6c 6f 63 61 74 65 64 20 62 79 0a 2a 2a 20 allocated by.**
1dc70 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 subsequent calls
1dc80 20 74 6f 20 6f 74 68 65 72 20 53 51 4c 69 74 65 to other SQLite
1dc90 20 69 6e 74 65 72 66 61 63 65 20 66 75 6e 63 74 interface funct
1dca0 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e ions..**.** When
1dcb0 20 74 68 65 20 73 65 72 69 61 6c 69 7a 65 64 20 the serialized
1dcc0 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 65 5d [threading mode]
1dcd0 20 69 73 20 69 6e 20 75 73 65 2c 20 69 74 20 6d is in use, it m
1dce0 69 67 68 74 20 62 65 20 74 68 65 0a 2a 2a 20 63 ight be the.** c
1dcf0 61 73 65 20 74 68 61 74 20 61 20 73 65 63 6f 6e ase that a secon
1dd00 64 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 6f d error occurs o
1dd10 6e 20 61 20 73 65 70 61 72 61 74 65 20 74 68 72 n a separate thr
1dd20 65 61 64 20 69 6e 20 62 65 74 77 65 65 6e 0a 2a ead in between.*
1dd30 2a 20 74 68 65 20 74 69 6d 65 20 6f 66 20 74 68 * the time of th
1dd40 65 20 66 69 72 73 74 20 65 72 72 6f 72 20 61 6e e first error an
1dd50 64 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 74 68 d the call to th
1dd60 65 73 65 20 69 6e 74 65 72 66 61 63 65 73 2e 0a ese interfaces..
1dd70 2a 2a 20 57 68 65 6e 20 74 68 61 74 20 68 61 70 ** When that hap
1dd80 70 65 6e 73 2c 20 74 68 65 20 73 65 63 6f 6e 64 pens, the second
1dd90 20 65 72 72 6f 72 20 77 69 6c 6c 20 62 65 20 72 error will be r
1dda0 65 70 6f 72 74 65 64 20 73 69 6e 63 65 20 74 68 eported since th
1ddb0 65 73 65 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 ese.** interface
1ddc0 73 20 61 6c 77 61 79 73 20 72 65 70 6f 72 74 20 s always report
1ddd0 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 the most recent
1dde0 72 65 73 75 6c 74 2e 20 20 54 6f 20 61 76 6f 69 result. To avoi
1ddf0 64 0a 2a 2a 20 74 68 69 73 2c 20 65 61 63 68 20 d.** this, each
1de00 74 68 72 65 61 64 20 63 61 6e 20 6f 62 74 61 69 thread can obtai
1de10 6e 20 65 78 63 6c 75 73 69 76 65 20 75 73 65 20 n exclusive use
1de20 6f 66 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 of the [database
1de30 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44 0a 2a connection] D.*
1de40 2a 20 62 79 20 69 6e 76 6f 6b 69 6e 67 20 5b 73 * by invoking [s
1de50 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 qlite3_mutex_ent
1de60 65 72 5d 28 5b 73 71 6c 69 74 65 33 5f 64 62 5f er]([sqlite3_db_
1de70 6d 75 74 65 78 5d 28 44 29 29 20 62 65 66 6f 72 mutex](D)) befor
1de80 65 20 62 65 67 69 6e 6e 69 6e 67 0a 2a 2a 20 74 e beginning.** t
1de90 6f 20 75 73 65 20 44 20 61 6e 64 20 69 6e 76 6f o use D and invo
1dea0 6b 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 6d 75 king [sqlite3_mu
1deb0 74 65 78 5f 6c 65 61 76 65 5d 28 5b 73 71 6c 69 tex_leave]([sqli
1dec0 74 65 33 5f 64 62 5f 6d 75 74 65 78 5d 28 44 29 te3_db_mutex](D)
1ded0 29 20 61 66 74 65 72 0a 2a 2a 20 61 6c 6c 20 63 ) after.** all c
1dee0 61 6c 6c 73 20 74 6f 20 74 68 65 20 69 6e 74 65 alls to the inte
1def0 72 66 61 63 65 73 20 6c 69 73 74 65 64 20 68 65 rfaces listed he
1df00 72 65 20 61 72 65 20 63 6f 6d 70 6c 65 74 65 64 re are completed
1df10 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 69 6e ..**.** If an in
1df20 74 65 72 66 61 63 65 20 66 61 69 6c 73 20 77 69 terface fails wi
1df30 74 68 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 th SQLITE_MISUSE
1df40 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 65 , that means the
1df50 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20 77 61 interface.** wa
1df60 73 20 69 6e 76 6f 6b 65 64 20 69 6e 63 6f 72 72 s invoked incorr
1df70 65 63 74 6c 79 20 62 79 20 74 68 65 20 61 70 70 ectly by the app
1df80 6c 69 63 61 74 69 6f 6e 2e 20 20 49 6e 20 74 68 lication. In th
1df90 61 74 20 63 61 73 65 2c 20 74 68 65 0a 2a 2a 20 at case, the.**
1dfa0 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 20 6d error code and m
1dfb0 65 73 73 61 67 65 20 6d 61 79 20 6f 72 20 6d 61 essage may or ma
1dfc0 79 20 6e 6f 74 20 62 65 20 73 65 74 2e 0a 2a 2a y not be set..**
1dfd0 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 .** Requirements
1dfe0 3a 0a 2a 2a 20 5b 48 31 32 38 30 31 5d 20 5b 48 :.** [H12801] [H
1dff0 31 32 38 30 32 5d 20 5b 48 31 32 38 30 33 5d 20 12802] [H12803]
1e000 5b 48 31 32 38 30 37 5d 20 5b 48 31 32 38 30 38 [H12807] [H12808
1e010 5d 20 5b 48 31 32 38 30 39 5d 0a 2a 2f 0a 53 51 ] [H12809].*/.SQ
1e020 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
1e030 69 74 65 33 5f 65 72 72 63 6f 64 65 28 73 71 6c ite3_errcode(sql
1e040 69 74 65 33 20 2a 64 62 29 3b 0a 53 51 4c 49 54 ite3 *db);.SQLIT
1e050 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
1e060 33 5f 65 78 74 65 6e 64 65 64 5f 65 72 72 63 6f 3_extended_errco
1e070 64 65 28 73 71 6c 69 74 65 33 20 2a 64 62 29 3b de(sqlite3 *db);
1e080 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 .SQLITE_API cons
1e090 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f t char *sqlite3_
1e0a0 65 72 72 6d 73 67 28 73 71 6c 69 74 65 33 2a 29 errmsg(sqlite3*)
1e0b0 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e ;.SQLITE_API con
1e0c0 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 st void *sqlite3
1e0d0 5f 65 72 72 6d 73 67 31 36 28 73 71 6c 69 74 65 _errmsg16(sqlite
1e0e0 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 3*);../*.** CAPI
1e0f0 33 52 45 46 3a 20 53 51 4c 20 53 74 61 74 65 6d 3REF: SQL Statem
1e100 65 6e 74 20 4f 62 6a 65 63 74 20 7b 48 31 33 30 ent Object {H130
1e110 30 30 7d 20 3c 48 31 33 30 31 30 3e 0a 2a 2a 20 00} <H13010>.**
1e120 4b 45 59 57 4f 52 44 53 3a 20 7b 70 72 65 70 61 KEYWORDS: {prepa
1e130 72 65 64 20 73 74 61 74 65 6d 65 6e 74 7d 20 7b red statement} {
1e140 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
1e150 6e 74 73 7d 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 6e nts}.**.** An in
1e160 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f stance of this o
1e170 62 6a 65 63 74 20 72 65 70 72 65 73 65 6e 74 73 bject represents
1e180 20 61 20 73 69 6e 67 6c 65 20 53 51 4c 20 73 74 a single SQL st
1e190 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 54 68 69 73 atement..** This
1e1a0 20 6f 62 6a 65 63 74 20 69 73 20 76 61 72 69 6f object is vario
1e1b0 75 73 6c 79 20 6b 6e 6f 77 6e 20 61 73 20 61 20 usly known as a
1e1c0 22 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d "prepared statem
1e1d0 65 6e 74 22 20 6f 72 20 61 0a 2a 2a 20 22 63 6f ent" or a.** "co
1e1e0 6d 70 69 6c 65 64 20 53 51 4c 20 73 74 61 74 65 mpiled SQL state
1e1f0 6d 65 6e 74 22 20 6f 72 20 73 69 6d 70 6c 79 20 ment" or simply
1e200 61 73 20 61 20 22 73 74 61 74 65 6d 65 6e 74 22 as a "statement"
1e210 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6c 69 66 65 ..**.** The life
1e220 20 6f 66 20 61 20 73 74 61 74 65 6d 65 6e 74 20 of a statement
1e230 6f 62 6a 65 63 74 20 67 6f 65 73 20 73 6f 6d 65 object goes some
1e240 74 68 69 6e 67 20 6c 69 6b 65 20 74 68 69 73 3a thing like this:
1e250 0a 2a 2a 0a 2a 2a 20 3c 6f 6c 3e 0a 2a 2a 20 3c .**.** <ol>.** <
1e260 6c 69 3e 20 43 72 65 61 74 65 20 74 68 65 20 6f li> Create the o
1e270 62 6a 65 63 74 20 75 73 69 6e 67 20 5b 73 71 6c bject using [sql
1e280 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 ite3_prepare_v2(
1e290 29 5d 20 6f 72 20 61 20 72 65 6c 61 74 65 64 0a )] or a related.
1e2a0 2a 2a 20 20 20 20 20 20 66 75 6e 63 74 69 6f 6e ** function
1e2b0 2e 0a 2a 2a 20 3c 6c 69 3e 20 42 69 6e 64 20 76 ..** <li> Bind v
1e2c0 61 6c 75 65 73 20 74 6f 20 5b 68 6f 73 74 20 70 alues to [host p
1e2d0 61 72 61 6d 65 74 65 72 73 5d 20 75 73 69 6e 67 arameters] using
1e2e0 20 74 68 65 20 73 71 6c 69 74 65 33 5f 62 69 6e the sqlite3_bin
1e2f0 64 5f 2a 28 29 0a 2a 2a 20 20 20 20 20 20 69 6e d_*().** in
1e300 74 65 72 66 61 63 65 73 2e 0a 2a 2a 20 3c 6c 69 terfaces..** <li
1e310 3e 20 52 75 6e 20 74 68 65 20 53 51 4c 20 62 79 > Run the SQL by
1e320 20 63 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 65 calling [sqlite
1e330 33 5f 73 74 65 70 28 29 5d 20 6f 6e 65 20 6f 72 3_step()] one or
1e340 20 6d 6f 72 65 20 74 69 6d 65 73 2e 0a 2a 2a 20 more times..**
1e350 3c 6c 69 3e 20 52 65 73 65 74 20 74 68 65 20 73 <li> Reset the s
1e360 74 61 74 65 6d 65 6e 74 20 75 73 69 6e 67 20 5b tatement using [
1e370 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d sqlite3_reset()]
1e380 20 74 68 65 6e 20 67 6f 20 62 61 63 6b 0a 2a 2a then go back.**
1e390 20 20 20 20 20 20 74 6f 20 73 74 65 70 20 32 2e to step 2.
1e3a0 20 20 44 6f 20 74 68 69 73 20 7a 65 72 6f 20 6f Do this zero o
1e3b0 72 20 6d 6f 72 65 20 74 69 6d 65 73 2e 0a 2a 2a r more times..**
1e3c0 20 3c 6c 69 3e 20 44 65 73 74 72 6f 79 20 74 68 <li> Destroy th
1e3d0 65 20 6f 62 6a 65 63 74 20 75 73 69 6e 67 20 5b e object using [
1e3e0 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 sqlite3_finalize
1e3f0 28 29 5d 2e 0a 2a 2a 20 3c 2f 6f 6c 3e 0a 2a 2a ()]..** </ol>.**
1e400 0a 2a 2a 20 52 65 66 65 72 20 74 6f 20 64 6f 63 .** Refer to doc
1e410 75 6d 65 6e 74 61 74 69 6f 6e 20 6f 6e 20 69 6e umentation on in
1e420 64 69 76 69 64 75 61 6c 20 6d 65 74 68 6f 64 73 dividual methods
1e430 20 61 62 6f 76 65 20 66 6f 72 20 61 64 64 69 74 above for addit
1e440 69 6f 6e 61 6c 0a 2a 2a 20 69 6e 66 6f 72 6d 61 ional.** informa
1e450 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 tion..*/.typedef
1e460 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f struct sqlite3_
1e470 73 74 6d 74 20 73 71 6c 69 74 65 33 5f 73 74 6d stmt sqlite3_stm
1e480 74 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 t;../*.** CAPI3R
1e490 45 46 3a 20 52 75 6e 2d 74 69 6d 65 20 4c 69 6d EF: Run-time Lim
1e4a0 69 74 73 20 7b 48 31 32 37 36 30 7d 20 3c 53 32 its {H12760} <S2
1e4b0 30 36 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 0600>.**.** This
1e4c0 20 69 6e 74 65 72 66 61 63 65 20 61 6c 6c 6f 77 interface allow
1e4d0 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20 76 61 s the size of va
1e4e0 72 69 6f 75 73 20 63 6f 6e 73 74 72 75 63 74 73 rious constructs
1e4f0 20 74 6f 20 62 65 20 6c 69 6d 69 74 65 64 0a 2a to be limited.*
1e500 2a 20 6f 6e 20 61 20 63 6f 6e 6e 65 63 74 69 6f * on a connectio
1e510 6e 20 62 79 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 n by connection
1e520 62 61 73 69 73 2e 20 20 54 68 65 20 66 69 72 73 basis. The firs
1e530 74 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 74 t parameter is t
1e540 68 65 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 he.** [database
1e550 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 77 68 6f 73 connection] whos
1e560 65 20 6c 69 6d 69 74 20 69 73 20 74 6f 20 62 65 e limit is to be
1e570 20 73 65 74 20 6f 72 20 71 75 65 72 69 65 64 2e set or queried.
1e580 20 20 54 68 65 0a 2a 2a 20 73 65 63 6f 6e 64 20 The.** second
1e590 70 61 72 61 6d 65 74 65 72 20 69 73 20 6f 6e 65 parameter is one
1e5a0 20 6f 66 20 74 68 65 20 5b 6c 69 6d 69 74 20 63 of the [limit c
1e5b0 61 74 65 67 6f 72 69 65 73 5d 20 74 68 61 74 20 ategories] that
1e5c0 64 65 66 69 6e 65 20 61 0a 2a 2a 20 63 6c 61 73 define a.** clas
1e5d0 73 20 6f 66 20 63 6f 6e 73 74 72 75 63 74 73 20 s of constructs
1e5e0 74 6f 20 62 65 20 73 69 7a 65 20 6c 69 6d 69 74 to be size limit
1e5f0 65 64 2e 20 20 54 68 65 20 74 68 69 72 64 20 70 ed. The third p
1e600 61 72 61 6d 65 74 65 72 20 69 73 20 74 68 65 0a arameter is the.
1e610 2a 2a 20 6e 65 77 20 6c 69 6d 69 74 20 66 6f 72 ** new limit for
1e620 20 74 68 61 74 20 63 6f 6e 73 74 72 75 63 74 2e that construct.
1e630 20 20 54 68 65 20 66 75 6e 63 74 69 6f 6e 20 72 The function r
1e640 65 74 75 72 6e 73 20 74 68 65 20 6f 6c 64 20 6c eturns the old l
1e650 69 6d 69 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 imit..**.** If t
1e660 68 65 20 6e 65 77 20 6c 69 6d 69 74 20 69 73 20 he new limit is
1e670 61 20 6e 65 67 61 74 69 76 65 20 6e 75 6d 62 65 a negative numbe
1e680 72 2c 20 74 68 65 20 6c 69 6d 69 74 20 69 73 20 r, the limit is
1e690 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a 20 46 6f unchanged..** Fo
1e6a0 72 20 74 68 65 20 6c 69 6d 69 74 20 63 61 74 65 r the limit cate
1e6b0 67 6f 72 79 20 6f 66 20 53 51 4c 49 54 45 5f 4c gory of SQLITE_L
1e6c0 49 4d 49 54 5f 58 59 5a 20 74 68 65 72 65 20 69 IMIT_XYZ there i
1e6d0 73 20 61 20 0a 2a 2a 20 5b 6c 69 6d 69 74 73 20 s a .** [limits
1e6e0 7c 20 68 61 72 64 20 75 70 70 65 72 20 62 6f 75 | hard upper bou
1e6f0 6e 64 5d 0a 2a 2a 20 73 65 74 20 62 79 20 61 20 nd].** set by a
1e700 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 43 20 70 compile-time C p
1e710 72 65 70 72 6f 63 65 73 73 6f 72 20 6d 61 63 72 reprocessor macr
1e720 6f 20 6e 61 6d 65 64 20 0a 2a 2a 20 5b 6c 69 6d o named .** [lim
1e730 69 74 73 20 7c 20 53 51 4c 49 54 45 5f 4d 41 58 its | SQLITE_MAX
1e740 5f 58 59 5a 5d 2e 0a 2a 2a 20 28 54 68 65 20 22 _XYZ]..** (The "
1e750 5f 4c 49 4d 49 54 5f 22 20 69 6e 20 74 68 65 20 _LIMIT_" in the
1e760 6e 61 6d 65 20 69 73 20 63 68 61 6e 67 65 64 20 name is changed
1e770 74 6f 20 22 5f 4d 41 58 5f 22 2e 29 0a 2a 2a 20 to "_MAX_".).**
1e780 41 74 74 65 6d 70 74 73 20 74 6f 20 69 6e 63 72 Attempts to incr
1e790 65 61 73 65 20 61 20 6c 69 6d 69 74 20 61 62 6f ease a limit abo
1e7a0 76 65 20 69 74 73 20 68 61 72 64 20 75 70 70 65 ve its hard uppe
1e7b0 72 20 62 6f 75 6e 64 20 61 72 65 0a 2a 2a 20 73 r bound are.** s
1e7c0 69 6c 65 6e 74 6c 79 20 74 72 75 6e 63 61 74 65 ilently truncate
1e7d0 64 20 74 6f 20 74 68 65 20 68 61 72 64 20 75 70 d to the hard up
1e7e0 70 65 72 20 6c 69 6d 69 74 2e 0a 2a 2a 0a 2a 2a per limit..**.**
1e7f0 20 52 75 6e 20 74 69 6d 65 20 6c 69 6d 69 74 73 Run time limits
1e800 20 61 72 65 20 69 6e 74 65 6e 64 65 64 20 66 6f are intended fo
1e810 72 20 75 73 65 20 69 6e 20 61 70 70 6c 69 63 61 r use in applica
1e820 74 69 6f 6e 73 20 74 68 61 74 20 6d 61 6e 61 67 tions that manag
1e830 65 0a 2a 2a 20 62 6f 74 68 20 74 68 65 69 72 20 e.** both their
1e840 6f 77 6e 20 69 6e 74 65 72 6e 61 6c 20 64 61 74 own internal dat
1e850 61 62 61 73 65 20 61 6e 64 20 61 6c 73 6f 20 64 abase and also d
1e860 61 74 61 62 61 73 65 73 20 74 68 61 74 20 61 72 atabases that ar
1e870 65 20 63 6f 6e 74 72 6f 6c 6c 65 64 0a 2a 2a 20 e controlled.**
1e880 62 79 20 75 6e 74 72 75 73 74 65 64 20 65 78 74 by untrusted ext
1e890 65 72 6e 61 6c 20 73 6f 75 72 63 65 73 2e 20 20 ernal sources.
1e8a0 41 6e 20 65 78 61 6d 70 6c 65 20 61 70 70 6c 69 An example appli
1e8b0 63 61 74 69 6f 6e 20 6d 69 67 68 74 20 62 65 20 cation might be
1e8c0 61 0a 2a 2a 20 77 65 62 20 62 72 6f 77 73 65 72 a.** web browser
1e8d0 20 74 68 61 74 20 68 61 73 20 69 74 73 20 6f 77 that has its ow
1e8e0 6e 20 64 61 74 61 62 61 73 65 73 20 66 6f 72 20 n databases for
1e8f0 73 74 6f 72 69 6e 67 20 68 69 73 74 6f 72 79 20 storing history
1e900 61 6e 64 0a 2a 2a 20 73 65 70 61 72 61 74 65 20 and.** separate
1e910 64 61 74 61 62 61 73 65 73 20 63 6f 6e 74 72 6f databases contro
1e920 6c 6c 65 64 20 62 79 20 4a 61 76 61 53 63 72 69 lled by JavaScri
1e930 70 74 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 pt applications
1e940 64 6f 77 6e 6c 6f 61 64 65 64 0a 2a 2a 20 6f 66 downloaded.** of
1e950 66 20 74 68 65 20 49 6e 74 65 72 6e 65 74 2e 20 f the Internet.
1e960 20 54 68 65 20 69 6e 74 65 72 6e 61 6c 20 64 61 The internal da
1e970 74 61 62 61 73 65 73 20 63 61 6e 20 62 65 20 67 tabases can be g
1e980 69 76 65 6e 20 74 68 65 0a 2a 2a 20 6c 61 72 67 iven the.** larg
1e990 65 2c 20 64 65 66 61 75 6c 74 20 6c 69 6d 69 74 e, default limit
1e9a0 73 2e 20 20 44 61 74 61 62 61 73 65 73 20 6d 61 s. Databases ma
1e9b0 6e 61 67 65 64 20 62 79 20 65 78 74 65 72 6e 61 naged by externa
1e9c0 6c 20 73 6f 75 72 63 65 73 20 63 61 6e 0a 2a 2a l sources can.**
1e9d0 20 62 65 20 67 69 76 65 6e 20 6d 75 63 68 20 73 be given much s
1e9e0 6d 61 6c 6c 65 72 20 6c 69 6d 69 74 73 20 64 65 maller limits de
1e9f0 73 69 67 6e 65 64 20 74 6f 20 70 72 65 76 65 6e signed to preven
1ea00 74 20 61 20 64 65 6e 69 61 6c 20 6f 66 20 73 65 t a denial of se
1ea10 72 76 69 63 65 0a 2a 2a 20 61 74 74 61 63 6b 2e rvice.** attack.
1ea20 20 20 44 65 76 65 6c 6f 70 65 72 73 20 6d 69 67 Developers mig
1ea30 68 74 20 61 6c 73 6f 20 77 61 6e 74 20 74 6f 20 ht also want to
1ea40 75 73 65 20 74 68 65 20 5b 73 71 6c 69 74 65 33 use the [sqlite3
1ea50 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 28 _set_authorizer(
1ea60 29 5d 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 )].** interface
1ea70 74 6f 20 66 75 72 74 68 65 72 20 63 6f 6e 74 72 to further contr
1ea80 6f 6c 20 75 6e 74 72 75 73 74 65 64 20 53 51 4c ol untrusted SQL
1ea90 2e 20 20 54 68 65 20 73 69 7a 65 20 6f 66 20 74 . The size of t
1eaa0 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 he database.** c
1eab0 72 65 61 74 65 64 20 62 79 20 61 6e 20 75 6e 74 reated by an unt
1eac0 72 75 73 74 65 64 20 73 63 72 69 70 74 20 63 61 rusted script ca
1ead0 6e 20 62 65 20 63 6f 6e 74 61 69 6e 65 64 20 75 n be contained u
1eae0 73 69 6e 67 20 74 68 65 0a 2a 2a 20 5b 6d 61 78 sing the.** [max
1eaf0 5f 70 61 67 65 5f 63 6f 75 6e 74 5d 20 5b 50 52 _page_count] [PR
1eb00 41 47 4d 41 5d 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 AGMA]..**.** New
1eb10 20 72 75 6e 2d 74 69 6d 65 20 6c 69 6d 69 74 20 run-time limit
1eb20 63 61 74 65 67 6f 72 69 65 73 20 6d 61 79 20 62 categories may b
1eb30 65 20 61 64 64 65 64 20 69 6e 20 66 75 74 75 72 e added in futur
1eb40 65 20 72 65 6c 65 61 73 65 73 2e 0a 2a 2a 0a 2a e releases..**.*
1eb50 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a * Requirements:.
1eb60 2a 2a 20 5b 48 31 32 37 36 32 5d 20 5b 48 31 32 ** [H12762] [H12
1eb70 37 36 36 5d 20 5b 48 31 32 37 36 39 5d 0a 2a 2f 766] [H12769].*/
1eb80 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
1eb90 73 71 6c 69 74 65 33 5f 6c 69 6d 69 74 28 73 71 sqlite3_limit(sq
1eba0 6c 69 74 65 33 2a 2c 20 69 6e 74 20 69 64 2c 20 lite3*, int id,
1ebb0 69 6e 74 20 6e 65 77 56 61 6c 29 3b 0a 0a 2f 2a int newVal);../*
1ebc0 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 75 .** CAPI3REF: Ru
1ebd0 6e 2d 54 69 6d 65 20 4c 69 6d 69 74 20 43 61 74 n-Time Limit Cat
1ebe0 65 67 6f 72 69 65 73 20 7b 48 31 32 37 39 30 7d egories {H12790}
1ebf0 20 3c 48 31 32 37 36 30 3e 0a 2a 2a 20 4b 45 59 <H12760>.** KEY
1ec00 57 4f 52 44 53 3a 20 7b 6c 69 6d 69 74 20 63 61 WORDS: {limit ca
1ec10 74 65 67 6f 72 79 7d 20 7b 6c 69 6d 69 74 20 63 tegory} {limit c
1ec20 61 74 65 67 6f 72 69 65 73 7d 0a 2a 2a 0a 2a 2a ategories}.**.**
1ec30 20 54 68 65 73 65 20 63 6f 6e 73 74 61 6e 74 73 These constants
1ec40 20 64 65 66 69 6e 65 20 76 61 72 69 6f 75 73 20 define various
1ec50 70 65 72 66 6f 72 6d 61 6e 63 65 20 6c 69 6d 69 performance limi
1ec60 74 73 0a 2a 2a 20 74 68 61 74 20 63 61 6e 20 62 ts.** that can b
1ec70 65 20 6c 6f 77 65 72 65 64 20 61 74 20 72 75 6e e lowered at run
1ec80 2d 74 69 6d 65 20 75 73 69 6e 67 20 5b 73 71 6c -time using [sql
1ec90 69 74 65 33 5f 6c 69 6d 69 74 28 29 5d 2e 0a 2a ite3_limit()]..*
1eca0 2a 20 54 68 65 20 73 79 6e 6f 70 73 69 73 20 6f * The synopsis o
1ecb0 66 20 74 68 65 20 6d 65 61 6e 69 6e 67 73 20 6f f the meanings o
1ecc0 66 20 74 68 65 20 76 61 72 69 6f 75 73 20 6c 69 f the various li
1ecd0 6d 69 74 73 20 69 73 20 73 68 6f 77 6e 20 62 65 mits is shown be
1ece0 6c 6f 77 2e 0a 2a 2a 20 41 64 64 69 74 69 6f 6e low..** Addition
1ecf0 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 al information i
1ed00 73 20 61 76 61 69 6c 61 62 6c 65 20 61 74 20 5b s available at [
1ed10 6c 69 6d 69 74 73 20 7c 20 4c 69 6d 69 74 73 20 limits | Limits
1ed20 69 6e 20 53 51 4c 69 74 65 5d 2e 0a 2a 2a 0a 2a in SQLite]..**.*
1ed30 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 3e 53 51 * <dl>.** <dt>SQ
1ed40 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 LITE_LIMIT_LENGT
1ed50 48 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 H</dt>.** <dd>Th
1ed60 65 20 6d 61 78 69 6d 75 6d 20 73 69 7a 65 20 6f e maximum size o
1ed70 66 20 61 6e 79 20 73 74 72 69 6e 67 20 6f 72 20 f any string or
1ed80 42 4c 4f 42 20 6f 72 20 74 61 62 6c 65 20 72 6f BLOB or table ro
1ed90 77 2e 3c 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 w.<dd>.**.** <dt
1eda0 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 53 51 >SQLITE_LIMIT_SQ
1edb0 4c 5f 4c 45 4e 47 54 48 3c 2f 64 74 3e 0a 2a 2a L_LENGTH</dt>.**
1edc0 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d <dd>The maximum
1edd0 20 6c 65 6e 67 74 68 20 6f 66 20 61 6e 20 53 51 length of an SQ
1ede0 4c 20 73 74 61 74 65 6d 65 6e 74 2e 3c 2f 64 64 L statement.</dd
1edf0 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 >.**.** <dt>SQLI
1ee00 54 45 5f 4c 49 4d 49 54 5f 43 4f 4c 55 4d 4e 3c TE_LIMIT_COLUMN<
1ee10 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 /dt>.** <dd>The
1ee20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f maximum number o
1ee30 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 61 20 74 f columns in a t
1ee40 61 62 6c 65 20 64 65 66 69 6e 69 74 69 6f 6e 20 able definition
1ee50 6f 72 20 69 6e 20 74 68 65 0a 2a 2a 20 72 65 73 or in the.** res
1ee60 75 6c 74 20 73 65 74 20 6f 66 20 61 20 5b 53 45 ult set of a [SE
1ee70 4c 45 43 54 5d 20 6f 72 20 74 68 65 20 6d 61 78 LECT] or the max
1ee80 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 63 imum number of c
1ee90 6f 6c 75 6d 6e 73 20 69 6e 20 61 6e 20 69 6e 64 olumns in an ind
1eea0 65 78 0a 2a 2a 20 6f 72 20 69 6e 20 61 6e 20 4f ex.** or in an O
1eeb0 52 44 45 52 20 42 59 20 6f 72 20 47 52 4f 55 50 RDER BY or GROUP
1eec0 20 42 59 20 63 6c 61 75 73 65 2e 3c 2f 64 64 3e BY clause.</dd>
1eed0 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 .**.** <dt>SQLIT
1eee0 45 5f 4c 49 4d 49 54 5f 45 58 50 52 5f 44 45 50 E_LIMIT_EXPR_DEP
1eef0 54 48 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 TH</dt>.** <dd>T
1ef00 68 65 20 6d 61 78 69 6d 75 6d 20 64 65 70 74 68 he maximum depth
1ef10 20 6f 66 20 74 68 65 20 70 61 72 73 65 20 74 72 of the parse tr
1ef20 65 65 20 6f 6e 20 61 6e 79 20 65 78 70 72 65 73 ee on any expres
1ef30 73 69 6f 6e 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a sion.</dd>.**.**
1ef40 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 <dt>SQLITE_LIMI
1ef50 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 T_COMPOUND_SELEC
1ef60 54 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 T</dt>.** <dd>Th
1ef70 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 e maximum number
1ef80 20 6f 66 20 74 65 72 6d 73 20 69 6e 20 61 20 63 of terms in a c
1ef90 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 20 73 ompound SELECT s
1efa0 74 61 74 65 6d 65 6e 74 2e 3c 2f 64 64 3e 0a 2a tatement.</dd>.*
1efb0 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f *.** <dt>SQLITE_
1efc0 4c 49 4d 49 54 5f 56 44 42 45 5f 4f 50 3c 2f 64 LIMIT_VDBE_OP</d
1efd0 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 t>.** <dd>The ma
1efe0 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 ximum number of
1eff0 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 69 6e 20 instructions in
1f000 61 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e a virtual machin
1f010 65 20 70 72 6f 67 72 61 6d 0a 2a 2a 20 75 73 65 e program.** use
1f020 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 61 d to implement a
1f030 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e n SQL statement.
1f040 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e </dd>.**.** <dt>
1f050 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 46 55 4e SQLITE_LIMIT_FUN
1f060 43 54 49 4f 4e 5f 41 52 47 3c 2f 64 74 3e 0a 2a CTION_ARG</dt>.*
1f070 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d 75 * <dd>The maximu
1f080 6d 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 m number of argu
1f090 6d 65 6e 74 73 20 6f 6e 20 61 20 66 75 6e 63 74 ments on a funct
1f0a0 69 6f 6e 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 ion.</dd>.**.**
1f0b0 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 <dt>SQLITE_LIMIT
1f0c0 5f 41 54 54 41 43 48 45 44 3c 2f 64 74 3e 0a 2a _ATTACHED</dt>.*
1f0d0 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d 75 * <dd>The maximu
1f0e0 6d 20 6e 75 6d 62 65 72 20 6f 66 20 5b 41 54 54 m number of [ATT
1f0f0 41 43 48 20 7c 20 61 74 74 61 63 68 65 64 20 64 ACH | attached d
1f100 61 74 61 62 61 73 65 73 5d 2e 3c 2f 64 64 3e 0a atabases].</dd>.
1f110 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 **.** <dt>SQLITE
1f120 5f 4c 49 4d 49 54 5f 4c 49 4b 45 5f 50 41 54 54 _LIMIT_LIKE_PATT
1f130 45 52 4e 5f 4c 45 4e 47 54 48 3c 2f 64 74 3e 0a ERN_LENGTH</dt>.
1f140 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d ** <dd>The maxim
1f150 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 um length of the
1f160 20 70 61 74 74 65 72 6e 20 61 72 67 75 6d 65 6e pattern argumen
1f170 74 20 74 6f 20 74 68 65 20 5b 4c 49 4b 45 5d 20 t to the [LIKE]
1f180 6f 72 0a 2a 2a 20 5b 47 4c 4f 42 5d 20 6f 70 65 or.** [GLOB] ope
1f190 72 61 74 6f 72 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a rators.</dd>.**.
1f1a0 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49 ** <dt>SQLITE_LI
1f1b0 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d MIT_VARIABLE_NUM
1f1c0 42 45 52 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e BER</dt>.** <dd>
1f1d0 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 The maximum numb
1f1e0 65 72 20 6f 66 20 76 61 72 69 61 62 6c 65 73 20 er of variables
1f1f0 69 6e 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d in an SQL statem
1f200 65 6e 74 20 74 68 61 74 20 63 61 6e 0a 2a 2a 20 ent that can.**
1f210 62 65 20 62 6f 75 6e 64 2e 3c 2f 64 64 3e 0a 2a be bound.</dd>.*
1f220 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f *.** <dt>SQLITE_
1f230 4c 49 4d 49 54 5f 54 52 49 47 47 45 52 5f 44 45 LIMIT_TRIGGER_DE
1f240 50 54 48 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e PTH</dt>.** <dd>
1f250 54 68 65 20 6d 61 78 69 6d 75 6d 20 64 65 70 74 The maximum dept
1f260 68 20 6f 66 20 72 65 63 75 72 73 69 6f 6e 20 66 h of recursion f
1f270 6f 72 20 74 72 69 67 67 65 72 73 2e 3c 2f 64 64 or triggers.</dd
1f280 3e 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f 0a 23 64 >.** </dl>.*/.#d
1f290 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d efine SQLITE_LIM
1f2a0 49 54 5f 4c 45 4e 47 54 48 20 20 20 20 20 20 20 IT_LENGTH
1f2b0 20 20 20 20 20 20 20 20 20 20 20 20 20 30 0a 23 0.#
1f2c0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 define SQLITE_LI
1f2d0 4d 49 54 5f 53 51 4c 5f 4c 45 4e 47 54 48 20 20 MIT_SQL_LENGTH
1f2e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 0a 1.
1f2f0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c #define SQLITE_L
1f300 49 4d 49 54 5f 43 4f 4c 55 4d 4e 20 20 20 20 20 IMIT_COLUMN
1f310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 2
1f320 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
1f330 4c 49 4d 49 54 5f 45 58 50 52 5f 44 45 50 54 48 LIMIT_EXPR_DEPTH
1f340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f350 33 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 3.#define SQLITE
1f360 5f 4c 49 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f _LIMIT_COMPOUND_
1f370 53 45 4c 45 43 54 20 20 20 20 20 20 20 20 20 20 SELECT
1f380 20 34 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 4.#define SQLIT
1f390 45 5f 4c 49 4d 49 54 5f 56 44 42 45 5f 4f 50 20 E_LIMIT_VDBE_OP
1f3a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f3b0 20 20 35 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 5.#define SQLI
1f3c0 54 45 5f 4c 49 4d 49 54 5f 46 55 4e 43 54 49 4f TE_LIMIT_FUNCTIO
1f3d0 4e 5f 41 52 47 20 20 20 20 20 20 20 20 20 20 20 N_ARG
1f3e0 20 20 20 36 0a 23 64 65 66 69 6e 65 20 53 51 4c 6.#define SQL
1f3f0 49 54 45 5f 4c 49 4d 49 54 5f 41 54 54 41 43 48 ITE_LIMIT_ATTACH
1f400 45 44 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ED
1f410 20 20 20 20 37 0a 23 64 65 66 69 6e 65 20 53 51 7.#define SQ
1f420 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 49 4b 45 5f LITE_LIMIT_LIKE_
1f430 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48 20 20 PATTERN_LENGTH
1f440 20 20 20 20 20 38 0a 23 64 65 66 69 6e 65 20 53 8.#define S
1f450 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41 52 49 QLITE_LIMIT_VARI
1f460 41 42 4c 45 5f 4e 55 4d 42 45 52 20 20 20 20 20 ABLE_NUMBER
1f470 20 20 20 20 20 20 39 0a 23 64 65 66 69 6e 65 20 9.#define
1f480 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 54 52 49 SQLITE_LIMIT_TRI
1f490 47 47 45 52 5f 44 45 50 54 48 20 20 20 20 20 20 GGER_DEPTH
1f4a0 20 20 20 20 20 20 31 30 0a 0a 2f 2a 0a 2a 2a 20 10../*.**
1f4b0 43 41 50 49 33 52 45 46 3a 20 43 6f 6d 70 69 6c CAPI3REF: Compil
1f4c0 69 6e 67 20 41 6e 20 53 51 4c 20 53 74 61 74 65 ing An SQL State
1f4d0 6d 65 6e 74 20 7b 48 31 33 30 31 30 7d 20 3c 53 ment {H13010} <S
1f4e0 31 30 30 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 10000>.** KEYWOR
1f4f0 44 53 3a 20 7b 53 51 4c 20 73 74 61 74 65 6d 65 DS: {SQL stateme
1f500 6e 74 20 63 6f 6d 70 69 6c 65 72 7d 0a 2a 2a 0a nt compiler}.**.
1f510 2a 2a 20 54 6f 20 65 78 65 63 75 74 65 20 61 6e ** To execute an
1f520 20 53 51 4c 20 71 75 65 72 79 2c 20 69 74 20 6d SQL query, it m
1f530 75 73 74 20 66 69 72 73 74 20 62 65 20 63 6f 6d ust first be com
1f540 70 69 6c 65 64 20 69 6e 74 6f 20 61 20 62 79 74 piled into a byt
1f550 65 2d 63 6f 64 65 0a 2a 2a 20 70 72 6f 67 72 61 e-code.** progra
1f560 6d 20 75 73 69 6e 67 20 6f 6e 65 20 6f 66 20 74 m using one of t
1f570 68 65 73 65 20 72 6f 75 74 69 6e 65 73 2e 0a 2a hese routines..*
1f580 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61 *.** The first a
1f590 72 67 75 6d 65 6e 74 2c 20 22 64 62 22 2c 20 69 rgument, "db", i
1f5a0 73 20 61 20 5b 64 61 74 61 62 61 73 65 20 63 6f s a [database co
1f5b0 6e 6e 65 63 74 69 6f 6e 5d 20 6f 62 74 61 69 6e nnection] obtain
1f5c0 65 64 20 66 72 6f 6d 20 61 0a 2a 2a 20 70 72 69 ed from a.** pri
1f5d0 6f 72 20 73 75 63 63 65 73 73 66 75 6c 20 63 61 or successful ca
1f5e0 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6f ll to [sqlite3_o
1f5f0 70 65 6e 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 pen()], [sqlite3
1f600 5f 6f 70 65 6e 5f 76 32 28 29 5d 20 6f 72 0a 2a _open_v2()] or.*
1f610 2a 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 * [sqlite3_open1
1f620 36 28 29 5d 2e 20 20 54 68 65 20 64 61 74 61 62 6()]. The datab
1f630 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6d ase connection m
1f640 75 73 74 20 6e 6f 74 20 68 61 76 65 20 62 65 65 ust not have bee
1f650 6e 20 63 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 n closed..**.**
1f660 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d The second argum
1f670 65 6e 74 2c 20 22 7a 53 71 6c 22 2c 20 69 73 20 ent, "zSql", is
1f680 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 74 6f the statement to
1f690 20 62 65 20 63 6f 6d 70 69 6c 65 64 2c 20 65 6e be compiled, en
1f6a0 63 6f 64 65 64 0a 2a 2a 20 61 73 20 65 69 74 68 coded.** as eith
1f6b0 65 72 20 55 54 46 2d 38 20 6f 72 20 55 54 46 2d er UTF-8 or UTF-
1f6c0 31 36 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 16. The sqlite3
1f6d0 5f 70 72 65 70 61 72 65 28 29 20 61 6e 64 20 73 _prepare() and s
1f6e0 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 qlite3_prepare_v
1f6f0 32 28 29 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 2().** interface
1f700 73 20 75 73 65 20 55 54 46 2d 38 2c 20 61 6e 64 s use UTF-8, and
1f710 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 sqlite3_prepare
1f720 31 36 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 16() and sqlite3
1f730 5f 70 72 65 70 61 72 65 31 36 5f 76 32 28 29 0a _prepare16_v2().
1f740 2a 2a 20 75 73 65 20 55 54 46 2d 31 36 2e 0a 2a ** use UTF-16..*
1f750 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e 42 79 74 *.** If the nByt
1f760 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6c 65 e argument is le
1f770 73 73 20 74 68 61 6e 20 7a 65 72 6f 2c 20 74 68 ss than zero, th
1f780 65 6e 20 7a 53 71 6c 20 69 73 20 72 65 61 64 20 en zSql is read
1f790 75 70 20 74 6f 20 74 68 65 0a 2a 2a 20 66 69 72 up to the.** fir
1f7a0 73 74 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 st zero terminat
1f7b0 6f 72 2e 20 49 66 20 6e 42 79 74 65 20 69 73 20 or. If nByte is
1f7c0 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2c 20 74 68 non-negative, th
1f7d0 65 6e 20 69 74 20 69 73 20 74 68 65 20 6d 61 78 en it is the max
1f7e0 69 6d 75 6d 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f imum.** number o
1f7f0 66 20 20 62 79 74 65 73 20 72 65 61 64 20 66 72 f bytes read fr
1f800 6f 6d 20 7a 53 71 6c 2e 20 20 57 68 65 6e 20 6e om zSql. When n
1f810 42 79 74 65 20 69 73 20 6e 6f 6e 2d 6e 65 67 61 Byte is non-nega
1f820 74 69 76 65 2c 20 74 68 65 0a 2a 2a 20 7a 53 71 tive, the.** zSq
1f830 6c 20 73 74 72 69 6e 67 20 65 6e 64 73 20 61 74 l string ends at
1f840 20 65 69 74 68 65 72 20 74 68 65 20 66 69 72 73 either the firs
1f850 74 20 27 5c 30 30 30 27 20 6f 72 20 27 5c 75 30 t '\000' or '\u0
1f860 30 30 30 27 20 63 68 61 72 61 63 74 65 72 20 6f 000' character o
1f870 72 0a 2a 2a 20 74 68 65 20 6e 42 79 74 65 2d 74 r.** the nByte-t
1f880 68 20 62 79 74 65 2c 20 77 68 69 63 68 65 76 65 h byte, whicheve
1f890 72 20 63 6f 6d 65 73 20 66 69 72 73 74 2e 20 49 r comes first. I
1f8a0 66 20 74 68 65 20 63 61 6c 6c 65 72 20 6b 6e 6f f the caller kno
1f8b0 77 73 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 73 ws.** that the s
1f8c0 75 70 70 6c 69 65 64 20 73 74 72 69 6e 67 20 69 upplied string i
1f8d0 73 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64 s nul-terminated
1f8e0 2c 20 74 68 65 6e 20 74 68 65 72 65 20 69 73 20 , then there is
1f8f0 61 20 73 6d 61 6c 6c 0a 2a 2a 20 70 65 72 66 6f a small.** perfo
1f900 72 6d 61 6e 63 65 20 61 64 76 61 6e 74 61 67 65 rmance advantage
1f910 20 74 6f 20 62 65 20 67 61 69 6e 65 64 20 62 79 to be gained by
1f920 20 70 61 73 73 69 6e 67 20 61 6e 20 6e 42 79 74 passing an nByt
1f930 65 20 70 61 72 61 6d 65 74 65 72 20 74 68 61 74 e parameter that
1f940 0a 2a 2a 20 69 73 20 65 71 75 61 6c 20 74 6f 20 .** is equal to
1f950 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 the number of by
1f960 74 65 73 20 69 6e 20 74 68 65 20 69 6e 70 75 74 tes in the input
1f970 20 73 74 72 69 6e 67 20 3c 69 3e 69 6e 63 6c 75 string <i>inclu
1f980 64 69 6e 67 3c 2f 69 3e 0a 2a 2a 20 74 68 65 20 ding</i>.** the
1f990 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 6f 72 20 62 nul-terminator b
1f9a0 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 ytes..**.** If p
1f9b0 7a 54 61 69 6c 20 69 73 20 6e 6f 74 20 4e 55 4c zTail is not NUL
1f9c0 4c 20 74 68 65 6e 20 2a 70 7a 54 61 69 6c 20 69 L then *pzTail i
1f9d0 73 20 6d 61 64 65 20 74 6f 20 70 6f 69 6e 74 20 s made to point
1f9e0 74 6f 20 74 68 65 20 66 69 72 73 74 20 62 79 74 to the first byt
1f9f0 65 0a 2a 2a 20 70 61 73 74 20 74 68 65 20 65 6e e.** past the en
1fa00 64 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 53 d of the first S
1fa10 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 20 QL statement in
1fa20 7a 53 71 6c 2e 20 20 54 68 65 73 65 20 72 6f 75 zSql. These rou
1fa30 74 69 6e 65 73 20 6f 6e 6c 79 0a 2a 2a 20 63 6f tines only.** co
1fa40 6d 70 69 6c 65 20 74 68 65 20 66 69 72 73 74 20 mpile the first
1fa50 73 74 61 74 65 6d 65 6e 74 20 69 6e 20 7a 53 71 statement in zSq
1fa60 6c 2c 20 73 6f 20 2a 70 7a 54 61 69 6c 20 69 73 l, so *pzTail is
1fa70 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 74 left pointing t
1fa80 6f 0a 2a 2a 20 77 68 61 74 20 72 65 6d 61 69 6e o.** what remain
1fa90 73 20 75 6e 63 6f 6d 70 69 6c 65 64 2e 0a 2a 2a s uncompiled..**
1faa0 0a 2a 2a 20 2a 70 70 53 74 6d 74 20 69 73 20 6c .** *ppStmt is l
1fab0 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 eft pointing to
1fac0 61 20 63 6f 6d 70 69 6c 65 64 20 5b 70 72 65 70 a compiled [prep
1fad0 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 ared statement]
1fae0 74 68 61 74 20 63 61 6e 20 62 65 0a 2a 2a 20 65 that can be.** e
1faf0 78 65 63 75 74 65 64 20 75 73 69 6e 67 20 5b 73 xecuted using [s
1fb00 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 2e 20 qlite3_step()].
1fb10 20 49 66 20 74 68 65 72 65 20 69 73 20 61 6e 20 If there is an
1fb20 65 72 72 6f 72 2c 20 2a 70 70 53 74 6d 74 20 69 error, *ppStmt i
1fb30 73 20 73 65 74 0a 2a 2a 20 74 6f 20 4e 55 4c 4c s set.** to NULL
1fb40 2e 20 20 49 66 20 74 68 65 20 69 6e 70 75 74 20 . If the input
1fb50 74 65 78 74 20 63 6f 6e 74 61 69 6e 73 20 6e 6f text contains no
1fb60 20 53 51 4c 20 28 69 66 20 74 68 65 20 69 6e 70 SQL (if the inp
1fb70 75 74 20 69 73 20 61 6e 20 65 6d 70 74 79 0a 2a ut is an empty.*
1fb80 2a 20 73 74 72 69 6e 67 20 6f 72 20 61 20 63 6f * string or a co
1fb90 6d 6d 65 6e 74 29 20 74 68 65 6e 20 2a 70 70 53 mment) then *ppS
1fba0 74 6d 74 20 69 73 20 73 65 74 20 74 6f 20 4e 55 tmt is set to NU
1fbb0 4c 4c 2e 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 69 LL..** The calli
1fbc0 6e 67 20 70 72 6f 63 65 64 75 72 65 20 69 73 20 ng procedure is
1fbd0 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 responsible for
1fbe0 64 65 6c 65 74 69 6e 67 20 74 68 65 20 63 6f 6d deleting the com
1fbf0 70 69 6c 65 64 0a 2a 2a 20 53 51 4c 20 73 74 61 piled.** SQL sta
1fc00 74 65 6d 65 6e 74 20 75 73 69 6e 67 20 5b 73 71 tement using [sq
1fc10 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 lite3_finalize()
1fc20 5d 20 61 66 74 65 72 20 69 74 20 68 61 73 20 66 ] after it has f
1fc30 69 6e 69 73 68 65 64 20 77 69 74 68 20 69 74 2e inished with it.
1fc40 0a 2a 2a 20 70 70 53 74 6d 74 20 6d 61 79 20 6e .** ppStmt may n
1fc50 6f 74 20 62 65 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a ot be NULL..**.*
1fc60 2a 20 4f 6e 20 73 75 63 63 65 73 73 2c 20 5b 53 * On success, [S
1fc70 51 4c 49 54 45 5f 4f 4b 5d 20 69 73 20 72 65 74 QLITE_OK] is ret
1fc80 75 72 6e 65 64 2c 20 6f 74 68 65 72 77 69 73 65 urned, otherwise
1fc90 20 61 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d an [error code]
1fca0 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a is returned..**
1fcb0 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
1fcc0 70 72 65 70 61 72 65 5f 76 32 28 29 20 61 6e 64 prepare_v2() and
1fcd0 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 sqlite3_prepare
1fce0 31 36 5f 76 32 28 29 20 69 6e 74 65 72 66 61 63 16_v2() interfac
1fcf0 65 73 20 61 72 65 0a 2a 2a 20 72 65 63 6f 6d 6d es are.** recomm
1fd00 65 6e 64 65 64 20 66 6f 72 20 61 6c 6c 20 6e 65 ended for all ne
1fd10 77 20 70 72 6f 67 72 61 6d 73 2e 20 54 68 65 20 w programs. The
1fd20 74 77 6f 20 6f 6c 64 65 72 20 69 6e 74 65 72 66 two older interf
1fd30 61 63 65 73 20 61 72 65 20 72 65 74 61 69 6e 65 aces are retaine
1fd40 64 0a 2a 2a 20 66 6f 72 20 62 61 63 6b 77 61 72 d.** for backwar
1fd50 64 73 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 ds compatibility
1fd60 2c 20 62 75 74 20 74 68 65 69 72 20 75 73 65 20 , but their use
1fd70 69 73 20 64 69 73 63 6f 75 72 61 67 65 64 2e 0a is discouraged..
1fd80 2a 2a 20 49 6e 20 74 68 65 20 22 76 32 22 20 69 ** In the "v2" i
1fd90 6e 74 65 72 66 61 63 65 73 2c 20 74 68 65 20 70 nterfaces, the p
1fda0 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
1fdb0 74 0a 2a 2a 20 74 68 61 74 20 69 73 20 72 65 74 t.** that is ret
1fdc0 75 72 6e 65 64 20 28 74 68 65 20 5b 73 71 6c 69 urned (the [sqli
1fdd0 74 65 33 5f 73 74 6d 74 5d 20 6f 62 6a 65 63 74 te3_stmt] object
1fde0 29 20 63 6f 6e 74 61 69 6e 73 20 61 20 63 6f 70 ) contains a cop
1fdf0 79 20 6f 66 20 74 68 65 0a 2a 2a 20 6f 72 69 67 y of the.** orig
1fe00 69 6e 61 6c 20 53 51 4c 20 74 65 78 74 2e 20 54 inal SQL text. T
1fe10 68 69 73 20 63 61 75 73 65 73 20 74 68 65 20 5b his causes the [
1fe20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 sqlite3_step()]
1fe30 69 6e 74 65 72 66 61 63 65 20 74 6f 0a 2a 2a 20 interface to.**
1fe40 62 65 68 61 76 65 20 61 20 64 69 66 66 65 72 65 behave a differe
1fe50 6e 74 6c 79 20 69 6e 20 74 77 6f 20 77 61 79 73 ntly in two ways
1fe60 3a 0a 2a 2a 0a 2a 2a 20 3c 6f 6c 3e 0a 2a 2a 20 :.**.** <ol>.**
1fe70 3c 6c 69 3e 0a 2a 2a 20 49 66 20 74 68 65 20 64 <li>.** If the d
1fe80 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 63 atabase schema c
1fe90 68 61 6e 67 65 73 2c 20 69 6e 73 74 65 61 64 20 hanges, instead
1fea0 6f 66 20 72 65 74 75 72 6e 69 6e 67 20 5b 53 51 of returning [SQ
1feb0 4c 49 54 45 5f 53 43 48 45 4d 41 5d 20 61 73 20 LITE_SCHEMA] as
1fec0 69 74 0a 2a 2a 20 61 6c 77 61 79 73 20 75 73 65 it.** always use
1fed0 64 20 74 6f 20 64 6f 2c 20 5b 73 71 6c 69 74 65 d to do, [sqlite
1fee0 33 5f 73 74 65 70 28 29 5d 20 77 69 6c 6c 20 61 3_step()] will a
1fef0 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 72 65 63 utomatically rec
1ff00 6f 6d 70 69 6c 65 20 74 68 65 20 53 51 4c 0a 2a ompile the SQL.*
1ff10 2a 20 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 * statement and
1ff20 74 72 79 20 74 6f 20 72 75 6e 20 69 74 20 61 67 try to run it ag
1ff30 61 69 6e 2e 20 20 49 66 20 74 68 65 20 73 63 68 ain. If the sch
1ff40 65 6d 61 20 68 61 73 20 63 68 61 6e 67 65 64 20 ema has changed
1ff50 69 6e 0a 2a 2a 20 61 20 77 61 79 20 74 68 61 74 in.** a way that
1ff60 20 6d 61 6b 65 73 20 74 68 65 20 73 74 61 74 65 makes the state
1ff70 6d 65 6e 74 20 6e 6f 20 6c 6f 6e 67 65 72 20 76 ment no longer v
1ff80 61 6c 69 64 2c 20 5b 73 71 6c 69 74 65 33 5f 73 alid, [sqlite3_s
1ff90 74 65 70 28 29 5d 20 77 69 6c 6c 20 73 74 69 6c tep()] will stil
1ffa0 6c 0a 2a 2a 20 72 65 74 75 72 6e 20 5b 53 51 4c l.** return [SQL
1ffb0 49 54 45 5f 53 43 48 45 4d 41 5d 2e 20 20 42 75 ITE_SCHEMA]. Bu
1ffc0 74 20 75 6e 6c 69 6b 65 20 74 68 65 20 6c 65 67 t unlike the leg
1ffd0 61 63 79 20 62 65 68 61 76 69 6f 72 2c 20 5b 53 acy behavior, [S
1ffe0 51 4c 49 54 45 5f 53 43 48 45 4d 41 5d 20 69 73 QLITE_SCHEMA] is
1fff0 0a 2a 2a 20 6e 6f 77 20 61 20 66 61 74 61 6c 20 .** now a fatal
20000 65 72 72 6f 72 2e 20 20 43 61 6c 6c 69 6e 67 20 error. Calling
20010 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 [sqlite3_prepare
20020 5f 76 32 28 29 5d 20 61 67 61 69 6e 20 77 69 6c _v2()] again wil
20030 6c 20 6e 6f 74 20 6d 61 6b 65 20 74 68 65 0a 2a l not make the.*
20040 2a 20 65 72 72 6f 72 20 67 6f 20 61 77 61 79 2e * error go away.
20050 20 20 4e 6f 74 65 3a 20 75 73 65 20 5b 73 71 6c Note: use [sql
20060 69 74 65 33 5f 65 72 72 6d 73 67 28 29 5d 20 74 ite3_errmsg()] t
20070 6f 20 66 69 6e 64 20 74 68 65 20 74 65 78 74 0a o find the text.
20080 2a 2a 20 6f 66 20 74 68 65 20 70 61 72 73 69 6e ** of the parsin
20090 67 20 65 72 72 6f 72 20 74 68 61 74 20 72 65 73 g error that res
200a0 75 6c 74 73 20 69 6e 20 61 6e 20 5b 53 51 4c 49 ults in an [SQLI
200b0 54 45 5f 53 43 48 45 4d 41 5d 20 72 65 74 75 72 TE_SCHEMA] retur
200c0 6e 2e 0a 2a 2a 20 3c 2f 6c 69 3e 0a 2a 2a 0a 2a n..** </li>.**.*
200d0 2a 20 3c 6c 69 3e 0a 2a 2a 20 57 68 65 6e 20 61 * <li>.** When a
200e0 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 n error occurs,
200f0 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d [sqlite3_step()]
20100 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 6f 6e 65 will return one
20110 20 6f 66 20 74 68 65 20 64 65 74 61 69 6c 65 64 of the detailed
20120 0a 2a 2a 20 5b 65 72 72 6f 72 20 63 6f 64 65 73 .** [error codes
20130 5d 20 6f 72 20 5b 65 78 74 65 6e 64 65 64 20 65 ] or [extended e
20140 72 72 6f 72 20 63 6f 64 65 73 5d 2e 20 20 54 68 rror codes]. Th
20150 65 20 6c 65 67 61 63 79 20 62 65 68 61 76 69 6f e legacy behavio
20160 72 20 77 61 73 20 74 68 61 74 0a 2a 2a 20 5b 73 r was that.** [s
20170 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 77 qlite3_step()] w
20180 6f 75 6c 64 20 6f 6e 6c 79 20 72 65 74 75 72 6e ould only return
20190 20 61 20 67 65 6e 65 72 69 63 20 5b 53 51 4c 49 a generic [SQLI
201a0 54 45 5f 45 52 52 4f 52 5d 20 72 65 73 75 6c 74 TE_ERROR] result
201b0 20 63 6f 64 65 0a 2a 2a 20 61 6e 64 20 79 6f 75 code.** and you
201c0 20 77 6f 75 6c 64 20 68 61 76 65 20 74 6f 20 6d would have to m
201d0 61 6b 65 20 61 20 73 65 63 6f 6e 64 20 63 61 6c ake a second cal
201e0 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 72 65 l to [sqlite3_re
201f0 73 65 74 28 29 5d 20 69 6e 20 6f 72 64 65 72 0a set()] in order.
20200 2a 2a 20 74 6f 20 66 69 6e 64 20 74 68 65 20 75 ** to find the u
20210 6e 64 65 72 6c 79 69 6e 67 20 63 61 75 73 65 20 nderlying cause
20220 6f 66 20 74 68 65 20 70 72 6f 62 6c 65 6d 2e 20 of the problem.
20230 57 69 74 68 20 74 68 65 20 22 76 32 22 20 70 72 With the "v2" pr
20240 65 70 61 72 65 0a 2a 2a 20 69 6e 74 65 72 66 61 epare.** interfa
20250 63 65 73 2c 20 74 68 65 20 75 6e 64 65 72 6c 79 ces, the underly
20260 69 6e 67 20 72 65 61 73 6f 6e 20 66 6f 72 20 74 ing reason for t
20270 68 65 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 he error is retu
20280 72 6e 65 64 20 69 6d 6d 65 64 69 61 74 65 6c 79 rned immediately
20290 2e 0a 2a 2a 20 3c 2f 6c 69 3e 0a 2a 2a 20 3c 2f ..** </li>.** </
202a0 6f 6c 3e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 ol>.**.** Requir
202b0 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 30 ements:.** [H130
202c0 31 31 5d 20 5b 48 31 33 30 31 32 5d 20 5b 48 31 11] [H13012] [H1
202d0 33 30 31 33 5d 20 5b 48 31 33 30 31 34 5d 20 5b 3013] [H13014] [
202e0 48 31 33 30 31 35 5d 20 5b 48 31 33 30 31 36 5d H13015] [H13016]
202f0 20 5b 48 31 33 30 31 39 5d 20 5b 48 31 33 30 32 [H13019] [H1302
20300 31 5d 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 1].**.*/.SQLITE_
20310 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
20320 70 72 65 70 61 72 65 28 0a 20 20 73 71 6c 69 74 prepare(. sqlit
20330 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 e3 *db,
20340 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68 /* Database h
20350 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 andle */. const
20360 20 63 68 61 72 20 2a 7a 53 71 6c 2c 20 20 20 20 char *zSql,
20370 20 20 20 2f 2a 20 53 51 4c 20 73 74 61 74 65 6d /* SQL statem
20380 65 6e 74 2c 20 55 54 46 2d 38 20 65 6e 63 6f 64 ent, UTF-8 encod
20390 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 ed */. int nByt
203a0 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e,
203b0 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 /* Maximum lengt
203c0 68 20 6f 66 20 7a 53 71 6c 20 69 6e 20 62 79 74 h of zSql in byt
203d0 65 73 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 es. */. sqlite3
203e0 5f 73 74 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20 _stmt **ppStmt,
203f0 20 2f 2a 20 4f 55 54 3a 20 53 74 61 74 65 6d 65 /* OUT: Stateme
20400 6e 74 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 nt handle */. c
20410 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70 7a 54 61 onst char **pzTa
20420 69 6c 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50 il /* OUT: P
20430 6f 69 6e 74 65 72 20 74 6f 20 75 6e 75 73 65 64 ointer to unused
20440 20 70 6f 72 74 69 6f 6e 20 6f 66 20 7a 53 71 6c portion of zSql
20450 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 */.);.SQLITE_AP
20460 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 72 I int sqlite3_pr
20470 65 70 61 72 65 5f 76 32 28 0a 20 20 73 71 6c 69 epare_v2(. sqli
20480 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 te3 *db,
20490 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 /* Database
204a0 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 handle */. cons
204b0 74 20 63 68 61 72 20 2a 7a 53 71 6c 2c 20 20 20 t char *zSql,
204c0 20 20 20 20 2f 2a 20 53 51 4c 20 73 74 61 74 65 /* SQL state
204d0 6d 65 6e 74 2c 20 55 54 46 2d 38 20 65 6e 63 6f ment, UTF-8 enco
204e0 64 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 ded */. int nBy
204f0 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 te,
20500 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 /* Maximum leng
20510 74 68 20 6f 66 20 7a 53 71 6c 20 69 6e 20 62 79 th of zSql in by
20520 74 65 73 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 tes. */. sqlite
20530 33 5f 73 74 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 3_stmt **ppStmt,
20540 20 20 2f 2a 20 4f 55 54 3a 20 53 74 61 74 65 6d /* OUT: Statem
20550 65 6e 74 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 ent handle */.
20560 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70 7a 54 const char **pzT
20570 61 69 6c 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 ail /* OUT:
20580 50 6f 69 6e 74 65 72 20 74 6f 20 75 6e 75 73 65 Pointer to unuse
20590 64 20 70 6f 72 74 69 6f 6e 20 6f 66 20 7a 53 71 d portion of zSq
205a0 6c 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 l */.);.SQLITE_A
205b0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 PI int sqlite3_p
205c0 72 65 70 61 72 65 31 36 28 0a 20 20 73 71 6c 69 repare16(. sqli
205d0 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 te3 *db,
205e0 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 /* Database
205f0 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 handle */. cons
20600 74 20 76 6f 69 64 20 2a 7a 53 71 6c 2c 20 20 20 t void *zSql,
20610 20 20 20 20 2f 2a 20 53 51 4c 20 73 74 61 74 65 /* SQL state
20620 6d 65 6e 74 2c 20 55 54 46 2d 31 36 20 65 6e 63 ment, UTF-16 enc
20630 6f 64 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 oded */. int nB
20640 79 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 yte,
20650 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c 65 6e /* Maximum len
20660 67 74 68 20 6f 66 20 7a 53 71 6c 20 69 6e 20 62 gth of zSql in b
20670 79 74 65 73 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 ytes. */. sqlit
20680 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53 74 6d 74 e3_stmt **ppStmt
20690 2c 20 20 2f 2a 20 4f 55 54 3a 20 53 74 61 74 65 , /* OUT: State
206a0 6d 65 6e 74 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 ment handle */.
206b0 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 2a 70 7a const void **pz
206c0 54 61 69 6c 20 20 20 20 20 2f 2a 20 4f 55 54 3a Tail /* OUT:
206d0 20 50 6f 69 6e 74 65 72 20 74 6f 20 75 6e 75 73 Pointer to unus
206e0 65 64 20 70 6f 72 74 69 6f 6e 20 6f 66 20 7a 53 ed portion of zS
206f0 71 6c 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f ql */.);.SQLITE_
20700 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
20710 70 72 65 70 61 72 65 31 36 5f 76 32 28 0a 20 20 prepare16_v2(.
20720 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 sqlite3 *db,
20730 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 /* Datab
20740 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 ase handle */.
20750 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 53 71 6c const void *zSql
20760 2c 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 20 73 , /* SQL s
20770 74 61 74 65 6d 65 6e 74 2c 20 55 54 46 2d 31 36 tatement, UTF-16
20780 20 65 6e 63 6f 64 65 64 20 2a 2f 0a 20 20 69 6e encoded */. in
20790 74 20 6e 42 79 74 65 2c 20 20 20 20 20 20 20 20 t nByte,
207a0 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d /* Maximum
207b0 20 6c 65 6e 67 74 68 20 6f 66 20 7a 53 71 6c 20 length of zSql
207c0 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20 73 in bytes. */. s
207d0 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70 qlite3_stmt **pp
207e0 53 74 6d 74 2c 20 20 2f 2a 20 4f 55 54 3a 20 53 Stmt, /* OUT: S
207f0 74 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65 20 tatement handle
20800 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 */. const void
20810 2a 2a 70 7a 54 61 69 6c 20 20 20 20 20 2f 2a 20 **pzTail /*
20820 4f 55 54 3a 20 50 6f 69 6e 74 65 72 20 74 6f 20 OUT: Pointer to
20830 75 6e 75 73 65 64 20 70 6f 72 74 69 6f 6e 20 6f unused portion o
20840 66 20 7a 53 71 6c 20 2a 2f 0a 29 3b 0a 0a 2f 2a f zSql */.);../*
20850 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 65 .** CAPI3REF: Re
20860 74 72 69 65 76 69 6e 67 20 53 74 61 74 65 6d 65 trieving Stateme
20870 6e 74 20 53 51 4c 20 7b 48 31 33 31 30 30 7d 20 nt SQL {H13100}
20880 3c 48 31 33 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 <H13000>.**.** T
20890 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 63 61 his interface ca
208a0 6e 20 62 65 20 75 73 65 64 20 74 6f 20 72 65 74 n be used to ret
208b0 72 69 65 76 65 20 61 20 73 61 76 65 64 20 63 6f rieve a saved co
208c0 70 79 20 6f 66 20 74 68 65 20 6f 72 69 67 69 6e py of the origin
208d0 61 6c 0a 2a 2a 20 53 51 4c 20 74 65 78 74 20 75 al.** SQL text u
208e0 73 65 64 20 74 6f 20 63 72 65 61 74 65 20 61 20 sed to create a
208f0 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d [prepared statem
20900 65 6e 74 5d 20 69 66 20 74 68 61 74 20 73 74 61 ent] if that sta
20910 74 65 6d 65 6e 74 20 77 61 73 0a 2a 2a 20 63 6f tement was.** co
20920 6d 70 69 6c 65 64 20 75 73 69 6e 67 20 65 69 74 mpiled using eit
20930 68 65 72 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 her [sqlite3_pre
20940 70 61 72 65 5f 76 32 28 29 5d 20 6f 72 20 5b 73 pare_v2()] or [s
20950 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 qlite3_prepare16
20960 5f 76 32 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 _v2()]..**.** Re
20970 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b quirements:.** [
20980 48 31 33 31 30 31 5d 20 5b 48 31 33 31 30 32 5d H13101] [H13102]
20990 20 5b 48 31 33 31 30 33 5d 0a 2a 2f 0a 53 51 4c [H13103].*/.SQL
209a0 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 ITE_API const ch
209b0 61 72 20 2a 73 71 6c 69 74 65 33 5f 73 71 6c 28 ar *sqlite3_sql(
209c0 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 sqlite3_stmt *pS
209d0 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 tmt);../*.** CAP
209e0 49 33 52 45 46 3a 20 44 79 6e 61 6d 69 63 61 6c I3REF: Dynamical
209f0 6c 79 20 54 79 70 65 64 20 56 61 6c 75 65 20 4f ly Typed Value O
20a00 62 6a 65 63 74 20 7b 48 31 35 30 30 30 7d 20 3c bject {H15000} <
20a10 53 32 30 32 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f S20200>.** KEYWO
20a20 52 44 53 3a 20 7b 70 72 6f 74 65 63 74 65 64 20 RDS: {protected
20a30 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 7d 20 7b sqlite3_value} {
20a40 75 6e 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 unprotected sqli
20a50 74 65 33 5f 76 61 6c 75 65 7d 0a 2a 2a 0a 2a 2a te3_value}.**.**
20a60 20 53 51 4c 69 74 65 20 75 73 65 73 20 74 68 65 SQLite uses the
20a70 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f sqlite3_value o
20a80 62 6a 65 63 74 20 74 6f 20 72 65 70 72 65 73 65 bject to represe
20a90 6e 74 20 61 6c 6c 20 76 61 6c 75 65 73 0a 2a 2a nt all values.**
20aa0 20 74 68 61 74 20 63 61 6e 20 62 65 20 73 74 6f that can be sto
20ab0 72 65 64 20 69 6e 20 61 20 64 61 74 61 62 61 73 red in a databas
20ac0 65 20 74 61 62 6c 65 2e 20 53 51 4c 69 74 65 20 e table. SQLite
20ad0 75 73 65 73 20 64 79 6e 61 6d 69 63 20 74 79 70 uses dynamic typ
20ae0 69 6e 67 0a 2a 2a 20 66 6f 72 20 74 68 65 20 76 ing.** for the v
20af0 61 6c 75 65 73 20 69 74 20 73 74 6f 72 65 73 2e alues it stores.
20b00 20 56 61 6c 75 65 73 20 73 74 6f 72 65 64 20 69 Values stored i
20b10 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 n sqlite3_value
20b20 6f 62 6a 65 63 74 73 0a 2a 2a 20 63 61 6e 20 62 objects.** can b
20b30 65 20 69 6e 74 65 67 65 72 73 2c 20 66 6c 6f 61 e integers, floa
20b40 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 ting point value
20b50 73 2c 20 73 74 72 69 6e 67 73 2c 20 42 4c 4f 42 s, strings, BLOB
20b60 73 2c 20 6f 72 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a s, or NULL..**.*
20b70 2a 20 41 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c * An sqlite3_val
20b80 75 65 20 6f 62 6a 65 63 74 20 6d 61 79 20 62 65 ue object may be
20b90 20 65 69 74 68 65 72 20 22 70 72 6f 74 65 63 74 either "protect
20ba0 65 64 22 20 6f 72 20 22 75 6e 70 72 6f 74 65 63 ed" or "unprotec
20bb0 74 65 64 22 2e 0a 2a 2a 20 53 6f 6d 65 20 69 6e ted"..** Some in
20bc0 74 65 72 66 61 63 65 73 20 72 65 71 75 69 72 65 terfaces require
20bd0 20 61 20 70 72 6f 74 65 63 74 65 64 20 73 71 6c a protected sql
20be0 69 74 65 33 5f 76 61 6c 75 65 2e 20 20 4f 74 68 ite3_value. Oth
20bf0 65 72 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a er interfaces.**
20c00 20 77 69 6c 6c 20 61 63 63 65 70 74 20 65 69 74 will accept eit
20c10 68 65 72 20 61 20 70 72 6f 74 65 63 74 65 64 20 her a protected
20c20 6f 72 20 61 6e 20 75 6e 70 72 6f 74 65 63 74 65 or an unprotecte
20c30 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2e d sqlite3_value.
20c40 0a 2a 2a 20 45 76 65 72 79 20 69 6e 74 65 72 66 .** Every interf
20c50 61 63 65 20 74 68 61 74 20 61 63 63 65 70 74 73 ace that accepts
20c60 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 61 sqlite3_value a
20c70 72 67 75 6d 65 6e 74 73 20 73 70 65 63 69 66 69 rguments specifi
20c80 65 73 0a 2a 2a 20 77 68 65 74 68 65 72 20 6f 72 es.** whether or
20c90 20 6e 6f 74 20 69 74 20 72 65 71 75 69 72 65 73 not it requires
20ca0 20 61 20 70 72 6f 74 65 63 74 65 64 20 73 71 6c a protected sql
20cb0 69 74 65 33 5f 76 61 6c 75 65 2e 0a 2a 2a 0a 2a ite3_value..**.*
20cc0 2a 20 54 68 65 20 74 65 72 6d 73 20 22 70 72 6f * The terms "pro
20cd0 74 65 63 74 65 64 22 20 61 6e 64 20 22 75 6e 70 tected" and "unp
20ce0 72 6f 74 65 63 74 65 64 22 20 72 65 66 65 72 20 rotected" refer
20cf0 74 6f 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f to whether or no
20d00 74 0a 2a 2a 20 61 20 6d 75 74 65 78 20 69 73 20 t.** a mutex is
20d10 68 65 6c 64 2e 20 20 41 20 69 6e 74 65 72 6e 61 held. A interna
20d20 6c 20 6d 75 74 65 78 20 69 73 20 68 65 6c 64 20 l mutex is held
20d30 66 6f 72 20 61 20 70 72 6f 74 65 63 74 65 64 0a for a protected.
20d40 2a 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 ** sqlite3_value
20d50 20 6f 62 6a 65 63 74 20 62 75 74 20 6e 6f 20 6d object but no m
20d60 75 74 65 78 20 69 73 20 68 65 6c 64 20 66 6f 72 utex is held for
20d70 20 61 6e 20 75 6e 70 72 6f 74 65 63 74 65 64 0a an unprotected.
20d80 2a 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 ** sqlite3_value
20d90 20 6f 62 6a 65 63 74 2e 20 20 49 66 20 53 51 4c object. If SQL
20da0 69 74 65 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 ite is compiled
20db0 74 6f 20 62 65 20 73 69 6e 67 6c 65 2d 74 68 72 to be single-thr
20dc0 65 61 64 65 64 0a 2a 2a 20 28 77 69 74 68 20 5b eaded.** (with [
20dd0 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 SQLITE_THREADSAF
20de0 45 3d 30 5d 20 61 6e 64 20 77 69 74 68 20 5b 73 E=0] and with [s
20df0 71 6c 69 74 65 33 5f 74 68 72 65 61 64 73 61 66 qlite3_threadsaf
20e00 65 28 29 5d 20 72 65 74 75 72 6e 69 6e 67 20 30 e()] returning 0
20e10 29 0a 2a 2a 20 6f 72 20 69 66 20 53 51 4c 69 74 ).** or if SQLit
20e20 65 20 69 73 20 72 75 6e 20 69 6e 20 6f 6e 65 20 e is run in one
20e30 6f 66 20 72 65 64 75 63 65 64 20 6d 75 74 65 78 of reduced mutex
20e40 20 6d 6f 64 65 73 20 0a 2a 2a 20 5b 53 51 4c 49 modes .** [SQLI
20e50 54 45 5f 43 4f 4e 46 49 47 5f 53 49 4e 47 4c 45 TE_CONFIG_SINGLE
20e60 54 48 52 45 41 44 5d 20 6f 72 20 5b 53 51 4c 49 THREAD] or [SQLI
20e70 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 4c 54 49 54 TE_CONFIG_MULTIT
20e80 48 52 45 41 44 5d 0a 2a 2a 20 74 68 65 6e 20 74 HREAD].** then t
20e90 68 65 72 65 20 69 73 20 6e 6f 20 64 69 73 74 69 here is no disti
20ea0 6e 63 74 69 6f 6e 20 62 65 74 77 65 65 6e 20 70 nction between p
20eb0 72 6f 74 65 63 74 65 64 20 61 6e 64 20 75 6e 70 rotected and unp
20ec0 72 6f 74 65 63 74 65 64 0a 2a 2a 20 73 71 6c 69 rotected.** sqli
20ed0 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 te3_value object
20ee0 73 20 61 6e 64 20 74 68 65 79 20 63 61 6e 20 62 s and they can b
20ef0 65 20 75 73 65 64 20 69 6e 74 65 72 63 68 61 6e e used interchan
20f00 67 65 61 62 6c 79 2e 20 20 48 6f 77 65 76 65 72 geably. However
20f10 2c 0a 2a 2a 20 66 6f 72 20 6d 61 78 69 6d 75 6d ,.** for maximum
20f20 20 63 6f 64 65 20 70 6f 72 74 61 62 69 6c 69 74 code portabilit
20f30 79 20 69 74 20 69 73 20 72 65 63 6f 6d 6d 65 6e y it is recommen
20f40 64 65 64 20 74 68 61 74 20 61 70 70 6c 69 63 61 ded that applica
20f50 74 69 6f 6e 73 0a 2a 2a 20 73 74 69 6c 6c 20 6d tions.** still m
20f60 61 6b 65 20 74 68 65 20 64 69 73 74 69 6e 63 74 ake the distinct
20f70 69 6f 6e 20 62 65 74 77 65 65 6e 20 62 65 74 77 ion between betw
20f80 65 65 6e 20 70 72 6f 74 65 63 74 65 64 20 61 6e een protected an
20f90 64 20 75 6e 70 72 6f 74 65 63 74 65 64 0a 2a 2a d unprotected.**
20fa0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f sqlite3_value o
20fb0 62 6a 65 63 74 73 20 65 76 65 6e 20 77 68 65 6e bjects even when
20fc0 20 6e 6f 74 20 73 74 72 69 63 74 6c 79 20 72 65 not strictly re
20fd0 71 75 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 quired..**.** Th
20fe0 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 e sqlite3_value
20ff0 6f 62 6a 65 63 74 73 20 74 68 61 74 20 61 72 65 objects that are
21000 20 70 61 73 73 65 64 20 61 73 20 70 61 72 61 6d passed as param
21010 65 74 65 72 73 20 69 6e 74 6f 20 74 68 65 0a 2a eters into the.*
21020 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e * implementation
21030 20 6f 66 20 5b 61 70 70 6c 69 63 61 74 69 6f 6e of [application
21040 2d 64 65 66 69 6e 65 64 20 53 51 4c 20 66 75 6e -defined SQL fun
21050 63 74 69 6f 6e 73 5d 20 61 72 65 20 70 72 6f 74 ctions] are prot
21060 65 63 74 65 64 2e 0a 2a 2a 20 54 68 65 20 73 71 ected..** The sq
21070 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 lite3_value obje
21080 63 74 20 72 65 74 75 72 6e 65 64 20 62 79 0a 2a ct returned by.*
21090 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d * [sqlite3_colum
210a0 6e 5f 76 61 6c 75 65 28 29 5d 20 69 73 20 75 6e n_value()] is un
210b0 70 72 6f 74 65 63 74 65 64 2e 0a 2a 2a 20 55 6e protected..** Un
210c0 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 protected sqlite
210d0 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 73 20 3_value objects
210e0 6d 61 79 20 6f 6e 6c 79 20 62 65 20 75 73 65 64 may only be used
210f0 20 77 69 74 68 0a 2a 2a 20 5b 73 71 6c 69 74 65 with.** [sqlite
21100 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 29 3_result_value()
21110 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 62 ] and [sqlite3_b
21120 69 6e 64 5f 76 61 6c 75 65 28 29 5d 2e 0a 2a 2a ind_value()]..**
21130 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 61 The [sqlite3_va
21140 6c 75 65 5f 62 6c 6f 62 20 7c 20 73 71 6c 69 74 lue_blob | sqlit
21150 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 29 5d e3_value_type()]
21160 20 66 61 6d 69 6c 79 20 6f 66 0a 2a 2a 20 69 6e family of.** in
21170 74 65 72 66 61 63 65 73 20 72 65 71 75 69 72 65 terfaces require
21180 20 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 protected sqlit
21190 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 73 e3_value objects
211a0 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 ..*/.typedef str
211b0 75 63 74 20 4d 65 6d 20 73 71 6c 69 74 65 33 5f uct Mem sqlite3_
211c0 76 61 6c 75 65 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 value;../*.** CA
211d0 50 49 33 52 45 46 3a 20 53 51 4c 20 46 75 6e 63 PI3REF: SQL Func
211e0 74 69 6f 6e 20 43 6f 6e 74 65 78 74 20 4f 62 6a tion Context Obj
211f0 65 63 74 20 7b 48 31 36 30 30 31 7d 20 3c 53 32 ect {H16001} <S2
21200 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 0200>.**.** The
21210 63 6f 6e 74 65 78 74 20 69 6e 20 77 68 69 63 68 context in which
21220 20 61 6e 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e an SQL function
21230 20 65 78 65 63 75 74 65 73 20 69 73 20 73 74 6f executes is sto
21240 72 65 64 20 69 6e 20 61 6e 0a 2a 2a 20 73 71 6c red in an.** sql
21250 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 6f 62 6a ite3_context obj
21260 65 63 74 2e 20 20 41 20 70 6f 69 6e 74 65 72 20 ect. A pointer
21270 74 6f 20 61 6e 20 73 71 6c 69 74 65 33 5f 63 6f to an sqlite3_co
21280 6e 74 65 78 74 20 6f 62 6a 65 63 74 0a 2a 2a 20 ntext object.**
21290 69 73 20 61 6c 77 61 79 73 20 66 69 72 73 74 20 is always first
212a0 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 61 70 parameter to [ap
212b0 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 plication-define
212c0 64 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 5d d SQL functions]
212d0 2e 0a 2a 2a 20 54 68 65 20 61 70 70 6c 69 63 61 ..** The applica
212e0 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 53 51 4c tion-defined SQL
212f0 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70 6c 65 6d function implem
21300 65 6e 74 61 74 69 6f 6e 20 77 69 6c 6c 20 70 61 entation will pa
21310 73 73 20 74 68 69 73 0a 2a 2a 20 70 6f 69 6e 74 ss this.** point
21320 65 72 20 74 68 72 6f 75 67 68 20 69 6e 74 6f 20 er through into
21330 63 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 calls to [sqlite
21340 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20 7c 20 73 3_result_int | s
21350 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 28 29 5d qlite3_result()]
21360 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 61 67 ,.** [sqlite3_ag
21370 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 gregate_context(
21380 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 75 73 65 )], [sqlite3_use
21390 72 5f 64 61 74 61 28 29 5d 2c 0a 2a 2a 20 5b 73 r_data()],.** [s
213a0 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 qlite3_context_d
213b0 62 5f 68 61 6e 64 6c 65 28 29 5d 2c 20 5b 73 71 b_handle()], [sq
213c0 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74 lite3_get_auxdat
213d0 61 28 29 5d 2c 0a 2a 2a 20 61 6e 64 2f 6f 72 20 a()],.** and/or
213e0 5b 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 [sqlite3_set_aux
213f0 64 61 74 61 28 29 5d 2e 0a 2a 2f 0a 74 79 70 65 data()]..*/.type
21400 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 def struct sqlit
21410 65 33 5f 63 6f 6e 74 65 78 74 20 73 71 6c 69 74 e3_context sqlit
21420 65 33 5f 63 6f 6e 74 65 78 74 3b 0a 0a 2f 2a 0a e3_context;../*.
21430 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 42 69 6e ** CAPI3REF: Bin
21440 64 69 6e 67 20 56 61 6c 75 65 73 20 54 6f 20 50 ding Values To P
21450 72 65 70 61 72 65 64 20 53 74 61 74 65 6d 65 6e repared Statemen
21460 74 73 20 7b 48 31 33 35 30 30 7d 20 3c 53 37 30 ts {H13500} <S70
21470 33 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 300>.** KEYWORDS
21480 3a 20 7b 68 6f 73 74 20 70 61 72 61 6d 65 74 65 : {host paramete
21490 72 7d 20 7b 68 6f 73 74 20 70 61 72 61 6d 65 74 r} {host paramet
214a0 65 72 73 7d 20 7b 68 6f 73 74 20 70 61 72 61 6d ers} {host param
214b0 65 74 65 72 20 6e 61 6d 65 7d 0a 2a 2a 20 4b 45 eter name}.** KE
214c0 59 57 4f 52 44 53 3a 20 7b 53 51 4c 20 70 61 72 YWORDS: {SQL par
214d0 61 6d 65 74 65 72 7d 20 7b 53 51 4c 20 70 61 72 ameter} {SQL par
214e0 61 6d 65 74 65 72 73 7d 20 7b 70 61 72 61 6d 65 ameters} {parame
214f0 74 65 72 20 62 69 6e 64 69 6e 67 7d 0a 2a 2a 0a ter binding}.**.
21500 2a 2a 20 49 6e 20 74 68 65 20 53 51 4c 20 73 74 ** In the SQL st
21510 72 69 6e 67 73 20 69 6e 70 75 74 20 74 6f 20 5b rings input to [
21520 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f sqlite3_prepare_
21530 76 32 28 29 5d 20 61 6e 64 20 69 74 73 20 76 61 v2()] and its va
21540 72 69 61 6e 74 73 2c 0a 2a 2a 20 6c 69 74 65 72 riants,.** liter
21550 61 6c 73 20 6d 61 79 20 62 65 20 72 65 70 6c 61 als may be repla
21560 63 65 64 20 62 79 20 61 20 5b 70 61 72 61 6d 65 ced by a [parame
21570 74 65 72 5d 20 74 68 61 74 20 6d 61 74 63 68 65 ter] that matche
21580 73 20 6f 6e 65 20 6f 66 20 66 6f 6c 6c 6f 77 69 s one of followi
21590 6e 67 0a 2a 2a 20 74 65 6d 70 6c 61 74 65 73 3a ng.** templates:
215a0 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c .**.** <ul>.** <
215b0 6c 69 3e 20 20 3f 0a 2a 2a 20 3c 6c 69 3e 20 20 li> ?.** <li>
215c0 3f 4e 4e 4e 0a 2a 2a 20 3c 6c 69 3e 20 20 3a 56 ?NNN.** <li> :V
215d0 56 56 0a 2a 2a 20 3c 6c 69 3e 20 20 40 56 56 56 VV.** <li> @VVV
215e0 0a 2a 2a 20 3c 6c 69 3e 20 20 24 56 56 56 0a 2a .** <li> $VVV.*
215f0 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 49 6e * </ul>.**.** In
21600 20 74 68 65 20 74 65 6d 70 6c 61 74 65 73 20 61 the templates a
21610 62 6f 76 65 2c 20 4e 4e 4e 20 72 65 70 72 65 73 bove, NNN repres
21620 65 6e 74 73 20 61 6e 20 69 6e 74 65 67 65 72 20 ents an integer
21630 6c 69 74 65 72 61 6c 2c 0a 2a 2a 20 61 6e 64 20 literal,.** and
21640 56 56 56 20 72 65 70 72 65 73 65 6e 74 73 20 61 VVV represents a
21650 6e 20 61 6c 70 68 61 6e 75 6d 65 72 69 63 20 69 n alphanumeric i
21660 64 65 6e 74 69 66 65 72 2e 20 20 54 68 65 20 76 dentifer. The v
21670 61 6c 75 65 73 20 6f 66 20 74 68 65 73 65 0a 2a alues of these.*
21680 2a 20 70 61 72 61 6d 65 74 65 72 73 20 28 61 6c * parameters (al
21690 73 6f 20 63 61 6c 6c 65 64 20 22 68 6f 73 74 20 so called "host
216a0 70 61 72 61 6d 65 74 65 72 20 6e 61 6d 65 73 22 parameter names"
216b0 20 6f 72 20 22 53 51 4c 20 70 61 72 61 6d 65 74 or "SQL paramet
216c0 65 72 73 22 29 0a 2a 2a 20 63 61 6e 20 62 65 20 ers").** can be
216d0 73 65 74 20 75 73 69 6e 67 20 74 68 65 20 73 71 set using the sq
216e0 6c 69 74 65 33 5f 62 69 6e 64 5f 2a 28 29 20 72 lite3_bind_*() r
216f0 6f 75 74 69 6e 65 73 20 64 65 66 69 6e 65 64 20 outines defined
21700 68 65 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 here..**.** The
21710 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 first argument t
21720 6f 20 74 68 65 20 73 71 6c 69 74 65 33 5f 62 69 o the sqlite3_bi
21730 6e 64 5f 2a 28 29 20 72 6f 75 74 69 6e 65 73 20 nd_*() routines
21740 69 73 20 61 6c 77 61 79 73 0a 2a 2a 20 61 20 70 is always.** a p
21750 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 5b 73 ointer to the [s
21760 71 6c 69 74 65 33 5f 73 74 6d 74 5d 20 6f 62 6a qlite3_stmt] obj
21770 65 63 74 20 72 65 74 75 72 6e 65 64 20 66 72 6f ect returned fro
21780 6d 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 m.** [sqlite3_pr
21790 65 70 61 72 65 5f 76 32 28 29 5d 20 6f 72 20 69 epare_v2()] or i
217a0 74 73 20 76 61 72 69 61 6e 74 73 2e 0a 2a 2a 0a ts variants..**.
217b0 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 ** The second ar
217c0 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 69 6e gument is the in
217d0 64 65 78 20 6f 66 20 74 68 65 20 53 51 4c 20 70 dex of the SQL p
217e0 61 72 61 6d 65 74 65 72 20 74 6f 20 62 65 20 73 arameter to be s
217f0 65 74 2e 0a 2a 2a 20 54 68 65 20 6c 65 66 74 6d et..** The leftm
21800 6f 73 74 20 53 51 4c 20 70 61 72 61 6d 65 74 65 ost SQL paramete
21810 72 20 68 61 73 20 61 6e 20 69 6e 64 65 78 20 6f r has an index o
21820 66 20 31 2e 20 20 57 68 65 6e 20 74 68 65 20 73 f 1. When the s
21830 61 6d 65 20 6e 61 6d 65 64 0a 2a 2a 20 53 51 4c ame named.** SQL
21840 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 75 73 parameter is us
21850 65 64 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 ed more than onc
21860 65 2c 20 73 65 63 6f 6e 64 20 61 6e 64 20 73 75 e, second and su
21870 62 73 65 71 75 65 6e 74 0a 2a 2a 20 6f 63 63 75 bsequent.** occu
21880 72 72 65 6e 63 65 73 20 68 61 76 65 20 74 68 65 rrences have the
21890 20 73 61 6d 65 20 69 6e 64 65 78 20 61 73 20 74 same index as t
218a0 68 65 20 66 69 72 73 74 20 6f 63 63 75 72 72 65 he first occurre
218b0 6e 63 65 2e 0a 2a 2a 20 54 68 65 20 69 6e 64 65 nce..** The inde
218c0 78 20 66 6f 72 20 6e 61 6d 65 64 20 70 61 72 61 x for named para
218d0 6d 65 74 65 72 73 20 63 61 6e 20 62 65 20 6c 6f meters can be lo
218e0 6f 6b 65 64 20 75 70 20 75 73 69 6e 67 20 74 68 oked up using th
218f0 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69 e.** [sqlite3_bi
21900 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e 64 nd_parameter_ind
21910 65 78 28 29 5d 20 41 50 49 20 69 66 20 64 65 73 ex()] API if des
21920 69 72 65 64 2e 20 20 54 68 65 20 69 6e 64 65 78 ired. The index
21930 0a 2a 2a 20 66 6f 72 20 22 3f 4e 4e 4e 22 20 70 .** for "?NNN" p
21940 61 72 61 6d 65 74 65 72 73 20 69 73 20 74 68 65 arameters is the
21950 20 76 61 6c 75 65 20 6f 66 20 4e 4e 4e 2e 0a 2a value of NNN..*
21960 2a 20 54 68 65 20 4e 4e 4e 20 76 61 6c 75 65 20 * The NNN value
21970 6d 75 73 74 20 62 65 20 62 65 74 77 65 65 6e 20 must be between
21980 31 20 61 6e 64 20 74 68 65 20 5b 73 71 6c 69 74 1 and the [sqlit
21990 65 33 5f 6c 69 6d 69 74 28 29 5d 0a 2a 2a 20 70 e3_limit()].** p
219a0 61 72 61 6d 65 74 65 72 20 5b 53 51 4c 49 54 45 arameter [SQLITE
219b0 5f 4c 49 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f _LIMIT_VARIABLE_
219c0 4e 55 4d 42 45 52 5d 20 28 64 65 66 61 75 6c 74 NUMBER] (default
219d0 20 76 61 6c 75 65 3a 20 39 39 39 29 2e 0a 2a 2a value: 999)..**
219e0 0a 2a 2a 20 54 68 65 20 74 68 69 72 64 20 61 72 .** The third ar
219f0 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 76 61 gument is the va
21a00 6c 75 65 20 74 6f 20 62 69 6e 64 20 74 6f 20 74 lue to bind to t
21a10 68 65 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a he parameter..**
21a20 0a 2a 2a 20 49 6e 20 74 68 6f 73 65 20 72 6f 75 .** In those rou
21a30 74 69 6e 65 73 20 74 68 61 74 20 68 61 76 65 20 tines that have
21a40 61 20 66 6f 75 72 74 68 20 61 72 67 75 6d 65 6e a fourth argumen
21a50 74 2c 20 69 74 73 20 76 61 6c 75 65 20 69 73 20 t, its value is
21a60 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 the.** number of
21a70 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 70 61 bytes in the pa
21a80 72 61 6d 65 74 65 72 2e 20 20 54 6f 20 62 65 20 rameter. To be
21a90 63 6c 65 61 72 3a 20 74 68 65 20 76 61 6c 75 65 clear: the value
21aa0 20 69 73 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 is the.** numbe
21ab0 72 20 6f 66 20 3c 75 3e 62 79 74 65 73 3c 2f 75 r of <u>bytes</u
21ac0 3e 20 69 6e 20 74 68 65 20 76 61 6c 75 65 2c 20 > in the value,
21ad0 6e 6f 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f not the number o
21ae0 66 20 63 68 61 72 61 63 74 65 72 73 2e 0a 2a 2a f characters..**
21af0 20 49 66 20 74 68 65 20 66 6f 75 72 74 68 20 70 If the fourth p
21b00 61 72 61 6d 65 74 65 72 20 69 73 20 6e 65 67 61 arameter is nega
21b10 74 69 76 65 2c 20 74 68 65 20 6c 65 6e 67 74 68 tive, the length
21b20 20 6f 66 20 74 68 65 20 73 74 72 69 6e 67 20 69 of the string i
21b30 73 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 s.** the number
21b40 6f 66 20 62 79 74 65 73 20 75 70 20 74 6f 20 74 of bytes up to t
21b50 68 65 20 66 69 72 73 74 20 7a 65 72 6f 20 74 65 he first zero te
21b60 72 6d 69 6e 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 rminator..**.**
21b70 54 68 65 20 66 69 66 74 68 20 61 72 67 75 6d 65 The fifth argume
21b80 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 69 nt to sqlite3_bi
21b90 6e 64 5f 62 6c 6f 62 28 29 2c 20 73 71 6c 69 74 nd_blob(), sqlit
21ba0 65 33 5f 62 69 6e 64 5f 74 65 78 74 28 29 2c 20 e3_bind_text(),
21bb0 61 6e 64 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 and.** sqlite3_b
21bc0 69 6e 64 5f 74 65 78 74 31 36 28 29 20 69 73 20 ind_text16() is
21bd0 61 20 64 65 73 74 72 75 63 74 6f 72 20 75 73 65 a destructor use
21be0 64 20 74 6f 20 64 69 73 70 6f 73 65 20 6f 66 20 d to dispose of
21bf0 74 68 65 20 42 4c 4f 42 20 6f 72 0a 2a 2a 20 73 the BLOB or.** s
21c00 74 72 69 6e 67 20 61 66 74 65 72 20 53 51 4c 69 tring after SQLi
21c10 74 65 20 68 61 73 20 66 69 6e 69 73 68 65 64 20 te has finished
21c20 77 69 74 68 20 69 74 2e 20 49 66 20 74 68 65 20 with it. If the
21c30 66 69 66 74 68 20 61 72 67 75 6d 65 6e 74 20 69 fifth argument i
21c40 73 0a 2a 2a 20 74 68 65 20 73 70 65 63 69 61 6c s.** the special
21c50 20 76 61 6c 75 65 20 5b 53 51 4c 49 54 45 5f 53 value [SQLITE_S
21c60 54 41 54 49 43 5d 2c 20 74 68 65 6e 20 53 51 4c TATIC], then SQL
21c70 69 74 65 20 61 73 73 75 6d 65 73 20 74 68 61 74 ite assumes that
21c80 20 74 68 65 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 the.** informat
21c90 69 6f 6e 20 69 73 20 69 6e 20 73 74 61 74 69 63 ion is in static
21ca0 2c 20 75 6e 6d 61 6e 61 67 65 64 20 73 70 61 63 , unmanaged spac
21cb0 65 20 61 6e 64 20 64 6f 65 73 20 6e 6f 74 20 6e e and does not n
21cc0 65 65 64 20 74 6f 20 62 65 20 66 72 65 65 64 2e eed to be freed.
21cd0 0a 2a 2a 20 49 66 20 74 68 65 20 66 69 66 74 68 .** If the fifth
21ce0 20 61 72 67 75 6d 65 6e 74 20 68 61 73 20 74 68 argument has th
21cf0 65 20 76 61 6c 75 65 20 5b 53 51 4c 49 54 45 5f e value [SQLITE_
21d00 54 52 41 4e 53 49 45 4e 54 5d 2c 20 74 68 65 6e TRANSIENT], then
21d10 0a 2a 2a 20 53 51 4c 69 74 65 20 6d 61 6b 65 73 .** SQLite makes
21d20 20 69 74 73 20 6f 77 6e 20 70 72 69 76 61 74 65 its own private
21d30 20 63 6f 70 79 20 6f 66 20 74 68 65 20 64 61 74 copy of the dat
21d40 61 20 69 6d 6d 65 64 69 61 74 65 6c 79 2c 20 62 a immediately, b
21d50 65 66 6f 72 65 0a 2a 2a 20 74 68 65 20 73 71 6c efore.** the sql
21d60 69 74 65 33 5f 62 69 6e 64 5f 2a 28 29 20 72 6f ite3_bind_*() ro
21d70 75 74 69 6e 65 20 72 65 74 75 72 6e 73 2e 0a 2a utine returns..*
21d80 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
21d90 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 28 29 _bind_zeroblob()
21da0 20 72 6f 75 74 69 6e 65 20 62 69 6e 64 73 20 61 routine binds a
21db0 20 42 4c 4f 42 20 6f 66 20 6c 65 6e 67 74 68 20 BLOB of length
21dc0 4e 20 74 68 61 74 0a 2a 2a 20 69 73 20 66 69 6c N that.** is fil
21dd0 6c 65 64 20 77 69 74 68 20 7a 65 72 6f 65 73 2e led with zeroes.
21de0 20 20 41 20 7a 65 72 6f 62 6c 6f 62 20 75 73 65 A zeroblob use
21df0 73 20 61 20 66 69 78 65 64 20 61 6d 6f 75 6e 74 s a fixed amount
21e00 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 28 6a of memory.** (j
21e10 75 73 74 20 61 6e 20 69 6e 74 65 67 65 72 20 74 ust an integer t
21e20 6f 20 68 6f 6c 64 20 69 74 73 20 73 69 7a 65 29 o hold its size)
21e30 20 77 68 69 6c 65 20 69 74 20 69 73 20 62 65 69 while it is bei
21e40 6e 67 20 70 72 6f 63 65 73 73 65 64 2e 0a 2a 2a ng processed..**
21e50 20 5a 65 72 6f 62 6c 6f 62 73 20 61 72 65 20 69 Zeroblobs are i
21e60 6e 74 65 6e 64 65 64 20 74 6f 20 73 65 72 76 65 ntended to serve
21e70 20 61 73 20 70 6c 61 63 65 68 6f 6c 64 65 72 73 as placeholders
21e80 20 66 6f 72 20 42 4c 4f 42 73 20 77 68 6f 73 65 for BLOBs whose
21e90 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 69 73 20 6c .** content is l
21ea0 61 74 65 72 20 77 72 69 74 74 65 6e 20 75 73 69 ater written usi
21eb0 6e 67 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 ng.** [sqlite3_b
21ec0 6c 6f 62 5f 6f 70 65 6e 20 7c 20 69 6e 63 72 65 lob_open | incre
21ed0 6d 65 6e 74 61 6c 20 42 4c 4f 42 20 49 2f 4f 5d mental BLOB I/O]
21ee0 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 20 41 20 routines..** A
21ef0 6e 65 67 61 74 69 76 65 20 76 61 6c 75 65 20 66 negative value f
21f00 6f 72 20 74 68 65 20 7a 65 72 6f 62 6c 6f 62 20 or the zeroblob
21f10 72 65 73 75 6c 74 73 20 69 6e 20 61 20 7a 65 72 results in a zer
21f20 6f 2d 6c 65 6e 67 74 68 20 42 4c 4f 42 2e 0a 2a o-length BLOB..*
21f30 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
21f40 5f 62 69 6e 64 5f 2a 28 29 20 72 6f 75 74 69 6e _bind_*() routin
21f50 65 73 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 es must be calle
21f60 64 20 61 66 74 65 72 0a 2a 2a 20 5b 73 71 6c 69 d after.** [sqli
21f70 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 te3_prepare_v2()
21f80 5d 20 28 61 6e 64 20 69 74 73 20 76 61 72 69 61 ] (and its varia
21f90 6e 74 73 29 20 6f 72 20 5b 73 71 6c 69 74 65 33 nts) or [sqlite3
21fa0 5f 72 65 73 65 74 28 29 5d 20 61 6e 64 0a 2a 2a _reset()] and.**
21fb0 20 62 65 66 6f 72 65 20 5b 73 71 6c 69 74 65 33 before [sqlite3
21fc0 5f 73 74 65 70 28 29 5d 2e 0a 2a 2a 20 42 69 6e _step()]..** Bin
21fd0 64 69 6e 67 73 20 61 72 65 20 6e 6f 74 20 63 6c dings are not cl
21fe0 65 61 72 65 64 20 62 79 20 74 68 65 20 5b 73 71 eared by the [sq
21ff0 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20 72 lite3_reset()] r
22000 6f 75 74 69 6e 65 2e 0a 2a 2a 20 55 6e 62 6f 75 outine..** Unbou
22010 6e 64 20 70 61 72 61 6d 65 74 65 72 73 20 61 72 nd parameters ar
22020 65 20 69 6e 74 65 72 70 72 65 74 65 64 20 61 73 e interpreted as
22030 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 NULL..**.** The
22040 73 65 20 72 6f 75 74 69 6e 65 73 20 72 65 74 75 se routines retu
22050 72 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 6f rn [SQLITE_OK] o
22060 6e 20 73 75 63 63 65 73 73 20 6f 72 20 61 6e 20 n success or an
22070 65 72 72 6f 72 20 63 6f 64 65 20 69 66 0a 2a 2a error code if.**
22080 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 20 77 anything goes w
22090 72 6f 6e 67 2e 20 20 5b 53 51 4c 49 54 45 5f 52 rong. [SQLITE_R
220a0 41 4e 47 45 5d 20 69 73 20 72 65 74 75 72 6e 65 ANGE] is returne
220b0 64 20 69 66 20 74 68 65 20 70 61 72 61 6d 65 74 d if the paramet
220c0 65 72 0a 2a 2a 20 69 6e 64 65 78 20 69 73 20 6f er.** index is o
220d0 75 74 20 6f 66 20 72 61 6e 67 65 2e 20 20 5b 53 ut of range. [S
220e0 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5d 20 69 73 20 QLITE_NOMEM] is
220f0 72 65 74 75 72 6e 65 64 20 69 66 20 6d 61 6c 6c returned if mall
22100 6f 63 28 29 20 66 61 69 6c 73 2e 0a 2a 2a 20 5b oc() fails..** [
22110 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 5d 20 6d SQLITE_MISUSE] m
22120 69 67 68 74 20 62 65 20 72 65 74 75 72 6e 65 64 ight be returned
22130 20 69 66 20 74 68 65 73 65 20 72 6f 75 74 69 6e if these routin
22140 65 73 20 61 72 65 20 63 61 6c 6c 65 64 20 6f 6e es are called on
22150 20 61 0a 2a 2a 20 76 69 72 74 75 61 6c 20 6d 61 a.** virtual ma
22160 63 68 69 6e 65 20 74 68 61 74 20 69 73 20 74 68 chine that is th
22170 65 20 77 72 6f 6e 67 20 73 74 61 74 65 20 6f 72 e wrong state or
22180 20 77 68 69 63 68 20 68 61 73 20 61 6c 72 65 61 which has alrea
22190 64 79 20 62 65 65 6e 20 66 69 6e 61 6c 69 7a 65 dy been finalize
221a0 64 2e 0a 2a 2a 20 44 65 74 65 63 74 69 6f 6e 20 d..** Detection
221b0 6f 66 20 6d 69 73 75 73 65 20 69 73 20 75 6e 72 of misuse is unr
221c0 65 6c 69 61 62 6c 65 2e 20 20 41 70 70 6c 69 63 eliable. Applic
221d0 61 74 69 6f 6e 73 20 73 68 6f 75 6c 64 20 6e 6f ations should no
221e0 74 20 64 65 70 65 6e 64 0a 2a 2a 20 6f 6e 20 53 t depend.** on S
221f0 51 4c 49 54 45 5f 4d 49 53 55 53 45 20 72 65 74 QLITE_MISUSE ret
22200 75 72 6e 73 2e 20 20 53 51 4c 49 54 45 5f 4d 49 urns. SQLITE_MI
22210 53 55 53 45 20 69 73 20 69 6e 74 65 6e 64 65 64 SUSE is intended
22220 20 74 6f 20 69 6e 64 69 63 61 74 65 20 61 0a 2a to indicate a.*
22230 2a 20 61 20 6c 6f 67 69 63 20 65 72 72 6f 72 20 * a logic error
22240 69 6e 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 in the applicati
22250 6f 6e 2e 20 20 46 75 74 75 72 65 20 76 65 72 73 on. Future vers
22260 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 6d ions of SQLite m
22270 69 67 68 74 0a 2a 2a 20 70 61 6e 69 63 20 72 61 ight.** panic ra
22280 74 68 65 72 20 74 68 61 6e 20 72 65 74 75 72 6e ther than return
22290 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 2e 0a SQLITE_MISUSE..
222a0 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 **.** See also:
222b0 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 [sqlite3_bind_pa
222c0 72 61 6d 65 74 65 72 5f 63 6f 75 6e 74 28 29 5d rameter_count()]
222d0 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69 ,.** [sqlite3_bi
222e0 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61 6d nd_parameter_nam
222f0 65 28 29 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 74 e()], and [sqlit
22300 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 e3_bind_paramete
22310 72 5f 69 6e 64 65 78 28 29 5d 2e 0a 2a 2a 0a 2a r_index()]..**.*
22320 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a * Requirements:.
22330 2a 2a 20 5b 48 31 33 35 30 36 5d 20 5b 48 31 33 ** [H13506] [H13
22340 35 30 39 5d 20 5b 48 31 33 35 31 32 5d 20 5b 48 509] [H13512] [H
22350 31 33 35 31 35 5d 20 5b 48 31 33 35 31 38 5d 20 13515] [H13518]
22360 5b 48 31 33 35 32 31 5d 20 5b 48 31 33 35 32 34 [H13521] [H13524
22370 5d 20 5b 48 31 33 35 32 37 5d 0a 2a 2a 20 5b 48 ] [H13527].** [H
22380 31 33 35 33 30 5d 20 5b 48 31 33 35 33 33 5d 20 13530] [H13533]
22390 5b 48 31 33 35 33 36 5d 20 5b 48 31 33 35 33 39 [H13536] [H13539
223a0 5d 20 5b 48 31 33 35 34 32 5d 20 5b 48 31 33 35 ] [H13542] [H135
223b0 34 35 5d 20 5b 48 31 33 35 34 38 5d 20 5b 48 31 45] [H13548] [H1
223c0 33 35 35 31 5d 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 3551].**.*/.SQLI
223d0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
223e0 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 28 73 71 6c e3_bind_blob(sql
223f0 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c ite3_stmt*, int,
22400 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e const void*, in
22410 74 20 6e 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 t n, void(*)(voi
22420 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 d*));.SQLITE_API
22430 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e int sqlite3_bin
22440 64 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 d_double(sqlite3
22450 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 64 6f 75 _stmt*, int, dou
22460 62 6c 65 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 ble);.SQLITE_API
22470 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e int sqlite3_bin
22480 64 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 d_int(sqlite3_st
22490 6d 74 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a mt*, int, int);.
224a0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
224b0 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 qlite3_bind_int6
224c0 34 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 4(sqlite3_stmt*,
224d0 20 69 6e 74 2c 20 73 71 6c 69 74 65 33 5f 69 6e int, sqlite3_in
224e0 74 36 34 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 t64);.SQLITE_API
224f0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e int sqlite3_bin
22500 64 5f 6e 75 6c 6c 28 73 71 6c 69 74 65 33 5f 73 d_null(sqlite3_s
22510 74 6d 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 tmt*, int);.SQLI
22520 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
22530 65 33 5f 62 69 6e 64 5f 74 65 78 74 28 73 71 6c e3_bind_text(sql
22540 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c ite3_stmt*, int,
22550 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e const char*, in
22560 74 20 6e 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 t n, void(*)(voi
22570 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 d*));.SQLITE_API
22580 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e int sqlite3_bin
22590 64 5f 74 65 78 74 31 36 28 73 71 6c 69 74 65 33 d_text16(sqlite3
225a0 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 63 6f 6e _stmt*, int, con
225b0 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 2c 20 76 st void*, int, v
225c0 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a oid(*)(void*));.
225d0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
225e0 71 6c 69 74 65 33 5f 62 69 6e 64 5f 76 61 6c 75 qlite3_bind_valu
225f0 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c e(sqlite3_stmt*,
22600 20 69 6e 74 2c 20 63 6f 6e 73 74 20 73 71 6c 69 int, const sqli
22610 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c te3_value*);.SQL
22620 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
22630 74 65 33 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f te3_bind_zeroblo
22640 62 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c b(sqlite3_stmt*,
22650 20 69 6e 74 2c 20 69 6e 74 20 6e 29 3b 0a 0a 2f int, int n);../
22660 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4e *.** CAPI3REF: N
22670 75 6d 62 65 72 20 4f 66 20 53 51 4c 20 50 61 72 umber Of SQL Par
22680 61 6d 65 74 65 72 73 20 7b 48 31 33 36 30 30 7d ameters {H13600}
22690 20 3c 53 37 30 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 <S70300>.**.**
226a0 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 61 6e This routine can
226b0 20 62 65 20 75 73 65 64 20 74 6f 20 66 69 6e 64 be used to find
226c0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 5b the number of [
226d0 53 51 4c 20 70 61 72 61 6d 65 74 65 72 73 5d 0a SQL parameters].
226e0 2a 2a 20 69 6e 20 61 20 5b 70 72 65 70 61 72 65 ** in a [prepare
226f0 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e 20 20 53 d statement]. S
22700 51 4c 20 70 61 72 61 6d 65 74 65 72 73 20 61 72 QL parameters ar
22710 65 20 74 6f 6b 65 6e 73 20 6f 66 20 74 68 65 0a e tokens of the.
22720 2a 2a 20 66 6f 72 6d 20 22 3f 22 2c 20 22 3f 4e ** form "?", "?N
22730 4e 4e 22 2c 20 22 3a 41 41 41 22 2c 20 22 24 41 NN", ":AAA", "$A
22740 41 41 22 2c 20 6f 72 20 22 40 41 41 41 22 20 74 AA", or "@AAA" t
22750 68 61 74 20 73 65 72 76 65 20 61 73 0a 2a 2a 20 hat serve as.**
22760 70 6c 61 63 65 68 6f 6c 64 65 72 73 20 66 6f 72 placeholders for
22770 20 76 61 6c 75 65 73 20 74 68 61 74 20 61 72 65 values that are
22780 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 [sqlite3_bind_b
22790 6c 6f 62 20 7c 20 62 6f 75 6e 64 5d 0a 2a 2a 20 lob | bound].**
227a0 74 6f 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 to the parameter
227b0 73 20 61 74 20 61 20 6c 61 74 65 72 20 74 69 6d s at a later tim
227c0 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f e..**.** This ro
227d0 75 74 69 6e 65 20 61 63 74 75 61 6c 6c 79 20 72 utine actually r
227e0 65 74 75 72 6e 73 20 74 68 65 20 69 6e 64 65 78 eturns the index
227f0 20 6f 66 20 74 68 65 20 6c 61 72 67 65 73 74 20 of the largest
22800 28 72 69 67 68 74 6d 6f 73 74 29 0a 2a 2a 20 70 (rightmost).** p
22810 61 72 61 6d 65 74 65 72 2e 20 46 6f 72 20 61 6c arameter. For al
22820 6c 20 66 6f 72 6d 73 20 65 78 63 65 70 74 20 3f l forms except ?
22830 4e 4e 4e 2c 20 74 68 69 73 20 77 69 6c 6c 20 63 NNN, this will c
22840 6f 72 72 65 73 70 6f 6e 64 20 74 6f 20 74 68 65 orrespond to the
22850 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 75 6e .** number of un
22860 69 71 75 65 20 70 61 72 61 6d 65 74 65 72 73 2e ique parameters.
22870 20 20 49 66 20 70 61 72 61 6d 65 74 65 72 73 20 If parameters
22880 6f 66 20 74 68 65 20 3f 4e 4e 4e 20 61 72 65 20 of the ?NNN are
22890 75 73 65 64 2c 0a 2a 2a 20 74 68 65 72 65 20 6d used,.** there m
228a0 61 79 20 62 65 20 67 61 70 73 20 69 6e 20 74 68 ay be gaps in th
228b0 65 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 53 65 e list..**.** Se
228c0 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 e also: [sqlite3
228d0 5f 62 69 6e 64 5f 62 6c 6f 62 7c 73 71 6c 69 74 _bind_blob|sqlit
228e0 65 33 5f 62 69 6e 64 28 29 5d 2c 0a 2a 2a 20 5b e3_bind()],.** [
228f0 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 sqlite3_bind_par
22900 61 6d 65 74 65 72 5f 6e 61 6d 65 28 29 5d 2c 20 ameter_name()],
22910 61 6e 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f and.** [sqlite3_
22920 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 69 bind_parameter_i
22930 6e 64 65 78 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 ndex()]..**.** R
22940 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 equirements:.**
22950 5b 48 31 33 36 30 31 5d 0a 2a 2f 0a 53 51 4c 49 [H13601].*/.SQLI
22960 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
22970 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 e3_bind_paramete
22980 72 5f 63 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f r_count(sqlite3_
22990 73 74 6d 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 stmt*);../*.** C
229a0 41 50 49 33 52 45 46 3a 20 4e 61 6d 65 20 4f 66 API3REF: Name Of
229b0 20 41 20 48 6f 73 74 20 50 61 72 61 6d 65 74 65 A Host Paramete
229c0 72 20 7b 48 31 33 36 32 30 7d 20 3c 53 37 30 33 r {H13620} <S703
229d0 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 00>.**.** This r
229e0 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 61 outine returns a
229f0 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 pointer to the
22a00 6e 61 6d 65 20 6f 66 20 74 68 65 20 6e 2d 74 68 name of the n-th
22a10 0a 2a 2a 20 5b 53 51 4c 20 70 61 72 61 6d 65 74 .** [SQL paramet
22a20 65 72 5d 20 69 6e 20 61 20 5b 70 72 65 70 61 72 er] in a [prepar
22a30 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e 0a 2a ed statement]..*
22a40 2a 20 53 51 4c 20 70 61 72 61 6d 65 74 65 72 73 * SQL parameters
22a50 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 3f 4e of the form "?N
22a60 4e 4e 22 20 6f 72 20 22 3a 41 41 41 22 20 6f 72 NN" or ":AAA" or
22a70 20 22 40 41 41 41 22 20 6f 72 20 22 24 41 41 41 "@AAA" or "$AAA
22a80 22 0a 2a 2a 20 68 61 76 65 20 61 20 6e 61 6d 65 ".** have a name
22a90 20 77 68 69 63 68 20 69 73 20 74 68 65 20 73 74 which is the st
22aa0 72 69 6e 67 20 22 3f 4e 4e 4e 22 20 6f 72 20 22 ring "?NNN" or "
22ab0 3a 41 41 41 22 20 6f 72 20 22 40 41 41 41 22 20 :AAA" or "@AAA"
22ac0 6f 72 20 22 24 41 41 41 22 0a 2a 2a 20 72 65 73 or "$AAA".** res
22ad0 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2a 20 49 6e pectively..** In
22ae0 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 74 68 other words, th
22af0 65 20 69 6e 69 74 69 61 6c 20 22 3a 22 20 6f 72 e initial ":" or
22b00 20 22 24 22 20 6f 72 20 22 40 22 20 6f 72 20 22 "$" or "@" or "
22b10 3f 22 0a 2a 2a 20 69 73 20 69 6e 63 6c 75 64 65 ?".** is include
22b20 64 20 61 73 20 70 61 72 74 20 6f 66 20 74 68 65 d as part of the
22b30 20 6e 61 6d 65 2e 0a 2a 2a 20 50 61 72 61 6d 65 name..** Parame
22b40 74 65 72 73 20 6f 66 20 74 68 65 20 66 6f 72 6d ters of the form
22b50 20 22 3f 22 20 77 69 74 68 6f 75 74 20 61 20 66 "?" without a f
22b60 6f 6c 6c 6f 77 69 6e 67 20 69 6e 74 65 67 65 72 ollowing integer
22b70 20 68 61 76 65 20 6e 6f 20 6e 61 6d 65 0a 2a 2a have no name.**
22b80 20 61 6e 64 20 61 72 65 20 61 6c 73 6f 20 72 65 and are also re
22b90 66 65 72 72 65 64 20 74 6f 20 61 73 20 22 61 6e ferred to as "an
22ba0 6f 6e 79 6d 6f 75 73 20 70 61 72 61 6d 65 74 65 onymous paramete
22bb0 72 73 22 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 rs"..**.** The f
22bc0 69 72 73 74 20 68 6f 73 74 20 70 61 72 61 6d 65 irst host parame
22bd0 74 65 72 20 68 61 73 20 61 6e 20 69 6e 64 65 78 ter has an index
22be0 20 6f 66 20 31 2c 20 6e 6f 74 20 30 2e 0a 2a 2a of 1, not 0..**
22bf0 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65 .** If the value
22c00 20 6e 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e n is out of ran
22c10 67 65 20 6f 72 20 69 66 20 74 68 65 20 6e 2d 74 ge or if the n-t
22c20 68 20 70 61 72 61 6d 65 74 65 72 20 69 73 0a 2a h parameter is.*
22c30 2a 20 6e 61 6d 65 6c 65 73 73 2c 20 74 68 65 6e * nameless, then
22c40 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 NULL is returne
22c50 64 2e 20 20 54 68 65 20 72 65 74 75 72 6e 65 64 d. The returned
22c60 20 73 74 72 69 6e 67 20 69 73 0a 2a 2a 20 61 6c string is.** al
22c70 77 61 79 73 20 69 6e 20 55 54 46 2d 38 20 65 6e ways in UTF-8 en
22c80 63 6f 64 69 6e 67 20 65 76 65 6e 20 69 66 20 74 coding even if t
22c90 68 65 20 6e 61 6d 65 64 20 70 61 72 61 6d 65 74 he named paramet
22ca0 65 72 20 77 61 73 0a 2a 2a 20 6f 72 69 67 69 6e er was.** origin
22cb0 61 6c 6c 79 20 73 70 65 63 69 66 69 65 64 20 61 ally specified a
22cc0 73 20 55 54 46 2d 31 36 20 69 6e 20 5b 73 71 6c s UTF-16 in [sql
22cd0 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 28 29 ite3_prepare16()
22ce0 5d 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 ] or.** [sqlite3
22cf0 5f 70 72 65 70 61 72 65 31 36 5f 76 32 28 29 5d _prepare16_v2()]
22d00 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f ..**.** See also
22d10 3a 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f : [sqlite3_bind_
22d20 62 6c 6f 62 7c 73 71 6c 69 74 65 33 5f 62 69 6e blob|sqlite3_bin
22d30 64 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65 d()],.** [sqlite
22d40 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 3_bind_parameter
22d50 5f 63 6f 75 6e 74 28 29 5d 2c 20 61 6e 64 0a 2a _count()], and.*
22d60 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f * [sqlite3_bind_
22d70 70 61 72 61 6d 65 74 65 72 5f 69 6e 64 65 78 28 parameter_index(
22d80 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 )]..**.** Requir
22d90 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 36 ements:.** [H136
22da0 32 31 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 21].*/.SQLITE_AP
22db0 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 I const char *sq
22dc0 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d lite3_bind_param
22dd0 65 74 65 72 5f 6e 61 6d 65 28 73 71 6c 69 74 65 eter_name(sqlite
22de0 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 29 3b 0a 0a 3_stmt*, int);..
22df0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
22e00 49 6e 64 65 78 20 4f 66 20 41 20 50 61 72 61 6d Index Of A Param
22e10 65 74 65 72 20 57 69 74 68 20 41 20 47 69 76 65 eter With A Give
22e20 6e 20 4e 61 6d 65 20 7b 48 31 33 36 34 30 7d 20 n Name {H13640}
22e30 3c 53 37 30 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 52 <S70300>.**.** R
22e40 65 74 75 72 6e 20 74 68 65 20 69 6e 64 65 78 20 eturn the index
22e50 6f 66 20 61 6e 20 53 51 4c 20 70 61 72 61 6d 65 of an SQL parame
22e60 74 65 72 20 67 69 76 65 6e 20 69 74 73 20 6e 61 ter given its na
22e70 6d 65 2e 20 20 54 68 65 0a 2a 2a 20 69 6e 64 65 me. The.** inde
22e80 78 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 x value returned
22e90 20 69 73 20 73 75 69 74 61 62 6c 65 20 66 6f 72 is suitable for
22ea0 20 75 73 65 20 61 73 20 74 68 65 20 73 65 63 6f use as the seco
22eb0 6e 64 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 nd.** parameter
22ec0 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 to [sqlite3_bind
22ed0 5f 62 6c 6f 62 7c 73 71 6c 69 74 65 33 5f 62 69 _blob|sqlite3_bi
22ee0 6e 64 28 29 5d 2e 20 20 41 20 7a 65 72 6f 0a 2a nd()]. A zero.*
22ef0 2a 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 * is returned if
22f00 20 6e 6f 20 6d 61 74 63 68 69 6e 67 20 70 61 72 no matching par
22f10 61 6d 65 74 65 72 20 69 73 20 66 6f 75 6e 64 2e ameter is found.
22f20 20 20 54 68 65 20 70 61 72 61 6d 65 74 65 72 0a The parameter.
22f30 2a 2a 20 6e 61 6d 65 20 6d 75 73 74 20 62 65 20 ** name must be
22f40 67 69 76 65 6e 20 69 6e 20 55 54 46 2d 38 20 65 given in UTF-8 e
22f50 76 65 6e 20 69 66 20 74 68 65 20 6f 72 69 67 69 ven if the origi
22f60 6e 61 6c 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a nal statement.**
22f70 20 77 61 73 20 70 72 65 70 61 72 65 64 20 66 72 was prepared fr
22f80 6f 6d 20 55 54 46 2d 31 36 20 74 65 78 74 20 75 om UTF-16 text u
22f90 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 70 72 sing [sqlite3_pr
22fa0 65 70 61 72 65 31 36 5f 76 32 28 29 5d 2e 0a 2a epare16_v2()]..*
22fb0 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b *.** See also: [
22fc0 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f sqlite3_bind_blo
22fd0 62 7c 73 71 6c 69 74 65 33 5f 62 69 6e 64 28 29 b|sqlite3_bind()
22fe0 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 ],.** [sqlite3_b
22ff0 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f ind_parameter_co
23000 75 6e 74 28 29 5d 2c 20 61 6e 64 0a 2a 2a 20 5b unt()], and.** [
23010 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 sqlite3_bind_par
23020 61 6d 65 74 65 72 5f 69 6e 64 65 78 28 29 5d 2e ameter_index()].
23030 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 .**.** Requireme
23040 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 36 34 31 5d nts:.** [H13641]
23050 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
23060 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f nt sqlite3_bind_
23070 70 61 72 61 6d 65 74 65 72 5f 69 6e 64 65 78 28 parameter_index(
23080 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 63 sqlite3_stmt*, c
23090 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 onst char *zName
230a0 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
230b0 45 46 3a 20 52 65 73 65 74 20 41 6c 6c 20 42 69 EF: Reset All Bi
230c0 6e 64 69 6e 67 73 20 4f 6e 20 41 20 50 72 65 70 ndings On A Prep
230d0 61 72 65 64 20 53 74 61 74 65 6d 65 6e 74 20 7b ared Statement {
230e0 48 31 33 36 36 30 7d 20 3c 53 37 30 33 30 30 3e H13660} <S70300>
230f0 0a 2a 2a 0a 2a 2a 20 43 6f 6e 74 72 61 72 79 20 .**.** Contrary
23100 74 6f 20 74 68 65 20 69 6e 74 75 69 74 69 6f 6e to the intuition
23110 20 6f 66 20 6d 61 6e 79 2c 20 5b 73 71 6c 69 74 of many, [sqlit
23120 65 33 5f 72 65 73 65 74 28 29 5d 20 64 6f 65 73 e3_reset()] does
23130 20 6e 6f 74 20 72 65 73 65 74 0a 2a 2a 20 74 68 not reset.** th
23140 65 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f e [sqlite3_bind_
23150 62 6c 6f 62 20 7c 20 62 69 6e 64 69 6e 67 73 5d blob | bindings]
23160 20 6f 6e 20 61 20 5b 70 72 65 70 61 72 65 64 20 on a [prepared
23170 73 74 61 74 65 6d 65 6e 74 5d 2e 0a 2a 2a 20 55 statement]..** U
23180 73 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 se this routine
23190 74 6f 20 72 65 73 65 74 20 61 6c 6c 20 68 6f 73 to reset all hos
231a0 74 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 t parameters to
231b0 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 NULL..**.** Requ
231c0 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 irements:.** [H1
231d0 33 36 36 31 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 3661].*/.SQLITE_
231e0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
231f0 63 6c 65 61 72 5f 62 69 6e 64 69 6e 67 73 28 73 clear_bindings(s
23200 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 0a qlite3_stmt*);..
23210 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
23220 4e 75 6d 62 65 72 20 4f 66 20 43 6f 6c 75 6d 6e Number Of Column
23230 73 20 49 6e 20 41 20 52 65 73 75 6c 74 20 53 65 s In A Result Se
23240 74 20 7b 48 31 33 37 31 30 7d 20 3c 53 31 30 37 t {H13710} <S107
23250 30 30 3e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 00>.**.** Return
23260 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 the number of c
23270 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 72 65 olumns in the re
23280 73 75 6c 74 20 73 65 74 20 72 65 74 75 72 6e 65 sult set returne
23290 64 20 62 79 20 74 68 65 0a 2a 2a 20 5b 70 72 65 d by the.** [pre
232a0 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d pared statement]
232b0 2e 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 . This routine r
232c0 65 74 75 72 6e 73 20 30 20 69 66 20 70 53 74 6d eturns 0 if pStm
232d0 74 20 69 73 20 61 6e 20 53 51 4c 0a 2a 2a 20 73 t is an SQL.** s
232e0 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20 64 6f tatement that do
232f0 65 73 20 6e 6f 74 20 72 65 74 75 72 6e 20 64 61 es not return da
23300 74 61 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 20 ta (for example
23310 61 6e 20 5b 55 50 44 41 54 45 5d 29 2e 0a 2a 2a an [UPDATE])..**
23320 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 .** Requirements
23330 3a 0a 2a 2a 20 5b 48 31 33 37 31 31 5d 0a 2a 2f :.** [H13711].*/
23340 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
23350 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63 sqlite3_column_c
23360 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d ount(sqlite3_stm
23370 74 20 2a 70 53 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a t *pStmt);../*.*
23380 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6c 75 * CAPI3REF: Colu
23390 6d 6e 20 4e 61 6d 65 73 20 49 6e 20 41 20 52 65 mn Names In A Re
233a0 73 75 6c 74 20 53 65 74 20 7b 48 31 33 37 32 30 sult Set {H13720
233b0 7d 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a } <S10700>.**.**
233c0 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 These routines
233d0 72 65 74 75 72 6e 20 74 68 65 20 6e 61 6d 65 20 return the name
233e0 61 73 73 69 67 6e 65 64 20 74 6f 20 61 20 70 61 assigned to a pa
233f0 72 74 69 63 75 6c 61 72 20 63 6f 6c 75 6d 6e 0a rticular column.
23400 2a 2a 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 ** in the result
23410 20 73 65 74 20 6f 66 20 61 20 5b 53 45 4c 45 43 set of a [SELEC
23420 54 5d 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 54 T] statement. T
23430 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d he sqlite3_colum
23440 6e 5f 6e 61 6d 65 28 29 0a 2a 2a 20 69 6e 74 65 n_name().** inte
23450 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 61 20 rface returns a
23460 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 7a 65 72 pointer to a zer
23470 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46 o-terminated UTF
23480 2d 38 20 73 74 72 69 6e 67 0a 2a 2a 20 61 6e 64 -8 string.** and
23490 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
234a0 6e 61 6d 65 31 36 28 29 20 72 65 74 75 72 6e 73 name16() returns
234b0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 a pointer to a
234c0 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 0a zero-terminated.
234d0 2a 2a 20 55 54 46 2d 31 36 20 73 74 72 69 6e 67 ** UTF-16 string
234e0 2e 20 20 54 68 65 20 66 69 72 73 74 20 70 61 72 . The first par
234f0 61 6d 65 74 65 72 20 69 73 20 74 68 65 20 5b 70 ameter is the [p
23500 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
23510 74 5d 0a 2a 2a 20 74 68 61 74 20 69 6d 70 6c 65 t].** that imple
23520 6d 65 6e 74 73 20 74 68 65 20 5b 53 45 4c 45 43 ments the [SELEC
23530 54 5d 20 73 74 61 74 65 6d 65 6e 74 2e 20 54 68 T] statement. Th
23540 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 e second paramet
23550 65 72 20 69 73 20 74 68 65 0a 2a 2a 20 63 6f 6c er is the.** col
23560 75 6d 6e 20 6e 75 6d 62 65 72 2e 20 20 54 68 65 umn number. The
23570 20 6c 65 66 74 6d 6f 73 74 20 63 6f 6c 75 6d 6e leftmost column
23580 20 69 73 20 6e 75 6d 62 65 72 20 30 2e 0a 2a 2a is number 0..**
23590 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64 .** The returned
235a0 20 73 74 72 69 6e 67 20 70 6f 69 6e 74 65 72 20 string pointer
235b0 69 73 20 76 61 6c 69 64 20 75 6e 74 69 6c 20 65 is valid until e
235c0 69 74 68 65 72 20 74 68 65 20 5b 70 72 65 70 61 ither the [prepa
235d0 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 0a 2a red statement].*
235e0 2a 20 69 73 20 64 65 73 74 72 6f 79 65 64 20 62 * is destroyed b
235f0 79 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c y [sqlite3_final
23600 69 7a 65 28 29 5d 20 6f 72 20 75 6e 74 69 6c 20 ize()] or until
23610 74 68 65 20 6e 65 78 74 20 63 61 6c 6c 20 74 6f the next call to
23620 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 .** sqlite3_colu
23630 6d 6e 5f 6e 61 6d 65 28 29 20 6f 72 20 73 71 6c mn_name() or sql
23640 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 ite3_column_name
23650 31 36 28 29 20 6f 6e 20 74 68 65 20 73 61 6d 65 16() on the same
23660 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 49 column..**.** I
23670 66 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 f sqlite3_malloc
23680 28 29 20 66 61 69 6c 73 20 64 75 72 69 6e 67 20 () fails during
23690 74 68 65 20 70 72 6f 63 65 73 73 69 6e 67 20 6f the processing o
236a0 66 20 65 69 74 68 65 72 20 72 6f 75 74 69 6e 65 f either routine
236b0 0a 2a 2a 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 .** (for example
236c0 20 64 75 72 69 6e 67 20 61 20 63 6f 6e 76 65 72 during a conver
236d0 73 69 6f 6e 20 66 72 6f 6d 20 55 54 46 2d 38 20 sion from UTF-8
236e0 74 6f 20 55 54 46 2d 31 36 29 20 74 68 65 6e 20 to UTF-16) then
236f0 61 0a 2a 2a 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 a.** NULL pointe
23700 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a r is returned..*
23710 2a 0a 2a 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 *.** The name of
23720 20 61 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e a result column
23730 20 69 73 20 74 68 65 20 76 61 6c 75 65 20 6f 66 is the value of
23740 20 74 68 65 20 22 41 53 22 20 63 6c 61 75 73 65 the "AS" clause
23750 20 66 6f 72 0a 2a 2a 20 74 68 61 74 20 63 6f 6c for.** that col
23760 75 6d 6e 2c 20 69 66 20 74 68 65 72 65 20 69 73 umn, if there is
23770 20 61 6e 20 41 53 20 63 6c 61 75 73 65 2e 20 20 an AS clause.
23780 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 41 If there is no A
23790 53 20 63 6c 61 75 73 65 0a 2a 2a 20 74 68 65 6e S clause.** then
237a0 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 the name of the
237b0 20 63 6f 6c 75 6d 6e 20 69 73 20 75 6e 73 70 65 column is unspe
237c0 63 69 66 69 65 64 20 61 6e 64 20 6d 61 79 20 63 cified and may c
237d0 68 61 6e 67 65 20 66 72 6f 6d 0a 2a 2a 20 6f 6e hange from.** on
237e0 65 20 72 65 6c 65 61 73 65 20 6f 66 20 53 51 4c e release of SQL
237f0 69 74 65 20 74 6f 20 74 68 65 20 6e 65 78 74 2e ite to the next.
23800 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 .**.** Requireme
23810 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 37 32 31 5d nts:.** [H13721]
23820 20 5b 48 31 33 37 32 33 5d 20 5b 48 31 33 37 32 [H13723] [H1372
23830 34 5d 20 5b 48 31 33 37 32 35 5d 20 5b 48 31 33 4] [H13725] [H13
23840 37 32 36 5d 20 5b 48 31 33 37 32 37 5d 0a 2a 2f 726] [H13727].*/
23850 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 .SQLITE_API cons
23860 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f t char *sqlite3_
23870 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 73 71 6c 69 column_name(sqli
23880 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 4e te3_stmt*, int N
23890 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f );.SQLITE_API co
238a0 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 nst void *sqlite
238b0 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 28 3_column_name16(
238c0 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 sqlite3_stmt*, i
238d0 6e 74 20 4e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 nt N);../*.** CA
238e0 50 49 33 52 45 46 3a 20 53 6f 75 72 63 65 20 4f PI3REF: Source O
238f0 66 20 44 61 74 61 20 49 6e 20 41 20 51 75 65 72 f Data In A Quer
23900 79 20 52 65 73 75 6c 74 20 7b 48 31 33 37 34 30 y Result {H13740
23910 7d 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a } <S10700>.**.**
23920 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 These routines
23930 70 72 6f 76 69 64 65 20 61 20 6d 65 61 6e 73 20 provide a means
23940 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 77 68 61 to determine wha
23950 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 77 68 61 74 t column of what
23960 0a 2a 2a 20 74 61 62 6c 65 20 69 6e 20 77 68 69 .** table in whi
23970 63 68 20 64 61 74 61 62 61 73 65 20 61 20 72 65 ch database a re
23980 73 75 6c 74 20 6f 66 20 61 20 5b 53 45 4c 45 43 sult of a [SELEC
23990 54 5d 20 73 74 61 74 65 6d 65 6e 74 20 63 6f 6d T] statement com
239a0 65 73 20 66 72 6f 6d 2e 0a 2a 2a 20 54 68 65 20 es from..** The
239b0 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 name of the data
239c0 62 61 73 65 20 6f 72 20 74 61 62 6c 65 20 6f 72 base or table or
239d0 20 63 6f 6c 75 6d 6e 20 63 61 6e 20 62 65 20 72 column can be r
239e0 65 74 75 72 6e 65 64 20 61 73 0a 2a 2a 20 65 69 eturned as.** ei
239f0 74 68 65 72 20 61 20 55 54 46 2d 38 20 6f 72 20 ther a UTF-8 or
23a00 55 54 46 2d 31 36 20 73 74 72 69 6e 67 2e 20 20 UTF-16 string.
23a10 54 68 65 20 5f 64 61 74 61 62 61 73 65 5f 20 72 The _database_ r
23a20 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e 0a 2a outines return.*
23a30 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6e * the database n
23a40 61 6d 65 2c 20 74 68 65 20 5f 74 61 62 6c 65 5f ame, the _table_
23a50 20 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e routines return
23a60 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 2c the table name,
23a70 20 61 6e 64 0a 2a 2a 20 74 68 65 20 6f 72 69 67 and.** the orig
23a80 69 6e 5f 20 72 6f 75 74 69 6e 65 73 20 72 65 74 in_ routines ret
23a90 75 72 6e 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e urn the column n
23aa0 61 6d 65 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 ame..** The retu
23ab0 72 6e 65 64 20 73 74 72 69 6e 67 20 69 73 20 76 rned string is v
23ac0 61 6c 69 64 20 75 6e 74 69 6c 20 74 68 65 20 5b alid until the [
23ad0 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
23ae0 6e 74 5d 20 69 73 20 64 65 73 74 72 6f 79 65 64 nt] is destroyed
23af0 0a 2a 2a 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 .** using [sqlit
23b00 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 20 6f e3_finalize()] o
23b10 72 20 75 6e 74 69 6c 20 74 68 65 20 73 61 6d 65 r until the same
23b20 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 information is
23b30 72 65 71 75 65 73 74 65 64 0a 2a 2a 20 61 67 61 requested.** aga
23b40 69 6e 20 69 6e 20 61 20 64 69 66 66 65 72 65 6e in in a differen
23b50 74 20 65 6e 63 6f 64 69 6e 67 2e 0a 2a 2a 0a 2a t encoding..**.*
23b60 2a 20 54 68 65 20 6e 61 6d 65 73 20 72 65 74 75 * The names retu
23b70 72 6e 65 64 20 61 72 65 20 74 68 65 20 6f 72 69 rned are the ori
23b80 67 69 6e 61 6c 20 75 6e 2d 61 6c 69 61 73 65 64 ginal un-aliased
23b90 20 6e 61 6d 65 73 20 6f 66 20 74 68 65 0a 2a 2a names of the.**
23ba0 20 64 61 74 61 62 61 73 65 2c 20 74 61 62 6c 65 database, table
23bb0 2c 20 61 6e 64 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a , and column..**
23bc0 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61 72 .** The first ar
23bd0 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 66 6f gument to the fo
23be0 6c 6c 6f 77 69 6e 67 20 63 61 6c 6c 73 20 69 73 llowing calls is
23bf0 20 61 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 a [prepared sta
23c00 74 65 6d 65 6e 74 5d 2e 0a 2a 2a 20 54 68 65 73 tement]..** Thes
23c10 65 20 66 75 6e 63 74 69 6f 6e 73 20 72 65 74 75 e functions retu
23c20 72 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 rn information a
23c30 62 6f 75 74 20 74 68 65 20 4e 74 68 20 63 6f 6c bout the Nth col
23c40 75 6d 6e 20 72 65 74 75 72 6e 65 64 20 62 79 0a umn returned by.
23c50 2a 2a 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 ** the statement
23c60 2c 20 77 68 65 72 65 20 4e 20 69 73 20 74 68 65 , where N is the
23c70 20 73 65 63 6f 6e 64 20 66 75 6e 63 74 69 6f 6e second function
23c80 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a argument..**.**
23c90 20 49 66 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 If the Nth colu
23ca0 6d 6e 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 mn returned by t
23cb0 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 he statement is
23cc0 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 an expression or
23cd0 0a 2a 2a 20 73 75 62 71 75 65 72 79 20 61 6e 64 .** subquery and
23ce0 20 69 73 20 6e 6f 74 20 61 20 63 6f 6c 75 6d 6e is not a column
23cf0 20 76 61 6c 75 65 2c 20 74 68 65 6e 20 61 6c 6c value, then all
23d00 20 6f 66 20 74 68 65 73 65 20 66 75 6e 63 74 69 of these functi
23d10 6f 6e 73 20 72 65 74 75 72 6e 0a 2a 2a 20 4e 55 ons return.** NU
23d20 4c 4c 2e 20 20 54 68 65 73 65 20 72 6f 75 74 69 LL. These routi
23d30 6e 65 20 6d 69 67 68 74 20 61 6c 73 6f 20 72 65 ne might also re
23d40 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 61 20 6d turn NULL if a m
23d50 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
23d60 20 65 72 72 6f 72 0a 2a 2a 20 6f 63 63 75 72 73 error.** occurs
23d70 2e 20 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68 . Otherwise, th
23d80 65 79 20 72 65 74 75 72 6e 20 74 68 65 20 6e 61 ey return the na
23d90 6d 65 20 6f 66 20 74 68 65 20 61 74 74 61 63 68 me of the attach
23da0 65 64 20 64 61 74 61 62 61 73 65 2c 20 74 61 62 ed database, tab
23db0 6c 65 0a 2a 2a 20 61 6e 64 20 63 6f 6c 75 6d 6e le.** and column
23dc0 20 74 68 61 74 20 71 75 65 72 79 20 72 65 73 75 that query resu
23dd0 6c 74 20 63 6f 6c 75 6d 6e 20 77 61 73 20 65 78 lt column was ex
23de0 74 72 61 63 74 65 64 20 66 72 6f 6d 2e 0a 2a 2a tracted from..**
23df0 0a 2a 2a 20 41 73 20 77 69 74 68 20 61 6c 6c 20 .** As with all
23e00 6f 74 68 65 72 20 53 51 4c 69 74 65 20 41 50 49 other SQLite API
23e10 73 2c 20 74 68 6f 73 65 20 70 6f 73 74 66 69 78 s, those postfix
23e20 65 64 20 77 69 74 68 20 22 31 36 22 20 72 65 74 ed with "16" ret
23e30 75 72 6e 0a 2a 2a 20 55 54 46 2d 31 36 20 65 6e urn.** UTF-16 en
23e40 63 6f 64 65 64 20 73 74 72 69 6e 67 73 2c 20 74 coded strings, t
23e50 68 65 20 6f 74 68 65 72 20 66 75 6e 63 74 69 6f he other functio
23e60 6e 73 20 72 65 74 75 72 6e 20 55 54 46 2d 38 2e ns return UTF-8.
23e70 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 {END}.**.** The
23e80 73 65 20 41 50 49 73 20 61 72 65 20 6f 6e 6c 79 se APIs are only
23e90 20 61 76 61 69 6c 61 62 6c 65 20 69 66 20 74 68 available if th
23ea0 65 20 6c 69 62 72 61 72 79 20 77 61 73 20 63 6f e library was co
23eb0 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68 65 0a mpiled with the.
23ec0 2a 2a 20 5b 53 51 4c 49 54 45 5f 45 4e 41 42 4c ** [SQLITE_ENABL
23ed0 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 E_COLUMN_METADAT
23ee0 41 5d 20 43 2d 70 72 65 70 72 6f 63 65 73 73 6f A] C-preprocesso
23ef0 72 20 73 79 6d 62 6f 6c 20 64 65 66 69 6e 65 64 r symbol defined
23f00 2e 0a 2a 2a 0a 2a 2a 20 7b 41 31 33 37 35 31 7d ..**.** {A13751}
23f10 0a 2a 2a 20 49 66 20 74 77 6f 20 6f 72 20 6d 6f .** If two or mo
23f20 72 65 20 74 68 72 65 61 64 73 20 63 61 6c 6c 20 re threads call
23f30 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74 one or more of t
23f40 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 67 hese routines ag
23f50 61 69 6e 73 74 20 74 68 65 20 73 61 6d 65 0a 2a ainst the same.*
23f60 2a 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 * prepared state
23f70 6d 65 6e 74 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 ment and column
23f80 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 at the same time
23f90 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 then the result
23fa0 73 20 61 72 65 0a 2a 2a 20 75 6e 64 65 66 69 6e s are.** undefin
23fb0 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 ed..**.** Requir
23fc0 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 37 ements:.** [H137
23fd0 34 31 5d 20 5b 48 31 33 37 34 32 5d 20 5b 48 31 41] [H13742] [H1
23fe0 33 37 34 33 5d 20 5b 48 31 33 37 34 34 5d 20 5b 3743] [H13744] [
23ff0 48 31 33 37 34 35 5d 20 5b 48 31 33 37 34 36 5d H13745] [H13746]
24000 20 5b 48 31 33 37 34 38 5d 0a 2a 2a 0a 2a 2a 20 [H13748].**.**
24010 49 66 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 74 If two or more t
24020 68 72 65 61 64 73 20 63 61 6c 6c 20 6f 6e 65 20 hreads call one
24030 6f 72 20 6d 6f 72 65 0a 2a 2a 20 5b 73 71 6c 69 or more.** [sqli
24040 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 te3_column_datab
24050 61 73 65 5f 6e 61 6d 65 20 7c 20 63 6f 6c 75 6d ase_name | colum
24060 6e 20 6d 65 74 61 64 61 74 61 20 69 6e 74 65 72 n metadata inter
24070 66 61 63 65 73 5d 0a 2a 2a 20 66 6f 72 20 74 68 faces].** for th
24080 65 20 73 61 6d 65 20 5b 70 72 65 70 61 72 65 64 e same [prepared
24090 20 73 74 61 74 65 6d 65 6e 74 5d 20 61 6e 64 20 statement] and
240a0 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 0a 2a 2a result column.**
240b0 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d at the same tim
240c0 65 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c e then the resul
240d0 74 73 20 61 72 65 20 75 6e 64 65 66 69 6e 65 64 ts are undefined
240e0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
240f0 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 const char *sqli
24100 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 te3_column_datab
24110 61 73 65 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 ase_name(sqlite3
24120 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c _stmt*,int);.SQL
24130 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f ITE_API const vo
24140 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 id *sqlite3_colu
24150 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 mn_database_name
24160 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 16(sqlite3_stmt*
24170 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 ,int);.SQLITE_AP
24180 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 I const char *sq
24190 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 lite3_column_tab
241a0 6c 65 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f le_name(sqlite3_
241b0 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 stmt*,int);.SQLI
241c0 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 TE_API const voi
241d0 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d d *sqlite3_colum
241e0 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 31 36 28 73 n_table_name16(s
241f0 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 qlite3_stmt*,int
24200 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f );.SQLITE_API co
24210 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 nst char *sqlite
24220 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 3_column_origin_
24230 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d name(sqlite3_stm
24240 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f t*,int);.SQLITE_
24250 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a API const void *
24260 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f sqlite3_column_o
24270 72 69 67 69 6e 5f 6e 61 6d 65 31 36 28 73 71 6c rigin_name16(sql
24280 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b ite3_stmt*,int);
24290 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
242a0 3a 20 44 65 63 6c 61 72 65 64 20 44 61 74 61 74 : Declared Datat
242b0 79 70 65 20 4f 66 20 41 20 51 75 65 72 79 20 52 ype Of A Query R
242c0 65 73 75 6c 74 20 7b 48 31 33 37 36 30 7d 20 3c esult {H13760} <
242d0 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 S10700>.**.** Th
242e0 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 e first paramete
242f0 72 20 69 73 20 61 20 5b 70 72 65 70 61 72 65 64 r is a [prepared
24300 20 73 74 61 74 65 6d 65 6e 74 5d 2e 0a 2a 2a 20 statement]..**
24310 49 66 20 74 68 69 73 20 73 74 61 74 65 6d 65 6e If this statemen
24320 74 20 69 73 20 61 20 5b 53 45 4c 45 43 54 5d 20 t is a [SELECT]
24330 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 74 68 statement and th
24340 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 e Nth column of
24350 74 68 65 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 the.** returned
24360 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20 74 68 result set of th
24370 61 74 20 5b 53 45 4c 45 43 54 5d 20 69 73 20 61 at [SELECT] is a
24380 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 28 6e table column (n
24390 6f 74 20 61 6e 0a 2a 2a 20 65 78 70 72 65 73 73 ot an.** express
243a0 69 6f 6e 20 6f 72 20 73 75 62 71 75 65 72 79 29 ion or subquery)
243b0 20 74 68 65 6e 20 74 68 65 20 64 65 63 6c 61 72 then the declar
243c0 65 64 20 74 79 70 65 20 6f 66 20 74 68 65 20 74 ed type of the t
243d0 61 62 6c 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 69 able.** column i
243e0 73 20 72 65 74 75 72 6e 65 64 2e 20 20 49 66 20 s returned. If
243f0 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 6f the Nth column o
24400 66 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 f the result set
24410 20 69 73 20 61 6e 0a 2a 2a 20 65 78 70 72 65 73 is an.** expres
24420 73 69 6f 6e 20 6f 72 20 73 75 62 71 75 65 72 79 sion or subquery
24430 2c 20 74 68 65 6e 20 61 20 4e 55 4c 4c 20 70 6f , then a NULL po
24440 69 6e 74 65 72 20 69 73 20 72 65 74 75 72 6e 65 inter is returne
24450 64 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e d..** The return
24460 65 64 20 73 74 72 69 6e 67 20 69 73 20 61 6c 77 ed string is alw
24470 61 79 73 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 ays UTF-8 encode
24480 64 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 46 d. {END}.**.** F
24490 6f 72 20 65 78 61 6d 70 6c 65 2c 20 67 69 76 65 or example, give
244a0 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 n the database s
244b0 63 68 65 6d 61 3a 0a 2a 2a 0a 2a 2a 20 43 52 45 chema:.**.** CRE
244c0 41 54 45 20 54 41 42 4c 45 20 74 31 28 63 31 20 ATE TABLE t1(c1
244d0 56 41 52 49 41 4e 54 29 3b 0a 2a 2a 0a 2a 2a 20 VARIANT);.**.**
244e0 61 6e 64 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e and the followin
244f0 67 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 62 g statement to b
24500 65 20 63 6f 6d 70 69 6c 65 64 3a 0a 2a 2a 0a 2a e compiled:.**.*
24510 2a 20 53 45 4c 45 43 54 20 63 31 20 2b 20 31 2c * SELECT c1 + 1,
24520 20 63 31 20 46 52 4f 4d 20 74 31 3b 0a 2a 2a 0a c1 FROM t1;.**.
24530 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 ** this routine
24540 77 6f 75 6c 64 20 72 65 74 75 72 6e 20 74 68 65 would return the
24550 20 73 74 72 69 6e 67 20 22 56 41 52 49 41 4e 54 string "VARIANT
24560 22 20 66 6f 72 20 74 68 65 20 73 65 63 6f 6e 64 " for the second
24570 20 72 65 73 75 6c 74 0a 2a 2a 20 63 6f 6c 75 6d result.** colum
24580 6e 20 28 69 3d 3d 31 29 2c 20 61 6e 64 20 61 20 n (i==1), and a
24590 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 66 6f 72 NULL pointer for
245a0 20 74 68 65 20 66 69 72 73 74 20 72 65 73 75 6c the first resul
245b0 74 20 63 6f 6c 75 6d 6e 20 28 69 3d 3d 30 29 2e t column (i==0).
245c0 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 75 73 .**.** SQLite us
245d0 65 73 20 64 79 6e 61 6d 69 63 20 72 75 6e 2d 74 es dynamic run-t
245e0 69 6d 65 20 74 79 70 69 6e 67 2e 20 20 53 6f 20 ime typing. So
245f0 6a 75 73 74 20 62 65 63 61 75 73 65 20 61 20 63 just because a c
24600 6f 6c 75 6d 6e 0a 2a 2a 20 69 73 20 64 65 63 6c olumn.** is decl
24610 61 72 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20 ared to contain
24620 61 20 70 61 72 74 69 63 75 6c 61 72 20 74 79 70 a particular typ
24630 65 20 64 6f 65 73 20 6e 6f 74 20 6d 65 61 6e 20 e does not mean
24640 74 68 61 74 20 74 68 65 0a 2a 2a 20 64 61 74 61 that the.** data
24650 20 73 74 6f 72 65 64 20 69 6e 20 74 68 61 74 20 stored in that
24660 63 6f 6c 75 6d 6e 20 69 73 20 6f 66 20 74 68 65 column is of the
24670 20 64 65 63 6c 61 72 65 64 20 74 79 70 65 2e 20 declared type.
24680 20 53 51 4c 69 74 65 20 69 73 0a 2a 2a 20 73 74 SQLite is.** st
24690 72 6f 6e 67 6c 79 20 74 79 70 65 64 2c 20 62 75 rongly typed, bu
246a0 74 20 74 68 65 20 74 79 70 69 6e 67 20 69 73 20 t the typing is
246b0 64 79 6e 61 6d 69 63 20 6e 6f 74 20 73 74 61 74 dynamic not stat
246c0 69 63 2e 20 20 54 79 70 65 0a 2a 2a 20 69 73 20 ic. Type.** is
246d0 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
246e0 69 6e 64 69 76 69 64 75 61 6c 20 76 61 6c 75 65 individual value
246f0 73 2c 20 6e 6f 74 20 77 69 74 68 20 74 68 65 20 s, not with the
24700 63 6f 6e 74 61 69 6e 65 72 73 0a 2a 2a 20 75 73 containers.** us
24710 65 64 20 74 6f 20 68 6f 6c 64 20 74 68 6f 73 65 ed to hold those
24720 20 76 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 52 values..**.** R
24730 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 equirements:.**
24740 5b 48 31 33 37 36 31 5d 20 5b 48 31 33 37 36 32 [H13761] [H13762
24750 5d 20 5b 48 31 33 37 36 33 5d 0a 2a 2f 0a 53 51 ] [H13763].*/.SQ
24760 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 LITE_API const c
24770 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c har *sqlite3_col
24780 75 6d 6e 5f 64 65 63 6c 74 79 70 65 28 73 71 6c umn_decltype(sql
24790 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b ite3_stmt*,int);
247a0 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 .SQLITE_API cons
247b0 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f t void *sqlite3_
247c0 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65 31 column_decltype1
247d0 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 6(sqlite3_stmt*,
247e0 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 int);../*.** CAP
247f0 49 33 52 45 46 3a 20 45 76 61 6c 75 61 74 65 20 I3REF: Evaluate
24800 41 6e 20 53 51 4c 20 53 74 61 74 65 6d 65 6e 74 An SQL Statement
24810 20 7b 48 31 33 32 30 30 7d 20 3c 53 31 30 30 30 {H13200} <S1000
24820 30 3e 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 61 0>.**.** After a
24830 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 [prepared state
24840 6d 65 6e 74 5d 20 68 61 73 20 62 65 65 6e 20 70 ment] has been p
24850 72 65 70 61 72 65 64 20 75 73 69 6e 67 20 65 69 repared using ei
24860 74 68 65 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 ther.** [sqlite3
24870 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20 6f _prepare_v2()] o
24880 72 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 r [sqlite3_prepa
24890 72 65 31 36 5f 76 32 28 29 5d 20 6f 72 20 6f 6e re16_v2()] or on
248a0 65 20 6f 66 20 74 68 65 20 6c 65 67 61 63 79 0a e of the legacy.
248b0 2a 2a 20 69 6e 74 65 72 66 61 63 65 73 20 5b 73 ** interfaces [s
248c0 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 qlite3_prepare()
248d0 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 70 72 ] or [sqlite3_pr
248e0 65 70 61 72 65 31 36 28 29 5d 2c 20 74 68 69 73 epare16()], this
248f0 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 6d 75 73 function.** mus
24900 74 20 62 65 20 63 61 6c 6c 65 64 20 6f 6e 65 20 t be called one
24910 6f 72 20 6d 6f 72 65 20 74 69 6d 65 73 20 74 6f or more times to
24920 20 65 76 61 6c 75 61 74 65 20 74 68 65 20 73 74 evaluate the st
24930 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 atement..**.** T
24940 68 65 20 64 65 74 61 69 6c 73 20 6f 66 20 74 68 he details of th
24950 65 20 62 65 68 61 76 69 6f 72 20 6f 66 20 74 68 e behavior of th
24960 65 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 e sqlite3_step()
24970 20 69 6e 74 65 72 66 61 63 65 20 64 65 70 65 6e interface depen
24980 64 0a 2a 2a 20 6f 6e 20 77 68 65 74 68 65 72 20 d.** on whether
24990 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 77 61 the statement wa
249a0 73 20 70 72 65 70 61 72 65 64 20 75 73 69 6e 67 s prepared using
249b0 20 74 68 65 20 6e 65 77 65 72 20 22 76 32 22 20 the newer "v2"
249c0 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20 5b 73 71 interface.** [sq
249d0 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 lite3_prepare_v2
249e0 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 ()] and [sqlite3
249f0 5f 70 72 65 70 61 72 65 31 36 5f 76 32 28 29 5d _prepare16_v2()]
24a00 20 6f 72 20 74 68 65 20 6f 6c 64 65 72 20 6c 65 or the older le
24a10 67 61 63 79 0a 2a 2a 20 69 6e 74 65 72 66 61 63 gacy.** interfac
24a20 65 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 e [sqlite3_prepa
24a30 72 65 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 re()] and [sqlit
24a40 65 33 5f 70 72 65 70 61 72 65 31 36 28 29 5d 2e e3_prepare16()].
24a50 20 20 54 68 65 20 75 73 65 20 6f 66 20 74 68 65 The use of the
24a60 0a 2a 2a 20 6e 65 77 20 22 76 32 22 20 69 6e 74 .** new "v2" int
24a70 65 72 66 61 63 65 20 69 73 20 72 65 63 6f 6d 6d erface is recomm
24a80 65 6e 64 65 64 20 66 6f 72 20 6e 65 77 20 61 70 ended for new ap
24a90 70 6c 69 63 61 74 69 6f 6e 73 20 62 75 74 20 74 plications but t
24aa0 68 65 20 6c 65 67 61 63 79 0a 2a 2a 20 69 6e 74 he legacy.** int
24ab0 65 72 66 61 63 65 20 77 69 6c 6c 20 63 6f 6e 74 erface will cont
24ac0 69 6e 75 65 20 74 6f 20 62 65 20 73 75 70 70 6f inue to be suppo
24ad0 72 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 rted..**.** In t
24ae0 68 65 20 6c 65 67 61 63 79 20 69 6e 74 65 72 66 he legacy interf
24af0 61 63 65 2c 20 74 68 65 20 72 65 74 75 72 6e 20 ace, the return
24b00 76 61 6c 75 65 20 77 69 6c 6c 20 62 65 20 65 69 value will be ei
24b10 74 68 65 72 20 5b 53 51 4c 49 54 45 5f 42 55 53 ther [SQLITE_BUS
24b20 59 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 44 Y],.** [SQLITE_D
24b30 4f 4e 45 5d 2c 20 5b 53 51 4c 49 54 45 5f 52 4f ONE], [SQLITE_RO
24b40 57 5d 2c 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f W], [SQLITE_ERRO
24b50 52 5d 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f 4d R], or [SQLITE_M
24b60 49 53 55 53 45 5d 2e 0a 2a 2a 20 57 69 74 68 20 ISUSE]..** With
24b70 74 68 65 20 22 76 32 22 20 69 6e 74 65 72 66 61 the "v2" interfa
24b80 63 65 2c 20 61 6e 79 20 6f 66 20 74 68 65 20 6f ce, any of the o
24b90 74 68 65 72 20 5b 72 65 73 75 6c 74 20 63 6f 64 ther [result cod
24ba0 65 73 5d 20 6f 72 0a 2a 2a 20 5b 65 78 74 65 6e es] or.** [exten
24bb0 64 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73 ded result codes
24bc0 5d 20 6d 69 67 68 74 20 62 65 20 72 65 74 75 72 ] might be retur
24bd0 6e 65 64 20 61 73 20 77 65 6c 6c 2e 0a 2a 2a 0a ned as well..**.
24be0 2a 2a 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d ** [SQLITE_BUSY]
24bf0 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 means that the
24c00 64 61 74 61 62 61 73 65 20 65 6e 67 69 6e 65 20 database engine
24c10 77 61 73 20 75 6e 61 62 6c 65 20 74 6f 20 61 63 was unable to ac
24c20 71 75 69 72 65 20 74 68 65 0a 2a 2a 20 64 61 74 quire the.** dat
24c30 61 62 61 73 65 20 6c 6f 63 6b 73 20 69 74 20 6e abase locks it n
24c40 65 65 64 73 20 74 6f 20 64 6f 20 69 74 73 20 6a eeds to do its j
24c50 6f 62 2e 20 20 49 66 20 74 68 65 20 73 74 61 74 ob. If the stat
24c60 65 6d 65 6e 74 20 69 73 20 61 20 5b 43 4f 4d 4d ement is a [COMM
24c70 49 54 5d 0a 2a 2a 20 6f 72 20 6f 63 63 75 72 73 IT].** or occurs
24c80 20 6f 75 74 73 69 64 65 20 6f 66 20 61 6e 20 65 outside of an e
24c90 78 70 6c 69 63 69 74 20 74 72 61 6e 73 61 63 74 xplicit transact
24ca0 69 6f 6e 2c 20 74 68 65 6e 20 79 6f 75 20 63 61 ion, then you ca
24cb0 6e 20 72 65 74 72 79 20 74 68 65 0a 2a 2a 20 73 n retry the.** s
24cc0 74 61 74 65 6d 65 6e 74 2e 20 20 49 66 20 74 68 tatement. If th
24cd0 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 6e e statement is n
24ce0 6f 74 20 61 20 5b 43 4f 4d 4d 49 54 5d 20 61 6e ot a [COMMIT] an
24cf0 64 20 6f 63 63 75 72 73 20 77 69 74 68 69 6e 20 d occurs within
24d00 61 0a 2a 2a 20 65 78 70 6c 69 63 69 74 20 74 72 a.** explicit tr
24d10 61 6e 73 61 63 74 69 6f 6e 20 74 68 65 6e 20 79 ansaction then y
24d20 6f 75 20 73 68 6f 75 6c 64 20 72 6f 6c 6c 62 61 ou should rollba
24d30 63 6b 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 ck the transacti
24d40 6f 6e 20 62 65 66 6f 72 65 0a 2a 2a 20 63 6f 6e on before.** con
24d50 74 69 6e 75 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 5b tinuing..**.** [
24d60 53 51 4c 49 54 45 5f 44 4f 4e 45 5d 20 6d 65 61 SQLITE_DONE] mea
24d70 6e 73 20 74 68 61 74 20 74 68 65 20 73 74 61 74 ns that the stat
24d80 65 6d 65 6e 74 20 68 61 73 20 66 69 6e 69 73 68 ement has finish
24d90 65 64 20 65 78 65 63 75 74 69 6e 67 0a 2a 2a 20 ed executing.**
24da0 73 75 63 63 65 73 73 66 75 6c 6c 79 2e 20 20 73 successfully. s
24db0 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 73 68 qlite3_step() sh
24dc0 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63 61 6c 6c ould not be call
24dd0 65 64 20 61 67 61 69 6e 20 6f 6e 20 74 68 69 73 ed again on this
24de0 20 76 69 72 74 75 61 6c 0a 2a 2a 20 6d 61 63 68 virtual.** mach
24df0 69 6e 65 20 77 69 74 68 6f 75 74 20 66 69 72 73 ine without firs
24e00 74 20 63 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 t calling [sqlit
24e10 65 33 5f 72 65 73 65 74 28 29 5d 20 74 6f 20 72 e3_reset()] to r
24e20 65 73 65 74 20 74 68 65 20 76 69 72 74 75 61 6c eset the virtual
24e30 0a 2a 2a 20 6d 61 63 68 69 6e 65 20 62 61 63 6b .** machine back
24e40 20 74 6f 20 69 74 73 20 69 6e 69 74 69 61 6c 20 to its initial
24e50 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 state..**.** If
24e60 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e the SQL statemen
24e70 74 20 62 65 69 6e 67 20 65 78 65 63 75 74 65 64 t being executed
24e80 20 72 65 74 75 72 6e 73 20 61 6e 79 20 64 61 74 returns any dat
24e90 61 2c 20 74 68 65 6e 20 5b 53 51 4c 49 54 45 5f a, then [SQLITE_
24ea0 52 4f 57 5d 0a 2a 2a 20 69 73 20 72 65 74 75 72 ROW].** is retur
24eb0 6e 65 64 20 65 61 63 68 20 74 69 6d 65 20 61 20 ned each time a
24ec0 6e 65 77 20 72 6f 77 20 6f 66 20 64 61 74 61 20 new row of data
24ed0 69 73 20 72 65 61 64 79 20 66 6f 72 20 70 72 6f is ready for pro
24ee0 63 65 73 73 69 6e 67 20 62 79 20 74 68 65 0a 2a cessing by the.*
24ef0 2a 20 63 61 6c 6c 65 72 2e 20 54 68 65 20 76 61 * caller. The va
24f00 6c 75 65 73 20 6d 61 79 20 62 65 20 61 63 63 65 lues may be acce
24f10 73 73 65 64 20 75 73 69 6e 67 20 74 68 65 20 5b ssed using the [
24f20 63 6f 6c 75 6d 6e 20 61 63 63 65 73 73 20 66 75 column access fu
24f30 6e 63 74 69 6f 6e 73 5d 2e 0a 2a 2a 20 73 71 6c nctions]..** sql
24f40 69 74 65 33 5f 73 74 65 70 28 29 20 69 73 20 63 ite3_step() is c
24f50 61 6c 6c 65 64 20 61 67 61 69 6e 20 74 6f 20 72 alled again to r
24f60 65 74 72 69 65 76 65 20 74 68 65 20 6e 65 78 74 etrieve the next
24f70 20 72 6f 77 20 6f 66 20 64 61 74 61 2e 0a 2a 2a row of data..**
24f80 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f .** [SQLITE_ERRO
24f90 52 5d 20 6d 65 61 6e 73 20 74 68 61 74 20 61 20 R] means that a
24fa0 72 75 6e 2d 74 69 6d 65 20 65 72 72 6f 72 20 28 run-time error (
24fb0 73 75 63 68 20 61 73 20 61 20 63 6f 6e 73 74 72 such as a constr
24fc0 61 69 6e 74 0a 2a 2a 20 76 69 6f 6c 61 74 69 6f aint.** violatio
24fd0 6e 29 20 68 61 73 20 6f 63 63 75 72 72 65 64 2e n) has occurred.
24fe0 20 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 sqlite3_step()
24ff0 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63 should not be c
25000 61 6c 6c 65 64 20 61 67 61 69 6e 20 6f 6e 0a 2a alled again on.*
25010 2a 20 74 68 65 20 56 4d 2e 20 4d 6f 72 65 20 69 * the VM. More i
25020 6e 66 6f 72 6d 61 74 69 6f 6e 20 6d 61 79 20 62 nformation may b
25030 65 20 66 6f 75 6e 64 20 62 79 20 63 61 6c 6c 69 e found by calli
25040 6e 67 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d ng [sqlite3_errm
25050 73 67 28 29 5d 2e 0a 2a 2a 20 57 69 74 68 20 74 sg()]..** With t
25060 68 65 20 6c 65 67 61 63 79 20 69 6e 74 65 72 66 he legacy interf
25070 61 63 65 2c 20 61 20 6d 6f 72 65 20 73 70 65 63 ace, a more spec
25080 69 66 69 63 20 65 72 72 6f 72 20 63 6f 64 65 20 ific error code
25090 28 66 6f 72 20 65 78 61 6d 70 6c 65 2c 0a 2a 2a (for example,.**
250a0 20 5b 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 [SQLITE_INTERRU
250b0 50 54 5d 2c 20 5b 53 51 4c 49 54 45 5f 53 43 48 PT], [SQLITE_SCH
250c0 45 4d 41 5d 2c 20 5b 53 51 4c 49 54 45 5f 43 4f EMA], [SQLITE_CO
250d0 52 52 55 50 54 5d 2c 20 61 6e 64 20 73 6f 20 66 RRUPT], and so f
250e0 6f 72 74 68 29 0a 2a 2a 20 63 61 6e 20 62 65 20 orth).** can be
250f0 6f 62 74 61 69 6e 65 64 20 62 79 20 63 61 6c 6c obtained by call
25100 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 ing [sqlite3_res
25110 65 74 28 29 5d 20 6f 6e 20 74 68 65 0a 2a 2a 20 et()] on the.**
25120 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d [prepared statem
25130 65 6e 74 5d 2e 20 20 49 6e 20 74 68 65 20 22 76 ent]. In the "v
25140 32 22 20 69 6e 74 65 72 66 61 63 65 2c 0a 2a 2a 2" interface,.**
25150 20 74 68 65 20 6d 6f 72 65 20 73 70 65 63 69 66 the more specif
25160 69 63 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 ic error code is
25170 20 72 65 74 75 72 6e 65 64 20 64 69 72 65 63 74 returned direct
25180 6c 79 20 62 79 20 73 71 6c 69 74 65 33 5f 73 74 ly by sqlite3_st
25190 65 70 28 29 2e 0a 2a 2a 0a 2a 2a 20 5b 53 51 4c ep()..**.** [SQL
251a0 49 54 45 5f 4d 49 53 55 53 45 5d 20 6d 65 61 6e ITE_MISUSE] mean
251b0 73 20 74 68 61 74 20 74 68 65 20 74 68 69 73 20 s that the this
251c0 72 6f 75 74 69 6e 65 20 77 61 73 20 63 61 6c 6c routine was call
251d0 65 64 20 69 6e 61 70 70 72 6f 70 72 69 61 74 65 ed inappropriate
251e0 6c 79 2e 0a 2a 2a 20 50 65 72 68 61 70 73 20 69 ly..** Perhaps i
251f0 74 20 77 61 73 20 63 61 6c 6c 65 64 20 6f 6e 20 t was called on
25200 61 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 a [prepared stat
25210 65 6d 65 6e 74 5d 20 74 68 61 74 20 68 61 73 0a ement] that has.
25220 2a 2a 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 ** already been
25230 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a [sqlite3_finaliz
25240 65 20 7c 20 66 69 6e 61 6c 69 7a 65 64 5d 20 6f e | finalized] o
25250 72 20 6f 6e 20 6f 6e 65 20 74 68 61 74 20 68 61 r on one that ha
25260 64 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 d.** previously
25270 72 65 74 75 72 6e 65 64 20 5b 53 51 4c 49 54 45 returned [SQLITE
25280 5f 45 52 52 4f 52 5d 20 6f 72 20 5b 53 51 4c 49 _ERROR] or [SQLI
25290 54 45 5f 44 4f 4e 45 5d 2e 20 20 4f 72 20 69 74 TE_DONE]. Or it
252a0 20 63 6f 75 6c 64 0a 2a 2a 20 62 65 20 74 68 65 could.** be the
252b0 20 63 61 73 65 20 74 68 61 74 20 74 68 65 20 73 case that the s
252c0 61 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e ame database con
252d0 6e 65 63 74 69 6f 6e 20 69 73 20 62 65 69 6e 67 nection is being
252e0 20 75 73 65 64 20 62 79 20 74 77 6f 20 6f 72 0a used by two or.
252f0 2a 2a 20 6d 6f 72 65 20 74 68 72 65 61 64 73 20 ** more threads
25300 61 74 20 74 68 65 20 73 61 6d 65 20 6d 6f 6d 65 at the same mome
25310 6e 74 20 69 6e 20 74 69 6d 65 2e 0a 2a 2a 0a 2a nt in time..**.*
25320 2a 20 3c 62 3e 47 6f 6f 66 79 20 49 6e 74 65 72 * <b>Goofy Inter
25330 66 61 63 65 20 41 6c 65 72 74 3a 3c 2f 62 3e 20 face Alert:</b>
25340 49 6e 20 74 68 65 20 6c 65 67 61 63 79 20 69 6e In the legacy in
25350 74 65 72 66 61 63 65 2c 20 74 68 65 20 73 71 6c terface, the sql
25360 69 74 65 33 5f 73 74 65 70 28 29 0a 2a 2a 20 41 ite3_step().** A
25370 50 49 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e PI always return
25380 73 20 61 20 67 65 6e 65 72 69 63 20 65 72 72 6f s a generic erro
25390 72 20 63 6f 64 65 2c 20 5b 53 51 4c 49 54 45 5f r code, [SQLITE_
253a0 45 52 52 4f 52 5d 2c 20 66 6f 6c 6c 6f 77 69 6e ERROR], followin
253b0 67 20 61 6e 79 0a 2a 2a 20 65 72 72 6f 72 20 6f g any.** error o
253c0 74 68 65 72 20 74 68 61 6e 20 5b 53 51 4c 49 54 ther than [SQLIT
253d0 45 5f 42 55 53 59 5d 20 61 6e 64 20 5b 53 51 4c E_BUSY] and [SQL
253e0 49 54 45 5f 4d 49 53 55 53 45 5d 2e 20 20 59 6f ITE_MISUSE]. Yo
253f0 75 20 6d 75 73 74 20 63 61 6c 6c 0a 2a 2a 20 5b u must call.** [
25400 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d sqlite3_reset()]
25410 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e or [sqlite3_fin
25420 61 6c 69 7a 65 28 29 5d 20 69 6e 20 6f 72 64 65 alize()] in orde
25430 72 20 74 6f 20 66 69 6e 64 20 6f 6e 65 20 6f 66 r to find one of
25440 20 74 68 65 0a 2a 2a 20 73 70 65 63 69 66 69 63 the.** specific
25450 20 5b 65 72 72 6f 72 20 63 6f 64 65 73 5d 20 74 [error codes] t
25460 68 61 74 20 62 65 74 74 65 72 20 64 65 73 63 72 hat better descr
25470 69 62 65 73 20 74 68 65 20 65 72 72 6f 72 2e 0a ibes the error..
25480 2a 2a 20 57 65 20 61 64 6d 69 74 20 74 68 61 74 ** We admit that
25490 20 74 68 69 73 20 69 73 20 61 20 67 6f 6f 66 79 this is a goofy
254a0 20 64 65 73 69 67 6e 2e 20 20 54 68 65 20 70 72 design. The pr
254b0 6f 62 6c 65 6d 20 68 61 73 20 62 65 65 6e 20 66 oblem has been f
254c0 69 78 65 64 0a 2a 2a 20 77 69 74 68 20 74 68 65 ixed.** with the
254d0 20 22 76 32 22 20 69 6e 74 65 72 66 61 63 65 2e "v2" interface.
254e0 20 20 49 66 20 79 6f 75 20 70 72 65 70 61 72 65 If you prepare
254f0 20 61 6c 6c 20 6f 66 20 79 6f 75 72 20 53 51 4c all of your SQL
25500 20 73 74 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 75 statements.** u
25510 73 69 6e 67 20 65 69 74 68 65 72 20 5b 73 71 6c sing either [sql
25520 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 ite3_prepare_v2(
25530 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 70 )] or [sqlite3_p
25540 72 65 70 61 72 65 31 36 5f 76 32 28 29 5d 20 69 repare16_v2()] i
25550 6e 73 74 65 61 64 0a 2a 2a 20 6f 66 20 74 68 65 nstead.** of the
25560 20 6c 65 67 61 63 79 20 5b 73 71 6c 69 74 65 33 legacy [sqlite3
25570 5f 70 72 65 70 61 72 65 28 29 5d 20 61 6e 64 20 _prepare()] and
25580 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 [sqlite3_prepare
25590 31 36 28 29 5d 20 69 6e 74 65 72 66 61 63 65 73 16()] interfaces
255a0 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 6d 6f ,.** then the mo
255b0 72 65 20 73 70 65 63 69 66 69 63 20 5b 65 72 72 re specific [err
255c0 6f 72 20 63 6f 64 65 73 5d 20 61 72 65 20 72 65 or codes] are re
255d0 74 75 72 6e 65 64 20 64 69 72 65 63 74 6c 79 0a turned directly.
255e0 2a 2a 20 62 79 20 73 71 6c 69 74 65 33 5f 73 74 ** by sqlite3_st
255f0 65 70 28 29 2e 20 20 54 68 65 20 75 73 65 20 6f ep(). The use o
25600 66 20 74 68 65 20 22 76 32 22 20 69 6e 74 65 72 f the "v2" inter
25610 66 61 63 65 20 69 73 20 72 65 63 6f 6d 6d 65 6e face is recommen
25620 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 ded..**.** Requi
25630 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 rements:.** [H13
25640 32 30 32 5d 20 5b 48 31 35 33 30 34 5d 20 5b 48 202] [H15304] [H
25650 31 35 33 30 36 5d 20 5b 48 31 35 33 30 38 5d 20 15306] [H15308]
25660 5b 48 31 35 33 31 30 5d 0a 2a 2f 0a 53 51 4c 49 [H15310].*/.SQLI
25670 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
25680 65 33 5f 73 74 65 70 28 73 71 6c 69 74 65 33 5f e3_step(sqlite3_
25690 73 74 6d 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 stmt*);../*.** C
256a0 41 50 49 33 52 45 46 3a 20 4e 75 6d 62 65 72 20 API3REF: Number
256b0 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 61 20 of columns in a
256c0 72 65 73 75 6c 74 20 73 65 74 20 7b 48 31 33 37 result set {H137
256d0 37 30 7d 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 0a 70} <S10700>.**.
256e0 2a 2a 20 52 65 74 75 72 6e 73 20 74 68 65 20 6e ** Returns the n
256f0 75 6d 62 65 72 20 6f 66 20 76 61 6c 75 65 73 20 umber of values
25700 69 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 72 in the current r
25710 6f 77 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 ow of the result
25720 20 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 set..**.** Requ
25730 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 irements:.** [H1
25740 33 37 37 31 5d 20 5b 48 31 33 37 37 32 5d 0a 2a 3771] [H13772].*
25750 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
25760 20 73 71 6c 69 74 65 33 5f 64 61 74 61 5f 63 6f sqlite3_data_co
25770 75 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 unt(sqlite3_stmt
25780 20 2a 70 53 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a *pStmt);../*.**
25790 20 43 41 50 49 33 52 45 46 3a 20 46 75 6e 64 61 CAPI3REF: Funda
257a0 6d 65 6e 74 61 6c 20 44 61 74 61 74 79 70 65 73 mental Datatypes
257b0 20 7b 48 31 30 32 36 35 7d 20 3c 53 31 30 31 31 {H10265} <S1011
257c0 30 3e 3c 53 31 30 31 32 30 3e 0a 2a 2a 20 4b 45 0><S10120>.** KE
257d0 59 57 4f 52 44 53 3a 20 53 51 4c 49 54 45 5f 54 YWORDS: SQLITE_T
257e0 45 58 54 0a 2a 2a 0a 2a 2a 20 7b 48 31 30 32 36 EXT.**.** {H1026
257f0 36 7d 20 45 76 65 72 79 20 76 61 6c 75 65 20 69 6} Every value i
25800 6e 20 53 51 4c 69 74 65 20 68 61 73 20 6f 6e 65 n SQLite has one
25810 20 6f 66 20 66 69 76 65 20 66 75 6e 64 61 6d 65 of five fundame
25820 6e 74 61 6c 20 64 61 74 61 74 79 70 65 73 3a 0a ntal datatypes:.
25830 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c **.** <ul>.** <l
25840 69 3e 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64 i> 64-bit signed
25850 20 69 6e 74 65 67 65 72 0a 2a 2a 20 3c 6c 69 3e integer.** <li>
25860 20 36 34 2d 62 69 74 20 49 45 45 45 20 66 6c 6f 64-bit IEEE flo
25870 61 74 69 6e 67 20 70 6f 69 6e 74 20 6e 75 6d 62 ating point numb
25880 65 72 0a 2a 2a 20 3c 6c 69 3e 20 73 74 72 69 6e er.** <li> strin
25890 67 0a 2a 2a 20 3c 6c 69 3e 20 42 4c 4f 42 0a 2a g.** <li> BLOB.*
258a0 2a 20 3c 6c 69 3e 20 4e 55 4c 4c 0a 2a 2a 20 3c * <li> NULL.** <
258b0 2f 75 6c 3e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a /ul> {END}.**.**
258c0 20 54 68 65 73 65 20 63 6f 6e 73 74 61 6e 74 73 These constants
258d0 20 61 72 65 20 63 6f 64 65 73 20 66 6f 72 20 65 are codes for e
258e0 61 63 68 20 6f 66 20 74 68 6f 73 65 20 74 79 70 ach of those typ
258f0 65 73 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 es..**.** Note t
25900 68 61 74 20 74 68 65 20 53 51 4c 49 54 45 5f 54 hat the SQLITE_T
25910 45 58 54 20 63 6f 6e 73 74 61 6e 74 20 77 61 73 EXT constant was
25920 20 61 6c 73 6f 20 75 73 65 64 20 69 6e 20 53 51 also used in SQ
25930 4c 69 74 65 20 76 65 72 73 69 6f 6e 20 32 0a 2a Lite version 2.*
25940 2a 20 66 6f 72 20 61 20 63 6f 6d 70 6c 65 74 65 * for a complete
25950 6c 79 20 64 69 66 66 65 72 65 6e 74 20 6d 65 61 ly different mea
25960 6e 69 6e 67 2e 20 20 53 6f 66 74 77 61 72 65 20 ning. Software
25970 74 68 61 74 20 6c 69 6e 6b 73 20 61 67 61 69 6e that links again
25980 73 74 20 62 6f 74 68 0a 2a 2a 20 53 51 4c 69 74 st both.** SQLit
25990 65 20 76 65 72 73 69 6f 6e 20 32 20 61 6e 64 20 e version 2 and
259a0 53 51 4c 69 74 65 20 76 65 72 73 69 6f 6e 20 33 SQLite version 3
259b0 20 73 68 6f 75 6c 64 20 75 73 65 20 53 51 4c 49 should use SQLI
259c0 54 45 33 5f 54 45 58 54 2c 20 6e 6f 74 0a 2a 2a TE3_TEXT, not.**
259d0 20 53 51 4c 49 54 45 5f 54 45 58 54 2e 0a 2a 2f SQLITE_TEXT..*/
259e0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
259f0 49 4e 54 45 47 45 52 20 20 31 0a 23 64 65 66 69 INTEGER 1.#defi
25a00 6e 65 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 20 ne SQLITE_FLOAT
25a10 20 20 20 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 2.#define SQL
25a20 49 54 45 5f 42 4c 4f 42 20 20 20 20 20 34 0a 23 ITE_BLOB 4.#
25a30 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 55 define SQLITE_NU
25a40 4c 4c 20 20 20 20 20 35 0a 23 69 66 64 65 66 20 LL 5.#ifdef
25a50 53 51 4c 49 54 45 5f 54 45 58 54 0a 23 20 75 6e SQLITE_TEXT.# un
25a60 64 65 66 20 53 51 4c 49 54 45 5f 54 45 58 54 0a def SQLITE_TEXT.
25a70 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 53 #else.# define S
25a80 51 4c 49 54 45 5f 54 45 58 54 20 20 20 20 20 33 QLITE_TEXT 3
25a90 0a 23 65 6e 64 69 66 0a 23 64 65 66 69 6e 65 20 .#endif.#define
25aa0 53 51 4c 49 54 45 33 5f 54 45 58 54 20 20 20 20 SQLITE3_TEXT
25ab0 20 33 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 3../*.** CAPI3R
25ac0 45 46 3a 20 52 65 73 75 6c 74 20 56 61 6c 75 65 EF: Result Value
25ad0 73 20 46 72 6f 6d 20 41 20 51 75 65 72 79 20 7b s From A Query {
25ae0 48 31 33 38 30 30 7d 20 3c 53 31 30 37 30 30 3e H13800} <S10700>
25af0 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 63 .** KEYWORDS: {c
25b00 6f 6c 75 6d 6e 20 61 63 63 65 73 73 20 66 75 6e olumn access fun
25b10 63 74 69 6f 6e 73 7d 0a 2a 2a 0a 2a 2a 20 54 68 ctions}.**.** Th
25b20 65 73 65 20 72 6f 75 74 69 6e 65 73 20 66 6f 72 ese routines for
25b30 6d 20 74 68 65 20 22 72 65 73 75 6c 74 20 73 65 m the "result se
25b40 74 20 71 75 65 72 79 22 20 69 6e 74 65 72 66 61 t query" interfa
25b50 63 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 ce..**.** These
25b60 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20 routines return
25b70 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 information abou
25b80 74 20 61 20 73 69 6e 67 6c 65 20 63 6f 6c 75 6d t a single colum
25b90 6e 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 n of the current
25ba0 0a 2a 2a 20 72 65 73 75 6c 74 20 72 6f 77 20 6f .** result row o
25bb0 66 20 61 20 71 75 65 72 79 2e 20 20 49 6e 20 65 f a query. In e
25bc0 76 65 72 79 20 63 61 73 65 20 74 68 65 20 66 69 very case the fi
25bd0 72 73 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20 rst argument is
25be0 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 a pointer.** to
25bf0 74 68 65 20 5b 70 72 65 70 61 72 65 64 20 73 74 the [prepared st
25c00 61 74 65 6d 65 6e 74 5d 20 74 68 61 74 20 69 73 atement] that is
25c10 20 62 65 69 6e 67 20 65 76 61 6c 75 61 74 65 64 being evaluated
25c20 20 28 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 (the [sqlite3_s
25c30 74 6d 74 2a 5d 0a 2a 2a 20 74 68 61 74 20 77 61 tmt*].** that wa
25c40 73 20 72 65 74 75 72 6e 65 64 20 66 72 6f 6d 20 s returned from
25c50 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 [sqlite3_prepare
25c60 5f 76 32 28 29 5d 20 6f 72 20 6f 6e 65 20 6f 66 _v2()] or one of
25c70 20 69 74 73 20 76 61 72 69 61 6e 74 73 29 0a 2a its variants).*
25c80 2a 20 61 6e 64 20 74 68 65 20 73 65 63 6f 6e 64 * and the second
25c90 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 argument is the
25ca0 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 63 6f index of the co
25cb0 6c 75 6d 6e 20 66 6f 72 20 77 68 69 63 68 20 69 lumn for which i
25cc0 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 73 68 nformation.** sh
25cd0 6f 75 6c 64 20 62 65 20 72 65 74 75 72 6e 65 64 ould be returned
25ce0 2e 20 20 54 68 65 20 6c 65 66 74 6d 6f 73 74 20 . The leftmost
25cf0 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 72 65 column of the re
25d00 73 75 6c 74 20 73 65 74 20 68 61 73 20 74 68 65 sult set has the
25d10 20 69 6e 64 65 78 20 30 2e 0a 2a 2a 0a 2a 2a 20 index 0..**.**
25d20 49 66 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 If the SQL state
25d30 6d 65 6e 74 20 64 6f 65 73 20 6e 6f 74 20 63 75 ment does not cu
25d40 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 20 74 6f rrently point to
25d50 20 61 20 76 61 6c 69 64 20 72 6f 77 2c 20 6f 72 a valid row, or
25d60 20 69 66 20 74 68 65 0a 2a 2a 20 63 6f 6c 75 6d if the.** colum
25d70 6e 20 69 6e 64 65 78 20 69 73 20 6f 75 74 20 6f n index is out o
25d80 66 20 72 61 6e 67 65 2c 20 74 68 65 20 72 65 73 f range, the res
25d90 75 6c 74 20 69 73 20 75 6e 64 65 66 69 6e 65 64 ult is undefined
25da0 2e 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 ..** These routi
25db0 6e 65 73 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 nes may only be
25dc0 63 61 6c 6c 65 64 20 77 68 65 6e 20 74 68 65 20 called when the
25dd0 6d 6f 73 74 20 72 65 63 65 6e 74 20 63 61 6c 6c most recent call
25de0 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f to.** [sqlite3_
25df0 73 74 65 70 28 29 5d 20 68 61 73 20 72 65 74 75 step()] has retu
25e00 72 6e 65 64 20 5b 53 51 4c 49 54 45 5f 52 4f 57 rned [SQLITE_ROW
25e10 5d 20 61 6e 64 20 6e 65 69 74 68 65 72 0a 2a 2a ] and neither.**
25e20 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 [sqlite3_reset(
25e30 29 5d 20 6e 6f 72 20 5b 73 71 6c 69 74 65 33 5f )] nor [sqlite3_
25e40 66 69 6e 61 6c 69 7a 65 28 29 5d 20 68 61 76 65 finalize()] have
25e50 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 73 75 62 been called sub
25e60 73 65 71 75 65 6e 74 6c 79 2e 0a 2a 2a 20 49 66 sequently..** If
25e70 20 61 6e 79 20 6f 66 20 74 68 65 73 65 20 72 6f any of these ro
25e80 75 74 69 6e 65 73 20 61 72 65 20 63 61 6c 6c 65 utines are calle
25e90 64 20 61 66 74 65 72 20 5b 73 71 6c 69 74 65 33 d after [sqlite3
25ea0 5f 72 65 73 65 74 28 29 5d 20 6f 72 0a 2a 2a 20 _reset()] or.**
25eb0 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a [sqlite3_finaliz
25ec0 65 28 29 5d 20 6f 72 20 61 66 74 65 72 20 5b 73 e()] or after [s
25ed0 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 68 qlite3_step()] h
25ee0 61 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 73 as returned.** s
25ef0 6f 6d 65 74 68 69 6e 67 20 6f 74 68 65 72 20 74 omething other t
25f00 68 61 6e 20 5b 53 51 4c 49 54 45 5f 52 4f 57 5d han [SQLITE_ROW]
25f10 2c 20 74 68 65 20 72 65 73 75 6c 74 73 20 61 72 , the results ar
25f20 65 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 20 e undefined..**
25f30 49 66 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 If [sqlite3_step
25f40 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f ()] or [sqlite3_
25f50 72 65 73 65 74 28 29 5d 20 6f 72 20 5b 73 71 6c reset()] or [sql
25f60 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d ite3_finalize()]
25f70 0a 2a 2a 20 61 72 65 20 63 61 6c 6c 65 64 20 66 .** are called f
25f80 72 6f 6d 20 61 20 64 69 66 66 65 72 65 6e 74 20 rom a different
25f90 74 68 72 65 61 64 20 77 68 69 6c 65 20 61 6e 79 thread while any
25fa0 20 6f 66 20 74 68 65 73 65 20 72 6f 75 74 69 6e of these routin
25fb0 65 73 0a 2a 2a 20 61 72 65 20 70 65 6e 64 69 6e es.** are pendin
25fc0 67 2c 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 g, then the resu
25fd0 6c 74 73 20 61 72 65 20 75 6e 64 65 66 69 6e 65 lts are undefine
25fe0 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c d..**.** The sql
25ff0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 ite3_column_type
26000 28 29 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 () routine retur
26010 6e 73 20 74 68 65 0a 2a 2a 20 5b 53 51 4c 49 54 ns the.** [SQLIT
26020 45 5f 49 4e 54 45 47 45 52 20 7c 20 64 61 74 61 E_INTEGER | data
26030 74 79 70 65 20 63 6f 64 65 5d 20 66 6f 72 20 74 type code] for t
26040 68 65 20 69 6e 69 74 69 61 6c 20 64 61 74 61 20 he initial data
26050 74 79 70 65 0a 2a 2a 20 6f 66 20 74 68 65 20 72 type.** of the r
26060 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 2e 20 20 54 esult column. T
26070 68 65 20 72 65 74 75 72 6e 65 64 20 76 61 6c 75 he returned valu
26080 65 20 69 73 20 6f 6e 65 20 6f 66 20 5b 53 51 4c e is one of [SQL
26090 49 54 45 5f 49 4e 54 45 47 45 52 5d 2c 0a 2a 2a ITE_INTEGER],.**
260a0 20 5b 53 51 4c 49 54 45 5f 46 4c 4f 41 54 5d 2c [SQLITE_FLOAT],
260b0 20 5b 53 51 4c 49 54 45 5f 54 45 58 54 5d 2c 20 [SQLITE_TEXT],
260c0 5b 53 51 4c 49 54 45 5f 42 4c 4f 42 5d 2c 20 6f [SQLITE_BLOB], o
260d0 72 20 5b 53 51 4c 49 54 45 5f 4e 55 4c 4c 5d 2e r [SQLITE_NULL].
260e0 20 20 54 68 65 20 76 61 6c 75 65 0a 2a 2a 20 72 The value.** r
260f0 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 eturned by sqlit
26100 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 29 e3_column_type()
26110 20 69 73 20 6f 6e 6c 79 20 6d 65 61 6e 69 6e 67 is only meaning
26120 66 75 6c 20 69 66 20 6e 6f 20 74 79 70 65 0a 2a ful if no type.*
26130 2a 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 68 61 * conversions ha
26140 76 65 20 6f 63 63 75 72 72 65 64 20 61 73 20 64 ve occurred as d
26150 65 73 63 72 69 62 65 64 20 62 65 6c 6f 77 2e 20 escribed below.
26160 20 41 66 74 65 72 20 61 20 74 79 70 65 20 63 6f After a type co
26170 6e 76 65 72 73 69 6f 6e 2c 0a 2a 2a 20 74 68 65 nversion,.** the
26180 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 value returned
26190 62 79 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d by sqlite3_colum
261a0 6e 5f 74 79 70 65 28 29 20 69 73 20 75 6e 64 65 n_type() is unde
261b0 66 69 6e 65 64 2e 20 20 46 75 74 75 72 65 0a 2a fined. Future.*
261c0 2a 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 * versions of SQ
261d0 4c 69 74 65 20 6d 61 79 20 63 68 61 6e 67 65 20 Lite may change
261e0 74 68 65 20 62 65 68 61 76 69 6f 72 20 6f 66 20 the behavior of
261f0 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 sqlite3_column_t
26200 79 70 65 28 29 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 ype().** followi
26210 6e 67 20 61 20 74 79 70 65 20 63 6f 6e 76 65 72 ng a type conver
26220 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 sion..**.** If t
26230 68 65 20 72 65 73 75 6c 74 20 69 73 20 61 20 42 he result is a B
26240 4c 4f 42 20 6f 72 20 55 54 46 2d 38 20 73 74 72 LOB or UTF-8 str
26250 69 6e 67 20 74 68 65 6e 20 74 68 65 20 73 71 6c ing then the sql
26260 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 ite3_column_byte
26270 73 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 72 s().** routine r
26280 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 eturns the numbe
26290 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 r of bytes in th
262a0 61 74 20 42 4c 4f 42 20 6f 72 20 73 74 72 69 6e at BLOB or strin
262b0 67 2e 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 73 g..** If the res
262c0 75 6c 74 20 69 73 20 61 20 55 54 46 2d 31 36 20 ult is a UTF-16
262d0 73 74 72 69 6e 67 2c 20 74 68 65 6e 20 73 71 6c string, then sql
262e0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 ite3_column_byte
262f0 73 28 29 20 63 6f 6e 76 65 72 74 73 0a 2a 2a 20 s() converts.**
26300 74 68 65 20 73 74 72 69 6e 67 20 74 6f 20 55 54 the string to UT
26310 46 2d 38 20 61 6e 64 20 74 68 65 6e 20 72 65 74 F-8 and then ret
26320 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 urns the number
26330 6f 66 20 62 79 74 65 73 2e 0a 2a 2a 20 49 66 20 of bytes..** If
26340 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 61 20 the result is a
26350 6e 75 6d 65 72 69 63 20 76 61 6c 75 65 20 74 68 numeric value th
26360 65 6e 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d en sqlite3_colum
26370 6e 5f 62 79 74 65 73 28 29 20 75 73 65 73 0a 2a n_bytes() uses.*
26380 2a 20 5b 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 * [sqlite3_snpri
26390 6e 74 66 28 29 5d 20 74 6f 20 63 6f 6e 76 65 72 ntf()] to conver
263a0 74 20 74 68 61 74 20 76 61 6c 75 65 20 74 6f 20 t that value to
263b0 61 20 55 54 46 2d 38 20 73 74 72 69 6e 67 20 61 a UTF-8 string a
263c0 6e 64 20 72 65 74 75 72 6e 73 0a 2a 2a 20 74 68 nd returns.** th
263d0 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 e number of byte
263e0 73 20 69 6e 20 74 68 61 74 20 73 74 72 69 6e 67 s in that string
263f0 2e 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 72 ..** The value r
26400 65 74 75 72 6e 65 64 20 64 6f 65 73 20 6e 6f 74 eturned does not
26410 20 69 6e 63 6c 75 64 65 20 74 68 65 20 7a 65 72 include the zer
26420 6f 20 74 65 72 6d 69 6e 61 74 6f 72 20 61 74 20 o terminator at
26430 74 68 65 20 65 6e 64 0a 2a 2a 20 6f 66 20 74 68 the end.** of th
26440 65 20 73 74 72 69 6e 67 2e 20 20 46 6f 72 20 63 e string. For c
26450 6c 61 72 69 74 79 3a 20 74 68 65 20 76 61 6c 75 larity: the valu
26460 65 20 72 65 74 75 72 6e 65 64 20 69 73 20 74 68 e returned is th
26470 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 62 e number of.** b
26480 79 74 65 73 20 69 6e 20 74 68 65 20 73 74 72 69 ytes in the stri
26490 6e 67 2c 20 6e 6f 74 20 74 68 65 20 6e 75 6d 62 ng, not the numb
264a0 65 72 20 6f 66 20 63 68 61 72 61 63 74 65 72 73 er of characters
264b0 2e 0a 2a 2a 0a 2a 2a 20 53 74 72 69 6e 67 73 20 ..**.** Strings
264c0 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 returned by sqli
264d0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 te3_column_text(
264e0 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 63 6f ) and sqlite3_co
264f0 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 2c 0a 2a lumn_text16(),.*
26500 2a 20 65 76 65 6e 20 65 6d 70 74 79 20 73 74 72 * even empty str
26510 69 6e 67 73 2c 20 61 72 65 20 61 6c 77 61 79 73 ings, are always
26520 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 65 64 zero terminated
26530 2e 20 20 54 68 65 20 72 65 74 75 72 6e 0a 2a 2a . The return.**
26540 20 76 61 6c 75 65 20 66 72 6f 6d 20 73 71 6c 69 value from sqli
26550 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 te3_column_blob(
26560 29 20 66 6f 72 20 61 20 7a 65 72 6f 2d 6c 65 6e ) for a zero-len
26570 67 74 68 20 42 4c 4f 42 20 69 73 20 61 6e 20 61 gth BLOB is an a
26580 72 62 69 74 72 61 72 79 0a 2a 2a 20 70 6f 69 6e rbitrary.** poin
26590 74 65 72 2c 20 70 6f 73 73 69 62 6c 79 20 65 76 ter, possibly ev
265a0 65 6e 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 en a NULL pointe
265b0 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c r..**.** The sql
265c0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 ite3_column_byte
265d0 73 31 36 28 29 20 72 6f 75 74 69 6e 65 20 69 73 s16() routine is
265e0 20 73 69 6d 69 6c 61 72 20 74 6f 20 73 71 6c 69 similar to sqli
265f0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 te3_column_bytes
26600 28 29 0a 2a 2a 20 62 75 74 20 6c 65 61 76 65 73 ().** but leaves
26610 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20 55 the result in U
26620 54 46 2d 31 36 20 69 6e 20 6e 61 74 69 76 65 20 TF-16 in native
26630 62 79 74 65 20 6f 72 64 65 72 20 69 6e 73 74 65 byte order inste
26640 61 64 20 6f 66 20 55 54 46 2d 38 2e 0a 2a 2a 20 ad of UTF-8..**
26650 54 68 65 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 The zero termina
26660 74 6f 72 20 69 73 20 6e 6f 74 20 69 6e 63 6c 75 tor is not inclu
26670 64 65 64 20 69 6e 20 74 68 69 73 20 63 6f 75 6e ded in this coun
26680 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 62 6a t..**.** The obj
26690 65 63 74 20 72 65 74 75 72 6e 65 64 20 62 79 20 ect returned by
266a0 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f [sqlite3_column_
266b0 76 61 6c 75 65 28 29 5d 20 69 73 20 61 6e 0a 2a value()] is an.*
266c0 2a 20 5b 75 6e 70 72 6f 74 65 63 74 65 64 20 73 * [unprotected s
266d0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 qlite3_value] ob
266e0 6a 65 63 74 2e 20 20 41 6e 20 75 6e 70 72 6f 74 ject. An unprot
266f0 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 ected sqlite3_va
26700 6c 75 65 20 6f 62 6a 65 63 74 0a 2a 2a 20 6d 61 lue object.** ma
26710 79 20 6f 6e 6c 79 20 62 65 20 75 73 65 64 20 77 y only be used w
26720 69 74 68 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e ith [sqlite3_bin
26730 64 5f 76 61 6c 75 65 28 29 5d 20 61 6e 64 20 5b d_value()] and [
26740 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 sqlite3_result_v
26750 61 6c 75 65 28 29 5d 2e 0a 2a 2a 20 49 66 20 74 alue()]..** If t
26760 68 65 20 5b 75 6e 70 72 6f 74 65 63 74 65 64 20 he [unprotected
26770 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f sqlite3_value] o
26780 62 6a 65 63 74 20 72 65 74 75 72 6e 65 64 20 62 bject returned b
26790 79 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f y.** [sqlite3_co
267a0 6c 75 6d 6e 5f 76 61 6c 75 65 28 29 5d 20 69 73 lumn_value()] is
267b0 20 75 73 65 64 20 69 6e 20 61 6e 79 20 6f 74 68 used in any oth
267c0 65 72 20 77 61 79 2c 20 69 6e 63 6c 75 64 69 6e er way, includin
267d0 67 20 63 61 6c 6c 73 0a 2a 2a 20 74 6f 20 72 6f g calls.** to ro
267e0 75 74 69 6e 65 73 20 6c 69 6b 65 20 5b 73 71 6c utines like [sql
267f0 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28 29 ite3_value_int()
26800 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 ], [sqlite3_valu
26810 65 5f 74 65 78 74 28 29 5d 2c 0a 2a 2a 20 6f 72 e_text()],.** or
26820 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f [sqlite3_value_
26830 62 79 74 65 73 28 29 5d 2c 20 74 68 65 6e 20 74 bytes()], then t
26840 68 65 20 62 65 68 61 76 69 6f 72 20 69 73 20 75 he behavior is u
26850 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 ndefined..**.**
26860 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 These routines a
26870 74 74 65 6d 70 74 20 74 6f 20 63 6f 6e 76 65 72 ttempt to conver
26880 74 20 74 68 65 20 76 61 6c 75 65 20 77 68 65 72 t the value wher
26890 65 20 61 70 70 72 6f 70 72 69 61 74 65 2e 20 20 e appropriate.
268a0 46 6f 72 0a 2a 2a 20 65 78 61 6d 70 6c 65 2c 20 For.** example,
268b0 69 66 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 if the internal
268c0 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 69 representation i
268d0 73 20 46 4c 4f 41 54 20 61 6e 64 20 61 20 74 65 s FLOAT and a te
268e0 78 74 20 72 65 73 75 6c 74 0a 2a 2a 20 69 73 20 xt result.** is
268f0 72 65 71 75 65 73 74 65 64 2c 20 5b 73 71 6c 69 requested, [sqli
26900 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 5d 20 te3_snprintf()]
26910 69 73 20 75 73 65 64 20 69 6e 74 65 72 6e 61 6c is used internal
26920 6c 79 20 74 6f 20 70 65 72 66 6f 72 6d 20 74 68 ly to perform th
26930 65 0a 2a 2a 20 63 6f 6e 76 65 72 73 69 6f 6e 20 e.** conversion
26940 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 2e 20 20 automatically.
26950 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 61 The following ta
26960 62 6c 65 20 64 65 74 61 69 6c 73 20 74 68 65 20 ble details the
26970 63 6f 6e 76 65 72 73 69 6f 6e 73 0a 2a 2a 20 74 conversions.** t
26980 68 61 74 20 61 72 65 20 61 70 70 6c 69 65 64 3a hat are applied:
26990 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f .**.** <blockquo
269a0 74 65 3e 0a 2a 2a 20 3c 74 61 62 6c 65 20 62 6f te>.** <table bo
269b0 72 64 65 72 3d 22 31 22 3e 0a 2a 2a 20 3c 74 72 rder="1">.** <tr
269c0 3e 3c 74 68 3e 20 49 6e 74 65 72 6e 61 6c 3c 62 ><th> Internal<b
269d0 72 3e 54 79 70 65 20 3c 74 68 3e 20 52 65 71 75 r>Type <th> Requ
269e0 65 73 74 65 64 3c 62 72 3e 54 79 70 65 20 3c 74 ested<br>Type <t
269f0 68 3e 20 20 43 6f 6e 76 65 72 73 69 6f 6e 0a 2a h> Conversion.*
26a00 2a 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 4e *.** <tr><td> N
26a10 55 4c 4c 20 20 20 20 3c 74 64 3e 20 49 4e 54 45 ULL <td> INTE
26a20 47 45 52 20 20 20 3c 74 64 3e 20 52 65 73 75 6c GER <td> Resul
26a30 74 20 69 73 20 30 0a 2a 2a 20 3c 74 72 3e 3c 74 t is 0.** <tr><t
26a40 64 3e 20 20 4e 55 4c 4c 20 20 20 20 3c 74 64 3e d> NULL <td>
26a50 20 20 46 4c 4f 41 54 20 20 20 20 3c 74 64 3e 20 FLOAT <td>
26a60 52 65 73 75 6c 74 20 69 73 20 30 2e 30 0a 2a 2a Result is 0.0.**
26a70 20 3c 74 72 3e 3c 74 64 3e 20 20 4e 55 4c 4c 20 <tr><td> NULL
26a80 20 20 20 3c 74 64 3e 20 20 20 54 45 58 54 20 20 <td> TEXT
26a90 20 20 3c 74 64 3e 20 52 65 73 75 6c 74 20 69 73 <td> Result is
26aa0 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 0a 2a 2a NULL pointer.**
26ab0 20 3c 74 72 3e 3c 74 64 3e 20 20 4e 55 4c 4c 20 <tr><td> NULL
26ac0 20 20 20 3c 74 64 3e 20 20 20 42 4c 4f 42 20 20 <td> BLOB
26ad0 20 20 3c 74 64 3e 20 52 65 73 75 6c 74 20 69 73 <td> Result is
26ae0 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 0a 2a 2a NULL pointer.**
26af0 20 3c 74 72 3e 3c 74 64 3e 20 49 4e 54 45 47 45 <tr><td> INTEGE
26b00 52 20 20 3c 74 64 3e 20 20 46 4c 4f 41 54 20 20 R <td> FLOAT
26b10 20 20 3c 74 64 3e 20 43 6f 6e 76 65 72 74 20 66 <td> Convert f
26b20 72 6f 6d 20 69 6e 74 65 67 65 72 20 74 6f 20 66 rom integer to f
26b30 6c 6f 61 74 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e loat.** <tr><td>
26b40 20 49 4e 54 45 47 45 52 20 20 3c 74 64 3e 20 20 INTEGER <td>
26b50 20 54 45 58 54 20 20 20 20 3c 74 64 3e 20 41 53 TEXT <td> AS
26b60 43 49 49 20 72 65 6e 64 65 72 69 6e 67 20 6f 66 CII rendering of
26b70 20 74 68 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20 the integer.**
26b80 3c 74 72 3e 3c 74 64 3e 20 49 4e 54 45 47 45 52 <tr><td> INTEGER
26b90 20 20 3c 74 64 3e 20 20 20 42 4c 4f 42 20 20 20 <td> BLOB
26ba0 20 3c 74 64 3e 20 53 61 6d 65 20 61 73 20 49 4e <td> Same as IN
26bb0 54 45 47 45 52 2d 3e 54 45 58 54 0a 2a 2a 20 3c TEGER->TEXT.** <
26bc0 74 72 3e 3c 74 64 3e 20 20 46 4c 4f 41 54 20 20 tr><td> FLOAT
26bd0 20 3c 74 64 3e 20 49 4e 54 45 47 45 52 20 20 20 <td> INTEGER
26be0 3c 74 64 3e 20 43 6f 6e 76 65 72 74 20 66 72 6f <td> Convert fro
26bf0 6d 20 66 6c 6f 61 74 20 74 6f 20 69 6e 74 65 67 m float to integ
26c00 65 72 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 er.** <tr><td>
26c10 46 4c 4f 41 54 20 20 20 3c 74 64 3e 20 20 20 54 FLOAT <td> T
26c20 45 58 54 20 20 20 20 3c 74 64 3e 20 41 53 43 49 EXT <td> ASCI
26c30 49 20 72 65 6e 64 65 72 69 6e 67 20 6f 66 20 74 I rendering of t
26c40 68 65 20 66 6c 6f 61 74 0a 2a 2a 20 3c 74 72 3e he float.** <tr>
26c50 3c 74 64 3e 20 20 46 4c 4f 41 54 20 20 20 3c 74 <td> FLOAT <t
26c60 64 3e 20 20 20 42 4c 4f 42 20 20 20 20 3c 74 64 d> BLOB <td
26c70 3e 20 53 61 6d 65 20 61 73 20 46 4c 4f 41 54 2d > Same as FLOAT-
26c80 3e 54 45 58 54 0a 2a 2a 20 3c 74 72 3e 3c 74 64 >TEXT.** <tr><td
26c90 3e 20 20 54 45 58 54 20 20 20 20 3c 74 64 3e 20 > TEXT <td>
26ca0 49 4e 54 45 47 45 52 20 20 20 3c 74 64 3e 20 55 INTEGER <td> U
26cb0 73 65 20 61 74 6f 69 28 29 0a 2a 2a 20 3c 74 72 se atoi().** <tr
26cc0 3e 3c 74 64 3e 20 20 54 45 58 54 20 20 20 20 3c ><td> TEXT <
26cd0 74 64 3e 20 20 46 4c 4f 41 54 20 20 20 20 3c 74 td> FLOAT <t
26ce0 64 3e 20 55 73 65 20 61 74 6f 66 28 29 0a 2a 2a d> Use atof().**
26cf0 20 3c 74 72 3e 3c 74 64 3e 20 20 54 45 58 54 20 <tr><td> TEXT
26d00 20 20 20 3c 74 64 3e 20 20 20 42 4c 4f 42 20 20 <td> BLOB
26d10 20 20 3c 74 64 3e 20 4e 6f 20 63 68 61 6e 67 65 <td> No change
26d20 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 42 4c .** <tr><td> BL
26d30 4f 42 20 20 20 20 3c 74 64 3e 20 49 4e 54 45 47 OB <td> INTEG
26d40 45 52 20 20 20 3c 74 64 3e 20 43 6f 6e 76 65 72 ER <td> Conver
26d50 74 20 74 6f 20 54 45 58 54 20 74 68 65 6e 20 75 t to TEXT then u
26d60 73 65 20 61 74 6f 69 28 29 0a 2a 2a 20 3c 74 72 se atoi().** <tr
26d70 3e 3c 74 64 3e 20 20 42 4c 4f 42 20 20 20 20 3c ><td> BLOB <
26d80 74 64 3e 20 20 46 4c 4f 41 54 20 20 20 20 3c 74 td> FLOAT <t
26d90 64 3e 20 43 6f 6e 76 65 72 74 20 74 6f 20 54 45 d> Convert to TE
26da0 58 54 20 74 68 65 6e 20 75 73 65 20 61 74 6f 66 XT then use atof
26db0 28 29 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 ().** <tr><td>
26dc0 42 4c 4f 42 20 20 20 20 3c 74 64 3e 20 20 20 54 BLOB <td> T
26dd0 45 58 54 20 20 20 20 3c 74 64 3e 20 41 64 64 20 EXT <td> Add
26de0 61 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 6f a zero terminato
26df0 72 20 69 66 20 6e 65 65 64 65 64 0a 2a 2a 20 3c r if needed.** <
26e00 2f 74 61 62 6c 65 3e 0a 2a 2a 20 3c 2f 62 6c 6f /table>.** </blo
26e10 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 54 ckquote>.**.** T
26e20 68 65 20 74 61 62 6c 65 20 61 62 6f 76 65 20 6d he table above m
26e30 61 6b 65 73 20 72 65 66 65 72 65 6e 63 65 20 74 akes reference t
26e40 6f 20 73 74 61 6e 64 61 72 64 20 43 20 6c 69 62 o standard C lib
26e50 72 61 72 79 20 66 75 6e 63 74 69 6f 6e 73 20 61 rary functions a
26e60 74 6f 69 28 29 0a 2a 2a 20 61 6e 64 20 61 74 6f toi().** and ato
26e70 66 28 29 2e 20 20 53 51 4c 69 74 65 20 64 6f 65 f(). SQLite doe
26e80 73 20 6e 6f 74 20 72 65 61 6c 6c 79 20 75 73 65 s not really use
26e90 20 74 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 these functions
26ea0 2e 20 20 49 74 20 68 61 73 20 69 74 73 0a 2a 2a . It has its.**
26eb0 20 6f 77 6e 20 65 71 75 69 76 61 6c 65 6e 74 20 own equivalent
26ec0 69 6e 74 65 72 6e 61 6c 20 72 6f 75 74 69 6e 65 internal routine
26ed0 73 2e 20 20 54 68 65 20 61 74 6f 69 28 29 20 61 s. The atoi() a
26ee0 6e 64 20 61 74 6f 66 28 29 20 6e 61 6d 65 73 20 nd atof() names
26ef0 61 72 65 0a 2a 2a 20 75 73 65 64 20 69 6e 20 74 are.** used in t
26f00 68 65 20 74 61 62 6c 65 20 66 6f 72 20 62 72 65 he table for bre
26f10 76 69 74 79 20 61 6e 64 20 62 65 63 61 75 73 65 vity and because
26f20 20 74 68 65 79 20 61 72 65 20 66 61 6d 69 6c 69 they are famili
26f30 61 72 20 74 6f 20 6d 6f 73 74 0a 2a 2a 20 43 20 ar to most.** C
26f40 70 72 6f 67 72 61 6d 6d 65 72 73 2e 0a 2a 2a 0a programmers..**.
26f50 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 77 68 65 ** Note that whe
26f60 6e 20 74 79 70 65 20 63 6f 6e 76 65 72 73 69 6f n type conversio
26f70 6e 73 20 6f 63 63 75 72 2c 20 70 6f 69 6e 74 65 ns occur, pointe
26f80 72 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 70 rs returned by p
26f90 72 69 6f 72 0a 2a 2a 20 63 61 6c 6c 73 20 74 6f rior.** calls to
26fa0 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
26fb0 62 6c 6f 62 28 29 2c 20 73 71 6c 69 74 65 33 5f blob(), sqlite3_
26fc0 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 2c 20 61 column_text(), a
26fd0 6e 64 2f 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 nd/or.** sqlite3
26fe0 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 _column_text16()
26ff0 20 6d 61 79 20 62 65 20 69 6e 76 61 6c 69 64 61 may be invalida
27000 74 65 64 2e 0a 2a 2a 20 54 79 70 65 20 63 6f 6e ted..** Type con
27010 76 65 72 73 69 6f 6e 73 20 61 6e 64 20 70 6f 69 versions and poi
27020 6e 74 65 72 20 69 6e 76 61 6c 69 64 61 74 69 6f nter invalidatio
27030 6e 73 20 6d 69 67 68 74 20 6f 63 63 75 72 0a 2a ns might occur.*
27040 2a 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 * in the followi
27050 6e 67 20 63 61 73 65 73 3a 0a 2a 2a 0a 2a 2a 20 ng cases:.**.**
27060 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 54 68 65 <ul>.** <li> The
27070 20 69 6e 69 74 69 61 6c 20 63 6f 6e 74 65 6e 74 initial content
27080 20 69 73 20 61 20 42 4c 4f 42 20 61 6e 64 20 73 is a BLOB and s
27090 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 qlite3_column_te
270a0 78 74 28 29 20 6f 72 0a 2a 2a 20 20 20 20 20 20 xt() or.**
270b0 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 sqlite3_column_t
270c0 65 78 74 31 36 28 29 20 69 73 20 63 61 6c 6c 65 ext16() is calle
270d0 64 2e 20 20 41 20 7a 65 72 6f 2d 74 65 72 6d 69 d. A zero-termi
270e0 6e 61 74 6f 72 20 6d 69 67 68 74 0a 2a 2a 20 20 nator might.**
270f0 20 20 20 20 6e 65 65 64 20 74 6f 20 62 65 20 61 need to be a
27100 64 64 65 64 20 74 6f 20 74 68 65 20 73 74 72 69 dded to the stri
27110 6e 67 2e 3c 2f 6c 69 3e 0a 2a 2a 20 3c 6c 69 3e ng.</li>.** <li>
27120 20 54 68 65 20 69 6e 69 74 69 61 6c 20 63 6f 6e The initial con
27130 74 65 6e 74 20 69 73 20 55 54 46 2d 38 20 74 65 tent is UTF-8 te
27140 78 74 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 63 xt and sqlite3_c
27150 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36 28 29 20 olumn_bytes16()
27160 6f 72 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 or.** sqlit
27170 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 e3_column_text16
27180 28 29 20 69 73 20 63 61 6c 6c 65 64 2e 20 20 54 () is called. T
27190 68 65 20 63 6f 6e 74 65 6e 74 20 6d 75 73 74 20 he content must
271a0 62 65 20 63 6f 6e 76 65 72 74 65 64 0a 2a 2a 20 be converted.**
271b0 20 20 20 20 20 74 6f 20 55 54 46 2d 31 36 2e 3c to UTF-16.<
271c0 2f 6c 69 3e 0a 2a 2a 20 3c 6c 69 3e 20 54 68 65 /li>.** <li> The
271d0 20 69 6e 69 74 69 61 6c 20 63 6f 6e 74 65 6e 74 initial content
271e0 20 69 73 20 55 54 46 2d 31 36 20 74 65 78 74 20 is UTF-16 text
271f0 61 6e 64 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 and sqlite3_colu
27200 6d 6e 5f 62 79 74 65 73 28 29 20 6f 72 0a 2a 2a mn_bytes() or.**
27210 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f sqlite3_co
27220 6c 75 6d 6e 5f 74 65 78 74 28 29 20 69 73 20 63 lumn_text() is c
27230 61 6c 6c 65 64 2e 20 20 54 68 65 20 63 6f 6e 74 alled. The cont
27240 65 6e 74 20 6d 75 73 74 20 62 65 20 63 6f 6e 76 ent must be conv
27250 65 72 74 65 64 0a 2a 2a 20 20 20 20 20 20 74 6f erted.** to
27260 20 55 54 46 2d 38 2e 3c 2f 6c 69 3e 0a 2a 2a 20 UTF-8.</li>.**
27270 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 43 6f 6e 76 </ul>.**.** Conv
27280 65 72 73 69 6f 6e 73 20 62 65 74 77 65 65 6e 20 ersions between
27290 55 54 46 2d 31 36 62 65 20 61 6e 64 20 55 54 46 UTF-16be and UTF
272a0 2d 31 36 6c 65 20 61 72 65 20 61 6c 77 61 79 73 -16le are always
272b0 20 64 6f 6e 65 20 69 6e 20 70 6c 61 63 65 20 61 done in place a
272c0 6e 64 20 64 6f 0a 2a 2a 20 6e 6f 74 20 69 6e 76 nd do.** not inv
272d0 61 6c 69 64 61 74 65 20 61 20 70 72 69 6f 72 20 alidate a prior
272e0 70 6f 69 6e 74 65 72 2c 20 74 68 6f 75 67 68 20 pointer, though
272f0 6f 66 20 63 6f 75 72 73 65 20 74 68 65 20 63 6f of course the co
27300 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 62 75 66 ntent of the buf
27310 66 65 72 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 fer.** that the
27320 70 72 69 6f 72 20 70 6f 69 6e 74 65 72 20 70 6f prior pointer po
27330 69 6e 74 73 20 74 6f 20 77 69 6c 6c 20 68 61 76 ints to will hav
27340 65 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 2e e been modified.
27350 20 20 4f 74 68 65 72 20 6b 69 6e 64 73 0a 2a 2a Other kinds.**
27360 20 6f 66 20 63 6f 6e 76 65 72 73 69 6f 6e 20 61 of conversion a
27370 72 65 20 64 6f 6e 65 20 69 6e 20 70 6c 61 63 65 re done in place
27380 20 77 68 65 6e 20 69 74 20 69 73 20 70 6f 73 73 when it is poss
27390 69 62 6c 65 2c 20 62 75 74 20 73 6f 6d 65 74 69 ible, but someti
273a0 6d 65 73 20 74 68 65 79 0a 2a 2a 20 61 72 65 20 mes they.** are
273b0 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 61 6e 64 not possible and
273c0 20 69 6e 20 74 68 6f 73 65 20 63 61 73 65 73 20 in those cases
273d0 70 72 69 6f 72 20 70 6f 69 6e 74 65 72 73 20 61 prior pointers a
273e0 72 65 20 69 6e 76 61 6c 69 64 61 74 65 64 2e 0a re invalidated..
273f0 2a 2a 0a 2a 2a 20 54 68 65 20 73 61 66 65 73 74 **.** The safest
27400 20 61 6e 64 20 65 61 73 69 65 73 74 20 74 6f 20 and easiest to
27410 72 65 6d 65 6d 62 65 72 20 70 6f 6c 69 63 79 20 remember policy
27420 69 73 20 74 6f 20 69 6e 76 6f 6b 65 20 74 68 65 is to invoke the
27430 73 65 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 69 se routines.** i
27440 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c n one of the fol
27450 6c 6f 77 69 6e 67 20 77 61 79 73 3a 0a 2a 2a 0a lowing ways:.**.
27460 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 20 3c 6c 69 3e ** <ul>.** <li>
27470 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 sqlite3_column_t
27480 65 78 74 28 29 20 66 6f 6c 6c 6f 77 65 64 20 62 ext() followed b
27490 79 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e y sqlite3_column
274a0 5f 62 79 74 65 73 28 29 3c 2f 6c 69 3e 0a 2a 2a _bytes()</li>.**
274b0 20 20 3c 6c 69 3e 73 71 6c 69 74 65 33 5f 63 6f <li>sqlite3_co
274c0 6c 75 6d 6e 5f 62 6c 6f 62 28 29 20 66 6f 6c 6c lumn_blob() foll
274d0 6f 77 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f owed by sqlite3_
274e0 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 3c 2f column_bytes()</
274f0 6c 69 3e 0a 2a 2a 20 20 3c 6c 69 3e 73 71 6c 69 li>.** <li>sqli
27500 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 te3_column_text1
27510 36 28 29 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 6() followed by
27520 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 sqlite3_column_b
27530 79 74 65 73 31 36 28 29 3c 2f 6c 69 3e 0a 2a 2a ytes16()</li>.**
27540 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 49 6e 20 </ul>.**.** In
27550 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 79 6f 75 other words, you
27560 20 73 68 6f 75 6c 64 20 63 61 6c 6c 20 73 71 6c should call sql
27570 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 ite3_column_text
27580 28 29 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 (),.** sqlite3_c
27590 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 29 2c 20 6f 72 olumn_blob(), or
275a0 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
275b0 74 65 78 74 31 36 28 29 20 66 69 72 73 74 20 74 text16() first t
275c0 6f 20 66 6f 72 63 65 20 74 68 65 20 72 65 73 75 o force the resu
275d0 6c 74 0a 2a 2a 20 69 6e 74 6f 20 74 68 65 20 64 lt.** into the d
275e0 65 73 69 72 65 64 20 66 6f 72 6d 61 74 2c 20 74 esired format, t
275f0 68 65 6e 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74 hen invoke sqlit
27600 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 e3_column_bytes(
27610 29 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f ) or.** sqlite3_
27620 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36 28 29 column_bytes16()
27630 20 74 6f 20 66 69 6e 64 20 74 68 65 20 73 69 7a to find the siz
27640 65 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 2e e of the result.
27650 20 20 44 6f 20 6e 6f 74 20 6d 69 78 20 63 61 6c Do not mix cal
27660 6c 73 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 ls.** to sqlite3
27670 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 20 6f _column_text() o
27680 72 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e r sqlite3_column
27690 5f 62 6c 6f 62 28 29 20 77 69 74 68 20 63 61 6c _blob() with cal
276a0 6c 73 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 ls to.** sqlite3
276b0 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36 28 _column_bytes16(
276c0 29 2c 20 61 6e 64 20 64 6f 20 6e 6f 74 20 6d 69 ), and do not mi
276d0 78 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 x calls to sqlit
276e0 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 e3_column_text16
276f0 28 29 0a 2a 2a 20 77 69 74 68 20 63 61 6c 6c 73 ().** with calls
27700 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 to sqlite3_colu
27710 6d 6e 5f 62 79 74 65 73 28 29 2e 0a 2a 2a 0a 2a mn_bytes()..**.*
27720 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 73 20 72 * The pointers r
27730 65 74 75 72 6e 65 64 20 61 72 65 20 76 61 6c 69 eturned are vali
27740 64 20 75 6e 74 69 6c 20 61 20 74 79 70 65 20 63 d until a type c
27750 6f 6e 76 65 72 73 69 6f 6e 20 6f 63 63 75 72 73 onversion occurs
27760 20 61 73 0a 2a 2a 20 64 65 73 63 72 69 62 65 64 as.** described
27770 20 61 62 6f 76 65 2c 20 6f 72 20 75 6e 74 69 6c above, or until
27780 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 [sqlite3_step()
27790 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 72 65 ] or [sqlite3_re
277a0 73 65 74 28 29 5d 20 6f 72 0a 2a 2a 20 5b 73 71 set()] or.** [sq
277b0 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 lite3_finalize()
277c0 5d 20 69 73 20 63 61 6c 6c 65 64 2e 20 20 54 68 ] is called. Th
277d0 65 20 6d 65 6d 6f 72 79 20 73 70 61 63 65 20 75 e memory space u
277e0 73 65 64 20 74 6f 20 68 6f 6c 64 20 73 74 72 69 sed to hold stri
277f0 6e 67 73 0a 2a 2a 20 61 6e 64 20 42 4c 4f 42 73 ngs.** and BLOBs
27800 20 69 73 20 66 72 65 65 64 20 61 75 74 6f 6d 61 is freed automa
27810 74 69 63 61 6c 6c 79 2e 20 20 44 6f 20 3c 62 3e tically. Do <b>
27820 6e 6f 74 3c 2f 62 3e 20 70 61 73 73 20 74 68 65 not</b> pass the
27830 20 70 6f 69 6e 74 65 72 73 20 72 65 74 75 72 6e pointers return
27840 65 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 ed.** [sqlite3_c
27850 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 29 5d 2c 20 5b olumn_blob()], [
27860 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 sqlite3_column_t
27870 65 78 74 28 29 5d 2c 20 65 74 63 2e 20 69 6e 74 ext()], etc. int
27880 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66 72 o.** [sqlite3_fr
27890 65 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 ee()]..**.** If
278a0 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 a memory allocat
278b0 69 6f 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 ion error occurs
278c0 20 64 75 72 69 6e 67 20 74 68 65 20 65 76 61 6c during the eval
278d0 75 61 74 69 6f 6e 20 6f 66 20 61 6e 79 0a 2a 2a uation of any.**
278e0 20 6f 66 20 74 68 65 73 65 20 72 6f 75 74 69 6e of these routin
278f0 65 73 2c 20 61 20 64 65 66 61 75 6c 74 20 76 61 es, a default va
27900 6c 75 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e lue is returned.
27910 20 20 54 68 65 20 64 65 66 61 75 6c 74 20 76 61 The default va
27920 6c 75 65 0a 2a 2a 20 69 73 20 65 69 74 68 65 72 lue.** is either
27930 20 74 68 65 20 69 6e 74 65 67 65 72 20 30 2c 20 the integer 0,
27940 74 68 65 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 the floating poi
27950 6e 74 20 6e 75 6d 62 65 72 20 30 2e 30 2c 20 6f nt number 0.0, o
27960 72 20 61 20 4e 55 4c 4c 0a 2a 2a 20 70 6f 69 6e r a NULL.** poin
27970 74 65 72 2e 20 20 53 75 62 73 65 71 75 65 6e 74 ter. Subsequent
27980 20 63 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 calls to [sqlit
27990 65 33 5f 65 72 72 63 6f 64 65 28 29 5d 20 77 69 e3_errcode()] wi
279a0 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 5b 53 51 ll return.** [SQ
279b0 4c 49 54 45 5f 4e 4f 4d 45 4d 5d 2e 0a 2a 2a 0a LITE_NOMEM]..**.
279c0 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a ** Requirements:
279d0 0a 2a 2a 20 5b 48 31 33 38 30 33 5d 20 5b 48 31 .** [H13803] [H1
279e0 33 38 30 36 5d 20 5b 48 31 33 38 30 39 5d 20 5b 3806] [H13809] [
279f0 48 31 33 38 31 32 5d 20 5b 48 31 33 38 31 35 5d H13812] [H13815]
27a00 20 5b 48 31 33 38 31 38 5d 20 5b 48 31 33 38 32 [H13818] [H1382
27a10 31 5d 20 5b 48 31 33 38 32 34 5d 0a 2a 2a 20 5b 1] [H13824].** [
27a20 48 31 33 38 32 37 5d 20 5b 48 31 33 38 33 30 5d H13827] [H13830]
27a30 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 .*/.SQLITE_API c
27a40 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 onst void *sqlit
27a50 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 73 e3_column_blob(s
27a60 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e qlite3_stmt*, in
27a70 74 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f t iCol);.SQLITE_
27a80 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
27a90 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 73 71 6c column_bytes(sql
27aa0 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 ite3_stmt*, int
27ab0 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50 iCol);.SQLITE_AP
27ac0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f I int sqlite3_co
27ad0 6c 75 6d 6e 5f 62 79 74 65 73 31 36 28 73 71 6c lumn_bytes16(sql
27ae0 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 ite3_stmt*, int
27af0 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50 iCol);.SQLITE_AP
27b00 49 20 64 6f 75 62 6c 65 20 73 71 6c 69 74 65 33 I double sqlite3
27b10 5f 63 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65 28 73 _column_double(s
27b20 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e qlite3_stmt*, in
27b30 74 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f t iCol);.SQLITE_
27b40 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
27b50 63 6f 6c 75 6d 6e 5f 69 6e 74 28 73 71 6c 69 74 column_int(sqlit
27b60 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 e3_stmt*, int iC
27b70 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 ol);.SQLITE_API
27b80 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 73 71 sqlite3_int64 sq
27b90 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 lite3_column_int
27ba0 36 34 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 64(sqlite3_stmt*
27bb0 2c 20 69 6e 74 20 69 43 6f 6c 29 3b 0a 53 51 4c , int iCol);.SQL
27bc0 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 75 6e ITE_API const un
27bd0 73 69 67 6e 65 64 20 63 68 61 72 20 2a 73 71 6c signed char *sql
27be0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 ite3_column_text
27bf0 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 (sqlite3_stmt*,
27c00 69 6e 74 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 int iCol);.SQLIT
27c10 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 E_API const void
27c20 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e *sqlite3_column
27c30 5f 74 65 78 74 31 36 28 73 71 6c 69 74 65 33 5f _text16(sqlite3_
27c40 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 stmt*, int iCol)
27c50 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 ;.SQLITE_API int
27c60 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
27c70 74 79 70 65 28 73 71 6c 69 74 65 33 5f 73 74 6d type(sqlite3_stm
27c80 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b 0a 53 t*, int iCol);.S
27c90 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 QLITE_API sqlite
27ca0 33 5f 76 61 6c 75 65 20 2a 73 71 6c 69 74 65 33 3_value *sqlite3
27cb0 5f 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28 73 71 _column_value(sq
27cc0 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 lite3_stmt*, int
27cd0 20 69 43 6f 6c 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 iCol);../*.** C
27ce0 41 50 49 33 52 45 46 3a 20 44 65 73 74 72 6f 79 API3REF: Destroy
27cf0 20 41 20 50 72 65 70 61 72 65 64 20 53 74 61 74 A Prepared Stat
27d00 65 6d 65 6e 74 20 4f 62 6a 65 63 74 20 7b 48 31 ement Object {H1
27d10 33 33 30 30 7d 20 3c 53 37 30 33 30 30 3e 3c 53 3300} <S70300><S
27d20 33 30 31 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 30100>.**.** The
27d30 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a sqlite3_finaliz
27d40 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 e() function is
27d50 63 61 6c 6c 65 64 20 74 6f 20 64 65 6c 65 74 65 called to delete
27d60 20 61 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 a [prepared sta
27d70 74 65 6d 65 6e 74 5d 2e 0a 2a 2a 20 49 66 20 74 tement]..** If t
27d80 68 65 20 73 74 61 74 65 6d 65 6e 74 20 77 61 73 he statement was
27d90 20 65 78 65 63 75 74 65 64 20 73 75 63 63 65 73 executed succes
27da0 73 66 75 6c 6c 79 20 6f 72 20 6e 6f 74 20 65 78 sfully or not ex
27db0 65 63 75 74 65 64 20 61 74 20 61 6c 6c 2c 20 74 ecuted at all, t
27dc0 68 65 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b hen.** SQLITE_OK
27dd0 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49 66 is returned. If
27de0 20 65 78 65 63 75 74 69 6f 6e 20 6f 66 20 74 68 execution of th
27df0 65 20 73 74 61 74 65 6d 65 6e 74 20 66 61 69 6c e statement fail
27e00 65 64 20 74 68 65 6e 20 61 6e 0a 2a 2a 20 5b 65 ed then an.** [e
27e10 72 72 6f 72 20 63 6f 64 65 5d 20 6f 72 20 5b 65 rror code] or [e
27e20 78 74 65 6e 64 65 64 20 65 72 72 6f 72 20 63 6f xtended error co
27e30 64 65 5d 20 69 73 20 72 65 74 75 72 6e 65 64 2e de] is returned.
27e40 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
27e50 69 6e 65 20 63 61 6e 20 62 65 20 63 61 6c 6c 65 ine can be calle
27e60 64 20 61 74 20 61 6e 79 20 70 6f 69 6e 74 20 64 d at any point d
27e70 75 72 69 6e 67 20 74 68 65 20 65 78 65 63 75 74 uring the execut
27e80 69 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 5b 70 ion of the.** [p
27e90 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
27ea0 74 5d 2e 20 20 49 66 20 74 68 65 20 76 69 72 74 t]. If the virt
27eb0 75 61 6c 20 6d 61 63 68 69 6e 65 20 68 61 73 20 ual machine has
27ec0 6e 6f 74 0a 2a 2a 20 63 6f 6d 70 6c 65 74 65 64 not.** completed
27ed0 20 65 78 65 63 75 74 69 6f 6e 20 77 68 65 6e 20 execution when
27ee0 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 this routine is
27ef0 63 61 6c 6c 65 64 2c 20 74 68 61 74 20 69 73 20 called, that is
27f00 6c 69 6b 65 0a 2a 2a 20 65 6e 63 6f 75 6e 74 65 like.** encounte
27f10 72 69 6e 67 20 61 6e 20 65 72 72 6f 72 20 6f 72 ring an error or
27f20 20 61 6e 20 5b 73 71 6c 69 74 65 33 5f 69 6e 74 an [sqlite3_int
27f30 65 72 72 75 70 74 20 7c 20 69 6e 74 65 72 72 75 errupt | interru
27f40 70 74 5d 2e 0a 2a 2a 20 49 6e 63 6f 6d 70 6c 65 pt]..** Incomple
27f50 74 65 20 75 70 64 61 74 65 73 20 6d 61 79 20 62 te updates may b
27f60 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61 6e e rolled back an
27f70 64 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 63 d transactions c
27f80 61 6e 63 65 6c 65 64 2c 0a 2a 2a 20 64 65 70 65 anceled,.** depe
27f90 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 63 69 72 nding on the cir
27fa0 63 75 6d 73 74 61 6e 63 65 73 2c 20 61 6e 64 20 cumstances, and
27fb0 74 68 65 0a 2a 2a 20 5b 65 72 72 6f 72 20 63 6f the.** [error co
27fc0 64 65 5d 20 72 65 74 75 72 6e 65 64 20 77 69 6c de] returned wil
27fd0 6c 20 62 65 20 5b 53 51 4c 49 54 45 5f 41 42 4f l be [SQLITE_ABO
27fe0 52 54 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 RT]..**.** Requi
27ff0 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 31 rements:.** [H11
28000 33 30 32 5d 20 5b 48 31 31 33 30 34 5d 0a 2a 2f 302] [H11304].*/
28010 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
28020 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 sqlite3_finalize
28030 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 (sqlite3_stmt *p
28040 53 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 Stmt);../*.** CA
28050 50 49 33 52 45 46 3a 20 52 65 73 65 74 20 41 20 PI3REF: Reset A
28060 50 72 65 70 61 72 65 64 20 53 74 61 74 65 6d 65 Prepared Stateme
28070 6e 74 20 4f 62 6a 65 63 74 20 7b 48 31 33 33 33 nt Object {H1333
28080 30 7d 20 3c 53 37 30 33 30 30 3e 0a 2a 2a 0a 2a 0} <S70300>.**.*
28090 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 * The sqlite3_re
280a0 73 65 74 28 29 20 66 75 6e 63 74 69 6f 6e 20 69 set() function i
280b0 73 20 63 61 6c 6c 65 64 20 74 6f 20 72 65 73 65 s called to rese
280c0 74 20 61 20 5b 70 72 65 70 61 72 65 64 20 73 74 t a [prepared st
280d0 61 74 65 6d 65 6e 74 5d 0a 2a 2a 20 6f 62 6a 65 atement].** obje
280e0 63 74 20 62 61 63 6b 20 74 6f 20 69 74 73 20 69 ct back to its i
280f0 6e 69 74 69 61 6c 20 73 74 61 74 65 2c 20 72 65 nitial state, re
28100 61 64 79 20 74 6f 20 62 65 20 72 65 2d 65 78 65 ady to be re-exe
28110 63 75 74 65 64 2e 0a 2a 2a 20 41 6e 79 20 53 51 cuted..** Any SQ
28120 4c 20 73 74 61 74 65 6d 65 6e 74 20 76 61 72 69 L statement vari
28130 61 62 6c 65 73 20 74 68 61 74 20 68 61 64 20 76 ables that had v
28140 61 6c 75 65 73 20 62 6f 75 6e 64 20 74 6f 20 74 alues bound to t
28150 68 65 6d 20 75 73 69 6e 67 0a 2a 2a 20 74 68 65 hem using.** the
28160 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 [sqlite3_bind_b
28170 6c 6f 62 20 7c 20 73 71 6c 69 74 65 33 5f 62 69 lob | sqlite3_bi
28180 6e 64 5f 2a 28 29 20 41 50 49 5d 20 72 65 74 61 nd_*() API] reta
28190 69 6e 20 74 68 65 69 72 20 76 61 6c 75 65 73 2e in their values.
281a0 0a 2a 2a 20 55 73 65 20 5b 73 71 6c 69 74 65 33 .** Use [sqlite3
281b0 5f 63 6c 65 61 72 5f 62 69 6e 64 69 6e 67 73 28 _clear_bindings(
281c0 29 5d 20 74 6f 20 72 65 73 65 74 20 74 68 65 20 )] to reset the
281d0 62 69 6e 64 69 6e 67 73 2e 0a 2a 2a 0a 2a 2a 20 bindings..**.**
281e0 7b 48 31 31 33 33 32 7d 20 54 68 65 20 5b 73 71 {H11332} The [sq
281f0 6c 69 74 65 33 5f 72 65 73 65 74 28 53 29 5d 20 lite3_reset(S)]
28200 69 6e 74 65 72 66 61 63 65 20 72 65 73 65 74 73 interface resets
28210 20 74 68 65 20 5b 70 72 65 70 61 72 65 64 20 73 the [prepared s
28220 74 61 74 65 6d 65 6e 74 5d 20 53 0a 2a 2a 20 20 tatement] S.**
28230 20 20 20 20 20 20 20 20 62 61 63 6b 20 74 6f 20 back to
28240 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 the beginning of
28250 20 69 74 73 20 70 72 6f 67 72 61 6d 2e 0a 2a 2a its program..**
28260 0a 2a 2a 20 7b 48 31 31 33 33 34 7d 20 49 66 20 .** {H11334} If
28270 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 the most recent
28280 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 call to [sqlite3
28290 5f 73 74 65 70 28 53 29 5d 20 66 6f 72 20 74 68 _step(S)] for th
282a0 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 70 e.** [p
282b0 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
282c0 74 5d 20 53 20 72 65 74 75 72 6e 65 64 20 5b 53 t] S returned [S
282d0 51 4c 49 54 45 5f 52 4f 57 5d 20 6f 72 20 5b 53 QLITE_ROW] or [S
282e0 51 4c 49 54 45 5f 44 4f 4e 45 5d 2c 0a 2a 2a 20 QLITE_DONE],.**
282f0 20 20 20 20 20 20 20 20 20 6f 72 20 69 66 20 5b or if [
28300 73 71 6c 69 74 65 33 5f 73 74 65 70 28 53 29 5d sqlite3_step(S)]
28310 20 68 61 73 20 6e 65 76 65 72 20 62 65 66 6f 72 has never befor
28320 65 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 6f 6e e been called on
28330 20 53 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 S,.**
28340 74 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f 72 65 then [sqlite3_re
28350 73 65 74 28 53 29 5d 20 72 65 74 75 72 6e 73 20 set(S)] returns
28360 5b 53 51 4c 49 54 45 5f 4f 4b 5d 2e 0a 2a 2a 0a [SQLITE_OK]..**.
28370 2a 2a 20 7b 48 31 31 33 33 36 7d 20 49 66 20 74 ** {H11336} If t
28380 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 63 he most recent c
28390 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f all to [sqlite3_
283a0 73 74 65 70 28 53 29 5d 20 66 6f 72 20 74 68 65 step(S)] for the
283b0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 70 72 .** [pr
283c0 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 epared statement
283d0 5d 20 53 20 69 6e 64 69 63 61 74 65 64 20 61 6e ] S indicated an
283e0 20 65 72 72 6f 72 2c 20 74 68 65 6e 0a 2a 2a 20 error, then.**
283f0 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 [sqlite
28400 33 5f 72 65 73 65 74 28 53 29 5d 20 72 65 74 75 3_reset(S)] retu
28410 72 6e 73 20 61 6e 20 61 70 70 72 6f 70 72 69 61 rns an appropria
28420 74 65 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 2e te [error code].
28430 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 33 33 38 7d 20 .**.** {H11338}
28440 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 The [sqlite3_res
28450 65 74 28 53 29 5d 20 69 6e 74 65 72 66 61 63 65 et(S)] interface
28460 20 64 6f 65 73 20 6e 6f 74 20 63 68 61 6e 67 65 does not change
28470 20 74 68 65 20 76 61 6c 75 65 73 0a 2a 2a 20 20 the values.**
28480 20 20 20 20 20 20 20 20 6f 66 20 61 6e 79 20 5b of any [
28490 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f sqlite3_bind_blo
284a0 62 7c 62 69 6e 64 69 6e 67 73 5d 20 6f 6e 20 74 b|bindings] on t
284b0 68 65 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 he [prepared sta
284c0 74 65 6d 65 6e 74 5d 20 53 2e 0a 2a 2f 0a 53 51 tement] S..*/.SQ
284d0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
284e0 69 74 65 33 5f 72 65 73 65 74 28 73 71 6c 69 74 ite3_reset(sqlit
284f0 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 3b e3_stmt *pStmt);
28500 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
28510 3a 20 43 72 65 61 74 65 20 4f 72 20 52 65 64 65 : Create Or Rede
28520 66 69 6e 65 20 53 51 4c 20 46 75 6e 63 74 69 6f fine SQL Functio
28530 6e 73 20 7b 48 31 36 31 30 30 7d 20 3c 53 32 30 ns {H16100} <S20
28540 32 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 200>.** KEYWORDS
28550 3a 20 7b 66 75 6e 63 74 69 6f 6e 20 63 72 65 61 : {function crea
28560 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73 7d 0a 2a tion routines}.*
28570 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 61 70 70 * KEYWORDS: {app
28580 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 lication-defined
28590 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 7d 0a 2a SQL function}.*
285a0 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 61 70 70 * KEYWORDS: {app
285b0 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 lication-defined
285c0 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 7d 0a SQL functions}.
285d0 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 74 77 6f 20 **.** These two
285e0 66 75 6e 63 74 69 6f 6e 73 20 28 63 6f 6c 6c 65 functions (colle
285f0 63 74 69 76 65 6c 79 20 6b 6e 6f 77 6e 20 61 73 ctively known as
28600 20 22 66 75 6e 63 74 69 6f 6e 20 63 72 65 61 74 "function creat
28610 69 6f 6e 20 72 6f 75 74 69 6e 65 73 22 29 0a 2a ion routines").*
28620 2a 20 61 72 65 20 75 73 65 64 20 74 6f 20 61 64 * are used to ad
28630 64 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20 d SQL functions
28640 6f 72 20 61 67 67 72 65 67 61 74 65 73 20 6f 72 or aggregates or
28650 20 74 6f 20 72 65 64 65 66 69 6e 65 20 74 68 65 to redefine the
28660 20 62 65 68 61 76 69 6f 72 0a 2a 2a 20 6f 66 20 behavior.** of
28670 65 78 69 73 74 69 6e 67 20 53 51 4c 20 66 75 6e existing SQL fun
28680 63 74 69 6f 6e 73 20 6f 72 20 61 67 67 72 65 67 ctions or aggreg
28690 61 74 65 73 2e 20 20 54 68 65 20 6f 6e 6c 79 20 ates. The only
286a0 64 69 66 66 65 72 65 6e 63 65 20 62 65 74 77 65 difference betwe
286b0 65 6e 20 74 68 65 0a 2a 2a 20 74 77 6f 20 69 73 en the.** two is
286c0 20 74 68 61 74 20 74 68 65 20 73 65 63 6f 6e 64 that the second
286d0 20 70 61 72 61 6d 65 74 65 72 2c 20 74 68 65 20 parameter, the
286e0 6e 61 6d 65 20 6f 66 20 74 68 65 20 28 73 63 61 name of the (sca
286f0 6c 61 72 29 20 66 75 6e 63 74 69 6f 6e 20 6f 72 lar) function or
28700 0a 2a 2a 20 61 67 67 72 65 67 61 74 65 2c 20 69 .** aggregate, i
28710 73 20 65 6e 63 6f 64 65 64 20 69 6e 20 55 54 46 s encoded in UTF
28720 2d 38 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 63 -8 for sqlite3_c
28730 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 29 reate_function()
28740 20 61 6e 64 20 55 54 46 2d 31 36 0a 2a 2a 20 66 and UTF-16.** f
28750 6f 72 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 or sqlite3_creat
28760 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28 29 2e 0a e_function16()..
28770 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 **.** The first
28780 70 61 72 61 6d 65 74 65 72 20 69 73 20 74 68 65 parameter is the
28790 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 [database conne
287a0 63 74 69 6f 6e 5d 20 74 6f 20 77 68 69 63 68 20 ction] to which
287b0 74 68 65 20 53 51 4c 0a 2a 2a 20 66 75 6e 63 74 the SQL.** funct
287c0 69 6f 6e 20 69 73 20 74 6f 20 62 65 20 61 64 64 ion is to be add
287d0 65 64 2e 20 20 49 66 20 61 20 73 69 6e 67 6c 65 ed. If a single
287e0 20 70 72 6f 67 72 61 6d 20 75 73 65 73 20 6d 6f program uses mo
287f0 72 65 20 74 68 61 6e 20 6f 6e 65 20 64 61 74 61 re than one data
28800 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 base.** connecti
28810 6f 6e 20 69 6e 74 65 72 6e 61 6c 6c 79 2c 20 74 on internally, t
28820 68 65 6e 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e hen SQL function
28830 73 20 6d 75 73 74 20 62 65 20 61 64 64 65 64 20 s must be added
28840 69 6e 64 69 76 69 64 75 61 6c 6c 79 20 74 6f 0a individually to.
28850 2a 2a 20 65 61 63 68 20 64 61 74 61 62 61 73 65 ** each database
28860 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2a 0a connection..**.
28870 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 70 61 ** The second pa
28880 72 61 6d 65 74 65 72 20 69 73 20 74 68 65 20 6e rameter is the n
28890 61 6d 65 20 6f 66 20 74 68 65 20 53 51 4c 20 66 ame of the SQL f
288a0 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 63 72 unction to be cr
288b0 65 61 74 65 64 20 6f 72 0a 2a 2a 20 72 65 64 65 eated or.** rede
288c0 66 69 6e 65 64 2e 20 20 54 68 65 20 6c 65 6e 67 fined. The leng
288d0 74 68 20 6f 66 20 74 68 65 20 6e 61 6d 65 20 69 th of the name i
288e0 73 20 6c 69 6d 69 74 65 64 20 74 6f 20 32 35 35 s limited to 255
288f0 20 62 79 74 65 73 2c 20 65 78 63 6c 75 73 69 76 bytes, exclusiv
28900 65 20 6f 66 0a 2a 2a 20 74 68 65 20 7a 65 72 6f e of.** the zero
28910 2d 74 65 72 6d 69 6e 61 74 6f 72 2e 20 20 4e 6f -terminator. No
28920 74 65 20 74 68 61 74 20 74 68 65 20 6e 61 6d 65 te that the name
28930 20 6c 65 6e 67 74 68 20 6c 69 6d 69 74 20 69 73 length limit is
28940 20 69 6e 20 62 79 74 65 73 2c 20 6e 6f 74 0a 2a in bytes, not.*
28950 2a 20 63 68 61 72 61 63 74 65 72 73 2e 20 20 41 * characters. A
28960 6e 79 20 61 74 74 65 6d 70 74 20 74 6f 20 63 72 ny attempt to cr
28970 65 61 74 65 20 61 20 66 75 6e 63 74 69 6f 6e 20 eate a function
28980 77 69 74 68 20 61 20 6c 6f 6e 67 65 72 20 6e 61 with a longer na
28990 6d 65 0a 2a 2a 20 77 69 6c 6c 20 72 65 73 75 6c me.** will resul
289a0 74 20 69 6e 20 5b 53 51 4c 49 54 45 5f 45 52 52 t in [SQLITE_ERR
289b0 4f 52 5d 20 62 65 69 6e 67 20 72 65 74 75 72 6e OR] being return
289c0 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 68 ed..**.** The th
289d0 69 72 64 20 70 61 72 61 6d 65 74 65 72 20 28 6e ird parameter (n
289e0 41 72 67 29 0a 2a 2a 20 69 73 20 74 68 65 20 6e Arg).** is the n
289f0 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e umber of argumen
28a00 74 73 20 74 68 61 74 20 74 68 65 20 53 51 4c 20 ts that the SQL
28a10 66 75 6e 63 74 69 6f 6e 20 6f 72 0a 2a 2a 20 61 function or.** a
28a20 67 67 72 65 67 61 74 65 20 74 61 6b 65 73 2e 20 ggregate takes.
28a30 49 66 20 74 68 69 73 20 70 61 72 61 6d 65 74 65 If this paramete
28a40 72 20 69 73 20 2d 31 2c 20 74 68 65 6e 20 74 68 r is -1, then th
28a50 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 6f e SQL function o
28a60 72 0a 2a 2a 20 61 67 67 72 65 67 61 74 65 20 6d r.** aggregate m
28a70 61 79 20 74 61 6b 65 20 61 6e 79 20 6e 75 6d 62 ay take any numb
28a80 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 er of arguments
28a90 62 65 74 77 65 65 6e 20 30 20 61 6e 64 20 74 68 between 0 and th
28aa0 65 20 6c 69 6d 69 74 0a 2a 2a 20 73 65 74 20 62 e limit.** set b
28ab0 79 20 5b 73 71 6c 69 74 65 33 5f 6c 69 6d 69 74 y [sqlite3_limit
28ac0 5d 28 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f ]([SQLITE_LIMIT_
28ad0 46 55 4e 43 54 49 4f 4e 5f 41 52 47 5d 29 2e 20 FUNCTION_ARG]).
28ae0 20 49 66 20 74 68 65 20 74 68 69 72 64 0a 2a 2a If the third.**
28af0 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 6c 65 parameter is le
28b00 73 73 20 74 68 61 6e 20 2d 31 20 6f 72 20 67 72 ss than -1 or gr
28b10 65 61 74 65 72 20 74 68 61 6e 20 31 32 37 20 74 eater than 127 t
28b20 68 65 6e 20 74 68 65 20 62 65 68 61 76 69 6f 72 hen the behavior
28b30 20 69 73 0a 2a 2a 20 75 6e 64 65 66 69 6e 65 64 is.** undefined
28b40 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 75 72 ..**.** The four
28b50 74 68 20 70 61 72 61 6d 65 74 65 72 2c 20 65 54 th parameter, eT
28b60 65 78 74 52 65 70 2c 20 73 70 65 63 69 66 69 65 extRep, specifie
28b70 73 20 77 68 61 74 0a 2a 2a 20 5b 53 51 4c 49 54 s what.** [SQLIT
28b80 45 5f 55 54 46 38 20 7c 20 74 65 78 74 20 65 6e E_UTF8 | text en
28b90 63 6f 64 69 6e 67 5d 20 74 68 69 73 20 53 51 4c coding] this SQL
28ba0 20 66 75 6e 63 74 69 6f 6e 20 70 72 65 66 65 72 function prefer
28bb0 73 20 66 6f 72 0a 2a 2a 20 69 74 73 20 70 61 72 s for.** its par
28bc0 61 6d 65 74 65 72 73 2e 20 20 41 6e 79 20 53 51 ameters. Any SQ
28bd0 4c 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70 6c 65 L function imple
28be0 6d 65 6e 74 61 74 69 6f 6e 20 73 68 6f 75 6c 64 mentation should
28bf0 20 62 65 20 61 62 6c 65 20 74 6f 20 77 6f 72 6b be able to work
28c00 0a 2a 2a 20 77 6f 72 6b 20 77 69 74 68 20 55 54 .** work with UT
28c10 46 2d 38 2c 20 55 54 46 2d 31 36 6c 65 2c 20 6f F-8, UTF-16le, o
28c20 72 20 55 54 46 2d 31 36 62 65 2e 20 20 42 75 74 r UTF-16be. But
28c30 20 73 6f 6d 65 20 69 6d 70 6c 65 6d 65 6e 74 61 some implementa
28c40 74 69 6f 6e 73 20 6d 61 79 20 62 65 0a 2a 2a 20 tions may be.**
28c50 6d 6f 72 65 20 65 66 66 69 63 69 65 6e 74 20 77 more efficient w
28c60 69 74 68 20 6f 6e 65 20 65 6e 63 6f 64 69 6e 67 ith one encoding
28c70 20 74 68 61 6e 20 61 6e 6f 74 68 65 72 2e 20 20 than another.
28c80 41 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d An application m
28c90 61 79 0a 2a 2a 20 69 6e 76 6f 6b 65 20 73 71 6c ay.** invoke sql
28ca0 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 ite3_create_func
28cb0 74 69 6f 6e 28 29 20 6f 72 20 73 71 6c 69 74 65 tion() or sqlite
28cc0 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 3_create_functio
28cd0 6e 31 36 28 29 20 6d 75 6c 74 69 70 6c 65 0a 2a n16() multiple.*
28ce0 2a 20 74 69 6d 65 73 20 77 69 74 68 20 74 68 65 * times with the
28cf0 20 73 61 6d 65 20 66 75 6e 63 74 69 6f 6e 20 62 same function b
28d00 75 74 20 77 69 74 68 20 64 69 66 66 65 72 65 6e ut with differen
28d10 74 20 76 61 6c 75 65 73 20 6f 66 20 65 54 65 78 t values of eTex
28d20 74 52 65 70 2e 0a 2a 2a 20 57 68 65 6e 20 6d 75 tRep..** When mu
28d30 6c 74 69 70 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 ltiple implement
28d40 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 73 61 ations of the sa
28d50 6d 65 20 66 75 6e 63 74 69 6f 6e 20 61 72 65 20 me function are
28d60 61 76 61 69 6c 61 62 6c 65 2c 20 53 51 4c 69 74 available, SQLit
28d70 65 0a 2a 2a 20 77 69 6c 6c 20 70 69 63 6b 20 74 e.** will pick t
28d80 68 65 20 6f 6e 65 20 74 68 61 74 20 69 6e 76 6f he one that invo
28d90 6c 76 65 73 20 74 68 65 20 6c 65 61 73 74 20 61 lves the least a
28da0 6d 6f 75 6e 74 20 6f 66 20 64 61 74 61 20 63 6f mount of data co
28db0 6e 76 65 72 73 69 6f 6e 2e 0a 2a 2a 20 49 66 20 nversion..** If
28dc0 74 68 65 72 65 20 69 73 20 6f 6e 6c 79 20 61 20 there is only a
28dd0 73 69 6e 67 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 single implement
28de0 61 74 69 6f 6e 20 77 68 69 63 68 20 64 6f 65 73 ation which does
28df0 20 6e 6f 74 20 63 61 72 65 20 77 68 61 74 20 74 not care what t
28e00 65 78 74 0a 2a 2a 20 65 6e 63 6f 64 69 6e 67 20 ext.** encoding
28e10 69 73 20 75 73 65 64 2c 20 74 68 65 6e 20 74 68 is used, then th
28e20 65 20 66 6f 75 72 74 68 20 61 72 67 75 6d 65 6e e fourth argumen
28e30 74 20 73 68 6f 75 6c 64 20 62 65 20 5b 53 51 4c t should be [SQL
28e40 49 54 45 5f 41 4e 59 5d 2e 0a 2a 2a 0a 2a 2a 20 ITE_ANY]..**.**
28e50 54 68 65 20 66 69 66 74 68 20 70 61 72 61 6d 65 The fifth parame
28e60 74 65 72 20 69 73 20 61 6e 20 61 72 62 69 74 72 ter is an arbitr
28e70 61 72 79 20 70 6f 69 6e 74 65 72 2e 20 20 54 68 ary pointer. Th
28e80 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e e implementation
28e90 20 6f 66 20 74 68 65 0a 2a 2a 20 66 75 6e 63 74 of the.** funct
28ea0 69 6f 6e 20 63 61 6e 20 67 61 69 6e 20 61 63 63 ion can gain acc
28eb0 65 73 73 20 74 6f 20 74 68 69 73 20 70 6f 69 6e ess to this poin
28ec0 74 65 72 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 ter using [sqlit
28ed0 65 33 5f 75 73 65 72 5f 64 61 74 61 28 29 5d 2e e3_user_data()].
28ee0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 76 65 6e .**.** The seven
28ef0 74 68 2c 20 65 69 67 68 74 68 20 61 6e 64 20 6e th, eighth and n
28f00 69 6e 74 68 20 70 61 72 61 6d 65 74 65 72 73 2c inth parameters,
28f10 20 78 46 75 6e 63 2c 20 78 53 74 65 70 20 61 6e xFunc, xStep an
28f20 64 20 78 46 69 6e 61 6c 2c 20 61 72 65 0a 2a 2a d xFinal, are.**
28f30 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 43 2d 6c pointers to C-l
28f40 61 6e 67 75 61 67 65 20 66 75 6e 63 74 69 6f 6e anguage function
28f50 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 s that implement
28f60 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f the SQL functio
28f70 6e 20 6f 72 0a 2a 2a 20 61 67 67 72 65 67 61 74 n or.** aggregat
28f80 65 2e 20 41 20 73 63 61 6c 61 72 20 53 51 4c 20 e. A scalar SQL
28f90 66 75 6e 63 74 69 6f 6e 20 72 65 71 75 69 72 65 function require
28fa0 73 20 61 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 s an implementat
28fb0 69 6f 6e 20 6f 66 20 74 68 65 20 78 46 75 6e 63 ion of the xFunc
28fc0 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 6f 6e 6c .** callback onl
28fd0 79 2c 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 73 y, NULL pointers
28fe0 20 73 68 6f 75 6c 64 20 62 65 20 70 61 73 73 65 should be passe
28ff0 64 20 61 73 20 74 68 65 20 78 53 74 65 70 20 61 d as the xStep a
29000 6e 64 20 78 46 69 6e 61 6c 0a 2a 2a 20 70 61 72 nd xFinal.** par
29010 61 6d 65 74 65 72 73 2e 20 41 6e 20 61 67 67 72 ameters. An aggr
29020 65 67 61 74 65 20 53 51 4c 20 66 75 6e 63 74 69 egate SQL functi
29030 6f 6e 20 72 65 71 75 69 72 65 73 20 61 6e 20 69 on requires an i
29040 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 mplementation of
29050 20 78 53 74 65 70 0a 2a 2a 20 61 6e 64 20 78 46 xStep.** and xF
29060 69 6e 61 6c 20 61 6e 64 20 4e 55 4c 4c 20 73 68 inal and NULL sh
29070 6f 75 6c 64 20 62 65 20 70 61 73 73 65 64 20 66 ould be passed f
29080 6f 72 20 78 46 75 6e 63 2e 20 54 6f 20 64 65 6c or xFunc. To del
29090 65 74 65 20 61 6e 20 65 78 69 73 74 69 6e 67 0a ete an existing.
290a0 2a 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 ** SQL function
290b0 6f 72 20 61 67 67 72 65 67 61 74 65 2c 20 70 61 or aggregate, pa
290c0 73 73 20 4e 55 4c 4c 20 66 6f 72 20 61 6c 6c 20 ss NULL for all
290d0 74 68 72 65 65 20 66 75 6e 63 74 69 6f 6e 20 63 three function c
290e0 61 6c 6c 62 61 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 allbacks..**.**
290f0 49 74 20 69 73 20 70 65 72 6d 69 74 74 65 64 20 It is permitted
29100 74 6f 20 72 65 67 69 73 74 65 72 20 6d 75 6c 74 to register mult
29110 69 70 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 iple implementat
29120 69 6f 6e 73 20 6f 66 20 74 68 65 20 73 61 6d 65 ions of the same
29130 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 77 69 .** functions wi
29140 74 68 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 th the same name
29150 20 62 75 74 20 77 69 74 68 20 65 69 74 68 65 72 but with either
29160 20 64 69 66 66 65 72 69 6e 67 20 6e 75 6d 62 65 differing numbe
29170 72 73 20 6f 66 0a 2a 2a 20 61 72 67 75 6d 65 6e rs of.** argumen
29180 74 73 20 6f 72 20 64 69 66 66 65 72 69 6e 67 20 ts or differing
29190 70 72 65 66 65 72 72 65 64 20 74 65 78 74 20 65 preferred text e
291a0 6e 63 6f 64 69 6e 67 73 2e 20 20 53 51 4c 69 74 ncodings. SQLit
291b0 65 20 77 69 6c 6c 20 75 73 65 0a 2a 2a 20 74 68 e will use.** th
291c0 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e e implementation
291d0 20 74 68 61 74 20 6d 6f 73 74 20 63 6c 6f 73 65 that most close
291e0 6c 79 20 6d 61 74 63 68 65 73 20 74 68 65 20 77 ly matches the w
291f0 61 79 20 69 6e 20 77 68 69 63 68 20 74 68 65 0a ay in which the.
29200 2a 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 ** SQL function
29210 69 73 20 75 73 65 64 2e 20 20 41 20 66 75 6e 63 is used. A func
29220 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 tion implementat
29230 69 6f 6e 20 77 69 74 68 20 61 20 6e 6f 6e 2d 6e ion with a non-n
29240 65 67 61 74 69 76 65 0a 2a 2a 20 6e 41 72 67 20 egative.** nArg
29250 70 61 72 61 6d 65 74 65 72 20 69 73 20 61 20 62 parameter is a b
29260 65 74 74 65 72 20 6d 61 74 63 68 20 74 68 61 6e etter match than
29270 20 61 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70 6c a function impl
29280 65 6d 65 6e 74 61 74 69 6f 6e 20 77 69 74 68 0a ementation with.
29290 2a 2a 20 61 20 6e 65 67 61 74 69 76 65 20 6e 41 ** a negative nA
292a0 72 67 2e 20 20 41 20 66 75 6e 63 74 69 6f 6e 20 rg. A function
292b0 77 68 65 72 65 20 74 68 65 20 70 72 65 66 65 72 where the prefer
292c0 72 65 64 20 74 65 78 74 20 65 6e 63 6f 64 69 6e red text encodin
292d0 67 0a 2a 2a 20 6d 61 74 63 68 65 73 20 74 68 65 g.** matches the
292e0 20 64 61 74 61 62 61 73 65 20 65 6e 63 6f 64 69 database encodi
292f0 6e 67 20 69 73 20 61 20 62 65 74 74 65 72 0a 2a ng is a better.*
29300 2a 20 6d 61 74 63 68 20 74 68 61 6e 20 61 20 66 * match than a f
29310 75 6e 63 74 69 6f 6e 20 77 68 65 72 65 20 74 68 unction where th
29320 65 20 65 6e 63 6f 64 69 6e 67 20 69 73 20 64 69 e encoding is di
29330 66 66 65 72 65 6e 74 2e 20 20 0a 2a 2a 20 41 20 fferent. .** A
29340 66 75 6e 63 74 69 6f 6e 20 77 68 65 72 65 20 74 function where t
29350 68 65 20 65 6e 63 6f 64 69 6e 67 20 64 69 66 66 he encoding diff
29360 65 72 65 6e 63 65 20 69 73 20 62 65 74 77 65 65 erence is betwee
29370 6e 20 55 54 46 31 36 6c 65 20 61 6e 64 20 55 54 n UTF16le and UT
29380 46 31 36 62 65 0a 2a 2a 20 69 73 20 61 20 63 6c F16be.** is a cl
29390 6f 73 65 72 20 6d 61 74 63 68 20 74 68 61 6e 20 oser match than
293a0 61 20 66 75 6e 63 74 69 6f 6e 20 77 68 65 72 65 a function where
293b0 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 64 69 the encoding di
293c0 66 66 65 72 65 6e 63 65 20 69 73 0a 2a 2a 20 62 fference is.** b
293d0 65 74 77 65 65 6e 20 55 54 46 38 20 61 6e 64 20 etween UTF8 and
293e0 55 54 46 31 36 2e 0a 2a 2a 0a 2a 2a 20 42 75 69 UTF16..**.** Bui
293f0 6c 74 2d 69 6e 20 66 75 6e 63 74 69 6f 6e 73 20 lt-in functions
29400 6d 61 79 20 62 65 20 6f 76 65 72 6c 6f 61 64 65 may be overloade
29410 64 20 62 79 20 6e 65 77 20 61 70 70 6c 69 63 61 d by new applica
29420 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e tion-defined fun
29430 63 74 69 6f 6e 73 2e 0a 2a 2a 20 54 68 65 20 66 ctions..** The f
29440 69 72 73 74 20 61 70 70 6c 69 63 61 74 69 6f 6e irst application
29450 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f -defined functio
29460 6e 20 77 69 74 68 20 61 20 67 69 76 65 6e 20 6e n with a given n
29470 61 6d 65 20 6f 76 65 72 72 69 64 65 73 20 61 6c ame overrides al
29480 6c 0a 2a 2a 20 62 75 69 6c 74 2d 69 6e 20 66 75 l.** built-in fu
29490 6e 63 74 69 6f 6e 73 20 69 6e 20 74 68 65 20 73 nctions in the s
294a0 61 6d 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f ame [database co
294b0 6e 6e 65 63 74 69 6f 6e 5d 20 77 69 74 68 20 74 nnection] with t
294c0 68 65 20 73 61 6d 65 20 6e 61 6d 65 2e 0a 2a 2a he same name..**
294d0 20 53 75 62 73 65 71 75 65 6e 74 20 61 70 70 6c Subsequent appl
294e0 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 ication-defined
294f0 66 75 6e 63 74 69 6f 6e 73 20 6f 66 20 74 68 65 functions of the
29500 20 73 61 6d 65 20 6e 61 6d 65 20 6f 6e 6c 79 20 same name only
29510 6f 76 65 72 72 69 64 65 20 0a 2a 2a 20 70 72 69 override .** pri
29520 6f 72 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 or application-d
29530 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 73 efined functions
29540 20 74 68 61 74 20 61 72 65 20 61 6e 20 65 78 61 that are an exa
29550 63 74 20 6d 61 74 63 68 20 66 6f 72 20 74 68 65 ct match for the
29560 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 .** number of pa
29570 72 61 6d 65 74 65 72 73 20 61 6e 64 20 70 72 65 rameters and pre
29580 66 65 72 72 65 64 20 65 6e 63 6f 64 69 6e 67 2e ferred encoding.
29590 0a 2a 2a 0a 2a 2a 20 41 6e 20 61 70 70 6c 69 63 .**.** An applic
295a0 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 ation-defined fu
295b0 6e 63 74 69 6f 6e 20 69 73 20 70 65 72 6d 69 74 nction is permit
295c0 74 65 64 20 74 6f 20 63 61 6c 6c 20 6f 74 68 65 ted to call othe
295d0 72 0a 2a 2a 20 53 51 4c 69 74 65 20 69 6e 74 65 r.** SQLite inte
295e0 72 66 61 63 65 73 2e 20 20 48 6f 77 65 76 65 72 rfaces. However
295f0 2c 20 73 75 63 68 20 63 61 6c 6c 73 20 6d 75 73 , such calls mus
29600 74 20 6e 6f 74 0a 2a 2a 20 63 6c 6f 73 65 20 74 t not.** close t
29610 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e he database conn
29620 65 63 74 69 6f 6e 20 6e 6f 72 20 66 69 6e 61 6c ection nor final
29630 69 7a 65 20 6f 72 20 72 65 73 65 74 20 74 68 65 ize or reset the
29640 20 70 72 65 70 61 72 65 64 0a 2a 2a 20 73 74 61 prepared.** sta
29650 74 65 6d 65 6e 74 20 69 6e 20 77 68 69 63 68 20 tement in which
29660 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 the function is
29670 72 75 6e 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 52 running..**.** R
29680 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 equirements:.**
29690 5b 48 31 36 31 30 33 5d 20 5b 48 31 36 31 30 36 [H16103] [H16106
296a0 5d 20 5b 48 31 36 31 30 39 5d 20 5b 48 31 36 31 ] [H16109] [H161
296b0 31 32 5d 20 5b 48 31 36 31 31 38 5d 20 5b 48 31 12] [H16118] [H1
296c0 36 31 32 31 5d 20 5b 48 31 36 31 32 37 5d 0a 2a 6121] [H16127].*
296d0 2a 20 5b 48 31 36 31 33 30 5d 20 5b 48 31 36 31 * [H16130] [H161
296e0 33 33 5d 20 5b 48 31 36 31 33 36 5d 20 5b 48 31 33] [H16136] [H1
296f0 36 31 33 39 5d 20 5b 48 31 36 31 34 32 5d 0a 2a 6139] [H16142].*
29700 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
29710 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f sqlite3_create_
29720 66 75 6e 63 74 69 6f 6e 28 0a 20 20 73 71 6c 69 function(. sqli
29730 74 65 33 20 2a 64 62 2c 0a 20 20 63 6f 6e 73 74 te3 *db,. const
29740 20 63 68 61 72 20 2a 7a 46 75 6e 63 74 69 6f 6e char *zFunction
29750 4e 61 6d 65 2c 0a 20 20 69 6e 74 20 6e 41 72 67 Name,. int nArg
29760 2c 0a 20 20 69 6e 74 20 65 54 65 78 74 52 65 70 ,. int eTextRep
29770 2c 0a 20 20 76 6f 69 64 20 2a 70 41 70 70 2c 0a ,. void *pApp,.
29780 20 20 76 6f 69 64 20 28 2a 78 46 75 6e 63 29 28 void (*xFunc)(
29790 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a sqlite3_context*
297a0 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c ,int,sqlite3_val
297b0 75 65 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a ue**),. void (*
297c0 78 53 74 65 70 29 28 73 71 6c 69 74 65 33 5f 63 xStep)(sqlite3_c
297d0 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 ontext*,int,sqli
297e0 74 65 33 5f 76 61 6c 75 65 2a 2a 29 2c 0a 20 20 te3_value**),.
297f0 76 6f 69 64 20 28 2a 78 46 69 6e 61 6c 29 28 73 void (*xFinal)(s
29800 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 qlite3_context*)
29810 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .);.SQLITE_API i
29820 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 nt sqlite3_creat
29830 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28 0a 20 20 e_function16(.
29840 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 63 sqlite3 *db,. c
29850 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 46 75 6e 63 onst void *zFunc
29860 74 69 6f 6e 4e 61 6d 65 2c 0a 20 20 69 6e 74 20 tionName,. int
29870 6e 41 72 67 2c 0a 20 20 69 6e 74 20 65 54 65 78 nArg,. int eTex
29880 74 52 65 70 2c 0a 20 20 76 6f 69 64 20 2a 70 41 tRep,. void *pA
29890 70 70 2c 0a 20 20 76 6f 69 64 20 28 2a 78 46 75 pp,. void (*xFu
298a0 6e 63 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 nc)(sqlite3_cont
298b0 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 ext*,int,sqlite3
298c0 5f 76 61 6c 75 65 2a 2a 29 2c 0a 20 20 76 6f 69 _value**),. voi
298d0 64 20 28 2a 78 53 74 65 70 29 28 73 71 6c 69 74 d (*xStep)(sqlit
298e0 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c e3_context*,int,
298f0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 sqlite3_value**)
29900 2c 0a 20 20 76 6f 69 64 20 28 2a 78 46 69 6e 61 ,. void (*xFina
29910 6c 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 l)(sqlite3_conte
29920 78 74 2a 29 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 xt*).);../*.** C
29930 41 50 49 33 52 45 46 3a 20 54 65 78 74 20 45 6e API3REF: Text En
29940 63 6f 64 69 6e 67 73 20 7b 48 31 30 32 36 37 7d codings {H10267}
29950 20 3c 53 35 30 32 30 30 3e 20 3c 48 31 36 31 30 <S50200> <H1610
29960 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 63 0>.**.** These c
29970 6f 6e 73 74 61 6e 74 20 64 65 66 69 6e 65 20 69 onstant define i
29980 6e 74 65 67 65 72 20 63 6f 64 65 73 20 74 68 61 nteger codes tha
29990 74 20 72 65 70 72 65 73 65 6e 74 20 74 68 65 20 t represent the
299a0 76 61 72 69 6f 75 73 0a 2a 2a 20 74 65 78 74 20 various.** text
299b0 65 6e 63 6f 64 69 6e 67 73 20 73 75 70 70 6f 72 encodings suppor
299c0 74 65 64 20 62 79 20 53 51 4c 69 74 65 2e 0a 2a ted by SQLite..*
299d0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
299e0 5f 55 54 46 38 20 20 20 20 20 20 20 20 20 20 20 _UTF8
299f0 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 1.#define SQLITE
29a00 5f 55 54 46 31 36 4c 45 20 20 20 20 20 20 20 20 _UTF16LE
29a10 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 2.#define SQLITE
29a20 5f 55 54 46 31 36 42 45 20 20 20 20 20 20 20 20 _UTF16BE
29a30 33 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 3.#define SQLITE
29a40 5f 55 54 46 31 36 20 20 20 20 20 20 20 20 20 20 _UTF16
29a50 34 20 20 20 20 2f 2a 20 55 73 65 20 6e 61 74 69 4 /* Use nati
29a60 76 65 20 62 79 74 65 20 6f 72 64 65 72 20 2a 2f ve byte order */
29a70 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
29a80 41 4e 59 20 20 20 20 20 20 20 20 20 20 20 20 35 ANY 5
29a90 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 63 /* sqlite3_c
29aa0 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 6f reate_function o
29ab0 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 nly */.#define S
29ac0 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 QLITE_UTF16_ALIG
29ad0 4e 45 44 20 20 38 20 20 20 20 2f 2a 20 73 71 6c NED 8 /* sql
29ae0 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c ite3_create_coll
29af0 61 74 69 6f 6e 20 6f 6e 6c 79 20 2a 2f 0a 0a 2f ation only */../
29b00 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44 *.** CAPI3REF: D
29b10 65 70 72 65 63 61 74 65 64 20 46 75 6e 63 74 69 eprecated Functi
29b20 6f 6e 73 0a 2a 2a 20 44 45 50 52 45 43 41 54 45 ons.** DEPRECATE
29b30 44 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 66 75 D.**.** These fu
29b40 6e 63 74 69 6f 6e 73 20 61 72 65 20 5b 64 65 70 nctions are [dep
29b50 72 65 63 61 74 65 64 5d 2e 20 20 49 6e 20 6f 72 recated]. In or
29b60 64 65 72 20 74 6f 20 6d 61 69 6e 74 61 69 6e 0a der to maintain.
29b70 2a 2a 20 62 61 63 6b 77 61 72 64 73 20 63 6f 6d ** backwards com
29b80 70 61 74 69 62 69 6c 69 74 79 20 77 69 74 68 20 patibility with
29b90 6f 6c 64 65 72 20 63 6f 64 65 2c 20 74 68 65 73 older code, thes
29ba0 65 20 66 75 6e 63 74 69 6f 6e 73 20 63 6f 6e 74 e functions cont
29bb0 69 6e 75 65 20 0a 2a 2a 20 74 6f 20 62 65 20 73 inue .** to be s
29bc0 75 70 70 6f 72 74 65 64 2e 20 20 48 6f 77 65 76 upported. Howev
29bd0 65 72 2c 20 6e 65 77 20 61 70 70 6c 69 63 61 74 er, new applicat
29be0 69 6f 6e 73 20 73 68 6f 75 6c 64 20 61 76 6f 69 ions should avoi
29bf0 64 0a 2a 2a 20 74 68 65 20 75 73 65 20 6f 66 20 d.** the use of
29c00 74 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 2e these functions.
29c10 20 20 54 6f 20 68 65 6c 70 20 65 6e 63 6f 75 72 To help encour
29c20 61 67 65 20 70 65 6f 70 6c 65 20 74 6f 20 61 76 age people to av
29c30 6f 69 64 0a 2a 2a 20 75 73 69 6e 67 20 74 68 65 oid.** using the
29c40 73 65 20 66 75 6e 63 74 69 6f 6e 73 2c 20 77 65 se functions, we
29c50 20 61 72 65 20 6e 6f 74 20 67 6f 69 6e 67 20 74 are not going t
29c60 6f 20 74 65 6c 6c 20 79 6f 75 20 77 68 61 74 20 o tell you what
29c70 74 68 65 79 20 64 6f 2e 0a 2a 2f 0a 23 69 66 6e they do..*/.#ifn
29c80 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
29c90 44 45 50 52 45 43 41 54 45 44 0a 53 51 4c 49 54 DEPRECATED.SQLIT
29ca0 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 44 45 50 E_API SQLITE_DEP
29cb0 52 45 43 41 54 45 44 20 69 6e 74 20 73 71 6c 69 RECATED int sqli
29cc0 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f te3_aggregate_co
29cd0 75 6e 74 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 unt(sqlite3_cont
29ce0 65 78 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 ext*);.SQLITE_AP
29cf0 49 20 53 51 4c 49 54 45 5f 44 45 50 52 45 43 41 I SQLITE_DEPRECA
29d00 54 45 44 20 69 6e 74 20 73 71 6c 69 74 65 33 5f TED int sqlite3_
29d10 65 78 70 69 72 65 64 28 73 71 6c 69 74 65 33 5f expired(sqlite3_
29d20 73 74 6d 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 stmt*);.SQLITE_A
29d30 50 49 20 53 51 4c 49 54 45 5f 44 45 50 52 45 43 PI SQLITE_DEPREC
29d40 41 54 45 44 20 69 6e 74 20 73 71 6c 69 74 65 33 ATED int sqlite3
29d50 5f 74 72 61 6e 73 66 65 72 5f 62 69 6e 64 69 6e _transfer_bindin
29d60 67 73 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a gs(sqlite3_stmt*
29d70 2c 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 , sqlite3_stmt*)
29d80 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c ;.SQLITE_API SQL
29d90 49 54 45 5f 44 45 50 52 45 43 41 54 45 44 20 69 ITE_DEPRECATED i
29da0 6e 74 20 73 71 6c 69 74 65 33 5f 67 6c 6f 62 61 nt sqlite3_globa
29db0 6c 5f 72 65 63 6f 76 65 72 28 76 6f 69 64 29 3b l_recover(void);
29dc0 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 .SQLITE_API SQLI
29dd0 54 45 5f 44 45 50 52 45 43 41 54 45 44 20 76 6f TE_DEPRECATED vo
29de0 69 64 20 73 71 6c 69 74 65 33 5f 74 68 72 65 61 id sqlite3_threa
29df0 64 5f 63 6c 65 61 6e 75 70 28 76 6f 69 64 29 3b d_cleanup(void);
29e00 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 .SQLITE_API SQLI
29e10 54 45 5f 44 45 50 52 45 43 41 54 45 44 20 69 6e TE_DEPRECATED in
29e20 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 t sqlite3_memory
29e30 5f 61 6c 61 72 6d 28 76 6f 69 64 28 2a 29 28 76 _alarm(void(*)(v
29e40 6f 69 64 2a 2c 73 71 6c 69 74 65 33 5f 69 6e 74 oid*,sqlite3_int
29e50 36 34 2c 69 6e 74 29 2c 76 6f 69 64 2a 2c 73 71 64,int),void*,sq
29e60 6c 69 74 65 33 5f 69 6e 74 36 34 29 3b 0a 23 65 lite3_int64);.#e
29e70 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 ndif../*.** CAPI
29e80 33 52 45 46 3a 20 4f 62 74 61 69 6e 69 6e 67 20 3REF: Obtaining
29e90 53 51 4c 20 46 75 6e 63 74 69 6f 6e 20 50 61 72 SQL Function Par
29ea0 61 6d 65 74 65 72 20 56 61 6c 75 65 73 20 7b 48 ameter Values {H
29eb0 31 35 31 30 30 7d 20 3c 53 32 30 32 30 30 3e 0a 15100} <S20200>.
29ec0 2a 2a 0a 2a 2a 20 54 68 65 20 43 2d 6c 61 6e 67 **.** The C-lang
29ed0 75 61 67 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 uage implementat
29ee0 69 6f 6e 20 6f 66 20 53 51 4c 20 66 75 6e 63 74 ion of SQL funct
29ef0 69 6f 6e 73 20 61 6e 64 20 61 67 67 72 65 67 61 ions and aggrega
29f00 74 65 73 20 75 73 65 73 0a 2a 2a 20 74 68 69 73 tes uses.** this
29f10 20 73 65 74 20 6f 66 20 69 6e 74 65 72 66 61 63 set of interfac
29f20 65 20 72 6f 75 74 69 6e 65 73 20 74 6f 20 61 63 e routines to ac
29f30 63 65 73 73 20 74 68 65 20 70 61 72 61 6d 65 74 cess the paramet
29f40 65 72 20 76 61 6c 75 65 73 20 6f 6e 0a 2a 2a 20 er values on.**
29f50 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 6f 72 20 the function or
29f60 61 67 67 72 65 67 61 74 65 2e 0a 2a 2a 0a 2a 2a aggregate..**.**
29f70 20 54 68 65 20 78 46 75 6e 63 20 28 66 6f 72 20 The xFunc (for
29f80 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e 73 scalar functions
29f90 29 20 6f 72 20 78 53 74 65 70 20 28 66 6f 72 20 ) or xStep (for
29fa0 61 67 67 72 65 67 61 74 65 73 29 20 70 61 72 61 aggregates) para
29fb0 6d 65 74 65 72 73 0a 2a 2a 20 74 6f 20 5b 73 71 meters.** to [sq
29fc0 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e lite3_create_fun
29fd0 63 74 69 6f 6e 28 29 5d 20 61 6e 64 20 5b 73 71 ction()] and [sq
29fe0 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e lite3_create_fun
29ff0 63 74 69 6f 6e 31 36 28 29 5d 0a 2a 2a 20 64 65 ction16()].** de
2a000 66 69 6e 65 20 63 61 6c 6c 62 61 63 6b 73 20 74 fine callbacks t
2a010 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 hat implement th
2a020 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20 e SQL functions
2a030 61 6e 64 20 61 67 67 72 65 67 61 74 65 73 2e 0a and aggregates..
2a040 2a 2a 20 54 68 65 20 34 74 68 20 70 61 72 61 6d ** The 4th param
2a050 65 74 65 72 20 74 6f 20 74 68 65 73 65 20 63 61 eter to these ca
2a060 6c 6c 62 61 63 6b 73 20 69 73 20 61 6e 20 61 72 llbacks is an ar
2a070 72 61 79 20 6f 66 20 70 6f 69 6e 74 65 72 73 20 ray of pointers
2a080 74 6f 0a 2a 2a 20 5b 70 72 6f 74 65 63 74 65 64 to.** [protected
2a090 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 sqlite3_value]
2a0a0 6f 62 6a 65 63 74 73 2e 20 20 54 68 65 72 65 20 objects. There
2a0b0 69 73 20 6f 6e 65 20 5b 73 71 6c 69 74 65 33 5f is one [sqlite3_
2a0c0 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20 66 6f value] object fo
2a0d0 72 0a 2a 2a 20 65 61 63 68 20 70 61 72 61 6d 65 r.** each parame
2a0e0 74 65 72 20 74 6f 20 74 68 65 20 53 51 4c 20 66 ter to the SQL f
2a0f0 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 73 65 20 unction. These
2a100 72 6f 75 74 69 6e 65 73 20 61 72 65 20 75 73 65 routines are use
2a110 64 20 74 6f 0a 2a 2a 20 65 78 74 72 61 63 74 20 d to.** extract
2a120 76 61 6c 75 65 73 20 66 72 6f 6d 20 74 68 65 20 values from the
2a130 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 [sqlite3_value]
2a140 6f 62 6a 65 63 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 objects..**.** T
2a150 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 77 6f hese routines wo
2a160 72 6b 20 6f 6e 6c 79 20 77 69 74 68 20 5b 70 72 rk only with [pr
2a170 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f otected sqlite3_
2a180 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 73 2e 0a value] objects..
2a190 2a 2a 20 41 6e 79 20 61 74 74 65 6d 70 74 20 74 ** Any attempt t
2a1a0 6f 20 75 73 65 20 74 68 65 73 65 20 72 6f 75 74 o use these rout
2a1b0 69 6e 65 73 20 6f 6e 20 61 6e 20 5b 75 6e 70 72 ines on an [unpr
2a1c0 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f otected sqlite3_
2a1d0 76 61 6c 75 65 5d 0a 2a 2a 20 6f 62 6a 65 63 74 value].** object
2a1e0 20 72 65 73 75 6c 74 73 20 69 6e 20 75 6e 64 65 results in unde
2a1f0 66 69 6e 65 64 20 62 65 68 61 76 69 6f 72 2e 0a fined behavior..
2a200 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 **.** These rout
2a210 69 6e 65 73 20 77 6f 72 6b 20 6a 75 73 74 20 6c ines work just l
2a220 69 6b 65 20 74 68 65 20 63 6f 72 72 65 73 70 6f ike the correspo
2a230 6e 64 69 6e 67 20 5b 63 6f 6c 75 6d 6e 20 61 63 nding [column ac
2a240 63 65 73 73 20 66 75 6e 63 74 69 6f 6e 73 5d 0a cess functions].
2a250 2a 2a 20 65 78 63 65 70 74 20 74 68 61 74 20 20 ** except that
2a260 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 74 these routines t
2a270 61 6b 65 20 61 20 73 69 6e 67 6c 65 20 5b 70 72 ake a single [pr
2a280 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f otected sqlite3_
2a290 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 0a 2a 2a value] object.**
2a2a0 20 70 6f 69 6e 74 65 72 20 69 6e 73 74 65 61 64 pointer instead
2a2b0 20 6f 66 20 61 20 5b 73 71 6c 69 74 65 33 5f 73 of a [sqlite3_s
2a2c0 74 6d 74 2a 5d 20 70 6f 69 6e 74 65 72 20 61 6e tmt*] pointer an
2a2d0 64 20 61 6e 20 69 6e 74 65 67 65 72 20 63 6f 6c d an integer col
2a2e0 75 6d 6e 20 6e 75 6d 62 65 72 2e 0a 2a 2a 0a 2a umn number..**.*
2a2f0 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 76 61 * The sqlite3_va
2a300 6c 75 65 5f 74 65 78 74 31 36 28 29 20 69 6e 74 lue_text16() int
2a310 65 72 66 61 63 65 20 65 78 74 72 61 63 74 73 20 erface extracts
2a320 61 20 55 54 46 2d 31 36 20 73 74 72 69 6e 67 0a a UTF-16 string.
2a330 2a 2a 20 69 6e 20 74 68 65 20 6e 61 74 69 76 65 ** in the native
2a340 20 62 79 74 65 2d 6f 72 64 65 72 20 6f 66 20 74 byte-order of t
2a350 68 65 20 68 6f 73 74 20 6d 61 63 68 69 6e 65 2e he host machine.
2a360 20 20 54 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 The.** sqlite3
2a370 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 62 65 28 _value_text16be(
2a380 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 76 61 ) and sqlite3_va
2a390 6c 75 65 5f 74 65 78 74 31 36 6c 65 28 29 20 69 lue_text16le() i
2a3a0 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20 65 78 74 nterfaces.** ext
2a3b0 72 61 63 74 20 55 54 46 2d 31 36 20 73 74 72 69 ract UTF-16 stri
2a3c0 6e 67 73 20 61 73 20 62 69 67 2d 65 6e 64 69 61 ngs as big-endia
2a3d0 6e 20 61 6e 64 20 6c 69 74 74 6c 65 2d 65 6e 64 n and little-end
2a3e0 69 61 6e 20 72 65 73 70 65 63 74 69 76 65 6c 79 ian respectively
2a3f0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 ..**.** The sqli
2a400 74 65 33 5f 76 61 6c 75 65 5f 6e 75 6d 65 72 69 te3_value_numeri
2a410 63 5f 74 79 70 65 28 29 20 69 6e 74 65 72 66 61 c_type() interfa
2a420 63 65 20 61 74 74 65 6d 70 74 73 20 74 6f 20 61 ce attempts to a
2a430 70 70 6c 79 0a 2a 2a 20 6e 75 6d 65 72 69 63 20 pply.** numeric
2a440 61 66 66 69 6e 69 74 79 20 74 6f 20 74 68 65 20 affinity to the
2a450 76 61 6c 75 65 2e 20 20 54 68 69 73 20 6d 65 61 value. This mea
2a460 6e 73 20 74 68 61 74 20 61 6e 20 61 74 74 65 6d ns that an attem
2a470 70 74 20 69 73 0a 2a 2a 20 6d 61 64 65 20 74 6f pt is.** made to
2a480 20 63 6f 6e 76 65 72 74 20 74 68 65 20 76 61 6c convert the val
2a490 75 65 20 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 ue to an integer
2a4a0 20 6f 72 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 or floating poi
2a4b0 6e 74 2e 20 20 49 66 0a 2a 2a 20 73 75 63 68 20 nt. If.** such
2a4c0 61 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20 a conversion is
2a4d0 70 6f 73 73 69 62 6c 65 20 77 69 74 68 6f 75 74 possible without
2a4e0 20 6c 6f 73 73 20 6f 66 20 69 6e 66 6f 72 6d 61 loss of informa
2a4f0 74 69 6f 6e 20 28 69 6e 20 6f 74 68 65 72 0a 2a tion (in other.*
2a500 2a 20 77 6f 72 64 73 2c 20 69 66 20 74 68 65 20 * words, if the
2a510 76 61 6c 75 65 20 69 73 20 61 20 73 74 72 69 6e value is a strin
2a520 67 20 74 68 61 74 20 6c 6f 6f 6b 73 20 6c 69 6b g that looks lik
2a530 65 20 61 20 6e 75 6d 62 65 72 29 0a 2a 2a 20 74 e a number).** t
2a540 68 65 6e 20 74 68 65 20 63 6f 6e 76 65 72 73 69 hen the conversi
2a550 6f 6e 20 69 73 20 70 65 72 66 6f 72 6d 65 64 2e on is performed.
2a560 20 20 4f 74 68 65 72 77 69 73 65 20 6e 6f 20 63 Otherwise no c
2a570 6f 6e 76 65 72 73 69 6f 6e 20 6f 63 63 75 72 73 onversion occurs
2a580 2e 0a 2a 2a 20 54 68 65 20 5b 53 51 4c 49 54 45 ..** The [SQLITE
2a590 5f 49 4e 54 45 47 45 52 20 7c 20 64 61 74 61 74 _INTEGER | datat
2a5a0 79 70 65 5d 20 61 66 74 65 72 20 63 6f 6e 76 65 ype] after conve
2a5b0 72 73 69 6f 6e 20 69 73 20 72 65 74 75 72 6e 65 rsion is returne
2a5c0 64 2e 0a 2a 2a 0a 2a 2a 20 50 6c 65 61 73 65 20 d..**.** Please
2a5d0 70 61 79 20 70 61 72 74 69 63 75 6c 61 72 20 61 pay particular a
2a5e0 74 74 65 6e 74 69 6f 6e 20 74 6f 20 74 68 65 20 ttention to the
2a5f0 66 61 63 74 20 74 68 61 74 20 74 68 65 20 70 6f fact that the po
2a600 69 6e 74 65 72 20 72 65 74 75 72 6e 65 64 0a 2a inter returned.*
2a610 2a 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f * from [sqlite3_
2a620 76 61 6c 75 65 5f 62 6c 6f 62 28 29 5d 2c 20 5b value_blob()], [
2a630 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 sqlite3_value_te
2a640 78 74 28 29 5d 2c 20 6f 72 0a 2a 2a 20 5b 73 71 xt()], or.** [sq
2a650 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 lite3_value_text
2a660 31 36 28 29 5d 20 63 61 6e 20 62 65 20 69 6e 76 16()] can be inv
2a670 61 6c 69 64 61 74 65 64 20 62 79 20 61 20 73 75 alidated by a su
2a680 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 20 74 6f bsequent call to
2a690 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c .** [sqlite3_val
2a6a0 75 65 5f 62 79 74 65 73 28 29 5d 2c 20 5b 73 71 ue_bytes()], [sq
2a6b0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 lite3_value_byte
2a6c0 73 31 36 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 s16()], [sqlite3
2a6d0 5f 76 61 6c 75 65 5f 74 65 78 74 28 29 5d 2c 0a _value_text()],.
2a6e0 2a 2a 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 76 ** or [sqlite3_v
2a6f0 61 6c 75 65 5f 74 65 78 74 31 36 28 29 5d 2e 0a alue_text16()]..
2a700 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 **.** These rout
2a710 69 6e 65 73 20 6d 75 73 74 20 62 65 20 63 61 6c ines must be cal
2a720 6c 65 64 20 66 72 6f 6d 20 74 68 65 20 73 61 6d led from the sam
2a730 65 20 74 68 72 65 61 64 20 61 73 0a 2a 2a 20 74 e thread as.** t
2a740 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 he SQL function
2a750 74 68 61 74 20 73 75 70 70 6c 69 65 64 20 74 68 that supplied th
2a760 65 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 e [sqlite3_value
2a770 2a 5d 20 70 61 72 61 6d 65 74 65 72 73 2e 0a 2a *] parameters..*
2a780 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 *.** Requirement
2a790 73 3a 0a 2a 2a 20 5b 48 31 35 31 30 33 5d 20 5b s:.** [H15103] [
2a7a0 48 31 35 31 30 36 5d 20 5b 48 31 35 31 30 39 5d H15106] [H15109]
2a7b0 20 5b 48 31 35 31 31 32 5d 20 5b 48 31 35 31 31 [H15112] [H1511
2a7c0 35 5d 20 5b 48 31 35 31 31 38 5d 20 5b 48 31 35 5] [H15118] [H15
2a7d0 31 32 31 5d 20 5b 48 31 35 31 32 34 5d 0a 2a 2a 121] [H15124].**
2a7e0 20 5b 48 31 35 31 32 37 5d 20 5b 48 31 35 31 33 [H15127] [H1513
2a7f0 30 5d 20 5b 48 31 35 31 33 33 5d 20 5b 48 31 35 0] [H15133] [H15
2a800 31 33 36 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 136].*/.SQLITE_A
2a810 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 PI const void *s
2a820 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f qlite3_value_blo
2a830 62 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a b(sqlite3_value*
2a840 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e );.SQLITE_API in
2a850 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f t sqlite3_value_
2a860 62 79 74 65 73 28 73 71 6c 69 74 65 33 5f 76 61 bytes(sqlite3_va
2a870 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 lue*);.SQLITE_AP
2a880 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 I int sqlite3_va
2a890 6c 75 65 5f 62 79 74 65 73 31 36 28 73 71 6c 69 lue_bytes16(sqli
2a8a0 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c te3_value*);.SQL
2a8b0 49 54 45 5f 41 50 49 20 64 6f 75 62 6c 65 20 73 ITE_API double s
2a8c0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 qlite3_value_dou
2a8d0 62 6c 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 ble(sqlite3_valu
2a8e0 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 e*);.SQLITE_API
2a8f0 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 int sqlite3_valu
2a900 65 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f 76 61 e_int(sqlite3_va
2a910 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 lue*);.SQLITE_AP
2a920 49 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 I sqlite3_int64
2a930 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e sqlite3_value_in
2a940 74 36 34 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 t64(sqlite3_valu
2a950 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 e*);.SQLITE_API
2a960 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 const unsigned c
2a970 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c har *sqlite3_val
2a980 75 65 5f 74 65 78 74 28 73 71 6c 69 74 65 33 5f ue_text(sqlite3_
2a990 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f value*);.SQLITE_
2a9a0 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a API const void *
2a9b0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 sqlite3_value_te
2a9c0 78 74 31 36 28 73 71 6c 69 74 65 33 5f 76 61 6c xt16(sqlite3_val
2a9d0 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 ue*);.SQLITE_API
2a9e0 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c const void *sql
2a9f0 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 ite3_value_text1
2aa00 36 6c 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 6le(sqlite3_valu
2aa10 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 e*);.SQLITE_API
2aa20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 const void *sqli
2aa30 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 te3_value_text16
2aa40 62 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 be(sqlite3_value
2aa50 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 *);.SQLITE_API i
2aa60 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 nt sqlite3_value
2aa70 5f 74 79 70 65 28 73 71 6c 69 74 65 33 5f 76 61 _type(sqlite3_va
2aa80 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 lue*);.SQLITE_AP
2aa90 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 I int sqlite3_va
2aaa0 6c 75 65 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65 lue_numeric_type
2aab0 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 (sqlite3_value*)
2aac0 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
2aad0 46 3a 20 4f 62 74 61 69 6e 20 41 67 67 72 65 67 F: Obtain Aggreg
2aae0 61 74 65 20 46 75 6e 63 74 69 6f 6e 20 43 6f 6e ate Function Con
2aaf0 74 65 78 74 20 7b 48 31 36 32 31 30 7d 20 3c 53 text {H16210} <S
2ab00 32 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20200>.**.** The
2ab10 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
2ab20 6f 66 20 61 67 67 72 65 67 61 74 65 20 53 51 4c of aggregate SQL
2ab30 20 66 75 6e 63 74 69 6f 6e 73 20 75 73 65 20 74 functions use t
2ab40 68 69 73 20 72 6f 75 74 69 6e 65 20 74 6f 20 61 his routine to a
2ab50 6c 6c 6f 63 61 74 65 0a 2a 2a 20 61 20 73 74 72 llocate.** a str
2ab60 75 63 74 75 72 65 20 66 6f 72 20 73 74 6f 72 69 ucture for stori
2ab70 6e 67 20 74 68 65 69 72 20 73 74 61 74 65 2e 0a ng their state..
2ab80 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 **.** The first
2ab90 74 69 6d 65 20 74 68 65 20 73 71 6c 69 74 65 33 time the sqlite3
2aba0 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 _aggregate_conte
2abb0 78 74 28 29 20 72 6f 75 74 69 6e 65 20 69 73 20 xt() routine is
2abc0 63 61 6c 6c 65 64 20 66 6f 72 20 61 0a 2a 2a 20 called for a.**
2abd0 70 61 72 74 69 63 75 6c 61 72 20 61 67 67 72 65 particular aggre
2abe0 67 61 74 65 2c 20 53 51 4c 69 74 65 20 61 6c 6c gate, SQLite all
2abf0 6f 63 61 74 65 73 20 6e 42 79 74 65 73 20 6f 66 ocates nBytes of
2ac00 20 6d 65 6d 6f 72 79 2c 20 7a 65 72 6f 65 73 20 memory, zeroes
2ac10 6f 75 74 20 74 68 61 74 0a 2a 2a 20 6d 65 6d 6f out that.** memo
2ac20 72 79 2c 20 61 6e 64 20 72 65 74 75 72 6e 73 20 ry, and returns
2ac30 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e a pointer to it.
2ac40 20 4f 6e 20 73 65 63 6f 6e 64 20 61 6e 64 20 73 On second and s
2ac50 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 ubsequent calls
2ac60 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 61 67 to.** sqlite3_ag
2ac70 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 gregate_context(
2ac80 29 20 66 6f 72 20 74 68 65 20 73 61 6d 65 20 61 ) for the same a
2ac90 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f ggregate functio
2aca0 6e 20 69 6e 64 65 78 2c 0a 2a 2a 20 74 68 65 20 n index,.** the
2acb0 73 61 6d 65 20 62 75 66 66 65 72 20 69 73 20 72 same buffer is r
2acc0 65 74 75 72 6e 65 64 2e 20 54 68 65 20 69 6d 70 eturned. The imp
2acd0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 lementation of t
2ace0 68 65 20 61 67 67 72 65 67 61 74 65 20 63 61 6e he aggregate can
2acf0 20 75 73 65 0a 2a 2a 20 74 68 65 20 72 65 74 75 use.** the retu
2ad00 72 6e 65 64 20 62 75 66 66 65 72 20 74 6f 20 61 rned buffer to a
2ad10 63 63 75 6d 75 6c 61 74 65 20 64 61 74 61 2e 0a ccumulate data..
2ad20 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 61 75 74 **.** SQLite aut
2ad30 6f 6d 61 74 69 63 61 6c 6c 79 20 66 72 65 65 73 omatically frees
2ad40 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 the allocated b
2ad50 75 66 66 65 72 20 77 68 65 6e 20 74 68 65 20 61 uffer when the a
2ad60 67 67 72 65 67 61 74 65 0a 2a 2a 20 71 75 65 72 ggregate.** quer
2ad70 79 20 63 6f 6e 63 6c 75 64 65 73 2e 0a 2a 2a 0a y concludes..**.
2ad80 2a 2a 20 54 68 65 20 66 69 72 73 74 20 70 61 72 ** The first par
2ad90 61 6d 65 74 65 72 20 73 68 6f 75 6c 64 20 62 65 ameter should be
2ada0 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 0a 2a a copy of the.*
2adb0 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 * [sqlite3_conte
2adc0 78 74 20 7c 20 53 51 4c 20 66 75 6e 63 74 69 6f xt | SQL functio
2add0 6e 20 63 6f 6e 74 65 78 74 5d 20 74 68 61 74 20 n context] that
2ade0 69 73 20 74 68 65 20 66 69 72 73 74 20 70 61 72 is the first par
2adf0 61 6d 65 74 65 72 0a 2a 2a 20 74 6f 20 74 68 65 ameter.** to the
2ae00 20 63 61 6c 6c 62 61 63 6b 20 72 6f 75 74 69 6e callback routin
2ae10 65 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 e that implement
2ae20 73 20 74 68 65 20 61 67 67 72 65 67 61 74 65 20 s the aggregate
2ae30 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 function..**.**
2ae40 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 75 73 This routine mus
2ae50 74 20 62 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d t be called from
2ae60 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 the same thread
2ae70 20 69 6e 20 77 68 69 63 68 0a 2a 2a 20 74 68 65 in which.** the
2ae80 20 61 67 67 72 65 67 61 74 65 20 53 51 4c 20 66 aggregate SQL f
2ae90 75 6e 63 74 69 6f 6e 20 69 73 20 72 75 6e 6e 69 unction is runni
2aea0 6e 67 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 ng..**.** Requir
2aeb0 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36 32 ements:.** [H162
2aec0 31 31 5d 20 5b 48 31 36 32 31 33 5d 20 5b 48 31 11] [H16213] [H1
2aed0 36 32 31 35 5d 20 5b 48 31 36 32 31 37 5d 0a 2a 6215] [H16217].*
2aee0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 /.SQLITE_API voi
2aef0 64 20 2a 73 71 6c 69 74 65 33 5f 61 67 67 72 65 d *sqlite3_aggre
2af00 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 73 71 6c gate_context(sql
2af10 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 69 ite3_context*, i
2af20 6e 74 20 6e 42 79 74 65 73 29 3b 0a 0a 2f 2a 0a nt nBytes);../*.
2af30 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 55 73 65 ** CAPI3REF: Use
2af40 72 20 44 61 74 61 20 46 6f 72 20 46 75 6e 63 74 r Data For Funct
2af50 69 6f 6e 73 20 7b 48 31 36 32 34 30 7d 20 3c 53 ions {H16240} <S
2af60 32 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20200>.**.** The
2af70 20 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 sqlite3_user_da
2af80 74 61 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 ta() interface r
2af90 65 74 75 72 6e 73 20 61 20 63 6f 70 79 20 6f 66 eturns a copy of
2afa0 0a 2a 2a 20 74 68 65 20 70 6f 69 6e 74 65 72 20 .** the pointer
2afb0 74 68 61 74 20 77 61 73 20 74 68 65 20 70 55 73 that was the pUs
2afc0 65 72 44 61 74 61 20 70 61 72 61 6d 65 74 65 72 erData parameter
2afd0 20 28 74 68 65 20 35 74 68 20 70 61 72 61 6d 65 (the 5th parame
2afe0 74 65 72 29 0a 2a 2a 20 6f 66 20 74 68 65 20 5b ter).** of the [
2aff0 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 sqlite3_create_f
2b000 75 6e 63 74 69 6f 6e 28 29 5d 0a 2a 2a 20 61 6e unction()].** an
2b010 64 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 d [sqlite3_creat
2b020 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28 29 5d 20 e_function16()]
2b030 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20 6f 72 routines that or
2b040 69 67 69 6e 61 6c 6c 79 0a 2a 2a 20 72 65 67 69 iginally.** regi
2b050 73 74 65 72 65 64 20 74 68 65 20 61 70 70 6c 69 stered the appli
2b060 63 61 74 69 6f 6e 20 64 65 66 69 6e 65 64 20 66 cation defined f
2b070 75 6e 63 74 69 6f 6e 2e 20 7b 45 4e 44 7d 0a 2a unction. {END}.*
2b080 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
2b090 65 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64 e must be called
2b0a0 20 66 72 6f 6d 20 74 68 65 20 73 61 6d 65 20 74 from the same t
2b0b0 68 72 65 61 64 20 69 6e 20 77 68 69 63 68 0a 2a hread in which.*
2b0c0 2a 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f * the applicatio
2b0d0 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 n-defined functi
2b0e0 6f 6e 20 69 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a on is running..*
2b0f0 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 *.** Requirement
2b100 73 3a 0a 2a 2a 20 5b 48 31 36 32 34 33 5d 0a 2a s:.** [H16243].*
2b110 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 /.SQLITE_API voi
2b120 64 20 2a 73 71 6c 69 74 65 33 5f 75 73 65 72 5f d *sqlite3_user_
2b130 64 61 74 61 28 73 71 6c 69 74 65 33 5f 63 6f 6e data(sqlite3_con
2b140 74 65 78 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 text*);../*.** C
2b150 41 50 49 33 52 45 46 3a 20 44 61 74 61 62 61 73 API3REF: Databas
2b160 65 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 46 6f 72 e Connection For
2b170 20 46 75 6e 63 74 69 6f 6e 73 20 7b 48 31 36 32 Functions {H162
2b180 35 30 7d 20 3c 53 36 30 36 30 30 3e 3c 53 32 30 50} <S60600><S20
2b190 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 200>.**.** The s
2b1a0 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 qlite3_context_d
2b1b0 62 5f 68 61 6e 64 6c 65 28 29 20 69 6e 74 65 72 b_handle() inter
2b1c0 66 61 63 65 20 72 65 74 75 72 6e 73 20 61 20 63 face returns a c
2b1d0 6f 70 79 20 6f 66 0a 2a 2a 20 74 68 65 20 70 6f opy of.** the po
2b1e0 69 6e 74 65 72 20 74 6f 20 74 68 65 20 5b 64 61 inter to the [da
2b1f0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
2b200 6e 5d 20 28 74 68 65 20 31 73 74 20 70 61 72 61 n] (the 1st para
2b210 6d 65 74 65 72 29 0a 2a 2a 20 6f 66 20 74 68 65 meter).** of the
2b220 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 [sqlite3_create
2b230 5f 66 75 6e 63 74 69 6f 6e 28 29 5d 0a 2a 2a 20 _function()].**
2b240 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 and [sqlite3_cre
2b250 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28 29 ate_function16()
2b260 5d 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20 ] routines that
2b270 6f 72 69 67 69 6e 61 6c 6c 79 0a 2a 2a 20 72 65 originally.** re
2b280 67 69 73 74 65 72 65 64 20 74 68 65 20 61 70 70 gistered the app
2b290 6c 69 63 61 74 69 6f 6e 20 64 65 66 69 6e 65 64 lication defined
2b2a0 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a function..**.**
2b2b0 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a Requirements:.*
2b2c0 2a 20 5b 48 31 36 32 35 33 5d 0a 2a 2f 0a 53 51 * [H16253].*/.SQ
2b2d0 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 LITE_API sqlite3
2b2e0 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 *sqlite3_contex
2b2f0 74 5f 64 62 5f 68 61 6e 64 6c 65 28 73 71 6c 69 t_db_handle(sqli
2b300 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 0a te3_context*);..
2b310 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
2b320 46 75 6e 63 74 69 6f 6e 20 41 75 78 69 6c 69 61 Function Auxilia
2b330 72 79 20 44 61 74 61 20 7b 48 31 36 32 37 30 7d ry Data {H16270}
2b340 20 3c 53 32 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 <S20200>.**.**
2b350 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 77 The following tw
2b360 6f 20 66 75 6e 63 74 69 6f 6e 73 20 6d 61 79 20 o functions may
2b370 62 65 20 75 73 65 64 20 62 79 20 73 63 61 6c 61 be used by scala
2b380 72 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20 r SQL functions
2b390 74 6f 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 20 to.** associate
2b3a0 6d 65 74 61 64 61 74 61 20 77 69 74 68 20 61 72 metadata with ar
2b3b0 67 75 6d 65 6e 74 20 76 61 6c 75 65 73 2e 20 49 gument values. I
2b3c0 66 20 74 68 65 20 73 61 6d 65 20 76 61 6c 75 65 f the same value
2b3d0 20 69 73 20 70 61 73 73 65 64 20 74 6f 0a 2a 2a is passed to.**
2b3e0 20 6d 75 6c 74 69 70 6c 65 20 69 6e 76 6f 63 61 multiple invoca
2b3f0 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 73 61 6d tions of the sam
2b400 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 64 e SQL function d
2b410 75 72 69 6e 67 20 71 75 65 72 79 20 65 78 65 63 uring query exec
2b420 75 74 69 6f 6e 2c 20 75 6e 64 65 72 0a 2a 2a 20 ution, under.**
2b430 73 6f 6d 65 20 63 69 72 63 75 6d 73 74 61 6e 63 some circumstanc
2b440 65 73 20 74 68 65 20 61 73 73 6f 63 69 61 74 65 es the associate
2b450 64 20 6d 65 74 61 64 61 74 61 20 6d 61 79 20 62 d metadata may b
2b460 65 20 70 72 65 73 65 72 76 65 64 2e 20 54 68 69 e preserved. Thi
2b470 73 20 6d 61 79 0a 2a 2a 20 62 65 20 75 73 65 64 s may.** be used
2b480 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 , for example, t
2b490 6f 20 61 64 64 20 61 20 72 65 67 75 6c 61 72 2d o add a regular-
2b4a0 65 78 70 72 65 73 73 69 6f 6e 20 6d 61 74 63 68 expression match
2b4b0 69 6e 67 20 73 63 61 6c 61 72 0a 2a 2a 20 66 75 ing scalar.** fu
2b4c0 6e 63 74 69 6f 6e 2e 20 54 68 65 20 63 6f 6d 70 nction. The comp
2b4d0 69 6c 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 iled version of
2b4e0 74 68 65 20 72 65 67 75 6c 61 72 20 65 78 70 72 the regular expr
2b4f0 65 73 73 69 6f 6e 20 69 73 20 73 74 6f 72 65 64 ession is stored
2b500 20 61 73 0a 2a 2a 20 6d 65 74 61 64 61 74 61 20 as.** metadata
2b510 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
2b520 74 68 65 20 53 51 4c 20 76 61 6c 75 65 20 70 61 the SQL value pa
2b530 73 73 65 64 20 61 73 20 74 68 65 20 72 65 67 75 ssed as the regu
2b540 6c 61 72 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a lar expression.*
2b550 2a 20 70 61 74 74 65 72 6e 2e 20 20 54 68 65 20 * pattern. The
2b560 63 6f 6d 70 69 6c 65 64 20 72 65 67 75 6c 61 72 compiled regular
2b570 20 65 78 70 72 65 73 73 69 6f 6e 20 63 61 6e 20 expression can
2b580 62 65 20 72 65 75 73 65 64 20 6f 6e 20 6d 75 6c be reused on mul
2b590 74 69 70 6c 65 0a 2a 2a 20 69 6e 76 6f 63 61 74 tiple.** invocat
2b5a0 69 6f 6e 73 20 6f 66 20 74 68 65 20 73 61 6d 65 ions of the same
2b5b0 20 66 75 6e 63 74 69 6f 6e 20 73 6f 20 74 68 61 function so tha
2b5c0 74 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 70 t the original p
2b5d0 61 74 74 65 72 6e 20 73 74 72 69 6e 67 0a 2a 2a attern string.**
2b5e0 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 does not need t
2b5f0 6f 20 62 65 20 72 65 63 6f 6d 70 69 6c 65 64 20 o be recompiled
2b600 6f 6e 20 65 61 63 68 20 69 6e 76 6f 63 61 74 69 on each invocati
2b610 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 on..**.** The sq
2b620 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74 lite3_get_auxdat
2b630 61 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 a() interface re
2b640 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 turns a pointer
2b650 74 6f 20 74 68 65 20 6d 65 74 61 64 61 74 61 0a to the metadata.
2b660 2a 2a 20 61 73 73 6f 63 69 61 74 65 64 20 62 79 ** associated by
2b670 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73 65 74 the sqlite3_set
2b680 5f 61 75 78 64 61 74 61 28 29 20 66 75 6e 63 74 _auxdata() funct
2b690 69 6f 6e 20 77 69 74 68 20 74 68 65 20 4e 74 68 ion with the Nth
2b6a0 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 76 61 6c argument.** val
2b6b0 75 65 20 74 6f 20 74 68 65 20 61 70 70 6c 69 63 ue to the applic
2b6c0 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 ation-defined fu
2b6d0 6e 63 74 69 6f 6e 2e 20 49 66 20 6e 6f 20 6d 65 nction. If no me
2b6e0 74 61 64 61 74 61 20 68 61 73 20 62 65 65 6e 20 tadata has been
2b6f0 65 76 65 72 0a 2a 2a 20 62 65 65 6e 20 73 65 74 ever.** been set
2b700 20 66 6f 72 20 74 68 65 20 4e 74 68 20 61 72 67 for the Nth arg
2b710 75 6d 65 6e 74 20 6f 66 20 74 68 65 20 66 75 6e ument of the fun
2b720 63 74 69 6f 6e 2c 20 6f 72 20 69 66 20 74 68 65 ction, or if the
2b730 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 0a 2a corresponding.*
2b740 2a 20 66 75 6e 63 74 69 6f 6e 20 70 61 72 61 6d * function param
2b750 65 74 65 72 20 68 61 73 20 63 68 61 6e 67 65 64 eter has changed
2b760 20 73 69 6e 63 65 20 74 68 65 20 6d 65 74 61 2d since the meta-
2b770 64 61 74 61 20 77 61 73 20 73 65 74 2c 0a 2a 2a data was set,.**
2b780 20 74 68 65 6e 20 73 71 6c 69 74 65 33 5f 67 65 then sqlite3_ge
2b790 74 5f 61 75 78 64 61 74 61 28 29 20 72 65 74 75 t_auxdata() retu
2b7a0 72 6e 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 rns a NULL point
2b7b0 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 er..**.** The sq
2b7c0 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61 74 lite3_set_auxdat
2b7d0 61 28 29 20 69 6e 74 65 72 66 61 63 65 20 73 61 a() interface sa
2b7e0 76 65 73 20 74 68 65 20 6d 65 74 61 64 61 74 61 ves the metadata
2b7f0 0a 2a 2a 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 .** pointed to b
2b800 79 20 69 74 73 20 33 72 64 20 70 61 72 61 6d 65 y its 3rd parame
2b810 74 65 72 20 61 73 20 74 68 65 20 6d 65 74 61 64 ter as the metad
2b820 61 74 61 20 66 6f 72 20 74 68 65 20 4e 2d 74 68 ata for the N-th
2b830 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 6f 66 20 .** argument of
2b840 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d the application-
2b850 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e defined function
2b860 2e 20 20 53 75 62 73 65 71 75 65 6e 74 0a 2a 2a . Subsequent.**
2b870 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 calls to sqlite
2b880 33 5f 67 65 74 5f 61 75 78 64 61 74 61 28 29 20 3_get_auxdata()
2b890 6d 69 67 68 74 20 72 65 74 75 72 6e 20 74 68 69 might return thi
2b8a0 73 20 64 61 74 61 2c 20 69 66 20 69 74 20 68 61 s data, if it ha
2b8b0 73 0a 2a 2a 20 6e 6f 74 20 62 65 65 6e 20 64 65 s.** not been de
2b8c0 73 74 72 6f 79 65 64 2e 0a 2a 2a 20 49 66 20 69 stroyed..** If i
2b8d0 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 53 t is not NULL, S
2b8e0 51 4c 69 74 65 20 77 69 6c 6c 20 69 6e 76 6f 6b QLite will invok
2b8f0 65 20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 e the destructor
2b900 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 67 69 76 .** function giv
2b910 65 6e 20 62 79 20 74 68 65 20 34 74 68 20 70 61 en by the 4th pa
2b920 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74 rameter to sqlit
2b930 65 33 5f 73 65 74 5f 61 75 78 64 61 74 61 28 29 e3_set_auxdata()
2b940 20 6f 6e 0a 2a 2a 20 74 68 65 20 6d 65 74 61 64 on.** the metad
2b950 61 74 61 20 77 68 65 6e 20 74 68 65 20 63 6f 72 ata when the cor
2b960 72 65 73 70 6f 6e 64 69 6e 67 20 66 75 6e 63 74 responding funct
2b970 69 6f 6e 20 70 61 72 61 6d 65 74 65 72 20 63 68 ion parameter ch
2b980 61 6e 67 65 73 0a 2a 2a 20 6f 72 20 77 68 65 6e anges.** or when
2b990 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 the SQL stateme
2b9a0 6e 74 20 63 6f 6d 70 6c 65 74 65 73 2c 20 77 68 nt completes, wh
2b9b0 69 63 68 65 76 65 72 20 63 6f 6d 65 73 20 66 69 ichever comes fi
2b9c0 72 73 74 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 rst..**.** SQLit
2b9d0 65 20 69 73 20 66 72 65 65 20 74 6f 20 63 61 6c e is free to cal
2b9e0 6c 20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 l the destructor
2b9f0 20 61 6e 64 20 64 72 6f 70 20 6d 65 74 61 64 61 and drop metada
2ba00 74 61 20 6f 6e 20 61 6e 79 0a 2a 2a 20 70 61 72 ta on any.** par
2ba10 61 6d 65 74 65 72 20 6f 66 20 61 6e 79 20 66 75 ameter of any fu
2ba20 6e 63 74 69 6f 6e 20 61 74 20 61 6e 79 20 74 69 nction at any ti
2ba30 6d 65 2e 20 20 54 68 65 20 6f 6e 6c 79 20 67 75 me. The only gu
2ba40 61 72 61 6e 74 65 65 20 69 73 20 74 68 61 74 0a arantee is that.
2ba50 2a 2a 20 74 68 65 20 64 65 73 74 72 75 63 74 6f ** the destructo
2ba60 72 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c 65 64 r will be called
2ba70 20 62 65 66 6f 72 65 20 74 68 65 20 6d 65 74 61 before the meta
2ba80 64 61 74 61 20 69 73 20 64 72 6f 70 70 65 64 2e data is dropped.
2ba90 0a 2a 2a 0a 2a 2a 20 49 6e 20 70 72 61 63 74 69 .**.** In practi
2baa0 63 65 2c 20 6d 65 74 61 64 61 74 61 20 69 73 20 ce, metadata is
2bab0 70 72 65 73 65 72 76 65 64 20 62 65 74 77 65 65 preserved betwee
2bac0 6e 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 73 n function calls
2bad0 20 66 6f 72 0a 2a 2a 20 65 78 70 72 65 73 73 69 for.** expressi
2bae0 6f 6e 73 20 74 68 61 74 20 61 72 65 20 63 6f 6e ons that are con
2baf0 73 74 61 6e 74 20 61 74 20 63 6f 6d 70 69 6c 65 stant at compile
2bb00 20 74 69 6d 65 2e 20 54 68 69 73 20 69 6e 63 6c time. This incl
2bb10 75 64 65 73 20 6c 69 74 65 72 61 6c 0a 2a 2a 20 udes literal.**
2bb20 76 61 6c 75 65 73 20 61 6e 64 20 53 51 4c 20 76 values and SQL v
2bb30 61 72 69 61 62 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 ariables..**.**
2bb40 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 6d These routines m
2bb50 75 73 74 20 62 65 20 63 61 6c 6c 65 64 20 66 72 ust be called fr
2bb60 6f 6d 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 om the same thre
2bb70 61 64 20 69 6e 20 77 68 69 63 68 0a 2a 2a 20 74 ad in which.** t
2bb80 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 he SQL function
2bb90 69 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a 2a 0a 2a is running..**.*
2bba0 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a * Requirements:.
2bbb0 2a 2a 20 5b 48 31 36 32 37 32 5d 20 5b 48 31 36 ** [H16272] [H16
2bbc0 32 37 34 5d 20 5b 48 31 36 32 37 36 5d 20 5b 48 274] [H16276] [H
2bbd0 31 36 32 37 37 5d 20 5b 48 31 36 32 37 38 5d 20 16277] [H16278]
2bbe0 5b 48 31 36 32 37 39 5d 0a 2a 2f 0a 53 51 4c 49 [H16279].*/.SQLI
2bbf0 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c TE_API void *sql
2bc00 69 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61 ite3_get_auxdata
2bc10 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 (sqlite3_context
2bc20 2a 2c 20 69 6e 74 20 4e 29 3b 0a 53 51 4c 49 54 *, int N);.SQLIT
2bc30 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 E_API void sqlit
2bc40 65 33 5f 73 65 74 5f 61 75 78 64 61 74 61 28 73 e3_set_auxdata(s
2bc50 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c qlite3_context*,
2bc60 20 69 6e 74 20 4e 2c 20 76 6f 69 64 2a 2c 20 76 int N, void*, v
2bc70 6f 69 64 20 28 2a 29 28 76 6f 69 64 2a 29 29 3b oid (*)(void*));
2bc80 0a 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 .../*.** CAPI3RE
2bc90 46 3a 20 43 6f 6e 73 74 61 6e 74 73 20 44 65 66 F: Constants Def
2bca0 69 6e 69 6e 67 20 53 70 65 63 69 61 6c 20 44 65 ining Special De
2bcb0 73 74 72 75 63 74 6f 72 20 42 65 68 61 76 69 6f structor Behavio
2bcc0 72 20 7b 48 31 30 32 38 30 7d 20 3c 53 33 30 31 r {H10280} <S301
2bcd0 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 00>.**.** These
2bce0 61 72 65 20 73 70 65 63 69 61 6c 20 76 61 6c 75 are special valu
2bcf0 65 73 20 66 6f 72 20 74 68 65 20 64 65 73 74 72 es for the destr
2bd00 75 63 74 6f 72 20 74 68 61 74 20 69 73 20 70 61 uctor that is pa
2bd10 73 73 65 64 20 69 6e 20 61 73 20 74 68 65 0a 2a ssed in as the.*
2bd20 2a 20 66 69 6e 61 6c 20 61 72 67 75 6d 65 6e 74 * final argument
2bd30 20 74 6f 20 72 6f 75 74 69 6e 65 73 20 6c 69 6b to routines lik
2bd40 65 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c e [sqlite3_resul
2bd50 74 5f 62 6c 6f 62 28 29 5d 2e 20 20 49 66 20 74 t_blob()]. If t
2bd60 68 65 20 64 65 73 74 72 75 63 74 6f 72 0a 2a 2a he destructor.**
2bd70 20 61 72 67 75 6d 65 6e 74 20 69 73 20 53 51 4c argument is SQL
2bd80 49 54 45 5f 53 54 41 54 49 43 2c 20 69 74 20 6d ITE_STATIC, it m
2bd90 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 63 6f eans that the co
2bda0 6e 74 65 6e 74 20 70 6f 69 6e 74 65 72 20 69 73 ntent pointer is
2bdb0 20 63 6f 6e 73 74 61 6e 74 0a 2a 2a 20 61 6e 64 constant.** and
2bdc0 20 77 69 6c 6c 20 6e 65 76 65 72 20 63 68 61 6e will never chan
2bdd0 67 65 2e 20 20 49 74 20 64 6f 65 73 20 6e 6f 74 ge. It does not
2bde0 20 6e 65 65 64 20 74 6f 20 62 65 20 64 65 73 74 need to be dest
2bdf0 72 6f 79 65 64 2e 20 20 54 68 65 0a 2a 2a 20 53 royed. The.** S
2be00 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 20 QLITE_TRANSIENT
2be10 76 61 6c 75 65 20 6d 65 61 6e 73 20 74 68 61 74 value means that
2be20 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 77 69 6c the content wil
2be30 6c 20 6c 69 6b 65 6c 79 20 63 68 61 6e 67 65 20 l likely change
2be40 69 6e 0a 2a 2a 20 74 68 65 20 6e 65 61 72 20 66 in.** the near f
2be50 75 74 75 72 65 20 61 6e 64 20 74 68 61 74 20 53 uture and that S
2be60 51 4c 69 74 65 20 73 68 6f 75 6c 64 20 6d 61 6b QLite should mak
2be70 65 20 69 74 73 20 6f 77 6e 20 70 72 69 76 61 74 e its own privat
2be80 65 20 63 6f 70 79 20 6f 66 0a 2a 2a 20 74 68 65 e copy of.** the
2be90 20 63 6f 6e 74 65 6e 74 20 62 65 66 6f 72 65 20 content before
2bea0 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a returning..**.**
2beb0 20 54 68 65 20 74 79 70 65 64 65 66 20 69 73 20 The typedef is
2bec0 6e 65 63 65 73 73 61 72 79 20 74 6f 20 77 6f 72 necessary to wor
2bed0 6b 20 61 72 6f 75 6e 64 20 70 72 6f 62 6c 65 6d k around problem
2bee0 73 20 69 6e 20 63 65 72 74 61 69 6e 0a 2a 2a 20 s in certain.**
2bef0 43 2b 2b 20 63 6f 6d 70 69 6c 65 72 73 2e 20 20 C++ compilers.
2bf00 53 65 65 20 74 69 63 6b 65 74 20 23 32 31 39 31 See ticket #2191
2bf10 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 76 6f 69 ..*/.typedef voi
2bf20 64 20 28 2a 73 71 6c 69 74 65 33 5f 64 65 73 74 d (*sqlite3_dest
2bf30 72 75 63 74 6f 72 5f 74 79 70 65 29 28 76 6f 69 ructor_type)(voi
2bf40 64 2a 29 3b 0a 23 64 65 66 69 6e 65 20 53 51 4c d*);.#define SQL
2bf50 49 54 45 5f 53 54 41 54 49 43 20 20 20 20 20 20 ITE_STATIC
2bf60 28 28 73 71 6c 69 74 65 33 5f 64 65 73 74 72 75 ((sqlite3_destru
2bf70 63 74 6f 72 5f 74 79 70 65 29 30 29 0a 23 64 65 ctor_type)0).#de
2bf80 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 52 41 4e fine SQLITE_TRAN
2bf90 53 49 45 4e 54 20 20 20 28 28 73 71 6c 69 74 65 SIENT ((sqlite
2bfa0 33 5f 64 65 73 74 72 75 63 74 6f 72 5f 74 79 70 3_destructor_typ
2bfb0 65 29 2d 31 29 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 e)-1)../*.** CAP
2bfc0 49 33 52 45 46 3a 20 53 65 74 74 69 6e 67 20 54 I3REF: Setting T
2bfd0 68 65 20 52 65 73 75 6c 74 20 4f 66 20 41 6e 20 he Result Of An
2bfe0 53 51 4c 20 46 75 6e 63 74 69 6f 6e 20 7b 48 31 SQL Function {H1
2bff0 36 34 30 30 7d 20 3c 53 32 30 32 30 30 3e 0a 2a 6400} <S20200>.*
2c000 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 *.** These routi
2c010 6e 65 73 20 61 72 65 20 75 73 65 64 20 62 79 20 nes are used by
2c020 74 68 65 20 78 46 75 6e 63 20 6f 72 20 78 46 69 the xFunc or xFi
2c030 6e 61 6c 20 63 61 6c 6c 62 61 63 6b 73 20 74 68 nal callbacks th
2c040 61 74 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 20 at.** implement
2c050 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20 61 6e SQL functions an
2c060 64 20 61 67 67 72 65 67 61 74 65 73 2e 20 20 53 d aggregates. S
2c070 65 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 ee.** [sqlite3_c
2c080 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 29 reate_function()
2c090 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 63 ] and [sqlite3_c
2c0a0 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 reate_function16
2c0b0 28 29 5d 0a 2a 2a 20 66 6f 72 20 61 64 64 69 74 ()].** for addit
2c0c0 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f ional informatio
2c0d0 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 66 n..**.** These f
2c0e0 75 6e 63 74 69 6f 6e 73 20 77 6f 72 6b 20 76 65 unctions work ve
2c0f0 72 79 20 6d 75 63 68 20 6c 69 6b 65 20 74 68 65 ry much like the
2c100 20 5b 70 61 72 61 6d 65 74 65 72 20 62 69 6e 64 [parameter bind
2c110 69 6e 67 5d 20 66 61 6d 69 6c 79 20 6f 66 0a 2a ing] family of.*
2c120 2a 20 66 75 6e 63 74 69 6f 6e 73 20 75 73 65 64 * functions used
2c130 20 74 6f 20 62 69 6e 64 20 76 61 6c 75 65 73 20 to bind values
2c140 74 6f 20 68 6f 73 74 20 70 61 72 61 6d 65 74 65 to host paramete
2c150 72 73 20 69 6e 20 70 72 65 70 61 72 65 64 20 73 rs in prepared s
2c160 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 20 52 65 tatements..** Re
2c170 66 65 72 20 74 6f 20 74 68 65 20 5b 53 51 4c 20 fer to the [SQL
2c180 70 61 72 61 6d 65 74 65 72 5d 20 64 6f 63 75 6d parameter] docum
2c190 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20 61 64 64 entation for add
2c1a0 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 itional informat
2c1b0 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 ion..**.** The s
2c1c0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c qlite3_result_bl
2c1d0 6f 62 28 29 20 69 6e 74 65 72 66 61 63 65 20 73 ob() interface s
2c1e0 65 74 73 20 74 68 65 20 72 65 73 75 6c 74 20 66 ets the result f
2c1f0 72 6f 6d 0a 2a 2a 20 61 6e 20 61 70 70 6c 69 63 rom.** an applic
2c200 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 ation-defined fu
2c210 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 74 68 65 nction to be the
2c220 20 42 4c 4f 42 20 77 68 6f 73 65 20 63 6f 6e 74 BLOB whose cont
2c230 65 6e 74 20 69 73 20 70 6f 69 6e 74 65 64 0a 2a ent is pointed.*
2c240 2a 20 74 6f 20 62 79 20 74 68 65 20 73 65 63 6f * to by the seco
2c250 6e 64 20 70 61 72 61 6d 65 74 65 72 20 61 6e 64 nd parameter and
2c260 20 77 68 69 63 68 20 69 73 20 4e 20 62 79 74 65 which is N byte
2c270 73 20 6c 6f 6e 67 20 77 68 65 72 65 20 4e 20 69 s long where N i
2c280 73 20 74 68 65 0a 2a 2a 20 74 68 69 72 64 20 70 s the.** third p
2c290 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 arameter..**.**
2c2a0 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 The sqlite3_resu
2c2b0 6c 74 5f 7a 65 72 6f 62 6c 6f 62 28 29 20 69 6e lt_zeroblob() in
2c2c0 74 65 72 66 61 63 65 73 20 73 65 74 20 74 68 65 terfaces set the
2c2d0 20 72 65 73 75 6c 74 20 6f 66 0a 2a 2a 20 74 68 result of.** th
2c2e0 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 e application-de
2c2f0 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 74 fined function t
2c300 6f 20 62 65 20 61 20 42 4c 4f 42 20 63 6f 6e 74 o be a BLOB cont
2c310 61 69 6e 69 6e 67 20 61 6c 6c 20 7a 65 72 6f 0a aining all zero.
2c320 2a 2a 20 62 79 74 65 73 20 61 6e 64 20 4e 20 62 ** bytes and N b
2c330 79 74 65 73 20 69 6e 20 73 69 7a 65 2c 20 77 68 ytes in size, wh
2c340 65 72 65 20 4e 20 69 73 20 74 68 65 20 76 61 6c ere N is the val
2c350 75 65 20 6f 66 20 74 68 65 20 32 6e 64 20 70 61 ue of the 2nd pa
2c360 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 rameter..**.** T
2c370 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c he sqlite3_resul
2c380 74 5f 64 6f 75 62 6c 65 28 29 20 69 6e 74 65 72 t_double() inter
2c390 66 61 63 65 20 73 65 74 73 20 74 68 65 20 72 65 face sets the re
2c3a0 73 75 6c 74 20 66 72 6f 6d 0a 2a 2a 20 61 6e 20 sult from.** an
2c3b0 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 application-defi
2c3c0 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 ned function to
2c3d0 62 65 20 61 20 66 6c 6f 61 74 69 6e 67 20 70 6f be a floating po
2c3e0 69 6e 74 20 76 61 6c 75 65 20 73 70 65 63 69 66 int value specif
2c3f0 69 65 64 0a 2a 2a 20 62 79 20 69 74 73 20 32 6e ied.** by its 2n
2c400 64 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a d argument..**.*
2c410 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 * The sqlite3_re
2c420 73 75 6c 74 5f 65 72 72 6f 72 28 29 20 61 6e 64 sult_error() and
2c430 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
2c440 65 72 72 6f 72 31 36 28 29 20 66 75 6e 63 74 69 error16() functi
2c450 6f 6e 73 0a 2a 2a 20 63 61 75 73 65 20 74 68 65 ons.** cause the
2c460 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 53 51 4c implemented SQL
2c470 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 74 68 72 function to thr
2c480 6f 77 20 61 6e 20 65 78 63 65 70 74 69 6f 6e 2e ow an exception.
2c490 0a 2a 2a 20 53 51 4c 69 74 65 20 75 73 65 73 20 .** SQLite uses
2c4a0 74 68 65 20 73 74 72 69 6e 67 20 70 6f 69 6e 74 the string point
2c4b0 65 64 20 74 6f 20 62 79 20 74 68 65 0a 2a 2a 20 ed to by the.**
2c4c0 32 6e 64 20 70 61 72 61 6d 65 74 65 72 20 6f 66 2nd parameter of
2c4d0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
2c4e0 65 72 72 6f 72 28 29 20 6f 72 20 73 71 6c 69 74 error() or sqlit
2c4f0 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 e3_result_error1
2c500 36 28 29 0a 2a 2a 20 61 73 20 74 68 65 20 74 65 6().** as the te
2c510 78 74 20 6f 66 20 61 6e 20 65 72 72 6f 72 20 6d xt of an error m
2c520 65 73 73 61 67 65 2e 20 20 53 51 4c 69 74 65 20 essage. SQLite
2c530 69 6e 74 65 72 70 72 65 74 73 20 74 68 65 20 65 interprets the e
2c540 72 72 6f 72 0a 2a 2a 20 6d 65 73 73 61 67 65 20 rror.** message
2c550 73 74 72 69 6e 67 20 66 72 6f 6d 20 73 71 6c 69 string from sqli
2c560 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 te3_result_error
2c570 28 29 20 61 73 20 55 54 46 2d 38 2e 20 53 51 4c () as UTF-8. SQL
2c580 69 74 65 0a 2a 2a 20 69 6e 74 65 72 70 72 65 74 ite.** interpret
2c590 73 20 74 68 65 20 73 74 72 69 6e 67 20 66 72 6f s the string fro
2c5a0 6d 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 m sqlite3_result
2c5b0 5f 65 72 72 6f 72 31 36 28 29 20 61 73 20 55 54 _error16() as UT
2c5c0 46 2d 31 36 20 69 6e 20 6e 61 74 69 76 65 0a 2a F-16 in native.*
2c5d0 2a 20 62 79 74 65 20 6f 72 64 65 72 2e 20 20 49 * byte order. I
2c5e0 66 20 74 68 65 20 74 68 69 72 64 20 70 61 72 61 f the third para
2c5f0 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 meter to sqlite3
2c600 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 29 0a _result_error().
2c610 2a 2a 20 6f 72 20 73 71 6c 69 74 65 33 5f 72 65 ** or sqlite3_re
2c620 73 75 6c 74 5f 65 72 72 6f 72 31 36 28 29 20 69 sult_error16() i
2c630 73 20 6e 65 67 61 74 69 76 65 20 74 68 65 6e 20 s negative then
2c640 53 51 4c 69 74 65 20 74 61 6b 65 73 20 61 73 20 SQLite takes as
2c650 74 68 65 20 65 72 72 6f 72 0a 2a 2a 20 6d 65 73 the error.** mes
2c660 73 61 67 65 20 61 6c 6c 20 74 65 78 74 20 75 70 sage all text up
2c670 20 74 68 72 6f 75 67 68 20 74 68 65 20 66 69 72 through the fir
2c680 73 74 20 7a 65 72 6f 20 63 68 61 72 61 63 74 65 st zero characte
2c690 72 2e 0a 2a 2a 20 49 66 20 74 68 65 20 74 68 69 r..** If the thi
2c6a0 72 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 rd parameter to
2c6b0 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 sqlite3_result_e
2c6c0 72 72 6f 72 28 29 20 6f 72 0a 2a 2a 20 73 71 6c rror() or.** sql
2c6d0 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f ite3_result_erro
2c6e0 72 31 36 28 29 20 69 73 20 6e 6f 6e 2d 6e 65 67 r16() is non-neg
2c6f0 61 74 69 76 65 20 74 68 65 6e 20 53 51 4c 69 74 ative then SQLit
2c700 65 20 74 61 6b 65 73 20 74 68 61 74 20 6d 61 6e e takes that man
2c710 79 0a 2a 2a 20 62 79 74 65 73 20 28 6e 6f 74 20 y.** bytes (not
2c720 63 68 61 72 61 63 74 65 72 73 29 20 66 72 6f 6d characters) from
2c730 20 74 68 65 20 32 6e 64 20 70 61 72 61 6d 65 74 the 2nd paramet
2c740 65 72 20 61 73 20 74 68 65 20 65 72 72 6f 72 20 er as the error
2c750 6d 65 73 73 61 67 65 2e 0a 2a 2a 20 54 68 65 20 message..** The
2c760 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 sqlite3_result_e
2c770 72 72 6f 72 28 29 20 61 6e 64 20 73 71 6c 69 74 rror() and sqlit
2c780 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 e3_result_error1
2c790 36 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 6().** routines
2c7a0 6d 61 6b 65 20 61 20 70 72 69 76 61 74 65 20 63 make a private c
2c7b0 6f 70 79 20 6f 66 20 74 68 65 20 65 72 72 6f 72 opy of the error
2c7c0 20 6d 65 73 73 61 67 65 20 74 65 78 74 20 62 65 message text be
2c7d0 66 6f 72 65 0a 2a 2a 20 74 68 65 79 20 72 65 74 fore.** they ret
2c7e0 75 72 6e 2e 20 20 48 65 6e 63 65 2c 20 74 68 65 urn. Hence, the
2c7f0 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f calling functio
2c800 6e 20 63 61 6e 20 64 65 61 6c 6c 6f 63 61 74 65 n can deallocate
2c810 20 6f 72 0a 2a 2a 20 6d 6f 64 69 66 79 20 74 68 or.** modify th
2c820 65 20 74 65 78 74 20 61 66 74 65 72 20 74 68 65 e text after the
2c830 79 20 72 65 74 75 72 6e 20 77 69 74 68 6f 75 74 y return without
2c840 20 68 61 72 6d 2e 0a 2a 2a 20 54 68 65 20 73 71 harm..** The sq
2c850 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 lite3_result_err
2c860 6f 72 5f 63 6f 64 65 28 29 20 66 75 6e 63 74 69 or_code() functi
2c870 6f 6e 20 63 68 61 6e 67 65 73 20 74 68 65 20 65 on changes the e
2c880 72 72 6f 72 20 63 6f 64 65 0a 2a 2a 20 72 65 74 rror code.** ret
2c890 75 72 6e 65 64 20 62 79 20 53 51 4c 69 74 65 20 urned by SQLite
2c8a0 61 73 20 61 20 72 65 73 75 6c 74 20 6f 66 20 61 as a result of a
2c8b0 6e 20 65 72 72 6f 72 20 69 6e 20 61 20 66 75 6e n error in a fun
2c8c0 63 74 69 6f 6e 2e 20 20 42 79 20 64 65 66 61 75 ction. By defau
2c8d0 6c 74 2c 0a 2a 2a 20 74 68 65 20 65 72 72 6f 72 lt,.** the error
2c8e0 20 63 6f 64 65 20 69 73 20 53 51 4c 49 54 45 5f code is SQLITE_
2c8f0 45 52 52 4f 52 2e 20 20 41 20 73 75 62 73 65 71 ERROR. A subseq
2c900 75 65 6e 74 20 63 61 6c 6c 20 74 6f 20 73 71 6c uent call to sql
2c910 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f ite3_result_erro
2c920 72 28 29 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 65 r().** or sqlite
2c930 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 3_result_error16
2c940 28 29 20 72 65 73 65 74 73 20 74 68 65 20 65 72 () resets the er
2c950 72 6f 72 20 63 6f 64 65 20 74 6f 20 53 51 4c 49 ror code to SQLI
2c960 54 45 5f 45 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20 TE_ERROR..**.**
2c970 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 The sqlite3_resu
2c980 6c 74 5f 74 6f 6f 62 69 67 28 29 20 69 6e 74 65 lt_toobig() inte
2c990 72 66 61 63 65 20 63 61 75 73 65 73 20 53 51 4c rface causes SQL
2c9a0 69 74 65 20 74 6f 20 74 68 72 6f 77 20 61 6e 20 ite to throw an
2c9b0 65 72 72 6f 72 0a 2a 2a 20 69 6e 64 69 63 61 74 error.** indicat
2c9c0 69 6e 67 20 74 68 61 74 20 61 20 73 74 72 69 6e ing that a strin
2c9d0 67 20 6f 72 20 42 4c 4f 42 20 69 73 20 74 6f 20 g or BLOB is to
2c9e0 6c 6f 6e 67 20 74 6f 20 72 65 70 72 65 73 65 6e long to represen
2c9f0 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c t..**.** The sql
2ca00 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 6f 6d 65 ite3_result_nome
2ca10 6d 28 29 20 69 6e 74 65 72 66 61 63 65 20 63 61 m() interface ca
2ca20 75 73 65 73 20 53 51 4c 69 74 65 20 74 6f 20 74 uses SQLite to t
2ca30 68 72 6f 77 20 61 6e 20 65 72 72 6f 72 0a 2a 2a hrow an error.**
2ca40 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68 61 74 indicating that
2ca50 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 a memory alloca
2ca60 74 69 6f 6e 20 66 61 69 6c 65 64 2e 0a 2a 2a 0a tion failed..**.
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 69 6e 74 28 29 20 69 6e 74 65 esult_int() inte
2ca90 72 66 61 63 65 20 73 65 74 73 20 74 68 65 20 72 rface sets the r
2caa0 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 6f eturn value.** o
2cab0 66 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f f the applicatio
2cac0 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 n-defined functi
2cad0 6f 6e 20 74 6f 20 62 65 20 74 68 65 20 33 32 2d on to be the 32-
2cae0 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 bit signed integ
2caf0 65 72 0a 2a 2a 20 76 61 6c 75 65 20 67 69 76 65 er.** value give
2cb00 6e 20 69 6e 20 74 68 65 20 32 6e 64 20 61 72 67 n in the 2nd arg
2cb10 75 6d 65 6e 74 2e 0a 2a 2a 20 54 68 65 20 73 71 ument..** The sq
2cb20 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 lite3_result_int
2cb30 36 34 28 29 20 69 6e 74 65 72 66 61 63 65 20 73 64() interface s
2cb40 65 74 73 20 74 68 65 20 72 65 74 75 72 6e 20 76 ets the return v
2cb50 61 6c 75 65 0a 2a 2a 20 6f 66 20 74 68 65 20 61 alue.** of the a
2cb60 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e pplication-defin
2cb70 65 64 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 ed function to b
2cb80 65 20 74 68 65 20 36 34 2d 62 69 74 20 73 69 67 e the 64-bit sig
2cb90 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 76 ned integer.** v
2cba0 61 6c 75 65 20 67 69 76 65 6e 20 69 6e 20 74 68 alue given in th
2cbb0 65 20 32 6e 64 20 61 72 67 75 6d 65 6e 74 2e 0a e 2nd argument..
2cbc0 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 **.** The sqlite
2cbd0 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 28 29 20 3_result_null()
2cbe0 69 6e 74 65 72 66 61 63 65 20 73 65 74 73 20 74 interface sets t
2cbf0 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a he return value.
2cc00 2a 2a 20 6f 66 20 74 68 65 20 61 70 70 6c 69 63 ** of the applic
2cc10 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 ation-defined fu
2cc20 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 4e 55 4c nction to be NUL
2cc30 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c L..**.** The sql
2cc40 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 ite3_result_text
2cc50 28 29 2c 20 73 71 6c 69 74 65 33 5f 72 65 73 75 (), sqlite3_resu
2cc60 6c 74 5f 74 65 78 74 31 36 28 29 2c 0a 2a 2a 20 lt_text16(),.**
2cc70 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 sqlite3_result_t
2cc80 65 78 74 31 36 6c 65 28 29 2c 20 61 6e 64 20 73 ext16le(), and s
2cc90 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 qlite3_result_te
2cca0 78 74 31 36 62 65 28 29 20 69 6e 74 65 72 66 61 xt16be() interfa
2ccb0 63 65 73 0a 2a 2a 20 73 65 74 20 74 68 65 20 72 ces.** set the r
2ccc0 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 74 eturn value of t
2ccd0 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 he application-d
2cce0 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 efined function
2ccf0 74 6f 20 62 65 0a 2a 2a 20 61 20 74 65 78 74 20 to be.** a text
2cd00 73 74 72 69 6e 67 20 77 68 69 63 68 20 69 73 20 string which is
2cd10 72 65 70 72 65 73 65 6e 74 65 64 20 61 73 20 55 represented as U
2cd20 54 46 2d 38 2c 20 55 54 46 2d 31 36 20 6e 61 74 TF-8, UTF-16 nat
2cd30 69 76 65 20 62 79 74 65 20 6f 72 64 65 72 2c 0a ive byte order,.
2cd40 2a 2a 20 55 54 46 2d 31 36 20 6c 69 74 74 6c 65 ** UTF-16 little
2cd50 20 65 6e 64 69 61 6e 2c 20 6f 72 20 55 54 46 2d endian, or UTF-
2cd60 31 36 20 62 69 67 20 65 6e 64 69 61 6e 2c 20 72 16 big endian, r
2cd70 65 73 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2a 20 espectively..**
2cd80 53 51 4c 69 74 65 20 74 61 6b 65 73 20 74 68 65 SQLite takes the
2cd90 20 74 65 78 74 20 72 65 73 75 6c 74 20 66 72 6f text result fro
2cda0 6d 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f m the applicatio
2cdb0 6e 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 32 6e n from.** the 2n
2cdc0 64 20 70 61 72 61 6d 65 74 65 72 20 6f 66 20 74 d parameter of t
2cdd0 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c he sqlite3_resul
2cde0 74 5f 74 65 78 74 2a 20 69 6e 74 65 72 66 61 63 t_text* interfac
2cdf0 65 73 2e 0a 2a 2a 20 49 66 20 74 68 65 20 33 72 es..** If the 3r
2ce00 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 d parameter to t
2ce10 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c he sqlite3_resul
2ce20 74 5f 74 65 78 74 2a 20 69 6e 74 65 72 66 61 63 t_text* interfac
2ce30 65 73 0a 2a 2a 20 69 73 20 6e 65 67 61 74 69 76 es.** is negativ
2ce40 65 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 20 74 e, then SQLite t
2ce50 61 6b 65 73 20 72 65 73 75 6c 74 20 74 65 78 74 akes result text
2ce60 20 66 72 6f 6d 20 74 68 65 20 32 6e 64 20 70 61 from the 2nd pa
2ce70 72 61 6d 65 74 65 72 0a 2a 2a 20 74 68 72 6f 75 rameter.** throu
2ce80 67 68 20 74 68 65 20 66 69 72 73 74 20 7a 65 72 gh the first zer
2ce90 6f 20 63 68 61 72 61 63 74 65 72 2e 0a 2a 2a 20 o character..**
2cea0 49 66 20 74 68 65 20 33 72 64 20 70 61 72 61 6d If the 3rd param
2ceb0 65 74 65 72 20 74 6f 20 74 68 65 20 73 71 6c 69 eter to the sqli
2cec0 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 2a te3_result_text*
2ced0 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20 69 interfaces.** i
2cee0 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2c 20 s non-negative,
2cef0 74 68 65 6e 20 61 73 20 6d 61 6e 79 20 62 79 74 then as many byt
2cf00 65 73 20 28 6e 6f 74 20 63 68 61 72 61 63 74 65 es (not characte
2cf10 72 73 29 20 6f 66 20 74 68 65 20 74 65 78 74 0a rs) of the text.
2cf20 2a 2a 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 ** pointed to by
2cf30 20 74 68 65 20 32 6e 64 20 70 61 72 61 6d 65 74 the 2nd paramet
2cf40 65 72 20 61 72 65 20 74 61 6b 65 6e 20 61 73 20 er are taken as
2cf50 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d the application-
2cf60 64 65 66 69 6e 65 64 0a 2a 2a 20 66 75 6e 63 74 defined.** funct
2cf70 69 6f 6e 20 72 65 73 75 6c 74 2e 0a 2a 2a 20 49 ion result..** I
2cf80 66 20 74 68 65 20 34 74 68 20 70 61 72 61 6d 65 f the 4th parame
2cf90 74 65 72 20 74 6f 20 74 68 65 20 73 71 6c 69 74 ter to the sqlit
2cfa0 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 2a 20 e3_result_text*
2cfb0 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20 6f 72 interfaces.** or
2cfc0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
2cfd0 62 6c 6f 62 20 69 73 20 61 20 6e 6f 6e 2d 4e 55 blob is a non-NU
2cfe0 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 74 68 65 6e LL pointer, then
2cff0 20 53 51 4c 69 74 65 20 63 61 6c 6c 73 20 74 68 SQLite calls th
2d000 61 74 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 61 at.** function a
2d010 73 20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 s the destructor
2d020 20 6f 6e 20 74 68 65 20 74 65 78 74 20 6f 72 20 on the text or
2d030 42 4c 4f 42 20 72 65 73 75 6c 74 20 77 68 65 6e BLOB result when
2d040 20 69 74 20 68 61 73 0a 2a 2a 20 66 69 6e 69 73 it has.** finis
2d050 68 65 64 20 75 73 69 6e 67 20 74 68 61 74 20 72 hed using that r
2d060 65 73 75 6c 74 2e 0a 2a 2a 20 49 66 20 74 68 65 esult..** If the
2d070 20 34 74 68 20 70 61 72 61 6d 65 74 65 72 20 74 4th parameter t
2d080 6f 20 74 68 65 20 73 71 6c 69 74 65 33 5f 72 65 o the sqlite3_re
2d090 73 75 6c 74 5f 74 65 78 74 2a 20 69 6e 74 65 72 sult_text* inter
2d0a0 66 61 63 65 73 20 6f 72 20 74 6f 0a 2a 2a 20 73 faces or to.** s
2d0b0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c qlite3_result_bl
2d0c0 6f 62 20 69 73 20 74 68 65 20 73 70 65 63 69 61 ob is the specia
2d0d0 6c 20 63 6f 6e 73 74 61 6e 74 20 53 51 4c 49 54 l constant SQLIT
2d0e0 45 5f 53 54 41 54 49 43 2c 20 74 68 65 6e 20 53 E_STATIC, then S
2d0f0 51 4c 69 74 65 0a 2a 2a 20 61 73 73 75 6d 65 73 QLite.** assumes
2d100 20 74 68 61 74 20 74 68 65 20 74 65 78 74 20 6f that the text o
2d110 72 20 42 4c 4f 42 20 72 65 73 75 6c 74 20 69 73 r BLOB result is
2d120 20 69 6e 20 63 6f 6e 73 74 61 6e 74 20 73 70 61 in constant spa
2d130 63 65 20 61 6e 64 20 64 6f 65 73 20 6e 6f 74 0a ce and does not.
2d140 2a 2a 20 63 6f 70 79 20 74 68 65 20 63 6f 6e 74 ** copy the cont
2d150 65 6e 74 20 6f 66 20 74 68 65 20 70 61 72 61 6d ent of the param
2d160 65 74 65 72 20 6e 6f 72 20 63 61 6c 6c 20 61 20 eter nor call a
2d170 64 65 73 74 72 75 63 74 6f 72 20 6f 6e 20 74 68 destructor on th
2d180 65 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20 77 68 65 e content.** whe
2d190 6e 20 69 74 20 68 61 73 20 66 69 6e 69 73 68 65 n it has finishe
2d1a0 64 20 75 73 69 6e 67 20 74 68 61 74 20 72 65 73 d using that res
2d1b0 75 6c 74 2e 0a 2a 2a 20 49 66 20 74 68 65 20 34 ult..** If the 4
2d1c0 74 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 th parameter to
2d1d0 74 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 the sqlite3_resu
2d1e0 6c 74 5f 74 65 78 74 2a 20 69 6e 74 65 72 66 61 lt_text* interfa
2d1f0 63 65 73 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 65 ces.** or sqlite
2d200 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20 69 73 3_result_blob is
2d210 20 74 68 65 20 73 70 65 63 69 61 6c 20 63 6f 6e the special con
2d220 73 74 61 6e 74 20 53 51 4c 49 54 45 5f 54 52 41 stant SQLITE_TRA
2d230 4e 53 49 45 4e 54 0a 2a 2a 20 74 68 65 6e 20 53 NSIENT.** then S
2d240 51 4c 69 74 65 20 6d 61 6b 65 73 20 61 20 63 6f QLite makes a co
2d250 70 79 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 py of the result
2d260 20 69 6e 74 6f 20 73 70 61 63 65 20 6f 62 74 61 into space obta
2d270 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 66 72 6f ined from.** fro
2d280 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f m [sqlite3_mallo
2d290 63 28 29 5d 20 62 65 66 6f 72 65 20 69 74 20 72 c()] before it r
2d2a0 65 74 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 eturns..**.** Th
2d2b0 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 e sqlite3_result
2d2c0 5f 76 61 6c 75 65 28 29 20 69 6e 74 65 72 66 61 _value() interfa
2d2d0 63 65 20 73 65 74 73 20 74 68 65 20 72 65 73 75 ce sets the resu
2d2e0 6c 74 20 6f 66 0a 2a 2a 20 74 68 65 20 61 70 70 lt of.** the app
2d2f0 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 lication-defined
2d300 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 function to be
2d310 61 20 63 6f 70 79 20 74 68 65 0a 2a 2a 20 5b 75 a copy the.** [u
2d320 6e 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 nprotected sqlit
2d330 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 e3_value] object
2d340 20 73 70 65 63 69 66 69 65 64 20 62 79 20 74 68 specified by th
2d350 65 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72 2e e 2nd parameter.
2d360 20 20 54 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 The.** sqlite3
2d370 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 29 20 _result_value()
2d380 69 6e 74 65 72 66 61 63 65 20 6d 61 6b 65 73 20 interface makes
2d390 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 5b 73 a copy of the [s
2d3a0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 0a 2a 2a qlite3_value].**
2d3b0 20 73 6f 20 74 68 61 74 20 74 68 65 20 5b 73 71 so that the [sq
2d3c0 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 73 70 65 lite3_value] spe
2d3d0 63 69 66 69 65 64 20 69 6e 20 74 68 65 20 70 61 cified in the pa
2d3e0 72 61 6d 65 74 65 72 20 6d 61 79 20 63 68 61 6e rameter may chan
2d3f0 67 65 20 6f 72 0a 2a 2a 20 62 65 20 64 65 61 6c ge or.** be deal
2d400 6c 6f 63 61 74 65 64 20 61 66 74 65 72 20 73 71 located after sq
2d410 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c lite3_result_val
2d420 75 65 28 29 20 72 65 74 75 72 6e 73 20 77 69 74 ue() returns wit
2d430 68 6f 75 74 20 68 61 72 6d 2e 0a 2a 2a 20 41 20 hout harm..** A
2d440 5b 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 [protected sqlit
2d450 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 e3_value] object
2d460 20 6d 61 79 20 61 6c 77 61 79 73 20 62 65 20 75 may always be u
2d470 73 65 64 20 77 68 65 72 65 20 61 6e 0a 2a 2a 20 sed where an.**
2d480 5b 75 6e 70 72 6f 74 65 63 74 65 64 20 73 71 6c [unprotected sql
2d490 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 ite3_value] obje
2d4a0 63 74 20 69 73 20 72 65 71 75 69 72 65 64 2c 20 ct is required,
2d4b0 73 6f 20 65 69 74 68 65 72 0a 2a 2a 20 6b 69 6e so either.** kin
2d4c0 64 20 6f 66 20 5b 73 71 6c 69 74 65 33 5f 76 61 d of [sqlite3_va
2d4d0 6c 75 65 5d 20 6f 62 6a 65 63 74 20 63 61 6e 20 lue] object can
2d4e0 62 65 20 75 73 65 64 20 77 69 74 68 20 74 68 69 be used with thi
2d4f0 73 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a s interface..**.
2d500 2a 2a 20 49 66 20 74 68 65 73 65 20 72 6f 75 74 ** If these rout
2d510 69 6e 65 73 20 61 72 65 20 63 61 6c 6c 65 64 20 ines are called
2d520 66 72 6f 6d 20 77 69 74 68 69 6e 20 74 68 65 20 from within the
2d530 64 69 66 66 65 72 65 6e 74 20 74 68 72 65 61 64 different thread
2d540 0a 2a 2a 20 74 68 61 6e 20 74 68 65 20 6f 6e 65 .** than the one
2d550 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 containing the
2d560 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 application-defi
2d570 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 ned function tha
2d580 74 20 72 65 63 65 69 76 65 64 0a 2a 2a 20 74 68 t received.** th
2d590 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 e [sqlite3_conte
2d5a0 78 74 5d 20 70 6f 69 6e 74 65 72 2c 20 74 68 65 xt] pointer, the
2d5b0 20 72 65 73 75 6c 74 73 20 61 72 65 20 75 6e 64 results are und
2d5c0 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 efined..**.** Re
2d5d0 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b quirements:.** [
2d5e0 48 31 36 34 30 33 5d 20 5b 48 31 36 34 30 36 5d H16403] [H16406]
2d5f0 20 5b 48 31 36 34 30 39 5d 20 5b 48 31 36 34 31 [H16409] [H1641
2d600 32 5d 20 5b 48 31 36 34 31 35 5d 20 5b 48 31 36 2] [H16415] [H16
2d610 34 31 38 5d 20 5b 48 31 36 34 32 31 5d 20 5b 48 418] [H16421] [H
2d620 31 36 34 32 34 5d 0a 2a 2a 20 5b 48 31 36 34 32 16424].** [H1642
2d630 37 5d 20 5b 48 31 36 34 33 30 5d 20 5b 48 31 36 7] [H16430] [H16
2d640 34 33 33 5d 20 5b 48 31 36 34 33 36 5d 20 5b 48 433] [H16436] [H
2d650 31 36 34 33 39 5d 20 5b 48 31 36 34 34 32 5d 20 16439] [H16442]
2d660 5b 48 31 36 34 34 35 5d 20 5b 48 31 36 34 34 38 [H16445] [H16448
2d670 5d 0a 2a 2a 20 5b 48 31 36 34 35 31 5d 20 5b 48 ].** [H16451] [H
2d680 31 36 34 35 34 5d 20 5b 48 31 36 34 35 37 5d 20 16454] [H16457]
2d690 5b 48 31 36 34 36 30 5d 20 5b 48 31 36 34 36 33 [H16460] [H16463
2d6a0 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ].*/.SQLITE_API
2d6b0 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 void sqlite3_res
2d6c0 75 6c 74 5f 62 6c 6f 62 28 73 71 6c 69 74 65 33 ult_blob(sqlite3
2d6d0 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e 73 74 _context*, const
2d6e0 20 76 6f 69 64 2a 2c 20 69 6e 74 2c 20 76 6f 69 void*, int, voi
2d6f0 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 d(*)(void*));.SQ
2d700 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 LITE_API void sq
2d710 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 lite3_result_dou
2d720 62 6c 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 ble(sqlite3_cont
2d730 65 78 74 2a 2c 20 64 6f 75 62 6c 65 29 3b 0a 53 ext*, double);.S
2d740 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 QLITE_API void s
2d750 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 qlite3_result_er
2d760 72 6f 72 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 ror(sqlite3_cont
2d770 65 78 74 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 ext*, const char
2d780 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f *, int);.SQLITE_
2d790 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 API void sqlite3
2d7a0 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 28 _result_error16(
2d7b0 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a sqlite3_context*
2d7c0 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 , const void*, i
2d7d0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 nt);.SQLITE_API
2d7e0 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 void sqlite3_res
2d7f0 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67 ult_error_toobig
2d800 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 (sqlite3_context
2d810 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 *);.SQLITE_API v
2d820 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 oid sqlite3_resu
2d830 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 73 lt_error_nomem(s
2d840 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 qlite3_context*)
2d850 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 ;.SQLITE_API voi
2d860 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 d sqlite3_result
2d870 5f 65 72 72 6f 72 5f 63 6f 64 65 28 73 71 6c 69 _error_code(sqli
2d880 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 69 6e te3_context*, in
2d890 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 t);.SQLITE_API v
2d8a0 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 oid sqlite3_resu
2d8b0 6c 74 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f 63 lt_int(sqlite3_c
2d8c0 6f 6e 74 65 78 74 2a 2c 20 69 6e 74 29 3b 0a 53 ontext*, int);.S
2d8d0 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 QLITE_API void s
2d8e0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e qlite3_result_in
2d8f0 74 36 34 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 t64(sqlite3_cont
2d900 65 78 74 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e ext*, sqlite3_in
2d910 74 36 34 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 t64);.SQLITE_API
2d920 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 void sqlite3_re
2d930 73 75 6c 74 5f 6e 75 6c 6c 28 73 71 6c 69 74 65 sult_null(sqlite
2d940 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 53 51 4c 3_context*);.SQL
2d950 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c ITE_API void sql
2d960 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 ite3_result_text
2d970 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 (sqlite3_context
2d980 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 *, const char*,
2d990 69 6e 74 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 int, void(*)(voi
2d9a0 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 d*));.SQLITE_API
2d9b0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 void sqlite3_re
2d9c0 73 75 6c 74 5f 74 65 78 74 31 36 28 73 71 6c 69 sult_text16(sqli
2d9d0 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 6f te3_context*, co
2d9e0 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 2c 20 nst void*, int,
2d9f0 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b void(*)(void*));
2da00 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 .SQLITE_API void
2da10 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
2da20 74 65 78 74 31 36 6c 65 28 73 71 6c 69 74 65 33 text16le(sqlite3
2da30 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e 73 74 _context*, const
2da40 20 76 6f 69 64 2a 2c 20 69 6e 74 2c 76 6f 69 64 void*, int,void
2da50 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c (*)(void*));.SQL
2da60 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c ITE_API void sql
2da70 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 ite3_result_text
2da80 31 36 62 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 16be(sqlite3_con
2da90 74 65 78 74 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 text*, const voi
2daa0 64 2a 2c 20 69 6e 74 2c 76 6f 69 64 28 2a 29 28 d*, int,void(*)(
2dab0 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f void*));.SQLITE_
2dac0 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 API void sqlite3
2dad0 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 73 71 _result_value(sq
2dae0 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 lite3_context*,
2daf0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b sqlite3_value*);
2db00 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 .SQLITE_API void
2db10 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
2db20 7a 65 72 6f 62 6c 6f 62 28 73 71 6c 69 74 65 33 zeroblob(sqlite3
2db30 5f 63 6f 6e 74 65 78 74 2a 2c 20 69 6e 74 20 6e _context*, int n
2db40 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
2db50 45 46 3a 20 44 65 66 69 6e 65 20 4e 65 77 20 43 EF: Define New C
2db60 6f 6c 6c 61 74 69 6e 67 20 53 65 71 75 65 6e 63 ollating Sequenc
2db70 65 73 20 7b 48 31 36 36 30 30 7d 20 3c 53 32 30 es {H16600} <S20
2db80 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 300>.**.** These
2db90 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 75 functions are u
2dba0 73 65 64 20 74 6f 20 61 64 64 20 6e 65 77 20 63 sed to add new c
2dbb0 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 ollation sequenc
2dbc0 65 73 20 74 6f 20 74 68 65 0a 2a 2a 20 5b 64 61 es to the.** [da
2dbd0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
2dbe0 6e 5d 20 73 70 65 63 69 66 69 65 64 20 61 73 20 n] specified as
2dbf0 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 the first argume
2dc00 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 61 nt..**.** The na
2dc10 6d 65 20 6f 66 20 74 68 65 20 6e 65 77 20 63 6f me of the new co
2dc20 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 llation sequence
2dc30 20 69 73 20 73 70 65 63 69 66 69 65 64 20 61 73 is specified as
2dc40 20 61 20 55 54 46 2d 38 20 73 74 72 69 6e 67 0a a UTF-8 string.
2dc50 2a 2a 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 63 ** for sqlite3_c
2dc60 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 28 reate_collation(
2dc70 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 63 72 ) and sqlite3_cr
2dc80 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 eate_collation_v
2dc90 32 28 29 0a 2a 2a 20 61 6e 64 20 61 20 55 54 46 2().** and a UTF
2dca0 2d 31 36 20 73 74 72 69 6e 67 20 66 6f 72 20 73 -16 string for s
2dcb0 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f qlite3_create_co
2dcc0 6c 6c 61 74 69 6f 6e 31 36 28 29 2e 20 49 6e 20 llation16(). In
2dcd0 61 6c 6c 20 63 61 73 65 73 0a 2a 2a 20 74 68 65 all cases.** the
2dce0 20 6e 61 6d 65 20 69 73 20 70 61 73 73 65 64 20 name is passed
2dcf0 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 66 75 as the second fu
2dd00 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 2e nction argument.
2dd10 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 68 69 72 64 .**.** The third
2dd20 20 61 72 67 75 6d 65 6e 74 20 6d 61 79 20 62 65 argument may be
2dd30 20 6f 6e 65 20 6f 66 20 74 68 65 20 63 6f 6e 73 one of the cons
2dd40 74 61 6e 74 73 20 5b 53 51 4c 49 54 45 5f 55 54 tants [SQLITE_UT
2dd50 46 38 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f F8],.** [SQLITE_
2dd60 55 54 46 31 36 4c 45 5d 2c 20 6f 72 20 5b 53 51 UTF16LE], or [SQ
2dd70 4c 49 54 45 5f 55 54 46 31 36 42 45 5d 2c 20 69 LITE_UTF16BE], i
2dd80 6e 64 69 63 61 74 69 6e 67 20 74 68 61 74 20 74 ndicating that t
2dd90 68 65 20 75 73 65 72 2d 73 75 70 70 6c 69 65 64 he user-supplied
2dda0 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 65 78 70 65 .** routine expe
2ddb0 63 74 73 20 74 6f 20 62 65 20 70 61 73 73 65 64 cts to be passed
2ddc0 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 73 74 72 pointers to str
2ddd0 69 6e 67 73 20 65 6e 63 6f 64 65 64 20 75 73 69 ings encoded usi
2dde0 6e 67 20 55 54 46 2d 38 2c 0a 2a 2a 20 55 54 46 ng UTF-8,.** UTF
2ddf0 2d 31 36 20 6c 69 74 74 6c 65 2d 65 6e 64 69 61 -16 little-endia
2de00 6e 2c 20 6f 72 20 55 54 46 2d 31 36 20 62 69 67 n, or UTF-16 big
2de10 2d 65 6e 64 69 61 6e 2c 20 72 65 73 70 65 63 74 -endian, respect
2de20 69 76 65 6c 79 2e 20 54 68 65 0a 2a 2a 20 74 68 ively. The.** th
2de30 69 72 64 20 61 72 67 75 6d 65 6e 74 20 6d 69 67 ird argument mig
2de40 68 74 20 61 6c 73 6f 20 62 65 20 5b 53 51 4c 49 ht also be [SQLI
2de50 54 45 5f 55 54 46 31 36 5d 20 74 6f 20 69 6e 64 TE_UTF16] to ind
2de60 69 63 61 74 65 20 74 68 61 74 20 74 68 65 20 72 icate that the r
2de70 6f 75 74 69 6e 65 0a 2a 2a 20 65 78 70 65 63 74 outine.** expect
2de80 73 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 62 65 s pointers to be
2de90 20 55 54 46 2d 31 36 20 73 74 72 69 6e 67 73 20 UTF-16 strings
2dea0 69 6e 20 74 68 65 20 6e 61 74 69 76 65 20 62 79 in the native by
2deb0 74 65 20 6f 72 64 65 72 2c 20 6f 72 20 74 68 65 te order, or the
2dec0 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 63 61 6e .** argument can
2ded0 20 62 65 20 5b 53 51 4c 49 54 45 5f 55 54 46 31 be [SQLITE_UTF1
2dee0 36 5f 41 4c 49 47 4e 45 44 5d 20 69 66 20 74 68 6_ALIGNED] if th
2def0 65 0a 2a 2a 20 74 68 65 20 72 6f 75 74 69 6e 65 e.** the routine
2df00 20 65 78 70 65 63 74 73 20 70 6f 69 6e 74 65 72 expects pointer
2df10 73 20 74 6f 20 31 36 2d 62 69 74 20 77 6f 72 64 s to 16-bit word
2df20 20 61 6c 69 67 6e 65 64 20 73 74 72 69 6e 67 73 aligned strings
2df30 0a 2a 2a 20 6f 66 20 55 54 46 2d 31 36 20 69 6e .** of UTF-16 in
2df40 20 74 68 65 20 6e 61 74 69 76 65 20 62 79 74 65 the native byte
2df50 20 6f 72 64 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 20 order..**.** A
2df60 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 75 pointer to the u
2df70 73 65 72 20 73 75 70 70 6c 69 65 64 20 72 6f 75 ser supplied rou
2df80 74 69 6e 65 20 6d 75 73 74 20 62 65 20 70 61 73 tine must be pas
2df90 73 65 64 20 61 73 20 74 68 65 20 66 69 66 74 68 sed as the fifth
2dfa0 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 2e 20 20 49 .** argument. I
2dfb0 66 20 69 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 f it is NULL, th
2dfc0 69 73 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 is is the same a
2dfd0 73 20 64 65 6c 65 74 69 6e 67 20 74 68 65 20 63 s deleting the c
2dfe0 6f 6c 6c 61 74 69 6f 6e 0a 2a 2a 20 73 65 71 75 ollation.** sequ
2dff0 65 6e 63 65 20 28 73 6f 20 74 68 61 74 20 53 51 ence (so that SQ
2e000 4c 69 74 65 20 63 61 6e 6e 6f 74 20 63 61 6c 6c Lite cannot call
2e010 20 69 74 20 61 6e 79 6d 6f 72 65 29 2e 0a 2a 2a it anymore)..**
2e020 20 45 61 63 68 20 74 69 6d 65 20 74 68 65 20 61 Each time the a
2e030 70 70 6c 69 63 61 74 69 6f 6e 20 73 75 70 70 6c pplication suppl
2e040 69 65 64 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 ied function is
2e050 69 6e 76 6f 6b 65 64 2c 20 69 74 20 69 73 20 70 invoked, it is p
2e060 61 73 73 65 64 0a 2a 2a 20 61 73 20 69 74 73 20 assed.** as its
2e070 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 20 first parameter
2e080 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 76 6f a copy of the vo
2e090 69 64 2a 20 70 61 73 73 65 64 20 61 73 20 74 68 id* passed as th
2e0a0 65 20 66 6f 75 72 74 68 20 61 72 67 75 6d 65 6e e fourth argumen
2e0b0 74 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f t.** to sqlite3_
2e0c0 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e create_collation
2e0d0 28 29 20 6f 72 20 73 71 6c 69 74 65 33 5f 63 72 () or sqlite3_cr
2e0e0 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 eate_collation16
2e0f0 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 ()..**.** The re
2e100 6d 61 69 6e 69 6e 67 20 61 72 67 75 6d 65 6e 74 maining argument
2e110 73 20 74 6f 20 74 68 65 20 61 70 70 6c 69 63 61 s to the applica
2e120 74 69 6f 6e 2d 73 75 70 70 6c 69 65 64 20 72 6f tion-supplied ro
2e130 75 74 69 6e 65 20 61 72 65 20 74 77 6f 20 73 74 utine are two st
2e140 72 69 6e 67 73 2c 0a 2a 2a 20 65 61 63 68 20 72 rings,.** each r
2e150 65 70 72 65 73 65 6e 74 65 64 20 62 79 20 61 20 epresented by a
2e160 28 6c 65 6e 67 74 68 2c 20 64 61 74 61 29 20 70 (length, data) p
2e170 61 69 72 20 61 6e 64 20 65 6e 63 6f 64 65 64 20 air and encoded
2e180 69 6e 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 0a in the encoding.
2e190 2a 2a 20 74 68 61 74 20 77 61 73 20 70 61 73 73 ** that was pass
2e1a0 65 64 20 61 73 20 74 68 65 20 74 68 69 72 64 20 ed as the third
2e1b0 61 72 67 75 6d 65 6e 74 20 77 68 65 6e 20 74 68 argument when th
2e1c0 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 e collation sequ
2e1d0 65 6e 63 65 20 77 61 73 0a 2a 2a 20 72 65 67 69 ence was.** regi
2e1e0 73 74 65 72 65 64 2e 20 7b 45 4e 44 7d 20 20 54 stered. {END} T
2e1f0 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 64 he application d
2e200 65 66 69 6e 65 64 20 63 6f 6c 6c 61 74 69 6f 6e efined collation
2e210 20 72 6f 75 74 69 6e 65 20 73 68 6f 75 6c 64 0a routine should.
2e220 2a 2a 20 72 65 74 75 72 6e 20 6e 65 67 61 74 69 ** return negati
2e230 76 65 2c 20 7a 65 72 6f 20 6f 72 20 70 6f 73 69 ve, zero or posi
2e240 74 69 76 65 20 69 66 20 74 68 65 20 66 69 72 73 tive if the firs
2e250 74 20 73 74 72 69 6e 67 20 69 73 20 6c 65 73 73 t string is less
2e260 20 74 68 61 6e 2c 0a 2a 2a 20 65 71 75 61 6c 20 than,.** equal
2e270 74 6f 2c 20 6f 72 20 67 72 65 61 74 65 72 20 74 to, or greater t
2e280 68 61 6e 20 74 68 65 20 73 65 63 6f 6e 64 20 73 han the second s
2e290 74 72 69 6e 67 2e 20 69 2e 65 2e 20 28 53 54 52 tring. i.e. (STR
2e2a0 49 4e 47 31 20 2d 20 53 54 52 49 4e 47 32 29 2e ING1 - STRING2).
2e2b0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 .**.** The sqlit
2e2c0 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 e3_create_collat
2e2d0 69 6f 6e 5f 76 32 28 29 20 77 6f 72 6b 73 20 6c ion_v2() works l
2e2e0 69 6b 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61 ike sqlite3_crea
2e2f0 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 28 29 0a 2a te_collation().*
2e300 2a 20 65 78 63 65 70 74 20 74 68 61 74 20 69 74 * except that it
2e310 20 74 61 6b 65 73 20 61 6e 20 65 78 74 72 61 20 takes an extra
2e320 61 72 67 75 6d 65 6e 74 20 77 68 69 63 68 20 69 argument which i
2e330 73 20 61 20 64 65 73 74 72 75 63 74 6f 72 20 66 s a destructor f
2e340 6f 72 0a 2a 2a 20 74 68 65 20 63 6f 6c 6c 61 74 or.** the collat
2e350 69 6f 6e 2e 20 20 54 68 65 20 64 65 73 74 72 75 ion. The destru
2e360 63 74 6f 72 20 69 73 20 63 61 6c 6c 65 64 20 77 ctor is called w
2e370 68 65 6e 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f hen the collatio
2e380 6e 20 69 73 0a 2a 2a 20 64 65 73 74 72 6f 79 65 n is.** destroye
2e390 64 20 61 6e 64 20 69 73 20 70 61 73 73 65 64 20 d and is passed
2e3a0 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 66 6f a copy of the fo
2e3b0 75 72 74 68 20 70 61 72 61 6d 65 74 65 72 20 76 urth parameter v
2e3c0 6f 69 64 2a 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 oid* pointer.**
2e3d0 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63 of the sqlite3_c
2e3e0 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f reate_collation_
2e3f0 76 32 28 29 2e 0a 2a 2a 20 43 6f 6c 6c 61 74 69 v2()..** Collati
2e400 6f 6e 73 20 61 72 65 20 64 65 73 74 72 6f 79 65 ons are destroye
2e410 64 20 77 68 65 6e 20 74 68 65 79 20 61 72 65 20 d when they are
2e420 6f 76 65 72 72 69 64 64 65 6e 20 62 79 20 6c 61 overridden by la
2e430 74 65 72 20 63 61 6c 6c 73 20 74 6f 20 74 68 65 ter calls to the
2e440 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6f 6e 20 63 72 .** collation cr
2e450 65 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 73 eation functions
2e460 20 6f 72 20 77 68 65 6e 20 74 68 65 20 5b 64 61 or when the [da
2e470 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
2e480 6e 5d 20 69 73 20 63 6c 6f 73 65 64 0a 2a 2a 20 n] is closed.**
2e490 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 63 using [sqlite3_c
2e4a0 6c 6f 73 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 53 lose()]..**.** S
2e4b0 65 65 20 61 6c 73 6f 3a 20 20 5b 73 71 6c 69 74 ee also: [sqlit
2e4c0 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 e3_collation_nee
2e4d0 64 65 64 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 ded()] and [sqli
2e4e0 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 te3_collation_ne
2e4f0 65 64 65 64 31 36 28 29 5d 2e 0a 2a 2a 0a 2a 2a eded16()]..**.**
2e500 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a Requirements:.*
2e510 2a 20 5b 48 31 36 36 30 33 5d 20 5b 48 31 36 36 * [H16603] [H166
2e520 30 34 5d 20 5b 48 31 36 36 30 36 5d 20 5b 48 31 04] [H16606] [H1
2e530 36 36 30 39 5d 20 5b 48 31 36 36 31 32 5d 20 5b 6609] [H16612] [
2e540 48 31 36 36 31 35 5d 20 5b 48 31 36 36 31 38 5d H16615] [H16618]
2e550 20 5b 48 31 36 36 32 31 5d 0a 2a 2a 20 5b 48 31 [H16621].** [H1
2e560 36 36 32 34 5d 20 5b 48 31 36 36 32 37 5d 20 5b 6624] [H16627] [
2e570 48 31 36 36 33 30 5d 0a 2a 2f 0a 53 51 4c 49 54 H16630].*/.SQLIT
2e580 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
2e590 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 3_create_collati
2e5a0 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20 on(. sqlite3*,
2e5b0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
2e5c0 4e 61 6d 65 2c 20 0a 20 20 69 6e 74 20 65 54 65 Name, . int eTe
2e5d0 78 74 52 65 70 2c 20 0a 20 20 76 6f 69 64 2a 2c xtRep, . void*,
2e5e0 0a 20 20 69 6e 74 28 2a 78 43 6f 6d 70 61 72 65 . int(*xCompare
2e5f0 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 )(void*,int,cons
2e600 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 t void*,int,cons
2e610 74 20 76 6f 69 64 2a 29 0a 29 3b 0a 53 51 4c 49 t void*).);.SQLI
2e620 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
2e630 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 e3_create_collat
2e640 69 6f 6e 5f 76 32 28 0a 20 20 73 71 6c 69 74 65 ion_v2(. sqlite
2e650 33 2a 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 3*, . const cha
2e660 72 20 2a 7a 4e 61 6d 65 2c 20 0a 20 20 69 6e 74 r *zName, . int
2e670 20 65 54 65 78 74 52 65 70 2c 20 0a 20 20 76 6f eTextRep, . vo
2e680 69 64 2a 2c 0a 20 20 69 6e 74 28 2a 78 43 6f 6d id*,. int(*xCom
2e690 70 61 72 65 29 28 76 6f 69 64 2a 2c 69 6e 74 2c pare)(void*,int,
2e6a0 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c const void*,int,
2e6b0 63 6f 6e 73 74 20 76 6f 69 64 2a 29 2c 0a 20 20 const void*),.
2e6c0 76 6f 69 64 28 2a 78 44 65 73 74 72 6f 79 29 28 void(*xDestroy)(
2e6d0 76 6f 69 64 2a 29 0a 29 3b 0a 53 51 4c 49 54 45 void*).);.SQLITE
2e6e0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
2e6f0 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f _create_collatio
2e700 6e 31 36 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c n16(. sqlite3*,
2e710 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a . const void *
2e720 7a 4e 61 6d 65 2c 0a 20 20 69 6e 74 20 65 54 65 zName,. int eTe
2e730 78 74 52 65 70 2c 20 0a 20 20 76 6f 69 64 2a 2c xtRep, . void*,
2e740 0a 20 20 69 6e 74 28 2a 78 43 6f 6d 70 61 72 65 . int(*xCompare
2e750 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 )(void*,int,cons
2e760 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 t void*,int,cons
2e770 74 20 76 6f 69 64 2a 29 0a 29 3b 0a 0a 2f 2a 0a t void*).);../*.
2e780 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6c ** CAPI3REF: Col
2e790 6c 61 74 69 6f 6e 20 4e 65 65 64 65 64 20 43 61 lation Needed Ca
2e7a0 6c 6c 62 61 63 6b 73 20 7b 48 31 36 37 30 30 7d llbacks {H16700}
2e7b0 20 3c 53 32 30 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 <S20300>.**.**
2e7c0 54 6f 20 61 76 6f 69 64 20 68 61 76 69 6e 67 20 To avoid having
2e7d0 74 6f 20 72 65 67 69 73 74 65 72 20 61 6c 6c 20 to register all
2e7e0 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e collation sequen
2e7f0 63 65 73 20 62 65 66 6f 72 65 20 61 20 64 61 74 ces before a dat
2e800 61 62 61 73 65 0a 2a 2a 20 63 61 6e 20 62 65 20 abase.** can be
2e810 75 73 65 64 2c 20 61 20 73 69 6e 67 6c 65 20 63 used, a single c
2e820 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e allback function
2e830 20 6d 61 79 20 62 65 20 72 65 67 69 73 74 65 72 may be register
2e840 65 64 20 77 69 74 68 20 74 68 65 0a 2a 2a 20 5b ed with the.** [
2e850 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
2e860 69 6f 6e 5d 20 74 6f 20 62 65 20 63 61 6c 6c 65 ion] to be calle
2e870 64 20 77 68 65 6e 65 76 65 72 20 61 6e 20 75 6e d whenever an un
2e880 64 65 66 69 6e 65 64 20 63 6f 6c 6c 61 74 69 6f defined collatio
2e890 6e 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20 69 73 n.** sequence is
2e8a0 20 72 65 71 75 69 72 65 64 2e 0a 2a 2a 0a 2a 2a required..**.**
2e8b0 20 49 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e If the function
2e8c0 20 69 73 20 72 65 67 69 73 74 65 72 65 64 20 75 is registered u
2e8d0 73 69 6e 67 20 74 68 65 20 73 71 6c 69 74 65 33 sing the sqlite3
2e8e0 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 _collation_neede
2e8f0 64 28 29 20 41 50 49 2c 0a 2a 2a 20 74 68 65 6e d() API,.** then
2e900 20 69 74 20 69 73 20 70 61 73 73 65 64 20 74 68 it is passed th
2e910 65 20 6e 61 6d 65 73 20 6f 66 20 75 6e 64 65 66 e names of undef
2e920 69 6e 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 ined collation s
2e930 65 71 75 65 6e 63 65 73 20 61 73 20 73 74 72 69 equences as stri
2e940 6e 67 73 0a 2a 2a 20 65 6e 63 6f 64 65 64 20 69 ngs.** encoded i
2e950 6e 20 55 54 46 2d 38 2e 20 7b 48 31 36 37 30 33 n UTF-8. {H16703
2e960 7d 20 49 66 20 73 71 6c 69 74 65 33 5f 63 6f 6c } If sqlite3_col
2e970 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36 28 lation_needed16(
2e980 29 20 69 73 20 75 73 65 64 2c 0a 2a 2a 20 74 68 ) is used,.** th
2e990 65 20 6e 61 6d 65 73 20 61 72 65 20 70 61 73 73 e names are pass
2e9a0 65 64 20 61 73 20 55 54 46 2d 31 36 20 69 6e 20 ed as UTF-16 in
2e9b0 6d 61 63 68 69 6e 65 20 6e 61 74 69 76 65 20 62 machine native b
2e9c0 79 74 65 20 6f 72 64 65 72 2e 0a 2a 2a 20 41 20 yte order..** A
2e9d0 63 61 6c 6c 20 74 6f 20 65 69 74 68 65 72 20 66 call to either f
2e9e0 75 6e 63 74 69 6f 6e 20 72 65 70 6c 61 63 65 73 unction replaces
2e9f0 20 61 6e 79 20 65 78 69 73 74 69 6e 67 20 63 61 any existing ca
2ea00 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 57 68 llback..**.** Wh
2ea10 65 6e 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 en the callback
2ea20 69 73 20 69 6e 76 6f 6b 65 64 2c 20 74 68 65 20 is invoked, the
2ea30 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 70 first argument p
2ea40 61 73 73 65 64 20 69 73 20 61 20 63 6f 70 79 0a assed is a copy.
2ea50 2a 2a 20 6f 66 20 74 68 65 20 73 65 63 6f 6e 64 ** of the second
2ea60 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c argument to sql
2ea70 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e ite3_collation_n
2ea80 65 65 64 65 64 28 29 20 6f 72 0a 2a 2a 20 73 71 eeded() or.** sq
2ea90 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f lite3_collation_
2eaa0 6e 65 65 64 65 64 31 36 28 29 2e 20 20 54 68 65 needed16(). The
2eab0 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 second argument
2eac0 20 69 73 20 74 68 65 20 64 61 74 61 62 61 73 65 is the database
2ead0 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 .** connection.
2eae0 20 54 68 65 20 74 68 69 72 64 20 61 72 67 75 6d The third argum
2eaf0 65 6e 74 20 69 73 20 6f 6e 65 20 6f 66 20 5b 53 ent is one of [S
2eb00 51 4c 49 54 45 5f 55 54 46 38 5d 2c 20 5b 53 51 QLITE_UTF8], [SQ
2eb10 4c 49 54 45 5f 55 54 46 31 36 42 45 5d 2c 0a 2a LITE_UTF16BE],.*
2eb20 2a 20 6f 72 20 5b 53 51 4c 49 54 45 5f 55 54 46 * or [SQLITE_UTF
2eb30 31 36 4c 45 5d 2c 20 69 6e 64 69 63 61 74 69 6e 16LE], indicatin
2eb40 67 20 74 68 65 20 6d 6f 73 74 20 64 65 73 69 72 g the most desir
2eb50 61 62 6c 65 20 66 6f 72 6d 20 6f 66 20 74 68 65 able form of the
2eb60 20 63 6f 6c 6c 61 74 69 6f 6e 0a 2a 2a 20 73 65 collation.** se
2eb70 71 75 65 6e 63 65 20 66 75 6e 63 74 69 6f 6e 20 quence function
2eb80 72 65 71 75 69 72 65 64 2e 20 20 54 68 65 20 66 required. The f
2eb90 6f 75 72 74 68 20 70 61 72 61 6d 65 74 65 72 20 ourth parameter
2eba0 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 is the name of t
2ebb0 68 65 0a 2a 2a 20 72 65 71 75 69 72 65 64 20 63 he.** required c
2ebc0 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 ollation sequenc
2ebd0 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c e..**.** The cal
2ebe0 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 73 lback function s
2ebf0 68 6f 75 6c 64 20 72 65 67 69 73 74 65 72 20 74 hould register t
2ec00 68 65 20 64 65 73 69 72 65 64 20 63 6f 6c 6c 61 he desired colla
2ec10 74 69 6f 6e 20 75 73 69 6e 67 0a 2a 2a 20 5b 73 tion using.** [s
2ec20 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f qlite3_create_co
2ec30 6c 6c 61 74 69 6f 6e 28 29 5d 2c 20 5b 73 71 6c llation()], [sql
2ec40 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c ite3_create_coll
2ec50 61 74 69 6f 6e 31 36 28 29 5d 2c 20 6f 72 0a 2a ation16()], or.*
2ec60 2a 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 * [sqlite3_creat
2ec70 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 29 e_collation_v2()
2ec80 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 ]..**.** Require
2ec90 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36 37 30 ments:.** [H1670
2eca0 32 5d 20 5b 48 31 36 37 30 34 5d 20 5b 48 31 36 2] [H16704] [H16
2ecb0 37 30 36 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 706].*/.SQLITE_A
2ecc0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 PI int sqlite3_c
2ecd0 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 28 ollation_needed(
2ece0 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20 0a 20 20 . sqlite3*, .
2ecf0 76 6f 69 64 2a 2c 20 0a 20 20 76 6f 69 64 28 2a void*, . void(*
2ed00 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 2a )(void*,sqlite3*
2ed10 2c 69 6e 74 20 65 54 65 78 74 52 65 70 2c 63 6f ,int eTextRep,co
2ed20 6e 73 74 20 63 68 61 72 2a 29 0a 29 3b 0a 53 51 nst char*).);.SQ
2ed30 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
2ed40 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e ite3_collation_n
2ed50 65 65 64 65 64 31 36 28 0a 20 20 73 71 6c 69 74 eeded16(. sqlit
2ed60 65 33 2a 2c 20 0a 20 20 76 6f 69 64 2a 2c 0a 20 e3*, . void*,.
2ed70 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 2c 73 void(*)(void*,s
2ed80 71 6c 69 74 65 33 2a 2c 69 6e 74 20 65 54 65 78 qlite3*,int eTex
2ed90 74 52 65 70 2c 63 6f 6e 73 74 20 76 6f 69 64 2a tRep,const void*
2eda0 29 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 53 70 65 63 ).);../*.** Spec
2edb0 69 66 79 20 74 68 65 20 6b 65 79 20 66 6f 72 20 ify the key for
2edc0 61 6e 20 65 6e 63 72 79 70 74 65 64 20 64 61 74 an encrypted dat
2edd0 61 62 61 73 65 2e 20 20 54 68 69 73 20 72 6f 75 abase. This rou
2ede0 74 69 6e 65 20 73 68 6f 75 6c 64 20 62 65 0a 2a tine should be.*
2edf0 2a 20 63 61 6c 6c 65 64 20 72 69 67 68 74 20 61 * called right a
2ee00 66 74 65 72 20 73 71 6c 69 74 65 33 5f 6f 70 65 fter sqlite3_ope
2ee10 6e 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 n()..**.** The c
2ee20 6f 64 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 ode to implement
2ee30 20 74 68 69 73 20 41 50 49 20 69 73 20 6e 6f 74 this API is not
2ee40 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 74 68 available in th
2ee50 65 20 70 75 62 6c 69 63 20 72 65 6c 65 61 73 65 e public release
2ee60 0a 2a 2a 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a .** of SQLite..*
2ee70 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
2ee80 20 73 71 6c 69 74 65 33 5f 6b 65 79 28 0a 20 20 sqlite3_key(.
2ee90 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 sqlite3 *db,
2eea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
2eeb0 2a 20 44 61 74 61 62 61 73 65 20 74 6f 20 62 65 * Database to be
2eec0 20 72 65 6b 65 79 65 64 20 2a 2f 0a 20 20 63 6f rekeyed */. co
2eed0 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 nst void *pKey,
2eee0 69 6e 74 20 6e 4b 65 79 20 20 20 20 20 2f 2a 20 int nKey /*
2eef0 54 68 65 20 6b 65 79 20 2a 2f 0a 29 3b 0a 0a 2f The key */.);../
2ef00 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 *.** Change the
2ef10 6b 65 79 20 6f 6e 20 61 6e 20 6f 70 65 6e 20 64 key on an open d
2ef20 61 74 61 62 61 73 65 2e 20 20 49 66 20 74 68 65 atabase. If the
2ef30 20 63 75 72 72 65 6e 74 20 64 61 74 61 62 61 73 current databas
2ef40 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 65 6e 63 72 e is not.** encr
2ef50 79 70 74 65 64 2c 20 74 68 69 73 20 72 6f 75 74 ypted, this rout
2ef60 69 6e 65 20 77 69 6c 6c 20 65 6e 63 72 79 70 74 ine will encrypt
2ef70 20 69 74 2e 20 20 49 66 20 70 4e 65 77 3d 3d 30 it. If pNew==0
2ef80 20 6f 72 20 6e 4e 65 77 3d 3d 30 2c 20 74 68 65 or nNew==0, the
2ef90 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 69 73 20 .** database is
2efa0 64 65 63 72 79 70 74 65 64 2e 0a 2a 2a 0a 2a 2a decrypted..**.**
2efb0 20 54 68 65 20 63 6f 64 65 20 74 6f 20 69 6d 70 The code to imp
2efc0 6c 65 6d 65 6e 74 20 74 68 69 73 20 41 50 49 20 lement this API
2efd0 69 73 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 is not available
2efe0 20 69 6e 20 74 68 65 20 70 75 62 6c 69 63 20 72 in the public r
2eff0 65 6c 65 61 73 65 0a 2a 2a 20 6f 66 20 53 51 4c elease.** of SQL
2f000 69 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 ite..*/.SQLITE_A
2f010 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 72 PI int sqlite3_r
2f020 65 6b 65 79 28 0a 20 20 73 71 6c 69 74 65 33 20 ekey(. sqlite3
2f030 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 *db,
2f040 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 /* Databa
2f050 73 65 20 74 6f 20 62 65 20 72 65 6b 65 79 65 64 se to be rekeyed
2f060 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 */. const void
2f070 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79 *pKey, int nKey
2f080 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 /* The new
2f090 6b 65 79 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a key */.);../*.**
2f0a0 20 43 41 50 49 33 52 45 46 3a 20 53 75 73 70 65 CAPI3REF: Suspe
2f0b0 6e 64 20 45 78 65 63 75 74 69 6f 6e 20 46 6f 72 nd Execution For
2f0c0 20 41 20 53 68 6f 72 74 20 54 69 6d 65 20 7b 48 A Short Time {H
2f0d0 31 30 35 33 30 7d 20 3c 53 34 30 34 31 30 3e 0a 10530} <S40410>.
2f0e0 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 **.** The sqlite
2f0f0 33 5f 73 6c 65 65 70 28 29 20 66 75 6e 63 74 69 3_sleep() functi
2f100 6f 6e 20 63 61 75 73 65 73 20 74 68 65 20 63 75 on causes the cu
2f110 72 72 65 6e 74 20 74 68 72 65 61 64 20 74 6f 20 rrent thread to
2f120 73 75 73 70 65 6e 64 20 65 78 65 63 75 74 69 6f suspend executio
2f130 6e 0a 2a 2a 20 66 6f 72 20 61 74 20 6c 65 61 73 n.** for at leas
2f140 74 20 61 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 t a number of mi
2f150 6c 6c 69 73 65 63 6f 6e 64 73 20 73 70 65 63 69 lliseconds speci
2f160 66 69 65 64 20 69 6e 20 69 74 73 20 70 61 72 61 fied in its para
2f170 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 meter..**.** If
2f180 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 the operating sy
2f190 73 74 65 6d 20 64 6f 65 73 20 6e 6f 74 20 73 75 stem does not su
2f1a0 70 70 6f 72 74 20 73 6c 65 65 70 20 72 65 71 75 pport sleep requ
2f1b0 65 73 74 73 20 77 69 74 68 0a 2a 2a 20 6d 69 6c ests with.** mil
2f1c0 6c 69 73 65 63 6f 6e 64 20 74 69 6d 65 20 72 65 lisecond time re
2f1d0 73 6f 6c 75 74 69 6f 6e 2c 20 74 68 65 6e 20 74 solution, then t
2f1e0 68 65 20 74 69 6d 65 20 77 69 6c 6c 20 62 65 20 he time will be
2f1f0 72 6f 75 6e 64 65 64 20 75 70 20 74 6f 0a 2a 2a rounded up to.**
2f200 20 74 68 65 20 6e 65 61 72 65 73 74 20 73 65 63 the nearest sec
2f210 6f 6e 64 2e 20 54 68 65 20 6e 75 6d 62 65 72 20 ond. The number
2f220 6f 66 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 of milliseconds
2f230 6f 66 20 73 6c 65 65 70 20 61 63 74 75 61 6c 6c of sleep actuall
2f240 79 0a 2a 2a 20 72 65 71 75 65 73 74 65 64 20 66 y.** requested f
2f250 72 6f 6d 20 74 68 65 20 6f 70 65 72 61 74 69 6e rom the operatin
2f260 67 20 73 79 73 74 65 6d 20 69 73 20 72 65 74 75 g system is retu
2f270 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 rned..**.** SQLi
2f280 74 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 te implements th
2f290 69 73 20 69 6e 74 65 72 66 61 63 65 20 62 79 20 is interface by
2f2a0 63 61 6c 6c 69 6e 67 20 74 68 65 20 78 53 6c 65 calling the xSle
2f2b0 65 70 28 29 0a 2a 2a 20 6d 65 74 68 6f 64 20 6f ep().** method o
2f2c0 66 20 74 68 65 20 64 65 66 61 75 6c 74 20 5b 73 f the default [s
2f2d0 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65 qlite3_vfs] obje
2f2e0 63 74 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 ct..**.** Requir
2f2f0 65 6d 65 6e 74 73 3a 20 5b 48 31 30 35 33 33 5d ements: [H10533]
2f300 20 5b 48 31 30 35 33 36 5d 0a 2a 2f 0a 53 51 4c [H10536].*/.SQL
2f310 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
2f320 74 65 33 5f 73 6c 65 65 70 28 69 6e 74 29 3b 0a te3_sleep(int);.
2f330 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
2f340 20 4e 61 6d 65 20 4f 66 20 54 68 65 20 46 6f 6c Name Of The Fol
2f350 64 65 72 20 48 6f 6c 64 69 6e 67 20 54 65 6d 70 der Holding Temp
2f360 6f 72 61 72 79 20 46 69 6c 65 73 20 7b 48 31 30 orary Files {H10
2f370 33 31 30 7d 20 3c 53 32 30 30 30 30 3e 0a 2a 2a 310} <S20000>.**
2f380 0a 2a 2a 20 49 66 20 74 68 69 73 20 67 6c 6f 62 .** If this glob
2f390 61 6c 20 76 61 72 69 61 62 6c 65 20 69 73 20 6d al variable is m
2f3a0 61 64 65 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 ade to point to
2f3b0 61 20 73 74 72 69 6e 67 20 77 68 69 63 68 20 69 a string which i
2f3c0 73 0a 2a 2a 20 74 68 65 20 6e 61 6d 65 20 6f 66 s.** the name of
2f3d0 20 61 20 66 6f 6c 64 65 72 20 28 61 2e 6b 2e 61 a folder (a.k.a
2f3e0 2e 20 64 69 72 65 63 74 6f 72 79 29 2c 20 74 68 . directory), th
2f3f0 65 6e 20 61 6c 6c 20 74 65 6d 70 6f 72 61 72 79 en all temporary
2f400 20 66 69 6c 65 73 0a 2a 2a 20 63 72 65 61 74 65 files.** create
2f410 64 20 62 79 20 53 51 4c 69 74 65 20 77 69 6c 6c d by SQLite will
2f420 20 62 65 20 70 6c 61 63 65 64 20 69 6e 20 74 68 be placed in th
2f430 61 74 20 64 69 72 65 63 74 6f 72 79 2e 20 20 49 at directory. I
2f440 66 20 74 68 69 73 20 76 61 72 69 61 62 6c 65 0a f this variable.
2f450 2a 2a 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 ** is a NULL poi
2f460 6e 74 65 72 2c 20 74 68 65 6e 20 53 51 4c 69 74 nter, then SQLit
2f470 65 20 70 65 72 66 6f 72 6d 73 20 61 20 73 65 61 e performs a sea
2f480 72 63 68 20 66 6f 72 20 61 6e 20 61 70 70 72 6f rch for an appro
2f490 70 72 69 61 74 65 0a 2a 2a 20 74 65 6d 70 6f 72 priate.** tempor
2f4a0 61 72 79 20 66 69 6c 65 20 64 69 72 65 63 74 6f ary file directo
2f4b0 72 79 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 ry..**.** It is
2f4c0 6e 6f 74 20 73 61 66 65 20 74 6f 20 72 65 61 64 not safe to read
2f4d0 20 6f 72 20 6d 6f 64 69 66 79 20 74 68 69 73 20 or modify this
2f4e0 76 61 72 69 61 62 6c 65 20 69 6e 20 6d 6f 72 65 variable in more
2f4f0 20 74 68 61 6e 20 6f 6e 65 0a 2a 2a 20 74 68 72 than one.** thr
2f500 65 61 64 20 61 74 20 61 20 74 69 6d 65 2e 20 20 ead at a time.
2f510 49 74 20 69 73 20 6e 6f 74 20 73 61 66 65 20 74 It is not safe t
2f520 6f 20 72 65 61 64 20 6f 72 20 6d 6f 64 69 66 79 o read or modify
2f530 20 74 68 69 73 20 76 61 72 69 61 62 6c 65 0a 2a this variable.*
2f540 2a 20 69 66 20 61 20 5b 64 61 74 61 62 61 73 65 * if a [database
2f550 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 69 73 20 connection] is
2f560 62 65 69 6e 67 20 75 73 65 64 20 61 74 20 74 68 being used at th
2f570 65 20 73 61 6d 65 20 74 69 6d 65 20 69 6e 20 61 e same time in a
2f580 20 73 65 70 61 72 61 74 65 0a 2a 2a 20 74 68 72 separate.** thr
2f590 65 61 64 2e 0a 2a 2a 20 49 74 20 69 73 20 69 6e ead..** It is in
2f5a0 74 65 6e 64 65 64 20 74 68 61 74 20 74 68 69 73 tended that this
2f5b0 20 76 61 72 69 61 62 6c 65 20 62 65 20 73 65 74 variable be set
2f5c0 20 6f 6e 63 65 0a 2a 2a 20 61 73 20 70 61 72 74 once.** as part
2f5d0 20 6f 66 20 70 72 6f 63 65 73 73 20 69 6e 69 74 of process init
2f5e0 69 61 6c 69 7a 61 74 69 6f 6e 20 61 6e 64 20 62 ialization and b
2f5f0 65 66 6f 72 65 20 61 6e 79 20 53 51 4c 69 74 65 efore any SQLite
2f600 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20 72 6f interface.** ro
2f610 75 74 69 6e 65 73 20 68 61 76 65 20 62 65 65 6e utines have been
2f620 20 63 61 6c 6c 65 64 20 61 6e 64 20 74 68 61 74 called and that
2f630 20 74 68 69 73 20 76 61 72 69 61 62 6c 65 20 72 this variable r
2f640 65 6d 61 69 6e 20 75 6e 63 68 61 6e 67 65 64 0a emain unchanged.
2f650 2a 2a 20 74 68 65 72 65 61 66 74 65 72 2e 0a 2a ** thereafter..*
2f660 2a 0a 2a 2a 20 54 68 65 20 5b 74 65 6d 70 5f 73 *.** The [temp_s
2f670 74 6f 72 65 5f 64 69 72 65 63 74 6f 72 79 20 70 tore_directory p
2f680 72 61 67 6d 61 5d 20 6d 61 79 20 6d 6f 64 69 66 ragma] may modif
2f690 79 20 74 68 69 73 20 76 61 72 69 61 62 6c 65 20 y this variable
2f6a0 61 6e 64 20 63 61 75 73 65 0a 2a 2a 20 69 74 20 and cause.** it
2f6b0 74 6f 20 70 6f 69 6e 74 20 74 6f 20 6d 65 6d 6f to point to memo
2f6c0 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d ry obtained from
2f6d0 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 [sqlite3_malloc
2f6e0 5d 2e 20 20 46 75 72 74 68 65 72 6d 6f 72 65 2c ]. Furthermore,
2f6f0 0a 2a 2a 20 74 68 65 20 5b 74 65 6d 70 5f 73 74 .** the [temp_st
2f700 6f 72 65 5f 64 69 72 65 63 74 6f 72 79 20 70 72 ore_directory pr
2f710 61 67 6d 61 5d 20 61 6c 77 61 79 73 20 61 73 73 agma] always ass
2f720 75 6d 65 73 20 74 68 61 74 20 61 6e 79 20 73 74 umes that any st
2f730 72 69 6e 67 0a 2a 2a 20 74 68 61 74 20 74 68 69 ring.** that thi
2f740 73 20 76 61 72 69 61 62 6c 65 20 70 6f 69 6e 74 s variable point
2f750 73 20 74 6f 20 69 73 20 68 65 6c 64 20 69 6e 20 s to is held in
2f760 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 memory obtained
2f770 66 72 6f 6d 20 0a 2a 2a 20 5b 73 71 6c 69 74 65 from .** [sqlite
2f780 33 5f 6d 61 6c 6c 6f 63 5d 20 61 6e 64 20 74 68 3_malloc] and th
2f790 65 20 70 72 61 67 6d 61 20 6d 61 79 20 61 74 74 e pragma may att
2f7a0 65 6d 70 74 20 74 6f 20 66 72 65 65 20 74 68 61 empt to free tha
2f7b0 74 20 6d 65 6d 6f 72 79 0a 2a 2a 20 75 73 69 6e t memory.** usin
2f7c0 67 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 5d g [sqlite3_free]
2f7d0 2e 0a 2a 2a 20 48 65 6e 63 65 2c 20 69 66 20 74 ..** Hence, if t
2f7e0 68 69 73 20 76 61 72 69 61 62 6c 65 20 69 73 20 his variable is
2f7f0 6d 6f 64 69 66 69 65 64 20 64 69 72 65 63 74 6c modified directl
2f800 79 2c 20 65 69 74 68 65 72 20 69 74 20 73 68 6f y, either it sho
2f810 75 6c 64 20 62 65 0a 2a 2a 20 6d 61 64 65 20 4e uld be.** made N
2f820 55 4c 4c 20 6f 72 20 6d 61 64 65 20 74 6f 20 70 ULL or made to p
2f830 6f 69 6e 74 20 74 6f 20 6d 65 6d 6f 72 79 20 6f oint to memory o
2f840 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 5b 73 71 btained from [sq
2f850 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 5d 0a 2a 2a lite3_malloc].**
2f860 20 6f 72 20 65 6c 73 65 20 74 68 65 20 75 73 65 or else the use
2f870 20 6f 66 20 74 68 65 20 5b 74 65 6d 70 5f 73 74 of the [temp_st
2f880 6f 72 65 5f 64 69 72 65 63 74 6f 72 79 20 70 72 ore_directory pr
2f890 61 67 6d 61 5d 20 73 68 6f 75 6c 64 20 62 65 20 agma] should be
2f8a0 61 76 6f 69 64 65 64 2e 0a 2a 2f 0a 53 51 4c 49 avoided..*/.SQLI
2f8b0 54 45 5f 41 50 49 20 63 68 61 72 20 2a 73 71 6c TE_API char *sql
2f8c0 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 63 74 ite3_temp_direct
2f8d0 6f 72 79 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 ory;../*.** CAPI
2f8e0 33 52 45 46 3a 20 54 65 73 74 20 46 6f 72 20 41 3REF: Test For A
2f8f0 75 74 6f 2d 43 6f 6d 6d 69 74 20 4d 6f 64 65 20 uto-Commit Mode
2f900 7b 48 31 32 39 33 30 7d 20 3c 53 36 30 32 30 30 {H12930} <S60200
2f910 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b >.** KEYWORDS: {
2f920 61 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 7d autocommit mode}
2f930 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 .**.** The sqlit
2f940 65 33 5f 67 65 74 5f 61 75 74 6f 63 6f 6d 6d 69 e3_get_autocommi
2f950 74 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 t() interface re
2f960 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 20 6f turns non-zero o
2f970 72 0a 2a 2a 20 7a 65 72 6f 20 69 66 20 74 68 65 r.** zero if the
2f980 20 67 69 76 65 6e 20 64 61 74 61 62 61 73 65 20 given database
2f990 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 6f 72 connection is or
2f9a0 20 69 73 20 6e 6f 74 20 69 6e 20 61 75 74 6f 63 is not in autoc
2f9b0 6f 6d 6d 69 74 20 6d 6f 64 65 2c 0a 2a 2a 20 72 ommit mode,.** r
2f9c0 65 73 70 65 63 74 69 76 65 6c 79 2e 20 20 41 75 espectively. Au
2f9d0 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 20 69 73 tocommit mode is
2f9e0 20 6f 6e 20 62 79 20 64 65 66 61 75 6c 74 2e 0a on by default..
2f9f0 2a 2a 20 41 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f ** Autocommit mo
2fa00 64 65 20 69 73 20 64 69 73 61 62 6c 65 64 20 62 de is disabled b
2fa10 79 20 61 20 5b 42 45 47 49 4e 5d 20 73 74 61 74 y a [BEGIN] stat
2fa20 65 6d 65 6e 74 2e 0a 2a 2a 20 41 75 74 6f 63 6f ement..** Autoco
2fa30 6d 6d 69 74 20 6d 6f 64 65 20 69 73 20 72 65 2d mmit mode is re-
2fa40 65 6e 61 62 6c 65 64 20 62 79 20 61 20 5b 43 4f enabled by a [CO
2fa50 4d 4d 49 54 5d 20 6f 72 20 5b 52 4f 4c 4c 42 41 MMIT] or [ROLLBA
2fa60 43 4b 5d 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 63 65 CK]..**.** If ce
2fa70 72 74 61 69 6e 20 6b 69 6e 64 73 20 6f 66 20 65 rtain kinds of e
2fa80 72 72 6f 72 73 20 6f 63 63 75 72 20 6f 6e 20 61 rrors occur on a
2fa90 20 73 74 61 74 65 6d 65 6e 74 20 77 69 74 68 69 statement withi
2faa0 6e 20 61 20 6d 75 6c 74 69 2d 73 74 61 74 65 6d n a multi-statem
2fab0 65 6e 74 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 ent.** transacti
2fac0 6f 6e 20 28 65 72 72 6f 72 73 20 69 6e 63 6c 75 on (errors inclu
2fad0 64 69 6e 67 20 5b 53 51 4c 49 54 45 5f 46 55 4c ding [SQLITE_FUL
2fae0 4c 5d 2c 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 L], [SQLITE_IOER
2faf0 52 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4e R],.** [SQLITE_N
2fb00 4f 4d 45 4d 5d 2c 20 5b 53 51 4c 49 54 45 5f 42 OMEM], [SQLITE_B
2fb10 55 53 59 5d 2c 20 61 6e 64 20 5b 53 51 4c 49 54 USY], and [SQLIT
2fb20 45 5f 49 4e 54 45 52 52 55 50 54 5d 29 20 74 68 E_INTERRUPT]) th
2fb30 65 6e 20 74 68 65 0a 2a 2a 20 74 72 61 6e 73 61 en the.** transa
2fb40 63 74 69 6f 6e 20 6d 69 67 68 74 20 62 65 20 72 ction might be r
2fb50 6f 6c 6c 65 64 20 62 61 63 6b 20 61 75 74 6f 6d olled back autom
2fb60 61 74 69 63 61 6c 6c 79 2e 20 20 54 68 65 20 6f atically. The o
2fb70 6e 6c 79 20 77 61 79 20 74 6f 0a 2a 2a 20 66 69 nly way to.** fi
2fb80 6e 64 20 6f 75 74 20 77 68 65 74 68 65 72 20 53 nd out whether S
2fb90 51 4c 69 74 65 20 61 75 74 6f 6d 61 74 69 63 61 QLite automatica
2fba0 6c 6c 79 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 lly rolled back
2fbb0 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 the transaction
2fbc0 61 66 74 65 72 0a 2a 2a 20 61 6e 20 65 72 72 6f after.** an erro
2fbd0 72 20 69 73 20 74 6f 20 75 73 65 20 74 68 69 73 r is to use this
2fbe0 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a function..**.**
2fbf0 20 49 66 20 61 6e 6f 74 68 65 72 20 74 68 72 65 If another thre
2fc00 61 64 20 63 68 61 6e 67 65 73 20 74 68 65 20 61 ad changes the a
2fc10 75 74 6f 63 6f 6d 6d 69 74 20 73 74 61 74 75 73 utocommit status
2fc20 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
2fc30 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 .** connection w
2fc40 68 69 6c 65 20 74 68 69 73 20 72 6f 75 74 69 6e hile this routin
2fc50 65 20 69 73 20 72 75 6e 6e 69 6e 67 2c 20 74 68 e is running, th
2fc60 65 6e 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 en the return va
2fc70 6c 75 65 0a 2a 2a 20 69 73 20 75 6e 64 65 66 69 lue.** is undefi
2fc80 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 ned..**.** Requi
2fc90 72 65 6d 65 6e 74 73 3a 20 5b 48 31 32 39 33 31 rements: [H12931
2fca0 5d 20 5b 48 31 32 39 33 32 5d 20 5b 48 31 32 39 ] [H12932] [H129
2fcb0 33 33 5d 20 5b 48 31 32 39 33 34 5d 0a 2a 2f 0a 33] [H12934].*/.
2fcc0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
2fcd0 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 74 6f 63 qlite3_get_autoc
2fce0 6f 6d 6d 69 74 28 73 71 6c 69 74 65 33 2a 29 3b ommit(sqlite3*);
2fcf0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
2fd00 3a 20 46 69 6e 64 20 54 68 65 20 44 61 74 61 62 : Find The Datab
2fd10 61 73 65 20 48 61 6e 64 6c 65 20 4f 66 20 41 20 ase Handle Of A
2fd20 50 72 65 70 61 72 65 64 20 53 74 61 74 65 6d 65 Prepared Stateme
2fd30 6e 74 20 7b 48 31 33 31 32 30 7d 20 3c 53 36 30 nt {H13120} <S60
2fd40 36 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 600>.**.** The s
2fd50 71 6c 69 74 65 33 5f 64 62 5f 68 61 6e 64 6c 65 qlite3_db_handle
2fd60 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 interface retur
2fd70 6e 73 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 ns the [database
2fd80 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 68 61 6e connection] han
2fd90 64 6c 65 0a 2a 2a 20 74 6f 20 77 68 69 63 68 20 dle.** to which
2fda0 61 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 a [prepared stat
2fdb0 65 6d 65 6e 74 5d 20 62 65 6c 6f 6e 67 73 2e 20 ement] belongs.
2fdc0 20 54 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 The [database c
2fdd0 6f 6e 6e 65 63 74 69 6f 6e 5d 0a 2a 2a 20 72 65 onnection].** re
2fde0 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 turned by sqlite
2fdf0 33 5f 64 62 5f 68 61 6e 64 6c 65 20 69 73 20 74 3_db_handle is t
2fe00 68 65 20 73 61 6d 65 20 5b 64 61 74 61 62 61 73 he same [databas
2fe10 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 74 68 e connection] th
2fe20 61 74 20 77 61 73 20 74 68 65 20 66 69 72 73 74 at was the first
2fe30 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 argument.** to
2fe40 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 the [sqlite3_pre
2fe50 70 61 72 65 5f 76 32 28 29 5d 20 63 61 6c 6c 20 pare_v2()] call
2fe60 28 6f 72 20 69 74 73 20 76 61 72 69 61 6e 74 73 (or its variants
2fe70 29 20 74 68 61 74 20 77 61 73 20 75 73 65 64 20 ) that was used
2fe80 74 6f 0a 2a 2a 20 63 72 65 61 74 65 20 74 68 65 to.** create the
2fe90 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 20 74 68 statement in th
2fea0 65 20 66 69 72 73 74 20 70 6c 61 63 65 2e 0a 2a e first place..*
2feb0 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 *.** Requirement
2fec0 73 3a 20 5b 48 31 33 31 32 33 5d 0a 2a 2f 0a 53 s: [H13123].*/.S
2fed0 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 QLITE_API sqlite
2fee0 33 20 2a 73 71 6c 69 74 65 33 5f 64 62 5f 68 61 3 *sqlite3_db_ha
2fef0 6e 64 6c 65 28 73 71 6c 69 74 65 33 5f 73 74 6d ndle(sqlite3_stm
2ff00 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 t*);../*.** CAPI
2ff10 33 52 45 46 3a 20 46 69 6e 64 20 74 68 65 20 6e 3REF: Find the n
2ff20 65 78 74 20 70 72 65 70 61 72 65 64 20 73 74 61 ext prepared sta
2ff30 74 65 6d 65 6e 74 20 7b 48 31 33 31 34 30 7d 20 tement {H13140}
2ff40 3c 53 36 30 36 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 <S60600>.**.** T
2ff50 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 72 65 his interface re
2ff60 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 turns a pointer
2ff70 74 6f 20 74 68 65 20 6e 65 78 74 20 5b 70 72 65 to the next [pre
2ff80 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d pared statement]
2ff90 20 61 66 74 65 72 0a 2a 2a 20 70 53 74 6d 74 20 after.** pStmt
2ffa0 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
2ffb0 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f the [database co
2ffc0 6e 6e 65 63 74 69 6f 6e 5d 20 70 44 62 2e 20 20 nnection] pDb.
2ffd0 49 66 20 70 53 74 6d 74 20 69 73 20 4e 55 4c 4c If pStmt is NULL
2ffe0 0a 2a 2a 20 74 68 65 6e 20 74 68 69 73 20 69 6e .** then this in
2fff0 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 terface returns
30000 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
30010 20 66 69 72 73 74 20 70 72 65 70 61 72 65 64 20 first prepared
30020 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 61 73 73 statement.** ass
30030 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 ociated with the
30040 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
30050 74 69 6f 6e 20 70 44 62 2e 20 20 49 66 20 6e 6f tion pDb. If no
30060 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d prepared statem
30070 65 6e 74 0a 2a 2a 20 73 61 74 69 73 66 69 65 73 ent.** satisfies
30080 20 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20 the conditions
30090 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2c of this routine,
300a0 20 69 74 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c it returns NULL
300b0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 64 61 74 ..**.** The [dat
300c0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
300d0 5d 20 70 6f 69 6e 74 65 72 20 44 20 69 6e 20 61 ] pointer D in a
300e0 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 5b 73 71 6c call to.** [sql
300f0 69 74 65 33 5f 6e 65 78 74 5f 73 74 6d 74 28 44 ite3_next_stmt(D
30100 2c 53 29 5d 20 6d 75 73 74 20 72 65 66 65 72 20 ,S)] must refer
30110 74 6f 20 61 6e 20 6f 70 65 6e 20 64 61 74 61 62 to an open datab
30120 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f ase.** connectio
30130 6e 20 61 6e 64 20 69 6e 20 70 61 72 74 69 63 75 n and in particu
30140 6c 61 72 20 6d 75 73 74 20 6e 6f 74 20 62 65 20 lar must not be
30150 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a a NULL pointer..
30160 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e **.** Requiremen
30170 74 73 3a 20 5b 48 31 33 31 34 33 5d 20 5b 48 31 ts: [H13143] [H1
30180 33 31 34 36 5d 20 5b 48 31 33 31 34 39 5d 20 5b 3146] [H13149] [
30190 48 31 33 31 35 32 5d 0a 2a 2f 0a 53 51 4c 49 54 H13152].*/.SQLIT
301a0 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 73 74 E_API sqlite3_st
301b0 6d 74 20 2a 73 71 6c 69 74 65 33 5f 6e 65 78 74 mt *sqlite3_next
301c0 5f 73 74 6d 74 28 73 71 6c 69 74 65 33 20 2a 70 _stmt(sqlite3 *p
301d0 44 62 2c 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 Db, sqlite3_stmt
301e0 20 2a 70 53 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a *pStmt);../*.**
301f0 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6d 6d 69 CAPI3REF: Commi
30200 74 20 41 6e 64 20 52 6f 6c 6c 62 61 63 6b 20 4e t And Rollback N
30210 6f 74 69 66 69 63 61 74 69 6f 6e 20 43 61 6c 6c otification Call
30220 62 61 63 6b 73 20 7b 48 31 32 39 35 30 7d 20 3c backs {H12950} <
30230 53 36 30 34 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 S60400>.**.** Th
30240 65 20 73 71 6c 69 74 65 33 5f 63 6f 6d 6d 69 74 e sqlite3_commit
30250 5f 68 6f 6f 6b 28 29 20 69 6e 74 65 72 66 61 63 _hook() interfac
30260 65 20 72 65 67 69 73 74 65 72 73 20 61 20 63 61 e registers a ca
30270 6c 6c 62 61 63 6b 0a 2a 2a 20 66 75 6e 63 74 69 llback.** functi
30280 6f 6e 20 74 6f 20 62 65 20 69 6e 76 6f 6b 65 64 on to be invoked
30290 20 77 68 65 6e 65 76 65 72 20 61 20 74 72 61 6e whenever a tran
302a0 73 61 63 74 69 6f 6e 20 69 73 20 5b 43 4f 4d 4d saction is [COMM
302b0 49 54 20 7c 20 63 6f 6d 6d 69 74 74 65 64 5d 2e IT | committed].
302c0 0a 2a 2a 20 41 6e 79 20 63 61 6c 6c 62 61 63 6b .** Any callback
302d0 20 73 65 74 20 62 79 20 61 20 70 72 65 76 69 6f set by a previo
302e0 75 73 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 us call to sqlit
302f0 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 28 29 e3_commit_hook()
30300 0a 2a 2a 20 66 6f 72 20 74 68 65 20 73 61 6d 65 .** for the same
30310 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
30320 74 69 6f 6e 20 69 73 20 6f 76 65 72 72 69 64 64 tion is overridd
30330 65 6e 2e 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 en..** The sqlit
30340 65 33 5f 72 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b e3_rollback_hook
30350 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 67 () interface reg
30360 69 73 74 65 72 73 20 61 20 63 61 6c 6c 62 61 63 isters a callbac
30370 6b 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 74 6f k.** function to
30380 20 62 65 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e be invoked when
30390 65 76 65 72 20 61 20 74 72 61 6e 73 61 63 74 69 ever a transacti
303a0 6f 6e 20 69 73 20 5b 52 4f 4c 4c 42 41 43 4b 20 on is [ROLLBACK
303b0 7c 20 72 6f 6c 6c 65 64 20 62 61 63 6b 5d 2e 0a | rolled back]..
303c0 2a 2a 20 41 6e 79 20 63 61 6c 6c 62 61 63 6b 20 ** Any callback
303d0 73 65 74 20 62 79 20 61 20 70 72 65 76 69 6f 75 set by a previou
303e0 73 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 s call to sqlite
303f0 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 28 29 0a 3_commit_hook().
30400 2a 2a 20 66 6f 72 20 74 68 65 20 73 61 6d 65 20 ** for the same
30410 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
30420 69 6f 6e 20 69 73 20 6f 76 65 72 72 69 64 64 65 ion is overridde
30430 6e 2e 0a 2a 2a 20 54 68 65 20 70 41 72 67 20 61 n..** The pArg a
30440 72 67 75 6d 65 6e 74 20 69 73 20 70 61 73 73 65 rgument is passe
30450 64 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 d through to the
30460 20 63 61 6c 6c 62 61 63 6b 2e 0a 2a 2a 20 49 66 callback..** If
30470 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 6f 6e the callback on
30480 20 61 20 63 6f 6d 6d 69 74 20 68 6f 6f 6b 20 66 a commit hook f
30490 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 unction returns
304a0 6e 6f 6e 2d 7a 65 72 6f 2c 0a 2a 2a 20 74 68 65 non-zero,.** the
304b0 6e 20 74 68 65 20 63 6f 6d 6d 69 74 20 69 73 20 n the commit is
304c0 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f 20 61 converted into a
304d0 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a rollback..**.**
304e0 20 49 66 20 61 6e 6f 74 68 65 72 20 66 75 6e 63 If another func
304f0 74 69 6f 6e 20 77 61 73 20 70 72 65 76 69 6f 75 tion was previou
30500 73 6c 79 20 72 65 67 69 73 74 65 72 65 64 2c 20 sly registered,
30510 69 74 73 0a 2a 2a 20 70 41 72 67 20 76 61 6c 75 its.** pArg valu
30520 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 e is returned.
30530 4f 74 68 65 72 77 69 73 65 20 4e 55 4c 4c 20 69 Otherwise NULL i
30540 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a s returned..**.*
30550 2a 20 54 68 65 20 63 61 6c 6c 62 61 63 6b 20 69 * The callback i
30560 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6d 75 mplementation mu
30570 73 74 20 6e 6f 74 20 64 6f 20 61 6e 79 74 68 69 st not do anythi
30580 6e 67 20 74 68 61 74 20 77 69 6c 6c 20 6d 6f 64 ng that will mod
30590 69 66 79 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 ify.** the datab
305a0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 ase connection t
305b0 68 61 74 20 69 6e 76 6f 6b 65 64 20 74 68 65 20 hat invoked the
305c0 63 61 6c 6c 62 61 63 6b 2e 20 20 41 6e 79 20 61 callback. Any a
305d0 63 74 69 6f 6e 73 0a 2a 2a 20 74 6f 20 6d 6f 64 ctions.** to mod
305e0 69 66 79 20 74 68 65 20 64 61 74 61 62 61 73 65 ify the database
305f0 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6d 75 73 74 connection must
30600 20 62 65 20 64 65 66 65 72 72 65 64 20 75 6e 74 be deferred unt
30610 69 6c 20 61 66 74 65 72 20 74 68 65 0a 2a 2a 20 il after the.**
30620 63 6f 6d 70 6c 65 74 69 6f 6e 20 6f 66 20 74 68 completion of th
30630 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 e [sqlite3_step(
30640 29 5d 20 63 61 6c 6c 20 74 68 61 74 20 74 72 69 )] call that tri
30650 67 67 65 72 65 64 20 74 68 65 20 63 6f 6d 6d 69 ggered the commi
30660 74 0a 2a 2a 20 6f 72 20 72 6f 6c 6c 62 61 63 6b t.** or rollback
30670 20 68 6f 6f 6b 20 69 6e 20 74 68 65 20 66 69 72 hook in the fir
30680 73 74 20 70 6c 61 63 65 2e 0a 2a 2a 20 4e 6f 74 st place..** Not
30690 65 20 74 68 61 74 20 5b 73 71 6c 69 74 65 33 5f e that [sqlite3_
306a0 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20 61 6e prepare_v2()] an
306b0 64 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 d [sqlite3_step(
306c0 29 5d 20 62 6f 74 68 20 6d 6f 64 69 66 79 20 74 )] both modify t
306d0 68 65 69 72 0a 2a 2a 20 64 61 74 61 62 61 73 65 heir.** database
306e0 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 66 6f 72 connections for
306f0 20 74 68 65 20 6d 65 61 6e 69 6e 67 20 6f 66 20 the meaning of
30700 22 6d 6f 64 69 66 79 22 20 69 6e 20 74 68 69 73 "modify" in this
30710 20 70 61 72 61 67 72 61 70 68 2e 0a 2a 2a 0a 2a paragraph..**.*
30720 2a 20 52 65 67 69 73 74 65 72 69 6e 67 20 61 20 * Registering a
30730 4e 55 4c 4c 20 66 75 6e 63 74 69 6f 6e 20 64 69 NULL function di
30740 73 61 62 6c 65 73 20 74 68 65 20 63 61 6c 6c 62 sables the callb
30750 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 ack..**.** When
30760 74 68 65 20 63 6f 6d 6d 69 74 20 68 6f 6f 6b 20 the commit hook
30770 63 61 6c 6c 62 61 63 6b 20 72 6f 75 74 69 6e 65 callback routine
30780 20 72 65 74 75 72 6e 73 20 7a 65 72 6f 2c 20 74 returns zero, t
30790 68 65 20 5b 43 4f 4d 4d 49 54 5d 0a 2a 2a 20 6f he [COMMIT].** o
307a0 70 65 72 61 74 69 6f 6e 20 69 73 20 61 6c 6c 6f peration is allo
307b0 77 65 64 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 wed to continue
307c0 6e 6f 72 6d 61 6c 6c 79 2e 20 20 49 66 20 74 68 normally. If th
307d0 65 20 63 6f 6d 6d 69 74 20 68 6f 6f 6b 0a 2a 2a e commit hook.**
307e0 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 returns non-zer
307f0 6f 2c 20 74 68 65 6e 20 74 68 65 20 5b 43 4f 4d o, then the [COM
30800 4d 49 54 5d 20 69 73 20 63 6f 6e 76 65 72 74 65 MIT] is converte
30810 64 20 69 6e 74 6f 20 61 20 5b 52 4f 4c 4c 42 41 d into a [ROLLBA
30820 43 4b 5d 2e 0a 2a 2a 20 54 68 65 20 72 6f 6c 6c CK]..** The roll
30830 62 61 63 6b 20 68 6f 6f 6b 20 69 73 20 69 6e 76 back hook is inv
30840 6f 6b 65 64 20 6f 6e 20 61 20 72 6f 6c 6c 62 61 oked on a rollba
30850 63 6b 20 74 68 61 74 20 72 65 73 75 6c 74 73 20 ck that results
30860 66 72 6f 6d 20 61 20 63 6f 6d 6d 69 74 0a 2a 2a from a commit.**
30870 20 68 6f 6f 6b 20 72 65 74 75 72 6e 69 6e 67 20 hook returning
30880 6e 6f 6e 2d 7a 65 72 6f 2c 20 6a 75 73 74 20 61 non-zero, just a
30890 73 20 69 74 20 77 6f 75 6c 64 20 62 65 20 77 69 s it would be wi
308a0 74 68 20 61 6e 79 20 6f 74 68 65 72 20 72 6f 6c th any other rol
308b0 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 lback..**.** For
308c0 20 74 68 65 20 70 75 72 70 6f 73 65 73 20 6f 66 the purposes of
308d0 20 74 68 69 73 20 41 50 49 2c 20 61 20 74 72 61 this API, a tra
308e0 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 61 69 64 nsaction is said
308f0 20 74 6f 20 68 61 76 65 20 62 65 65 6e 0a 2a 2a to have been.**
30900 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 69 66 20 rolled back if
30910 61 6e 20 65 78 70 6c 69 63 69 74 20 22 52 4f 4c an explicit "ROL
30920 4c 42 41 43 4b 22 20 73 74 61 74 65 6d 65 6e 74 LBACK" statement
30930 20 69 73 20 65 78 65 63 75 74 65 64 2c 20 6f 72 is executed, or
30940 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20 6f 72 20 .** an error or
30950 63 6f 6e 73 74 72 61 69 6e 74 20 63 61 75 73 65 constraint cause
30960 73 20 61 6e 20 69 6d 70 6c 69 63 69 74 20 72 6f s an implicit ro
30970 6c 6c 62 61 63 6b 20 74 6f 20 6f 63 63 75 72 2e llback to occur.
30980 0a 2a 2a 20 54 68 65 20 72 6f 6c 6c 62 61 63 6b .** The rollback
30990 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 6e 6f 74 callback is not
309a0 20 69 6e 76 6f 6b 65 64 20 69 66 20 61 20 74 72 invoked if a tr
309b0 61 6e 73 61 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 ansaction is.**
309c0 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 72 6f automatically ro
309d0 6c 6c 65 64 20 62 61 63 6b 20 62 65 63 61 75 73 lled back becaus
309e0 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 e the database c
309f0 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 63 6c 6f onnection is clo
30a00 73 65 64 2e 0a 2a 2a 20 54 68 65 20 72 6f 6c 6c sed..** The roll
30a10 62 61 63 6b 20 63 61 6c 6c 62 61 63 6b 20 69 73 back callback is
30a20 20 6e 6f 74 20 69 6e 76 6f 6b 65 64 20 69 66 20 not invoked if
30a30 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 a transaction is
30a40 0a 2a 2a 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 .** rolled back
30a50 62 65 63 61 75 73 65 20 61 20 63 6f 6d 6d 69 74 because a commit
30a60 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e callback return
30a70 65 64 20 6e 6f 6e 2d 7a 65 72 6f 2e 0a 2a 2a 20 ed non-zero..**
30a80 3c 74 6f 64 6f 3e 20 43 68 65 63 6b 20 6f 6e 20 <todo> Check on
30a90 74 68 69 73 20 3c 2f 74 6f 64 6f 3e 0a 2a 2a 0a this </todo>.**.
30aa0 2a 2a 20 53 65 65 20 61 6c 73 6f 20 74 68 65 20 ** See also the
30ab0 5b 73 71 6c 69 74 65 33 5f 75 70 64 61 74 65 5f [sqlite3_update_
30ac0 68 6f 6f 6b 28 29 5d 20 69 6e 74 65 72 66 61 63 hook()] interfac
30ad0 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 e..**.** Require
30ae0 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 39 35 ments:.** [H1295
30af0 31 5d 20 5b 48 31 32 39 35 32 5d 20 5b 48 31 32 1] [H12952] [H12
30b00 39 35 33 5d 20 5b 48 31 32 39 35 34 5d 20 5b 48 953] [H12954] [H
30b10 31 32 39 35 35 5d 0a 2a 2a 20 5b 48 31 32 39 36 12955].** [H1296
30b20 31 5d 20 5b 48 31 32 39 36 32 5d 20 5b 48 31 32 1] [H12962] [H12
30b30 39 36 33 5d 20 5b 48 31 32 39 36 34 5d 0a 2a 2f 963] [H12964].*/
30b40 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 .SQLITE_API void
30b50 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6d 6d 69 74 *sqlite3_commit
30b60 5f 68 6f 6f 6b 28 73 71 6c 69 74 65 33 2a 2c 20 _hook(sqlite3*,
30b70 69 6e 74 28 2a 29 28 76 6f 69 64 2a 29 2c 20 76 int(*)(void*), v
30b80 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 oid*);.SQLITE_AP
30b90 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f I void *sqlite3_
30ba0 72 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 28 73 71 rollback_hook(sq
30bb0 6c 69 74 65 33 2a 2c 20 76 6f 69 64 28 2a 29 28 lite3*, void(*)(
30bc0 76 6f 69 64 20 2a 29 2c 20 76 6f 69 64 2a 29 3b void *), void*);
30bd0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
30be0 3a 20 44 61 74 61 20 43 68 61 6e 67 65 20 4e 6f : Data Change No
30bf0 74 69 66 69 63 61 74 69 6f 6e 20 43 61 6c 6c 62 tification Callb
30c00 61 63 6b 73 20 7b 48 31 32 39 37 30 7d 20 3c 53 acks {H12970} <S
30c10 36 30 34 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 60400>.**.** The
30c20 20 73 71 6c 69 74 65 33 5f 75 70 64 61 74 65 5f sqlite3_update_
30c30 68 6f 6f 6b 28 29 20 69 6e 74 65 72 66 61 63 65 hook() interface
30c40 20 72 65 67 69 73 74 65 72 73 20 61 20 63 61 6c registers a cal
30c50 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 0a 2a lback function.*
30c60 2a 20 77 69 74 68 20 74 68 65 20 5b 64 61 74 61 * with the [data
30c70 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d base connection]
30c80 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 20 74 identified by t
30c90 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e he first argumen
30ca0 74 0a 2a 2a 20 74 6f 20 62 65 20 69 6e 76 6f 6b t.** to be invok
30cb0 65 64 20 77 68 65 6e 65 76 65 72 20 61 20 72 6f ed whenever a ro
30cc0 77 20 69 73 20 75 70 64 61 74 65 64 2c 20 69 6e w is updated, in
30cd0 73 65 72 74 65 64 20 6f 72 20 64 65 6c 65 74 65 serted or delete
30ce0 64 2e 0a 2a 2a 20 41 6e 79 20 63 61 6c 6c 62 61 d..** Any callba
30cf0 63 6b 20 73 65 74 20 62 79 20 61 20 70 72 65 76 ck set by a prev
30d00 69 6f 75 73 20 63 61 6c 6c 20 74 6f 20 74 68 69 ious call to thi
30d10 73 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 66 6f s function.** fo
30d20 72 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 r the same datab
30d30 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 ase connection i
30d40 73 20 6f 76 65 72 72 69 64 64 65 6e 2e 0a 2a 2a s overridden..**
30d50 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 61 .** The second a
30d60 72 67 75 6d 65 6e 74 20 69 73 20 61 20 70 6f 69 rgument is a poi
30d70 6e 74 65 72 20 74 6f 20 74 68 65 20 66 75 6e 63 nter to the func
30d80 74 69 6f 6e 20 74 6f 20 69 6e 76 6f 6b 65 20 77 tion to invoke w
30d90 68 65 6e 20 61 0a 2a 2a 20 72 6f 77 20 69 73 20 hen a.** row is
30da0 75 70 64 61 74 65 64 2c 20 69 6e 73 65 72 74 65 updated, inserte
30db0 64 20 6f 72 20 64 65 6c 65 74 65 64 2e 0a 2a 2a d or deleted..**
30dc0 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d The first argum
30dd0 65 6e 74 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 ent to the callb
30de0 61 63 6b 20 69 73 20 61 20 63 6f 70 79 20 6f 66 ack is a copy of
30df0 20 74 68 65 20 74 68 69 72 64 20 61 72 67 75 6d the third argum
30e00 65 6e 74 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 ent.** to sqlite
30e10 33 5f 75 70 64 61 74 65 5f 68 6f 6f 6b 28 29 2e 3_update_hook().
30e20 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 63 .** The second c
30e30 61 6c 6c 62 61 63 6b 20 61 72 67 75 6d 65 6e 74 allback argument
30e40 20 69 73 20 6f 6e 65 20 6f 66 20 5b 53 51 4c 49 is one of [SQLI
30e50 54 45 5f 49 4e 53 45 52 54 5d 2c 20 5b 53 51 4c TE_INSERT], [SQL
30e60 49 54 45 5f 44 45 4c 45 54 45 5d 2c 0a 2a 2a 20 ITE_DELETE],.**
30e70 6f 72 20 5b 53 51 4c 49 54 45 5f 55 50 44 41 54 or [SQLITE_UPDAT
30e80 45 5d 2c 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e E], depending on
30e90 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 74 the operation t
30ea0 68 61 74 20 63 61 75 73 65 64 20 74 68 65 20 63 hat caused the c
30eb0 61 6c 6c 62 61 63 6b 0a 2a 2a 20 74 6f 20 62 65 allback.** to be
30ec0 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2a 20 54 68 65 invoked..** The
30ed0 20 74 68 69 72 64 20 61 6e 64 20 66 6f 75 72 74 third and fourt
30ee0 68 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 h arguments to t
30ef0 68 65 20 63 61 6c 6c 62 61 63 6b 20 63 6f 6e 74 he callback cont
30f00 61 69 6e 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 ain pointers to
30f10 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 the.** database
30f20 61 6e 64 20 74 61 62 6c 65 20 6e 61 6d 65 20 63 and table name c
30f30 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 61 66 ontaining the af
30f40 66 65 63 74 65 64 20 72 6f 77 2e 0a 2a 2a 20 54 fected row..** T
30f50 68 65 20 66 69 6e 61 6c 20 63 61 6c 6c 62 61 63 he final callbac
30f60 6b 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 74 k parameter is t
30f70 68 65 20 5b 72 6f 77 69 64 5d 20 6f 66 20 74 68 he [rowid] of th
30f80 65 20 72 6f 77 2e 0a 2a 2a 20 49 6e 20 74 68 65 e row..** In the
30f90 20 63 61 73 65 20 6f 66 20 61 6e 20 75 70 64 61 case of an upda
30fa0 74 65 2c 20 74 68 69 73 20 69 73 20 74 68 65 20 te, this is the
30fb0 5b 72 6f 77 69 64 5d 20 61 66 74 65 72 20 74 68 [rowid] after th
30fc0 65 20 75 70 64 61 74 65 20 74 61 6b 65 73 20 70 e update takes p
30fd0 6c 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 lace..**.** The
30fe0 75 70 64 61 74 65 20 68 6f 6f 6b 20 69 73 20 6e update hook is n
30ff0 6f 74 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e 20 ot invoked when
31000 69 6e 74 65 72 6e 61 6c 20 73 79 73 74 65 6d 20 internal system
31010 74 61 62 6c 65 73 20 61 72 65 0a 2a 2a 20 6d 6f tables are.** mo
31020 64 69 66 69 65 64 20 28 69 2e 65 2e 20 73 71 6c dified (i.e. sql
31030 69 74 65 5f 6d 61 73 74 65 72 20 61 6e 64 20 73 ite_master and s
31040 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 29 2e qlite_sequence).
31050 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 63 75 .**.** In the cu
31060 72 72 65 6e 74 20 69 6d 70 6c 65 6d 65 6e 74 61 rrent implementa
31070 74 69 6f 6e 2c 20 74 68 65 20 75 70 64 61 74 65 tion, the update
31080 20 68 6f 6f 6b 0a 2a 2a 20 69 73 20 6e 6f 74 20 hook.** is not
31090 69 6e 76 6f 6b 65 64 20 77 68 65 6e 20 64 75 70 invoked when dup
310a0 6c 69 63 61 74 69 6f 6e 20 72 6f 77 73 20 61 72 lication rows ar
310b0 65 20 64 65 6c 65 74 65 64 20 62 65 63 61 75 73 e deleted becaus
310c0 65 20 6f 66 20 61 6e 0a 2a 2a 20 5b 4f 4e 20 43 e of an.** [ON C
310d0 4f 4e 46 4c 49 43 54 20 7c 20 4f 4e 20 43 4f 4e ONFLICT | ON CON
310e0 46 4c 49 43 54 20 52 45 50 4c 41 43 45 5d 20 63 FLICT REPLACE] c
310f0 6c 61 75 73 65 2e 20 20 4e 6f 72 20 69 73 20 74 lause. Nor is t
31100 68 65 20 75 70 64 61 74 65 20 68 6f 6f 6b 0a 2a he update hook.*
31110 2a 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e 20 72 * invoked when r
31120 6f 77 73 20 61 72 65 20 64 65 6c 65 74 65 64 20 ows are deleted
31130 75 73 69 6e 67 20 74 68 65 20 5b 74 72 75 6e 63 using the [trunc
31140 61 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e ate optimization
31150 5d 2e 0a 2a 2a 20 54 68 65 20 65 78 63 65 70 74 ]..** The except
31160 69 6f 6e 73 20 64 65 66 69 6e 65 64 20 69 6e 20 ions defined in
31170 74 68 69 73 20 70 61 72 61 67 72 61 70 68 20 6d this paragraph m
31180 69 67 68 74 20 63 68 61 6e 67 65 20 69 6e 20 61 ight change in a
31190 20 66 75 74 75 72 65 0a 2a 2a 20 72 65 6c 65 61 future.** relea
311a0 73 65 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2a se of SQLite..**
311b0 0a 2a 2a 20 54 68 65 20 75 70 64 61 74 65 20 68 .** The update h
311c0 6f 6f 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 ook implementati
311d0 6f 6e 20 6d 75 73 74 20 6e 6f 74 20 64 6f 20 61 on must not do a
311e0 6e 79 74 68 69 6e 67 20 74 68 61 74 20 77 69 6c nything that wil
311f0 6c 20 6d 6f 64 69 66 79 0a 2a 2a 20 74 68 65 20 l modify.** the
31200 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
31210 69 6f 6e 20 74 68 61 74 20 69 6e 76 6f 6b 65 64 ion that invoked
31220 20 74 68 65 20 75 70 64 61 74 65 20 68 6f 6f 6b the update hook
31230 2e 20 20 41 6e 79 20 61 63 74 69 6f 6e 73 0a 2a . Any actions.*
31240 2a 20 74 6f 20 6d 6f 64 69 66 79 20 74 68 65 20 * to modify the
31250 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
31260 69 6f 6e 20 6d 75 73 74 20 62 65 20 64 65 66 65 ion must be defe
31270 72 72 65 64 20 75 6e 74 69 6c 20 61 66 74 65 72 rred until after
31280 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 6c 65 74 69 the.** completi
31290 6f 6e 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 on of the [sqlit
312a0 65 33 5f 73 74 65 70 28 29 5d 20 63 61 6c 6c 20 e3_step()] call
312b0 74 68 61 74 20 74 72 69 67 67 65 72 65 64 20 74 that triggered t
312c0 68 65 20 75 70 64 61 74 65 20 68 6f 6f 6b 2e 0a he update hook..
312d0 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 5b 73 71 ** Note that [sq
312e0 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 lite3_prepare_v2
312f0 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 ()] and [sqlite3
31300 5f 73 74 65 70 28 29 5d 20 62 6f 74 68 20 6d 6f _step()] both mo
31310 64 69 66 79 20 74 68 65 69 72 0a 2a 2a 20 64 61 dify their.** da
31320 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
31330 6e 73 20 66 6f 72 20 74 68 65 20 6d 65 61 6e 69 ns for the meani
31340 6e 67 20 6f 66 20 22 6d 6f 64 69 66 79 22 20 69 ng of "modify" i
31350 6e 20 74 68 69 73 20 70 61 72 61 67 72 61 70 68 n this paragraph
31360 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 6f 74 68 ..**.** If anoth
31370 65 72 20 66 75 6e 63 74 69 6f 6e 20 77 61 73 20 er function was
31380 70 72 65 76 69 6f 75 73 6c 79 20 72 65 67 69 73 previously regis
31390 74 65 72 65 64 2c 20 69 74 73 20 70 41 72 67 20 tered, its pArg
313a0 76 61 6c 75 65 0a 2a 2a 20 69 73 20 72 65 74 75 value.** is retu
313b0 72 6e 65 64 2e 20 20 4f 74 68 65 72 77 69 73 65 rned. Otherwise
313c0 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 NULL is returne
313d0 64 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 d..**.** See als
313e0 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 o the [sqlite3_c
313f0 6f 6d 6d 69 74 5f 68 6f 6f 6b 28 29 5d 20 61 6e ommit_hook()] an
31400 64 20 5b 73 71 6c 69 74 65 33 5f 72 6f 6c 6c 62 d [sqlite3_rollb
31410 61 63 6b 5f 68 6f 6f 6b 28 29 5d 0a 2a 2a 20 69 ack_hook()].** i
31420 6e 74 65 72 66 61 63 65 73 2e 0a 2a 2a 0a 2a 2a nterfaces..**.**
31430 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a Requirements:.*
31440 2a 20 5b 48 31 32 39 37 31 5d 20 5b 48 31 32 39 * [H12971] [H129
31450 37 33 5d 20 5b 48 31 32 39 37 35 5d 20 5b 48 31 73] [H12975] [H1
31460 32 39 37 37 5d 20 5b 48 31 32 39 37 39 5d 20 5b 2977] [H12979] [
31470 48 31 32 39 38 31 5d 20 5b 48 31 32 39 38 33 5d H12981] [H12983]
31480 20 5b 48 31 32 39 38 36 5d 0a 2a 2f 0a 53 51 4c [H12986].*/.SQL
31490 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 ITE_API void *sq
314a0 6c 69 74 65 33 5f 75 70 64 61 74 65 5f 68 6f 6f lite3_update_hoo
314b0 6b 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20 0a k(. sqlite3*, .
314c0 20 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 20 2a void(*)(void *
314d0 2c 69 6e 74 20 2c 63 68 61 72 20 63 6f 6e 73 74 ,int ,char const
314e0 20 2a 2c 63 68 61 72 20 63 6f 6e 73 74 20 2a 2c *,char const *,
314f0 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 2c 0a sqlite3_int64),.
31500 20 20 76 6f 69 64 2a 0a 29 3b 0a 0a 2f 2a 0a 2a void*.);../*.*
31510 2a 20 43 41 50 49 33 52 45 46 3a 20 45 6e 61 62 * CAPI3REF: Enab
31520 6c 65 20 4f 72 20 44 69 73 61 62 6c 65 20 53 68 le Or Disable Sh
31530 61 72 65 64 20 50 61 67 65 72 20 43 61 63 68 65 ared Pager Cache
31540 20 7b 48 31 30 33 33 30 7d 20 3c 53 33 30 39 30 {H10330} <S3090
31550 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 0>.** KEYWORDS:
31560 7b 73 68 61 72 65 64 20 63 61 63 68 65 7d 0a 2a {shared cache}.*
31570 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
31580 65 20 65 6e 61 62 6c 65 73 20 6f 72 20 64 69 73 e enables or dis
31590 61 62 6c 65 73 20 74 68 65 20 73 68 61 72 69 6e ables the sharin
315a0 67 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 g of the databas
315b0 65 20 63 61 63 68 65 0a 2a 2a 20 61 6e 64 20 73 e cache.** and s
315c0 63 68 65 6d 61 20 64 61 74 61 20 73 74 72 75 63 chema data struc
315d0 74 75 72 65 73 20 62 65 74 77 65 65 6e 20 5b 64 tures between [d
315e0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
315f0 6f 6e 20 7c 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 on | connections
31600 5d 0a 2a 2a 20 74 6f 20 74 68 65 20 73 61 6d 65 ].** to the same
31610 20 64 61 74 61 62 61 73 65 2e 20 53 68 61 72 69 database. Shari
31620 6e 67 20 69 73 20 65 6e 61 62 6c 65 64 20 69 66 ng is enabled if
31630 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 69 73 the argument is
31640 20 74 72 75 65 0a 2a 2a 20 61 6e 64 20 64 69 73 true.** and dis
31650 61 62 6c 65 64 20 69 66 20 74 68 65 20 61 72 67 abled if the arg
31660 75 6d 65 6e 74 20 69 73 20 66 61 6c 73 65 2e 0a ument is false..
31670 2a 2a 0a 2a 2a 20 43 61 63 68 65 20 73 68 61 72 **.** Cache shar
31680 69 6e 67 20 69 73 20 65 6e 61 62 6c 65 64 20 61 ing is enabled a
31690 6e 64 20 64 69 73 61 62 6c 65 64 20 66 6f 72 20 nd disabled for
316a0 61 6e 20 65 6e 74 69 72 65 20 70 72 6f 63 65 73 an entire proces
316b0 73 2e 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 20 s..** This is a
316c0 63 68 61 6e 67 65 20 61 73 20 6f 66 20 53 51 4c change as of SQL
316d0 69 74 65 20 76 65 72 73 69 6f 6e 20 33 2e 35 2e ite version 3.5.
316e0 30 2e 20 49 6e 20 70 72 69 6f 72 20 76 65 72 73 0. In prior vers
316f0 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 2c 0a ions of SQLite,.
31700 2a 2a 20 73 68 61 72 69 6e 67 20 77 61 73 20 65 ** sharing was e
31710 6e 61 62 6c 65 64 20 6f 72 20 64 69 73 61 62 6c nabled or disabl
31720 65 64 20 66 6f 72 20 65 61 63 68 20 74 68 72 65 ed for each thre
31730 61 64 20 73 65 70 61 72 61 74 65 6c 79 2e 0a 2a ad separately..*
31740 2a 0a 2a 2a 20 54 68 65 20 63 61 63 68 65 20 73 *.** The cache s
31750 68 61 72 69 6e 67 20 6d 6f 64 65 20 73 65 74 20 haring mode set
31760 62 79 20 74 68 69 73 20 69 6e 74 65 72 66 61 63 by this interfac
31770 65 20 65 66 66 65 63 74 73 20 61 6c 6c 20 73 75 e effects all su
31780 62 73 65 71 75 65 6e 74 0a 2a 2a 20 63 61 6c 6c bsequent.** call
31790 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6f 70 s to [sqlite3_op
317a0 65 6e 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f en()], [sqlite3_
317b0 6f 70 65 6e 5f 76 32 28 29 5d 2c 20 61 6e 64 20 open_v2()], and
317c0 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 [sqlite3_open16(
317d0 29 5d 2e 0a 2a 2a 20 45 78 69 73 74 69 6e 67 20 )]..** Existing
317e0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
317f0 69 6f 6e 73 20 63 6f 6e 74 69 6e 75 65 20 75 73 ions continue us
31800 65 20 74 68 65 20 73 68 61 72 69 6e 67 20 6d 6f e the sharing mo
31810 64 65 0a 2a 2a 20 74 68 61 74 20 77 61 73 20 69 de.** that was i
31820 6e 20 65 66 66 65 63 74 20 61 74 20 74 68 65 20 n effect at the
31830 74 69 6d 65 20 74 68 65 79 20 77 65 72 65 20 6f time they were o
31840 70 65 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 56 69 72 pened..**.** Vir
31850 74 75 61 6c 20 74 61 62 6c 65 73 20 63 61 6e 6e tual tables cann
31860 6f 74 20 62 65 20 75 73 65 64 20 77 69 74 68 20 ot be used with
31870 61 20 73 68 61 72 65 64 20 63 61 63 68 65 2e 20 a shared cache.
31880 20 57 68 65 6e 20 73 68 61 72 65 64 0a 2a 2a 20 When shared.**
31890 63 61 63 68 65 20 69 73 20 65 6e 61 62 6c 65 64 cache is enabled
318a0 2c 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 , the [sqlite3_c
318b0 72 65 61 74 65 5f 6d 6f 64 75 6c 65 28 29 5d 20 reate_module()]
318c0 41 50 49 20 75 73 65 64 20 74 6f 20 72 65 67 69 API used to regi
318d0 73 74 65 72 0a 2a 2a 20 76 69 72 74 75 61 6c 20 ster.** virtual
318e0 74 61 62 6c 65 73 20 77 69 6c 6c 20 61 6c 77 61 tables will alwa
318f0 79 73 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72 ys return an err
31900 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 or..**.** This r
31910 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 5b outine returns [
31920 53 51 4c 49 54 45 5f 4f 4b 5d 20 69 66 20 73 68 SQLITE_OK] if sh
31930 61 72 65 64 20 63 61 63 68 65 20 77 61 73 20 65 ared cache was e
31940 6e 61 62 6c 65 64 20 6f 72 20 64 69 73 61 62 6c nabled or disabl
31950 65 64 0a 2a 2a 20 73 75 63 63 65 73 73 66 75 6c ed.** successful
31960 6c 79 2e 20 20 41 6e 20 5b 65 72 72 6f 72 20 63 ly. An [error c
31970 6f 64 65 5d 20 69 73 20 72 65 74 75 72 6e 65 64 ode] is returned
31980 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2a 0a 2a otherwise..**.*
31990 2a 20 53 68 61 72 65 64 20 63 61 63 68 65 20 69 * Shared cache i
319a0 73 20 64 69 73 61 62 6c 65 64 20 62 79 20 64 65 s disabled by de
319b0 66 61 75 6c 74 2e 20 42 75 74 20 74 68 69 73 20 fault. But this
319c0 6d 69 67 68 74 20 63 68 61 6e 67 65 20 69 6e 0a might change in.
319d0 2a 2a 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 ** future releas
319e0 65 73 20 6f 66 20 53 51 4c 69 74 65 2e 20 20 41 es of SQLite. A
319f0 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 68 61 74 pplications that
31a00 20 63 61 72 65 20 61 62 6f 75 74 20 73 68 61 72 care about shar
31a10 65 64 0a 2a 2a 20 63 61 63 68 65 20 73 65 74 74 ed.** cache sett
31a20 69 6e 67 20 73 68 6f 75 6c 64 20 73 65 74 20 69 ing should set i
31a30 74 20 65 78 70 6c 69 63 69 74 6c 79 2e 0a 2a 2a t explicitly..**
31a40 0a 2a 2a 20 53 65 65 20 41 6c 73 6f 3a 20 20 5b .** See Also: [
31a50 53 51 4c 69 74 65 20 53 68 61 72 65 64 2d 43 61 SQLite Shared-Ca
31a60 63 68 65 20 4d 6f 64 65 5d 0a 2a 2a 0a 2a 2a 20 che Mode].**.**
31a70 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 20 5b 48 Requirements: [H
31a80 31 30 33 33 31 5d 20 5b 48 31 30 33 33 36 5d 20 10331] [H10336]
31a90 5b 48 31 30 33 33 37 5d 20 5b 48 31 30 33 33 39 [H10337] [H10339
31aa0 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ].*/.SQLITE_API
31ab0 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 6e 61 62 int sqlite3_enab
31ac0 6c 65 5f 73 68 61 72 65 64 5f 63 61 63 68 65 28 le_shared_cache(
31ad0 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 int);../*.** CAP
31ae0 49 33 52 45 46 3a 20 41 74 74 65 6d 70 74 20 54 I3REF: Attempt T
31af0 6f 20 46 72 65 65 20 48 65 61 70 20 4d 65 6d 6f o Free Heap Memo
31b00 72 79 20 7b 48 31 37 33 34 30 7d 20 3c 53 33 30 ry {H17340} <S30
31b10 32 32 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 220>.**.** The s
31b20 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d qlite3_release_m
31b30 65 6d 6f 72 79 28 29 20 69 6e 74 65 72 66 61 63 emory() interfac
31b40 65 20 61 74 74 65 6d 70 74 73 20 74 6f 20 66 72 e attempts to fr
31b50 65 65 20 4e 20 62 79 74 65 73 0a 2a 2a 20 6f 66 ee N bytes.** of
31b60 20 68 65 61 70 20 6d 65 6d 6f 72 79 20 62 79 20 heap memory by
31b70 64 65 61 6c 6c 6f 63 61 74 69 6e 67 20 6e 6f 6e deallocating non
31b80 2d 65 73 73 65 6e 74 69 61 6c 20 6d 65 6d 6f 72 -essential memor
31b90 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 0a 2a 2a y allocations.**
31ba0 20 68 65 6c 64 20 62 79 20 74 68 65 20 64 61 74 held by the dat
31bb0 61 62 61 73 65 20 6c 69 62 72 61 72 79 2e 20 7b abase library. {
31bc0 45 4e 44 7d 20 20 4d 65 6d 6f 72 79 20 75 73 65 END} Memory use
31bd0 64 20 74 6f 20 63 61 63 68 65 20 64 61 74 61 62 d to cache datab
31be0 61 73 65 0a 2a 2a 20 70 61 67 65 73 20 74 6f 20 ase.** pages to
31bf0 69 6d 70 72 6f 76 65 20 70 65 72 66 6f 72 6d 61 improve performa
31c00 6e 63 65 20 69 73 20 61 6e 20 65 78 61 6d 70 6c nce is an exampl
31c10 65 20 6f 66 20 6e 6f 6e 2d 65 73 73 65 6e 74 69 e of non-essenti
31c20 61 6c 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 20 73 71 al memory..** sq
31c30 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 lite3_release_me
31c40 6d 6f 72 79 28 29 20 72 65 74 75 72 6e 73 20 74 mory() returns t
31c50 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 he number of byt
31c60 65 73 20 61 63 74 75 61 6c 6c 79 20 66 72 65 65 es actually free
31c70 64 2c 0a 2a 2a 20 77 68 69 63 68 20 6d 69 67 68 d,.** which migh
31c80 74 20 62 65 20 6d 6f 72 65 20 6f 72 20 6c 65 73 t be more or les
31c90 73 20 74 68 61 6e 20 74 68 65 20 61 6d 6f 75 6e s than the amoun
31ca0 74 20 72 65 71 75 65 73 74 65 64 2e 0a 2a 2a 0a t requested..**.
31cb0 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a ** Requirements:
31cc0 20 5b 48 31 37 33 34 31 5d 20 5b 48 31 37 33 34 [H17341] [H1734
31cd0 32 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 2].*/.SQLITE_API
31ce0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 72 65 6c int sqlite3_rel
31cf0 65 61 73 65 5f 6d 65 6d 6f 72 79 28 69 6e 74 29 ease_memory(int)
31d00 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
31d10 46 3a 20 49 6d 70 6f 73 65 20 41 20 4c 69 6d 69 F: Impose A Limi
31d20 74 20 4f 6e 20 48 65 61 70 20 53 69 7a 65 20 7b t On Heap Size {
31d30 48 31 37 33 35 30 7d 20 3c 53 33 30 32 32 30 3e H17350} <S30220>
31d40 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 .**.** The sqlit
31d50 65 33 5f 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d e3_soft_heap_lim
31d60 69 74 28 29 20 69 6e 74 65 72 66 61 63 65 20 70 it() interface p
31d70 6c 61 63 65 73 20 61 20 22 73 6f 66 74 22 20 6c laces a "soft" l
31d80 69 6d 69 74 0a 2a 2a 20 6f 6e 20 74 68 65 20 61 imit.** on the a
31d90 6d 6f 75 6e 74 20 6f 66 20 68 65 61 70 20 6d 65 mount of heap me
31da0 6d 6f 72 79 20 74 68 61 74 20 6d 61 79 20 62 65 mory that may be
31db0 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 53 51 allocated by SQ
31dc0 4c 69 74 65 2e 0a 2a 2a 20 49 66 20 61 6e 20 69 Lite..** If an i
31dd0 6e 74 65 72 6e 61 6c 20 61 6c 6c 6f 63 61 74 69 nternal allocati
31de0 6f 6e 20 69 73 20 72 65 71 75 65 73 74 65 64 20 on is requested
31df0 74 68 61 74 20 77 6f 75 6c 64 20 65 78 63 65 65 that would excee
31e00 64 20 74 68 65 0a 2a 2a 20 73 6f 66 74 20 68 65 d the.** soft he
31e10 61 70 20 6c 69 6d 69 74 2c 20 5b 73 71 6c 69 74 ap limit, [sqlit
31e20 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 e3_release_memor
31e30 79 28 29 5d 20 69 73 20 69 6e 76 6f 6b 65 64 20 y()] is invoked
31e40 6f 6e 65 20 6f 72 0a 2a 2a 20 6d 6f 72 65 20 74 one or.** more t
31e50 69 6d 65 73 20 74 6f 20 66 72 65 65 20 75 70 20 imes to free up
31e60 73 6f 6d 65 20 73 70 61 63 65 20 62 65 66 6f 72 some space befor
31e70 65 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e e the allocation
31e80 20 69 73 20 70 65 72 66 6f 72 6d 65 64 2e 0a 2a is performed..*
31e90 2a 0a 2a 2a 20 54 68 65 20 6c 69 6d 69 74 20 69 *.** The limit i
31ea0 73 20 63 61 6c 6c 65 64 20 22 73 6f 66 74 22 2c s called "soft",
31eb0 20 62 65 63 61 75 73 65 20 69 66 20 5b 73 71 6c because if [sql
31ec0 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d ite3_release_mem
31ed0 6f 72 79 28 29 5d 0a 2a 2a 20 63 61 6e 6e 6f 74 ory()].** cannot
31ee0 20 66 72 65 65 20 73 75 66 66 69 63 69 65 6e 74 free sufficient
31ef0 20 6d 65 6d 6f 72 79 20 74 6f 20 70 72 65 76 65 memory to preve
31f00 6e 74 20 74 68 65 20 6c 69 6d 69 74 20 66 72 6f nt the limit fro
31f10 6d 20 62 65 69 6e 67 20 65 78 63 65 65 64 65 64 m being exceeded
31f20 2c 0a 2a 2a 20 74 68 65 20 6d 65 6d 6f 72 79 20 ,.** the memory
31f30 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 79 is allocated any
31f40 77 61 79 20 61 6e 64 20 74 68 65 20 63 75 72 72 way and the curr
31f50 65 6e 74 20 6f 70 65 72 61 74 69 6f 6e 20 70 72 ent operation pr
31f60 6f 63 65 65 64 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 oceeds..**.** A
31f70 6e 65 67 61 74 69 76 65 20 6f 72 20 7a 65 72 6f negative or zero
31f80 20 76 61 6c 75 65 20 66 6f 72 20 4e 20 6d 65 61 value for N mea
31f90 6e 73 20 74 68 61 74 20 74 68 65 72 65 20 69 73 ns that there is
31fa0 20 6e 6f 20 73 6f 66 74 20 68 65 61 70 20 6c 69 no soft heap li
31fb0 6d 69 74 20 61 6e 64 0a 2a 2a 20 5b 73 71 6c 69 mit and.** [sqli
31fc0 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f te3_release_memo
31fd0 72 79 28 29 5d 20 77 69 6c 6c 20 6f 6e 6c 79 20 ry()] will only
31fe0 62 65 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 6d be called when m
31ff0 65 6d 6f 72 79 20 69 73 20 65 78 68 61 75 73 74 emory is exhaust
32000 65 64 2e 0a 2a 2a 20 54 68 65 20 64 65 66 61 75 ed..** The defau
32010 6c 74 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 lt value for the
32020 20 73 6f 66 74 20 68 65 61 70 20 6c 69 6d 69 74 soft heap limit
32030 20 69 73 20 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 is zero..**.**
32040 53 51 4c 69 74 65 20 6d 61 6b 65 73 20 61 20 62 SQLite makes a b
32050 65 73 74 20 65 66 66 6f 72 74 20 74 6f 20 68 6f est effort to ho
32060 6e 6f 72 20 74 68 65 20 73 6f 66 74 20 68 65 61 nor the soft hea
32070 70 20 6c 69 6d 69 74 2e 0a 2a 2a 20 42 75 74 20 p limit..** But
32080 69 66 20 74 68 65 20 73 6f 66 74 20 68 65 61 70 if the soft heap
32090 20 6c 69 6d 69 74 20 63 61 6e 6e 6f 74 20 62 65 limit cannot be
320a0 20 68 6f 6e 6f 72 65 64 2c 20 65 78 65 63 75 74 honored, execut
320b0 69 6f 6e 20 77 69 6c 6c 0a 2a 2a 20 63 6f 6e 74 ion will.** cont
320c0 69 6e 75 65 20 77 69 74 68 6f 75 74 20 65 72 72 inue without err
320d0 6f 72 20 6f 72 20 6e 6f 74 69 66 69 63 61 74 69 or or notificati
320e0 6f 6e 2e 20 20 54 68 69 73 20 69 73 20 77 68 79 on. This is why
320f0 20 74 68 65 20 6c 69 6d 69 74 20 69 73 0a 2a 2a the limit is.**
32100 20 63 61 6c 6c 65 64 20 61 20 22 73 6f 66 74 22 called a "soft"
32110 20 6c 69 6d 69 74 2e 20 20 49 74 20 69 73 20 61 limit. It is a
32120 64 76 69 73 6f 72 79 20 6f 6e 6c 79 2e 0a 2a 2a dvisory only..**
32130 0a 2a 2a 20 50 72 69 6f 72 20 74 6f 20 53 51 4c .** Prior to SQL
32140 69 74 65 20 76 65 72 73 69 6f 6e 20 33 2e 35 2e ite version 3.5.
32150 30 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 0, this routine
32160 6f 6e 6c 79 20 63 6f 6e 73 74 72 61 69 6e 65 64 only constrained
32170 20 74 68 65 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 the memory.** a
32180 6c 6c 6f 63 61 74 65 64 20 62 79 20 61 20 73 69 llocated by a si
32190 6e 67 6c 65 20 74 68 72 65 61 64 20 2d 20 74 68 ngle thread - th
321a0 65 20 73 61 6d 65 20 74 68 72 65 61 64 20 69 6e e same thread in
321b0 20 77 68 69 63 68 20 74 68 69 73 20 72 6f 75 74 which this rout
321c0 69 6e 65 0a 2a 2a 20 72 75 6e 73 2e 20 20 42 65 ine.** runs. Be
321d0 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 53 51 4c ginning with SQL
321e0 69 74 65 20 76 65 72 73 69 6f 6e 20 33 2e 35 2e ite version 3.5.
321f0 30 2c 20 74 68 65 20 73 6f 66 74 20 68 65 61 70 0, the soft heap
32200 20 6c 69 6d 69 74 20 69 73 0a 2a 2a 20 61 70 70 limit is.** app
32210 6c 69 65 64 20 74 6f 20 61 6c 6c 20 74 68 72 65 lied to all thre
32220 61 64 73 2e 20 54 68 65 20 76 61 6c 75 65 20 73 ads. The value s
32230 70 65 63 69 66 69 65 64 20 66 6f 72 20 74 68 65 pecified for the
32240 20 73 6f 66 74 20 68 65 61 70 20 6c 69 6d 69 74 soft heap limit
32250 0a 2a 2a 20 69 73 20 61 6e 20 75 70 70 65 72 20 .** is an upper
32260 62 6f 75 6e 64 20 6f 6e 20 74 68 65 20 74 6f 74 bound on the tot
32270 61 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 al memory alloca
32280 74 69 6f 6e 20 66 6f 72 20 61 6c 6c 20 74 68 72 tion for all thr
32290 65 61 64 73 2e 20 49 6e 0a 2a 2a 20 76 65 72 73 eads. In.** vers
322a0 69 6f 6e 20 33 2e 35 2e 30 20 74 68 65 72 65 20 ion 3.5.0 there
322b0 69 73 20 6e 6f 20 6d 65 63 68 61 6e 69 73 6d 20 is no mechanism
322c0 66 6f 72 20 6c 69 6d 69 74 69 6e 67 20 74 68 65 for limiting the
322d0 20 68 65 61 70 20 75 73 61 67 65 20 66 6f 72 0a heap usage for.
322e0 2a 2a 20 69 6e 64 69 76 69 64 75 61 6c 20 74 68 ** individual th
322f0 72 65 61 64 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 reads..**.** Req
32300 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 uirements:.** [H
32310 31 36 33 35 31 5d 20 5b 48 31 36 33 35 32 5d 20 16351] [H16352]
32320 5b 48 31 36 33 35 33 5d 20 5b 48 31 36 33 35 34 [H16353] [H16354
32330 5d 20 5b 48 31 36 33 35 35 5d 20 5b 48 31 36 33 ] [H16355] [H163
32340 35 38 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 58].*/.SQLITE_AP
32350 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 73 I void sqlite3_s
32360 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74 28 69 oft_heap_limit(i
32370 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 nt);../*.** CAPI
32380 33 52 45 46 3a 20 45 78 74 72 61 63 74 20 4d 65 3REF: Extract Me
32390 74 61 64 61 74 61 20 41 62 6f 75 74 20 41 20 43 tadata About A C
323a0 6f 6c 75 6d 6e 20 4f 66 20 41 20 54 61 62 6c 65 olumn Of A Table
323b0 20 7b 48 31 32 38 35 30 7d 20 3c 53 36 30 33 30 {H12850} <S6030
323c0 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 0>.**.** This ro
323d0 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 6d 65 utine returns me
323e0 74 61 64 61 74 61 20 61 62 6f 75 74 20 61 20 73 tadata about a s
323f0 70 65 63 69 66 69 63 20 63 6f 6c 75 6d 6e 20 6f pecific column o
32400 66 20 61 20 73 70 65 63 69 66 69 63 0a 2a 2a 20 f a specific.**
32410 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 20 61 database table a
32420 63 63 65 73 73 69 62 6c 65 20 75 73 69 6e 67 20 ccessible using
32430 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f the [database co
32440 6e 6e 65 63 74 69 6f 6e 5d 20 68 61 6e 64 6c 65 nnection] handle
32450 0a 2a 2a 20 70 61 73 73 65 64 20 61 73 20 74 68 .** passed as th
32460 65 20 66 69 72 73 74 20 66 75 6e 63 74 69 6f 6e e first function
32470 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a argument..**.**
32480 20 54 68 65 20 63 6f 6c 75 6d 6e 20 69 73 20 69 The column is i
32490 64 65 6e 74 69 66 69 65 64 20 62 79 20 74 68 65 dentified by the
324a0 20 73 65 63 6f 6e 64 2c 20 74 68 69 72 64 20 61 second, third a
324b0 6e 64 20 66 6f 75 72 74 68 20 70 61 72 61 6d 65 nd fourth parame
324c0 74 65 72 73 20 74 6f 0a 2a 2a 20 74 68 69 73 20 ters to.** this
324d0 66 75 6e 63 74 69 6f 6e 2e 20 54 68 65 20 73 65 function. The se
324e0 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 69 cond parameter i
324f0 73 20 65 69 74 68 65 72 20 74 68 65 20 6e 61 6d s either the nam
32500 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 e of the databas
32510 65 0a 2a 2a 20 28 69 2e 65 2e 20 22 6d 61 69 6e e.** (i.e. "main
32520 22 2c 20 22 74 65 6d 70 22 20 6f 72 20 61 6e 20 ", "temp" or an
32530 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73 attached databas
32540 65 29 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 e) containing th
32550 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 74 e specified.** t
32560 61 62 6c 65 20 6f 72 20 4e 55 4c 4c 2e 20 49 66 able or NULL. If
32570 20 69 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 it is NULL, the
32580 6e 20 61 6c 6c 20 61 74 74 61 63 68 65 64 20 64 n all attached d
32590 61 74 61 62 61 73 65 73 20 61 72 65 20 73 65 61 atabases are sea
325a0 72 63 68 65 64 0a 2a 2a 20 66 6f 72 20 74 68 65 rched.** for the
325b0 20 74 61 62 6c 65 20 75 73 69 6e 67 20 74 68 65 table using the
325c0 20 73 61 6d 65 20 61 6c 67 6f 72 69 74 68 6d 20 same algorithm
325d0 75 73 65 64 20 62 79 20 74 68 65 20 64 61 74 61 used by the data
325e0 62 61 73 65 20 65 6e 67 69 6e 65 20 74 6f 0a 2a base engine to.*
325f0 2a 20 72 65 73 6f 6c 76 65 20 75 6e 71 75 61 6c * resolve unqual
32600 69 66 69 65 64 20 74 61 62 6c 65 20 72 65 66 65 ified table refe
32610 72 65 6e 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 rences..**.** Th
32620 65 20 74 68 69 72 64 20 61 6e 64 20 66 6f 75 72 e third and four
32630 74 68 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f th parameters to
32640 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 this function a
32650 72 65 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64 re the table and
32660 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 6e 61 6d 65 20 column.** name
32670 6f 66 20 74 68 65 20 64 65 73 69 72 65 64 20 63 of the desired c
32680 6f 6c 75 6d 6e 2c 20 72 65 73 70 65 63 74 69 76 olumn, respectiv
32690 65 6c 79 2e 20 4e 65 69 74 68 65 72 20 6f 66 20 ely. Neither of
326a0 74 68 65 73 65 20 70 61 72 61 6d 65 74 65 72 73 these parameters
326b0 0a 2a 2a 20 6d 61 79 20 62 65 20 4e 55 4c 4c 2e .** may be NULL.
326c0 0a 2a 2a 0a 2a 2a 20 4d 65 74 61 64 61 74 61 20 .**.** Metadata
326d0 69 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 77 is returned by w
326e0 72 69 74 69 6e 67 20 74 6f 20 74 68 65 20 6d 65 riting to the me
326f0 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 73 20 70 mory locations p
32700 61 73 73 65 64 20 61 73 20 74 68 65 20 35 74 68 assed as the 5th
32710 0a 2a 2a 20 61 6e 64 20 73 75 62 73 65 71 75 65 .** and subseque
32720 6e 74 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f nt parameters to
32730 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20 this function.
32740 41 6e 79 20 6f 66 20 74 68 65 73 65 20 61 72 67 Any of these arg
32750 75 6d 65 6e 74 73 20 6d 61 79 20 62 65 0a 2a 2a uments may be.**
32760 20 4e 55 4c 4c 2c 20 69 6e 20 77 68 69 63 68 20 NULL, in which
32770 63 61 73 65 20 74 68 65 20 63 6f 72 72 65 73 70 case the corresp
32780 6f 6e 64 69 6e 67 20 65 6c 65 6d 65 6e 74 20 6f onding element o
32790 66 20 6d 65 74 61 64 61 74 61 20 69 73 20 6f 6d f metadata is om
327a0 69 74 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 3c 62 6c itted..**.** <bl
327b0 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 20 3c 74 61 ockquote>.** <ta
327c0 62 6c 65 20 62 6f 72 64 65 72 3d 22 31 22 3e 0a ble border="1">.
327d0 2a 2a 20 3c 74 72 3e 3c 74 68 3e 20 50 61 72 61 ** <tr><th> Para
327e0 6d 65 74 65 72 20 3c 74 68 3e 20 4f 75 74 70 75 meter <th> Outpu
327f0 74 3c 62 72 3e 54 79 70 65 20 3c 74 68 3e 20 20 t<br>Type <th>
32800 44 65 73 63 72 69 70 74 69 6f 6e 0a 2a 2a 0a 2a Description.**.*
32810 2a 20 3c 74 72 3e 3c 74 64 3e 20 35 74 68 20 3c * <tr><td> 5th <
32820 74 64 3e 20 63 6f 6e 73 74 20 63 68 61 72 2a 20 td> const char*
32830 3c 74 64 3e 20 44 61 74 61 20 74 79 70 65 0a 2a <td> Data type.*
32840 2a 20 3c 74 72 3e 3c 74 64 3e 20 36 74 68 20 3c * <tr><td> 6th <
32850 74 64 3e 20 63 6f 6e 73 74 20 63 68 61 72 2a 20 td> const char*
32860 3c 74 64 3e 20 4e 61 6d 65 20 6f 66 20 64 65 66 <td> Name of def
32870 61 75 6c 74 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 ault collation s
32880 65 71 75 65 6e 63 65 0a 2a 2a 20 3c 74 72 3e 3c equence.** <tr><
32890 74 64 3e 20 37 74 68 20 3c 74 64 3e 20 69 6e 74 td> 7th <td> int
328a0 20 20 20 20 20 20 20 20 20 3c 74 64 3e 20 54 72 <td> Tr
328b0 75 65 20 69 66 20 63 6f 6c 75 6d 6e 20 68 61 73 ue if column has
328c0 20 61 20 4e 4f 54 20 4e 55 4c 4c 20 63 6f 6e 73 a NOT NULL cons
328d0 74 72 61 69 6e 74 0a 2a 2a 20 3c 74 72 3e 3c 74 traint.** <tr><t
328e0 64 3e 20 38 74 68 20 3c 74 64 3e 20 69 6e 74 20 d> 8th <td> int
328f0 20 20 20 20 20 20 20 20 3c 74 64 3e 20 54 72 75 <td> Tru
32900 65 20 69 66 20 63 6f 6c 75 6d 6e 20 69 73 20 70 e if column is p
32910 61 72 74 20 6f 66 20 74 68 65 20 50 52 49 4d 41 art of the PRIMA
32920 52 59 20 4b 45 59 0a 2a 2a 20 3c 74 72 3e 3c 74 RY KEY.** <tr><t
32930 64 3e 20 39 74 68 20 3c 74 64 3e 20 69 6e 74 20 d> 9th <td> int
32940 20 20 20 20 20 20 20 20 3c 74 64 3e 20 54 72 75 <td> Tru
32950 65 20 69 66 20 63 6f 6c 75 6d 6e 20 69 73 20 5b e if column is [
32960 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 5d 0a 2a AUTOINCREMENT].*
32970 2a 20 3c 2f 74 61 62 6c 65 3e 0a 2a 2a 20 3c 2f * </table>.** </
32980 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a blockquote>.**.*
32990 2a 20 54 68 65 20 6d 65 6d 6f 72 79 20 70 6f 69 * The memory poi
329a0 6e 74 65 64 20 74 6f 20 62 79 20 74 68 65 20 63 nted to by the c
329b0 68 61 72 61 63 74 65 72 20 70 6f 69 6e 74 65 72 haracter pointer
329c0 73 20 72 65 74 75 72 6e 65 64 20 66 6f 72 20 74 s returned for t
329d0 68 65 0a 2a 2a 20 64 65 63 6c 61 72 61 74 69 6f he.** declaratio
329e0 6e 20 74 79 70 65 20 61 6e 64 20 63 6f 6c 6c 61 n type and colla
329f0 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 69 73 tion sequence is
32a00 20 76 61 6c 69 64 20 6f 6e 6c 79 20 75 6e 74 69 valid only unti
32a10 6c 20 74 68 65 20 6e 65 78 74 0a 2a 2a 20 63 61 l the next.** ca
32a20 6c 6c 20 74 6f 20 61 6e 79 20 53 51 4c 69 74 65 ll to any SQLite
32a30 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a API function..*
32a40 2a 0a 2a 2a 20 49 66 20 74 68 65 20 73 70 65 63 *.** If the spec
32a50 69 66 69 65 64 20 74 61 62 6c 65 20 69 73 20 61 ified table is a
32a60 63 74 75 61 6c 6c 79 20 61 20 76 69 65 77 2c 20 ctually a view,
32a70 61 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 an [error code]
32a80 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a is returned..**.
32a90 2a 2a 20 49 66 20 74 68 65 20 73 70 65 63 69 66 ** If the specif
32aa0 69 65 64 20 63 6f 6c 75 6d 6e 20 69 73 20 22 72 ied column is "r
32ab0 6f 77 69 64 22 2c 20 22 6f 69 64 22 20 6f 72 20 owid", "oid" or
32ac0 22 5f 72 6f 77 69 64 5f 22 20 61 6e 64 20 61 6e "_rowid_" and an
32ad0 0a 2a 2a 20 5b 49 4e 54 45 47 45 52 20 50 52 49 .** [INTEGER PRI
32ae0 4d 41 52 59 20 4b 45 59 5d 20 63 6f 6c 75 6d 6e MARY KEY] column
32af0 20 68 61 73 20 62 65 65 6e 20 65 78 70 6c 69 63 has been explic
32b00 69 74 6c 79 20 64 65 63 6c 61 72 65 64 2c 20 74 itly declared, t
32b10 68 65 6e 20 74 68 65 20 6f 75 74 70 75 74 0a 2a hen the output.*
32b20 2a 20 70 61 72 61 6d 65 74 65 72 73 20 61 72 65 * parameters are
32b30 20 73 65 74 20 66 6f 72 20 74 68 65 20 65 78 70 set for the exp
32b40 6c 69 63 69 74 6c 79 20 64 65 63 6c 61 72 65 64 licitly declared
32b50 20 63 6f 6c 75 6d 6e 2e 20 49 66 20 74 68 65 72 column. If ther
32b60 65 20 69 73 20 6e 6f 0a 2a 2a 20 65 78 70 6c 69 e is no.** expli
32b70 63 69 74 6c 79 20 64 65 63 6c 61 72 65 64 20 5b citly declared [
32b80 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 INTEGER PRIMARY
32b90 4b 45 59 5d 20 63 6f 6c 75 6d 6e 2c 20 74 68 65 KEY] column, the
32ba0 6e 20 74 68 65 20 6f 75 74 70 75 74 0a 2a 2a 20 n the output.**
32bb0 70 61 72 61 6d 65 74 65 72 73 20 61 72 65 20 73 parameters are s
32bc0 65 74 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a et as follows:.*
32bd0 2a 0a 2a 2a 20 3c 70 72 65 3e 0a 2a 2a 20 20 20 *.** <pre>.**
32be0 20 20 64 61 74 61 20 74 79 70 65 3a 20 22 49 4e data type: "IN
32bf0 54 45 47 45 52 22 0a 2a 2a 20 20 20 20 20 63 6f TEGER".** co
32c00 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 llation sequence
32c10 3a 20 22 42 49 4e 41 52 59 22 0a 2a 2a 20 20 20 : "BINARY".**
32c20 20 20 6e 6f 74 20 6e 75 6c 6c 3a 20 30 0a 2a 2a not null: 0.**
32c30 20 20 20 20 20 70 72 69 6d 61 72 79 20 6b 65 79 primary key
32c40 3a 20 31 0a 2a 2a 20 20 20 20 20 61 75 74 6f 20 : 1.** auto
32c50 69 6e 63 72 65 6d 65 6e 74 3a 20 30 0a 2a 2a 20 increment: 0.**
32c60 3c 2f 70 72 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 </pre>.**.** Thi
32c70 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 6c s function may l
32c80 6f 61 64 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 oad one or more
32c90 73 63 68 65 6d 61 73 20 66 72 6f 6d 20 64 61 74 schemas from dat
32ca0 61 62 61 73 65 20 66 69 6c 65 73 2e 20 49 66 20 abase files. If
32cb0 61 6e 0a 2a 2a 20 65 72 72 6f 72 20 6f 63 63 75 an.** error occu
32cc0 72 73 20 64 75 72 69 6e 67 20 74 68 69 73 20 70 rs during this p
32cd0 72 6f 63 65 73 73 2c 20 6f 72 20 69 66 20 74 68 rocess, or if th
32ce0 65 20 72 65 71 75 65 73 74 65 64 20 74 61 62 6c e requested tabl
32cf0 65 20 6f 72 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 63 e or column.** c
32d00 61 6e 6e 6f 74 20 62 65 20 66 6f 75 6e 64 2c 20 annot be found,
32d10 61 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 an [error code]
32d20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 is returned and
32d30 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 an error message
32d40 20 6c 65 66 74 0a 2a 2a 20 69 6e 20 74 68 65 20 left.** in the
32d50 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 [database connec
32d60 74 69 6f 6e 5d 20 28 74 6f 20 62 65 20 72 65 74 tion] (to be ret
32d70 72 69 65 76 65 64 20 75 73 69 6e 67 20 73 71 6c rieved using sql
32d80 69 74 65 33 5f 65 72 72 6d 73 67 28 29 29 2e 0a ite3_errmsg())..
32d90 2a 2a 0a 2a 2a 20 54 68 69 73 20 41 50 49 20 69 **.** This API i
32da0 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 s only available
32db0 20 69 66 20 74 68 65 20 6c 69 62 72 61 72 79 20 if the library
32dc0 77 61 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 was compiled wit
32dd0 68 20 74 68 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 h the.** [SQLITE
32de0 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d _ENABLE_COLUMN_M
32df0 45 54 41 44 41 54 41 5d 20 43 2d 70 72 65 70 72 ETADATA] C-prepr
32e00 6f 63 65 73 73 6f 72 20 73 79 6d 62 6f 6c 20 64 ocessor symbol d
32e10 65 66 69 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 efined..*/.SQLIT
32e20 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
32e30 33 5f 74 61 62 6c 65 5f 63 6f 6c 75 6d 6e 5f 6d 3_table_column_m
32e40 65 74 61 64 61 74 61 28 0a 20 20 73 71 6c 69 74 etadata(. sqlit
32e50 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 e3 *db,
32e60 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 6e 65 63 /* Connec
32e70 74 69 6f 6e 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 tion handle */.
32e80 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 const char *zDb
32e90 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 Name, /*
32ea0 44 61 74 61 62 61 73 65 20 6e 61 6d 65 20 6f 72 Database name or
32eb0 20 4e 55 4c 4c 20 2a 2f 0a 20 20 63 6f 6e 73 74 NULL */. const
32ec0 20 63 68 61 72 20 2a 7a 54 61 62 6c 65 4e 61 6d char *zTableNam
32ed0 65 2c 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 e, /* Table
32ee0 6e 61 6d 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 name */. const
32ef0 63 68 61 72 20 2a 7a 43 6f 6c 75 6d 6e 4e 61 6d char *zColumnNam
32f00 65 2c 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 e, /* Column
32f10 6e 61 6d 65 20 2a 2f 0a 20 20 63 68 61 72 20 63 name */. char c
32f20 6f 6e 73 74 20 2a 2a 70 7a 44 61 74 61 54 79 70 onst **pzDataTyp
32f30 65 2c 20 20 20 20 2f 2a 20 4f 55 54 50 55 54 3a e, /* OUTPUT:
32f40 20 44 65 63 6c 61 72 65 64 20 64 61 74 61 20 74 Declared data t
32f50 79 70 65 20 2a 2f 0a 20 20 63 68 61 72 20 63 6f ype */. char co
32f60 6e 73 74 20 2a 2a 70 7a 43 6f 6c 6c 53 65 71 2c nst **pzCollSeq,
32f70 20 20 20 20 20 2f 2a 20 4f 55 54 50 55 54 3a 20 /* OUTPUT:
32f80 43 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e Collation sequen
32f90 63 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 69 6e 74 ce name */. int
32fa0 20 2a 70 4e 6f 74 4e 75 6c 6c 2c 20 20 20 20 20 *pNotNull,
32fb0 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 50 /* OUTP
32fc0 55 54 3a 20 54 72 75 65 20 69 66 20 4e 4f 54 20 UT: True if NOT
32fd0 4e 55 4c 4c 20 63 6f 6e 73 74 72 61 69 6e 74 20 NULL constraint
32fe0 65 78 69 73 74 73 20 2a 2f 0a 20 20 69 6e 74 20 exists */. int
32ff0 2a 70 50 72 69 6d 61 72 79 4b 65 79 2c 20 20 20 *pPrimaryKey,
33000 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 50 55 /* OUTPU
33010 54 3a 20 54 72 75 65 20 69 66 20 63 6f 6c 75 6d T: True if colum
33020 6e 20 70 61 72 74 20 6f 66 20 50 4b 20 2a 2f 0a n part of PK */.
33030 20 20 69 6e 74 20 2a 70 41 75 74 6f 69 6e 63 20 int *pAutoinc
33040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
33050 20 4f 55 54 50 55 54 3a 20 54 72 75 65 20 69 66 OUTPUT: True if
33060 20 63 6f 6c 75 6d 6e 20 69 73 20 61 75 74 6f 2d column is auto-
33070 69 6e 63 72 65 6d 65 6e 74 20 2a 2f 0a 29 3b 0a increment */.);.
33080 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
33090 20 4c 6f 61 64 20 41 6e 20 45 78 74 65 6e 73 69 Load An Extensi
330a0 6f 6e 20 7b 48 31 32 36 30 30 7d 20 3c 53 32 30 on {H12600} <S20
330b0 35 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 500>.**.** This
330c0 69 6e 74 65 72 66 61 63 65 20 6c 6f 61 64 73 20 interface loads
330d0 61 6e 20 53 51 4c 69 74 65 20 65 78 74 65 6e 73 an SQLite extens
330e0 69 6f 6e 20 6c 69 62 72 61 72 79 20 66 72 6f 6d ion library from
330f0 20 74 68 65 20 6e 61 6d 65 64 20 66 69 6c 65 2e the named file.
33100 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 30 31 7d 20 .**.** {H12601}
33110 54 68 65 20 73 71 6c 69 74 65 33 5f 6c 6f 61 64 The sqlite3_load
33120 5f 65 78 74 65 6e 73 69 6f 6e 28 29 20 69 6e 74 _extension() int
33130 65 72 66 61 63 65 20 61 74 74 65 6d 70 74 73 20 erface attempts
33140 74 6f 20 6c 6f 61 64 20 61 6e 0a 2a 2a 20 20 20 to load an.**
33150 20 20 20 20 20 20 20 53 51 4c 69 74 65 20 65 78 SQLite ex
33160 74 65 6e 73 69 6f 6e 20 6c 69 62 72 61 72 79 20 tension library
33170 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 74 68 65 contained in the
33180 20 66 69 6c 65 20 7a 46 69 6c 65 2e 0a 2a 2a 0a file zFile..**.
33190 2a 2a 20 7b 48 31 32 36 30 32 7d 20 54 68 65 20 ** {H12602} The
331a0 65 6e 74 72 79 20 70 6f 69 6e 74 20 69 73 20 7a entry point is z
331b0 50 72 6f 63 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 Proc..**.** {H12
331c0 36 30 33 7d 20 7a 50 72 6f 63 20 6d 61 79 20 62 603} zProc may b
331d0 65 20 30 2c 20 69 6e 20 77 68 69 63 68 20 63 61 e 0, in which ca
331e0 73 65 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 se the name of t
331f0 68 65 20 65 6e 74 72 79 20 70 6f 69 6e 74 0a 2a he entry point.*
33200 2a 20 20 20 20 20 20 20 20 20 20 64 65 66 61 75 * defau
33210 6c 74 73 20 74 6f 20 22 73 71 6c 69 74 65 33 5f lts to "sqlite3_
33220 65 78 74 65 6e 73 69 6f 6e 5f 69 6e 69 74 22 2e extension_init".
33230 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 30 34 7d 20 .**.** {H12604}
33240 54 68 65 20 73 71 6c 69 74 65 33 5f 6c 6f 61 64 The sqlite3_load
33250 5f 65 78 74 65 6e 73 69 6f 6e 28 29 20 69 6e 74 _extension() int
33260 65 72 66 61 63 65 20 73 68 61 6c 6c 20 72 65 74 erface shall ret
33270 75 72 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 urn.**
33280 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 6f 6e 20 73 [SQLITE_OK] on s
33290 75 63 63 65 73 73 20 61 6e 64 20 5b 53 51 4c 49 uccess and [SQLI
332a0 54 45 5f 45 52 52 4f 52 5d 20 69 66 20 73 6f 6d TE_ERROR] if som
332b0 65 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e ething goes wron
332c0 67 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 30 35 g..**.** {H12605
332d0 7d 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 } If an error oc
332e0 63 75 72 73 20 61 6e 64 20 70 7a 45 72 72 4d 73 curs and pzErrMs
332f0 67 20 69 73 20 6e 6f 74 20 30 2c 20 74 68 65 6e g is not 0, then
33300 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 the.**
33310 20 5b 73 71 6c 69 74 65 33 5f 6c 6f 61 64 5f 65 [sqlite3_load_e
33320 78 74 65 6e 73 69 6f 6e 28 29 5d 20 69 6e 74 65 xtension()] inte
33330 72 66 61 63 65 20 73 68 61 6c 6c 20 61 74 74 65 rface shall atte
33340 6d 70 74 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20 mpt to.**
33350 20 20 20 66 69 6c 6c 20 2a 70 7a 45 72 72 4d 73 fill *pzErrMs
33360 67 20 77 69 74 68 20 65 72 72 6f 72 20 6d 65 73 g with error mes
33370 73 61 67 65 20 74 65 78 74 20 73 74 6f 72 65 64 sage text stored
33380 20 69 6e 20 6d 65 6d 6f 72 79 0a 2a 2a 20 20 20 in memory.**
33390 20 20 20 20 20 20 20 6f 62 74 61 69 6e 65 64 20 obtained
333a0 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 61 from [sqlite3_ma
333b0 6c 6c 6f 63 28 29 5d 2e 20 7b 45 4e 44 7d 20 20 lloc()]. {END}
333c0 54 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 The calling func
333d0 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 tion.**
333e0 20 73 68 6f 75 6c 64 20 66 72 65 65 20 74 68 69 should free thi
333f0 73 20 6d 65 6d 6f 72 79 20 62 79 20 63 61 6c 6c s memory by call
33400 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 ing [sqlite3_fre
33410 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 e()]..**.** {H12
33420 36 30 36 7d 20 45 78 74 65 6e 73 69 6f 6e 20 6c 606} Extension l
33430 6f 61 64 69 6e 67 20 6d 75 73 74 20 62 65 20 65 oading must be e
33440 6e 61 62 6c 65 64 20 75 73 69 6e 67 0a 2a 2a 20 nabled using.**
33450 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 [sqlite
33460 33 5f 65 6e 61 62 6c 65 5f 6c 6f 61 64 5f 65 78 3_enable_load_ex
33470 74 65 6e 73 69 6f 6e 28 29 5d 20 70 72 69 6f 72 tension()] prior
33480 20 74 6f 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 to calling this
33490 20 41 50 49 2c 0a 2a 2a 20 20 20 20 20 20 20 20 API,.**
334a0 20 20 6f 74 68 65 72 77 69 73 65 20 61 6e 20 65 otherwise an e
334b0 72 72 6f 72 20 77 69 6c 6c 20 62 65 20 72 65 74 rror will be ret
334c0 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 urned..*/.SQLITE
334d0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
334e0 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 _load_extension(
334f0 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 . sqlite3 *db,
33500 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 61 64 /* Load
33510 20 74 68 65 20 65 78 74 65 6e 73 69 6f 6e 20 69 the extension i
33520 6e 74 6f 20 74 68 69 73 20 64 61 74 61 62 61 73 nto this databas
33530 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a e connection */.
33540 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 const char *zF
33550 69 6c 65 2c 20 20 20 20 2f 2a 20 4e 61 6d 65 20 ile, /* Name
33560 6f 66 20 74 68 65 20 73 68 61 72 65 64 20 6c 69 of the shared li
33570 62 72 61 72 79 20 63 6f 6e 74 61 69 6e 69 6e 67 brary containing
33580 20 65 78 74 65 6e 73 69 6f 6e 20 2a 2f 0a 20 20 extension */.
33590 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 72 6f const char *zPro
335a0 63 2c 20 20 20 20 2f 2a 20 45 6e 74 72 79 20 70 c, /* Entry p
335b0 6f 69 6e 74 2e 20 20 44 65 72 69 76 65 64 20 66 oint. Derived f
335c0 72 6f 6d 20 7a 46 69 6c 65 20 69 66 20 30 20 2a rom zFile if 0 *
335d0 2f 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 /. char **pzErr
335e0 4d 73 67 20 20 20 20 20 20 20 2f 2a 20 50 75 74 Msg /* Put
335f0 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 68 error message h
33600 65 72 65 20 69 66 20 6e 6f 74 20 30 20 2a 2f 0a ere if not 0 */.
33610 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
33620 45 46 3a 20 45 6e 61 62 6c 65 20 4f 72 20 44 69 EF: Enable Or Di
33630 73 61 62 6c 65 20 45 78 74 65 6e 73 69 6f 6e 20 sable Extension
33640 4c 6f 61 64 69 6e 67 20 7b 48 31 32 36 32 30 7d Loading {H12620}
33650 20 3c 53 32 30 35 30 30 3e 0a 2a 2a 0a 2a 2a 20 <S20500>.**.**
33660 53 6f 20 61 73 20 6e 6f 74 20 74 6f 20 6f 70 65 So as not to ope
33670 6e 20 73 65 63 75 72 69 74 79 20 68 6f 6c 65 73 n security holes
33680 20 69 6e 20 6f 6c 64 65 72 20 61 70 70 6c 69 63 in older applic
33690 61 74 69 6f 6e 73 20 74 68 61 74 20 61 72 65 0a ations that are.
336a0 2a 2a 20 75 6e 70 72 65 70 61 72 65 64 20 74 6f ** unprepared to
336b0 20 64 65 61 6c 20 77 69 74 68 20 65 78 74 65 6e deal with exten
336c0 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 2c 20 61 6e sion loading, an
336d0 64 20 61 73 20 61 20 6d 65 61 6e 73 20 6f 66 20 d as a means of
336e0 64 69 73 61 62 6c 69 6e 67 0a 2a 2a 20 65 78 74 disabling.** ext
336f0 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 20 77 ension loading w
33700 68 69 6c 65 20 65 76 61 6c 75 61 74 69 6e 67 20 hile evaluating
33710 75 73 65 72 2d 65 6e 74 65 72 65 64 20 53 51 4c user-entered SQL
33720 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 , the following
33730 41 50 49 0a 2a 2a 20 69 73 20 70 72 6f 76 69 64 API.** is provid
33740 65 64 20 74 6f 20 74 75 72 6e 20 74 68 65 20 5b ed to turn the [
33750 73 71 6c 69 74 65 33 5f 6c 6f 61 64 5f 65 78 74 sqlite3_load_ext
33760 65 6e 73 69 6f 6e 28 29 5d 20 6d 65 63 68 61 6e ension()] mechan
33770 69 73 6d 20 6f 6e 20 61 6e 64 20 6f 66 66 2e 0a ism on and off..
33780 2a 2a 0a 2a 2a 20 45 78 74 65 6e 73 69 6f 6e 20 **.** Extension
33790 6c 6f 61 64 69 6e 67 20 69 73 20 6f 66 66 20 62 loading is off b
337a0 79 20 64 65 66 61 75 6c 74 2e 20 53 65 65 20 74 y default. See t
337b0 69 63 6b 65 74 20 23 31 38 36 33 2e 0a 2a 2a 0a icket #1863..**.
337c0 2a 2a 20 7b 48 31 32 36 32 31 7d 20 43 61 6c 6c ** {H12621} Call
337d0 20 74 68 65 20 73 71 6c 69 74 65 33 5f 65 6e 61 the sqlite3_ena
337e0 62 6c 65 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 ble_load_extensi
337f0 6f 6e 28 29 20 72 6f 75 74 69 6e 65 20 77 69 74 on() routine wit
33800 68 20 6f 6e 6f 66 66 3d 3d 31 0a 2a 2a 20 20 20 h onoff==1.**
33810 20 20 20 20 20 20 20 74 6f 20 74 75 72 6e 20 65 to turn e
33820 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 xtension loading
33830 20 6f 6e 20 61 6e 64 20 63 61 6c 6c 20 69 74 20 on and call it
33840 77 69 74 68 20 6f 6e 6f 66 66 3d 3d 30 20 74 6f with onoff==0 to
33850 20 74 75 72 6e 0a 2a 2a 20 20 20 20 20 20 20 20 turn.**
33860 20 20 69 74 20 62 61 63 6b 20 6f 66 66 20 61 67 it back off ag
33870 61 69 6e 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 ain..**.** {H126
33880 32 32 7d 20 45 78 74 65 6e 73 69 6f 6e 20 6c 6f 22} Extension lo
33890 61 64 69 6e 67 20 69 73 20 6f 66 66 20 62 79 20 ading is off by
338a0 64 65 66 61 75 6c 74 2e 0a 2a 2f 0a 53 51 4c 49 default..*/.SQLI
338b0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
338c0 65 33 5f 65 6e 61 62 6c 65 5f 6c 6f 61 64 5f 65 e3_enable_load_e
338d0 78 74 65 6e 73 69 6f 6e 28 73 71 6c 69 74 65 33 xtension(sqlite3
338e0 20 2a 64 62 2c 20 69 6e 74 20 6f 6e 6f 66 66 29 *db, int onoff)
338f0 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
33900 46 3a 20 41 75 74 6f 6d 61 74 69 63 61 6c 6c 79 F: Automatically
33910 20 4c 6f 61 64 20 41 6e 20 45 78 74 65 6e 73 69 Load An Extensi
33920 6f 6e 73 20 7b 48 31 32 36 34 30 7d 20 3c 53 32 ons {H12640} <S2
33930 30 35 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 0500>.**.** This
33940 20 41 50 49 20 63 61 6e 20 62 65 20 69 6e 76 6f API can be invo
33950 6b 65 64 20 61 74 20 70 72 6f 67 72 61 6d 20 73 ked at program s
33960 74 61 72 74 75 70 20 69 6e 20 6f 72 64 65 72 20 tartup in order
33970 74 6f 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 6f to register.** o
33980 6e 65 20 6f 72 20 6d 6f 72 65 20 73 74 61 74 69 ne or more stati
33990 63 61 6c 6c 79 20 6c 69 6e 6b 65 64 20 65 78 74 cally linked ext
339a0 65 6e 73 69 6f 6e 73 20 74 68 61 74 20 77 69 6c ensions that wil
339b0 6c 20 62 65 20 61 76 61 69 6c 61 62 6c 65 0a 2a l be available.*
339c0 2a 20 74 6f 20 61 6c 6c 20 6e 65 77 20 5b 64 61 * to all new [da
339d0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
339e0 6e 73 5d 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a ns]. {END}.**.**
339f0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 74 This routine st
33a00 6f 72 65 73 20 61 20 70 6f 69 6e 74 65 72 20 74 ores a pointer t
33a10 6f 20 74 68 65 20 65 78 74 65 6e 73 69 6f 6e 20 o the extension
33a20 69 6e 20 61 6e 20 61 72 72 61 79 20 74 68 61 74 in an array that
33a30 20 69 73 0a 2a 2a 20 6f 62 74 61 69 6e 65 64 20 is.** obtained
33a40 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 61 from [sqlite3_ma
33a50 6c 6c 6f 63 28 29 5d 2e 20 20 49 66 20 79 6f 75 lloc()]. If you
33a60 20 72 75 6e 20 61 20 6d 65 6d 6f 72 79 20 6c 65 run a memory le
33a70 61 6b 20 63 68 65 63 6b 65 72 0a 2a 2a 20 6f 6e ak checker.** on
33a80 20 79 6f 75 72 20 70 72 6f 67 72 61 6d 20 61 6e your program an
33a90 64 20 69 74 20 72 65 70 6f 72 74 73 20 61 20 6c d it reports a l
33aa0 65 61 6b 20 62 65 63 61 75 73 65 20 6f 66 20 74 eak because of t
33ab0 68 69 73 20 61 72 72 61 79 2c 20 69 6e 76 6f 6b his array, invok
33ac0 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 72 65 e.** [sqlite3_re
33ad0 73 65 74 5f 61 75 74 6f 5f 65 78 74 65 6e 73 69 set_auto_extensi
33ae0 6f 6e 28 29 5d 20 70 72 69 6f 72 20 74 6f 20 73 on()] prior to s
33af0 68 75 74 64 6f 77 6e 20 74 6f 20 66 72 65 65 20 hutdown to free
33b00 74 68 65 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 0a 2a the memory..**.*
33b10 2a 20 7b 48 31 32 36 34 31 7d 20 54 68 69 73 20 * {H12641} This
33b20 66 75 6e 63 74 69 6f 6e 20 72 65 67 69 73 74 65 function registe
33b30 72 73 20 61 6e 20 65 78 74 65 6e 73 69 6f 6e 20 rs an extension
33b40 65 6e 74 72 79 20 70 6f 69 6e 74 20 74 68 61 74 entry point that
33b50 20 69 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 is.**
33b60 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 69 6e automatically in
33b70 76 6f 6b 65 64 20 77 68 65 6e 65 76 65 72 20 61 voked whenever a
33b80 20 6e 65 77 20 5b 64 61 74 61 62 61 73 65 20 63 new [database c
33b90 6f 6e 6e 65 63 74 69 6f 6e 5d 0a 2a 2a 20 20 20 onnection].**
33ba0 20 20 20 20 20 20 20 69 73 20 6f 70 65 6e 65 64 is opened
33bb0 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f using [sqlite3_
33bc0 6f 70 65 6e 28 29 5d 2c 20 5b 73 71 6c 69 74 65 open()], [sqlite
33bd0 33 5f 6f 70 65 6e 31 36 28 29 5d 2c 0a 2a 2a 20 3_open16()],.**
33be0 20 20 20 20 20 20 20 20 20 6f 72 20 5b 73 71 6c or [sql
33bf0 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d 2e ite3_open_v2()].
33c00 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 34 32 7d 20 .**.** {H12642}
33c10 44 75 70 6c 69 63 61 74 65 20 65 78 74 65 6e 73 Duplicate extens
33c20 69 6f 6e 73 20 61 72 65 20 64 65 74 65 63 74 65 ions are detecte
33c30 64 20 73 6f 20 63 61 6c 6c 69 6e 67 20 74 68 69 d so calling thi
33c40 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 20 20 20 s routine.**
33c50 20 20 20 20 20 20 6d 75 6c 74 69 70 6c 65 20 74 multiple t
33c60 69 6d 65 73 20 77 69 74 68 20 74 68 65 20 73 61 imes with the sa
33c70 6d 65 20 65 78 74 65 6e 73 69 6f 6e 20 69 73 20 me extension is
33c80 68 61 72 6d 6c 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 harmless..**.**
33c90 7b 48 31 32 36 34 33 7d 20 54 68 69 73 20 72 6f {H12643} This ro
33ca0 75 74 69 6e 65 20 73 74 6f 72 65 73 20 61 20 70 utine stores a p
33cb0 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 65 78 ointer to the ex
33cc0 74 65 6e 73 69 6f 6e 20 69 6e 20 61 6e 20 61 72 tension in an ar
33cd0 72 61 79 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ray.**
33ce0 74 68 61 74 20 69 73 20 6f 62 74 61 69 6e 65 64 that is obtained
33cf0 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d from [sqlite3_m
33d00 61 6c 6c 6f 63 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 alloc()]..**.**
33d10 7b 48 31 32 36 34 34 7d 20 41 75 74 6f 6d 61 74 {H12644} Automat
33d20 69 63 20 65 78 74 65 6e 73 69 6f 6e 73 20 61 70 ic extensions ap
33d30 70 6c 79 20 61 63 72 6f 73 73 20 61 6c 6c 20 74 ply across all t
33d40 68 72 65 61 64 73 2e 0a 2a 2f 0a 53 51 4c 49 54 hreads..*/.SQLIT
33d50 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
33d60 33 5f 61 75 74 6f 5f 65 78 74 65 6e 73 69 6f 6e 3_auto_extension
33d70 28 76 6f 69 64 20 28 2a 78 45 6e 74 72 79 50 6f (void (*xEntryPo
33d80 69 6e 74 29 28 76 6f 69 64 29 29 3b 0a 0a 2f 2a int)(void));../*
33d90 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 65 .** CAPI3REF: Re
33da0 73 65 74 20 41 75 74 6f 6d 61 74 69 63 20 45 78 set Automatic Ex
33db0 74 65 6e 73 69 6f 6e 20 4c 6f 61 64 69 6e 67 20 tension Loading
33dc0 7b 48 31 32 36 36 30 7d 20 3c 53 32 30 35 30 30 {H12660} <S20500
33dd0 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e >.**.** This fun
33de0 63 74 69 6f 6e 20 64 69 73 61 62 6c 65 73 20 61 ction disables a
33df0 6c 6c 20 70 72 65 76 69 6f 75 73 6c 79 20 72 65 ll previously re
33e00 67 69 73 74 65 72 65 64 20 61 75 74 6f 6d 61 74 gistered automat
33e10 69 63 0a 2a 2a 20 65 78 74 65 6e 73 69 6f 6e 73 ic.** extensions
33e20 2e 20 7b 45 4e 44 7d 20 20 49 74 20 75 6e 64 6f . {END} It undo
33e30 65 73 20 74 68 65 20 65 66 66 65 63 74 20 6f 66 es the effect of
33e40 20 61 6c 6c 20 70 72 69 6f 72 0a 2a 2a 20 5b 73 all prior.** [s
33e50 71 6c 69 74 65 33 5f 61 75 74 6f 5f 65 78 74 65 qlite3_auto_exte
33e60 6e 73 69 6f 6e 28 29 5d 20 63 61 6c 6c 73 2e 0a nsion()] calls..
33e70 2a 2a 0a 2a 2a 20 7b 48 31 32 36 36 31 7d 20 54 **.** {H12661} T
33e80 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 69 73 his function dis
33e90 61 62 6c 65 73 20 61 6c 6c 20 70 72 65 76 69 6f ables all previo
33ea0 75 73 6c 79 20 72 65 67 69 73 74 65 72 65 64 0a usly registered.
33eb0 2a 2a 20 20 20 20 20 20 20 20 20 20 61 75 74 6f ** auto
33ec0 6d 61 74 69 63 20 65 78 74 65 6e 73 69 6f 6e 73 matic extensions
33ed0 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 36 32 7d ..**.** {H12662}
33ee0 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 This function d
33ef0 69 73 61 62 6c 65 73 20 61 75 74 6f 6d 61 74 69 isables automati
33f00 63 20 65 78 74 65 6e 73 69 6f 6e 73 20 69 6e 20 c extensions in
33f10 61 6c 6c 20 74 68 72 65 61 64 73 2e 0a 2a 2f 0a all threads..*/.
33f20 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 SQLITE_API void
33f30 73 71 6c 69 74 65 33 5f 72 65 73 65 74 5f 61 75 sqlite3_reset_au
33f40 74 6f 5f 65 78 74 65 6e 73 69 6f 6e 28 76 6f 69 to_extension(voi
33f50 64 29 3b 0a 0a 2f 2a 0a 2a 2a 2a 2a 2a 2a 20 45 d);../*.****** E
33f60 58 50 45 52 49 4d 45 4e 54 41 4c 20 2d 20 73 75 XPERIMENTAL - su
33f70 62 6a 65 63 74 20 74 6f 20 63 68 61 6e 67 65 20 bject to change
33f80 77 69 74 68 6f 75 74 20 6e 6f 74 69 63 65 20 2a without notice *
33f90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
33fa0 0a 2a 2a 20 54 68 65 20 69 6e 74 65 72 66 61 63 .** The interfac
33fb0 65 20 74 6f 20 74 68 65 20 76 69 72 74 75 61 6c e to the virtual
33fc0 2d 74 61 62 6c 65 20 6d 65 63 68 61 6e 69 73 6d -table mechanism
33fd0 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 63 6f is currently co
33fe0 6e 73 69 64 65 72 65 64 0a 2a 2a 20 74 6f 20 62 nsidered.** to b
33ff0 65 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 2e 20 e experimental.
34000 20 54 68 65 20 69 6e 74 65 72 66 61 63 65 20 6d The interface m
34010 69 67 68 74 20 63 68 61 6e 67 65 20 69 6e 20 69 ight change in i
34020 6e 63 6f 6d 70 61 74 69 62 6c 65 20 77 61 79 73 ncompatible ways
34030 2e 0a 2a 2a 20 49 66 20 74 68 69 73 20 69 73 20 ..** If this is
34040 61 20 70 72 6f 62 6c 65 6d 20 66 6f 72 20 79 6f a problem for yo
34050 75 2c 20 64 6f 20 6e 6f 74 20 75 73 65 20 74 68 u, do not use th
34060 65 20 69 6e 74 65 72 66 61 63 65 20 61 74 20 74 e interface at t
34070 68 69 73 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 his time..**.**
34080 57 68 65 6e 20 74 68 65 20 76 69 72 74 75 61 6c When the virtual
34090 2d 74 61 62 6c 65 20 6d 65 63 68 61 6e 69 73 6d -table mechanism
340a0 20 73 74 61 62 69 6c 69 7a 65 73 2c 20 77 65 20 stabilizes, we
340b0 77 69 6c 6c 20 64 65 63 6c 61 72 65 20 74 68 65 will declare the
340c0 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 66 69 .** interface fi
340d0 78 65 64 2c 20 73 75 70 70 6f 72 74 20 69 74 20 xed, support it
340e0 69 6e 64 65 66 69 6e 69 74 65 6c 79 2c 20 61 6e indefinitely, an
340f0 64 20 72 65 6d 6f 76 65 20 74 68 69 73 20 63 6f d remove this co
34100 6d 6d 65 6e 74 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a mment..*/../*.**
34110 20 53 74 72 75 63 74 75 72 65 73 20 75 73 65 64 Structures used
34120 20 62 79 20 74 68 65 20 76 69 72 74 75 61 6c 20 by the virtual
34130 74 61 62 6c 65 20 69 6e 74 65 72 66 61 63 65 0a table interface.
34140 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 */.typedef struc
34150 74 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 73 t sqlite3_vtab s
34160 71 6c 69 74 65 33 5f 76 74 61 62 3b 0a 74 79 70 qlite3_vtab;.typ
34170 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 edef struct sqli
34180 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73 te3_index_info s
34190 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 qlite3_index_inf
341a0 6f 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 o;.typedef struc
341b0 74 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 t sqlite3_vtab_c
341c0 75 72 73 6f 72 20 73 71 6c 69 74 65 33 5f 76 74 ursor sqlite3_vt
341d0 61 62 5f 63 75 72 73 6f 72 3b 0a 74 79 70 65 64 ab_cursor;.typed
341e0 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 ef struct sqlite
341f0 33 5f 6d 6f 64 75 6c 65 20 73 71 6c 69 74 65 33 3_module sqlite3
34200 5f 6d 6f 64 75 6c 65 3b 0a 0a 2f 2a 0a 2a 2a 20 _module;../*.**
34210 43 41 50 49 33 52 45 46 3a 20 56 69 72 74 75 61 CAPI3REF: Virtua
34220 6c 20 54 61 62 6c 65 20 4f 62 6a 65 63 74 20 7b l Table Object {
34230 48 31 38 30 30 30 7d 20 3c 53 32 30 34 30 30 3e H18000} <S20400>
34240 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 73 71 .** KEYWORDS: sq
34250 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 7b 76 69 lite3_module {vi
34260 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 rtual table modu
34270 6c 65 7d 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e le}.** EXPERIMEN
34280 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 TAL.**.** This s
34290 74 72 75 63 74 75 72 65 2c 20 73 6f 6d 65 74 69 tructure, someti
342a0 6d 65 73 20 63 61 6c 6c 65 64 20 61 20 61 20 22 mes called a a "
342b0 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f virtual table mo
342c0 64 75 6c 65 22 2c 20 0a 2a 2a 20 64 65 66 69 6e dule", .** defin
342d0 65 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 es the implement
342e0 61 74 69 6f 6e 20 6f 66 20 61 20 5b 76 69 72 74 ation of a [virt
342f0 75 61 6c 20 74 61 62 6c 65 73 5d 2e 20 20 0a 2a ual tables]. .*
34300 2a 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65 * This structure
34310 20 63 6f 6e 73 69 73 74 73 20 6d 6f 73 74 6c 79 consists mostly
34320 20 6f 66 20 6d 65 74 68 6f 64 73 20 66 6f 72 20 of methods for
34330 74 68 65 20 6d 6f 64 75 6c 65 2e 0a 2a 2a 0a 2a the module..**.*
34340 2a 20 41 20 76 69 72 74 75 61 6c 20 74 61 62 6c * A virtual tabl
34350 65 20 6d 6f 64 75 6c 65 20 69 73 20 63 72 65 61 e module is crea
34360 74 65 64 20 62 79 20 66 69 6c 6c 69 6e 67 20 69 ted by filling i
34370 6e 20 61 20 70 65 72 73 69 73 74 65 6e 74 0a 2a n a persistent.*
34380 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 * instance of th
34390 69 73 20 73 74 72 75 63 74 75 72 65 20 61 6e 64 is structure and
343a0 20 70 61 73 73 69 6e 67 20 61 20 70 6f 69 6e 74 passing a point
343b0 65 72 20 74 6f 20 74 68 61 74 20 69 6e 73 74 61 er to that insta
343c0 6e 63 65 0a 2a 2a 20 74 6f 20 5b 73 71 6c 69 74 nce.** to [sqlit
343d0 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 e3_create_module
343e0 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f ()] or [sqlite3_
343f0 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 5f 76 32 create_module_v2
34400 28 29 5d 2e 0a 2a 2a 20 54 68 65 20 72 65 67 69 ()]..** The regi
34410 73 74 72 61 74 69 6f 6e 20 72 65 6d 61 69 6e 73 stration remains
34420 20 76 61 6c 69 64 20 75 6e 74 69 6c 20 69 74 20 valid until it
34430 69 73 20 72 65 70 6c 61 63 65 64 20 62 79 20 61 is replaced by a
34440 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 6d 6f different.** mo
34450 64 75 6c 65 20 6f 72 20 75 6e 74 69 6c 20 74 68 dule or until th
34460 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e e [database conn
34470 65 63 74 69 6f 6e 5d 20 63 6c 6f 73 65 73 2e 20 ection] closes.
34480 20 54 68 65 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20 The content.**
34490 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 of this structur
344a0 65 20 6d 75 73 74 20 6e 6f 74 20 63 68 61 6e 67 e must not chang
344b0 65 20 77 68 69 6c 65 20 69 74 20 69 73 20 72 65 e while it is re
344c0 67 69 73 74 65 72 65 64 20 77 69 74 68 0a 2a 2a gistered with.**
344d0 20 61 6e 79 20 64 61 74 61 62 61 73 65 20 63 6f any database co
344e0 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 nnection..*/.str
344f0 75 63 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 uct sqlite3_modu
34500 6c 65 20 7b 0a 20 20 69 6e 74 20 69 56 65 72 73 le {. int iVers
34510 69 6f 6e 3b 0a 20 20 69 6e 74 20 28 2a 78 43 72 ion;. int (*xCr
34520 65 61 74 65 29 28 73 71 6c 69 74 65 33 2a 2c 20 eate)(sqlite3*,
34530 76 6f 69 64 20 2a 70 41 75 78 2c 0a 20 20 20 20 void *pAux,.
34540 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 61 int a
34550 72 67 63 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 rgc, const char
34560 2a 63 6f 6e 73 74 2a 61 72 67 76 2c 0a 20 20 20 *const*argv,.
34570 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 sqli
34580 74 65 33 5f 76 74 61 62 20 2a 2a 70 70 56 54 61 te3_vtab **ppVTa
34590 62 2c 20 63 68 61 72 2a 2a 29 3b 0a 20 20 69 6e b, char**);. in
345a0 74 20 28 2a 78 43 6f 6e 6e 65 63 74 29 28 73 71 t (*xConnect)(sq
345b0 6c 69 74 65 33 2a 2c 20 76 6f 69 64 20 2a 70 41 lite3*, void *pA
345c0 75 78 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 ux,.
345d0 20 20 20 69 6e 74 20 61 72 67 63 2c 20 63 6f 6e int argc, con
345e0 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 74 2a 61 st char *const*a
345f0 72 67 76 2c 0a 20 20 20 20 20 20 20 20 20 20 20 rgv,.
34600 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 sqlite3_vtab
34610 20 2a 2a 70 70 56 54 61 62 2c 20 63 68 61 72 2a **ppVTab, char*
34620 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 42 65 73 *);. int (*xBes
34630 74 49 6e 64 65 78 29 28 73 71 6c 69 74 65 33 5f tIndex)(sqlite3_
34640 76 74 61 62 20 2a 70 56 54 61 62 2c 20 73 71 6c vtab *pVTab, sql
34650 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 2a ite3_index_info*
34660 29 3b 0a 20 20 69 6e 74 20 28 2a 78 44 69 73 63 );. int (*xDisc
34670 6f 6e 6e 65 63 74 29 28 73 71 6c 69 74 65 33 5f onnect)(sqlite3_
34680 76 74 61 62 20 2a 70 56 54 61 62 29 3b 0a 20 20 vtab *pVTab);.
34690 69 6e 74 20 28 2a 78 44 65 73 74 72 6f 79 29 28 int (*xDestroy)(
346a0 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 sqlite3_vtab *pV
346b0 54 61 62 29 3b 0a 20 20 69 6e 74 20 28 2a 78 4f Tab);. int (*xO
346c0 70 65 6e 29 28 73 71 6c 69 74 65 33 5f 76 74 61 pen)(sqlite3_vta
346d0 62 20 2a 70 56 54 61 62 2c 20 73 71 6c 69 74 65 b *pVTab, sqlite
346e0 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 2a 3_vtab_cursor **
346f0 70 70 43 75 72 73 6f 72 29 3b 0a 20 20 69 6e 74 ppCursor);. int
34700 20 28 2a 78 43 6c 6f 73 65 29 28 73 71 6c 69 74 (*xClose)(sqlit
34710 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 2a 29 e3_vtab_cursor*)
34720 3b 0a 20 20 69 6e 74 20 28 2a 78 46 69 6c 74 65 ;. int (*xFilte
34730 72 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f r)(sqlite3_vtab_
34740 63 75 72 73 6f 72 2a 2c 20 69 6e 74 20 69 64 78 cursor*, int idx
34750 4e 75 6d 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 Num, const char
34760 2a 69 64 78 53 74 72 2c 0a 20 20 20 20 20 20 20 *idxStr,.
34770 20 20 20 20 20 20 20 20 20 69 6e 74 20 61 72 67 int arg
34780 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 c, sqlite3_value
34790 20 2a 2a 61 72 67 76 29 3b 0a 20 20 69 6e 74 20 **argv);. int
347a0 28 2a 78 4e 65 78 74 29 28 73 71 6c 69 74 65 33 (*xNext)(sqlite3
347b0 5f 76 74 61 62 5f 63 75 72 73 6f 72 2a 29 3b 0a _vtab_cursor*);.
347c0 20 20 69 6e 74 20 28 2a 78 45 6f 66 29 28 73 71 int (*xEof)(sq
347d0 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f lite3_vtab_curso
347e0 72 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 43 6f r*);. int (*xCo
347f0 6c 75 6d 6e 29 28 73 71 6c 69 74 65 33 5f 76 74 lumn)(sqlite3_vt
34800 61 62 5f 63 75 72 73 6f 72 2a 2c 20 73 71 6c 69 ab_cursor*, sqli
34810 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 69 6e te3_context*, in
34820 74 29 3b 0a 20 20 69 6e 74 20 28 2a 78 52 6f 77 t);. int (*xRow
34830 69 64 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 id)(sqlite3_vtab
34840 5f 63 75 72 73 6f 72 2a 2c 20 73 71 6c 69 74 65 _cursor*, sqlite
34850 33 5f 69 6e 74 36 34 20 2a 70 52 6f 77 69 64 29 3_int64 *pRowid)
34860 3b 0a 20 20 69 6e 74 20 28 2a 78 55 70 64 61 74 ;. int (*xUpdat
34870 65 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 e)(sqlite3_vtab
34880 2a 2c 20 69 6e 74 2c 20 73 71 6c 69 74 65 33 5f *, int, sqlite3_
34890 76 61 6c 75 65 20 2a 2a 2c 20 73 71 6c 69 74 65 value **, sqlite
348a0 33 5f 69 6e 74 36 34 20 2a 29 3b 0a 20 20 69 6e 3_int64 *);. in
348b0 74 20 28 2a 78 42 65 67 69 6e 29 28 73 71 6c 69 t (*xBegin)(sqli
348c0 74 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62 29 te3_vtab *pVTab)
348d0 3b 0a 20 20 69 6e 74 20 28 2a 78 53 79 6e 63 29 ;. int (*xSync)
348e0 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 (sqlite3_vtab *p
348f0 56 54 61 62 29 3b 0a 20 20 69 6e 74 20 28 2a 78 VTab);. int (*x
34900 43 6f 6d 6d 69 74 29 28 73 71 6c 69 74 65 33 5f Commit)(sqlite3_
34910 76 74 61 62 20 2a 70 56 54 61 62 29 3b 0a 20 20 vtab *pVTab);.
34920 69 6e 74 20 28 2a 78 52 6f 6c 6c 62 61 63 6b 29 int (*xRollback)
34930 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 (sqlite3_vtab *p
34940 56 54 61 62 29 3b 0a 20 20 69 6e 74 20 28 2a 78 VTab);. int (*x
34950 46 69 6e 64 46 75 6e 63 74 69 6f 6e 29 28 73 71 FindFunction)(sq
34960 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 lite3_vtab *pVta
34970 62 2c 20 69 6e 74 20 6e 41 72 67 2c 20 63 6f 6e b, int nArg, con
34980 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 0a st char *zName,.
34990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
349a0 20 20 20 20 20 20 20 76 6f 69 64 20 28 2a 2a 70 void (**p
349b0 78 46 75 6e 63 29 28 73 71 6c 69 74 65 33 5f 63 xFunc)(sqlite3_c
349c0 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 ontext*,int,sqli
349d0 74 65 33 5f 76 61 6c 75 65 2a 2a 29 2c 0a 20 20 te3_value**),.
349e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
349f0 20 20 20 20 20 76 6f 69 64 20 2a 2a 70 70 41 72 void **ppAr
34a00 67 29 3b 0a 20 20 69 6e 74 20 28 2a 78 52 65 6e g);. int (*xRen
34a10 61 6d 65 29 28 73 71 6c 69 74 65 33 5f 76 74 61 ame)(sqlite3_vta
34a20 62 20 2a 70 56 74 61 62 2c 20 63 6f 6e 73 74 20 b *pVtab, const
34a30 63 68 61 72 20 2a 7a 4e 65 77 29 3b 0a 7d 3b 0a char *zNew);.};.
34a40 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
34a50 20 56 69 72 74 75 61 6c 20 54 61 62 6c 65 20 49 Virtual Table I
34a60 6e 64 65 78 69 6e 67 20 49 6e 66 6f 72 6d 61 74 ndexing Informat
34a70 69 6f 6e 20 7b 48 31 38 31 30 30 7d 20 3c 53 32 ion {H18100} <S2
34a80 30 34 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 0400>.** KEYWORD
34a90 53 3a 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 S: sqlite3_index
34aa0 5f 69 6e 66 6f 0a 2a 2a 20 45 58 50 45 52 49 4d _info.** EXPERIM
34ab0 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ENTAL.**.** The
34ac0 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e sqlite3_index_in
34ad0 66 6f 20 73 74 72 75 63 74 75 72 65 20 61 6e 64 fo structure and
34ae0 20 69 74 73 20 73 75 62 73 74 72 75 63 74 75 72 its substructur
34af0 65 73 20 69 73 20 75 73 65 64 20 74 6f 0a 2a 2a es is used to.**
34b00 20 70 61 73 73 20 69 6e 66 6f 72 6d 61 74 69 6f pass informatio
34b10 6e 20 69 6e 74 6f 20 61 6e 64 20 72 65 63 65 69 n into and recei
34b20 76 65 20 74 68 65 20 72 65 70 6c 79 20 66 72 6f ve the reply fro
34b30 6d 20 74 68 65 20 5b 78 42 65 73 74 49 6e 64 65 m the [xBestInde
34b40 78 5d 0a 2a 2a 20 6d 65 74 68 6f 64 20 6f 66 20 x].** method of
34b50 61 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 a [virtual table
34b60 20 6d 6f 64 75 6c 65 5d 2e 20 20 54 68 65 20 66 module]. The f
34b70 69 65 6c 64 73 20 75 6e 64 65 72 20 2a 2a 49 6e ields under **In
34b80 70 75 74 73 2a 2a 20 61 72 65 20 74 68 65 0a 2a puts** are the.*
34b90 2a 20 69 6e 70 75 74 73 20 74 6f 20 78 42 65 73 * inputs to xBes
34ba0 74 49 6e 64 65 78 20 61 6e 64 20 61 72 65 20 72 tIndex and are r
34bb0 65 61 64 2d 6f 6e 6c 79 2e 20 20 78 42 65 73 74 ead-only. xBest
34bc0 49 6e 64 65 78 20 69 6e 73 65 72 74 73 20 69 74 Index inserts it
34bd0 73 0a 2a 2a 20 72 65 73 75 6c 74 73 20 69 6e 74 s.** results int
34be0 6f 20 74 68 65 20 2a 2a 4f 75 74 70 75 74 73 2a o the **Outputs*
34bf0 2a 20 66 69 65 6c 64 73 2e 0a 2a 2a 0a 2a 2a 20 * fields..**.**
34c00 54 68 65 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b The aConstraint[
34c10 5d 20 61 72 72 61 79 20 72 65 63 6f 72 64 73 20 ] array records
34c20 57 48 45 52 45 20 63 6c 61 75 73 65 20 63 6f 6e WHERE clause con
34c30 73 74 72 61 69 6e 74 73 20 6f 66 20 74 68 65 20 straints of the
34c40 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20 3c 70 72 65 form:.**.** <pre
34c50 3e 63 6f 6c 75 6d 6e 20 4f 50 20 65 78 70 72 3c >column OP expr<
34c60 2f 70 72 65 3e 0a 2a 2a 0a 2a 2a 20 77 68 65 72 /pre>.**.** wher
34c70 65 20 4f 50 20 69 73 20 3d 2c 20 26 6c 74 3b 2c e OP is =, <,
34c80 20 26 6c 74 3b 3d 2c 20 26 67 74 3b 2c 20 6f 72 <=, >, or
34c90 20 26 67 74 3b 3d 2e 20 20 54 68 65 20 70 61 72 >=. The par
34ca0 74 69 63 75 6c 61 72 20 6f 70 65 72 61 74 6f 72 ticular operator
34cb0 20 69 73 0a 2a 2a 20 73 74 6f 72 65 64 20 69 6e is.** stored in
34cc0 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b 5d 2e 6f aConstraint[].o
34cd0 70 2e 20 20 54 68 65 20 69 6e 64 65 78 20 6f 66 p. The index of
34ce0 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69 73 20 73 the column is s
34cf0 74 6f 72 65 64 20 69 6e 0a 2a 2a 20 61 43 6f 6e tored in.** aCon
34d00 73 74 72 61 69 6e 74 5b 5d 2e 69 43 6f 6c 75 6d straint[].iColum
34d10 6e 2e 20 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b n. aConstraint[
34d20 5d 2e 75 73 61 62 6c 65 20 69 73 20 54 52 55 45 ].usable is TRUE
34d30 20 69 66 20 74 68 65 0a 2a 2a 20 65 78 70 72 20 if the.** expr
34d40 6f 6e 20 74 68 65 20 72 69 67 68 74 2d 68 61 6e on the right-han
34d50 64 20 73 69 64 65 20 63 61 6e 20 62 65 20 65 76 d side can be ev
34d60 61 6c 75 61 74 65 64 20 28 61 6e 64 20 74 68 75 aluated (and thu
34d70 73 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 s the constraint
34d80 0a 2a 2a 20 69 73 20 75 73 61 62 6c 65 29 20 61 .** is usable) a
34d90 6e 64 20 66 61 6c 73 65 20 69 66 20 69 74 20 63 nd false if it c
34da0 61 6e 6e 6f 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 annot..**.** The
34db0 20 6f 70 74 69 6d 69 7a 65 72 20 61 75 74 6f 6d optimizer autom
34dc0 61 74 69 63 61 6c 6c 79 20 69 6e 76 65 72 74 73 atically inverts
34dd0 20 74 65 72 6d 73 20 6f 66 20 74 68 65 20 66 6f terms of the fo
34de0 72 6d 20 22 65 78 70 72 20 4f 50 20 63 6f 6c 75 rm "expr OP colu
34df0 6d 6e 22 0a 2a 2a 20 61 6e 64 20 6d 61 6b 65 73 mn".** and makes
34e00 20 6f 74 68 65 72 20 73 69 6d 70 6c 69 66 69 63 other simplific
34e10 61 74 69 6f 6e 73 20 74 6f 20 74 68 65 20 57 48 ations to the WH
34e20 45 52 45 20 63 6c 61 75 73 65 20 69 6e 20 61 6e ERE clause in an
34e30 20 61 74 74 65 6d 70 74 20 74 6f 0a 2a 2a 20 67 attempt to.** g
34e40 65 74 20 61 73 20 6d 61 6e 79 20 57 48 45 52 45 et as many WHERE
34e50 20 63 6c 61 75 73 65 20 74 65 72 6d 73 20 69 6e clause terms in
34e60 74 6f 20 74 68 65 20 66 6f 72 6d 20 73 68 6f 77 to the form show
34e70 6e 20 61 62 6f 76 65 20 61 73 20 70 6f 73 73 69 n above as possi
34e80 62 6c 65 2e 0a 2a 2a 20 54 68 65 20 61 43 6f 6e ble..** The aCon
34e90 73 74 72 61 69 6e 74 5b 5d 20 61 72 72 61 79 20 straint[] array
34ea0 6f 6e 6c 79 20 72 65 70 6f 72 74 73 20 57 48 45 only reports WHE
34eb0 52 45 20 63 6c 61 75 73 65 20 74 65 72 6d 73 20 RE clause terms
34ec0 69 6e 20 74 68 65 20 63 6f 72 72 65 63 74 0a 2a in the correct.*
34ed0 2a 20 66 6f 72 6d 20 74 68 61 74 20 72 65 66 65 * form that refe
34ee0 72 20 74 6f 20 74 68 65 20 70 61 72 74 69 63 75 r to the particu
34ef0 6c 61 72 20 76 69 72 74 75 61 6c 20 74 61 62 6c lar virtual tabl
34f00 65 20 62 65 69 6e 67 20 71 75 65 72 69 65 64 2e e being queried.
34f10 0a 2a 2a 0a 2a 2a 20 49 6e 66 6f 72 6d 61 74 69 .**.** Informati
34f20 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 4f 52 44 on about the ORD
34f30 45 52 20 42 59 20 63 6c 61 75 73 65 20 69 73 20 ER BY clause is
34f40 73 74 6f 72 65 64 20 69 6e 20 61 4f 72 64 65 72 stored in aOrder
34f50 42 79 5b 5d 2e 0a 2a 2a 20 45 61 63 68 20 74 65 By[]..** Each te
34f60 72 6d 20 6f 66 20 61 4f 72 64 65 72 42 79 20 72 rm of aOrderBy r
34f70 65 63 6f 72 64 73 20 61 20 63 6f 6c 75 6d 6e 20 ecords a column
34f80 6f 66 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 of the ORDER BY
34f90 63 6c 61 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 clause..**.** Th
34fa0 65 20 5b 78 42 65 73 74 49 6e 64 65 78 5d 20 6d e [xBestIndex] m
34fb0 65 74 68 6f 64 20 6d 75 73 74 20 66 69 6c 6c 20 ethod must fill
34fc0 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65 aConstraintUsage
34fd0 5b 5d 20 77 69 74 68 20 69 6e 66 6f 72 6d 61 74 [] with informat
34fe0 69 6f 6e 0a 2a 2a 20 61 62 6f 75 74 20 77 68 61 ion.** about wha
34ff0 74 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 t parameters to
35000 70 61 73 73 20 74 6f 20 78 46 69 6c 74 65 72 2e pass to xFilter.
35010 20 20 49 66 20 61 72 67 76 49 6e 64 65 78 3e 30 If argvIndex>0
35020 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 72 69 67 then.** the rig
35030 68 74 2d 68 61 6e 64 20 73 69 64 65 20 6f 66 20 ht-hand side of
35040 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e the correspondin
35050 67 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b 5d 20 g aConstraint[]
35060 69 73 20 65 76 61 6c 75 61 74 65 64 0a 2a 2a 20 is evaluated.**
35070 61 6e 64 20 62 65 63 6f 6d 65 73 20 74 68 65 20 and becomes the
35080 61 72 67 76 49 6e 64 65 78 2d 74 68 20 65 6e 74 argvIndex-th ent
35090 72 79 20 69 6e 20 61 72 67 76 2e 20 20 49 66 20 ry in argv. If
350a0 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65 aConstraintUsage
350b0 5b 5d 2e 6f 6d 69 74 0a 2a 2a 20 69 73 20 74 72 [].omit.** is tr
350c0 75 65 2c 20 74 68 65 6e 20 74 68 65 20 63 6f 6e ue, then the con
350d0 73 74 72 61 69 6e 74 20 69 73 20 61 73 73 75 6d straint is assum
350e0 65 64 20 74 6f 20 62 65 20 66 75 6c 6c 79 20 68 ed to be fully h
350f0 61 6e 64 6c 65 64 20 62 79 20 74 68 65 0a 2a 2a andled by the.**
35100 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 61 virtual table a
35110 6e 64 20 69 73 20 6e 6f 74 20 63 68 65 63 6b 65 nd is not checke
35120 64 20 61 67 61 69 6e 20 62 79 20 53 51 4c 69 74 d again by SQLit
35130 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 64 78 e..**.** The idx
35140 4e 75 6d 20 61 6e 64 20 69 64 78 50 74 72 20 76 Num and idxPtr v
35150 61 6c 75 65 73 20 61 72 65 20 72 65 63 6f 72 64 alues are record
35160 65 64 20 61 6e 64 20 70 61 73 73 65 64 20 69 6e ed and passed in
35170 74 6f 20 74 68 65 0a 2a 2a 20 5b 78 46 69 6c 74 to the.** [xFilt
35180 65 72 5d 20 6d 65 74 68 6f 64 2e 0a 2a 2a 20 5b er] method..** [
35190 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 20 sqlite3_free()]
351a0 69 73 20 75 73 65 64 20 74 6f 20 66 72 65 65 20 is used to free
351b0 69 64 78 50 74 72 20 69 66 20 61 6e 64 20 6f 6e idxPtr if and on
351c0 6c 79 20 69 66 66 0a 2a 2a 20 6e 65 65 64 54 6f ly iff.** needTo
351d0 46 72 65 65 49 64 78 50 74 72 20 69 73 20 74 72 FreeIdxPtr is tr
351e0 75 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 72 ue..**.** The or
351f0 64 65 72 42 79 43 6f 6e 73 75 6d 65 64 20 6d 65 derByConsumed me
35200 61 6e 73 20 74 68 61 74 20 6f 75 74 70 75 74 20 ans that output
35210 66 72 6f 6d 20 5b 78 46 69 6c 74 65 72 5d 2f 5b from [xFilter]/[
35220 78 4e 65 78 74 5d 20 77 69 6c 6c 20 6f 63 63 75 xNext] will occu
35230 72 20 69 6e 0a 2a 2a 20 74 68 65 20 63 6f 72 72 r in.** the corr
35240 65 63 74 20 6f 72 64 65 72 20 74 6f 20 73 61 74 ect order to sat
35250 69 73 66 79 20 74 68 65 20 4f 52 44 45 52 20 42 isfy the ORDER B
35260 59 20 63 6c 61 75 73 65 20 73 6f 20 74 68 61 74 Y clause so that
35270 20 6e 6f 20 73 65 70 61 72 61 74 65 0a 2a 2a 20 no separate.**
35280 73 6f 72 74 69 6e 67 20 73 74 65 70 20 69 73 20 sorting step is
35290 72 65 71 75 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 required..**.**
352a0 54 68 65 20 65 73 74 69 6d 61 74 65 64 43 6f 73 The estimatedCos
352b0 74 20 76 61 6c 75 65 20 69 73 20 61 6e 20 65 73 t value is an es
352c0 74 69 6d 61 74 65 20 6f 66 20 74 68 65 20 63 6f timate of the co
352d0 73 74 20 6f 66 20 64 6f 69 6e 67 20 74 68 65 0a st of doing the.
352e0 2a 2a 20 70 61 72 74 69 63 75 6c 61 72 20 6c 6f ** particular lo
352f0 6f 6b 75 70 2e 20 20 41 20 66 75 6c 6c 20 73 63 okup. A full sc
35300 61 6e 20 6f 66 20 61 20 74 61 62 6c 65 20 77 69 an of a table wi
35310 74 68 20 4e 20 65 6e 74 72 69 65 73 20 73 68 6f th N entries sho
35320 75 6c 64 20 68 61 76 65 0a 2a 2a 20 61 20 63 6f uld have.** a co
35330 73 74 20 6f 66 20 4e 2e 20 20 41 20 62 69 6e 61 st of N. A bina
35340 72 79 20 73 65 61 72 63 68 20 6f 66 20 61 20 74 ry search of a t
35350 61 62 6c 65 20 6f 66 20 4e 20 65 6e 74 72 69 65 able of N entrie
35360 73 20 73 68 6f 75 6c 64 20 68 61 76 65 20 61 0a s should have a.
35370 2a 2a 20 63 6f 73 74 20 6f 66 20 61 70 70 72 6f ** cost of appro
35380 78 69 6d 61 74 65 6c 79 20 6c 6f 67 28 4e 29 2e ximately log(N).
35390 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74 .*/.struct sqlit
353a0 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 7b 0a e3_index_info {.
353b0 20 20 2f 2a 20 49 6e 70 75 74 73 20 2a 2f 0a 20 /* Inputs */.
353c0 20 69 6e 74 20 6e 43 6f 6e 73 74 72 61 69 6e 74 int nConstraint
353d0 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e ; /* N
353e0 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 umber of entries
353f0 20 69 6e 20 61 43 6f 6e 73 74 72 61 69 6e 74 20 in aConstraint
35400 2a 2f 0a 20 20 73 74 72 75 63 74 20 73 71 6c 69 */. struct sqli
35410 74 65 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72 te3_index_constr
35420 61 69 6e 74 20 7b 0a 20 20 20 20 20 69 6e 74 20 aint {. int
35430 69 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 iColumn;
35440 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 /* Column
35450 6f 6e 20 6c 65 66 74 2d 68 61 6e 64 20 73 69 64 on left-hand sid
35460 65 20 6f 66 20 63 6f 6e 73 74 72 61 69 6e 74 20 e of constraint
35470 2a 2f 0a 20 20 20 20 20 75 6e 73 69 67 6e 65 64 */. unsigned
35480 20 63 68 61 72 20 6f 70 3b 20 20 20 20 20 20 20 char op;
35490 20 20 2f 2a 20 43 6f 6e 73 74 72 61 69 6e 74 20 /* Constraint
354a0 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20 20 20 operator */.
354b0 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 75 unsigned char u
354c0 73 61 62 6c 65 3b 20 20 20 20 20 2f 2a 20 54 72 sable; /* Tr
354d0 75 65 20 69 66 20 74 68 69 73 20 63 6f 6e 73 74 ue if this const
354e0 72 61 69 6e 74 20 69 73 20 75 73 61 62 6c 65 20 raint is usable
354f0 2a 2f 0a 20 20 20 20 20 69 6e 74 20 69 54 65 72 */. int iTer
35500 6d 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 mOffset;
35510 20 20 2f 2a 20 55 73 65 64 20 69 6e 74 65 72 6e /* Used intern
35520 61 6c 6c 79 20 2d 20 78 42 65 73 74 49 6e 64 65 ally - xBestInde
35530 78 20 73 68 6f 75 6c 64 20 69 67 6e 6f 72 65 20 x should ignore
35540 2a 2f 0a 20 20 7d 20 2a 61 43 6f 6e 73 74 72 61 */. } *aConstra
35550 69 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 int;
35560 2f 2a 20 54 61 62 6c 65 20 6f 66 20 57 48 45 52 /* Table of WHER
35570 45 20 63 6c 61 75 73 65 20 63 6f 6e 73 74 72 61 E clause constra
35580 69 6e 74 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f ints */. int nO
35590 72 64 65 72 42 79 3b 20 20 20 20 20 20 20 20 20 rderBy;
355a0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
355b0 66 20 74 65 72 6d 73 20 69 6e 20 74 68 65 20 4f f terms in the O
355c0 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 2a RDER BY clause *
355d0 2f 0a 20 20 73 74 72 75 63 74 20 73 71 6c 69 74 /. struct sqlit
355e0 65 33 5f 69 6e 64 65 78 5f 6f 72 64 65 72 62 79 e3_index_orderby
355f0 20 7b 0a 20 20 20 20 20 69 6e 74 20 69 43 6f 6c {. int iCol
35600 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 umn;
35610 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 75 6d 62 /* Column numb
35620 65 72 20 2a 2f 0a 20 20 20 20 20 75 6e 73 69 67 er */. unsig
35630 6e 65 64 20 63 68 61 72 20 64 65 73 63 3b 20 20 ned char desc;
35640 20 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 /* True for
35650 20 44 45 53 43 2e 20 20 46 61 6c 73 65 20 66 6f DESC. False fo
35660 72 20 41 53 43 2e 20 2a 2f 0a 20 20 7d 20 2a 61 r ASC. */. } *a
35670 4f 72 64 65 72 42 79 3b 20 20 20 20 20 20 20 20 OrderBy;
35680 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 4f 52 /* The OR
35690 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 2a 2f DER BY clause */
356a0 0a 20 20 2f 2a 20 4f 75 74 70 75 74 73 20 2a 2f . /* Outputs */
356b0 0a 20 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 . struct sqlite
356c0 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69 3_index_constrai
356d0 6e 74 5f 75 73 61 67 65 20 7b 0a 20 20 20 20 69 nt_usage {. i
356e0 6e 74 20 61 72 67 76 49 6e 64 65 78 3b 20 20 20 nt argvIndex;
356f0 20 20 20 20 20 20 20 20 2f 2a 20 69 66 20 3e 30 /* if >0
35700 2c 20 63 6f 6e 73 74 72 61 69 6e 74 20 69 73 20 , constraint is
35710 70 61 72 74 20 6f 66 20 61 72 67 76 20 74 6f 20 part of argv to
35720 78 46 69 6c 74 65 72 20 2a 2f 0a 20 20 20 20 75 xFilter */. u
35730 6e 73 69 67 6e 65 64 20 63 68 61 72 20 6f 6d 69 nsigned char omi
35740 74 3b 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f t; /* Do no
35750 74 20 63 6f 64 65 20 61 20 74 65 73 74 20 66 6f t code a test fo
35760 72 20 74 68 69 73 20 63 6f 6e 73 74 72 61 69 6e r this constrain
35770 74 20 2a 2f 0a 20 20 7d 20 2a 61 43 6f 6e 73 74 t */. } *aConst
35780 72 61 69 6e 74 55 73 61 67 65 3b 0a 20 20 69 6e raintUsage;. in
35790 74 20 69 64 78 4e 75 6d 3b 20 20 20 20 20 20 20 t idxNum;
357a0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
357b0 65 72 20 75 73 65 64 20 74 6f 20 69 64 65 6e 74 er used to ident
357c0 69 66 79 20 74 68 65 20 69 6e 64 65 78 20 2a 2f ify the index */
357d0 0a 20 20 63 68 61 72 20 2a 69 64 78 53 74 72 3b . char *idxStr;
357e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
357f0 20 53 74 72 69 6e 67 2c 20 70 6f 73 73 69 62 6c String, possibl
35800 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 y obtained from
35810 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 20 2a sqlite3_malloc *
35820 2f 0a 20 20 69 6e 74 20 6e 65 65 64 54 6f 46 72 /. int needToFr
35830 65 65 49 64 78 53 74 72 3b 20 20 20 20 20 20 2f eeIdxStr; /
35840 2a 20 46 72 65 65 20 69 64 78 53 74 72 20 75 73 * Free idxStr us
35850 69 6e 67 20 73 71 6c 69 74 65 33 5f 66 72 65 65 ing sqlite3_free
35860 28 29 20 69 66 20 74 72 75 65 20 2a 2f 0a 20 20 () if true */.
35870 69 6e 74 20 6f 72 64 65 72 42 79 43 6f 6e 73 75 int orderByConsu
35880 6d 65 64 3b 20 20 20 20 20 20 20 2f 2a 20 54 72 med; /* Tr
35890 75 65 20 69 66 20 6f 75 74 70 75 74 20 69 73 20 ue if output is
358a0 61 6c 72 65 61 64 79 20 6f 72 64 65 72 65 64 20 already ordered
358b0 2a 2f 0a 20 20 64 6f 75 62 6c 65 20 65 73 74 69 */. double esti
358c0 6d 61 74 65 64 43 6f 73 74 3b 20 20 20 20 20 20 matedCost;
358d0 2f 2a 20 45 73 74 69 6d 61 74 65 64 20 63 6f 73 /* Estimated cos
358e0 74 20 6f 66 20 75 73 69 6e 67 20 74 68 69 73 20 t of using this
358f0 69 6e 64 65 78 20 2a 2f 0a 7d 3b 0a 23 64 65 66 index */.};.#def
35900 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 ine SQLITE_INDEX
35910 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 45 51 20 20 _CONSTRAINT_EQ
35920 20 20 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 2.#define SQLI
35930 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 TE_INDEX_CONSTRA
35940 49 4e 54 5f 47 54 20 20 20 20 34 0a 23 64 65 66 INT_GT 4.#def
35950 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 ine SQLITE_INDEX
35960 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 4c 45 20 20 _CONSTRAINT_LE
35970 20 20 38 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 8.#define SQLI
35980 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 TE_INDEX_CONSTRA
35990 49 4e 54 5f 4c 54 20 20 20 20 31 36 0a 23 64 65 INT_LT 16.#de
359a0 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 44 45 fine SQLITE_INDE
359b0 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 47 45 20 X_CONSTRAINT_GE
359c0 20 20 20 33 32 0a 23 64 65 66 69 6e 65 20 53 51 32.#define SQ
359d0 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 LITE_INDEX_CONST
359e0 52 41 49 4e 54 5f 4d 41 54 43 48 20 36 34 0a 0a RAINT_MATCH 64..
359f0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
35a00 52 65 67 69 73 74 65 72 20 41 20 56 69 72 74 75 Register A Virtu
35a10 61 6c 20 54 61 62 6c 65 20 49 6d 70 6c 65 6d 65 al Table Impleme
35a20 6e 74 61 74 69 6f 6e 20 7b 48 31 38 32 30 30 7d ntation {H18200}
35a30 20 3c 53 32 30 34 30 30 3e 0a 2a 2a 20 45 58 50 <S20400>.** EXP
35a40 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 ERIMENTAL.**.**
35a50 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 This routine is
35a60 75 73 65 64 20 74 6f 20 72 65 67 69 73 74 65 72 used to register
35a70 20 61 20 6e 65 77 20 5b 76 69 72 74 75 61 6c 20 a new [virtual
35a80 74 61 62 6c 65 20 6d 6f 64 75 6c 65 5d 20 6e 61 table module] na
35a90 6d 65 2e 0a 2a 2a 20 4d 6f 64 75 6c 65 20 6e 61 me..** Module na
35aa0 6d 65 73 20 6d 75 73 74 20 62 65 20 72 65 67 69 mes must be regi
35ab0 73 74 65 72 65 64 20 62 65 66 6f 72 65 0a 2a 2a stered before.**
35ac0 20 63 72 65 61 74 69 6e 67 20 61 20 6e 65 77 20 creating a new
35ad0 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 5d 20 [virtual table]
35ae0 75 73 69 6e 67 20 74 68 65 20 6d 6f 64 75 6c 65 using the module
35af0 2c 20 6f 72 20 62 65 66 6f 72 65 20 75 73 69 6e , or before usin
35b00 67 20 61 0a 2a 2a 20 70 72 65 65 78 69 73 74 69 g a.** preexisti
35b10 6e 67 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c ng [virtual tabl
35b20 65 5d 20 66 6f 72 20 74 68 65 20 6d 6f 64 75 6c e] for the modul
35b30 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 6f 64 e..**.** The mod
35b40 75 6c 65 20 6e 61 6d 65 20 69 73 20 72 65 67 69 ule name is regi
35b50 73 74 65 72 65 64 20 6f 6e 20 74 68 65 20 5b 64 stered on the [d
35b60 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
35b70 6f 6e 5d 20 73 70 65 63 69 66 69 65 64 0a 2a 2a on] specified.**
35b80 20 62 79 20 74 68 65 20 66 69 72 73 74 20 70 61 by the first pa
35b90 72 61 6d 65 74 65 72 2e 20 20 54 68 65 20 6e 61 rameter. The na
35ba0 6d 65 20 6f 66 20 74 68 65 20 6d 6f 64 75 6c 65 me of the module
35bb0 20 69 73 20 67 69 76 65 6e 20 62 79 20 74 68 65 is given by the
35bc0 20 0a 2a 2a 20 73 65 63 6f 6e 64 20 70 61 72 61 .** second para
35bd0 6d 65 74 65 72 2e 20 20 54 68 65 20 74 68 69 72 meter. The thir
35be0 64 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 61 d parameter is a
35bf0 20 70 6f 69 6e 74 65 72 20 74 6f 0a 2a 2a 20 74 pointer to.** t
35c00 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f he implementatio
35c10 6e 20 6f 66 20 74 68 65 20 5b 76 69 72 74 75 61 n of the [virtua
35c20 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 5d 2e l table module].
35c30 20 20 20 54 68 65 20 66 6f 75 72 74 68 0a 2a 2a The fourth.**
35c40 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 61 6e parameter is an
35c50 20 61 72 62 69 74 72 61 72 79 20 63 6c 69 65 6e arbitrary clien
35c60 74 20 64 61 74 61 20 70 6f 69 6e 74 65 72 20 74 t data pointer t
35c70 68 61 74 20 69 73 20 70 61 73 73 65 64 20 74 68 hat is passed th
35c80 72 6f 75 67 68 0a 2a 2a 20 69 6e 74 6f 20 74 68 rough.** into th
35c90 65 20 5b 78 43 72 65 61 74 65 5d 20 61 6e 64 20 e [xCreate] and
35ca0 5b 78 43 6f 6e 6e 65 63 74 5d 20 6d 65 74 68 6f [xConnect] metho
35cb0 64 73 20 6f 66 20 74 68 65 20 76 69 72 74 75 61 ds of the virtua
35cc0 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 0a 2a l table module.*
35cd0 2a 20 77 68 65 6e 20 61 20 6e 65 77 20 76 69 72 * when a new vir
35ce0 74 75 61 6c 20 74 61 62 6c 65 20 69 73 20 62 65 tual table is be
35cf0 20 62 65 69 6e 67 20 63 72 65 61 74 65 64 20 6f being created o
35d00 72 20 72 65 69 6e 69 74 69 61 6c 69 7a 65 64 2e r reinitialized.
35d10 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 .**.** This inte
35d20 72 66 61 63 65 20 68 61 73 20 65 78 61 63 74 6c rface has exactl
35d30 79 20 74 68 65 20 73 61 6d 65 20 65 66 66 65 63 y the same effec
35d40 74 20 61 73 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 t as calling.**
35d50 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f [sqlite3_create_
35d60 6d 6f 64 75 6c 65 5f 76 32 28 29 5d 20 77 69 74 module_v2()] wit
35d70 68 20 61 20 4e 55 4c 4c 20 63 6c 69 65 6e 74 20 h a NULL client
35d80 64 61 74 61 20 64 65 73 74 72 75 63 74 6f 72 2e data destructor.
35d90 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 .*/.SQLITE_API S
35da0 51 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 QLITE_EXPERIMENT
35db0 41 4c 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 AL int sqlite3_c
35dc0 72 65 61 74 65 5f 6d 6f 64 75 6c 65 28 0a 20 20 reate_module(.
35dd0 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 sqlite3 *db,
35de0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 51 /* SQ
35df0 4c 69 74 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 Lite connection
35e00 74 6f 20 72 65 67 69 73 74 65 72 20 6d 6f 64 75 to register modu
35e10 6c 65 20 77 69 74 68 20 2a 2f 0a 20 20 63 6f 6e le with */. con
35e20 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 st char *zName,
35e30 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 /* Name
35e40 6f 66 20 74 68 65 20 6d 6f 64 75 6c 65 20 2a 2f of the module */
35e50 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 . const sqlite3
35e60 5f 6d 6f 64 75 6c 65 20 2a 70 2c 20 20 20 2f 2a _module *p, /*
35e70 20 4d 65 74 68 6f 64 73 20 66 6f 72 20 74 68 65 Methods for the
35e80 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 76 6f 69 module */. voi
35e90 64 20 2a 70 43 6c 69 65 6e 74 44 61 74 61 20 20 d *pClientData
35ea0 20 20 20 20 20 20 20 20 2f 2a 20 43 6c 69 65 6e /* Clien
35eb0 74 20 64 61 74 61 20 66 6f 72 20 78 43 72 65 61 t data for xCrea
35ec0 74 65 2f 78 43 6f 6e 6e 65 63 74 20 2a 2f 0a 29 te/xConnect */.)
35ed0 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
35ee0 46 3a 20 52 65 67 69 73 74 65 72 20 41 20 56 69 F: Register A Vi
35ef0 72 74 75 61 6c 20 54 61 62 6c 65 20 49 6d 70 6c rtual Table Impl
35f00 65 6d 65 6e 74 61 74 69 6f 6e 20 7b 48 31 38 32 ementation {H182
35f10 31 30 7d 20 3c 53 32 30 34 30 30 3e 0a 2a 2a 20 10} <S20400>.**
35f20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a EXPERIMENTAL.**.
35f30 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
35f40 69 73 20 69 64 65 6e 74 69 63 61 6c 20 74 6f 20 is identical to
35f50 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 the [sqlite3_cre
35f60 61 74 65 5f 6d 6f 64 75 6c 65 28 29 5d 20 6d 65 ate_module()] me
35f70 74 68 6f 64 2c 0a 2a 2a 20 65 78 63 65 70 74 20 thod,.** except
35f80 74 68 61 74 20 69 74 20 68 61 73 20 61 6e 20 65 that it has an e
35f90 78 74 72 61 20 70 61 72 61 6d 65 74 65 72 20 74 xtra parameter t
35fa0 6f 20 73 70 65 63 69 66 79 20 0a 2a 2a 20 61 20 o specify .** a
35fb0 64 65 73 74 72 75 63 74 6f 72 20 66 75 6e 63 74 destructor funct
35fc0 69 6f 6e 20 66 6f 72 20 74 68 65 20 63 6c 69 65 ion for the clie
35fd0 6e 74 20 64 61 74 61 20 70 6f 69 6e 74 65 72 2e nt data pointer.
35fe0 20 20 53 51 4c 69 74 65 20 77 69 6c 6c 0a 2a 2a SQLite will.**
35ff0 20 69 6e 76 6f 6b 65 20 74 68 65 20 64 65 73 74 invoke the dest
36000 72 75 63 74 6f 72 20 66 75 6e 63 74 69 6f 6e 20 ructor function
36010 28 69 66 20 69 74 20 69 73 20 6e 6f 74 20 4e 55 (if it is not NU
36020 4c 4c 29 20 77 68 65 6e 20 53 51 4c 69 74 65 0a LL) when SQLite.
36030 2a 2a 20 6e 6f 20 6c 6f 6e 67 65 72 20 6e 65 65 ** no longer nee
36040 64 73 20 74 68 65 20 70 43 6c 69 65 6e 74 44 61 ds the pClientDa
36050 74 61 20 70 6f 69 6e 74 65 72 2e 20 20 0a 2a 2f ta pointer. .*/
36060 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 .SQLITE_API SQLI
36070 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 TE_EXPERIMENTAL
36080 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 int sqlite3_crea
36090 74 65 5f 6d 6f 64 75 6c 65 5f 76 32 28 0a 20 20 te_module_v2(.
360a0 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 sqlite3 *db,
360b0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 51 /* SQ
360c0 4c 69 74 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 Lite connection
360d0 74 6f 20 72 65 67 69 73 74 65 72 20 6d 6f 64 75 to register modu
360e0 6c 65 20 77 69 74 68 20 2a 2f 0a 20 20 63 6f 6e le with */. con
360f0 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 st char *zName,
36100 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 /* Name
36110 6f 66 20 74 68 65 20 6d 6f 64 75 6c 65 20 2a 2f of the module */
36120 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 . const sqlite3
36130 5f 6d 6f 64 75 6c 65 20 2a 70 2c 20 20 20 2f 2a _module *p, /*
36140 20 4d 65 74 68 6f 64 73 20 66 6f 72 20 74 68 65 Methods for the
36150 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 76 6f 69 module */. voi
36160 64 20 2a 70 43 6c 69 65 6e 74 44 61 74 61 2c 20 d *pClientData,
36170 20 20 20 20 20 20 20 20 2f 2a 20 43 6c 69 65 6e /* Clien
36180 74 20 64 61 74 61 20 66 6f 72 20 78 43 72 65 61 t data for xCrea
36190 74 65 2f 78 43 6f 6e 6e 65 63 74 20 2a 2f 0a 20 te/xConnect */.
361a0 20 76 6f 69 64 28 2a 78 44 65 73 74 72 6f 79 29 void(*xDestroy)
361b0 28 76 6f 69 64 2a 29 20 20 20 20 20 2f 2a 20 4d (void*) /* M
361c0 6f 64 75 6c 65 20 64 65 73 74 72 75 63 74 6f 72 odule destructor
361d0 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29 3b 0a function */.);.
361e0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
361f0 20 56 69 72 74 75 61 6c 20 54 61 62 6c 65 20 49 Virtual Table I
36200 6e 73 74 61 6e 63 65 20 4f 62 6a 65 63 74 20 7b nstance Object {
36210 48 31 38 30 31 30 7d 20 3c 53 32 30 34 30 30 3e H18010} <S20400>
36220 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 73 71 .** KEYWORDS: sq
36230 6c 69 74 65 33 5f 76 74 61 62 0a 2a 2a 20 45 58 lite3_vtab.** EX
36240 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a PERIMENTAL.**.**
36250 20 45 76 65 72 79 20 5b 76 69 72 74 75 61 6c 20 Every [virtual
36260 74 61 62 6c 65 20 6d 6f 64 75 6c 65 5d 20 69 6d table module] im
36270 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 75 73 65 plementation use
36280 73 20 61 20 73 75 62 63 6c 61 73 73 0a 2a 2a 20 s a subclass.**
36290 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 of the following
362a0 20 73 74 72 75 63 74 75 72 65 20 74 6f 20 64 65 structure to de
362b0 73 63 72 69 62 65 20 61 20 70 61 72 74 69 63 75 scribe a particu
362c0 6c 61 72 20 69 6e 73 74 61 6e 63 65 0a 2a 2a 20 lar instance.**
362d0 6f 66 20 74 68 65 20 5b 76 69 72 74 75 61 6c 20 of the [virtual
362e0 74 61 62 6c 65 5d 2e 20 20 45 61 63 68 20 73 75 table]. Each su
362f0 62 63 6c 61 73 73 20 77 69 6c 6c 0a 2a 2a 20 62 bclass will.** b
36300 65 20 74 61 69 6c 6f 72 65 64 20 74 6f 20 74 68 e tailored to th
36310 65 20 73 70 65 63 69 66 69 63 20 6e 65 65 64 73 e specific needs
36320 20 6f 66 20 74 68 65 20 6d 6f 64 75 6c 65 20 69 of the module i
36330 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 0a 2a mplementation..*
36340 2a 20 54 68 65 20 70 75 72 70 6f 73 65 20 6f 66 * The purpose of
36350 20 74 68 69 73 20 73 75 70 65 72 63 6c 61 73 73 this superclass
36360 20 69 73 20 74 6f 20 64 65 66 69 6e 65 20 63 65 is to define ce
36370 72 74 61 69 6e 20 66 69 65 6c 64 73 20 74 68 61 rtain fields tha
36380 74 20 61 72 65 0a 2a 2a 20 63 6f 6d 6d 6f 6e 20 t are.** common
36390 74 6f 20 61 6c 6c 20 6d 6f 64 75 6c 65 20 69 6d to all module im
363a0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a plementations..*
363b0 2a 0a 2a 2a 20 56 69 72 74 75 61 6c 20 74 61 62 *.** Virtual tab
363c0 6c 65 73 20 6d 65 74 68 6f 64 73 20 63 61 6e 20 les methods can
363d0 73 65 74 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 set an error mes
363e0 73 61 67 65 20 62 79 20 61 73 73 69 67 6e 69 6e sage by assignin
363f0 67 20 61 0a 2a 2a 20 73 74 72 69 6e 67 20 6f 62 g a.** string ob
36400 74 61 69 6e 65 64 20 66 72 6f 6d 20 5b 73 71 6c tained from [sql
36410 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 29 5d 20 ite3_mprintf()]
36420 74 6f 20 7a 45 72 72 4d 73 67 2e 20 20 54 68 65 to zErrMsg. The
36430 20 6d 65 74 68 6f 64 20 73 68 6f 75 6c 64 0a 2a method should.*
36440 2a 20 74 61 6b 65 20 63 61 72 65 20 74 68 61 74 * take care that
36450 20 61 6e 79 20 70 72 69 6f 72 20 73 74 72 69 6e any prior strin
36460 67 20 69 73 20 66 72 65 65 64 20 62 79 20 61 20 g is freed by a
36470 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 call to [sqlite3
36480 5f 66 72 65 65 28 29 5d 0a 2a 2a 20 70 72 69 6f _free()].** prio
36490 72 20 74 6f 20 61 73 73 69 67 6e 69 6e 67 20 61 r to assigning a
364a0 20 6e 65 77 20 73 74 72 69 6e 67 20 74 6f 20 7a new string to z
364b0 45 72 72 4d 73 67 2e 20 20 41 66 74 65 72 20 74 ErrMsg. After t
364c0 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 he error message
364d0 0a 2a 2a 20 69 73 20 64 65 6c 69 76 65 72 65 64 .** is delivered
364e0 20 75 70 20 74 6f 20 74 68 65 20 63 6c 69 65 6e up to the clien
364f0 74 20 61 70 70 6c 69 63 61 74 69 6f 6e 2c 20 74 t application, t
36500 68 65 20 73 74 72 69 6e 67 20 77 69 6c 6c 20 62 he string will b
36510 65 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 0a e automatically.
36520 2a 2a 20 66 72 65 65 64 20 62 79 20 73 71 6c 69 ** freed by sqli
36530 74 65 33 5f 66 72 65 65 28 29 20 61 6e 64 20 74 te3_free() and t
36540 68 65 20 7a 45 72 72 4d 73 67 20 66 69 65 6c 64 he zErrMsg field
36550 20 77 69 6c 6c 20 62 65 20 7a 65 72 6f 65 64 2e will be zeroed.
36560 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74 .*/.struct sqlit
36570 65 33 5f 76 74 61 62 20 7b 0a 20 20 63 6f 6e 73 e3_vtab {. cons
36580 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 t sqlite3_module
36590 20 2a 70 4d 6f 64 75 6c 65 3b 20 20 2f 2a 20 54 *pModule; /* T
365a0 68 65 20 6d 6f 64 75 6c 65 20 66 6f 72 20 74 68 he module for th
365b0 69 73 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 is virtual table
365c0 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 */. int nRef;
365d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
365e0 20 20 20 20 20 20 2f 2a 20 4e 4f 20 4c 4f 4e 47 /* NO LONG
365f0 45 52 20 55 53 45 44 20 2a 2f 0a 20 20 63 68 61 ER USED */. cha
36600 72 20 2a 7a 45 72 72 4d 73 67 3b 20 20 20 20 20 r *zErrMsg;
36610 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
36620 45 72 72 6f 72 20 6d 65 73 73 61 67 65 20 66 72 Error message fr
36630 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e om sqlite3_mprin
36640 74 66 28 29 20 2a 2f 0a 20 20 2f 2a 20 56 69 72 tf() */. /* Vir
36650 74 75 61 6c 20 74 61 62 6c 65 20 69 6d 70 6c 65 tual table imple
36660 6d 65 6e 74 61 74 69 6f 6e 73 20 77 69 6c 6c 20 mentations will
36670 74 79 70 69 63 61 6c 6c 79 20 61 64 64 20 61 64 typically add ad
36680 64 69 74 69 6f 6e 61 6c 20 66 69 65 6c 64 73 20 ditional fields
36690 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 */.};../*.** CAP
366a0 49 33 52 45 46 3a 20 56 69 72 74 75 61 6c 20 54 I3REF: Virtual T
366b0 61 62 6c 65 20 43 75 72 73 6f 72 20 4f 62 6a 65 able Cursor Obje
366c0 63 74 20 20 7b 48 31 38 30 32 30 7d 20 3c 53 32 ct {H18020} <S2
366d0 30 34 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 0400>.** KEYWORD
366e0 53 3a 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f S: sqlite3_vtab_
366f0 63 75 72 73 6f 72 20 7b 76 69 72 74 75 61 6c 20 cursor {virtual
36700 74 61 62 6c 65 20 63 75 72 73 6f 72 7d 0a 2a 2a table cursor}.**
36710 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a EXPERIMENTAL.**
36720 0a 2a 2a 20 45 76 65 72 79 20 5b 76 69 72 74 75 .** Every [virtu
36730 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 5d al table module]
36740 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
36750 75 73 65 73 20 61 20 73 75 62 63 6c 61 73 73 20 uses a subclass
36760 6f 66 20 74 68 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 of the.** follow
36770 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 74 6f ing structure to
36780 20 64 65 73 63 72 69 62 65 20 63 75 72 73 6f 72 describe cursor
36790 73 20 74 68 61 74 20 70 6f 69 6e 74 20 69 6e 74 s that point int
367a0 6f 20 74 68 65 0a 2a 2a 20 5b 76 69 72 74 75 61 o the.** [virtua
367b0 6c 20 74 61 62 6c 65 5d 20 61 6e 64 20 61 72 65 l table] and are
367c0 20 75 73 65 64 0a 2a 2a 20 74 6f 20 6c 6f 6f 70 used.** to loop
367d0 20 74 68 72 6f 75 67 68 20 74 68 65 20 76 69 72 through the vir
367e0 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 43 75 72 tual table. Cur
367f0 73 6f 72 73 20 61 72 65 20 63 72 65 61 74 65 64 sors are created
36800 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 5b 73 using the.** [s
36810 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 2e 78 4f qlite3_module.xO
36820 70 65 6e 20 7c 20 78 4f 70 65 6e 5d 20 6d 65 74 pen | xOpen] met
36830 68 6f 64 20 6f 66 20 74 68 65 20 6d 6f 64 75 6c hod of the modul
36840 65 20 61 6e 64 20 61 72 65 20 64 65 73 74 72 6f e and are destro
36850 79 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 5b 73 yed.** by the [s
36860 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 2e 78 43 qlite3_module.xC
36870 6c 6f 73 65 20 7c 20 78 43 6c 6f 73 65 5d 20 6d lose | xClose] m
36880 65 74 68 6f 64 2e 20 20 43 75 73 73 6f 72 73 20 ethod. Cussors
36890 61 72 65 20 75 73 65 64 0a 2a 2a 20 62 79 20 74 are used.** by t
368a0 68 65 20 5b 78 46 69 6c 74 65 72 5d 2c 20 5b 78 he [xFilter], [x
368b0 4e 65 78 74 5d 2c 20 5b 78 45 6f 66 5d 2c 20 5b Next], [xEof], [
368c0 78 43 6f 6c 75 6d 6e 5d 2c 20 61 6e 64 20 5b 78 xColumn], and [x
368d0 52 6f 77 69 64 5d 20 6d 65 74 68 6f 64 73 0a 2a Rowid] methods.*
368e0 2a 20 6f 66 20 74 68 65 20 6d 6f 64 75 6c 65 2e * of the module.
368f0 20 20 45 61 63 68 20 6d 6f 64 75 6c 65 20 69 6d Each module im
36900 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 77 69 6c plementation wil
36910 6c 20 64 65 66 69 6e 65 0a 2a 2a 20 74 68 65 20 l define.** the
36920 63 6f 6e 74 65 6e 74 20 6f 66 20 61 20 63 75 72 content of a cur
36930 73 6f 72 20 73 74 72 75 63 74 75 72 65 20 74 6f sor structure to
36940 20 73 75 69 74 20 69 74 73 20 6f 77 6e 20 6e 65 suit its own ne
36950 65 64 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 eds..**.** This
36960 73 75 70 65 72 63 6c 61 73 73 20 65 78 69 73 74 superclass exist
36970 73 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 64 65 s in order to de
36980 66 69 6e 65 20 66 69 65 6c 64 73 20 6f 66 20 74 fine fields of t
36990 68 65 20 63 75 72 73 6f 72 20 74 68 61 74 0a 2a he cursor that.*
369a0 2a 20 61 72 65 20 63 6f 6d 6d 6f 6e 20 74 6f 20 * are common to
369b0 61 6c 6c 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 all implementati
369c0 6f 6e 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73 ons..*/.struct s
369d0 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 qlite3_vtab_curs
369e0 6f 72 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 or {. sqlite3_v
369f0 74 61 62 20 2a 70 56 74 61 62 3b 20 20 20 20 20 tab *pVtab;
36a00 20 2f 2a 20 56 69 72 74 75 61 6c 20 74 61 62 6c /* Virtual tabl
36a10 65 20 6f 66 20 74 68 69 73 20 63 75 72 73 6f 72 e of this cursor
36a20 20 2a 2f 0a 20 20 2f 2a 20 56 69 72 74 75 61 6c */. /* Virtual
36a30 20 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 table implement
36a40 61 74 69 6f 6e 73 20 77 69 6c 6c 20 74 79 70 69 ations will typi
36a50 63 61 6c 6c 79 20 61 64 64 20 61 64 64 69 74 69 cally add additi
36a60 6f 6e 61 6c 20 66 69 65 6c 64 73 20 2a 2f 0a 7d onal fields */.}
36a70 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
36a80 46 3a 20 44 65 63 6c 61 72 65 20 54 68 65 20 53 F: Declare The S
36a90 63 68 65 6d 61 20 4f 66 20 41 20 56 69 72 74 75 chema Of A Virtu
36aa0 61 6c 20 54 61 62 6c 65 20 7b 48 31 38 32 38 30 al Table {H18280
36ab0 7d 20 3c 53 32 30 34 30 30 3e 0a 2a 2a 20 45 58 } <S20400>.** EX
36ac0 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a PERIMENTAL.**.**
36ad0 20 54 68 65 20 5b 78 43 72 65 61 74 65 5d 20 61 The [xCreate] a
36ae0 6e 64 20 5b 78 43 6f 6e 6e 65 63 74 5d 20 6d 65 nd [xConnect] me
36af0 74 68 6f 64 73 20 6f 66 20 61 0a 2a 2a 20 5b 76 thods of a.** [v
36b00 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 irtual table mod
36b10 75 6c 65 5d 20 63 61 6c 6c 20 74 68 69 73 20 69 ule] call this i
36b20 6e 74 65 72 66 61 63 65 0a 2a 2a 20 74 6f 20 64 nterface.** to d
36b30 65 63 6c 61 72 65 20 74 68 65 20 66 6f 72 6d 61 eclare the forma
36b40 74 20 28 74 68 65 20 6e 61 6d 65 73 20 61 6e 64 t (the names and
36b50 20 64 61 74 61 74 79 70 65 73 20 6f 66 20 74 68 datatypes of th
36b60 65 20 63 6f 6c 75 6d 6e 73 29 20 6f 66 0a 2a 2a e columns) of.**
36b70 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 the virtual tab
36b80 6c 65 73 20 74 68 65 79 20 69 6d 70 6c 65 6d 65 les they impleme
36b90 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 nt..*/.SQLITE_AP
36ba0 49 20 53 51 4c 49 54 45 5f 45 58 50 45 52 49 4d I SQLITE_EXPERIM
36bb0 45 4e 54 41 4c 20 69 6e 74 20 73 71 6c 69 74 65 ENTAL int sqlite
36bc0 33 5f 64 65 63 6c 61 72 65 5f 76 74 61 62 28 73 3_declare_vtab(s
36bd0 71 6c 69 74 65 33 2a 2c 20 63 6f 6e 73 74 20 63 qlite3*, const c
36be0 68 61 72 20 2a 7a 53 51 4c 29 3b 0a 0a 2f 2a 0a har *zSQL);../*.
36bf0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4f 76 65 ** CAPI3REF: Ove
36c00 72 6c 6f 61 64 20 41 20 46 75 6e 63 74 69 6f 6e rload A Function
36c10 20 46 6f 72 20 41 20 56 69 72 74 75 61 6c 20 54 For A Virtual T
36c20 61 62 6c 65 20 7b 48 31 38 33 30 30 7d 20 3c 53 able {H18300} <S
36c30 32 30 34 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 20400>.** EXPERI
36c40 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 56 69 72 MENTAL.**.** Vir
36c50 74 75 61 6c 20 74 61 62 6c 65 73 20 63 61 6e 20 tual tables can
36c60 70 72 6f 76 69 64 65 20 61 6c 74 65 72 6e 61 74 provide alternat
36c70 69 76 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 ive implementati
36c80 6f 6e 73 20 6f 66 20 66 75 6e 63 74 69 6f 6e 73 ons of functions
36c90 0a 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 5b 78 .** using the [x
36ca0 46 69 6e 64 46 75 6e 63 74 69 6f 6e 5d 20 6d 65 FindFunction] me
36cb0 74 68 6f 64 20 6f 66 20 74 68 65 20 5b 76 69 72 thod of the [vir
36cc0 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c tual table modul
36cd0 65 5d 2e 20 20 0a 2a 2a 20 42 75 74 20 67 6c 6f e]. .** But glo
36ce0 62 61 6c 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 bal versions of
36cf0 74 68 6f 73 65 20 66 75 6e 63 74 69 6f 6e 73 0a those functions.
36d00 2a 2a 20 6d 75 73 74 20 65 78 69 73 74 20 69 6e ** must exist in
36d10 20 6f 72 64 65 72 20 74 6f 20 62 65 20 6f 76 65 order to be ove
36d20 72 6c 6f 61 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 rloaded..**.** T
36d30 68 69 73 20 41 50 49 20 6d 61 6b 65 73 20 73 75 his API makes su
36d40 72 65 20 61 20 67 6c 6f 62 61 6c 20 76 65 72 73 re a global vers
36d50 69 6f 6e 20 6f 66 20 61 20 66 75 6e 63 74 69 6f ion of a functio
36d60 6e 20 77 69 74 68 20 61 20 70 61 72 74 69 63 75 n with a particu
36d70 6c 61 72 0a 2a 2a 20 6e 61 6d 65 20 61 6e 64 20 lar.** name and
36d80 6e 75 6d 62 65 72 20 6f 66 20 70 61 72 61 6d 65 number of parame
36d90 74 65 72 73 20 65 78 69 73 74 73 2e 20 20 49 66 ters exists. If
36da0 20 6e 6f 20 73 75 63 68 20 66 75 6e 63 74 69 6f no such functio
36db0 6e 20 65 78 69 73 74 73 0a 2a 2a 20 62 65 66 6f n exists.** befo
36dc0 72 65 20 74 68 69 73 20 41 50 49 20 69 73 20 63 re this API is c
36dd0 61 6c 6c 65 64 2c 20 61 20 6e 65 77 20 66 75 6e alled, a new fun
36de0 63 74 69 6f 6e 20 69 73 20 63 72 65 61 74 65 64 ction is created
36df0 2e 20 20 54 68 65 20 69 6d 70 6c 65 6d 65 6e 74 . The implement
36e00 61 74 69 6f 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 ation.** of the
36e10 6e 65 77 20 66 75 6e 63 74 69 6f 6e 20 61 6c 77 new function alw
36e20 61 79 73 20 63 61 75 73 65 73 20 61 6e 20 65 78 ays causes an ex
36e30 63 65 70 74 69 6f 6e 20 74 6f 20 62 65 20 74 68 ception to be th
36e40 72 6f 77 6e 2e 20 20 53 6f 0a 2a 2a 20 74 68 65 rown. So.** the
36e50 20 6e 65 77 20 66 75 6e 63 74 69 6f 6e 20 69 73 new function is
36e60 20 6e 6f 74 20 67 6f 6f 64 20 66 6f 72 20 61 6e not good for an
36e70 79 74 68 69 6e 67 20 62 79 20 69 74 73 65 6c 66 ything by itself
36e80 2e 20 20 49 74 73 20 6f 6e 6c 79 0a 2a 2a 20 70 . Its only.** p
36e90 75 72 70 6f 73 65 20 69 73 20 74 6f 20 62 65 20 urpose is to be
36ea0 61 20 70 6c 61 63 65 68 6f 6c 64 65 72 20 66 75 a placeholder fu
36eb0 6e 63 74 69 6f 6e 20 74 68 61 74 20 63 61 6e 20 nction that can
36ec0 62 65 20 6f 76 65 72 6c 6f 61 64 65 64 0a 2a 2a be overloaded.**
36ed0 20 62 79 20 61 20 5b 76 69 72 74 75 61 6c 20 74 by a [virtual t
36ee0 61 62 6c 65 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 able]..*/.SQLITE
36ef0 5f 41 50 49 20 53 51 4c 49 54 45 5f 45 58 50 45 _API SQLITE_EXPE
36f00 52 49 4d 45 4e 54 41 4c 20 69 6e 74 20 73 71 6c RIMENTAL int sql
36f10 69 74 65 33 5f 6f 76 65 72 6c 6f 61 64 5f 66 75 ite3_overload_fu
36f20 6e 63 74 69 6f 6e 28 73 71 6c 69 74 65 33 2a 2c nction(sqlite3*,
36f30 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 75 const char *zFu
36f40 6e 63 4e 61 6d 65 2c 20 69 6e 74 20 6e 41 72 67 ncName, int nArg
36f50 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 69 6e );../*.** The in
36f60 74 65 72 66 61 63 65 20 74 6f 20 74 68 65 20 76 terface to the v
36f70 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 6d 65 63 irtual-table mec
36f80 68 61 6e 69 73 6d 20 64 65 66 69 6e 65 64 20 61 hanism defined a
36f90 62 6f 76 65 20 28 62 61 63 6b 20 75 70 0a 2a 2a bove (back up.**
36fa0 20 74 6f 20 61 20 63 6f 6d 6d 65 6e 74 20 72 65 to a comment re
36fb0 6d 61 72 6b 61 62 6c 79 20 73 69 6d 69 6c 61 72 markably similar
36fc0 20 74 6f 20 74 68 69 73 20 6f 6e 65 29 20 69 73 to this one) is
36fd0 20 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e 73 69 currently consi
36fe0 64 65 72 65 64 0a 2a 2a 20 74 6f 20 62 65 20 65 dered.** to be e
36ff0 78 70 65 72 69 6d 65 6e 74 61 6c 2e 20 20 54 68 xperimental. Th
37000 65 20 69 6e 74 65 72 66 61 63 65 20 6d 69 67 68 e interface migh
37010 74 20 63 68 61 6e 67 65 20 69 6e 20 69 6e 63 6f t change in inco
37020 6d 70 61 74 69 62 6c 65 20 77 61 79 73 2e 0a 2a mpatible ways..*
37030 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 20 70 * If this is a p
37040 72 6f 62 6c 65 6d 20 66 6f 72 20 79 6f 75 2c 20 roblem for you,
37050 64 6f 20 6e 6f 74 20 75 73 65 20 74 68 65 20 69 do not use the i
37060 6e 74 65 72 66 61 63 65 20 61 74 20 74 68 69 73 nterface at this
37070 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 time..**.** Whe
37080 6e 20 74 68 65 20 76 69 72 74 75 61 6c 2d 74 61 n the virtual-ta
37090 62 6c 65 20 6d 65 63 68 61 6e 69 73 6d 20 73 74 ble mechanism st
370a0 61 62 69 6c 69 7a 65 73 2c 20 77 65 20 77 69 6c abilizes, we wil
370b0 6c 20 64 65 63 6c 61 72 65 20 74 68 65 0a 2a 2a l declare the.**
370c0 20 69 6e 74 65 72 66 61 63 65 20 66 69 78 65 64 interface fixed
370d0 2c 20 73 75 70 70 6f 72 74 20 69 74 20 69 6e 64 , support it ind
370e0 65 66 69 6e 69 74 65 6c 79 2c 20 61 6e 64 20 72 efinitely, and r
370f0 65 6d 6f 76 65 20 74 68 69 73 20 63 6f 6d 6d 65 emove this comme
37100 6e 74 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 20 45 58 nt..**.****** EX
37110 50 45 52 49 4d 45 4e 54 41 4c 20 2d 20 73 75 62 PERIMENTAL - sub
37120 6a 65 63 74 20 74 6f 20 63 68 61 6e 67 65 20 77 ject to change w
37130 69 74 68 6f 75 74 20 6e 6f 74 69 63 65 20 2a 2a ithout notice **
37140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2f 0a ************.*/.
37150 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
37160 20 41 20 48 61 6e 64 6c 65 20 54 6f 20 41 6e 20 A Handle To An
37170 4f 70 65 6e 20 42 4c 4f 42 20 7b 48 31 37 38 30 Open BLOB {H1780
37180 30 7d 20 3c 53 33 30 32 33 30 3e 0a 2a 2a 20 4b 0} <S30230>.** K
37190 45 59 57 4f 52 44 53 3a 20 7b 42 4c 4f 42 20 68 EYWORDS: {BLOB h
371a0 61 6e 64 6c 65 7d 20 7b 42 4c 4f 42 20 68 61 6e andle} {BLOB han
371b0 64 6c 65 73 7d 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 dles}.**.** An i
371c0 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 nstance of this
371d0 6f 62 6a 65 63 74 20 72 65 70 72 65 73 65 6e 74 object represent
371e0 73 20 61 6e 20 6f 70 65 6e 20 42 4c 4f 42 20 6f s an open BLOB o
371f0 6e 20 77 68 69 63 68 0a 2a 2a 20 5b 73 71 6c 69 n which.** [sqli
37200 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 20 7c 20 te3_blob_open |
37210 69 6e 63 72 65 6d 65 6e 74 61 6c 20 42 4c 4f 42 incremental BLOB
37220 20 49 2f 4f 5d 20 63 61 6e 20 62 65 20 70 65 72 I/O] can be per
37230 66 6f 72 6d 65 64 2e 0a 2a 2a 20 4f 62 6a 65 63 formed..** Objec
37240 74 73 20 6f 66 20 74 68 69 73 20 74 79 70 65 20 ts of this type
37250 61 72 65 20 63 72 65 61 74 65 64 20 62 79 20 5b are created by [
37260 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 sqlite3_blob_ope
37270 6e 28 29 5d 0a 2a 2a 20 61 6e 64 20 64 65 73 74 n()].** and dest
37280 72 6f 79 65 64 20 62 79 20 5b 73 71 6c 69 74 65 royed by [sqlite
37290 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28 29 5d 2e 3_blob_close()].
372a0 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74 65 33 .** The [sqlite3
372b0 5f 62 6c 6f 62 5f 72 65 61 64 28 29 5d 20 61 6e _blob_read()] an
372c0 64 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f d [sqlite3_blob_
372d0 77 72 69 74 65 28 29 5d 20 69 6e 74 65 72 66 61 write()] interfa
372e0 63 65 73 0a 2a 2a 20 63 61 6e 20 62 65 20 75 73 ces.** can be us
372f0 65 64 20 74 6f 20 72 65 61 64 20 6f 72 20 77 72 ed to read or wr
37300 69 74 65 20 73 6d 61 6c 6c 20 73 75 62 73 65 63 ite small subsec
37310 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 42 4c 4f tions of the BLO
37320 42 2e 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74 B..** The [sqlit
37330 65 33 5f 62 6c 6f 62 5f 62 79 74 65 73 28 29 5d e3_blob_bytes()]
37340 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 interface retur
37350 6e 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 ns the size of t
37360 68 65 20 42 4c 4f 42 20 69 6e 20 62 79 74 65 73 he BLOB in bytes
37370 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 ..*/.typedef str
37380 75 63 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 uct sqlite3_blob
37390 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 3b 0a 0a sqlite3_blob;..
373a0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
373b0 4f 70 65 6e 20 41 20 42 4c 4f 42 20 46 6f 72 20 Open A BLOB For
373c0 49 6e 63 72 65 6d 65 6e 74 61 6c 20 49 2f 4f 20 Incremental I/O
373d0 7b 48 31 37 38 31 30 7d 20 3c 53 33 30 32 33 30 {H17810} <S30230
373e0 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 >.**.** This int
373f0 65 72 66 61 63 65 73 20 6f 70 65 6e 73 20 61 20 erfaces opens a
37400 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 20 7c 20 68 [BLOB handle | h
37410 61 6e 64 6c 65 5d 20 74 6f 20 74 68 65 20 42 4c andle] to the BL
37420 4f 42 20 6c 6f 63 61 74 65 64 0a 2a 2a 20 69 6e OB located.** in
37430 20 72 6f 77 20 69 52 6f 77 2c 20 63 6f 6c 75 6d row iRow, colum
37440 6e 20 7a 43 6f 6c 75 6d 6e 2c 20 74 61 62 6c 65 n zColumn, table
37450 20 7a 54 61 62 6c 65 20 69 6e 20 64 61 74 61 62 zTable in datab
37460 61 73 65 20 7a 44 62 3b 0a 2a 2a 20 69 6e 20 6f ase zDb;.** in o
37470 74 68 65 72 20 77 6f 72 64 73 2c 20 74 68 65 20 ther words, the
37480 73 61 6d 65 20 42 4c 4f 42 20 74 68 61 74 20 77 same BLOB that w
37490 6f 75 6c 64 20 62 65 20 73 65 6c 65 63 74 65 64 ould be selected
374a0 20 62 79 3a 0a 2a 2a 0a 2a 2a 20 3c 70 72 65 3e by:.**.** <pre>
374b0 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20 7a .** SELECT z
374c0 43 6f 6c 75 6d 6e 20 46 52 4f 4d 20 7a 44 62 2e Column FROM zDb.
374d0 7a 54 61 62 6c 65 20 57 48 45 52 45 20 5b 72 6f zTable WHERE [ro
374e0 77 69 64 5d 20 3d 20 69 52 6f 77 3b 0a 2a 2a 20 wid] = iRow;.**
374f0 3c 2f 70 72 65 3e 20 7b 45 4e 44 7d 0a 2a 2a 0a </pre> {END}.**.
37500 2a 2a 20 49 66 20 74 68 65 20 66 6c 61 67 73 20 ** If the flags
37510 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e 6f 6e parameter is non
37520 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 -zero, then the
37530 42 4c 4f 42 20 69 73 20 6f 70 65 6e 65 64 20 66 BLOB is opened f
37540 6f 72 20 72 65 61 64 0a 2a 2a 20 61 6e 64 20 77 or read.** and w
37550 72 69 74 65 20 61 63 63 65 73 73 2e 20 49 66 20 rite access. If
37560 69 74 20 69 73 20 7a 65 72 6f 2c 20 74 68 65 20 it is zero, the
37570 42 4c 4f 42 20 69 73 20 6f 70 65 6e 65 64 20 66 BLOB is opened f
37580 6f 72 20 72 65 61 64 20 61 63 63 65 73 73 2e 0a or read access..
37590 2a 2a 20 49 74 20 69 73 20 6e 6f 74 20 70 6f 73 ** It is not pos
375a0 73 69 62 6c 65 20 74 6f 20 6f 70 65 6e 20 61 20 sible to open a
375b0 63 6f 6c 75 6d 6e 20 74 68 61 74 20 69 73 20 70 column that is p
375c0 61 72 74 20 6f 66 20 61 6e 20 69 6e 64 65 78 20 art of an index
375d0 6f 72 20 70 72 69 6d 61 72 79 20 0a 2a 2a 20 6b or primary .** k
375e0 65 79 20 66 6f 72 20 77 72 69 74 69 6e 67 2e 20 ey for writing.
375f0 5e 49 66 20 5b 66 6f 72 65 69 67 6e 20 6b 65 79 ^If [foreign key
37600 20 63 6f 6e 73 74 72 61 69 6e 74 73 5d 20 61 72 constraints] ar
37610 65 20 65 6e 61 62 6c 65 64 2c 20 69 74 20 69 73 e enabled, it is
37620 20 0a 2a 2a 20 6e 6f 74 20 70 6f 73 73 69 62 6c .** not possibl
37630 65 20 74 6f 20 6f 70 65 6e 20 61 20 63 6f 6c 75 e to open a colu
37640 6d 6e 20 74 68 61 74 20 69 73 20 70 61 72 74 20 mn that is part
37650 6f 66 20 61 20 5b 63 68 69 6c 64 20 6b 65 79 5d of a [child key]
37660 20 66 6f 72 20 77 72 69 74 69 6e 67 2e 0a 2a 2a for writing..**
37670 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68 .** Note that th
37680 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 e database name
37690 69 73 20 6e 6f 74 20 74 68 65 20 66 69 6c 65 6e is not the filen
376a0 61 6d 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e ame that contain
376b0 73 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 s.** the databas
376c0 65 20 62 75 74 20 72 61 74 68 65 72 20 74 68 65 e but rather the
376d0 20 73 79 6d 62 6f 6c 69 63 20 6e 61 6d 65 20 6f symbolic name o
376e0 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74 f the database t
376f0 68 61 74 0a 2a 2a 20 69 73 20 61 73 73 69 67 6e hat.** is assign
37700 65 64 20 77 68 65 6e 20 74 68 65 20 64 61 74 61 ed when the data
37710 62 61 73 65 20 69 73 20 63 6f 6e 6e 65 63 74 65 base is connecte
37720 64 20 75 73 69 6e 67 20 5b 41 54 54 41 43 48 5d d using [ATTACH]
37730 2e 0a 2a 2a 20 46 6f 72 20 74 68 65 20 6d 61 69 ..** For the mai
37740 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c n database file,
37750 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6e 61 the database na
37760 6d 65 20 69 73 20 22 6d 61 69 6e 22 2e 0a 2a 2a me is "main"..**
37770 20 46 6f 72 20 54 45 4d 50 20 74 61 62 6c 65 73 For TEMP tables
37780 2c 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6e , the database n
37790 61 6d 65 20 69 73 20 22 74 65 6d 70 22 2e 0a 2a ame is "temp"..*
377a0 2a 0a 2a 2a 20 4f 6e 20 73 75 63 63 65 73 73 2c *.** On success,
377b0 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 69 73 20 [SQLITE_OK] is
377c0 72 65 74 75 72 6e 65 64 20 61 6e 64 20 74 68 65 returned and the
377d0 20 6e 65 77 20 5b 42 4c 4f 42 20 68 61 6e 64 6c new [BLOB handl
377e0 65 5d 20 69 73 20 77 72 69 74 74 65 6e 0a 2a 2a e] is written.**
377f0 20 74 6f 20 2a 70 70 42 6c 6f 62 2e 20 4f 74 68 to *ppBlob. Oth
37800 65 72 77 69 73 65 20 61 6e 20 5b 65 72 72 6f 72 erwise an [error
37810 20 63 6f 64 65 5d 20 69 73 20 72 65 74 75 72 6e code] is return
37820 65 64 20 61 6e 64 20 2a 70 70 42 6c 6f 62 20 69 ed and *ppBlob i
37830 73 20 73 65 74 0a 2a 2a 20 74 6f 20 62 65 20 61 s set.** to be a
37840 20 6e 75 6c 6c 20 70 6f 69 6e 74 65 72 2e 0a 2a null pointer..*
37850 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
37860 73 65 74 73 20 74 68 65 20 5b 64 61 74 61 62 61 sets the [databa
37870 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 65 se connection] e
37880 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 20 6d 65 rror code and me
37890 73 73 61 67 65 0a 2a 2a 20 61 63 63 65 73 73 69 ssage.** accessi
378a0 62 6c 65 20 76 69 61 20 5b 73 71 6c 69 74 65 33 ble via [sqlite3
378b0 5f 65 72 72 63 6f 64 65 28 29 5d 20 61 6e 64 20 _errcode()] and
378c0 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 [sqlite3_errmsg(
378d0 29 5d 20 61 6e 64 20 72 65 6c 61 74 65 64 0a 2a )] and related.*
378e0 2a 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 4e 6f * functions. No
378f0 74 65 20 74 68 61 74 20 74 68 65 20 2a 70 70 42 te that the *ppB
37900 6c 6f 62 20 76 61 72 69 61 62 6c 65 20 69 73 20 lob variable is
37910 61 6c 77 61 79 73 20 69 6e 69 74 69 61 6c 69 7a always initializ
37920 65 64 20 69 6e 20 61 0a 2a 2a 20 77 61 79 20 74 ed in a.** way t
37930 68 61 74 20 6d 61 6b 65 73 20 69 74 20 73 61 66 hat makes it saf
37940 65 20 74 6f 20 69 6e 76 6f 6b 65 20 5b 73 71 6c e to invoke [sql
37950 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28 ite3_blob_close(
37960 29 5d 20 6f 6e 20 2a 70 70 42 6c 6f 62 0a 2a 2a )] on *ppBlob.**
37970 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 74 regardless of t
37980 68 65 20 73 75 63 63 65 73 73 20 6f 72 20 66 61 he success or fa
37990 69 6c 75 72 65 20 6f 66 20 74 68 69 73 20 72 6f ilure of this ro
379a0 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 utine..**.** If
379b0 74 68 65 20 72 6f 77 20 74 68 61 74 20 61 20 42 the row that a B
379c0 4c 4f 42 20 68 61 6e 64 6c 65 20 70 6f 69 6e 74 LOB handle point
379d0 73 20 74 6f 20 69 73 20 6d 6f 64 69 66 69 65 64 s to is modified
379e0 20 62 79 20 61 6e 0a 2a 2a 20 5b 55 50 44 41 54 by an.** [UPDAT
379f0 45 5d 2c 20 5b 44 45 4c 45 54 45 5d 2c 20 6f 72 E], [DELETE], or
37a00 20 62 79 20 5b 4f 4e 20 43 4f 4e 46 4c 49 43 54 by [ON CONFLICT
37a10 5d 20 73 69 64 65 2d 65 66 66 65 63 74 73 0a 2a ] side-effects.*
37a20 2a 20 74 68 65 6e 20 74 68 65 20 42 4c 4f 42 20 * then the BLOB
37a30 68 61 6e 64 6c 65 20 69 73 20 6d 61 72 6b 65 64 handle is marked
37a40 20 61 73 20 22 65 78 70 69 72 65 64 22 2e 0a 2a as "expired"..*
37a50 2a 20 54 68 69 73 20 69 73 20 74 72 75 65 20 69 * This is true i
37a60 66 20 61 6e 79 20 63 6f 6c 75 6d 6e 20 6f 66 20 f any column of
37a70 74 68 65 20 72 6f 77 20 69 73 20 63 68 61 6e 67 the row is chang
37a80 65 64 2c 20 65 76 65 6e 20 61 20 63 6f 6c 75 6d ed, even a colum
37a90 6e 0a 2a 2a 20 6f 74 68 65 72 20 74 68 61 6e 20 n.** other than
37aa0 74 68 65 20 6f 6e 65 20 74 68 65 20 42 4c 4f 42 the one the BLOB
37ab0 20 68 61 6e 64 6c 65 20 69 73 20 6f 70 65 6e 20 handle is open
37ac0 6f 6e 2e 0a 2a 2a 20 43 61 6c 6c 73 20 74 6f 20 on..** Calls to
37ad0 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65 [sqlite3_blob_re
37ae0 61 64 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 ad()] and [sqlit
37af0 65 33 5f 62 6c 6f 62 5f 77 72 69 74 65 28 29 5d e3_blob_write()]
37b00 20 66 6f 72 0a 2a 2a 20 61 20 65 78 70 69 72 65 for.** a expire
37b10 64 20 42 4c 4f 42 20 68 61 6e 64 6c 65 20 66 61 d BLOB handle fa
37b20 69 6c 20 77 69 74 68 20 61 6e 20 72 65 74 75 72 il with an retur
37b30 6e 20 63 6f 64 65 20 6f 66 20 5b 53 51 4c 49 54 n code of [SQLIT
37b40 45 5f 41 42 4f 52 54 5d 2e 0a 2a 2a 20 43 68 61 E_ABORT]..** Cha
37b50 6e 67 65 73 20 77 72 69 74 74 65 6e 20 69 6e 74 nges written int
37b60 6f 20 61 20 42 4c 4f 42 20 70 72 69 6f 72 20 74 o a BLOB prior t
37b70 6f 20 74 68 65 20 42 4c 4f 42 20 65 78 70 69 72 o the BLOB expir
37b80 69 6e 67 20 61 72 65 20 6e 6f 74 0a 2a 2a 20 72 ing are not.** r
37b90 6f 6c 6c 62 61 63 6b 20 62 79 20 74 68 65 20 65 ollback by the e
37ba0 78 70 69 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 xpiration of the
37bb0 20 42 4c 4f 42 2e 20 20 53 75 63 68 20 63 68 61 BLOB. Such cha
37bc0 6e 67 65 73 20 77 69 6c 6c 20 65 76 65 6e 74 75 nges will eventu
37bd0 61 6c 6c 79 0a 2a 2a 20 63 6f 6d 6d 69 74 20 69 ally.** commit i
37be0 66 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f f the transactio
37bf0 6e 20 63 6f 6e 74 69 6e 75 65 73 20 74 6f 20 63 n continues to c
37c00 6f 6d 70 6c 65 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a ompletion..**.**
37c10 20 55 73 65 20 74 68 65 20 5b 73 71 6c 69 74 65 Use the [sqlite
37c20 33 5f 62 6c 6f 62 5f 62 79 74 65 73 28 29 5d 20 3_blob_bytes()]
37c30 69 6e 74 65 72 66 61 63 65 20 74 6f 20 64 65 74 interface to det
37c40 65 72 6d 69 6e 65 20 74 68 65 20 73 69 7a 65 20 ermine the size
37c50 6f 66 0a 2a 2a 20 74 68 65 20 6f 70 65 6e 65 64 of.** the opened
37c60 20 62 6c 6f 62 2e 20 20 54 68 65 20 73 69 7a 65 blob. The size
37c70 20 6f 66 20 61 20 62 6c 6f 62 20 6d 61 79 20 6e of a blob may n
37c80 6f 74 20 62 65 20 63 68 61 6e 67 65 64 20 62 79 ot be changed by
37c90 20 74 68 69 73 0a 2a 2a 20 69 6e 74 65 72 66 61 this.** interfa
37ca0 63 65 2e 20 20 55 73 65 20 74 68 65 20 5b 55 50 ce. Use the [UP
37cb0 44 41 54 45 5d 20 53 51 4c 20 63 6f 6d 6d 61 6e DATE] SQL comman
37cc0 64 20 74 6f 20 63 68 61 6e 67 65 20 74 68 65 20 d to change the
37cd0 73 69 7a 65 20 6f 66 20 61 0a 2a 2a 20 62 6c 6f size of a.** blo
37ce0 62 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 73 71 b..**.** The [sq
37cf0 6c 69 74 65 33 5f 62 69 6e 64 5f 7a 65 72 6f 62 lite3_bind_zerob
37d00 6c 6f 62 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 lob()] and [sqli
37d10 74 65 33 5f 72 65 73 75 6c 74 5f 7a 65 72 6f 62 te3_result_zerob
37d20 6c 6f 62 28 29 5d 20 69 6e 74 65 72 66 61 63 65 lob()] interface
37d30 73 0a 2a 2a 20 61 6e 64 20 74 68 65 20 62 75 69 s.** and the bui
37d40 6c 74 2d 69 6e 20 5b 7a 65 72 6f 62 6c 6f 62 5d lt-in [zeroblob]
37d50 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 63 61 SQL function ca
37d60 6e 20 62 65 20 75 73 65 64 2c 20 69 66 20 64 65 n be used, if de
37d70 73 69 72 65 64 2c 0a 2a 2a 20 74 6f 20 63 72 65 sired,.** to cre
37d80 61 74 65 20 61 6e 20 65 6d 70 74 79 2c 20 7a 65 ate an empty, ze
37d90 72 6f 2d 66 69 6c 6c 65 64 20 62 6c 6f 62 20 69 ro-filled blob i
37da0 6e 20 77 68 69 63 68 20 74 6f 20 72 65 61 64 20 n which to read
37db0 6f 72 20 77 72 69 74 65 20 75 73 69 6e 67 0a 2a or write using.*
37dc0 2a 20 74 68 69 73 20 69 6e 74 65 72 66 61 63 65 * this interface
37dd0 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 61 76 6f 69 64 ..**.** To avoid
37de0 20 61 20 72 65 73 6f 75 72 63 65 20 6c 65 61 6b a resource leak
37df0 2c 20 65 76 65 72 79 20 6f 70 65 6e 20 5b 42 4c , every open [BL
37e00 4f 42 20 68 61 6e 64 6c 65 5d 20 73 68 6f 75 6c OB handle] shoul
37e10 64 20 65 76 65 6e 74 75 61 6c 6c 79 0a 2a 2a 20 d eventually.**
37e20 62 65 20 72 65 6c 65 61 73 65 64 20 62 79 20 61 be released by a
37e30 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 call to [sqlite
37e40 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28 29 5d 2e 3_blob_close()].
37e50 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 .**.** Requireme
37e60 6e 74 73 3a 0a 2a 2a 20 5b 48 31 37 38 31 33 5d nts:.** [H17813]
37e70 20 5b 48 31 37 38 31 34 5d 20 5b 48 31 37 38 31 [H17814] [H1781
37e80 36 5d 20 5b 48 31 37 38 31 39 5d 20 5b 48 31 37 6] [H17819] [H17
37e90 38 32 31 5d 20 5b 48 31 37 38 32 34 5d 0a 2a 2f 821] [H17824].*/
37ea0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
37eb0 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 sqlite3_blob_ope
37ec0 6e 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c 0a 20 n(. sqlite3*,.
37ed0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 const char *zDb
37ee0 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a ,. const char *
37ef0 7a 54 61 62 6c 65 2c 0a 20 20 63 6f 6e 73 74 20 zTable,. const
37f00 63 68 61 72 20 2a 7a 43 6f 6c 75 6d 6e 2c 0a 20 char *zColumn,.
37f10 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 sqlite3_int64 i
37f20 52 6f 77 2c 0a 20 20 69 6e 74 20 66 6c 61 67 73 Row,. int flags
37f30 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 ,. sqlite3_blob
37f40 20 2a 2a 70 70 42 6c 6f 62 0a 29 3b 0a 0a 2f 2a **ppBlob.);../*
37f50 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6c .** CAPI3REF: Cl
37f60 6f 73 65 20 41 20 42 4c 4f 42 20 48 61 6e 64 6c ose A BLOB Handl
37f70 65 20 7b 48 31 37 38 33 30 7d 20 3c 53 33 30 32 e {H17830} <S302
37f80 33 30 3e 0a 2a 2a 0a 2a 2a 20 43 6c 6f 73 65 73 30>.**.** Closes
37f90 20 61 6e 20 6f 70 65 6e 20 5b 42 4c 4f 42 20 68 an open [BLOB h
37fa0 61 6e 64 6c 65 5d 2e 0a 2a 2a 0a 2a 2a 20 43 6c andle]..**.** Cl
37fb0 6f 73 69 6e 67 20 61 20 42 4c 4f 42 20 73 68 61 osing a BLOB sha
37fc0 6c 6c 20 63 61 75 73 65 20 74 68 65 20 63 75 72 ll cause the cur
37fd0 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e rent transaction
37fe0 20 74 6f 20 63 6f 6d 6d 69 74 0a 2a 2a 20 69 66 to commit.** if
37ff0 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 6f 74 there are no ot
38000 68 65 72 20 42 4c 4f 42 73 2c 20 6e 6f 20 70 65 her BLOBs, no pe
38010 6e 64 69 6e 67 20 70 72 65 70 61 72 65 64 20 73 nding prepared s
38020 74 61 74 65 6d 65 6e 74 73 2c 20 61 6e 64 20 74 tatements, and t
38030 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 he.** database c
38040 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 69 6e 20 onnection is in
38050 5b 61 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 [autocommit mode
38060 5d 2e 0a 2a 2a 20 49 66 20 61 6e 79 20 77 72 69 ]..** If any wri
38070 74 65 73 20 77 65 72 65 20 6d 61 64 65 20 74 6f tes were made to
38080 20 74 68 65 20 42 4c 4f 42 2c 20 74 68 65 79 20 the BLOB, they
38090 6d 69 67 68 74 20 62 65 20 68 65 6c 64 20 69 6e might be held in
380a0 20 63 61 63 68 65 0a 2a 2a 20 75 6e 74 69 6c 20 cache.** until
380b0 74 68 65 20 63 6c 6f 73 65 20 6f 70 65 72 61 74 the close operat
380c0 69 6f 6e 20 69 66 20 74 68 65 79 20 77 69 6c 6c ion if they will
380d0 20 66 69 74 2e 0a 2a 2a 0a 2a 2a 20 43 6c 6f 73 fit..**.** Clos
380e0 69 6e 67 20 74 68 65 20 42 4c 4f 42 20 6f 66 74 ing the BLOB oft
380f0 65 6e 20 66 6f 72 63 65 73 20 74 68 65 20 63 68 en forces the ch
38100 61 6e 67 65 73 0a 2a 2a 20 6f 75 74 20 74 6f 20 anges.** out to
38110 64 69 73 6b 20 61 6e 64 20 73 6f 20 69 66 20 61 disk and so if a
38120 6e 79 20 49 2f 4f 20 65 72 72 6f 72 73 20 6f 63 ny I/O errors oc
38130 63 75 72 2c 20 74 68 65 79 20 77 69 6c 6c 20 6c cur, they will l
38140 69 6b 65 6c 79 20 6f 63 63 75 72 0a 2a 2a 20 61 ikely occur.** a
38150 74 20 74 68 65 20 74 69 6d 65 20 77 68 65 6e 20 t the time when
38160 74 68 65 20 42 4c 4f 42 20 69 73 20 63 6c 6f 73 the BLOB is clos
38170 65 64 2e 20 20 41 6e 79 20 65 72 72 6f 72 73 20 ed. Any errors
38180 74 68 61 74 20 6f 63 63 75 72 20 64 75 72 69 6e that occur durin
38190 67 0a 2a 2a 20 63 6c 6f 73 69 6e 67 20 61 72 65 g.** closing are
381a0 20 72 65 70 6f 72 74 65 64 20 61 73 20 61 20 6e reported as a n
381b0 6f 6e 2d 7a 65 72 6f 20 72 65 74 75 72 6e 20 76 on-zero return v
381c0 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 alue..**.** The
381d0 42 4c 4f 42 20 69 73 20 63 6c 6f 73 65 64 20 75 BLOB is closed u
381e0 6e 63 6f 6e 64 69 74 69 6f 6e 61 6c 6c 79 2e 20 nconditionally.
381f0 20 45 76 65 6e 20 69 66 20 74 68 69 73 20 72 6f Even if this ro
38200 75 74 69 6e 65 20 72 65 74 75 72 6e 73 0a 2a 2a utine returns.**
38210 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 2c 20 an error code,
38220 74 68 65 20 42 4c 4f 42 20 69 73 20 73 74 69 6c the BLOB is stil
38230 6c 20 63 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 l closed..**.**
38240 43 61 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f 75 Calling this rou
38250 74 69 6e 65 20 77 69 74 68 20 61 20 6e 75 6c 6c tine with a null
38260 20 70 6f 69 6e 74 65 72 20 28 77 68 69 63 68 20 pointer (which
38270 61 73 20 77 6f 75 6c 64 20 62 65 20 72 65 74 75 as would be retu
38280 72 6e 65 64 0a 2a 2a 20 62 79 20 66 61 69 6c 65 rned.** by faile
38290 64 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 d call to [sqlit
382a0 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 29 5d 29 e3_blob_open()])
382b0 20 69 73 20 61 20 68 61 72 6d 6c 65 73 73 20 6e is a harmless n
382c0 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 o-op..**.** Requ
382d0 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 irements:.** [H1
382e0 37 38 33 33 5d 20 5b 48 31 37 38 33 36 5d 20 5b 7833] [H17836] [
382f0 48 31 37 38 33 39 5d 0a 2a 2f 0a 53 51 4c 49 54 H17839].*/.SQLIT
38300 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
38310 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28 73 71 6c 3_blob_close(sql
38320 69 74 65 33 5f 62 6c 6f 62 20 2a 29 3b 0a 0a 2f ite3_blob *);../
38330 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 *.** CAPI3REF: R
38340 65 74 75 72 6e 20 54 68 65 20 53 69 7a 65 20 4f eturn The Size O
38350 66 20 41 6e 20 4f 70 65 6e 20 42 4c 4f 42 20 7b f An Open BLOB {
38360 48 31 37 38 34 30 7d 20 3c 53 33 30 32 33 30 3e H17840} <S30230>
38370 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 73 20 74 .**.** Returns t
38380 68 65 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73 he size in bytes
38390 20 6f 66 20 74 68 65 20 42 4c 4f 42 20 61 63 63 of the BLOB acc
383a0 65 73 73 69 62 6c 65 20 76 69 61 20 74 68 65 20 essible via the
383b0 0a 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 6c 79 .** successfully
383c0 20 6f 70 65 6e 65 64 20 5b 42 4c 4f 42 20 68 61 opened [BLOB ha
383d0 6e 64 6c 65 5d 20 69 6e 20 69 74 73 20 6f 6e 6c ndle] in its onl
383e0 79 20 61 72 67 75 6d 65 6e 74 2e 20 20 54 68 65 y argument. The
383f0 0a 2a 2a 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 .** incremental
38400 62 6c 6f 62 20 49 2f 4f 20 72 6f 75 74 69 6e 65 blob I/O routine
38410 73 20 63 61 6e 20 6f 6e 6c 79 20 72 65 61 64 20 s can only read
38420 6f 72 20 6f 76 65 72 77 72 69 74 69 6e 67 20 65 or overwriting e
38430 78 69 73 74 69 6e 67 0a 2a 2a 20 62 6c 6f 62 20 xisting.** blob
38440 63 6f 6e 74 65 6e 74 3b 20 74 68 65 79 20 63 61 content; they ca
38450 6e 6e 6f 74 20 63 68 61 6e 67 65 20 74 68 65 20 nnot change the
38460 73 69 7a 65 20 6f 66 20 61 20 62 6c 6f 62 2e 0a size of a blob..
38470 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
38480 6e 65 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e ne only works on
38490 20 61 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d a [BLOB handle]
384a0 20 77 68 69 63 68 20 68 61 73 20 62 65 65 6e 20 which has been
384b0 63 72 65 61 74 65 64 0a 2a 2a 20 62 79 20 61 20 created.** by a
384c0 70 72 69 6f 72 20 73 75 63 63 65 73 73 66 75 6c prior successful
384d0 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 call to [sqlite
384e0 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 29 5d 20 61 3_blob_open()] a
384f0 6e 64 20 77 68 69 63 68 20 68 61 73 20 6e 6f 74 nd which has not
38500 0a 2a 2a 20 62 65 65 6e 20 63 6c 6f 73 65 64 20 .** been closed
38510 62 79 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 by [sqlite3_blob
38520 5f 63 6c 6f 73 65 28 29 5d 2e 20 20 50 61 73 73 _close()]. Pass
38530 69 6e 67 20 61 6e 79 20 6f 74 68 65 72 20 70 6f ing any other po
38540 69 6e 74 65 72 20 69 6e 0a 2a 2a 20 74 6f 20 74 inter in.** to t
38550 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 73 75 his routine resu
38560 6c 74 73 20 69 6e 20 75 6e 64 65 66 69 6e 65 64 lts in undefined
38570 20 61 6e 64 20 70 72 6f 62 61 62 6c 79 20 75 6e and probably un
38580 64 65 73 69 72 61 62 6c 65 20 62 65 68 61 76 69 desirable behavi
38590 6f 72 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 or..**.** Requir
385a0 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 37 38 ements:.** [H178
385b0 34 33 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 43].*/.SQLITE_AP
385c0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c I int sqlite3_bl
385d0 6f 62 5f 62 79 74 65 73 28 73 71 6c 69 74 65 33 ob_bytes(sqlite3
385e0 5f 62 6c 6f 62 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a _blob *);../*.**
385f0 20 43 41 50 49 33 52 45 46 3a 20 52 65 61 64 20 CAPI3REF: Read
38600 44 61 74 61 20 46 72 6f 6d 20 41 20 42 4c 4f 42 Data From A BLOB
38610 20 49 6e 63 72 65 6d 65 6e 74 61 6c 6c 79 20 7b Incrementally {
38620 48 31 37 38 35 30 7d 20 3c 53 33 30 32 33 30 3e H17850} <S30230>
38630 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 .**.** This func
38640 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 tion is used to
38650 72 65 61 64 20 64 61 74 61 20 66 72 6f 6d 20 61 read data from a
38660 6e 20 6f 70 65 6e 20 5b 42 4c 4f 42 20 68 61 6e n open [BLOB han
38670 64 6c 65 5d 20 69 6e 74 6f 20 61 0a 2a 2a 20 63 dle] into a.** c
38680 61 6c 6c 65 72 2d 73 75 70 70 6c 69 65 64 20 62 aller-supplied b
38690 75 66 66 65 72 2e 20 4e 20 62 79 74 65 73 20 6f uffer. N bytes o
386a0 66 20 64 61 74 61 20 61 72 65 20 63 6f 70 69 65 f data are copie
386b0 64 20 69 6e 74 6f 20 62 75 66 66 65 72 20 5a 0a d into buffer Z.
386c0 2a 2a 20 66 72 6f 6d 20 74 68 65 20 6f 70 65 6e ** from the open
386d0 20 42 4c 4f 42 2c 20 73 74 61 72 74 69 6e 67 20 BLOB, starting
386e0 61 74 20 6f 66 66 73 65 74 20 69 4f 66 66 73 65 at offset iOffse
386f0 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6f 66 66 73 t..**.** If offs
38700 65 74 20 69 4f 66 66 73 65 74 20 69 73 20 6c 65 et iOffset is le
38710 73 73 20 74 68 61 6e 20 4e 20 62 79 74 65 73 20 ss than N bytes
38720 66 72 6f 6d 20 74 68 65 20 65 6e 64 20 6f 66 20 from the end of
38730 74 68 65 20 42 4c 4f 42 2c 0a 2a 2a 20 5b 53 51 the BLOB,.** [SQ
38740 4c 49 54 45 5f 45 52 52 4f 52 5d 20 69 73 20 72 LITE_ERROR] is r
38750 65 74 75 72 6e 65 64 20 61 6e 64 20 6e 6f 20 64 eturned and no d
38760 61 74 61 20 69 73 20 72 65 61 64 2e 20 20 49 66 ata is read. If
38770 20 4e 20 6f 72 20 69 4f 66 66 73 65 74 20 69 73 N or iOffset is
38780 0a 2a 2a 20 6c 65 73 73 20 74 68 61 6e 20 7a 65 .** less than ze
38790 72 6f 2c 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f ro, [SQLITE_ERRO
387a0 52 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 R] is returned a
387b0 6e 64 20 6e 6f 20 64 61 74 61 20 69 73 20 72 65 nd no data is re
387c0 61 64 2e 0a 2a 2a 20 54 68 65 20 73 69 7a 65 20 ad..** The size
387d0 6f 66 20 74 68 65 20 62 6c 6f 62 20 28 61 6e 64 of the blob (and
387e0 20 68 65 6e 63 65 20 74 68 65 20 6d 61 78 69 6d hence the maxim
387f0 75 6d 20 76 61 6c 75 65 20 6f 66 20 4e 2b 69 4f um value of N+iO
38800 66 66 73 65 74 29 0a 2a 2a 20 63 61 6e 20 62 65 ffset).** can be
38810 20 64 65 74 65 72 6d 69 6e 65 64 20 75 73 69 6e determined usin
38820 67 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 g the [sqlite3_b
38830 6c 6f 62 5f 62 79 74 65 73 28 29 5d 20 69 6e 74 lob_bytes()] int
38840 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 41 6e erface..**.** An
38850 20 61 74 74 65 6d 70 74 20 74 6f 20 72 65 61 64 attempt to read
38860 20 66 72 6f 6d 20 61 6e 20 65 78 70 69 72 65 64 from an expired
38870 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 66 [BLOB handle] f
38880 61 69 6c 73 20 77 69 74 68 20 61 6e 0a 2a 2a 20 ails with an.**
38890 65 72 72 6f 72 20 63 6f 64 65 20 6f 66 20 5b 53 error code of [S
388a0 51 4c 49 54 45 5f 41 42 4f 52 54 5d 2e 0a 2a 2a QLITE_ABORT]..**
388b0 0a 2a 2a 20 4f 6e 20 73 75 63 63 65 73 73 2c 20 .** On success,
388c0 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 SQLITE_OK is ret
388d0 75 72 6e 65 64 2e 0a 2a 2a 20 4f 74 68 65 72 77 urned..** Otherw
388e0 69 73 65 2c 20 61 6e 20 5b 65 72 72 6f 72 20 63 ise, an [error c
388f0 6f 64 65 5d 20 6f 72 20 61 6e 20 5b 65 78 74 65 ode] or an [exte
38900 6e 64 65 64 20 65 72 72 6f 72 20 63 6f 64 65 5d nded error code]
38910 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a is returned..**
38920 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
38930 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 61 only works on a
38940 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 77 [BLOB handle] w
38950 68 69 63 68 20 68 61 73 20 62 65 65 6e 20 63 72 hich has been cr
38960 65 61 74 65 64 0a 2a 2a 20 62 79 20 61 20 70 72 eated.** by a pr
38970 69 6f 72 20 73 75 63 63 65 73 73 66 75 6c 20 63 ior successful c
38980 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f all to [sqlite3_
38990 62 6c 6f 62 5f 6f 70 65 6e 28 29 5d 20 61 6e 64 blob_open()] and
389a0 20 77 68 69 63 68 20 68 61 73 20 6e 6f 74 0a 2a which has not.*
389b0 2a 20 62 65 65 6e 20 63 6c 6f 73 65 64 20 62 79 * been closed by
389c0 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 [sqlite3_blob_c
389d0 6c 6f 73 65 28 29 5d 2e 20 20 50 61 73 73 69 6e lose()]. Passin
389e0 67 20 61 6e 79 20 6f 74 68 65 72 20 70 6f 69 6e g any other poin
389f0 74 65 72 20 69 6e 0a 2a 2a 20 74 6f 20 74 68 69 ter in.** to thi
38a00 73 20 72 6f 75 74 69 6e 65 20 72 65 73 75 6c 74 s routine result
38a10 73 20 69 6e 20 75 6e 64 65 66 69 6e 65 64 20 61 s in undefined a
38a20 6e 64 20 70 72 6f 62 61 62 6c 79 20 75 6e 64 65 nd probably unde
38a30 73 69 72 61 62 6c 65 20 62 65 68 61 76 69 6f 72 sirable behavior
38a40 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f ..**.** See also
38a50 3a 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f : [sqlite3_blob_
38a60 77 72 69 74 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 write()]..**.**
38a70 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a Requirements:.**
38a80 20 5b 48 31 37 38 35 33 5d 20 5b 48 31 37 38 35 [H17853] [H1785
38a90 36 5d 20 5b 48 31 37 38 35 39 5d 20 5b 48 31 37 6] [H17859] [H17
38aa0 38 36 32 5d 20 5b 48 31 37 38 36 33 5d 20 5b 48 862] [H17863] [H
38ab0 31 37 38 36 35 5d 20 5b 48 31 37 38 36 38 5d 0a 17865] [H17868].
38ac0 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
38ad0 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 t sqlite3_blob_r
38ae0 65 61 64 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 ead(sqlite3_blob
38af0 20 2a 2c 20 76 6f 69 64 20 2a 5a 2c 20 69 6e 74 *, void *Z, int
38b00 20 4e 2c 20 69 6e 74 20 69 4f 66 66 73 65 74 29 N, int iOffset)
38b10 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
38b20 46 3a 20 57 72 69 74 65 20 44 61 74 61 20 49 6e F: Write Data In
38b30 74 6f 20 41 20 42 4c 4f 42 20 49 6e 63 72 65 6d to A BLOB Increm
38b40 65 6e 74 61 6c 6c 79 20 7b 48 31 37 38 37 30 7d entally {H17870}
38b50 20 3c 53 33 30 32 33 30 3e 0a 2a 2a 0a 2a 2a 20 <S30230>.**.**
38b60 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 This function is
38b70 20 75 73 65 64 20 74 6f 20 77 72 69 74 65 20 64 used to write d
38b80 61 74 61 20 69 6e 74 6f 20 61 6e 20 6f 70 65 6e ata into an open
38b90 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 66 [BLOB handle] f
38ba0 72 6f 6d 20 61 0a 2a 2a 20 63 61 6c 6c 65 72 2d rom a.** caller-
38bb0 73 75 70 70 6c 69 65 64 20 62 75 66 66 65 72 2e supplied buffer.
38bc0 20 4e 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 N bytes of data
38bd0 20 61 72 65 20 63 6f 70 69 65 64 20 66 72 6f 6d are copied from
38be0 20 74 68 65 20 62 75 66 66 65 72 20 5a 0a 2a 2a the buffer Z.**
38bf0 20 69 6e 74 6f 20 74 68 65 20 6f 70 65 6e 20 42 into the open B
38c00 4c 4f 42 2c 20 73 74 61 72 74 69 6e 67 20 61 74 LOB, starting at
38c10 20 6f 66 66 73 65 74 20 69 4f 66 66 73 65 74 2e offset iOffset.
38c20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 5b 42 .**.** If the [B
38c30 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 70 61 73 73 LOB handle] pass
38c40 65 64 20 61 73 20 74 68 65 20 66 69 72 73 74 20 ed as the first
38c50 61 72 67 75 6d 65 6e 74 20 77 61 73 20 6e 6f 74 argument was not
38c60 20 6f 70 65 6e 65 64 20 66 6f 72 0a 2a 2a 20 77 opened for.** w
38c70 72 69 74 69 6e 67 20 28 74 68 65 20 66 6c 61 67 riting (the flag
38c80 73 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b s parameter to [
38c90 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 sqlite3_blob_ope
38ca0 6e 28 29 5d 20 77 61 73 20 7a 65 72 6f 29 2c 0a n()] was zero),.
38cb0 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e ** this function
38cc0 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 returns [SQLITE
38cd0 5f 52 45 41 44 4f 4e 4c 59 5d 2e 0a 2a 2a 0a 2a _READONLY]..**.*
38ce0 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
38cf0 6d 61 79 20 6f 6e 6c 79 20 6d 6f 64 69 66 79 20 may only modify
38d00 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 the contents of
38d10 74 68 65 20 42 4c 4f 42 3b 20 69 74 20 69 73 0a the BLOB; it is.
38d20 2a 2a 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 ** not possible
38d30 74 6f 20 69 6e 63 72 65 61 73 65 20 74 68 65 20 to increase the
38d40 73 69 7a 65 20 6f 66 20 61 20 42 4c 4f 42 20 75 size of a BLOB u
38d50 73 69 6e 67 20 74 68 69 73 20 41 50 49 2e 0a 2a sing this API..*
38d60 2a 20 49 66 20 6f 66 66 73 65 74 20 69 4f 66 66 * If offset iOff
38d70 73 65 74 20 69 73 20 6c 65 73 73 20 74 68 61 6e set is less than
38d80 20 4e 20 62 79 74 65 73 20 66 72 6f 6d 20 74 68 N bytes from th
38d90 65 20 65 6e 64 20 6f 66 20 74 68 65 20 42 4c 4f e end of the BLO
38da0 42 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 45 52 B,.** [SQLITE_ER
38db0 52 4f 52 5d 20 69 73 20 72 65 74 75 72 6e 65 64 ROR] is returned
38dc0 20 61 6e 64 20 6e 6f 20 64 61 74 61 20 69 73 20 and no data is
38dd0 77 72 69 74 74 65 6e 2e 20 20 49 66 20 4e 20 69 written. If N i
38de0 73 0a 2a 2a 20 6c 65 73 73 20 74 68 61 6e 20 7a s.** less than z
38df0 65 72 6f 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f ero [SQLITE_ERRO
38e00 52 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 R] is returned a
38e10 6e 64 20 6e 6f 20 64 61 74 61 20 69 73 20 77 72 nd no data is wr
38e20 69 74 74 65 6e 2e 0a 2a 2a 20 54 68 65 20 73 69 itten..** The si
38e30 7a 65 20 6f 66 20 74 68 65 20 42 4c 4f 42 20 28 ze of the BLOB (
38e40 61 6e 64 20 68 65 6e 63 65 20 74 68 65 20 6d 61 and hence the ma
38e50 78 69 6d 75 6d 20 76 61 6c 75 65 20 6f 66 20 4e ximum value of N
38e60 2b 69 4f 66 66 73 65 74 29 0a 2a 2a 20 63 61 6e +iOffset).** can
38e70 20 62 65 20 64 65 74 65 72 6d 69 6e 65 64 20 75 be determined u
38e80 73 69 6e 67 20 74 68 65 20 5b 73 71 6c 69 74 65 sing the [sqlite
38e90 33 5f 62 6c 6f 62 5f 62 79 74 65 73 28 29 5d 20 3_blob_bytes()]
38ea0 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a interface..**.**
38eb0 20 41 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 77 An attempt to w
38ec0 72 69 74 65 20 74 6f 20 61 6e 20 65 78 70 69 72 rite to an expir
38ed0 65 64 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d ed [BLOB handle]
38ee0 20 66 61 69 6c 73 20 77 69 74 68 20 61 6e 0a 2a fails with an.*
38ef0 2a 20 65 72 72 6f 72 20 63 6f 64 65 20 6f 66 20 * error code of
38f00 5b 53 51 4c 49 54 45 5f 41 42 4f 52 54 5d 2e 20 [SQLITE_ABORT].
38f10 20 57 72 69 74 65 73 20 74 6f 20 74 68 65 20 42 Writes to the B
38f20 4c 4f 42 20 74 68 61 74 20 6f 63 63 75 72 72 65 LOB that occurre
38f30 64 0a 2a 2a 20 62 65 66 6f 72 65 20 74 68 65 20 d.** before the
38f40 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 65 78 [BLOB handle] ex
38f50 70 69 72 65 64 20 61 72 65 20 6e 6f 74 20 72 6f pired are not ro
38f60 6c 6c 65 64 20 62 61 63 6b 20 62 79 20 74 68 65 lled back by the
38f70 0a 2a 2a 20 65 78 70 69 72 61 74 69 6f 6e 20 6f .** expiration o
38f80 66 20 74 68 65 20 68 61 6e 64 6c 65 2c 20 74 68 f the handle, th
38f90 6f 75 67 68 20 6f 66 20 63 6f 75 72 73 65 20 74 ough of course t
38fa0 68 6f 73 65 20 63 68 61 6e 67 65 73 20 6d 69 67 hose changes mig
38fb0 68 74 0a 2a 2a 20 68 61 76 65 20 62 65 65 6e 20 ht.** have been
38fc0 6f 76 65 72 77 72 69 74 74 65 6e 20 62 79 20 74 overwritten by t
38fd0 68 65 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 he statement tha
38fe0 74 20 65 78 70 69 72 65 64 20 74 68 65 20 42 4c t expired the BL
38ff0 4f 42 20 68 61 6e 64 6c 65 0a 2a 2a 20 6f 72 20 OB handle.** or
39000 62 79 20 6f 74 68 65 72 20 69 6e 64 65 70 65 6e by other indepen
39010 64 65 6e 74 20 73 74 61 74 65 6d 65 6e 74 73 2e dent statements.
39020 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 75 63 63 65 73 .**.** On succes
39030 73 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 s, SQLITE_OK is
39040 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 4f 74 68 returned..** Oth
39050 65 72 77 69 73 65 2c 20 61 6e 20 20 5b 65 72 72 erwise, an [err
39060 6f 72 20 63 6f 64 65 5d 20 6f 72 20 61 6e 20 5b or code] or an [
39070 65 78 74 65 6e 64 65 64 20 65 72 72 6f 72 20 63 extended error c
39080 6f 64 65 5d 20 69 73 20 72 65 74 75 72 6e 65 64 ode] is returned
39090 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
390a0 74 69 6e 65 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 tine only works
390b0 6f 6e 20 61 20 5b 42 4c 4f 42 20 68 61 6e 64 6c on a [BLOB handl
390c0 65 5d 20 77 68 69 63 68 20 68 61 73 20 62 65 65 e] which has bee
390d0 6e 20 63 72 65 61 74 65 64 0a 2a 2a 20 62 79 20 n created.** by
390e0 61 20 70 72 69 6f 72 20 73 75 63 63 65 73 73 66 a prior successf
390f0 75 6c 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 ul call to [sqli
39100 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 29 5d te3_blob_open()]
39110 20 61 6e 64 20 77 68 69 63 68 20 68 61 73 20 6e and which has n
39120 6f 74 0a 2a 2a 20 62 65 65 6e 20 63 6c 6f 73 65 ot.** been close
39130 64 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 62 6c d by [sqlite3_bl
39140 6f 62 5f 63 6c 6f 73 65 28 29 5d 2e 20 20 50 61 ob_close()]. Pa
39150 73 73 69 6e 67 20 61 6e 79 20 6f 74 68 65 72 20 ssing any other
39160 70 6f 69 6e 74 65 72 20 69 6e 0a 2a 2a 20 74 6f pointer in.** to
39170 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 this routine re
39180 73 75 6c 74 73 20 69 6e 20 75 6e 64 65 66 69 6e sults in undefin
39190 65 64 20 61 6e 64 20 70 72 6f 62 61 62 6c 79 20 ed and probably
391a0 75 6e 64 65 73 69 72 61 62 6c 65 20 62 65 68 61 undesirable beha
391b0 76 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 vior..**.** See
391c0 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 62 also: [sqlite3_b
391d0 6c 6f 62 5f 72 65 61 64 28 29 5d 2e 0a 2a 2a 0a lob_read()]..**.
391e0 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a ** Requirements:
391f0 0a 2a 2a 20 5b 48 31 37 38 37 33 5d 20 5b 48 31 .** [H17873] [H1
39200 37 38 37 34 5d 20 5b 48 31 37 38 37 35 5d 20 5b 7874] [H17875] [
39210 48 31 37 38 37 36 5d 20 5b 48 31 37 38 37 37 5d H17876] [H17877]
39220 20 5b 48 31 37 38 37 39 5d 20 5b 48 31 37 38 38 [H17879] [H1788
39230 32 5d 20 5b 48 31 37 38 38 35 5d 0a 2a 2a 20 5b 2] [H17885].** [
39240 48 31 37 38 38 38 5d 0a 2a 2f 0a 53 51 4c 49 54 H17888].*/.SQLIT
39250 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
39260 33 5f 62 6c 6f 62 5f 77 72 69 74 65 28 73 71 6c 3_blob_write(sql
39270 69 74 65 33 5f 62 6c 6f 62 20 2a 2c 20 63 6f 6e ite3_blob *, con
39280 73 74 20 76 6f 69 64 20 2a 7a 2c 20 69 6e 74 20 st void *z, int
39290 6e 2c 20 69 6e 74 20 69 4f 66 66 73 65 74 29 3b n, int iOffset);
392a0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
392b0 3a 20 56 69 72 74 75 61 6c 20 46 69 6c 65 20 53 : Virtual File S
392c0 79 73 74 65 6d 20 4f 62 6a 65 63 74 73 20 7b 48 ystem Objects {H
392d0 31 31 32 30 30 7d 20 3c 53 32 30 31 30 30 3e 0a 11200} <S20100>.
392e0 2a 2a 0a 2a 2a 20 41 20 76 69 72 74 75 61 6c 20 **.** A virtual
392f0 66 69 6c 65 73 79 73 74 65 6d 20 28 56 46 53 29 filesystem (VFS)
39300 20 69 73 20 61 6e 20 5b 73 71 6c 69 74 65 33 5f is an [sqlite3_
39310 76 66 73 5d 20 6f 62 6a 65 63 74 0a 2a 2a 20 74 vfs] object.** t
39320 68 61 74 20 53 51 4c 69 74 65 20 75 73 65 73 20 hat SQLite uses
39330 74 6f 20 69 6e 74 65 72 61 63 74 0a 2a 2a 20 77 to interact.** w
39340 69 74 68 20 74 68 65 20 75 6e 64 65 72 6c 79 69 ith the underlyi
39350 6e 67 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 ng operating sys
39360 74 65 6d 2e 20 20 4d 6f 73 74 20 53 51 4c 69 74 tem. Most SQLit
39370 65 20 62 75 69 6c 64 73 20 63 6f 6d 65 20 77 69 e builds come wi
39380 74 68 20 61 0a 2a 2a 20 73 69 6e 67 6c 65 20 64 th a.** single d
39390 65 66 61 75 6c 74 20 56 46 53 20 74 68 61 74 20 efault VFS that
393a0 69 73 20 61 70 70 72 6f 70 72 69 61 74 65 20 66 is appropriate f
393b0 6f 72 20 74 68 65 20 68 6f 73 74 20 63 6f 6d 70 or the host comp
393c0 75 74 65 72 2e 0a 2a 2a 20 4e 65 77 20 56 46 53 uter..** New VFS
393d0 65 73 20 63 61 6e 20 62 65 20 72 65 67 69 73 74 es can be regist
393e0 65 72 65 64 20 61 6e 64 20 65 78 69 73 74 69 6e ered and existin
393f0 67 20 56 46 53 65 73 20 63 61 6e 20 62 65 20 75 g VFSes can be u
39400 6e 72 65 67 69 73 74 65 72 65 64 2e 0a 2a 2a 20 nregistered..**
39410 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e The following in
39420 74 65 72 66 61 63 65 73 20 61 72 65 20 70 72 6f terfaces are pro
39430 76 69 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 vided..**.** The
39440 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 66 69 6e sqlite3_vfs_fin
39450 64 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 d() interface re
39460 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 turns a pointer
39470 74 6f 20 61 20 56 46 53 20 67 69 76 65 6e 20 69 to a VFS given i
39480 74 73 20 6e 61 6d 65 2e 0a 2a 2a 20 4e 61 6d 65 ts name..** Name
39490 73 20 61 72 65 20 63 61 73 65 20 73 65 6e 73 69 s are case sensi
394a0 74 69 76 65 2e 0a 2a 2a 20 4e 61 6d 65 73 20 61 tive..** Names a
394b0 72 65 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 re zero-terminat
394c0 65 64 20 55 54 46 2d 38 20 73 74 72 69 6e 67 73 ed UTF-8 strings
394d0 2e 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73 ..** If there is
394e0 20 6e 6f 20 6d 61 74 63 68 2c 20 61 20 4e 55 4c no match, a NUL
394f0 4c 20 70 6f 69 6e 74 65 72 20 69 73 20 72 65 74 L pointer is ret
39500 75 72 6e 65 64 2e 0a 2a 2a 20 49 66 20 7a 56 66 urned..** If zVf
39510 73 4e 61 6d 65 20 69 73 20 4e 55 4c 4c 20 74 68 sName is NULL th
39520 65 6e 20 74 68 65 20 64 65 66 61 75 6c 74 20 56 en the default V
39530 46 53 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a FS is returned..
39540 2a 2a 0a 2a 2a 20 4e 65 77 20 56 46 53 65 73 20 **.** New VFSes
39550 61 72 65 20 72 65 67 69 73 74 65 72 65 64 20 77 are registered w
39560 69 74 68 20 73 71 6c 69 74 65 33 5f 76 66 73 5f ith sqlite3_vfs_
39570 72 65 67 69 73 74 65 72 28 29 2e 0a 2a 2a 20 45 register()..** E
39580 61 63 68 20 6e 65 77 20 56 46 53 20 62 65 63 6f ach new VFS beco
39590 6d 65 73 20 74 68 65 20 64 65 66 61 75 6c 74 20 mes the default
395a0 56 46 53 20 69 66 20 74 68 65 20 6d 61 6b 65 44 VFS if the makeD
395b0 66 6c 74 20 66 6c 61 67 20 69 73 20 73 65 74 2e flt flag is set.
395c0 0a 2a 2a 20 54 68 65 20 73 61 6d 65 20 56 46 53 .** The same VFS
395d0 20 63 61 6e 20 62 65 20 72 65 67 69 73 74 65 72 can be register
395e0 65 64 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 ed multiple time
395f0 73 20 77 69 74 68 6f 75 74 20 69 6e 6a 75 72 79 s without injury
39600 2e 0a 2a 2a 20 54 6f 20 6d 61 6b 65 20 61 6e 20 ..** To make an
39610 65 78 69 73 74 69 6e 67 20 56 46 53 20 69 6e 74 existing VFS int
39620 6f 20 74 68 65 20 64 65 66 61 75 6c 74 20 56 46 o the default VF
39630 53 2c 20 72 65 67 69 73 74 65 72 20 69 74 20 61 S, register it a
39640 67 61 69 6e 0a 2a 2a 20 77 69 74 68 20 74 68 65 gain.** with the
39650 20 6d 61 6b 65 44 66 6c 74 20 66 6c 61 67 20 73 makeDflt flag s
39660 65 74 2e 20 20 49 66 20 74 77 6f 20 64 69 66 66 et. If two diff
39670 65 72 65 6e 74 20 56 46 53 65 73 20 77 69 74 68 erent VFSes with
39680 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 6e 61 6d the.** same nam
39690 65 20 61 72 65 20 72 65 67 69 73 74 65 72 65 64 e are registered
396a0 2c 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 69 , the behavior i
396b0 73 20 75 6e 64 65 66 69 6e 65 64 2e 20 20 49 66 s undefined. If
396c0 20 61 0a 2a 2a 20 56 46 53 20 69 73 20 72 65 67 a.** VFS is reg
396d0 69 73 74 65 72 65 64 20 77 69 74 68 20 61 20 6e istered with a n
396e0 61 6d 65 20 74 68 61 74 20 69 73 20 4e 55 4c 4c ame that is NULL
396f0 20 6f 72 20 61 6e 20 65 6d 70 74 79 20 73 74 72 or an empty str
39700 69 6e 67 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 ing,.** then the
39710 20 62 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64 behavior is und
39720 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 55 6e efined..**.** Un
39730 72 65 67 69 73 74 65 72 20 61 20 56 46 53 20 77 register a VFS w
39740 69 74 68 20 74 68 65 20 73 71 6c 69 74 65 33 5f ith the sqlite3_
39750 76 66 73 5f 75 6e 72 65 67 69 73 74 65 72 28 29 vfs_unregister()
39760 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 20 49 interface..** I
39770 66 20 74 68 65 20 64 65 66 61 75 6c 74 20 56 46 f the default VF
39780 53 20 69 73 20 75 6e 72 65 67 69 73 74 65 72 65 S is unregistere
39790 64 2c 20 61 6e 6f 74 68 65 72 20 56 46 53 20 69 d, another VFS i
397a0 73 20 63 68 6f 73 65 6e 20 61 73 0a 2a 2a 20 74 s chosen as.** t
397b0 68 65 20 64 65 66 61 75 6c 74 2e 20 20 54 68 65 he default. The
397c0 20 63 68 6f 69 63 65 20 66 6f 72 20 74 68 65 20 choice for the
397d0 6e 65 77 20 56 46 53 20 69 73 20 61 72 62 69 74 new VFS is arbit
397e0 72 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 rary..**.** Requ
397f0 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 irements:.** [H1
39800 31 32 30 33 5d 20 5b 48 31 31 32 30 36 5d 20 5b 1203] [H11206] [
39810 48 31 31 32 30 39 5d 20 5b 48 31 31 32 31 32 5d H11209] [H11212]
39820 20 5b 48 31 31 32 31 35 5d 20 5b 48 31 31 32 31 [H11215] [H1121
39830 38 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 8].*/.SQLITE_API
39840 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 73 71 sqlite3_vfs *sq
39850 6c 69 74 65 33 5f 76 66 73 5f 66 69 6e 64 28 63 lite3_vfs_find(c
39860 6f 6e 73 74 20 63 68 61 72 20 2a 7a 56 66 73 4e onst char *zVfsN
39870 61 6d 65 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 ame);.SQLITE_API
39880 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 66 73 int sqlite3_vfs
39890 5f 72 65 67 69 73 74 65 72 28 73 71 6c 69 74 65 _register(sqlite
398a0 33 5f 76 66 73 2a 2c 20 69 6e 74 20 6d 61 6b 65 3_vfs*, int make
398b0 44 66 6c 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 Dflt);.SQLITE_AP
398c0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 66 I int sqlite3_vf
398d0 73 5f 75 6e 72 65 67 69 73 74 65 72 28 73 71 6c s_unregister(sql
398e0 69 74 65 33 5f 76 66 73 2a 29 3b 0a 0a 2f 2a 0a ite3_vfs*);../*.
398f0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4d 75 74 ** CAPI3REF: Mut
39900 65 78 65 73 20 7b 48 31 37 30 30 30 7d 20 3c 53 exes {H17000} <S
39910 32 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20000>.**.** The
39920 20 53 51 4c 69 74 65 20 63 6f 72 65 20 75 73 65 SQLite core use
39930 73 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 s these routines
39940 20 66 6f 72 20 74 68 72 65 61 64 0a 2a 2a 20 73 for thread.** s
39950 79 6e 63 68 72 6f 6e 69 7a 61 74 69 6f 6e 2e 20 ynchronization.
39960 54 68 6f 75 67 68 20 74 68 65 79 20 61 72 65 20 Though they are
39970 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 69 6e 74 intended for int
39980 65 72 6e 61 6c 0a 2a 2a 20 75 73 65 20 62 79 20 ernal.** use by
39990 53 51 4c 69 74 65 2c 20 63 6f 64 65 20 74 68 61 SQLite, code tha
399a0 74 20 6c 69 6e 6b 73 20 61 67 61 69 6e 73 74 20 t links against
399b0 53 51 4c 69 74 65 20 69 73 0a 2a 2a 20 70 65 72 SQLite is.** per
399c0 6d 69 74 74 65 64 20 74 6f 20 75 73 65 20 61 6e mitted to use an
399d0 79 20 6f 66 20 74 68 65 73 65 20 72 6f 75 74 69 y of these routi
399e0 6e 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 nes..**.** The S
399f0 51 4c 69 74 65 20 73 6f 75 72 63 65 20 63 6f 64 QLite source cod
39a00 65 20 63 6f 6e 74 61 69 6e 73 20 6d 75 6c 74 69 e contains multi
39a10 70 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 ple implementati
39a20 6f 6e 73 0a 2a 2a 20 6f 66 20 74 68 65 73 65 20 ons.** of these
39a30 6d 75 74 65 78 20 72 6f 75 74 69 6e 65 73 2e 20 mutex routines.
39a40 20 41 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20 An appropriate
39a50 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a implementation.*
39a60 2a 20 69 73 20 73 65 6c 65 63 74 65 64 20 61 75 * is selected au
39a70 74 6f 6d 61 74 69 63 61 6c 6c 79 20 61 74 20 63 tomatically at c
39a80 6f 6d 70 69 6c 65 2d 74 69 6d 65 2e 20 20 54 68 ompile-time. Th
39a90 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 69 e following.** i
39aa0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 61 mplementations a
39ab0 72 65 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 re available in
39ac0 74 68 65 20 53 51 4c 69 74 65 20 63 6f 72 65 3a the SQLite core:
39ad0 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c .**.** <ul>.** <
39ae0 6c 69 3e 20 20 20 53 51 4c 49 54 45 5f 4d 55 54 li> SQLITE_MUT
39af0 45 58 5f 4f 53 32 0a 2a 2a 20 3c 6c 69 3e 20 20 EX_OS2.** <li>
39b00 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 50 54 SQLITE_MUTEX_PT
39b10 48 52 45 41 44 0a 2a 2a 20 3c 6c 69 3e 20 20 20 HREAD.** <li>
39b20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 57 33 32 SQLITE_MUTEX_W32
39b30 0a 2a 2a 20 3c 6c 69 3e 20 20 20 53 51 4c 49 54 .** <li> SQLIT
39b40 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 0a 2a 2a 20 E_MUTEX_NOOP.**
39b50 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 </ul>.**.** The
39b60 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f SQLITE_MUTEX_NOO
39b70 50 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e P implementation
39b80 20 69 73 20 61 20 73 65 74 20 6f 66 20 72 6f 75 is a set of rou
39b90 74 69 6e 65 73 0a 2a 2a 20 74 68 61 74 20 64 6f tines.** that do
39ba0 65 73 20 6e 6f 20 72 65 61 6c 20 6c 6f 63 6b 69 es no real locki
39bb0 6e 67 20 61 6e 64 20 69 73 20 61 70 70 72 6f 70 ng and is approp
39bc0 72 69 61 74 65 20 66 6f 72 20 75 73 65 20 69 6e riate for use in
39bd0 0a 2a 2a 20 61 20 73 69 6e 67 6c 65 2d 74 68 72 .** a single-thr
39be0 65 61 64 65 64 20 61 70 70 6c 69 63 61 74 69 6f eaded applicatio
39bf0 6e 2e 20 20 54 68 65 20 53 51 4c 49 54 45 5f 4d n. The SQLITE_M
39c00 55 54 45 58 5f 4f 53 32 2c 0a 2a 2a 20 53 51 4c UTEX_OS2,.** SQL
39c10 49 54 45 5f 4d 55 54 45 58 5f 50 54 48 52 45 41 ITE_MUTEX_PTHREA
39c20 44 2c 20 61 6e 64 20 53 51 4c 49 54 45 5f 4d 55 D, and SQLITE_MU
39c30 54 45 58 5f 57 33 32 20 69 6d 70 6c 65 6d 65 6e TEX_W32 implemen
39c40 74 61 74 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 61 tations.** are a
39c50 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72 20 75 ppropriate for u
39c60 73 65 20 6f 6e 20 4f 53 2f 32 2c 20 55 6e 69 78 se on OS/2, Unix
39c70 2c 20 61 6e 64 20 57 69 6e 64 6f 77 73 2e 0a 2a , and Windows..*
39c80 2a 0a 2a 2a 20 49 66 20 53 51 4c 69 74 65 20 69 *.** If SQLite i
39c90 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 s compiled with
39ca0 74 68 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 the SQLITE_MUTEX
39cb0 5f 41 50 50 44 45 46 20 70 72 65 70 72 6f 63 65 _APPDEF preproce
39cc0 73 73 6f 72 0a 2a 2a 20 6d 61 63 72 6f 20 64 65 ssor.** macro de
39cd0 66 69 6e 65 64 20 28 77 69 74 68 20 22 2d 44 53 fined (with "-DS
39ce0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 41 50 50 44 QLITE_MUTEX_APPD
39cf0 45 46 3d 31 22 29 2c 20 74 68 65 6e 20 6e 6f 20 EF=1"), then no
39d00 6d 75 74 65 78 0a 2a 2a 20 69 6d 70 6c 65 6d 65 mutex.** impleme
39d10 6e 74 61 74 69 6f 6e 20 69 73 20 69 6e 63 6c 75 ntation is inclu
39d20 64 65 64 20 77 69 74 68 20 74 68 65 20 6c 69 62 ded with the lib
39d30 72 61 72 79 2e 20 49 6e 20 74 68 69 73 20 63 61 rary. In this ca
39d40 73 65 20 74 68 65 0a 2a 2a 20 61 70 70 6c 69 63 se the.** applic
39d50 61 74 69 6f 6e 20 6d 75 73 74 20 73 75 70 70 6c ation must suppl
39d60 79 20 61 20 63 75 73 74 6f 6d 20 6d 75 74 65 78 y a custom mutex
39d70 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
39d80 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 5b 53 51 using the.** [SQ
39d90 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 54 45 LITE_CONFIG_MUTE
39da0 58 5d 20 6f 70 74 69 6f 6e 20 6f 66 20 74 68 65 X] option of the
39db0 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 sqlite3_config(
39dc0 29 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 62 65 ) function.** be
39dd0 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 73 71 6c fore calling sql
39de0 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 ite3_initialize(
39df0 29 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 70 ) or any other p
39e00 75 62 6c 69 63 20 73 71 6c 69 74 65 33 5f 0a 2a ublic sqlite3_.*
39e10 2a 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 * function that
39e20 63 61 6c 6c 73 20 73 71 6c 69 74 65 33 5f 69 6e calls sqlite3_in
39e30 69 74 69 61 6c 69 7a 65 28 29 2e 0a 2a 2a 0a 2a itialize()..**.*
39e40 2a 20 7b 48 31 37 30 31 31 7d 20 54 68 65 20 73 * {H17011} The s
39e50 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c qlite3_mutex_all
39e60 6f 63 28 29 20 72 6f 75 74 69 6e 65 20 61 6c 6c oc() routine all
39e70 6f 63 61 74 65 73 20 61 20 6e 65 77 0a 2a 2a 20 ocates a new.**
39e80 6d 75 74 65 78 20 61 6e 64 20 72 65 74 75 72 6e mutex and return
39e90 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 s a pointer to i
39ea0 74 2e 20 7b 48 31 37 30 31 32 7d 20 49 66 20 69 t. {H17012} If i
39eb0 74 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 0a 2a t returns NULL.*
39ec0 2a 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 61 * that means tha
39ed0 74 20 61 20 6d 75 74 65 78 20 63 6f 75 6c 64 20 t a mutex could
39ee0 6e 6f 74 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 not be allocated
39ef0 2e 20 7b 48 31 37 30 31 33 7d 20 53 51 4c 69 74 . {H17013} SQLit
39f00 65 0a 2a 2a 20 77 69 6c 6c 20 75 6e 77 69 6e 64 e.** will unwind
39f10 20 69 74 73 20 73 74 61 63 6b 20 61 6e 64 20 72 its stack and r
39f20 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 2e 20 eturn an error.
39f30 7b 48 31 37 30 31 34 7d 20 54 68 65 20 61 72 67 {H17014} The arg
39f40 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 73 71 6c 69 ument.** to sqli
39f50 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 te3_mutex_alloc(
39f60 29 20 69 73 20 6f 6e 65 20 6f 66 20 74 68 65 73 ) is one of thes
39f70 65 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 e integer consta
39f80 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a nts:.**.** <ul>.
39f90 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f ** <li> SQLITE_
39fa0 4d 55 54 45 58 5f 46 41 53 54 0a 2a 2a 20 3c 6c MUTEX_FAST.** <l
39fb0 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 i> SQLITE_MUTEX
39fc0 5f 52 45 43 55 52 53 49 56 45 0a 2a 2a 20 3c 6c _RECURSIVE.** <l
39fd0 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 i> SQLITE_MUTEX
39fe0 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 0a 2a _STATIC_MASTER.*
39ff0 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d * <li> SQLITE_M
3a000 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d 0a UTEX_STATIC_MEM.
3a010 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f ** <li> SQLITE_
3a020 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d MUTEX_STATIC_MEM
3a030 32 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 2.** <li> SQLIT
3a040 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 50 E_MUTEX_STATIC_P
3a050 52 4e 47 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c RNG.** <li> SQL
3a060 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 ITE_MUTEX_STATIC
3a070 5f 4c 52 55 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 _LRU.** <li> SQ
3a080 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 LITE_MUTEX_STATI
3a090 43 5f 4c 52 55 32 0a 2a 2a 20 3c 2f 75 6c 3e 0a C_LRU2.** </ul>.
3a0a0 2a 2a 0a 2a 2a 20 7b 48 31 37 30 31 35 7d 20 54 **.** {H17015} T
3a0b0 68 65 20 66 69 72 73 74 20 74 77 6f 20 63 6f 6e he first two con
3a0c0 73 74 61 6e 74 73 20 63 61 75 73 65 20 73 71 6c stants cause sql
3a0d0 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 ite3_mutex_alloc
3a0e0 28 29 20 74 6f 20 63 72 65 61 74 65 0a 2a 2a 20 () to create.**
3a0f0 61 20 6e 65 77 20 6d 75 74 65 78 2e 20 20 54 68 a new mutex. Th
3a100 65 20 6e 65 77 20 6d 75 74 65 78 20 69 73 20 72 e new mutex is r
3a110 65 63 75 72 73 69 76 65 20 77 68 65 6e 20 53 51 ecursive when SQ
3a120 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 LITE_MUTEX_RECUR
3a130 53 49 56 45 0a 2a 2a 20 69 73 20 75 73 65 64 20 SIVE.** is used
3a140 62 75 74 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 but not necessar
3a150 69 6c 79 20 73 6f 20 77 68 65 6e 20 53 51 4c 49 ily so when SQLI
3a160 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 73 TE_MUTEX_FAST is
3a170 20 75 73 65 64 2e 20 7b 45 4e 44 7d 0a 2a 2a 20 used. {END}.**
3a180 54 68 65 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d The mutex implem
3a190 65 6e 74 61 74 69 6f 6e 20 64 6f 65 73 20 6e 6f entation does no
3a1a0 74 20 6e 65 65 64 20 74 6f 20 6d 61 6b 65 20 61 t need to make a
3a1b0 20 64 69 73 74 69 6e 63 74 69 6f 6e 0a 2a 2a 20 distinction.**
3a1c0 62 65 74 77 65 65 6e 20 53 51 4c 49 54 45 5f 4d between SQLITE_M
3a1d0 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 61 UTEX_RECURSIVE a
3a1e0 6e 64 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f nd SQLITE_MUTEX_
3a1f0 46 41 53 54 20 69 66 20 69 74 20 64 6f 65 73 0a FAST if it does.
3a200 2a 2a 20 6e 6f 74 20 77 61 6e 74 20 74 6f 2e 20 ** not want to.
3a210 20 7b 48 31 37 30 31 36 7d 20 42 75 74 20 53 51 {H17016} But SQ
3a220 4c 69 74 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 72 Lite will only r
3a230 65 71 75 65 73 74 20 61 20 72 65 63 75 72 73 69 equest a recursi
3a240 76 65 20 6d 75 74 65 78 20 69 6e 0a 2a 2a 20 63 ve mutex in.** c
3a250 61 73 65 73 20 77 68 65 72 65 20 69 74 20 72 65 ases where it re
3a260 61 6c 6c 79 20 6e 65 65 64 73 20 6f 6e 65 2e 20 ally needs one.
3a270 20 7b 45 4e 44 7d 20 49 66 20 61 20 66 61 73 74 {END} If a fast
3a280 65 72 20 6e 6f 6e 2d 72 65 63 75 72 73 69 76 65 er non-recursive
3a290 20 6d 75 74 65 78 0a 2a 2a 20 69 6d 70 6c 65 6d mutex.** implem
3a2a0 65 6e 74 61 74 69 6f 6e 20 69 73 20 61 76 61 69 entation is avai
3a2b0 6c 61 62 6c 65 20 6f 6e 20 74 68 65 20 68 6f 73 lable on the hos
3a2c0 74 20 70 6c 61 74 66 6f 72 6d 2c 20 74 68 65 20 t platform, the
3a2d0 6d 75 74 65 78 20 73 75 62 73 79 73 74 65 6d 0a mutex subsystem.
3a2e0 2a 2a 20 6d 69 67 68 74 20 72 65 74 75 72 6e 20 ** might return
3a2f0 73 75 63 68 20 61 20 6d 75 74 65 78 20 69 6e 20 such a mutex in
3a300 72 65 73 70 6f 6e 73 65 20 74 6f 20 53 51 4c 49 response to SQLI
3a310 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 2e 0a 2a TE_MUTEX_FAST..*
3a320 2a 0a 2a 2a 20 7b 48 31 37 30 31 37 7d 20 54 68 *.** {H17017} Th
3a330 65 20 6f 74 68 65 72 20 61 6c 6c 6f 77 65 64 20 e other allowed
3a340 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 73 71 parameters to sq
3a350 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f lite3_mutex_allo
3a360 63 28 29 20 65 61 63 68 20 72 65 74 75 72 6e 0a c() each return.
3a370 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 ** a pointer to
3a380 61 20 73 74 61 74 69 63 20 70 72 65 65 78 69 73 a static preexis
3a390 74 69 6e 67 20 6d 75 74 65 78 2e 20 7b 45 4e 44 ting mutex. {END
3a3a0 7d 20 20 53 69 78 20 73 74 61 74 69 63 20 6d 75 } Six static mu
3a3b0 74 65 78 65 73 20 61 72 65 0a 2a 2a 20 75 73 65 texes are.** use
3a3c0 64 20 62 79 20 74 68 65 20 63 75 72 72 65 6e 74 d by the current
3a3d0 20 76 65 72 73 69 6f 6e 20 6f 66 20 53 51 4c 69 version of SQLi
3a3e0 74 65 2e 20 20 46 75 74 75 72 65 20 76 65 72 73 te. Future vers
3a3f0 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 0a 2a ions of SQLite.*
3a400 2a 20 6d 61 79 20 61 64 64 20 61 64 64 69 74 69 * may add additi
3a410 6f 6e 61 6c 20 73 74 61 74 69 63 20 6d 75 74 65 onal static mute
3a420 78 65 73 2e 20 20 53 74 61 74 69 63 20 6d 75 74 xes. Static mut
3a430 65 78 65 73 20 61 72 65 20 66 6f 72 20 69 6e 74 exes are for int
3a440 65 72 6e 61 6c 0a 2a 2a 20 75 73 65 20 62 79 20 ernal.** use by
3a450 53 51 4c 69 74 65 20 6f 6e 6c 79 2e 20 20 41 70 SQLite only. Ap
3a460 70 6c 69 63 61 74 69 6f 6e 73 20 74 68 61 74 20 plications that
3a470 75 73 65 20 53 51 4c 69 74 65 20 6d 75 74 65 78 use SQLite mutex
3a480 65 73 20 73 68 6f 75 6c 64 0a 2a 2a 20 75 73 65 es should.** use
3a490 20 6f 6e 6c 79 20 74 68 65 20 64 79 6e 61 6d 69 only the dynami
3a4a0 63 20 6d 75 74 65 78 65 73 20 72 65 74 75 72 6e c mutexes return
3a4b0 65 64 20 62 79 20 53 51 4c 49 54 45 5f 4d 55 54 ed by SQLITE_MUT
3a4c0 45 58 5f 46 41 53 54 20 6f 72 0a 2a 2a 20 53 51 EX_FAST or.** SQ
3a4d0 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 LITE_MUTEX_RECUR
3a4e0 53 49 56 45 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 SIVE..**.** {H17
3a4f0 30 31 38 7d 20 4e 6f 74 65 20 74 68 61 74 20 69 018} Note that i
3a500 66 20 6f 6e 65 20 6f 66 20 74 68 65 20 64 79 6e f one of the dyn
3a510 61 6d 69 63 20 6d 75 74 65 78 20 70 61 72 61 6d amic mutex param
3a520 65 74 65 72 73 20 28 53 51 4c 49 54 45 5f 4d 55 eters (SQLITE_MU
3a530 54 45 58 5f 46 41 53 54 0a 2a 2a 20 6f 72 20 53 TEX_FAST.** or S
3a540 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 QLITE_MUTEX_RECU
3a550 52 53 49 56 45 29 20 69 73 20 75 73 65 64 20 74 RSIVE) is used t
3a560 68 65 6e 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 hen sqlite3_mute
3a570 78 5f 61 6c 6c 6f 63 28 29 0a 2a 2a 20 72 65 74 x_alloc().** ret
3a580 75 72 6e 73 20 61 20 64 69 66 66 65 72 65 6e 74 urns a different
3a590 20 6d 75 74 65 78 20 6f 6e 20 65 76 65 72 79 20 mutex on every
3a5a0 63 61 6c 6c 2e 20 20 7b 48 31 37 30 33 34 7d 20 call. {H17034}
3a5b0 42 75 74 20 66 6f 72 20 74 68 65 20 73 74 61 74 But for the stat
3a5c0 69 63 0a 2a 2a 20 6d 75 74 65 78 20 74 79 70 65 ic.** mutex type
3a5d0 73 2c 20 74 68 65 20 73 61 6d 65 20 6d 75 74 65 s, the same mute
3a5e0 78 20 69 73 20 72 65 74 75 72 6e 65 64 20 6f 6e x is returned on
3a5f0 20 65 76 65 72 79 20 63 61 6c 6c 20 74 68 61 74 every call that
3a600 20 68 61 73 0a 2a 2a 20 74 68 65 20 73 61 6d 65 has.** the same
3a610 20 74 79 70 65 20 6e 75 6d 62 65 72 2e 0a 2a 2a type number..**
3a620 0a 2a 2a 20 7b 48 31 37 30 31 39 7d 20 54 68 65 .** {H17019} The
3a630 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 66 sqlite3_mutex_f
3a640 72 65 65 28 29 20 72 6f 75 74 69 6e 65 20 64 65 ree() routine de
3a650 61 6c 6c 6f 63 61 74 65 73 20 61 20 70 72 65 76 allocates a prev
3a660 69 6f 75 73 6c 79 0a 2a 2a 20 61 6c 6c 6f 63 61 iously.** alloca
3a670 74 65 64 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 ted dynamic mute
3a680 78 2e 20 7b 48 31 37 30 32 30 7d 20 53 51 4c 69 x. {H17020} SQLi
3a690 74 65 20 69 73 20 63 61 72 65 66 75 6c 20 74 6f te is careful to
3a6a0 20 64 65 61 6c 6c 6f 63 61 74 65 20 65 76 65 72 deallocate ever
3a6b0 79 0a 2a 2a 20 64 79 6e 61 6d 69 63 20 6d 75 74 y.** dynamic mut
3a6c0 65 78 20 74 68 61 74 20 69 74 20 61 6c 6c 6f 63 ex that it alloc
3a6d0 61 74 65 73 2e 20 7b 41 31 37 30 32 31 7d 20 54 ates. {A17021} T
3a6e0 68 65 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 he dynamic mutex
3a6f0 65 73 20 6d 75 73 74 20 6e 6f 74 20 62 65 20 69 es must not be i
3a700 6e 0a 2a 2a 20 75 73 65 20 77 68 65 6e 20 74 68 n.** use when th
3a710 65 79 20 61 72 65 20 64 65 61 6c 6c 6f 63 61 74 ey are deallocat
3a720 65 64 2e 20 7b 41 31 37 30 32 32 7d 20 41 74 74 ed. {A17022} Att
3a730 65 6d 70 74 69 6e 67 20 74 6f 20 64 65 61 6c 6c empting to deall
3a740 6f 63 61 74 65 20 61 20 73 74 61 74 69 63 0a 2a ocate a static.*
3a750 2a 20 6d 75 74 65 78 20 72 65 73 75 6c 74 73 20 * mutex results
3a760 69 6e 20 75 6e 64 65 66 69 6e 65 64 20 62 65 68 in undefined beh
3a770 61 76 69 6f 72 2e 20 7b 48 31 37 30 32 33 7d 20 avior. {H17023}
3a780 53 51 4c 69 74 65 20 6e 65 76 65 72 20 64 65 61 SQLite never dea
3a790 6c 6c 6f 63 61 74 65 73 0a 2a 2a 20 61 20 73 74 llocates.** a st
3a7a0 61 74 69 63 20 6d 75 74 65 78 2e 20 7b 45 4e 44 atic mutex. {END
3a7b0 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 }.**.** The sqli
3a7c0 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 te3_mutex_enter(
3a7d0 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 ) and sqlite3_mu
3a7e0 74 65 78 5f 74 72 79 28 29 20 72 6f 75 74 69 6e tex_try() routin
3a7f0 65 73 20 61 74 74 65 6d 70 74 0a 2a 2a 20 74 6f es attempt.** to
3a800 20 65 6e 74 65 72 20 61 20 6d 75 74 65 78 2e 20 enter a mutex.
3a810 7b 48 31 37 30 32 34 7d 20 49 66 20 61 6e 6f 74 {H17024} If anot
3a820 68 65 72 20 74 68 72 65 61 64 20 69 73 20 61 6c her thread is al
3a830 72 65 61 64 79 20 77 69 74 68 69 6e 20 74 68 65 ready within the
3a840 20 6d 75 74 65 78 2c 0a 2a 2a 20 73 71 6c 69 74 mutex,.** sqlit
3a850 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 29 e3_mutex_enter()
3a860 20 77 69 6c 6c 20 62 6c 6f 63 6b 20 61 6e 64 20 will block and
3a870 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 sqlite3_mutex_tr
3a880 79 28 29 20 77 69 6c 6c 20 72 65 74 75 72 6e 0a y() will return.
3a890 2a 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 20 ** SQLITE_BUSY.
3a8a0 7b 48 31 37 30 32 35 7d 20 20 54 68 65 20 73 71 {H17025} The sq
3a8b0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 lite3_mutex_try(
3a8c0 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 ) interface retu
3a8d0 72 6e 73 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 0a rns [SQLITE_OK].
3a8e0 2a 2a 20 75 70 6f 6e 20 73 75 63 63 65 73 73 66 ** upon successf
3a8f0 75 6c 20 65 6e 74 72 79 2e 20 20 7b 48 31 37 30 ul entry. {H170
3a900 32 36 7d 20 4d 75 74 65 78 65 73 20 63 72 65 61 26} Mutexes crea
3a910 74 65 64 20 75 73 69 6e 67 0a 2a 2a 20 53 51 4c ted using.** SQL
3a920 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 ITE_MUTEX_RECURS
3a930 49 56 45 20 63 61 6e 20 62 65 20 65 6e 74 65 72 IVE can be enter
3a940 65 64 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 ed multiple time
3a950 73 20 62 79 20 74 68 65 20 73 61 6d 65 20 74 68 s by the same th
3a960 72 65 61 64 2e 0a 2a 2a 20 7b 48 31 37 30 32 37 read..** {H17027
3a970 7d 20 49 6e 20 73 75 63 68 20 63 61 73 65 73 20 } In such cases
3a980 74 68 65 2c 0a 2a 2a 20 6d 75 74 65 78 20 6d 75 the,.** mutex mu
3a990 73 74 20 62 65 20 65 78 69 74 65 64 20 61 6e 20 st be exited an
3a9a0 65 71 75 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 equal number of
3a9b0 74 69 6d 65 73 20 62 65 66 6f 72 65 20 61 6e 6f times before ano
3a9c0 74 68 65 72 20 74 68 72 65 61 64 0a 2a 2a 20 63 ther thread.** c
3a9d0 61 6e 20 65 6e 74 65 72 2e 20 20 7b 41 31 37 30 an enter. {A170
3a9e0 32 38 7d 20 49 66 20 74 68 65 20 73 61 6d 65 20 28} If the same
3a9f0 74 68 72 65 61 64 20 74 72 69 65 73 20 74 6f 20 thread tries to
3aa00 65 6e 74 65 72 20 61 6e 79 20 6f 74 68 65 72 0a enter any other.
3aa10 2a 2a 20 6b 69 6e 64 20 6f 66 20 6d 75 74 65 78 ** kind of mutex
3aa20 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65 2c more than once,
3aa30 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 69 73 the behavior is
3aa40 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 20 7b undefined..** {
3aa50 48 31 37 30 32 39 7d 20 53 51 4c 69 74 65 20 77 H17029} SQLite w
3aa60 69 6c 6c 20 6e 65 76 65 72 20 65 78 68 69 62 69 ill never exhibi
3aa70 74 0a 2a 2a 20 73 75 63 68 20 62 65 68 61 76 69 t.** such behavi
3aa80 6f 72 20 69 6e 20 69 74 73 20 6f 77 6e 20 75 73 or in its own us
3aa90 65 20 6f 66 20 6d 75 74 65 78 65 73 2e 0a 2a 2a e of mutexes..**
3aaa0 0a 2a 2a 20 53 6f 6d 65 20 73 79 73 74 65 6d 73 .** Some systems
3aab0 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 57 (for example, W
3aac0 69 6e 64 6f 77 73 20 39 35 29 20 64 6f 20 6e 6f indows 95) do no
3aad0 74 20 73 75 70 70 6f 72 74 20 74 68 65 20 6f 70 t support the op
3aae0 65 72 61 74 69 6f 6e 0a 2a 2a 20 69 6d 70 6c 65 eration.** imple
3aaf0 6d 65 6e 74 65 64 20 62 79 20 73 71 6c 69 74 65 mented by sqlite
3ab00 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 2e 20 20 3_mutex_try().
3ab10 4f 6e 20 74 68 6f 73 65 20 73 79 73 74 65 6d 73 On those systems
3ab20 2c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f , sqlite3_mutex_
3ab30 74 72 79 28 29 0a 2a 2a 20 77 69 6c 6c 20 61 6c try().** will al
3ab40 77 61 79 73 20 72 65 74 75 72 6e 20 53 51 4c 49 ways return SQLI
3ab50 54 45 5f 42 55 53 59 2e 20 20 7b 48 31 37 30 33 TE_BUSY. {H1703
3ab60 30 7d 20 54 68 65 20 53 51 4c 69 74 65 20 63 6f 0} The SQLite co
3ab70 72 65 20 6f 6e 6c 79 20 65 76 65 72 20 75 73 65 re only ever use
3ab80 73 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 s.** sqlite3_mut
3ab90 65 78 5f 74 72 79 28 29 20 61 73 20 61 6e 20 6f ex_try() as an o
3aba0 70 74 69 6d 69 7a 61 74 69 6f 6e 20 73 6f 20 74 ptimization so t
3abb0 68 69 73 20 69 73 20 61 63 63 65 70 74 61 62 6c his is acceptabl
3abc0 65 20 62 65 68 61 76 69 6f 72 2e 0a 2a 2a 0a 2a e behavior..**.*
3abd0 2a 20 7b 48 31 37 30 33 31 7d 20 54 68 65 20 73 * {H17031} The s
3abe0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
3abf0 76 65 28 29 20 72 6f 75 74 69 6e 65 20 65 78 69 ve() routine exi
3ac00 74 73 20 61 20 6d 75 74 65 78 20 74 68 61 74 20 ts a mutex that
3ac10 77 61 73 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c was.** previousl
3ac20 79 20 65 6e 74 65 72 65 64 20 62 79 20 74 68 65 y entered by the
3ac30 20 73 61 6d 65 20 74 68 72 65 61 64 2e 20 20 7b same thread. {
3ac40 41 31 37 30 33 32 7d 20 54 68 65 20 62 65 68 61 A17032} The beha
3ac50 76 69 6f 72 0a 2a 2a 20 69 73 20 75 6e 64 65 66 vior.** is undef
3ac60 69 6e 65 64 20 69 66 20 74 68 65 20 6d 75 74 65 ined if the mute
3ac70 78 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 x is not current
3ac80 6c 79 20 65 6e 74 65 72 65 64 20 62 79 20 74 68 ly entered by th
3ac90 65 0a 2a 2a 20 63 61 6c 6c 69 6e 67 20 74 68 72 e.** calling thr
3aca0 65 61 64 20 6f 72 20 69 73 20 6e 6f 74 20 63 75 ead or is not cu
3acb0 72 72 65 6e 74 6c 79 20 61 6c 6c 6f 63 61 74 65 rrently allocate
3acc0 64 2e 20 20 7b 48 31 37 30 33 33 7d 20 53 51 4c d. {H17033} SQL
3acd0 69 74 65 20 77 69 6c 6c 0a 2a 2a 20 6e 65 76 65 ite will.** neve
3ace0 72 20 64 6f 20 65 69 74 68 65 72 2e 20 7b 45 4e r do either. {EN
3acf0 44 7d 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 D}.**.** If the
3ad00 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 argument to sqli
3ad10 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 te3_mutex_enter(
3ad20 29 2c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ), sqlite3_mutex
3ad30 5f 74 72 79 28 29 2c 20 6f 72 0a 2a 2a 20 73 71 _try(), or.** sq
3ad40 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
3ad50 65 28 29 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f e() is a NULL po
3ad60 69 6e 74 65 72 2c 20 74 68 65 6e 20 61 6c 6c 20 inter, then all
3ad70 74 68 72 65 65 20 72 6f 75 74 69 6e 65 73 0a 2a three routines.*
3ad80 2a 20 62 65 68 61 76 65 20 61 73 20 6e 6f 2d 6f * behave as no-o
3ad90 70 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c ps..**.** See al
3ada0 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 so: [sqlite3_mut
3adb0 65 78 5f 68 65 6c 64 28 29 5d 20 61 6e 64 20 5b ex_held()] and [
3adc0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f sqlite3_mutex_no
3add0 74 68 65 6c 64 28 29 5d 2e 0a 2a 2f 0a 53 51 4c theld()]..*/.SQL
3ade0 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f ITE_API sqlite3_
3adf0 6d 75 74 65 78 20 2a 73 71 6c 69 74 65 33 5f 6d mutex *sqlite3_m
3ae00 75 74 65 78 5f 61 6c 6c 6f 63 28 69 6e 74 29 3b utex_alloc(int);
3ae10 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 .SQLITE_API void
3ae20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 66 sqlite3_mutex_f
3ae30 72 65 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 ree(sqlite3_mute
3ae40 78 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 x*);.SQLITE_API
3ae50 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 void sqlite3_mut
3ae60 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 74 65 33 ex_enter(sqlite3
3ae70 5f 6d 75 74 65 78 2a 29 3b 0a 53 51 4c 49 54 45 _mutex*);.SQLITE
3ae80 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
3ae90 5f 6d 75 74 65 78 5f 74 72 79 28 73 71 6c 69 74 _mutex_try(sqlit
3aea0 65 33 5f 6d 75 74 65 78 2a 29 3b 0a 53 51 4c 49 e3_mutex*);.SQLI
3aeb0 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 TE_API void sqli
3aec0 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
3aed0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 3b sqlite3_mutex*);
3aee0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
3aef0 3a 20 4d 75 74 65 78 20 4d 65 74 68 6f 64 73 20 : Mutex Methods
3af00 4f 62 6a 65 63 74 20 7b 48 31 37 31 32 30 7d 20 Object {H17120}
3af10 3c 53 32 30 31 33 30 3e 0a 2a 2a 20 45 58 50 45 <S20130>.** EXPE
3af20 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 41 RIMENTAL.**.** A
3af30 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 n instance of th
3af40 69 73 20 73 74 72 75 63 74 75 72 65 20 64 65 66 is structure def
3af50 69 6e 65 73 20 74 68 65 20 6c 6f 77 2d 6c 65 76 ines the low-lev
3af60 65 6c 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 75 el routines.** u
3af70 73 65 64 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 sed to allocate
3af80 61 6e 64 20 75 73 65 20 6d 75 74 65 78 65 73 2e and use mutexes.
3af90 0a 2a 2a 0a 2a 2a 20 55 73 75 61 6c 6c 79 2c 20 .**.** Usually,
3afa0 74 68 65 20 64 65 66 61 75 6c 74 20 6d 75 74 65 the default mute
3afb0 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e x implementation
3afc0 73 20 70 72 6f 76 69 64 65 64 20 62 79 20 53 51 s provided by SQ
3afd0 4c 69 74 65 20 61 72 65 0a 2a 2a 20 73 75 66 66 Lite are.** suff
3afe0 69 63 69 65 6e 74 2c 20 68 6f 77 65 76 65 72 20 icient, however
3aff0 74 68 65 20 75 73 65 72 20 68 61 73 20 74 68 65 the user has the
3b000 20 6f 70 74 69 6f 6e 20 6f 66 20 73 75 62 73 74 option of subst
3b010 69 74 75 74 69 6e 67 20 61 20 63 75 73 74 6f 6d ituting a custom
3b020 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 .** implementati
3b030 6f 6e 20 66 6f 72 20 73 70 65 63 69 61 6c 69 7a on for specializ
3b040 65 64 20 64 65 70 6c 6f 79 6d 65 6e 74 73 20 6f ed deployments o
3b050 72 20 73 79 73 74 65 6d 73 20 66 6f 72 20 77 68 r systems for wh
3b060 69 63 68 20 53 51 4c 69 74 65 0a 2a 2a 20 64 6f ich SQLite.** do
3b070 65 73 20 6e 6f 74 20 70 72 6f 76 69 64 65 20 61 es not provide a
3b080 20 73 75 69 74 61 62 6c 65 20 69 6d 70 6c 65 6d suitable implem
3b090 65 6e 74 61 74 69 6f 6e 2e 20 49 6e 20 74 68 69 entation. In thi
3b0a0 73 20 63 61 73 65 2c 20 74 68 65 20 75 73 65 72 s case, the user
3b0b0 0a 2a 2a 20 63 72 65 61 74 65 73 20 61 6e 64 20 .** creates and
3b0c0 70 6f 70 75 6c 61 74 65 73 20 61 6e 20 69 6e 73 populates an ins
3b0d0 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 tance of this st
3b0e0 72 75 63 74 75 72 65 20 74 6f 20 70 61 73 73 0a ructure to pass.
3b0f0 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f ** to sqlite3_co
3b100 6e 66 69 67 28 29 20 61 6c 6f 6e 67 20 77 69 74 nfig() along wit
3b110 68 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 43 4f h the [SQLITE_CO
3b120 4e 46 49 47 5f 4d 55 54 45 58 5d 20 6f 70 74 69 NFIG_MUTEX] opti
3b130 6f 6e 2e 0a 2a 2a 20 41 64 64 69 74 69 6f 6e 61 on..** Additiona
3b140 6c 6c 79 2c 20 61 6e 20 69 6e 73 74 61 6e 63 65 lly, an instance
3b150 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 of this structu
3b160 72 65 20 63 61 6e 20 62 65 20 75 73 65 64 20 61 re can be used a
3b170 73 20 61 6e 0a 2a 2a 20 6f 75 74 70 75 74 20 76 s an.** output v
3b180 61 72 69 61 62 6c 65 20 77 68 65 6e 20 71 75 65 ariable when que
3b190 72 79 69 6e 67 20 74 68 65 20 73 79 73 74 65 6d rying the system
3b1a0 20 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 for the current
3b1b0 20 6d 75 74 65 78 0a 2a 2a 20 69 6d 70 6c 65 6d mutex.** implem
3b1c0 65 6e 74 61 74 69 6f 6e 2c 20 75 73 69 6e 67 20 entation, using
3b1d0 74 68 65 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 the [SQLITE_CONF
3b1e0 49 47 5f 47 45 54 4d 55 54 45 58 5d 20 6f 70 74 IG_GETMUTEX] opt
3b1f0 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 ion..**.** The x
3b200 4d 75 74 65 78 49 6e 69 74 20 6d 65 74 68 6f 64 MutexInit method
3b210 20 64 65 66 69 6e 65 64 20 62 79 20 74 68 69 73 defined by this
3b220 20 73 74 72 75 63 74 75 72 65 20 69 73 20 69 6e structure is in
3b230 76 6f 6b 65 64 20 61 73 0a 2a 2a 20 70 61 72 74 voked as.** part
3b240 20 6f 66 20 73 79 73 74 65 6d 20 69 6e 69 74 69 of system initi
3b250 61 6c 69 7a 61 74 69 6f 6e 20 62 79 20 74 68 65 alization by the
3b260 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c sqlite3_initial
3b270 69 7a 65 28 29 20 66 75 6e 63 74 69 6f 6e 2e 0a ize() function..
3b280 2a 2a 20 7b 48 31 37 30 30 31 7d 20 54 68 65 20 ** {H17001} The
3b290 78 4d 75 74 65 78 49 6e 69 74 20 72 6f 75 74 69 xMutexInit routi
3b2a0 6e 65 20 73 68 61 6c 6c 20 62 65 20 63 61 6c 6c ne shall be call
3b2b0 65 64 20 62 79 20 53 51 4c 69 74 65 20 6f 6e 63 ed by SQLite onc
3b2c0 65 20 66 6f 72 20 65 61 63 68 0a 2a 2a 20 65 66 e for each.** ef
3b2d0 66 65 63 74 69 76 65 20 63 61 6c 6c 20 74 6f 20 fective call to
3b2e0 5b 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c [sqlite3_initial
3b2f0 69 7a 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 ize()]..**.** Th
3b300 65 20 78 4d 75 74 65 78 45 6e 64 20 6d 65 74 68 e xMutexEnd meth
3b310 6f 64 20 64 65 66 69 6e 65 64 20 62 79 20 74 68 od defined by th
3b320 69 73 20 73 74 72 75 63 74 75 72 65 20 69 73 20 is structure is
3b330 69 6e 76 6f 6b 65 64 20 61 73 0a 2a 2a 20 70 61 invoked as.** pa
3b340 72 74 20 6f 66 20 73 79 73 74 65 6d 20 73 68 75 rt of system shu
3b350 74 64 6f 77 6e 20 62 79 20 74 68 65 20 73 71 6c tdown by the sql
3b360 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 20 ite3_shutdown()
3b370 66 75 6e 63 74 69 6f 6e 2e 20 54 68 65 0a 2a 2a function. The.**
3b380 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
3b390 6f 66 20 74 68 69 73 20 6d 65 74 68 6f 64 20 69 of this method i
3b3a0 73 20 65 78 70 65 63 74 65 64 20 74 6f 20 72 65 s expected to re
3b3b0 6c 65 61 73 65 20 61 6c 6c 20 6f 75 74 73 74 61 lease all outsta
3b3c0 6e 64 69 6e 67 0a 2a 2a 20 72 65 73 6f 75 72 63 nding.** resourc
3b3d0 65 73 20 6f 62 74 61 69 6e 65 64 20 62 79 20 74 es obtained by t
3b3e0 68 65 20 6d 75 74 65 78 20 6d 65 74 68 6f 64 73 he mutex methods
3b3f0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c implementation,
3b400 20 65 73 70 65 63 69 61 6c 6c 79 0a 2a 2a 20 74 especially.** t
3b410 68 6f 73 65 20 6f 62 74 61 69 6e 65 64 20 62 79 hose obtained by
3b420 20 74 68 65 20 78 4d 75 74 65 78 49 6e 69 74 20 the xMutexInit
3b430 6d 65 74 68 6f 64 2e 20 7b 48 31 37 30 30 33 7d method. {H17003}
3b440 20 54 68 65 20 78 4d 75 74 65 78 45 6e 64 28 29 The xMutexEnd()
3b450 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 73 68 .** interface sh
3b460 61 6c 6c 20 62 65 20 69 6e 76 6f 6b 65 64 20 6f all be invoked o
3b470 6e 63 65 20 66 6f 72 20 65 61 63 68 20 63 61 6c nce for each cal
3b480 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 68 l to [sqlite3_sh
3b490 75 74 64 6f 77 6e 28 29 5d 2e 0a 2a 2a 0a 2a 2a utdown()]..**.**
3b4a0 20 54 68 65 20 72 65 6d 61 69 6e 69 6e 67 20 73 The remaining s
3b4b0 65 76 65 6e 20 6d 65 74 68 6f 64 73 20 64 65 66 even methods def
3b4c0 69 6e 65 64 20 62 79 20 74 68 69 73 20 73 74 72 ined by this str
3b4d0 75 63 74 75 72 65 20 28 78 4d 75 74 65 78 41 6c ucture (xMutexAl
3b4e0 6c 6f 63 2c 0a 2a 2a 20 78 4d 75 74 65 78 46 72 loc,.** xMutexFr
3b4f0 65 65 2c 20 78 4d 75 74 65 78 45 6e 74 65 72 2c ee, xMutexEnter,
3b500 20 78 4d 75 74 65 78 54 72 79 2c 20 78 4d 75 74 xMutexTry, xMut
3b510 65 78 4c 65 61 76 65 2c 20 78 4d 75 74 65 78 48 exLeave, xMutexH
3b520 65 6c 64 20 61 6e 64 0a 2a 2a 20 78 4d 75 74 65 eld and.** xMute
3b530 78 4e 6f 74 68 65 6c 64 29 20 69 6d 70 6c 65 6d xNotheld) implem
3b540 65 6e 74 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e ent the followin
3b550 67 20 69 6e 74 65 72 66 61 63 65 73 20 28 72 65 g interfaces (re
3b560 73 70 65 63 74 69 76 65 6c 79 29 3a 0a 2a 2a 0a spectively):.**.
3b570 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 20 20 3c 6c 69 ** <ul>.** <li
3b580 3e 20 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 > [sqlite3_mute
3b590 78 5f 61 6c 6c 6f 63 28 29 5d 20 3c 2f 6c 69 3e x_alloc()] </li>
3b5a0 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b 73 71 6c .** <li> [sql
3b5b0 69 74 65 33 5f 6d 75 74 65 78 5f 66 72 65 65 28 ite3_mutex_free(
3b5c0 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 20 20 3c 6c )] </li>.** <l
3b5d0 69 3e 20 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 i> [sqlite3_mut
3b5e0 65 78 5f 65 6e 74 65 72 28 29 5d 20 3c 2f 6c 69 ex_enter()] </li
3b5f0 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b 73 71 >.** <li> [sq
3b600 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 lite3_mutex_try(
3b610 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 20 20 3c 6c )] </li>.** <l
3b620 69 3e 20 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 i> [sqlite3_mut
3b630 65 78 5f 6c 65 61 76 65 28 29 5d 20 3c 2f 6c 69 ex_leave()] </li
3b640 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b 73 71 >.** <li> [sq
3b650 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
3b660 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 20 20 3c ()] </li>.** <
3b670 6c 69 3e 20 20 5b 73 71 6c 69 74 65 33 5f 6d 75 li> [sqlite3_mu
3b680 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 5d 20 3c tex_notheld()] <
3b690 2f 6c 69 3e 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a /li>.** </ul>.**
3b6a0 0a 2a 2a 20 54 68 65 20 6f 6e 6c 79 20 64 69 66 .** The only dif
3b6b0 66 65 72 65 6e 63 65 20 69 73 20 74 68 61 74 20 ference is that
3b6c0 74 68 65 20 70 75 62 6c 69 63 20 73 71 6c 69 74 the public sqlit
3b6d0 65 33 5f 58 58 58 20 66 75 6e 63 74 69 6f 6e 73 e3_XXX functions
3b6e0 20 65 6e 75 6d 65 72 61 74 65 64 0a 2a 2a 20 61 enumerated.** a
3b6f0 62 6f 76 65 20 73 69 6c 65 6e 74 6c 79 20 69 67 bove silently ig
3b700 6e 6f 72 65 20 61 6e 79 20 69 6e 76 6f 63 61 74 nore any invocat
3b710 69 6f 6e 73 20 74 68 61 74 20 70 61 73 73 20 61 ions that pass a
3b720 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 69 6e NULL pointer in
3b730 73 74 65 61 64 0a 2a 2a 20 6f 66 20 61 20 76 61 stead.** of a va
3b740 6c 69 64 20 6d 75 74 65 78 20 68 61 6e 64 6c 65 lid mutex handle
3b750 2e 20 54 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 . The implementa
3b760 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 6d 65 74 tions of the met
3b770 68 6f 64 73 20 64 65 66 69 6e 65 64 0a 2a 2a 20 hods defined.**
3b780 62 79 20 74 68 69 73 20 73 74 72 75 63 74 75 72 by this structur
3b790 65 20 61 72 65 20 6e 6f 74 20 72 65 71 75 69 72 e are not requir
3b7a0 65 64 20 74 6f 20 68 61 6e 64 6c 65 20 74 68 69 ed to handle thi
3b7b0 73 20 63 61 73 65 2c 20 74 68 65 20 72 65 73 75 s case, the resu
3b7c0 6c 74 73 0a 2a 2a 20 6f 66 20 70 61 73 73 69 6e lts.** of passin
3b7d0 67 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 g a NULL pointer
3b7e0 20 69 6e 73 74 65 61 64 20 6f 66 20 61 20 76 61 instead of a va
3b7f0 6c 69 64 20 6d 75 74 65 78 20 68 61 6e 64 6c 65 lid mutex handle
3b800 20 61 72 65 20 75 6e 64 65 66 69 6e 65 64 0a 2a are undefined.*
3b810 2a 20 28 69 2e 65 2e 20 69 74 20 69 73 20 61 63 * (i.e. it is ac
3b820 63 65 70 74 61 62 6c 65 20 74 6f 20 70 72 6f 76 ceptable to prov
3b830 69 64 65 20 61 6e 20 69 6d 70 6c 65 6d 65 6e 74 ide an implement
3b840 61 74 69 6f 6e 20 74 68 61 74 20 73 65 67 66 61 ation that segfa
3b850 75 6c 74 73 20 69 66 0a 2a 2a 20 69 74 20 69 73 ults if.** it is
3b860 20 70 61 73 73 65 64 20 61 20 4e 55 4c 4c 20 70 passed a NULL p
3b870 6f 69 6e 74 65 72 29 2e 0a 2a 2a 0a 2a 2a 20 54 ointer)..**.** T
3b880 68 65 20 78 4d 75 74 65 78 49 6e 69 74 28 29 20 he xMutexInit()
3b890 6d 65 74 68 6f 64 20 6d 75 73 74 20 62 65 20 74 method must be t
3b8a0 68 72 65 61 64 73 61 66 65 2e 20 20 49 74 20 6d hreadsafe. It m
3b8b0 75 73 74 20 62 65 20 68 61 72 6d 6c 65 73 73 20 ust be harmless
3b8c0 74 6f 0a 2a 2a 20 69 6e 76 6f 6b 65 20 78 4d 75 to.** invoke xMu
3b8d0 74 65 78 49 6e 69 74 28 29 20 6d 75 74 69 70 6c texInit() mutipl
3b8e0 65 20 74 69 6d 65 73 20 77 69 74 68 69 6e 20 74 e times within t
3b8f0 68 65 20 73 61 6d 65 20 70 72 6f 63 65 73 73 20 he same process
3b900 61 6e 64 20 77 69 74 68 6f 75 74 0a 2a 2a 20 69 and without.** i
3b910 6e 74 65 72 76 65 6e 69 6e 67 20 63 61 6c 6c 73 ntervening calls
3b920 20 74 6f 20 78 4d 75 74 65 78 45 6e 64 28 29 2e to xMutexEnd().
3b930 20 20 53 65 63 6f 6e 64 20 61 6e 64 20 73 75 62 Second and sub
3b940 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 74 6f sequent calls to
3b950 0a 2a 2a 20 78 4d 75 74 65 78 49 6e 69 74 28 29 .** xMutexInit()
3b960 20 6d 75 73 74 20 62 65 20 6e 6f 2d 6f 70 73 2e must be no-ops.
3b970 0a 2a 2a 0a 2a 2a 20 78 4d 75 74 65 78 49 6e 69 .**.** xMutexIni
3b980 74 28 29 20 6d 75 73 74 20 6e 6f 74 20 75 73 65 t() must not use
3b990 20 53 51 4c 69 74 65 20 6d 65 6d 6f 72 79 20 61 SQLite memory a
3b9a0 6c 6c 6f 63 61 74 69 6f 6e 20 28 5b 73 71 6c 69 llocation ([sqli
3b9b0 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 0a 2a 2a te3_malloc()].**
3b9c0 20 61 6e 64 20 69 74 73 20 61 73 73 6f 63 69 61 and its associa
3b9d0 74 65 73 29 2e 20 20 53 69 6d 69 6c 61 72 6c 79 tes). Similarly
3b9e0 2c 20 78 4d 75 74 65 78 41 6c 6c 6f 63 28 29 20 , xMutexAlloc()
3b9f0 6d 75 73 74 20 6e 6f 74 20 75 73 65 20 53 51 4c must not use SQL
3ba00 69 74 65 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c ite memory.** al
3ba10 6c 6f 63 61 74 69 6f 6e 20 66 6f 72 20 61 20 73 location for a s
3ba20 74 61 74 69 63 20 6d 75 74 65 78 2e 20 20 48 6f tatic mutex. Ho
3ba30 77 65 76 65 72 20 78 4d 75 74 65 78 41 6c 6c 6f wever xMutexAllo
3ba40 63 28 29 20 6d 61 79 20 75 73 65 20 53 51 4c 69 c() may use SQLi
3ba50 74 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c te.** memory all
3ba60 6f 63 61 74 69 6f 6e 20 66 6f 72 20 61 20 66 61 ocation for a fa
3ba70 73 74 20 6f 72 20 72 65 63 75 72 73 69 76 65 20 st or recursive
3ba80 6d 75 74 65 78 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c mutex..**.** SQL
3ba90 69 74 65 20 77 69 6c 6c 20 69 6e 76 6f 6b 65 20 ite will invoke
3baa0 74 68 65 20 78 4d 75 74 65 78 45 6e 64 28 29 20 the xMutexEnd()
3bab0 6d 65 74 68 6f 64 20 77 68 65 6e 20 5b 73 71 6c method when [sql
3bac0 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 5d ite3_shutdown()]
3bad0 20 69 73 0a 2a 2a 20 63 61 6c 6c 65 64 2c 20 62 is.** called, b
3bae0 75 74 20 6f 6e 6c 79 20 69 66 20 74 68 65 20 70 ut only if the p
3baf0 72 69 6f 72 20 63 61 6c 6c 20 74 6f 20 78 4d 75 rior call to xMu
3bb00 74 65 78 49 6e 69 74 20 72 65 74 75 72 6e 65 64 texInit returned
3bb10 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a 20 49 SQLITE_OK..** I
3bb20 66 20 78 4d 75 74 65 78 49 6e 69 74 20 66 61 69 f xMutexInit fai
3bb30 6c 73 20 69 6e 20 61 6e 79 20 77 61 79 2c 20 69 ls in any way, i
3bb40 74 20 69 73 20 65 78 70 65 63 74 65 64 20 74 6f t is expected to
3bb50 20 63 6c 65 61 6e 20 75 70 20 61 66 74 65 72 20 clean up after
3bb60 69 74 73 65 6c 66 0a 2a 2a 20 70 72 69 6f 72 20 itself.** prior
3bb70 74 6f 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f to returning..*/
3bb80 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
3bb90 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 sqlite3_mutex_me
3bba0 74 68 6f 64 73 20 73 71 6c 69 74 65 33 5f 6d 75 thods sqlite3_mu
3bbb0 74 65 78 5f 6d 65 74 68 6f 64 73 3b 0a 73 74 72 tex_methods;.str
3bbc0 75 63 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 uct sqlite3_mute
3bbd0 78 5f 6d 65 74 68 6f 64 73 20 7b 0a 20 20 69 6e x_methods {. in
3bbe0 74 20 28 2a 78 4d 75 74 65 78 49 6e 69 74 29 28 t (*xMutexInit)(
3bbf0 76 6f 69 64 29 3b 0a 20 20 69 6e 74 20 28 2a 78 void);. int (*x
3bc00 4d 75 74 65 78 45 6e 64 29 28 76 6f 69 64 29 3b MutexEnd)(void);
3bc10 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 . sqlite3_mutex
3bc20 20 2a 28 2a 78 4d 75 74 65 78 41 6c 6c 6f 63 29 *(*xMutexAlloc)
3bc30 28 69 6e 74 29 3b 0a 20 20 76 6f 69 64 20 28 2a (int);. void (*
3bc40 78 4d 75 74 65 78 46 72 65 65 29 28 73 71 6c 69 xMutexFree)(sqli
3bc50 74 65 33 5f 6d 75 74 65 78 20 2a 29 3b 0a 20 20 te3_mutex *);.
3bc60 76 6f 69 64 20 28 2a 78 4d 75 74 65 78 45 6e 74 void (*xMutexEnt
3bc70 65 72 29 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 er)(sqlite3_mute
3bc80 78 20 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 4d x *);. int (*xM
3bc90 75 74 65 78 54 72 79 29 28 73 71 6c 69 74 65 33 utexTry)(sqlite3
3bca0 5f 6d 75 74 65 78 20 2a 29 3b 0a 20 20 76 6f 69 _mutex *);. voi
3bcb0 64 20 28 2a 78 4d 75 74 65 78 4c 65 61 76 65 29 d (*xMutexLeave)
3bcc0 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a (sqlite3_mutex *
3bcd0 29 3b 0a 20 20 69 6e 74 20 28 2a 78 4d 75 74 65 );. int (*xMute
3bce0 78 48 65 6c 64 29 28 73 71 6c 69 74 65 33 5f 6d xHeld)(sqlite3_m
3bcf0 75 74 65 78 20 2a 29 3b 0a 20 20 69 6e 74 20 28 utex *);. int (
3bd00 2a 78 4d 75 74 65 78 4e 6f 74 68 65 6c 64 29 28 *xMutexNotheld)(
3bd10 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 29 sqlite3_mutex *)
3bd20 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 ;.};../*.** CAPI
3bd30 33 52 45 46 3a 20 4d 75 74 65 78 20 56 65 72 69 3REF: Mutex Veri
3bd40 66 69 63 61 74 69 6f 6e 20 52 6f 75 74 69 6e 65 fication Routine
3bd50 73 20 7b 48 31 37 30 38 30 7d 20 3c 53 32 30 31 s {H17080} <S201
3bd60 33 30 3e 20 3c 53 33 30 38 30 30 3e 0a 2a 2a 0a 30> <S30800>.**.
3bd70 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d ** The sqlite3_m
3bd80 75 74 65 78 5f 68 65 6c 64 28 29 20 61 6e 64 20 utex_held() and
3bd90 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f sqlite3_mutex_no
3bda0 74 68 65 6c 64 28 29 20 72 6f 75 74 69 6e 65 73 theld() routines
3bdb0 0a 2a 2a 20 61 72 65 20 69 6e 74 65 6e 64 65 64 .** are intended
3bdc0 20 66 6f 72 20 75 73 65 20 69 6e 73 69 64 65 20 for use inside
3bdd0 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 assert() stateme
3bde0 6e 74 73 2e 20 7b 48 31 37 30 38 31 7d 20 54 68 nts. {H17081} Th
3bdf0 65 20 53 51 4c 69 74 65 20 63 6f 72 65 0a 2a 2a e SQLite core.**
3be00 20 6e 65 76 65 72 20 75 73 65 73 20 74 68 65 73 never uses thes
3be10 65 20 72 6f 75 74 69 6e 65 73 20 65 78 63 65 70 e routines excep
3be20 74 20 69 6e 73 69 64 65 20 61 6e 20 61 73 73 65 t inside an asse
3be30 72 74 28 29 20 61 6e 64 20 61 70 70 6c 69 63 61 rt() and applica
3be40 74 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 61 64 76 tions.** are adv
3be50 69 73 65 64 20 74 6f 20 66 6f 6c 6c 6f 77 20 74 ised to follow t
3be60 68 65 20 6c 65 61 64 20 6f 66 20 74 68 65 20 63 he lead of the c
3be70 6f 72 65 2e 20 20 7b 48 31 37 30 38 32 7d 20 54 ore. {H17082} T
3be80 68 65 20 63 6f 72 65 20 6f 6e 6c 79 0a 2a 2a 20 he core only.**
3be90 70 72 6f 76 69 64 65 73 20 69 6d 70 6c 65 6d 65 provides impleme
3bea0 6e 74 61 74 69 6f 6e 73 20 66 6f 72 20 74 68 65 ntations for the
3beb0 73 65 20 72 6f 75 74 69 6e 65 73 20 77 68 65 6e se routines when
3bec0 20 69 74 20 69 73 20 63 6f 6d 70 69 6c 65 64 0a it is compiled.
3bed0 2a 2a 20 77 69 74 68 20 74 68 65 20 53 51 4c 49 ** with the SQLI
3bee0 54 45 5f 44 45 42 55 47 20 66 6c 61 67 2e 20 20 TE_DEBUG flag.
3bef0 7b 41 31 37 30 38 37 7d 20 45 78 74 65 72 6e 61 {A17087} Externa
3bf00 6c 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e l mutex implemen
3bf10 74 61 74 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 6f tations.** are o
3bf20 6e 6c 79 20 72 65 71 75 69 72 65 64 20 74 6f 20 nly required to
3bf30 70 72 6f 76 69 64 65 20 74 68 65 73 65 20 72 6f provide these ro
3bf40 75 74 69 6e 65 73 20 69 66 20 53 51 4c 49 54 45 utines if SQLITE
3bf50 5f 44 45 42 55 47 20 69 73 0a 2a 2a 20 64 65 66 _DEBUG is.** def
3bf60 69 6e 65 64 20 61 6e 64 20 69 66 20 4e 44 45 42 ined and if NDEB
3bf70 55 47 20 69 73 20 6e 6f 74 20 64 65 66 69 6e 65 UG is not define
3bf80 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 30 38 33 d..**.** {H17083
3bf90 7d 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 } These routines
3bfa0 20 73 68 6f 75 6c 64 20 72 65 74 75 72 6e 20 74 should return t
3bfb0 72 75 65 20 69 66 20 74 68 65 20 6d 75 74 65 78 rue if the mutex
3bfc0 20 69 6e 20 74 68 65 69 72 20 61 72 67 75 6d 65 in their argume
3bfd0 6e 74 0a 2a 2a 20 69 73 20 68 65 6c 64 20 6f 72 nt.** is held or
3bfe0 20 6e 6f 74 20 68 65 6c 64 2c 20 72 65 73 70 65 not held, respe
3bff0 63 74 69 76 65 6c 79 2c 20 62 79 20 74 68 65 20 ctively, by the
3c000 63 61 6c 6c 69 6e 67 20 74 68 72 65 61 64 2e 0a calling thread..
3c010 2a 2a 0a 2a 2a 20 7b 58 31 37 30 38 34 7d 20 54 **.** {X17084} T
3c020 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f he implementatio
3c030 6e 20 69 73 20 6e 6f 74 20 72 65 71 75 69 72 65 n is not require
3c040 64 20 74 6f 20 70 72 6f 76 69 64 65 64 20 76 65 d to provided ve
3c050 72 73 69 6f 6e 73 20 6f 66 20 74 68 65 73 65 0a rsions of these.
3c060 2a 2a 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74 ** routines that
3c070 20 61 63 74 75 61 6c 6c 79 20 77 6f 72 6b 2e 20 actually work.
3c080 49 66 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 If the implement
3c090 61 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 70 ation does not p
3c0a0 72 6f 76 69 64 65 20 77 6f 72 6b 69 6e 67 0a 2a rovide working.*
3c0b0 2a 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 74 68 * versions of th
3c0c0 65 73 65 20 72 6f 75 74 69 6e 65 73 2c 20 69 74 ese routines, it
3c0d0 20 73 68 6f 75 6c 64 20 61 74 20 6c 65 61 73 74 should at least
3c0e0 20 70 72 6f 76 69 64 65 20 73 74 75 62 73 20 74 provide stubs t
3c0f0 68 61 74 20 61 6c 77 61 79 73 0a 2a 2a 20 72 65 hat always.** re
3c100 74 75 72 6e 20 74 72 75 65 20 73 6f 20 74 68 61 turn true so tha
3c110 74 20 6f 6e 65 20 64 6f 65 73 20 6e 6f 74 20 67 t one does not g
3c120 65 74 20 73 70 75 72 69 6f 75 73 20 61 73 73 65 et spurious asse
3c130 72 74 69 6f 6e 20 66 61 69 6c 75 72 65 73 2e 0a rtion failures..
3c140 2a 2a 0a 2a 2a 20 7b 48 31 37 30 38 35 7d 20 49 **.** {H17085} I
3c150 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 74 f the argument t
3c160 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f o sqlite3_mutex_
3c170 68 65 6c 64 28 29 20 69 73 20 61 20 4e 55 4c 4c held() is a NULL
3c180 20 70 6f 69 6e 74 65 72 20 74 68 65 6e 0a 2a 2a pointer then.**
3c190 20 74 68 65 20 72 6f 75 74 69 6e 65 20 73 68 6f the routine sho
3c1a0 75 6c 64 20 72 65 74 75 72 6e 20 31 2e 20 20 7b uld return 1. {
3c1b0 45 4e 44 7d 20 54 68 69 73 20 73 65 65 6d 73 20 END} This seems
3c1c0 63 6f 75 6e 74 65 72 2d 69 6e 74 75 69 74 69 76 counter-intuitiv
3c1d0 65 20 73 69 6e 63 65 0a 2a 2a 20 63 6c 65 61 72 e since.** clear
3c1e0 6c 79 20 74 68 65 20 6d 75 74 65 78 20 63 61 6e ly the mutex can
3c1f0 6e 6f 74 20 62 65 20 68 65 6c 64 20 69 66 20 69 not be held if i
3c200 74 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 t does not exist
3c210 2e 20 20 42 75 74 20 74 68 65 0a 2a 2a 20 74 68 . But the.** th
3c220 65 20 72 65 61 73 6f 6e 20 74 68 65 20 6d 75 74 e reason the mut
3c230 65 78 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 ex does not exis
3c240 74 20 69 73 20 62 65 63 61 75 73 65 20 74 68 65 t is because the
3c250 20 62 75 69 6c 64 20 69 73 20 6e 6f 74 0a 2a 2a build is not.**
3c260 20 75 73 69 6e 67 20 6d 75 74 65 78 65 73 2e 20 using mutexes.
3c270 20 41 6e 64 20 77 65 20 64 6f 20 6e 6f 74 20 77 And we do not w
3c280 61 6e 74 20 74 68 65 20 61 73 73 65 72 74 28 29 ant the assert()
3c290 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 0a containing the.
3c2a0 2a 2a 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 ** call to sqlit
3c2b0 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 20 e3_mutex_held()
3c2c0 74 6f 20 66 61 69 6c 2c 20 73 6f 20 61 20 6e 6f to fail, so a no
3c2d0 6e 2d 7a 65 72 6f 20 72 65 74 75 72 6e 20 69 73 n-zero return is
3c2e0 0a 2a 2a 20 74 68 65 20 61 70 70 72 6f 70 72 69 .** the appropri
3c2f0 61 74 65 20 74 68 69 6e 67 20 74 6f 20 64 6f 2e ate thing to do.
3c300 20 20 7b 48 31 37 30 38 36 7d 20 54 68 65 20 73 {H17086} The s
3c310 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 qlite3_mutex_not
3c320 68 65 6c 64 28 29 0a 2a 2a 20 69 6e 74 65 72 66 held().** interf
3c330 61 63 65 20 73 68 6f 75 6c 64 20 61 6c 73 6f 20 ace should also
3c340 72 65 74 75 72 6e 20 31 20 77 68 65 6e 20 67 69 return 1 when gi
3c350 76 65 6e 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 ven a NULL point
3c360 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 er..*/.SQLITE_AP
3c370 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d 75 I int sqlite3_mu
3c380 74 65 78 5f 68 65 6c 64 28 73 71 6c 69 74 65 33 tex_held(sqlite3
3c390 5f 6d 75 74 65 78 2a 29 3b 0a 53 51 4c 49 54 45 _mutex*);.SQLITE
3c3a0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
3c3b0 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 73 _mutex_notheld(s
3c3c0 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 3b 0a qlite3_mutex*);.
3c3d0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
3c3e0 20 4d 75 74 65 78 20 54 79 70 65 73 20 7b 48 31 Mutex Types {H1
3c3f0 37 30 30 31 7d 20 3c 48 31 37 30 30 30 3e 0a 2a 7001} <H17000>.*
3c400 2a 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74 65 *.** The [sqlite
3c410 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 5d 3_mutex_alloc()]
3c420 20 69 6e 74 65 72 66 61 63 65 20 74 61 6b 65 73 interface takes
3c430 20 61 20 73 69 6e 67 6c 65 20 61 72 67 75 6d 65 a single argume
3c440 6e 74 0a 2a 2a 20 77 68 69 63 68 20 69 73 20 6f nt.** which is o
3c450 6e 65 20 6f 66 20 74 68 65 73 65 20 69 6e 74 65 ne of these inte
3c460 67 65 72 20 63 6f 6e 73 74 61 6e 74 73 2e 0a 2a ger constants..*
3c470 2a 0a 2a 2a 20 54 68 65 20 73 65 74 20 6f 66 20 *.** The set of
3c480 73 74 61 74 69 63 20 6d 75 74 65 78 65 73 20 6d static mutexes m
3c490 61 79 20 63 68 61 6e 67 65 20 66 72 6f 6d 20 6f ay change from o
3c4a0 6e 65 20 53 51 4c 69 74 65 20 72 65 6c 65 61 73 ne SQLite releas
3c4b0 65 20 74 6f 20 74 68 65 0a 2a 2a 20 6e 65 78 74 e to the.** next
3c4c0 2e 20 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 . Applications
3c4d0 74 68 61 74 20 6f 76 65 72 72 69 64 65 20 74 68 that override th
3c4e0 65 20 62 75 69 6c 74 2d 69 6e 20 6d 75 74 65 78 e built-in mutex
3c4f0 20 6c 6f 67 69 63 20 6d 75 73 74 20 62 65 0a 2a logic must be.*
3c500 2a 20 70 72 65 70 61 72 65 64 20 74 6f 20 61 63 * prepared to ac
3c510 63 6f 6d 6d 6f 64 61 74 65 20 61 64 64 69 74 69 commodate additi
3c520 6f 6e 61 6c 20 73 74 61 74 69 63 20 6d 75 74 65 onal static mute
3c530 78 65 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 xes..*/.#define
3c540 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 SQLITE_MUTEX_FAS
3c550 54 20 20 20 20 20 20 20 20 20 20 20 20 20 30 0a T 0.
3c560 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d #define SQLITE_M
3c570 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 20 UTEX_RECURSIVE
3c580 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 1.#define
3c590 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 SQLITE_MUTEX_STA
3c5a0 54 49 43 5f 4d 41 53 54 45 52 20 20 20 20 32 0a TIC_MASTER 2.
3c5b0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d #define SQLITE_M
3c5c0 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d 20 UTEX_STATIC_MEM
3c5d0 20 20 20 20 20 20 33 20 20 2f 2a 20 73 71 6c 69 3 /* sqli
3c5e0 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 2a 2f 0a te3_malloc() */.
3c5f0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d #define SQLITE_M
3c600 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d 32 UTEX_STATIC_MEM2
3c610 20 20 20 20 20 20 34 20 20 2f 2a 20 4e 4f 54 20 4 /* NOT
3c620 55 53 45 44 20 2a 2f 0a 23 64 65 66 69 6e 65 20 USED */.#define
3c630 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 SQLITE_MUTEX_STA
3c640 54 49 43 5f 4f 50 45 4e 20 20 20 20 20 20 34 20 TIC_OPEN 4
3c650 20 2f 2a 20 73 71 6c 69 74 65 33 42 74 72 65 65 /* sqlite3Btree
3c660 4f 70 65 6e 28 29 20 2a 2f 0a 23 64 65 66 69 6e Open() */.#defin
3c670 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 e SQLITE_MUTEX_S
3c680 54 41 54 49 43 5f 50 52 4e 47 20 20 20 20 20 20 TATIC_PRNG
3c690 35 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 72 61 5 /* sqlite3_ra
3c6a0 6e 64 6f 6d 28 29 20 2a 2f 0a 23 64 65 66 69 6e ndom() */.#defin
3c6b0 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 e SQLITE_MUTEX_S
3c6c0 54 41 54 49 43 5f 4c 52 55 20 20 20 20 20 20 20 TATIC_LRU
3c6d0 36 20 20 2f 2a 20 6c 72 75 20 70 61 67 65 20 6c 6 /* lru page l
3c6e0 69 73 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 ist */.#define S
3c6f0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 QLITE_MUTEX_STAT
3c700 49 43 5f 4c 52 55 32 20 20 20 20 20 20 37 20 20 IC_LRU2 7
3c710 2f 2a 20 6c 72 75 20 70 61 67 65 20 6c 69 73 74 /* lru page list
3c720 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 */../*.** CAPI3
3c730 52 45 46 3a 20 52 65 74 72 69 65 76 65 20 74 68 REF: Retrieve th
3c740 65 20 6d 75 74 65 78 20 66 6f 72 20 61 20 64 61 e mutex for a da
3c750 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
3c760 6e 20 7b 48 31 37 30 30 32 7d 20 3c 48 31 37 30 n {H17002} <H170
3c770 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 00>.**.** This i
3c780 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 nterface returns
3c790 20 61 20 70 6f 69 6e 74 65 72 20 74 68 65 20 5b a pointer the [
3c7a0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5d 20 6f sqlite3_mutex] o
3c7b0 62 6a 65 63 74 20 74 68 61 74 20 0a 2a 2a 20 73 bject that .** s
3c7c0 65 72 69 61 6c 69 7a 65 73 20 61 63 63 65 73 73 erializes access
3c7d0 20 74 6f 20 74 68 65 20 5b 64 61 74 61 62 61 73 to the [databas
3c7e0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 67 69 e connection] gi
3c7f0 76 65 6e 20 69 6e 20 74 68 65 20 61 72 67 75 6d ven in the argum
3c800 65 6e 74 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 ent.** when the
3c810 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 65 5d [threading mode]
3c820 20 69 73 20 53 65 72 69 61 6c 69 7a 65 64 2e 0a is Serialized..
3c830 2a 2a 20 49 66 20 74 68 65 20 5b 74 68 72 65 61 ** If the [threa
3c840 64 69 6e 67 20 6d 6f 64 65 5d 20 69 73 20 53 69 ding mode] is Si
3c850 6e 67 6c 65 2d 74 68 72 65 61 64 20 6f 72 20 4d ngle-thread or M
3c860 75 6c 74 69 2d 74 68 72 65 61 64 20 74 68 65 6e ulti-thread then
3c870 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 this.** routine
3c880 20 72 65 74 75 72 6e 73 20 61 20 4e 55 4c 4c 20 returns a NULL
3c890 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 pointer..*/.SQLI
3c8a0 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 6d TE_API sqlite3_m
3c8b0 75 74 65 78 20 2a 73 71 6c 69 74 65 33 5f 64 62 utex *sqlite3_db
3c8c0 5f 6d 75 74 65 78 28 73 71 6c 69 74 65 33 2a 29 _mutex(sqlite3*)
3c8d0 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
3c8e0 46 3a 20 4c 6f 77 2d 4c 65 76 65 6c 20 43 6f 6e F: Low-Level Con
3c8f0 74 72 6f 6c 20 4f 66 20 44 61 74 61 62 61 73 65 trol Of Database
3c900 20 46 69 6c 65 73 20 7b 48 31 31 33 30 30 7d 20 Files {H11300}
3c910 3c 53 33 30 38 30 30 3e 0a 2a 2a 0a 2a 2a 20 7b <S30800>.**.** {
3c920 48 31 31 33 30 31 7d 20 54 68 65 20 5b 73 71 6c H11301} The [sql
3c930 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f ite3_file_contro
3c940 6c 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20 6d l()] interface m
3c950 61 6b 65 73 20 61 20 64 69 72 65 63 74 20 63 61 akes a direct ca
3c960 6c 6c 20 74 6f 20 74 68 65 0a 2a 2a 20 78 46 69 ll to the.** xFi
3c970 6c 65 43 6f 6e 74 72 6f 6c 20 6d 65 74 68 6f 64 leControl method
3c980 20 66 6f 72 20 74 68 65 20 5b 73 71 6c 69 74 65 for the [sqlite
3c990 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d 20 6f 62 3_io_methods] ob
3c9a0 6a 65 63 74 20 61 73 73 6f 63 69 61 74 65 64 0a ject associated.
3c9b0 2a 2a 20 77 69 74 68 20 61 20 70 61 72 74 69 63 ** with a partic
3c9c0 75 6c 61 72 20 64 61 74 61 62 61 73 65 20 69 64 ular database id
3c9d0 65 6e 74 69 66 69 65 64 20 62 79 20 74 68 65 20 entified by the
3c9e0 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2e second argument.
3c9f0 20 7b 48 31 31 33 30 32 7d 20 54 68 65 0a 2a 2a {H11302} The.**
3ca00 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 name of the dat
3ca10 61 62 61 73 65 20 69 73 20 74 68 65 20 6e 61 6d abase is the nam
3ca20 65 20 61 73 73 69 67 6e 65 64 20 74 6f 20 74 68 e assigned to th
3ca30 65 20 64 61 74 61 62 61 73 65 20 62 79 20 74 68 e database by th
3ca40 65 0a 2a 2a 20 3c 61 20 68 72 65 66 3d 22 6c 61 e.** <a href="la
3ca50 6e 67 5f 61 74 74 61 63 68 2e 68 74 6d 6c 22 3e ng_attach.html">
3ca60 41 54 54 41 43 48 3c 2f 61 3e 20 53 51 4c 20 63 ATTACH</a> SQL c
3ca70 6f 6d 6d 61 6e 64 20 74 68 61 74 20 6f 70 65 6e ommand that open
3ca80 65 64 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 ed the.** databa
3ca90 73 65 2e 20 7b 48 31 31 33 30 33 7d 20 54 6f 20 se. {H11303} To
3caa0 63 6f 6e 74 72 6f 6c 20 74 68 65 20 6d 61 69 6e control the main
3cab0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 database file,
3cac0 75 73 65 20 74 68 65 20 6e 61 6d 65 20 22 6d 61 use the name "ma
3cad0 69 6e 22 0a 2a 2a 20 6f 72 20 61 20 4e 55 4c 4c in".** or a NULL
3cae0 20 70 6f 69 6e 74 65 72 2e 20 7b 48 31 31 33 30 pointer. {H1130
3caf0 34 7d 20 54 68 65 20 74 68 69 72 64 20 61 6e 64 4} The third and
3cb00 20 66 6f 75 72 74 68 20 70 61 72 61 6d 65 74 65 fourth paramete
3cb10 72 73 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69 rs to this routi
3cb20 6e 65 0a 2a 2a 20 61 72 65 20 70 61 73 73 65 64 ne.** are passed
3cb30 20 64 69 72 65 63 74 6c 79 20 74 68 72 6f 75 67 directly throug
3cb40 68 20 74 6f 20 74 68 65 20 73 65 63 6f 6e 64 20 h to the second
3cb50 61 6e 64 20 74 68 69 72 64 20 70 61 72 61 6d 65 and third parame
3cb60 74 65 72 73 20 6f 66 0a 2a 2a 20 74 68 65 20 78 ters of.** the x
3cb70 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 6d 65 74 68 FileControl meth
3cb80 6f 64 2e 20 20 7b 48 31 31 33 30 35 7d 20 54 68 od. {H11305} Th
3cb90 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f e return value o
3cba0 66 20 74 68 65 20 78 46 69 6c 65 43 6f 6e 74 72 f the xFileContr
3cbb0 6f 6c 0a 2a 2a 20 6d 65 74 68 6f 64 20 62 65 63 ol.** method bec
3cbc0 6f 6d 65 73 20 74 68 65 20 72 65 74 75 72 6e 20 omes the return
3cbd0 76 61 6c 75 65 20 6f 66 20 74 68 69 73 20 72 6f value of this ro
3cbe0 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 utine..**.** {H1
3cbf0 31 33 30 36 7d 20 49 66 20 74 68 65 20 73 65 63 1306} If the sec
3cc00 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 28 7a ond parameter (z
3cc10 44 62 4e 61 6d 65 29 20 64 6f 65 73 20 6e 6f 74 DbName) does not
3cc20 20 6d 61 74 63 68 20 74 68 65 20 6e 61 6d 65 20 match the name
3cc30 6f 66 20 61 6e 79 0a 2a 2a 20 6f 70 65 6e 20 64 of any.** open d
3cc40 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68 atabase file, th
3cc50 65 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 en SQLITE_ERROR
3cc60 69 73 20 72 65 74 75 72 6e 65 64 2e 20 7b 48 31 is returned. {H1
3cc70 31 33 30 37 7d 20 54 68 69 73 20 65 72 72 6f 72 1307} This error
3cc80 0a 2a 2a 20 63 6f 64 65 20 69 73 20 6e 6f 74 20 .** code is not
3cc90 72 65 6d 65 6d 62 65 72 65 64 20 61 6e 64 20 77 remembered and w
3cca0 69 6c 6c 20 6e 6f 74 20 62 65 20 72 65 63 61 6c ill not be recal
3ccb0 6c 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33 5f led by [sqlite3_
3ccc0 65 72 72 63 6f 64 65 28 29 5d 0a 2a 2a 20 6f 72 errcode()].** or
3ccd0 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 [sqlite3_errmsg
3cce0 28 29 5d 2e 20 7b 41 31 31 33 30 38 7d 20 54 68 ()]. {A11308} Th
3ccf0 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 78 46 69 e underlying xFi
3cd00 6c 65 43 6f 6e 74 72 6f 6c 20 6d 65 74 68 6f 64 leControl method
3cd10 20 6d 69 67 68 74 0a 2a 2a 20 61 6c 73 6f 20 72 might.** also r
3cd20 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 eturn SQLITE_ERR
3cd30 4f 52 2e 20 20 7b 41 31 31 33 30 39 7d 20 54 68 OR. {A11309} Th
3cd40 65 72 65 20 69 73 20 6e 6f 20 77 61 79 20 74 6f ere is no way to
3cd50 20 64 69 73 74 69 6e 67 75 69 73 68 20 62 65 74 distinguish bet
3cd60 77 65 65 6e 0a 2a 2a 20 61 6e 20 69 6e 63 6f 72 ween.** an incor
3cd70 72 65 63 74 20 7a 44 62 4e 61 6d 65 20 61 6e 64 rect zDbName and
3cd80 20 61 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 an SQLITE_ERROR
3cd90 20 72 65 74 75 72 6e 20 66 72 6f 6d 20 74 68 65 return from the
3cda0 20 75 6e 64 65 72 6c 79 69 6e 67 0a 2a 2a 20 78 underlying.** x
3cdb0 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 6d 65 74 68 FileControl meth
3cdc0 6f 64 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 od. {END}.**.**
3cdd0 53 65 65 20 61 6c 73 6f 3a 20 5b 53 51 4c 49 54 See also: [SQLIT
3cde0 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54 E_FCNTL_LOCKSTAT
3cdf0 45 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 E].*/.SQLITE_API
3ce00 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 66 69 6c int sqlite3_fil
3ce10 65 5f 63 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65 e_control(sqlite
3ce20 33 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 3*, const char *
3ce30 7a 44 62 4e 61 6d 65 2c 20 69 6e 74 20 6f 70 2c zDbName, int op,
3ce40 20 76 6f 69 64 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 void*);../*.**
3ce50 43 41 50 49 33 52 45 46 3a 20 54 65 73 74 69 6e CAPI3REF: Testin
3ce60 67 20 49 6e 74 65 72 66 61 63 65 20 7b 48 31 31 g Interface {H11
3ce70 34 30 30 7d 20 3c 53 33 30 38 30 30 3e 0a 2a 2a 400} <S30800>.**
3ce80 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
3ce90 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 29 20 69 test_control() i
3cea0 6e 74 65 72 66 61 63 65 20 69 73 20 75 73 65 64 nterface is used
3ceb0 20 74 6f 20 72 65 61 64 20 6f 75 74 20 69 6e 74 to read out int
3cec0 65 72 6e 61 6c 0a 2a 2a 20 73 74 61 74 65 20 6f ernal.** state o
3ced0 66 20 53 51 4c 69 74 65 20 61 6e 64 20 74 6f 20 f SQLite and to
3cee0 69 6e 6a 65 63 74 20 66 61 75 6c 74 73 20 69 6e inject faults in
3cef0 74 6f 20 53 51 4c 69 74 65 20 66 6f 72 20 74 65 to SQLite for te
3cf00 73 74 69 6e 67 0a 2a 2a 20 70 75 72 70 6f 73 65 sting.** purpose
3cf10 73 2e 20 20 54 68 65 20 66 69 72 73 74 20 70 61 s. The first pa
3cf20 72 61 6d 65 74 65 72 20 69 73 20 61 6e 20 6f 70 rameter is an op
3cf30 65 72 61 74 69 6f 6e 20 63 6f 64 65 20 74 68 61 eration code tha
3cf40 74 20 64 65 74 65 72 6d 69 6e 65 73 0a 2a 2a 20 t determines.**
3cf50 74 68 65 20 6e 75 6d 62 65 72 2c 20 6d 65 61 6e the number, mean
3cf60 69 6e 67 2c 20 61 6e 64 20 6f 70 65 72 61 74 69 ing, and operati
3cf70 6f 6e 20 6f 66 20 61 6c 6c 20 73 75 62 73 65 71 on of all subseq
3cf80 75 65 6e 74 20 70 61 72 61 6d 65 74 65 72 73 2e uent parameters.
3cf90 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 .**.** This inte
3cfa0 72 66 61 63 65 20 69 73 20 6e 6f 74 20 66 6f 72 rface is not for
3cfb0 20 75 73 65 20 62 79 20 61 70 70 6c 69 63 61 74 use by applicat
3cfc0 69 6f 6e 73 2e 20 20 49 74 20 65 78 69 73 74 73 ions. It exists
3cfd0 20 73 6f 6c 65 6c 79 0a 2a 2a 20 66 6f 72 20 76 solely.** for v
3cfe0 65 72 69 66 79 69 6e 67 20 74 68 65 20 63 6f 72 erifying the cor
3cff0 72 65 63 74 20 6f 70 65 72 61 74 69 6f 6e 20 6f rect operation o
3d000 66 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 f the SQLite lib
3d010 72 61 72 79 2e 20 20 44 65 70 65 6e 64 69 6e 67 rary. Depending
3d020 0a 2a 2a 20 6f 6e 20 68 6f 77 20 74 68 65 20 53 .** on how the S
3d030 51 4c 69 74 65 20 6c 69 62 72 61 72 79 20 69 73 QLite library is
3d040 20 63 6f 6d 70 69 6c 65 64 2c 20 74 68 69 73 20 compiled, this
3d050 69 6e 74 65 72 66 61 63 65 20 6d 69 67 68 74 20 interface might
3d060 6e 6f 74 20 65 78 69 73 74 2e 0a 2a 2a 0a 2a 2a not exist..**.**
3d070 20 54 68 65 20 64 65 74 61 69 6c 73 20 6f 66 20 The details of
3d080 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 63 6f the operation co
3d090 64 65 73 2c 20 74 68 65 69 72 20 6d 65 61 6e 69 des, their meani
3d0a0 6e 67 73 2c 20 74 68 65 20 70 61 72 61 6d 65 74 ngs, the paramet
3d0b0 65 72 73 0a 2a 2a 20 74 68 65 79 20 74 61 6b 65 ers.** they take
3d0c0 2c 20 61 6e 64 20 77 68 61 74 20 74 68 65 79 20 , and what they
3d0d0 64 6f 20 61 72 65 20 61 6c 6c 20 73 75 62 6a 65 do are all subje
3d0e0 63 74 20 74 6f 20 63 68 61 6e 67 65 20 77 69 74 ct to change wit
3d0f0 68 6f 75 74 20 6e 6f 74 69 63 65 2e 0a 2a 2a 20 hout notice..**
3d100 55 6e 6c 69 6b 65 20 6d 6f 73 74 20 6f 66 20 74 Unlike most of t
3d110 68 65 20 53 51 4c 69 74 65 20 41 50 49 2c 20 74 he SQLite API, t
3d120 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
3d130 6e 6f 74 20 67 75 61 72 61 6e 74 65 65 64 20 74 not guaranteed t
3d140 6f 0a 2a 2a 20 6f 70 65 72 61 74 65 20 63 6f 6e o.** operate con
3d150 73 69 73 74 65 6e 74 6c 79 20 66 72 6f 6d 20 6f sistently from o
3d160 6e 65 20 72 65 6c 65 61 73 65 20 74 6f 20 74 68 ne release to th
3d170 65 20 6e 65 78 74 2e 0a 2a 2f 0a 53 51 4c 49 54 e next..*/.SQLIT
3d180 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
3d190 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 69 3_test_control(i
3d1a0 6e 74 20 6f 70 2c 20 2e 2e 2e 29 3b 0a 0a 2f 2a nt op, ...);../*
3d1b0 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 54 65 .** CAPI3REF: Te
3d1c0 73 74 69 6e 67 20 49 6e 74 65 72 66 61 63 65 20 sting Interface
3d1d0 4f 70 65 72 61 74 69 6f 6e 20 43 6f 64 65 73 20 Operation Codes
3d1e0 7b 48 31 31 34 31 30 7d 20 3c 48 31 31 34 30 30 {H11410} <H11400
3d1f0 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 63 6f >.**.** These co
3d200 6e 73 74 61 6e 74 73 20 61 72 65 20 74 68 65 20 nstants are the
3d210 76 61 6c 69 64 20 6f 70 65 72 61 74 69 6f 6e 20 valid operation
3d220 63 6f 64 65 20 70 61 72 61 6d 65 74 65 72 73 20 code parameters
3d230 75 73 65 64 0a 2a 2a 20 61 73 20 74 68 65 20 66 used.** as the f
3d240 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f irst argument to
3d250 20 5b 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 [sqlite3_test_c
3d260 6f 6e 74 72 6f 6c 28 29 5d 2e 0a 2a 2a 0a 2a 2a ontrol()]..**.**
3d270 20 54 68 65 73 65 20 70 61 72 61 6d 65 74 65 72 These parameter
3d280 73 20 61 6e 64 20 74 68 65 69 72 20 6d 65 61 6e s and their mean
3d290 69 6e 67 73 20 61 72 65 20 73 75 62 6a 65 63 74 ings are subject
3d2a0 20 74 6f 20 63 68 61 6e 67 65 0a 2a 2a 20 77 69 to change.** wi
3d2b0 74 68 6f 75 74 20 6e 6f 74 69 63 65 2e 20 20 54 thout notice. T
3d2c0 68 65 73 65 20 76 61 6c 75 65 73 20 61 72 65 20 hese values are
3d2d0 66 6f 72 20 74 65 73 74 69 6e 67 20 70 75 72 70 for testing purp
3d2e0 6f 73 65 73 20 6f 6e 6c 79 2e 0a 2a 2a 20 41 70 oses only..** Ap
3d2f0 70 6c 69 63 61 74 69 6f 6e 73 20 73 68 6f 75 6c plications shoul
3d300 64 20 6e 6f 74 20 75 73 65 20 61 6e 79 20 6f 66 d not use any of
3d310 20 74 68 65 73 65 20 70 61 72 61 6d 65 74 65 72 these parameter
3d320 73 20 6f 72 20 74 68 65 0a 2a 2a 20 5b 73 71 6c s or the.** [sql
3d330 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f ite3_test_contro
3d340 6c 28 29 5d 20 69 6e 74 65 72 66 61 63 65 2e 0a l()] interface..
3d350 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
3d360 45 5f 54 45 53 54 43 54 52 4c 5f 50 52 4e 47 5f E_TESTCTRL_PRNG_
3d370 53 41 56 45 20 20 20 20 20 20 20 20 20 20 20 20 SAVE
3d380 20 20 20 20 35 0a 23 64 65 66 69 6e 65 20 53 51 5.#define SQ
3d390 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 50 52 LITE_TESTCTRL_PR
3d3a0 4e 47 5f 52 45 53 54 4f 52 45 20 20 20 20 20 20 NG_RESTORE
3d3b0 20 20 20 20 20 20 20 36 0a 23 64 65 66 69 6e 65 6.#define
3d3c0 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c SQLITE_TESTCTRL
3d3d0 5f 50 52 4e 47 5f 52 45 53 45 54 20 20 20 20 20 _PRNG_RESET
3d3e0 20 20 20 20 20 20 20 20 20 20 37 0a 23 64 65 66 7.#def
3d3f0 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43 ine SQLITE_TESTC
3d400 54 52 4c 5f 42 49 54 56 45 43 5f 54 45 53 54 20 TRL_BITVEC_TEST
3d410 20 20 20 20 20 20 20 20 20 20 20 20 20 38 0a 23 8.#
3d420 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 define SQLITE_TE
3d430 53 54 43 54 52 4c 5f 46 41 55 4c 54 5f 49 4e 53 STCTRL_FAULT_INS
3d440 54 41 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 TALL
3d450 39 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 9.#define SQLITE
3d460 5f 54 45 53 54 43 54 52 4c 5f 42 45 4e 49 47 4e _TESTCTRL_BENIGN
3d470 5f 4d 41 4c 4c 4f 43 5f 48 4f 4f 4b 53 20 20 20 _MALLOC_HOOKS
3d480 20 20 31 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 10.#define SQL
3d490 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 50 45 4e ITE_TESTCTRL_PEN
3d4a0 44 49 4e 47 5f 42 59 54 45 20 20 20 20 20 20 20 DING_BYTE
3d4b0 20 20 20 20 20 31 31 0a 23 64 65 66 69 6e 65 20 11.#define
3d4c0 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f SQLITE_TESTCTRL_
3d4d0 41 53 53 45 52 54 20 20 20 20 20 20 20 20 20 20 ASSERT
3d4e0 20 20 20 20 20 20 20 20 31 32 0a 23 64 65 66 69 12.#defi
3d4f0 6e 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54 ne SQLITE_TESTCT
3d500 52 4c 5f 41 4c 57 41 59 53 20 20 20 20 20 20 20 RL_ALWAYS
3d510 20 20 20 20 20 20 20 20 20 20 20 31 33 0a 23 64 13.#d
3d520 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 53 efine SQLITE_TES
3d530 54 43 54 52 4c 5f 52 45 53 45 52 56 45 20 20 20 TCTRL_RESERVE
3d540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34 14
3d550 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
3d560 3a 20 53 51 4c 69 74 65 20 52 75 6e 74 69 6d 65 : SQLite Runtime
3d570 20 53 74 61 74 75 73 20 7b 48 31 37 32 30 30 7d Status {H17200}
3d580 20 3c 53 36 30 32 30 30 3e 0a 2a 2a 20 45 58 50 <S60200>.** EXP
3d590 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 ERIMENTAL.**.**
3d5a0 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 69 This interface i
3d5b0 73 20 75 73 65 64 20 74 6f 20 72 65 74 72 69 65 s used to retrie
3d5c0 76 65 20 72 75 6e 74 69 6d 65 20 73 74 61 74 75 ve runtime statu
3d5d0 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a s information.**
3d5e0 20 61 62 6f 75 74 20 74 68 65 20 70 72 65 66 6f about the prefo
3d5f0 72 6d 61 6e 63 65 20 6f 66 20 53 51 4c 69 74 65 rmance of SQLite
3d600 2c 20 61 6e 64 20 6f 70 74 69 6f 6e 61 6c 6c 79 , and optionally
3d610 20 74 6f 20 72 65 73 65 74 20 76 61 72 69 6f 75 to reset variou
3d620 73 0a 2a 2a 20 68 69 67 68 77 61 74 65 72 20 6d s.** highwater m
3d630 61 72 6b 73 2e 20 20 54 68 65 20 66 69 72 73 74 arks. The first
3d640 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 6e 20 argument is an
3d650 69 6e 74 65 67 65 72 20 63 6f 64 65 20 66 6f 72 integer code for
3d660 0a 2a 2a 20 74 68 65 20 73 70 65 63 69 66 69 63 .** the specific
3d670 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 6d 65 parameter to me
3d680 61 73 75 72 65 2e 20 20 52 65 63 6f 67 6e 69 7a asure. Recogniz
3d690 65 64 20 69 6e 74 65 67 65 72 20 63 6f 64 65 73 ed integer codes
3d6a0 0a 2a 2a 20 61 72 65 20 6f 66 20 74 68 65 20 66 .** are of the f
3d6b0 6f 72 6d 20 5b 53 51 4c 49 54 45 5f 53 54 41 54 orm [SQLITE_STAT
3d6c0 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 20 7c US_MEMORY_USED |
3d6d0 20 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 2e SQLITE_STATUS_.
3d6e0 2e 2e 5d 2e 0a 2a 2a 20 54 68 65 20 63 75 72 72 ..]..** The curr
3d6f0 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 74 68 65 ent value of the
3d700 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 72 65 parameter is re
3d710 74 75 72 6e 65 64 20 69 6e 74 6f 20 2a 70 43 75 turned into *pCu
3d720 72 72 65 6e 74 2e 0a 2a 2a 20 54 68 65 20 68 69 rrent..** The hi
3d730 67 68 65 73 74 20 72 65 63 6f 72 64 65 64 20 76 ghest recorded v
3d740 61 6c 75 65 20 69 73 20 72 65 74 75 72 6e 65 64 alue is returned
3d750 20 69 6e 20 2a 70 48 69 67 68 77 61 74 65 72 2e in *pHighwater.
3d760 20 20 49 66 20 74 68 65 0a 2a 2a 20 72 65 73 65 If the.** rese
3d770 74 46 6c 61 67 20 69 73 20 74 72 75 65 2c 20 74 tFlag is true, t
3d780 68 65 6e 20 74 68 65 20 68 69 67 68 65 73 74 20 hen the highest
3d790 72 65 63 6f 72 64 20 76 61 6c 75 65 20 69 73 20 record value is
3d7a0 72 65 73 65 74 20 61 66 74 65 72 0a 2a 2a 20 2a reset after.** *
3d7b0 70 48 69 67 68 77 61 74 65 72 20 69 73 20 77 72 pHighwater is wr
3d7c0 69 74 74 65 6e 2e 20 53 6f 6d 65 20 70 61 72 61 itten. Some para
3d7d0 6d 65 74 65 72 73 20 64 6f 20 6e 6f 74 20 72 65 meters do not re
3d7e0 63 6f 72 64 20 74 68 65 20 68 69 67 68 65 73 74 cord the highest
3d7f0 0a 2a 2a 20 76 61 6c 75 65 2e 20 20 46 6f 72 20 .** value. For
3d800 74 68 6f 73 65 20 70 61 72 61 6d 65 74 65 72 73 those parameters
3d810 0a 2a 2a 20 6e 6f 74 68 69 6e 67 20 69 73 20 77 .** nothing is w
3d820 72 69 74 74 65 6e 20 69 6e 74 6f 20 2a 70 48 69 ritten into *pHi
3d830 67 68 77 61 74 65 72 20 61 6e 64 20 74 68 65 20 ghwater and the
3d840 72 65 73 65 74 46 6c 61 67 20 69 73 20 69 67 6e resetFlag is ign
3d850 6f 72 65 64 2e 0a 2a 2a 20 4f 74 68 65 72 20 70 ored..** Other p
3d860 61 72 61 6d 65 74 65 72 73 20 72 65 63 6f 72 64 arameters record
3d870 20 6f 6e 6c 79 20 74 68 65 20 68 69 67 68 77 61 only the highwa
3d880 74 65 72 20 6d 61 72 6b 20 61 6e 64 20 6e 6f 74 ter mark and not
3d890 20 74 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20 the current.**
3d8a0 76 61 6c 75 65 2e 20 20 46 6f 72 20 74 68 65 73 value. For thes
3d8b0 65 20 6c 61 74 74 65 72 20 70 61 72 61 6d 65 74 e latter paramet
3d8c0 65 72 73 20 6e 6f 74 68 69 6e 67 20 69 73 20 77 ers nothing is w
3d8d0 72 69 74 74 65 6e 20 69 6e 74 6f 20 2a 70 43 75 ritten into *pCu
3d8e0 72 72 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 rrent..**.** Thi
3d8f0 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e s routine return
3d900 73 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 s SQLITE_OK on s
3d910 75 63 63 65 73 73 20 61 6e 64 20 61 20 6e 6f 6e uccess and a non
3d920 2d 7a 65 72 6f 0a 2a 2a 20 5b 65 72 72 6f 72 20 -zero.** [error
3d930 63 6f 64 65 5d 20 6f 6e 20 66 61 69 6c 75 72 65 code] on failure
3d940 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
3d950 74 69 6e 65 20 69 73 20 74 68 72 65 61 64 73 61 tine is threadsa
3d960 66 65 20 62 75 74 20 69 73 20 6e 6f 74 20 61 74 fe but is not at
3d970 6f 6d 69 63 2e 20 20 54 68 69 73 20 72 6f 75 74 omic. This rout
3d980 69 6e 65 20 63 61 6e 20 62 65 0a 2a 2a 20 63 61 ine can be.** ca
3d990 6c 6c 65 64 20 77 68 69 6c 65 20 6f 74 68 65 72 lled while other
3d9a0 20 74 68 72 65 61 64 73 20 61 72 65 20 72 75 6e threads are run
3d9b0 6e 69 6e 67 20 74 68 65 20 73 61 6d 65 20 6f 72 ning the same or
3d9c0 20 64 69 66 66 65 72 65 6e 74 20 53 51 4c 69 74 different SQLit
3d9d0 65 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 73 2e e.** interfaces.
3d9e0 20 20 48 6f 77 65 76 65 72 20 74 68 65 20 76 61 However the va
3d9f0 6c 75 65 73 20 72 65 74 75 72 6e 65 64 20 69 6e lues returned in
3da00 20 2a 70 43 75 72 72 65 6e 74 20 61 6e 64 0a 2a *pCurrent and.*
3da10 2a 20 2a 70 48 69 67 68 77 61 74 65 72 20 72 65 * *pHighwater re
3da20 66 6c 65 63 74 20 74 68 65 20 73 74 61 74 75 73 flect the status
3da30 20 6f 66 20 53 51 4c 69 74 65 20 61 74 20 64 69 of SQLite at di
3da40 66 66 65 72 65 6e 74 20 70 6f 69 6e 74 73 20 69 fferent points i
3da50 6e 20 74 69 6d 65 0a 2a 2a 20 61 6e 64 20 69 74 n time.** and it
3da60 20 69 73 20 70 6f 73 73 69 62 6c 65 20 74 68 61 is possible tha
3da70 74 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 t another thread
3da80 20 6d 69 67 68 74 20 63 68 61 6e 67 65 20 74 68 might change th
3da90 65 20 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20 69 e parameter.** i
3daa0 6e 20 62 65 74 77 65 65 6e 20 74 68 65 20 74 69 n between the ti
3dab0 6d 65 73 20 77 68 65 6e 20 2a 70 43 75 72 72 65 mes when *pCurre
3dac0 6e 74 20 61 6e 64 20 2a 70 48 69 67 68 77 61 74 nt and *pHighwat
3dad0 65 72 20 61 72 65 20 77 72 69 74 74 65 6e 2e 0a er are written..
3dae0 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 **.** See also:
3daf0 5b 73 71 6c 69 74 65 33 5f 64 62 5f 73 74 61 74 [sqlite3_db_stat
3db00 75 73 28 29 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f us()].*/.SQLITE_
3db10 41 50 49 20 53 51 4c 49 54 45 5f 45 58 50 45 52 API SQLITE_EXPER
3db20 49 4d 45 4e 54 41 4c 20 69 6e 74 20 73 71 6c 69 IMENTAL int sqli
3db30 74 65 33 5f 73 74 61 74 75 73 28 69 6e 74 20 6f te3_status(int o
3db40 70 2c 20 69 6e 74 20 2a 70 43 75 72 72 65 6e 74 p, int *pCurrent
3db50 2c 20 69 6e 74 20 2a 70 48 69 67 68 77 61 74 65 , int *pHighwate
3db60 72 2c 20 69 6e 74 20 72 65 73 65 74 46 6c 61 67 r, int resetFlag
3db70 29 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 );.../*.** CAPI3
3db80 52 45 46 3a 20 53 74 61 74 75 73 20 50 61 72 61 REF: Status Para
3db90 6d 65 74 65 72 73 20 7b 48 31 37 32 35 30 7d 20 meters {H17250}
3dba0 3c 48 31 37 32 30 30 3e 0a 2a 2a 20 45 58 50 45 <H17200>.** EXPE
3dbb0 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 RIMENTAL.**.** T
3dbc0 68 65 73 65 20 69 6e 74 65 67 65 72 20 63 6f 6e hese integer con
3dbd0 73 74 61 6e 74 73 20 64 65 73 69 67 6e 61 74 65 stants designate
3dbe0 20 76 61 72 69 6f 75 73 20 72 75 6e 2d 74 69 6d various run-tim
3dbf0 65 20 73 74 61 74 75 73 20 70 61 72 61 6d 65 74 e status paramet
3dc00 65 72 73 0a 2a 2a 20 74 68 61 74 20 63 61 6e 20 ers.** that can
3dc10 62 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 5b be returned by [
3dc20 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73 28 29 sqlite3_status()
3dc30 5d 2e 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e 0a 2a 2a ]..**.** <dl>.**
3dc40 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 41 54 <dt>SQLITE_STAT
3dc50 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 3c 2f US_MEMORY_USED</
3dc60 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 dt>.** <dd>This
3dc70 70 61 72 61 6d 65 74 65 72 20 69 73 20 74 68 65 parameter is the
3dc80 20 63 75 72 72 65 6e 74 20 61 6d 6f 75 6e 74 20 current amount
3dc90 6f 66 20 6d 65 6d 6f 72 79 20 63 68 65 63 6b 65 of memory checke
3dca0 64 20 6f 75 74 0a 2a 2a 20 75 73 69 6e 67 20 5b d out.** using [
3dcb0 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 sqlite3_malloc()
3dcc0 5d 2c 20 65 69 74 68 65 72 20 64 69 72 65 63 74 ], either direct
3dcd0 6c 79 20 6f 72 20 69 6e 64 69 72 65 63 74 6c 79 ly or indirectly
3dce0 2e 20 20 54 68 65 0a 2a 2a 20 66 69 67 75 72 65 . The.** figure
3dcf0 20 69 6e 63 6c 75 64 65 73 20 63 61 6c 6c 73 20 includes calls
3dd00 6d 61 64 65 20 74 6f 20 5b 73 71 6c 69 74 65 33 made to [sqlite3
3dd10 5f 6d 61 6c 6c 6f 63 28 29 5d 20 62 79 20 74 68 _malloc()] by th
3dd20 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 0a 2a 2a e application.**
3dd30 20 61 6e 64 20 69 6e 74 65 72 6e 61 6c 20 6d 65 and internal me
3dd40 6d 6f 72 79 20 75 73 61 67 65 20 62 79 20 74 68 mory usage by th
3dd50 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 e SQLite library
3dd60 2e 20 20 53 63 72 61 74 63 68 20 6d 65 6d 6f 72 . Scratch memor
3dd70 79 0a 2a 2a 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 y.** controlled
3dd80 62 79 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 by [SQLITE_CONFI
3dd90 47 5f 53 43 52 41 54 43 48 5d 20 61 6e 64 20 61 G_SCRATCH] and a
3dda0 75 78 69 6c 69 61 72 79 20 70 61 67 65 2d 63 61 uxiliary page-ca
3ddb0 63 68 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20 63 6f che.** memory co
3ddc0 6e 74 72 6f 6c 6c 65 64 20 62 79 20 5b 53 51 4c ntrolled by [SQL
3ddd0 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 ITE_CONFIG_PAGEC
3dde0 41 43 48 45 5d 20 69 73 20 6e 6f 74 20 69 6e 63 ACHE] is not inc
3ddf0 6c 75 64 65 64 20 69 6e 0a 2a 2a 20 74 68 69 73 luded in.** this
3de00 20 70 61 72 61 6d 65 74 65 72 2e 20 20 54 68 65 parameter. The
3de10 20 61 6d 6f 75 6e 74 20 72 65 74 75 72 6e 65 64 amount returned
3de20 20 69 73 20 74 68 65 20 73 75 6d 20 6f 66 20 74 is the sum of t
3de30 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a he allocation.**
3de40 20 73 69 7a 65 73 20 61 73 20 72 65 70 6f 72 74 sizes as report
3de50 65 64 20 62 79 20 74 68 65 20 78 53 69 7a 65 20 ed by the xSize
3de60 6d 65 74 68 6f 64 20 69 6e 20 5b 73 71 6c 69 74 method in [sqlit
3de70 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 5d 2e e3_mem_methods].
3de80 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e </dd>.**.** <dt>
3de90 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 41 SQLITE_STATUS_MA
3dea0 4c 4c 4f 43 5f 53 49 5a 45 3c 2f 64 74 3e 0a 2a LLOC_SIZE</dt>.*
3deb0 2a 20 3c 64 64 3e 54 68 69 73 20 70 61 72 61 6d * <dd>This param
3dec0 65 74 65 72 20 72 65 63 6f 72 64 73 20 74 68 65 eter records the
3ded0 20 6c 61 72 67 65 73 74 20 6d 65 6d 6f 72 79 20 largest memory
3dee0 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 71 75 65 allocation reque
3def0 73 74 0a 2a 2a 20 68 61 6e 64 65 64 20 74 6f 20 st.** handed to
3df00 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 [sqlite3_malloc(
3df10 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 72 )] or [sqlite3_r
3df20 65 61 6c 6c 6f 63 28 29 5d 20 28 6f 72 20 74 68 ealloc()] (or th
3df30 65 69 72 0a 2a 2a 20 69 6e 74 65 72 6e 61 6c 20 eir.** internal
3df40 65 71 75 69 76 61 6c 65 6e 74 73 29 2e 20 20 4f equivalents). O
3df50 6e 6c 79 20 74 68 65 20 76 61 6c 75 65 20 72 65 nly the value re
3df60 74 75 72 6e 65 64 20 69 6e 20 74 68 65 0a 2a 2a turned in the.**
3df70 20 2a 70 48 69 67 68 77 61 74 65 72 20 70 61 72 *pHighwater par
3df80 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c 69 74 ameter to [sqlit
3df90 65 33 5f 73 74 61 74 75 73 28 29 5d 20 69 73 20 e3_status()] is
3dfa0 6f 66 20 69 6e 74 65 72 65 73 74 2e 20 20 0a 2a of interest. .*
3dfb0 2a 20 54 68 65 20 76 61 6c 75 65 20 77 72 69 74 * The value writ
3dfc0 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 2a 70 43 ten into the *pC
3dfd0 75 72 72 65 6e 74 20 70 61 72 61 6d 65 74 65 72 urrent parameter
3dfe0 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 3c 2f is undefined.</
3dff0 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 dd>.**.** <dt>SQ
3e000 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45 LITE_STATUS_PAGE
3e010 43 41 43 48 45 5f 55 53 45 44 3c 2f 64 74 3e 0a CACHE_USED</dt>.
3e020 2a 2a 20 3c 64 64 3e 54 68 69 73 20 70 61 72 61 ** <dd>This para
3e030 6d 65 74 65 72 20 72 65 74 75 72 6e 73 20 74 68 meter returns th
3e040 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 e number of page
3e050 73 20 75 73 65 64 20 6f 75 74 20 6f 66 20 74 68 s used out of th
3e060 65 0a 2a 2a 20 5b 70 61 67 65 63 61 63 68 65 20 e.** [pagecache
3e070 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 memory allocator
3e080 5d 20 74 68 61 74 20 77 61 73 20 63 6f 6e 66 69 ] that was confi
3e090 67 75 72 65 64 20 75 73 69 6e 67 20 0a 2a 2a 20 gured using .**
3e0a0 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 [SQLITE_CONFIG_P
3e0b0 41 47 45 43 41 43 48 45 5d 2e 20 20 54 68 65 0a AGECACHE]. The.
3e0c0 2a 2a 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 ** value returne
3e0d0 64 20 69 73 20 69 6e 20 70 61 67 65 73 2c 20 6e d is in pages, n
3e0e0 6f 74 20 69 6e 20 62 79 74 65 73 2e 3c 2f 64 64 ot in bytes.</dd
3e0f0 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 >.**.** <dt>SQLI
3e100 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45 43 41 TE_STATUS_PAGECA
3e110 43 48 45 5f 4f 56 45 52 46 4c 4f 57 3c 2f 64 74 CHE_OVERFLOW</dt
3e120 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 70 61 >.** <dd>This pa
3e130 72 61 6d 65 74 65 72 20 72 65 74 75 72 6e 73 20 rameter returns
3e140 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 the number of by
3e150 74 65 73 20 6f 66 20 70 61 67 65 20 63 61 63 68 tes of page cach
3e160 65 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 e.** allocation
3e170 77 68 69 63 68 20 63 6f 75 6c 64 20 6e 6f 74 20 which could not
3e180 62 65 20 73 74 61 74 69 73 66 69 65 64 20 62 79 be statisfied by
3e190 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 43 4f 4e the [SQLITE_CON
3e1a0 46 49 47 5f 50 41 47 45 43 41 43 48 45 5d 0a 2a FIG_PAGECACHE].*
3e1b0 2a 20 62 75 66 66 65 72 20 61 6e 64 20 77 68 65 * buffer and whe
3e1c0 72 65 20 66 6f 72 63 65 64 20 74 6f 20 6f 76 65 re forced to ove
3e1d0 72 66 6c 6f 77 20 74 6f 20 5b 73 71 6c 69 74 65 rflow to [sqlite
3e1e0 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e 20 20 54 68 3_malloc()]. Th
3e1f0 65 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 76 61 e.** returned va
3e200 6c 75 65 20 69 6e 63 6c 75 64 65 73 20 61 6c 6c lue includes all
3e210 6f 63 61 74 69 6f 6e 73 20 74 68 61 74 20 6f 76 ocations that ov
3e220 65 72 66 6c 6f 77 65 64 20 62 65 63 61 75 73 65 erflowed because
3e230 20 74 68 65 79 0a 2a 2a 20 77 68 65 72 65 20 74 they.** where t
3e240 6f 6f 20 6c 61 72 67 65 20 28 74 68 65 79 20 77 oo large (they w
3e250 65 72 65 20 6c 61 72 67 65 72 20 74 68 61 6e 20 ere larger than
3e260 74 68 65 20 22 73 7a 22 20 70 61 72 61 6d 65 74 the "sz" paramet
3e270 65 72 20 74 6f 0a 2a 2a 20 5b 53 51 4c 49 54 45 er to.** [SQLITE
3e280 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 _CONFIG_PAGECACH
3e290 45 5d 29 20 61 6e 64 20 61 6c 6c 6f 63 61 74 69 E]) and allocati
3e2a0 6f 6e 73 20 74 68 61 74 20 6f 76 65 72 66 6c 6f ons that overflo
3e2b0 77 65 64 20 62 65 63 61 75 73 65 0a 2a 2a 20 6e wed because.** n
3e2c0 6f 20 73 70 61 63 65 20 77 61 73 20 6c 65 66 74 o space was left
3e2d0 20 69 6e 20 74 68 65 20 70 61 67 65 20 63 61 63 in the page cac
3e2e0 68 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c he.</dd>.**.** <
3e2f0 64 74 3e 53 51 4c 49 54 45 5f 53 54 41 54 55 53 dt>SQLITE_STATUS
3e300 5f 50 41 47 45 43 41 43 48 45 5f 53 49 5a 45 3c _PAGECACHE_SIZE<
3e310 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 /dt>.** <dd>This
3e320 20 70 61 72 61 6d 65 74 65 72 20 72 65 63 6f 72 parameter recor
3e330 64 73 20 74 68 65 20 6c 61 72 67 65 73 74 20 6d ds the largest m
3e340 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
3e350 20 72 65 71 75 65 73 74 0a 2a 2a 20 68 61 6e 64 request.** hand
3e360 65 64 20 74 6f 20 5b 70 61 67 65 63 61 63 68 65 ed to [pagecache
3e370 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f memory allocato
3e380 72 5d 2e 20 20 4f 6e 6c 79 20 74 68 65 20 76 61 r]. Only the va
3e390 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 6e 20 lue returned in
3e3a0 74 68 65 0a 2a 2a 20 2a 70 48 69 67 68 77 61 74 the.** *pHighwat
3e3b0 65 72 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 er parameter to
3e3c0 5b 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73 28 [sqlite3_status(
3e3d0 29 5d 20 69 73 20 6f 66 20 69 6e 74 65 72 65 73 )] is of interes
3e3e0 74 2e 20 20 0a 2a 2a 20 54 68 65 20 76 61 6c 75 t. .** The valu
3e3f0 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 e written into t
3e400 68 65 20 2a 70 43 75 72 72 65 6e 74 20 70 61 72 he *pCurrent par
3e410 61 6d 65 74 65 72 20 69 73 20 75 6e 64 65 66 69 ameter is undefi
3e420 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 ned.</dd>.**.**
3e430 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 41 54 55 <dt>SQLITE_STATU
3e440 53 5f 53 43 52 41 54 43 48 5f 55 53 45 44 3c 2f S_SCRATCH_USED</
3e450 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 dt>.** <dd>This
3e460 70 61 72 61 6d 65 74 65 72 20 72 65 74 75 72 6e parameter return
3e470 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 s the number of
3e480 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 75 73 65 64 allocations used
3e490 20 6f 75 74 20 6f 66 20 74 68 65 0a 2a 2a 20 5b out of the.** [
3e4a0 73 63 72 61 74 63 68 20 6d 65 6d 6f 72 79 20 61 scratch memory a
3e4b0 6c 6c 6f 63 61 74 6f 72 5d 20 63 6f 6e 66 69 67 llocator] config
3e4c0 75 72 65 64 20 75 73 69 6e 67 0a 2a 2a 20 5b 53 ured using.** [S
3e4d0 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 43 52 QLITE_CONFIG_SCR
3e4e0 41 54 43 48 5d 2e 20 20 54 68 65 20 76 61 6c 75 ATCH]. The valu
3e4f0 65 20 72 65 74 75 72 6e 65 64 20 69 73 20 69 6e e returned is in
3e500 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2c 20 6e 6f allocations, no
3e510 74 0a 2a 2a 20 69 6e 20 62 79 74 65 73 2e 20 20 t.** in bytes.
3e520 53 69 6e 63 65 20 61 20 73 69 6e 67 6c 65 20 74 Since a single t
3e530 68 72 65 61 64 20 6d 61 79 20 6f 6e 6c 79 20 68 hread may only h
3e540 61 76 65 20 6f 6e 65 20 73 63 72 61 74 63 68 20 ave one scratch
3e550 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 6f 75 allocation.** ou
3e560 74 73 74 61 6e 64 69 6e 67 20 61 74 20 74 69 6d tstanding at tim
3e570 65 2c 20 74 68 69 73 20 70 61 72 61 6d 65 74 65 e, this paramete
3e580 72 20 61 6c 73 6f 20 72 65 70 6f 72 74 73 20 74 r also reports t
3e590 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 72 he number of thr
3e5a0 65 61 64 73 0a 2a 2a 20 75 73 69 6e 67 20 73 63 eads.** using sc
3e5b0 72 61 74 63 68 20 6d 65 6d 6f 72 79 20 61 74 20 ratch memory at
3e5c0 74 68 65 20 73 61 6d 65 20 74 69 6d 65 2e 3c 2f the same time.</
3e5d0 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 dd>.**.** <dt>SQ
3e5e0 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41 LITE_STATUS_SCRA
3e5f0 54 43 48 5f 4f 56 45 52 46 4c 4f 57 3c 2f 64 74 TCH_OVERFLOW</dt
3e600 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 70 61 >.** <dd>This pa
3e610 72 61 6d 65 74 65 72 20 72 65 74 75 72 6e 73 20 rameter returns
3e620 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 the number of by
3e630 74 65 73 20 6f 66 20 73 63 72 61 74 63 68 20 6d tes of scratch m
3e640 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 emory.** allocat
3e650 69 6f 6e 20 77 68 69 63 68 20 63 6f 75 6c 64 20 ion which could
3e660 6e 6f 74 20 62 65 20 73 74 61 74 69 73 66 69 65 not be statisfie
3e670 64 20 62 79 20 74 68 65 20 5b 53 51 4c 49 54 45 d by the [SQLITE
3e680 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54 43 48 5d _CONFIG_SCRATCH]
3e690 0a 2a 2a 20 62 75 66 66 65 72 20 61 6e 64 20 77 .** buffer and w
3e6a0 68 65 72 65 20 66 6f 72 63 65 64 20 74 6f 20 6f here forced to o
3e6b0 76 65 72 66 6c 6f 77 20 74 6f 20 5b 73 71 6c 69 verflow to [sqli
3e6c0 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e 20 20 te3_malloc()].
3e6d0 54 68 65 20 76 61 6c 75 65 73 0a 2a 2a 20 72 65 The values.** re
3e6e0 74 75 72 6e 65 64 20 69 6e 63 6c 75 64 65 20 6f turned include o
3e6f0 76 65 72 66 6c 6f 77 73 20 62 65 63 61 75 73 65 verflows because
3e700 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 61 the requested a
3e710 6c 6c 6f 63 61 74 69 6f 6e 20 77 61 73 20 74 6f llocation was to
3e720 6f 0a 2a 2a 20 6c 61 72 67 65 72 20 28 74 68 61 o.** larger (tha
3e730 74 20 69 73 2c 20 62 65 63 61 75 73 65 20 74 68 t is, because th
3e740 65 20 72 65 71 75 65 73 74 65 64 20 61 6c 6c 6f e requested allo
3e750 63 61 74 69 6f 6e 20 77 61 73 20 6c 61 72 67 65 cation was large
3e760 72 20 74 68 61 6e 20 74 68 65 0a 2a 2a 20 22 73 r than the.** "s
3e770 7a 22 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 z" parameter to
3e780 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 [SQLITE_CONFIG_S
3e790 43 52 41 54 43 48 5d 29 20 61 6e 64 20 62 65 63 CRATCH]) and bec
3e7a0 61 75 73 65 20 6e 6f 20 73 63 72 61 74 63 68 20 ause no scratch
3e7b0 62 75 66 66 65 72 0a 2a 2a 20 73 6c 6f 74 73 20 buffer.** slots
3e7c0 77 65 72 65 20 61 76 61 69 6c 61 62 6c 65 2e 0a were available..
3e7d0 2a 2a 20 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c ** </dd>.**.** <
3e7e0 64 74 3e 53 51 4c 49 54 45 5f 53 54 41 54 55 53 dt>SQLITE_STATUS
3e7f0 5f 53 43 52 41 54 43 48 5f 53 49 5a 45 3c 2f 64 _SCRATCH_SIZE</d
3e800 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 70 t>.** <dd>This p
3e810 61 72 61 6d 65 74 65 72 20 72 65 63 6f 72 64 73 arameter records
3e820 20 74 68 65 20 6c 61 72 67 65 73 74 20 6d 65 6d the largest mem
3e830 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 ory allocation r
3e840 65 71 75 65 73 74 0a 2a 2a 20 68 61 6e 64 65 64 equest.** handed
3e850 20 74 6f 20 5b 73 63 72 61 74 63 68 20 6d 65 6d to [scratch mem
3e860 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 5d 2e 20 ory allocator].
3e870 20 4f 6e 6c 79 20 74 68 65 20 76 61 6c 75 65 20 Only the value
3e880 72 65 74 75 72 6e 65 64 20 69 6e 20 74 68 65 0a returned in the.
3e890 2a 2a 20 2a 70 48 69 67 68 77 61 74 65 72 20 70 ** *pHighwater p
3e8a0 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c arameter to [sql
3e8b0 69 74 65 33 5f 73 74 61 74 75 73 28 29 5d 20 69 ite3_status()] i
3e8c0 73 20 6f 66 20 69 6e 74 65 72 65 73 74 2e 20 20 s of interest.
3e8d0 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 77 72 .** The value wr
3e8e0 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 2a itten into the *
3e8f0 70 43 75 72 72 65 6e 74 20 70 61 72 61 6d 65 74 pCurrent paramet
3e900 65 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e er is undefined.
3e910 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e </dd>.**.** <dt>
3e920 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 SQLITE_STATUS_PA
3e930 52 53 45 52 5f 53 54 41 43 4b 3c 2f 64 74 3e 0a RSER_STACK</dt>.
3e940 2a 2a 20 3c 64 64 3e 54 68 69 73 20 70 61 72 61 ** <dd>This para
3e950 6d 65 74 65 72 20 72 65 63 6f 72 64 73 20 74 68 meter records th
3e960 65 20 64 65 65 70 65 73 74 20 70 61 72 73 65 72 e deepest parser
3e970 20 73 74 61 63 6b 2e 20 20 49 74 20 69 73 20 6f stack. It is o
3e980 6e 6c 79 0a 2a 2a 20 6d 65 61 6e 69 6e 67 66 75 nly.** meaningfu
3e990 6c 20 69 66 20 53 51 4c 69 74 65 20 69 73 20 63 l if SQLite is c
3e9a0 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 5b 59 59 ompiled with [YY
3e9b0 54 52 41 43 4b 4d 41 58 53 54 41 43 4b 44 45 50 TRACKMAXSTACKDEP
3e9c0 54 48 5d 2e 3c 2f 64 64 3e 0a 2a 2a 20 3c 2f 64 TH].</dd>.** </d
3e9d0 6c 3e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 73 74 61 l>.**.** New sta
3e9e0 74 75 73 20 70 61 72 61 6d 65 74 65 72 73 20 6d tus parameters m
3e9f0 61 79 20 62 65 20 61 64 64 65 64 20 66 72 6f 6d ay be added from
3ea00 20 74 69 6d 65 20 74 6f 20 74 69 6d 65 2e 0a 2a time to time..*
3ea10 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
3ea20 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 _STATUS_MEMORY_U
3ea30 53 45 44 20 20 20 20 20 20 20 20 20 20 30 0a 23 SED 0.#
3ea40 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 define SQLITE_ST
3ea50 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 55 ATUS_PAGECACHE_U
3ea60 53 45 44 20 20 20 20 20 20 20 31 0a 23 64 65 66 SED 1.#def
3ea70 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 41 54 55 ine SQLITE_STATU
3ea80 53 5f 50 41 47 45 43 41 43 48 45 5f 4f 56 45 52 S_PAGECACHE_OVER
3ea90 46 4c 4f 57 20 20 20 32 0a 23 64 65 66 69 6e 65 FLOW 2.#define
3eaa0 20 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 SQLITE_STATUS_S
3eab0 43 52 41 54 43 48 5f 55 53 45 44 20 20 20 20 20 CRATCH_USED
3eac0 20 20 20 20 33 0a 23 64 65 66 69 6e 65 20 53 51 3.#define SQ
3ead0 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41 LITE_STATUS_SCRA
3eae0 54 43 48 5f 4f 56 45 52 46 4c 4f 57 20 20 20 20 TCH_OVERFLOW
3eaf0 20 34 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 4.#define SQLIT
3eb00 45 5f 53 54 41 54 55 53 5f 4d 41 4c 4c 4f 43 5f E_STATUS_MALLOC_
3eb10 53 49 5a 45 20 20 20 20 20 20 20 20 20 20 35 0a SIZE 5.
3eb20 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 #define SQLITE_S
3eb30 54 41 54 55 53 5f 50 41 52 53 45 52 5f 53 54 41 TATUS_PARSER_STA
3eb40 43 4b 20 20 20 20 20 20 20 20 20 36 0a 23 64 65 CK 6.#de
3eb50 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 41 54 fine SQLITE_STAT
3eb60 55 53 5f 50 41 47 45 43 41 43 48 45 5f 53 49 5a US_PAGECACHE_SIZ
3eb70 45 20 20 20 20 20 20 20 37 0a 23 64 65 66 69 6e E 7.#defin
3eb80 65 20 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f e SQLITE_STATUS_
3eb90 53 43 52 41 54 43 48 5f 53 49 5a 45 20 20 20 20 SCRATCH_SIZE
3eba0 20 20 20 20 20 38 0a 0a 2f 2a 0a 2a 2a 20 43 41 8../*.** CA
3ebb0 50 49 33 52 45 46 3a 20 44 61 74 61 62 61 73 65 PI3REF: Database
3ebc0 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 53 74 61 74 Connection Stat
3ebd0 75 73 20 7b 48 31 37 35 30 30 7d 20 3c 53 36 30 us {H17500} <S60
3ebe0 32 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 200>.** EXPERIME
3ebf0 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 NTAL.**.** This
3ec00 69 6e 74 65 72 66 61 63 65 20 69 73 20 75 73 65 interface is use
3ec10 64 20 74 6f 20 72 65 74 72 69 65 76 65 20 72 75 d to retrieve ru
3ec20 6e 74 69 6d 65 20 73 74 61 74 75 73 20 69 6e 66 ntime status inf
3ec30 6f 72 6d 61 74 69 6f 6e 20 0a 2a 2a 20 61 62 6f ormation .** abo
3ec40 75 74 20 61 20 73 69 6e 67 6c 65 20 5b 64 61 74 ut a single [dat
3ec50 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
3ec60 5d 2e 20 20 54 68 65 20 66 69 72 73 74 20 61 72 ]. The first ar
3ec70 67 75 6d 65 6e 74 20 69 73 20 74 68 65 0a 2a 2a gument is the.**
3ec80 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
3ec90 74 69 6f 6e 20 6f 62 6a 65 63 74 20 74 6f 20 62 tion object to b
3eca0 65 20 69 6e 74 65 72 72 6f 67 61 74 65 64 2e 20 e interrogated.
3ecb0 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 The second argu
3ecc0 6d 65 6e 74 0a 2a 2a 20 69 73 20 74 68 65 20 70 ment.** is the p
3ecd0 61 72 61 6d 65 74 65 72 20 74 6f 20 69 6e 74 65 arameter to inte
3ece0 72 72 6f 67 61 74 65 2e 20 20 43 75 72 72 65 6e rrogate. Curren
3ecf0 74 6c 79 2c 20 74 68 65 20 6f 6e 6c 79 20 61 6c tly, the only al
3ed00 6c 6f 77 65 64 20 76 61 6c 75 65 0a 2a 2a 20 66 lowed value.** f
3ed10 6f 72 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61 or the second pa
3ed20 72 61 6d 65 74 65 72 20 69 73 20 5b 53 51 4c 49 rameter is [SQLI
3ed30 54 45 5f 44 42 53 54 41 54 55 53 5f 4c 4f 4f 4b TE_DBSTATUS_LOOK
3ed40 41 53 49 44 45 5f 55 53 45 44 5d 2e 0a 2a 2a 20 ASIDE_USED]..**
3ed50 41 64 64 69 74 69 6f 6e 61 6c 20 6f 70 74 69 6f Additional optio
3ed60 6e 73 20 77 69 6c 6c 20 6c 69 6b 65 6c 79 20 61 ns will likely a
3ed70 70 70 65 61 72 20 69 6e 20 66 75 74 75 72 65 20 ppear in future
3ed80 72 65 6c 65 61 73 65 73 20 6f 66 20 53 51 4c 69 releases of SQLi
3ed90 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 75 te..**.** The cu
3eda0 72 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 74 rrent value of t
3edb0 68 65 20 72 65 71 75 65 73 74 65 64 20 70 61 72 he requested par
3edc0 61 6d 65 74 65 72 20 69 73 20 77 72 69 74 74 65 ameter is writte
3edd0 6e 20 69 6e 74 6f 20 2a 70 43 75 72 0a 2a 2a 20 n into *pCur.**
3ede0 61 6e 64 20 74 68 65 20 68 69 67 68 65 73 74 20 and the highest
3edf0 69 6e 73 74 61 6e 74 61 6e 65 6f 75 73 20 76 61 instantaneous va
3ee00 6c 75 65 20 69 73 20 77 72 69 74 74 65 6e 20 69 lue is written i
3ee10 6e 74 6f 20 2a 70 48 69 77 74 72 2e 20 20 49 66 nto *pHiwtr. If
3ee20 0a 2a 2a 20 74 68 65 20 72 65 73 65 74 46 6c 67 .** the resetFlg
3ee30 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74 is true, then t
3ee40 68 65 20 68 69 67 68 65 73 74 20 69 6e 73 74 61 he highest insta
3ee50 6e 74 61 6e 65 6f 75 73 20 76 61 6c 75 65 20 69 ntaneous value i
3ee60 73 0a 2a 2a 20 72 65 73 65 74 20 62 61 63 6b 20 s.** reset back
3ee70 64 6f 77 6e 20 74 6f 20 74 68 65 20 63 75 72 72 down to the curr
3ee80 65 6e 74 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a ent value..**.**
3ee90 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 See also: [sqli
3eea0 74 65 33 5f 73 74 61 74 75 73 28 29 5d 20 61 6e te3_status()] an
3eeb0 64 20 5b 73 71 6c 69 74 65 33 5f 73 74 6d 74 5f d [sqlite3_stmt_
3eec0 73 74 61 74 75 73 28 29 5d 2e 0a 2a 2f 0a 53 51 status()]..*/.SQ
3eed0 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f LITE_API SQLITE_
3eee0 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 69 6e 74 EXPERIMENTAL int
3eef0 20 73 71 6c 69 74 65 33 5f 64 62 5f 73 74 61 74 sqlite3_db_stat
3ef00 75 73 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 us(sqlite3*, int
3ef10 20 6f 70 2c 20 69 6e 74 20 2a 70 43 75 72 2c 20 op, int *pCur,
3ef20 69 6e 74 20 2a 70 48 69 77 74 72 2c 20 69 6e 74 int *pHiwtr, int
3ef30 20 72 65 73 65 74 46 6c 67 29 3b 0a 0a 2f 2a 0a resetFlg);../*.
3ef40 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 53 74 61 ** CAPI3REF: Sta
3ef50 74 75 73 20 50 61 72 61 6d 65 74 65 72 73 20 66 tus Parameters f
3ef60 6f 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e or database conn
3ef70 65 63 74 69 6f 6e 73 20 7b 48 31 37 35 32 30 7d ections {H17520}
3ef80 20 3c 48 31 37 35 30 30 3e 0a 2a 2a 20 45 58 50 <H17500>.** EXP
3ef90 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 ERIMENTAL.**.**
3efa0 54 68 65 73 65 20 63 6f 6e 73 74 61 6e 74 73 20 These constants
3efb0 61 72 65 20 74 68 65 20 61 76 61 69 6c 61 62 6c are the availabl
3efc0 65 20 69 6e 74 65 67 65 72 20 22 76 65 72 62 73 e integer "verbs
3efd0 22 20 74 68 61 74 20 63 61 6e 20 62 65 20 70 61 " that can be pa
3efe0 73 73 65 64 20 61 73 0a 2a 2a 20 74 68 65 20 73 ssed as.** the s
3eff0 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 econd argument t
3f000 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 64 o the [sqlite3_d
3f010 62 5f 73 74 61 74 75 73 28 29 5d 20 69 6e 74 65 b_status()] inte
3f020 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 rface..**.** New
3f030 20 76 65 72 62 73 20 6d 61 79 20 62 65 20 61 64 verbs may be ad
3f040 64 65 64 20 69 6e 20 66 75 74 75 72 65 20 72 65 ded in future re
3f050 6c 65 61 73 65 73 20 6f 66 20 53 51 4c 69 74 65 leases of SQLite
3f060 2e 20 45 78 69 73 74 69 6e 67 20 76 65 72 62 73 . Existing verbs
3f070 0a 2a 2a 20 6d 69 67 68 74 20 62 65 20 64 69 73 .** might be dis
3f080 63 6f 6e 74 69 6e 75 65 64 2e 20 41 70 70 6c 69 continued. Appli
3f090 63 61 74 69 6f 6e 73 20 73 68 6f 75 6c 64 20 63 cations should c
3f0a0 68 65 63 6b 20 74 68 65 20 72 65 74 75 72 6e 20 heck the return
3f0b0 63 6f 64 65 20 66 72 6f 6d 0a 2a 2a 20 5b 73 71 code from.** [sq
3f0c0 6c 69 74 65 33 5f 64 62 5f 73 74 61 74 75 73 28 lite3_db_status(
3f0d0 29 5d 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 )] to make sure
3f0e0 74 68 61 74 20 74 68 65 20 63 61 6c 6c 20 77 6f that the call wo
3f0f0 72 6b 65 64 2e 0a 2a 2a 20 54 68 65 20 5b 73 71 rked..** The [sq
3f100 6c 69 74 65 33 5f 64 62 5f 73 74 61 74 75 73 28 lite3_db_status(
3f110 29 5d 20 69 6e 74 65 72 66 61 63 65 20 77 69 6c )] interface wil
3f120 6c 20 72 65 74 75 72 6e 20 61 20 6e 6f 6e 2d 7a l return a non-z
3f130 65 72 6f 20 65 72 72 6f 72 20 63 6f 64 65 0a 2a ero error code.*
3f140 2a 20 69 66 20 61 20 64 69 73 63 6f 6e 74 69 6e * if a discontin
3f150 75 65 64 20 6f 72 20 75 6e 73 75 70 70 6f 72 74 ued or unsupport
3f160 65 64 20 76 65 72 62 20 69 73 20 69 6e 76 6f 6b ed verb is invok
3f170 65 64 2e 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e 0a 2a ed..**.** <dl>.*
3f180 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 44 42 53 * <dt>SQLITE_DBS
3f190 54 41 54 55 53 5f 4c 4f 4f 4b 41 53 49 44 45 5f TATUS_LOOKASIDE_
3f1a0 55 53 45 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 USED</dt>.** <dd
3f1b0 3e 54 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 >This parameter
3f1c0 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 returns the numb
3f1d0 65 72 20 6f 66 20 6c 6f 6f 6b 61 73 69 64 65 20 er of lookaside
3f1e0 6d 65 6d 6f 72 79 20 73 6c 6f 74 73 20 63 75 72 memory slots cur
3f1f0 72 65 6e 74 6c 79 0a 2a 2a 20 63 68 65 63 6b 65 rently.** checke
3f200 64 20 6f 75 74 2e 3c 2f 64 64 3e 0a 2a 2a 20 3c d out.</dd>.** <
3f210 2f 64 6c 3e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 /dl>.*/.#define
3f220 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f SQLITE_DBSTATUS_
3f230 4c 4f 4f 4b 41 53 49 44 45 5f 55 53 45 44 20 20 LOOKASIDE_USED
3f240 20 20 20 30 0a 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 0.../*.** CAP
3f250 49 33 52 45 46 3a 20 50 72 65 70 61 72 65 64 20 I3REF: Prepared
3f260 53 74 61 74 65 6d 65 6e 74 20 53 74 61 74 75 73 Statement Status
3f270 20 7b 48 31 37 35 35 30 7d 20 3c 53 36 30 32 30 {H17550} <S6020
3f280 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 0>.** EXPERIMENT
3f290 41 4c 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 70 72 AL.**.** Each pr
3f2a0 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 epared statement
3f2b0 20 6d 61 69 6e 74 61 69 6e 73 20 76 61 72 69 6f maintains vario
3f2c0 75 73 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 53 54 us.** [SQLITE_ST
3f2d0 4d 54 53 54 41 54 55 53 5f 53 4f 52 54 20 7c 20 MTSTATUS_SORT |
3f2e0 63 6f 75 6e 74 65 72 73 5d 20 74 68 61 74 20 6d counters] that m
3f2f0 65 61 73 75 72 65 20 74 68 65 20 6e 75 6d 62 65 easure the numbe
3f300 72 0a 2a 2a 20 6f 66 20 74 69 6d 65 73 20 69 74 r.** of times it
3f310 20 68 61 73 20 70 65 72 66 6f 72 6d 65 64 20 73 has performed s
3f320 70 65 63 69 66 69 63 20 6f 70 65 72 61 74 69 6f pecific operatio
3f330 6e 73 2e 20 20 54 68 65 73 65 20 63 6f 75 6e 74 ns. These count
3f340 65 72 73 20 63 61 6e 0a 2a 2a 20 62 65 20 75 73 ers can.** be us
3f350 65 64 20 74 6f 20 6d 6f 6e 69 74 6f 72 20 74 68 ed to monitor th
3f360 65 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 63 68 e performance ch
3f370 61 72 61 63 74 65 72 69 73 74 69 63 73 20 6f 66 aracteristics of
3f380 20 74 68 65 20 70 72 65 70 61 72 65 64 0a 2a 2a the prepared.**
3f390 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 20 46 6f statements. Fo
3f3a0 72 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 74 68 r example, if th
3f3b0 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 61 62 6c e number of tabl
3f3c0 65 20 73 74 65 70 73 20 67 72 65 61 74 6c 79 20 e steps greatly
3f3d0 65 78 63 65 65 64 73 0a 2a 2a 20 74 68 65 20 6e exceeds.** the n
3f3e0 75 6d 62 65 72 20 6f 66 20 74 61 62 6c 65 20 73 umber of table s
3f3f0 65 61 72 63 68 65 73 20 6f 72 20 72 65 73 75 6c earches or resul
3f400 74 20 72 6f 77 73 2c 20 74 68 61 74 20 77 6f 75 t rows, that wou
3f410 6c 64 20 74 65 6e 64 20 74 6f 20 69 6e 64 69 63 ld tend to indic
3f420 61 74 65 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 ate.** that the
3f430 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
3f440 6e 74 20 69 73 20 75 73 69 6e 67 20 61 20 66 75 nt is using a fu
3f450 6c 6c 20 74 61 62 6c 65 20 73 63 61 6e 20 72 61 ll table scan ra
3f460 74 68 65 72 20 74 68 61 6e 0a 2a 2a 20 61 6e 20 ther than.** an
3f470 69 6e 64 65 78 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 index. .**.** T
3f480 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 69 73 his interface is
3f490 20 75 73 65 64 20 74 6f 20 72 65 74 72 69 65 76 used to retriev
3f4a0 65 20 61 6e 64 20 72 65 73 65 74 20 63 6f 75 6e e and reset coun
3f4b0 74 65 72 20 76 61 6c 75 65 73 20 66 72 6f 6d 0a ter values from.
3f4c0 2a 2a 20 61 20 5b 70 72 65 70 61 72 65 64 20 73 ** a [prepared s
3f4d0 74 61 74 65 6d 65 6e 74 5d 2e 20 20 54 68 65 20 tatement]. The
3f4e0 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 69 first argument i
3f4f0 73 20 74 68 65 20 70 72 65 70 61 72 65 64 20 73 s the prepared s
3f500 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 6f 62 6a 65 tatement.** obje
3f510 63 74 20 74 6f 20 62 65 20 69 6e 74 65 72 72 6f ct to be interro
3f520 67 61 74 65 64 2e 20 20 54 68 65 20 73 65 63 6f gated. The seco
3f530 6e 64 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 69 nd argument.** i
3f540 73 20 61 6e 20 69 6e 74 65 67 65 72 20 63 6f 64 s an integer cod
3f550 65 20 66 6f 72 20 61 20 73 70 65 63 69 66 69 63 e for a specific
3f560 20 5b 53 51 4c 49 54 45 5f 53 54 4d 54 53 54 41 [SQLITE_STMTSTA
3f570 54 55 53 5f 53 4f 52 54 20 7c 20 63 6f 75 6e 74 TUS_SORT | count
3f580 65 72 5d 0a 2a 2a 20 74 6f 20 62 65 20 69 6e 74 er].** to be int
3f590 65 72 72 6f 67 61 74 65 64 2e 20 0a 2a 2a 20 54 errogated. .** T
3f5a0 68 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 he current value
3f5b0 20 6f 66 20 74 68 65 20 72 65 71 75 65 73 74 65 of the requeste
3f5c0 64 20 63 6f 75 6e 74 65 72 20 69 73 20 72 65 74 d counter is ret
3f5d0 75 72 6e 65 64 2e 0a 2a 2a 20 49 66 20 74 68 65 urned..** If the
3f5e0 20 72 65 73 65 74 46 6c 67 20 69 73 20 74 72 75 resetFlg is tru
3f5f0 65 2c 20 74 68 65 6e 20 74 68 65 20 63 6f 75 6e e, then the coun
3f600 74 65 72 20 69 73 20 72 65 73 65 74 20 74 6f 20 ter is reset to
3f610 7a 65 72 6f 20 61 66 74 65 72 20 74 68 69 73 0a zero after this.
3f620 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 63 61 6c ** interface cal
3f630 6c 20 72 65 74 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a l returns..**.**
3f640 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 See also: [sqli
3f650 74 65 33 5f 73 74 61 74 75 73 28 29 5d 20 61 6e te3_status()] an
3f660 64 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f 73 74 d [sqlite3_db_st
3f670 61 74 75 73 28 29 5d 2e 0a 2a 2f 0a 53 51 4c 49 atus()]..*/.SQLI
3f680 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 45 58 TE_API SQLITE_EX
3f690 50 45 52 49 4d 45 4e 54 41 4c 20 69 6e 74 20 73 PERIMENTAL int s
3f6a0 71 6c 69 74 65 33 5f 73 74 6d 74 5f 73 74 61 74 qlite3_stmt_stat
3f6b0 75 73 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a us(sqlite3_stmt*
3f6c0 2c 20 69 6e 74 20 6f 70 2c 69 6e 74 20 72 65 73 , int op,int res
3f6d0 65 74 46 6c 67 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 etFlg);../*.** C
3f6e0 41 50 49 33 52 45 46 3a 20 53 74 61 74 75 73 20 API3REF: Status
3f6f0 50 61 72 61 6d 65 74 65 72 73 20 66 6f 72 20 70 Parameters for p
3f700 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
3f710 74 73 20 7b 48 31 37 35 37 30 7d 20 3c 48 31 37 ts {H17570} <H17
3f720 35 35 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 550>.** EXPERIME
3f730 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 NTAL.**.** These
3f740 20 70 72 65 70 72 6f 63 65 73 73 6f 72 20 6d 61 preprocessor ma
3f750 63 72 6f 73 20 64 65 66 69 6e 65 20 69 6e 74 65 cros define inte
3f760 67 65 72 20 63 6f 64 65 73 20 74 68 61 74 20 6e ger codes that n
3f770 61 6d 65 20 63 6f 75 6e 74 65 72 0a 2a 2a 20 76 ame counter.** v
3f780 61 6c 75 65 73 20 61 73 73 6f 63 69 61 74 65 64 alues associated
3f790 20 77 69 74 68 20 74 68 65 20 5b 73 71 6c 69 74 with the [sqlit
3f7a0 65 33 5f 73 74 6d 74 5f 73 74 61 74 75 73 28 29 e3_stmt_status()
3f7b0 5d 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 20 ] interface..**
3f7c0 54 68 65 20 6d 65 61 6e 69 6e 67 73 20 6f 66 20 The meanings of
3f7d0 74 68 65 20 76 61 72 69 6f 75 73 20 63 6f 75 6e the various coun
3f7e0 74 65 72 73 20 61 72 65 20 61 73 20 66 6f 6c 6c ters are as foll
3f7f0 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e 0a ows:.**.** <dl>.
3f800 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 ** <dt>SQLITE_ST
3f810 4d 54 53 54 41 54 55 53 5f 46 55 4c 4c 53 43 41 MTSTATUS_FULLSCA
3f820 4e 5f 53 54 45 50 3c 2f 64 74 3e 0a 2a 2a 20 3c N_STEP</dt>.** <
3f830 64 64 3e 54 68 69 73 20 69 73 20 74 68 65 20 6e dd>This is the n
3f840 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 74 umber of times t
3f850 68 61 74 20 53 51 4c 69 74 65 20 68 61 73 20 73 hat SQLite has s
3f860 74 65 70 70 65 64 20 66 6f 72 77 61 72 64 20 69 tepped forward i
3f870 6e 0a 2a 2a 20 61 20 74 61 62 6c 65 20 61 73 20 n.** a table as
3f880 70 61 72 74 20 6f 66 20 61 20 66 75 6c 6c 20 74 part of a full t
3f890 61 62 6c 65 20 73 63 61 6e 2e 20 20 4c 61 72 67 able scan. Larg
3f8a0 65 20 6e 75 6d 62 65 72 73 20 66 6f 72 20 74 68 e numbers for th
3f8b0 69 73 20 63 6f 75 6e 74 65 72 0a 2a 2a 20 6d 61 is counter.** ma
3f8c0 79 20 69 6e 64 69 63 61 74 65 20 6f 70 70 6f 72 y indicate oppor
3f8d0 74 75 6e 69 74 69 65 73 20 66 6f 72 20 70 65 72 tunities for per
3f8e0 66 6f 72 6d 61 6e 63 65 20 69 6d 70 72 6f 76 65 formance improve
3f8f0 6d 65 6e 74 20 74 68 72 6f 75 67 68 20 0a 2a 2a ment through .**
3f900 20 63 61 72 65 66 75 6c 20 75 73 65 20 6f 66 20 careful use of
3f910 69 6e 64 69 63 65 73 2e 3c 2f 64 64 3e 0a 2a 2a indices.</dd>.**
3f920 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 .** <dt>SQLITE_S
3f930 54 4d 54 53 54 41 54 55 53 5f 53 4f 52 54 3c 2f TMTSTATUS_SORT</
3f940 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 dt>.** <dd>This
3f950 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 is the number of
3f960 20 73 6f 72 74 20 6f 70 65 72 61 74 69 6f 6e 73 sort operations
3f970 20 74 68 61 74 20 68 61 76 65 20 6f 63 63 75 72 that have occur
3f980 72 65 64 2e 0a 2a 2a 20 41 20 6e 6f 6e 2d 7a 65 red..** A non-ze
3f990 72 6f 20 76 61 6c 75 65 20 69 6e 20 74 68 69 73 ro value in this
3f9a0 20 63 6f 75 6e 74 65 72 20 6d 61 79 20 69 6e 64 counter may ind
3f9b0 69 63 61 74 65 20 61 6e 20 6f 70 70 6f 72 74 75 icate an opportu
3f9c0 6e 69 74 79 20 74 6f 0a 2a 2a 20 69 6d 70 72 6f nity to.** impro
3f9d0 76 65 6d 65 6e 74 20 70 65 72 66 6f 72 6d 61 6e vement performan
3f9e0 63 65 20 74 68 72 6f 75 67 68 20 63 61 72 65 66 ce through caref
3f9f0 75 6c 20 75 73 65 20 6f 66 20 69 6e 64 69 63 65 ul use of indice
3fa00 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 2f s.</dd>.**.** </
3fa10 64 6c 3e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 dl>.*/.#define S
3fa20 51 4c 49 54 45 5f 53 54 4d 54 53 54 41 54 55 53 QLITE_STMTSTATUS
3fa30 5f 46 55 4c 4c 53 43 41 4e 5f 53 54 45 50 20 20 _FULLSCAN_STEP
3fa40 20 20 20 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 1.#define SQL
3fa50 49 54 45 5f 53 54 4d 54 53 54 41 54 55 53 5f 53 ITE_STMTSTATUS_S
3fa60 4f 52 54 20 20 20 20 20 20 20 20 20 20 20 20 20 ORT
3fa70 20 32 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 2../*.** CAPI3R
3fa80 45 46 3a 20 43 75 73 74 6f 6d 20 50 61 67 65 20 EF: Custom Page
3fa90 43 61 63 68 65 20 4f 62 6a 65 63 74 0a 2a 2a 20 Cache Object.**
3faa0 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a EXPERIMENTAL.**.
3fab0 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 70 ** The sqlite3_p
3fac0 63 61 63 68 65 20 74 79 70 65 20 69 73 20 6f 70 cache type is op
3fad0 61 71 75 65 2e 20 20 49 74 20 69 73 20 69 6d 70 aque. It is imp
3fae0 6c 65 6d 65 6e 74 65 64 20 62 79 0a 2a 2a 20 74 lemented by.** t
3faf0 68 65 20 70 6c 75 67 67 61 62 6c 65 20 6d 6f 64 he pluggable mod
3fb00 75 6c 65 2e 20 20 54 68 65 20 53 51 4c 69 74 65 ule. The SQLite
3fb10 20 63 6f 72 65 20 68 61 73 20 6e 6f 20 6b 6e 6f core has no kno
3fb20 77 6c 65 64 67 65 20 6f 66 0a 2a 2a 20 69 74 73 wledge of.** its
3fb30 20 73 69 7a 65 20 6f 72 20 69 6e 74 65 72 6e 61 size or interna
3fb40 6c 20 73 74 72 75 63 74 75 72 65 20 61 6e 64 20 l structure and
3fb50 6e 65 76 65 72 20 64 65 61 6c 73 20 77 69 74 68 never deals with
3fb60 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f the.** sqlite3_
3fb70 70 63 61 63 68 65 20 6f 62 6a 65 63 74 20 65 78 pcache object ex
3fb80 63 65 70 74 20 62 79 20 68 6f 6c 64 69 6e 67 20 cept by holding
3fb90 61 6e 64 20 70 61 73 73 69 6e 67 20 70 6f 69 6e and passing poin
3fba0 74 65 72 73 0a 2a 2a 20 74 6f 20 74 68 65 20 6f ters.** to the o
3fbb0 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 bject..**.** See
3fbc0 20 5b 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 [sqlite3_pcache
3fbd0 5f 6d 65 74 68 6f 64 73 5d 20 66 6f 72 20 61 64 _methods] for ad
3fbe0 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 ditional informa
3fbf0 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 tion..*/.typedef
3fc00 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f struct sqlite3_
3fc10 70 63 61 63 68 65 20 73 71 6c 69 74 65 33 5f 70 pcache sqlite3_p
3fc20 63 61 63 68 65 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 cache;../*.** CA
3fc30 50 49 33 52 45 46 3a 20 41 70 70 6c 69 63 61 74 PI3REF: Applicat
3fc40 69 6f 6e 20 44 65 66 69 6e 65 64 20 50 61 67 65 ion Defined Page
3fc50 20 43 61 63 68 65 2e 0a 2a 2a 20 4b 45 59 57 4f Cache..** KEYWO
3fc60 52 44 53 3a 20 7b 70 61 67 65 20 63 61 63 68 65 RDS: {page cache
3fc70 7d 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 }.** EXPERIMENTA
3fc80 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 73 71 6c L.**.** The [sql
3fc90 69 74 65 33 5f 63 6f 6e 66 69 67 5d 28 5b 53 51 ite3_config]([SQ
3fca0 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 43 41 43 LITE_CONFIG_PCAC
3fcb0 48 45 5d 2c 20 2e 2e 2e 29 20 69 6e 74 65 72 66 HE], ...) interf
3fcc0 61 63 65 20 63 61 6e 0a 2a 2a 20 72 65 67 69 73 ace can.** regis
3fcd0 74 65 72 20 61 6e 20 61 6c 74 65 72 6e 61 74 69 ter an alternati
3fce0 76 65 20 70 61 67 65 20 63 61 63 68 65 20 69 6d ve page cache im
3fcf0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 62 79 20 plementation by
3fd00 70 61 73 73 69 6e 67 20 69 6e 20 61 6e 20 0a 2a passing in an .*
3fd10 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 * instance of th
3fd20 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 e sqlite3_pcache
3fd30 5f 6d 65 74 68 6f 64 73 20 73 74 72 75 63 74 75 _methods structu
3fd40 72 65 2e 20 54 68 65 20 6d 61 6a 6f 72 69 74 79 re. The majority
3fd50 20 6f 66 20 74 68 65 20 0a 2a 2a 20 68 65 61 70 of the .** heap
3fd60 20 6d 65 6d 6f 72 79 20 75 73 65 64 20 62 79 20 memory used by
3fd70 53 51 4c 69 74 65 20 69 73 20 75 73 65 64 20 62 SQLite is used b
3fd80 79 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65 y the page cache
3fd90 20 74 6f 20 63 61 63 68 65 20 64 61 74 61 20 72 to cache data r
3fda0 65 61 64 20 0a 2a 2a 20 66 72 6f 6d 2c 20 6f 72 ead .** from, or
3fdb0 20 72 65 61 64 79 20 74 6f 20 62 65 20 77 72 69 ready to be wri
3fdc0 74 74 65 6e 20 74 6f 2c 20 74 68 65 20 64 61 74 tten to, the dat
3fdd0 61 62 61 73 65 20 66 69 6c 65 2e 20 42 79 20 69 abase file. By i
3fde0 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 61 20 0a 2a mplementing a .*
3fdf0 2a 20 63 75 73 74 6f 6d 20 70 61 67 65 20 63 61 * custom page ca
3fe00 63 68 65 20 75 73 69 6e 67 20 74 68 69 73 20 41 che using this A
3fe10 50 49 2c 20 61 6e 20 61 70 70 6c 69 63 61 74 69 PI, an applicati
3fe20 6f 6e 20 63 61 6e 20 63 6f 6e 74 72 6f 6c 20 6d on can control m
3fe30 6f 72 65 20 0a 2a 2a 20 70 72 65 63 69 73 65 6c ore .** precisel
3fe40 79 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 y the amount of
3fe50 6d 65 6d 6f 72 79 20 63 6f 6e 73 75 6d 65 64 20 memory consumed
3fe60 62 79 20 53 51 4c 69 74 65 2c 20 74 68 65 20 77 by SQLite, the w
3fe70 61 79 20 69 6e 20 77 68 69 63 68 20 0a 2a 2a 20 ay in which .**
3fe80 74 68 61 74 20 6d 65 6d 6f 72 79 20 69 73 20 61 that memory is a
3fe90 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 72 65 6c llocated and rel
3fea0 65 61 73 65 64 2c 20 61 6e 64 20 74 68 65 20 70 eased, and the p
3feb0 6f 6c 69 63 69 65 73 20 75 73 65 64 20 74 6f 20 olicies used to
3fec0 0a 2a 2a 20 64 65 74 65 72 6d 69 6e 65 20 65 78 .** determine ex
3fed0 61 63 74 6c 79 20 77 68 69 63 68 20 70 61 72 74 actly which part
3fee0 73 20 6f 66 20 61 20 64 61 74 61 62 61 73 65 20 s of a database
3fef0 66 69 6c 65 20 61 72 65 20 63 61 63 68 65 64 20 file are cached
3ff00 61 6e 64 20 66 6f 72 20 0a 2a 2a 20 68 6f 77 20 and for .** how
3ff10 6c 6f 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 long..**.** The
3ff20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 contents of the
3ff30 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d sqlite3_pcache_m
3ff40 65 74 68 6f 64 73 20 73 74 72 75 63 74 75 72 65 ethods structure
3ff50 20 61 72 65 20 63 6f 70 69 65 64 20 74 6f 20 61 are copied to a
3ff60 6e 0a 2a 2a 20 69 6e 74 65 72 6e 61 6c 20 62 75 n.** internal bu
3ff70 66 66 65 72 20 62 79 20 53 51 4c 69 74 65 20 77 ffer by SQLite w
3ff80 69 74 68 69 6e 20 74 68 65 20 63 61 6c 6c 20 74 ithin the call t
3ff90 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 o [sqlite3_confi
3ffa0 67 5d 2e 20 20 48 65 6e 63 65 0a 2a 2a 20 74 68 g]. Hence.** th
3ffb0 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d 61 e application ma
3ffc0 79 20 64 69 73 63 61 72 64 20 74 68 65 20 70 61 y discard the pa
3ffd0 72 61 6d 65 74 65 72 20 61 66 74 65 72 20 74 68 rameter after th
3ffe0 65 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 5b 73 71 e call to.** [sq
3fff0 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d 20 lite3_config()]
40000 72 65 74 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 returns..**.** T
40010 68 65 20 78 49 6e 69 74 28 29 20 6d 65 74 68 6f he xInit() metho
40020 64 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 63 65 d is called once
40030 20 66 6f 72 20 65 61 63 68 20 63 61 6c 6c 20 74 for each call t
40040 6f 20 5b 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 o [sqlite3_initi
40050 61 6c 69 7a 65 28 29 5d 0a 2a 2a 20 28 75 73 75 alize()].** (usu
40060 61 6c 6c 79 20 6f 6e 6c 79 20 6f 6e 63 65 20 64 ally only once d
40070 75 72 69 6e 67 20 74 68 65 20 6c 69 66 65 74 69 uring the lifeti
40080 6d 65 20 6f 66 20 74 68 65 20 70 72 6f 63 65 73 me of the proces
40090 73 29 2e 20 49 74 20 69 73 20 70 61 73 73 65 64 s). It is passed
400a0 0a 2a 2a 20 61 20 63 6f 70 79 20 6f 66 20 74 68 .** a copy of th
400b0 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 e sqlite3_pcache
400c0 5f 6d 65 74 68 6f 64 73 2e 70 41 72 67 20 76 61 _methods.pArg va
400d0 6c 75 65 2e 20 49 74 20 63 61 6e 20 62 65 20 75 lue. It can be u
400e0 73 65 64 20 74 6f 20 73 65 74 0a 2a 2a 20 75 70 sed to set.** up
400f0 20 67 6c 6f 62 61 6c 20 73 74 72 75 63 74 75 72 global structur
40100 65 73 20 61 6e 64 20 6d 75 74 65 78 65 73 20 72 es and mutexes r
40110 65 71 75 69 72 65 64 20 62 79 20 74 68 65 20 63 equired by the c
40120 75 73 74 6f 6d 20 70 61 67 65 20 63 61 63 68 65 ustom page cache
40130 20 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 .** implementat
40140 69 6f 6e 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ion. .**.** The
40150 78 53 68 75 74 64 6f 77 6e 28 29 20 6d 65 74 68 xShutdown() meth
40160 6f 64 20 69 73 20 63 61 6c 6c 65 64 20 66 72 6f od is called fro
40170 6d 20 77 69 74 68 69 6e 20 5b 73 71 6c 69 74 65 m within [sqlite
40180 33 5f 73 68 75 74 64 6f 77 6e 28 29 5d 2c 20 0a 3_shutdown()], .
40190 2a 2a 20 69 66 20 74 68 65 20 61 70 70 6c 69 63 ** if the applic
401a0 61 74 69 6f 6e 20 69 6e 76 6f 6b 65 73 20 74 68 ation invokes th
401b0 69 73 20 41 50 49 2e 20 49 74 20 63 61 6e 20 62 is API. It can b
401c0 65 20 75 73 65 64 20 74 6f 20 63 6c 65 61 6e 20 e used to clean
401d0 75 70 20 0a 2a 2a 20 61 6e 79 20 6f 75 74 73 74 up .** any outst
401e0 61 6e 64 69 6e 67 20 72 65 73 6f 75 72 63 65 73 anding resources
401f0 20 62 65 66 6f 72 65 20 70 72 6f 63 65 73 73 20 before process
40200 73 68 75 74 64 6f 77 6e 2c 20 69 66 20 72 65 71 shutdown, if req
40210 75 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c uired..**.** SQL
40220 69 74 65 20 68 6f 6c 64 73 20 61 20 5b 53 51 4c ite holds a [SQL
40230 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 ITE_MUTEX_RECURS
40240 49 56 45 5d 20 6d 75 74 65 78 20 77 68 65 6e 20 IVE] mutex when
40250 69 74 20 69 6e 76 6f 6b 65 73 0a 2a 2a 20 74 68 it invokes.** th
40260 65 20 78 49 6e 69 74 20 6d 65 74 68 6f 64 2c 20 e xInit method,
40270 73 6f 20 74 68 65 20 78 49 6e 69 74 20 6d 65 74 so the xInit met
40280 68 6f 64 20 6e 65 65 64 20 6e 6f 74 20 62 65 20 hod need not be
40290 74 68 72 65 61 64 73 61 66 65 2e 20 20 54 68 65 threadsafe. The
402a0 0a 2a 2a 20 78 53 68 75 74 64 6f 77 6e 20 6d 65 .** xShutdown me
402b0 74 68 6f 64 20 69 73 20 6f 6e 6c 79 20 63 61 6c thod is only cal
402c0 6c 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 led from [sqlite
402d0 33 5f 73 68 75 74 64 6f 77 6e 28 29 5d 20 73 6f 3_shutdown()] so
402e0 20 69 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 it does.** not
402f0 6e 65 65 64 20 74 6f 20 62 65 20 74 68 72 65 61 need to be threa
40300 64 73 61 66 65 20 65 69 74 68 65 72 2e 20 20 41 dsafe either. A
40310 6c 6c 20 6f 74 68 65 72 20 6d 65 74 68 6f 64 73 ll other methods
40320 20 6d 75 73 74 20 62 65 20 74 68 72 65 61 64 73 must be threads
40330 61 66 65 0a 2a 2a 20 69 6e 20 6d 75 6c 74 69 74 afe.** in multit
40340 68 72 65 61 64 65 64 20 61 70 70 6c 69 63 61 74 hreaded applicat
40350 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 ions..**.** SQLi
40360 74 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 69 6e te will never in
40370 76 6f 6b 65 20 78 49 6e 69 74 28 29 20 6d 6f 72 voke xInit() mor
40380 65 20 74 68 61 6e 20 6f 6e 63 65 20 77 69 74 68 e than once with
40390 6f 75 74 20 61 6e 20 69 6e 74 65 72 76 65 6e 69 out an interveni
403a0 6e 67 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 78 53 ng.** call to xS
403b0 68 75 74 64 6f 77 6e 28 29 2e 0a 2a 2a 0a 2a 2a hutdown()..**.**
403c0 20 54 68 65 20 78 43 72 65 61 74 65 28 29 20 6d The xCreate() m
403d0 65 74 68 6f 64 20 69 73 20 75 73 65 64 20 74 6f ethod is used to
403e0 20 63 6f 6e 73 74 72 75 63 74 20 61 20 6e 65 77 construct a new
403f0 20 63 61 63 68 65 20 69 6e 73 74 61 6e 63 65 2e cache instance.
40400 20 20 53 51 4c 69 74 65 0a 2a 2a 20 77 69 6c 6c SQLite.** will
40410 20 74 79 70 69 63 61 6c 6c 79 20 63 72 65 61 74 typically creat
40420 65 20 6f 6e 65 20 63 61 63 68 65 20 69 6e 73 74 e one cache inst
40430 61 6e 63 65 20 66 6f 72 20 65 61 63 68 20 6f 70 ance for each op
40440 65 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 en database file
40450 2c 0a 2a 2a 20 74 68 6f 75 67 68 20 74 68 69 73 ,.** though this
40460 20 69 73 20 6e 6f 74 20 67 75 61 72 61 6e 74 65 is not guarante
40470 65 64 2e 20 54 68 65 0a 2a 2a 20 66 69 72 73 74 ed. The.** first
40480 20 70 61 72 61 6d 65 74 65 72 2c 20 73 7a 50 61 parameter, szPa
40490 67 65 2c 20 69 73 20 74 68 65 20 73 69 7a 65 20 ge, is the size
404a0 69 6e 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 in bytes of the
404b0 70 61 67 65 73 20 74 68 61 74 20 6d 75 73 74 0a pages that must.
404c0 2a 2a 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 20 ** be allocated
404d0 62 79 20 74 68 65 20 63 61 63 68 65 2e 20 20 73 by the cache. s
404e0 7a 50 61 67 65 20 77 69 6c 6c 20 6e 6f 74 20 62 zPage will not b
404f0 65 20 61 20 70 6f 77 65 72 20 6f 66 20 74 77 6f e a power of two
40500 2e 20 20 73 7a 50 61 67 65 0a 2a 2a 20 77 69 6c . szPage.** wil
40510 6c 20 74 68 65 20 70 61 67 65 20 73 69 7a 65 20 l the page size
40520 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
40530 66 69 6c 65 20 74 68 61 74 20 69 73 20 74 6f 20 file that is to
40540 62 65 20 63 61 63 68 65 64 20 70 6c 75 73 20 61 be cached plus a
40550 6e 0a 2a 2a 20 69 6e 63 72 65 6d 65 6e 74 20 28 n.** increment (
40560 68 65 72 65 20 63 61 6c 6c 65 64 20 22 52 22 29 here called "R")
40570 20 6f 66 20 61 62 6f 75 74 20 31 30 30 20 6f 72 of about 100 or
40580 20 32 30 30 2e 20 20 53 51 4c 69 74 65 20 77 69 200. SQLite wi
40590 6c 6c 20 75 73 65 20 74 68 65 0a 2a 2a 20 65 78 ll use the.** ex
405a0 74 72 61 20 52 20 62 79 74 65 73 20 6f 6e 20 65 tra R bytes on e
405b0 61 63 68 20 70 61 67 65 20 74 6f 20 73 74 6f 72 ach page to stor
405c0 65 20 6d 65 74 61 64 61 74 61 20 61 62 6f 75 74 e metadata about
405d0 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 0a the underlying.
405e0 2a 2a 20 64 61 74 61 62 61 73 65 20 70 61 67 65 ** database page
405f0 20 6f 6e 20 64 69 73 6b 2e 20 20 54 68 65 20 76 on disk. The v
40600 61 6c 75 65 20 6f 66 20 52 20 64 65 70 65 6e 64 alue of R depend
40610 73 0a 2a 2a 20 6f 6e 20 74 68 65 20 53 51 4c 69 s.** on the SQLi
40620 74 65 20 76 65 72 73 69 6f 6e 2c 20 74 68 65 20 te version, the
40630 74 61 72 67 65 74 20 70 6c 61 74 66 6f 72 6d 2c target platform,
40640 20 61 6e 64 20 68 6f 77 20 53 51 4c 69 74 65 20 and how SQLite
40650 77 61 73 20 63 6f 6d 70 69 6c 65 64 2e 0a 2a 2a was compiled..**
40660 20 52 20 69 73 20 63 6f 6e 73 74 61 6e 74 20 66 R is constant f
40670 6f 72 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 or a particular
40680 62 75 69 6c 64 20 6f 66 20 53 51 4c 69 74 65 2e build of SQLite.
40690 20 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 The second arg
406a0 75 6d 65 6e 74 20 74 6f 0a 2a 2a 20 78 43 72 65 ument to.** xCre
406b0 61 74 65 28 29 2c 20 62 50 75 72 67 65 61 62 6c ate(), bPurgeabl
406c0 65 2c 20 69 73 20 74 72 75 65 20 69 66 20 74 68 e, is true if th
406d0 65 20 63 61 63 68 65 20 62 65 69 6e 67 20 63 72 e cache being cr
406e0 65 61 74 65 64 20 77 69 6c 6c 0a 2a 2a 20 62 65 eated will.** be
406f0 20 75 73 65 64 20 74 6f 20 63 61 63 68 65 20 64 used to cache d
40700 61 74 61 62 61 73 65 20 70 61 67 65 73 20 6f 66 atabase pages of
40710 20 61 20 66 69 6c 65 20 73 74 6f 72 65 64 20 6f a file stored o
40720 6e 20 64 69 73 6b 2c 20 6f 72 0a 2a 2a 20 66 61 n disk, or.** fa
40730 6c 73 65 20 69 66 20 69 74 20 69 73 20 75 73 65 lse if it is use
40740 64 20 66 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f d for an in-memo
40750 72 79 20 64 61 74 61 62 61 73 65 2e 20 54 68 65 ry database. The
40760 20 63 61 63 68 65 20 69 6d 70 6c 65 6d 65 6e 74 cache implement
40770 61 74 69 6f 6e 0a 2a 2a 20 64 6f 65 73 20 6e 6f ation.** does no
40780 74 20 68 61 76 65 20 74 6f 20 64 6f 20 61 6e 79 t have to do any
40790 74 68 69 6e 67 20 73 70 65 63 69 61 6c 20 62 61 thing special ba
407a0 73 65 64 20 77 69 74 68 20 74 68 65 20 76 61 6c sed with the val
407b0 75 65 20 6f 66 20 62 50 75 72 67 65 61 62 6c 65 ue of bPurgeable
407c0 3b 0a 2a 2a 20 69 74 20 69 73 20 70 75 72 65 6c ;.** it is purel
407d0 79 20 61 64 76 69 73 6f 72 79 2e 20 20 4f 6e 20 y advisory. On
407e0 61 20 63 61 63 68 65 20 77 68 65 72 65 20 62 50 a cache where bP
407f0 75 72 67 65 61 62 6c 65 20 69 73 20 66 61 6c 73 urgeable is fals
40800 65 2c 20 53 51 4c 69 74 65 20 77 69 6c 6c 0a 2a e, SQLite will.*
40810 2a 20 6e 65 76 65 72 20 69 6e 76 6f 6b 65 20 78 * never invoke x
40820 55 6e 70 69 6e 28 29 20 65 78 63 65 70 74 20 74 Unpin() except t
40830 6f 20 64 65 6c 69 62 65 72 61 74 65 6c 79 20 64 o deliberately d
40840 65 6c 65 74 65 20 61 20 70 61 67 65 2e 0a 2a 2a elete a page..**
40850 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c In other words,
40860 20 61 20 63 61 63 68 65 20 63 72 65 61 74 65 64 a cache created
40870 20 77 69 74 68 20 62 50 75 72 67 65 61 62 6c 65 with bPurgeable
40880 20 73 65 74 20 74 6f 20 66 61 6c 73 65 20 77 69 set to false wi
40890 6c 6c 0a 2a 2a 20 6e 65 76 65 72 20 63 6f 6e 74 ll.** never cont
408a0 61 69 6e 20 61 6e 79 20 75 6e 70 69 6e 6e 65 64 ain any unpinned
408b0 20 70 61 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 pages..**.** Th
408c0 65 20 78 43 61 63 68 65 73 69 7a 65 28 29 20 6d e xCachesize() m
408d0 65 74 68 6f 64 20 6d 61 79 20 62 65 20 63 61 6c ethod may be cal
408e0 6c 65 64 20 61 74 20 61 6e 79 20 74 69 6d 65 20 led at any time
408f0 62 79 20 53 51 4c 69 74 65 20 74 6f 20 73 65 74 by SQLite to set
40900 20 74 68 65 0a 2a 2a 20 73 75 67 67 65 73 74 65 the.** suggeste
40910 64 20 6d 61 78 69 6d 75 6d 20 63 61 63 68 65 2d d maximum cache-
40920 73 69 7a 65 20 28 6e 75 6d 62 65 72 20 6f 66 20 size (number of
40930 70 61 67 65 73 20 73 74 6f 72 65 64 20 62 79 29 pages stored by)
40940 20 74 68 65 20 63 61 63 68 65 0a 2a 2a 20 69 6e the cache.** in
40950 73 74 61 6e 63 65 20 70 61 73 73 65 64 20 61 73 stance passed as
40960 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d the first argum
40970 65 6e 74 2e 20 54 68 69 73 20 69 73 20 74 68 65 ent. This is the
40980 20 76 61 6c 75 65 20 63 6f 6e 66 69 67 75 72 65 value configure
40990 64 20 75 73 69 6e 67 0a 2a 2a 20 74 68 65 20 53 d using.** the S
409a0 51 4c 69 74 65 20 22 5b 50 52 41 47 4d 41 20 63 QLite "[PRAGMA c
409b0 61 63 68 65 5f 73 69 7a 65 5d 22 20 63 6f 6d 6d ache_size]" comm
409c0 61 6e 64 2e 20 41 73 20 77 69 74 68 20 74 68 65 and. As with the
409d0 20 62 50 75 72 67 65 61 62 6c 65 20 70 61 72 61 bPurgeable para
409e0 6d 65 74 65 72 2c 0a 2a 2a 20 74 68 65 20 69 6d meter,.** the im
409f0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 plementation is
40a00 6e 6f 74 20 72 65 71 75 69 72 65 64 20 74 6f 20 not required to
40a10 64 6f 20 61 6e 79 74 68 69 6e 67 20 77 69 74 68 do anything with
40a20 20 74 68 69 73 0a 2a 2a 20 76 61 6c 75 65 3b 20 this.** value;
40a30 69 74 20 69 73 20 61 64 76 69 73 6f 72 79 20 6f it is advisory o
40a40 6e 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 nly..**.** The x
40a50 50 61 67 65 63 6f 75 6e 74 28 29 20 6d 65 74 68 Pagecount() meth
40a60 6f 64 20 73 68 6f 75 6c 64 20 72 65 74 75 72 6e od should return
40a70 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 the number of p
40a80 61 67 65 73 20 63 75 72 72 65 6e 74 6c 79 0a 2a ages currently.*
40a90 2a 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 * stored in the
40aa0 63 61 63 68 65 2e 0a 2a 2a 20 0a 2a 2a 20 54 68 cache..** .** Th
40ab0 65 20 78 46 65 74 63 68 28 29 20 6d 65 74 68 6f e xFetch() metho
40ac0 64 20 69 73 20 75 73 65 64 20 74 6f 20 66 65 74 d is used to fet
40ad0 63 68 20 61 20 70 61 67 65 20 61 6e 64 20 72 65 ch a page and re
40ae0 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 turn a pointer t
40af0 6f 20 69 74 2e 20 0a 2a 2a 20 41 20 27 70 61 67 o it. .** A 'pag
40b00 65 27 2c 20 69 6e 20 74 68 69 73 20 63 6f 6e 74 e', in this cont
40b10 65 78 74 2c 20 69 73 20 61 20 62 75 66 66 65 72 ext, is a buffer
40b20 20 6f 66 20 73 7a 50 61 67 65 20 62 79 74 65 73 of szPage bytes
40b30 20 61 6c 69 67 6e 65 64 20 61 74 20 61 6e 0a 2a aligned at an.*
40b40 2a 20 38 2d 62 79 74 65 20 62 6f 75 6e 64 61 72 * 8-byte boundar
40b50 79 2e 20 54 68 65 20 70 61 67 65 20 74 6f 20 62 y. The page to b
40b60 65 20 66 65 74 63 68 65 64 20 69 73 20 64 65 74 e fetched is det
40b70 65 72 6d 69 6e 65 64 20 62 79 20 74 68 65 20 6b ermined by the k
40b80 65 79 2e 20 54 68 65 0a 2a 2a 20 6d 69 6d 69 6d ey. The.** mimim
40b90 75 6d 20 6b 65 79 20 76 61 6c 75 65 20 69 73 20 um key value is
40ba0 31 2e 20 41 66 74 65 72 20 69 74 20 68 61 73 20 1. After it has
40bb0 62 65 65 6e 20 72 65 74 72 69 65 76 65 64 20 75 been retrieved u
40bc0 73 69 6e 67 20 78 46 65 74 63 68 2c 20 74 68 65 sing xFetch, the
40bd0 20 70 61 67 65 20 0a 2a 2a 20 69 73 20 63 6f 6e page .** is con
40be0 73 69 64 65 72 65 64 20 74 6f 20 62 65 20 22 70 sidered to be "p
40bf0 69 6e 6e 65 64 22 2e 0a 2a 2a 0a 2a 2a 20 49 66 inned"..**.** If
40c00 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 70 the requested p
40c10 61 67 65 20 69 73 20 61 6c 72 65 61 64 79 20 69 age is already i
40c20 6e 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65 n the page cache
40c30 2c 20 74 68 65 6e 20 74 68 65 20 70 61 67 65 20 , then the page
40c40 63 61 63 68 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 cache.** impleme
40c50 6e 74 61 74 69 6f 6e 20 6d 75 73 74 20 72 65 74 ntation must ret
40c60 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f urn a pointer to
40c70 20 74 68 65 20 70 61 67 65 20 62 75 66 66 65 72 the page buffer
40c80 20 77 69 74 68 20 69 74 73 20 63 6f 6e 74 65 6e with its conten
40c90 74 0a 2a 2a 20 69 6e 74 61 63 74 2e 20 20 49 66 t.** intact. If
40ca0 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 70 the requested p
40cb0 61 67 65 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 age is not alrea
40cc0 64 79 20 69 6e 20 74 68 65 20 63 61 63 68 65 2c dy in the cache,
40cd0 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 62 65 68 then the.** beh
40ce0 61 76 69 6f 72 20 6f 66 20 74 68 65 20 63 61 63 avior of the cac
40cf0 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f he implementatio
40d00 6e 20 69 73 20 64 65 74 65 72 6d 69 6e 65 64 20 n is determined
40d10 62 79 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 by the value of
40d20 74 68 65 0a 2a 2a 20 63 72 65 61 74 65 46 6c 61 the.** createFla
40d30 67 20 70 61 72 61 6d 65 74 65 72 20 70 61 73 73 g parameter pass
40d40 65 64 20 74 6f 20 78 46 65 74 63 68 2c 20 61 63 ed to xFetch, ac
40d50 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 20 66 cording to the f
40d60 6f 6c 6c 6f 77 69 6e 67 20 74 61 62 6c 65 3a 0a ollowing table:.
40d70 2a 2a 0a 2a 2a 20 3c 74 61 62 6c 65 20 62 6f 72 **.** <table bor
40d80 64 65 72 3d 31 20 77 69 64 74 68 3d 38 35 25 20 der=1 width=85%
40d90 61 6c 69 67 6e 3d 63 65 6e 74 65 72 3e 0a 2a 2a align=center>.**
40da0 20 3c 74 72 3e 3c 74 68 3e 20 63 72 65 61 74 65 <tr><th> create
40db0 46 6c 61 67 20 3c 74 68 3e 20 42 65 68 61 76 69 Flag <th> Behavi
40dc0 6f 75 72 20 77 68 65 6e 20 70 61 67 65 20 69 73 our when page is
40dd0 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 69 6e 20 not already in
40de0 63 61 63 68 65 0a 2a 2a 20 3c 74 72 3e 3c 74 64 cache.** <tr><td
40df0 3e 20 30 20 3c 74 64 3e 20 44 6f 20 6e 6f 74 20 > 0 <td> Do not
40e00 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 70 allocate a new p
40e10 61 67 65 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c age. Return NUL
40e20 4c 2e 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 31 L..** <tr><td> 1
40e30 20 3c 74 64 3e 20 41 6c 6c 6f 63 61 74 65 20 61 <td> Allocate a
40e40 20 6e 65 77 20 70 61 67 65 20 69 66 20 69 74 20 new page if it
40e50 65 61 73 79 20 61 6e 64 20 63 6f 6e 76 65 6e 69 easy and conveni
40e60 65 6e 74 20 74 6f 20 64 6f 20 73 6f 2e 0a 2a 2a ent to do so..**
40e70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
40e80 20 4f 74 68 65 72 77 69 73 65 20 72 65 74 75 72 Otherwise retur
40e90 6e 20 4e 55 4c 4c 2e 0a 2a 2a 20 3c 74 72 3e 3c n NULL..** <tr><
40ea0 74 64 3e 20 32 20 3c 74 64 3e 20 4d 61 6b 65 20 td> 2 <td> Make
40eb0 65 76 65 72 79 20 65 66 66 6f 72 74 20 74 6f 20 every effort to
40ec0 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 70 allocate a new p
40ed0 61 67 65 2e 20 20 4f 6e 6c 79 20 72 65 74 75 72 age. Only retur
40ee0 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 n.**
40ef0 20 20 20 20 20 4e 55 4c 4c 20 69 66 20 61 6c 6c NULL if all
40f00 6f 63 61 74 69 6e 67 20 61 20 6e 65 77 20 70 61 ocating a new pa
40f10 67 65 20 69 73 20 65 66 66 65 63 74 69 76 65 6c ge is effectivel
40f20 79 20 69 6d 70 6f 73 73 69 62 6c 65 2e 0a 2a 2a y impossible..**
40f30 20 3c 2f 74 61 62 6c 65 3e 0a 2a 2a 0a 2a 2a 20 </table>.**.**
40f40 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 6f 72 6d SQLite will norm
40f50 61 6c 6c 79 20 69 6e 76 6f 6b 65 20 78 46 65 74 ally invoke xFet
40f60 63 68 28 29 20 77 69 74 68 20 61 20 63 72 65 61 ch() with a crea
40f70 74 65 46 6c 61 67 20 6f 66 20 30 20 6f 72 20 31 teFlag of 0 or 1
40f80 2e 20 20 49 66 0a 2a 2a 20 61 20 63 61 6c 6c 20 . If.** a call
40f90 74 6f 20 78 46 65 74 63 68 28 29 20 77 69 74 68 to xFetch() with
40fa0 20 63 72 65 61 74 65 46 6c 61 67 3d 3d 31 20 72 createFlag==1 r
40fb0 65 74 75 72 6e 73 20 4e 55 4c 4c 2c 20 74 68 65 eturns NULL, the
40fc0 6e 20 53 51 4c 69 74 65 20 77 69 6c 6c 0a 2a 2a n SQLite will.**
40fd0 20 61 74 74 65 6d 70 74 20 74 6f 20 75 6e 70 69 attempt to unpi
40fe0 6e 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 63 61 n one or more ca
40ff0 63 68 65 20 70 61 67 65 73 20 62 79 20 73 70 69 che pages by spi
41000 6c 6c 69 6e 67 20 74 68 65 20 63 6f 6e 74 65 6e lling the conten
41010 74 20 6f 66 0a 2a 2a 20 70 69 6e 6e 65 64 20 70 t of.** pinned p
41020 61 67 65 73 20 74 6f 20 64 69 73 6b 20 61 6e 64 ages to disk and
41030 20 73 79 6e 63 68 69 6e 67 20 74 68 65 20 6f 70 synching the op
41040 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 64 erating system d
41050 69 73 6b 20 63 61 63 68 65 2e 20 41 66 74 65 72 isk cache. After
41060 0a 2a 2a 20 61 74 74 65 6d 70 74 69 6e 67 20 74 .** attempting t
41070 6f 20 75 6e 70 69 6e 20 70 61 67 65 73 2c 20 74 o unpin pages, t
41080 68 65 20 78 46 65 74 63 68 28 29 20 6d 65 74 68 he xFetch() meth
41090 6f 64 20 77 69 6c 6c 20 62 65 20 69 6e 76 6f 6b od will be invok
410a0 65 64 20 61 67 61 69 6e 20 77 69 74 68 0a 2a 2a ed again with.**
410b0 20 61 20 63 72 65 61 74 65 46 6c 61 67 20 6f 66 a createFlag of
410c0 20 32 2e 0a 2a 2a 0a 2a 2a 20 78 55 6e 70 69 6e 2..**.** xUnpin
410d0 28 29 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 () is called by
410e0 53 51 4c 69 74 65 20 77 69 74 68 20 61 20 70 6f SQLite with a po
410f0 69 6e 74 65 72 20 74 6f 20 61 20 63 75 72 72 65 inter to a curre
41100 6e 74 6c 79 20 70 69 6e 6e 65 64 20 70 61 67 65 ntly pinned page
41110 0a 2a 2a 20 61 73 20 69 74 73 20 73 65 63 6f 6e .** as its secon
41120 64 20 61 72 67 75 6d 65 6e 74 2e 20 49 66 20 74 d argument. If t
41130 68 65 20 74 68 69 72 64 20 70 61 72 61 6d 65 74 he third paramet
41140 65 72 2c 20 64 69 73 63 61 72 64 2c 20 69 73 20 er, discard, is
41150 6e 6f 6e 2d 7a 65 72 6f 2c 0a 2a 2a 20 74 68 65 non-zero,.** the
41160 6e 20 74 68 65 20 70 61 67 65 20 73 68 6f 75 6c n the page shoul
41170 64 20 62 65 20 65 76 69 63 74 65 64 20 66 72 6f d be evicted fro
41180 6d 20 74 68 65 20 63 61 63 68 65 2e 20 49 6e 20 m the cache. In
41190 74 68 69 73 20 63 61 73 65 20 53 51 4c 69 74 65 this case SQLite
411a0 20 0a 2a 2a 20 61 73 73 75 6d 65 73 20 74 68 61 .** assumes tha
411b0 74 20 74 68 65 20 6e 65 78 74 20 74 69 6d 65 20 t the next time
411c0 74 68 65 20 70 61 67 65 20 69 73 20 72 65 74 72 the page is retr
411d0 69 65 76 65 64 20 66 72 6f 6d 20 74 68 65 20 63 ieved from the c
411e0 61 63 68 65 20 75 73 69 6e 67 0a 2a 2a 20 74 68 ache using.** th
411f0 65 20 78 46 65 74 63 68 28 29 20 6d 65 74 68 6f e xFetch() metho
41200 64 2c 20 69 74 20 77 69 6c 6c 20 62 65 20 7a 65 d, it will be ze
41210 72 6f 65 64 2e 20 49 66 20 74 68 65 20 64 69 73 roed. If the dis
41220 63 61 72 64 20 70 61 72 61 6d 65 74 65 72 20 69 card parameter i
41230 73 0a 2a 2a 20 7a 65 72 6f 2c 20 74 68 65 6e 20 s.** zero, then
41240 74 68 65 20 70 61 67 65 20 69 73 20 63 6f 6e 73 the page is cons
41250 69 64 65 72 65 64 20 74 6f 20 62 65 20 75 6e 70 idered to be unp
41260 69 6e 6e 65 64 2e 20 54 68 65 20 63 61 63 68 65 inned. The cache
41270 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a implementation.
41280 2a 2a 20 6d 61 79 20 63 68 6f 6f 73 65 20 74 6f ** may choose to
41290 20 65 76 69 63 74 20 75 6e 70 69 6e 6e 65 64 20 evict unpinned
412a0 70 61 67 65 73 20 61 74 20 61 6e 79 20 74 69 6d pages at any tim
412b0 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 63 e..**.** The cac
412c0 68 65 20 69 73 20 6e 6f 74 20 72 65 71 75 69 72 he is not requir
412d0 65 64 20 74 6f 20 70 65 72 66 6f 72 6d 20 61 6e ed to perform an
412e0 79 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e y reference coun
412f0 74 69 6e 67 2e 20 41 20 73 69 6e 67 6c 65 20 0a ting. A single .
41300 2a 2a 20 63 61 6c 6c 20 74 6f 20 78 55 6e 70 69 ** call to xUnpi
41310 6e 28 29 20 75 6e 70 69 6e 73 20 74 68 65 20 70 n() unpins the p
41320 61 67 65 20 72 65 67 61 72 64 6c 65 73 73 20 6f age regardless o
41330 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 f the number of
41340 70 72 69 6f 72 20 63 61 6c 6c 73 20 0a 2a 2a 20 prior calls .**
41350 74 6f 20 78 46 65 74 63 68 28 29 2e 0a 2a 2a 0a to xFetch()..**.
41360 2a 2a 20 54 68 65 20 78 52 65 6b 65 79 28 29 20 ** The xRekey()
41370 6d 65 74 68 6f 64 20 69 73 20 75 73 65 64 20 74 method is used t
41380 6f 20 63 68 61 6e 67 65 20 74 68 65 20 6b 65 79 o change the key
41390 20 76 61 6c 75 65 20 61 73 73 6f 63 69 61 74 65 value associate
413a0 64 20 77 69 74 68 20 74 68 65 0a 2a 2a 20 70 61 d with the.** pa
413b0 67 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65 ge passed as the
413c0 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 second argument
413d0 20 66 72 6f 6d 20 6f 6c 64 4b 65 79 20 74 6f 20 from oldKey to
413e0 6e 65 77 4b 65 79 2e 20 49 66 20 74 68 65 20 63 newKey. If the c
413f0 61 63 68 65 0a 2a 2a 20 70 72 65 76 69 6f 75 73 ache.** previous
41400 6c 79 20 63 6f 6e 74 61 69 6e 73 20 61 6e 20 65 ly contains an e
41410 6e 74 72 79 20 61 73 73 6f 63 69 61 74 65 64 20 ntry associated
41420 77 69 74 68 20 6e 65 77 4b 65 79 2c 20 69 74 20 with newKey, it
41430 73 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 64 69 73 should be.** dis
41440 63 61 72 64 65 64 2e 20 41 6e 79 20 70 72 69 6f carded. Any prio
41450 72 20 63 61 63 68 65 20 65 6e 74 72 79 20 61 73 r cache entry as
41460 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 6e 65 sociated with ne
41470 77 4b 65 79 20 69 73 20 67 75 61 72 61 6e 74 65 wKey is guarante
41480 65 64 20 6e 6f 74 0a 2a 2a 20 74 6f 20 62 65 20 ed not.** to be
41490 70 69 6e 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 57 68 pinned..**.** Wh
414a0 65 6e 20 53 51 4c 69 74 65 20 63 61 6c 6c 73 20 en SQLite calls
414b0 74 68 65 20 78 54 72 75 6e 63 61 74 65 28 29 20 the xTruncate()
414c0 6d 65 74 68 6f 64 2c 20 74 68 65 20 63 61 63 68 method, the cach
414d0 65 20 6d 75 73 74 20 64 69 73 63 61 72 64 20 61 e must discard a
414e0 6c 6c 0a 2a 2a 20 65 78 69 73 74 69 6e 67 20 63 ll.** existing c
414f0 61 63 68 65 20 65 6e 74 72 69 65 73 20 77 69 74 ache entries wit
41500 68 20 70 61 67 65 20 6e 75 6d 62 65 72 73 20 28 h page numbers (
41510 6b 65 79 73 29 20 67 72 65 61 74 65 72 20 74 68 keys) greater th
41520 61 6e 20 6f 72 20 65 71 75 61 6c 0a 2a 2a 20 74 an or equal.** t
41530 6f 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 o the value of t
41540 68 65 20 69 4c 69 6d 69 74 20 70 61 72 61 6d 65 he iLimit parame
41550 74 65 72 20 70 61 73 73 65 64 20 74 6f 20 78 54 ter passed to xT
41560 72 75 6e 63 61 74 65 28 29 2e 20 49 66 20 61 6e runcate(). If an
41570 79 0a 2a 2a 20 6f 66 20 74 68 65 73 65 20 70 61 y.** of these pa
41580 67 65 73 20 61 72 65 20 70 69 6e 6e 65 64 2c 20 ges are pinned,
41590 74 68 65 79 20 61 72 65 20 69 6d 70 6c 69 63 69 they are implici
415a0 74 6c 79 20 75 6e 70 69 6e 6e 65 64 2c 20 6d 65 tly unpinned, me
415b0 61 6e 69 6e 67 20 74 68 61 74 0a 2a 2a 20 74 68 aning that.** th
415c0 65 79 20 63 61 6e 20 62 65 20 73 61 66 65 6c 79 ey can be safely
415d0 20 64 69 73 63 61 72 64 65 64 2e 0a 2a 2a 0a 2a discarded..**.*
415e0 2a 20 54 68 65 20 78 44 65 73 74 72 6f 79 28 29 * The xDestroy()
415f0 20 6d 65 74 68 6f 64 20 69 73 20 75 73 65 64 20 method is used
41600 74 6f 20 64 65 6c 65 74 65 20 61 20 63 61 63 68 to delete a cach
41610 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 78 e allocated by x
41620 43 72 65 61 74 65 28 29 2e 0a 2a 2a 20 41 6c 6c Create()..** All
41630 20 72 65 73 6f 75 72 63 65 73 20 61 73 73 6f 63 resources assoc
41640 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 73 iated with the s
41650 70 65 63 69 66 69 65 64 20 63 61 63 68 65 20 73 pecified cache s
41660 68 6f 75 6c 64 20 62 65 20 66 72 65 65 64 2e 20 hould be freed.
41670 41 66 74 65 72 0a 2a 2a 20 63 61 6c 6c 69 6e 67 After.** calling
41680 20 74 68 65 20 78 44 65 73 74 72 6f 79 28 29 20 the xDestroy()
41690 6d 65 74 68 6f 64 2c 20 53 51 4c 69 74 65 20 63 method, SQLite c
416a0 6f 6e 73 69 64 65 72 73 20 74 68 65 20 5b 73 71 onsiders the [sq
416b0 6c 69 74 65 33 5f 70 63 61 63 68 65 2a 5d 0a 2a lite3_pcache*].*
416c0 2a 20 68 61 6e 64 6c 65 20 69 6e 76 61 6c 69 64 * handle invalid
416d0 2c 20 61 6e 64 20 77 69 6c 6c 20 6e 6f 74 20 75 , and will not u
416e0 73 65 20 69 74 20 77 69 74 68 20 61 6e 79 20 6f se it with any o
416f0 74 68 65 72 20 73 71 6c 69 74 65 33 5f 70 63 61 ther sqlite3_pca
41700 63 68 65 5f 6d 65 74 68 6f 64 73 0a 2a 2a 20 66 che_methods.** f
41710 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2f 0a 74 79 70 unctions..*/.typ
41720 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 edef struct sqli
41730 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f te3_pcache_metho
41740 64 73 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 ds sqlite3_pcach
41750 65 5f 6d 65 74 68 6f 64 73 3b 0a 73 74 72 75 63 e_methods;.struc
41760 74 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 t sqlite3_pcache
41770 5f 6d 65 74 68 6f 64 73 20 7b 0a 20 20 76 6f 69 _methods {. voi
41780 64 20 2a 70 41 72 67 3b 0a 20 20 69 6e 74 20 28 d *pArg;. int (
41790 2a 78 49 6e 69 74 29 28 76 6f 69 64 2a 29 3b 0a *xInit)(void*);.
417a0 20 20 76 6f 69 64 20 28 2a 78 53 68 75 74 64 6f void (*xShutdo
417b0 77 6e 29 28 76 6f 69 64 2a 29 3b 0a 20 20 73 71 wn)(void*);. sq
417c0 6c 69 74 65 33 5f 70 63 61 63 68 65 20 2a 28 2a lite3_pcache *(*
417d0 78 43 72 65 61 74 65 29 28 69 6e 74 20 73 7a 50 xCreate)(int szP
417e0 61 67 65 2c 20 69 6e 74 20 62 50 75 72 67 65 61 age, int bPurgea
417f0 62 6c 65 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 ble);. void (*x
41800 43 61 63 68 65 73 69 7a 65 29 28 73 71 6c 69 74 Cachesize)(sqlit
41810 65 33 5f 70 63 61 63 68 65 2a 2c 20 69 6e 74 20 e3_pcache*, int
41820 6e 43 61 63 68 65 73 69 7a 65 29 3b 0a 20 20 69 nCachesize);. i
41830 6e 74 20 28 2a 78 50 61 67 65 63 6f 75 6e 74 29 nt (*xPagecount)
41840 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2a (sqlite3_pcache*
41850 29 3b 0a 20 20 76 6f 69 64 20 2a 28 2a 78 46 65 );. void *(*xFe
41860 74 63 68 29 28 73 71 6c 69 74 65 33 5f 70 63 61 tch)(sqlite3_pca
41870 63 68 65 2a 2c 20 75 6e 73 69 67 6e 65 64 20 6b che*, unsigned k
41880 65 79 2c 20 69 6e 74 20 63 72 65 61 74 65 46 6c ey, int createFl
41890 61 67 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 55 ag);. void (*xU
418a0 6e 70 69 6e 29 28 73 71 6c 69 74 65 33 5f 70 63 npin)(sqlite3_pc
418b0 61 63 68 65 2a 2c 20 76 6f 69 64 2a 2c 20 69 6e ache*, void*, in
418c0 74 20 64 69 73 63 61 72 64 29 3b 0a 20 20 76 6f t discard);. vo
418d0 69 64 20 28 2a 78 52 65 6b 65 79 29 28 73 71 6c id (*xRekey)(sql
418e0 69 74 65 33 5f 70 63 61 63 68 65 2a 2c 20 76 6f ite3_pcache*, vo
418f0 69 64 2a 2c 20 75 6e 73 69 67 6e 65 64 20 6f 6c id*, unsigned ol
41900 64 4b 65 79 2c 20 75 6e 73 69 67 6e 65 64 20 6e dKey, unsigned n
41910 65 77 4b 65 79 29 3b 0a 20 20 76 6f 69 64 20 28 ewKey);. void (
41920 2a 78 54 72 75 6e 63 61 74 65 29 28 73 71 6c 69 *xTruncate)(sqli
41930 74 65 33 5f 70 63 61 63 68 65 2a 2c 20 75 6e 73 te3_pcache*, uns
41940 69 67 6e 65 64 20 69 4c 69 6d 69 74 29 3b 0a 20 igned iLimit);.
41950 20 76 6f 69 64 20 28 2a 78 44 65 73 74 72 6f 79 void (*xDestroy
41960 29 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 )(sqlite3_pcache
41970 2a 29 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 *);.};../*.** CA
41980 50 49 33 52 45 46 3a 20 4f 6e 6c 69 6e 65 20 42 PI3REF: Online B
41990 61 63 6b 75 70 20 4f 62 6a 65 63 74 0a 2a 2a 20 ackup Object.**
419a0 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a EXPERIMENTAL.**.
419b0 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 62 ** The sqlite3_b
419c0 61 63 6b 75 70 20 6f 62 6a 65 63 74 20 72 65 63 ackup object rec
419d0 6f 72 64 73 20 73 74 61 74 65 20 69 6e 66 6f 72 ords state infor
419e0 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 61 6e 20 mation about an
419f0 6f 6e 67 6f 69 6e 67 0a 2a 2a 20 6f 6e 6c 69 6e ongoing.** onlin
41a00 65 20 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 e backup operati
41a10 6f 6e 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 on. The sqlite3
41a20 5f 62 61 63 6b 75 70 20 6f 62 6a 65 63 74 20 69 _backup object i
41a30 73 20 63 72 65 61 74 65 64 20 62 79 0a 2a 2a 20 s created by.**
41a40 61 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 a call to [sqlit
41a50 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 29 e3_backup_init()
41a60 5d 20 61 6e 64 20 69 73 20 64 65 73 74 72 6f 79 ] and is destroy
41a70 65 64 20 62 79 20 61 20 63 61 6c 6c 20 74 6f 0a ed by a call to.
41a80 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 61 63 6b ** [sqlite3_back
41a90 75 70 5f 66 69 6e 69 73 68 28 29 5d 2e 0a 2a 2a up_finish()]..**
41aa0 0a 2a 2a 20 53 65 65 20 41 6c 73 6f 3a 20 5b 55 .** See Also: [U
41ab0 73 69 6e 67 20 74 68 65 20 53 51 4c 69 74 65 20 sing the SQLite
41ac0 4f 6e 6c 69 6e 65 20 42 61 63 6b 75 70 20 41 50 Online Backup AP
41ad0 49 5d 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 I].*/.typedef st
41ae0 72 75 63 74 20 73 71 6c 69 74 65 33 5f 62 61 63 ruct sqlite3_bac
41af0 6b 75 70 20 73 71 6c 69 74 65 33 5f 62 61 63 6b kup sqlite3_back
41b00 75 70 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 up;../*.** CAPI3
41b10 52 45 46 3a 20 4f 6e 6c 69 6e 65 20 42 61 63 6b REF: Online Back
41b20 75 70 20 41 50 49 2e 0a 2a 2a 20 45 58 50 45 52 up API..** EXPER
41b30 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 IMENTAL.**.** Th
41b40 69 73 20 41 50 49 20 69 73 20 75 73 65 64 20 74 is API is used t
41b50 6f 20 6f 76 65 72 77 72 69 74 65 20 74 68 65 20 o overwrite the
41b60 63 6f 6e 74 65 6e 74 73 20 6f 66 20 6f 6e 65 20 contents of one
41b70 64 61 74 61 62 61 73 65 20 77 69 74 68 20 74 68 database with th
41b80 61 74 0a 2a 2a 20 6f 66 20 61 6e 6f 74 68 65 72 at.** of another
41b90 2e 20 49 74 20 69 73 20 75 73 65 66 75 6c 20 65 . It is useful e
41ba0 69 74 68 65 72 20 66 6f 72 20 63 72 65 61 74 69 ither for creati
41bb0 6e 67 20 62 61 63 6b 75 70 73 20 6f 66 20 64 61 ng backups of da
41bc0 74 61 62 61 73 65 73 20 6f 72 0a 2a 2a 20 66 6f tabases or.** fo
41bd0 72 20 63 6f 70 79 69 6e 67 20 69 6e 2d 6d 65 6d r copying in-mem
41be0 6f 72 79 20 64 61 74 61 62 61 73 65 73 20 74 6f ory databases to
41bf0 20 6f 72 20 66 72 6f 6d 20 70 65 72 73 69 73 74 or from persist
41c00 65 6e 74 20 66 69 6c 65 73 2e 20 0a 2a 2a 0a 2a ent files. .**.*
41c10 2a 20 53 65 65 20 41 6c 73 6f 3a 20 5b 55 73 69 * See Also: [Usi
41c20 6e 67 20 74 68 65 20 53 51 4c 69 74 65 20 4f 6e ng the SQLite On
41c30 6c 69 6e 65 20 42 61 63 6b 75 70 20 41 50 49 5d line Backup API]
41c40 0a 2a 2a 0a 2a 2a 20 45 78 63 6c 75 73 69 76 65 .**.** Exclusive
41c50 20 61 63 63 65 73 73 20 69 73 20 72 65 71 75 69 access is requi
41c60 72 65 64 20 74 6f 20 74 68 65 20 64 65 73 74 69 red to the desti
41c70 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 20 nation database
41c80 66 6f 72 20 74 68 65 20 0a 2a 2a 20 64 75 72 61 for the .** dura
41c90 74 69 6f 6e 20 6f 66 20 74 68 65 20 6f 70 65 72 tion of the oper
41ca0 61 74 69 6f 6e 2e 20 48 6f 77 65 76 65 72 20 74 ation. However t
41cb0 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 he source databa
41cc0 73 65 20 69 73 20 6f 6e 6c 79 0a 2a 2a 20 72 65 se is only.** re
41cd0 61 64 2d 6c 6f 63 6b 65 64 20 77 68 69 6c 65 20 ad-locked while
41ce0 69 74 20 69 73 20 61 63 74 75 61 6c 6c 79 20 62 it is actually b
41cf0 65 69 6e 67 20 72 65 61 64 2c 20 69 74 20 69 73 eing read, it is
41d00 20 6e 6f 74 20 6c 6f 63 6b 65 64 0a 2a 2a 20 63 not locked.** c
41d10 6f 6e 74 69 6e 75 6f 75 73 6c 79 20 66 6f 72 20 ontinuously for
41d20 74 68 65 20 65 6e 74 69 72 65 20 6f 70 65 72 61 the entire opera
41d30 74 69 6f 6e 2e 20 54 68 75 73 2c 20 74 68 65 20 tion. Thus, the
41d40 62 61 63 6b 75 70 20 6d 61 79 20 62 65 0a 2a 2a backup may be.**
41d50 20 70 65 72 66 6f 72 6d 65 64 20 6f 6e 20 61 20 performed on a
41d60 6c 69 76 65 20 64 61 74 61 62 61 73 65 20 77 69 live database wi
41d70 74 68 6f 75 74 20 70 72 65 76 65 6e 74 69 6e 67 thout preventing
41d80 20 6f 74 68 65 72 20 75 73 65 72 73 20 66 72 6f other users fro
41d90 6d 0a 2a 2a 20 77 72 69 74 69 6e 67 20 74 6f 20 m.** writing to
41da0 74 68 65 20 64 61 74 61 62 61 73 65 20 66 6f 72 the database for
41db0 20 61 6e 20 65 78 74 65 6e 64 65 64 20 70 65 72 an extended per
41dc0 69 6f 64 20 6f 66 20 74 69 6d 65 2e 0a 2a 2a 20 iod of time..**
41dd0 0a 2a 2a 20 54 6f 20 70 65 72 66 6f 72 6d 20 61 .** To perform a
41de0 20 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f backup operatio
41df0 6e 3a 20 0a 2a 2a 20 20 20 3c 6f 6c 3e 0a 2a 2a n: .** <ol>.**
41e00 20 20 20 20 20 3c 6c 69 3e 3c 62 3e 73 71 6c 69 <li><b>sqli
41e10 74 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 te3_backup_init(
41e20 29 3c 2f 62 3e 20 69 73 20 63 61 6c 6c 65 64 20 )</b> is called
41e30 6f 6e 63 65 20 74 6f 20 69 6e 69 74 69 61 6c 69 once to initiali
41e40 7a 65 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 ze the.**
41e50 20 20 62 61 63 6b 75 70 2c 20 0a 2a 2a 20 20 20 backup, .**
41e60 20 20 3c 6c 69 3e 3c 62 3e 73 71 6c 69 74 65 33 <li><b>sqlite3
41e70 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 3c 2f _backup_step()</
41e80 62 3e 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 65 b> is called one
41e90 20 6f 72 20 6d 6f 72 65 20 74 69 6d 65 73 20 74 or more times t
41ea0 6f 20 74 72 61 6e 73 66 65 72 20 0a 2a 2a 20 20 o transfer .**
41eb0 20 20 20 20 20 20 20 74 68 65 20 64 61 74 61 20 the data
41ec0 62 65 74 77 65 65 6e 20 74 68 65 20 74 77 6f 20 between the two
41ed0 64 61 74 61 62 61 73 65 73 2c 20 61 6e 64 20 66 databases, and f
41ee0 69 6e 61 6c 6c 79 0a 2a 2a 20 20 20 20 20 3c 6c inally.** <l
41ef0 69 3e 3c 62 3e 73 71 6c 69 74 65 33 5f 62 61 63 i><b>sqlite3_bac
41f00 6b 75 70 5f 66 69 6e 69 73 68 28 29 3c 2f 62 3e kup_finish()</b>
41f10 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 72 65 is called to re
41f20 6c 65 61 73 65 20 61 6c 6c 20 72 65 73 6f 75 72 lease all resour
41f30 63 65 73 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 ces .**
41f40 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
41f50 74 68 65 20 62 61 63 6b 75 70 20 6f 70 65 72 61 the backup opera
41f60 74 69 6f 6e 2e 20 0a 2a 2a 20 20 20 3c 2f 6f 6c tion. .** </ol
41f70 3e 0a 2a 2a 20 54 68 65 72 65 20 73 68 6f 75 6c >.** There shoul
41f80 64 20 62 65 20 65 78 61 63 74 6c 79 20 6f 6e 65 d be exactly one
41f90 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 call to sqlite3
41fa0 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 29 _backup_finish()
41fb0 20 66 6f 72 20 65 61 63 68 0a 2a 2a 20 73 75 63 for each.** suc
41fc0 63 65 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f 20 cessful call to
41fd0 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 69 sqlite3_backup_i
41fe0 6e 69 74 28 29 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e nit()..**.** <b>
41ff0 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 69 sqlite3_backup_i
42000 6e 69 74 28 29 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20 nit()</b>.**.**
42010 54 68 65 20 66 69 72 73 74 20 74 77 6f 20 61 72 The first two ar
42020 67 75 6d 65 6e 74 73 20 70 61 73 73 65 64 20 74 guments passed t
42030 6f 20 5b 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 o [sqlite3_backu
42040 70 5f 69 6e 69 74 28 29 5d 20 61 72 65 20 74 68 p_init()] are th
42050 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 68 61 e database.** ha
42060 6e 64 6c 65 20 61 73 73 6f 63 69 61 74 65 64 20 ndle associated
42070 77 69 74 68 20 74 68 65 20 64 65 73 74 69 6e 61 with the destina
42080 74 69 6f 6e 20 64 61 74 61 62 61 73 65 20 61 6e tion database an
42090 64 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6e d the database n
420a0 61 6d 65 20 0a 2a 2a 20 75 73 65 64 20 74 6f 20 ame .** used to
420b0 61 74 74 61 63 68 20 74 68 65 20 64 65 73 74 69 attach the desti
420c0 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 20 nation database
420d0 74 6f 20 74 68 65 20 68 61 6e 64 6c 65 2e 20 54 to the handle. T
420e0 68 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 he database name
420f0 0a 2a 2a 20 69 73 20 22 6d 61 69 6e 22 20 66 6f .** is "main" fo
42100 72 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 r the main datab
42110 61 73 65 2c 20 22 74 65 6d 70 22 20 66 6f 72 20 ase, "temp" for
42120 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 64 61 the temporary da
42130 74 61 62 61 73 65 2c 20 6f 72 0a 2a 2a 20 74 68 tabase, or.** th
42140 65 20 6e 61 6d 65 20 73 70 65 63 69 66 69 65 64 e name specified
42150 20 61 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 as part of the
42160 5b 41 54 54 41 43 48 5d 20 73 74 61 74 65 6d 65 [ATTACH] stateme
42170 6e 74 20 69 66 20 74 68 65 20 64 65 73 74 69 6e nt if the destin
42180 61 74 69 6f 6e 20 69 73 0a 2a 2a 20 61 6e 20 61 ation is.** an a
42190 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65 ttached database
421a0 2e 20 54 68 65 20 74 68 69 72 64 20 61 6e 64 20 . The third and
421b0 66 6f 75 72 74 68 20 61 72 67 75 6d 65 6e 74 73 fourth arguments
421c0 20 70 61 73 73 65 64 20 74 6f 20 0a 2a 2a 20 73 passed to .** s
421d0 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 69 6e qlite3_backup_in
421e0 69 74 28 29 20 69 64 65 6e 74 69 66 79 20 74 68 it() identify th
421f0 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e e [database conn
42200 65 63 74 69 6f 6e 5d 0a 2a 2a 20 61 6e 64 20 64 ection].** and d
42210 61 74 61 62 61 73 65 20 6e 61 6d 65 20 75 73 65 atabase name use
42220 64 0a 2a 2a 20 74 6f 20 61 63 63 65 73 73 20 74 d.** to access t
42230 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 he source databa
42240 73 65 2e 20 54 68 65 20 76 61 6c 75 65 73 20 70 se. The values p
42250 61 73 73 65 64 20 66 6f 72 20 74 68 65 20 73 6f assed for the so
42260 75 72 63 65 20 61 6e 64 20 0a 2a 2a 20 64 65 73 urce and .** des
42270 74 69 6e 61 74 69 6f 6e 20 5b 64 61 74 61 62 61 tination [databa
42280 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 70 se connection] p
42290 61 72 61 6d 65 74 65 72 73 20 6d 75 73 74 20 6e arameters must n
422a0 6f 74 20 62 65 20 74 68 65 20 73 61 6d 65 2e 0a ot be the same..
422b0 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f **.** If an erro
422c0 72 20 6f 63 63 75 72 73 20 77 69 74 68 69 6e 20 r occurs within
422d0 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 69 sqlite3_backup_i
422e0 6e 69 74 28 29 2c 20 74 68 65 6e 20 4e 55 4c 4c nit(), then NULL
422f0 20 69 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 is returned.**
42300 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 and an error cod
42310 65 20 61 6e 64 20 65 72 72 6f 72 20 6d 65 73 73 e and error mess
42320 61 67 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f age written into
42330 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 the [database c
42340 6f 6e 6e 65 63 74 69 6f 6e 5d 20 0a 2a 2a 20 70 onnection] .** p
42350 61 73 73 65 64 20 61 73 20 74 68 65 20 66 69 72 assed as the fir
42360 73 74 20 61 72 67 75 6d 65 6e 74 2e 20 54 68 65 st argument. The
42370 79 20 6d 61 79 20 62 65 20 72 65 74 72 69 65 76 y may be retriev
42380 65 64 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 ed using the.**
42390 5b 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 [sqlite3_errcode
423a0 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 65 72 ()], [sqlite3_er
423b0 72 6d 73 67 28 29 5d 2c 20 61 6e 64 20 5b 73 71 rmsg()], and [sq
423c0 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36 28 29 lite3_errmsg16()
423d0 5d 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 20 ] functions..**
423e0 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 73 75 Otherwise, if su
423f0 63 63 65 73 73 66 75 6c 2c 20 61 20 70 6f 69 6e ccessful, a poin
42400 74 65 72 20 74 6f 20 61 6e 20 5b 73 71 6c 69 74 ter to an [sqlit
42410 65 33 5f 62 61 63 6b 75 70 5d 20 6f 62 6a 65 63 e3_backup] objec
42420 74 20 69 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64 t is.** returned
42430 2e 20 54 68 69 73 20 70 6f 69 6e 74 65 72 20 6d . This pointer m
42440 61 79 20 62 65 20 75 73 65 64 20 77 69 74 68 20 ay be used with
42450 74 68 65 20 73 71 6c 69 74 65 33 5f 62 61 63 6b the sqlite3_back
42460 75 70 5f 73 74 65 70 28 29 20 61 6e 64 0a 2a 2a up_step() and.**
42470 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f sqlite3_backup_
42480 66 69 6e 69 73 68 28 29 20 66 75 6e 63 74 69 6f finish() functio
42490 6e 73 20 74 6f 20 70 65 72 66 6f 72 6d 20 74 68 ns to perform th
424a0 65 20 73 70 65 63 69 66 69 65 64 20 62 61 63 6b e specified back
424b0 75 70 20 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e up .** operation
424c0 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 73 71 6c 69 74 ..**.** <b>sqlit
424d0 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 e3_backup_step()
424e0 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20 46 75 6e 63 74 </b>.**.** Funct
424f0 69 6f 6e 20 5b 73 71 6c 69 74 65 33 5f 62 61 63 ion [sqlite3_bac
42500 6b 75 70 5f 73 74 65 70 28 29 5d 20 69 73 20 75 kup_step()] is u
42510 73 65 64 20 74 6f 20 63 6f 70 79 20 75 70 20 74 sed to copy up t
42520 6f 20 6e 50 61 67 65 20 70 61 67 65 73 20 62 65 o nPage pages be
42530 74 77 65 65 6e 20 0a 2a 2a 20 74 68 65 20 73 6f tween .** the so
42540 75 72 63 65 20 61 6e 64 20 64 65 73 74 69 6e 61 urce and destina
42550 74 69 6f 6e 20 64 61 74 61 62 61 73 65 73 2c 20 tion databases,
42560 77 68 65 72 65 20 6e 50 61 67 65 20 69 73 20 74 where nPage is t
42570 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 he value of the
42580 0a 2a 2a 20 73 65 63 6f 6e 64 20 70 61 72 61 6d .** second param
42590 65 74 65 72 20 70 61 73 73 65 64 20 74 6f 20 73 eter passed to s
425a0 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 qlite3_backup_st
425b0 65 70 28 29 2e 20 49 66 20 6e 50 61 67 65 20 69 ep(). If nPage i
425c0 73 20 61 20 6e 65 67 61 74 69 76 65 0a 2a 2a 20 s a negative.**
425d0 76 61 6c 75 65 2c 20 61 6c 6c 20 72 65 6d 61 69 value, all remai
425e0 6e 69 6e 67 20 73 6f 75 72 63 65 20 70 61 67 65 ning source page
425f0 73 20 61 72 65 20 63 6f 70 69 65 64 2e 20 49 66 s are copied. If
42600 20 74 68 65 20 72 65 71 75 69 72 65 64 20 70 61 the required pa
42610 67 65 73 20 61 72 65 20 0a 2a 2a 20 73 75 63 63 ges are .** succ
42620 65 73 66 75 6c 6c 79 20 63 6f 70 69 65 64 2c 20 esfully copied,
42630 62 75 74 20 74 68 65 72 65 20 61 72 65 20 73 74 but there are st
42640 69 6c 6c 20 6d 6f 72 65 20 70 61 67 65 73 20 74 ill more pages t
42650 6f 20 63 6f 70 79 20 62 65 66 6f 72 65 20 74 68 o copy before th
42660 65 20 0a 2a 2a 20 62 61 63 6b 75 70 20 69 73 20 e .** backup is
42670 63 6f 6d 70 6c 65 74 65 2c 20 69 74 20 72 65 74 complete, it ret
42680 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d urns [SQLITE_OK]
42690 2e 20 49 66 20 6e 6f 20 65 72 72 6f 72 20 6f 63 . If no error oc
426a0 63 75 72 65 64 20 61 6e 64 20 74 68 65 72 65 20 cured and there
426b0 0a 2a 2a 20 61 72 65 20 6e 6f 20 6d 6f 72 65 20 .** are no more
426c0 70 61 67 65 73 20 74 6f 20 63 6f 70 79 2c 20 74 pages to copy, t
426d0 68 65 6e 20 5b 53 51 4c 49 54 45 5f 44 4f 4e 45 hen [SQLITE_DONE
426e0 5d 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49 ] is returned. I
426f0 66 20 61 6e 20 65 72 72 6f 72 20 0a 2a 2a 20 6f f an error .** o
42700 63 63 75 72 73 2c 20 74 68 65 6e 20 61 6e 20 53 ccurs, then an S
42710 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65 QLite error code
42720 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 41 73 is returned. As
42730 20 77 65 6c 6c 20 61 73 20 5b 53 51 4c 49 54 45 well as [SQLITE
42740 5f 4f 4b 5d 20 61 6e 64 0a 2a 2a 20 5b 53 51 4c _OK] and.** [SQL
42750 49 54 45 5f 44 4f 4e 45 5d 2c 20 61 20 63 61 6c ITE_DONE], a cal
42760 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 l to sqlite3_bac
42770 6b 75 70 5f 73 74 65 70 28 29 20 6d 61 79 20 72 kup_step() may r
42780 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f 52 45 eturn [SQLITE_RE
42790 41 44 4f 4e 4c 59 5d 2c 0a 2a 2a 20 5b 53 51 4c ADONLY],.** [SQL
427a0 49 54 45 5f 4e 4f 4d 45 4d 5d 2c 20 5b 53 51 4c ITE_NOMEM], [SQL
427b0 49 54 45 5f 42 55 53 59 5d 2c 20 5b 53 51 4c 49 ITE_BUSY], [SQLI
427c0 54 45 5f 4c 4f 43 4b 45 44 5d 2c 20 6f 72 20 61 TE_LOCKED], or a
427d0 6e 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 49 4f 45 n.** [SQLITE_IOE
427e0 52 52 5f 41 43 43 45 53 53 20 7c 20 53 51 4c 49 RR_ACCESS | SQLI
427f0 54 45 5f 49 4f 45 52 52 5f 58 58 58 5d 20 65 78 TE_IOERR_XXX] ex
42800 74 65 6e 64 65 64 20 65 72 72 6f 72 20 63 6f 64 tended error cod
42810 65 2e 0a 2a 2a 0a 2a 2a 20 41 73 20 77 65 6c 6c e..**.** As well
42820 20 61 73 20 74 68 65 20 63 61 73 65 20 77 68 65 as the case whe
42830 72 65 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 re the destinati
42840 6f 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 on database file
42850 20 77 61 73 20 6f 70 65 6e 65 64 20 66 6f 72 0a was opened for.
42860 2a 2a 20 72 65 61 64 2d 6f 6e 6c 79 20 61 63 63 ** read-only acc
42870 65 73 73 2c 20 73 71 6c 69 74 65 33 5f 62 61 63 ess, sqlite3_bac
42880 6b 75 70 5f 73 74 65 70 28 29 20 6d 61 79 20 72 kup_step() may r
42890 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f 52 45 eturn [SQLITE_RE
428a0 41 44 4f 4e 4c 59 5d 20 69 66 0a 2a 2a 20 74 68 ADONLY] if.** th
428b0 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 69 73 e destination is
428c0 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 an in-memory da
428d0 74 61 62 61 73 65 20 77 69 74 68 20 61 20 64 69 tabase with a di
428e0 66 66 65 72 65 6e 74 20 70 61 67 65 20 73 69 7a fferent page siz
428f0 65 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 73 6f e.** from the so
42900 75 72 63 65 20 64 61 74 61 62 61 73 65 2e 0a 2a urce database..*
42910 2a 0a 2a 2a 20 49 66 20 73 71 6c 69 74 65 33 5f *.** If sqlite3_
42920 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20 63 61 backup_step() ca
42930 6e 6e 6f 74 20 6f 62 74 61 69 6e 20 61 20 72 65 nnot obtain a re
42940 71 75 69 72 65 64 20 66 69 6c 65 2d 73 79 73 74 quired file-syst
42950 65 6d 20 6c 6f 63 6b 2c 20 74 68 65 6e 0a 2a 2a em lock, then.**
42960 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 75 the [sqlite3_bu
42970 73 79 5f 68 61 6e 64 6c 65 72 20 7c 20 62 75 73 sy_handler | bus
42980 79 2d 68 61 6e 64 6c 65 72 20 66 75 6e 63 74 69 y-handler functi
42990 6f 6e 5d 0a 2a 2a 20 69 73 20 69 6e 76 6f 6b 65 on].** is invoke
429a0 64 20 28 69 66 20 6f 6e 65 20 69 73 20 73 70 65 d (if one is spe
429b0 63 69 66 69 65 64 29 2e 20 49 66 20 74 68 65 20 cified). If the
429c0 0a 2a 2a 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 .** busy-handler
429d0 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 returns non-zer
429e0 6f 20 62 65 66 6f 72 65 20 74 68 65 20 6c 6f 63 o before the loc
429f0 6b 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2c 20 k is available,
42a00 74 68 65 6e 20 0a 2a 2a 20 5b 53 51 4c 49 54 45 then .** [SQLITE
42a10 5f 42 55 53 59 5d 20 69 73 20 72 65 74 75 72 6e _BUSY] is return
42a20 65 64 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 ed to the caller
42a30 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 . In this case t
42a40 68 65 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 73 71 he call to.** sq
42a50 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 lite3_backup_ste
42a60 70 28 29 20 63 61 6e 20 62 65 20 72 65 74 72 69 p() can be retri
42a70 65 64 20 6c 61 74 65 72 2e 20 49 66 20 74 68 65 ed later. If the
42a80 20 73 6f 75 72 63 65 0a 2a 2a 20 5b 64 61 74 61 source.** [data
42a90 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d base connection]
42aa0 0a 2a 2a 20 69 73 20 62 65 69 6e 67 20 75 73 65 .** is being use
42ab0 64 20 74 6f 20 77 72 69 74 65 20 74 6f 20 74 68 d to write to th
42ac0 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 e source databas
42ad0 65 20 77 68 65 6e 20 73 71 6c 69 74 65 33 5f 62 e when sqlite3_b
42ae0 61 63 6b 75 70 5f 73 74 65 70 28 29 0a 2a 2a 20 ackup_step().**
42af0 69 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 6e 20 is called, then
42b00 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5d 20 [SQLITE_LOCKED]
42b10 69 73 20 72 65 74 75 72 6e 65 64 20 69 6d 6d 65 is returned imme
42b20 64 69 61 74 65 6c 79 2e 20 41 67 61 69 6e 2c 20 diately. Again,
42b30 69 6e 20 74 68 69 73 0a 2a 2a 20 63 61 73 65 20 in this.** case
42b40 74 68 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 the call to sqli
42b50 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 te3_backup_step(
42b60 29 20 63 61 6e 20 62 65 20 72 65 74 72 69 65 64 ) can be retried
42b70 20 6c 61 74 65 72 20 6f 6e 2e 20 49 66 0a 2a 2a later on. If.**
42b80 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 41 [SQLITE_IOERR_A
42b90 43 43 45 53 53 20 7c 20 53 51 4c 49 54 45 5f 49 CCESS | SQLITE_I
42ba0 4f 45 52 52 5f 58 58 58 5d 2c 20 5b 53 51 4c 49 OERR_XXX], [SQLI
42bb0 54 45 5f 4e 4f 4d 45 4d 5d 2c 20 6f 72 0a 2a 2a TE_NOMEM], or.**
42bc0 20 5b 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c [SQLITE_READONL
42bd0 59 5d 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20 Y] is returned,
42be0 74 68 65 6e 20 0a 2a 2a 20 74 68 65 72 65 20 69 then .** there i
42bf0 73 20 6e 6f 20 70 6f 69 6e 74 20 69 6e 20 72 65 s no point in re
42c00 74 72 79 69 6e 67 20 74 68 65 20 63 61 6c 6c 20 trying the call
42c10 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 to sqlite3_backu
42c20 70 5f 73 74 65 70 28 29 2e 20 54 68 65 73 65 20 p_step(). These
42c30 0a 2a 2a 20 65 72 72 6f 72 73 20 61 72 65 20 63 .** errors are c
42c40 6f 6e 73 69 64 65 72 65 64 20 66 61 74 61 6c 2e onsidered fatal.
42c50 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74 20 74 At this point t
42c60 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d he application m
42c70 75 73 74 20 61 63 63 65 70 74 20 0a 2a 2a 20 74 ust accept .** t
42c80 68 61 74 20 74 68 65 20 62 61 63 6b 75 70 20 6f hat the backup o
42c90 70 65 72 61 74 69 6f 6e 20 68 61 73 20 66 61 69 peration has fai
42ca0 6c 65 64 20 61 6e 64 20 70 61 73 73 20 74 68 65 led and pass the
42cb0 20 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f backup operatio
42cc0 6e 20 68 61 6e 64 6c 65 20 0a 2a 2a 20 74 6f 20 n handle .** to
42cd0 74 68 65 20 73 71 6c 69 74 65 33 5f 62 61 63 6b the sqlite3_back
42ce0 75 70 5f 66 69 6e 69 73 68 28 29 20 74 6f 20 72 up_finish() to r
42cf0 65 6c 65 61 73 65 20 61 73 73 6f 63 69 61 74 65 elease associate
42d00 64 20 72 65 73 6f 75 72 63 65 73 2e 0a 2a 2a 0a d resources..**.
42d10 2a 2a 20 46 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 ** Following the
42d20 20 66 69 72 73 74 20 63 61 6c 6c 20 74 6f 20 73 first call to s
42d30 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 qlite3_backup_st
42d40 65 70 28 29 2c 20 61 6e 20 65 78 63 6c 75 73 69 ep(), an exclusi
42d50 76 65 20 6c 6f 63 6b 20 69 73 0a 2a 2a 20 6f 62 ve lock is.** ob
42d60 74 61 69 6e 65 64 20 6f 6e 20 74 68 65 20 64 65 tained on the de
42d70 73 74 69 6e 61 74 69 6f 6e 20 66 69 6c 65 2e 20 stination file.
42d80 49 74 20 69 73 20 6e 6f 74 20 72 65 6c 65 61 73 It is not releas
42d90 65 64 20 75 6e 74 69 6c 20 65 69 74 68 65 72 20 ed until either
42da0 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b .** sqlite3_back
42db0 75 70 5f 66 69 6e 69 73 68 28 29 20 69 73 20 63 up_finish() is c
42dc0 61 6c 6c 65 64 20 6f 72 20 74 68 65 20 62 61 63 alled or the bac
42dd0 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 kup operation is
42de0 20 63 6f 6d 70 6c 65 74 65 20 0a 2a 2a 20 61 6e complete .** an
42df0 64 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 d sqlite3_backup
42e00 5f 73 74 65 70 28 29 20 72 65 74 75 72 6e 73 20 _step() returns
42e10 5b 53 51 4c 49 54 45 5f 44 4f 4e 45 5d 2e 20 41 [SQLITE_DONE]. A
42e20 64 64 69 74 69 6f 6e 61 6c 6c 79 2c 20 65 61 63 dditionally, eac
42e30 68 20 74 69 6d 65 20 0a 2a 2a 20 61 20 63 61 6c h time .** a cal
42e40 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 l to sqlite3_bac
42e50 6b 75 70 5f 73 74 65 70 28 29 20 69 73 20 6d 61 kup_step() is ma
42e60 64 65 20 61 20 5b 73 68 61 72 65 64 20 6c 6f 63 de a [shared loc
42e70 6b 5d 20 69 73 20 6f 62 74 61 69 6e 65 64 20 6f k] is obtained o
42e80 6e 0a 2a 2a 20 74 68 65 20 73 6f 75 72 63 65 20 n.** the source
42e90 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 54 database file. T
42ea0 68 69 73 20 6c 6f 63 6b 20 69 73 20 72 65 6c 65 his lock is rele
42eb0 61 73 65 64 20 62 65 66 6f 72 65 20 74 68 65 0a ased before the.
42ec0 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 ** sqlite3_backu
42ed0 70 5f 73 74 65 70 28 29 20 63 61 6c 6c 20 72 65 p_step() call re
42ee0 74 75 72 6e 73 2e 20 42 65 63 61 75 73 65 20 74 turns. Because t
42ef0 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 he source databa
42f00 73 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 6c 6f 63 se is not.** loc
42f10 6b 65 64 20 62 65 74 77 65 65 6e 20 63 61 6c 6c ked between call
42f20 73 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 s to sqlite3_bac
42f30 6b 75 70 5f 73 74 65 70 28 29 2c 20 69 74 20 6d kup_step(), it m
42f40 61 79 20 62 65 20 6d 6f 64 69 66 69 65 64 20 6d ay be modified m
42f50 69 64 2d 77 61 79 0a 2a 2a 20 74 68 72 6f 75 67 id-way.** throug
42f60 68 20 74 68 65 20 62 61 63 6b 75 70 20 70 72 6f h the backup pro
42f70 63 65 64 75 72 65 2e 20 49 66 20 74 68 65 20 73 cedure. If the s
42f80 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 20 69 ource database i
42f90 73 20 6d 6f 64 69 66 69 65 64 20 62 79 20 61 6e s modified by an
42fa0 0a 2a 2a 20 65 78 74 65 72 6e 61 6c 20 70 72 6f .** external pro
42fb0 63 65 73 73 20 6f 72 20 76 69 61 20 61 20 64 61 cess or via a da
42fc0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
42fd0 6e 20 6f 74 68 65 72 20 74 68 61 6e 20 74 68 65 n other than the
42fe0 20 6f 6e 65 20 62 65 69 6e 67 0a 2a 2a 20 75 73 one being.** us
42ff0 65 64 20 62 79 20 74 68 65 20 62 61 63 6b 75 70 ed by the backup
43000 20 6f 70 65 72 61 74 69 6f 6e 2c 20 74 68 65 6e operation, then
43010 20 74 68 65 20 62 61 63 6b 75 70 20 77 69 6c 6c the backup will
43020 20 62 65 20 74 72 61 6e 73 70 61 72 65 6e 74 6c be transparentl
43030 79 0a 2a 2a 20 72 65 73 74 61 72 74 65 64 20 62 y.** restarted b
43040 79 20 74 68 65 20 6e 65 78 74 20 63 61 6c 6c 20 y the next call
43050 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 to sqlite3_backu
43060 70 5f 73 74 65 70 28 29 2e 20 49 66 20 74 68 65 p_step(). If the
43070 20 73 6f 75 72 63 65 20 0a 2a 2a 20 64 61 74 61 source .** data
43080 62 61 73 65 20 69 73 20 6d 6f 64 69 66 69 65 64 base is modified
43090 20 62 79 20 74 68 65 20 75 73 69 6e 67 20 74 68 by the using th
430a0 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 e same database
430b0 63 6f 6e 6e 65 63 74 69 6f 6e 20 61 73 20 69 73 connection as is
430c0 20 75 73 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 used.** by the
430d0 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e backup operation
430e0 2c 20 74 68 65 6e 20 74 68 65 20 62 61 63 6b 75 , then the backu
430f0 70 20 64 61 74 61 62 61 73 65 20 69 73 20 74 72 p database is tr
43100 61 6e 73 70 61 72 65 6e 74 6c 79 20 0a 2a 2a 20 ansparently .**
43110 75 70 64 61 74 65 64 20 61 74 20 74 68 65 20 73 updated at the s
43120 61 6d 65 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 ame time..**.**
43130 3c 62 3e 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 <b>sqlite3_backu
43140 70 5f 66 69 6e 69 73 68 28 29 3c 2f 62 3e 0a 2a p_finish()</b>.*
43150 2a 0a 2a 2a 20 4f 6e 63 65 20 73 71 6c 69 74 65 *.** Once sqlite
43160 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20 3_backup_step()
43170 68 61 73 20 72 65 74 75 72 6e 65 64 20 5b 53 51 has returned [SQ
43180 4c 49 54 45 5f 44 4f 4e 45 5d 2c 20 6f 72 20 77 LITE_DONE], or w
43190 68 65 6e 20 74 68 65 20 0a 2a 2a 20 61 70 70 6c hen the .** appl
431a0 69 63 61 74 69 6f 6e 20 77 69 73 68 65 73 20 74 ication wishes t
431b0 6f 20 61 62 61 6e 64 6f 6e 20 74 68 65 20 62 61 o abandon the ba
431c0 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 2c 20 ckup operation,
431d0 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 61 63 the [sqlite3_bac
431e0 6b 75 70 5d 0a 2a 2a 20 6f 62 6a 65 63 74 20 73 kup].** object s
431f0 68 6f 75 6c 64 20 62 65 20 70 61 73 73 65 64 20 hould be passed
43200 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 to sqlite3_backu
43210 70 5f 66 69 6e 69 73 68 28 29 2e 20 54 68 69 73 p_finish(). This
43220 20 72 65 6c 65 61 73 65 73 20 61 6c 6c 0a 2a 2a releases all.**
43230 20 72 65 73 6f 75 72 63 65 73 20 61 73 73 6f 63 resources assoc
43240 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 62 iated with the b
43250 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 2e ackup operation.
43260 20 49 66 20 73 71 6c 69 74 65 33 5f 62 61 63 6b If sqlite3_back
43270 75 70 5f 73 74 65 70 28 29 0a 2a 2a 20 68 61 73 up_step().** has
43280 20 6e 6f 74 20 79 65 74 20 72 65 74 75 72 6e 65 not yet returne
43290 64 20 5b 53 51 4c 49 54 45 5f 44 4f 4e 45 5d 2c d [SQLITE_DONE],
432a0 20 74 68 65 6e 20 61 6e 79 20 61 63 74 69 76 65 then any active
432b0 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 write-transacti
432c0 6f 6e 20 6f 6e 20 74 68 65 0a 2a 2a 20 64 65 73 on on the.** des
432d0 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 tination databas
432e0 65 20 69 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b e is rolled back
432f0 2e 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 . The [sqlite3_b
43300 61 63 6b 75 70 5d 20 6f 62 6a 65 63 74 20 69 73 ackup] object is
43310 20 69 6e 76 61 6c 69 64 0a 2a 2a 20 61 6e 64 20 invalid.** and
43320 6d 61 79 20 6e 6f 74 20 62 65 20 75 73 65 64 20 may not be used
43330 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 63 61 6c 6c following a call
43340 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b to sqlite3_back
43350 75 70 5f 66 69 6e 69 73 68 28 29 2e 0a 2a 2a 0a up_finish()..**.
43360 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 72 65 74 ** The value ret
43370 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 urned by sqlite3
43380 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 20 69 _backup_finish i
43390 73 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 69 66 s [SQLITE_OK] if
433a0 20 6e 6f 20 65 72 72 6f 72 0a 2a 2a 20 6f 63 63 no error.** occ
433b0 75 72 72 65 64 2c 20 72 65 67 61 72 64 6c 65 73 urred, regardles
433c0 73 20 6f 72 20 77 68 65 74 68 65 72 20 6f 72 20 s or whether or
433d0 6e 6f 74 20 73 71 6c 69 74 65 33 5f 62 61 63 6b not sqlite3_back
433e0 75 70 5f 73 74 65 70 28 29 20 77 61 73 20 63 61 up_step() was ca
433f0 6c 6c 65 64 0a 2a 2a 20 61 20 73 75 66 66 69 63 lled.** a suffic
43400 69 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20 74 ient number of t
43410 69 6d 65 73 20 74 6f 20 63 6f 6d 70 6c 65 74 65 imes to complete
43420 20 74 68 65 20 62 61 63 6b 75 70 20 6f 70 65 72 the backup oper
43430 61 74 69 6f 6e 2e 20 4f 72 2c 20 69 66 0a 2a 2a ation. Or, if.**
43440 20 61 6e 20 6f 75 74 2d 6f 66 2d 6d 65 6d 6f 72 an out-of-memor
43450 79 20 63 6f 6e 64 69 74 69 6f 6e 20 6f 72 20 49 y condition or I
43460 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 65 64 20 O error occured
43470 64 75 72 69 6e 67 20 61 20 63 61 6c 6c 20 74 6f during a call to
43480 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b .** sqlite3_back
43490 75 70 5f 73 74 65 70 28 29 20 74 68 65 6e 20 5b up_step() then [
434a0 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5d 20 6f 72 SQLITE_NOMEM] or
434b0 20 61 6e 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 49 an.** [SQLITE_I
434c0 4f 45 52 52 5f 41 43 43 45 53 53 20 7c 20 53 51 OERR_ACCESS | SQ
434d0 4c 49 54 45 5f 49 4f 45 52 52 5f 58 58 58 5d 20 LITE_IOERR_XXX]
434e0 65 72 72 6f 72 20 63 6f 64 65 0a 2a 2a 20 69 73 error code.** is
434f0 20 72 65 74 75 72 6e 65 64 2e 20 49 6e 20 74 68 returned. In th
43500 69 73 20 63 61 73 65 20 74 68 65 20 65 72 72 6f is case the erro
43510 72 20 63 6f 64 65 20 61 6e 64 20 61 6e 20 65 72 r code and an er
43520 72 6f 72 20 6d 65 73 73 61 67 65 20 61 72 65 0a ror message are.
43530 2a 2a 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 ** written to th
43540 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 5b 64 e destination [d
43550 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
43560 6f 6e 5d 2e 0a 2a 2a 0a 2a 2a 20 41 20 72 65 74 on]..**.** A ret
43570 75 72 6e 20 6f 66 20 5b 53 51 4c 49 54 45 5f 42 urn of [SQLITE_B
43580 55 53 59 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f USY] or [SQLITE_
43590 4c 4f 43 4b 45 44 5d 20 66 72 6f 6d 20 73 71 6c LOCKED] from sql
435a0 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 ite3_backup_step
435b0 28 29 20 69 73 0a 2a 2a 20 6e 6f 74 20 61 20 70 () is.** not a p
435c0 65 72 6d 61 6e 65 6e 74 20 65 72 72 6f 72 20 61 ermanent error a
435d0 6e 64 20 64 6f 65 73 20 6e 6f 74 20 61 66 66 65 nd does not affe
435e0 63 74 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 ct the return va
435f0 6c 75 65 20 6f 66 0a 2a 2a 20 73 71 6c 69 74 65 lue of.** sqlite
43600 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 3_backup_finish(
43610 29 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 73 71 6c 69 )..**.** <b>sqli
43620 74 65 33 5f 62 61 63 6b 75 70 5f 72 65 6d 61 69 te3_backup_remai
43630 6e 69 6e 67 28 29 2c 20 73 71 6c 69 74 65 33 5f ning(), sqlite3_
43640 62 61 63 6b 75 70 5f 70 61 67 65 63 6f 75 6e 74 backup_pagecount
43650 28 29 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20 45 61 63 ()</b>.**.** Eac
43660 68 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 h call to sqlite
43670 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20 3_backup_step()
43680 73 65 74 73 20 74 77 6f 20 76 61 6c 75 65 73 20 sets two values
43690 73 74 6f 72 65 64 20 69 6e 74 65 72 6e 61 6c 6c stored internall
436a0 79 0a 2a 2a 20 62 79 20 61 6e 20 5b 73 71 6c 69 y.** by an [sqli
436b0 74 65 33 5f 62 61 63 6b 75 70 5d 20 6f 62 6a 65 te3_backup] obje
436c0 63 74 2e 20 54 68 65 20 6e 75 6d 62 65 72 20 6f ct. The number o
436d0 66 20 70 61 67 65 73 20 73 74 69 6c 6c 20 74 6f f pages still to
436e0 20 62 65 20 62 61 63 6b 65 64 0a 2a 2a 20 75 70 be backed.** up
436f0 2c 20 77 68 69 63 68 20 6d 61 79 20 62 65 20 71 , which may be q
43700 75 65 72 69 65 64 20 62 79 20 73 71 6c 69 74 65 ueried by sqlite
43710 33 5f 62 61 63 6b 75 70 5f 72 65 6d 61 69 6e 69 3_backup_remaini
43720 6e 67 28 29 2c 20 61 6e 64 20 74 68 65 20 74 6f ng(), and the to
43730 74 61 6c 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 tal.** number of
43740 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 73 6f pages in the so
43750 75 72 63 65 20 64 61 74 61 62 61 73 65 20 66 69 urce database fi
43760 6c 65 2c 20 77 68 69 63 68 20 6d 61 79 20 62 65 le, which may be
43770 20 71 75 65 72 69 65 64 20 62 79 0a 2a 2a 20 73 queried by.** s
43780 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 70 61 qlite3_backup_pa
43790 67 65 63 6f 75 6e 74 28 29 2e 0a 2a 2a 0a 2a 2a gecount()..**.**
437a0 20 54 68 65 20 76 61 6c 75 65 73 20 72 65 74 75 The values retu
437b0 72 6e 65 64 20 62 79 20 74 68 65 73 65 20 66 75 rned by these fu
437c0 6e 63 74 69 6f 6e 73 20 61 72 65 20 6f 6e 6c 79 nctions are only
437d0 20 75 70 64 61 74 65 64 20 62 79 0a 2a 2a 20 73 updated by.** s
437e0 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 qlite3_backup_st
437f0 65 70 28 29 2e 20 49 66 20 74 68 65 20 73 6f 75 ep(). If the sou
43800 72 63 65 20 64 61 74 61 62 61 73 65 20 69 73 20 rce database is
43810 6d 6f 64 69 66 69 65 64 20 64 75 72 69 6e 67 20 modified during
43820 61 20 62 61 63 6b 75 70 0a 2a 2a 20 6f 70 65 72 a backup.** oper
43830 61 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20 ation, then the
43840 76 61 6c 75 65 73 20 61 72 65 20 6e 6f 74 20 75 values are not u
43850 70 64 61 74 65 64 20 74 6f 20 61 63 63 6f 75 6e pdated to accoun
43860 74 20 66 6f 72 20 61 6e 79 20 65 78 74 72 61 0a t for any extra.
43870 2a 2a 20 70 61 67 65 73 20 74 68 61 74 20 6e 65 ** pages that ne
43880 65 64 20 74 6f 20 62 65 20 75 70 64 61 74 65 64 ed to be updated
43890 20 6f 72 20 74 68 65 20 73 69 7a 65 20 6f 66 20 or the size of
438a0 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 the source datab
438b0 61 73 65 20 66 69 6c 65 0a 2a 2a 20 63 68 61 6e ase file.** chan
438c0 67 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 43 ging..**.** <b>C
438d0 6f 6e 63 75 72 72 65 6e 74 20 55 73 61 67 65 20 oncurrent Usage
438e0 6f 66 20 44 61 74 61 62 61 73 65 20 48 61 6e 64 of Database Hand
438f0 6c 65 73 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20 54 68 les</b>.**.** Th
43900 65 20 73 6f 75 72 63 65 20 5b 64 61 74 61 62 61 e source [databa
43910 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 6d se connection] m
43920 61 79 20 62 65 20 75 73 65 64 20 62 79 20 74 68 ay be used by th
43930 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 66 6f e application fo
43940 72 20 6f 74 68 65 72 0a 2a 2a 20 70 75 72 70 6f r other.** purpo
43950 73 65 73 20 77 68 69 6c 65 20 61 20 62 61 63 6b ses while a back
43960 75 70 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 up operation is
43970 75 6e 64 65 72 77 61 79 20 6f 72 20 62 65 69 6e underway or bein
43980 67 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 0a 2a g initialized..*
43990 2a 20 49 66 20 53 51 4c 69 74 65 20 69 73 20 63 * If SQLite is c
439a0 6f 6d 70 69 6c 65 64 20 61 6e 64 20 63 6f 6e 66 ompiled and conf
439b0 69 67 75 72 65 64 20 74 6f 20 73 75 70 70 6f 72 igured to suppor
439c0 74 20 74 68 72 65 61 64 73 61 66 65 20 64 61 74 t threadsafe dat
439d0 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 abase.** connect
439e0 69 6f 6e 73 2c 20 74 68 65 6e 20 74 68 65 20 73 ions, then the s
439f0 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 20 63 ource database c
43a00 6f 6e 6e 65 63 74 69 6f 6e 20 6d 61 79 20 62 65 onnection may be
43a10 20 75 73 65 64 20 63 6f 6e 63 75 72 72 65 6e 74 used concurrent
43a20 6c 79 0a 2a 2a 20 66 72 6f 6d 20 77 69 74 68 69 ly.** from withi
43a30 6e 20 6f 74 68 65 72 20 74 68 72 65 61 64 73 2e n other threads.
43a40 0a 2a 2a 0a 2a 2a 20 48 6f 77 65 76 65 72 2c 20 .**.** However,
43a50 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 the application
43a60 6d 75 73 74 20 67 75 61 72 61 6e 74 65 65 20 74 must guarantee t
43a70 68 61 74 20 74 68 65 20 64 65 73 74 69 6e 61 74 hat the destinat
43a80 69 6f 6e 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 ion database.**
43a90 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c connection handl
43aa0 65 20 69 73 20 6e 6f 74 20 70 61 73 73 65 64 20 e is not passed
43ab0 74 6f 20 61 6e 79 20 6f 74 68 65 72 20 41 50 49 to any other API
43ac0 20 28 62 79 20 61 6e 79 20 74 68 72 65 61 64 29 (by any thread)
43ad0 20 61 66 74 65 72 20 0a 2a 2a 20 73 71 6c 69 74 after .** sqlit
43ae0 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 29 e3_backup_init()
43af0 20 69 73 20 63 61 6c 6c 65 64 20 61 6e 64 20 62 is called and b
43b00 65 66 6f 72 65 20 74 68 65 20 63 6f 72 72 65 73 efore the corres
43b10 70 6f 6e 64 69 6e 67 20 63 61 6c 6c 20 74 6f 0a ponding call to.
43b20 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 ** sqlite3_backu
43b30 70 5f 66 69 6e 69 73 68 28 29 2e 20 55 6e 66 6f p_finish(). Unfo
43b40 72 74 75 6e 61 74 65 6c 79 20 53 51 4c 69 74 65 rtunately SQLite
43b50 20 64 6f 65 73 20 6e 6f 74 20 63 75 72 72 65 6e does not curren
43b60 74 6c 79 20 63 68 65 63 6b 0a 2a 2a 20 66 6f 72 tly check.** for
43b70 20 74 68 69 73 2c 20 69 66 20 74 68 65 20 61 70 this, if the ap
43b80 70 6c 69 63 61 74 69 6f 6e 20 64 6f 65 73 20 75 plication does u
43b90 73 65 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 se the destinati
43ba0 6f 6e 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e on [database con
43bb0 6e 65 63 74 69 6f 6e 5d 0a 2a 2a 20 66 6f 72 20 nection].** for
43bc0 73 6f 6d 65 20 6f 74 68 65 72 20 70 75 72 70 6f some other purpo
43bd0 73 65 20 64 75 72 69 6e 67 20 61 20 62 61 63 6b se during a back
43be0 75 70 20 6f 70 65 72 61 74 69 6f 6e 2c 20 74 68 up operation, th
43bf0 69 6e 67 73 20 6d 61 79 20 61 70 70 65 61 72 20 ings may appear
43c00 74 6f 0a 2a 2a 20 77 6f 72 6b 20 63 6f 72 72 65 to.** work corre
43c10 63 74 6c 79 20 62 75 74 20 69 6e 20 66 61 63 74 ctly but in fact
43c20 20 62 65 20 73 75 62 74 6c 79 20 6d 61 6c 66 75 be subtly malfu
43c30 6e 63 74 69 6f 6e 69 6e 67 2e 20 20 55 73 65 20 nctioning. Use
43c40 6f 66 20 74 68 65 0a 2a 2a 20 64 65 73 74 69 6e of the.** destin
43c50 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 20 63 ation database c
43c60 6f 6e 6e 65 63 74 69 6f 6e 20 77 68 69 6c 65 20 onnection while
43c70 61 20 62 61 63 6b 75 70 20 69 73 20 69 6e 20 70 a backup is in p
43c80 72 6f 67 72 65 73 73 20 6d 69 67 68 74 0a 2a 2a rogress might.**
43c90 20 61 6c 73 6f 20 63 61 75 73 65 20 61 20 6d 75 also cause a mu
43ca0 74 65 78 20 64 65 61 64 6c 6f 63 6b 2e 0a 2a 2a tex deadlock..**
43cb0 0a 2a 2a 20 46 75 72 74 68 65 72 6d 6f 72 65 2c .** Furthermore,
43cc0 20 69 66 20 72 75 6e 6e 69 6e 67 20 69 6e 20 5b if running in [
43cd0 73 68 61 72 65 64 20 63 61 63 68 65 20 6d 6f 64 shared cache mod
43ce0 65 5d 2c 20 74 68 65 20 61 70 70 6c 69 63 61 74 e], the applicat
43cf0 69 6f 6e 20 6d 75 73 74 0a 2a 2a 20 67 75 61 72 ion must.** guar
43d00 61 6e 74 65 65 20 74 68 61 74 20 74 68 65 20 73 antee that the s
43d10 68 61 72 65 64 20 63 61 63 68 65 20 75 73 65 64 hared cache used
43d20 20 62 79 20 74 68 65 20 64 65 73 74 69 6e 61 74 by the destinat
43d30 69 6f 6e 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 ion database.**
43d40 69 73 20 6e 6f 74 20 61 63 63 65 73 73 65 64 20 is not accessed
43d50 77 68 69 6c 65 20 74 68 65 20 62 61 63 6b 75 70 while the backup
43d60 20 69 73 20 72 75 6e 6e 69 6e 67 2e 20 49 6e 20 is running. In
43d70 70 72 61 63 74 69 63 65 20 74 68 69 73 20 6d 65 practice this me
43d80 61 6e 73 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 ans.** that the
43d90 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d 75 73 74 application must
43da0 20 67 75 61 72 61 6e 74 65 65 20 74 68 61 74 20 guarantee that
43db0 74 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 the file-system
43dc0 66 69 6c 65 20 62 65 69 6e 67 20 0a 2a 2a 20 62 file being .** b
43dd0 61 63 6b 65 64 20 75 70 20 74 6f 20 69 73 20 6e acked up to is n
43de0 6f 74 20 61 63 63 65 73 73 65 64 20 62 79 20 61 ot accessed by a
43df0 6e 79 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 69 ny connection wi
43e00 74 68 69 6e 20 74 68 65 20 70 72 6f 63 65 73 73 thin the process
43e10 2c 0a 2a 2a 20 6e 6f 74 20 6a 75 73 74 20 74 68 ,.** not just th
43e20 65 20 73 70 65 63 69 66 69 63 20 63 6f 6e 6e 65 e specific conne
43e30 63 74 69 6f 6e 20 74 68 61 74 20 77 61 73 20 70 ction that was p
43e40 61 73 73 65 64 20 74 6f 20 73 71 6c 69 74 65 33 assed to sqlite3
43e50 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 29 2e 0a _backup_init()..
43e60 2a 2a 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74 **.** The [sqlit
43e70 65 33 5f 62 61 63 6b 75 70 5d 20 6f 62 6a 65 63 e3_backup] objec
43e80 74 20 69 74 73 65 6c 66 20 69 73 20 70 61 72 74 t itself is part
43e90 69 61 6c 6c 79 20 74 68 72 65 61 64 73 61 66 65 ially threadsafe
43ea0 2e 20 4d 75 6c 74 69 70 6c 65 20 0a 2a 2a 20 74 . Multiple .** t
43eb0 68 72 65 61 64 73 20 6d 61 79 20 73 61 66 65 6c hreads may safel
43ec0 79 20 6d 61 6b 65 20 6d 75 6c 74 69 70 6c 65 20 y make multiple
43ed0 63 6f 6e 63 75 72 72 65 6e 74 20 63 61 6c 6c 73 concurrent calls
43ee0 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b to sqlite3_back
43ef0 75 70 5f 73 74 65 70 28 29 2e 0a 2a 2a 20 48 6f up_step()..** Ho
43f00 77 65 76 65 72 2c 20 74 68 65 20 73 71 6c 69 74 wever, the sqlit
43f10 65 33 5f 62 61 63 6b 75 70 5f 72 65 6d 61 69 6e e3_backup_remain
43f20 69 6e 67 28 29 20 61 6e 64 20 73 71 6c 69 74 65 ing() and sqlite
43f30 33 5f 62 61 63 6b 75 70 5f 70 61 67 65 63 6f 75 3_backup_pagecou
43f40 6e 74 28 29 0a 2a 2a 20 41 50 49 73 20 61 72 65 nt().** APIs are
43f50 20 6e 6f 74 20 73 74 72 69 63 74 6c 79 20 73 70 not strictly sp
43f60 65 61 6b 69 6e 67 20 74 68 72 65 61 64 73 61 66 eaking threadsaf
43f70 65 2e 20 49 66 20 74 68 65 79 20 61 72 65 20 69 e. If they are i
43f80 6e 76 6f 6b 65 64 20 61 74 20 74 68 65 0a 2a 2a nvoked at the.**
43f90 20 73 61 6d 65 20 74 69 6d 65 20 61 73 20 61 6e same time as an
43fa0 6f 74 68 65 72 20 74 68 72 65 61 64 20 69 73 20 other thread is
43fb0 69 6e 76 6f 6b 69 6e 67 20 73 71 6c 69 74 65 33 invoking sqlite3
43fc0 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20 69 _backup_step() i
43fd0 74 20 69 73 0a 2a 2a 20 70 6f 73 73 69 62 6c 65 t is.** possible
43fe0 20 74 68 61 74 20 74 68 65 79 20 72 65 74 75 72 that they retur
43ff0 6e 20 69 6e 76 61 6c 69 64 20 76 61 6c 75 65 73 n invalid values
44000 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
44010 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a sqlite3_backup *
44020 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 69 sqlite3_backup_i
44030 6e 69 74 28 0a 20 20 73 71 6c 69 74 65 33 20 2a nit(. sqlite3 *
44040 70 44 65 73 74 2c 20 20 20 20 20 20 20 20 20 20 pDest,
44050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
44060 20 44 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 Destination dat
44070 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a abase handle */.
44080 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 const char *zD
44090 65 73 74 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 estName,
440a0 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 73 74 /* Dest
440b0 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 ination database
440c0 20 6e 61 6d 65 20 2a 2f 0a 20 20 73 71 6c 69 74 name */. sqlit
440d0 65 33 20 2a 70 53 6f 75 72 63 65 2c 20 20 20 20 e3 *pSource,
440e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
440f0 20 20 2f 2a 20 53 6f 75 72 63 65 20 64 61 74 61 /* Source data
44100 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 base handle */.
44110 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 6f const char *zSo
44120 75 72 63 65 4e 61 6d 65 20 20 20 20 20 20 20 20 urceName
44130 20 20 20 20 20 20 20 20 2f 2a 20 53 6f 75 72 63 /* Sourc
44140 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 e database name
44150 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 */.);.SQLITE_API
44160 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 61 63 int sqlite3_bac
44170 6b 75 70 5f 73 74 65 70 28 73 71 6c 69 74 65 33 kup_step(sqlite3
44180 5f 62 61 63 6b 75 70 20 2a 70 2c 20 69 6e 74 20 _backup *p, int
44190 6e 50 61 67 65 29 3b 0a 53 51 4c 49 54 45 5f 41 nPage);.SQLITE_A
441a0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 PI int sqlite3_b
441b0 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 73 71 6c ackup_finish(sql
441c0 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 29 3b ite3_backup *p);
441d0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
441e0 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 72 sqlite3_backup_r
441f0 65 6d 61 69 6e 69 6e 67 28 73 71 6c 69 74 65 33 emaining(sqlite3
44200 5f 62 61 63 6b 75 70 20 2a 70 29 3b 0a 53 51 4c _backup *p);.SQL
44210 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
44220 74 65 33 5f 62 61 63 6b 75 70 5f 70 61 67 65 63 te3_backup_pagec
44230 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f 62 61 63 ount(sqlite3_bac
44240 6b 75 70 20 2a 70 29 3b 0a 0a 2f 2a 0a 2a 2a 20 kup *p);../*.**
44250 43 41 50 49 33 52 45 46 3a 20 55 6e 6c 6f 63 6b CAPI3REF: Unlock
44260 20 4e 6f 74 69 66 69 63 61 74 69 6f 6e 0a 2a 2a Notification.**
44270 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a EXPERIMENTAL.**
44280 0a 2a 2a 20 57 68 65 6e 20 72 75 6e 6e 69 6e 67 .** When running
44290 20 69 6e 20 73 68 61 72 65 64 2d 63 61 63 68 65 in shared-cache
442a0 20 6d 6f 64 65 2c 20 61 20 64 61 74 61 62 61 73 mode, a databas
442b0 65 20 6f 70 65 72 61 74 69 6f 6e 20 6d 61 79 20 e operation may
442c0 66 61 69 6c 20 77 69 74 68 0a 2a 2a 20 61 6e 20 fail with.** an
442d0 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5d 20 [SQLITE_LOCKED]
442e0 65 72 72 6f 72 20 69 66 20 74 68 65 20 72 65 71 error if the req
442f0 75 69 72 65 64 20 6c 6f 63 6b 73 20 6f 6e 20 74 uired locks on t
44300 68 65 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 he shared-cache
44310 6f 72 0a 2a 2a 20 69 6e 64 69 76 69 64 75 61 6c or.** individual
44320 20 74 61 62 6c 65 73 20 77 69 74 68 69 6e 20 74 tables within t
44330 68 65 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 he shared-cache
44340 63 61 6e 6e 6f 74 20 62 65 20 6f 62 74 61 69 6e cannot be obtain
44350 65 64 2e 20 53 65 65 0a 2a 2a 20 5b 53 51 4c 69 ed. See.** [SQLi
44360 74 65 20 53 68 61 72 65 64 2d 43 61 63 68 65 20 te Shared-Cache
44370 4d 6f 64 65 5d 20 66 6f 72 20 61 20 64 65 73 63 Mode] for a desc
44380 72 69 70 74 69 6f 6e 20 6f 66 20 73 68 61 72 65 ription of share
44390 64 2d 63 61 63 68 65 20 6c 6f 63 6b 69 6e 67 2e d-cache locking.
443a0 20 0a 2a 2a 20 54 68 69 73 20 41 50 49 20 6d 61 .** This API ma
443b0 79 20 62 65 20 75 73 65 64 20 74 6f 20 72 65 67 y be used to reg
443c0 69 73 74 65 72 20 61 20 63 61 6c 6c 62 61 63 6b ister a callback
443d0 20 74 68 61 74 20 53 51 4c 69 74 65 20 77 69 6c that SQLite wil
443e0 6c 20 69 6e 76 6f 6b 65 20 0a 2a 2a 20 77 68 65 l invoke .** whe
443f0 6e 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e n the connection
44400 20 63 75 72 72 65 6e 74 6c 79 20 68 6f 6c 64 69 currently holdi
44410 6e 67 20 74 68 65 20 72 65 71 75 69 72 65 64 20 ng the required
44420 6c 6f 63 6b 20 72 65 6c 69 6e 71 75 69 73 68 65 lock relinquishe
44430 73 20 69 74 2e 0a 2a 2a 20 54 68 69 73 20 41 50 s it..** This AP
44440 49 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 I is only availa
44450 62 6c 65 20 69 66 20 74 68 65 20 6c 69 62 72 61 ble if the libra
44460 72 79 20 77 61 73 20 63 6f 6d 70 69 6c 65 64 20 ry was compiled
44470 77 69 74 68 20 74 68 65 0a 2a 2a 20 5b 53 51 4c with the.** [SQL
44480 49 54 45 5f 45 4e 41 42 4c 45 5f 55 4e 4c 4f 43 ITE_ENABLE_UNLOC
44490 4b 5f 4e 4f 54 49 46 59 5d 20 43 2d 70 72 65 70 K_NOTIFY] C-prep
444a0 72 6f 63 65 73 73 6f 72 20 73 79 6d 62 6f 6c 20 rocessor symbol
444b0 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 defined..**.** S
444c0 65 65 20 41 6c 73 6f 3a 20 5b 55 73 69 6e 67 20 ee Also: [Using
444d0 74 68 65 20 53 51 4c 69 74 65 20 55 6e 6c 6f 63 the SQLite Unloc
444e0 6b 20 4e 6f 74 69 66 69 63 61 74 69 6f 6e 20 46 k Notification F
444f0 65 61 74 75 72 65 5d 2e 0a 2a 2a 0a 2a 2a 20 53 eature]..**.** S
44500 68 61 72 65 64 2d 63 61 63 68 65 20 6c 6f 63 6b hared-cache lock
44510 73 20 61 72 65 20 72 65 6c 65 61 73 65 64 20 77 s are released w
44520 68 65 6e 20 61 20 64 61 74 61 62 61 73 65 20 63 hen a database c
44530 6f 6e 6e 65 63 74 69 6f 6e 20 63 6f 6e 63 6c 75 onnection conclu
44540 64 65 73 0a 2a 2a 20 69 74 73 20 63 75 72 72 65 des.** its curre
44550 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 nt transaction,
44560 65 69 74 68 65 72 20 62 79 20 63 6f 6d 6d 69 74 either by commit
44570 74 69 6e 67 20 69 74 20 6f 72 20 72 6f 6c 6c 69 ting it or rolli
44580 6e 67 20 69 74 20 62 61 63 6b 2e 20 0a 2a 2a 0a ng it back. .**.
44590 2a 2a 20 57 68 65 6e 20 61 20 63 6f 6e 6e 65 63 ** When a connec
445a0 74 69 6f 6e 20 28 6b 6e 6f 77 6e 20 61 73 20 74 tion (known as t
445b0 68 65 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 he blocked conne
445c0 63 74 69 6f 6e 29 20 66 61 69 6c 73 20 74 6f 20 ction) fails to
445d0 6f 62 74 61 69 6e 20 61 0a 2a 2a 20 73 68 61 72 obtain a.** shar
445e0 65 64 2d 63 61 63 68 65 20 6c 6f 63 6b 20 61 6e ed-cache lock an
445f0 64 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 d SQLITE_LOCKED
44600 69 73 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 is returned to t
44610 68 65 20 63 61 6c 6c 65 72 2c 20 74 68 65 0a 2a he caller, the.*
44620 2a 20 69 64 65 6e 74 69 74 79 20 6f 66 20 74 68 * identity of th
44630 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 e database conne
44640 63 74 69 6f 6e 20 28 74 68 65 20 62 6c 6f 63 6b ction (the block
44650 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 29 20 ing connection)
44660 74 68 61 74 0a 2a 2a 20 68 61 73 20 6c 6f 63 6b that.** has lock
44670 65 64 20 74 68 65 20 72 65 71 75 69 72 65 64 20 ed the required
44680 72 65 73 6f 75 72 63 65 20 69 73 20 73 74 6f 72 resource is stor
44690 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 2e 20 41 ed internally. A
446a0 66 74 65 72 20 61 6e 20 0a 2a 2a 20 61 70 70 6c fter an .** appl
446b0 69 63 61 74 69 6f 6e 20 72 65 63 65 69 76 65 73 ication receives
446c0 20 61 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 an SQLITE_LOCKE
446d0 44 20 65 72 72 6f 72 2c 20 69 74 20 6d 61 79 20 D error, it may
446e0 63 61 6c 6c 20 74 68 65 0a 2a 2a 20 73 71 6c 69 call the.** sqli
446f0 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 te3_unlock_notif
44700 79 28 29 20 6d 65 74 68 6f 64 20 77 69 74 68 20 y() method with
44710 74 68 65 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e the blocked conn
44720 65 63 74 69 6f 6e 20 68 61 6e 64 6c 65 20 61 73 ection handle as
44730 20 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 61 .** the first a
44740 72 67 75 6d 65 6e 74 20 74 6f 20 72 65 67 69 73 rgument to regis
44750 74 65 72 20 66 6f 72 20 61 20 63 61 6c 6c 62 61 ter for a callba
44760 63 6b 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 ck that will be
44770 69 6e 76 6f 6b 65 64 0a 2a 2a 20 77 68 65 6e 20 invoked.** when
44780 74 68 65 20 62 6c 6f 63 6b 69 6e 67 20 63 6f 6e the blocking con
44790 6e 65 63 74 69 6f 6e 73 20 63 75 72 72 65 6e 74 nections current
447a0 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 transaction is
447b0 63 6f 6e 63 6c 75 64 65 64 2e 20 54 68 65 0a 2a concluded. The.*
447c0 2a 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e * callback is in
447d0 76 6f 6b 65 64 20 66 72 6f 6d 20 77 69 74 68 69 voked from withi
447e0 6e 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 n the [sqlite3_s
447f0 74 65 70 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 tep] or [sqlite3
44800 5f 63 6c 6f 73 65 5d 0a 2a 2a 20 63 61 6c 6c 20 _close].** call
44810 74 68 61 74 20 63 6f 6e 63 6c 75 64 65 73 20 74 that concludes t
44820 68 65 20 62 6c 6f 63 6b 69 6e 67 20 63 6f 6e 6e he blocking conn
44830 65 63 74 69 6f 6e 73 20 74 72 61 6e 73 61 63 74 ections transact
44840 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 71 ion..**.** If sq
44850 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 lite3_unlock_not
44860 69 66 79 28 29 20 69 73 20 63 61 6c 6c 65 64 20 ify() is called
44870 69 6e 20 61 20 6d 75 6c 74 69 2d 74 68 72 65 61 in a multi-threa
44880 64 65 64 20 61 70 70 6c 69 63 61 74 69 6f 6e 2c ded application,
44890 0a 2a 2a 20 74 68 65 72 65 20 69 73 20 61 20 63 .** there is a c
448a0 68 61 6e 63 65 20 74 68 61 74 20 74 68 65 20 62 hance that the b
448b0 6c 6f 63 6b 69 6e 67 20 63 6f 6e 6e 65 63 74 69 locking connecti
448c0 6f 6e 20 77 69 6c 6c 20 68 61 76 65 20 61 6c 72 on will have alr
448d0 65 61 64 79 0a 2a 2a 20 63 6f 6e 63 6c 75 64 65 eady.** conclude
448e0 64 20 69 74 73 20 74 72 61 6e 73 61 63 74 69 6f d its transactio
448f0 6e 20 62 79 20 74 68 65 20 74 69 6d 65 20 73 71 n by the time sq
44900 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 lite3_unlock_not
44910 69 66 79 28 29 20 69 73 20 69 6e 76 6f 6b 65 64 ify() is invoked
44920 2e 0a 2a 2a 20 49 66 20 74 68 69 73 20 68 61 70 ..** If this hap
44930 70 65 6e 73 2c 20 74 68 65 6e 20 74 68 65 20 73 pens, then the s
44940 70 65 63 69 66 69 65 64 20 63 61 6c 6c 62 61 63 pecified callbac
44950 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20 69 6d 6d k is invoked imm
44960 65 64 69 61 74 65 6c 79 2c 0a 2a 2a 20 66 72 6f ediately,.** fro
44970 6d 20 77 69 74 68 69 6e 20 74 68 65 20 63 61 6c m within the cal
44980 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 75 6e 6c l to sqlite3_unl
44990 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 2e 0a 2a 2a ock_notify()..**
449a0 0a 2a 2a 20 49 66 20 74 68 65 20 62 6c 6f 63 6b .** If the block
449b0 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 ed connection is
449c0 20 61 74 74 65 6d 70 74 69 6e 67 20 74 6f 20 6f attempting to o
449d0 62 74 61 69 6e 20 61 20 77 72 69 74 65 2d 6c 6f btain a write-lo
449e0 63 6b 20 6f 6e 20 61 0a 2a 2a 20 73 68 61 72 65 ck on a.** share
449f0 64 2d 63 61 63 68 65 20 74 61 62 6c 65 2c 20 61 d-cache table, a
44a00 6e 64 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 nd more than one
44a10 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f other connectio
44a20 6e 20 63 75 72 72 65 6e 74 6c 79 20 68 6f 6c 64 n currently hold
44a30 73 0a 2a 2a 20 61 20 72 65 61 64 2d 6c 6f 63 6b s.** a read-lock
44a40 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 74 61 62 on the same tab
44a50 6c 65 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 20 le, then SQLite
44a60 61 72 62 69 74 72 61 72 69 6c 79 20 73 65 6c 65 arbitrarily sele
44a70 63 74 73 20 6f 6e 65 20 6f 66 20 0a 2a 2a 20 74 cts one of .** t
44a80 68 65 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 he other connect
44a90 69 6f 6e 73 20 74 6f 20 75 73 65 20 61 73 20 74 ions to use as t
44aa0 68 65 20 62 6c 6f 63 6b 69 6e 67 20 63 6f 6e 6e he blocking conn
44ab0 65 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 ection..**.** Th
44ac0 65 72 65 20 6d 61 79 20 62 65 20 61 74 20 6d 6f ere may be at mo
44ad0 73 74 20 6f 6e 65 20 75 6e 6c 6f 63 6b 2d 6e 6f st one unlock-no
44ae0 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 72 65 tify callback re
44af0 67 69 73 74 65 72 65 64 20 62 79 20 61 20 0a 2a gistered by a .*
44b00 2a 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 * blocked connec
44b10 74 69 6f 6e 2e 20 49 66 20 73 71 6c 69 74 65 33 tion. If sqlite3
44b20 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 _unlock_notify()
44b30 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 is called when
44b40 74 68 65 0a 2a 2a 20 62 6c 6f 63 6b 65 64 20 63 the.** blocked c
44b50 6f 6e 6e 65 63 74 69 6f 6e 20 61 6c 72 65 61 64 onnection alread
44b60 79 20 68 61 73 20 61 20 72 65 67 69 73 74 65 72 y has a register
44b70 65 64 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 ed unlock-notify
44b80 20 63 61 6c 6c 62 61 63 6b 2c 0a 2a 2a 20 74 68 callback,.** th
44b90 65 6e 20 74 68 65 20 6e 65 77 20 63 61 6c 6c 62 en the new callb
44ba0 61 63 6b 20 72 65 70 6c 61 63 65 73 20 74 68 65 ack replaces the
44bb0 20 6f 6c 64 2e 20 49 66 20 73 71 6c 69 74 65 33 old. If sqlite3
44bc0 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 _unlock_notify()
44bd0 20 69 73 0a 2a 2a 20 63 61 6c 6c 65 64 20 77 69 is.** called wi
44be0 74 68 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 th a NULL pointe
44bf0 72 20 61 73 20 69 74 73 20 73 65 63 6f 6e 64 20 r as its second
44c00 61 72 67 75 6d 65 6e 74 2c 20 74 68 65 6e 20 61 argument, then a
44c10 6e 79 20 65 78 69 73 74 69 6e 67 0a 2a 2a 20 75 ny existing.** u
44c20 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c nlock-notify cal
44c30 6c 62 61 63 6b 20 69 73 20 63 61 6e 63 65 6c 6c lback is cancell
44c40 65 64 2e 20 54 68 65 20 62 6c 6f 63 6b 65 64 20 ed. The blocked
44c50 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 0a 2a 2a 20 connections .**
44c60 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 unlock-notify ca
44c70 6c 6c 62 61 63 6b 20 6d 61 79 20 61 6c 73 6f 20 llback may also
44c80 62 65 20 63 61 6e 63 65 6c 65 64 20 62 79 20 63 be canceled by c
44c90 6c 6f 73 69 6e 67 20 74 68 65 20 62 6c 6f 63 6b losing the block
44ca0 65 64 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e ed.** connection
44cb0 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f using [sqlite3_
44cc0 63 6c 6f 73 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 close()]..**.**
44cd0 54 68 65 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 The unlock-notif
44ce0 79 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 6e 6f y callback is no
44cf0 74 20 72 65 65 6e 74 72 61 6e 74 2e 20 49 66 20 t reentrant. If
44d00 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 69 an application i
44d10 6e 76 6f 6b 65 73 0a 2a 2a 20 61 6e 79 20 73 71 nvokes.** any sq
44d20 6c 69 74 65 33 5f 78 78 78 20 41 50 49 20 66 75 lite3_xxx API fu
44d30 6e 63 74 69 6f 6e 73 20 66 72 6f 6d 20 77 69 74 nctions from wit
44d40 68 69 6e 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f hin an unlock-no
44d50 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 2c 20 61 tify callback, a
44d60 0a 2a 2a 20 63 72 61 73 68 20 6f 72 20 64 65 61 .** crash or dea
44d70 64 6c 6f 63 6b 20 6d 61 79 20 62 65 20 74 68 65 dlock may be the
44d80 20 72 65 73 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 55 result..**.** U
44d90 6e 6c 65 73 73 20 64 65 61 64 6c 6f 63 6b 20 69 nless deadlock i
44da0 73 20 64 65 74 65 63 74 65 64 20 28 73 65 65 20 s detected (see
44db0 62 65 6c 6f 77 29 2c 20 73 71 6c 69 74 65 33 5f below), sqlite3_
44dc0 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 20 unlock_notify()
44dd0 61 6c 77 61 79 73 0a 2a 2a 20 72 65 74 75 72 6e always.** return
44de0 73 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a 0a s SQLITE_OK..**.
44df0 2a 2a 20 3c 62 3e 43 61 6c 6c 62 61 63 6b 20 49 ** <b>Callback I
44e00 6e 76 6f 63 61 74 69 6f 6e 20 44 65 74 61 69 6c nvocation Detail
44e10 73 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e s</b>.**.** When
44e20 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 an unlock-notif
44e30 79 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 72 65 y callback is re
44e40 67 69 73 74 65 72 65 64 2c 20 74 68 65 20 61 70 gistered, the ap
44e50 70 6c 69 63 61 74 69 6f 6e 20 70 72 6f 76 69 64 plication provid
44e60 65 73 20 61 20 0a 2a 2a 20 73 69 6e 67 6c 65 20 es a .** single
44e70 76 6f 69 64 2a 20 70 6f 69 6e 74 65 72 20 74 68 void* pointer th
44e80 61 74 20 69 73 20 70 61 73 73 65 64 20 74 6f 20 at is passed to
44e90 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 77 68 65 the callback whe
44ea0 6e 20 69 74 20 69 73 20 69 6e 76 6f 6b 65 64 2e n it is invoked.
44eb0 0a 2a 2a 20 48 6f 77 65 76 65 72 2c 20 74 68 65 .** However, the
44ec0 20 73 69 67 6e 61 74 75 72 65 20 6f 66 20 74 68 signature of th
44ed0 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 e callback funct
44ee0 69 6f 6e 20 61 6c 6c 6f 77 73 20 53 51 4c 69 74 ion allows SQLit
44ef0 65 20 74 6f 20 70 61 73 73 0a 2a 2a 20 69 74 20 e to pass.** it
44f00 61 6e 20 61 72 72 61 79 20 6f 66 20 76 6f 69 64 an array of void
44f10 2a 20 63 6f 6e 74 65 78 74 20 70 6f 69 6e 74 65 * context pointe
44f20 72 73 2e 20 54 68 65 20 66 69 72 73 74 20 61 72 rs. The first ar
44f30 67 75 6d 65 6e 74 20 70 61 73 73 65 64 20 74 6f gument passed to
44f40 0a 2a 2a 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f .** an unlock-no
44f50 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 69 73 tify callback is
44f60 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e a pointer to an
44f70 20 61 72 72 61 79 20 6f 66 20 76 6f 69 64 2a 20 array of void*
44f80 70 6f 69 6e 74 65 72 73 2c 0a 2a 2a 20 61 6e 64 pointers,.** and
44f90 20 74 68 65 20 73 65 63 6f 6e 64 20 69 73 20 74 the second is t
44fa0 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 he number of ent
44fb0 72 69 65 73 20 69 6e 20 74 68 65 20 61 72 72 61 ries in the arra
44fc0 79 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 y..**.** When a
44fd0 62 6c 6f 63 6b 69 6e 67 20 63 6f 6e 6e 65 63 74 blocking connect
44fe0 69 6f 6e 73 20 74 72 61 6e 73 61 63 74 69 6f 6e ions transaction
44ff0 20 69 73 20 63 6f 6e 63 6c 75 64 65 64 2c 20 74 is concluded, t
45000 68 65 72 65 20 6d 61 79 20 62 65 0a 2a 2a 20 6d here may be.** m
45010 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 62 6c 6f ore than one blo
45020 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 cked connection
45030 74 68 61 74 20 68 61 73 20 72 65 67 69 73 74 65 that has registe
45040 72 65 64 20 66 6f 72 20 61 6e 20 75 6e 6c 6f 63 red for an unloc
45050 6b 2d 6e 6f 74 69 66 79 0a 2a 2a 20 63 61 6c 6c k-notify.** call
45060 62 61 63 6b 2e 20 49 66 20 74 77 6f 20 6f 72 20 back. If two or
45070 6d 6f 72 65 20 73 75 63 68 20 62 6c 6f 63 6b 65 more such blocke
45080 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 68 61 d connections ha
45090 76 65 20 73 70 65 63 69 66 69 65 64 20 74 68 65 ve specified the
450a0 0a 2a 2a 20 73 61 6d 65 20 63 61 6c 6c 62 61 63 .** same callbac
450b0 6b 20 66 75 6e 63 74 69 6f 6e 2c 20 74 68 65 6e k function, then
450c0 20 69 6e 73 74 65 61 64 20 6f 66 20 69 6e 76 6f instead of invo
450d0 6b 69 6e 67 20 74 68 65 20 63 61 6c 6c 62 61 63 king the callbac
450e0 6b 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 6d 75 k function.** mu
450f0 6c 74 69 70 6c 65 20 74 69 6d 65 73 2c 20 69 74 ltiple times, it
45100 20 69 73 20 69 6e 76 6f 6b 65 64 20 6f 6e 63 65 is invoked once
45110 20 77 69 74 68 20 74 68 65 20 73 65 74 20 6f 66 with the set of
45120 20 76 6f 69 64 2a 20 63 6f 6e 74 65 78 74 20 70 void* context p
45130 6f 69 6e 74 65 72 73 0a 2a 2a 20 73 70 65 63 69 ointers.** speci
45140 66 69 65 64 20 62 79 20 74 68 65 20 62 6c 6f 63 fied by the bloc
45150 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 ked connections
45160 62 75 6e 64 6c 65 64 20 74 6f 67 65 74 68 65 72 bundled together
45170 20 69 6e 74 6f 20 61 6e 20 61 72 72 61 79 2e 0a into an array..
45180 2a 2a 20 54 68 69 73 20 67 69 76 65 73 20 74 68 ** This gives th
45190 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 61 6e e application an
451a0 20 6f 70 70 6f 72 74 75 6e 69 74 79 20 74 6f 20 opportunity to
451b0 70 72 69 6f 72 69 74 69 7a 65 20 61 6e 79 20 61 prioritize any a
451c0 63 74 69 6f 6e 73 20 0a 2a 2a 20 72 65 6c 61 74 ctions .** relat
451d0 65 64 20 74 6f 20 74 68 65 20 73 65 74 20 6f 66 ed to the set of
451e0 20 75 6e 62 6c 6f 63 6b 65 64 20 64 61 74 61 62 unblocked datab
451f0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e ase connections.
45200 0a 2a 2a 0a 2a 2a 20 3c 62 3e 44 65 61 64 6c 6f .**.** <b>Deadlo
45210 63 6b 20 44 65 74 65 63 74 69 6f 6e 3c 2f 62 3e ck Detection</b>
45220 0a 2a 2a 0a 2a 2a 20 41 73 73 75 6d 69 6e 67 20 .**.** Assuming
45230 74 68 61 74 20 61 66 74 65 72 20 72 65 67 69 73 that after regis
45240 74 65 72 69 6e 67 20 66 6f 72 20 61 6e 20 75 6e tering for an un
45250 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c lock-notify call
45260 62 61 63 6b 20 61 20 0a 2a 2a 20 64 61 74 61 62 back a .** datab
45270 61 73 65 20 77 61 69 74 73 20 66 6f 72 20 74 68 ase waits for th
45280 65 20 63 61 6c 6c 62 61 63 6b 20 74 6f 20 62 65 e callback to be
45290 20 69 73 73 75 65 64 20 62 65 66 6f 72 65 20 74 issued before t
452a0 61 6b 69 6e 67 20 61 6e 79 20 66 75 72 74 68 65 aking any furthe
452b0 72 0a 2a 2a 20 61 63 74 69 6f 6e 20 28 61 20 72 r.** action (a r
452c0 65 61 73 6f 6e 61 62 6c 65 20 61 73 73 75 6d 70 easonable assump
452d0 74 69 6f 6e 29 2c 20 74 68 65 6e 20 75 73 69 6e tion), then usin
452e0 67 20 74 68 69 73 20 41 50 49 20 6d 61 79 20 63 g this API may c
452f0 61 75 73 65 20 74 68 65 0a 2a 2a 20 61 70 70 6c ause the.** appl
45300 69 63 61 74 69 6f 6e 20 74 6f 20 64 65 61 64 6c ication to deadl
45310 6f 63 6b 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 ock. For example
45320 2c 20 69 66 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 , if connection
45330 58 20 69 73 20 77 61 69 74 69 6e 67 20 66 6f 72 X is waiting for
45340 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 59 .** connection Y
45350 27 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 74 's transaction t
45360 6f 20 62 65 20 63 6f 6e 63 6c 75 64 65 64 2c 20 o be concluded,
45370 61 6e 64 20 73 69 6d 69 6c 61 72 6c 79 20 63 6f and similarly co
45380 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 59 20 69 73 nnection.** Y is
45390 20 77 61 69 74 69 6e 67 20 6f 6e 20 63 6f 6e 6e waiting on conn
453a0 65 63 74 69 6f 6e 20 58 27 73 20 74 72 61 6e 73 ection X's trans
453b0 61 63 74 69 6f 6e 2c 20 74 68 65 6e 20 6e 65 69 action, then nei
453c0 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a ther connection.
453d0 2a 2a 20 77 69 6c 6c 20 70 72 6f 63 65 65 64 20 ** will proceed
453e0 61 6e 64 20 74 68 65 20 73 79 73 74 65 6d 20 6d and the system m
453f0 61 79 20 72 65 6d 61 69 6e 20 64 65 61 64 6c 6f ay remain deadlo
45400 63 6b 65 64 20 69 6e 64 65 66 69 6e 69 74 65 6c cked indefinitel
45410 79 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 61 76 6f 69 y..**.** To avoi
45420 64 20 74 68 69 73 20 73 63 65 6e 61 72 69 6f 2c d this scenario,
45430 20 74 68 65 20 73 71 6c 69 74 65 33 5f 75 6e 6c the sqlite3_unl
45440 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 20 70 65 72 ock_notify() per
45450 66 6f 72 6d 73 20 64 65 61 64 6c 6f 63 6b 0a 2a forms deadlock.*
45460 2a 20 64 65 74 65 63 74 69 6f 6e 2e 20 49 66 20 * detection. If
45470 61 20 67 69 76 65 6e 20 63 61 6c 6c 20 74 6f 20 a given call to
45480 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e sqlite3_unlock_n
45490 6f 74 69 66 79 28 29 20 77 6f 75 6c 64 20 70 75 otify() would pu
454a0 74 20 74 68 65 0a 2a 2a 20 73 79 73 74 65 6d 20 t the.** system
454b0 69 6e 20 61 20 64 65 61 64 6c 6f 63 6b 65 64 20 in a deadlocked
454c0 73 74 61 74 65 2c 20 74 68 65 6e 20 53 51 4c 49 state, then SQLI
454d0 54 45 5f 4c 4f 43 4b 45 44 20 69 73 20 72 65 74 TE_LOCKED is ret
454e0 75 72 6e 65 64 20 61 6e 64 20 6e 6f 0a 2a 2a 20 urned and no.**
454f0 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 unlock-notify ca
45500 6c 6c 62 61 63 6b 20 69 73 20 72 65 67 69 73 74 llback is regist
45510 65 72 65 64 2e 20 54 68 65 20 73 79 73 74 65 6d ered. The system
45520 20 69 73 20 73 61 69 64 20 74 6f 20 62 65 20 69 is said to be i
45530 6e 0a 2a 2a 20 61 20 64 65 61 64 6c 6f 63 6b 65 n.** a deadlocke
45540 64 20 73 74 61 74 65 20 69 66 20 63 6f 6e 6e 65 d state if conne
45550 63 74 69 6f 6e 20 41 20 68 61 73 20 72 65 67 69 ction A has regi
45560 73 74 65 72 65 64 20 66 6f 72 20 61 6e 20 75 6e stered for an un
45570 6c 6f 63 6b 2d 6e 6f 74 69 66 79 0a 2a 2a 20 63 lock-notify.** c
45580 61 6c 6c 62 61 63 6b 20 6f 6e 20 74 68 65 20 63 allback on the c
45590 6f 6e 63 6c 75 73 69 6f 6e 20 6f 66 20 63 6f 6e onclusion of con
455a0 6e 65 63 74 69 6f 6e 20 42 27 73 20 74 72 61 6e nection B's tran
455b0 73 61 63 74 69 6f 6e 2c 20 61 6e 64 20 63 6f 6e saction, and con
455c0 6e 65 63 74 69 6f 6e 0a 2a 2a 20 42 20 68 61 73 nection.** B has
455d0 20 69 74 73 65 6c 66 20 72 65 67 69 73 74 65 72 itself register
455e0 65 64 20 66 6f 72 20 61 6e 20 75 6e 6c 6f 63 6b ed for an unlock
455f0 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b -notify callback
45600 20 77 68 65 6e 20 63 6f 6e 6e 65 63 74 69 6f 6e when connection
45610 0a 2a 2a 20 41 27 73 20 74 72 61 6e 73 61 63 74 .** A's transact
45620 69 6f 6e 20 69 73 20 63 6f 6e 63 6c 75 64 65 64 ion is concluded
45630 2e 20 49 6e 64 69 72 65 63 74 20 64 65 61 64 6c . Indirect deadl
45640 6f 63 6b 20 69 73 20 61 6c 73 6f 20 64 65 74 65 ock is also dete
45650 63 74 65 64 2c 20 73 6f 0a 2a 2a 20 74 68 65 20 cted, so.** the
45660 73 79 73 74 65 6d 20 69 73 20 61 6c 73 6f 20 63 system is also c
45670 6f 6e 73 69 64 65 72 65 64 20 74 6f 20 62 65 20 onsidered to be
45680 64 65 61 64 6c 6f 63 6b 65 64 20 69 66 20 63 6f deadlocked if co
45690 6e 6e 65 63 74 69 6f 6e 20 42 20 68 61 73 0a 2a nnection B has.*
456a0 2a 20 72 65 67 69 73 74 65 72 65 64 20 66 6f 72 * registered for
456b0 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 an unlock-notif
456c0 79 20 63 61 6c 6c 62 61 63 6b 20 6f 6e 20 74 68 y callback on th
456d0 65 20 63 6f 6e 63 6c 75 73 69 6f 6e 20 6f 66 20 e conclusion of
456e0 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 43 27 connection.** C'
456f0 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 77 s transaction, w
45700 68 65 72 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 here connection
45710 43 20 69 73 20 77 61 69 74 69 6e 67 20 6f 6e 20 C is waiting on
45720 63 6f 6e 6e 65 63 74 69 6f 6e 20 41 2e 20 41 6e connection A. An
45730 79 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 6c y.** number of l
45740 65 76 65 6c 73 20 6f 66 20 69 6e 64 69 72 65 63 evels of indirec
45750 74 69 6f 6e 20 61 72 65 20 61 6c 6c 6f 77 65 64 tion are allowed
45760 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 54 68 65 20 22 ..**.** <b>The "
45770 44 52 4f 50 20 54 41 42 4c 45 22 20 45 78 63 65 DROP TABLE" Exce
45780 70 74 69 6f 6e 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20 ption</b>.**.**
45790 57 68 65 6e 20 61 20 63 61 6c 6c 20 74 6f 20 5b When a call to [
457a0 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 sqlite3_step()]
457b0 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4c returns SQLITE_L
457c0 4f 43 4b 45 44 2c 20 69 74 20 69 73 20 61 6c 6d OCKED, it is alm
457d0 6f 73 74 20 0a 2a 2a 20 61 6c 77 61 79 73 20 61 ost .** always a
457e0 70 70 72 6f 70 72 69 61 74 65 20 74 6f 20 63 61 ppropriate to ca
457f0 6c 6c 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 ll sqlite3_unloc
45800 6b 5f 6e 6f 74 69 66 79 28 29 2e 20 54 68 65 72 k_notify(). Ther
45810 65 20 69 73 20 68 6f 77 65 76 65 72 2c 0a 2a 2a e is however,.**
45820 20 6f 6e 65 20 65 78 63 65 70 74 69 6f 6e 2e 20 one exception.
45830 57 68 65 6e 20 65 78 65 63 75 74 69 6e 67 20 61 When executing a
45840 20 22 44 52 4f 50 20 54 41 42 4c 45 22 20 6f 72 "DROP TABLE" or
45850 20 22 44 52 4f 50 20 49 4e 44 45 58 22 20 73 74 "DROP INDEX" st
45860 61 74 65 6d 65 6e 74 2c 0a 2a 2a 20 53 51 4c 69 atement,.** SQLi
45870 74 65 20 63 68 65 63 6b 73 20 69 66 20 74 68 65 te checks if the
45880 72 65 20 61 72 65 20 61 6e 79 20 63 75 72 72 65 re are any curre
45890 6e 74 6c 79 20 65 78 65 63 75 74 69 6e 67 20 53 ntly executing S
458a0 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 ELECT statements
458b0 0a 2a 2a 20 74 68 61 74 20 62 65 6c 6f 6e 67 20 .** that belong
458c0 74 6f 20 74 68 65 20 73 61 6d 65 20 63 6f 6e 6e to the same conn
458d0 65 63 74 69 6f 6e 2e 20 49 66 20 74 68 65 72 65 ection. If there
458e0 20 61 72 65 2c 20 53 51 4c 49 54 45 5f 4c 4f 43 are, SQLITE_LOC
458f0 4b 45 44 20 69 73 0a 2a 2a 20 72 65 74 75 72 6e KED is.** return
45900 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 ed. In this case
45910 20 74 68 65 72 65 20 69 73 20 6e 6f 20 22 62 6c there is no "bl
45920 6f 63 6b 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f ocking connectio
45930 6e 22 2c 20 73 6f 20 69 6e 76 6f 6b 69 6e 67 0a n", so invoking.
45940 2a 2a 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 ** sqlite3_unloc
45950 6b 5f 6e 6f 74 69 66 79 28 29 20 72 65 73 75 6c k_notify() resul
45960 74 73 20 69 6e 20 74 68 65 20 75 6e 6c 6f 63 6b ts in the unlock
45970 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b -notify callback
45980 20 62 65 69 6e 67 0a 2a 2a 20 69 6e 76 6f 6b 65 being.** invoke
45990 64 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 20 49 d immediately. I
459a0 66 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f f the applicatio
459b0 6e 20 74 68 65 6e 20 72 65 2d 61 74 74 65 6d 70 n then re-attemp
459c0 74 73 20 74 68 65 20 22 44 52 4f 50 20 54 41 42 ts the "DROP TAB
459d0 4c 45 22 0a 2a 2a 20 6f 72 20 22 44 52 4f 50 20 LE".** or "DROP
459e0 49 4e 44 45 58 22 20 71 75 65 72 79 2c 20 61 6e INDEX" query, an
459f0 20 69 6e 66 69 6e 69 74 65 20 6c 6f 6f 70 20 6d infinite loop m
45a00 69 67 68 74 20 62 65 20 74 68 65 20 72 65 73 75 ight be the resu
45a10 6c 74 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 65 20 77 61 lt..**.** One wa
45a20 79 20 61 72 6f 75 6e 64 20 74 68 69 73 20 70 72 y around this pr
45a30 6f 62 6c 65 6d 20 69 73 20 74 6f 20 63 68 65 63 oblem is to chec
45a40 6b 20 74 68 65 20 65 78 74 65 6e 64 65 64 20 65 k the extended e
45a50 72 72 6f 72 20 63 6f 64 65 20 72 65 74 75 72 6e rror code return
45a60 65 64 0a 2a 2a 20 62 79 20 61 6e 20 73 71 6c 69 ed.** by an sqli
45a70 74 65 33 5f 73 74 65 70 28 29 20 63 61 6c 6c 2e te3_step() call.
45a80 20 49 66 20 74 68 65 72 65 20 69 73 20 61 20 62 If there is a b
45a90 6c 6f 63 6b 69 6e 67 20 63 6f 6e 6e 65 63 74 69 locking connecti
45aa0 6f 6e 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 on, then the.**
45ab0 65 78 74 65 6e 64 65 64 20 65 72 72 6f 72 20 63 extended error c
45ac0 6f 64 65 20 69 73 20 73 65 74 20 74 6f 20 53 51 ode is set to SQ
45ad0 4c 49 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 LITE_LOCKED_SHAR
45ae0 45 44 43 41 43 48 45 2e 20 4f 74 68 65 72 77 69 EDCACHE. Otherwi
45af0 73 65 2c 20 69 6e 0a 2a 2a 20 74 68 65 20 73 70 se, in.** the sp
45b00 65 63 69 61 6c 20 22 44 52 4f 50 20 54 41 42 4c ecial "DROP TABL
45b10 45 2f 49 4e 44 45 58 22 20 63 61 73 65 2c 20 74 E/INDEX" case, t
45b20 68 65 20 65 78 74 65 6e 64 65 64 20 65 72 72 6f he extended erro
45b30 72 20 63 6f 64 65 20 69 73 20 6a 75 73 74 20 0a r code is just .
45b40 2a 2a 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 ** SQLITE_LOCKED
45b50 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
45b60 69 6e 74 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f int sqlite3_unlo
45b70 63 6b 5f 6e 6f 74 69 66 79 28 0a 20 20 73 71 6c ck_notify(. sql
45b80 69 74 65 33 20 2a 70 42 6c 6f 63 6b 65 64 2c 20 ite3 *pBlocked,
45b90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
45ba0 20 20 20 20 20 20 20 20 20 2f 2a 20 57 61 69 74 /* Wait
45bb0 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a ing connection *
45bc0 2f 0a 20 20 76 6f 69 64 20 28 2a 78 4e 6f 74 69 /. void (*xNoti
45bd0 66 79 29 28 76 6f 69 64 20 2a 2a 61 70 41 72 67 fy)(void **apArg
45be0 2c 20 69 6e 74 20 6e 41 72 67 29 2c 20 20 20 20 , int nArg),
45bf0 2f 2a 20 43 61 6c 6c 62 61 63 6b 20 66 75 6e 63 /* Callback func
45c00 74 69 6f 6e 20 74 6f 20 69 6e 76 6f 6b 65 20 2a tion to invoke *
45c10 2f 0a 20 20 76 6f 69 64 20 2a 70 4e 6f 74 69 66 /. void *pNotif
45c20 79 41 72 67 20 20 20 20 20 20 20 20 20 20 20 20 yArg
45c30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
45c40 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 70 /* Argument to p
45c50 61 73 73 20 74 6f 20 78 4e 6f 74 69 66 79 20 2a ass to xNotify *
45c60 2f 0a 29 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 /.);.../*.** CAP
45c70 49 33 52 45 46 3a 20 53 74 72 69 6e 67 20 43 6f I3REF: String Co
45c80 6d 70 61 72 69 73 6f 6e 0a 2a 2a 20 45 58 50 45 mparison.** EXPE
45c90 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 RIMENTAL.**.** T
45ca0 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 72 6e he [sqlite3_strn
45cb0 69 63 6d 70 28 29 5d 20 41 50 49 20 61 6c 6c 6f icmp()] API allo
45cc0 77 73 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 ws applications
45cd0 61 6e 64 20 65 78 74 65 6e 73 69 6f 6e 73 20 74 and extensions t
45ce0 6f 0a 2a 2a 20 63 6f 6d 70 61 72 65 20 74 68 65 o.** compare the
45cf0 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 77 6f contents of two
45d00 20 62 75 66 66 65 72 73 20 63 6f 6e 74 61 69 6e buffers contain
45d10 69 6e 67 20 55 54 46 2d 38 20 73 74 72 69 6e 67 ing UTF-8 string
45d20 73 20 69 6e 20 61 0a 2a 2a 20 63 61 73 65 2d 69 s in a.** case-i
45d30 6e 64 65 6e 64 65 6e 74 20 66 61 73 68 69 6f 6e ndendent fashion
45d40 2c 20 75 73 69 6e 67 20 74 68 65 20 73 61 6d 65 , using the same
45d50 20 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66 20 63 definition of c
45d60 61 73 65 20 69 6e 64 65 70 65 6e 64 65 6e 63 65 ase independence
45d70 20 0a 2a 2a 20 74 68 61 74 20 53 51 4c 69 74 65 .** that SQLite
45d80 20 75 73 65 73 20 69 6e 74 65 72 6e 61 6c 6c 79 uses internally
45d90 20 77 68 65 6e 20 63 6f 6d 70 61 72 69 6e 67 20 when comparing
45da0 69 64 65 6e 74 69 66 69 65 72 73 2e 0a 2a 2f 0a identifiers..*/.
45db0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
45dc0 71 6c 69 74 65 33 5f 73 74 72 6e 69 63 6d 70 28 qlite3_strnicmp(
45dd0 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 63 6f const char *, co
45de0 6e 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 29 nst char *, int)
45df0 3b 0a 0a 2f 2a 0a 2a 2a 20 55 6e 64 6f 20 74 68 ;../*.** Undo th
45e00 65 20 68 61 63 6b 20 74 68 61 74 20 63 6f 6e 76 e hack that conv
45e10 65 72 74 73 20 66 6c 6f 61 74 69 6e 67 20 70 6f erts floating po
45e20 69 6e 74 20 74 79 70 65 73 20 74 6f 20 69 6e 74 int types to int
45e30 65 67 65 72 20 66 6f 72 0a 2a 2a 20 62 75 69 6c eger for.** buil
45e40 64 73 20 6f 6e 20 70 72 6f 63 65 73 73 6f 72 73 ds on processors
45e50 20 77 69 74 68 6f 75 74 20 66 6c 6f 61 74 69 6e without floatin
45e60 67 20 70 6f 69 6e 74 20 73 75 70 70 6f 72 74 2e g point support.
45e70 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 .*/.#ifdef SQLIT
45e80 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f E_OMIT_FLOATING_
45e90 50 4f 49 4e 54 0a 23 20 75 6e 64 65 66 20 64 6f POINT.# undef do
45ea0 75 62 6c 65 0a 23 65 6e 64 69 66 0a 0a 23 69 66 uble.#endif..#if
45eb0 20 30 0a 7d 20 20 2f 2a 20 45 6e 64 20 6f 66 20 0.} /* End of
45ec0 74 68 65 20 27 65 78 74 65 72 6e 20 22 43 22 27 the 'extern "C"'
45ed0 20 62 6c 6f 63 6b 20 2a 2f 0a 23 65 6e 64 69 66 block */.#endif
45ee0 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 2a 2a 2a 2a .#endif.../*****
45ef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 ********* End of
45f00 20 73 71 6c 69 74 65 33 2e 68 20 2a 2a 2a 2a 2a sqlite3.h *****
45f10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
45f20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
45f30 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
45f40 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e ********* Contin
45f50 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 uing where we le
45f60 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 ft off in sqlite
45f70 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a Int.h **********
45f80 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
45f90 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 ********* Includ
45fa0 65 20 68 61 73 68 2e 68 20 69 6e 20 74 68 65 20 e hash.h in the
45fb0 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 middle of sqlite
45fc0 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a Int.h **********
45fd0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
45fe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 ********* Begin
45ff0 66 69 6c 65 20 68 61 73 68 2e 68 20 2a 2a 2a 2a file hash.h ****
46000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
46010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
46020 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 ********/./*.**
46030 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 32 2001 September 2
46040 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 2.**.** The auth
46050 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
46060 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
46070 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
46080 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
46090 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
460a0 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
460b0 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
460c0 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
460d0 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
460e0 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
460f0 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
46100 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
46110 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
46120 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
46130 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
46140 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
46150 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
46160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
46170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
46180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
46190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
461a0 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 .** This is the
461b0 68 65 61 64 65 72 20 66 69 6c 65 20 66 6f 72 20 header file for
461c0 74 68 65 20 67 65 6e 65 72 69 63 20 68 61 73 68 the generic hash
461d0 2d 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 61 -table implemena
461e0 74 69 6f 6e 0a 2a 2a 20 75 73 65 64 20 69 6e 20 tion.** used in
461f0 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 SQLite..**.** $I
46200 64 3a 20 68 61 73 68 2e 68 2c 76 20 31 2e 31 35 d: hash.h,v 1.15
46210 20 32 30 30 39 2f 30 35 2f 30 32 20 31 33 3a 32 2009/05/02 13:2
46220 39 3a 33 38 20 64 72 68 20 45 78 70 20 24 0a 2a 9:38 drh Exp $.*
46230 2f 0a 23 69 66 6e 64 65 66 20 5f 53 51 4c 49 54 /.#ifndef _SQLIT
46240 45 5f 48 41 53 48 5f 48 5f 0a 23 64 65 66 69 6e E_HASH_H_.#defin
46250 65 20 5f 53 51 4c 49 54 45 5f 48 41 53 48 5f 48 e _SQLITE_HASH_H
46260 5f 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 64 65 _../* Forward de
46270 63 6c 61 72 61 74 69 6f 6e 73 20 6f 66 20 73 74 clarations of st
46280 72 75 63 74 75 72 65 73 2e 20 2a 2f 0a 74 79 70 ructures. */.typ
46290 65 64 65 66 20 73 74 72 75 63 74 20 48 61 73 68 edef struct Hash
462a0 20 48 61 73 68 3b 0a 74 79 70 65 64 65 66 20 73 Hash;.typedef s
462b0 74 72 75 63 74 20 48 61 73 68 45 6c 65 6d 20 48 truct HashElem H
462c0 61 73 68 45 6c 65 6d 3b 0a 0a 2f 2a 20 41 20 63 ashElem;../* A c
462d0 6f 6d 70 6c 65 74 65 20 68 61 73 68 20 74 61 62 omplete hash tab
462e0 6c 65 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 le is an instanc
462f0 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 e of the followi
46300 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a ng structure..**
46310 20 54 68 65 20 69 6e 74 65 72 6e 61 6c 73 20 6f The internals o
46320 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 f this structure
46330 20 61 72 65 20 69 6e 74 65 6e 64 65 64 20 74 6f are intended to
46340 20 62 65 20 6f 70 61 71 75 65 20 2d 2d 20 63 6c be opaque -- cl
46350 69 65 6e 74 0a 2a 2a 20 63 6f 64 65 20 73 68 6f ient.** code sho
46360 75 6c 64 20 6e 6f 74 20 61 74 74 65 6d 70 74 20 uld not attempt
46370 74 6f 20 61 63 63 65 73 73 20 6f 72 20 6d 6f 64 to access or mod
46380 69 66 79 20 74 68 65 20 66 69 65 6c 64 73 20 6f ify the fields o
46390 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 f this structure
463a0 0a 2a 2a 20 64 69 72 65 63 74 6c 79 2e 20 20 43 .** directly. C
463b0 68 61 6e 67 65 20 74 68 69 73 20 73 74 72 75 63 hange this struc
463c0 74 75 72 65 20 6f 6e 6c 79 20 62 79 20 75 73 69 ture only by usi
463d0 6e 67 20 74 68 65 20 72 6f 75 74 69 6e 65 73 20 ng the routines
463e0 62 65 6c 6f 77 2e 0a 2a 2a 20 48 6f 77 65 76 65 below..** Howeve
463f0 72 2c 20 73 6f 6d 65 20 6f 66 20 74 68 65 20 22 r, some of the "
46400 70 72 6f 63 65 64 75 72 65 73 22 20 61 6e 64 20 procedures" and
46410 22 66 75 6e 63 74 69 6f 6e 73 22 20 66 6f 72 20 "functions" for
46420 6d 6f 64 69 66 79 69 6e 67 20 61 6e 64 0a 2a 2a modifying and.**
46430 20 61 63 63 65 73 73 69 6e 67 20 74 68 69 73 20 accessing this
46440 73 74 72 75 63 74 75 72 65 20 61 72 65 20 72 65 structure are re
46450 61 6c 6c 79 20 6d 61 63 72 6f 73 2c 20 73 6f 20 ally macros, so
46460 77 65 20 63 61 6e 27 74 20 72 65 61 6c 6c 79 20 we can't really
46470 6d 61 6b 65 0a 2a 2a 20 74 68 69 73 20 73 74 72 make.** this str
46480 75 63 74 75 72 65 20 6f 70 61 71 75 65 2e 0a 2a ucture opaque..*
46490 2a 0a 2a 2a 20 41 6c 6c 20 65 6c 65 6d 65 6e 74 *.** All element
464a0 73 20 6f 66 20 74 68 65 20 68 61 73 68 20 74 61 s of the hash ta
464b0 62 6c 65 20 61 72 65 20 6f 6e 20 61 20 73 69 6e ble are on a sin
464c0 67 6c 65 20 64 6f 75 62 6c 79 2d 6c 69 6e 6b 65 gle doubly-linke
464d0 64 20 6c 69 73 74 2e 0a 2a 2a 20 48 61 73 68 2e d list..** Hash.
464e0 66 69 72 73 74 20 70 6f 69 6e 74 73 20 74 6f 20 first points to
464f0 74 68 65 20 68 65 61 64 20 6f 66 20 74 68 69 73 the head of this
46500 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 list..**.** The
46510 72 65 20 61 72 65 20 48 61 73 68 2e 68 74 73 69 re are Hash.htsi
46520 7a 65 20 62 75 63 6b 65 74 73 2e 20 20 45 61 63 ze buckets. Eac
46530 68 20 62 75 63 6b 65 74 20 70 6f 69 6e 74 73 20 h bucket points
46540 74 6f 20 61 20 73 70 6f 74 20 69 6e 0a 2a 2a 20 to a spot in.**
46550 74 68 65 20 67 6c 6f 62 61 6c 20 64 6f 75 62 6c the global doubl
46560 79 2d 6c 69 6e 6b 65 64 20 6c 69 73 74 2e 20 20 y-linked list.
46570 54 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 The contents of
46580 74 68 65 20 62 75 63 6b 65 74 20 61 72 65 20 74 the bucket are t
46590 68 65 0a 2a 2a 20 65 6c 65 6d 65 6e 74 20 70 6f he.** element po
465a0 69 6e 74 65 64 20 74 6f 20 70 6c 75 73 20 74 68 inted to plus th
465b0 65 20 6e 65 78 74 20 5f 68 74 2e 63 6f 75 6e 74 e next _ht.count
465c0 2d 31 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 74 -1 elements in t
465d0 68 65 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 48 he list..**.** H
465e0 61 73 68 2e 68 74 73 69 7a 65 20 61 6e 64 20 48 ash.htsize and H
465f0 61 73 68 2e 68 74 20 6d 61 79 20 62 65 20 7a 65 ash.ht may be ze
46600 72 6f 2e 20 20 49 6e 20 74 68 61 74 20 63 61 73 ro. In that cas
46610 65 20 6c 6f 6f 6b 75 70 20 69 73 20 64 6f 6e 65 e lookup is done
46620 0a 2a 2a 20 62 79 20 61 20 6c 69 6e 65 61 72 20 .** by a linear
46630 73 65 61 72 63 68 20 6f 66 20 74 68 65 20 67 6c search of the gl
46640 6f 62 61 6c 20 6c 69 73 74 2e 20 20 46 6f 72 20 obal list. For
46650 73 6d 61 6c 6c 20 74 61 62 6c 65 73 2c 20 74 68 small tables, th
46660 65 20 0a 2a 2a 20 48 61 73 68 2e 68 74 20 74 61 e .** Hash.ht ta
46670 62 6c 65 20 69 73 20 6e 65 76 65 72 20 61 6c 6c ble is never all
46680 6f 63 61 74 65 64 20 62 65 63 61 75 73 65 20 69 ocated because i
46690 66 20 74 68 65 72 65 20 61 72 65 20 66 65 77 20 f there are few
466a0 65 6c 65 6d 65 6e 74 73 0a 2a 2a 20 69 6e 20 74 elements.** in t
466b0 68 65 20 74 61 62 6c 65 2c 20 69 74 20 69 73 20 he table, it is
466c0 66 61 73 74 65 72 20 74 6f 20 64 6f 20 61 20 6c faster to do a l
466d0 69 6e 65 61 72 20 73 65 61 72 63 68 20 74 68 61 inear search tha
466e0 6e 20 74 6f 20 6d 61 6e 61 67 65 0a 2a 2a 20 74 n to manage.** t
466f0 68 65 20 68 61 73 68 20 74 61 62 6c 65 2e 0a 2a he hash table..*
46700 2f 0a 73 74 72 75 63 74 20 48 61 73 68 20 7b 0a /.struct Hash {.
46710 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68 unsigned int h
46720 74 73 69 7a 65 3b 20 20 20 20 20 20 2f 2a 20 4e tsize; /* N
46730 75 6d 62 65 72 20 6f 66 20 62 75 63 6b 65 74 73 umber of buckets
46740 20 69 6e 20 74 68 65 20 68 61 73 68 20 74 61 62 in the hash tab
46750 6c 65 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 le */. unsigned
46760 20 69 6e 74 20 63 6f 75 6e 74 3b 20 20 20 20 20 int count;
46770 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 /* Number of e
46780 6e 74 72 69 65 73 20 69 6e 20 74 68 69 73 20 74 ntries in this t
46790 61 62 6c 65 20 2a 2f 0a 20 20 48 61 73 68 45 6c able */. HashEl
467a0 65 6d 20 2a 66 69 72 73 74 3b 20 20 20 20 20 20 em *first;
467b0 20 20 20 20 2f 2a 20 54 68 65 20 66 69 72 73 74 /* The first
467c0 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 element of the
467d0 61 72 72 61 79 20 2a 2f 0a 20 20 73 74 72 75 63 array */. struc
467e0 74 20 5f 68 74 20 7b 20 20 20 20 20 20 20 20 20 t _ht {
467f0 20 20 20 20 20 2f 2a 20 74 68 65 20 68 61 73 68 /* the hash
46800 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 69 6e table */. in
46810 74 20 63 6f 75 6e 74 3b 20 20 20 20 20 20 20 20 t count;
46820 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
46830 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 77 69 er of entries wi
46840 74 68 20 74 68 69 73 20 68 61 73 68 20 2a 2f 0a th this hash */.
46850 20 20 20 20 48 61 73 68 45 6c 65 6d 20 2a 63 68 HashElem *ch
46860 61 69 6e 3b 20 20 20 20 20 20 20 20 20 20 20 2f ain; /
46870 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 66 69 72 * Pointer to fir
46880 73 74 20 65 6e 74 72 79 20 77 69 74 68 20 74 68 st entry with th
46890 69 73 20 68 61 73 68 20 2a 2f 0a 20 20 7d 20 2a is hash */. } *
468a0 68 74 3b 0a 7d 3b 0a 0a 2f 2a 20 45 61 63 68 20 ht;.};../* Each
468b0 65 6c 65 6d 65 6e 74 20 69 6e 20 74 68 65 20 68 element in the h
468c0 61 73 68 20 74 61 62 6c 65 20 69 73 20 61 6e 20 ash table is an
468d0 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 instance of the
468e0 66 6f 6c 6c 6f 77 69 6e 67 20 0a 2a 2a 20 73 74 following .** st
468f0 72 75 63 74 75 72 65 2e 20 20 41 6c 6c 20 65 6c ructure. All el
46900 65 6d 65 6e 74 73 20 61 72 65 20 73 74 6f 72 65 ements are store
46910 64 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20 64 6f d on a single do
46920 75 62 6c 79 2d 6c 69 6e 6b 65 64 20 6c 69 73 74 ubly-linked list
46930 2e 0a 2a 2a 0a 2a 2a 20 41 67 61 69 6e 2c 20 74 ..**.** Again, t
46940 68 69 73 20 73 74 72 75 63 74 75 72 65 20 69 73 his structure is
46950 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 62 65 20 intended to be
46960 6f 70 61 71 75 65 2c 20 62 75 74 20 69 74 20 63 opaque, but it c
46970 61 6e 27 74 20 72 65 61 6c 6c 79 0a 2a 2a 20 62 an't really.** b
46980 65 20 6f 70 61 71 75 65 20 62 65 63 61 75 73 65 e opaque because
46990 20 69 74 20 69 73 20 75 73 65 64 20 62 79 20 6d it is used by m
469a0 61 63 72 6f 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 acros..*/.struct
469b0 20 48 61 73 68 45 6c 65 6d 20 7b 0a 20 20 48 61 HashElem {. Ha
469c0 73 68 45 6c 65 6d 20 2a 6e 65 78 74 2c 20 2a 70 shElem *next, *p
469d0 72 65 76 3b 20 20 20 20 20 20 20 2f 2a 20 4e 65 rev; /* Ne
469e0 78 74 20 61 6e 64 20 70 72 65 76 69 6f 75 73 20 xt and previous
469f0 65 6c 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 elements in the
46a00 74 61 62 6c 65 20 2a 2f 0a 20 20 76 6f 69 64 20 table */. void
46a10 2a 64 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 *data;
46a20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 20 /* Data
46a30 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
46a40 74 68 69 73 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a this element */.
46a50 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 4b const char *pK
46a60 65 79 3b 20 69 6e 74 20 6e 4b 65 79 3b 20 20 2f ey; int nKey; /
46a70 2a 20 4b 65 79 20 61 73 73 6f 63 69 61 74 65 64 * Key associated
46a80 20 77 69 74 68 20 74 68 69 73 20 65 6c 65 6d 65 with this eleme
46a90 6e 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 nt */.};../*.**
46aa0 41 63 63 65 73 73 20 72 6f 75 74 69 6e 65 73 2e Access routines.
46ab0 20 20 54 6f 20 64 65 6c 65 74 65 2c 20 69 6e 73 To delete, ins
46ac0 65 72 74 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 ert a NULL point
46ad0 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 er..*/.SQLITE_PR
46ae0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
46af0 65 33 48 61 73 68 49 6e 69 74 28 48 61 73 68 2a e3HashInit(Hash*
46b00 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
46b10 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 48 E void *sqlite3H
46b20 61 73 68 49 6e 73 65 72 74 28 48 61 73 68 2a 2c ashInsert(Hash*,
46b30 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 4b 65 const char *pKe
46b40 79 2c 20 69 6e 74 20 6e 4b 65 79 2c 20 76 6f 69 y, int nKey, voi
46b50 64 20 2a 70 44 61 74 61 29 3b 0a 53 51 4c 49 54 d *pData);.SQLIT
46b60 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a E_PRIVATE void *
46b70 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64 28 sqlite3HashFind(
46b80 63 6f 6e 73 74 20 48 61 73 68 2a 2c 20 63 6f 6e const Hash*, con
46b90 73 74 20 63 68 61 72 20 2a 70 4b 65 79 2c 20 69 st char *pKey, i
46ba0 6e 74 20 6e 4b 65 79 29 3b 0a 53 51 4c 49 54 45 nt nKey);.SQLITE
46bb0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
46bc0 6c 69 74 65 33 48 61 73 68 43 6c 65 61 72 28 48 lite3HashClear(H
46bd0 61 73 68 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 4d 61 ash*);../*.** Ma
46be0 63 72 6f 73 20 66 6f 72 20 6c 6f 6f 70 69 6e 67 cros for looping
46bf0 20 6f 76 65 72 20 61 6c 6c 20 65 6c 65 6d 65 6e over all elemen
46c00 74 73 20 6f 66 20 61 20 68 61 73 68 20 74 61 62 ts of a hash tab
46c10 6c 65 2e 20 20 54 68 65 20 69 64 69 6f 6d 20 69 le. The idiom i
46c20 73 0a 2a 2a 20 6c 69 6b 65 20 74 68 69 73 3a 0a s.** like this:.
46c30 2a 2a 0a 2a 2a 20 20 20 48 61 73 68 20 68 3b 0a **.** Hash h;.
46c40 2a 2a 20 20 20 48 61 73 68 45 6c 65 6d 20 2a 70 ** HashElem *p
46c50 3b 0a 2a 2a 20 20 20 2e 2e 2e 0a 2a 2a 20 20 20 ;.** ....**
46c60 66 6f 72 28 70 3d 73 71 6c 69 74 65 48 61 73 68 for(p=sqliteHash
46c70 46 69 72 73 74 28 26 68 29 3b 20 70 3b 20 70 3d First(&h); p; p=
46c80 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28 70 sqliteHashNext(p
46c90 29 29 7b 0a 2a 2a 20 20 20 20 20 53 6f 6d 65 53 )){.** SomeS
46ca0 74 72 75 63 74 75 72 65 20 2a 70 44 61 74 61 20 tructure *pData
46cb0 3d 20 73 71 6c 69 74 65 48 61 73 68 44 61 74 61 = sqliteHashData
46cc0 28 70 29 3b 0a 2a 2a 20 20 20 20 20 2f 2f 20 64 (p);.** // d
46cd0 6f 20 73 6f 6d 65 74 68 69 6e 67 20 77 69 74 68 o something with
46ce0 20 70 44 61 74 61 0a 2a 2a 20 20 20 7d 0a 2a 2f pData.** }.*/
46cf0 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 48 .#define sqliteH
46d00 61 73 68 46 69 72 73 74 28 48 29 20 20 28 28 48 ashFirst(H) ((H
46d10 29 2d 3e 66 69 72 73 74 29 0a 23 64 65 66 69 6e )->first).#defin
46d20 65 20 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74 e sqliteHashNext
46d30 28 45 29 20 20 20 28 28 45 29 2d 3e 6e 65 78 74 (E) ((E)->next
46d40 29 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 ).#define sqlite
46d50 48 61 73 68 44 61 74 61 28 45 29 20 20 20 28 28 HashData(E) ((
46d60 45 29 2d 3e 64 61 74 61 29 0a 2f 2a 20 23 64 65 E)->data)./* #de
46d70 66 69 6e 65 20 73 71 6c 69 74 65 48 61 73 68 4b fine sqliteHashK
46d80 65 79 28 45 29 20 20 20 20 28 28 45 29 2d 3e 70 ey(E) ((E)->p
46d90 4b 65 79 29 20 2f 2f 20 4e 4f 54 20 55 53 45 44 Key) // NOT USED
46da0 20 2a 2f 0a 2f 2a 20 23 64 65 66 69 6e 65 20 73 */./* #define s
46db0 71 6c 69 74 65 48 61 73 68 4b 65 79 73 69 7a 65 qliteHashKeysize
46dc0 28 45 29 20 28 28 45 29 2d 3e 6e 4b 65 79 29 20 (E) ((E)->nKey)
46dd0 20 2f 2f 20 4e 4f 54 20 55 53 45 44 20 2a 2f 0a // NOT USED */.
46de0 0a 2f 2a 0a 2a 2a 20 4e 75 6d 62 65 72 20 6f 66 ./*.** Number of
46df0 20 65 6e 74 72 69 65 73 20 69 6e 20 61 20 68 61 entries in a ha
46e00 73 68 20 74 61 62 6c 65 0a 2a 2f 0a 2f 2a 20 23 sh table.*/./* #
46e10 64 65 66 69 6e 65 20 73 71 6c 69 74 65 48 61 73 define sqliteHas
46e20 68 43 6f 75 6e 74 28 48 29 20 20 28 28 48 29 2d hCount(H) ((H)-
46e30 3e 63 6f 75 6e 74 29 20 2f 2f 20 4e 4f 54 20 55 >count) // NOT U
46e40 53 45 44 20 2a 2f 0a 0a 23 65 6e 64 69 66 20 2f SED */..#endif /
46e50 2a 20 5f 53 51 4c 49 54 45 5f 48 41 53 48 5f 48 * _SQLITE_HASH_H
46e60 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a _ */../*********
46e70 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 68 61 73 ***** End of has
46e80 68 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a h.h ************
46e90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
46ea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
46eb0 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
46ec0 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 ***** Continuing
46ed0 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f where we left o
46ee0 66 66 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e ff in sqliteInt.
46ef0 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a h **************
46f00 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
46f10 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 70 61 ***** Include pa
46f20 72 73 65 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 rse.h in the mid
46f30 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 dle of sqliteInt
46f40 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .h *************
46f50 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
46f60 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
46f70 20 70 61 72 73 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a parse.h *******
46f80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
46f90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
46fa0 2a 2a 2a 2a 2f 0a 23 64 65 66 69 6e 65 20 54 4b ****/.#define TK
46fb0 5f 53 45 4d 49 20 20 20 20 20 20 20 20 20 20 20 _SEMI
46fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
46fd0 20 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 58 1.#define TK_EX
46fe0 50 4c 41 49 4e 20 20 20 20 20 20 20 20 20 20 20 PLAIN
46ff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 0a 2.
47000 23 64 65 66 69 6e 65 20 54 4b 5f 51 55 45 52 59 #define TK_QUERY
47010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47020 20 20 20 20 20 20 20 20 20 20 20 33 0a 23 64 65 3.#de
47030 66 69 6e 65 20 54 4b 5f 50 4c 41 4e 20 20 20 20 fine TK_PLAN
47040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47050 20 20 20 20 20 20 20 20 34 0a 23 64 65 66 69 6e 4.#defin
47060 65 20 54 4b 5f 42 45 47 49 4e 20 20 20 20 20 20 e TK_BEGIN
47070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47080 20 20 20 20 20 35 0a 23 64 65 66 69 6e 65 20 54 5.#define T
47090 4b 5f 54 52 41 4e 53 41 43 54 49 4f 4e 20 20 20 K_TRANSACTION
470a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
470b0 20 20 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44 6.#define TK_D
470c0 45 46 45 52 52 45 44 20 20 20 20 20 20 20 20 20 EFERRED
470d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 7
470e0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4d 4d 45 .#define TK_IMME
470f0 44 49 41 54 45 20 20 20 20 20 20 20 20 20 20 20 DIATE
47100 20 20 20 20 20 20 20 20 20 20 20 20 38 0a 23 64 8.#d
47110 65 66 69 6e 65 20 54 4b 5f 45 58 43 4c 55 53 49 efine TK_EXCLUSI
47120 56 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 VE
47130 20 20 20 20 20 20 20 20 20 39 0a 23 64 65 66 69 9.#defi
47140 6e 65 20 54 4b 5f 43 4f 4d 4d 49 54 20 20 20 20 ne TK_COMMIT
47150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47160 20 20 20 20 20 31 30 0a 23 64 65 66 69 6e 65 20 10.#define
47170 54 4b 5f 45 4e 44 20 20 20 20 20 20 20 20 20 20 TK_END
47180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47190 20 20 31 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 11.#define TK_
471a0 52 4f 4c 4c 42 41 43 4b 20 20 20 20 20 20 20 20 ROLLBACK
471b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
471c0 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53 41 56 2.#define TK_SAV
471d0 45 50 4f 49 4e 54 20 20 20 20 20 20 20 20 20 20 EPOINT
471e0 20 20 20 20 20 20 20 20 20 20 20 20 31 33 0a 23 13.#
471f0 64 65 66 69 6e 65 20 54 4b 5f 52 45 4c 45 41 53 define TK_RELEAS
47200 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 E
47210 20 20 20 20 20 20 20 20 20 31 34 0a 23 64 65 66 14.#def
47220 69 6e 65 20 54 4b 5f 54 4f 20 20 20 20 20 20 20 ine TK_TO
47230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47240 20 20 20 20 20 20 31 35 0a 23 64 65 66 69 6e 65 15.#define
47250 20 54 4b 5f 54 41 42 4c 45 20 20 20 20 20 20 20 TK_TABLE
47260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47270 20 20 20 31 36 0a 23 64 65 66 69 6e 65 20 54 4b 16.#define TK
47280 5f 43 52 45 41 54 45 20 20 20 20 20 20 20 20 20 _CREATE
47290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
472a0 31 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 46 17.#define TK_IF
472b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
472c0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 38 0a 18.
472d0 23 64 65 66 69 6e 65 20 54 4b 5f 4e 4f 54 20 20 #define TK_NOT
472e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
472f0 20 20 20 20 20 20 20 20 20 20 31 39 0a 23 64 65 19.#de
47300 66 69 6e 65 20 54 4b 5f 45 58 49 53 54 53 20 20 fine TK_EXISTS
47310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47320 20 20 20 20 20 20 20 32 30 0a 23 64 65 66 69 6e 20.#defin
47330 65 20 54 4b 5f 54 45 4d 50 20 20 20 20 20 20 20 e TK_TEMP
47340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47350 20 20 20 20 32 31 0a 23 64 65 66 69 6e 65 20 54 21.#define T
47360 4b 5f 4c 50 20 20 20 20 20 20 20 20 20 20 20 20 K_LP
47370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47380 20 32 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 22.#define TK_R
47390 50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 P
473a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 33 23
473b0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 53 20 20 .#define TK_AS
473c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
473d0 20 20 20 20 20 20 20 20 20 20 20 32 34 0a 23 64 24.#d
473e0 65 66 69 6e 65 20 54 4b 5f 43 4f 4d 4d 41 20 20 efine TK_COMMA
473f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47400 20 20 20 20 20 20 20 20 32 35 0a 23 64 65 66 69 25.#defi
47410 6e 65 20 54 4b 5f 49 44 20 20 20 20 20 20 20 20 ne TK_ID
47420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47430 20 20 20 20 20 32 36 0a 23 64 65 66 69 6e 65 20 26.#define
47440 54 4b 5f 49 4e 44 45 58 45 44 20 20 20 20 20 20 TK_INDEXED
47450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47460 20 20 32 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 27.#define TK_
47470 41 42 4f 52 54 20 20 20 20 20 20 20 20 20 20 20 ABORT
47480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 2
47490 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 43 54 8.#define TK_ACT
474a0 49 4f 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 ION
474b0 20 20 20 20 20 20 20 20 20 20 20 20 32 39 0a 23 29.#
474c0 64 65 66 69 6e 65 20 54 4b 5f 41 46 54 45 52 20 define TK_AFTER
474d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
474e0 20 20 20 20 20 20 20 20 20 33 30 0a 23 64 65 66 30.#def
474f0 69 6e 65 20 54 4b 5f 41 4e 41 4c 59 5a 45 20 20 ine TK_ANALYZE
47500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47510 20 20 20 20 20 20 33 31 0a 23 64 65 66 69 6e 65 31.#define
47520 20 54 4b 5f 41 53 43 20 20 20 20 20 20 20 20 20 TK_ASC
47530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47540 20 20 20 33 32 0a 23 64 65 66 69 6e 65 20 54 4b 32.#define TK
47550 5f 41 54 54 41 43 48 20 20 20 20 20 20 20 20 20 _ATTACH
47560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47570 33 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 42 45 33.#define TK_BE
47580 46 4f 52 45 20 20 20 20 20 20 20 20 20 20 20 20 FORE
47590 20 20 20 20 20 20 20 20 20 20 20 20 20 33 34 0a 34.
475a0 23 64 65 66 69 6e 65 20 54 4b 5f 42 59 20 20 20 #define TK_BY
475b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
475c0 20 20 20 20 20 20 20 20 20 20 33 35 0a 23 64 65 35.#de
475d0 66 69 6e 65 20 54 4b 5f 43 41 53 43 41 44 45 20 fine TK_CASCADE
475e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
475f0 20 20 20 20 20 20 20 33 36 0a 23 64 65 66 69 6e 36.#defin
47600 65 20 54 4b 5f 43 41 53 54 20 20 20 20 20 20 20 e TK_CAST
47610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47620 20 20 20 20 33 37 0a 23 64 65 66 69 6e 65 20 54 37.#define T
47630 4b 5f 43 4f 4c 55 4d 4e 4b 57 20 20 20 20 20 20 K_COLUMNKW
47640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47650 20 33 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 38.#define TK_C
47660 4f 4e 46 4c 49 43 54 20 20 20 20 20 20 20 20 20 ONFLICT
47670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33 39 39
47680 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44 41 54 41 .#define TK_DATA
47690 42 41 53 45 20 20 20 20 20 20 20 20 20 20 20 20 BASE
476a0 20 20 20 20 20 20 20 20 20 20 20 34 30 0a 23 64 40.#d
476b0 65 66 69 6e 65 20 54 4b 5f 44 45 53 43 20 20 20 efine TK_DESC
476c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
476d0 20 20 20 20 20 20 20 20 34 31 0a 23 64 65 66 69 41.#defi
476e0 6e 65 20 54 4b 5f 44 45 54 41 43 48 20 20 20 20 ne TK_DETACH
476f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47700 20 20 20 20 20 34 32 0a 23 64 65 66 69 6e 65 20 42.#define
47710 54 4b 5f 45 41 43 48 20 20 20 20 20 20 20 20 20 TK_EACH
47720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47730 20 20 34 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43.#define TK_
47740 46 41 49 4c 20 20 20 20 20 20 20 20 20 20 20 20 FAIL
47750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 4
47760 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 46 4f 52 4.#define TK_FOR
47770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47780 20 20 20 20 20 20 20 20 20 20 20 20 34 35 0a 23 45.#
47790 64 65 66 69 6e 65 20 54 4b 5f 49 47 4e 4f 52 45 define TK_IGNORE
477a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
477b0 20 20 20 20 20 20 20 20 20 34 36 0a 23 64 65 66 46.#def
477c0 69 6e 65 20 54 4b 5f 49 4e 49 54 49 41 4c 4c 59 ine TK_INITIALLY
477d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
477e0 20 20 20 20 20 20 34 37 0a 23 64 65 66 69 6e 65 47.#define
477f0 20 54 4b 5f 49 4e 53 54 45 41 44 20 20 20 20 20 TK_INSTEAD
47800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47810 20 20 20 34 38 0a 23 64 65 66 69 6e 65 20 54 4b 48.#define TK
47820 5f 4c 49 4b 45 5f 4b 57 20 20 20 20 20 20 20 20 _LIKE_KW
47830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47840 34 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4d 41 49.#define TK_MA
47850 54 43 48 20 20 20 20 20 20 20 20 20 20 20 20 20 TCH
47860 20 20 20 20 20 20 20 20 20 20 20 20 20 35 30 0a 50.
47870 23 64 65 66 69 6e 65 20 54 4b 5f 4e 4f 20 20 20 #define TK_NO
47880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47890 20 20 20 20 20 20 20 20 20 20 35 31 0a 23 64 65 51.#de
478a0 66 69 6e 65 20 54 4b 5f 4b 45 59 20 20 20 20 20 fine TK_KEY
478b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
478c0 20 20 20 20 20 20 20 35 32 0a 23 64 65 66 69 6e 52.#defin
478d0 65 20 54 4b 5f 4f 46 20 20 20 20 20 20 20 20 20 e TK_OF
478e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
478f0 20 20 20 20 35 33 0a 23 64 65 66 69 6e 65 20 54 53.#define T
47900 4b 5f 4f 46 46 53 45 54 20 20 20 20 20 20 20 20 K_OFFSET
47910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47920 20 35 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 50 54.#define TK_P
47930 52 41 47 4d 41 20 20 20 20 20 20 20 20 20 20 20 RAGMA
47940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 35 55
47950 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 41 49 53 .#define TK_RAIS
47960 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 E
47970 20 20 20 20 20 20 20 20 20 20 20 35 36 0a 23 64 56.#d
47980 65 66 69 6e 65 20 54 4b 5f 52 45 50 4c 41 43 45 efine TK_REPLACE
47990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
479a0 20 20 20 20 20 20 20 20 35 37 0a 23 64 65 66 69 57.#defi
479b0 6e 65 20 54 4b 5f 52 45 53 54 52 49 43 54 20 20 ne TK_RESTRICT
479c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
479d0 20 20 20 20 20 35 38 0a 23 64 65 66 69 6e 65 20 58.#define
479e0 54 4b 5f 52 4f 57 20 20 20 20 20 20 20 20 20 20 TK_ROW
479f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47a00 20 20 35 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 59.#define TK_
47a10 54 52 49 47 47 45 52 20 20 20 20 20 20 20 20 20 TRIGGER
47a20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36 6
47a30 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 56 41 43 0.#define TK_VAC
47a40 55 55 4d 20 20 20 20 20 20 20 20 20 20 20 20 20 UUM
47a50 20 20 20 20 20 20 20 20 20 20 20 20 36 31 0a 23 61.#
47a60 64 65 66 69 6e 65 20 54 4b 5f 56 49 45 57 20 20 define TK_VIEW
47a70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47a80 20 20 20 20 20 20 20 20 20 36 32 0a 23 64 65 66 62.#def
47a90 69 6e 65 20 54 4b 5f 56 49 52 54 55 41 4c 20 20 ine TK_VIRTUAL
47aa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47ab0 20 20 20 20 20 20 36 33 0a 23 64 65 66 69 6e 65 63.#define
47ac0 20 54 4b 5f 52 45 49 4e 44 45 58 20 20 20 20 20 TK_REINDEX
47ad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47ae0 20 20 20 36 34 0a 23 64 65 66 69 6e 65 20 54 4b 64.#define TK
47af0 5f 52 45 4e 41 4d 45 20 20 20 20 20 20 20 20 20 _RENAME
47b00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47b10 36 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 54 65.#define TK_CT
47b20 49 4d 45 5f 4b 57 20 20 20 20 20 20 20 20 20 20 IME_KW
47b30 20 20 20 20 20 20 20 20 20 20 20 20 20 36 36 0a 66.
47b40 23 64 65 66 69 6e 65 20 54 4b 5f 41 4e 59 20 20 #define TK_ANY
47b50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47b60 20 20 20 20 20 20 20 20 20 20 36 37 0a 23 64 65 67.#de
47b70 66 69 6e 65 20 54 4b 5f 4f 52 20 20 20 20 20 20 fine TK_OR
47b80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47b90 20 20 20 20 20 20 20 36 38 0a 23 64 65 66 69 6e 68.#defin
47ba0 65 20 54 4b 5f 41 4e 44 20 20 20 20 20 20 20 20 e TK_AND
47bb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47bc0 20 20 20 20 36 39 0a 23 64 65 66 69 6e 65 20 54 69.#define T
47bd0 4b 5f 49 53 20 20 20 20 20 20 20 20 20 20 20 20 K_IS
47be0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47bf0 20 37 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 42 70.#define TK_B
47c00 45 54 57 45 45 4e 20 20 20 20 20 20 20 20 20 20 ETWEEN
47c10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 31 71
47c20 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e 20 20 .#define TK_IN
47c30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47c40 20 20 20 20 20 20 20 20 20 20 20 37 32 0a 23 64 72.#d
47c50 65 66 69 6e 65 20 54 4b 5f 49 53 4e 55 4c 4c 20 efine TK_ISNULL
47c60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47c70 20 20 20 20 20 20 20 20 37 33 0a 23 64 65 66 69 73.#defi
47c80 6e 65 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 20 20 20 ne TK_NOTNULL
47c90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47ca0 20 20 20 20 20 37 34 0a 23 64 65 66 69 6e 65 20 74.#define
47cb0 54 4b 5f 4e 45 20 20 20 20 20 20 20 20 20 20 20 TK_NE
47cc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47cd0 20 20 37 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 75.#define TK_
47ce0 45 51 20 20 20 20 20 20 20 20 20 20 20 20 20 20 EQ
47cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 7
47d00 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 47 54 20 6.#define TK_GT
47d10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47d20 20 20 20 20 20 20 20 20 20 20 20 20 37 37 0a 23 77.#
47d30 64 65 66 69 6e 65 20 54 4b 5f 4c 45 20 20 20 20 define TK_LE
47d40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47d50 20 20 20 20 20 20 20 20 20 37 38 0a 23 64 65 66 78.#def
47d60 69 6e 65 20 54 4b 5f 4c 54 20 20 20 20 20 20 20 ine TK_LT
47d70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47d80 20 20 20 20 20 20 37 39 0a 23 64 65 66 69 6e 65 79.#define
47d90 20 54 4b 5f 47 45 20 20 20 20 20 20 20 20 20 20 TK_GE
47da0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47db0 20 20 20 38 30 0a 23 64 65 66 69 6e 65 20 54 4b 80.#define TK
47dc0 5f 45 53 43 41 50 45 20 20 20 20 20 20 20 20 20 _ESCAPE
47dd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47de0 38 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 42 49 81.#define TK_BI
47df0 54 41 4e 44 20 20 20 20 20 20 20 20 20 20 20 20 TAND
47e00 20 20 20 20 20 20 20 20 20 20 20 20 20 38 32 0a 82.
47e10 23 64 65 66 69 6e 65 20 54 4b 5f 42 49 54 4f 52 #define TK_BITOR
47e20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47e30 20 20 20 20 20 20 20 20 20 20 38 33 0a 23 64 65 83.#de
47e40 66 69 6e 65 20 54 4b 5f 4c 53 48 49 46 54 20 20 fine TK_LSHIFT
47e50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47e60 20 20 20 20 20 20 20 38 34 0a 23 64 65 66 69 6e 84.#defin
47e70 65 20 54 4b 5f 52 53 48 49 46 54 20 20 20 20 20 e TK_RSHIFT
47e80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47e90 20 20 20 20 38 35 0a 23 64 65 66 69 6e 65 20 54 85.#define T
47ea0 4b 5f 50 4c 55 53 20 20 20 20 20 20 20 20 20 20 K_PLUS
47eb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47ec0 20 38 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4d 86.#define TK_M
47ed0 49 4e 55 53 20 20 20 20 20 20 20 20 20 20 20 20 INUS
47ee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 37 87
47ef0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53 54 41 52 .#define TK_STAR
47f00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47f10 20 20 20 20 20 20 20 20 20 20 20 38 38 0a 23 64 88.#d
47f20 65 66 69 6e 65 20 54 4b 5f 53 4c 41 53 48 20 20 efine TK_SLASH
47f30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47f40 20 20 20 20 20 20 20 20 38 39 0a 23 64 65 66 69 89.#defi
47f50 6e 65 20 54 4b 5f 52 45 4d 20 20 20 20 20 20 20 ne TK_REM
47f60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47f70 20 20 20 20 20 39 30 0a 23 64 65 66 69 6e 65 20 90.#define
47f80 54 4b 5f 43 4f 4e 43 41 54 20 20 20 20 20 20 20 TK_CONCAT
47f90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47fa0 20 20 39 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 91.#define TK_
47fb0 43 4f 4c 4c 41 54 45 20 20 20 20 20 20 20 20 20 COLLATE
47fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39 9
47fd0 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 42 49 54 2.#define TK_BIT
47fe0 4e 4f 54 20 20 20 20 20 20 20 20 20 20 20 20 20 NOT
47ff0 20 20 20 20 20 20 20 20 20 20 20 20 39 33 0a 23 93.#
48000 64 65 66 69 6e 65 20 54 4b 5f 53 54 52 49 4e 47 define TK_STRING
48010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48020 20 20 20 20 20 20 20 20 20 39 34 0a 23 64 65 66 94.#def
48030 69 6e 65 20 54 4b 5f 4a 4f 49 4e 5f 4b 57 20 20 ine TK_JOIN_KW
48040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48050 20 20 20 20 20 20 39 35 0a 23 64 65 66 69 6e 65 95.#define
48060 20 54 4b 5f 43 4f 4e 53 54 52 41 49 4e 54 20 20 TK_CONSTRAINT
48070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48080 20 20 20 39 36 0a 23 64 65 66 69 6e 65 20 54 4b 96.#define TK
48090 5f 44 45 46 41 55 4c 54 20 20 20 20 20 20 20 20 _DEFAULT
480a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
480b0 39 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4e 55 97.#define TK_NU
480c0 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 LL
480d0 20 20 20 20 20 20 20 20 20 20 20 20 20 39 38 0a 98.
480e0 23 64 65 66 69 6e 65 20 54 4b 5f 50 52 49 4d 41 #define TK_PRIMA
480f0 52 59 20 20 20 20 20 20 20 20 20 20 20 20 20 20 RY
48100 20 20 20 20 20 20 20 20 20 20 39 39 0a 23 64 65 99.#de
48110 66 69 6e 65 20 54 4b 5f 55 4e 49 51 55 45 20 20 fine TK_UNIQUE
48120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48130 20 20 20 20 20 20 20 31 30 30 0a 23 64 65 66 69 100.#defi
48140 6e 65 20 54 4b 5f 43 48 45 43 4b 20 20 20 20 20 ne TK_CHECK
48150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48160 20 20 20 20 20 31 30 31 0a 23 64 65 66 69 6e 65 101.#define
48170 20 54 4b 5f 52 45 46 45 52 45 4e 43 45 53 20 20 TK_REFERENCES
48180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48190 20 20 20 31 30 32 0a 23 64 65 66 69 6e 65 20 54 102.#define T
481a0 4b 5f 41 55 54 4f 49 4e 43 52 20 20 20 20 20 20 K_AUTOINCR
481b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
481c0 20 31 30 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 103.#define TK_
481d0 4f 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ON
481e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
481f0 30 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44 45 04.#define TK_DE
48200 4c 45 54 45 20 20 20 20 20 20 20 20 20 20 20 20 LETE
48210 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 35 105
48220 0a 23 64 65 66 69 6e 65 20 54 4b 5f 55 50 44 41 .#define TK_UPDA
48230 54 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 TE
48240 20 20 20 20 20 20 20 20 20 20 20 31 30 36 0a 23 106.#
48250 64 65 66 69 6e 65 20 54 4b 5f 53 45 54 20 20 20 define TK_SET
48260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48270 20 20 20 20 20 20 20 20 20 31 30 37 0a 23 64 65 107.#de
48280 66 69 6e 65 20 54 4b 5f 44 45 46 45 52 52 41 42 fine TK_DEFERRAB
48290 4c 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 LE
482a0 20 20 20 20 20 20 20 31 30 38 0a 23 64 65 66 69 108.#defi
482b0 6e 65 20 54 4b 5f 46 4f 52 45 49 47 4e 20 20 20 ne TK_FOREIGN
482c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
482d0 20 20 20 20 20 31 30 39 0a 23 64 65 66 69 6e 65 109.#define
482e0 20 54 4b 5f 44 52 4f 50 20 20 20 20 20 20 20 20 TK_DROP
482f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48300 20 20 20 31 31 30 0a 23 64 65 66 69 6e 65 20 54 110.#define T
48310 4b 5f 55 4e 49 4f 4e 20 20 20 20 20 20 20 20 20 K_UNION
48320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48330 20 31 31 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 111.#define TK_
48340 41 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 20 ALL
48350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
48360 31 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 58 12.#define TK_EX
48370 43 45 50 54 20 20 20 20 20 20 20 20 20 20 20 20 CEPT
48380 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 33 113
48390 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e 54 45 .#define TK_INTE
483a0 52 53 45 43 54 20 20 20 20 20 20 20 20 20 20 20 RSECT
483b0 20 20 20 20 20 20 20 20 20 20 20 31 31 34 0a 23 114.#
483c0 64 65 66 69 6e 65 20 54 4b 5f 53 45 4c 45 43 54 define TK_SELECT
483d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
483e0 20 20 20 20 20 20 20 20 20 31 31 35 0a 23 64 65 115.#de
483f0 66 69 6e 65 20 54 4b 5f 44 49 53 54 49 4e 43 54 fine TK_DISTINCT
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 31 31 36 0a 23 64 65 66 69 116.#defi
48420 6e 65 20 54 4b 5f 44 4f 54 20 20 20 20 20 20 20 ne TK_DOT
48430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48440 20 20 20 20 20 31 31 37 0a 23 64 65 66 69 6e 65 117.#define
48450 20 54 4b 5f 46 52 4f 4d 20 20 20 20 20 20 20 20 TK_FROM
48460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48470 20 20 20 31 31 38 0a 23 64 65 66 69 6e 65 20 54 118.#define T
48480 4b 5f 4a 4f 49 4e 20 20 20 20 20 20 20 20 20 20 K_JOIN
48490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
484a0 20 31 31 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 119.#define TK_
484b0 55 53 49 4e 47 20 20 20 20 20 20 20 20 20 20 20 USING
484c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
484d0 32 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4f 52 20.#define TK_OR
484e0 44 45 52 20 20 20 20 20 20 20 20 20 20 20 20 20 DER
484f0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 31 121
48500 0a 23 64 65 66 69 6e 65 20 54 4b 5f 47 52 4f 55 .#define TK_GROU
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 31 32 32 0a 23 122.#
48530 64 65 66 69 6e 65 20 54 4b 5f 48 41 56 49 4e 47 define TK_HAVING
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 31 32 33 0a 23 64 65 123.#de
48560 66 69 6e 65 20 54 4b 5f 4c 49 4d 49 54 20 20 20 fine TK_LIMIT
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 31 32 34 0a 23 64 65 66 69 124.#defi
48590 6e 65 20 54 4b 5f 57 48 45 52 45 20 20 20 20 20 ne TK_WHERE
485a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
485b0 20 20 20 20 20 31 32 35 0a 23 64 65 66 69 6e 65 125.#define
485c0 20 54 4b 5f 49 4e 54 4f 20 20 20 20 20 20 20 20 TK_INTO
485d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
485e0 20 20 20 31 32 36 0a 23 64 65 66 69 6e 65 20 54 126.#define T
485f0 4b 5f 56 41 4c 55 45 53 20 20 20 20 20 20 20 20 K_VALUES
48600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48610 20 31 32 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 127.#define TK_
48620 49 4e 53 45 52 54 20 20 20 20 20 20 20 20 20 20 INSERT
48630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
48640 32 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e 28.#define TK_IN
48650 54 45 47 45 52 20 20 20 20 20 20 20 20 20 20 20 TEGER
48660 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 39 129
48670 0a 23 64 65 66 69 6e 65 20 54 4b 5f 46 4c 4f 41 .#define TK_FLOA
48680 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 T
48690 20 20 20 20 20 20 20 20 20 20 20 31 33 30 0a 23 130.#
486a0 64 65 66 69 6e 65 20 54 4b 5f 42 4c 4f 42 20 20 define TK_BLOB
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 31 33 31 0a 23 64 65 131.#de
486d0 66 69 6e 65 20 54 4b 5f 52 45 47 49 53 54 45 52 fine TK_REGISTER
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 31 33 32 0a 23 64 65 66 69 132.#defi
48700 6e 65 20 54 4b 5f 56 41 52 49 41 42 4c 45 20 20 ne TK_VARIABLE
48710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48720 20 20 20 20 20 31 33 33 0a 23 64 65 66 69 6e 65 133.#define
48730 20 54 4b 5f 43 41 53 45 20 20 20 20 20 20 20 20 TK_CASE
48740 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48750 20 20 20 31 33 34 0a 23 64 65 66 69 6e 65 20 54 134.#define T
48760 4b 5f 57 48 45 4e 20 20 20 20 20 20 20 20 20 20 K_WHEN
48770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48780 20 31 33 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 135.#define TK_
48790 54 48 45 4e 20 20 20 20 20 20 20 20 20 20 20 20 THEN
487a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
487b0 33 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 4c 36.#define TK_EL
487c0 53 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 SE
487d0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33 37 137
487e0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e 44 45 .#define TK_INDE
487f0 58 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 X
48800 20 20 20 20 20 20 20 20 20 20 20 31 33 38 0a 23 138.#
48810 64 65 66 69 6e 65 20 54 4b 5f 41 4c 54 45 52 20 define TK_ALTER
48820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48830 20 20 20 20 20 20 20 20 20 31 33 39 0a 23 64 65 139.#de
48840 66 69 6e 65 20 54 4b 5f 41 44 44 20 20 20 20 20 fine TK_ADD
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 31 34 30 0a 23 64 65 66 69 140.#defi
48870 6e 65 20 54 4b 5f 54 4f 5f 54 45 58 54 20 20 20 ne TK_TO_TEXT
48880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48890 20 20 20 20 20 31 34 31 0a 23 64 65 66 69 6e 65 141.#define
488a0 20 54 4b 5f 54 4f 5f 42 4c 4f 42 20 20 20 20 20 TK_TO_BLOB
488b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
488c0 20 20 20 31 34 32 0a 23 64 65 66 69 6e 65 20 54 142.#define T
488d0 4b 5f 54 4f 5f 4e 55 4d 45 52 49 43 20 20 20 20 K_TO_NUMERIC
488e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
488f0 20 31 34 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 143.#define TK_
48900 54 4f 5f 49 4e 54 20 20 20 20 20 20 20 20 20 20 TO_INT
48910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
48920 34 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 4f 44.#define TK_TO
48930 5f 52 45 41 4c 20 20 20 20 20 20 20 20 20 20 20 _REAL
48940 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34 35 145
48950 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 53 4e 4f .#define TK_ISNO
48960 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 T
48970 20 20 20 20 20 20 20 20 20 20 20 31 34 36 0a 23 146.#
48980 64 65 66 69 6e 65 20 54 4b 5f 45 4e 44 5f 4f 46 define TK_END_OF
48990 5f 46 49 4c 45 20 20 20 20 20 20 20 20 20 20 20 _FILE
489a0 20 20 20 20 20 20 20 20 20 31 34 37 0a 23 64 65 147.#de
489b0 66 69 6e 65 20 54 4b 5f 49 4c 4c 45 47 41 4c 20 fine TK_ILLEGAL
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 31 34 38 0a 23 64 65 66 69 148.#defi
489e0 6e 65 20 54 4b 5f 53 50 41 43 45 20 20 20 20 20 ne TK_SPACE
489f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48a00 20 20 20 20 20 31 34 39 0a 23 64 65 66 69 6e 65 149.#define
48a10 20 54 4b 5f 55 4e 43 4c 4f 53 45 44 5f 53 54 52 TK_UNCLOSED_STR
48a20 49 4e 47 20 20 20 20 20 20 20 20 20 20 20 20 20 ING
48a30 20 20 20 31 35 30 0a 23 64 65 66 69 6e 65 20 54 150.#define T
48a40 4b 5f 46 55 4e 43 54 49 4f 4e 20 20 20 20 20 20 K_FUNCTION
48a50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48a60 20 31 35 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 151.#define TK_
48a70 43 4f 4c 55 4d 4e 20 20 20 20 20 20 20 20 20 20 COLUMN
48a80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
48a90 35 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 47 52.#define TK_AG
48aa0 47 5f 46 55 4e 43 54 49 4f 4e 20 20 20 20 20 20 G_FUNCTION
48ab0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 35 33 153
48ac0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 47 47 5f .#define TK_AGG_
48ad0 43 4f 4c 55 4d 4e 20 20 20 20 20 20 20 20 20 20 COLUMN
48ae0 20 20 20 20 20 20 20 20 20 20 20 31 35 34 0a 23 154.#
48af0 64 65 66 69 6e 65 20 54 4b 5f 43 4f 4e 53 54 5f define TK_CONST_
48b00 46 55 4e 43 20 20 20 20 20 20 20 20 20 20 20 20 FUNC
48b10 20 20 20 20 20 20 20 20 20 31 35 35 0a 23 64 65 155.#de
48b20 66 69 6e 65 20 54 4b 5f 55 4d 49 4e 55 53 20 20 fine TK_UMINUS
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 31 35 36 0a 23 64 65 66 69 156.#defi
48b50 6e 65 20 54 4b 5f 55 50 4c 55 53 20 20 20 20 20 ne TK_UPLUS
48b60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48b70 20 20 20 20 20 31 35 37 0a 0a 2f 2a 2a 2a 2a 2a 157../*****
48b80 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 ********* End of
48b90 20 70 61 72 73 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a parse.h *******
48ba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
48bb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
48bc0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
48bd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e ********* Contin
48be0 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 uing where we le
48bf0 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 ft off in sqlite
48c00 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a Int.h **********
48c10 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 69 6e 63 6c 75 ********/.#inclu
48c20 64 65 20 3c 73 74 64 69 6f 2e 68 3e 0a 23 69 6e de <stdio.h>.#in
48c30 63 6c 75 64 65 20 3c 73 74 64 6c 69 62 2e 68 3e clude <stdlib.h>
48c40 0a 23 69 6e 63 6c 75 64 65 20 3c 73 74 72 69 6e .#include <strin
48c50 67 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 61 g.h>.#include <a
48c60 73 73 65 72 74 2e 68 3e 0a 23 69 6e 63 6c 75 64 ssert.h>.#includ
48c70 65 20 3c 73 74 64 64 65 66 2e 68 3e 0a 0a 2f 2a e <stddef.h>../*
48c80 0a 2a 2a 20 49 66 20 63 6f 6d 70 69 6c 69 6e 67 .** If compiling
48c90 20 66 6f 72 20 61 20 70 72 6f 63 65 73 73 6f 72 for a processor
48ca0 20 74 68 61 74 20 6c 61 63 6b 73 20 66 6c 6f 61 that lacks floa
48cb0 74 69 6e 67 20 70 6f 69 6e 74 20 73 75 70 70 6f ting point suppo
48cc0 72 74 2c 0a 2a 2a 20 73 75 62 73 74 69 74 75 74 rt,.** substitut
48cd0 65 20 69 6e 74 65 67 65 72 20 66 6f 72 20 66 6c e integer for fl
48ce0 6f 61 74 69 6e 67 2d 70 6f 69 6e 74 0a 2a 2f 0a oating-point.*/.
48cf0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d #ifdef SQLITE_OM
48d00 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e IT_FLOATING_POIN
48d10 54 0a 23 20 64 65 66 69 6e 65 20 64 6f 75 62 6c T.# define doubl
48d20 65 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 0a 23 e sqlite_int64.#
48d30 20 64 65 66 69 6e 65 20 4c 4f 4e 47 44 4f 55 42 define LONGDOUB
48d40 4c 45 5f 54 59 50 45 20 73 71 6c 69 74 65 5f 69 LE_TYPE sqlite_i
48d50 6e 74 36 34 0a 23 20 69 66 6e 64 65 66 20 53 51 nt64.# ifndef SQ
48d60 4c 49 54 45 5f 42 49 47 5f 44 42 4c 0a 23 20 20 LITE_BIG_DBL.#
48d70 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 42 define SQLITE_B
48d80 49 47 5f 44 42 4c 20 28 28 28 73 71 6c 69 74 65 IG_DBL (((sqlite
48d90 33 5f 69 6e 74 36 34 29 31 29 3c 3c 35 30 29 0a 3_int64)1)<<50).
48da0 23 20 65 6e 64 69 66 0a 23 20 64 65 66 69 6e 65 # endif.# define
48db0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 41 54 SQLITE_OMIT_DAT
48dc0 45 54 49 4d 45 5f 46 55 4e 43 53 20 31 0a 23 20 ETIME_FUNCS 1.#
48dd0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 4d define SQLITE_OM
48de0 49 54 5f 54 52 41 43 45 20 31 0a 23 20 75 6e 64 IT_TRACE 1.# und
48df0 65 66 20 53 51 4c 49 54 45 5f 4d 49 58 45 44 5f ef SQLITE_MIXED_
48e00 45 4e 44 49 41 4e 5f 36 34 42 49 54 5f 46 4c 4f ENDIAN_64BIT_FLO
48e10 41 54 0a 23 20 75 6e 64 65 66 20 53 51 4c 49 54 AT.# undef SQLIT
48e20 45 5f 48 41 56 45 5f 49 53 4e 41 4e 0a 23 65 6e E_HAVE_ISNAN.#en
48e30 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 dif.#ifndef SQLI
48e40 54 45 5f 42 49 47 5f 44 42 4c 0a 23 20 64 65 66 TE_BIG_DBL.# def
48e50 69 6e 65 20 53 51 4c 49 54 45 5f 42 49 47 5f 44 ine SQLITE_BIG_D
48e60 42 4c 20 28 31 65 39 39 29 0a 23 65 6e 64 69 66 BL (1e99).#endif
48e70 0a 0a 2f 2a 0a 2a 2a 20 4f 4d 49 54 5f 54 45 4d ../*.** OMIT_TEM
48e80 50 44 42 20 69 73 20 73 65 74 20 74 6f 20 31 20 PDB is set to 1
48e90 69 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 if SQLITE_OMIT_T
48ea0 45 4d 50 44 42 20 69 73 20 64 65 66 69 6e 65 64 EMPDB is defined
48eb0 2c 20 6f 72 20 30 0a 2a 2a 20 61 66 74 65 72 77 , or 0.** afterw
48ec0 61 72 64 2e 20 48 61 76 69 6e 67 20 74 68 69 73 ard. Having this
48ed0 20 6d 61 63 72 6f 20 61 6c 6c 6f 77 73 20 75 73 macro allows us
48ee0 20 74 6f 20 63 61 75 73 65 20 74 68 65 20 43 20 to cause the C
48ef0 63 6f 6d 70 69 6c 65 72 20 0a 2a 2a 20 74 6f 20 compiler .** to
48f00 6f 6d 69 74 20 63 6f 64 65 20 75 73 65 64 20 62 omit code used b
48f10 79 20 54 45 4d 50 20 74 61 62 6c 65 73 20 77 69 y TEMP tables wi
48f20 74 68 6f 75 74 20 6d 65 73 73 79 20 23 69 66 6e thout messy #ifn
48f30 64 65 66 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a def statements..
48f40 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
48f50 5f 4f 4d 49 54 5f 54 45 4d 50 44 42 0a 23 64 65 _OMIT_TEMPDB.#de
48f60 66 69 6e 65 20 4f 4d 49 54 5f 54 45 4d 50 44 42 fine OMIT_TEMPDB
48f70 20 31 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 1.#else.#define
48f80 20 4f 4d 49 54 5f 54 45 4d 50 44 42 20 30 0a 23 OMIT_TEMPDB 0.#
48f90 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 endif../*.** If
48fa0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 the following ma
48fb0 63 72 6f 20 69 73 20 73 65 74 20 74 6f 20 31 2c cro is set to 1,
48fc0 20 74 68 65 6e 20 4e 55 4c 4c 20 76 61 6c 75 65 then NULL value
48fd0 73 20 61 72 65 20 63 6f 6e 73 69 64 65 72 65 64 s are considered
48fe0 0a 2a 2a 20 64 69 73 74 69 6e 63 74 20 77 68 65 .** distinct whe
48ff0 6e 20 64 65 74 65 72 6d 69 6e 69 6e 67 20 77 68 n determining wh
49000 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 77 6f ether or not two
49010 20 65 6e 74 72 69 65 73 20 61 72 65 20 74 68 65 entries are the
49020 20 73 61 6d 65 0a 2a 2a 20 69 6e 20 61 20 55 4e same.** in a UN
49030 49 51 55 45 20 69 6e 64 65 78 2e 20 20 54 68 69 IQUE index. Thi
49040 73 20 69 73 20 74 68 65 20 77 61 79 20 50 6f 73 s is the way Pos
49050 74 67 72 65 53 51 4c 2c 20 4f 72 61 63 6c 65 2c tgreSQL, Oracle,
49060 20 44 42 32 2c 20 4d 79 53 51 4c 2c 0a 2a 2a 20 DB2, MySQL,.**
49070 4f 43 45 4c 4f 54 2c 20 61 6e 64 20 46 69 72 65 OCELOT, and Fire
49080 62 69 72 64 20 61 6c 6c 20 77 6f 72 6b 2e 20 20 bird all work.
49090 54 68 65 20 53 51 4c 39 32 20 73 70 65 63 20 65 The SQL92 spec e
490a0 78 70 6c 69 63 69 74 6c 79 20 73 61 79 73 20 74 xplicitly says t
490b0 68 69 73 0a 2a 2a 20 69 73 20 74 68 65 20 77 61 his.** is the wa
490c0 79 20 74 68 69 6e 67 73 20 61 72 65 20 73 75 70 y things are sup
490d0 70 6f 73 65 20 74 6f 20 77 6f 72 6b 2e 0a 2a 2a pose to work..**
490e0 0a 2a 2a 20 49 66 20 74 68 65 20 66 6f 6c 6c 6f .** If the follo
490f0 77 69 6e 67 20 6d 61 63 72 6f 20 69 73 20 73 65 wing macro is se
49100 74 20 74 6f 20 30 2c 20 74 68 65 20 4e 55 4c 4c t to 0, the NULL
49110 73 20 61 72 65 20 69 6e 64 69 73 74 69 6e 63 74 s are indistinct
49120 20 66 6f 72 0a 2a 2a 20 61 20 55 4e 49 51 55 45 for.** a UNIQUE
49130 20 69 6e 64 65 78 2e 20 20 49 6e 20 74 68 69 73 index. In this
49140 20 6d 6f 64 65 2c 20 79 6f 75 20 63 61 6e 20 6f mode, you can o
49150 6e 6c 79 20 68 61 76 65 20 61 20 73 69 6e 67 6c nly have a singl
49160 65 20 4e 55 4c 4c 20 65 6e 74 72 79 0a 2a 2a 20 e NULL entry.**
49170 66 6f 72 20 61 20 63 6f 6c 75 6d 6e 20 64 65 63 for a column dec
49180 6c 61 72 65 64 20 55 4e 49 51 55 45 2e 20 20 54 lared UNIQUE. T
49190 68 69 73 20 69 73 20 74 68 65 20 77 61 79 20 49 his is the way I
491a0 6e 66 6f 72 6d 69 78 20 61 6e 64 20 53 51 4c 20 nformix and SQL
491b0 53 65 72 76 65 72 0a 2a 2a 20 77 6f 72 6b 2e 0a Server.** work..
491c0 2a 2f 0a 23 64 65 66 69 6e 65 20 4e 55 4c 4c 5f */.#define NULL_
491d0 44 49 53 54 49 4e 43 54 5f 46 4f 52 5f 55 4e 49 DISTINCT_FOR_UNI
491e0 51 55 45 20 31 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 QUE 1../*.** The
491f0 20 22 66 69 6c 65 20 66 6f 72 6d 61 74 22 20 6e "file format" n
49200 75 6d 62 65 72 20 69 73 20 61 6e 20 69 6e 74 65 umber is an inte
49210 67 65 72 20 74 68 61 74 20 69 73 20 69 6e 63 72 ger that is incr
49220 65 6d 65 6e 74 65 64 20 77 68 65 6e 65 76 65 72 emented whenever
49230 0a 2a 2a 20 74 68 65 20 56 44 42 45 2d 6c 65 76 .** the VDBE-lev
49240 65 6c 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 63 el file format c
49250 68 61 6e 67 65 73 2e 20 20 54 68 65 20 66 6f 6c hanges. The fol
49260 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 73 20 64 65 lowing macros de
49270 66 69 6e 65 20 74 68 65 0a 2a 2a 20 74 68 65 20 fine the.** the
49280 64 65 66 61 75 6c 74 20 66 69 6c 65 20 66 6f 72 default file for
49290 6d 61 74 20 66 6f 72 20 6e 65 77 20 64 61 74 61 mat for new data
492a0 62 61 73 65 73 20 61 6e 64 20 74 68 65 20 6d 61 bases and the ma
492b0 78 69 6d 75 6d 20 66 69 6c 65 20 66 6f 72 6d 61 ximum file forma
492c0 74 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 6c 69 t.** that the li
492d0 62 72 61 72 79 20 63 61 6e 20 72 65 61 64 2e 0a brary can read..
492e0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
492f0 45 5f 4d 41 58 5f 46 49 4c 45 5f 46 4f 52 4d 41 E_MAX_FILE_FORMA
49300 54 20 34 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 T 4.#ifndef SQLI
49310 54 45 5f 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f TE_DEFAULT_FILE_
49320 46 4f 52 4d 41 54 0a 23 20 64 65 66 69 6e 65 20 FORMAT.# define
49330 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 46 SQLITE_DEFAULT_F
49340 49 4c 45 5f 46 4f 52 4d 41 54 20 31 0a 23 65 6e ILE_FORMAT 1.#en
49350 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c dif..#ifndef SQL
49360 49 54 45 5f 44 45 46 41 55 4c 54 5f 52 45 43 55 ITE_DEFAULT_RECU
49370 52 53 49 56 45 5f 54 52 49 47 47 45 52 53 0a 23 RSIVE_TRIGGERS.#
49380 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 define SQLITE_D
49390 45 46 41 55 4c 54 5f 52 45 43 55 52 53 49 56 45 EFAULT_RECURSIVE
493a0 5f 54 52 49 47 47 45 52 53 20 30 0a 23 65 6e 64 _TRIGGERS 0.#end
493b0 69 66 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 76 69 64 if../*.** Provid
493c0 65 20 61 20 64 65 66 61 75 6c 74 20 76 61 6c 75 e a default valu
493d0 65 20 66 6f 72 20 53 51 4c 49 54 45 5f 54 45 4d e for SQLITE_TEM
493e0 50 5f 53 54 4f 52 45 20 69 6e 20 63 61 73 65 20 P_STORE in case
493f0 69 74 20 69 73 20 6e 6f 74 20 73 70 65 63 69 66 it is not specif
49400 69 65 64 0a 2a 2a 20 6f 6e 20 74 68 65 20 63 6f ied.** on the co
49410 6d 6d 61 6e 64 2d 6c 69 6e 65 0a 2a 2f 0a 23 69 mmand-line.*/.#i
49420 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 54 45 4d fndef SQLITE_TEM
49430 50 5f 53 54 4f 52 45 0a 23 20 64 65 66 69 6e 65 P_STORE.# define
49440 20 53 51 4c 49 54 45 5f 54 45 4d 50 5f 53 54 4f SQLITE_TEMP_STO
49450 52 45 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a RE 1.#endif../*.
49460 2a 2a 20 47 43 43 20 64 6f 65 73 20 6e 6f 74 20 ** GCC does not
49470 64 65 66 69 6e 65 20 74 68 65 20 6f 66 66 73 65 define the offse
49480 74 6f 66 28 29 20 6d 61 63 72 6f 20 73 6f 20 77 tof() macro so w
49490 65 27 6c 6c 20 68 61 76 65 20 74 6f 20 64 6f 20 e'll have to do
494a0 69 74 0a 2a 2a 20 6f 75 72 73 65 6c 76 65 73 2e it.** ourselves.
494b0 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 6f 66 66 73 .*/.#ifndef offs
494c0 65 74 6f 66 0a 23 64 65 66 69 6e 65 20 6f 66 66 etof.#define off
494d0 73 65 74 6f 66 28 53 54 52 55 43 54 55 52 45 2c setof(STRUCTURE,
494e0 46 49 45 4c 44 29 20 28 28 69 6e 74 29 28 28 63 FIELD) ((int)((c
494f0 68 61 72 2a 29 26 28 28 53 54 52 55 43 54 55 52 har*)&((STRUCTUR
49500 45 2a 29 30 29 2d 3e 46 49 45 4c 44 29 29 0a 23 E*)0)->FIELD)).#
49510 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 endif../*.** Che
49520 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68 69 ck to see if thi
49530 73 20 6d 61 63 68 69 6e 65 20 75 73 65 73 20 45 s machine uses E
49540 42 43 44 49 43 2e 20 20 28 59 65 73 2c 20 62 65 BCDIC. (Yes, be
49550 6c 69 65 76 65 20 69 74 20 6f 72 0a 2a 2a 20 6e lieve it or.** n
49560 6f 74 2c 20 74 68 65 72 65 20 61 72 65 20 73 74 ot, there are st
49570 69 6c 6c 20 6d 61 63 68 69 6e 65 73 20 6f 75 74 ill machines out
49580 20 74 68 65 72 65 20 74 68 61 74 20 75 73 65 20 there that use
49590 45 42 43 44 49 43 2e 29 0a 2a 2f 0a 23 69 66 20 EBCDIC.).*/.#if
495a0 27 41 27 20 3d 3d 20 27 5c 33 30 31 27 0a 23 20 'A' == '\301'.#
495b0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 42 define SQLITE_EB
495c0 43 44 49 43 20 31 0a 23 65 6c 73 65 0a 23 20 64 CDIC 1.#else.# d
495d0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 53 43 efine SQLITE_ASC
495e0 49 49 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a II 1.#endif../*.
495f0 2a 2a 20 49 6e 74 65 67 65 72 73 20 6f 66 20 6b ** Integers of k
49600 6e 6f 77 6e 20 73 69 7a 65 73 2e 20 20 54 68 65 nown sizes. The
49610 73 65 20 74 79 70 65 64 65 66 73 20 6d 69 67 68 se typedefs migh
49620 74 20 63 68 61 6e 67 65 20 66 6f 72 20 61 72 63 t change for arc
49630 68 69 74 65 63 74 75 72 65 73 0a 2a 2a 20 77 68 hitectures.** wh
49640 65 72 65 20 74 68 65 20 73 69 7a 65 73 20 76 65 ere the sizes ve
49650 72 79 2e 20 20 50 72 65 70 72 6f 63 65 73 73 6f ry. Preprocesso
49660 72 20 6d 61 63 72 6f 73 20 61 72 65 20 61 76 61 r macros are ava
49670 69 6c 61 62 6c 65 20 73 6f 20 74 68 61 74 20 74 ilable so that t
49680 68 65 0a 2a 2a 20 74 79 70 65 73 20 63 61 6e 20 he.** types can
49690 62 65 20 63 6f 6e 76 65 6e 69 65 6e 74 6c 79 20 be conveniently
496a0 72 65 64 65 66 69 6e 65 64 20 61 74 20 63 6f 6d redefined at com
496b0 70 69 6c 65 2d 74 79 70 65 2e 20 20 4c 69 6b 65 pile-type. Like
496c0 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 this:.**.**
496d0 20 20 20 20 20 63 63 20 27 2d 44 55 49 4e 54 50 cc '-DUINTP
496e0 54 52 5f 54 59 50 45 3d 6c 6f 6e 67 20 6c 6f 6e TR_TYPE=long lon
496f0 67 20 69 6e 74 27 20 2e 2e 2e 0a 2a 2f 0a 23 69 g int' ....*/.#i
49700 66 6e 64 65 66 20 55 49 4e 54 33 32 5f 54 59 50 fndef UINT32_TYP
49710 45 0a 23 20 69 66 64 65 66 20 48 41 56 45 5f 55 E.# ifdef HAVE_U
49720 49 4e 54 33 32 5f 54 0a 23 20 20 64 65 66 69 6e INT32_T.# defin
49730 65 20 55 49 4e 54 33 32 5f 54 59 50 45 20 75 69 e UINT32_TYPE ui
49740 6e 74 33 32 5f 74 0a 23 20 65 6c 73 65 0a 23 20 nt32_t.# else.#
49750 20 64 65 66 69 6e 65 20 55 49 4e 54 33 32 5f 54 define UINT32_T
49760 59 50 45 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 YPE unsigned int
49770 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a .# endif.#endif.
49780 23 69 66 6e 64 65 66 20 55 49 4e 54 31 36 5f 54 #ifndef UINT16_T
49790 59 50 45 0a 23 20 69 66 64 65 66 20 48 41 56 45 YPE.# ifdef HAVE
497a0 5f 55 49 4e 54 31 36 5f 54 0a 23 20 20 64 65 66 _UINT16_T.# def
497b0 69 6e 65 20 55 49 4e 54 31 36 5f 54 59 50 45 20 ine UINT16_TYPE
497c0 75 69 6e 74 31 36 5f 74 0a 23 20 65 6c 73 65 0a uint16_t.# else.
497d0 23 20 20 64 65 66 69 6e 65 20 55 49 4e 54 31 36 # define UINT16
497e0 5f 54 59 50 45 20 75 6e 73 69 67 6e 65 64 20 73 _TYPE unsigned s
497f0 68 6f 72 74 20 69 6e 74 0a 23 20 65 6e 64 69 66 hort int.# endif
49800 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 .#endif.#ifndef
49810 49 4e 54 31 36 5f 54 59 50 45 0a 23 20 69 66 64 INT16_TYPE.# ifd
49820 65 66 20 48 41 56 45 5f 49 4e 54 31 36 5f 54 0a ef HAVE_INT16_T.
49830 23 20 20 64 65 66 69 6e 65 20 49 4e 54 31 36 5f # define INT16_
49840 54 59 50 45 20 69 6e 74 31 36 5f 74 0a 23 20 65 TYPE int16_t.# e
49850 6c 73 65 0a 23 20 20 64 65 66 69 6e 65 20 49 4e lse.# define IN
49860 54 31 36 5f 54 59 50 45 20 73 68 6f 72 74 20 69 T16_TYPE short i
49870 6e 74 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 nt.# endif.#endi
49880 66 0a 23 69 66 6e 64 65 66 20 55 49 4e 54 38 5f f.#ifndef UINT8_
49890 54 59 50 45 0a 23 20 69 66 64 65 66 20 48 41 56 TYPE.# ifdef HAV
498a0 45 5f 55 49 4e 54 38 5f 54 0a 23 20 20 64 65 66 E_UINT8_T.# def
498b0 69 6e 65 20 55 49 4e 54 38 5f 54 59 50 45 20 75 ine UINT8_TYPE u
498c0 69 6e 74 38 5f 74 0a 23 20 65 6c 73 65 0a 23 20 int8_t.# else.#
498d0 20 64 65 66 69 6e 65 20 55 49 4e 54 38 5f 54 59 define UINT8_TY
498e0 50 45 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 PE unsigned char
498f0 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a .# endif.#endif.
49900 23 69 66 6e 64 65 66 20 49 4e 54 38 5f 54 59 50 #ifndef INT8_TYP
49910 45 0a 23 20 69 66 64 65 66 20 48 41 56 45 5f 49 E.# ifdef HAVE_I
49920 4e 54 38 5f 54 0a 23 20 20 64 65 66 69 6e 65 20 NT8_T.# define
49930 49 4e 54 38 5f 54 59 50 45 20 69 6e 74 38 5f 74 INT8_TYPE int8_t
49940 0a 23 20 65 6c 73 65 0a 23 20 20 64 65 66 69 6e .# else.# defin
49950 65 20 49 4e 54 38 5f 54 59 50 45 20 73 69 67 6e e INT8_TYPE sign
49960 65 64 20 63 68 61 72 0a 23 20 65 6e 64 69 66 0a ed char.# endif.
49970 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 4c #endif.#ifndef L
49980 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 50 45 0a 23 ONGDOUBLE_TYPE.#
49990 20 64 65 66 69 6e 65 20 4c 4f 4e 47 44 4f 55 42 define LONGDOUB
499a0 4c 45 5f 54 59 50 45 20 6c 6f 6e 67 20 64 6f 75 LE_TYPE long dou
499b0 62 6c 65 0a 23 65 6e 64 69 66 0a 74 79 70 65 64 ble.#endif.typed
499c0 65 66 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 ef sqlite_int64
499d0 69 36 34 3b 20 20 20 20 20 20 20 20 20 20 2f 2a i64; /*
499e0 20 38 2d 62 79 74 65 20 73 69 67 6e 65 64 20 69 8-byte signed i
499f0 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 65 64 65 nteger */.typede
49a00 66 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 f sqlite_uint64
49a10 75 36 34 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 u64; /*
49a20 38 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 64 20 8-byte unsigned
49a30 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 65 64 integer */.typed
49a40 65 66 20 55 49 4e 54 33 32 5f 54 59 50 45 20 75 ef UINT32_TYPE u
49a50 33 32 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 32; /*
49a60 20 34 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 64 4-byte unsigned
49a70 20 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 65 integer */.type
49a80 64 65 66 20 55 49 4e 54 31 36 5f 54 59 50 45 20 def UINT16_TYPE
49a90 75 31 36 3b 20 20 20 20 20 20 20 20 20 20 20 2f u16; /
49aa0 2a 20 32 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 * 2-byte unsigne
49ab0 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 d integer */.typ
49ac0 65 64 65 66 20 49 4e 54 31 36 5f 54 59 50 45 20 edef INT16_TYPE
49ad0 69 31 36 3b 20 20 20 20 20 20 20 20 20 20 20 20 i16;
49ae0 2f 2a 20 32 2d 62 79 74 65 20 73 69 67 6e 65 64 /* 2-byte signed
49af0 20 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 65 integer */.type
49b00 64 65 66 20 55 49 4e 54 38 5f 54 59 50 45 20 75 def UINT8_TYPE u
49b10 38 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 8; /
49b20 2a 20 31 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 * 1-byte unsigne
49b30 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 d integer */.typ
49b40 65 64 65 66 20 49 4e 54 38 5f 54 59 50 45 20 69 edef INT8_TYPE i
49b50 38 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 8;
49b60 2f 2a 20 31 2d 62 79 74 65 20 73 69 67 6e 65 64 /* 1-byte signed
49b70 20 69 6e 74 65 67 65 72 20 2a 2f 0a 0a 2f 2a 0a integer */../*.
49b80 2a 2a 20 53 51 4c 49 54 45 5f 4d 41 58 5f 55 33 ** SQLITE_MAX_U3
49b90 32 20 69 73 20 61 20 75 36 34 20 63 6f 6e 73 74 2 is a u64 const
49ba0 61 6e 74 20 74 68 61 74 20 69 73 20 74 68 65 20 ant that is the
49bb0 6d 61 78 69 6d 75 6d 20 75 36 34 20 76 61 6c 75 maximum u64 valu
49bc0 65 0a 2a 2a 20 74 68 61 74 20 63 61 6e 20 62 65 e.** that can be
49bd0 20 73 74 6f 72 65 64 20 69 6e 20 61 20 75 33 32 stored in a u32
49be0 20 77 69 74 68 6f 75 74 20 6c 6f 73 73 20 6f 66 without loss of
49bf0 20 64 61 74 61 2e 20 20 54 68 65 20 76 61 6c 75 data. The valu
49c00 65 0a 2a 2a 20 69 73 20 30 78 30 30 30 30 30 30 e.** is 0x000000
49c10 30 30 66 66 66 66 66 66 66 66 2e 20 20 42 75 74 00ffffffff. But
49c20 20 62 65 63 61 75 73 65 20 6f 66 20 71 75 69 72 because of quir
49c30 6b 73 20 6f 66 20 73 6f 6d 65 20 63 6f 6d 70 69 ks of some compi
49c40 6c 65 72 73 2c 20 77 65 0a 2a 2a 20 68 61 76 65 lers, we.** have
49c50 20 74 6f 20 73 70 65 63 69 66 79 20 74 68 65 20 to specify the
49c60 76 61 6c 75 65 20 69 6e 20 74 68 65 20 6c 65 73 value in the les
49c70 73 20 69 6e 74 75 69 74 69 76 65 20 6d 61 6e 6e s intuitive mann
49c80 65 72 20 73 68 6f 77 6e 3a 0a 2a 2f 0a 23 64 65 er shown:.*/.#de
49c90 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f fine SQLITE_MAX_
49ca0 55 33 32 20 20 28 28 28 28 75 36 34 29 31 29 3c U32 ((((u64)1)<
49cb0 3c 33 32 29 2d 31 29 0a 0a 2f 2a 0a 2a 2a 20 4d <32)-1)../*.** M
49cc0 61 63 72 6f 73 20 74 6f 20 64 65 74 65 72 6d 69 acros to determi
49cd0 6e 65 20 77 68 65 74 68 65 72 20 74 68 65 20 6d ne whether the m
49ce0 61 63 68 69 6e 65 20 69 73 20 62 69 67 20 6f 72 achine is big or
49cf0 20 6c 69 74 74 6c 65 20 65 6e 64 69 61 6e 2c 0a little endian,.
49d00 2a 2a 20 65 76 61 6c 75 61 74 65 64 20 61 74 20 ** evaluated at
49d10 72 75 6e 74 69 6d 65 2e 0a 2a 2f 0a 23 69 66 64 runtime..*/.#ifd
49d20 65 66 20 53 51 4c 49 54 45 5f 41 4d 41 4c 47 41 ef SQLITE_AMALGA
49d30 4d 41 54 49 4f 4e 0a 53 51 4c 49 54 45 5f 50 52 MATION.SQLITE_PR
49d40 49 56 41 54 45 20 63 6f 6e 73 74 20 69 6e 74 20 IVATE const int
49d50 73 71 6c 69 74 65 33 6f 6e 65 20 3d 20 31 3b 0a sqlite3one = 1;.
49d60 23 65 6c 73 65 0a 53 51 4c 49 54 45 5f 50 52 49 #else.SQLITE_PRI
49d70 56 41 54 45 20 63 6f 6e 73 74 20 69 6e 74 20 73 VATE const int s
49d80 71 6c 69 74 65 33 6f 6e 65 3b 0a 23 65 6e 64 69 qlite3one;.#endi
49d90 66 0a 23 69 66 20 64 65 66 69 6e 65 64 28 69 33 f.#if defined(i3
49da0 38 36 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 86) || defined(_
49db0 5f 69 33 38 36 5f 5f 29 20 7c 7c 20 64 65 66 69 _i386__) || defi
49dc0 6e 65 64 28 5f 4d 5f 49 58 38 36 29 5c 0a 20 20 ned(_M_IX86)\.
49dd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
49de0 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 64 65 || de
49df0 66 69 6e 65 64 28 5f 5f 78 38 36 5f 36 34 29 20 fined(__x86_64)
49e00 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f 78 38 36 || defined(__x86
49e10 5f 36 34 5f 5f 29 0a 23 20 64 65 66 69 6e 65 20 _64__).# define
49e20 53 51 4c 49 54 45 5f 42 49 47 45 4e 44 49 41 4e SQLITE_BIGENDIAN
49e30 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 53 0.# define S
49e40 51 4c 49 54 45 5f 4c 49 54 54 4c 45 45 4e 44 49 QLITE_LITTLEENDI
49e50 41 4e 20 31 0a 23 20 64 65 66 69 6e 65 20 53 51 AN 1.# define SQ
49e60 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 LITE_UTF16NATIVE
49e70 20 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 SQLITE_UTF16LE
49e80 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 .#else.# define
49e90 53 51 4c 49 54 45 5f 42 49 47 45 4e 44 49 41 4e SQLITE_BIGENDIAN
49ea0 20 20 20 20 28 2a 28 63 68 61 72 20 2a 29 28 26 (*(char *)(&
49eb0 73 71 6c 69 74 65 33 6f 6e 65 29 3d 3d 30 29 0a sqlite3one)==0).
49ec0 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f # define SQLITE_
49ed0 4c 49 54 54 4c 45 45 4e 44 49 41 4e 20 28 2a 28 LITTLEENDIAN (*(
49ee0 63 68 61 72 20 2a 29 28 26 73 71 6c 69 74 65 33 char *)(&sqlite3
49ef0 6f 6e 65 29 3d 3d 31 29 0a 23 20 64 65 66 69 6e one)==1).# defin
49f00 65 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 e SQLITE_UTF16NA
49f10 54 49 56 45 20 28 53 51 4c 49 54 45 5f 42 49 47 TIVE (SQLITE_BIG
49f20 45 4e 44 49 41 4e 3f 53 51 4c 49 54 45 5f 55 54 ENDIAN?SQLITE_UT
49f30 46 31 36 42 45 3a 53 51 4c 49 54 45 5f 55 54 46 F16BE:SQLITE_UTF
49f40 31 36 4c 45 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 16LE).#endif../*
49f50 0a 2a 2a 20 43 6f 6e 73 74 61 6e 74 73 20 66 6f .** Constants fo
49f60 72 20 74 68 65 20 6c 61 72 67 65 73 74 20 61 6e r the largest an
49f70 64 20 73 6d 61 6c 6c 65 73 74 20 70 6f 73 73 69 d smallest possi
49f80 62 6c 65 20 36 34 2d 62 69 74 20 73 69 67 6e 65 ble 64-bit signe
49f90 64 20 69 6e 74 65 67 65 72 73 2e 0a 2a 2a 20 54 d integers..** T
49fa0 68 65 73 65 20 6d 61 63 72 6f 73 20 61 72 65 20 hese macros are
49fb0 64 65 73 69 67 6e 65 64 20 74 6f 20 77 6f 72 6b designed to work
49fc0 20 63 6f 72 72 65 63 74 6c 79 20 6f 6e 20 62 6f correctly on bo
49fd0 74 68 20 33 32 2d 62 69 74 20 61 6e 64 20 36 34 th 32-bit and 64
49fe0 2d 62 69 74 0a 2a 2a 20 63 6f 6d 70 69 6c 65 72 -bit.** compiler
49ff0 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4c 41 s..*/.#define LA
4a000 52 47 45 53 54 5f 49 4e 54 36 34 20 20 28 30 78 RGEST_INT64 (0x
4a010 66 66 66 66 66 66 66 66 7c 28 28 28 69 36 34 29 ffffffff|(((i64)
4a020 30 78 37 66 66 66 66 66 66 66 29 3c 3c 33 32 29 0x7fffffff)<<32)
4a030 29 0a 23 64 65 66 69 6e 65 20 53 4d 41 4c 4c 45 ).#define SMALLE
4a040 53 54 5f 49 4e 54 36 34 20 28 28 28 69 36 34 29 ST_INT64 (((i64)
4a050 2d 31 29 20 2d 20 4c 41 52 47 45 53 54 5f 49 4e -1) - LARGEST_IN
4a060 54 36 34 29 0a 0a 2f 2a 20 0a 2a 2a 20 52 6f 75 T64)../* .** Rou
4a070 6e 64 20 75 70 20 61 20 6e 75 6d 62 65 72 20 74 nd up a number t
4a080 6f 20 74 68 65 20 6e 65 78 74 20 6c 61 72 67 65 o the next large
4a090 72 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20 38 2e r multiple of 8.
4a0a0 20 20 54 68 69 73 20 69 73 20 75 73 65 64 0a 2a This is used.*
4a0b0 2a 20 74 6f 20 66 6f 72 63 65 20 38 2d 62 79 74 * to force 8-byt
4a0c0 65 20 61 6c 69 67 6e 6d 65 6e 74 20 6f 6e 20 36 e alignment on 6
4a0d0 34 2d 62 69 74 20 61 72 63 68 69 74 65 63 74 75 4-bit architectu
4a0e0 72 65 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 res..*/.#define
4a0f0 52 4f 55 4e 44 38 28 78 29 20 20 20 20 20 28 28 ROUND8(x) ((
4a100 28 78 29 2b 37 29 26 7e 37 29 0a 0a 2f 2a 0a 2a (x)+7)&~7)../*.*
4a110 2a 20 52 6f 75 6e 64 20 64 6f 77 6e 20 74 6f 20 * Round down to
4a120 74 68 65 20 6e 65 61 72 65 73 74 20 6d 75 6c 74 the nearest mult
4a130 69 70 6c 65 20 6f 66 20 38 0a 2a 2f 0a 23 64 65 iple of 8.*/.#de
4a140 66 69 6e 65 20 52 4f 55 4e 44 44 4f 57 4e 38 28 fine ROUNDDOWN8(
4a150 78 29 20 28 28 78 29 26 7e 37 29 0a 0a 2f 2a 0a x) ((x)&~7)../*.
4a160 2a 2a 20 41 73 73 65 72 74 20 74 68 61 74 20 74 ** Assert that t
4a170 68 65 20 70 6f 69 6e 74 65 72 20 58 20 69 73 20 he pointer X is
4a180 61 6c 69 67 6e 65 64 20 74 6f 20 61 6e 20 38 2d aligned to an 8-
4a190 62 79 74 65 20 62 6f 75 6e 64 61 72 79 2e 0a 2a byte boundary..*
4a1a0 2f 0a 23 64 65 66 69 6e 65 20 45 49 47 48 54 5f /.#define EIGHT_
4a1b0 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 58 BYTE_ALIGNMENT(X
4a1c0 29 20 20 20 28 28 28 28 63 68 61 72 2a 29 28 58 ) ((((char*)(X
4a1d0 29 20 2d 20 28 63 68 61 72 2a 29 30 29 26 37 29 ) - (char*)0)&7)
4a1e0 3d 3d 30 29 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 ==0).../*.** An
4a1f0 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 instance of the
4a200 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 following struct
4a210 75 72 65 20 69 73 20 75 73 65 64 20 74 6f 20 73 ure is used to s
4a220 74 6f 72 65 20 74 68 65 20 62 75 73 79 2d 68 61 tore the busy-ha
4a230 6e 64 6c 65 72 0a 2a 2a 20 63 61 6c 6c 62 61 63 ndler.** callbac
4a240 6b 20 66 6f 72 20 61 20 67 69 76 65 6e 20 73 71 k for a given sq
4a250 6c 69 74 65 20 68 61 6e 64 6c 65 2e 20 0a 2a 2a lite handle. .**
4a260 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 2e 62 .** The sqlite.b
4a270 75 73 79 48 61 6e 64 6c 65 72 20 6d 65 6d 62 65 usyHandler membe
4a280 72 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 20 r of the sqlite
4a290 73 74 72 75 63 74 20 63 6f 6e 74 61 69 6e 73 20 struct contains
4a2a0 74 68 65 20 62 75 73 79 0a 2a 2a 20 63 61 6c 6c the busy.** call
4a2b0 62 61 63 6b 20 66 6f 72 20 74 68 65 20 64 61 74 back for the dat
4a2c0 61 62 61 73 65 20 68 61 6e 64 6c 65 2e 20 45 61 abase handle. Ea
4a2d0 63 68 20 70 61 67 65 72 20 6f 70 65 6e 65 64 20 ch pager opened
4a2e0 76 69 61 20 74 68 65 20 73 71 6c 69 74 65 0a 2a via the sqlite.*
4a2f0 2a 20 68 61 6e 64 6c 65 20 69 73 20 70 61 73 73 * handle is pass
4a300 65 64 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 ed a pointer to
4a310 73 71 6c 69 74 65 2e 62 75 73 79 48 61 6e 64 6c sqlite.busyHandl
4a320 65 72 2e 20 54 68 65 20 62 75 73 79 2d 68 61 6e er. The busy-han
4a330 64 6c 65 72 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b dler.** callback
4a340 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e is currently in
4a350 76 6f 6b 65 64 20 6f 6e 6c 79 20 66 72 6f 6d 20 voked only from
4a360 77 69 74 68 69 6e 20 70 61 67 65 72 2e 63 2e 0a within pager.c..
4a370 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 */.typedef struc
4a380 74 20 42 75 73 79 48 61 6e 64 6c 65 72 20 42 75 t BusyHandler Bu
4a390 73 79 48 61 6e 64 6c 65 72 3b 0a 73 74 72 75 63 syHandler;.struc
4a3a0 74 20 42 75 73 79 48 61 6e 64 6c 65 72 20 7b 0a t BusyHandler {.
4a3b0 20 20 69 6e 74 20 28 2a 78 46 75 6e 63 29 28 76 int (*xFunc)(v
4a3c0 6f 69 64 20 2a 2c 69 6e 74 29 3b 20 20 2f 2a 20 oid *,int); /*
4a3d0 54 68 65 20 62 75 73 79 20 63 61 6c 6c 62 61 63 The busy callbac
4a3e0 6b 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 72 k */. void *pAr
4a3f0 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 g;
4a400 20 20 2f 2a 20 46 69 72 73 74 20 61 72 67 20 74 /* First arg t
4a410 6f 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 20 o busy callback
4a420 2a 2f 0a 20 20 69 6e 74 20 6e 42 75 73 79 3b 20 */. int nBusy;
4a430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4a440 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 65 64 20 77 /* Incremented w
4a450 69 74 68 20 65 61 63 68 20 62 75 73 79 20 63 61 ith each busy ca
4a460 6c 6c 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 ll */.};../*.**
4a470 4e 61 6d 65 20 6f 66 20 74 68 65 20 6d 61 73 74 Name of the mast
4a480 65 72 20 64 61 74 61 62 61 73 65 20 74 61 62 6c er database tabl
4a490 65 2e 20 20 54 68 65 20 6d 61 73 74 65 72 20 64 e. The master d
4a4a0 61 74 61 62 61 73 65 20 74 61 62 6c 65 0a 2a 2a atabase table.**
4a4b0 20 69 73 20 61 20 73 70 65 63 69 61 6c 20 74 61 is a special ta
4a4c0 62 6c 65 20 74 68 61 74 20 68 6f 6c 64 73 20 74 ble that holds t
4a4d0 68 65 20 6e 61 6d 65 73 20 61 6e 64 20 61 74 74 he names and att
4a4e0 72 69 62 75 74 65 73 20 6f 66 20 61 6c 6c 0a 2a ributes of all.*
4a4f0 2a 20 75 73 65 72 20 74 61 62 6c 65 73 20 61 6e * user tables an
4a500 64 20 69 6e 64 69 63 65 73 2e 0a 2a 2f 0a 23 64 d indices..*/.#d
4a510 65 66 69 6e 65 20 4d 41 53 54 45 52 5f 4e 41 4d efine MASTER_NAM
4a520 45 20 20 20 20 20 20 20 22 73 71 6c 69 74 65 5f E "sqlite_
4a530 6d 61 73 74 65 72 22 0a 23 64 65 66 69 6e 65 20 master".#define
4a540 54 45 4d 50 5f 4d 41 53 54 45 52 5f 4e 41 4d 45 TEMP_MASTER_NAME
4a550 20 20 22 73 71 6c 69 74 65 5f 74 65 6d 70 5f 6d "sqlite_temp_m
4a560 61 73 74 65 72 22 0a 0a 2f 2a 0a 2a 2a 20 54 68 aster"../*.** Th
4a570 65 20 72 6f 6f 74 2d 70 61 67 65 20 6f 66 20 74 e root-page of t
4a580 68 65 20 6d 61 73 74 65 72 20 64 61 74 61 62 61 he master databa
4a590 73 65 20 74 61 62 6c 65 2e 0a 2a 2f 0a 23 64 65 se table..*/.#de
4a5a0 66 69 6e 65 20 4d 41 53 54 45 52 5f 52 4f 4f 54 fine MASTER_ROOT
4a5b0 20 20 20 20 20 20 20 31 0a 0a 2f 2a 0a 2a 2a 20 1../*.**
4a5c0 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 The name of the
4a5d0 73 63 68 65 6d 61 20 74 61 62 6c 65 2e 0a 2a 2f schema table..*/
4a5e0 0a 23 64 65 66 69 6e 65 20 53 43 48 45 4d 41 5f .#define SCHEMA_
4a5f0 54 41 42 4c 45 28 78 29 20 20 28 28 21 4f 4d 49 TABLE(x) ((!OMI
4a600 54 5f 54 45 4d 50 44 42 29 26 26 28 78 3d 3d 31 T_TEMPDB)&&(x==1
4a610 29 3f 54 45 4d 50 5f 4d 41 53 54 45 52 5f 4e 41 )?TEMP_MASTER_NA
4a620 4d 45 3a 4d 41 53 54 45 52 5f 4e 41 4d 45 29 0a ME:MASTER_NAME).
4a630 0a 2f 2a 0a 2a 2a 20 41 20 63 6f 6e 76 65 6e 69 ./*.** A conveni
4a640 65 6e 63 65 20 6d 61 63 72 6f 20 74 68 61 74 20 ence macro that
4a650 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 returns the numb
4a660 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 69 er of elements i
4a670 6e 0a 2a 2a 20 61 6e 20 61 72 72 61 79 2e 0a 2a n.** an array..*
4a680 2f 0a 23 64 65 66 69 6e 65 20 41 72 72 61 79 53 /.#define ArrayS
4a690 69 7a 65 28 58 29 20 20 20 20 28 28 69 6e 74 29 ize(X) ((int)
4a6a0 28 73 69 7a 65 6f 66 28 58 29 2f 73 69 7a 65 6f (sizeof(X)/sizeo
4a6b0 66 28 58 5b 30 5d 29 29 29 0a 0a 2f 2a 0a 2a 2a f(X[0])))../*.**
4a6c0 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 The following v
4a6d0 61 6c 75 65 20 61 73 20 61 20 64 65 73 74 72 75 alue as a destru
4a6e0 63 74 6f 72 20 6d 65 61 6e 73 20 74 6f 20 75 73 ctor means to us
4a6f0 65 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 e sqlite3DbFree(
4a700 29 2e 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 6e )..** This is an
4a710 20 69 6e 74 65 72 6e 61 6c 20 65 78 74 65 6e 73 internal extens
4a720 69 6f 6e 20 74 6f 20 53 51 4c 49 54 45 5f 53 54 ion to SQLITE_ST
4a730 41 54 49 43 20 61 6e 64 20 53 51 4c 49 54 45 5f ATIC and SQLITE_
4a740 54 52 41 4e 53 49 45 4e 54 2e 0a 2a 2f 0a 23 64 TRANSIENT..*/.#d
4a750 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 59 4e efine SQLITE_DYN
4a760 41 4d 49 43 20 20 20 28 28 73 71 6c 69 74 65 33 AMIC ((sqlite3
4a770 5f 64 65 73 74 72 75 63 74 6f 72 5f 74 79 70 65 _destructor_type
4a780 29 73 71 6c 69 74 65 33 44 62 46 72 65 65 29 0a )sqlite3DbFree).
4a790 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 53 51 4c 49 ./*.** When SQLI
4a7a0 54 45 5f 4f 4d 49 54 5f 57 53 44 20 69 73 20 64 TE_OMIT_WSD is d
4a7b0 65 66 69 6e 65 64 2c 20 69 74 20 6d 65 61 6e 73 efined, it means
4a7c0 20 74 68 61 74 20 74 68 65 20 74 61 72 67 65 74 that the target
4a7d0 20 70 6c 61 74 66 6f 72 6d 20 64 6f 65 73 0a 2a platform does.*
4a7e0 2a 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 57 72 * not support Wr
4a7f0 69 74 61 62 6c 65 20 53 74 61 74 69 63 20 44 61 itable Static Da
4a800 74 61 20 28 57 53 44 29 20 73 75 63 68 20 61 73 ta (WSD) such as
4a810 20 67 6c 6f 62 61 6c 20 61 6e 64 20 73 74 61 74 global and stat
4a820 69 63 20 76 61 72 69 61 62 6c 65 73 2e 0a 2a 2a ic variables..**
4a830 20 41 6c 6c 20 76 61 72 69 61 62 6c 65 73 20 6d All variables m
4a840 75 73 74 20 65 69 74 68 65 72 20 62 65 20 6f 6e ust either be on
4a850 20 74 68 65 20 73 74 61 63 6b 20 6f 72 20 64 79 the stack or dy
4a860 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 namically alloca
4a870 74 65 64 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 ted from.** the
4a880 68 65 61 70 2e 20 20 57 68 65 6e 20 57 53 44 20 heap. When WSD
4a890 69 73 20 75 6e 73 75 70 70 6f 72 74 65 64 2c 20 is unsupported,
4a8a0 74 68 65 20 76 61 72 69 61 62 6c 65 20 64 65 63 the variable dec
4a8b0 6c 61 72 61 74 69 6f 6e 73 20 73 63 61 74 74 65 larations scatte
4a8c0 72 65 64 0a 2a 2a 20 74 68 72 6f 75 67 68 6f 75 red.** throughou
4a8d0 74 20 74 68 65 20 53 51 4c 69 74 65 20 63 6f 64 t the SQLite cod
4a8e0 65 20 6d 75 73 74 20 62 65 63 6f 6d 65 20 63 6f e must become co
4a8f0 6e 73 74 61 6e 74 73 20 69 6e 73 74 65 61 64 2e nstants instead.
4a900 20 20 54 68 65 20 53 51 4c 49 54 45 5f 57 53 44 The SQLITE_WSD
4a910 0a 2a 2a 20 6d 61 63 72 6f 20 69 73 20 75 73 65 .** macro is use
4a920 64 20 66 6f 72 20 74 68 69 73 20 70 75 72 70 6f d for this purpo
4a930 73 65 2e 20 20 41 6e 64 20 69 6e 73 74 65 61 64 se. And instead
4a940 20 6f 66 20 72 65 66 65 72 65 6e 63 69 6e 67 20 of referencing
4a950 74 68 65 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 the variable.**
4a960 64 69 72 65 63 74 6c 79 2c 20 77 65 20 75 73 65 directly, we use
4a970 20 69 74 73 20 63 6f 6e 73 74 61 6e 74 20 61 73 its constant as
4a980 20 61 20 6b 65 79 20 74 6f 20 6c 6f 6f 6b 75 70 a key to lookup
4a990 20 74 68 65 20 72 75 6e 2d 74 69 6d 65 20 61 6c the run-time al
4a9a0 6c 6f 63 61 74 65 64 0a 2a 2a 20 62 75 66 66 65 located.** buffe
4a9b0 72 20 74 68 61 74 20 68 6f 6c 64 73 20 72 65 61 r that holds rea
4a9c0 6c 20 76 61 72 69 61 62 6c 65 2e 20 20 54 68 65 l variable. The
4a9d0 20 63 6f 6e 73 74 61 6e 74 20 69 73 20 61 6c 73 constant is als
4a9e0 6f 20 74 68 65 20 69 6e 69 74 69 61 6c 69 7a 65 o the initialize
4a9f0 72 0a 2a 2a 20 66 6f 72 20 74 68 65 20 72 75 6e r.** for the run
4aa00 2d 74 69 6d 65 20 61 6c 6c 6f 63 61 74 65 64 20 -time allocated
4aa10 62 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 6e buffer..**.** In
4aa20 20 74 68 65 20 75 73 75 61 6c 20 63 61 73 65 20 the usual case
4aa30 77 68 65 72 65 20 57 53 44 20 69 73 20 73 75 70 where WSD is sup
4aa40 70 6f 72 74 65 64 2c 20 74 68 65 20 53 51 4c 49 ported, the SQLI
4aa50 54 45 5f 57 53 44 20 61 6e 64 20 47 4c 4f 42 41 TE_WSD and GLOBA
4aa60 4c 0a 2a 2a 20 6d 61 63 72 6f 73 20 62 65 63 6f L.** macros beco
4aa70 6d 65 20 6e 6f 2d 6f 70 73 20 61 6e 64 20 68 61 me no-ops and ha
4aa80 76 65 20 7a 65 72 6f 20 70 65 72 66 6f 72 6d 61 ve zero performa
4aa90 6e 63 65 20 69 6d 70 61 63 74 2e 0a 2a 2f 0a 23 nce impact..*/.#
4aaa0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 ifdef SQLITE_OMI
4aab0 54 5f 57 53 44 0a 20 20 23 64 65 66 69 6e 65 20 T_WSD. #define
4aac0 53 51 4c 49 54 45 5f 57 53 44 20 63 6f 6e 73 74 SQLITE_WSD const
4aad0 0a 20 20 23 64 65 66 69 6e 65 20 47 4c 4f 42 41 . #define GLOBA
4aae0 4c 28 74 2c 76 29 20 28 2a 28 74 2a 29 73 71 6c L(t,v) (*(t*)sql
4aaf0 69 74 65 33 5f 77 73 64 5f 66 69 6e 64 28 28 76 ite3_wsd_find((v
4ab00 6f 69 64 2a 29 26 28 76 29 2c 20 73 69 7a 65 6f oid*)&(v), sizeo
4ab10 66 28 76 29 29 29 0a 20 20 23 64 65 66 69 6e 65 f(v))). #define
4ab20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
4ab30 6e 66 69 67 20 47 4c 4f 42 41 4c 28 73 74 72 75 nfig GLOBAL(stru
4ab40 63 74 20 53 71 6c 69 74 65 33 43 6f 6e 66 69 67 ct Sqlite3Config
4ab50 2c 20 73 71 6c 69 74 65 33 43 6f 6e 66 69 67 29 , sqlite3Config)
4ab60 0a 53 51 4c 49 54 45 5f 41 50 49 20 20 20 69 6e .SQLITE_API in
4ab70 74 20 73 71 6c 69 74 65 33 5f 77 73 64 5f 69 6e t sqlite3_wsd_in
4ab80 69 74 28 69 6e 74 20 4e 2c 20 69 6e 74 20 4a 29 it(int N, int J)
4ab90 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 20 20 76 ;.SQLITE_API v
4aba0 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 77 73 64 oid *sqlite3_wsd
4abb0 5f 66 69 6e 64 28 76 6f 69 64 20 2a 4b 2c 20 69 _find(void *K, i
4abc0 6e 74 20 4c 29 3b 0a 23 65 6c 73 65 0a 20 20 23 nt L);.#else. #
4abd0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 57 53 define SQLITE_WS
4abe0 44 20 0a 20 20 23 64 65 66 69 6e 65 20 47 4c 4f D . #define GLO
4abf0 42 41 4c 28 74 2c 76 29 20 76 0a 20 20 23 64 65 BAL(t,v) v. #de
4ac00 66 69 6e 65 20 73 71 6c 69 74 65 33 47 6c 6f 62 fine sqlite3Glob
4ac10 61 6c 43 6f 6e 66 69 67 20 73 71 6c 69 74 65 33 alConfig sqlite3
4ac20 43 6f 6e 66 69 67 0a 23 65 6e 64 69 66 0a 0a 2f Config.#endif../
4ac30 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 *.** The followi
4ac40 6e 67 20 6d 61 63 72 6f 73 20 61 72 65 20 75 73 ng macros are us
4ac50 65 64 20 74 6f 20 73 75 70 70 72 65 73 73 20 63 ed to suppress c
4ac60 6f 6d 70 69 6c 65 72 20 77 61 72 6e 69 6e 67 73 ompiler warnings
4ac70 20 61 6e 64 20 74 6f 0a 2a 2a 20 6d 61 6b 65 20 and to.** make
4ac80 69 74 20 63 6c 65 61 72 20 74 6f 20 68 75 6d 61 it clear to huma
4ac90 6e 20 72 65 61 64 65 72 73 20 77 68 65 6e 20 61 n readers when a
4aca0 20 66 75 6e 63 74 69 6f 6e 20 70 61 72 61 6d 65 function parame
4acb0 74 65 72 20 69 73 20 64 65 6c 69 62 65 72 61 74 ter is deliberat
4acc0 65 6c 79 20 0a 2a 2a 20 6c 65 66 74 20 75 6e 75 ely .** left unu
4acd0 73 65 64 20 77 69 74 68 69 6e 20 74 68 65 20 62 sed within the b
4ace0 6f 64 79 20 6f 66 20 61 20 66 75 6e 63 74 69 6f ody of a functio
4acf0 6e 2e 20 54 68 69 73 20 75 73 75 61 6c 6c 79 20 n. This usually
4ad00 68 61 70 70 65 6e 73 20 77 68 65 6e 0a 2a 2a 20 happens when.**
4ad10 61 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 a function is ca
4ad20 6c 6c 65 64 20 76 69 61 20 61 20 66 75 6e 63 74 lled via a funct
4ad30 69 6f 6e 20 70 6f 69 6e 74 65 72 2e 20 46 6f 72 ion pointer. For
4ad40 20 65 78 61 6d 70 6c 65 20 74 68 65 20 0a 2a 2a example the .**
4ad50 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
4ad60 6f 66 20 61 6e 20 53 51 4c 20 61 67 67 72 65 67 of an SQL aggreg
4ad70 61 74 65 20 73 74 65 70 20 63 61 6c 6c 62 61 63 ate step callbac
4ad80 6b 20 6d 61 79 20 6e 6f 74 20 75 73 65 20 74 68 k may not use th
4ad90 65 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 69 e.** parameter i
4ada0 6e 64 69 63 61 74 69 6e 67 20 74 68 65 20 6e 75 ndicating the nu
4adb0 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 mber of argument
4adc0 73 20 70 61 73 73 65 64 20 74 6f 20 74 68 65 20 s passed to the
4add0 61 67 67 72 65 67 61 74 65 2c 0a 2a 2a 20 69 66 aggregate,.** if
4ade0 20 69 74 20 6b 6e 6f 77 73 20 74 68 61 74 20 74 it knows that t
4adf0 68 69 73 20 69 73 20 65 6e 66 6f 72 63 65 64 20 his is enforced
4ae00 65 6c 73 65 77 68 65 72 65 2e 0a 2a 2a 0a 2a 2a elsewhere..**.**
4ae10 20 57 68 65 6e 20 61 20 66 75 6e 63 74 69 6f 6e When a function
4ae20 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e 6f parameter is no
4ae30 74 20 75 73 65 64 20 61 74 20 61 6c 6c 20 77 69 t used at all wi
4ae40 74 68 69 6e 20 74 68 65 20 62 6f 64 79 20 6f 66 thin the body of
4ae50 20 61 20 66 75 6e 63 74 69 6f 6e 2c 0a 2a 2a 20 a function,.**
4ae60 69 74 20 69 73 20 67 65 6e 65 72 61 6c 6c 79 20 it is generally
4ae70 6e 61 6d 65 64 20 22 4e 6f 74 55 73 65 64 22 20 named "NotUsed"
4ae80 6f 72 20 22 4e 6f 74 55 73 65 64 32 22 20 74 6f or "NotUsed2" to
4ae90 20 6d 61 6b 65 20 74 68 69 6e 67 73 20 65 76 65 make things eve
4aea0 6e 20 63 6c 65 61 72 65 72 2e 0a 2a 2a 20 48 6f n clearer..** Ho
4aeb0 77 65 76 65 72 2c 20 74 68 65 73 65 20 6d 61 63 wever, these mac
4aec0 72 6f 73 20 6d 61 79 20 61 6c 73 6f 20 62 65 20 ros may also be
4aed0 75 73 65 64 20 74 6f 20 73 75 70 70 72 65 73 73 used to suppress
4aee0 20 77 61 72 6e 69 6e 67 73 20 72 65 6c 61 74 65 warnings relate
4aef0 64 20 74 6f 0a 2a 2a 20 70 61 72 61 6d 65 74 65 d to.** paramete
4af00 72 73 20 74 68 61 74 20 6d 61 79 20 6f 72 20 6d rs that may or m
4af10 61 79 20 6e 6f 74 20 62 65 20 75 73 65 64 20 64 ay not be used d
4af20 65 70 65 6e 64 69 6e 67 20 6f 6e 20 63 6f 6d 70 epending on comp
4af30 69 6c 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 2e ilation options.
4af40 0a 2a 2a 20 46 6f 72 20 65 78 61 6d 70 6c 65 20 .** For example
4af50 74 68 6f 73 65 20 70 61 72 61 6d 65 74 65 72 73 those parameters
4af60 20 6f 6e 6c 79 20 75 73 65 64 20 69 6e 20 61 73 only used in as
4af70 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 sert() statement
4af80 73 2e 20 49 6e 20 74 68 65 73 65 0a 2a 2a 20 63 s. In these.** c
4af90 61 73 65 73 20 74 68 65 20 70 61 72 61 6d 65 74 ases the paramet
4afa0 65 72 73 20 61 72 65 20 6e 61 6d 65 64 20 61 73 ers are named as
4afb0 20 70 65 72 20 74 68 65 20 75 73 75 61 6c 20 63 per the usual c
4afc0 6f 6e 76 65 6e 74 69 6f 6e 73 2e 0a 2a 2f 0a 23 onventions..*/.#
4afd0 64 65 66 69 6e 65 20 55 4e 55 53 45 44 5f 50 41 define UNUSED_PA
4afe0 52 41 4d 45 54 45 52 28 78 29 20 28 76 6f 69 64 RAMETER(x) (void
4aff0 29 28 78 29 0a 23 64 65 66 69 6e 65 20 55 4e 55 )(x).#define UNU
4b000 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28 78 SED_PARAMETER2(x
4b010 2c 79 29 20 55 4e 55 53 45 44 5f 50 41 52 41 4d ,y) UNUSED_PARAM
4b020 45 54 45 52 28 78 29 2c 55 4e 55 53 45 44 5f 50 ETER(x),UNUSED_P
4b030 41 52 41 4d 45 54 45 52 28 79 29 0a 0a 2f 2a 0a ARAMETER(y)../*.
4b040 2a 2a 20 46 6f 72 77 61 72 64 20 72 65 66 65 72 ** Forward refer
4b050 65 6e 63 65 73 20 74 6f 20 73 74 72 75 63 74 75 ences to structu
4b060 72 65 73 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 res.*/.typedef s
4b070 74 72 75 63 74 20 41 67 67 49 6e 66 6f 20 41 67 truct AggInfo Ag
4b080 67 49 6e 66 6f 3b 0a 74 79 70 65 64 65 66 20 73 gInfo;.typedef s
4b090 74 72 75 63 74 20 41 75 74 68 43 6f 6e 74 65 78 truct AuthContex
4b0a0 74 20 41 75 74 68 43 6f 6e 74 65 78 74 3b 0a 74 t AuthContext;.t
4b0b0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 41 75 ypedef struct Au
4b0c0 74 6f 69 6e 63 49 6e 66 6f 20 41 75 74 6f 69 6e toincInfo Autoin
4b0d0 63 49 6e 66 6f 3b 0a 74 79 70 65 64 65 66 20 73 cInfo;.typedef s
4b0e0 74 72 75 63 74 20 42 69 74 76 65 63 20 42 69 74 truct Bitvec Bit
4b0f0 76 65 63 3b 0a 74 79 70 65 64 65 66 20 73 74 72 vec;.typedef str
4b100 75 63 74 20 52 6f 77 53 65 74 20 52 6f 77 53 65 uct RowSet RowSe
4b110 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 t;.typedef struc
4b120 74 20 43 6f 6c 6c 53 65 71 20 43 6f 6c 6c 53 65 t CollSeq CollSe
4b130 71 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 q;.typedef struc
4b140 74 20 43 6f 6c 75 6d 6e 20 43 6f 6c 75 6d 6e 3b t Column Column;
4b150 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
4b160 44 62 20 44 62 3b 0a 74 79 70 65 64 65 66 20 73 Db Db;.typedef s
4b170 74 72 75 63 74 20 53 63 68 65 6d 61 20 53 63 68 truct Schema Sch
4b180 65 6d 61 3b 0a 74 79 70 65 64 65 66 20 73 74 72 ema;.typedef str
4b190 75 63 74 20 45 78 70 72 20 45 78 70 72 3b 0a 74 uct Expr Expr;.t
4b1a0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 45 78 ypedef struct Ex
4b1b0 70 72 4c 69 73 74 20 45 78 70 72 4c 69 73 74 3b prList ExprList;
4b1c0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
4b1d0 45 78 70 72 53 70 61 6e 20 45 78 70 72 53 70 61 ExprSpan ExprSpa
4b1e0 6e 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 n;.typedef struc
4b1f0 74 20 46 4b 65 79 20 46 4b 65 79 3b 0a 74 79 70 t FKey FKey;.typ
4b200 65 64 65 66 20 73 74 72 75 63 74 20 46 75 6e 63 edef struct Func
4b210 44 65 66 20 46 75 6e 63 44 65 66 3b 0a 74 79 70 Def FuncDef;.typ
4b220 65 64 65 66 20 73 74 72 75 63 74 20 46 75 6e 63 edef struct Func
4b230 44 65 66 48 61 73 68 20 46 75 6e 63 44 65 66 48 DefHash FuncDefH
4b240 61 73 68 3b 0a 74 79 70 65 64 65 66 20 73 74 72 ash;.typedef str
4b250 75 63 74 20 49 64 4c 69 73 74 20 49 64 4c 69 73 uct IdList IdLis
4b260 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 t;.typedef struc
4b270 74 20 49 6e 64 65 78 20 49 6e 64 65 78 3b 0a 74 t Index Index;.t
4b280 79 70 65 64 65 66 20 73 74 72 75 63 74 20 49 6e ypedef struct In
4b290 64 65 78 53 61 6d 70 6c 65 20 49 6e 64 65 78 53 dexSample IndexS
4b2a0 61 6d 70 6c 65 3b 0a 74 79 70 65 64 65 66 20 73 ample;.typedef s
4b2b0 74 72 75 63 74 20 4b 65 79 43 6c 61 73 73 20 4b truct KeyClass K
4b2c0 65 79 43 6c 61 73 73 3b 0a 74 79 70 65 64 65 66 eyClass;.typedef
4b2d0 20 73 74 72 75 63 74 20 4b 65 79 49 6e 66 6f 20 struct KeyInfo
4b2e0 4b 65 79 49 6e 66 6f 3b 0a 74 79 70 65 64 65 66 KeyInfo;.typedef
4b2f0 20 73 74 72 75 63 74 20 4c 6f 6f 6b 61 73 69 64 struct Lookasid
4b300 65 20 4c 6f 6f 6b 61 73 69 64 65 3b 0a 74 79 70 e Lookaside;.typ
4b310 65 64 65 66 20 73 74 72 75 63 74 20 4c 6f 6f 6b edef struct Look
4b320 61 73 69 64 65 53 6c 6f 74 20 4c 6f 6f 6b 61 73 asideSlot Lookas
4b330 69 64 65 53 6c 6f 74 3b 0a 74 79 70 65 64 65 66 ideSlot;.typedef
4b340 20 73 74 72 75 63 74 20 4d 6f 64 75 6c 65 20 4d struct Module M
4b350 6f 64 75 6c 65 3b 0a 74 79 70 65 64 65 66 20 73 odule;.typedef s
4b360 74 72 75 63 74 20 4e 61 6d 65 43 6f 6e 74 65 78 truct NameContex
4b370 74 20 4e 61 6d 65 43 6f 6e 74 65 78 74 3b 0a 74 t NameContext;.t
4b380 79 70 65 64 65 66 20 73 74 72 75 63 74 20 50 61 ypedef struct Pa
4b390 72 73 65 20 50 61 72 73 65 3b 0a 74 79 70 65 64 rse Parse;.typed
4b3a0 65 66 20 73 74 72 75 63 74 20 53 61 76 65 70 6f ef struct Savepo
4b3b0 69 6e 74 20 53 61 76 65 70 6f 69 6e 74 3b 0a 74 int Savepoint;.t
4b3c0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 53 65 ypedef struct Se
4b3d0 6c 65 63 74 20 53 65 6c 65 63 74 3b 0a 74 79 70 lect Select;.typ
4b3e0 65 64 65 66 20 73 74 72 75 63 74 20 53 72 63 4c edef struct SrcL
4b3f0 69 73 74 20 53 72 63 4c 69 73 74 3b 0a 74 79 70 ist SrcList;.typ
4b400 65 64 65 66 20 73 74 72 75 63 74 20 53 74 72 41 edef struct StrA
4b410 63 63 75 6d 20 53 74 72 41 63 63 75 6d 3b 0a 74 ccum StrAccum;.t
4b420 79 70 65 64 65 66 20 73 74 72 75 63 74 20 54 61 ypedef struct Ta
4b430 62 6c 65 20 54 61 62 6c 65 3b 0a 74 79 70 65 64 ble Table;.typed
4b440 65 66 20 73 74 72 75 63 74 20 54 61 62 6c 65 4c ef struct TableL
4b450 6f 63 6b 20 54 61 62 6c 65 4c 6f 63 6b 3b 0a 74 ock TableLock;.t
4b460 79 70 65 64 65 66 20 73 74 72 75 63 74 20 54 6f ypedef struct To
4b470 6b 65 6e 20 54 6f 6b 65 6e 3b 0a 74 79 70 65 64 ken Token;.typed
4b480 65 66 20 73 74 72 75 63 74 20 54 72 69 67 67 65 ef struct Trigge
4b490 72 50 72 67 20 54 72 69 67 67 65 72 50 72 67 3b rPrg TriggerPrg;
4b4a0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
4b4b0 54 72 69 67 67 65 72 53 74 65 70 20 54 72 69 67 TriggerStep Trig
4b4c0 67 65 72 53 74 65 70 3b 0a 74 79 70 65 64 65 66 gerStep;.typedef
4b4d0 20 73 74 72 75 63 74 20 54 72 69 67 67 65 72 20 struct Trigger
4b4e0 54 72 69 67 67 65 72 3b 0a 74 79 70 65 64 65 66 Trigger;.typedef
4b4f0 20 73 74 72 75 63 74 20 55 6e 70 61 63 6b 65 64 struct Unpacked
4b500 52 65 63 6f 72 64 20 55 6e 70 61 63 6b 65 64 52 Record UnpackedR
4b510 65 63 6f 72 64 3b 0a 74 79 70 65 64 65 66 20 73 ecord;.typedef s
4b520 74 72 75 63 74 20 56 54 61 62 6c 65 20 56 54 61 truct VTable VTa
4b530 62 6c 65 3b 0a 74 79 70 65 64 65 66 20 73 74 72 ble;.typedef str
4b540 75 63 74 20 57 61 6c 6b 65 72 20 57 61 6c 6b 65 uct Walker Walke
4b550 72 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 r;.typedef struc
4b560 74 20 57 68 65 72 65 50 6c 61 6e 20 57 68 65 72 t WherePlan Wher
4b570 65 50 6c 61 6e 3b 0a 74 79 70 65 64 65 66 20 73 ePlan;.typedef s
4b580 74 72 75 63 74 20 57 68 65 72 65 49 6e 66 6f 20 truct WhereInfo
4b590 57 68 65 72 65 49 6e 66 6f 3b 0a 74 79 70 65 64 WhereInfo;.typed
4b5a0 65 66 20 73 74 72 75 63 74 20 57 68 65 72 65 4c ef struct WhereL
4b5b0 65 76 65 6c 20 57 68 65 72 65 4c 65 76 65 6c 3b evel WhereLevel;
4b5c0 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 65 72 20 73 6f ../*.** Defer so
4b5d0 75 72 63 69 6e 67 20 76 64 62 65 2e 68 20 61 6e urcing vdbe.h an
4b5e0 64 20 62 74 72 65 65 2e 68 20 75 6e 74 69 6c 20 d btree.h until
4b5f0 61 66 74 65 72 20 74 68 65 20 22 75 38 22 20 61 after the "u8" a
4b600 6e 64 20 0a 2a 2a 20 22 42 75 73 79 48 61 6e 64 nd .** "BusyHand
4b610 6c 65 72 22 20 74 79 70 65 64 65 66 73 2e 20 76 ler" typedefs. v
4b620 64 62 65 2e 68 20 61 6c 73 6f 20 72 65 71 75 69 dbe.h also requi
4b630 72 65 73 20 61 20 66 65 77 20 6f 66 20 74 68 65 res a few of the
4b640 20 6f 70 61 71 75 65 0a 2a 2a 20 70 6f 69 6e 74 opaque.** point
4b650 65 72 20 74 79 70 65 73 20 28 69 2e 65 2e 20 46 er types (i.e. F
4b660 75 6e 63 44 65 66 29 20 64 65 66 69 6e 65 64 20 uncDef) defined
4b670 61 62 6f 76 65 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a above..*/./*****
4b680 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 ********* Includ
4b690 65 20 62 74 72 65 65 2e 68 20 69 6e 20 74 68 65 e btree.h in the
4b6a0 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 middle of sqlit
4b6b0 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a eInt.h *********
4b6c0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
4b6d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 ********* Begin
4b6e0 66 69 6c 65 20 62 74 72 65 65 2e 68 20 2a 2a 2a file btree.h ***
4b6f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4b700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4b710 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 ********/./*.**
4b720 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 2001 September 1
4b730 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 5.**.** The auth
4b740 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
4b750 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
4b760 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
4b770 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
4b780 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
4b790 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
4b7a0 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
4b7b0 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
4b7c0 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
4b7d0 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
4b7e0 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
4b7f0 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
4b800 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
4b810 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
4b820 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
4b830 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
4b840 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
4b850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4b860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4b870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4b880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4b890 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72 20 .** This header
4b8a0 66 69 6c 65 20 64 65 66 69 6e 65 73 20 74 68 65 file defines the
4b8b0 20 69 6e 74 65 72 66 61 63 65 20 74 68 61 74 20 interface that
4b8c0 74 68 65 20 73 71 6c 69 74 65 20 42 2d 54 72 65 the sqlite B-Tre
4b8d0 65 20 66 69 6c 65 0a 2a 2a 20 73 75 62 73 79 73 e file.** subsys
4b8e0 74 65 6d 2e 20 20 53 65 65 20 63 6f 6d 6d 65 6e tem. See commen
4b8f0 74 73 20 69 6e 20 74 68 65 20 73 6f 75 72 63 65 ts in the source
4b900 20 63 6f 64 65 20 66 6f 72 20 61 20 64 65 74 61 code for a deta
4b910 69 6c 65 64 20 64 65 73 63 72 69 70 74 69 6f 6e iled description
4b920 0a 2a 2a 20 6f 66 20 77 68 61 74 20 65 61 63 68 .** of what each
4b930 20 69 6e 74 65 72 66 61 63 65 20 72 6f 75 74 69 interface routi
4b940 6e 65 20 64 6f 65 73 2e 0a 2a 2a 0a 2a 2a 20 40 ne does..**.** @
4b950 28 23 29 20 24 49 64 3a 20 62 74 72 65 65 2e 68 (#) $Id: btree.h
4b960 2c 76 20 31 2e 31 32 30 20 32 30 30 39 2f 30 37 ,v 1.120 2009/07
4b970 2f 32 32 20 30 30 3a 33 35 3a 32 34 20 64 72 68 /22 00:35:24 drh
4b980 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 Exp $.*/.#ifnde
4b990 66 20 5f 42 54 52 45 45 5f 48 5f 0a 23 64 65 66 f _BTREE_H_.#def
4b9a0 69 6e 65 20 5f 42 54 52 45 45 5f 48 5f 0a 0a 2f ine _BTREE_H_../
4b9b0 2a 20 54 4f 44 4f 3a 20 54 68 69 73 20 64 65 66 * TODO: This def
4b9c0 69 6e 69 74 69 6f 6e 20 69 73 20 6a 75 73 74 20 inition is just
4b9d0 69 6e 63 6c 75 64 65 64 20 73 6f 20 6f 74 68 65 included so othe
4b9e0 72 20 6d 6f 64 75 6c 65 73 20 63 6f 6d 70 69 6c r modules compil
4b9f0 65 2e 20 49 74 0a 2a 2a 20 6e 65 65 64 73 20 74 e. It.** needs t
4ba00 6f 20 62 65 20 72 65 76 69 73 69 74 65 64 2e 0a o be revisited..
4ba10 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
4ba20 45 5f 4e 5f 42 54 52 45 45 5f 4d 45 54 41 20 31 E_N_BTREE_META 1
4ba30 30 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 64 65 66 69 0../*.** If defi
4ba40 6e 65 64 20 61 73 20 6e 6f 6e 2d 7a 65 72 6f 2c ned as non-zero,
4ba50 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 69 73 20 auto-vacuum is
4ba60 65 6e 61 62 6c 65 64 20 62 79 20 64 65 66 61 75 enabled by defau
4ba70 6c 74 2e 20 4f 74 68 65 72 77 69 73 65 0a 2a 2a lt. Otherwise.**
4ba80 20 69 74 20 6d 75 73 74 20 62 65 20 74 75 72 6e it must be turn
4ba90 65 64 20 6f 6e 20 66 6f 72 20 65 61 63 68 20 64 ed on for each d
4baa0 61 74 61 62 61 73 65 20 75 73 69 6e 67 20 22 50 atabase using "P
4bab0 52 41 47 4d 41 20 61 75 74 6f 5f 76 61 63 75 75 RAGMA auto_vacuu
4bac0 6d 20 3d 20 31 22 2e 0a 2a 2f 0a 23 69 66 6e 64 m = 1"..*/.#ifnd
4bad0 65 66 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c ef SQLITE_DEFAUL
4bae0 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 23 T_AUTOVACUUM. #
4baf0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 define SQLITE_DE
4bb00 46 41 55 4c 54 5f 41 55 54 4f 56 41 43 55 55 4d FAULT_AUTOVACUUM
4bb10 20 30 0a 23 65 6e 64 69 66 0a 0a 23 64 65 66 69 0.#endif..#defi
4bb20 6e 65 20 42 54 52 45 45 5f 41 55 54 4f 56 41 43 ne BTREE_AUTOVAC
4bb30 55 55 4d 5f 4e 4f 4e 45 20 30 20 20 20 20 20 20 UUM_NONE 0
4bb40 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 64 6f 20 61 /* Do not do a
4bb50 75 74 6f 2d 76 61 63 75 75 6d 20 2a 2f 0a 23 64 uto-vacuum */.#d
4bb60 65 66 69 6e 65 20 42 54 52 45 45 5f 41 55 54 4f efine BTREE_AUTO
4bb70 56 41 43 55 55 4d 5f 46 55 4c 4c 20 31 20 20 20 VACUUM_FULL 1
4bb80 20 20 20 20 20 2f 2a 20 44 6f 20 66 75 6c 6c 20 /* Do full
4bb90 61 75 74 6f 2d 76 61 63 75 75 6d 20 2a 2f 0a 23 auto-vacuum */.#
4bba0 64 65 66 69 6e 65 20 42 54 52 45 45 5f 41 55 54 define BTREE_AUT
4bbb0 4f 56 41 43 55 55 4d 5f 49 4e 43 52 20 32 20 20 OVACUUM_INCR 2
4bbc0 20 20 20 20 20 20 2f 2a 20 49 6e 63 72 65 6d 65 /* Increme
4bbd0 6e 74 61 6c 20 76 61 63 75 75 6d 20 2a 2f 0a 0a ntal vacuum */..
4bbe0 2f 2a 0a 2a 2a 20 46 6f 72 77 61 72 64 20 64 65 /*.** Forward de
4bbf0 63 6c 61 72 61 74 69 6f 6e 73 20 6f 66 20 73 74 clarations of st
4bc00 72 75 63 74 75 72 65 0a 2a 2f 0a 74 79 70 65 64 ructure.*/.typed
4bc10 65 66 20 73 74 72 75 63 74 20 42 74 72 65 65 20 ef struct Btree
4bc20 42 74 72 65 65 3b 0a 74 79 70 65 64 65 66 20 73 Btree;.typedef s
4bc30 74 72 75 63 74 20 42 74 43 75 72 73 6f 72 20 42 truct BtCursor B
4bc40 74 43 75 72 73 6f 72 3b 0a 74 79 70 65 64 65 66 tCursor;.typedef
4bc50 20 73 74 72 75 63 74 20 42 74 53 68 61 72 65 64 struct BtShared
4bc60 20 42 74 53 68 61 72 65 64 3b 0a 74 79 70 65 64 BtShared;.typed
4bc70 65 66 20 73 74 72 75 63 74 20 42 74 72 65 65 4d ef struct BtreeM
4bc80 75 74 65 78 41 72 72 61 79 20 42 74 72 65 65 4d utexArray BtreeM
4bc90 75 74 65 78 41 72 72 61 79 3b 0a 0a 2f 2a 0a 2a utexArray;../*.*
4bca0 2a 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65 * This structure
4bcb0 20 72 65 63 6f 72 64 73 20 61 6c 6c 20 6f 66 20 records all of
4bcc0 74 68 65 20 42 74 72 65 65 73 20 74 68 61 74 20 the Btrees that
4bcd0 6e 65 65 64 20 74 6f 20 68 6f 6c 64 0a 2a 2a 20 need to hold.**
4bce0 61 20 6d 75 74 65 78 20 62 65 66 6f 72 65 20 77 a mutex before w
4bcf0 65 20 65 6e 74 65 72 20 73 71 6c 69 74 65 33 56 e enter sqlite3V
4bd00 64 62 65 45 78 65 63 28 29 2e 20 20 54 68 65 20 dbeExec(). The
4bd10 42 74 72 65 65 73 20 61 72 65 0a 2a 2a 20 61 72 Btrees are.** ar
4bd20 65 20 70 6c 61 63 65 64 20 69 6e 20 61 42 74 72 e placed in aBtr
4bd30 65 65 5b 5d 20 69 6e 20 6f 72 64 65 72 20 6f 66 ee[] in order of
4bd40 20 61 42 74 72 65 65 5b 5d 2d 3e 70 42 74 2e 20 aBtree[]->pBt.
4bd50 20 54 68 61 74 20 77 61 79 2c 0a 2a 2a 20 77 65 That way,.** we
4bd60 20 63 61 6e 20 61 6c 77 61 79 73 20 6c 6f 63 6b can always lock
4bd70 20 61 6e 64 20 75 6e 6c 6f 63 6b 20 74 68 65 6d and unlock them
4bd80 20 61 6c 6c 20 71 75 69 63 6b 6c 79 2e 0a 2a 2f all quickly..*/
4bd90 0a 73 74 72 75 63 74 20 42 74 72 65 65 4d 75 74 .struct BtreeMut
4bda0 65 78 41 72 72 61 79 20 7b 0a 20 20 69 6e 74 20 exArray {. int
4bdb0 6e 4d 75 74 65 78 3b 0a 20 20 42 74 72 65 65 20 nMutex;. Btree
4bdc0 2a 61 42 74 72 65 65 5b 53 51 4c 49 54 45 5f 4d *aBtree[SQLITE_M
4bdd0 41 58 5f 41 54 54 41 43 48 45 44 2b 31 5d 3b 0a AX_ATTACHED+1];.
4bde0 7d 3b 0a 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 };...SQLITE_PRIV
4bdf0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
4be00 74 72 65 65 4f 70 65 6e 28 0a 20 20 63 6f 6e 73 treeOpen(. cons
4be10 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d t char *zFilenam
4be20 65 2c 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 e, /* Name of
4be30 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 74 6f database file to
4be40 20 6f 70 65 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 open */. sqlit
4be50 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 e3 *db,
4be60 20 20 20 20 2f 2a 20 41 73 73 6f 63 69 61 74 65 /* Associate
4be70 64 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 d database conne
4be80 63 74 69 6f 6e 20 2a 2f 0a 20 20 42 74 72 65 65 ction */. Btree
4be90 20 2a 2a 70 70 42 74 72 65 65 2c 20 20 20 20 20 **ppBtree,
4bea0 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 6f 70 /* Return op
4beb0 65 6e 20 42 74 72 65 65 2a 20 68 65 72 65 20 2a en Btree* here *
4bec0 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 /. int flags,
4bed0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
4bee0 46 6c 61 67 73 20 2a 2f 0a 20 20 69 6e 74 20 76 Flags */. int v
4bef0 66 73 46 6c 61 67 73 20 20 20 20 20 20 20 20 20 fsFlags
4bf00 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 70 61 73 /* Flags pas
4bf10 73 65 64 20 74 68 72 6f 75 67 68 20 74 6f 20 56 sed through to V
4bf20 46 53 20 6f 70 65 6e 20 2a 2f 0a 29 3b 0a 0a 2f FS open */.);../
4bf30 2a 20 54 68 65 20 66 6c 61 67 73 20 70 61 72 61 * The flags para
4bf40 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 meter to sqlite3
4bf50 42 74 72 65 65 4f 70 65 6e 20 63 61 6e 20 62 65 BtreeOpen can be
4bf60 20 74 68 65 20 62 69 74 77 69 73 65 20 6f 72 20 the bitwise or
4bf70 6f 66 20 74 68 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 of the.** follow
4bf80 69 6e 67 20 76 61 6c 75 65 73 2e 0a 2a 2a 0a 2a ing values..**.*
4bf90 2a 20 4e 4f 54 45 3a 20 20 54 68 65 73 65 20 76 * NOTE: These v
4bfa0 61 6c 75 65 73 20 6d 75 73 74 20 6d 61 74 63 68 alues must match
4bfb0 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 the correspondi
4bfc0 6e 67 20 50 41 47 45 52 5f 20 76 61 6c 75 65 73 ng PAGER_ values
4bfd0 20 69 6e 0a 2a 2a 20 70 61 67 65 72 2e 68 2e 0a in.** pager.h..
4bfe0 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 */.#define BTREE
4bff0 5f 4f 4d 49 54 5f 4a 4f 55 52 4e 41 4c 20 20 31 _OMIT_JOURNAL 1
4c000 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 75 73 65 20 /* Do not use
4c010 6a 6f 75 72 6e 61 6c 2e 20 20 4e 6f 20 61 72 67 journal. No arg
4c020 75 6d 65 6e 74 20 2a 2f 0a 23 64 65 66 69 6e 65 ument */.#define
4c030 20 42 54 52 45 45 5f 4e 4f 5f 52 45 41 44 4c 4f BTREE_NO_READLO
4c040 43 4b 20 20 20 32 20 20 2f 2a 20 4f 6d 69 74 20 CK 2 /* Omit
4c050 72 65 61 64 6c 6f 63 6b 73 20 6f 6e 20 72 65 61 readlocks on rea
4c060 64 6f 6e 6c 79 20 66 69 6c 65 73 20 2a 2f 0a 23 donly files */.#
4c070 64 65 66 69 6e 65 20 42 54 52 45 45 5f 4d 45 4d define BTREE_MEM
4c080 4f 52 59 20 20 20 20 20 20 20 20 34 20 20 2f 2a ORY 4 /*
4c090 20 49 6e 2d 6d 65 6d 6f 72 79 20 44 42 2e 20 20 In-memory DB.
4c0a0 4e 6f 20 61 72 67 75 6d 65 6e 74 20 2a 2f 0a 23 No argument */.#
4c0b0 64 65 66 69 6e 65 20 42 54 52 45 45 5f 52 45 41 define BTREE_REA
4c0c0 44 4f 4e 4c 59 20 20 20 20 20 20 38 20 20 2f 2a DONLY 8 /*
4c0d0 20 4f 70 65 6e 20 74 68 65 20 64 61 74 61 62 61 Open the databa
4c0e0 73 65 20 69 6e 20 72 65 61 64 2d 6f 6e 6c 79 20 se in read-only
4c0f0 6d 6f 64 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 mode */.#define
4c100 42 54 52 45 45 5f 52 45 41 44 57 52 49 54 45 20 BTREE_READWRITE
4c110 20 20 20 31 36 20 20 2f 2a 20 4f 70 65 6e 20 66 16 /* Open f
4c120 6f 72 20 62 6f 74 68 20 72 65 61 64 69 6e 67 20 or both reading
4c130 61 6e 64 20 77 72 69 74 69 6e 67 20 2a 2f 0a 23 and writing */.#
4c140 64 65 66 69 6e 65 20 42 54 52 45 45 5f 43 52 45 define BTREE_CRE
4c150 41 54 45 20 20 20 20 20 20 20 33 32 20 20 2f 2a ATE 32 /*
4c160 20 43 72 65 61 74 65 20 74 68 65 20 64 61 74 61 Create the data
4c170 62 61 73 65 20 69 66 20 69 74 20 64 6f 65 73 20 base if it does
4c180 6e 6f 74 20 65 78 69 73 74 20 2a 2f 0a 0a 53 51 not exist */..SQ
4c190 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
4c1a0 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 6f sqlite3BtreeClo
4c1b0 73 65 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 se(Btree*);.SQLI
4c1c0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
4c1d0 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 43 61 qlite3BtreeSetCa
4c1e0 63 68 65 53 69 7a 65 28 42 74 72 65 65 2a 2c 69 cheSize(Btree*,i
4c1f0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
4c200 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
4c210 74 72 65 65 53 65 74 53 61 66 65 74 79 4c 65 76 treeSetSafetyLev
4c220 65 6c 28 42 74 72 65 65 2a 2c 69 6e 74 2c 69 6e el(Btree*,int,in
4c230 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
4c240 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
4c250 72 65 65 53 79 6e 63 44 69 73 61 62 6c 65 64 28 reeSyncDisabled(
4c260 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f Btree*);.SQLITE_
4c270 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
4c280 74 65 33 42 74 72 65 65 53 65 74 50 61 67 65 53 te3BtreeSetPageS
4c290 69 7a 65 28 42 74 72 65 65 20 2a 70 2c 20 69 6e ize(Btree *p, in
4c2a0 74 20 6e 50 61 67 65 73 69 7a 65 2c 20 69 6e 74 t nPagesize, int
4c2b0 20 6e 52 65 73 65 72 76 65 2c 20 69 6e 74 20 65 nReserve, int e
4c2c0 46 69 78 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 Fix);.SQLITE_PRI
4c2d0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
4c2e0 42 74 72 65 65 47 65 74 50 61 67 65 53 69 7a 65 BtreeGetPageSize
4c2f0 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 (Btree*);.SQLITE
4c300 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
4c310 69 74 65 33 42 74 72 65 65 4d 61 78 50 61 67 65 ite3BtreeMaxPage
4c320 43 6f 75 6e 74 28 42 74 72 65 65 2a 2c 69 6e 74 Count(Btree*,int
4c330 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
4c340 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
4c350 65 65 47 65 74 52 65 73 65 72 76 65 28 42 74 72 eeGetReserve(Btr
4c360 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ee*);.SQLITE_PRI
4c370 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
4c380 42 74 72 65 65 53 65 74 41 75 74 6f 56 61 63 75 BtreeSetAutoVacu
4c390 75 6d 28 42 74 72 65 65 20 2a 2c 20 69 6e 74 29 um(Btree *, int)
4c3a0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
4c3b0 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
4c3c0 65 47 65 74 41 75 74 6f 56 61 63 75 75 6d 28 42 eGetAutoVacuum(B
4c3d0 74 72 65 65 20 2a 29 3b 0a 53 51 4c 49 54 45 5f tree *);.SQLITE_
4c3e0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
4c3f0 74 65 33 42 74 72 65 65 42 65 67 69 6e 54 72 61 te3BtreeBeginTra
4c400 6e 73 28 42 74 72 65 65 2a 2c 69 6e 74 29 3b 0a ns(Btree*,int);.
4c410 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
4c420 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 nt sqlite3BtreeC
4c430 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 42 74 ommitPhaseOne(Bt
4c440 72 65 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 ree*, const char
4c450 20 2a 7a 4d 61 73 74 65 72 29 3b 0a 53 51 4c 49 *zMaster);.SQLI
4c460 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
4c470 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 qlite3BtreeCommi
4c480 74 50 68 61 73 65 54 77 6f 28 42 74 72 65 65 2a tPhaseTwo(Btree*
4c490 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
4c4a0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
4c4b0 65 65 43 6f 6d 6d 69 74 28 42 74 72 65 65 2a 29 eeCommit(Btree*)
4c4c0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
4c4d0 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
4c4e0 65 52 6f 6c 6c 62 61 63 6b 28 42 74 72 65 65 2a eRollback(Btree*
4c4f0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
4c500 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
4c510 65 65 42 65 67 69 6e 53 74 6d 74 28 42 74 72 65 eeBeginStmt(Btre
4c520 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f e*,int);.SQLITE_
4c530 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
4c540 74 65 33 42 74 72 65 65 43 72 65 61 74 65 54 61 te3BtreeCreateTa
4c550 62 6c 65 28 42 74 72 65 65 2a 2c 20 69 6e 74 2a ble(Btree*, int*
4c560 2c 20 69 6e 74 20 66 6c 61 67 73 29 3b 0a 53 51 , int flags);.SQ
4c570 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
4c580 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 sqlite3BtreeIsI
4c590 6e 54 72 61 6e 73 28 42 74 72 65 65 2a 29 3b 0a nTrans(Btree*);.
4c5a0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
4c5b0 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 nt sqlite3BtreeI
4c5c0 73 49 6e 52 65 61 64 54 72 61 6e 73 28 42 74 72 sInReadTrans(Btr
4c5d0 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ee*);.SQLITE_PRI
4c5e0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
4c5f0 42 74 72 65 65 49 73 49 6e 42 61 63 6b 75 70 28 BtreeIsInBackup(
4c600 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f Btree*);.SQLITE_
4c610 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 PRIVATE void *sq
4c620 6c 69 74 65 33 42 74 72 65 65 53 63 68 65 6d 61 lite3BtreeSchema
4c630 28 42 74 72 65 65 20 2a 2c 20 69 6e 74 2c 20 76 (Btree *, int, v
4c640 6f 69 64 28 2a 29 28 76 6f 69 64 20 2a 29 29 3b oid(*)(void *));
4c650 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
4c660 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
4c670 53 63 68 65 6d 61 4c 6f 63 6b 65 64 28 42 74 72 SchemaLocked(Btr
4c680 65 65 20 2a 70 42 74 72 65 65 29 3b 0a 53 51 4c ee *pBtree);.SQL
4c690 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
4c6a0 73 71 6c 69 74 65 33 42 74 72 65 65 4c 6f 63 6b sqlite3BtreeLock
4c6b0 54 61 62 6c 65 28 42 74 72 65 65 20 2a 70 42 74 Table(Btree *pBt
4c6c0 72 65 65 2c 20 69 6e 74 20 69 54 61 62 2c 20 75 ree, int iTab, u
4c6d0 38 20 69 73 57 72 69 74 65 4c 6f 63 6b 29 3b 0a 8 isWriteLock);.
4c6e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
4c6f0 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 nt sqlite3BtreeS
4c700 61 76 65 70 6f 69 6e 74 28 42 74 72 65 65 20 2a avepoint(Btree *
4c710 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 0a 53 51 , int, int);..SQ
4c720 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e LITE_PRIVATE con
4c730 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 st char *sqlite3
4c740 42 74 72 65 65 47 65 74 46 69 6c 65 6e 61 6d 65 BtreeGetFilename
4c750 28 42 74 72 65 65 20 2a 29 3b 0a 53 51 4c 49 54 (Btree *);.SQLIT
4c760 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 E_PRIVATE const
4c770 63 68 61 72 20 2a 73 71 6c 69 74 65 33 42 74 72 char *sqlite3Btr
4c780 65 65 47 65 74 4a 6f 75 72 6e 61 6c 6e 61 6d 65 eeGetJournalname
4c790 28 42 74 72 65 65 20 2a 29 3b 0a 53 51 4c 49 54 (Btree *);.SQLIT
4c7a0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
4c7b0 6c 69 74 65 33 42 74 72 65 65 43 6f 70 79 46 69 lite3BtreeCopyFi
4c7c0 6c 65 28 42 74 72 65 65 20 2a 2c 20 42 74 72 65 le(Btree *, Btre
4c7d0 65 20 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 e *);..SQLITE_PR
4c7e0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
4c7f0 33 42 74 72 65 65 49 6e 63 72 56 61 63 75 75 6d 3BtreeIncrVacuum
4c800 28 42 74 72 65 65 20 2a 29 3b 0a 0a 2f 2a 20 54 (Btree *);../* T
4c810 68 65 20 66 6c 61 67 73 20 70 61 72 61 6d 65 74 he flags paramet
4c820 65 72 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72 er to sqlite3Btr
4c830 65 65 43 72 65 61 74 65 54 61 62 6c 65 20 63 61 eeCreateTable ca
4c840 6e 20 62 65 20 74 68 65 20 62 69 74 77 69 73 65 n be the bitwise
4c850 20 4f 52 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f OR.** of the fo
4c860 6c 6c 6f 77 69 6e 67 20 66 6c 61 67 73 3a 0a 2a llowing flags:.*
4c870 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f /.#define BTREE_
4c880 49 4e 54 4b 45 59 20 20 20 20 20 31 20 20 20 20 INTKEY 1
4c890 2f 2a 20 54 61 62 6c 65 20 68 61 73 20 6f 6e 6c /* Table has onl
4c8a0 79 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20 y 64-bit signed
4c8b0 69 6e 74 65 67 65 72 20 6b 65 79 73 20 2a 2f 0a integer keys */.
4c8c0 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 5a 45 #define BTREE_ZE
4c8d0 52 4f 44 41 54 41 20 20 20 32 20 20 20 20 2f 2a RODATA 2 /*
4c8e0 20 54 61 62 6c 65 20 68 61 73 20 6b 65 79 73 20 Table has keys
4c8f0 6f 6e 6c 79 20 2d 20 6e 6f 20 64 61 74 61 20 2a only - no data *
4c900 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f /.#define BTREE_
4c910 4c 45 41 46 44 41 54 41 20 20 20 34 20 20 20 20 LEAFDATA 4
4c920 2f 2a 20 44 61 74 61 20 73 74 6f 72 65 64 20 69 /* Data stored i
4c930 6e 20 6c 65 61 76 65 73 20 6f 6e 6c 79 2e 20 20 n leaves only.
4c940 49 6d 70 6c 69 65 73 20 49 4e 54 4b 45 59 20 2a Implies INTKEY *
4c950 2f 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 /..SQLITE_PRIVAT
4c960 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
4c970 65 65 44 72 6f 70 54 61 62 6c 65 28 42 74 72 65 eeDropTable(Btre
4c980 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2a 29 3b 0a e*, int, int*);.
4c990 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
4c9a0 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 nt sqlite3BtreeC
4c9b0 6c 65 61 72 54 61 62 6c 65 28 42 74 72 65 65 2a learTable(Btree*
4c9c0 2c 20 69 6e 74 2c 20 69 6e 74 2a 29 3b 0a 53 51 , int, int*);.SQ
4c9d0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
4c9e0 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 54 72 d sqlite3BtreeTr
4c9f0 69 70 41 6c 6c 43 75 72 73 6f 72 73 28 42 74 72 ipAllCursors(Btr
4ca00 65 65 2a 2c 20 69 6e 74 29 3b 0a 0a 53 51 4c 49 ee*, int);..SQLI
4ca10 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
4ca20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 4d sqlite3BtreeGetM
4ca30 65 74 61 28 42 74 72 65 65 20 2a 70 42 74 72 65 eta(Btree *pBtre
4ca40 65 2c 20 69 6e 74 20 69 64 78 2c 20 75 33 32 20 e, int idx, u32
4ca50 2a 70 56 61 6c 75 65 29 3b 0a 53 51 4c 49 54 45 *pValue);.SQLITE
4ca60 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
4ca70 69 74 65 33 42 74 72 65 65 55 70 64 61 74 65 4d ite3BtreeUpdateM
4ca80 65 74 61 28 42 74 72 65 65 2a 2c 20 69 6e 74 20 eta(Btree*, int
4ca90 69 64 78 2c 20 75 33 32 20 76 61 6c 75 65 29 3b idx, u32 value);
4caa0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 65 63 6f ../*.** The seco
4cab0 6e 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 nd parameter to
4cac0 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 4d sqlite3BtreeGetM
4cad0 65 74 61 20 6f 72 20 73 71 6c 69 74 65 33 42 74 eta or sqlite3Bt
4cae0 72 65 65 55 70 64 61 74 65 4d 65 74 61 0a 2a 2a reeUpdateMeta.**
4caf0 20 73 68 6f 75 6c 64 20 62 65 20 6f 6e 65 20 6f should be one o
4cb00 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
4cb10 76 61 6c 75 65 73 2e 20 54 68 65 20 69 6e 74 65 values. The inte
4cb20 67 65 72 20 76 61 6c 75 65 73 20 61 72 65 20 61 ger values are a
4cb30 73 73 69 67 6e 65 64 20 0a 2a 2a 20 74 6f 20 63 ssigned .** to c
4cb40 6f 6e 73 74 61 6e 74 73 20 73 6f 20 74 68 61 74 onstants so that
4cb50 20 74 68 65 20 6f 66 66 73 65 74 20 6f 66 20 74 the offset of t
4cb60 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 he corresponding
4cb70 20 66 69 65 6c 64 20 69 6e 20 61 6e 0a 2a 2a 20 field in an.**
4cb80 53 51 4c 69 74 65 20 64 61 74 61 62 61 73 65 20 SQLite database
4cb90 68 65 61 64 65 72 20 6d 61 79 20 62 65 20 66 6f header may be fo
4cba0 75 6e 64 20 75 73 69 6e 67 20 74 68 65 20 66 6f und using the fo
4cbb0 6c 6c 6f 77 69 6e 67 20 66 6f 72 6d 75 6c 61 3a llowing formula:
4cbc0 0a 2a 2a 0a 2a 2a 20 20 20 6f 66 66 73 65 74 20 .**.** offset
4cbd0 3d 20 33 36 20 2b 20 28 69 64 78 20 2a 20 34 29 = 36 + (idx * 4)
4cbe0 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 65 78 61 6d 70 .**.** For examp
4cbf0 6c 65 2c 20 74 68 65 20 66 72 65 65 2d 70 61 67 le, the free-pag
4cc00 65 2d 63 6f 75 6e 74 20 66 69 65 6c 64 20 69 73 e-count field is
4cc10 20 6c 6f 63 61 74 65 64 20 61 74 20 62 79 74 65 located at byte
4cc20 20 6f 66 66 73 65 74 20 33 36 20 6f 66 0a 2a 2a offset 36 of.**
4cc30 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
4cc40 6c 65 20 68 65 61 64 65 72 2e 20 54 68 65 20 69 le header. The i
4cc50 6e 63 72 2d 76 61 63 75 75 6d 2d 66 6c 61 67 20 ncr-vacuum-flag
4cc60 66 69 65 6c 64 20 69 73 20 6c 6f 63 61 74 65 64 field is located
4cc70 20 61 74 0a 2a 2a 20 62 79 74 65 20 6f 66 66 73 at.** byte offs
4cc80 65 74 20 36 34 20 28 3d 3d 20 33 36 2b 34 2a 37 et 64 (== 36+4*7
4cc90 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 )..*/.#define BT
4cca0 52 45 45 5f 46 52 45 45 5f 50 41 47 45 5f 43 4f REE_FREE_PAGE_CO
4ccb0 55 4e 54 20 20 20 20 20 30 0a 23 64 65 66 69 6e UNT 0.#defin
4ccc0 65 20 42 54 52 45 45 5f 53 43 48 45 4d 41 5f 56 e BTREE_SCHEMA_V
4ccd0 45 52 53 49 4f 4e 20 20 20 20 20 20 31 0a 23 64 ERSION 1.#d
4cce0 65 66 69 6e 65 20 42 54 52 45 45 5f 46 49 4c 45 efine BTREE_FILE
4ccf0 5f 46 4f 52 4d 41 54 20 20 20 20 20 20 20 20 20 _FORMAT
4cd00 32 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 2.#define BTREE_
4cd10 44 45 46 41 55 4c 54 5f 43 41 43 48 45 5f 53 49 DEFAULT_CACHE_SI
4cd20 5a 45 20 20 33 0a 23 64 65 66 69 6e 65 20 42 54 ZE 3.#define BT
4cd30 52 45 45 5f 4c 41 52 47 45 53 54 5f 52 4f 4f 54 REE_LARGEST_ROOT
4cd40 5f 50 41 47 45 20 20 20 34 0a 23 64 65 66 69 6e _PAGE 4.#defin
4cd50 65 20 42 54 52 45 45 5f 54 45 58 54 5f 45 4e 43 e BTREE_TEXT_ENC
4cd60 4f 44 49 4e 47 20 20 20 20 20 20 20 35 0a 23 64 ODING 5.#d
4cd70 65 66 69 6e 65 20 42 54 52 45 45 5f 55 53 45 52 efine BTREE_USER
4cd80 5f 56 45 52 53 49 4f 4e 20 20 20 20 20 20 20 20 _VERSION
4cd90 36 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 6.#define BTREE_
4cda0 49 4e 43 52 5f 56 41 43 55 55 4d 20 20 20 20 20 INCR_VACUUM
4cdb0 20 20 20 20 37 0a 0a 53 51 4c 49 54 45 5f 50 52 7..SQLITE_PR
4cdc0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
4cdd0 33 42 74 72 65 65 43 75 72 73 6f 72 28 0a 20 20 3BtreeCursor(.
4cde0 42 74 72 65 65 2a 2c 20 20 20 20 20 20 20 20 20 Btree*,
4cdf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ce00 20 20 20 20 20 2f 2a 20 42 54 72 65 65 20 63 6f /* BTree co
4ce10 6e 74 61 69 6e 69 6e 67 20 74 61 62 6c 65 20 74 ntaining table t
4ce20 6f 20 6f 70 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 o open */. int
4ce30 69 54 61 62 6c 65 2c 20 20 20 20 20 20 20 20 20 iTable,
4ce40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ce50 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 72 6f 6f /* Index of roo
4ce60 74 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 t page */. int
4ce70 77 72 46 6c 61 67 2c 20 20 20 20 20 20 20 20 20 wrFlag,
4ce80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ce90 20 2f 2a 20 31 20 66 6f 72 20 77 72 69 74 69 6e /* 1 for writin
4cea0 67 2e 20 20 30 20 66 6f 72 20 72 65 61 64 2d 6f g. 0 for read-o
4ceb0 6e 6c 79 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 nly */. struct
4cec0 4b 65 79 49 6e 66 6f 2a 2c 20 20 20 20 20 20 20 KeyInfo*,
4ced0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
4cee0 20 46 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 First argument
4cef0 74 6f 20 63 6f 6d 70 61 72 65 20 66 75 6e 63 74 to compare funct
4cf00 69 6f 6e 20 2a 2f 0a 20 20 42 74 43 75 72 73 6f ion */. BtCurso
4cf10 72 20 2a 70 43 75 72 73 6f 72 20 20 20 20 20 20 r *pCursor
4cf20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
4cf30 20 53 70 61 63 65 20 74 6f 20 77 72 69 74 65 20 Space to write
4cf40 63 75 72 73 6f 72 20 73 74 72 75 63 74 75 72 65 cursor structure
4cf50 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 */.);.SQLITE_PR
4cf60 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
4cf70 33 42 74 72 65 65 43 75 72 73 6f 72 53 69 7a 65 3BtreeCursorSize
4cf80 28 76 6f 69 64 29 3b 0a 0a 53 51 4c 49 54 45 5f (void);..SQLITE_
4cf90 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
4cfa0 74 65 33 42 74 72 65 65 43 6c 6f 73 65 43 75 72 te3BtreeCloseCur
4cfb0 73 6f 72 28 42 74 43 75 72 73 6f 72 2a 29 3b 0a sor(BtCursor*);.
4cfc0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
4cfd0 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d nt sqlite3BtreeM
4cfe0 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 0a 20 ovetoUnpacked(.
4cff0 20 42 74 43 75 72 73 6f 72 2a 2c 0a 20 20 55 6e BtCursor*,. Un
4d000 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a 70 55 packedRecord *pU
4d010 6e 4b 65 79 2c 0a 20 20 69 36 34 20 69 6e 74 4b nKey,. i64 intK
4d020 65 79 2c 0a 20 20 69 6e 74 20 62 69 61 73 2c 0a ey,. int bias,.
4d030 20 20 69 6e 74 20 2a 70 52 65 73 0a 29 3b 0a 53 int *pRes.);.S
4d040 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
4d050 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 t sqlite3BtreeCu
4d060 72 73 6f 72 48 61 73 4d 6f 76 65 64 28 42 74 43 rsorHasMoved(BtC
4d070 75 72 73 6f 72 2a 2c 20 69 6e 74 2a 29 3b 0a 53 ursor*, int*);.S
4d080 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
4d090 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 65 t sqlite3BtreeDe
4d0a0 6c 65 74 65 28 42 74 43 75 72 73 6f 72 2a 29 3b lete(BtCursor*);
4d0b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
4d0c0 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
4d0d0 49 6e 73 65 72 74 28 42 74 43 75 72 73 6f 72 2a Insert(BtCursor*
4d0e0 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b , const void *pK
4d0f0 65 79 2c 20 69 36 34 20 6e 4b 65 79 2c 0a 20 20 ey, i64 nKey,.
4d100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d120 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 44 61 74 const void *pDat
4d130 61 2c 20 69 6e 74 20 6e 44 61 74 61 2c 0a 20 20 a, int nData,.
4d140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d160 69 6e 74 20 6e 5a 65 72 6f 2c 20 69 6e 74 20 62 int nZero, int b
4d170 69 61 73 2c 20 69 6e 74 20 73 65 65 6b 52 65 73 ias, int seekRes
4d180 75 6c 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ult);.SQLITE_PRI
4d190 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
4d1a0 42 74 72 65 65 46 69 72 73 74 28 42 74 43 75 72 BtreeFirst(BtCur
4d1b0 73 6f 72 2a 2c 20 69 6e 74 20 2a 70 52 65 73 29 sor*, int *pRes)
4d1c0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
4d1d0 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
4d1e0 65 4c 61 73 74 28 42 74 43 75 72 73 6f 72 2a 2c eLast(BtCursor*,
4d1f0 20 69 6e 74 20 2a 70 52 65 73 29 3b 0a 53 51 4c int *pRes);.SQL
4d200 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
4d210 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65 78 74 sqlite3BtreeNext
4d220 28 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 20 (BtCursor*, int
4d230 2a 70 52 65 73 29 3b 0a 53 51 4c 49 54 45 5f 50 *pRes);.SQLITE_P
4d240 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
4d250 65 33 42 74 72 65 65 45 6f 66 28 42 74 43 75 72 e3BtreeEof(BtCur
4d260 73 6f 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 sor*);.SQLITE_PR
4d270 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
4d280 33 42 74 72 65 65 50 72 65 76 69 6f 75 73 28 42 3BtreePrevious(B
4d290 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 20 2a 70 tCursor*, int *p
4d2a0 52 65 73 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 Res);.SQLITE_PRI
4d2b0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
4d2c0 42 74 72 65 65 4b 65 79 53 69 7a 65 28 42 74 43 BtreeKeySize(BtC
4d2d0 75 72 73 6f 72 2a 2c 20 69 36 34 20 2a 70 53 69 ursor*, i64 *pSi
4d2e0 7a 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 ze);.SQLITE_PRIV
4d2f0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
4d300 74 72 65 65 4b 65 79 28 42 74 43 75 72 73 6f 72 treeKey(BtCursor
4d310 2a 2c 20 75 33 32 20 6f 66 66 73 65 74 2c 20 75 *, u32 offset, u
4d320 33 32 20 61 6d 74 2c 20 76 6f 69 64 2a 29 3b 0a 32 amt, void*);.
4d330 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 SQLITE_PRIVATE c
4d340 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 onst void *sqlit
4d350 65 33 42 74 72 65 65 4b 65 79 46 65 74 63 68 28 e3BtreeKeyFetch(
4d360 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 20 2a BtCursor*, int *
4d370 70 41 6d 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 pAmt);.SQLITE_PR
4d380 49 56 41 54 45 20 63 6f 6e 73 74 20 76 6f 69 64 IVATE const void
4d390 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 *sqlite3BtreeDa
4d3a0 74 61 46 65 74 63 68 28 42 74 43 75 72 73 6f 72 taFetch(BtCursor
4d3b0 2a 2c 20 69 6e 74 20 2a 70 41 6d 74 29 3b 0a 53 *, int *pAmt);.S
4d3c0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
4d3d0 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 t sqlite3BtreeDa
4d3e0 74 61 53 69 7a 65 28 42 74 43 75 72 73 6f 72 2a taSize(BtCursor*
4d3f0 2c 20 75 33 32 20 2a 70 53 69 7a 65 29 3b 0a 53 , u32 *pSize);.S
4d400 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
4d410 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 t sqlite3BtreeDa
4d420 74 61 28 42 74 43 75 72 73 6f 72 2a 2c 20 75 33 ta(BtCursor*, u3
4d430 32 20 6f 66 66 73 65 74 2c 20 75 33 32 20 61 6d 2 offset, u32 am
4d440 74 2c 20 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 t, void*);.SQLIT
4d450 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
4d460 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 43 61 qlite3BtreeSetCa
4d470 63 68 65 64 52 6f 77 69 64 28 42 74 43 75 72 73 chedRowid(BtCurs
4d480 6f 72 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 or*, sqlite3_int
4d490 36 34 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 64);.SQLITE_PRIV
4d4a0 41 54 45 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 ATE sqlite3_int6
4d4b0 34 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 4 sqlite3BtreeGe
4d4c0 74 43 61 63 68 65 64 52 6f 77 69 64 28 42 74 43 tCachedRowid(BtC
4d4d0 75 72 73 6f 72 2a 29 3b 0a 0a 53 51 4c 49 54 45 ursor*);..SQLITE
4d4e0 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 _PRIVATE char *s
4d4f0 71 6c 69 74 65 33 42 74 72 65 65 49 6e 74 65 67 qlite3BtreeInteg
4d500 72 69 74 79 43 68 65 63 6b 28 42 74 72 65 65 2a rityCheck(Btree*
4d510 2c 20 69 6e 74 20 2a 61 52 6f 6f 74 2c 20 69 6e , int *aRoot, in
4d520 74 20 6e 52 6f 6f 74 2c 20 69 6e 74 2c 20 69 6e t nRoot, int, in
4d530 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 t*);.SQLITE_PRIV
4d540 41 54 45 20 73 74 72 75 63 74 20 50 61 67 65 72 ATE struct Pager
4d550 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 *sqlite3BtreePa
4d560 67 65 72 28 42 74 72 65 65 2a 29 3b 0a 0a 53 51 ger(Btree*);..SQ
4d570 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
4d580 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 75 74 sqlite3BtreePut
4d590 44 61 74 61 28 42 74 43 75 72 73 6f 72 2a 2c 20 Data(BtCursor*,
4d5a0 75 33 32 20 6f 66 66 73 65 74 2c 20 75 33 32 20 u32 offset, u32
4d5b0 61 6d 74 2c 20 76 6f 69 64 2a 29 3b 0a 53 51 4c amt, void*);.SQL
4d5c0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
4d5d0 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 61 63 sqlite3BtreeCac
4d5e0 68 65 4f 76 65 72 66 6c 6f 77 28 42 74 43 75 72 heOverflow(BtCur
4d5f0 73 6f 72 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 sor *);.SQLITE_P
4d600 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
4d610 74 65 33 42 74 72 65 65 43 6c 65 61 72 43 75 72 te3BtreeClearCur
4d620 73 6f 72 28 42 74 43 75 72 73 6f 72 20 2a 29 3b sor(BtCursor *);
4d630 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 ..#ifndef NDEBUG
4d640 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
4d650 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
4d660 43 75 72 73 6f 72 49 73 56 61 6c 69 64 28 42 74 CursorIsValid(Bt
4d670 43 75 72 73 6f 72 2a 29 3b 0a 23 65 6e 64 69 66 Cursor*);.#endif
4d680 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
4d690 5f 4f 4d 49 54 5f 42 54 52 45 45 43 4f 55 4e 54 _OMIT_BTREECOUNT
4d6a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
4d6b0 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
4d6c0 43 6f 75 6e 74 28 42 74 43 75 72 73 6f 72 20 2a Count(BtCursor *
4d6d0 2c 20 69 36 34 20 2a 29 3b 0a 23 65 6e 64 69 66 , i64 *);.#endif
4d6e0 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ..#ifdef SQLITE_
4d6f0 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 TEST.SQLITE_PRIV
4d700 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
4d710 74 72 65 65 43 75 72 73 6f 72 49 6e 66 6f 28 42 treeCursorInfo(B
4d720 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 2a 2c 20 tCursor*, int*,
4d730 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
4d740 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
4d750 33 42 74 72 65 65 43 75 72 73 6f 72 4c 69 73 74 3BtreeCursorList
4d760 28 42 74 72 65 65 2a 29 3b 0a 23 65 6e 64 69 66 (Btree*);.#endif
4d770 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 61 72 ../*.** If we ar
4d780 65 20 6e 6f 74 20 75 73 69 6e 67 20 73 68 61 72 e not using shar
4d790 65 64 20 63 61 63 68 65 2c 20 74 68 65 6e 20 74 ed cache, then t
4d7a0 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 20 here is no need
4d7b0 74 6f 0a 2a 2a 20 75 73 65 20 6d 75 74 65 78 65 to.** use mutexe
4d7c0 73 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 20 s to access the
4d7d0 42 74 53 68 61 72 65 64 20 73 74 72 75 63 74 75 BtShared structu
4d7e0 72 65 73 2e 20 20 53 6f 20 6d 61 6b 65 20 74 68 res. So make th
4d7f0 65 0a 2a 2a 20 45 6e 74 65 72 20 61 6e 64 20 4c e.** Enter and L
4d800 65 61 76 65 20 70 72 6f 63 65 64 75 72 65 73 20 eave procedures
4d810 6e 6f 2d 6f 70 73 2e 0a 2a 2f 0a 23 69 66 6e 64 no-ops..*/.#ifnd
4d820 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 ef SQLITE_OMIT_S
4d830 48 41 52 45 44 5f 43 41 43 48 45 0a 53 51 4c 49 HARED_CACHE.SQLI
4d840 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 TE_PRIVATE voi
4d850 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e d sqlite3BtreeEn
4d860 74 65 72 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c ter(Btree*);.SQL
4d870 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f ITE_PRIVATE vo
4d880 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 id sqlite3BtreeE
4d890 6e 74 65 72 41 6c 6c 28 73 71 6c 69 74 65 33 2a nterAll(sqlite3*
4d8a0 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e );.#else.# defin
4d8b0 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e e sqlite3BtreeEn
4d8c0 74 65 72 28 58 29 20 0a 23 20 64 65 66 69 6e 65 ter(X) .# define
4d8d0 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 sqlite3BtreeEnt
4d8e0 65 72 41 6c 6c 28 58 29 0a 23 65 6e 64 69 66 0a erAll(X).#endif.
4d8f0 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 .#if !defined(SQ
4d900 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 LITE_OMIT_SHARED
4d910 5f 43 41 43 48 45 29 20 26 26 20 53 51 4c 49 54 _CACHE) && SQLIT
4d920 45 5f 54 48 52 45 41 44 53 41 46 45 0a 53 51 4c E_THREADSAFE.SQL
4d930 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f ITE_PRIVATE vo
4d940 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c id sqlite3BtreeL
4d950 65 61 76 65 28 42 74 72 65 65 2a 29 3b 0a 53 51 eave(Btree*);.SQ
4d960 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 LITE_PRIVATE v
4d970 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 oid sqlite3Btree
4d980 45 6e 74 65 72 43 75 72 73 6f 72 28 42 74 43 75 EnterCursor(BtCu
4d990 72 73 6f 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 rsor*);.SQLITE_P
4d9a0 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 RIVATE void sq
4d9b0 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 43 lite3BtreeLeaveC
4d9c0 75 72 73 6f 72 28 42 74 43 75 72 73 6f 72 2a 29 ursor(BtCursor*)
4d9d0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
4d9e0 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 void sqlite3B
4d9f0 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 73 71 6c treeLeaveAll(sql
4da00 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 ite3*);.SQLITE_P
4da10 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 RIVATE void sq
4da20 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 41 lite3BtreeMutexA
4da30 72 72 61 79 45 6e 74 65 72 28 42 74 72 65 65 4d rrayEnter(BtreeM
4da40 75 74 65 78 41 72 72 61 79 2a 29 3b 0a 53 51 4c utexArray*);.SQL
4da50 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f ITE_PRIVATE vo
4da60 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d id sqlite3BtreeM
4da70 75 74 65 78 41 72 72 61 79 4c 65 61 76 65 28 42 utexArrayLeave(B
4da80 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 2a 29 treeMutexArray*)
4da90 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
4daa0 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 void sqlite3B
4dab0 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 49 6e treeMutexArrayIn
4dac0 73 65 72 74 28 42 74 72 65 65 4d 75 74 65 78 41 sert(BtreeMutexA
4dad0 72 72 61 79 2a 2c 20 42 74 72 65 65 2a 29 3b 0a rray*, Btree*);.
4dae0 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 #ifndef NDEBUG.
4daf0 20 2f 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e /* These routin
4db00 65 73 20 61 72 65 20 75 73 65 64 20 69 6e 73 69 es are used insi
4db10 64 65 20 61 73 73 65 72 74 28 29 20 73 74 61 74 de assert() stat
4db20 65 6d 65 6e 74 73 20 6f 6e 6c 79 2e 20 2a 2f 0a ements only. */.
4db30 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
4db40 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
4db50 65 48 6f 6c 64 73 4d 75 74 65 78 28 42 74 72 65 eHoldsMutex(Btre
4db60 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 e*);.SQLITE_PRIV
4db70 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 ATE int sqlite
4db80 33 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 3BtreeHoldsAllMu
4db90 74 65 78 65 73 28 73 71 6c 69 74 65 33 2a 29 3b texes(sqlite3*);
4dba0 0a 23 65 6e 64 69 66 0a 23 65 6c 73 65 0a 0a 23 .#endif.#else..#
4dbb0 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42 define sqlite3B
4dbc0 74 72 65 65 4c 65 61 76 65 28 58 29 0a 23 20 64 treeLeave(X).# d
4dbd0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42 74 72 efine sqlite3Btr
4dbe0 65 65 45 6e 74 65 72 43 75 72 73 6f 72 28 58 29 eeEnterCursor(X)
4dbf0 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 .# define sqlite
4dc00 33 42 74 72 65 65 4c 65 61 76 65 43 75 72 73 6f 3BtreeLeaveCurso
4dc10 72 28 58 29 0a 23 20 64 65 66 69 6e 65 20 73 71 r(X).# define sq
4dc20 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 41 lite3BtreeLeaveA
4dc30 6c 6c 28 58 29 0a 23 20 64 65 66 69 6e 65 20 73 ll(X).# define s
4dc40 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 qlite3BtreeMutex
4dc50 41 72 72 61 79 45 6e 74 65 72 28 58 29 0a 23 20 ArrayEnter(X).#
4dc60 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42 74 define sqlite3Bt
4dc70 72 65 65 4d 75 74 65 78 41 72 72 61 79 4c 65 61 reeMutexArrayLea
4dc80 76 65 28 58 29 0a 23 20 64 65 66 69 6e 65 20 73 ve(X).# define s
4dc90 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 qlite3BtreeMutex
4dca0 41 72 72 61 79 49 6e 73 65 72 74 28 58 2c 59 29 ArrayInsert(X,Y)
4dcb0 0a 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 ..# define sqlit
4dcc0 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 e3BtreeHoldsMute
4dcd0 78 28 58 29 20 31 0a 23 20 64 65 66 69 6e 65 20 x(X) 1.# define
4dce0 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 sqlite3BtreeHold
4dcf0 73 41 6c 6c 4d 75 74 65 78 65 73 28 58 29 20 31 sAllMutexes(X) 1
4dd00 0a 23 65 6e 64 69 66 0a 0a 0a 23 65 6e 64 69 66 .#endif...#endif
4dd10 20 2f 2a 20 5f 42 54 52 45 45 5f 48 5f 20 2a 2f /* _BTREE_H_ */
4dd20 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
4dd30 2a 20 45 6e 64 20 6f 66 20 62 74 72 65 65 2e 68 * End of btree.h
4dd40 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
4dd50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4dd60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4dd70 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
4dd80 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 * Continuing whe
4dd90 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 re we left off i
4dda0 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a n sqliteInt.h **
4ddb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4ddc0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
4ddd0 2a 20 49 6e 63 6c 75 64 65 20 76 64 62 65 2e 68 * Include vdbe.h
4dde0 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f in the middle o
4ddf0 66 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a f sqliteInt.h **
4de00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4de10 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
4de20 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 76 64 62 * Begin file vdb
4de30 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e.h ************
4de40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4de50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4de60 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 /./*.** 2001 Sep
4de70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 tember 15.**.**
4de80 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c The author discl
4de90 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 aims copyright t
4dea0 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f o this source co
4deb0 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 de. In place of
4dec0 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 .** a legal noti
4ded0 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c ce, here is a bl
4dee0 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 essing:.**.**
4def0 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 May you do good
4df00 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a and not evil..*
4df10 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e * May you fin
4df20 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f d forgiveness fo
4df30 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 r yourself and f
4df40 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a orgive others..*
4df50 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 * May you sha
4df60 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 re freely, never
4df70 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 taking more tha
4df80 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a n you give..**.*
4df90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4dfa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4dfb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4dfc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4dfd0 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 48 65 61 64 ********.** Head
4dfe0 65 72 20 66 69 6c 65 20 66 6f 72 20 74 68 65 20 er file for the
4dff0 56 69 72 74 75 61 6c 20 44 61 74 61 42 61 73 65 Virtual DataBase
4e000 20 45 6e 67 69 6e 65 20 28 56 44 42 45 29 0a 2a Engine (VDBE).*
4e010 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72 *.** This header
4e020 20 64 65 66 69 6e 65 73 20 74 68 65 20 69 6e 74 defines the int
4e030 65 72 66 61 63 65 20 74 6f 20 74 68 65 20 76 69 erface to the vi
4e040 72 74 75 61 6c 20 64 61 74 61 62 61 73 65 20 65 rtual database e
4e050 6e 67 69 6e 65 0a 2a 2a 20 6f 72 20 56 44 42 45 ngine.** or VDBE
4e060 2e 20 20 54 68 65 20 56 44 42 45 20 69 6d 70 6c . The VDBE impl
4e070 65 6d 65 6e 74 73 20 61 6e 20 61 62 73 74 72 61 ements an abstra
4e080 63 74 20 6d 61 63 68 69 6e 65 20 74 68 61 74 20 ct machine that
4e090 72 75 6e 73 20 61 0a 2a 2a 20 73 69 6d 70 6c 65 runs a.** simple
4e0a0 20 70 72 6f 67 72 61 6d 20 74 6f 20 61 63 63 65 program to acce
4e0b0 73 73 20 61 6e 64 20 6d 6f 64 69 66 79 20 74 68 ss and modify th
4e0c0 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 64 61 74 e underlying dat
4e0d0 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 abase..**.** $Id
4e0e0 3a 20 76 64 62 65 2e 68 2c 76 20 31 2e 31 34 32 : vdbe.h,v 1.142
4e0f0 20 32 30 30 39 2f 30 37 2f 32 34 20 31 37 3a 35 2009/07/24 17:5
4e100 38 3a 35 33 20 64 61 6e 69 65 6c 6b 31 39 37 37 8:53 danielk1977
4e110 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 Exp $.*/.#ifnde
4e120 66 20 5f 53 51 4c 49 54 45 5f 56 44 42 45 5f 48 f _SQLITE_VDBE_H
4e130 5f 0a 23 64 65 66 69 6e 65 20 5f 53 51 4c 49 54 _.#define _SQLIT
4e140 45 5f 56 44 42 45 5f 48 5f 0a 0a 2f 2a 0a 2a 2a E_VDBE_H_../*.**
4e150 20 41 20 73 69 6e 67 6c 65 20 56 44 42 45 20 69 A single VDBE i
4e160 73 20 61 6e 20 6f 70 61 71 75 65 20 73 74 72 75 s an opaque stru
4e170 63 74 75 72 65 20 6e 61 6d 65 64 20 22 56 64 62 cture named "Vdb
4e180 65 22 2e 20 20 4f 6e 6c 79 20 72 6f 75 74 69 6e e". Only routin
4e190 65 73 0a 2a 2a 20 69 6e 20 74 68 65 20 73 6f 75 es.** in the sou
4e1a0 72 63 65 20 66 69 6c 65 20 73 71 6c 69 74 65 56 rce file sqliteV
4e1b0 64 62 65 2e 63 20 61 72 65 20 61 6c 6c 6f 77 65 dbe.c are allowe
4e1c0 64 20 74 6f 20 73 65 65 20 74 68 65 20 69 6e 73 d to see the ins
4e1d0 69 64 65 73 0a 2a 2a 20 6f 66 20 74 68 69 73 20 ides.** of this
4e1e0 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 74 79 structure..*/.ty
4e1f0 70 65 64 65 66 20 73 74 72 75 63 74 20 56 64 62 pedef struct Vdb
4e200 65 20 56 64 62 65 3b 0a 0a 2f 2a 0a 2a 2a 20 54 e Vdbe;../*.** T
4e210 68 65 20 6e 61 6d 65 73 20 6f 66 20 74 68 65 20 he names of the
4e220 66 6f 6c 6c 6f 77 69 6e 67 20 74 79 70 65 73 20 following types
4e230 64 65 63 6c 61 72 65 64 20 69 6e 20 76 64 62 65 declared in vdbe
4e240 49 6e 74 2e 68 20 61 72 65 20 72 65 71 75 69 72 Int.h are requir
4e250 65 64 0a 2a 2a 20 66 6f 72 20 74 68 65 20 56 64 ed.** for the Vd
4e260 62 65 4f 70 20 64 65 66 69 6e 69 74 69 6f 6e 2e beOp definition.
4e270 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 .*/.typedef stru
4e280 63 74 20 56 64 62 65 46 75 6e 63 20 56 64 62 65 ct VdbeFunc Vdbe
4e290 46 75 6e 63 3b 0a 74 79 70 65 64 65 66 20 73 74 Func;.typedef st
4e2a0 72 75 63 74 20 4d 65 6d 20 4d 65 6d 3b 0a 74 79 ruct Mem Mem;.ty
4e2b0 70 65 64 65 66 20 73 74 72 75 63 74 20 53 75 62 pedef struct Sub
4e2c0 50 72 6f 67 72 61 6d 20 53 75 62 50 72 6f 67 72 Program SubProgr
4e2d0 61 6d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 69 6e am;../*.** A sin
4e2e0 67 6c 65 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 gle instruction
4e2f0 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d of the virtual m
4e300 61 63 68 69 6e 65 20 68 61 73 20 61 6e 20 6f 70 achine has an op
4e310 63 6f 64 65 0a 2a 2a 20 61 6e 64 20 61 73 20 6d code.** and as m
4e320 61 6e 79 20 61 73 20 74 68 72 65 65 20 6f 70 65 any as three ope
4e330 72 61 6e 64 73 2e 20 20 54 68 65 20 69 6e 73 74 rands. The inst
4e340 72 75 63 74 69 6f 6e 20 69 73 20 72 65 63 6f 72 ruction is recor
4e350 64 65 64 0a 2a 2a 20 61 73 20 61 6e 20 69 6e 73 ded.** as an ins
4e360 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c tance of the fol
4e370 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 lowing structure
4e380 3a 0a 2a 2f 0a 73 74 72 75 63 74 20 56 64 62 65 :.*/.struct Vdbe
4e390 4f 70 20 7b 0a 20 20 75 38 20 6f 70 63 6f 64 65 Op {. u8 opcode
4e3a0 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 68 ; /* Wh
4e3b0 61 74 20 6f 70 65 72 61 74 69 6f 6e 20 74 6f 20 at operation to
4e3c0 70 65 72 66 6f 72 6d 20 2a 2f 0a 20 20 73 69 67 perform */. sig
4e3d0 6e 65 64 20 63 68 61 72 20 70 34 74 79 70 65 3b ned char p4type;
4e3e0 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 50 /* One of the P
4e3f0 34 5f 78 78 78 20 63 6f 6e 73 74 61 6e 74 73 20 4_xxx constants
4e400 66 6f 72 20 70 34 20 2a 2f 0a 20 20 75 38 20 6f for p4 */. u8 o
4e410 70 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 pflags;
4e420 2f 2a 20 4e 6f 74 20 63 75 72 72 65 6e 74 6c 79 /* Not currently
4e430 20 75 73 65 64 20 2a 2f 0a 20 20 75 38 20 70 35 used */. u8 p5
4e440 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
4e450 2a 20 46 69 66 74 68 20 70 61 72 61 6d 65 74 65 * Fifth paramete
4e460 72 20 69 73 20 61 6e 20 75 6e 73 69 67 6e 65 64 r is an unsigned
4e470 20 63 68 61 72 61 63 74 65 72 20 2a 2f 0a 20 20 character */.
4e480 69 6e 74 20 70 31 3b 20 20 20 20 20 20 20 20 20 int p1;
4e490 20 20 20 20 2f 2a 20 46 69 72 73 74 20 6f 70 65 /* First ope
4e4a0 72 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 70 32 rand */. int p2
4e4b0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
4e4c0 20 53 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 Second paramete
4e4d0 72 20 28 6f 66 74 65 6e 20 74 68 65 20 6a 75 6d r (often the jum
4e4e0 70 20 64 65 73 74 69 6e 61 74 69 6f 6e 29 20 2a p destination) *
4e4f0 2f 0a 20 20 69 6e 74 20 70 33 3b 20 20 20 20 20 /. int p3;
4e500 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 /* The t
4e510 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 20 2a hird parameter *
4e520 2f 0a 20 20 75 6e 69 6f 6e 20 7b 20 20 20 20 20 /. union {
4e530 20 20 20 20 20 20 20 20 2f 2a 20 66 6f 75 72 74 /* fourt
4e540 68 20 70 61 72 61 6d 65 74 65 72 20 2a 2f 0a 20 h parameter */.
4e550 20 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 int i;
4e560 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 74 /* Int
4e570 65 67 65 72 20 76 61 6c 75 65 20 69 66 20 70 34 eger value if p4
4e580 74 79 70 65 3d 3d 50 34 5f 49 4e 54 33 32 20 2a type==P4_INT32 *
4e590 2f 0a 20 20 20 20 76 6f 69 64 20 2a 70 3b 20 20 /. void *p;
4e5a0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
4e5b0 47 65 6e 65 72 69 63 20 70 6f 69 6e 74 65 72 20 Generic pointer
4e5c0 2a 2f 0a 20 20 20 20 63 68 61 72 20 2a 7a 3b 20 */. char *z;
4e5d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
4e5e0 20 50 6f 69 6e 74 65 72 20 74 6f 20 64 61 74 61 Pointer to data
4e5f0 20 66 6f 72 20 73 74 72 69 6e 67 20 28 63 68 61 for string (cha
4e600 72 20 61 72 72 61 79 29 20 74 79 70 65 73 20 2a r array) types *
4e610 2f 0a 20 20 20 20 69 36 34 20 2a 70 49 36 34 3b /. i64 *pI64;
4e620 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
4e630 55 73 65 64 20 77 68 65 6e 20 70 34 74 79 70 65 Used when p4type
4e640 20 69 73 20 50 34 5f 49 4e 54 36 34 20 2a 2f 0a is P4_INT64 */.
4e650 20 20 20 20 64 6f 75 62 6c 65 20 2a 70 52 65 61 double *pRea
4e660 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 l; /* Us
4e670 65 64 20 77 68 65 6e 20 70 34 74 79 70 65 20 69 ed when p4type i
4e680 73 20 50 34 5f 52 45 41 4c 20 2a 2f 0a 20 20 20 s P4_REAL */.
4e690 20 46 75 6e 63 44 65 66 20 2a 70 46 75 6e 63 3b FuncDef *pFunc;
4e6a0 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 /* Used
4e6b0 77 68 65 6e 20 70 34 74 79 70 65 20 69 73 20 50 when p4type is P
4e6c0 34 5f 46 55 4e 43 44 45 46 20 2a 2f 0a 20 20 20 4_FUNCDEF */.
4e6d0 20 56 64 62 65 46 75 6e 63 20 2a 70 56 64 62 65 VdbeFunc *pVdbe
4e6e0 46 75 6e 63 3b 20 20 20 2f 2a 20 55 73 65 64 20 Func; /* Used
4e6f0 77 68 65 6e 20 70 34 74 79 70 65 20 69 73 20 50 when p4type is P
4e700 34 5f 56 44 42 45 46 55 4e 43 20 2a 2f 0a 20 20 4_VDBEFUNC */.
4e710 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c CollSeq *pColl
4e720 3b 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 ; /* Used
4e730 20 77 68 65 6e 20 70 34 74 79 70 65 20 69 73 20 when p4type is
4e740 50 34 5f 43 4f 4c 4c 53 45 51 20 2a 2f 0a 20 20 P4_COLLSEQ */.
4e750 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 20 20 20 20 Mem *pMem;
4e760 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 /* Used
4e770 20 77 68 65 6e 20 70 34 74 79 70 65 20 69 73 20 when p4type is
4e780 50 34 5f 4d 45 4d 20 2a 2f 0a 20 20 20 20 56 54 P4_MEM */. VT
4e790 61 62 6c 65 20 2a 70 56 74 61 62 3b 20 20 20 20 able *pVtab;
4e7a0 20 20 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65 /* Used whe
4e7b0 6e 20 70 34 74 79 70 65 20 69 73 20 50 34 5f 56 n p4type is P4_V
4e7c0 54 41 42 20 2a 2f 0a 20 20 20 20 4b 65 79 49 6e TAB */. KeyIn
4e7d0 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 20 20 20 fo *pKeyInfo;
4e7e0 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 70 /* Used when p
4e7f0 34 74 79 70 65 20 69 73 20 50 34 5f 4b 45 59 49 4type is P4_KEYI
4e800 4e 46 4f 20 2a 2f 0a 20 20 20 20 69 6e 74 20 2a NFO */. int *
4e810 61 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ai;
4e820 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 70 /* Used when p
4e830 34 74 79 70 65 20 69 73 20 50 34 5f 49 4e 54 41 4type is P4_INTA
4e840 52 52 41 59 20 2a 2f 0a 20 20 20 20 53 75 62 50 RRAY */. SubP
4e850 72 6f 67 72 61 6d 20 2a 70 50 72 6f 67 72 61 6d rogram *pProgram
4e860 3b 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 ; /* Used when
4e870 70 34 74 79 70 65 20 69 73 20 50 34 5f 53 55 42 p4type is P4_SUB
4e880 50 52 4f 47 52 41 4d 20 2a 2f 0a 20 20 7d 20 70 PROGRAM */. } p
4e890 34 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 4;.#ifdef SQLITE
4e8a0 5f 44 45 42 55 47 0a 20 20 63 68 61 72 20 2a 7a _DEBUG. char *z
4e8b0 43 6f 6d 6d 65 6e 74 3b 20 20 20 20 20 20 20 20 Comment;
4e8c0 20 20 2f 2a 20 43 6f 6d 6d 65 6e 74 20 74 6f 20 /* Comment to
4e8d0 69 6d 70 72 6f 76 65 20 72 65 61 64 61 62 69 6c improve readabil
4e8e0 69 74 79 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 ity */.#endif.#i
4e8f0 66 64 65 66 20 56 44 42 45 5f 50 52 4f 46 49 4c fdef VDBE_PROFIL
4e900 45 0a 20 20 69 6e 74 20 63 6e 74 3b 20 20 20 20 E. int cnt;
4e910 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
4e920 4e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 Number of times
4e930 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e this instruction
4e940 20 77 61 73 20 65 78 65 63 75 74 65 64 20 2a 2f was executed */
4e950 0a 20 20 75 36 34 20 63 79 63 6c 65 73 3b 20 20 . u64 cycles;
4e960 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
4e970 6f 74 61 6c 20 74 69 6d 65 20 73 70 65 6e 74 20 otal time spent
4e980 65 78 65 63 75 74 69 6e 67 20 74 68 69 73 20 69 executing this i
4e990 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 23 65 nstruction */.#e
4e9a0 6e 64 69 66 0a 7d 3b 0a 74 79 70 65 64 65 66 20 ndif.};.typedef
4e9b0 73 74 72 75 63 74 20 56 64 62 65 4f 70 20 56 64 struct VdbeOp Vd
4e9c0 62 65 4f 70 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 41 20 beOp;.../*.** A
4e9d0 73 75 62 2d 72 6f 75 74 69 6e 65 20 75 73 65 64 sub-routine used
4e9e0 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 61 20 to implement a
4e9f0 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 2e trigger program.
4ea00 0a 2a 2f 0a 73 74 72 75 63 74 20 53 75 62 50 72 .*/.struct SubPr
4ea10 6f 67 72 61 6d 20 7b 0a 20 20 56 64 62 65 4f 70 ogram {. VdbeOp
4ea20 20 2a 61 4f 70 3b 20 20 20 20 20 20 20 20 20 20 *aOp;
4ea30 20 20 20 20 20 20 20 20 2f 2a 20 41 72 72 61 79 /* Array
4ea40 20 6f 66 20 6f 70 63 6f 64 65 73 20 66 6f 72 20 of opcodes for
4ea50 73 75 62 2d 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 sub-program */.
4ea60 20 69 6e 74 20 6e 4f 70 3b 20 20 20 20 20 20 20 int nOp;
4ea70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
4ea80 2a 20 45 6c 65 6d 65 6e 74 73 20 69 6e 20 61 4f * Elements in aO
4ea90 70 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 65 p[] */. int nMe
4eaa0 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 m;
4eab0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
4eac0 20 6f 66 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 of memory cells
4ead0 20 72 65 71 75 69 72 65 64 20 2a 2f 0a 20 20 69 required */. i
4eae0 6e 74 20 6e 43 73 72 3b 20 20 20 20 20 20 20 20 nt nCsr;
4eaf0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
4eb00 4e 75 6d 62 65 72 20 6f 66 20 63 75 72 73 6f 72 Number of cursor
4eb10 73 20 72 65 71 75 69 72 65 64 20 2a 2f 0a 20 20 s required */.
4eb20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20 20 int nRef;
4eb30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
4eb40 20 4e 75 6d 62 65 72 20 6f 66 20 70 6f 69 6e 74 Number of point
4eb50 65 72 73 20 74 6f 20 74 68 69 73 20 73 74 72 75 ers to this stru
4eb60 63 74 75 72 65 20 2a 2f 0a 20 20 76 6f 69 64 20 cture */. void
4eb70 2a 74 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20 20 *token;
4eb80 20 20 20 20 20 20 20 20 20 2f 2a 20 69 64 20 74 /* id t
4eb90 68 61 74 20 6d 61 79 20 62 65 20 75 73 65 64 20 hat may be used
4eba0 74 6f 20 72 65 63 75 72 73 69 76 65 20 74 72 69 to recursive tri
4ebb0 67 67 65 72 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a ggers */.};../*.
4ebc0 2a 2a 20 41 20 73 6d 61 6c 6c 65 72 20 76 65 72 ** A smaller ver
4ebd0 73 69 6f 6e 20 6f 66 20 56 64 62 65 4f 70 20 75 sion of VdbeOp u
4ebe0 73 65 64 20 66 6f 72 20 74 68 65 20 56 64 62 65 sed for the Vdbe
4ebf0 41 64 64 4f 70 4c 69 73 74 28 29 20 66 75 6e 63 AddOpList() func
4ec00 74 69 6f 6e 20 62 65 63 61 75 73 65 0a 2a 2a 20 tion because.**
4ec10 69 74 20 74 61 6b 65 73 20 75 70 20 6c 65 73 73 it takes up less
4ec20 20 73 70 61 63 65 2e 0a 2a 2f 0a 73 74 72 75 63 space..*/.struc
4ec30 74 20 56 64 62 65 4f 70 4c 69 73 74 20 7b 0a 20 t VdbeOpList {.
4ec40 20 75 38 20 6f 70 63 6f 64 65 3b 20 20 20 20 20 u8 opcode;
4ec50 20 20 20 20 20 2f 2a 20 57 68 61 74 20 6f 70 65 /* What ope
4ec60 72 61 74 69 6f 6e 20 74 6f 20 70 65 72 66 6f 72 ration to perfor
4ec70 6d 20 2a 2f 0a 20 20 73 69 67 6e 65 64 20 63 68 m */. signed ch
4ec80 61 72 20 70 31 3b 20 20 20 20 20 2f 2a 20 46 69 ar p1; /* Fi
4ec90 72 73 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 rst operand */.
4eca0 20 73 69 67 6e 65 64 20 63 68 61 72 20 70 32 3b signed char p2;
4ecb0 20 20 20 20 20 2f 2a 20 53 65 63 6f 6e 64 20 70 /* Second p
4ecc0 61 72 61 6d 65 74 65 72 20 28 6f 66 74 65 6e 20 arameter (often
4ecd0 74 68 65 20 6a 75 6d 70 20 64 65 73 74 69 6e 61 the jump destina
4ece0 74 69 6f 6e 29 20 2a 2f 0a 20 20 73 69 67 6e 65 tion) */. signe
4ecf0 64 20 63 68 61 72 20 70 33 3b 20 20 20 20 20 2f d char p3; /
4ed00 2a 20 54 68 69 72 64 20 70 61 72 61 6d 65 74 65 * Third paramete
4ed10 72 20 2a 2f 0a 7d 3b 0a 74 79 70 65 64 65 66 20 r */.};.typedef
4ed20 73 74 72 75 63 74 20 56 64 62 65 4f 70 4c 69 73 struct VdbeOpLis
4ed30 74 20 56 64 62 65 4f 70 4c 69 73 74 3b 0a 0a 2f t VdbeOpList;../
4ed40 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c *.** Allowed val
4ed50 75 65 73 20 6f 66 20 56 64 62 65 4f 70 2e 70 34 ues of VdbeOp.p4
4ed60 74 79 70 65 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 type.*/.#define
4ed70 50 34 5f 4e 4f 54 55 53 45 44 20 20 20 20 30 20 P4_NOTUSED 0
4ed80 20 20 2f 2a 20 54 68 65 20 50 34 20 70 61 72 61 /* The P4 para
4ed90 6d 65 74 65 72 20 69 73 20 6e 6f 74 20 75 73 65 meter is not use
4eda0 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f d */.#define P4_
4edb0 44 59 4e 41 4d 49 43 20 20 28 2d 31 29 20 20 2f DYNAMIC (-1) /
4edc0 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 * Pointer to a s
4edd0 74 72 69 6e 67 20 6f 62 74 61 69 6e 65 64 20 66 tring obtained f
4ede0 72 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 rom sqliteMalloc
4edf0 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 () */.#define P4
4ee00 5f 53 54 41 54 49 43 20 20 20 28 2d 32 29 20 20 _STATIC (-2)
4ee10 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 61 20 /* Pointer to a
4ee20 73 74 61 74 69 63 20 73 74 72 69 6e 67 20 2a 2f static string */
4ee30 0a 23 64 65 66 69 6e 65 20 50 34 5f 43 4f 4c 4c .#define P4_COLL
4ee40 53 45 51 20 20 28 2d 34 29 20 20 2f 2a 20 50 34 SEQ (-4) /* P4
4ee50 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f is a pointer to
4ee60 20 61 20 43 6f 6c 6c 53 65 71 20 73 74 72 75 63 a CollSeq struc
4ee70 74 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 ture */.#define
4ee80 50 34 5f 46 55 4e 43 44 45 46 20 20 28 2d 35 29 P4_FUNCDEF (-5)
4ee90 20 20 2f 2a 20 50 34 20 69 73 20 61 20 70 6f 69 /* P4 is a poi
4eea0 6e 74 65 72 20 74 6f 20 61 20 46 75 6e 63 44 65 nter to a FuncDe
4eeb0 66 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 23 f structure */.#
4eec0 64 65 66 69 6e 65 20 50 34 5f 4b 45 59 49 4e 46 define P4_KEYINF
4eed0 4f 20 20 28 2d 36 29 20 20 2f 2a 20 50 34 20 69 O (-6) /* P4 i
4eee0 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 s a pointer to a
4eef0 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 KeyInfo structu
4ef00 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 re */.#define P4
4ef10 5f 56 44 42 45 46 55 4e 43 20 28 2d 37 29 20 20 _VDBEFUNC (-7)
4ef20 2f 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 /* P4 is a point
4ef30 65 72 20 74 6f 20 61 20 56 64 62 65 46 75 6e 63 er to a VdbeFunc
4ef40 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 23 64 structure */.#d
4ef50 65 66 69 6e 65 20 50 34 5f 4d 45 4d 20 20 20 20 efine P4_MEM
4ef60 20 20 28 2d 38 29 20 20 2f 2a 20 50 34 20 69 73 (-8) /* P4 is
4ef70 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 a pointer to a
4ef80 4d 65 6d 2a 20 20 20 20 73 74 72 75 63 74 75 72 Mem* structur
4ef90 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f e */.#define P4_
4efa0 54 52 41 4e 53 49 45 4e 54 20 28 2d 39 29 20 2f TRANSIENT (-9) /
4efb0 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 * P4 is a pointe
4efc0 72 20 74 6f 20 61 20 74 72 61 6e 73 69 65 6e 74 r to a transient
4efd0 20 73 74 72 69 6e 67 20 2a 2f 0a 23 64 65 66 69 string */.#defi
4efe0 6e 65 20 50 34 5f 56 54 41 42 20 20 20 20 20 28 ne P4_VTAB (
4eff0 2d 31 30 29 20 2f 2a 20 50 34 20 69 73 20 61 20 -10) /* P4 is a
4f000 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 73 71 pointer to an sq
4f010 6c 69 74 65 33 5f 76 74 61 62 20 73 74 72 75 63 lite3_vtab struc
4f020 74 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 ture */.#define
4f030 50 34 5f 4d 50 52 49 4e 54 46 20 20 28 2d 31 31 P4_MPRINTF (-11
4f040 29 20 2f 2a 20 50 34 20 69 73 20 61 20 73 74 72 ) /* P4 is a str
4f050 69 6e 67 20 6f 62 74 61 69 6e 65 64 20 66 72 6f ing obtained fro
4f060 6d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 m sqlite3_mprint
4f070 66 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 f() */.#define P
4f080 34 5f 52 45 41 4c 20 20 20 20 20 28 2d 31 32 29 4_REAL (-12)
4f090 20 2f 2a 20 50 34 20 69 73 20 61 20 36 34 2d 62 /* P4 is a 64-b
4f0a0 69 74 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e it floating poin
4f0b0 74 20 76 61 6c 75 65 20 2a 2f 0a 23 64 65 66 69 t value */.#defi
4f0c0 6e 65 20 50 34 5f 49 4e 54 36 34 20 20 20 20 28 ne P4_INT64 (
4f0d0 2d 31 33 29 20 2f 2a 20 50 34 20 69 73 20 61 20 -13) /* P4 is a
4f0e0 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 64-bit signed in
4f0f0 74 65 67 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65 teger */.#define
4f100 20 50 34 5f 49 4e 54 33 32 20 20 20 20 28 2d 31 P4_INT32 (-1
4f110 34 29 20 2f 2a 20 50 34 20 69 73 20 61 20 33 32 4) /* P4 is a 32
4f120 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65 -bit signed inte
4f130 67 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 ger */.#define P
4f140 34 5f 49 4e 54 41 52 52 41 59 20 28 2d 31 35 29 4_INTARRAY (-15)
4f150 20 2f 2a 20 50 34 20 69 73 20 61 20 76 65 63 74 /* P4 is a vect
4f160 6f 72 20 6f 66 20 33 32 2d 62 69 74 20 69 6e 74 or of 32-bit int
4f170 65 67 65 72 73 20 2a 2f 0a 23 64 65 66 69 6e 65 egers */.#define
4f180 20 50 34 5f 53 55 42 50 52 4f 47 52 41 4d 20 20 P4_SUBPROGRAM
4f190 28 2d 31 38 29 20 2f 2a 20 50 34 20 69 73 20 61 (-18) /* P4 is a
4f1a0 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 53 75 pointer to a Su
4f1b0 62 50 72 6f 67 72 61 6d 20 73 74 72 75 63 74 75 bProgram structu
4f1c0 72 65 20 2a 2f 0a 0a 2f 2a 20 57 68 65 6e 20 61 re */../* When a
4f1d0 64 64 69 6e 67 20 61 20 50 34 20 61 72 67 75 6d dding a P4 argum
4f1e0 65 6e 74 20 75 73 69 6e 67 20 50 34 5f 4b 45 59 ent using P4_KEY
4f1f0 49 4e 46 4f 2c 20 61 20 63 6f 70 79 20 6f 66 20 INFO, a copy of
4f200 74 68 65 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 the KeyInfo stru
4f210 63 74 75 72 65 0a 2a 2a 20 69 73 20 6d 61 64 65 cture.** is made
4f220 2e 20 20 54 68 61 74 20 63 6f 70 79 20 69 73 20 . That copy is
4f230 66 72 65 65 64 20 77 68 65 6e 20 74 68 65 20 56 freed when the V
4f240 64 62 65 20 69 73 20 66 69 6e 61 6c 69 7a 65 64 dbe is finalized
4f250 2e 20 20 42 75 74 20 69 66 20 74 68 65 0a 2a 2a . But if the.**
4f260 20 61 72 67 75 6d 65 6e 74 20 69 73 20 50 34 5f argument is P4_
4f270 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 2c KEYINFO_HANDOFF,
4f280 20 74 68 65 20 70 61 73 73 65 64 20 69 6e 20 70 the passed in p
4f290 6f 69 6e 74 65 72 20 69 73 20 75 73 65 64 2e 20 ointer is used.
4f2a0 20 49 74 20 73 74 69 6c 6c 0a 2a 2a 20 67 65 74 It still.** get
4f2b0 73 20 66 72 65 65 64 20 77 68 65 6e 20 74 68 65 s freed when the
4f2c0 20 56 64 62 65 20 69 73 20 66 69 6e 61 6c 69 7a Vdbe is finaliz
4f2d0 65 64 20 73 6f 20 69 74 20 73 74 69 6c 6c 20 73 ed so it still s
4f2e0 68 6f 75 6c 64 20 62 65 20 6f 62 74 61 69 6e 65 hould be obtaine
4f2f0 64 0a 2a 2a 20 66 72 6f 6d 20 61 20 73 69 6e 67 d.** from a sing
4f300 6c 65 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 le sqliteMalloc(
4f310 29 2e 20 20 42 75 74 20 6e 6f 20 63 6f 70 79 20 ). But no copy
4f320 69 73 20 6d 61 64 65 20 61 6e 64 20 74 68 65 20 is made and the
4f330 63 61 6c 6c 69 6e 67 0a 2a 2a 20 66 75 6e 63 74 calling.** funct
4f340 69 6f 6e 20 73 68 6f 75 6c 64 20 2a 6e 6f 74 2a ion should *not*
4f350 20 74 72 79 20 74 6f 20 66 72 65 65 20 74 68 65 try to free the
4f360 20 4b 65 79 49 6e 66 6f 2e 0a 2a 2f 0a 23 64 65 KeyInfo..*/.#de
4f370 66 69 6e 65 20 50 34 5f 4b 45 59 49 4e 46 4f 5f fine P4_KEYINFO_
4f380 48 41 4e 44 4f 46 46 20 28 2d 31 36 29 0a 23 64 HANDOFF (-16).#d
4f390 65 66 69 6e 65 20 50 34 5f 4b 45 59 49 4e 46 4f efine P4_KEYINFO
4f3a0 5f 53 54 41 54 49 43 20 20 28 2d 31 37 29 0a 0a _STATIC (-17)..
4f3b0 2f 2a 0a 2a 2a 20 54 68 65 20 56 64 62 65 2e 61 /*.** The Vdbe.a
4f3c0 43 6f 6c 4e 61 6d 65 20 61 72 72 61 79 20 63 6f ColName array co
4f3d0 6e 74 61 69 6e 73 20 35 6e 20 4d 65 6d 20 73 74 ntains 5n Mem st
4f3e0 72 75 63 74 75 72 65 73 2c 20 77 68 65 72 65 20 ructures, where
4f3f0 6e 20 69 73 20 74 68 65 20 0a 2a 2a 20 6e 75 6d n is the .** num
4f400 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 6f ber of columns o
4f410 66 20 64 61 74 61 20 72 65 74 75 72 6e 65 64 20 f data returned
4f420 62 79 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 by the statement
4f430 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 43 4f 4c ..*/.#define COL
4f440 4e 41 4d 45 5f 4e 41 4d 45 20 20 20 20 20 30 0a NAME_NAME 0.
4f450 23 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f #define COLNAME_
4f460 44 45 43 4c 54 59 50 45 20 31 0a 23 64 65 66 69 DECLTYPE 1.#defi
4f470 6e 65 20 43 4f 4c 4e 41 4d 45 5f 44 41 54 41 42 ne COLNAME_DATAB
4f480 41 53 45 20 32 0a 23 64 65 66 69 6e 65 20 43 4f ASE 2.#define CO
4f490 4c 4e 41 4d 45 5f 54 41 42 4c 45 20 20 20 20 33 LNAME_TABLE 3
4f4a0 0a 23 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 .#define COLNAME
4f4b0 5f 43 4f 4c 55 4d 4e 20 20 20 34 0a 23 69 66 64 _COLUMN 4.#ifd
4f4c0 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 ef SQLITE_ENABLE
4f4d0 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 _COLUMN_METADATA
4f4e0 0a 23 20 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d .# define COLNAM
4f4f0 45 5f 4e 20 20 20 20 20 20 20 20 35 20 20 20 20 E_N 5
4f500 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 43 /* Number of C
4f510 4f 4c 4e 41 4d 45 5f 78 78 78 20 73 79 6d 62 6f OLNAME_xxx symbo
4f520 6c 73 20 2a 2f 0a 23 65 6c 73 65 0a 23 20 69 66 ls */.#else.# if
4f530 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
4f540 44 45 43 4c 54 59 50 45 0a 23 20 20 20 64 65 66 DECLTYPE.# def
4f550 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 4e 20 20 20 ine COLNAME_N
4f560 20 20 20 31 20 20 20 20 20 20 2f 2a 20 53 74 6f 1 /* Sto
4f570 72 65 20 6f 6e 6c 79 20 74 68 65 20 6e 61 6d 65 re only the name
4f580 20 2a 2f 0a 23 20 65 6c 73 65 0a 23 20 20 20 64 */.# else.# d
4f590 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 4e 20 efine COLNAME_N
4f5a0 20 20 20 20 20 32 20 20 20 20 20 20 2f 2a 20 53 2 /* S
4f5b0 74 6f 72 65 20 74 68 65 20 6e 61 6d 65 20 61 6e tore the name an
4f5c0 64 20 64 65 63 6c 74 79 70 65 20 2a 2f 0a 23 20 d decltype */.#
4f5d0 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a 2f 2a endif.#endif../*
4f5e0 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e .** The followin
4f5f0 67 20 6d 61 63 72 6f 20 63 6f 6e 76 65 72 74 73 g macro converts
4f600 20 61 20 72 65 6c 61 74 69 76 65 20 61 64 64 72 a relative addr
4f610 65 73 73 20 69 6e 20 74 68 65 20 70 32 20 66 69 ess in the p2 fi
4f620 65 6c 64 0a 2a 2a 20 6f 66 20 61 20 56 64 62 65 eld.** of a Vdbe
4f630 4f 70 20 73 74 72 75 63 74 75 72 65 20 69 6e 74 Op structure int
4f640 6f 20 61 20 6e 65 67 61 74 69 76 65 20 6e 75 6d o a negative num
4f650 62 65 72 20 73 6f 20 74 68 61 74 20 0a 2a 2a 20 ber so that .**
4f660 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
4f670 4c 69 73 74 28 29 20 6b 6e 6f 77 73 20 74 68 61 List() knows tha
4f680 74 20 74 68 65 20 61 64 64 72 65 73 73 20 69 73 t the address is
4f690 20 72 65 6c 61 74 69 76 65 2e 20 20 43 61 6c 6c relative. Call
4f6a0 69 6e 67 0a 2a 2a 20 74 68 65 20 6d 61 63 72 6f ing.** the macro
4f6b0 20 61 67 61 69 6e 20 72 65 73 74 6f 72 65 73 20 again restores
4f6c0 74 68 65 20 61 64 64 72 65 73 73 2e 0a 2a 2f 0a the address..*/.
4f6d0 23 64 65 66 69 6e 65 20 41 44 44 52 28 58 29 20 #define ADDR(X)
4f6e0 20 28 2d 31 2d 28 58 29 29 0a 0a 2f 2a 0a 2a 2a (-1-(X))../*.**
4f6f0 20 54 68 65 20 6d 61 6b 65 66 69 6c 65 20 73 63 The makefile sc
4f700 61 6e 73 20 74 68 65 20 76 64 62 65 2e 63 20 73 ans the vdbe.c s
4f710 6f 75 72 63 65 20 66 69 6c 65 20 61 6e 64 20 63 ource file and c
4f720 72 65 61 74 65 73 20 74 68 65 20 22 6f 70 63 6f reates the "opco
4f730 64 65 73 2e 68 22 0a 2a 2a 20 68 65 61 64 65 72 des.h".** header
4f740 20 66 69 6c 65 20 74 68 61 74 20 64 65 66 69 6e file that defin
4f750 65 73 20 61 20 6e 75 6d 62 65 72 20 66 6f 72 20 es a number for
4f760 65 61 63 68 20 6f 70 63 6f 64 65 20 75 73 65 64 each opcode used
4f770 20 62 79 20 74 68 65 20 56 44 42 45 2e 0a 2a 2f by the VDBE..*/
4f780 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
4f790 20 49 6e 63 6c 75 64 65 20 6f 70 63 6f 64 65 73 Include opcodes
4f7a0 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 .h in the middle
4f7b0 20 6f 66 20 76 64 62 65 2e 68 20 2a 2a 2a 2a 2a of vdbe.h *****
4f7c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
4f7d0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
4f7e0 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 70 63 6f Begin file opco
4f7f0 64 65 73 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a des.h **********
4f800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4f810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
4f820 0a 2f 2a 20 41 75 74 6f 6d 61 74 69 63 61 6c 6c ./* Automaticall
4f830 79 20 67 65 6e 65 72 61 74 65 64 2e 20 20 44 6f y generated. Do
4f840 20 6e 6f 74 20 65 64 69 74 20 2a 2f 0a 2f 2a 20 not edit */./*
4f850 53 65 65 20 74 68 65 20 6d 6b 6f 70 63 6f 64 65 See the mkopcode
4f860 68 2e 61 77 6b 20 73 63 72 69 70 74 20 66 6f 72 h.awk script for
4f870 20 64 65 74 61 69 6c 73 20 2a 2f 0a 23 64 65 66 details */.#def
4f880 69 6e 65 20 4f 50 5f 56 4e 65 78 74 20 20 20 20 ine OP_VNext
4f890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4f8a0 20 20 20 20 20 20 20 20 20 20 20 20 31 0a 23 64 1.#d
4f8b0 65 66 69 6e 65 20 4f 50 5f 41 66 66 69 6e 69 74 efine OP_Affinit
4f8c0 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 y
4f8d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 0a 2.
4f8e0 23 64 65 66 69 6e 65 20 4f 50 5f 43 6f 6c 75 6d #define OP_Colum
4f8f0 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 n
4f900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4f910 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 65 74 3.#define OP_Set
4f920 43 6f 6f 6b 69 65 20 20 20 20 20 20 20 20 20 20 Cookie
4f930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4f940 20 20 34 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 4.#define OP_S
4f950 65 65 6b 20 20 20 20 20 20 20 20 20 20 20 20 20 eek
4f960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4f970 20 20 20 20 35 0a 23 64 65 66 69 6e 65 20 4f 50 5.#define OP
4f980 5f 52 65 61 6c 20 20 20 20 20 20 20 20 20 20 20 _Real
4f990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4f9a0 20 20 20 20 31 33 30 20 20 20 2f 2a 20 73 61 6d 130 /* sam
4f9b0 65 20 61 73 20 54 4b 5f 46 4c 4f 41 54 20 20 20 e as TK_FLOAT
4f9c0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 */.#define OP_S
4f9d0 65 71 75 65 6e 63 65 20 20 20 20 20 20 20 20 20 equence
4f9e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4f9f0 20 20 20 20 36 0a 23 64 65 66 69 6e 65 20 4f 50 6.#define OP
4fa00 5f 53 61 76 65 70 6f 69 6e 74 20 20 20 20 20 20 _Savepoint
4fa10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fa20 20 20 20 20 20 20 37 0a 23 64 65 66 69 6e 65 20 7.#define
4fa30 4f 50 5f 47 65 20 20 20 20 20 20 20 20 20 20 20 OP_Ge
4fa40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fa50 20 20 20 20 20 20 20 38 30 20 20 20 2f 2a 20 73 80 /* s
4fa60 61 6d 65 20 61 73 20 54 4b 5f 47 45 20 20 20 20 ame as TK_GE
4fa70 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 */.#define OP
4fa80 5f 52 6f 77 4b 65 79 20 20 20 20 20 20 20 20 20 _RowKey
4fa90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4faa0 20 20 20 20 20 20 38 0a 23 64 65 66 69 6e 65 20 8.#define
4fab0 4f 50 5f 53 43 6f 70 79 20 20 20 20 20 20 20 20 OP_SCopy
4fac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fad0 20 20 20 20 20 20 20 20 39 0a 23 64 65 66 69 6e 9.#defin
4fae0 65 20 4f 50 5f 45 71 20 20 20 20 20 20 20 20 20 e OP_Eq
4faf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fb00 20 20 20 20 20 20 20 20 20 37 36 20 20 20 2f 2a 76 /*
4fb10 20 73 61 6d 65 20 61 73 20 54 4b 5f 45 51 20 20 same as TK_EQ
4fb20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 */.#define
4fb30 4f 50 5f 4f 70 65 6e 57 72 69 74 65 20 20 20 20 OP_OpenWrite
4fb40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fb50 20 20 20 20 20 20 20 31 30 0a 23 64 65 66 69 6e 10.#defin
4fb60 65 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 20 20 20 20 e OP_NotNull
4fb70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fb80 20 20 20 20 20 20 20 20 20 37 34 20 20 20 2f 2a 74 /*
4fb90 20 73 61 6d 65 20 61 73 20 54 4b 5f 4e 4f 54 4e same as TK_NOTN
4fba0 55 4c 4c 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 ULL */.#define
4fbb0 4f 50 5f 49 66 20 20 20 20 20 20 20 20 20 20 20 OP_If
4fbc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fbd0 20 20 20 20 20 20 20 31 31 0a 23 64 65 66 69 6e 11.#defin
4fbe0 65 20 4f 50 5f 54 6f 49 6e 74 20 20 20 20 20 20 e OP_ToInt
4fbf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fc00 20 20 20 20 20 20 20 20 31 34 34 20 20 20 2f 2a 144 /*
4fc10 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 49 same as TK_TO_I
4fc20 4e 54 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 NT */.#define
4fc30 4f 50 5f 53 74 72 69 6e 67 38 20 20 20 20 20 20 OP_String8
4fc40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fc50 20 20 20 20 20 20 20 39 34 20 20 20 2f 2a 20 73 94 /* s
4fc60 61 6d 65 20 61 73 20 54 4b 5f 53 54 52 49 4e 47 ame as TK_STRING
4fc70 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 */.#define OP
4fc80 5f 43 6f 6c 6c 53 65 71 20 20 20 20 20 20 20 20 _CollSeq
4fc90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fca0 20 20 20 20 20 31 32 0a 23 64 65 66 69 6e 65 20 12.#define
4fcb0 4f 50 5f 4f 70 65 6e 52 65 61 64 20 20 20 20 20 OP_OpenRead
4fcc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fcd0 20 20 20 20 20 20 20 31 33 0a 23 64 65 66 69 6e 13.#defin
4fce0 65 20 4f 50 5f 45 78 70 69 72 65 20 20 20 20 20 e OP_Expire
4fcf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fd00 20 20 20 20 20 20 20 20 20 31 34 0a 23 64 65 66 14.#def
4fd10 69 6e 65 20 4f 50 5f 41 75 74 6f 43 6f 6d 6d 69 ine OP_AutoCommi
4fd20 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t
4fd30 20 20 20 20 20 20 20 20 20 20 20 31 35 0a 23 64 15.#d
4fd40 65 66 69 6e 65 20 4f 50 5f 47 74 20 20 20 20 20 efine OP_Gt
4fd50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fd60 20 20 20 20 20 20 20 20 20 20 20 20 20 37 37 20 77
4fd70 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f /* same as TK_
4fd80 47 54 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 GT */.#def
4fd90 69 6e 65 20 4f 50 5f 50 61 67 65 63 6f 75 6e 74 ine OP_Pagecount
4fda0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fdb0 20 20 20 20 20 20 20 20 20 20 20 31 36 0a 23 64 16.#d
4fdc0 65 66 69 6e 65 20 4f 50 5f 49 6e 74 65 67 72 69 efine OP_Integri
4fdd0 74 79 43 6b 20 20 20 20 20 20 20 20 20 20 20 20 tyCk
4fde0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 37 0a 17.
4fdf0 23 64 65 66 69 6e 65 20 4f 50 5f 53 6f 72 74 20 #define OP_Sort
4fe00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fe10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
4fe20 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 6f 70 8.#define OP_Cop
4fe30 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 y
4fe40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fe50 20 32 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 54 20.#define OP_T
4fe60 72 61 63 65 20 20 20 20 20 20 20 20 20 20 20 20 race
4fe70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fe80 20 20 20 32 31 0a 23 64 65 66 69 6e 65 20 4f 50 21.#define OP
4fe90 5f 46 75 6e 63 74 69 6f 6e 20 20 20 20 20 20 20 _Function
4fea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4feb0 20 20 20 20 20 32 32 0a 23 64 65 66 69 6e 65 20 22.#define
4fec0 4f 50 5f 49 66 4e 65 67 20 20 20 20 20 20 20 20 OP_IfNeg
4fed0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fee0 20 20 20 20 20 20 20 32 33 0a 23 64 65 66 69 6e 23.#defin
4fef0 65 20 4f 50 5f 41 6e 64 20 20 20 20 20 20 20 20 e OP_And
4ff00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ff10 20 20 20 20 20 20 20 20 20 36 39 20 20 20 2f 2a 69 /*
4ff20 20 73 61 6d 65 20 61 73 20 54 4b 5f 41 4e 44 20 same as TK_AND
4ff30 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 */.#define
4ff40 4f 50 5f 53 75 62 74 72 61 63 74 20 20 20 20 20 OP_Subtract
4ff50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ff60 20 20 20 20 20 20 20 38 37 20 20 20 2f 2a 20 73 87 /* s
4ff70 61 6d 65 20 61 73 20 54 4b 5f 4d 49 4e 55 53 20 ame as TK_MINUS
4ff80 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 */.#define OP
4ff90 5f 4e 6f 6f 70 20 20 20 20 20 20 20 20 20 20 20 _Noop
4ffa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ffb0 20 20 20 20 20 32 34 0a 23 64 65 66 69 6e 65 20 24.#define
4ffc0 4f 50 5f 50 72 6f 67 72 61 6d 20 20 20 20 20 20 OP_Program
4ffd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ffe0 20 20 20 20 20 20 20 32 35 0a 23 64 65 66 69 6e 25.#defin
4fff0 65 20 4f 50 5f 52 65 74 75 72 6e 20 20 20 20 20 e OP_Return
50000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50010 20 20 20 20 20 20 20 20 20 32 36 0a 23 64 65 66 26.#def
50020 69 6e 65 20 4f 50 5f 52 65 6d 61 69 6e 64 65 72 ine OP_Remainder
50030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50040 20 20 20 20 20 20 20 20 20 20 20 39 30 20 20 20 90
50050 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 52 45 /* same as TK_RE
50060 4d 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e M */.#defin
50070 65 20 4f 50 5f 4e 65 77 52 6f 77 69 64 20 20 20 e OP_NewRowid
50080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50090 20 20 20 20 20 20 20 20 20 32 37 0a 23 64 65 66 27.#def
500a0 69 6e 65 20 4f 50 5f 4d 75 6c 74 69 70 6c 79 20 ine OP_Multiply
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 20 20 20 38 38 20 20 20 88
500d0 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 53 54 /* same as TK_ST
500e0 41 52 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e AR */.#defin
500f0 65 20 4f 50 5f 46 6b 43 6f 75 6e 74 65 72 20 20 e OP_FkCounter
50100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50110 20 20 20 20 20 20 20 20 20 32 38 0a 23 64 65 66 28.#def
50120 69 6e 65 20 4f 50 5f 56 61 72 69 61 62 6c 65 20 ine OP_Variable
50130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50140 20 20 20 20 20 20 20 20 20 20 20 32 39 0a 23 64 29.#d
50150 65 66 69 6e 65 20 4f 50 5f 53 74 72 69 6e 67 20 efine OP_String
50160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50170 20 20 20 20 20 20 20 20 20 20 20 20 20 33 30 0a 30.
50180 23 64 65 66 69 6e 65 20 4f 50 5f 52 65 61 6c 41 #define OP_RealA
50190 66 66 69 6e 69 74 79 20 20 20 20 20 20 20 20 20 ffinity
501a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33 3
501b0 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 52 65 1.#define OP_VRe
501c0 6e 61 6d 65 20 20 20 20 20 20 20 20 20 20 20 20 name
501d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
501e0 20 33 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 50 32.#define OP_P
501f0 61 72 73 65 53 63 68 65 6d 61 20 20 20 20 20 20 arseSchema
50200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50210 20 20 20 33 33 0a 23 64 65 66 69 6e 65 20 4f 50 33.#define OP
50220 5f 56 4f 70 65 6e 20 20 20 20 20 20 20 20 20 20 _VOpen
50230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50240 20 20 20 20 20 33 34 0a 23 64 65 66 69 6e 65 20 34.#define
50250 4f 50 5f 43 6c 6f 73 65 20 20 20 20 20 20 20 20 OP_Close
50260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50270 20 20 20 20 20 20 20 33 35 0a 23 64 65 66 69 6e 35.#defin
50280 65 20 4f 50 5f 43 72 65 61 74 65 49 6e 64 65 78 e OP_CreateIndex
50290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
502a0 20 20 20 20 20 20 20 20 20 33 36 0a 23 64 65 66 36.#def
502b0 69 6e 65 20 4f 50 5f 49 73 55 6e 69 71 75 65 20 ine OP_IsUnique
502c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
502d0 20 20 20 20 20 20 20 20 20 20 20 33 37 0a 23 64 37.#d
502e0 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 46 6f 75 6e efine OP_NotFoun
502f0 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 d
50300 20 20 20 20 20 20 20 20 20 20 20 20 20 33 38 0a 38.
50310 23 64 65 66 69 6e 65 20 4f 50 5f 49 6e 74 36 34 #define OP_Int64
50320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33 3
50340 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4d 75 73 9.#define OP_Mus
50350 74 42 65 49 6e 74 20 20 20 20 20 20 20 20 20 20 tBeInt
50360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50370 20 34 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 48 40.#define OP_H
50380 61 6c 74 20 20 20 20 20 20 20 20 20 20 20 20 20 alt
50390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
503a0 20 20 20 34 31 0a 23 64 65 66 69 6e 65 20 4f 50 41.#define OP
503b0 5f 52 6f 77 69 64 20 20 20 20 20 20 20 20 20 20 _Rowid
503c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
503d0 20 20 20 20 20 34 32 0a 23 64 65 66 69 6e 65 20 42.#define
503e0 4f 50 5f 49 64 78 4c 54 20 20 20 20 20 20 20 20 OP_IdxLT
503f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50400 20 20 20 20 20 20 20 34 33 0a 23 64 65 66 69 6e 43.#defin
50410 65 20 4f 50 5f 41 64 64 49 6d 6d 20 20 20 20 20 e OP_AddImm
50420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50430 20 20 20 20 20 20 20 20 20 34 34 0a 23 64 65 66 44.#def
50440 69 6e 65 20 4f 50 5f 52 6f 77 44 61 74 61 20 20 ine OP_RowData
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 34 35 0a 23 64 45.#d
50470 65 66 69 6e 65 20 4f 50 5f 4d 65 6d 4d 61 78 20 efine OP_MemMax
50480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50490 20 20 20 20 20 20 20 20 20 20 20 20 20 34 36 0a 46.
504a0 23 64 65 66 69 6e 65 20 4f 50 5f 4f 72 20 20 20 #define OP_Or
504b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
504c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36 6
504d0 38 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 8 /* same as T
504e0 4b 5f 4f 52 20 20 20 20 20 20 20 2a 2f 0a 23 64 K_OR */.#d
504f0 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 45 78 69 73 efine OP_NotExis
50500 74 73 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ts
50510 20 20 20 20 20 20 20 20 20 20 20 20 20 34 37 0a 47.
50520 23 64 65 66 69 6e 65 20 4f 50 5f 47 6f 73 75 62 #define OP_Gosub
50530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 4
50550 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 44 69 76 8.#define OP_Div
50560 69 64 65 20 20 20 20 20 20 20 20 20 20 20 20 20 ide
50570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50580 20 38 39 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 89 /* same as
50590 20 54 4b 5f 53 4c 41 53 48 20 20 20 20 2a 2f 0a TK_SLASH */.
505a0 23 64 65 66 69 6e 65 20 4f 50 5f 49 6e 74 65 67 #define OP_Integ
505b0 65 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 er
505c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 4
505d0 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 54 6f 4e 9.#define OP_ToN
505e0 75 6d 65 72 69 63 20 20 20 20 20 20 20 20 20 20 umeric
505f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50600 31 34 33 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 143 /* same as
50610 20 54 4b 5f 54 4f 5f 4e 55 4d 45 52 49 43 2a 2f TK_TO_NUMERIC*/
50620 0a 23 64 65 66 69 6e 65 20 4f 50 5f 50 72 65 76 .#define OP_Prev
50630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50650 35 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 6f 50.#define OP_Ro
50660 77 53 65 74 52 65 61 64 20 20 20 20 20 20 20 20 wSetRead
50670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50680 20 20 35 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 51.#define OP_
50690 43 6f 6e 63 61 74 20 20 20 20 20 20 20 20 20 20 Concat
506a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
506b0 20 20 20 20 39 31 20 20 20 2f 2a 20 73 61 6d 65 91 /* same
506c0 20 61 73 20 54 4b 5f 43 4f 4e 43 41 54 20 20 20 as TK_CONCAT
506d0 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 6f */.#define OP_Ro
506e0 77 53 65 74 41 64 64 20 20 20 20 20 20 20 20 20 wSetAdd
506f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50700 20 20 35 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52.#define OP_
50710 42 69 74 41 6e 64 20 20 20 20 20 20 20 20 20 20 BitAnd
50720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50730 20 20 20 20 38 32 20 20 20 2f 2a 20 73 61 6d 65 82 /* same
50740 20 61 73 20 54 4b 5f 42 49 54 41 4e 44 20 20 20 as TK_BITAND
50750 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 43 */.#define OP_VC
50760 6f 6c 75 6d 6e 20 20 20 20 20 20 20 20 20 20 20 olumn
50770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50780 20 20 35 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53.#define OP_
50790 43 72 65 61 74 65 54 61 62 6c 65 20 20 20 20 20 CreateTable
507a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
507b0 20 20 20 20 35 34 0a 23 64 65 66 69 6e 65 20 4f 54.#define O
507c0 50 5f 4c 61 73 74 20 20 20 20 20 20 20 20 20 20 P_Last
507d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
507e0 20 20 20 20 20 20 35 35 0a 23 64 65 66 69 6e 65 55.#define
507f0 20 4f 50 5f 53 65 65 6b 4c 65 20 20 20 20 20 20 OP_SeekLe
50800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50810 20 20 20 20 20 20 20 20 35 36 0a 23 64 65 66 69 56.#defi
50820 6e 65 20 4f 50 5f 49 73 4e 75 6c 6c 20 20 20 20 ne OP_IsNull
50830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50840 20 20 20 20 20 20 20 20 20 20 37 33 20 20 20 2f 73 /
50850 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 49 53 4e * same as TK_ISN
50860 55 4c 4c 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 ULL */.#define
50870 20 4f 50 5f 49 6e 63 72 56 61 63 75 75 6d 20 20 OP_IncrVacuum
50880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50890 20 20 20 20 20 20 20 20 35 37 0a 23 64 65 66 69 57.#defi
508a0 6e 65 20 4f 50 5f 49 64 78 52 6f 77 69 64 20 20 ne OP_IdxRowid
508b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
508c0 20 20 20 20 20 20 20 20 20 20 35 38 0a 23 64 65 58.#de
508d0 66 69 6e 65 20 4f 50 5f 53 68 69 66 74 52 69 67 fine OP_ShiftRig
508e0 68 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ht
508f0 20 20 20 20 20 20 20 20 20 20 20 20 38 35 20 20 85
50900 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 52 /* same as TK_R
50910 53 48 49 46 54 20 20 20 2a 2f 0a 23 64 65 66 69 SHIFT */.#defi
50920 6e 65 20 4f 50 5f 52 65 73 65 74 43 6f 75 6e 74 ne OP_ResetCount
50930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50940 20 20 20 20 20 20 20 20 20 20 35 39 0a 23 64 65 59.#de
50950 66 69 6e 65 20 4f 50 5f 59 69 65 6c 64 20 20 20 fine OP_Yield
50960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50970 20 20 20 20 20 20 20 20 20 20 20 20 36 30 0a 23 60.#
50980 64 65 66 69 6e 65 20 4f 50 5f 44 72 6f 70 54 72 define OP_DropTr
50990 69 67 67 65 72 20 20 20 20 20 20 20 20 20 20 20 igger
509a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36 31 61
509b0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 44 72 6f 70 .#define OP_Drop
509c0 49 6e 64 65 78 20 20 20 20 20 20 20 20 20 20 20 Index
509d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
509e0 36 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 50 61 62.#define OP_Pa
509f0 72 61 6d 20 20 20 20 20 20 20 20 20 20 20 20 20 ram
50a00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50a10 20 20 36 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f 63.#define OP_
50a20 49 64 78 47 45 20 20 20 20 20 20 20 20 20 20 20 IdxGE
50a30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50a40 20 20 20 20 36 34 0a 23 64 65 66 69 6e 65 20 4f 64.#define O
50a50 50 5f 49 64 78 44 65 6c 65 74 65 20 20 20 20 20 P_IdxDelete
50a60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50a70 20 20 20 20 20 20 36 35 0a 23 64 65 66 69 6e 65 65.#define
50a80 20 4f 50 5f 56 61 63 75 75 6d 20 20 20 20 20 20 OP_Vacuum
50a90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50aa0 20 20 20 20 20 20 20 20 36 36 0a 23 64 65 66 69 66.#defi
50ab0 6e 65 20 4f 50 5f 49 66 4e 6f 74 20 20 20 20 20 ne OP_IfNot
50ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50ad0 20 20 20 20 20 20 20 20 20 20 36 37 0a 23 64 65 67.#de
50ae0 66 69 6e 65 20 4f 50 5f 44 72 6f 70 54 61 62 6c fine OP_DropTabl
50af0 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e
50b00 20 20 20 20 20 20 20 20 20 20 20 20 37 30 0a 23 70.#
50b10 64 65 66 69 6e 65 20 4f 50 5f 53 65 65 6b 4c 74 define OP_SeekLt
50b20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50b30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 31 71
50b40 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4d 61 6b 65 .#define OP_Make
50b50 52 65 63 6f 72 64 20 20 20 20 20 20 20 20 20 20 Record
50b60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50b70 37 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 54 6f 72.#define OP_To
50b80 42 6c 6f 62 20 20 20 20 20 20 20 20 20 20 20 20 Blob
50b90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50ba0 20 31 34 32 20 20 20 2f 2a 20 73 61 6d 65 20 61 142 /* same a
50bb0 73 20 54 4b 5f 54 4f 5f 42 4c 4f 42 20 20 2a 2f s TK_TO_BLOB */
50bc0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 65 73 75 .#define OP_Resu
50bd0 6c 74 52 6f 77 20 20 20 20 20 20 20 20 20 20 20 ltRow
50be0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50bf0 38 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 44 65 81.#define OP_De
50c00 6c 65 74 65 20 20 20 20 20 20 20 20 20 20 20 20 lete
50c10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50c20 20 20 39 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 92.#define OP_
50c30 41 67 67 46 69 6e 61 6c 20 20 20 20 20 20 20 20 AggFinal
50c40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50c50 20 20 20 20 39 35 0a 23 64 65 66 69 6e 65 20 4f 95.#define O
50c60 50 5f 43 6f 6d 70 61 72 65 20 20 20 20 20 20 20 P_Compare
50c70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50c80 20 20 20 20 20 20 39 36 0a 23 64 65 66 69 6e 65 96.#define
50c90 20 4f 50 5f 53 68 69 66 74 4c 65 66 74 20 20 20 OP_ShiftLeft
50ca0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50cb0 20 20 20 20 20 20 20 20 38 34 20 20 20 2f 2a 20 84 /*
50cc0 73 61 6d 65 20 61 73 20 54 4b 5f 4c 53 48 49 46 same as TK_LSHIF
50cd0 54 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f T */.#define O
50ce0 50 5f 47 6f 74 6f 20 20 20 20 20 20 20 20 20 20 P_Goto
50cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50d00 20 20 20 20 20 20 39 37 0a 23 64 65 66 69 6e 65 97.#define
50d10 20 4f 50 5f 54 61 62 6c 65 4c 6f 63 6b 20 20 20 OP_TableLock
50d20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50d30 20 20 20 20 20 20 20 20 39 38 0a 23 64 65 66 69 98.#defi
50d40 6e 65 20 4f 50 5f 43 6c 65 61 72 20 20 20 20 20 ne OP_Clear
50d50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50d60 20 20 20 20 20 20 20 20 20 20 39 39 0a 23 64 65 99.#de
50d70 66 69 6e 65 20 4f 50 5f 4c 65 20 20 20 20 20 20 fine OP_Le
50d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50d90 20 20 20 20 20 20 20 20 20 20 20 20 37 38 20 20 78
50da0 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4c /* same as TK_L
50db0 45 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 E */.#defi
50dc0 6e 65 20 4f 50 5f 56 65 72 69 66 79 43 6f 6f 6b ne OP_VerifyCook
50dd0 69 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ie
50de0 20 20 20 20 20 20 20 20 20 31 30 30 0a 23 64 65 100.#de
50df0 66 69 6e 65 20 4f 50 5f 41 67 67 53 74 65 70 20 fine OP_AggStep
50e00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50e10 20 20 20 20 20 20 20 20 20 20 20 31 30 31 0a 23 101.#
50e20 64 65 66 69 6e 65 20 4f 50 5f 54 6f 54 65 78 74 define OP_ToText
50e30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50e40 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34 31 141
50e50 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b /* same as TK
50e60 5f 54 4f 5f 54 45 58 54 20 20 2a 2f 0a 23 64 65 _TO_TEXT */.#de
50e70 66 69 6e 65 20 4f 50 5f 4e 6f 74 20 20 20 20 20 fine OP_Not
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 20 31 39 20 20 19
50ea0 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4e /* same as TK_N
50eb0 4f 54 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 OT */.#defi
50ec0 6e 65 20 4f 50 5f 54 6f 52 65 61 6c 20 20 20 20 ne OP_ToReal
50ed0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50ee0 20 20 20 20 20 20 20 20 20 31 34 35 20 20 20 2f 145 /
50ef0 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f * same as TK_TO_
50f00 52 45 41 4c 20 20 2a 2f 0a 23 64 65 66 69 6e 65 REAL */.#define
50f10 20 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e 20 OP_Transaction
50f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50f30 20 20 20 20 20 20 20 31 30 32 0a 23 64 65 66 69 102.#defi
50f40 6e 65 20 4f 50 5f 56 46 69 6c 74 65 72 20 20 20 ne OP_VFilter
50f50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50f60 20 20 20 20 20 20 20 20 20 31 30 33 0a 23 64 65 103.#de
50f70 66 69 6e 65 20 4f 50 5f 4e 65 20 20 20 20 20 20 fine OP_Ne
50f80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50f90 20 20 20 20 20 20 20 20 20 20 20 20 37 35 20 20 75
50fa0 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4e /* same as TK_N
50fb0 45 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 E */.#defi
50fc0 6e 65 20 4f 50 5f 56 44 65 73 74 72 6f 79 20 20 ne OP_VDestroy
50fd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50fe0 20 20 20 20 20 20 20 20 20 31 30 34 0a 23 64 65 104.#de
50ff0 66 69 6e 65 20 4f 50 5f 42 69 74 4f 72 20 20 20 fine OP_BitOr
51000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51010 20 20 20 20 20 20 20 20 20 20 20 20 38 33 20 20 83
51020 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 42 /* same as TK_B
51030 49 54 4f 52 20 20 20 20 2a 2f 0a 23 64 65 66 69 ITOR */.#defi
51040 6e 65 20 4f 50 5f 4e 65 78 74 20 20 20 20 20 20 ne OP_Next
51050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51060 20 20 20 20 20 20 20 20 20 31 30 35 0a 23 64 65 105.#de
51070 66 69 6e 65 20 4f 50 5f 43 6f 75 6e 74 20 20 20 fine OP_Count
51080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51090 20 20 20 20 20 20 20 20 20 20 20 31 30 36 0a 23 106.#
510a0 64 65 66 69 6e 65 20 4f 50 5f 49 64 78 49 6e 73 define OP_IdxIns
510b0 65 72 74 20 20 20 20 20 20 20 20 20 20 20 20 20 ert
510c0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 37 107
510d0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4c 74 20 20 .#define OP_Lt
510e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
510f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51100 37 39 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 79 /* same as
51110 54 4b 5f 4c 54 20 20 20 20 20 20 20 2a 2f 0a 23 TK_LT */.#
51120 64 65 66 69 6e 65 20 4f 50 5f 46 6b 49 66 5a 65 define OP_FkIfZe
51130 72 6f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ro
51140 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 38 108
51150 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 65 65 6b .#define OP_Seek
51160 47 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 Ge
51170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
51180 30 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 6e 09.#define OP_In
51190 73 65 72 74 20 20 20 20 20 20 20 20 20 20 20 20 sert
511a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
511b0 20 31 31 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 110.#define OP_
511c0 44 65 73 74 72 6f 79 20 20 20 20 20 20 20 20 20 Destroy
511d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
511e0 20 20 20 31 31 31 0a 23 64 65 66 69 6e 65 20 4f 111.#define O
511f0 50 5f 52 65 61 64 43 6f 6f 6b 69 65 20 20 20 20 P_ReadCookie
51200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51210 20 20 20 20 20 31 31 32 0a 23 64 65 66 69 6e 65 112.#define
51220 20 4f 50 5f 52 6f 77 53 65 74 54 65 73 74 20 20 OP_RowSetTest
51230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51240 20 20 20 20 20 20 20 31 31 33 0a 23 64 65 66 69 113.#defi
51250 6e 65 20 4f 50 5f 4c 6f 61 64 41 6e 61 6c 79 73 ne OP_LoadAnalys
51260 69 73 20 20 20 20 20 20 20 20 20 20 20 20 20 20 is
51270 20 20 20 20 20 20 20 20 20 31 31 34 0a 23 64 65 114.#de
51280 66 69 6e 65 20 4f 50 5f 45 78 70 6c 61 69 6e 20 fine OP_Explain
51290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
512a0 20 20 20 20 20 20 20 20 20 20 20 31 31 35 0a 23 115.#
512b0 64 65 66 69 6e 65 20 4f 50 5f 48 61 6c 74 49 66 define OP_HaltIf
512c0 4e 75 6c 6c 20 20 20 20 20 20 20 20 20 20 20 20 Null
512d0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 36 116
512e0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4f 70 65 6e .#define OP_Open
512f0 50 73 65 75 64 6f 20 20 20 20 20 20 20 20 20 20 Pseudo
51300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
51310 31 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4f 70 17.#define OP_Op
51320 65 6e 45 70 68 65 6d 65 72 61 6c 20 20 20 20 20 enEphemeral
51330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51340 20 31 31 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 118.#define OP_
51350 4e 75 6c 6c 20 20 20 20 20 20 20 20 20 20 20 20 Null
51360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51370 20 20 20 31 31 39 0a 23 64 65 66 69 6e 65 20 4f 119.#define O
51380 50 5f 4d 6f 76 65 20 20 20 20 20 20 20 20 20 20 P_Move
51390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
513a0 20 20 20 20 20 31 32 30 0a 23 64 65 66 69 6e 65 120.#define
513b0 20 4f 50 5f 42 6c 6f 62 20 20 20 20 20 20 20 20 OP_Blob
513c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
513d0 20 20 20 20 20 20 20 31 32 31 0a 23 64 65 66 69 121.#defi
513e0 6e 65 20 4f 50 5f 41 64 64 20 20 20 20 20 20 20 ne OP_Add
513f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51400 20 20 20 20 20 20 20 20 20 20 38 36 20 20 20 2f 86 /
51410 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 50 4c 55 * same as TK_PLU
51420 53 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 S */.#define
51430 20 4f 50 5f 52 65 77 69 6e 64 20 20 20 20 20 20 OP_Rewind
51440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51450 20 20 20 20 20 20 20 31 32 32 0a 23 64 65 66 69 122.#defi
51460 6e 65 20 4f 50 5f 53 65 65 6b 47 74 20 20 20 20 ne OP_SeekGt
51470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51480 20 20 20 20 20 20 20 20 20 31 32 33 0a 23 64 65 123.#de
51490 66 69 6e 65 20 4f 50 5f 56 42 65 67 69 6e 20 20 fine OP_VBegin
514a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
514b0 20 20 20 20 20 20 20 20 20 20 20 31 32 34 0a 23 124.#
514c0 64 65 66 69 6e 65 20 4f 50 5f 56 55 70 64 61 74 define OP_VUpdat
514d0 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e
514e0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 35 125
514f0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 66 5a 65 .#define OP_IfZe
51500 72 6f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ro
51510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
51520 32 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 42 69 26.#define OP_Bi
51530 74 4e 6f 74 20 20 20 20 20 20 20 20 20 20 20 20 tNot
51540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51550 20 20 39 33 20 20 20 2f 2a 20 73 61 6d 65 20 61 93 /* same a
51560 73 20 54 4b 5f 42 49 54 4e 4f 54 20 20 20 2a 2f s TK_BITNOT */
51570 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 43 72 65 .#define OP_VCre
51580 61 74 65 20 20 20 20 20 20 20 20 20 20 20 20 20 ate
51590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
515a0 32 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 46 6f 27.#define OP_Fo
515b0 75 6e 64 20 20 20 20 20 20 20 20 20 20 20 20 20 und
515c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
515d0 20 31 32 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 128.#define OP_
515e0 49 66 50 6f 73 20 20 20 20 20 20 20 20 20 20 20 IfPos
515f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51600 20 20 20 31 32 39 0a 23 64 65 66 69 6e 65 20 4f 129.#define O
51610 50 5f 4e 75 6c 6c 52 6f 77 20 20 20 20 20 20 20 P_NullRow
51620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51630 20 20 20 20 20 31 33 31 0a 23 64 65 66 69 6e 65 131.#define
51640 20 4f 50 5f 4a 75 6d 70 20 20 20 20 20 20 20 20 OP_Jump
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 31 33 32 0a 23 64 65 66 69 132.#defi
51670 6e 65 20 4f 50 5f 50 65 72 6d 75 74 61 74 69 6f ne OP_Permutatio
51680 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 n
51690 20 20 20 20 20 20 20 20 20 31 33 33 0a 0a 2f 2a 133../*
516a0 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6f The following o
516b0 70 63 6f 64 65 20 76 61 6c 75 65 73 20 61 72 65 pcode values are
516c0 20 6e 65 76 65 72 20 75 73 65 64 20 2a 2f 0a 23 never used */.#
516d0 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 55 73 65 define OP_NotUse
516e0 64 5f 31 33 34 20 20 20 20 20 20 20 20 20 20 20 d_134
516f0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33 34 134
51700 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 55 .#define OP_NotU
51710 73 65 64 5f 31 33 35 20 20 20 20 20 20 20 20 20 sed_135
51720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
51730 33 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 35.#define OP_No
51740 74 55 73 65 64 5f 31 33 36 20 20 20 20 20 20 20 tUsed_136
51750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51760 20 31 33 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 136.#define OP_
51770 4e 6f 74 55 73 65 64 5f 31 33 37 20 20 20 20 20 NotUsed_137
51780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51790 20 20 20 31 33 37 0a 23 64 65 66 69 6e 65 20 4f 137.#define O
517a0 50 5f 4e 6f 74 55 73 65 64 5f 31 33 38 20 20 20 P_NotUsed_138
517b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
517c0 20 20 20 20 20 31 33 38 0a 23 64 65 66 69 6e 65 138.#define
517d0 20 4f 50 5f 4e 6f 74 55 73 65 64 5f 31 33 39 20 OP_NotUsed_139
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 31 33 39 0a 23 64 65 66 69 139.#defi
51800 6e 65 20 4f 50 5f 4e 6f 74 55 73 65 64 5f 31 34 ne OP_NotUsed_14
51810 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0
51820 20 20 20 20 20 20 20 20 20 31 34 30 0a 0a 0a 2f 140.../
51830 2a 20 50 72 6f 70 65 72 74 69 65 73 20 73 75 63 * Properties suc
51840 68 20 61 73 20 22 6f 75 74 32 22 20 6f 72 20 22 h as "out2" or "
51850 6a 75 6d 70 22 20 74 68 61 74 20 61 72 65 20 73 jump" that are s
51860 70 65 63 69 66 69 65 64 20 69 6e 0a 2a 2a 20 63 pecified in.** c
51870 6f 6d 6d 65 6e 74 73 20 66 6f 6c 6c 6f 77 69 6e omments followin
51880 67 20 74 68 65 20 22 63 61 73 65 22 20 66 6f 72 g the "case" for
51890 20 65 61 63 68 20 6f 70 63 6f 64 65 20 69 6e 20 each opcode in
518a0 74 68 65 20 76 64 62 65 2e 63 0a 2a 2a 20 61 72 the vdbe.c.** ar
518b0 65 20 65 6e 63 6f 64 65 64 20 69 6e 74 6f 20 62 e encoded into b
518c0 69 74 76 65 63 74 6f 72 73 20 61 73 20 66 6f 6c itvectors as fol
518d0 6c 6f 77 73 3a 0a 2a 2f 0a 23 64 65 66 69 6e 65 lows:.*/.#define
518e0 20 4f 50 46 4c 47 5f 4a 55 4d 50 20 20 20 20 20 OPFLG_JUMP
518f0 20 20 20 20 20 20 20 30 78 30 30 30 31 20 20 2f 0x0001 /
51900 2a 20 6a 75 6d 70 3a 20 20 50 32 20 68 6f 6c 64 * jump: P2 hold
51910 73 20 6a 6d 70 20 74 61 72 67 65 74 20 2a 2f 0a s jmp target */.
51920 23 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f 4f 55 #define OPFLG_OU
51930 54 32 5f 50 52 45 52 45 4c 45 41 53 45 20 30 78 T2_PRERELEASE 0x
51940 30 30 30 32 20 20 2f 2a 20 6f 75 74 32 2d 70 72 0002 /* out2-pr
51950 65 72 65 6c 65 61 73 65 3a 20 2a 2f 0a 23 64 65 erelease: */.#de
51960 66 69 6e 65 20 4f 50 46 4c 47 5f 49 4e 31 20 20 fine OPFLG_IN1
51970 20 20 20 20 20 20 20 20 20 20 20 30 78 30 30 30 0x000
51980 34 20 20 2f 2a 20 69 6e 31 3a 20 20 20 50 31 20 4 /* in1: P1
51990 69 73 20 61 6e 20 69 6e 70 75 74 20 2a 2f 0a 23 is an input */.#
519a0 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f 49 4e 32 define OPFLG_IN2
519b0 20 20 20 20 20 20 20 20 20 20 20 20 20 30 78 30 0x0
519c0 30 30 38 20 20 2f 2a 20 69 6e 32 3a 20 20 20 50 008 /* in2: P
519d0 32 20 69 73 20 61 6e 20 69 6e 70 75 74 20 2a 2f 2 is an input */
519e0 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f 49 .#define OPFLG_I
519f0 4e 33 20 20 20 20 20 20 20 20 20 20 20 20 20 30 N3 0
51a00 78 30 30 31 30 20 20 2f 2a 20 69 6e 33 3a 20 20 x0010 /* in3:
51a10 20 50 33 20 69 73 20 61 6e 20 69 6e 70 75 74 20 P3 is an input
51a20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 47 */.#define OPFLG
51a30 5f 4f 55 54 33 20 20 20 20 20 20 20 20 20 20 20 _OUT3
51a40 20 30 78 30 30 32 30 20 20 2f 2a 20 6f 75 74 33 0x0020 /* out3
51a50 3a 20 20 50 33 20 69 73 20 61 6e 20 6f 75 74 70 : P3 is an outp
51a60 75 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 ut */.#define OP
51a70 46 4c 47 5f 49 4e 49 54 49 41 4c 49 5a 45 52 20 FLG_INITIALIZER
51a80 7b 5c 0a 2f 2a 20 20 20 30 20 2a 2f 20 30 78 30 {\./* 0 */ 0x0
51a90 30 2c 20 30 78 30 31 2c 20 30 78 30 30 2c 20 30 0, 0x01, 0x00, 0
51aa0 78 30 30 2c 20 30 78 31 30 2c 20 30 78 30 38 2c x00, 0x10, 0x08,
51ab0 20 30 78 30 32 2c 20 30 78 30 30 2c 5c 0a 2f 2a 0x02, 0x00,\./*
51ac0 20 20 20 38 20 2a 2f 20 30 78 30 30 2c 20 30 78 8 */ 0x00, 0x
51ad0 30 34 2c 20 30 78 30 30 2c 20 30 78 30 35 2c 20 04, 0x00, 0x05,
51ae0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
51af0 2c 20 30 78 30 30 2c 5c 0a 2f 2a 20 20 31 36 20 , 0x00,\./* 16
51b00 2a 2f 20 30 78 30 32 2c 20 30 78 30 30 2c 20 30 */ 0x02, 0x00, 0
51b10 78 30 31 2c 20 30 78 30 34 2c 20 30 78 30 34 2c x01, 0x04, 0x04,
51b20 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
51b30 35 2c 5c 0a 2f 2a 20 20 32 34 20 2a 2f 20 30 78 5,\./* 24 */ 0x
51b40 30 30 2c 20 30 78 30 31 2c 20 30 78 30 34 2c 20 00, 0x01, 0x04,
51b50 30 78 30 32 2c 20 30 78 30 30 2c 20 30 78 30 30 0x02, 0x00, 0x00
51b60 2c 20 30 78 30 32 2c 20 30 78 30 34 2c 5c 0a 2f , 0x02, 0x04,\./
51b70 2a 20 20 33 32 20 2a 2f 20 30 78 30 30 2c 20 30 * 32 */ 0x00, 0
51b80 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
51b90 20 30 78 30 32 2c 20 30 78 31 31 2c 20 30 78 31 0x02, 0x11, 0x1
51ba0 31 2c 20 30 78 30 32 2c 5c 0a 2f 2a 20 20 34 30 1, 0x02,\./* 40
51bb0 20 2a 2f 20 30 78 30 35 2c 20 30 78 30 30 2c 20 */ 0x05, 0x00,
51bc0 30 78 30 32 2c 20 30 78 31 31 2c 20 30 78 30 34 0x02, 0x11, 0x04
51bd0 2c 20 30 78 30 30 2c 20 30 78 30 38 2c 20 30 78 , 0x00, 0x08, 0x
51be0 31 31 2c 5c 0a 2f 2a 20 20 34 38 20 2a 2f 20 30 11,\./* 48 */ 0
51bf0 78 30 31 2c 20 30 78 30 32 2c 20 30 78 30 31 2c x01, 0x02, 0x01,
51c00 20 30 78 32 31 2c 20 30 78 30 38 2c 20 30 78 30 0x21, 0x08, 0x0
51c10 30 2c 20 30 78 30 32 2c 20 30 78 30 31 2c 5c 0a 0, 0x02, 0x01,\.
51c20 2f 2a 20 20 35 36 20 2a 2f 20 30 78 31 31 2c 20 /* 56 */ 0x11,
51c30 30 78 30 31 2c 20 30 78 30 32 2c 20 30 78 30 30 0x01, 0x02, 0x00
51c40 2c 20 30 78 30 34 2c 20 30 78 30 30 2c 20 30 78 , 0x04, 0x00, 0x
51c50 30 30 2c 20 30 78 30 32 2c 5c 0a 2f 2a 20 20 36 00, 0x02,\./* 6
51c60 34 20 2a 2f 20 30 78 31 31 2c 20 30 78 30 30 2c 4 */ 0x11, 0x00,
51c70 20 30 78 30 30 2c 20 30 78 30 35 2c 20 30 78 32 0x00, 0x05, 0x2
51c80 63 2c 20 30 78 32 63 2c 20 30 78 30 30 2c 20 30 c, 0x2c, 0x00, 0
51c90 78 31 31 2c 5c 0a 2f 2a 20 20 37 32 20 2a 2f 20 x11,\./* 72 */
51ca0 30 78 30 30 2c 20 30 78 30 35 2c 20 30 78 30 35 0x00, 0x05, 0x05
51cb0 2c 20 30 78 31 35 2c 20 30 78 31 35 2c 20 30 78 , 0x15, 0x15, 0x
51cc0 31 35 2c 20 30 78 31 35 2c 20 30 78 31 35 2c 5c 15, 0x15, 0x15,\
51cd0 0a 2f 2a 20 20 38 30 20 2a 2f 20 30 78 31 35 2c ./* 80 */ 0x15,
51ce0 20 30 78 30 30 2c 20 30 78 32 63 2c 20 30 78 32 0x00, 0x2c, 0x2
51cf0 63 2c 20 30 78 32 63 2c 20 30 78 32 63 2c 20 30 c, 0x2c, 0x2c, 0
51d00 78 32 63 2c 20 30 78 32 63 2c 5c 0a 2f 2a 20 20 x2c, 0x2c,\./*
51d10 38 38 20 2a 2f 20 30 78 32 63 2c 20 30 78 32 63 88 */ 0x2c, 0x2c
51d20 2c 20 30 78 32 63 2c 20 30 78 32 63 2c 20 30 78 , 0x2c, 0x2c, 0x
51d30 30 30 2c 20 30 78 30 34 2c 20 30 78 30 32 2c 20 00, 0x04, 0x02,
51d40 30 78 30 30 2c 5c 0a 2f 2a 20 20 39 36 20 2a 2f 0x00,\./* 96 */
51d50 20 30 78 30 30 2c 20 30 78 30 31 2c 20 30 78 30 0x00, 0x01, 0x0
51d60 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
51d70 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 31 2c x00, 0x00, 0x01,
51d80 5c 0a 2f 2a 20 31 30 34 20 2a 2f 20 30 78 30 30 \./* 104 */ 0x00
51d90 2c 20 30 78 30 31 2c 20 30 78 30 32 2c 20 30 78 , 0x01, 0x02, 0x
51da0 30 38 2c 20 30 78 30 31 2c 20 30 78 31 31 2c 20 08, 0x01, 0x11,
51db0 30 78 30 30 2c 20 30 78 30 32 2c 5c 0a 2f 2a 20 0x00, 0x02,\./*
51dc0 31 31 32 20 2a 2f 20 30 78 30 32 2c 20 30 78 31 112 */ 0x02, 0x1
51dd0 35 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 5, 0x00, 0x00, 0
51de0 78 31 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x10, 0x00, 0x00,
51df0 20 30 78 30 32 2c 5c 0a 2f 2a 20 31 32 30 20 2a 0x02,\./* 120 *
51e00 2f 20 30 78 30 30 2c 20 30 78 30 32 2c 20 30 78 / 0x00, 0x02, 0x
51e10 30 31 2c 20 30 78 31 31 2c 20 30 78 30 30 2c 20 01, 0x11, 0x00,
51e20 30 78 30 30 2c 20 30 78 30 35 2c 20 30 78 30 30 0x00, 0x05, 0x00
51e30 2c 5c 0a 2f 2a 20 31 32 38 20 2a 2f 20 30 78 31 ,\./* 128 */ 0x1
51e40 31 2c 20 30 78 30 35 2c 20 30 78 30 32 2c 20 30 1, 0x05, 0x02, 0
51e50 78 30 30 2c 20 30 78 30 31 2c 20 30 78 30 30 2c x00, 0x01, 0x00,
51e60 20 30 78 30 30 2c 20 30 78 30 30 2c 5c 0a 2f 2a 0x00, 0x00,\./*
51e70 20 31 33 36 20 2a 2f 20 30 78 30 30 2c 20 30 78 136 */ 0x00, 0x
51e80 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
51e90 30 78 30 30 2c 20 30 78 30 34 2c 20 30 78 30 34 0x00, 0x04, 0x04
51ea0 2c 20 30 78 30 34 2c 5c 0a 2f 2a 20 31 34 34 20 , 0x04,\./* 144
51eb0 2a 2f 20 30 78 30 34 2c 20 30 78 30 34 2c 7d 0a */ 0x04, 0x04,}.
51ec0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
51ed0 20 45 6e 64 20 6f 66 20 6f 70 63 6f 64 65 73 2e End of opcodes.
51ee0 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a h **************
51ef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
51f00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
51f10 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
51f20 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 Continuing wher
51f30 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e e we left off in
51f40 20 76 64 62 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a vdbe.h ********
51f50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
51f60 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 74 6f 74 79 70 ../*.** Prototyp
51f70 65 73 20 66 6f 72 20 74 68 65 20 56 44 42 45 20 es for the VDBE
51f80 69 6e 74 65 72 66 61 63 65 2e 20 20 53 65 65 20 interface. See
51f90 63 6f 6d 6d 65 6e 74 73 20 6f 6e 20 74 68 65 20 comments on the
51fa0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a implementation.*
51fb0 2a 20 66 6f 72 20 61 20 64 65 73 63 72 69 70 74 * for a descript
51fc0 69 6f 6e 20 6f 66 20 77 68 61 74 20 65 61 63 68 ion of what each
51fd0 20 6f 66 20 74 68 65 73 65 20 72 6f 75 74 69 6e of these routin
51fe0 65 73 20 64 6f 65 73 2e 0a 2a 2f 0a 53 51 4c 49 es does..*/.SQLI
51ff0 54 45 5f 50 52 49 56 41 54 45 20 56 64 62 65 20 TE_PRIVATE Vdbe
52000 2a 73 71 6c 69 74 65 33 56 64 62 65 43 72 65 61 *sqlite3VdbeCrea
52010 74 65 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 te(sqlite3*);.SQ
52020 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
52030 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
52040 70 30 28 56 64 62 65 2a 2c 69 6e 74 29 3b 0a 53 p0(Vdbe*,int);.S
52050 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
52060 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 t sqlite3VdbeAdd
52070 4f 70 31 28 56 64 62 65 2a 2c 69 6e 74 2c 69 6e Op1(Vdbe*,int,in
52080 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
52090 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
520a0 62 65 41 64 64 4f 70 32 28 56 64 62 65 2a 2c 69 beAddOp2(Vdbe*,i
520b0 6e 74 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c nt,int,int);.SQL
520c0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
520d0 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
520e0 33 28 56 64 62 65 2a 2c 69 6e 74 2c 69 6e 74 2c 3(Vdbe*,int,int,
520f0 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 int,int);.SQLITE
52100 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
52110 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 56 ite3VdbeAddOp4(V
52120 64 62 65 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 dbe*,int,int,int
52130 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 20 ,int,const char
52140 2a 7a 50 34 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 *zP4,int);.SQLIT
52150 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
52160 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69 lite3VdbeAddOpLi
52170 73 74 28 56 64 62 65 2a 2c 20 69 6e 74 20 6e 4f st(Vdbe*, int nO
52180 70 2c 20 56 64 62 65 4f 70 4c 69 73 74 20 63 6f p, VdbeOpList co
52190 6e 73 74 20 2a 61 4f 70 29 3b 0a 53 51 4c 49 54 nst *aOp);.SQLIT
521a0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
521b0 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 qlite3VdbeChange
521c0 50 31 28 56 64 62 65 2a 2c 20 69 6e 74 20 61 64 P1(Vdbe*, int ad
521d0 64 72 2c 20 69 6e 74 20 50 31 29 3b 0a 53 51 4c dr, int P1);.SQL
521e0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
521f0 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e sqlite3VdbeChan
52200 67 65 50 32 28 56 64 62 65 2a 2c 20 69 6e 74 20 geP2(Vdbe*, int
52210 61 64 64 72 2c 20 69 6e 74 20 50 32 29 3b 0a 53 addr, int P2);.S
52220 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
52230 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 id sqlite3VdbeCh
52240 61 6e 67 65 50 33 28 56 64 62 65 2a 2c 20 69 6e angeP3(Vdbe*, in
52250 74 20 61 64 64 72 2c 20 69 6e 74 20 50 33 29 3b t addr, int P3);
52260 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
52270 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
52280 43 68 61 6e 67 65 50 35 28 56 64 62 65 2a 2c 20 ChangeP5(Vdbe*,
52290 75 38 20 50 35 29 3b 0a 53 51 4c 49 54 45 5f 50 u8 P5);.SQLITE_P
522a0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
522b0 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 te3VdbeJumpHere(
522c0 56 64 62 65 2a 2c 20 69 6e 74 20 61 64 64 72 29 Vdbe*, int addr)
522d0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
522e0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
522f0 65 43 68 61 6e 67 65 54 6f 4e 6f 6f 70 28 56 64 eChangeToNoop(Vd
52300 62 65 2a 2c 20 69 6e 74 20 61 64 64 72 2c 20 69 be*, int addr, i
52310 6e 74 20 4e 29 3b 0a 53 51 4c 49 54 45 5f 50 52 nt N);.SQLITE_PR
52320 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
52330 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 56 e3VdbeChangeP4(V
52340 64 62 65 2a 2c 20 69 6e 74 20 61 64 64 72 2c 20 dbe*, int addr,
52350 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 34 2c const char *zP4,
52360 20 69 6e 74 20 4e 29 3b 0a 53 51 4c 49 54 45 5f int N);.SQLITE_
52370 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
52380 69 74 65 33 56 64 62 65 55 73 65 73 42 74 72 65 ite3VdbeUsesBtre
52390 65 28 56 64 62 65 2a 2c 20 69 6e 74 29 3b 0a 53 e(Vdbe*, int);.S
523a0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 56 64 QLITE_PRIVATE Vd
523b0 62 65 4f 70 20 2a 73 71 6c 69 74 65 33 56 64 62 beOp *sqlite3Vdb
523c0 65 47 65 74 4f 70 28 56 64 62 65 2a 2c 20 69 6e eGetOp(Vdbe*, in
523d0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
523e0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
523f0 62 65 4d 61 6b 65 4c 61 62 65 6c 28 56 64 62 65 beMakeLabel(Vdbe
52400 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
52410 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
52420 64 62 65 44 65 6c 65 74 65 28 56 64 62 65 2a 29 dbeDelete(Vdbe*)
52430 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
52440 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
52450 65 4d 61 6b 65 52 65 61 64 79 28 56 64 62 65 2a eMakeReady(Vdbe*
52460 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 ,int,int,int,int
52470 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 ,int,int);.SQLIT
52480 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
52490 6c 69 74 65 33 56 64 62 65 46 69 6e 61 6c 69 7a lite3VdbeFinaliz
524a0 65 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 e(Vdbe*);.SQLITE
524b0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
524c0 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 lite3VdbeResolve
524d0 4c 61 62 65 6c 28 56 64 62 65 2a 2c 20 69 6e 74 Label(Vdbe*, int
524e0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
524f0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
52500 65 43 75 72 72 65 6e 74 41 64 64 72 28 56 64 62 eCurrentAddr(Vdb
52510 65 2a 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 e*);.#ifdef SQLI
52520 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 5f TE_DEBUG.SQLITE_
52530 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 PRIVATE int sq
52540 6c 69 74 65 33 56 64 62 65 41 73 73 65 72 74 4d lite3VdbeAssertM
52550 61 79 41 62 6f 72 74 28 56 64 62 65 20 2a 2c 20 ayAbort(Vdbe *,
52560 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
52570 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 VATE void sqli
52580 74 65 33 56 64 62 65 54 72 61 63 65 28 56 64 62 te3VdbeTrace(Vdb
52590 65 2a 2c 46 49 4c 45 2a 29 3b 0a 23 65 6e 64 69 e*,FILE*);.#endi
525a0 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 f.SQLITE_PRIVATE
525b0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
525c0 65 52 65 73 65 74 53 74 65 70 52 65 73 75 6c 74 eResetStepResult
525d0 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f (Vdbe*);.SQLITE_
525e0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
525f0 74 65 33 56 64 62 65 52 65 73 65 74 28 56 64 62 te3VdbeReset(Vdb
52600 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 e*);.SQLITE_PRIV
52610 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
52620 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 56 VdbeSetNumCols(V
52630 64 62 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 dbe*,int);.SQLIT
52640 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
52650 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e lite3VdbeSetColN
52660 61 6d 65 28 56 64 62 65 2a 2c 20 69 6e 74 2c 20 ame(Vdbe*, int,
52670 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 int, const char
52680 2a 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a *, void(*)(void*
52690 29 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ));.SQLITE_PRIVA
526a0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
526b0 64 62 65 43 6f 75 6e 74 43 68 61 6e 67 65 73 28 dbeCountChanges(
526c0 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 Vdbe*);.SQLITE_P
526d0 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 20 2a RIVATE sqlite3 *
526e0 73 71 6c 69 74 65 33 56 64 62 65 44 62 28 56 64 sqlite3VdbeDb(Vd
526f0 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 be*);.SQLITE_PRI
52700 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
52710 33 56 64 62 65 53 65 74 53 71 6c 28 56 64 62 65 3VdbeSetSql(Vdbe
52720 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a *, const char *z
52730 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 29 3b 0a 53 , int n, int);.S
52740 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
52750 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 53 77 id sqlite3VdbeSw
52760 61 70 28 56 64 62 65 2a 2c 56 64 62 65 2a 29 3b ap(Vdbe*,Vdbe*);
52770 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
52780 56 64 62 65 4f 70 20 2a 73 71 6c 69 74 65 33 56 VdbeOp *sqlite3V
52790 64 62 65 54 61 6b 65 4f 70 41 72 72 61 79 28 56 dbeTakeOpArray(V
527a0 64 62 65 2a 2c 20 69 6e 74 2a 2c 20 69 6e 74 2a dbe*, int*, int*
527b0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
527c0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 E void sqlite3Vd
527d0 62 65 50 72 6f 67 72 61 6d 44 65 6c 65 74 65 28 beProgramDelete(
527e0 73 71 6c 69 74 65 33 20 2a 2c 20 53 75 62 50 72 sqlite3 *, SubPr
527f0 6f 67 72 61 6d 20 2a 2c 20 69 6e 74 29 3b 0a 0a ogram *, int);..
52800 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e #ifdef SQLITE_EN
52810 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 ABLE_MEMORY_MANA
52820 47 45 4d 45 4e 54 0a 53 51 4c 49 54 45 5f 50 52 GEMENT.SQLITE_PR
52830 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
52840 33 56 64 62 65 52 65 6c 65 61 73 65 4d 65 6d 6f 3VdbeReleaseMemo
52850 72 79 28 69 6e 74 29 3b 0a 23 65 6e 64 69 66 0a ry(int);.#endif.
52860 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 55 SQLITE_PRIVATE U
52870 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a 73 npackedRecord *s
52880 71 6c 69 74 65 33 56 64 62 65 52 65 63 6f 72 64 qlite3VdbeRecord
52890 55 6e 70 61 63 6b 28 4b 65 79 49 6e 66 6f 2a 2c Unpack(KeyInfo*,
528a0 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c int,const void*,
528b0 63 68 61 72 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 char*,int);.SQLI
528c0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
528d0 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 sqlite3VdbeDelet
528e0 65 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 28 eUnpackedRecord(
528f0 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 2a 29 UnpackedRecord*)
52900 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
52910 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 int sqlite3Vdbe
52920 52 65 63 6f 72 64 43 6f 6d 70 61 72 65 28 69 6e RecordCompare(in
52930 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 55 6e t,const void*,Un
52940 70 61 63 6b 65 64 52 65 63 6f 72 64 2a 29 3b 0a packedRecord*);.
52950 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 ..#ifndef NDEBUG
52960 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
52970 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 void sqlite3Vd
52980 62 65 43 6f 6d 6d 65 6e 74 28 56 64 62 65 2a 2c beComment(Vdbe*,
52990 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e const char*, ..
529a0 2e 29 3b 0a 23 20 64 65 66 69 6e 65 20 56 64 62 .);.# define Vdb
529b0 65 43 6f 6d 6d 65 6e 74 28 58 29 20 20 73 71 6c eComment(X) sql
529c0 69 74 65 33 56 64 62 65 43 6f 6d 6d 65 6e 74 20 ite3VdbeComment
529d0 58 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 X.SQLITE_PRIVATE
529e0 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 void sqlite3V
529f0 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 56 dbeNoopComment(V
52a00 64 62 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 dbe*, const char
52a10 2a 2c 20 2e 2e 2e 29 3b 0a 23 20 64 65 66 69 6e *, ...);.# defin
52a20 65 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e e VdbeNoopCommen
52a30 74 28 58 29 20 20 73 71 6c 69 74 65 33 56 64 62 t(X) sqlite3Vdb
52a40 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 20 58 0a 23 eNoopComment X.#
52a50 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 56 64 else.# define Vd
52a60 62 65 43 6f 6d 6d 65 6e 74 28 58 29 0a 23 20 64 beComment(X).# d
52a70 65 66 69 6e 65 20 56 64 62 65 4e 6f 6f 70 43 6f efine VdbeNoopCo
52a80 6d 6d 65 6e 74 28 58 29 0a 23 65 6e 64 69 66 0a mment(X).#endif.
52a90 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a .#endif../******
52aa0 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 ******** End of
52ab0 76 64 62 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a vdbe.h *********
52ac0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
52ad0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
52ae0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
52af0 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 ******** Continu
52b00 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 ing where we lef
52b10 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 49 t off in sqliteI
52b20 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a nt.h ***********
52b30 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
52b40 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 ******** Include
52b50 20 70 61 67 65 72 2e 68 20 69 6e 20 74 68 65 20 pager.h in the
52b60 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 middle of sqlite
52b70 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a Int.h **********
52b80 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
52b90 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 ******** Begin f
52ba0 69 6c 65 20 70 61 67 65 72 2e 68 20 2a 2a 2a 2a ile pager.h ****
52bb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
52bc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
52bd0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 *******/./*.** 2
52be0 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 001 September 15
52bf0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
52c00 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
52c10 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
52c20 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
52c30 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
52c40 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
52c50 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
52c60 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
52c70 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
52c80 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
52c90 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
52ca0 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
52cb0 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
52cc0 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
52cd0 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
52ce0 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
52cf0 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
52d00 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
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 2a 2a 2a 2a 2a 2a ****************
52d30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
52d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
52d50 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72 20 66 ** This header f
52d60 69 6c 65 20 64 65 66 69 6e 65 73 20 74 68 65 20 ile defines the
52d70 69 6e 74 65 72 66 61 63 65 20 74 68 61 74 20 74 interface that t
52d80 68 65 20 73 71 6c 69 74 65 20 70 61 67 65 20 63 he sqlite page c
52d90 61 63 68 65 0a 2a 2a 20 73 75 62 73 79 73 74 65 ache.** subsyste
52da0 6d 2e 20 20 54 68 65 20 70 61 67 65 20 63 61 63 m. The page cac
52db0 68 65 20 73 75 62 73 79 73 74 65 6d 20 72 65 61 he subsystem rea
52dc0 64 73 20 61 6e 64 20 77 72 69 74 65 73 20 61 20 ds and writes a
52dd0 66 69 6c 65 20 61 20 70 61 67 65 0a 2a 2a 20 61 file a page.** a
52de0 74 20 61 20 74 69 6d 65 20 61 6e 64 20 70 72 6f t a time and pro
52df0 76 69 64 65 73 20 61 20 6a 6f 75 72 6e 61 6c 20 vides a journal
52e00 66 6f 72 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a for rollback..**
52e10 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a 20 70 61 .** @(#) $Id: pa
52e20 67 65 72 2e 68 2c 76 20 31 2e 31 30 34 20 32 30 ger.h,v 1.104 20
52e30 30 39 2f 30 37 2f 32 34 20 31 39 3a 30 31 3a 31 09/07/24 19:01:1
52e40 39 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 9 drh Exp $.*/..
52e50 23 69 66 6e 64 65 66 20 5f 50 41 47 45 52 5f 48 #ifndef _PAGER_H
52e60 5f 0a 23 64 65 66 69 6e 65 20 5f 50 41 47 45 52 _.#define _PAGER
52e70 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 61 75 _H_../*.** Defau
52e80 6c 74 20 6d 61 78 69 6d 75 6d 20 73 69 7a 65 20 lt maximum size
52e90 66 6f 72 20 70 65 72 73 69 73 74 65 6e 74 20 6a for persistent j
52ea0 6f 75 72 6e 61 6c 20 66 69 6c 65 73 2e 20 41 20 ournal files. A
52eb0 6e 65 67 61 74 69 76 65 20 0a 2a 2a 20 76 61 6c negative .** val
52ec0 75 65 20 6d 65 61 6e 73 20 6e 6f 20 6c 69 6d 69 ue means no limi
52ed0 74 2e 20 54 68 69 73 20 76 61 6c 75 65 20 6d 61 t. This value ma
52ee0 79 20 62 65 20 6f 76 65 72 72 69 64 64 65 6e 20 y be overridden
52ef0 75 73 69 6e 67 20 74 68 65 20 0a 2a 2a 20 73 71 using the .** sq
52f00 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e 61 lite3PagerJourna
52f10 6c 53 69 7a 65 4c 69 6d 69 74 28 29 20 41 50 49 lSizeLimit() API
52f20 2e 20 53 65 65 20 61 6c 73 6f 20 22 50 52 41 47 . See also "PRAG
52f30 4d 41 20 6a 6f 75 72 6e 61 6c 5f 73 69 7a 65 5f MA journal_size_
52f40 6c 69 6d 69 74 22 2e 0a 2a 2f 0a 23 69 66 6e 64 limit"..*/.#ifnd
52f50 65 66 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c ef SQLITE_DEFAUL
52f60 54 5f 4a 4f 55 52 4e 41 4c 5f 53 49 5a 45 5f 4c T_JOURNAL_SIZE_L
52f70 49 4d 49 54 0a 20 20 23 64 65 66 69 6e 65 20 53 IMIT. #define S
52f80 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 4a 4f QLITE_DEFAULT_JO
52f90 55 52 4e 41 4c 5f 53 49 5a 45 5f 4c 49 4d 49 54 URNAL_SIZE_LIMIT
52fa0 20 2d 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a -1.#endif../*.*
52fb0 2a 20 54 68 65 20 74 79 70 65 20 75 73 65 64 20 * The type used
52fc0 74 6f 20 72 65 70 72 65 73 65 6e 74 20 61 20 70 to represent a p
52fd0 61 67 65 20 6e 75 6d 62 65 72 2e 20 20 54 68 65 age number. The
52fe0 20 66 69 72 73 74 20 70 61 67 65 20 69 6e 20 61 first page in a
52ff0 20 66 69 6c 65 0a 2a 2a 20 69 73 20 63 61 6c 6c file.** is call
53000 65 64 20 70 61 67 65 20 31 2e 20 20 30 20 69 73 ed page 1. 0 is
53010 20 75 73 65 64 20 74 6f 20 72 65 70 72 65 73 65 used to represe
53020 6e 74 20 22 6e 6f 74 20 61 20 70 61 67 65 22 2e nt "not a page".
53030 0a 2a 2f 0a 74 79 70 65 64 65 66 20 75 33 32 20 .*/.typedef u32
53040 50 67 6e 6f 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 Pgno;../*.** Eac
53050 68 20 6f 70 65 6e 20 66 69 6c 65 20 69 73 20 6d h open file is m
53060 61 6e 61 67 65 64 20 62 79 20 61 20 73 65 70 61 anaged by a sepa
53070 72 61 74 65 20 69 6e 73 74 61 6e 63 65 20 6f 66 rate instance of
53080 20 74 68 65 20 22 50 61 67 65 72 22 20 73 74 72 the "Pager" str
53090 75 63 74 75 72 65 2e 0a 2a 2f 0a 74 79 70 65 64 ucture..*/.typed
530a0 65 66 20 73 74 72 75 63 74 20 50 61 67 65 72 20 ef struct Pager
530b0 50 61 67 65 72 3b 0a 0a 2f 2a 0a 2a 2a 20 48 61 Pager;../*.** Ha
530c0 6e 64 6c 65 20 74 79 70 65 20 66 6f 72 20 70 61 ndle type for pa
530d0 67 65 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 ges..*/.typedef
530e0 73 74 72 75 63 74 20 50 67 48 64 72 20 44 62 50 struct PgHdr DbP
530f0 61 67 65 3b 0a 0a 2f 2a 0a 2a 2a 20 50 61 67 65 age;../*.** Page
53100 20 6e 75 6d 62 65 72 20 50 41 47 45 52 5f 4d 4a number PAGER_MJ
53110 5f 50 47 4e 4f 20 69 73 20 6e 65 76 65 72 20 75 _PGNO is never u
53120 73 65 64 20 69 6e 20 61 6e 20 53 51 4c 69 74 65 sed in an SQLite
53130 20 64 61 74 61 62 61 73 65 20 28 69 74 20 69 73 database (it is
53140 0a 2a 2a 20 72 65 73 65 72 76 65 64 20 66 6f 72 .** reserved for
53150 20 77 6f 72 6b 69 6e 67 20 61 72 6f 75 6e 64 20 working around
53160 61 20 77 69 6e 64 6f 77 73 2f 70 6f 73 69 78 20 a windows/posix
53170 69 6e 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 29 incompatibility)
53180 2e 20 49 74 20 69 73 0a 2a 2a 20 75 73 65 64 20 . It is.** used
53190 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 74 in the journal t
531a0 6f 20 73 69 67 6e 69 66 79 20 74 68 61 74 20 74 o signify that t
531b0 68 65 20 72 65 6d 61 69 6e 64 65 72 20 6f 66 20 he remainder of
531c0 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
531d0 20 0a 2a 2a 20 69 73 20 64 65 76 6f 74 65 64 20 .** is devoted
531e0 74 6f 20 73 74 6f 72 69 6e 67 20 61 20 6d 61 73 to storing a mas
531f0 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 ter journal name
53200 20 2d 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 - there are no
53210 6d 6f 72 65 20 70 61 67 65 73 20 74 6f 0a 2a 2a more pages to.**
53220 20 72 6f 6c 6c 20 62 61 63 6b 2e 20 53 65 65 20 roll back. See
53230 63 6f 6d 6d 65 6e 74 73 20 66 6f 72 20 66 75 6e comments for fun
53240 63 74 69 6f 6e 20 77 72 69 74 65 4d 61 73 74 65 ction writeMaste
53250 72 4a 6f 75 72 6e 61 6c 28 29 20 69 6e 20 70 61 rJournal() in pa
53260 67 65 72 2e 63 20 0a 2a 2a 20 66 6f 72 20 64 65 ger.c .** for de
53270 74 61 69 6c 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e tails..*/.#defin
53280 65 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 e PAGER_MJ_PGNO(
53290 78 29 20 28 28 50 67 6e 6f 29 28 28 50 45 4e 44 x) ((Pgno)((PEND
532a0 49 4e 47 5f 42 59 54 45 2f 28 28 78 29 2d 3e 70 ING_BYTE/((x)->p
532b0 61 67 65 53 69 7a 65 29 29 2b 31 29 29 0a 0a 2f ageSize))+1))../
532c0 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c *.** Allowed val
532d0 75 65 73 20 66 6f 72 20 74 68 65 20 66 6c 61 67 ues for the flag
532e0 73 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 s parameter to s
532f0 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e 28 qlite3PagerOpen(
53300 29 2e 0a 2a 2a 0a 2a 2a 20 4e 4f 54 45 3a 20 54 )..**.** NOTE: T
53310 68 65 73 65 20 76 61 6c 75 65 73 20 6d 75 73 74 hese values must
53320 20 6d 61 74 63 68 20 74 68 65 20 63 6f 72 72 65 match the corre
53330 73 70 6f 6e 64 69 6e 67 20 42 54 52 45 45 5f 20 sponding BTREE_
53340 76 61 6c 75 65 73 20 69 6e 20 62 74 72 65 65 2e values in btree.
53350 68 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 h..*/.#define PA
53360 47 45 52 5f 4f 4d 49 54 5f 4a 4f 55 52 4e 41 4c GER_OMIT_JOURNAL
53370 20 20 30 78 30 30 30 31 20 20 20 20 2f 2a 20 44 0x0001 /* D
53380 6f 20 6e 6f 74 20 75 73 65 20 61 20 72 6f 6c 6c o not use a roll
53390 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a back journal */.
533a0 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 4e 4f #define PAGER_NO
533b0 5f 52 45 41 44 4c 4f 43 4b 20 20 20 30 78 30 30 _READLOCK 0x00
533c0 30 32 20 20 20 20 2f 2a 20 4f 6d 69 74 20 72 65 02 /* Omit re
533d0 61 64 6c 6f 63 6b 73 20 6f 6e 20 72 65 61 64 6f adlocks on reado
533e0 6e 6c 79 20 66 69 6c 65 73 20 2a 2f 0a 0a 2f 2a nly files */../*
533f0 0a 2a 2a 20 56 61 6c 69 64 20 76 61 6c 75 65 73 .** Valid values
53400 20 66 6f 72 20 74 68 65 20 73 65 63 6f 6e 64 20 for the second
53410 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 argument to sqli
53420 74 65 33 50 61 67 65 72 4c 6f 63 6b 69 6e 67 4d te3PagerLockingM
53430 6f 64 65 28 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e ode()..*/.#defin
53440 65 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d e PAGER_LOCKINGM
53450 4f 44 45 5f 51 55 45 52 59 20 20 20 20 20 20 2d ODE_QUERY -
53460 31 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 1.#define PAGER_
53470 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d LOCKINGMODE_NORM
53480 41 4c 20 20 20 20 20 20 30 0a 23 64 65 66 69 6e AL 0.#defin
53490 65 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d e PAGER_LOCKINGM
534a0 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 20 20 20 ODE_EXCLUSIVE
534b0 31 0a 0a 2f 2a 0a 2a 2a 20 56 61 6c 69 64 20 76 1../*.** Valid v
534c0 61 6c 75 65 73 20 66 6f 72 20 74 68 65 20 73 65 alues for the se
534d0 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f cond argument to
534e0 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 sqlite3PagerJou
534f0 72 6e 61 6c 4d 6f 64 65 28 29 2e 0a 2a 2f 0a 23 rnalMode()..*/.#
53500 64 65 66 69 6e 65 20 50 41 47 45 52 5f 4a 4f 55 define PAGER_JOU
53510 52 4e 41 4c 4d 4f 44 45 5f 51 55 45 52 59 20 20 RNALMODE_QUERY
53520 20 20 20 20 2d 31 0a 23 64 65 66 69 6e 65 20 50 -1.#define P
53530 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 AGER_JOURNALMODE
53540 5f 44 45 4c 45 54 45 20 20 20 20 20 20 30 20 20 _DELETE 0
53550 20 2f 2a 20 43 6f 6d 6d 69 74 20 62 79 20 64 65 /* Commit by de
53560 6c 65 74 69 6e 67 20 6a 6f 75 72 6e 61 6c 20 66 leting journal f
53570 69 6c 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 ile */.#define P
53580 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 AGER_JOURNALMODE
53590 5f 50 45 52 53 49 53 54 20 20 20 20 20 31 20 20 _PERSIST 1
535a0 20 2f 2a 20 43 6f 6d 6d 69 74 20 62 79 20 7a 65 /* Commit by ze
535b0 72 6f 69 6e 67 20 6a 6f 75 72 6e 61 6c 20 68 65 roing journal he
535c0 61 64 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 ader */.#define
535d0 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 PAGER_JOURNALMOD
535e0 45 5f 4f 46 46 20 20 20 20 20 20 20 20 20 32 20 E_OFF 2
535f0 20 20 2f 2a 20 4a 6f 75 72 6e 61 6c 20 6f 6d 69 /* Journal omi
53600 74 74 65 64 2e 20 20 2a 2f 0a 23 64 65 66 69 6e tted. */.#defin
53610 65 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d e PAGER_JOURNALM
53620 4f 44 45 5f 54 52 55 4e 43 41 54 45 20 20 20 20 ODE_TRUNCATE
53630 33 20 20 20 2f 2a 20 43 6f 6d 6d 69 74 20 62 79 3 /* Commit by
53640 20 74 72 75 6e 63 61 74 69 6e 67 20 6a 6f 75 72 truncating jour
53650 6e 61 6c 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 nal */.#define P
53660 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 AGER_JOURNALMODE
53670 5f 4d 45 4d 4f 52 59 20 20 20 20 20 20 34 20 20 _MEMORY 4
53680 20 2f 2a 20 49 6e 2d 6d 65 6d 6f 72 79 20 6a 6f /* In-memory jo
53690 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 0a 2f urnal file */../
536a0 2a 0a 2a 2a 20 54 68 65 20 72 65 6d 61 69 6e 64 *.** The remaind
536b0 65 72 20 6f 66 20 74 68 69 73 20 66 69 6c 65 20 er of this file
536c0 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 64 65 63 contains the dec
536d0 6c 61 72 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 larations of the
536e0 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 74 68 functions.** th
536f0 61 74 20 6d 61 6b 65 20 75 70 20 74 68 65 20 50 at make up the P
53700 61 67 65 72 20 73 75 62 2d 73 79 73 74 65 6d 20 ager sub-system
53710 41 50 49 2e 20 53 65 65 20 73 6f 75 72 63 65 20 API. See source
53720 63 6f 64 65 20 63 6f 6d 6d 65 6e 74 73 20 66 6f code comments fo
53730 72 20 0a 2a 2a 20 61 20 64 65 74 61 69 6c 65 64 r .** a detailed
53740 20 64 65 73 63 72 69 70 74 69 6f 6e 20 6f 66 20 description of
53750 65 61 63 68 20 72 6f 75 74 69 6e 65 2e 0a 2a 2f each routine..*/
53760 0a 0a 2f 2a 20 4f 70 65 6e 20 61 6e 64 20 63 6c ../* Open and cl
53770 6f 73 65 20 61 20 50 61 67 65 72 20 63 6f 6e 6e ose a Pager conn
53780 65 63 74 69 6f 6e 2e 20 2a 2f 20 0a 53 51 4c 49 ection. */ .SQLI
53790 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
537a0 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e 28 qlite3PagerOpen(
537b0 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c . sqlite3_vfs*,
537c0 0a 20 20 50 61 67 65 72 20 2a 2a 70 70 50 61 67 . Pager **ppPag
537d0 65 72 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 er,. const char
537e0 2a 2c 0a 20 20 69 6e 74 2c 0a 20 20 69 6e 74 2c *,. int,. int,
537f0 0a 20 20 69 6e 74 2c 0a 20 20 76 6f 69 64 28 2a . int,. void(*
53800 29 28 44 62 50 61 67 65 2a 29 0a 29 3b 0a 53 51 )(DbPage*).);.SQ
53810 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
53820 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6c 6f sqlite3PagerClo
53830 73 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 se(Pager *pPager
53840 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
53850 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 E int sqlite3Pag
53860 65 72 52 65 61 64 46 69 6c 65 68 65 61 64 65 72 erReadFileheader
53870 28 50 61 67 65 72 2a 2c 20 69 6e 74 2c 20 75 6e (Pager*, int, un
53880 73 69 67 6e 65 64 20 63 68 61 72 2a 29 3b 0a 0a signed char*);..
53890 2f 2a 20 46 75 6e 63 74 69 6f 6e 73 20 75 73 65 /* Functions use
538a0 64 20 74 6f 20 63 6f 6e 66 69 67 75 72 65 20 61 d to configure a
538b0 20 50 61 67 65 72 20 6f 62 6a 65 63 74 2e 20 2a Pager object. *
538c0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
538d0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 void sqlite3Pag
538e0 65 72 53 65 74 42 75 73 79 68 61 6e 64 6c 65 72 erSetBusyhandler
538f0 28 50 61 67 65 72 2a 2c 20 69 6e 74 28 2a 29 28 (Pager*, int(*)(
53900 76 6f 69 64 20 2a 29 2c 20 76 6f 69 64 20 2a 29 void *), void *)
53910 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
53920 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 int sqlite3Page
53930 72 53 65 74 50 61 67 65 73 69 7a 65 28 50 61 67 rSetPagesize(Pag
53940 65 72 2a 2c 20 75 31 36 2a 2c 20 69 6e 74 29 3b er*, u16*, int);
53950 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
53960 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 int sqlite3Pager
53970 4d 61 78 50 61 67 65 43 6f 75 6e 74 28 50 61 67 MaxPageCount(Pag
53980 65 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 er*, int);.SQLIT
53990 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
539a0 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 43 61 qlite3PagerSetCa
539b0 63 68 65 73 69 7a 65 28 50 61 67 65 72 2a 2c 20 chesize(Pager*,
539c0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
539d0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
539e0 33 50 61 67 65 72 53 65 74 53 61 66 65 74 79 4c 3PagerSetSafetyL
539f0 65 76 65 6c 28 50 61 67 65 72 2a 2c 69 6e 74 2c evel(Pager*,int,
53a00 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
53a10 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
53a20 50 61 67 65 72 4c 6f 63 6b 69 6e 67 4d 6f 64 65 PagerLockingMode
53a30 28 50 61 67 65 72 20 2a 2c 20 69 6e 74 29 3b 0a (Pager *, int);.
53a40 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
53a50 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a nt sqlite3PagerJ
53a60 6f 75 72 6e 61 6c 4d 6f 64 65 28 50 61 67 65 72 ournalMode(Pager
53a70 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 *, int);.SQLITE
53a80 5f 50 52 49 56 41 54 45 20 69 36 34 20 73 71 6c _PRIVATE i64 sql
53a90 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c ite3PagerJournal
53aa0 53 69 7a 65 4c 69 6d 69 74 28 50 61 67 65 72 20 SizeLimit(Pager
53ab0 2a 2c 20 69 36 34 29 3b 0a 53 51 4c 49 54 45 5f *, i64);.SQLITE_
53ac0 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f PRIVATE sqlite3_
53ad0 62 61 63 6b 75 70 20 2a 2a 73 71 6c 69 74 65 33 backup **sqlite3
53ae0 50 61 67 65 72 42 61 63 6b 75 70 50 74 72 28 50 PagerBackupPtr(P
53af0 61 67 65 72 2a 29 3b 0a 0a 2f 2a 20 46 75 6e 63 ager*);../* Func
53b00 74 69 6f 6e 73 20 75 73 65 64 20 74 6f 20 6f 62 tions used to ob
53b10 74 61 69 6e 20 61 6e 64 20 72 65 6c 65 61 73 65 tain and release
53b20 20 70 61 67 65 20 72 65 66 65 72 65 6e 63 65 73 page references
53b30 2e 20 2a 2f 20 0a 53 51 4c 49 54 45 5f 50 52 49 . */ .SQLITE_PRI
53b40 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
53b50 50 61 67 65 72 41 63 71 75 69 72 65 28 50 61 67 PagerAcquire(Pag
53b60 65 72 20 2a 70 50 61 67 65 72 2c 20 50 67 6e 6f er *pPager, Pgno
53b70 20 70 67 6e 6f 2c 20 44 62 50 61 67 65 20 2a 2a pgno, DbPage **
53b80 70 70 50 61 67 65 2c 20 69 6e 74 20 63 6c 72 46 ppPage, int clrF
53b90 6c 61 67 29 3b 0a 23 64 65 66 69 6e 65 20 73 71 lag);.#define sq
53ba0 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 41 2c lite3PagerGet(A,
53bb0 42 2c 43 29 20 73 71 6c 69 74 65 33 50 61 67 65 B,C) sqlite3Page
53bc0 72 41 63 71 75 69 72 65 28 41 2c 42 2c 43 2c 30 rAcquire(A,B,C,0
53bd0 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ).SQLITE_PRIVATE
53be0 20 44 62 50 61 67 65 20 2a 73 71 6c 69 74 65 33 DbPage *sqlite3
53bf0 50 61 67 65 72 4c 6f 6f 6b 75 70 28 50 61 67 65 PagerLookup(Page
53c00 72 20 2a 70 50 61 67 65 72 2c 20 50 67 6e 6f 20 r *pPager, Pgno
53c10 70 67 6e 6f 29 3b 0a 53 51 4c 49 54 45 5f 50 52 pgno);.SQLITE_PR
53c20 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
53c30 65 33 50 61 67 65 72 52 65 66 28 44 62 50 61 67 e3PagerRef(DbPag
53c40 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 e*);.SQLITE_PRIV
53c50 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
53c60 50 61 67 65 72 55 6e 72 65 66 28 44 62 50 61 67 PagerUnref(DbPag
53c70 65 2a 29 3b 0a 0a 2f 2a 20 4f 70 65 72 61 74 69 e*);../* Operati
53c80 6f 6e 73 20 6f 6e 20 70 61 67 65 20 72 65 66 65 ons on page refe
53c90 72 65 6e 63 65 73 2e 20 2a 2f 0a 53 51 4c 49 54 rences. */.SQLIT
53ca0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
53cb0 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 lite3PagerWrite(
53cc0 44 62 50 61 67 65 2a 29 3b 0a 53 51 4c 49 54 45 DbPage*);.SQLITE
53cd0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
53ce0 6c 69 74 65 33 50 61 67 65 72 44 6f 6e 74 57 72 lite3PagerDontWr
53cf0 69 74 65 28 44 62 50 61 67 65 2a 29 3b 0a 53 51 ite(DbPage*);.SQ
53d00 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
53d10 20 73 71 6c 69 74 65 33 50 61 67 65 72 4d 6f 76 sqlite3PagerMov
53d20 65 70 61 67 65 28 50 61 67 65 72 2a 2c 44 62 50 epage(Pager*,DbP
53d30 61 67 65 2a 2c 50 67 6e 6f 2c 69 6e 74 29 3b 0a age*,Pgno,int);.
53d40 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
53d50 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 nt sqlite3PagerP
53d60 61 67 65 52 65 66 63 6f 75 6e 74 28 44 62 50 61 ageRefcount(DbPa
53d70 67 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ge*);.SQLITE_PRI
53d80 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 VATE void *sqlit
53d90 65 33 50 61 67 65 72 47 65 74 44 61 74 61 28 44 e3PagerGetData(D
53da0 62 50 61 67 65 20 2a 29 3b 20 0a 53 51 4c 49 54 bPage *); .SQLIT
53db0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a E_PRIVATE void *
53dc0 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 45 sqlite3PagerGetE
53dd0 78 74 72 61 28 44 62 50 61 67 65 20 2a 29 3b 20 xtra(DbPage *);
53de0 0a 0a 2f 2a 20 46 75 6e 63 74 69 6f 6e 73 20 75 ../* Functions u
53df0 73 65 64 20 74 6f 20 6d 61 6e 61 67 65 20 70 61 sed to manage pa
53e00 67 65 72 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 ger transactions
53e10 20 61 6e 64 20 73 61 76 65 70 6f 69 6e 74 73 2e and savepoints.
53e20 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 */.SQLITE_PRIVA
53e30 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 TE int sqlite3Pa
53e40 67 65 72 50 61 67 65 63 6f 75 6e 74 28 50 61 67 gerPagecount(Pag
53e50 65 72 2a 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c 49 er*, int*);.SQLI
53e60 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
53e70 71 6c 69 74 65 33 50 61 67 65 72 42 65 67 69 6e qlite3PagerBegin
53e80 28 50 61 67 65 72 2a 2c 20 69 6e 74 20 65 78 46 (Pager*, int exF
53e90 6c 61 67 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 lag, int);.SQLIT
53ea0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
53eb0 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 lite3PagerCommit
53ec0 50 68 61 73 65 4f 6e 65 28 50 61 67 65 72 2a 2c PhaseOne(Pager*,
53ed0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 61 73 const char *zMas
53ee0 74 65 72 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 ter, int);.SQLIT
53ef0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
53f00 6c 69 74 65 33 50 61 67 65 72 53 79 6e 63 28 50 lite3PagerSync(P
53f10 61 67 65 72 20 2a 70 50 61 67 65 72 29 3b 0a 53 ager *pPager);.S
53f20 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
53f30 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f t sqlite3PagerCo
53f40 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 50 61 67 mmitPhaseTwo(Pag
53f50 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 er*);.SQLITE_PRI
53f60 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
53f70 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 50 61 PagerRollback(Pa
53f80 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ger*);.SQLITE_PR
53f90 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
53fa0 33 50 61 67 65 72 4f 70 65 6e 53 61 76 65 70 6f 3PagerOpenSavepo
53fb0 69 6e 74 28 50 61 67 65 72 20 2a 70 50 61 67 65 int(Pager *pPage
53fc0 72 2c 20 69 6e 74 20 6e 29 3b 0a 53 51 4c 49 54 r, int n);.SQLIT
53fd0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
53fe0 6c 69 74 65 33 50 61 67 65 72 53 61 76 65 70 6f lite3PagerSavepo
53ff0 69 6e 74 28 50 61 67 65 72 20 2a 70 50 61 67 65 int(Pager *pPage
54000 72 2c 20 69 6e 74 20 6f 70 2c 20 69 6e 74 20 69 r, int op, int i
54010 53 61 76 65 70 6f 69 6e 74 29 3b 0a 53 51 4c 49 Savepoint);.SQLI
54020 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
54030 71 6c 69 74 65 33 50 61 67 65 72 53 68 61 72 65 qlite3PagerShare
54040 64 4c 6f 63 6b 28 50 61 67 65 72 20 2a 70 50 61 dLock(Pager *pPa
54050 67 65 72 29 3b 0a 0a 2f 2a 20 46 75 6e 63 74 69 ger);../* Functi
54060 6f 6e 73 20 75 73 65 64 20 74 6f 20 71 75 65 72 ons used to quer
54070 79 20 70 61 67 65 72 20 73 74 61 74 65 20 61 6e y pager state an
54080 64 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 2e d configuration.
54090 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 */.SQLITE_PRIVA
540a0 54 45 20 75 38 20 73 71 6c 69 74 65 33 50 61 67 TE u8 sqlite3Pag
540b0 65 72 49 73 72 65 61 64 6f 6e 6c 79 28 50 61 67 erIsreadonly(Pag
540c0 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 er*);.SQLITE_PRI
540d0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
540e0 50 61 67 65 72 52 65 66 63 6f 75 6e 74 28 50 61 PagerRefcount(Pa
540f0 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ger*);.SQLITE_PR
54100 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 IVATE const char
54110 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 46 69 *sqlite3PagerFi
54120 6c 65 6e 61 6d 65 28 50 61 67 65 72 2a 29 3b 0a lename(Pager*);.
54130 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 SQLITE_PRIVATE c
54140 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 76 66 73 onst sqlite3_vfs
54150 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 56 66 *sqlite3PagerVf
54160 73 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 s(Pager*);.SQLIT
54170 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 E_PRIVATE sqlite
54180 33 5f 66 69 6c 65 20 2a 73 71 6c 69 74 65 33 50 3_file *sqlite3P
54190 61 67 65 72 46 69 6c 65 28 50 61 67 65 72 2a 29 agerFile(Pager*)
541a0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
541b0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c const char *sql
541c0 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c ite3PagerJournal
541d0 6e 61 6d 65 28 50 61 67 65 72 2a 29 3b 0a 53 51 name(Pager*);.SQ
541e0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
541f0 20 73 71 6c 69 74 65 33 50 61 67 65 72 4e 6f 73 sqlite3PagerNos
54200 79 6e 63 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c ync(Pager*);.SQL
54210 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
54220 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 54 65 *sqlite3PagerTe
54230 6d 70 53 70 61 63 65 28 50 61 67 65 72 2a 29 3b mpSpace(Pager*);
54240 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
54250 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 int sqlite3Pager
54260 49 73 4d 65 6d 64 62 28 50 61 67 65 72 2a 29 3b IsMemdb(Pager*);
54270 0a 0a 2f 2a 20 46 75 6e 63 74 69 6f 6e 73 20 75 ../* Functions u
54280 73 65 64 20 74 6f 20 74 72 75 6e 63 61 74 65 20 sed to truncate
54290 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
542a0 65 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e. */.SQLITE_PRI
542b0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
542c0 33 50 61 67 65 72 54 72 75 6e 63 61 74 65 49 6d 3PagerTruncateIm
542d0 61 67 65 28 50 61 67 65 72 2a 2c 50 67 6e 6f 29 age(Pager*,Pgno)
542e0 3b 0a 0a 2f 2a 20 46 75 6e 63 74 69 6f 6e 73 20 ;../* Functions
542f0 74 6f 20 73 75 70 70 6f 72 74 20 74 65 73 74 69 to support testi
54300 6e 67 20 61 6e 64 20 64 65 62 75 67 67 69 6e 67 ng and debugging
54310 2e 20 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 . */.#if !define
54320 64 28 4e 44 45 42 55 47 29 20 7c 7c 20 64 65 66 d(NDEBUG) || def
54330 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 ined(SQLITE_TEST
54340 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ).SQLITE_PRIVATE
54350 20 20 20 50 67 6e 6f 20 73 71 6c 69 74 65 33 50 Pgno sqlite3P
54360 61 67 65 72 50 61 67 65 6e 75 6d 62 65 72 28 44 agerPagenumber(D
54370 62 50 61 67 65 2a 29 3b 0a 53 51 4c 49 54 45 5f bPage*);.SQLITE_
54380 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 PRIVATE int sq
54390 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 lite3PagerIswrit
543a0 65 61 62 6c 65 28 44 62 50 61 67 65 2a 29 3b 0a eable(DbPage*);.
543b0 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 #endif.#ifdef SQ
543c0 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 LITE_TEST.SQLITE
543d0 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 2a _PRIVATE int *
543e0 73 71 6c 69 74 65 33 50 61 67 65 72 53 74 61 74 sqlite3PagerStat
543f0 73 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 s(Pager*);.SQLIT
54400 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 E_PRIVATE void
54410 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 sqlite3PagerRef
54420 64 75 6d 70 28 50 61 67 65 72 2a 29 3b 0a 20 20 dump(Pager*);.
54430 76 6f 69 64 20 64 69 73 61 62 6c 65 5f 73 69 6d void disable_sim
54440 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 ulated_io_errors
54450 28 76 6f 69 64 29 3b 0a 20 20 76 6f 69 64 20 65 (void);. void e
54460 6e 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f nable_simulated_
54470 69 6f 5f 65 72 72 6f 72 73 28 76 6f 69 64 29 3b io_errors(void);
54480 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 .#else.# define
54490 64 69 73 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 disable_simulate
544a0 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29 0a 23 20 d_io_errors().#
544b0 64 65 66 69 6e 65 20 65 6e 61 62 6c 65 5f 73 69 define enable_si
544c0 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 mulated_io_error
544d0 73 28 29 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 s().#endif..#end
544e0 69 66 20 2f 2a 20 5f 50 41 47 45 52 5f 48 5f 20 if /* _PAGER_H_
544f0 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../***********
54500 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 61 67 65 72 *** End of pager
54510 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .h *************
54520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54540 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
54550 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 *** Continuing w
54560 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 here we left off
54570 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 in sqliteInt.h
54580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54590 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
545a0 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 70 63 61 63 *** Include pcac
545b0 68 65 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 he.h in the midd
545c0 6c 65 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e le of sqliteInt.
545d0 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a h **************
545e0 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
545f0 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 70 *** Begin file p
54600 63 61 63 68 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a cache.h ********
54610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54630 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 41 **/./*.** 2008 A
54640 75 67 75 73 74 20 30 35 0a 2a 2a 0a 2a 2a 20 54 ugust 05.**.** T
54650 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 he author discla
54660 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f ims copyright to
54670 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 this source cod
54680 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a e. In place of.
54690 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 ** a legal notic
546a0 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 e, here is a ble
546b0 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 ssing:.**.**
546c0 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 May you do good
546d0 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a and not evil..**
546e0 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 May you find
546f0 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 forgiveness for
54700 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f yourself and fo
54710 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a rgive others..**
54720 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 May you shar
54730 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 e freely, never
54740 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e taking more than
54750 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a you give..**.**
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 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
547a0 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 *******.** This
547b0 68 65 61 64 65 72 20 66 69 6c 65 20 64 65 66 69 header file defi
547c0 6e 65 73 20 74 68 65 20 69 6e 74 65 72 66 61 63 nes the interfac
547d0 65 20 74 68 61 74 20 74 68 65 20 73 71 6c 69 74 e that the sqlit
547e0 65 20 70 61 67 65 20 63 61 63 68 65 0a 2a 2a 20 e page cache.**
547f0 73 75 62 73 79 73 74 65 6d 2e 20 0a 2a 2a 0a 2a subsystem. .**.*
54800 2a 20 40 28 23 29 20 24 49 64 3a 20 70 63 61 63 * @(#) $Id: pcac
54810 68 65 2e 68 2c 76 20 31 2e 32 30 20 32 30 30 39 he.h,v 1.20 2009
54820 2f 30 37 2f 32 35 20 31 31 3a 34 36 3a 34 39 20 /07/25 11:46:49
54830 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 danielk1977 Exp
54840 24 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 5f 50 $.*/..#ifndef _P
54850 43 41 43 48 45 5f 48 5f 0a 0a 74 79 70 65 64 65 CACHE_H_..typede
54860 66 20 73 74 72 75 63 74 20 50 67 48 64 72 20 50 f struct PgHdr P
54870 67 48 64 72 3b 0a 74 79 70 65 64 65 66 20 73 74 gHdr;.typedef st
54880 72 75 63 74 20 50 43 61 63 68 65 20 50 43 61 63 ruct PCache PCac
54890 68 65 3b 0a 0a 2f 2a 0a 2a 2a 20 45 76 65 72 79 he;../*.** Every
548a0 20 70 61 67 65 20 69 6e 20 74 68 65 20 63 61 63 page in the cac
548b0 68 65 20 69 73 20 63 6f 6e 74 72 6f 6c 6c 65 64 he is controlled
548c0 20 62 79 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 by an instance
548d0 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 of the following
548e0 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 0a 2a .** structure..*
548f0 2f 0a 73 74 72 75 63 74 20 50 67 48 64 72 20 7b /.struct PgHdr {
54900 0a 20 20 76 6f 69 64 20 2a 70 44 61 74 61 3b 20 . void *pData;
54910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
54920 20 20 2f 2a 20 43 6f 6e 74 65 6e 74 20 6f 66 20 /* Content of
54930 74 68 69 73 20 70 61 67 65 20 2a 2f 0a 20 20 76 this page */. v
54940 6f 69 64 20 2a 70 45 78 74 72 61 3b 20 20 20 20 oid *pExtra;
54950 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
54960 20 45 78 74 72 61 20 63 6f 6e 74 65 6e 74 20 2a Extra content *
54970 2f 0a 20 20 50 67 48 64 72 20 2a 70 44 69 72 74 /. PgHdr *pDirt
54980 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 y;
54990 20 20 20 2f 2a 20 54 72 61 6e 73 69 65 6e 74 20 /* Transient
549a0 6c 69 73 74 20 6f 66 20 64 69 72 74 79 20 70 61 list of dirty pa
549b0 67 65 73 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 ges */. Pgno pg
549c0 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 no;
549d0 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 /* Page
549e0 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 69 73 20 number for this
549f0 70 61 67 65 20 2a 2f 0a 20 20 50 61 67 65 72 20 page */. Pager
54a00 2a 70 50 61 67 65 72 3b 20 20 20 20 20 20 20 20 *pPager;
54a10 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
54a20 70 61 67 65 72 20 74 68 69 73 20 70 61 67 65 20 pager this page
54a30 69 73 20 70 61 72 74 20 6f 66 20 2a 2f 0a 23 69 is part of */.#i
54a40 66 64 65 66 20 53 51 4c 49 54 45 5f 43 48 45 43 fdef SQLITE_CHEC
54a50 4b 5f 50 41 47 45 53 0a 20 20 75 33 32 20 70 61 K_PAGES. u32 pa
54a60 67 65 48 61 73 68 3b 20 20 20 20 20 20 20 20 20 geHash;
54a70 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 73 68 /* Hash
54a80 20 6f 66 20 70 61 67 65 20 63 6f 6e 74 65 6e 74 of page content
54a90 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 75 31 36 */.#endif. u16
54aa0 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 flags;
54ab0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 /* P
54ac0 47 48 44 52 20 66 6c 61 67 73 20 64 65 66 69 6e GHDR flags defin
54ad0 65 64 20 62 65 6c 6f 77 20 2a 2f 0a 0a 20 20 2f ed below */.. /
54ae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54af0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54b00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54b10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54b20 2a 2a 2a 2a 2a 2a 0a 20 20 2a 2a 20 45 6c 65 6d ******. ** Elem
54b30 65 6e 74 73 20 61 62 6f 76 65 20 61 72 65 20 70 ents above are p
54b40 75 62 6c 69 63 2e 20 20 41 6c 6c 20 74 68 61 74 ublic. All that
54b50 20 66 6f 6c 6c 6f 77 73 20 69 73 20 70 72 69 76 follows is priv
54b60 61 74 65 20 74 6f 20 70 63 61 63 68 65 2e 63 0a ate to pcache.c.
54b70 20 20 2a 2a 20 61 6e 64 20 73 68 6f 75 6c 64 20 ** and should
54b80 6e 6f 74 20 62 65 20 61 63 63 65 73 73 65 64 20 not be accessed
54b90 62 79 20 6f 74 68 65 72 20 6d 6f 64 75 6c 65 73 by other modules
54ba0 2e 0a 20 20 2a 2f 0a 20 20 69 31 36 20 6e 52 65 .. */. i16 nRe
54bb0 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 f;
54bc0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
54bd0 72 20 6f 66 20 75 73 65 72 73 20 6f 66 20 74 68 r of users of th
54be0 69 73 20 70 61 67 65 20 2a 2f 0a 20 20 50 43 61 is page */. PCa
54bf0 63 68 65 20 2a 70 43 61 63 68 65 3b 20 20 20 20 che *pCache;
54c00 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 /* C
54c10 61 63 68 65 20 74 68 61 74 20 6f 77 6e 73 20 74 ache that owns t
54c20 68 69 73 20 70 61 67 65 20 2a 2f 0a 0a 20 20 50 his page */.. P
54c30 67 48 64 72 20 2a 70 44 69 72 74 79 4e 65 78 74 gHdr *pDirtyNext
54c40 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
54c50 20 4e 65 78 74 20 65 6c 65 6d 65 6e 74 20 69 6e Next element in
54c60 20 6c 69 73 74 20 6f 66 20 64 69 72 74 79 20 70 list of dirty p
54c70 61 67 65 73 20 2a 2f 0a 20 20 50 67 48 64 72 20 ages */. PgHdr
54c80 2a 70 44 69 72 74 79 50 72 65 76 3b 20 20 20 20 *pDirtyPrev;
54c90 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 76 /* Prev
54ca0 69 6f 75 73 20 65 6c 65 6d 65 6e 74 20 69 6e 20 ious element in
54cb0 6c 69 73 74 20 6f 66 20 64 69 72 74 79 20 70 61 list of dirty pa
54cc0 67 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 42 69 ges */.};../* Bi
54cd0 74 20 76 61 6c 75 65 73 20 66 6f 72 20 50 67 48 t values for PgH
54ce0 64 72 2e 66 6c 61 67 73 20 2a 2f 0a 23 64 65 66 dr.flags */.#def
54cf0 69 6e 65 20 50 47 48 44 52 5f 44 49 52 54 59 20 ine PGHDR_DIRTY
54d00 20 20 20 20 20 20 20 20 20 20 20 20 30 78 30 30 0x00
54d10 32 20 20 2f 2a 20 50 61 67 65 20 68 61 73 20 63 2 /* Page has c
54d20 68 61 6e 67 65 64 20 2a 2f 0a 23 64 65 66 69 6e hanged */.#defin
54d30 65 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e e PGHDR_NEED_SYN
54d40 43 20 20 20 20 20 20 20 20 20 30 78 30 30 34 20 C 0x004
54d50 20 2f 2a 20 46 73 79 6e 63 20 74 68 65 20 72 6f /* Fsync the ro
54d60 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 62 llback journal b
54d70 65 66 6f 72 65 0a 20 20 20 20 20 20 20 20 20 20 efore.
54d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
54d90 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 **
54da0 77 72 69 74 69 6e 67 20 74 68 69 73 20 70 61 67 writing this pag
54db0 65 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 e to the databas
54dc0 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 47 48 e */.#define PGH
54dd0 44 52 5f 4e 45 45 44 5f 52 45 41 44 20 20 20 20 DR_NEED_READ
54de0 20 20 20 20 20 30 78 30 30 38 20 20 2f 2a 20 43 0x008 /* C
54df0 6f 6e 74 65 6e 74 20 69 73 20 75 6e 72 65 61 64 ontent is unread
54e00 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 47 48 44 */.#define PGHD
54e10 52 5f 52 45 55 53 45 5f 55 4e 4c 49 4b 45 4c 59 R_REUSE_UNLIKELY
54e20 20 20 20 20 30 78 30 31 30 20 20 2f 2a 20 41 20 0x010 /* A
54e30 68 69 6e 74 20 74 68 61 74 20 72 65 75 73 65 20 hint that reuse
54e40 69 73 20 75 6e 6c 69 6b 65 6c 79 20 2a 2f 0a 23 is unlikely */.#
54e50 64 65 66 69 6e 65 20 50 47 48 44 52 5f 44 4f 4e define PGHDR_DON
54e60 54 5f 57 52 49 54 45 20 20 20 20 20 20 20 20 30 T_WRITE 0
54e70 78 30 32 30 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 x020 /* Do not
54e80 77 72 69 74 65 20 63 6f 6e 74 65 6e 74 20 74 6f write content to
54e90 20 64 69 73 6b 20 2a 2f 0a 0a 2f 2a 20 49 6e 69 disk */../* Ini
54ea0 74 69 61 6c 69 7a 65 20 61 6e 64 20 73 68 75 74 tialize and shut
54eb0 64 6f 77 6e 20 74 68 65 20 70 61 67 65 20 63 61 down the page ca
54ec0 63 68 65 20 73 75 62 73 79 73 74 65 6d 20 2a 2f che subsystem */
54ed0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
54ee0 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 int sqlite3Pcach
54ef0 65 49 6e 69 74 69 61 6c 69 7a 65 28 76 6f 69 64 eInitialize(void
54f00 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
54f10 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 E void sqlite3Pc
54f20 61 63 68 65 53 68 75 74 64 6f 77 6e 28 76 6f 69 acheShutdown(voi
54f30 64 29 3b 0a 0a 2f 2a 20 50 61 67 65 20 63 61 63 d);../* Page cac
54f40 68 65 20 62 75 66 66 65 72 20 6d 61 6e 61 67 65 he buffer manage
54f50 6d 65 6e 74 3a 0a 2a 2a 20 54 68 65 73 65 20 72 ment:.** These r
54f60 6f 75 74 69 6e 65 73 20 69 6d 70 6c 65 6d 65 6e outines implemen
54f70 74 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f t SQLITE_CONFIG_
54f80 50 41 47 45 43 41 43 48 45 2e 0a 2a 2f 0a 53 51 PAGECACHE..*/.SQ
54f90 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
54fa0 64 20 73 71 6c 69 74 65 33 50 43 61 63 68 65 42 d sqlite3PCacheB
54fb0 75 66 66 65 72 53 65 74 75 70 28 76 6f 69 64 20 ufferSetup(void
54fc0 2a 2c 20 69 6e 74 20 73 7a 2c 20 69 6e 74 20 6e *, int sz, int n
54fd0 29 3b 0a 0a 2f 2a 20 43 72 65 61 74 65 20 61 20 );../* Create a
54fe0 6e 65 77 20 70 61 67 65 72 20 63 61 63 68 65 2e new pager cache.
54ff0 0a 2a 2a 20 55 6e 64 65 72 20 6d 65 6d 6f 72 79 .** Under memory
55000 20 73 74 72 65 73 73 2c 20 69 6e 76 6f 6b 65 20 stress, invoke
55010 78 53 74 72 65 73 73 20 74 6f 20 74 72 79 20 74 xStress to try t
55020 6f 20 6d 61 6b 65 20 70 61 67 65 73 20 63 6c 65 o make pages cle
55030 61 6e 2e 0a 2a 2a 20 4f 6e 6c 79 20 63 6c 65 61 an..** Only clea
55040 6e 20 61 6e 64 20 75 6e 70 69 6e 6e 65 64 20 70 n and unpinned p
55050 61 67 65 73 20 63 61 6e 20 62 65 20 72 65 63 6c ages can be recl
55060 61 69 6d 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 aimed..*/.SQLITE
55070 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
55080 6c 69 74 65 33 50 63 61 63 68 65 4f 70 65 6e 28 lite3PcacheOpen(
55090 0a 20 20 69 6e 74 20 73 7a 50 61 67 65 2c 20 20 . int szPage,
550a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
550b0 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 65 76 65 /* Size of eve
550c0 72 79 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 ry page */. int
550d0 20 73 7a 45 78 74 72 61 2c 20 20 20 20 20 20 20 szExtra,
550e0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 /* E
550f0 78 74 72 61 20 73 70 61 63 65 20 61 73 73 6f 63 xtra space assoc
55100 69 61 74 65 64 20 77 69 74 68 20 65 61 63 68 20 iated with each
55110 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 62 50 page */. int bP
55120 75 72 67 65 61 62 6c 65 2c 20 20 20 20 20 20 20 urgeable,
55130 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
55140 20 69 66 20 70 61 67 65 73 20 61 72 65 20 6f 6e if pages are on
55150 20 62 61 63 6b 69 6e 67 20 73 74 6f 72 65 20 2a backing store *
55160 2f 0a 20 20 69 6e 74 20 28 2a 78 53 74 72 65 73 /. int (*xStres
55170 73 29 28 76 6f 69 64 2a 2c 20 50 67 48 64 72 2a s)(void*, PgHdr*
55180 29 2c 20 2f 2a 20 43 61 6c 6c 20 74 6f 20 74 72 ), /* Call to tr
55190 79 20 74 6f 20 6d 61 6b 65 20 70 61 67 65 73 20 y to make pages
551a0 63 6c 65 61 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 clean */. void
551b0 2a 70 53 74 72 65 73 73 2c 20 20 20 20 20 20 20 *pStress,
551c0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67 /* Arg
551d0 75 6d 65 6e 74 20 74 6f 20 78 53 74 72 65 73 73 ument to xStress
551e0 20 2a 2f 0a 20 20 50 43 61 63 68 65 20 2a 70 54 */. PCache *pT
551f0 6f 49 6e 69 74 20 20 20 20 20 20 20 20 20 20 20 oInit
55200 20 20 20 20 20 2f 2a 20 50 72 65 61 6c 6c 6f 63 /* Prealloc
55210 61 74 65 64 20 73 70 61 63 65 20 66 6f 72 20 74 ated space for t
55220 68 65 20 50 43 61 63 68 65 20 2a 2f 0a 29 3b 0a he PCache */.);.
55230 0a 2f 2a 20 4d 6f 64 69 66 79 20 74 68 65 20 70 ./* Modify the p
55240 61 67 65 2d 73 69 7a 65 20 61 66 74 65 72 20 74 age-size after t
55250 68 65 20 63 61 63 68 65 20 68 61 73 20 62 65 65 he cache has bee
55260 6e 20 63 72 65 61 74 65 64 2e 20 2a 2f 0a 53 51 n created. */.SQ
55270 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
55280 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 53 d sqlite3PcacheS
55290 65 74 50 61 67 65 53 69 7a 65 28 50 43 61 63 68 etPageSize(PCach
552a0 65 20 2a 2c 20 69 6e 74 29 3b 0a 0a 2f 2a 20 52 e *, int);../* R
552b0 65 74 75 72 6e 20 74 68 65 20 73 69 7a 65 20 69 eturn the size i
552c0 6e 20 62 79 74 65 73 20 6f 66 20 61 20 50 43 61 n bytes of a PCa
552d0 63 68 65 20 6f 62 6a 65 63 74 2e 20 20 55 73 65 che object. Use
552e0 64 20 74 6f 20 70 72 65 61 6c 6c 6f 63 61 74 65 d to preallocate
552f0 0a 2a 2a 20 73 74 6f 72 61 67 65 20 73 70 61 63 .** storage spac
55300 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e..*/.SQLITE_PRI
55310 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
55320 50 63 61 63 68 65 53 69 7a 65 28 76 6f 69 64 29 PcacheSize(void)
55330 3b 0a 0a 2f 2a 20 4f 6e 65 20 72 65 6c 65 61 73 ;../* One releas
55340 65 20 70 65 72 20 73 75 63 63 65 73 73 66 75 6c e per successful
55350 20 66 65 74 63 68 2e 20 20 50 61 67 65 20 69 73 fetch. Page is
55360 20 70 69 6e 6e 65 64 20 75 6e 74 69 6c 20 72 65 pinned until re
55370 6c 65 61 73 65 64 2e 0a 2a 2a 20 52 65 66 65 72 leased..** Refer
55380 65 6e 63 65 20 63 6f 75 6e 74 65 64 2e 20 0a 2a ence counted. .*
55390 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
553a0 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 int sqlite3Pcac
553b0 68 65 46 65 74 63 68 28 50 43 61 63 68 65 2a 2c heFetch(PCache*,
553c0 20 50 67 6e 6f 2c 20 69 6e 74 20 63 72 65 61 74 Pgno, int creat
553d0 65 46 6c 61 67 2c 20 50 67 48 64 72 2a 2a 29 3b eFlag, PgHdr**);
553e0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
553f0 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 void sqlite3Pcac
55400 68 65 52 65 6c 65 61 73 65 28 50 67 48 64 72 2a heRelease(PgHdr*
55410 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 );..SQLITE_PRIVA
55420 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 TE void sqlite3P
55430 63 61 63 68 65 44 72 6f 70 28 50 67 48 64 72 2a cacheDrop(PgHdr*
55440 29 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 ); /* Re
55450 6d 6f 76 65 20 70 61 67 65 20 66 72 6f 6d 20 63 move page from c
55460 61 63 68 65 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 ache */.SQLITE_P
55470 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
55480 74 65 33 50 63 61 63 68 65 4d 61 6b 65 44 69 72 te3PcacheMakeDir
55490 74 79 28 50 67 48 64 72 2a 29 3b 20 20 20 20 2f ty(PgHdr*); /
554a0 2a 20 4d 61 6b 65 20 73 75 72 65 20 70 61 67 65 * Make sure page
554b0 20 69 73 20 6d 61 72 6b 65 64 20 64 69 72 74 79 is marked dirty
554c0 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 */.SQLITE_PRIVA
554d0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 TE void sqlite3P
554e0 63 61 63 68 65 4d 61 6b 65 43 6c 65 61 6e 28 50 cacheMakeClean(P
554f0 67 48 64 72 2a 29 3b 20 20 20 20 2f 2a 20 4d 61 gHdr*); /* Ma
55500 72 6b 20 61 20 73 69 6e 67 6c 65 20 70 61 67 65 rk a single page
55510 20 61 73 20 63 6c 65 61 6e 20 2a 2f 0a 53 51 4c as clean */.SQL
55520 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
55530 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c sqlite3PcacheCl
55540 65 61 6e 41 6c 6c 28 50 43 61 63 68 65 2a 29 3b eanAll(PCache*);
55550 20 20 20 20 2f 2a 20 4d 61 72 6b 20 61 6c 6c 20 /* Mark all
55560 64 69 72 74 79 20 6c 69 73 74 20 70 61 67 65 73 dirty list pages
55570 20 61 73 20 63 6c 65 61 6e 20 2a 2f 0a 0a 2f 2a as clean */../*
55580 20 43 68 61 6e 67 65 20 61 20 70 61 67 65 20 6e Change a page n
55590 75 6d 62 65 72 2e 20 20 55 73 65 64 20 62 79 20 umber. Used by
555a0 69 6e 63 72 2d 76 61 63 75 75 6d 2e 20 2a 2f 0a incr-vacuum. */.
555b0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
555c0 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 oid sqlite3Pcach
555d0 65 4d 6f 76 65 28 50 67 48 64 72 2a 2c 20 50 67 eMove(PgHdr*, Pg
555e0 6e 6f 29 3b 0a 0a 2f 2a 20 52 65 6d 6f 76 65 20 no);../* Remove
555f0 61 6c 6c 20 70 61 67 65 73 20 77 69 74 68 20 70 all pages with p
55600 67 6e 6f 3e 78 2e 20 20 52 65 73 65 74 20 74 68 gno>x. Reset th
55610 65 20 63 61 63 68 65 20 69 66 20 78 3d 3d 30 20 e cache if x==0
55620 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
55630 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 E void sqlite3Pc
55640 61 63 68 65 54 72 75 6e 63 61 74 65 28 50 43 61 acheTruncate(PCa
55650 63 68 65 2a 2c 20 50 67 6e 6f 20 78 29 3b 0a 0a che*, Pgno x);..
55660 2f 2a 20 47 65 74 20 61 20 6c 69 73 74 20 6f 66 /* Get a list of
55670 20 61 6c 6c 20 64 69 72 74 79 20 70 61 67 65 73 all dirty pages
55680 20 69 6e 20 74 68 65 20 63 61 63 68 65 2c 20 73 in the cache, s
55690 6f 72 74 65 64 20 62 79 20 70 61 67 65 20 6e 75 orted by page nu
556a0 6d 62 65 72 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 mber */.SQLITE_P
556b0 52 49 56 41 54 45 20 50 67 48 64 72 20 2a 73 71 RIVATE PgHdr *sq
556c0 6c 69 74 65 33 50 63 61 63 68 65 44 69 72 74 79 lite3PcacheDirty
556d0 4c 69 73 74 28 50 43 61 63 68 65 2a 29 3b 0a 0a List(PCache*);..
556e0 2f 2a 20 52 65 73 65 74 20 61 6e 64 20 63 6c 6f /* Reset and clo
556f0 73 65 20 74 68 65 20 63 61 63 68 65 20 6f 62 6a se the cache obj
55700 65 63 74 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ect */.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 43 6c 6f 73 65 28 50 43 e3PcacheClose(PC
55730 61 63 68 65 2a 29 3b 0a 0a 2f 2a 20 43 6c 65 61 ache*);../* Clea
55740 72 20 66 6c 61 67 73 20 66 72 6f 6d 20 70 61 67 r flags from pag
55750 65 73 20 6f 66 20 74 68 65 20 70 61 67 65 20 63 es of the page c
55760 61 63 68 65 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 ache */.SQLITE_P
55770 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
55780 74 65 33 50 63 61 63 68 65 43 6c 65 61 72 53 79 te3PcacheClearSy
55790 6e 63 46 6c 61 67 73 28 50 43 61 63 68 65 20 2a ncFlags(PCache *
557a0 29 3b 0a 0a 2f 2a 20 44 69 73 63 61 72 64 20 74 );../* Discard t
557b0 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 he contents of t
557c0 68 65 20 63 61 63 68 65 20 2a 2f 0a 53 51 4c 49 he cache */.SQLI
557d0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
557e0 73 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c 65 sqlite3PcacheCle
557f0 61 72 28 50 43 61 63 68 65 2a 29 3b 0a 0a 2f 2a ar(PCache*);../*
55800 20 52 65 74 75 72 6e 20 74 68 65 20 74 6f 74 61 Return the tota
55810 6c 20 6e 75 6d 62 65 72 20 6f 66 20 6f 75 74 73 l number of outs
55820 74 61 6e 64 69 6e 67 20 70 61 67 65 20 72 65 66 tanding page ref
55830 65 72 65 6e 63 65 73 20 2a 2f 0a 53 51 4c 49 54 erences */.SQLIT
55840 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
55850 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 43 6f lite3PcacheRefCo
55860 75 6e 74 28 50 43 61 63 68 65 2a 29 3b 0a 0a 2f unt(PCache*);../
55870 2a 20 49 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 * Increment the
55880 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 reference count
55890 6f 66 20 61 6e 20 65 78 69 73 74 69 6e 67 20 70 of an existing p
558a0 61 67 65 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 age */.SQLITE_PR
558b0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
558c0 65 33 50 63 61 63 68 65 52 65 66 28 50 67 48 64 e3PcacheRef(PgHd
558d0 72 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 r*);..SQLITE_PRI
558e0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
558f0 50 63 61 63 68 65 50 61 67 65 52 65 66 63 6f 75 PcachePageRefcou
55900 6e 74 28 50 67 48 64 72 2a 29 3b 0a 0a 2f 2a 20 nt(PgHdr*);../*
55910 52 65 74 75 72 6e 20 74 68 65 20 74 6f 74 61 6c Return the total
55920 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 number of pages
55930 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 63 stored in the c
55940 61 63 68 65 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 ache */.SQLITE_P
55950 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
55960 65 33 50 63 61 63 68 65 50 61 67 65 63 6f 75 6e e3PcachePagecoun
55970 74 28 50 43 61 63 68 65 2a 29 3b 0a 0a 23 69 66 t(PCache*);..#if
55980 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f defined(SQLITE_
55990 43 48 45 43 4b 5f 50 41 47 45 53 29 20 7c 7c 20 CHECK_PAGES) ||
559a0 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 defined(SQLITE_D
559b0 45 42 55 47 29 0a 2f 2a 20 49 74 65 72 61 74 65 EBUG)./* Iterate
559c0 20 74 68 72 6f 75 67 68 20 61 6c 6c 20 64 69 72 through all dir
559d0 74 79 20 70 61 67 65 73 20 63 75 72 72 65 6e 74 ty pages current
559e0 6c 79 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 ly stored in the
559f0 20 63 61 63 68 65 2e 20 54 68 69 73 0a 2a 2a 20 cache. This.**
55a00 69 6e 74 65 72 66 61 63 65 20 69 73 20 6f 6e 6c interface is onl
55a10 79 20 61 76 61 69 6c 61 62 6c 65 20 69 66 20 53 y available if S
55a20 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 QLITE_CHECK_PAGE
55a30 53 20 69 73 20 64 65 66 69 6e 65 64 20 77 68 65 S is defined whe
55a40 6e 20 74 68 65 20 0a 2a 2a 20 6c 69 62 72 61 72 n the .** librar
55a50 79 20 69 73 20 62 75 69 6c 74 2e 0a 2a 2f 0a 53 y is built..*/.S
55a60 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
55a70 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 id sqlite3Pcache
55a80 49 74 65 72 61 74 65 44 69 72 74 79 28 50 43 61 IterateDirty(PCa
55a90 63 68 65 20 2a 70 43 61 63 68 65 2c 20 76 6f 69 che *pCache, voi
55aa0 64 20 28 2a 78 49 74 65 72 29 28 50 67 48 64 72 d (*xIter)(PgHdr
55ab0 20 2a 29 29 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a *));.#endif../*
55ac0 20 53 65 74 20 61 6e 64 20 67 65 74 20 74 68 65 Set and get the
55ad0 20 73 75 67 67 65 73 74 65 64 20 63 61 63 68 65 suggested cache
55ae0 2d 73 69 7a 65 20 66 6f 72 20 74 68 65 20 73 70 -size for the sp
55af0 65 63 69 66 69 65 64 20 70 61 67 65 72 2d 63 61 ecified pager-ca
55b00 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f che..**.** If no
55b10 20 67 6c 6f 62 61 6c 20 6d 61 78 69 6d 75 6d 20 global maximum
55b20 69 73 20 63 6f 6e 66 69 67 75 72 65 64 2c 20 74 is configured, t
55b30 68 65 6e 20 74 68 65 20 73 79 73 74 65 6d 20 61 hen the system a
55b40 74 74 65 6d 70 74 73 20 74 6f 20 6c 69 6d 69 74 ttempts to limit
55b50 0a 2a 2a 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 .** the total nu
55b60 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 63 61 mber of pages ca
55b70 63 68 65 64 20 62 79 20 70 75 72 67 65 61 62 6c ched by purgeabl
55b80 65 20 70 61 67 65 72 2d 63 61 63 68 65 73 20 74 e pager-caches t
55b90 6f 20 74 68 65 20 73 75 6d 0a 2a 2a 20 6f 66 20 o the sum.** of
55ba0 74 68 65 20 73 75 67 67 65 73 74 65 64 20 63 61 the suggested ca
55bb0 63 68 65 2d 73 69 7a 65 73 2e 0a 2a 2f 0a 53 51 che-sizes..*/.SQ
55bc0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
55bd0 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 53 d sqlite3PcacheS
55be0 65 74 43 61 63 68 65 73 69 7a 65 28 50 43 61 63 etCachesize(PCac
55bf0 68 65 20 2a 2c 20 69 6e 74 29 3b 0a 23 69 66 64 he *, int);.#ifd
55c00 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 ef SQLITE_TEST.S
55c10 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
55c20 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 47 t sqlite3PcacheG
55c30 65 74 43 61 63 68 65 73 69 7a 65 28 50 43 61 63 etCachesize(PCac
55c40 68 65 20 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 23 he *);.#endif..#
55c50 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 ifdef SQLITE_ENA
55c60 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 BLE_MEMORY_MANAG
55c70 45 4d 45 4e 54 0a 2f 2a 20 54 72 79 20 74 6f 20 EMENT./* Try to
55c80 72 65 74 75 72 6e 20 6d 65 6d 6f 72 79 20 75 73 return memory us
55c90 65 64 20 62 79 20 74 68 65 20 70 63 61 63 68 65 ed by the pcache
55ca0 20 6d 6f 64 75 6c 65 20 74 6f 20 74 68 65 20 6d module to the m
55cb0 61 69 6e 20 6d 65 6d 6f 72 79 20 68 65 61 70 20 ain memory heap
55cc0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
55cd0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 E int sqlite3Pca
55ce0 63 68 65 52 65 6c 65 61 73 65 4d 65 6d 6f 72 79 cheReleaseMemory
55cf0 28 69 6e 74 29 3b 0a 23 65 6e 64 69 66 0a 0a 23 (int);.#endif..#
55d00 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 ifdef SQLITE_TES
55d10 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 T.SQLITE_PRIVATE
55d20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 void sqlite3Pca
55d30 63 68 65 53 74 61 74 73 28 69 6e 74 2a 2c 69 6e cheStats(int*,in
55d40 74 2a 2c 69 6e 74 2a 2c 69 6e 74 2a 29 3b 0a 23 t*,int*,int*);.#
55d50 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52 endif..SQLITE_PR
55d60 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
55d70 65 33 50 43 61 63 68 65 53 65 74 44 65 66 61 75 e3PCacheSetDefau
55d80 6c 74 28 76 6f 69 64 29 3b 0a 0a 23 65 6e 64 69 lt(void);..#endi
55d90 66 20 2f 2a 20 5f 50 43 41 43 48 45 5f 48 5f 20 f /* _PCACHE_H_
55da0 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../***********
55db0 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 63 61 63 68 *** End of pcach
55dc0 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e.h ************
55dd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
55de0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
55df0 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
55e00 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 *** Continuing w
55e10 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 here we left off
55e20 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 in sqliteInt.h
55e30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
55e40 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/../**********
55e50 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 6f 73 2e **** Include os.
55e60 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 h in the middle
55e70 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a of sqliteInt.h *
55e80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
55e90 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
55ea0 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 **** Begin file
55eb0 6f 73 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a os.h ***********
55ec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
55ed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
55ee0 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 ***/./*.** 2001
55ef0 53 65 70 74 65 6d 62 65 72 20 31 36 0a 2a 2a 0a September 16.**.
55f00 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 ** The author di
55f10 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 sclaims copyrigh
55f20 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 t to this source
55f30 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 code. In place
55f40 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e of.** a legal n
55f50 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 otice, here is a
55f60 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a blessing:.**.**
55f70 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 May you do g
55f80 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c ood and not evil
55f90 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
55fa0 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 find forgiveness
55fb0 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e for yourself an
55fc0 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 d forgive others
55fd0 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
55fe0 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 share freely, ne
55ff0 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 ver taking more
56000 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a than you give..*
56010 2a 0a 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 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
56040 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
56050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
56060 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 .**.** This head
56070 65 72 20 66 69 6c 65 20 28 74 6f 67 65 74 68 65 er file (togethe
56080 72 20 77 69 74 68 20 69 73 20 63 6f 6d 70 61 6e r with is compan
56090 69 6f 6e 20 43 20 73 6f 75 72 63 65 2d 63 6f 64 ion C source-cod
560a0 65 20 66 69 6c 65 0a 2a 2a 20 22 6f 73 2e 63 22 e file.** "os.c"
560b0 29 20 61 74 74 65 6d 70 74 20 74 6f 20 61 62 73 ) attempt to abs
560c0 74 72 61 63 74 20 74 68 65 20 75 6e 64 65 72 6c tract the underl
560d0 79 69 6e 67 20 6f 70 65 72 61 74 69 6e 67 20 73 ying operating s
560e0 79 73 74 65 6d 20 73 6f 20 74 68 61 74 0a 2a 2a ystem so that.**
560f0 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 the SQLite libr
56100 61 72 79 20 77 69 6c 6c 20 77 6f 72 6b 20 6f 6e ary will work on
56110 20 62 6f 74 68 20 50 4f 53 49 58 20 61 6e 64 20 both POSIX and
56120 77 69 6e 64 6f 77 73 20 73 79 73 74 65 6d 73 2e windows systems.
56130 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 .**.** This head
56140 65 72 20 66 69 6c 65 20 69 73 20 23 69 6e 63 6c er file is #incl
56150 75 64 65 2d 65 64 20 62 79 20 73 71 6c 69 74 65 ude-ed by sqlite
56160 49 6e 74 2e 68 20 61 6e 64 20 74 68 75 73 20 65 Int.h and thus e
56170 6e 64 73 20 75 70 0a 2a 2a 20 62 65 69 6e 67 20 nds up.** being
56180 69 6e 63 6c 75 64 65 64 20 62 79 20 65 76 65 72 included by ever
56190 79 20 73 6f 75 72 63 65 20 66 69 6c 65 2e 0a 2a y source file..*
561a0 2a 0a 2a 2a 20 24 49 64 3a 20 6f 73 2e 68 2c 76 *.** $Id: os.h,v
561b0 20 31 2e 31 30 38 20 32 30 30 39 2f 30 32 2f 30 1.108 2009/02/0
561c0 35 20 31 36 3a 33 31 3a 34 36 20 64 72 68 20 45 5 16:31:46 drh E
561d0 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 xp $.*/.#ifndef
561e0 5f 53 51 4c 49 54 45 5f 4f 53 5f 48 5f 0a 23 64 _SQLITE_OS_H_.#d
561f0 65 66 69 6e 65 20 5f 53 51 4c 49 54 45 5f 4f 53 efine _SQLITE_OS
56200 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 46 69 67 75 72 _H_../*.** Figur
56210 65 20 6f 75 74 20 69 66 20 77 65 20 61 72 65 20 e out if we are
56220 64 65 61 6c 69 6e 67 20 77 69 74 68 20 55 6e 69 dealing with Uni
56230 78 2c 20 57 69 6e 64 6f 77 73 2c 20 6f 72 20 73 x, Windows, or s
56240 6f 6d 65 20 6f 74 68 65 72 0a 2a 2a 20 6f 70 65 ome other.** ope
56250 72 61 74 69 6e 67 20 73 79 73 74 65 6d 2e 20 20 rating system.
56260 41 66 74 65 72 20 74 68 65 20 66 6f 6c 6c 6f 77 After the follow
56270 69 6e 67 20 62 6c 6f 63 6b 20 6f 66 20 70 72 65 ing block of pre
56280 70 72 6f 63 65 73 73 20 6d 61 63 72 6f 73 2c 0a process macros,.
56290 2a 2a 20 61 6c 6c 20 6f 66 20 53 51 4c 49 54 45 ** all of SQLITE
562a0 5f 4f 53 5f 55 4e 49 58 2c 20 53 51 4c 49 54 45 _OS_UNIX, SQLITE
562b0 5f 4f 53 5f 57 49 4e 2c 20 53 51 4c 49 54 45 5f _OS_WIN, SQLITE_
562c0 4f 53 5f 4f 53 32 2c 20 61 6e 64 20 53 51 4c 49 OS_OS2, and SQLI
562d0 54 45 5f 4f 53 5f 4f 54 48 45 52 20 0a 2a 2a 20 TE_OS_OTHER .**
562e0 77 69 6c 6c 20 64 65 66 69 6e 65 64 20 74 6f 20 will defined to
562f0 65 69 74 68 65 72 20 31 20 6f 72 20 30 2e 20 20 either 1 or 0.
56300 4f 6e 65 20 6f 66 20 74 68 65 20 66 6f 75 72 20 One of the four
56310 77 69 6c 6c 20 62 65 20 31 2e 20 20 54 68 65 20 will be 1. The
56320 6f 74 68 65 72 20 0a 2a 2a 20 74 68 72 65 65 20 other .** three
56330 77 69 6c 6c 20 62 65 20 30 2e 0a 2a 2f 0a 23 69 will be 0..*/.#i
56340 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 f defined(SQLITE
56350 5f 4f 53 5f 4f 54 48 45 52 29 0a 23 20 69 66 20 _OS_OTHER).# if
56360 53 51 4c 49 54 45 5f 4f 53 5f 4f 54 48 45 52 3d SQLITE_OS_OTHER=
56370 3d 31 0a 23 20 20 20 75 6e 64 65 66 20 53 51 4c =1.# undef SQL
56380 49 54 45 5f 4f 53 5f 55 4e 49 58 0a 23 20 20 20 ITE_OS_UNIX.#
56390 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 define SQLITE_OS
563a0 5f 55 4e 49 58 20 30 0a 23 20 20 20 75 6e 64 65 _UNIX 0.# unde
563b0 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 0a f SQLITE_OS_WIN.
563c0 23 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 # define SQLIT
563d0 45 5f 4f 53 5f 57 49 4e 20 30 0a 23 20 20 20 75 E_OS_WIN 0.# u
563e0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 53 5f 4f ndef SQLITE_OS_O
563f0 53 32 0a 23 20 20 20 64 65 66 69 6e 65 20 53 51 S2.# define SQ
56400 4c 49 54 45 5f 4f 53 5f 4f 53 32 20 30 0a 23 20 LITE_OS_OS2 0.#
56410 65 6c 73 65 0a 23 20 20 20 75 6e 64 65 66 20 53 else.# undef S
56420 51 4c 49 54 45 5f 4f 53 5f 4f 54 48 45 52 0a 23 QLITE_OS_OTHER.#
56430 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 23 69 endif.#endif.#i
56440 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 f !defined(SQLIT
56450 45 5f 4f 53 5f 55 4e 49 58 29 20 26 26 20 21 64 E_OS_UNIX) && !d
56460 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 53 efined(SQLITE_OS
56470 5f 4f 54 48 45 52 29 0a 23 20 64 65 66 69 6e 65 _OTHER).# define
56480 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 54 48 45 52 SQLITE_OS_OTHER
56490 20 30 0a 23 20 69 66 6e 64 65 66 20 53 51 4c 49 0.# ifndef SQLI
564a0 54 45 5f 4f 53 5f 57 49 4e 0a 23 20 20 20 69 66 TE_OS_WIN.# if
564b0 20 64 65 66 69 6e 65 64 28 5f 57 49 4e 33 32 29 defined(_WIN32)
564c0 20 7c 7c 20 64 65 66 69 6e 65 64 28 57 49 4e 33 || defined(WIN3
564d0 32 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f 2) || defined(__
564e0 43 59 47 57 49 4e 5f 5f 29 20 7c 7c 20 64 65 66 CYGWIN__) || def
564f0 69 6e 65 64 28 5f 5f 4d 49 4e 47 57 33 32 5f 5f ined(__MINGW32__
56500 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f 42 ) || defined(__B
56510 4f 52 4c 41 4e 44 43 5f 5f 29 0a 23 20 20 20 20 ORLANDC__).#
56520 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f define SQLITE_O
56530 53 5f 57 49 4e 20 31 0a 23 20 20 20 20 20 64 65 S_WIN 1.# de
56540 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 55 fine SQLITE_OS_U
56550 4e 49 58 20 30 0a 23 20 20 20 20 20 64 65 66 69 NIX 0.# defi
56560 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 ne SQLITE_OS_OS2
56570 20 30 0a 23 20 20 20 65 6c 69 66 20 64 65 66 69 0.# elif defi
56580 6e 65 64 28 5f 5f 45 4d 58 5f 5f 29 20 7c 7c 20 ned(__EMX__) ||
56590 64 65 66 69 6e 65 64 28 5f 4f 53 32 29 20 7c 7c defined(_OS2) ||
565a0 20 64 65 66 69 6e 65 64 28 4f 53 32 29 20 7c 7c defined(OS2) ||
565b0 20 64 65 66 69 6e 65 64 28 5f 4f 53 32 5f 29 20 defined(_OS2_)
565c0 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f 4f 53 32 || defined(__OS2
565d0 5f 5f 29 0a 23 20 20 20 20 20 64 65 66 69 6e 65 __).# define
565e0 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 20 30 SQLITE_OS_WIN 0
565f0 0a 23 20 20 20 20 20 64 65 66 69 6e 65 20 53 51 .# define SQ
56600 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 20 30 0a 23 LITE_OS_UNIX 0.#
56610 20 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 define SQLI
56620 54 45 5f 4f 53 5f 4f 53 32 20 31 0a 23 20 20 20 TE_OS_OS2 1.#
56630 65 6c 73 65 0a 23 20 20 20 20 20 64 65 66 69 6e else.# defin
56640 65 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 20 e SQLITE_OS_WIN
56650 30 0a 23 20 20 20 20 20 64 65 66 69 6e 65 20 53 0.# define S
56660 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 20 31 0a QLITE_OS_UNIX 1.
56670 23 20 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c # define SQL
56680 49 54 45 5f 4f 53 5f 4f 53 32 20 30 0a 23 20 20 ITE_OS_OS2 0.#
56690 65 6e 64 69 66 0a 23 20 65 6c 73 65 0a 23 20 20 endif.# else.#
566a0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 define SQLITE_OS
566b0 5f 55 4e 49 58 20 30 0a 23 20 20 64 65 66 69 6e _UNIX 0.# defin
566c0 65 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 20 e SQLITE_OS_OS2
566d0 30 0a 23 20 65 6e 64 69 66 0a 23 65 6c 73 65 0a 0.# endif.#else.
566e0 23 20 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f # ifndef SQLITE_
566f0 4f 53 5f 57 49 4e 0a 23 20 20 64 65 66 69 6e 65 OS_WIN.# define
56700 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 20 30 SQLITE_OS_WIN 0
56710 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a .# endif.#endif.
56720 0a 2f 2a 0a 2a 2a 20 44 65 74 65 72 6d 69 6e 65 ./*.** Determine
56730 20 69 66 20 77 65 20 61 72 65 20 64 65 61 6c 69 if we are deali
56740 6e 67 20 77 69 74 68 20 57 69 6e 64 6f 77 73 43 ng with WindowsC
56750 45 20 2d 20 77 68 69 63 68 20 68 61 73 20 61 20 E - which has a
56760 6d 75 63 68 0a 2a 2a 20 72 65 64 75 63 65 64 20 much.** reduced
56770 41 50 49 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 API..*/.#if defi
56780 6e 65 64 28 5f 57 49 4e 33 32 5f 57 43 45 29 0a ned(_WIN32_WCE).
56790 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f # define SQLITE_
567a0 4f 53 5f 57 49 4e 43 45 20 31 0a 23 65 6c 73 65 OS_WINCE 1.#else
567b0 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 .# define SQLITE
567c0 5f 4f 53 5f 57 49 4e 43 45 20 30 0a 23 65 6e 64 _OS_WINCE 0.#end
567d0 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 69 6e if.../*.** Defin
567e0 65 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 73 69 e the maximum si
567f0 7a 65 20 6f 66 20 61 20 74 65 6d 70 6f 72 61 72 ze of a temporar
56800 79 20 66 69 6c 65 6e 61 6d 65 0a 2a 2f 0a 23 69 y filename.*/.#i
56810 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 0a f SQLITE_OS_WIN.
56820 23 20 69 6e 63 6c 75 64 65 20 3c 77 69 6e 64 6f # include <windo
56830 77 73 2e 68 3e 0a 23 20 64 65 66 69 6e 65 20 53 ws.h>.# define S
56840 51 4c 49 54 45 5f 54 45 4d 50 4e 41 4d 45 5f 53 QLITE_TEMPNAME_S
56850 49 5a 45 20 28 4d 41 58 5f 50 41 54 48 2b 35 30 IZE (MAX_PATH+50
56860 29 0a 23 65 6c 69 66 20 53 51 4c 49 54 45 5f 4f ).#elif SQLITE_O
56870 53 5f 4f 53 32 0a 23 20 69 66 20 28 5f 5f 47 4e S_OS2.# if (__GN
56880 55 43 5f 5f 20 3e 20 33 20 7c 7c 20 5f 5f 47 4e UC__ > 3 || __GN
56890 55 43 5f 5f 20 3d 3d 20 33 20 26 26 20 5f 5f 47 UC__ == 3 && __G
568a0 4e 55 43 5f 4d 49 4e 4f 52 5f 5f 20 3e 3d 20 33 NUC_MINOR__ >= 3
568b0 29 20 26 26 20 64 65 66 69 6e 65 64 28 4f 53 32 ) && defined(OS2
568c0 5f 48 49 47 48 5f 4d 45 4d 4f 52 59 29 0a 23 20 _HIGH_MEMORY).#
568d0 20 69 6e 63 6c 75 64 65 20 3c 6f 73 32 73 61 66 include <os2saf
568e0 65 2e 68 3e 20 2f 2a 20 68 61 73 20 74 6f 20 62 e.h> /* has to b
568f0 65 20 69 6e 63 6c 75 64 65 64 20 62 65 66 6f 72 e included befor
56900 65 20 6f 73 32 2e 68 20 66 6f 72 20 6c 69 6e 6b e os2.h for link
56910 69 6e 67 20 74 6f 20 77 6f 72 6b 20 2a 2f 0a 23 ing to work */.#
56920 20 65 6e 64 69 66 0a 23 20 64 65 66 69 6e 65 20 endif.# define
56930 49 4e 43 4c 5f 44 4f 53 44 41 54 45 54 49 4d 45 INCL_DOSDATETIME
56940 0a 23 20 64 65 66 69 6e 65 20 49 4e 43 4c 5f 44 .# define INCL_D
56950 4f 53 46 49 4c 45 4d 47 52 0a 23 20 64 65 66 69 OSFILEMGR.# defi
56960 6e 65 20 49 4e 43 4c 5f 44 4f 53 45 52 52 4f 52 ne INCL_DOSERROR
56970 53 0a 23 20 64 65 66 69 6e 65 20 49 4e 43 4c 5f S.# define INCL_
56980 44 4f 53 4d 49 53 43 0a 23 20 64 65 66 69 6e 65 DOSMISC.# define
56990 20 49 4e 43 4c 5f 44 4f 53 50 52 4f 43 45 53 53 INCL_DOSPROCESS
569a0 0a 23 20 64 65 66 69 6e 65 20 49 4e 43 4c 5f 44 .# define INCL_D
569b0 4f 53 4d 4f 44 55 4c 45 4d 47 52 0a 23 20 64 65 OSMODULEMGR.# de
569c0 66 69 6e 65 20 49 4e 43 4c 5f 44 4f 53 53 45 4d fine INCL_DOSSEM
569d0 41 50 48 4f 52 45 53 0a 23 20 69 6e 63 6c 75 64 APHORES.# includ
569e0 65 20 3c 6f 73 32 2e 68 3e 0a 23 20 69 6e 63 6c e <os2.h>.# incl
569f0 75 64 65 20 3c 75 63 6f 6e 76 2e 68 3e 0a 23 20 ude <uconv.h>.#
56a00 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 define SQLITE_TE
56a10 4d 50 4e 41 4d 45 5f 53 49 5a 45 20 28 43 43 48 MPNAME_SIZE (CCH
56a20 4d 41 58 50 41 54 48 43 4f 4d 50 29 0a 23 65 6c MAXPATHCOMP).#el
56a30 73 65 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 se.# define SQLI
56a40 54 45 5f 54 45 4d 50 4e 41 4d 45 5f 53 49 5a 45 TE_TEMPNAME_SIZE
56a50 20 32 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 200.#endif../*
56a60 49 66 20 74 68 65 20 53 45 54 5f 46 55 4c 4c 53 If the SET_FULLS
56a70 59 4e 43 20 6d 61 63 72 6f 20 69 73 20 6e 6f 74 YNC macro is not
56a80 20 64 65 66 69 6e 65 64 20 61 62 6f 76 65 2c 20 defined above,
56a90 74 68 65 6e 20 6d 61 6b 65 20 69 74 0a 2a 2a 20 then make it.**
56aa0 61 20 6e 6f 2d 6f 70 0a 2a 2f 0a 23 69 66 6e 64 a no-op.*/.#ifnd
56ab0 65 66 20 53 45 54 5f 46 55 4c 4c 53 59 4e 43 0a ef SET_FULLSYNC.
56ac0 23 20 64 65 66 69 6e 65 20 53 45 54 5f 46 55 4c # define SET_FUL
56ad0 4c 53 59 4e 43 28 78 2c 79 29 0a 23 65 6e 64 69 LSYNC(x,y).#endi
56ae0 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 64 65 66 f../*.** The def
56af0 61 75 6c 74 20 73 69 7a 65 20 6f 66 20 61 20 64 ault size of a d
56b00 69 73 6b 20 73 65 63 74 6f 72 0a 2a 2f 0a 23 69 isk sector.*/.#i
56b10 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45 46 fndef SQLITE_DEF
56b20 41 55 4c 54 5f 53 45 43 54 4f 52 5f 53 49 5a 45 AULT_SECTOR_SIZE
56b30 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 .# define SQLITE
56b40 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f 52 5f _DEFAULT_SECTOR_
56b50 53 49 5a 45 20 35 31 32 0a 23 65 6e 64 69 66 0a SIZE 512.#endif.
56b60 0a 2f 2a 0a 2a 2a 20 54 65 6d 70 6f 72 61 72 79 ./*.** Temporary
56b70 20 66 69 6c 65 73 20 61 72 65 20 6e 61 6d 65 64 files are named
56b80 20 73 74 61 72 74 69 6e 67 20 77 69 74 68 20 74 starting with t
56b90 68 69 73 20 70 72 65 66 69 78 20 66 6f 6c 6c 6f his prefix follo
56ba0 77 65 64 20 62 79 20 31 36 20 72 61 6e 64 6f 6d wed by 16 random
56bb0 0a 2a 2a 20 61 6c 70 68 61 6e 75 6d 65 72 69 63 .** alphanumeric
56bc0 20 63 68 61 72 61 63 74 65 72 73 2c 20 61 6e 64 characters, and
56bd0 20 6e 6f 20 66 69 6c 65 20 65 78 74 65 6e 73 69 no file extensi
56be0 6f 6e 2e 20 54 68 65 79 20 61 72 65 20 73 74 6f on. They are sto
56bf0 72 65 64 20 69 6e 20 74 68 65 0a 2a 2a 20 4f 53 red in the.** OS
56c00 27 73 20 73 74 61 6e 64 61 72 64 20 74 65 6d 70 's standard temp
56c10 6f 72 61 72 79 20 66 69 6c 65 20 64 69 72 65 63 orary file direc
56c20 74 6f 72 79 2c 20 61 6e 64 20 61 72 65 20 64 65 tory, and are de
56c30 6c 65 74 65 64 20 70 72 69 6f 72 20 74 6f 20 65 leted prior to e
56c40 78 69 74 2e 0a 2a 2a 20 49 66 20 73 71 6c 69 74 xit..** If sqlit
56c50 65 20 69 73 20 62 65 69 6e 67 20 65 6d 62 65 64 e is being embed
56c60 64 65 64 20 69 6e 20 61 6e 6f 74 68 65 72 20 70 ded in another p
56c70 72 6f 67 72 61 6d 2c 20 79 6f 75 20 6d 61 79 20 rogram, you may
56c80 77 69 73 68 20 74 6f 20 63 68 61 6e 67 65 20 74 wish to change t
56c90 68 65 0a 2a 2a 20 70 72 65 66 69 78 20 74 6f 20 he.** prefix to
56ca0 72 65 66 6c 65 63 74 20 79 6f 75 72 20 70 72 6f reflect your pro
56cb0 67 72 61 6d 27 73 20 6e 61 6d 65 2c 20 73 6f 20 gram's name, so
56cc0 74 68 61 74 20 69 66 20 79 6f 75 72 20 70 72 6f that if your pro
56cd0 67 72 61 6d 20 65 78 69 74 73 0a 2a 2a 20 70 72 gram exits.** pr
56ce0 65 6d 61 74 75 72 65 6c 79 2c 20 6f 6c 64 20 74 ematurely, old t
56cf0 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 20 63 emporary files c
56d00 61 6e 20 62 65 20 65 61 73 69 6c 79 20 69 64 65 an be easily ide
56d10 6e 74 69 66 69 65 64 2e 20 54 68 69 73 20 63 61 ntified. This ca
56d20 6e 20 62 65 20 64 6f 6e 65 0a 2a 2a 20 75 73 69 n be done.** usi
56d30 6e 67 20 2d 44 53 51 4c 49 54 45 5f 54 45 4d 50 ng -DSQLITE_TEMP
56d40 5f 46 49 4c 45 5f 50 52 45 46 49 58 3d 6d 79 70 _FILE_PREFIX=myp
56d50 72 65 66 69 78 5f 20 6f 6e 20 74 68 65 20 63 6f refix_ on the co
56d60 6d 70 69 6c 65 72 20 63 6f 6d 6d 61 6e 64 20 6c mpiler command l
56d70 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 32 30 30 36 2d ine..**.** 2006-
56d80 31 30 2d 33 31 3a 20 20 54 68 65 20 64 65 66 61 10-31: The defa
56d90 75 6c 74 20 70 72 65 66 69 78 20 75 73 65 64 20 ult prefix used
56da0 74 6f 20 62 65 20 22 73 71 6c 69 74 65 5f 22 2e to be "sqlite_".
56db0 20 20 42 75 74 20 74 68 65 6e 0a 2a 2a 20 4d 63 But then.** Mc
56dc0 61 66 65 65 20 73 74 61 72 74 65 64 20 75 73 69 afee started usi
56dd0 6e 67 20 53 51 4c 69 74 65 20 69 6e 20 74 68 65 ng SQLite in the
56de0 69 72 20 61 6e 74 69 2d 76 69 72 75 73 20 70 72 ir anti-virus pr
56df0 6f 64 75 63 74 20 61 6e 64 20 69 74 0a 2a 2a 20 oduct and it.**
56e00 73 74 61 72 74 65 64 20 70 75 74 74 69 6e 67 20 started putting
56e10 66 69 6c 65 73 20 77 69 74 68 20 74 68 65 20 22 files with the "
56e20 73 71 6c 69 74 65 22 20 6e 61 6d 65 20 69 6e 20 sqlite" name in
56e30 74 68 65 20 63 3a 2f 74 65 6d 70 20 66 6f 6c 64 the c:/temp fold
56e40 65 72 2e 0a 2a 2a 20 54 68 69 73 20 61 6e 6e 6f er..** This anno
56e50 79 65 64 20 6d 61 6e 79 20 77 69 6e 64 6f 77 73 yed many windows
56e60 20 75 73 65 72 73 2e 20 20 54 68 6f 73 65 20 75 users. Those u
56e70 73 65 72 73 20 77 6f 75 6c 64 20 74 68 65 6e 20 sers would then
56e80 64 6f 20 61 20 0a 2a 2a 20 47 6f 6f 67 6c 65 20 do a .** Google
56e90 73 65 61 72 63 68 20 66 6f 72 20 22 73 71 6c 69 search for "sqli
56ea0 74 65 22 2c 20 66 69 6e 64 20 74 68 65 20 74 65 te", find the te
56eb0 6c 65 70 68 6f 6e 65 20 6e 75 6d 62 65 72 73 20 lephone numbers
56ec0 6f 66 20 74 68 65 0a 2a 2a 20 64 65 76 65 6c 6f of the.** develo
56ed0 70 65 72 73 20 61 6e 64 20 63 61 6c 6c 20 74 6f pers and call to
56ee0 20 77 61 6b 65 20 74 68 65 6d 20 75 70 20 61 74 wake them up at
56ef0 20 6e 69 67 68 74 20 61 6e 64 20 63 6f 6d 70 6c night and compl
56f00 61 69 6e 2e 0a 2a 2a 20 46 6f 72 20 74 68 69 73 ain..** For this
56f10 20 72 65 61 73 6f 6e 2c 20 74 68 65 20 64 65 66 reason, the def
56f20 61 75 6c 74 20 6e 61 6d 65 20 70 72 65 66 69 78 ault name prefix
56f30 20 69 73 20 63 68 61 6e 67 65 64 20 74 6f 20 62 is changed to b
56f40 65 20 22 73 71 6c 69 74 65 22 20 0a 2a 2a 20 73 e "sqlite" .** s
56f50 70 65 6c 6c 65 64 20 62 61 63 6b 77 61 72 64 73 pelled backwards
56f60 2e 20 20 53 6f 20 74 68 65 20 74 65 6d 70 20 66 . So the temp f
56f70 69 6c 65 73 20 61 72 65 20 73 74 69 6c 6c 20 69 iles are still i
56f80 64 65 6e 74 69 66 69 65 64 2c 20 62 75 74 0a 2a dentified, but.*
56f90 2a 20 61 6e 79 62 6f 64 79 20 73 6d 61 72 74 20 * anybody smart
56fa0 65 6e 6f 75 67 68 20 74 6f 20 66 69 67 75 72 65 enough to figure
56fb0 20 6f 75 74 20 74 68 65 20 63 6f 64 65 20 69 73 out the code is
56fc0 20 61 6c 73 6f 20 6c 69 6b 65 6c 79 20 73 6d 61 also likely sma
56fd0 72 74 0a 2a 2a 20 65 6e 6f 75 67 68 20 74 6f 20 rt.** enough to
56fe0 6b 6e 6f 77 20 74 68 61 74 20 63 61 6c 6c 69 6e know that callin
56ff0 67 20 74 68 65 20 64 65 76 65 6c 6f 70 65 72 20 g the developer
57000 77 69 6c 6c 20 6e 6f 74 20 68 65 6c 70 20 67 65 will not help ge
57010 74 20 72 69 64 0a 2a 2a 20 6f 66 20 74 68 65 20 t rid.** of the
57020 66 69 6c 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 file..*/.#ifndef
57030 20 53 51 4c 49 54 45 5f 54 45 4d 50 5f 46 49 4c SQLITE_TEMP_FIL
57040 45 5f 50 52 45 46 49 58 0a 23 20 64 65 66 69 6e E_PREFIX.# defin
57050 65 20 53 51 4c 49 54 45 5f 54 45 4d 50 5f 46 49 e SQLITE_TEMP_FI
57060 4c 45 5f 50 52 45 46 49 58 20 22 65 74 69 6c 71 LE_PREFIX "etilq
57070 73 5f 22 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a s_".#endif../*.*
57080 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * The following
57090 76 61 6c 75 65 73 20 6d 61 79 20 62 65 20 70 61 values may be pa
570a0 73 73 65 64 20 61 73 20 74 68 65 20 73 65 63 6f ssed as the seco
570b0 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 0a 2a nd argument to.*
570c0 2a 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 * sqlite3OsLock(
570d0 29 2e 20 54 68 65 20 76 61 72 69 6f 75 73 20 6c ). The various l
570e0 6f 63 6b 73 20 65 78 68 69 62 69 74 20 74 68 65 ocks exhibit the
570f0 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 65 6d 61 6e following seman
57100 74 69 63 73 3a 0a 2a 2a 0a 2a 2a 20 53 48 41 52 tics:.**.** SHAR
57110 45 44 3a 20 20 20 20 41 6e 79 20 6e 75 6d 62 65 ED: Any numbe
57120 72 20 6f 66 20 70 72 6f 63 65 73 73 65 73 20 6d r of processes m
57130 61 79 20 68 6f 6c 64 20 61 20 53 48 41 52 45 44 ay hold a SHARED
57140 20 6c 6f 63 6b 20 73 69 6d 75 6c 74 61 6e 65 6f lock simultaneo
57150 75 73 6c 79 2e 0a 2a 2a 20 52 45 53 45 52 56 45 usly..** RESERVE
57160 44 3a 20 20 41 20 73 69 6e 67 6c 65 20 70 72 6f D: A single pro
57170 63 65 73 73 20 6d 61 79 20 68 6f 6c 64 20 61 20 cess may hold a
57180 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 6f 6e RESERVED lock on
57190 20 61 20 66 69 6c 65 20 61 74 0a 2a 2a 20 20 20 a file at.**
571a0 20 20 20 20 20 20 20 20 20 61 6e 79 20 74 69 6d any tim
571b0 65 2e 20 4f 74 68 65 72 20 70 72 6f 63 65 73 73 e. Other process
571c0 65 73 20 6d 61 79 20 68 6f 6c 64 20 61 6e 64 20 es may hold and
571d0 6f 62 74 61 69 6e 20 6e 65 77 20 53 48 41 52 45 obtain new SHARE
571e0 44 20 6c 6f 63 6b 73 2e 0a 2a 2a 20 50 45 4e 44 D locks..** PEND
571f0 49 4e 47 3a 20 20 20 41 20 73 69 6e 67 6c 65 20 ING: A single
57200 70 72 6f 63 65 73 73 20 6d 61 79 20 68 6f 6c 64 process may hold
57210 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 a PENDING lock
57220 6f 6e 20 61 20 66 69 6c 65 20 61 74 0a 2a 2a 20 on a file at.**
57230 20 20 20 20 20 20 20 20 20 20 20 61 6e 79 20 6f any o
57240 6e 65 20 74 69 6d 65 2e 20 45 78 69 73 74 69 6e ne time. Existin
57250 67 20 53 48 41 52 45 44 20 6c 6f 63 6b 73 20 6d g SHARED locks m
57260 61 79 20 70 65 72 73 69 73 74 2c 20 62 75 74 20 ay persist, but
57270 6e 6f 20 6e 65 77 0a 2a 2a 20 20 20 20 20 20 20 no new.**
57280 20 20 20 20 20 53 48 41 52 45 44 20 6c 6f 63 6b SHARED lock
57290 73 20 6d 61 79 20 62 65 20 6f 62 74 61 69 6e 65 s may be obtaine
572a0 64 20 62 79 20 6f 74 68 65 72 20 70 72 6f 63 65 d by other proce
572b0 73 73 65 73 2e 0a 2a 2a 20 45 58 43 4c 55 53 49 sses..** EXCLUSI
572c0 56 45 3a 20 41 6e 20 45 58 43 4c 55 53 49 56 45 VE: An EXCLUSIVE
572d0 20 6c 6f 63 6b 20 70 72 65 63 6c 75 64 65 73 20 lock precludes
572e0 61 6c 6c 20 6f 74 68 65 72 20 6c 6f 63 6b 73 2e all other locks.
572f0 0a 2a 2a 0a 2a 2a 20 50 45 4e 44 49 4e 47 5f 4c .**.** PENDING_L
57300 4f 43 4b 20 6d 61 79 20 6e 6f 74 20 62 65 20 70 OCK may not be p
57310 61 73 73 65 64 20 64 69 72 65 63 74 6c 79 20 74 assed directly t
57320 6f 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 o sqlite3OsLock(
57330 29 2e 20 49 6e 73 74 65 61 64 2c 20 61 0a 2a 2a ). Instead, a.**
57340 20 70 72 6f 63 65 73 73 20 74 68 61 74 20 72 65 process that re
57350 71 75 65 73 74 73 20 61 6e 20 45 58 43 4c 55 53 quests an EXCLUS
57360 49 56 45 20 6c 6f 63 6b 20 6d 61 79 20 61 63 74 IVE lock may act
57370 75 61 6c 6c 79 20 6f 62 74 61 69 6e 20 61 20 50 ually obtain a P
57380 45 4e 44 49 4e 47 0a 2a 2a 20 6c 6f 63 6b 2e 20 ENDING.** lock.
57390 54 68 69 73 20 63 61 6e 20 62 65 20 75 70 67 72 This can be upgr
573a0 61 64 65 64 20 74 6f 20 61 6e 20 45 58 43 4c 55 aded to an EXCLU
573b0 53 49 56 45 20 6c 6f 63 6b 20 62 79 20 61 20 73 SIVE lock by a s
573c0 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 20 74 ubsequent call t
573d0 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 4f 73 4c 6f o.** sqlite3OsLo
573e0 63 6b 28 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 ck()..*/.#define
573f0 20 4e 4f 5f 4c 4f 43 4b 20 20 20 20 20 20 20 20 NO_LOCK
57400 20 30 0a 23 64 65 66 69 6e 65 20 53 48 41 52 45 0.#define SHARE
57410 44 5f 4c 4f 43 4b 20 20 20 20 20 31 0a 23 64 65 D_LOCK 1.#de
57420 66 69 6e 65 20 52 45 53 45 52 56 45 44 5f 4c 4f fine RESERVED_LO
57430 43 4b 20 20 20 32 0a 23 64 65 66 69 6e 65 20 50 CK 2.#define P
57440 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 20 20 20 33 ENDING_LOCK 3
57450 0a 23 64 65 66 69 6e 65 20 45 58 43 4c 55 53 49 .#define EXCLUSI
57460 56 45 5f 4c 4f 43 4b 20 20 34 0a 0a 2f 2a 0a 2a VE_LOCK 4../*.*
57470 2a 20 46 69 6c 65 20 4c 6f 63 6b 69 6e 67 20 4e * File Locking N
57480 6f 74 65 73 3a 20 20 28 4d 6f 73 74 6c 79 20 61 otes: (Mostly a
57490 62 6f 75 74 20 77 69 6e 64 6f 77 73 20 62 75 74 bout windows but
574a0 20 61 6c 73 6f 20 73 6f 6d 65 20 69 6e 66 6f 20 also some info
574b0 66 6f 72 20 55 6e 69 78 29 0a 2a 2a 0a 2a 2a 20 for Unix).**.**
574c0 57 65 20 63 61 6e 6e 6f 74 20 75 73 65 20 4c 6f We cannot use Lo
574d0 63 6b 46 69 6c 65 45 78 28 29 20 6f 72 20 55 6e ckFileEx() or Un
574e0 6c 6f 63 6b 46 69 6c 65 45 78 28 29 20 6f 6e 20 lockFileEx() on
574f0 57 69 6e 39 35 2f 39 38 2f 4d 45 20 62 65 63 61 Win95/98/ME beca
57500 75 73 65 0a 2a 2a 20 74 68 6f 73 65 20 66 75 6e use.** those fun
57510 63 74 69 6f 6e 73 20 61 72 65 20 6e 6f 74 20 61 ctions are not a
57520 76 61 69 6c 61 62 6c 65 2e 20 20 53 6f 20 77 65 vailable. So we
57530 20 75 73 65 20 6f 6e 6c 79 20 4c 6f 63 6b 46 69 use only LockFi
57540 6c 65 28 29 20 61 6e 64 0a 2a 2a 20 55 6e 6c 6f le() and.** Unlo
57550 63 6b 46 69 6c 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 ckFile()..**.**
57560 4c 6f 63 6b 46 69 6c 65 28 29 20 70 72 65 76 65 LockFile() preve
57570 6e 74 73 20 6e 6f 74 20 6a 75 73 74 20 77 72 69 nts not just wri
57580 74 69 6e 67 20 62 75 74 20 61 6c 73 6f 20 72 65 ting but also re
57590 61 64 69 6e 67 20 62 79 20 6f 74 68 65 72 20 70 ading by other p
575a0 72 6f 63 65 73 73 65 73 2e 0a 2a 2a 20 41 20 53 rocesses..** A S
575b0 48 41 52 45 44 5f 4c 4f 43 4b 20 69 73 20 6f 62 HARED_LOCK is ob
575c0 74 61 69 6e 65 64 20 62 79 20 6c 6f 63 6b 69 6e tained by lockin
575d0 67 20 61 20 73 69 6e 67 6c 65 20 72 61 6e 64 6f g a single rando
575e0 6d 6c 79 2d 63 68 6f 73 65 6e 20 0a 2a 2a 20 62 mly-chosen .** b
575f0 79 74 65 20 6f 75 74 20 6f 66 20 61 20 73 70 65 yte out of a spe
57600 63 69 66 69 63 20 72 61 6e 67 65 20 6f 66 20 62 cific range of b
57610 79 74 65 73 2e 20 54 68 65 20 6c 6f 63 6b 20 62 ytes. The lock b
57620 79 74 65 20 69 73 20 6f 62 74 61 69 6e 65 64 20 yte is obtained
57630 61 74 20 0a 2a 2a 20 72 61 6e 64 6f 6d 20 73 6f at .** random so
57640 20 74 77 6f 20 73 65 70 61 72 61 74 65 20 72 65 two separate re
57650 61 64 65 72 73 20 63 61 6e 20 70 72 6f 62 61 62 aders can probab
57660 6c 79 20 61 63 63 65 73 73 20 74 68 65 20 66 69 ly access the fi
57670 6c 65 20 61 74 20 74 68 65 20 0a 2a 2a 20 73 61 le at the .** sa
57680 6d 65 20 74 69 6d 65 2c 20 75 6e 6c 65 73 73 20 me time, unless
57690 74 68 65 79 20 61 72 65 20 75 6e 6c 75 63 6b 79 they are unlucky
576a0 20 61 6e 64 20 63 68 6f 6f 73 65 20 74 68 65 20 and choose the
576b0 73 61 6d 65 20 6c 6f 63 6b 20 62 79 74 65 2e 0a same lock byte..
576c0 2a 2a 20 41 6e 20 45 58 43 4c 55 53 49 56 45 5f ** An EXCLUSIVE_
576d0 4c 4f 43 4b 20 69 73 20 6f 62 74 61 69 6e 65 64 LOCK is obtained
576e0 20 62 79 20 6c 6f 63 6b 69 6e 67 20 61 6c 6c 20 by locking all
576f0 62 79 74 65 73 20 69 6e 20 74 68 65 20 72 61 6e bytes in the ran
57700 67 65 2e 0a 2a 2a 20 54 68 65 72 65 20 63 61 6e ge..** There can
57710 20 6f 6e 6c 79 20 62 65 20 6f 6e 65 20 77 72 69 only be one wri
57720 74 65 72 2e 20 20 41 20 52 45 53 45 52 56 45 44 ter. A RESERVED
57730 5f 4c 4f 43 4b 20 69 73 20 6f 62 74 61 69 6e 65 _LOCK is obtaine
57740 64 20 62 79 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 d by locking.**
57750 61 20 73 69 6e 67 6c 65 20 62 79 74 65 20 6f 66 a single byte of
57760 20 74 68 65 20 66 69 6c 65 20 74 68 61 74 20 69 the file that i
57770 73 20 64 65 73 69 67 6e 61 74 65 64 20 61 73 20 s designated as
57780 74 68 65 20 72 65 73 65 72 76 65 64 20 6c 6f 63 the reserved loc
57790 6b 20 62 79 74 65 2e 0a 2a 2a 20 41 20 50 45 4e k byte..** A PEN
577a0 44 49 4e 47 5f 4c 4f 43 4b 20 69 73 20 6f 62 74 DING_LOCK is obt
577b0 61 69 6e 65 64 20 62 79 20 6c 6f 63 6b 69 6e 67 ained by locking
577c0 20 61 20 64 65 73 69 67 6e 61 74 65 64 20 62 79 a designated by
577d0 74 65 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f te different fro
577e0 6d 0a 2a 2a 20 74 68 65 20 52 45 53 45 52 56 45 m.** the RESERVE
577f0 44 5f 4c 4f 43 4b 20 62 79 74 65 2e 0a 2a 2a 0a D_LOCK byte..**.
57800 2a 2a 20 4f 6e 20 57 69 6e 4e 54 2f 32 4b 2f 58 ** On WinNT/2K/X
57810 50 20 73 79 73 74 65 6d 73 2c 20 4c 6f 63 6b 46 P systems, LockF
57820 69 6c 65 45 78 28 29 20 61 6e 64 20 55 6e 6c 6f ileEx() and Unlo
57830 63 6b 46 69 6c 65 45 78 28 29 20 61 72 65 20 61 ckFileEx() are a
57840 76 61 69 6c 61 62 6c 65 2c 0a 2a 2a 20 77 68 69 vailable,.** whi
57850 63 68 20 6d 65 61 6e 73 20 77 65 20 63 61 6e 20 ch means we can
57860 75 73 65 20 72 65 61 64 65 72 2f 77 72 69 74 65 use reader/write
57870 72 20 6c 6f 63 6b 73 2e 20 20 57 68 65 6e 20 72 r locks. When r
57880 65 61 64 65 72 2f 77 72 69 74 65 72 20 6c 6f 63 eader/writer loc
57890 6b 73 0a 2a 2a 20 61 72 65 20 75 73 65 64 2c 20 ks.** are used,
578a0 74 68 65 20 6c 6f 63 6b 20 69 73 20 70 6c 61 63 the lock is plac
578b0 65 64 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 72 ed on the same r
578c0 61 6e 67 65 20 6f 66 20 62 79 74 65 73 20 74 68 ange of bytes th
578d0 61 74 20 69 73 20 75 73 65 64 0a 2a 2a 20 66 6f at is used.** fo
578e0 72 20 70 72 6f 62 61 62 69 6c 69 73 74 69 63 20 r probabilistic
578f0 6c 6f 63 6b 69 6e 67 20 69 6e 20 57 69 6e 39 35 locking in Win95
57900 2f 39 38 2f 4d 45 2e 20 20 48 65 6e 63 65 2c 20 /98/ME. Hence,
57910 74 68 65 20 6c 6f 63 6b 69 6e 67 20 73 63 68 65 the locking sche
57920 6d 65 0a 2a 2a 20 77 69 6c 6c 20 73 75 70 70 6f me.** will suppo
57930 72 74 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 57 rt two or more W
57940 69 6e 39 35 20 72 65 61 64 65 72 73 20 6f 72 20 in95 readers or
57950 74 77 6f 20 6f 72 20 6d 6f 72 65 20 57 69 6e 4e two or more WinN
57960 54 20 72 65 61 64 65 72 73 2e 0a 2a 2a 20 42 75 T readers..** Bu
57970 74 20 61 20 73 69 6e 67 6c 65 20 57 69 6e 39 35 t a single Win95
57980 20 72 65 61 64 65 72 20 77 69 6c 6c 20 6c 6f 63 reader will loc
57990 6b 20 6f 75 74 20 61 6c 6c 20 57 69 6e 4e 54 20 k out all WinNT
579a0 72 65 61 64 65 72 73 20 61 6e 64 20 61 20 73 69 readers and a si
579b0 6e 67 6c 65 0a 2a 2a 20 57 69 6e 4e 54 20 72 65 ngle.** WinNT re
579c0 61 64 65 72 20 77 69 6c 6c 20 6c 6f 63 6b 20 6f ader will lock o
579d0 75 74 20 61 6c 6c 20 6f 74 68 65 72 20 57 69 6e ut all other Win
579e0 39 35 20 72 65 61 64 65 72 73 2e 0a 2a 2a 0a 2a 95 readers..**.*
579f0 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * The following
57a00 23 64 65 66 69 6e 65 73 20 73 70 65 63 69 66 79 #defines specify
57a10 20 74 68 65 20 72 61 6e 67 65 20 6f 66 20 62 79 the range of by
57a20 74 65 73 20 75 73 65 64 20 66 6f 72 20 6c 6f 63 tes used for loc
57a30 6b 69 6e 67 2e 0a 2a 2a 20 53 48 41 52 45 44 5f king..** SHARED_
57a40 53 49 5a 45 20 69 73 20 74 68 65 20 6e 75 6d 62 SIZE is the numb
57a50 65 72 20 6f 66 20 62 79 74 65 73 20 61 76 61 69 er of bytes avai
57a60 6c 61 62 6c 65 20 69 6e 20 74 68 65 20 70 6f 6f lable in the poo
57a70 6c 20 66 72 6f 6d 20 77 68 69 63 68 0a 2a 2a 20 l from which.**
57a80 61 20 72 61 6e 64 6f 6d 20 62 79 74 65 20 69 73 a random byte is
57a90 20 73 65 6c 65 63 74 65 64 20 66 6f 72 20 61 20 selected for a
57aa0 73 68 61 72 65 64 20 6c 6f 63 6b 2e 20 20 54 68 shared lock. Th
57ab0 65 20 70 6f 6f 6c 20 6f 66 20 62 79 74 65 73 20 e pool of bytes
57ac0 66 6f 72 0a 2a 2a 20 73 68 61 72 65 64 20 6c 6f for.** shared lo
57ad0 63 6b 73 20 62 65 67 69 6e 73 20 61 74 20 53 48 cks begins at SH
57ae0 41 52 45 44 5f 46 49 52 53 54 2e 20 0a 2a 2a 0a ARED_FIRST. .**.
57af0 2a 2a 20 54 68 65 20 73 61 6d 65 20 6c 6f 63 6b ** The same lock
57b00 69 6e 67 20 73 74 72 61 74 65 67 79 20 61 6e 64 ing strategy and
57b10 0a 2a 2a 20 62 79 74 65 20 72 61 6e 67 65 73 20 .** byte ranges
57b20 61 72 65 20 75 73 65 64 20 66 6f 72 20 55 6e 69 are used for Uni
57b30 78 2e 20 20 54 68 69 73 20 6c 65 61 76 65 73 20 x. This leaves
57b40 6f 70 65 6e 20 74 68 65 20 70 6f 73 73 69 62 6c open the possibl
57b50 69 74 79 20 6f 66 20 68 61 76 69 6e 67 0a 2a 2a ity of having.**
57b60 20 63 6c 69 65 6e 74 73 20 6f 6e 20 77 69 6e 39 clients on win9
57b70 35 2c 20 77 69 6e 4e 54 2c 20 61 6e 64 20 75 6e 5, winNT, and un
57b80 69 78 20 61 6c 6c 20 74 61 6c 6b 69 6e 67 20 74 ix all talking t
57b90 6f 20 74 68 65 20 73 61 6d 65 20 73 68 61 72 65 o the same share
57ba0 64 20 66 69 6c 65 0a 2a 2a 20 61 6e 64 20 61 6c d file.** and al
57bb0 6c 20 6c 6f 63 6b 69 6e 67 20 63 6f 72 72 65 63 l locking correc
57bc0 74 6c 79 2e 20 20 54 6f 20 64 6f 20 73 6f 20 77 tly. To do so w
57bd0 6f 75 6c 64 20 72 65 71 75 69 72 65 20 74 68 61 ould require tha
57be0 74 20 73 61 6d 62 61 20 28 6f 72 20 77 68 61 74 t samba (or what
57bf0 65 76 65 72 0a 2a 2a 20 74 6f 6f 6c 20 69 73 20 ever.** tool is
57c00 62 65 69 6e 67 20 75 73 65 64 20 66 6f 72 20 66 being used for f
57c10 69 6c 65 20 73 68 61 72 69 6e 67 29 20 69 6d 70 ile sharing) imp
57c20 6c 65 6d 65 6e 74 73 20 6c 6f 63 6b 73 20 63 6f lements locks co
57c30 72 72 65 63 74 6c 79 20 62 65 74 77 65 65 6e 0a rrectly between.
57c40 2a 2a 20 77 69 6e 64 6f 77 73 20 61 6e 64 20 75 ** windows and u
57c50 6e 69 78 2e 20 20 49 27 6d 20 67 75 65 73 73 69 nix. I'm guessi
57c60 6e 67 20 74 68 61 74 20 69 73 6e 27 74 20 6c 69 ng that isn't li
57c70 6b 65 6c 79 20 74 6f 20 68 61 70 70 65 6e 2c 20 kely to happen,
57c80 62 75 74 20 62 79 0a 2a 2a 20 75 73 69 6e 67 20 but by.** using
57c90 74 68 65 20 73 61 6d 65 20 6c 6f 63 6b 69 6e 67 the same locking
57ca0 20 72 61 6e 67 65 20 77 65 20 61 72 65 20 61 74 range we are at
57cb0 20 6c 65 61 73 74 20 6f 70 65 6e 20 74 6f 20 74 least open to t
57cc0 68 65 20 70 6f 73 73 69 62 69 6c 69 74 79 2e 0a he possibility..
57cd0 2a 2a 0a 2a 2a 20 4c 6f 63 6b 69 6e 67 20 69 6e **.** Locking in
57ce0 20 77 69 6e 64 6f 77 73 20 69 73 20 6d 61 6e 64 windows is mand
57cf0 69 74 6f 72 79 2e 20 20 46 6f 72 20 74 68 69 73 itory. For this
57d00 20 72 65 61 73 6f 6e 2c 20 77 65 20 63 61 6e 6e reason, we cann
57d10 6f 74 20 73 74 6f 72 65 0a 2a 2a 20 61 63 74 75 ot store.** actu
57d20 61 6c 20 64 61 74 61 20 69 6e 20 74 68 65 20 62 al data in the b
57d30 79 74 65 73 20 75 73 65 64 20 66 6f 72 20 6c 6f ytes used for lo
57d40 63 6b 69 6e 67 2e 20 20 54 68 65 20 70 61 67 65 cking. The page
57d50 72 20 6e 65 76 65 72 20 61 6c 6c 6f 63 61 74 65 r never allocate
57d60 73 0a 2a 2a 20 74 68 65 20 70 61 67 65 73 20 69 s.** the pages i
57d70 6e 76 6f 6c 76 65 64 20 69 6e 20 6c 6f 63 6b 69 nvolved in locki
57d80 6e 67 20 74 68 65 72 65 66 6f 72 65 2e 20 20 53 ng therefore. S
57d90 48 41 52 45 44 5f 53 49 5a 45 20 69 73 20 73 65 HARED_SIZE is se
57da0 6c 65 63 74 65 64 20 73 6f 0a 2a 2a 20 74 68 61 lected so.** tha
57db0 74 20 61 6c 6c 20 6c 6f 63 6b 73 20 77 69 6c 6c t all locks will
57dc0 20 66 69 74 20 6f 6e 20 61 20 73 69 6e 67 6c 65 fit on a single
57dd0 20 70 61 67 65 20 65 76 65 6e 20 61 74 20 74 68 page even at th
57de0 65 20 6d 69 6e 69 6d 75 6d 20 70 61 67 65 20 73 e minimum page s
57df0 69 7a 65 2e 0a 2a 2a 20 50 45 4e 44 49 4e 47 5f ize..** PENDING_
57e00 42 59 54 45 20 64 65 66 69 6e 65 73 20 74 68 65 BYTE defines the
57e10 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 beginning of th
57e20 65 20 6c 6f 63 6b 73 2e 20 20 42 79 20 64 65 66 e locks. By def
57e30 61 75 6c 74 20 50 45 4e 44 49 4e 47 5f 42 59 54 ault PENDING_BYT
57e40 45 0a 2a 2a 20 69 73 20 73 65 74 20 68 69 67 68 E.** is set high
57e50 20 73 6f 20 74 68 61 74 20 77 65 20 64 6f 6e 27 so that we don'
57e60 74 20 68 61 76 65 20 74 6f 20 61 6c 6c 6f 63 61 t have to alloca
57e70 74 65 20 61 6e 20 75 6e 75 73 65 64 20 70 61 67 te an unused pag
57e80 65 20 65 78 63 65 70 74 0a 2a 2a 20 66 6f 72 20 e except.** for
57e90 76 65 72 79 20 6c 61 72 67 65 20 64 61 74 61 62 very large datab
57ea0 61 73 65 73 2e 20 20 42 75 74 20 6f 6e 65 20 73 ases. But one s
57eb0 68 6f 75 6c 64 20 74 65 73 74 20 74 68 65 20 70 hould test the p
57ec0 61 67 65 20 73 6b 69 70 70 69 6e 67 20 6c 6f 67 age skipping log
57ed0 69 63 20 0a 2a 2a 20 62 79 20 73 65 74 74 69 6e ic .** by settin
57ee0 67 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 6c g PENDING_BYTE l
57ef0 6f 77 20 61 6e 64 20 72 75 6e 6e 69 6e 67 20 74 ow and running t
57f00 68 65 20 65 6e 74 69 72 65 20 72 65 67 72 65 73 he entire regres
57f10 73 69 6f 6e 20 73 75 69 74 65 2e 0a 2a 2a 0a 2a sion suite..**.*
57f20 2a 20 43 68 61 6e 67 69 6e 67 20 74 68 65 20 76 * Changing the v
57f30 61 6c 75 65 20 6f 66 20 50 45 4e 44 49 4e 47 5f alue of PENDING_
57f40 42 59 54 45 20 72 65 73 75 6c 74 73 20 69 6e 20 BYTE results in
57f50 61 20 73 75 62 74 6c 79 20 69 6e 63 6f 6d 70 61 a subtly incompa
57f60 74 69 62 6c 65 0a 2a 2a 20 66 69 6c 65 20 66 6f tible.** file fo
57f70 72 6d 61 74 2e 20 20 44 65 70 65 6e 64 69 6e 67 rmat. Depending
57f80 20 6f 6e 20 68 6f 77 20 69 74 20 69 73 20 63 68 on how it is ch
57f90 61 6e 67 65 64 2c 20 79 6f 75 20 6d 69 67 68 74 anged, you might
57fa0 20 6e 6f 74 20 6e 6f 74 69 63 65 0a 2a 2a 20 74 not notice.** t
57fb0 68 65 20 69 6e 63 6f 6d 70 61 74 69 62 69 6c 69 he incompatibili
57fc0 74 79 20 72 69 67 68 74 20 61 77 61 79 2c 20 65 ty right away, e
57fd0 76 65 6e 20 72 75 6e 6e 69 6e 67 20 61 20 66 75 ven running a fu
57fe0 6c 6c 20 72 65 67 72 65 73 73 69 6f 6e 20 74 65 ll regression te
57ff0 73 74 2e 0a 2a 2a 20 54 68 65 20 64 65 66 61 75 st..** The defau
58000 6c 74 20 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 50 lt location of P
58010 45 4e 44 49 4e 47 5f 42 59 54 45 20 69 73 20 74 ENDING_BYTE is t
58020 68 65 20 66 69 72 73 74 20 62 79 74 65 20 70 61 he first byte pa
58030 73 74 20 74 68 65 0a 2a 2a 20 31 47 42 20 62 6f st the.** 1GB bo
58040 75 6e 64 61 72 79 2e 0a 2a 2a 0a 2a 2f 0a 23 64 undary..**.*/.#d
58050 65 66 69 6e 65 20 50 45 4e 44 49 4e 47 5f 42 59 efine PENDING_BY
58060 54 45 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 TE sqlite3P
58070 65 6e 64 69 6e 67 42 79 74 65 0a 23 64 65 66 69 endingByte.#defi
58080 6e 65 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 ne RESERVED_BYTE
58090 20 20 20 20 20 28 50 45 4e 44 49 4e 47 5f 42 59 (PENDING_BY
580a0 54 45 2b 31 29 0a 23 64 65 66 69 6e 65 20 53 48 TE+1).#define SH
580b0 41 52 45 44 5f 46 49 52 53 54 20 20 20 20 20 20 ARED_FIRST
580c0 28 50 45 4e 44 49 4e 47 5f 42 59 54 45 2b 32 29 (PENDING_BYTE+2)
580d0 0a 23 64 65 66 69 6e 65 20 53 48 41 52 45 44 5f .#define SHARED_
580e0 53 49 5a 45 20 20 20 20 20 20 20 35 31 30 0a 0a SIZE 510..
580f0 2f 2a 0a 2a 2a 20 57 72 61 70 70 65 72 20 61 72 /*.** Wrapper ar
58100 6f 75 6e 64 20 4f 53 20 73 70 65 63 69 66 69 63 ound OS specific
58110 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 sqlite3_os_init
58120 28 29 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a () function..*/.
58130 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
58140 6e 74 20 73 71 6c 69 74 65 33 4f 73 49 6e 69 74 nt sqlite3OsInit
58150 28 76 6f 69 64 29 3b 0a 0a 2f 2a 20 0a 2a 2a 20 (void);../* .**
58160 46 75 6e 63 74 69 6f 6e 73 20 66 6f 72 20 61 63 Functions for ac
58170 63 65 73 73 69 6e 67 20 73 71 6c 69 74 65 33 5f cessing sqlite3_
58180 66 69 6c 65 20 6d 65 74 68 6f 64 73 20 0a 2a 2f file methods .*/
58190 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
581a0 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f int sqlite3OsClo
581b0 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a se(sqlite3_file*
581c0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
581d0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 52 E int sqlite3OsR
581e0 65 61 64 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ead(sqlite3_file
581f0 2a 2c 20 76 6f 69 64 2a 2c 20 69 6e 74 20 61 6d *, void*, int am
58200 74 2c 20 69 36 34 20 6f 66 66 73 65 74 29 3b 0a t, i64 offset);.
58210 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
58220 6e 74 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 nt sqlite3OsWrit
58230 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c e(sqlite3_file*,
58240 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e const void*, in
58250 74 20 61 6d 74 2c 20 69 36 34 20 6f 66 66 73 65 t amt, i64 offse
58260 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
58270 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 TE int sqlite3Os
58280 54 72 75 6e 63 61 74 65 28 73 71 6c 69 74 65 33 Truncate(sqlite3
58290 5f 66 69 6c 65 2a 2c 20 69 36 34 20 73 69 7a 65 _file*, i64 size
582a0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
582b0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 53 E int sqlite3OsS
582c0 79 6e 63 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ync(sqlite3_file
582d0 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f *, int);.SQLITE_
582e0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
582f0 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 73 71 te3OsFileSize(sq
58300 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 36 34 lite3_file*, i64
58310 20 2a 70 53 69 7a 65 29 3b 0a 53 51 4c 49 54 45 *pSize);.SQLITE
58320 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
58330 69 74 65 33 4f 73 4c 6f 63 6b 28 73 71 6c 69 74 ite3OsLock(sqlit
58340 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a e3_file*, int);.
58350 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
58360 6e 74 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f nt sqlite3OsUnlo
58370 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a ck(sqlite3_file*
58380 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
58390 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
583a0 65 33 4f 73 43 68 65 63 6b 52 65 73 65 72 76 65 e3OsCheckReserve
583b0 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 dLock(sqlite3_fi
583c0 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 52 65 le *id, int *pRe
583d0 73 4f 75 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 sOut);.SQLITE_PR
583e0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
583f0 33 4f 73 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 73 3OsFileControl(s
58400 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 69 6e 74 qlite3_file*,int
58410 2c 76 6f 69 64 2a 29 3b 0a 23 64 65 66 69 6e 65 ,void*);.#define
58420 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 44 42 SQLITE_FCNTL_DB
58430 5f 55 4e 43 48 41 4e 47 45 44 20 30 78 63 61 30 _UNCHANGED 0xca0
58440 39 33 66 61 30 0a 53 51 4c 49 54 45 5f 50 52 49 93fa0.SQLITE_PRI
58450 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
58460 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 73 71 6c OsSectorSize(sql
58470 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 3b 0a ite3_file *id);.
58480 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
58490 6e 74 20 73 71 6c 69 74 65 33 4f 73 44 65 76 69 nt sqlite3OsDevi
584a0 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63 ceCharacteristic
584b0 73 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a s(sqlite3_file *
584c0 69 64 29 3b 0a 0a 2f 2a 20 0a 2a 2a 20 46 75 6e id);../* .** Fun
584d0 63 74 69 6f 6e 73 20 66 6f 72 20 61 63 63 65 73 ctions for acces
584e0 73 69 6e 67 20 73 71 6c 69 74 65 33 5f 76 66 73 sing sqlite3_vfs
584f0 20 6d 65 74 68 6f 64 73 20 0a 2a 2f 0a 53 51 4c methods .*/.SQL
58500 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
58510 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 73 71 sqlite3OsOpen(sq
58520 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e lite3_vfs *, con
58530 73 74 20 63 68 61 72 20 2a 2c 20 73 71 6c 69 74 st char *, sqlit
58540 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 2c 20 69 e3_file*, int, i
58550 6e 74 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 nt *);.SQLITE_PR
58560 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
58570 33 4f 73 44 65 6c 65 74 65 28 73 71 6c 69 74 65 3OsDelete(sqlite
58580 33 5f 76 66 73 20 2a 2c 20 63 6f 6e 73 74 20 63 3_vfs *, const c
58590 68 61 72 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c har *, int);.SQL
585a0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
585b0 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73 73 28 sqlite3OsAccess(
585c0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 63 sqlite3_vfs *, c
585d0 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 onst char *, int
585e0 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 3b , int *pResOut);
585f0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
58600 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 46 75 6c int sqlite3OsFul
58610 6c 50 61 74 68 6e 61 6d 65 28 73 71 6c 69 74 65 lPathname(sqlite
58620 33 5f 76 66 73 20 2a 2c 20 63 6f 6e 73 74 20 63 3_vfs *, const c
58630 68 61 72 20 2a 2c 20 69 6e 74 2c 20 63 68 61 72 har *, int, char
58640 20 2a 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c *);.#ifndef SQL
58650 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 ITE_OMIT_LOAD_EX
58660 54 45 4e 53 49 4f 4e 0a 53 51 4c 49 54 45 5f 50 TENSION.SQLITE_P
58670 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c RIVATE void *sql
58680 69 74 65 33 4f 73 44 6c 4f 70 65 6e 28 73 71 6c ite3OsDlOpen(sql
58690 69 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e 73 ite3_vfs *, cons
586a0 74 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c 49 54 t char *);.SQLIT
586b0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
586c0 71 6c 69 74 65 33 4f 73 44 6c 45 72 72 6f 72 28 qlite3OsDlError(
586d0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 69 sqlite3_vfs *, i
586e0 6e 74 2c 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c nt, char *);.SQL
586f0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
58700 20 28 2a 73 71 6c 69 74 65 33 4f 73 44 6c 53 79 (*sqlite3OsDlSy
58710 6d 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c m(sqlite3_vfs *,
58720 20 76 6f 69 64 20 2a 2c 20 63 6f 6e 73 74 20 63 void *, const c
58730 68 61 72 20 2a 29 29 28 76 6f 69 64 29 3b 0a 53 har *))(void);.S
58740 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
58750 69 64 20 73 71 6c 69 74 65 33 4f 73 44 6c 43 6c id sqlite3OsDlCl
58760 6f 73 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 ose(sqlite3_vfs
58770 2a 2c 20 76 6f 69 64 20 2a 29 3b 0a 23 65 6e 64 *, void *);.#end
58780 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 if /* SQLITE_OMI
58790 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e T_LOAD_EXTENSION
587a0 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 */.SQLITE_PRIVA
587b0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 TE int sqlite3Os
587c0 52 61 6e 64 6f 6d 6e 65 73 73 28 73 71 6c 69 74 Randomness(sqlit
587d0 65 33 5f 76 66 73 20 2a 2c 20 69 6e 74 2c 20 63 e3_vfs *, int, c
587e0 68 61 72 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 har *);.SQLITE_P
587f0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
58800 65 33 4f 73 53 6c 65 65 70 28 73 71 6c 69 74 65 e3OsSleep(sqlite
58810 33 5f 76 66 73 20 2a 2c 20 69 6e 74 29 3b 0a 53 3_vfs *, int);.S
58820 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
58830 74 20 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65 t sqlite3OsCurre
58840 6e 74 54 69 6d 65 28 73 71 6c 69 74 65 33 5f 76 ntTime(sqlite3_v
58850 66 73 20 2a 2c 20 64 6f 75 62 6c 65 2a 29 3b 0a fs *, double*);.
58860 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 6e 69 65 6e ./*.** Convenien
58870 63 65 20 66 75 6e 63 74 69 6f 6e 73 20 66 6f 72 ce functions for
58880 20 6f 70 65 6e 69 6e 67 20 61 6e 64 20 63 6c 6f opening and clo
58890 73 69 6e 67 20 66 69 6c 65 73 20 75 73 69 6e 67 sing files using
588a0 20 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 61 6c .** sqlite3_mal
588b0 6c 6f 63 28 29 20 74 6f 20 6f 62 74 61 69 6e 20 loc() to obtain
588c0 73 70 61 63 65 20 66 6f 72 20 74 68 65 20 66 69 space for the fi
588d0 6c 65 2d 68 61 6e 64 6c 65 20 73 74 72 75 63 74 le-handle struct
588e0 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ure..*/.SQLITE_P
588f0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
58900 65 33 4f 73 4f 70 65 6e 4d 61 6c 6c 6f 63 28 73 e3OsOpenMalloc(s
58910 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f qlite3_vfs *, co
58920 6e 73 74 20 63 68 61 72 20 2a 2c 20 73 71 6c 69 nst char *, sqli
58930 74 65 33 5f 66 69 6c 65 20 2a 2a 2c 20 69 6e 74 te3_file **, int
58940 2c 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 ,int*);.SQLITE_P
58950 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
58960 65 33 4f 73 43 6c 6f 73 65 46 72 65 65 28 73 71 e3OsCloseFree(sq
58970 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 3b 0a 0a lite3_file *);..
58980 23 65 6e 64 69 66 20 2f 2a 20 5f 53 51 4c 49 54 #endif /* _SQLIT
58990 45 5f 4f 53 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a E_OS_H_ */../***
589a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
589b0 6f 66 20 6f 73 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a of os.h ********
589c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
589d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
589e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
589f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 *********** Cont
58a00 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 inuing where we
58a10 6c 65 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 left off in sqli
58a20 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a teInt.h ********
58a30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
58a40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c *********** Incl
58a50 75 64 65 20 6d 75 74 65 78 2e 68 20 69 6e 20 74 ude mutex.h in t
58a60 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c he middle of sql
58a70 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a iteInt.h *******
58a80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
58a90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
58aa0 6e 20 66 69 6c 65 20 6d 75 74 65 78 2e 68 20 2a n file mutex.h *
58ab0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58ac0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58ad0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a **********/./*.*
58ae0 2a 20 32 30 30 37 20 41 75 67 75 73 74 20 32 38 * 2007 August 28
58af0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
58b00 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
58b10 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
58b20 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
58b30 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
58b40 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
58b50 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
58b60 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
58b70 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
58b80 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
58b90 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
58ba0 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
58bb0 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
58bc0 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
58bd0 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
58be0 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
58bf0 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
58c00 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
58c10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58c20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58c30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58c40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
58c50 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 **.** This file
58c60 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 63 6f 6d contains the com
58c70 6d 6f 6e 20 68 65 61 64 65 72 20 66 6f 72 20 61 mon header for a
58c80 6c 6c 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 ll mutex impleme
58c90 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2a 20 54 68 65 ntations..** The
58ca0 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 68 65 61 sqliteInt.h hea
58cb0 64 65 72 20 23 69 6e 63 6c 75 64 65 73 20 74 68 der #includes th
58cc0 69 73 20 66 69 6c 65 20 73 6f 20 74 68 61 74 20 is file so that
58cd0 69 74 20 69 73 20 61 76 61 69 6c 61 62 6c 65 0a it is available.
58ce0 2a 2a 20 74 6f 20 61 6c 6c 20 73 6f 75 72 63 65 ** to all source
58cf0 20 66 69 6c 65 73 2e 20 20 57 65 20 62 72 65 61 files. We brea
58d00 6b 20 69 74 20 6f 75 74 20 69 6e 20 61 6e 20 65 k it out in an e
58d10 66 66 6f 72 74 20 74 6f 20 6b 65 65 70 20 74 68 ffort to keep th
58d20 65 20 63 6f 64 65 0a 2a 2a 20 62 65 74 74 65 72 e code.** better
58d30 20 6f 72 67 61 6e 69 7a 65 64 2e 0a 2a 2a 0a 2a organized..**.*
58d40 2a 20 4e 4f 54 45 3a 20 20 73 6f 75 72 63 65 20 * NOTE: source
58d50 66 69 6c 65 73 20 73 68 6f 75 6c 64 20 2a 6e 6f files should *no
58d60 74 2a 20 23 69 6e 63 6c 75 64 65 20 74 68 69 73 t* #include this
58d70 20 68 65 61 64 65 72 20 66 69 6c 65 20 64 69 72 header file dir
58d80 65 63 74 6c 79 2e 0a 2a 2a 20 53 6f 75 72 63 65 ectly..** Source
58d90 20 66 69 6c 65 73 20 73 68 6f 75 6c 64 20 23 69 files should #i
58da0 6e 63 6c 75 64 65 20 74 68 65 20 73 71 6c 69 74 nclude the sqlit
58db0 65 49 6e 74 2e 68 20 66 69 6c 65 20 61 6e 64 20 eInt.h file and
58dc0 6c 65 74 20 74 68 61 74 20 66 69 6c 65 0a 2a 2a let that file.**
58dd0 20 69 6e 63 6c 75 64 65 20 74 68 69 73 20 6f 6e include this on
58de0 65 20 69 6e 64 69 72 65 63 74 6c 79 2e 0a 2a 2a e indirectly..**
58df0 0a 2a 2a 20 24 49 64 3a 20 6d 75 74 65 78 2e 68 .** $Id: mutex.h
58e00 2c 76 20 31 2e 39 20 32 30 30 38 2f 31 30 2f 30 ,v 1.9 2008/10/0
58e10 37 20 31 35 3a 32 35 3a 34 38 20 64 72 68 20 45 7 15:25:48 drh E
58e20 78 70 20 24 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 xp $.*/.../*.**
58e30 46 69 67 75 72 65 20 6f 75 74 20 77 68 61 74 20 Figure out what
58e40 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 63 version of the c
58e50 6f 64 65 20 74 6f 20 75 73 65 2e 20 20 54 68 65 ode to use. The
58e60 20 63 68 6f 69 63 65 73 20 61 72 65 0a 2a 2a 0a choices are.**.
58e70 2a 2a 20 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 ** SQLITE_MUTE
58e80 58 5f 4f 4d 49 54 20 20 20 20 20 20 20 20 20 4e X_OMIT N
58e90 6f 20 6d 75 74 65 78 20 6c 6f 67 69 63 2e 20 20 o mutex logic.
58ea0 4e 6f 74 20 65 76 65 6e 20 73 74 75 62 73 2e 20 Not even stubs.
58eb0 20 54 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 The.**
58ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58ed0 20 20 20 20 6d 75 74 65 78 65 73 20 69 6d 70 6c mutexes impl
58ee0 65 6d 65 6e 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 emention cannot
58ef0 62 65 20 6f 76 65 72 72 69 64 64 65 6e 0a 2a 2a be overridden.**
58f00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58f10 20 20 20 20 20 20 20 20 20 20 20 20 20 61 74 20 at
58f20 73 74 61 72 74 2d 74 69 6d 65 2e 0a 2a 2a 0a 2a start-time..**.*
58f30 2a 20 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 * SQLITE_MUTEX
58f40 5f 4e 4f 4f 50 20 20 20 20 20 20 20 20 20 46 6f _NOOP Fo
58f50 72 20 73 69 6e 67 6c 65 2d 74 68 72 65 61 64 65 r single-threade
58f60 64 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 2e 20 d applications.
58f70 20 4e 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 No.**
58f80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58f90 20 20 20 6d 75 74 75 61 6c 20 65 78 63 6c 75 73 mutual exclus
58fa0 69 6f 6e 20 69 73 20 70 72 6f 76 69 64 65 64 2e ion is provided.
58fb0 20 20 42 75 74 20 74 68 69 73 0a 2a 2a 20 20 20 But this.**
58fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58fd0 20 20 20 20 20 20 20 20 20 20 69 6d 70 6c 65 6d implem
58fe0 65 6e 74 61 74 69 6f 6e 20 63 61 6e 20 62 65 20 entation can be
58ff0 6f 76 65 72 72 69 64 64 65 6e 20 61 74 0a 2a 2a overridden at.**
59000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59010 20 20 20 20 20 20 20 20 20 20 20 20 20 73 74 61 sta
59020 72 74 2d 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 20 rt-time..**.**
59030 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 50 54 SQLITE_MUTEX_PT
59040 48 52 45 41 44 53 20 20 20 20 20 46 6f 72 20 6d HREADS For m
59050 75 6c 74 69 2d 74 68 72 65 61 64 65 64 20 61 70 ulti-threaded ap
59060 70 6c 69 63 61 74 69 6f 6e 73 20 6f 6e 20 55 6e plications on Un
59070 69 78 2e 0a 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49 ix..**.** SQLI
59080 54 45 5f 4d 55 54 45 58 5f 57 33 32 20 20 20 20 TE_MUTEX_W32
59090 20 20 20 20 20 20 46 6f 72 20 6d 75 6c 74 69 2d For multi-
590a0 74 68 72 65 61 64 65 64 20 61 70 70 6c 69 63 61 threaded applica
590b0 74 69 6f 6e 73 20 6f 6e 20 57 69 6e 33 32 2e 0a tions on Win32..
590c0 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f 4d **.** SQLITE_M
590d0 55 54 45 58 5f 4f 53 32 20 20 20 20 20 20 20 20 UTEX_OS2
590e0 20 20 46 6f 72 20 6d 75 6c 74 69 2d 74 68 72 65 For multi-thre
590f0 61 64 65 64 20 61 70 70 6c 69 63 61 74 69 6f 6e aded application
59100 73 20 6f 6e 20 4f 53 2f 32 2e 0a 2a 2f 0a 23 69 s on OS/2..*/.#i
59110 66 20 21 53 51 4c 49 54 45 5f 54 48 52 45 41 44 f !SQLITE_THREAD
59120 53 41 46 45 0a 23 20 64 65 66 69 6e 65 20 53 51 SAFE.# define SQ
59130 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 4d 49 54 0a LITE_MUTEX_OMIT.
59140 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54 #endif.#if SQLIT
59150 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 20 E_THREADSAFE &&
59160 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f !defined(SQLITE_
59170 4d 55 54 45 58 5f 4e 4f 4f 50 29 0a 23 20 20 69 MUTEX_NOOP).# i
59180 66 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 f SQLITE_OS_UNIX
59190 0a 23 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c .# define SQL
591a0 49 54 45 5f 4d 55 54 45 58 5f 50 54 48 52 45 41 ITE_MUTEX_PTHREA
591b0 44 53 0a 23 20 20 65 6c 69 66 20 53 51 4c 49 54 DS.# elif SQLIT
591c0 45 5f 4f 53 5f 57 49 4e 0a 23 20 20 20 20 64 65 E_OS_WIN.# de
591d0 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 fine SQLITE_MUTE
591e0 58 5f 57 33 32 0a 23 20 20 65 6c 69 66 20 53 51 X_W32.# elif SQ
591f0 4c 49 54 45 5f 4f 53 5f 4f 53 32 0a 23 20 20 20 LITE_OS_OS2.#
59200 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d define SQLITE_M
59210 55 54 45 58 5f 4f 53 32 0a 23 20 20 65 6c 73 65 UTEX_OS2.# else
59220 0a 23 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c .# define SQL
59230 49 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 0a 23 ITE_MUTEX_NOOP.#
59240 20 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a endif.#endif..
59250 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4d 55 #ifdef SQLITE_MU
59260 54 45 58 5f 4f 4d 49 54 0a 2f 2a 0a 2a 2a 20 49 TEX_OMIT./*.** I
59270 66 20 74 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f f this is a no-o
59280 70 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e p implementation
59290 2c 20 69 6d 70 6c 65 6d 65 6e 74 20 65 76 65 72 , implement ever
592a0 79 74 68 69 6e 67 20 61 73 20 6d 61 63 72 6f 73 ything as macros
592b0 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 73 71 6c ..*/.#define sql
592c0 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 ite3_mutex_alloc
592d0 28 58 29 20 20 20 20 28 28 73 71 6c 69 74 65 33 (X) ((sqlite3
592e0 5f 6d 75 74 65 78 2a 29 38 29 0a 23 64 65 66 69 _mutex*)8).#defi
592f0 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ne sqlite3_mutex
59300 5f 66 72 65 65 28 58 29 0a 23 64 65 66 69 6e 65 _free(X).#define
59310 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 sqlite3_mutex_e
59320 6e 74 65 72 28 58 29 0a 23 64 65 66 69 6e 65 20 nter(X).#define
59330 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 sqlite3_mutex_tr
59340 79 28 58 29 20 20 20 20 20 20 53 51 4c 49 54 45 y(X) SQLITE
59350 5f 4f 4b 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 _OK.#define sqli
59360 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
59370 58 29 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 X).#define sqlit
59380 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 58 29 e3_mutex_held(X)
59390 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 73 1.#define s
593a0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 qlite3_mutex_not
593b0 68 65 6c 64 28 58 29 20 20 31 0a 23 64 65 66 69 held(X) 1.#defi
593c0 6e 65 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 ne sqlite3MutexA
593d0 6c 6c 6f 63 28 58 29 20 20 20 20 20 20 28 28 73 lloc(X) ((s
593e0 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 38 29 qlite3_mutex*)8)
593f0 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 .#define sqlite3
59400 4d 75 74 65 78 49 6e 69 74 28 29 20 20 20 20 20 MutexInit()
59410 20 20 20 53 51 4c 49 54 45 5f 4f 4b 0a 23 64 65 SQLITE_OK.#de
59420 66 69 6e 65 20 73 71 6c 69 74 65 33 4d 75 74 65 fine sqlite3Mute
59430 78 45 6e 64 28 29 0a 23 65 6e 64 69 66 20 2f 2a xEnd().#endif /*
59440 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f defined(SQLITE_
59450 4d 55 54 45 58 5f 4f 4d 49 54 29 20 2a 2f 0a 0a MUTEX_OMIT) */..
59460 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
59470 45 6e 64 20 6f 66 20 6d 75 74 65 78 2e 68 20 2a End of mutex.h *
59480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
59490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
594a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
594b0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
594c0 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 Continuing where
594d0 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 we left off in
594e0 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a sqliteInt.h ****
594f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
59500 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 64 61 74 ../*.** Each dat
59510 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20 62 65 abase file to be
59520 20 61 63 63 65 73 73 65 64 20 62 79 20 74 68 65 accessed by the
59530 20 73 79 73 74 65 6d 20 69 73 20 61 6e 20 69 6e system is an in
59540 73 74 61 6e 63 65 0a 2a 2a 20 6f 66 20 74 68 65 stance.** of the
59550 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 following struc
59560 74 75 72 65 2e 20 20 54 68 65 72 65 20 61 72 65 ture. There are
59570 20 6e 6f 72 6d 61 6c 6c 79 20 74 77 6f 20 6f 66 normally two of
59580 20 74 68 65 73 65 20 73 74 72 75 63 74 75 72 65 these structure
59590 73 0a 2a 2a 20 69 6e 20 74 68 65 20 73 71 6c 69 s.** in the sqli
595a0 74 65 2e 61 44 62 5b 5d 20 61 72 72 61 79 2e 20 te.aDb[] array.
595b0 20 61 44 62 5b 30 5d 20 69 73 20 74 68 65 20 6d aDb[0] is the m
595c0 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c ain database fil
595d0 65 20 61 6e 64 0a 2a 2a 20 61 44 62 5b 31 5d 20 e and.** aDb[1]
595e0 69 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 is the database
595f0 66 69 6c 65 20 75 73 65 64 20 74 6f 20 68 6f 6c file used to hol
59600 64 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c d temporary tabl
59610 65 73 2e 20 20 41 64 64 69 74 69 6f 6e 61 6c 0a es. Additional.
59620 2a 2a 20 64 61 74 61 62 61 73 65 73 20 6d 61 79 ** databases may
59630 20 62 65 20 61 74 74 61 63 68 65 64 2e 0a 2a 2f be attached..*/
59640 0a 73 74 72 75 63 74 20 44 62 20 7b 0a 20 20 63 .struct Db {. c
59650 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 har *zName;
59660 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 /* Name of t
59670 68 69 73 20 64 61 74 61 62 61 73 65 20 2a 2f 0a his database */.
59680 20 20 42 74 72 65 65 20 2a 70 42 74 3b 20 20 20 Btree *pBt;
59690 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 42 2a /* The B*
596a0 54 72 65 65 20 73 74 72 75 63 74 75 72 65 20 66 Tree structure f
596b0 6f 72 20 74 68 69 73 20 64 61 74 61 62 61 73 65 or this database
596c0 20 66 69 6c 65 20 2a 2f 0a 20 20 75 38 20 69 6e file */. u8 in
596d0 54 72 61 6e 73 3b 20 20 20 20 20 20 20 20 20 20 Trans;
596e0 2f 2a 20 30 3a 20 6e 6f 74 20 77 72 69 74 61 62 /* 0: not writab
596f0 6c 65 2e 20 20 31 3a 20 54 72 61 6e 73 61 63 74 le. 1: Transact
59700 69 6f 6e 2e 20 20 32 3a 20 43 68 65 63 6b 70 6f ion. 2: Checkpo
59710 69 6e 74 20 2a 2f 0a 20 20 75 38 20 73 61 66 65 int */. u8 safe
59720 74 79 5f 6c 65 76 65 6c 3b 20 20 20 20 20 2f 2a ty_level; /*
59730 20 48 6f 77 20 61 67 67 72 65 73 73 69 76 65 20 How aggressive
59740 61 74 20 73 79 6e 63 69 6e 67 20 64 61 74 61 20 at syncing data
59750 74 6f 20 64 69 73 6b 20 2a 2f 0a 20 20 53 63 68 to disk */. Sch
59760 65 6d 61 20 2a 70 53 63 68 65 6d 61 3b 20 20 20 ema *pSchema;
59770 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 /* Pointer to
59780 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 database schema
59790 28 70 6f 73 73 69 62 6c 79 20 73 68 61 72 65 64 (possibly shared
597a0 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 ) */.};../*.** A
597b0 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 n instance of th
597c0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 e following stru
597d0 63 74 75 72 65 20 73 74 6f 72 65 73 20 61 20 64 cture stores a d
597e0 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 2e 0a atabase schema..
597f0 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 61 **.** If there a
59800 72 65 20 6e 6f 20 76 69 72 74 75 61 6c 20 74 61 re no virtual ta
59810 62 6c 65 73 20 63 6f 6e 66 69 67 75 72 65 64 20 bles configured
59820 69 6e 20 74 68 69 73 20 73 63 68 65 6d 61 2c 20 in this schema,
59830 74 68 65 0a 2a 2a 20 53 63 68 65 6d 61 2e 64 62 the.** Schema.db
59840 20 76 61 72 69 61 62 6c 65 20 69 73 20 73 65 74 variable is set
59850 20 74 6f 20 4e 55 4c 4c 2e 20 41 66 74 65 72 20 to NULL. After
59860 74 68 65 20 66 69 72 73 74 20 76 69 72 74 75 61 the first virtua
59870 6c 20 74 61 62 6c 65 0a 2a 2a 20 68 61 73 20 62 l table.** has b
59880 65 65 6e 20 61 64 64 65 64 2c 20 69 74 20 69 73 een added, it is
59890 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f set to point to
598a0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f the database co
598b0 6e 6e 65 63 74 69 6f 6e 20 0a 2a 2a 20 75 73 65 nnection .** use
598c0 64 20 74 6f 20 63 72 65 61 74 65 20 74 68 65 20 d to create the
598d0 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 4f 6e 63 65 connection. Once
598e0 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 a virtual table
598f0 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 61 64 64 has been.** add
59900 65 64 20 74 6f 20 74 68 65 20 53 63 68 65 6d 61 ed to the Schema
59910 20 73 74 72 75 63 74 75 72 65 20 61 6e 64 20 74 structure and t
59920 68 65 20 53 63 68 65 6d 61 2e 64 62 20 76 61 72 he Schema.db var
59930 69 61 62 6c 65 20 70 6f 70 75 6c 61 74 65 64 2c iable populated,
59940 20 0a 2a 2a 20 6f 6e 6c 79 20 74 68 61 74 20 64 .** only that d
59950 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
59960 6f 6e 20 6d 61 79 20 75 73 65 20 74 68 65 20 53 on may use the S
59970 63 68 65 6d 61 20 74 6f 20 70 72 65 70 61 72 65 chema to prepare
59980 20 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 73 2e .** statements.
59990 0a 2a 2f 0a 73 74 72 75 63 74 20 53 63 68 65 6d .*/.struct Schem
599a0 61 20 7b 0a 20 20 69 6e 74 20 73 63 68 65 6d 61 a {. int schema
599b0 5f 63 6f 6f 6b 69 65 3b 20 20 20 2f 2a 20 44 61 _cookie; /* Da
599c0 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 76 65 tabase schema ve
599d0 72 73 69 6f 6e 20 6e 75 6d 62 65 72 20 66 6f 72 rsion number for
599e0 20 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a 20 20 this file */.
599f0 48 61 73 68 20 74 62 6c 48 61 73 68 3b 20 20 20 Hash tblHash;
59a00 20 20 20 20 20 2f 2a 20 41 6c 6c 20 74 61 62 6c /* All tabl
59a10 65 73 20 69 6e 64 65 78 65 64 20 62 79 20 6e 61 es indexed by na
59a20 6d 65 20 2a 2f 0a 20 20 48 61 73 68 20 69 64 78 me */. Hash idx
59a30 48 61 73 68 3b 20 20 20 20 20 20 20 20 2f 2a 20 Hash; /*
59a40 41 6c 6c 20 28 6e 61 6d 65 64 29 20 69 6e 64 69 All (named) indi
59a50 63 65 73 20 69 6e 64 65 78 65 64 20 62 79 20 6e ces indexed by n
59a60 61 6d 65 20 2a 2f 0a 20 20 48 61 73 68 20 74 72 ame */. Hash tr
59a70 69 67 48 61 73 68 3b 20 20 20 20 20 20 20 2f 2a igHash; /*
59a80 20 41 6c 6c 20 74 72 69 67 67 65 72 73 20 69 6e All triggers in
59a90 64 65 78 65 64 20 62 79 20 6e 61 6d 65 20 2a 2f dexed by name */
59aa0 0a 20 20 48 61 73 68 20 66 6b 65 79 48 61 73 68 . Hash fkeyHash
59ab0 3b 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20 66 ; /* All f
59ac0 6f 72 65 69 67 6e 20 6b 65 79 73 20 62 79 20 72 oreign keys by r
59ad0 65 66 65 72 65 6e 63 65 64 20 74 61 62 6c 65 20 eferenced table
59ae0 6e 61 6d 65 20 2a 2f 0a 20 20 54 61 62 6c 65 20 name */. Table
59af0 2a 70 53 65 71 54 61 62 3b 20 20 20 20 20 20 2f *pSeqTab; /
59b00 2a 20 54 68 65 20 73 71 6c 69 74 65 5f 73 65 71 * The sqlite_seq
59b10 75 65 6e 63 65 20 74 61 62 6c 65 20 75 73 65 64 uence table used
59b20 20 62 79 20 41 55 54 4f 49 4e 43 52 45 4d 45 4e by AUTOINCREMEN
59b30 54 20 2a 2f 0a 20 20 75 38 20 66 69 6c 65 5f 66 T */. u8 file_f
59b40 6f 72 6d 61 74 3b 20 20 20 20 20 20 2f 2a 20 53 ormat; /* S
59b50 63 68 65 6d 61 20 66 6f 72 6d 61 74 20 76 65 72 chema format ver
59b60 73 69 6f 6e 20 66 6f 72 20 74 68 69 73 20 66 69 sion for this fi
59b70 6c 65 20 2a 2f 0a 20 20 75 38 20 65 6e 63 3b 20 le */. u8 enc;
59b80 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
59b90 54 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 75 73 Text encoding us
59ba0 65 64 20 62 79 20 74 68 69 73 20 64 61 74 61 62 ed by this datab
59bb0 61 73 65 20 2a 2f 0a 20 20 75 31 36 20 66 6c 61 ase */. u16 fla
59bc0 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a gs; /*
59bd0 20 46 6c 61 67 73 20 61 73 73 6f 63 69 61 74 65 Flags associate
59be0 64 20 77 69 74 68 20 74 68 69 73 20 73 63 68 65 d with this sche
59bf0 6d 61 20 2a 2f 0a 20 20 69 6e 74 20 63 61 63 68 ma */. int cach
59c00 65 5f 73 69 7a 65 3b 20 20 20 20 20 20 2f 2a 20 e_size; /*
59c10 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 Number of pages
59c20 74 6f 20 75 73 65 20 69 6e 20 74 68 65 20 63 61 to use in the ca
59c30 63 68 65 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 che */.#ifndef S
59c40 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 QLITE_OMIT_VIRTU
59c50 41 4c 54 41 42 4c 45 0a 20 20 73 71 6c 69 74 65 ALTABLE. sqlite
59c60 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20 2f 3 *db; /
59c70 2a 20 22 4f 77 6e 65 72 22 20 63 6f 6e 6e 65 63 * "Owner" connec
59c80 74 69 6f 6e 2e 20 53 65 65 20 63 6f 6d 6d 65 6e tion. See commen
59c90 74 20 61 62 6f 76 65 20 2a 2f 0a 23 65 6e 64 69 t above */.#endi
59ca0 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 f.};../*.** Thes
59cb0 65 20 6d 61 63 72 6f 73 20 63 61 6e 20 62 65 20 e macros can be
59cc0 75 73 65 64 20 74 6f 20 74 65 73 74 2c 20 73 65 used to test, se
59cd0 74 2c 20 6f 72 20 63 6c 65 61 72 20 62 69 74 73 t, or clear bits
59ce0 20 69 6e 20 74 68 65 20 0a 2a 2a 20 44 62 2e 66 in the .** Db.f
59cf0 6c 61 67 73 20 66 69 65 6c 64 2e 0a 2a 2f 0a 23 lags field..*/.#
59d00 64 65 66 69 6e 65 20 44 62 48 61 73 50 72 6f 70 define DbHasProp
59d10 65 72 74 79 28 44 2c 49 2c 50 29 20 20 20 20 20 erty(D,I,P)
59d20 28 28 28 44 29 2d 3e 61 44 62 5b 49 5d 2e 70 53 (((D)->aDb[I].pS
59d30 63 68 65 6d 61 2d 3e 66 6c 61 67 73 26 28 50 29 chema->flags&(P)
59d40 29 3d 3d 28 50 29 29 0a 23 64 65 66 69 6e 65 20 )==(P)).#define
59d50 44 62 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 DbHasAnyProperty
59d60 28 44 2c 49 2c 50 29 20 20 28 28 28 44 29 2d 3e (D,I,P) (((D)->
59d70 61 44 62 5b 49 5d 2e 70 53 63 68 65 6d 61 2d 3e aDb[I].pSchema->
59d80 66 6c 61 67 73 26 28 50 29 29 21 3d 30 29 0a 23 flags&(P))!=0).#
59d90 64 65 66 69 6e 65 20 44 62 53 65 74 50 72 6f 70 define DbSetProp
59da0 65 72 74 79 28 44 2c 49 2c 50 29 20 20 20 20 20 erty(D,I,P)
59db0 28 44 29 2d 3e 61 44 62 5b 49 5d 2e 70 53 63 68 (D)->aDb[I].pSch
59dc0 65 6d 61 2d 3e 66 6c 61 67 73 7c 3d 28 50 29 0a ema->flags|=(P).
59dd0 23 64 65 66 69 6e 65 20 44 62 43 6c 65 61 72 50 #define DbClearP
59de0 72 6f 70 65 72 74 79 28 44 2c 49 2c 50 29 20 20 roperty(D,I,P)
59df0 20 28 44 29 2d 3e 61 44 62 5b 49 5d 2e 70 53 63 (D)->aDb[I].pSc
59e00 68 65 6d 61 2d 3e 66 6c 61 67 73 26 3d 7e 28 50 hema->flags&=~(P
59e10 29 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 )../*.** Allowed
59e20 20 76 61 6c 75 65 73 20 66 6f 72 20 74 68 65 20 values for the
59e30 44 42 2e 66 6c 61 67 73 20 66 69 65 6c 64 2e 0a DB.flags field..
59e40 2a 2a 0a 2a 2a 20 54 68 65 20 44 42 5f 53 63 68 **.** The DB_Sch
59e50 65 6d 61 4c 6f 61 64 65 64 20 66 6c 61 67 20 69 emaLoaded flag i
59e60 73 20 73 65 74 20 61 66 74 65 72 20 74 68 65 20 s set after the
59e70 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 database schema
59e80 68 61 73 20 62 65 65 6e 0a 2a 2a 20 72 65 61 64 has been.** read
59e90 20 69 6e 74 6f 20 69 6e 74 65 72 6e 61 6c 20 68 into internal h
59ea0 61 73 68 20 74 61 62 6c 65 73 2e 0a 2a 2a 0a 2a ash tables..**.*
59eb0 2a 20 44 42 5f 55 6e 72 65 73 65 74 56 69 65 77 * DB_UnresetView
59ec0 73 20 6d 65 61 6e 73 20 74 68 61 74 20 6f 6e 65 s means that one
59ed0 20 6f 72 20 6d 6f 72 65 20 76 69 65 77 73 20 68 or more views h
59ee0 61 76 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 ave column names
59ef0 20 74 68 61 74 0a 2a 2a 20 68 61 76 65 20 62 65 that.** have be
59f00 65 6e 20 66 69 6c 6c 65 64 20 6f 75 74 2e 20 20 en filled out.
59f10 49 66 20 74 68 65 20 73 63 68 65 6d 61 20 63 68 If the schema ch
59f20 61 6e 67 65 73 2c 20 74 68 65 73 65 20 63 6f 6c anges, these col
59f30 75 6d 6e 20 6e 61 6d 65 73 20 6d 69 67 68 74 0a umn names might.
59f40 2a 2a 20 63 68 61 6e 67 65 73 20 61 6e 64 20 73 ** changes and s
59f50 6f 20 74 68 65 20 76 69 65 77 20 77 69 6c 6c 20 o the view will
59f60 6e 65 65 64 20 74 6f 20 62 65 20 72 65 73 65 74 need to be reset
59f70 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 44 42 5f ..*/.#define DB_
59f80 53 63 68 65 6d 61 4c 6f 61 64 65 64 20 20 20 20 SchemaLoaded
59f90 30 78 30 30 30 31 20 20 2f 2a 20 54 68 65 20 73 0x0001 /* The s
59fa0 63 68 65 6d 61 20 68 61 73 20 62 65 65 6e 20 6c chema has been l
59fb0 6f 61 64 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 oaded */.#define
59fc0 20 44 42 5f 55 6e 72 65 73 65 74 56 69 65 77 73 DB_UnresetViews
59fd0 20 20 20 20 30 78 30 30 30 32 20 20 2f 2a 20 53 0x0002 /* S
59fe0 6f 6d 65 20 76 69 65 77 73 20 68 61 76 65 20 64 ome views have d
59ff0 65 66 69 6e 65 64 20 63 6f 6c 75 6d 6e 20 6e 61 efined column na
5a000 6d 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 44 mes */.#define D
5a010 42 5f 45 6d 70 74 79 20 20 20 20 20 20 20 20 20 B_Empty
5a020 20 20 30 78 30 30 30 34 20 20 2f 2a 20 54 68 65 0x0004 /* The
5a030 20 66 69 6c 65 20 69 73 20 65 6d 70 74 79 20 28 file is empty (
5a040 6c 65 6e 67 74 68 20 30 20 62 79 74 65 73 29 20 length 0 bytes)
5a050 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 75 */../*.** The nu
5a060 6d 62 65 72 20 6f 66 20 64 69 66 66 65 72 65 6e mber of differen
5a070 74 20 6b 69 6e 64 73 20 6f 66 20 74 68 69 6e 67 t kinds of thing
5a080 73 20 74 68 61 74 20 63 61 6e 20 62 65 20 6c 69 s that can be li
5a090 6d 69 74 65 64 0a 2a 2a 20 75 73 69 6e 67 20 74 mited.** using t
5a0a0 68 65 20 73 71 6c 69 74 65 33 5f 6c 69 6d 69 74 he sqlite3_limit
5a0b0 28 29 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2f () interface..*/
5a0c0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
5a0d0 4e 5f 4c 49 4d 49 54 20 28 53 51 4c 49 54 45 5f N_LIMIT (SQLITE_
5a0e0 4c 49 4d 49 54 5f 54 52 49 47 47 45 52 5f 44 45 LIMIT_TRIGGER_DE
5a0f0 50 54 48 2b 31 29 0a 0a 2f 2a 0a 2a 2a 20 4c 6f PTH+1)../*.** Lo
5a100 6f 6b 61 73 69 64 65 20 6d 61 6c 6c 6f 63 20 69 okaside malloc i
5a110 73 20 61 20 73 65 74 20 6f 66 20 66 69 78 65 64 s a set of fixed
5a120 2d 73 69 7a 65 20 62 75 66 66 65 72 73 20 74 68 -size buffers th
5a130 61 74 20 63 61 6e 20 62 65 20 75 73 65 64 0a 2a at can be used.*
5a140 2a 20 74 6f 20 73 61 74 69 73 66 79 20 73 6d 61 * to satisfy sma
5a150 6c 6c 20 74 72 61 6e 73 69 65 6e 74 20 6d 65 6d ll transient mem
5a160 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 ory allocation r
5a170 65 71 75 65 73 74 73 20 66 6f 72 20 6f 62 6a 65 equests for obje
5a180 63 74 73 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 cts.** associate
5a190 64 20 77 69 74 68 20 61 20 70 61 72 74 69 63 75 d with a particu
5a1a0 6c 61 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e lar database con
5a1b0 6e 65 63 74 69 6f 6e 2e 20 20 54 68 65 20 75 73 nection. The us
5a1c0 65 20 6f 66 0a 2a 2a 20 6c 6f 6f 6b 61 73 69 64 e of.** lookasid
5a1d0 65 20 6d 61 6c 6c 6f 63 20 70 72 6f 76 69 64 65 e malloc provide
5a1e0 73 20 61 20 73 69 67 6e 69 66 69 63 61 6e 74 20 s a significant
5a1f0 70 65 72 66 6f 72 6d 61 6e 63 65 20 65 6e 68 61 performance enha
5a200 6e 63 65 6d 65 6e 74 0a 2a 2a 20 28 61 70 70 72 ncement.** (appr
5a210 6f 78 20 31 30 25 29 20 62 79 20 61 76 6f 69 64 ox 10%) by avoid
5a220 69 6e 67 20 6e 75 6d 65 72 6f 75 73 20 6d 61 6c ing numerous mal
5a230 6c 6f 63 2f 66 72 65 65 20 72 65 71 75 65 73 74 loc/free request
5a240 73 20 77 68 69 6c 65 20 70 61 72 73 69 6e 67 0a s while parsing.
5a250 2a 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 ** SQL statement
5a260 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 4c 6f 6f s..**.** The Loo
5a270 6b 61 73 69 64 65 20 73 74 72 75 63 74 75 72 65 kaside structure
5a280 20 68 6f 6c 64 73 20 63 6f 6e 66 69 67 75 72 61 holds configura
5a290 74 69 6f 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e tion information
5a2a0 20 61 62 6f 75 74 20 74 68 65 0a 2a 2a 20 6c 6f about the.** lo
5a2b0 6f 6b 61 73 69 64 65 20 6d 61 6c 6c 6f 63 20 73 okaside malloc s
5a2c0 75 62 73 79 73 74 65 6d 2e 20 20 45 61 63 68 20 ubsystem. Each
5a2d0 61 76 61 69 6c 61 62 6c 65 20 6d 65 6d 6f 72 79 available memory
5a2e0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e 0a 2a allocation in.*
5a2f0 2a 20 74 68 65 20 6c 6f 6f 6b 61 73 69 64 65 20 * the lookaside
5a300 73 75 62 73 79 73 74 65 6d 20 69 73 20 73 74 6f subsystem is sto
5a310 72 65 64 20 6f 6e 20 61 20 6c 69 6e 6b 65 64 20 red on a linked
5a320 6c 69 73 74 20 6f 66 20 4c 6f 6f 6b 61 73 69 64 list of Lookasid
5a330 65 53 6c 6f 74 0a 2a 2a 20 6f 62 6a 65 63 74 73 eSlot.** objects
5a340 2e 0a 2a 2a 0a 2a 2a 20 4c 6f 6f 6b 61 73 69 64 ..**.** Lookasid
5a350 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 61 72 e allocations ar
5a360 65 20 6f 6e 6c 79 20 61 6c 6c 6f 77 65 64 20 66 e only allowed f
5a370 6f 72 20 6f 62 6a 65 63 74 73 20 74 68 61 74 20 or objects that
5a380 61 72 65 20 61 73 73 6f 63 69 61 74 65 64 0a 2a are associated.*
5a390 2a 20 77 69 74 68 20 61 20 70 61 72 74 69 63 75 * with a particu
5a3a0 6c 61 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e lar database con
5a3b0 6e 65 63 74 69 6f 6e 2e 20 20 48 65 6e 63 65 2c nection. Hence,
5a3c0 20 73 63 68 65 6d 61 20 69 6e 66 6f 72 6d 61 74 schema informat
5a3d0 69 6f 6e 20 63 61 6e 6e 6f 74 0a 2a 2a 20 62 65 ion cannot.** be
5a3e0 20 73 74 6f 72 65 64 20 69 6e 20 6c 6f 6f 6b 61 stored in looka
5a3f0 73 69 64 65 20 62 65 63 61 75 73 65 20 69 6e 20 side because in
5a400 73 68 61 72 65 64 20 63 61 63 68 65 20 6d 6f 64 shared cache mod
5a410 65 20 74 68 65 20 73 63 68 65 6d 61 20 69 6e 66 e the schema inf
5a420 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 73 ormation.** is s
5a430 68 61 72 65 64 20 62 79 20 6d 75 6c 74 69 70 6c hared by multipl
5a440 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 e database conne
5a450 63 74 69 6f 6e 73 2e 20 20 54 68 65 72 65 66 6f ctions. Therefo
5a460 72 65 2c 20 77 68 69 6c 65 20 70 61 72 73 69 6e re, while parsin
5a470 67 0a 2a 2a 20 73 63 68 65 6d 61 20 69 6e 66 6f g.** schema info
5a480 72 6d 61 74 69 6f 6e 2c 20 74 68 65 20 4c 6f 6f rmation, the Loo
5a490 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c 65 64 20 kaside.bEnabled
5a4a0 66 6c 61 67 20 69 73 20 63 6c 65 61 72 65 64 20 flag is cleared
5a4b0 73 6f 20 74 68 61 74 0a 2a 2a 20 6c 6f 6f 6b 61 so that.** looka
5a4c0 73 69 64 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 side allocations
5a4d0 20 61 72 65 20 6e 6f 74 20 75 73 65 64 20 74 6f are not used to
5a4e0 20 63 6f 6e 73 74 72 75 63 74 20 74 68 65 20 73 construct the s
5a4f0 63 68 65 6d 61 20 6f 62 6a 65 63 74 73 2e 0a 2a chema objects..*
5a500 2f 0a 73 74 72 75 63 74 20 4c 6f 6f 6b 61 73 69 /.struct Lookasi
5a510 64 65 20 7b 0a 20 20 75 31 36 20 73 7a 3b 20 20 de {. u16 sz;
5a520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
5a530 2a 20 53 69 7a 65 20 6f 66 20 65 61 63 68 20 62 * Size of each b
5a540 75 66 66 65 72 20 69 6e 20 62 79 74 65 73 20 2a uffer in bytes *
5a550 2f 0a 20 20 75 38 20 62 45 6e 61 62 6c 65 64 3b /. u8 bEnabled;
5a560 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 /* F
5a570 61 6c 73 65 20 74 6f 20 64 69 73 61 62 6c 65 20 alse to disable
5a580 6e 65 77 20 6c 6f 6f 6b 61 73 69 64 65 20 61 6c new lookaside al
5a590 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 75 locations */. u
5a5a0 38 20 62 4d 61 6c 6c 6f 63 65 64 3b 20 20 20 20 8 bMalloced;
5a5b0 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
5a5c0 66 20 70 53 74 61 72 74 20 6f 62 74 61 69 6e 65 f pStart obtaine
5a5d0 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d d from sqlite3_m
5a5e0 61 6c 6c 6f 63 28 29 20 2a 2f 0a 20 20 69 6e 74 alloc() */. int
5a5f0 20 6e 4f 75 74 3b 20 20 20 20 20 20 20 20 20 20 nOut;
5a600 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
5a610 66 20 62 75 66 66 65 72 73 20 63 75 72 72 65 6e f buffers curren
5a620 74 6c 79 20 63 68 65 63 6b 65 64 20 6f 75 74 20 tly checked out
5a630 2a 2f 0a 20 20 69 6e 74 20 6d 78 4f 75 74 3b 20 */. int mxOut;
5a640 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
5a650 48 69 67 68 77 61 74 65 72 20 6d 61 72 6b 20 66 Highwater mark f
5a660 6f 72 20 6e 4f 75 74 20 2a 2f 0a 20 20 4c 6f 6f or nOut */. Loo
5a670 6b 61 73 69 64 65 53 6c 6f 74 20 2a 70 46 72 65 kasideSlot *pFre
5a680 65 3b 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 e; /* List of
5a690 61 76 61 69 6c 61 62 6c 65 20 62 75 66 66 65 72 available buffer
5a6a0 73 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 53 74 s */. void *pSt
5a6b0 61 72 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f art; /
5a6c0 2a 20 46 69 72 73 74 20 62 79 74 65 20 6f 66 20 * First byte of
5a6d0 61 76 61 69 6c 61 62 6c 65 20 6d 65 6d 6f 72 79 available memory
5a6e0 20 73 70 61 63 65 20 2a 2f 0a 20 20 76 6f 69 64 space */. void
5a6f0 20 2a 70 45 6e 64 3b 20 20 20 20 20 20 20 20 20 *pEnd;
5a700 20 20 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74 /* First byt
5a710 65 20 70 61 73 74 20 65 6e 64 20 6f 66 20 61 76 e past end of av
5a720 61 69 6c 61 62 6c 65 20 73 70 61 63 65 20 2a 2f ailable space */
5a730 0a 7d 3b 0a 73 74 72 75 63 74 20 4c 6f 6f 6b 61 .};.struct Looka
5a740 73 69 64 65 53 6c 6f 74 20 7b 0a 20 20 4c 6f 6f sideSlot {. Loo
5a750 6b 61 73 69 64 65 53 6c 6f 74 20 2a 70 4e 65 78 kasideSlot *pNex
5a760 74 3b 20 20 20 20 2f 2a 20 4e 65 78 74 20 62 75 t; /* Next bu
5a770 66 66 65 72 20 69 6e 20 74 68 65 20 6c 69 73 74 ffer in the list
5a780 20 6f 66 20 66 72 65 65 20 62 75 66 66 65 72 73 of free buffers
5a790 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 */.};../*.** A
5a7a0 68 61 73 68 20 74 61 62 6c 65 20 66 6f 72 20 66 hash table for f
5a7b0 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69 74 69 unction definiti
5a7c0 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 48 61 73 68 20 ons..**.** Hash
5a7d0 65 61 63 68 20 46 75 6e 63 44 65 66 20 73 74 72 each FuncDef str
5a7e0 75 63 74 75 72 65 20 69 6e 74 6f 20 6f 6e 65 20 ucture into one
5a7f0 6f 66 20 74 68 65 20 46 75 6e 63 44 65 66 48 61 of the FuncDefHa
5a800 73 68 2e 61 5b 5d 20 73 6c 6f 74 73 2e 0a 2a 2a sh.a[] slots..**
5a810 20 43 6f 6c 6c 69 73 69 6f 6e 73 20 61 72 65 20 Collisions are
5a820 6f 6e 20 74 68 65 20 46 75 6e 63 44 65 66 2e 70 on the FuncDef.p
5a830 48 61 73 68 20 63 68 61 69 6e 2e 0a 2a 2f 0a 73 Hash chain..*/.s
5a840 74 72 75 63 74 20 46 75 6e 63 44 65 66 48 61 73 truct FuncDefHas
5a850 68 20 7b 0a 20 20 46 75 6e 63 44 65 66 20 2a 61 h {. FuncDef *a
5a860 5b 32 33 5d 3b 20 20 20 20 20 20 20 2f 2a 20 48 [23]; /* H
5a870 61 73 68 20 74 61 62 6c 65 20 66 6f 72 20 66 75 ash table for fu
5a880 6e 63 74 69 6f 6e 73 20 2a 2f 0a 7d 3b 0a 0a 2f nctions */.};../
5a890 2a 0a 2a 2a 20 45 61 63 68 20 64 61 74 61 62 61 *.** Each databa
5a8a0 73 65 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 se is an instanc
5a8b0 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 e of the followi
5a8c0 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a ng structure..**
5a8d0 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 2e 6c .** The sqlite.l
5a8e0 61 73 74 52 6f 77 69 64 20 72 65 63 6f 72 64 73 astRowid records
5a8f0 20 74 68 65 20 6c 61 73 74 20 69 6e 73 65 72 74 the last insert
5a900 20 72 6f 77 69 64 20 67 65 6e 65 72 61 74 65 64 rowid generated
5a910 20 62 79 20 61 6e 0a 2a 2a 20 69 6e 73 65 72 74 by an.** insert
5a920 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 49 6e 73 statement. Ins
5a930 65 72 74 73 20 6f 6e 20 76 69 65 77 73 20 64 6f erts on views do
5a940 20 6e 6f 74 20 61 66 66 65 63 74 20 69 74 73 20 not affect its
5a950 76 61 6c 75 65 2e 20 20 45 61 63 68 0a 2a 2a 20 value. Each.**
5a960 74 72 69 67 67 65 72 20 68 61 73 20 69 74 73 20 trigger has its
5a970 6f 77 6e 20 63 6f 6e 74 65 78 74 2c 20 73 6f 20 own context, so
5a980 74 68 61 74 20 6c 61 73 74 52 6f 77 69 64 20 63 that lastRowid c
5a990 61 6e 20 62 65 20 75 70 64 61 74 65 64 20 69 6e an be updated in
5a9a0 73 69 64 65 0a 2a 2a 20 74 72 69 67 67 65 72 73 side.** triggers
5a9b0 20 61 73 20 75 73 75 61 6c 2e 20 20 54 68 65 20 as usual. The
5a9c0 70 72 65 76 69 6f 75 73 20 76 61 6c 75 65 20 77 previous value w
5a9d0 69 6c 6c 20 62 65 20 72 65 73 74 6f 72 65 64 20 ill be restored
5a9e0 6f 6e 63 65 20 74 68 65 20 74 72 69 67 67 65 72 once the trigger
5a9f0 0a 2a 2a 20 65 78 69 74 73 2e 20 20 55 70 6f 6e .** exits. Upon
5aa00 20 65 6e 74 65 72 69 6e 67 20 61 20 62 65 66 6f entering a befo
5aa10 72 65 20 6f 72 20 69 6e 73 74 65 61 64 20 6f 66 re or instead of
5aa20 20 74 72 69 67 67 65 72 2c 20 6c 61 73 74 52 6f trigger, lastRo
5aa30 77 69 64 20 69 73 20 6e 6f 0a 2a 2a 20 6c 6f 6e wid is no.** lon
5aa40 67 65 72 20 28 73 69 6e 63 65 20 61 66 74 65 72 ger (since after
5aa50 20 76 65 72 73 69 6f 6e 20 32 2e 38 2e 31 32 29 version 2.8.12)
5aa60 20 72 65 73 65 74 20 74 6f 20 2d 31 2e 0a 2a 2a reset to -1..**
5aa70 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 2e 6e .** The sqlite.n
5aa80 43 68 61 6e 67 65 20 64 6f 65 73 20 6e 6f 74 20 Change does not
5aa90 63 6f 75 6e 74 20 63 68 61 6e 67 65 73 20 77 69 count changes wi
5aaa0 74 68 69 6e 20 74 72 69 67 67 65 72 73 20 61 6e thin triggers an
5aab0 64 20 6b 65 65 70 73 20 6e 6f 0a 2a 2a 20 63 6f d keeps no.** co
5aac0 6e 74 65 78 74 2e 20 20 49 74 20 69 73 20 72 65 ntext. It is re
5aad0 73 65 74 20 61 74 20 73 74 61 72 74 20 6f 66 20 set at start of
5aae0 73 71 6c 69 74 65 33 5f 65 78 65 63 2e 0a 2a 2a sqlite3_exec..**
5aaf0 20 54 68 65 20 73 71 6c 69 74 65 2e 6c 73 43 68 The sqlite.lsCh
5ab00 61 6e 67 65 20 72 65 70 72 65 73 65 6e 74 73 20 ange represents
5ab10 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 the number of ch
5ab20 61 6e 67 65 73 20 6d 61 64 65 20 62 79 20 74 68 anges made by th
5ab30 65 20 6c 61 73 74 0a 2a 2a 20 69 6e 73 65 72 74 e last.** insert
5ab40 2c 20 75 70 64 61 74 65 2c 20 6f 72 20 64 65 6c , update, or del
5ab50 65 74 65 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 ete statement.
5ab60 49 74 20 72 65 6d 61 69 6e 73 20 63 6f 6e 73 74 It remains const
5ab70 61 6e 74 20 74 68 72 6f 75 67 68 6f 75 74 20 74 ant throughout t
5ab80 68 65 0a 2a 2a 20 6c 65 6e 67 74 68 20 6f 66 20 he.** length of
5ab90 61 20 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 a statement and
5aba0 69 73 20 74 68 65 6e 20 75 70 64 61 74 65 64 20 is then updated
5abb0 62 79 20 4f 50 5f 53 65 74 43 6f 75 6e 74 73 2e by OP_SetCounts.
5abc0 20 20 49 74 20 6b 65 65 70 73 20 61 0a 2a 2a 20 It keeps a.**
5abd0 63 6f 6e 74 65 78 74 20 73 74 61 63 6b 20 6a 75 context stack ju
5abe0 73 74 20 6c 69 6b 65 20 6c 61 73 74 52 6f 77 69 st like lastRowi
5abf0 64 20 73 6f 20 74 68 61 74 20 74 68 65 20 63 6f d so that the co
5ac00 75 6e 74 20 6f 66 20 63 68 61 6e 67 65 73 0a 2a unt of changes.*
5ac10 2a 20 77 69 74 68 69 6e 20 61 20 74 72 69 67 67 * within a trigg
5ac20 65 72 20 69 73 20 6e 6f 74 20 73 65 65 6e 20 6f er is not seen o
5ac30 75 74 73 69 64 65 20 74 68 65 20 74 72 69 67 67 utside the trigg
5ac40 65 72 2e 20 20 43 68 61 6e 67 65 73 20 74 6f 20 er. Changes to
5ac50 76 69 65 77 73 20 64 6f 20 6e 6f 74 0a 2a 2a 20 views do not.**
5ac60 61 66 66 65 63 74 20 74 68 65 20 76 61 6c 75 65 affect the value
5ac70 20 6f 66 20 6c 73 43 68 61 6e 67 65 2e 0a 2a 2a of lsChange..**
5ac80 20 54 68 65 20 73 71 6c 69 74 65 2e 63 73 43 68 The sqlite.csCh
5ac90 61 6e 67 65 20 6b 65 65 70 73 20 74 72 61 63 6b ange keeps track
5aca0 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f of the number o
5acb0 66 20 63 75 72 72 65 6e 74 20 63 68 61 6e 67 65 f current change
5acc0 73 20 28 73 69 6e 63 65 0a 2a 2a 20 74 68 65 20 s (since.** the
5acd0 6c 61 73 74 20 73 74 61 74 65 6d 65 6e 74 29 20 last statement)
5ace0 61 6e 64 20 69 73 20 75 73 65 64 20 74 6f 20 75 and is used to u
5acf0 70 64 61 74 65 20 73 71 6c 69 74 65 5f 6c 73 43 pdate sqlite_lsC
5ad00 68 61 6e 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 hange..**.** The
5ad10 20 6d 65 6d 62 65 72 20 76 61 72 69 61 62 6c 65 member variable
5ad20 73 20 73 71 6c 69 74 65 2e 65 72 72 43 6f 64 65 s sqlite.errCode
5ad30 2c 20 73 71 6c 69 74 65 2e 7a 45 72 72 4d 73 67 , sqlite.zErrMsg
5ad40 20 61 6e 64 20 73 71 6c 69 74 65 2e 7a 45 72 72 and sqlite.zErr
5ad50 4d 73 67 31 36 0a 2a 2a 20 73 74 6f 72 65 20 74 Msg16.** store t
5ad60 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 65 he most recent e
5ad70 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 2c 20 69 rror code and, i
5ad80 66 20 61 70 70 6c 69 63 61 62 6c 65 2c 20 73 74 f applicable, st
5ad90 72 69 6e 67 2e 20 54 68 65 0a 2a 2a 20 69 6e 74 ring. The.** int
5ada0 65 72 6e 61 6c 20 66 75 6e 63 74 69 6f 6e 20 73 ernal function s
5adb0 71 6c 69 74 65 33 45 72 72 6f 72 28 29 20 69 73 qlite3Error() is
5adc0 20 75 73 65 64 20 74 6f 20 73 65 74 20 74 68 65 used to set the
5add0 73 65 20 76 61 72 69 61 62 6c 65 73 0a 2a 2a 20 se variables.**
5ade0 63 6f 6e 73 69 73 74 65 6e 74 6c 79 2e 0a 2a 2f consistently..*/
5adf0 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 20 .struct sqlite3
5ae00 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 {. sqlite3_vfs
5ae10 2a 70 56 66 73 3b 20 20 20 20 20 20 20 20 20 20 *pVfs;
5ae20 20 20 2f 2a 20 4f 53 20 49 6e 74 65 72 66 61 63 /* OS Interfac
5ae30 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 62 3b 20 e */. int nDb;
5ae40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5ae50 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
5ae60 66 20 62 61 63 6b 65 6e 64 73 20 63 75 72 72 65 f backends curre
5ae70 6e 74 6c 79 20 69 6e 20 75 73 65 20 2a 2f 0a 20 ntly in use */.
5ae80 20 44 62 20 2a 61 44 62 3b 20 20 20 20 20 20 20 Db *aDb;
5ae90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
5aea0 2a 20 41 6c 6c 20 62 61 63 6b 65 6e 64 73 20 2a * All backends *
5aeb0 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 3b 20 20 /. int flags;
5aec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5aed0 20 20 2f 2a 20 4d 69 73 63 65 6c 6c 61 6e 65 6f /* Miscellaneo
5aee0 75 73 20 66 6c 61 67 73 2e 20 53 65 65 20 62 65 us flags. See be
5aef0 6c 6f 77 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 65 low */. int ope
5af00 6e 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 nFlags;
5af10 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 /* Flags
5af20 70 61 73 73 65 64 20 74 6f 20 73 71 6c 69 74 65 passed to sqlite
5af30 33 5f 76 66 73 2e 78 4f 70 65 6e 28 29 20 2a 2f 3_vfs.xOpen() */
5af40 0a 20 20 69 6e 74 20 65 72 72 43 6f 64 65 3b 20 . int errCode;
5af50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5af60 20 2f 2a 20 4d 6f 73 74 20 72 65 63 65 6e 74 20 /* Most recent
5af70 65 72 72 6f 72 20 63 6f 64 65 20 28 53 51 4c 49 error code (SQLI
5af80 54 45 5f 2a 29 20 2a 2f 0a 20 20 69 6e 74 20 65 TE_*) */. int e
5af90 72 72 4d 61 73 6b 3b 20 20 20 20 20 20 20 20 20 rrMask;
5afa0 20 20 20 20 20 20 20 20 20 2f 2a 20 26 20 72 65 /* & re
5afb0 73 75 6c 74 20 63 6f 64 65 73 20 77 69 74 68 20 sult codes with
5afc0 74 68 69 73 20 62 65 66 6f 72 65 20 72 65 74 75 this before retu
5afd0 72 6e 69 6e 67 20 2a 2f 0a 20 20 75 38 20 61 75 rning */. u8 au
5afe0 74 6f 43 6f 6d 6d 69 74 3b 20 20 20 20 20 20 20 toCommit;
5aff0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
5b000 61 75 74 6f 2d 63 6f 6d 6d 69 74 20 66 6c 61 67 auto-commit flag
5b010 2e 20 2a 2f 0a 20 20 75 38 20 74 65 6d 70 5f 73 . */. u8 temp_s
5b020 74 6f 72 65 3b 20 20 20 20 20 20 20 20 20 20 20 tore;
5b030 20 20 20 20 20 2f 2a 20 31 3a 20 66 69 6c 65 20 /* 1: file
5b040 32 3a 20 6d 65 6d 6f 72 79 20 30 3a 20 64 65 66 2: memory 0: def
5b050 61 75 6c 74 20 2a 2f 0a 20 20 75 38 20 6d 61 6c ault */. u8 mal
5b060 6c 6f 63 46 61 69 6c 65 64 3b 20 20 20 20 20 20 locFailed;
5b070 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
5b080 69 66 20 77 65 20 68 61 76 65 20 73 65 65 6e 20 if we have seen
5b090 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 a malloc failure
5b0a0 20 2a 2f 0a 20 20 75 38 20 64 66 6c 74 4c 6f 63 */. u8 dfltLoc
5b0b0 6b 4d 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20 kMode;
5b0c0 20 20 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 6c /* Default l
5b0d0 6f 63 6b 69 6e 67 2d 6d 6f 64 65 20 66 6f 72 20 ocking-mode for
5b0e0 61 74 74 61 63 68 65 64 20 64 62 73 20 2a 2f 0a attached dbs */.
5b0f0 20 20 75 38 20 64 66 6c 74 4a 6f 75 72 6e 61 6c u8 dfltJournal
5b100 4d 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20 20 Mode;
5b110 2f 2a 20 44 65 66 61 75 6c 74 20 6a 6f 75 72 6e /* Default journ
5b120 61 6c 20 6d 6f 64 65 20 66 6f 72 20 61 74 74 61 al mode for atta
5b130 63 68 65 64 20 64 62 73 20 2a 2f 0a 20 20 73 69 ched dbs */. si
5b140 67 6e 65 64 20 63 68 61 72 20 6e 65 78 74 41 75 gned char nextAu
5b150 74 6f 76 61 63 3b 20 20 20 20 20 20 2f 2a 20 41 tovac; /* A
5b160 75 74 6f 76 61 63 20 73 65 74 74 69 6e 67 20 61 utovac setting a
5b170 66 74 65 72 20 56 41 43 55 55 4d 20 69 66 20 3e fter VACUUM if >
5b180 3d 30 20 2a 2f 0a 20 20 69 6e 74 20 6e 65 78 74 =0 */. int next
5b190 50 61 67 65 73 69 7a 65 3b 20 20 20 20 20 20 20 Pagesize;
5b1a0 20 20 20 20 20 20 2f 2a 20 50 61 67 65 73 69 7a /* Pagesiz
5b1b0 65 20 61 66 74 65 72 20 56 41 43 55 55 4d 20 69 e after VACUUM i
5b1c0 66 20 3e 30 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 f >0 */. int nT
5b1d0 61 62 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20 able;
5b1e0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
5b1f0 72 20 6f 66 20 74 61 62 6c 65 73 20 69 6e 20 74 r of tables in t
5b200 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 he database */.
5b210 20 43 6f 6c 6c 53 65 71 20 2a 70 44 66 6c 74 43 CollSeq *pDfltC
5b220 6f 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 2f oll; /
5b230 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 63 6f * The default co
5b240 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 llating sequence
5b250 20 28 42 49 4e 41 52 59 29 20 2a 2f 0a 20 20 69 (BINARY) */. i
5b260 36 34 20 6c 61 73 74 52 6f 77 69 64 3b 20 20 20 64 lastRowid;
5b270 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
5b280 52 4f 57 49 44 20 6f 66 20 6d 6f 73 74 20 72 65 ROWID of most re
5b290 63 65 6e 74 20 69 6e 73 65 72 74 20 28 73 65 65 cent insert (see
5b2a0 20 61 62 6f 76 65 29 20 2a 2f 0a 20 20 75 33 32 above) */. u32
5b2b0 20 6d 61 67 69 63 3b 20 20 20 20 20 20 20 20 20 magic;
5b2c0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 /* Ma
5b2d0 67 69 63 20 6e 75 6d 62 65 72 20 66 6f 72 20 64 gic number for d
5b2e0 65 74 65 63 74 20 6c 69 62 72 61 72 79 20 6d 69 etect library mi
5b2f0 73 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 suse */. int nC
5b300 68 61 6e 67 65 3b 20 20 20 20 20 20 20 20 20 20 hange;
5b310 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 /* Value
5b320 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c returned by sql
5b330 69 74 65 33 5f 63 68 61 6e 67 65 73 28 29 20 2a ite3_changes() *
5b340 2f 0a 20 20 69 6e 74 20 6e 54 6f 74 61 6c 43 68 /. int nTotalCh
5b350 61 6e 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 ange;
5b360 20 20 2f 2a 20 56 61 6c 75 65 20 72 65 74 75 72 /* Value retur
5b370 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 74 ned by sqlite3_t
5b380 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28 29 20 2a otal_changes() *
5b390 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 /. sqlite3_mute
5b3a0 78 20 2a 6d 75 74 65 78 3b 20 20 20 20 20 20 20 x *mutex;
5b3b0 20 20 2f 2a 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 /* Connection
5b3c0 6d 75 74 65 78 20 2a 2f 0a 20 20 69 6e 74 20 61 mutex */. int a
5b3d0 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4e 5f 4c Limit[SQLITE_N_L
5b3e0 49 4d 49 54 5d 3b 20 20 20 2f 2a 20 4c 69 6d 69 IMIT]; /* Limi
5b3f0 74 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 73 ts */. struct s
5b400 71 6c 69 74 65 33 49 6e 69 74 49 6e 66 6f 20 7b qlite3InitInfo {
5b410 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 /* Informa
5b420 74 69 6f 6e 20 75 73 65 64 20 64 75 72 69 6e 67 tion used during
5b430 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 initialization
5b440 2a 2f 0a 20 20 20 20 69 6e 74 20 69 44 62 3b 20 */. int iDb;
5b450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b460 20 20 20 2f 2a 20 57 68 65 6e 20 62 61 63 6b 20 /* When back
5b470 69 73 20 62 65 69 6e 67 20 69 6e 69 74 69 61 6c is being initial
5b480 69 7a 65 64 20 2a 2f 0a 20 20 20 20 69 6e 74 20 ized */. int
5b490 6e 65 77 54 6e 75 6d 3b 20 20 20 20 20 20 20 20 newTnum;
5b4a0 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74 70 /* Rootp
5b4b0 61 67 65 20 6f 66 20 74 61 62 6c 65 20 62 65 69 age of table bei
5b4c0 6e 67 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a ng initialized *
5b4d0 2f 0a 20 20 20 20 75 38 20 62 75 73 79 3b 20 20 /. u8 busy;
5b4e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b4f0 20 20 2f 2a 20 54 52 55 45 20 69 66 20 63 75 72 /* TRUE if cur
5b500 72 65 6e 74 6c 79 20 69 6e 69 74 69 61 6c 69 7a rently initializ
5b510 69 6e 67 20 2a 2f 0a 20 20 20 20 75 38 20 6f 72 ing */. u8 or
5b520 70 68 61 6e 54 72 69 67 67 65 72 3b 20 20 20 20 phanTrigger;
5b530 20 20 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 73 /* Last s
5b540 74 61 74 65 6d 65 6e 74 20 69 73 20 6f 72 70 68 tatement is orph
5b550 61 6e 65 64 20 54 45 4d 50 20 74 72 69 67 67 65 aned TEMP trigge
5b560 72 20 2a 2f 0a 20 20 7d 20 69 6e 69 74 3b 0a 20 r */. } init;.
5b570 20 69 6e 74 20 6e 45 78 74 65 6e 73 69 6f 6e 3b int nExtension;
5b580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
5b590 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6c 6f 61 64 * Number of load
5b5a0 65 64 20 65 78 74 65 6e 73 69 6f 6e 73 20 2a 2f ed extensions */
5b5b0 0a 20 20 76 6f 69 64 20 2a 2a 61 45 78 74 65 6e . void **aExten
5b5c0 73 69 6f 6e 3b 20 20 20 20 20 20 20 20 20 20 20 sion;
5b5d0 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 73 68 61 /* Array of sha
5b5e0 72 65 64 20 6c 69 62 72 61 72 79 20 68 61 6e 64 red library hand
5b5f0 6c 65 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 les */. struct
5b600 56 64 62 65 20 2a 70 56 64 62 65 3b 20 20 20 20 Vdbe *pVdbe;
5b610 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f /* List o
5b620 66 20 61 63 74 69 76 65 20 76 69 72 74 75 61 6c f active virtual
5b630 20 6d 61 63 68 69 6e 65 73 20 2a 2f 0a 20 20 69 machines */. i
5b640 6e 74 20 61 63 74 69 76 65 56 64 62 65 43 6e 74 nt activeVdbeCnt
5b650 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
5b660 4e 75 6d 62 65 72 20 6f 66 20 56 44 42 45 73 20 Number of VDBEs
5b670 63 75 72 72 65 6e 74 6c 79 20 65 78 65 63 75 74 currently execut
5b680 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 77 72 69 ing */. int wri
5b690 74 65 56 64 62 65 43 6e 74 3b 20 20 20 20 20 20 teVdbeCnt;
5b6a0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
5b6b0 20 6f 66 20 61 63 74 69 76 65 20 56 44 42 45 73 of active VDBEs
5b6c0 20 74 68 61 74 20 61 72 65 20 77 72 69 74 69 6e that are writin
5b6d0 67 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 54 g */. void (*xT
5b6e0 72 61 63 65 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 race)(void*,cons
5b6f0 74 20 63 68 61 72 2a 29 3b 20 20 20 20 20 20 20 t char*);
5b700 20 2f 2a 20 54 72 61 63 65 20 66 75 6e 63 74 69 /* Trace functi
5b710 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 54 on */. void *pT
5b720 72 61 63 65 41 72 67 3b 20 20 20 20 20 20 20 20 raceArg;
5b730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b740 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f /* Argument to
5b750 20 74 68 65 20 74 72 61 63 65 20 66 75 6e 63 74 the trace funct
5b760 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a ion */. void (*
5b770 78 50 72 6f 66 69 6c 65 29 28 76 6f 69 64 2a 2c xProfile)(void*,
5b780 63 6f 6e 73 74 20 63 68 61 72 2a 2c 75 36 34 29 const char*,u64)
5b790 3b 20 20 2f 2a 20 50 72 6f 66 69 6c 69 6e 67 20 ; /* Profiling
5b7a0 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f function */. vo
5b7b0 69 64 20 2a 70 50 72 6f 66 69 6c 65 41 72 67 3b id *pProfileArg;
5b7c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b7d0 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d /* Argum
5b7e0 65 6e 74 20 74 6f 20 70 72 6f 66 69 6c 65 20 66 ent to profile f
5b7f0 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 unction */. voi
5b800 64 20 2a 70 43 6f 6d 6d 69 74 41 72 67 3b 20 20 d *pCommitArg;
5b810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
5b820 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 78 43 * Argument to xC
5b830 6f 6d 6d 69 74 43 61 6c 6c 62 61 63 6b 28 29 20 ommitCallback()
5b840 2a 2f 20 20 20 0a 20 20 69 6e 74 20 28 2a 78 43 */ . int (*xC
5b850 6f 6d 6d 69 74 43 61 6c 6c 62 61 63 6b 29 28 76 ommitCallback)(v
5b860 6f 69 64 2a 29 3b 20 20 20 20 2f 2a 20 49 6e 76 oid*); /* Inv
5b870 6f 6b 65 64 20 61 74 20 65 76 65 72 79 20 63 6f oked at every co
5b880 6d 6d 69 74 2e 20 2a 2f 0a 20 20 76 6f 69 64 20 mmit. */. void
5b890 2a 70 52 6f 6c 6c 62 61 63 6b 41 72 67 3b 20 20 *pRollbackArg;
5b8a0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
5b8b0 41 72 67 75 6d 65 6e 74 20 74 6f 20 78 52 6f 6c Argument to xRol
5b8c0 6c 62 61 63 6b 43 61 6c 6c 62 61 63 6b 28 29 20 lbackCallback()
5b8d0 2a 2f 20 20 20 0a 20 20 76 6f 69 64 20 28 2a 78 */ . void (*x
5b8e0 52 6f 6c 6c 62 61 63 6b 43 61 6c 6c 62 61 63 6b RollbackCallback
5b8f0 29 28 76 6f 69 64 2a 29 3b 20 2f 2a 20 49 6e 76 )(void*); /* Inv
5b900 6f 6b 65 64 20 61 74 20 65 76 65 72 79 20 63 6f oked at every co
5b910 6d 6d 69 74 2e 20 2a 2f 0a 20 20 76 6f 69 64 20 mmit. */. void
5b920 2a 70 55 70 64 61 74 65 41 72 67 3b 0a 20 20 76 *pUpdateArg;. v
5b930 6f 69 64 20 28 2a 78 55 70 64 61 74 65 43 61 6c oid (*xUpdateCal
5b940 6c 62 61 63 6b 29 28 76 6f 69 64 2a 2c 69 6e 74 lback)(void*,int
5b950 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f , const char*,co
5b960 6e 73 74 20 63 68 61 72 2a 2c 73 71 6c 69 74 65 nst char*,sqlite
5b970 5f 69 6e 74 36 34 29 3b 0a 20 20 76 6f 69 64 28 _int64);. void(
5b980 2a 78 43 6f 6c 6c 4e 65 65 64 65 64 29 28 76 6f *xCollNeeded)(vo
5b990 69 64 2a 2c 73 71 6c 69 74 65 33 2a 2c 69 6e 74 id*,sqlite3*,int
5b9a0 20 65 54 65 78 74 52 65 70 2c 63 6f 6e 73 74 20 eTextRep,const
5b9b0 63 68 61 72 2a 29 3b 0a 20 20 76 6f 69 64 28 2a char*);. void(*
5b9c0 78 43 6f 6c 6c 4e 65 65 64 65 64 31 36 29 28 76 xCollNeeded16)(v
5b9d0 6f 69 64 2a 2c 73 71 6c 69 74 65 33 2a 2c 69 6e oid*,sqlite3*,in
5b9e0 74 20 65 54 65 78 74 52 65 70 2c 63 6f 6e 73 74 t eTextRep,const
5b9f0 20 76 6f 69 64 2a 29 3b 0a 20 20 76 6f 69 64 20 void*);. void
5ba00 2a 70 43 6f 6c 6c 4e 65 65 64 65 64 41 72 67 3b *pCollNeededArg;
5ba10 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 . sqlite3_value
5ba20 20 2a 70 45 72 72 3b 20 20 20 20 20 20 20 20 20 *pErr;
5ba30 20 2f 2a 20 4d 6f 73 74 20 72 65 63 65 6e 74 20 /* Most recent
5ba40 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 2a 2f error message */
5ba50 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 4d 73 67 . char *zErrMsg
5ba60 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
5ba70 20 2f 2a 20 4d 6f 73 74 20 72 65 63 65 6e 74 20 /* Most recent
5ba80 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 28 55 error message (U
5ba90 54 46 2d 38 20 65 6e 63 6f 64 65 64 29 20 2a 2f TF-8 encoded) */
5baa0 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 4d 73 67 . char *zErrMsg
5bab0 31 36 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 16;
5bac0 20 2f 2a 20 4d 6f 73 74 20 72 65 63 65 6e 74 20 /* Most recent
5bad0 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 28 55 error message (U
5bae0 54 46 2d 31 36 20 65 6e 63 6f 64 65 64 29 20 2a TF-16 encoded) *
5baf0 2f 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 20 /. union {.
5bb00 76 6f 6c 61 74 69 6c 65 20 69 6e 74 20 69 73 49 volatile int isI
5bb10 6e 74 65 72 72 75 70 74 65 64 3b 20 2f 2a 20 54 nterrupted; /* T
5bb20 72 75 65 20 69 66 20 73 71 6c 69 74 65 33 5f 69 rue if sqlite3_i
5bb30 6e 74 65 72 72 75 70 74 20 68 61 73 20 62 65 65 nterrupt has bee
5bb40 6e 20 63 61 6c 6c 65 64 20 2a 2f 0a 20 20 20 20 n called */.
5bb50 64 6f 75 62 6c 65 20 6e 6f 74 55 73 65 64 31 3b double notUsed1;
5bb60 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 /* S
5bb70 70 61 63 65 72 20 2a 2f 0a 20 20 7d 20 75 31 3b pacer */. } u1;
5bb80 0a 20 20 4c 6f 6f 6b 61 73 69 64 65 20 6c 6f 6f . Lookaside loo
5bb90 6b 61 73 69 64 65 3b 20 20 20 20 20 20 20 20 20 kaside;
5bba0 20 2f 2a 20 4c 6f 6f 6b 61 73 69 64 65 20 6d 61 /* Lookaside ma
5bbb0 6c 6c 6f 63 20 63 6f 6e 66 69 67 75 72 61 74 69 lloc configurati
5bbc0 6f 6e 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 on */.#ifndef SQ
5bbd0 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 LITE_OMIT_AUTHOR
5bbe0 49 5a 41 54 49 4f 4e 0a 20 20 69 6e 74 20 28 2a IZATION. int (*
5bbf0 78 41 75 74 68 29 28 76 6f 69 64 2a 2c 69 6e 74 xAuth)(void*,int
5bc00 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e ,const char*,con
5bc10 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 st char*,const c
5bc20 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a har*,const char*
5bc30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 );.
5bc40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5bc50 20 20 20 2f 2a 20 41 63 63 65 73 73 20 61 75 74 /* Access aut
5bc60 68 6f 72 69 7a 61 74 69 6f 6e 20 66 75 6e 63 74 horization funct
5bc70 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 ion */. void *p
5bc80 41 75 74 68 41 72 67 3b 20 20 20 20 20 20 20 20 AuthArg;
5bc90 20 20 20 20 20 20 20 2f 2a 20 31 73 74 20 61 72 /* 1st ar
5bca0 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 61 63 gument to the ac
5bcb0 63 65 73 73 20 61 75 74 68 20 66 75 6e 63 74 69 cess auth functi
5bcc0 6f 6e 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 on */.#endif.#if
5bcd0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
5bce0 5f 50 52 4f 47 52 45 53 53 5f 43 41 4c 4c 42 41 _PROGRESS_CALLBA
5bcf0 43 4b 0a 20 20 69 6e 74 20 28 2a 78 50 72 6f 67 CK. int (*xProg
5bd00 72 65 73 73 29 28 76 6f 69 64 20 2a 29 3b 20 20 ress)(void *);
5bd10 20 20 20 2f 2a 20 54 68 65 20 70 72 6f 67 72 65 /* The progre
5bd20 73 73 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 ss callback */.
5bd30 20 76 6f 69 64 20 2a 70 50 72 6f 67 72 65 73 73 void *pProgress
5bd40 41 72 67 3b 20 20 20 20 20 20 20 20 20 20 20 2f Arg; /
5bd50 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 * Argument to th
5bd60 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 e progress callb
5bd70 61 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 72 ack */. int nPr
5bd80 6f 67 72 65 73 73 4f 70 73 3b 20 20 20 20 20 20 ogressOps;
5bd90 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
5bda0 20 6f 66 20 6f 70 63 6f 64 65 73 20 66 6f 72 20 of opcodes for
5bdb0 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 63 progress callbac
5bdc0 6b 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 6e k */.#endif.#ifn
5bdd0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
5bde0 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 48 VIRTUALTABLE. H
5bdf0 61 73 68 20 61 4d 6f 64 75 6c 65 3b 20 20 20 20 ash aModule;
5be00 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
5be10 70 6f 70 75 6c 61 74 65 64 20 62 79 20 73 71 6c populated by sql
5be20 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 ite3_create_modu
5be30 6c 65 28 29 20 2a 2f 0a 20 20 54 61 62 6c 65 20 le() */. Table
5be40 2a 70 56 54 61 62 3b 20 20 20 20 20 20 20 20 20 *pVTab;
5be50 20 20 20 20 20 20 20 20 2f 2a 20 76 74 61 62 20 /* vtab
5be60 77 69 74 68 20 61 63 74 69 76 65 20 43 6f 6e 6e with active Conn
5be70 65 63 74 2f 43 72 65 61 74 65 20 6d 65 74 68 6f ect/Create metho
5be80 64 20 2a 2f 0a 20 20 56 54 61 62 6c 65 20 2a 2a d */. VTable **
5be90 61 56 54 72 61 6e 73 3b 20 20 20 20 20 20 20 20 aVTrans;
5bea0 20 20 20 20 20 2f 2a 20 56 69 72 74 75 61 6c 20 /* Virtual
5beb0 74 61 62 6c 65 73 20 77 69 74 68 20 6f 70 65 6e tables with open
5bec0 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 2a 2f transactions */
5bed0 0a 20 20 69 6e 74 20 6e 56 54 72 61 6e 73 3b 20 . int nVTrans;
5bee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5bef0 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 64 20 73 69 /* Allocated si
5bf00 7a 65 20 6f 66 20 61 56 54 72 61 6e 73 20 2a 2f ze of aVTrans */
5bf10 0a 20 20 56 54 61 62 6c 65 20 2a 70 44 69 73 63 . VTable *pDisc
5bf20 6f 6e 6e 65 63 74 3b 20 20 20 20 2f 2a 20 44 69 onnect; /* Di
5bf30 73 63 6f 6e 6e 65 63 74 20 74 68 65 73 65 20 69 sconnect these i
5bf40 6e 20 6e 65 78 74 20 73 71 6c 69 74 65 33 5f 70 n next sqlite3_p
5bf50 72 65 70 61 72 65 28 29 20 2a 2f 0a 23 65 6e 64 repare() */.#end
5bf60 69 66 0a 20 20 46 75 6e 63 44 65 66 48 61 73 68 if. FuncDefHash
5bf70 20 61 46 75 6e 63 3b 20 20 20 20 20 20 20 20 20 aFunc;
5bf80 20 20 20 2f 2a 20 48 61 73 68 20 74 61 62 6c 65 /* Hash table
5bf90 20 6f 66 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 66 of connection f
5bfa0 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 20 20 48 61 unctions */. Ha
5bfb0 73 68 20 61 43 6f 6c 6c 53 65 71 3b 20 20 20 20 sh aCollSeq;
5bfc0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 /* A
5bfd0 6c 6c 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 ll collating seq
5bfe0 75 65 6e 63 65 73 20 2a 2f 0a 20 20 42 75 73 79 uences */. Busy
5bff0 48 61 6e 64 6c 65 72 20 62 75 73 79 48 61 6e 64 Handler busyHand
5c000 6c 65 72 3b 20 20 20 20 20 20 2f 2a 20 42 75 73 ler; /* Bus
5c010 79 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 y callback */.
5c020 69 6e 74 20 62 75 73 79 54 69 6d 65 6f 75 74 3b int busyTimeout;
5c030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
5c040 20 42 75 73 79 20 68 61 6e 64 6c 65 72 20 74 69 Busy handler ti
5c050 6d 65 6f 75 74 2c 20 69 6e 20 6d 73 65 63 20 2a meout, in msec *
5c060 2f 0a 20 20 44 62 20 61 44 62 53 74 61 74 69 63 /. Db aDbStatic
5c070 5b 32 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20 [2];
5c080 20 20 2f 2a 20 53 74 61 74 69 63 20 73 70 61 63 /* Static spac
5c090 65 20 66 6f 72 20 74 68 65 20 32 20 64 65 66 61 e for the 2 defa
5c0a0 75 6c 74 20 62 61 63 6b 65 6e 64 73 20 2a 2f 0a ult backends */.
5c0b0 20 20 53 61 76 65 70 6f 69 6e 74 20 2a 70 53 61 Savepoint *pSa
5c0c0 76 65 70 6f 69 6e 74 3b 20 20 20 20 20 20 20 20 vepoint;
5c0d0 2f 2a 20 4c 69 73 74 20 6f 66 20 61 63 74 69 76 /* List of activ
5c0e0 65 20 73 61 76 65 70 6f 69 6e 74 73 20 2a 2f 0a e savepoints */.
5c0f0 20 20 69 6e 74 20 6e 53 61 76 65 70 6f 69 6e 74 int nSavepoint
5c100 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
5c110 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 6f 6e /* Number of non
5c120 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 73 61 76 -transaction sav
5c130 65 70 6f 69 6e 74 73 20 2a 2f 0a 20 20 69 6e 74 epoints */. int
5c140 20 6e 53 74 61 74 65 6d 65 6e 74 3b 20 20 20 20 nStatement;
5c150 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
5c160 6d 62 65 72 20 6f 66 20 6e 65 73 74 65 64 20 73 mber of nested s
5c170 74 61 74 65 6d 65 6e 74 2d 74 72 61 6e 73 61 63 tatement-transac
5c180 74 69 6f 6e 73 20 20 2a 2f 0a 20 20 75 38 20 69 tions */. u8 i
5c190 73 54 72 61 6e 73 61 63 74 69 6f 6e 53 61 76 65 sTransactionSave
5c1a0 70 6f 69 6e 74 3b 20 20 20 20 2f 2a 20 54 72 75 point; /* Tru
5c1b0 65 20 69 66 20 74 68 65 20 6f 75 74 65 72 6d 6f e if the outermo
5c1c0 73 74 20 73 61 76 65 70 6f 69 6e 74 20 69 73 20 st savepoint is
5c1d0 61 20 54 53 20 2a 2f 0a 20 20 69 36 34 20 6e 44 a TS */. i64 nD
5c1e0 65 66 65 72 72 65 64 43 6f 6e 73 3b 20 20 20 20 eferredCons;
5c1f0 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 74 20 64 /* Net d
5c200 65 66 65 72 72 65 64 20 63 6f 6e 73 74 72 61 69 eferred constrai
5c210 6e 74 73 20 74 68 69 73 20 74 72 61 6e 73 61 63 nts this transac
5c220 74 69 6f 6e 2e 20 2a 2f 0a 0a 23 69 66 64 65 66 tion. */..#ifdef
5c230 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 55 SQLITE_ENABLE_U
5c240 4e 4c 4f 43 4b 5f 4e 4f 54 49 46 59 0a 20 20 2f NLOCK_NOTIFY. /
5c250 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * The following
5c260 76 61 72 69 61 62 6c 65 73 20 61 72 65 20 61 6c variables are al
5c270 6c 20 70 72 6f 74 65 63 74 65 64 20 62 79 20 74 l protected by t
5c280 68 65 20 53 54 41 54 49 43 5f 4d 41 53 54 45 52 he STATIC_MASTER
5c290 20 0a 20 20 2a 2a 20 6d 75 74 65 78 2c 20 6e 6f . ** mutex, no
5c2a0 74 20 62 79 20 73 71 6c 69 74 65 33 2e 6d 75 74 t by sqlite3.mut
5c2b0 65 78 2e 20 54 68 65 79 20 61 72 65 20 75 73 65 ex. They are use
5c2c0 64 20 62 79 20 63 6f 64 65 20 69 6e 20 6e 6f 74 d by code in not
5c2d0 69 66 79 2e 63 2e 20 0a 20 20 2a 2a 0a 20 20 2a ify.c. . **. *
5c2e0 2a 20 57 68 65 6e 20 58 2e 70 55 6e 6c 6f 63 6b * When X.pUnlock
5c2f0 43 6f 6e 6e 65 63 74 69 6f 6e 3d 3d 59 2c 20 74 Connection==Y, t
5c300 68 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20 58 hat means that X
5c310 20 69 73 20 77 61 69 74 69 6e 67 20 66 6f 72 20 is waiting for
5c320 59 20 74 6f 0a 20 20 2a 2a 20 75 6e 6c 6f 63 6b Y to. ** unlock
5c330 20 73 6f 20 74 68 61 74 20 69 74 20 63 61 6e 20 so that it can
5c340 70 72 6f 63 65 65 64 2e 0a 20 20 2a 2a 0a 20 20 proceed.. **.
5c350 2a 2a 20 57 68 65 6e 20 58 2e 70 42 6c 6f 63 6b ** When X.pBlock
5c360 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e 3d 3d 59 ingConnection==Y
5c370 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 61 , that means tha
5c380 74 20 73 6f 6d 65 74 68 69 6e 67 20 74 68 61 74 t something that
5c390 20 58 20 74 72 69 65 64 0a 20 20 2a 2a 20 74 72 X tried. ** tr
5c3a0 69 65 64 20 74 6f 20 64 6f 20 72 65 63 65 6e 74 ied to do recent
5c3b0 6c 79 20 66 61 69 6c 65 64 20 77 69 74 68 20 61 ly failed with a
5c3c0 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 n SQLITE_LOCKED
5c3d0 65 72 72 6f 72 20 64 75 65 20 74 6f 20 6c 6f 63 error due to loc
5c3e0 6b 73 0a 20 20 2a 2a 20 68 65 6c 64 20 62 79 20 ks. ** held by
5c3f0 59 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 Y.. */. sqlite
5c400 33 20 2a 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 3 *pBlockingConn
5c410 65 63 74 69 6f 6e 3b 20 2f 2a 20 43 6f 6e 6e 65 ection; /* Conne
5c420 63 74 69 6f 6e 20 74 68 61 74 20 63 61 75 73 65 ction that cause
5c430 64 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 d SQLITE_LOCKED
5c440 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 70 55 */. sqlite3 *pU
5c450 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e 3b nlockConnection;
5c460 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f /* Co
5c470 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 77 61 74 63 nnection to watc
5c480 68 20 66 6f 72 20 75 6e 6c 6f 63 6b 20 2a 2f 0a h for unlock */.
5c490 20 20 76 6f 69 64 20 2a 70 55 6e 6c 6f 63 6b 41 void *pUnlockA
5c4a0 72 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 rg;
5c4b0 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d /* Argum
5c4c0 65 6e 74 20 74 6f 20 78 55 6e 6c 6f 63 6b 4e 6f ent to xUnlockNo
5c4d0 74 69 66 79 20 2a 2f 0a 20 20 76 6f 69 64 20 28 tify */. void (
5c4e0 2a 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79 29 28 *xUnlockNotify)(
5c4f0 76 6f 69 64 20 2a 2a 2c 20 69 6e 74 29 3b 20 20 void **, int);
5c500 2f 2a 20 55 6e 6c 6f 63 6b 20 6e 6f 74 69 66 79 /* Unlock notify
5c510 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 73 callback */. s
5c520 71 6c 69 74 65 33 20 2a 70 4e 65 78 74 42 6c 6f qlite3 *pNextBlo
5c530 63 6b 65 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 cked; /*
5c540 4e 65 78 74 20 69 6e 20 6c 69 73 74 20 6f 66 20 Next in list of
5c550 61 6c 6c 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e all blocked conn
5c560 65 63 74 69 6f 6e 73 20 2a 2f 0a 23 65 6e 64 69 ections */.#endi
5c570 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 6d 61 f.};../*.** A ma
5c580 63 72 6f 20 74 6f 20 64 69 73 63 6f 76 65 72 20 cro to discover
5c590 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 the encoding of
5c5a0 61 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a 23 a database..*/.#
5c5b0 64 65 66 69 6e 65 20 45 4e 43 28 64 62 29 20 28 define ENC(db) (
5c5c0 28 64 62 29 2d 3e 61 44 62 5b 30 5d 2e 70 53 63 (db)->aDb[0].pSc
5c5d0 68 65 6d 61 2d 3e 65 6e 63 29 0a 0a 2f 2a 0a 2a hema->enc)../*.*
5c5e0 2a 20 50 6f 73 73 69 62 6c 65 20 76 61 6c 75 65 * Possible value
5c5f0 73 20 66 6f 72 20 74 68 65 20 73 71 6c 69 74 65 s for the sqlite
5c600 2e 66 6c 61 67 73 20 61 6e 64 20 6f 72 20 44 62 .flags and or Db
5c610 2e 66 6c 61 67 73 20 66 69 65 6c 64 73 2e 0a 2a .flags fields..*
5c620 2a 0a 2a 2a 20 4f 6e 20 73 71 6c 69 74 65 2e 66 *.** On sqlite.f
5c630 6c 61 67 73 2c 20 74 68 65 20 53 51 4c 49 54 45 lags, the SQLITE
5c640 5f 49 6e 54 72 61 6e 73 20 76 61 6c 75 65 20 6d _InTrans value m
5c650 65 61 6e 73 20 74 68 61 74 20 77 65 20 68 61 76 eans that we hav
5c660 65 0a 2a 2a 20 65 78 65 63 75 74 65 64 20 61 20 e.** executed a
5c670 42 45 47 49 4e 2e 20 20 4f 6e 20 44 62 2e 66 6c BEGIN. On Db.fl
5c680 61 67 73 2c 20 53 51 4c 49 54 45 5f 49 6e 54 72 ags, SQLITE_InTr
5c690 61 6e 73 20 6d 65 61 6e 73 20 61 20 73 74 61 74 ans means a stat
5c6a0 65 6d 65 6e 74 0a 2a 2a 20 74 72 61 6e 73 61 63 ement.** transac
5c6b0 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65 20 6f tion is active o
5c6c0 6e 20 74 68 61 74 20 70 61 72 74 69 63 75 6c 61 n that particula
5c6d0 72 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e r database file.
5c6e0 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 .*/.#define SQLI
5c6f0 54 45 5f 56 64 62 65 54 72 61 63 65 20 20 20 20 TE_VdbeTrace
5c700 20 20 30 78 30 30 30 30 30 30 30 31 20 20 2f 2a 0x00000001 /*
5c710 20 54 72 75 65 20 74 6f 20 74 72 61 63 65 20 56 True to trace V
5c720 44 42 45 20 65 78 65 63 75 74 69 6f 6e 20 2a 2f DBE execution */
5c730 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
5c740 49 6e 54 72 61 6e 73 20 20 20 20 20 20 20 20 30 InTrans 0
5c750 78 30 30 30 30 30 30 30 38 20 20 2f 2a 20 54 72 x00000008 /* Tr
5c760 75 65 20 69 66 20 69 6e 20 61 20 74 72 61 6e 73 ue if in a trans
5c770 61 63 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e action */.#defin
5c780 65 20 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 e SQLITE_InternC
5c790 68 61 6e 67 65 73 20 20 30 78 30 30 30 30 30 30 hanges 0x000000
5c7a0 31 30 20 20 2f 2a 20 55 6e 63 6f 6d 6d 69 74 74 10 /* Uncommitt
5c7b0 65 64 20 48 61 73 68 20 74 61 62 6c 65 20 63 68 ed Hash table ch
5c7c0 61 6e 67 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 anges */.#define
5c7d0 20 53 51 4c 49 54 45 5f 46 75 6c 6c 43 6f 6c 4e SQLITE_FullColN
5c7e0 61 6d 65 73 20 20 20 30 78 30 30 30 30 30 30 32 ames 0x0000002
5c7f0 30 20 20 2f 2a 20 53 68 6f 77 20 66 75 6c 6c 20 0 /* Show full
5c800 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 6f 6e 20 column names on
5c810 53 45 4c 45 43 54 20 2a 2f 0a 23 64 65 66 69 6e SELECT */.#defin
5c820 65 20 53 51 4c 49 54 45 5f 53 68 6f 72 74 43 6f e SQLITE_ShortCo
5c830 6c 4e 61 6d 65 73 20 20 30 78 30 30 30 30 30 30 lNames 0x000000
5c840 34 30 20 20 2f 2a 20 53 68 6f 77 20 73 68 6f 72 40 /* Show shor
5c850 74 20 63 6f 6c 75 6d 6e 73 20 6e 61 6d 65 73 20 t columns names
5c860 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
5c870 45 5f 43 6f 75 6e 74 52 6f 77 73 20 20 20 20 20 E_CountRows
5c880 20 30 78 30 30 30 30 30 30 38 30 20 20 2f 2a 20 0x00000080 /*
5c890 43 6f 75 6e 74 20 72 6f 77 73 20 63 68 61 6e 67 Count rows chang
5c8a0 65 64 20 62 79 20 49 4e 53 45 52 54 2c 20 2a 2f ed by INSERT, */
5c8b0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
5c8c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5c8d0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 20 20 /*
5c8e0 44 45 4c 45 54 45 2c 20 6f 72 20 55 50 44 41 54 DELETE, or UPDAT
5c8f0 45 20 61 6e 64 20 72 65 74 75 72 6e 20 2a 2f 0a E and return */.
5c900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5c910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5c920 20 20 20 20 20 20 20 20 20 20 2f 2a 20 20 20 74 /* t
5c930 68 65 20 63 6f 75 6e 74 20 75 73 69 6e 67 20 61 he count using a
5c940 20 63 61 6c 6c 62 61 63 6b 2e 20 2a 2f 0a 23 64 callback. */.#d
5c950 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 75 6c efine SQLITE_Nul
5c960 6c 43 61 6c 6c 62 61 63 6b 20 20 20 30 78 30 30 lCallback 0x00
5c970 30 30 30 31 30 30 20 20 2f 2a 20 49 6e 76 6f 6b 000100 /* Invok
5c980 65 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 6f e the callback o
5c990 6e 63 65 20 69 66 20 74 68 65 20 2a 2f 0a 20 20 nce if the */.
5c9a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5c9b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5c9c0 20 20 20 20 20 20 20 20 2f 2a 20 20 20 72 65 73 /* res
5c9d0 75 6c 74 20 73 65 74 20 69 73 20 65 6d 70 74 79 ult set is empty
5c9e0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
5c9f0 54 45 5f 53 71 6c 54 72 61 63 65 20 20 20 20 20 TE_SqlTrace
5ca00 20 20 30 78 30 30 30 30 30 32 30 30 20 20 2f 2a 0x00000200 /*
5ca10 20 44 65 62 75 67 20 70 72 69 6e 74 20 53 51 4c Debug print SQL
5ca20 20 61 73 20 69 74 20 65 78 65 63 75 74 65 73 20 as it executes
5ca30 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
5ca40 45 5f 56 64 62 65 4c 69 73 74 69 6e 67 20 20 20 E_VdbeListing
5ca50 20 30 78 30 30 30 30 30 34 30 30 20 20 2f 2a 20 0x00000400 /*
5ca60 44 65 62 75 67 20 6c 69 73 74 69 6e 67 73 20 6f Debug listings o
5ca70 66 20 56 44 42 45 20 70 72 6f 67 72 61 6d 73 20 f VDBE programs
5ca80 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
5ca90 45 5f 57 72 69 74 65 53 63 68 65 6d 61 20 20 20 E_WriteSchema
5caa0 20 30 78 30 30 30 30 30 38 30 30 20 20 2f 2a 20 0x00000800 /*
5cab0 4f 4b 20 74 6f 20 75 70 64 61 74 65 20 53 51 4c OK to update SQL
5cac0 49 54 45 5f 4d 41 53 54 45 52 20 2a 2f 0a 23 64 ITE_MASTER */.#d
5cad0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 6f 52 efine SQLITE_NoR
5cae0 65 61 64 6c 6f 63 6b 20 20 20 20 20 30 78 30 30 eadlock 0x00
5caf0 30 30 31 30 30 30 20 20 2f 2a 20 52 65 61 64 6c 001000 /* Readl
5cb00 6f 63 6b 73 20 61 72 65 20 6f 6d 69 74 74 65 64 ocks are omitted
5cb10 20 77 68 65 6e 20 0a 20 20 20 20 20 20 20 20 20 when .
5cb20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5cb30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5cb40 20 2a 2a 20 61 63 63 65 73 73 69 6e 67 20 72 65 ** accessing re
5cb50 61 64 2d 6f 6e 6c 79 20 64 61 74 61 62 61 73 65 ad-only database
5cb60 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c s */.#define SQL
5cb70 49 54 45 5f 49 67 6e 6f 72 65 43 68 65 63 6b 73 ITE_IgnoreChecks
5cb80 20 20 20 30 78 30 30 30 30 32 30 30 30 20 20 2f 0x00002000 /
5cb90 2a 20 44 6f 20 6e 6f 74 20 65 6e 66 6f 72 63 65 * Do not enforce
5cba0 20 63 68 65 63 6b 20 63 6f 6e 73 74 72 61 69 6e check constrain
5cbb0 74 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 ts */.#define SQ
5cbc0 4c 49 54 45 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 LITE_ReadUncommi
5cbd0 74 74 65 64 20 30 78 30 30 30 30 34 30 30 30 20 tted 0x00004000
5cbe0 2f 2a 20 46 6f 72 20 73 68 61 72 65 64 2d 63 61 /* For shared-ca
5cbf0 63 68 65 20 6d 6f 64 65 20 2a 2f 0a 23 64 65 66 che mode */.#def
5cc00 69 6e 65 20 53 51 4c 49 54 45 5f 4c 65 67 61 63 ine SQLITE_Legac
5cc10 79 46 69 6c 65 46 6d 74 20 20 30 78 30 30 30 30 yFileFmt 0x0000
5cc20 38 30 30 30 20 20 2f 2a 20 43 72 65 61 74 65 20 8000 /* Create
5cc30 6e 65 77 20 64 61 74 61 62 61 73 65 73 20 69 6e new databases in
5cc40 20 66 6f 72 6d 61 74 20 31 20 2a 2f 0a 23 64 65 format 1 */.#de
5cc50 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 75 6c 6c fine SQLITE_Full
5cc60 46 53 79 6e 63 20 20 20 20 20 20 30 78 30 30 30 FSync 0x000
5cc70 31 30 30 30 30 20 20 2f 2a 20 55 73 65 20 66 75 10000 /* Use fu
5cc80 6c 6c 20 66 73 79 6e 63 20 6f 6e 20 74 68 65 20 ll fsync on the
5cc90 62 61 63 6b 65 6e 64 20 2a 2f 0a 23 64 65 66 69 backend */.#defi
5cca0 6e 65 20 53 51 4c 49 54 45 5f 4c 6f 61 64 45 78 ne SQLITE_LoadEx
5ccb0 74 65 6e 73 69 6f 6e 20 20 30 78 30 30 30 32 30 tension 0x00020
5ccc0 30 30 30 20 20 2f 2a 20 45 6e 61 62 6c 65 20 6c 000 /* Enable l
5ccd0 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 20 2a 2f oad_extension */
5cce0 0a 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 ..#define SQLITE
5ccf0 5f 52 65 63 6f 76 65 72 79 4d 6f 64 65 20 20 20 _RecoveryMode
5cd00 30 78 30 30 30 34 30 30 30 30 20 20 2f 2a 20 49 0x00040000 /* I
5cd10 67 6e 6f 72 65 20 73 63 68 65 6d 61 20 65 72 72 gnore schema err
5cd20 6f 72 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 ors */.#define S
5cd30 51 4c 49 54 45 5f 52 65 76 65 72 73 65 4f 72 64 QLITE_ReverseOrd
5cd40 65 72 20 20 20 30 78 30 30 31 30 30 30 30 30 20 er 0x00100000
5cd50 20 2f 2a 20 52 65 76 65 72 73 65 20 75 6e 6f 72 /* Reverse unor
5cd60 64 65 72 65 64 20 53 45 4c 45 43 54 73 20 2a 2f dered SELECTs */
5cd70 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
5cd80 52 65 63 54 72 69 67 67 65 72 73 20 20 20 20 30 RecTriggers 0
5cd90 78 30 30 32 30 30 30 30 30 20 20 2f 2a 20 45 6e x00200000 /* En
5cda0 61 62 6c 65 20 72 65 63 75 72 73 69 76 65 20 74 able recursive t
5cdb0 72 69 67 67 65 72 73 20 2a 2f 0a 23 64 65 66 69 riggers */.#defi
5cdc0 6e 65 20 53 51 4c 49 54 45 5f 46 6f 72 65 69 67 ne SQLITE_Foreig
5cdd0 6e 4b 65 79 73 20 20 20 20 30 78 30 30 34 30 30 nKeys 0x00400
5cde0 30 30 30 20 20 2f 2a 20 45 6e 66 6f 72 63 65 20 000 /* Enforce
5cdf0 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 foreign key cons
5ce00 74 72 61 69 6e 74 73 20 20 2a 2f 0a 0a 2f 2a 0a traints */../*.
5ce10 2a 2a 20 50 6f 73 73 69 62 6c 65 20 76 61 6c 75 ** Possible valu
5ce20 65 73 20 66 6f 72 20 74 68 65 20 73 71 6c 69 74 es for the sqlit
5ce30 65 2e 6d 61 67 69 63 20 66 69 65 6c 64 2e 0a 2a e.magic field..*
5ce40 2a 20 54 68 65 20 6e 75 6d 62 65 72 73 20 61 72 * The numbers ar
5ce50 65 20 6f 62 74 61 69 6e 65 64 20 61 74 20 72 61 e obtained at ra
5ce60 6e 64 6f 6d 20 61 6e 64 20 68 61 76 65 20 6e 6f ndom and have no
5ce70 20 73 70 65 63 69 61 6c 20 6d 65 61 6e 69 6e 67 special meaning
5ce80 2c 20 6f 74 68 65 72 0a 2a 2a 20 74 68 61 6e 20 , other.** than
5ce90 62 65 69 6e 67 20 64 69 73 74 69 6e 63 74 20 66 being distinct f
5cea0 72 6f 6d 20 6f 6e 65 20 61 6e 6f 74 68 65 72 2e rom one another.
5ceb0 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 .*/.#define SQLI
5cec0 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 20 20 20 TE_MAGIC_OPEN
5ced0 20 20 30 78 61 30 32 39 61 36 39 37 20 20 2f 2a 0xa029a697 /*
5cee0 20 44 61 74 61 62 61 73 65 20 69 73 20 6f 70 65 Database is ope
5cef0 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c n */.#define SQL
5cf00 49 54 45 5f 4d 41 47 49 43 5f 43 4c 4f 53 45 44 ITE_MAGIC_CLOSED
5cf10 20 20 20 30 78 39 66 33 63 32 64 33 33 20 20 2f 0x9f3c2d33 /
5cf20 2a 20 44 61 74 61 62 61 73 65 20 69 73 20 63 6c * Database is cl
5cf30 6f 73 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 osed */.#define
5cf40 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 53 49 43 SQLITE_MAGIC_SIC
5cf50 4b 20 20 20 20 20 30 78 34 62 37 37 31 32 39 30 K 0x4b771290
5cf60 20 20 2f 2a 20 45 72 72 6f 72 20 61 6e 64 20 61 /* Error and a
5cf70 77 61 69 74 69 6e 67 20 63 6c 6f 73 65 20 2a 2f waiting close */
5cf80 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
5cf90 4d 41 47 49 43 5f 42 55 53 59 20 20 20 20 20 30 MAGIC_BUSY 0
5cfa0 78 66 30 33 62 37 39 30 36 20 20 2f 2a 20 44 61 xf03b7906 /* Da
5cfb0 74 61 62 61 73 65 20 63 75 72 72 65 6e 74 6c 79 tabase currently
5cfc0 20 69 6e 20 75 73 65 20 2a 2f 0a 23 64 65 66 69 in use */.#defi
5cfd0 6e 65 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f ne SQLITE_MAGIC_
5cfe0 45 52 52 4f 52 20 20 20 20 30 78 62 35 33 35 37 ERROR 0xb5357
5cff0 39 33 30 20 20 2f 2a 20 41 6e 20 53 51 4c 49 54 930 /* An SQLIT
5d000 45 5f 4d 49 53 55 53 45 20 65 72 72 6f 72 20 6f E_MISUSE error o
5d010 63 63 75 72 72 65 64 20 2a 2f 0a 0a 2f 2a 0a 2a ccurred */../*.*
5d020 2a 20 45 61 63 68 20 53 51 4c 20 66 75 6e 63 74 * Each SQL funct
5d030 69 6f 6e 20 69 73 20 64 65 66 69 6e 65 64 20 62 ion is defined b
5d040 79 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 y an instance of
5d050 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a the following.*
5d060 2a 20 73 74 72 75 63 74 75 72 65 2e 20 20 41 20 * structure. A
5d070 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 69 73 20 pointer to this
5d080 73 74 72 75 63 74 75 72 65 20 69 73 20 73 74 6f structure is sto
5d090 72 65 64 20 69 6e 20 74 68 65 20 73 71 6c 69 74 red in the sqlit
5d0a0 65 2e 61 46 75 6e 63 0a 2a 2a 20 68 61 73 68 20 e.aFunc.** hash
5d0b0 74 61 62 6c 65 2e 20 20 57 68 65 6e 20 6d 75 6c table. When mul
5d0c0 74 69 70 6c 65 20 66 75 6e 63 74 69 6f 6e 73 20 tiple functions
5d0d0 68 61 76 65 20 74 68 65 20 73 61 6d 65 20 6e 61 have the same na
5d0e0 6d 65 2c 20 74 68 65 20 68 61 73 68 20 74 61 62 me, the hash tab
5d0f0 6c 65 0a 2a 2a 20 70 6f 69 6e 74 73 20 74 6f 20 le.** points to
5d100 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 a linked list of
5d110 20 74 68 65 73 65 20 73 74 72 75 63 74 75 72 65 these structure
5d120 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 46 75 6e s..*/.struct Fun
5d130 63 44 65 66 20 7b 0a 20 20 69 31 36 20 6e 41 72 cDef {. i16 nAr
5d140 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a g; /*
5d150 20 4e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d Number of argum
5d160 65 6e 74 73 2e 20 20 2d 31 20 6d 65 61 6e 73 20 ents. -1 means
5d170 75 6e 6c 69 6d 69 74 65 64 20 2a 2f 0a 20 20 75 unlimited */. u
5d180 38 20 69 50 72 65 66 45 6e 63 3b 20 20 20 20 20 8 iPrefEnc;
5d190 20 20 20 20 2f 2a 20 50 72 65 66 65 72 72 65 64 /* Preferred
5d1a0 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 28 text encoding (
5d1b0 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 31 36 4c SQLITE_UTF8, 16L
5d1c0 45 2c 20 31 36 42 45 29 20 2a 2f 0a 20 20 75 38 E, 16BE) */. u8
5d1d0 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 flags;
5d1e0 20 20 20 2f 2a 20 53 6f 6d 65 20 63 6f 6d 62 69 /* Some combi
5d1f0 6e 61 74 69 6f 6e 20 6f 66 20 53 51 4c 49 54 45 nation of SQLITE
5d200 5f 46 55 4e 43 5f 2a 20 2a 2f 0a 20 20 76 6f 69 _FUNC_* */. voi
5d210 64 20 2a 70 55 73 65 72 44 61 74 61 3b 20 20 20 d *pUserData;
5d220 20 20 2f 2a 20 55 73 65 72 20 64 61 74 61 20 70 /* User data p
5d230 61 72 61 6d 65 74 65 72 20 2a 2f 0a 20 20 46 75 arameter */. Fu
5d240 6e 63 44 65 66 20 2a 70 4e 65 78 74 3b 20 20 20 ncDef *pNext;
5d250 20 20 20 2f 2a 20 4e 65 78 74 20 66 75 6e 63 74 /* Next funct
5d260 69 6f 6e 20 77 69 74 68 20 73 61 6d 65 20 6e 61 ion with same na
5d270 6d 65 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 me */. void (*x
5d280 46 75 6e 63 29 28 73 71 6c 69 74 65 33 5f 63 6f Func)(sqlite3_co
5d290 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 ntext*,int,sqlit
5d2a0 65 33 5f 76 61 6c 75 65 2a 2a 29 3b 20 2f 2a 20 e3_value**); /*
5d2b0 52 65 67 75 6c 61 72 20 66 75 6e 63 74 69 6f 6e Regular function
5d2c0 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 53 74 */. void (*xSt
5d2d0 65 70 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 ep)(sqlite3_cont
5d2e0 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 ext*,int,sqlite3
5d2f0 5f 76 61 6c 75 65 2a 2a 29 3b 20 2f 2a 20 41 67 _value**); /* Ag
5d300 67 72 65 67 61 74 65 20 73 74 65 70 20 2a 2f 0a gregate step */.
5d310 20 20 76 6f 69 64 20 28 2a 78 46 69 6e 61 6c 69 void (*xFinali
5d320 7a 65 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 ze)(sqlite3_cont
5d330 65 78 74 2a 29 3b 20 20 20 20 20 20 20 20 20 20 ext*);
5d340 20 20 20 20 20 20 2f 2a 20 41 67 67 72 65 67 61 /* Aggrega
5d350 74 65 20 66 69 6e 61 6c 69 7a 65 72 20 2a 2f 0a te finalizer */.
5d360 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 char *zName;
5d370 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 20 6e 61 /* SQL na
5d380 6d 65 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 me of the functi
5d390 6f 6e 2e 20 2a 2f 0a 20 20 46 75 6e 63 44 65 66 on. */. FuncDef
5d3a0 20 2a 70 48 61 73 68 3b 20 20 20 20 20 20 2f 2a *pHash; /*
5d3b0 20 4e 65 78 74 20 77 69 74 68 20 61 20 64 69 66 Next with a dif
5d3c0 66 65 72 65 6e 74 20 6e 61 6d 65 20 62 75 74 20 ferent name but
5d3d0 74 68 65 20 73 61 6d 65 20 68 61 73 68 20 2a 2f the same hash */
5d3e0 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 50 6f 73 73 69 .};../*.** Possi
5d3f0 62 6c 65 20 76 61 6c 75 65 73 20 66 6f 72 20 46 ble values for F
5d400 75 6e 63 44 65 66 2e 66 6c 61 67 73 0a 2a 2f 0a uncDef.flags.*/.
5d410 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 #define SQLITE_F
5d420 55 4e 43 5f 4c 49 4b 45 20 20 20 20 20 30 78 30 UNC_LIKE 0x0
5d430 31 20 2f 2a 20 43 61 6e 64 69 64 61 74 65 20 66 1 /* Candidate f
5d440 6f 72 20 74 68 65 20 4c 49 4b 45 20 6f 70 74 69 or the LIKE opti
5d450 6d 69 7a 61 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 mization */.#def
5d460 69 6e 65 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f ine SQLITE_FUNC_
5d470 43 41 53 45 20 20 20 20 20 30 78 30 32 20 2f 2a CASE 0x02 /*
5d480 20 43 61 73 65 2d 73 65 6e 73 69 74 69 76 65 20 Case-sensitive
5d490 4c 49 4b 45 2d 74 79 70 65 20 66 75 6e 63 74 69 LIKE-type functi
5d4a0 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 on */.#define SQ
5d4b0 4c 49 54 45 5f 46 55 4e 43 5f 45 50 48 45 4d 20 LITE_FUNC_EPHEM
5d4c0 20 20 20 30 78 30 34 20 2f 2a 20 45 70 68 65 6d 0x04 /* Ephem
5d4d0 65 72 61 6c 2e 20 20 44 65 6c 65 74 65 20 77 69 eral. Delete wi
5d4e0 74 68 20 56 44 42 45 20 2a 2f 0a 23 64 65 66 69 th VDBE */.#defi
5d4f0 6e 65 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e ne SQLITE_FUNC_N
5d500 45 45 44 43 4f 4c 4c 20 30 78 30 38 20 2f 2a 20 EEDCOLL 0x08 /*
5d510 73 71 6c 69 74 65 33 47 65 74 46 75 6e 63 43 6f sqlite3GetFuncCo
5d520 6c 6c 53 65 71 28 29 20 6d 69 67 68 74 20 62 65 llSeq() might be
5d530 20 63 61 6c 6c 65 64 20 2a 2f 0a 23 64 65 66 69 called */.#defi
5d540 6e 65 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 50 ne SQLITE_FUNC_P
5d550 52 49 56 41 54 45 20 20 30 78 31 30 20 2f 2a 20 RIVATE 0x10 /*
5d560 41 6c 6c 6f 77 65 64 20 66 6f 72 20 69 6e 74 65 Allowed for inte
5d570 72 6e 61 6c 20 75 73 65 20 6f 6e 6c 79 20 2a 2f rnal use only */
5d580 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
5d590 46 55 4e 43 5f 43 4f 55 4e 54 20 20 20 20 30 78 FUNC_COUNT 0x
5d5a0 32 30 20 2f 2a 20 42 75 69 6c 74 2d 69 6e 20 63 20 /* Built-in c
5d5b0 6f 75 6e 74 28 2a 29 20 61 67 67 72 65 67 61 74 ount(*) aggregat
5d5c0 65 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 e */../*.** The
5d5d0 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 72 65 65 20 following three
5d5e0 6d 61 63 72 6f 73 2c 20 46 55 4e 43 54 49 4f 4e macros, FUNCTION
5d5f0 28 29 2c 20 4c 49 4b 45 46 55 4e 43 28 29 20 61 (), LIKEFUNC() a
5d600 6e 64 20 41 47 47 52 45 47 41 54 45 28 29 20 61 nd AGGREGATE() a
5d610 72 65 0a 2a 2a 20 75 73 65 64 20 74 6f 20 63 72 re.** used to cr
5d620 65 61 74 65 20 74 68 65 20 69 6e 69 74 69 61 6c eate the initial
5d630 69 7a 65 72 73 20 66 6f 72 20 74 68 65 20 46 75 izers for the Fu
5d640 6e 63 44 65 66 20 73 74 72 75 63 74 75 72 65 73 ncDef structures
5d650 2e 0a 2a 2a 0a 2a 2a 20 20 20 46 55 4e 43 54 49 ..**.** FUNCTI
5d660 4f 4e 28 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 ON(zName, nArg,
5d670 69 41 72 67 2c 20 62 4e 43 2c 20 78 46 75 6e 63 iArg, bNC, xFunc
5d680 29 0a 2a 2a 20 20 20 20 20 55 73 65 64 20 74 6f ).** Used to
5d690 20 63 72 65 61 74 65 20 61 20 73 63 61 6c 61 72 create a scalar
5d6a0 20 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69 function defini
5d6b0 74 69 6f 6e 20 6f 66 20 61 20 66 75 6e 63 74 69 tion of a functi
5d6c0 6f 6e 20 7a 4e 61 6d 65 20 0a 2a 2a 20 20 20 20 on zName .**
5d6d0 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 62 79 20 implemented by
5d6e0 43 20 66 75 6e 63 74 69 6f 6e 20 78 46 75 6e 63 C function xFunc
5d6f0 20 74 68 61 74 20 61 63 63 65 70 74 73 20 6e 41 that accepts nA
5d700 72 67 20 61 72 67 75 6d 65 6e 74 73 2e 20 54 68 rg arguments. Th
5d710 65 0a 2a 2a 20 20 20 20 20 76 61 6c 75 65 20 70 e.** value p
5d720 61 73 73 65 64 20 61 73 20 69 41 72 67 20 69 73 assed as iArg is
5d730 20 63 61 73 74 20 74 6f 20 61 20 28 76 6f 69 64 cast to a (void
5d740 2a 29 20 61 6e 64 20 6d 61 64 65 20 61 76 61 69 *) and made avai
5d750 6c 61 62 6c 65 0a 2a 2a 20 20 20 20 20 61 73 20 lable.** as
5d760 74 68 65 20 75 73 65 72 2d 64 61 74 61 20 28 73 the user-data (s
5d770 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61 qlite3_user_data
5d780 28 29 29 20 66 6f 72 20 74 68 65 20 66 75 6e 63 ()) for the func
5d790 74 69 6f 6e 2e 20 49 66 20 0a 2a 2a 20 20 20 20 tion. If .**
5d7a0 20 61 72 67 75 6d 65 6e 74 20 62 4e 43 20 69 73 argument bNC is
5d7b0 20 74 72 75 65 2c 20 74 68 65 6e 20 74 68 65 20 true, then the
5d7c0 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 45 44 SQLITE_FUNC_NEED
5d7d0 43 4f 4c 4c 20 66 6c 61 67 20 69 73 20 73 65 74 COLL flag is set
5d7e0 2e 0a 2a 2a 0a 2a 2a 20 20 20 41 47 47 52 45 47 ..**.** AGGREG
5d7f0 41 54 45 28 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c ATE(zName, nArg,
5d800 20 69 41 72 67 2c 20 62 4e 43 2c 20 78 53 74 65 iArg, bNC, xSte
5d810 70 2c 20 78 46 69 6e 61 6c 29 0a 2a 2a 20 20 20 p, xFinal).**
5d820 20 20 55 73 65 64 20 74 6f 20 63 72 65 61 74 65 Used to create
5d830 20 61 6e 20 61 67 67 72 65 67 61 74 65 20 66 75 an aggregate fu
5d840 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69 74 69 6f nction definitio
5d850 6e 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 62 79 n implemented by
5d860 0a 2a 2a 20 20 20 20 20 74 68 65 20 43 20 66 75 .** the C fu
5d870 6e 63 74 69 6f 6e 73 20 78 53 74 65 70 20 61 6e nctions xStep an
5d880 64 20 78 46 69 6e 61 6c 2e 20 54 68 65 20 66 69 d xFinal. The fi
5d890 72 73 74 20 66 6f 75 72 20 70 61 72 61 6d 65 74 rst four paramet
5d8a0 65 72 73 0a 2a 2a 20 20 20 20 20 61 72 65 20 69 ers.** are i
5d8b0 6e 74 65 72 70 72 65 74 65 64 20 69 6e 20 74 68 nterpreted in th
5d8c0 65 20 73 61 6d 65 20 77 61 79 20 61 73 20 74 68 e same way as th
5d8d0 65 20 66 69 72 73 74 20 34 20 70 61 72 61 6d 65 e first 4 parame
5d8e0 74 65 72 73 20 74 6f 0a 2a 2a 20 20 20 20 20 46 ters to.** F
5d8f0 55 4e 43 54 49 4f 4e 28 29 2e 0a 2a 2a 0a 2a 2a UNCTION()..**.**
5d900 20 20 20 4c 49 4b 45 46 55 4e 43 28 7a 4e 61 6d LIKEFUNC(zNam
5d910 65 2c 20 6e 41 72 67 2c 20 70 41 72 67 2c 20 66 e, nArg, pArg, f
5d920 6c 61 67 73 29 0a 2a 2a 20 20 20 20 20 55 73 65 lags).** Use
5d930 64 20 74 6f 20 63 72 65 61 74 65 20 61 20 73 63 d to create a sc
5d940 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20 64 65 alar function de
5d950 66 69 6e 69 74 69 6f 6e 20 6f 66 20 61 20 66 75 finition of a fu
5d960 6e 63 74 69 6f 6e 20 7a 4e 61 6d 65 20 0a 2a 2a nction zName .**
5d970 20 20 20 20 20 74 68 61 74 20 61 63 63 65 70 74 that accept
5d980 73 20 6e 41 72 67 20 61 72 67 75 6d 65 6e 74 73 s nArg arguments
5d990 20 61 6e 64 20 69 73 20 69 6d 70 6c 65 6d 65 6e and is implemen
5d9a0 74 65 64 20 62 79 20 61 20 63 61 6c 6c 20 74 6f ted by a call to
5d9b0 20 43 20 0a 2a 2a 20 20 20 20 20 66 75 6e 63 74 C .** funct
5d9c0 69 6f 6e 20 6c 69 6b 65 46 75 6e 63 2e 20 41 72 ion likeFunc. Ar
5d9d0 67 75 6d 65 6e 74 20 70 41 72 67 20 69 73 20 63 gument pArg is c
5d9e0 61 73 74 20 74 6f 20 61 20 28 76 6f 69 64 20 2a ast to a (void *
5d9f0 29 20 61 6e 64 20 6d 61 64 65 0a 2a 2a 20 20 20 ) and made.**
5da00 20 20 61 76 61 69 6c 61 62 6c 65 20 61 73 20 74 available as t
5da10 68 65 20 66 75 6e 63 74 69 6f 6e 20 75 73 65 72 he function user
5da20 2d 64 61 74 61 20 28 73 71 6c 69 74 65 33 5f 75 -data (sqlite3_u
5da30 73 65 72 5f 64 61 74 61 28 29 29 2e 20 54 68 65 ser_data()). The
5da40 0a 2a 2a 20 20 20 20 20 46 75 6e 63 44 65 66 2e .** FuncDef.
5da50 66 6c 61 67 73 20 76 61 72 69 61 62 6c 65 20 69 flags variable i
5da60 73 20 73 65 74 20 74 6f 20 74 68 65 20 76 61 6c s set to the val
5da70 75 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65 ue passed as the
5da80 20 66 6c 61 67 73 0a 2a 2a 20 20 20 20 20 70 61 flags.** pa
5da90 72 61 6d 65 74 65 72 2e 0a 2a 2f 0a 23 64 65 66 rameter..*/.#def
5daa0 69 6e 65 20 46 55 4e 43 54 49 4f 4e 28 7a 4e 61 ine FUNCTION(zNa
5dab0 6d 65 2c 20 6e 41 72 67 2c 20 69 41 72 67 2c 20 me, nArg, iArg,
5dac0 62 4e 43 2c 20 78 46 75 6e 63 29 20 5c 0a 20 20 bNC, xFunc) \.
5dad0 7b 6e 41 72 67 2c 20 53 51 4c 49 54 45 5f 55 54 {nArg, SQLITE_UT
5dae0 46 38 2c 20 62 4e 43 2a 53 51 4c 49 54 45 5f 46 F8, bNC*SQLITE_F
5daf0 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c 2c 20 5c 0a UNC_NEEDCOLL, \.
5db00 20 20 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54 4f SQLITE_INT_TO
5db10 5f 50 54 52 28 69 41 72 67 29 2c 20 30 2c 20 78 _PTR(iArg), 0, x
5db20 46 75 6e 63 2c 20 30 2c 20 30 2c 20 23 7a 4e 61 Func, 0, 0, #zNa
5db30 6d 65 2c 20 30 7d 0a 23 64 65 66 69 6e 65 20 53 me, 0}.#define S
5db40 54 52 5f 46 55 4e 43 54 49 4f 4e 28 7a 4e 61 6d TR_FUNCTION(zNam
5db50 65 2c 20 6e 41 72 67 2c 20 70 41 72 67 2c 20 62 e, nArg, pArg, b
5db60 4e 43 2c 20 78 46 75 6e 63 29 20 5c 0a 20 20 7b NC, xFunc) \. {
5db70 6e 41 72 67 2c 20 53 51 4c 49 54 45 5f 55 54 46 nArg, SQLITE_UTF
5db80 38 2c 20 62 4e 43 2a 53 51 4c 49 54 45 5f 46 55 8, bNC*SQLITE_FU
5db90 4e 43 5f 4e 45 45 44 43 4f 4c 4c 2c 20 5c 0a 20 NC_NEEDCOLL, \.
5dba0 20 20 70 41 72 67 2c 20 30 2c 20 78 46 75 6e 63 pArg, 0, xFunc
5dbb0 2c 20 30 2c 20 30 2c 20 23 7a 4e 61 6d 65 2c 20 , 0, 0, #zName,
5dbc0 30 7d 0a 23 64 65 66 69 6e 65 20 4c 49 4b 45 46 0}.#define LIKEF
5dbd0 55 4e 43 28 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c UNC(zName, nArg,
5dbe0 20 61 72 67 2c 20 66 6c 61 67 73 29 20 5c 0a 20 arg, flags) \.
5dbf0 20 7b 6e 41 72 67 2c 20 53 51 4c 49 54 45 5f 55 {nArg, SQLITE_U
5dc00 54 46 38 2c 20 66 6c 61 67 73 2c 20 28 76 6f 69 TF8, flags, (voi
5dc10 64 20 2a 29 61 72 67 2c 20 30 2c 20 6c 69 6b 65 d *)arg, 0, like
5dc20 46 75 6e 63 2c 20 30 2c 20 30 2c 20 23 7a 4e 61 Func, 0, 0, #zNa
5dc30 6d 65 2c 20 30 7d 0a 23 64 65 66 69 6e 65 20 41 me, 0}.#define A
5dc40 47 47 52 45 47 41 54 45 28 7a 4e 61 6d 65 2c 20 GGREGATE(zName,
5dc50 6e 41 72 67 2c 20 61 72 67 2c 20 6e 63 2c 20 78 nArg, arg, nc, x
5dc60 53 74 65 70 2c 20 78 46 69 6e 61 6c 29 20 5c 0a Step, xFinal) \.
5dc70 20 20 7b 6e 41 72 67 2c 20 53 51 4c 49 54 45 5f {nArg, SQLITE_
5dc80 55 54 46 38 2c 20 6e 63 2a 53 51 4c 49 54 45 5f UTF8, nc*SQLITE_
5dc90 46 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c 2c 20 5c FUNC_NEEDCOLL, \
5dca0 0a 20 20 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54 . SQLITE_INT_T
5dcb0 4f 5f 50 54 52 28 61 72 67 29 2c 20 30 2c 20 30 O_PTR(arg), 0, 0
5dcc0 2c 20 78 53 74 65 70 2c 78 46 69 6e 61 6c 2c 23 , xStep,xFinal,#
5dcd0 7a 4e 61 6d 65 2c 30 7d 0a 0a 2f 2a 0a 2a 2a 20 zName,0}../*.**
5dce0 41 6c 6c 20 63 75 72 72 65 6e 74 20 73 61 76 65 All current save
5dcf0 70 6f 69 6e 74 73 20 61 72 65 20 73 74 6f 72 65 points are store
5dd00 64 20 69 6e 20 61 20 6c 69 6e 6b 65 64 20 6c 69 d in a linked li
5dd10 73 74 20 73 74 61 72 74 69 6e 67 20 61 74 0a 2a st starting at.*
5dd20 2a 20 73 71 6c 69 74 65 33 2e 70 53 61 76 65 70 * sqlite3.pSavep
5dd30 6f 69 6e 74 2e 20 54 68 65 20 66 69 72 73 74 20 oint. The first
5dd40 65 6c 65 6d 65 6e 74 20 69 6e 20 74 68 65 20 6c element in the l
5dd50 69 73 74 20 69 73 20 74 68 65 20 6d 6f 73 74 20 ist is the most
5dd60 72 65 63 65 6e 74 6c 79 0a 2a 2a 20 6f 70 65 6e recently.** open
5dd70 65 64 20 73 61 76 65 70 6f 69 6e 74 2e 20 53 61 ed savepoint. Sa
5dd80 76 65 70 6f 69 6e 74 73 20 61 72 65 20 61 64 64 vepoints are add
5dd90 65 64 20 74 6f 20 74 68 65 20 6c 69 73 74 20 62 ed to the list b
5dda0 79 20 74 68 65 20 76 64 62 65 0a 2a 2a 20 4f 50 y the vdbe.** OP
5ddb0 5f 53 61 76 65 70 6f 69 6e 74 20 69 6e 73 74 72 _Savepoint instr
5ddc0 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63 uction..*/.struc
5ddd0 74 20 53 61 76 65 70 6f 69 6e 74 20 7b 0a 20 20 t Savepoint {.
5dde0 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 char *zName;
5ddf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5de00 20 20 20 20 2f 2a 20 53 61 76 65 70 6f 69 6e 74 /* Savepoint
5de10 20 6e 61 6d 65 20 28 6e 75 6c 2d 74 65 72 6d 69 name (nul-termi
5de20 6e 61 74 65 64 29 20 2a 2f 0a 20 20 69 6e 74 20 nated) */. int
5de30 6e 44 65 66 65 72 72 65 64 43 6f 6e 73 3b 20 20 nDeferredCons;
5de40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5de50 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 64 65 66 /* Number of def
5de60 65 72 72 65 64 20 66 6b 20 76 69 6f 6c 61 74 69 erred fk violati
5de70 6f 6e 73 20 2a 2f 0a 20 20 53 61 76 65 70 6f 69 ons */. Savepoi
5de80 6e 74 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 nt *pNext;
5de90 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
5dea0 50 61 72 65 6e 74 20 73 61 76 65 70 6f 69 6e 74 Parent savepoint
5deb0 20 28 69 66 20 61 6e 79 29 20 2a 2f 0a 7d 3b 0a (if any) */.};.
5dec0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f ./*.** The follo
5ded0 77 69 6e 67 20 61 72 65 20 75 73 65 64 20 61 73 wing are used as
5dee0 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 the second para
5def0 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 meter to sqlite3
5df00 53 61 76 65 70 6f 69 6e 74 28 29 2c 0a 2a 2a 20 Savepoint(),.**
5df10 61 6e 64 20 61 73 20 74 68 65 20 50 31 20 61 72 and as the P1 ar
5df20 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 4f 50 gument to the OP
5df30 5f 53 61 76 65 70 6f 69 6e 74 20 69 6e 73 74 72 _Savepoint instr
5df40 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69 uction..*/.#defi
5df50 6e 65 20 53 41 56 45 50 4f 49 4e 54 5f 42 45 47 ne SAVEPOINT_BEG
5df60 49 4e 20 20 20 20 20 20 30 0a 23 64 65 66 69 6e IN 0.#defin
5df70 65 20 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 e SAVEPOINT_RELE
5df80 41 53 45 20 20 20 20 31 0a 23 64 65 66 69 6e 65 ASE 1.#define
5df90 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 SAVEPOINT_ROLLB
5dfa0 41 43 4b 20 20 20 32 0a 0a 0a 2f 2a 0a 2a 2a 20 ACK 2.../*.**
5dfb0 45 61 63 68 20 53 51 4c 69 74 65 20 6d 6f 64 75 Each SQLite modu
5dfc0 6c 65 20 28 76 69 72 74 75 61 6c 20 74 61 62 6c le (virtual tabl
5dfd0 65 20 64 65 66 69 6e 69 74 69 6f 6e 29 20 69 73 e definition) is
5dfe0 20 64 65 66 69 6e 65 64 20 62 79 20 61 6e 0a 2a defined by an.*
5dff0 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 * instance of th
5e000 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 e following stru
5e010 63 74 75 72 65 2c 20 73 74 6f 72 65 64 20 69 6e cture, stored in
5e020 20 74 68 65 20 73 71 6c 69 74 65 33 2e 61 4d 6f the sqlite3.aMo
5e030 64 75 6c 65 0a 2a 2a 20 68 61 73 68 20 74 61 62 dule.** hash tab
5e040 6c 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4d 6f le..*/.struct Mo
5e050 64 75 6c 65 20 7b 0a 20 20 63 6f 6e 73 74 20 73 dule {. const s
5e060 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 qlite3_module *p
5e070 4d 6f 64 75 6c 65 3b 20 20 20 20 20 20 20 2f 2a Module; /*
5e080 20 43 61 6c 6c 62 61 63 6b 20 70 6f 69 6e 74 65 Callback pointe
5e090 72 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 rs */. const ch
5e0a0 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 ar *zName;
5e0b0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
5e0c0 4e 61 6d 65 20 70 61 73 73 65 64 20 74 6f 20 63 Name passed to c
5e0d0 72 65 61 74 65 5f 6d 6f 64 75 6c 65 28 29 20 2a reate_module() *
5e0e0 2f 0a 20 20 76 6f 69 64 20 2a 70 41 75 78 3b 20 /. void *pAux;
5e0f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5e100 20 20 20 20 20 20 20 20 20 2f 2a 20 70 41 75 78 /* pAux
5e110 20 70 61 73 73 65 64 20 74 6f 20 63 72 65 61 74 passed to creat
5e120 65 5f 6d 6f 64 75 6c 65 28 29 20 2a 2f 0a 20 20 e_module() */.
5e130 76 6f 69 64 20 28 2a 78 44 65 73 74 72 6f 79 29 void (*xDestroy)
5e140 28 76 6f 69 64 20 2a 29 3b 20 20 20 20 20 20 20 (void *);
5e150 20 20 20 20 20 2f 2a 20 4d 6f 64 75 6c 65 20 64 /* Module d
5e160 65 73 74 72 75 63 74 6f 72 20 66 75 6e 63 74 69 estructor functi
5e170 6f 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 on */.};../*.**
5e180 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 information abou
5e190 74 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 6f 66 t each column of
5e1a0 20 61 6e 20 53 51 4c 20 74 61 62 6c 65 20 69 73 an SQL table is
5e1b0 20 68 65 6c 64 20 69 6e 20 61 6e 20 69 6e 73 74 held in an inst
5e1c0 61 6e 63 65 0a 2a 2a 20 6f 66 20 74 68 69 73 20 ance.** of this
5e1d0 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 structure..*/.st
5e1e0 72 75 63 74 20 43 6f 6c 75 6d 6e 20 7b 0a 20 20 ruct Column {.
5e1f0 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 char *zName;
5e200 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 69 73 /* Name of this
5e210 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 45 78 70 column */. Exp
5e220 72 20 2a 70 44 66 6c 74 3b 20 20 20 20 20 2f 2a r *pDflt; /*
5e230 20 44 65 66 61 75 6c 74 20 76 61 6c 75 65 20 6f Default value o
5e240 66 20 74 68 69 73 20 63 6f 6c 75 6d 6e 20 2a 2f f this column */
5e250 0a 20 20 63 68 61 72 20 2a 7a 44 66 6c 74 3b 20 . char *zDflt;
5e260 20 20 20 20 2f 2a 20 4f 72 69 67 69 6e 61 6c 20 /* Original
5e270 74 65 78 74 20 6f 66 20 74 68 65 20 64 65 66 61 text of the defa
5e280 75 6c 74 20 76 61 6c 75 65 20 2a 2f 0a 20 20 63 ult value */. c
5e290 68 61 72 20 2a 7a 54 79 70 65 3b 20 20 20 20 20 har *zType;
5e2a0 2f 2a 20 44 61 74 61 20 74 79 70 65 20 66 6f 72 /* Data type for
5e2b0 20 74 68 69 73 20 63 6f 6c 75 6d 6e 20 2a 2f 0a this column */.
5e2c0 20 20 63 68 61 72 20 2a 7a 43 6f 6c 6c 3b 20 20 char *zColl;
5e2d0 20 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67 20 /* Collating
5e2e0 73 65 71 75 65 6e 63 65 2e 20 20 49 66 20 4e 55 sequence. If NU
5e2f0 4c 4c 2c 20 75 73 65 20 74 68 65 20 64 65 66 61 LL, use the defa
5e300 75 6c 74 20 2a 2f 0a 20 20 75 38 20 6e 6f 74 4e ult */. u8 notN
5e310 75 6c 6c 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 ull; /* Tru
5e320 65 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20 e if there is a
5e330 4e 4f 54 20 4e 55 4c 4c 20 63 6f 6e 73 74 72 61 NOT NULL constra
5e340 69 6e 74 20 2a 2f 0a 20 20 75 38 20 69 73 50 72 int */. u8 isPr
5e350 69 6d 4b 65 79 3b 20 20 20 20 2f 2a 20 54 72 75 imKey; /* Tru
5e360 65 20 69 66 20 74 68 69 73 20 63 6f 6c 75 6d 6e e if this column
5e370 20 69 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 is part of the
5e380 50 52 49 4d 41 52 59 20 4b 45 59 20 2a 2f 0a 20 PRIMARY KEY */.
5e390 20 63 68 61 72 20 61 66 66 69 6e 69 74 79 3b 20 char affinity;
5e3a0 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 /* One of the
5e3b0 53 51 4c 49 54 45 5f 41 46 46 5f 2e 2e 2e 20 76 SQLITE_AFF_... v
5e3c0 61 6c 75 65 73 20 2a 2f 0a 23 69 66 6e 64 65 66 alues */.#ifndef
5e3d0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 SQLITE_OMIT_VIR
5e3e0 54 55 41 4c 54 41 42 4c 45 0a 20 20 75 38 20 69 TUALTABLE. u8 i
5e3f0 73 48 69 64 64 65 6e 3b 20 20 20 20 20 2f 2a 20 sHidden; /*
5e400 54 72 75 65 20 69 66 20 74 68 69 73 20 63 6f 6c True if this col
5e410 75 6d 6e 20 69 73 20 27 68 69 64 64 65 6e 27 20 umn is 'hidden'
5e420 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a */.#endif.};../*
5e430 0a 2a 2a 20 41 20 22 43 6f 6c 6c 61 74 69 6e 67 .** A "Collating
5e440 20 53 65 71 75 65 6e 63 65 22 20 69 73 20 64 65 Sequence" is de
5e450 66 69 6e 65 64 20 62 79 20 61 6e 20 69 6e 73 74 fined by an inst
5e460 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c ance of the foll
5e470 6f 77 69 6e 67 0a 2a 2a 20 73 74 72 75 63 74 75 owing.** structu
5e480 72 65 2e 20 43 6f 6e 63 65 70 74 75 61 6c 6c 79 re. Conceptually
5e490 2c 20 61 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 , a collating se
5e4a0 71 75 65 6e 63 65 20 63 6f 6e 73 69 73 74 73 20 quence consists
5e4b0 6f 66 20 61 20 6e 61 6d 65 20 61 6e 64 0a 2a 2a of a name and.**
5e4c0 20 61 20 63 6f 6d 70 61 72 69 73 6f 6e 20 72 6f a comparison ro
5e4d0 75 74 69 6e 65 20 74 68 61 74 20 64 65 66 69 6e utine that defin
5e4e0 65 73 20 74 68 65 20 6f 72 64 65 72 20 6f 66 20 es the order of
5e4f0 74 68 61 74 20 73 65 71 75 65 6e 63 65 2e 0a 2a that sequence..*
5e500 2a 0a 2a 2a 20 54 68 65 72 65 20 6d 61 79 20 74 *.** There may t
5e510 77 6f 20 73 65 70 61 72 61 74 65 20 69 6d 70 6c wo separate impl
5e520 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 ementations of t
5e530 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 75 6e he collation fun
5e540 63 74 69 6f 6e 2c 20 6f 6e 65 0a 2a 2a 20 74 68 ction, one.** th
5e550 61 74 20 70 72 6f 63 65 73 73 65 73 20 74 65 78 at processes tex
5e560 74 20 69 6e 20 55 54 46 2d 38 20 65 6e 63 6f 64 t in UTF-8 encod
5e570 69 6e 67 20 28 43 6f 6c 6c 53 65 71 2e 78 43 6d ing (CollSeq.xCm
5e580 70 29 20 61 6e 64 20 61 6e 6f 74 68 65 72 20 74 p) and another t
5e590 68 61 74 0a 2a 2a 20 70 72 6f 63 65 73 73 65 73 hat.** processes
5e5a0 20 74 65 78 74 20 65 6e 63 6f 64 65 64 20 69 6e text encoded in
5e5b0 20 55 54 46 2d 31 36 20 28 43 6f 6c 6c 53 65 71 UTF-16 (CollSeq
5e5c0 2e 78 43 6d 70 31 36 29 2c 20 75 73 69 6e 67 20 .xCmp16), using
5e5d0 74 68 65 20 6d 61 63 68 69 6e 65 0a 2a 2a 20 6e the machine.** n
5e5e0 61 74 69 76 65 20 62 79 74 65 20 6f 72 64 65 72 ative byte order
5e5f0 2e 20 57 68 65 6e 20 61 20 63 6f 6c 6c 61 74 69 . When a collati
5e600 6f 6e 20 73 65 71 75 65 6e 63 65 20 69 73 20 69 on sequence is i
5e610 6e 76 6f 6b 65 64 2c 20 53 51 4c 69 74 65 20 73 nvoked, SQLite s
5e620 65 6c 65 63 74 73 0a 2a 2a 20 74 68 65 20 76 65 elects.** the ve
5e630 72 73 69 6f 6e 20 74 68 61 74 20 77 69 6c 6c 20 rsion that will
5e640 72 65 71 75 69 72 65 20 74 68 65 20 6c 65 61 73 require the leas
5e650 74 20 65 78 70 65 6e 73 69 76 65 20 65 6e 63 6f t expensive enco
5e660 64 69 6e 67 0a 2a 2a 20 74 72 61 6e 73 6c 61 74 ding.** translat
5e670 69 6f 6e 73 2c 20 69 66 20 61 6e 79 2e 0a 2a 2a ions, if any..**
5e680 0a 2a 2a 20 54 68 65 20 43 6f 6c 6c 53 65 71 2e .** The CollSeq.
5e690 70 55 73 65 72 20 6d 65 6d 62 65 72 20 76 61 72 pUser member var
5e6a0 69 61 62 6c 65 20 69 73 20 61 6e 20 65 78 74 72 iable is an extr
5e6b0 61 20 70 61 72 61 6d 65 74 65 72 20 74 68 61 74 a parameter that
5e6c0 20 70 61 73 73 65 64 20 69 6e 0a 2a 2a 20 61 73 passed in.** as
5e6d0 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d the first argum
5e6e0 65 6e 74 20 74 6f 20 74 68 65 20 55 54 46 2d 38 ent to the UTF-8
5e6f0 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 comparison func
5e700 74 69 6f 6e 2c 20 78 43 6d 70 2e 0a 2a 2a 20 43 tion, xCmp..** C
5e710 6f 6c 6c 53 65 71 2e 70 55 73 65 72 31 36 20 69 ollSeq.pUser16 i
5e720 73 20 74 68 65 20 65 71 75 69 76 61 6c 65 6e 74 s the equivalent
5e730 20 66 6f 72 20 74 68 65 20 55 54 46 2d 31 36 20 for the UTF-16
5e740 63 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 74 comparison funct
5e750 69 6f 6e 2c 0a 2a 2a 20 78 43 6d 70 31 36 2e 0a ion,.** xCmp16..
5e760 2a 2a 0a 2a 2a 20 49 66 20 62 6f 74 68 20 43 6f **.** If both Co
5e770 6c 6c 53 65 71 2e 78 43 6d 70 20 61 6e 64 20 43 llSeq.xCmp and C
5e780 6f 6c 6c 53 65 71 2e 78 43 6d 70 31 36 20 61 72 ollSeq.xCmp16 ar
5e790 65 20 4e 55 4c 4c 2c 20 69 74 20 6d 65 61 6e 73 e NULL, it means
5e7a0 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 63 6f 6c that the.** col
5e7b0 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 lating sequence
5e7c0 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 20 20 49 is undefined. I
5e7d0 6e 64 69 63 65 73 20 62 75 69 6c 74 20 6f 6e 20 ndices built on
5e7e0 61 6e 20 75 6e 64 65 66 69 6e 65 64 0a 2a 2a 20 an undefined.**
5e7f0 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e collating sequen
5e800 63 65 20 6d 61 79 20 6e 6f 74 20 62 65 20 72 65 ce may not be re
5e810 61 64 20 6f 72 20 77 72 69 74 74 65 6e 2e 0a 2a ad or written..*
5e820 2f 0a 73 74 72 75 63 74 20 43 6f 6c 6c 53 65 71 /.struct CollSeq
5e830 20 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 {. char *zName
5e840 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 ; /* Na
5e850 6d 65 20 6f 66 20 74 68 65 20 63 6f 6c 6c 61 74 me of the collat
5e860 69 6e 67 20 73 65 71 75 65 6e 63 65 2c 20 55 54 ing sequence, UT
5e870 46 2d 38 20 65 6e 63 6f 64 65 64 20 2a 2f 0a 20 F-8 encoded */.
5e880 20 75 38 20 65 6e 63 3b 20 20 20 20 20 20 20 20 u8 enc;
5e890 20 20 20 20 20 20 20 2f 2a 20 54 65 78 74 20 65 /* Text e
5e8a0 6e 63 6f 64 69 6e 67 20 68 61 6e 64 6c 65 64 20 ncoding handled
5e8b0 62 79 20 78 43 6d 70 28 29 20 2a 2f 0a 20 20 75 by xCmp() */. u
5e8c0 38 20 74 79 70 65 3b 20 20 20 20 20 20 20 20 20 8 type;
5e8d0 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 /* One of t
5e8e0 68 65 20 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f 2e he SQLITE_COLL_.
5e8f0 2e 2e 20 76 61 6c 75 65 73 20 62 65 6c 6f 77 20 .. values below
5e900 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 55 73 65 72 */. void *pUser
5e910 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 ; /* Fi
5e920 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 rst argument to
5e930 78 43 6d 70 28 29 20 2a 2f 0a 20 20 69 6e 74 20 xCmp() */. int
5e940 28 2a 78 43 6d 70 29 28 76 6f 69 64 2a 2c 69 6e (*xCmp)(void*,in
5e950 74 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 t, const void*,
5e960 69 6e 74 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a int, const void*
5e970 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c );. void (*xDel
5e980 29 28 76 6f 69 64 2a 29 3b 20 20 2f 2a 20 44 65 )(void*); /* De
5e990 73 74 72 75 63 74 6f 72 20 66 6f 72 20 70 55 73 structor for pUs
5e9a0 65 72 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 er */.};../*.**
5e9b0 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73 20 6f Allowed values o
5e9c0 66 20 43 6f 6c 6c 53 65 71 2e 74 79 70 65 3a 0a f CollSeq.type:.
5e9d0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
5e9e0 45 5f 43 4f 4c 4c 5f 42 49 4e 41 52 59 20 20 31 E_COLL_BINARY 1
5e9f0 20 20 2f 2a 20 54 68 65 20 64 65 66 61 75 6c 74 /* The default
5ea00 20 6d 65 6d 63 6d 70 28 29 20 63 6f 6c 6c 61 74 memcmp() collat
5ea10 69 6e 67 20 73 65 71 75 65 6e 63 65 20 2a 2f 0a ing sequence */.
5ea20 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 #define SQLITE_C
5ea30 4f 4c 4c 5f 4e 4f 43 41 53 45 20 20 32 20 20 2f OLL_NOCASE 2 /
5ea40 2a 20 54 68 65 20 62 75 69 6c 74 2d 69 6e 20 4e * The built-in N
5ea50 4f 43 41 53 45 20 63 6f 6c 6c 61 74 69 6e 67 20 OCASE collating
5ea60 73 65 71 75 65 6e 63 65 20 2a 2f 0a 23 64 65 66 sequence */.#def
5ea70 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f ine SQLITE_COLL_
5ea80 52 45 56 45 52 53 45 20 33 20 20 2f 2a 20 54 68 REVERSE 3 /* Th
5ea90 65 20 62 75 69 6c 74 2d 69 6e 20 52 45 56 45 52 e built-in REVER
5eaa0 53 45 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 SE collating seq
5eab0 75 65 6e 63 65 20 2a 2f 0a 23 64 65 66 69 6e 65 uence */.#define
5eac0 20 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f 55 53 45 SQLITE_COLL_USE
5ead0 52 20 20 20 20 30 20 20 2f 2a 20 41 6e 79 20 6f R 0 /* Any o
5eae0 74 68 65 72 20 75 73 65 72 2d 64 65 66 69 6e 65 ther user-define
5eaf0 64 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 d collating sequ
5eb00 65 6e 63 65 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 ence */../*.** A
5eb10 20 73 6f 72 74 20 6f 72 64 65 72 20 63 61 6e 20 sort order can
5eb20 62 65 20 65 69 74 68 65 72 20 41 53 43 20 6f 72 be either ASC or
5eb30 20 44 45 53 43 2e 0a 2a 2f 0a 23 64 65 66 69 6e DESC..*/.#defin
5eb40 65 20 53 51 4c 49 54 45 5f 53 4f 5f 41 53 43 20 e SQLITE_SO_ASC
5eb50 20 20 20 20 20 20 30 20 20 2f 2a 20 53 6f 72 74 0 /* Sort
5eb60 20 69 6e 20 61 73 63 65 6e 64 69 6e 67 20 6f 72 in ascending or
5eb70 64 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 der */.#define S
5eb80 51 4c 49 54 45 5f 53 4f 5f 44 45 53 43 20 20 20 QLITE_SO_DESC
5eb90 20 20 20 31 20 20 2f 2a 20 53 6f 72 74 20 69 6e 1 /* Sort in
5eba0 20 61 73 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 ascending order
5ebb0 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6c 75 6d */../*.** Colum
5ebc0 6e 20 61 66 66 69 6e 69 74 79 20 74 79 70 65 73 n affinity types
5ebd0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 75 73 ..**.** These us
5ebe0 65 64 20 74 6f 20 68 61 76 65 20 6d 6e 65 6d 6f ed to have mnemo
5ebf0 6e 69 63 20 6e 61 6d 65 20 6c 69 6b 65 20 27 69 nic name like 'i
5ec00 27 20 66 6f 72 20 53 51 4c 49 54 45 5f 41 46 46 ' for SQLITE_AFF
5ec10 5f 49 4e 54 45 47 45 52 20 61 6e 64 0a 2a 2a 20 _INTEGER and.**
5ec20 27 74 27 20 66 6f 72 20 53 51 4c 49 54 45 5f 41 't' for SQLITE_A
5ec30 46 46 5f 54 45 58 54 2e 20 20 42 75 74 20 77 65 FF_TEXT. But we
5ec40 20 63 61 6e 20 73 61 76 65 20 61 20 6c 69 74 74 can save a litt
5ec50 6c 65 20 73 70 61 63 65 20 61 6e 64 20 69 6d 70 le space and imp
5ec60 72 6f 76 65 0a 2a 2a 20 74 68 65 20 73 70 65 65 rove.** the spee
5ec70 64 20 61 20 6c 69 74 74 6c 65 20 62 79 20 6e 75 d a little by nu
5ec80 6d 62 65 72 69 6e 67 20 74 68 65 20 76 61 6c 75 mbering the valu
5ec90 65 73 20 63 6f 6e 73 65 63 75 74 69 76 65 6c 79 es consecutively
5eca0 2e 20 20 0a 2a 2a 0a 2a 2a 20 42 75 74 20 72 61 . .**.** But ra
5ecb0 74 68 65 72 20 74 68 61 6e 20 73 74 61 72 74 20 ther than start
5ecc0 77 69 74 68 20 30 20 6f 72 20 31 2c 20 77 65 20 with 0 or 1, we
5ecd0 62 65 67 69 6e 20 77 69 74 68 20 27 61 27 2e 20 begin with 'a'.
5ece0 20 54 68 61 74 20 77 61 79 2c 0a 2a 2a 20 77 68 That way,.** wh
5ecf0 65 6e 20 6d 75 6c 74 69 70 6c 65 20 61 66 66 69 en multiple affi
5ed00 6e 69 74 79 20 74 79 70 65 73 20 61 72 65 20 63 nity types are c
5ed10 6f 6e 63 61 74 65 6e 61 74 65 64 20 69 6e 74 6f oncatenated into
5ed20 20 61 20 73 74 72 69 6e 67 20 61 6e 64 0a 2a 2a a string and.**
5ed30 20 75 73 65 64 20 61 73 20 74 68 65 20 50 34 20 used as the P4
5ed40 6f 70 65 72 61 6e 64 2c 20 74 68 65 79 20 77 69 operand, they wi
5ed50 6c 6c 20 62 65 20 6d 6f 72 65 20 72 65 61 64 61 ll be more reada
5ed60 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 ble..**.** Note
5ed70 61 6c 73 6f 20 74 68 61 74 20 74 68 65 20 6e 75 also that the nu
5ed80 6d 65 72 69 63 20 74 79 70 65 73 20 61 72 65 20 meric types are
5ed90 67 72 6f 75 70 65 64 20 74 6f 67 65 74 68 65 72 grouped together
5eda0 20 73 6f 20 74 68 61 74 20 74 65 73 74 69 6e 67 so that testing
5edb0 0a 2a 2a 20 66 6f 72 20 61 20 6e 75 6d 65 72 69 .** for a numeri
5edc0 63 20 74 79 70 65 20 69 73 20 61 20 73 69 6e 67 c type is a sing
5edd0 6c 65 20 63 6f 6d 70 61 72 69 73 6f 6e 2e 0a 2a le comparison..*
5ede0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
5edf0 5f 41 46 46 5f 54 45 58 54 20 20 20 20 20 27 61 _AFF_TEXT 'a
5ee00 27 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 '.#define SQLITE
5ee10 5f 41 46 46 5f 4e 4f 4e 45 20 20 20 20 20 27 62 _AFF_NONE 'b
5ee20 27 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 '.#define SQLITE
5ee30 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20 20 27 63 _AFF_NUMERIC 'c
5ee40 27 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 '.#define SQLITE
5ee50 5f 41 46 46 5f 49 4e 54 45 47 45 52 20 20 27 64 _AFF_INTEGER 'd
5ee60 27 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 '.#define SQLITE
5ee70 5f 41 46 46 5f 52 45 41 4c 20 20 20 20 20 27 65 _AFF_REAL 'e
5ee80 27 0a 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 '..#define sqlit
5ee90 65 33 49 73 4e 75 6d 65 72 69 63 41 66 66 69 6e e3IsNumericAffin
5eea0 69 74 79 28 58 29 20 20 28 28 58 29 3e 3d 53 51 ity(X) ((X)>=SQ
5eeb0 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 LITE_AFF_NUMERIC
5eec0 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 53 51 4c )../*.** The SQL
5eed0 49 54 45 5f 41 46 46 5f 4d 41 53 4b 20 76 61 6c ITE_AFF_MASK val
5eee0 75 65 73 20 6d 61 73 6b 73 20 6f 66 66 20 74 68 ues masks off th
5eef0 65 20 73 69 67 6e 69 66 69 63 61 6e 74 20 62 69 e significant bi
5ef00 74 73 20 6f 66 20 61 6e 0a 2a 2a 20 61 66 66 69 ts of an.** affi
5ef10 6e 69 74 79 20 76 61 6c 75 65 2e 20 0a 2a 2f 0a nity value. .*/.
5ef20 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 #define SQLITE_A
5ef30 46 46 5f 4d 41 53 4b 20 20 20 20 20 30 78 36 37 FF_MASK 0x67
5ef40 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 69 74 69 6f 6e ../*.** Addition
5ef50 61 6c 20 62 69 74 20 76 61 6c 75 65 73 20 74 68 al bit values th
5ef60 61 74 20 63 61 6e 20 62 65 20 4f 52 65 64 20 77 at can be ORed w
5ef70 69 74 68 20 61 6e 20 61 66 66 69 6e 69 74 79 20 ith an affinity
5ef80 77 69 74 68 6f 75 74 0a 2a 2a 20 63 68 61 6e 67 without.** chang
5ef90 69 6e 67 20 74 68 65 20 61 66 66 69 6e 69 74 79 ing the affinity
5efa0 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c ..*/.#define SQL
5efb0 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20 20 ITE_JUMPIFNULL
5efc0 20 30 78 30 38 20 20 2f 2a 20 6a 75 6d 70 73 20 0x08 /* jumps
5efd0 69 66 20 65 69 74 68 65 72 20 6f 70 65 72 61 6e if either operan
5efe0 64 20 69 73 20 4e 55 4c 4c 20 2a 2f 0a 23 64 65 d is NULL */.#de
5eff0 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 4f 52 fine SQLITE_STOR
5f000 45 50 32 20 20 20 20 20 20 30 78 31 30 20 20 2f EP2 0x10 /
5f010 2a 20 53 74 6f 72 65 20 72 65 73 75 6c 74 20 69 * Store result i
5f020 6e 20 72 65 67 5b 50 32 5d 20 72 61 74 68 65 72 n reg[P2] rather
5f030 20 74 68 61 6e 20 6a 75 6d 70 20 2a 2f 0a 23 64 than jump */.#d
5f040 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 55 4c efine SQLITE_NUL
5f050 4c 45 51 20 20 20 20 20 20 20 30 78 38 30 20 20 LEQ 0x80
5f060 2f 2a 20 4e 55 4c 4c 3d 4e 55 4c 4c 20 2a 2f 0a /* NULL=NULL */.
5f070 0a 2f 2a 0a 2a 2a 20 41 6e 20 6f 62 6a 65 63 74 ./*.** An object
5f080 20 6f 66 20 74 68 69 73 20 74 79 70 65 20 69 73 of this type is
5f090 20 63 72 65 61 74 65 64 20 66 6f 72 20 65 61 63 created for eac
5f0a0 68 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 h virtual table
5f0b0 70 72 65 73 65 6e 74 20 69 6e 0a 2a 2a 20 74 68 present in.** th
5f0c0 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d e database schem
5f0d0 61 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 a. .**.** If the
5f0e0 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 database schema
5f0f0 20 69 73 20 73 68 61 72 65 64 2c 20 74 68 65 6e is shared, then
5f100 20 74 68 65 72 65 20 69 73 20 6f 6e 65 20 69 6e there is one in
5f110 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 0a 2a stance of this.*
5f120 2a 20 73 74 72 75 63 74 75 72 65 20 66 6f 72 20 * structure for
5f130 65 61 63 68 20 64 61 74 61 62 61 73 65 20 63 6f each database co
5f140 6e 6e 65 63 74 69 6f 6e 20 28 73 71 6c 69 74 65 nnection (sqlite
5f150 33 2a 29 20 74 68 61 74 20 75 73 65 73 20 74 68 3*) that uses th
5f160 65 20 73 68 61 72 65 64 0a 2a 2a 20 73 63 68 65 e shared.** sche
5f170 6d 61 2e 20 54 68 69 73 20 69 73 20 62 65 63 61 ma. This is beca
5f180 75 73 65 20 65 61 63 68 20 64 61 74 61 62 61 73 use each databas
5f190 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 72 65 71 e connection req
5f1a0 75 69 72 65 73 20 69 74 73 20 6f 77 6e 20 75 6e uires its own un
5f1b0 69 71 75 65 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 ique.** instance
5f1c0 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f of the sqlite3_
5f1d0 76 74 61 62 2a 20 68 61 6e 64 6c 65 20 75 73 65 vtab* handle use
5f1e0 64 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 20 d to access the
5f1f0 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 0a 2a virtual table .*
5f200 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e * implementation
5f210 2e 20 73 71 6c 69 74 65 33 5f 76 74 61 62 2a 20 . sqlite3_vtab*
5f220 68 61 6e 64 6c 65 73 20 63 61 6e 20 6e 6f 74 20 handles can not
5f230 62 65 20 73 68 61 72 65 64 20 62 65 74 77 65 65 be shared betwee
5f240 6e 20 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 n .** database c
5f250 6f 6e 6e 65 63 74 69 6f 6e 73 2c 20 65 76 65 6e onnections, even
5f260 20 77 68 65 6e 20 74 68 65 20 72 65 73 74 20 6f when the rest o
5f270 66 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 f the in-memory
5f280 64 61 74 61 62 61 73 65 20 0a 2a 2a 20 73 63 68 database .** sch
5f290 65 6d 61 20 69 73 20 73 68 61 72 65 64 2c 20 61 ema is shared, a
5f2a0 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 s the implementa
5f2b0 74 69 6f 6e 20 6f 66 74 65 6e 20 73 74 6f 72 65 tion often store
5f2c0 73 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a s the database.*
5f2d0 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 6e * connection han
5f2e0 64 6c 65 20 70 61 73 73 65 64 20 74 6f 20 69 74 dle passed to it
5f2f0 20 76 69 61 20 74 68 65 20 78 43 6f 6e 6e 65 63 via the xConnec
5f300 74 28 29 20 6f 72 20 78 43 72 65 61 74 65 28 29 t() or xCreate()
5f310 20 6d 65 74 68 6f 64 0a 2a 2a 20 64 75 72 69 6e method.** durin
5f320 67 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e g initialization
5f330 20 69 6e 74 65 72 6e 61 6c 6c 79 2e 20 54 68 69 internally. Thi
5f340 73 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 s database conne
5f350 63 74 69 6f 6e 20 68 61 6e 64 6c 65 20 6d 61 79 ction handle may
5f360 0a 2a 2a 20 74 68 65 6e 20 75 73 65 64 20 62 79 .** then used by
5f370 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 the virtual tab
5f380 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f le implementatio
5f390 6e 20 74 6f 20 61 63 63 65 73 73 20 72 65 61 6c n to access real
5f3a0 20 74 61 62 6c 65 73 20 0a 2a 2a 20 77 69 74 68 tables .** with
5f3b0 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2e in the database.
5f3c0 20 53 6f 20 74 68 61 74 20 74 68 65 79 20 61 70 So that they ap
5f3d0 70 65 61 72 20 61 73 20 70 61 72 74 20 6f 66 20 pear as part of
5f3e0 74 68 65 20 63 61 6c 6c 65 72 73 20 0a 2a 2a 20 the callers .**
5f3f0 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65 transaction, the
5f400 73 65 20 61 63 63 65 73 73 65 73 20 6e 65 65 64 se accesses need
5f410 20 74 6f 20 62 65 20 6d 61 64 65 20 76 69 61 20 to be made via
5f420 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 the same databas
5f430 65 20 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e e .** connection
5f440 20 61 73 20 74 68 61 74 20 75 73 65 64 20 74 6f as that used to
5f450 20 65 78 65 63 75 74 65 20 53 51 4c 20 6f 70 65 execute SQL ope
5f460 72 61 74 69 6f 6e 73 20 6f 6e 20 74 68 65 20 76 rations on the v
5f470 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a 2a 2a irtual table..**
5f480 0a 2a 2a 20 41 6c 6c 20 56 54 61 62 6c 65 20 6f .** All VTable o
5f490 62 6a 65 63 74 73 20 74 68 61 74 20 63 6f 72 72 bjects that corr
5f4a0 65 73 70 6f 6e 64 20 74 6f 20 61 20 73 69 6e 67 espond to a sing
5f4b0 6c 65 20 74 61 62 6c 65 20 69 6e 20 61 20 73 68 le table in a sh
5f4c0 61 72 65 64 0a 2a 2a 20 64 61 74 61 62 61 73 65 ared.** database
5f4d0 20 73 63 68 65 6d 61 20 61 72 65 20 69 6e 69 74 schema are init
5f4e0 69 61 6c 6c 79 20 73 74 6f 72 65 64 20 69 6e 20 ially stored in
5f4f0 61 20 6c 69 6e 6b 65 64 2d 6c 69 73 74 20 70 6f a linked-list po
5f500 69 6e 74 65 64 20 74 6f 20 62 79 0a 2a 2a 20 74 inted to by.** t
5f510 68 65 20 54 61 62 6c 65 2e 70 56 54 61 62 6c 65 he Table.pVTable
5f520 20 6d 65 6d 62 65 72 20 76 61 72 69 61 62 6c 65 member variable
5f530 20 6f 66 20 74 68 65 20 63 6f 72 72 65 73 70 6f of the correspo
5f540 6e 64 69 6e 67 20 54 61 62 6c 65 20 6f 62 6a 65 nding Table obje
5f550 63 74 2e 0a 2a 2a 20 57 68 65 6e 20 61 6e 20 73 ct..** When an s
5f560 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 qlite3_prepare()
5f570 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 72 65 operation is re
5f580 71 75 69 72 65 64 20 74 6f 20 61 63 63 65 73 73 quired to access
5f590 20 74 68 65 20 76 69 72 74 75 61 6c 0a 2a 2a 20 the virtual.**
5f5a0 74 61 62 6c 65 2c 20 69 74 20 73 65 61 72 63 68 table, it search
5f5b0 65 73 20 74 68 65 20 6c 69 73 74 20 66 6f 72 20 es the list for
5f5c0 74 68 65 20 56 54 61 62 6c 65 20 74 68 61 74 20 the VTable that
5f5d0 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 74 corresponds to t
5f5e0 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 he.** database c
5f5f0 6f 6e 6e 65 63 74 69 6f 6e 20 64 6f 69 6e 67 20 onnection doing
5f600 74 68 65 20 70 72 65 70 61 72 69 6e 67 20 73 6f the preparing so
5f610 20 61 73 20 74 6f 20 75 73 65 20 74 68 65 20 63 as to use the c
5f620 6f 72 72 65 63 74 0a 2a 2a 20 73 71 6c 69 74 65 orrect.** sqlite
5f630 33 5f 76 74 61 62 2a 20 68 61 6e 64 6c 65 20 69 3_vtab* handle i
5f640 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65 64 20 71 n the compiled q
5f650 75 65 72 79 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e uery..**.** When
5f660 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 54 61 an in-memory Ta
5f670 62 6c 65 20 6f 62 6a 65 63 74 20 69 73 20 64 65 ble object is de
5f680 6c 65 74 65 64 20 28 66 6f 72 20 65 78 61 6d 70 leted (for examp
5f690 6c 65 20 77 68 65 6e 20 74 68 65 0a 2a 2a 20 73 le when the.** s
5f6a0 63 68 65 6d 61 20 69 73 20 62 65 69 6e 67 20 72 chema is being r
5f6b0 65 6c 6f 61 64 65 64 20 66 6f 72 20 73 6f 6d 65 eloaded for some
5f6c0 20 72 65 61 73 6f 6e 29 2c 20 74 68 65 20 56 54 reason), the VT
5f6d0 61 62 6c 65 20 6f 62 6a 65 63 74 73 20 61 72 65 able objects are
5f6e0 20 6e 6f 74 20 0a 2a 2a 20 64 65 6c 65 74 65 64 not .** deleted
5f6f0 20 61 6e 64 20 74 68 65 20 73 71 6c 69 74 65 33 and the sqlite3
5f700 5f 76 74 61 62 2a 20 68 61 6e 64 6c 65 73 20 61 _vtab* handles a
5f710 72 65 20 6e 6f 74 20 78 44 69 73 63 6f 6e 6e 65 re not xDisconne
5f720 63 74 28 29 65 64 20 0a 2a 2a 20 69 6d 6d 65 64 ct()ed .** immed
5f730 69 61 74 65 6c 79 2e 20 49 6e 73 74 65 61 64 2c iately. Instead,
5f740 20 74 68 65 79 20 61 72 65 20 6d 6f 76 65 64 20 they are moved
5f750 66 72 6f 6d 20 74 68 65 20 54 61 62 6c 65 2e 70 from the Table.p
5f760 56 54 61 62 6c 65 20 6c 69 73 74 20 74 6f 0a 2a VTable list to.*
5f770 2a 20 61 6e 6f 74 68 65 72 20 6c 69 6e 6b 65 64 * another linked
5f780 20 6c 69 73 74 20 68 65 61 64 65 64 20 62 79 20 list headed by
5f790 74 68 65 20 73 71 6c 69 74 65 33 2e 70 44 69 73 the sqlite3.pDis
5f7a0 63 6f 6e 6e 65 63 74 20 6d 65 6d 62 65 72 20 6f connect member o
5f7b0 66 20 74 68 65 0a 2a 2a 20 63 6f 72 72 65 73 70 f the.** corresp
5f7c0 6f 6e 64 69 6e 67 20 73 71 6c 69 74 65 33 20 73 onding sqlite3 s
5f7d0 74 72 75 63 74 75 72 65 2e 20 54 68 65 79 20 61 tructure. They a
5f7e0 72 65 20 74 68 65 6e 20 64 65 6c 65 74 65 64 2f re then deleted/
5f7f0 78 44 69 73 63 6f 6e 6e 65 63 74 65 64 20 0a 2a xDisconnected .*
5f800 2a 20 6e 65 78 74 20 74 69 6d 65 20 61 20 73 74 * next time a st
5f810 61 74 65 6d 65 6e 74 20 69 73 20 70 72 65 70 61 atement is prepa
5f820 72 65 64 20 75 73 69 6e 67 20 73 61 69 64 20 73 red using said s
5f830 71 6c 69 74 65 33 2a 2e 20 54 68 69 73 20 69 73 qlite3*. This is
5f840 20 64 6f 6e 65 0a 2a 2a 20 74 6f 20 61 76 6f 69 done.** to avoi
5f850 64 20 64 65 61 64 6c 6f 63 6b 20 69 73 73 75 65 d deadlock issue
5f860 73 20 69 6e 76 6f 6c 76 69 6e 67 20 6d 75 6c 74 s involving mult
5f870 69 70 6c 65 20 73 71 6c 69 74 65 33 2e 6d 75 74 iple sqlite3.mut
5f880 65 78 20 6d 75 74 65 78 65 73 2e 0a 2a 2a 20 52 ex mutexes..** R
5f890 65 66 65 72 20 74 6f 20 63 6f 6d 6d 65 6e 74 73 efer to comments
5f8a0 20 61 62 6f 76 65 20 66 75 6e 63 74 69 6f 6e 20 above function
5f8b0 73 71 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f 63 sqlite3VtabUnloc
5f8c0 6b 4c 69 73 74 28 29 20 66 6f 72 20 61 6e 0a 2a kList() for an.*
5f8d0 2a 20 65 78 70 6c 61 6e 61 74 69 6f 6e 20 61 73 * explanation as
5f8e0 20 74 6f 20 77 68 79 20 69 74 20 69 73 20 73 61 to why it is sa
5f8f0 66 65 20 74 6f 20 61 64 64 20 61 6e 20 65 6e 74 fe to add an ent
5f900 72 79 20 74 6f 20 61 6e 20 73 71 6c 69 74 65 33 ry to an sqlite3
5f910 2e 70 44 69 73 63 6f 6e 6e 65 63 74 0a 2a 2a 20 .pDisconnect.**
5f920 6c 69 73 74 20 77 69 74 68 6f 75 74 20 68 6f 6c list without hol
5f930 64 69 6e 67 20 74 68 65 20 63 6f 72 72 65 73 70 ding the corresp
5f940 6f 6e 64 69 6e 67 20 73 71 6c 69 74 65 33 2e 6d onding sqlite3.m
5f950 75 74 65 78 20 6d 75 74 65 78 2e 0a 2a 2a 0a 2a utex mutex..**.*
5f960 2a 20 54 68 65 20 6d 65 6d 6f 72 79 20 66 6f 72 * The memory for
5f970 20 6f 62 6a 65 63 74 73 20 6f 66 20 74 68 69 73 objects of this
5f980 20 74 79 70 65 20 69 73 20 61 6c 77 61 79 73 20 type is always
5f990 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 0a 2a 2a allocated by .**
5f9a0 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 sqlite3DbMalloc
5f9b0 28 29 2c 20 75 73 69 6e 67 20 74 68 65 20 63 6f (), using the co
5f9c0 6e 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c 65 20 nnection handle
5f9d0 73 74 6f 72 65 64 20 69 6e 20 56 54 61 62 6c 65 stored in VTable
5f9e0 2e 64 62 20 61 73 20 0a 2a 2a 20 74 68 65 20 66 .db as .** the f
5f9f0 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e 0a 2a irst argument..*
5fa00 2f 0a 73 74 72 75 63 74 20 56 54 61 62 6c 65 20 /.struct VTable
5fa10 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b {. sqlite3 *db;
5fa20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
5fa30 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 Database connec
5fa40 74 69 6f 6e 20 61 73 73 6f 63 69 61 74 65 64 20 tion associated
5fa50 77 69 74 68 20 74 68 69 73 20 74 61 62 6c 65 20 with this table
5fa60 2a 2f 0a 20 20 4d 6f 64 75 6c 65 20 2a 70 4d 6f */. Module *pMo
5fa70 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f d; /
5fa80 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 6d 6f 64 * Pointer to mod
5fa90 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 ule implementati
5faa0 6f 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f on */. sqlite3_
5fab0 76 74 61 62 20 2a 70 56 74 61 62 3b 20 20 20 20 vtab *pVtab;
5fac0 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 /* Pointer to
5fad0 76 74 61 62 20 69 6e 73 74 61 6e 63 65 20 2a 2f vtab instance */
5fae0 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 . int nRef;
5faf0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
5fb00 4e 75 6d 62 65 72 20 6f 66 20 70 6f 69 6e 74 65 Number of pointe
5fb10 72 73 20 74 6f 20 74 68 69 73 20 73 74 72 75 63 rs to this struc
5fb20 74 75 72 65 20 2a 2f 0a 20 20 56 54 61 62 6c 65 ture */. VTable
5fb30 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 20 20 *pNext;
5fb40 20 20 20 20 2f 2a 20 4e 65 78 74 20 69 6e 20 6c /* Next in l
5fb50 69 6e 6b 65 64 20 6c 69 73 74 20 28 73 65 65 20 inked list (see
5fb60 61 62 6f 76 65 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a above) */.};../*
5fb70 0a 2a 2a 20 45 61 63 68 20 53 51 4c 20 74 61 62 .** Each SQL tab
5fb80 6c 65 20 69 73 20 72 65 70 72 65 73 65 6e 74 65 le is represente
5fb90 64 20 69 6e 20 6d 65 6d 6f 72 79 20 62 79 20 61 d in memory by a
5fba0 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 n instance of th
5fbb0 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 e.** following s
5fbc0 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 tructure..**.**
5fbd0 54 61 62 6c 65 2e 7a 4e 61 6d 65 20 69 73 20 74 Table.zName is t
5fbe0 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 he name of the t
5fbf0 61 62 6c 65 2e 20 20 54 68 65 20 63 61 73 65 20 able. The case
5fc00 6f 66 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 0a of the original.
5fc10 2a 2a 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 ** CREATE TABLE
5fc20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 73 74 6f statement is sto
5fc30 72 65 64 2c 20 62 75 74 20 63 61 73 65 20 69 73 red, but case is
5fc40 20 6e 6f 74 20 73 69 67 6e 69 66 69 63 61 6e 74 not significant
5fc50 20 66 6f 72 0a 2a 2a 20 63 6f 6d 70 61 72 69 73 for.** comparis
5fc60 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 61 62 6c 65 ons..**.** Table
5fc70 2e 6e 43 6f 6c 20 69 73 20 74 68 65 20 6e 75 6d .nCol is the num
5fc80 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 ber of columns i
5fc90 6e 20 74 68 69 73 20 74 61 62 6c 65 2e 20 20 54 n this table. T
5fca0 61 62 6c 65 2e 61 43 6f 6c 20 69 73 20 61 0a 2a able.aCol is a.*
5fcb0 2a 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 * pointer to an
5fcc0 61 72 72 61 79 20 6f 66 20 43 6f 6c 75 6d 6e 20 array of Column
5fcd0 73 74 72 75 63 74 75 72 65 73 2c 20 6f 6e 65 20 structures, one
5fce0 66 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e 2e for each column.
5fcf0 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 74 61 .**.** If the ta
5fd00 62 6c 65 20 68 61 73 20 61 6e 20 49 4e 54 45 47 ble has an INTEG
5fd10 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 ER PRIMARY KEY,
5fd20 74 68 65 6e 20 54 61 62 6c 65 2e 69 50 4b 65 79 then Table.iPKey
5fd30 20 69 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66 is the index of
5fd40 0a 2a 2a 20 74 68 65 20 63 6f 6c 75 6d 6e 20 74 .** the column t
5fd50 68 61 74 20 69 73 20 74 68 61 74 20 6b 65 79 2e hat is that key.
5fd60 20 20 20 4f 74 68 65 72 77 69 73 65 20 54 61 62 Otherwise Tab
5fd70 6c 65 2e 69 50 4b 65 79 20 69 73 20 6e 65 67 61 le.iPKey is nega
5fd80 74 69 76 65 2e 20 20 4e 6f 74 65 0a 2a 2a 20 74 tive. Note.** t
5fd90 68 61 74 20 74 68 65 20 64 61 74 61 74 79 70 65 hat the datatype
5fda0 20 6f 66 20 74 68 65 20 50 52 49 4d 41 52 59 20 of the PRIMARY
5fdb0 4b 45 59 20 6d 75 73 74 20 62 65 20 49 4e 54 45 KEY must be INTE
5fdc0 47 45 52 20 66 6f 72 20 74 68 69 73 20 66 69 65 GER for this fie
5fdd0 6c 64 20 74 6f 0a 2a 2a 20 62 65 20 73 65 74 2e ld to.** be set.
5fde0 20 20 41 6e 20 49 4e 54 45 47 45 52 20 50 52 49 An INTEGER PRI
5fdf0 4d 41 52 59 20 4b 45 59 20 69 73 20 75 73 65 64 MARY KEY is used
5fe00 20 61 73 20 74 68 65 20 72 6f 77 69 64 20 66 6f as the rowid fo
5fe10 72 20 65 61 63 68 20 72 6f 77 20 6f 66 0a 2a 2a r each row of.**
5fe20 20 74 68 65 20 74 61 62 6c 65 2e 20 20 49 66 20 the table. If
5fe30 61 20 74 61 62 6c 65 20 68 61 73 20 6e 6f 20 49 a table has no I
5fe40 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b NTEGER PRIMARY K
5fe50 45 59 2c 20 74 68 65 6e 20 61 20 72 61 6e 64 6f EY, then a rando
5fe60 6d 20 72 6f 77 69 64 0a 2a 2a 20 69 73 20 67 65 m rowid.** is ge
5fe70 6e 65 72 61 74 65 64 20 66 6f 72 20 65 61 63 68 nerated for each
5fe80 20 72 6f 77 20 6f 66 20 74 68 65 20 74 61 62 6c row of the tabl
5fe90 65 2e 20 20 54 46 5f 48 61 73 50 72 69 6d 61 72 e. TF_HasPrimar
5fea0 79 4b 65 79 20 69 73 20 73 65 74 20 69 66 0a 2a yKey is set if.*
5feb0 2a 20 74 68 65 20 74 61 62 6c 65 20 68 61 73 20 * the table has
5fec0 61 6e 79 20 50 52 49 4d 41 52 59 20 4b 45 59 2c any PRIMARY KEY,
5fed0 20 49 4e 54 45 47 45 52 20 6f 72 20 6f 74 68 65 INTEGER or othe
5fee0 72 77 69 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 61 62 rwise..**.** Tab
5fef0 6c 65 2e 74 6e 75 6d 20 69 73 20 74 68 65 20 70 le.tnum is the p
5ff00 61 67 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 age number for t
5ff10 68 65 20 72 6f 6f 74 20 42 54 72 65 65 20 70 61 he root BTree pa
5ff20 67 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 ge of the table
5ff30 69 6e 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 in the.** databa
5ff40 73 65 20 66 69 6c 65 2e 20 20 49 66 20 54 61 62 se file. If Tab
5ff50 6c 65 2e 69 44 62 20 69 73 20 74 68 65 20 69 6e le.iDb is the in
5ff60 64 65 78 20 6f 66 20 74 68 65 20 64 61 74 61 62 dex of the datab
5ff70 61 73 65 20 74 61 62 6c 65 20 62 61 63 6b 65 6e ase table backen
5ff80 64 0a 2a 2a 20 69 6e 20 73 71 6c 69 74 65 2e 61 d.** in sqlite.a
5ff90 44 62 5b 5d 2e 20 20 30 20 69 73 20 66 6f 72 20 Db[]. 0 is for
5ffa0 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 the main databas
5ffb0 65 20 61 6e 64 20 31 20 69 73 20 66 6f 72 20 74 e and 1 is for t
5ffc0 68 65 20 66 69 6c 65 20 74 68 61 74 0a 2a 2a 20 he file that.**
5ffd0 68 6f 6c 64 73 20 74 65 6d 70 6f 72 61 72 79 20 holds temporary
5ffe0 74 61 62 6c 65 73 20 61 6e 64 20 69 6e 64 69 63 tables and indic
5fff0 65 73 2e 20 20 49 66 20 54 46 5f 45 70 68 65 6d es. If TF_Ephem
60000 65 72 61 6c 20 69 73 20 73 65 74 0a 2a 2a 20 74 eral is set.** t
60010 68 65 6e 20 74 68 65 20 74 61 62 6c 65 20 69 73 hen the table is
60020 20 73 74 6f 72 65 64 20 69 6e 20 61 20 66 69 6c stored in a fil
60030 65 20 74 68 61 74 20 69 73 20 61 75 74 6f 6d 61 e that is automa
60040 74 69 63 61 6c 6c 79 20 64 65 6c 65 74 65 64 0a tically deleted.
60050 2a 2a 20 77 68 65 6e 20 74 68 65 20 56 44 42 45 ** when the VDBE
60060 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 74 cursor to the t
60070 61 62 6c 65 20 69 73 20 63 6c 6f 73 65 64 2e 20 able is closed.
60080 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 54 61 In this case Ta
60090 62 6c 65 2e 74 6e 75 6d 20 0a 2a 2a 20 72 65 66 ble.tnum .** ref
600a0 65 72 73 20 56 44 42 45 20 63 75 72 73 6f 72 20 ers VDBE cursor
600b0 6e 75 6d 62 65 72 20 74 68 61 74 20 68 6f 6c 64 number that hold
600c0 73 20 74 68 65 20 74 61 62 6c 65 20 6f 70 65 6e s the table open
600d0 2c 20 6e 6f 74 20 74 6f 20 74 68 65 20 72 6f 6f , not to the roo
600e0 74 0a 2a 2a 20 70 61 67 65 20 6e 75 6d 62 65 72 t.** page number
600f0 2e 20 20 54 72 61 6e 73 69 65 6e 74 20 74 61 62 . Transient tab
60100 6c 65 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 les are used to
60110 68 6f 6c 64 20 74 68 65 20 72 65 73 75 6c 74 73 hold the results
60120 20 6f 66 20 61 0a 2a 2a 20 73 75 62 2d 71 75 65 of a.** sub-que
60130 72 79 20 74 68 61 74 20 61 70 70 65 61 72 73 20 ry that appears
60140 69 6e 73 74 65 61 64 20 6f 66 20 61 20 72 65 61 instead of a rea
60150 6c 20 74 61 62 6c 65 20 6e 61 6d 65 20 69 6e 20 l table name in
60160 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 the FROM clause
60170 0a 2a 2a 20 6f 66 20 61 20 53 45 4c 45 43 54 20 .** of a SELECT
60180 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 statement..*/.st
60190 72 75 63 74 20 54 61 62 6c 65 20 7b 0a 20 20 73 ruct Table {. s
601a0 71 6c 69 74 65 33 20 2a 64 62 4d 65 6d 3b 20 20 qlite3 *dbMem;
601b0 20 20 20 20 2f 2a 20 44 42 20 63 6f 6e 6e 65 63 /* DB connec
601c0 74 69 6f 6e 20 75 73 65 64 20 66 6f 72 20 6c 6f tion used for lo
601d0 6f 6b 61 73 69 64 65 20 61 6c 6c 6f 63 61 74 69 okaside allocati
601e0 6f 6e 73 2e 20 2a 2f 0a 20 20 63 68 61 72 20 2a ons. */. char *
601f0 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 2f zName; /
60200 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 * Name of the ta
60210 62 6c 65 20 6f 72 20 76 69 65 77 20 2a 2f 0a 20 ble or view */.
60220 20 69 6e 74 20 69 50 4b 65 79 3b 20 20 20 20 20 int iPKey;
60230 20 20 20 20 20 20 2f 2a 20 49 66 20 6e 6f 74 20 /* If not
60240 6e 65 67 61 74 69 76 65 2c 20 75 73 65 20 61 43 negative, use aC
60250 6f 6c 5b 69 50 4b 65 79 5d 20 61 73 20 74 68 65 ol[iPKey] as the
60260 20 70 72 69 6d 61 72 79 20 6b 65 79 20 2a 2f 0a primary key */.
60270 20 20 69 6e 74 20 6e 43 6f 6c 3b 20 20 20 20 20 int nCol;
60280 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
60290 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 of columns in t
602a0 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20 43 his table */. C
602b0 6f 6c 75 6d 6e 20 2a 61 43 6f 6c 3b 20 20 20 20 olumn *aCol;
602c0 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 /* Informati
602d0 6f 6e 20 61 62 6f 75 74 20 65 61 63 68 20 63 6f on about each co
602e0 6c 75 6d 6e 20 2a 2f 0a 20 20 49 6e 64 65 78 20 lumn */. Index
602f0 2a 70 49 6e 64 65 78 3b 20 20 20 20 20 20 20 2f *pIndex; /
60300 2a 20 4c 69 73 74 20 6f 66 20 53 51 4c 20 69 6e * List of SQL in
60310 64 65 78 65 73 20 6f 6e 20 74 68 69 73 20 74 61 dexes on this ta
60320 62 6c 65 2e 20 2a 2f 0a 20 20 69 6e 74 20 74 6e ble. */. int tn
60330 75 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f um; /
60340 2a 20 52 6f 6f 74 20 42 54 72 65 65 20 6e 6f 64 * Root BTree nod
60350 65 20 66 6f 72 20 74 68 69 73 20 74 61 62 6c 65 e for this table
60360 20 28 73 65 65 20 6e 6f 74 65 20 61 62 6f 76 65 (see note above
60370 29 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 ) */. Select *p
60380 53 65 6c 65 63 74 3b 20 20 20 20 20 2f 2a 20 4e Select; /* N
60390 55 4c 4c 20 66 6f 72 20 74 61 62 6c 65 73 2e 20 ULL for tables.
603a0 20 50 6f 69 6e 74 73 20 74 6f 20 64 65 66 69 6e Points to defin
603b0 69 74 69 6f 6e 20 69 66 20 61 20 76 69 65 77 2e ition if a view.
603c0 20 2a 2f 0a 20 20 75 31 36 20 6e 52 65 66 3b 20 */. u16 nRef;
603d0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
603e0 6d 62 65 72 20 6f 66 20 70 6f 69 6e 74 65 72 73 mber of pointers
603f0 20 74 6f 20 74 68 69 73 20 54 61 62 6c 65 20 2a to this Table *
60400 2f 0a 20 20 75 38 20 74 61 62 46 6c 61 67 73 3b /. u8 tabFlags;
60410 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b /* Mask
60420 20 6f 66 20 54 46 5f 2a 20 76 61 6c 75 65 73 20 of TF_* values
60430 2a 2f 0a 20 20 75 38 20 6b 65 79 43 6f 6e 66 3b */. u8 keyConf;
60440 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 68 61 /* Wha
60450 74 20 74 6f 20 64 6f 20 69 6e 20 63 61 73 65 20 t to do in case
60460 6f 66 20 75 6e 69 71 75 65 6e 65 73 73 20 63 6f of uniqueness co
60470 6e 66 6c 69 63 74 20 6f 6e 20 69 50 4b 65 79 20 nflict on iPKey
60480 2a 2f 0a 20 20 46 4b 65 79 20 2a 70 46 4b 65 79 */. FKey *pFKey
60490 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 6e ; /* Lin
604a0 6b 65 64 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 ked list of all
604b0 66 6f 72 65 69 67 6e 20 6b 65 79 73 20 69 6e 20 foreign keys in
604c0 74 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20 this table */.
604d0 63 68 61 72 20 2a 7a 43 6f 6c 41 66 66 3b 20 20 char *zColAff;
604e0 20 20 20 20 20 2f 2a 20 53 74 72 69 6e 67 20 64 /* String d
604f0 65 66 69 6e 69 6e 67 20 74 68 65 20 61 66 66 69 efining the affi
60500 6e 69 74 79 20 6f 66 20 65 61 63 68 20 63 6f 6c nity of each col
60510 75 6d 6e 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 umn */.#ifndef S
60520 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 45 43 4b QLITE_OMIT_CHECK
60530 0a 20 20 45 78 70 72 20 2a 70 43 68 65 63 6b 3b . Expr *pCheck;
60540 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 41 /* The A
60550 4e 44 20 6f 66 20 61 6c 6c 20 43 48 45 43 4b 20 ND of all CHECK
60560 63 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a 23 constraints */.#
60570 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51 endif.#ifndef SQ
60580 4c 49 54 45 5f 4f 4d 49 54 5f 41 4c 54 45 52 54 LITE_OMIT_ALTERT
60590 41 42 4c 45 0a 20 20 69 6e 74 20 61 64 64 43 6f ABLE. int addCo
605a0 6c 4f 66 66 73 65 74 3b 20 20 20 20 2f 2a 20 4f lOffset; /* O
605b0 66 66 73 65 74 20 69 6e 20 43 52 45 41 54 45 20 ffset in CREATE
605c0 54 41 42 4c 45 20 73 74 6d 74 20 74 6f 20 61 64 TABLE stmt to ad
605d0 64 20 61 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 2a d a new column *
605e0 2f 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 /.#endif.#ifndef
605f0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 SQLITE_OMIT_VIR
60600 54 55 41 4c 54 41 42 4c 45 0a 20 20 56 54 61 62 TUALTABLE. VTab
60610 6c 65 20 2a 70 56 54 61 62 6c 65 3b 20 20 20 20 le *pVTable;
60620 20 2f 2a 20 4c 69 73 74 20 6f 66 20 56 54 61 62 /* List of VTab
60630 6c 65 20 6f 62 6a 65 63 74 73 2e 20 2a 2f 0a 20 le objects. */.
60640 20 69 6e 74 20 6e 4d 6f 64 75 6c 65 41 72 67 3b int nModuleArg;
60650 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
60660 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 of arguments to
60670 74 68 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 the module */.
60680 63 68 61 72 20 2a 2a 61 7a 4d 6f 64 75 6c 65 41 char **azModuleA
60690 72 67 3b 20 20 2f 2a 20 54 65 78 74 20 6f 66 20 rg; /* Text of
606a0 61 6c 6c 20 6d 6f 64 75 6c 65 20 61 72 67 73 2e all module args.
606b0 20 5b 30 5d 20 69 73 20 6d 6f 64 75 6c 65 20 6e [0] is module n
606c0 61 6d 65 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 ame */.#endif.
606d0 54 72 69 67 67 65 72 20 2a 70 54 72 69 67 67 65 Trigger *pTrigge
606e0 72 3b 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 r; /* List of
606f0 74 72 69 67 67 65 72 73 20 73 74 6f 72 65 64 20 triggers stored
60700 69 6e 20 70 53 63 68 65 6d 61 20 2a 2f 0a 20 20 in pSchema */.
60710 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 3b Schema *pSchema;
60720 20 20 20 20 20 2f 2a 20 53 63 68 65 6d 61 20 74 /* Schema t
60730 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 69 hat contains thi
60740 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20 54 61 62 s table */. Tab
60750 6c 65 20 2a 70 4e 65 78 74 5a 6f 6d 62 69 65 3b le *pNextZombie;
60760 20 20 2f 2a 20 4e 65 78 74 20 6f 6e 20 74 68 65 /* Next on the
60770 20 50 61 72 73 65 2e 70 5a 6f 6d 62 69 65 54 61 Parse.pZombieTa
60780 62 20 6c 69 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a b list */.};../*
60790 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 .** Allowed valu
607a0 65 73 20 66 6f 72 20 54 61 62 65 2e 74 61 62 46 es for Tabe.tabF
607b0 6c 61 67 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 lags..*/.#define
607c0 20 54 46 5f 52 65 61 64 6f 6e 6c 79 20 20 20 20 TF_Readonly
607d0 20 20 20 20 30 78 30 31 20 20 20 20 2f 2a 20 52 0x01 /* R
607e0 65 61 64 2d 6f 6e 6c 79 20 73 79 73 74 65 6d 20 ead-only system
607f0 74 61 62 6c 65 20 2a 2f 0a 23 64 65 66 69 6e 65 table */.#define
60800 20 54 46 5f 45 70 68 65 6d 65 72 61 6c 20 20 20 TF_Ephemeral
60810 20 20 20 20 30 78 30 32 20 20 20 20 2f 2a 20 41 0x02 /* A
60820 6e 20 65 70 68 65 6d 65 72 61 6c 20 74 61 62 6c n ephemeral tabl
60830 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54 46 5f e */.#define TF_
60840 48 61 73 50 72 69 6d 61 72 79 4b 65 79 20 20 20 HasPrimaryKey
60850 30 78 30 34 20 20 20 20 2f 2a 20 54 61 62 6c 65 0x04 /* Table
60860 20 68 61 73 20 61 20 70 72 69 6d 61 72 79 20 6b has a primary k
60870 65 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54 46 ey */.#define TF
60880 5f 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74 20 20 _Autoincrement
60890 20 30 78 30 38 20 20 20 20 2f 2a 20 49 6e 74 65 0x08 /* Inte
608a0 67 65 72 20 70 72 69 6d 61 72 79 20 6b 65 79 20 ger primary key
608b0 69 73 20 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74 is autoincrement
608c0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54 46 5f 56 */.#define TF_V
608d0 69 72 74 75 61 6c 20 20 20 20 20 20 20 20 20 30 irtual 0
608e0 78 31 30 20 20 20 20 2f 2a 20 49 73 20 61 20 76 x10 /* Is a v
608f0 69 72 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a irtual table */.
60900 23 64 65 66 69 6e 65 20 54 46 5f 4e 65 65 64 4d #define TF_NeedM
60910 65 74 61 64 61 74 61 20 20 20 20 30 78 32 30 20 etadata 0x20
60920 20 20 20 2f 2a 20 61 43 6f 6c 5b 5d 2e 7a 54 79 /* aCol[].zTy
60930 70 65 20 61 6e 64 20 61 43 6f 6c 5b 5d 2e 70 43 pe and aCol[].pC
60940 6f 6c 6c 20 6d 69 73 73 69 6e 67 20 2a 2f 0a 0a oll missing */..
60950 0a 0a 2f 2a 0a 2a 2a 20 54 65 73 74 20 74 6f 20 ../*.** Test to
60960 73 65 65 20 77 68 65 74 68 65 72 20 6f 72 20 6e see whether or n
60970 6f 74 20 61 20 74 61 62 6c 65 20 69 73 20 61 20 ot a table is a
60980 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 virtual table.
60990 54 68 69 73 20 69 73 0a 2a 2a 20 64 6f 6e 65 20 This is.** done
609a0 61 73 20 61 20 6d 61 63 72 6f 20 73 6f 20 74 68 as a macro so th
609b0 61 74 20 69 74 20 77 69 6c 6c 20 62 65 20 6f 70 at it will be op
609c0 74 69 6d 69 7a 65 64 20 6f 75 74 20 77 68 65 6e timized out when
609d0 20 76 69 72 74 75 61 6c 0a 2a 2a 20 74 61 62 6c virtual.** tabl
609e0 65 20 73 75 70 70 6f 72 74 20 69 73 20 6f 6d 69 e support is omi
609f0 74 74 65 64 20 66 72 6f 6d 20 74 68 65 20 62 75 tted from the bu
60a00 69 6c 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 ild..*/.#ifndef
60a10 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 SQLITE_OMIT_VIRT
60a20 55 41 4c 54 41 42 4c 45 0a 23 20 20 64 65 66 69 UALTABLE.# defi
60a30 6e 65 20 49 73 56 69 72 74 75 61 6c 28 58 29 20 ne IsVirtual(X)
60a40 20 20 20 20 20 28 28 28 58 29 2d 3e 74 61 62 46 (((X)->tabF
60a50 6c 61 67 73 20 26 20 54 46 5f 56 69 72 74 75 61 lags & TF_Virtua
60a60 6c 29 21 3d 30 29 0a 23 20 20 64 65 66 69 6e 65 l)!=0).# define
60a70 20 49 73 48 69 64 64 65 6e 43 6f 6c 75 6d 6e 28 IsHiddenColumn(
60a80 58 29 20 28 28 58 29 2d 3e 69 73 48 69 64 64 65 X) ((X)->isHidde
60a90 6e 29 0a 23 65 6c 73 65 0a 23 20 20 64 65 66 69 n).#else.# defi
60aa0 6e 65 20 49 73 56 69 72 74 75 61 6c 28 58 29 20 ne IsVirtual(X)
60ab0 20 20 20 20 20 30 0a 23 20 20 64 65 66 69 6e 65 0.# define
60ac0 20 49 73 48 69 64 64 65 6e 43 6f 6c 75 6d 6e 28 IsHiddenColumn(
60ad0 58 29 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a X) 0.#endif../*.
60ae0 2a 2a 20 45 61 63 68 20 66 6f 72 65 69 67 6e 20 ** Each foreign
60af0 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 69 key constraint i
60b00 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 s an instance of
60b10 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 the following s
60b20 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 tructure..**.**
60b30 41 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 69 73 A foreign key is
60b40 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
60b50 20 74 77 6f 20 74 61 62 6c 65 73 2e 20 20 54 68 two tables. Th
60b60 65 20 22 66 72 6f 6d 22 20 74 61 62 6c 65 20 69 e "from" table i
60b70 73 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 74 s.** the table t
60b80 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 hat contains the
60b90 20 52 45 46 45 52 45 4e 43 45 53 20 63 6c 61 75 REFERENCES clau
60ba0 73 65 20 74 68 61 74 20 63 72 65 61 74 65 73 20 se that creates
60bb0 74 68 65 20 66 6f 72 65 69 67 6e 0a 2a 2a 20 6b the foreign.** k
60bc0 65 79 2e 20 20 54 68 65 20 22 74 6f 22 20 74 61 ey. The "to" ta
60bd0 62 6c 65 20 69 73 20 74 68 65 20 74 61 62 6c 65 ble is the table
60be0 20 74 68 61 74 20 69 73 20 6e 61 6d 65 64 20 69 that is named i
60bf0 6e 20 74 68 65 20 52 45 46 45 52 45 4e 43 45 53 n the REFERENCES
60c00 20 63 6c 61 75 73 65 2e 0a 2a 2a 20 43 6f 6e 73 clause..** Cons
60c10 69 64 65 72 20 74 68 69 73 20 65 78 61 6d 70 6c ider this exampl
60c20 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 43 52 45 e:.**.** CRE
60c30 41 54 45 20 54 41 42 4c 45 20 65 78 31 28 0a 2a ATE TABLE ex1(.*
60c40 2a 20 20 20 20 20 20 20 61 20 49 4e 54 45 47 45 * a INTEGE
60c50 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 0a 2a R PRIMARY KEY,.*
60c60 2a 20 20 20 20 20 20 20 62 20 49 4e 54 45 47 45 * b INTEGE
60c70 52 20 43 4f 4e 53 54 52 41 49 4e 54 20 66 6b 31 R CONSTRAINT fk1
60c80 20 52 45 46 45 52 45 4e 43 45 53 20 65 78 32 28 REFERENCES ex2(
60c90 78 29 0a 2a 2a 20 20 20 20 20 29 3b 0a 2a 2a 0a x).** );.**.
60ca0 2a 2a 20 46 6f 72 20 66 6f 72 65 69 67 6e 20 6b ** For foreign k
60cb0 65 79 20 22 66 6b 31 22 2c 20 74 68 65 20 66 72 ey "fk1", the fr
60cc0 6f 6d 2d 74 61 62 6c 65 20 69 73 20 22 65 78 31 om-table is "ex1
60cd0 22 20 61 6e 64 20 74 68 65 20 74 6f 2d 74 61 62 " and the to-tab
60ce0 6c 65 20 69 73 20 22 65 78 32 22 2e 0a 2a 2a 0a le is "ex2"..**.
60cf0 2a 2a 20 45 61 63 68 20 52 45 46 45 52 45 4e 43 ** Each REFERENC
60d00 45 53 20 63 6c 61 75 73 65 20 67 65 6e 65 72 61 ES clause genera
60d10 74 65 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 tes an instance
60d20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 of the following
60d30 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 77 68 structure.** wh
60d40 69 63 68 20 69 73 20 61 74 74 61 63 68 65 64 20 ich is attached
60d50 74 6f 20 74 68 65 20 66 72 6f 6d 2d 74 61 62 6c to the from-tabl
60d60 65 2e 20 20 54 68 65 20 74 6f 2d 74 61 62 6c 65 e. The to-table
60d70 20 6e 65 65 64 20 6e 6f 74 20 65 78 69 73 74 20 need not exist
60d80 77 68 65 6e 0a 2a 2a 20 74 68 65 20 66 72 6f 6d when.** the from
60d90 2d 74 61 62 6c 65 20 69 73 20 63 72 65 61 74 65 -table is create
60da0 64 2e 20 20 54 68 65 20 65 78 69 73 74 65 6e 63 d. The existenc
60db0 65 20 6f 66 20 74 68 65 20 74 6f 2d 74 61 62 6c e of the to-tabl
60dc0 65 20 69 73 20 6e 6f 74 20 63 68 65 63 6b 65 64 e is not checked
60dd0 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 46 4b 65 79 ..*/.struct FKey
60de0 20 7b 0a 20 20 54 61 62 6c 65 20 2a 70 46 72 6f {. Table *pFro
60df0 6d 3b 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 m; /* Table
60e00 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 52 containing the R
60e10 45 46 45 52 45 4e 43 45 53 20 63 6c 61 75 73 65 EFERENCES clause
60e20 20 28 61 6b 61 3a 20 43 68 69 6c 64 29 20 2a 2f (aka: Child) */
60e30 0a 20 20 46 4b 65 79 20 2a 70 4e 65 78 74 46 72 . FKey *pNextFr
60e40 6f 6d 3b 20 20 2f 2a 20 4e 65 78 74 20 66 6f 72 om; /* Next for
60e50 65 69 67 6e 20 6b 65 79 20 69 6e 20 70 46 72 6f eign key in pFro
60e60 6d 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 54 6f m */. char *zTo
60e70 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 ; /* Name
60e80 20 6f 66 20 74 61 62 6c 65 20 74 68 61 74 20 74 of table that t
60e90 68 65 20 6b 65 79 20 70 6f 69 6e 74 73 20 74 6f he key points to
60ea0 20 28 61 6b 61 3a 20 50 61 72 65 6e 74 29 20 2a (aka: Parent) *
60eb0 2f 0a 20 20 46 4b 65 79 20 2a 70 4e 65 78 74 54 /. FKey *pNextT
60ec0 6f 3b 20 20 20 20 2f 2a 20 4e 65 78 74 20 66 6f o; /* Next fo
60ed0 72 65 69 67 6e 20 6b 65 79 20 6f 6e 20 74 61 62 reign key on tab
60ee0 6c 65 20 6e 61 6d 65 64 20 7a 54 6f 20 2a 2f 0a le named zTo */.
60ef0 20 20 46 4b 65 79 20 2a 70 50 72 65 76 54 6f 3b FKey *pPrevTo;
60f00 20 20 20 20 2f 2a 20 50 72 65 76 69 6f 75 73 20 /* Previous
60f10 66 6f 72 65 69 67 6e 20 6b 65 79 20 6f 6e 20 74 foreign key on t
60f20 61 62 6c 65 20 6e 61 6d 65 64 20 7a 54 6f 20 2a able named zTo *
60f30 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 3b 20 20 20 /. int nCol;
60f40 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
60f50 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 of columns in th
60f60 69 73 20 6b 65 79 20 2a 2f 0a 20 20 2f 2a 20 45 is key */. /* E
60f70 56 3a 20 52 2d 33 30 33 32 33 2d 32 31 39 31 37 V: R-30323-21917
60f80 20 2a 2f 0a 20 20 75 38 20 69 73 44 65 66 65 72 */. u8 isDefer
60f90 72 65 64 3b 20 20 20 20 2f 2a 20 54 72 75 65 20 red; /* True
60fa0 69 66 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 68 if constraint ch
60fb0 65 63 6b 69 6e 67 20 69 73 20 64 65 66 65 72 72 ecking is deferr
60fc0 65 64 20 74 69 6c 6c 20 43 4f 4d 4d 49 54 20 2a ed till COMMIT *
60fd0 2f 0a 20 20 75 38 20 61 41 63 74 69 6f 6e 5b 32 /. u8 aAction[2
60fe0 5d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f ]; /* O
60ff0 4e 20 44 45 4c 45 54 45 20 61 6e 64 20 4f 4e 20 N DELETE and ON
61000 55 50 44 41 54 45 20 61 63 74 69 6f 6e 73 2c 20 UPDATE actions,
61010 72 65 73 70 65 63 74 69 76 65 6c 79 20 2a 2f 0a respectively */.
61020 20 20 54 72 69 67 67 65 72 20 2a 61 70 54 72 69 Trigger *apTri
61030 67 67 65 72 5b 32 5d 3b 20 20 2f 2a 20 54 72 69 gger[2]; /* Tri
61040 67 67 65 72 73 20 66 6f 72 20 61 41 63 74 69 6f ggers for aActio
61050 6e 5b 5d 20 61 63 74 69 6f 6e 73 20 2a 2f 0a 20 n[] actions */.
61060 20 73 74 72 75 63 74 20 73 43 6f 6c 4d 61 70 20 struct sColMap
61070 7b 20 20 2f 2a 20 4d 61 70 70 69 6e 67 20 6f 66 { /* Mapping of
61080 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 70 46 72 6f columns in pFro
61090 6d 20 74 6f 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 m to columns in
610a0 7a 54 6f 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 zTo */. int i
610b0 46 72 6f 6d 3b 20 20 20 20 20 20 20 20 20 2f 2a From; /*
610c0 20 49 6e 64 65 78 20 6f 66 20 63 6f 6c 75 6d 6e Index of column
610d0 20 69 6e 20 70 46 72 6f 6d 20 2a 2f 0a 20 20 20 in pFrom */.
610e0 20 63 68 61 72 20 2a 7a 43 6f 6c 3b 20 20 20 20 char *zCol;
610f0 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 63 /* Name of c
61100 6f 6c 75 6d 6e 20 69 6e 20 7a 54 6f 2e 20 20 49 olumn in zTo. I
61110 66 20 30 20 75 73 65 20 50 52 49 4d 41 52 59 20 f 0 use PRIMARY
61120 4b 45 59 20 2a 2f 0a 20 20 7d 20 61 43 6f 6c 5b KEY */. } aCol[
61130 31 5d 3b 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 1]; /* On
61140 65 20 65 6e 74 72 79 20 66 6f 72 20 65 61 63 68 e entry for each
61150 20 6f 66 20 6e 43 6f 6c 20 63 6f 6c 75 6d 6e 20 of nCol column
61160 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 53 s */.};../*.** S
61170 51 4c 69 74 65 20 73 75 70 70 6f 72 74 73 20 6d QLite supports m
61180 61 6e 79 20 64 69 66 66 65 72 65 6e 74 20 77 61 any different wa
61190 79 73 20 74 6f 20 72 65 73 6f 6c 76 65 20 61 20 ys to resolve a
611a0 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 65 72 constraint.** er
611b0 72 6f 72 2e 20 20 52 4f 4c 4c 42 41 43 4b 20 70 ror. ROLLBACK p
611c0 72 6f 63 65 73 73 69 6e 67 20 6d 65 61 6e 73 20 rocessing means
611d0 74 68 61 74 20 61 20 63 6f 6e 73 74 72 61 69 6e that a constrain
611e0 74 20 76 69 6f 6c 61 74 69 6f 6e 0a 2a 2a 20 63 t violation.** c
611f0 61 75 73 65 73 20 74 68 65 20 6f 70 65 72 61 74 auses the operat
61200 69 6f 6e 20 69 6e 20 70 72 6f 63 65 73 73 20 74 ion in process t
61210 6f 20 66 61 69 6c 20 61 6e 64 20 66 6f 72 20 74 o fail and for t
61220 68 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 he current trans
61230 61 63 74 69 6f 6e 0a 2a 2a 20 74 6f 20 62 65 20 action.** to be
61240 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 20 41 42 rolled back. AB
61250 4f 52 54 20 70 72 6f 63 65 73 73 69 6e 67 20 6d ORT processing m
61260 65 61 6e 73 20 74 68 65 20 6f 70 65 72 61 74 69 eans the operati
61270 6f 6e 20 69 6e 20 70 72 6f 63 65 73 73 0a 2a 2a on in process.**
61280 20 66 61 69 6c 73 20 61 6e 64 20 61 6e 79 20 70 fails and any p
61290 72 69 6f 72 20 63 68 61 6e 67 65 73 20 66 72 6f rior changes fro
612a0 6d 20 74 68 61 74 20 6f 6e 65 20 6f 70 65 72 61 m that one opera
612b0 74 69 6f 6e 20 61 72 65 20 62 61 63 6b 65 64 20 tion are backed
612c0 6f 75 74 2c 0a 2a 2a 20 62 75 74 20 74 68 65 20 out,.** but the
612d0 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 6e transaction is n
612e0 6f 74 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 ot rolled back.
612f0 20 46 41 49 4c 20 70 72 6f 63 65 73 73 69 6e 67 FAIL processing
61300 20 6d 65 61 6e 73 20 74 68 61 74 0a 2a 2a 20 74 means that.** t
61310 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 69 6e 20 he operation in
61320 70 72 6f 67 72 65 73 73 20 73 74 6f 70 73 20 61 progress stops a
61330 6e 64 20 72 65 74 75 72 6e 73 20 61 6e 20 65 72 nd returns an er
61340 72 6f 72 20 63 6f 64 65 2e 20 20 42 75 74 20 70 ror code. But p
61350 72 69 6f 72 0a 2a 2a 20 63 68 61 6e 67 65 73 20 rior.** changes
61360 64 75 65 20 74 6f 20 74 68 65 20 73 61 6d 65 20 due to the same
61370 6f 70 65 72 61 74 69 6f 6e 20 61 72 65 20 6e 6f operation are no
61380 74 20 62 61 63 6b 65 64 20 6f 75 74 20 61 6e 64 t backed out and
61390 20 6e 6f 20 72 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 no rollback.**
613a0 6f 63 63 75 72 73 2e 20 20 49 47 4e 4f 52 45 20 occurs. IGNORE
613b0 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 70 means that the p
613c0 61 72 74 69 63 75 6c 61 72 20 72 6f 77 20 74 68 articular row th
613d0 61 74 20 63 61 75 73 65 64 20 74 68 65 20 63 6f at caused the co
613e0 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 65 72 72 6f nstraint.** erro
613f0 72 20 69 73 20 6e 6f 74 20 69 6e 73 65 72 74 65 r is not inserte
61400 64 20 6f 72 20 75 70 64 61 74 65 64 2e 20 20 50 d or updated. P
61410 72 6f 63 65 73 73 69 6e 67 20 63 6f 6e 74 69 6e rocessing contin
61420 75 65 73 20 61 6e 64 20 6e 6f 20 65 72 72 6f 72 ues and no error
61430 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 2e .** is returned.
61440 20 20 52 45 50 4c 41 43 45 20 6d 65 61 6e 73 20 REPLACE means
61450 74 68 61 74 20 70 72 65 65 78 69 73 74 69 6e 67 that preexisting
61460 20 64 61 74 61 62 61 73 65 20 72 6f 77 73 20 74 database rows t
61470 68 61 74 20 63 61 75 73 65 64 0a 2a 2a 20 61 20 hat caused.** a
61480 55 4e 49 51 55 45 20 63 6f 6e 73 74 72 61 69 6e UNIQUE constrain
61490 74 20 76 69 6f 6c 61 74 69 6f 6e 20 61 72 65 20 t violation are
614a0 72 65 6d 6f 76 65 64 20 73 6f 20 74 68 61 74 20 removed so that
614b0 74 68 65 20 6e 65 77 20 69 6e 73 65 72 74 20 6f the new insert o
614c0 72 0a 2a 2a 20 75 70 64 61 74 65 20 63 61 6e 20 r.** update can
614d0 70 72 6f 63 65 65 64 2e 20 20 50 72 6f 63 65 73 proceed. Proces
614e0 73 69 6e 67 20 63 6f 6e 74 69 6e 75 65 73 20 61 sing continues a
614f0 6e 64 20 6e 6f 20 65 72 72 6f 72 20 69 73 20 72 nd no error is r
61500 65 70 6f 72 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 eported..**.** R
61510 45 53 54 52 49 43 54 2c 20 53 45 54 4e 55 4c 4c ESTRICT, SETNULL
61520 2c 20 61 6e 64 20 43 41 53 43 41 44 45 20 61 63 , and CASCADE ac
61530 74 69 6f 6e 73 20 61 70 70 6c 79 20 6f 6e 6c 79 tions apply only
61540 20 74 6f 20 66 6f 72 65 69 67 6e 20 6b 65 79 73 to foreign keys
61550 2e 0a 2a 2a 20 52 45 53 54 52 49 43 54 20 69 73 ..** RESTRICT is
61560 20 74 68 65 20 73 61 6d 65 20 61 73 20 41 42 4f the same as ABO
61570 52 54 20 66 6f 72 20 49 4d 4d 45 44 49 41 54 45 RT for IMMEDIATE
61580 20 66 6f 72 65 69 67 6e 20 6b 65 79 73 20 61 6e foreign keys an
61590 64 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 61 73 d the.** same as
615a0 20 52 4f 4c 4c 42 41 43 4b 20 66 6f 72 20 44 45 ROLLBACK for DE
615b0 46 45 52 52 45 44 20 6b 65 79 73 2e 20 20 53 45 FERRED keys. SE
615c0 54 4e 55 4c 4c 20 6d 65 61 6e 73 20 74 68 61 74 TNULL means that
615d0 20 74 68 65 20 66 6f 72 65 69 67 6e 0a 2a 2a 20 the foreign.**
615e0 6b 65 79 20 69 73 20 73 65 74 20 74 6f 20 4e 55 key is set to NU
615f0 4c 4c 2e 20 20 43 41 53 43 41 44 45 20 6d 65 61 LL. CASCADE mea
61600 6e 73 20 74 68 61 74 20 61 20 44 45 4c 45 54 45 ns that a DELETE
61610 20 6f 72 20 55 50 44 41 54 45 20 6f 66 20 74 68 or UPDATE of th
61620 65 0a 2a 2a 20 72 65 66 65 72 65 6e 63 65 64 20 e.** referenced
61630 74 61 62 6c 65 20 72 6f 77 20 69 73 20 70 72 6f table row is pro
61640 70 61 67 61 74 65 64 20 69 6e 74 6f 20 74 68 65 pagated into the
61650 20 72 6f 77 20 74 68 61 74 20 68 6f 6c 64 73 20 row that holds
61660 74 68 65 0a 2a 2a 20 66 6f 72 65 69 67 6e 20 6b the.** foreign k
61670 65 79 2e 0a 2a 2a 20 0a 2a 2a 20 54 68 65 20 66 ey..** .** The f
61680 6f 6c 6c 6f 77 69 6e 67 20 73 79 6d 62 6f 6c 69 ollowing symboli
61690 63 20 76 61 6c 75 65 73 20 61 72 65 20 75 73 65 c values are use
616a0 64 20 74 6f 20 72 65 63 6f 72 64 20 77 68 69 63 d to record whic
616b0 68 20 74 79 70 65 0a 2a 2a 20 6f 66 20 61 63 74 h type.** of act
616c0 69 6f 6e 20 74 6f 20 74 61 6b 65 2e 0a 2a 2f 0a ion to take..*/.
616d0 23 64 65 66 69 6e 65 20 4f 45 5f 4e 6f 6e 65 20 #define OE_None
616e0 20 20 20 20 30 20 20 20 2f 2a 20 54 68 65 72 65 0 /* There
616f0 20 69 73 20 6e 6f 20 63 6f 6e 73 74 72 61 69 6e is no constrain
61700 74 20 74 6f 20 63 68 65 63 6b 20 2a 2f 0a 23 64 t to check */.#d
61710 65 66 69 6e 65 20 4f 45 5f 52 6f 6c 6c 62 61 63 efine OE_Rollbac
61720 6b 20 31 20 20 20 2f 2a 20 46 61 69 6c 20 74 68 k 1 /* Fail th
61730 65 20 6f 70 65 72 61 74 69 6f 6e 20 61 6e 64 20 e operation and
61740 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20 74 72 61 rollback the tra
61750 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 nsaction */.#def
61760 69 6e 65 20 4f 45 5f 41 62 6f 72 74 20 20 20 20 ine OE_Abort
61770 32 20 20 20 2f 2a 20 42 61 63 6b 20 6f 75 74 20 2 /* Back out
61780 63 68 61 6e 67 65 73 20 62 75 74 20 64 6f 20 6e changes but do n
61790 6f 20 72 6f 6c 6c 62 61 63 6b 20 74 72 61 6e 73 o rollback trans
617a0 61 63 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e action */.#defin
617b0 65 20 4f 45 5f 46 61 69 6c 20 20 20 20 20 33 20 e OE_Fail 3
617c0 20 20 2f 2a 20 53 74 6f 70 20 74 68 65 20 6f 70 /* Stop the op
617d0 65 72 61 74 69 6f 6e 20 62 75 74 20 6c 65 61 76 eration but leav
617e0 65 20 61 6c 6c 20 70 72 69 6f 72 20 63 68 61 6e e all prior chan
617f0 67 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f ges */.#define O
61800 45 5f 49 67 6e 6f 72 65 20 20 20 34 20 20 20 2f E_Ignore 4 /
61810 2a 20 49 67 6e 6f 72 65 20 74 68 65 20 65 72 72 * Ignore the err
61820 6f 72 2e 20 44 6f 20 6e 6f 74 20 64 6f 20 74 68 or. Do not do th
61830 65 20 49 4e 53 45 52 54 20 6f 72 20 55 50 44 41 e INSERT or UPDA
61840 54 45 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 45 TE */.#define OE
61850 5f 52 65 70 6c 61 63 65 20 20 35 20 20 20 2f 2a _Replace 5 /*
61860 20 44 65 6c 65 74 65 20 65 78 69 73 74 69 6e 67 Delete existing
61870 20 72 65 63 6f 72 64 2c 20 74 68 65 6e 20 64 6f record, then do
61880 20 49 4e 53 45 52 54 20 6f 72 20 55 50 44 41 54 INSERT or UPDAT
61890 45 20 2a 2f 0a 0a 23 64 65 66 69 6e 65 20 4f 45 E */..#define OE
618a0 5f 52 65 73 74 72 69 63 74 20 36 20 20 20 2f 2a _Restrict 6 /*
618b0 20 4f 45 5f 41 62 6f 72 74 20 66 6f 72 20 49 4d OE_Abort for IM
618c0 4d 45 44 49 41 54 45 2c 20 4f 45 5f 52 6f 6c 6c MEDIATE, OE_Roll
618d0 62 61 63 6b 20 66 6f 72 20 44 45 46 45 52 52 45 back for DEFERRE
618e0 44 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 45 5f D */.#define OE_
618f0 53 65 74 4e 75 6c 6c 20 20 37 20 20 20 2f 2a 20 SetNull 7 /*
61900 53 65 74 20 74 68 65 20 66 6f 72 65 69 67 6e 20 Set the foreign
61910 6b 65 79 20 76 61 6c 75 65 20 74 6f 20 4e 55 4c key value to NUL
61920 4c 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 45 5f L */.#define OE_
61930 53 65 74 44 66 6c 74 20 20 38 20 20 20 2f 2a 20 SetDflt 8 /*
61940 53 65 74 20 74 68 65 20 66 6f 72 65 69 67 6e 20 Set the foreign
61950 6b 65 79 20 76 61 6c 75 65 20 74 6f 20 69 74 73 key value to its
61960 20 64 65 66 61 75 6c 74 20 2a 2f 0a 23 64 65 66 default */.#def
61970 69 6e 65 20 4f 45 5f 43 61 73 63 61 64 65 20 20 ine OE_Cascade
61980 39 20 20 20 2f 2a 20 43 61 73 63 61 64 65 20 74 9 /* Cascade t
61990 68 65 20 63 68 61 6e 67 65 73 20 2a 2f 0a 0a 23 he changes */..#
619a0 64 65 66 69 6e 65 20 4f 45 5f 44 65 66 61 75 6c define OE_Defaul
619b0 74 20 20 39 39 20 20 2f 2a 20 44 6f 20 77 68 61 t 99 /* Do wha
619c0 74 65 76 65 72 20 74 68 65 20 64 65 66 61 75 6c tever the defaul
619d0 74 20 61 63 74 69 6f 6e 20 69 73 20 2a 2f 0a 0a t action is */..
619e0 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e ./*.** An instan
619f0 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 ce of the follow
61a00 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 69 73 ing structure is
61a10 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 66 passed as the f
61a20 69 72 73 74 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 irst.** argument
61a30 20 74 6f 20 73 71 6c 69 74 65 33 56 64 62 65 4b to sqlite3VdbeK
61a40 65 79 43 6f 6d 70 61 72 65 20 61 6e 64 20 69 73 eyCompare and is
61a50 20 75 73 65 64 20 74 6f 20 63 6f 6e 74 72 6f 6c used to control
61a60 20 74 68 65 20 0a 2a 2a 20 63 6f 6d 70 61 72 69 the .** compari
61a70 73 6f 6e 20 6f 66 20 74 68 65 20 74 77 6f 20 69 son of the two i
61a80 6e 64 65 78 20 6b 65 79 73 2e 0a 2a 2f 0a 73 74 ndex keys..*/.st
61a90 72 75 63 74 20 4b 65 79 49 6e 66 6f 20 7b 0a 20 ruct KeyInfo {.
61aa0 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 sqlite3 *db;
61ab0 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 /* The data
61ac0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
61ad0 2a 2f 0a 20 20 75 38 20 65 6e 63 3b 20 20 20 20 */. u8 enc;
61ae0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 78 74 /* Text
61af0 20 65 6e 63 6f 64 69 6e 67 20 2d 20 6f 6e 65 20 encoding - one
61b00 6f 66 20 74 68 65 20 54 45 58 54 5f 55 74 66 2a of the TEXT_Utf*
61b10 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 75 31 36 values */. u16
61b20 20 6e 46 69 65 6c 64 3b 20 20 20 20 20 20 20 20 nField;
61b30 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e /* Number of en
61b40 74 72 69 65 73 20 69 6e 20 61 43 6f 6c 6c 5b 5d tries in aColl[]
61b50 20 2a 2f 0a 20 20 75 38 20 2a 61 53 6f 72 74 4f */. u8 *aSortO
61b60 72 64 65 72 3b 20 20 20 20 20 2f 2a 20 49 66 20 rder; /* If
61b70 64 65 66 69 6e 65 64 20 61 6e 20 61 53 6f 72 74 defined an aSort
61b80 4f 72 64 65 72 5b 69 5d 20 69 73 20 74 72 75 65 Order[i] is true
61b90 2c 20 73 6f 72 74 20 44 45 53 43 20 2a 2f 0a 20 , sort DESC */.
61ba0 20 43 6f 6c 6c 53 65 71 20 2a 61 43 6f 6c 6c 5b CollSeq *aColl[
61bb0 31 5d 3b 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e 1]; /* Collatin
61bc0 67 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20 65 g sequence for e
61bd0 61 63 68 20 74 65 72 6d 20 6f 66 20 74 68 65 20 ach term of the
61be0 6b 65 79 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a key */.};../*.**
61bf0 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 An instance of
61c00 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 the following st
61c10 72 75 63 74 75 72 65 20 68 6f 6c 64 73 20 69 6e ructure holds in
61c20 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 formation about
61c30 61 0a 2a 2a 20 73 69 6e 67 6c 65 20 69 6e 64 65 a.** single inde
61c40 78 20 72 65 63 6f 72 64 20 74 68 61 74 20 68 61 x record that ha
61c50 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 70 s already been p
61c60 61 72 73 65 64 20 6f 75 74 20 69 6e 74 6f 20 69 arsed out into i
61c70 6e 64 69 76 69 64 75 61 6c 0a 2a 2a 20 76 61 6c ndividual.** val
61c80 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 72 65 63 ues..**.** A rec
61c90 6f 72 64 20 69 73 20 61 6e 20 6f 62 6a 65 63 74 ord is an object
61ca0 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 6f that contains o
61cb0 6e 65 20 6f 72 20 6d 6f 72 65 20 66 69 65 6c 64 ne or more field
61cc0 73 20 6f 66 20 64 61 74 61 2e 0a 2a 2a 20 52 65 s of data..** Re
61cd0 63 6f 72 64 73 20 61 72 65 20 75 73 65 64 20 74 cords are used t
61ce0 6f 20 73 74 6f 72 65 20 74 68 65 20 63 6f 6e 74 o store the cont
61cf0 65 6e 74 20 6f 66 20 61 20 74 61 62 6c 65 20 72 ent of a table r
61d00 6f 77 20 61 6e 64 20 74 6f 20 73 74 6f 72 65 0a ow and to store.
61d10 2a 2a 20 74 68 65 20 6b 65 79 20 6f 66 20 61 6e ** the key of an
61d20 20 69 6e 64 65 78 2e 20 20 41 20 62 6c 6f 62 20 index. A blob
61d30 65 6e 63 6f 64 69 6e 67 20 6f 66 20 61 20 72 65 encoding of a re
61d40 63 6f 72 64 20 69 73 20 63 72 65 61 74 65 64 20 cord is created
61d50 62 79 0a 2a 2a 20 74 68 65 20 4f 50 5f 4d 61 6b by.** the OP_Mak
61d60 65 52 65 63 6f 72 64 20 6f 70 63 6f 64 65 20 6f eRecord opcode o
61d70 66 20 74 68 65 20 56 44 42 45 20 61 6e 64 20 69 f the VDBE and i
61d80 73 20 64 69 73 61 73 73 65 6d 62 6c 65 64 20 62 s disassembled b
61d90 79 20 74 68 65 0a 2a 2a 20 4f 50 5f 43 6f 6c 75 y the.** OP_Colu
61da0 6d 6e 20 6f 70 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a mn opcode..**.**
61db0 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65 20 This structure
61dc0 68 6f 6c 64 73 20 61 20 72 65 63 6f 72 64 20 74 holds a record t
61dd0 68 61 74 20 68 61 73 20 61 6c 72 65 61 64 79 20 hat has already
61de0 62 65 65 6e 20 64 69 73 61 73 73 65 6d 62 6c 65 been disassemble
61df0 64 0a 2a 2a 20 69 6e 74 6f 20 69 74 73 20 63 6f d.** into its co
61e00 6e 73 74 69 74 75 65 6e 74 20 66 69 65 6c 64 73 nstituent fields
61e10 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 55 6e 70 61 ..*/.struct Unpa
61e20 63 6b 65 64 52 65 63 6f 72 64 20 7b 0a 20 20 4b ckedRecord {. K
61e30 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f eyInfo *pKeyInfo
61e40 3b 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6f 6e 20 ; /* Collation
61e50 61 6e 64 20 73 6f 72 74 2d 6f 72 64 65 72 20 69 and sort-order i
61e60 6e 66 6f 72 6d 61 74 69 6f 6e 20 2a 2f 0a 20 20 nformation */.
61e70 75 31 36 20 6e 46 69 65 6c 64 3b 20 20 20 20 20 u16 nField;
61e80 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
61e90 20 65 6e 74 72 69 65 73 20 69 6e 20 61 70 4d 65 entries in apMe
61ea0 6d 5b 5d 20 2a 2f 0a 20 20 75 31 36 20 66 6c 61 m[] */. u16 fla
61eb0 67 73 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 gs; /*
61ec0 42 6f 6f 6c 65 61 6e 20 73 65 74 74 69 6e 67 73 Boolean settings
61ed0 2e 20 20 55 4e 50 41 43 4b 45 44 5f 2e 2e 2e 20 . UNPACKED_...
61ee0 62 65 6c 6f 77 20 2a 2f 0a 20 20 69 36 34 20 72 below */. i64 r
61ef0 6f 77 69 64 3b 20 20 20 20 20 20 20 20 20 20 2f owid; /
61f00 2a 20 55 73 65 64 20 62 79 20 55 4e 50 41 43 4b * Used by UNPACK
61f10 45 44 5f 50 52 45 46 49 58 5f 53 45 41 52 43 48 ED_PREFIX_SEARCH
61f20 20 2a 2f 0a 20 20 4d 65 6d 20 2a 61 4d 65 6d 3b */. Mem *aMem;
61f30 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c /* Val
61f40 75 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a ues */.};../*.**
61f50 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73 20 Allowed values
61f60 6f 66 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 of UnpackedRecor
61f70 64 2e 66 6c 61 67 73 0a 2a 2f 0a 23 64 65 66 69 d.flags.*/.#defi
61f80 6e 65 20 55 4e 50 41 43 4b 45 44 5f 4e 45 45 44 ne UNPACKED_NEED
61f90 5f 46 52 45 45 20 20 20 20 20 30 78 30 30 30 31 _FREE 0x0001
61fa0 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 69 73 20 66 /* Memory is f
61fb0 72 6f 6d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f rom sqlite3Mallo
61fc0 63 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 55 c() */.#define U
61fd0 4e 50 41 43 4b 45 44 5f 4e 45 45 44 5f 44 45 53 NPACKED_NEED_DES
61fe0 54 52 4f 59 20 20 30 78 30 30 30 32 20 20 2f 2a TROY 0x0002 /*
61ff0 20 61 70 4d 65 6d 5b 5d 73 20 73 68 6f 75 6c 64 apMem[]s should
62000 20 61 6c 6c 20 62 65 20 64 65 73 74 72 6f 79 65 all be destroye
62010 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 55 4e 50 d */.#define UNP
62020 41 43 4b 45 44 5f 49 47 4e 4f 52 45 5f 52 4f 57 ACKED_IGNORE_ROW
62030 49 44 20 20 30 78 30 30 30 34 20 20 2f 2a 20 49 ID 0x0004 /* I
62040 67 6e 6f 72 65 20 74 72 61 69 6c 69 6e 67 20 72 gnore trailing r
62050 6f 77 69 64 20 6f 6e 20 6b 65 79 31 20 2a 2f 0a owid on key1 */.
62060 23 64 65 66 69 6e 65 20 55 4e 50 41 43 4b 45 44 #define UNPACKED
62070 5f 49 4e 43 52 4b 45 59 20 20 20 20 20 20 20 30 _INCRKEY 0
62080 78 30 30 30 38 20 20 2f 2a 20 4d 61 6b 65 20 74 x0008 /* Make t
62090 68 69 73 20 6b 65 79 20 61 6e 20 65 70 73 69 6c his key an epsil
620a0 6f 6e 20 6c 61 72 67 65 72 20 2a 2f 0a 23 64 65 on larger */.#de
620b0 66 69 6e 65 20 55 4e 50 41 43 4b 45 44 5f 50 52 fine UNPACKED_PR
620c0 45 46 49 58 5f 4d 41 54 43 48 20 20 30 78 30 30 EFIX_MATCH 0x00
620d0 31 30 20 20 2f 2a 20 41 20 70 72 65 66 69 78 20 10 /* A prefix
620e0 6d 61 74 63 68 20 69 73 20 63 6f 6e 73 69 64 65 match is conside
620f0 72 65 64 20 4f 4b 20 2a 2f 0a 23 64 65 66 69 6e red OK */.#defin
62100 65 20 55 4e 50 41 43 4b 45 44 5f 50 52 45 46 49 e UNPACKED_PREFI
62110 58 5f 53 45 41 52 43 48 20 30 78 30 30 32 30 20 X_SEARCH 0x0020
62120 20 2f 2a 20 41 20 70 72 65 66 69 78 20 6d 61 74 /* A prefix mat
62130 63 68 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64 ch is considered
62140 20 4f 4b 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 45 61 OK */../*.** Ea
62150 63 68 20 53 51 4c 20 69 6e 64 65 78 20 69 73 20 ch SQL index is
62160 72 65 70 72 65 73 65 6e 74 65 64 20 69 6e 20 6d represented in m
62170 65 6d 6f 72 79 20 62 79 20 61 6e 0a 2a 2a 20 69 emory by an.** i
62180 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 nstance of the f
62190 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 ollowing structu
621a0 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f re..**.** The co
621b0 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20 74 61 62 lumns of the tab
621c0 6c 65 20 74 68 61 74 20 61 72 65 20 74 6f 20 62 le that are to b
621d0 65 20 69 6e 64 65 78 65 64 20 61 72 65 20 64 65 e indexed are de
621e0 73 63 72 69 62 65 64 0a 2a 2a 20 62 79 20 74 68 scribed.** by th
621f0 65 20 61 69 43 6f 6c 75 6d 6e 5b 5d 20 66 69 65 e aiColumn[] fie
62200 6c 64 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 ld of this struc
62210 74 75 72 65 2e 20 20 46 6f 72 20 65 78 61 6d 70 ture. For examp
62220 6c 65 2c 20 73 75 70 70 6f 73 65 0a 2a 2a 20 77 le, suppose.** w
62230 65 20 68 61 76 65 20 74 68 65 20 66 6f 6c 6c 6f e have the follo
62240 77 69 6e 67 20 74 61 62 6c 65 20 61 6e 64 20 69 wing table and i
62250 6e 64 65 78 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 ndex:.**.**
62260 43 52 45 41 54 45 20 54 41 42 4c 45 20 45 78 31 CREATE TABLE Ex1
62270 28 63 31 20 69 6e 74 2c 20 63 32 20 69 6e 74 2c (c1 int, c2 int,
62280 20 63 33 20 74 65 78 74 29 3b 0a 2a 2a 20 20 20 c3 text);.**
62290 20 20 43 52 45 41 54 45 20 49 4e 44 45 58 20 45 CREATE INDEX E
622a0 78 32 20 4f 4e 20 45 78 31 28 63 33 2c 63 31 29 x2 ON Ex1(c3,c1)
622b0 3b 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 54 ;.**.** In the T
622c0 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 20 64 able structure d
622d0 65 73 63 72 69 62 69 6e 67 20 45 78 31 2c 20 6e escribing Ex1, n
622e0 43 6f 6c 3d 3d 33 20 62 65 63 61 75 73 65 20 74 Col==3 because t
622f0 68 65 72 65 20 61 72 65 0a 2a 2a 20 74 68 72 65 here are.** thre
62300 65 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 e columns in the
62310 20 74 61 62 6c 65 2e 20 20 49 6e 20 74 68 65 20 table. In the
62320 49 6e 64 65 78 20 73 74 72 75 63 74 75 72 65 20 Index structure
62330 64 65 73 63 72 69 62 69 6e 67 0a 2a 2a 20 45 78 describing.** Ex
62340 32 2c 20 6e 43 6f 6c 75 6d 6e 3d 3d 32 20 73 69 2, nColumn==2 si
62350 6e 63 65 20 32 20 6f 66 20 74 68 65 20 33 20 63 nce 2 of the 3 c
62360 6f 6c 75 6d 6e 73 20 6f 66 20 45 78 31 20 61 72 olumns of Ex1 ar
62370 65 20 69 6e 64 65 78 65 64 2e 0a 2a 2a 20 54 68 e indexed..** Th
62380 65 20 76 61 6c 75 65 20 6f 66 20 61 69 43 6f 6c e value of aiCol
62390 75 6d 6e 20 69 73 20 7b 32 2c 20 30 7d 2e 20 20 umn is {2, 0}.
623a0 61 69 43 6f 6c 75 6d 6e 5b 30 5d 3d 3d 32 20 62 aiColumn[0]==2 b
623b0 65 63 61 75 73 65 20 74 68 65 20 0a 2a 2a 20 66 ecause the .** f
623c0 69 72 73 74 20 63 6f 6c 75 6d 6e 20 74 6f 20 62 irst column to b
623d0 65 20 69 6e 64 65 78 65 64 20 28 63 33 29 20 68 e indexed (c3) h
623e0 61 73 20 61 6e 20 69 6e 64 65 78 20 6f 66 20 32 as an index of 2
623f0 20 69 6e 20 45 78 31 2e 61 43 6f 6c 5b 5d 2e 0a in Ex1.aCol[]..
62400 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 63 6f ** The second co
62410 6c 75 6d 6e 20 74 6f 20 62 65 20 69 6e 64 65 78 lumn to be index
62420 65 64 20 28 63 31 29 20 68 61 73 20 61 6e 20 69 ed (c1) has an i
62430 6e 64 65 78 20 6f 66 20 30 20 69 6e 0a 2a 2a 20 ndex of 0 in.**
62440 45 78 31 2e 61 43 6f 6c 5b 5d 2c 20 68 65 6e 63 Ex1.aCol[], henc
62450 65 20 45 78 32 2e 61 69 43 6f 6c 75 6d 6e 5b 31 e Ex2.aiColumn[1
62460 5d 3d 3d 30 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ]==0..**.** The
62470 49 6e 64 65 78 2e 6f 6e 45 72 72 6f 72 20 66 69 Index.onError fi
62480 65 6c 64 20 64 65 74 65 72 6d 69 6e 65 73 20 77 eld determines w
62490 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 hether or not th
624a0 65 20 69 6e 64 65 78 65 64 20 63 6f 6c 75 6d 6e e indexed column
624b0 73 0a 2a 2a 20 6d 75 73 74 20 62 65 20 75 6e 69 s.** must be uni
624c0 71 75 65 20 61 6e 64 20 77 68 61 74 20 74 6f 20 que and what to
624d0 64 6f 20 69 66 20 74 68 65 79 20 61 72 65 20 6e do if they are n
624e0 6f 74 2e 20 20 57 68 65 6e 20 49 6e 64 65 78 2e ot. When Index.
624f0 6f 6e 45 72 72 6f 72 3d 4f 45 5f 4e 6f 6e 65 2c onError=OE_None,
62500 0a 2a 2a 20 69 74 20 6d 65 61 6e 73 20 74 68 69 .** it means thi
62510 73 20 69 73 20 6e 6f 74 20 61 20 75 6e 69 71 75 s is not a uniqu
62520 65 20 69 6e 64 65 78 2e 20 20 4f 74 68 65 72 77 e index. Otherw
62530 69 73 65 20 69 74 20 69 73 20 61 20 75 6e 69 71 ise it is a uniq
62540 75 65 20 69 6e 64 65 78 0a 2a 2a 20 61 6e 64 20 ue index.** and
62550 74 68 65 20 76 61 6c 75 65 20 6f 66 20 49 6e 64 the value of Ind
62560 65 78 2e 6f 6e 45 72 72 6f 72 20 69 6e 64 69 63 ex.onError indic
62570 61 74 65 20 74 68 65 20 77 68 69 63 68 20 63 6f ate the which co
62580 6e 66 6c 69 63 74 20 72 65 73 6f 6c 75 74 69 6f nflict resolutio
62590 6e 20 0a 2a 2a 20 61 6c 67 6f 72 69 74 68 6d 20 n .** algorithm
625a0 74 6f 20 65 6d 70 6c 6f 79 20 77 68 65 6e 65 76 to employ whenev
625b0 65 72 20 61 6e 20 61 74 74 65 6d 70 74 20 69 73 er an attempt is
625c0 20 6d 61 64 65 20 74 6f 20 69 6e 73 65 72 74 20 made to insert
625d0 61 20 6e 6f 6e 2d 75 6e 69 71 75 65 0a 2a 2a 20 a non-unique.**
625e0 65 6c 65 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 72 75 element..*/.stru
625f0 63 74 20 49 6e 64 65 78 20 7b 0a 20 20 63 68 61 ct Index {. cha
62600 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 2f 2a r *zName; /*
62610 20 4e 61 6d 65 20 6f 66 20 74 68 69 73 20 69 6e Name of this in
62620 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f dex */. int nCo
62630 6c 75 6d 6e 3b 20 20 20 20 20 2f 2a 20 4e 75 6d lumn; /* Num
62640 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 ber of columns i
62650 6e 20 74 68 65 20 74 61 62 6c 65 20 75 73 65 64 n the table used
62660 20 62 79 20 74 68 69 73 20 69 6e 64 65 78 20 2a by this index *
62670 2f 0a 20 20 69 6e 74 20 2a 61 69 43 6f 6c 75 6d /. int *aiColum
62680 6e 3b 20 20 20 2f 2a 20 57 68 69 63 68 20 63 6f n; /* Which co
62690 6c 75 6d 6e 73 20 61 72 65 20 75 73 65 64 20 62 lumns are used b
626a0 79 20 74 68 69 73 20 69 6e 64 65 78 2e 20 20 31 y this index. 1
626b0 73 74 20 69 73 20 30 20 2a 2f 0a 20 20 75 6e 73 st is 0 */. uns
626c0 69 67 6e 65 64 20 2a 61 69 52 6f 77 45 73 74 3b igned *aiRowEst;
626d0 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66 20 41 4e /* Result of AN
626e0 41 4c 59 5a 45 3a 20 45 73 74 2e 20 72 6f 77 73 ALYZE: Est. rows
626f0 20 73 65 6c 65 63 74 65 64 20 62 79 20 65 61 63 selected by eac
62700 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 54 61 h column */. Ta
62710 62 6c 65 20 2a 70 54 61 62 6c 65 3b 20 20 20 2f ble *pTable; /
62720 2a 20 54 68 65 20 53 51 4c 20 74 61 62 6c 65 20 * The SQL table
62730 62 65 69 6e 67 20 69 6e 64 65 78 65 64 20 2a 2f being indexed */
62740 0a 20 20 69 6e 74 20 74 6e 75 6d 3b 20 20 20 20 . int tnum;
62750 20 20 20 20 2f 2a 20 50 61 67 65 20 63 6f 6e 74 /* Page cont
62760 61 69 6e 69 6e 67 20 72 6f 6f 74 20 6f 66 20 74 aining root of t
62770 68 69 73 20 69 6e 64 65 78 20 69 6e 20 64 61 74 his index in dat
62780 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 abase file */.
62790 75 38 20 6f 6e 45 72 72 6f 72 3b 20 20 20 20 20 u8 onError;
627a0 20 2f 2a 20 4f 45 5f 41 62 6f 72 74 2c 20 4f 45 /* OE_Abort, OE
627b0 5f 49 67 6e 6f 72 65 2c 20 4f 45 5f 52 65 70 6c _Ignore, OE_Repl
627c0 61 63 65 2c 20 6f 72 20 4f 45 5f 4e 6f 6e 65 20 ace, or OE_None
627d0 2a 2f 0a 20 20 75 38 20 61 75 74 6f 49 6e 64 65 */. u8 autoInde
627e0 78 3b 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 x; /* True if
627f0 20 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c is automaticall
62800 79 20 63 72 65 61 74 65 64 20 28 65 78 3a 20 62 y created (ex: b
62810 79 20 55 4e 49 51 55 45 29 20 2a 2f 0a 20 20 63 y UNIQUE) */. c
62820 68 61 72 20 2a 7a 43 6f 6c 41 66 66 3b 20 20 20 har *zColAff;
62830 2f 2a 20 53 74 72 69 6e 67 20 64 65 66 69 6e 69 /* String defini
62840 6e 67 20 74 68 65 20 61 66 66 69 6e 69 74 79 20 ng the affinity
62850 6f 66 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 2a of each column *
62860 2f 0a 20 20 49 6e 64 65 78 20 2a 70 4e 65 78 74 /. Index *pNext
62870 3b 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 78 74 ; /* The next
62880 20 69 6e 64 65 78 20 61 73 73 6f 63 69 61 74 65 index associate
62890 64 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 d with the same
628a0 74 61 62 6c 65 20 2a 2f 0a 20 20 53 63 68 65 6d table */. Schem
628b0 61 20 2a 70 53 63 68 65 6d 61 3b 20 2f 2a 20 53 a *pSchema; /* S
628c0 63 68 65 6d 61 20 63 6f 6e 74 61 69 6e 69 6e 67 chema containing
628d0 20 74 68 69 73 20 69 6e 64 65 78 20 2a 2f 0a 20 this index */.
628e0 20 75 38 20 2a 61 53 6f 72 74 4f 72 64 65 72 3b u8 *aSortOrder;
628f0 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 73 69 /* Array of si
62900 7a 65 20 49 6e 64 65 78 2e 6e 43 6f 6c 75 6d 6e ze Index.nColumn
62910 2e 20 54 72 75 65 3d 3d 44 45 53 43 2c 20 46 61 . True==DESC, Fa
62920 6c 73 65 3d 3d 41 53 43 20 2a 2f 0a 20 20 63 68 lse==ASC */. ch
62930 61 72 20 2a 2a 61 7a 43 6f 6c 6c 3b 20 20 20 2f ar **azColl; /
62940 2a 20 41 72 72 61 79 20 6f 66 20 63 6f 6c 6c 61 * Array of colla
62950 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 6e 61 tion sequence na
62960 6d 65 73 20 66 6f 72 20 69 6e 64 65 78 20 2a 2f mes for index */
62970 0a 20 20 49 6e 64 65 78 53 61 6d 70 6c 65 20 2a . IndexSample *
62980 61 53 61 6d 70 6c 65 3b 20 20 20 20 2f 2a 20 41 aSample; /* A
62990 72 72 61 79 20 6f 66 20 53 51 4c 49 54 45 5f 49 rray of SQLITE_I
629a0 4e 44 45 58 5f 53 41 4d 50 4c 45 53 20 73 61 6d NDEX_SAMPLES sam
629b0 70 6c 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a ples */.};../*.*
629c0 2a 20 45 61 63 68 20 73 61 6d 70 6c 65 20 73 74 * Each sample st
629d0 6f 72 65 64 20 69 6e 20 74 68 65 20 73 71 6c 69 ored in the sqli
629e0 74 65 5f 73 74 61 74 32 20 74 61 62 6c 65 20 69 te_stat2 table i
629f0 73 20 72 65 70 72 65 73 65 6e 74 65 64 20 69 6e s represented in
62a00 20 6d 65 6d 6f 72 79 20 0a 2a 2a 20 75 73 69 6e memory .** usin
62a10 67 20 61 20 73 74 72 75 63 74 75 72 65 20 6f 66 g a structure of
62a20 20 74 68 69 73 20 74 79 70 65 2e 0a 2a 2f 0a 73 this type..*/.s
62a30 74 72 75 63 74 20 49 6e 64 65 78 53 61 6d 70 6c truct IndexSampl
62a40 65 20 7b 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 e {. union {.
62a50 20 20 63 68 61 72 20 2a 7a 3b 20 20 20 20 20 20 char *z;
62a60 20 20 2f 2a 20 56 61 6c 75 65 20 69 66 20 65 54 /* Value if eT
62a70 79 70 65 20 69 73 20 53 51 4c 49 54 45 5f 54 45 ype is SQLITE_TE
62a80 58 54 20 6f 72 20 53 51 4c 49 54 45 5f 42 4c 4f XT or SQLITE_BLO
62a90 42 20 2a 2f 0a 20 20 20 20 64 6f 75 62 6c 65 20 B */. double
62aa0 72 3b 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 r; /* Valu
62ab0 65 20 69 66 20 65 54 79 70 65 20 69 73 20 53 51 e if eType is SQ
62ac0 4c 49 54 45 5f 46 4c 4f 41 54 20 6f 72 20 53 51 LITE_FLOAT or SQ
62ad0 4c 49 54 45 5f 49 4e 54 45 47 45 52 20 2a 2f 0a LITE_INTEGER */.
62ae0 20 20 7d 20 75 3b 0a 20 20 75 38 20 65 54 79 70 } u;. u8 eTyp
62af0 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53 51 e; /* SQ
62b00 4c 49 54 45 5f 4e 55 4c 4c 2c 20 53 51 4c 49 54 LITE_NULL, SQLIT
62b10 45 5f 49 4e 54 45 47 45 52 20 2e 2e 2e 20 65 74 E_INTEGER ... et
62b20 63 2e 20 2a 2f 0a 20 20 75 38 20 6e 42 79 74 65 c. */. u8 nByte
62b30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a ; /* Siz
62b40 65 20 69 6e 20 62 79 74 65 20 6f 66 20 74 65 78 e in byte of tex
62b50 74 20 6f 72 20 62 6c 6f 62 2e 20 2a 2f 0a 7d 3b t or blob. */.};
62b60 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 74 6f 6b ../*.** Each tok
62b70 65 6e 20 63 6f 6d 69 6e 67 20 6f 75 74 20 6f 66 en coming out of
62b80 20 74 68 65 20 6c 65 78 65 72 20 69 73 20 61 6e the lexer is an
62b90 20 69 6e 73 74 61 6e 63 65 20 6f 66 0a 2a 2a 20 instance of.**
62ba0 74 68 69 73 20 73 74 72 75 63 74 75 72 65 2e 20 this structure.
62bb0 20 54 6f 6b 65 6e 73 20 61 72 65 20 61 6c 73 6f Tokens are also
62bc0 20 75 73 65 64 20 61 73 20 70 61 72 74 20 6f 66 used as part of
62bd0 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a an expression..
62be0 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 69 66 20 54 6f **.** Note if To
62bf0 6b 65 6e 2e 7a 3d 3d 30 20 74 68 65 6e 20 54 6f ken.z==0 then To
62c00 6b 65 6e 2e 64 79 6e 20 61 6e 64 20 54 6f 6b 65 ken.dyn and Toke
62c10 6e 2e 6e 20 61 72 65 20 75 6e 64 65 66 69 6e 65 n.n are undefine
62c20 64 20 61 6e 64 0a 2a 2a 20 6d 61 79 20 63 6f 6e d and.** may con
62c30 74 61 69 6e 20 72 61 6e 64 6f 6d 20 76 61 6c 75 tain random valu
62c40 65 73 2e 20 20 44 6f 20 6e 6f 74 20 6d 61 6b 65 es. Do not make
62c50 20 61 6e 79 20 61 73 73 75 6d 70 74 69 6f 6e 73 any assumptions
62c60 20 61 62 6f 75 74 20 54 6f 6b 65 6e 2e 64 79 6e about Token.dyn
62c70 0a 2a 2a 20 61 6e 64 20 54 6f 6b 65 6e 2e 6e 20 .** and Token.n
62c80 77 68 65 6e 20 54 6f 6b 65 6e 2e 7a 3d 3d 30 2e when Token.z==0.
62c90 0a 2a 2f 0a 73 74 72 75 63 74 20 54 6f 6b 65 6e .*/.struct Token
62ca0 20 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 {. const char
62cb0 2a 7a 3b 20 20 20 20 20 2f 2a 20 54 65 78 74 20 *z; /* Text
62cc0 6f 66 20 74 68 65 20 74 6f 6b 65 6e 2e 20 20 4e of the token. N
62cd0 6f 74 20 4e 55 4c 4c 2d 74 65 72 6d 69 6e 61 74 ot NULL-terminat
62ce0 65 64 21 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 ed! */. unsigne
62cf0 64 20 69 6e 74 20 6e 3b 20 20 20 20 2f 2a 20 4e d int n; /* N
62d00 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 umber of charact
62d10 65 72 73 20 69 6e 20 74 68 69 73 20 74 6f 6b 65 ers in this toke
62d20 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 n */.};../*.** A
62d30 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 n instance of th
62d40 69 73 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e is structure con
62d50 74 61 69 6e 73 20 69 6e 66 6f 72 6d 61 74 69 6f tains informatio
62d60 6e 20 6e 65 65 64 65 64 20 74 6f 20 67 65 6e 65 n needed to gene
62d70 72 61 74 65 0a 2a 2a 20 63 6f 64 65 20 66 6f 72 rate.** code for
62d80 20 61 20 53 45 4c 45 43 54 20 74 68 61 74 20 63 a SELECT that c
62d90 6f 6e 74 61 69 6e 73 20 61 67 67 72 65 67 61 74 ontains aggregat
62da0 65 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 0a e functions..**.
62db0 2a 2a 20 49 66 20 45 78 70 72 2e 6f 70 3d 3d 54 ** If Expr.op==T
62dc0 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 6f 72 20 K_AGG_COLUMN or
62dd0 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 20 TK_AGG_FUNCTION
62de0 74 68 65 6e 20 45 78 70 72 2e 70 41 67 67 49 6e then Expr.pAggIn
62df0 66 6f 20 69 73 20 61 0a 2a 2a 20 70 6f 69 6e 74 fo is a.** point
62e00 65 72 20 74 6f 20 74 68 69 73 20 73 74 72 75 63 er to this struc
62e10 74 75 72 65 2e 20 20 54 68 65 20 45 78 70 72 2e ture. The Expr.
62e20 69 43 6f 6c 75 6d 6e 20 66 69 65 6c 64 20 69 73 iColumn field is
62e30 20 74 68 65 20 69 6e 64 65 78 20 69 6e 0a 2a 2a the index in.**
62e40 20 41 67 67 49 6e 66 6f 2e 61 43 6f 6c 5b 5d 20 AggInfo.aCol[]
62e50 6f 72 20 41 67 67 49 6e 66 6f 2e 61 46 75 6e 63 or AggInfo.aFunc
62e60 5b 5d 20 6f 66 20 69 6e 66 6f 72 6d 61 74 69 6f [] of informatio
62e70 6e 20 6e 65 65 64 65 64 20 74 6f 20 67 65 6e 65 n needed to gene
62e80 72 61 74 65 0a 2a 2a 20 63 6f 64 65 20 66 6f 72 rate.** code for
62e90 20 74 68 61 74 20 6e 6f 64 65 2e 0a 2a 2a 0a 2a that node..**.*
62ea0 2a 20 41 67 67 49 6e 66 6f 2e 70 47 72 6f 75 70 * AggInfo.pGroup
62eb0 42 79 20 61 6e 64 20 41 67 67 49 6e 66 6f 2e 61 By and AggInfo.a
62ec0 46 75 6e 63 2e 70 45 78 70 72 20 70 6f 69 6e 74 Func.pExpr point
62ed0 20 74 6f 20 66 69 65 6c 64 73 20 77 69 74 68 69 to fields withi
62ee0 6e 20 74 68 65 0a 2a 2a 20 6f 72 69 67 69 6e 61 n the.** origina
62ef0 6c 20 53 65 6c 65 63 74 20 73 74 72 75 63 74 75 l Select structu
62f00 72 65 20 74 68 61 74 20 64 65 73 63 72 69 62 65 re that describe
62f10 73 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61 s the SELECT sta
62f20 74 65 6d 65 6e 74 2e 20 20 54 68 65 73 65 0a 2a tement. These.*
62f30 2a 20 66 69 65 6c 64 73 20 64 6f 20 6e 6f 74 20 * fields do not
62f40 6e 65 65 64 20 74 6f 20 62 65 20 66 72 65 65 64 need to be freed
62f50 20 77 68 65 6e 20 64 65 61 6c 6c 6f 63 61 74 69 when deallocati
62f60 6e 67 20 74 68 65 20 41 67 67 49 6e 66 6f 20 73 ng the AggInfo s
62f70 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 72 tructure..*/.str
62f80 75 63 74 20 41 67 67 49 6e 66 6f 20 7b 0a 20 20 uct AggInfo {.
62f90 75 38 20 64 69 72 65 63 74 4d 6f 64 65 3b 20 20 u8 directMode;
62fa0 20 20 20 20 20 20 20 20 2f 2a 20 44 69 72 65 63 /* Direc
62fb0 74 20 72 65 6e 64 65 72 69 6e 67 20 6d 6f 64 65 t rendering mode
62fc0 20 6d 65 61 6e 73 20 74 61 6b 65 20 64 61 74 61 means take data
62fd0 20 64 69 72 65 63 74 6c 79 0a 20 20 20 20 20 20 directly.
62fe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
62ff0 20 20 20 20 2a 2a 20 66 72 6f 6d 20 73 6f 75 72 ** from sour
63000 63 65 20 74 61 62 6c 65 73 20 72 61 74 68 65 72 ce tables rather
63010 20 74 68 61 6e 20 66 72 6f 6d 20 61 63 63 75 6d than from accum
63020 75 6c 61 74 6f 72 73 20 2a 2f 0a 20 20 75 38 20 ulators */. u8
63030 75 73 65 53 6f 72 74 69 6e 67 49 64 78 3b 20 20 useSortingIdx;
63040 20 20 20 20 20 2f 2a 20 49 6e 20 64 69 72 65 63 /* In direc
63050 74 20 6d 6f 64 65 2c 20 72 65 66 65 72 65 6e 63 t mode, referenc
63060 65 20 74 68 65 20 73 6f 72 74 69 6e 67 20 69 6e e the sorting in
63070 64 65 78 20 72 61 74 68 65 72 0a 20 20 20 20 20 dex rather.
63080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63090 20 20 20 20 20 2a 2a 20 74 68 61 6e 20 74 68 65 ** than the
630a0 20 73 6f 75 72 63 65 20 74 61 62 6c 65 20 2a 2f source table */
630b0 0a 20 20 69 6e 74 20 73 6f 72 74 69 6e 67 49 64 . int sortingId
630c0 78 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 x; /* Cu
630d0 72 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20 74 rsor number of t
630e0 68 65 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 he sorting index
630f0 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a */. ExprList *
63100 70 47 72 6f 75 70 42 79 3b 20 20 20 20 20 2f 2a pGroupBy; /*
63110 20 54 68 65 20 67 72 6f 75 70 20 62 79 20 63 6c The group by cl
63120 61 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 ause */. int nS
63130 6f 72 74 69 6e 67 43 6f 6c 75 6d 6e 3b 20 20 20 ortingColumn;
63140 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 /* Number of c
63150 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 73 6f olumns in the so
63160 72 74 69 6e 67 20 69 6e 64 65 78 20 2a 2f 0a 20 rting index */.
63170 20 73 74 72 75 63 74 20 41 67 67 49 6e 66 6f 5f struct AggInfo_
63180 63 6f 6c 20 7b 20 20 20 20 2f 2a 20 46 6f 72 20 col { /* For
63190 65 61 63 68 20 63 6f 6c 75 6d 6e 20 75 73 65 64 each column used
631a0 20 69 6e 20 73 6f 75 72 63 65 20 74 61 62 6c 65 in source table
631b0 73 20 2a 2f 0a 20 20 20 20 54 61 62 6c 65 20 2a s */. Table *
631c0 70 54 61 62 3b 20 20 20 20 20 20 20 20 20 20 20 pTab;
631d0 20 20 2f 2a 20 53 6f 75 72 63 65 20 74 61 62 6c /* Source tabl
631e0 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 54 61 e */. int iTa
631f0 62 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 ble;
63200 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d 62 /* Cursor numb
63210 65 72 20 6f 66 20 74 68 65 20 73 6f 75 72 63 65 er of the source
63220 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 69 6e table */. in
63230 74 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 t iColumn;
63240 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e /* Column
63250 20 6e 75 6d 62 65 72 20 77 69 74 68 69 6e 20 74 number within t
63260 68 65 20 73 6f 75 72 63 65 20 74 61 62 6c 65 20 he source table
63270 2a 2f 0a 20 20 20 20 69 6e 74 20 69 53 6f 72 74 */. int iSort
63280 65 72 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 erColumn;
63290 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 /* Column number
632a0 20 69 6e 20 74 68 65 20 73 6f 72 74 69 6e 67 20 in the sorting
632b0 69 6e 64 65 78 20 2a 2f 0a 20 20 20 20 69 6e 74 index */. int
632c0 20 69 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 iMem;
632d0 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 /* Memory
632e0 6c 6f 63 61 74 69 6f 6e 20 74 68 61 74 20 61 63 location that ac
632f0 74 73 20 61 73 20 61 63 63 75 6d 75 6c 61 74 6f ts as accumulato
63300 72 20 2a 2f 0a 20 20 20 20 45 78 70 72 20 2a 70 r */. Expr *p
63310 45 78 70 72 3b 20 20 20 20 20 20 20 20 20 20 20 Expr;
63320 20 20 2f 2a 20 54 68 65 20 6f 72 69 67 69 6e 61 /* The origina
63330 6c 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a l expression */.
63340 20 20 7d 20 2a 61 43 6f 6c 3b 0a 20 20 69 6e 74 } *aCol;. int
63350 20 6e 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 nColumn;
63360 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
63370 66 20 75 73 65 64 20 65 6e 74 72 69 65 73 20 69 f used entries i
63380 6e 20 61 43 6f 6c 5b 5d 20 2a 2f 0a 20 20 69 6e n aCol[] */. in
63390 74 20 6e 43 6f 6c 75 6d 6e 41 6c 6c 6f 63 3b 20 t nColumnAlloc;
633a0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
633b0 6f 66 20 73 6c 6f 74 73 20 61 6c 6c 6f 63 61 74 of slots allocat
633c0 65 64 20 66 6f 72 20 61 43 6f 6c 5b 5d 20 2a 2f ed for aCol[] */
633d0 0a 20 20 69 6e 74 20 6e 41 63 63 75 6d 75 6c 61 . int nAccumula
633e0 74 6f 72 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 tor; /* Nu
633f0 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 mber of columns
63400 74 68 61 74 20 73 68 6f 77 20 74 68 72 6f 75 67 that show throug
63410 68 20 74 6f 20 74 68 65 20 6f 75 74 70 75 74 2e h to the output.
63420 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
63430 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 41 64 ** Ad
63440 64 69 74 69 6f 6e 61 6c 20 63 6f 6c 75 6d 6e 73 ditional columns
63450 20 61 72 65 20 75 73 65 64 20 6f 6e 6c 79 20 61 are used only a
63460 73 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f 0a s parameters to.
63470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63480 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61 67 67 ** agg
63490 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 regate functions
634a0 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 41 67 67 */. struct Agg
634b0 49 6e 66 6f 5f 66 75 6e 63 20 7b 20 20 20 2f 2a Info_func { /*
634c0 20 46 6f 72 20 65 61 63 68 20 61 67 67 72 65 67 For each aggreg
634d0 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a ate function */.
634e0 20 20 20 20 45 78 70 72 20 2a 70 45 78 70 72 3b Expr *pExpr;
634f0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
63500 45 78 70 72 65 73 73 69 6f 6e 20 65 6e 63 6f 64 Expression encod
63510 69 6e 67 20 74 68 65 20 66 75 6e 63 74 69 6f 6e ing the function
63520 20 2a 2f 0a 20 20 20 20 46 75 6e 63 44 65 66 20 */. FuncDef
63530 2a 70 46 75 6e 63 3b 20 20 20 20 20 20 20 20 20 *pFunc;
63540 20 2f 2a 20 54 68 65 20 61 67 67 72 65 67 61 74 /* The aggregat
63550 65 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70 6c 65 e function imple
63560 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 20 20 mentation */.
63570 20 69 6e 74 20 69 4d 65 6d 3b 20 20 20 20 20 20 int iMem;
63580 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d /* Mem
63590 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 20 74 68 61 ory location tha
635a0 74 20 61 63 74 73 20 61 73 20 61 63 63 75 6d 75 t acts as accumu
635b0 6c 61 74 6f 72 20 2a 2f 0a 20 20 20 20 69 6e 74 lator */. int
635c0 20 69 44 69 73 74 69 6e 63 74 3b 20 20 20 20 20 iDistinct;
635d0 20 20 20 20 20 20 2f 2a 20 45 70 68 65 6d 65 72 /* Ephemer
635e0 61 6c 20 74 61 62 6c 65 20 75 73 65 64 20 74 6f al table used to
635f0 20 65 6e 66 6f 72 63 65 20 44 49 53 54 49 4e 43 enforce DISTINC
63600 54 20 2a 2f 0a 20 20 7d 20 2a 61 46 75 6e 63 3b T */. } *aFunc;
63610 0a 20 20 69 6e 74 20 6e 46 75 6e 63 3b 20 20 20 . int nFunc;
63620 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
63630 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 mber of entries
63640 69 6e 20 61 46 75 6e 63 5b 5d 20 2a 2f 0a 20 20 in aFunc[] */.
63650 69 6e 74 20 6e 46 75 6e 63 41 6c 6c 6f 63 3b 20 int nFuncAlloc;
63660 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
63670 72 20 6f 66 20 73 6c 6f 74 73 20 61 6c 6c 6f 63 r of slots alloc
63680 61 74 65 64 20 66 6f 72 20 61 46 75 6e 63 5b 5d ated for aFunc[]
63690 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61 */.};../*.** Ea
636a0 63 68 20 6e 6f 64 65 20 6f 66 20 61 6e 20 65 78 ch node of an ex
636b0 70 72 65 73 73 69 6f 6e 20 69 6e 20 74 68 65 20 pression in the
636c0 70 61 72 73 65 20 74 72 65 65 20 69 73 20 61 6e parse tree is an
636d0 20 69 6e 73 74 61 6e 63 65 0a 2a 2a 20 6f 66 20 instance.** of
636e0 74 68 69 73 20 73 74 72 75 63 74 75 72 65 2e 0a this structure..
636f0 2a 2a 0a 2a 2a 20 45 78 70 72 2e 6f 70 20 69 73 **.** Expr.op is
63700 20 74 68 65 20 6f 70 63 6f 64 65 2e 20 54 68 65 the opcode. The
63710 20 69 6e 74 65 67 65 72 20 70 61 72 73 65 72 20 integer parser
63720 74 6f 6b 65 6e 20 63 6f 64 65 73 20 61 72 65 20 token codes are
63730 72 65 75 73 65 64 0a 2a 2a 20 61 73 20 6f 70 63 reused.** as opc
63740 6f 64 65 73 20 68 65 72 65 2e 20 46 6f 72 20 65 odes here. For e
63750 78 61 6d 70 6c 65 2c 20 74 68 65 20 70 61 72 73 xample, the pars
63760 65 72 20 64 65 66 69 6e 65 73 20 54 4b 5f 47 45 er defines TK_GE
63770 20 74 6f 20 62 65 20 61 6e 20 69 6e 74 65 67 65 to be an intege
63780 72 0a 2a 2a 20 63 6f 64 65 20 72 65 70 72 65 73 r.** code repres
63790 65 6e 74 69 6e 67 20 74 68 65 20 22 3e 3d 22 20 enting the ">="
637a0 6f 70 65 72 61 74 6f 72 2e 20 54 68 69 73 20 73 operator. This s
637b0 61 6d 65 20 69 6e 74 65 67 65 72 20 63 6f 64 65 ame integer code
637c0 20 69 73 20 72 65 75 73 65 64 0a 2a 2a 20 74 6f is reused.** to
637d0 20 72 65 70 72 65 73 65 6e 74 20 74 68 65 20 67 represent the g
637e0 72 65 61 74 65 72 2d 74 68 61 6e 2d 6f 72 2d 65 reater-than-or-e
637f0 71 75 61 6c 2d 74 6f 20 6f 70 65 72 61 74 6f 72 qual-to operator
63800 20 69 6e 20 74 68 65 20 65 78 70 72 65 73 73 69 in the expressi
63810 6f 6e 0a 2a 2a 20 74 72 65 65 2e 0a 2a 2a 0a 2a on.** tree..**.*
63820 2a 20 49 66 20 74 68 65 20 65 78 70 72 65 73 73 * If the express
63830 69 6f 6e 20 69 73 20 61 6e 20 53 51 4c 20 6c 69 ion is an SQL li
63840 74 65 72 61 6c 20 28 54 4b 5f 49 4e 54 45 47 45 teral (TK_INTEGE
63850 52 2c 20 54 4b 5f 46 4c 4f 41 54 2c 20 54 4b 5f R, TK_FLOAT, TK_
63860 42 4c 4f 42 2c 20 0a 2a 2a 20 6f 72 20 54 4b 5f BLOB, .** or TK_
63870 53 54 52 49 4e 47 29 2c 20 74 68 65 6e 20 45 78 STRING), then Ex
63880 70 72 2e 74 6f 6b 65 6e 20 63 6f 6e 74 61 69 6e pr.token contain
63890 73 20 74 68 65 20 74 65 78 74 20 6f 66 20 74 68 s the text of th
638a0 65 20 53 51 4c 20 6c 69 74 65 72 61 6c 2e 20 49 e SQL literal. I
638b0 66 0a 2a 2a 20 74 68 65 20 65 78 70 72 65 73 73 f.** the express
638c0 69 6f 6e 20 69 73 20 61 20 76 61 72 69 61 62 6c ion is a variabl
638d0 65 20 28 54 4b 5f 56 41 52 49 41 42 4c 45 29 2c e (TK_VARIABLE),
638e0 20 74 68 65 6e 20 45 78 70 72 2e 74 6f 6b 65 6e then Expr.token
638f0 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 0a 2a contains the .*
63900 2a 20 76 61 72 69 61 62 6c 65 20 6e 61 6d 65 2e * variable name.
63910 20 46 69 6e 61 6c 6c 79 2c 20 69 66 20 74 68 65 Finally, if the
63920 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 expression is a
63930 6e 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 28 n SQL function (
63940 54 4b 5f 46 55 4e 43 54 49 4f 4e 29 2c 0a 2a 2a TK_FUNCTION),.**
63950 20 74 68 65 6e 20 45 78 70 72 2e 74 6f 6b 65 6e then Expr.token
63960 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 6e 61 contains the na
63970 6d 65 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 me of the functi
63980 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 45 78 70 72 2e 70 on..**.** Expr.p
63990 52 69 67 68 74 20 61 6e 64 20 45 78 70 72 2e 70 Right and Expr.p
639a0 4c 65 66 74 20 61 72 65 20 74 68 65 20 6c 65 66 Left are the lef
639b0 74 20 61 6e 64 20 72 69 67 68 74 20 73 75 62 65 t and right sube
639c0 78 70 72 65 73 73 69 6f 6e 73 20 6f 66 20 61 0a xpressions of a.
639d0 2a 2a 20 62 69 6e 61 72 79 20 6f 70 65 72 61 74 ** binary operat
639e0 6f 72 2e 20 45 69 74 68 65 72 20 6f 72 20 62 6f or. Either or bo
639f0 74 68 20 6d 61 79 20 62 65 20 4e 55 4c 4c 2e 0a th may be NULL..
63a00 2a 2a 0a 2a 2a 20 45 78 70 72 2e 78 2e 70 4c 69 **.** Expr.x.pLi
63a10 73 74 20 69 73 20 61 20 6c 69 73 74 20 6f 66 20 st is a list of
63a20 61 72 67 75 6d 65 6e 74 73 20 69 66 20 74 68 65 arguments if the
63a30 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 expression is a
63a40 6e 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 2c 0a n SQL function,.
63a50 2a 2a 20 61 20 43 41 53 45 20 65 78 70 72 65 73 ** a CASE expres
63a60 73 69 6f 6e 20 6f 72 20 61 6e 20 49 4e 20 65 78 sion or an IN ex
63a70 70 72 65 73 73 69 6f 6e 20 6f 66 20 74 68 65 20 pression of the
63a80 66 6f 72 6d 20 22 3c 6c 68 73 3e 20 49 4e 20 28 form "<lhs> IN (
63a90 3c 79 3e 2c 20 3c 7a 3e 2e 2e 2e 29 22 2e 0a 2a <y>, <z>...)"..*
63aa0 2a 20 45 78 70 72 2e 78 2e 70 53 65 6c 65 63 74 * Expr.x.pSelect
63ab0 20 69 73 20 75 73 65 64 20 69 66 20 74 68 65 20 is used if the
63ac0 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 20 expression is a
63ad0 73 75 62 2d 73 65 6c 65 63 74 20 6f 72 20 61 6e sub-select or an
63ae0 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 66 0a 2a expression of.*
63af0 2a 20 74 68 65 20 66 6f 72 6d 20 22 3c 6c 68 73 * the form "<lhs
63b00 3e 20 49 4e 20 28 53 45 4c 45 43 54 20 2e 2e 2e > IN (SELECT ...
63b10 29 22 2e 20 49 66 20 74 68 65 20 45 50 5f 78 49 )". If the EP_xI
63b20 73 53 65 6c 65 63 74 20 62 69 74 20 69 73 20 73 sSelect bit is s
63b30 65 74 20 69 6e 20 74 68 65 0a 2a 2a 20 45 78 70 et in the.** Exp
63b40 72 2e 66 6c 61 67 73 20 6d 61 73 6b 2c 20 74 68 r.flags mask, th
63b50 65 6e 20 45 78 70 72 2e 78 2e 70 53 65 6c 65 63 en Expr.x.pSelec
63b60 74 20 69 73 20 76 61 6c 69 64 2e 20 4f 74 68 65 t is valid. Othe
63b70 72 77 69 73 65 2c 20 45 78 70 72 2e 78 2e 70 4c rwise, Expr.x.pL
63b80 69 73 74 20 69 73 20 0a 2a 2a 20 76 61 6c 69 64 ist is .** valid
63b90 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 78 70 72 65 ..**.** An expre
63ba0 73 73 69 6f 6e 20 6f 66 20 74 68 65 20 66 6f 72 ssion of the for
63bb0 6d 20 49 44 20 6f 72 20 49 44 2e 49 44 20 72 65 m ID or ID.ID re
63bc0 66 65 72 73 20 74 6f 20 61 20 63 6f 6c 75 6d 6e fers to a column
63bd0 20 69 6e 20 61 20 74 61 62 6c 65 2e 0a 2a 2a 20 in a table..**
63be0 46 6f 72 20 73 75 63 68 20 65 78 70 72 65 73 73 For such express
63bf0 69 6f 6e 73 2c 20 45 78 70 72 2e 6f 70 20 69 73 ions, Expr.op is
63c00 20 73 65 74 20 74 6f 20 54 4b 5f 43 4f 4c 55 4d set to TK_COLUM
63c10 4e 20 61 6e 64 20 45 78 70 72 2e 69 54 61 62 6c N and Expr.iTabl
63c20 65 20 69 73 0a 2a 2a 20 74 68 65 20 69 6e 74 65 e is.** the inte
63c30 67 65 72 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 ger cursor numbe
63c40 72 20 6f 66 20 61 20 56 44 42 45 20 63 75 72 73 r of a VDBE curs
63c50 6f 72 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 or pointing to t
63c60 68 61 74 20 74 61 62 6c 65 20 61 6e 64 0a 2a 2a hat table and.**
63c70 20 45 78 70 72 2e 69 43 6f 6c 75 6d 6e 20 69 73 Expr.iColumn is
63c80 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 the column numb
63c90 65 72 20 66 6f 72 20 74 68 65 20 73 70 65 63 69 er for the speci
63ca0 66 69 63 20 63 6f 6c 75 6d 6e 2e 20 20 49 66 20 fic column. If
63cb0 74 68 65 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f the.** expressio
63cc0 6e 20 69 73 20 75 73 65 64 20 61 73 20 61 20 72 n is used as a r
63cd0 65 73 75 6c 74 20 69 6e 20 61 6e 20 61 67 67 72 esult in an aggr
63ce0 65 67 61 74 65 20 53 45 4c 45 43 54 2c 20 74 68 egate SELECT, th
63cf0 65 6e 20 74 68 65 0a 2a 2a 20 76 61 6c 75 65 20 en the.** value
63d00 69 73 20 61 6c 73 6f 20 73 74 6f 72 65 64 20 69 is also stored i
63d10 6e 20 74 68 65 20 45 78 70 72 2e 69 41 67 67 20 n the Expr.iAgg
63d20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 61 67 column in the ag
63d30 67 72 65 67 61 74 65 20 73 6f 20 74 68 61 74 0a gregate so that.
63d40 2a 2a 20 69 74 20 63 61 6e 20 62 65 20 61 63 63 ** it can be acc
63d50 65 73 73 65 64 20 61 66 74 65 72 20 61 6c 6c 20 essed after all
63d60 61 67 67 72 65 67 61 74 65 73 20 61 72 65 20 63 aggregates are c
63d70 6f 6d 70 75 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 omputed..**.** I
63d80 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e f the expression
63d90 20 69 73 20 61 6e 20 75 6e 62 6f 75 6e 64 20 76 is an unbound v
63da0 61 72 69 61 62 6c 65 20 6d 61 72 6b 65 72 20 28 ariable marker (
63db0 61 20 71 75 65 73 74 69 6f 6e 20 6d 61 72 6b 20 a question mark
63dc0 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 20 27 3f .** character '?
63dd0 27 20 69 6e 20 74 68 65 20 6f 72 69 67 69 6e 61 ' in the origina
63de0 6c 20 53 51 4c 29 20 74 68 65 6e 20 74 68 65 20 l SQL) then the
63df0 45 78 70 72 2e 69 54 61 62 6c 65 20 68 6f 6c 64 Expr.iTable hold
63e00 73 20 74 68 65 20 69 6e 64 65 78 20 0a 2a 2a 20 s the index .**
63e10 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 61 74 20 number for that
63e20 76 61 72 69 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 variable..**.**
63e30 49 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f If the expressio
63e40 6e 20 69 73 20 61 20 73 75 62 71 75 65 72 79 20 n is a subquery
63e50 74 68 65 6e 20 45 78 70 72 2e 69 43 6f 6c 75 6d then Expr.iColum
63e60 6e 20 68 6f 6c 64 73 20 61 6e 20 69 6e 74 65 67 n holds an integ
63e70 65 72 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 6e er.** register n
63e80 75 6d 62 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 umber containing
63e90 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74 the result of t
63ea0 68 65 20 73 75 62 71 75 65 72 79 2e 20 20 49 66 he subquery. If
63eb0 20 74 68 65 0a 2a 2a 20 73 75 62 71 75 65 72 79 the.** subquery
63ec0 20 67 69 76 65 73 20 61 20 63 6f 6e 73 74 61 6e gives a constan
63ed0 74 20 72 65 73 75 6c 74 2c 20 74 68 65 6e 20 69 t result, then i
63ee0 54 61 62 6c 65 20 69 73 20 2d 31 2e 20 20 49 66 Table is -1. If
63ef0 20 74 68 65 20 73 75 62 71 75 65 72 79 0a 2a 2a the subquery.**
63f00 20 67 69 76 65 73 20 61 20 64 69 66 66 65 72 65 gives a differe
63f10 6e 74 20 61 6e 73 77 65 72 20 61 74 20 64 69 66 nt answer at dif
63f20 66 65 72 65 6e 74 20 74 69 6d 65 73 20 64 75 72 ferent times dur
63f30 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 20 70 72 ing statement pr
63f40 6f 63 65 73 73 69 6e 67 0a 2a 2a 20 74 68 65 6e ocessing.** then
63f50 20 69 54 61 62 6c 65 20 69 73 20 74 68 65 20 61 iTable is the a
63f60 64 64 72 65 73 73 20 6f 66 20 61 20 73 75 62 72 ddress of a subr
63f70 6f 75 74 69 6e 65 20 74 68 61 74 20 63 6f 6d 70 outine that comp
63f80 75 74 65 73 20 74 68 65 20 73 75 62 71 75 65 72 utes the subquer
63f90 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 y..**.** If the
63fa0 45 78 70 72 20 69 73 20 6f 66 20 74 79 70 65 20 Expr is of type
63fb0 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 61 6e 64 20 74 OP_Column, and t
63fc0 68 65 20 74 61 62 6c 65 20 69 74 20 69 73 20 73 he table it is s
63fd0 65 6c 65 63 74 69 6e 67 20 66 72 6f 6d 0a 2a 2a electing from.**
63fe0 20 69 73 20 61 20 64 69 73 6b 20 74 61 62 6c 65 is a disk table
63ff0 20 6f 72 20 74 68 65 20 22 6f 6c 64 2e 2a 22 20 or the "old.*"
64000 70 73 65 75 64 6f 2d 74 61 62 6c 65 2c 20 74 68 pseudo-table, th
64010 65 6e 20 70 54 61 62 20 70 6f 69 6e 74 73 20 74 en pTab points t
64020 6f 20 74 68 65 0a 2a 2a 20 63 6f 72 72 65 73 70 o the.** corresp
64030 6f 6e 64 69 6e 67 20 74 61 62 6c 65 20 64 65 66 onding table def
64040 69 6e 69 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 41 inition..**.** A
64050 4c 4c 4f 43 41 54 49 4f 4e 20 4e 4f 54 45 53 3a LLOCATION NOTES:
64060 0a 2a 2a 0a 2a 2a 20 45 78 70 72 20 6f 62 6a 65 .**.** Expr obje
64070 63 74 73 20 63 61 6e 20 75 73 65 20 61 20 6c 6f cts can use a lo
64080 74 20 6f 66 20 6d 65 6d 6f 72 79 20 73 70 61 63 t of memory spac
64090 65 20 69 6e 20 64 61 74 61 62 61 73 65 20 73 63 e in database sc
640a0 68 65 6d 61 2e 20 20 54 6f 0a 2a 2a 20 68 65 6c hema. To.** hel
640b0 70 20 72 65 64 75 63 65 20 6d 65 6d 6f 72 79 20 p reduce memory
640c0 72 65 71 75 69 72 65 6d 65 6e 74 73 2c 20 73 6f requirements, so
640d0 6d 65 74 69 6d 65 73 20 61 6e 20 45 78 70 72 20 metimes an Expr
640e0 6f 62 6a 65 63 74 20 77 69 6c 6c 20 62 65 0a 2a object will be.*
640f0 2a 20 74 72 75 6e 63 61 74 65 64 2e 20 20 41 6e * truncated. An
64100 64 20 74 6f 20 72 65 64 75 63 65 20 74 68 65 20 d to reduce the
64110 6e 75 6d 62 65 72 20 6f 66 20 6d 65 6d 6f 72 79 number of memory
64120 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2c 20 73 6f allocations, so
64130 6d 65 74 69 6d 65 73 0a 2a 2a 20 74 77 6f 20 6f metimes.** two o
64140 72 20 6d 6f 72 65 20 45 78 70 72 20 6f 62 6a 65 r more Expr obje
64150 63 74 73 20 77 69 6c 6c 20 62 65 20 73 74 6f 72 cts will be stor
64160 65 64 20 69 6e 20 61 20 73 69 6e 67 6c 65 20 6d ed in a single m
64170 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
64180 2c 0a 2a 2a 20 74 6f 67 65 74 68 65 72 20 77 69 ,.** together wi
64190 74 68 20 45 78 70 72 2e 7a 54 6f 6b 65 6e 20 73 th Expr.zToken s
641a0 74 72 69 6e 67 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 trings..**.** If
641b0 20 74 68 65 20 45 50 5f 52 65 64 75 63 65 64 20 the EP_Reduced
641c0 61 6e 64 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 and EP_TokenOnly
641d0 20 66 6c 61 67 73 20 61 72 65 20 73 65 74 20 77 flags are set w
641e0 68 65 6e 0a 2a 2a 20 61 6e 20 45 78 70 72 20 6f hen.** an Expr o
641f0 62 6a 65 63 74 20 69 73 20 74 72 75 6e 63 61 74 bject is truncat
64200 65 64 2e 20 20 57 68 65 6e 20 45 50 5f 52 65 64 ed. When EP_Red
64210 75 63 65 64 20 69 73 20 73 65 74 2c 20 74 68 65 uced is set, the
64220 6e 20 61 6c 6c 0a 2a 2a 20 74 68 65 20 63 68 69 n all.** the chi
64230 6c 64 20 45 78 70 72 20 6f 62 6a 65 63 74 73 20 ld Expr objects
64240 69 6e 20 74 68 65 20 45 78 70 72 2e 70 4c 65 66 in the Expr.pLef
64250 74 20 61 6e 64 20 45 78 70 72 2e 70 52 69 67 68 t and Expr.pRigh
64260 74 20 73 75 62 74 72 65 65 73 0a 2a 2a 20 61 72 t subtrees.** ar
64270 65 20 63 6f 6e 74 61 69 6e 65 64 20 77 69 74 68 e contained with
64280 69 6e 20 74 68 65 20 73 61 6d 65 20 6d 65 6d 6f in the same memo
64290 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20 ry allocation.
642a0 4e 6f 74 65 2c 20 68 6f 77 65 76 65 72 2c 20 74 Note, however, t
642b0 68 61 74 0a 2a 2a 20 74 68 65 20 73 75 62 74 72 hat.** the subtr
642c0 65 65 73 20 69 6e 20 45 78 70 72 2e 78 2e 70 4c ees in Expr.x.pL
642d0 69 73 74 20 6f 72 20 45 78 70 72 2e 78 2e 70 53 ist or Expr.x.pS
642e0 65 6c 65 63 74 20 61 72 65 20 61 6c 77 61 79 73 elect are always
642f0 20 73 65 70 61 72 61 74 65 6c 79 0a 2a 2a 20 61 separately.** a
64300 6c 6c 6f 63 61 74 65 64 2c 20 72 65 67 61 72 64 llocated, regard
64310 6c 65 73 73 20 6f 66 20 77 68 65 74 68 65 72 20 less of whether
64320 6f 72 20 6e 6f 74 20 45 50 5f 52 65 64 75 63 65 or not EP_Reduce
64330 64 20 69 73 20 73 65 74 2e 0a 2a 2f 0a 73 74 72 d is set..*/.str
64340 75 63 74 20 45 78 70 72 20 7b 0a 20 20 75 38 20 uct Expr {. u8
64350 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 op;
64360 20 20 20 20 2f 2a 20 4f 70 65 72 61 74 69 6f 6e /* Operation
64370 20 70 65 72 66 6f 72 6d 65 64 20 62 79 20 74 68 performed by th
64380 69 73 20 6e 6f 64 65 20 2a 2f 0a 20 20 63 68 61 is node */. cha
64390 72 20 61 66 66 69 6e 69 74 79 3b 20 20 20 20 20 r affinity;
643a0 20 20 20 20 2f 2a 20 54 68 65 20 61 66 66 69 6e /* The affin
643b0 69 74 79 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d ity of the colum
643c0 6e 20 6f 72 20 30 20 69 66 20 6e 6f 74 20 61 20 n or 0 if not a
643d0 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 75 31 36 20 column */. u16
643e0 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 flags;
643f0 20 20 20 2f 2a 20 56 61 72 69 6f 75 73 20 66 6c /* Various fl
64400 61 67 73 2e 20 20 45 50 5f 2a 20 53 65 65 20 62 ags. EP_* See b
64410 65 6c 6f 77 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20 elow */. union
64420 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 54 6f 6b {. char *zTok
64430 65 6e 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 en; /*
64440 54 6f 6b 65 6e 20 76 61 6c 75 65 2e 20 5a 65 72 Token value. Zer
64450 6f 20 74 65 72 6d 69 6e 61 74 65 64 20 61 6e 64 o terminated and
64460 20 64 65 71 75 6f 74 65 64 20 2a 2f 0a 20 20 20 dequoted */.
64470 20 69 6e 74 20 69 56 61 6c 75 65 3b 20 20 20 20 int iValue;
64480 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 74 65 67 /* Integ
64490 65 72 20 76 61 6c 75 65 20 69 66 20 45 50 5f 49 er value if EP_I
644a0 6e 74 56 61 6c 75 65 20 2a 2f 0a 20 20 7d 20 75 ntValue */. } u
644b0 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 45 ;.. /* If the E
644c0 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 20 66 6c 61 67 P_TokenOnly flag
644d0 20 69 73 20 73 65 74 20 69 6e 20 74 68 65 20 45 is set in the E
644e0 78 70 72 2e 66 6c 61 67 73 20 6d 61 73 6b 2c 20 xpr.flags mask,
644f0 74 68 65 6e 20 6e 6f 0a 20 20 2a 2a 20 73 70 61 then no. ** spa
64500 63 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 ce is allocated
64510 66 6f 72 20 74 68 65 20 66 69 65 6c 64 73 20 62 for the fields b
64520 65 6c 6f 77 20 74 68 69 73 20 70 6f 69 6e 74 2e elow this point.
64530 20 41 6e 20 61 74 74 65 6d 70 74 20 74 6f 0a 20 An attempt to.
64540 20 2a 2a 20 61 63 63 65 73 73 20 74 68 65 6d 20 ** access them
64550 77 69 6c 6c 20 72 65 73 75 6c 74 20 69 6e 20 61 will result in a
64560 20 73 65 67 66 61 75 6c 74 20 6f 72 20 6d 61 6c segfault or mal
64570 66 75 6e 63 74 69 6f 6e 2e 20 0a 20 20 2a 2a 2a function. . ***
64580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
64590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
645a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
645b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
645c0 2a 2a 2f 0a 0a 20 20 45 78 70 72 20 2a 70 4c 65 **/.. Expr *pLe
645d0 66 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a ft; /*
645e0 20 4c 65 66 74 20 73 75 62 6e 6f 64 65 20 2a 2f Left subnode */
645f0 0a 20 20 45 78 70 72 20 2a 70 52 69 67 68 74 3b . Expr *pRight;
64600 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 69 67 /* Rig
64610 68 74 20 73 75 62 6e 6f 64 65 20 2a 2f 0a 20 20 ht subnode */.
64620 75 6e 69 6f 6e 20 7b 0a 20 20 20 20 45 78 70 72 union {. Expr
64630 4c 69 73 74 20 2a 70 4c 69 73 74 3b 20 20 20 20 List *pList;
64640 20 2f 2a 20 46 75 6e 63 74 69 6f 6e 20 61 72 67 /* Function arg
64650 75 6d 65 6e 74 73 20 6f 72 20 69 6e 20 22 3c 65 uments or in "<e
64660 78 70 72 3e 20 49 4e 20 28 3c 65 78 70 72 2d 6c xpr> IN (<expr-l
64670 69 73 74 29 22 20 2a 2f 0a 20 20 20 20 53 65 6c ist)" */. Sel
64680 65 63 74 20 2a 70 53 65 6c 65 63 74 3b 20 20 20 ect *pSelect;
64690 20 20 2f 2a 20 55 73 65 64 20 66 6f 72 20 73 75 /* Used for su
646a0 62 2d 73 65 6c 65 63 74 73 20 61 6e 64 20 22 3c b-selects and "<
646b0 65 78 70 72 3e 20 49 4e 20 28 3c 73 65 6c 65 63 expr> IN (<selec
646c0 74 3e 29 22 20 2a 2f 0a 20 20 7d 20 78 3b 0a 20 t>)" */. } x;.
646d0 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b CollSeq *pColl;
646e0 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 /* The c
646f0 6f 6c 6c 61 74 69 6f 6e 20 74 79 70 65 20 6f 66 ollation type of
64700 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6f 72 20 30 the column or 0
64710 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 */.. /* If the
64720 20 45 50 5f 52 65 64 75 63 65 64 20 66 6c 61 67 EP_Reduced flag
64730 20 69 73 20 73 65 74 20 69 6e 20 74 68 65 20 45 is set in the E
64740 78 70 72 2e 66 6c 61 67 73 20 6d 61 73 6b 2c 20 xpr.flags mask,
64750 74 68 65 6e 20 6e 6f 0a 20 20 2a 2a 20 73 70 61 then no. ** spa
64760 63 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 ce is allocated
64770 66 6f 72 20 74 68 65 20 66 69 65 6c 64 73 20 62 for the fields b
64780 65 6c 6f 77 20 74 68 69 73 20 70 6f 69 6e 74 2e elow this point.
64790 20 41 6e 20 61 74 74 65 6d 70 74 20 74 6f 0a 20 An attempt to.
647a0 20 2a 2a 20 61 63 63 65 73 73 20 74 68 65 6d 20 ** access them
647b0 77 69 6c 6c 20 72 65 73 75 6c 74 20 69 6e 20 61 will result in a
647c0 20 73 65 67 66 61 75 6c 74 20 6f 72 20 6d 61 6c segfault or mal
647d0 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 2a 2a 2a 2a function.. ****
647e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
647f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
64800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
64810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
64820 2a 2f 0a 0a 20 20 69 6e 74 20 69 54 61 62 6c 65 */.. int iTable
64830 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
64840 54 4b 5f 43 4f 4c 55 4d 4e 3a 20 63 75 72 73 6f TK_COLUMN: curso
64850 72 20 6e 75 6d 62 65 72 20 6f 66 20 74 61 62 6c r number of tabl
64860 65 20 68 6f 6c 64 69 6e 67 20 63 6f 6c 75 6d 6e e holding column
64870 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
64880 20 20 20 20 20 20 20 20 20 20 2a 2a 20 54 4b 5f ** TK_
64890 52 45 47 49 53 54 45 52 3a 20 72 65 67 69 73 74 REGISTER: regist
648a0 65 72 20 6e 75 6d 62 65 72 0a 20 20 20 20 20 20 er number.
648b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
648c0 20 20 20 2a 2a 20 54 4b 5f 54 52 49 47 47 45 52 ** TK_TRIGGER
648d0 3a 20 31 20 2d 3e 20 6e 65 77 2c 20 30 20 2d 3e : 1 -> new, 0 ->
648e0 20 6f 6c 64 20 2a 2f 0a 20 20 69 31 36 20 69 43 old */. i16 iC
648f0 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 20 olumn;
64900 20 2f 2a 20 54 4b 5f 43 4f 4c 55 4d 4e 3a 20 63 /* TK_COLUMN: c
64910 6f 6c 75 6d 6e 20 69 6e 64 65 78 2e 20 20 2d 31 olumn index. -1
64920 20 66 6f 72 20 72 6f 77 69 64 20 2a 2f 0a 20 20 for rowid */.
64930 69 31 36 20 69 41 67 67 3b 20 20 20 20 20 20 20 i16 iAgg;
64940 20 20 20 20 20 20 20 2f 2a 20 57 68 69 63 68 20 /* Which
64950 65 6e 74 72 79 20 69 6e 20 70 41 67 67 49 6e 66 entry in pAggInf
64960 6f 2d 3e 61 43 6f 6c 5b 5d 20 6f 72 20 2d 3e 61 o->aCol[] or ->a
64970 46 75 6e 63 5b 5d 20 2a 2f 0a 20 20 69 31 36 20 Func[] */. i16
64980 69 52 69 67 68 74 4a 6f 69 6e 54 61 62 6c 65 3b iRightJoinTable;
64990 20 20 20 2f 2a 20 49 66 20 45 50 5f 46 72 6f 6d /* If EP_From
649a0 4a 6f 69 6e 2c 20 74 68 65 20 72 69 67 68 74 20 Join, the right
649b0 74 61 62 6c 65 20 6f 66 20 74 68 65 20 6a 6f 69 table of the joi
649c0 6e 20 2a 2f 0a 20 20 75 38 20 66 6c 61 67 73 32 n */. u8 flags2
649d0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
649e0 20 53 65 63 6f 6e 64 20 73 65 74 20 6f 66 20 66 Second set of f
649f0 6c 61 67 73 2e 20 20 45 50 32 5f 2e 2e 2e 20 2a lags. EP2_... *
64a00 2f 0a 20 20 75 38 20 6f 70 32 3b 20 20 20 20 20 /. u8 op2;
64a10 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 /* If
64a20 20 61 20 54 4b 5f 52 45 47 49 53 54 45 52 2c 20 a TK_REGISTER,
64a30 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 76 61 6c the original val
64a40 75 65 20 6f 66 20 45 78 70 72 2e 6f 70 20 2a 2f ue of Expr.op */
64a50 0a 20 20 41 67 67 49 6e 66 6f 20 2a 70 41 67 67 . AggInfo *pAgg
64a60 49 6e 66 6f 3b 20 20 20 20 20 2f 2a 20 55 73 65 Info; /* Use
64a70 64 20 62 79 20 54 4b 5f 41 47 47 5f 43 4f 4c 55 d by TK_AGG_COLU
64a80 4d 4e 20 61 6e 64 20 54 4b 5f 41 47 47 5f 46 55 MN and TK_AGG_FU
64a90 4e 43 54 49 4f 4e 20 2a 2f 0a 20 20 54 61 62 6c NCTION */. Tabl
64aa0 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20 20 e *pTab;
64ab0 20 20 20 2f 2a 20 54 61 62 6c 65 20 66 6f 72 20 /* Table for
64ac0 54 4b 5f 43 4f 4c 55 4d 4e 20 65 78 70 72 65 73 TK_COLUMN expres
64ad0 73 69 6f 6e 73 2e 20 2a 2f 0a 23 69 66 20 53 51 sions. */.#if SQ
64ae0 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45 LITE_MAX_EXPR_DE
64af0 50 54 48 3e 30 0a 20 20 69 6e 74 20 6e 48 65 69 PTH>0. int nHei
64b00 67 68 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f ght; /
64b10 2a 20 48 65 69 67 68 74 20 6f 66 20 74 68 65 20 * Height of the
64b20 74 72 65 65 20 68 65 61 64 65 64 20 62 79 20 74 tree headed by t
64b30 68 69 73 20 6e 6f 64 65 20 2a 2f 0a 23 65 6e 64 his node */.#end
64b40 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 if.};../*.** The
64b50 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 74 following are t
64b60 68 65 20 6d 65 61 6e 69 6e 67 73 20 6f 66 20 62 he meanings of b
64b70 69 74 73 20 69 6e 20 74 68 65 20 45 78 70 72 2e its in the Expr.
64b80 66 6c 61 67 73 20 66 69 65 6c 64 2e 0a 2a 2f 0a flags field..*/.
64b90 23 64 65 66 69 6e 65 20 45 50 5f 46 72 6f 6d 4a #define EP_FromJ
64ba0 6f 69 6e 20 20 20 30 78 30 30 30 31 20 20 2f 2a oin 0x0001 /*
64bb0 20 4f 72 69 67 69 6e 61 74 65 64 20 69 6e 20 4f Originated in O
64bc0 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c 61 75 73 N or USING claus
64bd0 65 20 6f 66 20 61 20 6a 6f 69 6e 20 2a 2f 0a 23 e of a join */.#
64be0 64 65 66 69 6e 65 20 45 50 5f 41 67 67 20 20 20 define EP_Agg
64bf0 20 20 20 20 20 30 78 30 30 30 32 20 20 2f 2a 20 0x0002 /*
64c00 43 6f 6e 74 61 69 6e 73 20 6f 6e 65 20 6f 72 20 Contains one or
64c10 6d 6f 72 65 20 61 67 67 72 65 67 61 74 65 20 66 more aggregate f
64c20 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 23 64 65 66 unctions */.#def
64c30 69 6e 65 20 45 50 5f 52 65 73 6f 6c 76 65 64 20 ine EP_Resolved
64c40 20 20 30 78 30 30 30 34 20 20 2f 2a 20 49 44 73 0x0004 /* IDs
64c50 20 68 61 76 65 20 62 65 65 6e 20 72 65 73 6f 6c have been resol
64c60 76 65 64 20 74 6f 20 43 4f 4c 55 4d 4e 73 20 2a ved to COLUMNs *
64c70 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 45 72 72 /.#define EP_Err
64c80 6f 72 20 20 20 20 20 20 30 78 30 30 30 38 20 20 or 0x0008
64c90 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 63 6f /* Expression co
64ca0 6e 74 61 69 6e 73 20 6f 6e 65 20 6f 72 20 6d 6f ntains one or mo
64cb0 72 65 20 65 72 72 6f 72 73 20 2a 2f 0a 23 64 65 re errors */.#de
64cc0 66 69 6e 65 20 45 50 5f 44 69 73 74 69 6e 63 74 fine EP_Distinct
64cd0 20 20 20 30 78 30 30 31 30 20 20 2f 2a 20 41 67 0x0010 /* Ag
64ce0 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e gregate function
64cf0 20 77 69 74 68 20 44 49 53 54 49 4e 43 54 20 6b with DISTINCT k
64d00 65 79 77 6f 72 64 20 2a 2f 0a 23 64 65 66 69 6e eyword */.#defin
64d10 65 20 45 50 5f 56 61 72 53 65 6c 65 63 74 20 20 e EP_VarSelect
64d20 30 78 30 30 32 30 20 20 2f 2a 20 70 53 65 6c 65 0x0020 /* pSele
64d30 63 74 20 69 73 20 63 6f 72 72 65 6c 61 74 65 64 ct is correlated
64d40 2c 20 6e 6f 74 20 63 6f 6e 73 74 61 6e 74 20 2a , not constant *
64d50 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 44 62 6c /.#define EP_Dbl
64d60 51 75 6f 74 65 64 20 20 30 78 30 30 34 30 20 20 Quoted 0x0040
64d70 2f 2a 20 74 6f 6b 65 6e 2e 7a 20 77 61 73 20 6f /* token.z was o
64d80 72 69 67 69 6e 61 6c 6c 79 20 69 6e 20 22 2e 2e riginally in "..
64d90 2e 22 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 ." */.#define EP
64da0 5f 49 6e 66 69 78 46 75 6e 63 20 20 30 78 30 30 _InfixFunc 0x00
64db0 38 30 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 80 /* True for
64dc0 61 6e 20 69 6e 66 69 78 20 66 75 6e 63 74 69 6f an infix functio
64dd0 6e 3a 20 4c 49 4b 45 2c 20 47 4c 4f 42 2c 20 65 n: LIKE, GLOB, e
64de0 74 63 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 tc */.#define EP
64df0 5f 45 78 70 43 6f 6c 6c 61 74 65 20 30 78 30 31 _ExpCollate 0x01
64e00 30 30 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67 00 /* Collating
64e10 20 73 65 71 75 65 6e 63 65 20 73 70 65 63 69 66 sequence specif
64e20 69 65 64 20 65 78 70 6c 69 63 69 74 6c 79 20 2a ied explicitly *
64e30 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 41 6e 79 /.#define EP_Any
64e40 41 66 66 20 20 20 20 20 30 78 30 32 30 30 20 20 Aff 0x0200
64e50 2f 2a 20 43 61 6e 20 74 61 6b 65 20 61 20 63 61 /* Can take a ca
64e60 63 68 65 64 20 63 6f 6c 75 6d 6e 20 6f 66 20 61 ched column of a
64e70 6e 79 20 61 66 66 69 6e 69 74 79 20 2a 2f 0a 23 ny affinity */.#
64e80 64 65 66 69 6e 65 20 45 50 5f 46 69 78 65 64 44 define EP_FixedD
64e90 65 73 74 20 20 30 78 30 34 30 30 20 20 2f 2a 20 est 0x0400 /*
64ea0 52 65 73 75 6c 74 20 6e 65 65 64 65 64 20 69 6e Result needed in
64eb0 20 61 20 73 70 65 63 69 66 69 63 20 72 65 67 69 a specific regi
64ec0 73 74 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 ster */.#define
64ed0 45 50 5f 49 6e 74 56 61 6c 75 65 20 20 20 30 78 EP_IntValue 0x
64ee0 30 38 30 30 20 20 2f 2a 20 49 6e 74 65 67 65 72 0800 /* Integer
64ef0 20 76 61 6c 75 65 20 63 6f 6e 74 61 69 6e 65 64 value contained
64f00 20 69 6e 20 75 2e 69 56 61 6c 75 65 20 2a 2f 0a in u.iValue */.
64f10 23 64 65 66 69 6e 65 20 45 50 5f 78 49 73 53 65 #define EP_xIsSe
64f20 6c 65 63 74 20 20 30 78 31 30 30 30 20 20 2f 2a lect 0x1000 /*
64f30 20 78 2e 70 53 65 6c 65 63 74 20 69 73 20 76 61 x.pSelect is va
64f40 6c 69 64 20 28 6f 74 68 65 72 77 69 73 65 20 78 lid (otherwise x
64f50 2e 70 4c 69 73 74 20 69 73 29 20 2a 2f 0a 0a 23 .pList is) */..#
64f60 64 65 66 69 6e 65 20 45 50 5f 52 65 64 75 63 65 define EP_Reduce
64f70 64 20 20 20 20 30 78 32 30 30 30 20 20 2f 2a 20 d 0x2000 /*
64f80 45 78 70 72 20 73 74 72 75 63 74 20 69 73 20 45 Expr struct is E
64f90 58 50 52 5f 52 45 44 55 43 45 44 53 49 5a 45 20 XPR_REDUCEDSIZE
64fa0 62 79 74 65 73 20 6f 6e 6c 79 20 2a 2f 0a 23 64 bytes only */.#d
64fb0 65 66 69 6e 65 20 45 50 5f 54 6f 6b 65 6e 4f 6e efine EP_TokenOn
64fc0 6c 79 20 20 30 78 34 30 30 30 20 20 2f 2a 20 45 ly 0x4000 /* E
64fd0 78 70 72 20 73 74 72 75 63 74 20 69 73 20 45 58 xpr struct is EX
64fe0 50 52 5f 54 4f 4b 45 4e 4f 4e 4c 59 53 49 5a 45 PR_TOKENONLYSIZE
64ff0 20 62 79 74 65 73 20 6f 6e 6c 79 20 2a 2f 0a 23 bytes only */.#
65000 64 65 66 69 6e 65 20 45 50 5f 53 74 61 74 69 63 define EP_Static
65010 20 20 20 20 20 30 78 38 30 30 30 20 20 2f 2a 20 0x8000 /*
65020 48 65 6c 64 20 69 6e 20 6d 65 6d 6f 72 79 20 6e Held in memory n
65030 6f 74 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d ot obtained from
65040 20 6d 61 6c 6c 6f 63 28 29 20 2a 2f 0a 0a 2f 2a malloc() */../*
65050 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e .** The followin
65060 67 20 61 72 65 20 74 68 65 20 6d 65 61 6e 69 6e g are the meanin
65070 67 73 20 6f 66 20 62 69 74 73 20 69 6e 20 74 68 gs of bits in th
65080 65 20 45 78 70 72 2e 66 6c 61 67 73 32 20 66 69 e Expr.flags2 fi
65090 65 6c 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 eld..*/.#define
650a0 45 50 32 5f 4d 61 6c 6c 6f 63 65 64 54 6f 6b 65 EP2_MallocedToke
650b0 6e 20 20 30 78 30 30 30 31 20 20 2f 2a 20 4e 65 n 0x0001 /* Ne
650c0 65 64 20 74 6f 20 73 71 6c 69 74 65 33 44 62 46 ed to sqlite3DbF
650d0 72 65 65 28 29 20 45 78 70 72 2e 7a 54 6f 6b 65 ree() Expr.zToke
650e0 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 32 n */.#define EP2
650f0 5f 49 72 72 65 64 75 63 69 62 6c 65 20 20 20 20 _Irreducible
65100 30 78 30 30 30 32 20 20 2f 2a 20 43 61 6e 6e 6f 0x0002 /* Canno
65110 74 20 45 58 50 52 44 55 50 5f 52 45 44 55 43 45 t EXPRDUP_REDUCE
65120 20 74 68 69 73 20 45 78 70 72 20 2a 2f 0a 0a 2f this Expr */../
65130 2a 0a 2a 2a 20 54 68 65 20 70 73 65 75 64 6f 2d *.** The pseudo-
65140 72 6f 75 74 69 6e 65 20 73 71 6c 69 74 65 33 45 routine sqlite3E
65150 78 70 72 53 65 74 49 72 72 65 64 75 63 69 62 6c xprSetIrreducibl
65160 65 20 73 65 74 73 20 74 68 65 20 45 50 32 5f 49 e sets the EP2_I
65170 72 72 65 64 75 63 69 62 6c 65 0a 2a 2a 20 66 6c rreducible.** fl
65180 61 67 20 6f 6e 20 61 6e 20 65 78 70 72 65 73 73 ag on an express
65190 69 6f 6e 20 73 74 72 75 63 74 75 72 65 2e 20 20 ion structure.
651a0 54 68 69 73 20 66 6c 61 67 20 69 73 20 75 73 65 This flag is use
651b0 64 20 66 6f 72 20 56 56 26 41 20 6f 6e 6c 79 2e d for VV&A only.
651c0 20 20 54 68 65 0a 2a 2a 20 72 6f 75 74 69 6e 65 The.** routine
651d0 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 is implemented
651e0 61 73 20 61 20 6d 61 63 72 6f 20 74 68 61 74 20 as a macro that
651f0 6f 6e 6c 79 20 77 6f 72 6b 73 20 77 68 65 6e 20 only works when
65200 69 6e 20 64 65 62 75 67 67 69 6e 67 20 6d 6f 64 in debugging mod
65210 65 2c 0a 2a 2a 20 73 6f 20 61 73 20 6e 6f 74 20 e,.** so as not
65220 74 6f 20 62 75 72 64 65 6e 20 70 72 6f 64 75 63 to burden produc
65230 74 69 6f 6e 20 63 6f 64 65 2e 0a 2a 2f 0a 23 69 tion code..*/.#i
65240 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 fdef SQLITE_DEBU
65250 47 0a 23 20 64 65 66 69 6e 65 20 45 78 70 72 53 G.# define ExprS
65260 65 74 49 72 72 65 64 75 63 69 62 6c 65 28 58 29 etIrreducible(X)
65270 20 20 28 58 29 2d 3e 66 6c 61 67 73 32 20 7c 3d (X)->flags2 |=
65280 20 45 50 32 5f 49 72 72 65 64 75 63 69 62 6c 65 EP2_Irreducible
65290 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 .#else.# define
652a0 45 78 70 72 53 65 74 49 72 72 65 64 75 63 69 62 ExprSetIrreducib
652b0 6c 65 28 58 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a le(X).#endif../*
652c0 0a 2a 2a 20 54 68 65 73 65 20 6d 61 63 72 6f 73 .** These macros
652d0 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 can be used to
652e0 74 65 73 74 2c 20 73 65 74 2c 20 6f 72 20 63 6c test, set, or cl
652f0 65 61 72 20 62 69 74 73 20 69 6e 20 74 68 65 20 ear bits in the
65300 0a 2a 2a 20 45 78 70 72 2e 66 6c 61 67 73 20 66 .** Expr.flags f
65310 69 65 6c 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 ield..*/.#define
65320 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 ExprHasProperty
65330 28 45 2c 50 29 20 20 20 20 20 28 28 28 45 29 2d (E,P) (((E)-
65340 3e 66 6c 61 67 73 26 28 50 29 29 3d 3d 28 50 29 >flags&(P))==(P)
65350 29 0a 23 64 65 66 69 6e 65 20 45 78 70 72 48 61 ).#define ExprHa
65360 73 41 6e 79 50 72 6f 70 65 72 74 79 28 45 2c 50 sAnyProperty(E,P
65370 29 20 20 28 28 28 45 29 2d 3e 66 6c 61 67 73 26 ) (((E)->flags&
65380 28 50 29 29 21 3d 30 29 0a 23 64 65 66 69 6e 65 (P))!=0).#define
65390 20 45 78 70 72 53 65 74 50 72 6f 70 65 72 74 79 ExprSetProperty
653a0 28 45 2c 50 29 20 20 20 20 20 28 45 29 2d 3e 66 (E,P) (E)->f
653b0 6c 61 67 73 7c 3d 28 50 29 0a 23 64 65 66 69 6e lags|=(P).#defin
653c0 65 20 45 78 70 72 43 6c 65 61 72 50 72 6f 70 65 e ExprClearPrope
653d0 72 74 79 28 45 2c 50 29 20 20 20 28 45 29 2d 3e rty(E,P) (E)->
653e0 66 6c 61 67 73 26 3d 7e 28 50 29 0a 0a 2f 2a 0a flags&=~(P)../*.
653f0 2a 2a 20 4d 61 63 72 6f 73 20 74 6f 20 64 65 74 ** Macros to det
65400 65 72 6d 69 6e 65 20 74 68 65 20 6e 75 6d 62 65 ermine the numbe
65410 72 20 6f 66 20 62 79 74 65 73 20 72 65 71 75 69 r of bytes requi
65420 72 65 64 20 62 79 20 61 20 6e 6f 72 6d 61 6c 20 red by a normal
65430 45 78 70 72 20 0a 2a 2a 20 73 74 72 75 63 74 2c Expr .** struct,
65440 20 61 6e 20 45 78 70 72 20 73 74 72 75 63 74 20 an Expr struct
65450 77 69 74 68 20 74 68 65 20 45 50 5f 52 65 64 75 with the EP_Redu
65460 63 65 64 20 66 6c 61 67 20 73 65 74 20 69 6e 20 ced flag set in
65470 45 78 70 72 2e 66 6c 61 67 73 20 0a 2a 2a 20 61 Expr.flags .** a
65480 6e 64 20 61 6e 20 45 78 70 72 20 73 74 72 75 63 nd an Expr struc
65490 74 20 77 69 74 68 20 74 68 65 20 45 50 5f 54 6f t with the EP_To
654a0 6b 65 6e 4f 6e 6c 79 20 66 6c 61 67 20 73 65 74 kenOnly flag set
654b0 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 45 58 50 ..*/.#define EXP
654c0 52 5f 46 55 4c 4c 53 49 5a 45 20 20 20 20 20 20 R_FULLSIZE
654d0 20 20 20 20 20 73 69 7a 65 6f 66 28 45 78 70 72 sizeof(Expr
654e0 29 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 ) /* F
654f0 75 6c 6c 20 73 69 7a 65 20 2a 2f 0a 23 64 65 66 ull size */.#def
65500 69 6e 65 20 45 58 50 52 5f 52 45 44 55 43 45 44 ine EXPR_REDUCED
65510 53 49 5a 45 20 20 20 20 20 20 20 20 6f 66 66 73 SIZE offs
65520 65 74 6f 66 28 45 78 70 72 2c 69 54 61 62 6c 65 etof(Expr,iTable
65530 29 20 20 2f 2a 20 43 6f 6d 6d 6f 6e 20 66 65 61 ) /* Common fea
65540 74 75 72 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 tures */.#define
65550 20 45 58 50 52 5f 54 4f 4b 45 4e 4f 4e 4c 59 53 EXPR_TOKENONLYS
65560 49 5a 45 20 20 20 20 20 20 6f 66 66 73 65 74 6f IZE offseto
65570 66 28 45 78 70 72 2c 70 4c 65 66 74 29 20 20 20 f(Expr,pLeft)
65580 2f 2a 20 46 65 77 65 72 20 66 65 61 74 75 72 65 /* Fewer feature
65590 73 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 46 6c 61 67 s */../*.** Flag
655a0 73 20 70 61 73 73 65 64 20 74 6f 20 74 68 65 20 s passed to the
655b0 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 29 sqlite3ExprDup()
655c0 20 66 75 6e 63 74 69 6f 6e 2e 20 53 65 65 20 74 function. See t
655d0 68 65 20 68 65 61 64 65 72 20 63 6f 6d 6d 65 6e he header commen
655e0 74 20 0a 2a 2a 20 61 62 6f 76 65 20 73 71 6c 69 t .** above sqli
655f0 74 65 33 45 78 70 72 44 75 70 28 29 20 66 6f 72 te3ExprDup() for
65600 20 64 65 74 61 69 6c 73 2e 0a 2a 2f 0a 23 64 65 details..*/.#de
65610 66 69 6e 65 20 45 58 50 52 44 55 50 5f 52 45 44 fine EXPRDUP_RED
65620 55 43 45 20 20 20 20 20 20 20 20 20 30 78 30 30 UCE 0x00
65630 30 31 20 20 2f 2a 20 55 73 65 64 20 72 65 64 75 01 /* Used redu
65640 63 65 64 2d 73 69 7a 65 20 45 78 70 72 20 6e 6f ced-size Expr no
65650 64 65 73 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 20 des */../*.** A
65660 6c 69 73 74 20 6f 66 20 65 78 70 72 65 73 73 69 list of expressi
65670 6f 6e 73 2e 20 20 45 61 63 68 20 65 78 70 72 65 ons. Each expre
65680 73 73 69 6f 6e 20 6d 61 79 20 6f 70 74 69 6f 6e ssion may option
65690 61 6c 6c 79 20 68 61 76 65 20 61 0a 2a 2a 20 6e ally have a.** n
656a0 61 6d 65 2e 20 20 41 6e 20 65 78 70 72 2f 6e 61 ame. An expr/na
656b0 6d 65 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 63 me combination c
656c0 61 6e 20 62 65 20 75 73 65 64 20 69 6e 20 73 65 an be used in se
656d0 76 65 72 61 6c 20 77 61 79 73 2c 20 73 75 63 68 veral ways, such
656e0 0a 2a 2a 20 61 73 20 74 68 65 20 6c 69 73 74 20 .** as the list
656f0 6f 66 20 22 65 78 70 72 20 41 53 20 49 44 22 20 of "expr AS ID"
65700 66 69 65 6c 64 73 20 66 6f 6c 6c 6f 77 69 6e 67 fields following
65710 20 61 20 22 53 45 4c 45 43 54 22 20 6f 72 20 69 a "SELECT" or i
65720 6e 20 74 68 65 0a 2a 2a 20 6c 69 73 74 20 6f 66 n the.** list of
65730 20 22 49 44 20 3d 20 65 78 70 72 22 20 69 74 65 "ID = expr" ite
65740 6d 73 20 69 6e 20 61 6e 20 55 50 44 41 54 45 2e ms in an UPDATE.
65750 20 20 41 20 6c 69 73 74 20 6f 66 20 65 78 70 72 A list of expr
65760 65 73 73 69 6f 6e 73 20 63 61 6e 0a 2a 2a 20 61 essions can.** a
65770 6c 73 6f 20 62 65 20 75 73 65 64 20 61 73 20 74 lso be used as t
65780 68 65 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 61 he argument to a
65790 20 66 75 6e 63 74 69 6f 6e 2c 20 69 6e 20 77 68 function, in wh
657a0 69 63 68 20 63 61 73 65 20 74 68 65 20 61 2e 7a ich case the a.z
657b0 4e 61 6d 65 0a 2a 2a 20 66 69 65 6c 64 20 69 73 Name.** field is
657c0 20 6e 6f 74 20 75 73 65 64 2e 0a 2a 2f 0a 73 74 not used..*/.st
657d0 72 75 63 74 20 45 78 70 72 4c 69 73 74 20 7b 0a ruct ExprList {.
657e0 20 20 69 6e 74 20 6e 45 78 70 72 3b 20 20 20 20 int nExpr;
657f0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
65800 65 72 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e er of expression
65810 73 20 6f 6e 20 74 68 65 20 6c 69 73 74 20 2a 2f s on the list */
65820 0a 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 3b 20 20 . int nAlloc;
65830 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
65840 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 61 ber of entries a
65850 6c 6c 6f 63 61 74 65 64 20 62 65 6c 6f 77 20 2a llocated below *
65860 2f 0a 20 20 69 6e 74 20 69 45 43 75 72 73 6f 72 /. int iECursor
65870 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 44 ; /* VD
65880 42 45 20 43 75 72 73 6f 72 20 61 73 73 6f 63 69 BE Cursor associ
65890 61 74 65 64 20 77 69 74 68 20 74 68 69 73 20 45 ated with this E
658a0 78 70 72 4c 69 73 74 20 2a 2f 0a 20 20 73 74 72 xprList */. str
658b0 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 uct ExprList_ite
658c0 6d 20 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 45 m {. Expr *pE
658d0 78 70 72 3b 20 20 20 20 20 20 20 20 20 20 20 2f xpr; /
658e0 2a 20 54 68 65 20 6c 69 73 74 20 6f 66 20 65 78 * The list of ex
658f0 70 72 65 73 73 69 6f 6e 73 20 2a 2f 0a 20 20 20 pressions */.
65900 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 char *zName;
65910 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e /* Token
65920 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
65930 20 74 68 69 73 20 65 78 70 72 65 73 73 69 6f 6e this expression
65940 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 2a 7a 53 */. char *zS
65950 70 61 6e 3b 20 20 20 20 20 20 20 20 20 20 20 2f pan; /
65960 2a 20 4f 72 69 67 69 6e 61 6c 20 74 65 78 74 20 * Original text
65970 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f of the expressio
65980 6e 20 2a 2f 0a 20 20 20 20 75 38 20 73 6f 72 74 n */. u8 sort
65990 4f 72 64 65 72 3b 20 20 20 20 20 20 20 20 20 20 Order;
659a0 2f 2a 20 31 20 66 6f 72 20 44 45 53 43 20 6f 72 /* 1 for DESC or
659b0 20 30 20 66 6f 72 20 41 53 43 20 2a 2f 0a 20 20 0 for ASC */.
659c0 20 20 75 38 20 64 6f 6e 65 3b 20 20 20 20 20 20 u8 done;
659d0 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 66 6c /* A fl
659e0 61 67 20 74 6f 20 69 6e 64 69 63 61 74 65 20 77 ag to indicate w
659f0 68 65 6e 20 70 72 6f 63 65 73 73 69 6e 67 20 69 hen processing i
65a00 73 20 66 69 6e 69 73 68 65 64 20 2a 2f 0a 20 20 s finished */.
65a10 20 20 75 31 36 20 69 43 6f 6c 3b 20 20 20 20 20 u16 iCol;
65a20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 /* For
65a30 4f 52 44 45 52 20 42 59 2c 20 63 6f 6c 75 6d 6e ORDER BY, column
65a40 20 6e 75 6d 62 65 72 20 69 6e 20 72 65 73 75 6c number in resul
65a50 74 20 73 65 74 20 2a 2f 0a 20 20 20 20 75 31 36 t set */. u16
65a60 20 69 41 6c 69 61 73 3b 20 20 20 20 20 20 20 20 iAlias;
65a70 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 74 /* Index int
65a80 6f 20 50 61 72 73 65 2e 61 41 6c 69 61 73 5b 5d o Parse.aAlias[]
65a90 20 66 6f 72 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 for zName */.
65aa0 7d 20 2a 61 3b 20 20 20 20 20 20 20 20 20 20 20 } *a;
65ab0 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 65 6e /* One en
65ac0 74 72 79 20 66 6f 72 20 65 61 63 68 20 65 78 70 try for each exp
65ad0 72 65 73 73 69 6f 6e 20 2a 2f 0a 7d 3b 0a 0a 2f ression */.};../
65ae0 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 *.** An instance
65af0 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 of this structu
65b00 72 65 20 69 73 20 75 73 65 64 20 62 79 20 74 68 re is used by th
65b10 65 20 70 61 72 73 65 72 20 74 6f 20 72 65 63 6f e parser to reco
65b20 72 64 20 62 6f 74 68 0a 2a 2a 20 74 68 65 20 70 rd both.** the p
65b30 61 72 73 65 20 74 72 65 65 20 66 6f 72 20 61 6e arse tree for an
65b40 20 65 78 70 72 65 73 73 69 6f 6e 20 61 6e 64 20 expression and
65b50 74 68 65 20 73 70 61 6e 20 6f 66 20 69 6e 70 75 the span of inpu
65b60 74 20 74 65 78 74 20 66 6f 72 20 61 6e 0a 2a 2a t text for an.**
65b70 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2f 0a expression..*/.
65b80 73 74 72 75 63 74 20 45 78 70 72 53 70 61 6e 20 struct ExprSpan
65b90 7b 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 3b {. Expr *pExpr;
65ba0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
65bb0 20 65 78 70 72 65 73 73 69 6f 6e 20 70 61 72 73 expression pars
65bc0 65 20 74 72 65 65 20 2a 2f 0a 20 20 63 6f 6e 73 e tree */. cons
65bd0 74 20 63 68 61 72 20 2a 7a 53 74 61 72 74 3b 20 t char *zStart;
65be0 20 20 2f 2a 20 46 69 72 73 74 20 63 68 61 72 61 /* First chara
65bf0 63 74 65 72 20 6f 66 20 69 6e 70 75 74 20 74 65 cter of input te
65c00 78 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 xt */. const ch
65c10 61 72 20 2a 7a 45 6e 64 3b 20 20 20 20 20 2f 2a ar *zEnd; /*
65c20 20 4f 6e 65 20 63 68 61 72 61 63 74 65 72 20 70 One character p
65c30 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 20 69 ast the end of i
65c40 6e 70 75 74 20 74 65 78 74 20 2a 2f 0a 7d 3b 0a nput text */.};.
65c50 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e ./*.** An instan
65c60 63 65 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 ce of this struc
65c70 74 75 72 65 20 63 61 6e 20 68 6f 6c 64 20 61 20 ture can hold a
65c80 73 69 6d 70 6c 65 20 6c 69 73 74 20 6f 66 20 69 simple list of i
65c90 64 65 6e 74 69 66 69 65 72 73 2c 0a 2a 2a 20 73 dentifiers,.** s
65ca0 75 63 68 20 61 73 20 74 68 65 20 6c 69 73 74 20 uch as the list
65cb0 22 61 2c 62 2c 63 22 20 69 6e 20 74 68 65 20 66 "a,b,c" in the f
65cc0 6f 6c 6c 6f 77 69 6e 67 20 73 74 61 74 65 6d 65 ollowing stateme
65cd0 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 nts:.**.**
65ce0 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 28 61 2c INSERT INTO t(a,
65cf0 62 2c 63 29 20 56 41 4c 55 45 53 20 2e 2e 2e 3b b,c) VALUES ...;
65d00 0a 2a 2a 20 20 20 20 20 20 43 52 45 41 54 45 20 .** CREATE
65d10 49 4e 44 45 58 20 69 64 78 20 4f 4e 20 74 28 61 INDEX idx ON t(a
65d20 2c 62 2c 63 29 3b 0a 2a 2a 20 20 20 20 20 20 43 ,b,c);.** C
65d30 52 45 41 54 45 20 54 52 49 47 47 45 52 20 74 72 REATE TRIGGER tr
65d40 69 67 20 42 45 46 4f 52 45 20 55 50 44 41 54 45 ig BEFORE UPDATE
65d50 20 4f 4e 20 74 28 61 2c 62 2c 63 29 20 2e 2e 2e ON t(a,b,c) ...
65d60 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 20 49 64 4c 69 ;.**.** The IdLi
65d70 73 74 2e 61 2e 69 64 78 20 66 69 65 6c 64 20 69 st.a.idx field i
65d80 73 20 75 73 65 64 20 77 68 65 6e 20 74 68 65 20 s used when the
65d90 49 64 4c 69 73 74 20 72 65 70 72 65 73 65 6e 74 IdList represent
65da0 73 20 74 68 65 20 6c 69 73 74 20 6f 66 0a 2a 2a s the list of.**
65db0 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 61 66 column names af
65dc0 74 65 72 20 61 20 74 61 62 6c 65 20 6e 61 6d 65 ter a table name
65dd0 20 69 6e 20 61 6e 20 49 4e 53 45 52 54 20 73 74 in an INSERT st
65de0 61 74 65 6d 65 6e 74 2e 20 20 49 6e 20 74 68 65 atement. In the
65df0 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 0a 2a 2a statement.**.**
65e00 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f INSERT INTO
65e10 20 74 28 61 2c 62 2c 63 29 20 2e 2e 2e 0a 2a 2a t(a,b,c) ....**
65e20 0a 2a 2a 20 49 66 20 22 61 22 20 69 73 20 74 68 .** If "a" is th
65e30 65 20 6b 2d 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 e k-th column of
65e40 20 74 61 62 6c 65 20 22 74 22 2c 20 74 68 65 6e table "t", then
65e50 20 49 64 4c 69 73 74 2e 61 5b 30 5d 2e 69 64 78 IdList.a[0].idx
65e60 3d 3d 6b 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 49 ==k..*/.struct I
65e70 64 4c 69 73 74 20 7b 0a 20 20 73 74 72 75 63 74 dList {. struct
65e80 20 49 64 4c 69 73 74 5f 69 74 65 6d 20 7b 0a 20 IdList_item {.
65e90 20 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 char *zName;
65ea0 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 /* Name of
65eb0 74 68 65 20 69 64 65 6e 74 69 66 69 65 72 20 2a the identifier *
65ec0 2f 0a 20 20 20 20 69 6e 74 20 69 64 78 3b 20 20 /. int idx;
65ed0 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 /* Index
65ee0 20 69 6e 20 73 6f 6d 65 20 54 61 62 6c 65 2e 61 in some Table.a
65ef0 43 6f 6c 5b 5d 20 6f 66 20 61 20 63 6f 6c 75 6d Col[] of a colum
65f00 6e 20 6e 61 6d 65 64 20 7a 4e 61 6d 65 20 2a 2f n named zName */
65f10 0a 20 20 7d 20 2a 61 3b 0a 20 20 69 6e 74 20 6e . } *a;. int n
65f20 49 64 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e Id; /* N
65f30 75 6d 62 65 72 20 6f 66 20 69 64 65 6e 74 69 66 umber of identif
65f40 69 65 72 73 20 6f 6e 20 74 68 65 20 6c 69 73 74 iers on the list
65f50 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 */. int nAlloc
65f60 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 ; /* Number
65f70 20 6f 66 20 65 6e 74 72 69 65 73 20 61 6c 6c 6f of entries allo
65f80 63 61 74 65 64 20 66 6f 72 20 61 5b 5d 20 62 65 cated for a[] be
65f90 6c 6f 77 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a low */.};../*.**
65fa0 20 54 68 65 20 62 69 74 6d 61 73 6b 20 64 61 74 The bitmask dat
65fb0 61 74 79 70 65 20 64 65 66 69 6e 65 64 20 62 65 atype defined be
65fc0 6c 6f 77 20 69 73 20 75 73 65 64 20 66 6f 72 20 low is used for
65fd0 76 61 72 69 6f 75 73 20 6f 70 74 69 6d 69 7a 61 various optimiza
65fe0 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 43 68 61 tions..**.** Cha
65ff0 6e 67 69 6e 67 20 74 68 69 73 20 66 72 6f 6d 20 nging this from
66000 61 20 36 34 2d 62 69 74 20 74 6f 20 61 20 33 32 a 64-bit to a 32
66010 2d 62 69 74 20 74 79 70 65 20 6c 69 6d 69 74 73 -bit type limits
66020 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a the number of.*
66030 2a 20 74 61 62 6c 65 73 20 69 6e 20 61 20 6a 6f * tables in a jo
66040 69 6e 20 74 6f 20 33 32 20 69 6e 73 74 65 61 64 in to 32 instead
66050 20 6f 66 20 36 34 2e 20 20 42 75 74 20 69 74 20 of 64. But it
66060 61 6c 73 6f 20 72 65 64 75 63 65 73 20 74 68 65 also reduces the
66070 20 73 69 7a 65 0a 2a 2a 20 6f 66 20 74 68 65 20 size.** of the
66080 6c 69 62 72 61 72 79 20 62 79 20 37 33 38 20 62 library by 738 b
66090 79 74 65 73 20 6f 6e 20 69 78 38 36 2e 0a 2a 2f ytes on ix86..*/
660a0 0a 74 79 70 65 64 65 66 20 75 36 34 20 42 69 74 .typedef u64 Bit
660b0 6d 61 73 6b 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 mask;../*.** The
660c0 20 6e 75 6d 62 65 72 20 6f 66 20 62 69 74 73 20 number of bits
660d0 69 6e 20 61 20 42 69 74 6d 61 73 6b 2e 20 20 22 in a Bitmask. "
660e0 42 4d 53 22 20 6d 65 61 6e 73 20 22 42 69 74 4d BMS" means "BitM
660f0 61 73 6b 20 53 69 7a 65 22 2e 0a 2a 2f 0a 23 64 ask Size"..*/.#d
66100 65 66 69 6e 65 20 42 4d 53 20 20 28 28 69 6e 74 efine BMS ((int
66110 29 28 73 69 7a 65 6f 66 28 42 69 74 6d 61 73 6b )(sizeof(Bitmask
66120 29 2a 38 29 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 )*8))../*.** The
66130 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 following struc
66140 74 75 72 65 20 64 65 73 63 72 69 62 65 73 20 74 ture describes t
66150 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f he FROM clause o
66160 66 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 f a SELECT state
66170 6d 65 6e 74 2e 0a 2a 2a 20 45 61 63 68 20 74 61 ment..** Each ta
66180 62 6c 65 20 6f 72 20 73 75 62 71 75 65 72 79 20 ble or subquery
66190 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 in the FROM clau
661a0 73 65 20 69 73 20 61 20 73 65 70 61 72 61 74 65 se is a separate
661b0 20 65 6c 65 6d 65 6e 74 20 6f 66 0a 2a 2a 20 74 element of.** t
661c0 68 65 20 53 72 63 4c 69 73 74 2e 61 5b 5d 20 61 he SrcList.a[] a
661d0 72 72 61 79 2e 0a 2a 2a 0a 2a 2a 20 57 69 74 68 rray..**.** With
661e0 20 74 68 65 20 61 64 64 69 74 69 6f 6e 20 6f 66 the addition of
661f0 20 6d 75 6c 74 69 70 6c 65 20 64 61 74 61 62 61 multiple databa
66200 73 65 20 73 75 70 70 6f 72 74 2c 20 74 68 65 20 se support, the
66210 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 following struct
66220 75 72 65 0a 2a 2a 20 63 61 6e 20 61 6c 73 6f 20 ure.** can also
66230 62 65 20 75 73 65 64 20 74 6f 20 64 65 73 63 72 be used to descr
66240 69 62 65 20 61 20 70 61 72 74 69 63 75 6c 61 72 ibe a particular
66250 20 74 61 62 6c 65 20 73 75 63 68 20 61 73 20 74 table such as t
66260 68 65 20 74 61 62 6c 65 20 74 68 61 74 0a 2a 2a he table that.**
66270 20 69 73 20 6d 6f 64 69 66 69 65 64 20 62 79 20 is modified by
66280 61 6e 20 49 4e 53 45 52 54 2c 20 44 45 4c 45 54 an INSERT, DELET
66290 45 2c 20 6f 72 20 55 50 44 41 54 45 20 73 74 61 E, or UPDATE sta
662a0 74 65 6d 65 6e 74 2e 20 20 49 6e 20 73 74 61 6e tement. In stan
662b0 64 61 72 64 20 53 51 4c 2c 0a 2a 2a 20 73 75 63 dard SQL,.** suc
662c0 68 20 61 20 74 61 62 6c 65 20 6d 75 73 74 20 62 h a table must b
662d0 65 20 61 20 73 69 6d 70 6c 65 20 6e 61 6d 65 3a e a simple name:
662e0 20 49 44 2e 20 20 42 75 74 20 69 6e 20 53 51 4c ID. But in SQL
662f0 69 74 65 2c 20 74 68 65 20 74 61 62 6c 65 20 63 ite, the table c
66300 61 6e 0a 2a 2a 20 6e 6f 77 20 62 65 20 69 64 65 an.** now be ide
66310 6e 74 69 66 69 65 64 20 62 79 20 61 20 64 61 74 ntified by a dat
66320 61 62 61 73 65 20 6e 61 6d 65 2c 20 61 20 64 6f abase name, a do
66330 74 2c 20 74 68 65 6e 20 74 68 65 20 74 61 62 6c t, then the tabl
66340 65 20 6e 61 6d 65 3a 20 49 44 2e 49 44 2e 0a 2a e name: ID.ID..*
66350 2a 0a 2a 2a 20 54 68 65 20 6a 6f 69 6e 74 79 70 *.** The jointyp
66360 65 20 73 74 61 72 74 73 20 6f 75 74 20 73 68 6f e starts out sho
66370 77 69 6e 67 20 74 68 65 20 6a 6f 69 6e 20 74 79 wing the join ty
66380 70 65 20 62 65 74 77 65 65 6e 20 74 68 65 20 63 pe between the c
66390 75 72 72 65 6e 74 20 74 61 62 6c 65 0a 2a 2a 20 urrent table.**
663a0 61 6e 64 20 74 68 65 20 6e 65 78 74 20 74 61 62 and the next tab
663b0 6c 65 20 6f 6e 20 74 68 65 20 6c 69 73 74 2e 20 le on the list.
663c0 20 54 68 65 20 70 61 72 73 65 72 20 62 75 69 6c The parser buil
663d0 64 73 20 74 68 65 20 6c 69 73 74 20 74 68 69 73 ds the list this
663e0 20 77 61 79 2e 0a 2a 2a 20 42 75 74 20 73 71 6c way..** But sql
663f0 69 74 65 33 53 72 63 4c 69 73 74 53 68 69 66 74 ite3SrcListShift
66400 4a 6f 69 6e 54 79 70 65 28 29 20 6c 61 74 65 72 JoinType() later
66410 20 73 68 69 66 74 73 20 74 68 65 20 6a 6f 69 6e shifts the join
66420 74 79 70 65 73 20 73 6f 20 74 68 61 74 20 65 61 types so that ea
66430 63 68 0a 2a 2a 20 6a 6f 69 6e 74 79 70 65 20 65 ch.** jointype e
66440 78 70 72 65 73 73 65 73 20 74 68 65 20 6a 6f 69 xpresses the joi
66450 6e 20 62 65 74 77 65 65 6e 20 74 68 65 20 74 61 n between the ta
66460 62 6c 65 20 61 6e 64 20 74 68 65 20 70 72 65 76 ble and the prev
66470 69 6f 75 73 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 ious table..*/.s
66480 74 72 75 63 74 20 53 72 63 4c 69 73 74 20 7b 0a truct SrcList {.
66490 20 20 69 31 36 20 6e 53 72 63 3b 20 20 20 20 20 i16 nSrc;
664a0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
664b0 74 61 62 6c 65 73 20 6f 72 20 73 75 62 71 75 65 tables or subque
664c0 72 69 65 73 20 69 6e 20 74 68 65 20 46 52 4f 4d ries in the FROM
664d0 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 31 36 clause */. i16
664e0 20 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 20 2f 2a nAlloc; /*
664f0 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 Number of entri
66500 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20 es allocated in
66510 61 5b 5d 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 73 a[] below */. s
66520 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 truct SrcList_it
66530 65 6d 20 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a em {. char *z
66540 44 61 74 61 62 61 73 65 3b 20 20 2f 2a 20 4e 61 Database; /* Na
66550 6d 65 20 6f 66 20 64 61 74 61 62 61 73 65 20 68 me of database h
66560 6f 6c 64 69 6e 67 20 74 68 69 73 20 74 61 62 6c olding this tabl
66570 65 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 2a 7a e */. char *z
66580 4e 61 6d 65 3b 20 20 20 20 20 20 2f 2a 20 4e 61 Name; /* Na
66590 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 me of the table
665a0 2a 2f 0a 20 20 20 20 63 68 61 72 20 2a 7a 41 6c */. char *zAl
665b0 69 61 73 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 ias; /* The
665c0 22 42 22 20 70 61 72 74 20 6f 66 20 61 20 22 41 "B" part of a "A
665d0 20 41 53 20 42 22 20 70 68 72 61 73 65 2e 20 20 AS B" phrase.
665e0 7a 4e 61 6d 65 20 69 73 20 74 68 65 20 22 41 22 zName is the "A"
665f0 20 2a 2f 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 */. Table *p
66600 54 61 62 3b 20 20 20 20 20 20 2f 2a 20 41 6e 20 Tab; /* An
66610 53 51 4c 20 74 61 62 6c 65 20 63 6f 72 72 65 73 SQL table corres
66620 70 6f 6e 64 69 6e 67 20 74 6f 20 7a 4e 61 6d 65 ponding to zName
66630 20 2a 2f 0a 20 20 20 20 53 65 6c 65 63 74 20 2a */. Select *
66640 70 53 65 6c 65 63 74 3b 20 20 2f 2a 20 41 20 53 pSelect; /* A S
66650 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 ELECT statement
66660 75 73 65 64 20 69 6e 20 70 6c 61 63 65 20 6f 66 used in place of
66670 20 61 20 74 61 62 6c 65 20 6e 61 6d 65 20 2a 2f a table name */
66680 0a 20 20 20 20 75 38 20 69 73 50 6f 70 75 6c 61 . u8 isPopula
66690 74 65 64 3b 20 20 20 2f 2a 20 54 65 6d 70 6f 72 ted; /* Tempor
666a0 61 72 79 20 74 61 62 6c 65 20 61 73 73 6f 63 69 ary table associ
666b0 61 74 65 64 20 77 69 74 68 20 53 45 4c 45 43 54 ated with SELECT
666c0 20 69 73 20 70 6f 70 75 6c 61 74 65 64 20 2a 2f is populated */
666d0 0a 20 20 20 20 75 38 20 6a 6f 69 6e 74 79 70 65 . u8 jointype
666e0 3b 20 20 20 20 20 20 2f 2a 20 54 79 70 65 20 6f ; /* Type o
666f0 66 20 6a 6f 69 6e 20 62 65 74 77 65 65 6e 20 74 f join between t
66700 68 69 73 20 61 62 6c 65 20 61 6e 64 20 74 68 65 his able and the
66710 20 70 72 65 76 69 6f 75 73 20 2a 2f 0a 20 20 20 previous */.
66720 20 75 38 20 6e 6f 74 49 6e 64 65 78 65 64 3b 20 u8 notIndexed;
66730 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 /* True if th
66740 65 72 65 20 69 73 20 61 20 4e 4f 54 20 49 4e 44 ere is a NOT IND
66750 45 58 45 44 20 63 6c 61 75 73 65 20 2a 2f 0a 20 EXED clause */.
66760 20 20 20 69 6e 74 20 69 43 75 72 73 6f 72 3b 20 int iCursor;
66770 20 20 20 20 20 2f 2a 20 54 68 65 20 56 44 42 45 /* The VDBE
66780 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 75 cursor number u
66790 73 65 64 20 74 6f 20 61 63 63 65 73 73 20 74 68 sed to access th
667a0 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 is table */.
667b0 45 78 70 72 20 2a 70 4f 6e 3b 20 20 20 20 20 20 Expr *pOn;
667c0 20 20 2f 2a 20 54 68 65 20 4f 4e 20 63 6c 61 75 /* The ON clau
667d0 73 65 20 6f 66 20 61 20 6a 6f 69 6e 20 2a 2f 0a se of a join */.
667e0 20 20 20 20 49 64 4c 69 73 74 20 2a 70 55 73 69 IdList *pUsi
667f0 6e 67 3b 20 20 20 2f 2a 20 54 68 65 20 55 53 49 ng; /* The USI
66800 4e 47 20 63 6c 61 75 73 65 20 6f 66 20 61 20 6a NG clause of a j
66810 6f 69 6e 20 2a 2f 0a 20 20 20 20 42 69 74 6d 61 oin */. Bitma
66820 73 6b 20 63 6f 6c 55 73 65 64 3b 20 20 2f 2a 20 sk colUsed; /*
66830 42 69 74 20 4e 20 28 31 3c 3c 4e 29 20 73 65 74 Bit N (1<<N) set
66840 20 69 66 20 63 6f 6c 75 6d 6e 20 4e 20 6f 66 20 if column N of
66850 70 54 61 62 20 69 73 20 75 73 65 64 20 2a 2f 0a pTab is used */.
66860 20 20 20 20 63 68 61 72 20 2a 7a 49 6e 64 65 78 char *zIndex
66870 3b 20 20 20 20 20 2f 2a 20 49 64 65 6e 74 69 66 ; /* Identif
66880 69 65 72 20 66 72 6f 6d 20 22 49 4e 44 45 58 45 ier from "INDEXE
66890 44 20 42 59 20 3c 7a 49 6e 64 65 78 3e 22 20 63 D BY <zIndex>" c
668a0 6c 61 75 73 65 20 2a 2f 0a 20 20 20 20 49 6e 64 lause */. Ind
668b0 65 78 20 2a 70 49 6e 64 65 78 3b 20 20 20 20 2f ex *pIndex; /
668c0 2a 20 49 6e 64 65 78 20 73 74 72 75 63 74 75 72 * Index structur
668d0 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 e corresponding
668e0 74 6f 20 7a 49 6e 64 65 78 2c 20 69 66 20 61 6e to zIndex, if an
668f0 79 20 2a 2f 0a 20 20 7d 20 61 5b 31 5d 3b 20 20 y */. } a[1];
66900 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e /* On
66910 65 20 65 6e 74 72 79 20 66 6f 72 20 65 61 63 68 e entry for each
66920 20 69 64 65 6e 74 69 66 69 65 72 20 6f 6e 20 74 identifier on t
66930 68 65 20 6c 69 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f he list */.};../
66940 2a 0a 2a 2a 20 50 65 72 6d 69 74 74 65 64 20 76 *.** Permitted v
66950 61 6c 75 65 73 20 6f 66 20 74 68 65 20 53 72 63 alues of the Src
66960 4c 69 73 74 2e 61 2e 6a 6f 69 6e 74 79 70 65 20 List.a.jointype
66970 66 69 65 6c 64 0a 2a 2f 0a 23 64 65 66 69 6e 65 field.*/.#define
66980 20 4a 54 5f 49 4e 4e 45 52 20 20 20 20 20 30 78 JT_INNER 0x
66990 30 30 30 31 20 20 20 20 2f 2a 20 41 6e 79 20 6b 0001 /* Any k
669a0 69 6e 64 20 6f 66 20 69 6e 6e 65 72 20 6f 72 20 ind of inner or
669b0 63 72 6f 73 73 20 6a 6f 69 6e 20 2a 2f 0a 23 64 cross join */.#d
669c0 65 66 69 6e 65 20 4a 54 5f 43 52 4f 53 53 20 20 efine JT_CROSS
669d0 20 20 20 30 78 30 30 30 32 20 20 20 20 2f 2a 20 0x0002 /*
669e0 45 78 70 6c 69 63 69 74 20 75 73 65 20 6f 66 20 Explicit use of
669f0 74 68 65 20 43 52 4f 53 53 20 6b 65 79 77 6f 72 the CROSS keywor
66a00 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 54 5f d */.#define JT_
66a10 4e 41 54 55 52 41 4c 20 20 20 30 78 30 30 30 34 NATURAL 0x0004
66a20 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 /* True for
66a30 61 20 22 6e 61 74 75 72 61 6c 22 20 6a 6f 69 6e a "natural" join
66a40 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 54 5f 4c */.#define JT_L
66a50 45 46 54 20 20 20 20 20 20 30 78 30 30 30 38 20 EFT 0x0008
66a60 20 20 20 2f 2a 20 4c 65 66 74 20 6f 75 74 65 72 /* Left outer
66a70 20 6a 6f 69 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 join */.#define
66a80 20 4a 54 5f 52 49 47 48 54 20 20 20 20 20 30 78 JT_RIGHT 0x
66a90 30 30 31 30 20 20 20 20 2f 2a 20 52 69 67 68 74 0010 /* Right
66aa0 20 6f 75 74 65 72 20 6a 6f 69 6e 20 2a 2f 0a 23 outer join */.#
66ab0 64 65 66 69 6e 65 20 4a 54 5f 4f 55 54 45 52 20 define JT_OUTER
66ac0 20 20 20 20 30 78 30 30 32 30 20 20 20 20 2f 2a 0x0020 /*
66ad0 20 54 68 65 20 22 4f 55 54 45 52 22 20 6b 65 79 The "OUTER" key
66ae0 77 6f 72 64 20 69 73 20 70 72 65 73 65 6e 74 20 word is present
66af0 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 54 5f 45 52 */.#define JT_ER
66b00 52 4f 52 20 20 20 20 20 30 78 30 30 34 30 20 20 ROR 0x0040
66b10 20 20 2f 2a 20 75 6e 6b 6e 6f 77 6e 20 6f 72 20 /* unknown or
66b20 75 6e 73 75 70 70 6f 72 74 65 64 20 6a 6f 69 6e unsupported join
66b30 20 74 79 70 65 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a type */.../*.**
66b40 20 41 20 57 68 65 72 65 50 6c 61 6e 20 6f 62 6a A WherePlan obj
66b50 65 63 74 20 68 6f 6c 64 73 20 69 6e 66 6f 72 6d ect holds inform
66b60 61 74 69 6f 6e 20 74 68 61 74 20 64 65 73 63 72 ation that descr
66b70 69 62 65 73 20 61 20 6c 6f 6f 6b 75 70 0a 2a 2a ibes a lookup.**
66b80 20 73 74 72 61 74 65 67 79 2e 0a 2a 2a 0a 2a 2a strategy..**.**
66b90 20 54 68 69 73 20 6f 62 6a 65 63 74 20 69 73 20 This object is
66ba0 69 6e 74 65 6e 64 65 64 20 74 6f 20 62 65 20 6f intended to be o
66bb0 70 61 71 75 65 20 6f 75 74 73 69 64 65 20 6f 66 paque outside of
66bc0 20 74 68 65 20 77 68 65 72 65 2e 63 20 6d 6f 64 the where.c mod
66bd0 75 6c 65 2e 0a 2a 2a 20 49 74 20 69 73 20 69 6e ule..** It is in
66be0 63 6c 75 64 65 64 20 68 65 72 65 20 6f 6e 6c 79 cluded here only
66bf0 20 73 6f 20 74 68 61 74 20 74 68 61 74 20 63 6f so that that co
66c00 6d 70 69 6c 65 72 20 77 69 6c 6c 20 6b 6e 6f 77 mpiler will know
66c10 20 68 6f 77 20 62 69 67 20 69 74 0a 2a 2a 20 69 how big it.** i
66c20 73 2e 20 20 4e 6f 6e 65 20 6f 66 20 74 68 65 20 s. None of the
66c30 66 69 65 6c 64 73 20 69 6e 20 74 68 69 73 20 6f fields in this o
66c40 62 6a 65 63 74 20 73 68 6f 75 6c 64 20 62 65 20 bject should be
66c50 75 73 65 64 20 6f 75 74 73 69 64 65 20 6f 66 0a used outside of.
66c60 2a 2a 20 74 68 65 20 77 68 65 72 65 2e 63 20 6d ** the where.c m
66c70 6f 64 75 6c 65 2e 0a 2a 2a 0a 2a 2a 20 57 69 74 odule..**.** Wit
66c80 68 69 6e 20 74 68 65 20 75 6e 69 6f 6e 2c 20 70 hin the union, p
66c90 49 64 78 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 Idx is only used
66ca0 20 77 68 65 6e 20 77 73 46 6c 61 67 73 26 57 48 when wsFlags&WH
66cb0 45 52 45 5f 49 4e 44 45 58 45 44 20 69 73 20 74 ERE_INDEXED is t
66cc0 72 75 65 2e 0a 2a 2a 20 70 54 65 72 6d 20 69 73 rue..** pTerm is
66cd0 20 6f 6e 6c 79 20 75 73 65 64 20 77 68 65 6e 20 only used when
66ce0 77 73 46 6c 61 67 73 26 57 48 45 52 45 5f 4d 55 wsFlags&WHERE_MU
66cf0 4c 54 49 5f 4f 52 20 69 73 20 74 72 75 65 2e 20 LTI_OR is true.
66d00 20 41 6e 64 20 70 56 74 61 62 49 64 78 0a 2a 2a And pVtabIdx.**
66d10 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 77 68 is only used wh
66d20 65 6e 20 77 73 46 6c 61 67 73 26 57 48 45 52 45 en wsFlags&WHERE
66d30 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 69 73 _VIRTUALTABLE is
66d40 20 74 72 75 65 2e 20 20 49 74 20 69 73 20 6e 65 true. It is ne
66d50 76 65 72 20 74 68 65 0a 2a 2a 20 63 61 73 65 20 ver the.** case
66d60 74 68 61 74 20 6d 6f 72 65 20 74 68 61 6e 20 6f that more than o
66d70 6e 65 20 6f 66 20 74 68 65 73 65 20 63 6f 6e 64 ne of these cond
66d80 69 74 69 6f 6e 73 20 69 73 20 74 72 75 65 2e 0a itions is true..
66d90 2a 2f 0a 73 74 72 75 63 74 20 57 68 65 72 65 50 */.struct WhereP
66da0 6c 61 6e 20 7b 0a 20 20 75 33 32 20 77 73 46 6c lan {. u32 wsFl
66db0 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 ags;
66dc0 20 20 20 20 20 20 20 2f 2a 20 57 48 45 52 45 5f /* WHERE_
66dd0 2a 20 66 6c 61 67 73 20 74 68 61 74 20 64 65 73 * flags that des
66de0 63 72 69 62 65 20 74 68 65 20 73 74 72 61 74 65 cribe the strate
66df0 67 79 20 2a 2f 0a 20 20 75 33 32 20 6e 45 71 3b gy */. u32 nEq;
66e00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
66e10 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
66e20 20 6f 66 20 3d 3d 20 63 6f 6e 73 74 72 61 69 6e of == constrain
66e30 74 73 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b 0a ts */. union {.
66e40 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b Index *pIdx;
66e50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
66e60 20 20 20 2f 2a 20 49 6e 64 65 78 20 77 68 65 6e /* Index when
66e70 20 57 48 45 52 45 5f 49 4e 44 45 58 45 44 20 69 WHERE_INDEXED i
66e80 73 20 74 72 75 65 20 2a 2f 0a 20 20 20 20 73 74 s true */. st
66e90 72 75 63 74 20 57 68 65 72 65 54 65 72 6d 20 2a ruct WhereTerm *
66ea0 70 54 65 72 6d 3b 20 20 20 20 20 20 20 2f 2a 20 pTerm; /*
66eb0 57 48 45 52 45 20 63 6c 61 75 73 65 20 74 65 72 WHERE clause ter
66ec0 6d 20 66 6f 72 20 4f 52 2d 73 65 61 72 63 68 20 m for OR-search
66ed0 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 69 */. sqlite3_i
66ee0 6e 64 65 78 5f 69 6e 66 6f 20 2a 70 56 74 61 62 ndex_info *pVtab
66ef0 49 64 78 3b 20 20 2f 2a 20 56 69 72 74 75 61 6c Idx; /* Virtual
66f00 20 74 61 62 6c 65 20 69 6e 64 65 78 20 74 6f 20 table index to
66f10 75 73 65 20 2a 2f 0a 20 20 7d 20 75 3b 0a 7d 3b use */. } u;.};
66f20 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 65 61 63 68 ../*.** For each
66f30 20 6e 65 73 74 65 64 20 6c 6f 6f 70 20 69 6e 20 nested loop in
66f40 61 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 69 a WHERE clause i
66f50 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c 20 74 mplementation, t
66f60 68 65 20 57 68 65 72 65 49 6e 66 6f 0a 2a 2a 20 he WhereInfo.**
66f70 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 structure contai
66f80 6e 73 20 61 20 73 69 6e 67 6c 65 20 69 6e 73 74 ns a single inst
66f90 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 72 ance of this str
66fa0 75 63 74 75 72 65 2e 20 20 54 68 69 73 20 73 74 ucture. This st
66fb0 72 75 63 74 75 72 65 0a 2a 2a 20 69 73 20 69 6e ructure.** is in
66fc0 74 65 6e 64 65 64 20 74 6f 20 62 65 20 70 72 69 tended to be pri
66fd0 76 61 74 65 20 74 68 65 20 74 68 65 20 77 68 65 vate the the whe
66fe0 72 65 2e 63 20 6d 6f 64 75 6c 65 20 61 6e 64 20 re.c module and
66ff0 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 0a 2a 2a should not be.**
67000 20 61 63 63 65 73 73 20 6f 72 20 6d 6f 64 69 66 access or modif
67010 69 65 64 20 62 79 20 6f 74 68 65 72 20 6d 6f 64 ied by other mod
67020 75 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ules..**.** The
67030 70 49 64 78 49 6e 66 6f 20 66 69 65 6c 64 20 69 pIdxInfo field i
67040 73 20 75 73 65 64 20 74 6f 20 68 65 6c 70 20 70 s used to help p
67050 69 63 6b 20 74 68 65 20 62 65 73 74 20 69 6e 64 ick the best ind
67060 65 78 20 6f 6e 20 61 0a 2a 2a 20 76 69 72 74 75 ex on a.** virtu
67070 61 6c 20 74 61 62 6c 65 2e 20 20 54 68 65 20 70 al table. The p
67080 49 64 78 49 6e 66 6f 20 70 6f 69 6e 74 65 72 20 IdxInfo pointer
67090 63 6f 6e 74 61 69 6e 73 20 69 6e 64 65 78 69 6e contains indexin
670a0 67 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e g.** information
670b0 20 66 6f 72 20 74 68 65 20 69 2d 74 68 20 74 61 for the i-th ta
670c0 62 6c 65 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 ble in the FROM
670d0 63 6c 61 75 73 65 20 62 65 66 6f 72 65 20 72 65 clause before re
670e0 6f 72 64 65 72 69 6e 67 2e 0a 2a 2a 20 41 6c 6c ordering..** All
670f0 20 74 68 65 20 70 49 64 78 49 6e 66 6f 20 70 6f the pIdxInfo po
67100 69 6e 74 65 72 73 20 61 72 65 20 66 72 65 65 64 inters are freed
67110 20 62 79 20 77 68 65 72 65 49 6e 66 6f 46 72 65 by whereInfoFre
67120 65 28 29 20 69 6e 20 77 68 65 72 65 2e 63 2e 0a e() in where.c..
67130 2a 2a 20 41 6c 6c 20 6f 74 68 65 72 20 69 6e 66 ** All other inf
67140 6f 72 6d 61 74 69 6f 6e 20 69 6e 20 74 68 65 20 ormation in the
67150 69 2d 74 68 20 57 68 65 72 65 4c 65 76 65 6c 20 i-th WhereLevel
67160 6f 62 6a 65 63 74 20 66 6f 72 20 74 68 65 20 69 object for the i
67170 2d 74 68 20 74 61 62 6c 65 0a 2a 2a 20 61 66 74 -th table.** aft
67180 65 72 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f er FROM clause o
67190 72 64 65 72 69 6e 67 2e 0a 2a 2f 0a 73 74 72 75 rdering..*/.stru
671a0 63 74 20 57 68 65 72 65 4c 65 76 65 6c 20 7b 0a ct WhereLevel {.
671b0 20 20 57 68 65 72 65 50 6c 61 6e 20 70 6c 61 6e WherePlan plan
671c0 3b 20 20 20 20 20 20 20 2f 2a 20 71 75 65 72 79 ; /* query
671d0 20 70 6c 61 6e 20 66 6f 72 20 74 68 69 73 20 65 plan for this e
671e0 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 46 52 lement of the FR
671f0 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 OM clause */. i
67200 6e 74 20 69 4c 65 66 74 4a 6f 69 6e 3b 20 20 20 nt iLeftJoin;
67210 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 63 /* Memory c
67220 65 6c 6c 20 75 73 65 64 20 74 6f 20 69 6d 70 6c ell used to impl
67230 65 6d 65 6e 74 20 4c 45 46 54 20 4f 55 54 45 52 ement LEFT OUTER
67240 20 4a 4f 49 4e 20 2a 2f 0a 20 20 69 6e 74 20 69 JOIN */. int i
67250 54 61 62 43 75 72 3b 20 20 20 20 20 20 20 20 20 TabCur;
67260 20 2f 2a 20 54 68 65 20 56 44 42 45 20 63 75 72 /* The VDBE cur
67270 73 6f 72 20 75 73 65 64 20 74 6f 20 61 63 63 65 sor used to acce
67280 73 73 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a ss the table */.
67290 20 20 69 6e 74 20 69 49 64 78 43 75 72 3b 20 20 int iIdxCur;
672a0 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 /* The V
672b0 44 42 45 20 63 75 72 73 6f 72 20 75 73 65 64 20 DBE cursor used
672c0 74 6f 20 61 63 63 65 73 73 20 70 49 64 78 20 2a to access pIdx *
672d0 2f 0a 20 20 69 6e 74 20 61 64 64 72 42 72 6b 3b /. int addrBrk;
672e0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d /* Jum
672f0 70 20 68 65 72 65 20 74 6f 20 62 72 65 61 6b 20 p here to break
67300 6f 75 74 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 out of the loop
67310 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 4e 78 74 */. int addrNxt
67320 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4a 75 ; /* Ju
67330 6d 70 20 68 65 72 65 20 74 6f 20 73 74 61 72 74 mp here to start
67340 20 74 68 65 20 6e 65 78 74 20 49 4e 20 63 6f 6d the next IN com
67350 62 69 6e 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e bination */. in
67360 74 20 61 64 64 72 43 6f 6e 74 3b 20 20 20 20 20 t addrCont;
67370 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 /* Jump here
67380 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 77 69 74 to continue wit
67390 68 20 74 68 65 20 6e 65 78 74 20 6c 6f 6f 70 20 h the next loop
673a0 63 79 63 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 61 cycle */. int a
673b0 64 64 72 46 69 72 73 74 3b 20 20 20 20 20 20 20 ddrFirst;
673c0 20 2f 2a 20 46 69 72 73 74 20 69 6e 73 74 72 75 /* First instru
673d0 63 74 69 6f 6e 20 6f 66 20 69 6e 74 65 72 69 6f ction of interio
673e0 72 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 2a 2f r of the loop */
673f0 0a 20 20 75 38 20 69 46 72 6f 6d 3b 20 20 20 20 . u8 iFrom;
67400 20 20 20 20 20 20 20 20 20 2f 2a 20 57 68 69 63 /* Whic
67410 68 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 46 h entry in the F
67420 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 ROM clause */.
67430 75 38 20 6f 70 2c 20 70 35 3b 20 20 20 20 20 20 u8 op, p5;
67440 20 20 20 20 20 20 2f 2a 20 4f 70 63 6f 64 65 20 /* Opcode
67450 61 6e 64 20 50 35 20 6f 66 20 74 68 65 20 6f 70 and P5 of the op
67460 63 6f 64 65 20 74 68 61 74 20 65 6e 64 73 20 74 code that ends t
67470 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 he loop */. int
67480 20 70 31 2c 20 70 32 3b 20 20 20 20 20 20 20 20 p1, p2;
67490 20 20 20 2f 2a 20 4f 70 65 72 61 6e 64 73 20 6f /* Operands o
674a0 66 20 74 68 65 20 6f 70 63 6f 64 65 20 75 73 65 f the opcode use
674b0 64 20 74 6f 20 65 6e 64 73 20 74 68 65 20 6c 6f d to ends the lo
674c0 6f 70 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b 20 op */. union {
674d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
674e0 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 68 61 Information tha
674f0 74 20 64 65 70 65 6e 64 73 20 6f 6e 20 70 6c 61 t depends on pla
67500 6e 2e 77 73 46 6c 61 67 73 20 2a 2f 0a 20 20 20 n.wsFlags */.
67510 20 73 74 72 75 63 74 20 7b 0a 20 20 20 20 20 20 struct {.
67520 69 6e 74 20 6e 49 6e 3b 20 20 20 20 20 20 20 20 int nIn;
67530 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
67540 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 49 of entries in aI
67550 6e 4c 6f 6f 70 5b 5d 20 2a 2f 0a 20 20 20 20 20 nLoop[] */.
67560 20 73 74 72 75 63 74 20 49 6e 4c 6f 6f 70 20 7b struct InLoop {
67570 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 43 75 . int iCu
67580 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 r;
67590 2f 2a 20 54 68 65 20 56 44 42 45 20 63 75 72 73 /* The VDBE curs
675a0 6f 72 20 75 73 65 64 20 62 79 20 74 68 69 73 20 or used by this
675b0 49 4e 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 20 IN operator */.
675c0 20 20 20 20 20 20 20 69 6e 74 20 61 64 64 72 49 int addrI
675d0 6e 54 6f 70 3b 20 20 20 20 20 20 20 20 20 2f 2a nTop; /*
675e0 20 54 6f 70 20 6f 66 20 74 68 65 20 49 4e 20 6c Top of the IN l
675f0 6f 6f 70 20 2a 2f 0a 20 20 20 20 20 20 7d 20 2a oop */. } *
67600 61 49 6e 4c 6f 6f 70 3b 20 20 20 20 20 20 20 20 aInLoop;
67610 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f /* Informatio
67620 6e 20 61 62 6f 75 74 20 65 61 63 68 20 6e 65 73 n about each nes
67630 74 65 64 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 ted IN operator
67640 2a 2f 0a 20 20 20 20 7d 20 69 6e 3b 20 20 20 20 */. } in;
67650 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
67660 55 73 65 64 20 77 68 65 6e 20 70 6c 61 6e 2e 77 Used when plan.w
67670 73 46 6c 61 67 73 26 57 48 45 52 45 5f 49 4e 5f sFlags&WHERE_IN_
67680 41 42 4c 45 20 2a 2f 0a 20 20 7d 20 75 3b 0a 0a ABLE */. } u;..
67690 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 /* The followi
676a0 6e 67 20 66 69 65 6c 64 20 69 73 20 72 65 61 6c ng field is real
676b0 6c 79 20 6e 6f 74 20 70 61 72 74 20 6f 66 20 74 ly not part of t
676c0 68 65 20 63 75 72 72 65 6e 74 20 6c 65 76 65 6c he current level
676d0 2e 20 20 42 75 74 0a 20 20 2a 2a 20 77 65 20 6e . But. ** we n
676e0 65 65 64 20 61 20 70 6c 61 63 65 20 74 6f 20 63 eed a place to c
676f0 61 63 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 ache virtual tab
67700 6c 65 20 69 6e 64 65 78 20 69 6e 66 6f 72 6d 61 le index informa
67710 74 69 6f 6e 20 66 6f 72 20 65 61 63 68 0a 20 20 tion for each.
67720 2a 2a 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 ** virtual table
67730 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 in the FROM cla
67740 75 73 65 20 61 6e 64 20 74 68 65 20 57 68 65 72 use and the Wher
67750 65 4c 65 76 65 6c 20 73 74 72 75 63 74 75 72 65 eLevel structure
67760 20 69 73 0a 20 20 2a 2a 20 61 20 63 6f 6e 76 65 is. ** a conve
67770 6e 69 65 6e 74 20 70 6c 61 63 65 20 73 69 6e 63 nient place sinc
67780 65 20 74 68 65 72 65 20 69 73 20 6f 6e 65 20 57 e there is one W
67790 68 65 72 65 4c 65 76 65 6c 20 66 6f 72 20 65 61 hereLevel for ea
677a0 63 68 20 46 52 4f 4d 20 63 6c 61 75 73 65 0a 20 ch FROM clause.
677b0 20 2a 2a 20 65 6c 65 6d 65 6e 74 2e 0a 20 20 2a ** element.. *
677c0 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 /. sqlite3_inde
677d0 78 5f 69 6e 66 6f 20 2a 70 49 64 78 49 6e 66 6f x_info *pIdxInfo
677e0 3b 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 66 6f ; /* Index info
677f0 20 66 6f 72 20 6e 2d 74 68 20 73 6f 75 72 63 65 for n-th source
67800 20 74 61 62 6c 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a table */.};../*
67810 0a 2a 2a 20 46 6c 61 67 73 20 61 70 70 72 6f 70 .** Flags approp
67820 72 69 61 74 65 20 66 6f 72 20 74 68 65 20 77 63 riate for the wc
67830 74 72 6c 46 6c 61 67 73 20 70 61 72 61 6d 65 74 trlFlags paramet
67840 65 72 20 6f 66 20 73 71 6c 69 74 65 33 57 68 65 er of sqlite3Whe
67850 72 65 42 65 67 69 6e 28 29 0a 2a 2a 20 61 6e 64 reBegin().** and
67860 20 74 68 65 20 57 68 65 72 65 49 6e 66 6f 2e 77 the WhereInfo.w
67870 63 74 72 6c 46 6c 61 67 73 20 6d 65 6d 62 65 72 ctrlFlags member
67880 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 ..*/.#define WHE
67890 52 45 5f 4f 52 44 45 52 42 59 5f 4e 4f 52 4d 41 RE_ORDERBY_NORMA
678a0 4c 20 20 20 30 78 30 30 30 30 20 2f 2a 20 4e 6f L 0x0000 /* No
678b0 2d 6f 70 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 -op */.#define W
678c0 48 45 52 45 5f 4f 52 44 45 52 42 59 5f 4d 49 4e HERE_ORDERBY_MIN
678d0 20 20 20 20 20 20 30 78 30 30 30 31 20 2f 2a 20 0x0001 /*
678e0 4f 52 44 45 52 20 42 59 20 70 72 6f 63 65 73 73 ORDER BY process
678f0 69 6e 67 20 66 6f 72 20 6d 69 6e 28 29 20 66 75 ing for min() fu
67900 6e 63 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 nc */.#define WH
67910 45 52 45 5f 4f 52 44 45 52 42 59 5f 4d 41 58 20 ERE_ORDERBY_MAX
67920 20 20 20 20 20 30 78 30 30 30 32 20 2f 2a 20 4f 0x0002 /* O
67930 52 44 45 52 20 42 59 20 70 72 6f 63 65 73 73 69 RDER BY processi
67940 6e 67 20 66 6f 72 20 6d 61 78 28 29 20 66 75 6e ng for max() fun
67950 63 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 c */.#define WHE
67960 52 45 5f 4f 4e 45 50 41 53 53 5f 44 45 53 49 52 RE_ONEPASS_DESIR
67970 45 44 20 20 30 78 30 30 30 34 20 2f 2a 20 57 61 ED 0x0004 /* Wa
67980 6e 74 20 74 6f 20 64 6f 20 6f 6e 65 2d 70 61 73 nt to do one-pas
67990 73 20 55 50 44 41 54 45 2f 44 45 4c 45 54 45 20 s UPDATE/DELETE
679a0 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 */.#define WHERE
679b0 5f 44 55 50 4c 49 43 41 54 45 53 5f 4f 4b 20 20 _DUPLICATES_OK
679c0 20 20 30 78 30 30 30 38 20 2f 2a 20 4f 6b 20 74 0x0008 /* Ok t
679d0 6f 20 72 65 74 75 72 6e 20 61 20 72 6f 77 20 6d o return a row m
679e0 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65 20 2a 2f ore than once */
679f0 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f 4f .#define WHERE_O
67a00 4d 49 54 5f 4f 50 45 4e 20 20 20 20 20 20 20 20 MIT_OPEN
67a10 30 78 30 30 31 30 20 2f 2a 20 54 61 62 6c 65 20 0x0010 /* Table
67a20 63 75 72 73 6f 72 20 61 72 65 20 61 6c 72 65 61 cursor are alrea
67a30 64 79 20 6f 70 65 6e 20 2a 2f 0a 23 64 65 66 69 dy open */.#defi
67a40 6e 65 20 57 48 45 52 45 5f 4f 4d 49 54 5f 43 4c ne WHERE_OMIT_CL
67a50 4f 53 45 20 20 20 20 20 20 20 30 78 30 30 32 30 OSE 0x0020
67a60 20 2f 2a 20 4f 6d 69 74 20 63 6c 6f 73 65 20 6f /* Omit close o
67a70 66 20 74 61 62 6c 65 20 26 20 69 6e 64 65 78 20 f table & index
67a80 63 75 72 73 6f 72 73 20 2a 2f 0a 23 64 65 66 69 cursors */.#defi
67a90 6e 65 20 57 48 45 52 45 5f 46 4f 52 43 45 5f 54 ne WHERE_FORCE_T
67aa0 41 42 4c 45 20 20 20 20 20 20 30 78 30 30 34 30 ABLE 0x0040
67ab0 20 2f 2a 20 44 6f 20 6e 6f 74 20 75 73 65 20 61 /* Do not use a
67ac0 6e 20 69 6e 64 65 78 2d 6f 6e 6c 79 20 73 65 61 n index-only sea
67ad0 72 63 68 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 rch */../*.** Th
67ae0 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 70 e WHERE clause p
67af0 72 6f 63 65 73 73 69 6e 67 20 72 6f 75 74 69 6e rocessing routin
67b00 65 20 68 61 73 20 74 77 6f 20 68 61 6c 76 65 73 e has two halves
67b10 2e 20 20 54 68 65 0a 2a 2a 20 66 69 72 73 74 20 . The.** first
67b20 70 61 72 74 20 64 6f 65 73 20 74 68 65 20 73 74 part does the st
67b30 61 72 74 20 6f 66 20 74 68 65 20 57 48 45 52 45 art of the WHERE
67b40 20 6c 6f 6f 70 20 61 6e 64 20 74 68 65 20 73 65 loop and the se
67b50 63 6f 6e 64 0a 2a 2a 20 68 61 6c 66 20 64 6f 65 cond.** half doe
67b60 73 20 74 68 65 20 74 61 69 6c 20 6f 66 20 74 68 s the tail of th
67b70 65 20 57 48 45 52 45 20 6c 6f 6f 70 2e 20 20 41 e WHERE loop. A
67b80 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 0a 2a 2a n instance of.**
67b90 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 this structure
67ba0 69 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 is returned by t
67bb0 68 65 20 66 69 72 73 74 20 68 61 6c 66 20 61 6e he first half an
67bc0 64 20 70 61 73 73 65 64 0a 2a 2a 20 69 6e 74 6f d passed.** into
67bd0 20 74 68 65 20 73 65 63 6f 6e 64 20 68 61 6c 66 the second half
67be0 20 74 6f 20 67 69 76 65 20 73 6f 6d 65 20 63 6f to give some co
67bf0 6e 74 69 6e 75 69 74 79 2e 0a 2a 2f 0a 73 74 72 ntinuity..*/.str
67c00 75 63 74 20 57 68 65 72 65 49 6e 66 6f 20 7b 0a uct WhereInfo {.
67c10 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 3b Parse *pParse;
67c20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e /* Parsin
67c30 67 20 61 6e 64 20 63 6f 64 65 20 67 65 6e 65 72 g and code gener
67c40 61 74 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f ating context */
67c50 0a 20 20 75 31 36 20 77 63 74 72 6c 46 6c 61 67 . u16 wctrlFlag
67c60 73 3b 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 s; /* Flags
67c70 20 6f 72 69 67 69 6e 61 6c 6c 79 20 70 61 73 73 originally pass
67c80 65 64 20 74 6f 20 73 71 6c 69 74 65 33 57 68 65 ed to sqlite3Whe
67c90 72 65 42 65 67 69 6e 28 29 20 2a 2f 0a 20 20 75 reBegin() */. u
67ca0 38 20 6f 6b 4f 6e 65 50 61 73 73 3b 20 20 20 20 8 okOnePass;
67cb0 20 20 20 20 2f 2a 20 4f 6b 20 74 6f 20 75 73 65 /* Ok to use
67cc0 20 6f 6e 65 2d 70 61 73 73 20 61 6c 67 6f 72 69 one-pass algori
67cd0 74 68 6d 20 66 6f 72 20 55 50 44 41 54 45 20 6f thm for UPDATE o
67ce0 72 20 44 45 4c 45 54 45 20 2a 2f 0a 20 20 53 72 r DELETE */. Sr
67cf0 63 4c 69 73 74 20 2a 70 54 61 62 4c 69 73 74 3b cList *pTabList;
67d00 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
67d10 4c 69 73 74 20 6f 66 20 74 61 62 6c 65 73 20 69 List of tables i
67d20 6e 20 74 68 65 20 6a 6f 69 6e 20 2a 2f 0a 20 20 n the join */.
67d30 69 6e 74 20 69 54 6f 70 3b 20 20 20 20 20 20 20 int iTop;
67d40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
67d50 2a 20 54 68 65 20 76 65 72 79 20 62 65 67 69 6e * The very begin
67d60 6e 69 6e 67 20 6f 66 20 74 68 65 20 57 48 45 52 ning of the WHER
67d70 45 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 E loop */. int
67d80 69 43 6f 6e 74 69 6e 75 65 3b 20 20 20 20 20 20 iContinue;
67d90 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4a 75 /* Ju
67da0 6d 70 20 68 65 72 65 20 74 6f 20 63 6f 6e 74 69 mp here to conti
67db0 6e 75 65 20 77 69 74 68 20 6e 65 78 74 20 72 65 nue with next re
67dc0 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 69 42 cord */. int iB
67dd0 72 65 61 6b 3b 20 20 20 20 20 20 20 20 20 20 20 reak;
67de0 20 20 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 /* Jump
67df0 20 68 65 72 65 20 74 6f 20 62 72 65 61 6b 20 6f here to break o
67e00 75 74 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 2a ut of the loop *
67e10 2f 0a 20 20 69 6e 74 20 6e 4c 65 76 65 6c 3b 20 /. int nLevel;
67e20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
67e30 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
67e40 6e 65 73 74 65 64 20 6c 6f 6f 70 20 2a 2f 0a 20 nested loop */.
67e50 20 73 74 72 75 63 74 20 57 68 65 72 65 43 6c 61 struct WhereCla
67e60 75 73 65 20 2a 70 57 43 3b 20 20 20 20 20 20 20 use *pWC;
67e70 2f 2a 20 44 65 63 6f 6d 70 6f 73 69 74 69 6f 6e /* Decomposition
67e80 20 6f 66 20 74 68 65 20 57 48 45 52 45 20 63 6c of the WHERE cl
67e90 61 75 73 65 20 2a 2f 0a 20 20 57 68 65 72 65 4c ause */. WhereL
67ea0 65 76 65 6c 20 61 5b 31 5d 3b 20 20 20 20 20 20 evel a[1];
67eb0 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f /* Info
67ec0 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 65 61 rmation about ea
67ed0 63 68 20 6e 65 73 74 20 6c 6f 6f 70 20 69 6e 20 ch nest loop in
67ee0 57 48 45 52 45 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a WHERE */.};../*.
67ef0 2a 2a 20 41 20 4e 61 6d 65 43 6f 6e 74 65 78 74 ** A NameContext
67f00 20 64 65 66 69 6e 65 73 20 61 20 63 6f 6e 74 65 defines a conte
67f10 78 74 20 69 6e 20 77 68 69 63 68 20 74 6f 20 72 xt in which to r
67f20 65 73 6f 6c 76 65 20 74 61 62 6c 65 20 61 6e 64 esolve table and
67f30 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 6e 61 6d 65 73 column.** names
67f40 2e 20 20 54 68 65 20 63 6f 6e 74 65 78 74 20 63 . The context c
67f50 6f 6e 73 69 73 74 73 20 6f 66 20 61 20 6c 69 73 onsists of a lis
67f60 74 20 6f 66 20 74 61 62 6c 65 73 20 28 74 68 65 t of tables (the
67f70 20 70 53 72 63 4c 69 73 74 29 20 66 69 65 6c 64 pSrcList) field
67f80 20 61 6e 64 0a 2a 2a 20 61 20 6c 69 73 74 20 6f and.** a list o
67f90 66 20 6e 61 6d 65 64 20 65 78 70 72 65 73 73 69 f named expressi
67fa0 6f 6e 20 28 70 45 4c 69 73 74 29 2e 20 20 54 68 on (pEList). Th
67fb0 65 20 6e 61 6d 65 64 20 65 78 70 72 65 73 73 69 e named expressi
67fc0 6f 6e 20 6c 69 73 74 20 6d 61 79 0a 2a 2a 20 62 on list may.** b
67fd0 65 20 4e 55 4c 4c 2e 20 20 54 68 65 20 70 53 72 e NULL. The pSr
67fe0 63 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f c corresponds to
67ff0 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 the FROM clause
68000 20 6f 66 20 61 20 53 45 4c 45 43 54 20 6f 72 0a of a SELECT or.
68010 2a 2a 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20 ** to the table
68020 62 65 69 6e 67 20 6f 70 65 72 61 74 65 64 20 6f being operated o
68030 6e 20 62 79 20 49 4e 53 45 52 54 2c 20 55 50 44 n by INSERT, UPD
68040 41 54 45 2c 20 6f 72 20 44 45 4c 45 54 45 2e 20 ATE, or DELETE.
68050 20 54 68 65 0a 2a 2a 20 70 45 4c 69 73 74 20 63 The.** pEList c
68060 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 74 68 orresponds to th
68070 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20 e result set of
68080 61 20 53 45 4c 45 43 54 20 61 6e 64 20 69 73 20 a SELECT and is
68090 4e 55 4c 4c 20 66 6f 72 0a 2a 2a 20 6f 74 68 65 NULL for.** othe
680a0 72 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a r statements..**
680b0 0a 2a 2a 20 4e 61 6d 65 43 6f 6e 74 65 78 74 73 .** NameContexts
680c0 20 63 61 6e 20 62 65 20 6e 65 73 74 65 64 2e 20 can be nested.
680d0 20 57 68 65 6e 20 72 65 73 6f 6c 76 69 6e 67 20 When resolving
680e0 6e 61 6d 65 73 2c 20 74 68 65 20 69 6e 6e 65 72 names, the inner
680f0 2d 6d 6f 73 74 20 0a 2a 2a 20 63 6f 6e 74 65 78 -most .** contex
68100 74 20 69 73 20 73 65 61 72 63 68 65 64 20 66 69 t is searched fi
68110 72 73 74 2e 20 20 49 66 20 6e 6f 20 6d 61 74 63 rst. If no matc
68120 68 20 69 73 20 66 6f 75 6e 64 2c 20 74 68 65 20 h is found, the
68130 6e 65 78 74 20 6f 75 74 65 72 0a 2a 2a 20 63 6f next outer.** co
68140 6e 74 65 78 74 20 69 73 20 63 68 65 63 6b 65 64 ntext is checked
68150 2e 20 20 49 66 20 74 68 65 72 65 20 69 73 20 73 . If there is s
68160 74 69 6c 6c 20 6e 6f 20 6d 61 74 63 68 2c 20 74 till no match, t
68170 68 65 20 6e 65 78 74 20 63 6f 6e 74 65 78 74 0a he next context.
68180 2a 2a 20 69 73 20 63 68 65 63 6b 65 64 2e 20 20 ** is checked.
68190 54 68 69 73 20 70 72 6f 63 65 73 73 20 63 6f 6e This process con
681a0 74 69 6e 75 65 73 20 75 6e 74 69 6c 20 65 69 74 tinues until eit
681b0 68 65 72 20 61 20 6d 61 74 63 68 20 69 73 20 66 her a match is f
681c0 6f 75 6e 64 0a 2a 2a 20 6f 72 20 61 6c 6c 20 63 ound.** or all c
681d0 6f 6e 74 65 78 74 73 20 61 72 65 20 63 68 65 63 ontexts are chec
681e0 6b 2e 20 20 57 68 65 6e 20 61 20 6d 61 74 63 68 k. When a match
681f0 20 69 73 20 66 6f 75 6e 64 2c 20 74 68 65 20 6e is found, the n
68200 52 65 66 20 6d 65 6d 62 65 72 20 6f 66 0a 2a 2a Ref member of.**
68210 20 74 68 65 20 63 6f 6e 74 65 78 74 20 63 6f 6e the context con
68220 74 61 69 6e 69 6e 67 20 74 68 65 20 6d 61 74 63 taining the matc
68230 68 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 h is incremented
68240 2e 20 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 73 75 . .**.** Each su
68250 62 71 75 65 72 79 20 67 65 74 73 20 61 20 6e 65 bquery gets a ne
68260 77 20 4e 61 6d 65 43 6f 6e 74 65 78 74 2e 20 20 w NameContext.
68270 54 68 65 20 70 4e 65 78 74 20 66 69 65 6c 64 20 The pNext field
68280 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 0a 2a 2a points to the.**
68290 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 69 6e 20 NameContext in
682a0 74 68 65 20 70 61 72 65 6e 74 20 71 75 65 72 79 the parent query
682b0 2e 20 20 54 68 75 73 20 74 68 65 20 70 72 6f 63 . Thus the proc
682c0 65 73 73 20 6f 66 20 73 63 61 6e 6e 69 6e 67 20 ess of scanning
682d0 74 68 65 0a 2a 2a 20 4e 61 6d 65 43 6f 6e 74 65 the.** NameConte
682e0 78 74 20 6c 69 73 74 20 63 6f 72 72 65 73 70 6f xt list correspo
682f0 6e 64 73 20 74 6f 20 73 65 61 72 63 68 69 6e 67 nds to searching
68300 20 74 68 72 6f 75 67 68 20 73 75 63 63 65 73 73 through success
68310 69 76 65 6c 79 20 6f 75 74 65 72 0a 2a 2a 20 73 ively outer.** s
68320 75 62 71 75 65 72 69 65 73 20 6c 6f 6f 6b 69 6e ubqueries lookin
68330 67 20 66 6f 72 20 61 20 6d 61 74 63 68 2e 0a 2a g for a match..*
68340 2f 0a 73 74 72 75 63 74 20 4e 61 6d 65 43 6f 6e /.struct NameCon
68350 74 65 78 74 20 7b 0a 20 20 50 61 72 73 65 20 2a text {. Parse *
68360 70 50 61 72 73 65 3b 20 20 20 20 20 20 20 2f 2a pParse; /*
68370 20 54 68 65 20 70 61 72 73 65 72 20 2a 2f 0a 20 The parser */.
68380 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 4c 69 SrcList *pSrcLi
68390 73 74 3b 20 20 20 2f 2a 20 4f 6e 65 20 6f 72 20 st; /* One or
683a0 6d 6f 72 65 20 74 61 62 6c 65 73 20 75 73 65 64 more tables used
683b0 20 74 6f 20 72 65 73 6f 6c 76 65 20 6e 61 6d 65 to resolve name
683c0 73 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 s */. ExprList
683d0 2a 70 45 4c 69 73 74 3b 20 20 20 20 2f 2a 20 4f *pEList; /* O
683e0 70 74 69 6f 6e 61 6c 20 6c 69 73 74 20 6f 66 20 ptional list of
683f0 6e 61 6d 65 64 20 65 78 70 72 65 73 73 69 6f 6e named expression
68400 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66 3b s */. int nRef;
68410 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
68420 75 6d 62 65 72 20 6f 66 20 6e 61 6d 65 73 20 72 umber of names r
68430 65 73 6f 6c 76 65 64 20 62 79 20 74 68 69 73 20 esolved by this
68440 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 context */. int
68450 20 6e 45 72 72 3b 20 20 20 20 20 20 20 20 20 20 nErr;
68460 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 /* Number of e
68470 72 72 6f 72 73 20 65 6e 63 6f 75 6e 74 65 72 65 rrors encountere
68480 64 20 77 68 69 6c 65 20 72 65 73 6f 6c 76 69 6e d while resolvin
68490 67 20 6e 61 6d 65 73 20 2a 2f 0a 20 20 75 38 20 g names */. u8
684a0 61 6c 6c 6f 77 41 67 67 3b 20 20 20 20 20 20 20 allowAgg;
684b0 20 20 2f 2a 20 41 67 67 72 65 67 61 74 65 20 66 /* Aggregate f
684c0 75 6e 63 74 69 6f 6e 73 20 61 6c 6c 6f 77 65 64 unctions allowed
684d0 20 68 65 72 65 20 2a 2f 0a 20 20 75 38 20 68 61 here */. u8 ha
684e0 73 41 67 67 3b 20 20 20 20 20 20 20 20 20 20 20 sAgg;
684f0 2f 2a 20 54 72 75 65 20 69 66 20 61 67 67 72 65 /* True if aggre
68500 67 61 74 65 73 20 61 72 65 20 73 65 65 6e 20 2a gates are seen *
68510 2f 0a 20 20 75 38 20 69 73 43 68 65 63 6b 3b 20 /. u8 isCheck;
68520 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
68530 20 69 66 20 72 65 73 6f 6c 76 69 6e 67 20 6e 61 if resolving na
68540 6d 65 73 20 69 6e 20 61 20 43 48 45 43 4b 20 63 mes in a CHECK c
68550 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20 20 69 onstraint */. i
68560 6e 74 20 6e 44 65 70 74 68 3b 20 20 20 20 20 20 nt nDepth;
68570 20 20 20 20 2f 2a 20 44 65 70 74 68 20 6f 66 20 /* Depth of
68580 73 75 62 71 75 65 72 79 20 72 65 63 75 72 73 69 subquery recursi
68590 6f 6e 2e 20 31 20 66 6f 72 20 6e 6f 20 72 65 63 on. 1 for no rec
685a0 75 72 73 69 6f 6e 20 2a 2f 0a 20 20 41 67 67 49 ursion */. AggI
685b0 6e 66 6f 20 2a 70 41 67 67 49 6e 66 6f 3b 20 20 nfo *pAggInfo;
685c0 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 /* Information
685d0 61 62 6f 75 74 20 61 67 67 72 65 67 61 74 65 73 about aggregates
685e0 20 61 74 20 74 68 69 73 20 6c 65 76 65 6c 20 2a at this level *
685f0 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 /. NameContext
68600 2a 70 4e 65 78 74 3b 20 20 2f 2a 20 4e 65 78 74 *pNext; /* Next
68610 20 6f 75 74 65 72 20 6e 61 6d 65 20 63 6f 6e 74 outer name cont
68620 65 78 74 2e 20 20 4e 55 4c 4c 20 66 6f 72 20 6f ext. NULL for o
68630 75 74 65 72 6d 6f 73 74 20 2a 2f 0a 7d 3b 0a 0a utermost */.};..
68640 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 /*.** An instanc
68650 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 e of the followi
68660 6e 67 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e ng structure con
68670 74 61 69 6e 73 20 61 6c 6c 20 69 6e 66 6f 72 6d tains all inform
68680 61 74 69 6f 6e 0a 2a 2a 20 6e 65 65 64 65 64 20 ation.** needed
68690 74 6f 20 67 65 6e 65 72 61 74 65 20 63 6f 64 65 to generate code
686a0 20 66 6f 72 20 61 20 73 69 6e 67 6c 65 20 53 45 for a single SE
686b0 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a LECT statement..
686c0 2a 2a 0a 2a 2a 20 6e 4c 69 6d 69 74 20 69 73 20 **.** nLimit is
686d0 73 65 74 20 74 6f 20 2d 31 20 69 66 20 74 68 65 set to -1 if the
686e0 72 65 20 69 73 20 6e 6f 20 4c 49 4d 49 54 20 63 re is no LIMIT c
686f0 6c 61 75 73 65 2e 20 20 6e 4f 66 66 73 65 74 20 lause. nOffset
68700 69 73 20 73 65 74 20 74 6f 20 30 2e 0a 2a 2a 20 is set to 0..**
68710 49 66 20 74 68 65 72 65 20 69 73 20 61 20 4c 49 If there is a LI
68720 4d 49 54 20 63 6c 61 75 73 65 2c 20 74 68 65 20 MIT clause, the
68730 70 61 72 73 65 72 20 73 65 74 73 20 6e 4c 69 6d parser sets nLim
68740 69 74 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 it to the value
68750 6f 66 20 74 68 65 0a 2a 2a 20 6c 69 6d 69 74 20 of the.** limit
68760 61 6e 64 20 6e 4f 66 66 73 65 74 20 74 6f 20 74 and nOffset to t
68770 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 he value of the
68780 6f 66 66 73 65 74 20 28 6f 72 20 30 20 69 66 20 offset (or 0 if
68790 74 68 65 72 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 there is not.**
687a0 6f 66 66 73 65 74 29 2e 20 20 42 75 74 20 6c 61 offset). But la
687b0 74 65 72 20 6f 6e 2c 20 6e 4c 69 6d 69 74 20 61 ter on, nLimit a
687c0 6e 64 20 6e 4f 66 66 73 65 74 20 62 65 63 6f 6d nd nOffset becom
687d0 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 6c 6f 63 e the memory loc
687e0 61 74 69 6f 6e 73 0a 2a 2a 20 69 6e 20 74 68 65 ations.** in the
687f0 20 56 44 42 45 20 74 68 61 74 20 72 65 63 6f 72 VDBE that recor
68800 64 20 74 68 65 20 6c 69 6d 69 74 20 61 6e 64 20 d the limit and
68810 6f 66 66 73 65 74 20 63 6f 75 6e 74 65 72 73 2e offset counters.
68820 0a 2a 2a 0a 2a 2a 20 61 64 64 72 4f 70 65 6e 45 .**.** addrOpenE
68830 70 68 6d 5b 5d 20 65 6e 74 72 69 65 73 20 63 6f phm[] entries co
68840 6e 74 61 69 6e 20 74 68 65 20 61 64 64 72 65 73 ntain the addres
68850 73 20 6f 66 20 4f 50 5f 4f 70 65 6e 45 70 68 65 s of OP_OpenEphe
68860 6d 65 72 61 6c 20 6f 70 63 6f 64 65 73 2e 0a 2a meral opcodes..*
68870 2a 20 54 68 65 73 65 20 61 64 64 72 65 73 73 65 * These addresse
68880 73 20 6d 75 73 74 20 62 65 20 73 74 6f 72 65 64 s must be stored
68890 20 73 6f 20 74 68 61 74 20 77 65 20 63 61 6e 20 so that we can
688a0 67 6f 20 62 61 63 6b 20 61 6e 64 20 66 69 6c 6c go back and fill
688b0 20 69 6e 0a 2a 2a 20 74 68 65 20 50 34 5f 4b 45 in.** the P4_KE
688c0 59 49 4e 46 4f 20 61 6e 64 20 50 32 20 70 61 72 YINFO and P2 par
688d0 61 6d 65 74 65 72 73 20 6c 61 74 65 72 2e 20 20 ameters later.
688e0 4e 65 69 74 68 65 72 20 74 68 65 20 4b 65 79 49 Neither the KeyI
688f0 6e 66 6f 20 6e 6f 72 0a 2a 2a 20 74 68 65 20 6e nfo nor.** the n
68900 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 umber of columns
68910 20 69 6e 20 50 32 20 63 61 6e 20 62 65 20 63 6f in P2 can be co
68920 6d 70 75 74 65 64 20 61 74 20 74 68 65 20 73 61 mputed at the sa
68930 6d 65 20 74 69 6d 65 0a 2a 2a 20 61 73 20 74 68 me time.** as th
68940 65 20 4f 50 5f 4f 70 65 6e 45 70 68 6d 20 69 6e e OP_OpenEphm in
68950 73 74 72 75 63 74 69 6f 6e 20 69 73 20 63 6f 64 struction is cod
68960 65 64 20 62 65 63 61 75 73 65 20 6e 6f 74 0a 2a ed because not.*
68970 2a 20 65 6e 6f 75 67 68 20 69 6e 66 6f 72 6d 61 * enough informa
68980 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 63 tion about the c
68990 6f 6d 70 6f 75 6e 64 20 71 75 65 72 79 20 69 73 ompound query is
689a0 20 6b 6e 6f 77 6e 20 61 74 20 74 68 61 74 20 70 known at that p
689b0 6f 69 6e 74 2e 0a 2a 2a 20 54 68 65 20 4b 65 79 oint..** The Key
689c0 49 6e 66 6f 20 66 6f 72 20 61 64 64 72 4f 70 65 Info for addrOpe
689d0 6e 54 72 61 6e 5b 30 5d 20 61 6e 64 20 5b 31 5d nTran[0] and [1]
689e0 20 63 6f 6e 74 61 69 6e 73 20 63 6f 6c 6c 61 74 contains collat
689f0 69 6e 67 20 73 65 71 75 65 6e 63 65 73 0a 2a 2a ing sequences.**
68a00 20 66 6f 72 20 74 68 65 20 72 65 73 75 6c 74 20 for the result
68a10 73 65 74 2e 20 20 54 68 65 20 4b 65 79 49 6e 66 set. The KeyInf
68a20 6f 20 66 6f 72 20 61 64 64 72 4f 70 65 6e 54 72 o for addrOpenTr
68a30 61 6e 5b 32 5d 20 63 6f 6e 74 61 69 6e 73 20 63 an[2] contains c
68a40 6f 6c 6c 61 74 69 6e 67 0a 2a 2a 20 73 65 71 75 ollating.** sequ
68a50 65 6e 63 65 73 20 66 6f 72 20 74 68 65 20 4f 52 ences for the OR
68a60 44 45 52 20 42 59 20 63 6c 61 75 73 65 2e 0a 2a DER BY clause..*
68a70 2f 0a 73 74 72 75 63 74 20 53 65 6c 65 63 74 20 /.struct Select
68a80 7b 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 {. ExprList *pE
68a90 4c 69 73 74 3b 20 20 20 20 20 20 2f 2a 20 54 68 List; /* Th
68aa0 65 20 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20 e fields of the
68ab0 72 65 73 75 6c 74 20 2a 2f 0a 20 20 75 38 20 6f result */. u8 o
68ac0 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 p;
68ad0 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 3a 20 54 4b /* One of: TK
68ae0 5f 55 4e 49 4f 4e 20 54 4b 5f 41 4c 4c 20 54 4b _UNION TK_ALL TK
68af0 5f 49 4e 54 45 52 53 45 43 54 20 54 4b 5f 45 58 _INTERSECT TK_EX
68b00 43 45 50 54 20 2a 2f 0a 20 20 63 68 61 72 20 61 CEPT */. char a
68b10 66 66 69 6e 69 74 79 3b 20 20 20 20 20 20 20 20 ffinity;
68b20 20 2f 2a 20 4d 61 6b 65 52 65 63 6f 72 64 20 77 /* MakeRecord w
68b30 69 74 68 20 74 68 69 73 20 61 66 66 69 6e 69 74 ith this affinit
68b40 79 20 66 6f 72 20 53 52 54 5f 53 65 74 20 2a 2f y for SRT_Set */
68b50 0a 20 20 75 31 36 20 73 65 6c 46 6c 61 67 73 3b . u16 selFlags;
68b60 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 72 /* Var
68b70 69 6f 75 73 20 53 46 5f 2a 20 76 61 6c 75 65 73 ious SF_* values
68b80 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 */. SrcList *p
68b90 53 72 63 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 Src; /*
68ba0 54 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 The FROM clause
68bb0 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 65 72 */. Expr *pWher
68bc0 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 e; /* T
68bd0 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 he WHERE clause
68be0 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 */. ExprList *p
68bf0 47 72 6f 75 70 42 79 3b 20 20 20 20 2f 2a 20 54 GroupBy; /* T
68c00 68 65 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 he GROUP BY clau
68c10 73 65 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 48 se */. Expr *pH
68c20 61 76 69 6e 67 3b 20 20 20 20 20 20 20 20 20 2f aving; /
68c30 2a 20 54 68 65 20 48 41 56 49 4e 47 20 63 6c 61 * The HAVING cla
68c40 75 73 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 use */. ExprLis
68c50 74 20 2a 70 4f 72 64 65 72 42 79 3b 20 20 20 20 t *pOrderBy;
68c60 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42 59 20 /* The ORDER BY
68c70 63 6c 61 75 73 65 20 2a 2f 0a 20 20 53 65 6c 65 clause */. Sele
68c80 63 74 20 2a 70 50 72 69 6f 72 3b 20 20 20 20 20 ct *pPrior;
68c90 20 20 20 2f 2a 20 50 72 69 6f 72 20 73 65 6c 65 /* Prior sele
68ca0 63 74 20 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 ct in a compound
68cb0 20 73 65 6c 65 63 74 20 73 74 61 74 65 6d 65 6e select statemen
68cc0 74 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 t */. Select *p
68cd0 4e 65 78 74 3b 20 20 20 20 20 20 20 20 20 2f 2a Next; /*
68ce0 20 4e 65 78 74 20 73 65 6c 65 63 74 20 74 6f 20 Next select to
68cf0 74 68 65 20 6c 65 66 74 20 69 6e 20 61 20 63 6f the left in a co
68d00 6d 70 6f 75 6e 64 20 2a 2f 0a 20 20 53 65 6c 65 mpound */. Sele
68d10 63 74 20 2a 70 52 69 67 68 74 6d 6f 73 74 3b 20 ct *pRightmost;
68d20 20 20 20 2f 2a 20 52 69 67 68 74 2d 6d 6f 73 74 /* Right-most
68d30 20 73 65 6c 65 63 74 20 69 6e 20 61 20 63 6f 6d select in a com
68d40 70 6f 75 6e 64 20 73 65 6c 65 63 74 20 73 74 61 pound select sta
68d50 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 45 78 70 72 tement */. Expr
68d60 20 2a 70 4c 69 6d 69 74 3b 20 20 20 20 20 20 20 *pLimit;
68d70 20 20 20 2f 2a 20 4c 49 4d 49 54 20 65 78 70 72 /* LIMIT expr
68d80 65 73 73 69 6f 6e 2e 20 4e 55 4c 4c 20 6d 65 61 ession. NULL mea
68d90 6e 73 20 6e 6f 74 20 75 73 65 64 2e 20 2a 2f 0a ns not used. */.
68da0 20 20 45 78 70 72 20 2a 70 4f 66 66 73 65 74 3b Expr *pOffset;
68db0 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 46 46 53 /* OFFS
68dc0 45 54 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 4e ET expression. N
68dd0 55 4c 4c 20 6d 65 61 6e 73 20 6e 6f 74 20 75 73 ULL means not us
68de0 65 64 2e 20 2a 2f 0a 20 20 69 6e 74 20 69 4c 69 ed. */. int iLi
68df0 6d 69 74 2c 20 69 4f 66 66 73 65 74 3b 20 20 20 mit, iOffset;
68e00 2f 2a 20 4d 65 6d 6f 72 79 20 72 65 67 69 73 74 /* Memory regist
68e10 65 72 73 20 68 6f 6c 64 69 6e 67 20 4c 49 4d 49 ers holding LIMI
68e20 54 20 26 20 4f 46 46 53 45 54 20 63 6f 75 6e 74 T & OFFSET count
68e30 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 ers */. int add
68e40 72 4f 70 65 6e 45 70 68 6d 5b 33 5d 3b 20 20 20 rOpenEphm[3];
68e50 2f 2a 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 20 /* OP_OpenEphem
68e60 6f 70 63 6f 64 65 73 20 72 65 6c 61 74 65 64 20 opcodes related
68e70 74 6f 20 74 68 69 73 20 73 65 6c 65 63 74 20 2a to this select *
68e80 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f /.};../*.** Allo
68e90 77 65 64 20 76 61 6c 75 65 73 20 66 6f 72 20 53 wed values for S
68ea0 65 6c 65 63 74 2e 73 65 6c 46 6c 61 67 73 2e 20 elect.selFlags.
68eb0 20 54 68 65 20 22 53 46 22 20 70 72 65 66 69 78 The "SF" prefix
68ec0 20 73 74 61 6e 64 73 20 66 6f 72 0a 2a 2a 20 22 stands for.** "
68ed0 53 65 6c 65 63 74 20 46 6c 61 67 22 2e 0a 2a 2f Select Flag"..*/
68ee0 0a 23 64 65 66 69 6e 65 20 53 46 5f 44 69 73 74 .#define SF_Dist
68ef0 69 6e 63 74 20 20 20 20 20 20 20 20 30 78 30 30 inct 0x00
68f00 30 31 20 20 2f 2a 20 4f 75 74 70 75 74 20 73 68 01 /* Output sh
68f10 6f 75 6c 64 20 62 65 20 44 49 53 54 49 4e 43 54 ould be DISTINCT
68f20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 46 5f 52 */.#define SF_R
68f30 65 73 6f 6c 76 65 64 20 20 20 20 20 20 20 20 30 esolved 0
68f40 78 30 30 30 32 20 20 2f 2a 20 49 64 65 6e 74 69 x0002 /* Identi
68f50 66 69 65 72 73 20 68 61 76 65 20 62 65 65 6e 20 fiers have been
68f60 72 65 73 6f 6c 76 65 64 20 2a 2f 0a 23 64 65 66 resolved */.#def
68f70 69 6e 65 20 53 46 5f 41 67 67 72 65 67 61 74 65 ine SF_Aggregate
68f80 20 20 20 20 20 20 20 30 78 30 30 30 34 20 20 2f 0x0004 /
68f90 2a 20 43 6f 6e 74 61 69 6e 73 20 61 67 67 72 65 * Contains aggre
68fa0 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 2a gate functions *
68fb0 2f 0a 23 64 65 66 69 6e 65 20 53 46 5f 55 73 65 /.#define SF_Use
68fc0 73 45 70 68 65 6d 65 72 61 6c 20 20 20 30 78 30 sEphemeral 0x0
68fd0 30 30 38 20 20 2f 2a 20 55 73 65 73 20 74 68 65 008 /* Uses the
68fe0 20 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 20 6f OpenEphemeral o
68ff0 70 63 6f 64 65 20 2a 2f 0a 23 64 65 66 69 6e 65 pcode */.#define
69000 20 53 46 5f 45 78 70 61 6e 64 65 64 20 20 20 20 SF_Expanded
69010 20 20 20 20 30 78 30 30 31 30 20 20 2f 2a 20 73 0x0010 /* s
69020 71 6c 69 74 65 33 53 65 6c 65 63 74 45 78 70 61 qlite3SelectExpa
69030 6e 64 28 29 20 63 61 6c 6c 65 64 20 6f 6e 20 74 nd() called on t
69040 68 69 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 his */.#define S
69050 46 5f 48 61 73 54 79 70 65 49 6e 66 6f 20 20 20 F_HasTypeInfo
69060 20 20 30 78 30 30 32 30 20 20 2f 2a 20 46 52 4f 0x0020 /* FRO
69070 4d 20 73 75 62 71 75 65 72 69 65 73 20 68 61 76 M subqueries hav
69080 65 20 54 61 62 6c 65 20 6d 65 74 61 64 61 74 61 e Table metadata
69090 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 */.../*.** The
690a0 72 65 73 75 6c 74 73 20 6f 66 20 61 20 73 65 6c results of a sel
690b0 65 63 74 20 63 61 6e 20 62 65 20 64 69 73 74 72 ect can be distr
690c0 69 62 75 74 65 64 20 69 6e 20 73 65 76 65 72 61 ibuted in severa
690d0 6c 20 77 61 79 73 2e 20 20 54 68 65 0a 2a 2a 20 l ways. The.**
690e0 22 53 52 54 22 20 70 72 65 66 69 78 20 6d 65 61 "SRT" prefix mea
690f0 6e 73 20 22 53 45 4c 45 43 54 20 52 65 73 75 6c ns "SELECT Resul
69100 74 20 54 79 70 65 22 2e 0a 2a 2f 0a 23 64 65 66 t Type"..*/.#def
69110 69 6e 65 20 53 52 54 5f 55 6e 69 6f 6e 20 20 20 ine SRT_Union
69120 20 20 20 20 20 31 20 20 2f 2a 20 53 74 6f 72 65 1 /* Store
69130 20 72 65 73 75 6c 74 20 61 73 20 6b 65 79 73 20 result as keys
69140 69 6e 20 61 6e 20 69 6e 64 65 78 20 2a 2f 0a 23 in an index */.#
69150 64 65 66 69 6e 65 20 53 52 54 5f 45 78 63 65 70 define SRT_Excep
69160 74 20 20 20 20 20 20 20 32 20 20 2f 2a 20 52 65 t 2 /* Re
69170 6d 6f 76 65 20 72 65 73 75 6c 74 20 66 72 6f 6d move result from
69180 20 61 20 55 4e 49 4f 4e 20 69 6e 64 65 78 20 2a a UNION index *
69190 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 45 78 /.#define SRT_Ex
691a0 69 73 74 73 20 20 20 20 20 20 20 33 20 20 2f 2a ists 3 /*
691b0 20 53 74 6f 72 65 20 31 20 69 66 20 74 68 65 20 Store 1 if the
691c0 72 65 73 75 6c 74 20 69 73 20 6e 6f 74 20 65 6d result is not em
691d0 70 74 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 pty */.#define S
691e0 52 54 5f 44 69 73 63 61 72 64 20 20 20 20 20 20 RT_Discard
691f0 34 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 73 61 76 4 /* Do not sav
69200 65 20 74 68 65 20 72 65 73 75 6c 74 73 20 61 6e e the results an
69210 79 77 68 65 72 65 20 2a 2f 0a 0a 2f 2a 20 54 68 ywhere */../* Th
69220 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 e ORDER BY claus
69230 65 20 69 73 20 69 67 6e 6f 72 65 64 20 66 6f 72 e is ignored for
69240 20 61 6c 6c 20 6f 66 20 74 68 65 20 61 62 6f 76 all of the abov
69250 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 49 67 6e e */.#define Ign
69260 6f 72 61 62 6c 65 4f 72 64 65 72 62 79 28 58 29 orableOrderby(X)
69270 20 28 28 58 2d 3e 65 44 65 73 74 29 3c 3d 53 52 ((X->eDest)<=SR
69280 54 5f 44 69 73 63 61 72 64 29 0a 0a 23 64 65 66 T_Discard)..#def
69290 69 6e 65 20 53 52 54 5f 4f 75 74 70 75 74 20 20 ine SRT_Output
692a0 20 20 20 20 20 35 20 20 2f 2a 20 4f 75 74 70 75 5 /* Outpu
692b0 74 20 65 61 63 68 20 72 6f 77 20 6f 66 20 72 65 t each row of re
692c0 73 75 6c 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 sult */.#define
692d0 53 52 54 5f 4d 65 6d 20 20 20 20 20 20 20 20 20 SRT_Mem
692e0 20 36 20 20 2f 2a 20 53 74 6f 72 65 20 72 65 73 6 /* Store res
692f0 75 6c 74 20 69 6e 20 61 20 6d 65 6d 6f 72 79 20 ult in a memory
69300 63 65 6c 6c 20 2a 2f 0a 23 64 65 66 69 6e 65 20 cell */.#define
69310 53 52 54 5f 53 65 74 20 20 20 20 20 20 20 20 20 SRT_Set
69320 20 37 20 20 2f 2a 20 53 74 6f 72 65 20 72 65 73 7 /* Store res
69330 75 6c 74 73 20 61 73 20 6b 65 79 73 20 69 6e 20 ults as keys in
69340 61 6e 20 69 6e 64 65 78 20 2a 2f 0a 23 64 65 66 an index */.#def
69350 69 6e 65 20 53 52 54 5f 54 61 62 6c 65 20 20 20 ine SRT_Table
69360 20 20 20 20 20 38 20 20 2f 2a 20 53 74 6f 72 65 8 /* Store
69370 20 72 65 73 75 6c 74 20 61 73 20 64 61 74 61 20 result as data
69380 77 69 74 68 20 61 6e 20 61 75 74 6f 6d 61 74 69 with an automati
69390 63 20 72 6f 77 69 64 20 2a 2f 0a 23 64 65 66 69 c rowid */.#defi
693a0 6e 65 20 53 52 54 5f 45 70 68 65 6d 54 61 62 20 ne SRT_EphemTab
693b0 20 20 20 20 39 20 20 2f 2a 20 43 72 65 61 74 65 9 /* Create
693c0 20 74 72 61 6e 73 69 65 6e 74 20 74 61 62 20 61 transient tab a
693d0 6e 64 20 73 74 6f 72 65 20 6c 69 6b 65 20 53 52 nd store like SR
693e0 54 5f 54 61 62 6c 65 20 2a 2f 0a 23 64 65 66 69 T_Table */.#defi
693f0 6e 65 20 53 52 54 5f 43 6f 72 6f 75 74 69 6e 65 ne SRT_Coroutine
69400 20 20 20 31 30 20 20 2f 2a 20 47 65 6e 65 72 61 10 /* Genera
69410 74 65 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 te a single row
69420 6f 66 20 72 65 73 75 6c 74 20 2a 2f 0a 0a 2f 2a of result */../*
69430 0a 2a 2a 20 41 20 73 74 72 75 63 74 75 72 65 20 .** A structure
69440 75 73 65 64 20 74 6f 20 63 75 73 74 6f 6d 69 7a used to customiz
69450 65 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 6f e the behavior o
69460 66 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 f sqlite3Select(
69470 29 2e 20 53 65 65 0a 2a 2a 20 63 6f 6d 6d 65 6e ). See.** commen
69480 74 73 20 61 62 6f 76 65 20 73 71 6c 69 74 65 33 ts above sqlite3
69490 53 65 6c 65 63 74 28 29 20 66 6f 72 20 64 65 74 Select() for det
694a0 61 69 6c 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 ails..*/.typedef
694b0 20 73 74 72 75 63 74 20 53 65 6c 65 63 74 44 65 struct SelectDe
694c0 73 74 20 53 65 6c 65 63 74 44 65 73 74 3b 0a 73 st SelectDest;.s
694d0 74 72 75 63 74 20 53 65 6c 65 63 74 44 65 73 74 truct SelectDest
694e0 20 7b 0a 20 20 75 38 20 65 44 65 73 74 3b 20 20 {. u8 eDest;
694f0 20 20 20 20 20 20 20 2f 2a 20 48 6f 77 20 74 6f /* How to
69500 20 64 69 73 70 6f 73 65 20 6f 66 20 74 68 65 20 dispose of the
69510 72 65 73 75 6c 74 73 20 2a 2f 0a 20 20 75 38 20 results */. u8
69520 61 66 66 69 6e 69 74 79 3b 20 20 20 20 20 20 2f affinity; /
69530 2a 20 41 66 66 69 6e 69 74 79 20 75 73 65 64 20 * Affinity used
69540 77 68 65 6e 20 65 44 65 73 74 3d 3d 53 52 54 5f when eDest==SRT_
69550 53 65 74 20 2a 2f 0a 20 20 69 6e 74 20 69 50 61 Set */. int iPa
69560 72 6d 3b 20 20 20 20 20 20 20 20 2f 2a 20 41 20 rm; /* A
69570 70 61 72 61 6d 65 74 65 72 20 75 73 65 64 20 62 parameter used b
69580 79 20 74 68 65 20 65 44 65 73 74 20 64 69 73 70 y the eDest disp
69590 6f 73 61 6c 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 osal method */.
695a0 20 69 6e 74 20 69 4d 65 6d 3b 20 20 20 20 20 20 int iMem;
695b0 20 20 20 2f 2a 20 42 61 73 65 20 72 65 67 69 73 /* Base regis
695c0 74 65 72 20 77 68 65 72 65 20 72 65 73 75 6c 74 ter where result
695d0 73 20 61 72 65 20 77 72 69 74 74 65 6e 20 2a 2f s are written */
695e0 0a 20 20 69 6e 74 20 6e 4d 65 6d 3b 20 20 20 20 . int nMem;
695f0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
69600 66 20 72 65 67 69 73 74 65 72 73 20 61 6c 6c 6f f registers allo
69610 63 61 74 65 64 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a cated */.};../*.
69620 2a 2a 20 44 75 72 69 6e 67 20 63 6f 64 65 20 67 ** During code g
69630 65 6e 65 72 61 74 69 6f 6e 20 6f 66 20 73 74 61 eneration of sta
69640 74 65 6d 65 6e 74 73 20 74 68 61 74 20 64 6f 20 tements that do
69650 69 6e 73 65 72 74 73 20 69 6e 74 6f 20 41 55 54 inserts into AUT
69660 4f 49 4e 43 52 45 4d 45 4e 54 20 0a 2a 2a 20 74 OINCREMENT .** t
69670 61 62 6c 65 73 2c 20 74 68 65 20 66 6f 6c 6c 6f ables, the follo
69680 77 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e wing information
69690 20 69 73 20 61 74 74 61 63 68 65 64 20 74 6f 20 is attached to
696a0 74 68 65 20 54 61 62 6c 65 2e 75 2e 61 75 74 6f the Table.u.auto
696b0 49 6e 63 2e 70 0a 2a 2a 20 70 6f 69 6e 74 65 72 Inc.p.** pointer
696c0 20 6f 66 20 65 61 63 68 20 61 75 74 6f 69 6e 63 of each autoinc
696d0 72 65 6d 65 6e 74 20 74 61 62 6c 65 20 74 6f 20 rement table to
696e0 72 65 63 6f 72 64 20 73 6f 6d 65 20 73 69 64 65 record some side
696f0 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 68 61 information tha
69700 74 0a 2a 2a 20 74 68 65 20 63 6f 64 65 20 67 65 t.** the code ge
69710 6e 65 72 61 74 6f 72 20 6e 65 65 64 73 2e 20 20 nerator needs.
69720 57 65 20 68 61 76 65 20 74 6f 20 6b 65 65 70 20 We have to keep
69730 70 65 72 2d 74 61 62 6c 65 20 61 75 74 6f 69 6e per-table autoin
69740 63 72 65 6d 65 6e 74 0a 2a 2a 20 69 6e 66 6f 72 crement.** infor
69750 6d 61 74 69 6f 6e 20 69 6e 20 63 61 73 65 20 69 mation in case i
69760 6e 73 65 72 74 73 20 61 72 65 20 64 6f 77 6e 20 nserts are down
69770 77 69 74 68 69 6e 20 74 72 69 67 67 65 72 73 2e within triggers.
69780 20 20 54 72 69 67 67 65 72 73 20 64 6f 20 6e 6f Triggers do no
69790 74 0a 2a 2a 20 6e 6f 72 6d 61 6c 6c 79 20 63 6f t.** normally co
697a0 6f 72 64 69 6e 61 74 65 20 74 68 65 69 72 20 61 ordinate their a
697b0 63 74 69 76 69 74 69 65 73 2c 20 62 75 74 20 77 ctivities, but w
697c0 65 20 64 6f 20 6e 65 65 64 20 74 6f 20 63 6f 6f e do need to coo
697d0 72 64 69 6e 61 74 65 20 74 68 65 0a 2a 2a 20 6c rdinate the.** l
697e0 6f 61 64 69 6e 67 20 61 6e 64 20 73 61 76 69 6e oading and savin
697f0 67 20 6f 66 20 61 75 74 6f 69 6e 63 72 65 6d 65 g of autoincreme
69800 6e 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a nt information..
69810 2a 2f 0a 73 74 72 75 63 74 20 41 75 74 6f 69 6e */.struct Autoin
69820 63 49 6e 66 6f 20 7b 0a 20 20 41 75 74 6f 69 6e cInfo {. Autoin
69830 63 49 6e 66 6f 20 2a 70 4e 65 78 74 3b 20 20 20 cInfo *pNext;
69840 2f 2a 20 4e 65 78 74 20 69 6e 66 6f 20 62 6c 6f /* Next info blo
69850 63 6b 20 69 6e 20 61 20 6c 69 73 74 20 6f 66 20 ck in a list of
69860 74 68 65 6d 20 61 6c 6c 20 2a 2f 0a 20 20 54 61 them all */. Ta
69870 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20 ble *pTab;
69880 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 74 68 69 /* Table thi
69890 73 20 69 6e 66 6f 20 62 6c 6f 63 6b 20 72 65 66 s info block ref
698a0 65 72 73 20 74 6f 20 2a 2f 0a 20 20 69 6e 74 20 ers to */. int
698b0 69 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 iDb;
698c0 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20 73 71 /* Index in sq
698d0 6c 69 74 65 33 2e 61 44 62 5b 5d 20 6f 66 20 64 lite3.aDb[] of d
698e0 61 74 61 62 61 73 65 20 68 6f 6c 64 69 6e 67 20 atabase holding
698f0 70 54 61 62 20 2a 2f 0a 20 20 69 6e 74 20 72 65 pTab */. int re
69900 67 43 74 72 3b 20 20 20 20 20 20 20 20 20 20 20 gCtr;
69910 2f 2a 20 4d 65 6d 6f 72 79 20 72 65 67 69 73 74 /* Memory regist
69920 65 72 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 72 er holding the r
69930 6f 77 69 64 20 63 6f 75 6e 74 65 72 20 2a 2f 0a owid counter */.
69940 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 53 69 7a 65 20 6f };../*.** Size o
69950 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63 61 63 f the column cac
69960 68 65 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 he.*/.#ifndef SQ
69970 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 0a LITE_N_COLCACHE.
69980 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f # define SQLITE_
69990 4e 5f 43 4f 4c 43 41 43 48 45 20 31 30 0a 23 65 N_COLCACHE 10.#e
699a0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 74 20 6c ndif../*.** At l
699b0 65 61 73 74 20 6f 6e 65 20 69 6e 73 74 61 6e 63 east one instanc
699c0 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 e of the followi
699d0 6e 67 20 73 74 72 75 63 74 75 72 65 20 69 73 20 ng structure is
699e0 63 72 65 61 74 65 64 20 66 6f 72 20 65 61 63 68 created for each
699f0 20 0a 2a 2a 20 74 72 69 67 67 65 72 20 74 68 61 .** trigger tha
69a00 74 20 6d 61 79 20 62 65 20 66 69 72 65 64 20 77 t may be fired w
69a10 68 69 6c 65 20 70 61 72 73 69 6e 67 20 61 6e 20 hile parsing an
69a20 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45 20 6f INSERT, UPDATE o
69a30 72 20 44 45 4c 45 54 45 0a 2a 2a 20 73 74 61 74 r DELETE.** stat
69a40 65 6d 65 6e 74 2e 20 41 6c 6c 20 73 75 63 68 20 ement. All such
69a50 6f 62 6a 65 63 74 73 20 61 72 65 20 73 74 6f 72 objects are stor
69a60 65 64 20 69 6e 20 74 68 65 20 6c 69 6e 6b 65 64 ed in the linked
69a70 20 6c 69 73 74 20 68 65 61 64 65 64 20 61 74 0a list headed at.
69a80 2a 2a 20 50 61 72 73 65 2e 70 54 72 69 67 67 65 ** Parse.pTrigge
69a90 72 50 72 67 20 61 6e 64 20 64 65 6c 65 74 65 64 rPrg and deleted
69aa0 20 6f 6e 63 65 20 73 74 61 74 65 6d 65 6e 74 20 once statement
69ab0 63 6f 6d 70 69 6c 61 74 69 6f 6e 20 68 61 73 20 compilation has
69ac0 62 65 65 6e 0a 2a 2a 20 63 6f 6d 70 6c 65 74 65 been.** complete
69ad0 64 2e 0a 2a 2a 0a 2a 2a 20 41 20 56 64 62 65 20 d..**.** A Vdbe
69ae0 73 75 62 2d 70 72 6f 67 72 61 6d 20 74 68 61 74 sub-program that
69af0 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 implements the
69b00 62 6f 64 79 20 61 6e 64 20 57 48 45 4e 20 63 6c body and WHEN cl
69b10 61 75 73 65 20 6f 66 20 74 72 69 67 67 65 72 0a ause of trigger.
69b20 2a 2a 20 54 72 69 67 67 65 72 50 72 67 2e 70 54 ** TriggerPrg.pT
69b30 72 69 67 67 65 72 2c 20 61 73 73 75 6d 69 6e 67 rigger, assuming
69b40 20 61 20 64 65 66 61 75 6c 74 20 4f 4e 20 43 4f a default ON CO
69b50 4e 46 4c 49 43 54 20 63 6c 61 75 73 65 20 6f 66 NFLICT clause of
69b60 0a 2a 2a 20 54 72 69 67 67 65 72 50 72 67 2e 6f .** TriggerPrg.o
69b70 72 63 6f 6e 66 2c 20 69 73 20 73 74 6f 72 65 64 rconf, is stored
69b80 20 69 6e 20 74 68 65 20 54 72 69 67 67 65 72 50 in the TriggerP
69b90 72 67 2e 70 50 72 6f 67 72 61 6d 20 76 61 72 69 rg.pProgram vari
69ba0 61 62 6c 65 2e 0a 2a 2a 20 54 68 65 20 50 61 72 able..** The Par
69bb0 73 65 2e 70 54 72 69 67 67 65 72 50 72 67 20 6c se.pTriggerPrg l
69bc0 69 73 74 20 6e 65 76 65 72 20 63 6f 6e 74 61 69 ist never contai
69bd0 6e 73 20 74 77 6f 20 65 6e 74 72 69 65 73 20 77 ns two entries w
69be0 69 74 68 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 ith the same.**
69bf0 76 61 6c 75 65 73 20 66 6f 72 20 62 6f 74 68 20 values for both
69c00 70 54 72 69 67 67 65 72 20 61 6e 64 20 6f 72 63 pTrigger and orc
69c10 6f 6e 66 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 54 onf..**.** The T
69c20 72 69 67 67 65 72 50 72 67 2e 6f 6c 64 6d 61 73 riggerPrg.oldmas
69c30 6b 20 76 61 72 69 61 62 6c 65 20 69 73 20 73 65 k variable is se
69c40 74 20 74 6f 20 61 20 6d 61 73 6b 20 6f 66 20 6f t to a mask of o
69c50 6c 64 2e 2a 20 63 6f 6c 75 6d 6e 73 0a 2a 2a 20 ld.* columns.**
69c60 61 63 63 65 73 73 65 64 20 28 6f 72 20 73 65 74 accessed (or set
69c70 20 74 6f 20 30 20 66 6f 72 20 74 72 69 67 67 65 to 0 for trigge
69c80 72 73 20 66 69 72 65 64 20 61 73 20 61 20 72 65 rs fired as a re
69c90 73 75 6c 74 20 6f 66 20 49 4e 53 45 52 54 20 0a sult of INSERT .
69ca0 2a 2a 20 73 74 61 74 65 6d 65 6e 74 73 29 2e 0a ** statements)..
69cb0 2a 2f 0a 73 74 72 75 63 74 20 54 72 69 67 67 65 */.struct Trigge
69cc0 72 50 72 67 20 7b 0a 20 20 54 72 69 67 67 65 72 rPrg {. Trigger
69cd0 20 2a 70 54 72 69 67 67 65 72 3b 20 20 20 20 20 *pTrigger;
69ce0 20 2f 2a 20 54 72 69 67 67 65 72 20 74 68 69 73 /* Trigger this
69cf0 20 70 72 6f 67 72 61 6d 20 77 61 73 20 63 6f 64 program was cod
69d00 65 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 69 6e 74 ed from */. int
69d10 20 6f 72 63 6f 6e 66 3b 20 20 20 20 20 20 20 20 orconf;
69d20 20 20 20 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 /* Default
69d30 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 70 6f 6c 69 ON CONFLICT poli
69d40 63 79 20 2a 2f 0a 20 20 53 75 62 50 72 6f 67 72 cy */. SubProgr
69d50 61 6d 20 2a 70 50 72 6f 67 72 61 6d 3b 20 20 20 am *pProgram;
69d60 2f 2a 20 50 72 6f 67 72 61 6d 20 69 6d 70 6c 65 /* Program imple
69d70 6d 65 6e 74 69 6e 67 20 70 54 72 69 67 67 65 72 menting pTrigger
69d80 2f 6f 72 63 6f 6e 66 20 2a 2f 0a 20 20 75 33 32 /orconf */. u32
69d90 20 6f 6c 64 6d 61 73 6b 3b 20 20 20 20 20 20 20 oldmask;
69da0 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 /* Mask of
69db0 6f 6c 64 2e 2a 20 63 6f 6c 75 6d 6e 73 20 61 63 old.* columns ac
69dc0 63 65 73 73 65 64 20 2a 2f 0a 20 20 54 72 69 67 cessed */. Trig
69dd0 67 65 72 50 72 67 20 2a 70 4e 65 78 74 3b 20 20 gerPrg *pNext;
69de0 20 20 20 20 2f 2a 20 4e 65 78 74 20 65 6e 74 72 /* Next entr
69df0 79 20 69 6e 20 50 61 72 73 65 2e 70 54 72 69 67 y in Parse.pTrig
69e00 67 65 72 50 72 67 20 6c 69 73 74 20 2a 2f 0a 7d gerPrg list */.}
69e10 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 53 51 4c 20 ;../*.** An SQL
69e20 70 61 72 73 65 72 20 63 6f 6e 74 65 78 74 2e 20 parser context.
69e30 20 41 20 63 6f 70 79 20 6f 66 20 74 68 69 73 20 A copy of this
69e40 73 74 72 75 63 74 75 72 65 20 69 73 20 70 61 73 structure is pas
69e50 73 65 64 20 74 68 72 6f 75 67 68 0a 2a 2a 20 74 sed through.** t
69e60 68 65 20 70 61 72 73 65 72 20 61 6e 64 20 64 6f he parser and do
69e70 77 6e 20 69 6e 74 6f 20 61 6c 6c 20 74 68 65 20 wn into all the
69e80 70 61 72 73 65 72 20 61 63 74 69 6f 6e 20 72 6f parser action ro
69e90 75 74 69 6e 65 20 69 6e 20 6f 72 64 65 72 20 74 utine in order t
69ea0 6f 0a 2a 2a 20 63 61 72 72 79 20 61 72 6f 75 6e o.** carry aroun
69eb0 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 68 d information th
69ec0 61 74 20 69 73 20 67 6c 6f 62 61 6c 20 74 6f 20 at is global to
69ed0 74 68 65 20 65 6e 74 69 72 65 20 70 61 72 73 65 the entire parse
69ee0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 74 72 75 ..**.** The stru
69ef0 63 74 75 72 65 20 69 73 20 64 69 76 69 64 65 64 cture is divided
69f00 20 69 6e 74 6f 20 74 77 6f 20 70 61 72 74 73 2e into two parts.
69f10 20 20 57 68 65 6e 20 74 68 65 20 70 61 72 73 65 When the parse
69f20 72 20 61 6e 64 20 63 6f 64 65 0a 2a 2a 20 67 65 r and code.** ge
69f30 6e 65 72 61 74 65 20 63 61 6c 6c 20 74 68 65 6d nerate call them
69f40 73 65 6c 76 65 73 20 72 65 63 75 72 73 69 76 65 selves recursive
69f50 6c 79 2c 20 74 68 65 20 66 69 72 73 74 20 70 61 ly, the first pa
69f60 72 74 20 6f 66 20 74 68 65 20 73 74 72 75 63 74 rt of the struct
69f70 75 72 65 0a 2a 2a 20 69 73 20 63 6f 6e 73 74 61 ure.** is consta
69f80 6e 74 20 62 75 74 20 74 68 65 20 73 65 63 6f 6e nt but the secon
69f90 64 20 70 61 72 74 20 69 73 20 72 65 73 65 74 20 d part is reset
69fa0 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 at the beginning
69fb0 20 61 6e 64 20 65 6e 64 20 6f 66 0a 2a 2a 20 65 and end of.** e
69fc0 61 63 68 20 72 65 63 75 72 73 69 6f 6e 2e 0a 2a ach recursion..*
69fd0 2a 0a 2a 2a 20 54 68 65 20 6e 54 61 62 6c 65 4c *.** The nTableL
69fe0 6f 63 6b 20 61 6e 64 20 61 54 61 62 6c 65 4c 6f ock and aTableLo
69ff0 63 6b 20 76 61 72 69 61 62 6c 65 73 20 61 72 65 ck variables are
6a000 20 6f 6e 6c 79 20 75 73 65 64 20 69 66 20 74 68 only used if th
6a010 65 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 0a e shared-cache .
6a020 2a 2a 20 66 65 61 74 75 72 65 20 69 73 20 65 6e ** feature is en
6a030 61 62 6c 65 64 20 28 69 66 20 73 71 6c 69 74 65 abled (if sqlite
6a040 33 54 73 64 28 29 2d 3e 75 73 65 53 68 61 72 65 3Tsd()->useShare
6a050 64 44 61 74 61 20 69 73 20 74 72 75 65 29 2e 20 dData is true).
6a060 54 68 65 79 20 61 72 65 0a 2a 2a 20 75 73 65 64 They are.** used
6a070 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 73 65 to store the se
6a080 74 20 6f 66 20 74 61 62 6c 65 2d 6c 6f 63 6b 73 t of table-locks
6a090 20 72 65 71 75 69 72 65 64 20 62 79 20 74 68 65 required by the
6a0a0 20 73 74 61 74 65 6d 65 6e 74 20 62 65 69 6e 67 statement being
6a0b0 0a 2a 2a 20 63 6f 6d 70 69 6c 65 64 2e 20 46 75 .** compiled. Fu
6a0c0 6e 63 74 69 6f 6e 20 73 71 6c 69 74 65 33 54 61 nction sqlite3Ta
6a0d0 62 6c 65 4c 6f 63 6b 28 29 20 69 73 20 75 73 65 bleLock() is use
6a0e0 64 20 74 6f 20 61 64 64 20 65 6e 74 72 69 65 73 d to add entries
6a0f0 20 74 6f 20 74 68 65 0a 2a 2a 20 6c 69 73 74 2e to the.** list.
6a100 0a 2a 2f 0a 73 74 72 75 63 74 20 50 61 72 73 65 .*/.struct Parse
6a110 20 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 {. sqlite3 *db
6a120 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 ; /* The
6a130 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 73 main database s
6a140 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 69 6e tructure */. in
6a150 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 t rc;
6a160 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 /* Return cod
6a170 65 20 66 72 6f 6d 20 65 78 65 63 75 74 69 6f 6e e from execution
6a180 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 */. char *zErr
6a190 4d 73 67 3b 20 20 20 20 20 20 20 2f 2a 20 41 6e Msg; /* An
6a1a0 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 2a error message *
6a1b0 2f 0a 20 20 56 64 62 65 20 2a 70 56 64 62 65 3b /. Vdbe *pVdbe;
6a1c0 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 65 /* An e
6a1d0 6e 67 69 6e 65 20 66 6f 72 20 65 78 65 63 75 74 ngine for execut
6a1e0 69 6e 67 20 64 61 74 61 62 61 73 65 20 62 79 74 ing database byt
6a1f0 65 63 6f 64 65 20 2a 2f 0a 20 20 75 38 20 63 6f ecode */. u8 co
6a200 6c 4e 61 6d 65 73 53 65 74 3b 20 20 20 20 20 20 lNamesSet;
6a210 2f 2a 20 54 52 55 45 20 61 66 74 65 72 20 4f 50 /* TRUE after OP
6a220 5f 43 6f 6c 75 6d 6e 4e 61 6d 65 20 68 61 73 20 _ColumnName has
6a230 62 65 65 6e 20 69 73 73 75 65 64 20 74 6f 20 70 been issued to p
6a240 56 64 62 65 20 2a 2f 0a 20 20 75 38 20 6e 61 6d Vdbe */. u8 nam
6a250 65 43 6c 61 73 68 3b 20 20 20 20 20 20 20 20 2f eClash; /
6a260 2a 20 41 20 70 65 72 6d 61 6e 65 6e 74 20 74 61 * A permanent ta
6a270 62 6c 65 20 6e 61 6d 65 20 63 6c 61 73 68 65 73 ble name clashes
6a280 20 77 69 74 68 20 74 65 6d 70 20 74 61 62 6c 65 with temp table
6a290 20 6e 61 6d 65 20 2a 2f 0a 20 20 75 38 20 63 68 name */. u8 ch
6a2a0 65 63 6b 53 63 68 65 6d 61 3b 20 20 20 20 20 20 eckSchema;
6a2b0 2f 2a 20 43 61 75 73 65 73 20 73 63 68 65 6d 61 /* Causes schema
6a2c0 20 63 6f 6f 6b 69 65 20 63 68 65 63 6b 20 61 66 cookie check af
6a2d0 74 65 72 20 61 6e 20 65 72 72 6f 72 20 2a 2f 0a ter an error */.
6a2e0 20 20 75 38 20 6e 65 73 74 65 64 3b 20 20 20 20 u8 nested;
6a2f0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
6a300 20 6f 66 20 6e 65 73 74 65 64 20 63 61 6c 6c 73 of nested calls
6a310 20 74 6f 20 74 68 65 20 70 61 72 73 65 72 2f 63 to the parser/c
6a320 6f 64 65 20 67 65 6e 65 72 61 74 6f 72 20 2a 2f ode generator */
6a330 0a 20 20 75 38 20 70 61 72 73 65 45 72 72 6f 72 . u8 parseError
6a340 3b 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 ; /* True
6a350 61 66 74 65 72 20 61 20 70 61 72 73 69 6e 67 20 after a parsing
6a360 65 72 72 6f 72 2e 20 20 54 69 63 6b 65 74 20 23 error. Ticket #
6a370 31 37 39 34 20 2a 2f 0a 20 20 75 38 20 6e 54 65 1794 */. u8 nTe
6a380 6d 70 52 65 67 3b 20 20 20 20 20 20 20 20 20 2f mpReg; /
6a390 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 65 6d 70 * Number of temp
6a3a0 6f 72 61 72 79 20 72 65 67 69 73 74 65 72 73 20 orary registers
6a3b0 69 6e 20 61 54 65 6d 70 52 65 67 5b 5d 20 2a 2f in aTempReg[] */
6a3c0 0a 20 20 75 38 20 6e 54 65 6d 70 49 6e 55 73 65 . u8 nTempInUse
6a3d0 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 ; /* Numbe
6a3e0 72 20 6f 66 20 61 54 65 6d 70 52 65 67 5b 5d 20 r of aTempReg[]
6a3f0 63 75 72 72 65 6e 74 6c 79 20 63 68 65 63 6b 65 currently checke
6a400 64 20 6f 75 74 20 2a 2f 0a 20 20 69 6e 74 20 61 d out */. int a
6a410 54 65 6d 70 52 65 67 5b 38 5d 3b 20 20 20 20 20 TempReg[8];
6a420 2f 2a 20 48 6f 6c 64 69 6e 67 20 61 72 65 61 20 /* Holding area
6a430 66 6f 72 20 74 65 6d 70 6f 72 61 72 79 20 72 65 for temporary re
6a440 67 69 73 74 65 72 73 20 2a 2f 0a 20 20 69 6e 74 gisters */. int
6a450 20 6e 52 61 6e 67 65 52 65 67 3b 20 20 20 20 20 nRangeReg;
6a460 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 /* Size of the
6a470 20 74 65 6d 70 6f 72 61 72 79 20 72 65 67 69 73 temporary regis
6a480 74 65 72 20 62 6c 6f 63 6b 20 2a 2f 0a 20 20 69 ter block */. i
6a490 6e 74 20 69 52 61 6e 67 65 52 65 67 3b 20 20 20 nt iRangeReg;
6a4a0 20 20 20 20 2f 2a 20 46 69 72 73 74 20 72 65 67 /* First reg
6a4b0 69 73 74 65 72 20 69 6e 20 74 65 6d 70 6f 72 61 ister in tempora
6a4c0 72 79 20 72 65 67 69 73 74 65 72 20 62 6c 6f 63 ry register bloc
6a4d0 6b 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 72 72 3b k */. int nErr;
6a4e0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
6a4f0 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73 20 umber of errors
6a500 73 65 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 seen */. int nT
6a510 61 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f ab; /
6a520 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 72 65 76 * Number of prev
6a530 69 6f 75 73 6c 79 20 61 6c 6c 6f 63 61 74 65 64 iously allocated
6a540 20 56 44 42 45 20 63 75 72 73 6f 72 73 20 2a 2f VDBE cursors */
6a550 0a 20 20 69 6e 74 20 6e 4d 65 6d 3b 20 20 20 20 . int nMem;
6a560 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
6a570 72 20 6f 66 20 6d 65 6d 6f 72 79 20 63 65 6c 6c r of memory cell
6a580 73 20 75 73 65 64 20 73 6f 20 66 61 72 20 2a 2f s used so far */
6a590 0a 20 20 69 6e 74 20 6e 53 65 74 3b 20 20 20 20 . int nSet;
6a5a0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
6a5b0 72 20 6f 66 20 73 65 74 73 20 75 73 65 64 20 73 r of sets used s
6a5c0 6f 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20 63 o far */. int c
6a5d0 6b 42 61 73 65 3b 20 20 20 20 20 20 20 20 20 20 kBase;
6a5e0 2f 2a 20 42 61 73 65 20 72 65 67 69 73 74 65 72 /* Base register
6a5f0 20 6f 66 20 64 61 74 61 20 64 75 72 69 6e 67 20 of data during
6a600 63 68 65 63 6b 20 63 6f 6e 73 74 72 61 69 6e 74 check constraint
6a610 73 20 2a 2f 0a 20 20 69 6e 74 20 69 43 61 63 68 s */. int iCach
6a620 65 4c 65 76 65 6c 3b 20 20 20 20 20 2f 2a 20 43 eLevel; /* C
6a630 6f 6c 43 61 63 68 65 20 76 61 6c 69 64 20 77 68 olCache valid wh
6a640 65 6e 20 61 43 6f 6c 43 61 63 68 65 5b 5d 2e 69 en aColCache[].i
6a650 4c 65 76 65 6c 3c 3d 69 43 61 63 68 65 4c 65 76 Level<=iCacheLev
6a660 65 6c 20 2a 2f 0a 20 20 69 6e 74 20 69 43 61 63 el */. int iCac
6a670 68 65 43 6e 74 3b 20 20 20 20 20 20 20 2f 2a 20 heCnt; /*
6a680 43 6f 75 6e 74 65 72 20 75 73 65 64 20 74 6f 20 Counter used to
6a690 67 65 6e 65 72 61 74 65 20 61 43 6f 6c 43 61 63 generate aColCac
6a6a0 68 65 5b 5d 2e 6c 72 75 20 76 61 6c 75 65 73 20 he[].lru values
6a6b0 2a 2f 0a 20 20 75 38 20 6e 43 6f 6c 43 61 63 68 */. u8 nColCach
6a6c0 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d e; /* Num
6a6d0 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 ber of entries i
6a6e0 6e 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63 61 63 n the column cac
6a6f0 68 65 20 2a 2f 0a 20 20 75 38 20 69 43 6f 6c 43 he */. u8 iColC
6a700 61 63 68 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 ache; /*
6a710 4e 65 78 74 20 65 6e 74 72 79 20 6f 66 20 74 68 Next entry of th
6a720 65 20 63 61 63 68 65 20 74 6f 20 72 65 70 6c 61 e cache to repla
6a730 63 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 79 ce */. struct y
6a740 43 6f 6c 43 61 63 68 65 20 7b 0a 20 20 20 20 69 ColCache {. i
6a750 6e 74 20 69 54 61 62 6c 65 3b 20 20 20 20 20 20 nt iTable;
6a760 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 63 75 /* Table cu
6a770 72 73 6f 72 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 rsor number */.
6a780 20 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20 int iColumn;
6a790 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c /* Tabl
6a7a0 65 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 e column number
6a7b0 2a 2f 0a 20 20 20 20 75 38 20 61 66 66 43 68 61 */. u8 affCha
6a7c0 6e 67 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 nge; /*
6a7d0 54 72 75 65 20 69 66 20 74 68 69 73 20 72 65 67 True if this reg
6a7e0 69 73 74 65 72 20 68 61 73 20 68 61 64 20 61 6e ister has had an
6a7f0 20 61 66 66 69 6e 69 74 79 20 63 68 61 6e 67 65 affinity change
6a800 20 2a 2f 0a 20 20 20 20 75 38 20 74 65 6d 70 52 */. u8 tempR
6a810 65 67 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a eg; /*
6a820 20 69 52 65 67 20 69 73 20 61 20 74 65 6d 70 20 iReg is a temp
6a830 72 65 67 69 73 74 65 72 20 74 68 61 74 20 6e 65 register that ne
6a840 65 64 73 20 74 6f 20 62 65 20 66 72 65 65 64 20 eds to be freed
6a850 2a 2f 0a 20 20 20 20 69 6e 74 20 69 4c 65 76 65 */. int iLeve
6a860 6c 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 l; /*
6a870 4e 65 73 74 69 6e 67 20 6c 65 76 65 6c 20 2a 2f Nesting level */
6a880 0a 20 20 20 20 69 6e 74 20 69 52 65 67 3b 20 20 . int iReg;
6a890 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 /* Re
6a8a0 67 20 77 69 74 68 20 76 61 6c 75 65 20 6f 66 20 g with value of
6a8b0 74 68 69 73 20 63 6f 6c 75 6d 6e 2e 20 30 20 6d this column. 0 m
6a8c0 65 61 6e 73 20 6e 6f 6e 65 2e 20 2a 2f 0a 20 20 eans none. */.
6a8d0 20 20 69 6e 74 20 6c 72 75 3b 20 20 20 20 20 20 int lru;
6a8e0 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 61 73 74 /* Least
6a8f0 20 72 65 63 65 6e 74 6c 79 20 75 73 65 64 20 65 recently used e
6a900 6e 74 72 79 20 68 61 73 20 74 68 65 20 73 6d 61 ntry has the sma
6a910 6c 6c 65 73 74 20 76 61 6c 75 65 20 2a 2f 0a 20 llest value */.
6a920 20 7d 20 61 43 6f 6c 43 61 63 68 65 5b 53 51 4c } aColCache[SQL
6a930 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 5d 3b ITE_N_COLCACHE];
6a940 20 20 2f 2a 20 4f 6e 65 20 66 6f 72 20 65 61 63 /* One for eac
6a950 68 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 20 65 h column cache e
6a960 6e 74 72 79 20 2a 2f 0a 20 20 75 33 32 20 77 72 ntry */. u32 wr
6a970 69 74 65 4d 61 73 6b 3b 20 20 20 20 20 20 20 2f iteMask; /
6a980 2a 20 53 74 61 72 74 20 61 20 77 72 69 74 65 20 * Start a write
6a990 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 74 transaction on t
6a9a0 68 65 73 65 20 64 61 74 61 62 61 73 65 73 20 2a hese databases *
6a9b0 2f 0a 20 20 75 33 32 20 63 6f 6f 6b 69 65 4d 61 /. u32 cookieMa
6a9c0 73 6b 3b 20 20 20 20 20 20 2f 2a 20 42 69 74 6d sk; /* Bitm
6a9d0 61 73 6b 20 6f 66 20 73 63 68 65 6d 61 20 76 65 ask of schema ve
6a9e0 72 69 66 69 65 64 20 64 61 74 61 62 61 73 65 73 rified databases
6a9f0 20 2a 2f 0a 20 20 75 38 20 69 73 4d 75 6c 74 69 */. u8 isMulti
6aa00 57 72 69 74 65 3b 20 20 20 20 20 2f 2a 20 54 72 Write; /* Tr
6aa10 75 65 20 69 66 20 73 74 61 74 65 6d 65 6e 74 20 ue if statement
6aa20 6d 61 79 20 61 66 66 65 63 74 2f 69 6e 73 65 72 may affect/inser
6aa30 74 20 6d 75 6c 74 69 70 6c 65 20 72 6f 77 73 20 t multiple rows
6aa40 2a 2f 0a 20 20 75 38 20 6d 61 79 41 62 6f 72 74 */. u8 mayAbort
6aa50 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 ; /* Tru
6aa60 65 20 69 66 20 73 74 61 74 65 6d 65 6e 74 20 6d e if statement m
6aa70 61 79 20 74 68 72 6f 77 20 61 6e 20 41 42 4f 52 ay throw an ABOR
6aa80 54 20 65 78 63 65 70 74 69 6f 6e 20 2a 2f 0a 20 T exception */.
6aa90 20 69 6e 74 20 63 6f 6f 6b 69 65 47 6f 74 6f 3b int cookieGoto;
6aaa0 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 /* Address
6aab0 20 6f 66 20 4f 50 5f 47 6f 74 6f 20 74 6f 20 63 of OP_Goto to c
6aac0 6f 6f 6b 69 65 20 76 65 72 69 66 69 65 72 20 73 ookie verifier s
6aad0 75 62 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69 ubroutine */. i
6aae0 6e 74 20 63 6f 6f 6b 69 65 56 61 6c 75 65 5b 53 nt cookieValue[S
6aaf0 51 4c 49 54 45 5f 4d 41 58 5f 41 54 54 41 43 48 QLITE_MAX_ATTACH
6ab00 45 44 2b 32 5d 3b 20 20 2f 2a 20 56 61 6c 75 65 ED+2]; /* Value
6ab10 73 20 6f 66 20 63 6f 6f 6b 69 65 73 20 74 6f 20 s of cookies to
6ab20 76 65 72 69 66 79 20 2a 2f 0a 23 69 66 6e 64 65 verify */.#ifnde
6ab30 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 f SQLITE_OMIT_SH
6ab40 41 52 45 44 5f 43 41 43 48 45 0a 20 20 69 6e 74 ARED_CACHE. int
6ab50 20 6e 54 61 62 6c 65 4c 6f 63 6b 3b 20 20 20 20 nTableLock;
6ab60 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
6ab70 20 6c 6f 63 6b 73 20 69 6e 20 61 54 61 62 6c 65 locks in aTable
6ab80 4c 6f 63 6b 20 2a 2f 0a 20 20 54 61 62 6c 65 4c Lock */. TableL
6ab90 6f 63 6b 20 2a 61 54 61 62 6c 65 4c 6f 63 6b 3b ock *aTableLock;
6aba0 20 2f 2a 20 52 65 71 75 69 72 65 64 20 74 61 62 /* Required tab
6abb0 6c 65 20 6c 6f 63 6b 73 20 66 6f 72 20 73 68 61 le locks for sha
6abc0 72 65 64 2d 63 61 63 68 65 20 6d 6f 64 65 20 2a red-cache mode *
6abd0 2f 0a 23 65 6e 64 69 66 0a 20 20 69 6e 74 20 72 /.#endif. int r
6abe0 65 67 52 6f 77 69 64 3b 20 20 20 20 20 20 20 20 egRowid;
6abf0 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c 64 /* Register hold
6ac00 69 6e 67 20 72 6f 77 69 64 20 6f 66 20 43 52 45 ing rowid of CRE
6ac10 41 54 45 20 54 41 42 4c 45 20 65 6e 74 72 79 20 ATE TABLE entry
6ac20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 52 6f 6f 74 */. int regRoot
6ac30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 ; /* Reg
6ac40 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 72 6f ister holding ro
6ac50 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 66 ot page number f
6ac60 6f 72 20 6e 65 77 20 6f 62 6a 65 63 74 73 20 2a or new objects *
6ac70 2f 0a 20 20 41 75 74 6f 69 6e 63 49 6e 66 6f 20 /. AutoincInfo
6ac80 2a 70 41 69 6e 63 3b 20 20 2f 2a 20 49 6e 66 6f *pAinc; /* Info
6ac90 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 41 55 rmation about AU
6aca0 54 4f 49 4e 43 52 45 4d 45 4e 54 20 63 6f 75 6e TOINCREMENT coun
6acb0 74 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d ters */. int nM
6acc0 61 78 41 72 67 3b 20 20 20 20 20 20 20 20 20 2f axArg; /
6acd0 2a 20 4d 61 78 20 61 72 67 73 20 70 61 73 73 65 * Max args passe
6ace0 64 20 74 6f 20 75 73 65 72 20 66 75 6e 63 74 69 d to user functi
6acf0 6f 6e 20 62 79 20 73 75 62 2d 70 72 6f 67 72 61 on by sub-progra
6ad00 6d 20 2a 2f 0a 0a 20 20 2f 2a 20 49 6e 66 6f 72 m */.. /* Infor
6ad10 6d 61 74 69 6f 6e 20 75 73 65 64 20 77 68 69 6c mation used whil
6ad20 65 20 63 6f 64 69 6e 67 20 74 72 69 67 67 65 72 e coding trigger
6ad30 20 70 72 6f 67 72 61 6d 73 2e 20 2a 2f 0a 20 20 programs. */.
6ad40 50 61 72 73 65 20 2a 70 54 6f 70 6c 65 76 65 6c Parse *pToplevel
6ad50 3b 20 20 20 20 2f 2a 20 50 61 72 73 65 20 73 74 ; /* Parse st
6ad60 72 75 63 74 75 72 65 20 66 6f 72 20 6d 61 69 6e ructure for main
6ad70 20 70 72 6f 67 72 61 6d 20 28 6f 72 20 4e 55 4c program (or NUL
6ad80 4c 29 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 L) */. Table *p
6ad90 54 72 69 67 67 65 72 54 61 62 3b 20 20 2f 2a 20 TriggerTab; /*
6ada0 54 61 62 6c 65 20 74 72 69 67 67 65 72 73 20 61 Table triggers a
6adb0 72 65 20 62 65 69 6e 67 20 63 6f 64 65 64 20 66 re being coded f
6adc0 6f 72 20 2a 2f 0a 20 20 75 33 32 20 6f 6c 64 6d or */. u32 oldm
6add0 61 73 6b 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 ask; /*
6ade0 4d 61 73 6b 20 6f 66 20 6f 6c 64 2e 2a 20 63 6f Mask of old.* co
6adf0 6c 75 6d 6e 73 20 72 65 66 65 72 65 6e 63 65 64 lumns referenced
6ae00 20 2a 2f 0a 20 20 75 38 20 65 54 72 69 67 67 65 */. u8 eTrigge
6ae10 72 4f 70 3b 20 20 20 20 20 20 20 2f 2a 20 54 4b rOp; /* TK
6ae20 5f 55 50 44 41 54 45 2c 20 54 4b 5f 49 4e 53 45 _UPDATE, TK_INSE
6ae30 52 54 20 6f 72 20 54 4b 5f 44 45 4c 45 54 45 20 RT or TK_DELETE
6ae40 2a 2f 0a 20 20 75 38 20 65 4f 72 63 6f 6e 66 3b */. u8 eOrconf;
6ae50 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 66 /* Def
6ae60 61 75 6c 74 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 ault ON CONFLICT
6ae70 20 70 6f 6c 69 63 79 20 66 6f 72 20 74 72 69 67 policy for trig
6ae80 67 65 72 20 73 74 65 70 73 20 2a 2f 0a 20 20 75 ger steps */. u
6ae90 38 20 64 69 73 61 62 6c 65 54 72 69 67 67 65 72 8 disableTrigger
6aea0 73 3b 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 64 s; /* True to d
6aeb0 69 73 61 62 6c 65 20 74 72 69 67 67 65 72 73 20 isable triggers
6aec0 2a 2f 0a 0a 20 20 2f 2a 20 41 62 6f 76 65 20 69 */.. /* Above i
6aed0 73 20 63 6f 6e 73 74 61 6e 74 20 62 65 74 77 65 s constant betwe
6aee0 65 6e 20 72 65 63 75 72 73 69 6f 6e 73 2e 20 20 en recursions.
6aef0 42 65 6c 6f 77 20 69 73 20 72 65 73 65 74 20 62 Below is reset b
6af00 65 66 6f 72 65 20 61 6e 64 20 61 66 74 65 72 0a efore and after.
6af10 20 20 2a 2a 20 65 61 63 68 20 72 65 63 75 72 73 ** each recurs
6af20 69 6f 6e 20 2a 2f 0a 0a 20 20 69 6e 74 20 6e 56 ion */.. int nV
6af30 61 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f ar; /
6af40 2a 20 4e 75 6d 62 65 72 20 6f 66 20 27 3f 27 20 * Number of '?'
6af50 76 61 72 69 61 62 6c 65 73 20 73 65 65 6e 20 69 variables seen i
6af60 6e 20 74 68 65 20 53 51 4c 20 73 6f 20 66 61 72 n the SQL so far
6af70 20 2a 2f 0a 20 20 69 6e 74 20 6e 56 61 72 45 78 */. int nVarEx
6af80 70 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 pr; /* Nu
6af90 6d 62 65 72 20 6f 66 20 75 73 65 64 20 73 6c 6f mber of used slo
6afa0 74 73 20 69 6e 20 61 70 56 61 72 45 78 70 72 5b ts in apVarExpr[
6afb0 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 56 61 72 45 ] */. int nVarE
6afc0 78 70 72 41 6c 6c 6f 63 3b 20 20 20 2f 2a 20 4e xprAlloc; /* N
6afd0 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 63 61 74 umber of allocat
6afe0 65 64 20 73 6c 6f 74 73 20 69 6e 20 61 70 56 61 ed slots in apVa
6aff0 72 45 78 70 72 5b 5d 20 2a 2f 0a 20 20 45 78 70 rExpr[] */. Exp
6b000 72 20 2a 2a 61 70 56 61 72 45 78 70 72 3b 20 20 r **apVarExpr;
6b010 20 20 2f 2a 20 50 6f 69 6e 74 65 72 73 20 74 6f /* Pointers to
6b020 20 3a 61 61 61 20 61 6e 64 20 24 61 61 61 61 20 :aaa and $aaaa
6b030 77 69 6c 64 63 61 72 64 20 65 78 70 72 65 73 73 wildcard express
6b040 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 ions */. int nA
6b050 6c 69 61 73 3b 20 20 20 20 20 20 20 20 20 20 2f lias; /
6b060 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 6c 69 61 * Number of alia
6b070 73 65 64 20 72 65 73 75 6c 74 20 73 65 74 20 63 sed result set c
6b080 6f 6c 75 6d 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 olumns */. int
6b090 6e 41 6c 69 61 73 41 6c 6c 6f 63 3b 20 20 20 20 nAliasAlloc;
6b0a0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 6c /* Number of al
6b0b0 6c 6f 63 61 74 65 64 20 73 6c 6f 74 73 20 66 6f located slots fo
6b0c0 72 20 61 41 6c 69 61 73 5b 5d 20 2a 2f 0a 20 20 r aAlias[] */.
6b0d0 69 6e 74 20 2a 61 41 6c 69 61 73 3b 20 20 20 20 int *aAlias;
6b0e0 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 /* Register
6b0f0 20 75 73 65 64 20 74 6f 20 68 6f 6c 64 20 61 6c used to hold al
6b100 69 61 73 65 64 20 72 65 73 75 6c 74 20 2a 2f 0a iased result */.
6b110 20 20 75 38 20 65 78 70 6c 61 69 6e 3b 20 20 20 u8 explain;
6b120 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
6b130 66 20 74 68 65 20 45 58 50 4c 41 49 4e 20 66 6c f the EXPLAIN fl
6b140 61 67 20 69 73 20 66 6f 75 6e 64 20 6f 6e 20 74 ag is found on t
6b150 68 65 20 71 75 65 72 79 20 2a 2f 0a 20 20 54 6f he query */. To
6b160 6b 65 6e 20 73 4e 61 6d 65 54 6f 6b 65 6e 3b 20 ken sNameToken;
6b170 20 20 20 2f 2a 20 54 6f 6b 65 6e 20 77 69 74 68 /* Token with
6b180 20 75 6e 71 75 61 6c 69 66 69 65 64 20 73 63 68 unqualified sch
6b190 65 6d 61 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20 ema object name
6b1a0 2a 2f 0a 20 20 54 6f 6b 65 6e 20 73 4c 61 73 74 */. Token sLast
6b1b0 54 6f 6b 65 6e 3b 20 20 20 20 2f 2a 20 54 68 65 Token; /* The
6b1c0 20 6c 61 73 74 20 74 6f 6b 65 6e 20 70 61 72 73 last token pars
6b1d0 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 ed */. const ch
6b1e0 61 72 20 2a 7a 54 61 69 6c 3b 20 20 20 2f 2a 20 ar *zTail; /*
6b1f0 41 6c 6c 20 53 51 4c 20 74 65 78 74 20 70 61 73 All SQL text pas
6b200 74 20 74 68 65 20 6c 61 73 74 20 73 65 6d 69 63 t the last semic
6b210 6f 6c 6f 6e 20 70 61 72 73 65 64 20 2a 2f 0a 20 olon parsed */.
6b220 20 54 61 62 6c 65 20 2a 70 4e 65 77 54 61 62 6c Table *pNewTabl
6b230 65 3b 20 20 20 20 2f 2a 20 41 20 74 61 62 6c 65 e; /* A table
6b240 20 62 65 69 6e 67 20 63 6f 6e 73 74 72 75 63 74 being construct
6b250 65 64 20 62 79 20 43 52 45 41 54 45 20 54 41 42 ed by CREATE TAB
6b260 4c 45 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 20 LE */. Trigger
6b270 2a 70 4e 65 77 54 72 69 67 67 65 72 3b 20 20 20 *pNewTrigger;
6b280 20 20 2f 2a 20 54 72 69 67 67 65 72 20 75 6e 64 /* Trigger und
6b290 65 72 20 63 6f 6e 73 74 72 75 63 74 20 62 79 20 er construct by
6b2a0 61 20 43 52 45 41 54 45 20 54 52 49 47 47 45 52 a CREATE TRIGGER
6b2b0 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 */. const char
6b2c0 20 2a 7a 41 75 74 68 43 6f 6e 74 65 78 74 3b 20 *zAuthContext;
6b2d0 2f 2a 20 54 68 65 20 36 74 68 20 70 61 72 61 6d /* The 6th param
6b2e0 65 74 65 72 20 74 6f 20 64 62 2d 3e 78 41 75 74 eter to db->xAut
6b2f0 68 20 63 61 6c 6c 62 61 63 6b 73 20 2a 2f 0a 23 h callbacks */.#
6b300 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
6b310 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a IT_VIRTUALTABLE.
6b320 20 20 54 6f 6b 65 6e 20 73 41 72 67 3b 20 20 20 Token sArg;
6b330 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
6b340 43 6f 6d 70 6c 65 74 65 20 74 65 78 74 20 6f 66 Complete text of
6b350 20 61 20 6d 6f 64 75 6c 65 20 61 72 67 75 6d 65 a module argume
6b360 6e 74 20 2a 2f 0a 20 20 75 38 20 64 65 63 6c 61 nt */. u8 decla
6b370 72 65 56 74 61 62 3b 20 20 20 20 20 20 20 20 20 reVtab;
6b380 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 69 6e /* True if in
6b390 73 69 64 65 20 73 71 6c 69 74 65 33 5f 64 65 63 side sqlite3_dec
6b3a0 6c 61 72 65 5f 76 74 61 62 28 29 20 2a 2f 0a 20 lare_vtab() */.
6b3b0 20 69 6e 74 20 6e 56 74 61 62 4c 6f 63 6b 3b 20 int nVtabLock;
6b3c0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
6b3d0 75 6d 62 65 72 20 6f 66 20 76 69 72 74 75 61 6c umber of virtual
6b3e0 20 74 61 62 6c 65 73 20 74 6f 20 6c 6f 63 6b 20 tables to lock
6b3f0 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 2a 61 70 56 */. Table **apV
6b400 74 61 62 4c 6f 63 6b 3b 20 20 20 20 20 20 20 20 tabLock;
6b410 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 76 69 /* Pointer to vi
6b420 72 74 75 61 6c 20 74 61 62 6c 65 73 20 6e 65 65 rtual tables nee
6b430 64 69 6e 67 20 6c 6f 63 6b 69 6e 67 20 2a 2f 0a ding locking */.
6b440 23 65 6e 64 69 66 0a 20 20 69 6e 74 20 6e 48 65 #endif. int nHe
6b450 69 67 68 74 3b 20 20 20 20 20 20 20 20 20 20 20 ight;
6b460 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 74 /* Expression t
6b470 72 65 65 20 68 65 69 67 68 74 20 6f 66 20 63 75 ree height of cu
6b480 72 72 65 6e 74 20 73 75 62 2d 73 65 6c 65 63 74 rrent sub-select
6b490 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 5a 6f */. Table *pZo
6b4a0 6d 62 69 65 54 61 62 3b 20 20 20 20 20 20 2f 2a mbieTab; /*
6b4b0 20 4c 69 73 74 20 6f 66 20 54 61 62 6c 65 20 6f List of Table o
6b4c0 62 6a 65 63 74 73 20 74 6f 20 64 65 6c 65 74 65 bjects to delete
6b4d0 20 61 66 74 65 72 20 63 6f 64 65 20 67 65 6e 20 after code gen
6b4e0 2a 2f 0a 20 20 54 72 69 67 67 65 72 50 72 67 20 */. TriggerPrg
6b4f0 2a 70 54 72 69 67 67 65 72 50 72 67 3b 20 20 20 *pTriggerPrg;
6b500 20 2f 2a 20 4c 69 6e 6b 65 64 20 6c 69 73 74 20 /* Linked list
6b510 6f 66 20 63 6f 64 65 64 20 74 72 69 67 67 65 72 of coded trigger
6b520 73 20 2a 2f 0a 7d 3b 0a 0a 23 69 66 64 65 66 20 s */.};..#ifdef
6b530 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 SQLITE_OMIT_VIRT
6b540 55 41 4c 54 41 42 4c 45 0a 20 20 23 64 65 66 69 UALTABLE. #defi
6b550 6e 65 20 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54 ne IN_DECLARE_VT
6b560 41 42 20 30 0a 23 65 6c 73 65 0a 20 20 23 64 65 AB 0.#else. #de
6b570 66 69 6e 65 20 49 4e 5f 44 45 43 4c 41 52 45 5f fine IN_DECLARE_
6b580 56 54 41 42 20 28 70 50 61 72 73 65 2d 3e 64 65 VTAB (pParse->de
6b590 63 6c 61 72 65 56 74 61 62 29 0a 23 65 6e 64 69 clareVtab).#endi
6b5a0 66 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 f../*.** An inst
6b5b0 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c ance of the foll
6b5c0 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 owing structure
6b5d0 63 61 6e 20 62 65 20 64 65 63 6c 61 72 65 64 20 can be declared
6b5e0 6f 6e 20 61 20 73 74 61 63 6b 20 61 6e 64 20 75 on a stack and u
6b5f0 73 65 64 0a 2a 2a 20 74 6f 20 73 61 76 65 20 74 sed.** to save t
6b600 68 65 20 50 61 72 73 65 2e 7a 41 75 74 68 43 6f he Parse.zAuthCo
6b610 6e 74 65 78 74 20 76 61 6c 75 65 20 73 6f 20 74 ntext value so t
6b620 68 61 74 20 69 74 20 63 61 6e 20 62 65 20 72 65 hat it can be re
6b630 73 74 6f 72 65 64 20 6c 61 74 65 72 2e 0a 2a 2f stored later..*/
6b640 0a 73 74 72 75 63 74 20 41 75 74 68 43 6f 6e 74 .struct AuthCont
6b650 65 78 74 20 7b 0a 20 20 63 6f 6e 73 74 20 63 68 ext {. const ch
6b660 61 72 20 2a 7a 41 75 74 68 43 6f 6e 74 65 78 74 ar *zAuthContext
6b670 3b 20 20 20 2f 2a 20 50 75 74 20 73 61 76 65 64 ; /* Put saved
6b680 20 50 61 72 73 65 2e 7a 41 75 74 68 43 6f 6e 74 Parse.zAuthCont
6b690 65 78 74 20 68 65 72 65 20 2a 2f 0a 20 20 50 61 ext here */. Pa
6b6a0 72 73 65 20 2a 70 50 61 72 73 65 3b 20 20 20 20 rse *pParse;
6b6b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
6b6c0 20 50 61 72 73 65 20 73 74 72 75 63 74 75 72 65 Parse structure
6b6d0 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 42 69 */.};../*.** Bi
6b6e0 74 66 69 65 6c 64 20 66 6c 61 67 73 20 66 6f 72 tfield flags for
6b6f0 20 50 35 20 76 61 6c 75 65 20 69 6e 20 4f 50 5f P5 value in OP_
6b700 49 6e 73 65 72 74 20 61 6e 64 20 4f 50 5f 44 65 Insert and OP_De
6b710 6c 65 74 65 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 lete.*/.#define
6b720 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45 20 20 OPFLAG_NCHANGE
6b730 20 20 20 20 20 30 78 30 31 20 20 20 20 2f 2a 20 0x01 /*
6b740 53 65 74 20 74 6f 20 75 70 64 61 74 65 20 64 62 Set to update db
6b750 2d 3e 6e 43 68 61 6e 67 65 20 2a 2f 0a 23 64 65 ->nChange */.#de
6b760 66 69 6e 65 20 4f 50 46 4c 41 47 5f 4c 41 53 54 fine OPFLAG_LAST
6b770 52 4f 57 49 44 20 20 20 20 20 30 78 30 32 20 20 ROWID 0x02
6b780 20 20 2f 2a 20 53 65 74 20 74 6f 20 75 70 64 61 /* Set to upda
6b790 74 65 20 64 62 2d 3e 6c 61 73 74 52 6f 77 69 64 te db->lastRowid
6b7a0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c */.#define OPFL
6b7b0 41 47 5f 49 53 55 50 44 41 54 45 20 20 20 20 20 AG_ISUPDATE
6b7c0 20 30 78 30 34 20 20 20 20 2f 2a 20 54 68 69 73 0x04 /* This
6b7d0 20 4f 50 5f 49 6e 73 65 72 74 20 69 73 20 61 6e OP_Insert is an
6b7e0 20 73 71 6c 20 55 50 44 41 54 45 20 2a 2f 0a 23 sql UPDATE */.#
6b7f0 64 65 66 69 6e 65 20 4f 50 46 4c 41 47 5f 41 50 define OPFLAG_AP
6b800 50 45 4e 44 20 20 20 20 20 20 20 20 30 78 30 38 PEND 0x08
6b810 20 20 20 20 2f 2a 20 54 68 69 73 20 69 73 20 6c /* This is l
6b820 69 6b 65 6c 79 20 74 6f 20 62 65 20 61 6e 20 61 ikely to be an a
6b830 70 70 65 6e 64 20 2a 2f 0a 23 64 65 66 69 6e 65 ppend */.#define
6b840 20 4f 50 46 4c 41 47 5f 55 53 45 53 45 45 4b 52 OPFLAG_USESEEKR
6b850 45 53 55 4c 54 20 30 78 31 30 20 20 20 20 2f 2a ESULT 0x10 /*
6b860 20 54 72 79 20 74 6f 20 61 76 6f 69 64 20 61 20 Try to avoid a
6b870 73 65 65 6b 20 69 6e 20 42 74 72 65 65 49 6e 73 seek in BtreeIns
6b880 65 72 74 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 ert() */.#define
6b890 20 4f 50 46 4c 41 47 5f 43 4c 45 41 52 43 41 43 OPFLAG_CLEARCAC
6b8a0 48 45 20 20 20 20 30 78 32 30 20 20 20 20 2f 2a HE 0x20 /*
6b8b0 20 43 6c 65 61 72 20 70 73 65 75 64 6f 2d 74 61 Clear pseudo-ta
6b8c0 62 6c 65 20 63 61 63 68 65 20 69 6e 20 4f 50 5f ble cache in OP_
6b8d0 43 6f 6c 75 6d 6e 20 2a 2f 0a 0a 2f 2a 0a 20 2a Column */../*. *
6b8e0 20 45 61 63 68 20 74 72 69 67 67 65 72 20 70 72 Each trigger pr
6b8f0 65 73 65 6e 74 20 69 6e 20 74 68 65 20 64 61 74 esent in the dat
6b900 61 62 61 73 65 20 73 63 68 65 6d 61 20 69 73 20 abase schema is
6b910 73 74 6f 72 65 64 20 61 73 20 61 6e 20 69 6e 73 stored as an ins
6b920 74 61 6e 63 65 20 6f 66 0a 20 2a 20 73 74 72 75 tance of. * stru
6b930 63 74 20 54 72 69 67 67 65 72 2e 20 0a 20 2a 0a ct Trigger. . *.
6b940 20 2a 20 50 6f 69 6e 74 65 72 73 20 74 6f 20 69 * Pointers to i
6b950 6e 73 74 61 6e 63 65 73 20 6f 66 20 73 74 72 75 nstances of stru
6b960 63 74 20 54 72 69 67 67 65 72 20 61 72 65 20 73 ct Trigger are s
6b970 74 6f 72 65 64 20 69 6e 20 74 77 6f 20 77 61 79 tored in two way
6b980 73 2e 0a 20 2a 20 31 2e 20 49 6e 20 74 68 65 20 s.. * 1. In the
6b990 22 74 72 69 67 48 61 73 68 22 20 68 61 73 68 20 "trigHash" hash
6b9a0 74 61 62 6c 65 20 28 70 61 72 74 20 6f 66 20 74 table (part of t
6b9b0 68 65 20 73 71 6c 69 74 65 33 2a 20 74 68 61 74 he sqlite3* that
6b9c0 20 72 65 70 72 65 73 65 6e 74 73 20 74 68 65 20 represents the
6b9d0 0a 20 2a 20 20 20 20 64 61 74 61 62 61 73 65 29 . * database)
6b9e0 2e 20 54 68 69 73 20 61 6c 6c 6f 77 73 20 54 72 . This allows Tr
6b9f0 69 67 67 65 72 20 73 74 72 75 63 74 75 72 65 73 igger structures
6ba00 20 74 6f 20 62 65 20 72 65 74 72 69 65 76 65 64 to be retrieved
6ba10 20 62 79 20 6e 61 6d 65 2e 0a 20 2a 20 32 2e 20 by name.. * 2.
6ba20 41 6c 6c 20 74 72 69 67 67 65 72 73 20 61 73 73 All triggers ass
6ba30 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 20 73 ociated with a s
6ba40 69 6e 67 6c 65 20 74 61 62 6c 65 20 66 6f 72 6d ingle table form
6ba50 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 2c 20 a linked list,
6ba60 75 73 69 6e 67 20 74 68 65 0a 20 2a 20 20 20 20 using the. *
6ba70 70 4e 65 78 74 20 6d 65 6d 62 65 72 20 6f 66 20 pNext member of
6ba80 73 74 72 75 63 74 20 54 72 69 67 67 65 72 2e 20 struct Trigger.
6ba90 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 A pointer to the
6baa0 20 66 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f first element o
6bab0 66 20 74 68 65 0a 20 2a 20 20 20 20 6c 69 6e 6b f the. * link
6bac0 65 64 20 6c 69 73 74 20 69 73 20 73 74 6f 72 65 ed list is store
6bad0 64 20 61 73 20 74 68 65 20 22 70 54 72 69 67 67 d as the "pTrigg
6bae0 65 72 22 20 6d 65 6d 62 65 72 20 6f 66 20 74 68 er" member of th
6baf0 65 20 61 73 73 6f 63 69 61 74 65 64 0a 20 2a 20 e associated. *
6bb00 20 20 20 73 74 72 75 63 74 20 54 61 62 6c 65 2e struct Table.
6bb10 0a 20 2a 0a 20 2a 20 54 68 65 20 22 73 74 65 70 . *. * The "step
6bb20 5f 6c 69 73 74 22 20 6d 65 6d 62 65 72 20 70 6f _list" member po
6bb30 69 6e 74 73 20 74 6f 20 74 68 65 20 66 69 72 73 ints to the firs
6bb40 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20 61 20 6c t element of a l
6bb50 69 6e 6b 65 64 20 6c 69 73 74 0a 20 2a 20 63 6f inked list. * co
6bb60 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 53 51 4c ntaining the SQL
6bb70 20 73 74 61 74 65 6d 65 6e 74 73 20 73 70 65 63 statements spec
6bb80 69 66 69 65 64 20 61 73 20 74 68 65 20 74 72 69 ified as the tri
6bb90 67 67 65 72 20 70 72 6f 67 72 61 6d 2e 0a 20 2a gger program.. *
6bba0 2f 0a 73 74 72 75 63 74 20 54 72 69 67 67 65 72 /.struct Trigger
6bbb0 20 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 {. char *zName
6bbc0 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
6bbd0 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 The name of the
6bbe0 74 72 69 67 67 65 72 20 20 20 20 20 20 20 20 20 trigger
6bbf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a *
6bc00 2f 0a 20 20 63 68 61 72 20 2a 74 61 62 6c 65 3b /. char *table;
6bc10 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
6bc20 68 65 20 74 61 62 6c 65 20 6f 72 20 76 69 65 77 he table or view
6bc30 20 74 6f 20 77 68 69 63 68 20 74 68 65 20 74 72 to which the tr
6bc40 69 67 67 65 72 20 61 70 70 6c 69 65 73 20 2a 2f igger applies */
6bc50 0a 20 20 75 38 20 6f 70 3b 20 20 20 20 20 20 20 . u8 op;
6bc60 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e /* On
6bc70 65 20 6f 66 20 54 4b 5f 44 45 4c 45 54 45 2c 20 e of TK_DELETE,
6bc80 54 4b 5f 55 50 44 41 54 45 2c 20 54 4b 5f 49 4e TK_UPDATE, TK_IN
6bc90 53 45 52 54 20 20 20 20 20 20 20 20 20 2a 2f 0a SERT */.
6bca0 20 20 75 38 20 74 72 5f 74 6d 3b 20 20 20 20 20 u8 tr_tm;
6bcb0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 /* One
6bcc0 20 6f 66 20 54 52 49 47 47 45 52 5f 42 45 46 4f of TRIGGER_BEFO
6bcd0 52 45 2c 20 54 52 49 47 47 45 52 5f 41 46 54 45 RE, TRIGGER_AFTE
6bce0 52 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 R */. Expr *pWh
6bcf0 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f en; /
6bd00 2a 20 54 68 65 20 57 48 45 4e 20 63 6c 61 75 73 * The WHEN claus
6bd10 65 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 e of the express
6bd20 69 6f 6e 20 28 6d 61 79 20 62 65 20 4e 55 4c 4c ion (may be NULL
6bd30 29 20 2a 2f 0a 20 20 49 64 4c 69 73 74 20 2a 70 ) */. IdList *p
6bd40 43 6f 6c 75 6d 6e 73 3b 20 20 20 20 20 20 20 2f Columns; /
6bd50 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 * If this is an
6bd60 55 50 44 41 54 45 20 4f 46 20 3c 63 6f 6c 75 6d UPDATE OF <colum
6bd70 6e 2d 6c 69 73 74 3e 20 74 72 69 67 67 65 72 2c n-list> trigger,
6bd80 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
6bd90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68 th
6bda0 65 20 3c 63 6f 6c 75 6d 6e 2d 6c 69 73 74 3e 20 e <column-list>
6bdb0 69 73 20 73 74 6f 72 65 64 20 68 65 72 65 20 2a is stored here *
6bdc0 2f 0a 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68 /. Schema *pSch
6bdd0 65 6d 61 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 ema; /* S
6bde0 63 68 65 6d 61 20 63 6f 6e 74 61 69 6e 69 6e 67 chema containing
6bdf0 20 74 68 65 20 74 72 69 67 67 65 72 20 2a 2f 0a the trigger */.
6be00 20 20 53 63 68 65 6d 61 20 2a 70 54 61 62 53 63 Schema *pTabSc
6be10 68 65 6d 61 3b 20 20 20 20 20 2f 2a 20 53 63 68 hema; /* Sch
6be20 65 6d 61 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 ema containing t
6be30 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 54 72 he table */. Tr
6be40 69 67 67 65 72 53 74 65 70 20 2a 73 74 65 70 5f iggerStep *step_
6be50 6c 69 73 74 3b 20 2f 2a 20 4c 69 6e 6b 20 6c 69 list; /* Link li
6be60 73 74 20 6f 66 20 74 72 69 67 67 65 72 20 70 72 st of trigger pr
6be70 6f 67 72 61 6d 20 73 74 65 70 73 20 20 20 20 20 ogram steps
6be80 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 54 72 69 */. Tri
6be90 67 67 65 72 20 2a 70 4e 65 78 74 3b 20 20 20 20 gger *pNext;
6bea0 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 74 72 69 /* Next tri
6beb0 67 67 65 72 20 61 73 73 6f 63 69 61 74 65 64 20 gger associated
6bec0 77 69 74 68 20 74 68 65 20 74 61 62 6c 65 20 2a with the table *
6bed0 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 74 72 /.};../*.** A tr
6bee0 69 67 67 65 72 20 69 73 20 65 69 74 68 65 72 20 igger is either
6bef0 61 20 42 45 46 4f 52 45 20 6f 72 20 61 6e 20 41 a BEFORE or an A
6bf00 46 54 45 52 20 74 72 69 67 67 65 72 2e 20 20 54 FTER trigger. T
6bf10 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6e he following con
6bf20 73 74 61 6e 74 73 0a 2a 2a 20 64 65 74 65 72 6d stants.** determ
6bf30 69 6e 65 20 77 68 69 63 68 2e 20 0a 2a 2a 0a 2a ine which. .**.*
6bf40 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20 6d * If there are m
6bf50 75 6c 74 69 70 6c 65 20 74 72 69 67 67 65 72 73 ultiple triggers
6bf60 2c 20 79 6f 75 20 6d 69 67 68 74 20 6f 66 20 73 , you might of s
6bf70 6f 6d 65 20 42 45 46 4f 52 45 20 61 6e 64 20 73 ome BEFORE and s
6bf80 6f 6d 65 20 41 46 54 45 52 2e 0a 2a 2a 20 49 6e ome AFTER..** In
6bf90 20 74 68 61 74 20 63 61 73 65 73 2c 20 74 68 65 that cases, the
6bfa0 20 63 6f 6e 73 74 61 6e 74 73 20 62 65 6c 6f 77 constants below
6bfb0 20 63 61 6e 20 62 65 20 4f 52 65 64 20 74 6f 67 can be ORed tog
6bfc0 65 74 68 65 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e ether..*/.#defin
6bfd0 65 20 54 52 49 47 47 45 52 5f 42 45 46 4f 52 45 e TRIGGER_BEFORE
6bfe0 20 20 31 0a 23 64 65 66 69 6e 65 20 54 52 49 47 1.#define TRIG
6bff0 47 45 52 5f 41 46 54 45 52 20 20 20 32 0a 0a 2f GER_AFTER 2../
6c000 2a 0a 20 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 *. * An instance
6c010 20 6f 66 20 73 74 72 75 63 74 20 54 72 69 67 67 of struct Trigg
6c020 65 72 53 74 65 70 20 69 73 20 75 73 65 64 20 74 erStep is used t
6c030 6f 20 73 74 6f 72 65 20 61 20 73 69 6e 67 6c 65 o store a single
6c040 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 0a 20 SQL statement.
6c050 2a 20 74 68 61 74 20 69 73 20 61 20 70 61 72 74 * that is a part
6c060 20 6f 66 20 61 20 74 72 69 67 67 65 72 2d 70 72 of a trigger-pr
6c070 6f 67 72 61 6d 2e 20 0a 20 2a 0a 20 2a 20 49 6e ogram. . *. * In
6c080 73 74 61 6e 63 65 73 20 6f 66 20 73 74 72 75 63 stances of struc
6c090 74 20 54 72 69 67 67 65 72 53 74 65 70 20 61 72 t TriggerStep ar
6c0a0 65 20 73 74 6f 72 65 64 20 69 6e 20 61 20 73 69 e stored in a si
6c0b0 6e 67 6c 79 20 6c 69 6e 6b 65 64 20 6c 69 73 74 ngly linked list
6c0c0 20 28 6c 69 6e 6b 65 64 0a 20 2a 20 75 73 69 6e (linked. * usin
6c0d0 67 20 74 68 65 20 22 70 4e 65 78 74 22 20 6d 65 g the "pNext" me
6c0e0 6d 62 65 72 29 20 72 65 66 65 72 65 6e 63 65 64 mber) referenced
6c0f0 20 62 79 20 74 68 65 20 22 73 74 65 70 5f 6c 69 by the "step_li
6c100 73 74 22 20 6d 65 6d 62 65 72 20 6f 66 20 74 68 st" member of th
6c110 65 20 0a 20 2a 20 61 73 73 6f 63 69 61 74 65 64 e . * associated
6c120 20 73 74 72 75 63 74 20 54 72 69 67 67 65 72 20 struct Trigger
6c130 69 6e 73 74 61 6e 63 65 2e 20 54 68 65 20 66 69 instance. The fi
6c140 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 rst element of t
6c150 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 69 he linked list i
6c160 73 0a 20 2a 20 74 68 65 20 66 69 72 73 74 20 73 s. * the first s
6c170 74 65 70 20 6f 66 20 74 68 65 20 74 72 69 67 67 tep of the trigg
6c180 65 72 2d 70 72 6f 67 72 61 6d 2e 0a 20 2a 20 0a er-program.. * .
6c190 20 2a 20 54 68 65 20 22 6f 70 22 20 6d 65 6d 62 * The "op" memb
6c1a0 65 72 20 69 6e 64 69 63 61 74 65 73 20 77 68 65 er indicates whe
6c1b0 74 68 65 72 20 74 68 69 73 20 69 73 20 61 20 22 ther this is a "
6c1c0 44 45 4c 45 54 45 22 2c 20 22 49 4e 53 45 52 54 DELETE", "INSERT
6c1d0 22 2c 20 22 55 50 44 41 54 45 22 20 6f 72 0a 20 ", "UPDATE" or.
6c1e0 2a 20 22 53 45 4c 45 43 54 22 20 73 74 61 74 65 * "SELECT" state
6c1f0 6d 65 6e 74 2e 20 54 68 65 20 6d 65 61 6e 69 6e ment. The meanin
6c200 67 73 20 6f 66 20 74 68 65 20 6f 74 68 65 72 20 gs of the other
6c210 6d 65 6d 62 65 72 73 20 69 73 20 64 65 74 65 72 members is deter
6c220 6d 69 6e 65 64 20 62 79 20 74 68 65 20 0a 20 2a mined by the . *
6c230 20 76 61 6c 75 65 20 6f 66 20 22 6f 70 22 20 61 value of "op" a
6c240 73 20 66 6f 6c 6c 6f 77 73 3a 0a 20 2a 0a 20 2a s follows:. *. *
6c250 20 28 6f 70 20 3d 3d 20 54 4b 5f 49 4e 53 45 52 (op == TK_INSER
6c260 54 29 0a 20 2a 20 6f 72 63 6f 6e 66 20 20 20 20 T). * orconf
6c270 2d 3e 20 73 74 6f 72 65 73 20 74 68 65 20 4f 4e -> stores the ON
6c280 20 43 4f 4e 46 4c 49 43 54 20 61 6c 67 6f 72 69 CONFLICT algori
6c290 74 68 6d 0a 20 2a 20 70 53 65 6c 65 63 74 20 20 thm. * pSelect
6c2a0 20 2d 3e 20 49 66 20 74 68 69 73 20 69 73 20 61 -> If this is a
6c2b0 6e 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 2e 2e n INSERT INTO ..
6c2c0 2e 20 53 45 4c 45 43 54 20 2e 2e 2e 20 73 74 61 . SELECT ... sta
6c2d0 74 65 6d 65 6e 74 2c 20 74 68 65 6e 0a 20 2a 20 tement, then. *
6c2e0 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68 69 thi
6c2f0 73 20 73 74 6f 72 65 73 20 61 20 70 6f 69 6e 74 s stores a point
6c300 65 72 20 74 6f 20 74 68 65 20 53 45 4c 45 43 54 er to the SELECT
6c310 20 73 74 61 74 65 6d 65 6e 74 2e 20 4f 74 68 65 statement. Othe
6c320 72 77 69 73 65 20 4e 55 4c 4c 2e 0a 20 2a 20 74 rwise NULL.. * t
6c330 61 72 67 65 74 20 20 20 20 2d 3e 20 41 20 74 6f arget -> A to
6c340 6b 65 6e 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 ken holding the
6c350 71 75 6f 74 65 64 20 6e 61 6d 65 20 6f 66 20 74 quoted name of t
6c360 68 65 20 74 61 62 6c 65 20 74 6f 20 69 6e 73 65 he table to inse
6c370 72 74 20 69 6e 74 6f 2e 0a 20 2a 20 70 45 78 70 rt into.. * pExp
6c380 72 4c 69 73 74 20 2d 3e 20 49 66 20 74 68 69 73 rList -> If this
6c390 20 69 73 20 61 6e 20 49 4e 53 45 52 54 20 49 4e is an INSERT IN
6c3a0 54 4f 20 2e 2e 2e 20 56 41 4c 55 45 53 20 2e 2e TO ... VALUES ..
6c3b0 2e 20 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 . statement, the
6c3c0 6e 0a 20 2a 20 20 20 20 20 20 20 20 20 20 20 20 n. *
6c3d0 20 20 74 68 69 73 20 73 74 6f 72 65 73 20 76 61 this stores va
6c3e0 6c 75 65 73 20 74 6f 20 62 65 20 69 6e 73 65 72 lues to be inser
6c3f0 74 65 64 2e 20 4f 74 68 65 72 77 69 73 65 20 4e ted. Otherwise N
6c400 55 4c 4c 2e 0a 20 2a 20 70 49 64 4c 69 73 74 20 ULL.. * pIdList
6c410 20 20 2d 3e 20 49 66 20 74 68 69 73 20 69 73 20 -> If this is
6c420 61 6e 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 2e an INSERT INTO .
6c430 2e 2e 20 28 3c 63 6f 6c 75 6d 6e 2d 6e 61 6d 65 .. (<column-name
6c440 73 3e 29 20 56 41 4c 55 45 53 20 2e 2e 2e 20 0a s>) VALUES ... .
6c450 20 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
6c460 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 6e 20 statement, then
6c470 74 68 69 73 20 73 74 6f 72 65 73 20 74 68 65 20 this stores the
6c480 63 6f 6c 75 6d 6e 2d 6e 61 6d 65 73 20 74 6f 20 column-names to
6c490 62 65 0a 20 2a 20 20 20 20 20 20 20 20 20 20 20 be. *
6c4a0 20 20 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f inserted into
6c4b0 2e 0a 20 2a 0a 20 2a 20 28 6f 70 20 3d 3d 20 54 .. *. * (op == T
6c4c0 4b 5f 44 45 4c 45 54 45 29 0a 20 2a 20 74 61 72 K_DELETE). * tar
6c4d0 67 65 74 20 20 20 20 2d 3e 20 41 20 74 6f 6b 65 get -> A toke
6c4e0 6e 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 71 75 n holding the qu
6c4f0 6f 74 65 64 20 6e 61 6d 65 20 6f 66 20 74 68 65 oted name of the
6c500 20 74 61 62 6c 65 20 74 6f 20 64 65 6c 65 74 65 table to delete
6c510 20 66 72 6f 6d 2e 0a 20 2a 20 70 57 68 65 72 65 from.. * pWhere
6c520 20 20 20 20 2d 3e 20 54 68 65 20 57 48 45 52 45 -> The WHERE
6c530 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20 44 clause of the D
6c540 45 4c 45 54 45 20 73 74 61 74 65 6d 65 6e 74 20 ELETE statement
6c550 69 66 20 6f 6e 65 20 69 73 20 73 70 65 63 69 66 if one is specif
6c560 69 65 64 2e 0a 20 2a 20 20 20 20 20 20 20 20 20 ied.. *
6c570 20 20 20 20 20 4f 74 68 65 72 77 69 73 65 20 4e Otherwise N
6c580 55 4c 4c 2e 0a 20 2a 20 0a 20 2a 20 28 6f 70 20 ULL.. * . * (op
6c590 3d 3d 20 54 4b 5f 55 50 44 41 54 45 29 0a 20 2a == TK_UPDATE). *
6c5a0 20 74 61 72 67 65 74 20 20 20 20 2d 3e 20 41 20 target -> A
6c5b0 74 6f 6b 65 6e 20 68 6f 6c 64 69 6e 67 20 74 68 token holding th
6c5c0 65 20 71 75 6f 74 65 64 20 6e 61 6d 65 20 6f 66 e quoted name of
6c5d0 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 75 70 the table to up
6c5e0 64 61 74 65 20 72 6f 77 73 20 6f 66 2e 0a 20 2a date rows of.. *
6c5f0 20 70 57 68 65 72 65 20 20 20 20 2d 3e 20 54 68 pWhere -> Th
6c600 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 6f e WHERE clause o
6c610 66 20 74 68 65 20 55 50 44 41 54 45 20 73 74 61 f the UPDATE sta
6c620 74 65 6d 65 6e 74 20 69 66 20 6f 6e 65 20 69 73 tement if one is
6c630 20 73 70 65 63 69 66 69 65 64 2e 0a 20 2a 20 20 specified.. *
6c640 20 20 20 20 20 20 20 20 20 20 20 20 4f 74 68 65 Othe
6c650 72 77 69 73 65 20 4e 55 4c 4c 2e 0a 20 2a 20 70 rwise NULL.. * p
6c660 45 78 70 72 4c 69 73 74 20 2d 3e 20 41 20 6c 69 ExprList -> A li
6c670 73 74 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e st of the column
6c680 73 20 74 6f 20 75 70 64 61 74 65 20 61 6e 64 20 s to update and
6c690 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20 the expressions
6c6a0 74 6f 20 75 70 64 61 74 65 0a 20 2a 20 20 20 20 to update. *
6c6b0 20 20 20 20 20 20 20 20 20 20 74 68 65 6d 20 74 them t
6c6c0 6f 2e 20 53 65 65 20 73 71 6c 69 74 65 33 55 70 o. See sqlite3Up
6c6d0 64 61 74 65 28 29 20 64 6f 63 75 6d 65 6e 74 61 date() documenta
6c6e0 74 69 6f 6e 20 6f 66 20 22 70 43 68 61 6e 67 65 tion of "pChange
6c6f0 73 22 0a 20 2a 20 20 20 20 20 20 20 20 20 20 20 s". *
6c700 20 20 20 61 72 67 75 6d 65 6e 74 2e 0a 20 2a 20 argument.. *
6c710 0a 20 2a 2f 0a 73 74 72 75 63 74 20 54 72 69 67 . */.struct Trig
6c720 67 65 72 53 74 65 70 20 7b 0a 20 20 75 38 20 6f gerStep {. u8 o
6c730 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 p;
6c740 20 2f 2a 20 4f 6e 65 20 6f 66 20 54 4b 5f 44 45 /* One of TK_DE
6c750 4c 45 54 45 2c 20 54 4b 5f 55 50 44 41 54 45 2c LETE, TK_UPDATE,
6c760 20 54 4b 5f 49 4e 53 45 52 54 2c 20 54 4b 5f 53 TK_INSERT, TK_S
6c770 45 4c 45 43 54 20 2a 2f 0a 20 20 75 38 20 6f 72 ELECT */. u8 or
6c780 63 6f 6e 66 3b 20 20 20 20 20 20 20 20 20 20 20 conf;
6c790 2f 2a 20 4f 45 5f 52 6f 6c 6c 62 61 63 6b 20 65 /* OE_Rollback e
6c7a0 74 63 2e 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 tc. */. Trigger
6c7b0 20 2a 70 54 72 69 67 3b 20 20 20 20 20 20 2f 2a *pTrig; /*
6c7c0 20 54 68 65 20 74 72 69 67 67 65 72 20 74 68 61 The trigger tha
6c7d0 74 20 74 68 69 73 20 73 74 65 70 20 69 73 20 61 t this step is a
6c7e0 20 70 61 72 74 20 6f 66 20 2a 2f 0a 20 20 53 65 part of */. Se
6c7f0 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 3b 20 20 lect *pSelect;
6c800 20 20 20 2f 2a 20 53 45 4c 45 43 54 20 73 74 61 /* SELECT sta
6c810 74 6d 65 6e 74 20 6f 72 20 52 48 53 20 6f 66 20 tment or RHS of
6c820 49 4e 53 45 52 54 20 49 4e 54 4f 20 2e 2e 20 53 INSERT INTO .. S
6c830 45 4c 45 43 54 20 2e 2e 2e 20 2a 2f 0a 20 20 54 ELECT ... */. T
6c840 6f 6b 65 6e 20 74 61 72 67 65 74 3b 20 20 20 20 oken target;
6c850 20 20 20 20 2f 2a 20 54 61 72 67 65 74 20 74 61 /* Target ta
6c860 62 6c 65 20 66 6f 72 20 44 45 4c 45 54 45 2c 20 ble for DELETE,
6c870 55 50 44 41 54 45 2c 20 49 4e 53 45 52 54 20 2a UPDATE, INSERT *
6c880 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 65 72 65 /. Expr *pWhere
6c890 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 ; /* The
6c8a0 57 48 45 52 45 20 63 6c 61 75 73 65 20 66 6f 72 WHERE clause for
6c8b0 20 44 45 4c 45 54 45 20 6f 72 20 55 50 44 41 54 DELETE or UPDAT
6c8c0 45 20 73 74 65 70 73 20 2a 2f 0a 20 20 45 78 70 E steps */. Exp
6c8d0 72 4c 69 73 74 20 2a 70 45 78 70 72 4c 69 73 74 rList *pExprList
6c8e0 3b 20 2f 2a 20 53 45 54 20 63 6c 61 75 73 65 20 ; /* SET clause
6c8f0 66 6f 72 20 55 50 44 41 54 45 2e 20 20 56 41 4c for UPDATE. VAL
6c900 55 45 53 20 63 6c 61 75 73 65 20 66 6f 72 20 49 UES clause for I
6c910 4e 53 45 52 54 20 2a 2f 0a 20 20 49 64 4c 69 73 NSERT */. IdLis
6c920 74 20 2a 70 49 64 4c 69 73 74 3b 20 20 20 20 20 t *pIdList;
6c930 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 /* Column names
6c940 66 6f 72 20 49 4e 53 45 52 54 20 2a 2f 0a 20 20 for INSERT */.
6c950 54 72 69 67 67 65 72 53 74 65 70 20 2a 70 4e 65 TriggerStep *pNe
6c960 78 74 3b 20 20 2f 2a 20 4e 65 78 74 20 69 6e 20 xt; /* Next in
6c970 74 68 65 20 6c 69 6e 6b 2d 6c 69 73 74 20 2a 2f the link-list */
6c980 0a 20 20 54 72 69 67 67 65 72 53 74 65 70 20 2a . TriggerStep *
6c990 70 4c 61 73 74 3b 20 20 2f 2a 20 4c 61 73 74 20 pLast; /* Last
6c9a0 65 6c 65 6d 65 6e 74 20 69 6e 20 6c 69 6e 6b 2d element in link-
6c9b0 6c 69 73 74 2e 20 56 61 6c 69 64 20 66 6f 72 20 list. Valid for
6c9c0 31 73 74 20 65 6c 65 6d 20 6f 6e 6c 79 20 2a 2f 1st elem only */
6c9d0 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 .};../*.** The f
6c9e0 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 ollowing structu
6c9f0 72 65 20 63 6f 6e 74 61 69 6e 73 20 69 6e 66 6f re contains info
6ca00 72 6d 61 74 69 6f 6e 20 75 73 65 64 20 62 79 20 rmation used by
6ca10 74 68 65 20 73 71 6c 69 74 65 46 69 78 2e 2e 2e the sqliteFix...
6ca20 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 61 73 20 .** routines as
6ca30 74 68 65 79 20 77 61 6c 6b 20 74 68 65 20 70 61 they walk the pa
6ca40 72 73 65 20 74 72 65 65 20 74 6f 20 6d 61 6b 65 rse tree to make
6ca50 20 64 61 74 61 62 61 73 65 20 72 65 66 65 72 65 database refere
6ca60 6e 63 65 73 0a 2a 2a 20 65 78 70 6c 69 63 69 74 nces.** explicit
6ca70 2e 20 20 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 . .*/.typedef s
6ca80 74 72 75 63 74 20 44 62 46 69 78 65 72 20 44 62 truct DbFixer Db
6ca90 46 69 78 65 72 3b 0a 73 74 72 75 63 74 20 44 62 Fixer;.struct Db
6caa0 46 69 78 65 72 20 7b 0a 20 20 50 61 72 73 65 20 Fixer {. Parse
6cab0 2a 70 50 61 72 73 65 3b 20 20 20 20 20 20 2f 2a *pParse; /*
6cac0 20 54 68 65 20 70 61 72 73 69 6e 67 20 63 6f 6e The parsing con
6cad0 74 65 78 74 2e 20 20 45 72 72 6f 72 20 6d 65 73 text. Error mes
6cae0 73 61 67 65 73 20 77 72 69 74 74 65 6e 20 68 65 sages written he
6caf0 72 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 re */. const ch
6cb00 61 72 20 2a 7a 44 62 3b 20 20 20 20 2f 2a 20 4d ar *zDb; /* M
6cb10 61 6b 65 20 73 75 72 65 20 61 6c 6c 20 6f 62 6a ake sure all obj
6cb20 65 63 74 73 20 61 72 65 20 63 6f 6e 74 61 69 6e ects are contain
6cb30 65 64 20 69 6e 20 74 68 69 73 20 64 61 74 61 62 ed in this datab
6cb40 61 73 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 ase */. const c
6cb50 68 61 72 20 2a 7a 54 79 70 65 3b 20 20 2f 2a 20 har *zType; /*
6cb60 54 79 70 65 20 6f 66 20 74 68 65 20 63 6f 6e 74 Type of the cont
6cb70 61 69 6e 65 72 20 2d 20 75 73 65 64 20 66 6f 72 ainer - used for
6cb80 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 error messages
6cb90 2a 2f 0a 20 20 63 6f 6e 73 74 20 54 6f 6b 65 6e */. const Token
6cba0 20 2a 70 4e 61 6d 65 3b 20 2f 2a 20 4e 61 6d 65 *pName; /* Name
6cbb0 20 6f 66 20 74 68 65 20 63 6f 6e 74 61 69 6e 65 of the containe
6cbc0 72 20 2d 20 75 73 65 64 20 66 6f 72 20 65 72 72 r - used for err
6cbd0 6f 72 20 6d 65 73 73 61 67 65 73 20 2a 2f 0a 7d or messages */.}
6cbe0 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 6f 62 6a 65 ;../*.** An obje
6cbf0 63 74 65 64 20 75 73 65 64 20 74 6f 20 61 63 63 cted used to acc
6cc00 75 6d 75 6c 61 74 65 20 74 68 65 20 74 65 78 74 umulate the text
6cc10 20 6f 66 20 61 20 73 74 72 69 6e 67 20 77 68 65 of a string whe
6cc20 72 65 20 77 65 0a 2a 2a 20 64 6f 20 6e 6f 74 20 re we.** do not
6cc30 6e 65 63 65 73 73 61 72 69 6c 79 20 6b 6e 6f 77 necessarily know
6cc40 20 68 6f 77 20 62 69 67 20 74 68 65 20 73 74 72 how big the str
6cc50 69 6e 67 20 77 69 6c 6c 20 62 65 20 69 6e 20 74 ing will be in t
6cc60 68 65 20 65 6e 64 2e 0a 2a 2f 0a 73 74 72 75 63 he end..*/.struc
6cc70 74 20 53 74 72 41 63 63 75 6d 20 7b 0a 20 20 73 t StrAccum {. s
6cc80 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 qlite3 *db;
6cc90 20 20 20 20 2f 2a 20 4f 70 74 69 6f 6e 61 6c 20 /* Optional
6cca0 64 61 74 61 62 61 73 65 20 66 6f 72 20 6c 6f 6f database for loo
6ccb0 6b 61 73 69 64 65 2e 20 20 43 61 6e 20 62 65 20 kaside. Can be
6ccc0 4e 55 4c 4c 20 2a 2f 0a 20 20 63 68 61 72 20 2a NULL */. char *
6ccd0 7a 42 61 73 65 3b 20 20 20 20 20 20 20 20 20 2f zBase; /
6cce0 2a 20 41 20 62 61 73 65 20 61 6c 6c 6f 63 61 74 * A base allocat
6ccf0 69 6f 6e 2e 20 20 4e 6f 74 20 66 72 6f 6d 20 6d ion. Not from m
6cd00 61 6c 6c 6f 63 2e 20 2a 2f 0a 20 20 63 68 61 72 alloc. */. char
6cd10 20 2a 7a 54 65 78 74 3b 20 20 20 20 20 20 20 20 *zText;
6cd20 20 2f 2a 20 54 68 65 20 73 74 72 69 6e 67 20 63 /* The string c
6cd30 6f 6c 6c 65 63 74 65 64 20 73 6f 20 66 61 72 20 ollected so far
6cd40 2a 2f 0a 20 20 69 6e 74 20 20 6e 43 68 61 72 3b */. int nChar;
6cd50 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e /* Len
6cd60 67 74 68 20 6f 66 20 74 68 65 20 73 74 72 69 6e gth of the strin
6cd70 67 20 73 6f 20 66 61 72 20 2a 2f 0a 20 20 69 6e g so far */. in
6cd80 74 20 20 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 20 t nAlloc;
6cd90 20 20 20 2f 2a 20 41 6d 6f 75 6e 74 20 6f 66 20 /* Amount of
6cda0 73 70 61 63 65 20 61 6c 6c 6f 63 61 74 65 64 20 space allocated
6cdb0 69 6e 20 7a 54 65 78 74 20 2a 2f 0a 20 20 69 6e in zText */. in
6cdc0 74 20 20 6d 78 41 6c 6c 6f 63 3b 20 20 20 20 20 t mxAlloc;
6cdd0 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 61 6c /* Maximum al
6cde0 6c 6f 77 65 64 20 73 74 72 69 6e 67 20 6c 65 6e lowed string len
6cdf0 67 74 68 20 2a 2f 0a 20 20 75 38 20 20 20 6d 61 gth */. u8 ma
6ce00 6c 6c 6f 63 46 61 69 6c 65 64 3b 20 20 20 2f 2a llocFailed; /*
6ce10 20 42 65 63 6f 6d 65 73 20 74 72 75 65 20 69 66 Becomes true if
6ce20 20 61 6e 79 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f any memory allo
6ce30 63 61 74 69 6f 6e 20 66 61 69 6c 73 20 2a 2f 0a cation fails */.
6ce40 20 20 75 38 20 20 20 75 73 65 4d 61 6c 6c 6f 63 u8 useMalloc
6ce50 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 ; /* True i
6ce60 66 20 7a 54 65 78 74 20 69 73 20 65 6e 6c 61 72 f zText is enlar
6ce70 67 65 61 62 6c 65 20 75 73 69 6e 67 20 72 65 61 geable using rea
6ce80 6c 6c 6f 63 20 2a 2f 0a 20 20 75 38 20 20 20 74 lloc */. u8 t
6ce90 6f 6f 42 69 67 3b 20 20 20 20 20 20 20 20 20 2f ooBig; /
6cea0 2a 20 42 65 63 6f 6d 65 73 20 74 72 75 65 20 69 * Becomes true i
6ceb0 66 20 73 74 72 69 6e 67 20 73 69 7a 65 20 65 78 f string size ex
6cec0 63 65 65 64 73 20 6c 69 6d 69 74 73 20 2a 2f 0a ceeds limits */.
6ced0 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 70 6f 69 6e };../*.** A poin
6cee0 74 65 72 20 74 6f 20 74 68 69 73 20 73 74 72 75 ter to this stru
6cef0 63 74 75 72 65 20 69 73 20 75 73 65 64 20 74 6f cture is used to
6cf00 20 63 6f 6d 6d 75 6e 69 63 61 74 65 20 69 6e 66 communicate inf
6cf10 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 66 72 6f 6d ormation.** from
6cf20 20 73 71 6c 69 74 65 33 49 6e 69 74 20 61 6e 64 sqlite3Init and
6cf30 20 4f 50 5f 50 61 72 73 65 53 63 68 65 6d 61 20 OP_ParseSchema
6cf40 69 6e 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33 into the sqlite3
6cf50 49 6e 69 74 43 61 6c 6c 62 61 63 6b 2e 0a 2a 2f InitCallback..*/
6cf60 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
6cf70 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b {. sqlite3 *db;
6cf80 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 /* The d
6cf90 61 74 61 62 61 73 65 20 62 65 69 6e 67 20 69 6e atabase being in
6cfa0 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 69 itialized */. i
6cfb0 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 20 nt iDb;
6cfc0 20 20 20 2f 2a 20 30 20 66 6f 72 20 6d 61 69 6e /* 0 for main
6cfd0 20 64 61 74 61 62 61 73 65 2e 20 20 31 20 66 6f database. 1 fo
6cfe0 72 20 54 45 4d 50 2c 20 32 2e 2e 20 66 6f 72 20 r TEMP, 2.. for
6cff0 41 54 54 41 43 48 65 64 20 2a 2f 0a 20 20 63 68 ATTACHed */. ch
6d000 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 3b 20 20 ar **pzErrMsg;
6d010 20 20 2f 2a 20 45 72 72 6f 72 20 6d 65 73 73 61 /* Error messa
6d020 67 65 20 73 74 6f 72 65 64 20 68 65 72 65 20 2a ge stored here *
6d030 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 /. int rc;
6d040 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c /* Resul
6d050 74 20 63 6f 64 65 20 73 74 6f 72 65 64 20 68 65 t code stored he
6d060 72 65 20 2a 2f 0a 7d 20 49 6e 69 74 44 61 74 61 re */.} InitData
6d070 3b 0a 0a 2f 2a 0a 2a 2a 20 53 74 72 75 63 74 75 ;../*.** Structu
6d080 72 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 67 6c re containing gl
6d090 6f 62 61 6c 20 63 6f 6e 66 69 67 75 72 61 74 69 obal configurati
6d0a0 6f 6e 20 64 61 74 61 20 66 6f 72 20 74 68 65 20 on data for the
6d0b0 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 2e 0a SQLite library..
6d0c0 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 74 72 75 63 **.** This struc
6d0d0 74 75 72 65 20 61 6c 73 6f 20 63 6f 6e 74 61 69 ture also contai
6d0e0 6e 73 20 73 6f 6d 65 20 73 74 61 74 65 20 69 6e ns some state in
6d0f0 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 formation..*/.st
6d100 72 75 63 74 20 53 71 6c 69 74 65 33 43 6f 6e 66 ruct Sqlite3Conf
6d110 69 67 20 7b 0a 20 20 69 6e 74 20 62 4d 65 6d 73 ig {. int bMems
6d120 74 61 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 tat;
6d130 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
6d140 20 74 6f 20 65 6e 61 62 6c 65 20 6d 65 6d 6f 72 to enable memor
6d150 79 20 73 74 61 74 75 73 20 2a 2f 0a 20 20 69 6e y status */. in
6d160 74 20 62 43 6f 72 65 4d 75 74 65 78 3b 20 20 20 t bCoreMutex;
6d170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6d180 2f 2a 20 54 72 75 65 20 74 6f 20 65 6e 61 62 6c /* True to enabl
6d190 65 20 63 6f 72 65 20 6d 75 74 65 78 69 6e 67 20 e core mutexing
6d1a0 2a 2f 0a 20 20 69 6e 74 20 62 46 75 6c 6c 4d 75 */. int bFullMu
6d1b0 74 65 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 tex;
6d1c0 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 /* True t
6d1d0 6f 20 65 6e 61 62 6c 65 20 66 75 6c 6c 20 6d 75 o enable full mu
6d1e0 74 65 78 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 texing */. int
6d1f0 6d 78 53 74 72 6c 65 6e 3b 20 20 20 20 20 20 20 mxStrlen;
6d200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
6d210 20 4d 61 78 69 6d 75 6d 20 73 74 72 69 6e 67 20 Maximum string
6d220 6c 65 6e 67 74 68 20 2a 2f 0a 20 20 69 6e 74 20 length */. int
6d230 73 7a 4c 6f 6f 6b 61 73 69 64 65 3b 20 20 20 20 szLookaside;
6d240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
6d250 20 44 65 66 61 75 6c 74 20 6c 6f 6f 6b 61 73 69 Default lookasi
6d260 64 65 20 62 75 66 66 65 72 20 73 69 7a 65 20 2a de buffer size *
6d270 2f 0a 20 20 69 6e 74 20 6e 4c 6f 6f 6b 61 73 69 /. int nLookasi
6d280 64 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 de;
6d290 20 20 20 20 20 20 2f 2a 20 44 65 66 61 75 6c 74 /* Default
6d2a0 20 6c 6f 6f 6b 61 73 69 64 65 20 62 75 66 66 65 lookaside buffe
6d2b0 72 20 63 6f 75 6e 74 20 2a 2f 0a 20 20 73 71 6c r count */. sql
6d2c0 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 ite3_mem_methods
6d2d0 20 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f m; /
6d2e0 2a 20 4c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f * Low-level memo
6d2f0 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e ry allocation in
6d300 74 65 72 66 61 63 65 20 2a 2f 0a 20 20 73 71 6c terface */. sql
6d310 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f ite3_mutex_metho
6d320 64 73 20 6d 75 74 65 78 3b 20 20 20 20 20 20 2f ds mutex; /
6d330 2a 20 4c 6f 77 2d 6c 65 76 65 6c 20 6d 75 74 65 * Low-level mute
6d340 78 20 69 6e 74 65 72 66 61 63 65 20 2a 2f 0a 20 x interface */.
6d350 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f sqlite3_pcache_
6d360 6d 65 74 68 6f 64 73 20 70 63 61 63 68 65 3b 20 methods pcache;
6d370 20 20 20 2f 2a 20 4c 6f 77 2d 6c 65 76 65 6c 20 /* Low-level
6d380 70 61 67 65 2d 63 61 63 68 65 20 69 6e 74 65 72 page-cache inter
6d390 66 61 63 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a face */. void *
6d3a0 70 48 65 61 70 3b 20 20 20 20 20 20 20 20 20 20 pHeap;
6d3b0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 /* H
6d3c0 65 61 70 20 73 74 6f 72 61 67 65 20 73 70 61 63 eap storage spac
6d3d0 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 48 65 61 70 e */. int nHeap
6d3e0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
6d3f0 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 /* Size
6d400 20 6f 66 20 70 48 65 61 70 5b 5d 20 2a 2f 0a 20 of pHeap[] */.
6d410 20 69 6e 74 20 6d 6e 52 65 71 2c 20 6d 78 52 65 int mnReq, mxRe
6d420 71 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 q;
6d430 20 20 20 2f 2a 20 4d 69 6e 20 61 6e 64 20 6d 61 /* Min and ma
6d440 78 20 68 65 61 70 20 72 65 71 75 65 73 74 73 20 x heap requests
6d450 73 69 7a 65 73 20 2a 2f 0a 20 20 76 6f 69 64 20 sizes */. void
6d460 2a 70 53 63 72 61 74 63 68 3b 20 20 20 20 20 20 *pScratch;
6d470 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
6d480 53 63 72 61 74 63 68 20 6d 65 6d 6f 72 79 20 2a Scratch memory *
6d490 2f 0a 20 20 69 6e 74 20 73 7a 53 63 72 61 74 63 /. int szScratc
6d4a0 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 h;
6d4b0 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 /* Size of
6d4c0 20 65 61 63 68 20 73 63 72 61 74 63 68 20 62 75 each scratch bu
6d4d0 66 66 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 ffer */. int nS
6d4e0 63 72 61 74 63 68 3b 20 20 20 20 20 20 20 20 20 cratch;
6d4f0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
6d500 75 6d 62 65 72 20 6f 66 20 73 63 72 61 74 63 68 umber of scratch
6d510 20 62 75 66 66 65 72 73 20 2a 2f 0a 20 20 76 6f buffers */. vo
6d520 69 64 20 2a 70 50 61 67 65 3b 20 20 20 20 20 20 id *pPage;
6d530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6d540 2f 2a 20 50 61 67 65 20 63 61 63 68 65 20 6d 65 /* Page cache me
6d550 6d 6f 72 79 20 2a 2f 0a 20 20 69 6e 74 20 73 7a mory */. int sz
6d560 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 Page;
6d570 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 /* S
6d580 69 7a 65 20 6f 66 20 65 61 63 68 20 70 61 67 65 ize of each page
6d590 20 69 6e 20 70 50 61 67 65 5b 5d 20 2a 2f 0a 20 in pPage[] */.
6d5a0 20 69 6e 74 20 6e 50 61 67 65 3b 20 20 20 20 20 int nPage;
6d5b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6d5c0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
6d5d0 70 61 67 65 73 20 69 6e 20 70 50 61 67 65 5b 5d pages in pPage[]
6d5e0 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 50 61 72 73 */. int mxPars
6d5f0 65 72 53 74 61 63 6b 3b 20 20 20 20 20 20 20 20 erStack;
6d600 20 20 20 20 20 20 20 20 2f 2a 20 6d 61 78 69 6d /* maxim
6d610 75 6d 20 64 65 70 74 68 20 6f 66 20 74 68 65 20 um depth of the
6d620 70 61 72 73 65 72 20 73 74 61 63 6b 20 2a 2f 0a parser stack */.
6d630 20 20 69 6e 74 20 73 68 61 72 65 64 43 61 63 68 int sharedCach
6d640 65 45 6e 61 62 6c 65 64 3b 20 20 20 20 20 20 20 eEnabled;
6d650 20 20 20 20 2f 2a 20 74 72 75 65 20 69 66 20 73 /* true if s
6d660 68 61 72 65 64 2d 63 61 63 68 65 20 6d 6f 64 65 hared-cache mode
6d670 20 65 6e 61 62 6c 65 64 20 2a 2f 0a 20 20 2f 2a enabled */. /*
6d680 20 54 68 65 20 61 62 6f 76 65 20 6d 69 67 68 74 The above might
6d690 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 be initialized
6d6a0 74 6f 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 20 54 68 to non-zero. Th
6d6b0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6e 65 65 64 e following need
6d6c0 20 74 6f 20 61 6c 77 61 79 73 0a 20 20 2a 2a 20 to always. **
6d6d0 69 6e 69 74 69 61 6c 6c 79 20 62 65 20 7a 65 72 initially be zer
6d6e0 6f 2c 20 68 6f 77 65 76 65 72 2e 20 2a 2f 0a 20 o, however. */.
6d6f0 20 69 6e 74 20 69 73 49 6e 69 74 3b 20 20 20 20 int isInit;
6d700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6d710 20 20 20 2f 2a 20 54 72 75 65 20 61 66 74 65 72 /* True after
6d720 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 initialization
6d730 68 61 73 20 66 69 6e 69 73 68 65 64 20 2a 2f 0a has finished */.
6d740 20 20 69 6e 74 20 69 6e 50 72 6f 67 72 65 73 73 int inProgress
6d750 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
6d760 20 20 20 20 2f 2a 20 54 72 75 65 20 77 68 69 6c /* True whil
6d770 65 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e e initialization
6d780 20 69 6e 20 70 72 6f 67 72 65 73 73 20 2a 2f 0a in progress */.
6d790 20 20 69 6e 74 20 69 73 4d 75 74 65 78 49 6e 69 int isMutexIni
6d7a0 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t;
6d7b0 20 20 20 20 2f 2a 20 54 72 75 65 20 61 66 74 65 /* True afte
6d7c0 72 20 6d 75 74 65 78 65 73 20 61 72 65 20 69 6e r mutexes are in
6d7d0 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 69 itialized */. i
6d7e0 6e 74 20 69 73 4d 61 6c 6c 6f 63 49 6e 69 74 3b nt isMallocInit;
6d7f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6d800 20 2f 2a 20 54 72 75 65 20 61 66 74 65 72 20 6d /* True after m
6d810 61 6c 6c 6f 63 20 69 73 20 69 6e 69 74 69 61 6c alloc is initial
6d820 69 7a 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 73 ized */. int is
6d830 50 43 61 63 68 65 49 6e 69 74 3b 20 20 20 20 20 PCacheInit;
6d840 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
6d850 72 75 65 20 61 66 74 65 72 20 6d 61 6c 6c 6f 63 rue after malloc
6d860 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 is initialized
6d870 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 */. sqlite3_mut
6d880 65 78 20 2a 70 49 6e 69 74 4d 75 74 65 78 3b 20 ex *pInitMutex;
6d890 20 20 20 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 /* Mutex
6d8a0 75 73 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f used by sqlite3_
6d8b0 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 2a 2f 0a initialize() */.
6d8c0 20 20 69 6e 74 20 6e 52 65 66 49 6e 69 74 4d 75 int nRefInitMu
6d8d0 74 65 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 tex;
6d8e0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
6d8f0 20 75 73 65 72 73 20 6f 66 20 70 49 6e 69 74 4d users of pInitM
6d900 75 74 65 78 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a utex */.};../*.*
6d910 2a 20 43 6f 6e 74 65 78 74 20 70 6f 69 6e 74 65 * Context pointe
6d920 72 20 70 61 73 73 65 64 20 64 6f 77 6e 20 74 68 r passed down th
6d930 72 6f 75 67 68 20 74 68 65 20 74 72 65 65 2d 77 rough the tree-w
6d940 61 6c 6b 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 57 alk..*/.struct W
6d950 61 6c 6b 65 72 20 7b 0a 20 20 69 6e 74 20 28 2a alker {. int (*
6d960 78 45 78 70 72 43 61 6c 6c 62 61 63 6b 29 28 57 xExprCallback)(W
6d970 61 6c 6b 65 72 2a 2c 20 45 78 70 72 2a 29 3b 20 alker*, Expr*);
6d980 20 20 20 20 2f 2a 20 43 61 6c 6c 62 61 63 6b 20 /* Callback
6d990 66 6f 72 20 65 78 70 72 65 73 73 69 6f 6e 73 20 for expressions
6d9a0 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 53 65 6c 65 */. int (*xSele
6d9b0 63 74 43 61 6c 6c 62 61 63 6b 29 28 57 61 6c 6b ctCallback)(Walk
6d9c0 65 72 2a 2c 53 65 6c 65 63 74 2a 29 3b 20 20 2f er*,Select*); /
6d9d0 2a 20 43 61 6c 6c 62 61 63 6b 20 66 6f 72 20 53 * Callback for S
6d9e0 45 4c 45 43 54 73 20 2a 2f 0a 20 20 50 61 72 73 ELECTs */. Pars
6d9f0 65 20 2a 70 50 61 72 73 65 3b 20 20 20 20 20 20 e *pParse;
6da00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6da10 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65 72 20 /* Parser
6da20 63 6f 6e 74 65 78 74 2e 20 20 2a 2f 0a 20 20 75 context. */. u
6da30 6e 69 6f 6e 20 7b 20 20 20 20 20 20 20 20 20 20 nion {
6da40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6da50 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 74 72 /* Extr
6da60 61 20 64 61 74 61 20 66 6f 72 20 63 61 6c 6c 62 a data for callb
6da70 61 63 6b 20 2a 2f 0a 20 20 20 20 4e 61 6d 65 43 ack */. NameC
6da80 6f 6e 74 65 78 74 20 2a 70 4e 43 3b 20 20 20 20 ontext *pNC;
6da90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6daa0 20 20 20 20 20 20 2f 2a 20 4e 61 6d 69 6e 67 20 /* Naming
6dab0 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 20 20 69 context */. i
6dac0 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 nt i;
6dad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6dae0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 74 /* Int
6daf0 65 67 65 72 20 76 61 6c 75 65 20 2a 2f 0a 20 20 eger value */.
6db00 7d 20 75 3b 0a 7d 3b 0a 0a 2f 2a 20 46 6f 72 77 } u;.};../* Forw
6db10 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 ard declarations
6db20 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 */.SQLITE_PRIVA
6db30 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57 61 TE int sqlite3Wa
6db40 6c 6b 45 78 70 72 28 57 61 6c 6b 65 72 2a 2c 20 lkExpr(Walker*,
6db50 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 Expr*);.SQLITE_P
6db60 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
6db70 65 33 57 61 6c 6b 45 78 70 72 4c 69 73 74 28 57 e3WalkExprList(W
6db80 61 6c 6b 65 72 2a 2c 20 45 78 70 72 4c 69 73 74 alker*, ExprList
6db90 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
6dba0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57 61 TE int sqlite3Wa
6dbb0 6c 6b 53 65 6c 65 63 74 28 57 61 6c 6b 65 72 2a lkSelect(Walker*
6dbc0 2c 20 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 , Select*);.SQLI
6dbd0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
6dbe0 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 qlite3WalkSelect
6dbf0 45 78 70 72 28 57 61 6c 6b 65 72 2a 2c 20 53 65 Expr(Walker*, Se
6dc00 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 lect*);.SQLITE_P
6dc10 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
6dc20 65 33 57 61 6c 6b 53 65 6c 65 63 74 46 72 6f 6d e3WalkSelectFrom
6dc30 28 57 61 6c 6b 65 72 2a 2c 20 53 65 6c 65 63 74 (Walker*, Select
6dc40 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 *);../*.** Retur
6dc50 6e 20 63 6f 64 65 20 66 72 6f 6d 20 74 68 65 20 n code from the
6dc60 70 61 72 73 65 2d 74 72 65 65 20 77 61 6c 6b 69 parse-tree walki
6dc70 6e 67 20 70 72 69 6d 69 74 69 76 65 73 20 61 6e ng primitives an
6dc80 64 20 74 68 65 69 72 0a 2a 2a 20 63 61 6c 6c 62 d their.** callb
6dc90 61 63 6b 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 acks..*/.#define
6dca0 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 20 20 20 WRC_Continue
6dcb0 20 30 20 20 20 2f 2a 20 43 6f 6e 74 69 6e 75 65 0 /* Continue
6dcc0 20 64 6f 77 6e 20 69 6e 74 6f 20 63 68 69 6c 64 down into child
6dcd0 72 65 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 ren */.#define W
6dce0 52 43 5f 50 72 75 6e 65 20 20 20 20 20 20 20 31 RC_Prune 1
6dcf0 20 20 20 2f 2a 20 4f 6d 69 74 20 63 68 69 6c 64 /* Omit child
6dd00 72 65 6e 20 62 75 74 20 63 6f 6e 74 69 6e 75 65 ren but continue
6dd10 20 77 61 6c 6b 69 6e 67 20 73 69 62 6c 69 6e 67 walking sibling
6dd20 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 52 43 s */.#define WRC
6dd30 5f 41 62 6f 72 74 20 20 20 20 20 20 20 32 20 20 _Abort 2
6dd40 20 2f 2a 20 41 62 61 6e 64 6f 6e 20 74 68 65 20 /* Abandon the
6dd50 74 72 65 65 20 77 61 6c 6b 20 2a 2f 0a 0a 2f 2a tree walk */../*
6dd60 0a 2a 2a 20 41 73 73 75 6d 69 6e 67 20 7a 49 6e .** Assuming zIn
6dd70 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 66 points to the f
6dd80 69 72 73 74 20 62 79 74 65 20 6f 66 20 61 20 55 irst byte of a U
6dd90 54 46 2d 38 20 63 68 61 72 61 63 74 65 72 2c 0a TF-8 character,.
6dda0 2a 2a 20 61 64 76 61 6e 63 65 20 7a 49 6e 20 74 ** advance zIn t
6ddb0 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 66 o point to the f
6ddc0 69 72 73 74 20 62 79 74 65 20 6f 66 20 74 68 65 irst byte of the
6ddd0 20 6e 65 78 74 20 55 54 46 2d 38 20 63 68 61 72 next UTF-8 char
6dde0 61 63 74 65 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e acter..*/.#defin
6ddf0 65 20 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55 54 e SQLITE_SKIP_UT
6de00 46 38 28 7a 49 6e 29 20 7b 20 20 20 20 20 20 20 F8(zIn) {
6de10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6de20 20 5c 0a 20 20 69 66 28 20 28 2a 28 7a 49 6e 2b \. if( (*(zIn+
6de30 2b 29 29 3e 3d 30 78 63 30 20 29 7b 20 20 20 20 +))>=0xc0 ){
6de40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6de50 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 \.
6de60 77 68 69 6c 65 28 20 28 2a 7a 49 6e 20 26 20 30 while( (*zIn & 0
6de70 78 63 30 29 3d 3d 30 78 38 30 20 29 7b 20 7a 49 xc0)==0x80 ){ zI
6de80 6e 2b 2b 3b 20 7d 20 20 20 20 20 20 20 20 20 20 n++; }
6de90 20 20 20 5c 0a 20 20 7d 20 20 20 20 20 20 20 20 \. }
6dea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6deb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6dec0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 7d 0a \.}.
6ded0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 ./*.** The SQLIT
6dee0 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 20 6d E_CORRUPT_BKPT m
6def0 61 63 72 6f 20 63 61 6e 20 62 65 20 65 69 74 68 acro can be eith
6df00 65 72 20 61 20 63 6f 6e 73 74 61 6e 74 20 28 66 er a constant (f
6df10 6f 72 20 70 72 6f 64 75 63 74 69 6f 6e 0a 2a 2a or production.**
6df20 20 62 75 69 6c 64 73 29 20 6f 72 20 61 20 66 75 builds) or a fu
6df30 6e 63 74 69 6f 6e 20 63 61 6c 6c 20 28 66 6f 72 nction call (for
6df40 20 64 65 62 75 67 67 69 6e 67 29 2e 20 20 49 66 debugging). If
6df50 20 69 74 20 69 73 20 61 20 66 75 6e 63 74 69 6f it is a functio
6df60 6e 20 63 61 6c 6c 2c 0a 2a 2a 20 69 74 20 61 6c n call,.** it al
6df70 6c 6f 77 73 20 74 68 65 20 6f 70 65 72 61 74 6f lows the operato
6df80 72 20 74 6f 20 73 65 74 20 61 20 62 72 65 61 6b r to set a break
6df90 70 6f 69 6e 74 20 61 74 20 74 68 65 20 73 70 6f point at the spo
6dfa0 74 20 77 68 65 72 65 20 64 61 74 61 62 61 73 65 t where database
6dfb0 0a 2a 2a 20 63 6f 72 72 75 70 74 69 6f 6e 20 69 .** corruption i
6dfc0 73 20 66 69 72 73 74 20 64 65 74 65 63 74 65 64 s first detected
6dfd0 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 ..*/.#ifdef SQLI
6dfe0 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 5f TE_DEBUG.SQLITE_
6dff0 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 PRIVATE int sq
6e000 6c 69 74 65 33 43 6f 72 72 75 70 74 28 76 6f 69 lite3Corrupt(voi
6e010 64 29 3b 0a 23 20 64 65 66 69 6e 65 20 53 51 4c d);.# define SQL
6e020 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
6e030 20 73 71 6c 69 74 65 33 43 6f 72 72 75 70 74 28 sqlite3Corrupt(
6e040 29 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 ).#else.# define
6e050 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
6e060 42 4b 50 54 20 53 51 4c 49 54 45 5f 43 4f 52 52 BKPT SQLITE_CORR
6e070 55 50 54 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a UPT.#endif../*.*
6e080 2a 20 54 68 65 20 63 74 79 70 65 2e 68 20 68 65 * The ctype.h he
6e090 61 64 65 72 20 69 73 20 6e 65 65 64 65 64 20 66 ader is needed f
6e0a0 6f 72 20 6e 6f 6e 2d 41 53 43 49 49 20 73 79 73 or non-ASCII sys
6e0b0 74 65 6d 73 2e 20 20 49 74 20 69 73 20 61 6c 73 tems. It is als
6e0c0 6f 0a 2a 2a 20 6e 65 65 64 65 64 20 62 79 20 46 o.** needed by F
6e0d0 54 53 33 20 77 68 65 6e 20 46 54 53 33 20 69 73 TS3 when FTS3 is
6e0e0 20 69 6e 63 6c 75 64 65 64 20 69 6e 20 74 68 65 included in the
6e0f0 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e 2e 0a 2a amalgamation..*
6e100 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 /.#if !defined(S
6e110 51 4c 49 54 45 5f 41 53 43 49 49 29 20 7c 7c 20 QLITE_ASCII) ||
6e120 5c 0a 20 20 20 20 28 64 65 66 69 6e 65 64 28 53 \. (defined(S
6e130 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 QLITE_ENABLE_FTS
6e140 33 29 20 26 26 20 64 65 66 69 6e 65 64 28 53 51 3) && defined(SQ
6e150 4c 49 54 45 5f 41 4d 41 4c 47 41 4d 41 54 49 4f LITE_AMALGAMATIO
6e160 4e 29 29 0a 23 20 69 6e 63 6c 75 64 65 20 3c 63 N)).# include <c
6e170 74 79 70 65 2e 68 3e 0a 23 65 6e 64 69 66 0a 0a type.h>.#endif..
6e180 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 /*.** The follow
6e190 69 6e 67 20 6d 61 63 72 6f 73 20 6d 69 6d 69 63 ing macros mimic
6e1a0 20 74 68 65 20 73 74 61 6e 64 61 72 64 20 6c 69 the standard li
6e1b0 62 72 61 72 79 20 66 75 6e 63 74 69 6f 6e 73 20 brary functions
6e1c0 74 6f 75 70 70 65 72 28 29 2c 0a 2a 2a 20 69 73 toupper(),.** is
6e1d0 73 70 61 63 65 28 29 2c 20 69 73 61 6c 6e 75 6d space(), isalnum
6e1e0 28 29 2c 20 69 73 64 69 67 69 74 28 29 20 61 6e (), isdigit() an
6e1f0 64 20 69 73 78 64 69 67 69 74 28 29 2c 20 72 65 d isxdigit(), re
6e200 73 70 65 63 74 69 76 65 6c 79 2e 20 54 68 65 0a spectively. The.
6e210 2a 2a 20 73 71 6c 69 74 65 20 76 65 72 73 69 6f ** sqlite versio
6e220 6e 73 20 6f 6e 6c 79 20 77 6f 72 6b 20 66 6f 72 ns only work for
6e230 20 41 53 43 49 49 20 63 68 61 72 61 63 74 65 72 ASCII character
6e240 73 2c 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66 s, regardless of
6e250 20 6c 6f 63 61 6c 65 2e 0a 2a 2f 0a 23 69 66 64 locale..*/.#ifd
6e260 65 66 20 53 51 4c 49 54 45 5f 41 53 43 49 49 0a ef SQLITE_ASCII.
6e270 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 # define sqlite3
6e280 54 6f 75 70 70 65 72 28 78 29 20 20 28 28 78 29 Toupper(x) ((x)
6e290 26 7e 28 73 71 6c 69 74 65 33 43 74 79 70 65 4d &~(sqlite3CtypeM
6e2a0 61 70 5b 28 75 6e 73 69 67 6e 65 64 20 63 68 61 ap[(unsigned cha
6e2b0 72 29 28 78 29 5d 26 30 78 32 30 29 29 0a 23 20 r)(x)]&0x20)).#
6e2c0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 49 73 define sqlite3Is
6e2d0 73 70 61 63 65 28 78 29 20 20 20 28 73 71 6c 69 space(x) (sqli
6e2e0 74 65 33 43 74 79 70 65 4d 61 70 5b 28 75 6e 73 te3CtypeMap[(uns
6e2f0 69 67 6e 65 64 20 63 68 61 72 29 28 78 29 5d 26 igned char)(x)]&
6e300 30 78 30 31 29 0a 23 20 64 65 66 69 6e 65 20 73 0x01).# define s
6e310 71 6c 69 74 65 33 49 73 61 6c 6e 75 6d 28 78 29 qlite3Isalnum(x)
6e320 20 20 20 28 73 71 6c 69 74 65 33 43 74 79 70 65 (sqlite3Ctype
6e330 4d 61 70 5b 28 75 6e 73 69 67 6e 65 64 20 63 68 Map[(unsigned ch
6e340 61 72 29 28 78 29 5d 26 30 78 30 36 29 0a 23 20 ar)(x)]&0x06).#
6e350 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 49 73 define sqlite3Is
6e360 61 6c 70 68 61 28 78 29 20 20 20 28 73 71 6c 69 alpha(x) (sqli
6e370 74 65 33 43 74 79 70 65 4d 61 70 5b 28 75 6e 73 te3CtypeMap[(uns
6e380 69 67 6e 65 64 20 63 68 61 72 29 28 78 29 5d 26 igned char)(x)]&
6e390 30 78 30 32 29 0a 23 20 64 65 66 69 6e 65 20 73 0x02).# define s
6e3a0 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 78 29 qlite3Isdigit(x)
6e3b0 20 20 20 28 73 71 6c 69 74 65 33 43 74 79 70 65 (sqlite3Ctype
6e3c0 4d 61 70 5b 28 75 6e 73 69 67 6e 65 64 20 63 68 Map[(unsigned ch
6e3d0 61 72 29 28 78 29 5d 26 30 78 30 34 29 0a 23 20 ar)(x)]&0x04).#
6e3e0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 49 73 define sqlite3Is
6e3f0 78 64 69 67 69 74 28 78 29 20 20 28 73 71 6c 69 xdigit(x) (sqli
6e400 74 65 33 43 74 79 70 65 4d 61 70 5b 28 75 6e 73 te3CtypeMap[(uns
6e410 69 67 6e 65 64 20 63 68 61 72 29 28 78 29 5d 26 igned char)(x)]&
6e420 30 78 30 38 29 0a 23 20 64 65 66 69 6e 65 20 73 0x08).# define s
6e430 71 6c 69 74 65 33 54 6f 6c 6f 77 65 72 28 78 29 qlite3Tolower(x)
6e440 20 20 20 28 73 71 6c 69 74 65 33 55 70 70 65 72 (sqlite3Upper
6e450 54 6f 4c 6f 77 65 72 5b 28 75 6e 73 69 67 6e 65 ToLower[(unsigne
6e460 64 20 63 68 61 72 29 28 78 29 5d 29 0a 23 65 6c d char)(x)]).#el
6e470 73 65 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 se.# define sqli
6e480 74 65 33 54 6f 75 70 70 65 72 28 78 29 20 20 20 te3Toupper(x)
6e490 74 6f 75 70 70 65 72 28 28 75 6e 73 69 67 6e 65 toupper((unsigne
6e4a0 64 20 63 68 61 72 29 28 78 29 29 0a 23 20 64 65 d char)(x)).# de
6e4b0 66 69 6e 65 20 73 71 6c 69 74 65 33 49 73 73 70 fine sqlite3Issp
6e4c0 61 63 65 28 78 29 20 20 20 69 73 73 70 61 63 65 ace(x) isspace
6e4d0 28 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 ((unsigned char)
6e4e0 28 78 29 29 0a 23 20 64 65 66 69 6e 65 20 73 71 (x)).# define sq
6e4f0 6c 69 74 65 33 49 73 61 6c 6e 75 6d 28 78 29 20 lite3Isalnum(x)
6e500 20 20 69 73 61 6c 6e 75 6d 28 28 75 6e 73 69 67 isalnum((unsig
6e510 6e 65 64 20 63 68 61 72 29 28 78 29 29 0a 23 20 ned char)(x)).#
6e520 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 49 73 define sqlite3Is
6e530 61 6c 70 68 61 28 78 29 20 20 20 69 73 61 6c 70 alpha(x) isalp
6e540 68 61 28 28 75 6e 73 69 67 6e 65 64 20 63 68 61 ha((unsigned cha
6e550 72 29 28 78 29 29 0a 23 20 64 65 66 69 6e 65 20 r)(x)).# define
6e560 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 78 sqlite3Isdigit(x
6e570 29 20 20 20 69 73 64 69 67 69 74 28 28 75 6e 73 ) isdigit((uns
6e580 69 67 6e 65 64 20 63 68 61 72 29 28 78 29 29 0a igned char)(x)).
6e590 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 # define sqlite3
6e5a0 49 73 78 64 69 67 69 74 28 78 29 20 20 69 73 78 Isxdigit(x) isx
6e5b0 64 69 67 69 74 28 28 75 6e 73 69 67 6e 65 64 20 digit((unsigned
6e5c0 63 68 61 72 29 28 78 29 29 0a 23 20 64 65 66 69 char)(x)).# defi
6e5d0 6e 65 20 73 71 6c 69 74 65 33 54 6f 6c 6f 77 65 ne sqlite3Tolowe
6e5e0 72 28 78 29 20 20 20 74 6f 6c 6f 77 65 72 28 28 r(x) tolower((
6e5f0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28 78 unsigned char)(x
6e600 29 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a )).#endif../*.**
6e610 20 49 6e 74 65 72 6e 61 6c 20 66 75 6e 63 74 69 Internal functi
6e620 6f 6e 20 70 72 6f 74 6f 74 79 70 65 73 0a 2a 2f on prototypes.*/
6e630 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6e640 69 6e 74 20 73 71 6c 69 74 65 33 53 74 72 49 43 int sqlite3StrIC
6e650 6d 70 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c mp(const char *,
6e660 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b 0a const char *);.
6e670 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
6e680 6e 74 20 73 71 6c 69 74 65 33 49 73 4e 75 6d 62 nt sqlite3IsNumb
6e690 65 72 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 er(const char*,
6e6a0 69 6e 74 2a 2c 20 75 38 29 3b 0a 53 51 4c 49 54 int*, u8);.SQLIT
6e6b0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
6e6c0 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 63 6f lite3Strlen30(co
6e6d0 6e 73 74 20 63 68 61 72 2a 29 3b 0a 23 64 65 66 nst char*);.#def
6e6e0 69 6e 65 20 73 71 6c 69 74 65 33 53 74 72 4e 49 ine sqlite3StrNI
6e6f0 43 6d 70 20 73 71 6c 69 74 65 33 5f 73 74 72 6e Cmp sqlite3_strn
6e700 69 63 6d 70 0a 0a 53 51 4c 49 54 45 5f 50 52 49 icmp..SQLITE_PRI
6e710 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
6e720 4d 61 6c 6c 6f 63 49 6e 69 74 28 76 6f 69 64 29 MallocInit(void)
6e730 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6e740 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 61 6c void sqlite3Mal
6e750 6c 6f 63 45 6e 64 28 76 6f 69 64 29 3b 0a 53 51 locEnd(void);.SQ
6e760 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
6e770 64 20 2a 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 d *sqlite3Malloc
6e780 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 (int);.SQLITE_PR
6e790 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 IVATE void *sqli
6e7a0 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 69 6e te3MallocZero(in
6e7b0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
6e7c0 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 TE void *sqlite3
6e7d0 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 71 6c DbMallocZero(sql
6e7e0 69 74 65 33 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c ite3*, int);.SQL
6e7f0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
6e800 20 2a 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f *sqlite3DbMallo
6e810 63 52 61 77 28 73 71 6c 69 74 65 33 2a 2c 20 69 cRaw(sqlite3*, i
6e820 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
6e830 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 ATE char *sqlite
6e840 33 44 62 53 74 72 44 75 70 28 73 71 6c 69 74 65 3DbStrDup(sqlite
6e850 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 3*,const char*);
6e860 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6e870 63 68 61 72 20 2a 73 71 6c 69 74 65 33 44 62 53 char *sqlite3DbS
6e880 74 72 4e 44 75 70 28 73 71 6c 69 74 65 33 2a 2c trNDup(sqlite3*,
6e890 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 const char*, int
6e8a0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6e8b0 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 52 E void *sqlite3R
6e8c0 65 61 6c 6c 6f 63 28 76 6f 69 64 2a 2c 20 69 6e ealloc(void*, in
6e8d0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
6e8e0 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 TE void *sqlite3
6e8f0 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 DbReallocOrFree(
6e900 73 71 6c 69 74 65 33 20 2a 2c 20 76 6f 69 64 20 sqlite3 *, void
6e910 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f *, int);.SQLITE_
6e920 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 PRIVATE void *sq
6e930 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 73 lite3DbRealloc(s
6e940 71 6c 69 74 65 33 20 2a 2c 20 76 6f 69 64 20 2a qlite3 *, void *
6e950 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
6e960 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
6e970 74 65 33 44 62 46 72 65 65 28 73 71 6c 69 74 65 te3DbFree(sqlite
6e980 33 2a 2c 20 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 3*, void*);.SQLI
6e990 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
6e9a0 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 qlite3MallocSize
6e9b0 28 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f (void*);.SQLITE_
6e9c0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
6e9d0 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 te3DbMallocSize(
6e9e0 73 71 6c 69 74 65 33 2a 2c 20 76 6f 69 64 2a 29 sqlite3*, void*)
6e9f0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6ea00 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 53 63 void *sqlite3Sc
6ea10 72 61 74 63 68 4d 61 6c 6c 6f 63 28 69 6e 74 29 ratchMalloc(int)
6ea20 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6ea30 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 63 72 void sqlite3Scr
6ea40 61 74 63 68 46 72 65 65 28 76 6f 69 64 2a 29 3b atchFree(void*);
6ea50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6ea60 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61 67 void *sqlite3Pag
6ea70 65 4d 61 6c 6c 6f 63 28 69 6e 74 29 3b 0a 53 51 eMalloc(int);.SQ
6ea80 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
6ea90 64 20 73 71 6c 69 74 65 33 50 61 67 65 46 72 65 d sqlite3PageFre
6eaa0 65 28 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45 e(void*);.SQLITE
6eab0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
6eac0 6c 69 74 65 33 4d 65 6d 53 65 74 44 65 66 61 75 lite3MemSetDefau
6ead0 6c 74 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 lt(void);.SQLITE
6eae0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
6eaf0 6c 69 74 65 33 42 65 6e 69 67 6e 4d 61 6c 6c 6f lite3BenignMallo
6eb00 63 48 6f 6f 6b 73 28 76 6f 69 64 20 28 2a 29 28 cHooks(void (*)(
6eb10 76 6f 69 64 29 2c 20 76 6f 69 64 20 28 2a 29 28 void), void (*)(
6eb20 76 6f 69 64 29 29 3b 0a 53 51 4c 49 54 45 5f 50 void));.SQLITE_P
6eb30 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
6eb40 65 33 4d 65 6d 6f 72 79 41 6c 61 72 6d 28 76 6f e3MemoryAlarm(vo
6eb50 69 64 20 28 2a 29 28 76 6f 69 64 2a 2c 20 73 71 id (*)(void*, sq
6eb60 6c 69 74 65 33 5f 69 6e 74 36 34 2c 20 69 6e 74 lite3_int64, int
6eb70 29 2c 20 76 6f 69 64 2a 2c 20 73 71 6c 69 74 65 ), void*, sqlite
6eb80 33 5f 69 6e 74 36 34 29 3b 0a 0a 2f 2a 0a 2a 2a 3_int64);../*.**
6eb90 20 4f 6e 20 73 79 73 74 65 6d 73 20 77 69 74 68 On systems with
6eba0 20 61 6d 70 6c 65 20 73 74 61 63 6b 20 73 70 61 ample stack spa
6ebb0 63 65 20 61 6e 64 20 74 68 61 74 20 73 75 70 70 ce and that supp
6ebc0 6f 72 74 20 61 6c 6c 6f 63 61 28 29 2c 20 6d 61 ort alloca(), ma
6ebd0 6b 65 0a 2a 2a 20 75 73 65 20 6f 66 20 61 6c 6c ke.** use of all
6ebe0 6f 63 61 28 29 20 74 6f 20 6f 62 74 61 69 6e 20 oca() to obtain
6ebf0 73 70 61 63 65 20 66 6f 72 20 6c 61 72 67 65 20 space for large
6ec00 61 75 74 6f 6d 61 74 69 63 20 6f 62 6a 65 63 74 automatic object
6ec10 73 2e 20 20 42 79 20 64 65 66 61 75 6c 74 2c 0a s. By default,.
6ec20 2a 2a 20 6f 62 74 61 69 6e 20 73 70 61 63 65 20 ** obtain space
6ec30 66 72 6f 6d 20 6d 61 6c 6c 6f 63 28 29 2e 0a 2a from malloc()..*
6ec40 2a 0a 2a 2a 20 54 68 65 20 61 6c 6c 6f 63 61 28 *.** The alloca(
6ec50 29 20 72 6f 75 74 69 6e 65 20 6e 65 76 65 72 20 ) routine never
6ec60 72 65 74 75 72 6e 73 20 4e 55 4c 4c 2e 20 20 54 returns NULL. T
6ec70 68 69 73 20 77 69 6c 6c 20 63 61 75 73 65 20 63 his will cause c
6ec80 6f 64 65 20 70 61 74 68 73 0a 2a 2a 20 74 68 61 ode paths.** tha
6ec90 74 20 64 65 61 6c 20 77 69 74 68 20 73 71 6c 69 t deal with sqli
6eca0 74 65 33 53 74 61 63 6b 41 6c 6c 6f 63 28 29 20 te3StackAlloc()
6ecb0 66 61 69 6c 75 72 65 73 20 74 6f 20 62 65 20 75 failures to be u
6ecc0 6e 72 65 61 63 68 61 62 6c 65 2e 0a 2a 2f 0a 23 nreachable..*/.#
6ecd0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 55 53 45 ifdef SQLITE_USE
6ece0 5f 41 4c 4c 4f 43 41 0a 23 20 64 65 66 69 6e 65 _ALLOCA.# define
6ecf0 20 73 71 6c 69 74 65 33 53 74 61 63 6b 41 6c 6c sqlite3StackAll
6ed00 6f 63 52 61 77 28 44 2c 4e 29 20 20 20 61 6c 6c ocRaw(D,N) all
6ed10 6f 63 61 28 4e 29 0a 23 20 64 65 66 69 6e 65 20 oca(N).# define
6ed20 73 71 6c 69 74 65 33 53 74 61 63 6b 41 6c 6c 6f sqlite3StackAllo
6ed30 63 5a 65 72 6f 28 44 2c 4e 29 20 20 6d 65 6d 73 cZero(D,N) mems
6ed40 65 74 28 61 6c 6c 6f 63 61 28 4e 29 2c 20 30 2c et(alloca(N), 0,
6ed50 20 4e 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c N).# define sql
6ed60 69 74 65 33 53 74 61 63 6b 46 72 65 65 28 44 2c ite3StackFree(D,
6ed70 50 29 20 20 20 20 20 20 20 0a 23 65 6c 73 65 0a P) .#else.
6ed80 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 # define sqlite3
6ed90 53 74 61 63 6b 41 6c 6c 6f 63 52 61 77 28 44 2c StackAllocRaw(D,
6eda0 4e 29 20 20 20 73 71 6c 69 74 65 33 44 62 4d 61 N) sqlite3DbMa
6edb0 6c 6c 6f 63 52 61 77 28 44 2c 4e 29 0a 23 20 64 llocRaw(D,N).# d
6edc0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 53 74 61 efine sqlite3Sta
6edd0 63 6b 41 6c 6c 6f 63 5a 65 72 6f 28 44 2c 4e 29 ckAllocZero(D,N)
6ede0 20 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f sqlite3DbMallo
6edf0 63 5a 65 72 6f 28 44 2c 4e 29 0a 23 20 64 65 66 cZero(D,N).# def
6ee00 69 6e 65 20 73 71 6c 69 74 65 33 53 74 61 63 6b ine sqlite3Stack
6ee10 46 72 65 65 28 44 2c 50 29 20 20 20 20 20 20 20 Free(D,P)
6ee20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 44 2c sqlite3DbFree(D,
6ee30 50 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 P).#endif..#ifde
6ee40 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f f SQLITE_ENABLE_
6ee50 4d 45 4d 53 59 53 33 0a 53 51 4c 49 54 45 5f 50 MEMSYS3.SQLITE_P
6ee60 52 49 56 41 54 45 20 63 6f 6e 73 74 20 73 71 6c RIVATE const sql
6ee70 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 ite3_mem_methods
6ee80 20 2a 73 71 6c 69 74 65 33 4d 65 6d 47 65 74 4d *sqlite3MemGetM
6ee90 65 6d 73 79 73 33 28 76 6f 69 64 29 3b 0a 23 65 emsys3(void);.#e
6eea0 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 ndif.#ifdef SQLI
6eeb0 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 TE_ENABLE_MEMSYS
6eec0 35 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 5.SQLITE_PRIVATE
6eed0 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d const sqlite3_m
6eee0 65 6d 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 em_methods *sqli
6eef0 74 65 33 4d 65 6d 47 65 74 4d 65 6d 73 79 73 35 te3MemGetMemsys5
6ef00 28 76 6f 69 64 29 3b 0a 23 65 6e 64 69 66 0a 0a (void);.#endif..
6ef10 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
6ef20 4d 55 54 45 58 5f 4f 4d 49 54 0a 53 51 4c 49 54 MUTEX_OMIT.SQLIT
6ef30 45 5f 50 52 49 56 41 54 45 20 20 20 73 71 6c 69 E_PRIVATE sqli
6ef40 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 te3_mutex_method
6ef50 73 20 2a 73 71 6c 69 74 65 33 44 65 66 61 75 6c s *sqlite3Defaul
6ef60 74 4d 75 74 65 78 28 76 6f 69 64 29 3b 0a 53 51 tMutex(void);.SQ
6ef70 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 73 LITE_PRIVATE s
6ef80 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 73 71 qlite3_mutex *sq
6ef90 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 lite3MutexAlloc(
6efa0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
6efb0 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 VATE int sqlit
6efc0 65 33 4d 75 74 65 78 49 6e 69 74 28 76 6f 69 64 e3MutexInit(void
6efd0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6efe0 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 4d E int sqlite3M
6eff0 75 74 65 78 45 6e 64 28 76 6f 69 64 29 3b 0a 23 utexEnd(void);.#
6f000 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52 endif..SQLITE_PR
6f010 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
6f020 33 53 74 61 74 75 73 56 61 6c 75 65 28 69 6e 74 3StatusValue(int
6f030 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6f040 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74 E void sqlite3St
6f050 61 74 75 73 41 64 64 28 69 6e 74 2c 20 69 6e 74 atusAdd(int, int
6f060 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6f070 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74 E void sqlite3St
6f080 61 74 75 73 53 65 74 28 69 6e 74 2c 20 69 6e 74 atusSet(int, int
6f090 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 );..SQLITE_PRIVA
6f0a0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73 TE int sqlite3Is
6f0b0 4e 61 4e 28 64 6f 75 62 6c 65 29 3b 0a 0a 53 51 NaN(double);..SQ
6f0c0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
6f0d0 64 20 73 71 6c 69 74 65 33 56 58 50 72 69 6e 74 d sqlite3VXPrint
6f0e0 66 28 53 74 72 41 63 63 75 6d 2a 2c 20 69 6e 74 f(StrAccum*, int
6f0f0 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 76 , const char*, v
6f100 61 5f 6c 69 73 74 29 3b 0a 53 51 4c 49 54 45 5f a_list);.SQLITE_
6f110 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 PRIVATE char *sq
6f120 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 73 71 6c lite3MPrintf(sql
6f130 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 ite3*,const char
6f140 2a 2c 20 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45 5f *, ...);.SQLITE_
6f150 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 PRIVATE char *sq
6f160 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 73 71 lite3VMPrintf(sq
6f170 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 lite3*,const cha
6f180 72 2a 2c 20 76 61 5f 6c 69 73 74 29 3b 0a 53 51 r*, va_list);.SQ
6f190 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 LITE_PRIVATE cha
6f1a0 72 20 2a 73 71 6c 69 74 65 33 4d 41 70 70 65 6e r *sqlite3MAppen
6f1b0 64 66 28 73 71 6c 69 74 65 33 2a 2c 63 68 61 72 df(sqlite3*,char
6f1c0 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 2e 2e *,const char*,..
6f1d0 2e 29 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28 .);.#if defined(
6f1e0 53 51 4c 49 54 45 5f 54 45 53 54 29 20 7c 7c 20 SQLITE_TEST) ||
6f1f0 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 defined(SQLITE_D
6f200 45 42 55 47 29 0a 53 51 4c 49 54 45 5f 50 52 49 EBUG).SQLITE_PRI
6f210 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 VATE void sqli
6f220 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 63 te3DebugPrintf(c
6f230 6f 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 onst char*, ...)
6f240 3b 0a 23 65 6e 64 69 66 0a 23 69 66 20 64 65 66 ;.#endif.#if def
6f250 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 ined(SQLITE_TEST
6f260 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ).SQLITE_PRIVATE
6f270 20 20 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 void *sqlite3
6f280 54 65 73 74 54 65 78 74 54 6f 50 74 72 28 63 6f TestTextToPtr(co
6f290 6e 73 74 20 63 68 61 72 2a 29 3b 0a 23 65 6e 64 nst char*);.#end
6f2a0 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 if.SQLITE_PRIVAT
6f2b0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 65 E void sqlite3Se
6f2c0 74 53 74 72 69 6e 67 28 63 68 61 72 20 2a 2a 2c tString(char **,
6f2d0 20 73 71 6c 69 74 65 33 2a 2c 20 63 6f 6e 73 74 sqlite3*, const
6f2e0 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 53 51 char*, ...);.SQ
6f2f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
6f300 64 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 d sqlite3ErrorMs
6f310 67 28 50 61 72 73 65 2a 2c 20 63 6f 6e 73 74 20 g(Parse*, const
6f320 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 53 51 4c char*, ...);.SQL
6f330 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
6f340 20 73 71 6c 69 74 65 33 45 72 72 6f 72 43 6c 65 sqlite3ErrorCle
6f350 61 72 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 ar(Parse*);.SQLI
6f360 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
6f370 71 6c 69 74 65 33 44 65 71 75 6f 74 65 28 63 68 qlite3Dequote(ch
6f380 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ar*);.SQLITE_PRI
6f390 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
6f3a0 4b 65 79 77 6f 72 64 43 6f 64 65 28 63 6f 6e 73 KeywordCode(cons
6f3b0 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a t unsigned char*
6f3c0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
6f3d0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
6f3e0 65 33 52 75 6e 50 61 72 73 65 72 28 50 61 72 73 e3RunParser(Pars
6f3f0 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c e*, const char*,
6f400 20 63 68 61 72 20 2a 2a 29 3b 0a 53 51 4c 49 54 char **);.SQLIT
6f410 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
6f420 71 6c 69 74 65 33 46 69 6e 69 73 68 43 6f 64 69 qlite3FinishCodi
6f430 6e 67 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 ng(Parse*);.SQLI
6f440 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
6f450 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 qlite3GetTempReg
6f460 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 (Parse*);.SQLITE
6f470 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
6f480 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 lite3ReleaseTemp
6f490 52 65 67 28 50 61 72 73 65 2a 2c 69 6e 74 29 3b Reg(Parse*,int);
6f4a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6f4b0 69 6e 74 20 73 71 6c 69 74 65 33 47 65 74 54 65 int sqlite3GetTe
6f4c0 6d 70 52 61 6e 67 65 28 50 61 72 73 65 2a 2c 69 mpRange(Parse*,i
6f4d0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
6f4e0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
6f4f0 52 65 6c 65 61 73 65 54 65 6d 70 52 61 6e 67 65 ReleaseTempRange
6f500 28 50 61 72 73 65 2a 2c 69 6e 74 2c 69 6e 74 29 (Parse*,int,int)
6f510 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6f520 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45 78 Expr *sqlite3Ex
6f530 70 72 41 6c 6c 6f 63 28 73 71 6c 69 74 65 33 2a prAlloc(sqlite3*
6f540 2c 69 6e 74 2c 63 6f 6e 73 74 20 54 6f 6b 65 6e ,int,const Token
6f550 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 *,int);.SQLITE_P
6f560 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c RIVATE Expr *sql
6f570 69 74 65 33 45 78 70 72 28 73 71 6c 69 74 65 33 ite3Expr(sqlite3
6f580 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 *,int,const char
6f590 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
6f5a0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 TE void sqlite3E
6f5b0 78 70 72 41 74 74 61 63 68 53 75 62 74 72 65 65 xprAttachSubtree
6f5c0 73 28 73 71 6c 69 74 65 33 2a 2c 45 78 70 72 2a s(sqlite3*,Expr*
6f5d0 2c 45 78 70 72 2a 2c 45 78 70 72 2a 29 3b 0a 53 ,Expr*,Expr*);.S
6f5e0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 QLITE_PRIVATE Ex
6f5f0 70 72 20 2a 73 71 6c 69 74 65 33 50 45 78 70 72 pr *sqlite3PExpr
6f600 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 45 78 (Parse*, int, Ex
6f610 70 72 2a 2c 20 45 78 70 72 2a 2c 20 63 6f 6e 73 pr*, Expr*, cons
6f620 74 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 t Token*);.SQLIT
6f630 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a E_PRIVATE Expr *
6f640 73 71 6c 69 74 65 33 45 78 70 72 41 6e 64 28 73 sqlite3ExprAnd(s
6f650 71 6c 69 74 65 33 2a 2c 45 78 70 72 2a 2c 20 45 qlite3*,Expr*, E
6f660 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 xpr*);.SQLITE_PR
6f670 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 IVATE Expr *sqli
6f680 74 65 33 45 78 70 72 46 75 6e 63 74 69 6f 6e 28 te3ExprFunction(
6f690 50 61 72 73 65 2a 2c 45 78 70 72 4c 69 73 74 2a Parse*,ExprList*
6f6a0 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 , Token*);.SQLIT
6f6b0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
6f6c0 71 6c 69 74 65 33 45 78 70 72 41 73 73 69 67 6e qlite3ExprAssign
6f6d0 56 61 72 4e 75 6d 62 65 72 28 50 61 72 73 65 2a VarNumber(Parse*
6f6e0 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 , Expr*);.SQLITE
6f6f0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
6f700 6c 69 74 65 33 45 78 70 72 43 6c 65 61 72 28 73 lite3ExprClear(s
6f710 71 6c 69 74 65 33 2a 2c 20 45 78 70 72 2a 29 3b qlite3*, Expr*);
6f720 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6f730 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 void sqlite3Expr
6f740 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33 2a 2c Delete(sqlite3*,
6f750 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f Expr*);.SQLITE_
6f760 50 52 49 56 41 54 45 20 45 78 70 72 4c 69 73 74 PRIVATE ExprList
6f770 20 2a 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 *sqlite3ExprLis
6f780 74 41 70 70 65 6e 64 28 50 61 72 73 65 2a 2c 45 tAppend(Parse*,E
6f790 78 70 72 4c 69 73 74 2a 2c 45 78 70 72 2a 29 3b xprList*,Expr*);
6f7a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6f7b0 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 void sqlite3Expr
6f7c0 4c 69 73 74 53 65 74 4e 61 6d 65 28 50 61 72 73 ListSetName(Pars
6f7d0 65 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 54 6f 6b e*,ExprList*,Tok
6f7e0 65 6e 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 en*,int);.SQLITE
6f7f0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
6f800 6c 69 74 65 33 45 78 70 72 4c 69 73 74 53 65 74 lite3ExprListSet
6f810 53 70 61 6e 28 50 61 72 73 65 2a 2c 45 78 70 72 Span(Parse*,Expr
6f820 4c 69 73 74 2a 2c 45 78 70 72 53 70 61 6e 2a 29 List*,ExprSpan*)
6f830 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6f840 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 void sqlite3Exp
6f850 72 4c 69 73 74 44 65 6c 65 74 65 28 73 71 6c 69 rListDelete(sqli
6f860 74 65 33 2a 2c 20 45 78 70 72 4c 69 73 74 2a 29 te3*, ExprList*)
6f870 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6f880 20 69 6e 74 20 73 71 6c 69 74 65 33 49 6e 69 74 int sqlite3Init
6f890 28 73 71 6c 69 74 65 33 2a 2c 20 63 68 61 72 2a (sqlite3*, char*
6f8a0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
6f8b0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 6e TE int sqlite3In
6f8c0 69 74 43 61 6c 6c 62 61 63 6b 28 76 6f 69 64 2a itCallback(void*
6f8d0 2c 20 69 6e 74 2c 20 63 68 61 72 2a 2a 2c 20 63 , int, char**, c
6f8e0 68 61 72 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 har**);.SQLITE_P
6f8f0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
6f900 74 65 33 50 72 61 67 6d 61 28 50 61 72 73 65 2a te3Pragma(Parse*
6f910 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 54 ,Token*,Token*,T
6f920 6f 6b 65 6e 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 oken*,int);.SQLI
6f930 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
6f940 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65 sqlite3ResetInte
6f950 72 6e 61 6c 53 63 68 65 6d 61 28 73 71 6c 69 74 rnalSchema(sqlit
6f960 65 33 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 e3*, int);.SQLIT
6f970 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
6f980 71 6c 69 74 65 33 42 65 67 69 6e 50 61 72 73 65 qlite3BeginParse
6f990 28 50 61 72 73 65 2a 2c 69 6e 74 29 3b 0a 53 51 (Parse*,int);.SQ
6f9a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
6f9b0 64 20 73 71 6c 69 74 65 33 43 6f 6d 6d 69 74 49 d sqlite3CommitI
6f9c0 6e 74 65 72 6e 61 6c 43 68 61 6e 67 65 73 28 73 nternalChanges(s
6f9d0 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 qlite3*);.SQLITE
6f9e0 5f 50 52 49 56 41 54 45 20 54 61 62 6c 65 20 2a _PRIVATE Table *
6f9f0 73 71 6c 69 74 65 33 52 65 73 75 6c 74 53 65 74 sqlite3ResultSet
6fa00 4f 66 53 65 6c 65 63 74 28 50 61 72 73 65 2a 2c OfSelect(Parse*,
6fa10 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54 45 Select*);.SQLITE
6fa20 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
6fa30 6c 69 74 65 33 4f 70 65 6e 4d 61 73 74 65 72 54 lite3OpenMasterT
6fa40 61 62 6c 65 28 50 61 72 73 65 20 2a 2c 20 69 6e able(Parse *, in
6fa50 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
6fa60 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 TE void sqlite3S
6fa70 74 61 72 74 54 61 62 6c 65 28 50 61 72 73 65 2a tartTable(Parse*
6fa80 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 69 ,Token*,Token*,i
6fa90 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 29 3b nt,int,int,int);
6faa0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6fab0 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64 64 43 void sqlite3AddC
6fac0 6f 6c 75 6d 6e 28 50 61 72 73 65 2a 2c 54 6f 6b olumn(Parse*,Tok
6fad0 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 en*);.SQLITE_PRI
6fae0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
6faf0 33 41 64 64 4e 6f 74 4e 75 6c 6c 28 50 61 72 73 3AddNotNull(Pars
6fb00 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 e*, int);.SQLITE
6fb10 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
6fb20 6c 69 74 65 33 41 64 64 50 72 69 6d 61 72 79 4b lite3AddPrimaryK
6fb30 65 79 28 50 61 72 73 65 2a 2c 20 45 78 70 72 4c ey(Parse*, ExprL
6fb40 69 73 74 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 ist*, int, int,
6fb50 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
6fb60 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
6fb70 33 41 64 64 43 68 65 63 6b 43 6f 6e 73 74 72 61 3AddCheckConstra
6fb80 69 6e 74 28 50 61 72 73 65 2a 2c 20 45 78 70 72 int(Parse*, Expr
6fb90 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
6fba0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 TE void sqlite3A
6fbb0 64 64 43 6f 6c 75 6d 6e 54 79 70 65 28 50 61 72 ddColumnType(Par
6fbc0 73 65 2a 2c 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c se*,Token*);.SQL
6fbd0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
6fbe0 20 73 71 6c 69 74 65 33 41 64 64 44 65 66 61 75 sqlite3AddDefau
6fbf0 6c 74 56 61 6c 75 65 28 50 61 72 73 65 2a 2c 45 ltValue(Parse*,E
6fc00 78 70 72 53 70 61 6e 2a 29 3b 0a 53 51 4c 49 54 xprSpan*);.SQLIT
6fc10 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
6fc20 71 6c 69 74 65 33 41 64 64 43 6f 6c 6c 61 74 65 qlite3AddCollate
6fc30 54 79 70 65 28 50 61 72 73 65 2a 2c 20 54 6f 6b Type(Parse*, Tok
6fc40 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 en*);.SQLITE_PRI
6fc50 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
6fc60 33 45 6e 64 54 61 62 6c 65 28 50 61 72 73 65 2a 3EndTable(Parse*
6fc70 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 53 ,Token*,Token*,S
6fc80 65 6c 65 63 74 2a 29 3b 0a 0a 53 51 4c 49 54 45 elect*);..SQLITE
6fc90 5f 50 52 49 56 41 54 45 20 42 69 74 76 65 63 20 _PRIVATE Bitvec
6fca0 2a 73 71 6c 69 74 65 33 42 69 74 76 65 63 43 72 *sqlite3BitvecCr
6fcb0 65 61 74 65 28 75 33 32 29 3b 0a 53 51 4c 49 54 eate(u32);.SQLIT
6fcc0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
6fcd0 6c 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28 lite3BitvecTest(
6fce0 42 69 74 76 65 63 2a 2c 20 75 33 32 29 3b 0a 53 Bitvec*, u32);.S
6fcf0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
6fd00 74 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 53 t sqlite3BitvecS
6fd10 65 74 28 42 69 74 76 65 63 2a 2c 20 75 33 32 29 et(Bitvec*, u32)
6fd20 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6fd30 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 69 74 void sqlite3Bit
6fd40 76 65 63 43 6c 65 61 72 28 42 69 74 76 65 63 2a vecClear(Bitvec*
6fd50 2c 20 75 33 32 2c 20 76 6f 69 64 2a 29 3b 0a 53 , u32, void*);.S
6fd60 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
6fd70 69 64 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 id sqlite3Bitvec
6fd80 44 65 73 74 72 6f 79 28 42 69 74 76 65 63 2a 29 Destroy(Bitvec*)
6fd90 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6fda0 20 75 33 32 20 73 71 6c 69 74 65 33 42 69 74 76 u32 sqlite3Bitv
6fdb0 65 63 53 69 7a 65 28 42 69 74 76 65 63 2a 29 3b ecSize(Bitvec*);
6fdc0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6fdd0 69 6e 74 20 73 71 6c 69 74 65 33 42 69 74 76 65 int sqlite3Bitve
6fde0 63 42 75 69 6c 74 69 6e 54 65 73 74 28 69 6e 74 cBuiltinTest(int
6fdf0 2c 69 6e 74 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f ,int*);..SQLITE_
6fe00 50 52 49 56 41 54 45 20 52 6f 77 53 65 74 20 2a PRIVATE RowSet *
6fe10 73 71 6c 69 74 65 33 52 6f 77 53 65 74 49 6e 69 sqlite3RowSetIni
6fe20 74 28 73 71 6c 69 74 65 33 2a 2c 20 76 6f 69 64 t(sqlite3*, void
6fe30 2a 2c 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 29 *, unsigned int)
6fe40 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6fe50 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 6f 77 void sqlite3Row
6fe60 53 65 74 43 6c 65 61 72 28 52 6f 77 53 65 74 2a SetClear(RowSet*
6fe70 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6fe80 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 6f E void sqlite3Ro
6fe90 77 53 65 74 49 6e 73 65 72 74 28 52 6f 77 53 65 wSetInsert(RowSe
6fea0 74 2a 2c 20 69 36 34 29 3b 0a 53 51 4c 49 54 45 t*, i64);.SQLITE
6feb0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
6fec0 69 74 65 33 52 6f 77 53 65 74 54 65 73 74 28 52 ite3RowSetTest(R
6fed0 6f 77 53 65 74 2a 2c 20 75 38 20 69 42 61 74 63 owSet*, u8 iBatc
6fee0 68 2c 20 69 36 34 29 3b 0a 53 51 4c 49 54 45 5f h, i64);.SQLITE_
6fef0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
6ff00 74 65 33 52 6f 77 53 65 74 4e 65 78 74 28 52 6f te3RowSetNext(Ro
6ff10 77 53 65 74 2a 2c 20 69 36 34 2a 29 3b 0a 0a 53 wSet*, i64*);..S
6ff20 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
6ff30 69 64 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 id sqlite3Create
6ff40 56 69 65 77 28 50 61 72 73 65 2a 2c 54 6f 6b 65 View(Parse*,Toke
6ff50 6e 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a n*,Token*,Token*
6ff60 2c 53 65 6c 65 63 74 2a 2c 69 6e 74 2c 69 6e 74 ,Select*,int,int
6ff70 29 3b 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 );..#if !defined
6ff80 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 (SQLITE_OMIT_VIE
6ff90 57 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 W) || !defined(S
6ffa0 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 QLITE_OMIT_VIRTU
6ffb0 41 4c 54 41 42 4c 45 29 0a 53 51 4c 49 54 45 5f ALTABLE).SQLITE_
6ffc0 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 PRIVATE int sq
6ffd0 6c 69 74 65 33 56 69 65 77 47 65 74 43 6f 6c 75 lite3ViewGetColu
6ffe0 6d 6e 4e 61 6d 65 73 28 50 61 72 73 65 2a 2c 54 mnNames(Parse*,T
6fff0 61 62 6c 65 2a 29 3b 0a 23 65 6c 73 65 0a 23 20 able*);.#else.#
70000 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56 69 define sqlite3Vi
70010 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 ewGetColumnNames
70020 28 41 2c 42 29 20 30 0a 23 65 6e 64 69 66 0a 0a (A,B) 0.#endif..
70030 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
70040 6f 69 64 20 73 71 6c 69 74 65 33 44 72 6f 70 54 oid sqlite3DropT
70050 61 62 6c 65 28 50 61 72 73 65 2a 2c 20 53 72 63 able(Parse*, Src
70060 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 List*, int, int)
70070 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
70080 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65 6c void sqlite3Del
70090 65 74 65 54 61 62 6c 65 28 54 61 62 6c 65 2a 29 eteTable(Table*)
700a0 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ;.#ifndef SQLITE
700b0 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d _OMIT_AUTOINCREM
700c0 45 4e 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ENT.SQLITE_PRIVA
700d0 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 TE void sqlite
700e0 33 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74 42 65 3AutoincrementBe
700f0 67 69 6e 28 50 61 72 73 65 20 2a 70 50 61 72 73 gin(Parse *pPars
70100 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 e);.SQLITE_PRIVA
70110 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 TE void sqlite
70120 33 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74 45 6e 3AutoincrementEn
70130 64 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 29 d(Parse *pParse)
70140 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 ;.#else.# define
70150 20 73 71 6c 69 74 65 33 41 75 74 6f 69 6e 63 72 sqlite3Autoincr
70160 65 6d 65 6e 74 42 65 67 69 6e 28 58 29 0a 23 20 ementBegin(X).#
70170 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 41 75 define sqlite3Au
70180 74 6f 69 6e 63 72 65 6d 65 6e 74 45 6e 64 28 58 toincrementEnd(X
70190 29 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f ).#endif.SQLITE_
701a0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
701b0 69 74 65 33 49 6e 73 65 72 74 28 50 61 72 73 65 ite3Insert(Parse
701c0 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 45 78 70 *, SrcList*, Exp
701d0 72 4c 69 73 74 2a 2c 20 53 65 6c 65 63 74 2a 2c rList*, Select*,
701e0 20 49 64 4c 69 73 74 2a 2c 20 69 6e 74 29 3b 0a IdList*, int);.
701f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
70200 6f 69 64 20 2a 73 71 6c 69 74 65 33 41 72 72 61 oid *sqlite3Arra
70210 79 41 6c 6c 6f 63 61 74 65 28 73 71 6c 69 74 65 yAllocate(sqlite
70220 33 2a 2c 76 6f 69 64 2a 2c 69 6e 74 2c 69 6e 74 3*,void*,int,int
70230 2c 69 6e 74 2a 2c 69 6e 74 2a 2c 69 6e 74 2a 29 ,int*,int*,int*)
70240 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
70250 20 49 64 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 IdList *sqlite3
70260 49 64 4c 69 73 74 41 70 70 65 6e 64 28 73 71 6c IdListAppend(sql
70270 69 74 65 33 2a 2c 20 49 64 4c 69 73 74 2a 2c 20 ite3*, IdList*,
70280 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f Token*);.SQLITE_
70290 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
702a0 74 65 33 49 64 4c 69 73 74 49 6e 64 65 78 28 49 te3IdListIndex(I
702b0 64 4c 69 73 74 2a 2c 63 6f 6e 73 74 20 63 68 61 dList*,const cha
702c0 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 r*);.SQLITE_PRIV
702d0 41 54 45 20 53 72 63 4c 69 73 74 20 2a 73 71 6c ATE SrcList *sql
702e0 69 74 65 33 53 72 63 4c 69 73 74 45 6e 6c 61 72 ite3SrcListEnlar
702f0 67 65 28 73 71 6c 69 74 65 33 2a 2c 20 53 72 63 ge(sqlite3*, Src
70300 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 List*, int, int)
70310 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
70320 20 53 72 63 4c 69 73 74 20 2a 73 71 6c 69 74 65 SrcList *sqlite
70330 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64 28 73 3SrcListAppend(s
70340 71 6c 69 74 65 33 2a 2c 20 53 72 63 4c 69 73 74 qlite3*, SrcList
70350 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e *, Token*, Token
70360 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
70370 54 45 20 53 72 63 4c 69 73 74 20 2a 73 71 6c 69 TE SrcList *sqli
70380 74 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64 te3SrcListAppend
70390 46 72 6f 6d 54 65 72 6d 28 50 61 72 73 65 2a 2c FromTerm(Parse*,
703a0 20 53 72 63 4c 69 73 74 2a 2c 20 54 6f 6b 65 6e SrcList*, Token
703b0 2a 2c 20 54 6f 6b 65 6e 2a 2c 0a 20 20 20 20 20 *, Token*,.
703c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
703d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
703e0 20 54 6f 6b 65 6e 2a 2c 20 53 65 6c 65 63 74 2a Token*, Select*
703f0 2c 20 45 78 70 72 2a 2c 20 49 64 4c 69 73 74 2a , Expr*, IdList*
70400 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
70410 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 72 E void sqlite3Sr
70420 63 4c 69 73 74 49 6e 64 65 78 65 64 42 79 28 50 cListIndexedBy(P
70430 61 72 73 65 20 2a 2c 20 53 72 63 4c 69 73 74 20 arse *, SrcList
70440 2a 2c 20 54 6f 6b 65 6e 20 2a 29 3b 0a 53 51 4c *, Token *);.SQL
70450 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
70460 73 71 6c 69 74 65 33 49 6e 64 65 78 65 64 42 79 sqlite3IndexedBy
70470 4c 6f 6f 6b 75 70 28 50 61 72 73 65 20 2a 2c 20 Lookup(Parse *,
70480 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 struct SrcList_i
70490 74 65 6d 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 tem *);.SQLITE_P
704a0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
704b0 74 65 33 53 72 63 4c 69 73 74 53 68 69 66 74 4a te3SrcListShiftJ
704c0 6f 69 6e 54 79 70 65 28 53 72 63 4c 69 73 74 2a oinType(SrcList*
704d0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
704e0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 72 E void sqlite3Sr
704f0 63 4c 69 73 74 41 73 73 69 67 6e 43 75 72 73 6f cListAssignCurso
70500 72 73 28 50 61 72 73 65 2a 2c 20 53 72 63 4c 69 rs(Parse*, SrcLi
70510 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 st*);.SQLITE_PRI
70520 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
70530 33 49 64 4c 69 73 74 44 65 6c 65 74 65 28 73 71 3IdListDelete(sq
70540 6c 69 74 65 33 2a 2c 20 49 64 4c 69 73 74 2a 29 lite3*, IdList*)
70550 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
70560 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 72 63 void sqlite3Src
70570 4c 69 73 74 44 65 6c 65 74 65 28 73 71 6c 69 74 ListDelete(sqlit
70580 65 33 2a 2c 20 53 72 63 4c 69 73 74 2a 29 3b 0a e3*, SrcList*);.
70590 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 49 SQLITE_PRIVATE I
705a0 6e 64 65 78 20 2a 73 71 6c 69 74 65 33 43 72 65 ndex *sqlite3Cre
705b0 61 74 65 49 6e 64 65 78 28 50 61 72 73 65 2a 2c ateIndex(Parse*,
705c0 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 53 72 Token*,Token*,Sr
705d0 63 4c 69 73 74 2a 2c 45 78 70 72 4c 69 73 74 2a cList*,ExprList*
705e0 2c 69 6e 74 2c 54 6f 6b 65 6e 2a 2c 0a 20 20 20 ,int,Token*,.
705f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
70600 20 20 20 20 20 54 6f 6b 65 6e 2a 2c 20 69 6e 74 Token*, int
70610 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
70620 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
70630 74 65 33 44 72 6f 70 49 6e 64 65 78 28 50 61 72 te3DropIndex(Par
70640 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 69 se*, SrcList*, i
70650 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
70660 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 ATE int sqlite3S
70670 65 6c 65 63 74 28 50 61 72 73 65 2a 2c 20 53 65 elect(Parse*, Se
70680 6c 65 63 74 2a 2c 20 53 65 6c 65 63 74 44 65 73 lect*, SelectDes
70690 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 t*);.SQLITE_PRIV
706a0 41 54 45 20 53 65 6c 65 63 74 20 2a 73 71 6c 69 ATE Select *sqli
706b0 74 65 33 53 65 6c 65 63 74 4e 65 77 28 50 61 72 te3SelectNew(Par
706c0 73 65 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 53 72 se*,ExprList*,Sr
706d0 63 4c 69 73 74 2a 2c 45 78 70 72 2a 2c 45 78 70 cList*,Expr*,Exp
706e0 72 4c 69 73 74 2a 2c 0a 20 20 20 20 20 20 20 20 rList*,.
706f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
70700 20 45 78 70 72 2a 2c 45 78 70 72 4c 69 73 74 2a Expr*,ExprList*
70710 2c 69 6e 74 2c 45 78 70 72 2a 2c 45 78 70 72 2a ,int,Expr*,Expr*
70720 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
70730 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 65 E void sqlite3Se
70740 6c 65 63 74 44 65 6c 65 74 65 28 73 71 6c 69 74 lectDelete(sqlit
70750 65 33 2a 2c 20 53 65 6c 65 63 74 2a 29 3b 0a 53 e3*, Select*);.S
70760 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 54 61 QLITE_PRIVATE Ta
70770 62 6c 65 20 2a 73 71 6c 69 74 65 33 53 72 63 4c ble *sqlite3SrcL
70780 69 73 74 4c 6f 6f 6b 75 70 28 50 61 72 73 65 2a istLookup(Parse*
70790 2c 20 53 72 63 4c 69 73 74 2a 29 3b 0a 53 51 4c , SrcList*);.SQL
707a0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
707b0 73 71 6c 69 74 65 33 49 73 52 65 61 64 4f 6e 6c sqlite3IsReadOnl
707c0 79 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a y(Parse*, Table*
707d0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
707e0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
707f0 74 65 33 4f 70 65 6e 54 61 62 6c 65 28 50 61 72 te3OpenTable(Par
70800 73 65 2a 2c 20 69 6e 74 20 69 43 75 72 2c 20 69 se*, int iCur, i
70810 6e 74 20 69 44 62 2c 20 54 61 62 6c 65 2a 2c 20 nt iDb, Table*,
70820 69 6e 74 29 3b 0a 23 69 66 20 64 65 66 69 6e 65 int);.#if define
70830 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f d(SQLITE_ENABLE_
70840 55 50 44 41 54 45 5f 44 45 4c 45 54 45 5f 4c 49 UPDATE_DELETE_LI
70850 4d 49 54 29 20 26 26 20 21 64 65 66 69 6e 65 64 MIT) && !defined
70860 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 (SQLITE_OMIT_SUB
70870 51 55 45 52 59 29 0a 53 51 4c 49 54 45 5f 50 52 QUERY).SQLITE_PR
70880 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 IVATE Expr *sqli
70890 74 65 33 4c 69 6d 69 74 57 68 65 72 65 28 50 61 te3LimitWhere(Pa
708a0 72 73 65 20 2a 2c 20 53 72 63 4c 69 73 74 20 2a rse *, SrcList *
708b0 2c 20 45 78 70 72 20 2a 2c 20 45 78 70 72 4c 69 , Expr *, ExprLi
708c0 73 74 20 2a 2c 20 45 78 70 72 20 2a 2c 20 45 78 st *, Expr *, Ex
708d0 70 72 20 2a 2c 20 63 68 61 72 20 2a 29 3b 0a 23 pr *, char *);.#
708e0 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 endif.SQLITE_PRI
708f0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
70900 33 44 65 6c 65 74 65 46 72 6f 6d 28 50 61 72 73 3DeleteFrom(Pars
70910 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 45 78 e*, SrcList*, Ex
70920 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 pr*);.SQLITE_PRI
70930 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
70940 33 55 70 64 61 74 65 28 50 61 72 73 65 2a 2c 20 3Update(Parse*,
70950 53 72 63 4c 69 73 74 2a 2c 20 45 78 70 72 4c 69 SrcList*, ExprLi
70960 73 74 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 29 st*, Expr*, int)
70970 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
70980 20 57 68 65 72 65 49 6e 66 6f 20 2a 73 71 6c 69 WhereInfo *sqli
70990 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 50 61 te3WhereBegin(Pa
709a0 72 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 rse*, SrcList*,
709b0 45 78 70 72 2a 2c 20 45 78 70 72 4c 69 73 74 2a Expr*, ExprList*
709c0 2a 2c 20 75 31 36 29 3b 0a 53 51 4c 49 54 45 5f *, u16);.SQLITE_
709d0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
709e0 69 74 65 33 57 68 65 72 65 45 6e 64 28 57 68 65 ite3WhereEnd(Whe
709f0 72 65 49 6e 66 6f 2a 29 3b 0a 53 51 4c 49 54 45 reInfo*);.SQLITE
70a00 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
70a10 69 74 65 33 45 78 70 72 43 6f 64 65 47 65 74 43 ite3ExprCodeGetC
70a20 6f 6c 75 6d 6e 28 50 61 72 73 65 2a 2c 20 54 61 olumn(Parse*, Ta
70a30 62 6c 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 ble*, int, int,
70a40 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 int, int);.SQLIT
70a50 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
70a60 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 4d 6f qlite3ExprCodeMo
70a70 76 65 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 ve(Parse*, int,
70a80 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 int, int);.SQLIT
70a90 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
70aa0 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 43 6f qlite3ExprCodeCo
70ab0 70 79 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 py(Parse*, int,
70ac0 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 int, int);.SQLIT
70ad0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
70ae0 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 53 qlite3ExprCacheS
70af0 74 6f 72 65 28 50 61 72 73 65 2a 2c 20 69 6e 74 tore(Parse*, int
70b00 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c , int, int);.SQL
70b10 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
70b20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 sqlite3ExprCach
70b30 65 50 75 73 68 28 50 61 72 73 65 2a 29 3b 0a 53 ePush(Parse*);.S
70b40 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
70b50 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 id sqlite3ExprCa
70b60 63 68 65 50 6f 70 28 50 61 72 73 65 2a 2c 20 69 chePop(Parse*, i
70b70 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
70b80 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
70b90 45 78 70 72 43 61 63 68 65 52 65 6d 6f 76 65 28 ExprCacheRemove(
70ba0 50 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 Parse*, int);.SQ
70bb0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
70bc0 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 d sqlite3ExprCac
70bd0 68 65 43 6c 65 61 72 28 50 61 72 73 65 2a 29 3b heClear(Parse*);
70be0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
70bf0 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 void sqlite3Expr
70c00 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 61 CacheAffinityCha
70c10 6e 67 65 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c nge(Parse*, int,
70c20 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
70c30 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
70c40 65 33 45 78 70 72 48 61 72 64 43 6f 70 79 28 50 e3ExprHardCopy(P
70c50 61 72 73 65 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a arse*,int,int);.
70c60 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
70c70 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f nt sqlite3ExprCo
70c80 64 65 28 50 61 72 73 65 2a 2c 20 45 78 70 72 2a de(Parse*, Expr*
70c90 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
70ca0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
70cb0 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 50 e3ExprCodeTemp(P
70cc0 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e arse*, Expr*, in
70cd0 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 t*);.SQLITE_PRIV
70ce0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 ATE int sqlite3E
70cf0 78 70 72 43 6f 64 65 54 61 72 67 65 74 28 50 61 xprCodeTarget(Pa
70d00 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 rse*, Expr*, int
70d10 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
70d20 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 E int sqlite3Exp
70d30 72 43 6f 64 65 41 6e 64 43 61 63 68 65 28 50 61 rCodeAndCache(Pa
70d40 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 rse*, Expr*, int
70d50 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
70d60 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 E void sqlite3Ex
70d70 70 72 43 6f 64 65 43 6f 6e 73 74 61 6e 74 73 28 prCodeConstants(
70d80 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 29 3b 0a Parse*, Expr*);.
70d90 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
70da0 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f nt sqlite3ExprCo
70db0 64 65 45 78 70 72 4c 69 73 74 28 50 61 72 73 65 deExprList(Parse
70dc0 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 69 6e *, ExprList*, in
70dd0 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f t, int);.SQLITE_
70de0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
70df0 69 74 65 33 45 78 70 72 49 66 54 72 75 65 28 50 ite3ExprIfTrue(P
70e00 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e arse*, Expr*, in
70e10 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f t, int);.SQLITE_
70e20 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
70e30 69 74 65 33 45 78 70 72 49 66 46 61 6c 73 65 28 ite3ExprIfFalse(
70e40 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 Parse*, Expr*, i
70e50 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 nt, int);.SQLITE
70e60 5f 50 52 49 56 41 54 45 20 54 61 62 6c 65 20 2a _PRIVATE Table *
70e70 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 sqlite3FindTable
70e80 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 (sqlite3*,const
70e90 63 68 61 72 2a 2c 20 63 6f 6e 73 74 20 63 68 61 char*, const cha
70ea0 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 r*);.SQLITE_PRIV
70eb0 41 54 45 20 54 61 62 6c 65 20 2a 73 71 6c 69 74 ATE Table *sqlit
70ec0 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 50 61 e3LocateTable(Pa
70ed0 72 73 65 2a 2c 69 6e 74 20 69 73 56 69 65 77 2c rse*,int isView,
70ee0 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 63 6f 6e const char*, con
70ef0 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 st char*);.SQLIT
70f00 45 5f 50 52 49 56 41 54 45 20 49 6e 64 65 78 20 E_PRIVATE Index
70f10 2a 73 71 6c 69 74 65 33 46 69 6e 64 49 6e 64 65 *sqlite3FindInde
70f20 78 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 x(sqlite3*,const
70f30 20 63 68 61 72 2a 2c 20 63 6f 6e 73 74 20 63 68 char*, const ch
70f40 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ar*);.SQLITE_PRI
70f50 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
70f60 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 3UnlinkAndDelete
70f70 54 61 62 6c 65 28 73 71 6c 69 74 65 33 2a 2c 69 Table(sqlite3*,i
70f80 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b nt,const char*);
70f90 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
70fa0 76 6f 69 64 20 73 71 6c 69 74 65 33 55 6e 6c 69 void sqlite3Unli
70fb0 6e 6b 41 6e 64 44 65 6c 65 74 65 49 6e 64 65 78 nkAndDeleteIndex
70fc0 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74 2c 63 6f (sqlite3*,int,co
70fd0 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 nst char*);.SQLI
70fe0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
70ff0 73 71 6c 69 74 65 33 56 61 63 75 75 6d 28 50 61 sqlite3Vacuum(Pa
71000 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 rse*);.SQLITE_PR
71010 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
71020 33 52 75 6e 56 61 63 75 75 6d 28 63 68 61 72 2a 3RunVacuum(char*
71030 2a 2c 20 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 *, sqlite3*);.SQ
71040 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 LITE_PRIVATE cha
71050 72 20 2a 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 r *sqlite3NameFr
71060 6f 6d 54 6f 6b 65 6e 28 73 71 6c 69 74 65 33 2a omToken(sqlite3*
71070 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 , Token*);.SQLIT
71080 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
71090 6c 69 74 65 33 45 78 70 72 43 6f 6d 70 61 72 65 lite3ExprCompare
710a0 28 45 78 70 72 2a 2c 20 45 78 70 72 2a 29 3b 0a (Expr*, Expr*);.
710b0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
710c0 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 41 oid sqlite3ExprA
710d0 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74 65 73 nalyzeAggregates
710e0 28 4e 61 6d 65 43 6f 6e 74 65 78 74 2a 2c 20 45 (NameContext*, E
710f0 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 xpr*);.SQLITE_PR
71100 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
71110 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 67 e3ExprAnalyzeAgg
71120 4c 69 73 74 28 4e 61 6d 65 43 6f 6e 74 65 78 74 List(NameContext
71130 2a 2c 45 78 70 72 4c 69 73 74 2a 29 3b 0a 53 51 *,ExprList*);.SQ
71140 4c 49 54 45 5f 50 52 49 56 41 54 45 20 56 64 62 LITE_PRIVATE Vdb
71150 65 20 2a 73 71 6c 69 74 65 33 47 65 74 56 64 62 e *sqlite3GetVdb
71160 65 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 e(Parse*);.SQLIT
71170 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a E_PRIVATE Expr *
71180 73 71 6c 69 74 65 33 43 72 65 61 74 65 49 64 45 sqlite3CreateIdE
71190 78 70 72 28 50 61 72 73 65 20 2a 2c 20 63 6f 6e xpr(Parse *, con
711a0 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 st char*);.SQLIT
711b0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
711c0 71 6c 69 74 65 33 50 72 6e 67 53 61 76 65 53 74 qlite3PrngSaveSt
711d0 61 74 65 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 ate(void);.SQLIT
711e0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
711f0 71 6c 69 74 65 33 50 72 6e 67 52 65 73 74 6f 72 qlite3PrngRestor
71200 65 53 74 61 74 65 28 76 6f 69 64 29 3b 0a 53 51 eState(void);.SQ
71210 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
71220 64 20 73 71 6c 69 74 65 33 50 72 6e 67 52 65 73 d sqlite3PrngRes
71230 65 74 53 74 61 74 65 28 76 6f 69 64 29 3b 0a 53 etState(void);.S
71240 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
71250 69 64 20 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61 id sqlite3Rollba
71260 63 6b 41 6c 6c 28 73 71 6c 69 74 65 33 2a 29 3b ckAll(sqlite3*);
71270 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
71280 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 64 65 void sqlite3Code
71290 56 65 72 69 66 79 53 63 68 65 6d 61 28 50 61 72 VerifySchema(Par
712a0 73 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 se*, int);.SQLIT
712b0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
712c0 71 6c 69 74 65 33 42 65 67 69 6e 54 72 61 6e 73 qlite3BeginTrans
712d0 61 63 74 69 6f 6e 28 50 61 72 73 65 2a 2c 20 69 action(Parse*, i
712e0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
712f0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
71300 43 6f 6d 6d 69 74 54 72 61 6e 73 61 63 74 69 6f CommitTransactio
71310 6e 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 n(Parse*);.SQLIT
71320 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
71330 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b 54 72 qlite3RollbackTr
71340 61 6e 73 61 63 74 69 6f 6e 28 50 61 72 73 65 2a ansaction(Parse*
71350 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
71360 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 61 E void sqlite3Sa
71370 76 65 70 6f 69 6e 74 28 50 61 72 73 65 2a 2c 20 vepoint(Parse*,
71380 69 6e 74 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 int, Token*);.SQ
71390 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
713a0 64 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 53 61 d sqlite3CloseSa
713b0 76 65 70 6f 69 6e 74 73 28 73 71 6c 69 74 65 33 vepoints(sqlite3
713c0 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 *);.SQLITE_PRIV
713d0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 ATE int sqlite3E
713e0 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 28 45 78 xprIsConstant(Ex
713f0 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 pr*);.SQLITE_PRI
71400 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
71410 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 4e 6f ExprIsConstantNo
71420 74 4a 6f 69 6e 28 45 78 70 72 2a 29 3b 0a 53 51 tJoin(Expr*);.SQ
71430 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
71440 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f sqlite3ExprIsCo
71450 6e 73 74 61 6e 74 4f 72 46 75 6e 63 74 69 6f 6e nstantOrFunction
71460 28 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f (Expr*);.SQLITE_
71470 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
71480 74 65 33 45 78 70 72 49 73 49 6e 74 65 67 65 72 te3ExprIsInteger
71490 28 45 78 70 72 2a 2c 20 69 6e 74 2a 29 3b 0a 53 (Expr*, int*);.S
714a0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
714b0 74 20 73 71 6c 69 74 65 33 49 73 52 6f 77 69 64 t sqlite3IsRowid
714c0 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 (const char*);.S
714d0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
714e0 69 64 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 id sqlite3Genera
714f0 74 65 52 6f 77 44 65 6c 65 74 65 28 50 61 72 73 teRowDelete(Pars
71500 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 2c e*, Table*, int,
71510 20 69 6e 74 2c 20 69 6e 74 2c 20 54 72 69 67 67 int, int, Trigg
71520 65 72 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 er *, int);.SQLI
71530 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
71540 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65 52 sqlite3GenerateR
71550 6f 77 49 6e 64 65 78 44 65 6c 65 74 65 28 50 61 owIndexDelete(Pa
71560 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e rse*, Table*, in
71570 74 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 t, int*);.SQLITE
71580 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
71590 69 74 65 33 47 65 6e 65 72 61 74 65 49 6e 64 65 ite3GenerateInde
715a0 78 4b 65 79 28 50 61 72 73 65 2a 2c 20 49 6e 64 xKey(Parse*, Ind
715b0 65 78 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 ex*, int, int, i
715c0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
715d0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
715e0 47 65 6e 65 72 61 74 65 43 6f 6e 73 74 72 61 69 GenerateConstrai
715f0 6e 74 43 68 65 63 6b 73 28 50 61 72 73 65 2a 2c ntChecks(Parse*,
71600 54 61 62 6c 65 2a 2c 69 6e 74 2c 69 6e 74 2c 0a Table*,int,int,.
71610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
71620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
71630 20 20 20 20 20 69 6e 74 2a 2c 69 6e 74 2c 69 6e int*,int,in
71640 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2a 29 3b t,int,int,int*);
71650 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
71660 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6d 70 void sqlite3Comp
71670 6c 65 74 65 49 6e 73 65 72 74 69 6f 6e 28 50 61 leteInsertion(Pa
71680 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e rse*, Table*, in
71690 74 2c 20 69 6e 74 2c 20 69 6e 74 2a 2c 20 69 6e t, int, int*, in
716a0 74 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 t, int, int);.SQ
716b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
716c0 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c sqlite3OpenTabl
716d0 65 41 6e 64 49 6e 64 69 63 65 73 28 50 61 72 73 eAndIndices(Pars
716e0 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 2c e*, Table*, int,
716f0 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
71700 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
71710 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72 e3BeginWriteOper
71720 61 74 69 6f 6e 28 50 61 72 73 65 2a 2c 20 69 6e ation(Parse*, in
71730 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f t, int);.SQLITE_
71740 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
71750 69 74 65 33 4d 75 6c 74 69 57 72 69 74 65 28 50 ite3MultiWrite(P
71760 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 arse*);.SQLITE_P
71770 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
71780 74 65 33 4d 61 79 41 62 6f 72 74 28 50 61 72 73 te3MayAbort(Pars
71790 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 e*);.SQLITE_PRIV
717a0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
717b0 48 61 6c 74 43 6f 6e 73 74 72 61 69 6e 74 28 50 HaltConstraint(P
717c0 61 72 73 65 2a 2c 20 69 6e 74 2c 20 63 68 61 72 arse*, int, char
717d0 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f *, int);.SQLITE_
717e0 50 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 PRIVATE Expr *sq
717f0 6c 69 74 65 33 45 78 70 72 44 75 70 28 73 71 6c lite3ExprDup(sql
71800 69 74 65 33 2a 2c 45 78 70 72 2a 2c 69 6e 74 29 ite3*,Expr*,int)
71810 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
71820 20 45 78 70 72 4c 69 73 74 20 2a 73 71 6c 69 74 ExprList *sqlit
71830 65 33 45 78 70 72 4c 69 73 74 44 75 70 28 73 71 e3ExprListDup(sq
71840 6c 69 74 65 33 2a 2c 45 78 70 72 4c 69 73 74 2a lite3*,ExprList*
71850 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ,int);.SQLITE_PR
71860 49 56 41 54 45 20 53 72 63 4c 69 73 74 20 2a 73 IVATE SrcList *s
71870 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 75 70 qlite3SrcListDup
71880 28 73 71 6c 69 74 65 33 2a 2c 53 72 63 4c 69 73 (sqlite3*,SrcLis
71890 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f t*,int);.SQLITE_
718a0 50 52 49 56 41 54 45 20 49 64 4c 69 73 74 20 2a PRIVATE IdList *
718b0 73 71 6c 69 74 65 33 49 64 4c 69 73 74 44 75 70 sqlite3IdListDup
718c0 28 73 71 6c 69 74 65 33 2a 2c 49 64 4c 69 73 74 (sqlite3*,IdList
718d0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
718e0 54 45 20 53 65 6c 65 63 74 20 2a 73 71 6c 69 74 TE Select *sqlit
718f0 65 33 53 65 6c 65 63 74 44 75 70 28 73 71 6c 69 e3SelectDup(sqli
71900 74 65 33 2a 2c 53 65 6c 65 63 74 2a 2c 69 6e 74 te3*,Select*,int
71910 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
71920 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 75 E void sqlite3Fu
71930 6e 63 44 65 66 49 6e 73 65 72 74 28 46 75 6e 63 ncDefInsert(Func
71940 44 65 66 48 61 73 68 2a 2c 20 46 75 6e 63 44 65 DefHash*, FuncDe
71950 66 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 f*);.SQLITE_PRIV
71960 41 54 45 20 46 75 6e 63 44 65 66 20 2a 73 71 6c ATE FuncDef *sql
71970 69 74 65 33 46 69 6e 64 46 75 6e 63 74 69 6f 6e ite3FindFunction
71980 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 (sqlite3*,const
71990 63 68 61 72 2a 2c 69 6e 74 2c 69 6e 74 2c 75 38 char*,int,int,u8
719a0 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ,int);.SQLITE_PR
719b0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
719c0 65 33 52 65 67 69 73 74 65 72 42 75 69 6c 74 69 e3RegisterBuilti
719d0 6e 46 75 6e 63 74 69 6f 6e 73 28 73 71 6c 69 74 nFunctions(sqlit
719e0 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 e3*);.SQLITE_PRI
719f0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
71a00 33 52 65 67 69 73 74 65 72 44 61 74 65 54 69 6d 3RegisterDateTim
71a10 65 46 75 6e 63 74 69 6f 6e 73 28 76 6f 69 64 29 eFunctions(void)
71a20 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
71a30 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65 67 void sqlite3Reg
71a40 69 73 74 65 72 47 6c 6f 62 61 6c 46 75 6e 63 74 isterGlobalFunct
71a50 69 6f 6e 73 28 76 6f 69 64 29 3b 0a 23 69 66 64 ions(void);.#ifd
71a60 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a ef SQLITE_DEBUG.
71a70 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
71a80 20 69 6e 74 20 73 71 6c 69 74 65 33 53 61 66 65 int sqlite3Safe
71a90 74 79 4f 6e 28 73 71 6c 69 74 65 33 2a 29 3b 0a tyOn(sqlite3*);.
71aa0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
71ab0 20 69 6e 74 20 73 71 6c 69 74 65 33 53 61 66 65 int sqlite3Safe
71ac0 74 79 4f 66 66 28 73 71 6c 69 74 65 33 2a 29 3b tyOff(sqlite3*);
71ad0 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 .#else.# define
71ae0 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 sqlite3SafetyOn(
71af0 41 29 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 A) 0.# define sq
71b00 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 41 lite3SafetyOff(A
71b10 29 20 30 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 ) 0.#endif.SQLIT
71b20 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
71b30 6c 69 74 65 33 53 61 66 65 74 79 43 68 65 63 6b lite3SafetyCheck
71b40 4f 6b 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 Ok(sqlite3*);.SQ
71b50 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
71b60 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 43 68 sqlite3SafetyCh
71b70 65 63 6b 53 69 63 6b 4f 72 4f 6b 28 73 71 6c 69 eckSickOrOk(sqli
71b80 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 te3*);.SQLITE_PR
71b90 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
71ba0 65 33 43 68 61 6e 67 65 43 6f 6f 6b 69 65 28 50 e3ChangeCookie(P
71bb0 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 0a 23 69 arse*, int);..#i
71bc0 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 f !defined(SQLIT
71bd0 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 26 26 20 E_OMIT_VIEW) &&
71be0 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f !defined(SQLITE_
71bf0 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 0a 53 51 OMIT_TRIGGER).SQ
71c00 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
71c10 64 20 73 71 6c 69 74 65 33 4d 61 74 65 72 69 61 d sqlite3Materia
71c20 6c 69 7a 65 56 69 65 77 28 50 61 72 73 65 2a 2c lizeView(Parse*,
71c30 20 54 61 62 6c 65 2a 2c 20 45 78 70 72 2a 2c 20 Table*, Expr*,
71c40 69 6e 74 29 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 int);.#endif..#i
71c50 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
71c60 54 5f 54 52 49 47 47 45 52 0a 53 51 4c 49 54 45 T_TRIGGER.SQLITE
71c70 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 _PRIVATE void
71c80 73 71 6c 69 74 65 33 42 65 67 69 6e 54 72 69 67 sqlite3BeginTrig
71c90 67 65 72 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65 ger(Parse*, Toke
71ca0 6e 2a 2c 54 6f 6b 65 6e 2a 2c 69 6e 74 2c 69 6e n*,Token*,int,in
71cb0 74 2c 49 64 4c 69 73 74 2a 2c 53 72 63 4c 69 73 t,IdList*,SrcLis
71cc0 74 2a 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 t*,.
71cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 45 E
71ce0 78 70 72 2a 2c 69 6e 74 2c 20 69 6e 74 29 3b 0a xpr*,int, int);.
71cf0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
71d00 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 69 6e void sqlite3Fin
71d10 69 73 68 54 72 69 67 67 65 72 28 50 61 72 73 65 ishTrigger(Parse
71d20 2a 2c 20 54 72 69 67 67 65 72 53 74 65 70 2a 2c *, TriggerStep*,
71d30 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 Token*);.SQLITE
71d40 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 _PRIVATE void
71d50 73 71 6c 69 74 65 33 44 72 6f 70 54 72 69 67 67 sqlite3DropTrigg
71d60 65 72 28 50 61 72 73 65 2a 2c 20 53 72 63 4c 69 er(Parse*, SrcLi
71d70 73 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 st*, int);.SQLIT
71d80 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 E_PRIVATE void
71d90 20 73 71 6c 69 74 65 33 44 72 6f 70 54 72 69 67 sqlite3DropTrig
71da0 67 65 72 50 74 72 28 50 61 72 73 65 2a 2c 20 54 gerPtr(Parse*, T
71db0 72 69 67 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 rigger*);.SQLITE
71dc0 5f 50 52 49 56 41 54 45 20 20 20 54 72 69 67 67 _PRIVATE Trigg
71dd0 65 72 20 2a 73 71 6c 69 74 65 33 54 72 69 67 67 er *sqlite3Trigg
71de0 65 72 73 45 78 69 73 74 28 50 61 72 73 65 20 2a ersExist(Parse *
71df0 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 2c 20 45 , Table*, int, E
71e00 78 70 72 4c 69 73 74 2a 2c 20 69 6e 74 20 2a 70 xprList*, int *p
71e10 4d 61 73 6b 29 3b 0a 53 51 4c 49 54 45 5f 50 52 Mask);.SQLITE_PR
71e20 49 56 41 54 45 20 20 20 54 72 69 67 67 65 72 20 IVATE Trigger
71e30 2a 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 4c *sqlite3TriggerL
71e40 69 73 74 28 50 61 72 73 65 20 2a 2c 20 54 61 62 ist(Parse *, Tab
71e50 6c 65 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 le *);.SQLITE_PR
71e60 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c IVATE void sql
71e70 69 74 65 33 43 6f 64 65 52 6f 77 54 72 69 67 67 ite3CodeRowTrigg
71e80 65 72 28 50 61 72 73 65 2a 2c 20 54 72 69 67 67 er(Parse*, Trigg
71e90 65 72 20 2a 2c 20 69 6e 74 2c 20 45 78 70 72 4c er *, int, ExprL
71ea0 69 73 74 2a 2c 20 69 6e 74 2c 20 54 61 62 6c 65 ist*, int, Table
71eb0 20 2a 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 *,.
71ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
71ed0 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a int, int, int);.
71ee0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
71ef0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 64 void sqlite3Cod
71f00 65 52 6f 77 54 72 69 67 67 65 72 44 69 72 65 63 eRowTriggerDirec
71f10 74 28 50 61 72 73 65 20 2a 2c 20 54 72 69 67 67 t(Parse *, Trigg
71f20 65 72 20 2a 2c 20 54 61 62 6c 65 20 2a 2c 20 69 er *, Table *, i
71f30 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 20 nt, int, int);.
71f40 20 76 6f 69 64 20 73 71 6c 69 74 65 56 69 65 77 void sqliteView
71f50 54 72 69 67 67 65 72 73 28 50 61 72 73 65 2a 2c Triggers(Parse*,
71f60 20 54 61 62 6c 65 2a 2c 20 45 78 70 72 2a 2c 20 Table*, Expr*,
71f70 69 6e 74 2c 20 45 78 70 72 4c 69 73 74 2a 29 3b int, ExprList*);
71f80 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
71f90 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65 void sqlite3De
71fa0 6c 65 74 65 54 72 69 67 67 65 72 53 74 65 70 28 leteTriggerStep(
71fb0 73 71 6c 69 74 65 33 2a 2c 20 54 72 69 67 67 65 sqlite3*, Trigge
71fc0 72 53 74 65 70 2a 29 3b 0a 53 51 4c 49 54 45 5f rStep*);.SQLITE_
71fd0 50 52 49 56 41 54 45 20 20 20 54 72 69 67 67 65 PRIVATE Trigge
71fe0 72 53 74 65 70 20 2a 73 71 6c 69 74 65 33 54 72 rStep *sqlite3Tr
71ff0 69 67 67 65 72 53 65 6c 65 63 74 53 74 65 70 28 iggerSelectStep(
72000 73 71 6c 69 74 65 33 2a 2c 53 65 6c 65 63 74 2a sqlite3*,Select*
72010 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
72020 45 20 20 20 54 72 69 67 67 65 72 53 74 65 70 20 E TriggerStep
72030 2a 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 49 *sqlite3TriggerI
72040 6e 73 65 72 74 53 74 65 70 28 73 71 6c 69 74 65 nsertStep(sqlite
72050 33 2a 2c 54 6f 6b 65 6e 2a 2c 20 49 64 4c 69 73 3*,Token*, IdLis
72060 74 2a 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 t*,.
72070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
72080 20 20 20 20 20 20 20 20 20 20 20 20 45 78 70 72 Expr
72090 4c 69 73 74 2a 2c 53 65 6c 65 63 74 2a 2c 75 38 List*,Select*,u8
720a0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
720b0 45 20 20 20 54 72 69 67 67 65 72 53 74 65 70 20 E TriggerStep
720c0 2a 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 55 *sqlite3TriggerU
720d0 70 64 61 74 65 53 74 65 70 28 73 71 6c 69 74 65 pdateStep(sqlite
720e0 33 2a 2c 54 6f 6b 65 6e 2a 2c 45 78 70 72 4c 69 3*,Token*,ExprLi
720f0 73 74 2a 2c 20 45 78 70 72 2a 2c 20 75 38 29 3b st*, Expr*, u8);
72100 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
72110 20 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 73 TriggerStep *s
72120 71 6c 69 74 65 33 54 72 69 67 67 65 72 44 65 6c qlite3TriggerDel
72130 65 74 65 53 74 65 70 28 73 71 6c 69 74 65 33 2a eteStep(sqlite3*
72140 2c 54 6f 6b 65 6e 2a 2c 20 45 78 70 72 2a 29 3b ,Token*, Expr*);
72150 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
72160 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65 void sqlite3De
72170 6c 65 74 65 54 72 69 67 67 65 72 28 73 71 6c 69 leteTrigger(sqli
72180 74 65 33 2a 2c 20 54 72 69 67 67 65 72 2a 29 3b te3*, Trigger*);
72190 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
721a0 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 55 6e void sqlite3Un
721b0 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 54 72 69 linkAndDeleteTri
721c0 67 67 65 72 28 73 71 6c 69 74 65 33 2a 2c 69 6e gger(sqlite3*,in
721d0 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a t,const char*);.
721e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
721f0 20 75 33 32 20 73 71 6c 69 74 65 33 54 72 69 67 u32 sqlite3Trig
72200 67 65 72 4f 6c 64 6d 61 73 6b 28 50 61 72 73 65 gerOldmask(Parse
72210 2a 2c 54 72 69 67 67 65 72 2a 2c 45 78 70 72 4c *,Trigger*,ExprL
72220 69 73 74 2a 2c 54 61 62 6c 65 2a 2c 69 6e 74 29 ist*,Table*,int)
72230 3b 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 ;.# define sqlit
72240 65 33 50 61 72 73 65 54 6f 70 6c 65 76 65 6c 28 e3ParseToplevel(
72250 70 29 20 28 28 70 29 2d 3e 70 54 6f 70 6c 65 76 p) ((p)->pToplev
72260 65 6c 20 3f 20 28 70 29 2d 3e 70 54 6f 70 6c 65 el ? (p)->pTople
72270 76 65 6c 20 3a 20 28 70 29 29 0a 23 65 6c 73 65 vel : (p)).#else
72280 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 .# define sqlite
72290 33 54 72 69 67 67 65 72 73 45 78 69 73 74 28 42 3TriggersExist(B
722a0 2c 43 2c 44 2c 45 2c 46 29 20 30 0a 23 20 64 65 ,C,D,E,F) 0.# de
722b0 66 69 6e 65 20 73 71 6c 69 74 65 33 44 65 6c 65 fine sqlite3Dele
722c0 74 65 54 72 69 67 67 65 72 28 41 2c 42 29 0a 23 teTrigger(A,B).#
722d0 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 44 define sqlite3D
722e0 72 6f 70 54 72 69 67 67 65 72 50 74 72 28 41 2c ropTriggerPtr(A,
722f0 42 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 B).# define sqli
72300 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 te3UnlinkAndDele
72310 74 65 54 72 69 67 67 65 72 28 41 2c 42 2c 43 29 teTrigger(A,B,C)
72320 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 .# define sqlite
72330 33 43 6f 64 65 52 6f 77 54 72 69 67 67 65 72 28 3CodeRowTrigger(
72340 41 2c 42 2c 43 2c 44 2c 45 2c 46 2c 47 2c 48 2c A,B,C,D,E,F,G,H,
72350 49 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 I).# define sqli
72360 74 65 33 43 6f 64 65 52 6f 77 54 72 69 67 67 65 te3CodeRowTrigge
72370 72 44 69 72 65 63 74 28 41 2c 42 2c 43 2c 44 2c rDirect(A,B,C,D,
72380 45 2c 46 29 0a 23 20 64 65 66 69 6e 65 20 73 71 E,F).# define sq
72390 6c 69 74 65 33 54 72 69 67 67 65 72 4c 69 73 74 lite3TriggerList
723a0 28 58 2c 20 59 29 20 30 0a 23 20 64 65 66 69 6e (X, Y) 0.# defin
723b0 65 20 73 71 6c 69 74 65 33 50 61 72 73 65 54 6f e sqlite3ParseTo
723c0 70 6c 65 76 65 6c 28 70 29 20 70 0a 23 20 64 65 plevel(p) p.# de
723d0 66 69 6e 65 20 73 71 6c 69 74 65 33 54 72 69 67 fine sqlite3Trig
723e0 67 65 72 4f 6c 64 6d 61 73 6b 28 41 2c 42 2c 43 gerOldmask(A,B,C
723f0 2c 44 2c 45 29 20 30 0a 23 65 6e 64 69 66 0a 0a ,D,E) 0.#endif..
72400 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
72410 6e 74 20 73 71 6c 69 74 65 33 4a 6f 69 6e 54 79 nt sqlite3JoinTy
72420 70 65 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e pe(Parse*, Token
72430 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e *, Token*, Token
72440 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
72450 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 TE void sqlite3C
72460 72 65 61 74 65 46 6f 72 65 69 67 6e 4b 65 79 28 reateForeignKey(
72470 50 61 72 73 65 2a 2c 20 45 78 70 72 4c 69 73 74 Parse*, ExprList
72480 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 45 78 70 72 4c *, Token*, ExprL
72490 69 73 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 ist*, int);.SQLI
724a0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
724b0 73 71 6c 69 74 65 33 44 65 66 65 72 46 6f 72 65 sqlite3DeferFore
724c0 69 67 6e 4b 65 79 28 50 61 72 73 65 2a 2c 20 69 ignKey(Parse*, i
724d0 6e 74 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c nt);.#ifndef SQL
724e0 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 ITE_OMIT_AUTHORI
724f0 5a 41 54 49 4f 4e 0a 53 51 4c 49 54 45 5f 50 52 ZATION.SQLITE_PR
72500 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c IVATE void sql
72510 69 74 65 33 41 75 74 68 52 65 61 64 28 50 61 72 ite3AuthRead(Par
72520 73 65 2a 2c 45 78 70 72 2a 2c 53 63 68 65 6d 61 se*,Expr*,Schema
72530 2a 2c 53 72 63 4c 69 73 74 2a 29 3b 0a 53 51 4c *,SrcList*);.SQL
72540 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e ITE_PRIVATE in
72550 74 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 t sqlite3AuthChe
72560 63 6b 28 50 61 72 73 65 2a 2c 69 6e 74 2c 20 63 ck(Parse*,int, c
72570 6f 6e 73 74 20 63 68 61 72 2a 2c 20 63 6f 6e 73 onst char*, cons
72580 74 20 63 68 61 72 2a 2c 20 63 6f 6e 73 74 20 63 t char*, const c
72590 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 har*);.SQLITE_PR
725a0 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c IVATE void sql
725b0 69 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74 50 ite3AuthContextP
725c0 75 73 68 28 50 61 72 73 65 2a 2c 20 41 75 74 68 ush(Parse*, Auth
725d0 43 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e 73 74 20 Context*, const
725e0 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 char*);.SQLITE_P
725f0 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 RIVATE void sq
72600 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74 lite3AuthContext
72610 50 6f 70 28 41 75 74 68 43 6f 6e 74 65 78 74 2a Pop(AuthContext*
72620 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
72630 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 41 E int sqlite3A
72640 75 74 68 52 65 61 64 43 6f 6c 28 50 61 72 73 65 uthReadCol(Parse
72650 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c *, const char *,
72660 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 69 const char *, i
72670 6e 74 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 nt);.#else.# def
72680 69 6e 65 20 73 71 6c 69 74 65 33 41 75 74 68 52 ine sqlite3AuthR
72690 65 61 64 28 61 2c 62 2c 63 2c 64 29 0a 23 20 64 ead(a,b,c,d).# d
726a0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 41 75 74 efine sqlite3Aut
726b0 68 43 68 65 63 6b 28 61 2c 62 2c 63 2c 64 2c 65 hCheck(a,b,c,d,e
726c0 29 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b 0a 23 ) SQLITE_OK.#
726d0 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 41 define sqlite3A
726e0 75 74 68 43 6f 6e 74 65 78 74 50 75 73 68 28 61 uthContextPush(a
726f0 2c 62 2c 63 29 0a 23 20 64 65 66 69 6e 65 20 73 ,b,c).# define s
72700 71 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 65 78 qlite3AuthContex
72710 74 50 6f 70 28 61 29 20 20 28 28 76 6f 69 64 29 tPop(a) ((void)
72720 28 61 29 29 0a 23 65 6e 64 69 66 0a 53 51 4c 49 (a)).#endif.SQLI
72730 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
72740 73 71 6c 69 74 65 33 41 74 74 61 63 68 28 50 61 sqlite3Attach(Pa
72750 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 45 78 70 rse*, Expr*, Exp
72760 72 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 r*, Expr*);.SQLI
72770 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
72780 73 71 6c 69 74 65 33 44 65 74 61 63 68 28 50 61 sqlite3Detach(Pa
72790 72 73 65 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 rse*, Expr*);.SQ
727a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
727b0 20 73 71 6c 69 74 65 33 42 74 72 65 65 46 61 63 sqlite3BtreeFac
727c0 74 6f 72 79 28 63 6f 6e 73 74 20 73 71 6c 69 74 tory(const sqlit
727d0 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 e3 *db, const ch
727e0 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 0a 20 ar *zFilename,.
727f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
72800 20 20 20 20 20 20 69 6e 74 20 6f 6d 69 74 4a 6f int omitJo
72810 75 72 6e 61 6c 2c 20 69 6e 74 20 6e 43 61 63 68 urnal, int nCach
72820 65 2c 20 69 6e 74 20 66 6c 61 67 73 2c 20 42 74 e, int flags, Bt
72830 72 65 65 20 2a 2a 70 70 42 74 72 65 65 29 3b 0a ree **ppBtree);.
72840 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
72850 6e 74 20 73 71 6c 69 74 65 33 46 69 78 49 6e 69 nt sqlite3FixIni
72860 74 28 44 62 46 69 78 65 72 2a 2c 20 50 61 72 73 t(DbFixer*, Pars
72870 65 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 63 e*, int, const c
72880 68 61 72 2a 2c 20 63 6f 6e 73 74 20 54 6f 6b 65 har*, const Toke
72890 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 n*);.SQLITE_PRIV
728a0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 ATE int sqlite3F
728b0 69 78 53 72 63 4c 69 73 74 28 44 62 46 69 78 65 ixSrcList(DbFixe
728c0 72 2a 2c 20 53 72 63 4c 69 73 74 2a 29 3b 0a 53 r*, SrcList*);.S
728d0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
728e0 74 20 73 71 6c 69 74 65 33 46 69 78 53 65 6c 65 t sqlite3FixSele
728f0 63 74 28 44 62 46 69 78 65 72 2a 2c 20 53 65 6c ct(DbFixer*, Sel
72900 65 63 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ect*);.SQLITE_PR
72910 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
72920 33 46 69 78 45 78 70 72 28 44 62 46 69 78 65 72 3FixExpr(DbFixer
72930 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 *, Expr*);.SQLIT
72940 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
72950 6c 69 74 65 33 46 69 78 45 78 70 72 4c 69 73 74 lite3FixExprList
72960 28 44 62 46 69 78 65 72 2a 2c 20 45 78 70 72 4c (DbFixer*, ExprL
72970 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ist*);.SQLITE_PR
72980 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
72990 33 46 69 78 54 72 69 67 67 65 72 53 74 65 70 28 3FixTriggerStep(
729a0 44 62 46 69 78 65 72 2a 2c 20 54 72 69 67 67 65 DbFixer*, Trigge
729b0 72 53 74 65 70 2a 29 3b 0a 53 51 4c 49 54 45 5f rStep*);.SQLITE_
729c0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
729d0 74 65 33 41 74 6f 46 28 63 6f 6e 73 74 20 63 68 te3AtoF(const ch
729e0 61 72 20 2a 7a 2c 20 64 6f 75 62 6c 65 2a 29 3b ar *z, double*);
729f0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
72a00 69 6e 74 20 73 71 6c 69 74 65 33 47 65 74 49 6e int sqlite3GetIn
72a10 74 33 32 28 63 6f 6e 73 74 20 63 68 61 72 20 2a t32(const char *
72a20 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f , int*);.SQLITE_
72a30 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
72a40 74 65 33 46 69 74 73 49 6e 36 34 42 69 74 73 28 te3FitsIn64Bits(
72a50 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 69 6e const char *, in
72a60 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
72a70 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 55 74 TE int sqlite3Ut
72a80 66 31 36 42 79 74 65 4c 65 6e 28 63 6f 6e 73 74 f16ByteLen(const
72a90 20 76 6f 69 64 20 2a 70 44 61 74 61 2c 20 69 6e void *pData, in
72aa0 74 20 6e 43 68 61 72 29 3b 0a 53 51 4c 49 54 45 t nChar);.SQLITE
72ab0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
72ac0 69 74 65 33 55 74 66 38 43 68 61 72 4c 65 6e 28 ite3Utf8CharLen(
72ad0 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44 61 74 const char *pDat
72ae0 61 2c 20 69 6e 74 20 6e 42 79 74 65 29 3b 0a 53 a, int nByte);.S
72af0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
72b00 74 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 t sqlite3Utf8Rea
72b10 64 28 63 6f 6e 73 74 20 75 38 2a 2c 20 63 6f 6e d(const u8*, con
72b20 73 74 20 75 38 2a 2a 29 3b 0a 0a 2f 2a 0a 2a 2a st u8**);../*.**
72b30 20 52 6f 75 74 69 6e 65 73 20 74 6f 20 72 65 61 Routines to rea
72b40 64 20 61 6e 64 20 77 72 69 74 65 20 76 61 72 69 d and write vari
72b50 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 able-length inte
72b60 67 65 72 73 2e 20 20 54 68 65 73 65 20 75 73 65 gers. These use
72b70 64 20 74 6f 0a 2a 2a 20 62 65 20 64 65 66 69 6e d to.** be defin
72b80 65 64 20 6c 6f 63 61 6c 6c 79 2c 20 62 75 74 20 ed locally, but
72b90 6e 6f 77 20 77 65 20 75 73 65 20 74 68 65 20 76 now we use the v
72ba0 61 72 69 6e 74 20 72 6f 75 74 69 6e 65 73 20 69 arint routines i
72bb0 6e 20 74 68 65 20 75 74 69 6c 2e 63 0a 2a 2a 20 n the util.c.**
72bc0 66 69 6c 65 2e 20 20 43 6f 64 65 20 73 68 6f 75 file. Code shou
72bd0 6c 64 20 75 73 65 20 74 68 65 20 4d 41 43 52 4f ld use the MACRO
72be0 20 66 6f 72 6d 73 20 62 65 6c 6f 77 2c 20 61 73 forms below, as
72bf0 20 74 68 65 20 56 61 72 69 6e 74 33 32 20 76 65 the Varint32 ve
72c00 72 73 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 63 6f rsions.** are co
72c10 64 65 64 20 74 6f 20 61 73 73 75 6d 65 20 74 68 ded to assume th
72c20 65 20 73 69 6e 67 6c 65 20 62 79 74 65 20 63 61 e single byte ca
72c30 73 65 20 69 73 20 61 6c 72 65 61 64 79 20 68 61 se is already ha
72c40 6e 64 6c 65 64 20 28 77 68 69 63 68 20 0a 2a 2a ndled (which .**
72c50 20 74 68 65 20 4d 41 43 52 4f 20 66 6f 72 6d 20 the MACRO form
72c60 64 6f 65 73 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 does)..*/.SQLITE
72c70 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
72c80 69 74 65 33 50 75 74 56 61 72 69 6e 74 28 75 6e ite3PutVarint(un
72c90 73 69 67 6e 65 64 20 63 68 61 72 2a 2c 20 75 36 signed char*, u6
72ca0 34 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 4);.SQLITE_PRIVA
72cb0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 75 TE int sqlite3Pu
72cc0 74 56 61 72 69 6e 74 33 32 28 75 6e 73 69 67 6e tVarint32(unsign
72cd0 65 64 20 63 68 61 72 2a 2c 20 75 33 32 29 3b 0a ed char*, u32);.
72ce0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75 SQLITE_PRIVATE u
72cf0 38 20 73 71 6c 69 74 65 33 47 65 74 56 61 72 69 8 sqlite3GetVari
72d00 6e 74 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 nt(const unsigne
72d10 64 20 63 68 61 72 20 2a 2c 20 75 36 34 20 2a 29 d char *, u64 *)
72d20 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
72d30 20 75 38 20 73 71 6c 69 74 65 33 47 65 74 56 61 u8 sqlite3GetVa
72d40 72 69 6e 74 33 32 28 63 6f 6e 73 74 20 75 6e 73 rint32(const uns
72d50 69 67 6e 65 64 20 63 68 61 72 20 2a 2c 20 75 33 igned char *, u3
72d60 32 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 2 *);.SQLITE_PRI
72d70 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
72d80 56 61 72 69 6e 74 4c 65 6e 28 75 36 34 20 76 29 VarintLen(u64 v)
72d90 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 68 65 61 ;../*.** The hea
72da0 64 65 72 20 6f 66 20 61 20 72 65 63 6f 72 64 20 der of a record
72db0 63 6f 6e 73 69 73 74 73 20 6f 66 20 61 20 73 65 consists of a se
72dc0 71 75 65 6e 63 65 20 76 61 72 69 61 62 6c 65 2d quence variable-
72dd0 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 73 2e length integers.
72de0 0a 2a 2a 20 54 68 65 73 65 20 69 6e 74 65 67 65 .** These intege
72df0 72 73 20 61 72 65 20 61 6c 6d 6f 73 74 20 61 6c rs are almost al
72e00 77 61 79 73 20 73 6d 61 6c 6c 20 61 6e 64 20 61 ways small and a
72e10 72 65 20 65 6e 63 6f 64 65 64 20 61 73 20 61 20 re encoded as a
72e20 73 69 6e 67 6c 65 20 62 79 74 65 2e 0a 2a 2a 20 single byte..**
72e30 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 The following ma
72e40 63 72 6f 73 20 74 61 6b 65 20 61 64 76 61 6e 74 cros take advant
72e50 61 67 65 20 74 68 69 73 20 66 61 63 74 20 74 6f age this fact to
72e60 20 70 72 6f 76 69 64 65 20 61 20 66 61 73 74 20 provide a fast
72e70 65 6e 63 6f 64 65 0a 2a 2a 20 61 6e 64 20 64 65 encode.** and de
72e80 63 6f 64 65 20 6f 66 20 74 68 65 20 69 6e 74 65 code of the inte
72e90 67 65 72 73 20 69 6e 20 61 20 72 65 63 6f 72 64 gers in a record
72ea0 20 68 65 61 64 65 72 2e 20 20 49 74 20 69 73 20 header. It is
72eb0 66 61 73 74 65 72 20 66 6f 72 20 74 68 65 20 63 faster for the c
72ec0 6f 6d 6d 6f 6e 0a 2a 2a 20 63 61 73 65 20 77 68 ommon.** case wh
72ed0 65 72 65 20 74 68 65 20 69 6e 74 65 67 65 72 20 ere the integer
72ee0 69 73 20 61 20 73 69 6e 67 6c 65 20 62 79 74 65 is a single byte
72ef0 2e 20 20 49 74 20 69 73 20 61 20 6c 69 74 74 6c . It is a littl
72f00 65 20 73 6c 6f 77 65 72 20 77 68 65 6e 20 74 68 e slower when th
72f10 65 0a 2a 2a 20 69 6e 74 65 67 65 72 20 69 73 20 e.** integer is
72f20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 62 79 74 65 two or more byte
72f30 73 2e 20 20 42 75 74 20 6f 76 65 72 61 6c 6c 20 s. But overall
72f40 69 74 20 69 73 20 66 61 73 74 65 72 2e 0a 2a 2a it is faster..**
72f50 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e .** The followin
72f60 67 20 65 78 70 72 65 73 73 69 6f 6e 73 20 61 72 g expressions ar
72f70 65 20 65 71 75 69 76 61 6c 65 6e 74 3a 0a 2a 2a e equivalent:.**
72f80 0a 2a 2a 20 20 20 20 20 78 20 3d 20 73 71 6c 69 .** x = sqli
72f90 74 65 33 47 65 74 56 61 72 69 6e 74 33 32 28 20 te3GetVarint32(
72fa0 41 2c 20 26 42 20 29 3b 0a 2a 2a 20 20 20 20 20 A, &B );.**
72fb0 78 20 3d 20 73 71 6c 69 74 65 33 50 75 74 56 61 x = sqlite3PutVa
72fc0 72 69 6e 74 33 32 28 20 41 2c 20 42 20 29 3b 0a rint32( A, B );.
72fd0 2a 2a 0a 2a 2a 20 20 20 20 20 78 20 3d 20 67 65 **.** x = ge
72fe0 74 56 61 72 69 6e 74 33 32 28 20 41 2c 20 42 20 tVarint32( A, B
72ff0 29 3b 0a 2a 2a 20 20 20 20 20 78 20 3d 20 70 75 );.** x = pu
73000 74 56 61 72 69 6e 74 33 32 28 20 41 2c 20 42 20 tVarint32( A, B
73010 29 3b 0a 2a 2a 0a 2a 2f 0a 23 64 65 66 69 6e 65 );.**.*/.#define
73020 20 67 65 74 56 61 72 69 6e 74 33 32 28 41 2c 42 getVarint32(A,B
73030 29 20 20 28 75 38 29 28 28 2a 28 41 29 3c 28 75 ) (u8)((*(A)<(u
73040 38 29 30 78 38 30 29 20 3f 20 28 28 42 29 20 3d 8)0x80) ? ((B) =
73050 20 28 75 33 32 29 2a 28 41 29 29 2c 31 20 3a 20 (u32)*(A)),1 :
73060 73 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74 sqlite3GetVarint
73070 33 32 28 28 41 29 2c 20 28 75 33 32 20 2a 29 26 32((A), (u32 *)&
73080 28 42 29 29 29 0a 23 64 65 66 69 6e 65 20 70 75 (B))).#define pu
73090 74 56 61 72 69 6e 74 33 32 28 41 2c 42 29 20 20 tVarint32(A,B)
730a0 28 75 38 29 28 28 28 75 33 32 29 28 42 29 3c 28 (u8)(((u32)(B)<(
730b0 75 33 32 29 30 78 38 30 29 20 3f 20 28 2a 28 41 u32)0x80) ? (*(A
730c0 29 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 ) = (unsigned ch
730d0 61 72 29 28 42 29 29 2c 31 20 3a 20 73 71 6c 69 ar)(B)),1 : sqli
730e0 74 65 33 50 75 74 56 61 72 69 6e 74 33 32 28 28 te3PutVarint32((
730f0 41 29 2c 20 28 42 29 29 29 0a 23 64 65 66 69 6e A), (B))).#defin
73100 65 20 67 65 74 56 61 72 69 6e 74 20 20 20 20 73 e getVarint s
73110 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74 0a qlite3GetVarint.
73120 23 64 65 66 69 6e 65 20 70 75 74 56 61 72 69 6e #define putVarin
73130 74 20 20 20 20 73 71 6c 69 74 65 33 50 75 74 56 t sqlite3PutV
73140 61 72 69 6e 74 0a 0a 0a 53 51 4c 49 54 45 5f 50 arint...SQLITE_P
73150 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 RIVATE const cha
73160 72 20 2a 73 71 6c 69 74 65 33 49 6e 64 65 78 41 r *sqlite3IndexA
73170 66 66 69 6e 69 74 79 53 74 72 28 56 64 62 65 20 ffinityStr(Vdbe
73180 2a 2c 20 49 6e 64 65 78 20 2a 29 3b 0a 53 51 4c *, Index *);.SQL
73190 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
731a0 20 73 71 6c 69 74 65 33 54 61 62 6c 65 41 66 66 sqlite3TableAff
731b0 69 6e 69 74 79 53 74 72 28 56 64 62 65 20 2a 2c inityStr(Vdbe *,
731c0 20 54 61 62 6c 65 20 2a 29 3b 0a 53 51 4c 49 54 Table *);.SQLIT
731d0 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 73 E_PRIVATE char s
731e0 71 6c 69 74 65 33 43 6f 6d 70 61 72 65 41 66 66 qlite3CompareAff
731f0 69 6e 69 74 79 28 45 78 70 72 20 2a 70 45 78 70 inity(Expr *pExp
73200 72 2c 20 63 68 61 72 20 61 66 66 32 29 3b 0a 53 r, char aff2);.S
73210 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
73220 74 20 73 71 6c 69 74 65 33 49 6e 64 65 78 41 66 t sqlite3IndexAf
73230 66 69 6e 69 74 79 4f 6b 28 45 78 70 72 20 2a 70 finityOk(Expr *p
73240 45 78 70 72 2c 20 63 68 61 72 20 69 64 78 5f 61 Expr, char idx_a
73250 66 66 69 6e 69 74 79 29 3b 0a 53 51 4c 49 54 45 ffinity);.SQLITE
73260 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 73 71 _PRIVATE char sq
73270 6c 69 74 65 33 45 78 70 72 41 66 66 69 6e 69 74 lite3ExprAffinit
73280 79 28 45 78 70 72 20 2a 70 45 78 70 72 29 3b 0a y(Expr *pExpr);.
73290 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
732a0 6e 74 20 73 71 6c 69 74 65 33 41 74 6f 69 36 34 nt sqlite3Atoi64
732b0 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69 36 (const char*, i6
732c0 34 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 4*);.SQLITE_PRIV
732d0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
732e0 45 72 72 6f 72 28 73 71 6c 69 74 65 33 2a 2c 20 Error(sqlite3*,
732f0 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a int, const char*
73300 2c 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ,...);.SQLITE_PR
73310 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 IVATE void *sqli
73320 74 65 33 48 65 78 54 6f 42 6c 6f 62 28 73 71 6c te3HexToBlob(sql
73330 69 74 65 33 2a 2c 20 63 6f 6e 73 74 20 63 68 61 ite3*, const cha
73340 72 20 2a 7a 2c 20 69 6e 74 20 6e 29 3b 0a 53 51 r *z, int n);.SQ
73350 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
73360 20 73 71 6c 69 74 65 33 54 77 6f 50 61 72 74 4e sqlite3TwoPartN
73370 61 6d 65 28 50 61 72 73 65 20 2a 2c 20 54 6f 6b ame(Parse *, Tok
73380 65 6e 20 2a 2c 20 54 6f 6b 65 6e 20 2a 2c 20 54 en *, Token *, T
73390 6f 6b 65 6e 20 2a 2a 29 3b 0a 53 51 4c 49 54 45 oken **);.SQLITE
733a0 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 _PRIVATE const c
733b0 68 61 72 20 2a 73 71 6c 69 74 65 33 45 72 72 53 har *sqlite3ErrS
733c0 74 72 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f tr(int);.SQLITE_
733d0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
733e0 74 65 33 52 65 61 64 53 63 68 65 6d 61 28 50 61 te3ReadSchema(Pa
733f0 72 73 65 20 2a 70 50 61 72 73 65 29 3b 0a 53 51 rse *pParse);.SQ
73400 4c 49 54 45 5f 50 52 49 56 41 54 45 20 43 6f 6c LITE_PRIVATE Col
73410 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 46 69 6e lSeq *sqlite3Fin
73420 64 43 6f 6c 6c 53 65 71 28 73 71 6c 69 74 65 33 dCollSeq(sqlite3
73430 2a 2c 75 38 20 65 6e 63 2c 20 63 6f 6e 73 74 20 *,u8 enc, const
73440 63 68 61 72 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 char*,int);.SQLI
73450 54 45 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 TE_PRIVATE CollS
73460 65 71 20 2a 73 71 6c 69 74 65 33 4c 6f 63 61 74 eq *sqlite3Locat
73470 65 43 6f 6c 6c 53 65 71 28 50 61 72 73 65 20 2a eCollSeq(Parse *
73480 70 50 61 72 73 65 2c 20 63 6f 6e 73 74 20 63 68 pParse, const ch
73490 61 72 2a 7a 4e 61 6d 65 29 3b 0a 53 51 4c 49 54 ar*zName);.SQLIT
734a0 45 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 65 E_PRIVATE CollSe
734b0 71 20 2a 73 71 6c 69 74 65 33 45 78 70 72 43 6f q *sqlite3ExprCo
734c0 6c 6c 53 65 71 28 50 61 72 73 65 20 2a 70 50 61 llSeq(Parse *pPa
734d0 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72 rse, Expr *pExpr
734e0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
734f0 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45 E Expr *sqlite3E
73500 78 70 72 53 65 74 43 6f 6c 6c 28 50 61 72 73 65 xprSetColl(Parse
73510 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a *pParse, Expr *
73520 2c 20 54 6f 6b 65 6e 20 2a 29 3b 0a 53 51 4c 49 , Token *);.SQLI
73530 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
73540 71 6c 69 74 65 33 43 68 65 63 6b 43 6f 6c 6c 53 qlite3CheckCollS
73550 65 71 28 50 61 72 73 65 20 2a 2c 20 43 6f 6c 6c eq(Parse *, Coll
73560 53 65 71 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 Seq *);.SQLITE_P
73570 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
73580 65 33 43 68 65 63 6b 4f 62 6a 65 63 74 4e 61 6d e3CheckObjectNam
73590 65 28 50 61 72 73 65 20 2a 2c 20 63 6f 6e 73 74 e(Parse *, const
735a0 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c 49 54 45 char *);.SQLITE
735b0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
735c0 6c 69 74 65 33 56 64 62 65 53 65 74 43 68 61 6e lite3VdbeSetChan
735d0 67 65 73 28 73 71 6c 69 74 65 33 20 2a 2c 20 69 ges(sqlite3 *, i
735e0 6e 74 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 nt);..SQLITE_PRI
735f0 56 41 54 45 20 63 6f 6e 73 74 20 76 6f 69 64 20 VATE const void
73600 2a 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 *sqlite3ValueTex
73610 74 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a t(sqlite3_value*
73620 2c 20 75 38 29 3b 0a 53 51 4c 49 54 45 5f 50 52 , u8);.SQLITE_PR
73630 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
73640 33 56 61 6c 75 65 42 79 74 65 73 28 73 71 6c 69 3ValueBytes(sqli
73650 74 65 33 5f 76 61 6c 75 65 2a 2c 20 75 38 29 3b te3_value*, u8);
73660 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
73670 76 6f 69 64 20 73 71 6c 69 74 65 33 56 61 6c 75 void sqlite3Valu
73680 65 53 65 74 53 74 72 28 73 71 6c 69 74 65 33 5f eSetStr(sqlite3_
73690 76 61 6c 75 65 2a 2c 20 69 6e 74 2c 20 63 6f 6e value*, int, con
736a0 73 74 20 76 6f 69 64 20 2a 2c 75 38 2c 20 0a 20 st void *,u8, .
736b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
736c0 20 20 20 20 20 20 20 76 6f 69 64 28 2a 29 28 76 void(*)(v
736d0 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 50 oid*));.SQLITE_P
736e0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
736f0 74 65 33 56 61 6c 75 65 46 72 65 65 28 73 71 6c te3ValueFree(sql
73700 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 ite3_value*);.SQ
73710 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c LITE_PRIVATE sql
73720 69 74 65 33 5f 76 61 6c 75 65 20 2a 73 71 6c 69 ite3_value *sqli
73730 74 65 33 56 61 6c 75 65 4e 65 77 28 73 71 6c 69 te3ValueNew(sqli
73740 74 65 33 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 te3 *);.SQLITE_P
73750 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c RIVATE char *sql
73760 69 74 65 33 55 74 66 31 36 74 6f 38 28 73 71 6c ite3Utf16to8(sql
73770 69 74 65 33 20 2a 2c 20 63 6f 6e 73 74 20 76 6f ite3 *, const vo
73780 69 64 2a 2c 20 69 6e 74 29 3b 0a 23 69 66 64 65 id*, int);.#ifde
73790 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f f SQLITE_ENABLE_
737a0 53 54 41 54 32 0a 53 51 4c 49 54 45 5f 50 52 49 STAT2.SQLITE_PRI
737b0 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 VATE char *sqlit
737c0 65 33 55 74 66 38 74 6f 31 36 28 73 71 6c 69 74 e3Utf8to16(sqlit
737d0 65 33 20 2a 2c 20 75 38 2c 20 63 68 61 72 20 2a e3 *, u8, char *
737e0 2c 20 69 6e 74 2c 20 69 6e 74 20 2a 29 3b 0a 23 , int, int *);.#
737f0 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 endif.SQLITE_PRI
73800 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
73810 56 61 6c 75 65 46 72 6f 6d 45 78 70 72 28 73 71 ValueFromExpr(sq
73820 6c 69 74 65 33 20 2a 2c 20 45 78 70 72 20 2a 2c lite3 *, Expr *,
73830 20 75 38 2c 20 75 38 2c 20 73 71 6c 69 74 65 33 u8, u8, sqlite3
73840 5f 76 61 6c 75 65 20 2a 2a 29 3b 0a 53 51 4c 49 _value **);.SQLI
73850 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
73860 73 71 6c 69 74 65 33 56 61 6c 75 65 41 70 70 6c sqlite3ValueAppl
73870 79 41 66 66 69 6e 69 74 79 28 73 71 6c 69 74 65 yAffinity(sqlite
73880 33 5f 76 61 6c 75 65 20 2a 2c 20 75 38 2c 20 75 3_value *, u8, u
73890 38 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 8);.#ifndef SQLI
738a0 54 45 5f 41 4d 41 4c 47 41 4d 41 54 49 4f 4e 0a TE_AMALGAMATION.
738b0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 SQLITE_PRIVATE c
738c0 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 onst unsigned ch
738d0 61 72 20 73 71 6c 69 74 65 33 55 70 70 65 72 54 ar sqlite3UpperT
738e0 6f 4c 6f 77 65 72 5b 5d 3b 0a 53 51 4c 49 54 45 oLower[];.SQLITE
738f0 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 75 _PRIVATE const u
73900 6e 73 69 67 6e 65 64 20 63 68 61 72 20 73 71 6c nsigned char sql
73910 69 74 65 33 43 74 79 70 65 4d 61 70 5b 5d 3b 0a ite3CtypeMap[];.
73920 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53 SQLITE_PRIVATE S
73930 51 4c 49 54 45 5f 57 53 44 20 73 74 72 75 63 74 QLITE_WSD struct
73940 20 53 71 6c 69 74 65 33 43 6f 6e 66 69 67 20 73 Sqlite3Config s
73950 71 6c 69 74 65 33 43 6f 6e 66 69 67 3b 0a 53 51 qlite3Config;.SQ
73960 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53 51 4c LITE_PRIVATE SQL
73970 49 54 45 5f 57 53 44 20 46 75 6e 63 44 65 66 48 ITE_WSD FuncDefH
73980 61 73 68 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 ash sqlite3Globa
73990 6c 46 75 6e 63 74 69 6f 6e 73 3b 0a 53 51 4c 49 lFunctions;.SQLI
739a0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
739b0 71 6c 69 74 65 33 50 65 6e 64 69 6e 67 42 79 74 qlite3PendingByt
739c0 65 3b 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 45 e;.#endif.SQLITE
739d0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
739e0 6c 69 74 65 33 52 6f 6f 74 50 61 67 65 4d 6f 76 lite3RootPageMov
739f0 65 64 28 44 62 2a 2c 20 69 6e 74 2c 20 69 6e 74 ed(Db*, int, int
73a00 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
73a10 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65 E void sqlite3Re
73a20 69 6e 64 65 78 28 50 61 72 73 65 2a 2c 20 54 6f index(Parse*, To
73a30 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 ken*, Token*);.S
73a40 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
73a50 69 64 20 73 71 6c 69 74 65 33 41 6c 74 65 72 46 id sqlite3AlterF
73a60 75 6e 63 74 69 6f 6e 73 28 73 71 6c 69 74 65 33 unctions(sqlite3
73a70 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
73a80 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 TE void sqlite3A
73a90 6c 74 65 72 52 65 6e 61 6d 65 54 61 62 6c 65 28 lterRenameTable(
73aa0 50 61 72 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a Parse*, SrcList*
73ab0 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 , Token*);.SQLIT
73ac0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
73ad0 6c 69 74 65 33 47 65 74 54 6f 6b 65 6e 28 63 6f lite3GetToken(co
73ae0 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 nst unsigned cha
73af0 72 20 2a 2c 20 69 6e 74 20 2a 29 3b 0a 53 51 4c r *, int *);.SQL
73b00 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
73b10 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 sqlite3NestedPa
73b20 72 73 65 28 50 61 72 73 65 2a 2c 20 63 6f 6e 73 rse(Parse*, cons
73b30 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 53 t char*, ...);.S
73b40 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
73b50 69 64 20 73 71 6c 69 74 65 33 45 78 70 69 72 65 id sqlite3Expire
73b60 50 72 65 70 61 72 65 64 53 74 61 74 65 6d 65 6e PreparedStatemen
73b70 74 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 ts(sqlite3*);.SQ
73b80 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
73b90 64 20 73 71 6c 69 74 65 33 43 6f 64 65 53 75 62 d sqlite3CodeSub
73ba0 73 65 6c 65 63 74 28 50 61 72 73 65 20 2a 2c 20 select(Parse *,
73bb0 45 78 70 72 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 Expr *, int, int
73bc0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
73bd0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 65 E void sqlite3Se
73be0 6c 65 63 74 50 72 65 70 28 50 61 72 73 65 2a 2c lectPrep(Parse*,
73bf0 20 53 65 6c 65 63 74 2a 2c 20 4e 61 6d 65 43 6f Select*, NameCo
73c00 6e 74 65 78 74 2a 29 3b 0a 53 51 4c 49 54 45 5f ntext*);.SQLITE_
73c10 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
73c20 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 te3ResolveExprNa
73c30 6d 65 73 28 4e 61 6d 65 43 6f 6e 74 65 78 74 2a mes(NameContext*
73c40 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 , Expr*);.SQLITE
73c50 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
73c60 6c 69 74 65 33 52 65 73 6f 6c 76 65 53 65 6c 65 lite3ResolveSele
73c70 63 74 4e 61 6d 65 73 28 50 61 72 73 65 2a 2c 20 ctNames(Parse*,
73c80 53 65 6c 65 63 74 2a 2c 20 4e 61 6d 65 43 6f 6e Select*, NameCon
73c90 74 65 78 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 text*);.SQLITE_P
73ca0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
73cb0 65 33 52 65 73 6f 6c 76 65 4f 72 64 65 72 47 72 e3ResolveOrderGr
73cc0 6f 75 70 42 79 28 50 61 72 73 65 2a 2c 20 53 65 oupBy(Parse*, Se
73cd0 6c 65 63 74 2a 2c 20 45 78 70 72 4c 69 73 74 2a lect*, ExprList*
73ce0 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a , const char*);.
73cf0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
73d00 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6c 75 6d oid sqlite3Colum
73d10 6e 44 65 66 61 75 6c 74 28 56 64 62 65 20 2a 2c nDefault(Vdbe *,
73d20 20 54 61 62 6c 65 20 2a 2c 20 69 6e 74 2c 20 69 Table *, int, i
73d30 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
73d40 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
73d50 41 6c 74 65 72 46 69 6e 69 73 68 41 64 64 43 6f AlterFinishAddCo
73d60 6c 75 6d 6e 28 50 61 72 73 65 20 2a 2c 20 54 6f lumn(Parse *, To
73d70 6b 65 6e 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 ken *);.SQLITE_P
73d80 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
73d90 74 65 33 41 6c 74 65 72 42 65 67 69 6e 41 64 64 te3AlterBeginAdd
73da0 43 6f 6c 75 6d 6e 28 50 61 72 73 65 20 2a 2c 20 Column(Parse *,
73db0 53 72 63 4c 69 73 74 20 2a 29 3b 0a 53 51 4c 49 SrcList *);.SQLI
73dc0 54 45 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 TE_PRIVATE CollS
73dd0 65 71 20 2a 73 71 6c 69 74 65 33 47 65 74 43 6f eq *sqlite3GetCo
73de0 6c 6c 53 65 71 28 73 71 6c 69 74 65 33 2a 2c 20 llSeq(sqlite3*,
73df0 75 38 2c 20 43 6f 6c 6c 53 65 71 20 2a 2c 20 63 u8, CollSeq *, c
73e00 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c onst char*);.SQL
73e10 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 ITE_PRIVATE char
73e20 20 73 71 6c 69 74 65 33 41 66 66 69 6e 69 74 79 sqlite3Affinity
73e30 54 79 70 65 28 63 6f 6e 73 74 20 63 68 61 72 2a Type(const char*
73e40 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
73e50 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 6e E void sqlite3An
73e60 61 6c 79 7a 65 28 50 61 72 73 65 2a 2c 20 54 6f alyze(Parse*, To
73e70 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 ken*, Token*);.S
73e80 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
73e90 74 20 73 71 6c 69 74 65 33 49 6e 76 6f 6b 65 42 t sqlite3InvokeB
73ea0 75 73 79 48 61 6e 64 6c 65 72 28 42 75 73 79 48 usyHandler(BusyH
73eb0 61 6e 64 6c 65 72 2a 29 3b 0a 53 51 4c 49 54 45 andler*);.SQLITE
73ec0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
73ed0 69 74 65 33 46 69 6e 64 44 62 28 73 71 6c 69 74 ite3FindDb(sqlit
73ee0 65 33 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 e3*, Token*);.SQ
73ef0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
73f00 20 73 71 6c 69 74 65 33 46 69 6e 64 44 62 4e 61 sqlite3FindDbNa
73f10 6d 65 28 73 71 6c 69 74 65 33 20 2a 2c 20 63 6f me(sqlite3 *, co
73f20 6e 73 74 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c nst char *);.SQL
73f30 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
73f40 73 71 6c 69 74 65 33 41 6e 61 6c 79 73 69 73 4c sqlite3AnalysisL
73f50 6f 61 64 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74 oad(sqlite3*,int
73f60 20 69 44 42 29 3b 0a 53 51 4c 49 54 45 5f 50 52 iDB);.SQLITE_PR
73f70 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
73f80 65 33 44 65 6c 65 74 65 49 6e 64 65 78 53 61 6d e3DeleteIndexSam
73f90 70 6c 65 73 28 49 6e 64 65 78 2a 29 3b 0a 53 51 ples(Index*);.SQ
73fa0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
73fb0 64 20 73 71 6c 69 74 65 33 44 65 66 61 75 6c 74 d sqlite3Default
73fc0 52 6f 77 45 73 74 28 49 6e 64 65 78 2a 29 3b 0a RowEst(Index*);.
73fd0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
73fe0 6f 69 64 20 73 71 6c 69 74 65 33 52 65 67 69 73 oid sqlite3Regis
73ff0 74 65 72 4c 69 6b 65 46 75 6e 63 74 69 6f 6e 73 terLikeFunctions
74000 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 29 3b (sqlite3*, int);
74010 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
74020 69 6e 74 20 73 71 6c 69 74 65 33 49 73 4c 69 6b int sqlite3IsLik
74030 65 46 75 6e 63 74 69 6f 6e 28 73 71 6c 69 74 65 eFunction(sqlite
74040 33 2a 2c 45 78 70 72 2a 2c 69 6e 74 2a 2c 63 68 3*,Expr*,int*,ch
74050 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ar*);.SQLITE_PRI
74060 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
74070 33 4d 69 6e 69 6d 75 6d 46 69 6c 65 46 6f 72 6d 3MinimumFileForm
74080 61 74 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 at(Parse*, int,
74090 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
740a0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
740b0 33 53 63 68 65 6d 61 46 72 65 65 28 76 6f 69 64 3SchemaFree(void
740c0 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 *);.SQLITE_PRIV
740d0 41 54 45 20 53 63 68 65 6d 61 20 2a 73 71 6c 69 ATE Schema *sqli
740e0 74 65 33 53 63 68 65 6d 61 47 65 74 28 73 71 6c te3SchemaGet(sql
740f0 69 74 65 33 20 2a 2c 20 42 74 72 65 65 20 2a 29 ite3 *, Btree *)
74100 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
74110 20 69 6e 74 20 73 71 6c 69 74 65 33 53 63 68 65 int sqlite3Sche
74120 6d 61 54 6f 49 6e 64 65 78 28 73 71 6c 69 74 65 maToIndex(sqlite
74130 33 20 2a 64 62 2c 20 53 63 68 65 6d 61 20 2a 29 3 *db, Schema *)
74140 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
74150 20 4b 65 79 49 6e 66 6f 20 2a 73 71 6c 69 74 65 KeyInfo *sqlite
74160 33 49 6e 64 65 78 4b 65 79 69 6e 66 6f 28 50 61 3IndexKeyinfo(Pa
74170 72 73 65 20 2a 2c 20 49 6e 64 65 78 20 2a 29 3b rse *, Index *);
74180 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
74190 69 6e 74 20 73 71 6c 69 74 65 33 43 72 65 61 74 int sqlite3Creat
741a0 65 46 75 6e 63 28 73 71 6c 69 74 65 33 20 2a 2c eFunc(sqlite3 *,
741b0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 69 const char *, i
741c0 6e 74 2c 20 69 6e 74 2c 20 76 6f 69 64 20 2a 2c nt, int, void *,
741d0 20 0a 20 20 76 6f 69 64 20 28 2a 29 28 73 71 6c . void (*)(sql
741e0 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e ite3_context*,in
741f0 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 t,sqlite3_value
74200 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a 29 28 **),. void (*)(
74210 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a sqlite3_context*
74220 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c ,int,sqlite3_val
74230 75 65 20 2a 2a 29 2c 20 76 6f 69 64 20 28 2a 29 ue **), void (*)
74240 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 (sqlite3_context
74250 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 *));.SQLITE_PRIV
74260 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 41 ATE int sqlite3A
74270 70 69 45 78 69 74 28 73 71 6c 69 74 65 33 20 2a piExit(sqlite3 *
74280 64 62 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 db, int);.SQLITE
74290 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
742a0 69 74 65 33 4f 70 65 6e 54 65 6d 70 44 61 74 61 ite3OpenTempData
742b0 62 61 73 65 28 50 61 72 73 65 20 2a 29 3b 0a 0a base(Parse *);..
742c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
742d0 6f 69 64 20 73 71 6c 69 74 65 33 53 74 72 41 63 oid sqlite3StrAc
742e0 63 75 6d 49 6e 69 74 28 53 74 72 41 63 63 75 6d cumInit(StrAccum
742f0 2a 2c 20 63 68 61 72 2a 2c 20 69 6e 74 2c 20 69 *, char*, int, i
74300 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
74310 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
74320 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 53 StrAccumAppend(S
74330 74 72 41 63 63 75 6d 2a 2c 63 6f 6e 73 74 20 63 trAccum*,const c
74340 68 61 72 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 har*,int);.SQLIT
74350 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a E_PRIVATE char *
74360 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 46 sqlite3StrAccumF
74370 69 6e 69 73 68 28 53 74 72 41 63 63 75 6d 2a 29 inish(StrAccum*)
74380 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
74390 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74 72 void sqlite3Str
743a0 41 63 63 75 6d 52 65 73 65 74 28 53 74 72 41 63 AccumReset(StrAc
743b0 63 75 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 cum*);.SQLITE_PR
743c0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
743d0 65 33 53 65 6c 65 63 74 44 65 73 74 49 6e 69 74 e3SelectDestInit
743e0 28 53 65 6c 65 63 74 44 65 73 74 2a 2c 69 6e 74 (SelectDest*,int
743f0 2c 69 6e 74 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 ,int);..SQLITE_P
74400 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
74410 74 65 33 42 61 63 6b 75 70 52 65 73 74 61 72 74 te3BackupRestart
74420 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 (sqlite3_backup
74430 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
74440 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 TE void sqlite3B
74450 61 63 6b 75 70 55 70 64 61 74 65 28 73 71 6c 69 ackupUpdate(sqli
74460 74 65 33 5f 62 61 63 6b 75 70 20 2a 2c 20 50 67 te3_backup *, Pg
74470 6e 6f 2c 20 63 6f 6e 73 74 20 75 38 20 2a 29 3b no, const u8 *);
74480 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 69 6e 74 65 ../*.** The inte
74490 72 66 61 63 65 20 74 6f 20 74 68 65 20 4c 45 4d rface to the LEM
744a0 4f 4e 2d 67 65 6e 65 72 61 74 65 64 20 70 61 72 ON-generated par
744b0 73 65 72 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ser.*/.SQLITE_PR
744c0 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 IVATE void *sqli
744d0 74 65 33 50 61 72 73 65 72 41 6c 6c 6f 63 28 76 te3ParserAlloc(v
744e0 6f 69 64 2a 28 2a 29 28 73 69 7a 65 5f 74 29 29 oid*(*)(size_t))
744f0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
74500 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 72 void sqlite3Par
74510 73 65 72 46 72 65 65 28 76 6f 69 64 2a 2c 20 76 serFree(void*, v
74520 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a oid(*)(void*));.
74530 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
74540 6f 69 64 20 73 71 6c 69 74 65 33 50 61 72 73 65 oid sqlite3Parse
74550 72 28 76 6f 69 64 2a 2c 20 69 6e 74 2c 20 54 6f r(void*, int, To
74560 6b 65 6e 2c 20 50 61 72 73 65 2a 29 3b 0a 23 69 ken, Parse*);.#i
74570 66 64 65 66 20 59 59 54 52 41 43 4b 4d 41 58 53 fdef YYTRACKMAXS
74580 54 41 43 4b 44 45 50 54 48 0a 53 51 4c 49 54 45 TACKDEPTH.SQLITE
74590 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 _PRIVATE int s
745a0 71 6c 69 74 65 33 50 61 72 73 65 72 53 74 61 63 qlite3ParserStac
745b0 6b 50 65 61 6b 28 76 6f 69 64 2a 29 3b 0a 23 65 kPeak(void*);.#e
745c0 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49 ndif..SQLITE_PRI
745d0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
745e0 33 41 75 74 6f 4c 6f 61 64 45 78 74 65 6e 73 69 3AutoLoadExtensi
745f0 6f 6e 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a 23 ons(sqlite3*);.#
74600 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
74610 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f IT_LOAD_EXTENSIO
74620 4e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 N.SQLITE_PRIVATE
74630 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 void sqlite3C
74640 6c 6f 73 65 45 78 74 65 6e 73 69 6f 6e 73 28 73 loseExtensions(s
74650 71 6c 69 74 65 33 2a 29 3b 0a 23 65 6c 73 65 0a qlite3*);.#else.
74660 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 # define sqlite3
74670 43 6c 6f 73 65 45 78 74 65 6e 73 69 6f 6e 73 28 CloseExtensions(
74680 58 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 X).#endif..#ifnd
74690 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 ef SQLITE_OMIT_S
746a0 48 41 52 45 44 5f 43 41 43 48 45 0a 53 51 4c 49 HARED_CACHE.SQLI
746b0 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 TE_PRIVATE voi
746c0 64 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f d sqlite3TableLo
746d0 63 6b 28 50 61 72 73 65 20 2a 2c 20 69 6e 74 2c ck(Parse *, int,
746e0 20 69 6e 74 2c 20 75 38 2c 20 63 6f 6e 73 74 20 int, u8, const
746f0 63 68 61 72 20 2a 29 3b 0a 23 65 6c 73 65 0a 20 char *);.#else.
74700 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 #define sqlite3
74710 54 61 62 6c 65 4c 6f 63 6b 28 76 2c 77 2c 78 2c TableLock(v,w,x,
74720 79 2c 7a 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 y,z).#endif..#if
74730 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a def SQLITE_TEST.
74740 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
74750 20 69 6e 74 20 73 71 6c 69 74 65 33 55 74 66 38 int sqlite3Utf8
74760 54 6f 38 28 75 6e 73 69 67 6e 65 64 20 63 68 61 To8(unsigned cha
74770 72 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 r*);.#endif..#if
74780 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
74790 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 23 20 20 VIRTUALTABLE.#
747a0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56 74 define sqlite3Vt
747b0 61 62 43 6c 65 61 72 28 59 29 0a 23 20 20 64 65 abClear(Y).# de
747c0 66 69 6e 65 20 73 71 6c 69 74 65 33 56 74 61 62 fine sqlite3Vtab
747d0 53 79 6e 63 28 58 2c 59 29 20 53 51 4c 49 54 45 Sync(X,Y) SQLITE
747e0 5f 4f 4b 0a 23 20 20 64 65 66 69 6e 65 20 73 71 _OK.# define sq
747f0 6c 69 74 65 33 56 74 61 62 52 6f 6c 6c 62 61 63 lite3VtabRollbac
74800 6b 28 58 29 0a 23 20 20 64 65 66 69 6e 65 20 73 k(X).# define s
74810 71 6c 69 74 65 33 56 74 61 62 43 6f 6d 6d 69 74 qlite3VtabCommit
74820 28 58 29 0a 23 20 20 64 65 66 69 6e 65 20 73 71 (X).# define sq
74830 6c 69 74 65 33 56 74 61 62 49 6e 53 79 6e 63 28 lite3VtabInSync(
74840 64 62 29 20 30 0a 23 20 20 64 65 66 69 6e 65 20 db) 0.# define
74850 73 71 6c 69 74 65 33 56 74 61 62 4c 6f 63 6b 28 sqlite3VtabLock(
74860 58 29 20 0a 23 20 20 64 65 66 69 6e 65 20 73 71 X) .# define sq
74870 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b 28 lite3VtabUnlock(
74880 58 29 0a 23 20 20 64 65 66 69 6e 65 20 73 71 6c X).# define sql
74890 69 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b 4c 69 ite3VtabUnlockLi
748a0 73 74 28 58 29 0a 23 65 6c 73 65 0a 53 51 4c 49 st(X).#else.SQLI
748b0 54 45 5f 50 52 49 56 41 54 45 20 20 20 20 76 6f TE_PRIVATE vo
748c0 69 64 20 73 71 6c 69 74 65 33 56 74 61 62 43 6c id sqlite3VtabCl
748d0 65 61 72 28 54 61 62 6c 65 2a 29 3b 0a 53 51 4c ear(Table*);.SQL
748e0 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 20 69 ITE_PRIVATE i
748f0 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62 53 79 nt sqlite3VtabSy
74900 6e 63 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 nc(sqlite3 *db,
74910 63 68 61 72 20 2a 2a 29 3b 0a 53 51 4c 49 54 45 char **);.SQLITE
74920 5f 50 52 49 56 41 54 45 20 20 20 20 69 6e 74 20 _PRIVATE int
74930 73 71 6c 69 74 65 33 56 74 61 62 52 6f 6c 6c 62 sqlite3VtabRollb
74940 61 63 6b 28 73 71 6c 69 74 65 33 20 2a 64 62 29 ack(sqlite3 *db)
74950 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
74960 20 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 56 int sqlite3V
74970 74 61 62 43 6f 6d 6d 69 74 28 73 71 6c 69 74 65 tabCommit(sqlite
74980 33 20 2a 64 62 29 3b 0a 53 51 4c 49 54 45 5f 50 3 *db);.SQLITE_P
74990 52 49 56 41 54 45 20 20 20 20 76 6f 69 64 20 73 RIVATE void s
749a0 71 6c 69 74 65 33 56 74 61 62 4c 6f 63 6b 28 56 qlite3VtabLock(V
749b0 54 61 62 6c 65 20 2a 29 3b 0a 53 51 4c 49 54 45 Table *);.SQLITE
749c0 5f 50 52 49 56 41 54 45 20 20 20 20 76 6f 69 64 _PRIVATE void
749d0 20 73 71 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f sqlite3VtabUnlo
749e0 63 6b 28 56 54 61 62 6c 65 20 2a 29 3b 0a 53 51 ck(VTable *);.SQ
749f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 20 LITE_PRIVATE
74a00 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62 void sqlite3Vtab
74a10 55 6e 6c 6f 63 6b 4c 69 73 74 28 73 71 6c 69 74 UnlockList(sqlit
74a20 65 33 2a 29 3b 0a 23 20 20 64 65 66 69 6e 65 20 e3*);.# define
74a30 73 71 6c 69 74 65 33 56 74 61 62 49 6e 53 79 6e sqlite3VtabInSyn
74a40 63 28 64 62 29 20 28 28 64 62 29 2d 3e 6e 56 54 c(db) ((db)->nVT
74a50 72 61 6e 73 3e 30 20 26 26 20 28 64 62 29 2d 3e rans>0 && (db)->
74a60 61 56 54 72 61 6e 73 3d 3d 30 29 0a 23 65 6e 64 aVTrans==0).#end
74a70 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 if.SQLITE_PRIVAT
74a80 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 E void sqlite3Vt
74a90 61 62 4d 61 6b 65 57 72 69 74 61 62 6c 65 28 50 abMakeWritable(P
74aa0 61 72 73 65 2a 2c 54 61 62 6c 65 2a 29 3b 0a 53 arse*,Table*);.S
74ab0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
74ac0 69 64 20 73 71 6c 69 74 65 33 56 74 61 62 42 65 id sqlite3VtabBe
74ad0 67 69 6e 50 61 72 73 65 28 50 61 72 73 65 2a 2c ginParse(Parse*,
74ae0 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 2c Token*, Token*,
74af0 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 Token*);.SQLITE
74b00 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
74b10 6c 69 74 65 33 56 74 61 62 46 69 6e 69 73 68 50 lite3VtabFinishP
74b20 61 72 73 65 28 50 61 72 73 65 2a 2c 20 54 6f 6b arse(Parse*, Tok
74b30 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 en*);.SQLITE_PRI
74b40 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
74b50 33 56 74 61 62 41 72 67 49 6e 69 74 28 50 61 72 3VtabArgInit(Par
74b60 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 se*);.SQLITE_PRI
74b70 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
74b80 33 56 74 61 62 41 72 67 45 78 74 65 6e 64 28 50 3VtabArgExtend(P
74b90 61 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a arse*, Token*);.
74ba0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
74bb0 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62 43 61 nt sqlite3VtabCa
74bc0 6c 6c 43 72 65 61 74 65 28 73 71 6c 69 74 65 33 llCreate(sqlite3
74bd0 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68 *, int, const ch
74be0 61 72 20 2a 2c 20 63 68 61 72 20 2a 2a 29 3b 0a ar *, char **);.
74bf0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
74c00 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62 43 61 nt sqlite3VtabCa
74c10 6c 6c 43 6f 6e 6e 65 63 74 28 50 61 72 73 65 2a llConnect(Parse*
74c20 2c 20 54 61 62 6c 65 2a 29 3b 0a 53 51 4c 49 54 , Table*);.SQLIT
74c30 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
74c40 6c 69 74 65 33 56 74 61 62 43 61 6c 6c 44 65 73 lite3VtabCallDes
74c50 74 72 6f 79 28 73 71 6c 69 74 65 33 2a 2c 20 69 troy(sqlite3*, i
74c60 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a nt, const char *
74c70 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
74c80 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 74 61 E int sqlite3Vta
74c90 62 42 65 67 69 6e 28 73 71 6c 69 74 65 33 20 2a bBegin(sqlite3 *
74ca0 2c 20 56 54 61 62 6c 65 20 2a 29 3b 0a 53 51 4c , VTable *);.SQL
74cb0 49 54 45 5f 50 52 49 56 41 54 45 20 46 75 6e 63 ITE_PRIVATE Func
74cc0 44 65 66 20 2a 73 71 6c 69 74 65 33 56 74 61 62 Def *sqlite3Vtab
74cd0 4f 76 65 72 6c 6f 61 64 46 75 6e 63 74 69 6f 6e OverloadFunction
74ce0 28 73 71 6c 69 74 65 33 20 2a 2c 46 75 6e 63 44 (sqlite3 *,FuncD
74cf0 65 66 2a 2c 20 69 6e 74 20 6e 41 72 67 2c 20 45 ef*, int nArg, E
74d00 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 xpr*);.SQLITE_PR
74d10 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
74d20 65 33 49 6e 76 61 6c 69 64 46 75 6e 63 74 69 6f e3InvalidFunctio
74d30 6e 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 n(sqlite3_contex
74d40 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 t*,int,sqlite3_v
74d50 61 6c 75 65 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f alue**);.SQLITE_
74d60 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
74d70 74 65 33 54 72 61 6e 73 66 65 72 42 69 6e 64 69 te3TransferBindi
74d80 6e 67 73 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 ngs(sqlite3_stmt
74d90 20 2a 2c 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 *, sqlite3_stmt
74da0 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 *);.SQLITE_PRIV
74db0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 ATE int sqlite3R
74dc0 65 70 72 65 70 61 72 65 28 56 64 62 65 2a 29 3b eprepare(Vdbe*);
74dd0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
74de0 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 void sqlite3Expr
74df0 4c 69 73 74 43 68 65 63 6b 4c 65 6e 67 74 68 28 ListCheckLength(
74e00 50 61 72 73 65 2a 2c 20 45 78 70 72 4c 69 73 74 Parse*, ExprList
74e10 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b *, const char*);
74e20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
74e30 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 CollSeq *sqlite3
74e40 42 69 6e 61 72 79 43 6f 6d 70 61 72 65 43 6f 6c BinaryCompareCol
74e50 6c 53 65 71 28 50 61 72 73 65 20 2a 2c 20 45 78 lSeq(Parse *, Ex
74e60 70 72 20 2a 2c 20 45 78 70 72 20 2a 29 3b 0a 53 pr *, Expr *);.S
74e70 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
74e80 74 20 73 71 6c 69 74 65 33 54 65 6d 70 49 6e 4d t sqlite3TempInM
74e90 65 6d 6f 72 79 28 63 6f 6e 73 74 20 73 71 6c 69 emory(const sqli
74ea0 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 te3*);.SQLITE_PR
74eb0 49 56 41 54 45 20 56 54 61 62 6c 65 20 2a 73 71 IVATE VTable *sq
74ec0 6c 69 74 65 33 47 65 74 56 54 61 62 6c 65 28 73 lite3GetVTable(s
74ed0 71 6c 69 74 65 33 2a 2c 20 54 61 62 6c 65 2a 29 qlite3*, Table*)
74ee0 3b 0a 0a 2f 2a 20 44 65 63 6c 61 72 61 74 69 6f ;../* Declaratio
74ef0 6e 73 20 66 6f 72 20 66 75 6e 63 74 69 6f 6e 73 ns for functions
74f00 20 69 6e 20 66 6b 65 79 2e 63 2e 20 41 6c 6c 20 in fkey.c. All
74f10 6f 66 20 74 68 65 73 65 20 61 72 65 20 72 65 70 of these are rep
74f20 6c 61 63 65 64 20 62 79 0a 2a 2a 20 6e 6f 2d 6f laced by.** no-o
74f30 70 20 6d 61 63 72 6f 73 20 69 66 20 4f 4d 49 54 p macros if OMIT
74f40 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 20 69 73 20 _FOREIGN_KEY is
74f50 64 65 66 69 6e 65 64 2e 20 49 6e 20 74 68 69 73 defined. In this
74f60 20 63 61 73 65 20 6e 6f 20 66 6f 72 65 69 67 6e case no foreign
74f70 0a 2a 2a 20 6b 65 79 20 66 75 6e 63 74 69 6f 6e .** key function
74f80 61 6c 69 74 79 20 69 73 20 61 76 61 69 6c 61 62 ality is availab
74f90 6c 65 2e 20 49 66 20 4f 4d 49 54 5f 54 52 49 47 le. If OMIT_TRIG
74fa0 47 45 52 20 69 73 20 64 65 66 69 6e 65 64 20 62 GER is defined b
74fb0 75 74 0a 2a 2a 20 4f 4d 49 54 5f 46 4f 52 45 49 ut.** OMIT_FOREI
74fc0 47 4e 5f 4b 45 59 20 69 73 20 6e 6f 74 2c 20 6f GN_KEY is not, o
74fd0 6e 6c 79 20 73 6f 6d 65 20 6f 66 20 74 68 65 20 nly some of the
74fe0 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 6e 6f functions are no
74ff0 2d 6f 70 65 64 2e 20 49 6e 0a 2a 2a 20 74 68 69 -oped. In.** thi
75000 73 20 63 61 73 65 20 66 6f 72 65 69 67 6e 20 6b s case foreign k
75010 65 79 73 20 61 72 65 20 70 61 72 73 65 64 2c 20 eys are parsed,
75020 62 75 74 20 6e 6f 20 6f 74 68 65 72 20 66 75 6e but no other fun
75030 63 74 69 6f 6e 61 6c 69 74 79 20 69 73 20 0a 2a ctionality is .*
75040 2a 20 70 72 6f 76 69 64 65 64 20 28 65 6e 66 6f * provided (enfo
75050 72 63 65 6d 65 6e 74 20 6f 66 20 46 4b 20 63 6f rcement of FK co
75060 6e 73 74 72 61 69 6e 74 73 20 72 65 71 75 69 72 nstraints requir
75070 65 73 20 74 68 65 20 74 72 69 67 67 65 72 73 20 es the triggers
75080 73 75 62 2d 73 79 73 74 65 6d 29 2e 0a 2a 2f 0a sub-system)..*/.
75090 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c #if !defined(SQL
750a0 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e ITE_OMIT_FOREIGN
750b0 5f 4b 45 59 29 20 26 26 20 21 64 65 66 69 6e 65 _KEY) && !define
750c0 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 d(SQLITE_OMIT_TR
750d0 49 47 47 45 52 29 0a 53 51 4c 49 54 45 5f 50 52 IGGER).SQLITE_PR
750e0 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c IVATE void sql
750f0 69 74 65 33 46 6b 43 68 65 63 6b 28 50 61 72 73 ite3FkCheck(Pars
75100 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 2c e*, Table*, int,
75110 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
75120 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c IVATE void sql
75130 69 74 65 33 46 6b 44 72 6f 70 54 61 62 6c 65 28 ite3FkDropTable(
75140 50 61 72 73 65 2a 2c 20 53 72 63 4c 69 73 74 20 Parse*, SrcList
75150 2a 2c 20 54 61 62 6c 65 2a 29 3b 0a 53 51 4c 49 *, Table*);.SQLI
75160 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 TE_PRIVATE voi
75170 64 20 73 71 6c 69 74 65 33 46 6b 41 63 74 69 6f d sqlite3FkActio
75180 6e 73 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 ns(Parse*, Table
75190 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 69 6e *, ExprList*, in
751a0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
751b0 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 TE int sqlite3
751c0 46 6b 52 65 71 75 69 72 65 64 28 50 61 72 73 65 FkRequired(Parse
751d0 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 2a 2c *, Table*, int*,
751e0 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
751f0 49 56 41 54 45 20 20 20 75 33 32 20 73 71 6c 69 IVATE u32 sqli
75200 74 65 33 46 6b 4f 6c 64 6d 61 73 6b 28 50 61 72 te3FkOldmask(Par
75210 73 65 2a 2c 20 54 61 62 6c 65 2a 29 3b 0a 53 51 se*, Table*);.SQ
75220 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 46 LITE_PRIVATE F
75230 4b 65 79 20 2a 73 71 6c 69 74 65 33 46 6b 52 65 Key *sqlite3FkRe
75240 66 65 72 65 6e 63 65 73 28 54 61 62 6c 65 20 2a ferences(Table *
75250 29 3b 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 );.#else. #defi
75260 6e 65 20 73 71 6c 69 74 65 33 46 6b 41 63 74 69 ne sqlite3FkActi
75270 6f 6e 73 28 61 2c 62 2c 63 2c 64 29 0a 20 20 23 ons(a,b,c,d). #
75280 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 46 6b define sqlite3Fk
75290 43 68 65 63 6b 28 61 2c 62 2c 63 2c 64 29 0a 20 Check(a,b,c,d).
752a0 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 #define sqlite3
752b0 46 6b 44 72 6f 70 54 61 62 6c 65 28 61 2c 62 2c FkDropTable(a,b,
752c0 63 29 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c c). #define sql
752d0 69 74 65 33 46 6b 4f 6c 64 6d 61 73 6b 28 61 2c ite3FkOldmask(a,
752e0 62 29 20 20 20 20 20 20 30 0a 20 20 23 64 65 66 b) 0. #def
752f0 69 6e 65 20 73 71 6c 69 74 65 33 46 6b 52 65 71 ine sqlite3FkReq
75300 75 69 72 65 64 28 61 2c 62 2c 63 2c 64 29 20 30 uired(a,b,c,d) 0
75310 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 .#endif.#ifndef
75320 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 SQLITE_OMIT_FORE
75330 49 47 4e 5f 4b 45 59 0a 53 51 4c 49 54 45 5f 50 IGN_KEY.SQLITE_P
75340 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 RIVATE void sq
75350 6c 69 74 65 33 46 6b 44 65 6c 65 74 65 28 54 61 lite3FkDelete(Ta
75360 62 6c 65 2a 29 3b 0a 23 65 6c 73 65 0a 20 20 23 ble*);.#else. #
75370 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 46 6b define sqlite3Fk
75380 44 65 6c 65 74 65 28 61 29 0a 23 65 6e 64 69 66 Delete(a).#endif
75390 0a 0a 0a 2f 2a 0a 2a 2a 20 41 76 61 69 6c 61 62 .../*.** Availab
753a0 6c 65 20 66 61 75 6c 74 20 69 6e 6a 65 63 74 6f le fault injecto
753b0 72 73 2e 20 20 53 68 6f 75 6c 64 20 62 65 20 6e rs. Should be n
753c0 75 6d 62 65 72 65 64 20 62 65 67 69 6e 6e 69 6e umbered beginnin
753d0 67 20 77 69 74 68 20 30 2e 0a 2a 2f 0a 23 64 65 g with 0..*/.#de
753e0 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 41 55 4c fine SQLITE_FAUL
753f0 54 49 4e 4a 45 43 54 4f 52 5f 4d 41 4c 4c 4f 43 TINJECTOR_MALLOC
75400 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 53 0.#define S
75410 51 4c 49 54 45 5f 46 41 55 4c 54 49 4e 4a 45 43 QLITE_FAULTINJEC
75420 54 4f 52 5f 43 4f 55 4e 54 20 20 20 20 20 20 31 TOR_COUNT 1
75430 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 69 6e 74 65 ../*.** The inte
75440 72 66 61 63 65 20 74 6f 20 74 68 65 20 63 6f 64 rface to the cod
75450 65 20 69 6e 20 66 61 75 6c 74 2e 63 20 75 73 65 e in fault.c use
75460 64 20 66 6f 72 20 69 64 65 6e 74 69 66 79 69 6e d for identifyin
75470 67 20 22 62 65 6e 69 67 6e 22 0a 2a 2a 20 6d 61 g "benign".** ma
75480 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73 2e 20 54 lloc failures. T
75490 68 69 73 20 69 73 20 6f 6e 6c 79 20 70 72 65 73 his is only pres
754a0 65 6e 74 20 69 66 20 53 51 4c 49 54 45 5f 4f 4d ent if SQLITE_OM
754b0 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 0a IT_BUILTIN_TEST.
754c0 2a 2a 20 69 73 20 6e 6f 74 20 64 65 66 69 6e 65 ** is not define
754d0 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 d..*/.#ifndef SQ
754e0 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 LITE_OMIT_BUILTI
754f0 4e 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 52 N_TEST.SQLITE_PR
75500 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c IVATE void sql
75510 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d ite3BeginBenignM
75520 61 6c 6c 6f 63 28 76 6f 69 64 29 3b 0a 53 51 4c alloc(void);.SQL
75530 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f ITE_PRIVATE vo
75540 69 64 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e id sqlite3EndBen
75550 69 67 6e 4d 61 6c 6c 6f 63 28 76 6f 69 64 29 3b ignMalloc(void);
75560 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 .#else. #define
75570 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e sqlite3BeginBen
75580 69 67 6e 4d 61 6c 6c 6f 63 28 29 0a 20 20 23 64 ignMalloc(). #d
75590 65 66 69 6e 65 20 73 71 6c 69 74 65 33 45 6e 64 efine sqlite3End
755a0 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 0a 23 BenignMalloc().#
755b0 65 6e 64 69 66 0a 0a 23 64 65 66 69 6e 65 20 49 endif..#define I
755c0 4e 5f 49 4e 44 45 58 5f 52 4f 57 49 44 20 20 20 N_INDEX_ROWID
755d0 20 20 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e 1.#defin
755e0 65 20 49 4e 5f 49 4e 44 45 58 5f 45 50 48 20 20 e IN_INDEX_EPH
755f0 20 20 20 20 20 20 20 20 20 20 20 32 0a 23 64 65 2.#de
75600 66 69 6e 65 20 49 4e 5f 49 4e 44 45 58 5f 49 4e fine IN_INDEX_IN
75610 44 45 58 20 20 20 20 20 20 20 20 20 20 20 33 0a DEX 3.
75620 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
75630 6e 74 20 73 71 6c 69 74 65 33 46 69 6e 64 49 6e nt sqlite3FindIn
75640 49 6e 64 65 78 28 50 61 72 73 65 20 2a 2c 20 45 Index(Parse *, E
75650 78 70 72 20 2a 2c 20 69 6e 74 2a 29 3b 0a 0a 23 xpr *, int*);..#
75660 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 ifdef SQLITE_ENA
75670 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45 BLE_ATOMIC_WRITE
75680 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
75690 20 20 69 6e 74 20 73 71 6c 69 74 65 33 4a 6f 75 int sqlite3Jou
756a0 72 6e 61 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 rnalOpen(sqlite3
756b0 5f 76 66 73 20 2a 2c 20 63 6f 6e 73 74 20 63 68 _vfs *, const ch
756c0 61 72 20 2a 2c 20 73 71 6c 69 74 65 33 5f 66 69 ar *, sqlite3_fi
756d0 6c 65 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b le *, int, int);
756e0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
756f0 20 20 69 6e 74 20 73 71 6c 69 74 65 33 4a 6f 75 int sqlite3Jou
75700 72 6e 61 6c 53 69 7a 65 28 73 71 6c 69 74 65 33 rnalSize(sqlite3
75710 5f 76 66 73 20 2a 29 3b 0a 53 51 4c 49 54 45 5f _vfs *);.SQLITE_
75720 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 PRIVATE int sq
75730 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 43 72 65 61 lite3JournalCrea
75740 74 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 te(sqlite3_file
75750 2a 29 3b 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 *);.#else. #def
75760 69 6e 65 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e ine sqlite3Journ
75770 61 6c 53 69 7a 65 28 70 56 66 73 29 20 28 28 70 alSize(pVfs) ((p
75780 56 66 73 29 2d 3e 73 7a 4f 73 46 69 6c 65 29 0a Vfs)->szOsFile).
75790 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 #endif..SQLITE_P
757a0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
757b0 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 4f 70 65 te3MemJournalOpe
757c0 6e 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a n(sqlite3_file *
757d0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
757e0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d E int sqlite3Mem
757f0 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 76 6f 69 64 JournalSize(void
75800 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
75810 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73 4d E int sqlite3IsM
75820 65 6d 4a 6f 75 72 6e 61 6c 28 73 71 6c 69 74 65 emJournal(sqlite
75830 33 5f 66 69 6c 65 20 2a 29 3b 0a 0a 23 69 66 20 3_file *);..#if
75840 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f SQLITE_MAX_EXPR_
75850 44 45 50 54 48 3e 30 0a 53 51 4c 49 54 45 5f 50 DEPTH>0.SQLITE_P
75860 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 RIVATE void sq
75870 6c 69 74 65 33 45 78 70 72 53 65 74 48 65 69 67 lite3ExprSetHeig
75880 68 74 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 ht(Parse *pParse
75890 2c 20 45 78 70 72 20 2a 70 29 3b 0a 53 51 4c 49 , Expr *p);.SQLI
758a0 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 TE_PRIVATE int
758b0 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 45 78 sqlite3SelectEx
758c0 70 72 48 65 69 67 68 74 28 53 65 6c 65 63 74 20 prHeight(Select
758d0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
758e0 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 TE int sqlite3
758f0 45 78 70 72 43 68 65 63 6b 48 65 69 67 68 74 28 ExprCheckHeight(
75900 50 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 23 65 Parse*, int);.#e
75910 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 73 71 lse. #define sq
75920 6c 69 74 65 33 45 78 70 72 53 65 74 48 65 69 67 lite3ExprSetHeig
75930 68 74 28 78 2c 79 29 0a 20 20 23 64 65 66 69 6e ht(x,y). #defin
75940 65 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 45 e sqlite3SelectE
75950 78 70 72 48 65 69 67 68 74 28 78 29 20 30 0a 20 xprHeight(x) 0.
75960 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 #define sqlite3
75970 45 78 70 72 43 68 65 63 6b 48 65 69 67 68 74 28 ExprCheckHeight(
75980 78 2c 79 29 0a 23 65 6e 64 69 66 0a 0a 53 51 4c x,y).#endif..SQL
75990 49 54 45 5f 50 52 49 56 41 54 45 20 75 33 32 20 ITE_PRIVATE u32
759a0 73 71 6c 69 74 65 33 47 65 74 34 62 79 74 65 28 sqlite3Get4byte(
759b0 63 6f 6e 73 74 20 75 38 2a 29 3b 0a 53 51 4c 49 const u8*);.SQLI
759c0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
759d0 73 71 6c 69 74 65 33 50 75 74 34 62 79 74 65 28 sqlite3Put4byte(
759e0 75 38 2a 2c 20 75 33 32 29 3b 0a 0a 23 69 66 64 u8*, u32);..#ifd
759f0 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 ef SQLITE_ENABLE
75a00 5f 55 4e 4c 4f 43 4b 5f 4e 4f 54 49 46 59 0a 53 _UNLOCK_NOTIFY.S
75a10 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
75a20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6e 6e void sqlite3Conn
75a30 65 63 74 69 6f 6e 42 6c 6f 63 6b 65 64 28 73 71 ectionBlocked(sq
75a40 6c 69 74 65 33 20 2a 2c 20 73 71 6c 69 74 65 33 lite3 *, sqlite3
75a50 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 *);.SQLITE_PRIV
75a60 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 ATE void sqlit
75a70 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 55 6e 6c 6f e3ConnectionUnlo
75a80 63 6b 65 64 28 73 71 6c 69 74 65 33 20 2a 64 62 cked(sqlite3 *db
75a90 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
75aa0 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 E void sqlite3
75ab0 43 6f 6e 6e 65 63 74 69 6f 6e 43 6c 6f 73 65 64 ConnectionClosed
75ac0 28 73 71 6c 69 74 65 33 20 2a 64 62 29 3b 0a 23 (sqlite3 *db);.#
75ad0 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 73 else. #define s
75ae0 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e qlite3Connection
75af0 42 6c 6f 63 6b 65 64 28 78 2c 79 29 0a 20 20 23 Blocked(x,y). #
75b00 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 43 6f define sqlite3Co
75b10 6e 6e 65 63 74 69 6f 6e 55 6e 6c 6f 63 6b 65 64 nnectionUnlocked
75b20 28 78 29 0a 20 20 23 64 65 66 69 6e 65 20 73 71 (x). #define sq
75b30 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 43 lite3ConnectionC
75b40 6c 6f 73 65 64 28 78 29 0a 23 65 6e 64 69 66 0a losed(x).#endif.
75b50 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 .#ifdef SQLITE_D
75b60 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 EBUG.SQLITE_PRIV
75b70 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 ATE void sqlit
75b80 65 33 50 61 72 73 65 72 54 72 61 63 65 28 46 49 e3ParserTrace(FI
75b90 4c 45 2a 2c 20 63 68 61 72 20 2a 29 3b 0a 23 65 LE*, char *);.#e
75ba0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 ndif../*.** If t
75bb0 68 65 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 he SQLITE_ENABLE
75bc0 20 49 4f 54 52 41 43 45 20 65 78 69 73 74 73 20 IOTRACE exists
75bd0 74 68 65 6e 20 74 68 65 20 67 6c 6f 62 61 6c 20 then the global
75be0 76 61 72 69 61 62 6c 65 0a 2a 2a 20 73 71 6c 69 variable.** sqli
75bf0 74 65 33 49 6f 54 72 61 63 65 20 69 73 20 61 20 te3IoTrace is a
75c00 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 70 72 69 pointer to a pri
75c10 6e 74 66 2d 6c 69 6b 65 20 72 6f 75 74 69 6e 65 ntf-like routine
75c20 20 75 73 65 64 20 74 6f 0a 2a 2a 20 70 72 69 6e used to.** prin
75c30 74 20 49 2f 4f 20 74 72 61 63 69 6e 67 20 6d 65 t I/O tracing me
75c40 73 73 61 67 65 73 2e 20 0a 2a 2f 0a 23 69 66 64 ssages. .*/.#ifd
75c50 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 ef SQLITE_ENABLE
75c60 5f 49 4f 54 52 41 43 45 0a 23 20 64 65 66 69 6e _IOTRACE.# defin
75c70 65 20 49 4f 54 52 41 43 45 28 41 29 20 20 69 66 e IOTRACE(A) if
75c80 28 20 73 71 6c 69 74 65 33 49 6f 54 72 61 63 65 ( sqlite3IoTrace
75c90 20 29 7b 20 73 71 6c 69 74 65 33 49 6f 54 72 61 ){ sqlite3IoTra
75ca0 63 65 20 41 3b 20 7d 0a 53 51 4c 49 54 45 5f 50 ce A; }.SQLITE_P
75cb0 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 RIVATE void sq
75cc0 6c 69 74 65 33 56 64 62 65 49 4f 54 72 61 63 65 lite3VdbeIOTrace
75cd0 53 71 6c 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 Sql(Vdbe*);.SQLI
75ce0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
75cf0 28 2a 73 71 6c 69 74 65 33 49 6f 54 72 61 63 65 (*sqlite3IoTrace
75d00 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 2e 2e )(const char*,..
75d10 2e 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 .);.#else.# defi
75d20 6e 65 20 49 4f 54 52 41 43 45 28 41 29 0a 23 20 ne IOTRACE(A).#
75d30 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56 64 define sqlite3Vd
75d40 62 65 49 4f 54 72 61 63 65 53 71 6c 28 58 29 0a beIOTraceSql(X).
75d50 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 0a 0a #endif..#endif..
75d60 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
75d70 45 6e 64 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 End of sqliteInt
75d80 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .h *************
75d90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
75da0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
75db0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
75dc0 42 65 67 69 6e 20 66 69 6c 65 20 67 6c 6f 62 61 Begin file globa
75dd0 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a l.c ************
75de0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
75df0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
75e00 2f 2a 0a 2a 2a 20 32 30 30 38 20 4a 75 6e 65 20 /*.** 2008 June
75e10 31 33 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 13.**.** The aut
75e20 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f hor disclaims co
75e30 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 pyright to this
75e40 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e source code. In
75e50 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c place of.** a l
75e60 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 egal notice, her
75e70 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a e is a blessing:
75e80 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f .**.** May yo
75e90 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f u do good and no
75ea0 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 t evil..** Ma
75eb0 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 y you find forgi
75ec0 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 veness for yours
75ed0 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 elf and forgive
75ee0 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 others..** Ma
75ef0 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 y you share free
75f00 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 ly, never taking
75f10 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 more than you g
75f20 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a ive..**.********
75f30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
75f40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
75f50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
75f60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
75f70 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c *.**.** This fil
75f80 65 20 63 6f 6e 74 61 69 6e 73 20 64 65 66 69 6e e contains defin
75f90 69 74 69 6f 6e 73 20 6f 66 20 67 6c 6f 62 61 6c itions of global
75fa0 20 76 61 72 69 61 62 6c 65 73 20 61 6e 64 20 63 variables and c
75fb0 6f 6e 74 61 6e 74 73 2e 0a 2a 2f 0a 0a 0a 2f 2a ontants..*/.../*
75fc0 20 41 6e 20 61 72 72 61 79 20 74 6f 20 6d 61 70 An array to map
75fd0 20 61 6c 6c 20 75 70 70 65 72 2d 63 61 73 65 20 all upper-case
75fe0 63 68 61 72 61 63 74 65 72 73 20 69 6e 74 6f 20 characters into
75ff0 74 68 65 69 72 20 63 6f 72 72 65 73 70 6f 6e 64 their correspond
76000 69 6e 67 0a 2a 2a 20 6c 6f 77 65 72 2d 63 61 73 ing.** lower-cas
76010 65 20 63 68 61 72 61 63 74 65 72 2e 20 0a 2a 2a e character. .**
76020 0a 2a 2a 20 53 51 4c 69 74 65 20 6f 6e 6c 79 20 .** SQLite only
76030 63 6f 6e 73 69 64 65 72 73 20 55 53 2d 41 53 43 considers US-ASC
76040 49 49 20 28 6f 72 20 45 42 43 44 49 43 29 20 63 II (or EBCDIC) c
76050 68 61 72 61 63 74 65 72 73 2e 20 20 57 65 20 64 haracters. We d
76060 6f 20 6e 6f 74 0a 2a 2a 20 68 61 6e 64 6c 65 20 o not.** handle
76070 63 61 73 65 20 63 6f 6e 76 65 72 73 69 6f 6e 73 case conversions
76080 20 66 6f 72 20 74 68 65 20 55 54 46 20 63 68 61 for the UTF cha
76090 72 61 63 74 65 72 20 73 65 74 20 73 69 6e 63 65 racter set since
760a0 20 74 68 65 20 74 61 62 6c 65 73 0a 2a 2a 20 69 the tables.** i
760b0 6e 76 6f 6c 76 65 64 20 61 72 65 20 6e 65 61 72 nvolved are near
760c0 6c 79 20 61 73 20 62 69 67 20 6f 72 20 62 69 67 ly as big or big
760d0 67 65 72 20 74 68 61 6e 20 53 51 4c 69 74 65 20 ger than SQLite
760e0 69 74 73 65 6c 66 2e 0a 2a 2f 0a 53 51 4c 49 54 itself..*/.SQLIT
760f0 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 E_PRIVATE const
76100 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 73 71 unsigned char sq
76110 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 lite3UpperToLowe
76120 72 5b 5d 20 3d 20 7b 0a 23 69 66 64 65 66 20 53 r[] = {.#ifdef S
76130 51 4c 49 54 45 5f 41 53 43 49 49 0a 20 20 20 20 QLITE_ASCII.
76140 20 20 30 2c 20 20 31 2c 20 20 32 2c 20 20 33 2c 0, 1, 2, 3,
76150 20 20 34 2c 20 20 35 2c 20 20 36 2c 20 20 37 2c 4, 5, 6, 7,
76160 20 20 38 2c 20 20 39 2c 20 31 30 2c 20 31 31 2c 8, 9, 10, 11,
76170 20 31 32 2c 20 31 33 2c 20 31 34 2c 20 31 35 2c 12, 13, 14, 15,
76180 20 31 36 2c 20 31 37 2c 0a 20 20 20 20 20 31 38 16, 17,. 18
76190 2c 20 31 39 2c 20 32 30 2c 20 32 31 2c 20 32 32 , 19, 20, 21, 22
761a0 2c 20 32 33 2c 20 32 34 2c 20 32 35 2c 20 32 36 , 23, 24, 25, 26
761b0 2c 20 32 37 2c 20 32 38 2c 20 32 39 2c 20 33 30 , 27, 28, 29, 30
761c0 2c 20 33 31 2c 20 33 32 2c 20 33 33 2c 20 33 34 , 31, 32, 33, 34
761d0 2c 20 33 35 2c 0a 20 20 20 20 20 33 36 2c 20 33 , 35,. 36, 3
761e0 37 2c 20 33 38 2c 20 33 39 2c 20 34 30 2c 20 34 7, 38, 39, 40, 4
761f0 31 2c 20 34 32 2c 20 34 33 2c 20 34 34 2c 20 34 1, 42, 43, 44, 4
76200 35 2c 20 34 36 2c 20 34 37 2c 20 34 38 2c 20 34 5, 46, 47, 48, 4
76210 39 2c 20 35 30 2c 20 35 31 2c 20 35 32 2c 20 35 9, 50, 51, 52, 5
76220 33 2c 0a 20 20 20 20 20 35 34 2c 20 35 35 2c 20 3,. 54, 55,
76230 35 36 2c 20 35 37 2c 20 35 38 2c 20 35 39 2c 20 56, 57, 58, 59,
76240 36 30 2c 20 36 31 2c 20 36 32 2c 20 36 33 2c 20 60, 61, 62, 63,
76250 36 34 2c 20 39 37 2c 20 39 38 2c 20 39 39 2c 31 64, 97, 98, 99,1
76260 30 30 2c 31 30 31 2c 31 30 32 2c 31 30 33 2c 0a 00,101,102,103,.
76270 20 20 20 20 31 30 34 2c 31 30 35 2c 31 30 36 2c 104,105,106,
76280 31 30 37 2c 31 30 38 2c 31 30 39 2c 31 31 30 2c 107,108,109,110,
76290 31 31 31 2c 31 31 32 2c 31 31 33 2c 31 31 34 2c 111,112,113,114,
762a0 31 31 35 2c 31 31 36 2c 31 31 37 2c 31 31 38 2c 115,116,117,118,
762b0 31 31 39 2c 31 32 30 2c 31 32 31 2c 0a 20 20 20 119,120,121,.
762c0 20 31 32 32 2c 20 39 31 2c 20 39 32 2c 20 39 33 122, 91, 92, 93
762d0 2c 20 39 34 2c 20 39 35 2c 20 39 36 2c 20 39 37 , 94, 95, 96, 97
762e0 2c 20 39 38 2c 20 39 39 2c 31 30 30 2c 31 30 31 , 98, 99,100,101
762f0 2c 31 30 32 2c 31 30 33 2c 31 30 34 2c 31 30 35 ,102,103,104,105
76300 2c 31 30 36 2c 31 30 37 2c 0a 20 20 20 20 31 30 ,106,107,. 10
76310 38 2c 31 30 39 2c 31 31 30 2c 31 31 31 2c 31 31 8,109,110,111,11
76320 32 2c 31 31 33 2c 31 31 34 2c 31 31 35 2c 31 31 2,113,114,115,11
76330 36 2c 31 31 37 2c 31 31 38 2c 31 31 39 2c 31 32 6,117,118,119,12
76340 30 2c 31 32 31 2c 31 32 32 2c 31 32 33 2c 31 32 0,121,122,123,12
76350 34 2c 31 32 35 2c 0a 20 20 20 20 31 32 36 2c 31 4,125,. 126,1
76360 32 37 2c 31 32 38 2c 31 32 39 2c 31 33 30 2c 31 27,128,129,130,1
76370 33 31 2c 31 33 32 2c 31 33 33 2c 31 33 34 2c 31 31,132,133,134,1
76380 33 35 2c 31 33 36 2c 31 33 37 2c 31 33 38 2c 31 35,136,137,138,1
76390 33 39 2c 31 34 30 2c 31 34 31 2c 31 34 32 2c 31 39,140,141,142,1
763a0 34 33 2c 0a 20 20 20 20 31 34 34 2c 31 34 35 2c 43,. 144,145,
763b0 31 34 36 2c 31 34 37 2c 31 34 38 2c 31 34 39 2c 146,147,148,149,
763c0 31 35 30 2c 31 35 31 2c 31 35 32 2c 31 35 33 2c 150,151,152,153,
763d0 31 35 34 2c 31 35 35 2c 31 35 36 2c 31 35 37 2c 154,155,156,157,
763e0 31 35 38 2c 31 35 39 2c 31 36 30 2c 31 36 31 2c 158,159,160,161,
763f0 0a 20 20 20 20 31 36 32 2c 31 36 33 2c 31 36 34 . 162,163,164
76400 2c 31 36 35 2c 31 36 36 2c 31 36 37 2c 31 36 38 ,165,166,167,168
76410 2c 31 36 39 2c 31 37 30 2c 31 37 31 2c 31 37 32 ,169,170,171,172
76420 2c 31 37 33 2c 31 37 34 2c 31 37 35 2c 31 37 36 ,173,174,175,176
76430 2c 31 37 37 2c 31 37 38 2c 31 37 39 2c 0a 20 20 ,177,178,179,.
76440 20 20 31 38 30 2c 31 38 31 2c 31 38 32 2c 31 38 180,181,182,18
76450 33 2c 31 38 34 2c 31 38 35 2c 31 38 36 2c 31 38 3,184,185,186,18
76460 37 2c 31 38 38 2c 31 38 39 2c 31 39 30 2c 31 39 7,188,189,190,19
76470 31 2c 31 39 32 2c 31 39 33 2c 31 39 34 2c 31 39 1,192,193,194,19
76480 35 2c 31 39 36 2c 31 39 37 2c 0a 20 20 20 20 31 5,196,197,. 1
76490 39 38 2c 31 39 39 2c 32 30 30 2c 32 30 31 2c 32 98,199,200,201,2
764a0 30 32 2c 32 30 33 2c 32 30 34 2c 32 30 35 2c 32 02,203,204,205,2
764b0 30 36 2c 32 30 37 2c 32 30 38 2c 32 30 39 2c 32 06,207,208,209,2
764c0 31 30 2c 32 31 31 2c 32 31 32 2c 32 31 33 2c 32 10,211,212,213,2
764d0 31 34 2c 32 31 35 2c 0a 20 20 20 20 32 31 36 2c 14,215,. 216,
764e0 32 31 37 2c 32 31 38 2c 32 31 39 2c 32 32 30 2c 217,218,219,220,
764f0 32 32 31 2c 32 32 32 2c 32 32 33 2c 32 32 34 2c 221,222,223,224,
76500 32 32 35 2c 32 32 36 2c 32 32 37 2c 32 32 38 2c 225,226,227,228,
76510 32 32 39 2c 32 33 30 2c 32 33 31 2c 32 33 32 2c 229,230,231,232,
76520 32 33 33 2c 0a 20 20 20 20 32 33 34 2c 32 33 35 233,. 234,235
76530 2c 32 33 36 2c 32 33 37 2c 32 33 38 2c 32 33 39 ,236,237,238,239
76540 2c 32 34 30 2c 32 34 31 2c 32 34 32 2c 32 34 33 ,240,241,242,243
76550 2c 32 34 34 2c 32 34 35 2c 32 34 36 2c 32 34 37 ,244,245,246,247
76560 2c 32 34 38 2c 32 34 39 2c 32 35 30 2c 32 35 31 ,248,249,250,251
76570 2c 0a 20 20 20 20 32 35 32 2c 32 35 33 2c 32 35 ,. 252,253,25
76580 34 2c 32 35 35 0a 23 65 6e 64 69 66 0a 23 69 66 4,255.#endif.#if
76590 64 65 66 20 53 51 4c 49 54 45 5f 45 42 43 44 49 def SQLITE_EBCDI
765a0 43 0a 20 20 20 20 20 20 30 2c 20 20 31 2c 20 20 C. 0, 1,
765b0 32 2c 20 20 33 2c 20 20 34 2c 20 20 35 2c 20 20 2, 3, 4, 5,
765c0 36 2c 20 20 37 2c 20 20 38 2c 20 20 39 2c 20 31 6, 7, 8, 9, 1
765d0 30 2c 20 31 31 2c 20 31 32 2c 20 31 33 2c 20 31 0, 11, 12, 13, 1
765e0 34 2c 20 31 35 2c 20 2f 2a 20 30 78 20 2a 2f 0a 4, 15, /* 0x */.
765f0 20 20 20 20 20 31 36 2c 20 31 37 2c 20 31 38 2c 16, 17, 18,
76600 20 31 39 2c 20 32 30 2c 20 32 31 2c 20 32 32 2c 19, 20, 21, 22,
76610 20 32 33 2c 20 32 34 2c 20 32 35 2c 20 32 36 2c 23, 24, 25, 26,
76620 20 32 37 2c 20 32 38 2c 20 32 39 2c 20 33 30 2c 27, 28, 29, 30,
76630 20 33 31 2c 20 2f 2a 20 31 78 20 2a 2f 0a 20 20 31, /* 1x */.
76640 20 20 20 33 32 2c 20 33 33 2c 20 33 34 2c 20 33 32, 33, 34, 3
76650 35 2c 20 33 36 2c 20 33 37 2c 20 33 38 2c 20 33 5, 36, 37, 38, 3
76660 39 2c 20 34 30 2c 20 34 31 2c 20 34 32 2c 20 34 9, 40, 41, 42, 4
76670 33 2c 20 34 34 2c 20 34 35 2c 20 34 36 2c 20 34 3, 44, 45, 46, 4
76680 37 2c 20 2f 2a 20 32 78 20 2a 2f 0a 20 20 20 20 7, /* 2x */.
76690 20 34 38 2c 20 34 39 2c 20 35 30 2c 20 35 31 2c 48, 49, 50, 51,
766a0 20 35 32 2c 20 35 33 2c 20 35 34 2c 20 35 35 2c 52, 53, 54, 55,
766b0 20 35 36 2c 20 35 37 2c 20 35 38 2c 20 35 39 2c 56, 57, 58, 59,
766c0 20 36 30 2c 20 36 31 2c 20 36 32 2c 20 36 33 2c 60, 61, 62, 63,
766d0 20 2f 2a 20 33 78 20 2a 2f 0a 20 20 20 20 20 36 /* 3x */. 6
766e0 34 2c 20 36 35 2c 20 36 36 2c 20 36 37 2c 20 36 4, 65, 66, 67, 6
766f0 38 2c 20 36 39 2c 20 37 30 2c 20 37 31 2c 20 37 8, 69, 70, 71, 7
76700 32 2c 20 37 33 2c 20 37 34 2c 20 37 35 2c 20 37 2, 73, 74, 75, 7
76710 36 2c 20 37 37 2c 20 37 38 2c 20 37 39 2c 20 2f 6, 77, 78, 79, /
76720 2a 20 34 78 20 2a 2f 0a 20 20 20 20 20 38 30 2c * 4x */. 80,
76730 20 38 31 2c 20 38 32 2c 20 38 33 2c 20 38 34 2c 81, 82, 83, 84,
76740 20 38 35 2c 20 38 36 2c 20 38 37 2c 20 38 38 2c 85, 86, 87, 88,
76750 20 38 39 2c 20 39 30 2c 20 39 31 2c 20 39 32 2c 89, 90, 91, 92,
76760 20 39 33 2c 20 39 34 2c 20 39 35 2c 20 2f 2a 20 93, 94, 95, /*
76770 35 78 20 2a 2f 0a 20 20 20 20 20 39 36 2c 20 39 5x */. 96, 9
76780 37 2c 20 36 36 2c 20 36 37 2c 20 36 38 2c 20 36 7, 66, 67, 68, 6
76790 39 2c 20 37 30 2c 20 37 31 2c 20 37 32 2c 20 37 9, 70, 71, 72, 7
767a0 33 2c 31 30 36 2c 31 30 37 2c 31 30 38 2c 31 30 3,106,107,108,10
767b0 39 2c 31 31 30 2c 31 31 31 2c 20 2f 2a 20 36 78 9,110,111, /* 6x
767c0 20 2a 2f 0a 20 20 20 20 31 31 32 2c 20 38 31 2c */. 112, 81,
767d0 20 38 32 2c 20 38 33 2c 20 38 34 2c 20 38 35 2c 82, 83, 84, 85,
767e0 20 38 36 2c 20 38 37 2c 20 38 38 2c 20 38 39 2c 86, 87, 88, 89,
767f0 31 32 32 2c 31 32 33 2c 31 32 34 2c 31 32 35 2c 122,123,124,125,
76800 31 32 36 2c 31 32 37 2c 20 2f 2a 20 37 78 20 2a 126,127, /* 7x *
76810 2f 0a 20 20 20 20 31 32 38 2c 31 32 39 2c 31 33 /. 128,129,13
76820 30 2c 31 33 31 2c 31 33 32 2c 31 33 33 2c 31 33 0,131,132,133,13
76830 34 2c 31 33 35 2c 31 33 36 2c 31 33 37 2c 31 33 4,135,136,137,13
76840 38 2c 31 33 39 2c 31 34 30 2c 31 34 31 2c 31 34 8,139,140,141,14
76850 32 2c 31 34 33 2c 20 2f 2a 20 38 78 20 2a 2f 0a 2,143, /* 8x */.
76860 20 20 20 20 31 34 34 2c 31 34 35 2c 31 34 36 2c 144,145,146,
76870 31 34 37 2c 31 34 38 2c 31 34 39 2c 31 35 30 2c 147,148,149,150,
76880 31 35 31 2c 31 35 32 2c 31 35 33 2c 31 35 34 2c 151,152,153,154,
76890 31 35 35 2c 31 35 36 2c 31 35 37 2c 31 35 36 2c 155,156,157,156,
768a0 31 35 39 2c 20 2f 2a 20 39 78 20 2a 2f 0a 20 20 159, /* 9x */.
768b0 20 20 31 36 30 2c 31 36 31 2c 31 36 32 2c 31 36 160,161,162,16
768c0 33 2c 31 36 34 2c 31 36 35 2c 31 36 36 2c 31 36 3,164,165,166,16
768d0 37 2c 31 36 38 2c 31 36 39 2c 31 37 30 2c 31 37 7,168,169,170,17
768e0 31 2c 31 34 30 2c 31 34 31 2c 31 34 32 2c 31 37 1,140,141,142,17
768f0 35 2c 20 2f 2a 20 41 78 20 2a 2f 0a 20 20 20 20 5, /* Ax */.
76900 31 37 36 2c 31 37 37 2c 31 37 38 2c 31 37 39 2c 176,177,178,179,
76910 31 38 30 2c 31 38 31 2c 31 38 32 2c 31 38 33 2c 180,181,182,183,
76920 31 38 34 2c 31 38 35 2c 31 38 36 2c 31 38 37 2c 184,185,186,187,
76930 31 38 38 2c 31 38 39 2c 31 39 30 2c 31 39 31 2c 188,189,190,191,
76940 20 2f 2a 20 42 78 20 2a 2f 0a 20 20 20 20 31 39 /* Bx */. 19
76950 32 2c 31 32 39 2c 31 33 30 2c 31 33 31 2c 31 33 2,129,130,131,13
76960 32 2c 31 33 33 2c 31 33 34 2c 31 33 35 2c 31 33 2,133,134,135,13
76970 36 2c 31 33 37 2c 32 30 32 2c 32 30 33 2c 32 30 6,137,202,203,20
76980 34 2c 32 30 35 2c 32 30 36 2c 32 30 37 2c 20 2f 4,205,206,207, /
76990 2a 20 43 78 20 2a 2f 0a 20 20 20 20 32 30 38 2c * Cx */. 208,
769a0 31 34 35 2c 31 34 36 2c 31 34 37 2c 31 34 38 2c 145,146,147,148,
769b0 31 34 39 2c 31 35 30 2c 31 35 31 2c 31 35 32 2c 149,150,151,152,
769c0 31 35 33 2c 32 31 38 2c 32 31 39 2c 32 32 30 2c 153,218,219,220,
769d0 32 32 31 2c 32 32 32 2c 32 32 33 2c 20 2f 2a 20 221,222,223, /*
769e0 44 78 20 2a 2f 0a 20 20 20 20 32 32 34 2c 32 32 Dx */. 224,22
769f0 35 2c 31 36 32 2c 31 36 33 2c 31 36 34 2c 31 36 5,162,163,164,16
76a00 35 2c 31 36 36 2c 31 36 37 2c 31 36 38 2c 31 36 5,166,167,168,16
76a10 39 2c 32 33 32 2c 32 30 33 2c 32 30 34 2c 32 30 9,232,203,204,20
76a20 35 2c 32 30 36 2c 32 30 37 2c 20 2f 2a 20 45 78 5,206,207, /* Ex
76a30 20 2a 2f 0a 20 20 20 20 32 33 39 2c 32 34 30 2c */. 239,240,
76a40 32 34 31 2c 32 34 32 2c 32 34 33 2c 32 34 34 2c 241,242,243,244,
76a50 32 34 35 2c 32 34 36 2c 32 34 37 2c 32 34 38 2c 245,246,247,248,
76a60 32 34 39 2c 32 31 39 2c 32 32 30 2c 32 32 31 2c 249,219,220,221,
76a70 32 32 32 2c 32 35 35 2c 20 2f 2a 20 46 78 20 2a 222,255, /* Fx *
76a80 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a /.#endif.};../*.
76a90 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 ** The following
76aa0 20 32 35 36 20 62 79 74 65 20 6c 6f 6f 6b 75 70 256 byte lookup
76ab0 20 74 61 62 6c 65 20 69 73 20 75 73 65 64 20 74 table is used t
76ac0 6f 20 73 75 70 70 6f 72 74 20 53 51 4c 69 74 65 o support SQLite
76ad0 73 20 62 75 69 6c 74 2d 69 6e 0a 2a 2a 20 65 71 s built-in.** eq
76ae0 75 69 76 61 6c 65 6e 74 73 20 74 6f 20 74 68 65 uivalents to the
76af0 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 61 6e 64 following stand
76b00 61 72 64 20 6c 69 62 72 61 72 79 20 66 75 6e 63 ard library func
76b10 74 69 6f 6e 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 69 tions:.**.** i
76b20 73 73 70 61 63 65 28 29 20 20 20 20 20 20 20 20 sspace()
76b30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
76b40 30 78 30 31 0a 2a 2a 20 20 20 69 73 61 6c 70 68 0x01.** isalph
76b50 61 28 29 20 20 20 20 20 20 20 20 20 20 20 20 20 a()
76b60 20 20 20 20 20 20 20 20 20 20 20 30 78 30 32 0a 0x02.
76b70 2a 2a 20 20 20 69 73 64 69 67 69 74 28 29 20 20 ** isdigit()
76b80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
76b90 20 20 20 20 20 20 30 78 30 34 0a 2a 2a 20 20 20 0x04.**
76ba0 69 73 61 6c 6e 75 6d 28 29 20 20 20 20 20 20 20 isalnum()
76bb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
76bc0 20 30 78 30 36 0a 2a 2a 20 20 20 69 73 78 64 69 0x06.** isxdi
76bd0 67 69 74 28 29 20 20 20 20 20 20 20 20 20 20 20 git()
76be0 20 20 20 20 20 20 20 20 20 20 20 20 30 78 30 38 0x08
76bf0 0a 2a 2a 20 20 20 74 6f 75 70 70 65 72 28 29 20 .** toupper()
76c00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
76c10 20 20 20 20 20 20 20 30 78 32 30 0a 2a 2a 0a 2a 0x20.**.*
76c20 2a 20 42 69 74 20 30 78 32 30 20 69 73 20 73 65 * Bit 0x20 is se
76c30 74 20 69 66 20 74 68 65 20 6d 61 70 70 65 64 20 t if the mapped
76c40 63 68 61 72 61 63 74 65 72 20 72 65 71 75 69 72 character requir
76c50 65 73 20 74 72 61 6e 73 6c 61 74 69 6f 6e 20 74 es translation t
76c60 6f 20 75 70 70 65 72 0a 2a 2a 20 63 61 73 65 2e o upper.** case.
76c70 20 69 2e 65 2e 20 69 66 20 74 68 65 20 63 68 61 i.e. if the cha
76c80 72 61 63 74 65 72 20 69 73 20 61 20 6c 6f 77 65 racter is a lowe
76c90 72 2d 63 61 73 65 20 41 53 43 49 49 20 63 68 61 r-case ASCII cha
76ca0 72 61 63 74 65 72 2e 0a 2a 2a 20 49 66 20 78 20 racter..** If x
76cb0 69 73 20 61 20 6c 6f 77 65 72 2d 63 61 73 65 20 is a lower-case
76cc0 41 53 43 49 49 20 63 68 61 72 61 63 74 65 72 2c ASCII character,
76cd0 20 74 68 65 6e 20 69 74 73 20 75 70 70 65 72 2d then its upper-
76ce0 63 61 73 65 20 65 71 75 69 76 61 6c 65 6e 74 0a case equivalent.
76cf0 2a 2a 20 69 73 20 28 78 20 2d 20 30 78 32 30 29 ** is (x - 0x20)
76d00 2e 20 54 68 65 72 65 66 6f 72 65 20 74 6f 75 70 . Therefore toup
76d10 70 65 72 28 29 20 63 61 6e 20 62 65 20 69 6d 70 per() can be imp
76d20 6c 65 6d 65 6e 74 65 64 20 61 73 3a 0a 2a 2a 0a lemented as:.**.
76d30 2a 2a 20 20 20 28 78 20 26 20 7e 28 6d 61 70 5b ** (x & ~(map[
76d40 78 5d 26 30 78 32 30 29 29 0a 2a 2a 0a 2a 2a 20 x]&0x20)).**.**
76d50 53 74 61 6e 64 61 72 64 20 66 75 6e 63 74 69 6f Standard functio
76d60 6e 20 74 6f 6c 6f 77 65 72 28 29 20 69 73 20 69 n tolower() is i
76d70 6d 70 6c 65 6d 65 6e 74 65 64 20 75 73 69 6e 67 mplemented using
76d80 20 74 68 65 20 73 71 6c 69 74 65 33 55 70 70 65 the sqlite3Uppe
76d90 72 54 6f 4c 6f 77 65 72 5b 5d 0a 2a 2a 20 61 72 rToLower[].** ar
76da0 72 61 79 2e 20 74 6f 6c 6f 77 65 72 28 29 20 69 ray. tolower() i
76db0 73 20 75 73 65 64 20 6d 6f 72 65 20 6f 66 74 65 s used more ofte
76dc0 6e 20 74 68 61 6e 20 74 6f 75 70 70 65 72 28 29 n than toupper()
76dd0 20 62 79 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a by SQLite..**.*
76de0 2a 20 53 51 4c 69 74 65 27 73 20 76 65 72 73 69 * SQLite's versi
76df0 6f 6e 73 20 61 72 65 20 69 64 65 6e 74 69 63 61 ons are identica
76e00 6c 20 74 6f 20 74 68 65 20 73 74 61 6e 64 61 72 l to the standar
76e10 64 20 76 65 72 73 69 6f 6e 73 20 61 73 73 75 6d d versions assum
76e20 69 6e 67 20 61 0a 2a 2a 20 6c 6f 63 61 6c 65 20 ing a.** locale
76e30 6f 66 20 22 43 22 2e 20 54 68 65 79 20 61 72 65 of "C". They are
76e40 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 20 implemented as
76e50 6d 61 63 72 6f 73 20 69 6e 20 73 71 6c 69 74 65 macros in sqlite
76e60 49 6e 74 2e 68 2e 0a 2a 2f 0a 23 69 66 64 65 66 Int.h..*/.#ifdef
76e70 20 53 51 4c 49 54 45 5f 41 53 43 49 49 0a 53 51 SQLITE_ASCII.SQ
76e80 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e LITE_PRIVATE con
76e90 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 st unsigned char
76ea0 20 73 71 6c 69 74 65 33 43 74 79 70 65 4d 61 70 sqlite3CtypeMap
76eb0 5b 32 35 36 5d 20 3d 20 7b 0a 20 20 30 78 30 30 [256] = {. 0x00
76ec0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
76ed0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
76ee0 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 0x00, 0x00, /*
76ef0 30 30 2e 2e 30 37 20 20 20 20 2e 2e 2e 2e 2e 2e 00..07 ......
76f00 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 .. */. 0x00, 0x
76f10 30 31 2c 20 30 78 30 31 2c 20 30 78 30 31 2c 20 01, 0x01, 0x01,
76f20 30 78 30 31 2c 20 30 78 30 31 2c 20 30 78 30 30 0x01, 0x01, 0x00
76f30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 30 38 2e 2e , 0x00, /* 08..
76f40 30 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 0f ........ *
76f50 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c 20 /. 0x00, 0x00,
76f60 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
76f70 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
76f80 30 30 2c 20 20 2f 2a 20 31 30 2e 2e 31 37 20 20 00, /* 10..17
76f90 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 ........ */.
76fa0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
76fb0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
76fc0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
76fd0 20 2f 2a 20 31 38 2e 2e 31 66 20 20 20 20 2e 2e /* 18..1f ..
76fe0 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 31 ...... */. 0x01
76ff0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
77000 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
77010 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 0x00, 0x00, /*
77020 32 30 2e 2e 32 37 20 20 20 20 20 21 22 23 24 25 20..27 !"#$%
77030 26 27 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 &' */. 0x00, 0x
77040 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
77050 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
77060 2c 20 30 78 30 30 2c 20 20 2f 2a 20 32 38 2e 2e , 0x00, /* 28..
77070 32 66 20 20 20 20 28 29 2a 2b 2c 2d 2e 2f 20 2a 2f ()*+,-./ *
77080 2f 0a 20 20 30 78 30 63 2c 20 30 78 30 63 2c 20 /. 0x0c, 0x0c,
77090 30 78 30 63 2c 20 30 78 30 63 2c 20 30 78 30 63 0x0c, 0x0c, 0x0c
770a0 2c 20 30 78 30 63 2c 20 30 78 30 63 2c 20 30 78 , 0x0c, 0x0c, 0x
770b0 30 63 2c 20 20 2f 2a 20 33 30 2e 2e 33 37 20 20 0c, /* 30..37
770c0 20 20 30 31 32 33 34 35 36 37 20 2a 2f 0a 20 20 01234567 */.
770d0 30 78 30 63 2c 20 30 78 30 63 2c 20 30 78 30 30 0x0c, 0x0c, 0x00
770e0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
770f0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
77100 20 2f 2a 20 33 38 2e 2e 33 66 20 20 20 20 38 39 /* 38..3f 89
77110 3a 3b 3c 3d 3e 3f 20 2a 2f 0a 0a 20 20 30 78 30 :;<=>? */.. 0x0
77120 30 2c 20 30 78 30 61 2c 20 30 78 30 61 2c 20 30 0, 0x0a, 0x0a, 0
77130 78 30 61 2c 20 30 78 30 61 2c 20 30 78 30 61 2c x0a, 0x0a, 0x0a,
77140 20 30 78 30 61 2c 20 30 78 30 32 2c 20 20 2f 2a 0x0a, 0x02, /*
77150 20 34 30 2e 2e 34 37 20 20 20 20 40 41 42 43 44 40..47 @ABCD
77160 45 46 47 20 2a 2f 0a 20 20 30 78 30 32 2c 20 30 EFG */. 0x02, 0
77170 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30 32 2c x02, 0x02, 0x02,
77180 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30 0x02, 0x02, 0x0
77190 32 2c 20 30 78 30 32 2c 20 20 2f 2a 20 34 38 2e 2, 0x02, /* 48.
771a0 2e 34 66 20 20 20 20 48 49 4a 4b 4c 4d 4e 4f 20 .4f HIJKLMNO
771b0 2a 2f 0a 20 20 30 78 30 32 2c 20 30 78 30 32 2c */. 0x02, 0x02,
771c0 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30 0x02, 0x02, 0x0
771d0 32 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 2, 0x02, 0x02, 0
771e0 78 30 32 2c 20 20 2f 2a 20 35 30 2e 2e 35 37 20 x02, /* 50..57
771f0 20 20 20 50 51 52 53 54 55 56 57 20 2a 2f 0a 20 PQRSTUVW */.
77200 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30 0x02, 0x02, 0x0
77210 32 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 2, 0x00, 0x00, 0
77220 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
77230 20 20 2f 2a 20 35 38 2e 2e 35 66 20 20 20 20 58 /* 58..5f X
77240 59 5a 5b 5c 5d 5e 5f 20 2a 2f 0a 20 20 30 78 30 YZ[\]^_ */. 0x0
77250 30 2c 20 30 78 32 61 2c 20 30 78 32 61 2c 20 30 0, 0x2a, 0x2a, 0
77260 78 32 61 2c 20 30 78 32 61 2c 20 30 78 32 61 2c x2a, 0x2a, 0x2a,
77270 20 30 78 32 61 2c 20 30 78 32 32 2c 20 20 2f 2a 0x2a, 0x22, /*
77280 20 36 30 2e 2e 36 37 20 20 20 20 60 61 62 63 64 60..67 `abcd
77290 65 66 67 20 2a 2f 0a 20 20 30 78 32 32 2c 20 30 efg */. 0x22, 0
772a0 78 32 32 2c 20 30 78 32 32 2c 20 30 78 32 32 2c x22, 0x22, 0x22,
772b0 20 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78 32 0x22, 0x22, 0x2
772c0 32 2c 20 30 78 32 32 2c 20 20 2f 2a 20 36 38 2e 2, 0x22, /* 68.
772d0 2e 36 66 20 20 20 20 68 69 6a 6b 6c 6d 6e 6f 20 .6f hijklmno
772e0 2a 2f 0a 20 20 30 78 32 32 2c 20 30 78 32 32 2c */. 0x22, 0x22,
772f0 20 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78 32 0x22, 0x22, 0x2
77300 32 2c 20 30 78 32 32 2c 20 30 78 32 32 2c 20 30 2, 0x22, 0x22, 0
77310 78 32 32 2c 20 20 2f 2a 20 37 30 2e 2e 37 37 20 x22, /* 70..77
77320 20 20 20 70 71 72 73 74 75 76 77 20 2a 2f 0a 20 pqrstuvw */.
77330 20 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78 32 0x22, 0x22, 0x2
77340 32 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 2, 0x00, 0x00, 0
77350 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
77360 20 20 2f 2a 20 37 38 2e 2e 37 66 20 20 20 20 78 /* 78..7f x
77370 79 7a 7b 7c 7d 7e 2e 20 2a 2f 0a 0a 20 20 30 78 yz{|}~. */.. 0x
77380 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
77390 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
773a0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f , 0x00, 0x00, /
773b0 2a 20 38 30 2e 2e 38 37 20 20 20 20 2e 2e 2e 2e * 80..87 ....
773c0 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 .... */. 0x00,
773d0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
773e0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
773f0 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 38 38 00, 0x00, /* 88
77400 2e 2e 38 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e ..8f ........
77410 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 30 */. 0x00, 0x00
77420 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
77430 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
77440 30 78 30 30 2c 20 20 2f 2a 20 39 30 2e 2e 39 37 0x00, /* 90..97
77450 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a ........ */.
77460 20 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 0x00, 0x00, 0x
77470 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
77480 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
77490 2c 20 20 2f 2a 20 39 38 2e 2e 39 66 20 20 20 20 , /* 98..9f
774a0 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 ........ */. 0x
774b0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
774c0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
774d0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f , 0x00, 0x00, /
774e0 2a 20 61 30 2e 2e 61 37 20 20 20 20 2e 2e 2e 2e * a0..a7 ....
774f0 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 .... */. 0x00,
77500 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
77510 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
77520 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 61 38 00, 0x00, /* a8
77530 2e 2e 61 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e ..af ........
77540 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 30 */. 0x00, 0x00
77550 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
77560 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
77570 30 78 30 30 2c 20 20 2f 2a 20 62 30 2e 2e 62 37 0x00, /* b0..b7
77580 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a ........ */.
77590 20 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 0x00, 0x00, 0x
775a0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
775b0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 0x00, 0x00, 0x00
775c0 2c 20 20 2f 2a 20 62 38 2e 2e 62 66 20 20 20 20 , /* b8..bf
775d0 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 0a 20 20 30 ........ */.. 0
775e0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
775f0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
77600 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20 0, 0x00, 0x00,
77610 2f 2a 20 63 30 2e 2e 63 37 20 20 20 20 2e 2e 2e /* c0..c7 ...
77620 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c ..... */. 0x00,
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 30 78 30 30 2c 20 20 2f 2a 20 63 x00, 0x00, /* c
77660 38 2e 2e 63 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 8..cf .......
77670 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 . */. 0x00, 0x0
77680 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
77690 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
776a0 20 30 78 30 30 2c 20 20 2f 2a 20 64 30 2e 2e 64 0x00, /* d0..d
776b0 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 7 ........ */
776c0 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 . 0x00, 0x00, 0
776d0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
776e0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
776f0 30 2c 20 20 2f 2a 20 64 38 2e 2e 64 66 20 20 20 0, /* d8..df
77700 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 ........ */. 0
77710 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
77720 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
77730 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20 0, 0x00, 0x00,
77740 2f 2a 20 65 30 2e 2e 65 37 20 20 20 20 2e 2e 2e /* e0..e7 ...
77750 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c ..... */. 0x00,
77760 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
77770 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
77780 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 65 x00, 0x00, /* e
77790 38 2e 2e 65 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 8..ef .......
777a0 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 . */. 0x00, 0x0
777b0 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
777c0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
777d0 20 30 78 30 30 2c 20 20 2f 2a 20 66 30 2e 2e 66 0x00, /* f0..f
777e0 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 7 ........ */
777f0 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 . 0x00, 0x00, 0
77800 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
77810 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
77820 30 20 20 20 2f 2a 20 66 38 2e 2e 66 66 20 20 20 0 /* f8..ff
77830 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 7d 3b 0a ........ */.};.
77840 23 65 6e 64 69 66 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 #endif..../*.**
77850 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 69 The following si
77860 6e 67 6c 65 74 6f 6e 20 63 6f 6e 74 61 69 6e 73 ngleton contains
77870 20 74 68 65 20 67 6c 6f 62 61 6c 20 63 6f 6e 66 the global conf
77880 69 67 75 72 61 74 69 6f 6e 20 66 6f 72 0a 2a 2a iguration for.**
77890 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 the SQLite libr
778a0 61 72 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ary..*/.SQLITE_P
778b0 52 49 56 41 54 45 20 53 51 4c 49 54 45 5f 57 53 RIVATE SQLITE_WS
778c0 44 20 73 74 72 75 63 74 20 53 71 6c 69 74 65 33 D struct Sqlite3
778d0 43 6f 6e 66 69 67 20 73 71 6c 69 74 65 33 43 6f Config sqlite3Co
778e0 6e 66 69 67 20 3d 20 7b 0a 20 20 20 53 51 4c 49 nfig = {. SQLI
778f0 54 45 5f 44 45 46 41 55 4c 54 5f 4d 45 4d 53 54 TE_DEFAULT_MEMST
77900 41 54 55 53 2c 20 20 2f 2a 20 62 4d 65 6d 73 74 ATUS, /* bMemst
77910 61 74 20 2a 2f 0a 20 20 20 31 2c 20 20 20 20 20 at */. 1,
77920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
77930 20 20 20 20 2f 2a 20 62 43 6f 72 65 4d 75 74 65 /* bCoreMute
77940 78 20 2a 2f 0a 20 20 20 53 51 4c 49 54 45 5f 54 x */. SQLITE_T
77950 48 52 45 41 44 53 41 46 45 3d 3d 31 2c 20 20 20 HREADSAFE==1,
77960 20 20 20 2f 2a 20 62 46 75 6c 6c 4d 75 74 65 78 /* bFullMutex
77970 20 2a 2f 0a 20 20 20 30 78 37 66 66 66 66 66 66 */. 0x7ffffff
77980 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e,
77990 20 20 2f 2a 20 6d 78 53 74 72 6c 65 6e 20 2a 2f /* mxStrlen */
779a0 0a 20 20 20 31 30 30 2c 20 20 20 20 20 20 20 20 . 100,
779b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
779c0 2a 20 73 7a 4c 6f 6f 6b 61 73 69 64 65 20 2a 2f * szLookaside */
779d0 0a 20 20 20 35 30 30 2c 20 20 20 20 20 20 20 20 . 500,
779e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
779f0 2a 20 6e 4c 6f 6f 6b 61 73 69 64 65 20 2a 2f 0a * nLookaside */.
77a00 20 20 20 7b 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c {0,0,0,0,0,0,
77a10 30 2c 30 7d 2c 20 20 20 20 20 20 20 20 20 2f 2a 0,0}, /*
77a20 20 6d 20 2a 2f 0a 20 20 20 7b 30 2c 30 2c 30 2c m */. {0,0,0,
77a30 30 2c 30 2c 30 2c 30 2c 30 2c 30 7d 2c 20 20 20 0,0,0,0,0,0},
77a40 20 20 20 20 2f 2a 20 6d 75 74 65 78 20 2a 2f 0a /* mutex */.
77a50 20 20 20 7b 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c {0,0,0,0,0,0,
77a60 30 2c 30 2c 30 2c 30 2c 30 7d 2c 20 20 20 2f 2a 0,0,0,0,0}, /*
77a70 20 70 63 61 63 68 65 20 2a 2f 0a 20 20 20 28 76 pcache */. (v
77a80 6f 69 64 2a 29 30 2c 20 20 20 20 20 20 20 20 20 oid*)0,
77a90 20 20 20 20 20 20 20 20 20 2f 2a 20 70 48 65 61 /* pHea
77aa0 70 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 p */. 0,
77ab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
77ac0 20 20 20 2f 2a 20 6e 48 65 61 70 20 2a 2f 0a 20 /* nHeap */.
77ad0 20 20 30 2c 20 30 2c 20 20 20 20 20 20 20 20 20 0, 0,
77ae0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
77af0 6d 6e 48 65 61 70 2c 20 6d 78 48 65 61 70 20 2a mnHeap, mxHeap *
77b00 2f 0a 20 20 20 28 76 6f 69 64 2a 29 30 2c 20 20 /. (void*)0,
77b10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
77b20 2f 2a 20 70 53 63 72 61 74 63 68 20 2a 2f 0a 20 /* pScratch */.
77b30 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 0,
77b40 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
77b50 73 7a 53 63 72 61 74 63 68 20 2a 2f 0a 20 20 20 szScratch */.
77b60 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
77b70 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6e 53 /* nS
77b80 63 72 61 74 63 68 20 2a 2f 0a 20 20 20 28 76 6f cratch */. (vo
77b90 69 64 2a 29 30 2c 20 20 20 20 20 20 20 20 20 20 id*)0,
77ba0 20 20 20 20 20 20 20 20 2f 2a 20 70 50 61 67 65 /* pPage
77bb0 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 20 */. 0,
77bc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
77bd0 20 20 2f 2a 20 73 7a 50 61 67 65 20 2a 2f 0a 20 /* szPage */.
77be0 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 0,
77bf0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
77c00 6e 50 61 67 65 20 2a 2f 0a 20 20 20 30 2c 20 20 nPage */. 0,
77c10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
77c20 20 20 20 20 20 20 20 2f 2a 20 6d 78 50 61 72 73 /* mxPars
77c30 65 72 53 74 61 63 6b 20 2a 2f 0a 20 20 20 30 2c erStack */. 0,
77c40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
77c50 20 20 20 20 20 20 20 20 20 2f 2a 20 73 68 61 72 /* shar
77c60 65 64 43 61 63 68 65 45 6e 61 62 6c 65 64 20 2a edCacheEnabled *
77c70 2f 0a 20 20 20 2f 2a 20 41 6c 6c 20 74 68 65 20 /. /* All the
77c80 72 65 73 74 20 73 68 6f 75 6c 64 20 61 6c 77 61 rest should alwa
77c90 79 73 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 ys be initialize
77ca0 64 20 74 6f 20 7a 65 72 6f 20 2a 2f 0a 20 20 20 d to zero */.
77cb0 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
77cc0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 73 /* is
77cd0 49 6e 69 74 20 2a 2f 0a 20 20 20 30 2c 20 20 20 Init */. 0,
77ce0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
77cf0 20 20 20 20 20 20 2f 2a 20 69 6e 50 72 6f 67 72 /* inProgr
77d00 65 73 73 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 ess */. 0,
77d10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
77d20 20 20 20 20 20 2f 2a 20 69 73 4d 75 74 65 78 49 /* isMutexI
77d30 6e 69 74 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 nit */. 0,
77d40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
77d50 20 20 20 20 20 2f 2a 20 69 73 4d 61 6c 6c 6f 63 /* isMalloc
77d60 49 6e 69 74 20 2a 2f 0a 20 20 20 30 2c 20 20 20 Init */. 0,
77d70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
77d80 20 20 20 20 20 20 2f 2a 20 69 73 50 43 61 63 68 /* isPCach
77d90 65 49 6e 69 74 20 2a 2f 0a 20 20 20 30 2c 20 20 eInit */. 0,
77da0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
77db0 20 20 20 20 20 20 20 2f 2a 20 70 49 6e 69 74 4d /* pInitM
77dc0 75 74 65 78 20 2a 2f 0a 20 20 20 30 2c 20 20 20 utex */. 0,
77dd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
77de0 20 20 20 20 20 20 2f 2a 20 6e 52 65 66 49 6e 69 /* nRefIni
77df0 74 4d 75 74 65 78 20 2a 2f 0a 7d 3b 0a 0a 0a 2f tMutex */.};.../
77e00 2a 0a 2a 2a 20 48 61 73 68 20 74 61 62 6c 65 20 *.** Hash table
77e10 66 6f 72 20 67 6c 6f 62 61 6c 20 66 75 6e 63 74 for global funct
77e20 69 6f 6e 73 20 2d 20 66 75 6e 63 74 69 6f 6e 73 ions - functions
77e30 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 0a 2a common to all.*
77e40 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 * database conne
77e50 63 74 69 6f 6e 73 2e 20 20 41 66 74 65 72 20 69 ctions. After i
77e60 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 2c 20 74 nitialization, t
77e70 68 69 73 20 74 61 62 6c 65 20 69 73 0a 2a 2a 20 his table is.**
77e80 72 65 61 64 2d 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 read-only..*/.SQ
77e90 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53 51 4c LITE_PRIVATE SQL
77ea0 49 54 45 5f 57 53 44 20 46 75 6e 63 44 65 66 48 ITE_WSD FuncDefH
77eb0 61 73 68 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 ash sqlite3Globa
77ec0 6c 46 75 6e 63 74 69 6f 6e 73 3b 0a 0a 2f 2a 0a lFunctions;../*.
77ed0 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20 ** The value of
77ee0 74 68 65 20 22 70 65 6e 64 69 6e 67 22 20 62 79 the "pending" by
77ef0 74 65 20 6d 75 73 74 20 62 65 20 30 78 34 30 30 te must be 0x400
77f00 30 30 30 30 30 20 28 31 20 62 79 74 65 20 70 61 00000 (1 byte pa
77f10 73 74 20 74 68 65 0a 2a 2a 20 31 2d 67 69 62 61 st the.** 1-giba
77f20 62 79 74 65 20 62 6f 75 6e 64 61 72 79 29 20 69 byte boundary) i
77f30 6e 20 61 20 63 6f 6d 70 61 74 69 62 6c 65 20 64 n a compatible d
77f40 61 74 61 62 61 73 65 2e 20 20 53 51 4c 69 74 65 atabase. SQLite
77f50 20 6e 65 76 65 72 20 75 73 65 73 0a 2a 2a 20 74 never uses.** t
77f60 68 65 20 64 61 74 61 62 61 73 65 20 70 61 67 65 he database page
77f70 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 that contains t
77f80 68 65 20 70 65 6e 64 69 6e 67 20 62 79 74 65 2e he pending byte.
77f90 20 20 49 74 20 6e 65 76 65 72 20 61 74 74 65 6d It never attem
77fa0 70 74 73 0a 2a 2a 20 74 6f 20 72 65 61 64 20 6f pts.** to read o
77fb0 72 20 77 72 69 74 65 20 74 68 61 74 20 70 61 67 r write that pag
77fc0 65 2e 20 20 54 68 65 20 70 65 6e 64 69 6e 67 20 e. The pending
77fd0 62 79 74 65 20 70 61 67 65 20 69 73 20 73 65 74 byte page is set
77fe0 20 61 73 73 69 67 6e 0a 2a 2a 20 66 6f 72 20 75 assign.** for u
77ff0 73 65 20 62 79 20 74 68 65 20 56 46 53 20 6c 61 se by the VFS la
78000 79 65 72 73 20 61 73 20 73 70 61 63 65 20 66 6f yers as space fo
78010 72 20 6d 61 6e 61 67 69 6e 67 20 66 69 6c 65 20 r managing file
78020 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 44 75 72 locks..**.** Dur
78030 69 6e 67 20 74 65 73 74 69 6e 67 2c 20 69 74 20 ing testing, it
78040 69 73 20 6f 66 74 65 6e 20 64 65 73 69 72 61 62 is often desirab
78050 6c 65 20 74 6f 20 6d 6f 76 65 20 74 68 65 20 70 le to move the p
78060 65 6e 64 69 6e 67 20 62 79 74 65 20 74 6f 0a 2a ending byte to.*
78070 2a 20 61 20 64 69 66 66 65 72 65 6e 74 20 70 6f * a different po
78080 73 69 74 69 6f 6e 20 69 6e 20 74 68 65 20 66 69 sition in the fi
78090 6c 65 2e 20 20 54 68 69 73 20 61 6c 6c 6f 77 73 le. This allows
780a0 20 63 6f 64 65 20 74 68 61 74 20 68 61 73 20 74 code that has t
780b0 6f 0a 2a 2a 20 64 65 61 6c 20 77 69 74 68 20 74 o.** deal with t
780c0 68 65 20 70 65 6e 64 69 6e 67 20 62 79 74 65 20 he pending byte
780d0 74 6f 20 72 75 6e 20 6f 6e 20 66 69 6c 65 73 20 to run on files
780e0 74 68 61 74 20 61 72 65 20 6d 75 63 68 20 73 6d that are much sm
780f0 61 6c 6c 65 72 0a 2a 2a 20 74 68 61 6e 20 31 20 aller.** than 1
78100 47 69 42 2e 20 20 54 68 65 20 73 71 6c 69 74 65 GiB. The sqlite
78110 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 29 3_test_control()
78120 20 69 6e 74 65 72 66 61 63 65 20 63 61 6e 20 62 interface can b
78130 65 20 75 73 65 64 20 74 6f 0a 2a 2a 20 6d 6f 76 e used to.** mov
78140 65 20 74 68 65 20 70 65 6e 64 69 6e 67 20 62 79 e the pending by
78150 74 65 2e 0a 2a 2a 0a 2a 2a 20 49 4d 50 4f 52 54 te..**.** IMPORT
78160 41 4e 54 3a 20 20 43 68 61 6e 67 69 6e 67 20 74 ANT: Changing t
78170 68 65 20 70 65 6e 64 69 6e 67 20 62 79 74 65 20 he pending byte
78180 74 6f 20 61 6e 79 20 76 61 6c 75 65 20 6f 74 68 to any value oth
78190 65 72 20 74 68 61 6e 0a 2a 2a 20 30 78 34 30 30 er than.** 0x400
781a0 30 30 30 30 30 20 72 65 73 75 6c 74 73 20 69 6e 00000 results in
781b0 20 61 6e 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65 an incompatible
781c0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 66 database file f
781d0 6f 72 6d 61 74 21 0a 2a 2a 20 43 68 61 6e 67 69 ormat!.** Changi
781e0 6e 67 20 74 68 65 20 70 65 6e 64 69 6e 67 20 62 ng the pending b
781f0 79 74 65 20 64 75 72 69 6e 67 20 6f 70 65 72 61 yte during opera
78200 74 69 6e 67 20 72 65 73 75 6c 74 73 20 69 6e 20 ting results in
78210 75 6e 64 65 66 69 6e 65 64 0a 2a 2a 20 61 6e 64 undefined.** and
78220 20 64 69 6c 65 74 65 72 69 6f 75 73 20 62 65 68 dileterious beh
78230 61 76 69 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 avior..*/.SQLITE
78240 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
78250 69 74 65 33 50 65 6e 64 69 6e 67 42 79 74 65 20 ite3PendingByte
78260 3d 20 30 78 34 30 30 30 30 30 30 30 3b 0a 0a 2f = 0x40000000;../
78270 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
78280 6e 64 20 6f 66 20 67 6c 6f 62 61 6c 2e 63 20 2a nd of global.c *
78290 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
782a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
782b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
782c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 ************** B
782d0 65 67 69 6e 20 66 69 6c 65 20 73 74 61 74 75 73 egin file status
782e0 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
782f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
78300 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
78310 2a 0a 2a 2a 20 32 30 30 38 20 4a 75 6e 65 20 31 *.** 2008 June 1
78320 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 8.**.** The auth
78330 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
78340 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
78350 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
78360 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
78370 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
78380 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
78390 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
783a0 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
783b0 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
783c0 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
783d0 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
783e0 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
783f0 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
78400 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
78410 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
78420 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
78430 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
78440 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
78450 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
78460 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
78470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
78480 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d 6f 64 75 .**.** This modu
78490 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 le implements th
784a0 65 20 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73 e sqlite3_status
784b0 28 29 20 69 6e 74 65 72 66 61 63 65 20 61 6e 64 () interface and
784c0 20 72 65 6c 61 74 65 64 0a 2a 2a 20 66 75 6e 63 related.** func
784d0 74 69 6f 6e 61 6c 69 74 79 2e 0a 2a 2a 0a 2a 2a tionality..**.**
784e0 20 24 49 64 3a 20 73 74 61 74 75 73 2e 63 2c 76 $Id: status.c,v
784f0 20 31 2e 39 20 32 30 30 38 2f 30 39 2f 30 32 20 1.9 2008/09/02
78500 30 30 3a 35 32 3a 35 32 20 64 72 68 20 45 78 70 00:52:52 drh Exp
78510 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 56 61 72 $.*/../*.** Var
78520 69 61 62 6c 65 73 20 69 6e 20 77 68 69 63 68 20 iables in which
78530 74 6f 20 72 65 63 6f 72 64 20 73 74 61 74 75 73 to record status
78540 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f information..*/
78550 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
78560 73 71 6c 69 74 65 33 53 74 61 74 54 79 70 65 20 sqlite3StatType
78570 73 71 6c 69 74 65 33 53 74 61 74 54 79 70 65 3b sqlite3StatType;
78580 0a 73 74 61 74 69 63 20 53 51 4c 49 54 45 5f 57 .static SQLITE_W
78590 53 44 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 SD struct sqlite
785a0 33 53 74 61 74 54 79 70 65 20 7b 0a 20 20 69 6e 3StatType {. in
785b0 74 20 6e 6f 77 56 61 6c 75 65 5b 39 5d 3b 20 20 t nowValue[9];
785c0 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e /* Curren
785d0 74 20 76 61 6c 75 65 20 2a 2f 0a 20 20 69 6e 74 t value */. int
785e0 20 6d 78 56 61 6c 75 65 5b 39 5d 3b 20 20 20 20 mxValue[9];
785f0 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d /* Maximum
78600 20 76 61 6c 75 65 20 2a 2f 0a 7d 20 73 71 6c 69 value */.} sqli
78610 74 65 33 53 74 61 74 20 3d 20 7b 20 7b 30 2c 7d te3Stat = { {0,}
78620 2c 20 7b 30 2c 7d 20 7d 3b 0a 0a 0a 2f 2a 20 54 , {0,} };.../* T
78630 68 65 20 22 77 73 64 53 74 61 74 22 20 6d 61 63 he "wsdStat" mac
78640 72 6f 20 77 69 6c 6c 20 72 65 73 6f 6c 76 65 20 ro will resolve
78650 74 6f 20 74 68 65 20 73 74 61 74 75 73 20 69 6e to the status in
78660 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 73 74 61 formation.** sta
78670 74 65 20 76 65 63 74 6f 72 2e 20 20 49 66 20 77 te vector. If w
78680 72 69 74 61 62 6c 65 20 73 74 61 74 69 63 20 64 ritable static d
78690 61 74 61 20 69 73 20 75 6e 73 75 70 70 6f 72 74 ata is unsupport
786a0 65 64 20 6f 6e 20 74 68 65 20 74 61 72 67 65 74 ed on the target
786b0 2c 0a 2a 2a 20 77 65 20 68 61 76 65 20 74 6f 20 ,.** we have to
786c0 6c 6f 63 61 74 65 20 74 68 65 20 73 74 61 74 65 locate the state
786d0 20 76 65 63 74 6f 72 20 61 74 20 72 75 6e 2d 74 vector at run-t
786e0 69 6d 65 2e 20 20 49 6e 20 74 68 65 20 6d 6f 72 ime. In the mor
786f0 65 20 63 6f 6d 6d 6f 6e 0a 2a 2a 20 63 61 73 65 e common.** case
78700 20 77 68 65 72 65 20 77 72 69 74 61 62 6c 65 20 where writable
78710 73 74 61 74 69 63 20 64 61 74 61 20 69 73 20 73 static data is s
78720 75 70 70 6f 72 74 65 64 2c 20 77 73 64 53 74 61 upported, wsdSta
78730 74 20 63 61 6e 20 72 65 66 65 72 20 64 69 72 65 t can refer dire
78740 63 74 6c 79 0a 2a 2a 20 74 6f 20 74 68 65 20 22 ctly.** to the "
78750 73 71 6c 69 74 65 33 53 74 61 74 22 20 73 74 61 sqlite3Stat" sta
78760 74 65 20 76 65 63 74 6f 72 20 64 65 63 6c 61 72 te vector declar
78770 65 64 20 61 62 6f 76 65 2e 0a 2a 2f 0a 23 69 66 ed above..*/.#if
78780 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
78790 57 53 44 0a 23 20 64 65 66 69 6e 65 20 77 73 64 WSD.# define wsd
787a0 53 74 61 74 49 6e 69 74 20 20 73 71 6c 69 74 65 StatInit sqlite
787b0 33 53 74 61 74 54 79 70 65 20 2a 78 20 3d 20 26 3StatType *x = &
787c0 47 4c 4f 42 41 4c 28 73 71 6c 69 74 65 33 53 74 GLOBAL(sqlite3St
787d0 61 74 54 79 70 65 2c 73 71 6c 69 74 65 33 53 74 atType,sqlite3St
787e0 61 74 29 0a 23 20 64 65 66 69 6e 65 20 77 73 64 at).# define wsd
787f0 53 74 61 74 20 78 5b 30 5d 0a 23 65 6c 73 65 0a Stat x[0].#else.
78800 23 20 64 65 66 69 6e 65 20 77 73 64 53 74 61 74 # define wsdStat
78810 49 6e 69 74 0a 23 20 64 65 66 69 6e 65 20 77 73 Init.# define ws
78820 64 53 74 61 74 20 73 71 6c 69 74 65 33 53 74 61 dStat sqlite3Sta
78830 74 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 t.#endif../*.**
78840 52 65 74 75 72 6e 20 74 68 65 20 63 75 72 72 65 Return the curre
78850 6e 74 20 76 61 6c 75 65 20 6f 66 20 61 20 73 74 nt value of a st
78860 61 74 75 73 20 70 61 72 61 6d 65 74 65 72 2e 0a atus parameter..
78870 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
78880 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 74 61 E int sqlite3Sta
78890 74 75 73 56 61 6c 75 65 28 69 6e 74 20 6f 70 29 tusValue(int op)
788a0 7b 0a 20 20 77 73 64 53 74 61 74 49 6e 69 74 3b {. wsdStatInit;
788b0 0a 20 20 61 73 73 65 72 74 28 20 6f 70 3e 3d 30 . assert( op>=0
788c0 20 26 26 20 6f 70 3c 41 72 72 61 79 53 69 7a 65 && op<ArraySize
788d0 28 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 (wsdStat.nowValu
788e0 65 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 77 e) );. return w
788f0 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 5b sdStat.nowValue[
78900 6f 70 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 op];.}../*.** Ad
78910 64 20 4e 20 74 6f 20 74 68 65 20 76 61 6c 75 65 d N to the value
78920 20 6f 66 20 61 20 73 74 61 74 75 73 20 72 65 63 of a status rec
78930 6f 72 64 2e 20 20 49 74 20 69 73 20 61 73 73 75 ord. It is assu
78940 6d 65 64 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 med that the.**
78950 63 61 6c 6c 65 72 20 68 6f 6c 64 73 20 61 70 70 caller holds app
78960 72 6f 70 72 69 61 74 65 20 6c 6f 63 6b 73 2e 0a ropriate locks..
78970 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
78980 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74 E void sqlite3St
78990 61 74 75 73 41 64 64 28 69 6e 74 20 6f 70 2c 20 atusAdd(int op,
789a0 69 6e 74 20 4e 29 7b 0a 20 20 77 73 64 53 74 61 int N){. wsdSta
789b0 74 49 6e 69 74 3b 0a 20 20 61 73 73 65 72 74 28 tInit;. assert(
789c0 20 6f 70 3e 3d 30 20 26 26 20 6f 70 3c 41 72 72 op>=0 && op<Arr
789d0 61 79 53 69 7a 65 28 77 73 64 53 74 61 74 2e 6e aySize(wsdStat.n
789e0 6f 77 56 61 6c 75 65 29 20 29 3b 0a 20 20 77 73 owValue) );. ws
789f0 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f dStat.nowValue[o
78a00 70 5d 20 2b 3d 20 4e 3b 0a 20 20 69 66 28 20 77 p] += N;. if( w
78a10 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 5b sdStat.nowValue[
78a20 6f 70 5d 3e 77 73 64 53 74 61 74 2e 6d 78 56 61 op]>wsdStat.mxVa
78a30 6c 75 65 5b 6f 70 5d 20 29 7b 0a 20 20 20 20 77 lue[op] ){. w
78a40 73 64 53 74 61 74 2e 6d 78 56 61 6c 75 65 5b 6f sdStat.mxValue[o
78a50 70 5d 20 3d 20 77 73 64 53 74 61 74 2e 6e 6f 77 p] = wsdStat.now
78a60 56 61 6c 75 65 5b 6f 70 5d 3b 0a 20 20 7d 0a 7d Value[op];. }.}
78a70 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 ../*.** Set the
78a80 76 61 6c 75 65 20 6f 66 20 61 20 73 74 61 74 75 value of a statu
78a90 73 20 74 6f 20 58 2e 0a 2a 2f 0a 53 51 4c 49 54 s to X..*/.SQLIT
78aa0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
78ab0 71 6c 69 74 65 33 53 74 61 74 75 73 53 65 74 28 qlite3StatusSet(
78ac0 69 6e 74 20 6f 70 2c 20 69 6e 74 20 58 29 7b 0a int op, int X){.
78ad0 20 20 77 73 64 53 74 61 74 49 6e 69 74 3b 0a 20 wsdStatInit;.
78ae0 20 61 73 73 65 72 74 28 20 6f 70 3e 3d 30 20 26 assert( op>=0 &
78af0 26 20 6f 70 3c 41 72 72 61 79 53 69 7a 65 28 77 & op<ArraySize(w
78b00 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 29 sdStat.nowValue)
78b10 20 29 3b 0a 20 20 77 73 64 53 74 61 74 2e 6e 6f );. wsdStat.no
78b20 77 56 61 6c 75 65 5b 6f 70 5d 20 3d 20 58 3b 0a wValue[op] = X;.
78b30 20 20 69 66 28 20 77 73 64 53 74 61 74 2e 6e 6f if( wsdStat.no
78b40 77 56 61 6c 75 65 5b 6f 70 5d 3e 77 73 64 53 74 wValue[op]>wsdSt
78b50 61 74 2e 6d 78 56 61 6c 75 65 5b 6f 70 5d 20 29 at.mxValue[op] )
78b60 7b 0a 20 20 20 20 77 73 64 53 74 61 74 2e 6d 78 {. wsdStat.mx
78b70 56 61 6c 75 65 5b 6f 70 5d 20 3d 20 77 73 64 53 Value[op] = wsdS
78b80 74 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d tat.nowValue[op]
78b90 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51 ;. }.}../*.** Q
78ba0 75 65 72 79 20 73 74 61 74 75 73 20 69 6e 66 6f uery status info
78bb0 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 rmation..**.** T
78bc0 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 his implementati
78bd0 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 61 74 20 on assumes that
78be0 72 65 61 64 69 6e 67 20 6f 72 20 77 72 69 74 69 reading or writi
78bf0 6e 67 20 61 6e 20 61 6c 69 67 6e 65 64 0a 2a 2a ng an aligned.**
78c00 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 20 32-bit integer
78c10 69 73 20 61 6e 20 61 74 6f 6d 69 63 20 6f 70 65 is an atomic ope
78c20 72 61 74 69 6f 6e 2e 20 20 49 66 20 74 68 61 74 ration. If that
78c30 20 61 73 73 75 6d 70 74 69 6f 6e 20 69 73 20 6e assumption is n
78c40 6f 74 20 74 72 75 65 2c 0a 2a 2a 20 74 68 65 6e ot true,.** then
78c50 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 this routine is
78c60 20 6e 6f 74 20 74 68 72 65 61 64 73 61 66 65 2e not threadsafe.
78c70 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
78c80 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 61 74 75 nt sqlite3_statu
78c90 73 28 69 6e 74 20 6f 70 2c 20 69 6e 74 20 2a 70 s(int op, int *p
78ca0 43 75 72 72 65 6e 74 2c 20 69 6e 74 20 2a 70 48 Current, int *pH
78cb0 69 67 68 77 61 74 65 72 2c 20 69 6e 74 20 72 65 ighwater, int re
78cc0 73 65 74 46 6c 61 67 29 7b 0a 20 20 77 73 64 53 setFlag){. wsdS
78cd0 74 61 74 49 6e 69 74 3b 0a 20 20 69 66 28 20 6f tatInit;. if( o
78ce0 70 3c 30 20 7c 7c 20 6f 70 3e 3d 41 72 72 61 79 p<0 || op>=Array
78cf0 53 69 7a 65 28 77 73 64 53 74 61 74 2e 6e 6f 77 Size(wsdStat.now
78d00 56 61 6c 75 65 29 20 29 7b 0a 20 20 20 20 72 65 Value) ){. re
78d10 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 turn SQLITE_MISU
78d20 53 45 3b 0a 20 20 7d 0a 20 20 2a 70 43 75 72 72 SE;. }. *pCurr
78d30 65 6e 74 20 3d 20 77 73 64 53 74 61 74 2e 6e 6f ent = wsdStat.no
78d40 77 56 61 6c 75 65 5b 6f 70 5d 3b 0a 20 20 2a 70 wValue[op];. *p
78d50 48 69 67 68 77 61 74 65 72 20 3d 20 77 73 64 53 Highwater = wsdS
78d60 74 61 74 2e 6d 78 56 61 6c 75 65 5b 6f 70 5d 3b tat.mxValue[op];
78d70 0a 20 20 69 66 28 20 72 65 73 65 74 46 6c 61 67 . if( resetFlag
78d80 20 29 7b 0a 20 20 20 20 77 73 64 53 74 61 74 2e ){. wsdStat.
78d90 6d 78 56 61 6c 75 65 5b 6f 70 5d 20 3d 20 77 73 mxValue[op] = ws
78da0 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f dStat.nowValue[o
78db0 70 5d 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e p];. }. return
78dc0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f SQLITE_OK;.}../
78dd0 2a 0a 2a 2a 20 51 75 65 72 79 20 73 74 61 74 75 *.** Query statu
78de0 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f s information fo
78df0 72 20 61 20 73 69 6e 67 6c 65 20 64 61 74 61 62 r a single datab
78e00 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a ase connection.*
78e10 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
78e20 20 73 71 6c 69 74 65 33 5f 64 62 5f 73 74 61 74 sqlite3_db_stat
78e30 75 73 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 us(. sqlite3 *d
78e40 62 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 b, /* T
78e50 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e he database conn
78e60 65 63 74 69 6f 6e 20 77 68 6f 73 65 20 73 74 61 ection whose sta
78e70 74 75 73 20 69 73 20 64 65 73 69 72 65 64 20 2a tus is desired *
78e80 2f 0a 20 20 69 6e 74 20 6f 70 2c 20 20 20 20 20 /. int op,
78e90 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61 /* Sta
78ea0 74 75 73 20 76 65 72 62 20 2a 2f 0a 20 20 69 6e tus verb */. in
78eb0 74 20 2a 70 43 75 72 72 65 6e 74 2c 20 20 20 20 t *pCurrent,
78ec0 20 20 20 20 2f 2a 20 57 72 69 74 65 20 63 75 72 /* Write cur
78ed0 72 65 6e 74 20 76 61 6c 75 65 20 68 65 72 65 20 rent value here
78ee0 2a 2f 0a 20 20 69 6e 74 20 2a 70 48 69 67 68 77 */. int *pHighw
78ef0 61 74 65 72 2c 20 20 20 20 20 20 2f 2a 20 57 72 ater, /* Wr
78f00 69 74 65 20 68 69 67 68 2d 77 61 74 65 72 20 6d ite high-water m
78f10 61 72 6b 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e ark here */. in
78f20 74 20 72 65 73 65 74 46 6c 61 67 20 20 20 20 20 t resetFlag
78f30 20 20 20 20 2f 2a 20 52 65 73 65 74 20 68 69 67 /* Reset hig
78f40 68 2d 77 61 74 65 72 20 6d 61 72 6b 20 69 66 20 h-water mark if
78f50 74 72 75 65 20 2a 2f 0a 29 7b 0a 20 20 73 77 69 true */.){. swi
78f60 74 63 68 28 20 6f 70 20 29 7b 0a 20 20 20 20 63 tch( op ){. c
78f70 61 73 65 20 53 51 4c 49 54 45 5f 44 42 53 54 41 ase SQLITE_DBSTA
78f80 54 55 53 5f 4c 4f 4f 4b 41 53 49 44 45 5f 55 53 TUS_LOOKASIDE_US
78f90 45 44 3a 20 7b 0a 20 20 20 20 20 20 2a 70 43 75 ED: {. *pCu
78fa0 72 72 65 6e 74 20 3d 20 64 62 2d 3e 6c 6f 6f 6b rrent = db->look
78fb0 61 73 69 64 65 2e 6e 4f 75 74 3b 0a 20 20 20 20 aside.nOut;.
78fc0 20 20 2a 70 48 69 67 68 77 61 74 65 72 20 3d 20 *pHighwater =
78fd0 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6d 78 db->lookaside.mx
78fe0 4f 75 74 3b 0a 20 20 20 20 20 20 69 66 28 20 72 Out;. if( r
78ff0 65 73 65 74 46 6c 61 67 20 29 7b 0a 20 20 20 20 esetFlag ){.
79000 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 db->lookasid
79010 65 2e 6d 78 4f 75 74 20 3d 20 64 62 2d 3e 6c 6f e.mxOut = db->lo
79020 6f 6b 61 73 69 64 65 2e 6e 4f 75 74 3b 0a 20 20 okaside.nOut;.
79030 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 }. brea
79040 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 k;. }. def
79050 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 72 65 ault: {. re
79060 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f turn SQLITE_ERRO
79070 52 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 R;. }. }. r
79080 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
79090 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .}../***********
790a0 2a 2a 2a 20 45 6e 64 20 6f 66 20 73 74 61 74 75 *** End of statu
790b0 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a s.c ************
790c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
790d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
790e0 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
790f0 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 64 *** Begin file d
79100 61 74 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ate.c **********
79110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
79120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
79130 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 33 20 4f **/./*.** 2003 O
79140 63 74 6f 62 65 72 20 33 31 0a 2a 2a 0a 2a 2a 20 ctober 31.**.**
79150 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c The author discl
79160 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 aims copyright t
79170 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f o this source co
79180 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 de. In place of
79190 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 .** a legal noti
791a0 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c ce, here is a bl
791b0 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 essing:.**.**
791c0 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 May you do good
791d0 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a and not evil..*
791e0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e * May you fin
791f0 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f d forgiveness fo
79200 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 r yourself and f
79210 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a orgive others..*
79220 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 * May you sha
79230 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 re freely, never
79240 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 taking more tha
79250 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a n you give..**.*
79260 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
79270 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
79280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
79290 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
792a0 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 ********.** This
792b0 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 file contains t
792c0 68 65 20 43 20 66 75 6e 63 74 69 6f 6e 73 20 74 he C functions t
792d0 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20 64 61 hat implement da
792e0 74 65 20 61 6e 64 20 74 69 6d 65 0a 2a 2a 20 66 te and time.** f
792f0 75 6e 63 74 69 6f 6e 73 20 66 6f 72 20 53 51 4c unctions for SQL
79300 69 74 65 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 65 ite. .**.** The
79310 72 65 20 69 73 20 6f 6e 6c 79 20 6f 6e 65 20 65 re is only one e
79320 78 70 6f 72 74 65 64 20 73 79 6d 62 6f 6c 20 69 xported symbol i
79330 6e 20 74 68 69 73 20 66 69 6c 65 20 2d 20 74 68 n this file - th
79340 65 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 73 71 e function.** sq
79350 6c 69 74 65 33 52 65 67 69 73 74 65 72 44 61 74 lite3RegisterDat
79360 65 54 69 6d 65 46 75 6e 63 74 69 6f 6e 73 28 29 eTimeFunctions()
79370 20 66 6f 75 6e 64 20 61 74 20 74 68 65 20 62 6f found at the bo
79380 74 74 6f 6d 20 6f 66 20 74 68 65 20 66 69 6c 65 ttom of the file
79390 2e 0a 2a 2a 20 41 6c 6c 20 6f 74 68 65 72 20 63 ..** All other c
793a0 6f 64 65 20 68 61 73 20 66 69 6c 65 20 73 63 6f ode has file sco
793b0 70 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 64 pe..**.** $Id: d
793c0 61 74 65 2e 63 2c 76 20 31 2e 31 30 37 20 32 30 ate.c,v 1.107 20
793d0 30 39 2f 30 35 2f 30 33 20 32 30 3a 32 33 3a 35 09/05/03 20:23:5
793e0 33 20 64 72 68 20 45 78 70 20 24 0a 2a 2a 0a 2a 3 drh Exp $.**.*
793f0 2a 20 53 51 4c 69 74 65 20 70 72 6f 63 65 73 73 * SQLite process
79400 65 73 20 61 6c 6c 20 74 69 6d 65 73 20 61 6e 64 es all times and
79410 20 64 61 74 65 73 20 61 73 20 4a 75 6c 69 61 6e dates as Julian
79420 20 44 61 79 20 6e 75 6d 62 65 72 73 2e 20 20 54 Day numbers. T
79430 68 65 0a 2a 2a 20 64 61 74 65 73 20 61 6e 64 20 he.** dates and
79440 74 69 6d 65 73 20 61 72 65 20 73 74 6f 72 65 64 times are stored
79450 20 61 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f as the number o
79460 66 20 64 61 79 73 20 73 69 6e 63 65 20 6e 6f 6f f days since noo
79470 6e 0a 2a 2a 20 69 6e 20 47 72 65 65 6e 77 69 63 n.** in Greenwic
79480 68 20 6f 6e 20 4e 6f 76 65 6d 62 65 72 20 32 34 h on November 24
79490 2c 20 34 37 31 34 20 42 2e 43 2e 20 61 63 63 6f , 4714 B.C. acco
794a0 72 64 69 6e 67 20 74 6f 20 74 68 65 20 47 72 65 rding to the Gre
794b0 67 6f 72 69 61 6e 0a 2a 2a 20 63 61 6c 65 6e 64 gorian.** calend
794c0 61 72 20 73 79 73 74 65 6d 2e 20 0a 2a 2a 0a 2a ar system. .**.*
794d0 2a 20 31 39 37 30 2d 30 31 2d 30 31 20 30 30 3a * 1970-01-01 00:
794e0 30 30 3a 30 30 20 69 73 20 4a 44 20 32 34 34 30 00:00 is JD 2440
794f0 35 38 37 2e 35 0a 2a 2a 20 32 30 30 30 2d 30 31 587.5.** 2000-01
79500 2d 30 31 20 30 30 3a 30 30 3a 30 30 20 69 73 20 -01 00:00:00 is
79510 4a 44 20 32 34 35 31 35 34 34 2e 35 0a 2a 2a 0a JD 2451544.5.**.
79520 2a 2a 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e ** This implemen
79530 74 69 6f 6e 20 72 65 71 75 69 72 65 73 20 79 65 tion requires ye
79540 61 72 73 20 74 6f 20 62 65 20 65 78 70 72 65 73 ars to be expres
79550 73 65 64 20 61 73 20 61 20 34 2d 64 69 67 69 74 sed as a 4-digit
79560 20 6e 75 6d 62 65 72 0a 2a 2a 20 77 68 69 63 68 number.** which
79570 20 6d 65 61 6e 73 20 74 68 61 74 20 6f 6e 6c 79 means that only
79580 20 64 61 74 65 73 20 62 65 74 77 65 65 6e 20 30 dates between 0
79590 30 30 30 2d 30 31 2d 30 31 20 61 6e 64 20 39 39 000-01-01 and 99
795a0 39 39 2d 31 32 2d 33 31 20 63 61 6e 0a 2a 2a 20 99-12-31 can.**
795b0 62 65 20 72 65 70 72 65 73 65 6e 74 65 64 2c 20 be represented,
795c0 65 76 65 6e 20 74 68 6f 75 67 68 20 6a 75 6c 69 even though juli
795d0 61 6e 20 64 61 79 20 6e 75 6d 62 65 72 73 20 61 an day numbers a
795e0 6c 6c 6f 77 20 61 20 6d 75 63 68 20 77 69 64 65 llow a much wide
795f0 72 0a 2a 2a 20 72 61 6e 67 65 20 6f 66 20 64 61 r.** range of da
79600 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 47 tes..**.** The G
79610 72 65 67 6f 72 69 61 6e 20 63 61 6c 65 6e 64 61 regorian calenda
79620 72 20 73 79 73 74 65 6d 20 69 73 20 75 73 65 64 r system is used
79630 20 66 6f 72 20 61 6c 6c 20 64 61 74 65 73 20 61 for all dates a
79640 6e 64 20 74 69 6d 65 73 2c 0a 2a 2a 20 65 76 65 nd times,.** eve
79650 6e 20 74 68 6f 73 65 20 74 68 61 74 20 70 72 65 n those that pre
79660 64 61 74 65 20 74 68 65 20 47 72 65 67 6f 72 69 date the Gregori
79670 61 6e 20 63 61 6c 65 6e 64 61 72 2e 20 20 48 69 an calendar. Hi
79680 73 74 6f 72 69 61 6e 73 20 75 73 75 61 6c 6c 79 storians usually
79690 0a 2a 2a 20 75 73 65 20 74 68 65 20 4a 75 6c 69 .** use the Juli
796a0 61 6e 20 63 61 6c 65 6e 64 61 72 20 66 6f 72 20 an calendar for
796b0 64 61 74 65 73 20 70 72 69 6f 72 20 74 6f 20 31 dates prior to 1
796c0 35 38 32 2d 31 30 2d 31 35 20 61 6e 64 20 66 6f 582-10-15 and fo
796d0 72 20 73 6f 6d 65 0a 2a 2a 20 64 61 74 65 73 20 r some.** dates
796e0 61 66 74 65 72 77 61 72 64 73 2c 20 64 65 70 65 afterwards, depe
796f0 6e 64 69 6e 67 20 6f 6e 20 6c 6f 63 61 6c 65 2e nding on locale.
79700 20 20 42 65 77 61 72 65 20 6f 66 20 74 68 69 73 Beware of this
79710 20 64 69 66 66 65 72 65 6e 63 65 2e 0a 2a 2a 0a difference..**.
79720 2a 2a 20 54 68 65 20 63 6f 6e 76 65 72 73 69 6f ** The conversio
79730 6e 20 61 6c 67 6f 72 69 74 68 6d 73 20 61 72 65 n algorithms are
79740 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 62 61 73 implemented bas
79750 65 64 20 6f 6e 20 64 65 73 63 72 69 70 74 69 6f ed on descriptio
79760 6e 73 0a 2a 2a 20 69 6e 20 74 68 65 20 66 6f 6c ns.** in the fol
79770 6c 6f 77 69 6e 67 20 74 65 78 74 3a 0a 2a 2a 0a lowing text:.**.
79780 2a 2a 20 20 20 20 20 20 4a 65 61 6e 20 4d 65 65 ** Jean Mee
79790 75 73 0a 2a 2a 20 20 20 20 20 20 41 73 74 72 6f us.** Astro
797a0 6e 6f 6d 69 63 61 6c 20 41 6c 67 6f 72 69 74 68 nomical Algorith
797b0 6d 73 2c 20 32 6e 64 20 45 64 69 74 69 6f 6e 2c ms, 2nd Edition,
797c0 20 31 39 39 38 0a 2a 2a 20 20 20 20 20 20 49 53 1998.** IS
797d0 42 4d 20 30 2d 39 34 33 33 39 36 2d 36 31 2d 31 BM 0-943396-61-1
797e0 0a 2a 2a 20 20 20 20 20 20 57 69 6c 6c 6d 61 6e .** Willman
797f0 6e 2d 42 65 6c 6c 2c 20 49 6e 63 0a 2a 2a 20 20 n-Bell, Inc.**
79800 20 20 20 20 52 69 63 68 6d 6f 6e 64 2c 20 56 69 Richmond, Vi
79810 72 67 69 6e 69 61 20 28 55 53 41 29 0a 2a 2f 0a rginia (USA).*/.
79820 23 69 6e 63 6c 75 64 65 20 3c 74 69 6d 65 2e 68 #include <time.h
79830 3e 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 >..#ifndef SQLIT
79840 45 5f 4f 4d 49 54 5f 44 41 54 45 54 49 4d 45 5f E_OMIT_DATETIME_
79850 46 55 4e 43 53 0a 0a 2f 2a 0a 2a 2a 20 4f 6e 20 FUNCS../*.** On
79860 72 65 63 65 6e 74 20 57 69 6e 64 6f 77 73 20 70 recent Windows p
79870 6c 61 74 66 6f 72 6d 73 2c 20 74 68 65 20 6c 6f latforms, the lo
79880 63 61 6c 74 69 6d 65 5f 73 28 29 20 66 75 6e 63 caltime_s() func
79890 74 69 6f 6e 20 69 73 20 61 76 61 69 6c 61 62 6c tion is availabl
798a0 65 0a 2a 2a 20 61 73 20 70 61 72 74 20 6f 66 20 e.** as part of
798b0 74 68 65 20 22 53 65 63 75 72 65 20 43 52 54 22 the "Secure CRT"
798c0 2e 20 49 74 20 69 73 20 65 73 73 65 6e 74 69 61 . It is essentia
798d0 6c 6c 79 20 65 71 75 69 76 61 6c 65 6e 74 20 74 lly equivalent t
798e0 6f 20 0a 2a 2a 20 6c 6f 63 61 6c 74 69 6d 65 5f o .** localtime_
798f0 72 28 29 20 61 76 61 69 6c 61 62 6c 65 20 75 6e r() available un
79900 64 65 72 20 6d 6f 73 74 20 50 4f 53 49 58 20 70 der most POSIX p
79910 6c 61 74 66 6f 72 6d 73 2c 20 65 78 63 65 70 74 latforms, except
79920 20 74 68 61 74 20 74 68 65 20 0a 2a 2a 20 6f 72 that the .** or
79930 64 65 72 20 6f 66 20 74 68 65 20 70 61 72 61 6d der of the param
79940 65 74 65 72 73 20 69 73 20 72 65 76 65 72 73 65 eters is reverse
79950 64 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 68 74 74 d..**.** See htt
79960 70 3a 2f 2f 6d 73 64 6e 2e 6d 69 63 72 6f 73 6f p://msdn.microso
79970 66 74 2e 63 6f 6d 2f 65 6e 2d 75 73 2f 6c 69 62 ft.com/en-us/lib
79980 72 61 72 79 2f 61 34 34 32 78 33 79 65 28 56 53 rary/a442x3ye(VS
79990 2e 38 30 29 2e 61 73 70 78 2e 0a 2a 2a 0a 2a 2a .80).aspx..**.**
799a0 20 49 66 20 74 68 65 20 75 73 65 72 20 68 61 73 If the user has
799b0 20 6e 6f 74 20 69 6e 64 69 63 61 74 65 64 20 74 not indicated t
799c0 6f 20 75 73 65 20 6c 6f 63 61 6c 74 69 6d 65 5f o use localtime_
799d0 72 28 29 20 6f 72 20 6c 6f 63 61 6c 74 69 6d 65 r() or localtime
799e0 5f 73 28 29 0a 2a 2a 20 61 6c 72 65 61 64 79 2c _s().** already,
799f0 20 63 68 65 63 6b 20 66 6f 72 20 61 6e 20 4d 53 check for an MS
79a00 56 43 20 62 75 69 6c 64 20 65 6e 76 69 72 6f 6e VC build environ
79a10 6d 65 6e 74 20 74 68 61 74 20 70 72 6f 76 69 64 ment that provid
79a20 65 73 20 0a 2a 2a 20 6c 6f 63 61 6c 74 69 6d 65 es .** localtime
79a30 5f 73 28 29 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 _s()..*/.#if !de
79a40 66 69 6e 65 64 28 48 41 56 45 5f 4c 4f 43 41 4c fined(HAVE_LOCAL
79a50 54 49 4d 45 5f 52 29 20 26 26 20 21 64 65 66 69 TIME_R) && !defi
79a60 6e 65 64 28 48 41 56 45 5f 4c 4f 43 41 4c 54 49 ned(HAVE_LOCALTI
79a70 4d 45 5f 53 29 20 26 26 20 5c 0a 20 20 20 20 20 ME_S) && \.
79a80 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52 defined(_MSC_VER
79a90 29 20 26 26 20 64 65 66 69 6e 65 64 28 5f 43 52 ) && defined(_CR
79aa0 54 5f 49 4e 53 45 43 55 52 45 5f 44 45 50 52 45 T_INSECURE_DEPRE
79ab0 43 41 54 45 29 0a 23 64 65 66 69 6e 65 20 48 41 CATE).#define HA
79ac0 56 45 5f 4c 4f 43 41 4c 54 49 4d 45 5f 53 20 31 VE_LOCALTIME_S 1
79ad0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 .#endif../*.** A
79ae0 20 73 74 72 75 63 74 75 72 65 20 66 6f 72 20 68 structure for h
79af0 6f 6c 64 69 6e 67 20 61 20 73 69 6e 67 6c 65 20 olding a single
79b00 64 61 74 65 20 61 6e 64 20 74 69 6d 65 2e 0a 2a date and time..*
79b10 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 /.typedef struct
79b20 20 44 61 74 65 54 69 6d 65 20 44 61 74 65 54 69 DateTime DateTi
79b30 6d 65 3b 0a 73 74 72 75 63 74 20 44 61 74 65 54 me;.struct DateT
79b40 69 6d 65 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f ime {. sqlite3_
79b50 69 6e 74 36 34 20 69 4a 44 3b 20 2f 2a 20 54 68 int64 iJD; /* Th
79b60 65 20 6a 75 6c 69 61 6e 20 64 61 79 20 6e 75 6d e julian day num
79b70 62 65 72 20 74 69 6d 65 73 20 38 36 34 30 30 30 ber times 864000
79b80 30 30 20 2a 2f 0a 20 20 69 6e 74 20 59 2c 20 4d 00 */. int Y, M
79b90 2c 20 44 3b 20 20 20 20 20 20 20 2f 2a 20 59 65 , D; /* Ye
79ba0 61 72 2c 20 6d 6f 6e 74 68 2c 20 61 6e 64 20 64 ar, month, and d
79bb0 61 79 20 2a 2f 0a 20 20 69 6e 74 20 68 2c 20 6d ay */. int h, m
79bc0 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 6f ; /* Ho
79bd0 75 72 20 61 6e 64 20 6d 69 6e 75 74 65 73 20 2a ur and minutes *
79be0 2f 0a 20 20 69 6e 74 20 74 7a 3b 20 20 20 20 20 /. int tz;
79bf0 20 20 20 20 20 20 20 2f 2a 20 54 69 6d 65 7a 6f /* Timezo
79c00 6e 65 20 6f 66 66 73 65 74 20 69 6e 20 6d 69 6e ne offset in min
79c10 75 74 65 73 20 2a 2f 0a 20 20 64 6f 75 62 6c 65 utes */. double
79c20 20 73 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 s; /*
79c30 53 65 63 6f 6e 64 73 20 2a 2f 0a 20 20 63 68 61 Seconds */. cha
79c40 72 20 76 61 6c 69 64 59 4d 44 3b 20 20 20 20 20 r validYMD;
79c50 2f 2a 20 54 72 75 65 20 28 31 29 20 69 66 20 59 /* True (1) if Y
79c60 2c 4d 2c 44 20 61 72 65 20 76 61 6c 69 64 20 2a ,M,D are valid *
79c70 2f 0a 20 20 63 68 61 72 20 76 61 6c 69 64 48 4d /. char validHM
79c80 53 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20 28 S; /* True (
79c90 31 29 20 69 66 20 68 2c 6d 2c 73 20 61 72 65 20 1) if h,m,s are
79ca0 76 61 6c 69 64 20 2a 2f 0a 20 20 63 68 61 72 20 valid */. char
79cb0 76 61 6c 69 64 4a 44 3b 20 20 20 20 20 20 2f 2a validJD; /*
79cc0 20 54 72 75 65 20 28 31 29 20 69 66 20 69 4a 44 True (1) if iJD
79cd0 20 69 73 20 76 61 6c 69 64 20 2a 2f 0a 20 20 63 is valid */. c
79ce0 68 61 72 20 76 61 6c 69 64 54 5a 3b 20 20 20 20 har validTZ;
79cf0 20 20 2f 2a 20 54 72 75 65 20 28 31 29 20 69 66 /* True (1) if
79d00 20 74 7a 20 69 73 20 76 61 6c 69 64 20 2a 2f 0a tz is valid */.
79d10 7d 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 };.../*.** Conve
79d20 72 74 20 7a 44 61 74 65 20 69 6e 74 6f 20 6f 6e rt zDate into on
79d30 65 20 6f 72 20 6d 6f 72 65 20 69 6e 74 65 67 65 e or more intege
79d40 72 73 2e 20 20 41 64 64 69 74 69 6f 6e 61 6c 20 rs. Additional
79d50 61 72 67 75 6d 65 6e 74 73 0a 2a 2a 20 63 6f 6d arguments.** com
79d60 65 20 69 6e 20 67 72 6f 75 70 73 20 6f 66 20 35 e in groups of 5
79d70 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a as follows:.**.
79d80 2a 2a 20 20 20 20 20 20 20 4e 20 20 20 20 20 20 ** N
79d90 20 6e 75 6d 62 65 72 20 6f 66 20 64 69 67 69 74 number of digit
79da0 73 20 69 6e 20 74 68 65 20 69 6e 74 65 67 65 72 s in the integer
79db0 0a 2a 2a 20 20 20 20 20 20 20 6d 69 6e 20 20 20 .** min
79dc0 20 20 6d 69 6e 69 6d 75 6d 20 61 6c 6c 6f 77 65 minimum allowe
79dd0 64 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 69 d value of the i
79de0 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 20 nteger.**
79df0 6d 61 78 20 20 20 20 20 6d 61 78 69 6d 75 6d 20 max maximum
79e00 61 6c 6c 6f 77 65 64 20 76 61 6c 75 65 20 6f 66 allowed value of
79e10 20 74 68 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20 the integer.**
79e20 20 20 20 20 20 20 6e 65 78 74 43 20 20 20 66 69 nextC fi
79e30 72 73 74 20 63 68 61 72 61 63 74 65 72 20 61 66 rst character af
79e40 74 65 72 20 74 68 65 20 69 6e 74 65 67 65 72 0a ter the integer.
79e50 2a 2a 20 20 20 20 20 20 20 70 56 61 6c 20 20 20 ** pVal
79e60 20 77 68 65 72 65 20 74 6f 20 77 72 69 74 65 20 where to write
79e70 74 68 65 20 69 6e 74 65 67 65 72 73 20 76 61 6c the integers val
79e80 75 65 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6e 76 65 72 ue..**.** Conver
79e90 73 69 6f 6e 73 20 63 6f 6e 74 69 6e 75 65 20 75 sions continue u
79ea0 6e 74 69 6c 20 6f 6e 65 20 77 69 74 68 20 6e 65 ntil one with ne
79eb0 78 74 43 3d 3d 30 20 69 73 20 65 6e 63 6f 75 6e xtC==0 is encoun
79ec0 74 65 72 65 64 2e 0a 2a 2a 20 54 68 65 20 66 75 tered..** The fu
79ed0 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 nction returns t
79ee0 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 75 63 he number of suc
79ef0 63 65 73 73 66 75 6c 20 63 6f 6e 76 65 72 73 69 cessful conversi
79f00 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ons..*/.static i
79f10 6e 74 20 67 65 74 44 69 67 69 74 73 28 63 6f 6e nt getDigits(con
79f20 73 74 20 63 68 61 72 20 2a 7a 44 61 74 65 2c 20 st char *zDate,
79f30 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 ...){. va_list
79f40 61 70 3b 0a 20 20 69 6e 74 20 76 61 6c 3b 0a 20 ap;. int val;.
79f50 20 69 6e 74 20 4e 3b 0a 20 20 69 6e 74 20 6d 69 int N;. int mi
79f60 6e 3b 0a 20 20 69 6e 74 20 6d 61 78 3b 0a 20 20 n;. int max;.
79f70 69 6e 74 20 6e 65 78 74 43 3b 0a 20 20 69 6e 74 int nextC;. int
79f80 20 2a 70 56 61 6c 3b 0a 20 20 69 6e 74 20 63 6e *pVal;. int cn
79f90 74 20 3d 20 30 3b 0a 20 20 76 61 5f 73 74 61 72 t = 0;. va_star
79fa0 74 28 61 70 2c 20 7a 44 61 74 65 29 3b 0a 20 20 t(ap, zDate);.
79fb0 64 6f 7b 0a 20 20 20 20 4e 20 3d 20 76 61 5f 61 do{. N = va_a
79fc0 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20 rg(ap, int);.
79fd0 20 6d 69 6e 20 3d 20 76 61 5f 61 72 67 28 61 70 min = va_arg(ap
79fe0 2c 20 69 6e 74 29 3b 0a 20 20 20 20 6d 61 78 20 , int);. max
79ff0 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 = va_arg(ap, int
7a000 29 3b 0a 20 20 20 20 6e 65 78 74 43 20 3d 20 76 );. nextC = v
7a010 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a a_arg(ap, int);.
7a020 20 20 20 20 70 56 61 6c 20 3d 20 76 61 5f 61 72 pVal = va_ar
7a030 67 28 61 70 2c 20 69 6e 74 2a 29 3b 0a 20 20 20 g(ap, int*);.
7a040 20 76 61 6c 20 3d 20 30 3b 0a 20 20 20 20 77 68 val = 0;. wh
7a050 69 6c 65 28 20 4e 2d 2d 20 29 7b 0a 20 20 20 20 ile( N-- ){.
7a060 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 49 73 if( !sqlite3Is
7a070 64 69 67 69 74 28 2a 7a 44 61 74 65 29 20 29 7b digit(*zDate) ){
7a080 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e . goto en
7a090 64 5f 67 65 74 44 69 67 69 74 73 3b 0a 20 20 20 d_getDigits;.
7a0a0 20 20 20 7d 0a 20 20 20 20 20 20 76 61 6c 20 3d }. val =
7a0b0 20 76 61 6c 2a 31 30 20 2b 20 2a 7a 44 61 74 65 val*10 + *zDate
7a0c0 20 2d 20 27 30 27 3b 0a 20 20 20 20 20 20 7a 44 - '0';. zD
7a0d0 61 74 65 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 ate++;. }.
7a0e0 20 69 66 28 20 76 61 6c 3c 6d 69 6e 20 7c 7c 20 if( val<min ||
7a0f0 76 61 6c 3e 6d 61 78 20 7c 7c 20 28 6e 65 78 74 val>max || (next
7a100 43 21 3d 30 20 26 26 20 6e 65 78 74 43 21 3d 2a C!=0 && nextC!=*
7a110 7a 44 61 74 65 29 20 29 7b 0a 20 20 20 20 20 20 zDate) ){.
7a120 67 6f 74 6f 20 65 6e 64 5f 67 65 74 44 69 67 69 goto end_getDigi
7a130 74 73 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a 70 ts;. }. *p
7a140 56 61 6c 20 3d 20 76 61 6c 3b 0a 20 20 20 20 7a Val = val;. z
7a150 44 61 74 65 2b 2b 3b 0a 20 20 20 20 63 6e 74 2b Date++;. cnt+
7a160 2b 3b 0a 20 20 7d 77 68 69 6c 65 28 20 6e 65 78 +;. }while( nex
7a170 74 43 20 29 3b 0a 65 6e 64 5f 67 65 74 44 69 67 tC );.end_getDig
7a180 69 74 73 3a 0a 20 20 76 61 5f 65 6e 64 28 61 70 its:. va_end(ap
7a190 29 3b 0a 20 20 72 65 74 75 72 6e 20 63 6e 74 3b );. return cnt;
7a1a0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 74 .}../*.** Read t
7a1b0 65 78 74 20 66 72 6f 6d 20 7a 5b 5d 20 61 6e 64 ext from z[] and
7a1c0 20 63 6f 6e 76 65 72 74 20 69 6e 74 6f 20 61 20 convert into a
7a1d0 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 6e floating point n
7a1e0 75 6d 62 65 72 2e 20 20 52 65 74 75 72 6e 0a 2a umber. Return.*
7a1f0 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 * the number of
7a200 64 69 67 69 74 73 20 63 6f 6e 76 65 72 74 65 64 digits converted
7a210 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 67 65 74 ..*/.#define get
7a220 56 61 6c 75 65 20 73 71 6c 69 74 65 33 41 74 6f Value sqlite3Ato
7a230 46 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 73 65 20 61 F../*.** Parse a
7a240 20 74 69 6d 65 7a 6f 6e 65 20 65 78 74 65 6e 73 timezone extens
7a250 69 6f 6e 20 6f 6e 20 74 68 65 20 65 6e 64 20 6f ion on the end o
7a260 66 20 61 20 64 61 74 65 2d 74 69 6d 65 2e 0a 2a f a date-time..*
7a270 2a 20 54 68 65 20 65 78 74 65 6e 73 69 6f 6e 20 * The extension
7a280 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 3a 0a is of the form:.
7a290 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 28 2b 2f **.** (+/
7a2a0 2d 29 48 48 3a 4d 4d 0a 2a 2a 0a 2a 2a 20 4f 72 -)HH:MM.**.** Or
7a2b0 20 74 68 65 20 22 7a 75 6c 75 22 20 6e 6f 74 61 the "zulu" nota
7a2c0 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 tion:.**.**
7a2d0 20 20 20 5a 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 Z.**.** If th
7a2e0 65 20 70 61 72 73 65 20 69 73 20 73 75 63 63 65 e parse is succe
7a2f0 73 73 66 75 6c 2c 20 77 72 69 74 65 20 74 68 65 ssful, write the
7a300 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 6e 75 74 number of minut
7a310 65 73 0a 2a 2a 20 6f 66 20 63 68 61 6e 67 65 20 es.** of change
7a320 69 6e 20 70 2d 3e 74 7a 20 61 6e 64 20 72 65 74 in p->tz and ret
7a330 75 72 6e 20 30 2e 20 20 49 66 20 61 20 70 61 72 urn 0. If a par
7a340 73 65 72 20 65 72 72 6f 72 20 6f 63 63 75 72 73 ser error occurs
7a350 2c 0a 2a 2a 20 72 65 74 75 72 6e 20 6e 6f 6e 2d ,.** return non-
7a360 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 41 20 6d 69 zero..**.** A mi
7a370 73 73 69 6e 67 20 73 70 65 63 69 66 69 65 72 20 ssing specifier
7a380 69 73 20 6e 6f 74 20 63 6f 6e 73 69 64 65 72 65 is not considere
7a390 64 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f 0a 73 d an error..*/.s
7a3a0 74 61 74 69 63 20 69 6e 74 20 70 61 72 73 65 54 tatic int parseT
7a3b0 69 6d 65 7a 6f 6e 65 28 63 6f 6e 73 74 20 63 68 imezone(const ch
7a3c0 61 72 20 2a 7a 44 61 74 65 2c 20 44 61 74 65 54 ar *zDate, DateT
7a3d0 69 6d 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 73 ime *p){. int s
7a3e0 67 6e 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 48 gn = 0;. int nH
7a3f0 72 2c 20 6e 4d 6e 3b 0a 20 20 69 6e 74 20 63 3b r, nMn;. int c;
7a400 0a 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 . while( sqlite
7a410 33 49 73 73 70 61 63 65 28 2a 7a 44 61 74 65 29 3Isspace(*zDate)
7a420 20 29 7b 20 7a 44 61 74 65 2b 2b 3b 20 7d 0a 20 ){ zDate++; }.
7a430 20 70 2d 3e 74 7a 20 3d 20 30 3b 0a 20 20 63 20 p->tz = 0;. c
7a440 3d 20 2a 7a 44 61 74 65 3b 0a 20 20 69 66 28 20 = *zDate;. if(
7a450 63 3d 3d 27 2d 27 20 29 7b 0a 20 20 20 20 73 67 c=='-' ){. sg
7a460 6e 20 3d 20 2d 31 3b 0a 20 20 7d 65 6c 73 65 20 n = -1;. }else
7a470 69 66 28 20 63 3d 3d 27 2b 27 20 29 7b 0a 20 20 if( c=='+' ){.
7a480 20 20 73 67 6e 20 3d 20 2b 31 3b 0a 20 20 7d 65 sgn = +1;. }e
7a490 6c 73 65 20 69 66 28 20 63 3d 3d 27 5a 27 20 7c lse if( c=='Z' |
7a4a0 7c 20 63 3d 3d 27 7a 27 20 29 7b 0a 20 20 20 20 | c=='z' ){.
7a4b0 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 20 67 6f 74 zDate++;. got
7a4c0 6f 20 7a 75 6c 75 5f 74 69 6d 65 3b 0a 20 20 7d o zulu_time;. }
7a4d0 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e else{. return
7a4e0 20 63 21 3d 30 3b 0a 20 20 7d 0a 20 20 7a 44 61 c!=0;. }. zDa
7a4f0 74 65 2b 2b 3b 0a 20 20 69 66 28 20 67 65 74 44 te++;. if( getD
7a500 69 67 69 74 73 28 7a 44 61 74 65 2c 20 32 2c 20 igits(zDate, 2,
7a510 30 2c 20 31 34 2c 20 27 3a 27 2c 20 26 6e 48 72 0, 14, ':', &nHr
7a520 2c 20 32 2c 20 30 2c 20 35 39 2c 20 30 2c 20 26 , 2, 0, 59, 0, &
7a530 6e 4d 6e 29 21 3d 32 20 29 7b 0a 20 20 20 20 72 nMn)!=2 ){. r
7a540 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 7a eturn 1;. }. z
7a550 44 61 74 65 20 2b 3d 20 35 3b 0a 20 20 70 2d 3e Date += 5;. p->
7a560 74 7a 20 3d 20 73 67 6e 2a 28 6e 4d 6e 20 2b 20 tz = sgn*(nMn +
7a570 6e 48 72 2a 36 30 29 3b 0a 7a 75 6c 75 5f 74 69 nHr*60);.zulu_ti
7a580 6d 65 3a 0a 20 20 77 68 69 6c 65 28 20 73 71 6c me:. while( sql
7a590 69 74 65 33 49 73 73 70 61 63 65 28 2a 7a 44 61 ite3Isspace(*zDa
7a5a0 74 65 29 20 29 7b 20 7a 44 61 74 65 2b 2b 3b 20 te) ){ zDate++;
7a5b0 7d 0a 20 20 72 65 74 75 72 6e 20 2a 7a 44 61 74 }. return *zDat
7a5c0 65 21 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 e!=0;.}../*.** P
7a5d0 61 72 73 65 20 74 69 6d 65 73 20 6f 66 20 74 68 arse times of th
7a5e0 65 20 66 6f 72 6d 20 48 48 3a 4d 4d 20 6f 72 20 e form HH:MM or
7a5f0 48 48 3a 4d 4d 3a 53 53 20 6f 72 20 48 48 3a 4d HH:MM:SS or HH:M
7a600 4d 3a 53 53 2e 46 46 46 46 2e 0a 2a 2a 20 54 68 M:SS.FFFF..** Th
7a610 65 20 48 48 2c 20 4d 4d 2c 20 61 6e 64 20 53 53 e HH, MM, and SS
7a620 20 6d 75 73 74 20 65 61 63 68 20 62 65 20 65 78 must each be ex
7a630 61 63 74 6c 79 20 32 20 64 69 67 69 74 73 2e 20 actly 2 digits.
7a640 20 54 68 65 0a 2a 2a 20 66 72 61 63 74 69 6f 6e The.** fraction
7a650 61 6c 20 73 65 63 6f 6e 64 73 20 46 46 46 46 20 al seconds FFFF
7a660 63 61 6e 20 62 65 20 6f 6e 65 20 6f 72 20 6d 6f can be one or mo
7a670 72 65 20 64 69 67 69 74 73 2e 0a 2a 2a 0a 2a 2a re digits..**.**
7a680 20 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 Return 1 if the
7a690 72 65 20 69 73 20 61 20 70 61 72 73 69 6e 67 20 re is a parsing
7a6a0 65 72 72 6f 72 20 61 6e 64 20 30 20 6f 6e 20 73 error and 0 on s
7a6b0 75 63 63 65 73 73 2e 0a 2a 2f 0a 73 74 61 74 69 uccess..*/.stati
7a6c0 63 20 69 6e 74 20 70 61 72 73 65 48 68 4d 6d 53 c int parseHhMmS
7a6d0 73 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 s(const char *zD
7a6e0 61 74 65 2c 20 44 61 74 65 54 69 6d 65 20 2a 70 ate, DateTime *p
7a6f0 29 7b 0a 20 20 69 6e 74 20 68 2c 20 6d 2c 20 73 ){. int h, m, s
7a700 3b 0a 20 20 64 6f 75 62 6c 65 20 6d 73 20 3d 20 ;. double ms =
7a710 30 2e 30 3b 0a 20 20 69 66 28 20 67 65 74 44 69 0.0;. if( getDi
7a720 67 69 74 73 28 7a 44 61 74 65 2c 20 32 2c 20 30 gits(zDate, 2, 0
7a730 2c 20 32 34 2c 20 27 3a 27 2c 20 26 68 2c 20 32 , 24, ':', &h, 2
7a740 2c 20 30 2c 20 35 39 2c 20 30 2c 20 26 6d 29 21 , 0, 59, 0, &m)!
7a750 3d 32 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e =2 ){. return
7a760 20 31 3b 0a 20 20 7d 0a 20 20 7a 44 61 74 65 20 1;. }. zDate
7a770 2b 3d 20 35 3b 0a 20 20 69 66 28 20 2a 7a 44 61 += 5;. if( *zDa
7a780 74 65 3d 3d 27 3a 27 20 29 7b 0a 20 20 20 20 7a te==':' ){. z
7a790 44 61 74 65 2b 2b 3b 0a 20 20 20 20 69 66 28 20 Date++;. if(
7a7a0 67 65 74 44 69 67 69 74 73 28 7a 44 61 74 65 2c getDigits(zDate,
7a7b0 20 32 2c 20 30 2c 20 35 39 2c 20 30 2c 20 26 73 2, 0, 59, 0, &s
7a7c0 29 21 3d 31 20 29 7b 0a 20 20 20 20 20 20 72 65 )!=1 ){. re
7a7d0 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 turn 1;. }.
7a7e0 20 20 7a 44 61 74 65 20 2b 3d 20 32 3b 0a 20 20 zDate += 2;.
7a7f0 20 20 69 66 28 20 2a 7a 44 61 74 65 3d 3d 27 2e if( *zDate=='.
7a800 27 20 26 26 20 73 71 6c 69 74 65 33 49 73 64 69 ' && sqlite3Isdi
7a810 67 69 74 28 7a 44 61 74 65 5b 31 5d 29 20 29 7b git(zDate[1]) ){
7a820 0a 20 20 20 20 20 20 64 6f 75 62 6c 65 20 72 53 . double rS
7a830 63 61 6c 65 20 3d 20 31 2e 30 3b 0a 20 20 20 20 cale = 1.0;.
7a840 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 20 20 zDate++;.
7a850 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49 while( sqlite3I
7a860 73 64 69 67 69 74 28 2a 7a 44 61 74 65 29 20 29 sdigit(*zDate) )
7a870 7b 0a 20 20 20 20 20 20 20 20 6d 73 20 3d 20 6d {. ms = m
7a880 73 2a 31 30 2e 30 20 2b 20 2a 7a 44 61 74 65 20 s*10.0 + *zDate
7a890 2d 20 27 30 27 3b 0a 20 20 20 20 20 20 20 20 72 - '0';. r
7a8a0 53 63 61 6c 65 20 2a 3d 20 31 30 2e 30 3b 0a 20 Scale *= 10.0;.
7a8b0 20 20 20 20 20 20 20 7a 44 61 74 65 2b 2b 3b 0a zDate++;.
7a8c0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6d 73 }. ms
7a8d0 20 2f 3d 20 72 53 63 61 6c 65 3b 0a 20 20 20 20 /= rScale;.
7a8e0 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 }. }else{. s
7a8f0 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 2d 3e 76 = 0;. }. p->v
7a900 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20 70 2d alidJD = 0;. p-
7a910 3e 76 61 6c 69 64 48 4d 53 20 3d 20 31 3b 0a 20 >validHMS = 1;.
7a920 20 70 2d 3e 68 20 3d 20 68 3b 0a 20 20 70 2d 3e p->h = h;. p->
7a930 6d 20 3d 20 6d 3b 0a 20 20 70 2d 3e 73 20 3d 20 m = m;. p->s =
7a940 73 20 2b 20 6d 73 3b 0a 20 20 69 66 28 20 70 61 s + ms;. if( pa
7a950 72 73 65 54 69 6d 65 7a 6f 6e 65 28 7a 44 61 74 rseTimezone(zDat
7a960 65 2c 20 70 29 20 29 20 72 65 74 75 72 6e 20 31 e, p) ) return 1
7a970 3b 0a 20 20 70 2d 3e 76 61 6c 69 64 54 5a 20 3d ;. p->validTZ =
7a980 20 28 70 2d 3e 74 7a 21 3d 30 29 3f 31 3a 30 3b (p->tz!=0)?1:0;
7a990 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a . return 0;.}..
7a9a0 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 66 72 /*.** Convert fr
7a9b0 6f 6d 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48 om YYYY-MM-DD HH
7a9c0 3a 4d 4d 3a 53 53 20 74 6f 20 6a 75 6c 69 61 6e :MM:SS to julian
7a9d0 20 64 61 79 2e 20 20 57 65 20 61 6c 77 61 79 73 day. We always
7a9e0 20 61 73 73 75 6d 65 0a 2a 2a 20 74 68 61 74 20 assume.** that
7a9f0 74 68 65 20 59 59 59 59 2d 4d 4d 2d 44 44 20 69 the YYYY-MM-DD i
7aa00 73 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 s according to t
7aa10 68 65 20 47 72 65 67 6f 72 69 61 6e 20 63 61 6c he Gregorian cal
7aa20 65 6e 64 61 72 2e 0a 2a 2a 0a 2a 2a 20 52 65 66 endar..**.** Ref
7aa30 65 72 65 6e 63 65 3a 20 20 4d 65 65 75 73 20 70 erence: Meeus p
7aa40 61 67 65 20 36 31 0a 2a 2f 0a 73 74 61 74 69 63 age 61.*/.static
7aa50 20 76 6f 69 64 20 63 6f 6d 70 75 74 65 4a 44 28 void computeJD(
7aa60 44 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 DateTime *p){.
7aa70 69 6e 74 20 59 2c 20 4d 2c 20 44 2c 20 41 2c 20 int Y, M, D, A,
7aa80 42 2c 20 58 31 2c 20 58 32 3b 0a 0a 20 20 69 66 B, X1, X2;.. if
7aa90 28 20 70 2d 3e 76 61 6c 69 64 4a 44 20 29 20 72 ( p->validJD ) r
7aaa0 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 70 2d 3e eturn;. if( p->
7aab0 76 61 6c 69 64 59 4d 44 20 29 7b 0a 20 20 20 20 validYMD ){.
7aac0 59 20 3d 20 70 2d 3e 59 3b 0a 20 20 20 20 4d 20 Y = p->Y;. M
7aad0 3d 20 70 2d 3e 4d 3b 0a 20 20 20 20 44 20 3d 20 = p->M;. D =
7aae0 70 2d 3e 44 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 p->D;. }else{.
7aaf0 20 20 20 59 20 3d 20 32 30 30 30 3b 20 20 2f 2a Y = 2000; /*
7ab00 20 49 66 20 6e 6f 20 59 4d 44 20 73 70 65 63 69 If no YMD speci
7ab10 66 69 65 64 2c 20 61 73 73 75 6d 65 20 32 30 30 fied, assume 200
7ab20 30 2d 4a 61 6e 2d 30 31 20 2a 2f 0a 20 20 20 20 0-Jan-01 */.
7ab30 4d 20 3d 20 31 3b 0a 20 20 20 20 44 20 3d 20 31 M = 1;. D = 1
7ab40 3b 0a 20 20 7d 0a 20 20 69 66 28 20 4d 3c 3d 32 ;. }. if( M<=2
7ab50 20 29 7b 0a 20 20 20 20 59 2d 2d 3b 0a 20 20 20 ){. Y--;.
7ab60 20 4d 20 2b 3d 20 31 32 3b 0a 20 20 7d 0a 20 20 M += 12;. }.
7ab70 41 20 3d 20 59 2f 31 30 30 3b 0a 20 20 42 20 3d A = Y/100;. B =
7ab80 20 32 20 2d 20 41 20 2b 20 28 41 2f 34 29 3b 0a 2 - A + (A/4);.
7ab90 20 20 58 31 20 3d 20 33 36 35 32 35 2a 28 59 2b X1 = 36525*(Y+
7aba0 34 37 31 36 29 2f 31 30 30 3b 0a 20 20 58 32 20 4716)/100;. X2
7abb0 3d 20 33 30 36 30 30 31 2a 28 4d 2b 31 29 2f 31 = 306001*(M+1)/1
7abc0 30 30 30 30 3b 0a 20 20 70 2d 3e 69 4a 44 20 3d 0000;. p->iJD =
7abd0 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 (sqlite3_int64)
7abe0 28 28 58 31 20 2b 20 58 32 20 2b 20 44 20 2b 20 ((X1 + X2 + D +
7abf0 42 20 2d 20 31 35 32 34 2e 35 20 29 20 2a 20 38 B - 1524.5 ) * 8
7ac00 36 34 30 30 30 30 30 29 3b 0a 20 20 70 2d 3e 76 6400000);. p->v
7ac10 61 6c 69 64 4a 44 20 3d 20 31 3b 0a 20 20 69 66 alidJD = 1;. if
7ac20 28 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20 29 7b ( p->validHMS ){
7ac30 0a 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 70 . p->iJD += p
7ac40 2d 3e 68 2a 33 36 30 30 30 30 30 20 2b 20 70 2d ->h*3600000 + p-
7ac50 3e 6d 2a 36 30 30 30 30 20 2b 20 28 73 71 6c 69 >m*60000 + (sqli
7ac60 74 65 33 5f 69 6e 74 36 34 29 28 70 2d 3e 73 2a te3_int64)(p->s*
7ac70 31 30 30 30 29 3b 0a 20 20 20 20 69 66 28 20 70 1000);. if( p
7ac80 2d 3e 76 61 6c 69 64 54 5a 20 29 7b 0a 20 20 20 ->validTZ ){.
7ac90 20 20 20 70 2d 3e 69 4a 44 20 2d 3d 20 70 2d 3e p->iJD -= p->
7aca0 74 7a 2a 36 30 30 30 30 3b 0a 20 20 20 20 20 20 tz*60000;.
7acb0 70 2d 3e 76 61 6c 69 64 59 4d 44 20 3d 20 30 3b p->validYMD = 0;
7acc0 0a 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 48 . p->validH
7acd0 4d 53 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d MS = 0;. p-
7ace0 3e 76 61 6c 69 64 54 5a 20 3d 20 30 3b 0a 20 20 >validTZ = 0;.
7acf0 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a }. }.}../*.**
7ad00 20 50 61 72 73 65 20 64 61 74 65 73 20 6f 66 20 Parse dates of
7ad10 74 68 65 20 66 6f 72 6d 0a 2a 2a 0a 2a 2a 20 20 the form.**.**
7ad20 20 20 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48 YYYY-MM-DD HH
7ad30 3a 4d 4d 3a 53 53 2e 46 46 46 0a 2a 2a 20 20 20 :MM:SS.FFF.**
7ad40 20 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48 3a YYYY-MM-DD HH:
7ad50 4d 4d 3a 53 53 0a 2a 2a 20 20 20 20 20 59 59 59 MM:SS.** YYY
7ad60 59 2d 4d 4d 2d 44 44 20 48 48 3a 4d 4d 0a 2a 2a Y-MM-DD HH:MM.**
7ad70 20 20 20 20 20 59 59 59 59 2d 4d 4d 2d 44 44 0a YYYY-MM-DD.
7ad80 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20 **.** Write the
7ad90 72 65 73 75 6c 74 20 69 6e 74 6f 20 74 68 65 20 result into the
7ada0 44 61 74 65 54 69 6d 65 20 73 74 72 75 63 74 75 DateTime structu
7adb0 72 65 20 61 6e 64 20 72 65 74 75 72 6e 20 30 0a re and return 0.
7adc0 2a 2a 20 6f 6e 20 73 75 63 63 65 73 73 20 61 6e ** on success an
7add0 64 20 31 20 69 66 20 74 68 65 20 69 6e 70 75 74 d 1 if the input
7ade0 20 73 74 72 69 6e 67 20 69 73 20 6e 6f 74 20 61 string is not a
7adf0 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 0a 2a 2a 20 well-formed.**
7ae00 64 61 74 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 date..*/.static
7ae10 69 6e 74 20 70 61 72 73 65 59 79 79 79 4d 6d 44 int parseYyyyMmD
7ae20 64 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 d(const char *zD
7ae30 61 74 65 2c 20 44 61 74 65 54 69 6d 65 20 2a 70 ate, DateTime *p
7ae40 29 7b 0a 20 20 69 6e 74 20 59 2c 20 4d 2c 20 44 ){. int Y, M, D
7ae50 2c 20 6e 65 67 3b 0a 0a 20 20 69 66 28 20 7a 44 , neg;.. if( zD
7ae60 61 74 65 5b 30 5d 3d 3d 27 2d 27 20 29 7b 0a 20 ate[0]=='-' ){.
7ae70 20 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 20 zDate++;.
7ae80 6e 65 67 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 neg = 1;. }else
7ae90 7b 0a 20 20 20 20 6e 65 67 20 3d 20 30 3b 0a 20 {. neg = 0;.
7aea0 20 7d 0a 20 20 69 66 28 20 67 65 74 44 69 67 69 }. if( getDigi
7aeb0 74 73 28 7a 44 61 74 65 2c 34 2c 30 2c 39 39 39 ts(zDate,4,0,999
7aec0 39 2c 27 2d 27 2c 26 59 2c 32 2c 31 2c 31 32 2c 9,'-',&Y,2,1,12,
7aed0 27 2d 27 2c 26 4d 2c 32 2c 31 2c 33 31 2c 30 2c '-',&M,2,1,31,0,
7aee0 26 44 29 21 3d 33 20 29 7b 0a 20 20 20 20 72 65 &D)!=3 ){. re
7aef0 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 7a 44 turn 1;. }. zD
7af00 61 74 65 20 2b 3d 20 31 30 3b 0a 20 20 77 68 69 ate += 10;. whi
7af10 6c 65 28 20 73 71 6c 69 74 65 33 49 73 73 70 61 le( sqlite3Isspa
7af20 63 65 28 2a 7a 44 61 74 65 29 20 7c 7c 20 27 54 ce(*zDate) || 'T
7af30 27 3d 3d 2a 28 75 38 2a 29 7a 44 61 74 65 20 29 '==*(u8*)zDate )
7af40 7b 20 7a 44 61 74 65 2b 2b 3b 20 7d 0a 20 20 69 { zDate++; }. i
7af50 66 28 20 70 61 72 73 65 48 68 4d 6d 53 73 28 7a f( parseHhMmSs(z
7af60 44 61 74 65 2c 20 70 29 3d 3d 30 20 29 7b 0a 20 Date, p)==0 ){.
7af70 20 20 20 2f 2a 20 57 65 20 67 6f 74 20 74 68 65 /* We got the
7af80 20 74 69 6d 65 20 2a 2f 0a 20 20 7d 65 6c 73 65 time */. }else
7af90 20 69 66 28 20 2a 7a 44 61 74 65 3d 3d 30 20 29 if( *zDate==0 )
7afa0 7b 0a 20 20 20 20 70 2d 3e 76 61 6c 69 64 48 4d {. p->validHM
7afb0 53 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a S = 0;. }else{.
7afc0 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 return 1;.
7afd0 7d 0a 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d }. p->validJD =
7afe0 20 30 3b 0a 20 20 70 2d 3e 76 61 6c 69 64 59 4d 0;. p->validYM
7aff0 44 20 3d 20 31 3b 0a 20 20 70 2d 3e 59 20 3d 20 D = 1;. p->Y =
7b000 6e 65 67 20 3f 20 2d 59 20 3a 20 59 3b 0a 20 20 neg ? -Y : Y;.
7b010 70 2d 3e 4d 20 3d 20 4d 3b 0a 20 20 70 2d 3e 44 p->M = M;. p->D
7b020 20 3d 20 44 3b 0a 20 20 69 66 28 20 70 2d 3e 76 = D;. if( p->v
7b030 61 6c 69 64 54 5a 20 29 7b 0a 20 20 20 20 63 6f alidTZ ){. co
7b040 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20 20 7d 0a mputeJD(p);. }.
7b050 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f return 0;.}../
7b060 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 74 69 6d *.** Set the tim
7b070 65 20 74 6f 20 74 68 65 20 63 75 72 72 65 6e 74 e to the current
7b080 20 74 69 6d 65 20 72 65 70 6f 72 74 65 64 20 62 time reported b
7b090 79 20 74 68 65 20 56 46 53 0a 2a 2f 0a 73 74 61 y the VFS.*/.sta
7b0a0 74 69 63 20 76 6f 69 64 20 73 65 74 44 61 74 65 tic void setDate
7b0b0 54 69 6d 65 54 6f 43 75 72 72 65 6e 74 28 73 71 TimeToCurrent(sq
7b0c0 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 lite3_context *c
7b0d0 6f 6e 74 65 78 74 2c 20 44 61 74 65 54 69 6d 65 ontext, DateTime
7b0e0 20 2a 70 29 7b 0a 20 20 64 6f 75 62 6c 65 20 72 *p){. double r
7b0f0 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 ;. sqlite3 *db
7b100 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 = sqlite3_contex
7b110 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 t_db_handle(cont
7b120 65 78 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 4f ext);. sqlite3O
7b130 73 43 75 72 72 65 6e 74 54 69 6d 65 28 64 62 2d sCurrentTime(db-
7b140 3e 70 56 66 73 2c 20 26 72 29 3b 0a 20 20 70 2d >pVfs, &r);. p-
7b150 3e 69 4a 44 20 3d 20 28 73 71 6c 69 74 65 33 5f >iJD = (sqlite3_
7b160 69 6e 74 36 34 29 28 72 2a 38 36 34 30 30 30 30 int64)(r*8640000
7b170 30 2e 30 20 2b 20 30 2e 35 29 3b 0a 20 20 70 2d 0.0 + 0.5);. p-
7b180 3e 76 61 6c 69 64 4a 44 20 3d 20 31 3b 0a 7d 0a >validJD = 1;.}.
7b190 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 ./*.** Attempt t
7b1a0 6f 20 70 61 72 73 65 20 74 68 65 20 67 69 76 65 o parse the give
7b1b0 6e 20 73 74 72 69 6e 67 20 69 6e 74 6f 20 61 20 n string into a
7b1c0 4a 75 6c 69 61 6e 20 44 61 79 20 4e 75 6d 62 65 Julian Day Numbe
7b1d0 72 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 74 68 r. Return.** th
7b1e0 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f e number of erro
7b1f0 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f rs..**.** The fo
7b200 6c 6c 6f 77 69 6e 67 20 61 72 65 20 61 63 63 65 llowing are acce
7b210 70 74 61 62 6c 65 20 66 6f 72 6d 73 20 66 6f 72 ptable forms for
7b220 20 74 68 65 20 69 6e 70 75 74 20 73 74 72 69 6e the input strin
7b230 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 59 59 g:.**.** YY
7b240 59 59 2d 4d 4d 2d 44 44 20 48 48 3a 4d 4d 3a 53 YY-MM-DD HH:MM:S
7b250 53 2e 46 46 46 20 20 2b 2f 2d 48 48 3a 4d 4d 0a S.FFF +/-HH:MM.
7b260 2a 2a 20 20 20 20 20 20 44 44 44 44 2e 44 44 20 ** DDDD.DD
7b270 0a 2a 2a 20 20 20 20 20 20 6e 6f 77 0a 2a 2a 0a .** now.**.
7b280 2a 2a 20 49 6e 20 74 68 65 20 66 69 72 73 74 20 ** In the first
7b290 66 6f 72 6d 2c 20 74 68 65 20 2b 2f 2d 48 48 3a form, the +/-HH:
7b2a0 4d 4d 20 69 73 20 61 6c 77 61 79 73 20 6f 70 74 MM is always opt
7b2b0 69 6f 6e 61 6c 2e 20 20 54 68 65 20 66 72 61 63 ional. The frac
7b2c0 74 69 6f 6e 61 6c 0a 2a 2a 20 73 65 63 6f 6e 64 tional.** second
7b2d0 73 20 65 78 74 65 6e 73 69 6f 6e 20 28 74 68 65 s extension (the
7b2e0 20 22 2e 46 46 46 22 29 20 69 73 20 6f 70 74 69 ".FFF") is opti
7b2f0 6f 6e 61 6c 2e 20 20 54 68 65 20 73 65 63 6f 6e onal. The secon
7b300 64 73 20 70 6f 72 74 69 6f 6e 0a 2a 2a 20 28 22 ds portion.** ("
7b310 3a 53 53 2e 46 46 46 22 29 20 69 73 20 6f 70 74 :SS.FFF") is opt
7b320 69 6f 6e 2e 20 20 54 68 65 20 79 65 61 72 20 61 ion. The year a
7b330 6e 64 20 64 61 74 65 20 63 61 6e 20 62 65 20 6f nd date can be o
7b340 6d 69 74 74 65 64 20 61 73 20 6c 6f 6e 67 0a 2a mitted as long.*
7b350 2a 20 61 73 20 74 68 65 72 65 20 69 73 20 61 20 * as there is a
7b360 74 69 6d 65 20 73 74 72 69 6e 67 2e 20 20 54 68 time string. Th
7b370 65 20 74 69 6d 65 20 73 74 72 69 6e 67 20 63 61 e time string ca
7b380 6e 20 62 65 20 6f 6d 69 74 74 65 64 20 61 73 20 n be omitted as
7b390 6c 6f 6e 67 0a 2a 2a 20 61 73 20 74 68 65 72 65 long.** as there
7b3a0 20 69 73 20 61 20 79 65 61 72 20 61 6e 64 20 64 is a year and d
7b3b0 61 74 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ate..*/.static i
7b3c0 6e 74 20 70 61 72 73 65 44 61 74 65 4f 72 54 69 nt parseDateOrTi
7b3d0 6d 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f me(. sqlite3_co
7b3e0 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 ntext *context,
7b3f0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
7b400 44 61 74 65 2c 20 0a 20 20 44 61 74 65 54 69 6d Date, . DateTim
7b410 65 20 2a 70 0a 29 7b 0a 20 20 69 6e 74 20 69 73 e *p.){. int is
7b420 52 65 61 6c 4e 75 6d 3b 20 20 20 20 2f 2a 20 52 RealNum; /* R
7b430 65 74 75 72 6e 20 66 72 6f 6d 20 73 71 6c 69 74 eturn from sqlit
7b440 65 33 49 73 4e 75 6d 62 65 72 28 29 2e 20 20 4e e3IsNumber(). N
7b450 6f 74 20 75 73 65 64 20 2a 2f 0a 20 20 69 66 28 ot used */. if(
7b460 20 70 61 72 73 65 59 79 79 79 4d 6d 44 64 28 7a parseYyyyMmDd(z
7b470 44 61 74 65 2c 70 29 3d 3d 30 20 29 7b 0a 20 20 Date,p)==0 ){.
7b480 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65 return 0;. }e
7b490 6c 73 65 20 69 66 28 20 70 61 72 73 65 48 68 4d lse if( parseHhM
7b4a0 6d 53 73 28 7a 44 61 74 65 2c 20 70 29 3d 3d 30 mSs(zDate, p)==0
7b4b0 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 ){. return 0
7b4c0 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73 71 ;. }else if( sq
7b4d0 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 44 61 lite3StrICmp(zDa
7b4e0 74 65 2c 22 6e 6f 77 22 29 3d 3d 30 29 7b 0a 20 te,"now")==0){.
7b4f0 20 20 20 73 65 74 44 61 74 65 54 69 6d 65 54 6f setDateTimeTo
7b500 43 75 72 72 65 6e 74 28 63 6f 6e 74 65 78 74 2c Current(context,
7b510 20 70 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 p);. return
7b520 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73 0;. }else if( s
7b530 71 6c 69 74 65 33 49 73 4e 75 6d 62 65 72 28 7a qlite3IsNumber(z
7b540 44 61 74 65 2c 20 26 69 73 52 65 61 6c 4e 75 6d Date, &isRealNum
7b550 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 20 29 , SQLITE_UTF8) )
7b560 7b 0a 20 20 20 20 64 6f 75 62 6c 65 20 72 3b 0a {. double r;.
7b570 20 20 20 20 67 65 74 56 61 6c 75 65 28 7a 44 61 getValue(zDa
7b580 74 65 2c 20 26 72 29 3b 0a 20 20 20 20 70 2d 3e te, &r);. p->
7b590 69 4a 44 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 iJD = (sqlite3_i
7b5a0 6e 74 36 34 29 28 72 2a 38 36 34 30 30 30 30 30 nt64)(r*86400000
7b5b0 2e 30 20 2b 20 30 2e 35 29 3b 0a 20 20 20 20 70 .0 + 0.5);. p
7b5c0 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 31 3b 0a 20 ->validJD = 1;.
7b5d0 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
7b5e0 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a . return 1;.}..
7b5f0 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68 /*.** Compute th
7b600 65 20 59 65 61 72 2c 20 4d 6f 6e 74 68 2c 20 61 e Year, Month, a
7b610 6e 64 20 44 61 79 20 66 72 6f 6d 20 74 68 65 20 nd Day from the
7b620 6a 75 6c 69 61 6e 20 64 61 79 20 6e 75 6d 62 65 julian day numbe
7b630 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 r..*/.static voi
7b640 64 20 63 6f 6d 70 75 74 65 59 4d 44 28 44 61 74 d computeYMD(Dat
7b650 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 69 6e 74 eTime *p){. int
7b660 20 5a 2c 20 41 2c 20 42 2c 20 43 2c 20 44 2c 20 Z, A, B, C, D,
7b670 45 2c 20 58 31 3b 0a 20 20 69 66 28 20 70 2d 3e E, X1;. if( p->
7b680 76 61 6c 69 64 59 4d 44 20 29 20 72 65 74 75 72 validYMD ) retur
7b690 6e 3b 0a 20 20 69 66 28 20 21 70 2d 3e 76 61 6c n;. if( !p->val
7b6a0 69 64 4a 44 20 29 7b 0a 20 20 20 20 70 2d 3e 59 idJD ){. p->Y
7b6b0 20 3d 20 32 30 30 30 3b 0a 20 20 20 20 70 2d 3e = 2000;. p->
7b6c0 4d 20 3d 20 31 3b 0a 20 20 20 20 70 2d 3e 44 20 M = 1;. p->D
7b6d0 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 = 1;. }else{.
7b6e0 20 20 5a 20 3d 20 28 69 6e 74 29 28 28 70 2d 3e Z = (int)((p->
7b6f0 69 4a 44 20 2b 20 34 33 32 30 30 30 30 30 29 2f iJD + 43200000)/
7b700 38 36 34 30 30 30 30 30 29 3b 0a 20 20 20 20 41 86400000);. A
7b710 20 3d 20 28 69 6e 74 29 28 28 5a 20 2d 20 31 38 = (int)((Z - 18
7b720 36 37 32 31 36 2e 32 35 29 2f 33 36 35 32 34 2e 67216.25)/36524.
7b730 32 35 29 3b 0a 20 20 20 20 41 20 3d 20 5a 20 2b 25);. A = Z +
7b740 20 31 20 2b 20 41 20 2d 20 28 41 2f 34 29 3b 0a 1 + A - (A/4);.
7b750 20 20 20 20 42 20 3d 20 41 20 2b 20 31 35 32 34 B = A + 1524
7b760 3b 0a 20 20 20 20 43 20 3d 20 28 69 6e 74 29 28 ;. C = (int)(
7b770 28 42 20 2d 20 31 32 32 2e 31 29 2f 33 36 35 2e (B - 122.1)/365.
7b780 32 35 29 3b 0a 20 20 20 20 44 20 3d 20 28 33 36 25);. D = (36
7b790 35 32 35 2a 43 29 2f 31 30 30 3b 0a 20 20 20 20 525*C)/100;.
7b7a0 45 20 3d 20 28 69 6e 74 29 28 28 42 2d 44 29 2f E = (int)((B-D)/
7b7b0 33 30 2e 36 30 30 31 29 3b 0a 20 20 20 20 58 31 30.6001);. X1
7b7c0 20 3d 20 28 69 6e 74 29 28 33 30 2e 36 30 30 31 = (int)(30.6001
7b7d0 2a 45 29 3b 0a 20 20 20 20 70 2d 3e 44 20 3d 20 *E);. p->D =
7b7e0 42 20 2d 20 44 20 2d 20 58 31 3b 0a 20 20 20 20 B - D - X1;.
7b7f0 70 2d 3e 4d 20 3d 20 45 3c 31 34 20 3f 20 45 2d p->M = E<14 ? E-
7b800 31 20 3a 20 45 2d 31 33 3b 0a 20 20 20 20 70 2d 1 : E-13;. p-
7b810 3e 59 20 3d 20 70 2d 3e 4d 3e 32 20 3f 20 43 20 >Y = p->M>2 ? C
7b820 2d 20 34 37 31 36 20 3a 20 43 20 2d 20 34 37 31 - 4716 : C - 471
7b830 35 3b 0a 20 20 7d 0a 20 20 70 2d 3e 76 61 6c 69 5;. }. p->vali
7b840 64 59 4d 44 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a dYMD = 1;.}../*.
7b850 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20 48 ** Compute the H
7b860 6f 75 72 2c 20 4d 69 6e 75 74 65 2c 20 61 6e 64 our, Minute, and
7b870 20 53 65 63 6f 6e 64 73 20 66 72 6f 6d 20 74 68 Seconds from th
7b880 65 20 6a 75 6c 69 61 6e 20 64 61 79 20 6e 75 6d e julian day num
7b890 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ber..*/.static v
7b8a0 6f 69 64 20 63 6f 6d 70 75 74 65 48 4d 53 28 44 oid computeHMS(D
7b8b0 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 69 ateTime *p){. i
7b8c0 6e 74 20 73 3b 0a 20 20 69 66 28 20 70 2d 3e 76 nt s;. if( p->v
7b8d0 61 6c 69 64 48 4d 53 20 29 20 72 65 74 75 72 6e alidHMS ) return
7b8e0 3b 0a 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 ;. computeJD(p)
7b8f0 3b 0a 20 20 73 20 3d 20 28 69 6e 74 29 28 28 70 ;. s = (int)((p
7b900 2d 3e 69 4a 44 20 2b 20 34 33 32 30 30 30 30 30 ->iJD + 43200000
7b910 29 20 25 20 38 36 34 30 30 30 30 30 29 3b 0a 20 ) % 86400000);.
7b920 20 70 2d 3e 73 20 3d 20 73 2f 31 30 30 30 2e 30 p->s = s/1000.0
7b930 3b 0a 20 20 73 20 3d 20 28 69 6e 74 29 70 2d 3e ;. s = (int)p->
7b940 73 3b 0a 20 20 70 2d 3e 73 20 2d 3d 20 73 3b 0a s;. p->s -= s;.
7b950 20 20 70 2d 3e 68 20 3d 20 73 2f 33 36 30 30 3b p->h = s/3600;
7b960 0a 20 20 73 20 2d 3d 20 70 2d 3e 68 2a 33 36 30 . s -= p->h*360
7b970 30 3b 0a 20 20 70 2d 3e 6d 20 3d 20 73 2f 36 30 0;. p->m = s/60
7b980 3b 0a 20 20 70 2d 3e 73 20 2b 3d 20 73 20 2d 20 ;. p->s += s -
7b990 70 2d 3e 6d 2a 36 30 3b 0a 20 20 70 2d 3e 76 61 p->m*60;. p->va
7b9a0 6c 69 64 48 4d 53 20 3d 20 31 3b 0a 7d 0a 0a 2f lidHMS = 1;.}../
7b9b0 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 62 6f 74 *.** Compute bot
7b9c0 68 20 59 4d 44 20 61 6e 64 20 48 4d 53 0a 2a 2f h YMD and HMS.*/
7b9d0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f 6d .static void com
7b9e0 70 75 74 65 59 4d 44 5f 48 4d 53 28 44 61 74 65 puteYMD_HMS(Date
7b9f0 54 69 6d 65 20 2a 70 29 7b 0a 20 20 63 6f 6d 70 Time *p){. comp
7ba00 75 74 65 59 4d 44 28 70 29 3b 0a 20 20 63 6f 6d uteYMD(p);. com
7ba10 70 75 74 65 48 4d 53 28 70 29 3b 0a 7d 0a 0a 2f puteHMS(p);.}../
7ba20 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65 20 59 *.** Clear the Y
7ba30 4d 44 20 61 6e 64 20 48 4d 53 20 61 6e 64 20 74 MD and HMS and t
7ba40 68 65 20 54 5a 0a 2a 2f 0a 73 74 61 74 69 63 20 he TZ.*/.static
7ba50 76 6f 69 64 20 63 6c 65 61 72 59 4d 44 5f 48 4d void clearYMD_HM
7ba60 53 5f 54 5a 28 44 61 74 65 54 69 6d 65 20 2a 70 S_TZ(DateTime *p
7ba70 29 7b 0a 20 20 70 2d 3e 76 61 6c 69 64 59 4d 44 ){. p->validYMD
7ba80 20 3d 20 30 3b 0a 20 20 70 2d 3e 76 61 6c 69 64 = 0;. p->valid
7ba90 48 4d 53 20 3d 20 30 3b 0a 20 20 70 2d 3e 76 61 HMS = 0;. p->va
7baa0 6c 69 64 54 5a 20 3d 20 30 3b 0a 7d 0a 0a 23 69 lidTZ = 0;.}..#i
7bab0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
7bac0 54 5f 4c 4f 43 41 4c 54 49 4d 45 0a 2f 2a 0a 2a T_LOCALTIME./*.*
7bad0 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20 64 69 * Compute the di
7bae0 66 66 65 72 65 6e 63 65 20 28 69 6e 20 6d 69 6c fference (in mil
7baf0 6c 69 73 65 63 6f 6e 64 73 29 0a 2a 2a 20 62 65 liseconds).** be
7bb00 74 77 65 65 6e 20 6c 6f 63 61 6c 74 69 6d 65 20 tween localtime
7bb10 61 6e 64 20 55 54 43 20 28 61 2e 6b 2e 61 2e 20 and UTC (a.k.a.
7bb20 47 4d 54 29 0a 2a 2a 20 66 6f 72 20 74 68 65 20 GMT).** for the
7bb30 74 69 6d 65 20 76 61 6c 75 65 20 70 20 77 68 65 time value p whe
7bb40 72 65 20 70 20 69 73 20 69 6e 20 55 54 43 2e 0a re p is in UTC..
7bb50 2a 2f 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 */.static sqlite
7bb60 33 5f 69 6e 74 36 34 20 6c 6f 63 61 6c 74 69 6d 3_int64 localtim
7bb70 65 4f 66 66 73 65 74 28 44 61 74 65 54 69 6d 65 eOffset(DateTime
7bb80 20 2a 70 29 7b 0a 20 20 44 61 74 65 54 69 6d 65 *p){. DateTime
7bb90 20 78 2c 20 79 3b 0a 20 20 74 69 6d 65 5f 74 20 x, y;. time_t
7bba0 74 3b 0a 20 20 78 20 3d 20 2a 70 3b 0a 20 20 63 t;. x = *p;. c
7bbb0 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53 28 26 78 omputeYMD_HMS(&x
7bbc0 29 3b 0a 20 20 69 66 28 20 78 2e 59 3c 31 39 37 );. if( x.Y<197
7bbd0 31 20 7c 7c 20 78 2e 59 3e 3d 32 30 33 38 20 29 1 || x.Y>=2038 )
7bbe0 7b 0a 20 20 20 20 78 2e 59 20 3d 20 32 30 30 30 {. x.Y = 2000
7bbf0 3b 0a 20 20 20 20 78 2e 4d 20 3d 20 31 3b 0a 20 ;. x.M = 1;.
7bc00 20 20 20 78 2e 44 20 3d 20 31 3b 0a 20 20 20 20 x.D = 1;.
7bc10 78 2e 68 20 3d 20 30 3b 0a 20 20 20 20 78 2e 6d x.h = 0;. x.m
7bc20 20 3d 20 30 3b 0a 20 20 20 20 78 2e 73 20 3d 20 = 0;. x.s =
7bc30 30 2e 30 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 0.0;. } else {.
7bc40 20 20 20 20 69 6e 74 20 73 20 3d 20 28 69 6e 74 int s = (int
7bc50 29 28 78 2e 73 20 2b 20 30 2e 35 29 3b 0a 20 20 )(x.s + 0.5);.
7bc60 20 20 78 2e 73 20 3d 20 73 3b 0a 20 20 7d 0a 20 x.s = s;. }.
7bc70 20 78 2e 74 7a 20 3d 20 30 3b 0a 20 20 78 2e 76 x.tz = 0;. x.v
7bc80 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20 63 6f alidJD = 0;. co
7bc90 6d 70 75 74 65 4a 44 28 26 78 29 3b 0a 20 20 74 mputeJD(&x);. t
7bca0 20 3d 20 28 74 69 6d 65 5f 74 29 28 78 2e 69 4a = (time_t)(x.iJ
7bcb0 44 2f 31 30 30 30 20 2d 20 32 31 30 38 36 36 37 D/1000 - 2108667
7bcc0 36 2a 28 69 36 34 29 31 30 30 30 30 29 3b 0a 23 6*(i64)10000);.#
7bcd0 69 66 64 65 66 20 48 41 56 45 5f 4c 4f 43 41 4c ifdef HAVE_LOCAL
7bce0 54 49 4d 45 5f 52 0a 20 20 7b 0a 20 20 20 20 73 TIME_R. {. s
7bcf0 74 72 75 63 74 20 74 6d 20 73 4c 6f 63 61 6c 3b truct tm sLocal;
7bd00 0a 20 20 20 20 6c 6f 63 61 6c 74 69 6d 65 5f 72 . localtime_r
7bd10 28 26 74 2c 20 26 73 4c 6f 63 61 6c 29 3b 0a 20 (&t, &sLocal);.
7bd20 20 20 20 79 2e 59 20 3d 20 73 4c 6f 63 61 6c 2e y.Y = sLocal.
7bd30 74 6d 5f 79 65 61 72 20 2b 20 31 39 30 30 3b 0a tm_year + 1900;.
7bd40 20 20 20 20 79 2e 4d 20 3d 20 73 4c 6f 63 61 6c y.M = sLocal
7bd50 2e 74 6d 5f 6d 6f 6e 20 2b 20 31 3b 0a 20 20 20 .tm_mon + 1;.
7bd60 20 79 2e 44 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d y.D = sLocal.tm
7bd70 5f 6d 64 61 79 3b 0a 20 20 20 20 79 2e 68 20 3d _mday;. y.h =
7bd80 20 73 4c 6f 63 61 6c 2e 74 6d 5f 68 6f 75 72 3b sLocal.tm_hour;
7bd90 0a 20 20 20 20 79 2e 6d 20 3d 20 73 4c 6f 63 61 . y.m = sLoca
7bda0 6c 2e 74 6d 5f 6d 69 6e 3b 0a 20 20 20 20 79 2e l.tm_min;. y.
7bdb0 73 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 73 65 s = sLocal.tm_se
7bdc0 63 3b 0a 20 20 7d 0a 23 65 6c 69 66 20 64 65 66 c;. }.#elif def
7bdd0 69 6e 65 64 28 48 41 56 45 5f 4c 4f 43 41 4c 54 ined(HAVE_LOCALT
7bde0 49 4d 45 5f 53 29 20 26 26 20 48 41 56 45 5f 4c IME_S) && HAVE_L
7bdf0 4f 43 41 4c 54 49 4d 45 5f 53 0a 20 20 7b 0a 20 OCALTIME_S. {.
7be00 20 20 20 73 74 72 75 63 74 20 74 6d 20 73 4c 6f struct tm sLo
7be10 63 61 6c 3b 0a 20 20 20 20 6c 6f 63 61 6c 74 69 cal;. localti
7be20 6d 65 5f 73 28 26 73 4c 6f 63 61 6c 2c 20 26 74 me_s(&sLocal, &t
7be30 29 3b 0a 20 20 20 20 79 2e 59 20 3d 20 73 4c 6f );. y.Y = sLo
7be40 63 61 6c 2e 74 6d 5f 79 65 61 72 20 2b 20 31 39 cal.tm_year + 19
7be50 30 30 3b 0a 20 20 20 20 79 2e 4d 20 3d 20 73 4c 00;. y.M = sL
7be60 6f 63 61 6c 2e 74 6d 5f 6d 6f 6e 20 2b 20 31 3b ocal.tm_mon + 1;
7be70 0a 20 20 20 20 79 2e 44 20 3d 20 73 4c 6f 63 61 . y.D = sLoca
7be80 6c 2e 74 6d 5f 6d 64 61 79 3b 0a 20 20 20 20 79 l.tm_mday;. y
7be90 2e 68 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 68 .h = sLocal.tm_h
7bea0 6f 75 72 3b 0a 20 20 20 20 79 2e 6d 20 3d 20 73 our;. y.m = s
7beb0 4c 6f 63 61 6c 2e 74 6d 5f 6d 69 6e 3b 0a 20 20 Local.tm_min;.
7bec0 20 20 79 2e 73 20 3d 20 73 4c 6f 63 61 6c 2e 74 y.s = sLocal.t
7bed0 6d 5f 73 65 63 3b 0a 20 20 7d 0a 23 65 6c 73 65 m_sec;. }.#else
7bee0 0a 20 20 7b 0a 20 20 20 20 73 74 72 75 63 74 20 . {. struct
7bef0 74 6d 20 2a 70 54 6d 3b 0a 20 20 20 20 73 71 6c tm *pTm;. sql
7bf00 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
7bf10 28 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c (sqlite3MutexAll
7bf20 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f oc(SQLITE_MUTEX_
7bf30 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 29 3b STATIC_MASTER));
7bf40 0a 20 20 20 20 70 54 6d 20 3d 20 6c 6f 63 61 6c . pTm = local
7bf50 74 69 6d 65 28 26 74 29 3b 0a 20 20 20 20 79 2e time(&t);. y.
7bf60 59 20 3d 20 70 54 6d 2d 3e 74 6d 5f 79 65 61 72 Y = pTm->tm_year
7bf70 20 2b 20 31 39 30 30 3b 0a 20 20 20 20 79 2e 4d + 1900;. y.M
7bf80 20 3d 20 70 54 6d 2d 3e 74 6d 5f 6d 6f 6e 20 2b = pTm->tm_mon +
7bf90 20 31 3b 0a 20 20 20 20 79 2e 44 20 3d 20 70 54 1;. y.D = pT
7bfa0 6d 2d 3e 74 6d 5f 6d 64 61 79 3b 0a 20 20 20 20 m->tm_mday;.
7bfb0 79 2e 68 20 3d 20 70 54 6d 2d 3e 74 6d 5f 68 6f y.h = pTm->tm_ho
7bfc0 75 72 3b 0a 20 20 20 20 79 2e 6d 20 3d 20 70 54 ur;. y.m = pT
7bfd0 6d 2d 3e 74 6d 5f 6d 69 6e 3b 0a 20 20 20 20 79 m->tm_min;. y
7bfe0 2e 73 20 3d 20 70 54 6d 2d 3e 74 6d 5f 73 65 63 .s = pTm->tm_sec
7bff0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 ;. sqlite3_mu
7c000 74 65 78 5f 6c 65 61 76 65 28 73 71 6c 69 74 65 tex_leave(sqlite
7c010 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 3MutexAlloc(SQLI
7c020 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f TE_MUTEX_STATIC_
7c030 4d 41 53 54 45 52 29 29 3b 0a 20 20 7d 0a 23 65 MASTER));. }.#e
7c040 6e 64 69 66 0a 20 20 79 2e 76 61 6c 69 64 59 4d ndif. y.validYM
7c050 44 20 3d 20 31 3b 0a 20 20 79 2e 76 61 6c 69 64 D = 1;. y.valid
7c060 48 4d 53 20 3d 20 31 3b 0a 20 20 79 2e 76 61 6c HMS = 1;. y.val
7c070 69 64 4a 44 20 3d 20 30 3b 0a 20 20 79 2e 76 61 idJD = 0;. y.va
7c080 6c 69 64 54 5a 20 3d 20 30 3b 0a 20 20 63 6f 6d lidTZ = 0;. com
7c090 70 75 74 65 4a 44 28 26 79 29 3b 0a 20 20 72 65 puteJD(&y);. re
7c0a0 74 75 72 6e 20 79 2e 69 4a 44 20 2d 20 78 2e 69 turn y.iJD - x.i
7c0b0 4a 44 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 JD;.}.#endif /*
7c0c0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 43 41 SQLITE_OMIT_LOCA
7c0d0 4c 54 49 4d 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 LTIME */../*.**
7c0e0 50 72 6f 63 65 73 73 20 61 20 6d 6f 64 69 66 69 Process a modifi
7c0f0 65 72 20 74 6f 20 61 20 64 61 74 65 2d 74 69 6d er to a date-tim
7c100 65 20 73 74 61 6d 70 2e 20 20 54 68 65 20 6d 6f e stamp. The mo
7c110 64 69 66 69 65 72 73 20 61 72 65 0a 2a 2a 20 61 difiers are.** a
7c120 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a s follows:.**.**
7c130 20 20 20 20 20 4e 4e 4e 20 64 61 79 73 0a 2a 2a NNN days.**
7c140 20 20 20 20 20 4e 4e 4e 20 68 6f 75 72 73 0a 2a NNN hours.*
7c150 2a 20 20 20 20 20 4e 4e 4e 20 6d 69 6e 75 74 65 * NNN minute
7c160 73 0a 2a 2a 20 20 20 20 20 4e 4e 4e 2e 4e 4e 4e s.** NNN.NNN
7c170 4e 20 73 65 63 6f 6e 64 73 0a 2a 2a 20 20 20 20 N seconds.**
7c180 20 4e 4e 4e 20 6d 6f 6e 74 68 73 0a 2a 2a 20 20 NNN months.**
7c190 20 20 20 4e 4e 4e 20 79 65 61 72 73 0a 2a 2a 20 NNN years.**
7c1a0 20 20 20 20 73 74 61 72 74 20 6f 66 20 6d 6f 6e start of mon
7c1b0 74 68 0a 2a 2a 20 20 20 20 20 73 74 61 72 74 20 th.** start
7c1c0 6f 66 20 79 65 61 72 0a 2a 2a 20 20 20 20 20 73 of year.** s
7c1d0 74 61 72 74 20 6f 66 20 77 65 65 6b 0a 2a 2a 20 tart of week.**
7c1e0 20 20 20 20 73 74 61 72 74 20 6f 66 20 64 61 79 start of day
7c1f0 0a 2a 2a 20 20 20 20 20 77 65 65 6b 64 61 79 20 .** weekday
7c200 4e 0a 2a 2a 20 20 20 20 20 75 6e 69 78 65 70 6f N.** unixepo
7c210 63 68 0a 2a 2a 20 20 20 20 20 6c 6f 63 61 6c 74 ch.** localt
7c220 69 6d 65 0a 2a 2a 20 20 20 20 20 75 74 63 0a 2a ime.** utc.*
7c230 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 30 20 6f 6e *.** Return 0 on
7c240 20 73 75 63 63 65 73 73 20 61 6e 64 20 31 20 69 success and 1 i
7c250 66 20 74 68 65 72 65 20 69 73 20 61 6e 79 20 6b f there is any k
7c260 69 6e 64 20 6f 66 20 65 72 72 6f 72 2e 0a 2a 2f ind of error..*/
7c270 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 72 73 .static int pars
7c280 65 4d 6f 64 69 66 69 65 72 28 63 6f 6e 73 74 20 eModifier(const
7c290 63 68 61 72 20 2a 7a 4d 6f 64 2c 20 44 61 74 65 char *zMod, Date
7c2a0 54 69 6d 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 Time *p){. int
7c2b0 72 63 20 3d 20 31 3b 0a 20 20 69 6e 74 20 6e 3b rc = 1;. int n;
7c2c0 0a 20 20 64 6f 75 62 6c 65 20 72 3b 0a 20 20 63 . double r;. c
7c2d0 68 61 72 20 2a 7a 2c 20 7a 42 75 66 5b 33 30 5d har *z, zBuf[30]
7c2e0 3b 0a 20 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 ;. z = zBuf;.
7c2f0 66 6f 72 28 6e 3d 30 3b 20 6e 3c 41 72 72 61 79 for(n=0; n<Array
7c300 53 69 7a 65 28 7a 42 75 66 29 2d 31 20 26 26 20 Size(zBuf)-1 &&
7c310 7a 4d 6f 64 5b 6e 5d 3b 20 6e 2b 2b 29 7b 0a 20 zMod[n]; n++){.
7c320 20 20 20 7a 5b 6e 5d 20 3d 20 28 63 68 61 72 29 z[n] = (char)
7c330 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f sqlite3UpperToLo
7c340 77 65 72 5b 28 75 38 29 7a 4d 6f 64 5b 6e 5d 5d wer[(u8)zMod[n]]
7c350 3b 0a 20 20 7d 0a 20 20 7a 5b 6e 5d 20 3d 20 30 ;. }. z[n] = 0
7c360 3b 0a 20 20 73 77 69 74 63 68 28 20 7a 5b 30 5d ;. switch( z[0]
7c370 20 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 ){.#ifndef SQLI
7c380 54 45 5f 4f 4d 49 54 5f 4c 4f 43 41 4c 54 49 4d TE_OMIT_LOCALTIM
7c390 45 0a 20 20 20 20 63 61 73 65 20 27 6c 27 3a 20 E. case 'l':
7c3a0 7b 0a 20 20 20 20 20 20 2f 2a 20 20 20 20 6c 6f {. /* lo
7c3b0 63 61 6c 74 69 6d 65 0a 20 20 20 20 20 20 2a 2a caltime. **
7c3c0 0a 20 20 20 20 20 20 2a 2a 20 41 73 73 75 6d 69 . ** Assumi
7c3d0 6e 67 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 ng the current t
7c3e0 69 6d 65 20 76 61 6c 75 65 20 69 73 20 55 54 43 ime value is UTC
7c3f0 20 28 61 2e 6b 2e 61 2e 20 47 4d 54 29 2c 20 73 (a.k.a. GMT), s
7c400 68 69 66 74 20 69 74 20 74 6f 0a 20 20 20 20 20 hift it to.
7c410 20 2a 2a 20 73 68 6f 77 20 6c 6f 63 61 6c 20 74 ** show local t
7c420 69 6d 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 ime.. */.
7c430 20 20 20 20 69 66 28 20 73 74 72 63 6d 70 28 7a if( strcmp(z
7c440 2c 20 22 6c 6f 63 61 6c 74 69 6d 65 22 29 3d 3d , "localtime")==
7c450 30 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f 6d 0 ){. com
7c460 70 75 74 65 4a 44 28 70 29 3b 0a 20 20 20 20 20 puteJD(p);.
7c470 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 6c 6f 63 p->iJD += loc
7c480 61 6c 74 69 6d 65 4f 66 66 73 65 74 28 70 29 3b altimeOffset(p);
7c490 0a 20 20 20 20 20 20 20 20 63 6c 65 61 72 59 4d . clearYM
7c4a0 44 5f 48 4d 53 5f 54 5a 28 70 29 3b 0a 20 20 20 D_HMS_TZ(p);.
7c4b0 20 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 rc = 0;.
7c4c0 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b }. break
7c4d0 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 ;. }.#endif.
7c4e0 20 20 20 63 61 73 65 20 27 75 27 3a 20 7b 0a 20 case 'u': {.
7c4f0 20 20 20 20 20 2f 2a 0a 20 20 20 20 20 20 2a 2a /*. **
7c500 20 20 20 20 75 6e 69 78 65 70 6f 63 68 0a 20 20 unixepoch.
7c510 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 **. **
7c520 54 72 65 61 74 20 74 68 65 20 63 75 72 72 65 6e Treat the curren
7c530 74 20 76 61 6c 75 65 20 6f 66 20 70 2d 3e 69 4a t value of p->iJ
7c540 44 20 61 73 20 74 68 65 20 6e 75 6d 62 65 72 20 D as the number
7c550 6f 66 0a 20 20 20 20 20 20 2a 2a 20 73 65 63 6f of. ** seco
7c560 6e 64 73 20 73 69 6e 63 65 20 31 39 37 30 2e 20 nds since 1970.
7c570 20 43 6f 6e 76 65 72 74 20 74 6f 20 61 20 72 65 Convert to a re
7c580 61 6c 20 6a 75 6c 69 61 6e 20 64 61 79 20 6e 75 al julian day nu
7c590 6d 62 65 72 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 mber.. */.
7c5a0 20 20 20 20 20 69 66 28 20 73 74 72 63 6d 70 28 if( strcmp(
7c5b0 7a 2c 20 22 75 6e 69 78 65 70 6f 63 68 22 29 3d z, "unixepoch")=
7c5c0 3d 30 20 26 26 20 70 2d 3e 76 61 6c 69 64 4a 44 =0 && p->validJD
7c5d0 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 ){. p->i
7c5e0 4a 44 20 3d 20 28 70 2d 3e 69 4a 44 20 2b 20 34 JD = (p->iJD + 4
7c5f0 33 32 30 30 29 2f 38 36 34 30 30 20 2b 20 32 31 3200)/86400 + 21
7c600 30 38 36 36 37 36 2a 28 69 36 34 29 31 30 30 30 086676*(i64)1000
7c610 30 30 30 30 3b 0a 20 20 20 20 20 20 20 20 63 6c 0000;. cl
7c620 65 61 72 59 4d 44 5f 48 4d 53 5f 54 5a 28 70 29 earYMD_HMS_TZ(p)
7c630 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 30 ;. rc = 0
7c640 3b 0a 20 20 20 20 20 20 7d 0a 23 69 66 6e 64 65 ;. }.#ifnde
7c650 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f f SQLITE_OMIT_LO
7c660 43 41 4c 54 49 4d 45 0a 20 20 20 20 20 20 65 6c CALTIME. el
7c670 73 65 20 69 66 28 20 73 74 72 63 6d 70 28 7a 2c se if( strcmp(z,
7c680 20 22 75 74 63 22 29 3d 3d 30 20 29 7b 0a 20 20 "utc")==0 ){.
7c690 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e sqlite3_in
7c6a0 74 36 34 20 63 31 3b 0a 20 20 20 20 20 20 20 20 t64 c1;.
7c6b0 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20 20 computeJD(p);.
7c6c0 20 20 20 20 20 20 63 31 20 3d 20 6c 6f 63 61 6c c1 = local
7c6d0 74 69 6d 65 4f 66 66 73 65 74 28 70 29 3b 0a 20 timeOffset(p);.
7c6e0 20 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 2d 3d p->iJD -=
7c6f0 20 63 31 3b 0a 20 20 20 20 20 20 20 20 63 6c 65 c1;. cle
7c700 61 72 59 4d 44 5f 48 4d 53 5f 54 5a 28 70 29 3b arYMD_HMS_TZ(p);
7c710 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 . p->iJD
7c720 2b 3d 20 63 31 20 2d 20 6c 6f 63 61 6c 74 69 6d += c1 - localtim
7c730 65 4f 66 66 73 65 74 28 70 29 3b 0a 20 20 20 20 eOffset(p);.
7c740 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 20 rc = 0;.
7c750 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 }.#endif.
7c760 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
7c770 20 20 63 61 73 65 20 27 77 27 3a 20 7b 0a 20 20 case 'w': {.
7c780 20 20 20 20 2f 2a 0a 20 20 20 20 20 20 2a 2a 20 /*. **
7c790 20 20 20 77 65 65 6b 64 61 79 20 4e 0a 20 20 20 weekday N.
7c7a0 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 4d **. ** M
7c7b0 6f 76 65 20 74 68 65 20 64 61 74 65 20 74 6f 20 ove the date to
7c7c0 74 68 65 20 73 61 6d 65 20 74 69 6d 65 20 6f 6e the same time on
7c7d0 20 74 68 65 20 6e 65 78 74 20 6f 63 63 75 72 72 the next occurr
7c7e0 65 6e 63 65 20 6f 66 0a 20 20 20 20 20 20 2a 2a ence of. **
7c7f0 20 77 65 65 6b 64 61 79 20 4e 20 77 68 65 72 65 weekday N where
7c800 20 30 3d 3d 53 75 6e 64 61 79 2c 20 31 3d 3d 4d 0==Sunday, 1==M
7c810 6f 6e 64 61 79 2c 20 61 6e 64 20 73 6f 20 66 6f onday, and so fo
7c820 72 74 68 2e 20 20 49 66 20 74 68 65 0a 20 20 20 rth. If the.
7c830 20 20 20 2a 2a 20 64 61 74 65 20 69 73 20 61 6c ** date is al
7c840 72 65 61 64 79 20 6f 6e 20 74 68 65 20 61 70 70 ready on the app
7c850 72 6f 70 72 69 61 74 65 20 77 65 65 6b 64 61 79 ropriate weekday
7c860 2c 20 74 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f , this is a no-o
7c870 70 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 p.. */.
7c880 20 20 69 66 28 20 73 74 72 6e 63 6d 70 28 7a 2c if( strncmp(z,
7c890 20 22 77 65 65 6b 64 61 79 20 22 2c 20 38 29 3d "weekday ", 8)=
7c8a0 3d 30 20 26 26 20 67 65 74 56 61 6c 75 65 28 26 =0 && getValue(&
7c8b0 7a 5b 38 5d 2c 26 72 29 3e 30 0a 20 20 20 20 20 z[8],&r)>0.
7c8c0 20 20 20 20 20 20 20 20 20 20 20 20 26 26 20 28 && (
7c8d0 6e 3d 28 69 6e 74 29 72 29 3d 3d 72 20 26 26 20 n=(int)r)==r &&
7c8e0 6e 3e 3d 30 20 26 26 20 72 3c 37 20 29 7b 0a 20 n>=0 && r<7 ){.
7c8f0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 sqlite3_i
7c900 6e 74 36 34 20 5a 3b 0a 20 20 20 20 20 20 20 20 nt64 Z;.
7c910 63 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53 28 70 computeYMD_HMS(p
7c920 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 76 61 );. p->va
7c930 6c 69 64 54 5a 20 3d 20 30 3b 0a 20 20 20 20 20 lidTZ = 0;.
7c940 20 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 p->validJD =
7c950 30 3b 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75 0;. compu
7c960 74 65 4a 44 28 70 29 3b 0a 20 20 20 20 20 20 20 teJD(p);.
7c970 20 5a 20 3d 20 28 28 70 2d 3e 69 4a 44 20 2b 20 Z = ((p->iJD +
7c980 31 32 39 36 30 30 30 30 30 29 2f 38 36 34 30 30 129600000)/86400
7c990 30 30 30 29 20 25 20 37 3b 0a 20 20 20 20 20 20 000) % 7;.
7c9a0 20 20 69 66 28 20 5a 3e 6e 20 29 20 5a 20 2d 3d if( Z>n ) Z -=
7c9b0 20 37 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 7;. p->i
7c9c0 4a 44 20 2b 3d 20 28 6e 20 2d 20 5a 29 2a 38 36 JD += (n - Z)*86
7c9d0 34 30 30 30 30 30 3b 0a 20 20 20 20 20 20 20 20 400000;.
7c9e0 63 6c 65 61 72 59 4d 44 5f 48 4d 53 5f 54 5a 28 clearYMD_HMS_TZ(
7c9f0 70 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d p);. rc =
7ca00 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 0;. }.
7ca10 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
7ca20 20 20 20 63 61 73 65 20 27 73 27 3a 20 7b 0a 20 case 's': {.
7ca30 20 20 20 20 20 2f 2a 0a 20 20 20 20 20 20 2a 2a /*. **
7ca40 20 20 20 20 73 74 61 72 74 20 6f 66 20 54 54 54 start of TTT
7ca50 54 54 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 TT. **.
7ca60 20 20 2a 2a 20 4d 6f 76 65 20 74 68 65 20 64 61 ** Move the da
7ca70 74 65 20 62 61 63 6b 77 61 72 64 73 20 74 6f 20 te backwards to
7ca80 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 the beginning of
7ca90 20 74 68 65 20 63 75 72 72 65 6e 74 20 64 61 79 the current day
7caa0 2c 0a 20 20 20 20 20 20 2a 2a 20 6f 72 20 6d 6f ,. ** or mo
7cab0 6e 74 68 20 6f 72 20 79 65 61 72 2e 0a 20 20 20 nth or year..
7cac0 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 */. if(
7cad0 73 74 72 6e 63 6d 70 28 7a 2c 20 22 73 74 61 72 strncmp(z, "star
7cae0 74 20 6f 66 20 22 2c 20 39 29 21 3d 30 20 29 20 t of ", 9)!=0 )
7caf0 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7a 20 2b break;. z +
7cb00 3d 20 39 3b 0a 20 20 20 20 20 20 63 6f 6d 70 75 = 9;. compu
7cb10 74 65 59 4d 44 28 70 29 3b 0a 20 20 20 20 20 20 teYMD(p);.
7cb20 70 2d 3e 76 61 6c 69 64 48 4d 53 20 3d 20 31 3b p->validHMS = 1;
7cb30 0a 20 20 20 20 20 20 70 2d 3e 68 20 3d 20 70 2d . p->h = p-
7cb40 3e 6d 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d >m = 0;. p-
7cb50 3e 73 20 3d 20 30 2e 30 3b 0a 20 20 20 20 20 20 >s = 0.0;.
7cb60 70 2d 3e 76 61 6c 69 64 54 5a 20 3d 20 30 3b 0a p->validTZ = 0;.
7cb70 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 4a 44 p->validJD
7cb80 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 66 28 20 = 0;. if(
7cb90 73 74 72 63 6d 70 28 7a 2c 22 6d 6f 6e 74 68 22 strcmp(z,"month"
7cba0 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 )==0 ){.
7cbb0 70 2d 3e 44 20 3d 20 31 3b 0a 20 20 20 20 20 20 p->D = 1;.
7cbc0 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 rc = 0;.
7cbd0 7d 65 6c 73 65 20 69 66 28 20 73 74 72 63 6d 70 }else if( strcmp
7cbe0 28 7a 2c 22 79 65 61 72 22 29 3d 3d 30 20 29 7b (z,"year")==0 ){
7cbf0 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 . compute
7cc00 59 4d 44 28 70 29 3b 0a 20 20 20 20 20 20 20 20 YMD(p);.
7cc10 70 2d 3e 4d 20 3d 20 31 3b 0a 20 20 20 20 20 20 p->M = 1;.
7cc20 20 20 70 2d 3e 44 20 3d 20 31 3b 0a 20 20 20 20 p->D = 1;.
7cc30 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 20 rc = 0;.
7cc40 20 20 7d 65 6c 73 65 20 69 66 28 20 73 74 72 63 }else if( strc
7cc50 6d 70 28 7a 2c 22 64 61 79 22 29 3d 3d 30 20 29 mp(z,"day")==0 )
7cc60 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 30 {. rc = 0
7cc70 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
7cc80 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 break;. }.
7cc90 20 63 61 73 65 20 27 2b 27 3a 0a 20 20 20 20 63 case '+':. c
7cca0 61 73 65 20 27 2d 27 3a 0a 20 20 20 20 63 61 73 ase '-':. cas
7ccb0 65 20 27 30 27 3a 0a 20 20 20 20 63 61 73 65 20 e '0':. case
7ccc0 27 31 27 3a 0a 20 20 20 20 63 61 73 65 20 27 32 '1':. case '2
7ccd0 27 3a 0a 20 20 20 20 63 61 73 65 20 27 33 27 3a ':. case '3':
7cce0 0a 20 20 20 20 63 61 73 65 20 27 34 27 3a 0a 20 . case '4':.
7ccf0 20 20 20 63 61 73 65 20 27 35 27 3a 0a 20 20 20 case '5':.
7cd00 20 63 61 73 65 20 27 36 27 3a 0a 20 20 20 20 63 case '6':. c
7cd10 61 73 65 20 27 37 27 3a 0a 20 20 20 20 63 61 73 ase '7':. cas
7cd20 65 20 27 38 27 3a 0a 20 20 20 20 63 61 73 65 20 e '8':. case
7cd30 27 39 27 3a 20 7b 0a 20 20 20 20 20 20 64 6f 75 '9': {. dou
7cd40 62 6c 65 20 72 52 6f 75 6e 64 65 72 3b 0a 20 20 ble rRounder;.
7cd50 20 20 20 20 6e 20 3d 20 67 65 74 56 61 6c 75 65 n = getValue
7cd60 28 7a 2c 20 26 72 29 3b 0a 20 20 20 20 20 20 61 (z, &r);. a
7cd70 73 73 65 72 74 28 20 6e 3e 3d 31 20 29 3b 0a 20 ssert( n>=1 );.
7cd80 20 20 20 20 20 69 66 28 20 7a 5b 6e 5d 3d 3d 27 if( z[n]=='
7cd90 3a 27 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a :' ){. /*
7cda0 20 41 20 6d 6f 64 69 66 69 65 72 20 6f 66 20 74 A modifier of t
7cdb0 68 65 20 66 6f 72 6d 20 28 2b 7c 2d 29 48 48 3a he form (+|-)HH:
7cdc0 4d 4d 3a 53 53 2e 46 46 46 20 61 64 64 73 20 28 MM:SS.FFF adds (
7cdd0 6f 72 20 73 75 62 74 72 61 63 74 73 29 20 74 68 or subtracts) th
7cde0 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 70 65 e. ** spe
7cdf0 63 69 66 69 65 64 20 6e 75 6d 62 65 72 20 6f 66 cified number of
7ce00 20 68 6f 75 72 73 2c 20 6d 69 6e 75 74 65 73 2c hours, minutes,
7ce10 20 73 65 63 6f 6e 64 73 2c 20 61 6e 64 20 66 72 seconds, and fr
7ce20 61 63 74 69 6f 6e 61 6c 20 73 65 63 6f 6e 64 73 actional seconds
7ce30 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 6f 20 74 . ** to t
7ce40 68 65 20 74 69 6d 65 2e 20 20 54 68 65 20 22 2e he time. The ".
7ce50 46 46 46 22 20 6d 61 79 20 62 65 20 6f 6d 69 74 FFF" may be omit
7ce60 74 65 64 2e 20 20 54 68 65 20 22 3a 53 53 2e 46 ted. The ":SS.F
7ce70 46 46 22 20 6d 61 79 20 62 65 0a 20 20 20 20 20 FF" may be.
7ce80 20 20 20 2a 2a 20 6f 6d 69 74 74 65 64 2e 0a 20 ** omitted..
7ce90 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 */.
7cea0 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 32 const char *z2
7ceb0 20 3d 20 7a 3b 0a 20 20 20 20 20 20 20 20 44 61 = z;. Da
7cec0 74 65 54 69 6d 65 20 74 78 3b 0a 20 20 20 20 20 teTime tx;.
7ced0 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 sqlite3_int64
7cee0 20 64 61 79 3b 0a 20 20 20 20 20 20 20 20 69 66 day;. if
7cef0 28 20 21 73 71 6c 69 74 65 33 49 73 64 69 67 69 ( !sqlite3Isdigi
7cf00 74 28 2a 7a 32 29 20 29 20 7a 32 2b 2b 3b 0a 20 t(*z2) ) z2++;.
7cf10 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26 74 memset(&t
7cf20 78 2c 20 30 2c 20 73 69 7a 65 6f 66 28 74 78 29 x, 0, sizeof(tx)
7cf30 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 );. if( p
7cf40 61 72 73 65 48 68 4d 6d 53 73 28 7a 32 2c 20 26 arseHhMmSs(z2, &
7cf50 74 78 29 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 tx) ) break;.
7cf60 20 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 26 computeJD(&
7cf70 74 78 29 3b 0a 20 20 20 20 20 20 20 20 74 78 2e tx);. tx.
7cf80 69 4a 44 20 2d 3d 20 34 33 32 30 30 30 30 30 3b iJD -= 43200000;
7cf90 0a 20 20 20 20 20 20 20 20 64 61 79 20 3d 20 74 . day = t
7cfa0 78 2e 69 4a 44 2f 38 36 34 30 30 30 30 30 3b 0a x.iJD/86400000;.
7cfb0 20 20 20 20 20 20 20 20 74 78 2e 69 4a 44 20 2d tx.iJD -
7cfc0 3d 20 64 61 79 2a 38 36 34 30 30 30 30 30 3b 0a = day*86400000;.
7cfd0 20 20 20 20 20 20 20 20 69 66 28 20 7a 5b 30 5d if( z[0]
7cfe0 3d 3d 27 2d 27 20 29 20 74 78 2e 69 4a 44 20 3d =='-' ) tx.iJD =
7cff0 20 2d 74 78 2e 69 4a 44 3b 0a 20 20 20 20 20 20 -tx.iJD;.
7d000 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a computeJD(p);.
7d010 20 20 20 20 20 20 20 20 63 6c 65 61 72 59 4d 44 clearYMD
7d020 5f 48 4d 53 5f 54 5a 28 70 29 3b 0a 20 20 20 20 _HMS_TZ(p);.
7d030 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 74 78 p->iJD += tx
7d040 2e 69 4a 44 3b 0a 20 20 20 20 20 20 20 20 72 63 .iJD;. rc
7d050 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 62 72 = 0;. br
7d060 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 eak;. }.
7d070 20 20 20 7a 20 2b 3d 20 6e 3b 0a 20 20 20 20 20 z += n;.
7d080 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49 while( sqlite3I
7d090 73 73 70 61 63 65 28 2a 7a 29 20 29 20 7a 2b 2b sspace(*z) ) z++
7d0a0 3b 0a 20 20 20 20 20 20 6e 20 3d 20 73 71 6c 69 ;. n = sqli
7d0b0 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 29 3b 0a te3Strlen30(z);.
7d0c0 20 20 20 20 20 20 69 66 28 20 6e 3e 31 30 20 7c if( n>10 |
7d0d0 7c 20 6e 3c 33 20 29 20 62 72 65 61 6b 3b 0a 20 | n<3 ) break;.
7d0e0 20 20 20 20 20 69 66 28 20 7a 5b 6e 2d 31 5d 3d if( z[n-1]=
7d0f0 3d 27 73 27 20 29 7b 20 7a 5b 6e 2d 31 5d 20 3d ='s' ){ z[n-1] =
7d100 20 30 3b 20 6e 2d 2d 3b 20 7d 0a 20 20 20 20 20 0; n--; }.
7d110 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20 computeJD(p);.
7d120 20 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 rc = 0;.
7d130 20 20 20 72 52 6f 75 6e 64 65 72 20 3d 20 72 3c rRounder = r<
7d140 30 20 3f 20 2d 30 2e 35 20 3a 20 2b 30 2e 35 3b 0 ? -0.5 : +0.5;
7d150 0a 20 20 20 20 20 20 69 66 28 20 6e 3d 3d 33 20 . if( n==3
7d160 26 26 20 73 74 72 63 6d 70 28 7a 2c 22 64 61 79 && strcmp(z,"day
7d170 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 ")==0 ){.
7d180 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 73 71 6c 69 p->iJD += (sqli
7d190 74 65 33 5f 69 6e 74 36 34 29 28 72 2a 38 36 34 te3_int64)(r*864
7d1a0 30 30 30 30 30 2e 30 20 2b 20 72 52 6f 75 6e 64 00000.0 + rRound
7d1b0 65 72 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 er);. }else
7d1c0 20 69 66 28 20 6e 3d 3d 34 20 26 26 20 73 74 72 if( n==4 && str
7d1d0 63 6d 70 28 7a 2c 22 68 6f 75 72 22 29 3d 3d 30 cmp(z,"hour")==0
7d1e0 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 ){. p->i
7d1f0 4a 44 20 2b 3d 20 28 73 71 6c 69 74 65 33 5f 69 JD += (sqlite3_i
7d200 6e 74 36 34 29 28 72 2a 28 38 36 34 30 30 30 30 nt64)(r*(8640000
7d210 30 2e 30 2f 32 34 2e 30 29 20 2b 20 72 52 6f 75 0.0/24.0) + rRou
7d220 6e 64 65 72 29 3b 0a 20 20 20 20 20 20 7d 65 6c nder);. }el
7d230 73 65 20 69 66 28 20 6e 3d 3d 36 20 26 26 20 73 se if( n==6 && s
7d240 74 72 63 6d 70 28 7a 2c 22 6d 69 6e 75 74 65 22 trcmp(z,"minute"
7d250 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 )==0 ){.
7d260 70 2d 3e 69 4a 44 20 2b 3d 20 28 73 71 6c 69 74 p->iJD += (sqlit
7d270 65 33 5f 69 6e 74 36 34 29 28 72 2a 28 38 36 34 e3_int64)(r*(864
7d280 30 30 30 30 30 2e 30 2f 28 32 34 2e 30 2a 36 30 00000.0/(24.0*60
7d290 2e 30 29 29 20 2b 20 72 52 6f 75 6e 64 65 72 29 .0)) + rRounder)
7d2a0 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 ;. }else if
7d2b0 28 20 6e 3d 3d 36 20 26 26 20 73 74 72 63 6d 70 ( n==6 && strcmp
7d2c0 28 7a 2c 22 73 65 63 6f 6e 64 22 29 3d 3d 30 20 (z,"second")==0
7d2d0 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 4a ){. p->iJ
7d2e0 44 20 2b 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e D += (sqlite3_in
7d2f0 74 36 34 29 28 72 2a 28 38 36 34 30 30 30 30 30 t64)(r*(86400000
7d300 2e 30 2f 28 32 34 2e 30 2a 36 30 2e 30 2a 36 30 .0/(24.0*60.0*60
7d310 2e 30 29 29 20 2b 20 72 52 6f 75 6e 64 65 72 29 .0)) + rRounder)
7d320 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 ;. }else if
7d330 28 20 6e 3d 3d 35 20 26 26 20 73 74 72 63 6d 70 ( n==5 && strcmp
7d340 28 7a 2c 22 6d 6f 6e 74 68 22 29 3d 3d 30 20 29 (z,"month")==0 )
7d350 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 78 2c {. int x,
7d360 20 79 3b 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 y;. comp
7d370 75 74 65 59 4d 44 5f 48 4d 53 28 70 29 3b 0a 20 uteYMD_HMS(p);.
7d380 20 20 20 20 20 20 20 70 2d 3e 4d 20 2b 3d 20 28 p->M += (
7d390 69 6e 74 29 72 3b 0a 20 20 20 20 20 20 20 20 78 int)r;. x
7d3a0 20 3d 20 70 2d 3e 4d 3e 30 20 3f 20 28 70 2d 3e = p->M>0 ? (p->
7d3b0 4d 2d 31 29 2f 31 32 20 3a 20 28 70 2d 3e 4d 2d M-1)/12 : (p->M-
7d3c0 31 32 29 2f 31 32 3b 0a 20 20 20 20 20 20 20 20 12)/12;.
7d3d0 70 2d 3e 59 20 2b 3d 20 78 3b 0a 20 20 20 20 20 p->Y += x;.
7d3e0 20 20 20 70 2d 3e 4d 20 2d 3d 20 78 2a 31 32 3b p->M -= x*12;
7d3f0 0a 20 20 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 . p->vali
7d400 64 4a 44 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 dJD = 0;.
7d410 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20 computeJD(p);.
7d420 20 20 20 20 20 20 20 79 20 3d 20 28 69 6e 74 29 y = (int)
7d430 72 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 79 r;. if( y
7d440 21 3d 72 20 29 7b 0a 20 20 20 20 20 20 20 20 20 !=r ){.
7d450 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 73 71 6c 69 p->iJD += (sqli
7d460 74 65 33 5f 69 6e 74 36 34 29 28 28 72 20 2d 20 te3_int64)((r -
7d470 79 29 2a 33 30 2e 30 2a 38 36 34 30 30 30 30 30 y)*30.0*86400000
7d480 2e 30 20 2b 20 72 52 6f 75 6e 64 65 72 29 3b 0a .0 + rRounder);.
7d490 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
7d4a0 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d 34 20 26 }else if( n==4 &
7d4b0 26 20 73 74 72 63 6d 70 28 7a 2c 22 79 65 61 72 & strcmp(z,"year
7d4c0 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 ")==0 ){.
7d4d0 20 69 6e 74 20 79 20 3d 20 28 69 6e 74 29 72 3b int y = (int)r;
7d4e0 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 . compute
7d4f0 59 4d 44 5f 48 4d 53 28 70 29 3b 0a 20 20 20 20 YMD_HMS(p);.
7d500 20 20 20 20 70 2d 3e 59 20 2b 3d 20 79 3b 0a 20 p->Y += y;.
7d510 20 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 4a p->validJ
7d520 44 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 63 D = 0;. c
7d530 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20 20 20 omputeJD(p);.
7d540 20 20 20 20 20 69 66 28 20 79 21 3d 72 20 29 7b if( y!=r ){
7d550 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 69 4a . p->iJ
7d560 44 20 2b 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e D += (sqlite3_in
7d570 74 36 34 29 28 28 72 20 2d 20 79 29 2a 33 36 35 t64)((r - y)*365
7d580 2e 30 2a 38 36 34 30 30 30 30 30 2e 30 20 2b 20 .0*86400000.0 +
7d590 72 52 6f 75 6e 64 65 72 29 3b 0a 20 20 20 20 20 rRounder);.
7d5a0 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 }. }else
7d5b0 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 31 {. rc = 1
7d5c0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
7d5d0 63 6c 65 61 72 59 4d 44 5f 48 4d 53 5f 54 5a 28 clearYMD_HMS_TZ(
7d5e0 70 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b p);. break;
7d5f0 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 . }. defau
7d600 6c 74 3a 20 7b 0a 20 20 20 20 20 20 62 72 65 61 lt: {. brea
7d610 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 k;. }. }. r
7d620 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
7d630 2a 2a 20 50 72 6f 63 65 73 73 20 74 69 6d 65 20 ** Process time
7d640 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e function argumen
7d650 74 73 2e 20 20 61 72 67 76 5b 30 5d 20 69 73 20 ts. argv[0] is
7d660 61 20 64 61 74 65 2d 74 69 6d 65 20 73 74 61 6d a date-time stam
7d670 70 2e 0a 2a 2a 20 61 72 67 76 5b 31 5d 20 61 6e p..** argv[1] an
7d680 64 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 d following are
7d690 6d 6f 64 69 66 69 65 72 73 2e 20 20 50 61 72 73 modifiers. Pars
7d6a0 65 20 74 68 65 6d 20 61 6c 6c 20 61 6e 64 20 77 e them all and w
7d6b0 72 69 74 65 0a 2a 2a 20 74 68 65 20 72 65 73 75 rite.** the resu
7d6c0 6c 74 69 6e 67 20 74 69 6d 65 20 69 6e 74 6f 20 lting time into
7d6d0 74 68 65 20 44 61 74 65 54 69 6d 65 20 73 74 72 the DateTime str
7d6e0 75 63 74 75 72 65 20 70 2e 20 20 52 65 74 75 72 ucture p. Retur
7d6f0 6e 20 30 0a 2a 2a 20 6f 6e 20 73 75 63 63 65 73 n 0.** on succes
7d700 73 20 61 6e 64 20 31 20 69 66 20 74 68 65 72 65 s and 1 if there
7d710 20 61 72 65 20 61 6e 79 20 65 72 72 6f 72 73 2e are any errors.
7d720 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 .**.** If there
7d730 61 72 65 20 7a 65 72 6f 20 70 61 72 61 6d 65 74 are zero paramet
7d740 65 72 73 20 28 69 66 20 65 76 65 6e 20 61 72 67 ers (if even arg
7d750 76 5b 30 5d 20 69 73 20 75 6e 64 65 66 69 6e 65 v[0] is undefine
7d760 64 29 0a 2a 2a 20 74 68 65 6e 20 61 73 73 75 6d d).** then assum
7d770 65 20 61 20 64 65 66 61 75 6c 74 20 76 61 6c 75 e a default valu
7d780 65 20 6f 66 20 22 6e 6f 77 22 20 66 6f 72 20 61 e of "now" for a
7d790 72 67 76 5b 30 5d 2e 0a 2a 2f 0a 73 74 61 74 69 rgv[0]..*/.stati
7d7a0 63 20 69 6e 74 20 69 73 44 61 74 65 28 0a 20 20 c int isDate(.
7d7b0 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 sqlite3_context
7d7c0 2a 63 6f 6e 74 65 78 74 2c 20 0a 20 20 69 6e 74 *context, . int
7d7d0 20 61 72 67 63 2c 20 0a 20 20 73 71 6c 69 74 65 argc, . sqlite
7d7e0 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 2c 20 3_value **argv,
7d7f0 0a 20 20 44 61 74 65 54 69 6d 65 20 2a 70 0a 29 . DateTime *p.)
7d800 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 63 6f 6e {. int i;. con
7d810 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 st unsigned char
7d820 20 2a 7a 3b 0a 20 20 69 6e 74 20 65 54 79 70 65 *z;. int eType
7d830 3b 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c ;. memset(p, 0,
7d840 20 73 69 7a 65 6f 66 28 2a 70 29 29 3b 0a 20 20 sizeof(*p));.
7d850 69 66 28 20 61 72 67 63 3d 3d 30 20 29 7b 0a 20 if( argc==0 ){.
7d860 20 20 20 73 65 74 44 61 74 65 54 69 6d 65 54 6f setDateTimeTo
7d870 43 75 72 72 65 6e 74 28 63 6f 6e 74 65 78 74 2c Current(context,
7d880 20 70 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 p);. }else if(
7d890 20 28 65 54 79 70 65 20 3d 20 73 71 6c 69 74 65 (eType = sqlite
7d8a0 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 3_value_type(arg
7d8b0 76 5b 30 5d 29 29 3d 3d 53 51 4c 49 54 45 5f 46 v[0]))==SQLITE_F
7d8c0 4c 4f 41 54 0a 20 20 20 20 20 20 20 20 20 20 20 LOAT.
7d8d0 20 20 20 20 20 20 20 20 7c 7c 20 65 54 79 70 65 || eType
7d8e0 3d 3d 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 ==SQLITE_INTEGER
7d8f0 20 29 7b 0a 20 20 20 20 70 2d 3e 69 4a 44 20 3d ){. p->iJD =
7d900 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 (sqlite3_int64)
7d910 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 (sqlite3_value_d
7d920 6f 75 62 6c 65 28 61 72 67 76 5b 30 5d 29 2a 38 ouble(argv[0])*8
7d930 36 34 30 30 30 30 30 2e 30 20 2b 20 30 2e 35 29 6400000.0 + 0.5)
7d940 3b 0a 20 20 20 20 70 2d 3e 76 61 6c 69 64 4a 44 ;. p->validJD
7d950 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 = 1;. }else{.
7d960 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 5f 76 z = sqlite3_v
7d970 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 alue_text(argv[0
7d980 5d 29 3b 0a 20 20 20 20 69 66 28 20 21 7a 20 7c ]);. if( !z |
7d990 7c 20 70 61 72 73 65 44 61 74 65 4f 72 54 69 6d | parseDateOrTim
7d9a0 65 28 63 6f 6e 74 65 78 74 2c 20 28 63 68 61 72 e(context, (char
7d9b0 2a 29 7a 2c 20 70 29 20 29 7b 0a 20 20 20 20 20 *)z, p) ){.
7d9c0 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d return 1;. }
7d9d0 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 31 3b 20 . }. for(i=1;
7d9e0 69 3c 61 72 67 63 3b 20 69 2b 2b 29 7b 0a 20 20 i<argc; i++){.
7d9f0 20 20 69 66 28 20 28 7a 20 3d 20 73 71 6c 69 74 if( (z = sqlit
7da00 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 e3_value_text(ar
7da10 67 76 5b 69 5d 29 29 3d 3d 30 20 7c 7c 20 70 61 gv[i]))==0 || pa
7da20 72 73 65 4d 6f 64 69 66 69 65 72 28 28 63 68 61 rseModifier((cha
7da30 72 2a 29 7a 2c 20 70 29 20 29 7b 0a 20 20 20 20 r*)z, p) ){.
7da40 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 return 1;.
7da50 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 }. }. return 0
7da60 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 ;.}.../*.** The
7da70 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e following routin
7da80 65 73 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 es implement the
7da90 20 76 61 72 69 6f 75 73 20 64 61 74 65 20 61 6e various date an
7daa0 64 20 74 69 6d 65 20 66 75 6e 63 74 69 6f 6e 73 d time functions
7dab0 0a 2a 2a 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a .** of SQLite..*
7dac0 2f 0a 0a 2f 2a 0a 2a 2a 20 20 20 20 6a 75 6c 69 /../*.** juli
7dad0 61 6e 64 61 79 28 20 54 49 4d 45 53 54 52 49 4e anday( TIMESTRIN
7dae0 47 2c 20 4d 4f 44 2c 20 4d 4f 44 2c 20 2e 2e 2e G, MOD, MOD, ...
7daf0 29 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ).**.** Return t
7db00 68 65 20 6a 75 6c 69 61 6e 20 64 61 79 20 6e 75 he julian day nu
7db10 6d 62 65 72 20 6f 66 20 74 68 65 20 64 61 74 65 mber of the date
7db20 20 73 70 65 63 69 66 69 65 64 20 69 6e 20 74 68 specified in th
7db30 65 20 61 72 67 75 6d 65 6e 74 73 0a 2a 2f 0a 73 e arguments.*/.s
7db40 74 61 74 69 63 20 76 6f 69 64 20 6a 75 6c 69 61 tatic void julia
7db50 6e 64 61 79 46 75 6e 63 28 0a 20 20 73 71 6c 69 ndayFunc(. sqli
7db60 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e te3_context *con
7db70 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 text,. int argc
7db80 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 ,. sqlite3_valu
7db90 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 44 61 e **argv.){. Da
7dba0 74 65 54 69 6d 65 20 78 3b 0a 20 20 69 66 28 20 teTime x;. if(
7dbb0 69 73 44 61 74 65 28 63 6f 6e 74 65 78 74 2c 20 isDate(context,
7dbc0 61 72 67 63 2c 20 61 72 67 76 2c 20 26 78 29 3d argc, argv, &x)=
7dbd0 3d 30 20 29 7b 0a 20 20 20 20 63 6f 6d 70 75 74 =0 ){. comput
7dbe0 65 4a 44 28 26 78 29 3b 0a 20 20 20 20 73 71 6c eJD(&x);. sql
7dbf0 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 ite3_result_doub
7dc00 6c 65 28 63 6f 6e 74 65 78 74 2c 20 78 2e 69 4a le(context, x.iJ
7dc10 44 2f 38 36 34 30 30 30 30 30 2e 30 29 3b 0a 20 D/86400000.0);.
7dc20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 20 20 20 64 }.}../*.** d
7dc30 61 74 65 74 69 6d 65 28 20 54 49 4d 45 53 54 52 atetime( TIMESTR
7dc40 49 4e 47 2c 20 4d 4f 44 2c 20 4d 4f 44 2c 20 2e ING, MOD, MOD, .
7dc50 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e ..).**.** Return
7dc60 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48 3a 4d YYYY-MM-DD HH:M
7dc70 4d 3a 53 53 0a 2a 2f 0a 73 74 61 74 69 63 20 76 M:SS.*/.static v
7dc80 6f 69 64 20 64 61 74 65 74 69 6d 65 46 75 6e 63 oid datetimeFunc
7dc90 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 (. sqlite3_cont
7dca0 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 ext *context,.
7dcb0 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 int argc,. sqli
7dcc0 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 te3_value **argv
7dcd0 0a 29 7b 0a 20 20 44 61 74 65 54 69 6d 65 20 78 .){. DateTime x
7dce0 3b 0a 20 20 69 66 28 20 69 73 44 61 74 65 28 63 ;. if( isDate(c
7dcf0 6f 6e 74 65 78 74 2c 20 61 72 67 63 2c 20 61 72 ontext, argc, ar
7dd00 67 76 2c 20 26 78 29 3d 3d 30 20 29 7b 0a 20 20 gv, &x)==0 ){.
7dd10 20 20 63 68 61 72 20 7a 42 75 66 5b 31 30 30 5d char zBuf[100]
7dd20 3b 0a 20 20 20 20 63 6f 6d 70 75 74 65 59 4d 44 ;. computeYMD
7dd30 5f 48 4d 53 28 26 78 29 3b 0a 20 20 20 20 73 71 _HMS(&x);. sq
7dd40 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 lite3_snprintf(s
7dd50 69 7a 65 6f 66 28 7a 42 75 66 29 2c 20 7a 42 75 izeof(zBuf), zBu
7dd60 66 2c 20 22 25 30 34 64 2d 25 30 32 64 2d 25 30 f, "%04d-%02d-%0
7dd70 32 64 20 25 30 32 64 3a 25 30 32 64 3a 25 30 32 2d %02d:%02d:%02
7dd80 64 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 d",.
7dd90 20 20 20 20 20 20 20 20 20 78 2e 59 2c 20 78 2e x.Y, x.
7dda0 4d 2c 20 78 2e 44 2c 20 78 2e 68 2c 20 78 2e 6d M, x.D, x.h, x.m
7ddb0 2c 20 28 69 6e 74 29 28 78 2e 73 29 29 3b 0a 20 , (int)(x.s));.
7ddc0 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c sqlite3_resul
7ddd0 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 t_text(context,
7dde0 7a 42 75 66 2c 20 2d 31 2c 20 53 51 4c 49 54 45 zBuf, -1, SQLITE
7ddf0 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 7d _TRANSIENT);. }
7de00 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 20 20 20 74 69 6d .}../*.** tim
7de10 65 28 20 54 49 4d 45 53 54 52 49 4e 47 2c 20 4d e( TIMESTRING, M
7de20 4f 44 2c 20 4d 4f 44 2c 20 2e 2e 2e 29 0a 2a 2a OD, MOD, ...).**
7de30 0a 2a 2a 20 52 65 74 75 72 6e 20 48 48 3a 4d 4d .** Return HH:MM
7de40 3a 53 53 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f :SS.*/.static vo
7de50 69 64 20 74 69 6d 65 46 75 6e 63 28 0a 20 20 73 id timeFunc(. s
7de60 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a qlite3_context *
7de70 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 context,. int a
7de80 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 rgc,. sqlite3_v
7de90 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 alue **argv.){.
7dea0 20 44 61 74 65 54 69 6d 65 20 78 3b 0a 20 20 69 DateTime x;. i
7deb0 66 28 20 69 73 44 61 74 65 28 63 6f 6e 74 65 78 f( isDate(contex
7dec0 74 2c 20 61 72 67 63 2c 20 61 72 67 76 2c 20 26 t, argc, argv, &
7ded0 78 29 3d 3d 30 20 29 7b 0a 20 20 20 20 63 68 61 x)==0 ){. cha
7dee0 72 20 7a 42 75 66 5b 31 30 30 5d 3b 0a 20 20 20 r zBuf[100];.
7def0 20 63 6f 6d 70 75 74 65 48 4d 53 28 26 78 29 3b computeHMS(&x);
7df00 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 . sqlite3_snp
7df10 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 42 75 rintf(sizeof(zBu
7df20 66 29 2c 20 7a 42 75 66 2c 20 22 25 30 32 64 3a f), zBuf, "%02d:
7df30 25 30 32 64 3a 25 30 32 64 22 2c 20 78 2e 68 2c %02d:%02d", x.h,
7df40 20 78 2e 6d 2c 20 28 69 6e 74 29 78 2e 73 29 3b x.m, (int)x.s);
7df50 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 . sqlite3_res
7df60 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 ult_text(context
7df70 2c 20 7a 42 75 66 2c 20 2d 31 2c 20 53 51 4c 49 , zBuf, -1, SQLI
7df80 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 TE_TRANSIENT);.
7df90 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 20 20 20 64 }.}../*.** d
7dfa0 61 74 65 28 20 54 49 4d 45 53 54 52 49 4e 47 2c ate( TIMESTRING,
7dfb0 20 4d 4f 44 2c 20 4d 4f 44 2c 20 2e 2e 2e 29 0a MOD, MOD, ...).
7dfc0 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 59 59 59 **.** Return YYY
7dfd0 59 2d 4d 4d 2d 44 44 0a 2a 2f 0a 73 74 61 74 69 Y-MM-DD.*/.stati
7dfe0 63 20 76 6f 69 64 20 64 61 74 65 46 75 6e 63 28 c void dateFunc(
7dff0 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 . sqlite3_conte
7e000 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 xt *context,. i
7e010 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 nt argc,. sqlit
7e020 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a e3_value **argv.
7e030 29 7b 0a 20 20 44 61 74 65 54 69 6d 65 20 78 3b ){. DateTime x;
7e040 0a 20 20 69 66 28 20 69 73 44 61 74 65 28 63 6f . if( isDate(co
7e050 6e 74 65 78 74 2c 20 61 72 67 63 2c 20 61 72 67 ntext, argc, arg
7e060 76 2c 20 26 78 29 3d 3d 30 20 29 7b 0a 20 20 20 v, &x)==0 ){.
7e070 20 63 68 61 72 20 7a 42 75 66 5b 31 30 30 5d 3b char zBuf[100];
7e080 0a 20 20 20 20 63 6f 6d 70 75 74 65 59 4d 44 28 . computeYMD(
7e090 26 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 &x);. sqlite3
7e0a0 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 _snprintf(sizeof
7e0b0 28 7a 42 75 66 29 2c 20 7a 42 75 66 2c 20 22 25 (zBuf), zBuf, "%
7e0c0 30 34 64 2d 25 30 32 64 2d 25 30 32 64 22 2c 20 04d-%02d-%02d",
7e0d0 78 2e 59 2c 20 78 2e 4d 2c 20 78 2e 44 29 3b 0a x.Y, x.M, x.D);.
7e0e0 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 sqlite3_resu
7e0f0 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c lt_text(context,
7e100 20 7a 42 75 66 2c 20 2d 31 2c 20 53 51 4c 49 54 zBuf, -1, SQLIT
7e110 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 E_TRANSIENT);.
7e120 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 20 20 20 73 74 }.}../*.** st
7e130 72 66 74 69 6d 65 28 20 46 4f 52 4d 41 54 2c 20 rftime( FORMAT,
7e140 54 49 4d 45 53 54 52 49 4e 47 2c 20 4d 4f 44 2c TIMESTRING, MOD,
7e150 20 4d 4f 44 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a MOD, ...).**.**
7e160 20 52 65 74 75 72 6e 20 61 20 73 74 72 69 6e 67 Return a string
7e170 20 64 65 73 63 72 69 62 65 64 20 62 79 20 46 4f described by FO
7e180 52 4d 41 54 2e 20 20 43 6f 6e 76 65 72 73 69 6f RMAT. Conversio
7e190 6e 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a ns as follows:.*
7e1a0 2a 0a 2a 2a 20 20 20 25 64 20 20 64 61 79 20 6f *.** %d day o
7e1b0 66 20 6d 6f 6e 74 68 0a 2a 2a 20 20 20 25 66 20 f month.** %f
7e1c0 20 2a 2a 20 66 72 61 63 74 69 6f 6e 61 6c 20 73 ** fractional s
7e1d0 65 63 6f 6e 64 73 20 20 53 53 2e 53 53 53 0a 2a econds SS.SSS.*
7e1e0 2a 20 20 20 25 48 20 20 68 6f 75 72 20 30 30 2d * %H hour 00-
7e1f0 32 34 0a 2a 2a 20 20 20 25 6a 20 20 64 61 79 20 24.** %j day
7e200 6f 66 20 79 65 61 72 20 30 30 30 2d 33 36 36 0a of year 000-366.
7e210 2a 2a 20 20 20 25 4a 20 20 2a 2a 20 4a 75 6c 69 ** %J ** Juli
7e220 61 6e 20 64 61 79 20 6e 75 6d 62 65 72 0a 2a 2a an day number.**
7e230 20 20 20 25 6d 20 20 6d 6f 6e 74 68 20 30 31 2d %m month 01-
7e240 31 32 0a 2a 2a 20 20 20 25 4d 20 20 6d 69 6e 75 12.** %M minu
7e250 74 65 20 30 30 2d 35 39 0a 2a 2a 20 20 20 25 73 te 00-59.** %s
7e260 20 20 73 65 63 6f 6e 64 73 20 73 69 6e 63 65 20 seconds since
7e270 31 39 37 30 2d 30 31 2d 30 31 0a 2a 2a 20 20 20 1970-01-01.**
7e280 25 53 20 20 73 65 63 6f 6e 64 73 20 30 30 2d 35 %S seconds 00-5
7e290 39 0a 2a 2a 20 20 20 25 77 20 20 64 61 79 20 6f 9.** %w day o
7e2a0 66 20 77 65 65 6b 20 30 2d 36 20 20 73 75 6e 64 f week 0-6 sund
7e2b0 61 79 3d 3d 30 0a 2a 2a 20 20 20 25 57 20 20 77 ay==0.** %W w
7e2c0 65 65 6b 20 6f 66 20 79 65 61 72 20 30 30 2d 35 eek of year 00-5
7e2d0 33 0a 2a 2a 20 20 20 25 59 20 20 79 65 61 72 20 3.** %Y year
7e2e0 30 30 30 30 2d 39 39 39 39 0a 2a 2a 20 20 20 25 0000-9999.** %
7e2f0 25 20 20 25 0a 2a 2f 0a 73 74 61 74 69 63 20 76 % %.*/.static v
7e300 6f 69 64 20 73 74 72 66 74 69 6d 65 46 75 6e 63 oid strftimeFunc
7e310 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 (. sqlite3_cont
7e320 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 ext *context,.
7e330 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 int argc,. sqli
7e340 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 te3_value **argv
7e350 0a 29 7b 0a 20 20 44 61 74 65 54 69 6d 65 20 78 .){. DateTime x
7e360 3b 0a 20 20 75 36 34 20 6e 3b 0a 20 20 73 69 7a ;. u64 n;. siz
7e370 65 5f 74 20 69 2c 6a 3b 0a 20 20 63 68 61 72 20 e_t i,j;. char
7e380 2a 7a 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 *z;. sqlite3 *d
7e390 62 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 b;. const char
7e3a0 2a 7a 46 6d 74 20 3d 20 28 63 6f 6e 73 74 20 63 *zFmt = (const c
7e3b0 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c har*)sqlite3_val
7e3c0 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 ue_text(argv[0])
7e3d0 3b 0a 20 20 63 68 61 72 20 7a 42 75 66 5b 31 30 ;. char zBuf[10
7e3e0 30 5d 3b 0a 20 20 69 66 28 20 7a 46 6d 74 3d 3d 0];. if( zFmt==
7e3f0 30 20 7c 7c 20 69 73 44 61 74 65 28 63 6f 6e 74 0 || isDate(cont
7e400 65 78 74 2c 20 61 72 67 63 2d 31 2c 20 61 72 67 ext, argc-1, arg
7e410 76 2b 31 2c 20 26 78 29 20 29 20 72 65 74 75 72 v+1, &x) ) retur
7e420 6e 3b 0a 20 20 64 62 20 3d 20 73 71 6c 69 74 65 n;. db = sqlite
7e430 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 3_context_db_han
7e440 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 dle(context);.
7e450 66 6f 72 28 69 3d 30 2c 20 6e 3d 31 3b 20 7a 46 for(i=0, n=1; zF
7e460 6d 74 5b 69 5d 3b 20 69 2b 2b 2c 20 6e 2b 2b 29 mt[i]; i++, n++)
7e470 7b 0a 20 20 20 20 69 66 28 20 7a 46 6d 74 5b 69 {. if( zFmt[i
7e480 5d 3d 3d 27 25 27 20 29 7b 0a 20 20 20 20 20 20 ]=='%' ){.
7e490 73 77 69 74 63 68 28 20 7a 46 6d 74 5b 69 2b 31 switch( zFmt[i+1
7e4a0 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 63 61 73 ] ){. cas
7e4b0 65 20 27 64 27 3a 0a 20 20 20 20 20 20 20 20 63 e 'd':. c
7e4c0 61 73 65 20 27 48 27 3a 0a 20 20 20 20 20 20 20 ase 'H':.
7e4d0 20 63 61 73 65 20 27 6d 27 3a 0a 20 20 20 20 20 case 'm':.
7e4e0 20 20 20 63 61 73 65 20 27 4d 27 3a 0a 20 20 20 case 'M':.
7e4f0 20 20 20 20 20 63 61 73 65 20 27 53 27 3a 0a 20 case 'S':.
7e500 20 20 20 20 20 20 20 63 61 73 65 20 27 57 27 3a case 'W':
7e510 0a 20 20 20 20 20 20 20 20 20 20 6e 2b 2b 3b 0a . n++;.
7e520 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66 61 6c /* fal
7e530 6c 20 74 68 72 75 20 2a 2f 0a 20 20 20 20 20 20 l thru */.
7e540 20 20 63 61 73 65 20 27 77 27 3a 0a 20 20 20 20 case 'w':.
7e550 20 20 20 20 63 61 73 65 20 27 25 27 3a 0a 20 20 case '%':.
7e560 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
7e570 20 20 20 20 20 20 20 63 61 73 65 20 27 66 27 3a case 'f':
7e580 0a 20 20 20 20 20 20 20 20 20 20 6e 20 2b 3d 20 . n +=
7e590 38 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 8;. bre
7e5a0 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 ak;. case
7e5b0 20 27 6a 27 3a 0a 20 20 20 20 20 20 20 20 20 20 'j':.
7e5c0 6e 20 2b 3d 20 33 3b 0a 20 20 20 20 20 20 20 20 n += 3;.
7e5d0 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 break;.
7e5e0 20 63 61 73 65 20 27 59 27 3a 0a 20 20 20 20 20 case 'Y':.
7e5f0 20 20 20 20 20 6e 20 2b 3d 20 38 3b 0a 20 20 20 n += 8;.
7e600 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
7e610 20 20 20 20 20 20 63 61 73 65 20 27 73 27 3a 0a case 's':.
7e620 20 20 20 20 20 20 20 20 63 61 73 65 20 27 4a 27 case 'J'
7e630 3a 0a 20 20 20 20 20 20 20 20 20 20 6e 20 2b 3d :. n +=
7e640 20 35 30 3b 0a 20 20 20 20 20 20 20 20 20 20 62 50;. b
7e650 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 64 65 reak;. de
7e660 66 61 75 6c 74 3a 0a 20 20 20 20 20 20 20 20 20 fault:.
7e670 20 72 65 74 75 72 6e 3b 20 20 2f 2a 20 45 52 52 return; /* ERR
7e680 4f 52 2e 20 20 72 65 74 75 72 6e 20 61 20 4e 55 OR. return a NU
7e690 4c 4c 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20 LL */. }.
7e6a0 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 7d 0a 20 i++;. }.
7e6b0 20 7d 0a 20 20 74 65 73 74 63 61 73 65 28 20 6e }. testcase( n
7e6c0 3d 3d 73 69 7a 65 6f 66 28 7a 42 75 66 29 2d 31 ==sizeof(zBuf)-1
7e6d0 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 );. testcase(
7e6e0 6e 3d 3d 73 69 7a 65 6f 66 28 7a 42 75 66 29 20 n==sizeof(zBuf)
7e6f0 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 6e );. testcase( n
7e700 3d 3d 28 75 36 34 29 64 62 2d 3e 61 4c 69 6d 69 ==(u64)db->aLimi
7e710 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c t[SQLITE_LIMIT_L
7e720 45 4e 47 54 48 5d 2b 31 20 29 3b 0a 20 20 74 65 ENGTH]+1 );. te
7e730 73 74 63 61 73 65 28 20 6e 3d 3d 28 75 36 34 29 stcase( n==(u64)
7e740 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 db->aLimit[SQLIT
7e750 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 E_LIMIT_LENGTH]
7e760 29 3b 0a 20 20 69 66 28 20 6e 3c 73 69 7a 65 6f );. if( n<sizeo
7e770 66 28 7a 42 75 66 29 20 29 7b 0a 20 20 20 20 7a f(zBuf) ){. z
7e780 20 3d 20 7a 42 75 66 3b 0a 20 20 7d 65 6c 73 65 = zBuf;. }else
7e790 20 69 66 28 20 6e 3e 28 75 36 34 29 64 62 2d 3e if( n>(u64)db->
7e7a0 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 aLimit[SQLITE_LI
7e7b0 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 MIT_LENGTH] ){.
7e7c0 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c sqlite3_resul
7e7d0 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67 28 63 t_error_toobig(c
7e7e0 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 72 65 74 ontext);. ret
7e7f0 75 72 6e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 urn;. }else{.
7e800 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 44 62 4d z = sqlite3DbM
7e810 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 28 69 6e allocRaw(db, (in
7e820 74 29 6e 29 3b 0a 20 20 20 20 69 66 28 20 7a 3d t)n);. if( z=
7e830 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 =0 ){. sqli
7e840 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 te3_result_error
7e850 5f 6e 6f 6d 65 6d 28 63 6f 6e 74 65 78 74 29 3b _nomem(context);
7e860 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 . return;.
7e870 20 20 20 7d 0a 20 20 7d 0a 20 20 63 6f 6d 70 75 }. }. compu
7e880 74 65 4a 44 28 26 78 29 3b 0a 20 20 63 6f 6d 70 teJD(&x);. comp
7e890 75 74 65 59 4d 44 5f 48 4d 53 28 26 78 29 3b 0a uteYMD_HMS(&x);.
7e8a0 20 20 66 6f 72 28 69 3d 6a 3d 30 3b 20 7a 46 6d for(i=j=0; zFm
7e8b0 74 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 t[i]; i++){.
7e8c0 69 66 28 20 7a 46 6d 74 5b 69 5d 21 3d 27 25 27 if( zFmt[i]!='%'
7e8d0 20 29 7b 0a 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d ){. z[j++]
7e8e0 20 3d 20 7a 46 6d 74 5b 69 5d 3b 0a 20 20 20 20 = zFmt[i];.
7e8f0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 2b 2b }else{. i++
7e900 3b 0a 20 20 20 20 20 20 73 77 69 74 63 68 28 20 ;. switch(
7e910 7a 46 6d 74 5b 69 5d 20 29 7b 0a 20 20 20 20 20 zFmt[i] ){.
7e920 20 20 20 63 61 73 65 20 27 64 27 3a 20 20 73 71 case 'd': sq
7e930 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 33 lite3_snprintf(3
7e940 2c 20 26 7a 5b 6a 5d 2c 22 25 30 32 64 22 2c 78 , &z[j],"%02d",x
7e950 2e 44 29 3b 20 6a 2b 3d 32 3b 20 62 72 65 61 6b .D); j+=2; break
7e960 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 ;. case '
7e970 66 27 3a 20 7b 0a 20 20 20 20 20 20 20 20 20 20 f': {.
7e980 64 6f 75 62 6c 65 20 73 20 3d 20 78 2e 73 3b 0a double s = x.s;.
7e990 20 20 20 20 20 20 20 20 20 20 69 66 28 20 73 3e if( s>
7e9a0 35 39 2e 39 39 39 20 29 20 73 20 3d 20 35 39 2e 59.999 ) s = 59.
7e9b0 39 39 39 3b 0a 20 20 20 20 20 20 20 20 20 20 73 999;. s
7e9c0 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
7e9d0 37 2c 20 26 7a 5b 6a 5d 2c 22 25 30 36 2e 33 66 7, &z[j],"%06.3f
7e9e0 22 2c 20 73 29 3b 0a 20 20 20 20 20 20 20 20 20 ", s);.
7e9f0 20 6a 20 2b 3d 20 73 71 6c 69 74 65 33 53 74 72 j += sqlite3Str
7ea00 6c 65 6e 33 30 28 26 7a 5b 6a 5d 29 3b 0a 20 20 len30(&z[j]);.
7ea10 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
7ea20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
7ea30 20 63 61 73 65 20 27 48 27 3a 20 20 73 71 6c 69 case 'H': sqli
7ea40 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 33 2c 20 te3_snprintf(3,
7ea50 26 7a 5b 6a 5d 2c 22 25 30 32 64 22 2c 78 2e 68 &z[j],"%02d",x.h
7ea60 29 3b 20 6a 2b 3d 32 3b 20 62 72 65 61 6b 3b 0a ); j+=2; break;.
7ea70 20 20 20 20 20 20 20 20 63 61 73 65 20 27 57 27 case 'W'
7ea80 3a 20 2f 2a 20 46 61 6c 6c 20 74 68 72 75 20 2a : /* Fall thru *
7ea90 2f 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 /. case '
7eaa0 6a 27 3a 20 7b 0a 20 20 20 20 20 20 20 20 20 20 j': {.
7eab0 69 6e 74 20 6e 44 61 79 3b 20 20 20 20 20 20 20 int nDay;
7eac0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
7ead0 6f 66 20 64 61 79 73 20 73 69 6e 63 65 20 31 73 of days since 1s
7eae0 74 20 64 61 79 20 6f 66 20 79 65 61 72 20 2a 2f t day of year */
7eaf0 0a 20 20 20 20 20 20 20 20 20 20 44 61 74 65 54 . DateT
7eb00 69 6d 65 20 79 20 3d 20 78 3b 0a 20 20 20 20 20 ime y = x;.
7eb10 20 20 20 20 20 79 2e 76 61 6c 69 64 4a 44 20 3d y.validJD =
7eb20 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 79 2e 0;. y.
7eb30 4d 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 M = 1;.
7eb40 20 79 2e 44 20 3d 20 31 3b 0a 20 20 20 20 20 20 y.D = 1;.
7eb50 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 26 79 computeJD(&y
7eb60 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6e 44 61 );. nDa
7eb70 79 20 3d 20 28 69 6e 74 29 28 28 78 2e 69 4a 44 y = (int)((x.iJD
7eb80 2d 79 2e 69 4a 44 2b 34 33 32 30 30 30 30 30 29 -y.iJD+43200000)
7eb90 2f 38 36 34 30 30 30 30 30 29 3b 0a 20 20 20 20 /86400000);.
7eba0 20 20 20 20 20 20 69 66 28 20 7a 46 6d 74 5b 69 if( zFmt[i
7ebb0 5d 3d 3d 27 57 27 20 29 7b 0a 20 20 20 20 20 20 ]=='W' ){.
7ebc0 20 20 20 20 20 20 69 6e 74 20 77 64 3b 20 20 20 int wd;
7ebd0 2f 2a 20 30 3d 4d 6f 6e 64 61 79 2c 20 31 3d 54 /* 0=Monday, 1=T
7ebe0 75 65 73 64 61 79 2c 20 2e 2e 2e 20 36 3d 53 75 uesday, ... 6=Su
7ebf0 6e 64 61 79 20 2a 2f 0a 20 20 20 20 20 20 20 20 nday */.
7ec00 20 20 20 20 77 64 20 3d 20 28 69 6e 74 29 28 28 wd = (int)((
7ec10 28 78 2e 69 4a 44 2b 34 33 32 30 30 30 30 30 29 (x.iJD+43200000)
7ec20 2f 38 36 34 30 30 30 30 30 29 25 37 29 3b 0a 20 /86400000)%7);.
7ec30 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 sqlit
7ec40 65 33 5f 73 6e 70 72 69 6e 74 66 28 33 2c 20 26 e3_snprintf(3, &
7ec50 7a 5b 6a 5d 2c 22 25 30 32 64 22 2c 28 6e 44 61 z[j],"%02d",(nDa
7ec60 79 2b 37 2d 77 64 29 2f 37 29 3b 0a 20 20 20 20 y+7-wd)/7);.
7ec70 20 20 20 20 20 20 20 20 6a 20 2b 3d 20 32 3b 0a j += 2;.
7ec80 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b }else{
7ec90 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c . sql
7eca0 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 34 2c ite3_snprintf(4,
7ecb0 20 26 7a 5b 6a 5d 2c 22 25 30 33 64 22 2c 6e 44 &z[j],"%03d",nD
7ecc0 61 79 2b 31 29 3b 0a 20 20 20 20 20 20 20 20 20 ay+1);.
7ecd0 20 20 20 6a 20 2b 3d 20 33 3b 0a 20 20 20 20 20 j += 3;.
7ece0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 }.
7ecf0 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 break;.
7ed00 7d 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 }. case '
7ed10 4a 27 3a 20 7b 0a 20 20 20 20 20 20 20 20 20 20 J': {.
7ed20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
7ed30 28 32 30 2c 20 26 7a 5b 6a 5d 2c 22 25 2e 31 36 (20, &z[j],"%.16
7ed40 67 22 2c 78 2e 69 4a 44 2f 38 36 34 30 30 30 30 g",x.iJD/8640000
7ed50 30 2e 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 0.0);.
7ed60 6a 2b 3d 73 71 6c 69 74 65 33 53 74 72 6c 65 6e j+=sqlite3Strlen
7ed70 33 30 28 26 7a 5b 6a 5d 29 3b 0a 20 20 20 20 20 30(&z[j]);.
7ed80 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
7ed90 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 63 61 }. ca
7eda0 73 65 20 27 6d 27 3a 20 20 73 71 6c 69 74 65 33 se 'm': sqlite3
7edb0 5f 73 6e 70 72 69 6e 74 66 28 33 2c 20 26 7a 5b _snprintf(3, &z[
7edc0 6a 5d 2c 22 25 30 32 64 22 2c 78 2e 4d 29 3b 20 j],"%02d",x.M);
7edd0 6a 2b 3d 32 3b 20 62 72 65 61 6b 3b 0a 20 20 20 j+=2; break;.
7ede0 20 20 20 20 20 63 61 73 65 20 27 4d 27 3a 20 20 case 'M':
7edf0 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
7ee00 28 33 2c 20 26 7a 5b 6a 5d 2c 22 25 30 32 64 22 (3, &z[j],"%02d"
7ee10 2c 78 2e 6d 29 3b 20 6a 2b 3d 32 3b 20 62 72 65 ,x.m); j+=2; bre
7ee20 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 ak;. case
7ee30 20 27 73 27 3a 20 7b 0a 20 20 20 20 20 20 20 20 's': {.
7ee40 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e sqlite3_snprin
7ee50 74 66 28 33 30 2c 26 7a 5b 6a 5d 2c 22 25 6c 6c tf(30,&z[j],"%ll
7ee60 64 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 d",.
7ee70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 (
7ee80 69 36 34 29 28 78 2e 69 4a 44 2f 31 30 30 30 20 i64)(x.iJD/1000
7ee90 2d 20 32 31 30 38 36 36 37 36 2a 28 69 36 34 29 - 21086676*(i64)
7eea0 31 30 30 30 30 29 29 3b 0a 20 20 20 20 20 20 20 10000));.
7eeb0 20 20 20 6a 20 2b 3d 20 73 71 6c 69 74 65 33 53 j += sqlite3S
7eec0 74 72 6c 65 6e 33 30 28 26 7a 5b 6a 5d 29 3b 0a trlen30(&z[j]);.
7eed0 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b break;
7eee0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
7eef0 20 20 20 63 61 73 65 20 27 53 27 3a 20 20 73 71 case 'S': sq
7ef00 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 33 lite3_snprintf(3
7ef10 2c 26 7a 5b 6a 5d 2c 22 25 30 32 64 22 2c 28 69 ,&z[j],"%02d",(i
7ef20 6e 74 29 78 2e 73 29 3b 20 6a 2b 3d 32 3b 20 62 nt)x.s); j+=2; b
7ef30 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 reak;. ca
7ef40 73 65 20 27 77 27 3a 20 7b 0a 20 20 20 20 20 20 se 'w': {.
7ef50 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 28 63 68 z[j++] = (ch
7ef60 61 72 29 28 28 28 78 2e 69 4a 44 2b 31 32 39 36 ar)(((x.iJD+1296
7ef70 30 30 30 30 30 29 2f 38 36 34 30 30 30 30 30 29 00000)/86400000)
7ef80 20 25 20 37 29 20 2b 20 27 30 27 3b 0a 20 20 20 % 7) + '0';.
7ef90 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
7efa0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
7efb0 63 61 73 65 20 27 59 27 3a 20 7b 0a 20 20 20 20 case 'Y': {.
7efc0 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e sqlite3_sn
7efd0 70 72 69 6e 74 66 28 35 2c 26 7a 5b 6a 5d 2c 22 printf(5,&z[j],"
7efe0 25 30 34 64 22 2c 78 2e 59 29 3b 20 6a 2b 3d 73 %04d",x.Y); j+=s
7eff0 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 26 qlite3Strlen30(&
7f000 7a 5b 6a 5d 29 3b 0a 20 20 20 20 20 20 20 20 20 z[j]);.
7f010 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 break;.
7f020 7d 0a 20 20 20 20 20 20 20 20 64 65 66 61 75 6c }. defaul
7f030 74 3a 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 27 25 t: z[j++] = '%
7f040 27 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 '; break;.
7f050 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 7a 5b }. }. }. z[
7f060 6a 5d 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 j] = 0;. sqlite
7f070 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 3_result_text(co
7f080 6e 74 65 78 74 2c 20 7a 2c 20 2d 31 2c 0a 20 20 ntext, z, -1,.
7f090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7f0a0 20 20 20 20 7a 3d 3d 7a 42 75 66 20 3f 20 53 51 z==zBuf ? SQ
7f0b0 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 20 3a LITE_TRANSIENT :
7f0c0 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 29 SQLITE_DYNAMIC)
7f0d0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 63 75 72 72 65 ;.}../*.** curre
7f0e0 6e 74 5f 74 69 6d 65 28 29 0a 2a 2a 0a 2a 2a 20 nt_time().**.**
7f0f0 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 This function re
7f100 74 75 72 6e 73 20 74 68 65 20 73 61 6d 65 20 76 turns the same v
7f110 61 6c 75 65 20 61 73 20 74 69 6d 65 28 27 6e 6f alue as time('no
7f120 77 27 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 w')..*/.static v
7f130 6f 69 64 20 63 74 69 6d 65 46 75 6e 63 28 0a 20 oid ctimeFunc(.
7f140 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 sqlite3_context
7f150 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 *context,. int
7f160 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c 69 NotUsed,. sqli
7f170 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f 74 55 te3_value **NotU
7f180 73 65 64 32 0a 29 7b 0a 20 20 55 4e 55 53 45 44 sed2.){. UNUSED
7f190 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 _PARAMETER2(NotU
7f1a0 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a sed, NotUsed2);.
7f1b0 20 20 74 69 6d 65 46 75 6e 63 28 63 6f 6e 74 65 timeFunc(conte
7f1c0 78 74 2c 20 30 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a xt, 0, 0);.}../*
7f1d0 0a 2a 2a 20 63 75 72 72 65 6e 74 5f 64 61 74 65 .** current_date
7f1e0 28 29 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 ().**.** This fu
7f1f0 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 nction returns t
7f200 68 65 20 73 61 6d 65 20 76 61 6c 75 65 20 61 73 he same value as
7f210 20 64 61 74 65 28 27 6e 6f 77 27 29 2e 0a 2a 2f date('now')..*/
7f220 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 64 61 .static void cda
7f230 74 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 teFunc(. sqlite
7f240 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 3_context *conte
7f250 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 xt,. int NotUse
7f260 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c d,. sqlite3_val
7f270 75 65 20 2a 2a 4e 6f 74 55 73 65 64 32 0a 29 7b ue **NotUsed2.){
7f280 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
7f290 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f TER2(NotUsed, No
7f2a0 74 55 73 65 64 32 29 3b 0a 20 20 64 61 74 65 46 tUsed2);. dateF
7f2b0 75 6e 63 28 63 6f 6e 74 65 78 74 2c 20 30 2c 20 unc(context, 0,
7f2c0 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 63 75 72 0);.}../*.** cur
7f2d0 72 65 6e 74 5f 74 69 6d 65 73 74 61 6d 70 28 29 rent_timestamp()
7f2e0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 .**.** This func
7f2f0 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65 tion returns the
7f300 20 73 61 6d 65 20 76 61 6c 75 65 20 61 73 20 64 same value as d
7f310 61 74 65 74 69 6d 65 28 27 6e 6f 77 27 29 2e 0a atetime('now')..
7f320 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 */.static void c
7f330 74 69 6d 65 73 74 61 6d 70 46 75 6e 63 28 0a 20 timestampFunc(.
7f340 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 sqlite3_context
7f350 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 *context,. int
7f360 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c 69 NotUsed,. sqli
7f370 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f 74 55 te3_value **NotU
7f380 73 65 64 32 0a 29 7b 0a 20 20 55 4e 55 53 45 44 sed2.){. UNUSED
7f390 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 _PARAMETER2(NotU
7f3a0 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a sed, NotUsed2);.
7f3b0 20 20 64 61 74 65 74 69 6d 65 46 75 6e 63 28 63 datetimeFunc(c
7f3c0 6f 6e 74 65 78 74 2c 20 30 2c 20 30 29 3b 0a 7d ontext, 0, 0);.}
7f3d0 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 .#endif /* !defi
7f3e0 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f ned(SQLITE_OMIT_
7f3f0 44 41 54 45 54 49 4d 45 5f 46 55 4e 43 53 29 20 DATETIME_FUNCS)
7f400 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 */..#ifdef SQLIT
7f410 45 5f 4f 4d 49 54 5f 44 41 54 45 54 49 4d 45 5f E_OMIT_DATETIME_
7f420 46 55 4e 43 53 0a 2f 2a 0a 2a 2a 20 49 66 20 74 FUNCS./*.** If t
7f430 68 65 20 6c 69 62 72 61 72 79 20 69 73 20 63 6f he library is co
7f440 6d 70 69 6c 65 64 20 74 6f 20 6f 6d 69 74 20 74 mpiled to omit t
7f450 68 65 20 66 75 6c 6c 2d 73 63 61 6c 65 20 64 61 he full-scale da
7f460 74 65 20 61 6e 64 20 74 69 6d 65 0a 2a 2a 20 68 te and time.** h
7f470 61 6e 64 6c 69 6e 67 20 28 74 6f 20 67 65 74 20 andling (to get
7f480 61 20 73 6d 61 6c 6c 65 72 20 62 69 6e 61 72 79 a smaller binary
7f490 29 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 ), the following
7f4a0 20 6d 69 6e 69 6d 61 6c 20 76 65 72 73 69 6f 6e minimal version
7f4b0 0a 2a 2a 20 6f 66 20 74 68 65 20 66 75 6e 63 74 .** of the funct
7f4c0 69 6f 6e 73 20 63 75 72 72 65 6e 74 5f 74 69 6d ions current_tim
7f4d0 65 28 29 2c 20 63 75 72 72 65 6e 74 5f 64 61 74 e(), current_dat
7f4e0 65 28 29 20 61 6e 64 20 63 75 72 72 65 6e 74 5f e() and current_
7f4f0 74 69 6d 65 73 74 61 6d 70 28 29 0a 2a 2a 20 61 timestamp().** a
7f500 72 65 20 69 6e 63 6c 75 64 65 64 20 69 6e 73 74 re included inst
7f510 65 61 64 2e 20 54 68 69 73 20 69 73 20 74 6f 20 ead. This is to
7f520 73 75 70 70 6f 72 74 20 63 6f 6c 75 6d 6e 20 64 support column d
7f530 65 63 6c 61 72 61 74 69 6f 6e 73 20 74 68 61 74 eclarations that
7f540 0a 2a 2a 20 69 6e 63 6c 75 64 65 20 22 44 45 46 .** include "DEF
7f550 41 55 4c 54 20 43 55 52 52 45 4e 54 5f 54 49 4d AULT CURRENT_TIM
7f560 45 22 20 65 74 63 2e 0a 2a 2a 0a 2a 2a 20 54 68 E" etc..**.** Th
7f570 69 73 20 66 75 6e 63 74 69 6f 6e 20 75 73 65 73 is function uses
7f580 20 74 68 65 20 43 2d 6c 69 62 72 61 72 79 20 66 the C-library f
7f590 75 6e 63 74 69 6f 6e 73 20 74 69 6d 65 28 29 2c unctions time(),
7f5a0 20 67 6d 74 69 6d 65 28 29 0a 2a 2a 20 61 6e 64 gmtime().** and
7f5b0 20 73 74 72 66 74 69 6d 65 28 29 2e 20 54 68 65 strftime(). The
7f5c0 20 66 6f 72 6d 61 74 20 73 74 72 69 6e 67 20 74 format string t
7f5d0 6f 20 70 61 73 73 20 74 6f 20 73 74 72 66 74 69 o pass to strfti
7f5e0 6d 65 28 29 20 69 73 20 73 75 70 70 6c 69 65 64 me() is supplied
7f5f0 0a 2a 2a 20 61 73 20 74 68 65 20 75 73 65 72 2d .** as the user-
7f600 64 61 74 61 20 66 6f 72 20 74 68 65 20 66 75 6e data for the fun
7f610 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 ction..*/.static
7f620 20 76 6f 69 64 20 63 75 72 72 65 6e 74 54 69 6d void currentTim
7f630 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 eFunc(. sqlite3
7f640 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 _context *contex
7f650 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 t,. int argc,.
7f660 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a sqlite3_value *
7f670 2a 61 72 67 76 0a 29 7b 0a 20 20 74 69 6d 65 5f *argv.){. time_
7f680 74 20 74 3b 0a 20 20 63 68 61 72 20 2a 7a 46 6f t t;. char *zFo
7f690 72 6d 61 74 20 3d 20 28 63 68 61 72 20 2a 29 73 rmat = (char *)s
7f6a0 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61 qlite3_user_data
7f6b0 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 73 71 6c (context);. sql
7f6c0 69 74 65 33 20 2a 64 62 3b 0a 20 20 64 6f 75 62 ite3 *db;. doub
7f6d0 6c 65 20 72 54 3b 0a 20 20 63 68 61 72 20 7a 42 le rT;. char zB
7f6e0 75 66 5b 32 30 5d 3b 0a 0a 20 20 55 4e 55 53 45 uf[20];.. UNUSE
7f6f0 44 5f 50 41 52 41 4d 45 54 45 52 28 61 72 67 63 D_PARAMETER(argc
7f700 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 );. UNUSED_PARA
7f710 4d 45 54 45 52 28 61 72 67 76 29 3b 0a 0a 20 20 METER(argv);..
7f720 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e db = sqlite3_con
7f730 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 text_db_handle(c
7f740 6f 6e 74 65 78 74 29 3b 0a 20 20 73 71 6c 69 74 ontext);. sqlit
7f750 65 33 4f 73 43 75 72 72 65 6e 74 54 69 6d 65 28 e3OsCurrentTime(
7f760 64 62 2d 3e 70 56 66 73 2c 20 26 72 54 29 3b 0a db->pVfs, &rT);.
7f770 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
7f780 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 MIT_FLOATING_POI
7f790 4e 54 0a 20 20 74 20 3d 20 38 36 34 30 30 2e 30 NT. t = 86400.0
7f7a0 2a 28 72 54 20 2d 20 32 34 34 30 35 38 37 2e 35 *(rT - 2440587.5
7f7b0 29 20 2b 20 30 2e 35 3b 0a 23 65 6c 73 65 0a 20 ) + 0.5;.#else.
7f7c0 20 2f 2a 20 77 69 74 68 6f 75 74 20 66 6c 6f 61 /* without floa
7f7d0 74 69 6e 67 20 70 6f 69 6e 74 20 73 75 70 70 6f ting point suppo
7f7e0 72 74 2c 20 72 54 20 77 69 6c 6c 20 68 61 76 65 rt, rT will have
7f7f0 0a 20 20 2a 2a 20 61 6c 72 65 61 64 79 20 6c 6f . ** already lo
7f800 73 74 20 66 72 61 63 74 69 6f 6e 61 6c 20 64 61 st fractional da
7f810 79 20 70 72 65 63 69 73 69 6f 6e 2e 0a 20 20 2a y precision.. *
7f820 2f 0a 20 20 74 20 3d 20 38 36 34 30 30 20 2a 20 /. t = 86400 *
7f830 28 72 54 20 2d 20 32 34 34 30 35 38 37 29 20 2d (rT - 2440587) -
7f840 20 34 33 32 30 30 3b 0a 23 65 6e 64 69 66 0a 23 43200;.#endif.#
7f850 69 66 64 65 66 20 48 41 56 45 5f 47 4d 54 49 4d ifdef HAVE_GMTIM
7f860 45 5f 52 0a 20 20 7b 0a 20 20 20 20 73 74 72 75 E_R. {. stru
7f870 63 74 20 74 6d 20 73 4e 6f 77 3b 0a 20 20 20 20 ct tm sNow;.
7f880 67 6d 74 69 6d 65 5f 72 28 26 74 2c 20 26 73 4e gmtime_r(&t, &sN
7f890 6f 77 29 3b 0a 20 20 20 20 73 74 72 66 74 69 6d ow);. strftim
7f8a0 65 28 7a 42 75 66 2c 20 32 30 2c 20 7a 46 6f 72 e(zBuf, 20, zFor
7f8b0 6d 61 74 2c 20 26 73 4e 6f 77 29 3b 0a 20 20 7d mat, &sNow);. }
7f8c0 0a 23 65 6c 73 65 0a 20 20 7b 0a 20 20 20 20 73 .#else. {. s
7f8d0 74 72 75 63 74 20 74 6d 20 2a 70 54 6d 3b 0a 20 truct tm *pTm;.
7f8e0 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 sqlite3_mutex
7f8f0 5f 65 6e 74 65 72 28 73 71 6c 69 74 65 33 4d 75 _enter(sqlite3Mu
7f900 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f texAlloc(SQLITE_
7f910 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 MUTEX_STATIC_MAS
7f920 54 45 52 29 29 3b 0a 20 20 20 20 70 54 6d 20 3d TER));. pTm =
7f930 20 67 6d 74 69 6d 65 28 26 74 29 3b 0a 20 20 20 gmtime(&t);.
7f940 20 73 74 72 66 74 69 6d 65 28 7a 42 75 66 2c 20 strftime(zBuf,
7f950 32 30 2c 20 7a 46 6f 72 6d 61 74 2c 20 70 54 6d 20, zFormat, pTm
7f960 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d );. sqlite3_m
7f970 75 74 65 78 5f 6c 65 61 76 65 28 73 71 6c 69 74 utex_leave(sqlit
7f980 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c e3MutexAlloc(SQL
7f990 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 ITE_MUTEX_STATIC
7f9a0 5f 4d 41 53 54 45 52 29 29 3b 0a 20 20 7d 0a 23 _MASTER));. }.#
7f9b0 65 6e 64 69 66 0a 0a 20 20 73 71 6c 69 74 65 33 endif.. sqlite3
7f9c0 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e _result_text(con
7f9d0 74 65 78 74 2c 20 7a 42 75 66 2c 20 2d 31 2c 20 text, zBuf, -1,
7f9e0 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 SQLITE_TRANSIENT
7f9f0 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a );.}.#endif../*.
7fa00 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
7fa10 20 72 65 67 69 73 74 65 72 65 64 20 61 6c 6c 20 registered all
7fa20 6f 66 20 74 68 65 20 61 62 6f 76 65 20 43 20 66 of the above C f
7fa30 75 6e 63 74 69 6f 6e 73 20 61 73 20 53 51 4c 0a unctions as SQL.
7fa40 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 54 ** functions. T
7fa50 68 69 73 20 73 68 6f 75 6c 64 20 62 65 20 74 68 his should be th
7fa60 65 20 6f 6e 6c 79 20 72 6f 75 74 69 6e 65 20 69 e only routine i
7fa70 6e 20 74 68 69 73 20 66 69 6c 65 20 77 69 74 68 n this file with
7fa80 0a 2a 2a 20 65 78 74 65 72 6e 61 6c 20 6c 69 6e .** external lin
7fa90 6b 61 67 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f kage..*/.SQLITE_
7faa0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
7fab0 69 74 65 33 52 65 67 69 73 74 65 72 44 61 74 65 ite3RegisterDate
7fac0 54 69 6d 65 46 75 6e 63 74 69 6f 6e 73 28 76 6f TimeFunctions(vo
7fad0 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 53 51 id){. static SQ
7fae0 4c 49 54 45 5f 57 53 44 20 46 75 6e 63 44 65 66 LITE_WSD FuncDef
7faf0 20 61 44 61 74 65 54 69 6d 65 46 75 6e 63 73 5b aDateTimeFuncs[
7fb00 5d 20 3d 20 7b 0a 23 69 66 6e 64 65 66 20 53 51 ] = {.#ifndef SQ
7fb10 4c 49 54 45 5f 4f 4d 49 54 5f 44 41 54 45 54 49 LITE_OMIT_DATETI
7fb20 4d 45 5f 46 55 4e 43 53 0a 20 20 20 20 46 55 4e ME_FUNCS. FUN
7fb30 43 54 49 4f 4e 28 6a 75 6c 69 61 6e 64 61 79 2c CTION(julianday,
7fb40 20 20 20 20 20 20 20 20 2d 31 2c 20 30 2c 20 30 -1, 0, 0
7fb50 2c 20 6a 75 6c 69 61 6e 64 61 79 46 75 6e 63 20 , juliandayFunc
7fb60 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 ),. FUNCTION(
7fb70 64 61 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 date,
7fb80 20 20 2d 31 2c 20 30 2c 20 30 2c 20 64 61 74 65 -1, 0, 0, date
7fb90 46 75 6e 63 20 20 20 20 20 20 29 2c 0a 20 20 20 Func ),.
7fba0 20 46 55 4e 43 54 49 4f 4e 28 74 69 6d 65 2c 20 FUNCTION(time,
7fbb0 20 20 20 20 20 20 20 20 20 20 20 20 2d 31 2c 20 -1,
7fbc0 30 2c 20 30 2c 20 74 69 6d 65 46 75 6e 63 20 20 0, 0, timeFunc
7fbd0 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 ),. FUNCT
7fbe0 49 4f 4e 28 64 61 74 65 74 69 6d 65 2c 20 20 20 ION(datetime,
7fbf0 20 20 20 20 20 20 2d 31 2c 20 30 2c 20 30 2c 20 -1, 0, 0,
7fc00 64 61 74 65 74 69 6d 65 46 75 6e 63 20 20 29 2c datetimeFunc ),
7fc10 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 73 74 . FUNCTION(st
7fc20 72 66 74 69 6d 65 2c 20 20 20 20 20 20 20 20 20 rftime,
7fc30 2d 31 2c 20 30 2c 20 30 2c 20 73 74 72 66 74 69 -1, 0, 0, strfti
7fc40 6d 65 46 75 6e 63 20 20 29 2c 0a 20 20 20 20 46 meFunc ),. F
7fc50 55 4e 43 54 49 4f 4e 28 63 75 72 72 65 6e 74 5f UNCTION(current_
7fc60 74 69 6d 65 2c 20 20 20 20 20 20 30 2c 20 30 2c time, 0, 0,
7fc70 20 30 2c 20 63 74 69 6d 65 46 75 6e 63 20 20 20 0, ctimeFunc
7fc80 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f ),. FUNCTIO
7fc90 4e 28 63 75 72 72 65 6e 74 5f 74 69 6d 65 73 74 N(current_timest
7fca0 61 6d 70 2c 20 30 2c 20 30 2c 20 30 2c 20 63 74 amp, 0, 0, 0, ct
7fcb0 69 6d 65 73 74 61 6d 70 46 75 6e 63 29 2c 0a 20 imestampFunc),.
7fcc0 20 20 20 46 55 4e 43 54 49 4f 4e 28 63 75 72 72 FUNCTION(curr
7fcd0 65 6e 74 5f 64 61 74 65 2c 20 20 20 20 20 20 30 ent_date, 0
7fce0 2c 20 30 2c 20 30 2c 20 63 64 61 74 65 46 75 6e , 0, 0, cdateFun
7fcf0 63 20 20 20 20 20 29 2c 0a 23 65 6c 73 65 0a 20 c ),.#else.
7fd00 20 20 20 53 54 52 5f 46 55 4e 43 54 49 4f 4e 28 STR_FUNCTION(
7fd10 63 75 72 72 65 6e 74 5f 74 69 6d 65 2c 20 20 20 current_time,
7fd20 20 20 20 30 2c 20 22 25 48 3a 25 4d 3a 25 53 22 0, "%H:%M:%S"
7fd30 2c 20 20 20 20 20 20 20 20 20 20 30 2c 20 63 75 , 0, cu
7fd40 72 72 65 6e 74 54 69 6d 65 46 75 6e 63 29 2c 0a rrentTimeFunc),.
7fd50 20 20 20 20 53 54 52 5f 46 55 4e 43 54 49 4f 4e STR_FUNCTION
7fd60 28 63 75 72 72 65 6e 74 5f 74 69 6d 65 73 74 61 (current_timesta
7fd70 6d 70 2c 20 30 2c 20 22 25 59 2d 25 6d 2d 25 64 mp, 0, "%Y-%m-%d
7fd80 22 2c 20 20 20 20 20 20 20 20 20 20 30 2c 20 63 ", 0, c
7fd90 75 72 72 65 6e 74 54 69 6d 65 46 75 6e 63 29 2c urrentTimeFunc),
7fda0 0a 20 20 20 20 53 54 52 5f 46 55 4e 43 54 49 4f . STR_FUNCTIO
7fdb0 4e 28 63 75 72 72 65 6e 74 5f 64 61 74 65 2c 20 N(current_date,
7fdc0 20 20 20 20 20 30 2c 20 22 25 59 2d 25 6d 2d 25 0, "%Y-%m-%
7fdd0 64 20 25 48 3a 25 4d 3a 25 53 22 2c 20 30 2c 20 d %H:%M:%S", 0,
7fde0 63 75 72 72 65 6e 74 54 69 6d 65 46 75 6e 63 29 currentTimeFunc)
7fdf0 2c 0a 23 65 6e 64 69 66 0a 20 20 7d 3b 0a 20 20 ,.#endif. };.
7fe00 69 6e 74 20 69 3b 0a 20 20 46 75 6e 63 44 65 66 int i;. FuncDef
7fe10 48 61 73 68 20 2a 70 48 61 73 68 20 3d 20 26 47 Hash *pHash = &G
7fe20 4c 4f 42 41 4c 28 46 75 6e 63 44 65 66 48 61 73 LOBAL(FuncDefHas
7fe30 68 2c 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c h, sqlite3Global
7fe40 46 75 6e 63 74 69 6f 6e 73 29 3b 0a 20 20 46 75 Functions);. Fu
7fe50 6e 63 44 65 66 20 2a 61 46 75 6e 63 20 3d 20 28 ncDef *aFunc = (
7fe60 46 75 6e 63 44 65 66 2a 29 26 47 4c 4f 42 41 4c FuncDef*)&GLOBAL
7fe70 28 46 75 6e 63 44 65 66 2c 20 61 44 61 74 65 54 (FuncDef, aDateT
7fe80 69 6d 65 46 75 6e 63 73 29 3b 0a 0a 20 20 66 6f imeFuncs);.. fo
7fe90 72 28 69 3d 30 3b 20 69 3c 41 72 72 61 79 53 69 r(i=0; i<ArraySi
7fea0 7a 65 28 61 44 61 74 65 54 69 6d 65 46 75 6e 63 ze(aDateTimeFunc
7feb0 73 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 s); i++){. sq
7fec0 6c 69 74 65 33 46 75 6e 63 44 65 66 49 6e 73 65 lite3FuncDefInse
7fed0 72 74 28 70 48 61 73 68 2c 20 26 61 46 75 6e 63 rt(pHash, &aFunc
7fee0 5b 69 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 2a [i]);. }.}../**
7fef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
7ff00 20 6f 66 20 64 61 74 65 2e 63 20 2a 2a 2a 2a 2a of date.c *****
7ff10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7ff20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7ff30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
7ff40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 ************ Beg
7ff50 69 6e 20 66 69 6c 65 20 6f 73 2e 63 20 2a 2a 2a in file os.c ***
7ff60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7ff70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
7ff80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a ***********/./*.
7ff90 2a 2a 20 32 30 30 35 20 4e 6f 76 65 6d 62 65 72 ** 2005 November
7ffa0 20 32 39 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 29.**.** The au
7ffb0 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 thor disclaims c
7ffc0 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 opyright to this
7ffd0 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 source code. I
7ffe0 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 n place of.** a
7fff0 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 legal notice, he
80000 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 re is a blessing
80010 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 :.**.** May y
80020 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e ou do good and n
80030 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d ot evil..** M
80040 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 ay you find forg
80050 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 iveness for your
80060 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 self and forgive
80070 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d others..** M
80080 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 ay you share fre
80090 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e ely, never takin
800a0 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 g more than you
800b0 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a give..**.*******
800c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
800d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
800e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
800f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
80100 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 *******.**.** Th
80110 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 is file contains
80120 20 4f 53 20 69 6e 74 65 72 66 61 63 65 20 63 6f OS interface co
80130 64 65 20 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f de that is commo
80140 6e 20 74 6f 20 61 6c 6c 0a 2a 2a 20 61 72 63 68 n to all.** arch
80150 69 74 65 63 74 75 72 65 73 2e 0a 2a 2a 0a 2a 2a itectures..**.**
80160 20 24 49 64 3a 20 6f 73 2e 63 2c 76 20 31 2e 31 $Id: os.c,v 1.1
80170 32 37 20 32 30 30 39 2f 30 37 2f 32 37 20 31 31 27 2009/07/27 11
80180 3a 34 31 3a 32 31 20 64 61 6e 69 65 6c 6b 31 39 :41:21 danielk19
80190 37 37 20 45 78 70 20 24 0a 2a 2f 0a 23 64 65 66 77 Exp $.*/.#def
801a0 69 6e 65 20 5f 53 51 4c 49 54 45 5f 4f 53 5f 43 ine _SQLITE_OS_C
801b0 5f 20 31 0a 23 75 6e 64 65 66 20 5f 53 51 4c 49 _ 1.#undef _SQLI
801c0 54 45 5f 4f 53 5f 43 5f 0a 0a 2f 2a 0a 2a 2a 20 TE_OS_C_../*.**
801d0 54 68 65 20 64 65 66 61 75 6c 74 20 53 51 4c 69 The default SQLi
801e0 74 65 20 73 71 6c 69 74 65 33 5f 76 66 73 20 69 te sqlite3_vfs i
801f0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 64 mplementations d
80200 6f 20 6e 6f 74 20 61 6c 6c 6f 63 61 74 65 0a 2a o not allocate.*
80210 2a 20 6d 65 6d 6f 72 79 20 28 61 63 74 75 61 6c * memory (actual
80220 6c 79 2c 20 6f 73 5f 75 6e 69 78 2e 63 20 61 6c ly, os_unix.c al
80230 6c 6f 63 61 74 65 73 20 61 20 73 6d 61 6c 6c 20 locates a small
80240 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 amount of memory
80250 0a 2a 2a 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 .** from within
80260 4f 73 4f 70 65 6e 28 29 29 2c 20 62 75 74 20 73 OsOpen()), but s
80270 6f 6d 65 20 74 68 69 72 64 2d 70 61 72 74 79 20 ome third-party
80280 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 implementations
80290 6d 61 79 2e 0a 2a 2a 20 53 6f 20 77 65 20 74 65 may..** So we te
802a0 73 74 20 74 68 65 20 65 66 66 65 63 74 73 20 6f st the effects o
802b0 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 f a malloc() fai
802c0 6c 69 6e 67 20 61 6e 64 20 74 68 65 20 73 71 6c ling and the sql
802d0 69 74 65 33 4f 73 58 58 58 28 29 0a 2a 2a 20 66 ite3OsXXX().** f
802e0 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 69 6e unction returnin
802f0 67 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e g SQLITE_IOERR_N
80300 4f 4d 45 4d 20 75 73 69 6e 67 20 74 68 65 20 44 OMEM using the D
80310 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 O_OS_MALLOC_TEST
80320 20 6d 61 63 72 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 macro..**.** Th
80330 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 75 6e 63 e following func
80340 74 69 6f 6e 73 20 61 72 65 20 69 6e 73 74 72 75 tions are instru
80350 6d 65 6e 74 65 64 20 66 6f 72 20 6d 61 6c 6c 6f mented for mallo
80360 63 28 29 20 66 61 69 6c 75 72 65 20 0a 2a 2a 20 c() failure .**
80370 74 65 73 74 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 testing:.**.**
80380 20 20 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e sqlite3OsOpen
80390 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 ().** sqlite
803a0 33 4f 73 52 65 61 64 28 29 0a 2a 2a 20 20 20 20 3OsRead().**
803b0 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 sqlite3OsWrite(
803c0 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 ).** sqlite3
803d0 4f 73 53 79 6e 63 28 29 0a 2a 2a 20 20 20 20 20 OsSync().**
803e0 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 29 0a sqlite3OsLock().
803f0 2a 2a 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 **.*/.#if define
80400 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 20 26 d(SQLITE_TEST) &
80410 26 20 28 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e & (SQLITE_OS_WIN
80420 3d 3d 30 29 0a 20 20 23 64 65 66 69 6e 65 20 44 ==0). #define D
80430 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 O_OS_MALLOC_TEST
80440 28 78 29 20 69 66 20 28 21 78 20 7c 7c 20 21 73 (x) if (!x || !s
80450 71 6c 69 74 65 33 49 73 4d 65 6d 4a 6f 75 72 6e qlite3IsMemJourn
80460 61 6c 28 78 29 29 20 7b 20 20 20 20 20 5c 0a 20 al(x)) { \.
80470 20 20 20 76 6f 69 64 20 2a 70 54 73 74 41 6c 6c void *pTstAll
80480 6f 63 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c oc = sqlite3Mall
80490 6f 63 28 31 30 29 3b 20 20 20 20 20 20 20 20 20 oc(10);
804a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
804b0 20 20 20 20 5c 0a 20 20 20 20 69 66 20 28 21 70 \. if (!p
804c0 54 73 74 41 6c 6c 6f 63 29 20 72 65 74 75 72 6e TstAlloc) return
804d0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f SQLITE_IOERR_NO
804e0 4d 45 4d 3b 20 20 20 20 20 20 20 20 20 20 20 20 MEM;
804f0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
80500 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 54 sqlite3_free(pT
80510 73 74 41 6c 6c 6f 63 29 3b 20 20 20 20 20 20 20 stAlloc);
80520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
80530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
80540 20 20 5c 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 \. }.#else.
80550 23 64 65 66 69 6e 65 20 44 4f 5f 4f 53 5f 4d 41 #define DO_OS_MA
80560 4c 4c 4f 43 5f 54 45 53 54 28 78 29 0a 23 65 6e LLOC_TEST(x).#en
80570 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 dif../*.** The f
80580 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 ollowing routine
80590 73 20 61 72 65 20 63 6f 6e 76 65 6e 69 65 6e 63 s are convenienc
805a0 65 20 77 72 61 70 70 65 72 73 20 61 72 6f 75 6e e wrappers aroun
805b0 64 20 6d 65 74 68 6f 64 73 0a 2a 2a 20 6f 66 20 d methods.** of
805c0 74 68 65 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 the sqlite3_file
805d0 20 6f 62 6a 65 63 74 2e 20 20 54 68 69 73 20 69 object. This i
805e0 73 20 6d 6f 73 74 6c 79 20 6a 75 73 74 20 73 79 s mostly just sy
805f0 6e 74 61 63 74 69 63 20 73 75 67 61 72 2e 20 41 ntactic sugar. A
80600 6c 6c 0a 2a 2a 20 6f 66 20 74 68 69 73 20 77 6f ll.** of this wo
80610 75 6c 64 20 62 65 20 63 6f 6d 70 6c 65 74 65 6c uld be completel
80620 79 20 61 75 74 6f 6d 61 74 69 63 20 69 66 20 53 y automatic if S
80630 51 4c 69 74 65 20 77 65 72 65 20 63 6f 64 65 64 QLite were coded
80640 20 75 73 69 6e 67 0a 2a 2a 20 43 2b 2b 20 69 6e using.** C++ in
80650 73 74 65 61 64 20 6f 66 20 70 6c 61 69 6e 20 6f stead of plain o
80660 6c 64 20 43 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ld C..*/.SQLITE_
80670 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
80680 74 65 33 4f 73 43 6c 6f 73 65 28 73 71 6c 69 74 te3OsClose(sqlit
80690 65 33 5f 66 69 6c 65 20 2a 70 49 64 29 7b 0a 20 e3_file *pId){.
806a0 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
806b0 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 49 64 2d 3e _OK;. if( pId->
806c0 70 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 pMethods ){.
806d0 72 63 20 3d 20 70 49 64 2d 3e 70 4d 65 74 68 6f rc = pId->pMetho
806e0 64 73 2d 3e 78 43 6c 6f 73 65 28 70 49 64 29 3b ds->xClose(pId);
806f0 0a 20 20 20 20 70 49 64 2d 3e 70 4d 65 74 68 6f . pId->pMetho
80700 64 73 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 ds = 0;. }. re
80710 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 turn rc;.}.SQLIT
80720 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
80730 6c 69 74 65 33 4f 73 52 65 61 64 28 73 71 6c 69 lite3OsRead(sqli
80740 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 76 6f te3_file *id, vo
80750 69 64 20 2a 70 42 75 66 2c 20 69 6e 74 20 61 6d id *pBuf, int am
80760 74 2c 20 69 36 34 20 6f 66 66 73 65 74 29 7b 0a t, i64 offset){.
80770 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 DO_OS_MALLOC_T
80780 45 53 54 28 69 64 29 3b 0a 20 20 72 65 74 75 72 EST(id);. retur
80790 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e n id->pMethods->
807a0 78 52 65 61 64 28 69 64 2c 20 70 42 75 66 2c 20 xRead(id, pBuf,
807b0 61 6d 74 2c 20 6f 66 66 73 65 74 29 3b 0a 7d 0a amt, offset);.}.
807c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
807d0 6e 74 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 nt sqlite3OsWrit
807e0 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a e(sqlite3_file *
807f0 69 64 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a id, const void *
80800 70 42 75 66 2c 20 69 6e 74 20 61 6d 74 2c 20 69 pBuf, int amt, i
80810 36 34 20 6f 66 66 73 65 74 29 7b 0a 20 20 44 4f 64 offset){. DO
80820 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 28 _OS_MALLOC_TEST(
80830 69 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 64 id);. return id
80840 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 57 72 69 ->pMethods->xWri
80850 74 65 28 69 64 2c 20 70 42 75 66 2c 20 61 6d 74 te(id, pBuf, amt
80860 2c 20 6f 66 66 73 65 74 29 3b 0a 7d 0a 53 51 4c , offset);.}.SQL
80870 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
80880 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 74 sqlite3OsTruncat
80890 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a e(sqlite3_file *
808a0 69 64 2c 20 69 36 34 20 73 69 7a 65 29 7b 0a 20 id, i64 size){.
808b0 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 return id->pMet
808c0 68 6f 64 73 2d 3e 78 54 72 75 6e 63 61 74 65 28 hods->xTruncate(
808d0 69 64 2c 20 73 69 7a 65 29 3b 0a 7d 0a 53 51 4c id, size);.}.SQL
808e0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
808f0 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 73 71 sqlite3OsSync(sq
80900 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 lite3_file *id,
80910 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 44 4f int flags){. DO
80920 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 28 _OS_MALLOC_TEST(
80930 69 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 64 id);. return id
80940 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 53 79 6e ->pMethods->xSyn
80950 63 28 69 64 2c 20 66 6c 61 67 73 29 3b 0a 7d 0a c(id, flags);.}.
80960 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
80970 6e 74 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 nt sqlite3OsFile
80980 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 69 6c Size(sqlite3_fil
80990 65 20 2a 69 64 2c 20 69 36 34 20 2a 70 53 69 7a e *id, i64 *pSiz
809a0 65 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c e){. DO_OS_MALL
809b0 4f 43 5f 54 45 53 54 28 69 64 29 3b 0a 20 20 72 OC_TEST(id);. r
809c0 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f eturn id->pMetho
809d0 64 73 2d 3e 78 46 69 6c 65 53 69 7a 65 28 69 64 ds->xFileSize(id
809e0 2c 20 70 53 69 7a 65 29 3b 0a 7d 0a 53 51 4c 49 , pSize);.}.SQLI
809f0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
80a00 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 73 71 6c qlite3OsLock(sql
80a10 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 ite3_file *id, i
80a20 6e 74 20 6c 6f 63 6b 54 79 70 65 29 7b 0a 20 20 nt lockType){.
80a30 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 DO_OS_MALLOC_TES
80a40 54 28 69 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 T(id);. return
80a50 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 4c id->pMethods->xL
80a60 6f 63 6b 28 69 64 2c 20 6c 6f 63 6b 54 79 70 65 ock(id, lockType
80a70 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 );.}.SQLITE_PRIV
80a80 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f ATE int sqlite3O
80a90 73 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f sUnlock(sqlite3_
80aa0 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f file *id, int lo
80ab0 63 6b 54 79 70 65 29 7b 0a 20 20 72 65 74 75 72 ckType){. retur
80ac0 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e n id->pMethods->
80ad0 78 55 6e 6c 6f 63 6b 28 69 64 2c 20 6c 6f 63 6b xUnlock(id, lock
80ae0 54 79 70 65 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f Type);.}.SQLITE_
80af0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
80b00 74 65 33 4f 73 43 68 65 63 6b 52 65 73 65 72 76 te3OsCheckReserv
80b10 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 edLock(sqlite3_f
80b20 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 52 ile *id, int *pR
80b30 65 73 4f 75 74 29 7b 0a 20 20 44 4f 5f 4f 53 5f esOut){. DO_OS_
80b40 4d 41 4c 4c 4f 43 5f 54 45 53 54 28 69 64 29 3b MALLOC_TEST(id);
80b50 0a 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d . return id->pM
80b60 65 74 68 6f 64 73 2d 3e 78 43 68 65 63 6b 52 65 ethods->xCheckRe
80b70 73 65 72 76 65 64 4c 6f 63 6b 28 69 64 2c 20 70 servedLock(id, p
80b80 52 65 73 4f 75 74 29 3b 0a 7d 0a 53 51 4c 49 54 ResOut);.}.SQLIT
80b90 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
80ba0 6c 69 74 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72 lite3OsFileContr
80bb0 6f 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 ol(sqlite3_file
80bc0 2a 69 64 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69 *id, int op, voi
80bd0 64 20 2a 70 41 72 67 29 7b 0a 20 20 72 65 74 75 d *pArg){. retu
80be0 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d rn id->pMethods-
80bf0 3e 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 69 64 >xFileControl(id
80c00 2c 20 6f 70 2c 20 70 41 72 67 29 3b 0a 7d 0a 53 , op, pArg);.}.S
80c10 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
80c20 74 20 73 71 6c 69 74 65 33 4f 73 53 65 63 74 6f t sqlite3OsSecto
80c30 72 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 69 rSize(sqlite3_fi
80c40 6c 65 20 2a 69 64 29 7b 0a 20 20 69 6e 74 20 28 le *id){. int (
80c50 2a 78 53 65 63 74 6f 72 53 69 7a 65 29 28 73 71 *xSectorSize)(sq
80c60 6c 69 74 65 33 5f 66 69 6c 65 2a 29 20 3d 20 69 lite3_file*) = i
80c70 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 53 65 d->pMethods->xSe
80c80 63 74 6f 72 53 69 7a 65 3b 0a 20 20 72 65 74 75 ctorSize;. retu
80c90 72 6e 20 28 78 53 65 63 74 6f 72 53 69 7a 65 20 rn (xSectorSize
80ca0 3f 20 78 53 65 63 74 6f 72 53 69 7a 65 28 69 64 ? xSectorSize(id
80cb0 29 20 3a 20 53 51 4c 49 54 45 5f 44 45 46 41 55 ) : SQLITE_DEFAU
80cc0 4c 54 5f 53 45 43 54 4f 52 5f 53 49 5a 45 29 3b LT_SECTOR_SIZE);
80cd0 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 .}.SQLITE_PRIVAT
80ce0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 44 E int sqlite3OsD
80cf0 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 eviceCharacteris
80d00 74 69 63 73 28 73 71 6c 69 74 65 33 5f 66 69 6c tics(sqlite3_fil
80d10 65 20 2a 69 64 29 7b 0a 20 20 72 65 74 75 72 6e e *id){. return
80d20 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 id->pMethods->x
80d30 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 DeviceCharacteri
80d40 73 74 69 63 73 28 69 64 29 3b 0a 7d 0a 0a 2f 2a stics(id);.}../*
80d50 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 67 72 6f .** The next gro
80d60 75 70 20 6f 66 20 72 6f 75 74 69 6e 65 73 20 61 up of routines a
80d70 72 65 20 63 6f 6e 76 65 6e 69 65 6e 63 65 20 77 re convenience w
80d80 72 61 70 70 65 72 73 20 61 72 6f 75 6e 64 20 74 rappers around t
80d90 68 65 0a 2a 2a 20 56 46 53 20 6d 65 74 68 6f 64 he.** VFS method
80da0 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 s..*/.SQLITE_PRI
80db0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
80dc0 4f 73 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 OsOpen(. sqlite
80dd0 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 0a 20 20 3_vfs *pVfs, .
80de0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74 const char *zPat
80df0 68 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 h, . sqlite3_fi
80e00 6c 65 20 2a 70 46 69 6c 65 2c 20 0a 20 20 69 6e le *pFile, . in
80e10 74 20 66 6c 61 67 73 2c 20 0a 20 20 69 6e 74 20 t flags, . int
80e20 2a 70 46 6c 61 67 73 4f 75 74 0a 29 7b 0a 20 20 *pFlagsOut.){.
80e30 69 6e 74 20 72 63 3b 0a 20 20 44 4f 5f 4f 53 5f int rc;. DO_OS_
80e40 4d 41 4c 4c 4f 43 5f 54 45 53 54 28 30 29 3b 0a MALLOC_TEST(0);.
80e50 20 20 2f 2a 20 30 78 37 66 31 66 20 69 73 20 61 /* 0x7f1f is a
80e60 20 6d 61 73 6b 20 6f 66 20 53 51 4c 49 54 45 5f mask of SQLITE_
80e70 4f 50 45 4e 5f 20 66 6c 61 67 73 20 74 68 61 74 OPEN_ flags that
80e80 20 61 72 65 20 76 61 6c 69 64 20 74 6f 20 62 65 are valid to be
80e90 20 70 61 73 73 65 64 0a 20 20 2a 2a 20 64 6f 77 passed. ** dow
80ea0 6e 20 69 6e 74 6f 20 74 68 65 20 56 46 53 20 6c n into the VFS l
80eb0 61 79 65 72 2e 20 20 53 6f 6d 65 20 53 51 4c 49 ayer. Some SQLI
80ec0 54 45 5f 4f 50 45 4e 5f 20 66 6c 61 67 73 20 28 TE_OPEN_ flags (
80ed0 66 6f 72 20 65 78 61 6d 70 6c 65 2c 0a 20 20 2a for example,. *
80ee0 2a 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 46 55 * SQLITE_OPEN_FU
80ef0 4c 4c 4d 55 54 45 58 20 6f 72 20 53 51 4c 49 54 LLMUTEX or SQLIT
80f00 45 5f 4f 50 45 4e 5f 53 48 41 52 45 44 43 41 43 E_OPEN_SHAREDCAC
80f10 48 45 29 20 61 72 65 20 62 6c 6f 63 6b 65 64 20 HE) are blocked
80f20 62 65 66 6f 72 65 0a 20 20 2a 2a 20 72 65 61 63 before. ** reac
80f30 68 69 6e 67 20 74 68 65 20 56 46 53 2e 20 2a 2f hing the VFS. */
80f40 0a 20 20 72 63 20 3d 20 70 56 66 73 2d 3e 78 4f . rc = pVfs->xO
80f50 70 65 6e 28 70 56 66 73 2c 20 7a 50 61 74 68 2c pen(pVfs, zPath,
80f60 20 70 46 69 6c 65 2c 20 66 6c 61 67 73 20 26 20 pFile, flags &
80f70 30 78 37 66 31 66 2c 20 70 46 6c 61 67 73 4f 75 0x7f1f, pFlagsOu
80f80 74 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 t);. assert( rc
80f90 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 ==SQLITE_OK || p
80fa0 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 73 3d 3d File->pMethods==
80fb0 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 0 );. return rc
80fc0 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ;.}.SQLITE_PRIVA
80fd0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 TE int sqlite3Os
80fe0 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33 5f 76 Delete(sqlite3_v
80ff0 66 73 20 2a 70 56 66 73 2c 20 63 6f 6e 73 74 20 fs *pVfs, const
81000 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 69 6e 74 char *zPath, int
81010 20 64 69 72 53 79 6e 63 29 7b 0a 20 20 72 65 74 dirSync){. ret
81020 75 72 6e 20 70 56 66 73 2d 3e 78 44 65 6c 65 74 urn pVfs->xDelet
81030 65 28 70 56 66 73 2c 20 7a 50 61 74 68 2c 20 64 e(pVfs, zPath, d
81040 69 72 53 79 6e 63 29 3b 0a 7d 0a 53 51 4c 49 54 irSync);.}.SQLIT
81050 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
81060 6c 69 74 65 33 4f 73 41 63 63 65 73 73 28 0a 20 lite3OsAccess(.
81070 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 sqlite3_vfs *pV
81080 66 73 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 fs, . const cha
81090 72 20 2a 7a 50 61 74 68 2c 20 0a 20 20 69 6e 74 r *zPath, . int
810a0 20 66 6c 61 67 73 2c 20 0a 20 20 69 6e 74 20 2a flags, . int *
810b0 70 52 65 73 4f 75 74 0a 29 7b 0a 20 20 44 4f 5f pResOut.){. DO_
810c0 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 28 30 OS_MALLOC_TEST(0
810d0 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 56 66 73 );. return pVfs
810e0 2d 3e 78 41 63 63 65 73 73 28 70 56 66 73 2c 20 ->xAccess(pVfs,
810f0 7a 50 61 74 68 2c 20 66 6c 61 67 73 2c 20 70 52 zPath, flags, pR
81100 65 73 4f 75 74 29 3b 0a 7d 0a 53 51 4c 49 54 45 esOut);.}.SQLITE
81110 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
81120 69 74 65 33 4f 73 46 75 6c 6c 50 61 74 68 6e 61 ite3OsFullPathna
81130 6d 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 me(. sqlite3_vf
81140 73 20 2a 70 56 66 73 2c 20 0a 20 20 63 6f 6e 73 s *pVfs, . cons
81150 74 20 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 0a t char *zPath, .
81160 20 20 69 6e 74 20 6e 50 61 74 68 4f 75 74 2c 20 int nPathOut,
81170 0a 20 20 63 68 61 72 20 2a 7a 50 61 74 68 4f 75 . char *zPathOu
81180 74 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 56 t.){. return pV
81190 66 73 2d 3e 78 46 75 6c 6c 50 61 74 68 6e 61 6d fs->xFullPathnam
811a0 65 28 70 56 66 73 2c 20 7a 50 61 74 68 2c 20 6e e(pVfs, zPath, n
811b0 50 61 74 68 4f 75 74 2c 20 7a 50 61 74 68 4f 75 PathOut, zPathOu
811c0 74 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 t);.}.#ifndef SQ
811d0 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 LITE_OMIT_LOAD_E
811e0 58 54 45 4e 53 49 4f 4e 0a 53 51 4c 49 54 45 5f XTENSION.SQLITE_
811f0 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 PRIVATE void *sq
81200 6c 69 74 65 33 4f 73 44 6c 4f 70 65 6e 28 73 71 lite3OsDlOpen(sq
81210 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c lite3_vfs *pVfs,
81220 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 const char *zPa
81230 74 68 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 56 th){. return pV
81240 66 73 2d 3e 78 44 6c 4f 70 65 6e 28 70 56 66 73 fs->xDlOpen(pVfs
81250 2c 20 7a 50 61 74 68 29 3b 0a 7d 0a 53 51 4c 49 , zPath);.}.SQLI
81260 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
81270 73 71 6c 69 74 65 33 4f 73 44 6c 45 72 72 6f 72 sqlite3OsDlError
81280 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 (sqlite3_vfs *pV
81290 66 73 2c 20 69 6e 74 20 6e 42 79 74 65 2c 20 63 fs, int nByte, c
812a0 68 61 72 20 2a 7a 42 75 66 4f 75 74 29 7b 0a 20 har *zBufOut){.
812b0 20 70 56 66 73 2d 3e 78 44 6c 45 72 72 6f 72 28 pVfs->xDlError(
812c0 70 56 66 73 2c 20 6e 42 79 74 65 2c 20 7a 42 75 pVfs, nByte, zBu
812d0 66 4f 75 74 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f fOut);.}.SQLITE_
812e0 50 52 49 56 41 54 45 20 76 6f 69 64 20 28 2a 73 PRIVATE void (*s
812f0 71 6c 69 74 65 33 4f 73 44 6c 53 79 6d 28 73 71 qlite3OsDlSym(sq
81300 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c lite3_vfs *pVfs,
81310 20 76 6f 69 64 20 2a 70 48 64 6c 65 2c 20 63 6f void *pHdle, co
81320 6e 73 74 20 63 68 61 72 20 2a 7a 53 79 6d 29 29 nst char *zSym))
81330 28 76 6f 69 64 29 7b 0a 20 20 72 65 74 75 72 6e (void){. return
81340 20 70 56 66 73 2d 3e 78 44 6c 53 79 6d 28 70 56 pVfs->xDlSym(pV
81350 66 73 2c 20 70 48 64 6c 65 2c 20 7a 53 79 6d 29 fs, pHdle, zSym)
81360 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ;.}.SQLITE_PRIVA
81370 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4f TE void sqlite3O
81380 73 44 6c 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 sDlClose(sqlite3
81390 5f 76 66 73 20 2a 70 56 66 73 2c 20 76 6f 69 64 _vfs *pVfs, void
813a0 20 2a 70 48 61 6e 64 6c 65 29 7b 0a 20 20 70 56 *pHandle){. pV
813b0 66 73 2d 3e 78 44 6c 43 6c 6f 73 65 28 70 56 66 fs->xDlClose(pVf
813c0 73 2c 20 70 48 61 6e 64 6c 65 29 3b 0a 7d 0a 23 s, pHandle);.}.#
813d0 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
813e0 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 OMIT_LOAD_EXTENS
813f0 49 4f 4e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ION */.SQLITE_PR
81400 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
81410 33 4f 73 52 61 6e 64 6f 6d 6e 65 73 73 28 73 71 3OsRandomness(sq
81420 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c lite3_vfs *pVfs,
81430 20 69 6e 74 20 6e 42 79 74 65 2c 20 63 68 61 72 int nByte, char
81440 20 2a 7a 42 75 66 4f 75 74 29 7b 0a 20 20 72 65 *zBufOut){. re
81450 74 75 72 6e 20 70 56 66 73 2d 3e 78 52 61 6e 64 turn pVfs->xRand
81460 6f 6d 6e 65 73 73 28 70 56 66 73 2c 20 6e 42 79 omness(pVfs, nBy
81470 74 65 2c 20 7a 42 75 66 4f 75 74 29 3b 0a 7d 0a te, zBufOut);.}.
81480 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
81490 6e 74 20 73 71 6c 69 74 65 33 4f 73 53 6c 65 65 nt sqlite3OsSlee
814a0 70 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 p(sqlite3_vfs *p
814b0 56 66 73 2c 20 69 6e 74 20 6e 4d 69 63 72 6f 29 Vfs, int nMicro)
814c0 7b 0a 20 20 72 65 74 75 72 6e 20 70 56 66 73 2d {. return pVfs-
814d0 3e 78 53 6c 65 65 70 28 70 56 66 73 2c 20 6e 4d >xSleep(pVfs, nM
814e0 69 63 72 6f 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f icro);.}.SQLITE_
814f0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
81500 74 65 33 4f 73 43 75 72 72 65 6e 74 54 69 6d 65 te3OsCurrentTime
81510 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 (sqlite3_vfs *pV
81520 66 73 2c 20 64 6f 75 62 6c 65 20 2a 70 54 69 6d fs, double *pTim
81530 65 4f 75 74 29 7b 0a 20 20 72 65 74 75 72 6e 20 eOut){. return
81540 70 56 66 73 2d 3e 78 43 75 72 72 65 6e 74 54 69 pVfs->xCurrentTi
81550 6d 65 28 70 56 66 73 2c 20 70 54 69 6d 65 4f 75 me(pVfs, pTimeOu
81560 74 29 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 t);.}..SQLITE_PR
81570 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
81580 33 4f 73 4f 70 65 6e 4d 61 6c 6c 6f 63 28 0a 20 3OsOpenMalloc(.
81590 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 sqlite3_vfs *pV
815a0 66 73 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 fs, . const cha
815b0 72 20 2a 7a 46 69 6c 65 2c 20 0a 20 20 73 71 6c r *zFile, . sql
815c0 69 74 65 33 5f 66 69 6c 65 20 2a 2a 70 70 46 69 ite3_file **ppFi
815d0 6c 65 2c 20 0a 20 20 69 6e 74 20 66 6c 61 67 73 le, . int flags
815e0 2c 0a 20 20 69 6e 74 20 2a 70 4f 75 74 46 6c 61 ,. int *pOutFla
815f0 67 73 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d gs.){. int rc =
81600 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
81610 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 sqlite3_file *p
81620 46 69 6c 65 3b 0a 20 20 70 46 69 6c 65 20 3d 20 File;. pFile =
81630 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 (sqlite3_file *)
81640 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 70 56 sqlite3Malloc(pV
81650 66 73 2d 3e 73 7a 4f 73 46 69 6c 65 29 3b 0a 20 fs->szOsFile);.
81660 20 69 66 28 20 70 46 69 6c 65 20 29 7b 0a 20 20 if( pFile ){.
81670 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
81680 4f 70 65 6e 28 70 56 66 73 2c 20 7a 46 69 6c 65 Open(pVfs, zFile
81690 2c 20 70 46 69 6c 65 2c 20 66 6c 61 67 73 2c 20 , pFile, flags,
816a0 70 4f 75 74 46 6c 61 67 73 29 3b 0a 20 20 20 20 pOutFlags);.
816b0 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
816c0 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 K ){. sqlit
816d0 65 33 5f 66 72 65 65 28 70 46 69 6c 65 29 3b 0a e3_free(pFile);.
816e0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
816f0 20 2a 70 70 46 69 6c 65 20 3d 20 70 46 69 6c 65 *ppFile = pFile
81700 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 ;. }. }. re
81710 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 turn rc;.}.SQLIT
81720 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
81730 6c 69 74 65 33 4f 73 43 6c 6f 73 65 46 72 65 65 lite3OsCloseFree
81740 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 (sqlite3_file *p
81750 46 69 6c 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 File){. int rc
81760 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61 = SQLITE_OK;. a
81770 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a ssert( pFile );.
81780 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
81790 43 6c 6f 73 65 28 70 46 69 6c 65 29 3b 0a 20 20 Close(pFile);.
817a0 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 46 69 sqlite3_free(pFi
817b0 6c 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 le);. return rc
817c0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 ;.}../*.** This
817d0 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 77 72 function is a wr
817e0 61 70 70 65 72 20 61 72 6f 75 6e 64 20 74 68 65 apper around the
817f0 20 4f 53 20 73 70 65 63 69 66 69 63 20 69 6d 70 OS specific imp
81800 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 0a 2a lementation of.*
81810 2a 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 * sqlite3_os_ini
81820 74 28 29 2e 20 54 68 65 20 70 75 72 70 6f 73 65 t(). The purpose
81830 20 6f 66 20 74 68 65 20 77 72 61 70 70 65 72 20 of the wrapper
81840 69 73 20 74 6f 20 70 72 6f 76 69 64 65 20 74 68 is to provide th
81850 65 0a 2a 2a 20 61 62 69 6c 69 74 79 20 74 6f 20 e.** ability to
81860 73 69 6d 75 6c 61 74 65 20 61 20 6d 61 6c 6c 6f simulate a mallo
81870 63 20 66 61 69 6c 75 72 65 2c 20 73 6f 20 74 68 c failure, so th
81880 61 74 20 74 68 65 20 68 61 6e 64 6c 69 6e 67 20 at the handling
81890 6f 66 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 20 69 of an.** error i
818a0 6e 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 n sqlite3_os_ini
818b0 74 28 29 20 62 79 20 74 68 65 20 75 70 70 65 72 t() by the upper
818c0 20 6c 61 79 65 72 73 20 63 61 6e 20 62 65 20 74 layers can be t
818d0 65 73 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ested..*/.SQLITE
818e0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
818f0 69 74 65 33 4f 73 49 6e 69 74 28 76 6f 69 64 29 ite3OsInit(void)
81900 7b 0a 20 20 76 6f 69 64 20 2a 70 20 3d 20 73 71 {. void *p = sq
81910 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 31 30 29 lite3_malloc(10)
81920 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 ;. if( p==0 ) r
81930 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d eturn SQLITE_NOM
81940 45 4d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 EM;. sqlite3_fr
81950 65 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 ee(p);. return
81960 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 sqlite3_os_init(
81970 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 );.}../*.** The
81980 6c 69 73 74 20 6f 66 20 61 6c 6c 20 72 65 67 69 list of all regi
81990 73 74 65 72 65 64 20 56 46 53 20 69 6d 70 6c 65 stered VFS imple
819a0 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 73 mentations..*/.s
819b0 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 76 66 tatic sqlite3_vf
819c0 73 20 2a 20 53 51 4c 49 54 45 5f 57 53 44 20 76 s * SQLITE_WSD v
819d0 66 73 4c 69 73 74 20 3d 20 30 3b 0a 23 64 65 66 fsList = 0;.#def
819e0 69 6e 65 20 76 66 73 4c 69 73 74 20 47 4c 4f 42 ine vfsList GLOB
819f0 41 4c 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a AL(sqlite3_vfs *
81a00 2c 20 76 66 73 4c 69 73 74 29 0a 0a 2f 2a 0a 2a , vfsList)../*.*
81a10 2a 20 4c 6f 63 61 74 65 20 61 20 56 46 53 20 62 * Locate a VFS b
81a20 79 20 6e 61 6d 65 2e 20 20 49 66 20 6e 6f 20 6e y name. If no n
81a30 61 6d 65 20 69 73 20 67 69 76 65 6e 2c 20 73 69 ame is given, si
81a40 6d 70 6c 79 20 72 65 74 75 72 6e 20 74 68 65 0a mply return the.
81a50 2a 2a 20 66 69 72 73 74 20 56 46 53 20 6f 6e 20 ** first VFS on
81a60 74 68 65 20 6c 69 73 74 2e 0a 2a 2f 0a 53 51 4c the list..*/.SQL
81a70 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f ITE_API sqlite3_
81a80 76 66 73 20 2a 73 71 6c 69 74 65 33 5f 76 66 73 vfs *sqlite3_vfs
81a90 5f 66 69 6e 64 28 63 6f 6e 73 74 20 63 68 61 72 _find(const char
81aa0 20 2a 7a 56 66 73 29 7b 0a 20 20 73 71 6c 69 74 *zVfs){. sqlit
81ab0 65 33 5f 76 66 73 20 2a 70 56 66 73 20 3d 20 30 e3_vfs *pVfs = 0
81ac0 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 ;.#if SQLITE_THR
81ad0 45 41 44 53 41 46 45 0a 20 20 73 71 6c 69 74 65 EADSAFE. sqlite
81ae0 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b 0a 3_mutex *mutex;.
81af0 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 53 #endif.#ifndef S
81b00 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 QLITE_OMIT_AUTOI
81b10 4e 49 54 0a 20 20 69 6e 74 20 72 63 20 3d 20 73 NIT. int rc = s
81b20 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a qlite3_initializ
81b30 65 28 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 e();. if( rc )
81b40 72 65 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66 return 0;.#endif
81b50 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 .#if SQLITE_THRE
81b60 41 44 53 41 46 45 0a 20 20 6d 75 74 65 78 20 3d ADSAFE. mutex =
81b70 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c sqlite3MutexAll
81b80 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f oc(SQLITE_MUTEX_
81b90 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a STATIC_MASTER);.
81ba0 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33 #endif. sqlite3
81bb0 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 _mutex_enter(mut
81bc0 65 78 29 3b 0a 20 20 66 6f 72 28 70 56 66 73 20 ex);. for(pVfs
81bd0 3d 20 76 66 73 4c 69 73 74 3b 20 70 56 66 73 3b = vfsList; pVfs;
81be0 20 70 56 66 73 3d 70 56 66 73 2d 3e 70 4e 65 78 pVfs=pVfs->pNex
81bf0 74 29 7b 0a 20 20 20 20 69 66 28 20 7a 56 66 73 t){. if( zVfs
81c00 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 ==0 ) break;.
81c10 20 69 66 28 20 73 74 72 63 6d 70 28 7a 56 66 73 if( strcmp(zVfs
81c20 2c 20 70 56 66 73 2d 3e 7a 4e 61 6d 65 29 3d 3d , pVfs->zName)==
81c30 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 0 ) break;. }.
81c40 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
81c50 65 61 76 65 28 6d 75 74 65 78 29 3b 0a 20 20 72 eave(mutex);. r
81c60 65 74 75 72 6e 20 70 56 66 73 3b 0a 7d 0a 0a 2f eturn pVfs;.}../
81c70 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b 20 61 20 56 46 *.** Unlink a VF
81c80 53 20 66 72 6f 6d 20 74 68 65 20 6c 69 6e 6b 65 S from the linke
81c90 64 20 6c 69 73 74 0a 2a 2f 0a 73 74 61 74 69 63 d list.*/.static
81ca0 20 76 6f 69 64 20 76 66 73 55 6e 6c 69 6e 6b 28 void vfsUnlink(
81cb0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 sqlite3_vfs *pVf
81cc0 73 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 s){. assert( sq
81cd0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
81ce0 28 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c (sqlite3MutexAll
81cf0 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f oc(SQLITE_MUTEX_
81d00 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 29 20 STATIC_MASTER))
81d10 29 3b 0a 20 20 69 66 28 20 70 56 66 73 3d 3d 30 );. if( pVfs==0
81d20 20 29 7b 0a 20 20 20 20 2f 2a 20 4e 6f 2d 6f 70 ){. /* No-op
81d30 20 2a 2f 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 */. }else if(
81d40 76 66 73 4c 69 73 74 3d 3d 70 56 66 73 20 29 7b vfsList==pVfs ){
81d50 0a 20 20 20 20 76 66 73 4c 69 73 74 20 3d 20 70 . vfsList = p
81d60 56 66 73 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 65 Vfs->pNext;. }e
81d70 6c 73 65 20 69 66 28 20 76 66 73 4c 69 73 74 20 lse if( vfsList
81d80 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 ){. sqlite3_v
81d90 66 73 20 2a 70 20 3d 20 76 66 73 4c 69 73 74 3b fs *p = vfsList;
81da0 0a 20 20 20 20 77 68 69 6c 65 28 20 70 2d 3e 70 . while( p->p
81db0 4e 65 78 74 20 26 26 20 70 2d 3e 70 4e 65 78 74 Next && p->pNext
81dc0 21 3d 70 56 66 73 20 29 7b 0a 20 20 20 20 20 20 !=pVfs ){.
81dd0 70 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 p = p->pNext;.
81de0 20 20 7d 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 }. if( p->p
81df0 4e 65 78 74 3d 3d 70 56 66 73 20 29 7b 0a 20 20 Next==pVfs ){.
81e00 20 20 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 70 p->pNext = p
81e10 56 66 73 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 Vfs->pNext;.
81e20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 }. }.}../*.** R
81e30 65 67 69 73 74 65 72 20 61 20 56 46 53 20 77 69 egister a VFS wi
81e40 74 68 20 74 68 65 20 73 79 73 74 65 6d 2e 20 20 th the system.
81e50 49 74 20 69 73 20 68 61 72 6d 6c 65 73 73 20 74 It is harmless t
81e60 6f 20 72 65 67 69 73 74 65 72 20 74 68 65 20 73 o register the s
81e70 61 6d 65 0a 2a 2a 20 56 46 53 20 6d 75 6c 74 69 ame.** VFS multi
81e80 70 6c 65 20 74 69 6d 65 73 2e 20 20 54 68 65 20 ple times. The
81e90 6e 65 77 20 56 46 53 20 62 65 63 6f 6d 65 73 20 new VFS becomes
81ea0 74 68 65 20 64 65 66 61 75 6c 74 20 69 66 20 6d the default if m
81eb0 61 6b 65 44 66 6c 74 20 69 73 0a 2a 2a 20 74 72 akeDflt is.** tr
81ec0 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 ue..*/.SQLITE_AP
81ed0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 66 I int sqlite3_vf
81ee0 73 5f 72 65 67 69 73 74 65 72 28 73 71 6c 69 74 s_register(sqlit
81ef0 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e e3_vfs *pVfs, in
81f00 74 20 6d 61 6b 65 44 66 6c 74 29 7b 0a 20 20 73 t makeDflt){. s
81f10 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 qlite3_mutex *mu
81f20 74 65 78 20 3d 20 30 3b 0a 23 69 66 6e 64 65 66 tex = 0;.#ifndef
81f30 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 SQLITE_OMIT_AUT
81f40 4f 49 4e 49 54 0a 20 20 69 6e 74 20 72 63 20 3d OINIT. int rc =
81f50 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c sqlite3_initial
81f60 69 7a 65 28 29 3b 0a 20 20 69 66 28 20 72 63 20 ize();. if( rc
81f70 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 23 65 6e ) return rc;.#en
81f80 64 69 66 0a 20 20 6d 75 74 65 78 20 3d 20 73 71 dif. mutex = sq
81f90 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 lite3MutexAlloc(
81fa0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 SQLITE_MUTEX_STA
81fb0 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a 20 20 73 TIC_MASTER);. s
81fc0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 qlite3_mutex_ent
81fd0 65 72 28 6d 75 74 65 78 29 3b 0a 20 20 76 66 73 er(mutex);. vfs
81fe0 55 6e 6c 69 6e 6b 28 70 56 66 73 29 3b 0a 20 20 Unlink(pVfs);.
81ff0 69 66 28 20 6d 61 6b 65 44 66 6c 74 20 7c 7c 20 if( makeDflt ||
82000 76 66 73 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20 vfsList==0 ){.
82010 20 20 70 56 66 73 2d 3e 70 4e 65 78 74 20 3d 20 pVfs->pNext =
82020 76 66 73 4c 69 73 74 3b 0a 20 20 20 20 76 66 73 vfsList;. vfs
82030 4c 69 73 74 20 3d 20 70 56 66 73 3b 0a 20 20 7d List = pVfs;. }
82040 65 6c 73 65 7b 0a 20 20 20 20 70 56 66 73 2d 3e else{. pVfs->
82050 70 4e 65 78 74 20 3d 20 76 66 73 4c 69 73 74 2d pNext = vfsList-
82060 3e 70 4e 65 78 74 3b 0a 20 20 20 20 76 66 73 4c >pNext;. vfsL
82070 69 73 74 2d 3e 70 4e 65 78 74 20 3d 20 70 56 66 ist->pNext = pVf
82080 73 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 s;. }. assert(
82090 76 66 73 4c 69 73 74 29 3b 0a 20 20 73 71 6c 69 vfsList);. sqli
820a0 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
820b0 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e mutex);. return
820c0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f SQLITE_OK;.}../
820d0 2a 0a 2a 2a 20 55 6e 72 65 67 69 73 74 65 72 20 *.** Unregister
820e0 61 20 56 46 53 20 73 6f 20 74 68 61 74 20 69 74 a VFS so that it
820f0 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 61 63 is no longer ac
82100 63 65 73 73 69 62 6c 65 2e 0a 2a 2f 0a 53 51 4c cessible..*/.SQL
82110 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
82120 74 65 33 5f 76 66 73 5f 75 6e 72 65 67 69 73 74 te3_vfs_unregist
82130 65 72 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a er(sqlite3_vfs *
82140 70 56 66 73 29 7b 0a 23 69 66 20 53 51 4c 49 54 pVfs){.#if SQLIT
82150 45 5f 54 48 52 45 41 44 53 41 46 45 0a 20 20 73 E_THREADSAFE. s
82160 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 qlite3_mutex *mu
82170 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 tex = sqlite3Mut
82180 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d exAlloc(SQLITE_M
82190 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 UTEX_STATIC_MAST
821a0 45 52 29 3b 0a 23 65 6e 64 69 66 0a 20 20 73 71 ER);.#endif. sq
821b0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 lite3_mutex_ente
821c0 72 28 6d 75 74 65 78 29 3b 0a 20 20 76 66 73 55 r(mutex);. vfsU
821d0 6e 6c 69 6e 6b 28 70 56 66 73 29 3b 0a 20 20 73 nlink(pVfs);. s
821e0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
821f0 76 65 28 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 ve(mutex);. ret
82200 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
82210 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
82220 2a 20 45 6e 64 20 6f 66 20 6f 73 2e 63 20 2a 2a * End of os.c **
82230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
82240 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
82250 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
82260 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
82270 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 66 61 75 * Begin file fau
82280 6c 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a lt.c ***********
82290 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
822a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
822b0 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4a 61 6e /./*.** 2008 Jan
822c0 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 22.**.** The au
822d0 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 thor disclaims c
822e0 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 opyright to this
822f0 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 source code. I
82300 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 n place of.** a
82310 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 legal notice, he
82320 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 re is a blessing
82330 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 :.**.** May y
82340 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e ou do good and n
82350 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d ot evil..** M
82360 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 ay you find forg
82370 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 iveness for your
82380 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 self and forgive
82390 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d others..** M
823a0 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 ay you share fre
823b0 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e ely, never takin
823c0 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 g more than you
823d0 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a give..**.*******
823e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
823f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
82400 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
82410 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
82420 2a 2a 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 66 61 **.**.** $Id: fa
82430 75 6c 74 2e 63 2c 76 20 31 2e 31 31 20 32 30 30 ult.c,v 1.11 200
82440 38 2f 30 39 2f 30 32 20 30 30 3a 35 32 3a 35 32 8/09/02 00:52:52
82450 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f drh Exp $.*/../
82460 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 *.** This file c
82470 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 74 6f 20 ontains code to
82480 73 75 70 70 6f 72 74 20 74 68 65 20 63 6f 6e 63 support the conc
82490 65 70 74 20 6f 66 20 22 62 65 6e 69 67 6e 22 20 ept of "benign"
824a0 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 .** malloc failu
824b0 72 65 73 20 28 77 68 65 6e 20 74 68 65 20 78 4d res (when the xM
824c0 61 6c 6c 6f 63 28 29 20 6f 72 20 78 52 65 61 6c alloc() or xReal
824d0 6c 6f 63 28 29 20 6d 65 74 68 6f 64 20 6f 66 20 loc() method of
824e0 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d the.** sqlite3_m
824f0 65 6d 5f 6d 65 74 68 6f 64 73 20 73 74 72 75 63 em_methods struc
82500 74 75 72 65 20 66 61 69 6c 73 20 74 6f 20 61 6c ture fails to al
82510 6c 6f 63 61 74 65 20 61 20 62 6c 6f 63 6b 20 6f locate a block o
82520 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6e 64 20 f memory.** and
82530 72 65 74 75 72 6e 73 20 30 29 2e 20 0a 2a 2a 0a returns 0). .**.
82540 2a 2a 20 4d 6f 73 74 20 6d 61 6c 6c 6f 63 20 66 ** Most malloc f
82550 61 69 6c 75 72 65 73 20 61 72 65 20 6e 6f 6e 2d ailures are non-
82560 62 65 6e 69 67 6e 2e 20 41 66 74 65 72 20 74 68 benign. After th
82570 65 79 20 6f 63 63 75 72 2c 20 53 51 4c 69 74 65 ey occur, SQLite
82580 0a 2a 2a 20 61 62 61 6e 64 6f 6e 73 20 74 68 65 .** abandons the
82590 20 63 75 72 72 65 6e 74 20 6f 70 65 72 61 74 69 current operati
825a0 6f 6e 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61 on and returns a
825b0 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 28 75 73 n error code (us
825c0 75 61 6c 6c 79 0a 2a 2a 20 53 51 4c 49 54 45 5f ually.** SQLITE_
825d0 4e 4f 4d 45 4d 29 20 74 6f 20 74 68 65 20 75 73 NOMEM) to the us
825e0 65 72 2e 20 48 6f 77 65 76 65 72 2c 20 73 6f 6d er. However, som
825f0 65 74 69 6d 65 73 20 61 20 66 61 75 6c 74 20 69 etimes a fault i
82600 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 6c s not necessaril
82610 79 0a 2a 2a 20 66 61 74 61 6c 2e 20 46 6f 72 20 y.** fatal. For
82620 65 78 61 6d 70 6c 65 2c 20 69 66 20 61 20 6d 61 example, if a ma
82630 6c 6c 6f 63 20 66 61 69 6c 73 20 77 68 69 6c 65 lloc fails while
82640 20 72 65 73 69 7a 69 6e 67 20 61 20 68 61 73 68 resizing a hash
82650 20 74 61 62 6c 65 2c 20 74 68 69 73 20 0a 2a 2a table, this .**
82660 20 69 73 20 63 6f 6d 70 6c 65 74 65 6c 79 20 72 is completely r
82670 65 63 6f 76 65 72 61 62 6c 65 20 73 69 6d 70 6c ecoverable simpl
82680 79 20 62 79 20 6e 6f 74 20 63 61 72 72 79 69 6e y by not carryin
82690 67 20 6f 75 74 20 74 68 65 20 72 65 73 69 7a 65 g out the resize
826a0 2e 20 54 68 65 20 0a 2a 2a 20 68 61 73 68 20 74 . The .** hash t
826b0 61 62 6c 65 20 77 69 6c 6c 20 63 6f 6e 74 69 6e able will contin
826c0 75 65 20 74 6f 20 66 75 6e 63 74 69 6f 6e 20 6e ue to function n
826d0 6f 72 6d 61 6c 6c 79 2e 20 20 53 6f 20 61 20 6d ormally. So a m
826e0 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 20 0a 2a alloc failure .*
826f0 2a 20 64 75 72 69 6e 67 20 61 20 68 61 73 68 20 * during a hash
82700 74 61 62 6c 65 20 72 65 73 69 7a 65 20 69 73 20 table resize is
82710 61 20 62 65 6e 69 67 6e 20 66 61 75 6c 74 2e 0a a benign fault..
82720 2a 2f 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c */...#ifndef SQL
82730 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e ITE_OMIT_BUILTIN
82740 5f 54 45 53 54 0a 0a 2f 2a 0a 2a 2a 20 47 6c 6f _TEST../*.** Glo
82750 62 61 6c 20 76 61 72 69 61 62 6c 65 73 2e 0a 2a bal variables..*
82760 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 /.typedef struct
82770 20 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f BenignMallocHoo
82780 6b 73 20 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 ks BenignMallocH
82790 6f 6f 6b 73 3b 0a 73 74 61 74 69 63 20 53 51 4c ooks;.static SQL
827a0 49 54 45 5f 57 53 44 20 73 74 72 75 63 74 20 42 ITE_WSD struct B
827b0 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 enignMallocHooks
827c0 20 7b 0a 20 20 76 6f 69 64 20 28 2a 78 42 65 6e {. void (*xBen
827d0 69 67 6e 42 65 67 69 6e 29 28 76 6f 69 64 29 3b ignBegin)(void);
827e0 0a 20 20 76 6f 69 64 20 28 2a 78 42 65 6e 69 67 . void (*xBenig
827f0 6e 45 6e 64 29 28 76 6f 69 64 29 3b 0a 7d 20 73 nEnd)(void);.} s
82800 71 6c 69 74 65 33 48 6f 6f 6b 73 20 3d 20 7b 20 qlite3Hooks = {
82810 30 2c 20 30 20 7d 3b 0a 0a 2f 2a 20 54 68 65 20 0, 0 };../* The
82820 22 77 73 64 48 6f 6f 6b 73 22 20 6d 61 63 72 6f "wsdHooks" macro
82830 20 77 69 6c 6c 20 72 65 73 6f 6c 76 65 20 74 6f will resolve to
82840 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 the appropriate
82850 20 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f BenignMallocHoo
82860 6b 73 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e ks.** structure.
82870 20 20 49 66 20 77 72 69 74 61 62 6c 65 20 73 74 If writable st
82880 61 74 69 63 20 64 61 74 61 20 69 73 20 75 6e 73 atic data is uns
82890 75 70 70 6f 72 74 65 64 20 6f 6e 20 74 68 65 20 upported on the
828a0 74 61 72 67 65 74 2c 0a 2a 2a 20 77 65 20 68 61 target,.** we ha
828b0 76 65 20 74 6f 20 6c 6f 63 61 74 65 20 74 68 65 ve to locate the
828c0 20 73 74 61 74 65 20 76 65 63 74 6f 72 20 61 74 state vector at
828d0 20 72 75 6e 2d 74 69 6d 65 2e 20 20 49 6e 20 74 run-time. In t
828e0 68 65 20 6d 6f 72 65 20 63 6f 6d 6d 6f 6e 0a 2a he more common.*
828f0 2a 20 63 61 73 65 20 77 68 65 72 65 20 77 72 69 * case where wri
82900 74 61 62 6c 65 20 73 74 61 74 69 63 20 64 61 74 table static dat
82910 61 20 69 73 20 73 75 70 70 6f 72 74 65 64 2c 20 a is supported,
82920 77 73 64 48 6f 6f 6b 73 20 63 61 6e 20 72 65 66 wsdHooks can ref
82930 65 72 20 64 69 72 65 63 74 6c 79 0a 2a 2a 20 74 er directly.** t
82940 6f 20 74 68 65 20 22 73 71 6c 69 74 65 33 48 6f o the "sqlite3Ho
82950 6f 6b 73 22 20 73 74 61 74 65 20 76 65 63 74 6f oks" state vecto
82960 72 20 64 65 63 6c 61 72 65 64 20 61 62 6f 76 65 r declared above
82970 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 ..*/.#ifdef SQLI
82980 54 45 5f 4f 4d 49 54 5f 57 53 44 0a 23 20 64 65 TE_OMIT_WSD.# de
82990 66 69 6e 65 20 77 73 64 48 6f 6f 6b 73 49 6e 69 fine wsdHooksIni
829a0 74 20 5c 0a 20 20 42 65 6e 69 67 6e 4d 61 6c 6c t \. BenignMall
829b0 6f 63 48 6f 6f 6b 73 20 2a 78 20 3d 20 26 47 4c ocHooks *x = &GL
829c0 4f 42 41 4c 28 42 65 6e 69 67 6e 4d 61 6c 6c 6f OBAL(BenignMallo
829d0 63 48 6f 6f 6b 73 2c 73 71 6c 69 74 65 33 48 6f cHooks,sqlite3Ho
829e0 6f 6b 73 29 0a 23 20 64 65 66 69 6e 65 20 77 73 oks).# define ws
829f0 64 48 6f 6f 6b 73 20 78 5b 30 5d 0a 23 65 6c 73 dHooks x[0].#els
82a00 65 0a 23 20 64 65 66 69 6e 65 20 77 73 64 48 6f e.# define wsdHo
82a10 6f 6b 73 49 6e 69 74 0a 23 20 64 65 66 69 6e 65 oksInit.# define
82a20 20 77 73 64 48 6f 6f 6b 73 20 73 71 6c 69 74 65 wsdHooks sqlite
82a30 33 48 6f 6f 6b 73 0a 23 65 6e 64 69 66 0a 0a 0a 3Hooks.#endif...
82a40 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 68 /*.** Register h
82a50 6f 6f 6b 73 20 74 6f 20 63 61 6c 6c 20 77 68 65 ooks to call whe
82a60 6e 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 n sqlite3BeginBe
82a70 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 20 61 6e 64 nignMalloc() and
82a80 0a 2a 2a 20 73 71 6c 69 74 65 33 45 6e 64 42 65 .** sqlite3EndBe
82a90 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 20 61 72 65 nignMalloc() are
82aa0 20 63 61 6c 6c 65 64 2c 20 72 65 73 70 65 63 74 called, respect
82ab0 69 76 65 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ively..*/.SQLITE
82ac0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
82ad0 6c 69 74 65 33 42 65 6e 69 67 6e 4d 61 6c 6c 6f lite3BenignMallo
82ae0 63 48 6f 6f 6b 73 28 0a 20 20 76 6f 69 64 20 28 cHooks(. void (
82af0 2a 78 42 65 6e 69 67 6e 42 65 67 69 6e 29 28 76 *xBenignBegin)(v
82b00 6f 69 64 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78 oid),. void (*x
82b10 42 65 6e 69 67 6e 45 6e 64 29 28 76 6f 69 64 29 BenignEnd)(void)
82b20 0a 29 7b 0a 20 20 77 73 64 48 6f 6f 6b 73 49 6e .){. wsdHooksIn
82b30 69 74 3b 0a 20 20 77 73 64 48 6f 6f 6b 73 2e 78 it;. wsdHooks.x
82b40 42 65 6e 69 67 6e 42 65 67 69 6e 20 3d 20 78 42 BenignBegin = xB
82b50 65 6e 69 67 6e 42 65 67 69 6e 3b 0a 20 20 77 73 enignBegin;. ws
82b60 64 48 6f 6f 6b 73 2e 78 42 65 6e 69 67 6e 45 6e dHooks.xBenignEn
82b70 64 20 3d 20 78 42 65 6e 69 67 6e 45 6e 64 3b 0a d = xBenignEnd;.
82b80 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 28 73 }../*.** This (s
82b90 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d qlite3EndBenignM
82ba0 61 6c 6c 6f 63 28 29 29 20 69 73 20 63 61 6c 6c alloc()) is call
82bb0 65 64 20 62 79 20 53 51 4c 69 74 65 20 63 6f 64 ed by SQLite cod
82bc0 65 20 74 6f 20 69 6e 64 69 63 61 74 65 20 74 68 e to indicate th
82bd0 61 74 0a 2a 2a 20 73 75 62 73 65 71 75 65 6e 74 at.** subsequent
82be0 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73 malloc failures
82bf0 20 61 72 65 20 62 65 6e 69 67 6e 2e 20 41 20 63 are benign. A c
82c00 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 45 6e all to sqlite3En
82c10 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 0a dBenignMalloc().
82c20 2a 2a 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 ** indicates tha
82c30 74 20 73 75 62 73 65 71 75 65 6e 74 20 6d 61 6c t subsequent mal
82c40 6c 6f 63 20 66 61 69 6c 75 72 65 73 20 61 72 65 loc failures are
82c50 20 6e 6f 6e 2d 62 65 6e 69 67 6e 2e 0a 2a 2f 0a non-benign..*/.
82c60 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
82c70 6f 69 64 20 73 71 6c 69 74 65 33 42 65 67 69 6e oid sqlite3Begin
82c80 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 76 6f 69 BenignMalloc(voi
82c90 64 29 7b 0a 20 20 77 73 64 48 6f 6f 6b 73 49 6e d){. wsdHooksIn
82ca0 69 74 3b 0a 20 20 69 66 28 20 77 73 64 48 6f 6f it;. if( wsdHoo
82cb0 6b 73 2e 78 42 65 6e 69 67 6e 42 65 67 69 6e 20 ks.xBenignBegin
82cc0 29 7b 0a 20 20 20 20 77 73 64 48 6f 6f 6b 73 2e ){. wsdHooks.
82cd0 78 42 65 6e 69 67 6e 42 65 67 69 6e 28 29 3b 0a xBenignBegin();.
82ce0 20 20 7d 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 }.}.SQLITE_PRI
82cf0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
82d00 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 3EndBenignMalloc
82d10 28 76 6f 69 64 29 7b 0a 20 20 77 73 64 48 6f 6f (void){. wsdHoo
82d20 6b 73 49 6e 69 74 3b 0a 20 20 69 66 28 20 77 73 ksInit;. if( ws
82d30 64 48 6f 6f 6b 73 2e 78 42 65 6e 69 67 6e 45 6e dHooks.xBenignEn
82d40 64 20 29 7b 0a 20 20 20 20 77 73 64 48 6f 6f 6b d ){. wsdHook
82d50 73 2e 78 42 65 6e 69 67 6e 45 6e 64 28 29 3b 0a s.xBenignEnd();.
82d60 20 20 7d 0a 7d 0a 0a 23 65 6e 64 69 66 20 20 20 }.}..#endif
82d70 2f 2a 20 23 69 66 6e 64 65 66 20 53 51 4c 49 54 /* #ifndef SQLIT
82d80 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54 E_OMIT_BUILTIN_T
82d90 45 53 54 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a EST */../*******
82da0 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 66 ******* End of f
82db0 61 75 6c 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a ault.c *********
82dc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
82dd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
82de0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
82df0 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 ******* Begin fi
82e00 6c 65 20 6d 65 6d 30 2e 63 20 2a 2a 2a 2a 2a 2a le mem0.c ******
82e10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
82e20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
82e30 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 ******/./*.** 20
82e40 30 38 20 4f 63 74 6f 62 65 72 20 32 38 0a 2a 2a 08 October 28.**
82e50 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 .** The author d
82e60 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 isclaims copyrig
82e70 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 ht to this sourc
82e80 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 e code. In plac
82e90 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 e of.** a legal
82ea0 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 notice, here is
82eb0 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a a blessing:.**.*
82ec0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 * May you do
82ed0 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 good and not evi
82ee0 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 l..** May you
82ef0 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 find forgivenes
82f00 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 s for yourself a
82f10 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 nd forgive other
82f20 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 s..** May you
82f30 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e share freely, n
82f40 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 ever taking more
82f50 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a than you give..
82f60 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
82f70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
82f80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
82f90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
82fa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a ************.**.
82fb0 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e ** This file con
82fc0 74 61 69 6e 73 20 61 20 6e 6f 2d 6f 70 20 6d 65 tains a no-op me
82fd0 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
82fe0 64 72 69 76 65 72 73 20 66 6f 72 20 75 73 65 20 drivers for use
82ff0 77 68 65 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 5a when.** SQLITE_Z
83000 45 52 4f 5f 4d 41 4c 4c 4f 43 20 69 73 20 64 65 ERO_MALLOC is de
83010 66 69 6e 65 64 2e 20 20 54 68 65 20 61 6c 6c 6f fined. The allo
83020 63 61 74 69 6f 6e 20 64 72 69 76 65 72 73 20 69 cation drivers i
83030 6d 70 6c 65 6d 65 6e 74 65 64 0a 2a 2a 20 68 65 mplemented.** he
83040 72 65 20 61 6c 77 61 79 73 20 66 61 69 6c 2e 20 re always fail.
83050 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 6f 74 SQLite will not
83060 20 6f 70 65 72 61 74 65 20 77 69 74 68 20 74 68 operate with th
83070 65 73 65 20 64 72 69 76 65 72 73 2e 20 20 54 68 ese drivers. Th
83080 65 73 65 0a 2a 2a 20 61 72 65 20 6d 65 72 65 6c ese.** are merel
83090 79 20 70 6c 61 63 65 68 6f 6c 64 65 72 73 2e 20 y placeholders.
830a0 20 52 65 61 6c 20 64 72 69 76 65 72 73 20 6d 75 Real drivers mu
830b0 73 74 20 62 65 20 73 75 62 73 74 69 74 75 74 65 st be substitute
830c0 64 20 75 73 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 d using.** sqlit
830d0 65 33 5f 63 6f 6e 66 69 67 28 29 20 62 65 66 6f e3_config() befo
830e0 72 65 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6f re SQLite will o
830f0 70 65 72 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 perate..**.** $I
83100 64 3a 20 6d 65 6d 30 2e 63 2c 76 20 31 2e 31 20 d: mem0.c,v 1.1
83110 32 30 30 38 2f 31 30 2f 32 38 20 31 38 3a 35 38 2008/10/28 18:58
83120 3a 32 30 20 64 72 68 20 45 78 70 20 24 0a 2a 2f :20 drh Exp $.*/
83130 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 ../*.** This ver
83140 73 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f sion of the memo
83150 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 ry allocator is
83160 74 68 65 20 64 65 66 61 75 6c 74 2e 20 20 49 74 the default. It
83170 20 69 73 0a 2a 2a 20 75 73 65 64 20 77 68 65 6e is.** used when
83180 20 6e 6f 20 6f 74 68 65 72 20 6d 65 6d 6f 72 79 no other memory
83190 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 73 70 allocator is sp
831a0 65 63 69 66 69 65 64 20 75 73 69 6e 67 20 63 6f ecified using co
831b0 6d 70 69 6c 65 2d 74 69 6d 65 0a 2a 2a 20 6d 61 mpile-time.** ma
831c0 63 72 6f 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 cros..*/.#ifdef
831d0 53 51 4c 49 54 45 5f 5a 45 52 4f 5f 4d 41 4c 4c SQLITE_ZERO_MALL
831e0 4f 43 0a 0a 2f 2a 0a 2a 2a 20 4e 6f 2d 6f 70 20 OC../*.** No-op
831f0 76 65 72 73 69 6f 6e 73 20 6f 66 20 61 6c 6c 20 versions of all
83200 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
83210 6e 20 72 6f 75 74 69 6e 65 73 0a 2a 2f 0a 73 74 n routines.*/.st
83220 61 74 69 63 20 76 6f 69 64 20 2a 73 71 6c 69 74 atic void *sqlit
83230 65 33 4d 65 6d 4d 61 6c 6c 6f 63 28 69 6e 74 20 e3MemMalloc(int
83240 6e 42 79 74 65 29 7b 20 72 65 74 75 72 6e 20 30 nByte){ return 0
83250 3b 20 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 ; }.static void
83260 73 71 6c 69 74 65 33 4d 65 6d 46 72 65 65 28 76 sqlite3MemFree(v
83270 6f 69 64 20 2a 70 50 72 69 6f 72 29 7b 20 72 65 oid *pPrior){ re
83280 74 75 72 6e 3b 20 7d 0a 73 74 61 74 69 63 20 76 turn; }.static v
83290 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d 65 6d 52 oid *sqlite3MemR
832a0 65 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 50 72 ealloc(void *pPr
832b0 69 6f 72 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b ior, int nByte){
832c0 20 72 65 74 75 72 6e 20 30 3b 20 7d 0a 73 74 61 return 0; }.sta
832d0 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 4d tic int sqlite3M
832e0 65 6d 53 69 7a 65 28 76 6f 69 64 20 2a 70 50 72 emSize(void *pPr
832f0 69 6f 72 29 7b 20 72 65 74 75 72 6e 20 30 3b 20 ior){ return 0;
83300 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c }.static int sql
83310 69 74 65 33 4d 65 6d 52 6f 75 6e 64 75 70 28 69 ite3MemRoundup(i
83320 6e 74 20 6e 29 7b 20 72 65 74 75 72 6e 20 6e 3b nt n){ return n;
83330 20 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 }.static int sq
83340 6c 69 74 65 33 4d 65 6d 49 6e 69 74 28 76 6f 69 lite3MemInit(voi
83350 64 20 2a 4e 6f 74 55 73 65 64 29 7b 20 72 65 74 d *NotUsed){ ret
83360 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d urn SQLITE_OK; }
83370 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c .static void sql
83380 69 74 65 33 4d 65 6d 53 68 75 74 64 6f 77 6e 28 ite3MemShutdown(
83390 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b 20 void *NotUsed){
833a0 72 65 74 75 72 6e 3b 20 7d 0a 0a 2f 2a 0a 2a 2a return; }../*.**
833b0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 This routine is
833c0 20 74 68 65 20 6f 6e 6c 79 20 72 6f 75 74 69 6e the only routin
833d0 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 77 e in this file w
833e0 69 74 68 20 65 78 74 65 72 6e 61 6c 20 6c 69 6e ith external lin
833f0 6b 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 50 6f 70 75 kage..**.** Popu
83400 6c 61 74 65 20 74 68 65 20 6c 6f 77 2d 6c 65 76 late the low-lev
83410 65 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 el memory alloca
83420 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 70 6f tion function po
83430 69 6e 74 65 72 73 20 69 6e 0a 2a 2a 20 73 71 6c inters in.** sql
83440 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
83450 2e 6d 20 77 69 74 68 20 70 6f 69 6e 74 65 72 73 .m with pointers
83460 20 74 6f 20 74 68 65 20 72 6f 75 74 69 6e 65 73 to the routines
83470 20 69 6e 20 74 68 69 73 20 66 69 6c 65 2e 0a 2a in this file..*
83480 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
83490 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d void sqlite3Mem
834a0 53 65 74 44 65 66 61 75 6c 74 28 76 6f 69 64 29 SetDefault(void)
834b0 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 {. static const
834c0 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 sqlite3_mem_met
834d0 68 6f 64 73 20 64 65 66 61 75 6c 74 4d 65 74 68 hods defaultMeth
834e0 6f 64 73 20 3d 20 7b 0a 20 20 20 20 20 73 71 6c ods = {. sql
834f0 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 2c 0a 20 ite3MemMalloc,.
83500 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 46 72 sqlite3MemFr
83510 65 65 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 ee,. sqlite3
83520 4d 65 6d 52 65 61 6c 6c 6f 63 2c 0a 20 20 20 20 MemRealloc,.
83530 20 73 71 6c 69 74 65 33 4d 65 6d 53 69 7a 65 2c sqlite3MemSize,
83540 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d . sqlite3Mem
83550 52 6f 75 6e 64 75 70 2c 0a 20 20 20 20 20 73 71 Roundup,. sq
83560 6c 69 74 65 33 4d 65 6d 49 6e 69 74 2c 0a 20 20 lite3MemInit,.
83570 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 53 68 75 sqlite3MemShu
83580 74 64 6f 77 6e 2c 0a 20 20 20 20 20 30 0a 20 20 tdown,. 0.
83590 7d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e };. sqlite3_con
835a0 66 69 67 28 53 51 4c 49 54 45 5f 43 4f 4e 46 49 fig(SQLITE_CONFI
835b0 47 5f 4d 41 4c 4c 4f 43 2c 20 26 64 65 66 61 75 G_MALLOC, &defau
835c0 6c 74 4d 65 74 68 6f 64 73 29 3b 0a 7d 0a 0a 23 ltMethods);.}..#
835d0 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
835e0 5a 45 52 4f 5f 4d 41 4c 4c 4f 43 20 2a 2f 0a 0a ZERO_MALLOC */..
835f0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
83600 45 6e 64 20 6f 66 20 6d 65 6d 30 2e 63 20 2a 2a End of mem0.c **
83610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
83620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
83630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
83640 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
83650 42 65 67 69 6e 20 66 69 6c 65 20 6d 65 6d 31 2e Begin file mem1.
83660 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
83670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
83680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
83690 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67 75 73 /*.** 2007 Augus
836a0 74 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 t 14.**.** The a
836b0 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 uthor disclaims
836c0 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 copyright to thi
836d0 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 s source code.
836e0 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 In place of.** a
836f0 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 legal notice, h
83700 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e ere is a blessin
83710 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 g:.**.** May
83720 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 you do good and
83730 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 not evil..**
83740 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 May you find for
83750 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 giveness for you
83760 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 rself and forgiv
83770 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 e others..**
83780 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 May you share fr
83790 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 eely, never taki
837a0 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 ng more than you
837b0 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a give..**.******
837c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
837d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
837e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
837f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
83800 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 ***.**.** This f
83810 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 6c 6f 77 ile contains low
83820 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c -level memory al
83830 6c 6f 63 61 74 69 6f 6e 20 64 72 69 76 65 72 73 location drivers
83840 20 66 6f 72 20 77 68 65 6e 0a 2a 2a 20 53 51 4c for when.** SQL
83850 69 74 65 20 77 69 6c 6c 20 75 73 65 20 74 68 65 ite will use the
83860 20 73 74 61 6e 64 61 72 64 20 43 2d 6c 69 62 72 standard C-libr
83870 61 72 79 20 6d 61 6c 6c 6f 63 2f 72 65 61 6c 6c ary malloc/reall
83880 6f 63 2f 66 72 65 65 20 69 6e 74 65 72 66 61 63 oc/free interfac
83890 65 0a 2a 2a 20 74 6f 20 6f 62 74 61 69 6e 20 74 e.** to obtain t
838a0 68 65 20 6d 65 6d 6f 72 79 20 69 74 20 6e 65 65 he memory it nee
838b0 64 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 ds..**.** This f
838c0 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 69 6d 70 ile contains imp
838d0 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 lementations of
838e0 74 68 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 the low-level me
838f0 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a mory allocation.
83900 2a 2a 20 72 6f 75 74 69 6e 65 73 20 73 70 65 63 ** routines spec
83910 69 66 69 65 64 20 69 6e 20 74 68 65 20 73 71 6c ified in the sql
83920 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 ite3_mem_methods
83930 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 24 object..**.** $
83940 49 64 3a 20 6d 65 6d 31 2e 63 2c 76 20 31 2e 33 Id: mem1.c,v 1.3
83950 30 20 32 30 30 39 2f 30 33 2f 32 33 20 30 34 3a 0 2009/03/23 04:
83960 33 33 3a 33 33 20 64 61 6e 69 65 6c 6b 31 39 37 33:33 danielk197
83970 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 7 Exp $.*/../*.*
83980 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f * This version o
83990 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c f the memory all
839a0 6f 63 61 74 6f 72 20 69 73 20 74 68 65 20 64 65 ocator is the de
839b0 66 61 75 6c 74 2e 20 20 49 74 20 69 73 0a 2a 2a fault. It is.**
839c0 20 75 73 65 64 20 77 68 65 6e 20 6e 6f 20 6f 74 used when no ot
839d0 68 65 72 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 her memory alloc
839e0 61 74 6f 72 20 69 73 20 73 70 65 63 69 66 69 65 ator is specifie
839f0 64 20 75 73 69 6e 67 20 63 6f 6d 70 69 6c 65 2d d using compile-
83a00 74 69 6d 65 0a 2a 2a 20 6d 61 63 72 6f 73 2e 0a time.** macros..
83a10 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
83a20 5f 53 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43 0a 0a _SYSTEM_MALLOC..
83a30 2f 2a 0a 2a 2a 20 4c 69 6b 65 20 6d 61 6c 6c 6f /*.** Like mallo
83a40 63 28 29 2c 20 62 75 74 20 72 65 6d 65 6d 62 65 c(), but remembe
83a50 72 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 r the size of th
83a60 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 e allocation.**
83a70 73 6f 20 74 68 61 74 20 77 65 20 63 61 6e 20 66 so that we can f
83a80 69 6e 64 20 69 74 20 6c 61 74 65 72 20 75 73 69 ind it later usi
83a90 6e 67 20 73 71 6c 69 74 65 33 4d 65 6d 53 69 7a ng sqlite3MemSiz
83aa0 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 e()..**.** For t
83ab0 68 69 73 20 6c 6f 77 2d 6c 65 76 65 6c 20 72 6f his low-level ro
83ac0 75 74 69 6e 65 2c 20 77 65 20 61 72 65 20 67 75 utine, we are gu
83ad0 61 72 61 6e 74 65 65 64 20 74 68 61 74 20 6e 42 aranteed that nB
83ae0 79 74 65 3e 30 20 62 65 63 61 75 73 65 0a 2a 2a yte>0 because.**
83af0 20 63 61 73 65 73 20 6f 66 20 6e 42 79 74 65 3c cases of nByte<
83b00 3d 30 20 77 69 6c 6c 20 62 65 20 69 6e 74 65 72 =0 will be inter
83b10 63 65 70 74 65 64 20 61 6e 64 20 64 65 61 6c 74 cepted and dealt
83b20 20 77 69 74 68 20 62 79 20 68 69 67 68 65 72 20 with by higher
83b30 6c 65 76 65 6c 0a 2a 2a 20 72 6f 75 74 69 6e 65 level.** routine
83b40 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 s..*/.static voi
83b50 64 20 2a 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c d *sqlite3MemMal
83b60 6c 6f 63 28 69 6e 74 20 6e 42 79 74 65 29 7b 0a loc(int nByte){.
83b70 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 sqlite3_int64
83b80 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 42 *p;. assert( nB
83b90 79 74 65 3e 30 20 29 3b 0a 20 20 6e 42 79 74 65 yte>0 );. nByte
83ba0 20 3d 20 52 4f 55 4e 44 38 28 6e 42 79 74 65 29 = ROUND8(nByte)
83bb0 3b 0a 20 20 70 20 3d 20 6d 61 6c 6c 6f 63 28 20 ;. p = malloc(
83bc0 6e 42 79 74 65 2b 38 20 29 3b 0a 20 20 69 66 28 nByte+8 );. if(
83bd0 20 70 20 29 7b 0a 20 20 20 20 70 5b 30 5d 20 3d p ){. p[0] =
83be0 20 6e 42 79 74 65 3b 0a 20 20 20 20 70 2b 2b 3b nByte;. p++;
83bf0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 76 . }. return (v
83c00 6f 69 64 20 2a 29 70 3b 0a 7d 0a 0a 2f 2a 0a 2a oid *)p;.}../*.*
83c10 2a 20 4c 69 6b 65 20 66 72 65 65 28 29 20 62 75 * Like free() bu
83c20 74 20 77 6f 72 6b 73 20 66 6f 72 20 61 6c 6c 6f t works for allo
83c30 63 61 74 69 6f 6e 73 20 6f 62 74 61 69 6e 65 64 cations obtained
83c40 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 4d 65 6d from sqlite3Mem
83c50 4d 61 6c 6c 6f 63 28 29 0a 2a 2a 20 6f 72 20 73 Malloc().** or s
83c60 71 6c 69 74 65 33 4d 65 6d 52 65 61 6c 6c 6f 63 qlite3MemRealloc
83c70 28 29 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 ()..**.** For th
83c80 69 73 20 6c 6f 77 2d 6c 65 76 65 6c 20 72 6f 75 is low-level rou
83c90 74 69 6e 65 2c 20 77 65 20 61 6c 72 65 61 64 79 tine, we already
83ca0 20 6b 6e 6f 77 20 74 68 61 74 20 70 50 72 69 6f know that pPrio
83cb0 72 21 3d 30 20 73 69 6e 63 65 0a 2a 2a 20 63 61 r!=0 since.** ca
83cc0 73 65 73 20 77 68 65 72 65 20 70 50 72 69 6f 72 ses where pPrior
83cd0 3d 3d 30 20 77 69 6c 6c 20 68 61 76 65 20 62 65 ==0 will have be
83ce0 65 6e 20 69 6e 74 65 63 65 70 74 65 64 20 61 6e en intecepted an
83cf0 64 20 64 65 61 6c 74 20 77 69 74 68 0a 2a 2a 20 d dealt with.**
83d00 62 79 20 68 69 67 68 65 72 2d 6c 65 76 65 6c 20 by higher-level
83d10 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 73 74 61 routines..*/.sta
83d20 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 33 tic void sqlite3
83d30 4d 65 6d 46 72 65 65 28 76 6f 69 64 20 2a 70 50 MemFree(void *pP
83d40 72 69 6f 72 29 7b 0a 20 20 73 71 6c 69 74 65 33 rior){. sqlite3
83d50 5f 69 6e 74 36 34 20 2a 70 20 3d 20 28 73 71 6c _int64 *p = (sql
83d60 69 74 65 33 5f 69 6e 74 36 34 2a 29 70 50 72 69 ite3_int64*)pPri
83d70 6f 72 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 or;. assert( pP
83d80 72 69 6f 72 21 3d 30 20 29 3b 0a 20 20 70 2d 2d rior!=0 );. p--
83d90 3b 0a 20 20 66 72 65 65 28 70 29 3b 0a 7d 0a 0a ;. free(p);.}..
83da0 2f 2a 0a 2a 2a 20 4c 69 6b 65 20 72 65 61 6c 6c /*.** Like reall
83db0 6f 63 28 29 2e 20 20 52 65 73 69 7a 65 20 61 6e oc(). Resize an
83dc0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 70 72 65 76 allocation prev
83dd0 69 6f 75 73 6c 79 20 6f 62 74 61 69 6e 65 64 20 iously obtained
83de0 66 72 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65 33 4d from.** sqlite3M
83df0 65 6d 4d 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a 2a emMalloc()..**.*
83e00 2a 20 46 6f 72 20 74 68 69 73 20 6c 6f 77 2d 6c * For this low-l
83e10 65 76 65 6c 20 69 6e 74 65 72 66 61 63 65 2c 20 evel interface,
83e20 77 65 20 6b 6e 6f 77 20 74 68 61 74 20 70 50 72 we know that pPr
83e30 69 6f 72 21 3d 30 2e 20 20 43 61 73 65 73 20 77 ior!=0. Cases w
83e40 68 65 72 65 0a 2a 2a 20 70 50 72 69 6f 72 3d 3d here.** pPrior==
83e50 30 20 77 68 69 6c 65 20 68 61 76 65 20 62 65 65 0 while have bee
83e60 6e 20 69 6e 74 65 72 63 65 70 74 65 64 20 62 79 n intercepted by
83e70 20 68 69 67 68 65 72 2d 6c 65 76 65 6c 20 72 6f higher-level ro
83e80 75 74 69 6e 65 20 61 6e 64 0a 2a 2a 20 72 65 64 utine and.** red
83e90 69 72 65 63 74 65 64 20 74 6f 20 78 4d 61 6c 6c irected to xMall
83ea0 6f 63 2e 20 20 53 69 6d 69 6c 61 72 6c 79 2c 20 oc. Similarly,
83eb0 77 65 20 6b 6e 6f 77 20 74 68 61 74 20 6e 42 79 we know that nBy
83ec0 74 65 3e 30 20 62 65 63 61 75 73 65 73 0a 2a 2a te>0 becauses.**
83ed0 20 63 61 73 65 73 20 77 68 65 72 65 20 6e 42 79 cases where nBy
83ee0 74 65 3c 3d 30 20 77 69 6c 6c 20 68 61 76 65 20 te<=0 will have
83ef0 62 65 65 6e 20 69 6e 74 65 72 63 65 70 74 65 64 been intercepted
83f00 20 62 79 20 68 69 67 68 65 72 2d 6c 65 76 65 6c by higher-level
83f10 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 61 6e 64 .** routines and
83f20 20 72 65 64 69 72 65 63 74 65 64 20 74 6f 20 78 redirected to x
83f30 46 72 65 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 Free..*/.static
83f40 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d 65 6d void *sqlite3Mem
83f50 52 65 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 50 Realloc(void *pP
83f60 72 69 6f 72 2c 20 69 6e 74 20 6e 42 79 74 65 29 rior, int nByte)
83f70 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 {. sqlite3_int6
83f80 34 20 2a 70 20 3d 20 28 73 71 6c 69 74 65 33 5f 4 *p = (sqlite3_
83f90 69 6e 74 36 34 2a 29 70 50 72 69 6f 72 3b 0a 20 int64*)pPrior;.
83fa0 20 61 73 73 65 72 74 28 20 70 50 72 69 6f 72 21 assert( pPrior!
83fb0 3d 30 20 26 26 20 6e 42 79 74 65 3e 30 20 29 3b =0 && nByte>0 );
83fc0 0a 20 20 6e 42 79 74 65 20 3d 20 52 4f 55 4e 44 . nByte = ROUND
83fd0 38 28 6e 42 79 74 65 29 3b 0a 20 20 70 20 3d 20 8(nByte);. p =
83fe0 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 2a 29 (sqlite3_int64*)
83ff0 70 50 72 69 6f 72 3b 0a 20 20 70 2d 2d 3b 0a 20 pPrior;. p--;.
84000 20 70 20 3d 20 72 65 61 6c 6c 6f 63 28 70 2c 20 p = realloc(p,
84010 6e 42 79 74 65 2b 38 20 29 3b 0a 20 20 69 66 28 nByte+8 );. if(
84020 20 70 20 29 7b 0a 20 20 20 20 70 5b 30 5d 20 3d p ){. p[0] =
84030 20 6e 42 79 74 65 3b 0a 20 20 20 20 70 2b 2b 3b nByte;. p++;
84040 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 76 . }. return (v
84050 6f 69 64 2a 29 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a oid*)p;.}../*.**
84060 20 52 65 70 6f 72 74 20 74 68 65 20 61 6c 6c 6f Report the allo
84070 63 61 74 65 64 20 73 69 7a 65 20 6f 66 20 61 20 cated size of a
84080 70 72 69 6f 72 20 72 65 74 75 72 6e 20 66 72 6f prior return fro
84090 6d 20 78 4d 61 6c 6c 6f 63 28 29 0a 2a 2a 20 6f m xMalloc().** o
840a0 72 20 78 52 65 61 6c 6c 6f 63 28 29 2e 0a 2a 2f r xRealloc()..*/
840b0 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69 .static int sqli
840c0 74 65 33 4d 65 6d 53 69 7a 65 28 76 6f 69 64 20 te3MemSize(void
840d0 2a 70 50 72 69 6f 72 29 7b 0a 20 20 73 71 6c 69 *pPrior){. sqli
840e0 74 65 33 5f 69 6e 74 36 34 20 2a 70 3b 0a 20 20 te3_int64 *p;.
840f0 69 66 28 20 70 50 72 69 6f 72 3d 3d 30 20 29 20 if( pPrior==0 )
84100 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 20 3d 20 return 0;. p =
84110 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 2a 29 (sqlite3_int64*)
84120 70 50 72 69 6f 72 3b 0a 20 20 70 2d 2d 3b 0a 20 pPrior;. p--;.
84130 20 72 65 74 75 72 6e 20 28 69 6e 74 29 70 5b 30 return (int)p[0
84140 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 6e ];.}../*.** Roun
84150 64 20 75 70 20 61 20 72 65 71 75 65 73 74 20 73 d up a request s
84160 69 7a 65 20 74 6f 20 74 68 65 20 6e 65 78 74 20 ize to the next
84170 76 61 6c 69 64 20 61 6c 6c 6f 63 61 74 69 6f 6e valid allocation
84180 20 73 69 7a 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 size..*/.static
84190 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 52 int sqlite3MemR
841a0 6f 75 6e 64 75 70 28 69 6e 74 20 6e 29 7b 0a 20 oundup(int n){.
841b0 20 72 65 74 75 72 6e 20 52 4f 55 4e 44 38 28 6e return ROUND8(n
841c0 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 );.}../*.** Init
841d0 69 61 6c 69 7a 65 20 74 68 69 73 20 6d 6f 64 75 ialize this modu
841e0 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e le..*/.static in
841f0 74 20 73 71 6c 69 74 65 33 4d 65 6d 49 6e 69 74 t sqlite3MemInit
84200 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b (void *NotUsed){
84210 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
84220 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 TER(NotUsed);.
84230 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
84240 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 6e 69 ;.}../*.** Deini
84250 74 69 61 6c 69 7a 65 20 74 68 69 73 20 6d 6f 64 tialize this mod
84260 75 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ule..*/.static v
84270 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 53 68 oid sqlite3MemSh
84280 75 74 64 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f 74 utdown(void *Not
84290 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f Used){. UNUSED_
842a0 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 PARAMETER(NotUse
842b0 64 29 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a d);. return;.}.
842c0 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 ./*.** This rout
842d0 69 6e 65 20 69 73 20 74 68 65 20 6f 6e 6c 79 20 ine is the only
842e0 72 6f 75 74 69 6e 65 20 69 6e 20 74 68 69 73 20 routine in this
842f0 66 69 6c 65 20 77 69 74 68 20 65 78 74 65 72 6e file with extern
84300 61 6c 20 6c 69 6e 6b 61 67 65 2e 0a 2a 2a 0a 2a al linkage..**.*
84310 2a 20 50 6f 70 75 6c 61 74 65 20 74 68 65 20 6c * Populate the l
84320 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 ow-level memory
84330 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e 63 74 allocation funct
84340 69 6f 6e 20 70 6f 69 6e 74 65 72 73 20 69 6e 0a ion pointers in.
84350 2a 2a 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c ** sqlite3Global
84360 43 6f 6e 66 69 67 2e 6d 20 77 69 74 68 20 70 6f Config.m with po
84370 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20 72 6f inters to the ro
84380 75 74 69 6e 65 73 20 69 6e 20 74 68 69 73 20 66 utines in this f
84390 69 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ile..*/.SQLITE_P
843a0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
843b0 74 65 33 4d 65 6d 53 65 74 44 65 66 61 75 6c 74 te3MemSetDefault
843c0 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63 (void){. static
843d0 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d const sqlite3_m
843e0 65 6d 5f 6d 65 74 68 6f 64 73 20 64 65 66 61 75 em_methods defau
843f0 6c 74 4d 65 74 68 6f 64 73 20 3d 20 7b 0a 20 20 ltMethods = {.
84400 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c sqlite3MemMal
84410 6c 6f 63 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 loc,. sqlite
84420 33 4d 65 6d 46 72 65 65 2c 0a 20 20 20 20 20 73 3MemFree,. s
84430 71 6c 69 74 65 33 4d 65 6d 52 65 61 6c 6c 6f 63 qlite3MemRealloc
84440 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 ,. sqlite3Me
84450 6d 53 69 7a 65 2c 0a 20 20 20 20 20 73 71 6c 69 mSize,. sqli
84460 74 65 33 4d 65 6d 52 6f 75 6e 64 75 70 2c 0a 20 te3MemRoundup,.
84470 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 49 6e sqlite3MemIn
84480 69 74 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 it,. sqlite3
84490 4d 65 6d 53 68 75 74 64 6f 77 6e 2c 0a 20 20 20 MemShutdown,.
844a0 20 20 30 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 74 0. };. sqlit
844b0 65 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54 45 e3_config(SQLITE
844c0 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 2c 20 _CONFIG_MALLOC,
844d0 26 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 73 29 &defaultMethods)
844e0 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 ;.}..#endif /* S
844f0 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41 4c QLITE_SYSTEM_MAL
84500 4c 4f 43 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a LOC */../*******
84510 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d ******* End of m
84520 65 6d 31 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a em1.c **********
84530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
84540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
84550 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
84560 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 ******* Begin fi
84570 6c 65 20 6d 65 6d 32 2e 63 20 2a 2a 2a 2a 2a 2a le mem2.c ******
84580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
84590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
845a0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 ******/./*.** 20
845b0 30 37 20 41 75 67 75 73 74 20 31 35 0a 2a 2a 0a 07 August 15.**.
845c0 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 ** The author di
845d0 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 sclaims copyrigh
845e0 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 t to this source
845f0 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 code. In place
84600 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e of.** a legal n
84610 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 otice, here is a
84620 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a blessing:.**.**
84630 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 May you do g
84640 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c ood and not evil
84650 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
84660 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 find forgiveness
84670 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e for yourself an
84680 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 d forgive others
84690 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
846a0 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 share freely, ne
846b0 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 ver taking more
846c0 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a than you give..*
846d0 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *.**************
846e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
846f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
84700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
84710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a ***********.**.*
84720 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 * This file cont
84730 61 69 6e 73 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d ains low-level m
84740 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
84750 20 64 72 69 76 65 72 73 20 66 6f 72 20 77 68 65 drivers for whe
84760 6e 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c n.** SQLite will
84770 20 75 73 65 20 74 68 65 20 73 74 61 6e 64 61 72 use the standar
84780 64 20 43 2d 6c 69 62 72 61 72 79 20 6d 61 6c 6c d C-library mall
84790 6f 63 2f 72 65 61 6c 6c 6f 63 2f 66 72 65 65 20 oc/realloc/free
847a0 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20 74 6f 20 interface.** to
847b0 6f 62 74 61 69 6e 20 74 68 65 20 6d 65 6d 6f 72 obtain the memor
847c0 79 20 69 74 20 6e 65 65 64 73 20 77 68 69 6c 65 y it needs while
847d0 20 61 64 64 69 6e 67 20 6c 6f 74 73 20 6f 66 20 adding lots of
847e0 61 64 64 69 74 69 6f 6e 61 6c 20 64 65 62 75 67 additional debug
847f0 67 69 6e 67 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 ging.** informat
84800 69 6f 6e 20 74 6f 20 65 61 63 68 20 61 6c 6c 6f ion to each allo
84810 63 61 74 69 6f 6e 20 69 6e 20 6f 72 64 65 72 20 cation in order
84820 74 6f 20 68 65 6c 70 20 64 65 74 65 63 74 20 61 to help detect a
84830 6e 64 20 66 69 78 20 6d 65 6d 6f 72 79 0a 2a 2a nd fix memory.**
84840 20 6c 65 61 6b 73 20 61 6e 64 20 6d 65 6d 6f 72 leaks and memor
84850 79 20 75 73 61 67 65 20 65 72 72 6f 72 73 2e 0a y usage errors..
84860 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 **.** This file
84870 63 6f 6e 74 61 69 6e 73 20 69 6d 70 6c 65 6d 65 contains impleme
84880 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 ntations of the
84890 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 low-level memory
848a0 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 72 allocation.** r
848b0 6f 75 74 69 6e 65 73 20 73 70 65 63 69 66 69 65 outines specifie
848c0 64 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 33 d in the sqlite3
848d0 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 6f 62 6a _mem_methods obj
848e0 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 ect..**.** $Id:
848f0 6d 65 6d 32 2e 63 2c 76 20 31 2e 34 35 20 32 30 mem2.c,v 1.45 20
84900 30 39 2f 30 33 2f 32 33 20 30 34 3a 33 33 3a 33 09/03/23 04:33:3
84910 33 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 3 danielk1977 Ex
84920 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 p $.*/../*.** Th
84930 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 is version of th
84940 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 e memory allocat
84950 6f 72 20 69 73 20 75 73 65 64 20 6f 6e 6c 79 20 or is used only
84960 69 66 20 74 68 65 0a 2a 2a 20 53 51 4c 49 54 45 if the.** SQLITE
84970 5f 4d 45 4d 44 45 42 55 47 20 6d 61 63 72 6f 20 _MEMDEBUG macro
84980 69 73 20 64 65 66 69 6e 65 64 0a 2a 2f 0a 23 69 is defined.*/.#i
84990 66 64 65 66 20 53 51 4c 49 54 45 5f 4d 45 4d 44 fdef SQLITE_MEMD
849a0 45 42 55 47 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 EBUG../*.** The
849b0 62 61 63 6b 74 72 61 63 65 20 66 75 6e 63 74 69 backtrace functi
849c0 6f 6e 61 6c 69 74 79 20 69 73 20 6f 6e 6c 79 20 onality is only
849d0 61 76 61 69 6c 61 62 6c 65 20 77 69 74 68 20 47 available with G
849e0 4c 49 42 43 0a 2a 2f 0a 23 69 66 64 65 66 20 5f LIBC.*/.#ifdef _
849f0 5f 47 4c 49 42 43 5f 5f 0a 20 20 65 78 74 65 72 _GLIBC__. exter
84a00 6e 20 69 6e 74 20 62 61 63 6b 74 72 61 63 65 28 n int backtrace(
84a10 76 6f 69 64 2a 2a 2c 69 6e 74 29 3b 0a 20 20 65 void**,int);. e
84a20 78 74 65 72 6e 20 76 6f 69 64 20 62 61 63 6b 74 xtern void backt
84a30 72 61 63 65 5f 73 79 6d 62 6f 6c 73 5f 66 64 28 race_symbols_fd(
84a40 76 6f 69 64 2a 63 6f 6e 73 74 2a 2c 69 6e 74 2c void*const*,int,
84a50 69 6e 74 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 int);.#else.# de
84a60 66 69 6e 65 20 62 61 63 6b 74 72 61 63 65 28 41 fine backtrace(A
84a70 2c 42 29 20 31 0a 23 20 64 65 66 69 6e 65 20 62 ,B) 1.# define b
84a80 61 63 6b 74 72 61 63 65 5f 73 79 6d 62 6f 6c 73 acktrace_symbols
84a90 5f 66 64 28 41 2c 42 2c 43 29 0a 23 65 6e 64 69 _fd(A,B,C).#endi
84aa0 66 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 6d 65 f../*.** Each me
84ab0 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
84ac0 6c 6f 6f 6b 73 20 6c 69 6b 65 20 74 68 69 73 3a looks like this:
84ad0 0a 2a 2a 0a 2a 2a 20 20 2d 2d 2d 2d 2d 2d 2d 2d .**.** --------
84ae0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
84af0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
84b00 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
84b10 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
84b20 0a 2a 2a 20 20 7c 20 54 69 74 6c 65 20 7c 20 20 .** | Title |
84b30 62 61 63 6b 74 72 61 63 65 20 70 6f 69 6e 74 65 backtrace pointe
84b40 72 73 20 7c 20 20 4d 65 6d 42 6c 6f 63 6b 48 64 rs | MemBlockHd
84b50 72 20 7c 20 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 r | allocation
84b60 7c 20 20 45 6e 64 47 75 61 72 64 20 7c 0a 2a 2a | EndGuard |.**
84b70 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d --------------
84b80 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
84b90 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
84ba0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
84bb0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a ----------.**.**
84bc0 20 54 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e The application
84bd0 20 63 6f 64 65 20 73 65 65 73 20 6f 6e 6c 79 20 code sees only
84be0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
84bf0 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20 57 65 allocation. We
84c00 20 68 61 76 65 0a 2a 2a 20 74 6f 20 62 61 63 6b have.** to back
84c10 20 75 70 20 66 72 6f 6d 20 74 68 65 20 61 6c 6c up from the all
84c20 6f 63 61 74 69 6f 6e 20 70 6f 69 6e 74 65 72 20 ocation pointer
84c30 74 6f 20 66 69 6e 64 20 74 68 65 20 4d 65 6d 42 to find the MemB
84c40 6c 6f 63 6b 48 64 72 2e 20 20 54 68 65 0a 2a 2a lockHdr. The.**
84c50 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 74 65 6c MemBlockHdr tel
84c60 6c 73 20 75 73 20 74 68 65 20 73 69 7a 65 20 6f ls us the size o
84c70 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e f the allocation
84c80 20 61 6e 64 20 74 68 65 20 6e 75 6d 62 65 72 20 and the number
84c90 6f 66 0a 2a 2a 20 62 61 63 6b 74 72 61 63 65 20 of.** backtrace
84ca0 70 6f 69 6e 74 65 72 73 2e 20 20 54 68 65 72 65 pointers. There
84cb0 20 69 73 20 61 6c 73 6f 20 61 20 67 75 61 72 64 is also a guard
84cc0 20 77 6f 72 64 20 61 74 20 74 68 65 20 65 6e 64 word at the end
84cd0 20 6f 66 20 74 68 65 0a 2a 2a 20 4d 65 6d 42 6c of the.** MemBl
84ce0 6f 63 6b 48 64 72 2e 0a 2a 2f 0a 73 74 72 75 63 ockHdr..*/.struc
84cf0 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 7b 0a t MemBlockHdr {.
84d00 20 20 69 36 34 20 69 53 69 7a 65 3b 20 20 20 20 i64 iSize;
84d10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
84d20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 /* Size of
84d30 20 74 68 69 73 20 61 6c 6c 6f 63 61 74 69 6f 6e this allocation
84d40 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 4d 65 6d */. struct Mem
84d50 42 6c 6f 63 6b 48 64 72 20 2a 70 4e 65 78 74 2c BlockHdr *pNext,
84d60 20 2a 70 50 72 65 76 3b 20 20 2f 2a 20 4c 69 6e *pPrev; /* Lin
84d70 6b 65 64 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 ked list of all
84d80 75 6e 66 72 65 65 64 20 6d 65 6d 6f 72 79 20 2a unfreed memory *
84d90 2f 0a 20 20 63 68 61 72 20 6e 42 61 63 6b 74 72 /. char nBacktr
84da0 61 63 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 ace;
84db0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
84dc0 72 20 6f 66 20 62 61 63 6b 74 72 61 63 65 73 20 r of backtraces
84dd0 6f 6e 20 74 68 69 73 20 61 6c 6c 6f 63 20 2a 2f on this alloc */
84de0 0a 20 20 63 68 61 72 20 6e 42 61 63 6b 74 72 61 . char nBacktra
84df0 63 65 53 6c 6f 74 73 3b 20 20 20 20 20 20 20 20 ceSlots;
84e00 20 20 20 20 20 20 20 2f 2a 20 41 76 61 69 6c 61 /* Availa
84e10 62 6c 65 20 62 61 63 6b 74 72 61 63 65 20 73 6c ble backtrace sl
84e20 6f 74 73 20 2a 2f 0a 20 20 73 68 6f 72 74 20 6e ots */. short n
84e30 54 69 74 6c 65 3b 20 20 20 20 20 20 20 20 20 20 Title;
84e40 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
84e50 42 79 74 65 73 20 6f 66 20 74 69 74 6c 65 3b 20 Bytes of title;
84e60 69 6e 63 6c 75 64 65 73 20 27 5c 30 27 20 2a 2f includes '\0' */
84e70 0a 20 20 69 6e 74 20 69 46 6f 72 65 47 75 61 72 . int iForeGuar
84e80 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 d;
84e90 20 20 20 20 20 20 20 2f 2a 20 47 75 61 72 64 20 /* Guard
84ea0 77 6f 72 64 20 66 6f 72 20 73 61 6e 69 74 79 20 word for sanity
84eb0 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 47 75 61 */.};../*.** Gua
84ec0 72 64 20 77 6f 72 64 73 0a 2a 2f 0a 23 64 65 66 rd words.*/.#def
84ed0 69 6e 65 20 46 4f 52 45 47 55 41 52 44 20 30 78 ine FOREGUARD 0x
84ee0 38 30 46 35 45 31 35 33 0a 23 64 65 66 69 6e 65 80F5E153.#define
84ef0 20 52 45 41 52 47 55 41 52 44 20 30 78 45 34 36 REARGUARD 0xE46
84f00 37 36 42 35 33 0a 0a 2f 2a 0a 2a 2a 20 4e 75 6d 76B53../*.** Num
84f10 62 65 72 20 6f 66 20 6d 61 6c 6c 6f 63 20 73 69 ber of malloc si
84f20 7a 65 20 69 6e 63 72 65 6d 65 6e 74 73 20 74 6f ze increments to
84f30 20 74 72 61 63 6b 2e 0a 2a 2f 0a 23 64 65 66 69 track..*/.#defi
84f40 6e 65 20 4e 43 53 49 5a 45 20 20 31 30 30 30 0a ne NCSIZE 1000.
84f50 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 68 ./*.** All of th
84f60 65 20 73 74 61 74 69 63 20 76 61 72 69 61 62 6c e static variabl
84f70 65 73 20 75 73 65 64 20 62 79 20 74 68 69 73 20 es used by this
84f80 6d 6f 64 75 6c 65 20 61 72 65 20 63 6f 6c 6c 65 module are colle
84f90 63 74 65 64 0a 2a 2a 20 69 6e 74 6f 20 61 20 73 cted.** into a s
84fa0 69 6e 67 6c 65 20 73 74 72 75 63 74 75 72 65 20 ingle structure
84fb0 6e 61 6d 65 64 20 22 6d 65 6d 22 2e 20 20 54 68 named "mem". Th
84fc0 69 73 20 69 73 20 74 6f 20 6b 65 65 70 20 74 68 is is to keep th
84fd0 65 0a 2a 2a 20 73 74 61 74 69 63 20 76 61 72 69 e.** static vari
84fe0 61 62 6c 65 73 20 6f 72 67 61 6e 69 7a 65 64 20 ables organized
84ff0 61 6e 64 20 74 6f 20 72 65 64 75 63 65 20 6e 61 and to reduce na
85000 6d 65 73 70 61 63 65 20 70 6f 6c 6c 75 74 69 6f mespace pollutio
85010 6e 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73 20 6d n.** when this m
85020 6f 64 75 6c 65 20 69 73 20 63 6f 6d 62 69 6e 65 odule is combine
85030 64 20 77 69 74 68 20 6f 74 68 65 72 20 69 6e 20 d with other in
85040 74 68 65 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e the amalgamation
85050 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75 ..*/.static stru
85060 63 74 20 7b 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a ct {. . /*. *
85070 2a 20 4d 75 74 65 78 20 74 6f 20 63 6f 6e 74 72 * Mutex to contr
85080 6f 6c 20 61 63 63 65 73 73 20 74 6f 20 74 68 65 ol access to the
85090 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
850a0 6f 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a 20 20 on subsystem..
850b0 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 */. sqlite3_mut
850c0 65 78 20 2a 6d 75 74 65 78 3b 0a 0a 20 20 2f 2a ex *mutex;.. /*
850d0 0a 20 20 2a 2a 20 48 65 61 64 20 61 6e 64 20 74 . ** Head and t
850e0 61 69 6c 20 6f 66 20 61 20 6c 69 6e 6b 65 64 20 ail of a linked
850f0 6c 69 73 74 20 6f 66 20 61 6c 6c 20 6f 75 74 73 list of all outs
85100 74 61 6e 64 69 6e 67 20 61 6c 6c 6f 63 61 74 69 tanding allocati
85110 6f 6e 73 0a 20 20 2a 2f 0a 20 20 73 74 72 75 63 ons. */. struc
85120 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 t MemBlockHdr *p
85130 46 69 72 73 74 3b 0a 20 20 73 74 72 75 63 74 20 First;. struct
85140 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 4c 61 MemBlockHdr *pLa
85150 73 74 3b 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a 2a st;. . /*. **
85160 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6c The number of l
85170 65 76 65 6c 73 20 6f 66 20 62 61 63 6b 74 72 61 evels of backtra
85180 63 65 20 74 6f 20 73 61 76 65 20 69 6e 20 6e 65 ce to save in ne
85190 77 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e 0a 20 w allocations..
851a0 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 61 63 6b 74 */. int nBackt
851b0 72 61 63 65 3b 0a 20 20 76 6f 69 64 20 28 2a 78 race;. void (*x
851c0 42 61 63 6b 74 72 61 63 65 29 28 69 6e 74 2c 20 Backtrace)(int,
851d0 69 6e 74 2c 20 76 6f 69 64 20 2a 2a 29 3b 0a 0a int, void **);..
851e0 20 20 2f 2a 0a 20 20 2a 2a 20 54 69 74 6c 65 20 /*. ** Title
851f0 74 65 78 74 20 74 6f 20 69 6e 73 65 72 74 20 69 text to insert i
85200 6e 20 66 72 6f 6e 74 20 6f 66 20 65 61 63 68 20 n front of each
85210 62 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 6e 74 block. */. int
85220 20 6e 54 69 74 6c 65 3b 20 20 20 20 20 20 20 20 nTitle;
85230 2f 2a 20 42 79 74 65 73 20 6f 66 20 7a 54 69 74 /* Bytes of zTit
85240 6c 65 20 74 6f 20 73 61 76 65 2e 20 20 49 6e 63 le to save. Inc
85250 6c 75 64 65 73 20 27 5c 30 27 20 61 6e 64 20 70 ludes '\0' and p
85260 61 64 64 69 6e 67 20 2a 2f 0a 20 20 63 68 61 72 adding */. char
85270 20 7a 54 69 74 6c 65 5b 31 30 30 5d 3b 20 20 2f zTitle[100]; /
85280 2a 20 54 68 65 20 74 69 74 6c 65 20 74 65 78 74 * The title text
85290 20 2a 2f 0a 0a 20 20 2f 2a 20 0a 20 20 2a 2a 20 */.. /* . **
852a0 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 44 69 73 sqlite3MallocDis
852b0 61 6c 6c 6f 77 28 29 20 69 6e 63 72 65 6d 65 6e allow() incremen
852c0 74 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 ts the following
852d0 20 63 6f 75 6e 74 65 72 2e 0a 20 20 2a 2a 20 73 counter.. ** s
852e0 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 6c 6c 6f qlite3MallocAllo
852f0 77 28 29 20 64 65 63 72 65 6d 65 6e 74 73 20 69 w() decrements i
85300 74 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 64 69 t.. */. int di
85310 73 61 6c 6c 6f 77 3b 20 2f 2a 20 44 6f 20 6e 6f sallow; /* Do no
85320 74 20 61 6c 6c 6f 77 20 6d 65 6d 6f 72 79 20 61 t allow memory a
85330 6c 6c 6f 63 61 74 69 6f 6e 20 2a 2f 0a 0a 20 20 llocation */..
85340 2f 2a 0a 20 20 2a 2a 20 47 61 74 68 65 72 20 73 /*. ** Gather s
85350 74 61 74 69 73 74 69 63 73 20 6f 6e 20 74 68 65 tatistics on the
85360 20 73 69 7a 65 73 20 6f 66 20 6d 65 6d 6f 72 79 sizes of memory
85370 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e 0a 20 20 allocations..
85380 2a 2a 20 6e 41 6c 6c 6f 63 5b 69 5d 20 69 73 20 ** nAlloc[i] is
85390 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 6c the number of al
853a0 6c 6f 63 61 74 69 6f 6e 20 61 74 74 65 6d 70 74 location attempt
853b0 73 20 6f 66 20 69 2a 38 0a 20 20 2a 2a 20 62 79 s of i*8. ** by
853c0 74 65 73 2e 20 20 69 3d 3d 4e 43 53 49 5a 45 20 tes. i==NCSIZE
853d0 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 is the number of
853e0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 74 74 65 allocation atte
853f0 6d 70 74 73 20 66 6f 72 0a 20 20 2a 2a 20 73 69 mpts for. ** si
85400 7a 65 73 20 6d 6f 72 65 20 74 68 61 6e 20 4e 43 zes more than NC
85410 53 49 5a 45 2a 38 20 62 79 74 65 73 2e 0a 20 20 SIZE*8 bytes..
85420 2a 2f 0a 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 5b */. int nAlloc[
85430 4e 43 53 49 5a 45 5d 3b 20 20 20 20 20 20 2f 2a NCSIZE]; /*
85440 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 Total number of
85450 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a allocations */.
85460 20 20 69 6e 74 20 6e 43 75 72 72 65 6e 74 5b 4e int nCurrent[N
85470 43 53 49 5a 45 5d 3b 20 20 20 20 2f 2a 20 43 75 CSIZE]; /* Cu
85480 72 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20 rrent number of
85490 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a 20 allocations */.
854a0 20 69 6e 74 20 6d 78 43 75 72 72 65 6e 74 5b 4e int mxCurrent[N
854b0 43 53 49 5a 45 5d 3b 20 20 20 2f 2a 20 48 69 67 CSIZE]; /* Hig
854c0 68 77 61 74 65 72 20 6d 61 72 6b 20 66 6f 72 20 hwater mark for
854d0 6e 43 75 72 72 65 6e 74 20 2a 2f 0a 0a 7d 20 6d nCurrent */..} m
854e0 65 6d 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 41 64 6a 75 em;.../*.** Adju
854f0 73 74 20 6d 65 6d 6f 72 79 20 75 73 61 67 65 20 st memory usage
85500 73 74 61 74 69 73 74 69 63 73 0a 2a 2f 0a 73 74 statistics.*/.st
85510 61 74 69 63 20 76 6f 69 64 20 61 64 6a 75 73 74 atic void adjust
85520 53 74 61 74 73 28 69 6e 74 20 69 53 69 7a 65 2c Stats(int iSize,
85530 20 69 6e 74 20 69 6e 63 72 65 6d 65 6e 74 29 7b int increment){
85540 0a 20 20 69 6e 74 20 69 20 3d 20 52 4f 55 4e 44 . int i = ROUND
85550 38 28 69 53 69 7a 65 29 2f 38 3b 0a 20 20 69 66 8(iSize)/8;. if
85560 28 20 69 3e 4e 43 53 49 5a 45 2d 31 20 29 7b 0a ( i>NCSIZE-1 ){.
85570 20 20 20 20 69 20 3d 20 4e 43 53 49 5a 45 20 2d i = NCSIZE -
85580 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 6e 1;. }. if( in
85590 63 72 65 6d 65 6e 74 3e 30 20 29 7b 0a 20 20 20 crement>0 ){.
855a0 20 6d 65 6d 2e 6e 41 6c 6c 6f 63 5b 69 5d 2b 2b mem.nAlloc[i]++
855b0 3b 0a 20 20 20 20 6d 65 6d 2e 6e 43 75 72 72 65 ;. mem.nCurre
855c0 6e 74 5b 69 5d 2b 2b 3b 0a 20 20 20 20 69 66 28 nt[i]++;. if(
855d0 20 6d 65 6d 2e 6e 43 75 72 72 65 6e 74 5b 69 5d mem.nCurrent[i]
855e0 3e 6d 65 6d 2e 6d 78 43 75 72 72 65 6e 74 5b 69 >mem.mxCurrent[i
855f0 5d 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 2e 6d ] ){. mem.m
85600 78 43 75 72 72 65 6e 74 5b 69 5d 20 3d 20 6d 65 xCurrent[i] = me
85610 6d 2e 6e 43 75 72 72 65 6e 74 5b 69 5d 3b 0a 20 m.nCurrent[i];.
85620 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 }. }else{.
85630 20 20 6d 65 6d 2e 6e 43 75 72 72 65 6e 74 5b 69 mem.nCurrent[i
85640 5d 2d 2d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 ]--;. assert(
85650 20 6d 65 6d 2e 6e 43 75 72 72 65 6e 74 5b 69 5d mem.nCurrent[i]
85660 3e 3d 30 20 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a >=0 );. }.}../*
85670 0a 2a 2a 20 47 69 76 65 6e 20 61 6e 20 61 6c 6c .** Given an all
85680 6f 63 61 74 69 6f 6e 2c 20 66 69 6e 64 20 74 68 ocation, find th
85690 65 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 66 6f e MemBlockHdr fo
856a0 72 20 74 68 61 74 20 61 6c 6c 6f 63 61 74 69 6f r that allocatio
856b0 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f n..**.** This ro
856c0 75 74 69 6e 65 20 63 68 65 63 6b 73 20 74 68 65 utine checks the
856d0 20 67 75 61 72 64 73 20 61 74 20 65 69 74 68 65 guards at eithe
856e0 72 20 65 6e 64 20 6f 66 20 74 68 65 20 61 6c 6c r end of the all
856f0 6f 63 61 74 69 6f 6e 20 61 6e 64 0a 2a 2a 20 69 ocation and.** i
85700 66 20 74 68 65 79 20 61 72 65 20 69 6e 63 6f 72 f they are incor
85710 72 65 63 74 20 69 74 20 61 73 73 65 72 74 73 2e rect it asserts.
85720 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75 63 .*/.static struc
85730 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 73 t MemBlockHdr *s
85740 71 6c 69 74 65 33 4d 65 6d 73 79 73 47 65 74 48 qlite3MemsysGetH
85750 65 61 64 65 72 28 76 6f 69 64 20 2a 70 41 6c 6c eader(void *pAll
85760 6f 63 61 74 69 6f 6e 29 7b 0a 20 20 73 74 72 75 ocation){. stru
85770 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a ct MemBlockHdr *
85780 70 3b 0a 20 20 69 6e 74 20 2a 70 49 6e 74 3b 0a p;. int *pInt;.
85790 20 20 75 38 20 2a 70 55 38 3b 0a 20 20 69 6e 74 u8 *pU8;. int
857a0 20 6e 52 65 73 65 72 76 65 3b 0a 0a 20 20 70 20 nReserve;.. p
857b0 3d 20 28 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f = (struct MemBlo
857c0 63 6b 48 64 72 2a 29 70 41 6c 6c 6f 63 61 74 69 ckHdr*)pAllocati
857d0 6f 6e 3b 0a 20 20 70 2d 2d 3b 0a 20 20 61 73 73 on;. p--;. ass
857e0 65 72 74 28 20 70 2d 3e 69 46 6f 72 65 47 75 61 ert( p->iForeGua
857f0 72 64 3d 3d 28 69 6e 74 29 46 4f 52 45 47 55 41 rd==(int)FOREGUA
85800 52 44 20 29 3b 0a 20 20 6e 52 65 73 65 72 76 65 RD );. nReserve
85810 20 3d 20 52 4f 55 4e 44 38 28 70 2d 3e 69 53 69 = ROUND8(p->iSi
85820 7a 65 29 3b 0a 20 20 70 49 6e 74 20 3d 20 28 69 ze);. pInt = (i
85830 6e 74 2a 29 70 41 6c 6c 6f 63 61 74 69 6f 6e 3b nt*)pAllocation;
85840 0a 20 20 70 55 38 20 3d 20 28 75 38 2a 29 70 41 . pU8 = (u8*)pA
85850 6c 6c 6f 63 61 74 69 6f 6e 3b 0a 20 20 61 73 73 llocation;. ass
85860 65 72 74 28 20 70 49 6e 74 5b 6e 52 65 73 65 72 ert( pInt[nReser
85870 76 65 2f 73 69 7a 65 6f 66 28 69 6e 74 29 5d 3d ve/sizeof(int)]=
85880 3d 28 69 6e 74 29 52 45 41 52 47 55 41 52 44 20 =(int)REARGUARD
85890 29 3b 0a 20 20 2f 2a 20 54 68 69 73 20 63 68 65 );. /* This che
858a0 63 6b 73 20 61 6e 79 20 6f 66 20 74 68 65 20 22 cks any of the "
858b0 65 78 74 72 61 22 20 62 79 74 65 73 20 61 6c 6c extra" bytes all
858c0 6f 63 61 74 65 64 20 64 75 65 0a 20 20 2a 2a 20 ocated due. **
858d0 74 6f 20 72 6f 75 6e 64 69 6e 67 20 75 70 20 74 to rounding up t
858e0 6f 20 61 6e 20 38 20 62 79 74 65 20 62 6f 75 6e o an 8 byte boun
858f0 64 61 72 79 20 74 6f 20 65 6e 73 75 72 65 20 0a dary to ensure .
85900 20 20 2a 2a 20 74 68 65 79 20 68 61 76 65 6e 27 ** they haven'
85910 74 20 62 65 65 6e 20 6f 76 65 72 77 72 69 74 74 t been overwritt
85920 65 6e 2e 0a 20 20 2a 2f 0a 20 20 77 68 69 6c 65 en.. */. while
85930 28 20 6e 52 65 73 65 72 76 65 2d 2d 20 3e 20 70 ( nReserve-- > p
85940 2d 3e 69 53 69 7a 65 20 29 20 61 73 73 65 72 74 ->iSize ) assert
85950 28 20 70 55 38 5b 6e 52 65 73 65 72 76 65 5d 3d ( pU8[nReserve]=
85960 3d 30 78 36 35 20 29 3b 0a 20 20 72 65 74 75 72 =0x65 );. retur
85970 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 n p;.}../*.** Re
85980 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 turn the number
85990 6f 66 20 62 79 74 65 73 20 63 75 72 72 65 6e 74 of bytes current
859a0 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 61 74 20 ly allocated at
859b0 61 64 64 72 65 73 73 20 70 2e 0a 2a 2f 0a 73 74 address p..*/.st
859c0 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 atic int sqlite3
859d0 4d 65 6d 53 69 7a 65 28 76 6f 69 64 20 2a 70 29 MemSize(void *p)
859e0 7b 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c {. struct MemBl
859f0 6f 63 6b 48 64 72 20 2a 70 48 64 72 3b 0a 20 20 ockHdr *pHdr;.
85a00 69 66 28 20 21 70 20 29 7b 0a 20 20 20 20 72 65 if( !p ){. re
85a10 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 48 turn 0;. }. pH
85a20 64 72 20 3d 20 73 71 6c 69 74 65 33 4d 65 6d 73 dr = sqlite3Mems
85a30 79 73 47 65 74 48 65 61 64 65 72 28 70 29 3b 0a ysGetHeader(p);.
85a40 20 20 72 65 74 75 72 6e 20 70 48 64 72 2d 3e 69 return pHdr->i
85a50 53 69 7a 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 Size;.}../*.** I
85a60 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 65 nitialize the me
85a70 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
85a80 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 73 74 subsystem..*/.st
85a90 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 atic int sqlite3
85aa0 4d 65 6d 49 6e 69 74 28 76 6f 69 64 20 2a 4e 6f MemInit(void *No
85ab0 74 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 tUsed){. UNUSED
85ac0 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 _PARAMETER(NotUs
85ad0 65 64 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 ed);. assert( (
85ae0 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20 4d 65 sizeof(struct Me
85af0 6d 42 6c 6f 63 6b 48 64 72 29 26 37 29 20 3d 3d mBlockHdr)&7) ==
85b00 20 30 20 29 3b 0a 20 20 69 66 28 20 21 73 71 6c 0 );. if( !sql
85b10 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
85b20 2e 62 4d 65 6d 73 74 61 74 20 29 7b 0a 20 20 20 .bMemstat ){.
85b30 20 2f 2a 20 49 66 20 6d 65 6d 6f 72 79 20 73 74 /* If memory st
85b40 61 74 75 73 20 69 73 20 65 6e 61 62 6c 65 64 2c atus is enabled,
85b50 20 74 68 65 6e 20 74 68 65 20 6d 61 6c 6c 6f 63 then the malloc
85b60 2e 63 20 77 72 61 70 70 65 72 20 77 69 6c 6c 20 .c wrapper will
85b70 61 6c 72 65 61 64 79 0a 20 20 20 20 2a 2a 20 68 already. ** h
85b80 6f 6c 64 20 74 68 65 20 53 54 41 54 49 43 5f 4d old the STATIC_M
85b90 45 4d 20 6d 75 74 65 78 20 77 68 65 6e 20 74 68 EM mutex when th
85ba0 65 20 72 6f 75 74 69 6e 65 73 20 68 65 72 65 20 e routines here
85bb0 61 72 65 20 69 6e 76 6f 6b 65 64 2e 20 2a 2f 0a are invoked. */.
85bc0 20 20 20 20 6d 65 6d 2e 6d 75 74 65 78 20 3d 20 mem.mutex =
85bd0 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f sqlite3MutexAllo
85be0 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 c(SQLITE_MUTEX_S
85bf0 54 41 54 49 43 5f 4d 45 4d 29 3b 0a 20 20 7d 0a TATIC_MEM);. }.
85c00 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
85c10 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 OK;.}../*.** Dei
85c20 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 65 nitialize the me
85c30 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
85c40 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 73 74 subsystem..*/.st
85c50 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 atic void sqlite
85c60 33 4d 65 6d 53 68 75 74 64 6f 77 6e 28 76 6f 69 3MemShutdown(voi
85c70 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55 d *NotUsed){. U
85c80 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
85c90 4e 6f 74 55 73 65 64 29 3b 0a 20 20 6d 65 6d 2e NotUsed);. mem.
85ca0 6d 75 74 65 78 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a mutex = 0;.}../*
85cb0 0a 2a 2a 20 52 6f 75 6e 64 20 75 70 20 61 20 72 .** Round up a r
85cc0 65 71 75 65 73 74 20 73 69 7a 65 20 74 6f 20 74 equest size to t
85cd0 68 65 20 6e 65 78 74 20 76 61 6c 69 64 20 61 6c he next valid al
85ce0 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65 2e 0a 2a location size..*
85cf0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c /.static int sql
85d00 69 74 65 33 4d 65 6d 52 6f 75 6e 64 75 70 28 69 ite3MemRoundup(i
85d10 6e 74 20 6e 29 7b 0a 20 20 72 65 74 75 72 6e 20 nt n){. return
85d20 52 4f 55 4e 44 38 28 6e 29 3b 0a 7d 0a 0a 2f 2a ROUND8(n);.}../*
85d30 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6e 42 79 .** Allocate nBy
85d40 74 65 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f te bytes of memo
85d50 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ry..*/.static vo
85d60 69 64 20 2a 73 71 6c 69 74 65 33 4d 65 6d 4d 61 id *sqlite3MemMa
85d70 6c 6c 6f 63 28 69 6e 74 20 6e 42 79 74 65 29 7b lloc(int nByte){
85d80 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f . struct MemBlo
85d90 63 6b 48 64 72 20 2a 70 48 64 72 3b 0a 20 20 76 ckHdr *pHdr;. v
85da0 6f 69 64 20 2a 2a 70 42 74 3b 0a 20 20 63 68 61 oid **pBt;. cha
85db0 72 20 2a 7a 3b 0a 20 20 69 6e 74 20 2a 70 49 6e r *z;. int *pIn
85dc0 74 3b 0a 20 20 76 6f 69 64 20 2a 70 20 3d 20 30 t;. void *p = 0
85dd0 3b 0a 20 20 69 6e 74 20 74 6f 74 61 6c 53 69 7a ;. int totalSiz
85de0 65 3b 0a 20 20 69 6e 74 20 6e 52 65 73 65 72 76 e;. int nReserv
85df0 65 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 e;. sqlite3_mut
85e00 65 78 5f 65 6e 74 65 72 28 6d 65 6d 2e 6d 75 74 ex_enter(mem.mut
85e10 65 78 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6d ex);. assert( m
85e20 65 6d 2e 64 69 73 61 6c 6c 6f 77 3d 3d 30 20 29 em.disallow==0 )
85e30 3b 0a 20 20 6e 52 65 73 65 72 76 65 20 3d 20 52 ;. nReserve = R
85e40 4f 55 4e 44 38 28 6e 42 79 74 65 29 3b 0a 20 20 OUND8(nByte);.
85e50 74 6f 74 61 6c 53 69 7a 65 20 3d 20 6e 52 65 73 totalSize = nRes
85e60 65 72 76 65 20 2b 20 73 69 7a 65 6f 66 28 2a 70 erve + sizeof(*p
85e70 48 64 72 29 20 2b 20 73 69 7a 65 6f 66 28 69 6e Hdr) + sizeof(in
85e80 74 29 20 2b 0a 20 20 20 20 20 20 20 20 20 20 20 t) +.
85e90 20 20 20 20 6d 65 6d 2e 6e 42 61 63 6b 74 72 61 mem.nBacktra
85ea0 63 65 2a 73 69 7a 65 6f 66 28 76 6f 69 64 2a 29 ce*sizeof(void*)
85eb0 20 2b 20 6d 65 6d 2e 6e 54 69 74 6c 65 3b 0a 20 + mem.nTitle;.
85ec0 20 70 20 3d 20 6d 61 6c 6c 6f 63 28 74 6f 74 61 p = malloc(tota
85ed0 6c 53 69 7a 65 29 3b 0a 20 20 69 66 28 20 70 20 lSize);. if( p
85ee0 29 7b 0a 20 20 20 20 7a 20 3d 20 70 3b 0a 20 20 ){. z = p;.
85ef0 20 20 70 42 74 20 3d 20 28 76 6f 69 64 2a 2a 29 pBt = (void**)
85f00 26 7a 5b 6d 65 6d 2e 6e 54 69 74 6c 65 5d 3b 0a &z[mem.nTitle];.
85f10 20 20 20 20 70 48 64 72 20 3d 20 28 73 74 72 75 pHdr = (stru
85f20 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 2a 29 ct MemBlockHdr*)
85f30 26 70 42 74 5b 6d 65 6d 2e 6e 42 61 63 6b 74 72 &pBt[mem.nBacktr
85f40 61 63 65 5d 3b 0a 20 20 20 20 70 48 64 72 2d 3e ace];. pHdr->
85f50 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 70 pNext = 0;. p
85f60 48 64 72 2d 3e 70 50 72 65 76 20 3d 20 6d 65 6d Hdr->pPrev = mem
85f70 2e 70 4c 61 73 74 3b 0a 20 20 20 20 69 66 28 20 .pLast;. if(
85f80 6d 65 6d 2e 70 4c 61 73 74 20 29 7b 0a 20 20 20 mem.pLast ){.
85f90 20 20 20 6d 65 6d 2e 70 4c 61 73 74 2d 3e 70 4e mem.pLast->pN
85fa0 65 78 74 20 3d 20 70 48 64 72 3b 0a 20 20 20 20 ext = pHdr;.
85fb0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d }else{. mem
85fc0 2e 70 46 69 72 73 74 20 3d 20 70 48 64 72 3b 0a .pFirst = pHdr;.
85fd0 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 2e 70 4c }. mem.pL
85fe0 61 73 74 20 3d 20 70 48 64 72 3b 0a 20 20 20 20 ast = pHdr;.
85ff0 70 48 64 72 2d 3e 69 46 6f 72 65 47 75 61 72 64 pHdr->iForeGuard
86000 20 3d 20 46 4f 52 45 47 55 41 52 44 3b 0a 20 20 = FOREGUARD;.
86010 20 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 pHdr->nBacktra
86020 63 65 53 6c 6f 74 73 20 3d 20 6d 65 6d 2e 6e 42 ceSlots = mem.nB
86030 61 63 6b 74 72 61 63 65 3b 0a 20 20 20 20 70 48 acktrace;. pH
86040 64 72 2d 3e 6e 54 69 74 6c 65 20 3d 20 6d 65 6d dr->nTitle = mem
86050 2e 6e 54 69 74 6c 65 3b 0a 20 20 20 20 69 66 28 .nTitle;. if(
86060 20 6d 65 6d 2e 6e 42 61 63 6b 74 72 61 63 65 20 mem.nBacktrace
86070 29 7b 0a 20 20 20 20 20 20 76 6f 69 64 20 2a 61 ){. void *a
86080 41 64 64 72 5b 34 30 5d 3b 0a 20 20 20 20 20 20 Addr[40];.
86090 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 pHdr->nBacktrace
860a0 20 3d 20 62 61 63 6b 74 72 61 63 65 28 61 41 64 = backtrace(aAd
860b0 64 72 2c 20 6d 65 6d 2e 6e 42 61 63 6b 74 72 61 dr, mem.nBacktra
860c0 63 65 2b 31 29 2d 31 3b 0a 20 20 20 20 20 20 6d ce+1)-1;. m
860d0 65 6d 63 70 79 28 70 42 74 2c 20 26 61 41 64 64 emcpy(pBt, &aAdd
860e0 72 5b 31 5d 2c 20 70 48 64 72 2d 3e 6e 42 61 63 r[1], pHdr->nBac
860f0 6b 74 72 61 63 65 2a 73 69 7a 65 6f 66 28 76 6f ktrace*sizeof(vo
86100 69 64 2a 29 29 3b 0a 20 20 20 20 20 20 61 73 73 id*));. ass
86110 65 72 74 28 70 42 74 5b 30 5d 29 3b 0a 20 20 20 ert(pBt[0]);.
86120 20 20 20 69 66 28 20 6d 65 6d 2e 78 42 61 63 6b if( mem.xBack
86130 74 72 61 63 65 20 29 7b 0a 20 20 20 20 20 20 20 trace ){.
86140 20 6d 65 6d 2e 78 42 61 63 6b 74 72 61 63 65 28 mem.xBacktrace(
86150 6e 42 79 74 65 2c 20 70 48 64 72 2d 3e 6e 42 61 nByte, pHdr->nBa
86160 63 6b 74 72 61 63 65 2d 31 2c 20 26 61 41 64 64 cktrace-1, &aAdd
86170 72 5b 31 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 r[1]);. }.
86180 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
86190 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 pHdr->nBacktrace
861a0 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 = 0;. }.
861b0 69 66 28 20 6d 65 6d 2e 6e 54 69 74 6c 65 20 29 if( mem.nTitle )
861c0 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 7a {. memcpy(z
861d0 2c 20 6d 65 6d 2e 7a 54 69 74 6c 65 2c 20 6d 65 , mem.zTitle, me
861e0 6d 2e 6e 54 69 74 6c 65 29 3b 0a 20 20 20 20 7d m.nTitle);. }
861f0 0a 20 20 20 20 70 48 64 72 2d 3e 69 53 69 7a 65 . pHdr->iSize
86200 20 3d 20 6e 42 79 74 65 3b 0a 20 20 20 20 61 64 = nByte;. ad
86210 6a 75 73 74 53 74 61 74 73 28 6e 42 79 74 65 2c justStats(nByte,
86220 20 2b 31 29 3b 0a 20 20 20 20 70 49 6e 74 20 3d +1);. pInt =
86230 20 28 69 6e 74 2a 29 26 70 48 64 72 5b 31 5d 3b (int*)&pHdr[1];
86240 0a 20 20 20 20 70 49 6e 74 5b 6e 52 65 73 65 72 . pInt[nReser
86250 76 65 2f 73 69 7a 65 6f 66 28 69 6e 74 29 5d 20 ve/sizeof(int)]
86260 3d 20 52 45 41 52 47 55 41 52 44 3b 0a 20 20 20 = REARGUARD;.
86270 20 6d 65 6d 73 65 74 28 70 49 6e 74 2c 20 30 78 memset(pInt, 0x
86280 36 35 2c 20 6e 52 65 73 65 72 76 65 29 3b 0a 20 65, nReserve);.
86290 20 20 20 70 20 3d 20 28 76 6f 69 64 2a 29 70 49 p = (void*)pI
862a0 6e 74 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 nt;. }. sqlite
862b0 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 3_mutex_leave(me
862c0 6d 2e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 m.mutex);. retu
862d0 72 6e 20 70 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn p; .}../*.**
862e0 46 72 65 65 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a Free memory..*/.
862f0 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 static void sqli
86300 74 65 33 4d 65 6d 46 72 65 65 28 76 6f 69 64 20 te3MemFree(void
86310 2a 70 50 72 69 6f 72 29 7b 0a 20 20 73 74 72 75 *pPrior){. stru
86320 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a ct MemBlockHdr *
86330 70 48 64 72 3b 0a 20 20 76 6f 69 64 20 2a 2a 70 pHdr;. void **p
86340 42 74 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 Bt;. char *z;.
86350 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
86360 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 GlobalConfig.bMe
86370 6d 73 74 61 74 20 7c 7c 20 6d 65 6d 2e 6d 75 74 mstat || mem.mut
86380 65 78 21 3d 30 20 29 3b 0a 20 20 70 48 64 72 20 ex!=0 );. pHdr
86390 3d 20 73 71 6c 69 74 65 33 4d 65 6d 73 79 73 47 = sqlite3MemsysG
863a0 65 74 48 65 61 64 65 72 28 70 50 72 69 6f 72 29 etHeader(pPrior)
863b0 3b 0a 20 20 70 42 74 20 3d 20 28 76 6f 69 64 2a ;. pBt = (void*
863c0 2a 29 70 48 64 72 3b 0a 20 20 70 42 74 20 2d 3d *)pHdr;. pBt -=
863d0 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 pHdr->nBacktrac
863e0 65 53 6c 6f 74 73 3b 0a 20 20 73 71 6c 69 74 65 eSlots;. sqlite
863f0 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 3_mutex_enter(me
86400 6d 2e 6d 75 74 65 78 29 3b 0a 20 20 69 66 28 20 m.mutex);. if(
86410 70 48 64 72 2d 3e 70 50 72 65 76 20 29 7b 0a 20 pHdr->pPrev ){.
86420 20 20 20 61 73 73 65 72 74 28 20 70 48 64 72 2d assert( pHdr-
86430 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 3d 3d 70 >pPrev->pNext==p
86440 48 64 72 20 29 3b 0a 20 20 20 20 70 48 64 72 2d Hdr );. pHdr-
86450 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 20 3d 20 >pPrev->pNext =
86460 70 48 64 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d pHdr->pNext;. }
86470 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 else{. assert
86480 28 20 6d 65 6d 2e 70 46 69 72 73 74 3d 3d 70 48 ( mem.pFirst==pH
86490 64 72 20 29 3b 0a 20 20 20 20 6d 65 6d 2e 70 46 dr );. mem.pF
864a0 69 72 73 74 20 3d 20 70 48 64 72 2d 3e 70 4e 65 irst = pHdr->pNe
864b0 78 74 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 48 xt;. }. if( pH
864c0 64 72 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20 dr->pNext ){.
864d0 20 61 73 73 65 72 74 28 20 70 48 64 72 2d 3e 70 assert( pHdr->p
864e0 4e 65 78 74 2d 3e 70 50 72 65 76 3d 3d 70 48 64 Next->pPrev==pHd
864f0 72 20 29 3b 0a 20 20 20 20 70 48 64 72 2d 3e 70 r );. pHdr->p
86500 4e 65 78 74 2d 3e 70 50 72 65 76 20 3d 20 70 48 Next->pPrev = pH
86510 64 72 2d 3e 70 50 72 65 76 3b 0a 20 20 7d 65 6c dr->pPrev;. }el
86520 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 se{. assert(
86530 6d 65 6d 2e 70 4c 61 73 74 3d 3d 70 48 64 72 20 mem.pLast==pHdr
86540 29 3b 0a 20 20 20 20 6d 65 6d 2e 70 4c 61 73 74 );. mem.pLast
86550 20 3d 20 70 48 64 72 2d 3e 70 50 72 65 76 3b 0a = pHdr->pPrev;.
86560 20 20 7d 0a 20 20 7a 20 3d 20 28 63 68 61 72 2a }. z = (char*
86570 29 70 42 74 3b 0a 20 20 7a 20 2d 3d 20 70 48 64 )pBt;. z -= pHd
86580 72 2d 3e 6e 54 69 74 6c 65 3b 0a 20 20 61 64 6a r->nTitle;. adj
86590 75 73 74 53 74 61 74 73 28 70 48 64 72 2d 3e 69 ustStats(pHdr->i
865a0 53 69 7a 65 2c 20 2d 31 29 3b 0a 20 20 6d 65 6d Size, -1);. mem
865b0 73 65 74 28 7a 2c 20 30 78 32 62 2c 20 73 69 7a set(z, 0x2b, siz
865c0 65 6f 66 28 76 6f 69 64 2a 29 2a 70 48 64 72 2d eof(void*)*pHdr-
865d0 3e 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f 74 73 >nBacktraceSlots
865e0 20 2b 20 73 69 7a 65 6f 66 28 2a 70 48 64 72 29 + sizeof(*pHdr)
865f0 20 2b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 +.
86600 20 20 20 20 20 70 48 64 72 2d 3e 69 53 69 7a 65 pHdr->iSize
86610 20 2b 20 73 69 7a 65 6f 66 28 69 6e 74 29 20 2b + sizeof(int) +
86620 20 70 48 64 72 2d 3e 6e 54 69 74 6c 65 29 3b 0a pHdr->nTitle);.
86630 20 20 66 72 65 65 28 7a 29 3b 0a 20 20 73 71 6c free(z);. sql
86640 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 ite3_mutex_leave
86650 28 6d 65 6d 2e 6d 75 74 65 78 29 3b 20 20 0a 7d (mem.mutex); .}
86660 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 ../*.** Change t
86670 68 65 20 73 69 7a 65 20 6f 66 20 61 6e 20 65 78 he size of an ex
86680 69 73 74 69 6e 67 20 6d 65 6d 6f 72 79 20 61 6c isting memory al
86690 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 location..**.**
866a0 46 6f 72 20 74 68 69 73 20 64 65 62 75 67 67 69 For this debuggi
866b0 6e 67 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ng implementatio
866c0 6e 2c 20 77 65 20 2a 61 6c 77 61 79 73 2a 20 6d n, we *always* m
866d0 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68 ake a copy of th
866e0 65 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 e.** allocation
866f0 69 6e 74 6f 20 61 20 6e 65 77 20 70 6c 61 63 65 into a new place
86700 20 69 6e 20 6d 65 6d 6f 72 79 2e 20 20 49 6e 20 in memory. In
86710 74 68 69 73 20 77 61 79 2c 20 69 66 20 74 68 65 this way, if the
86720 20 0a 2a 2a 20 68 69 67 68 65 72 20 6c 65 76 65 .** higher leve
86730 6c 20 63 6f 64 65 20 69 73 20 75 73 69 6e 67 20 l code is using
86740 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6f pointer to the o
86750 6c 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 69 ld allocation, i
86760 74 20 69 73 20 0a 2a 2a 20 6d 75 63 68 20 6d 6f t is .** much mo
86770 72 65 20 6c 69 6b 65 6c 79 20 74 6f 20 62 72 65 re likely to bre
86780 61 6b 20 61 6e 64 20 77 65 20 61 72 65 20 6d 75 ak and we are mu
86790 63 68 20 6d 6f 72 65 20 6c 69 6b 69 6e 67 20 74 ch more liking t
867a0 6f 20 66 69 6e 64 0a 2a 2a 20 74 68 65 20 65 72 o find.** the er
867b0 72 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ror..*/.static v
867c0 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d 65 6d 52 oid *sqlite3MemR
867d0 65 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 50 72 ealloc(void *pPr
867e0 69 6f 72 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b ior, int nByte){
867f0 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f . struct MemBlo
86800 63 6b 48 64 72 20 2a 70 4f 6c 64 48 64 72 3b 0a ckHdr *pOldHdr;.
86810 20 20 76 6f 69 64 20 2a 70 4e 65 77 3b 0a 20 20 void *pNew;.
86820 61 73 73 65 72 74 28 20 6d 65 6d 2e 64 69 73 61 assert( mem.disa
86830 6c 6c 6f 77 3d 3d 30 20 29 3b 0a 20 20 70 4f 6c llow==0 );. pOl
86840 64 48 64 72 20 3d 20 73 71 6c 69 74 65 33 4d 65 dHdr = sqlite3Me
86850 6d 73 79 73 47 65 74 48 65 61 64 65 72 28 70 50 msysGetHeader(pP
86860 72 69 6f 72 29 3b 0a 20 20 70 4e 65 77 20 3d 20 rior);. pNew =
86870 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 sqlite3MemMalloc
86880 28 6e 42 79 74 65 29 3b 0a 20 20 69 66 28 20 70 (nByte);. if( p
86890 4e 65 77 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70 New ){. memcp
868a0 79 28 70 4e 65 77 2c 20 70 50 72 69 6f 72 2c 20 y(pNew, pPrior,
868b0 6e 42 79 74 65 3c 70 4f 6c 64 48 64 72 2d 3e 69 nByte<pOldHdr->i
868c0 53 69 7a 65 20 3f 20 6e 42 79 74 65 20 3a 20 70 Size ? nByte : p
868d0 4f 6c 64 48 64 72 2d 3e 69 53 69 7a 65 29 3b 0a OldHdr->iSize);.
868e0 20 20 20 20 69 66 28 20 6e 42 79 74 65 3e 70 4f if( nByte>pO
868f0 6c 64 48 64 72 2d 3e 69 53 69 7a 65 20 29 7b 0a ldHdr->iSize ){.
86900 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26 28 28 memset(&((
86910 63 68 61 72 2a 29 70 4e 65 77 29 5b 70 4f 6c 64 char*)pNew)[pOld
86920 48 64 72 2d 3e 69 53 69 7a 65 5d 2c 20 30 78 32 Hdr->iSize], 0x2
86930 62 2c 20 6e 42 79 74 65 20 2d 20 70 4f 6c 64 48 b, nByte - pOldH
86940 64 72 2d 3e 69 53 69 7a 65 29 3b 0a 20 20 20 20 dr->iSize);.
86950 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d }. sqlite3Mem
86960 46 72 65 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 Free(pPrior);.
86970 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b }. return pNew;
86980 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 6f 70 75 6c 61 .}../*.** Popula
86990 74 65 20 74 68 65 20 6c 6f 77 2d 6c 65 76 65 6c te the low-level
869a0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
869b0 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e on function poin
869c0 74 65 72 73 20 69 6e 0a 2a 2a 20 73 71 6c 69 74 ters in.** sqlit
869d0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d e3GlobalConfig.m
869e0 20 77 69 74 68 20 70 6f 69 6e 74 65 72 73 20 74 with pointers t
869f0 6f 20 74 68 65 20 72 6f 75 74 69 6e 65 73 20 69 o the routines i
86a00 6e 20 74 68 69 73 20 66 69 6c 65 2e 0a 2a 2f 0a n this file..*/.
86a10 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
86a20 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 53 65 oid sqlite3MemSe
86a30 74 44 65 66 61 75 6c 74 28 76 6f 69 64 29 7b 0a tDefault(void){.
86a40 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 static const s
86a50 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f qlite3_mem_metho
86a60 64 73 20 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 ds defaultMethod
86a70 73 20 3d 20 7b 0a 20 20 20 20 20 73 71 6c 69 74 s = {. sqlit
86a80 65 33 4d 65 6d 4d 61 6c 6c 6f 63 2c 0a 20 20 20 e3MemMalloc,.
86a90 20 20 73 71 6c 69 74 65 33 4d 65 6d 46 72 65 65 sqlite3MemFree
86aa0 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 ,. sqlite3Me
86ab0 6d 52 65 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 73 mRealloc,. s
86ac0 71 6c 69 74 65 33 4d 65 6d 53 69 7a 65 2c 0a 20 qlite3MemSize,.
86ad0 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 52 6f sqlite3MemRo
86ae0 75 6e 64 75 70 2c 0a 20 20 20 20 20 73 71 6c 69 undup,. sqli
86af0 74 65 33 4d 65 6d 49 6e 69 74 2c 0a 20 20 20 20 te3MemInit,.
86b00 20 73 71 6c 69 74 65 33 4d 65 6d 53 68 75 74 64 sqlite3MemShutd
86b10 6f 77 6e 2c 0a 20 20 20 20 20 30 0a 20 20 7d 3b own,. 0. };
86b20 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 . sqlite3_confi
86b30 67 28 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f g(SQLITE_CONFIG_
86b40 4d 41 4c 4c 4f 43 2c 20 26 64 65 66 61 75 6c 74 MALLOC, &default
86b50 4d 65 74 68 6f 64 73 29 3b 0a 7d 0a 0a 2f 2a 0a Methods);.}../*.
86b60 2a 2a 20 53 65 74 20 74 68 65 20 6e 75 6d 62 65 ** Set the numbe
86b70 72 20 6f 66 20 62 61 63 6b 74 72 61 63 65 20 6c r of backtrace l
86b80 65 76 65 6c 73 20 6b 65 70 74 20 66 6f 72 20 65 evels kept for e
86b90 61 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a ach allocation..
86ba0 2a 2a 20 41 20 76 61 6c 75 65 20 6f 66 20 7a 65 ** A value of ze
86bb0 72 6f 20 74 75 72 6e 73 20 6f 66 66 20 62 61 63 ro turns off bac
86bc0 6b 74 72 61 63 69 6e 67 2e 20 20 54 68 65 20 6e ktracing. The n
86bd0 75 6d 62 65 72 20 69 73 20 61 6c 77 61 79 73 20 umber is always
86be0 72 6f 75 6e 64 65 64 0a 2a 2a 20 75 70 20 74 6f rounded.** up to
86bf0 20 61 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20 32 a multiple of 2
86c00 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
86c10 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
86c20 4d 65 6d 64 65 62 75 67 42 61 63 6b 74 72 61 63 MemdebugBacktrac
86c30 65 28 69 6e 74 20 64 65 70 74 68 29 7b 0a 20 20 e(int depth){.
86c40 69 66 28 20 64 65 70 74 68 3c 30 20 29 7b 20 64 if( depth<0 ){ d
86c50 65 70 74 68 20 3d 20 30 3b 20 7d 0a 20 20 69 66 epth = 0; }. if
86c60 28 20 64 65 70 74 68 3e 32 30 20 29 7b 20 64 65 ( depth>20 ){ de
86c70 70 74 68 20 3d 20 32 30 3b 20 7d 0a 20 20 64 65 pth = 20; }. de
86c80 70 74 68 20 3d 20 28 64 65 70 74 68 2b 31 29 26 pth = (depth+1)&
86c90 30 78 66 65 3b 0a 20 20 6d 65 6d 2e 6e 42 61 63 0xfe;. mem.nBac
86ca0 6b 74 72 61 63 65 20 3d 20 64 65 70 74 68 3b 0a ktrace = depth;.
86cb0 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 }..SQLITE_PRIVAT
86cc0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 E void sqlite3Me
86cd0 6d 64 65 62 75 67 42 61 63 6b 74 72 61 63 65 43 mdebugBacktraceC
86ce0 61 6c 6c 62 61 63 6b 28 76 6f 69 64 20 28 2a 78 allback(void (*x
86cf0 42 61 63 6b 74 72 61 63 65 29 28 69 6e 74 2c 20 Backtrace)(int,
86d00 69 6e 74 2c 20 76 6f 69 64 20 2a 2a 29 29 7b 0a int, void **)){.
86d10 20 20 6d 65 6d 2e 78 42 61 63 6b 74 72 61 63 65 mem.xBacktrace
86d20 20 3d 20 78 42 61 63 6b 74 72 61 63 65 3b 0a 7d = xBacktrace;.}
86d30 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 ../*.** Set the
86d40 74 69 74 6c 65 20 73 74 72 69 6e 67 20 66 6f 72 title string for
86d50 20 73 75 62 73 65 71 75 65 6e 74 20 61 6c 6c 6f subsequent allo
86d60 63 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 cations..*/.SQLI
86d70 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
86d80 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 53 sqlite3MemdebugS
86d90 65 74 74 69 74 6c 65 28 63 6f 6e 73 74 20 63 68 ettitle(const ch
86da0 61 72 20 2a 7a 54 69 74 6c 65 29 7b 0a 20 20 75 ar *zTitle){. u
86db0 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e 20 3d 20 nsigned int n =
86dc0 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 sqlite3Strlen30(
86dd0 7a 54 69 74 6c 65 29 20 2b 20 31 3b 0a 20 20 73 zTitle) + 1;. s
86de0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 qlite3_mutex_ent
86df0 65 72 28 6d 65 6d 2e 6d 75 74 65 78 29 3b 0a 20 er(mem.mutex);.
86e00 20 69 66 28 20 6e 3e 3d 73 69 7a 65 6f 66 28 6d if( n>=sizeof(m
86e10 65 6d 2e 7a 54 69 74 6c 65 29 20 29 20 6e 20 3d em.zTitle) ) n =
86e20 20 73 69 7a 65 6f 66 28 6d 65 6d 2e 7a 54 69 74 sizeof(mem.zTit
86e30 6c 65 29 2d 31 3b 0a 20 20 6d 65 6d 63 70 79 28 le)-1;. memcpy(
86e40 6d 65 6d 2e 7a 54 69 74 6c 65 2c 20 7a 54 69 74 mem.zTitle, zTit
86e50 6c 65 2c 20 6e 29 3b 0a 20 20 6d 65 6d 2e 7a 54 le, n);. mem.zT
86e60 69 74 6c 65 5b 6e 5d 20 3d 20 30 3b 0a 20 20 6d itle[n] = 0;. m
86e70 65 6d 2e 6e 54 69 74 6c 65 20 3d 20 52 4f 55 4e em.nTitle = ROUN
86e80 44 38 28 6e 29 3b 0a 20 20 73 71 6c 69 74 65 33 D8(n);. sqlite3
86e90 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d _mutex_leave(mem
86ea0 2e 6d 75 74 65 78 29 3b 0a 7d 0a 0a 53 51 4c 49 .mutex);.}..SQLI
86eb0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
86ec0 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 53 sqlite3MemdebugS
86ed0 79 6e 63 28 29 7b 0a 20 20 73 74 72 75 63 74 20 ync(){. struct
86ee0 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 48 64 MemBlockHdr *pHd
86ef0 72 3b 0a 20 20 66 6f 72 28 70 48 64 72 3d 6d 65 r;. for(pHdr=me
86f00 6d 2e 70 46 69 72 73 74 3b 20 70 48 64 72 3b 20 m.pFirst; pHdr;
86f10 70 48 64 72 3d 70 48 64 72 2d 3e 70 4e 65 78 74 pHdr=pHdr->pNext
86f20 29 7b 0a 20 20 20 20 76 6f 69 64 20 2a 2a 70 42 ){. void **pB
86f30 74 20 3d 20 28 76 6f 69 64 2a 2a 29 70 48 64 72 t = (void**)pHdr
86f40 3b 0a 20 20 20 20 70 42 74 20 2d 3d 20 70 48 64 ;. pBt -= pHd
86f50 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f r->nBacktraceSlo
86f60 74 73 3b 0a 20 20 20 20 6d 65 6d 2e 78 42 61 63 ts;. mem.xBac
86f70 6b 74 72 61 63 65 28 70 48 64 72 2d 3e 69 53 69 ktrace(pHdr->iSi
86f80 7a 65 2c 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 ze, pHdr->nBackt
86f90 72 61 63 65 2d 31 2c 20 26 70 42 74 5b 31 5d 29 race-1, &pBt[1])
86fa0 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f ;. }.}../*.** O
86fb0 70 65 6e 20 74 68 65 20 66 69 6c 65 20 69 6e 64 pen the file ind
86fc0 69 63 61 74 65 64 20 61 6e 64 20 77 72 69 74 65 icated and write
86fd0 20 61 20 6c 6f 67 20 6f 66 20 61 6c 6c 20 75 6e a log of all un
86fe0 66 72 65 65 64 20 6d 65 6d 6f 72 79 20 0a 2a 2a freed memory .**
86ff0 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 69 6e 74 allocations int
87000 6f 20 74 68 61 74 20 6c 6f 67 2e 0a 2a 2f 0a 53 o that log..*/.S
87010 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
87020 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 id sqlite3Memdeb
87030 75 67 44 75 6d 70 28 63 6f 6e 73 74 20 63 68 61 ugDump(const cha
87040 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 r *zFilename){.
87050 20 46 49 4c 45 20 2a 6f 75 74 3b 0a 20 20 73 74 FILE *out;. st
87060 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 ruct MemBlockHdr
87070 20 2a 70 48 64 72 3b 0a 20 20 76 6f 69 64 20 2a *pHdr;. void *
87080 2a 70 42 74 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 *pBt;. int i;.
87090 20 6f 75 74 20 3d 20 66 6f 70 65 6e 28 7a 46 69 out = fopen(zFi
870a0 6c 65 6e 61 6d 65 2c 20 22 77 22 29 3b 0a 20 20 lename, "w");.
870b0 69 66 28 20 6f 75 74 3d 3d 30 20 29 7b 0a 20 20 if( out==0 ){.
870c0 20 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 fprintf(stderr
870d0 2c 20 22 2a 2a 20 55 6e 61 62 6c 65 20 74 6f 20 , "** Unable to
870e0 6f 75 74 70 75 74 20 6d 65 6d 6f 72 79 20 64 65 output memory de
870f0 62 75 67 20 6f 75 74 70 75 74 20 6c 6f 67 3a 20 bug output log:
87100 25 73 20 2a 2a 5c 6e 22 2c 0a 20 20 20 20 20 20 %s **\n",.
87110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7a 46 zF
87120 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 72 65 ilename);. re
87130 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 turn;. }. for(
87140 70 48 64 72 3d 6d 65 6d 2e 70 46 69 72 73 74 3b pHdr=mem.pFirst;
87150 20 70 48 64 72 3b 20 70 48 64 72 3d 70 48 64 72 pHdr; pHdr=pHdr
87160 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 63 68 ->pNext){. ch
87170 61 72 20 2a 7a 20 3d 20 28 63 68 61 72 2a 29 70 ar *z = (char*)p
87180 48 64 72 3b 0a 20 20 20 20 7a 20 2d 3d 20 70 48 Hdr;. z -= pH
87190 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 53 6c dr->nBacktraceSl
871a0 6f 74 73 2a 73 69 7a 65 6f 66 28 76 6f 69 64 2a ots*sizeof(void*
871b0 29 20 2b 20 70 48 64 72 2d 3e 6e 54 69 74 6c 65 ) + pHdr->nTitle
871c0 3b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 ;. fprintf(ou
871d0 74 2c 20 22 2a 2a 2a 2a 20 25 6c 6c 64 20 62 79 t, "**** %lld by
871e0 74 65 73 20 61 74 20 25 70 20 66 72 6f 6d 20 25 tes at %p from %
871f0 73 20 2a 2a 2a 2a 5c 6e 22 2c 20 0a 20 20 20 20 s ****\n", .
87200 20 20 20 20 20 20 20 20 70 48 64 72 2d 3e 69 53 pHdr->iS
87210 69 7a 65 2c 20 26 70 48 64 72 5b 31 5d 2c 20 70 ize, &pHdr[1], p
87220 48 64 72 2d 3e 6e 54 69 74 6c 65 20 3f 20 7a 20 Hdr->nTitle ? z
87230 3a 20 22 3f 3f 3f 22 29 3b 0a 20 20 20 20 69 66 : "???");. if
87240 28 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 ( pHdr->nBacktra
87250 63 65 20 29 7b 0a 20 20 20 20 20 20 66 66 6c 75 ce ){. fflu
87260 73 68 28 6f 75 74 29 3b 0a 20 20 20 20 20 20 70 sh(out);. p
87270 42 74 20 3d 20 28 76 6f 69 64 2a 2a 29 70 48 64 Bt = (void**)pHd
87280 72 3b 0a 20 20 20 20 20 20 70 42 74 20 2d 3d 20 r;. pBt -=
87290 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 pHdr->nBacktrace
872a0 53 6c 6f 74 73 3b 0a 20 20 20 20 20 20 62 61 63 Slots;. bac
872b0 6b 74 72 61 63 65 5f 73 79 6d 62 6f 6c 73 5f 66 ktrace_symbols_f
872c0 64 28 70 42 74 2c 20 70 48 64 72 2d 3e 6e 42 61 d(pBt, pHdr->nBa
872d0 63 6b 74 72 61 63 65 2c 20 66 69 6c 65 6e 6f 28 cktrace, fileno(
872e0 6f 75 74 29 29 3b 0a 20 20 20 20 20 20 66 70 72 out));. fpr
872f0 69 6e 74 66 28 6f 75 74 2c 20 22 5c 6e 22 29 3b intf(out, "\n");
87300 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 66 70 72 . }. }. fpr
87310 69 6e 74 66 28 6f 75 74 2c 20 22 43 4f 55 4e 54 intf(out, "COUNT
87320 53 3a 5c 6e 22 29 3b 0a 20 20 66 6f 72 28 69 3d S:\n");. for(i=
87330 30 3b 20 69 3c 4e 43 53 49 5a 45 2d 31 3b 20 69 0; i<NCSIZE-1; i
87340 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 6d 65 6d ++){. if( mem
87350 2e 6e 41 6c 6c 6f 63 5b 69 5d 20 29 7b 0a 20 20 .nAlloc[i] ){.
87360 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c fprintf(out,
87370 20 22 20 20 20 25 35 64 3a 20 25 31 30 64 20 25 " %5d: %10d %
87380 31 30 64 20 25 31 30 64 5c 6e 22 2c 20 0a 20 20 10d %10d\n", .
87390 20 20 20 20 20 20 20 20 20 20 69 2a 38 2c 20 6d i*8, m
873a0 65 6d 2e 6e 41 6c 6c 6f 63 5b 69 5d 2c 20 6d 65 em.nAlloc[i], me
873b0 6d 2e 6e 43 75 72 72 65 6e 74 5b 69 5d 2c 20 6d m.nCurrent[i], m
873c0 65 6d 2e 6d 78 43 75 72 72 65 6e 74 5b 69 5d 29 em.mxCurrent[i])
873d0 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 ;. }. }. if
873e0 28 20 6d 65 6d 2e 6e 41 6c 6c 6f 63 5b 4e 43 53 ( mem.nAlloc[NCS
873f0 49 5a 45 2d 31 5d 20 29 7b 0a 20 20 20 20 66 70 IZE-1] ){. fp
87400 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 20 20 25 rintf(out, " %
87410 35 64 3a 20 25 31 30 64 20 25 31 30 64 20 25 31 5d: %10d %10d %1
87420 30 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 0d\n",.
87430 20 20 20 20 4e 43 53 49 5a 45 2a 38 2d 38 2c 20 NCSIZE*8-8,
87440 6d 65 6d 2e 6e 41 6c 6c 6f 63 5b 4e 43 53 49 5a mem.nAlloc[NCSIZ
87450 45 2d 31 5d 2c 0a 20 20 20 20 20 20 20 20 20 20 E-1],.
87460 20 20 20 6d 65 6d 2e 6e 43 75 72 72 65 6e 74 5b mem.nCurrent[
87470 4e 43 53 49 5a 45 2d 31 5d 2c 20 6d 65 6d 2e 6d NCSIZE-1], mem.m
87480 78 43 75 72 72 65 6e 74 5b 4e 43 53 49 5a 45 2d xCurrent[NCSIZE-
87490 31 5d 29 3b 0a 20 20 7d 0a 20 20 66 63 6c 6f 73 1]);. }. fclos
874a0 65 28 6f 75 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a e(out);.}../*.**
874b0 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 Return the numb
874c0 65 72 20 6f 66 20 74 69 6d 65 73 20 73 71 6c 69 er of times sqli
874d0 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 28 29 20 68 te3MemMalloc() h
874e0 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 64 2e 0a as been called..
874f0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
87500 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d E int sqlite3Mem
87510 64 65 62 75 67 4d 61 6c 6c 6f 63 43 6f 75 6e 74 debugMallocCount
87520 28 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 (){. int i;. i
87530 6e 74 20 6e 54 6f 74 61 6c 20 3d 20 30 3b 0a 20 nt nTotal = 0;.
87540 20 66 6f 72 28 69 3d 30 3b 20 69 3c 4e 43 53 49 for(i=0; i<NCSI
87550 5a 45 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 6e 54 ZE; i++){. nT
87560 6f 74 61 6c 20 2b 3d 20 6d 65 6d 2e 6e 41 6c 6c otal += mem.nAll
87570 6f 63 5b 69 5d 3b 0a 20 20 7d 0a 20 20 72 65 74 oc[i];. }. ret
87580 75 72 6e 20 6e 54 6f 74 61 6c 3b 0a 7d 0a 0a 0a urn nTotal;.}...
87590 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
875a0 5f 4d 45 4d 44 45 42 55 47 20 2a 2f 0a 0a 2f 2a _MEMDEBUG */../*
875b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e ************* En
875c0 64 20 6f 66 20 6d 65 6d 32 2e 63 20 2a 2a 2a 2a d of mem2.c ****
875d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
875e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
875f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
87600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 ************* Be
87610 67 69 6e 20 66 69 6c 65 20 6d 65 6d 33 2e 63 20 gin file mem3.c
87620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
87630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
87640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
87650 0a 2a 2a 20 32 30 30 37 20 4f 63 74 6f 62 65 72 .** 2007 October
87660 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 14.**.** The au
87670 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 thor disclaims c
87680 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 opyright to this
87690 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 source code. I
876a0 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 n place of.** a
876b0 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 legal notice, he
876c0 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 re is a blessing
876d0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 :.**.** May y
876e0 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e ou do good and n
876f0 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d ot evil..** M
87700 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 ay you find forg
87710 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 iveness for your
87720 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 self and forgive
87730 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d others..** M
87740 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 ay you share fre
87750 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e ely, never takin
87760 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 g more than you
87770 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a give..**.*******
87780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
87790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
877a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
877b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
877c0 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 **.** This file
877d0 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 43 20 66 contains the C f
877e0 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 69 6d unctions that im
877f0 70 6c 65 6d 65 6e 74 20 61 20 6d 65 6d 6f 72 79 plement a memory
87800 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 .** allocation s
87810 75 62 73 79 73 74 65 6d 20 66 6f 72 20 75 73 65 ubsystem for use
87820 20 62 79 20 53 51 4c 69 74 65 2e 20 0a 2a 2a 0a by SQLite. .**.
87830 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 ** This version
87840 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c of the memory al
87850 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 location subsyst
87860 65 6d 20 6f 6d 69 74 73 20 61 6c 6c 0a 2a 2a 20 em omits all.**
87870 75 73 65 20 6f 66 20 6d 61 6c 6c 6f 63 28 29 2e use of malloc().
87880 20 54 68 65 20 53 51 4c 69 74 65 20 75 73 65 72 The SQLite user
87890 20 73 75 70 70 6c 69 65 73 20 61 20 62 6c 6f 63 supplies a bloc
878a0 6b 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 62 k of memory.** b
878b0 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 73 71 efore calling sq
878c0 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 lite3_initialize
878d0 28 29 20 66 72 6f 6d 20 77 68 69 63 68 20 61 6c () from which al
878e0 6c 6f 63 61 74 69 6f 6e 73 0a 2a 2a 20 61 72 65 locations.** are
878f0 20 6d 61 64 65 20 61 6e 64 20 72 65 74 75 72 6e made and return
87900 65 64 20 62 79 20 74 68 65 20 78 4d 61 6c 6c 6f ed by the xMallo
87910 63 28 29 20 61 6e 64 20 78 52 65 61 6c 6c 6f 63 c() and xRealloc
87920 28 29 20 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 () .** implement
87930 61 74 69 6f 6e 73 2e 20 4f 6e 63 65 20 73 71 6c ations. Once sql
87940 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 ite3_initialize(
87950 29 20 68 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 ) has been calle
87960 64 2c 0a 2a 2a 20 74 68 65 20 61 6d 6f 75 6e 74 d,.** the amount
87970 20 6f 66 20 6d 65 6d 6f 72 79 20 61 76 61 69 6c of memory avail
87980 61 62 6c 65 20 74 6f 20 53 51 4c 69 74 65 20 69 able to SQLite i
87990 73 20 66 69 78 65 64 20 61 6e 64 20 63 61 6e 6e s fixed and cann
879a0 6f 74 0a 2a 2a 20 62 65 20 63 68 61 6e 67 65 64 ot.** be changed
879b0 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 ..**.** This ver
879c0 73 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f sion of the memo
879d0 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 ry allocation su
879e0 62 73 79 73 74 65 6d 20 69 73 20 69 6e 63 6c 75 bsystem is inclu
879f0 64 65 64 0a 2a 2a 20 69 6e 20 74 68 65 20 62 75 ded.** in the bu
87a00 69 6c 64 20 6f 6e 6c 79 20 69 66 20 53 51 4c 49 ild only if SQLI
87a10 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 TE_ENABLE_MEMSYS
87a20 33 20 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 3 is defined..**
87a30 0a 2a 2a 20 24 49 64 3a 20 6d 65 6d 33 2e 63 2c .** $Id: mem3.c,
87a40 76 20 31 2e 32 35 20 32 30 30 38 2f 31 31 2f 31 v 1.25 2008/11/1
87a50 39 20 31 36 3a 35 32 3a 34 34 20 64 61 6e 69 65 9 16:52:44 danie
87a60 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a lk1977 Exp $.*/.
87a70 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 ./*.** This vers
87a80 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 ion of the memor
87a90 79 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 6f y allocator is o
87aa0 6e 6c 79 20 62 75 69 6c 74 20 69 6e 74 6f 20 74 nly built into t
87ab0 68 65 20 6c 69 62 72 61 72 79 0a 2a 2a 20 53 51 he library.** SQ
87ac0 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 LITE_ENABLE_MEMS
87ad0 59 53 33 20 69 73 20 64 65 66 69 6e 65 64 2e 20 YS3 is defined.
87ae0 44 65 66 69 6e 69 6e 67 20 74 68 69 73 20 73 79 Defining this sy
87af0 6d 62 6f 6c 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a mbol does not.**
87b00 20 6d 65 61 6e 20 74 68 61 74 20 74 68 65 20 6c mean that the l
87b10 69 62 72 61 72 79 20 77 69 6c 6c 20 75 73 65 20 ibrary will use
87b20 61 20 6d 65 6d 6f 72 79 2d 70 6f 6f 6c 20 62 79 a memory-pool by
87b30 20 64 65 66 61 75 6c 74 2c 20 6a 75 73 74 20 74 default, just t
87b40 68 61 74 0a 2a 2a 20 69 74 20 69 73 20 61 76 61 hat.** it is ava
87b50 69 6c 61 62 6c 65 2e 20 54 68 65 20 6d 65 6d 70 ilable. The memp
87b60 6f 6f 6c 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 ool allocator is
87b70 20 61 63 74 69 76 61 74 65 64 20 62 79 20 63 61 activated by ca
87b80 6c 6c 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33 lling.** sqlite3
87b90 5f 63 6f 6e 66 69 67 28 29 2e 0a 2a 2f 0a 23 69 _config()..*/.#i
87ba0 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 fdef SQLITE_ENAB
87bb0 4c 45 5f 4d 45 4d 53 59 53 33 0a 0a 2f 2a 0a 2a LE_MEMSYS3../*.*
87bc0 2a 20 4d 61 78 69 6d 75 6d 20 73 69 7a 65 20 28 * Maximum size (
87bd0 69 6e 20 4d 65 6d 33 42 6c 6f 63 6b 73 29 20 6f in Mem3Blocks) o
87be0 66 20 61 20 22 73 6d 61 6c 6c 22 20 63 68 75 6e f a "small" chun
87bf0 6b 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 58 k..*/.#define MX
87c00 5f 53 4d 41 4c 4c 20 31 30 0a 0a 0a 2f 2a 0a 2a _SMALL 10.../*.*
87c10 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 72 65 65 * Number of free
87c20 6c 69 73 74 20 68 61 73 68 20 73 6c 6f 74 73 0a list hash slots.
87c30 2a 2f 0a 23 64 65 66 69 6e 65 20 4e 5f 48 41 53 */.#define N_HAS
87c40 48 20 20 36 31 0a 0a 2f 2a 0a 2a 2a 20 41 20 6d H 61../*.** A m
87c50 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
87c60 20 28 61 6c 73 6f 20 63 61 6c 6c 65 64 20 61 20 (also called a
87c70 22 63 68 75 6e 6b 22 29 20 63 6f 6e 73 69 73 74 "chunk") consist
87c80 73 20 6f 66 20 74 77 6f 20 6f 72 20 0a 2a 2a 20 s of two or .**
87c90 6d 6f 72 65 20 62 6c 6f 63 6b 73 20 77 68 65 72 more blocks wher
87ca0 65 20 65 61 63 68 20 62 6c 6f 63 6b 20 69 73 20 e each block is
87cb0 38 20 62 79 74 65 73 2e 20 20 54 68 65 20 66 69 8 bytes. The fi
87cc0 72 73 74 20 38 20 62 79 74 65 73 20 61 72 65 20 rst 8 bytes are
87cd0 0a 2a 2a 20 61 20 68 65 61 64 65 72 20 74 68 61 .** a header tha
87ce0 74 20 69 73 20 6e 6f 74 20 72 65 74 75 72 6e 65 t is not returne
87cf0 64 20 74 6f 20 74 68 65 20 75 73 65 72 2e 0a 2a d to the user..*
87d00 2a 0a 2a 2a 20 41 20 63 68 75 6e 6b 20 69 73 20 *.** A chunk is
87d10 74 77 6f 20 6f 72 20 6d 6f 72 65 20 62 6c 6f 63 two or more bloc
87d20 6b 73 20 74 68 61 74 20 69 73 20 65 69 74 68 65 ks that is eithe
87d30 72 20 63 68 65 63 6b 65 64 20 6f 75 74 20 6f 72 r checked out or
87d40 0a 2a 2a 20 66 72 65 65 2e 20 20 54 68 65 20 66 .** free. The f
87d50 69 72 73 74 20 62 6c 6f 63 6b 20 68 61 73 20 66 irst block has f
87d60 6f 72 6d 61 74 20 75 2e 68 64 72 2e 20 20 75 2e ormat u.hdr. u.
87d70 68 64 72 2e 73 69 7a 65 34 78 20 69 73 20 34 20 hdr.size4x is 4
87d80 74 69 6d 65 73 20 74 68 65 0a 2a 2a 20 73 69 7a times the.** siz
87d90 65 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 e of the allocat
87da0 69 6f 6e 20 69 6e 20 62 6c 6f 63 6b 73 20 69 66 ion in blocks if
87db0 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 the allocation
87dc0 69 73 20 66 72 65 65 2e 0a 2a 2a 20 54 68 65 20 is free..** The
87dd0 75 2e 68 64 72 2e 73 69 7a 65 34 78 26 31 20 62 u.hdr.size4x&1 b
87de0 69 74 20 69 73 20 74 72 75 65 20 69 66 20 74 68 it is true if th
87df0 65 20 63 68 75 6e 6b 20 69 73 20 63 68 65 63 6b e chunk is check
87e00 65 64 20 6f 75 74 20 61 6e 64 0a 2a 2a 20 66 61 ed out and.** fa
87e10 6c 73 65 20 69 66 20 74 68 65 20 63 68 75 6e 6b lse if the chunk
87e20 20 69 73 20 6f 6e 20 74 68 65 20 66 72 65 65 6c is on the freel
87e30 69 73 74 2e 20 20 54 68 65 20 75 2e 68 64 72 2e ist. The u.hdr.
87e40 73 69 7a 65 34 78 26 32 20 62 69 74 0a 2a 2a 20 size4x&2 bit.**
87e50 69 73 20 74 72 75 65 20 69 66 20 74 68 65 20 70 is true if the p
87e60 72 65 76 69 6f 75 73 20 63 68 75 6e 6b 20 69 73 revious chunk is
87e70 20 63 68 65 63 6b 65 64 20 6f 75 74 20 61 6e 64 checked out and
87e80 20 66 61 6c 73 65 20 69 66 20 74 68 65 0a 2a 2a false if the.**
87e90 20 70 72 65 76 69 6f 75 73 20 63 68 75 6e 6b 20 previous chunk
87ea0 69 73 20 66 72 65 65 2e 20 20 54 68 65 20 75 2e is free. The u.
87eb0 68 64 72 2e 70 72 65 76 53 69 7a 65 20 66 69 65 hdr.prevSize fie
87ec0 6c 64 20 69 73 20 74 68 65 20 73 69 7a 65 20 6f ld is the size o
87ed0 66 0a 2a 2a 20 74 68 65 20 70 72 65 76 69 6f 75 f.** the previou
87ee0 73 20 63 68 75 6e 6b 20 69 6e 20 62 6c 6f 63 6b s chunk in block
87ef0 73 20 69 66 20 74 68 65 20 70 72 65 76 69 6f 75 s if the previou
87f00 73 20 63 68 75 6e 6b 20 69 73 20 6f 6e 20 74 68 s chunk is on th
87f10 65 0a 2a 2a 20 66 72 65 65 6c 69 73 74 2e 20 49 e.** freelist. I
87f20 66 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 63 f the previous c
87f30 68 75 6e 6b 20 69 73 20 63 68 65 63 6b 65 64 20 hunk is checked
87f40 6f 75 74 2c 20 74 68 65 6e 0a 2a 2a 20 75 2e 68 out, then.** u.h
87f50 64 72 2e 70 72 65 76 53 69 7a 65 20 63 61 6e 20 dr.prevSize can
87f60 62 65 20 70 61 72 74 20 6f 66 20 74 68 65 20 64 be part of the d
87f70 61 74 61 20 66 6f 72 20 74 68 61 74 20 63 68 75 ata for that chu
87f80 6e 6b 20 61 6e 64 20 73 68 6f 75 6c 64 0a 2a 2a nk and should.**
87f90 20 6e 6f 74 20 62 65 20 72 65 61 64 20 6f 72 20 not be read or
87fa0 77 72 69 74 74 65 6e 2e 0a 2a 2a 0a 2a 2a 20 57 written..**.** W
87fb0 65 20 6f 66 74 65 6e 20 69 64 65 6e 74 69 66 79 e often identify
87fc0 20 61 20 63 68 75 6e 6b 20 62 79 20 69 74 73 20 a chunk by its
87fd0 69 6e 64 65 78 20 69 6e 20 6d 65 6d 33 2e 61 50 index in mem3.aP
87fe0 6f 6f 6c 5b 5d 2e 20 20 57 68 65 6e 0a 2a 2a 20 ool[]. When.**
87ff0 74 68 69 73 20 69 73 20 64 6f 6e 65 2c 20 74 68 this is done, th
88000 65 20 63 68 75 6e 6b 20 69 6e 64 65 78 20 72 65 e chunk index re
88010 66 65 72 73 20 74 6f 20 74 68 65 20 73 65 63 6f fers to the seco
88020 6e 64 20 62 6c 6f 63 6b 20 6f 66 0a 2a 2a 20 74 nd block of.** t
88030 68 65 20 63 68 75 6e 6b 2e 20 20 49 6e 20 74 68 he chunk. In th
88040 69 73 20 77 61 79 2c 20 74 68 65 20 66 69 72 73 is way, the firs
88050 74 20 63 68 75 6e 6b 20 68 61 73 20 61 6e 20 69 t chunk has an i
88060 6e 64 65 78 20 6f 66 20 31 2e 0a 2a 2a 20 41 20 ndex of 1..** A
88070 63 68 75 6e 6b 20 69 6e 64 65 78 20 6f 66 20 30 chunk index of 0
88080 20 6d 65 61 6e 73 20 22 6e 6f 20 73 75 63 68 20 means "no such
88090 63 68 75 6e 6b 22 20 61 6e 64 20 69 73 20 74 68 chunk" and is th
880a0 65 20 65 71 75 69 76 61 6c 65 6e 74 0a 2a 2a 20 e equivalent.**
880b0 6f 66 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 of a NULL pointe
880c0 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 63 r..**.** The sec
880d0 6f 6e 64 20 62 6c 6f 63 6b 20 6f 66 20 66 72 65 ond block of fre
880e0 65 20 63 68 75 6e 6b 73 20 69 73 20 6f 66 20 74 e chunks is of t
880f0 68 65 20 66 6f 72 6d 20 75 2e 6c 69 73 74 2e 20 he form u.list.
88100 20 54 68 65 0a 2a 2a 20 74 77 6f 20 66 69 65 6c The.** two fiel
88110 64 73 20 66 6f 72 6d 20 61 20 64 6f 75 62 6c 65 ds form a double
88120 2d 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 -linked list of
88130 63 68 75 6e 6b 73 20 6f 66 20 72 65 6c 61 74 65 chunks of relate
88140 64 20 73 69 7a 65 73 2e 0a 2a 2a 20 50 6f 69 6e d sizes..** Poin
88150 74 65 72 73 20 74 6f 20 74 68 65 20 68 65 61 64 ters to the head
88160 20 6f 66 20 74 68 65 20 6c 69 73 74 20 61 72 65 of the list are
88170 20 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d 33 2e stored in mem3.
88180 61 69 53 6d 61 6c 6c 5b 5d 20 0a 2a 2a 20 66 6f aiSmall[] .** fo
88190 72 20 73 6d 61 6c 6c 65 72 20 63 68 75 6e 6b 73 r smaller chunks
881a0 20 61 6e 64 20 6d 65 6d 33 2e 61 69 48 61 73 68 and mem3.aiHash
881b0 5b 5d 20 66 6f 72 20 6c 61 72 67 65 72 20 63 68 [] for larger ch
881c0 75 6e 6b 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 unks..**.** The
881d0 73 65 63 6f 6e 64 20 62 6c 6f 63 6b 20 6f 66 20 second block of
881e0 61 20 63 68 75 6e 6b 20 69 73 20 75 73 65 72 20 a chunk is user
881f0 64 61 74 61 20 69 66 20 74 68 65 20 63 68 75 6e data if the chun
88200 6b 20 69 73 20 63 68 65 63 6b 65 64 20 0a 2a 2a k is checked .**
88210 20 6f 75 74 2e 20 20 49 66 20 61 20 63 68 75 6e out. If a chun
88220 6b 20 69 73 20 63 68 65 63 6b 65 64 20 6f 75 74 k is checked out
88230 2c 20 74 68 65 20 75 73 65 72 20 64 61 74 61 20 , the user data
88240 6d 61 79 20 65 78 74 65 6e 64 20 69 6e 74 6f 0a may extend into.
88250 2a 2a 20 74 68 65 20 75 2e 68 64 72 2e 70 72 65 ** the u.hdr.pre
88260 76 53 69 7a 65 20 76 61 6c 75 65 20 6f 66 20 74 vSize value of t
88270 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 75 he following chu
88280 6e 6b 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 nk..*/.typedef s
88290 74 72 75 63 74 20 4d 65 6d 33 42 6c 6f 63 6b 20 truct Mem3Block
882a0 4d 65 6d 33 42 6c 6f 63 6b 3b 0a 73 74 72 75 63 Mem3Block;.struc
882b0 74 20 4d 65 6d 33 42 6c 6f 63 6b 20 7b 0a 20 20 t Mem3Block {.
882c0 75 6e 69 6f 6e 20 7b 0a 20 20 20 20 73 74 72 75 union {. stru
882d0 63 74 20 7b 0a 20 20 20 20 20 20 75 33 32 20 70 ct {. u32 p
882e0 72 65 76 53 69 7a 65 3b 20 20 20 2f 2a 20 53 69 revSize; /* Si
882f0 7a 65 20 6f 66 20 70 72 65 76 69 6f 75 73 20 63 ze of previous c
88300 68 75 6e 6b 20 69 6e 20 4d 65 6d 33 42 6c 6f 63 hunk in Mem3Bloc
88310 6b 20 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 k elements */.
88320 20 20 20 20 75 33 32 20 73 69 7a 65 34 78 3b 20 u32 size4x;
88330 20 20 20 20 2f 2a 20 34 78 20 74 68 65 20 73 69 /* 4x the si
88340 7a 65 20 6f 66 20 63 75 72 72 65 6e 74 20 63 68 ze of current ch
88350 75 6e 6b 20 69 6e 20 4d 65 6d 33 42 6c 6f 63 6b unk in Mem3Block
88360 20 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 elements */.
88370 20 7d 20 68 64 72 3b 0a 20 20 20 20 73 74 72 75 } hdr;. stru
88380 63 74 20 7b 0a 20 20 20 20 20 20 75 33 32 20 6e ct {. u32 n
88390 65 78 74 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e ext; /* In
883a0 64 65 78 20 69 6e 20 6d 65 6d 33 2e 61 50 6f 6f dex in mem3.aPoo
883b0 6c 5b 5d 20 6f 66 20 6e 65 78 74 20 66 72 65 65 l[] of next free
883c0 20 63 68 75 6e 6b 20 2a 2f 0a 20 20 20 20 20 20 chunk */.
883d0 75 33 32 20 70 72 65 76 3b 20 20 20 20 20 20 20 u32 prev;
883e0 2f 2a 20 49 6e 64 65 78 20 69 6e 20 6d 65 6d 33 /* Index in mem3
883f0 2e 61 50 6f 6f 6c 5b 5d 20 6f 66 20 70 72 65 76 .aPool[] of prev
88400 69 6f 75 73 20 66 72 65 65 20 63 68 75 6e 6b 20 ious free chunk
88410 2a 2f 0a 20 20 20 20 7d 20 6c 69 73 74 3b 0a 20 */. } list;.
88420 20 7d 20 75 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 } u;.};../*.**
88430 41 6c 6c 20 6f 66 20 74 68 65 20 73 74 61 74 69 All of the stati
88440 63 20 76 61 72 69 61 62 6c 65 73 20 75 73 65 64 c variables used
88450 20 62 79 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 by this module
88460 61 72 65 20 63 6f 6c 6c 65 63 74 65 64 0a 2a 2a are collected.**
88470 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 73 into a single s
88480 74 72 75 63 74 75 72 65 20 6e 61 6d 65 64 20 22 tructure named "
88490 6d 65 6d 33 22 2e 20 20 54 68 69 73 20 69 73 20 mem3". This is
884a0 74 6f 20 6b 65 65 70 20 74 68 65 0a 2a 2a 20 73 to keep the.** s
884b0 74 61 74 69 63 20 76 61 72 69 61 62 6c 65 73 20 tatic variables
884c0 6f 72 67 61 6e 69 7a 65 64 20 61 6e 64 20 74 6f organized and to
884d0 20 72 65 64 75 63 65 20 6e 61 6d 65 73 70 61 63 reduce namespac
884e0 65 20 70 6f 6c 6c 75 74 69 6f 6e 0a 2a 2a 20 77 e pollution.** w
884f0 68 65 6e 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 hen this module
88500 69 73 20 63 6f 6d 62 69 6e 65 64 20 77 69 74 68 is combined with
88510 20 6f 74 68 65 72 20 69 6e 20 74 68 65 20 61 6d other in the am
88520 61 6c 67 61 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 algamation..*/.s
88530 74 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44 tatic SQLITE_WSD
88540 20 73 74 72 75 63 74 20 4d 65 6d 33 47 6c 6f 62 struct Mem3Glob
88550 61 6c 20 7b 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4d al {. /*. ** M
88560 65 6d 6f 72 79 20 61 76 61 69 6c 61 62 6c 65 20 emory available
88570 66 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 for allocation.
88580 6e 50 6f 6f 6c 20 69 73 20 74 68 65 20 73 69 7a nPool is the siz
88590 65 20 6f 66 20 74 68 65 20 61 72 72 61 79 0a 20 e of the array.
885a0 20 2a 2a 20 28 69 6e 20 4d 65 6d 33 42 6c 6f 63 ** (in Mem3Bloc
885b0 6b 73 29 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 ks) pointed to b
885c0 79 20 61 50 6f 6f 6c 20 6c 65 73 73 20 32 2e 0a y aPool less 2..
885d0 20 20 2a 2f 0a 20 20 75 33 32 20 6e 50 6f 6f 6c */. u32 nPool
885e0 3b 0a 20 20 4d 65 6d 33 42 6c 6f 63 6b 20 2a 61 ;. Mem3Block *a
885f0 50 6f 6f 6c 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a Pool;.. /*. **
88600 20 54 72 75 65 20 69 66 20 77 65 20 61 72 65 20 True if we are
88610 65 76 61 6c 75 61 74 69 6e 67 20 61 6e 20 6f 75 evaluating an ou
88620 74 2d 6f 66 2d 6d 65 6d 6f 72 79 20 63 61 6c 6c t-of-memory call
88630 62 61 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 back.. */. int
88640 20 61 6c 61 72 6d 42 75 73 79 3b 0a 20 20 0a 20 alarmBusy;. .
88650 20 2f 2a 0a 20 20 2a 2a 20 4d 75 74 65 78 20 74 /*. ** Mutex t
88660 6f 20 63 6f 6e 74 72 6f 6c 20 61 63 63 65 73 73 o control access
88670 20 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 to the memory a
88680 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 llocation subsys
88690 74 65 6d 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 tem.. */. sqli
886a0 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 te3_mutex *mutex
886b0 3b 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 ;. . /*. ** T
886c0 68 65 20 6d 69 6e 69 6d 75 6d 20 61 6d 6f 75 6e he minimum amoun
886d0 74 20 6f 66 20 66 72 65 65 20 73 70 61 63 65 20 t of free space
886e0 74 68 61 74 20 77 65 20 68 61 76 65 20 73 65 65 that we have see
886f0 6e 2e 0a 20 20 2a 2f 0a 20 20 75 33 32 20 6d 6e n.. */. u32 mn
88700 4d 61 73 74 65 72 3b 0a 0a 20 20 2f 2a 0a 20 20 Master;.. /*.
88710 2a 2a 20 69 4d 61 73 74 65 72 20 69 73 20 74 68 ** iMaster is th
88720 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 6d e index of the m
88730 61 73 74 65 72 20 63 68 75 6e 6b 2e 20 20 4d 6f aster chunk. Mo
88740 73 74 20 6e 65 77 20 61 6c 6c 6f 63 61 74 69 6f st new allocatio
88750 6e 73 0a 20 20 2a 2a 20 6f 63 63 75 72 20 6f 66 ns. ** occur of
88760 66 20 6f 66 20 74 68 69 73 20 63 68 75 6e 6b 2e f of this chunk.
88770 20 20 73 7a 4d 61 73 74 65 72 20 69 73 20 74 68 szMaster is th
88780 65 20 73 69 7a 65 20 28 69 6e 20 4d 65 6d 33 42 e size (in Mem3B
88790 6c 6f 63 6b 73 29 0a 20 20 2a 2a 20 6f 66 20 74 locks). ** of t
887a0 68 65 20 63 75 72 72 65 6e 74 20 6d 61 73 74 65 he current maste
887b0 72 2e 20 20 69 4d 61 73 74 65 72 20 69 73 20 30 r. iMaster is 0
887c0 20 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 74 if there is not
887d0 20 6d 61 73 74 65 72 20 63 68 75 6e 6b 2e 0a 20 master chunk..
887e0 20 2a 2a 20 54 68 65 20 6d 61 73 74 65 72 20 63 ** The master c
887f0 68 75 6e 6b 20 69 73 20 6e 6f 74 20 69 6e 20 65 hunk is not in e
88800 69 74 68 65 72 20 74 68 65 20 61 69 48 61 73 68 ither the aiHash
88810 5b 5d 20 6f 72 20 61 69 53 6d 61 6c 6c 5b 5d 2e [] or aiSmall[].
88820 0a 20 20 2a 2f 0a 20 20 75 33 32 20 69 4d 61 73 . */. u32 iMas
88830 74 65 72 3b 0a 20 20 75 33 32 20 73 7a 4d 61 73 ter;. u32 szMas
88840 74 65 72 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 ter;.. /*. **
88850 41 72 72 61 79 20 6f 66 20 6c 69 73 74 73 20 6f Array of lists o
88860 66 20 66 72 65 65 20 62 6c 6f 63 6b 73 20 61 63 f free blocks ac
88870 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 20 62 cording to the b
88880 6c 6f 63 6b 20 73 69 7a 65 20 0a 20 20 2a 2a 20 lock size . **
88890 66 6f 72 20 73 6d 61 6c 6c 65 72 20 63 68 75 6e for smaller chun
888a0 6b 73 2c 20 6f 72 20 61 20 68 61 73 68 20 6f 6e ks, or a hash on
888b0 20 74 68 65 20 62 6c 6f 63 6b 20 73 69 7a 65 20 the block size
888c0 66 6f 72 20 6c 61 72 67 65 72 0a 20 20 2a 2a 20 for larger. **
888d0 63 68 75 6e 6b 73 2e 0a 20 20 2a 2f 0a 20 20 75 chunks.. */. u
888e0 33 32 20 61 69 53 6d 61 6c 6c 5b 4d 58 5f 53 4d 32 aiSmall[MX_SM
888f0 41 4c 4c 2d 31 5d 3b 20 20 20 2f 2a 20 46 6f 72 ALL-1]; /* For
88900 20 73 69 7a 65 73 20 32 20 74 68 72 6f 75 67 68 sizes 2 through
88910 20 4d 58 5f 53 4d 41 4c 4c 2c 20 69 6e 63 6c 75 MX_SMALL, inclu
88920 73 69 76 65 20 2a 2f 0a 20 20 75 33 32 20 61 69 sive */. u32 ai
88930 48 61 73 68 5b 4e 5f 48 41 53 48 5d 3b 20 20 20 Hash[N_HASH];
88940 20 20 20 20 20 2f 2a 20 46 6f 72 20 73 69 7a 65 /* For size
88950 73 20 4d 58 5f 53 4d 41 4c 4c 2b 31 20 61 6e 64 s MX_SMALL+1 and
88960 20 6c 61 72 67 65 72 20 2a 2f 0a 7d 20 6d 65 6d larger */.} mem
88970 33 20 3d 20 7b 20 39 37 35 33 35 35 37 35 20 7d 3 = { 97535575 }
88980 3b 0a 0a 23 64 65 66 69 6e 65 20 6d 65 6d 33 20 ;..#define mem3
88990 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 4d 65 GLOBAL(struct Me
889a0 6d 33 47 6c 6f 62 61 6c 2c 20 6d 65 6d 33 29 0a m3Global, mem3).
889b0 0a 2f 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b 20 74 68 ./*.** Unlink th
889c0 65 20 63 68 75 6e 6b 20 61 74 20 6d 65 6d 33 2e e chunk at mem3.
889d0 61 50 6f 6f 6c 5b 69 5d 20 66 72 6f 6d 20 6c 69 aPool[i] from li
889e0 73 74 20 69 74 20 69 73 20 63 75 72 72 65 6e 74 st it is current
889f0 6c 79 0a 2a 2a 20 6f 6e 2e 20 20 2a 70 52 6f 6f ly.** on. *pRoo
88a00 74 20 69 73 20 74 68 65 20 6c 69 73 74 20 74 68 t is the list th
88a10 61 74 20 69 20 69 73 20 61 20 6d 65 6d 62 65 72 at i is a member
88a20 20 6f 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 of..*/.static v
88a30 6f 69 64 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e oid memsys3Unlin
88a40 6b 46 72 6f 6d 4c 69 73 74 28 75 33 32 20 69 2c kFromList(u32 i,
88a50 20 75 33 32 20 2a 70 52 6f 6f 74 29 7b 0a 20 20 u32 *pRoot){.
88a60 75 33 32 20 6e 65 78 74 20 3d 20 6d 65 6d 33 2e u32 next = mem3.
88a70 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e aPool[i].u.list.
88a80 6e 65 78 74 3b 0a 20 20 75 33 32 20 70 72 65 76 next;. u32 prev
88a90 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d = mem3.aPool[i]
88aa0 2e 75 2e 6c 69 73 74 2e 70 72 65 76 3b 0a 20 20 .u.list.prev;.
88ab0 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
88ac0 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e mutex_held(mem3.
88ad0 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 mutex) );. if(
88ae0 70 72 65 76 3d 3d 30 20 29 7b 0a 20 20 20 20 2a prev==0 ){. *
88af0 70 52 6f 6f 74 20 3d 20 6e 65 78 74 3b 0a 20 20 pRoot = next;.
88b00 7d 65 6c 73 65 7b 0a 20 20 20 20 6d 65 6d 33 2e }else{. mem3.
88b10 61 50 6f 6f 6c 5b 70 72 65 76 5d 2e 75 2e 6c 69 aPool[prev].u.li
88b20 73 74 2e 6e 65 78 74 20 3d 20 6e 65 78 74 3b 0a st.next = next;.
88b30 20 20 7d 0a 20 20 69 66 28 20 6e 65 78 74 20 29 }. if( next )
88b40 7b 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c {. mem3.aPool
88b50 5b 6e 65 78 74 5d 2e 75 2e 6c 69 73 74 2e 70 72 [next].u.list.pr
88b60 65 76 20 3d 20 70 72 65 76 3b 0a 20 20 7d 0a 20 ev = prev;. }.
88b70 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 mem3.aPool[i].u
88b80 2e 6c 69 73 74 2e 6e 65 78 74 20 3d 20 30 3b 0a .list.next = 0;.
88b90 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e mem3.aPool[i].
88ba0 75 2e 6c 69 73 74 2e 70 72 65 76 20 3d 20 30 3b u.list.prev = 0;
88bb0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b .}../*.** Unlink
88bc0 20 74 68 65 20 63 68 75 6e 6b 20 61 74 20 69 6e the chunk at in
88bd0 64 65 78 20 69 20 66 72 6f 6d 20 0a 2a 2a 20 77 dex i from .** w
88be0 68 61 74 65 76 65 72 20 6c 69 73 74 20 69 73 20 hatever list is
88bf0 63 75 72 72 65 6e 74 6c 79 20 61 20 6d 65 6d 62 currently a memb
88c00 65 72 20 6f 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 er of..*/.static
88c10 20 76 6f 69 64 20 6d 65 6d 73 79 73 33 55 6e 6c void memsys3Unl
88c20 69 6e 6b 28 75 33 32 20 69 29 7b 0a 20 20 75 33 ink(u32 i){. u3
88c30 32 20 73 69 7a 65 2c 20 68 61 73 68 3b 0a 20 20 2 size, hash;.
88c40 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
88c50 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e mutex_held(mem3.
88c60 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 mutex) );. asse
88c70 72 74 28 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b rt( (mem3.aPool[
88c80 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 i-1].u.hdr.size4
88c90 78 20 26 20 31 29 3d 3d 30 20 29 3b 0a 20 20 61 x & 1)==0 );. a
88ca0 73 73 65 72 74 28 20 69 3e 3d 31 20 29 3b 0a 20 ssert( i>=1 );.
88cb0 20 73 69 7a 65 20 3d 20 6d 65 6d 33 2e 61 50 6f size = mem3.aPo
88cc0 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 ol[i-1].u.hdr.si
88cd0 7a 65 34 78 2f 34 3b 0a 20 20 61 73 73 65 72 74 ze4x/4;. assert
88ce0 28 20 73 69 7a 65 3d 3d 6d 65 6d 33 2e 61 50 6f ( size==mem3.aPo
88cf0 6f 6c 5b 69 2b 73 69 7a 65 2d 31 5d 2e 75 2e 68 ol[i+size-1].u.h
88d00 64 72 2e 70 72 65 76 53 69 7a 65 20 29 3b 0a 20 dr.prevSize );.
88d10 20 61 73 73 65 72 74 28 20 73 69 7a 65 3e 3d 32 assert( size>=2
88d20 20 29 3b 0a 20 20 69 66 28 20 73 69 7a 65 20 3c );. if( size <
88d30 3d 20 4d 58 5f 53 4d 41 4c 4c 20 29 7b 0a 20 20 = MX_SMALL ){.
88d40 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 46 memsys3UnlinkF
88d50 72 6f 6d 4c 69 73 74 28 69 2c 20 26 6d 65 6d 33 romList(i, &mem3
88d60 2e 61 69 53 6d 61 6c 6c 5b 73 69 7a 65 2d 32 5d .aiSmall[size-2]
88d70 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
88d80 68 61 73 68 20 3d 20 73 69 7a 65 20 25 20 4e 5f hash = size % N_
88d90 48 41 53 48 3b 0a 20 20 20 20 6d 65 6d 73 79 73 HASH;. memsys
88da0 33 55 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74 28 3UnlinkFromList(
88db0 69 2c 20 26 6d 65 6d 33 2e 61 69 48 61 73 68 5b i, &mem3.aiHash[
88dc0 68 61 73 68 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f hash]);. }.}../
88dd0 2a 0a 2a 2a 20 4c 69 6e 6b 20 74 68 65 20 63 68 *.** Link the ch
88de0 75 6e 6b 20 61 74 20 6d 65 6d 33 2e 61 50 6f 6f unk at mem3.aPoo
88df0 6c 5b 69 5d 20 73 6f 20 74 68 61 74 20 69 73 20 l[i] so that is
88e00 6f 6e 20 74 68 65 20 6c 69 73 74 20 72 6f 6f 74 on the list root
88e10 65 64 0a 2a 2a 20 61 74 20 2a 70 52 6f 6f 74 2e ed.** at *pRoot.
88e20 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
88e30 6d 65 6d 73 79 73 33 4c 69 6e 6b 49 6e 74 6f 4c memsys3LinkIntoL
88e40 69 73 74 28 75 33 32 20 69 2c 20 75 33 32 20 2a ist(u32 i, u32 *
88e50 70 52 6f 6f 74 29 7b 0a 20 20 61 73 73 65 72 74 pRoot){. assert
88e60 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
88e70 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 held(mem3.mutex)
88e80 20 29 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c );. mem3.aPool
88e90 5b 69 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 20 [i].u.list.next
88ea0 3d 20 2a 70 52 6f 6f 74 3b 0a 20 20 6d 65 6d 33 = *pRoot;. mem3
88eb0 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 .aPool[i].u.list
88ec0 2e 70 72 65 76 20 3d 20 30 3b 0a 20 20 69 66 28 .prev = 0;. if(
88ed0 20 2a 70 52 6f 6f 74 20 29 7b 0a 20 20 20 20 6d *pRoot ){. m
88ee0 65 6d 33 2e 61 50 6f 6f 6c 5b 2a 70 52 6f 6f 74 em3.aPool[*pRoot
88ef0 5d 2e 75 2e 6c 69 73 74 2e 70 72 65 76 20 3d 20 ].u.list.prev =
88f00 69 3b 0a 20 20 7d 0a 20 20 2a 70 52 6f 6f 74 20 i;. }. *pRoot
88f10 3d 20 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 69 = i;.}../*.** Li
88f20 6e 6b 20 74 68 65 20 63 68 75 6e 6b 20 61 74 20 nk the chunk at
88f30 69 6e 64 65 78 20 69 20 69 6e 74 6f 20 65 69 74 index i into eit
88f40 68 65 72 20 74 68 65 20 61 70 70 72 6f 70 72 69 her the appropri
88f50 61 74 65 0a 2a 2a 20 73 6d 61 6c 6c 20 63 68 75 ate.** small chu
88f60 6e 6b 20 6c 69 73 74 2c 20 6f 72 20 69 6e 74 6f nk list, or into
88f70 20 74 68 65 20 6c 61 72 67 65 20 63 68 75 6e 6b the large chunk
88f80 20 68 61 73 68 20 74 61 62 6c 65 2e 0a 2a 2f 0a hash table..*/.
88f90 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 static void mems
88fa0 79 73 33 4c 69 6e 6b 28 75 33 32 20 69 29 7b 0a ys3Link(u32 i){.
88fb0 20 20 75 33 32 20 73 69 7a 65 2c 20 68 61 73 68 u32 size, hash
88fc0 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
88fd0 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d te3_mutex_held(m
88fe0 65 6d 33 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 em3.mutex) );.
88ff0 61 73 73 65 72 74 28 20 69 3e 3d 31 20 29 3b 0a assert( i>=1 );.
89000 20 20 61 73 73 65 72 74 28 20 28 6d 65 6d 33 2e assert( (mem3.
89010 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 aPool[i-1].u.hdr
89020 2e 73 69 7a 65 34 78 20 26 20 31 29 3d 3d 30 20 .size4x & 1)==0
89030 29 3b 0a 20 20 73 69 7a 65 20 3d 20 6d 65 6d 33 );. size = mem3
89040 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 .aPool[i-1].u.hd
89050 72 2e 73 69 7a 65 34 78 2f 34 3b 0a 20 20 61 73 r.size4x/4;. as
89060 73 65 72 74 28 20 73 69 7a 65 3d 3d 6d 65 6d 33 sert( size==mem3
89070 2e 61 50 6f 6f 6c 5b 69 2b 73 69 7a 65 2d 31 5d .aPool[i+size-1]
89080 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 20 .u.hdr.prevSize
89090 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a );. assert( siz
890a0 65 3e 3d 32 20 29 3b 0a 20 20 69 66 28 20 73 69 e>=2 );. if( si
890b0 7a 65 20 3c 3d 20 4d 58 5f 53 4d 41 4c 4c 20 29 ze <= MX_SMALL )
890c0 7b 0a 20 20 20 20 6d 65 6d 73 79 73 33 4c 69 6e {. memsys3Lin
890d0 6b 49 6e 74 6f 4c 69 73 74 28 69 2c 20 26 6d 65 kIntoList(i, &me
890e0 6d 33 2e 61 69 53 6d 61 6c 6c 5b 73 69 7a 65 2d m3.aiSmall[size-
890f0 32 5d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 2]);. }else{.
89100 20 20 68 61 73 68 20 3d 20 73 69 7a 65 20 25 20 hash = size %
89110 4e 5f 48 41 53 48 3b 0a 20 20 20 20 6d 65 6d 73 N_HASH;. mems
89120 79 73 33 4c 69 6e 6b 49 6e 74 6f 4c 69 73 74 28 ys3LinkIntoList(
89130 69 2c 20 26 6d 65 6d 33 2e 61 69 48 61 73 68 5b i, &mem3.aiHash[
89140 68 61 73 68 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f hash]);. }.}../
89150 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 54 41 54 *.** If the STAT
89160 49 43 5f 4d 45 4d 20 6d 75 74 65 78 20 69 73 20 IC_MEM mutex is
89170 6e 6f 74 20 61 6c 72 65 61 64 79 20 68 65 6c 64 not already held
89180 2c 20 6f 62 74 61 69 6e 20 69 74 20 6e 6f 77 2e , obtain it now.
89190 20 54 68 65 20 6d 75 74 65 78 0a 2a 2a 20 77 69 The mutex.** wi
891a0 6c 6c 20 61 6c 72 65 61 64 79 20 62 65 20 68 65 ll already be he
891b0 6c 64 20 28 6f 62 74 61 69 6e 65 64 20 62 79 20 ld (obtained by
891c0 63 6f 64 65 20 69 6e 20 6d 61 6c 6c 6f 63 2e 63 code in malloc.c
891d0 29 20 69 66 0a 2a 2a 20 73 71 6c 69 74 65 33 47 ) if.** sqlite3G
891e0 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d lobalConfig.bMem
891f0 53 74 61 74 20 69 73 20 74 72 75 65 2e 0a 2a 2f Stat is true..*/
89200 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d .static void mem
89210 73 79 73 33 45 6e 74 65 72 28 76 6f 69 64 29 7b sys3Enter(void){
89220 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c . if( sqlite3Gl
89230 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 obalConfig.bMems
89240 74 61 74 3d 3d 30 20 26 26 20 6d 65 6d 33 2e 6d tat==0 && mem3.m
89250 75 74 65 78 3d 3d 30 20 29 7b 0a 20 20 20 20 6d utex==0 ){. m
89260 65 6d 33 2e 6d 75 74 65 78 20 3d 20 73 71 6c 69 em3.mutex = sqli
89270 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 te3MutexAlloc(SQ
89280 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 LITE_MUTEX_STATI
89290 43 5f 4d 45 4d 29 3b 0a 20 20 7d 0a 20 20 73 71 C_MEM);. }. sq
892a0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 lite3_mutex_ente
892b0 72 28 6d 65 6d 33 2e 6d 75 74 65 78 29 3b 0a 7d r(mem3.mutex);.}
892c0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d .static void mem
892d0 73 79 73 33 4c 65 61 76 65 28 76 6f 69 64 29 7b sys3Leave(void){
892e0 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 . sqlite3_mutex
892f0 5f 6c 65 61 76 65 28 6d 65 6d 33 2e 6d 75 74 65 _leave(mem3.mute
89300 78 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c x);.}../*.** Cal
89310 6c 65 64 20 77 68 65 6e 20 77 65 20 61 72 65 20 led when we are
89320 75 6e 61 62 6c 65 20 74 6f 20 73 61 74 69 73 66 unable to satisf
89330 79 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 y an allocation
89340 6f 66 20 6e 42 79 74 65 73 2e 0a 2a 2f 0a 73 74 of nBytes..*/.st
89350 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 atic void memsys
89360 33 4f 75 74 4f 66 4d 65 6d 6f 72 79 28 69 6e 74 3OutOfMemory(int
89370 20 6e 42 79 74 65 29 7b 0a 20 20 69 66 28 20 21 nByte){. if( !
89380 6d 65 6d 33 2e 61 6c 61 72 6d 42 75 73 79 20 29 mem3.alarmBusy )
89390 7b 0a 20 20 20 20 6d 65 6d 33 2e 61 6c 61 72 6d {. mem3.alarm
893a0 42 75 73 79 20 3d 20 31 3b 0a 20 20 20 20 61 73 Busy = 1;. as
893b0 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
893c0 74 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 tex_held(mem3.mu
893d0 74 65 78 29 20 29 3b 0a 20 20 20 20 73 71 6c 69 tex) );. sqli
893e0 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
893f0 6d 65 6d 33 2e 6d 75 74 65 78 29 3b 0a 20 20 20 mem3.mutex);.
89400 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 sqlite3_release
89410 5f 6d 65 6d 6f 72 79 28 6e 42 79 74 65 29 3b 0a _memory(nByte);.
89420 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 sqlite3_mute
89430 78 5f 65 6e 74 65 72 28 6d 65 6d 33 2e 6d 75 74 x_enter(mem3.mut
89440 65 78 29 3b 0a 20 20 20 20 6d 65 6d 33 2e 61 6c ex);. mem3.al
89450 61 72 6d 42 75 73 79 20 3d 20 30 3b 0a 20 20 7d armBusy = 0;. }
89460 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 75 6e 6b .}.../*.** Chunk
89470 20 69 20 69 73 20 61 20 66 72 65 65 20 63 68 75 i is a free chu
89480 6e 6b 20 74 68 61 74 20 68 61 73 20 62 65 65 6e nk that has been
89490 20 75 6e 6c 69 6e 6b 65 64 2e 20 20 41 64 6a 75 unlinked. Adju
894a0 73 74 20 69 74 73 20 0a 2a 2a 20 73 69 7a 65 20 st its .** size
894b0 70 61 72 61 6d 65 74 65 72 73 20 66 6f 72 20 63 parameters for c
894c0 68 65 63 6b 2d 6f 75 74 20 61 6e 64 20 72 65 74 heck-out and ret
894d0 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f urn a pointer to
894e0 20 74 68 65 20 0a 2a 2a 20 75 73 65 72 20 70 6f the .** user po
894f0 72 74 69 6f 6e 20 6f 66 20 74 68 65 20 63 68 75 rtion of the chu
89500 6e 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f nk..*/.static vo
89510 69 64 20 2a 6d 65 6d 73 79 73 33 43 68 65 63 6b id *memsys3Check
89520 6f 75 74 28 75 33 32 20 69 2c 20 75 33 32 20 6e out(u32 i, u32 n
89530 42 6c 6f 63 6b 29 7b 0a 20 20 75 33 32 20 78 3b Block){. u32 x;
89540 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
89550 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 e3_mutex_held(me
89560 6d 33 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 m3.mutex) );. a
89570 73 73 65 72 74 28 20 69 3e 3d 31 20 29 3b 0a 20 ssert( i>=1 );.
89580 20 61 73 73 65 72 74 28 20 6d 65 6d 33 2e 61 50 assert( mem3.aP
89590 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 ool[i-1].u.hdr.s
895a0 69 7a 65 34 78 2f 34 3d 3d 6e 42 6c 6f 63 6b 20 ize4x/4==nBlock
895b0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6d 65 6d );. assert( mem
895c0 33 2e 61 50 6f 6f 6c 5b 69 2b 6e 42 6c 6f 63 6b 3.aPool[i+nBlock
895d0 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 -1].u.hdr.prevSi
895e0 7a 65 3d 3d 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 ze==nBlock );.
895f0 78 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 x = mem3.aPool[i
89600 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 -1].u.hdr.size4x
89610 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 ;. mem3.aPool[i
89620 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 -1].u.hdr.size4x
89630 20 3d 20 6e 42 6c 6f 63 6b 2a 34 20 7c 20 31 20 = nBlock*4 | 1
89640 7c 20 28 78 26 32 29 3b 0a 20 20 6d 65 6d 33 2e | (x&2);. mem3.
89650 61 50 6f 6f 6c 5b 69 2b 6e 42 6c 6f 63 6b 2d 31 aPool[i+nBlock-1
89660 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 ].u.hdr.prevSize
89670 20 3d 20 6e 42 6c 6f 63 6b 3b 0a 20 20 6d 65 6d = nBlock;. mem
89680 33 2e 61 50 6f 6f 6c 5b 69 2b 6e 42 6c 6f 63 6b 3.aPool[i+nBlock
89690 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 -1].u.hdr.size4x
896a0 20 7c 3d 20 32 3b 0a 20 20 72 65 74 75 72 6e 20 |= 2;. return
896b0 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 3b 0a &mem3.aPool[i];.
896c0 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 72 76 65 20 61 }../*.** Carve a
896d0 20 70 69 65 63 65 20 6f 66 66 20 6f 66 20 74 68 piece off of th
896e0 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6d 65 6d e end of the mem
896f0 33 2e 69 4d 61 73 74 65 72 20 66 72 65 65 20 63 3.iMaster free c
89700 68 75 6e 6b 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 hunk..** Return
89710 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
89720 20 6e 65 77 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e new allocation.
89730 20 20 4f 72 2c 20 69 66 20 74 68 65 20 6d 61 73 Or, if the mas
89740 74 65 72 20 63 68 75 6e 6b 0a 2a 2a 20 69 73 20 ter chunk.** is
89750 6e 6f 74 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 not large enough
89760 2c 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2f 0a 73 , return 0..*/.s
89770 74 61 74 69 63 20 76 6f 69 64 20 2a 6d 65 6d 73 tatic void *mems
89780 79 73 33 46 72 6f 6d 4d 61 73 74 65 72 28 75 33 ys3FromMaster(u3
89790 32 20 6e 42 6c 6f 63 6b 29 7b 0a 20 20 61 73 73 2 nBlock){. ass
897a0 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
897b0 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 ex_held(mem3.mut
897c0 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ex) );. assert(
897d0 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 3e 3d mem3.szMaster>=
897e0 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 69 66 28 20 nBlock );. if(
897f0 6e 42 6c 6f 63 6b 3e 3d 6d 65 6d 33 2e 73 7a 4d nBlock>=mem3.szM
89800 61 73 74 65 72 2d 31 20 29 7b 0a 20 20 20 20 2f aster-1 ){. /
89810 2a 20 55 73 65 20 74 68 65 20 65 6e 74 69 72 65 * Use the entire
89820 20 6d 61 73 74 65 72 20 2a 2f 0a 20 20 20 20 76 master */. v
89830 6f 69 64 20 2a 70 20 3d 20 6d 65 6d 73 79 73 33 oid *p = memsys3
89840 43 68 65 63 6b 6f 75 74 28 6d 65 6d 33 2e 69 4d Checkout(mem3.iM
89850 61 73 74 65 72 2c 20 6d 65 6d 33 2e 73 7a 4d 61 aster, mem3.szMa
89860 73 74 65 72 29 3b 0a 20 20 20 20 6d 65 6d 33 2e ster);. mem3.
89870 69 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 iMaster = 0;.
89880 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 20 3d mem3.szMaster =
89890 20 30 3b 0a 20 20 20 20 6d 65 6d 33 2e 6d 6e 4d 0;. mem3.mnM
898a0 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 72 aster = 0;. r
898b0 65 74 75 72 6e 20 70 3b 0a 20 20 7d 65 6c 73 65 eturn p;. }else
898c0 7b 0a 20 20 20 20 2f 2a 20 53 70 6c 69 74 20 74 {. /* Split t
898d0 68 65 20 6d 61 73 74 65 72 20 62 6c 6f 63 6b 2e he master block.
898e0 20 20 52 65 74 75 72 6e 20 74 68 65 20 74 61 69 Return the tai
898f0 6c 2e 20 2a 2f 0a 20 20 20 20 75 33 32 20 6e 65 l. */. u32 ne
89900 77 69 2c 20 78 3b 0a 20 20 20 20 6e 65 77 69 20 wi, x;. newi
89910 3d 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 2b = mem3.iMaster +
89920 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 20 2d mem3.szMaster -
89930 20 6e 42 6c 6f 63 6b 3b 0a 20 20 20 20 61 73 73 nBlock;. ass
89940 65 72 74 28 20 6e 65 77 69 20 3e 20 6d 65 6d 33 ert( newi > mem3
89950 2e 69 4d 61 73 74 65 72 2b 31 20 29 3b 0a 20 20 .iMaster+1 );.
89960 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d mem3.aPool[mem
89970 33 2e 69 4d 61 73 74 65 72 2b 6d 65 6d 33 2e 73 3.iMaster+mem3.s
89980 7a 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 zMaster-1].u.hdr
89990 2e 70 72 65 76 53 69 7a 65 20 3d 20 6e 42 6c 6f .prevSize = nBlo
899a0 63 6b 3b 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f ck;. mem3.aPo
899b0 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2b ol[mem3.iMaster+
899c0 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2d 31 5d mem3.szMaster-1]
899d0 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 7c 3d .u.hdr.size4x |=
899e0 20 32 3b 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f 2;. mem3.aPo
899f0 6f 6c 5b 6e 65 77 69 2d 31 5d 2e 75 2e 68 64 72 ol[newi-1].u.hdr
89a00 2e 73 69 7a 65 34 78 20 3d 20 6e 42 6c 6f 63 6b .size4x = nBlock
89a10 2a 34 20 2b 20 31 3b 0a 20 20 20 20 6d 65 6d 33 *4 + 1;. mem3
89a20 2e 73 7a 4d 61 73 74 65 72 20 2d 3d 20 6e 42 6c .szMaster -= nBl
89a30 6f 63 6b 3b 0a 20 20 20 20 6d 65 6d 33 2e 61 50 ock;. mem3.aP
89a40 6f 6f 6c 5b 6e 65 77 69 2d 31 5d 2e 75 2e 68 64 ool[newi-1].u.hd
89a50 72 2e 70 72 65 76 53 69 7a 65 20 3d 20 6d 65 6d r.prevSize = mem
89a60 33 2e 73 7a 4d 61 73 74 65 72 3b 0a 20 20 20 20 3.szMaster;.
89a70 78 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d x = mem3.aPool[m
89a80 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e 75 em3.iMaster-1].u
89a90 2e 68 64 72 2e 73 69 7a 65 34 78 20 26 20 32 3b .hdr.size4x & 2;
89aa0 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b . mem3.aPool[
89ab0 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e mem3.iMaster-1].
89ac0 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 3d 20 6d u.hdr.size4x = m
89ad0 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2a 34 20 7c em3.szMaster*4 |
89ae0 20 78 3b 0a 20 20 20 20 69 66 28 20 6d 65 6d 33 x;. if( mem3
89af0 2e 73 7a 4d 61 73 74 65 72 20 3c 20 6d 65 6d 33 .szMaster < mem3
89b00 2e 6d 6e 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 .mnMaster ){.
89b10 20 20 20 6d 65 6d 33 2e 6d 6e 4d 61 73 74 65 72 mem3.mnMaster
89b20 20 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 = mem3.szMaster
89b30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 ;. }. retu
89b40 72 6e 20 28 76 6f 69 64 2a 29 26 6d 65 6d 33 2e rn (void*)&mem3.
89b50 61 50 6f 6f 6c 5b 6e 65 77 69 5d 3b 0a 20 20 7d aPool[newi];. }
89b60 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 2a 70 52 6f 6f 74 .}../*.** *pRoot
89b70 20 69 73 20 74 68 65 20 68 65 61 64 20 6f 66 20 is the head of
89b80 61 20 6c 69 73 74 20 6f 66 20 66 72 65 65 20 63 a list of free c
89b90 68 75 6e 6b 73 20 6f 66 20 74 68 65 20 73 61 6d hunks of the sam
89ba0 65 20 73 69 7a 65 0a 2a 2a 20 6f 72 20 73 61 6d e size.** or sam
89bb0 65 20 73 69 7a 65 20 68 61 73 68 2e 20 20 49 6e e size hash. In
89bc0 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 2a 70 other words, *p
89bd0 52 6f 6f 74 20 69 73 20 61 6e 20 65 6e 74 72 79 Root is an entry
89be0 20 69 6e 20 65 69 74 68 65 72 0a 2a 2a 20 6d 65 in either.** me
89bf0 6d 33 2e 61 69 53 6d 61 6c 6c 5b 5d 20 6f 72 20 m3.aiSmall[] or
89c00 6d 65 6d 33 2e 61 69 48 61 73 68 5b 5d 2e 20 20 mem3.aiHash[].
89c10 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
89c20 69 6e 65 20 65 78 61 6d 69 6e 65 73 20 61 6c 6c ine examines all
89c30 20 65 6e 74 72 69 65 73 20 6f 6e 20 74 68 65 20 entries on the
89c40 67 69 76 65 6e 20 6c 69 73 74 20 61 6e 64 20 74 given list and t
89c50 72 69 65 73 0a 2a 2a 20 74 6f 20 63 6f 61 6c 65 ries.** to coale
89c60 73 63 65 20 65 61 63 68 20 65 6e 74 72 69 65 73 sce each entries
89c70 20 77 69 74 68 20 61 64 6a 61 63 65 6e 74 20 66 with adjacent f
89c80 72 65 65 20 63 68 75 6e 6b 73 2e 20 20 0a 2a 2a ree chunks. .**
89c90 0a 2a 2a 20 49 66 20 69 74 20 73 65 65 73 20 61 .** If it sees a
89ca0 20 63 68 75 6e 6b 20 74 68 61 74 20 69 73 20 6c chunk that is l
89cb0 61 72 67 65 72 20 74 68 61 6e 20 6d 65 6d 33 2e arger than mem3.
89cc0 69 4d 61 73 74 65 72 2c 20 69 74 20 72 65 70 6c iMaster, it repl
89cd0 61 63 65 73 20 0a 2a 2a 20 74 68 65 20 63 75 72 aces .** the cur
89ce0 72 65 6e 74 20 6d 65 6d 33 2e 69 4d 61 73 74 65 rent mem3.iMaste
89cf0 72 20 77 69 74 68 20 74 68 65 20 6e 65 77 20 6c r with the new l
89d00 61 72 67 65 72 20 63 68 75 6e 6b 2e 20 20 49 6e arger chunk. In
89d10 20 6f 72 64 65 72 20 66 6f 72 0a 2a 2a 20 74 68 order for.** th
89d20 69 73 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 is mem3.iMaster
89d30 72 65 70 6c 61 63 65 6d 65 6e 74 20 74 6f 20 77 replacement to w
89d40 6f 72 6b 2c 20 74 68 65 20 6d 61 73 74 65 72 20 ork, the master
89d50 63 68 75 6e 6b 20 6d 75 73 74 20 62 65 0a 2a 2a chunk must be.**
89d60 20 6c 69 6e 6b 65 64 20 69 6e 74 6f 20 74 68 65 linked into the
89d70 20 68 61 73 68 20 74 61 62 6c 65 73 2e 20 20 54 hash tables. T
89d80 68 61 74 20 69 73 20 6e 6f 74 20 74 68 65 20 6e hat is not the n
89d90 6f 72 6d 61 6c 20 73 74 61 74 65 20 6f 66 0a 2a ormal state of.*
89da0 2a 20 61 66 66 61 69 72 73 2c 20 6f 66 20 63 6f * affairs, of co
89db0 75 72 73 65 2e 20 20 54 68 65 20 63 61 6c 6c 69 urse. The calli
89dc0 6e 67 20 72 6f 75 74 69 6e 65 20 6d 75 73 74 20 ng routine must
89dd0 6c 69 6e 6b 20 74 68 65 20 6d 61 73 74 65 72 0a link the master.
89de0 2a 2a 20 63 68 75 6e 6b 20 62 65 66 6f 72 65 20 ** chunk before
89df0 69 6e 76 6f 6b 69 6e 67 20 74 68 69 73 20 72 6f invoking this ro
89e00 75 74 69 6e 65 2c 20 74 68 65 6e 20 6d 75 73 74 utine, then must
89e10 20 75 6e 6c 69 6e 6b 20 74 68 65 20 28 70 6f 73 unlink the (pos
89e20 73 69 62 6c 79 0a 2a 2a 20 63 68 61 6e 67 65 64 sibly.** changed
89e30 29 20 6d 61 73 74 65 72 20 63 68 75 6e 6b 20 6f ) master chunk o
89e40 6e 63 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 nce this routine
89e50 20 68 61 73 20 66 69 6e 69 73 68 65 64 2e 0a 2a has finished..*
89e60 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 /.static void me
89e70 6d 73 79 73 33 4d 65 72 67 65 28 75 33 32 20 2a msys3Merge(u32 *
89e80 70 52 6f 6f 74 29 7b 0a 20 20 75 33 32 20 69 4e pRoot){. u32 iN
89e90 65 78 74 2c 20 70 72 65 76 2c 20 73 69 7a 65 2c ext, prev, size,
89ea0 20 69 2c 20 78 3b 0a 0a 20 20 61 73 73 65 72 74 i, x;.. assert
89eb0 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
89ec0 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 held(mem3.mutex)
89ed0 20 29 3b 0a 20 20 66 6f 72 28 69 3d 2a 70 52 6f );. for(i=*pRo
89ee0 6f 74 3b 20 69 3e 30 3b 20 69 3d 69 4e 65 78 74 ot; i>0; i=iNext
89ef0 29 7b 0a 20 20 20 20 69 4e 65 78 74 20 3d 20 6d ){. iNext = m
89f00 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c em3.aPool[i].u.l
89f10 69 73 74 2e 6e 65 78 74 3b 0a 20 20 20 20 73 69 ist.next;. si
89f20 7a 65 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b ze = mem3.aPool[
89f30 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 i-1].u.hdr.size4
89f40 78 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 x;. assert( (
89f50 73 69 7a 65 26 31 29 3d 3d 30 20 29 3b 0a 20 20 size&1)==0 );.
89f60 20 20 69 66 28 20 28 73 69 7a 65 26 32 29 3d 3d if( (size&2)==
89f70 30 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 79 0 ){. memsy
89f80 73 33 55 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74 s3UnlinkFromList
89f90 28 69 2c 20 70 52 6f 6f 74 29 3b 0a 20 20 20 20 (i, pRoot);.
89fa0 20 20 61 73 73 65 72 74 28 20 69 20 3e 20 6d 65 assert( i > me
89fb0 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e m3.aPool[i-1].u.
89fc0 68 64 72 2e 70 72 65 76 53 69 7a 65 20 29 3b 0a hdr.prevSize );.
89fd0 20 20 20 20 20 20 70 72 65 76 20 3d 20 69 20 2d prev = i -
89fe0 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d mem3.aPool[i-1]
89ff0 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 3b .u.hdr.prevSize;
8a000 0a 20 20 20 20 20 20 69 66 28 20 70 72 65 76 3d . if( prev=
8a010 3d 69 4e 65 78 74 20 29 7b 0a 20 20 20 20 20 20 =iNext ){.
8a020 20 20 69 4e 65 78 74 20 3d 20 6d 65 6d 33 2e 61 iNext = mem3.a
8a030 50 6f 6f 6c 5b 70 72 65 76 5d 2e 75 2e 6c 69 73 Pool[prev].u.lis
8a040 74 2e 6e 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a t.next;. }.
8a050 20 20 20 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c memsys3Unl
8a060 69 6e 6b 28 70 72 65 76 29 3b 0a 20 20 20 20 20 ink(prev);.
8a070 20 73 69 7a 65 20 3d 20 69 20 2b 20 73 69 7a 65 size = i + size
8a080 2f 34 20 2d 20 70 72 65 76 3b 0a 20 20 20 20 20 /4 - prev;.
8a090 20 78 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b x = mem3.aPool[
8a0a0 70 72 65 76 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 prev-1].u.hdr.si
8a0b0 7a 65 34 78 20 26 20 32 3b 0a 20 20 20 20 20 20 ze4x & 2;.
8a0c0 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 70 72 65 76 2d mem3.aPool[prev-
8a0d0 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 1].u.hdr.size4x
8a0e0 3d 20 73 69 7a 65 2a 34 20 7c 20 78 3b 0a 20 20 = size*4 | x;.
8a0f0 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 70 mem3.aPool[p
8a100 72 65 76 2b 73 69 7a 65 2d 31 5d 2e 75 2e 68 64 rev+size-1].u.hd
8a110 72 2e 70 72 65 76 53 69 7a 65 20 3d 20 73 69 7a r.prevSize = siz
8a120 65 3b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 33 e;. memsys3
8a130 4c 69 6e 6b 28 70 72 65 76 29 3b 0a 20 20 20 20 Link(prev);.
8a140 20 20 69 20 3d 20 70 72 65 76 3b 0a 20 20 20 20 i = prev;.
8a150 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 69 7a }else{. siz
8a160 65 20 2f 3d 20 34 3b 0a 20 20 20 20 7d 0a 20 20 e /= 4;. }.
8a170 20 20 69 66 28 20 73 69 7a 65 3e 6d 65 6d 33 2e if( size>mem3.
8a180 73 7a 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20 szMaster ){.
8a190 20 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 3d mem3.iMaster =
8a1a0 20 69 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 73 i;. mem3.s
8a1b0 7a 4d 61 73 74 65 72 20 3d 20 73 69 7a 65 3b 0a zMaster = size;.
8a1c0 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a }. }.}../*.
8a1d0 2a 2a 20 52 65 74 75 72 6e 20 61 20 62 6c 6f 63 ** Return a bloc
8a1e0 6b 20 6f 66 20 6d 65 6d 6f 72 79 20 6f 66 20 61 k of memory of a
8a1f0 74 20 6c 65 61 73 74 20 6e 42 79 74 65 73 20 69 t least nBytes i
8a200 6e 20 73 69 7a 65 2e 0a 2a 2a 20 52 65 74 75 72 n size..** Retur
8a210 6e 20 4e 55 4c 4c 20 69 66 20 75 6e 61 62 6c 65 n NULL if unable
8a220 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e ..**.** This fun
8a230 63 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 ction assumes th
8a240 61 74 20 74 68 65 20 6e 65 63 65 73 73 61 72 79 at the necessary
8a250 20 6d 75 74 65 78 65 73 2c 20 69 66 20 61 6e 79 mutexes, if any
8a260 2c 20 61 72 65 0a 2a 2a 20 61 6c 72 65 61 64 79 , are.** already
8a270 20 68 65 6c 64 20 62 79 20 74 68 65 20 63 61 6c held by the cal
8a280 6c 65 72 2e 20 48 65 6e 63 65 20 22 55 6e 73 61 ler. Hence "Unsa
8a290 66 65 22 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 fe"..*/.static v
8a2a0 6f 69 64 20 2a 6d 65 6d 73 79 73 33 4d 61 6c 6c oid *memsys3Mall
8a2b0 6f 63 55 6e 73 61 66 65 28 69 6e 74 20 6e 42 79 ocUnsafe(int nBy
8a2c0 74 65 29 7b 0a 20 20 75 33 32 20 69 3b 0a 20 20 te){. u32 i;.
8a2d0 75 33 32 20 6e 42 6c 6f 63 6b 3b 0a 20 20 75 33 u32 nBlock;. u3
8a2e0 32 20 74 6f 46 72 65 65 3b 0a 0a 20 20 61 73 73 2 toFree;.. ass
8a2f0 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
8a300 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 ex_held(mem3.mut
8a310 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ex) );. assert(
8a320 20 73 69 7a 65 6f 66 28 4d 65 6d 33 42 6c 6f 63 sizeof(Mem3Bloc
8a330 6b 29 3d 3d 38 20 29 3b 0a 20 20 69 66 28 20 6e k)==8 );. if( n
8a340 42 79 74 65 3c 3d 31 32 20 29 7b 0a 20 20 20 20 Byte<=12 ){.
8a350 6e 42 6c 6f 63 6b 20 3d 20 32 3b 0a 20 20 7d 65 nBlock = 2;. }e
8a360 6c 73 65 7b 0a 20 20 20 20 6e 42 6c 6f 63 6b 20 lse{. nBlock
8a370 3d 20 28 6e 42 79 74 65 20 2b 20 31 31 29 2f 38 = (nByte + 11)/8
8a380 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 ;. }. assert(
8a390 6e 42 6c 6f 63 6b 3e 3d 32 20 29 3b 0a 0a 20 20 nBlock>=2 );..
8a3a0 2f 2a 20 53 54 45 50 20 31 3a 0a 20 20 2a 2a 20 /* STEP 1:. **
8a3b0 4c 6f 6f 6b 20 66 6f 72 20 61 6e 20 65 6e 74 72 Look for an entr
8a3c0 79 20 6f 66 20 74 68 65 20 63 6f 72 72 65 63 74 y of the correct
8a3d0 20 73 69 7a 65 20 69 6e 20 65 69 74 68 65 72 20 size in either
8a3e0 74 68 65 20 73 6d 61 6c 6c 0a 20 20 2a 2a 20 63 the small. ** c
8a3f0 68 75 6e 6b 20 74 61 62 6c 65 20 6f 72 20 69 6e hunk table or in
8a400 20 74 68 65 20 6c 61 72 67 65 20 63 68 75 6e 6b the large chunk
8a410 20 68 61 73 68 20 74 61 62 6c 65 2e 20 20 54 68 hash table. Th
8a420 69 73 20 69 73 0a 20 20 2a 2a 20 73 75 63 63 65 is is. ** succe
8a430 73 73 66 75 6c 20 6d 6f 73 74 20 6f 66 20 74 68 ssful most of th
8a440 65 20 74 69 6d 65 20 28 61 62 6f 75 74 20 39 20 e time (about 9
8a450 74 69 6d 65 73 20 6f 75 74 20 6f 66 20 31 30 29 times out of 10)
8a460 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e 42 6c .. */. if( nBl
8a470 6f 63 6b 20 3c 3d 20 4d 58 5f 53 4d 41 4c 4c 20 ock <= MX_SMALL
8a480 29 7b 0a 20 20 20 20 69 20 3d 20 6d 65 6d 33 2e ){. i = mem3.
8a490 61 69 53 6d 61 6c 6c 5b 6e 42 6c 6f 63 6b 2d 32 aiSmall[nBlock-2
8a4a0 5d 3b 0a 20 20 20 20 69 66 28 20 69 3e 30 20 29 ];. if( i>0 )
8a4b0 7b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 33 55 {. memsys3U
8a4c0 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74 28 69 2c nlinkFromList(i,
8a4d0 20 26 6d 65 6d 33 2e 61 69 53 6d 61 6c 6c 5b 6e &mem3.aiSmall[n
8a4e0 42 6c 6f 63 6b 2d 32 5d 29 3b 0a 20 20 20 20 20 Block-2]);.
8a4f0 20 72 65 74 75 72 6e 20 6d 65 6d 73 79 73 33 43 return memsys3C
8a500 68 65 63 6b 6f 75 74 28 69 2c 20 6e 42 6c 6f 63 heckout(i, nBloc
8a510 6b 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 k);. }. }els
8a520 65 7b 0a 20 20 20 20 69 6e 74 20 68 61 73 68 20 e{. int hash
8a530 3d 20 6e 42 6c 6f 63 6b 20 25 20 4e 5f 48 41 53 = nBlock % N_HAS
8a540 48 3b 0a 20 20 20 20 66 6f 72 28 69 3d 6d 65 6d H;. for(i=mem
8a550 33 2e 61 69 48 61 73 68 5b 68 61 73 68 5d 3b 20 3.aiHash[hash];
8a560 69 3e 30 3b 20 69 3d 6d 65 6d 33 2e 61 50 6f 6f i>0; i=mem3.aPoo
8a570 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 l[i].u.list.next
8a580 29 7b 0a 20 20 20 20 20 20 69 66 28 20 6d 65 6d ){. if( mem
8a590 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 3.aPool[i-1].u.h
8a5a0 64 72 2e 73 69 7a 65 34 78 2f 34 3d 3d 6e 42 6c dr.size4x/4==nBl
8a5b0 6f 63 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 6d ock ){. m
8a5c0 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 46 72 6f 6d emsys3UnlinkFrom
8a5d0 4c 69 73 74 28 69 2c 20 26 6d 65 6d 33 2e 61 69 List(i, &mem3.ai
8a5e0 48 61 73 68 5b 68 61 73 68 5d 29 3b 0a 20 20 20 Hash[hash]);.
8a5f0 20 20 20 20 20 72 65 74 75 72 6e 20 6d 65 6d 73 return mems
8a600 79 73 33 43 68 65 63 6b 6f 75 74 28 69 2c 20 6e ys3Checkout(i, n
8a610 42 6c 6f 63 6b 29 3b 0a 20 20 20 20 20 20 7d 0a Block);. }.
8a620 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 }. }.. /*
8a630 53 54 45 50 20 32 3a 0a 20 20 2a 2a 20 54 72 79 STEP 2:. ** Try
8a640 20 74 6f 20 73 61 74 69 73 66 79 20 74 68 65 20 to satisfy the
8a650 61 6c 6c 6f 63 61 74 69 6f 6e 20 62 79 20 63 61 allocation by ca
8a660 72 76 69 6e 67 20 61 20 70 69 65 63 65 20 6f 66 rving a piece of
8a670 66 20 6f 66 20 74 68 65 20 65 6e 64 0a 20 20 2a f of the end. *
8a680 2a 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72 20 * of the master
8a690 63 68 75 6e 6b 2e 20 20 54 68 69 73 20 73 74 65 chunk. This ste
8a6a0 70 20 75 73 75 61 6c 6c 79 20 77 6f 72 6b 73 20 p usually works
8a6b0 69 66 20 73 74 65 70 20 31 20 66 61 69 6c 73 2e if step 1 fails.
8a6c0 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6d 65 6d 33 . */. if( mem3
8a6d0 2e 73 7a 4d 61 73 74 65 72 3e 3d 6e 42 6c 6f 63 .szMaster>=nBloc
8a6e0 6b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 k ){. return
8a6f0 6d 65 6d 73 79 73 33 46 72 6f 6d 4d 61 73 74 65 memsys3FromMaste
8a700 72 28 6e 42 6c 6f 63 6b 29 3b 0a 20 20 7d 0a 0a r(nBlock);. }..
8a710 0a 20 20 2f 2a 20 53 54 45 50 20 33 3a 20 20 0a . /* STEP 3: .
8a720 20 20 2a 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67 ** Loop throug
8a730 68 20 74 68 65 20 65 6e 74 69 72 65 20 6d 65 6d h the entire mem
8a740 6f 72 79 20 70 6f 6f 6c 2e 20 20 43 6f 61 6c 65 ory pool. Coale
8a750 73 63 65 20 61 64 6a 61 63 65 6e 74 20 66 72 65 sce adjacent fre
8a760 65 0a 20 20 2a 2a 20 63 68 75 6e 6b 73 2e 20 20 e. ** chunks.
8a770 52 65 63 6f 6d 70 75 74 65 20 74 68 65 20 6d 61 Recompute the ma
8a780 73 74 65 72 20 63 68 75 6e 6b 20 61 73 20 74 68 ster chunk as th
8a790 65 20 6c 61 72 67 65 73 74 20 66 72 65 65 20 63 e largest free c
8a7a0 68 75 6e 6b 2e 0a 20 20 2a 2a 20 54 68 65 6e 20 hunk.. ** Then
8a7b0 74 72 79 20 61 67 61 69 6e 20 74 6f 20 73 61 74 try again to sat
8a7c0 69 73 66 79 20 74 68 65 20 61 6c 6c 6f 63 61 74 isfy the allocat
8a7d0 69 6f 6e 20 62 79 20 63 61 72 76 69 6e 67 20 61 ion by carving a
8a7e0 20 70 69 65 63 65 20 6f 66 66 0a 20 20 2a 2a 20 piece off. **
8a7f0 6f 66 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 of the end of th
8a800 65 20 6d 61 73 74 65 72 20 63 68 75 6e 6b 2e 20 e master chunk.
8a810 20 54 68 69 73 20 73 74 65 70 20 68 61 70 70 65 This step happe
8a820 6e 73 20 76 65 72 79 0a 20 20 2a 2a 20 72 61 72 ns very. ** rar
8a830 65 6c 79 20 28 77 65 20 68 6f 70 65 21 29 0a 20 ely (we hope!).
8a840 20 2a 2f 0a 20 20 66 6f 72 28 74 6f 46 72 65 65 */. for(toFree
8a850 3d 6e 42 6c 6f 63 6b 2a 31 36 3b 20 74 6f 46 72 =nBlock*16; toFr
8a860 65 65 3c 28 6d 65 6d 33 2e 6e 50 6f 6f 6c 2a 31 ee<(mem3.nPool*1
8a870 36 29 3b 20 74 6f 46 72 65 65 20 2a 3d 20 32 29 6); toFree *= 2)
8a880 7b 0a 20 20 20 20 6d 65 6d 73 79 73 33 4f 75 74 {. memsys3Out
8a890 4f 66 4d 65 6d 6f 72 79 28 74 6f 46 72 65 65 29 OfMemory(toFree)
8a8a0 3b 0a 20 20 20 20 69 66 28 20 6d 65 6d 33 2e 69 ;. if( mem3.i
8a8b0 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20 20 20 Master ){.
8a8c0 6d 65 6d 73 79 73 33 4c 69 6e 6b 28 6d 65 6d 33 memsys3Link(mem3
8a8d0 2e 69 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 .iMaster);.
8a8e0 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 3d 20 mem3.iMaster =
8a8f0 30 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 73 7a 0;. mem3.sz
8a900 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 Master = 0;.
8a910 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 }. for(i=0; i
8a920 3c 4e 5f 48 41 53 48 3b 20 69 2b 2b 29 7b 0a 20 <N_HASH; i++){.
8a930 20 20 20 20 20 6d 65 6d 73 79 73 33 4d 65 72 67 memsys3Merg
8a940 65 28 26 6d 65 6d 33 2e 61 69 48 61 73 68 5b 69 e(&mem3.aiHash[i
8a950 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f ]);. }. fo
8a960 72 28 69 3d 30 3b 20 69 3c 4d 58 5f 53 4d 41 4c r(i=0; i<MX_SMAL
8a970 4c 2d 31 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 L-1; i++){.
8a980 20 6d 65 6d 73 79 73 33 4d 65 72 67 65 28 26 6d memsys3Merge(&m
8a990 65 6d 33 2e 61 69 53 6d 61 6c 6c 5b 69 5d 29 3b em3.aiSmall[i]);
8a9a0 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6d . }. if( m
8a9b0 65 6d 33 2e 73 7a 4d 61 73 74 65 72 20 29 7b 0a em3.szMaster ){.
8a9c0 20 20 20 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c memsys3Unl
8a9d0 69 6e 6b 28 6d 65 6d 33 2e 69 4d 61 73 74 65 72 ink(mem3.iMaster
8a9e0 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6d 65 6d );. if( mem
8a9f0 33 2e 73 7a 4d 61 73 74 65 72 3e 3d 6e 42 6c 6f 3.szMaster>=nBlo
8aa00 63 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 ck ){. re
8aa10 74 75 72 6e 20 6d 65 6d 73 79 73 33 46 72 6f 6d turn memsys3From
8aa20 4d 61 73 74 65 72 28 6e 42 6c 6f 63 6b 29 3b 0a Master(nBlock);.
8aa30 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
8aa40 7d 0a 0a 20 20 2f 2a 20 49 66 20 6e 6f 6e 65 20 }.. /* If none
8aa50 6f 66 20 74 68 65 20 61 62 6f 76 65 20 77 6f 72 of the above wor
8aa60 6b 65 64 2c 20 74 68 65 6e 20 77 65 20 66 61 69 ked, then we fai
8aa70 6c 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 30 l. */. return 0
8aa80 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 ;.}../*.** Free
8aa90 61 6e 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 6d an outstanding m
8aaa0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
8aab0 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e ..**.** This fun
8aac0 63 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 ction assumes th
8aad0 61 74 20 74 68 65 20 6e 65 63 65 73 73 61 72 79 at the necessary
8aae0 20 6d 75 74 65 78 65 73 2c 20 69 66 20 61 6e 79 mutexes, if any
8aaf0 2c 20 61 72 65 0a 2a 2a 20 61 6c 72 65 61 64 79 , are.** already
8ab00 20 68 65 6c 64 20 62 79 20 74 68 65 20 63 61 6c held by the cal
8ab10 6c 65 72 2e 20 48 65 6e 63 65 20 22 55 6e 73 61 ler. Hence "Unsa
8ab20 66 65 22 2e 0a 2a 2f 0a 76 6f 69 64 20 6d 65 6d fe"..*/.void mem
8ab30 73 79 73 33 46 72 65 65 55 6e 73 61 66 65 28 76 sys3FreeUnsafe(v
8ab40 6f 69 64 20 2a 70 4f 6c 64 29 7b 0a 20 20 4d 65 oid *pOld){. Me
8ab50 6d 33 42 6c 6f 63 6b 20 2a 70 20 3d 20 28 4d 65 m3Block *p = (Me
8ab60 6d 33 42 6c 6f 63 6b 2a 29 70 4f 6c 64 3b 0a 20 m3Block*)pOld;.
8ab70 20 69 6e 74 20 69 3b 0a 20 20 75 33 32 20 73 69 int i;. u32 si
8ab80 7a 65 2c 20 78 3b 0a 20 20 61 73 73 65 72 74 28 ze, x;. assert(
8ab90 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
8aba0 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20 eld(mem3.mutex)
8abb0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 3e 6d );. assert( p>m
8abc0 65 6d 33 2e 61 50 6f 6f 6c 20 26 26 20 70 3c 26 em3.aPool && p<&
8abd0 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e mem3.aPool[mem3.
8abe0 6e 50 6f 6f 6c 5d 20 29 3b 0a 20 20 69 20 3d 20 nPool] );. i =
8abf0 70 20 2d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 3b 0a p - mem3.aPool;.
8ac00 20 20 61 73 73 65 72 74 28 20 28 6d 65 6d 33 2e assert( (mem3.
8ac10 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 aPool[i-1].u.hdr
8ac20 2e 73 69 7a 65 34 78 26 31 29 3d 3d 31 20 29 3b .size4x&1)==1 );
8ac30 0a 20 20 73 69 7a 65 20 3d 20 6d 65 6d 33 2e 61 . size = mem3.a
8ac40 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e Pool[i-1].u.hdr.
8ac50 73 69 7a 65 34 78 2f 34 3b 0a 20 20 61 73 73 65 size4x/4;. asse
8ac60 72 74 28 20 69 2b 73 69 7a 65 3c 3d 6d 65 6d 33 rt( i+size<=mem3
8ac70 2e 6e 50 6f 6f 6c 2b 31 20 29 3b 0a 20 20 6d 65 .nPool+1 );. me
8ac80 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e m3.aPool[i-1].u.
8ac90 68 64 72 2e 73 69 7a 65 34 78 20 26 3d 20 7e 31 hdr.size4x &= ~1
8aca0 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 ;. mem3.aPool[i
8acb0 2b 73 69 7a 65 2d 31 5d 2e 75 2e 68 64 72 2e 70 +size-1].u.hdr.p
8acc0 72 65 76 53 69 7a 65 20 3d 20 73 69 7a 65 3b 0a revSize = size;.
8acd0 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2b 73 mem3.aPool[i+s
8ace0 69 7a 65 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a ize-1].u.hdr.siz
8acf0 65 34 78 20 26 3d 20 7e 32 3b 0a 20 20 6d 65 6d e4x &= ~2;. mem
8ad00 73 79 73 33 4c 69 6e 6b 28 69 29 3b 0a 0a 20 20 sys3Link(i);..
8ad10 2f 2a 20 54 72 79 20 74 6f 20 65 78 70 61 6e 64 /* Try to expand
8ad20 20 74 68 65 20 6d 61 73 74 65 72 20 75 73 69 6e the master usin
8ad30 67 20 74 68 65 20 6e 65 77 6c 79 20 66 72 65 65 g the newly free
8ad40 64 20 63 68 75 6e 6b 20 2a 2f 0a 20 20 69 66 28 d chunk */. if(
8ad50 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 29 7b mem3.iMaster ){
8ad60 0a 20 20 20 20 77 68 69 6c 65 28 20 28 6d 65 6d . while( (mem
8ad70 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 3.aPool[mem3.iMa
8ad80 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 ster-1].u.hdr.si
8ad90 7a 65 34 78 26 32 29 3d 3d 30 20 29 7b 0a 20 20 ze4x&2)==0 ){.
8ada0 20 20 20 20 73 69 7a 65 20 3d 20 6d 65 6d 33 2e size = mem3.
8adb0 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 aPool[mem3.iMast
8adc0 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 er-1].u.hdr.prev
8add0 53 69 7a 65 3b 0a 20 20 20 20 20 20 6d 65 6d 33 Size;. mem3
8ade0 2e 69 4d 61 73 74 65 72 20 2d 3d 20 73 69 7a 65 .iMaster -= size
8adf0 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 73 7a 4d ;. mem3.szM
8ae00 61 73 74 65 72 20 2b 3d 20 73 69 7a 65 3b 0a 20 aster += size;.
8ae10 20 20 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 memsys3Unli
8ae20 6e 6b 28 6d 65 6d 33 2e 69 4d 61 73 74 65 72 29 nk(mem3.iMaster)
8ae30 3b 0a 20 20 20 20 20 20 78 20 3d 20 6d 65 6d 33 ;. x = mem3
8ae40 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 .aPool[mem3.iMas
8ae50 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a ter-1].u.hdr.siz
8ae60 65 34 78 20 26 20 32 3b 0a 20 20 20 20 20 20 6d e4x & 2;. m
8ae70 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 em3.aPool[mem3.i
8ae80 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e Master-1].u.hdr.
8ae90 73 69 7a 65 34 78 20 3d 20 6d 65 6d 33 2e 73 7a size4x = mem3.sz
8aea0 4d 61 73 74 65 72 2a 34 20 7c 20 78 3b 0a 20 20 Master*4 | x;.
8aeb0 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d mem3.aPool[m
8aec0 65 6d 33 2e 69 4d 61 73 74 65 72 2b 6d 65 6d 33 em3.iMaster+mem3
8aed0 2e 73 7a 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 .szMaster-1].u.h
8aee0 64 72 2e 70 72 65 76 53 69 7a 65 20 3d 20 6d 65 dr.prevSize = me
8aef0 6d 33 2e 73 7a 4d 61 73 74 65 72 3b 0a 20 20 20 m3.szMaster;.
8af00 20 7d 0a 20 20 20 20 78 20 3d 20 6d 65 6d 33 2e }. x = mem3.
8af10 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 aPool[mem3.iMast
8af20 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 er-1].u.hdr.size
8af30 34 78 20 26 20 32 3b 0a 20 20 20 20 77 68 69 6c 4x & 2;. whil
8af40 65 28 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d e( (mem3.aPool[m
8af50 65 6d 33 2e 69 4d 61 73 74 65 72 2b 6d 65 6d 33 em3.iMaster+mem3
8af60 2e 73 7a 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 .szMaster-1].u.h
8af70 64 72 2e 73 69 7a 65 34 78 26 31 29 3d 3d 30 20 dr.size4x&1)==0
8af80 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 33 ){. memsys3
8af90 55 6e 6c 69 6e 6b 28 6d 65 6d 33 2e 69 4d 61 73 Unlink(mem3.iMas
8afa0 74 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 ter+mem3.szMaste
8afb0 72 29 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 73 r);. mem3.s
8afc0 7a 4d 61 73 74 65 72 20 2b 3d 20 6d 65 6d 33 2e zMaster += mem3.
8afd0 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 aPool[mem3.iMast
8afe0 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 er+mem3.szMaster
8aff0 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 -1].u.hdr.size4x
8b000 2f 34 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 61 /4;. mem3.a
8b010 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 Pool[mem3.iMaste
8b020 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 r-1].u.hdr.size4
8b030 78 20 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 x = mem3.szMaste
8b040 72 2a 34 20 7c 20 78 3b 0a 20 20 20 20 20 20 6d r*4 | x;. m
8b050 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 em3.aPool[mem3.i
8b060 4d 61 73 74 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61 Master+mem3.szMa
8b070 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 ster-1].u.hdr.pr
8b080 65 76 53 69 7a 65 20 3d 20 6d 65 6d 33 2e 73 7a evSize = mem3.sz
8b090 4d 61 73 74 65 72 3b 0a 20 20 20 20 7d 0a 20 20 Master;. }.
8b0a0 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 }.}../*.** Retur
8b0b0 6e 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 6e n the size of an
8b0c0 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 61 6c 6c outstanding all
8b0d0 6f 63 61 74 69 6f 6e 2c 20 69 6e 20 62 79 74 65 ocation, in byte
8b0e0 73 2e 20 20 54 68 65 0a 2a 2a 20 73 69 7a 65 20 s. The.** size
8b0f0 72 65 74 75 72 6e 65 64 20 6f 6d 69 74 73 20 74 returned omits t
8b100 68 65 20 38 2d 62 79 74 65 20 68 65 61 64 65 72 he 8-byte header
8b110 20 6f 76 65 72 68 65 61 64 2e 20 20 54 68 69 73 overhead. This
8b120 20 6f 6e 6c 79 0a 2a 2a 20 77 6f 72 6b 73 20 66 only.** works f
8b130 6f 72 20 63 68 75 6e 6b 73 20 74 68 61 74 20 61 or chunks that a
8b140 72 65 20 63 75 72 72 65 6e 74 6c 79 20 63 68 65 re currently che
8b150 63 6b 65 64 20 6f 75 74 2e 0a 2a 2f 0a 73 74 61 cked out..*/.sta
8b160 74 69 63 20 69 6e 74 20 6d 65 6d 73 79 73 33 53 tic int memsys3S
8b170 69 7a 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 ize(void *p){.
8b180 4d 65 6d 33 42 6c 6f 63 6b 20 2a 70 42 6c 6f 63 Mem3Block *pBloc
8b190 6b 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 k;. if( p==0 )
8b1a0 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 42 6c 6f return 0;. pBlo
8b1b0 63 6b 20 3d 20 28 4d 65 6d 33 42 6c 6f 63 6b 2a ck = (Mem3Block*
8b1c0 29 70 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 )p;. assert( (p
8b1d0 42 6c 6f 63 6b 5b 2d 31 5d 2e 75 2e 68 64 72 2e Block[-1].u.hdr.
8b1e0 73 69 7a 65 34 78 26 31 29 21 3d 30 20 29 3b 0a size4x&1)!=0 );.
8b1f0 20 20 72 65 74 75 72 6e 20 28 70 42 6c 6f 63 6b return (pBlock
8b200 5b 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 [-1].u.hdr.size4
8b210 78 26 7e 33 29 2a 32 20 2d 20 34 3b 0a 7d 0a 0a x&~3)*2 - 4;.}..
8b220 2f 2a 0a 2a 2a 20 52 6f 75 6e 64 20 75 70 20 61 /*.** Round up a
8b230 20 72 65 71 75 65 73 74 20 73 69 7a 65 20 74 6f request size to
8b240 20 74 68 65 20 6e 65 78 74 20 76 61 6c 69 64 20 the next valid
8b250 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65 2e allocation size.
8b260 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d .*/.static int m
8b270 65 6d 73 79 73 33 52 6f 75 6e 64 75 70 28 69 6e emsys3Roundup(in
8b280 74 20 6e 29 7b 0a 20 20 69 66 28 20 6e 3c 3d 31 t n){. if( n<=1
8b290 32 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 2 ){. return
8b2a0 31 32 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 12;. }else{.
8b2b0 20 72 65 74 75 72 6e 20 28 28 6e 2b 31 31 29 26 return ((n+11)&
8b2c0 7e 37 29 20 2d 20 34 3b 0a 20 20 7d 0a 7d 0a 0a ~7) - 4;. }.}..
8b2d0 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6e /*.** Allocate n
8b2e0 42 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 2e Bytes of memory.
8b2f0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
8b300 2a 6d 65 6d 73 79 73 33 4d 61 6c 6c 6f 63 28 69 *memsys3Malloc(i
8b310 6e 74 20 6e 42 79 74 65 73 29 7b 0a 20 20 73 71 nt nBytes){. sq
8b320 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 3b 0a lite3_int64 *p;.
8b330 20 20 61 73 73 65 72 74 28 20 6e 42 79 74 65 73 assert( nBytes
8b340 3e 30 20 29 3b 20 20 20 20 20 20 20 20 20 20 2f >0 ); /
8b350 2a 20 6d 61 6c 6c 6f 63 2e 63 20 66 69 6c 74 65 * malloc.c filte
8b360 72 73 20 6f 75 74 20 30 20 62 79 74 65 20 72 65 rs out 0 byte re
8b370 71 75 65 73 74 73 20 2a 2f 0a 20 20 6d 65 6d 73 quests */. mems
8b380 79 73 33 45 6e 74 65 72 28 29 3b 0a 20 20 70 20 ys3Enter();. p
8b390 3d 20 6d 65 6d 73 79 73 33 4d 61 6c 6c 6f 63 55 = memsys3MallocU
8b3a0 6e 73 61 66 65 28 6e 42 79 74 65 73 29 3b 0a 20 nsafe(nBytes);.
8b3b0 20 6d 65 6d 73 79 73 33 4c 65 61 76 65 28 29 3b memsys3Leave();
8b3c0 0a 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 2a . return (void*
8b3d0 29 70 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 )p; .}../*.** Fr
8b3e0 65 65 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 76 6f ee memory..*/.vo
8b3f0 69 64 20 6d 65 6d 73 79 73 33 46 72 65 65 28 76 id memsys3Free(v
8b400 6f 69 64 20 2a 70 50 72 69 6f 72 29 7b 0a 20 20 oid *pPrior){.
8b410 61 73 73 65 72 74 28 20 70 50 72 69 6f 72 20 29 assert( pPrior )
8b420 3b 0a 20 20 6d 65 6d 73 79 73 33 45 6e 74 65 72 ;. memsys3Enter
8b430 28 29 3b 0a 20 20 6d 65 6d 73 79 73 33 46 72 65 ();. memsys3Fre
8b440 65 55 6e 73 61 66 65 28 70 50 72 69 6f 72 29 3b eUnsafe(pPrior);
8b450 0a 20 20 6d 65 6d 73 79 73 33 4c 65 61 76 65 28 . memsys3Leave(
8b460 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e );.}../*.** Chan
8b470 67 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 ge the size of a
8b480 6e 20 65 78 69 73 74 69 6e 67 20 6d 65 6d 6f 72 n existing memor
8b490 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2f 0a y allocation.*/.
8b4a0 76 6f 69 64 20 2a 6d 65 6d 73 79 73 33 52 65 61 void *memsys3Rea
8b4b0 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 50 72 69 6f lloc(void *pPrio
8b4c0 72 2c 20 69 6e 74 20 6e 42 79 74 65 73 29 7b 0a r, int nBytes){.
8b4d0 20 20 69 6e 74 20 6e 4f 6c 64 3b 0a 20 20 76 6f int nOld;. vo
8b4e0 69 64 20 2a 70 3b 0a 20 20 69 66 28 20 70 50 72 id *p;. if( pPr
8b4f0 69 6f 72 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 ior==0 ){. re
8b500 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 6d 61 6c turn sqlite3_mal
8b510 6c 6f 63 28 6e 42 79 74 65 73 29 3b 0a 20 20 7d loc(nBytes);. }
8b520 0a 20 20 69 66 28 20 6e 42 79 74 65 73 3c 3d 30 . if( nBytes<=0
8b530 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f ){. sqlite3_
8b540 66 72 65 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 free(pPrior);.
8b550 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a return 0;. }.
8b560 20 20 6e 4f 6c 64 20 3d 20 6d 65 6d 73 79 73 33 nOld = memsys3
8b570 53 69 7a 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 Size(pPrior);.
8b580 69 66 28 20 6e 42 79 74 65 73 3c 3d 6e 4f 6c 64 if( nBytes<=nOld
8b590 20 26 26 20 6e 42 79 74 65 73 3e 3d 6e 4f 6c 64 && nBytes>=nOld
8b5a0 2d 31 32 38 20 29 7b 0a 20 20 20 20 72 65 74 75 -128 ){. retu
8b5b0 72 6e 20 70 50 72 69 6f 72 3b 0a 20 20 7d 0a 20 rn pPrior;. }.
8b5c0 20 6d 65 6d 73 79 73 33 45 6e 74 65 72 28 29 3b memsys3Enter();
8b5d0 0a 20 20 70 20 3d 20 6d 65 6d 73 79 73 33 4d 61 . p = memsys3Ma
8b5e0 6c 6c 6f 63 55 6e 73 61 66 65 28 6e 42 79 74 65 llocUnsafe(nByte
8b5f0 73 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 s);. if( p ){.
8b600 20 20 20 69 66 28 20 6e 4f 6c 64 3c 6e 42 79 74 if( nOld<nByt
8b610 65 73 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 es ){. memc
8b620 70 79 28 70 2c 20 70 50 72 69 6f 72 2c 20 6e 4f py(p, pPrior, nO
8b630 6c 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a ld);. }else{.
8b640 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 2c 20 memcpy(p,
8b650 70 50 72 69 6f 72 2c 20 6e 42 79 74 65 73 29 3b pPrior, nBytes);
8b660 0a 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 73 79 . }. memsy
8b670 73 33 46 72 65 65 55 6e 73 61 66 65 28 70 50 72 s3FreeUnsafe(pPr
8b680 69 6f 72 29 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 ior);. }. mems
8b690 79 73 33 4c 65 61 76 65 28 29 3b 0a 20 20 72 65 ys3Leave();. re
8b6a0 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a turn p;.}../*.**
8b6b0 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 69 73 Initialize this
8b6c0 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 73 74 61 74 module..*/.stat
8b6d0 69 63 20 69 6e 74 20 6d 65 6d 73 79 73 33 49 6e ic int memsys3In
8b6e0 69 74 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 it(void *NotUsed
8b6f0 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 ){. UNUSED_PARA
8b700 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a METER(NotUsed);.
8b710 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 47 6c if( !sqlite3Gl
8b720 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 48 65 61 70 obalConfig.pHeap
8b730 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
8b740 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d QLITE_ERROR;. }
8b750 0a 0a 20 20 2f 2a 20 53 74 6f 72 65 20 61 20 70 .. /* Store a p
8b760 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6d 65 ointer to the me
8b770 6d 6f 72 79 20 62 6c 6f 63 6b 20 69 6e 20 67 6c mory block in gl
8b780 6f 62 61 6c 20 73 74 72 75 63 74 75 72 65 20 6d obal structure m
8b790 65 6d 33 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 em3. */. assert
8b7a0 28 20 73 69 7a 65 6f 66 28 4d 65 6d 33 42 6c 6f ( sizeof(Mem3Blo
8b7b0 63 6b 29 3d 3d 38 20 29 3b 0a 20 20 6d 65 6d 33 ck)==8 );. mem3
8b7c0 2e 61 50 6f 6f 6c 20 3d 20 28 4d 65 6d 33 42 6c .aPool = (Mem3Bl
8b7d0 6f 63 6b 20 2a 29 73 71 6c 69 74 65 33 47 6c 6f ock *)sqlite3Glo
8b7e0 62 61 6c 43 6f 6e 66 69 67 2e 70 48 65 61 70 3b balConfig.pHeap;
8b7f0 0a 20 20 6d 65 6d 33 2e 6e 50 6f 6f 6c 20 3d 20 . mem3.nPool =
8b800 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f (sqlite3GlobalCo
8b810 6e 66 69 67 2e 6e 48 65 61 70 20 2f 20 73 69 7a nfig.nHeap / siz
8b820 65 6f 66 28 4d 65 6d 33 42 6c 6f 63 6b 29 29 20 eof(Mem3Block))
8b830 2d 20 32 3b 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 - 2;.. /* Initi
8b840 61 6c 69 7a 65 20 74 68 65 20 6d 61 73 74 65 72 alize the master
8b850 20 62 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 6d 65 6d block. */. mem
8b860 33 2e 73 7a 4d 61 73 74 65 72 20 3d 20 6d 65 6d 3.szMaster = mem
8b870 33 2e 6e 50 6f 6f 6c 3b 0a 20 20 6d 65 6d 33 2e 3.nPool;. mem3.
8b880 6d 6e 4d 61 73 74 65 72 20 3d 20 6d 65 6d 33 2e mnMaster = mem3.
8b890 73 7a 4d 61 73 74 65 72 3b 0a 20 20 6d 65 6d 33 szMaster;. mem3
8b8a0 2e 69 4d 61 73 74 65 72 20 3d 20 31 3b 0a 20 20 .iMaster = 1;.
8b8b0 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 30 5d 2e 75 2e mem3.aPool[0].u.
8b8c0 68 64 72 2e 73 69 7a 65 34 78 20 3d 20 28 6d 65 hdr.size4x = (me
8b8d0 6d 33 2e 73 7a 4d 61 73 74 65 72 3c 3c 32 29 20 m3.szMaster<<2)
8b8e0 2b 20 32 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f + 2;. mem3.aPoo
8b8f0 6c 5b 6d 65 6d 33 2e 6e 50 6f 6f 6c 5d 2e 75 2e l[mem3.nPool].u.
8b900 68 64 72 2e 70 72 65 76 53 69 7a 65 20 3d 20 6d hdr.prevSize = m
8b910 65 6d 33 2e 6e 50 6f 6f 6c 3b 0a 20 20 6d 65 6d em3.nPool;. mem
8b920 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 6e 50 6f 3.aPool[mem3.nPo
8b930 6f 6c 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 ol].u.hdr.size4x
8b940 20 3d 20 31 3b 0a 0a 20 20 72 65 74 75 72 6e 20 = 1;.. return
8b950 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a SQLITE_OK;.}../*
8b960 0a 2a 2a 20 44 65 69 6e 69 74 69 61 6c 69 7a 65 .** Deinitialize
8b970 20 74 68 69 73 20 6d 6f 64 75 6c 65 2e 0a 2a 2f this module..*/
8b980 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d .static void mem
8b990 73 79 73 33 53 68 75 74 64 6f 77 6e 28 76 6f 69 sys3Shutdown(voi
8b9a0 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55 d *NotUsed){. U
8b9b0 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
8b9c0 4e 6f 74 55 73 65 64 29 3b 0a 20 20 6d 65 6d 33 NotUsed);. mem3
8b9d0 2e 6d 75 74 65 78 20 3d 20 30 3b 0a 20 20 72 65 .mutex = 0;. re
8b9e0 74 75 72 6e 3b 0a 7d 0a 0a 0a 0a 2f 2a 0a 2a 2a turn;.}..../*.**
8b9f0 20 4f 70 65 6e 20 74 68 65 20 66 69 6c 65 20 69 Open the file i
8ba00 6e 64 69 63 61 74 65 64 20 61 6e 64 20 77 72 69 ndicated and wri
8ba10 74 65 20 61 20 6c 6f 67 20 6f 66 20 61 6c 6c 20 te a log of all
8ba20 75 6e 66 72 65 65 64 20 6d 65 6d 6f 72 79 20 0a unfreed memory .
8ba30 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 69 ** allocations i
8ba40 6e 74 6f 20 74 68 61 74 20 6c 6f 67 2e 0a 2a 2f nto that log..*/
8ba50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
8ba60 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 73 void sqlite3Mems
8ba70 79 73 33 44 75 6d 70 28 63 6f 6e 73 74 20 63 68 ys3Dump(const ch
8ba80 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a ar *zFilename){.
8ba90 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 #ifdef SQLITE_DE
8baa0 42 55 47 0a 20 20 46 49 4c 45 20 2a 6f 75 74 3b BUG. FILE *out;
8bab0 0a 20 20 75 33 32 20 69 2c 20 6a 3b 0a 20 20 75 . u32 i, j;. u
8bac0 33 32 20 73 69 7a 65 3b 0a 20 20 69 66 28 20 7a 32 size;. if( z
8bad0 46 69 6c 65 6e 61 6d 65 3d 3d 30 20 7c 7c 20 7a Filename==0 || z
8bae0 46 69 6c 65 6e 61 6d 65 5b 30 5d 3d 3d 30 20 29 Filename[0]==0 )
8baf0 7b 0a 20 20 20 20 6f 75 74 20 3d 20 73 74 64 6f {. out = stdo
8bb00 75 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ut;. }else{.
8bb10 20 6f 75 74 20 3d 20 66 6f 70 65 6e 28 7a 46 69 out = fopen(zFi
8bb20 6c 65 6e 61 6d 65 2c 20 22 77 22 29 3b 0a 20 20 lename, "w");.
8bb30 20 20 69 66 28 20 6f 75 74 3d 3d 30 20 29 7b 0a if( out==0 ){.
8bb40 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 73 74 fprintf(st
8bb50 64 65 72 72 2c 20 22 2a 2a 20 55 6e 61 62 6c 65 derr, "** Unable
8bb60 20 74 6f 20 6f 75 74 70 75 74 20 6d 65 6d 6f 72 to output memor
8bb70 79 20 64 65 62 75 67 20 6f 75 74 70 75 74 20 6c y debug output l
8bb80 6f 67 3a 20 25 73 20 2a 2a 5c 6e 22 2c 0a 20 20 og: %s **\n",.
8bb90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
8bba0 20 20 20 20 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a zFilename);.
8bbb0 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 return;.
8bbc0 20 20 7d 0a 20 20 7d 0a 20 20 6d 65 6d 73 79 73 }. }. memsys
8bbd0 33 45 6e 74 65 72 28 29 3b 0a 20 20 66 70 72 69 3Enter();. fpri
8bbe0 6e 74 66 28 6f 75 74 2c 20 22 43 48 55 4e 4b 53 ntf(out, "CHUNKS
8bbf0 3a 5c 6e 22 29 3b 0a 20 20 66 6f 72 28 69 3d 31 :\n");. for(i=1
8bc00 3b 20 69 3c 3d 6d 65 6d 33 2e 6e 50 6f 6f 6c 3b ; i<=mem3.nPool;
8bc10 20 69 2b 3d 73 69 7a 65 2f 34 29 7b 0a 20 20 20 i+=size/4){.
8bc20 20 73 69 7a 65 20 3d 20 6d 65 6d 33 2e 61 50 6f size = mem3.aPo
8bc30 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 ol[i-1].u.hdr.si
8bc40 7a 65 34 78 3b 0a 20 20 20 20 69 66 28 20 73 69 ze4x;. if( si
8bc50 7a 65 2f 34 3c 3d 31 20 29 7b 0a 20 20 20 20 20 ze/4<=1 ){.
8bc60 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 25 fprintf(out, "%
8bc70 70 20 73 69 7a 65 20 65 72 72 6f 72 5c 6e 22 2c p size error\n",
8bc80 20 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 29 &mem3.aPool[i])
8bc90 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
8bca0 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 0 );. break
8bcb0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
8bcc0 28 73 69 7a 65 26 31 29 3d 3d 30 20 26 26 20 6d (size&1)==0 && m
8bcd0 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2b 73 69 7a 65 em3.aPool[i+size
8bce0 2f 34 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 /4-1].u.hdr.prev
8bcf0 53 69 7a 65 21 3d 73 69 7a 65 2f 34 20 29 7b 0a Size!=size/4 ){.
8bd00 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 fprintf(ou
8bd10 74 2c 20 22 25 70 20 74 61 69 6c 20 73 69 7a 65 t, "%p tail size
8bd20 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 5c does not match\
8bd30 6e 22 2c 20 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b n", &mem3.aPool[
8bd40 69 5d 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 i]);. asser
8bd50 74 28 20 30 20 29 3b 0a 20 20 20 20 20 20 62 72 t( 0 );. br
8bd60 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 eak;. }. i
8bd70 66 28 20 28 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b f( ((mem3.aPool[
8bd80 69 2b 73 69 7a 65 2f 34 2d 31 5d 2e 75 2e 68 64 i+size/4-1].u.hd
8bd90 72 2e 73 69 7a 65 34 78 26 32 29 3e 3e 31 29 21 r.size4x&2)>>1)!
8bda0 3d 28 73 69 7a 65 26 31 29 20 29 7b 0a 20 20 20 =(size&1) ){.
8bdb0 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 fprintf(out,
8bdc0 22 25 70 20 74 61 69 6c 20 63 68 65 63 6b 6f 75 "%p tail checkou
8bdd0 74 20 62 69 74 20 69 73 20 69 6e 63 6f 72 72 65 t bit is incorre
8bde0 63 74 5c 6e 22 2c 20 26 6d 65 6d 33 2e 61 50 6f ct\n", &mem3.aPo
8bdf0 6f 6c 5b 69 5d 29 3b 0a 20 20 20 20 20 20 61 73 ol[i]);. as
8be00 73 65 72 74 28 20 30 20 29 3b 0a 20 20 20 20 20 sert( 0 );.
8be10 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
8be20 20 20 69 66 28 20 73 69 7a 65 26 31 20 29 7b 0a if( size&1 ){.
8be30 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 fprintf(ou
8be40 74 2c 20 22 25 70 20 25 36 64 20 62 79 74 65 73 t, "%p %6d bytes
8be50 20 63 68 65 63 6b 65 64 20 6f 75 74 5c 6e 22 2c checked out\n",
8be60 20 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2c &mem3.aPool[i],
8be70 20 28 73 69 7a 65 2f 34 29 2a 38 2d 38 29 3b 0a (size/4)*8-8);.
8be80 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
8be90 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 25 fprintf(out, "%
8bea0 70 20 25 36 64 20 62 79 74 65 73 20 66 72 65 65 p %6d bytes free
8beb0 25 73 5c 6e 22 2c 20 26 6d 65 6d 33 2e 61 50 6f %s\n", &mem3.aPo
8bec0 6f 6c 5b 69 5d 2c 20 28 73 69 7a 65 2f 34 29 2a ol[i], (size/4)*
8bed0 38 2d 38 2c 0a 20 20 20 20 20 20 20 20 20 20 20 8-8,.
8bee0 20 20 20 20 20 20 20 69 3d 3d 6d 65 6d 33 2e 69 i==mem3.i
8bef0 4d 61 73 74 65 72 20 3f 20 22 20 2a 2a 6d 61 73 Master ? " **mas
8bf00 74 65 72 2a 2a 22 20 3a 20 22 22 29 3b 0a 20 20 ter**" : "");.
8bf10 20 20 7d 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d }. }. for(i=
8bf20 30 3b 20 69 3c 4d 58 5f 53 4d 41 4c 4c 2d 31 3b 0; i<MX_SMALL-1;
8bf30 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 6d i++){. if( m
8bf40 65 6d 33 2e 61 69 53 6d 61 6c 6c 5b 69 5d 3d 3d em3.aiSmall[i]==
8bf50 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 0 ) continue;.
8bf60 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 fprintf(out, "
8bf70 73 6d 61 6c 6c 28 25 32 64 29 3a 22 2c 20 69 29 small(%2d):", i)
8bf80 3b 0a 20 20 20 20 66 6f 72 28 6a 20 3d 20 6d 65 ;. for(j = me
8bf90 6d 33 2e 61 69 53 6d 61 6c 6c 5b 69 5d 3b 20 6a m3.aiSmall[i]; j
8bfa0 3e 30 3b 20 6a 3d 6d 65 6d 33 2e 61 50 6f 6f 6c >0; j=mem3.aPool
8bfb0 5b 6a 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 29 [j].u.list.next)
8bfc0 7b 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 {. fprintf(
8bfd0 6f 75 74 2c 20 22 20 25 70 28 25 64 29 22 2c 20 out, " %p(%d)",
8bfe0 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6a 5d 2c 0a &mem3.aPool[j],.
8bff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 6d (m
8c000 65 6d 33 2e 61 50 6f 6f 6c 5b 6a 2d 31 5d 2e 75 em3.aPool[j-1].u
8c010 2e 68 64 72 2e 73 69 7a 65 34 78 2f 34 29 2a 38 .hdr.size4x/4)*8
8c020 2d 38 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 -8);. }. f
8c030 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 5c 6e 22 printf(out, "\n"
8c040 29 3b 20 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d ); . }. for(i=
8c050 30 3b 20 69 3c 4e 5f 48 41 53 48 3b 20 69 2b 2b 0; i<N_HASH; i++
8c060 29 7b 0a 20 20 20 20 69 66 28 20 6d 65 6d 33 2e ){. if( mem3.
8c070 61 69 48 61 73 68 5b 69 5d 3d 3d 30 20 29 20 63 aiHash[i]==0 ) c
8c080 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 66 70 72 ontinue;. fpr
8c090 69 6e 74 66 28 6f 75 74 2c 20 22 68 61 73 68 28 intf(out, "hash(
8c0a0 25 32 64 29 3a 22 2c 20 69 29 3b 0a 20 20 20 20 %2d):", i);.
8c0b0 66 6f 72 28 6a 20 3d 20 6d 65 6d 33 2e 61 69 48 for(j = mem3.aiH
8c0c0 61 73 68 5b 69 5d 3b 20 6a 3e 30 3b 20 6a 3d 6d ash[i]; j>0; j=m
8c0d0 65 6d 33 2e 61 50 6f 6f 6c 5b 6a 5d 2e 75 2e 6c em3.aPool[j].u.l
8c0e0 69 73 74 2e 6e 65 78 74 29 7b 0a 20 20 20 20 20 ist.next){.
8c0f0 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 fprintf(out, "
8c100 25 70 28 25 64 29 22 2c 20 26 6d 65 6d 33 2e 61 %p(%d)", &mem3.a
8c110 50 6f 6f 6c 5b 6a 5d 2c 0a 20 20 20 20 20 20 20 Pool[j],.
8c120 20 20 20 20 20 20 20 28 6d 65 6d 33 2e 61 50 6f (mem3.aPo
8c130 6f 6c 5b 6a 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 ol[j-1].u.hdr.si
8c140 7a 65 34 78 2f 34 29 2a 38 2d 38 29 3b 0a 20 20 ze4x/4)*8-8);.
8c150 20 20 7d 0a 20 20 20 20 66 70 72 69 6e 74 66 28 }. fprintf(
8c160 6f 75 74 2c 20 22 5c 6e 22 29 3b 20 0a 20 20 7d out, "\n"); . }
8c170 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 . fprintf(out,
8c180 22 6d 61 73 74 65 72 3d 25 64 5c 6e 22 2c 20 6d "master=%d\n", m
8c190 65 6d 33 2e 69 4d 61 73 74 65 72 29 3b 0a 20 20 em3.iMaster);.
8c1a0 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 6e 6f fprintf(out, "no
8c1b0 77 55 73 65 64 3d 25 64 5c 6e 22 2c 20 6d 65 6d wUsed=%d\n", mem
8c1c0 33 2e 6e 50 6f 6f 6c 2a 38 20 2d 20 6d 65 6d 33 3.nPool*8 - mem3
8c1d0 2e 73 7a 4d 61 73 74 65 72 2a 38 29 3b 0a 20 20 .szMaster*8);.
8c1e0 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 6d 78 fprintf(out, "mx
8c1f0 55 73 65 64 3d 25 64 5c 6e 22 2c 20 6d 65 6d 33 Used=%d\n", mem3
8c200 2e 6e 50 6f 6f 6c 2a 38 20 2d 20 6d 65 6d 33 2e .nPool*8 - mem3.
8c210 6d 6e 4d 61 73 74 65 72 2a 38 29 3b 0a 20 20 73 mnMaster*8);. s
8c220 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
8c230 76 65 28 6d 65 6d 33 2e 6d 75 74 65 78 29 3b 0a ve(mem3.mutex);.
8c240 20 20 69 66 28 20 6f 75 74 3d 3d 73 74 64 6f 75 if( out==stdou
8c250 74 20 29 7b 0a 20 20 20 20 66 66 6c 75 73 68 28 t ){. fflush(
8c260 73 74 64 6f 75 74 29 3b 0a 20 20 7d 65 6c 73 65 stdout);. }else
8c270 7b 0a 20 20 20 20 66 63 6c 6f 73 65 28 6f 75 74 {. fclose(out
8c280 29 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 55 );. }.#else. U
8c290 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
8c2a0 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 23 65 6e 64 zFilename);.#end
8c2b0 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 if.}../*.** This
8c2c0 20 72 6f 75 74 69 6e 65 20 69 73 20 74 68 65 20 routine is the
8c2d0 6f 6e 6c 79 20 72 6f 75 74 69 6e 65 20 69 6e 20 only routine in
8c2e0 74 68 69 73 20 66 69 6c 65 20 77 69 74 68 20 65 this file with e
8c2f0 78 74 65 72 6e 61 6c 20 0a 2a 2a 20 6c 69 6e 6b xternal .** link
8c300 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 50 6f 70 75 6c age..**.** Popul
8c310 61 74 65 20 74 68 65 20 6c 6f 77 2d 6c 65 76 65 ate the low-leve
8c320 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 l memory allocat
8c330 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69 ion function poi
8c340 6e 74 65 72 73 20 69 6e 0a 2a 2a 20 73 71 6c 69 nters in.** sqli
8c350 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
8c360 6d 20 77 69 74 68 20 70 6f 69 6e 74 65 72 73 20 m with pointers
8c370 74 6f 20 74 68 65 20 72 6f 75 74 69 6e 65 73 20 to the routines
8c380 69 6e 20 74 68 69 73 20 66 69 6c 65 2e 20 54 68 in this file. Th
8c390 65 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 73 20 73 e.** arguments s
8c3a0 70 65 63 69 66 79 20 74 68 65 20 62 6c 6f 63 6b pecify the block
8c3b0 20 6f 66 20 6d 65 6d 6f 72 79 20 74 6f 20 6d 61 of memory to ma
8c3c0 6e 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 nage..**.** This
8c3d0 20 72 6f 75 74 69 6e 65 20 69 73 20 6f 6e 6c 79 routine is only
8c3e0 20 63 61 6c 6c 65 64 20 62 79 20 73 71 6c 69 74 called by sqlit
8c3f0 65 33 5f 63 6f 6e 66 69 67 28 29 2c 20 61 6e 64 e3_config(), and
8c400 20 74 68 65 72 65 66 6f 72 65 0a 2a 2a 20 69 73 therefore.** is
8c410 20 6e 6f 74 20 72 65 71 75 69 72 65 64 20 74 6f not required to
8c420 20 62 65 20 74 68 72 65 61 64 73 61 66 65 20 28 be threadsafe (
8c430 69 74 20 69 73 20 6e 6f 74 29 2e 0a 2a 2f 0a 53 it is not)..*/.S
8c440 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f QLITE_PRIVATE co
8c450 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f nst sqlite3_mem_
8c460 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 33 methods *sqlite3
8c470 4d 65 6d 47 65 74 4d 65 6d 73 79 73 33 28 76 6f MemGetMemsys3(vo
8c480 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f id){. static co
8c490 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f nst sqlite3_mem_
8c4a0 6d 65 74 68 6f 64 73 20 6d 65 6d 70 6f 6f 6c 4d methods mempoolM
8c4b0 65 74 68 6f 64 73 20 3d 20 7b 0a 20 20 20 20 20 ethods = {.
8c4c0 6d 65 6d 73 79 73 33 4d 61 6c 6c 6f 63 2c 0a 20 memsys3Malloc,.
8c4d0 20 20 20 20 6d 65 6d 73 79 73 33 46 72 65 65 2c memsys3Free,
8c4e0 0a 20 20 20 20 20 6d 65 6d 73 79 73 33 52 65 61 . memsys3Rea
8c4f0 6c 6c 6f 63 2c 0a 20 20 20 20 20 6d 65 6d 73 79 lloc,. memsy
8c500 73 33 53 69 7a 65 2c 0a 20 20 20 20 20 6d 65 6d s3Size,. mem
8c510 73 79 73 33 52 6f 75 6e 64 75 70 2c 0a 20 20 20 sys3Roundup,.
8c520 20 20 6d 65 6d 73 79 73 33 49 6e 69 74 2c 0a 20 memsys3Init,.
8c530 20 20 20 20 6d 65 6d 73 79 73 33 53 68 75 74 64 memsys3Shutd
8c540 6f 77 6e 2c 0a 20 20 20 20 20 30 0a 20 20 7d 3b own,. 0. };
8c550 0a 20 20 72 65 74 75 72 6e 20 26 6d 65 6d 70 6f . return &mempo
8c560 6f 6c 4d 65 74 68 6f 64 73 3b 0a 7d 0a 0a 23 65 olMethods;.}..#e
8c570 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 45 ndif /* SQLITE_E
8c580 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 33 20 2a 2f NABLE_MEMSYS3 */
8c590 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
8c5a0 2a 20 45 6e 64 20 6f 66 20 6d 65 6d 33 2e 63 20 * End of mem3.c
8c5b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8c5c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8c5d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8c5e0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
8c5f0 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 65 6d * Begin file mem
8c600 35 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 5.c ************
8c610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8c620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8c630 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 4f 63 74 /./*.** 2007 Oct
8c640 6f 62 65 72 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68 ober 14.**.** Th
8c650 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 e author disclai
8c660 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 ms copyright to
8c670 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 this source code
8c680 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a . In place of.*
8c690 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 * a legal notice
8c6a0 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 , here is a bles
8c6b0 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d sing:.**.** M
8c6c0 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 ay you do good a
8c6d0 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 nd not evil..**
8c6e0 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 May you find
8c6f0 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 forgiveness for
8c700 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 yourself and for
8c710 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 give others..**
8c720 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 May you share
8c730 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 freely, never t
8c740 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 aking more than
8c750 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a you give..**.***
8c760 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8c770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8c780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8c790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8c7a0 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 ******.** This f
8c7b0 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 ile contains the
8c7c0 20 43 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 C functions tha
8c7d0 74 20 69 6d 70 6c 65 6d 65 6e 74 20 61 20 6d 65 t implement a me
8c7e0 6d 6f 72 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 mory.** allocati
8c7f0 6f 6e 20 73 75 62 73 79 73 74 65 6d 20 66 6f 72 on subsystem for
8c800 20 75 73 65 20 62 79 20 53 51 4c 69 74 65 2e 20 use by SQLite.
8c810 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 .**.** This vers
8c820 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 ion of the memor
8c830 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 y allocation sub
8c840 73 79 73 74 65 6d 20 6f 6d 69 74 73 20 61 6c 6c system omits all
8c850 0a 2a 2a 20 75 73 65 20 6f 66 20 6d 61 6c 6c 6f .** use of mallo
8c860 63 28 29 2e 20 54 68 65 20 61 70 70 6c 69 63 61 c(). The applica
8c870 74 69 6f 6e 20 67 69 76 65 73 20 53 51 4c 69 74 tion gives SQLit
8c880 65 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d e a block of mem
8c890 6f 72 79 0a 2a 2a 20 62 65 66 6f 72 65 20 63 61 ory.** before ca
8c8a0 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 5f 69 6e lling sqlite3_in
8c8b0 69 74 69 61 6c 69 7a 65 28 29 20 66 72 6f 6d 20 itialize() from
8c8c0 77 68 69 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e which allocation
8c8d0 73 0a 2a 2a 20 61 72 65 20 6d 61 64 65 20 61 6e s.** are made an
8c8e0 64 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 d returned by th
8c8f0 65 20 78 4d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 e xMalloc() and
8c900 78 52 65 61 6c 6c 6f 63 28 29 20 0a 2a 2a 20 69 xRealloc() .** i
8c910 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 20 mplementations.
8c920 4f 6e 63 65 20 73 71 6c 69 74 65 33 5f 69 6e 69 Once sqlite3_ini
8c930 74 69 61 6c 69 7a 65 28 29 20 68 61 73 20 62 65 tialize() has be
8c940 65 6e 20 63 61 6c 6c 65 64 2c 0a 2a 2a 20 74 68 en called,.** th
8c950 65 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f e amount of memo
8c960 72 79 20 61 76 61 69 6c 61 62 6c 65 20 74 6f 20 ry available to
8c970 53 51 4c 69 74 65 20 69 73 20 66 69 78 65 64 20 SQLite is fixed
8c980 61 6e 64 20 63 61 6e 6e 6f 74 0a 2a 2a 20 62 65 and cannot.** be
8c990 20 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 changed..**.**
8c9a0 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 This version of
8c9b0 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 the memory alloc
8c9c0 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 20 ation subsystem
8c9d0 69 73 20 69 6e 63 6c 75 64 65 64 0a 2a 2a 20 69 is included.** i
8c9e0 6e 20 74 68 65 20 62 75 69 6c 64 20 6f 6e 6c 79 n the build only
8c9f0 20 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c if SQLITE_ENABL
8ca00 45 5f 4d 45 4d 53 59 53 35 20 69 73 20 64 65 66 E_MEMSYS5 is def
8ca10 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 ined..**.** This
8ca20 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f memory allocato
8ca30 72 20 75 73 65 73 20 74 68 65 20 66 6f 6c 6c 6f r uses the follo
8ca40 77 69 6e 67 20 61 6c 67 6f 72 69 74 68 6d 3a 0a wing algorithm:.
8ca50 2a 2a 0a 2a 2a 20 20 20 31 2e 20 20 41 6c 6c 20 **.** 1. All
8ca60 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
8ca70 6e 73 20 73 69 7a 65 73 20 61 72 65 20 72 6f 75 ns sizes are rou
8ca80 6e 64 65 64 20 75 70 20 74 6f 20 61 20 70 6f 77 nded up to a pow
8ca90 65 72 20 6f 66 20 32 2e 0a 2a 2a 0a 2a 2a 20 20 er of 2..**.**
8caa0 20 32 2e 20 20 49 66 20 74 77 6f 20 61 64 6a 61 2. If two adja
8cab0 63 65 6e 74 20 66 72 65 65 20 62 6c 6f 63 6b 73 cent free blocks
8cac0 20 61 72 65 20 74 68 65 20 68 61 6c 76 65 73 20 are the halves
8cad0 6f 66 20 61 20 6c 61 72 67 65 72 20 62 6c 6f 63 of a larger bloc
8cae0 6b 2c 0a 2a 2a 20 20 20 20 20 20 20 74 68 65 6e k,.** then
8caf0 20 74 68 65 20 74 77 6f 20 62 6c 6f 63 6b 73 20 the two blocks
8cb00 61 72 65 20 63 6f 61 6c 65 73 65 64 20 69 6e 74 are coalesed int
8cb10 6f 20 74 68 65 20 73 69 6e 67 6c 65 20 6c 61 72 o the single lar
8cb20 67 65 72 20 62 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a ger block..**.**
8cb30 20 20 20 33 2e 20 20 4e 65 77 20 6d 65 6d 6f 72 3. New memor
8cb40 79 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 y is allocated f
8cb50 72 6f 6d 20 74 68 65 20 66 69 72 73 74 20 61 76 rom the first av
8cb60 61 69 6c 61 62 6c 65 20 66 72 65 65 20 62 6c 6f ailable free blo
8cb70 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 61 ck..**.** This a
8cb80 6c 67 6f 72 69 74 68 6d 20 69 73 20 64 65 73 63 lgorithm is desc
8cb90 72 69 62 65 64 20 69 6e 3a 20 4a 2e 20 4d 2e 20 ribed in: J. M.
8cba0 52 6f 62 73 6f 6e 2e 20 22 42 6f 75 6e 64 73 20 Robson. "Bounds
8cbb0 66 6f 72 20 53 6f 6d 65 20 46 75 6e 63 74 69 6f for Some Functio
8cbc0 6e 73 0a 2a 2a 20 43 6f 6e 63 65 72 6e 69 6e 67 ns.** Concerning
8cbd0 20 44 79 6e 61 6d 69 63 20 53 74 6f 72 61 67 65 Dynamic Storage
8cbe0 20 41 6c 6c 6f 63 61 74 69 6f 6e 22 2e 20 4a 6f Allocation". Jo
8cbf0 75 72 6e 61 6c 20 6f 66 20 74 68 65 20 41 73 73 urnal of the Ass
8cc00 6f 63 69 61 74 69 6f 6e 20 66 6f 72 0a 2a 2a 20 ociation for.**
8cc10 43 6f 6d 70 75 74 69 6e 67 20 4d 61 63 68 69 6e Computing Machin
8cc20 65 72 79 2c 20 56 6f 6c 75 6d 65 20 32 31 2c 20 ery, Volume 21,
8cc30 4e 75 6d 62 65 72 20 38 2c 20 4a 75 6c 79 20 31 Number 8, July 1
8cc40 39 37 34 2c 20 70 61 67 65 73 20 34 39 31 2d 34 974, pages 491-4
8cc50 39 39 2e 0a 2a 2a 20 0a 2a 2a 20 4c 65 74 20 6e 99..** .** Let n
8cc60 20 62 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20 be the size of
8cc70 74 68 65 20 6c 61 72 67 65 73 74 20 61 6c 6c 6f the largest allo
8cc80 63 61 74 69 6f 6e 20 64 69 76 69 64 65 64 20 62 cation divided b
8cc90 79 20 74 68 65 20 6d 69 6e 69 6d 75 6d 0a 2a 2a y the minimum.**
8cca0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65 allocation size
8ccb0 20 28 61 66 74 65 72 20 72 6f 75 6e 64 69 6e 67 (after rounding
8ccc0 20 61 6c 6c 20 73 69 7a 65 73 20 75 70 20 74 6f all sizes up to
8ccd0 20 61 20 70 6f 77 65 72 20 6f 66 20 32 2e 29 20 a power of 2.)
8cce0 20 4c 65 74 20 4d 0a 2a 2a 20 62 65 20 74 68 65 Let M.** be the
8ccf0 20 6d 61 78 69 6d 75 6d 20 61 6d 6f 75 6e 74 20 maximum amount
8cd00 6f 66 20 6d 65 6d 6f 72 79 20 65 76 65 72 20 6f of memory ever o
8cd10 75 74 73 74 61 6e 64 69 6e 67 20 61 74 20 6f 6e utstanding at on
8cd20 65 20 74 69 6d 65 2e 20 20 4c 65 74 0a 2a 2a 20 e time. Let.**
8cd30 4e 20 62 65 20 74 68 65 20 74 6f 74 61 6c 20 61 N be the total a
8cd40 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 20 mount of memory
8cd50 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 61 6c available for al
8cd60 6c 6f 63 61 74 69 6f 6e 2e 20 20 52 6f 62 73 6f location. Robso
8cd70 6e 0a 2a 2a 20 70 72 6f 76 65 64 20 74 68 61 74 n.** proved that
8cd80 20 74 68 69 73 20 6d 65 6d 6f 72 79 20 61 6c 6c this memory all
8cd90 6f 63 61 74 6f 72 20 77 69 6c 6c 20 6e 65 76 65 ocator will neve
8cda0 72 20 62 72 65 61 6b 64 6f 77 6e 20 64 75 65 20 r breakdown due
8cdb0 74 6f 20 0a 2a 2a 20 66 72 61 67 6d 65 6e 74 61 to .** fragmenta
8cdc0 74 69 6f 6e 20 61 73 20 6c 6f 6e 67 20 61 73 20 tion as long as
8cdd0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f the following co
8cde0 6e 73 74 72 61 69 6e 74 20 68 6f 6c 64 73 3a 0a nstraint holds:.
8cdf0 2a 2a 0a 2a 2a 20 20 20 20 20 20 4e 20 3e 3d 20 **.** N >=
8ce00 20 4d 2a 28 31 20 2b 20 6c 6f 67 32 28 6e 29 2f M*(1 + log2(n)/
8ce10 32 29 20 2d 20 6e 20 2b 20 31 0a 2a 2a 0a 2a 2a 2) - n + 1.**.**
8ce20 20 54 68 65 20 73 71 6c 69 74 65 33 5f 73 74 61 The sqlite3_sta
8ce30 74 75 73 28 29 20 6c 6f 67 69 63 20 74 72 61 63 tus() logic trac
8ce40 6b 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 76 ks the maximum v
8ce50 61 6c 75 65 73 20 6f 66 20 6e 20 61 6e 64 20 4d alues of n and M
8ce60 20 73 6f 0a 2a 2a 20 74 68 61 74 20 61 6e 20 61 so.** that an a
8ce70 70 70 6c 69 63 61 74 69 6f 6e 20 63 61 6e 2c 20 pplication can,
8ce80 61 74 20 61 6e 79 20 74 69 6d 65 2c 20 76 65 72 at any time, ver
8ce90 69 66 79 20 74 68 69 73 20 63 6f 6e 73 74 72 61 ify this constra
8cea0 69 6e 74 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 int..*/../*.** T
8ceb0 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 his version of t
8cec0 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 he memory alloca
8ced0 74 6f 72 20 69 73 20 75 73 65 64 20 6f 6e 6c 79 tor is used only
8cee0 20 77 68 65 6e 20 0a 2a 2a 20 53 51 4c 49 54 45 when .** SQLITE
8cef0 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35 20 _ENABLE_MEMSYS5
8cf00 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 23 is defined..*/.#
8cf10 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 ifdef SQLITE_ENA
8cf20 42 4c 45 5f 4d 45 4d 53 59 53 35 0a 0a 2f 2a 0a BLE_MEMSYS5../*.
8cf30 2a 2a 20 41 20 6d 69 6e 69 6d 75 6d 20 61 6c 6c ** A minimum all
8cf40 6f 63 61 74 69 6f 6e 20 69 73 20 61 6e 20 69 6e ocation is an in
8cf50 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f stance of the fo
8cf60 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 llowing structur
8cf70 65 2e 0a 2a 2a 20 4c 61 72 67 65 72 20 61 6c 6c e..** Larger all
8cf80 6f 63 61 74 69 6f 6e 73 20 61 72 65 20 61 6e 20 ocations are an
8cf90 61 72 72 61 79 20 6f 66 20 74 68 65 73 65 20 73 array of these s
8cfa0 74 72 75 63 74 75 72 65 73 20 77 68 65 72 65 20 tructures where
8cfb0 74 68 65 0a 2a 2a 20 73 69 7a 65 20 6f 66 20 74 the.** size of t
8cfc0 68 65 20 61 72 72 61 79 20 69 73 20 61 20 70 6f he array is a po
8cfd0 77 65 72 20 6f 66 20 32 2e 0a 2a 2a 0a 2a 2a 20 wer of 2..**.**
8cfe0 54 68 65 20 73 69 7a 65 20 6f 66 20 74 68 69 73 The size of this
8cff0 20 6f 62 6a 65 63 74 20 6d 75 73 74 20 62 65 20 object must be
8d000 61 20 70 6f 77 65 72 20 6f 66 20 74 77 6f 2e 20 a power of two.
8d010 20 54 68 61 74 20 66 61 63 74 20 69 73 0a 2a 2a That fact is.**
8d020 20 76 65 72 69 66 69 65 64 20 69 6e 20 6d 65 6d verified in mem
8d030 73 79 73 35 49 6e 69 74 28 29 2e 0a 2a 2f 0a 74 sys5Init()..*/.t
8d040 79 70 65 64 65 66 20 73 74 72 75 63 74 20 4d 65 ypedef struct Me
8d050 6d 35 4c 69 6e 6b 20 4d 65 6d 35 4c 69 6e 6b 3b m5Link Mem5Link;
8d060 0a 73 74 72 75 63 74 20 4d 65 6d 35 4c 69 6e 6b .struct Mem5Link
8d070 20 7b 0a 20 20 69 6e 74 20 6e 65 78 74 3b 20 20 {. int next;
8d080 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 /* Index of
8d090 20 6e 65 78 74 20 66 72 65 65 20 63 68 75 6e 6b next free chunk
8d0a0 20 2a 2f 0a 20 20 69 6e 74 20 70 72 65 76 3b 20 */. int prev;
8d0b0 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f /* Index o
8d0c0 66 20 70 72 65 76 69 6f 75 73 20 66 72 65 65 20 f previous free
8d0d0 63 68 75 6e 6b 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a chunk */.};../*.
8d0e0 2a 2a 20 4d 61 78 69 6d 75 6d 20 73 69 7a 65 20 ** Maximum size
8d0f0 6f 66 20 61 6e 79 20 61 6c 6c 6f 63 61 74 69 6f of any allocatio
8d100 6e 20 69 73 20 28 28 31 3c 3c 4c 4f 47 4d 41 58 n is ((1<<LOGMAX
8d110 29 2a 6d 65 6d 35 2e 73 7a 41 74 6f 6d 29 2e 20 )*mem5.szAtom).
8d120 53 69 6e 63 65 0a 2a 2a 20 6d 65 6d 35 2e 73 7a Since.** mem5.sz
8d130 41 74 6f 6d 20 69 73 20 61 6c 77 61 79 73 20 61 Atom is always a
8d140 74 20 6c 65 61 73 74 20 38 20 61 6e 64 20 33 32 t least 8 and 32
8d150 2d 62 69 74 20 69 6e 74 65 67 65 72 73 20 61 72 -bit integers ar
8d160 65 20 75 73 65 64 2c 0a 2a 2a 20 69 74 20 69 73 e used,.** it is
8d170 20 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20 70 6f not actually po
8d180 73 73 69 62 6c 65 20 74 6f 20 72 65 61 63 68 20 ssible to reach
8d190 74 68 69 73 20 6c 69 6d 69 74 2e 0a 2a 2f 0a 23 this limit..*/.#
8d1a0 64 65 66 69 6e 65 20 4c 4f 47 4d 41 58 20 33 30 define LOGMAX 30
8d1b0 0a 0a 2f 2a 0a 2a 2a 20 4d 61 73 6b 73 20 75 73 ../*.** Masks us
8d1c0 65 64 20 66 6f 72 20 6d 65 6d 35 2e 61 43 74 72 ed for mem5.aCtr
8d1d0 6c 5b 5d 20 65 6c 65 6d 65 6e 74 73 2e 0a 2a 2f l[] elements..*/
8d1e0 0a 23 64 65 66 69 6e 65 20 43 54 52 4c 5f 4c 4f .#define CTRL_LO
8d1f0 47 53 49 5a 45 20 20 30 78 31 66 20 20 20 20 2f GSIZE 0x1f /
8d200 2a 20 4c 6f 67 32 20 53 69 7a 65 20 6f 66 20 74 * Log2 Size of t
8d210 68 69 73 20 62 6c 6f 63 6b 20 2a 2f 0a 23 64 65 his block */.#de
8d220 66 69 6e 65 20 43 54 52 4c 5f 46 52 45 45 20 20 fine CTRL_FREE
8d230 20 20 20 30 78 32 30 20 20 20 20 2f 2a 20 54 72 0x20 /* Tr
8d240 75 65 20 69 66 20 6e 6f 74 20 63 68 65 63 6b 65 ue if not checke
8d250 64 20 6f 75 74 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 d out */../*.**
8d260 41 6c 6c 20 6f 66 20 74 68 65 20 73 74 61 74 69 All of the stati
8d270 63 20 76 61 72 69 61 62 6c 65 73 20 75 73 65 64 c variables used
8d280 20 62 79 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 by this module
8d290 61 72 65 20 63 6f 6c 6c 65 63 74 65 64 0a 2a 2a are collected.**
8d2a0 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 73 into a single s
8d2b0 74 72 75 63 74 75 72 65 20 6e 61 6d 65 64 20 22 tructure named "
8d2c0 6d 65 6d 35 22 2e 20 20 54 68 69 73 20 69 73 20 mem5". This is
8d2d0 74 6f 20 6b 65 65 70 20 74 68 65 0a 2a 2a 20 73 to keep the.** s
8d2e0 74 61 74 69 63 20 76 61 72 69 61 62 6c 65 73 20 tatic variables
8d2f0 6f 72 67 61 6e 69 7a 65 64 20 61 6e 64 20 74 6f organized and to
8d300 20 72 65 64 75 63 65 20 6e 61 6d 65 73 70 61 63 reduce namespac
8d310 65 20 70 6f 6c 6c 75 74 69 6f 6e 0a 2a 2a 20 77 e pollution.** w
8d320 68 65 6e 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 hen this module
8d330 69 73 20 63 6f 6d 62 69 6e 65 64 20 77 69 74 68 is combined with
8d340 20 6f 74 68 65 72 20 69 6e 20 74 68 65 20 61 6d other in the am
8d350 61 6c 67 61 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 algamation..*/.s
8d360 74 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44 tatic SQLITE_WSD
8d370 20 73 74 72 75 63 74 20 4d 65 6d 35 47 6c 6f 62 struct Mem5Glob
8d380 61 6c 20 7b 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4d al {. /*. ** M
8d390 65 6d 6f 72 79 20 61 76 61 69 6c 61 62 6c 65 20 emory available
8d3a0 66 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 20 for allocation.
8d3b0 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 41 74 6f 6d */. int szAtom
8d3c0 3b 20 20 20 20 20 20 2f 2a 20 53 6d 61 6c 6c 65 ; /* Smalle
8d3d0 73 74 20 70 6f 73 73 69 62 6c 65 20 61 6c 6c 6f st possible allo
8d3e0 63 61 74 69 6f 6e 20 69 6e 20 62 79 74 65 73 20 cation in bytes
8d3f0 2a 2f 0a 20 20 69 6e 74 20 6e 42 6c 6f 63 6b 3b */. int nBlock;
8d400 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
8d410 6f 66 20 73 7a 41 74 6f 6d 20 73 69 7a 65 64 20 of szAtom sized
8d420 62 6c 6f 63 6b 73 20 69 6e 20 7a 50 6f 6f 6c 20 blocks in zPool
8d430 2a 2f 0a 20 20 75 38 20 2a 7a 50 6f 6f 6c 3b 20 */. u8 *zPool;
8d440 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 /* Memory
8d450 61 76 61 69 6c 61 62 6c 65 20 74 6f 20 62 65 20 available to be
8d460 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a 20 20 0a allocated */. .
8d470 20 20 2f 2a 0a 20 20 2a 2a 20 4d 75 74 65 78 20 /*. ** Mutex
8d480 74 6f 20 63 6f 6e 74 72 6f 6c 20 61 63 63 65 73 to control acces
8d490 73 20 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20 s to the memory
8d4a0 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 allocation subsy
8d4b0 73 74 65 6d 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c stem.. */. sql
8d4c0 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 ite3_mutex *mute
8d4d0 78 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 50 65 x;.. /*. ** Pe
8d4e0 72 66 6f 72 6d 61 6e 63 65 20 73 74 61 74 69 73 rformance statis
8d4f0 74 69 63 73 0a 20 20 2a 2f 0a 20 20 75 36 34 20 tics. */. u64
8d500 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20 20 nAlloc;
8d510 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 /* Total number
8d520 6f 66 20 63 61 6c 6c 73 20 74 6f 20 6d 61 6c 6c of calls to mall
8d530 6f 63 20 2a 2f 0a 20 20 75 36 34 20 74 6f 74 61 oc */. u64 tota
8d540 6c 41 6c 6c 6f 63 3b 20 20 20 20 20 2f 2a 20 54 lAlloc; /* T
8d550 6f 74 61 6c 20 6f 66 20 61 6c 6c 20 6d 61 6c 6c otal of all mall
8d560 6f 63 20 63 61 6c 6c 73 20 2d 20 69 6e 63 6c 75 oc calls - inclu
8d570 64 65 73 20 69 6e 74 65 72 6e 61 6c 20 66 72 61 des internal fra
8d580 67 20 2a 2f 0a 20 20 75 36 34 20 74 6f 74 61 6c g */. u64 total
8d590 45 78 63 65 73 73 3b 20 20 20 20 2f 2a 20 54 6f Excess; /* To
8d5a0 74 61 6c 20 69 6e 74 65 72 6e 61 6c 20 66 72 61 tal internal fra
8d5b0 67 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 20 gmentation */.
8d5c0 75 33 32 20 63 75 72 72 65 6e 74 4f 75 74 3b 20 u32 currentOut;
8d5d0 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 63 /* Current c
8d5e0 68 65 63 6b 6f 75 74 2c 20 69 6e 63 6c 75 64 69 heckout, includi
8d5f0 6e 67 20 69 6e 74 65 72 6e 61 6c 20 66 72 61 67 ng internal frag
8d600 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 20 75 mentation */. u
8d610 33 32 20 63 75 72 72 65 6e 74 43 6f 75 6e 74 3b 32 currentCount;
8d620 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 6e 75 /* Current nu
8d630 6d 62 65 72 20 6f 66 20 64 69 73 74 69 6e 63 74 mber of distinct
8d640 20 63 68 65 63 6b 6f 75 74 73 20 2a 2f 0a 20 20 checkouts */.
8d650 75 33 32 20 6d 61 78 4f 75 74 3b 20 20 20 20 20 u32 maxOut;
8d660 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 69 /* Maximum i
8d670 6e 73 74 61 6e 74 61 6e 65 6f 75 73 20 63 75 72 nstantaneous cur
8d680 72 65 6e 74 4f 75 74 20 2a 2f 0a 20 20 75 33 32 rentOut */. u32
8d690 20 6d 61 78 43 6f 75 6e 74 3b 20 20 20 20 20 20 maxCount;
8d6a0 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 69 6e 73 74 /* Maximum inst
8d6b0 61 6e 74 61 6e 65 6f 75 73 20 63 75 72 72 65 6e antaneous curren
8d6c0 74 43 6f 75 6e 74 20 2a 2f 0a 20 20 75 33 32 20 tCount */. u32
8d6d0 6d 61 78 52 65 71 75 65 73 74 3b 20 20 20 20 20 maxRequest;
8d6e0 2f 2a 20 4c 61 72 67 65 73 74 20 61 6c 6c 6f 63 /* Largest alloc
8d6f0 61 74 69 6f 6e 20 28 65 78 63 6c 75 73 69 76 65 ation (exclusive
8d700 20 6f 66 20 69 6e 74 65 72 6e 61 6c 20 66 72 61 of internal fra
8d710 67 29 20 2a 2f 0a 20 20 0a 20 20 2f 2a 0a 20 20 g) */. . /*.
8d720 2a 2a 20 4c 69 73 74 73 20 6f 66 20 66 72 65 65 ** Lists of free
8d730 20 62 6c 6f 63 6b 73 2e 20 20 61 69 46 72 65 65 blocks. aiFree
8d740 6c 69 73 74 5b 30 5d 20 69 73 20 61 20 6c 69 73 list[0] is a lis
8d750 74 20 6f 66 20 66 72 65 65 20 62 6c 6f 63 6b 73 t of free blocks
8d760 20 6f 66 0a 20 20 2a 2a 20 73 69 7a 65 20 6d 65 of. ** size me
8d770 6d 35 2e 73 7a 41 74 6f 6d 2e 20 20 61 69 46 72 m5.szAtom. aiFr
8d780 65 65 6c 69 73 74 5b 31 5d 20 68 6f 6c 64 73 20 eelist[1] holds
8d790 62 6c 6f 63 6b 73 20 6f 66 20 73 69 7a 65 20 73 blocks of size s
8d7a0 7a 41 74 6f 6d 2a 32 2e 0a 20 20 2a 2a 20 61 6e zAtom*2.. ** an
8d7b0 64 20 73 6f 20 66 6f 72 74 68 2e 0a 20 20 2a 2f d so forth.. */
8d7c0 0a 20 20 69 6e 74 20 61 69 46 72 65 65 6c 69 73 . int aiFreelis
8d7d0 74 5b 4c 4f 47 4d 41 58 2b 31 5d 3b 0a 0a 20 20 t[LOGMAX+1];..
8d7e0 2f 2a 0a 20 20 2a 2a 20 53 70 61 63 65 20 66 6f /*. ** Space fo
8d7f0 72 20 74 72 61 63 6b 69 6e 67 20 77 68 69 63 68 r tracking which
8d800 20 62 6c 6f 63 6b 73 20 61 72 65 20 63 68 65 63 blocks are chec
8d810 6b 65 64 20 6f 75 74 20 61 6e 64 20 74 68 65 20 ked out and the
8d820 73 69 7a 65 0a 20 20 2a 2a 20 6f 66 20 65 61 63 size. ** of eac
8d830 68 20 62 6c 6f 63 6b 2e 20 20 4f 6e 65 20 62 79 h block. One by
8d840 74 65 20 70 65 72 20 62 6c 6f 63 6b 2e 0a 20 20 te per block..
8d850 2a 2f 0a 20 20 75 38 20 2a 61 43 74 72 6c 3b 0a */. u8 *aCtrl;.
8d860 0a 7d 20 6d 65 6d 35 20 3d 20 7b 20 30 20 7d 3b .} mem5 = { 0 };
8d870 0a 0a 2f 2a 0a 2a 2a 20 41 63 63 65 73 73 20 74 ../*.** Access t
8d880 68 65 20 73 74 61 74 69 63 20 76 61 72 69 61 62 he static variab
8d890 6c 65 20 74 68 72 6f 75 67 68 20 61 20 6d 61 63 le through a mac
8d8a0 72 6f 20 66 6f 72 20 53 51 4c 49 54 45 5f 4f 4d ro for SQLITE_OM
8d8b0 49 54 5f 57 53 44 0a 2a 2f 0a 23 64 65 66 69 6e IT_WSD.*/.#defin
8d8c0 65 20 6d 65 6d 35 20 47 4c 4f 42 41 4c 28 73 74 e mem5 GLOBAL(st
8d8d0 72 75 63 74 20 4d 65 6d 35 47 6c 6f 62 61 6c 2c ruct Mem5Global,
8d8e0 20 6d 65 6d 35 29 0a 0a 2f 2a 0a 2a 2a 20 41 73 mem5)../*.** As
8d8f0 73 75 6d 69 6e 67 20 6d 65 6d 35 2e 7a 50 6f 6f suming mem5.zPoo
8d900 6c 20 69 73 20 64 69 76 69 64 65 64 20 75 70 20 l is divided up
8d910 69 6e 74 6f 20 61 6e 20 61 72 72 61 79 20 6f 66 into an array of
8d920 20 4d 65 6d 35 4c 69 6e 6b 0a 2a 2a 20 73 74 72 Mem5Link.** str
8d930 75 63 74 75 72 65 73 2c 20 72 65 74 75 72 6e 20 uctures, return
8d940 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
8d950 20 69 64 78 2d 74 68 20 73 75 63 68 20 6c 69 6b idx-th such lik
8d960 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d ..*/.#define MEM
8d970 35 4c 49 4e 4b 28 69 64 78 29 20 28 28 4d 65 6d 5LINK(idx) ((Mem
8d980 35 4c 69 6e 6b 20 2a 29 28 26 6d 65 6d 35 2e 7a 5Link *)(&mem5.z
8d990 50 6f 6f 6c 5b 28 69 64 78 29 2a 6d 65 6d 35 2e Pool[(idx)*mem5.
8d9a0 73 7a 41 74 6f 6d 5d 29 29 0a 0a 2f 2a 0a 2a 2a szAtom]))../*.**
8d9b0 20 55 6e 6c 69 6e 6b 20 74 68 65 20 63 68 75 6e Unlink the chun
8d9c0 6b 20 61 74 20 6d 65 6d 35 2e 61 50 6f 6f 6c 5b k at mem5.aPool[
8d9d0 69 5d 20 66 72 6f 6d 20 6c 69 73 74 20 69 74 20 i] from list it
8d9e0 69 73 20 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 is currently.**
8d9f0 6f 6e 2e 20 20 49 74 20 73 68 6f 75 6c 64 20 62 on. It should b
8da00 65 20 66 6f 75 6e 64 20 6f 6e 20 6d 65 6d 35 2e e found on mem5.
8da10 61 69 46 72 65 65 6c 69 73 74 5b 69 4c 6f 67 73 aiFreelist[iLogs
8da20 69 7a 65 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ize]..*/.static
8da30 76 6f 69 64 20 6d 65 6d 73 79 73 35 55 6e 6c 69 void memsys5Unli
8da40 6e 6b 28 69 6e 74 20 69 2c 20 69 6e 74 20 69 4c nk(int i, int iL
8da50 6f 67 73 69 7a 65 29 7b 0a 20 20 69 6e 74 20 6e ogsize){. int n
8da60 65 78 74 2c 20 70 72 65 76 3b 0a 20 20 61 73 73 ext, prev;. ass
8da70 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 6d ert( i>=0 && i<m
8da80 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 em5.nBlock );.
8da90 61 73 73 65 72 74 28 20 69 4c 6f 67 73 69 7a 65 assert( iLogsize
8daa0 3e 3d 30 20 26 26 20 69 4c 6f 67 73 69 7a 65 3c >=0 && iLogsize<
8dab0 3d 4c 4f 47 4d 41 58 20 29 3b 0a 20 20 61 73 73 =LOGMAX );. ass
8dac0 65 72 74 28 20 28 6d 65 6d 35 2e 61 43 74 72 6c ert( (mem5.aCtrl
8dad0 5b 69 5d 20 26 20 43 54 52 4c 5f 4c 4f 47 53 49 [i] & CTRL_LOGSI
8dae0 5a 45 29 3d 3d 69 4c 6f 67 73 69 7a 65 20 29 3b ZE)==iLogsize );
8daf0 0a 0a 20 20 6e 65 78 74 20 3d 20 4d 45 4d 35 4c .. next = MEM5L
8db00 49 4e 4b 28 69 29 2d 3e 6e 65 78 74 3b 0a 20 20 INK(i)->next;.
8db10 70 72 65 76 20 3d 20 4d 45 4d 35 4c 49 4e 4b 28 prev = MEM5LINK(
8db20 69 29 2d 3e 70 72 65 76 3b 0a 20 20 69 66 28 20 i)->prev;. if(
8db30 70 72 65 76 3c 30 20 29 7b 0a 20 20 20 20 6d 65 prev<0 ){. me
8db40 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 4c m5.aiFreelist[iL
8db50 6f 67 73 69 7a 65 5d 20 3d 20 6e 65 78 74 3b 0a ogsize] = next;.
8db60 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 4d 45 4d }else{. MEM
8db70 35 4c 49 4e 4b 28 70 72 65 76 29 2d 3e 6e 65 78 5LINK(prev)->nex
8db80 74 20 3d 20 6e 65 78 74 3b 0a 20 20 7d 0a 20 20 t = next;. }.
8db90 69 66 28 20 6e 65 78 74 3e 3d 30 20 29 7b 0a 20 if( next>=0 ){.
8dba0 20 20 20 4d 45 4d 35 4c 49 4e 4b 28 6e 65 78 74 MEM5LINK(next
8dbb0 29 2d 3e 70 72 65 76 20 3d 20 70 72 65 76 3b 0a )->prev = prev;.
8dbc0 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6e }.}../*.** Lin
8dbd0 6b 20 74 68 65 20 63 68 75 6e 6b 20 61 74 20 6d k the chunk at m
8dbe0 65 6d 35 2e 61 50 6f 6f 6c 5b 69 5d 20 73 6f 20 em5.aPool[i] so
8dbf0 74 68 61 74 20 69 73 20 6f 6e 20 74 68 65 20 69 that is on the i
8dc00 4c 6f 67 73 69 7a 65 0a 2a 2a 20 66 72 65 65 20 Logsize.** free
8dc10 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 list..*/.static
8dc20 76 6f 69 64 20 6d 65 6d 73 79 73 35 4c 69 6e 6b void memsys5Link
8dc30 28 69 6e 74 20 69 2c 20 69 6e 74 20 69 4c 6f 67 (int i, int iLog
8dc40 73 69 7a 65 29 7b 0a 20 20 69 6e 74 20 78 3b 0a size){. int x;.
8dc50 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
8dc60 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 3_mutex_held(mem
8dc70 35 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 5.mutex) );. as
8dc80 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c sert( i>=0 && i<
8dc90 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a 20 mem5.nBlock );.
8dca0 20 61 73 73 65 72 74 28 20 69 4c 6f 67 73 69 7a assert( iLogsiz
8dcb0 65 3e 3d 30 20 26 26 20 69 4c 6f 67 73 69 7a 65 e>=0 && iLogsize
8dcc0 3c 3d 4c 4f 47 4d 41 58 20 29 3b 0a 20 20 61 73 <=LOGMAX );. as
8dcd0 73 65 72 74 28 20 28 6d 65 6d 35 2e 61 43 74 72 sert( (mem5.aCtr
8dce0 6c 5b 69 5d 20 26 20 43 54 52 4c 5f 4c 4f 47 53 l[i] & CTRL_LOGS
8dcf0 49 5a 45 29 3d 3d 69 4c 6f 67 73 69 7a 65 20 29 IZE)==iLogsize )
8dd00 3b 0a 0a 20 20 78 20 3d 20 4d 45 4d 35 4c 49 4e ;.. x = MEM5LIN
8dd10 4b 28 69 29 2d 3e 6e 65 78 74 20 3d 20 6d 65 6d K(i)->next = mem
8dd20 35 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 4c 6f 5.aiFreelist[iLo
8dd30 67 73 69 7a 65 5d 3b 0a 20 20 4d 45 4d 35 4c 49 gsize];. MEM5LI
8dd40 4e 4b 28 69 29 2d 3e 70 72 65 76 20 3d 20 2d 31 NK(i)->prev = -1
8dd50 3b 0a 20 20 69 66 28 20 78 3e 3d 30 20 29 7b 0a ;. if( x>=0 ){.
8dd60 20 20 20 20 61 73 73 65 72 74 28 20 78 3c 6d 65 assert( x<me
8dd70 6d 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 20 m5.nBlock );.
8dd80 20 4d 45 4d 35 4c 49 4e 4b 28 78 29 2d 3e 70 72 MEM5LINK(x)->pr
8dd90 65 76 20 3d 20 69 3b 0a 20 20 7d 0a 20 20 6d 65 ev = i;. }. me
8dda0 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 4c m5.aiFreelist[iL
8ddb0 6f 67 73 69 7a 65 5d 20 3d 20 69 3b 0a 7d 0a 0a ogsize] = i;.}..
8ddc0 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 54 41 /*.** If the STA
8ddd0 54 49 43 5f 4d 45 4d 20 6d 75 74 65 78 20 69 73 TIC_MEM mutex is
8dde0 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 68 65 6c not already hel
8ddf0 64 2c 20 6f 62 74 61 69 6e 20 69 74 20 6e 6f 77 d, obtain it now
8de00 2e 20 54 68 65 20 6d 75 74 65 78 0a 2a 2a 20 77 . The mutex.** w
8de10 69 6c 6c 20 61 6c 72 65 61 64 79 20 62 65 20 68 ill already be h
8de20 65 6c 64 20 28 6f 62 74 61 69 6e 65 64 20 62 79 eld (obtained by
8de30 20 63 6f 64 65 20 69 6e 20 6d 61 6c 6c 6f 63 2e code in malloc.
8de40 63 29 20 69 66 0a 2a 2a 20 73 71 6c 69 74 65 33 c) if.** sqlite3
8de50 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 GlobalConfig.bMe
8de60 6d 53 74 61 74 20 69 73 20 74 72 75 65 2e 0a 2a mStat is true..*
8de70 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 /.static void me
8de80 6d 73 79 73 35 45 6e 74 65 72 28 76 6f 69 64 29 msys5Enter(void)
8de90 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 {. sqlite3_mute
8dea0 78 5f 65 6e 74 65 72 28 6d 65 6d 35 2e 6d 75 74 x_enter(mem5.mut
8deb0 65 78 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f ex);.}.static vo
8dec0 69 64 20 6d 65 6d 73 79 73 35 4c 65 61 76 65 28 id memsys5Leave(
8ded0 76 6f 69 64 29 7b 0a 20 20 73 71 6c 69 74 65 33 void){. sqlite3
8dee0 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d _mutex_leave(mem
8def0 35 2e 6d 75 74 65 78 29 3b 0a 7d 0a 0a 2f 2a 0a 5.mutex);.}../*.
8df00 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 69 ** Return the si
8df10 7a 65 20 6f 66 20 61 6e 20 6f 75 74 73 74 61 6e ze of an outstan
8df20 64 69 6e 67 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c ding allocation,
8df30 20 69 6e 20 62 79 74 65 73 2e 20 20 54 68 65 0a in bytes. The.
8df40 2a 2a 20 73 69 7a 65 20 72 65 74 75 72 6e 65 64 ** size returned
8df50 20 6f 6d 69 74 73 20 74 68 65 20 38 2d 62 79 74 omits the 8-byt
8df60 65 20 68 65 61 64 65 72 20 6f 76 65 72 68 65 61 e header overhea
8df70 64 2e 20 20 54 68 69 73 20 6f 6e 6c 79 0a 2a 2a d. This only.**
8df80 20 77 6f 72 6b 73 20 66 6f 72 20 63 68 75 6e 6b works for chunk
8df90 73 20 74 68 61 74 20 61 72 65 20 63 75 72 72 65 s that are curre
8dfa0 6e 74 6c 79 20 63 68 65 63 6b 65 64 20 6f 75 74 ntly checked out
8dfb0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
8dfc0 6d 65 6d 73 79 73 35 53 69 7a 65 28 76 6f 69 64 memsys5Size(void
8dfd0 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 53 69 7a *p){. int iSiz
8dfe0 65 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 20 29 e = 0;. if( p )
8dff0 7b 0a 20 20 20 20 69 6e 74 20 69 20 3d 20 28 28 {. int i = ((
8e000 75 38 20 2a 29 70 2d 6d 65 6d 35 2e 7a 50 6f 6f u8 *)p-mem5.zPoo
8e010 6c 29 2f 6d 65 6d 35 2e 73 7a 41 74 6f 6d 3b 0a l)/mem5.szAtom;.
8e020 20 20 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 assert( i>=0
8e030 20 26 26 20 69 3c 6d 65 6d 35 2e 6e 42 6c 6f 63 && i<mem5.nBloc
8e040 6b 20 29 3b 0a 20 20 20 20 69 53 69 7a 65 20 3d k );. iSize =
8e050 20 6d 65 6d 35 2e 73 7a 41 74 6f 6d 20 2a 20 28 mem5.szAtom * (
8e060 31 20 3c 3c 20 28 6d 65 6d 35 2e 61 43 74 72 6c 1 << (mem5.aCtrl
8e070 5b 69 5d 26 43 54 52 4c 5f 4c 4f 47 53 49 5a 45 [i]&CTRL_LOGSIZE
8e080 29 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e ));. }. return
8e090 20 69 53 69 7a 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a iSize;.}../*.**
8e0a0 20 46 69 6e 64 20 74 68 65 20 66 69 72 73 74 20 Find the first
8e0b0 65 6e 74 72 79 20 6f 6e 20 74 68 65 20 66 72 65 entry on the fre
8e0c0 65 6c 69 73 74 20 69 4c 6f 67 73 69 7a 65 2e 20 elist iLogsize.
8e0d0 20 55 6e 6c 69 6e 6b 20 74 68 61 74 0a 2a 2a 20 Unlink that.**
8e0e0 65 6e 74 72 79 20 61 6e 64 20 72 65 74 75 72 6e entry and return
8e0f0 20 69 74 73 20 69 6e 64 65 78 2e 20 0a 2a 2f 0a its index. .*/.
8e100 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 73 79 static int memsy
8e110 73 35 55 6e 6c 69 6e 6b 46 69 72 73 74 28 69 6e s5UnlinkFirst(in
8e120 74 20 69 4c 6f 67 73 69 7a 65 29 7b 0a 20 20 69 t iLogsize){. i
8e130 6e 74 20 69 3b 0a 20 20 69 6e 74 20 69 46 69 72 nt i;. int iFir
8e140 73 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 st;.. assert( i
8e150 4c 6f 67 73 69 7a 65 3e 3d 30 20 26 26 20 69 4c Logsize>=0 && iL
8e160 6f 67 73 69 7a 65 3c 3d 4c 4f 47 4d 41 58 20 29 ogsize<=LOGMAX )
8e170 3b 0a 20 20 69 20 3d 20 69 46 69 72 73 74 20 3d ;. i = iFirst =
8e180 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 mem5.aiFreelist
8e190 5b 69 4c 6f 67 73 69 7a 65 5d 3b 0a 20 20 61 73 [iLogsize];. as
8e1a0 73 65 72 74 28 20 69 46 69 72 73 74 3e 3d 30 20 sert( iFirst>=0
8e1b0 29 3b 0a 20 20 77 68 69 6c 65 28 20 69 3e 30 20 );. while( i>0
8e1c0 29 7b 0a 20 20 20 20 69 66 28 20 69 3c 69 46 69 ){. if( i<iFi
8e1d0 72 73 74 20 29 20 69 46 69 72 73 74 20 3d 20 69 rst ) iFirst = i
8e1e0 3b 0a 20 20 20 20 69 20 3d 20 4d 45 4d 35 4c 49 ;. i = MEM5LI
8e1f0 4e 4b 28 69 29 2d 3e 6e 65 78 74 3b 0a 20 20 7d NK(i)->next;. }
8e200 0a 20 20 6d 65 6d 73 79 73 35 55 6e 6c 69 6e 6b . memsys5Unlink
8e210 28 69 46 69 72 73 74 2c 20 69 4c 6f 67 73 69 7a (iFirst, iLogsiz
8e220 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 46 69 e);. return iFi
8e230 72 73 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 rst;.}../*.** Re
8e240 74 75 72 6e 20 61 20 62 6c 6f 63 6b 20 6f 66 20 turn a block of
8e250 6d 65 6d 6f 72 79 20 6f 66 20 61 74 20 6c 65 61 memory of at lea
8e260 73 74 20 6e 42 79 74 65 73 20 69 6e 20 73 69 7a st nBytes in siz
8e270 65 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 4e 55 4c e..** Return NUL
8e280 4c 20 69 66 20 75 6e 61 62 6c 65 2e 20 20 52 65 L if unable. Re
8e290 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 6e 42 79 turn NULL if nBy
8e2a0 74 65 73 3d 3d 30 2e 0a 2a 2a 0a 2a 2a 20 54 68 tes==0..**.** Th
8e2b0 65 20 63 61 6c 6c 65 72 20 67 75 61 72 61 6e 74 e caller guarant
8e2c0 65 65 73 20 74 68 61 74 20 6e 42 79 74 65 20 70 ees that nByte p
8e2d0 6f 73 69 74 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 54 ositive..**.** T
8e2e0 68 65 20 63 61 6c 6c 65 72 20 68 61 73 20 6f 62 he caller has ob
8e2f0 74 61 69 6e 65 64 20 61 20 6d 75 74 65 78 20 70 tained a mutex p
8e300 72 69 6f 72 20 74 6f 20 69 6e 76 6f 6b 69 6e 67 rior to invoking
8e310 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 this.** routine
8e320 20 73 6f 20 74 68 65 72 65 20 69 73 20 6e 65 76 so there is nev
8e330 65 72 20 61 6e 79 20 63 68 61 6e 63 65 20 74 68 er any chance th
8e340 61 74 20 74 77 6f 20 6f 72 20 6d 6f 72 65 0a 2a at two or more.*
8e350 2a 20 74 68 72 65 61 64 73 20 63 61 6e 20 62 65 * threads can be
8e360 20 69 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 in this routine
8e370 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d at the same tim
8e380 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 e..*/.static voi
8e390 64 20 2a 6d 65 6d 73 79 73 35 4d 61 6c 6c 6f 63 d *memsys5Malloc
8e3a0 55 6e 73 61 66 65 28 69 6e 74 20 6e 42 79 74 65 Unsafe(int nByte
8e3b0 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 ){. int i;
8e3c0 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f /* Index o
8e3d0 66 20 61 20 6d 65 6d 35 2e 61 50 6f 6f 6c 5b 5d f a mem5.aPool[]
8e3e0 20 73 6c 6f 74 20 2a 2f 0a 20 20 69 6e 74 20 69 slot */. int i
8e3f0 42 69 6e 3b 20 20 20 20 20 20 20 20 2f 2a 20 49 Bin; /* I
8e400 6e 64 65 78 20 69 6e 74 6f 20 6d 65 6d 35 2e 61 ndex into mem5.a
8e410 69 46 72 65 65 6c 69 73 74 5b 5d 20 2a 2f 0a 20 iFreelist[] */.
8e420 20 69 6e 74 20 69 46 75 6c 6c 53 7a 3b 20 20 20 int iFullSz;
8e430 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 61 6c 6c /* Size of all
8e440 6f 63 61 74 69 6f 6e 20 72 6f 75 6e 64 65 64 20 ocation rounded
8e450 75 70 20 74 6f 20 70 6f 77 65 72 20 6f 66 20 32 up to power of 2
8e460 20 2a 2f 0a 20 20 69 6e 74 20 69 4c 6f 67 73 69 */. int iLogsi
8e470 7a 65 3b 20 20 20 20 2f 2a 20 4c 6f 67 32 20 6f ze; /* Log2 o
8e480 66 20 69 46 75 6c 6c 53 7a 2f 50 4f 57 32 5f 4d f iFullSz/POW2_M
8e490 49 4e 20 2a 2f 0a 0a 20 20 2f 2a 20 6e 42 79 74 IN */.. /* nByt
8e4a0 65 20 6d 75 73 74 20 62 65 20 61 20 70 6f 73 69 e must be a posi
8e4b0 74 69 76 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 tive */. assert
8e4c0 28 20 6e 42 79 74 65 3e 30 20 29 3b 0a 0a 20 20 ( nByte>0 );..
8e4d0 2f 2a 20 4b 65 65 70 20 74 72 61 63 6b 20 6f 66 /* Keep track of
8e4e0 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 61 6c 6c the maximum all
8e4f0 6f 63 61 74 69 6f 6e 20 72 65 71 75 65 73 74 2e ocation request.
8e500 20 20 45 76 65 6e 20 75 6e 66 75 6c 66 69 6c 6c Even unfulfill
8e510 65 64 0a 20 20 2a 2a 20 72 65 71 75 65 73 74 73 ed. ** requests
8e520 20 61 72 65 20 63 6f 75 6e 74 65 64 20 2a 2f 0a are counted */.
8e530 20 20 69 66 28 20 28 75 33 32 29 6e 42 79 74 65 if( (u32)nByte
8e540 3e 6d 65 6d 35 2e 6d 61 78 52 65 71 75 65 73 74 >mem5.maxRequest
8e550 20 29 7b 0a 20 20 20 20 6d 65 6d 35 2e 6d 61 78 ){. mem5.max
8e560 52 65 71 75 65 73 74 20 3d 20 6e 42 79 74 65 3b Request = nByte;
8e570 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 62 6f 72 74 . }.. /* Abort
8e580 20 69 66 20 74 68 65 20 72 65 71 75 65 73 74 65 if the requeste
8e590 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a d allocation siz
8e5a0 65 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e e is larger than
8e5b0 20 74 68 65 20 6c 61 72 67 65 73 74 0a 20 20 2a the largest. *
8e5c0 2a 20 70 6f 77 65 72 20 6f 66 20 74 77 6f 20 74 * power of two t
8e5d0 68 61 74 20 77 65 20 63 61 6e 20 72 65 70 72 65 hat we can repre
8e5e0 73 65 6e 74 20 75 73 69 6e 67 20 33 32 2d 62 69 sent using 32-bi
8e5f0 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 t signed integer
8e600 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e 42 s.. */. if( nB
8e610 79 74 65 20 3e 20 30 78 34 30 30 30 30 30 30 30 yte > 0x40000000
8e620 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 ){. return 0
8e630 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 6f 75 6e ;. }.. /* Roun
8e640 64 20 6e 42 79 74 65 20 75 70 20 74 6f 20 74 68 d nByte up to th
8e650 65 20 6e 65 78 74 20 76 61 6c 69 64 20 70 6f 77 e next valid pow
8e660 65 72 20 6f 66 20 74 77 6f 20 2a 2f 0a 20 20 66 er of two */. f
8e670 6f 72 28 69 46 75 6c 6c 53 7a 3d 6d 65 6d 35 2e or(iFullSz=mem5.
8e680 73 7a 41 74 6f 6d 2c 20 69 4c 6f 67 73 69 7a 65 szAtom, iLogsize
8e690 3d 30 3b 20 69 46 75 6c 6c 53 7a 3c 6e 42 79 74 =0; iFullSz<nByt
8e6a0 65 3b 20 69 46 75 6c 6c 53 7a 20 2a 3d 20 32 2c e; iFullSz *= 2,
8e6b0 20 69 4c 6f 67 73 69 7a 65 2b 2b 29 7b 7d 0a 0a iLogsize++){}..
8e6c0 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 6d /* Make sure m
8e6d0 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 em5.aiFreelist[i
8e6e0 4c 6f 67 73 69 7a 65 5d 20 63 6f 6e 74 61 69 6e Logsize] contain
8e6f0 73 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 66 s at least one f
8e700 72 65 65 0a 20 20 2a 2a 20 62 6c 6f 63 6b 2e 20 ree. ** block.
8e710 20 49 66 20 6e 6f 74 2c 20 74 68 65 6e 20 73 70 If not, then sp
8e720 6c 69 74 20 61 20 62 6c 6f 63 6b 20 6f 66 20 74 lit a block of t
8e730 68 65 20 6e 65 78 74 20 6c 61 72 67 65 72 20 70 he next larger p
8e740 6f 77 65 72 20 6f 66 0a 20 20 2a 2a 20 74 77 6f ower of. ** two
8e750 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 63 72 65 in order to cre
8e760 61 74 65 20 61 20 6e 65 77 20 66 72 65 65 20 62 ate a new free b
8e770 6c 6f 63 6b 20 6f 66 20 73 69 7a 65 20 69 4c 6f lock of size iLo
8e780 67 73 69 7a 65 2e 0a 20 20 2a 2f 0a 20 20 66 6f gsize.. */. fo
8e790 72 28 69 42 69 6e 3d 69 4c 6f 67 73 69 7a 65 3b r(iBin=iLogsize;
8e7a0 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 mem5.aiFreelist
8e7b0 5b 69 42 69 6e 5d 3c 30 20 26 26 20 69 42 69 6e [iBin]<0 && iBin
8e7c0 3c 3d 4c 4f 47 4d 41 58 3b 20 69 42 69 6e 2b 2b <=LOGMAX; iBin++
8e7d0 29 7b 7d 0a 20 20 69 66 28 20 69 42 69 6e 3e 4c ){}. if( iBin>L
8e7e0 4f 47 4d 41 58 20 29 20 72 65 74 75 72 6e 20 30 OGMAX ) return 0
8e7f0 3b 0a 20 20 69 20 3d 20 6d 65 6d 73 79 73 35 55 ;. i = memsys5U
8e800 6e 6c 69 6e 6b 46 69 72 73 74 28 69 42 69 6e 29 nlinkFirst(iBin)
8e810 3b 0a 20 20 77 68 69 6c 65 28 20 69 42 69 6e 3e ;. while( iBin>
8e820 69 4c 6f 67 73 69 7a 65 20 29 7b 0a 20 20 20 20 iLogsize ){.
8e830 69 6e 74 20 6e 65 77 53 69 7a 65 3b 0a 0a 20 20 int newSize;..
8e840 20 20 69 42 69 6e 2d 2d 3b 0a 20 20 20 20 6e 65 iBin--;. ne
8e850 77 53 69 7a 65 20 3d 20 31 20 3c 3c 20 69 42 69 wSize = 1 << iBi
8e860 6e 3b 0a 20 20 20 20 6d 65 6d 35 2e 61 43 74 72 n;. mem5.aCtr
8e870 6c 5b 69 2b 6e 65 77 53 69 7a 65 5d 20 3d 20 43 l[i+newSize] = C
8e880 54 52 4c 5f 46 52 45 45 20 7c 20 69 42 69 6e 3b TRL_FREE | iBin;
8e890 0a 20 20 20 20 6d 65 6d 73 79 73 35 4c 69 6e 6b . memsys5Link
8e8a0 28 69 2b 6e 65 77 53 69 7a 65 2c 20 69 42 69 6e (i+newSize, iBin
8e8b0 29 3b 0a 20 20 7d 0a 20 20 6d 65 6d 35 2e 61 43 );. }. mem5.aC
8e8c0 74 72 6c 5b 69 5d 20 3d 20 69 4c 6f 67 73 69 7a trl[i] = iLogsiz
8e8d0 65 3b 0a 0a 20 20 2f 2a 20 55 70 64 61 74 65 20 e;.. /* Update
8e8e0 61 6c 6c 6f 63 61 74 6f 72 20 70 65 72 66 6f 72 allocator perfor
8e8f0 6d 61 6e 63 65 20 73 74 61 74 69 73 74 69 63 73 mance statistics
8e900 2e 20 2a 2f 0a 20 20 6d 65 6d 35 2e 6e 41 6c 6c . */. mem5.nAll
8e910 6f 63 2b 2b 3b 0a 20 20 6d 65 6d 35 2e 74 6f 74 oc++;. mem5.tot
8e920 61 6c 41 6c 6c 6f 63 20 2b 3d 20 69 46 75 6c 6c alAlloc += iFull
8e930 53 7a 3b 0a 20 20 6d 65 6d 35 2e 74 6f 74 61 6c Sz;. mem5.total
8e940 45 78 63 65 73 73 20 2b 3d 20 69 46 75 6c 6c 53 Excess += iFullS
8e950 7a 20 2d 20 6e 42 79 74 65 3b 0a 20 20 6d 65 6d z - nByte;. mem
8e960 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 2b 2b 5.currentCount++
8e970 3b 0a 20 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 ;. mem5.current
8e980 4f 75 74 20 2b 3d 20 69 46 75 6c 6c 53 7a 3b 0a Out += iFullSz;.
8e990 20 20 69 66 28 20 6d 65 6d 35 2e 6d 61 78 43 6f if( mem5.maxCo
8e9a0 75 6e 74 3c 6d 65 6d 35 2e 63 75 72 72 65 6e 74 unt<mem5.current
8e9b0 43 6f 75 6e 74 20 29 20 6d 65 6d 35 2e 6d 61 78 Count ) mem5.max
8e9c0 43 6f 75 6e 74 20 3d 20 6d 65 6d 35 2e 63 75 72 Count = mem5.cur
8e9d0 72 65 6e 74 43 6f 75 6e 74 3b 0a 20 20 69 66 28 rentCount;. if(
8e9e0 20 6d 65 6d 35 2e 6d 61 78 4f 75 74 3c 6d 65 6d mem5.maxOut<mem
8e9f0 35 2e 63 75 72 72 65 6e 74 4f 75 74 20 29 20 6d 5.currentOut ) m
8ea00 65 6d 35 2e 6d 61 78 4f 75 74 20 3d 20 6d 65 6d em5.maxOut = mem
8ea10 35 2e 63 75 72 72 65 6e 74 4f 75 74 3b 0a 0a 20 5.currentOut;..
8ea20 20 2f 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 /* Return a poi
8ea30 6e 74 65 72 20 74 6f 20 74 68 65 20 61 6c 6c 6f nter to the allo
8ea40 63 61 74 65 64 20 6d 65 6d 6f 72 79 2e 20 2a 2f cated memory. */
8ea50 0a 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 2a . return (void*
8ea60 29 26 6d 65 6d 35 2e 7a 50 6f 6f 6c 5b 69 2a 6d )&mem5.zPool[i*m
8ea70 65 6d 35 2e 73 7a 41 74 6f 6d 5d 3b 0a 7d 0a 0a em5.szAtom];.}..
8ea80 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 6e 20 6f 75 /*.** Free an ou
8ea90 74 73 74 61 6e 64 69 6e 67 20 6d 65 6d 6f 72 79 tstanding memory
8eaa0 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2f 0a allocation..*/.
8eab0 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 static void mems
8eac0 79 73 35 46 72 65 65 55 6e 73 61 66 65 28 76 6f ys5FreeUnsafe(vo
8ead0 69 64 20 2a 70 4f 6c 64 29 7b 0a 20 20 75 33 32 id *pOld){. u32
8eae0 20 73 69 7a 65 2c 20 69 4c 6f 67 73 69 7a 65 3b size, iLogsize;
8eaf0 0a 20 20 69 6e 74 20 69 42 6c 6f 63 6b 3b 0a 0a . int iBlock;..
8eb00 20 20 2f 2a 20 53 65 74 20 69 42 6c 6f 63 6b 20 /* Set iBlock
8eb10 74 6f 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 to the index of
8eb20 74 68 65 20 62 6c 6f 63 6b 20 70 6f 69 6e 74 65 the block pointe
8eb30 64 20 74 6f 20 62 79 20 70 4f 6c 64 20 69 6e 20 d to by pOld in
8eb40 0a 20 20 2a 2a 20 74 68 65 20 61 72 72 61 79 20 . ** the array
8eb50 6f 66 20 6d 65 6d 35 2e 73 7a 41 74 6f 6d 20 62 of mem5.szAtom b
8eb60 79 74 65 20 62 6c 6f 63 6b 73 20 70 6f 69 6e 74 yte blocks point
8eb70 65 64 20 74 6f 20 62 79 20 6d 65 6d 35 2e 7a 50 ed to by mem5.zP
8eb80 6f 6f 6c 2e 0a 20 20 2a 2f 0a 20 20 69 42 6c 6f ool.. */. iBlo
8eb90 63 6b 20 3d 20 28 28 75 38 20 2a 29 70 4f 6c 64 ck = ((u8 *)pOld
8eba0 2d 6d 65 6d 35 2e 7a 50 6f 6f 6c 29 2f 6d 65 6d -mem5.zPool)/mem
8ebb0 35 2e 73 7a 41 74 6f 6d 3b 0a 0a 20 20 2f 2a 20 5.szAtom;.. /*
8ebc0 43 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 70 Check that the p
8ebd0 6f 69 6e 74 65 72 20 70 4f 6c 64 20 70 6f 69 6e ointer pOld poin
8ebe0 74 73 20 74 6f 20 61 20 76 61 6c 69 64 2c 20 6e ts to a valid, n
8ebf0 6f 6e 2d 66 72 65 65 20 62 6c 6f 63 6b 2e 20 2a on-free block. *
8ec00 2f 0a 20 20 61 73 73 65 72 74 28 20 69 42 6c 6f /. assert( iBlo
8ec10 63 6b 3e 3d 30 20 26 26 20 69 42 6c 6f 63 6b 3c ck>=0 && iBlock<
8ec20 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a 20 mem5.nBlock );.
8ec30 20 61 73 73 65 72 74 28 20 28 28 75 38 20 2a 29 assert( ((u8 *)
8ec40 70 4f 6c 64 2d 6d 65 6d 35 2e 7a 50 6f 6f 6c 29 pOld-mem5.zPool)
8ec50 25 6d 65 6d 35 2e 73 7a 41 74 6f 6d 3d 3d 30 20 %mem5.szAtom==0
8ec60 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 6d 65 );. assert( (me
8ec70 6d 35 2e 61 43 74 72 6c 5b 69 42 6c 6f 63 6b 5d m5.aCtrl[iBlock]
8ec80 20 26 20 43 54 52 4c 5f 46 52 45 45 29 3d 3d 30 & CTRL_FREE)==0
8ec90 20 29 3b 0a 0a 20 20 69 4c 6f 67 73 69 7a 65 20 );.. iLogsize
8eca0 3d 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 6c = mem5.aCtrl[iBl
8ecb0 6f 63 6b 5d 20 26 20 43 54 52 4c 5f 4c 4f 47 53 ock] & CTRL_LOGS
8ecc0 49 5a 45 3b 0a 20 20 73 69 7a 65 20 3d 20 31 3c IZE;. size = 1<
8ecd0 3c 69 4c 6f 67 73 69 7a 65 3b 0a 20 20 61 73 73 <iLogsize;. ass
8ece0 65 72 74 28 20 69 42 6c 6f 63 6b 2b 73 69 7a 65 ert( iBlock+size
8ecf0 2d 31 3c 28 75 33 32 29 6d 65 6d 35 2e 6e 42 6c -1<(u32)mem5.nBl
8ed00 6f 63 6b 20 29 3b 0a 0a 20 20 6d 65 6d 35 2e 61 ock );.. mem5.a
8ed10 43 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20 7c 3d 20 Ctrl[iBlock] |=
8ed20 43 54 52 4c 5f 46 52 45 45 3b 0a 20 20 6d 65 6d CTRL_FREE;. mem
8ed30 35 2e 61 43 74 72 6c 5b 69 42 6c 6f 63 6b 2b 73 5.aCtrl[iBlock+s
8ed40 69 7a 65 2d 31 5d 20 7c 3d 20 43 54 52 4c 5f 46 ize-1] |= CTRL_F
8ed50 52 45 45 3b 0a 20 20 61 73 73 65 72 74 28 20 6d REE;. assert( m
8ed60 65 6d 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 em5.currentCount
8ed70 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 >0 );. assert(
8ed80 6d 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74 3e mem5.currentOut>
8ed90 3d 28 73 69 7a 65 2a 6d 65 6d 35 2e 73 7a 41 74 =(size*mem5.szAt
8eda0 6f 6d 29 20 29 3b 0a 20 20 6d 65 6d 35 2e 63 75 om) );. mem5.cu
8edb0 72 72 65 6e 74 43 6f 75 6e 74 2d 2d 3b 0a 20 20 rrentCount--;.
8edc0 6d 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74 20 mem5.currentOut
8edd0 2d 3d 20 73 69 7a 65 2a 6d 65 6d 35 2e 73 7a 41 -= size*mem5.szA
8ede0 74 6f 6d 3b 0a 20 20 61 73 73 65 72 74 28 20 6d tom;. assert( m
8edf0 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74 3e 30 em5.currentOut>0
8ee00 20 7c 7c 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 || mem5.current
8ee10 43 6f 75 6e 74 3d 3d 30 20 29 3b 0a 20 20 61 73 Count==0 );. as
8ee20 73 65 72 74 28 20 6d 65 6d 35 2e 63 75 72 72 65 sert( mem5.curre
8ee30 6e 74 43 6f 75 6e 74 3e 30 20 7c 7c 20 6d 65 6d ntCount>0 || mem
8ee40 35 2e 63 75 72 72 65 6e 74 4f 75 74 3d 3d 30 20 5.currentOut==0
8ee50 29 3b 0a 0a 20 20 6d 65 6d 35 2e 61 43 74 72 6c );.. mem5.aCtrl
8ee60 5b 69 42 6c 6f 63 6b 5d 20 3d 20 43 54 52 4c 5f [iBlock] = CTRL_
8ee70 46 52 45 45 20 7c 20 69 4c 6f 67 73 69 7a 65 3b FREE | iLogsize;
8ee80 0a 20 20 77 68 69 6c 65 28 20 41 4c 57 41 59 53 . while( ALWAYS
8ee90 28 69 4c 6f 67 73 69 7a 65 3c 4c 4f 47 4d 41 58 (iLogsize<LOGMAX
8eea0 29 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 42 75 ) ){. int iBu
8eeb0 64 64 79 3b 0a 20 20 20 20 69 66 28 20 28 69 42 ddy;. if( (iB
8eec0 6c 6f 63 6b 3e 3e 69 4c 6f 67 73 69 7a 65 29 20 lock>>iLogsize)
8eed0 26 20 31 20 29 7b 0a 20 20 20 20 20 20 69 42 75 & 1 ){. iBu
8eee0 64 64 79 20 3d 20 69 42 6c 6f 63 6b 20 2d 20 73 ddy = iBlock - s
8eef0 69 7a 65 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a ize;. }else{.
8ef00 20 20 20 20 20 20 69 42 75 64 64 79 20 3d 20 69 iBuddy = i
8ef10 42 6c 6f 63 6b 20 2b 20 73 69 7a 65 3b 0a 20 20 Block + size;.
8ef20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 }. assert(
8ef30 69 42 75 64 64 79 3e 3d 30 20 29 3b 0a 20 20 20 iBuddy>=0 );.
8ef40 20 69 66 28 20 28 69 42 75 64 64 79 2b 28 31 3c if( (iBuddy+(1<
8ef50 3c 69 4c 6f 67 73 69 7a 65 29 29 3e 6d 65 6d 35 <iLogsize))>mem5
8ef60 2e 6e 42 6c 6f 63 6b 20 29 20 62 72 65 61 6b 3b .nBlock ) break;
8ef70 0a 20 20 20 20 69 66 28 20 6d 65 6d 35 2e 61 43 . if( mem5.aC
8ef80 74 72 6c 5b 69 42 75 64 64 79 5d 21 3d 28 43 54 trl[iBuddy]!=(CT
8ef90 52 4c 5f 46 52 45 45 20 7c 20 69 4c 6f 67 73 69 RL_FREE | iLogsi
8efa0 7a 65 29 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 ze) ) break;.
8efb0 20 6d 65 6d 73 79 73 35 55 6e 6c 69 6e 6b 28 69 memsys5Unlink(i
8efc0 42 75 64 64 79 2c 20 69 4c 6f 67 73 69 7a 65 29 Buddy, iLogsize)
8efd0 3b 0a 20 20 20 20 69 4c 6f 67 73 69 7a 65 2b 2b ;. iLogsize++
8efe0 3b 0a 20 20 20 20 69 66 28 20 69 42 75 64 64 79 ;. if( iBuddy
8eff0 3c 69 42 6c 6f 63 6b 20 29 7b 0a 20 20 20 20 20 <iBlock ){.
8f000 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 75 64 mem5.aCtrl[iBud
8f010 64 79 5d 20 3d 20 43 54 52 4c 5f 46 52 45 45 20 dy] = CTRL_FREE
8f020 7c 20 69 4c 6f 67 73 69 7a 65 3b 0a 20 20 20 20 | iLogsize;.
8f030 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 6c mem5.aCtrl[iBl
8f040 6f 63 6b 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 ock] = 0;.
8f050 69 42 6c 6f 63 6b 20 3d 20 69 42 75 64 64 79 3b iBlock = iBuddy;
8f060 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
8f070 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 6c mem5.aCtrl[iBl
8f080 6f 63 6b 5d 20 3d 20 43 54 52 4c 5f 46 52 45 45 ock] = CTRL_FREE
8f090 20 7c 20 69 4c 6f 67 73 69 7a 65 3b 0a 20 20 20 | iLogsize;.
8f0a0 20 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 mem5.aCtrl[iB
8f0b0 75 64 64 79 5d 20 3d 20 30 3b 0a 20 20 20 20 7d uddy] = 0;. }
8f0c0 0a 20 20 20 20 73 69 7a 65 20 2a 3d 20 32 3b 0a . size *= 2;.
8f0d0 20 20 7d 0a 20 20 6d 65 6d 73 79 73 35 4c 69 6e }. memsys5Lin
8f0e0 6b 28 69 42 6c 6f 63 6b 2c 20 69 4c 6f 67 73 69 k(iBlock, iLogsi
8f0f0 7a 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c ze);.}../*.** Al
8f100 6c 6f 63 61 74 65 20 6e 42 79 74 65 73 20 6f 66 locate nBytes of
8f110 20 6d 65 6d 6f 72 79 0a 2a 2f 0a 73 74 61 74 69 memory.*/.stati
8f120 63 20 76 6f 69 64 20 2a 6d 65 6d 73 79 73 35 4d c void *memsys5M
8f130 61 6c 6c 6f 63 28 69 6e 74 20 6e 42 79 74 65 73 alloc(int nBytes
8f140 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 ){. sqlite3_int
8f150 36 34 20 2a 70 20 3d 20 30 3b 0a 20 20 69 66 28 64 *p = 0;. if(
8f160 20 6e 42 79 74 65 73 3e 30 20 29 7b 0a 20 20 20 nBytes>0 ){.
8f170 20 6d 65 6d 73 79 73 35 45 6e 74 65 72 28 29 3b memsys5Enter();
8f180 0a 20 20 20 20 70 20 3d 20 6d 65 6d 73 79 73 35 . p = memsys5
8f190 4d 61 6c 6c 6f 63 55 6e 73 61 66 65 28 6e 42 79 MallocUnsafe(nBy
8f1a0 74 65 73 29 3b 0a 20 20 20 20 6d 65 6d 73 79 73 tes);. memsys
8f1b0 35 4c 65 61 76 65 28 29 3b 0a 20 20 7d 0a 20 20 5Leave();. }.
8f1c0 72 65 74 75 72 6e 20 28 76 6f 69 64 2a 29 70 3b return (void*)p;
8f1d0 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 .}../*.** Free
8f1e0 6d 65 6d 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 memory..**.** Th
8f1f0 65 20 6f 75 74 65 72 20 6c 61 79 65 72 20 6d 65 e outer layer me
8f200 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 70 mory allocator p
8f210 72 65 76 65 6e 74 73 20 74 68 69 73 20 72 6f 75 revents this rou
8f220 74 69 6e 65 20 66 72 6f 6d 0a 2a 2a 20 62 65 69 tine from.** bei
8f230 6e 67 20 63 61 6c 6c 65 64 20 77 69 74 68 20 70 ng called with p
8f240 50 72 69 6f 72 3d 3d 30 2e 0a 2a 2f 0a 73 74 61 Prior==0..*/.sta
8f250 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 35 tic void memsys5
8f260 46 72 65 65 28 76 6f 69 64 20 2a 70 50 72 69 6f Free(void *pPrio
8f270 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 50 r){. assert( pP
8f280 72 69 6f 72 21 3d 30 20 29 3b 0a 20 20 6d 65 6d rior!=0 );. mem
8f290 73 79 73 35 45 6e 74 65 72 28 29 3b 0a 20 20 6d sys5Enter();. m
8f2a0 65 6d 73 79 73 35 46 72 65 65 55 6e 73 61 66 65 emsys5FreeUnsafe
8f2b0 28 70 50 72 69 6f 72 29 3b 0a 20 20 6d 65 6d 73 (pPrior);. mems
8f2c0 79 73 35 4c 65 61 76 65 28 29 3b 20 20 0a 7d 0a ys5Leave(); .}.
8f2d0 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 ./*.** Change th
8f2e0 65 20 73 69 7a 65 20 6f 66 20 61 6e 20 65 78 69 e size of an exi
8f2f0 73 74 69 6e 67 20 6d 65 6d 6f 72 79 20 61 6c 6c sting memory all
8f300 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 ocation..**.** T
8f310 68 65 20 6f 75 74 65 72 20 6c 61 79 65 72 20 6d he outer layer m
8f320 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 emory allocator
8f330 70 72 65 76 65 6e 74 73 20 74 68 69 73 20 72 6f prevents this ro
8f340 75 74 69 6e 65 20 66 72 6f 6d 0a 2a 2a 20 62 65 utine from.** be
8f350 69 6e 67 20 63 61 6c 6c 65 64 20 77 69 74 68 20 ing called with
8f360 70 50 72 69 6f 72 3d 3d 30 2e 20 20 0a 2a 2a 0a pPrior==0. .**.
8f370 2a 2a 20 6e 42 79 74 65 73 20 69 73 20 61 6c 77 ** nBytes is alw
8f380 61 79 73 20 61 20 76 61 6c 75 65 20 6f 62 74 61 ays a value obta
8f390 69 6e 65 64 20 66 72 6f 6d 20 61 20 70 72 69 6f ined from a prio
8f3a0 72 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 6d 65 6d r call to.** mem
8f3b0 73 79 73 35 52 6f 75 6e 64 28 29 2e 20 20 48 65 sys5Round(). He
8f3c0 6e 63 65 20 6e 42 79 74 65 73 20 69 73 20 61 6c nce nBytes is al
8f3d0 77 61 79 73 20 61 20 6e 6f 6e 2d 6e 65 67 61 74 ways a non-negat
8f3e0 69 76 65 20 70 6f 77 65 72 0a 2a 2a 20 6f 66 20 ive power.** of
8f3f0 74 77 6f 2e 20 20 49 66 20 6e 42 79 74 65 73 3d two. If nBytes=
8f400 3d 30 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 =0 that means th
8f410 61 74 20 61 6e 20 6f 76 65 72 73 69 7a 65 20 61 at an oversize a
8f420 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 28 61 6e llocation.** (an
8f430 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6c 61 72 67 allocation larg
8f440 65 72 20 74 68 61 6e 20 30 78 34 30 30 30 30 30 er than 0x400000
8f450 30 30 29 20 77 61 73 20 72 65 71 75 65 73 74 65 00) was requeste
8f460 64 20 61 6e 64 20 74 68 69 73 0a 2a 2a 20 72 6f d and this.** ro
8f470 75 74 69 6e 65 20 73 68 6f 75 6c 64 20 72 65 74 utine should ret
8f480 75 72 6e 20 30 20 77 69 74 68 6f 75 74 20 66 72 urn 0 without fr
8f490 65 65 69 6e 67 20 70 50 72 69 6f 72 2e 0a 2a 2f eeing pPrior..*/
8f4a0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6d 65 .static void *me
8f4b0 6d 73 79 73 35 52 65 61 6c 6c 6f 63 28 76 6f 69 msys5Realloc(voi
8f4c0 64 20 2a 70 50 72 69 6f 72 2c 20 69 6e 74 20 6e d *pPrior, int n
8f4d0 42 79 74 65 73 29 7b 0a 20 20 69 6e 74 20 6e 4f Bytes){. int nO
8f4e0 6c 64 3b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a 20 ld;. void *p;.
8f4f0 20 61 73 73 65 72 74 28 20 70 50 72 69 6f 72 21 assert( pPrior!
8f500 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
8f510 28 6e 42 79 74 65 73 26 28 6e 42 79 74 65 73 2d (nBytes&(nBytes-
8f520 31 29 29 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 1))==0 );. asse
8f530 72 74 28 20 6e 42 79 74 65 73 3e 3d 30 20 29 3b rt( nBytes>=0 );
8f540 0a 20 20 69 66 28 20 6e 42 79 74 65 73 3d 3d 30 . if( nBytes==0
8f550 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 ){. return 0
8f560 3b 0a 20 20 7d 0a 20 20 6e 4f 6c 64 20 3d 20 6d ;. }. nOld = m
8f570 65 6d 73 79 73 35 53 69 7a 65 28 70 50 72 69 6f emsys5Size(pPrio
8f580 72 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 73 r);. if( nBytes
8f590 3c 3d 6e 4f 6c 64 20 29 7b 0a 20 20 20 20 72 65 <=nOld ){. re
8f5a0 74 75 72 6e 20 70 50 72 69 6f 72 3b 0a 20 20 7d turn pPrior;. }
8f5b0 0a 20 20 6d 65 6d 73 79 73 35 45 6e 74 65 72 28 . memsys5Enter(
8f5c0 29 3b 0a 20 20 70 20 3d 20 6d 65 6d 73 79 73 35 );. p = memsys5
8f5d0 4d 61 6c 6c 6f 63 55 6e 73 61 66 65 28 6e 42 79 MallocUnsafe(nBy
8f5e0 74 65 73 29 3b 0a 20 20 69 66 28 20 70 20 29 7b tes);. if( p ){
8f5f0 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 2c 20 70 . memcpy(p, p
8f600 50 72 69 6f 72 2c 20 6e 4f 6c 64 29 3b 0a 20 20 Prior, nOld);.
8f610 20 20 6d 65 6d 73 79 73 35 46 72 65 65 55 6e 73 memsys5FreeUns
8f620 61 66 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 7d afe(pPrior);. }
8f630 0a 20 20 6d 65 6d 73 79 73 35 4c 65 61 76 65 28 . memsys5Leave(
8f640 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d );. return p;.}
8f650 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 6e 64 20 75 70 ../*.** Round up
8f660 20 61 20 72 65 71 75 65 73 74 20 73 69 7a 65 20 a request size
8f670 74 6f 20 74 68 65 20 6e 65 78 74 20 76 61 6c 69 to the next vali
8f680 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a d allocation siz
8f690 65 2e 20 20 49 66 0a 2a 2a 20 74 68 65 20 61 6c e. If.** the al
8f6a0 6c 6f 63 61 74 69 6f 6e 20 69 73 20 74 6f 6f 20 location is too
8f6b0 6c 61 72 67 65 20 74 6f 20 62 65 20 68 61 6e 64 large to be hand
8f6c0 6c 65 64 20 62 79 20 74 68 69 73 20 61 6c 6c 6f led by this allo
8f6d0 63 61 74 69 6f 6e 20 73 79 73 74 65 6d 2c 0a 2a cation system,.*
8f6e0 2a 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2a 0a 2a * return 0..**.*
8f6f0 2a 20 41 6c 6c 20 61 6c 6c 6f 63 61 74 69 6f 6e * All allocation
8f700 73 20 6d 75 73 74 20 62 65 20 61 20 70 6f 77 65 s must be a powe
8f710 72 20 6f 66 20 74 77 6f 20 61 6e 64 20 6d 75 73 r of two and mus
8f720 74 20 62 65 20 65 78 70 72 65 73 73 65 64 20 62 t be expressed b
8f730 79 20 61 0a 2a 2a 20 33 32 2d 62 69 74 20 73 69 y a.** 32-bit si
8f740 67 6e 65 64 20 69 6e 74 65 67 65 72 2e 20 20 48 gned integer. H
8f750 65 6e 63 65 20 74 68 65 20 6c 61 72 67 65 73 74 ence the largest
8f760 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 30 allocation is 0
8f770 78 34 30 30 30 30 30 30 30 0a 2a 2a 20 6f 72 20 x40000000.** or
8f780 31 30 37 33 37 34 31 38 32 34 20 62 79 74 65 73 1073741824 bytes
8f790 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
8f7a0 6d 65 6d 73 79 73 35 52 6f 75 6e 64 75 70 28 69 memsys5Roundup(i
8f7b0 6e 74 20 6e 29 7b 0a 20 20 69 6e 74 20 69 46 75 nt n){. int iFu
8f7c0 6c 6c 53 7a 3b 0a 20 20 69 66 28 20 6e 20 3e 20 llSz;. if( n >
8f7d0 30 78 34 30 30 30 30 30 30 30 20 29 20 72 65 74 0x40000000 ) ret
8f7e0 75 72 6e 20 30 3b 0a 20 20 66 6f 72 28 69 46 75 urn 0;. for(iFu
8f7f0 6c 6c 53 7a 3d 6d 65 6d 35 2e 73 7a 41 74 6f 6d llSz=mem5.szAtom
8f800 3b 20 69 46 75 6c 6c 53 7a 3c 6e 3b 20 69 46 75 ; iFullSz<n; iFu
8f810 6c 6c 53 7a 20 2a 3d 20 32 29 3b 0a 20 20 72 65 llSz *= 2);. re
8f820 74 75 72 6e 20 69 46 75 6c 6c 53 7a 3b 0a 7d 0a turn iFullSz;.}.
8f830 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ./*.** Return th
8f840 65 20 63 65 69 6c 69 6e 67 20 6f 66 20 74 68 65 e ceiling of the
8f850 20 6c 6f 67 61 72 69 74 68 6d 20 62 61 73 65 20 logarithm base
8f860 32 20 6f 66 20 69 56 61 6c 75 65 2e 0a 2a 2a 0a 2 of iValue..**.
8f870 2a 2a 20 45 78 61 6d 70 6c 65 73 3a 20 20 20 6d ** Examples: m
8f880 65 6d 73 79 73 35 4c 6f 67 28 31 29 20 2d 3e 20 emsys5Log(1) ->
8f890 30 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 0.**
8f8a0 20 6d 65 6d 73 79 73 35 4c 6f 67 28 32 29 20 2d memsys5Log(2) -
8f8b0 3e 20 31 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 > 1.**
8f8c0 20 20 20 6d 65 6d 73 79 73 35 4c 6f 67 28 34 29 memsys5Log(4)
8f8d0 20 2d 3e 20 32 0a 2a 2a 20 20 20 20 20 20 20 20 -> 2.**
8f8e0 20 20 20 20 20 6d 65 6d 73 79 73 35 4c 6f 67 28 memsys5Log(
8f8f0 35 29 20 2d 3e 20 33 0a 2a 2a 20 20 20 20 20 20 5) -> 3.**
8f900 20 20 20 20 20 20 20 6d 65 6d 73 79 73 35 4c 6f memsys5Lo
8f910 67 28 38 29 20 2d 3e 20 33 0a 2a 2a 20 20 20 20 g(8) -> 3.**
8f920 20 20 20 20 20 20 20 20 20 6d 65 6d 73 79 73 35 memsys5
8f930 4c 6f 67 28 39 29 20 2d 3e 20 34 0a 2a 2f 0a 73 Log(9) -> 4.*/.s
8f940 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 73 79 73 tatic int memsys
8f950 35 4c 6f 67 28 69 6e 74 20 69 56 61 6c 75 65 29 5Log(int iValue)
8f960 7b 0a 20 20 69 6e 74 20 69 4c 6f 67 3b 0a 20 20 {. int iLog;.
8f970 66 6f 72 28 69 4c 6f 67 3d 30 3b 20 28 31 3c 3c for(iLog=0; (1<<
8f980 69 4c 6f 67 29 3c 69 56 61 6c 75 65 3b 20 69 4c iLog)<iValue; iL
8f990 6f 67 2b 2b 29 3b 0a 20 20 72 65 74 75 72 6e 20 og++);. return
8f9a0 69 4c 6f 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 iLog;.}../*.** I
8f9b0 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 65 nitialize the me
8f9c0 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 2e 0a mory allocator..
8f9d0 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
8f9e0 6e 65 20 69 73 20 6e 6f 74 20 74 68 72 65 61 64 ne is not thread
8f9f0 73 61 66 65 2e 20 20 54 68 65 20 63 61 6c 6c 65 safe. The calle
8fa00 72 20 6d 75 73 74 20 62 65 20 68 6f 6c 64 69 6e r must be holdin
8fa10 67 20 61 20 6d 75 74 65 78 0a 2a 2a 20 74 6f 20 g a mutex.** to
8fa20 70 72 65 76 65 6e 74 20 6d 75 6c 74 69 70 6c 65 prevent multiple
8fa30 20 74 68 72 65 61 64 73 20 66 72 6f 6d 20 65 6e threads from en
8fa40 74 65 72 69 6e 67 20 61 74 20 74 68 65 20 73 61 tering at the sa
8fa50 6d 65 20 74 69 6d 65 2e 0a 2a 2f 0a 73 74 61 74 me time..*/.stat
8fa60 69 63 20 69 6e 74 20 6d 65 6d 73 79 73 35 49 6e ic int memsys5In
8fa70 69 74 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 it(void *NotUsed
8fa80 29 7b 0a 20 20 69 6e 74 20 69 69 3b 20 20 20 20 ){. int ii;
8fa90 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 /* Loop
8faa0 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 counter */. int
8fab0 20 6e 42 79 74 65 3b 20 20 20 20 20 20 20 20 20 nByte;
8fac0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 /* Number of byt
8fad0 65 73 20 6f 66 20 6d 65 6d 6f 72 79 20 61 76 61 es of memory ava
8fae0 69 6c 61 62 6c 65 20 74 6f 20 74 68 69 73 20 61 ilable to this a
8faf0 6c 6c 6f 63 61 74 6f 72 20 2a 2f 0a 20 20 75 38 llocator */. u8
8fb00 20 2a 7a 42 79 74 65 3b 20 20 20 20 20 20 20 20 *zByte;
8fb10 20 2f 2a 20 4d 65 6d 6f 72 79 20 75 73 61 62 6c /* Memory usabl
8fb20 65 20 62 79 20 74 68 69 73 20 61 6c 6c 6f 63 61 e by this alloca
8fb30 74 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 69 tor */. int nMi
8fb40 6e 4c 6f 67 3b 20 20 20 20 20 20 20 2f 2a 20 4c nLog; /* L
8fb50 6f 67 20 62 61 73 65 20 32 20 6f 66 20 6d 69 6e og base 2 of min
8fb60 69 6d 75 6d 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 imum allocation
8fb70 73 69 7a 65 20 69 6e 20 62 79 74 65 73 20 2a 2f size in bytes */
8fb80 0a 20 20 69 6e 74 20 69 4f 66 66 73 65 74 3b 20 . int iOffset;
8fb90 20 20 20 20 20 20 2f 2a 20 41 6e 20 6f 66 66 73 /* An offs
8fba0 65 74 20 69 6e 74 6f 20 6d 65 6d 35 2e 61 43 74 et into mem5.aCt
8fbb0 72 6c 5b 5d 20 2a 2f 0a 0a 20 20 55 4e 55 53 45 rl[] */.. UNUSE
8fbc0 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 D_PARAMETER(NotU
8fbd0 73 65 64 29 3b 0a 0a 20 20 2f 2a 20 46 6f 72 20 sed);.. /* For
8fbe0 74 68 65 20 70 75 72 70 6f 73 65 73 20 6f 66 20 the purposes of
8fbf0 74 68 69 73 20 72 6f 75 74 69 6e 65 2c 20 64 69 this routine, di
8fc00 73 61 62 6c 65 20 74 68 65 20 6d 75 74 65 78 20 sable the mutex
8fc10 2a 2f 0a 20 20 6d 65 6d 35 2e 6d 75 74 65 78 20 */. mem5.mutex
8fc20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 73 = 0;.. /* The s
8fc30 69 7a 65 20 6f 66 20 61 20 4d 65 6d 35 4c 69 6e ize of a Mem5Lin
8fc40 6b 20 6f 62 6a 65 63 74 20 6d 75 73 74 20 62 65 k object must be
8fc50 20 61 20 70 6f 77 65 72 20 6f 66 20 74 77 6f 2e a power of two.
8fc60 20 20 56 65 72 69 66 79 20 74 68 61 74 0a 20 20 Verify that.
8fc70 2a 2a 20 74 68 69 73 20 69 73 20 63 61 73 65 2e ** this is case.
8fc80 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 . */. assert(
8fc90 28 73 69 7a 65 6f 66 28 4d 65 6d 35 4c 69 6e 6b (sizeof(Mem5Link
8fca0 29 26 28 73 69 7a 65 6f 66 28 4d 65 6d 35 4c 69 )&(sizeof(Mem5Li
8fcb0 6e 6b 29 2d 31 29 29 3d 3d 30 20 29 3b 0a 0a 20 nk)-1))==0 );..
8fcc0 20 6e 42 79 74 65 20 3d 20 73 71 6c 69 74 65 33 nByte = sqlite3
8fcd0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 48 65 GlobalConfig.nHe
8fce0 61 70 3b 0a 20 20 7a 42 79 74 65 20 3d 20 28 75 ap;. zByte = (u
8fcf0 38 2a 29 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 8*)sqlite3Global
8fd00 43 6f 6e 66 69 67 2e 70 48 65 61 70 3b 0a 20 20 Config.pHeap;.
8fd10 61 73 73 65 72 74 28 20 7a 42 79 74 65 21 3d 30 assert( zByte!=0
8fd20 20 29 3b 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f ); /* sqlite3_
8fd30 63 6f 6e 66 69 67 28 29 20 64 6f 65 73 20 6e 6f config() does no
8fd40 74 20 61 6c 6c 6f 77 20 6f 74 68 65 72 77 69 73 t allow otherwis
8fd50 65 20 2a 2f 0a 0a 20 20 6e 4d 69 6e 4c 6f 67 20 e */.. nMinLog
8fd60 3d 20 6d 65 6d 73 79 73 35 4c 6f 67 28 73 71 6c = memsys5Log(sql
8fd70 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
8fd80 2e 6d 6e 52 65 71 29 3b 0a 20 20 6d 65 6d 35 2e .mnReq);. mem5.
8fd90 73 7a 41 74 6f 6d 20 3d 20 28 31 3c 3c 6e 4d 69 szAtom = (1<<nMi
8fda0 6e 4c 6f 67 29 3b 0a 20 20 77 68 69 6c 65 28 20 nLog);. while(
8fdb0 28 69 6e 74 29 73 69 7a 65 6f 66 28 4d 65 6d 35 (int)sizeof(Mem5
8fdc0 4c 69 6e 6b 29 3e 6d 65 6d 35 2e 73 7a 41 74 6f Link)>mem5.szAto
8fdd0 6d 20 29 7b 0a 20 20 20 20 6d 65 6d 35 2e 73 7a m ){. mem5.sz
8fde0 41 74 6f 6d 20 3d 20 6d 65 6d 35 2e 73 7a 41 74 Atom = mem5.szAt
8fdf0 6f 6d 20 3c 3c 20 31 3b 0a 20 20 7d 0a 0a 20 20 om << 1;. }..
8fe00 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 3d 20 28 6e mem5.nBlock = (n
8fe10 42 79 74 65 20 2f 20 28 6d 65 6d 35 2e 73 7a 41 Byte / (mem5.szA
8fe20 74 6f 6d 2b 73 69 7a 65 6f 66 28 75 38 29 29 29 tom+sizeof(u8)))
8fe30 3b 0a 20 20 6d 65 6d 35 2e 7a 50 6f 6f 6c 20 3d ;. mem5.zPool =
8fe40 20 7a 42 79 74 65 3b 0a 20 20 6d 65 6d 35 2e 61 zByte;. mem5.a
8fe50 43 74 72 6c 20 3d 20 28 75 38 20 2a 29 26 6d 65 Ctrl = (u8 *)&me
8fe60 6d 35 2e 7a 50 6f 6f 6c 5b 6d 65 6d 35 2e 6e 42 m5.zPool[mem5.nB
8fe70 6c 6f 63 6b 2a 6d 65 6d 35 2e 73 7a 41 74 6f 6d lock*mem5.szAtom
8fe80 5d 3b 0a 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 ];.. for(ii=0;
8fe90 69 69 3c 3d 4c 4f 47 4d 41 58 3b 20 69 69 2b 2b ii<=LOGMAX; ii++
8fea0 29 7b 0a 20 20 20 20 6d 65 6d 35 2e 61 69 46 72 ){. mem5.aiFr
8feb0 65 65 6c 69 73 74 5b 69 69 5d 20 3d 20 2d 31 3b eelist[ii] = -1;
8fec0 0a 20 20 7d 0a 0a 20 20 69 4f 66 66 73 65 74 20 . }.. iOffset
8fed0 3d 20 30 3b 0a 20 20 66 6f 72 28 69 69 3d 4c 4f = 0;. for(ii=LO
8fee0 47 4d 41 58 3b 20 69 69 3e 3d 30 3b 20 69 69 2d GMAX; ii>=0; ii-
8fef0 2d 29 7b 0a 20 20 20 20 69 6e 74 20 6e 41 6c 6c -){. int nAll
8ff00 6f 63 20 3d 20 28 31 3c 3c 69 69 29 3b 0a 20 20 oc = (1<<ii);.
8ff10 20 20 69 66 28 20 28 69 4f 66 66 73 65 74 2b 6e if( (iOffset+n
8ff20 41 6c 6c 6f 63 29 3c 3d 6d 65 6d 35 2e 6e 42 6c Alloc)<=mem5.nBl
8ff30 6f 63 6b 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d ock ){. mem
8ff40 35 2e 61 43 74 72 6c 5b 69 4f 66 66 73 65 74 5d 5.aCtrl[iOffset]
8ff50 20 3d 20 69 69 20 7c 20 43 54 52 4c 5f 46 52 45 = ii | CTRL_FRE
8ff60 45 3b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 35 E;. memsys5
8ff70 4c 69 6e 6b 28 69 4f 66 66 73 65 74 2c 20 69 69 Link(iOffset, ii
8ff80 29 3b 0a 20 20 20 20 20 20 69 4f 66 66 73 65 74 );. iOffset
8ff90 20 2b 3d 20 6e 41 6c 6c 6f 63 3b 0a 20 20 20 20 += nAlloc;.
8ffa0 7d 0a 20 20 20 20 61 73 73 65 72 74 28 28 69 4f }. assert((iO
8ffb0 66 66 73 65 74 2b 6e 41 6c 6c 6f 63 29 3e 6d 65 ffset+nAlloc)>me
8ffc0 6d 35 2e 6e 42 6c 6f 63 6b 29 3b 0a 20 20 7d 0a m5.nBlock);. }.
8ffd0 0a 20 20 2f 2a 20 49 66 20 61 20 6d 75 74 65 78 . /* If a mutex
8ffe0 20 69 73 20 72 65 71 75 69 72 65 64 20 66 6f 72 is required for
8fff0 20 6e 6f 72 6d 61 6c 20 6f 70 65 72 61 74 69 6f normal operatio
90000 6e 2c 20 61 6c 6c 6f 63 61 74 65 20 6f 6e 65 20 n, allocate one
90010 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 */. if( sqlite3
90020 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 GlobalConfig.bMe
90030 6d 73 74 61 74 3d 3d 30 20 29 7b 0a 20 20 20 20 mstat==0 ){.
90040 6d 65 6d 35 2e 6d 75 74 65 78 20 3d 20 73 71 6c mem5.mutex = sql
90050 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 ite3MutexAlloc(S
90060 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 QLITE_MUTEX_STAT
90070 49 43 5f 4d 45 4d 29 3b 0a 20 20 7d 0a 0a 20 20 IC_MEM);. }..
90080 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
90090 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 6e 69 ;.}../*.** Deini
900a0 74 69 61 6c 69 7a 65 20 74 68 69 73 20 6d 6f 64 tialize this mod
900b0 75 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ule..*/.static v
900c0 6f 69 64 20 6d 65 6d 73 79 73 35 53 68 75 74 64 oid memsys5Shutd
900d0 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 own(void *NotUse
900e0 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 d){. UNUSED_PAR
900f0 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b AMETER(NotUsed);
90100 0a 20 20 6d 65 6d 35 2e 6d 75 74 65 78 20 3d 20 . mem5.mutex =
90110 30 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 0;. return;.}..
90120 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 #ifdef SQLITE_TE
90130 53 54 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 74 68 ST./*.** Open th
90140 65 20 66 69 6c 65 20 69 6e 64 69 63 61 74 65 64 e file indicated
90150 20 61 6e 64 20 77 72 69 74 65 20 61 20 6c 6f 67 and write a log
90160 20 6f 66 20 61 6c 6c 20 75 6e 66 72 65 65 64 20 of all unfreed
90170 6d 65 6d 6f 72 79 20 0a 2a 2a 20 61 6c 6c 6f 63 memory .** alloc
90180 61 74 69 6f 6e 73 20 69 6e 74 6f 20 74 68 61 74 ations into that
90190 20 6c 6f 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f log..*/.SQLITE_
901a0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
901b0 69 74 65 33 4d 65 6d 73 79 73 35 44 75 6d 70 28 ite3Memsys5Dump(
901c0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c const char *zFil
901d0 65 6e 61 6d 65 29 7b 0a 20 20 46 49 4c 45 20 2a ename){. FILE *
901e0 6f 75 74 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 2c out;. int i, j,
901f0 20 6e 3b 0a 20 20 69 6e 74 20 6e 4d 69 6e 4c 6f n;. int nMinLo
90200 67 3b 0a 0a 20 20 69 66 28 20 7a 46 69 6c 65 6e g;.. if( zFilen
90210 61 6d 65 3d 3d 30 20 7c 7c 20 7a 46 69 6c 65 6e ame==0 || zFilen
90220 61 6d 65 5b 30 5d 3d 3d 30 20 29 7b 0a 20 20 20 ame[0]==0 ){.
90230 20 6f 75 74 20 3d 20 73 74 64 6f 75 74 3b 0a 20 out = stdout;.
90240 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6f 75 74 20 }else{. out
90250 3d 20 66 6f 70 65 6e 28 7a 46 69 6c 65 6e 61 6d = fopen(zFilenam
90260 65 2c 20 22 77 22 29 3b 0a 20 20 20 20 69 66 28 e, "w");. if(
90270 20 6f 75 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 out==0 ){.
90280 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c fprintf(stderr,
90290 20 22 2a 2a 20 55 6e 61 62 6c 65 20 74 6f 20 6f "** Unable to o
902a0 75 74 70 75 74 20 6d 65 6d 6f 72 79 20 64 65 62 utput memory deb
902b0 75 67 20 6f 75 74 70 75 74 20 6c 6f 67 3a 20 25 ug output log: %
902c0 73 20 2a 2a 5c 6e 22 2c 0a 20 20 20 20 20 20 20 s **\n",.
902d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7a z
902e0 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 20 Filename);.
902f0 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 return;. }.
90300 20 7d 0a 20 20 6d 65 6d 73 79 73 35 45 6e 74 65 }. memsys5Ente
90310 72 28 29 3b 0a 20 20 6e 4d 69 6e 4c 6f 67 20 3d r();. nMinLog =
90320 20 6d 65 6d 73 79 73 35 4c 6f 67 28 6d 65 6d 35 memsys5Log(mem5
90330 2e 73 7a 41 74 6f 6d 29 3b 0a 20 20 66 6f 72 28 .szAtom);. for(
90340 69 3d 30 3b 20 69 3c 3d 4c 4f 47 4d 41 58 20 26 i=0; i<=LOGMAX &
90350 26 20 69 2b 6e 4d 69 6e 4c 6f 67 3c 33 32 3b 20 & i+nMinLog<32;
90360 69 2b 2b 29 7b 0a 20 20 20 20 66 6f 72 28 6e 3d i++){. for(n=
90370 30 2c 20 6a 3d 6d 65 6d 35 2e 61 69 46 72 65 65 0, j=mem5.aiFree
90380 6c 69 73 74 5b 69 5d 3b 20 6a 3e 3d 30 3b 20 6a list[i]; j>=0; j
90390 20 3d 20 4d 45 4d 35 4c 49 4e 4b 28 6a 29 2d 3e = MEM5LINK(j)->
903a0 6e 65 78 74 2c 20 6e 2b 2b 29 7b 7d 0a 20 20 20 next, n++){}.
903b0 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 66 fprintf(out, "f
903c0 72 65 65 6c 69 73 74 20 69 74 65 6d 73 20 6f 66 reelist items of
903d0 20 73 69 7a 65 20 25 64 3a 20 25 64 5c 6e 22 2c size %d: %d\n",
903e0 20 6d 65 6d 35 2e 73 7a 41 74 6f 6d 20 3c 3c 20 mem5.szAtom <<
903f0 69 2c 20 6e 29 3b 0a 20 20 7d 0a 20 20 66 70 72 i, n);. }. fpr
90400 69 6e 74 66 28 6f 75 74 2c 20 22 6d 65 6d 35 2e intf(out, "mem5.
90410 6e 41 6c 6c 6f 63 20 20 20 20 20 20 20 3d 20 25 nAlloc = %
90420 6c 6c 75 5c 6e 22 2c 20 6d 65 6d 35 2e 6e 41 6c llu\n", mem5.nAl
90430 6c 6f 63 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 loc);. fprintf(
90440 6f 75 74 2c 20 22 6d 65 6d 35 2e 74 6f 74 61 6c out, "mem5.total
90450 41 6c 6c 6f 63 20 20 20 3d 20 25 6c 6c 75 5c 6e Alloc = %llu\n
90460 22 2c 20 6d 65 6d 35 2e 74 6f 74 61 6c 41 6c 6c ", mem5.totalAll
90470 6f 63 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f oc);. fprintf(o
90480 75 74 2c 20 22 6d 65 6d 35 2e 74 6f 74 61 6c 45 ut, "mem5.totalE
90490 78 63 65 73 73 20 20 3d 20 25 6c 6c 75 5c 6e 22 xcess = %llu\n"
904a0 2c 20 6d 65 6d 35 2e 74 6f 74 61 6c 45 78 63 65 , mem5.totalExce
904b0 73 73 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f ss);. fprintf(o
904c0 75 74 2c 20 22 6d 65 6d 35 2e 63 75 72 72 65 6e ut, "mem5.curren
904d0 74 4f 75 74 20 20 20 3d 20 25 75 5c 6e 22 2c 20 tOut = %u\n",
904e0 6d 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74 29 mem5.currentOut)
904f0 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c ;. fprintf(out,
90500 20 22 6d 65 6d 35 2e 63 75 72 72 65 6e 74 43 6f "mem5.currentCo
90510 75 6e 74 20 3d 20 25 75 5c 6e 22 2c 20 6d 65 6d unt = %u\n", mem
90520 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 29 3b 5.currentCount);
90530 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 . fprintf(out,
90540 22 6d 65 6d 35 2e 6d 61 78 4f 75 74 20 20 20 20 "mem5.maxOut
90550 20 20 20 3d 20 25 75 5c 6e 22 2c 20 6d 65 6d 35 = %u\n", mem5
90560 2e 6d 61 78 4f 75 74 29 3b 0a 20 20 66 70 72 69 .maxOut);. fpri
90570 6e 74 66 28 6f 75 74 2c 20 22 6d 65 6d 35 2e 6d ntf(out, "mem5.m
90580 61 78 43 6f 75 6e 74 20 20 20 20 20 3d 20 25 75 axCount = %u
90590 5c 6e 22 2c 20 6d 65 6d 35 2e 6d 61 78 43 6f 75 \n", mem5.maxCou
905a0 6e 74 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f nt);. fprintf(o
905b0 75 74 2c 20 22 6d 65 6d 35 2e 6d 61 78 52 65 71 ut, "mem5.maxReq
905c0 75 65 73 74 20 20 20 3d 20 25 75 5c 6e 22 2c 20 uest = %u\n",
905d0 6d 65 6d 35 2e 6d 61 78 52 65 71 75 65 73 74 29 mem5.maxRequest)
905e0 3b 0a 20 20 6d 65 6d 73 79 73 35 4c 65 61 76 65 ;. memsys5Leave
905f0 28 29 3b 0a 20 20 69 66 28 20 6f 75 74 3d 3d 73 ();. if( out==s
90600 74 64 6f 75 74 20 29 7b 0a 20 20 20 20 66 66 6c tdout ){. ffl
90610 75 73 68 28 73 74 64 6f 75 74 29 3b 0a 20 20 7d ush(stdout);. }
90620 65 6c 73 65 7b 0a 20 20 20 20 66 63 6c 6f 73 65 else{. fclose
90630 28 6f 75 74 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e (out);. }.}.#en
90640 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 dif../*.** This
90650 72 6f 75 74 69 6e 65 20 69 73 20 74 68 65 20 6f routine is the o
90660 6e 6c 79 20 72 6f 75 74 69 6e 65 20 69 6e 20 74 nly routine in t
90670 68 69 73 20 66 69 6c 65 20 77 69 74 68 20 65 78 his file with ex
90680 74 65 72 6e 61 6c 20 0a 2a 2a 20 6c 69 6e 6b 61 ternal .** linka
90690 67 65 2e 20 49 74 20 72 65 74 75 72 6e 73 20 61 ge. It returns a
906a0 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74 pointer to a st
906b0 61 74 69 63 20 73 71 6c 69 74 65 33 5f 6d 65 6d atic sqlite3_mem
906c0 5f 6d 65 74 68 6f 64 73 0a 2a 2a 20 73 74 72 75 _methods.** stru
906d0 63 74 20 70 6f 70 75 6c 61 74 65 64 20 77 69 74 ct populated wit
906e0 68 20 74 68 65 20 6d 65 6d 73 79 73 35 20 6d 65 h the memsys5 me
906f0 74 68 6f 64 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 thods..*/.SQLITE
90700 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 73 _PRIVATE const s
90710 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f qlite3_mem_metho
90720 64 73 20 2a 73 71 6c 69 74 65 33 4d 65 6d 47 65 ds *sqlite3MemGe
90730 74 4d 65 6d 73 79 73 35 28 76 6f 69 64 29 7b 0a tMemsys5(void){.
90740 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 static const s
90750 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f qlite3_mem_metho
90760 64 73 20 6d 65 6d 73 79 73 35 4d 65 74 68 6f 64 ds memsys5Method
90770 73 20 3d 20 7b 0a 20 20 20 20 20 6d 65 6d 73 79 s = {. memsy
90780 73 35 4d 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 6d s5Malloc,. m
90790 65 6d 73 79 73 35 46 72 65 65 2c 0a 20 20 20 20 emsys5Free,.
907a0 20 6d 65 6d 73 79 73 35 52 65 61 6c 6c 6f 63 2c memsys5Realloc,
907b0 0a 20 20 20 20 20 6d 65 6d 73 79 73 35 53 69 7a . memsys5Siz
907c0 65 2c 0a 20 20 20 20 20 6d 65 6d 73 79 73 35 52 e,. memsys5R
907d0 6f 75 6e 64 75 70 2c 0a 20 20 20 20 20 6d 65 6d oundup,. mem
907e0 73 79 73 35 49 6e 69 74 2c 0a 20 20 20 20 20 6d sys5Init,. m
907f0 65 6d 73 79 73 35 53 68 75 74 64 6f 77 6e 2c 0a emsys5Shutdown,.
90800 20 20 20 20 20 30 0a 20 20 7d 3b 0a 20 20 72 65 0. };. re
90810 74 75 72 6e 20 26 6d 65 6d 73 79 73 35 4d 65 74 turn &memsys5Met
90820 68 6f 64 73 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 hods;.}..#endif
90830 2f 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 /* SQLITE_ENABLE
90840 5f 4d 45 4d 53 59 53 35 20 2a 2f 0a 0a 2f 2a 2a _MEMSYS5 */../**
90850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
90860 20 6f 66 20 6d 65 6d 35 2e 63 20 2a 2a 2a 2a 2a of mem5.c *****
90870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
90880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
90890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
908a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 ************ Beg
908b0 69 6e 20 66 69 6c 65 20 6d 75 74 65 78 2e 63 20 in file mutex.c
908c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
908d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
908e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a ***********/./*.
908f0 2a 2a 20 32 30 30 37 20 41 75 67 75 73 74 20 31 ** 2007 August 1
90900 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 4.**.** The auth
90910 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
90920 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
90930 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
90940 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
90950 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
90960 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
90970 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
90980 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
90990 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
909a0 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
909b0 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
909c0 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
909d0 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
909e0 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
909f0 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
90a00 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
90a10 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
90a20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
90a30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
90a40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
90a50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
90a60 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f .** This file co
90a70 6e 74 61 69 6e 73 20 74 68 65 20 43 20 66 75 6e ntains the C fun
90a80 63 74 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c ctions that impl
90a90 65 6d 65 6e 74 20 6d 75 74 65 78 65 73 2e 0a 2a ement mutexes..*
90aa0 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 *.** This file c
90ab0 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 74 68 61 ontains code tha
90ac0 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 61 63 72 6f t is common acro
90ad0 73 73 20 61 6c 6c 20 6d 75 74 65 78 20 69 6d 70 ss all mutex imp
90ae0 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 0a 2a lementations...*
90af0 2a 0a 2a 2a 20 24 49 64 3a 20 6d 75 74 65 78 2e *.** $Id: mutex.
90b00 63 2c 76 20 31 2e 33 31 20 32 30 30 39 2f 30 37 c,v 1.31 2009/07
90b10 2f 31 36 20 31 38 3a 32 31 3a 31 38 20 64 72 68 /16 18:21:18 drh
90b20 20 45 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 20 64 Exp $.*/..#if d
90b30 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 efined(SQLITE_DE
90b40 42 55 47 29 20 26 26 20 21 64 65 66 69 6e 65 64 BUG) && !defined
90b50 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 4d (SQLITE_MUTEX_OM
90b60 49 54 29 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 64 65 IT)./*.** For de
90b70 62 75 67 67 69 6e 67 20 70 75 72 70 6f 73 65 73 bugging purposes
90b80 2c 20 72 65 63 6f 72 64 20 77 68 65 6e 20 74 68 , record when th
90b90 65 20 6d 75 74 65 78 20 73 75 62 73 79 73 74 65 e mutex subsyste
90ba0 6d 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 m is initialized
90bb0 0a 2a 2a 20 61 6e 64 20 75 6e 69 6e 69 74 69 61 .** and uninitia
90bc0 6c 69 7a 65 64 20 73 6f 20 74 68 61 74 20 77 65 lized so that we
90bd0 20 63 61 6e 20 61 73 73 65 72 74 28 29 20 69 66 can assert() if
90be0 20 74 68 65 72 65 20 69 73 20 61 6e 20 61 74 74 there is an att
90bf0 65 6d 70 74 20 74 6f 0a 2a 2a 20 61 6c 6c 6f 63 empt to.** alloc
90c00 61 74 65 20 61 20 6d 75 74 65 78 20 77 68 69 6c ate a mutex whil
90c10 65 20 74 68 65 20 73 79 73 74 65 6d 20 69 73 20 e the system is
90c20 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 2e 0a 2a uninitialized..*
90c30 2f 0a 73 74 61 74 69 63 20 53 51 4c 49 54 45 5f /.static SQLITE_
90c40 57 53 44 20 69 6e 74 20 6d 75 74 65 78 49 73 49 WSD int mutexIsI
90c50 6e 69 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 20 nit = 0;.#endif
90c60 2f 2a 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 /* SQLITE_DEBUG
90c70 2a 2f 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c */...#ifndef SQL
90c80 49 54 45 5f 4d 55 54 45 58 5f 4f 4d 49 54 0a 2f ITE_MUTEX_OMIT./
90c90 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 *.** Initialize
90ca0 74 68 65 20 6d 75 74 65 78 20 73 79 73 74 65 6d the mutex system
90cb0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
90cc0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d ATE int sqlite3M
90cd0 75 74 65 78 49 6e 69 74 28 76 6f 69 64 29 7b 20 utexInit(void){
90ce0 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
90cf0 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 73 71 6c TE_OK;. if( sql
90d00 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
90d10 2e 62 43 6f 72 65 4d 75 74 65 78 20 29 7b 0a 20 .bCoreMutex ){.
90d20 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 47 if( !sqlite3G
90d30 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 lobalConfig.mute
90d40 78 2e 78 4d 75 74 65 78 41 6c 6c 6f 63 20 29 7b x.xMutexAlloc ){
90d50 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 . /* If the
90d60 20 78 4d 75 74 65 78 41 6c 6c 6f 63 20 6d 65 74 xMutexAlloc met
90d70 68 6f 64 20 68 61 73 20 6e 6f 74 20 62 65 65 6e hod has not been
90d80 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20 75 set, then the u
90d90 73 65 72 20 64 69 64 20 6e 6f 74 0a 20 20 20 20 ser did not.
90da0 20 20 2a 2a 20 69 6e 73 74 61 6c 6c 20 61 20 6d ** install a m
90db0 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 utex implementat
90dc0 69 6f 6e 20 76 69 61 20 73 71 6c 69 74 65 33 5f ion via sqlite3_
90dd0 63 6f 6e 66 69 67 28 29 20 70 72 69 6f 72 20 74 config() prior t
90de0 6f 20 0a 20 20 20 20 20 20 2a 2a 20 73 71 6c 69 o . ** sqli
90df0 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 te3_initialize()
90e00 20 62 65 69 6e 67 20 63 61 6c 6c 65 64 2e 20 54 being called. T
90e10 68 69 73 20 62 6c 6f 63 6b 20 63 6f 70 69 65 73 his block copies
90e20 20 70 6f 69 6e 74 65 72 73 20 74 6f 0a 20 20 20 pointers to.
90e30 20 20 20 2a 2a 20 74 68 65 20 64 65 66 61 75 6c ** the defaul
90e40 74 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e t implementation
90e50 20 69 6e 74 6f 20 74 68 65 20 73 71 6c 69 74 65 into the sqlite
90e60 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 20 73 74 3GlobalConfig st
90e70 72 75 63 74 75 72 65 2e 0a 20 20 20 20 20 20 2a ructure.. *
90e80 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f /. sqlite3_
90e90 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 2a 70 mutex_methods *p
90ea0 46 72 6f 6d 20 3d 20 73 71 6c 69 74 65 33 44 65 From = sqlite3De
90eb0 66 61 75 6c 74 4d 75 74 65 78 28 29 3b 0a 20 20 faultMutex();.
90ec0 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 sqlite3_mute
90ed0 78 5f 6d 65 74 68 6f 64 73 20 2a 70 54 6f 20 3d x_methods *pTo =
90ee0 20 26 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 &sqlite3GlobalC
90ef0 6f 6e 66 69 67 2e 6d 75 74 65 78 3b 0a 0a 20 20 onfig.mutex;..
90f00 20 20 20 20 6d 65 6d 63 70 79 28 70 54 6f 2c 20 memcpy(pTo,
90f10 70 46 72 6f 6d 2c 20 6f 66 66 73 65 74 6f 66 28 pFrom, offsetof(
90f20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 sqlite3_mutex_me
90f30 74 68 6f 64 73 2c 20 78 4d 75 74 65 78 41 6c 6c thods, xMutexAll
90f40 6f 63 29 29 3b 0a 20 20 20 20 20 20 6d 65 6d 63 oc));. memc
90f50 70 79 28 26 70 54 6f 2d 3e 78 4d 75 74 65 78 46 py(&pTo->xMutexF
90f60 72 65 65 2c 20 26 70 46 72 6f 6d 2d 3e 78 4d 75 ree, &pFrom->xMu
90f70 74 65 78 46 72 65 65 2c 0a 20 20 20 20 20 20 20 texFree,.
90f80 20 20 20 20 20 20 73 69 7a 65 6f 66 28 2a 70 54 sizeof(*pT
90f90 6f 29 20 2d 20 6f 66 66 73 65 74 6f 66 28 73 71 o) - offsetof(sq
90fa0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 lite3_mutex_meth
90fb0 6f 64 73 2c 20 78 4d 75 74 65 78 46 72 65 65 29 ods, xMutexFree)
90fc0 29 3b 0a 20 20 20 20 20 20 70 54 6f 2d 3e 78 4d );. pTo->xM
90fd0 75 74 65 78 41 6c 6c 6f 63 20 3d 20 70 46 72 6f utexAlloc = pFro
90fe0 6d 2d 3e 78 4d 75 74 65 78 41 6c 6c 6f 63 3b 0a m->xMutexAlloc;.
90ff0 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 }. rc = s
91000 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
91010 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 49 ig.mutex.xMutexI
91020 6e 69 74 28 29 3b 0a 20 20 7d 0a 0a 23 69 66 64 nit();. }..#ifd
91030 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a ef SQLITE_DEBUG.
91040 20 20 47 4c 4f 42 41 4c 28 69 6e 74 2c 20 6d 75 GLOBAL(int, mu
91050 74 65 78 49 73 49 6e 69 74 29 20 3d 20 31 3b 0a texIsInit) = 1;.
91060 23 65 6e 64 69 66 0a 0a 20 20 72 65 74 75 72 6e #endif.. return
91070 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 68 rc;.}../*.** Sh
91080 75 74 64 6f 77 6e 20 74 68 65 20 6d 75 74 65 78 utdown the mutex
91090 20 73 79 73 74 65 6d 2e 20 54 68 69 73 20 63 61 system. This ca
910a0 6c 6c 20 66 72 65 65 73 20 72 65 73 6f 75 72 63 ll frees resourc
910b0 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 0a es allocated by.
910c0 2a 2a 20 73 71 6c 69 74 65 33 4d 75 74 65 78 49 ** sqlite3MutexI
910d0 6e 69 74 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 nit()..*/.SQLITE
910e0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
910f0 69 74 65 33 4d 75 74 65 78 45 6e 64 28 76 6f 69 ite3MutexEnd(voi
91100 64 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 d){. int rc = S
91110 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 QLITE_OK;. if(
91120 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
91130 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 fig.mutex.xMutex
91140 45 6e 64 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 End ){. rc =
91150 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
91160 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 fig.mutex.xMutex
91170 45 6e 64 28 29 3b 0a 20 20 7d 0a 0a 23 69 66 64 End();. }..#ifd
91180 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a ef SQLITE_DEBUG.
91190 20 20 47 4c 4f 42 41 4c 28 69 6e 74 2c 20 6d 75 GLOBAL(int, mu
911a0 74 65 78 49 73 49 6e 69 74 29 20 3d 20 30 3b 0a texIsInit) = 0;.
911b0 23 65 6e 64 69 66 0a 0a 20 20 72 65 74 75 72 6e #endif.. return
911c0 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 rc;.}../*.** Re
911d0 74 72 69 65 76 65 20 61 20 70 6f 69 6e 74 65 72 trieve a pointer
911e0 20 74 6f 20 61 20 73 74 61 74 69 63 20 6d 75 74 to a static mut
911f0 65 78 20 6f 72 20 61 6c 6c 6f 63 61 74 65 20 61 ex or allocate a
91200 20 6e 65 77 20 64 79 6e 61 6d 69 63 20 6f 6e 65 new dynamic one
91210 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
91220 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 73 sqlite3_mutex *s
91230 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c qlite3_mutex_all
91240 6f 63 28 69 6e 74 20 69 64 29 7b 0a 23 69 66 6e oc(int id){.#ifn
91250 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
91260 41 55 54 4f 49 4e 49 54 0a 20 20 69 66 28 20 73 AUTOINIT. if( s
91270 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a qlite3_initializ
91280 65 28 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a e() ) return 0;.
91290 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 #endif. return
912a0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
912b0 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 fig.mutex.xMutex
912c0 41 6c 6c 6f 63 28 69 64 29 3b 0a 7d 0a 0a 53 51 Alloc(id);.}..SQ
912d0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c LITE_PRIVATE sql
912e0 69 74 65 33 5f 6d 75 74 65 78 20 2a 73 71 6c 69 ite3_mutex *sqli
912f0 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 69 6e te3MutexAlloc(in
91300 74 20 69 64 29 7b 0a 20 20 69 66 28 20 21 73 71 t id){. if( !sq
91310 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
91320 67 2e 62 43 6f 72 65 4d 75 74 65 78 20 29 7b 0a g.bCoreMutex ){.
91330 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 return 0;.
91340 7d 0a 20 20 61 73 73 65 72 74 28 20 47 4c 4f 42 }. assert( GLOB
91350 41 4c 28 69 6e 74 2c 20 6d 75 74 65 78 49 73 49 AL(int, mutexIsI
91360 6e 69 74 29 20 29 3b 0a 20 20 72 65 74 75 72 6e nit) );. return
91370 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
91380 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 nfig.mutex.xMute
91390 78 41 6c 6c 6f 63 28 69 64 29 3b 0a 7d 0a 0a 2f xAlloc(id);.}../
913a0 2a 0a 2a 2a 20 46 72 65 65 20 61 20 64 79 6e 61 *.** Free a dyna
913b0 6d 69 63 20 6d 75 74 65 78 2e 0a 2a 2f 0a 53 51 mic mutex..*/.SQ
913c0 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 LITE_API void sq
913d0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 66 72 65 65 lite3_mutex_free
913e0 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a (sqlite3_mutex *
913f0 70 29 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 p){. if( p ){.
91400 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c sqlite3Global
91410 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 Config.mutex.xMu
91420 74 65 78 46 72 65 65 28 70 29 3b 0a 20 20 7d 0a texFree(p);. }.
91430 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 62 74 61 69 6e 20 }../*.** Obtain
91440 74 68 65 20 6d 75 74 65 78 20 70 2e 20 49 66 20 the mutex p. If
91450 73 6f 6d 65 20 6f 74 68 65 72 20 74 68 72 65 61 some other threa
91460 64 20 61 6c 72 65 61 64 79 20 68 61 73 20 74 68 d already has th
91470 65 20 6d 75 74 65 78 2c 20 62 6c 6f 63 6b 0a 2a e mutex, block.*
91480 2a 20 75 6e 74 69 6c 20 69 74 20 63 61 6e 20 62 * until it can b
91490 65 20 6f 62 74 61 69 6e 65 64 2e 0a 2a 2f 0a 53 e obtained..*/.S
914a0 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 QLITE_API void s
914b0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 qlite3_mutex_ent
914c0 65 72 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 er(sqlite3_mutex
914d0 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 29 7b *p){. if( p ){
914e0 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 . sqlite3Glob
914f0 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 alConfig.mutex.x
91500 4d 75 74 65 78 45 6e 74 65 72 28 70 29 3b 0a 20 MutexEnter(p);.
91510 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 62 74 61 }.}../*.** Obta
91520 69 6e 20 74 68 65 20 6d 75 74 65 78 20 70 2e 20 in the mutex p.
91530 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 72 If successful, r
91540 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e eturn SQLITE_OK.
91550 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 61 Otherwise, if a
91560 6e 6f 74 68 65 72 0a 2a 2a 20 74 68 72 65 61 64 nother.** thread
91570 20 68 6f 6c 64 73 20 74 68 65 20 6d 75 74 65 78 holds the mutex
91580 20 61 6e 64 20 69 74 20 63 61 6e 6e 6f 74 20 62 and it cannot b
91590 65 20 6f 62 74 61 69 6e 65 64 2c 20 72 65 74 75 e obtained, retu
915a0 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 0a rn SQLITE_BUSY..
915b0 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
915c0 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f t sqlite3_mutex_
915d0 74 72 79 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 try(sqlite3_mute
915e0 78 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 20 x *p){. int rc
915f0 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 = SQLITE_OK;. i
91600 66 28 20 70 20 29 7b 0a 20 20 20 20 72 65 74 75 f( p ){. retu
91610 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c rn sqlite3Global
91620 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 Config.mutex.xMu
91630 74 65 78 54 72 79 28 70 29 3b 0a 20 20 7d 0a 20 texTry(p);. }.
91640 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
91650 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
91660 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 20 72 _mutex_leave() r
91670 6f 75 74 69 6e 65 20 65 78 69 74 73 20 61 20 6d outine exits a m
91680 75 74 65 78 20 74 68 61 74 20 77 61 73 20 70 72 utex that was pr
91690 65 76 69 6f 75 73 6c 79 0a 2a 2a 20 65 6e 74 65 eviously.** ente
916a0 72 65 64 20 62 79 20 74 68 65 20 73 61 6d 65 20 red by the same
916b0 74 68 72 65 61 64 2e 20 20 54 68 65 20 62 65 68 thread. The beh
916c0 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e avior is undefin
916d0 65 64 20 69 66 20 74 68 65 20 6d 75 74 65 78 20 ed if the mutex
916e0 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 75 72 72 65 .** is not curre
916f0 6e 74 6c 79 20 65 6e 74 65 72 65 64 2e 20 49 66 ntly entered. If
91700 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 a NULL pointer
91710 69 73 20 70 61 73 73 65 64 20 61 73 20 61 6e 20 is passed as an
91720 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 68 69 73 argument.** this
91730 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e function is a n
91740 6f 2d 6f 70 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f o-op..*/.SQLITE_
91750 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 API void sqlite3
91760 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 73 71 6c _mutex_leave(sql
91770 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a ite3_mutex *p){.
91780 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 73 if( p ){. s
91790 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
917a0 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 4c ig.mutex.xMutexL
917b0 65 61 76 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a eave(p);. }.}..
917c0 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 2f #ifndef NDEBUG./
917d0 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
917e0 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 20 61 6e _mutex_held() an
917f0 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f d sqlite3_mutex_
91800 6e 6f 74 68 65 6c 64 28 29 20 72 6f 75 74 69 6e notheld() routin
91810 65 20 61 72 65 0a 2a 2a 20 69 6e 74 65 6e 64 65 e are.** intende
91820 64 20 66 6f 72 20 75 73 65 20 69 6e 73 69 64 65 d for use inside
91830 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d assert() statem
91840 65 6e 74 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ents..*/.SQLITE_
91850 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
91860 6d 75 74 65 78 5f 68 65 6c 64 28 73 71 6c 69 74 mutex_held(sqlit
91870 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 e3_mutex *p){.
91880 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 73 return p==0 || s
91890 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
918a0 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 48 ig.mutex.xMutexH
918b0 65 6c 64 28 70 29 3b 0a 7d 0a 53 51 4c 49 54 45 eld(p);.}.SQLITE
918c0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
918d0 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 73 _mutex_notheld(s
918e0 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 qlite3_mutex *p)
918f0 7b 0a 20 20 72 65 74 75 72 6e 20 70 3d 3d 30 20 {. return p==0
91900 7c 7c 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c || sqlite3Global
91910 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 Config.mutex.xMu
91920 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 3b 0a 7d texNotheld(p);.}
91930 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20 .#endif..#endif
91940 2f 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f /* SQLITE_MUTEX_
91950 4f 4d 49 54 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a OMIT */../******
91960 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 ******** End of
91970 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a mutex.c ********
91980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
91990 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
919a0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
919b0 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 ******** Begin f
919c0 69 6c 65 20 6d 75 74 65 78 5f 6e 6f 6f 70 2e 63 ile mutex_noop.c
919d0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
919e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
919f0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 *******/./*.** 2
91a00 30 30 38 20 4f 63 74 6f 62 65 72 20 30 37 0a 2a 008 October 07.*
91a10 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 *.** The author
91a20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 disclaims copyri
91a30 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 ght to this sour
91a40 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 ce code. In pla
91a50 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c ce of.** a legal
91a60 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 notice, here is
91a70 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a a blessing:.**.
91a80 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f ** May you do
91a90 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 good and not ev
91aa0 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f il..** May yo
91ab0 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 u find forgivene
91ac0 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 ss for yourself
91ad0 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 and forgive othe
91ae0 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f rs..** May yo
91af0 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 u share freely,
91b00 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 never taking mor
91b10 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e e than you give.
91b20 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .**.************
91b30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
91b40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
91b50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
91b60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
91b70 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 This file conta
91b80 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63 74 69 ins the C functi
91b90 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 ons that impleme
91ba0 6e 74 20 6d 75 74 65 78 65 73 2e 0a 2a 2a 0a 2a nt mutexes..**.*
91bb0 2a 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 * This implement
91bc0 61 74 69 6f 6e 20 69 6e 20 74 68 69 73 20 66 69 ation in this fi
91bd0 6c 65 20 64 6f 65 73 20 6e 6f 74 20 70 72 6f 76 le does not prov
91be0 69 64 65 20 61 6e 79 20 6d 75 74 75 61 6c 0a 2a ide any mutual.*
91bf0 2a 20 65 78 63 6c 75 73 69 6f 6e 20 61 6e 64 20 * exclusion and
91c00 69 73 20 74 68 75 73 20 73 75 69 74 61 62 6c 65 is thus suitable
91c10 20 66 6f 72 20 75 73 65 20 6f 6e 6c 79 20 69 6e for use only in
91c20 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a applications.**
91c30 20 74 68 61 74 20 75 73 65 20 53 51 4c 69 74 65 that use SQLite
91c40 20 69 6e 20 61 20 73 69 6e 67 6c 65 20 74 68 72 in a single thr
91c50 65 61 64 2e 20 20 54 68 65 20 72 6f 75 74 69 6e ead. The routin
91c60 65 73 20 64 65 66 69 6e 65 64 0a 2a 2a 20 68 65 es defined.** he
91c70 72 65 20 61 72 65 20 70 6c 61 63 65 2d 68 6f 6c re are place-hol
91c80 64 65 72 73 2e 20 20 41 70 70 6c 69 63 61 74 69 ders. Applicati
91c90 6f 6e 73 20 63 61 6e 20 73 75 62 73 74 69 74 75 ons can substitu
91ca0 74 65 20 77 6f 72 6b 69 6e 67 0a 2a 2a 20 6d 75 te working.** mu
91cb0 74 65 78 20 72 6f 75 74 69 6e 65 73 20 61 74 20 tex routines at
91cc0 73 74 61 72 74 2d 74 69 6d 65 20 75 73 69 6e 67 start-time using
91cd0 20 74 68 65 0a 2a 2a 0a 2a 2a 20 20 20 20 20 73 the.**.** s
91ce0 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 53 51 qlite3_config(SQ
91cf0 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 54 45 LITE_CONFIG_MUTE
91d00 58 2c 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 69 6e 74 X,...).**.** int
91d10 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 erface..**.** If
91d20 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 53 compiled with S
91d30 51 4c 49 54 45 5f 44 45 42 55 47 2c 20 74 68 65 QLITE_DEBUG, the
91d40 6e 20 61 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 67 n additional log
91d50 69 63 20 69 73 20 69 6e 73 65 72 74 65 64 0a 2a ic is inserted.*
91d60 2a 20 74 68 61 74 20 64 6f 65 73 20 65 72 72 6f * that does erro
91d70 72 20 63 68 65 63 6b 69 6e 67 20 6f 6e 20 6d 75 r checking on mu
91d80 74 65 78 65 73 20 74 6f 20 6d 61 6b 65 20 73 75 texes to make su
91d90 72 65 20 74 68 65 79 20 61 72 65 20 62 65 69 6e re they are bein
91da0 67 0a 2a 2a 20 63 61 6c 6c 65 64 20 63 6f 72 72 g.** called corr
91db0 65 63 74 6c 79 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 ectly..**.** $Id
91dc0 3a 20 6d 75 74 65 78 5f 6e 6f 6f 70 2e 63 2c 76 : mutex_noop.c,v
91dd0 20 31 2e 33 20 32 30 30 38 2f 31 32 2f 30 35 20 1.3 2008/12/05
91de0 31 37 3a 31 37 3a 30 38 20 64 72 68 20 45 78 70 17:17:08 drh Exp
91df0 20 24 0a 2a 2f 0a 0a 0a 23 69 66 20 64 65 66 69 $.*/...#if defi
91e00 6e 65 64 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 ned(SQLITE_MUTEX
91e10 5f 4e 4f 4f 50 29 20 26 26 20 21 64 65 66 69 6e _NOOP) && !defin
91e20 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 ed(SQLITE_DEBUG)
91e30 0a 2f 2a 0a 2a 2a 20 53 74 75 62 20 72 6f 75 74 ./*.** Stub rout
91e40 69 6e 65 73 20 66 6f 72 20 61 6c 6c 20 6d 75 74 ines for all mut
91e50 65 78 20 6d 65 74 68 6f 64 73 2e 0a 2a 2a 0a 2a ex methods..**.*
91e60 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 73 20 * This routines
91e70 70 72 6f 76 69 64 65 20 6e 6f 20 6d 75 74 75 61 provide no mutua
91e80 6c 20 65 78 63 6c 75 73 69 6f 6e 20 6f 72 20 65 l exclusion or e
91e90 72 72 6f 72 20 63 68 65 63 6b 69 6e 67 2e 0a 2a rror checking..*
91ea0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 6f 6f /.static int noo
91eb0 70 4d 75 74 65 78 48 65 6c 64 28 73 71 6c 69 74 pMutexHeld(sqlit
91ec0 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 20 72 65 e3_mutex *p){ re
91ed0 74 75 72 6e 20 31 3b 20 7d 0a 73 74 61 74 69 63 turn 1; }.static
91ee0 20 69 6e 74 20 6e 6f 6f 70 4d 75 74 65 78 4e 6f int noopMutexNo
91ef0 74 68 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 theld(sqlite3_mu
91f00 74 65 78 20 2a 70 29 7b 20 72 65 74 75 72 6e 20 tex *p){ return
91f10 31 3b 20 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 1; }.static int
91f20 6e 6f 6f 70 4d 75 74 65 78 49 6e 69 74 28 76 6f noopMutexInit(vo
91f30 69 64 29 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 id){ return SQLI
91f40 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 20 TE_OK; }.static
91f50 69 6e 74 20 6e 6f 6f 70 4d 75 74 65 78 45 6e 64 int noopMutexEnd
91f60 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 53 (void){ return S
91f70 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 QLITE_OK; }.stat
91f80 69 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ic sqlite3_mutex
91f90 20 2a 6e 6f 6f 70 4d 75 74 65 78 41 6c 6c 6f 63 *noopMutexAlloc
91fa0 28 69 6e 74 20 69 64 29 7b 20 72 65 74 75 72 6e (int id){ return
91fb0 20 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a (sqlite3_mutex*
91fc0 29 38 3b 20 7d 0a 73 74 61 74 69 63 20 76 6f 69 )8; }.static voi
91fd0 64 20 6e 6f 6f 70 4d 75 74 65 78 46 72 65 65 28 d noopMutexFree(
91fe0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 sqlite3_mutex *p
91ff0 29 7b 20 72 65 74 75 72 6e 3b 20 7d 0a 73 74 61 ){ return; }.sta
92000 74 69 63 20 76 6f 69 64 20 6e 6f 6f 70 4d 75 74 tic void noopMut
92010 65 78 45 6e 74 65 72 28 73 71 6c 69 74 65 33 5f exEnter(sqlite3_
92020 6d 75 74 65 78 20 2a 70 29 7b 20 72 65 74 75 72 mutex *p){ retur
92030 6e 3b 20 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 n; }.static int
92040 6e 6f 6f 70 4d 75 74 65 78 54 72 79 28 73 71 6c noopMutexTry(sql
92050 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 20 ite3_mutex *p){
92060 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
92070 3b 20 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 ; }.static void
92080 6e 6f 6f 70 4d 75 74 65 78 4c 65 61 76 65 28 73 noopMutexLeave(s
92090 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 qlite3_mutex *p)
920a0 7b 20 72 65 74 75 72 6e 3b 20 7d 0a 0a 53 51 4c { return; }..SQL
920b0 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69 ITE_PRIVATE sqli
920c0 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 te3_mutex_method
920d0 73 20 2a 73 71 6c 69 74 65 33 44 65 66 61 75 6c s *sqlite3Defaul
920e0 74 4d 75 74 65 78 28 76 6f 69 64 29 7b 0a 20 20 tMutex(void){.
920f0 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 6d static sqlite3_m
92100 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 73 4d 75 utex_methods sMu
92110 74 65 78 20 3d 20 7b 0a 20 20 20 20 6e 6f 6f 70 tex = {. noop
92120 4d 75 74 65 78 49 6e 69 74 2c 0a 20 20 20 20 6e MutexInit,. n
92130 6f 6f 70 4d 75 74 65 78 45 6e 64 2c 0a 20 20 20 oopMutexEnd,.
92140 20 6e 6f 6f 70 4d 75 74 65 78 41 6c 6c 6f 63 2c noopMutexAlloc,
92150 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78 46 72 . noopMutexFr
92160 65 65 2c 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 65 ee,. noopMute
92170 78 45 6e 74 65 72 2c 0a 20 20 20 20 6e 6f 6f 70 xEnter,. noop
92180 4d 75 74 65 78 54 72 79 2c 0a 20 20 20 20 6e 6f MutexTry,. no
92190 6f 70 4d 75 74 65 78 4c 65 61 76 65 2c 0a 0a 20 opMutexLeave,..
921a0 20 20 20 6e 6f 6f 70 4d 75 74 65 78 48 65 6c 64 noopMutexHeld
921b0 2c 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78 4e ,. noopMutexN
921c0 6f 74 68 65 6c 64 0a 20 20 7d 3b 0a 0a 20 20 72 otheld. };.. r
921d0 65 74 75 72 6e 20 26 73 4d 75 74 65 78 3b 0a 7d eturn &sMutex;.}
921e0 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65 66 69 6e .#endif /* defin
921f0 65 64 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f ed(SQLITE_MUTEX_
92200 4e 4f 4f 50 29 20 26 26 20 21 64 65 66 69 6e 65 NOOP) && !define
92210 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 20 d(SQLITE_DEBUG)
92220 2a 2f 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 */..#if defined(
92230 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f SQLITE_MUTEX_NOO
92240 50 29 20 26 26 20 64 65 66 69 6e 65 64 28 53 51 P) && defined(SQ
92250 4c 49 54 45 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a LITE_DEBUG)./*.*
92260 2a 20 49 6e 20 74 68 69 73 20 69 6d 70 6c 65 6d * In this implem
92270 65 6e 74 61 74 69 6f 6e 2c 20 65 72 72 6f 72 20 entation, error
92280 63 68 65 63 6b 69 6e 67 20 69 73 20 70 72 6f 76 checking is prov
92290 69 64 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 ided for testing
922a0 0a 2a 2a 20 61 6e 64 20 64 65 62 75 67 67 69 6e .** and debuggin
922b0 67 20 70 75 72 70 6f 73 65 73 2e 20 20 54 68 65 g purposes. The
922c0 20 6d 75 74 65 78 65 73 20 73 74 69 6c 6c 20 64 mutexes still d
922d0 6f 20 6e 6f 74 20 70 72 6f 76 69 64 65 20 61 6e o not provide an
922e0 79 0a 2a 2a 20 6d 75 74 75 61 6c 20 65 78 63 6c y.** mutual excl
922f0 75 73 69 6f 6e 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a usion..*/../*.**
92300 20 54 68 65 20 6d 75 74 65 78 20 6f 62 6a 65 63 The mutex objec
92310 74 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 t.*/.struct sqli
92320 74 65 33 5f 6d 75 74 65 78 20 7b 0a 20 20 69 6e te3_mutex {. in
92330 74 20 69 64 3b 20 20 20 20 20 2f 2a 20 54 68 65 t id; /* The
92340 20 6d 75 74 65 78 20 74 79 70 65 20 2a 2f 0a 20 mutex type */.
92350 20 69 6e 74 20 63 6e 74 3b 20 20 20 20 2f 2a 20 int cnt; /*
92360 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 Number of entrie
92370 73 20 77 69 74 68 6f 75 74 20 61 20 6d 61 74 63 s without a matc
92380 68 69 6e 67 20 6c 65 61 76 65 20 2a 2f 0a 7d 3b hing leave */.};
92390 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 ../*.** The sqli
923a0 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 te3_mutex_held()
923b0 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 and sqlite3_mut
923c0 65 78 5f 6e 6f 74 68 65 6c 64 28 29 20 72 6f 75 ex_notheld() rou
923d0 74 69 6e 65 20 61 72 65 0a 2a 2a 20 69 6e 74 65 tine are.** inte
923e0 6e 64 65 64 20 66 6f 72 20 75 73 65 20 69 6e 73 nded for use ins
923f0 69 64 65 20 61 73 73 65 72 74 28 29 20 73 74 61 ide assert() sta
92400 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 73 74 61 74 tements..*/.stat
92410 69 63 20 69 6e 74 20 64 65 62 75 67 4d 75 74 65 ic int debugMute
92420 78 48 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 xHeld(sqlite3_mu
92430 74 65 78 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 tex *p){. retur
92440 6e 20 70 3d 3d 30 20 7c 7c 20 70 2d 3e 63 6e 74 n p==0 || p->cnt
92450 3e 30 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 >0;.}.static int
92460 20 64 65 62 75 67 4d 75 74 65 78 4e 6f 74 68 65 debugMutexNothe
92470 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ld(sqlite3_mutex
92480 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 *p){. return p
92490 3d 3d 30 20 7c 7c 20 70 2d 3e 63 6e 74 3d 3d 30 ==0 || p->cnt==0
924a0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 ;.}../*.** Initi
924b0 61 6c 69 7a 65 20 61 6e 64 20 64 65 69 6e 69 74 alize and deinit
924c0 69 61 6c 69 7a 65 20 74 68 65 20 6d 75 74 65 78 ialize the mutex
924d0 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 73 subsystem..*/.s
924e0 74 61 74 69 63 20 69 6e 74 20 64 65 62 75 67 4d tatic int debugM
924f0 75 74 65 78 49 6e 69 74 28 76 6f 69 64 29 7b 20 utexInit(void){
92500 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
92510 3b 20 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 64 ; }.static int d
92520 65 62 75 67 4d 75 74 65 78 45 6e 64 28 76 6f 69 ebugMutexEnd(voi
92530 64 29 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 d){ return SQLIT
92540 45 5f 4f 4b 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20 54 E_OK; }../*.** T
92550 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 he sqlite3_mutex
92560 5f 61 6c 6c 6f 63 28 29 20 72 6f 75 74 69 6e 65 _alloc() routine
92570 20 61 6c 6c 6f 63 61 74 65 73 20 61 20 6e 65 77 allocates a new
92580 0a 2a 2a 20 6d 75 74 65 78 20 61 6e 64 20 72 65 .** mutex and re
92590 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 turns a pointer
925a0 74 6f 20 69 74 2e 20 20 49 66 20 69 74 20 72 65 to it. If it re
925b0 74 75 72 6e 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68 turns NULL.** th
925c0 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20 61 20 at means that a
925d0 6d 75 74 65 78 20 63 6f 75 6c 64 20 6e 6f 74 20 mutex could not
925e0 62 65 20 61 6c 6c 6f 63 61 74 65 64 2e 20 0a 2a be allocated. .*
925f0 2f 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 /.static sqlite3
92600 5f 6d 75 74 65 78 20 2a 64 65 62 75 67 4d 75 74 _mutex *debugMut
92610 65 78 41 6c 6c 6f 63 28 69 6e 74 20 69 64 29 7b exAlloc(int id){
92620 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 . static sqlite
92630 33 5f 6d 75 74 65 78 20 61 53 74 61 74 69 63 5b 3_mutex aStatic[
92640 36 5d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 6];. sqlite3_mu
92650 74 65 78 20 2a 70 4e 65 77 20 3d 20 30 3b 0a 20 tex *pNew = 0;.
92660 20 73 77 69 74 63 68 28 20 69 64 20 29 7b 0a 20 switch( id ){.
92670 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4d case SQLITE_M
92680 55 54 45 58 5f 46 41 53 54 3a 0a 20 20 20 20 63 UTEX_FAST:. c
92690 61 73 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 ase SQLITE_MUTEX
926a0 5f 52 45 43 55 52 53 49 56 45 3a 20 7b 0a 20 20 _RECURSIVE: {.
926b0 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 pNew = sqlit
926c0 65 33 4d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 e3Malloc(sizeof(
926d0 2a 70 4e 65 77 29 29 3b 0a 20 20 20 20 20 20 69 *pNew));. i
926e0 66 28 20 70 4e 65 77 20 29 7b 0a 20 20 20 20 20 f( pNew ){.
926f0 20 20 20 70 4e 65 77 2d 3e 69 64 20 3d 20 69 64 pNew->id = id
92700 3b 0a 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e ;. pNew->
92710 63 6e 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d cnt = 0;. }
92720 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
92730 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a }. default:
92740 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 {. assert(
92750 20 69 64 2d 32 20 3e 3d 20 30 20 29 3b 0a 20 20 id-2 >= 0 );.
92760 20 20 20 20 61 73 73 65 72 74 28 20 69 64 2d 32 assert( id-2
92770 20 3c 20 28 69 6e 74 29 28 73 69 7a 65 6f 66 28 < (int)(sizeof(
92780 61 53 74 61 74 69 63 29 2f 73 69 7a 65 6f 66 28 aStatic)/sizeof(
92790 61 53 74 61 74 69 63 5b 30 5d 29 29 20 29 3b 0a aStatic[0])) );.
927a0 20 20 20 20 20 20 70 4e 65 77 20 3d 20 26 61 53 pNew = &aS
927b0 74 61 74 69 63 5b 69 64 2d 32 5d 3b 0a 20 20 20 tatic[id-2];.
927c0 20 20 20 70 4e 65 77 2d 3e 69 64 20 3d 20 69 64 pNew->id = id
927d0 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
927e0 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
927f0 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a n pNew;.}../*.**
92800 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 65 This routine de
92810 61 6c 6c 6f 63 61 74 65 73 20 61 20 70 72 65 76 allocates a prev
92820 69 6f 75 73 6c 79 20 61 6c 6c 6f 63 61 74 65 64 iously allocated
92830 20 6d 75 74 65 78 2e 0a 2a 2f 0a 73 74 61 74 69 mutex..*/.stati
92840 63 20 76 6f 69 64 20 64 65 62 75 67 4d 75 74 65 c void debugMute
92850 78 46 72 65 65 28 73 71 6c 69 74 65 33 5f 6d 75 xFree(sqlite3_mu
92860 74 65 78 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 tex *p){. asser
92870 74 28 20 70 2d 3e 63 6e 74 3d 3d 30 20 29 3b 0a t( p->cnt==0 );.
92880 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64 3d assert( p->id=
92890 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 =SQLITE_MUTEX_FA
928a0 53 54 20 7c 7c 20 70 2d 3e 69 64 3d 3d 53 51 4c ST || p->id==SQL
928b0 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 ITE_MUTEX_RECURS
928c0 49 56 45 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 IVE );. sqlite3
928d0 5f 66 72 65 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a _free(p);.}../*.
928e0 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d ** The sqlite3_m
928f0 75 74 65 78 5f 65 6e 74 65 72 28 29 20 61 6e 64 utex_enter() and
92900 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 sqlite3_mutex_t
92910 72 79 28 29 20 72 6f 75 74 69 6e 65 73 20 61 74 ry() routines at
92920 74 65 6d 70 74 0a 2a 2a 20 74 6f 20 65 6e 74 65 tempt.** to ente
92930 72 20 61 20 6d 75 74 65 78 2e 20 20 49 66 20 61 r a mutex. If a
92940 6e 6f 74 68 65 72 20 74 68 72 65 61 64 20 69 73 nother thread is
92950 20 61 6c 72 65 61 64 79 20 77 69 74 68 69 6e 20 already within
92960 74 68 65 20 6d 75 74 65 78 2c 0a 2a 2a 20 73 71 the mutex,.** sq
92970 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 lite3_mutex_ente
92980 72 28 29 20 77 69 6c 6c 20 62 6c 6f 63 6b 20 61 r() will block a
92990 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 nd sqlite3_mutex
929a0 5f 74 72 79 28 29 20 77 69 6c 6c 20 72 65 74 75 _try() will retu
929b0 72 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 55 53 rn.** SQLITE_BUS
929c0 59 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f Y. The sqlite3_
929d0 6d 75 74 65 78 5f 74 72 79 28 29 20 69 6e 74 65 mutex_try() inte
929e0 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 53 51 rface returns SQ
929f0 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 75 70 6f 6e 20 LITE_OK.** upon
92a00 73 75 63 63 65 73 73 66 75 6c 20 65 6e 74 72 79 successful entry
92a10 2e 20 20 4d 75 74 65 78 65 73 20 63 72 65 61 74 . Mutexes creat
92a20 65 64 20 75 73 69 6e 67 20 53 51 4c 49 54 45 5f ed using SQLITE_
92a30 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 MUTEX_RECURSIVE
92a40 63 61 6e 0a 2a 2a 20 62 65 20 65 6e 74 65 72 65 can.** be entere
92a50 64 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 d multiple times
92a60 20 62 79 20 74 68 65 20 73 61 6d 65 20 74 68 72 by the same thr
92a70 65 61 64 2e 20 20 49 6e 20 73 75 63 68 20 63 61 ead. In such ca
92a80 73 65 73 20 74 68 65 2c 0a 2a 2a 20 6d 75 74 65 ses the,.** mute
92a90 78 20 6d 75 73 74 20 62 65 20 65 78 69 74 65 64 x must be exited
92aa0 20 61 6e 20 65 71 75 61 6c 20 6e 75 6d 62 65 72 an equal number
92ab0 20 6f 66 20 74 69 6d 65 73 20 62 65 66 6f 72 65 of times before
92ac0 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 0a another thread.
92ad0 2a 2a 20 63 61 6e 20 65 6e 74 65 72 2e 20 20 49 ** can enter. I
92ae0 66 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 f the same threa
92af0 64 20 74 72 69 65 73 20 74 6f 20 65 6e 74 65 72 d tries to enter
92b00 20 61 6e 79 20 6f 74 68 65 72 20 6b 69 6e 64 20 any other kind
92b10 6f 66 20 6d 75 74 65 78 0a 2a 2a 20 6d 6f 72 65 of mutex.** more
92b20 20 74 68 61 6e 20 6f 6e 63 65 2c 20 74 68 65 20 than once, the
92b30 62 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65 behavior is unde
92b40 66 69 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 fined..*/.static
92b50 20 76 6f 69 64 20 64 65 62 75 67 4d 75 74 65 78 void debugMutex
92b60 45 6e 74 65 72 28 73 71 6c 69 74 65 33 5f 6d 75 Enter(sqlite3_mu
92b70 74 65 78 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 tex *p){. asser
92b80 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 t( p->id==SQLITE
92b90 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 _MUTEX_RECURSIVE
92ba0 20 7c 7c 20 64 65 62 75 67 4d 75 74 65 78 4e 6f || debugMutexNo
92bb0 74 68 65 6c 64 28 70 29 20 29 3b 0a 20 20 70 2d theld(p) );. p-
92bc0 3e 63 6e 74 2b 2b 3b 0a 7d 0a 73 74 61 74 69 63 >cnt++;.}.static
92bd0 20 69 6e 74 20 64 65 62 75 67 4d 75 74 65 78 54 int debugMutexT
92be0 72 79 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ry(sqlite3_mutex
92bf0 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 *p){. assert(
92c00 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 p->id==SQLITE_MU
92c10 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 7c 7c TEX_RECURSIVE ||
92c20 20 64 65 62 75 67 4d 75 74 65 78 4e 6f 74 68 65 debugMutexNothe
92c30 6c 64 28 70 29 20 29 3b 0a 20 20 70 2d 3e 63 6e ld(p) );. p->cn
92c40 74 2b 2b 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 t++;. return SQ
92c50 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a LITE_OK;.}../*.*
92c60 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 * The sqlite3_mu
92c70 74 65 78 5f 6c 65 61 76 65 28 29 20 72 6f 75 74 tex_leave() rout
92c80 69 6e 65 20 65 78 69 74 73 20 61 20 6d 75 74 65 ine exits a mute
92c90 78 20 74 68 61 74 20 77 61 73 0a 2a 2a 20 70 72 x that was.** pr
92ca0 65 76 69 6f 75 73 6c 79 20 65 6e 74 65 72 65 64 eviously entered
92cb0 20 62 79 20 74 68 65 20 73 61 6d 65 20 74 68 72 by the same thr
92cc0 65 61 64 2e 20 20 54 68 65 20 62 65 68 61 76 69 ead. The behavi
92cd0 6f 72 0a 2a 2a 20 69 73 20 75 6e 64 65 66 69 6e or.** is undefin
92ce0 65 64 20 69 66 20 74 68 65 20 6d 75 74 65 78 20 ed if the mutex
92cf0 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 is not currently
92d00 20 65 6e 74 65 72 65 64 20 6f 72 0a 2a 2a 20 69 entered or.** i
92d10 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 s not currently
92d20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 53 51 4c 69 allocated. SQLi
92d30 74 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 64 6f te will never do
92d40 20 65 69 74 68 65 72 2e 0a 2a 2f 0a 73 74 61 74 either..*/.stat
92d50 69 63 20 76 6f 69 64 20 64 65 62 75 67 4d 75 74 ic void debugMut
92d60 65 78 4c 65 61 76 65 28 73 71 6c 69 74 65 33 5f exLeave(sqlite3_
92d70 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 73 73 mutex *p){. ass
92d80 65 72 74 28 20 64 65 62 75 67 4d 75 74 65 78 48 ert( debugMutexH
92d90 65 6c 64 28 70 29 20 29 3b 0a 20 20 70 2d 3e 63 eld(p) );. p->c
92da0 6e 74 2d 2d 3b 0a 20 20 61 73 73 65 72 74 28 20 nt--;. assert(
92db0 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 p->id==SQLITE_MU
92dc0 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 7c 7c TEX_RECURSIVE ||
92dd0 20 64 65 62 75 67 4d 75 74 65 78 4e 6f 74 68 65 debugMutexNothe
92de0 6c 64 28 70 29 20 29 3b 0a 7d 0a 0a 53 51 4c 49 ld(p) );.}..SQLI
92df0 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74 TE_PRIVATE sqlit
92e00 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 e3_mutex_methods
92e10 20 2a 73 71 6c 69 74 65 33 44 65 66 61 75 6c 74 *sqlite3Default
92e20 4d 75 74 65 78 28 76 6f 69 64 29 7b 0a 20 20 73 Mutex(void){. s
92e30 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 6d 75 tatic sqlite3_mu
92e40 74 65 78 5f 6d 65 74 68 6f 64 73 20 73 4d 75 74 tex_methods sMut
92e50 65 78 20 3d 20 7b 0a 20 20 20 20 64 65 62 75 67 ex = {. debug
92e60 4d 75 74 65 78 49 6e 69 74 2c 0a 20 20 20 20 64 MutexInit,. d
92e70 65 62 75 67 4d 75 74 65 78 45 6e 64 2c 0a 20 20 ebugMutexEnd,.
92e80 20 20 64 65 62 75 67 4d 75 74 65 78 41 6c 6c 6f debugMutexAllo
92e90 63 2c 0a 20 20 20 20 64 65 62 75 67 4d 75 74 65 c,. debugMute
92ea0 78 46 72 65 65 2c 0a 20 20 20 20 64 65 62 75 67 xFree,. debug
92eb0 4d 75 74 65 78 45 6e 74 65 72 2c 0a 20 20 20 20 MutexEnter,.
92ec0 64 65 62 75 67 4d 75 74 65 78 54 72 79 2c 0a 20 debugMutexTry,.
92ed0 20 20 20 64 65 62 75 67 4d 75 74 65 78 4c 65 61 debugMutexLea
92ee0 76 65 2c 0a 0a 20 20 20 20 64 65 62 75 67 4d 75 ve,.. debugMu
92ef0 74 65 78 48 65 6c 64 2c 0a 20 20 20 20 64 65 62 texHeld,. deb
92f00 75 67 4d 75 74 65 78 4e 6f 74 68 65 6c 64 0a 20 ugMutexNotheld.
92f10 20 7d 3b 0a 0a 20 20 72 65 74 75 72 6e 20 26 73 };.. return &s
92f20 4d 75 74 65 78 3b 0a 7d 0a 23 65 6e 64 69 66 20 Mutex;.}.#endif
92f30 2f 2a 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 /* defined(SQLIT
92f40 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 29 20 26 26 E_MUTEX_NOOP) &&
92f50 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f defined(SQLITE_
92f60 44 45 42 55 47 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a DEBUG) */../****
92f70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
92f80 66 20 6d 75 74 65 78 5f 6e 6f 6f 70 2e 63 20 2a f mutex_noop.c *
92f90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
92fa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
92fb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
92fc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
92fd0 20 66 69 6c 65 20 6d 75 74 65 78 5f 6f 73 32 2e file mutex_os2.
92fe0 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
92ff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
93000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a *********/./*.**
93010 20 32 30 30 37 20 41 75 67 75 73 74 20 32 38 0a 2007 August 28.
93020 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 **.** The author
93030 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 disclaims copyr
93040 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 ight to this sou
93050 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c rce code. In pl
93060 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 ace of.** a lega
93070 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 l notice, here i
93080 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a s a blessing:.**
93090 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 .** May you d
930a0 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 o good and not e
930b0 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 vil..** May y
930c0 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e ou find forgiven
930d0 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 ess for yourself
930e0 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 and forgive oth
930f0 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 ers..** May y
93100 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c ou share freely,
93110 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f never taking mo
93120 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 re than you give
93130 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ..**.***********
93140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
93150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
93160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
93170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
93180 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 * This file cont
93190 61 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63 74 ains the C funct
931a0 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 6d ions that implem
931b0 65 6e 74 20 6d 75 74 65 78 65 73 20 66 6f 72 20 ent mutexes for
931c0 4f 53 2f 32 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 OS/2.**.** $Id:
931d0 6d 75 74 65 78 5f 6f 73 32 2e 63 2c 76 20 31 2e mutex_os2.c,v 1.
931e0 31 31 20 32 30 30 38 2f 31 31 2f 32 32 20 31 39 11 2008/11/22 19
931f0 3a 35 30 3a 35 34 20 70 77 65 69 6c 62 61 63 68 :50:54 pweilbach
93200 65 72 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a er Exp $.*/../*.
93210 2a 2a 20 54 68 65 20 63 6f 64 65 20 69 6e 20 74 ** The code in t
93220 68 69 73 20 66 69 6c 65 20 69 73 20 6f 6e 6c 79 his file is only
93230 20 75 73 65 64 20 69 66 20 53 51 4c 49 54 45 5f used if SQLITE_
93240 4d 55 54 45 58 5f 4f 53 32 20 69 73 20 64 65 66 MUTEX_OS2 is def
93250 69 6e 65 64 2e 0a 2a 2a 20 53 65 65 20 74 68 65 ined..** See the
93260 20 6d 75 74 65 78 2e 68 20 66 69 6c 65 20 66 6f mutex.h file fo
93270 72 20 64 65 74 61 69 6c 73 2e 0a 2a 2f 0a 23 69 r details..*/.#i
93280 66 64 65 66 20 53 51 4c 49 54 45 5f 4d 55 54 45 fdef SQLITE_MUTE
93290 58 5f 4f 53 32 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a X_OS2../********
932a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 4f ************** O
932b0 53 2f 32 20 4d 75 74 65 78 20 49 6d 70 6c 65 6d S/2 Mutex Implem
932c0 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a entation *******
932d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
932e0 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6d 70 6c 65 **.** This imple
932f0 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 6d 75 74 mentation of mut
93300 65 78 65 73 20 69 73 20 62 75 69 6c 74 20 75 73 exes is built us
93310 69 6e 67 20 74 68 65 20 4f 53 2f 32 20 41 50 49 ing the OS/2 API
93320 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 ..*/../*.** The
93330 6d 75 74 65 78 20 6f 62 6a 65 63 74 0a 2a 2a 20 mutex object.**
93340 45 61 63 68 20 72 65 63 75 72 73 69 76 65 20 6d Each recursive m
93350 75 74 65 78 20 69 73 20 61 6e 20 69 6e 73 74 61 utex is an insta
93360 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f nce of the follo
93370 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a wing structure..
93380 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 */.struct sqlite
93390 33 5f 6d 75 74 65 78 20 7b 0a 20 20 48 4d 54 58 3_mutex {. HMTX
933a0 20 6d 75 74 65 78 3b 20 20 20 20 20 20 20 2f 2a mutex; /*
933b0 20 4d 75 74 65 78 20 63 6f 6e 74 72 6f 6c 6c 69 Mutex controlli
933c0 6e 67 20 74 68 65 20 6c 6f 63 6b 20 2a 2f 0a 20 ng the lock */.
933d0 20 69 6e 74 20 20 69 64 3b 20 20 20 20 20 20 20 int id;
933e0 20 20 20 2f 2a 20 4d 75 74 65 78 20 74 79 70 65 /* Mutex type
933f0 20 2a 2f 0a 20 20 69 6e 74 20 20 6e 52 65 66 3b */. int nRef;
93400 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
93410 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65 73 20 r of references
93420 2a 2f 0a 20 20 54 49 44 20 20 6f 77 6e 65 72 3b */. TID owner;
93430 20 20 20 20 20 20 20 2f 2a 20 54 68 72 65 61 64 /* Thread
93440 20 68 6f 6c 64 69 6e 67 20 74 68 69 73 20 6d 75 holding this mu
93450 74 65 78 20 2a 2f 0a 7d 3b 0a 0a 23 64 65 66 69 tex */.};..#defi
93460 6e 65 20 4f 53 32 5f 4d 55 54 45 58 5f 49 4e 49 ne OS2_MUTEX_INI
93470 54 49 41 4c 49 5a 45 52 20 20 20 30 2c 30 2c 30 TIALIZER 0,0,0
93480 2c 30 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 ,0../*.** Initia
93490 6c 69 7a 65 20 61 6e 64 20 64 65 69 6e 69 74 69 lize and deiniti
934a0 61 6c 69 7a 65 20 74 68 65 20 6d 75 74 65 78 20 alize the mutex
934b0 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 73 74 subsystem..*/.st
934c0 61 74 69 63 20 69 6e 74 20 6f 73 32 4d 75 74 65 atic int os2Mute
934d0 78 49 6e 69 74 28 76 6f 69 64 29 7b 20 72 65 74 xInit(void){ ret
934e0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d urn SQLITE_OK; }
934f0 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 4d .static int os2M
93500 75 74 65 78 45 6e 64 28 76 6f 69 64 29 7b 20 72 utexEnd(void){ r
93510 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
93520 20 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 }../*.** The sq
93530 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f lite3_mutex_allo
93540 63 28 29 20 72 6f 75 74 69 6e 65 20 61 6c 6c 6f c() routine allo
93550 63 61 74 65 73 20 61 20 6e 65 77 0a 2a 2a 20 6d cates a new.** m
93560 75 74 65 78 20 61 6e 64 20 72 65 74 75 72 6e 73 utex and returns
93570 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 a pointer to it
93580 2e 20 20 49 66 20 69 74 20 72 65 74 75 72 6e 73 . If it returns
93590 20 4e 55 4c 4c 0a 2a 2a 20 74 68 61 74 20 6d 65 NULL.** that me
935a0 61 6e 73 20 74 68 61 74 20 61 20 6d 75 74 65 78 ans that a mutex
935b0 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 61 6c could not be al
935c0 6c 6f 63 61 74 65 64 2e 20 0a 2a 2a 20 53 51 4c located. .** SQL
935d0 69 74 65 20 77 69 6c 6c 20 75 6e 77 69 6e 64 20 ite will unwind
935e0 69 74 73 20 73 74 61 63 6b 20 61 6e 64 20 72 65 its stack and re
935f0 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 2e 20 20 turn an error.
93600 54 68 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 The argument.**
93610 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 to sqlite3_mutex
93620 5f 61 6c 6c 6f 63 28 29 20 69 73 20 6f 6e 65 20 _alloc() is one
93630 6f 66 20 74 68 65 73 65 20 69 6e 74 65 67 65 72 of these integer
93640 20 63 6f 6e 73 74 61 6e 74 73 3a 0a 2a 2a 0a 2a constants:.**.*
93650 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 * <ul>.** <li>
93660 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 SQLITE_MUTEX_FAS
93670 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 T
93680 30 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 0.** <li> SQLIT
93690 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 E_MUTEX_RECURSIV
936a0 45 20 20 20 20 20 20 20 20 20 20 31 0a 2a 2a 20 E 1.**
936b0 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 <li> SQLITE_MUT
936c0 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 EX_STATIC_MASTER
936d0 20 20 20 20 20 20 32 0a 2a 2a 20 3c 6c 69 3e 20 2.** <li>
936e0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 SQLITE_MUTEX_ST
936f0 41 54 49 43 5f 4d 45 4d 20 20 20 20 20 20 20 20 ATIC_MEM
93700 20 33 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 3.** <li> SQLI
93710 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f TE_MUTEX_STATIC_
93720 50 52 4e 47 20 20 20 20 20 20 20 20 34 0a 2a 2a PRNG 4.**
93730 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 </ul>.**.** The
93740 20 66 69 72 73 74 20 74 77 6f 20 63 6f 6e 73 74 first two const
93750 61 6e 74 73 20 63 61 75 73 65 20 73 71 6c 69 74 ants cause sqlit
93760 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 e3_mutex_alloc()
93770 20 74 6f 20 63 72 65 61 74 65 0a 2a 2a 20 61 20 to create.** a
93780 6e 65 77 20 6d 75 74 65 78 2e 20 20 54 68 65 20 new mutex. The
93790 6e 65 77 20 6d 75 74 65 78 20 69 73 20 72 65 63 new mutex is rec
937a0 75 72 73 69 76 65 20 77 68 65 6e 20 53 51 4c 49 ursive when SQLI
937b0 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 TE_MUTEX_RECURSI
937c0 56 45 0a 2a 2a 20 69 73 20 75 73 65 64 20 62 75 VE.** is used bu
937d0 74 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 6c t not necessaril
937e0 79 20 73 6f 20 77 68 65 6e 20 53 51 4c 49 54 45 y so when SQLITE
937f0 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 73 20 75 _MUTEX_FAST is u
93800 73 65 64 2e 0a 2a 2a 20 54 68 65 20 6d 75 74 65 sed..** The mute
93810 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e x implementation
93820 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 does not need t
93830 6f 20 6d 61 6b 65 20 61 20 64 69 73 74 69 6e 63 o make a distinc
93840 74 69 6f 6e 0a 2a 2a 20 62 65 74 77 65 65 6e 20 tion.** between
93850 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 SQLITE_MUTEX_REC
93860 55 52 53 49 56 45 20 61 6e 64 20 53 51 4c 49 54 URSIVE and SQLIT
93870 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 66 20 E_MUTEX_FAST if
93880 69 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 77 it does.** not w
93890 61 6e 74 20 74 6f 2e 20 20 42 75 74 20 53 51 4c ant to. But SQL
938a0 69 74 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 72 65 ite will only re
938b0 71 75 65 73 74 20 61 20 72 65 63 75 72 73 69 76 quest a recursiv
938c0 65 20 6d 75 74 65 78 20 69 6e 0a 2a 2a 20 63 61 e mutex in.** ca
938d0 73 65 73 20 77 68 65 72 65 20 69 74 20 72 65 61 ses where it rea
938e0 6c 6c 79 20 6e 65 65 64 73 20 6f 6e 65 2e 20 20 lly needs one.
938f0 49 66 20 61 20 66 61 73 74 65 72 20 6e 6f 6e 2d If a faster non-
93900 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 0a recursive mutex.
93910 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ** implementatio
93920 6e 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20 6f n is available o
93930 6e 20 74 68 65 20 68 6f 73 74 20 70 6c 61 74 66 n the host platf
93940 6f 72 6d 2c 20 74 68 65 20 6d 75 74 65 78 20 73 orm, the mutex s
93950 75 62 73 79 73 74 65 6d 0a 2a 2a 20 6d 69 67 68 ubsystem.** migh
93960 74 20 72 65 74 75 72 6e 20 73 75 63 68 20 61 20 t return such a
93970 6d 75 74 65 78 20 69 6e 20 72 65 73 70 6f 6e 73 mutex in respons
93980 65 20 74 6f 20 53 51 4c 49 54 45 5f 4d 55 54 45 e to SQLITE_MUTE
93990 58 5f 46 41 53 54 2e 0a 2a 2a 0a 2a 2a 20 54 68 X_FAST..**.** Th
939a0 65 20 6f 74 68 65 72 20 61 6c 6c 6f 77 65 64 20 e other allowed
939b0 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 73 71 parameters to sq
939c0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f lite3_mutex_allo
939d0 63 28 29 20 65 61 63 68 20 72 65 74 75 72 6e 0a c() each return.
939e0 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 ** a pointer to
939f0 61 20 73 74 61 74 69 63 20 70 72 65 65 78 69 73 a static preexis
93a00 74 69 6e 67 20 6d 75 74 65 78 2e 20 20 54 68 72 ting mutex. Thr
93a10 65 65 20 73 74 61 74 69 63 20 6d 75 74 65 78 65 ee static mutexe
93a20 73 20 61 72 65 0a 2a 2a 20 75 73 65 64 20 62 79 s are.** used by
93a30 20 74 68 65 20 63 75 72 72 65 6e 74 20 76 65 72 the current ver
93a40 73 69 6f 6e 20 6f 66 20 53 51 4c 69 74 65 2e 20 sion of SQLite.
93a50 20 46 75 74 75 72 65 20 76 65 72 73 69 6f 6e 73 Future versions
93a60 20 6f 66 20 53 51 4c 69 74 65 0a 2a 2a 20 6d 61 of SQLite.** ma
93a70 79 20 61 64 64 20 61 64 64 69 74 69 6f 6e 61 6c y add additional
93a80 20 73 74 61 74 69 63 20 6d 75 74 65 78 65 73 2e static mutexes.
93a90 20 20 53 74 61 74 69 63 20 6d 75 74 65 78 65 73 Static mutexes
93aa0 20 61 72 65 20 66 6f 72 20 69 6e 74 65 72 6e 61 are for interna
93ab0 6c 0a 2a 2a 20 75 73 65 20 62 79 20 53 51 4c 69 l.** use by SQLi
93ac0 74 65 20 6f 6e 6c 79 2e 20 20 41 70 70 6c 69 63 te only. Applic
93ad0 61 74 69 6f 6e 73 20 74 68 61 74 20 75 73 65 20 ations that use
93ae0 53 51 4c 69 74 65 20 6d 75 74 65 78 65 73 20 73 SQLite mutexes s
93af0 68 6f 75 6c 64 0a 2a 2a 20 75 73 65 20 6f 6e 6c hould.** use onl
93b00 79 20 74 68 65 20 64 79 6e 61 6d 69 63 20 6d 75 y the dynamic mu
93b10 74 65 78 65 73 20 72 65 74 75 72 6e 65 64 20 62 texes returned b
93b20 79 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 y SQLITE_MUTEX_F
93b30 41 53 54 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 45 AST or.** SQLITE
93b40 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 _MUTEX_RECURSIVE
93b50 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 ..**.** Note tha
93b60 74 20 69 66 20 6f 6e 65 20 6f 66 20 74 68 65 20 t if one of the
93b70 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 20 70 61 dynamic mutex pa
93b80 72 61 6d 65 74 65 72 73 20 28 53 51 4c 49 54 45 rameters (SQLITE
93b90 5f 4d 55 54 45 58 5f 46 41 53 54 0a 2a 2a 20 6f _MUTEX_FAST.** o
93ba0 72 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 r SQLITE_MUTEX_R
93bb0 45 43 55 52 53 49 56 45 29 20 69 73 20 75 73 65 ECURSIVE) is use
93bc0 64 20 74 68 65 6e 20 73 71 6c 69 74 65 33 5f 6d d then sqlite3_m
93bd0 75 74 65 78 5f 61 6c 6c 6f 63 28 29 0a 2a 2a 20 utex_alloc().**
93be0 72 65 74 75 72 6e 73 20 61 20 64 69 66 66 65 72 returns a differ
93bf0 65 6e 74 20 6d 75 74 65 78 20 6f 6e 20 65 76 65 ent mutex on eve
93c00 72 79 20 63 61 6c 6c 2e 20 20 42 75 74 20 66 6f ry call. But fo
93c10 72 20 74 68 65 20 73 74 61 74 69 63 0a 2a 2a 20 r the static.**
93c20 6d 75 74 65 78 20 74 79 70 65 73 2c 20 74 68 65 mutex types, the
93c30 20 73 61 6d 65 20 6d 75 74 65 78 20 69 73 20 72 same mutex is r
93c40 65 74 75 72 6e 65 64 20 6f 6e 20 65 76 65 72 79 eturned on every
93c50 20 63 61 6c 6c 20 74 68 61 74 20 68 61 73 0a 2a call that has.*
93c60 2a 20 74 68 65 20 73 61 6d 65 20 74 79 70 65 20 * the same type
93c70 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 number..*/.stati
93c80 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 c sqlite3_mutex
93c90 2a 6f 73 32 4d 75 74 65 78 41 6c 6c 6f 63 28 69 *os2MutexAlloc(i
93ca0 6e 74 20 69 54 79 70 65 29 7b 0a 20 20 73 71 6c nt iType){. sql
93cb0 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 20 3d 20 ite3_mutex *p =
93cc0 4e 55 4c 4c 3b 0a 20 20 73 77 69 74 63 68 28 20 NULL;. switch(
93cd0 69 54 79 70 65 20 29 7b 0a 20 20 20 20 63 61 73 iType ){. cas
93ce0 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 e SQLITE_MUTEX_F
93cf0 41 53 54 3a 0a 20 20 20 20 63 61 73 65 20 53 51 AST:. case SQ
93d00 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 LITE_MUTEX_RECUR
93d10 53 49 56 45 3a 20 7b 0a 20 20 20 20 20 20 70 20 SIVE: {. p
93d20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a = sqlite3MallocZ
93d30 65 72 6f 28 20 73 69 7a 65 6f 66 28 2a 70 29 20 ero( sizeof(*p)
93d40 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 20 29 );. if( p )
93d50 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 64 20 {. p->id
93d60 3d 20 69 54 79 70 65 3b 0a 20 20 20 20 20 20 20 = iType;.
93d70 20 69 66 28 20 44 6f 73 43 72 65 61 74 65 4d 75 if( DosCreateMu
93d80 74 65 78 53 65 6d 28 20 30 2c 20 26 70 2d 3e 6d texSem( 0, &p->m
93d90 75 74 65 78 2c 20 30 2c 20 46 41 4c 53 45 20 29 utex, 0, FALSE )
93da0 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a != NO_ERROR ){.
93db0 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
93dc0 33 5f 66 72 65 65 28 20 70 20 29 3b 0a 20 20 20 3_free( p );.
93dd0 20 20 20 20 20 20 20 70 20 3d 20 4e 55 4c 4c 3b p = NULL;
93de0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
93df0 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a }. break;.
93e00 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c }. defaul
93e10 74 3a 20 7b 0a 20 20 20 20 20 20 73 74 61 74 69 t: {. stati
93e20 63 20 76 6f 6c 61 74 69 6c 65 20 69 6e 74 20 69 c volatile int i
93e30 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20 20 20 20 sInit = 0;.
93e40 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f static sqlite3_
93e50 6d 75 74 65 78 20 73 74 61 74 69 63 4d 75 74 65 mutex staticMute
93e60 78 65 73 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 xes[] = {.
93e70 20 20 7b 20 4f 53 32 5f 4d 55 54 45 58 5f 49 4e { OS2_MUTEX_IN
93e80 49 54 49 41 4c 49 5a 45 52 2c 20 7d 2c 0a 20 20 ITIALIZER, },.
93e90 20 20 20 20 20 20 7b 20 4f 53 32 5f 4d 55 54 45 { OS2_MUTE
93ea0 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 20 7d X_INITIALIZER, }
93eb0 2c 0a 20 20 20 20 20 20 20 20 7b 20 4f 53 32 5f ,. { OS2_
93ec0 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 MUTEX_INITIALIZE
93ed0 52 2c 20 7d 2c 0a 20 20 20 20 20 20 20 20 7b 20 R, },. {
93ee0 4f 53 32 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 OS2_MUTEX_INITIA
93ef0 4c 49 5a 45 52 2c 20 7d 2c 0a 20 20 20 20 20 20 LIZER, },.
93f00 20 20 7b 20 4f 53 32 5f 4d 55 54 45 58 5f 49 4e { OS2_MUTEX_IN
93f10 49 54 49 41 4c 49 5a 45 52 2c 20 7d 2c 0a 20 20 ITIALIZER, },.
93f20 20 20 20 20 20 20 7b 20 4f 53 32 5f 4d 55 54 45 { OS2_MUTE
93f30 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 20 7d X_INITIALIZER, }
93f40 2c 0a 20 20 20 20 20 20 7d 3b 0a 20 20 20 20 20 ,. };.
93f50 20 69 66 20 28 20 21 69 73 49 6e 69 74 20 29 7b if ( !isInit ){
93f60 0a 20 20 20 20 20 20 20 20 41 50 49 52 45 54 20 . APIRET
93f70 72 63 3b 0a 20 20 20 20 20 20 20 20 50 54 49 42 rc;. PTIB
93f80 20 70 74 69 62 3b 0a 20 20 20 20 20 20 20 20 50 ptib;. P
93f90 50 49 42 20 70 70 69 62 3b 0a 20 20 20 20 20 20 PIB ppib;.
93fa0 20 20 48 4d 54 58 20 6d 75 74 65 78 3b 0a 20 20 HMTX mutex;.
93fb0 20 20 20 20 20 20 63 68 61 72 20 6e 61 6d 65 5b char name[
93fc0 33 32 5d 3b 0a 20 20 20 20 20 20 20 20 44 6f 73 32];. Dos
93fd0 47 65 74 49 6e 66 6f 42 6c 6f 63 6b 73 28 20 26 GetInfoBlocks( &
93fe0 70 74 69 62 2c 20 26 70 70 69 62 20 29 3b 0a 20 ptib, &ppib );.
93ff0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 sqlite3_s
94000 6e 70 72 69 6e 74 66 28 20 73 69 7a 65 6f 66 28 nprintf( sizeof(
94010 6e 61 6d 65 29 2c 20 6e 61 6d 65 2c 20 22 5c 5c name), name, "\\
94020 53 45 4d 33 32 5c 5c 53 51 4c 49 54 45 25 30 34 SEM32\\SQLITE%04
94030 78 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 x",.
94040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 70 pp
94050 69 62 2d 3e 70 69 62 5f 75 6c 70 69 64 20 29 3b ib->pib_ulpid );
94060 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 . while(
94070 21 69 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 20 !isInit ){.
94080 20 20 20 20 20 6d 75 74 65 78 20 3d 20 30 3b 0a mutex = 0;.
94090 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 44 rc = D
940a0 6f 73 43 72 65 61 74 65 4d 75 74 65 78 53 65 6d osCreateMutexSem
940b0 28 20 6e 61 6d 65 2c 20 26 6d 75 74 65 78 2c 20 ( name, &mutex,
940c0 30 2c 20 46 41 4c 53 45 29 3b 0a 20 20 20 20 20 0, FALSE);.
940d0 20 20 20 20 20 69 66 28 20 72 63 20 3d 3d 20 4e if( rc == N
940e0 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 20 O_ERROR ){.
940f0 20 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 unsigned
94100 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 20 20 20 int i;.
94110 20 20 20 69 66 28 20 21 69 73 49 6e 69 74 20 29 if( !isInit )
94120 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {.
94130 66 6f 72 28 20 69 20 3d 20 30 3b 20 69 20 3c 20 for( i = 0; i <
94140 73 69 7a 65 6f 66 28 73 74 61 74 69 63 4d 75 74 sizeof(staticMut
94150 65 78 65 73 29 2f 73 69 7a 65 6f 66 28 73 74 61 exes)/sizeof(sta
94160 74 69 63 4d 75 74 65 78 65 73 5b 30 5d 29 3b 20 ticMutexes[0]);
94170 69 2b 2b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 i++ ){.
94180 20 20 20 20 20 20 20 44 6f 73 43 72 65 61 74 65 DosCreate
94190 4d 75 74 65 78 53 65 6d 28 20 30 2c 20 26 73 74 MutexSem( 0, &st
941a0 61 74 69 63 4d 75 74 65 78 65 73 5b 69 5d 2e 6d aticMutexes[i].m
941b0 75 74 65 78 2c 20 30 2c 20 46 41 4c 53 45 20 29 utex, 0, FALSE )
941c0 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;.
941d0 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 }.
941e0 69 73 49 6e 69 74 20 3d 20 31 3b 0a 20 20 20 20 isInit = 1;.
941f0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
94200 20 20 20 20 20 20 44 6f 73 43 6c 6f 73 65 4d 75 DosCloseMu
94210 74 65 78 53 65 6d 28 20 6d 75 74 65 78 20 29 3b texSem( mutex );
94220 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 . }else
94230 20 69 66 28 20 72 63 20 3d 3d 20 45 52 52 4f 52 if( rc == ERROR
94240 5f 44 55 50 4c 49 43 41 54 45 5f 4e 41 4d 45 20 _DUPLICATE_NAME
94250 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 44 ){. D
94260 6f 73 53 6c 65 65 70 28 20 31 20 29 3b 0a 20 20 osSleep( 1 );.
94270 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }else{.
94280 20 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 retur
94290 6e 20 70 3b 0a 20 20 20 20 20 20 20 20 20 20 7d n p;. }
942a0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
942b0 20 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 }. assert(
942c0 20 69 54 79 70 65 2d 32 20 3e 3d 20 30 20 29 3b iType-2 >= 0 );
942d0 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 . assert( i
942e0 54 79 70 65 2d 32 20 3c 20 73 69 7a 65 6f 66 28 Type-2 < sizeof(
942f0 73 74 61 74 69 63 4d 75 74 65 78 65 73 29 2f 73 staticMutexes)/s
94300 69 7a 65 6f 66 28 73 74 61 74 69 63 4d 75 74 65 izeof(staticMute
94310 78 65 73 5b 30 5d 29 20 29 3b 0a 20 20 20 20 20 xes[0]) );.
94320 20 70 20 3d 20 26 73 74 61 74 69 63 4d 75 74 65 p = &staticMute
94330 78 65 73 5b 69 54 79 70 65 2d 32 5d 3b 0a 20 20 xes[iType-2];.
94340 20 20 20 20 70 2d 3e 69 64 20 3d 20 69 54 79 70 p->id = iTyp
94350 65 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a e;. break;.
94360 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
94370 72 6e 20 70 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 rn p;.}.../*.**
94380 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 65 61 This routine dea
94390 6c 6c 6f 63 61 74 65 73 20 61 20 70 72 65 76 69 llocates a previ
943a0 6f 75 73 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 ously allocated
943b0 6d 75 74 65 78 2e 0a 2a 2a 20 53 51 4c 69 74 65 mutex..** SQLite
943c0 20 69 73 20 63 61 72 65 66 75 6c 20 74 6f 20 64 is careful to d
943d0 65 61 6c 6c 6f 63 61 74 65 20 65 76 65 72 79 20 eallocate every
943e0 6d 75 74 65 78 20 74 68 61 74 20 69 74 20 61 6c mutex that it al
943f0 6c 6f 63 61 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 locates..*/.stat
94400 69 63 20 76 6f 69 64 20 6f 73 32 4d 75 74 65 78 ic void os2Mutex
94410 46 72 65 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 Free(sqlite3_mut
94420 65 78 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 3d ex *p){. if( p=
94430 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 =0 ) return;. a
94440 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d ssert( p->nRef==
94450 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 0 );. assert( p
94460 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 ->id==SQLITE_MUT
94470 45 58 5f 46 41 53 54 20 7c 7c 20 70 2d 3e 69 64 EX_FAST || p->id
94480 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 ==SQLITE_MUTEX_R
94490 45 43 55 52 53 49 56 45 20 29 3b 0a 20 20 44 6f ECURSIVE );. Do
944a0 73 43 6c 6f 73 65 4d 75 74 65 78 53 65 6d 28 20 sCloseMutexSem(
944b0 70 2d 3e 6d 75 74 65 78 20 29 3b 0a 20 20 73 71 p->mutex );. sq
944c0 6c 69 74 65 33 5f 66 72 65 65 28 20 70 20 29 3b lite3_free( p );
944d0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 .}../*.** The sq
944e0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 lite3_mutex_ente
944f0 72 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f r() and sqlite3_
94500 6d 75 74 65 78 5f 74 72 79 28 29 20 72 6f 75 74 mutex_try() rout
94510 69 6e 65 73 20 61 74 74 65 6d 70 74 0a 2a 2a 20 ines attempt.**
94520 74 6f 20 65 6e 74 65 72 20 61 20 6d 75 74 65 78 to enter a mutex
94530 2e 20 20 49 66 20 61 6e 6f 74 68 65 72 20 74 68 . If another th
94540 72 65 61 64 20 69 73 20 61 6c 72 65 61 64 79 20 read is already
94550 77 69 74 68 69 6e 20 74 68 65 20 6d 75 74 65 78 within the mutex
94560 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 ,.** sqlite3_mut
94570 65 78 5f 65 6e 74 65 72 28 29 20 77 69 6c 6c 20 ex_enter() will
94580 62 6c 6f 63 6b 20 61 6e 64 20 73 71 6c 69 74 65 block and sqlite
94590 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 77 69 3_mutex_try() wi
945a0 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c ll return.** SQL
945b0 49 54 45 5f 42 55 53 59 2e 20 20 54 68 65 20 73 ITE_BUSY. The s
945c0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 qlite3_mutex_try
945d0 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 () interface ret
945e0 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a urns SQLITE_OK.*
945f0 2a 20 75 70 6f 6e 20 73 75 63 63 65 73 73 66 75 * upon successfu
94600 6c 20 65 6e 74 72 79 2e 20 20 4d 75 74 65 78 65 l entry. Mutexe
94610 73 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 20 s created using
94620 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 SQLITE_MUTEX_REC
94630 55 52 53 49 56 45 20 63 61 6e 0a 2a 2a 20 62 65 URSIVE can.** be
94640 20 65 6e 74 65 72 65 64 20 6d 75 6c 74 69 70 6c entered multipl
94650 65 20 74 69 6d 65 73 20 62 79 20 74 68 65 20 73 e times by the s
94660 61 6d 65 20 74 68 72 65 61 64 2e 20 20 49 6e 20 ame thread. In
94670 73 75 63 68 20 63 61 73 65 73 20 74 68 65 2c 0a such cases the,.
94680 2a 2a 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 ** mutex must be
94690 20 65 78 69 74 65 64 20 61 6e 20 65 71 75 61 6c exited an equal
946a0 20 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 number of times
946b0 20 62 65 66 6f 72 65 20 61 6e 6f 74 68 65 72 20 before another
946c0 74 68 72 65 61 64 0a 2a 2a 20 63 61 6e 20 65 6e thread.** can en
946d0 74 65 72 2e 20 20 49 66 20 74 68 65 20 73 61 6d ter. If the sam
946e0 65 20 74 68 72 65 61 64 20 74 72 69 65 73 20 74 e thread tries t
946f0 6f 20 65 6e 74 65 72 20 61 6e 79 20 6f 74 68 65 o enter any othe
94700 72 20 6b 69 6e 64 20 6f 66 20 6d 75 74 65 78 0a r kind of mutex.
94710 2a 2a 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 ** more than onc
94720 65 2c 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 e, the behavior
94730 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f is undefined..*/
94740 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f 73 32 .static void os2
94750 4d 75 74 65 78 45 6e 74 65 72 28 73 71 6c 69 74 MutexEnter(sqlit
94760 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 e3_mutex *p){.
94770 54 49 44 20 74 69 64 3b 0a 20 20 50 49 44 20 68 TID tid;. PID h
94780 6f 6c 64 65 72 31 3b 0a 20 20 55 4c 4f 4e 47 20 older1;. ULONG
94790 68 6f 6c 64 65 72 32 3b 0a 20 20 69 66 28 20 70 holder2;. if( p
947a0 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 ==0 ) return;.
947b0 61 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 assert( p->id==S
947c0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 QLITE_MUTEX_RECU
947d0 52 53 49 56 45 20 7c 7c 20 6f 73 32 4d 75 74 65 RSIVE || os2Mute
947e0 78 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a 20 xNotheld(p) );.
947f0 20 44 6f 73 52 65 71 75 65 73 74 4d 75 74 65 78 DosRequestMutex
94800 53 65 6d 28 70 2d 3e 6d 75 74 65 78 2c 20 53 45 Sem(p->mutex, SE
94810 4d 5f 49 4e 44 45 46 49 4e 49 54 45 5f 57 41 49 M_INDEFINITE_WAI
94820 54 29 3b 0a 20 20 44 6f 73 51 75 65 72 79 4d 75 T);. DosQueryMu
94830 74 65 78 53 65 6d 28 70 2d 3e 6d 75 74 65 78 2c texSem(p->mutex,
94840 20 26 68 6f 6c 64 65 72 31 2c 20 26 74 69 64 2c &holder1, &tid,
94850 20 26 68 6f 6c 64 65 72 32 29 3b 0a 20 20 70 2d &holder2);. p-
94860 3e 6f 77 6e 65 72 20 3d 20 74 69 64 3b 0a 20 20 >owner = tid;.
94870 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 7d 0a 73 74 61 p->nRef++;.}.sta
94880 74 69 63 20 69 6e 74 20 6f 73 32 4d 75 74 65 78 tic int os2Mutex
94890 54 72 79 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 Try(sqlite3_mute
948a0 78 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b x *p){. int rc;
948b0 0a 20 20 54 49 44 20 74 69 64 3b 0a 20 20 50 49 . TID tid;. PI
948c0 44 20 68 6f 6c 64 65 72 31 3b 0a 20 20 55 4c 4f D holder1;. ULO
948d0 4e 47 20 68 6f 6c 64 65 72 32 3b 0a 20 20 69 66 NG holder2;. if
948e0 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 ( p==0 ) return
948f0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61 73 73 SQLITE_OK;. ass
94900 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 ert( p->id==SQLI
94910 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 TE_MUTEX_RECURSI
94920 56 45 20 7c 7c 20 6f 73 32 4d 75 74 65 78 4e 6f VE || os2MutexNo
94930 74 68 65 6c 64 28 70 29 20 29 3b 0a 20 20 69 66 theld(p) );. if
94940 28 20 44 6f 73 52 65 71 75 65 73 74 4d 75 74 65 ( DosRequestMute
94950 78 53 65 6d 28 70 2d 3e 6d 75 74 65 78 2c 20 53 xSem(p->mutex, S
94960 45 4d 5f 49 4d 4d 45 44 49 41 54 45 5f 52 45 54 EM_IMMEDIATE_RET
94970 55 52 4e 29 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 URN) == NO_ERROR
94980 29 20 7b 0a 20 20 20 20 44 6f 73 51 75 65 72 79 ) {. DosQuery
94990 4d 75 74 65 78 53 65 6d 28 70 2d 3e 6d 75 74 65 MutexSem(p->mute
949a0 78 2c 20 26 68 6f 6c 64 65 72 31 2c 20 26 74 69 x, &holder1, &ti
949b0 64 2c 20 26 68 6f 6c 64 65 72 32 29 3b 0a 20 20 d, &holder2);.
949c0 20 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 74 69 64 p->owner = tid
949d0 3b 0a 20 20 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b ;. p->nRef++;
949e0 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 . rc = SQLITE
949f0 5f 4f 4b 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a _OK;. } else {.
94a00 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
94a10 42 55 53 59 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 BUSY;. }.. ret
94a20 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
94a30 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 The sqlite3_mut
94a40 65 78 5f 6c 65 61 76 65 28 29 20 72 6f 75 74 69 ex_leave() routi
94a50 6e 65 20 65 78 69 74 73 20 61 20 6d 75 74 65 78 ne exits a mutex
94a60 20 74 68 61 74 20 77 61 73 0a 2a 2a 20 70 72 65 that was.** pre
94a70 76 69 6f 75 73 6c 79 20 65 6e 74 65 72 65 64 20 viously entered
94a80 62 79 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 by the same thre
94a90 61 64 2e 20 20 54 68 65 20 62 65 68 61 76 69 6f ad. The behavio
94aa0 72 0a 2a 2a 20 69 73 20 75 6e 64 65 66 69 6e 65 r.** is undefine
94ab0 64 20 69 66 20 74 68 65 20 6d 75 74 65 78 20 69 d if the mutex i
94ac0 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 s not currently
94ad0 65 6e 74 65 72 65 64 20 6f 72 0a 2a 2a 20 69 73 entered or.** is
94ae0 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 61 not currently a
94af0 6c 6c 6f 63 61 74 65 64 2e 20 20 53 51 4c 69 74 llocated. SQLit
94b00 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 64 6f 20 e will never do
94b10 65 69 74 68 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 either..*/.stati
94b20 63 20 76 6f 69 64 20 6f 73 32 4d 75 74 65 78 4c c void os2MutexL
94b30 65 61 76 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 eave(sqlite3_mut
94b40 65 78 20 2a 70 29 7b 0a 20 20 54 49 44 20 74 69 ex *p){. TID ti
94b50 64 3b 0a 20 20 50 49 44 20 68 6f 6c 64 65 72 31 d;. PID holder1
94b60 3b 0a 20 20 55 4c 4f 4e 47 20 68 6f 6c 64 65 72 ;. ULONG holder
94b70 32 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 2;. if( p==0 )
94b80 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65 72 74 return;. assert
94b90 28 20 70 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 ( p->nRef>0 );.
94ba0 20 44 6f 73 51 75 65 72 79 4d 75 74 65 78 53 65 DosQueryMutexSe
94bb0 6d 28 70 2d 3e 6d 75 74 65 78 2c 20 26 68 6f 6c m(p->mutex, &hol
94bc0 64 65 72 31 2c 20 26 74 69 64 2c 20 26 68 6f 6c der1, &tid, &hol
94bd0 64 65 72 32 29 3b 0a 20 20 61 73 73 65 72 74 28 der2);. assert(
94be0 20 70 2d 3e 6f 77 6e 65 72 3d 3d 74 69 64 20 29 p->owner==tid )
94bf0 3b 0a 20 20 70 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 ;. p->nRef--;.
94c00 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 assert( p->nRef
94c10 3d 3d 30 20 7c 7c 20 70 2d 3e 69 64 3d 3d 53 51 ==0 || p->id==SQ
94c20 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 LITE_MUTEX_RECUR
94c30 53 49 56 45 20 29 3b 0a 20 20 44 6f 73 52 65 6c SIVE );. DosRel
94c40 65 61 73 65 4d 75 74 65 78 53 65 6d 28 70 2d 3e easeMutexSem(p->
94c50 6d 75 74 65 78 29 3b 0a 7d 0a 0a 23 69 66 64 65 mutex);.}..#ifde
94c60 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 2f f SQLITE_DEBUG./
94c70 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
94c80 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 20 61 6e _mutex_held() an
94c90 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f d sqlite3_mutex_
94ca0 6e 6f 74 68 65 6c 64 28 29 20 72 6f 75 74 69 6e notheld() routin
94cb0 65 20 61 72 65 0a 2a 2a 20 69 6e 74 65 6e 64 65 e are.** intende
94cc0 64 20 66 6f 72 20 75 73 65 20 69 6e 73 69 64 65 d for use inside
94cd0 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d assert() statem
94ce0 65 6e 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ents..*/.static
94cf0 69 6e 74 20 6f 73 32 4d 75 74 65 78 48 65 6c 64 int os2MutexHeld
94d00 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a (sqlite3_mutex *
94d10 70 29 7b 0a 20 20 54 49 44 20 74 69 64 3b 0a 20 p){. TID tid;.
94d20 20 50 49 44 20 70 69 64 3b 0a 20 20 55 4c 4f 4e PID pid;. ULON
94d30 47 20 75 6c 43 6f 75 6e 74 3b 0a 20 20 50 54 49 G ulCount;. PTI
94d40 42 20 70 74 69 62 3b 0a 20 20 69 66 28 20 70 21 B ptib;. if( p!
94d50 3d 30 20 29 20 7b 0a 20 20 20 20 44 6f 73 51 75 =0 ) {. DosQu
94d60 65 72 79 4d 75 74 65 78 53 65 6d 28 70 2d 3e 6d eryMutexSem(p->m
94d70 75 74 65 78 2c 20 26 70 69 64 2c 20 26 74 69 64 utex, &pid, &tid
94d80 2c 20 26 75 6c 43 6f 75 6e 74 29 3b 0a 20 20 7d , &ulCount);. }
94d90 20 65 6c 73 65 20 7b 0a 20 20 20 20 44 6f 73 47 else {. DosG
94da0 65 74 49 6e 66 6f 42 6c 6f 63 6b 73 28 26 70 74 etInfoBlocks(&pt
94db0 69 62 2c 20 4e 55 4c 4c 29 3b 0a 20 20 20 20 74 ib, NULL);. t
94dc0 69 64 20 3d 20 70 74 69 62 2d 3e 74 69 62 5f 70 id = ptib->tib_p
94dd0 74 69 62 32 2d 3e 74 69 62 32 5f 75 6c 74 69 64 tib2->tib2_ultid
94de0 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 ;. }. return p
94df0 3d 3d 30 20 7c 7c 20 28 70 2d 3e 6e 52 65 66 21 ==0 || (p->nRef!
94e00 3d 30 20 26 26 20 70 2d 3e 6f 77 6e 65 72 3d 3d =0 && p->owner==
94e10 74 69 64 29 3b 0a 7d 0a 73 74 61 74 69 63 20 69 tid);.}.static i
94e20 6e 74 20 6f 73 32 4d 75 74 65 78 4e 6f 74 68 65 nt os2MutexNothe
94e30 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ld(sqlite3_mutex
94e40 20 2a 70 29 7b 0a 20 20 54 49 44 20 74 69 64 3b *p){. TID tid;
94e50 0a 20 20 50 49 44 20 70 69 64 3b 0a 20 20 55 4c . PID pid;. UL
94e60 4f 4e 47 20 75 6c 43 6f 75 6e 74 3b 0a 20 20 50 ONG ulCount;. P
94e70 54 49 42 20 70 74 69 62 3b 0a 20 20 69 66 28 20 TIB ptib;. if(
94e80 70 21 3d 20 30 20 29 20 7b 0a 20 20 20 20 44 6f p!= 0 ) {. Do
94e90 73 51 75 65 72 79 4d 75 74 65 78 53 65 6d 28 70 sQueryMutexSem(p
94ea0 2d 3e 6d 75 74 65 78 2c 20 26 70 69 64 2c 20 26 ->mutex, &pid, &
94eb0 74 69 64 2c 20 26 75 6c 43 6f 75 6e 74 29 3b 0a tid, &ulCount);.
94ec0 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 44 } else {. D
94ed0 6f 73 47 65 74 49 6e 66 6f 42 6c 6f 63 6b 73 28 osGetInfoBlocks(
94ee0 26 70 74 69 62 2c 20 4e 55 4c 4c 29 3b 0a 20 20 &ptib, NULL);.
94ef0 20 20 74 69 64 20 3d 20 70 74 69 62 2d 3e 74 69 tid = ptib->ti
94f00 62 5f 70 74 69 62 32 2d 3e 74 69 62 32 5f 75 6c b_ptib2->tib2_ul
94f10 74 69 64 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 tid;. }. retur
94f20 6e 20 70 3d 3d 30 20 7c 7c 20 70 2d 3e 6e 52 65 n p==0 || p->nRe
94f30 66 3d 3d 30 20 7c 7c 20 70 2d 3e 6f 77 6e 65 72 f==0 || p->owner
94f40 21 3d 74 69 64 3b 0a 7d 0a 23 65 6e 64 69 66 0a !=tid;.}.#endif.
94f50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
94f60 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 sqlite3_mutex_me
94f70 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 33 44 65 thods *sqlite3De
94f80 66 61 75 6c 74 4d 75 74 65 78 28 76 6f 69 64 29 faultMutex(void)
94f90 7b 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69 74 {. static sqlit
94fa0 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 e3_mutex_methods
94fb0 20 73 4d 75 74 65 78 20 3d 20 7b 0a 20 20 20 20 sMutex = {.
94fc0 6f 73 32 4d 75 74 65 78 49 6e 69 74 2c 0a 20 20 os2MutexInit,.
94fd0 20 20 6f 73 32 4d 75 74 65 78 45 6e 64 2c 0a 20 os2MutexEnd,.
94fe0 20 20 20 6f 73 32 4d 75 74 65 78 41 6c 6c 6f 63 os2MutexAlloc
94ff0 2c 0a 20 20 20 20 6f 73 32 4d 75 74 65 78 46 72 ,. os2MutexFr
95000 65 65 2c 0a 20 20 20 20 6f 73 32 4d 75 74 65 78 ee,. os2Mutex
95010 45 6e 74 65 72 2c 0a 20 20 20 20 6f 73 32 4d 75 Enter,. os2Mu
95020 74 65 78 54 72 79 2c 0a 20 20 20 20 6f 73 32 4d texTry,. os2M
95030 75 74 65 78 4c 65 61 76 65 2c 0a 23 69 66 64 65 utexLeave,.#ifde
95040 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 f SQLITE_DEBUG.
95050 20 20 20 6f 73 32 4d 75 74 65 78 48 65 6c 64 2c os2MutexHeld,
95060 0a 20 20 20 20 6f 73 32 4d 75 74 65 78 4e 6f 74 . os2MutexNot
95070 68 65 6c 64 0a 23 65 6e 64 69 66 0a 20 20 7d 3b held.#endif. };
95080 0a 0a 20 20 72 65 74 75 72 6e 20 26 73 4d 75 74 .. return &sMut
95090 65 78 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 ex;.}.#endif /*
950a0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 53 32 SQLITE_MUTEX_OS2
950b0 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../**********
950c0 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 75 74 65 **** End of mute
950d0 78 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a x_os2.c ********
950e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
950f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
95100 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
95110 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 **** Begin file
95120 6d 75 74 65 78 5f 75 6e 69 78 2e 63 20 2a 2a 2a mutex_unix.c ***
95130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
95140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
95150 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 ***/./*.** 2007
95160 41 75 67 75 73 74 20 32 38 0a 2a 2a 0a 2a 2a 20 August 28.**.**
95170 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c The author discl
95180 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 aims copyright t
95190 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f o this source co
951a0 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 de. In place of
951b0 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 .** a legal noti
951c0 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c ce, here is a bl
951d0 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 essing:.**.**
951e0 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 May you do good
951f0 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a and not evil..*
95200 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e * May you fin
95210 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f d forgiveness fo
95220 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 r yourself and f
95230 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a orgive others..*
95240 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 * May you sha
95250 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 re freely, never
95260 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 taking more tha
95270 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a n you give..**.*
95280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
95290 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
952a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
952b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
952c0 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 ********.** This
952d0 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 file contains t
952e0 68 65 20 43 20 66 75 6e 63 74 69 6f 6e 73 20 74 he C functions t
952f0 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20 6d 75 hat implement mu
95300 74 65 78 65 73 20 66 6f 72 20 70 74 68 72 65 61 texes for pthrea
95310 64 73 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d 75 ds.**.** $Id: mu
95320 74 65 78 5f 75 6e 69 78 2e 63 2c 76 20 31 2e 31 tex_unix.c,v 1.1
95330 36 20 32 30 30 38 2f 31 32 2f 30 38 20 31 38 3a 6 2008/12/08 18:
95340 31 39 3a 31 38 20 64 72 68 20 45 78 70 20 24 0a 19:18 drh Exp $.
95350 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f */../*.** The co
95360 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 de in this file
95370 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 69 66 20 is only used if
95380 77 65 20 61 72 65 20 63 6f 6d 70 69 6c 69 6e 67 we are compiling
95390 20 74 68 72 65 61 64 73 61 66 65 0a 2a 2a 20 75 threadsafe.** u
953a0 6e 64 65 72 20 75 6e 69 78 20 77 69 74 68 20 70 nder unix with p
953b0 74 68 72 65 61 64 73 2e 0a 2a 2a 0a 2a 2a 20 4e threads..**.** N
953c0 6f 74 65 20 74 68 61 74 20 74 68 69 73 20 69 6d ote that this im
953d0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 72 65 71 plementation req
953e0 75 69 72 65 73 20 61 20 76 65 72 73 69 6f 6e 20 uires a version
953f0 6f 66 20 70 74 68 72 65 61 64 73 20 74 68 61 74 of pthreads that
95400 0a 2a 2a 20 73 75 70 70 6f 72 74 73 20 72 65 63 .** supports rec
95410 75 72 73 69 76 65 20 6d 75 74 65 78 65 73 2e 0a ursive mutexes..
95420 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
95430 5f 4d 55 54 45 58 5f 50 54 48 52 45 41 44 53 0a _MUTEX_PTHREADS.
95440 0a 23 69 6e 63 6c 75 64 65 20 3c 70 74 68 72 65 .#include <pthre
95450 61 64 2e 68 3e 0a 0a 0a 2f 2a 0a 2a 2a 20 45 61 ad.h>.../*.** Ea
95460 63 68 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 ch recursive mut
95470 65 78 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 ex is an instanc
95480 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 e of the followi
95490 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f ng structure..*/
954a0 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f .struct sqlite3_
954b0 6d 75 74 65 78 20 7b 0a 20 20 70 74 68 72 65 61 mutex {. pthrea
954c0 64 5f 6d 75 74 65 78 5f 74 20 6d 75 74 65 78 3b d_mutex_t mutex;
954d0 20 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 63 6f /* Mutex co
954e0 6e 74 72 6f 6c 6c 69 6e 67 20 74 68 65 20 6c 6f ntrolling the lo
954f0 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 69 64 3b 20 ck */. int id;
95500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
95510 20 20 20 2f 2a 20 4d 75 74 65 78 20 74 79 70 65 /* Mutex type
95520 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 */. int nRef;
95530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
95540 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e /* Number of en
95550 74 72 61 6e 63 65 73 20 2a 2f 0a 20 20 70 74 68 trances */. pth
95560 72 65 61 64 5f 74 20 6f 77 6e 65 72 3b 20 20 20 read_t owner;
95570 20 20 20 20 20 20 20 20 2f 2a 20 54 68 72 65 61 /* Threa
95580 64 20 74 68 61 74 20 69 73 20 77 69 74 68 69 6e d that is within
95590 20 74 68 69 73 20 6d 75 74 65 78 20 2a 2f 0a 23 this mutex */.#
955a0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 ifdef SQLITE_DEB
955b0 55 47 0a 20 20 69 6e 74 20 74 72 61 63 65 3b 20 UG. int trace;
955c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
955d0 2f 2a 20 54 72 75 65 20 74 6f 20 74 72 61 63 65 /* True to trace
955e0 20 63 68 61 6e 67 65 73 20 2a 2f 0a 23 65 6e 64 changes */.#end
955f0 69 66 0a 7d 3b 0a 23 69 66 64 65 66 20 53 51 4c if.};.#ifdef SQL
95600 49 54 45 5f 44 45 42 55 47 0a 23 64 65 66 69 6e ITE_DEBUG.#defin
95610 65 20 53 51 4c 49 54 45 33 5f 4d 55 54 45 58 5f e SQLITE3_MUTEX_
95620 49 4e 49 54 49 41 4c 49 5a 45 52 20 7b 20 50 54 INITIALIZER { PT
95630 48 52 45 41 44 5f 4d 55 54 45 58 5f 49 4e 49 54 HREAD_MUTEX_INIT
95640 49 41 4c 49 5a 45 52 2c 20 30 2c 20 30 2c 20 28 IALIZER, 0, 0, (
95650 70 74 68 72 65 61 64 5f 74 29 30 2c 20 30 20 7d pthread_t)0, 0 }
95660 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 53 .#else.#define S
95670 51 4c 49 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49 QLITE3_MUTEX_INI
95680 54 49 41 4c 49 5a 45 52 20 7b 20 50 54 48 52 45 TIALIZER { PTHRE
95690 41 44 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c AD_MUTEX_INITIAL
956a0 49 5a 45 52 2c 20 30 2c 20 30 2c 20 28 70 74 68 IZER, 0, 0, (pth
956b0 72 65 61 64 5f 74 29 30 20 7d 0a 23 65 6e 64 69 read_t)0 }.#endi
956c0 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c f../*.** The sql
956d0 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
956e0 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 ) and sqlite3_mu
956f0 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 20 72 6f tex_notheld() ro
95700 75 74 69 6e 65 20 61 72 65 0a 2a 2a 20 69 6e 74 utine are.** int
95710 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20 6f 6e ended for use on
95720 6c 79 20 69 6e 73 69 64 65 20 61 73 73 65 72 74 ly inside assert
95730 28 29 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 20 () statements.
95740 4f 6e 20 73 6f 6d 65 20 70 6c 61 74 66 6f 72 6d On some platform
95750 73 2c 0a 2a 2a 20 74 68 65 72 65 20 6d 69 67 68 s,.** there migh
95760 74 20 62 65 20 72 61 63 65 20 63 6f 6e 64 69 74 t be race condit
95770 69 6f 6e 73 20 74 68 61 74 20 63 61 6e 20 63 61 ions that can ca
95780 75 73 65 20 74 68 65 73 65 20 72 6f 75 74 69 6e use these routin
95790 65 73 20 74 6f 0a 2a 2a 20 64 65 6c 69 76 65 72 es to.** deliver
957a0 20 69 6e 63 6f 72 72 65 63 74 20 72 65 73 75 6c incorrect resul
957b0 74 73 2e 20 20 49 6e 20 70 61 72 74 69 63 75 6c ts. In particul
957c0 61 72 2c 20 69 66 20 70 74 68 72 65 61 64 5f 65 ar, if pthread_e
957d0 71 75 61 6c 28 29 20 69 73 0a 2a 2a 20 6e 6f 74 qual() is.** not
957e0 20 61 6e 20 61 74 6f 6d 69 63 20 6f 70 65 72 61 an atomic opera
957f0 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 73 65 tion, then these
95800 20 72 6f 75 74 69 6e 65 73 20 6d 69 67 68 74 20 routines might
95810 64 65 6c 69 76 65 72 79 0a 2a 2a 20 69 6e 63 6f delivery.** inco
95820 72 72 65 63 74 20 72 65 73 75 6c 74 73 2e 20 20 rrect results.
95830 4f 6e 20 6d 6f 73 74 20 70 6c 61 74 66 6f 72 6d On most platform
95840 73 2c 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c s, pthread_equal
95850 28 29 20 69 73 20 61 20 0a 2a 2a 20 63 6f 6d 70 () is a .** comp
95860 61 72 69 73 6f 6e 20 6f 66 20 74 77 6f 20 69 6e arison of two in
95870 74 65 67 65 72 73 20 61 6e 64 20 69 73 20 74 68 tegers and is th
95880 65 72 65 66 6f 72 65 20 61 74 6f 6d 69 63 2e 20 erefore atomic.
95890 20 42 75 74 20 77 65 20 61 72 65 0a 2a 2a 20 74 But we are.** t
958a0 6f 6c 64 20 74 68 61 74 20 48 50 55 58 20 69 73 old that HPUX is
958b0 20 6e 6f 74 20 73 75 63 68 20 61 20 70 6c 61 74 not such a plat
958c0 66 6f 72 6d 2e 20 20 49 66 20 73 6f 2c 20 74 68 form. If so, th
958d0 65 6e 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 en these routine
958e0 73 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 61 6c s.** will not al
958f0 77 61 79 73 20 77 6f 72 6b 20 63 6f 72 72 65 63 ways work correc
95900 74 6c 79 20 6f 6e 20 48 50 55 58 2e 0a 2a 2a 0a tly on HPUX..**.
95910 2a 2a 20 4f 6e 20 74 68 6f 73 65 20 70 6c 61 74 ** On those plat
95920 66 6f 72 6d 73 20 77 68 65 72 65 20 70 74 68 72 forms where pthr
95930 65 61 64 5f 65 71 75 61 6c 28 29 20 69 73 20 6e ead_equal() is n
95940 6f 74 20 61 74 6f 6d 69 63 2c 20 53 51 4c 69 74 ot atomic, SQLit
95950 65 0a 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 63 e.** should be c
95960 6f 6d 70 69 6c 65 64 20 77 69 74 68 6f 75 74 20 ompiled without
95970 2d 44 53 51 4c 49 54 45 5f 44 45 42 55 47 20 61 -DSQLITE_DEBUG a
95980 6e 64 20 77 69 74 68 20 2d 44 4e 44 45 42 55 47 nd with -DNDEBUG
95990 20 74 6f 0a 2a 2a 20 6d 61 6b 65 20 73 75 72 65 to.** make sure
959a0 20 6e 6f 20 61 73 73 65 72 74 28 29 20 73 74 61 no assert() sta
959b0 74 65 6d 65 6e 74 73 20 61 72 65 20 65 76 61 6c tements are eval
959c0 75 61 74 65 64 20 61 6e 64 20 68 65 6e 63 65 20 uated and hence
959d0 74 68 65 73 65 0a 2a 2a 20 72 6f 75 74 69 6e 65 these.** routine
959e0 73 20 61 72 65 20 6e 65 76 65 72 20 63 61 6c 6c s are never call
959f0 65 64 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 ed..*/.#if !defi
95a00 6e 65 64 28 4e 44 45 42 55 47 29 20 7c 7c 20 64 ned(NDEBUG) || d
95a10 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 efined(SQLITE_DE
95a20 42 55 47 29 0a 73 74 61 74 69 63 20 69 6e 74 20 BUG).static int
95a30 70 74 68 72 65 61 64 4d 75 74 65 78 48 65 6c 64 pthreadMutexHeld
95a40 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a (sqlite3_mutex *
95a50 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 70 2d p){. return (p-
95a60 3e 6e 52 65 66 21 3d 30 20 26 26 20 70 74 68 72 >nRef!=0 && pthr
95a70 65 61 64 5f 65 71 75 61 6c 28 70 2d 3e 6f 77 6e ead_equal(p->own
95a80 65 72 2c 20 70 74 68 72 65 61 64 5f 73 65 6c 66 er, pthread_self
95a90 28 29 29 29 3b 0a 7d 0a 73 74 61 74 69 63 20 69 ()));.}.static i
95aa0 6e 74 20 70 74 68 72 65 61 64 4d 75 74 65 78 4e nt pthreadMutexN
95ab0 6f 74 68 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d otheld(sqlite3_m
95ac0 75 74 65 78 20 2a 70 29 7b 0a 20 20 72 65 74 75 utex *p){. retu
95ad0 72 6e 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c rn p->nRef==0 ||
95ae0 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28 70 pthread_equal(p
95af0 2d 3e 6f 77 6e 65 72 2c 20 70 74 68 72 65 61 64 ->owner, pthread
95b00 5f 73 65 6c 66 28 29 29 3d 3d 30 3b 0a 7d 0a 23 _self())==0;.}.#
95b10 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 endif../*.** Ini
95b20 74 69 61 6c 69 7a 65 20 61 6e 64 20 64 65 69 6e tialize and dein
95b30 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 75 74 itialize the mut
95b40 65 78 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f ex subsystem..*/
95b50 0a 73 74 61 74 69 63 20 69 6e 74 20 70 74 68 72 .static int pthr
95b60 65 61 64 4d 75 74 65 78 49 6e 69 74 28 76 6f 69 eadMutexInit(voi
95b70 64 29 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 d){ return SQLIT
95b80 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 20 69 E_OK; }.static i
95b90 6e 74 20 70 74 68 72 65 61 64 4d 75 74 65 78 45 nt pthreadMutexE
95ba0 6e 64 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e nd(void){ return
95bb0 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 0a 2f SQLITE_OK; }../
95bc0 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
95bd0 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 72 _mutex_alloc() r
95be0 6f 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74 65 73 outine allocates
95bf0 20 61 20 6e 65 77 0a 2a 2a 20 6d 75 74 65 78 20 a new.** mutex
95c00 61 6e 64 20 72 65 74 75 72 6e 73 20 61 20 70 6f and returns a po
95c10 69 6e 74 65 72 20 74 6f 20 69 74 2e 20 20 49 66 inter to it. If
95c20 20 69 74 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c it returns NULL
95c30 0a 2a 2a 20 74 68 61 74 20 6d 65 61 6e 73 20 74 .** that means t
95c40 68 61 74 20 61 20 6d 75 74 65 78 20 63 6f 75 6c hat a mutex coul
95c50 64 20 6e 6f 74 20 62 65 20 61 6c 6c 6f 63 61 74 d not be allocat
95c60 65 64 2e 20 20 53 51 4c 69 74 65 0a 2a 2a 20 77 ed. SQLite.** w
95c70 69 6c 6c 20 75 6e 77 69 6e 64 20 69 74 73 20 73 ill unwind its s
95c80 74 61 63 6b 20 61 6e 64 20 72 65 74 75 72 6e 20 tack and return
95c90 61 6e 20 65 72 72 6f 72 2e 20 20 54 68 65 20 61 an error. The a
95ca0 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 73 71 rgument.** to sq
95cb0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f lite3_mutex_allo
95cc0 63 28 29 20 69 73 20 6f 6e 65 20 6f 66 20 74 68 c() is one of th
95cd0 65 73 65 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 ese integer cons
95ce0 74 61 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c tants:.**.** <ul
95cf0 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 >.** <li> SQLIT
95d00 45 5f 4d 55 54 45 58 5f 46 41 53 54 0a 2a 2a 20 E_MUTEX_FAST.**
95d10 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 <li> SQLITE_MUT
95d20 45 58 5f 52 45 43 55 52 53 49 56 45 0a 2a 2a 20 EX_RECURSIVE.**
95d30 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 <li> SQLITE_MUT
95d40 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 EX_STATIC_MASTER
95d50 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 .** <li> SQLITE
95d60 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 _MUTEX_STATIC_ME
95d70 4d 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 M.** <li> SQLIT
95d80 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d E_MUTEX_STATIC_M
95d90 45 4d 32 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c EM2.** <li> SQL
95da0 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 ITE_MUTEX_STATIC
95db0 5f 50 52 4e 47 0a 2a 2a 20 3c 6c 69 3e 20 20 53 _PRNG.** <li> S
95dc0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 QLITE_MUTEX_STAT
95dd0 49 43 5f 4c 52 55 0a 2a 2a 20 3c 6c 69 3e 20 20 IC_LRU.** <li>
95de0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 SQLITE_MUTEX_STA
95df0 54 49 43 5f 4c 52 55 32 0a 2a 2a 20 3c 2f 75 6c TIC_LRU2.** </ul
95e00 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 >.**.** The firs
95e10 74 20 74 77 6f 20 63 6f 6e 73 74 61 6e 74 73 20 t two constants
95e20 63 61 75 73 65 20 73 71 6c 69 74 65 33 5f 6d 75 cause sqlite3_mu
95e30 74 65 78 5f 61 6c 6c 6f 63 28 29 20 74 6f 20 63 tex_alloc() to c
95e40 72 65 61 74 65 0a 2a 2a 20 61 20 6e 65 77 20 6d reate.** a new m
95e50 75 74 65 78 2e 20 20 54 68 65 20 6e 65 77 20 6d utex. The new m
95e60 75 74 65 78 20 69 73 20 72 65 63 75 72 73 69 76 utex is recursiv
95e70 65 20 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55 e when SQLITE_MU
95e80 54 45 58 5f 52 45 43 55 52 53 49 56 45 0a 2a 2a TEX_RECURSIVE.**
95e90 20 69 73 20 75 73 65 64 20 62 75 74 20 6e 6f 74 is used but not
95ea0 20 6e 65 63 65 73 73 61 72 69 6c 79 20 73 6f 20 necessarily so
95eb0 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 when SQLITE_MUTE
95ec0 58 5f 46 41 53 54 20 69 73 20 75 73 65 64 2e 0a X_FAST is used..
95ed0 2a 2a 20 54 68 65 20 6d 75 74 65 78 20 69 6d 70 ** The mutex imp
95ee0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 64 6f 65 73 lementation does
95ef0 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 6d 61 6b not need to mak
95f00 65 20 61 20 64 69 73 74 69 6e 63 74 69 6f 6e 0a e a distinction.
95f10 2a 2a 20 62 65 74 77 65 65 6e 20 53 51 4c 49 54 ** between SQLIT
95f20 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 E_MUTEX_RECURSIV
95f30 45 20 61 6e 64 20 53 51 4c 49 54 45 5f 4d 55 54 E and SQLITE_MUT
95f40 45 58 5f 46 41 53 54 20 69 66 20 69 74 20 64 6f EX_FAST if it do
95f50 65 73 0a 2a 2a 20 6e 6f 74 20 77 61 6e 74 20 74 es.** not want t
95f60 6f 2e 20 20 42 75 74 20 53 51 4c 69 74 65 20 77 o. But SQLite w
95f70 69 6c 6c 20 6f 6e 6c 79 20 72 65 71 75 65 73 74 ill only request
95f80 20 61 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 a recursive mut
95f90 65 78 20 69 6e 0a 2a 2a 20 63 61 73 65 73 20 77 ex in.** cases w
95fa0 68 65 72 65 20 69 74 20 72 65 61 6c 6c 79 20 6e here it really n
95fb0 65 65 64 73 20 6f 6e 65 2e 20 20 49 66 20 61 20 eeds one. If a
95fc0 66 61 73 74 65 72 20 6e 6f 6e 2d 72 65 63 75 72 faster non-recur
95fd0 73 69 76 65 20 6d 75 74 65 78 0a 2a 2a 20 69 6d sive mutex.** im
95fe0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 plementation is
95ff0 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 74 68 65 available on the
96000 20 68 6f 73 74 20 70 6c 61 74 66 6f 72 6d 2c 20 host platform,
96010 74 68 65 20 6d 75 74 65 78 20 73 75 62 73 79 73 the mutex subsys
96020 74 65 6d 0a 2a 2a 20 6d 69 67 68 74 20 72 65 74 tem.** might ret
96030 75 72 6e 20 73 75 63 68 20 61 20 6d 75 74 65 78 urn such a mutex
96040 20 69 6e 20 72 65 73 70 6f 6e 73 65 20 74 6f 20 in response to
96050 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 SQLITE_MUTEX_FAS
96060 54 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 74 68 T..**.** The oth
96070 65 72 20 61 6c 6c 6f 77 65 64 20 70 61 72 61 6d er allowed param
96080 65 74 65 72 73 20 74 6f 20 73 71 6c 69 74 65 33 eters to sqlite3
96090 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 65 _mutex_alloc() e
960a0 61 63 68 20 72 65 74 75 72 6e 0a 2a 2a 20 61 20 ach return.** a
960b0 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74 61 pointer to a sta
960c0 74 69 63 20 70 72 65 65 78 69 73 74 69 6e 67 20 tic preexisting
960d0 6d 75 74 65 78 2e 20 20 53 69 78 20 73 74 61 74 mutex. Six stat
960e0 69 63 20 6d 75 74 65 78 65 73 20 61 72 65 0a 2a ic mutexes are.*
960f0 2a 20 75 73 65 64 20 62 79 20 74 68 65 20 63 75 * used by the cu
96100 72 72 65 6e 74 20 76 65 72 73 69 6f 6e 20 6f 66 rrent version of
96110 20 53 51 4c 69 74 65 2e 20 20 46 75 74 75 72 65 SQLite. Future
96120 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c versions of SQL
96130 69 74 65 0a 2a 2a 20 6d 61 79 20 61 64 64 20 61 ite.** may add a
96140 64 64 69 74 69 6f 6e 61 6c 20 73 74 61 74 69 63 dditional static
96150 20 6d 75 74 65 78 65 73 2e 20 20 53 74 61 74 69 mutexes. Stati
96160 63 20 6d 75 74 65 78 65 73 20 61 72 65 20 66 6f c mutexes are fo
96170 72 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75 73 r internal.** us
96180 65 20 62 79 20 53 51 4c 69 74 65 20 6f 6e 6c 79 e by SQLite only
96190 2e 20 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 . Applications
961a0 74 68 61 74 20 75 73 65 20 53 51 4c 69 74 65 20 that use SQLite
961b0 6d 75 74 65 78 65 73 20 73 68 6f 75 6c 64 0a 2a mutexes should.*
961c0 2a 20 75 73 65 20 6f 6e 6c 79 20 74 68 65 20 64 * use only the d
961d0 79 6e 61 6d 69 63 20 6d 75 74 65 78 65 73 20 72 ynamic mutexes r
961e0 65 74 75 72 6e 65 64 20 62 79 20 53 51 4c 49 54 eturned by SQLIT
961f0 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 6f 72 0a E_MUTEX_FAST or.
96200 2a 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f ** SQLITE_MUTEX_
96210 52 45 43 55 52 53 49 56 45 2e 0a 2a 2a 0a 2a 2a RECURSIVE..**.**
96220 20 4e 6f 74 65 20 74 68 61 74 20 69 66 20 6f 6e Note that if on
96230 65 20 6f 66 20 74 68 65 20 64 79 6e 61 6d 69 63 e of the dynamic
96240 20 6d 75 74 65 78 20 70 61 72 61 6d 65 74 65 72 mutex parameter
96250 73 20 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f s (SQLITE_MUTEX_
96260 46 41 53 54 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 FAST.** or SQLIT
96270 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 E_MUTEX_RECURSIV
96280 45 29 20 69 73 20 75 73 65 64 20 74 68 65 6e 20 E) is used then
96290 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c sqlite3_mutex_al
962a0 6c 6f 63 28 29 0a 2a 2a 20 72 65 74 75 72 6e 73 loc().** returns
962b0 20 61 20 64 69 66 66 65 72 65 6e 74 20 6d 75 74 a different mut
962c0 65 78 20 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c ex on every call
962d0 2e 20 20 42 75 74 20 66 6f 72 20 74 68 65 20 73 . But for the s
962e0 74 61 74 69 63 20 0a 2a 2a 20 6d 75 74 65 78 20 tatic .** mutex
962f0 74 79 70 65 73 2c 20 74 68 65 20 73 61 6d 65 20 types, the same
96300 6d 75 74 65 78 20 69 73 20 72 65 74 75 72 6e 65 mutex is returne
96310 64 20 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c 20 d on every call
96320 74 68 61 74 20 68 61 73 0a 2a 2a 20 74 68 65 20 that has.** the
96330 73 61 6d 65 20 74 79 70 65 20 6e 75 6d 62 65 72 same type number
96340 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 71 6c 69 ..*/.static sqli
96350 74 65 33 5f 6d 75 74 65 78 20 2a 70 74 68 72 65 te3_mutex *pthre
96360 61 64 4d 75 74 65 78 41 6c 6c 6f 63 28 69 6e 74 adMutexAlloc(int
96370 20 69 54 79 70 65 29 7b 0a 20 20 73 74 61 74 69 iType){. stati
96380 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 c sqlite3_mutex
96390 73 74 61 74 69 63 4d 75 74 65 78 65 73 5b 5d 20 staticMutexes[]
963a0 3d 20 7b 0a 20 20 20 20 53 51 4c 49 54 45 33 5f = {. SQLITE3_
963b0 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 MUTEX_INITIALIZE
963c0 52 2c 0a 20 20 20 20 53 51 4c 49 54 45 33 5f 4d R,. SQLITE3_M
963d0 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 UTEX_INITIALIZER
963e0 2c 0a 20 20 20 20 53 51 4c 49 54 45 33 5f 4d 55 ,. SQLITE3_MU
963f0 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c TEX_INITIALIZER,
96400 0a 20 20 20 20 53 51 4c 49 54 45 33 5f 4d 55 54 . SQLITE3_MUT
96410 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 0a EX_INITIALIZER,.
96420 20 20 20 20 53 51 4c 49 54 45 33 5f 4d 55 54 45 SQLITE3_MUTE
96430 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 0a 20 X_INITIALIZER,.
96440 20 20 20 53 51 4c 49 54 45 33 5f 4d 55 54 45 58 SQLITE3_MUTEX
96450 5f 49 4e 49 54 49 41 4c 49 5a 45 52 0a 20 20 7d _INITIALIZER. }
96460 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 ;. sqlite3_mute
96470 78 20 2a 70 3b 0a 20 20 73 77 69 74 63 68 28 20 x *p;. switch(
96480 69 54 79 70 65 20 29 7b 0a 20 20 20 20 63 61 73 iType ){. cas
96490 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 e SQLITE_MUTEX_R
964a0 45 43 55 52 53 49 56 45 3a 20 7b 0a 20 20 20 20 ECURSIVE: {.
964b0 20 20 70 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c p = sqlite3Mal
964c0 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f 66 28 locZero( sizeof(
964d0 2a 70 29 20 29 3b 0a 20 20 20 20 20 20 69 66 28 *p) );. if(
964e0 20 70 20 29 7b 0a 23 69 66 64 65 66 20 53 51 4c p ){.#ifdef SQL
964f0 49 54 45 5f 48 4f 4d 45 47 52 4f 57 4e 5f 52 45 ITE_HOMEGROWN_RE
96500 43 55 52 53 49 56 45 5f 4d 55 54 45 58 0a 20 20 CURSIVE_MUTEX.
96510 20 20 20 20 20 20 2f 2a 20 49 66 20 72 65 63 75 /* If recu
96520 72 73 69 76 65 20 6d 75 74 65 78 65 73 20 61 72 rsive mutexes ar
96530 65 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 2c e not available,
96540 20 77 65 20 77 69 6c 6c 20 68 61 76 65 20 74 6f we will have to
96550 0a 20 20 20 20 20 20 20 20 2a 2a 20 62 75 69 6c . ** buil
96560 64 20 6f 75 72 20 6f 77 6e 2e 20 20 53 65 65 20 d our own. See
96570 62 65 6c 6f 77 2e 20 2a 2f 0a 20 20 20 20 20 20 below. */.
96580 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f pthread_mutex_
96590 69 6e 69 74 28 26 70 2d 3e 6d 75 74 65 78 2c 20 init(&p->mutex,
965a0 30 29 3b 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 0);.#else.
965b0 20 20 2f 2a 20 55 73 65 20 61 20 72 65 63 75 72 /* Use a recur
965c0 73 69 76 65 20 6d 75 74 65 78 20 69 66 20 69 74 sive mutex if it
965d0 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20 2a 2f is available */
965e0 0a 20 20 20 20 20 20 20 20 70 74 68 72 65 61 64 . pthread
965f0 5f 6d 75 74 65 78 61 74 74 72 5f 74 20 72 65 63 _mutexattr_t rec
96600 75 72 73 69 76 65 41 74 74 72 3b 0a 20 20 20 20 ursiveAttr;.
96610 20 20 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65 pthread_mute
96620 78 61 74 74 72 5f 69 6e 69 74 28 26 72 65 63 75 xattr_init(&recu
96630 72 73 69 76 65 41 74 74 72 29 3b 0a 20 20 20 20 rsiveAttr);.
96640 20 20 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65 pthread_mute
96650 78 61 74 74 72 5f 73 65 74 74 79 70 65 28 26 72 xattr_settype(&r
96660 65 63 75 72 73 69 76 65 41 74 74 72 2c 20 50 54 ecursiveAttr, PT
96670 48 52 45 41 44 5f 4d 55 54 45 58 5f 52 45 43 55 HREAD_MUTEX_RECU
96680 52 53 49 56 45 29 3b 0a 20 20 20 20 20 20 20 20 RSIVE);.
96690 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 69 6e pthread_mutex_in
966a0 69 74 28 26 70 2d 3e 6d 75 74 65 78 2c 20 26 72 it(&p->mutex, &r
966b0 65 63 75 72 73 69 76 65 41 74 74 72 29 3b 0a 20 ecursiveAttr);.
966c0 20 20 20 20 20 20 20 70 74 68 72 65 61 64 5f 6d pthread_m
966d0 75 74 65 78 61 74 74 72 5f 64 65 73 74 72 6f 79 utexattr_destroy
966e0 28 26 72 65 63 75 72 73 69 76 65 41 74 74 72 29 (&recursiveAttr)
966f0 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20 ;.#endif.
96700 20 70 2d 3e 69 64 20 3d 20 69 54 79 70 65 3b 0a p->id = iType;.
96710 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 }. br
96720 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 eak;. }. c
96730 61 73 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 ase SQLITE_MUTEX
96740 5f 46 41 53 54 3a 20 7b 0a 20 20 20 20 20 20 70 _FAST: {. p
96750 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 = sqlite3Malloc
96760 5a 65 72 6f 28 20 73 69 7a 65 6f 66 28 2a 70 29 Zero( sizeof(*p)
96770 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 20 );. if( p
96780 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 64 ){. p->id
96790 20 3d 20 69 54 79 70 65 3b 0a 20 20 20 20 20 20 = iType;.
967a0 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f pthread_mutex_
967b0 69 6e 69 74 28 26 70 2d 3e 6d 75 74 65 78 2c 20 init(&p->mutex,
967c0 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 0);. }.
967d0 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
967e0 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 default: {.
967f0 20 20 20 20 61 73 73 65 72 74 28 20 69 54 79 70 assert( iTyp
96800 65 2d 32 20 3e 3d 20 30 20 29 3b 0a 20 20 20 20 e-2 >= 0 );.
96810 20 20 61 73 73 65 72 74 28 20 69 54 79 70 65 2d assert( iType-
96820 32 20 3c 20 41 72 72 61 79 53 69 7a 65 28 73 74 2 < ArraySize(st
96830 61 74 69 63 4d 75 74 65 78 65 73 29 20 29 3b 0a aticMutexes) );.
96840 20 20 20 20 20 20 70 20 3d 20 26 73 74 61 74 69 p = &stati
96850 63 4d 75 74 65 78 65 73 5b 69 54 79 70 65 2d 32 cMutexes[iType-2
96860 5d 3b 0a 20 20 20 20 20 20 70 2d 3e 69 64 20 3d ];. p->id =
96870 20 69 54 79 70 65 3b 0a 20 20 20 20 20 20 62 72 iType;. br
96880 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 eak;. }. }.
96890 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 0a 2f return p;.}.../
968a0 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
968b0 65 20 64 65 61 6c 6c 6f 63 61 74 65 73 20 61 20 e deallocates a
968c0 70 72 65 76 69 6f 75 73 6c 79 0a 2a 2a 20 61 6c previously.** al
968d0 6c 6f 63 61 74 65 64 20 6d 75 74 65 78 2e 20 20 located mutex.
968e0 53 51 4c 69 74 65 20 69 73 20 63 61 72 65 66 75 SQLite is carefu
968f0 6c 20 74 6f 20 64 65 61 6c 6c 6f 63 61 74 65 20 l to deallocate
96900 65 76 65 72 79 0a 2a 2a 20 6d 75 74 65 78 20 74 every.** mutex t
96910 68 61 74 20 69 74 20 61 6c 6c 6f 63 61 74 65 73 hat it allocates
96920 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
96930 20 70 74 68 72 65 61 64 4d 75 74 65 78 46 72 65 pthreadMutexFre
96940 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 e(sqlite3_mutex
96950 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 *p){. assert( p
96960 2d 3e 6e 52 65 66 3d 3d 30 20 29 3b 0a 20 20 61 ->nRef==0 );. a
96970 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 ssert( p->id==SQ
96980 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 LITE_MUTEX_FAST
96990 7c 7c 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 || p->id==SQLITE
969a0 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 _MUTEX_RECURSIVE
969b0 20 29 3b 0a 20 20 70 74 68 72 65 61 64 5f 6d 75 );. pthread_mu
969c0 74 65 78 5f 64 65 73 74 72 6f 79 28 26 70 2d 3e tex_destroy(&p->
969d0 6d 75 74 65 78 29 3b 0a 20 20 73 71 6c 69 74 65 mutex);. sqlite
969e0 33 5f 66 72 65 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 3_free(p);.}../*
969f0 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
96a00 6d 75 74 65 78 5f 65 6e 74 65 72 28 29 20 61 6e mutex_enter() an
96a10 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f d sqlite3_mutex_
96a20 74 72 79 28 29 20 72 6f 75 74 69 6e 65 73 20 61 try() routines a
96a30 74 74 65 6d 70 74 0a 2a 2a 20 74 6f 20 65 6e 74 ttempt.** to ent
96a40 65 72 20 61 20 6d 75 74 65 78 2e 20 20 49 66 20 er a mutex. If
96a50 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 20 69 another thread i
96a60 73 20 61 6c 72 65 61 64 79 20 77 69 74 68 69 6e s already within
96a70 20 74 68 65 20 6d 75 74 65 78 2c 0a 2a 2a 20 73 the mutex,.** s
96a80 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 qlite3_mutex_ent
96a90 65 72 28 29 20 77 69 6c 6c 20 62 6c 6f 63 6b 20 er() will block
96aa0 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 and sqlite3_mute
96ab0 78 5f 74 72 79 28 29 20 77 69 6c 6c 20 72 65 74 x_try() will ret
96ac0 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 55 urn.** SQLITE_BU
96ad0 53 59 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 SY. The sqlite3
96ae0 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 69 6e 74 _mutex_try() int
96af0 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 53 erface returns S
96b00 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 75 70 6f 6e QLITE_OK.** upon
96b10 20 73 75 63 63 65 73 73 66 75 6c 20 65 6e 74 72 successful entr
96b20 79 2e 20 20 4d 75 74 65 78 65 73 20 63 72 65 61 y. Mutexes crea
96b30 74 65 64 20 75 73 69 6e 67 20 53 51 4c 49 54 45 ted using SQLITE
96b40 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 _MUTEX_RECURSIVE
96b50 20 63 61 6e 0a 2a 2a 20 62 65 20 65 6e 74 65 72 can.** be enter
96b60 65 64 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 ed multiple time
96b70 73 20 62 79 20 74 68 65 20 73 61 6d 65 20 74 68 s by the same th
96b80 72 65 61 64 2e 20 20 49 6e 20 73 75 63 68 20 63 read. In such c
96b90 61 73 65 73 20 74 68 65 2c 0a 2a 2a 20 6d 75 74 ases the,.** mut
96ba0 65 78 20 6d 75 73 74 20 62 65 20 65 78 69 74 65 ex must be exite
96bb0 64 20 61 6e 20 65 71 75 61 6c 20 6e 75 6d 62 65 d an equal numbe
96bc0 72 20 6f 66 20 74 69 6d 65 73 20 62 65 66 6f 72 r of times befor
96bd0 65 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 e another thread
96be0 0a 2a 2a 20 63 61 6e 20 65 6e 74 65 72 2e 20 20 .** can enter.
96bf0 49 66 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 If the same thre
96c00 61 64 20 74 72 69 65 73 20 74 6f 20 65 6e 74 65 ad tries to ente
96c10 72 20 61 6e 79 20 6f 74 68 65 72 20 6b 69 6e 64 r any other kind
96c20 20 6f 66 20 6d 75 74 65 78 0a 2a 2a 20 6d 6f 72 of mutex.** mor
96c30 65 20 74 68 61 6e 20 6f 6e 63 65 2c 20 74 68 65 e than once, the
96c40 20 62 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64 behavior is und
96c50 65 66 69 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 efined..*/.stati
96c60 63 20 76 6f 69 64 20 70 74 68 72 65 61 64 4d 75 c void pthreadMu
96c70 74 65 78 45 6e 74 65 72 28 73 71 6c 69 74 65 33 texEnter(sqlite3
96c80 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 73 _mutex *p){. as
96c90 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c sert( p->id==SQL
96ca0 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 ITE_MUTEX_RECURS
96cb0 49 56 45 20 7c 7c 20 70 74 68 72 65 61 64 4d 75 IVE || pthreadMu
96cc0 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b texNotheld(p) );
96cd0 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ..#ifdef SQLITE_
96ce0 48 4f 4d 45 47 52 4f 57 4e 5f 52 45 43 55 52 53 HOMEGROWN_RECURS
96cf0 49 56 45 5f 4d 55 54 45 58 0a 20 20 2f 2a 20 49 IVE_MUTEX. /* I
96d00 66 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 f recursive mute
96d10 78 65 73 20 61 72 65 20 6e 6f 74 20 61 76 61 69 xes are not avai
96d20 6c 61 62 6c 65 2c 20 74 68 65 6e 20 77 65 20 68 lable, then we h
96d30 61 76 65 20 74 6f 20 67 72 6f 77 0a 20 20 2a 2a ave to grow. **
96d40 20 6f 75 72 20 6f 77 6e 2e 20 20 54 68 69 73 20 our own. This
96d50 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 61 implementation a
96d60 73 73 75 6d 65 73 20 74 68 61 74 20 70 74 68 72 ssumes that pthr
96d70 65 61 64 5f 65 71 75 61 6c 28 29 0a 20 20 2a 2a ead_equal(). **
96d80 20 69 73 20 61 74 6f 6d 69 63 20 2d 20 74 68 61 is atomic - tha
96d90 74 20 69 74 20 63 61 6e 6e 6f 74 20 62 65 20 64 t it cannot be d
96da0 65 63 65 69 76 65 64 20 69 6e 74 6f 20 74 68 69 eceived into thi
96db0 6e 6b 69 6e 67 20 73 65 6c 66 0a 20 20 2a 2a 20 nking self. **
96dc0 61 6e 64 20 70 2d 3e 6f 77 6e 65 72 20 61 72 65 and p->owner are
96dd0 20 65 71 75 61 6c 20 69 66 20 70 2d 3e 6f 77 6e equal if p->own
96de0 65 72 20 63 68 61 6e 67 65 73 20 62 65 74 77 65 er changes betwe
96df0 65 6e 20 74 77 6f 20 76 61 6c 75 65 73 0a 20 20 en two values.
96e00 2a 2a 20 74 68 61 74 20 61 72 65 20 6e 6f 74 20 ** that are not
96e10 65 71 75 61 6c 20 74 6f 20 73 65 6c 66 20 77 68 equal to self wh
96e20 69 6c 65 20 74 68 65 20 63 6f 6d 70 61 72 69 73 ile the comparis
96e30 6f 6e 20 69 73 20 74 61 6b 69 6e 67 20 70 6c 61 on is taking pla
96e40 63 65 2e 0a 20 20 2a 2a 20 54 68 69 73 20 69 6d ce.. ** This im
96e50 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 61 6c 73 plementation als
96e60 6f 20 61 73 73 75 6d 65 73 20 61 20 63 6f 68 65 o assumes a cohe
96e70 72 65 6e 74 20 63 61 63 68 65 20 2d 20 74 68 61 rent cache - tha
96e80 74 20 0a 20 20 2a 2a 20 73 65 70 61 72 61 74 65 t . ** separate
96e90 20 70 72 6f 63 65 73 73 65 73 20 63 61 6e 6e 6f processes canno
96ea0 74 20 72 65 61 64 20 64 69 66 66 65 72 65 6e 74 t read different
96eb0 20 76 61 6c 75 65 73 20 66 72 6f 6d 20 74 68 65 values from the
96ec0 20 73 61 6d 65 0a 20 20 2a 2a 20 61 64 64 72 65 same. ** addre
96ed0 73 73 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 ss at the same t
96ee0 69 6d 65 2e 20 20 49 66 20 65 69 74 68 65 72 20 ime. If either
96ef0 6f 66 20 74 68 65 73 65 20 74 77 6f 20 63 6f 6e of these two con
96f00 64 69 74 69 6f 6e 73 0a 20 20 2a 2a 20 61 72 65 ditions. ** are
96f10 20 6e 6f 74 20 6d 65 74 2c 20 74 68 65 6e 20 74 not met, then t
96f20 68 65 20 6d 75 74 65 78 65 73 20 77 69 6c 6c 20 he mutexes will
96f30 66 61 69 6c 20 61 6e 64 20 70 72 6f 62 6c 65 6d fail and problem
96f40 73 20 77 69 6c 6c 20 72 65 73 75 6c 74 2e 0a 20 s will result..
96f50 20 2a 2f 0a 20 20 7b 0a 20 20 20 20 70 74 68 72 */. {. pthr
96f60 65 61 64 5f 74 20 73 65 6c 66 20 3d 20 70 74 68 ead_t self = pth
96f70 72 65 61 64 5f 73 65 6c 66 28 29 3b 0a 20 20 20 read_self();.
96f80 20 69 66 28 20 70 2d 3e 6e 52 65 66 3e 30 20 26 if( p->nRef>0 &
96f90 26 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28 & pthread_equal(
96fa0 70 2d 3e 6f 77 6e 65 72 2c 20 73 65 6c 66 29 20 p->owner, self)
96fb0 29 7b 0a 20 20 20 20 20 20 70 2d 3e 6e 52 65 66 ){. p->nRef
96fc0 2b 2b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ++;. }else{.
96fd0 20 20 20 20 20 70 74 68 72 65 61 64 5f 6d 75 74 pthread_mut
96fe0 65 78 5f 6c 6f 63 6b 28 26 70 2d 3e 6d 75 74 65 ex_lock(&p->mute
96ff0 78 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 x);. assert
97000 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 29 3b 0a ( p->nRef==0 );.
97010 20 20 20 20 20 20 70 2d 3e 6f 77 6e 65 72 20 3d p->owner =
97020 20 73 65 6c 66 3b 0a 20 20 20 20 20 20 70 2d 3e self;. p->
97030 6e 52 65 66 20 3d 20 31 3b 0a 20 20 20 20 7d 0a nRef = 1;. }.
97040 20 20 7d 0a 23 65 6c 73 65 0a 20 20 2f 2a 20 55 }.#else. /* U
97050 73 65 20 74 68 65 20 62 75 69 6c 74 2d 69 6e 20 se the built-in
97060 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 65 recursive mutexe
97070 73 20 69 66 20 74 68 65 79 20 61 72 65 20 61 76 s if they are av
97080 61 69 6c 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 ailable.. */.
97090 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 6c 6f pthread_mutex_lo
970a0 63 6b 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 20 ck(&p->mutex);.
970b0 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 70 74 68 72 p->owner = pthr
970c0 65 61 64 5f 73 65 6c 66 28 29 3b 0a 20 20 70 2d ead_self();. p-
970d0 3e 6e 52 65 66 2b 2b 3b 0a 23 65 6e 64 69 66 0a >nRef++;.#endif.
970e0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 .#ifdef SQLITE_D
970f0 45 42 55 47 0a 20 20 69 66 28 20 70 2d 3e 74 72 EBUG. if( p->tr
97100 61 63 65 20 29 7b 0a 20 20 20 20 70 72 69 6e 74 ace ){. print
97110 66 28 22 65 6e 74 65 72 20 6d 75 74 65 78 20 25 f("enter mutex %
97120 70 20 28 25 64 29 20 77 69 74 68 20 6e 52 65 66 p (%d) with nRef
97130 3d 25 64 5c 6e 22 2c 20 70 2c 20 70 2d 3e 74 72 =%d\n", p, p->tr
97140 61 63 65 2c 20 70 2d 3e 6e 52 65 66 29 3b 0a 20 ace, p->nRef);.
97150 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 73 74 61 74 }.#endif.}.stat
97160 69 63 20 69 6e 74 20 70 74 68 72 65 61 64 4d 75 ic int pthreadMu
97170 74 65 78 54 72 79 28 73 71 6c 69 74 65 33 5f 6d texTry(sqlite3_m
97180 75 74 65 78 20 2a 70 29 7b 0a 20 20 69 6e 74 20 utex *p){. int
97190 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d rc;. assert( p-
971a0 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 >id==SQLITE_MUTE
971b0 58 5f 52 45 43 55 52 53 49 56 45 20 7c 7c 20 70 X_RECURSIVE || p
971c0 74 68 72 65 61 64 4d 75 74 65 78 4e 6f 74 68 65 threadMutexNothe
971d0 6c 64 28 70 29 20 29 3b 0a 0a 23 69 66 64 65 66 ld(p) );..#ifdef
971e0 20 53 51 4c 49 54 45 5f 48 4f 4d 45 47 52 4f 57 SQLITE_HOMEGROW
971f0 4e 5f 52 45 43 55 52 53 49 56 45 5f 4d 55 54 45 N_RECURSIVE_MUTE
97200 58 0a 20 20 2f 2a 20 49 66 20 72 65 63 75 72 73 X. /* If recurs
97210 69 76 65 20 6d 75 74 65 78 65 73 20 61 72 65 20 ive mutexes are
97220 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 2c 20 74 not available, t
97230 68 65 6e 20 77 65 20 68 61 76 65 20 74 6f 20 67 hen we have to g
97240 72 6f 77 0a 20 20 2a 2a 20 6f 75 72 20 6f 77 6e row. ** our own
97250 2e 20 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e . This implemen
97260 74 61 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 tation assumes t
97270 68 61 74 20 70 74 68 72 65 61 64 5f 65 71 75 61 hat pthread_equa
97280 6c 28 29 0a 20 20 2a 2a 20 69 73 20 61 74 6f 6d l(). ** is atom
97290 69 63 20 2d 20 74 68 61 74 20 69 74 20 63 61 6e ic - that it can
972a0 6e 6f 74 20 62 65 20 64 65 63 65 69 76 65 64 20 not be deceived
972b0 69 6e 74 6f 20 74 68 69 6e 6b 69 6e 67 20 73 65 into thinking se
972c0 6c 66 0a 20 20 2a 2a 20 61 6e 64 20 70 2d 3e 6f lf. ** and p->o
972d0 77 6e 65 72 20 61 72 65 20 65 71 75 61 6c 20 69 wner are equal i
972e0 66 20 70 2d 3e 6f 77 6e 65 72 20 63 68 61 6e 67 f p->owner chang
972f0 65 73 20 62 65 74 77 65 65 6e 20 74 77 6f 20 76 es between two v
97300 61 6c 75 65 73 0a 20 20 2a 2a 20 74 68 61 74 20 alues. ** that
97310 61 72 65 20 6e 6f 74 20 65 71 75 61 6c 20 74 6f are not equal to
97320 20 73 65 6c 66 20 77 68 69 6c 65 20 74 68 65 20 self while the
97330 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 74 61 comparison is ta
97340 6b 69 6e 67 20 70 6c 61 63 65 2e 0a 20 20 2a 2a king place.. **
97350 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 This implementa
97360 74 69 6f 6e 20 61 6c 73 6f 20 61 73 73 75 6d 65 tion also assume
97370 73 20 61 20 63 6f 68 65 72 65 6e 74 20 63 61 63 s a coherent cac
97380 68 65 20 2d 20 74 68 61 74 20 0a 20 20 2a 2a 20 he - that . **
97390 73 65 70 61 72 61 74 65 20 70 72 6f 63 65 73 73 separate process
973a0 65 73 20 63 61 6e 6e 6f 74 20 72 65 61 64 20 64 es cannot read d
973b0 69 66 66 65 72 65 6e 74 20 76 61 6c 75 65 73 20 ifferent values
973c0 66 72 6f 6d 20 74 68 65 20 73 61 6d 65 0a 20 20 from the same.
973d0 2a 2a 20 61 64 64 72 65 73 73 20 61 74 20 74 68 ** address at th
973e0 65 20 73 61 6d 65 20 74 69 6d 65 2e 20 20 49 66 e same time. If
973f0 20 65 69 74 68 65 72 20 6f 66 20 74 68 65 73 65 either of these
97400 20 74 77 6f 20 63 6f 6e 64 69 74 69 6f 6e 73 0a two conditions.
97410 20 20 2a 2a 20 61 72 65 20 6e 6f 74 20 6d 65 74 ** are not met
97420 2c 20 74 68 65 6e 20 74 68 65 20 6d 75 74 65 78 , then the mutex
97430 65 73 20 77 69 6c 6c 20 66 61 69 6c 20 61 6e 64 es will fail and
97440 20 70 72 6f 62 6c 65 6d 73 20 77 69 6c 6c 20 72 problems will r
97450 65 73 75 6c 74 2e 0a 20 20 2a 2f 0a 20 20 7b 0a esult.. */. {.
97460 20 20 20 20 70 74 68 72 65 61 64 5f 74 20 73 65 pthread_t se
97470 6c 66 20 3d 20 70 74 68 72 65 61 64 5f 73 65 6c lf = pthread_sel
97480 66 28 29 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e f();. if( p->
97490 6e 52 65 66 3e 30 20 26 26 20 70 74 68 72 65 61 nRef>0 && pthrea
974a0 64 5f 65 71 75 61 6c 28 70 2d 3e 6f 77 6e 65 72 d_equal(p->owner
974b0 2c 20 73 65 6c 66 29 20 29 7b 0a 20 20 20 20 20 , self) ){.
974c0 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 p->nRef++;.
974d0 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b rc = SQLITE_OK
974e0 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 ;. }else if(
974f0 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 74 72 pthread_mutex_tr
97500 79 6c 6f 63 6b 28 26 70 2d 3e 6d 75 74 65 78 29 ylock(&p->mutex)
97510 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73 ==0 ){. ass
97520 65 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 ert( p->nRef==0
97530 29 3b 0a 20 20 20 20 20 20 70 2d 3e 6f 77 6e 65 );. p->owne
97540 72 20 3d 20 73 65 6c 66 3b 0a 20 20 20 20 20 20 r = self;.
97550 70 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20 20 p->nRef = 1;.
97560 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f rc = SQLITE_O
97570 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 K;. }else{.
97580 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
97590 42 55 53 59 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a BUSY;. }. }.
975a0 23 65 6c 73 65 0a 20 20 2f 2a 20 55 73 65 20 74 #else. /* Use t
975b0 68 65 20 62 75 69 6c 74 2d 69 6e 20 72 65 63 75 he built-in recu
975c0 72 73 69 76 65 20 6d 75 74 65 78 65 73 20 69 66 rsive mutexes if
975d0 20 74 68 65 79 20 61 72 65 20 61 76 61 69 6c 61 they are availa
975e0 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 ble.. */. if(
975f0 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 74 72 pthread_mutex_tr
97600 79 6c 6f 63 6b 28 26 70 2d 3e 6d 75 74 65 78 29 ylock(&p->mutex)
97610 3d 3d 30 20 29 7b 0a 20 20 20 20 70 2d 3e 6f 77 ==0 ){. p->ow
97620 6e 65 72 20 3d 20 70 74 68 72 65 61 64 5f 73 65 ner = pthread_se
97630 6c 66 28 29 3b 0a 20 20 20 20 70 2d 3e 6e 52 65 lf();. p->nRe
97640 66 2b 2b 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 f++;. rc = SQ
97650 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 LITE_OK;. }else
97660 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 {. rc = SQLIT
97670 45 5f 42 55 53 59 3b 0a 20 20 7d 0a 23 65 6e 64 E_BUSY;. }.#end
97680 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 if..#ifdef SQLIT
97690 45 5f 44 45 42 55 47 0a 20 20 69 66 28 20 72 63 E_DEBUG. if( rc
976a0 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 ==SQLITE_OK && p
976b0 2d 3e 74 72 61 63 65 20 29 7b 0a 20 20 20 20 70 ->trace ){. p
976c0 72 69 6e 74 66 28 22 65 6e 74 65 72 20 6d 75 74 rintf("enter mut
976d0 65 78 20 25 70 20 28 25 64 29 20 77 69 74 68 20 ex %p (%d) with
976e0 6e 52 65 66 3d 25 64 5c 6e 22 2c 20 70 2c 20 70 nRef=%d\n", p, p
976f0 2d 3e 74 72 61 63 65 2c 20 70 2d 3e 6e 52 65 66 ->trace, p->nRef
97700 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 );. }.#endif.
97710 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
97720 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
97730 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 20 72 6f mutex_leave() ro
97740 75 74 69 6e 65 20 65 78 69 74 73 20 61 20 6d 75 utine exits a mu
97750 74 65 78 20 74 68 61 74 20 77 61 73 0a 2a 2a 20 tex that was.**
97760 70 72 65 76 69 6f 75 73 6c 79 20 65 6e 74 65 72 previously enter
97770 65 64 20 62 79 20 74 68 65 20 73 61 6d 65 20 74 ed by the same t
97780 68 72 65 61 64 2e 20 20 54 68 65 20 62 65 68 61 hread. The beha
97790 76 69 6f 72 0a 2a 2a 20 69 73 20 75 6e 64 65 66 vior.** is undef
977a0 69 6e 65 64 20 69 66 20 74 68 65 20 6d 75 74 65 ined if the mute
977b0 78 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 x is not current
977c0 6c 79 20 65 6e 74 65 72 65 64 20 6f 72 0a 2a 2a ly entered or.**
977d0 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c is not currentl
977e0 79 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 53 51 y allocated. SQ
977f0 4c 69 74 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 Lite will never
97800 64 6f 20 65 69 74 68 65 72 2e 0a 2a 2f 0a 73 74 do either..*/.st
97810 61 74 69 63 20 76 6f 69 64 20 70 74 68 72 65 61 atic void pthrea
97820 64 4d 75 74 65 78 4c 65 61 76 65 28 73 71 6c 69 dMutexLeave(sqli
97830 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 te3_mutex *p){.
97840 20 61 73 73 65 72 74 28 20 70 74 68 72 65 61 64 assert( pthread
97850 4d 75 74 65 78 48 65 6c 64 28 70 29 20 29 3b 0a MutexHeld(p) );.
97860 20 20 70 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 61 p->nRef--;. a
97870 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d ssert( p->nRef==
97880 30 20 7c 7c 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 0 || p->id==SQLI
97890 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 TE_MUTEX_RECURSI
978a0 56 45 20 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 VE );..#ifdef SQ
978b0 4c 49 54 45 5f 48 4f 4d 45 47 52 4f 57 4e 5f 52 LITE_HOMEGROWN_R
978c0 45 43 55 52 53 49 56 45 5f 4d 55 54 45 58 0a 20 ECURSIVE_MUTEX.
978d0 20 69 66 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 if( p->nRef==0
978e0 29 7b 0a 20 20 20 20 70 74 68 72 65 61 64 5f 6d ){. pthread_m
978f0 75 74 65 78 5f 75 6e 6c 6f 63 6b 28 26 70 2d 3e utex_unlock(&p->
97900 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 23 65 6c 73 mutex);. }.#els
97910 65 0a 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65 e. pthread_mute
97920 78 5f 75 6e 6c 6f 63 6b 28 26 70 2d 3e 6d 75 74 x_unlock(&p->mut
97930 65 78 29 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 ex);.#endif..#if
97940 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 def SQLITE_DEBUG
97950 0a 20 20 69 66 28 20 70 2d 3e 74 72 61 63 65 20 . if( p->trace
97960 29 7b 0a 20 20 20 20 70 72 69 6e 74 66 28 22 6c ){. printf("l
97970 65 61 76 65 20 6d 75 74 65 78 20 25 70 20 28 25 eave mutex %p (%
97980 64 29 20 77 69 74 68 20 6e 52 65 66 3d 25 64 5c d) with nRef=%d\
97990 6e 22 2c 20 70 2c 20 70 2d 3e 74 72 61 63 65 2c n", p, p->trace,
979a0 20 70 2d 3e 6e 52 65 66 29 3b 0a 20 20 7d 0a 23 p->nRef);. }.#
979b0 65 6e 64 69 66 0a 7d 0a 0a 53 51 4c 49 54 45 5f endif.}..SQLITE_
979c0 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f PRIVATE sqlite3_
979d0 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 2a 73 mutex_methods *s
979e0 71 6c 69 74 65 33 44 65 66 61 75 6c 74 4d 75 74 qlite3DefaultMut
979f0 65 78 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 ex(void){. stat
97a00 69 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ic sqlite3_mutex
97a10 5f 6d 65 74 68 6f 64 73 20 73 4d 75 74 65 78 20 _methods sMutex
97a20 3d 20 7b 0a 20 20 20 20 70 74 68 72 65 61 64 4d = {. pthreadM
97a30 75 74 65 78 49 6e 69 74 2c 0a 20 20 20 20 70 74 utexInit,. pt
97a40 68 72 65 61 64 4d 75 74 65 78 45 6e 64 2c 0a 20 hreadMutexEnd,.
97a50 20 20 20 70 74 68 72 65 61 64 4d 75 74 65 78 41 pthreadMutexA
97a60 6c 6c 6f 63 2c 0a 20 20 20 20 70 74 68 72 65 61 lloc,. pthrea
97a70 64 4d 75 74 65 78 46 72 65 65 2c 0a 20 20 20 20 dMutexFree,.
97a80 70 74 68 72 65 61 64 4d 75 74 65 78 45 6e 74 65 pthreadMutexEnte
97a90 72 2c 0a 20 20 20 20 70 74 68 72 65 61 64 4d 75 r,. pthreadMu
97aa0 74 65 78 54 72 79 2c 0a 20 20 20 20 70 74 68 72 texTry,. pthr
97ab0 65 61 64 4d 75 74 65 78 4c 65 61 76 65 2c 0a 23 eadMutexLeave,.#
97ac0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 ifdef SQLITE_DEB
97ad0 55 47 0a 20 20 20 20 70 74 68 72 65 61 64 4d 75 UG. pthreadMu
97ae0 74 65 78 48 65 6c 64 2c 0a 20 20 20 20 70 74 68 texHeld,. pth
97af0 72 65 61 64 4d 75 74 65 78 4e 6f 74 68 65 6c 64 readMutexNotheld
97b00 0a 23 65 6c 73 65 0a 20 20 20 20 30 2c 0a 20 20 .#else. 0,.
97b10 20 20 30 0a 23 65 6e 64 69 66 0a 20 20 7d 3b 0a 0.#endif. };.
97b20 0a 20 20 72 65 74 75 72 6e 20 26 73 4d 75 74 65 . return &sMute
97b30 78 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 x;.}..#endif /*
97b40 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 50 54 48 SQLITE_MUTEX_PTH
97b50 52 45 41 44 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a READ */../******
97b60 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 ******** End of
97b70 6d 75 74 65 78 5f 75 6e 69 78 2e 63 20 2a 2a 2a mutex_unix.c ***
97b80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
97b90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
97ba0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
97bb0 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 ******** Begin f
97bc0 69 6c 65 20 6d 75 74 65 78 5f 77 33 32 2e 63 20 ile mutex_w32.c
97bd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
97be0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
97bf0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 *******/./*.** 2
97c00 30 30 37 20 41 75 67 75 73 74 20 31 34 0a 2a 2a 007 August 14.**
97c10 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 .** The author d
97c20 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 isclaims copyrig
97c30 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 ht to this sourc
97c40 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 e code. In plac
97c50 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 e of.** a legal
97c60 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 notice, here is
97c70 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a a blessing:.**.*
97c80 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 * May you do
97c90 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 good and not evi
97ca0 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 l..** May you
97cb0 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 find forgivenes
97cc0 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 s for yourself a
97cd0 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 nd forgive other
97ce0 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 s..** May you
97cf0 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e share freely, n
97d00 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 ever taking more
97d10 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a than you give..
97d20 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
97d30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
97d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
97d50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
97d60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 ************.**
97d70 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 This file contai
97d80 6e 73 20 74 68 65 20 43 20 66 75 6e 63 74 69 6f ns the C functio
97d90 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e ns that implemen
97da0 74 20 6d 75 74 65 78 65 73 20 66 6f 72 20 77 69 t mutexes for wi
97db0 6e 33 32 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d n32.**.** $Id: m
97dc0 75 74 65 78 5f 77 33 32 2e 63 2c 76 20 31 2e 31 utex_w32.c,v 1.1
97dd0 38 20 32 30 30 39 2f 30 38 2f 31 30 20 30 33 3a 8 2009/08/10 03:
97de0 32 33 3a 32 31 20 73 68 61 6e 65 20 45 78 70 20 23:21 shane Exp
97df0 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 $.*/../*.** The
97e00 63 6f 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c code in this fil
97e10 65 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 69 e is only used i
97e20 66 20 77 65 20 61 72 65 20 63 6f 6d 70 69 6c 69 f we are compili
97e30 6e 67 20 6d 75 6c 74 69 74 68 72 65 61 64 65 64 ng multithreaded
97e40 0a 2a 2a 20 6f 6e 20 61 20 77 69 6e 33 32 20 73 .** on a win32 s
97e50 79 73 74 65 6d 2e 0a 2a 2f 0a 23 69 66 64 65 66 ystem..*/.#ifdef
97e60 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 57 33 SQLITE_MUTEX_W3
97e70 32 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 72 65 2../*.** Each re
97e80 63 75 72 73 69 76 65 20 6d 75 74 65 78 20 69 73 cursive mutex is
97e90 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 an instance of
97ea0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 the following st
97eb0 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 72 75 ructure..*/.stru
97ec0 63 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ct sqlite3_mutex
97ed0 20 7b 0a 20 20 43 52 49 54 49 43 41 4c 5f 53 45 {. CRITICAL_SE
97ee0 43 54 49 4f 4e 20 6d 75 74 65 78 3b 20 20 20 20 CTION mutex;
97ef0 2f 2a 20 4d 75 74 65 78 20 63 6f 6e 74 72 6f 6c /* Mutex control
97f00 6c 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 2a 2f ling the lock */
97f10 0a 20 20 69 6e 74 20 69 64 3b 20 20 20 20 20 20 . int id;
97f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
97f30 20 4d 75 74 65 78 20 74 79 70 65 20 2a 2f 0a 20 Mutex type */.
97f40 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20 int nRef;
97f50 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
97f60 75 6d 62 65 72 20 6f 66 20 65 6e 74 65 72 61 6e umber of enteran
97f70 63 65 73 20 2a 2f 0a 20 20 44 57 4f 52 44 20 6f ces */. DWORD o
97f80 77 6e 65 72 3b 20 20 20 20 20 20 20 20 20 20 20 wner;
97f90 20 20 20 20 2f 2a 20 54 68 72 65 61 64 20 68 6f /* Thread ho
97fa0 6c 64 69 6e 67 20 74 68 69 73 20 6d 75 74 65 78 lding this mutex
97fb0 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65 */.};../*.** Re
97fc0 74 75 72 6e 20 74 72 75 65 20 28 6e 6f 6e 2d 7a turn true (non-z
97fd0 65 72 6f 29 20 69 66 20 77 65 20 61 72 65 20 72 ero) if we are r
97fe0 75 6e 6e 69 6e 67 20 75 6e 64 65 72 20 57 69 6e unning under Win
97ff0 4e 54 2c 20 57 69 6e 32 4b 2c 20 57 69 6e 58 50 NT, Win2K, WinXP
98000 2c 0a 2a 2a 20 6f 72 20 57 69 6e 43 45 2e 20 20 ,.** or WinCE.
98010 52 65 74 75 72 6e 20 66 61 6c 73 65 20 28 7a 65 Return false (ze
98020 72 6f 29 20 66 6f 72 20 57 69 6e 39 35 2c 20 57 ro) for Win95, W
98030 69 6e 39 38 2c 20 6f 72 20 57 69 6e 4d 45 2e 0a in98, or WinME..
98040 2a 2a 0a 2a 2a 20 48 65 72 65 20 69 73 20 61 6e **.** Here is an
98050 20 69 6e 74 65 72 65 73 74 69 6e 67 20 6f 62 73 interesting obs
98060 65 72 76 61 74 69 6f 6e 3a 20 20 57 69 6e 39 35 ervation: Win95
98070 2c 20 57 69 6e 39 38 2c 20 61 6e 64 20 57 69 6e , Win98, and Win
98080 4d 45 20 6c 61 63 6b 0a 2a 2a 20 74 68 65 20 4c ME lack.** the L
98090 6f 63 6b 46 69 6c 65 45 78 28 29 20 41 50 49 2e ockFileEx() API.
980a0 20 20 42 75 74 20 77 65 20 63 61 6e 20 73 74 69 But we can sti
980b0 6c 6c 20 73 74 61 74 69 63 61 6c 6c 79 20 6c 69 ll statically li
980c0 6e 6b 20 61 67 61 69 6e 73 74 20 74 68 61 74 0a nk against that.
980d0 2a 2a 20 41 50 49 20 61 73 20 6c 6f 6e 67 20 61 ** API as long a
980e0 73 20 77 65 20 64 6f 6e 27 74 20 63 61 6c 6c 20 s we don't call
980f0 69 74 20 77 69 6e 20 72 75 6e 6e 69 6e 67 20 57 it win running W
98100 69 6e 39 35 2f 39 38 2f 4d 45 2e 20 20 41 20 63 in95/98/ME. A c
98110 61 6c 6c 20 74 6f 0a 2a 2a 20 74 68 69 73 20 72 all to.** this r
98120 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 74 outine is used t
98130 6f 20 64 65 74 65 72 6d 69 6e 65 20 69 66 20 74 o determine if t
98140 68 65 20 68 6f 73 74 20 69 73 20 57 69 6e 39 35 he host is Win95
98150 2f 39 38 2f 4d 45 20 6f 72 0a 2a 2a 20 57 69 6e /98/ME or.** Win
98160 4e 54 2f 32 4b 2f 58 50 20 73 6f 20 74 68 61 74 NT/2K/XP so that
98170 20 77 65 20 77 69 6c 6c 20 6b 6e 6f 77 20 77 68 we will know wh
98180 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 77 65 20 ether or not we
98190 63 61 6e 20 73 61 66 65 6c 79 20 63 61 6c 6c 0a can safely call.
981a0 2a 2a 20 74 68 65 20 4c 6f 63 6b 46 69 6c 65 45 ** the LockFileE
981b0 78 28 29 20 41 50 49 2e 0a 2a 2a 0a 2a 2a 20 6d x() API..**.** m
981c0 75 74 65 78 49 73 4e 54 28 29 20 69 73 20 6f 6e utexIsNT() is on
981d0 6c 79 20 75 73 65 64 20 66 6f 72 20 74 68 65 20 ly used for the
981e0 54 72 79 45 6e 74 65 72 43 72 69 74 69 63 61 6c TryEnterCritical
981f0 53 65 63 74 69 6f 6e 28 29 20 41 50 49 20 63 61 Section() API ca
98200 6c 6c 2c 0a 2a 2a 20 77 68 69 63 68 20 69 73 20 ll,.** which is
98210 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 69 only available i
98220 66 20 79 6f 75 72 20 61 70 70 6c 69 63 61 74 69 f your applicati
98230 6f 6e 20 77 61 73 20 63 6f 6d 70 69 6c 65 64 20 on was compiled
98240 77 69 74 68 20 0a 2a 2a 20 5f 57 49 4e 33 32 5f with .** _WIN32_
98250 57 49 4e 4e 54 20 64 65 66 69 6e 65 64 20 74 6f WINNT defined to
98260 20 61 20 76 61 6c 75 65 20 3e 3d 20 30 78 30 34 a value >= 0x04
98270 30 30 2e 20 20 43 75 72 72 65 6e 74 6c 79 2c 20 00. Currently,
98280 74 68 65 20 6f 6e 6c 79 0a 2a 2a 20 63 61 6c 6c the only.** call
98290 20 74 6f 20 54 72 79 45 6e 74 65 72 43 72 69 74 to TryEnterCrit
982a0 69 63 61 6c 53 65 63 74 69 6f 6e 28 29 20 69 73 icalSection() is
982b0 20 23 69 66 64 65 66 27 65 64 20 6f 75 74 2c 20 #ifdef'ed out,
982c0 73 6f 20 23 69 66 64 65 66 20 0a 2a 2a 20 74 68 so #ifdef .** th
982d0 69 73 20 6f 75 74 20 61 73 20 77 65 6c 6c 2e 0a is out as well..
982e0 2a 2f 0a 23 69 66 20 30 0a 23 69 66 20 53 51 4c */.#if 0.#if SQL
982f0 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 23 20 64 ITE_OS_WINCE.# d
98300 65 66 69 6e 65 20 6d 75 74 65 78 49 73 4e 54 28 efine mutexIsNT(
98310 29 20 20 28 31 29 0a 23 65 6c 73 65 0a 20 20 73 ) (1).#else. s
98320 74 61 74 69 63 20 69 6e 74 20 6d 75 74 65 78 49 tatic int mutexI
98330 73 4e 54 28 76 6f 69 64 29 7b 0a 20 20 20 20 73 sNT(void){. s
98340 74 61 74 69 63 20 69 6e 74 20 6f 73 54 79 70 65 tatic int osType
98350 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 6f 73 = 0;. if( os
98360 54 79 70 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 Type==0 ){.
98370 20 4f 53 56 45 52 53 49 4f 4e 49 4e 46 4f 20 73 OSVERSIONINFO s
98380 49 6e 66 6f 3b 0a 20 20 20 20 20 20 73 49 6e 66 Info;. sInf
98390 6f 2e 64 77 4f 53 56 65 72 73 69 6f 6e 49 6e 66 o.dwOSVersionInf
983a0 6f 53 69 7a 65 20 3d 20 73 69 7a 65 6f 66 28 73 oSize = sizeof(s
983b0 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 47 65 74 Info);. Get
983c0 56 65 72 73 69 6f 6e 45 78 28 26 73 49 6e 66 6f VersionEx(&sInfo
983d0 29 3b 0a 20 20 20 20 20 20 6f 73 54 79 70 65 20 );. osType
983e0 3d 20 73 49 6e 66 6f 2e 64 77 50 6c 61 74 66 6f = sInfo.dwPlatfo
983f0 72 6d 49 64 3d 3d 56 45 52 5f 50 4c 41 54 46 4f rmId==VER_PLATFO
98400 52 4d 5f 57 49 4e 33 32 5f 4e 54 20 3f 20 32 20 RM_WIN32_NT ? 2
98410 3a 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 : 1;. }. r
98420 65 74 75 72 6e 20 6f 73 54 79 70 65 3d 3d 32 3b eturn osType==2;
98430 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 . }.#endif /* S
98440 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 20 2a QLITE_OS_WINCE *
98450 2f 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 /.#endif..#ifdef
98460 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 2f 2a SQLITE_DEBUG./*
98470 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
98480 6d 75 74 65 78 5f 68 65 6c 64 28 29 20 61 6e 64 mutex_held() and
98490 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e sqlite3_mutex_n
984a0 6f 74 68 65 6c 64 28 29 20 72 6f 75 74 69 6e 65 otheld() routine
984b0 20 61 72 65 0a 2a 2a 20 69 6e 74 65 6e 64 65 64 are.** intended
984c0 20 66 6f 72 20 75 73 65 20 6f 6e 6c 79 20 69 6e for use only in
984d0 73 69 64 65 20 61 73 73 65 72 74 28 29 20 73 74 side assert() st
984e0 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 73 74 61 atements..*/.sta
984f0 74 69 63 20 69 6e 74 20 77 69 6e 4d 75 74 65 78 tic int winMutex
98500 48 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 Held(sqlite3_mut
98510 65 78 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e ex *p){. return
98520 20 70 2d 3e 6e 52 65 66 21 3d 30 20 26 26 20 70 p->nRef!=0 && p
98530 2d 3e 6f 77 6e 65 72 3d 3d 47 65 74 43 75 72 72 ->owner==GetCurr
98540 65 6e 74 54 68 72 65 61 64 49 64 28 29 3b 0a 7d entThreadId();.}
98550 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 4d .static int winM
98560 75 74 65 78 4e 6f 74 68 65 6c 64 28 73 71 6c 69 utexNotheld(sqli
98570 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 te3_mutex *p){.
98580 20 72 65 74 75 72 6e 20 70 2d 3e 6e 52 65 66 3d return p->nRef=
98590 3d 30 20 7c 7c 20 70 2d 3e 6f 77 6e 65 72 21 3d =0 || p->owner!=
985a0 47 65 74 43 75 72 72 65 6e 74 54 68 72 65 61 64 GetCurrentThread
985b0 49 64 28 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a Id();.}.#endif..
985c0 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a ./*.** Initializ
985d0 65 20 61 6e 64 20 64 65 69 6e 69 74 69 61 6c 69 e and deinitiali
985e0 7a 65 20 74 68 65 20 6d 75 74 65 78 20 73 75 62 ze the mutex sub
985f0 73 79 73 74 65 6d 2e 0a 2a 2f 0a 73 74 61 74 69 system..*/.stati
98600 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 c sqlite3_mutex
98610 77 69 6e 4d 75 74 65 78 5f 73 74 61 74 69 63 4d winMutex_staticM
98620 75 74 65 78 65 73 5b 36 5d 3b 0a 73 74 61 74 69 utexes[6];.stati
98630 63 20 69 6e 74 20 77 69 6e 4d 75 74 65 78 5f 69 c int winMutex_i
98640 73 49 6e 69 74 20 3d 20 30 3b 0a 2f 2a 20 41 73 sInit = 0;./* As
98650 20 77 69 6e 4d 75 74 65 78 49 6e 69 74 28 29 20 winMutexInit()
98660 61 6e 64 20 77 69 6e 4d 75 74 65 78 45 6e 64 28 and winMutexEnd(
98670 29 20 61 72 65 20 63 61 6c 6c 65 64 20 61 73 20 ) are called as
98680 70 61 72 74 0a 2a 2a 20 6f 66 20 74 68 65 20 73 part.** of the s
98690 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a qlite3_initializ
986a0 65 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 73 68 e and sqlite3_sh
986b0 75 74 64 6f 77 6e 28 29 0a 2a 2a 20 70 72 6f 63 utdown().** proc
986c0 65 73 73 69 6e 67 2c 20 74 68 65 20 22 69 6e 74 essing, the "int
986d0 65 72 6c 6f 63 6b 65 64 22 20 6d 61 67 69 63 20 erlocked" magic
986e0 69 73 20 70 72 6f 62 61 62 6c 79 20 6e 6f 74 0a is probably not.
986f0 2a 2a 20 73 74 72 69 63 74 6c 79 20 6e 65 63 65 ** strictly nece
98700 73 73 61 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 ssary..*/.static
98710 20 6c 6f 6e 67 20 77 69 6e 4d 75 74 65 78 5f 6c long winMutex_l
98720 6f 63 6b 20 3d 20 30 3b 0a 0a 73 74 61 74 69 63 ock = 0;..static
98730 20 69 6e 74 20 77 69 6e 4d 75 74 65 78 49 6e 69 int winMutexIni
98740 74 28 76 6f 69 64 29 7b 20 0a 20 20 2f 2a 20 54 t(void){ . /* T
98750 68 65 20 66 69 72 73 74 20 74 6f 20 69 6e 63 72 he first to incr
98760 65 6d 65 6e 74 20 74 6f 20 31 20 64 6f 65 73 20 ement to 1 does
98770 61 63 74 75 61 6c 20 69 6e 69 74 69 61 6c 69 7a actual initializ
98780 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 66 28 20 49 ation */. if( I
98790 6e 74 65 72 6c 6f 63 6b 65 64 43 6f 6d 70 61 72 nterlockedCompar
987a0 65 45 78 63 68 61 6e 67 65 28 26 77 69 6e 4d 75 eExchange(&winMu
987b0 74 65 78 5f 6c 6f 63 6b 2c 20 31 2c 20 30 29 3d tex_lock, 1, 0)=
987c0 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b =0 ){. int i;
987d0 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c . for(i=0; i<
987e0 41 72 72 61 79 53 69 7a 65 28 77 69 6e 4d 75 74 ArraySize(winMut
987f0 65 78 5f 73 74 61 74 69 63 4d 75 74 65 78 65 73 ex_staticMutexes
98800 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 49 ); i++){. I
98810 6e 69 74 69 61 6c 69 7a 65 43 72 69 74 69 63 61 nitializeCritica
98820 6c 53 65 63 74 69 6f 6e 28 26 77 69 6e 4d 75 74 lSection(&winMut
98830 65 78 5f 73 74 61 74 69 63 4d 75 74 65 78 65 73 ex_staticMutexes
98840 5b 69 5d 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 [i].mutex);.
98850 7d 0a 20 20 20 20 77 69 6e 4d 75 74 65 78 5f 69 }. winMutex_i
98860 73 49 6e 69 74 20 3d 20 31 3b 0a 20 20 7d 65 6c sInit = 1;. }el
98870 73 65 7b 0a 20 20 20 20 2f 2a 20 53 6f 6d 65 6f se{. /* Someo
98880 6e 65 20 65 6c 73 65 20 69 73 20 69 6e 20 74 68 ne else is in th
98890 65 20 70 72 6f 63 65 73 73 20 6f 66 20 69 6e 69 e process of ini
988a0 74 69 6e 67 20 74 68 65 20 73 74 61 74 69 63 20 ting the static
988b0 6d 75 74 65 78 65 73 20 2a 2f 0a 20 20 20 20 77 mutexes */. w
988c0 68 69 6c 65 28 20 21 77 69 6e 4d 75 74 65 78 5f hile( !winMutex_
988d0 69 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 20 20 isInit ){.
988e0 53 6c 65 65 70 28 31 29 3b 0a 20 20 20 20 7d 0a Sleep(1);. }.
988f0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c }. return SQL
98900 49 54 45 5f 4f 4b 3b 20 0a 7d 0a 0a 73 74 61 74 ITE_OK; .}..stat
98910 69 63 20 69 6e 74 20 77 69 6e 4d 75 74 65 78 45 ic int winMutexE
98920 6e 64 28 76 6f 69 64 29 7b 20 0a 20 20 2f 2a 20 nd(void){ . /*
98930 54 68 65 20 66 69 72 73 74 20 74 6f 20 64 65 63 The first to dec
98940 72 65 6d 65 6e 74 20 74 6f 20 30 20 64 6f 65 73 rement to 0 does
98950 20 61 63 74 75 61 6c 20 73 68 75 74 64 6f 77 6e actual shutdown
98960 20 0a 20 20 2a 2a 20 28 77 68 69 63 68 20 73 68 . ** (which sh
98970 6f 75 6c 64 20 62 65 20 74 68 65 20 6c 61 73 74 ould be the last
98980 20 74 6f 20 73 68 75 74 64 6f 77 6e 2e 29 20 2a to shutdown.) *
98990 2f 0a 20 20 69 66 28 20 49 6e 74 65 72 6c 6f 63 /. if( Interloc
989a0 6b 65 64 43 6f 6d 70 61 72 65 45 78 63 68 61 6e kedCompareExchan
989b0 67 65 28 26 77 69 6e 4d 75 74 65 78 5f 6c 6f 63 ge(&winMutex_loc
989c0 6b 2c 20 30 2c 20 31 29 3d 3d 31 20 29 7b 0a 20 k, 0, 1)==1 ){.
989d0 20 20 20 69 66 28 20 77 69 6e 4d 75 74 65 78 5f if( winMutex_
989e0 69 73 49 6e 69 74 3d 3d 31 20 29 7b 0a 20 20 20 isInit==1 ){.
989f0 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 int i;.
98a00 66 6f 72 28 69 3d 30 3b 20 69 3c 41 72 72 61 79 for(i=0; i<Array
98a10 53 69 7a 65 28 77 69 6e 4d 75 74 65 78 5f 73 74 Size(winMutex_st
98a20 61 74 69 63 4d 75 74 65 78 65 73 29 3b 20 69 2b aticMutexes); i+
98a30 2b 29 7b 0a 20 20 20 20 20 20 20 20 44 65 6c 65 +){. Dele
98a40 74 65 43 72 69 74 69 63 61 6c 53 65 63 74 69 6f teCriticalSectio
98a50 6e 28 26 77 69 6e 4d 75 74 65 78 5f 73 74 61 74 n(&winMutex_stat
98a60 69 63 4d 75 74 65 78 65 73 5b 69 5d 2e 6d 75 74 icMutexes[i].mut
98a70 65 78 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 ex);. }.
98a80 20 20 20 77 69 6e 4d 75 74 65 78 5f 69 73 49 6e winMutex_isIn
98a90 69 74 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 it = 0;. }.
98aa0 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 }. return SQLIT
98ab0 45 5f 4f 4b 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 E_OK; .}../*.**
98ac0 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 The sqlite3_mute
98ad0 78 5f 61 6c 6c 6f 63 28 29 20 72 6f 75 74 69 6e x_alloc() routin
98ae0 65 20 61 6c 6c 6f 63 61 74 65 73 20 61 20 6e 65 e allocates a ne
98af0 77 0a 2a 2a 20 6d 75 74 65 78 20 61 6e 64 20 72 w.** mutex and r
98b00 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 eturns a pointer
98b10 20 74 6f 20 69 74 2e 20 20 49 66 20 69 74 20 72 to it. If it r
98b20 65 74 75 72 6e 73 20 4e 55 4c 4c 0a 2a 2a 20 74 eturns NULL.** t
98b30 68 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20 61 hat means that a
98b40 20 6d 75 74 65 78 20 63 6f 75 6c 64 20 6e 6f 74 mutex could not
98b50 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 be allocated.
98b60 53 51 4c 69 74 65 0a 2a 2a 20 77 69 6c 6c 20 75 SQLite.** will u
98b70 6e 77 69 6e 64 20 69 74 73 20 73 74 61 63 6b 20 nwind its stack
98b80 61 6e 64 20 72 65 74 75 72 6e 20 61 6e 20 65 72 and return an er
98b90 72 6f 72 2e 20 20 54 68 65 20 61 72 67 75 6d 65 ror. The argume
98ba0 6e 74 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 nt.** to sqlite3
98bb0 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 69 _mutex_alloc() i
98bc0 73 20 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 69 s one of these i
98bd0 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 73 nteger constants
98be0 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 :.**.** <ul>.**
98bf0 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 <li> SQLITE_MUT
98c00 45 58 5f 46 41 53 54 0a 2a 2a 20 3c 6c 69 3e 20 EX_FAST.** <li>
98c10 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 SQLITE_MUTEX_RE
98c20 43 55 52 53 49 56 45 0a 2a 2a 20 3c 6c 69 3e 20 CURSIVE.** <li>
98c30 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 SQLITE_MUTEX_ST
98c40 41 54 49 43 5f 4d 41 53 54 45 52 0a 2a 2a 20 3c ATIC_MASTER.** <
98c50 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 li> SQLITE_MUTE
98c60 58 5f 53 54 41 54 49 43 5f 4d 45 4d 0a 2a 2a 20 X_STATIC_MEM.**
98c70 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 <li> SQLITE_MUT
98c80 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d 32 0a 2a EX_STATIC_MEM2.*
98c90 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d * <li> SQLITE_M
98ca0 55 54 45 58 5f 53 54 41 54 49 43 5f 50 52 4e 47 UTEX_STATIC_PRNG
98cb0 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 .** <li> SQLITE
98cc0 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 _MUTEX_STATIC_LR
98cd0 55 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 U.** <li> SQLIT
98ce0 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c E_MUTEX_STATIC_L
98cf0 52 55 32 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a RU2.** </ul>.**.
98d00 2a 2a 20 54 68 65 20 66 69 72 73 74 20 74 77 6f ** The first two
98d10 20 63 6f 6e 73 74 61 6e 74 73 20 63 61 75 73 65 constants cause
98d20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 sqlite3_mutex_a
98d30 6c 6c 6f 63 28 29 20 74 6f 20 63 72 65 61 74 65 lloc() to create
98d40 0a 2a 2a 20 61 20 6e 65 77 20 6d 75 74 65 78 2e .** a new mutex.
98d50 20 20 54 68 65 20 6e 65 77 20 6d 75 74 65 78 20 The new mutex
98d60 69 73 20 72 65 63 75 72 73 69 76 65 20 77 68 65 is recursive whe
98d70 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 n SQLITE_MUTEX_R
98d80 45 43 55 52 53 49 56 45 0a 2a 2a 20 69 73 20 75 ECURSIVE.** is u
98d90 73 65 64 20 62 75 74 20 6e 6f 74 20 6e 65 63 65 sed but not nece
98da0 73 73 61 72 69 6c 79 20 73 6f 20 77 68 65 6e 20 ssarily so when
98db0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 SQLITE_MUTEX_FAS
98dc0 54 20 69 73 20 75 73 65 64 2e 0a 2a 2a 20 54 68 T is used..** Th
98dd0 65 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e e mutex implemen
98de0 74 61 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 tation does not
98df0 6e 65 65 64 20 74 6f 20 6d 61 6b 65 20 61 20 64 need to make a d
98e00 69 73 74 69 6e 63 74 69 6f 6e 0a 2a 2a 20 62 65 istinction.** be
98e10 74 77 65 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 tween SQLITE_MUT
98e20 45 58 5f 52 45 43 55 52 53 49 56 45 20 61 6e 64 EX_RECURSIVE and
98e30 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 SQLITE_MUTEX_FA
98e40 53 54 20 69 66 20 69 74 20 64 6f 65 73 0a 2a 2a ST if it does.**
98e50 20 6e 6f 74 20 77 61 6e 74 20 74 6f 2e 20 20 42 not want to. B
98e60 75 74 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6f ut SQLite will o
98e70 6e 6c 79 20 72 65 71 75 65 73 74 20 61 20 72 65 nly request a re
98e80 63 75 72 73 69 76 65 20 6d 75 74 65 78 20 69 6e cursive mutex in
98e90 0a 2a 2a 20 63 61 73 65 73 20 77 68 65 72 65 20 .** cases where
98ea0 69 74 20 72 65 61 6c 6c 79 20 6e 65 65 64 73 20 it really needs
98eb0 6f 6e 65 2e 20 20 49 66 20 61 20 66 61 73 74 65 one. If a faste
98ec0 72 20 6e 6f 6e 2d 72 65 63 75 72 73 69 76 65 20 r non-recursive
98ed0 6d 75 74 65 78 0a 2a 2a 20 69 6d 70 6c 65 6d 65 mutex.** impleme
98ee0 6e 74 61 74 69 6f 6e 20 69 73 20 61 76 61 69 6c ntation is avail
98ef0 61 62 6c 65 20 6f 6e 20 74 68 65 20 68 6f 73 74 able on the host
98f00 20 70 6c 61 74 66 6f 72 6d 2c 20 74 68 65 20 6d platform, the m
98f10 75 74 65 78 20 73 75 62 73 79 73 74 65 6d 0a 2a utex subsystem.*
98f20 2a 20 6d 69 67 68 74 20 72 65 74 75 72 6e 20 73 * might return s
98f30 75 63 68 20 61 20 6d 75 74 65 78 20 69 6e 20 72 uch a mutex in r
98f40 65 73 70 6f 6e 73 65 20 74 6f 20 53 51 4c 49 54 esponse to SQLIT
98f50 45 5f 4d 55 54 45 58 5f 46 41 53 54 2e 0a 2a 2a E_MUTEX_FAST..**
98f60 0a 2a 2a 20 54 68 65 20 6f 74 68 65 72 20 61 6c .** The other al
98f70 6c 6f 77 65 64 20 70 61 72 61 6d 65 74 65 72 73 lowed parameters
98f80 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 to sqlite3_mute
98f90 78 5f 61 6c 6c 6f 63 28 29 20 65 61 63 68 20 72 x_alloc() each r
98fa0 65 74 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 eturn.** a point
98fb0 65 72 20 74 6f 20 61 20 73 74 61 74 69 63 20 70 er to a static p
98fc0 72 65 65 78 69 73 74 69 6e 67 20 6d 75 74 65 78 reexisting mutex
98fd0 2e 20 20 53 69 78 20 73 74 61 74 69 63 20 6d 75 . Six static mu
98fe0 74 65 78 65 73 20 61 72 65 0a 2a 2a 20 75 73 65 texes are.** use
98ff0 64 20 62 79 20 74 68 65 20 63 75 72 72 65 6e 74 d by the current
99000 20 76 65 72 73 69 6f 6e 20 6f 66 20 53 51 4c 69 version of SQLi
99010 74 65 2e 20 20 46 75 74 75 72 65 20 76 65 72 73 te. Future vers
99020 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 0a 2a ions of SQLite.*
99030 2a 20 6d 61 79 20 61 64 64 20 61 64 64 69 74 69 * may add additi
99040 6f 6e 61 6c 20 73 74 61 74 69 63 20 6d 75 74 65 onal static mute
99050 78 65 73 2e 20 20 53 74 61 74 69 63 20 6d 75 74 xes. Static mut
99060 65 78 65 73 20 61 72 65 20 66 6f 72 20 69 6e 74 exes are for int
99070 65 72 6e 61 6c 0a 2a 2a 20 75 73 65 20 62 79 20 ernal.** use by
99080 53 51 4c 69 74 65 20 6f 6e 6c 79 2e 20 20 41 70 SQLite only. Ap
99090 70 6c 69 63 61 74 69 6f 6e 73 20 74 68 61 74 20 plications that
990a0 75 73 65 20 53 51 4c 69 74 65 20 6d 75 74 65 78 use SQLite mutex
990b0 65 73 20 73 68 6f 75 6c 64 0a 2a 2a 20 75 73 65 es should.** use
990c0 20 6f 6e 6c 79 20 74 68 65 20 64 79 6e 61 6d 69 only the dynami
990d0 63 20 6d 75 74 65 78 65 73 20 72 65 74 75 72 6e c mutexes return
990e0 65 64 20 62 79 20 53 51 4c 49 54 45 5f 4d 55 54 ed by SQLITE_MUT
990f0 45 58 5f 46 41 53 54 20 6f 72 0a 2a 2a 20 53 51 EX_FAST or.** SQ
99100 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 LITE_MUTEX_RECUR
99110 53 49 56 45 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 SIVE..**.** Note
99120 20 74 68 61 74 20 69 66 20 6f 6e 65 20 6f 66 20 that if one of
99130 74 68 65 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 the dynamic mute
99140 78 20 70 61 72 61 6d 65 74 65 72 73 20 28 53 51 x parameters (SQ
99150 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 0a LITE_MUTEX_FAST.
99160 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f 4d 55 54 ** or SQLITE_MUT
99170 45 58 5f 52 45 43 55 52 53 49 56 45 29 20 69 73 EX_RECURSIVE) is
99180 20 75 73 65 64 20 74 68 65 6e 20 73 71 6c 69 74 used then sqlit
99190 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 e3_mutex_alloc()
991a0 0a 2a 2a 20 72 65 74 75 72 6e 73 20 61 20 64 69 .** returns a di
991b0 66 66 65 72 65 6e 74 20 6d 75 74 65 78 20 6f 6e fferent mutex on
991c0 20 65 76 65 72 79 20 63 61 6c 6c 2e 20 20 42 75 every call. Bu
991d0 74 20 66 6f 72 20 74 68 65 20 73 74 61 74 69 63 t for the static
991e0 20 0a 2a 2a 20 6d 75 74 65 78 20 74 79 70 65 73 .** mutex types
991f0 2c 20 74 68 65 20 73 61 6d 65 20 6d 75 74 65 78 , the same mutex
99200 20 69 73 20 72 65 74 75 72 6e 65 64 20 6f 6e 20 is returned on
99210 65 76 65 72 79 20 63 61 6c 6c 20 74 68 61 74 20 every call that
99220 68 61 73 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20 has.** the same
99230 74 79 70 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a type number..*/.
99240 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 6d static sqlite3_m
99250 75 74 65 78 20 2a 77 69 6e 4d 75 74 65 78 41 6c utex *winMutexAl
99260 6c 6f 63 28 69 6e 74 20 69 54 79 70 65 29 7b 0a loc(int iType){.
99270 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 sqlite3_mutex
99280 2a 70 3b 0a 0a 20 20 73 77 69 74 63 68 28 20 69 *p;.. switch( i
99290 54 79 70 65 20 29 7b 0a 20 20 20 20 63 61 73 65 Type ){. case
992a0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 SQLITE_MUTEX_FA
992b0 53 54 3a 0a 20 20 20 20 63 61 73 65 20 53 51 4c ST:. case SQL
992c0 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 ITE_MUTEX_RECURS
992d0 49 56 45 3a 20 7b 0a 20 20 20 20 20 20 70 20 3d IVE: {. p =
992e0 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 sqlite3MallocZe
992f0 72 6f 28 20 73 69 7a 65 6f 66 28 2a 70 29 20 29 ro( sizeof(*p) )
99300 3b 0a 20 20 20 20 20 20 69 66 28 20 70 20 29 7b ;. if( p ){
99310 20 20 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 64 . p->id
99320 20 3d 20 69 54 79 70 65 3b 0a 20 20 20 20 20 20 = iType;.
99330 20 20 49 6e 69 74 69 61 6c 69 7a 65 43 72 69 74 InitializeCrit
99340 69 63 61 6c 53 65 63 74 69 6f 6e 28 26 70 2d 3e icalSection(&p->
99350 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 7d 0a mutex);. }.
99360 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
99370 20 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 }. default:
99380 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 {. assert(
99390 77 69 6e 4d 75 74 65 78 5f 69 73 49 6e 69 74 3d winMutex_isInit=
993a0 3d 31 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 =1 );. asse
993b0 72 74 28 20 69 54 79 70 65 2d 32 20 3e 3d 20 30 rt( iType-2 >= 0
993c0 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 );. assert
993d0 28 20 69 54 79 70 65 2d 32 20 3c 20 41 72 72 61 ( iType-2 < Arra
993e0 79 53 69 7a 65 28 77 69 6e 4d 75 74 65 78 5f 73 ySize(winMutex_s
993f0 74 61 74 69 63 4d 75 74 65 78 65 73 29 20 29 3b taticMutexes) );
99400 0a 20 20 20 20 20 20 70 20 3d 20 26 77 69 6e 4d . p = &winM
99410 75 74 65 78 5f 73 74 61 74 69 63 4d 75 74 65 78 utex_staticMutex
99420 65 73 5b 69 54 79 70 65 2d 32 5d 3b 0a 20 20 20 es[iType-2];.
99430 20 20 20 70 2d 3e 69 64 20 3d 20 69 54 79 70 65 p->id = iType
99440 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
99450 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
99460 6e 20 70 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 n p;.}.../*.** T
99470 68 69 73 20 72 6f 75 74 69 6e 65 20 64 65 61 6c his routine deal
99480 6c 6f 63 61 74 65 73 20 61 20 70 72 65 76 69 6f locates a previo
99490 75 73 6c 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 usly.** allocate
994a0 64 20 6d 75 74 65 78 2e 20 20 53 51 4c 69 74 65 d mutex. SQLite
994b0 20 69 73 20 63 61 72 65 66 75 6c 20 74 6f 20 64 is careful to d
994c0 65 61 6c 6c 6f 63 61 74 65 20 65 76 65 72 79 0a eallocate every.
994d0 2a 2a 20 6d 75 74 65 78 20 74 68 61 74 20 69 74 ** mutex that it
994e0 20 61 6c 6c 6f 63 61 74 65 73 2e 0a 2a 2f 0a 73 allocates..*/.s
994f0 74 61 74 69 63 20 76 6f 69 64 20 77 69 6e 4d 75 tatic void winMu
99500 74 65 78 46 72 65 65 28 73 71 6c 69 74 65 33 5f texFree(sqlite3_
99510 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 73 73 mutex *p){. ass
99520 65 72 74 28 20 70 20 29 3b 0a 20 20 61 73 73 65 ert( p );. asse
99530 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 29 rt( p->nRef==0 )
99540 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 ;. assert( p->i
99550 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f d==SQLITE_MUTEX_
99560 46 41 53 54 20 7c 7c 20 70 2d 3e 69 64 3d 3d 53 FAST || p->id==S
99570 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 QLITE_MUTEX_RECU
99580 52 53 49 56 45 20 29 3b 0a 20 20 44 65 6c 65 74 RSIVE );. Delet
99590 65 43 72 69 74 69 63 61 6c 53 65 63 74 69 6f 6e eCriticalSection
995a0 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 73 (&p->mutex);. s
995b0 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a qlite3_free(p);.
995c0 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c }../*.** The sql
995d0 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
995e0 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d () and sqlite3_m
995f0 75 74 65 78 5f 74 72 79 28 29 20 72 6f 75 74 69 utex_try() routi
99600 6e 65 73 20 61 74 74 65 6d 70 74 0a 2a 2a 20 74 nes attempt.** t
99610 6f 20 65 6e 74 65 72 20 61 20 6d 75 74 65 78 2e o enter a mutex.
99620 20 20 49 66 20 61 6e 6f 74 68 65 72 20 74 68 72 If another thr
99630 65 61 64 20 69 73 20 61 6c 72 65 61 64 79 20 77 ead is already w
99640 69 74 68 69 6e 20 74 68 65 20 6d 75 74 65 78 2c ithin the mutex,
99650 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 .** sqlite3_mute
99660 78 5f 65 6e 74 65 72 28 29 20 77 69 6c 6c 20 62 x_enter() will b
99670 6c 6f 63 6b 20 61 6e 64 20 73 71 6c 69 74 65 33 lock and sqlite3
99680 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 77 69 6c _mutex_try() wil
99690 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 l return.** SQLI
996a0 54 45 5f 42 55 53 59 2e 20 20 54 68 65 20 73 71 TE_BUSY. The sq
996b0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 lite3_mutex_try(
996c0 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 ) interface retu
996d0 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a rns SQLITE_OK.**
996e0 20 75 70 6f 6e 20 73 75 63 63 65 73 73 66 75 6c upon successful
996f0 20 65 6e 74 72 79 2e 20 20 4d 75 74 65 78 65 73 entry. Mutexes
99700 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 20 53 created using S
99710 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 QLITE_MUTEX_RECU
99720 52 53 49 56 45 20 63 61 6e 0a 2a 2a 20 62 65 20 RSIVE can.** be
99730 65 6e 74 65 72 65 64 20 6d 75 6c 74 69 70 6c 65 entered multiple
99740 20 74 69 6d 65 73 20 62 79 20 74 68 65 20 73 61 times by the sa
99750 6d 65 20 74 68 72 65 61 64 2e 20 20 49 6e 20 73 me thread. In s
99760 75 63 68 20 63 61 73 65 73 20 74 68 65 2c 0a 2a uch cases the,.*
99770 2a 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 * mutex must be
99780 65 78 69 74 65 64 20 61 6e 20 65 71 75 61 6c 20 exited an equal
99790 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 number of times
997a0 62 65 66 6f 72 65 20 61 6e 6f 74 68 65 72 20 74 before another t
997b0 68 72 65 61 64 0a 2a 2a 20 63 61 6e 20 65 6e 74 hread.** can ent
997c0 65 72 2e 20 20 49 66 20 74 68 65 20 73 61 6d 65 er. If the same
997d0 20 74 68 72 65 61 64 20 74 72 69 65 73 20 74 6f thread tries to
997e0 20 65 6e 74 65 72 20 61 6e 79 20 6f 74 68 65 72 enter any other
997f0 20 6b 69 6e 64 20 6f 66 20 6d 75 74 65 78 0a 2a kind of mutex.*
99800 2a 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65 * more than once
99810 2c 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 69 , the behavior i
99820 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a s undefined..*/.
99830 73 74 61 74 69 63 20 76 6f 69 64 20 77 69 6e 4d static void winM
99840 75 74 65 78 45 6e 74 65 72 28 73 71 6c 69 74 65 utexEnter(sqlite
99850 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 3_mutex *p){. a
99860 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 ssert( p->id==SQ
99870 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 LITE_MUTEX_RECUR
99880 53 49 56 45 20 7c 7c 20 77 69 6e 4d 75 74 65 78 SIVE || winMutex
99890 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a 20 20 Notheld(p) );.
998a0 45 6e 74 65 72 43 72 69 74 69 63 61 6c 53 65 63 EnterCriticalSec
998b0 74 69 6f 6e 28 26 70 2d 3e 6d 75 74 65 78 29 3b tion(&p->mutex);
998c0 0a 20 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 47 65 . p->owner = Ge
998d0 74 43 75 72 72 65 6e 74 54 68 72 65 61 64 49 64 tCurrentThreadId
998e0 28 29 3b 20 0a 20 20 70 2d 3e 6e 52 65 66 2b 2b (); . p->nRef++
998f0 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 77 ;.}.static int w
99900 69 6e 4d 75 74 65 78 54 72 79 28 73 71 6c 69 74 inMutexTry(sqlit
99910 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 e3_mutex *p){.
99920 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
99930 42 55 53 59 3b 0a 20 20 61 73 73 65 72 74 28 20 BUSY;. assert(
99940 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 p->id==SQLITE_MU
99950 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 7c 7c TEX_RECURSIVE ||
99960 20 77 69 6e 4d 75 74 65 78 4e 6f 74 68 65 6c 64 winMutexNotheld
99970 28 70 29 20 29 3b 0a 20 20 2f 2a 0a 20 20 2a 2a (p) );. /*. **
99980 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 The sqlite3_mut
99990 65 78 5f 74 72 79 28 29 20 72 6f 75 74 69 6e 65 ex_try() routine
999a0 20 69 73 20 76 65 72 79 20 72 61 72 65 6c 79 20 is very rarely
999b0 75 73 65 64 2c 20 61 6e 64 20 77 68 65 6e 20 69 used, and when i
999c0 74 0a 20 20 2a 2a 20 69 73 20 75 73 65 64 20 69 t. ** is used i
999d0 74 20 69 73 20 6d 65 72 65 6c 79 20 61 6e 20 6f t is merely an o
999e0 70 74 69 6d 69 7a 61 74 69 6f 6e 2e 20 20 53 6f ptimization. So
999f0 20 69 74 20 69 73 20 4f 4b 20 66 6f 72 20 69 74 it is OK for it
99a00 20 74 6f 20 61 6c 77 61 79 73 0a 20 20 2a 2a 20 to always. **
99a10 66 61 69 6c 2e 20 20 0a 20 20 2a 2a 0a 20 20 2a fail. . **. *
99a20 2a 20 54 68 65 20 54 72 79 45 6e 74 65 72 43 72 * The TryEnterCr
99a30 69 74 69 63 61 6c 53 65 63 74 69 6f 6e 28 29 20 iticalSection()
99a40 69 6e 74 65 72 66 61 63 65 20 69 73 20 6f 6e 6c interface is onl
99a50 79 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 57 y available on W
99a60 69 6e 4e 54 2e 0a 20 20 2a 2a 20 41 6e 64 20 73 inNT.. ** And s
99a70 6f 6d 65 20 77 69 6e 64 6f 77 73 20 63 6f 6d 70 ome windows comp
99a80 69 6c 65 72 73 20 63 6f 6d 70 6c 61 69 6e 20 69 ilers complain i
99a90 66 20 79 6f 75 20 74 72 79 20 74 6f 20 75 73 65 f you try to use
99aa0 20 69 74 20 77 69 74 68 6f 75 74 0a 20 20 2a 2a it without. **
99ab0 20 66 69 72 73 74 20 64 6f 69 6e 67 20 73 6f 6d first doing som
99ac0 65 20 23 64 65 66 69 6e 65 73 20 74 68 61 74 20 e #defines that
99ad0 70 72 65 76 65 6e 74 20 53 51 4c 69 74 65 20 66 prevent SQLite f
99ae0 72 6f 6d 20 62 75 69 6c 64 69 6e 67 20 6f 6e 20 rom building on
99af0 57 69 6e 39 38 2e 0a 20 20 2a 2a 20 46 6f 72 20 Win98.. ** For
99b00 74 68 61 74 20 72 65 61 73 6f 6e 2c 20 77 65 20 that reason, we
99b10 77 69 6c 6c 20 6f 6d 69 74 20 74 68 69 73 20 6f will omit this o
99b20 70 74 69 6d 69 7a 61 74 69 6f 6e 20 66 6f 72 20 ptimization for
99b30 6e 6f 77 2e 20 20 53 65 65 0a 20 20 2a 2a 20 74 now. See. ** t
99b40 69 63 6b 65 74 20 23 32 36 38 35 2e 0a 20 20 2a icket #2685.. *
99b50 2f 0a 23 69 66 20 30 0a 20 20 69 66 28 20 6d 75 /.#if 0. if( mu
99b60 74 65 78 49 73 4e 54 28 29 20 26 26 20 54 72 79 texIsNT() && Try
99b70 45 6e 74 65 72 43 72 69 74 69 63 61 6c 53 65 63 EnterCriticalSec
99b80 74 69 6f 6e 28 26 70 2d 3e 6d 75 74 65 78 29 20 tion(&p->mutex)
99b90 29 7b 0a 20 20 20 20 70 2d 3e 6f 77 6e 65 72 20 ){. p->owner
99ba0 3d 20 47 65 74 43 75 72 72 65 6e 74 54 68 72 65 = GetCurrentThre
99bb0 61 64 49 64 28 29 3b 0a 20 20 20 20 70 2d 3e 6e adId();. p->n
99bc0 52 65 66 2b 2b 3b 0a 20 20 20 20 72 63 20 3d 20 Ref++;. rc =
99bd0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 23 SQLITE_OK;. }.#
99be0 65 6c 73 65 0a 20 20 55 4e 55 53 45 44 5f 50 41 else. UNUSED_PA
99bf0 52 41 4d 45 54 45 52 28 70 29 3b 0a 23 65 6e 64 RAMETER(p);.#end
99c00 69 66 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a if. return rc;.
99c10 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c }../*.** The sql
99c20 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 ite3_mutex_leave
99c30 28 29 20 72 6f 75 74 69 6e 65 20 65 78 69 74 73 () routine exits
99c40 20 61 20 6d 75 74 65 78 20 74 68 61 74 20 77 61 a mutex that wa
99c50 73 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 s.** previously
99c60 65 6e 74 65 72 65 64 20 62 79 20 74 68 65 20 73 entered by the s
99c70 61 6d 65 20 74 68 72 65 61 64 2e 20 20 54 68 65 ame thread. The
99c80 20 62 65 68 61 76 69 6f 72 0a 2a 2a 20 69 73 20 behavior.** is
99c90 75 6e 64 65 66 69 6e 65 64 20 69 66 20 74 68 65 undefined if the
99ca0 20 6d 75 74 65 78 20 69 73 20 6e 6f 74 20 63 75 mutex is not cu
99cb0 72 72 65 6e 74 6c 79 20 65 6e 74 65 72 65 64 20 rrently entered
99cc0 6f 72 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 75 72 or.** is not cur
99cd0 72 65 6e 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64 rently allocated
99ce0 2e 20 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e . SQLite will n
99cf0 65 76 65 72 20 64 6f 20 65 69 74 68 65 72 2e 0a ever do either..
99d00 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77 */.static void w
99d10 69 6e 4d 75 74 65 78 4c 65 61 76 65 28 73 71 6c inMutexLeave(sql
99d20 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a ite3_mutex *p){.
99d30 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 assert( p->nRe
99d40 66 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 f>0 );. assert(
99d50 20 70 2d 3e 6f 77 6e 65 72 3d 3d 47 65 74 43 75 p->owner==GetCu
99d60 72 72 65 6e 74 54 68 72 65 61 64 49 64 28 29 20 rrentThreadId()
99d70 29 3b 0a 20 20 70 2d 3e 6e 52 65 66 2d 2d 3b 0a );. p->nRef--;.
99d80 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 assert( p->nRe
99d90 66 3d 3d 30 20 7c 7c 20 70 2d 3e 69 64 3d 3d 53 f==0 || p->id==S
99da0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 QLITE_MUTEX_RECU
99db0 52 53 49 56 45 20 29 3b 0a 20 20 4c 65 61 76 65 RSIVE );. Leave
99dc0 43 72 69 74 69 63 61 6c 53 65 63 74 69 6f 6e 28 CriticalSection(
99dd0 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 7d 0a 0a 53 &p->mutex);.}..S
99de0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 QLITE_PRIVATE sq
99df0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 lite3_mutex_meth
99e00 6f 64 73 20 2a 73 71 6c 69 74 65 33 44 65 66 61 ods *sqlite3Defa
99e10 75 6c 74 4d 75 74 65 78 28 76 6f 69 64 29 7b 0a ultMutex(void){.
99e20 20 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 static sqlite3
99e30 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 73 _mutex_methods s
99e40 4d 75 74 65 78 20 3d 20 7b 0a 20 20 20 20 77 69 Mutex = {. wi
99e50 6e 4d 75 74 65 78 49 6e 69 74 2c 0a 20 20 20 20 nMutexInit,.
99e60 77 69 6e 4d 75 74 65 78 45 6e 64 2c 0a 20 20 20 winMutexEnd,.
99e70 20 77 69 6e 4d 75 74 65 78 41 6c 6c 6f 63 2c 0a winMutexAlloc,.
99e80 20 20 20 20 77 69 6e 4d 75 74 65 78 46 72 65 65 winMutexFree
99e90 2c 0a 20 20 20 20 77 69 6e 4d 75 74 65 78 45 6e ,. winMutexEn
99ea0 74 65 72 2c 0a 20 20 20 20 77 69 6e 4d 75 74 65 ter,. winMute
99eb0 78 54 72 79 2c 0a 20 20 20 20 77 69 6e 4d 75 74 xTry,. winMut
99ec0 65 78 4c 65 61 76 65 2c 0a 23 69 66 64 65 66 20 exLeave,.#ifdef
99ed0 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20 SQLITE_DEBUG.
99ee0 20 77 69 6e 4d 75 74 65 78 48 65 6c 64 2c 0a 20 winMutexHeld,.
99ef0 20 20 20 77 69 6e 4d 75 74 65 78 4e 6f 74 68 65 winMutexNothe
99f00 6c 64 0a 23 65 6c 73 65 0a 20 20 20 20 30 2c 0a ld.#else. 0,.
99f10 20 20 20 20 30 0a 23 65 6e 64 69 66 0a 20 20 7d 0.#endif. }
99f20 3b 0a 0a 20 20 72 65 74 75 72 6e 20 26 73 4d 75 ;.. return &sMu
99f30 74 65 78 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a tex;.}.#endif /*
99f40 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 57 33 SQLITE_MUTEX_W3
99f50 32 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2 */../*********
99f60 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 75 74 ***** End of mut
99f70 65 78 5f 77 33 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a ex_w32.c *******
99f80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
99f90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
99fa0 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
99fb0 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
99fc0 20 6d 61 6c 6c 6f 63 2e 63 20 2a 2a 2a 2a 2a 2a malloc.c ******
99fd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
99fe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
99ff0 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 ****/./*.** 2001
9a000 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a September 15.**
9a010 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 .** The author d
9a020 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 isclaims copyrig
9a030 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 ht to this sourc
9a040 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 e code. In plac
9a050 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 e of.** a legal
9a060 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 notice, here is
9a070 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a a blessing:.**.*
9a080 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 * May you do
9a090 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 good and not evi
9a0a0 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 l..** May you
9a0b0 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 find forgivenes
9a0c0 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 s for yourself a
9a0d0 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 nd forgive other
9a0e0 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 s..** May you
9a0f0 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e share freely, n
9a100 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 ever taking more
9a110 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a than you give..
9a120 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
9a130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9a140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9a150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9a160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a ************.**.
9a170 2a 2a 20 4d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 ** Memory alloca
9a180 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 73 20 75 tion functions u
9a190 73 65 64 20 74 68 72 6f 75 67 68 6f 75 74 20 73 sed throughout s
9a1a0 71 6c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 qlite..**.** $Id
9a1b0 3a 20 6d 61 6c 6c 6f 63 2e 63 2c 76 20 31 2e 36 : malloc.c,v 1.6
9a1c0 36 20 32 30 30 39 2f 30 37 2f 31 37 20 31 31 3a 6 2009/07/17 11:
9a1d0 34 34 3a 30 37 20 64 72 68 20 45 78 70 20 24 0a 44:07 drh Exp $.
9a1e0 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 */../*.** This r
9a1f0 6f 75 74 69 6e 65 20 72 75 6e 73 20 77 68 65 6e outine runs when
9a200 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f the memory allo
9a210 63 61 74 6f 72 20 73 65 65 73 20 74 68 61 74 20 cator sees that
9a220 74 68 65 0a 2a 2a 20 74 6f 74 61 6c 20 6d 65 6d the.** total mem
9a230 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 ory allocation i
9a240 73 20 61 62 6f 75 74 20 74 6f 20 65 78 63 65 65 s about to excee
9a250 64 20 74 68 65 20 73 6f 66 74 20 68 65 61 70 0a d the soft heap.
9a260 2a 2a 20 6c 69 6d 69 74 2e 0a 2a 2f 0a 73 74 61 ** limit..*/.sta
9a270 74 69 63 20 76 6f 69 64 20 73 6f 66 74 48 65 61 tic void softHea
9a280 70 4c 69 6d 69 74 45 6e 66 6f 72 63 65 72 28 0a pLimitEnforcer(.
9a290 20 20 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 2c void *NotUsed,
9a2a0 20 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 . sqlite3_int6
9a2b0 34 20 4e 6f 74 55 73 65 64 32 2c 0a 20 20 69 6e 4 NotUsed2,. in
9a2c0 74 20 61 6c 6c 6f 63 53 69 7a 65 0a 29 7b 0a 20 t allocSize.){.
9a2d0 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
9a2e0 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 R2(NotUsed, NotU
9a2f0 73 65 64 32 29 3b 0a 20 20 73 71 6c 69 74 65 33 sed2);. sqlite3
9a300 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 _release_memory(
9a310 61 6c 6c 6f 63 53 69 7a 65 29 3b 0a 7d 0a 0a 2f allocSize);.}../
9a320 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 73 6f 66 *.** Set the sof
9a330 74 20 68 65 61 70 2d 73 69 7a 65 20 6c 69 6d 69 t heap-size limi
9a340 74 20 66 6f 72 20 74 68 65 20 6c 69 62 72 61 72 t for the librar
9a350 79 2e 20 50 61 73 73 69 6e 67 20 61 20 7a 65 72 y. Passing a zer
9a360 6f 20 6f 72 20 0a 2a 2a 20 6e 65 67 61 74 69 76 o or .** negativ
9a370 65 20 76 61 6c 75 65 20 69 6e 64 69 63 61 74 65 e value indicate
9a380 73 20 6e 6f 20 6c 69 6d 69 74 2e 0a 2a 2f 0a 53 s no limit..*/.S
9a390 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 QLITE_API void s
9a3a0 71 6c 69 74 65 33 5f 73 6f 66 74 5f 68 65 61 70 qlite3_soft_heap
9a3b0 5f 6c 69 6d 69 74 28 69 6e 74 20 6e 29 7b 0a 20 _limit(int n){.
9a3c0 20 73 71 6c 69 74 65 33 5f 75 69 6e 74 36 34 20 sqlite3_uint64
9a3d0 69 4c 69 6d 69 74 3b 0a 20 20 69 6e 74 20 6f 76 iLimit;. int ov
9a3e0 65 72 61 67 65 3b 0a 20 20 69 66 28 20 6e 3c 30 erage;. if( n<0
9a3f0 20 29 7b 0a 20 20 20 20 69 4c 69 6d 69 74 20 3d ){. iLimit =
9a400 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 0;. }else{.
9a410 20 69 4c 69 6d 69 74 20 3d 20 6e 3b 0a 20 20 7d iLimit = n;. }
9a420 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
9a430 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 OMIT_AUTOINIT.
9a440 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 sqlite3_initiali
9a450 7a 65 28 29 3b 0a 23 65 6e 64 69 66 0a 20 20 69 ze();.#endif. i
9a460 66 28 20 69 4c 69 6d 69 74 3e 30 20 29 7b 0a 20 f( iLimit>0 ){.
9a470 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 6f 72 79 sqlite3Memory
9a480 41 6c 61 72 6d 28 73 6f 66 74 48 65 61 70 4c 69 Alarm(softHeapLi
9a490 6d 69 74 45 6e 66 6f 72 63 65 72 2c 20 30 2c 20 mitEnforcer, 0,
9a4a0 69 4c 69 6d 69 74 29 3b 0a 20 20 7d 65 6c 73 65 iLimit);. }else
9a4b0 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d {. sqlite3Mem
9a4c0 6f 72 79 41 6c 61 72 6d 28 30 2c 20 30 2c 20 30 oryAlarm(0, 0, 0
9a4d0 29 3b 0a 20 20 7d 0a 20 20 6f 76 65 72 61 67 65 );. }. overage
9a4e0 20 3d 20 28 69 6e 74 29 28 73 71 6c 69 74 65 33 = (int)(sqlite3
9a4f0 5f 6d 65 6d 6f 72 79 5f 75 73 65 64 28 29 20 2d _memory_used() -
9a500 20 28 69 36 34 29 6e 29 3b 0a 20 20 69 66 28 20 (i64)n);. if(
9a510 6f 76 65 72 61 67 65 3e 30 20 29 7b 0a 20 20 20 overage>0 ){.
9a520 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 sqlite3_release
9a530 5f 6d 65 6d 6f 72 79 28 6f 76 65 72 61 67 65 29 _memory(overage)
9a540 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 ;. }.}../*.** A
9a550 74 74 65 6d 70 74 20 74 6f 20 72 65 6c 65 61 73 ttempt to releas
9a560 65 20 75 70 20 74 6f 20 6e 20 62 79 74 65 73 20 e up to n bytes
9a570 6f 66 20 6e 6f 6e 2d 65 73 73 65 6e 74 69 61 6c of non-essential
9a580 20 6d 65 6d 6f 72 79 20 63 75 72 72 65 6e 74 6c memory currentl
9a590 79 0a 2a 2a 20 68 65 6c 64 20 62 79 20 53 51 4c y.** held by SQL
9a5a0 69 74 65 2e 20 41 6e 20 65 78 61 6d 70 6c 65 20 ite. An example
9a5b0 6f 66 20 6e 6f 6e 2d 65 73 73 65 6e 74 69 61 6c of non-essential
9a5c0 20 6d 65 6d 6f 72 79 20 69 73 20 6d 65 6d 6f 72 memory is memor
9a5d0 79 20 75 73 65 64 20 74 6f 0a 2a 2a 20 63 61 63 y used to.** cac
9a5e0 68 65 20 64 61 74 61 62 61 73 65 20 70 61 67 65 he database page
9a5f0 73 20 74 68 61 74 20 61 72 65 20 6e 6f 74 20 63 s that are not c
9a600 75 72 72 65 6e 74 6c 79 20 69 6e 20 75 73 65 2e urrently in use.
9a610 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
9a620 6e 74 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 nt sqlite3_relea
9a630 73 65 5f 6d 65 6d 6f 72 79 28 69 6e 74 20 6e 29 se_memory(int n)
9a640 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f {.#ifdef SQLITE_
9a650 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 ENABLE_MEMORY_MA
9a660 4e 41 47 45 4d 45 4e 54 0a 20 20 69 6e 74 20 6e NAGEMENT. int n
9a670 52 65 74 20 3d 20 30 3b 0a 23 69 66 20 30 0a 20 Ret = 0;.#if 0.
9a680 20 6e 52 65 74 20 2b 3d 20 73 71 6c 69 74 65 33 nRet += sqlite3
9a690 56 64 62 65 52 65 6c 65 61 73 65 4d 65 6d 6f 72 VdbeReleaseMemor
9a6a0 79 28 6e 29 3b 0a 23 65 6e 64 69 66 0a 20 20 6e y(n);.#endif. n
9a6b0 52 65 74 20 2b 3d 20 73 71 6c 69 74 65 33 50 63 Ret += sqlite3Pc
9a6c0 61 63 68 65 52 65 6c 65 61 73 65 4d 65 6d 6f 72 acheReleaseMemor
9a6d0 79 28 6e 2d 6e 52 65 74 29 3b 0a 20 20 72 65 74 y(n-nRet);. ret
9a6e0 75 72 6e 20 6e 52 65 74 3b 0a 23 65 6c 73 65 0a urn nRet;.#else.
9a6f0 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
9a700 45 52 28 6e 29 3b 0a 20 20 72 65 74 75 72 6e 20 ER(n);. return
9a710 53 51 4c 49 54 45 5f 4f 4b 3b 0a 23 65 6e 64 69 SQLITE_OK;.#endi
9a720 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 74 61 74 65 f.}../*.** State
9a730 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6c 6f 63 information loc
9a740 61 6c 20 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 al to the memory
9a750 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 allocation subs
9a760 79 73 74 65 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 ystem..*/.static
9a770 20 53 51 4c 49 54 45 5f 57 53 44 20 73 74 72 75 SQLITE_WSD stru
9a780 63 74 20 4d 65 6d 30 47 6c 6f 62 61 6c 20 7b 0a ct Mem0Global {.
9a790 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 /* Number of f
9a7a0 72 65 65 20 70 61 67 65 73 20 66 6f 72 20 73 63 ree pages for sc
9a7b0 72 61 74 63 68 20 61 6e 64 20 70 61 67 65 2d 63 ratch and page-c
9a7c0 61 63 68 65 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20 ache memory */.
9a7d0 20 75 33 32 20 6e 53 63 72 61 74 63 68 46 72 65 u32 nScratchFre
9a7e0 65 3b 0a 20 20 75 33 32 20 6e 50 61 67 65 46 72 e;. u32 nPageFr
9a7f0 65 65 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 6d ee;.. sqlite3_m
9a800 75 74 65 78 20 2a 6d 75 74 65 78 3b 20 20 20 20 utex *mutex;
9a810 20 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 74 6f /* Mutex to
9a820 20 73 65 72 69 61 6c 69 7a 65 20 61 63 63 65 73 serialize acces
9a830 73 20 2a 2f 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 s */.. /*. **
9a840 54 68 65 20 61 6c 61 72 6d 20 63 61 6c 6c 62 61 The alarm callba
9a850 63 6b 20 61 6e 64 20 69 74 73 20 61 72 67 75 6d ck and its argum
9a860 65 6e 74 73 2e 20 20 54 68 65 20 6d 65 6d 30 2e ents. The mem0.
9a870 6d 75 74 65 78 20 6c 6f 63 6b 20 77 69 6c 6c 0a mutex lock will.
9a880 20 20 2a 2a 20 62 65 20 68 65 6c 64 20 77 68 69 ** be held whi
9a890 6c 65 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 le the callback
9a8a0 69 73 20 72 75 6e 6e 69 6e 67 2e 20 20 52 65 63 is running. Rec
9a8b0 75 72 73 69 76 65 20 63 61 6c 6c 73 20 69 6e 74 ursive calls int
9a8c0 6f 0a 20 20 2a 2a 20 74 68 65 20 6d 65 6d 6f 72 o. ** the memor
9a8d0 79 20 73 75 62 73 79 73 74 65 6d 20 61 72 65 20 y subsystem are
9a8e0 61 6c 6c 6f 77 65 64 2c 20 62 75 74 20 6e 6f 20 allowed, but no
9a8f0 6e 65 77 20 63 61 6c 6c 62 61 63 6b 73 20 77 69 new callbacks wi
9a900 6c 6c 20 62 65 0a 20 20 2a 2a 20 69 73 73 75 65 ll be. ** issue
9a910 64 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 d.. */. sqlite
9a920 33 5f 69 6e 74 36 34 20 61 6c 61 72 6d 54 68 72 3_int64 alarmThr
9a930 65 73 68 6f 6c 64 3b 0a 20 20 76 6f 69 64 20 28 eshold;. void (
9a940 2a 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 29 28 *alarmCallback)(
9a950 76 6f 69 64 2a 2c 20 73 71 6c 69 74 65 33 5f 69 void*, sqlite3_i
9a960 6e 74 36 34 2c 69 6e 74 29 3b 0a 20 20 76 6f 69 nt64,int);. voi
9a970 64 20 2a 61 6c 61 72 6d 41 72 67 3b 0a 0a 20 20 d *alarmArg;..
9a980 2f 2a 0a 20 20 2a 2a 20 50 6f 69 6e 74 65 72 73 /*. ** Pointers
9a990 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 73 to the end of s
9a9a0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
9a9b0 69 67 2e 70 53 63 72 61 74 63 68 20 61 6e 64 0a ig.pScratch and.
9a9c0 20 20 2a 2a 20 73 71 6c 69 74 65 33 47 6c 6f 62 ** sqlite3Glob
9a9d0 61 6c 43 6f 6e 66 69 67 2e 70 50 61 67 65 20 74 alConfig.pPage t
9a9e0 6f 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d o a block of mem
9a9f0 6f 72 79 20 74 68 61 74 20 72 65 63 6f 72 64 73 ory that records
9aa00 0a 20 20 2a 2a 20 77 68 69 63 68 20 70 61 67 65 . ** which page
9aa10 73 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65 2e s are available.
9aa20 0a 20 20 2a 2f 0a 20 20 75 33 32 20 2a 61 53 63 . */. u32 *aSc
9aa30 72 61 74 63 68 46 72 65 65 3b 0a 20 20 75 33 32 ratchFree;. u32
9aa40 20 2a 61 50 61 67 65 46 72 65 65 3b 0a 7d 20 6d *aPageFree;.} m
9aa50 65 6d 30 20 3d 20 7b 20 30 2c 20 30 2c 20 30 2c em0 = { 0, 0, 0,
9aa60 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 20 7d 0, 0, 0, 0, 0 }
9aa70 3b 0a 0a 23 64 65 66 69 6e 65 20 6d 65 6d 30 20 ;..#define mem0
9aa80 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 4d 65 GLOBAL(struct Me
9aa90 6d 30 47 6c 6f 62 61 6c 2c 20 6d 65 6d 30 29 0a m0Global, mem0).
9aaa0 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a ./*.** Initializ
9aab0 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c e the memory all
9aac0 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 ocation subsyste
9aad0 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 m..*/.SQLITE_PRI
9aae0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
9aaf0 4d 61 6c 6c 6f 63 49 6e 69 74 28 76 6f 69 64 29 MallocInit(void)
9ab00 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 {. if( sqlite3G
9ab10 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 4d lobalConfig.m.xM
9ab20 61 6c 6c 6f 63 3d 3d 30 20 29 7b 0a 20 20 20 20 alloc==0 ){.
9ab30 73 71 6c 69 74 65 33 4d 65 6d 53 65 74 44 65 66 sqlite3MemSetDef
9ab40 61 75 6c 74 28 29 3b 0a 20 20 7d 0a 20 20 6d 65 ault();. }. me
9ab50 6d 73 65 74 28 26 6d 65 6d 30 2c 20 30 2c 20 73 mset(&mem0, 0, s
9ab60 69 7a 65 6f 66 28 6d 65 6d 30 29 29 3b 0a 20 20 izeof(mem0));.
9ab70 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 if( sqlite3Globa
9ab80 6c 43 6f 6e 66 69 67 2e 62 43 6f 72 65 4d 75 74 lConfig.bCoreMut
9ab90 65 78 20 29 7b 0a 20 20 20 20 6d 65 6d 30 2e 6d ex ){. mem0.m
9aba0 75 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 utex = sqlite3Mu
9abb0 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f texAlloc(SQLITE_
9abc0 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d MUTEX_STATIC_MEM
9abd0 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c );. }. if( sql
9abe0 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
9abf0 2e 70 53 63 72 61 74 63 68 20 26 26 20 73 71 6c .pScratch && sql
9ac00 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
9ac10 2e 73 7a 53 63 72 61 74 63 68 3e 3d 31 30 30 0a .szScratch>=100.
9ac20 20 20 20 20 20 20 26 26 20 73 71 6c 69 74 65 33 && sqlite3
9ac30 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 53 63 GlobalConfig.nSc
9ac40 72 61 74 63 68 3e 3d 30 20 29 7b 0a 20 20 20 20 ratch>=0 ){.
9ac50 69 6e 74 20 69 3b 0a 20 20 20 20 73 71 6c 69 74 int i;. sqlit
9ac60 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 e3GlobalConfig.s
9ac70 7a 53 63 72 61 74 63 68 20 3d 20 52 4f 55 4e 44 zScratch = ROUND
9ac80 44 4f 57 4e 38 28 73 71 6c 69 74 65 33 47 6c 6f DOWN8(sqlite3Glo
9ac90 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61 balConfig.szScra
9aca0 74 63 68 2d 34 29 3b 0a 20 20 20 20 6d 65 6d 30 tch-4);. mem0
9acb0 2e 61 53 63 72 61 74 63 68 46 72 65 65 20 3d 20 .aScratchFree =
9acc0 28 75 33 32 2a 29 26 28 28 63 68 61 72 2a 29 73 (u32*)&((char*)s
9acd0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
9ace0 69 67 2e 70 53 63 72 61 74 63 68 29 0a 20 20 20 ig.pScratch).
9acf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5b [
9ad00 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
9ad10 66 69 67 2e 73 7a 53 63 72 61 74 63 68 2a 73 71 fig.szScratch*sq
9ad20 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
9ad30 67 2e 6e 53 63 72 61 74 63 68 5d 3b 0a 20 20 20 g.nScratch];.
9ad40 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73 71 6c 69 for(i=0; i<sqli
9ad50 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
9ad60 6e 53 63 72 61 74 63 68 3b 20 69 2b 2b 29 7b 20 nScratch; i++){
9ad70 6d 65 6d 30 2e 61 53 63 72 61 74 63 68 46 72 65 mem0.aScratchFre
9ad80 65 5b 69 5d 20 3d 20 69 3b 20 7d 0a 20 20 20 20 e[i] = i; }.
9ad90 6d 65 6d 30 2e 6e 53 63 72 61 74 63 68 46 72 65 mem0.nScratchFre
9ada0 65 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 e = sqlite3Globa
9adb0 6c 43 6f 6e 66 69 67 2e 6e 53 63 72 61 74 63 68 lConfig.nScratch
9adc0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 ;. }else{. s
9add0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
9ade0 69 67 2e 70 53 63 72 61 74 63 68 20 3d 20 30 3b ig.pScratch = 0;
9adf0 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 . sqlite3Glob
9ae00 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61 74 alConfig.szScrat
9ae10 63 68 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 ch = 0;. }. if
9ae20 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 ( sqlite3GlobalC
9ae30 6f 6e 66 69 67 2e 70 50 61 67 65 20 26 26 20 73 onfig.pPage && s
9ae40 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
9ae50 69 67 2e 73 7a 50 61 67 65 3e 3d 35 31 32 0a 20 ig.szPage>=512.
9ae60 20 20 20 20 20 26 26 20 73 71 6c 69 74 65 33 47 && sqlite3G
9ae70 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 50 61 67 lobalConfig.nPag
9ae80 65 3e 3d 31 20 29 7b 0a 20 20 20 20 69 6e 74 20 e>=1 ){. int
9ae90 69 3b 0a 20 20 20 20 69 6e 74 20 6f 76 65 72 68 i;. int overh
9aea0 65 61 64 3b 0a 20 20 20 20 69 6e 74 20 73 7a 20 ead;. int sz
9aeb0 3d 20 52 4f 55 4e 44 44 4f 57 4e 38 28 73 71 6c = ROUNDDOWN8(sql
9aec0 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
9aed0 2e 73 7a 50 61 67 65 29 3b 0a 20 20 20 20 69 6e .szPage);. in
9aee0 74 20 6e 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f t n = sqlite3Glo
9aef0 62 61 6c 43 6f 6e 66 69 67 2e 6e 50 61 67 65 3b balConfig.nPage;
9af00 0a 20 20 20 20 6f 76 65 72 68 65 61 64 20 3d 20 . overhead =
9af10 28 34 2a 6e 20 2b 20 73 7a 20 2d 20 31 29 2f 73 (4*n + sz - 1)/s
9af20 7a 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c z;. sqlite3Gl
9af30 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 50 61 67 65 obalConfig.nPage
9af40 20 2d 3d 20 6f 76 65 72 68 65 61 64 3b 0a 20 20 -= overhead;.
9af50 20 20 6d 65 6d 30 2e 61 50 61 67 65 46 72 65 65 mem0.aPageFree
9af60 20 3d 20 28 75 33 32 2a 29 26 28 28 63 68 61 72 = (u32*)&((char
9af70 2a 29 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 *)sqlite3GlobalC
9af80 6f 6e 66 69 67 2e 70 50 61 67 65 29 0a 20 20 20 onfig.pPage).
9af90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5b [
9afa0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
9afb0 66 69 67 2e 73 7a 50 61 67 65 2a 73 71 6c 69 74 fig.szPage*sqlit
9afc0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e e3GlobalConfig.n
9afd0 50 61 67 65 5d 3b 0a 20 20 20 20 66 6f 72 28 69 Page];. for(i
9afe0 3d 30 3b 20 69 3c 73 71 6c 69 74 65 33 47 6c 6f =0; i<sqlite3Glo
9aff0 62 61 6c 43 6f 6e 66 69 67 2e 6e 50 61 67 65 3b balConfig.nPage;
9b000 20 69 2b 2b 29 7b 20 6d 65 6d 30 2e 61 50 61 67 i++){ mem0.aPag
9b010 65 46 72 65 65 5b 69 5d 20 3d 20 69 3b 20 7d 0a eFree[i] = i; }.
9b020 20 20 20 20 6d 65 6d 30 2e 6e 50 61 67 65 46 72 mem0.nPageFr
9b030 65 65 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 ee = sqlite3Glob
9b040 61 6c 43 6f 6e 66 69 67 2e 6e 50 61 67 65 3b 0a alConfig.nPage;.
9b050 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c }else{. sql
9b060 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
9b070 2e 70 50 61 67 65 20 3d 20 30 3b 0a 20 20 20 20 .pPage = 0;.
9b080 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
9b090 66 69 67 2e 73 7a 50 61 67 65 20 3d 20 30 3b 0a fig.szPage = 0;.
9b0a0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 73 71 6c }. return sql
9b0b0 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
9b0c0 2e 6d 2e 78 49 6e 69 74 28 73 71 6c 69 74 65 33 .m.xInit(sqlite3
9b0d0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 70 GlobalConfig.m.p
9b0e0 41 70 70 44 61 74 61 29 3b 0a 7d 0a 0a 2f 2a 0a AppData);.}../*.
9b0f0 2a 2a 20 44 65 69 6e 69 74 69 61 6c 69 7a 65 20 ** Deinitialize
9b100 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 the memory alloc
9b110 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 2e ation subsystem.
9b120 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
9b130 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d TE void sqlite3M
9b140 61 6c 6c 6f 63 45 6e 64 28 76 6f 69 64 29 7b 0a allocEnd(void){.
9b150 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f if( sqlite3Glo
9b160 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 53 68 75 balConfig.m.xShu
9b170 74 64 6f 77 6e 20 29 7b 0a 20 20 20 20 73 71 6c tdown ){. sql
9b180 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
9b190 2e 6d 2e 78 53 68 75 74 64 6f 77 6e 28 73 71 6c .m.xShutdown(sql
9b1a0 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
9b1b0 2e 6d 2e 70 41 70 70 44 61 74 61 29 3b 0a 20 20 .m.pAppData);.
9b1c0 7d 0a 20 20 6d 65 6d 73 65 74 28 26 6d 65 6d 30 }. memset(&mem0
9b1d0 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6d 65 6d 30 , 0, sizeof(mem0
9b1e0 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 ));.}../*.** Ret
9b1f0 75 72 6e 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f urn the amount o
9b200 66 20 6d 65 6d 6f 72 79 20 63 75 72 72 65 6e 74 f memory current
9b210 6c 79 20 63 68 65 63 6b 65 64 20 6f 75 74 2e 0a ly checked out..
9b220 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 */.SQLITE_API sq
9b230 6c 69 74 65 33 5f 69 6e 74 36 34 20 73 71 6c 69 lite3_int64 sqli
9b240 74 65 33 5f 6d 65 6d 6f 72 79 5f 75 73 65 64 28 te3_memory_used(
9b250 76 6f 69 64 29 7b 0a 20 20 69 6e 74 20 6e 2c 20 void){. int n,
9b260 6d 78 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e mx;. sqlite3_in
9b270 74 36 34 20 72 65 73 3b 0a 20 20 73 71 6c 69 74 t64 res;. sqlit
9b280 65 33 5f 73 74 61 74 75 73 28 53 51 4c 49 54 45 e3_status(SQLITE
9b290 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 _STATUS_MEMORY_U
9b2a0 53 45 44 2c 20 26 6e 2c 20 26 6d 78 2c 20 30 29 SED, &n, &mx, 0)
9b2b0 3b 0a 20 20 72 65 73 20 3d 20 28 73 71 6c 69 74 ;. res = (sqlit
9b2c0 65 33 5f 69 6e 74 36 34 29 6e 3b 20 20 2f 2a 20 e3_int64)n; /*
9b2d0 57 6f 72 6b 20 61 72 6f 75 6e 64 20 62 75 67 20 Work around bug
9b2e0 69 6e 20 42 6f 72 6c 61 6e 64 20 43 2e 20 54 69 in Borland C. Ti
9b2f0 63 6b 65 74 20 23 33 32 31 36 20 2a 2f 0a 20 20 cket #3216 */.
9b300 72 65 74 75 72 6e 20 72 65 73 3b 0a 7d 0a 0a 2f return res;.}../
9b310 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
9b320 6d 61 78 69 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f maximum amount o
9b330 66 20 6d 65 6d 6f 72 79 20 74 68 61 74 20 68 61 f memory that ha
9b340 73 20 65 76 65 72 20 62 65 65 6e 0a 2a 2a 20 63 s ever been.** c
9b350 68 65 63 6b 65 64 20 6f 75 74 20 73 69 6e 63 65 hecked out since
9b360 20 65 69 74 68 65 72 20 74 68 65 20 62 65 67 69 either the begi
9b370 6e 6e 69 6e 67 20 6f 66 20 74 68 69 73 20 70 72 nning of this pr
9b380 6f 63 65 73 73 0a 2a 2a 20 6f 72 20 73 69 6e 63 ocess.** or sinc
9b390 65 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e e the most recen
9b3a0 74 20 72 65 73 65 74 2e 0a 2a 2f 0a 53 51 4c 49 t reset..*/.SQLI
9b3b0 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 69 TE_API sqlite3_i
9b3c0 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 6d 65 6d nt64 sqlite3_mem
9b3d0 6f 72 79 5f 68 69 67 68 77 61 74 65 72 28 69 6e ory_highwater(in
9b3e0 74 20 72 65 73 65 74 46 6c 61 67 29 7b 0a 20 20 t resetFlag){.
9b3f0 69 6e 74 20 6e 2c 20 6d 78 3b 0a 20 20 73 71 6c int n, mx;. sql
9b400 69 74 65 33 5f 69 6e 74 36 34 20 72 65 73 3b 0a ite3_int64 res;.
9b410 20 20 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73 sqlite3_status
9b420 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d (SQLITE_STATUS_M
9b430 45 4d 4f 52 59 5f 55 53 45 44 2c 20 26 6e 2c 20 EMORY_USED, &n,
9b440 26 6d 78 2c 20 72 65 73 65 74 46 6c 61 67 29 3b &mx, resetFlag);
9b450 0a 20 20 72 65 73 20 3d 20 28 73 71 6c 69 74 65 . res = (sqlite
9b460 33 5f 69 6e 74 36 34 29 6d 78 3b 20 20 2f 2a 20 3_int64)mx; /*
9b470 57 6f 72 6b 20 61 72 6f 75 6e 64 20 62 75 67 20 Work around bug
9b480 69 6e 20 42 6f 72 6c 61 6e 64 20 43 2e 20 54 69 in Borland C. Ti
9b490 63 6b 65 74 20 23 33 32 31 36 20 2a 2f 0a 20 20 cket #3216 */.
9b4a0 72 65 74 75 72 6e 20 72 65 73 3b 0a 7d 0a 0a 2f return res;.}../
9b4b0 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 *.** Change the
9b4c0 61 6c 61 72 6d 20 63 61 6c 6c 62 61 63 6b 0a 2a alarm callback.*
9b4d0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
9b4e0 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 6f int sqlite3Memo
9b4f0 72 79 41 6c 61 72 6d 28 0a 20 20 76 6f 69 64 28 ryAlarm(. void(
9b500 2a 78 43 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 *xCallback)(void
9b510 20 2a 70 41 72 67 2c 20 73 71 6c 69 74 65 33 5f *pArg, sqlite3_
9b520 69 6e 74 36 34 20 75 73 65 64 2c 69 6e 74 20 4e int64 used,int N
9b530 29 2c 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 2c ),. void *pArg,
9b540 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 . sqlite3_int64
9b550 20 69 54 68 72 65 73 68 6f 6c 64 0a 29 7b 0a 20 iThreshold.){.
9b560 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 sqlite3_mutex_e
9b570 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29 nter(mem0.mutex)
9b580 3b 0a 20 20 6d 65 6d 30 2e 61 6c 61 72 6d 43 61 ;. mem0.alarmCa
9b590 6c 6c 62 61 63 6b 20 3d 20 78 43 61 6c 6c 62 61 llback = xCallba
9b5a0 63 6b 3b 0a 20 20 6d 65 6d 30 2e 61 6c 61 72 6d ck;. mem0.alarm
9b5b0 41 72 67 20 3d 20 70 41 72 67 3b 0a 20 20 6d 65 Arg = pArg;. me
9b5c0 6d 30 2e 61 6c 61 72 6d 54 68 72 65 73 68 6f 6c m0.alarmThreshol
9b5d0 64 20 3d 20 69 54 68 72 65 73 68 6f 6c 64 3b 0a d = iThreshold;.
9b5e0 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
9b5f0 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65 78 leave(mem0.mutex
9b600 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 );. return SQLI
9b610 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 TE_OK;.}..#ifnde
9b620 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 f SQLITE_OMIT_DE
9b630 50 52 45 43 41 54 45 44 0a 2f 2a 0a 2a 2a 20 44 PRECATED./*.** D
9b640 65 70 72 65 63 61 74 65 64 20 65 78 74 65 72 6e eprecated extern
9b650 61 6c 20 69 6e 74 65 72 66 61 63 65 2e 20 20 49 al interface. I
9b660 6e 74 65 72 6e 61 6c 2f 63 6f 72 65 20 53 51 4c nternal/core SQL
9b670 69 74 65 20 63 6f 64 65 0a 2a 2a 20 73 68 6f 75 ite code.** shou
9b680 6c 64 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 4d ld call sqlite3M
9b690 65 6d 6f 72 79 41 6c 61 72 6d 2e 0a 2a 2f 0a 53 emoryAlarm..*/.S
9b6a0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
9b6b0 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 61 6c 61 lite3_memory_ala
9b6c0 72 6d 28 0a 20 20 76 6f 69 64 28 2a 78 43 61 6c rm(. void(*xCal
9b6d0 6c 62 61 63 6b 29 28 76 6f 69 64 20 2a 70 41 72 lback)(void *pAr
9b6e0 67 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 g, sqlite3_int64
9b6f0 20 75 73 65 64 2c 69 6e 74 20 4e 29 2c 0a 20 20 used,int N),.
9b700 76 6f 69 64 20 2a 70 41 72 67 2c 0a 20 20 73 71 void *pArg,. sq
9b710 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 54 68 72 lite3_int64 iThr
9b720 65 73 68 6f 6c 64 0a 29 7b 0a 20 20 72 65 74 75 eshold.){. retu
9b730 72 6e 20 73 71 6c 69 74 65 33 4d 65 6d 6f 72 79 rn sqlite3Memory
9b740 41 6c 61 72 6d 28 78 43 61 6c 6c 62 61 63 6b 2c Alarm(xCallback,
9b750 20 70 41 72 67 2c 20 69 54 68 72 65 73 68 6f 6c pArg, iThreshol
9b760 64 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a d);.}.#endif../*
9b770 0a 2a 2a 20 54 72 69 67 67 65 72 20 74 68 65 20 .** Trigger the
9b780 61 6c 61 72 6d 20 0a 2a 2f 0a 73 74 61 74 69 63 alarm .*/.static
9b790 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 61 6c void sqlite3Mal
9b7a0 6c 6f 63 41 6c 61 72 6d 28 69 6e 74 20 6e 42 79 locAlarm(int nBy
9b7b0 74 65 29 7b 0a 20 20 76 6f 69 64 20 28 2a 78 43 te){. void (*xC
9b7c0 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a 2c 73 allback)(void*,s
9b7d0 71 6c 69 74 65 33 5f 69 6e 74 36 34 2c 69 6e 74 qlite3_int64,int
9b7e0 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 );. sqlite3_int
9b7f0 36 34 20 6e 6f 77 55 73 65 64 3b 0a 20 20 76 6f 64 nowUsed;. vo
9b800 69 64 20 2a 70 41 72 67 3b 0a 20 20 69 66 28 20 id *pArg;. if(
9b810 6d 65 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 mem0.alarmCallba
9b820 63 6b 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a ck==0 ) return;.
9b830 20 20 78 43 61 6c 6c 62 61 63 6b 20 3d 20 6d 65 xCallback = me
9b840 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b m0.alarmCallback
9b850 3b 0a 20 20 6e 6f 77 55 73 65 64 20 3d 20 73 71 ;. nowUsed = sq
9b860 6c 69 74 65 33 53 74 61 74 75 73 56 61 6c 75 65 lite3StatusValue
9b870 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d (SQLITE_STATUS_M
9b880 45 4d 4f 52 59 5f 55 53 45 44 29 3b 0a 20 20 70 EMORY_USED);. p
9b890 41 72 67 20 3d 20 6d 65 6d 30 2e 61 6c 61 72 6d Arg = mem0.alarm
9b8a0 41 72 67 3b 0a 20 20 6d 65 6d 30 2e 61 6c 61 72 Arg;. mem0.alar
9b8b0 6d 43 61 6c 6c 62 61 63 6b 20 3d 20 30 3b 0a 20 mCallback = 0;.
9b8c0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
9b8d0 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65 78 29 eave(mem0.mutex)
9b8e0 3b 0a 20 20 78 43 61 6c 6c 62 61 63 6b 28 70 41 ;. xCallback(pA
9b8f0 72 67 2c 20 6e 6f 77 55 73 65 64 2c 20 6e 42 79 rg, nowUsed, nBy
9b900 74 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d te);. sqlite3_m
9b910 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e utex_enter(mem0.
9b920 6d 75 74 65 78 29 3b 0a 20 20 6d 65 6d 30 2e 61 mutex);. mem0.a
9b930 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 20 3d 20 78 larmCallback = x
9b940 43 61 6c 6c 62 61 63 6b 3b 0a 20 20 6d 65 6d 30 Callback;. mem0
9b950 2e 61 6c 61 72 6d 41 72 67 20 3d 20 70 41 72 67 .alarmArg = pArg
9b960 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 6f 20 61 20 ;.}../*.** Do a
9b970 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
9b980 6e 20 77 69 74 68 20 73 74 61 74 69 73 74 69 63 n with statistic
9b990 73 20 61 6e 64 20 61 6c 61 72 6d 73 2e 20 20 41 s and alarms. A
9b9a0 73 73 75 6d 65 20 74 68 65 0a 2a 2a 20 6c 6f 63 ssume the.** loc
9b9b0 6b 20 69 73 20 61 6c 72 65 61 64 79 20 68 65 6c k is already hel
9b9c0 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 d..*/.static int
9b9d0 20 6d 61 6c 6c 6f 63 57 69 74 68 41 6c 61 72 6d mallocWithAlarm
9b9e0 28 69 6e 74 20 6e 2c 20 76 6f 69 64 20 2a 2a 70 (int n, void **p
9b9f0 70 29 7b 0a 20 20 69 6e 74 20 6e 46 75 6c 6c 3b p){. int nFull;
9ba00 0a 20 20 76 6f 69 64 20 2a 70 3b 0a 20 20 61 73 . void *p;. as
9ba10 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
9ba20 74 65 78 5f 68 65 6c 64 28 6d 65 6d 30 2e 6d 75 tex_held(mem0.mu
9ba30 74 65 78 29 20 29 3b 0a 20 20 6e 46 75 6c 6c 20 tex) );. nFull
9ba40 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 = sqlite3GlobalC
9ba50 6f 6e 66 69 67 2e 6d 2e 78 52 6f 75 6e 64 75 70 onfig.m.xRoundup
9ba60 28 6e 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 74 (n);. sqlite3St
9ba70 61 74 75 73 53 65 74 28 53 51 4c 49 54 45 5f 53 atusSet(SQLITE_S
9ba80 54 41 54 55 53 5f 4d 41 4c 4c 4f 43 5f 53 49 5a TATUS_MALLOC_SIZ
9ba90 45 2c 20 6e 29 3b 0a 20 20 69 66 28 20 6d 65 6d E, n);. if( mem
9baa0 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 21 0.alarmCallback!
9bab0 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 55 =0 ){. int nU
9bac0 73 65 64 20 3d 20 73 71 6c 69 74 65 33 53 74 61 sed = sqlite3Sta
9bad0 74 75 73 56 61 6c 75 65 28 53 51 4c 49 54 45 5f tusValue(SQLITE_
9bae0 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 STATUS_MEMORY_US
9baf0 45 44 29 3b 0a 20 20 20 20 69 66 28 20 6e 55 73 ED);. if( nUs
9bb00 65 64 2b 6e 46 75 6c 6c 20 3e 3d 20 6d 65 6d 30 ed+nFull >= mem0
9bb10 2e 61 6c 61 72 6d 54 68 72 65 73 68 6f 6c 64 20 .alarmThreshold
9bb20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
9bb30 4d 61 6c 6c 6f 63 41 6c 61 72 6d 28 6e 46 75 6c MallocAlarm(nFul
9bb40 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 l);. }. }.
9bb50 70 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 p = sqlite3Globa
9bb60 6c 43 6f 6e 66 69 67 2e 6d 2e 78 4d 61 6c 6c 6f lConfig.m.xMallo
9bb70 63 28 6e 46 75 6c 6c 29 3b 0a 20 20 69 66 28 20 c(nFull);. if(
9bb80 70 3d 3d 30 20 26 26 20 6d 65 6d 30 2e 61 6c 61 p==0 && mem0.ala
9bb90 72 6d 43 61 6c 6c 62 61 63 6b 20 29 7b 0a 20 20 rmCallback ){.
9bba0 20 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 sqlite3MallocA
9bbb0 6c 61 72 6d 28 6e 46 75 6c 6c 29 3b 0a 20 20 20 larm(nFull);.
9bbc0 20 70 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 p = sqlite3Glob
9bbd0 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 4d 61 6c 6c alConfig.m.xMall
9bbe0 6f 63 28 6e 46 75 6c 6c 29 3b 0a 20 20 7d 0a 20 oc(nFull);. }.
9bbf0 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 6e 46 if( p ){. nF
9bc00 75 6c 6c 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c ull = sqlite3Mal
9bc10 6c 6f 63 53 69 7a 65 28 70 29 3b 0a 20 20 20 20 locSize(p);.
9bc20 73 71 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 sqlite3StatusAdd
9bc30 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d (SQLITE_STATUS_M
9bc40 45 4d 4f 52 59 5f 55 53 45 44 2c 20 6e 46 75 6c EMORY_USED, nFul
9bc50 6c 29 3b 0a 20 20 7d 0a 20 20 2a 70 70 20 3d 20 l);. }. *pp =
9bc60 70 3b 0a 20 20 72 65 74 75 72 6e 20 6e 46 75 6c p;. return nFul
9bc70 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f l;.}../*.** Allo
9bc80 63 61 74 65 20 6d 65 6d 6f 72 79 2e 20 20 54 68 cate memory. Th
9bc90 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 6c 69 is routine is li
9bca0 6b 65 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f ke sqlite3_mallo
9bcb0 63 28 29 20 65 78 63 65 70 74 20 74 68 61 74 20 c() except that
9bcc0 69 74 0a 2a 2a 20 61 73 73 75 6d 65 73 20 74 68 it.** assumes th
9bcd0 65 20 6d 65 6d 6f 72 79 20 73 75 62 73 79 73 74 e memory subsyst
9bce0 65 6d 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 em has already b
9bcf0 65 65 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e een initialized.
9bd00 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
9bd10 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 TE void *sqlite3
9bd20 4d 61 6c 6c 6f 63 28 69 6e 74 20 6e 29 7b 0a 20 Malloc(int n){.
9bd30 20 76 6f 69 64 20 2a 70 3b 0a 20 20 69 66 28 20 void *p;. if(
9bd40 6e 3c 3d 30 20 7c 7c 20 6e 3e 3d 30 78 37 66 66 n<=0 || n>=0x7ff
9bd50 66 66 66 30 30 20 29 7b 0a 20 20 20 20 2f 2a 20 fff00 ){. /*
9bd60 41 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 A memory allocat
9bd70 69 6f 6e 20 6f 66 20 61 20 6e 75 6d 62 65 72 20 ion of a number
9bd80 6f 66 20 62 79 74 65 73 20 77 68 69 63 68 20 69 of bytes which i
9bd90 73 20 6e 65 61 72 20 74 68 65 20 6d 61 78 69 6d s near the maxim
9bda0 75 6d 0a 20 20 20 20 2a 2a 20 73 69 67 6e 65 64 um. ** signed
9bdb0 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20 6d integer value m
9bdc0 69 67 68 74 20 63 61 75 73 65 20 61 6e 20 69 6e ight cause an in
9bdd0 74 65 67 65 72 20 6f 76 65 72 66 6c 6f 77 20 69 teger overflow i
9bde0 6e 73 69 64 65 20 6f 66 20 74 68 65 0a 20 20 20 nside of the.
9bdf0 20 2a 2a 20 78 4d 61 6c 6c 6f 63 28 29 2e 20 20 ** xMalloc().
9be00 48 65 6e 63 65 20 77 65 20 6c 69 6d 69 74 20 74 Hence we limit t
9be10 68 65 20 6d 61 78 69 6d 75 6d 20 73 69 7a 65 20 he maximum size
9be20 74 6f 20 30 78 37 66 66 66 66 66 30 30 2c 20 67 to 0x7fffff00, g
9be30 69 76 69 6e 67 0a 20 20 20 20 2a 2a 20 32 35 35 iving. ** 255
9be40 20 62 79 74 65 73 20 6f 66 20 6f 76 65 72 68 65 bytes of overhe
9be50 61 64 2e 20 20 53 51 4c 69 74 65 20 69 74 73 65 ad. SQLite itse
9be60 6c 66 20 77 69 6c 6c 20 6e 65 76 65 72 20 75 73 lf will never us
9be70 65 20 61 6e 79 74 68 69 6e 67 20 6e 65 61 72 0a e anything near.
9be80 20 20 20 20 2a 2a 20 74 68 69 73 20 61 6d 6f 75 ** this amou
9be90 6e 74 2e 20 20 54 68 65 20 6f 6e 6c 79 20 77 61 nt. The only wa
9bea0 79 20 74 6f 20 72 65 61 63 68 20 74 68 65 20 6c y to reach the l
9beb0 69 6d 69 74 20 69 73 20 77 69 74 68 20 73 71 6c imit is with sql
9bec0 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 2a 2f ite3_malloc() */
9bed0 0a 20 20 20 20 70 20 3d 20 30 3b 0a 20 20 7d 65 . p = 0;. }e
9bee0 6c 73 65 20 69 66 28 20 73 71 6c 69 74 65 33 47 lse if( sqlite3G
9bef0 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d lobalConfig.bMem
9bf00 73 74 61 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 stat ){. sqli
9bf10 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 te3_mutex_enter(
9bf20 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 mem0.mutex);.
9bf30 20 6d 61 6c 6c 6f 63 57 69 74 68 41 6c 61 72 6d mallocWithAlarm
9bf40 28 6e 2c 20 26 70 29 3b 0a 20 20 20 20 73 71 6c (n, &p);. sql
9bf50 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 ite3_mutex_leave
9bf60 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 (mem0.mutex);.
9bf70 7d 65 6c 73 65 7b 0a 20 20 20 20 70 20 3d 20 73 }else{. p = s
9bf80 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
9bf90 69 67 2e 6d 2e 78 4d 61 6c 6c 6f 63 28 6e 29 3b ig.m.xMalloc(n);
9bfa0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b . }. return p;
9bfb0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 .}../*.** This v
9bfc0 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 ersion of the me
9bfd0 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
9bfe0 69 73 20 66 6f 72 20 75 73 65 20 62 79 20 74 68 is for use by th
9bff0 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2e 0a 2a e application..*
9c000 2a 20 46 69 72 73 74 20 6d 61 6b 65 20 73 75 72 * First make sur
9c010 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 73 75 62 e the memory sub
9c020 73 79 73 74 65 6d 20 69 73 20 69 6e 69 74 69 61 system is initia
9c030 6c 69 7a 65 64 2c 20 74 68 65 6e 20 64 6f 20 74 lized, then do t
9c040 68 65 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e he.** allocation
9c050 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
9c060 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 6d 61 void *sqlite3_ma
9c070 6c 6c 6f 63 28 69 6e 74 20 6e 29 7b 0a 23 69 66 lloc(int n){.#if
9c080 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
9c090 5f 41 55 54 4f 49 4e 49 54 0a 20 20 69 66 28 20 _AUTOINIT. if(
9c0a0 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 sqlite3_initiali
9c0b0 7a 65 28 29 20 29 20 72 65 74 75 72 6e 20 30 3b ze() ) return 0;
9c0c0 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e .#endif. return
9c0d0 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e sqlite3Malloc(n
9c0e0 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 );.}../*.** Each
9c0f0 20 74 68 72 65 61 64 20 6d 61 79 20 6f 6e 6c 79 thread may only
9c100 20 68 61 76 65 20 61 20 73 69 6e 67 6c 65 20 6f have a single o
9c110 75 74 73 74 61 6e 64 69 6e 67 20 61 6c 6c 6f 63 utstanding alloc
9c120 61 74 69 6f 6e 20 66 72 6f 6d 0a 2a 2a 20 78 53 ation from.** xS
9c130 63 72 61 74 63 68 4d 61 6c 6c 6f 63 28 29 2e 20 cratchMalloc().
9c140 20 57 65 20 76 65 72 69 66 79 20 74 68 69 73 20 We verify this
9c150 63 6f 6e 73 74 72 61 69 6e 74 20 69 6e 20 74 68 constraint in th
9c160 65 20 73 69 6e 67 6c 65 2d 74 68 72 65 61 64 65 e single-threade
9c170 64 0a 2a 2a 20 63 61 73 65 20 62 79 20 73 65 74 d.** case by set
9c180 74 69 6e 67 20 73 63 72 61 74 63 68 41 6c 6c 6f ting scratchAllo
9c190 63 4f 75 74 20 74 6f 20 31 20 77 68 65 6e 20 61 cOut to 1 when a
9c1a0 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 n allocation.**
9c1b0 69 73 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 63 is outstanding c
9c1c0 6c 65 61 72 69 6e 67 20 69 74 20 77 68 65 6e 20 learing it when
9c1d0 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 the allocation i
9c1e0 73 20 66 72 65 65 64 2e 0a 2a 2f 0a 23 69 66 20 s freed..*/.#if
9c1f0 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 SQLITE_THREADSAF
9c200 45 3d 3d 30 20 26 26 20 21 64 65 66 69 6e 65 64 E==0 && !defined
9c210 28 4e 44 45 42 55 47 29 0a 73 74 61 74 69 63 20 (NDEBUG).static
9c220 69 6e 74 20 73 63 72 61 74 63 68 41 6c 6c 6f 63 int scratchAlloc
9c230 4f 75 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a Out = 0;.#endif.
9c240 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 ../*.** Allocate
9c250 20 6d 65 6d 6f 72 79 20 74 68 61 74 20 69 73 20 memory that is
9c260 74 6f 20 62 65 20 75 73 65 64 20 61 6e 64 20 72 to be used and r
9c270 65 6c 65 61 73 65 64 20 72 69 67 68 74 20 61 77 eleased right aw
9c280 61 79 2e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 ay..** This rout
9c290 69 6e 65 20 69 73 20 73 69 6d 69 6c 61 72 20 74 ine is similar t
9c2a0 6f 20 61 6c 6c 6f 63 61 28 29 20 69 6e 20 74 68 o alloca() in th
9c2b0 61 74 20 69 74 20 69 73 20 6e 6f 74 20 69 6e 74 at it is not int
9c2c0 65 6e 64 65 64 0a 2a 2a 20 66 6f 72 20 73 69 74 ended.** for sit
9c2d0 75 61 74 69 6f 6e 73 20 77 68 65 72 65 20 74 68 uations where th
9c2e0 65 20 6d 65 6d 6f 72 79 20 6d 69 67 68 74 20 62 e memory might b
9c2f0 65 20 68 65 6c 64 20 6c 6f 6e 67 2d 74 65 72 6d e held long-term
9c300 2e 20 20 54 68 69 73 0a 2a 2a 20 72 6f 75 74 69 . This.** routi
9c310 6e 65 20 69 73 20 69 6e 74 65 6e 64 65 64 20 74 ne is intended t
9c320 6f 20 67 65 74 20 6d 65 6d 6f 72 79 20 74 6f 20 o get memory to
9c330 6f 6c 64 20 6c 61 72 67 65 20 74 72 61 6e 73 69 old large transi
9c340 65 6e 74 20 64 61 74 61 0a 2a 2a 20 73 74 72 75 ent data.** stru
9c350 63 74 75 72 65 73 20 74 68 61 74 20 77 6f 75 6c ctures that woul
9c360 64 20 6e 6f 74 20 6e 6f 72 6d 61 6c 6c 79 20 66 d not normally f
9c370 69 74 20 6f 6e 20 74 68 65 20 73 74 61 63 6b 20 it on the stack
9c380 6f 66 20 61 6e 0a 2a 2a 20 65 6d 62 65 64 64 65 of an.** embedde
9c390 64 20 70 72 6f 63 65 73 73 6f 72 2e 0a 2a 2f 0a d processor..*/.
9c3a0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
9c3b0 6f 69 64 20 2a 73 71 6c 69 74 65 33 53 63 72 61 oid *sqlite3Scra
9c3c0 74 63 68 4d 61 6c 6c 6f 63 28 69 6e 74 20 6e 29 tchMalloc(int n)
9c3d0 7b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a 20 20 61 {. void *p;. a
9c3e0 73 73 65 72 74 28 20 6e 3e 30 20 29 3b 0a 0a 23 ssert( n>0 );..#
9c3f0 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 if SQLITE_THREAD
9c400 53 41 46 45 3d 3d 30 20 26 26 20 21 64 65 66 69 SAFE==0 && !defi
9c410 6e 65 64 28 4e 44 45 42 55 47 29 0a 20 20 2f 2a ned(NDEBUG). /*
9c420 20 56 65 72 69 66 79 20 74 68 61 74 20 6e 6f 20 Verify that no
9c430 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 73 63 more than one sc
9c440 72 61 74 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e ratch allocation
9c450 20 70 65 72 20 74 68 72 65 61 64 0a 20 20 2a 2a per thread. **
9c460 20 69 73 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 is outstanding
9c470 61 74 20 6f 6e 65 20 74 69 6d 65 2e 20 20 28 54 at one time. (T
9c480 68 69 73 20 69 73 20 6f 6e 6c 79 20 63 68 65 63 his is only chec
9c490 6b 65 64 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20 ked in the. **
9c4a0 73 69 6e 67 6c 65 2d 74 68 72 65 61 64 65 64 20 single-threaded
9c4b0 63 61 73 65 20 73 69 6e 63 65 20 63 68 65 63 6b case since check
9c4c0 69 6e 67 20 69 6e 20 74 68 65 20 6d 75 6c 74 69 ing in the multi
9c4d0 2d 74 68 72 65 61 64 65 64 20 63 61 73 65 0a 20 -threaded case.
9c4e0 20 2a 2a 20 77 6f 75 6c 64 20 62 65 20 6d 75 63 ** would be muc
9c4f0 68 20 6d 6f 72 65 20 63 6f 6d 70 6c 69 63 61 74 h more complicat
9c500 65 64 2e 29 20 2a 2f 0a 20 20 61 73 73 65 72 74 ed.) */. assert
9c510 28 20 73 63 72 61 74 63 68 41 6c 6c 6f 63 4f 75 ( scratchAllocOu
9c520 74 3d 3d 30 20 29 3b 0a 23 65 6e 64 69 66 0a 0a t==0 );.#endif..
9c530 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f if( sqlite3Glo
9c540 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61 balConfig.szScra
9c550 74 63 68 3c 6e 20 29 7b 0a 20 20 20 20 67 6f 74 tch<n ){. got
9c560 6f 20 73 63 72 61 74 63 68 5f 6f 76 65 72 66 6c o scratch_overfl
9c570 6f 77 3b 0a 20 20 7d 65 6c 73 65 7b 20 20 0a 20 ow;. }else{ .
9c580 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 sqlite3_mutex
9c590 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65 _enter(mem0.mute
9c5a0 78 29 3b 0a 20 20 20 20 69 66 28 20 6d 65 6d 30 x);. if( mem0
9c5b0 2e 6e 53 63 72 61 74 63 68 46 72 65 65 3d 3d 30 .nScratchFree==0
9c5c0 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
9c5d0 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 3_mutex_leave(me
9c5e0 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 m0.mutex);.
9c5f0 20 67 6f 74 6f 20 73 63 72 61 74 63 68 5f 6f 76 goto scratch_ov
9c600 65 72 66 6c 6f 77 3b 0a 20 20 20 20 7d 65 6c 73 erflow;. }els
9c610 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a e{. int i;.
9c620 20 20 20 20 20 20 69 20 3d 20 6d 65 6d 30 2e 61 i = mem0.a
9c630 53 63 72 61 74 63 68 46 72 65 65 5b 2d 2d 6d 65 ScratchFree[--me
9c640 6d 30 2e 6e 53 63 72 61 74 63 68 46 72 65 65 5d m0.nScratchFree]
9c650 3b 0a 20 20 20 20 20 20 69 20 2a 3d 20 73 71 6c ;. i *= sql
9c660 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
9c670 2e 73 7a 53 63 72 61 74 63 68 3b 0a 20 20 20 20 .szScratch;.
9c680 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 41 sqlite3StatusA
9c690 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 dd(SQLITE_STATUS
9c6a0 5f 53 43 52 41 54 43 48 5f 55 53 45 44 2c 20 31 _SCRATCH_USED, 1
9c6b0 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
9c6c0 53 74 61 74 75 73 53 65 74 28 53 51 4c 49 54 45 StatusSet(SQLITE
9c6d0 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f _STATUS_SCRATCH_
9c6e0 53 49 5a 45 2c 20 6e 29 3b 0a 20 20 20 20 20 20 SIZE, n);.
9c6f0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 sqlite3_mutex_le
9c700 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b ave(mem0.mutex);
9c710 0a 20 20 20 20 20 20 70 20 3d 20 28 76 6f 69 64 . p = (void
9c720 2a 29 26 28 28 63 68 61 72 2a 29 73 71 6c 69 74 *)&((char*)sqlit
9c730 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 e3GlobalConfig.p
9c740 53 63 72 61 74 63 68 29 5b 69 5d 3b 0a 20 20 20 Scratch)[i];.
9c750 20 20 20 61 73 73 65 72 74 28 20 20 28 28 28 75 assert( (((u
9c760 38 2a 29 70 20 2d 20 28 75 38 2a 29 30 29 20 26 8*)p - (u8*)0) &
9c770 20 37 29 3d 3d 30 20 29 3b 0a 20 20 20 20 7d 0a 7)==0 );. }.
9c780 20 20 7d 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 }.#if SQLITE_T
9c790 48 52 45 41 44 53 41 46 45 3d 3d 30 20 26 26 20 HREADSAFE==0 &&
9c7a0 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 !defined(NDEBUG)
9c7b0 0a 20 20 73 63 72 61 74 63 68 41 6c 6c 6f 63 4f . scratchAllocO
9c7c0 75 74 20 3d 20 70 21 3d 30 3b 0a 23 65 6e 64 69 ut = p!=0;.#endi
9c7d0 66 0a 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 0a f.. return p;..
9c7e0 73 63 72 61 74 63 68 5f 6f 76 65 72 66 6c 6f 77 scratch_overflow
9c7f0 3a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 :. if( sqlite3G
9c800 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d lobalConfig.bMem
9c810 73 74 61 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 stat ){. sqli
9c820 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 te3_mutex_enter(
9c830 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 mem0.mutex);.
9c840 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 53 65 sqlite3StatusSe
9c850 74 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f t(SQLITE_STATUS_
9c860 53 43 52 41 54 43 48 5f 53 49 5a 45 2c 20 6e 29 SCRATCH_SIZE, n)
9c870 3b 0a 20 20 20 20 6e 20 3d 20 6d 61 6c 6c 6f 63 ;. n = malloc
9c880 57 69 74 68 41 6c 61 72 6d 28 6e 2c 20 26 70 29 WithAlarm(n, &p)
9c890 3b 0a 20 20 20 20 69 66 28 20 70 20 29 20 73 71 ;. if( p ) sq
9c8a0 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 28 53 lite3StatusAdd(S
9c8b0 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52 QLITE_STATUS_SCR
9c8c0 41 54 43 48 5f 4f 56 45 52 46 4c 4f 57 2c 20 6e ATCH_OVERFLOW, n
9c8d0 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d );. sqlite3_m
9c8e0 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e utex_leave(mem0.
9c8f0 6d 75 74 65 78 29 3b 0a 20 20 7d 65 6c 73 65 7b mutex);. }else{
9c900 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 . p = sqlite3
9c910 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 GlobalConfig.m.x
9c920 4d 61 6c 6c 6f 63 28 6e 29 3b 0a 20 20 7d 0a 23 Malloc(n);. }.#
9c930 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 if SQLITE_THREAD
9c940 53 41 46 45 3d 3d 30 20 26 26 20 21 64 65 66 69 SAFE==0 && !defi
9c950 6e 65 64 28 4e 44 45 42 55 47 29 0a 20 20 73 63 ned(NDEBUG). sc
9c960 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74 20 3d 20 ratchAllocOut =
9c970 70 21 3d 30 3b 0a 23 65 6e 64 69 66 0a 20 20 72 p!=0;.#endif. r
9c980 65 74 75 72 6e 20 70 3b 20 20 20 20 0a 7d 0a 53 eturn p; .}.S
9c990 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
9c9a0 69 64 20 73 71 6c 69 74 65 33 53 63 72 61 74 63 id sqlite3Scratc
9c9b0 68 46 72 65 65 28 76 6f 69 64 20 2a 70 29 7b 0a hFree(void *p){.
9c9c0 20 20 69 66 28 20 70 20 29 7b 0a 0a 23 69 66 20 if( p ){..#if
9c9d0 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 SQLITE_THREADSAF
9c9e0 45 3d 3d 30 20 26 26 20 21 64 65 66 69 6e 65 64 E==0 && !defined
9c9f0 28 4e 44 45 42 55 47 29 0a 20 20 20 20 2f 2a 20 (NDEBUG). /*
9ca00 56 65 72 69 66 79 20 74 68 61 74 20 6e 6f 20 6d Verify that no m
9ca10 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 73 63 72 ore than one scr
9ca20 61 74 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 atch allocation
9ca30 70 65 72 20 74 68 72 65 61 64 0a 20 20 20 20 2a per thread. *
9ca40 2a 20 69 73 20 6f 75 74 73 74 61 6e 64 69 6e 67 * is outstanding
9ca50 20 61 74 20 6f 6e 65 20 74 69 6d 65 2e 20 20 28 at one time. (
9ca60 54 68 69 73 20 69 73 20 6f 6e 6c 79 20 63 68 65 This is only che
9ca70 63 6b 65 64 20 69 6e 20 74 68 65 0a 20 20 20 20 cked in the.
9ca80 2a 2a 20 73 69 6e 67 6c 65 2d 74 68 72 65 61 64 ** single-thread
9ca90 65 64 20 63 61 73 65 20 73 69 6e 63 65 20 63 68 ed case since ch
9caa0 65 63 6b 69 6e 67 20 69 6e 20 74 68 65 20 6d 75 ecking in the mu
9cab0 6c 74 69 2d 74 68 72 65 61 64 65 64 20 63 61 73 lti-threaded cas
9cac0 65 0a 20 20 20 20 2a 2a 20 77 6f 75 6c 64 20 62 e. ** would b
9cad0 65 20 6d 75 63 68 20 6d 6f 72 65 20 63 6f 6d 70 e much more comp
9cae0 6c 69 63 61 74 65 64 2e 29 20 2a 2f 0a 20 20 20 licated.) */.
9caf0 20 61 73 73 65 72 74 28 20 73 63 72 61 74 63 68 assert( scratch
9cb00 41 6c 6c 6f 63 4f 75 74 3d 3d 31 20 29 3b 0a 20 AllocOut==1 );.
9cb10 20 20 20 73 63 72 61 74 63 68 41 6c 6c 6f 63 4f scratchAllocO
9cb20 75 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a ut = 0;.#endif..
9cb30 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 if( sqlite3G
9cb40 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72 lobalConfig.pScr
9cb50 61 74 63 68 3d 3d 30 0a 20 20 20 20 20 20 20 20 atch==0.
9cb60 20 20 20 7c 7c 20 70 3c 73 71 6c 69 74 65 33 47 || p<sqlite3G
9cb70 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72 lobalConfig.pScr
9cb80 61 74 63 68 0a 20 20 20 20 20 20 20 20 20 20 20 atch.
9cb90 7c 7c 20 70 3e 3d 28 76 6f 69 64 2a 29 6d 65 6d || p>=(void*)mem
9cba0 30 2e 61 53 63 72 61 74 63 68 46 72 65 65 20 29 0.aScratchFree )
9cbb0 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 {. if( sqli
9cbc0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
9cbd0 62 4d 65 6d 73 74 61 74 20 29 7b 0a 20 20 20 20 bMemstat ){.
9cbe0 20 20 20 20 69 6e 74 20 69 53 69 7a 65 20 3d 20 int iSize =
9cbf0 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a sqlite3MallocSiz
9cc00 65 28 70 29 3b 0a 20 20 20 20 20 20 20 20 73 71 e(p);. sq
9cc10 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 lite3_mutex_ente
9cc20 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 r(mem0.mutex);.
9cc30 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 sqlite3St
9cc40 61 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 atusAdd(SQLITE_S
9cc50 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f 4f 56 TATUS_SCRATCH_OV
9cc60 45 52 46 4c 4f 57 2c 20 2d 69 53 69 7a 65 29 3b ERFLOW, -iSize);
9cc70 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
9cc80 53 74 61 74 75 73 41 64 64 28 53 51 4c 49 54 45 StatusAdd(SQLITE
9cc90 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 _STATUS_MEMORY_U
9cca0 53 45 44 2c 20 2d 69 53 69 7a 65 29 3b 0a 20 20 SED, -iSize);.
9ccb0 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f sqlite3Glo
9ccc0 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 46 72 65 balConfig.m.xFre
9ccd0 65 28 70 29 3b 0a 20 20 20 20 20 20 20 20 73 71 e(p);. sq
9cce0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
9ccf0 65 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 e(mem0.mutex);.
9cd00 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
9cd10 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 sqlite3Globa
9cd20 6c 43 6f 6e 66 69 67 2e 6d 2e 78 46 72 65 65 28 lConfig.m.xFree(
9cd30 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 p);. }.
9cd40 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 6e 74 }else{. int
9cd50 20 69 3b 0a 20 20 20 20 20 20 69 20 3d 20 28 69 i;. i = (i
9cd60 6e 74 29 28 28 75 38 2a 29 70 20 2d 20 28 75 38 nt)((u8*)p - (u8
9cd70 2a 29 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 *)sqlite3GlobalC
9cd80 6f 6e 66 69 67 2e 70 53 63 72 61 74 63 68 29 3b onfig.pScratch);
9cd90 0a 20 20 20 20 20 20 69 20 2f 3d 20 73 71 6c 69 . i /= sqli
9cda0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
9cdb0 73 7a 53 63 72 61 74 63 68 3b 0a 20 20 20 20 20 szScratch;.
9cdc0 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 assert( i>=0 &&
9cdd0 20 69 3c 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c i<sqlite3Global
9cde0 43 6f 6e 66 69 67 2e 6e 53 63 72 61 74 63 68 20 Config.nScratch
9cdf0 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
9ce00 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d _mutex_enter(mem
9ce10 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 0.mutex);.
9ce20 61 73 73 65 72 74 28 20 6d 65 6d 30 2e 6e 53 63 assert( mem0.nSc
9ce30 72 61 74 63 68 46 72 65 65 3c 28 75 33 32 29 73 ratchFree<(u32)s
9ce40 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
9ce50 69 67 2e 6e 53 63 72 61 74 63 68 20 29 3b 0a 20 ig.nScratch );.
9ce60 20 20 20 20 20 6d 65 6d 30 2e 61 53 63 72 61 74 mem0.aScrat
9ce70 63 68 46 72 65 65 5b 6d 65 6d 30 2e 6e 53 63 72 chFree[mem0.nScr
9ce80 61 74 63 68 46 72 65 65 2b 2b 5d 20 3d 20 69 3b atchFree++] = i;
9ce90 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 . sqlite3St
9cea0 61 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 atusAdd(SQLITE_S
9ceb0 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f 55 53 TATUS_SCRATCH_US
9cec0 45 44 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 73 ED, -1);. s
9ced0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
9cee0 76 65 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a ve(mem0.mutex);.
9cef0 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a }. }.}../*.
9cf00 2a 2a 20 54 52 55 45 20 69 66 20 70 20 69 73 20 ** TRUE if p is
9cf10 61 20 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 6d 6f a lookaside memo
9cf20 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 72 ry allocation fr
9cf30 6f 6d 20 64 62 0a 2a 2f 0a 23 69 66 6e 64 65 66 om db.*/.#ifndef
9cf40 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 4f SQLITE_OMIT_LOO
9cf50 4b 41 53 49 44 45 0a 73 74 61 74 69 63 20 69 6e KASIDE.static in
9cf60 74 20 69 73 4c 6f 6f 6b 61 73 69 64 65 28 73 71 t isLookaside(sq
9cf70 6c 69 74 65 33 20 2a 64 62 2c 20 76 6f 69 64 20 lite3 *db, void
9cf80 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 64 62 *p){. return db
9cf90 20 26 26 20 70 20 26 26 20 70 3e 3d 64 62 2d 3e && p && p>=db->
9cfa0 6c 6f 6f 6b 61 73 69 64 65 2e 70 53 74 61 72 74 lookaside.pStart
9cfb0 20 26 26 20 70 3c 64 62 2d 3e 6c 6f 6f 6b 61 73 && p<db->lookas
9cfc0 69 64 65 2e 70 45 6e 64 3b 0a 7d 0a 23 65 6c 73 ide.pEnd;.}.#els
9cfd0 65 0a 23 64 65 66 69 6e 65 20 69 73 4c 6f 6f 6b e.#define isLook
9cfe0 61 73 69 64 65 28 41 2c 42 29 20 30 0a 23 65 6e aside(A,B) 0.#en
9cff0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 dif../*.** Retur
9d000 6e 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 20 n the size of a
9d010 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
9d020 6e 20 70 72 65 76 69 6f 75 73 6c 79 20 6f 62 74 n previously obt
9d030 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 73 71 ained from.** sq
9d040 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 29 20 6f 72 lite3Malloc() or
9d050 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 sqlite3_malloc(
9d060 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 )..*/.SQLITE_PRI
9d070 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
9d080 4d 61 6c 6c 6f 63 53 69 7a 65 28 76 6f 69 64 20 MallocSize(void
9d090 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 *p){. return sq
9d0a0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
9d0b0 67 2e 6d 2e 78 53 69 7a 65 28 70 29 3b 0a 7d 0a g.m.xSize(p);.}.
9d0c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
9d0d0 6e 74 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c nt sqlite3DbMall
9d0e0 6f 63 53 69 7a 65 28 73 71 6c 69 74 65 33 20 2a ocSize(sqlite3 *
9d0f0 64 62 2c 20 76 6f 69 64 20 2a 70 29 7b 0a 20 20 db, void *p){.
9d100 61 73 73 65 72 74 28 20 64 62 3d 3d 30 20 7c 7c assert( db==0 ||
9d110 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
9d120 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 eld(db->mutex) )
9d130 3b 0a 20 20 69 66 28 20 69 73 4c 6f 6f 6b 61 73 ;. if( isLookas
9d140 69 64 65 28 64 62 2c 20 70 29 20 29 7b 0a 20 20 ide(db, p) ){.
9d150 20 20 72 65 74 75 72 6e 20 64 62 2d 3e 6c 6f 6f return db->loo
9d160 6b 61 73 69 64 65 2e 73 7a 3b 0a 20 20 7d 65 6c kaside.sz;. }el
9d170 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 73 se{. return s
9d180 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
9d190 69 67 2e 6d 2e 78 53 69 7a 65 28 70 29 3b 0a 20 ig.m.xSize(p);.
9d1a0 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 }.}../*.** Free
9d1b0 20 6d 65 6d 6f 72 79 20 70 72 65 76 69 6f 75 73 memory previous
9d1c0 6c 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d ly obtained from
9d1d0 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 29 sqlite3Malloc()
9d1e0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
9d1f0 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 66 72 65 void sqlite3_fre
9d200 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 69 66 e(void *p){. if
9d210 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b ( p==0 ) return;
9d220 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c . if( sqlite3Gl
9d230 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 obalConfig.bMems
9d240 74 61 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 tat ){. sqlit
9d250 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d e3_mutex_enter(m
9d260 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 em0.mutex);.
9d270 73 71 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 sqlite3StatusAdd
9d280 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d (SQLITE_STATUS_M
9d290 45 4d 4f 52 59 5f 55 53 45 44 2c 20 2d 73 71 6c EMORY_USED, -sql
9d2a0 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 ite3MallocSize(p
9d2b0 29 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 ));. sqlite3G
9d2c0 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 46 lobalConfig.m.xF
9d2d0 72 65 65 28 70 29 3b 0a 20 20 20 20 73 71 6c 69 ree(p);. sqli
9d2e0 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
9d2f0 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 7d mem0.mutex);. }
9d300 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 else{. sqlite
9d310 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 3GlobalConfig.m.
9d320 78 46 72 65 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a xFree(p);. }.}.
9d330 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 6d 65 6d 6f ./*.** Free memo
9d340 72 79 20 74 68 61 74 20 6d 69 67 68 74 20 62 65 ry that might be
9d350 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
9d360 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 64 61 a particular da
9d370 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 tabase.** connec
9d380 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f tion..*/.SQLITE_
9d390 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
9d3a0 69 74 65 33 44 62 46 72 65 65 28 73 71 6c 69 74 ite3DbFree(sqlit
9d3b0 65 33 20 2a 64 62 2c 20 76 6f 69 64 20 2a 70 29 e3 *db, void *p)
9d3c0 7b 0a 20 20 61 73 73 65 72 74 28 20 64 62 3d 3d {. assert( db==
9d3d0 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 0 || sqlite3_mut
9d3e0 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 ex_held(db->mute
9d3f0 78 29 20 29 3b 0a 20 20 69 66 28 20 69 73 4c 6f x) );. if( isLo
9d400 6f 6b 61 73 69 64 65 28 64 62 2c 20 70 29 20 29 okaside(db, p) )
9d410 7b 0a 20 20 20 20 4c 6f 6f 6b 61 73 69 64 65 53 {. LookasideS
9d420 6c 6f 74 20 2a 70 42 75 66 20 3d 20 28 4c 6f 6f lot *pBuf = (Loo
9d430 6b 61 73 69 64 65 53 6c 6f 74 2a 29 70 3b 0a 20 kasideSlot*)p;.
9d440 20 20 20 70 42 75 66 2d 3e 70 4e 65 78 74 20 3d pBuf->pNext =
9d450 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 db->lookaside.p
9d460 46 72 65 65 3b 0a 20 20 20 20 64 62 2d 3e 6c 6f Free;. db->lo
9d470 6f 6b 61 73 69 64 65 2e 70 46 72 65 65 20 3d 20 okaside.pFree =
9d480 70 42 75 66 3b 0a 20 20 20 20 64 62 2d 3e 6c 6f pBuf;. db->lo
9d490 6f 6b 61 73 69 64 65 2e 6e 4f 75 74 2d 2d 3b 0a okaside.nOut--;.
9d4a0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c }else{. sql
9d4b0 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 ite3_free(p);.
9d4c0 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 }.}../*.** Chang
9d4d0 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 6e e the size of an
9d4e0 20 65 78 69 73 74 69 6e 67 20 6d 65 6d 6f 72 79 existing memory
9d4f0 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2f 0a 53 allocation.*/.S
9d500 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
9d510 69 64 20 2a 73 71 6c 69 74 65 33 52 65 61 6c 6c id *sqlite3Reall
9d520 6f 63 28 76 6f 69 64 20 2a 70 4f 6c 64 2c 20 69 oc(void *pOld, i
9d530 6e 74 20 6e 42 79 74 65 73 29 7b 0a 20 20 69 6e nt nBytes){. in
9d540 74 20 6e 4f 6c 64 2c 20 6e 4e 65 77 3b 0a 20 20 t nOld, nNew;.
9d550 76 6f 69 64 20 2a 70 4e 65 77 3b 0a 20 20 69 66 void *pNew;. if
9d560 28 20 70 4f 6c 64 3d 3d 30 20 29 7b 0a 20 20 20 ( pOld==0 ){.
9d570 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 4d return sqlite3M
9d580 61 6c 6c 6f 63 28 6e 42 79 74 65 73 29 3b 0a 20 alloc(nBytes);.
9d590 20 7d 0a 20 20 69 66 28 20 6e 42 79 74 65 73 3c }. if( nBytes<
9d5a0 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 =0 ){. sqlite
9d5b0 33 5f 66 72 65 65 28 70 4f 6c 64 29 3b 0a 20 20 3_free(pOld);.
9d5c0 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a return 0;. }.
9d5d0 20 20 69 66 28 20 6e 42 79 74 65 73 3e 3d 30 78 if( nBytes>=0x
9d5e0 37 66 66 66 66 66 30 30 20 29 7b 0a 20 20 20 20 7fffff00 ){.
9d5f0 2f 2a 20 54 68 65 20 30 78 37 66 66 66 66 30 30 /* The 0x7ffff00
9d600 20 6c 69 6d 69 74 20 74 65 72 6d 20 69 73 20 65 limit term is e
9d610 78 70 6c 61 69 6e 65 64 20 69 6e 20 63 6f 6d 6d xplained in comm
9d620 65 6e 74 73 20 6f 6e 20 73 71 6c 69 74 65 33 4d ents on sqlite3M
9d630 61 6c 6c 6f 63 28 29 20 2a 2f 0a 20 20 20 20 72 alloc() */. r
9d640 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e eturn 0;. }. n
9d650 4f 6c 64 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c Old = sqlite3Mal
9d660 6c 6f 63 53 69 7a 65 28 70 4f 6c 64 29 3b 0a 20 locSize(pOld);.
9d670 20 6e 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 47 nNew = sqlite3G
9d680 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 52 lobalConfig.m.xR
9d690 6f 75 6e 64 75 70 28 6e 42 79 74 65 73 29 3b 0a oundup(nBytes);.
9d6a0 20 20 69 66 28 20 6e 4f 6c 64 3d 3d 6e 4e 65 77 if( nOld==nNew
9d6b0 20 29 7b 0a 20 20 20 20 70 4e 65 77 20 3d 20 70 ){. pNew = p
9d6c0 4f 6c 64 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 Old;. }else if(
9d6d0 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
9d6e0 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74 20 29 7b nfig.bMemstat ){
9d6f0 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 . sqlite3_mut
9d700 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 ex_enter(mem0.mu
9d710 74 65 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 tex);. sqlite
9d720 33 53 74 61 74 75 73 53 65 74 28 53 51 4c 49 54 3StatusSet(SQLIT
9d730 45 5f 53 54 41 54 55 53 5f 4d 41 4c 4c 4f 43 5f E_STATUS_MALLOC_
9d740 53 49 5a 45 2c 20 6e 42 79 74 65 73 29 3b 0a 20 SIZE, nBytes);.
9d750 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 if( sqlite3St
9d760 61 74 75 73 56 61 6c 75 65 28 53 51 4c 49 54 45 atusValue(SQLITE
9d770 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 _STATUS_MEMORY_U
9d780 53 45 44 29 2b 6e 4e 65 77 2d 6e 4f 6c 64 20 3e SED)+nNew-nOld >
9d790 3d 20 0a 20 20 20 20 20 20 20 20 20 20 6d 65 6d = . mem
9d7a0 30 2e 61 6c 61 72 6d 54 68 72 65 73 68 6f 6c 64 0.alarmThreshold
9d7b0 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
9d7c0 33 4d 61 6c 6c 6f 63 41 6c 61 72 6d 28 6e 4e 65 3MallocAlarm(nNe
9d7d0 77 2d 6e 4f 6c 64 29 3b 0a 20 20 20 20 7d 0a 20 w-nOld);. }.
9d7e0 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 pNew = sqlite
9d7f0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 3GlobalConfig.m.
9d800 78 52 65 61 6c 6c 6f 63 28 70 4f 6c 64 2c 20 6e xRealloc(pOld, n
9d810 4e 65 77 29 3b 0a 20 20 20 20 69 66 28 20 70 4e New);. if( pN
9d820 65 77 3d 3d 30 20 26 26 20 6d 65 6d 30 2e 61 6c ew==0 && mem0.al
9d830 61 72 6d 43 61 6c 6c 62 61 63 6b 20 29 7b 0a 20 armCallback ){.
9d840 20 20 20 20 20 73 71 6c 69 74 65 33 4d 61 6c 6c sqlite3Mall
9d850 6f 63 41 6c 61 72 6d 28 6e 42 79 74 65 73 29 3b ocAlarm(nBytes);
9d860 0a 20 20 20 20 20 20 70 4e 65 77 20 3d 20 73 71 . pNew = sq
9d870 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
9d880 67 2e 6d 2e 78 52 65 61 6c 6c 6f 63 28 70 4f 6c g.m.xRealloc(pOl
9d890 64 2c 20 6e 4e 65 77 29 3b 0a 20 20 20 20 7d 0a d, nNew);. }.
9d8a0 20 20 20 20 69 66 28 20 70 4e 65 77 20 29 7b 0a if( pNew ){.
9d8b0 20 20 20 20 20 20 6e 4e 65 77 20 3d 20 73 71 6c nNew = sql
9d8c0 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 ite3MallocSize(p
9d8d0 4e 65 77 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 New);. sqli
9d8e0 74 65 33 53 74 61 74 75 73 41 64 64 28 53 51 4c te3StatusAdd(SQL
9d8f0 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 ITE_STATUS_MEMOR
9d900 59 5f 55 53 45 44 2c 20 6e 4e 65 77 2d 6e 4f 6c Y_USED, nNew-nOl
9d910 64 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 d);. }. sq
9d920 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
9d930 65 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 e(mem0.mutex);.
9d940 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4e 65 77 }else{. pNew
9d950 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c = sqlite3Global
9d960 43 6f 6e 66 69 67 2e 6d 2e 78 52 65 61 6c 6c 6f Config.m.xReallo
9d970 63 28 70 4f 6c 64 2c 20 6e 4e 65 77 29 3b 0a 20 c(pOld, nNew);.
9d980 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 }. return pNew
9d990 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 ;.}../*.** The p
9d9a0 75 62 6c 69 63 20 69 6e 74 65 72 66 61 63 65 20 ublic interface
9d9b0 74 6f 20 73 71 6c 69 74 65 33 52 65 61 6c 6c 6f to sqlite3Reallo
9d9c0 63 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 74 68 c. Make sure th
9d9d0 61 74 20 74 68 65 20 6d 65 6d 6f 72 79 0a 2a 2a at the memory.**
9d9e0 20 73 75 62 73 79 73 74 65 6d 20 69 73 20 69 6e subsystem is in
9d9f0 69 74 69 61 6c 69 7a 65 64 20 70 72 69 6f 72 20 itialized prior
9da00 74 6f 20 69 6e 76 6f 6b 69 6e 67 20 73 71 6c 69 to invoking sqli
9da10 74 65 52 65 61 6c 6c 6f 63 2e 0a 2a 2f 0a 53 51 teRealloc..*/.SQ
9da20 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 LITE_API void *s
9da30 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 76 qlite3_realloc(v
9da40 6f 69 64 20 2a 70 4f 6c 64 2c 20 69 6e 74 20 6e oid *pOld, int n
9da50 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 ){.#ifndef SQLIT
9da60 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a E_OMIT_AUTOINIT.
9da70 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 69 6e if( sqlite3_in
9da80 69 74 69 61 6c 69 7a 65 28 29 20 29 20 72 65 74 itialize() ) ret
9da90 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 20 20 urn 0;.#endif.
9daa0 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 52 65 return sqlite3Re
9dab0 61 6c 6c 6f 63 28 70 4f 6c 64 2c 20 6e 29 3b 0a alloc(pOld, n);.
9dac0 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 }.../*.** Alloca
9dad0 74 65 20 61 6e 64 20 7a 65 72 6f 20 6d 65 6d 6f te and zero memo
9dae0 72 79 2e 0a 2a 2f 20 0a 53 51 4c 49 54 45 5f 50 ry..*/ .SQLITE_P
9daf0 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c RIVATE void *sql
9db00 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 69 ite3MallocZero(i
9db10 6e 74 20 6e 29 7b 0a 20 20 76 6f 69 64 20 2a 70 nt n){. void *p
9db20 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 = sqlite3Malloc
9db30 28 6e 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a (n);. if( p ){.
9db40 20 20 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c memset(p, 0,
9db50 20 6e 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 n);. }. retur
9db60 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c n p;.}../*.** Al
9db70 6c 6f 63 61 74 65 20 61 6e 64 20 7a 65 72 6f 20 locate and zero
9db80 6d 65 6d 6f 72 79 2e 20 20 49 66 20 74 68 65 20 memory. If the
9db90 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 allocation fails
9dba0 2c 20 6d 61 6b 65 0a 2a 2a 20 74 68 65 20 6d 61 , make.** the ma
9dbb0 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 20 llocFailed flag
9dbc0 69 6e 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f in the connectio
9dbd0 6e 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51 n pointer..*/.SQ
9dbe0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
9dbf0 64 20 2a 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c d *sqlite3DbMall
9dc00 6f 63 5a 65 72 6f 28 73 71 6c 69 74 65 33 20 2a ocZero(sqlite3 *
9dc10 64 62 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 76 6f db, int n){. vo
9dc20 69 64 20 2a 70 20 3d 20 73 71 6c 69 74 65 33 44 id *p = sqlite3D
9dc30 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 6e bMallocRaw(db, n
9dc40 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 );. if( p ){.
9dc50 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 6e memset(p, 0, n
9dc60 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
9dc70 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f p;.}../*.** Allo
9dc80 63 61 74 65 20 61 6e 64 20 7a 65 72 6f 20 6d 65 cate and zero me
9dc90 6d 6f 72 79 2e 20 20 49 66 20 74 68 65 20 61 6c mory. If the al
9dca0 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c 20 location fails,
9dcb0 6d 61 6b 65 0a 2a 2a 20 74 68 65 20 6d 61 6c 6c make.** the mall
9dcc0 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 20 69 6e ocFailed flag in
9dcd0 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 the connection
9dce0 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 pointer..**.** I
9dcf0 66 20 64 62 21 3d 30 20 61 6e 64 20 64 62 2d 3e f db!=0 and db->
9dd00 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 69 73 20 mallocFailed is
9dd10 74 72 75 65 20 28 69 6e 64 69 63 61 74 69 6e 67 true (indicating
9dd20 20 61 20 70 72 69 6f 72 20 6d 61 6c 6c 6f 63 0a a prior malloc.
9dd30 2a 2a 20 66 61 69 6c 75 72 65 20 6f 6e 20 74 68 ** failure on th
9dd40 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 e same database
9dd50 63 6f 6e 6e 65 63 74 69 6f 6e 29 20 74 68 65 6e connection) then
9dd60 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 20 30 always return 0
9dd70 2e 0a 2a 2a 20 48 65 6e 63 65 20 66 6f 72 20 61 ..** Hence for a
9dd80 20 70 61 72 74 69 63 75 6c 61 72 20 64 61 74 61 particular data
9dd90 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c base connection,
9dda0 20 6f 6e 63 65 20 6d 61 6c 6c 6f 63 20 73 74 61 once malloc sta
9ddb0 72 74 73 0a 2a 2a 20 66 61 69 6c 69 6e 67 2c 20 rts.** failing,
9ddc0 69 74 20 66 61 69 6c 73 20 63 6f 6e 73 69 73 74 it fails consist
9ddd0 65 6e 74 6c 79 20 75 6e 74 69 6c 20 6d 61 6c 6c ently until mall
9dde0 6f 63 46 61 69 6c 65 64 20 69 73 20 72 65 73 65 ocFailed is rese
9ddf0 74 2e 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 6e t..** This is an
9de00 20 69 6d 70 6f 72 74 61 6e 74 20 61 73 73 75 6d important assum
9de10 70 74 69 6f 6e 2e 20 20 54 68 65 72 65 20 61 72 ption. There ar
9de20 65 20 6d 61 6e 79 20 70 6c 61 63 65 73 20 69 6e e many places in
9de30 20 74 68 65 0a 2a 2a 20 63 6f 64 65 20 74 68 61 the.** code tha
9de40 74 20 64 6f 20 74 68 69 6e 67 73 20 6c 69 6b 65 t do things like
9de50 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 this:.**.**
9de60 20 20 20 20 20 69 6e 74 20 2a 61 20 3d 20 28 69 int *a = (i
9de70 6e 74 2a 29 73 71 6c 69 74 65 33 44 62 4d 61 6c nt*)sqlite3DbMal
9de80 6c 6f 63 52 61 77 28 64 62 2c 20 31 30 30 29 3b locRaw(db, 100);
9de90 0a 2a 2a 20 20 20 20 20 20 20 20 20 69 6e 74 20 .** int
9dea0 2a 62 20 3d 20 28 69 6e 74 2a 29 73 71 6c 69 74 *b = (int*)sqlit
9deb0 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 e3DbMallocRaw(db
9dec0 2c 20 32 30 30 29 3b 0a 2a 2a 20 20 20 20 20 20 , 200);.**
9ded0 20 20 20 69 66 28 20 62 20 29 20 61 5b 31 30 5d if( b ) a[10]
9dee0 20 3d 20 39 3b 0a 2a 2a 0a 2a 2a 20 49 6e 20 6f = 9;.**.** In o
9def0 74 68 65 72 20 77 6f 72 64 73 2c 20 69 66 20 61 ther words, if a
9df00 20 73 75 62 73 65 71 75 65 6e 74 20 6d 61 6c 6c subsequent mall
9df10 6f 63 20 28 65 78 3a 20 22 62 22 29 20 77 6f 72 oc (ex: "b") wor
9df20 6b 65 64 2c 20 69 74 20 69 73 20 61 73 73 75 6d ked, it is assum
9df30 65 64 0a 2a 2a 20 74 68 61 74 20 61 6c 6c 20 70 ed.** that all p
9df40 72 69 6f 72 20 6d 61 6c 6c 6f 63 73 20 28 65 78 rior mallocs (ex
9df50 3a 20 22 61 22 29 20 77 6f 72 6b 65 64 20 74 6f : "a") worked to
9df60 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 o..*/.SQLITE_PRI
9df70 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 VATE void *sqlit
9df80 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 73 71 e3DbMallocRaw(sq
9df90 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 6e lite3 *db, int n
9dfa0 29 7b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a 20 20 ){. void *p;.
9dfb0 61 73 73 65 72 74 28 20 64 62 3d 3d 30 20 7c 7c assert( db==0 ||
9dfc0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
9dfd0 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 eld(db->mutex) )
9dfe0 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ;.#ifndef SQLITE
9dff0 5f 4f 4d 49 54 5f 4c 4f 4f 4b 41 53 49 44 45 0a _OMIT_LOOKASIDE.
9e000 20 20 69 66 28 20 64 62 20 29 7b 0a 20 20 20 20 if( db ){.
9e010 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 20 2a 70 LookasideSlot *p
9e020 42 75 66 3b 0a 20 20 20 20 69 66 28 20 64 62 2d Buf;. if( db-
9e030 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b >mallocFailed ){
9e040 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b . return 0;
9e050 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 64 . }. if( d
9e060 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 45 6e b->lookaside.bEn
9e070 61 62 6c 65 64 20 26 26 20 6e 3c 3d 64 62 2d 3e abled && n<=db->
9e080 6c 6f 6f 6b 61 73 69 64 65 2e 73 7a 0a 20 20 20 lookaside.sz.
9e090 20 20 20 20 20 20 26 26 20 28 70 42 75 66 20 3d && (pBuf =
9e0a0 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 db->lookaside.p
9e0b0 46 72 65 65 29 21 3d 30 20 29 7b 0a 20 20 20 20 Free)!=0 ){.
9e0c0 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e db->lookaside.
9e0d0 70 46 72 65 65 20 3d 20 70 42 75 66 2d 3e 70 4e pFree = pBuf->pN
9e0e0 65 78 74 3b 0a 20 20 20 20 20 20 64 62 2d 3e 6c ext;. db->l
9e0f0 6f 6f 6b 61 73 69 64 65 2e 6e 4f 75 74 2b 2b 3b ookaside.nOut++;
9e100 0a 20 20 20 20 20 20 69 66 28 20 64 62 2d 3e 6c . if( db->l
9e110 6f 6f 6b 61 73 69 64 65 2e 6e 4f 75 74 3e 64 62 ookaside.nOut>db
9e120 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6d 78 4f 75 ->lookaside.mxOu
9e130 74 20 29 7b 0a 20 20 20 20 20 20 20 20 64 62 2d t ){. db-
9e140 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6d 78 4f 75 74 >lookaside.mxOut
9e150 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 = db->lookaside
9e160 2e 6e 4f 75 74 3b 0a 20 20 20 20 20 20 7d 0a 20 .nOut;. }.
9e170 20 20 20 20 20 72 65 74 75 72 6e 20 28 76 6f 69 return (voi
9e180 64 2a 29 70 42 75 66 3b 0a 20 20 20 20 7d 0a 20 d*)pBuf;. }.
9e190 20 7d 0a 23 65 6c 73 65 0a 20 20 69 66 28 20 64 }.#else. if( d
9e1a0 62 20 26 26 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 b && db->mallocF
9e1b0 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 72 65 74 ailed ){. ret
9e1c0 75 72 6e 20 30 3b 0a 20 20 7d 0a 23 65 6e 64 69 urn 0;. }.#endi
9e1d0 66 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33 4d f. p = sqlite3M
9e1e0 61 6c 6c 6f 63 28 6e 29 3b 0a 20 20 69 66 28 20 alloc(n);. if(
9e1f0 21 70 20 26 26 20 64 62 20 29 7b 0a 20 20 20 20 !p && db ){.
9e200 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
9e210 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 = 1;. }. retu
9e220 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 rn p;.}../*.** R
9e230 65 73 69 7a 65 20 74 68 65 20 62 6c 6f 63 6b 20 esize the block
9e240 6f 66 20 6d 65 6d 6f 72 79 20 70 6f 69 6e 74 65 of memory pointe
9e250 64 20 74 6f 20 62 79 20 70 20 74 6f 20 6e 20 62 d to by p to n b
9e260 79 74 65 73 2e 20 49 66 20 74 68 65 0a 2a 2a 20 ytes. If the.**
9e270 72 65 73 69 7a 65 20 66 61 69 6c 73 2c 20 73 65 resize fails, se
9e280 74 20 74 68 65 20 6d 61 6c 6c 6f 63 46 61 69 6c t the mallocFail
9e290 65 64 20 66 6c 61 67 20 69 6e 20 74 68 65 20 63 ed flag in the c
9e2a0 6f 6e 6e 65 63 74 69 6f 6e 20 6f 62 6a 65 63 74 onnection object
9e2b0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
9e2c0 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 ATE void *sqlite
9e2d0 33 44 62 52 65 61 6c 6c 6f 63 28 73 71 6c 69 74 3DbRealloc(sqlit
9e2e0 65 33 20 2a 64 62 2c 20 76 6f 69 64 20 2a 70 2c e3 *db, void *p,
9e2f0 20 69 6e 74 20 6e 29 7b 0a 20 20 76 6f 69 64 20 int n){. void
9e300 2a 70 4e 65 77 20 3d 20 30 3b 0a 20 20 61 73 73 *pNew = 0;. ass
9e310 65 72 74 28 20 64 62 21 3d 30 20 29 3b 0a 20 20 ert( db!=0 );.
9e320 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
9e330 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d mutex_held(db->m
9e340 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 64 utex) );. if( d
9e350 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3d b->mallocFailed=
9e360 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 70 3d =0 ){. if( p=
9e370 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 =0 ){. retu
9e380 72 6e 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c rn sqlite3DbMall
9e390 6f 63 52 61 77 28 64 62 2c 20 6e 29 3b 0a 20 20 ocRaw(db, n);.
9e3a0 20 20 7d 0a 20 20 20 20 69 66 28 20 69 73 4c 6f }. if( isLo
9e3b0 6f 6b 61 73 69 64 65 28 64 62 2c 20 70 29 20 29 okaside(db, p) )
9e3c0 7b 0a 20 20 20 20 20 20 69 66 28 20 6e 3c 3d 64 {. if( n<=d
9e3d0 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 73 7a 20 b->lookaside.sz
9e3e0 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 ){. retur
9e3f0 6e 20 70 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 n p;. }.
9e400 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 pNew = sqlite
9e410 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 3DbMallocRaw(db,
9e420 20 6e 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 n);. if( p
9e430 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 20 20 6d New ){. m
9e440 65 6d 63 70 79 28 70 4e 65 77 2c 20 70 2c 20 64 emcpy(pNew, p, d
9e450 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 73 7a 29 b->lookaside.sz)
9e460 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 ;. sqlite
9e470 33 44 62 46 72 65 65 28 64 62 2c 20 70 29 3b 0a 3DbFree(db, p);.
9e480 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 }. }els
9e490 65 7b 0a 20 20 20 20 20 20 70 4e 65 77 20 3d 20 e{. pNew =
9e4a0 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 sqlite3_realloc(
9e4b0 70 2c 20 6e 29 3b 0a 20 20 20 20 20 20 69 66 28 p, n);. if(
9e4c0 20 21 70 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 !pNew ){.
9e4d0 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c db->mallocFail
9e4e0 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a ed = 1;. }.
9e4f0 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
9e500 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a rn pNew;.}../*.*
9e510 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 72 65 61 * Attempt to rea
9e520 6c 6c 6f 63 61 74 65 20 70 2e 20 20 49 66 20 74 llocate p. If t
9e530 68 65 20 72 65 61 6c 6c 6f 63 61 74 69 6f 6e 20 he reallocation
9e540 66 61 69 6c 73 2c 20 74 68 65 6e 20 66 72 65 65 fails, then free
9e550 20 70 0a 2a 2a 20 61 6e 64 20 73 65 74 20 74 68 p.** and set th
9e560 65 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66 e mallocFailed f
9e570 6c 61 67 20 69 6e 20 74 68 65 20 64 61 74 61 62 lag in the datab
9e580 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a ase connection..
9e590 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
9e5a0 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 44 E void *sqlite3D
9e5b0 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 73 bReallocOrFree(s
9e5c0 71 6c 69 74 65 33 20 2a 64 62 2c 20 76 6f 69 64 qlite3 *db, void
9e5d0 20 2a 70 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 76 *p, int n){. v
9e5e0 6f 69 64 20 2a 70 4e 65 77 3b 0a 20 20 70 4e 65 oid *pNew;. pNe
9e5f0 77 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61 w = sqlite3DbRea
9e600 6c 6c 6f 63 28 64 62 2c 20 70 2c 20 6e 29 3b 0a lloc(db, p, n);.
9e610 20 20 69 66 28 20 21 70 4e 65 77 20 29 7b 0a 20 if( !pNew ){.
9e620 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 sqlite3DbFree
9e630 28 64 62 2c 20 70 29 3b 0a 20 20 7d 0a 20 20 72 (db, p);. }. r
9e640 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f eturn pNew;.}../
9e650 2a 0a 2a 2a 20 4d 61 6b 65 20 61 20 63 6f 70 79 *.** Make a copy
9e660 20 6f 66 20 61 20 73 74 72 69 6e 67 20 69 6e 20 of a string in
9e670 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 memory obtained
9e680 66 72 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f from sqliteMallo
9e690 63 28 29 2e 20 54 68 65 73 65 20 0a 2a 2a 20 66 c(). These .** f
9e6a0 75 6e 63 74 69 6f 6e 73 20 63 61 6c 6c 20 73 71 unctions call sq
9e6b0 6c 69 74 65 33 4d 61 6c 6c 6f 63 52 61 77 28 29 lite3MallocRaw()
9e6c0 20 64 69 72 65 63 74 6c 79 20 69 6e 73 74 65 61 directly instea
9e6d0 64 20 6f 66 20 73 71 6c 69 74 65 4d 61 6c 6c 6f d of sqliteMallo
9e6e0 63 28 29 2e 20 54 68 69 73 0a 2a 2a 20 69 73 20 c(). This.** is
9e6f0 62 65 63 61 75 73 65 20 77 68 65 6e 20 6d 65 6d because when mem
9e700 6f 72 79 20 64 65 62 75 67 67 69 6e 67 20 69 73 ory debugging is
9e710 20 74 75 72 6e 65 64 20 6f 6e 2c 20 74 68 65 73 turned on, thes
9e720 65 20 74 77 6f 20 66 75 6e 63 74 69 6f 6e 73 20 e two functions
9e730 61 72 65 20 0a 2a 2a 20 63 61 6c 6c 65 64 20 76 are .** called v
9e740 69 61 20 6d 61 63 72 6f 73 20 74 68 61 74 20 72 ia macros that r
9e750 65 63 6f 72 64 20 74 68 65 20 63 75 72 72 65 6e ecord the curren
9e760 74 20 66 69 6c 65 20 61 6e 64 20 6c 69 6e 65 20 t file and line
9e770 6e 75 6d 62 65 72 20 69 6e 20 74 68 65 0a 2a 2a number in the.**
9e780 20 54 68 72 65 61 64 44 61 74 61 20 73 74 72 75 ThreadData stru
9e790 63 74 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 cture..*/.SQLITE
9e7a0 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 _PRIVATE char *s
9e7b0 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 73 qlite3DbStrDup(s
9e7c0 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 qlite3 *db, cons
9e7d0 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 63 68 t char *z){. ch
9e7e0 61 72 20 2a 7a 4e 65 77 3b 0a 20 20 73 69 7a 65 ar *zNew;. size
9e7f0 5f 74 20 6e 3b 0a 20 20 69 66 28 20 7a 3d 3d 30 _t n;. if( z==0
9e800 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 ){. return 0
9e810 3b 0a 20 20 7d 0a 20 20 6e 20 3d 20 73 71 6c 69 ;. }. n = sqli
9e820 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 29 20 2b te3Strlen30(z) +
9e830 20 31 3b 0a 20 20 61 73 73 65 72 74 28 20 28 6e 1;. assert( (n
9e840 26 30 78 37 66 66 66 66 66 66 66 29 3d 3d 6e 20 &0x7fffffff)==n
9e850 29 3b 0a 20 20 7a 4e 65 77 20 3d 20 73 71 6c 69 );. zNew = sqli
9e860 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 te3DbMallocRaw(d
9e870 62 2c 20 28 69 6e 74 29 6e 29 3b 0a 20 20 69 66 b, (int)n);. if
9e880 28 20 7a 4e 65 77 20 29 7b 0a 20 20 20 20 6d 65 ( zNew ){. me
9e890 6d 63 70 79 28 7a 4e 65 77 2c 20 7a 2c 20 6e 29 mcpy(zNew, z, n)
9e8a0 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a ;. }. return z
9e8b0 4e 65 77 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 New;.}.SQLITE_PR
9e8c0 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 IVATE char *sqli
9e8d0 74 65 33 44 62 53 74 72 4e 44 75 70 28 73 71 6c te3DbStrNDup(sql
9e8e0 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 ite3 *db, const
9e8f0 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 29 7b char *z, int n){
9e900 0a 20 20 63 68 61 72 20 2a 7a 4e 65 77 3b 0a 20 . char *zNew;.
9e910 20 69 66 28 20 7a 3d 3d 30 20 29 7b 0a 20 20 20 if( z==0 ){.
9e920 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 return 0;. }.
9e930 20 61 73 73 65 72 74 28 20 28 6e 26 30 78 37 66 assert( (n&0x7f
9e940 66 66 66 66 66 66 29 3d 3d 6e 20 29 3b 0a 20 20 ffffff)==n );.
9e950 7a 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 zNew = sqlite3Db
9e960 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 6e 2b MallocRaw(db, n+
9e970 31 29 3b 0a 20 20 69 66 28 20 7a 4e 65 77 20 29 1);. if( zNew )
9e980 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 7a 4e 65 {. memcpy(zNe
9e990 77 2c 20 7a 2c 20 6e 29 3b 0a 20 20 20 20 7a 4e w, z, n);. zN
9e9a0 65 77 5b 6e 5d 20 3d 20 30 3b 0a 20 20 7d 0a 20 ew[n] = 0;. }.
9e9b0 20 72 65 74 75 72 6e 20 7a 4e 65 77 3b 0a 7d 0a return zNew;.}.
9e9c0 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 ./*.** Create a
9e9d0 73 74 72 69 6e 67 20 66 72 6f 6d 20 74 68 65 20 string from the
9e9e0 7a 46 72 6f 6d 61 74 20 61 72 67 75 6d 65 6e 74 zFromat argument
9e9f0 20 61 6e 64 20 74 68 65 20 76 61 5f 6c 69 73 74 and the va_list
9ea00 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73 2e 0a 2a that follows..*
9ea10 2a 20 53 74 6f 72 65 20 74 68 65 20 73 74 72 69 * Store the stri
9ea20 6e 67 20 69 6e 20 6d 65 6d 6f 72 79 20 6f 62 74 ng in memory obt
9ea30 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 ained from sqlit
9ea40 65 4d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 6d 61 eMalloc() and ma
9ea50 6b 65 20 2a 70 7a 0a 2a 2a 20 70 6f 69 6e 74 20 ke *pz.** point
9ea60 74 6f 20 74 68 61 74 20 73 74 72 69 6e 67 2e 0a to that string..
9ea70 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
9ea80 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 65 E void sqlite3Se
9ea90 74 53 74 72 69 6e 67 28 63 68 61 72 20 2a 2a 70 tString(char **p
9eaa0 7a 2c 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 z, sqlite3 *db,
9eab0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 const char *zFor
9eac0 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f mat, ...){. va_
9ead0 6c 69 73 74 20 61 70 3b 0a 20 20 63 68 61 72 20 list ap;. char
9eae0 2a 7a 3b 0a 0a 20 20 76 61 5f 73 74 61 72 74 28 *z;.. va_start(
9eaf0 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 ap, zFormat);.
9eb00 7a 20 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 69 z = sqlite3VMPri
9eb10 6e 74 66 28 64 62 2c 20 7a 46 6f 72 6d 61 74 2c ntf(db, zFormat,
9eb20 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 ap);. va_end(a
9eb30 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 p);. sqlite3DbF
9eb40 72 65 65 28 64 62 2c 20 2a 70 7a 29 3b 0a 20 20 ree(db, *pz);.
9eb50 2a 70 7a 20 3d 20 7a 3b 0a 7d 0a 0a 0a 2f 2a 0a *pz = z;.}.../*.
9eb60 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
9eb70 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64 20 must be called
9eb80 62 65 66 6f 72 65 20 65 78 69 74 69 6e 67 20 61 before exiting a
9eb90 6e 79 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 20 ny API function
9eba0 28 69 2e 65 2e 20 0a 2a 2a 20 72 65 74 75 72 6e (i.e. .** return
9ebb0 69 6e 67 20 63 6f 6e 74 72 6f 6c 20 74 6f 20 74 ing control to t
9ebc0 68 65 20 75 73 65 72 29 20 74 68 61 74 20 68 61 he user) that ha
9ebd0 73 20 63 61 6c 6c 65 64 20 73 71 6c 69 74 65 33 s called sqlite3
9ebe0 5f 6d 61 6c 6c 6f 63 20 6f 72 0a 2a 2a 20 73 71 _malloc or.** sq
9ebf0 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 2e 0a 2a lite3_realloc..*
9ec00 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 65 *.** The returne
9ec10 64 20 76 61 6c 75 65 20 69 73 20 6e 6f 72 6d 61 d value is norma
9ec20 6c 6c 79 20 61 20 63 6f 70 79 20 6f 66 20 74 68 lly a copy of th
9ec30 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e e second argumen
9ec40 74 20 74 6f 20 74 68 69 73 0a 2a 2a 20 66 75 6e t to this.** fun
9ec50 63 74 69 6f 6e 2e 20 48 6f 77 65 76 65 72 2c 20 ction. However,
9ec60 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 if a malloc() fa
9ec70 69 6c 75 72 65 20 68 61 73 20 6f 63 63 75 72 72 ilure has occurr
9ec80 65 64 20 73 69 6e 63 65 20 74 68 65 20 70 72 65 ed since the pre
9ec90 76 69 6f 75 73 0a 2a 2a 20 69 6e 76 6f 63 61 74 vious.** invocat
9eca0 69 6f 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d ion SQLITE_NOMEM
9ecb0 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 6e 73 is returned ins
9ecc0 74 65 61 64 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 tead. .**.** If
9ecd0 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 the first argume
9ece0 6e 74 2c 20 64 62 2c 20 69 73 20 6e 6f 74 20 4e nt, db, is not N
9ecf0 55 4c 4c 20 61 6e 64 20 61 20 6d 61 6c 6c 6f 63 ULL and a malloc
9ed00 28 29 20 65 72 72 6f 72 20 68 61 73 20 6f 63 63 () error has occ
9ed10 75 72 72 65 64 2c 0a 2a 2a 20 74 68 65 6e 20 74 urred,.** then t
9ed20 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 65 72 he connection er
9ed30 72 6f 72 2d 63 6f 64 65 20 28 74 68 65 20 76 61 ror-code (the va
9ed40 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 lue returned by
9ed50 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 sqlite3_errcode(
9ed60 29 29 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 )).** is set to
9ed70 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 0a 2a 2f SQLITE_NOMEM..*/
9ed80 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
9ed90 69 6e 74 20 73 71 6c 69 74 65 33 41 70 69 45 78 int sqlite3ApiEx
9eda0 69 74 28 73 71 6c 69 74 65 33 2a 20 64 62 2c 20 it(sqlite3* db,
9edb0 69 6e 74 20 72 63 29 7b 0a 20 20 2f 2a 20 49 66 int rc){. /* If
9edc0 20 74 68 65 20 64 62 20 68 61 6e 64 6c 65 20 69 the db handle i
9edd0 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e s not NULL, then
9ede0 20 77 65 20 6d 75 73 74 20 68 6f 6c 64 20 74 68 we must hold th
9edf0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 6e e connection han
9ee00 64 6c 65 0a 20 20 2a 2a 20 6d 75 74 65 78 20 68 dle. ** mutex h
9ee10 65 72 65 2e 20 4f 74 68 65 72 77 69 73 65 20 74 ere. Otherwise t
9ee20 68 65 20 72 65 61 64 20 28 61 6e 64 20 70 6f 73 he read (and pos
9ee30 73 69 62 6c 65 20 77 72 69 74 65 29 20 6f 66 20 sible write) of
9ee40 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
9ee50 20 0a 20 20 2a 2a 20 69 73 20 75 6e 73 61 66 65 . ** is unsafe
9ee60 2c 20 61 73 20 69 73 20 74 68 65 20 63 61 6c 6c , as is the call
9ee70 20 74 6f 20 73 71 6c 69 74 65 33 45 72 72 6f 72 to sqlite3Error
9ee80 28 29 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 ().. */. asser
9ee90 74 28 20 21 64 62 20 7c 7c 20 73 71 6c 69 74 65 t( !db || sqlite
9eea0 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3_mutex_held(db-
9eeb0 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 >mutex) );. if(
9eec0 20 64 62 20 26 26 20 28 64 62 2d 3e 6d 61 6c 6c db && (db->mall
9eed0 6f 63 46 61 69 6c 65 64 20 7c 7c 20 72 63 3d 3d ocFailed || rc==
9eee0 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d SQLITE_IOERR_NOM
9eef0 45 4d 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 EM) ){. sqlit
9ef00 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 e3Error(db, SQLI
9ef10 54 45 5f 4e 4f 4d 45 4d 2c 20 30 29 3b 0a 20 20 TE_NOMEM, 0);.
9ef20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c db->mallocFail
9ef30 65 64 20 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d ed = 0;. rc =
9ef40 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
9ef50 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 20 26 }. return rc &
9ef60 20 28 64 62 20 3f 20 64 62 2d 3e 65 72 72 4d 61 (db ? db->errMa
9ef70 73 6b 20 3a 20 30 78 66 66 29 3b 0a 7d 0a 0a 2f sk : 0xff);.}../
9ef80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
9ef90 6e 64 20 6f 66 20 6d 61 6c 6c 6f 63 2e 63 20 2a nd of malloc.c *
9efa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9efb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9efc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
9efd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 ************** B
9efe0 65 67 69 6e 20 66 69 6c 65 20 70 72 69 6e 74 66 egin file printf
9eff0 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
9f000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9f010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
9f020 2a 0a 2a 2a 20 54 68 65 20 22 70 72 69 6e 74 66 *.** The "printf
9f030 22 20 63 6f 64 65 20 74 68 61 74 20 66 6f 6c 6c " code that foll
9f040 6f 77 73 20 64 61 74 65 73 20 66 72 6f 6d 20 74 ows dates from t
9f050 68 65 20 31 39 38 30 27 73 2e 20 20 49 74 20 69 he 1980's. It i
9f060 73 20 69 6e 0a 2a 2a 20 74 68 65 20 70 75 62 6c s in.** the publ
9f070 69 63 20 64 6f 6d 61 69 6e 2e 20 20 54 68 65 20 ic domain. The
9f080 6f 72 69 67 69 6e 61 6c 20 63 6f 6d 6d 65 6e 74 original comment
9f090 73 20 61 72 65 20 69 6e 63 6c 75 64 65 64 20 68 s are included h
9f0a0 65 72 65 20 66 6f 72 0a 2a 2a 20 63 6f 6d 70 6c ere for.** compl
9f0b0 65 74 65 6e 65 73 73 2e 20 20 54 68 65 79 20 61 eteness. They a
9f0c0 72 65 20 76 65 72 79 20 6f 75 74 2d 6f 66 2d 64 re very out-of-d
9f0d0 61 74 65 20 62 75 74 20 6d 69 67 68 74 20 62 65 ate but might be
9f0e0 20 75 73 65 66 75 6c 20 61 73 0a 2a 2a 20 61 6e useful as.** an
9f0f0 20 68 69 73 74 6f 72 69 63 61 6c 20 72 65 66 65 historical refe
9f100 72 65 6e 63 65 2e 20 20 4d 6f 73 74 20 6f 66 20 rence. Most of
9f110 74 68 65 20 22 65 6e 68 61 6e 63 65 6d 65 6e 74 the "enhancement
9f120 73 22 20 68 61 76 65 20 62 65 65 6e 20 62 61 63 s" have been bac
9f130 6b 65 64 0a 2a 2a 20 6f 75 74 20 73 6f 20 74 68 ked.** out so th
9f140 61 74 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 61 at the functiona
9f150 6c 69 74 79 20 69 73 20 6e 6f 77 20 74 68 65 20 lity is now the
9f160 73 61 6d 65 20 61 73 20 73 74 61 6e 64 61 72 64 same as standard
9f170 20 70 72 69 6e 74 66 28 29 2e 0a 2a 2a 0a 2a 2a printf()..**.**
9f180 20 24 49 64 3a 20 70 72 69 6e 74 66 2e 63 2c 76 $Id: printf.c,v
9f190 20 31 2e 31 30 34 20 32 30 30 39 2f 30 36 2f 30 1.104 2009/06/0
9f1a0 33 20 30 31 3a 32 34 3a 35 34 20 64 72 68 20 45 3 01:24:54 drh E
9f1b0 78 70 20 24 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a xp $.**.********
9f1c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9f1d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9f1e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9f1f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9f200 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c **.**.** The fol
9f210 6c 6f 77 69 6e 67 20 6d 6f 64 75 6c 65 73 20 69 lowing modules i
9f220 73 20 61 6e 20 65 6e 68 61 6e 63 65 64 20 72 65 s an enhanced re
9f230 70 6c 61 63 65 6d 65 6e 74 20 66 6f 72 20 74 68 placement for th
9f240 65 20 22 70 72 69 6e 74 66 22 20 73 75 62 72 6f e "printf" subro
9f250 75 74 69 6e 65 73 0a 2a 2a 20 66 6f 75 6e 64 20 utines.** found
9f260 69 6e 20 74 68 65 20 73 74 61 6e 64 61 72 64 20 in the standard
9f270 43 20 6c 69 62 72 61 72 79 2e 20 20 54 68 65 20 C library. The
9f280 66 6f 6c 6c 6f 77 69 6e 67 20 65 6e 68 61 6e 63 following enhanc
9f290 65 6d 65 6e 74 73 20 61 72 65 0a 2a 2a 20 73 75 ements are.** su
9f2a0 70 70 6f 72 74 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 pported:.**.**
9f2b0 20 20 20 20 2b 20 20 41 64 64 69 74 69 6f 6e 61 + Additiona
9f2c0 6c 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 54 68 l functions. Th
9f2d0 65 20 73 74 61 6e 64 61 72 64 20 73 65 74 20 6f e standard set o
9f2e0 66 20 22 70 72 69 6e 74 66 22 20 66 75 6e 63 74 f "printf" funct
9f2f0 69 6f 6e 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 ions.**
9f300 69 6e 63 6c 75 64 65 73 20 70 72 69 6e 74 66 2c includes printf,
9f310 20 66 70 72 69 6e 74 66 2c 20 73 70 72 69 6e 74 fprintf, sprint
9f320 66 2c 20 76 70 72 69 6e 74 66 2c 20 76 66 70 72 f, vprintf, vfpr
9f330 69 6e 74 66 2c 20 61 6e 64 0a 2a 2a 20 20 20 20 intf, and.**
9f340 20 20 20 20 20 76 73 70 72 69 6e 74 66 2e 20 20 vsprintf.
9f350 54 68 69 73 20 6d 6f 64 75 6c 65 20 61 64 64 73 This module adds
9f360 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a the following:.
9f370 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 **.**
9f380 2a 20 20 73 6e 70 72 69 6e 74 66 20 2d 2d 20 57 * snprintf -- W
9f390 6f 72 6b 73 20 6c 69 6b 65 20 73 70 72 69 6e 74 orks like sprint
9f3a0 66 2c 20 62 75 74 20 68 61 73 20 61 6e 20 65 78 f, but has an ex
9f3b0 74 72 61 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 tra argument.**
9f3c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9f3d0 20 20 20 20 20 20 20 20 20 77 68 69 63 68 20 69 which i
9f3e0 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 s the size of th
9f3f0 65 20 62 75 66 66 65 72 20 77 72 69 74 74 65 6e e buffer written
9f400 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 to..**.**
9f410 20 20 20 20 20 2a 20 20 6d 70 72 69 6e 74 66 20 * mprintf
9f420 2d 2d 20 20 53 69 6d 69 6c 61 72 20 74 6f 20 73 -- Similar to s
9f430 70 72 69 6e 74 66 2e 20 20 57 72 69 74 65 73 20 printf. Writes
9f440 6f 75 74 70 75 74 20 74 6f 20 6d 65 6d 6f 72 79 output to memory
9f450 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
9f460 20 20 20 20 20 20 20 20 20 20 20 20 20 6f 62 74 obt
9f470 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f ained from mallo
9f480 63 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 c..**.**
9f490 20 20 20 2a 20 20 78 70 72 69 6e 74 66 20 2d 2d * xprintf --
9f4a0 20 20 43 61 6c 6c 73 20 61 20 66 75 6e 63 74 69 Calls a functi
9f4b0 6f 6e 20 74 6f 20 64 69 73 70 6f 73 65 20 6f 66 on to dispose of
9f4c0 20 6f 75 74 70 75 74 2e 0a 2a 2a 0a 2a 2a 20 20 output..**.**
9f4d0 20 20 20 20 20 20 20 20 20 2a 20 20 6e 70 72 69 * npri
9f4e0 6e 74 66 20 2d 2d 20 20 4e 6f 20 6f 75 74 70 75 ntf -- No outpu
9f4f0 74 2c 20 62 75 74 20 72 65 74 75 72 6e 73 20 74 t, but returns t
9f500 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 he number of cha
9f510 72 61 63 74 65 72 73 0a 2a 2a 20 20 20 20 20 20 racters.**
9f520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9f530 20 20 20 20 74 68 61 74 20 77 6f 75 6c 64 20 68 that would h
9f540 61 76 65 20 62 65 65 6e 20 6f 75 74 70 75 74 20 ave been output
9f550 62 79 20 70 72 69 6e 74 66 2e 0a 2a 2a 0a 2a 2a by printf..**.**
9f560 20 20 20 20 20 20 20 20 20 20 20 2a 20 20 41 20 * A
9f570 76 2d 20 76 65 72 73 69 6f 6e 20 28 65 78 3a 20 v- version (ex:
9f580 76 73 6e 70 72 69 6e 74 66 29 20 6f 66 20 65 76 vsnprintf) of ev
9f590 65 72 79 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 ery function is
9f5a0 61 6c 73 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 also.**
9f5b0 20 20 20 20 20 73 75 70 70 6c 69 65 64 2e 0a 2a supplied..*
9f5c0 2a 0a 2a 2a 20 20 20 20 20 20 2b 20 20 41 20 66 *.** + A f
9f5d0 65 77 20 65 78 74 65 6e 73 69 6f 6e 73 20 74 6f ew extensions to
9f5e0 20 74 68 65 20 66 6f 72 6d 61 74 74 69 6e 67 20 the formatting
9f5f0 6e 6f 74 61 74 69 6f 6e 20 61 72 65 20 73 75 70 notation are sup
9f600 70 6f 72 74 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 ported:.**.**
9f610 20 20 20 20 20 20 20 20 2a 20 20 54 68 65 20 22 * The "
9f620 3d 22 20 66 6c 61 67 20 28 73 69 6d 69 6c 61 72 =" flag (similar
9f630 20 74 6f 20 22 2d 22 29 20 63 61 75 73 65 73 20 to "-") causes
9f640 74 68 65 20 6f 75 74 70 75 74 20 74 6f 20 62 65 the output to be
9f650 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
9f660 20 62 65 20 63 65 6e 74 65 72 65 64 20 69 6e 20 be centered in
9f670 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 6c the appropriatel
9f680 79 20 73 69 7a 65 64 20 66 69 65 6c 64 2e 0a 2a y sized field..*
9f690 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 2a *.** *
9f6a0 20 20 54 68 65 20 25 62 20 66 69 65 6c 64 20 6f The %b field o
9f6b0 75 74 70 75 74 73 20 61 6e 20 69 6e 74 65 67 65 utputs an intege
9f6c0 72 20 69 6e 20 62 69 6e 61 72 79 20 6e 6f 74 61 r in binary nota
9f6d0 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 tion..**.**
9f6e0 20 20 20 20 20 20 2a 20 20 54 68 65 20 25 63 20 * The %c
9f6f0 66 69 65 6c 64 20 6e 6f 77 20 61 63 63 65 70 74 field now accept
9f700 73 20 61 20 70 72 65 63 69 73 69 6f 6e 2e 20 20 s a precision.
9f710 54 68 65 20 63 68 61 72 61 63 74 65 72 20 6f 75 The character ou
9f720 74 70 75 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 tput.**
9f730 20 20 20 20 20 69 73 20 72 65 70 65 61 74 65 64 is repeated
9f740 20 62 79 20 74 68 65 20 6e 75 6d 62 65 72 20 6f by the number o
9f750 66 20 74 69 6d 65 73 20 74 68 65 20 70 72 65 63 f times the prec
9f760 69 73 69 6f 6e 20 73 70 65 63 69 66 69 65 73 2e ision specifies.
9f770 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 .**.**
9f780 20 2a 20 20 54 68 65 20 25 27 20 66 69 65 6c 64 * The %' field
9f790 20 77 6f 72 6b 73 20 6c 69 6b 65 20 25 63 2c 20 works like %c,
9f7a0 62 75 74 20 74 61 6b 65 73 20 61 73 20 69 74 73 but takes as its
9f7b0 20 63 68 61 72 61 63 74 65 72 20 74 68 65 0a 2a character the.*
9f7c0 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6e * n
9f7d0 65 78 74 20 63 68 61 72 61 63 74 65 72 20 6f 66 ext character of
9f7e0 20 74 68 65 20 66 6f 72 6d 61 74 20 73 74 72 69 the format stri
9f7f0 6e 67 2c 20 69 6e 73 74 65 61 64 20 6f 66 20 74 ng, instead of t
9f800 68 65 20 6e 65 78 74 0a 2a 2a 20 20 20 20 20 20 he next.**
9f810 20 20 20 20 20 20 20 20 61 72 67 75 6d 65 6e 74 argument
9f820 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 . For example,
9f830 20 70 72 69 6e 74 66 28 22 25 2e 37 38 27 2d 22 printf("%.78'-"
9f840 29 20 20 70 72 69 6e 74 73 20 37 38 20 6d 69 6e ) prints 78 min
9f850 75 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 us.**
9f860 20 20 20 73 69 67 6e 73 2c 20 74 68 65 20 73 61 signs, the sa
9f870 6d 65 20 61 73 20 20 70 72 69 6e 74 66 28 22 25 me as printf("%
9f880 2e 37 38 63 22 2c 27 2d 27 29 2e 0a 2a 2a 0a 2a .78c",'-')..**.*
9f890 2a 20 20 20 20 20 20 2b 20 20 57 68 65 6e 20 63 * + When c
9f8a0 6f 6d 70 69 6c 65 64 20 75 73 69 6e 67 20 47 43 ompiled using GC
9f8b0 43 20 6f 6e 20 61 20 53 50 41 52 43 2c 20 74 68 C on a SPARC, th
9f8c0 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 70 72 is version of pr
9f8d0 69 6e 74 66 20 69 73 0a 2a 2a 20 20 20 20 20 20 intf is.**
9f8e0 20 20 20 66 61 73 74 65 72 20 74 68 61 6e 20 74 faster than t
9f8f0 68 65 20 6c 69 62 72 61 72 79 20 70 72 69 6e 74 he library print
9f900 66 20 66 6f 72 20 53 55 4e 20 4f 53 20 34 2e 31 f for SUN OS 4.1
9f910 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 2b 20 20 ..**.** +
9f920 41 6c 6c 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 All functions ar
9f930 65 20 66 75 6c 6c 79 20 72 65 65 6e 74 72 61 6e e fully reentran
9f940 74 2e 0a 2a 2a 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 t..**.*/../*.**
9f950 43 6f 6e 76 65 72 73 69 6f 6e 20 74 79 70 65 73 Conversion types
9f960 20 66 61 6c 6c 20 69 6e 74 6f 20 76 61 72 69 6f fall into vario
9f970 75 73 20 63 61 74 65 67 6f 72 69 65 73 20 61 73 us categories as
9f980 20 64 65 66 69 6e 65 64 20 62 79 20 74 68 65 0a defined by the.
9f990 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 6e 75 ** following enu
9f9a0 6d 65 72 61 74 69 6f 6e 2e 0a 2a 2f 0a 23 64 65 meration..*/.#de
9f9b0 66 69 6e 65 20 65 74 52 41 44 49 58 20 20 20 20 fine etRADIX
9f9c0 20 20 20 31 20 2f 2a 20 49 6e 74 65 67 65 72 20 1 /* Integer
9f9d0 74 79 70 65 73 2e 20 20 25 64 2c 20 25 78 2c 20 types. %d, %x,
9f9e0 25 6f 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 %o, and so forth
9f9f0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 46 4c */.#define etFL
9fa00 4f 41 54 20 20 20 20 20 20 20 32 20 2f 2a 20 46 OAT 2 /* F
9fa10 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 2e 20 20 loating point.
9fa20 25 66 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 %f */.#define et
9fa30 45 58 50 20 20 20 20 20 20 20 20 20 33 20 2f 2a EXP 3 /*
9fa40 20 45 78 70 6f 6e 65 6e 74 69 6f 6e 61 6c 20 6e Exponentional n
9fa50 6f 74 61 74 69 6f 6e 2e 20 25 65 20 61 6e 64 20 otation. %e and
9fa60 25 45 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 %E */.#define et
9fa70 47 45 4e 45 52 49 43 20 20 20 20 20 34 20 2f 2a GENERIC 4 /*
9fa80 20 46 6c 6f 61 74 69 6e 67 20 6f 72 20 65 78 70 Floating or exp
9fa90 6f 6e 65 6e 74 69 61 6c 2c 20 64 65 70 65 6e 64 onential, depend
9faa0 69 6e 67 20 6f 6e 20 65 78 70 6f 6e 65 6e 74 2e ing on exponent.
9fab0 20 25 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 %g */.#define e
9fac0 74 53 49 5a 45 20 20 20 20 20 20 20 20 35 20 2f tSIZE 5 /
9fad0 2a 20 52 65 74 75 72 6e 20 6e 75 6d 62 65 72 20 * Return number
9fae0 6f 66 20 63 68 61 72 61 63 74 65 72 73 20 70 72 of characters pr
9faf0 6f 63 65 73 73 65 64 20 73 6f 20 66 61 72 2e 20 ocessed so far.
9fb00 25 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 %n */.#define et
9fb10 53 54 52 49 4e 47 20 20 20 20 20 20 36 20 2f 2a STRING 6 /*
9fb20 20 53 74 72 69 6e 67 73 2e 20 25 73 20 2a 2f 0a Strings. %s */.
9fb30 23 64 65 66 69 6e 65 20 65 74 44 59 4e 53 54 52 #define etDYNSTR
9fb40 49 4e 47 20 20 20 37 20 2f 2a 20 44 79 6e 61 6d ING 7 /* Dynam
9fb50 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 ically allocated
9fb60 20 73 74 72 69 6e 67 73 2e 20 25 7a 20 2a 2f 0a strings. %z */.
9fb70 23 64 65 66 69 6e 65 20 65 74 50 45 52 43 45 4e #define etPERCEN
9fb80 54 20 20 20 20 20 38 20 2f 2a 20 50 65 72 63 65 T 8 /* Perce
9fb90 6e 74 20 73 79 6d 62 6f 6c 2e 20 25 25 20 2a 2f nt symbol. %% */
9fba0 0a 23 64 65 66 69 6e 65 20 65 74 43 48 41 52 58 .#define etCHARX
9fbb0 20 20 20 20 20 20 20 39 20 2f 2a 20 43 68 61 72 9 /* Char
9fbc0 61 63 74 65 72 73 2e 20 25 63 20 2a 2f 0a 2f 2a acters. %c */./*
9fbd0 20 54 68 65 20 72 65 73 74 20 61 72 65 20 65 78 The rest are ex
9fbe0 74 65 6e 73 69 6f 6e 73 2c 20 6e 6f 74 20 6e 6f tensions, not no
9fbf0 72 6d 61 6c 6c 79 20 66 6f 75 6e 64 20 69 6e 20 rmally found in
9fc00 70 72 69 6e 74 66 28 29 20 2a 2f 0a 23 64 65 66 printf() */.#def
9fc10 69 6e 65 20 65 74 53 51 4c 45 53 43 41 50 45 20 ine etSQLESCAPE
9fc20 20 31 30 20 2f 2a 20 53 74 72 69 6e 67 73 20 77 10 /* Strings w
9fc30 69 74 68 20 27 5c 27 27 20 64 6f 75 62 6c 65 64 ith '\'' doubled
9fc40 2e 20 20 25 71 20 2a 2f 0a 23 64 65 66 69 6e 65 . %q */.#define
9fc50 20 65 74 53 51 4c 45 53 43 41 50 45 32 20 31 31 etSQLESCAPE2 11
9fc60 20 2f 2a 20 53 74 72 69 6e 67 73 20 77 69 74 68 /* Strings with
9fc70 20 27 5c 27 27 20 64 6f 75 62 6c 65 64 20 61 6e '\'' doubled an
9fc80 64 20 65 6e 63 6c 6f 73 65 64 20 69 6e 20 27 27 d enclosed in ''
9fc90 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
9fca0 20 20 20 20 20 20 20 20 20 20 20 20 4e 55 4c 4c NULL
9fcb0 20 70 6f 69 6e 74 65 72 73 20 72 65 70 6c 61 63 pointers replac
9fcc0 65 64 20 62 79 20 53 51 4c 20 4e 55 4c 4c 2e 20 ed by SQL NULL.
9fcd0 20 25 51 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 %Q */.#define e
9fce0 74 54 4f 4b 45 4e 20 20 20 20 20 20 31 32 20 2f tTOKEN 12 /
9fcf0 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 * a pointer to a
9fd00 20 54 6f 6b 65 6e 20 73 74 72 75 63 74 75 72 65 Token structure
9fd10 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 53 52 */.#define etSR
9fd20 43 4c 49 53 54 20 20 20 20 31 33 20 2f 2a 20 61 CLIST 13 /* a
9fd30 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 53 72 pointer to a Sr
9fd40 63 4c 69 73 74 20 2a 2f 0a 23 64 65 66 69 6e 65 cList */.#define
9fd50 20 65 74 50 4f 49 4e 54 45 52 20 20 20 20 31 34 etPOINTER 14
9fd60 20 2f 2a 20 54 68 65 20 25 70 20 63 6f 6e 76 65 /* The %p conve
9fd70 72 73 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 rsion */.#define
9fd80 20 65 74 53 51 4c 45 53 43 41 50 45 33 20 31 35 etSQLESCAPE3 15
9fd90 20 2f 2a 20 25 77 20 2d 3e 20 53 74 72 69 6e 67 /* %w -> String
9fda0 73 20 77 69 74 68 20 27 5c 22 27 20 64 6f 75 62 s with '\"' doub
9fdb0 6c 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 led */.#define e
9fdc0 74 4f 52 44 49 4e 41 4c 20 20 20 20 31 36 20 2f tORDINAL 16 /
9fdd0 2a 20 25 72 20 2d 3e 20 31 73 74 2c 20 32 6e 64 * %r -> 1st, 2nd
9fde0 2c 20 33 72 64 2c 20 34 74 68 2c 20 65 74 63 2e , 3rd, 4th, etc.
9fdf0 20 20 45 6e 67 6c 69 73 68 20 6f 6e 6c 79 20 2a English only *
9fe00 2f 0a 0a 23 64 65 66 69 6e 65 20 65 74 49 4e 56 /..#define etINV
9fe10 41 4c 49 44 20 20 20 20 20 30 20 2f 2a 20 41 6e ALID 0 /* An
9fe20 79 20 75 6e 72 65 63 6f 67 6e 69 7a 65 64 20 63 y unrecognized c
9fe30 6f 6e 76 65 72 73 69 6f 6e 20 74 79 70 65 20 2a onversion type *
9fe40 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 22 65 74 /.../*.** An "et
9fe50 42 79 74 65 22 20 69 73 20 61 6e 20 38 2d 62 69 Byte" is an 8-bi
9fe60 74 20 75 6e 73 69 67 6e 65 64 20 76 61 6c 75 65 t unsigned value
9fe70 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 75 6e 73 ..*/.typedef uns
9fe80 69 67 6e 65 64 20 63 68 61 72 20 65 74 42 79 74 igned char etByt
9fe90 65 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 62 e;../*.** Each b
9fea0 75 69 6c 74 69 6e 20 63 6f 6e 76 65 72 73 69 6f uiltin conversio
9feb0 6e 20 63 68 61 72 61 63 74 65 72 20 28 65 78 3a n character (ex:
9fec0 20 74 68 65 20 27 64 27 20 69 6e 20 22 25 64 22 the 'd' in "%d"
9fed0 29 20 69 73 20 64 65 73 63 72 69 62 65 64 0a 2a ) is described.*
9fee0 2a 20 62 79 20 61 6e 20 69 6e 73 74 61 6e 63 65 * by an instance
9fef0 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e of the followin
9ff00 67 20 73 74 72 75 63 74 75 72 65 0a 2a 2f 0a 74 g structure.*/.t
9ff10 79 70 65 64 65 66 20 73 74 72 75 63 74 20 65 74 ypedef struct et
9ff20 5f 69 6e 66 6f 20 7b 20 20 20 2f 2a 20 49 6e 66 _info { /* Inf
9ff30 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 65 ormation about e
9ff40 61 63 68 20 66 6f 72 6d 61 74 20 66 69 65 6c 64 ach format field
9ff50 20 2a 2f 0a 20 20 63 68 61 72 20 66 6d 74 74 79 */. char fmtty
9ff60 70 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f pe; /
9ff70 2a 20 54 68 65 20 66 6f 72 6d 61 74 20 66 69 65 * The format fie
9ff80 6c 64 20 63 6f 64 65 20 6c 65 74 74 65 72 20 2a ld code letter *
9ff90 2f 0a 20 20 65 74 42 79 74 65 20 62 61 73 65 3b /. etByte base;
9ffa0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
9ffb0 54 68 65 20 62 61 73 65 20 66 6f 72 20 72 61 64 The base for rad
9ffc0 69 78 20 63 6f 6e 76 65 72 73 69 6f 6e 20 2a 2f ix conversion */
9ffd0 0a 20 20 65 74 42 79 74 65 20 66 6c 61 67 73 3b . etByte flags;
9ffe0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f /* O
9fff0 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 46 4c ne or more of FL
a0000 41 47 5f 20 63 6f 6e 73 74 61 6e 74 73 20 62 65 AG_ constants be
a0010 6c 6f 77 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 low */. etByte
a0020 74 79 70 65 3b 20 20 20 20 20 20 20 20 20 20 20 type;
a0030 20 20 2f 2a 20 43 6f 6e 76 65 72 73 69 6f 6e 20 /* Conversion
a0040 70 61 72 61 64 69 67 6d 20 2a 2f 0a 20 20 65 74 paradigm */. et
a0050 42 79 74 65 20 63 68 61 72 73 65 74 3b 20 20 20 Byte charset;
a0060 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 /* Offset
a0070 20 69 6e 74 6f 20 61 44 69 67 69 74 73 5b 5d 20 into aDigits[]
a0080 6f 66 20 74 68 65 20 64 69 67 69 74 73 20 73 74 of the digits st
a0090 72 69 6e 67 20 2a 2f 0a 20 20 65 74 42 79 74 65 ring */. etByte
a00a0 20 70 72 65 66 69 78 3b 20 20 20 20 20 20 20 20 prefix;
a00b0 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74 /* Offset int
a00c0 6f 20 61 50 72 65 66 69 78 5b 5d 20 6f 66 20 74 o aPrefix[] of t
a00d0 68 65 20 70 72 65 66 69 78 20 73 74 72 69 6e 67 he prefix string
a00e0 20 2a 2f 0a 7d 20 65 74 5f 69 6e 66 6f 3b 0a 0a */.} et_info;..
a00f0 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 /*.** Allowed va
a0100 6c 75 65 73 20 66 6f 72 20 65 74 5f 69 6e 66 6f lues for et_info
a0110 2e 66 6c 61 67 73 0a 2a 2f 0a 23 64 65 66 69 6e .flags.*/.#defin
a0120 65 20 46 4c 41 47 5f 53 49 47 4e 45 44 20 20 31 e FLAG_SIGNED 1
a0130 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
a0140 74 68 65 20 76 61 6c 75 65 20 74 6f 20 63 6f 6e the value to con
a0150 76 65 72 74 20 69 73 20 73 69 67 6e 65 64 20 2a vert is signed *
a0160 2f 0a 23 64 65 66 69 6e 65 20 46 4c 41 47 5f 49 /.#define FLAG_I
a0170 4e 54 45 52 4e 20 20 32 20 20 20 20 20 2f 2a 20 NTERN 2 /*
a0180 54 72 75 65 20 69 66 20 66 6f 72 20 69 6e 74 65 True if for inte
a0190 72 6e 61 6c 20 75 73 65 20 6f 6e 6c 79 20 2a 2f rnal use only */
a01a0 0a 23 64 65 66 69 6e 65 20 46 4c 41 47 5f 53 54 .#define FLAG_ST
a01b0 52 49 4e 47 20 20 34 20 20 20 20 20 2f 2a 20 41 RING 4 /* A
a01c0 6c 6c 6f 77 20 69 6e 66 69 6e 69 74 79 20 70 72 llow infinity pr
a01d0 65 63 69 73 69 6f 6e 20 2a 2f 0a 0a 0a 2f 2a 0a ecision */.../*.
a01e0 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 ** The following
a01f0 20 74 61 62 6c 65 20 69 73 20 73 65 61 72 63 68 table is search
a0200 65 64 20 6c 69 6e 65 61 72 6c 79 2c 20 73 6f 20 ed linearly, so
a0210 69 74 20 69 73 20 67 6f 6f 64 20 74 6f 20 70 75 it is good to pu
a0220 74 20 74 68 65 0a 2a 2a 20 6d 6f 73 74 20 66 72 t the.** most fr
a0230 65 71 75 65 6e 74 6c 79 20 75 73 65 64 20 63 6f equently used co
a0240 6e 76 65 72 73 69 6f 6e 20 74 79 70 65 73 20 66 nversion types f
a0250 69 72 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 irst..*/.static
a0260 63 6f 6e 73 74 20 63 68 61 72 20 61 44 69 67 69 const char aDigi
a0270 74 73 5b 5d 20 3d 20 22 30 31 32 33 34 35 36 37 ts[] = "01234567
a0280 38 39 41 42 43 44 45 46 30 31 32 33 34 35 36 37 89ABCDEF01234567
a0290 38 39 61 62 63 64 65 66 22 3b 0a 73 74 61 74 69 89abcdef";.stati
a02a0 63 20 63 6f 6e 73 74 20 63 68 61 72 20 61 50 72 c const char aPr
a02b0 65 66 69 78 5b 5d 20 3d 20 22 2d 78 30 5c 30 30 efix[] = "-x0\00
a02c0 30 58 30 22 3b 0a 73 74 61 74 69 63 20 63 6f 6e 0X0";.static con
a02d0 73 74 20 65 74 5f 69 6e 66 6f 20 66 6d 74 69 6e st et_info fmtin
a02e0 66 6f 5b 5d 20 3d 20 7b 0a 20 20 7b 20 20 27 64 fo[] = {. { 'd
a02f0 27 2c 20 31 30 2c 20 31 2c 20 65 74 52 41 44 49 ', 10, 1, etRADI
a0300 58 2c 20 20 20 20 20 20 30 2c 20 20 30 20 7d 2c X, 0, 0 },
a0310 0a 20 20 7b 20 20 27 73 27 2c 20 20 30 2c 20 34 . { 's', 0, 4
a0320 2c 20 65 74 53 54 52 49 4e 47 2c 20 20 20 20 20 , etSTRING,
a0330 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 67 0, 0 },. { 'g
a0340 27 2c 20 20 30 2c 20 31 2c 20 65 74 47 45 4e 45 ', 0, 1, etGENE
a0350 52 49 43 2c 20 20 20 20 33 30 2c 20 30 20 7d 2c RIC, 30, 0 },
a0360 0a 20 20 7b 20 20 27 7a 27 2c 20 20 30 2c 20 34 . { 'z', 0, 4
a0370 2c 20 65 74 44 59 4e 53 54 52 49 4e 47 2c 20 20 , etDYNSTRING,
a0380 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 71 0, 0 },. { 'q
a0390 27 2c 20 20 30 2c 20 34 2c 20 65 74 53 51 4c 45 ', 0, 4, etSQLE
a03a0 53 43 41 50 45 2c 20 20 30 2c 20 20 30 20 7d 2c SCAPE, 0, 0 },
a03b0 0a 20 20 7b 20 20 27 51 27 2c 20 20 30 2c 20 34 . { 'Q', 0, 4
a03c0 2c 20 65 74 53 51 4c 45 53 43 41 50 45 32 2c 20 , etSQLESCAPE2,
a03d0 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 77 0, 0 },. { 'w
a03e0 27 2c 20 20 30 2c 20 34 2c 20 65 74 53 51 4c 45 ', 0, 4, etSQLE
a03f0 53 43 41 50 45 33 2c 20 30 2c 20 20 30 20 7d 2c SCAPE3, 0, 0 },
a0400 0a 20 20 7b 20 20 27 63 27 2c 20 20 30 2c 20 30 . { 'c', 0, 0
a0410 2c 20 65 74 43 48 41 52 58 2c 20 20 20 20 20 20 , etCHARX,
a0420 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 6f 0, 0 },. { 'o
a0430 27 2c 20 20 38 2c 20 30 2c 20 65 74 52 41 44 49 ', 8, 0, etRADI
a0440 58 2c 20 20 20 20 20 20 30 2c 20 20 32 20 7d 2c X, 0, 2 },
a0450 0a 20 20 7b 20 20 27 75 27 2c 20 31 30 2c 20 30 . { 'u', 10, 0
a0460 2c 20 65 74 52 41 44 49 58 2c 20 20 20 20 20 20 , etRADIX,
a0470 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 78 0, 0 },. { 'x
a0480 27 2c 20 31 36 2c 20 30 2c 20 65 74 52 41 44 49 ', 16, 0, etRADI
a0490 58 2c 20 20 20 20 20 20 31 36 2c 20 31 20 7d 2c X, 16, 1 },
a04a0 0a 20 20 7b 20 20 27 58 27 2c 20 31 36 2c 20 30 . { 'X', 16, 0
a04b0 2c 20 65 74 52 41 44 49 58 2c 20 20 20 20 20 20 , etRADIX,
a04c0 30 2c 20 20 34 20 7d 2c 0a 23 69 66 6e 64 65 66 0, 4 },.#ifndef
a04d0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f SQLITE_OMIT_FLO
a04e0 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 7b 20 ATING_POINT. {
a04f0 20 27 66 27 2c 20 20 30 2c 20 31 2c 20 65 74 46 'f', 0, 1, etF
a0500 4c 4f 41 54 2c 20 20 20 20 20 20 30 2c 20 20 30 LOAT, 0, 0
a0510 20 7d 2c 0a 20 20 7b 20 20 27 65 27 2c 20 20 30 },. { 'e', 0
a0520 2c 20 31 2c 20 65 74 45 58 50 2c 20 20 20 20 20 , 1, etEXP,
a0530 20 20 20 33 30 2c 20 30 20 7d 2c 0a 20 20 7b 20 30, 0 },. {
a0540 20 27 45 27 2c 20 20 30 2c 20 31 2c 20 65 74 45 'E', 0, 1, etE
a0550 58 50 2c 20 20 20 20 20 20 20 20 31 34 2c 20 30 XP, 14, 0
a0560 20 7d 2c 0a 20 20 7b 20 20 27 47 27 2c 20 20 30 },. { 'G', 0
a0570 2c 20 31 2c 20 65 74 47 45 4e 45 52 49 43 2c 20 , 1, etGENERIC,
a0580 20 20 20 31 34 2c 20 30 20 7d 2c 0a 23 65 6e 64 14, 0 },.#end
a0590 69 66 0a 20 20 7b 20 20 27 69 27 2c 20 31 30 2c if. { 'i', 10,
a05a0 20 31 2c 20 65 74 52 41 44 49 58 2c 20 20 20 20 1, etRADIX,
a05b0 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 0, 0 },. {
a05c0 27 6e 27 2c 20 20 30 2c 20 30 2c 20 65 74 53 49 'n', 0, 0, etSI
a05d0 5a 45 2c 20 20 20 20 20 20 20 30 2c 20 20 30 20 ZE, 0, 0
a05e0 7d 2c 0a 20 20 7b 20 20 27 25 27 2c 20 20 30 2c },. { '%', 0,
a05f0 20 30 2c 20 65 74 50 45 52 43 45 4e 54 2c 20 20 0, etPERCENT,
a0600 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 0, 0 },. {
a0610 27 70 27 2c 20 31 36 2c 20 30 2c 20 65 74 50 4f 'p', 16, 0, etPO
a0620 49 4e 54 45 52 2c 20 20 20 20 30 2c 20 20 31 20 INTER, 0, 1
a0630 7d 2c 0a 0a 2f 2a 20 41 6c 6c 20 74 68 65 20 72 },../* All the r
a0640 65 73 74 20 68 61 76 65 20 74 68 65 20 46 4c 41 est have the FLA
a0650 47 5f 49 4e 54 45 52 4e 20 62 69 74 20 73 65 74 G_INTERN bit set
a0660 20 61 6e 64 20 61 72 65 20 74 68 75 73 20 66 6f and are thus fo
a0670 72 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75 73 r internal.** us
a0680 65 20 6f 6e 6c 79 20 2a 2f 0a 20 20 7b 20 20 27 e only */. { '
a0690 54 27 2c 20 20 30 2c 20 32 2c 20 65 74 54 4f 4b T', 0, 2, etTOK
a06a0 45 4e 2c 20 20 20 20 20 20 30 2c 20 20 30 20 7d EN, 0, 0 }
a06b0 2c 0a 20 20 7b 20 20 27 53 27 2c 20 20 30 2c 20 ,. { 'S', 0,
a06c0 32 2c 20 65 74 53 52 43 4c 49 53 54 2c 20 20 20 2, etSRCLIST,
a06d0 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 0, 0 },. { '
a06e0 72 27 2c 20 31 30 2c 20 33 2c 20 65 74 4f 52 44 r', 10, 3, etORD
a06f0 49 4e 41 4c 2c 20 20 20 20 30 2c 20 20 30 20 7d INAL, 0, 0 }
a0700 2c 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 53 ,.};../*.** If S
a0710 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 QLITE_OMIT_FLOAT
a0720 49 4e 47 5f 50 4f 49 4e 54 20 69 73 20 64 65 66 ING_POINT is def
a0730 69 6e 65 64 2c 20 74 68 65 6e 20 6e 6f 6e 65 20 ined, then none
a0740 6f 66 20 74 68 65 20 66 6c 6f 61 74 69 6e 67 20 of the floating
a0750 70 6f 69 6e 74 0a 2a 2a 20 63 6f 6e 76 65 72 73 point.** convers
a0760 69 6f 6e 73 20 77 69 6c 6c 20 77 6f 72 6b 2e 0a ions will work..
a0770 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 */.#ifndef SQLIT
a0780 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f E_OMIT_FLOATING_
a0790 50 4f 49 4e 54 0a 2f 2a 0a 2a 2a 20 22 2a 76 61 POINT./*.** "*va
a07a0 6c 22 20 69 73 20 61 20 64 6f 75 62 6c 65 20 73 l" is a double s
a07b0 75 63 68 20 74 68 61 74 20 30 2e 31 20 3c 3d 20 uch that 0.1 <=
a07c0 2a 76 61 6c 20 3c 20 31 30 2e 30 0a 2a 2a 20 52 *val < 10.0.** R
a07d0 65 74 75 72 6e 20 74 68 65 20 61 73 63 69 69 20 eturn the ascii
a07e0 63 6f 64 65 20 66 6f 72 20 74 68 65 20 6c 65 61 code for the lea
a07f0 64 69 6e 67 20 64 69 67 69 74 20 6f 66 20 2a 76 ding digit of *v
a0800 61 6c 2c 20 74 68 65 6e 0a 2a 2a 20 6d 75 6c 74 al, then.** mult
a0810 69 70 6c 79 20 22 2a 76 61 6c 22 20 62 79 20 31 iply "*val" by 1
a0820 30 2e 30 20 74 6f 20 72 65 6e 6f 72 6d 61 6c 69 0.0 to renormali
a0830 7a 65 2e 0a 2a 2a 0a 2a 2a 20 45 78 61 6d 70 6c ze..**.** Exampl
a0840 65 3a 0a 2a 2a 20 20 20 20 20 69 6e 70 75 74 3a e:.** input:
a0850 20 20 20 20 20 2a 76 61 6c 20 3d 20 33 2e 31 34 *val = 3.14
a0860 31 35 39 0a 2a 2a 20 20 20 20 20 6f 75 74 70 75 159.** outpu
a0870 74 3a 20 20 20 20 2a 76 61 6c 20 3d 20 31 2e 34 t: *val = 1.4
a0880 31 35 39 20 20 20 20 66 75 6e 63 74 69 6f 6e 20 159 function
a0890 72 65 74 75 72 6e 20 3d 20 27 33 27 0a 2a 2a 0a return = '3'.**.
a08a0 2a 2a 20 54 68 65 20 63 6f 75 6e 74 65 72 20 2a ** The counter *
a08b0 63 6e 74 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 cnt is increment
a08c0 65 64 20 65 61 63 68 20 74 69 6d 65 2e 20 20 41 ed each time. A
a08d0 66 74 65 72 20 63 6f 75 6e 74 65 72 20 65 78 63 fter counter exc
a08e0 65 65 64 73 0a 2a 2a 20 31 36 20 28 74 68 65 20 eeds.** 16 (the
a08f0 6e 75 6d 62 65 72 20 6f 66 20 73 69 67 6e 69 66 number of signif
a0900 69 63 61 6e 74 20 64 69 67 69 74 73 20 69 6e 20 icant digits in
a0910 61 20 36 34 2d 62 69 74 20 66 6c 6f 61 74 29 20 a 64-bit float)
a0920 27 30 27 20 69 73 0a 2a 2a 20 61 6c 77 61 79 73 '0' is.** always
a0930 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 returned..*/.st
a0940 61 74 69 63 20 63 68 61 72 20 65 74 5f 67 65 74 atic char et_get
a0950 64 69 67 69 74 28 4c 4f 4e 47 44 4f 55 42 4c 45 digit(LONGDOUBLE
a0960 5f 54 59 50 45 20 2a 76 61 6c 2c 20 69 6e 74 20 _TYPE *val, int
a0970 2a 63 6e 74 29 7b 0a 20 20 69 6e 74 20 64 69 67 *cnt){. int dig
a0980 69 74 3b 0a 20 20 4c 4f 4e 47 44 4f 55 42 4c 45 it;. LONGDOUBLE
a0990 5f 54 59 50 45 20 64 3b 0a 20 20 69 66 28 20 28 _TYPE d;. if( (
a09a0 2a 63 6e 74 29 2b 2b 20 3e 3d 20 31 36 20 29 20 *cnt)++ >= 16 )
a09b0 72 65 74 75 72 6e 20 27 30 27 3b 0a 20 20 64 69 return '0';. di
a09c0 67 69 74 20 3d 20 28 69 6e 74 29 2a 76 61 6c 3b git = (int)*val;
a09d0 0a 20 20 64 20 3d 20 64 69 67 69 74 3b 0a 20 20 . d = digit;.
a09e0 64 69 67 69 74 20 2b 3d 20 27 30 27 3b 0a 20 20 digit += '0';.
a09f0 2a 76 61 6c 20 3d 20 28 2a 76 61 6c 20 2d 20 64 *val = (*val - d
a0a00 29 2a 31 30 2e 30 3b 0a 20 20 72 65 74 75 72 6e )*10.0;. return
a0a10 20 28 63 68 61 72 29 64 69 67 69 74 3b 0a 7d 0a (char)digit;.}.
a0a20 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
a0a30 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 _OMIT_FLOATING_P
a0a40 4f 49 4e 54 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 OINT */../*.** A
a0a50 70 70 65 6e 64 20 4e 20 73 70 61 63 65 20 63 68 ppend N space ch
a0a60 61 72 61 63 74 65 72 73 20 74 6f 20 74 68 65 20 aracters to the
a0a70 67 69 76 65 6e 20 73 74 72 69 6e 67 20 62 75 66 given string buf
a0a80 66 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 fer..*/.static v
a0a90 6f 69 64 20 61 70 70 65 6e 64 53 70 61 63 65 28 oid appendSpace(
a0aa0 53 74 72 41 63 63 75 6d 20 2a 70 41 63 63 75 6d StrAccum *pAccum
a0ab0 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 73 74 61 74 , int N){. stat
a0ac0 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 7a 53 ic const char zS
a0ad0 70 61 63 65 73 5b 5d 20 3d 20 22 20 20 20 20 20 paces[] = "
a0ae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a0af0 20 20 20 20 20 20 20 20 22 3b 0a 20 20 77 68 69 ";. whi
a0b00 6c 65 28 20 4e 3e 3d 28 69 6e 74 29 73 69 7a 65 le( N>=(int)size
a0b10 6f 66 28 7a 53 70 61 63 65 73 29 2d 31 20 29 7b of(zSpaces)-1 ){
a0b20 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 . sqlite3StrA
a0b30 63 63 75 6d 41 70 70 65 6e 64 28 70 41 63 63 75 ccumAppend(pAccu
a0b40 6d 2c 20 7a 53 70 61 63 65 73 2c 20 73 69 7a 65 m, zSpaces, size
a0b50 6f 66 28 7a 53 70 61 63 65 73 29 2d 31 29 3b 0a of(zSpaces)-1);.
a0b60 20 20 20 20 4e 20 2d 3d 20 73 69 7a 65 6f 66 28 N -= sizeof(
a0b70 7a 53 70 61 63 65 73 29 2d 31 3b 0a 20 20 7d 0a zSpaces)-1;. }.
a0b80 20 20 69 66 28 20 4e 3e 30 20 29 7b 0a 20 20 20 if( N>0 ){.
a0b90 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d sqlite3StrAccum
a0ba0 41 70 70 65 6e 64 28 70 41 63 63 75 6d 2c 20 7a Append(pAccum, z
a0bb0 53 70 61 63 65 73 2c 20 4e 29 3b 0a 20 20 7d 0a Spaces, N);. }.
a0bc0 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 6e 20 6d 61 63 68 }../*.** On mach
a0bd0 69 6e 65 73 20 77 69 74 68 20 61 20 73 6d 61 6c ines with a smal
a0be0 6c 20 73 74 61 63 6b 20 73 69 7a 65 2c 20 79 6f l stack size, yo
a0bf0 75 20 63 61 6e 20 72 65 64 65 66 69 6e 65 20 74 u can redefine t
a0c00 68 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 50 52 49 he.** SQLITE_PRI
a0c10 4e 54 5f 42 55 46 5f 53 49 5a 45 20 74 6f 20 62 NT_BUF_SIZE to b
a0c20 65 20 6c 65 73 73 20 74 68 61 6e 20 33 35 30 2e e less than 350.
a0c30 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 .*/.#ifndef SQLI
a0c40 54 45 5f 50 52 49 4e 54 5f 42 55 46 5f 53 49 5a TE_PRINT_BUF_SIZ
a0c50 45 0a 23 20 69 66 20 64 65 66 69 6e 65 64 28 53 E.# if defined(S
a0c60 51 4c 49 54 45 5f 53 4d 41 4c 4c 5f 53 54 41 43 QLITE_SMALL_STAC
a0c70 4b 29 0a 23 20 20 20 64 65 66 69 6e 65 20 53 51 K).# define SQ
a0c80 4c 49 54 45 5f 50 52 49 4e 54 5f 42 55 46 5f 53 LITE_PRINT_BUF_S
a0c90 49 5a 45 20 35 30 0a 23 20 65 6c 73 65 0a 23 20 IZE 50.# else.#
a0ca0 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f define SQLITE_
a0cb0 50 52 49 4e 54 5f 42 55 46 5f 53 49 5a 45 20 33 PRINT_BUF_SIZE 3
a0cc0 35 30 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 50.# endif.#endi
a0cd0 66 0a 23 64 65 66 69 6e 65 20 65 74 42 55 46 53 f.#define etBUFS
a0ce0 49 5a 45 20 53 51 4c 49 54 45 5f 50 52 49 4e 54 IZE SQLITE_PRINT
a0cf0 5f 42 55 46 5f 53 49 5a 45 20 20 2f 2a 20 53 69 _BUF_SIZE /* Si
a0d00 7a 65 20 6f 66 20 74 68 65 20 6f 75 74 70 75 74 ze of the output
a0d10 20 62 75 66 66 65 72 20 2a 2f 0a 0a 2f 2a 0a 2a buffer */../*.*
a0d20 2a 20 54 68 65 20 72 6f 6f 74 20 70 72 6f 67 72 * The root progr
a0d30 61 6d 2e 20 20 41 6c 6c 20 76 61 72 69 61 74 69 am. All variati
a0d40 6f 6e 73 20 63 61 6c 6c 20 74 68 69 73 20 63 6f ons call this co
a0d50 72 65 2e 0a 2a 2a 0a 2a 2a 20 49 4e 50 55 54 53 re..**.** INPUTS
a0d60 3a 0a 2a 2a 20 20 20 66 75 6e 63 20 20 20 54 68 :.** func Th
a0d70 69 73 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 is is a pointer
a0d80 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 20 74 61 to a function ta
a0d90 6b 69 6e 67 20 74 68 72 65 65 20 61 72 67 75 6d king three argum
a0da0 65 6e 74 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 ents.**
a0db0 20 20 20 31 2e 20 41 20 70 6f 69 6e 74 65 72 20 1. A pointer
a0dc0 74 6f 20 61 6e 79 74 68 69 6e 67 2e 20 20 53 61 to anything. Sa
a0dd0 6d 65 20 61 73 20 74 68 65 20 22 61 72 67 22 20 me as the "arg"
a0de0 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 20 20 20 parameter..**
a0df0 20 20 20 20 20 20 20 20 20 32 2e 20 41 20 70 6f 2. A po
a0e00 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6c 69 73 inter to the lis
a0e10 74 20 6f 66 20 63 68 61 72 61 63 74 65 72 73 20 t of characters
a0e20 74 6f 20 62 65 20 6f 75 74 70 75 74 0a 2a 2a 20 to be output.**
a0e30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 4e (N
a0e40 6f 74 65 2c 20 74 68 69 73 20 6c 69 73 74 20 69 ote, this list i
a0e50 73 20 4e 4f 54 20 6e 75 6c 6c 20 74 65 72 6d 69 s NOT null termi
a0e60 6e 61 74 65 64 2e 29 0a 2a 2a 20 20 20 20 20 20 nated.).**
a0e70 20 20 20 20 20 20 33 2e 20 41 6e 20 69 6e 74 65 3. An inte
a0e80 67 65 72 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 ger number of ch
a0e90 61 72 61 63 74 65 72 73 20 74 6f 20 62 65 20 6f aracters to be o
a0ea0 75 74 70 75 74 2e 0a 2a 2a 20 20 20 20 20 20 20 utput..**
a0eb0 20 20 20 20 20 20 20 20 28 4e 6f 74 65 3a 20 54 (Note: T
a0ec0 68 69 73 20 6e 75 6d 62 65 72 20 6d 69 67 68 74 his number might
a0ed0 20 62 65 20 7a 65 72 6f 2e 29 0a 2a 2a 0a 2a 2a be zero.).**.**
a0ee0 20 20 20 61 72 67 20 20 20 20 54 68 69 73 20 69 arg This i
a0ef0 73 20 74 68 65 20 70 6f 69 6e 74 65 72 20 74 6f s the pointer to
a0f00 20 61 6e 79 74 68 69 6e 67 20 77 68 69 63 68 20 anything which
a0f10 77 69 6c 6c 20 62 65 20 70 61 73 73 65 64 20 61 will be passed a
a0f20 73 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 s the.**
a0f30 20 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 first argument
a0f40 20 74 6f 20 22 66 75 6e 63 22 2e 20 20 55 73 65 to "func". Use
a0f50 20 69 74 20 66 6f 72 20 77 68 61 74 65 76 65 72 it for whatever
a0f60 20 79 6f 75 20 6c 69 6b 65 2e 0a 2a 2a 0a 2a 2a you like..**.**
a0f70 20 20 20 66 6d 74 20 20 20 20 54 68 69 73 20 69 fmt This i
a0f80 73 20 74 68 65 20 66 6f 72 6d 61 74 20 73 74 72 s the format str
a0f90 69 6e 67 2c 20 61 73 20 69 6e 20 74 68 65 20 75 ing, as in the u
a0fa0 73 75 61 6c 20 70 72 69 6e 74 2e 0a 2a 2a 0a 2a sual print..**.*
a0fb0 2a 20 20 20 61 70 20 20 20 20 20 54 68 69 73 20 * ap This
a0fc0 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 is a pointer to
a0fd0 61 20 6c 69 73 74 20 6f 66 20 61 72 67 75 6d 65 a list of argume
a0fe0 6e 74 73 2e 20 20 53 61 6d 65 20 61 73 20 69 6e nts. Same as in
a0ff0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 76 66 70 .** vfp
a1000 72 69 6e 74 2e 0a 2a 2a 0a 2a 2a 20 4f 55 54 50 rint..**.** OUTP
a1010 55 54 53 3a 0a 2a 2a 20 20 20 20 20 20 20 20 20 UTS:.**
a1020 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 The return valu
a1030 65 20 69 73 20 74 68 65 20 74 6f 74 61 6c 20 6e e is the total n
a1040 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 umber of charact
a1050 65 72 73 20 73 65 6e 74 20 74 6f 0a 2a 2a 20 20 ers sent to.**
a1060 20 20 20 20 20 20 20 20 74 68 65 20 66 75 6e 63 the func
a1070 74 69 6f 6e 20 22 66 75 6e 63 22 2e 20 20 52 65 tion "func". Re
a1080 74 75 72 6e 73 20 2d 31 20 6f 6e 20 61 20 65 72 turns -1 on a er
a1090 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 ror..**.** Note
a10a0 74 68 61 74 20 74 68 65 20 6f 72 64 65 72 20 69 that the order i
a10b0 6e 20 77 68 69 63 68 20 61 75 74 6f 6d 61 74 69 n which automati
a10c0 63 20 76 61 72 69 61 62 6c 65 73 20 61 72 65 20 c variables are
a10d0 64 65 63 6c 61 72 65 64 20 62 65 6c 6f 77 0a 2a declared below.*
a10e0 2a 20 73 65 65 6d 73 20 74 6f 20 6d 61 6b 65 20 * seems to make
a10f0 61 20 62 69 67 20 64 69 66 66 65 72 65 6e 63 65 a big difference
a1100 20 69 6e 20 64 65 74 65 72 6d 69 6e 69 6e 67 20 in determining
a1110 68 6f 77 20 66 61 73 74 20 74 68 69 73 20 62 65 how fast this be
a1120 61 73 74 0a 2a 2a 20 77 69 6c 6c 20 72 75 6e 2e ast.** will run.
a1130 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
a1140 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
a1150 58 50 72 69 6e 74 66 28 0a 20 20 53 74 72 41 63 XPrintf(. StrAc
a1160 63 75 6d 20 2a 70 41 63 63 75 6d 2c 20 20 20 20 cum *pAccum,
a1170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
a1180 20 41 63 63 75 6d 75 6c 61 74 65 20 72 65 73 75 Accumulate resu
a1190 6c 74 73 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e lts here */. in
a11a0 74 20 75 73 65 45 78 74 65 6e 64 65 64 2c 20 20 t useExtended,
a11b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a11c0 20 2f 2a 20 41 6c 6c 6f 77 20 65 78 74 65 6e 64 /* Allow extend
a11d0 65 64 20 25 2d 63 6f 6e 76 65 72 73 69 6f 6e 73 ed %-conversions
a11e0 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 */. const char
a11f0 20 2a 66 6d 74 2c 20 20 20 20 20 20 20 20 20 20 *fmt,
a1200 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 6d /* Form
a1210 61 74 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20 76 at string */. v
a1220 61 5f 6c 69 73 74 20 61 70 20 20 20 20 20 20 20 a_list ap
a1230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a1240 20 20 2f 2a 20 61 72 67 75 6d 65 6e 74 73 20 2a /* arguments *
a1250 2f 0a 29 7b 0a 20 20 69 6e 74 20 63 3b 20 20 20 /.){. int c;
a1260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a1270 20 20 2f 2a 20 4e 65 78 74 20 63 68 61 72 61 63 /* Next charac
a1280 74 65 72 20 69 6e 20 74 68 65 20 66 6f 72 6d 61 ter in the forma
a1290 74 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20 63 68 t string */. ch
a12a0 61 72 20 2a 62 75 66 70 74 3b 20 20 20 20 20 20 ar *bufpt;
a12b0 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e /* Poin
a12c0 74 65 72 20 74 6f 20 74 68 65 20 63 6f 6e 76 65 ter to the conve
a12d0 72 73 69 6f 6e 20 62 75 66 66 65 72 20 2a 2f 0a rsion buffer */.
a12e0 20 20 69 6e 74 20 70 72 65 63 69 73 69 6f 6e 3b int precision;
a12f0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
a1300 50 72 65 63 69 73 69 6f 6e 20 6f 66 20 74 68 65 Precision of the
a1310 20 63 75 72 72 65 6e 74 20 66 69 65 6c 64 20 2a current field *
a1320 2f 0a 20 20 69 6e 74 20 6c 65 6e 67 74 68 3b 20 /. int length;
a1330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
a1340 2a 20 4c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 * Length of the
a1350 66 69 65 6c 64 20 2a 2f 0a 20 20 69 6e 74 20 69 field */. int i
a1360 64 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 dx;
a1370 20 20 20 20 20 20 2f 2a 20 41 20 67 65 6e 65 72 /* A gener
a1380 61 6c 20 70 75 72 70 6f 73 65 20 6c 6f 6f 70 20 al purpose loop
a1390 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 counter */. int
a13a0 20 77 69 64 74 68 3b 20 20 20 20 20 20 20 20 20 width;
a13b0 20 20 20 20 20 20 20 20 2f 2a 20 57 69 64 74 68 /* Width
a13c0 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 of the current
a13d0 66 69 65 6c 64 20 2a 2f 0a 20 20 65 74 42 79 74 field */. etByt
a13e0 65 20 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74 69 e flag_leftjusti
a13f0 66 79 3b 20 20 20 2f 2a 20 54 72 75 65 20 69 66 fy; /* True if
a1400 20 22 2d 22 20 66 6c 61 67 20 69 73 20 70 72 65 "-" flag is pre
a1410 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 sent */. etByte
a1420 20 66 6c 61 67 5f 70 6c 75 73 73 69 67 6e 3b 20 flag_plussign;
a1430 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
a1440 22 2b 22 20 66 6c 61 67 20 69 73 20 70 72 65 73 "+" flag is pres
a1450 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 ent */. etByte
a1460 66 6c 61 67 5f 62 6c 61 6e 6b 73 69 67 6e 3b 20 flag_blanksign;
a1470 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 22 /* True if "
a1480 20 22 20 66 6c 61 67 20 69 73 20 70 72 65 73 65 " flag is prese
a1490 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 nt */. etByte f
a14a0 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f 72 lag_alternatefor
a14b0 6d 3b 20 2f 2a 20 54 72 75 65 20 69 66 20 22 23 m; /* True if "#
a14c0 22 20 66 6c 61 67 20 69 73 20 70 72 65 73 65 6e " flag is presen
a14d0 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c t */. etByte fl
a14e0 61 67 5f 61 6c 74 66 6f 72 6d 32 3b 20 20 20 20 ag_altform2;
a14f0 20 20 2f 2a 20 54 72 75 65 20 69 66 20 22 21 22 /* True if "!"
a1500 20 66 6c 61 67 20 69 73 20 70 72 65 73 65 6e 74 flag is present
a1510 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c 61 */. etByte fla
a1520 67 5f 7a 65 72 6f 70 61 64 3b 20 20 20 20 20 20 g_zeropad;
a1530 20 2f 2a 20 54 72 75 65 20 69 66 20 66 69 65 6c /* True if fiel
a1540 64 20 77 69 64 74 68 20 63 6f 6e 73 74 61 6e 74 d width constant
a1550 20 73 74 61 72 74 73 20 77 69 74 68 20 7a 65 72 starts with zer
a1560 6f 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c o */. etByte fl
a1570 61 67 5f 6c 6f 6e 67 3b 20 20 20 20 20 20 20 20 ag_long;
a1580 20 20 2f 2a 20 54 72 75 65 20 69 66 20 22 6c 22 /* True if "l"
a1590 20 66 6c 61 67 20 69 73 20 70 72 65 73 65 6e 74 flag is present
a15a0 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c 61 */. etByte fla
a15b0 67 5f 6c 6f 6e 67 6c 6f 6e 67 3b 20 20 20 20 20 g_longlong;
a15c0 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 /* True if the
a15d0 22 6c 6c 22 20 66 6c 61 67 20 69 73 20 70 72 65 "ll" flag is pre
a15e0 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 sent */. etByte
a15f0 20 64 6f 6e 65 3b 20 20 20 20 20 20 20 20 20 20 done;
a1600 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 74 65 72 /* Loop ter
a1610 6d 69 6e 61 74 69 6f 6e 20 66 6c 61 67 20 2a 2f mination flag */
a1620 0a 20 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 . sqlite_uint64
a1630 20 6c 6f 6e 67 76 61 6c 75 65 3b 20 20 20 2f 2a longvalue; /*
a1640 20 56 61 6c 75 65 20 66 6f 72 20 69 6e 74 65 67 Value for integ
a1650 65 72 20 74 79 70 65 73 20 2a 2f 0a 20 20 4c 4f er types */. LO
a1660 4e 47 44 4f 55 42 4c 45 5f 54 59 50 45 20 72 65 NGDOUBLE_TYPE re
a1670 61 6c 76 61 6c 75 65 3b 20 2f 2a 20 56 61 6c 75 alvalue; /* Valu
a1680 65 20 66 6f 72 20 72 65 61 6c 20 74 79 70 65 73 e for real types
a1690 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 65 74 5f 69 */. const et_i
a16a0 6e 66 6f 20 2a 69 6e 66 6f 70 3b 20 20 20 20 20 nfo *infop;
a16b0 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 /* Pointer to t
a16c0 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 69 he appropriate i
a16d0 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 2a 2f nfo structure */
a16e0 0a 20 20 63 68 61 72 20 62 75 66 5b 65 74 42 55 . char buf[etBU
a16f0 46 53 49 5a 45 5d 3b 20 20 20 20 20 20 20 2f 2a FSIZE]; /*
a1700 20 43 6f 6e 76 65 72 73 69 6f 6e 20 62 75 66 66 Conversion buff
a1710 65 72 20 2a 2f 0a 20 20 63 68 61 72 20 70 72 65 er */. char pre
a1720 66 69 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 fix;
a1730 20 20 20 2f 2a 20 50 72 65 66 69 78 20 63 68 61 /* Prefix cha
a1740 72 61 63 74 65 72 2e 20 20 22 2b 22 20 6f 72 20 racter. "+" or
a1750 22 2d 22 20 6f 72 20 22 20 22 20 6f 72 20 27 5c "-" or " " or '\
a1760 30 27 2e 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 0'. */. etByte
a1770 78 74 79 70 65 20 3d 20 30 3b 20 20 20 20 20 20 xtype = 0;
a1780 20 20 20 20 2f 2a 20 43 6f 6e 76 65 72 73 69 6f /* Conversio
a1790 6e 20 70 61 72 61 64 69 67 6d 20 2a 2f 0a 20 20 n paradigm */.
a17a0 63 68 61 72 20 2a 7a 45 78 74 72 61 3b 20 20 20 char *zExtra;
a17b0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 /* Ex
a17c0 74 72 61 20 6d 65 6d 6f 72 79 20 75 73 65 64 20 tra memory used
a17d0 66 6f 72 20 65 74 54 43 4c 45 53 43 41 50 45 20 for etTCLESCAPE
a17e0 63 6f 6e 76 65 72 73 69 6f 6e 73 20 2a 2f 0a 23 conversions */.#
a17f0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
a1800 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e IT_FLOATING_POIN
a1810 54 0a 20 20 69 6e 74 20 20 65 78 70 2c 20 65 32 T. int exp, e2
a1820 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
a1830 2a 20 65 78 70 6f 6e 65 6e 74 20 6f 66 20 72 65 * exponent of re
a1840 61 6c 20 6e 75 6d 62 65 72 73 20 2a 2f 0a 20 20 al numbers */.
a1850 64 6f 75 62 6c 65 20 72 6f 75 6e 64 65 72 3b 20 double rounder;
a1860 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 /* Us
a1870 65 64 20 66 6f 72 20 72 6f 75 6e 64 69 6e 67 20 ed for rounding
a1880 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 floating point v
a1890 61 6c 75 65 73 20 2a 2f 0a 20 20 65 74 42 79 74 alues */. etByt
a18a0 65 20 66 6c 61 67 5f 64 70 3b 20 20 20 20 20 20 e flag_dp;
a18b0 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
a18c0 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20 73 decimal point s
a18d0 68 6f 75 6c 64 20 62 65 20 73 68 6f 77 6e 20 2a hould be shown *
a18e0 2f 0a 20 20 65 74 42 79 74 65 20 66 6c 61 67 5f /. etByte flag_
a18f0 72 74 7a 3b 20 20 20 20 20 20 20 20 20 20 20 2f rtz; /
a1900 2a 20 54 72 75 65 20 69 66 20 74 72 61 69 6c 69 * True if traili
a1910 6e 67 20 7a 65 72 6f 73 20 73 68 6f 75 6c 64 20 ng zeros should
a1920 62 65 20 72 65 6d 6f 76 65 64 20 2a 2f 0a 20 20 be removed */.
a1930 65 74 42 79 74 65 20 66 6c 61 67 5f 65 78 70 3b etByte flag_exp;
a1940 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 /* Tr
a1950 75 65 20 74 6f 20 66 6f 72 63 65 20 64 69 73 70 ue to force disp
a1960 6c 61 79 20 6f 66 20 74 68 65 20 65 78 70 6f 6e lay of the expon
a1970 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 73 64 ent */. int nsd
a1980 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
a1990 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
a19a0 20 73 69 67 6e 69 66 69 63 61 6e 74 20 64 69 67 significant dig
a19b0 69 74 73 20 72 65 74 75 72 6e 65 64 20 2a 2f 0a its returned */.
a19c0 23 65 6e 64 69 66 0a 0a 20 20 6c 65 6e 67 74 68 #endif.. length
a19d0 20 3d 20 30 3b 0a 20 20 62 75 66 70 74 20 3d 20 = 0;. bufpt =
a19e0 30 3b 0a 20 20 66 6f 72 28 3b 20 28 63 3d 28 2a 0;. for(; (c=(*
a19f0 66 6d 74 29 29 21 3d 30 3b 20 2b 2b 66 6d 74 29 fmt))!=0; ++fmt)
a1a00 7b 0a 20 20 20 20 69 66 28 20 63 21 3d 27 25 27 {. if( c!='%'
a1a10 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 61 6d ){. int am
a1a20 74 3b 0a 20 20 20 20 20 20 62 75 66 70 74 20 3d t;. bufpt =
a1a30 20 28 63 68 61 72 20 2a 29 66 6d 74 3b 0a 20 20 (char *)fmt;.
a1a40 20 20 20 20 61 6d 74 20 3d 20 31 3b 0a 20 20 20 amt = 1;.
a1a50 20 20 20 77 68 69 6c 65 28 20 28 63 3d 28 2a 2b while( (c=(*+
a1a60 2b 66 6d 74 29 29 21 3d 27 25 27 20 26 26 20 63 +fmt))!='%' && c
a1a70 21 3d 30 20 29 20 61 6d 74 2b 2b 3b 0a 20 20 20 !=0 ) amt++;.
a1a80 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 sqlite3StrAcc
a1a90 75 6d 41 70 70 65 6e 64 28 70 41 63 63 75 6d 2c umAppend(pAccum,
a1aa0 20 62 75 66 70 74 2c 20 61 6d 74 29 3b 0a 20 20 bufpt, amt);.
a1ab0 20 20 20 20 69 66 28 20 63 3d 3d 30 20 29 20 62 if( c==0 ) b
a1ac0 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
a1ad0 69 66 28 20 28 63 3d 28 2a 2b 2b 66 6d 74 29 29 if( (c=(*++fmt))
a1ae0 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c ==0 ){. sql
a1af0 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 ite3StrAccumAppe
a1b00 6e 64 28 70 41 63 63 75 6d 2c 20 22 25 22 2c 20 nd(pAccum, "%",
a1b10 31 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 1);. break;
a1b20 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 46 69 . }. /* Fi
a1b30 6e 64 20 6f 75 74 20 77 68 61 74 20 66 6c 61 67 nd out what flag
a1b40 73 20 61 72 65 20 70 72 65 73 65 6e 74 20 2a 2f s are present */
a1b50 0a 20 20 20 20 66 6c 61 67 5f 6c 65 66 74 6a 75 . flag_leftju
a1b60 73 74 69 66 79 20 3d 20 66 6c 61 67 5f 70 6c 75 stify = flag_plu
a1b70 73 73 69 67 6e 20 3d 20 66 6c 61 67 5f 62 6c 61 ssign = flag_bla
a1b80 6e 6b 73 69 67 6e 20 3d 20 0a 20 20 20 20 20 66 nksign = . f
a1b90 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f 72 lag_alternatefor
a1ba0 6d 20 3d 20 66 6c 61 67 5f 61 6c 74 66 6f 72 6d m = flag_altform
a1bb0 32 20 3d 20 66 6c 61 67 5f 7a 65 72 6f 70 61 64 2 = flag_zeropad
a1bc0 20 3d 20 30 3b 0a 20 20 20 20 64 6f 6e 65 20 3d = 0;. done =
a1bd0 20 30 3b 0a 20 20 20 20 64 6f 7b 0a 20 20 20 20 0;. do{.
a1be0 20 20 73 77 69 74 63 68 28 20 63 20 29 7b 0a 20 switch( c ){.
a1bf0 20 20 20 20 20 20 20 63 61 73 65 20 27 2d 27 3a case '-':
a1c00 20 20 20 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74 flag_leftjust
a1c10 69 66 79 20 3d 20 31 3b 20 20 20 20 20 62 72 65 ify = 1; bre
a1c20 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 ak;. case
a1c30 20 27 2b 27 3a 20 20 20 66 6c 61 67 5f 70 6c 75 '+': flag_plu
a1c40 73 73 69 67 6e 20 3d 20 31 3b 20 20 20 20 20 20 ssign = 1;
a1c50 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 break;.
a1c60 20 63 61 73 65 20 27 20 27 3a 20 20 20 66 6c 61 case ' ': fla
a1c70 67 5f 62 6c 61 6e 6b 73 69 67 6e 20 3d 20 31 3b g_blanksign = 1;
a1c80 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
a1c90 20 20 20 20 20 20 63 61 73 65 20 27 23 27 3a 20 case '#':
a1ca0 20 20 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65 flag_alternate
a1cb0 66 6f 72 6d 20 3d 20 31 3b 20 20 20 62 72 65 61 form = 1; brea
a1cc0 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 k;. case
a1cd0 27 21 27 3a 20 20 20 66 6c 61 67 5f 61 6c 74 66 '!': flag_altf
a1ce0 6f 72 6d 32 20 3d 20 31 3b 20 20 20 20 20 20 20 orm2 = 1;
a1cf0 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 break;.
a1d00 63 61 73 65 20 27 30 27 3a 20 20 20 66 6c 61 67 case '0': flag
a1d10 5f 7a 65 72 6f 70 61 64 20 3d 20 31 3b 20 20 20 _zeropad = 1;
a1d20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
a1d30 20 20 20 20 20 64 65 66 61 75 6c 74 3a 20 20 20 default:
a1d40 20 64 6f 6e 65 20 3d 20 31 3b 20 20 20 20 20 20 done = 1;
a1d50 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b break
a1d60 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 77 ;. }. }w
a1d70 68 69 6c 65 28 20 21 64 6f 6e 65 20 26 26 20 28 hile( !done && (
a1d80 63 3d 28 2a 2b 2b 66 6d 74 29 29 21 3d 30 20 29 c=(*++fmt))!=0 )
a1d90 3b 0a 20 20 20 20 2f 2a 20 47 65 74 20 74 68 65 ;. /* Get the
a1da0 20 66 69 65 6c 64 20 77 69 64 74 68 20 2a 2f 0a field width */.
a1db0 20 20 20 20 77 69 64 74 68 20 3d 20 30 3b 0a 20 width = 0;.
a1dc0 20 20 20 69 66 28 20 63 3d 3d 27 2a 27 20 29 7b if( c=='*' ){
a1dd0 0a 20 20 20 20 20 20 77 69 64 74 68 20 3d 20 76 . width = v
a1de0 61 5f 61 72 67 28 61 70 2c 69 6e 74 29 3b 0a 20 a_arg(ap,int);.
a1df0 20 20 20 20 20 69 66 28 20 77 69 64 74 68 3c 30 if( width<0
a1e00 20 29 7b 0a 20 20 20 20 20 20 20 20 66 6c 61 67 ){. flag
a1e10 5f 6c 65 66 74 6a 75 73 74 69 66 79 20 3d 20 31 _leftjustify = 1
a1e20 3b 0a 20 20 20 20 20 20 20 20 77 69 64 74 68 20 ;. width
a1e30 3d 20 2d 77 69 64 74 68 3b 0a 20 20 20 20 20 20 = -width;.
a1e40 7d 0a 20 20 20 20 20 20 63 20 3d 20 2a 2b 2b 66 }. c = *++f
a1e50 6d 74 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 mt;. }else{.
a1e60 20 20 20 20 20 77 68 69 6c 65 28 20 63 3e 3d 27 while( c>='
a1e70 30 27 20 26 26 20 63 3c 3d 27 39 27 20 29 7b 0a 0' && c<='9' ){.
a1e80 20 20 20 20 20 20 20 20 77 69 64 74 68 20 3d 20 width =
a1e90 77 69 64 74 68 2a 31 30 20 2b 20 63 20 2d 20 27 width*10 + c - '
a1ea0 30 27 3b 0a 20 20 20 20 20 20 20 20 63 20 3d 20 0';. c =
a1eb0 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20 20 20 7d 0a *++fmt;. }.
a1ec0 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 77 69 }. if( wi
a1ed0 64 74 68 20 3e 20 65 74 42 55 46 53 49 5a 45 2d dth > etBUFSIZE-
a1ee0 31 30 20 29 7b 0a 20 20 20 20 20 20 77 69 64 74 10 ){. widt
a1ef0 68 20 3d 20 65 74 42 55 46 53 49 5a 45 2d 31 30 h = etBUFSIZE-10
a1f00 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 47 ;. }. /* G
a1f10 65 74 20 74 68 65 20 70 72 65 63 69 73 69 6f 6e et the precision
a1f20 20 2a 2f 0a 20 20 20 20 69 66 28 20 63 3d 3d 27 */. if( c=='
a1f30 2e 27 20 29 7b 0a 20 20 20 20 20 20 70 72 65 63 .' ){. prec
a1f40 69 73 69 6f 6e 20 3d 20 30 3b 0a 20 20 20 20 20 ision = 0;.
a1f50 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20 c = *++fmt;.
a1f60 20 20 20 69 66 28 20 63 3d 3d 27 2a 27 20 29 7b if( c=='*' ){
a1f70 0a 20 20 20 20 20 20 20 20 70 72 65 63 69 73 69 . precisi
a1f80 6f 6e 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 69 on = va_arg(ap,i
a1f90 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 nt);. if(
a1fa0 20 70 72 65 63 69 73 69 6f 6e 3c 30 20 29 20 70 precision<0 ) p
a1fb0 72 65 63 69 73 69 6f 6e 20 3d 20 2d 70 72 65 63 recision = -prec
a1fc0 69 73 69 6f 6e 3b 0a 20 20 20 20 20 20 20 20 63 ision;. c
a1fd0 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20 20 = *++fmt;.
a1fe0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
a1ff0 77 68 69 6c 65 28 20 63 3e 3d 27 30 27 20 26 26 while( c>='0' &&
a2000 20 63 3c 3d 27 39 27 20 29 7b 0a 20 20 20 20 20 c<='9' ){.
a2010 20 20 20 20 20 70 72 65 63 69 73 69 6f 6e 20 3d precision =
a2020 20 70 72 65 63 69 73 69 6f 6e 2a 31 30 20 2b 20 precision*10 +
a2030 63 20 2d 20 27 30 27 3b 0a 20 20 20 20 20 20 20 c - '0';.
a2040 20 20 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 c = *++fmt;.
a2050 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
a2060 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
a2070 20 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 2d 31 precision = -1
a2080 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 47 ;. }. /* G
a2090 65 74 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f et the conversio
a20a0 6e 20 74 79 70 65 20 6d 6f 64 69 66 69 65 72 20 n type modifier
a20b0 2a 2f 0a 20 20 20 20 69 66 28 20 63 3d 3d 27 6c */. if( c=='l
a20c0 27 20 29 7b 0a 20 20 20 20 20 20 66 6c 61 67 5f ' ){. flag_
a20d0 6c 6f 6e 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 long = 1;.
a20e0 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20 c = *++fmt;.
a20f0 20 20 69 66 28 20 63 3d 3d 27 6c 27 20 29 7b 0a if( c=='l' ){.
a2100 20 20 20 20 20 20 20 20 66 6c 61 67 5f 6c 6f 6e flag_lon
a2110 67 6c 6f 6e 67 20 3d 20 31 3b 0a 20 20 20 20 20 glong = 1;.
a2120 20 20 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 c = *++fmt;.
a2130 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
a2140 20 20 20 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e flag_longlon
a2150 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 g = 0;. }.
a2160 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
a2170 66 6c 61 67 5f 6c 6f 6e 67 20 3d 20 66 6c 61 67 flag_long = flag
a2180 5f 6c 6f 6e 67 6c 6f 6e 67 20 3d 20 30 3b 0a 20 _longlong = 0;.
a2190 20 20 20 7d 0a 20 20 20 20 2f 2a 20 46 65 74 63 }. /* Fetc
a21a0 68 20 74 68 65 20 69 6e 66 6f 20 65 6e 74 72 79 h the info entry
a21b0 20 66 6f 72 20 74 68 65 20 66 69 65 6c 64 20 2a for the field *
a21c0 2f 0a 20 20 20 20 69 6e 66 6f 70 20 3d 20 26 66 /. infop = &f
a21d0 6d 74 69 6e 66 6f 5b 30 5d 3b 0a 20 20 20 20 78 mtinfo[0];. x
a21e0 74 79 70 65 20 3d 20 65 74 49 4e 56 41 4c 49 44 type = etINVALID
a21f0 3b 0a 20 20 20 20 66 6f 72 28 69 64 78 3d 30 3b ;. for(idx=0;
a2200 20 69 64 78 3c 41 72 72 61 79 53 69 7a 65 28 66 idx<ArraySize(f
a2210 6d 74 69 6e 66 6f 29 3b 20 69 64 78 2b 2b 29 7b mtinfo); idx++){
a2220 0a 20 20 20 20 20 20 69 66 28 20 63 3d 3d 66 6d . if( c==fm
a2230 74 69 6e 66 6f 5b 69 64 78 5d 2e 66 6d 74 74 79 tinfo[idx].fmtty
a2240 70 65 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e pe ){. in
a2250 66 6f 70 20 3d 20 26 66 6d 74 69 6e 66 6f 5b 69 fop = &fmtinfo[i
a2260 64 78 5d 3b 0a 20 20 20 20 20 20 20 20 69 66 28 dx];. if(
a2270 20 75 73 65 45 78 74 65 6e 64 65 64 20 7c 7c 20 useExtended ||
a2280 28 69 6e 66 6f 70 2d 3e 66 6c 61 67 73 20 26 20 (infop->flags &
a2290 46 4c 41 47 5f 49 4e 54 45 52 4e 29 3d 3d 30 20 FLAG_INTERN)==0
a22a0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 78 74 79 ){. xty
a22b0 70 65 20 3d 20 69 6e 66 6f 70 2d 3e 74 79 70 65 pe = infop->type
a22c0 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b ;. }else{
a22d0 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 . retur
a22e0 6e 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 n;. }.
a22f0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
a2300 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 7a 45 }. }. zE
a2310 78 74 72 61 20 3d 20 30 3b 0a 0a 0a 20 20 20 20 xtra = 0;...
a2320 2f 2a 20 4c 69 6d 69 74 20 74 68 65 20 70 72 65 /* Limit the pre
a2330 63 69 73 69 6f 6e 20 74 6f 20 70 72 65 76 65 6e cision to preven
a2340 74 20 6f 76 65 72 66 6c 6f 77 69 6e 67 20 62 75 t overflowing bu
a2350 66 5b 5d 20 64 75 72 69 6e 67 20 63 6f 6e 76 65 f[] during conve
a2360 72 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 69 66 28 rsion */. if(
a2370 20 70 72 65 63 69 73 69 6f 6e 3e 65 74 42 55 46 precision>etBUF
a2380 53 49 5a 45 2d 34 30 20 26 26 20 28 69 6e 66 6f SIZE-40 && (info
a2390 70 2d 3e 66 6c 61 67 73 20 26 20 46 4c 41 47 5f p->flags & FLAG_
a23a0 53 54 52 49 4e 47 29 3d 3d 30 20 29 7b 0a 20 20 STRING)==0 ){.
a23b0 20 20 20 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 precision =
a23c0 65 74 42 55 46 53 49 5a 45 2d 34 30 3b 0a 20 20 etBUFSIZE-40;.
a23d0 20 20 7d 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20 }.. /*.
a23e0 2a 2a 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74 ** At this point
a23f0 2c 20 76 61 72 69 61 62 6c 65 73 20 61 72 65 20 , variables are
a2400 69 6e 69 74 69 61 6c 69 7a 65 64 20 61 73 20 66 initialized as f
a2410 6f 6c 6c 6f 77 73 3a 0a 20 20 20 20 2a 2a 0a 20 ollows:. **.
a2420 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f 61 6c 74 ** flag_alt
a2430 65 72 6e 61 74 65 66 6f 72 6d 20 20 20 20 20 20 ernateform
a2440 20 20 20 20 54 52 55 45 20 69 66 20 61 20 27 23 TRUE if a '#
a2450 27 20 69 73 20 70 72 65 73 65 6e 74 2e 0a 20 20 ' is present..
a2460 20 20 2a 2a 20 20 20 66 6c 61 67 5f 61 6c 74 66 ** flag_altf
a2470 6f 72 6d 32 20 20 20 20 20 20 20 20 20 20 20 20 orm2
a2480 20 20 20 54 52 55 45 20 69 66 20 61 20 27 21 27 TRUE if a '!'
a2490 20 69 73 20 70 72 65 73 65 6e 74 2e 0a 20 20 20 is present..
a24a0 20 2a 2a 20 20 20 66 6c 61 67 5f 70 6c 75 73 73 ** flag_pluss
a24b0 69 67 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 ign
a24c0 20 20 54 52 55 45 20 69 66 20 61 20 27 2b 27 20 TRUE if a '+'
a24d0 69 73 20 70 72 65 73 65 6e 74 2e 0a 20 20 20 20 is present..
a24e0 2a 2a 20 20 20 66 6c 61 67 5f 6c 65 66 74 6a 75 ** flag_leftju
a24f0 73 74 69 66 79 20 20 20 20 20 20 20 20 20 20 20 stify
a2500 20 54 52 55 45 20 69 66 20 61 20 27 2d 27 20 69 TRUE if a '-' i
a2510 73 20 70 72 65 73 65 6e 74 20 6f 72 20 69 66 20 s present or if
a2520 74 68 65 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 the. **
a2530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a2540 20 20 20 20 20 20 20 20 20 66 69 65 6c 64 20 77 field w
a2550 69 64 74 68 20 77 61 73 20 6e 65 67 61 74 69 76 idth was negativ
a2560 65 2e 0a 20 20 20 20 2a 2a 20 20 20 66 6c 61 67 e.. ** flag
a2570 5f 7a 65 72 6f 70 61 64 20 20 20 20 20 20 20 20 _zeropad
a2580 20 20 20 20 20 20 20 20 54 52 55 45 20 69 66 20 TRUE if
a2590 74 68 65 20 77 69 64 74 68 20 62 65 67 61 6e 20 the width began
a25a0 77 69 74 68 20 30 2e 0a 20 20 20 20 2a 2a 20 20 with 0.. **
a25b0 20 66 6c 61 67 5f 6c 6f 6e 67 20 20 20 20 20 20 flag_long
a25c0 20 20 20 20 20 20 20 20 20 20 20 20 20 54 52 55 TRU
a25d0 45 20 69 66 20 74 68 65 20 6c 65 74 74 65 72 20 E if the letter
a25e0 27 6c 27 20 28 65 6c 6c 29 20 70 72 65 66 69 78 'l' (ell) prefix
a25f0 65 64 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 ed. **
a2600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a2610 20 20 20 20 20 20 20 20 74 68 65 20 63 6f 6e 76 the conv
a2620 65 72 73 69 6f 6e 20 63 68 61 72 61 63 74 65 72 ersion character
a2630 2e 0a 20 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f .. ** flag_
a2640 6c 6f 6e 67 6c 6f 6e 67 20 20 20 20 20 20 20 20 longlong
a2650 20 20 20 20 20 20 20 54 52 55 45 20 69 66 20 74 TRUE if t
a2660 68 65 20 6c 65 74 74 65 72 20 27 6c 6c 27 20 28 he letter 'll' (
a2670 65 6c 6c 20 65 6c 6c 29 20 70 72 65 66 69 78 65 ell ell) prefixe
a2680 64 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 d. **
a2690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a26a0 20 20 20 20 20 20 20 74 68 65 20 63 6f 6e 76 65 the conve
a26b0 72 73 69 6f 6e 20 63 68 61 72 61 63 74 65 72 2e rsion character.
a26c0 0a 20 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f 62 . ** flag_b
a26d0 6c 61 6e 6b 73 69 67 6e 20 20 20 20 20 20 20 20 lanksign
a26e0 20 20 20 20 20 20 54 52 55 45 20 69 66 20 61 20 TRUE if a
a26f0 27 20 27 20 69 73 20 70 72 65 73 65 6e 74 2e 0a ' ' is present..
a2700 20 20 20 20 2a 2a 20 20 20 77 69 64 74 68 20 20 ** width
a2710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a2720 20 20 20 20 20 54 68 65 20 73 70 65 63 69 66 69 The specifi
a2730 65 64 20 66 69 65 6c 64 20 77 69 64 74 68 2e 20 ed field width.
a2740 20 54 68 69 73 20 69 73 0a 20 20 20 20 2a 2a 20 This is. **
a2750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a2760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61 6c al
a2770 77 61 79 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 ways non-negativ
a2780 65 2e 20 20 5a 65 72 6f 20 69 73 20 74 68 65 20 e. Zero is the
a2790 64 65 66 61 75 6c 74 2e 0a 20 20 20 20 2a 2a 20 default.. **
a27a0 20 20 70 72 65 63 69 73 69 6f 6e 20 20 20 20 20 precision
a27b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 54 68 Th
a27c0 65 20 73 70 65 63 69 66 69 65 64 20 70 72 65 63 e specified prec
a27d0 69 73 69 6f 6e 2e 20 20 54 68 65 20 64 65 66 61 ision. The defa
a27e0 75 6c 74 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 ult. **
a27f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a2800 20 20 20 20 20 20 20 20 20 69 73 20 2d 31 2e 0a is -1..
a2810 20 20 20 20 2a 2a 20 20 20 78 74 79 70 65 20 20 ** xtype
a2820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a2830 20 20 20 20 20 54 68 65 20 63 6c 61 73 73 20 6f The class o
a2840 66 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e f the conversion
a2850 2e 0a 20 20 20 20 2a 2a 20 20 20 69 6e 66 6f 70 .. ** infop
a2860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a2870 20 20 20 20 20 20 20 50 6f 69 6e 74 65 72 20 74 Pointer t
a2880 6f 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 o the appropriat
a2890 65 20 69 6e 66 6f 20 73 74 72 75 63 74 2e 0a 20 e info struct..
a28a0 20 20 20 2a 2f 0a 20 20 20 20 73 77 69 74 63 68 */. switch
a28b0 28 20 78 74 79 70 65 20 29 7b 0a 20 20 20 20 20 ( xtype ){.
a28c0 20 63 61 73 65 20 65 74 50 4f 49 4e 54 45 52 3a case etPOINTER:
a28d0 0a 20 20 20 20 20 20 20 20 66 6c 61 67 5f 6c 6f . flag_lo
a28e0 6e 67 6c 6f 6e 67 20 3d 20 73 69 7a 65 6f 66 28 nglong = sizeof(
a28f0 63 68 61 72 2a 29 3d 3d 73 69 7a 65 6f 66 28 69 char*)==sizeof(i
a2900 36 34 29 3b 0a 20 20 20 20 20 20 20 20 66 6c 61 64);. fla
a2910 67 5f 6c 6f 6e 67 20 3d 20 73 69 7a 65 6f 66 28 g_long = sizeof(
a2920 63 68 61 72 2a 29 3d 3d 73 69 7a 65 6f 66 28 6c char*)==sizeof(l
a2930 6f 6e 67 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 ong int);.
a2940 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72 6f 75 67 /* Fall throug
a2950 68 20 69 6e 74 6f 20 74 68 65 20 6e 65 78 74 20 h into the next
a2960 63 61 73 65 20 2a 2f 0a 20 20 20 20 20 20 63 61 case */. ca
a2970 73 65 20 65 74 4f 52 44 49 4e 41 4c 3a 0a 20 20 se etORDINAL:.
a2980 20 20 20 20 63 61 73 65 20 65 74 52 41 44 49 58 case etRADIX
a2990 3a 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 6e :. if( in
a29a0 66 6f 70 2d 3e 66 6c 61 67 73 20 26 20 46 4c 41 fop->flags & FLA
a29b0 47 5f 53 49 47 4e 45 44 20 29 7b 0a 20 20 20 20 G_SIGNED ){.
a29c0 20 20 20 20 20 20 69 36 34 20 76 3b 0a 20 20 20 i64 v;.
a29d0 20 20 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f if( flag_
a29e0 6c 6f 6e 67 6c 6f 6e 67 20 29 7b 0a 20 20 20 20 longlong ){.
a29f0 20 20 20 20 20 20 20 20 76 20 3d 20 76 61 5f 61 v = va_a
a2a00 72 67 28 61 70 2c 69 36 34 29 3b 0a 20 20 20 20 rg(ap,i64);.
a2a10 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 }else if(
a2a20 66 6c 61 67 5f 6c 6f 6e 67 20 29 7b 0a 20 20 20 flag_long ){.
a2a30 20 20 20 20 20 20 20 20 20 76 20 3d 20 76 61 5f v = va_
a2a40 61 72 67 28 61 70 2c 6c 6f 6e 67 20 69 6e 74 29 arg(ap,long int)
a2a50 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 ;. }els
a2a60 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 76 e{. v
a2a70 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 69 6e 74 = va_arg(ap,int
a2a80 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 );. }.
a2a90 20 20 20 20 20 20 20 20 20 69 66 28 20 76 3c 30 if( v<0
a2aa0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
a2ab0 6c 6f 6e 67 76 61 6c 75 65 20 3d 20 2d 76 3b 0a longvalue = -v;.
a2ac0 20 20 20 20 20 20 20 20 20 20 20 20 70 72 65 66 pref
a2ad0 69 78 20 3d 20 27 2d 27 3b 0a 20 20 20 20 20 20 ix = '-';.
a2ae0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
a2af0 20 20 20 20 20 20 20 6c 6f 6e 67 76 61 6c 75 65 longvalue
a2b00 20 3d 20 76 3b 0a 20 20 20 20 20 20 20 20 20 20 = v;.
a2b10 20 20 69 66 28 20 66 6c 61 67 5f 70 6c 75 73 73 if( flag_pluss
a2b20 69 67 6e 20 29 20 20 20 20 20 20 20 20 70 72 65 ign ) pre
a2b30 66 69 78 20 3d 20 27 2b 27 3b 0a 20 20 20 20 20 fix = '+';.
a2b40 20 20 20 20 20 20 20 65 6c 73 65 20 69 66 28 20 else if(
a2b50 66 6c 61 67 5f 62 6c 61 6e 6b 73 69 67 6e 20 29 flag_blanksign )
a2b60 20 20 70 72 65 66 69 78 20 3d 20 27 20 27 3b 0a prefix = ' ';.
a2b70 20 20 20 20 20 20 20 20 20 20 20 20 65 6c 73 65 else
a2b80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a2b90 20 20 20 20 20 20 20 70 72 65 66 69 78 20 3d 20 prefix =
a2ba0 30 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 0;. }.
a2bb0 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }else{.
a2bc0 20 20 20 20 20 20 20 20 69 66 28 20 66 6c 61 67 if( flag
a2bd0 5f 6c 6f 6e 67 6c 6f 6e 67 20 29 7b 0a 20 20 20 _longlong ){.
a2be0 20 20 20 20 20 20 20 20 20 6c 6f 6e 67 76 61 6c longval
a2bf0 75 65 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 75 ue = va_arg(ap,u
a2c00 36 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 64);. }
a2c10 65 6c 73 65 20 69 66 28 20 66 6c 61 67 5f 6c 6f else if( flag_lo
a2c20 6e 67 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 ng ){.
a2c30 20 20 6c 6f 6e 67 76 61 6c 75 65 20 3d 20 76 61 longvalue = va
a2c40 5f 61 72 67 28 61 70 2c 75 6e 73 69 67 6e 65 64 _arg(ap,unsigned
a2c50 20 6c 6f 6e 67 20 69 6e 74 29 3b 0a 20 20 20 20 long int);.
a2c60 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
a2c70 20 20 20 20 20 20 20 20 20 6c 6f 6e 67 76 61 6c longval
a2c80 75 65 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 75 ue = va_arg(ap,u
a2c90 6e 73 69 67 6e 65 64 20 69 6e 74 29 3b 0a 20 20 nsigned int);.
a2ca0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
a2cb0 20 20 20 20 70 72 65 66 69 78 20 3d 20 30 3b 0a prefix = 0;.
a2cc0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
a2cd0 20 20 69 66 28 20 6c 6f 6e 67 76 61 6c 75 65 3d if( longvalue=
a2ce0 3d 30 20 29 20 66 6c 61 67 5f 61 6c 74 65 72 6e =0 ) flag_altern
a2cf0 61 74 65 66 6f 72 6d 20 3d 20 30 3b 0a 20 20 20 ateform = 0;.
a2d00 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f 7a 65 if( flag_ze
a2d10 72 6f 70 61 64 20 26 26 20 70 72 65 63 69 73 69 ropad && precisi
a2d20 6f 6e 3c 77 69 64 74 68 2d 28 70 72 65 66 69 78 on<width-(prefix
a2d30 21 3d 30 29 20 29 7b 0a 20 20 20 20 20 20 20 20 !=0) ){.
a2d40 20 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 77 69 precision = wi
a2d50 64 74 68 2d 28 70 72 65 66 69 78 21 3d 30 29 3b dth-(prefix!=0);
a2d60 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
a2d70 20 20 20 62 75 66 70 74 20 3d 20 26 62 75 66 5b bufpt = &buf[
a2d80 65 74 42 55 46 53 49 5a 45 2d 31 5d 3b 0a 20 20 etBUFSIZE-1];.
a2d90 20 20 20 20 20 20 69 66 28 20 78 74 79 70 65 3d if( xtype=
a2da0 3d 65 74 4f 52 44 49 4e 41 4c 20 29 7b 0a 20 20 =etORDINAL ){.
a2db0 20 20 20 20 20 20 20 20 73 74 61 74 69 63 20 63 static c
a2dc0 6f 6e 73 74 20 63 68 61 72 20 7a 4f 72 64 5b 5d onst char zOrd[]
a2dd0 20 3d 20 22 74 68 73 74 6e 64 72 64 22 3b 0a 20 = "thstndrd";.
a2de0 20 20 20 20 20 20 20 20 20 69 6e 74 20 78 20 3d int x =
a2df0 20 28 69 6e 74 29 28 6c 6f 6e 67 76 61 6c 75 65 (int)(longvalue
a2e00 20 25 20 31 30 29 3b 0a 20 20 20 20 20 20 20 20 % 10);.
a2e10 20 20 69 66 28 20 78 3e 3d 34 20 7c 7c 20 28 6c if( x>=4 || (l
a2e20 6f 6e 67 76 61 6c 75 65 2f 31 30 29 25 31 30 3d ongvalue/10)%10=
a2e30 3d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 =1 ){.
a2e40 20 20 78 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 x = 0;.
a2e50 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 62 }. b
a2e60 75 66 5b 65 74 42 55 46 53 49 5a 45 2d 33 5d 20 uf[etBUFSIZE-3]
a2e70 3d 20 7a 4f 72 64 5b 78 2a 32 5d 3b 0a 20 20 20 = zOrd[x*2];.
a2e80 20 20 20 20 20 20 20 62 75 66 5b 65 74 42 55 46 buf[etBUF
a2e90 53 49 5a 45 2d 32 5d 20 3d 20 7a 4f 72 64 5b 78 SIZE-2] = zOrd[x
a2ea0 2a 32 2b 31 5d 3b 0a 20 20 20 20 20 20 20 20 20 *2+1];.
a2eb0 20 62 75 66 70 74 20 2d 3d 20 32 3b 0a 20 20 20 bufpt -= 2;.
a2ec0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7b }. {
a2ed0 0a 20 20 20 20 20 20 20 20 20 20 72 65 67 69 73 . regis
a2ee0 74 65 72 20 63 6f 6e 73 74 20 63 68 61 72 20 2a ter const char *
a2ef0 63 73 65 74 3b 20 20 20 20 20 20 2f 2a 20 55 73 cset; /* Us
a2f00 65 20 72 65 67 69 73 74 65 72 73 20 66 6f 72 20 e registers for
a2f10 73 70 65 65 64 20 2a 2f 0a 20 20 20 20 20 20 20 speed */.
a2f20 20 20 20 72 65 67 69 73 74 65 72 20 69 6e 74 20 register int
a2f30 62 61 73 65 3b 0a 20 20 20 20 20 20 20 20 20 20 base;.
a2f40 63 73 65 74 20 3d 20 26 61 44 69 67 69 74 73 5b cset = &aDigits[
a2f50 69 6e 66 6f 70 2d 3e 63 68 61 72 73 65 74 5d 3b infop->charset];
a2f60 0a 20 20 20 20 20 20 20 20 20 20 62 61 73 65 20 . base
a2f70 3d 20 69 6e 66 6f 70 2d 3e 62 61 73 65 3b 0a 20 = infop->base;.
a2f80 20 20 20 20 20 20 20 20 20 64 6f 7b 20 20 20 20 do{
a2f90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a2fa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a2fb0 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 76 65 72 /* Conver
a2fc0 74 20 74 6f 20 61 73 63 69 69 20 2a 2f 0a 20 20 t to ascii */.
a2fd0 20 20 20 20 20 20 20 20 20 20 2a 28 2d 2d 62 75 *(--bu
a2fe0 66 70 74 29 20 3d 20 63 73 65 74 5b 6c 6f 6e 67 fpt) = cset[long
a2ff0 76 61 6c 75 65 25 62 61 73 65 5d 3b 0a 20 20 20 value%base];.
a3000 20 20 20 20 20 20 20 20 20 6c 6f 6e 67 76 61 6c longval
a3010 75 65 20 3d 20 6c 6f 6e 67 76 61 6c 75 65 2f 62 ue = longvalue/b
a3020 61 73 65 3b 0a 20 20 20 20 20 20 20 20 20 20 7d ase;. }
a3030 77 68 69 6c 65 28 20 6c 6f 6e 67 76 61 6c 75 65 while( longvalue
a3040 3e 30 20 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a >0 );. }.
a3050 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d length =
a3060 20 28 69 6e 74 29 28 26 62 75 66 5b 65 74 42 55 (int)(&buf[etBU
a3070 46 53 49 5a 45 2d 31 5d 2d 62 75 66 70 74 29 3b FSIZE-1]-bufpt);
a3080 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 64 78 . for(idx
a3090 3d 70 72 65 63 69 73 69 6f 6e 2d 6c 65 6e 67 74 =precision-lengt
a30a0 68 3b 20 69 64 78 3e 30 3b 20 69 64 78 2d 2d 29 h; idx>0; idx--)
a30b0 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 28 2d 2d {. *(--
a30c0 62 75 66 70 74 29 20 3d 20 27 30 27 3b 20 20 20 bufpt) = '0';
a30d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a30e0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 5a 65 72 /* Zer
a30f0 6f 20 70 61 64 20 2a 2f 0a 20 20 20 20 20 20 20 o pad */.
a3100 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 }. if( p
a3110 72 65 66 69 78 20 29 20 2a 28 2d 2d 62 75 66 70 refix ) *(--bufp
a3120 74 29 20 3d 20 70 72 65 66 69 78 3b 20 20 20 20 t) = prefix;
a3130 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 /* Ad
a3140 64 20 73 69 67 6e 20 2a 2f 0a 20 20 20 20 20 20 d sign */.
a3150 20 20 69 66 28 20 66 6c 61 67 5f 61 6c 74 65 72 if( flag_alter
a3160 6e 61 74 65 66 6f 72 6d 20 26 26 20 69 6e 66 6f nateform && info
a3170 70 2d 3e 70 72 65 66 69 78 20 29 7b 20 20 20 20 p->prefix ){
a3180 20 20 2f 2a 20 41 64 64 20 22 30 22 20 6f 72 20 /* Add "0" or
a3190 22 30 78 22 20 2a 2f 0a 20 20 20 20 20 20 20 20 "0x" */.
a31a0 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 72 const char *pr
a31b0 65 3b 0a 20 20 20 20 20 20 20 20 20 20 63 68 61 e;. cha
a31c0 72 20 78 3b 0a 20 20 20 20 20 20 20 20 20 20 70 r x;. p
a31d0 72 65 20 3d 20 26 61 50 72 65 66 69 78 5b 69 6e re = &aPrefix[in
a31e0 66 6f 70 2d 3e 70 72 65 66 69 78 5d 3b 0a 20 20 fop->prefix];.
a31f0 20 20 20 20 20 20 20 20 66 6f 72 28 3b 20 28 78 for(; (x
a3200 3d 28 2a 70 72 65 29 29 21 3d 30 3b 20 70 72 65 =(*pre))!=0; pre
a3210 2b 2b 29 20 2a 28 2d 2d 62 75 66 70 74 29 20 3d ++) *(--bufpt) =
a3220 20 78 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 x;. }.
a3230 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 28 length = (
a3240 69 6e 74 29 28 26 62 75 66 5b 65 74 42 55 46 53 int)(&buf[etBUFS
a3250 49 5a 45 2d 31 5d 2d 62 75 66 70 74 29 3b 0a 20 IZE-1]-bufpt);.
a3260 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
a3270 20 20 20 20 63 61 73 65 20 65 74 46 4c 4f 41 54 case etFLOAT
a3280 3a 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 45 :. case etE
a3290 58 50 3a 0a 20 20 20 20 20 20 63 61 73 65 20 65 XP:. case e
a32a0 74 47 45 4e 45 52 49 43 3a 0a 20 20 20 20 20 20 tGENERIC:.
a32b0 20 20 72 65 61 6c 76 61 6c 75 65 20 3d 20 76 61 realvalue = va
a32c0 5f 61 72 67 28 61 70 2c 64 6f 75 62 6c 65 29 3b _arg(ap,double);
a32d0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
a32e0 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f OMIT_FLOATING_PO
a32f0 49 4e 54 0a 20 20 20 20 20 20 20 20 69 66 28 20 INT. if(
a3300 70 72 65 63 69 73 69 6f 6e 3c 30 20 29 20 70 72 precision<0 ) pr
a3310 65 63 69 73 69 6f 6e 20 3d 20 36 3b 20 20 20 20 ecision = 6;
a3320 20 20 20 20 20 2f 2a 20 53 65 74 20 64 65 66 61 /* Set defa
a3330 75 6c 74 20 70 72 65 63 69 73 69 6f 6e 20 2a 2f ult precision */
a3340 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 72 65 . if( pre
a3350 63 69 73 69 6f 6e 3e 65 74 42 55 46 53 49 5a 45 cision>etBUFSIZE
a3360 2f 32 2d 31 30 20 29 20 70 72 65 63 69 73 69 6f /2-10 ) precisio
a3370 6e 20 3d 20 65 74 42 55 46 53 49 5a 45 2f 32 2d n = etBUFSIZE/2-
a3380 31 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 10;. if(
a3390 72 65 61 6c 76 61 6c 75 65 3c 30 2e 30 20 29 7b realvalue<0.0 ){
a33a0 0a 20 20 20 20 20 20 20 20 20 20 72 65 61 6c 76 . realv
a33b0 61 6c 75 65 20 3d 20 2d 72 65 61 6c 76 61 6c 75 alue = -realvalu
a33c0 65 3b 0a 20 20 20 20 20 20 20 20 20 20 70 72 65 e;. pre
a33d0 66 69 78 20 3d 20 27 2d 27 3b 0a 20 20 20 20 20 fix = '-';.
a33e0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
a33f0 20 20 20 20 69 66 28 20 66 6c 61 67 5f 70 6c 75 if( flag_plu
a3400 73 73 69 67 6e 20 29 20 20 20 20 20 20 20 20 20 ssign )
a3410 20 70 72 65 66 69 78 20 3d 20 27 2b 27 3b 0a 20 prefix = '+';.
a3420 20 20 20 20 20 20 20 20 20 65 6c 73 65 20 69 66 else if
a3430 28 20 66 6c 61 67 5f 62 6c 61 6e 6b 73 69 67 6e ( flag_blanksign
a3440 20 29 20 20 20 20 70 72 65 66 69 78 20 3d 20 27 ) prefix = '
a3450 20 27 3b 0a 20 20 20 20 20 20 20 20 20 20 65 6c ';. el
a3460 73 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 se
a3470 20 20 20 20 20 20 20 20 20 20 20 70 72 65 66 69 prefi
a3480 78 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d x = 0;. }
a3490 0a 20 20 20 20 20 20 20 20 69 66 28 20 78 74 79 . if( xty
a34a0 70 65 3d 3d 65 74 47 45 4e 45 52 49 43 20 26 26 pe==etGENERIC &&
a34b0 20 70 72 65 63 69 73 69 6f 6e 3e 30 20 29 20 70 precision>0 ) p
a34c0 72 65 63 69 73 69 6f 6e 2d 2d 3b 0a 23 69 66 20 recision--;.#if
a34d0 30 0a 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 75 0. /* Rou
a34e0 6e 64 69 6e 67 20 77 6f 72 6b 73 20 6c 69 6b 65 nding works like
a34f0 20 42 53 44 20 77 68 65 6e 20 74 68 65 20 63 6f BSD when the co
a3500 6e 73 74 61 6e 74 20 30 2e 34 39 39 39 20 69 73 nstant 0.4999 is
a3510 20 75 73 65 64 2e 20 20 57 69 65 72 64 21 20 2a used. Wierd! *
a3520 2f 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 64 /. for(id
a3530 78 3d 70 72 65 63 69 73 69 6f 6e 2c 20 72 6f 75 x=precision, rou
a3540 6e 64 65 72 3d 30 2e 34 39 39 39 3b 20 69 64 78 nder=0.4999; idx
a3550 3e 30 3b 20 69 64 78 2d 2d 2c 20 72 6f 75 6e 64 >0; idx--, round
a3560 65 72 2a 3d 30 2e 31 29 3b 0a 23 65 6c 73 65 0a er*=0.1);.#else.
a3570 20 20 20 20 20 20 20 20 2f 2a 20 49 74 20 6d 61 /* It ma
a3580 6b 65 73 20 6d 6f 72 65 20 73 65 6e 73 65 20 74 kes more sense t
a3590 6f 20 75 73 65 20 30 2e 35 20 2a 2f 0a 20 20 20 o use 0.5 */.
a35a0 20 20 20 20 20 66 6f 72 28 69 64 78 3d 70 72 65 for(idx=pre
a35b0 63 69 73 69 6f 6e 2c 20 72 6f 75 6e 64 65 72 3d cision, rounder=
a35c0 30 2e 35 3b 20 69 64 78 3e 30 3b 20 69 64 78 2d 0.5; idx>0; idx-
a35d0 2d 2c 20 72 6f 75 6e 64 65 72 2a 3d 30 2e 31 29 -, rounder*=0.1)
a35e0 7b 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 {}.#endif.
a35f0 20 20 69 66 28 20 78 74 79 70 65 3d 3d 65 74 46 if( xtype==etF
a3600 4c 4f 41 54 20 29 20 72 65 61 6c 76 61 6c 75 65 LOAT ) realvalue
a3610 20 2b 3d 20 72 6f 75 6e 64 65 72 3b 0a 20 20 20 += rounder;.
a3620 20 20 20 20 20 2f 2a 20 4e 6f 72 6d 61 6c 69 7a /* Normaliz
a3630 65 20 72 65 61 6c 76 61 6c 75 65 20 74 6f 20 77 e realvalue to w
a3640 69 74 68 69 6e 20 31 30 2e 30 20 3e 20 72 65 61 ithin 10.0 > rea
a3650 6c 76 61 6c 75 65 20 3e 3d 20 31 2e 30 20 2a 2f lvalue >= 1.0 */
a3660 0a 20 20 20 20 20 20 20 20 65 78 70 20 3d 20 30 . exp = 0
a3670 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 71 ;. if( sq
a3680 6c 69 74 65 33 49 73 4e 61 4e 28 28 64 6f 75 62 lite3IsNaN((doub
a3690 6c 65 29 72 65 61 6c 76 61 6c 75 65 29 20 29 7b le)realvalue) ){
a36a0 0a 20 20 20 20 20 20 20 20 20 20 62 75 66 70 74 . bufpt
a36b0 20 3d 20 22 4e 61 4e 22 3b 0a 20 20 20 20 20 20 = "NaN";.
a36c0 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 33 3b 0a length = 3;.
a36d0 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b break;
a36e0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
a36f0 20 20 20 69 66 28 20 72 65 61 6c 76 61 6c 75 65 if( realvalue
a3700 3e 30 2e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 >0.0 ){.
a3710 20 20 77 68 69 6c 65 28 20 72 65 61 6c 76 61 6c while( realval
a3720 75 65 3e 3d 31 65 33 32 20 26 26 20 65 78 70 3c ue>=1e32 && exp<
a3730 3d 33 35 30 20 29 7b 20 72 65 61 6c 76 61 6c 75 =350 ){ realvalu
a3740 65 20 2a 3d 20 31 65 2d 33 32 3b 20 65 78 70 2b e *= 1e-32; exp+
a3750 3d 33 32 3b 20 7d 0a 20 20 20 20 20 20 20 20 20 =32; }.
a3760 20 77 68 69 6c 65 28 20 72 65 61 6c 76 61 6c 75 while( realvalu
a3770 65 3e 3d 31 65 38 20 26 26 20 65 78 70 3c 3d 33 e>=1e8 && exp<=3
a3780 35 30 20 29 7b 20 72 65 61 6c 76 61 6c 75 65 20 50 ){ realvalue
a3790 2a 3d 20 31 65 2d 38 3b 20 65 78 70 2b 3d 38 3b *= 1e-8; exp+=8;
a37a0 20 7d 0a 20 20 20 20 20 20 20 20 20 20 77 68 69 }. whi
a37b0 6c 65 28 20 72 65 61 6c 76 61 6c 75 65 3e 3d 31 le( realvalue>=1
a37c0 30 2e 30 20 26 26 20 65 78 70 3c 3d 33 35 30 20 0.0 && exp<=350
a37d0 29 7b 20 72 65 61 6c 76 61 6c 75 65 20 2a 3d 20 ){ realvalue *=
a37e0 30 2e 31 3b 20 65 78 70 2b 2b 3b 20 7d 0a 20 20 0.1; exp++; }.
a37f0 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 72 while( r
a3800 65 61 6c 76 61 6c 75 65 3c 31 65 2d 38 20 29 7b ealvalue<1e-8 ){
a3810 20 72 65 61 6c 76 61 6c 75 65 20 2a 3d 20 31 65 realvalue *= 1e
a3820 38 3b 20 65 78 70 2d 3d 38 3b 20 7d 0a 20 20 20 8; exp-=8; }.
a3830 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 72 65 while( re
a3840 61 6c 76 61 6c 75 65 3c 31 2e 30 20 29 7b 20 72 alvalue<1.0 ){ r
a3850 65 61 6c 76 61 6c 75 65 20 2a 3d 20 31 30 2e 30 ealvalue *= 10.0
a3860 3b 20 65 78 70 2d 2d 3b 20 7d 0a 20 20 20 20 20 ; exp--; }.
a3870 20 20 20 20 20 69 66 28 20 65 78 70 3e 33 35 30 if( exp>350
a3880 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
a3890 69 66 28 20 70 72 65 66 69 78 3d 3d 27 2d 27 20 if( prefix=='-'
a38a0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ){.
a38b0 20 62 75 66 70 74 20 3d 20 22 2d 49 6e 66 22 3b bufpt = "-Inf";
a38c0 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c . }el
a38d0 73 65 20 69 66 28 20 70 72 65 66 69 78 3d 3d 27 se if( prefix=='
a38e0 2b 27 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 +' ){.
a38f0 20 20 20 20 62 75 66 70 74 20 3d 20 22 2b 49 6e bufpt = "+In
a3900 66 22 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 f";.
a3910 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 }else{.
a3920 20 20 20 20 20 62 75 66 70 74 20 3d 20 22 49 6e bufpt = "In
a3930 66 22 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 f";.
a3940 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 6c 65 }. le
a3950 6e 67 74 68 20 3d 20 73 71 6c 69 74 65 33 53 74 ngth = sqlite3St
a3960 72 6c 65 6e 33 30 28 62 75 66 70 74 29 3b 0a 20 rlen30(bufpt);.
a3970 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b break
a3980 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
a3990 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
a39a0 62 75 66 70 74 20 3d 20 62 75 66 3b 0a 20 20 20 bufpt = buf;.
a39b0 20 20 20 20 20 2f 2a 0a 20 20 20 20 20 20 20 20 /*.
a39c0 2a 2a 20 49 66 20 74 68 65 20 66 69 65 6c 64 20 ** If the field
a39d0 74 79 70 65 20 69 73 20 65 74 47 45 4e 45 52 49 type is etGENERI
a39e0 43 2c 20 74 68 65 6e 20 63 6f 6e 76 65 72 74 20 C, then convert
a39f0 74 6f 20 65 69 74 68 65 72 20 65 74 45 58 50 0a to either etEXP.
a3a00 20 20 20 20 20 20 20 20 2a 2a 20 6f 72 20 65 74 ** or et
a3a10 46 4c 4f 41 54 2c 20 61 73 20 61 70 70 72 6f 70 FLOAT, as approp
a3a20 72 69 61 74 65 2e 0a 20 20 20 20 20 20 20 20 2a riate.. *
a3a30 2f 0a 20 20 20 20 20 20 20 20 66 6c 61 67 5f 65 /. flag_e
a3a40 78 70 20 3d 20 78 74 79 70 65 3d 3d 65 74 45 58 xp = xtype==etEX
a3a50 50 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 78 P;. if( x
a3a60 74 79 70 65 21 3d 65 74 46 4c 4f 41 54 20 29 7b type!=etFLOAT ){
a3a70 0a 20 20 20 20 20 20 20 20 20 20 72 65 61 6c 76 . realv
a3a80 61 6c 75 65 20 2b 3d 20 72 6f 75 6e 64 65 72 3b alue += rounder;
a3a90 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 . if( r
a3aa0 65 61 6c 76 61 6c 75 65 3e 3d 31 30 2e 30 20 29 ealvalue>=10.0 )
a3ab0 7b 20 72 65 61 6c 76 61 6c 75 65 20 2a 3d 20 30 { realvalue *= 0
a3ac0 2e 31 3b 20 65 78 70 2b 2b 3b 20 7d 0a 20 20 20 .1; exp++; }.
a3ad0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 }. i
a3ae0 66 28 20 78 74 79 70 65 3d 3d 65 74 47 45 4e 45 f( xtype==etGENE
a3af0 52 49 43 20 29 7b 0a 20 20 20 20 20 20 20 20 20 RIC ){.
a3b00 20 66 6c 61 67 5f 72 74 7a 20 3d 20 21 66 6c 61 flag_rtz = !fla
a3b10 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f 72 6d 3b g_alternateform;
a3b20 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 65 . if( e
a3b30 78 70 3c 2d 34 20 7c 7c 20 65 78 70 3e 70 72 65 xp<-4 || exp>pre
a3b40 63 69 73 69 6f 6e 20 29 7b 0a 20 20 20 20 20 20 cision ){.
a3b50 20 20 20 20 20 20 78 74 79 70 65 20 3d 20 65 74 xtype = et
a3b60 45 58 50 3b 0a 20 20 20 20 20 20 20 20 20 20 7d EXP;. }
a3b70 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 else{.
a3b80 20 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 70 72 precision = pr
a3b90 65 63 69 73 69 6f 6e 20 2d 20 65 78 70 3b 0a 20 ecision - exp;.
a3ba0 20 20 20 20 20 20 20 20 20 20 20 78 74 79 70 65 xtype
a3bb0 20 3d 20 65 74 46 4c 4f 41 54 3b 0a 20 20 20 20 = etFLOAT;.
a3bc0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
a3bd0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 }else{.
a3be0 20 66 6c 61 67 5f 72 74 7a 20 3d 20 30 3b 0a 20 flag_rtz = 0;.
a3bf0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
a3c00 20 69 66 28 20 78 74 79 70 65 3d 3d 65 74 45 58 if( xtype==etEX
a3c10 50 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 65 P ){. e
a3c20 32 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 2 = 0;. }
a3c30 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 else{.
a3c40 65 32 20 3d 20 65 78 70 3b 0a 20 20 20 20 20 20 e2 = exp;.
a3c50 20 20 7d 0a 20 20 20 20 20 20 20 20 6e 73 64 20 }. nsd
a3c60 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 66 6c 61 = 0;. fla
a3c70 67 5f 64 70 20 3d 20 28 70 72 65 63 69 73 69 6f g_dp = (precisio
a3c80 6e 3e 30 20 3f 31 3a 30 29 20 7c 20 66 6c 61 67 n>0 ?1:0) | flag
a3c90 5f 61 6c 74 65 72 6e 61 74 65 66 6f 72 6d 20 7c _alternateform |
a3ca0 20 66 6c 61 67 5f 61 6c 74 66 6f 72 6d 32 3b 0a flag_altform2;.
a3cb0 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73 /* The s
a3cc0 69 67 6e 20 69 6e 20 66 72 6f 6e 74 20 6f 66 20 ign in front of
a3cd0 74 68 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 the number */.
a3ce0 20 20 20 20 20 20 69 66 28 20 70 72 65 66 69 78 if( prefix
a3cf0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 28 ){. *(
a3d00 62 75 66 70 74 2b 2b 29 20 3d 20 70 72 65 66 69 bufpt++) = prefi
a3d10 78 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 x;. }.
a3d20 20 20 20 20 20 2f 2a 20 44 69 67 69 74 73 20 70 /* Digits p
a3d30 72 69 6f 72 20 74 6f 20 74 68 65 20 64 65 63 69 rior to the deci
a3d40 6d 61 6c 20 70 6f 69 6e 74 20 2a 2f 0a 20 20 20 mal point */.
a3d50 20 20 20 20 20 69 66 28 20 65 32 3c 30 20 29 7b if( e2<0 ){
a3d60 0a 20 20 20 20 20 20 20 20 20 20 2a 28 62 75 66 . *(buf
a3d70 70 74 2b 2b 29 20 3d 20 27 30 27 3b 0a 20 20 20 pt++) = '0';.
a3d80 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
a3d90 20 20 20 20 20 20 66 6f 72 28 3b 20 65 32 3e 3d for(; e2>=
a3da0 30 3b 20 65 32 2d 2d 29 7b 0a 20 20 20 20 20 20 0; e2--){.
a3db0 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 *(bufpt++)
a3dc0 20 3d 20 65 74 5f 67 65 74 64 69 67 69 74 28 26 = et_getdigit(&
a3dd0 72 65 61 6c 76 61 6c 75 65 2c 26 6e 73 64 29 3b realvalue,&nsd);
a3de0 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 . }.
a3df0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2f }. /
a3e00 2a 20 54 68 65 20 64 65 63 69 6d 61 6c 20 70 6f * The decimal po
a3e10 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 int */. i
a3e20 66 28 20 66 6c 61 67 5f 64 70 20 29 7b 0a 20 20 f( flag_dp ){.
a3e30 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b *(bufpt+
a3e40 2b 29 20 3d 20 27 2e 27 3b 0a 20 20 20 20 20 20 +) = '.';.
a3e50 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 22 }. /* "
a3e60 30 22 20 64 69 67 69 74 73 20 61 66 74 65 72 20 0" digits after
a3e70 74 68 65 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e the decimal poin
a3e80 74 20 62 75 74 20 62 65 66 6f 72 65 20 74 68 65 t but before the
a3e90 20 66 69 72 73 74 0a 20 20 20 20 20 20 20 20 2a first. *
a3ea0 2a 20 73 69 67 6e 69 66 69 63 61 6e 74 20 64 69 * significant di
a3eb0 67 69 74 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 git of the numbe
a3ec0 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 66 6f 72 r */. for
a3ed0 28 65 32 2b 2b 3b 20 65 32 3c 30 3b 20 70 72 65 (e2++; e2<0; pre
a3ee0 63 69 73 69 6f 6e 2d 2d 2c 20 65 32 2b 2b 29 7b cision--, e2++){
a3ef0 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 . asser
a3f00 74 28 20 70 72 65 63 69 73 69 6f 6e 3e 30 20 29 t( precision>0 )
a3f10 3b 0a 20 20 20 20 20 20 20 20 20 20 2a 28 62 75 ;. *(bu
a3f20 66 70 74 2b 2b 29 20 3d 20 27 30 27 3b 0a 20 20 fpt++) = '0';.
a3f30 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
a3f40 2f 2a 20 53 69 67 6e 69 66 69 63 61 6e 74 20 64 /* Significant d
a3f50 69 67 69 74 73 20 61 66 74 65 72 20 74 68 65 20 igits after the
a3f60 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20 2a 2f decimal point */
a3f70 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 . while(
a3f80 28 70 72 65 63 69 73 69 6f 6e 2d 2d 29 3e 30 20 (precision--)>0
a3f90 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 28 62 ){. *(b
a3fa0 75 66 70 74 2b 2b 29 20 3d 20 65 74 5f 67 65 74 ufpt++) = et_get
a3fb0 64 69 67 69 74 28 26 72 65 61 6c 76 61 6c 75 65 digit(&realvalue
a3fc0 2c 26 6e 73 64 29 3b 0a 20 20 20 20 20 20 20 20 ,&nsd);.
a3fd0 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 52 65 6d }. /* Rem
a3fe0 6f 76 65 20 74 72 61 69 6c 69 6e 67 20 7a 65 72 ove trailing zer
a3ff0 6f 73 20 61 6e 64 20 74 68 65 20 22 2e 22 20 69 os and the "." i
a4000 66 20 6e 6f 20 64 69 67 69 74 73 20 66 6f 6c 6c f no digits foll
a4010 6f 77 20 74 68 65 20 22 2e 22 20 2a 2f 0a 20 20 ow the "." */.
a4020 20 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f 72 if( flag_r
a4030 74 7a 20 26 26 20 66 6c 61 67 5f 64 70 20 29 7b tz && flag_dp ){
a4040 0a 20 20 20 20 20 20 20 20 20 20 77 68 69 6c 65 . while
a4050 28 20 62 75 66 70 74 5b 2d 31 5d 3d 3d 27 30 27 ( bufpt[-1]=='0'
a4060 20 29 20 2a 28 2d 2d 62 75 66 70 74 29 20 3d 20 ) *(--bufpt) =
a4070 30 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 0;. ass
a4080 65 72 74 28 20 62 75 66 70 74 3e 62 75 66 20 29 ert( bufpt>buf )
a4090 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 ;. if(
a40a0 62 75 66 70 74 5b 2d 31 5d 3d 3d 27 2e 27 20 29 bufpt[-1]=='.' )
a40b0 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 {. if
a40c0 28 20 66 6c 61 67 5f 61 6c 74 66 6f 72 6d 32 20 ( flag_altform2
a40d0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ){.
a40e0 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 27 30 *(bufpt++) = '0
a40f0 27 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d ';. }
a4100 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 else{.
a4110 20 20 20 20 2a 28 2d 2d 62 75 66 70 74 29 20 3d *(--bufpt) =
a4120 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 0;.
a4130 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 }. }.
a4140 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
a4150 2f 2a 20 41 64 64 20 74 68 65 20 22 65 4e 4e 4e /* Add the "eNNN
a4160 22 20 73 75 66 66 69 78 20 2a 2f 0a 20 20 20 20 " suffix */.
a4170 20 20 20 20 69 66 28 20 66 6c 61 67 5f 65 78 70 if( flag_exp
a4180 20 7c 7c 20 78 74 79 70 65 3d 3d 65 74 45 58 50 || xtype==etEXP
a4190 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 28 ){. *(
a41a0 62 75 66 70 74 2b 2b 29 20 3d 20 61 44 69 67 69 bufpt++) = aDigi
a41b0 74 73 5b 69 6e 66 6f 70 2d 3e 63 68 61 72 73 65 ts[infop->charse
a41c0 74 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 t];. if
a41d0 28 20 65 78 70 3c 30 20 29 7b 0a 20 20 20 20 20 ( exp<0 ){.
a41e0 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b *(bufpt++
a41f0 29 20 3d 20 27 2d 27 3b 20 65 78 70 20 3d 20 2d ) = '-'; exp = -
a4200 65 78 70 3b 0a 20 20 20 20 20 20 20 20 20 20 7d exp;. }
a4210 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 else{.
a4220 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 27 *(bufpt++) = '
a4230 2b 27 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a +';. }.
a4240 20 20 20 20 20 20 20 20 20 20 69 66 28 20 65 78 if( ex
a4250 70 3e 3d 31 30 30 20 29 7b 0a 20 20 20 20 20 20 p>=100 ){.
a4260 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 *(bufpt++)
a4270 20 3d 20 28 63 68 61 72 29 28 28 65 78 70 2f 31 = (char)((exp/1
a4280 30 30 29 2b 27 30 27 29 3b 20 20 20 20 20 20 20 00)+'0');
a4290 20 2f 2a 20 31 30 30 27 73 20 64 69 67 69 74 20 /* 100's digit
a42a0 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 65 */. e
a42b0 78 70 20 25 3d 20 31 30 30 3b 0a 20 20 20 20 20 xp %= 100;.
a42c0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 }.
a42d0 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 28 63 *(bufpt++) = (c
a42e0 68 61 72 29 28 65 78 70 2f 31 30 2b 27 30 27 29 har)(exp/10+'0')
a42f0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
a4300 20 31 30 27 73 20 64 69 67 69 74 20 2a 2f 0a 20 10's digit */.
a4310 20 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 *(bufpt
a4320 2b 2b 29 20 3d 20 28 63 68 61 72 29 28 65 78 70 ++) = (char)(exp
a4330 25 31 30 2b 27 30 27 29 3b 20 20 20 20 20 20 20 %10+'0');
a4340 20 20 20 20 20 20 2f 2a 20 31 27 73 20 64 69 67 /* 1's dig
a4350 69 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 7d 0a it */. }.
a4360 20 20 20 20 20 20 20 20 2a 62 75 66 70 74 20 3d *bufpt =
a4370 20 30 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 0;.. /*
a4380 54 68 65 20 63 6f 6e 76 65 72 74 65 64 20 6e 75 The converted nu
a4390 6d 62 65 72 20 69 73 20 69 6e 20 62 75 66 5b 5d mber is in buf[]
a43a0 20 61 6e 64 20 7a 65 72 6f 20 74 65 72 6d 69 6e and zero termin
a43b0 61 74 65 64 2e 20 4f 75 74 70 75 74 20 69 74 2e ated. Output it.
a43c0 0a 20 20 20 20 20 20 20 20 2a 2a 20 4e 6f 74 65 . ** Note
a43d0 20 74 68 61 74 20 74 68 65 20 6e 75 6d 62 65 72 that the number
a43e0 20 69 73 20 69 6e 20 74 68 65 20 75 73 75 61 6c is in the usual
a43f0 20 6f 72 64 65 72 2c 20 6e 6f 74 20 72 65 76 65 order, not reve
a4400 72 73 65 64 20 61 73 20 77 69 74 68 0a 20 20 20 rsed as with.
a4410 20 20 20 20 20 2a 2a 20 69 6e 74 65 67 65 72 20 ** integer
a4420 63 6f 6e 76 65 72 73 69 6f 6e 73 2e 20 2a 2f 0a conversions. */.
a4430 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d length =
a4440 20 28 69 6e 74 29 28 62 75 66 70 74 2d 62 75 66 (int)(bufpt-buf
a4450 29 3b 0a 20 20 20 20 20 20 20 20 62 75 66 70 74 );. bufpt
a4460 20 3d 20 62 75 66 3b 0a 0a 20 20 20 20 20 20 20 = buf;..
a4470 20 2f 2a 20 53 70 65 63 69 61 6c 20 63 61 73 65 /* Special case
a4480 3a 20 20 41 64 64 20 6c 65 61 64 69 6e 67 20 7a : Add leading z
a4490 65 72 6f 73 20 69 66 20 74 68 65 20 66 6c 61 67 eros if the flag
a44a0 5f 7a 65 72 6f 70 61 64 20 66 6c 61 67 20 69 73 _zeropad flag is
a44b0 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 65 74 20 . ** set
a44c0 61 6e 64 20 77 65 20 61 72 65 20 6e 6f 74 20 6c and we are not l
a44d0 65 66 74 20 6a 75 73 74 69 66 69 65 64 20 2a 2f eft justified */
a44e0 0a 20 20 20 20 20 20 20 20 69 66 28 20 66 6c 61 . if( fla
a44f0 67 5f 7a 65 72 6f 70 61 64 20 26 26 20 21 66 6c g_zeropad && !fl
a4500 61 67 5f 6c 65 66 74 6a 75 73 74 69 66 79 20 26 ag_leftjustify &
a4510 26 20 6c 65 6e 67 74 68 20 3c 20 77 69 64 74 68 & length < width
a4520 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 ){. int
a4530 20 69 3b 0a 20 20 20 20 20 20 20 20 20 20 69 6e i;. in
a4540 74 20 6e 50 61 64 20 3d 20 77 69 64 74 68 20 2d t nPad = width -
a4550 20 6c 65 6e 67 74 68 3b 0a 20 20 20 20 20 20 20 length;.
a4560 20 20 20 66 6f 72 28 69 3d 77 69 64 74 68 3b 20 for(i=width;
a4570 69 3e 3d 6e 50 61 64 3b 20 69 2d 2d 29 7b 0a 20 i>=nPad; i--){.
a4580 20 20 20 20 20 20 20 20 20 20 20 62 75 66 70 74 bufpt
a4590 5b 69 5d 20 3d 20 62 75 66 70 74 5b 69 2d 6e 50 [i] = bufpt[i-nP
a45a0 61 64 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 7d ad];. }
a45b0 0a 20 20 20 20 20 20 20 20 20 20 69 20 3d 20 70 . i = p
a45c0 72 65 66 69 78 21 3d 30 3b 0a 20 20 20 20 20 20 refix!=0;.
a45d0 20 20 20 20 77 68 69 6c 65 28 20 6e 50 61 64 2d while( nPad-
a45e0 2d 20 29 20 62 75 66 70 74 5b 69 2b 2b 5d 20 3d - ) bufpt[i++] =
a45f0 20 27 30 27 3b 0a 20 20 20 20 20 20 20 20 20 20 '0';.
a4600 6c 65 6e 67 74 68 20 3d 20 77 69 64 74 68 3b 0a length = width;.
a4610 20 20 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 }.#endif
a4620 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a . break;.
a4630 20 20 20 20 20 20 63 61 73 65 20 65 74 53 49 5a case etSIZ
a4640 45 3a 0a 20 20 20 20 20 20 20 20 2a 28 76 61 5f E:. *(va_
a4650 61 72 67 28 61 70 2c 69 6e 74 2a 29 29 20 3d 20 arg(ap,int*)) =
a4660 70 41 63 63 75 6d 2d 3e 6e 43 68 61 72 3b 0a 20 pAccum->nChar;.
a4670 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 length =
a4680 77 69 64 74 68 20 3d 20 30 3b 0a 20 20 20 20 20 width = 0;.
a4690 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
a46a0 63 61 73 65 20 65 74 50 45 52 43 45 4e 54 3a 0a case etPERCENT:.
a46b0 20 20 20 20 20 20 20 20 62 75 66 5b 30 5d 20 3d buf[0] =
a46c0 20 27 25 27 3b 0a 20 20 20 20 20 20 20 20 62 75 '%';. bu
a46d0 66 70 74 20 3d 20 62 75 66 3b 0a 20 20 20 20 20 fpt = buf;.
a46e0 20 20 20 6c 65 6e 67 74 68 20 3d 20 31 3b 0a 20 length = 1;.
a46f0 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
a4700 20 20 20 20 63 61 73 65 20 65 74 43 48 41 52 58 case etCHARX
a4710 3a 0a 20 20 20 20 20 20 20 20 63 20 3d 20 76 61 :. c = va
a4720 5f 61 72 67 28 61 70 2c 69 6e 74 29 3b 0a 20 20 _arg(ap,int);.
a4730 20 20 20 20 20 20 62 75 66 5b 30 5d 20 3d 20 28 buf[0] = (
a4740 63 68 61 72 29 63 3b 0a 20 20 20 20 20 20 20 20 char)c;.
a4750 69 66 28 20 70 72 65 63 69 73 69 6f 6e 3e 3d 30 if( precision>=0
a4760 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66 6f ){. fo
a4770 72 28 69 64 78 3d 31 3b 20 69 64 78 3c 70 72 65 r(idx=1; idx<pre
a4780 63 69 73 69 6f 6e 3b 20 69 64 78 2b 2b 29 20 62 cision; idx++) b
a4790 75 66 5b 69 64 78 5d 20 3d 20 28 63 68 61 72 29 uf[idx] = (char)
a47a0 63 3b 0a 20 20 20 20 20 20 20 20 20 20 6c 65 6e c;. len
a47b0 67 74 68 20 3d 20 70 72 65 63 69 73 69 6f 6e 3b gth = precision;
a47c0 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a . }else{.
a47d0 20 20 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 length
a47e0 20 3d 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 =1;. }.
a47f0 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 62 bufpt = b
a4800 75 66 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 uf;. brea
a4810 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 k;. case et
a4820 53 54 52 49 4e 47 3a 0a 20 20 20 20 20 20 63 61 STRING:. ca
a4830 73 65 20 65 74 44 59 4e 53 54 52 49 4e 47 3a 0a se etDYNSTRING:.
a4840 20 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 bufpt =
a4850 76 61 5f 61 72 67 28 61 70 2c 63 68 61 72 2a 29 va_arg(ap,char*)
a4860 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 62 75 ;. if( bu
a4870 66 70 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 fpt==0 ){.
a4880 20 20 20 20 62 75 66 70 74 20 3d 20 22 22 3b 0a bufpt = "";.
a4890 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 }else if
a48a0 28 20 78 74 79 70 65 3d 3d 65 74 44 59 4e 53 54 ( xtype==etDYNST
a48b0 52 49 4e 47 20 29 7b 0a 20 20 20 20 20 20 20 20 RING ){.
a48c0 20 20 7a 45 78 74 72 61 20 3d 20 62 75 66 70 74 zExtra = bufpt
a48d0 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
a48e0 20 20 20 20 69 66 28 20 70 72 65 63 69 73 69 6f if( precisio
a48f0 6e 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 n>=0 ){.
a4900 20 20 66 6f 72 28 6c 65 6e 67 74 68 3d 30 3b 20 for(length=0;
a4910 6c 65 6e 67 74 68 3c 70 72 65 63 69 73 69 6f 6e length<precision
a4920 20 26 26 20 62 75 66 70 74 5b 6c 65 6e 67 74 68 && bufpt[length
a4930 5d 3b 20 6c 65 6e 67 74 68 2b 2b 29 7b 7d 0a 20 ]; length++){}.
a4940 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }else{.
a4950 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d length =
a4960 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 sqlite3Strlen30
a4970 28 62 75 66 70 74 29 3b 0a 20 20 20 20 20 20 20 (bufpt);.
a4980 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b }. break
a4990 3b 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 53 ;. case etS
a49a0 51 4c 45 53 43 41 50 45 3a 0a 20 20 20 20 20 20 QLESCAPE:.
a49b0 63 61 73 65 20 65 74 53 51 4c 45 53 43 41 50 45 case etSQLESCAPE
a49c0 32 3a 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 2:. case et
a49d0 53 51 4c 45 53 43 41 50 45 33 3a 20 7b 0a 20 20 SQLESCAPE3: {.
a49e0 20 20 20 20 20 20 69 6e 74 20 69 2c 20 6a 2c 20 int i, j,
a49f0 6e 2c 20 69 73 6e 75 6c 6c 3b 0a 20 20 20 20 20 n, isnull;.
a4a00 20 20 20 69 6e 74 20 6e 65 65 64 51 75 6f 74 65 int needQuote
a4a10 3b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 63 ;. char c
a4a20 68 3b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 h;. char
a4a30 71 20 3d 20 28 28 78 74 79 70 65 3d 3d 65 74 53 q = ((xtype==etS
a4a40 51 4c 45 53 43 41 50 45 33 29 3f 27 22 27 3a 27 QLESCAPE3)?'"':'
a4a50 5c 27 27 29 3b 20 20 20 2f 2a 20 51 75 6f 74 65 \''); /* Quote
a4a60 20 63 68 61 72 61 63 74 65 72 20 2a 2f 0a 20 20 character */.
a4a70 20 20 20 20 20 20 63 68 61 72 20 2a 65 73 63 61 char *esca
a4a80 72 67 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 63 rg = va_arg(ap,c
a4a90 68 61 72 2a 29 3b 0a 20 20 20 20 20 20 20 20 69 har*);. i
a4aa0 73 6e 75 6c 6c 20 3d 20 65 73 63 61 72 67 3d 3d snull = escarg==
a4ab0 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 0;. if( i
a4ac0 73 6e 75 6c 6c 20 29 20 65 73 63 61 72 67 20 3d snull ) escarg =
a4ad0 20 28 78 74 79 70 65 3d 3d 65 74 53 51 4c 45 53 (xtype==etSQLES
a4ae0 43 41 50 45 32 20 3f 20 22 4e 55 4c 4c 22 20 3a CAPE2 ? "NULL" :
a4af0 20 22 28 4e 55 4c 4c 29 22 29 3b 0a 20 20 20 20 "(NULL)");.
a4b00 20 20 20 20 66 6f 72 28 69 3d 6e 3d 30 3b 20 28 for(i=n=0; (
a4b10 63 68 3d 65 73 63 61 72 67 5b 69 5d 29 21 3d 30 ch=escarg[i])!=0
a4b20 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 ; i++){.
a4b30 20 20 69 66 28 20 63 68 3d 3d 71 20 29 20 20 6e if( ch==q ) n
a4b40 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 ++;. }.
a4b50 20 20 20 20 20 20 6e 65 65 64 51 75 6f 74 65 20 needQuote
a4b60 3d 20 21 69 73 6e 75 6c 6c 20 26 26 20 78 74 79 = !isnull && xty
a4b70 70 65 3d 3d 65 74 53 51 4c 45 53 43 41 50 45 32 pe==etSQLESCAPE2
a4b80 3b 0a 20 20 20 20 20 20 20 20 6e 20 2b 3d 20 69 ;. n += i
a4b90 20 2b 20 31 20 2b 20 6e 65 65 64 51 75 6f 74 65 + 1 + needQuote
a4ba0 2a 32 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 *2;. if(
a4bb0 6e 3e 65 74 42 55 46 53 49 5a 45 20 29 7b 0a 20 n>etBUFSIZE ){.
a4bc0 20 20 20 20 20 20 20 20 20 62 75 66 70 74 20 3d bufpt =
a4bd0 20 7a 45 78 74 72 61 20 3d 20 73 71 6c 69 74 65 zExtra = sqlite
a4be0 33 4d 61 6c 6c 6f 63 28 20 6e 20 29 3b 0a 20 20 3Malloc( n );.
a4bf0 20 20 20 20 20 20 20 20 69 66 28 20 62 75 66 70 if( bufp
a4c00 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 t==0 ){.
a4c10 20 20 20 20 70 41 63 63 75 6d 2d 3e 6d 61 6c 6c pAccum->mall
a4c20 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 ocFailed = 1;.
a4c30 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e return
a4c40 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
a4c50 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
a4c60 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 62 bufpt = b
a4c70 75 66 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 uf;. }.
a4c80 20 20 20 20 20 20 6a 20 3d 20 30 3b 0a 20 20 20 j = 0;.
a4c90 20 20 20 20 20 69 66 28 20 6e 65 65 64 51 75 6f if( needQuo
a4ca0 74 65 20 29 20 62 75 66 70 74 5b 6a 2b 2b 5d 20 te ) bufpt[j++]
a4cb0 3d 20 71 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 = q;. for
a4cc0 28 69 3d 30 3b 20 28 63 68 3d 65 73 63 61 72 67 (i=0; (ch=escarg
a4cd0 5b 69 5d 29 21 3d 30 3b 20 69 2b 2b 29 7b 0a 20 [i])!=0; i++){.
a4ce0 20 20 20 20 20 20 20 20 20 62 75 66 70 74 5b 6a bufpt[j
a4cf0 2b 2b 5d 20 3d 20 63 68 3b 0a 20 20 20 20 20 20 ++] = ch;.
a4d00 20 20 20 20 69 66 28 20 63 68 3d 3d 71 20 29 20 if( ch==q )
a4d10 62 75 66 70 74 5b 6a 2b 2b 5d 20 3d 20 63 68 3b bufpt[j++] = ch;
a4d20 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
a4d30 20 20 20 69 66 28 20 6e 65 65 64 51 75 6f 74 65 if( needQuote
a4d40 20 29 20 62 75 66 70 74 5b 6a 2b 2b 5d 20 3d 20 ) bufpt[j++] =
a4d50 71 3b 0a 20 20 20 20 20 20 20 20 62 75 66 70 74 q;. bufpt
a4d60 5b 6a 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 [j] = 0;.
a4d70 20 6c 65 6e 67 74 68 20 3d 20 6a 3b 0a 20 20 20 length = j;.
a4d80 20 20 20 20 20 2f 2a 20 54 68 65 20 70 72 65 63 /* The prec
a4d90 69 73 69 6f 6e 20 69 73 20 69 67 6e 6f 72 65 64 ision is ignored
a4da0 20 6f 6e 20 25 71 20 61 6e 64 20 25 51 20 2a 2f on %q and %Q */
a4db0 0a 20 20 20 20 20 20 20 20 2f 2a 20 69 66 28 20 . /* if(
a4dc0 70 72 65 63 69 73 69 6f 6e 3e 3d 30 20 26 26 20 precision>=0 &&
a4dd0 70 72 65 63 69 73 69 6f 6e 3c 6c 65 6e 67 74 68 precision<length
a4de0 20 29 20 6c 65 6e 67 74 68 20 3d 20 70 72 65 63 ) length = prec
a4df0 69 73 69 6f 6e 3b 20 2a 2f 0a 20 20 20 20 20 20 ision; */.
a4e00 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d break;. }
a4e10 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 54 4f . case etTO
a4e20 4b 45 4e 3a 20 7b 0a 20 20 20 20 20 20 20 20 54 KEN: {. T
a4e30 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 20 3d 20 76 oken *pToken = v
a4e40 61 5f 61 72 67 28 61 70 2c 20 54 6f 6b 65 6e 2a a_arg(ap, Token*
a4e50 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 );. if( p
a4e60 54 6f 6b 65 6e 20 29 7b 0a 20 20 20 20 20 20 20 Token ){.
a4e70 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 sqlite3StrAcc
a4e80 75 6d 41 70 70 65 6e 64 28 70 41 63 63 75 6d 2c umAppend(pAccum,
a4e90 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 70 54 (const char*)pT
a4ea0 6f 6b 65 6e 2d 3e 7a 2c 20 70 54 6f 6b 65 6e 2d oken->z, pToken-
a4eb0 3e 6e 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 >n);. }.
a4ec0 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 length =
a4ed0 77 69 64 74 68 20 3d 20 30 3b 0a 20 20 20 20 20 width = 0;.
a4ee0 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
a4ef0 7d 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 53 }. case etS
a4f00 52 43 4c 49 53 54 3a 20 7b 0a 20 20 20 20 20 20 RCLIST: {.
a4f10 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 20 SrcList *pSrc
a4f20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 53 72 63 = va_arg(ap, Src
a4f30 4c 69 73 74 2a 29 3b 0a 20 20 20 20 20 20 20 20 List*);.
a4f40 69 6e 74 20 6b 20 3d 20 76 61 5f 61 72 67 28 61 int k = va_arg(a
a4f50 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 p, int);.
a4f60 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f struct SrcList_
a4f70 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d 20 26 70 item *pItem = &p
a4f80 53 72 63 2d 3e 61 5b 6b 5d 3b 0a 20 20 20 20 20 Src->a[k];.
a4f90 20 20 20 61 73 73 65 72 74 28 20 6b 3e 3d 30 20 assert( k>=0
a4fa0 26 26 20 6b 3c 70 53 72 63 2d 3e 6e 53 72 63 20 && k<pSrc->nSrc
a4fb0 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 );. if( p
a4fc0 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65 20 Item->zDatabase
a4fd0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c ){. sql
a4fe0 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 ite3StrAccumAppe
a4ff0 6e 64 28 70 41 63 63 75 6d 2c 20 70 49 74 65 6d nd(pAccum, pItem
a5000 2d 3e 7a 44 61 74 61 62 61 73 65 2c 20 2d 31 29 ->zDatabase, -1)
a5010 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 ;. sqli
a5020 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e te3StrAccumAppen
a5030 64 28 70 41 63 63 75 6d 2c 20 22 2e 22 2c 20 31 d(pAccum, ".", 1
a5040 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 );. }.
a5050 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 sqlite3StrA
a5060 63 63 75 6d 41 70 70 65 6e 64 28 70 41 63 63 75 ccumAppend(pAccu
a5070 6d 2c 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 2c m, pItem->zName,
a5080 20 2d 31 29 3b 0a 20 20 20 20 20 20 20 20 6c 65 -1);. le
a5090 6e 67 74 68 20 3d 20 77 69 64 74 68 20 3d 20 30 ngth = width = 0
a50a0 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b ;. break;
a50b0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 64 . }. d
a50c0 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 efault: {.
a50d0 20 20 61 73 73 65 72 74 28 20 78 74 79 70 65 3d assert( xtype=
a50e0 3d 65 74 49 4e 56 41 4c 49 44 20 29 3b 0a 20 20 =etINVALID );.
a50f0 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 return;.
a5100 20 20 20 20 7d 0a 20 20 20 20 7d 2f 2a 20 45 6e }. }/* En
a5110 64 20 73 77 69 74 63 68 20 6f 76 65 72 20 74 68 d switch over th
a5120 65 20 66 6f 72 6d 61 74 20 74 79 70 65 20 2a 2f e format type */
a5130 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 54 . /*. ** T
a5140 68 65 20 74 65 78 74 20 6f 66 20 74 68 65 20 63 he text of the c
a5150 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20 70 6f 69 onversion is poi
a5160 6e 74 65 64 20 74 6f 20 62 79 20 22 62 75 66 70 nted to by "bufp
a5170 74 22 20 61 6e 64 20 69 73 0a 20 20 20 20 2a 2a t" and is. **
a5180 20 22 6c 65 6e 67 74 68 22 20 63 68 61 72 61 63 "length" charac
a5190 74 65 72 73 20 6c 6f 6e 67 2e 20 20 54 68 65 20 ters long. The
a51a0 66 69 65 6c 64 20 77 69 64 74 68 20 69 73 20 22 field width is "
a51b0 77 69 64 74 68 22 2e 20 20 44 6f 0a 20 20 20 20 width". Do.
a51c0 2a 2a 20 74 68 65 20 6f 75 74 70 75 74 2e 0a 20 ** the output..
a51d0 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 66 */. if( !f
a51e0 6c 61 67 5f 6c 65 66 74 6a 75 73 74 69 66 79 20 lag_leftjustify
a51f0 29 7b 0a 20 20 20 20 20 20 72 65 67 69 73 74 65 ){. registe
a5200 72 20 69 6e 74 20 6e 73 70 61 63 65 3b 0a 20 20 r int nspace;.
a5210 20 20 20 20 6e 73 70 61 63 65 20 3d 20 77 69 64 nspace = wid
a5220 74 68 2d 6c 65 6e 67 74 68 3b 0a 20 20 20 20 20 th-length;.
a5230 20 69 66 28 20 6e 73 70 61 63 65 3e 30 20 29 7b if( nspace>0 ){
a5240 0a 20 20 20 20 20 20 20 20 61 70 70 65 6e 64 53 . appendS
a5250 70 61 63 65 28 70 41 63 63 75 6d 2c 20 6e 73 70 pace(pAccum, nsp
a5260 61 63 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 ace);. }.
a5270 20 20 7d 0a 20 20 20 20 69 66 28 20 6c 65 6e 67 }. if( leng
a5280 74 68 3e 30 20 29 7b 0a 20 20 20 20 20 20 73 71 th>0 ){. sq
a5290 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 lite3StrAccumApp
a52a0 65 6e 64 28 70 41 63 63 75 6d 2c 20 62 75 66 70 end(pAccum, bufp
a52b0 74 2c 20 6c 65 6e 67 74 68 29 3b 0a 20 20 20 20 t, length);.
a52c0 7d 0a 20 20 20 20 69 66 28 20 66 6c 61 67 5f 6c }. if( flag_l
a52d0 65 66 74 6a 75 73 74 69 66 79 20 29 7b 0a 20 20 eftjustify ){.
a52e0 20 20 20 20 72 65 67 69 73 74 65 72 20 69 6e 74 register int
a52f0 20 6e 73 70 61 63 65 3b 0a 20 20 20 20 20 20 6e nspace;. n
a5300 73 70 61 63 65 20 3d 20 77 69 64 74 68 2d 6c 65 space = width-le
a5310 6e 67 74 68 3b 0a 20 20 20 20 20 20 69 66 28 20 ngth;. if(
a5320 6e 73 70 61 63 65 3e 30 20 29 7b 0a 20 20 20 20 nspace>0 ){.
a5330 20 20 20 20 61 70 70 65 6e 64 53 70 61 63 65 28 appendSpace(
a5340 70 41 63 63 75 6d 2c 20 6e 73 70 61 63 65 29 3b pAccum, nspace);
a5350 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
a5360 20 20 20 69 66 28 20 7a 45 78 74 72 61 20 29 7b if( zExtra ){
a5370 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 . sqlite3_f
a5380 72 65 65 28 7a 45 78 74 72 61 29 3b 0a 20 20 20 ree(zExtra);.
a5390 20 7d 0a 20 20 7d 2f 2a 20 45 6e 64 20 66 6f 72 }. }/* End for
a53a0 20 6c 6f 6f 70 20 6f 76 65 72 20 74 68 65 20 66 loop over the f
a53b0 6f 72 6d 61 74 20 73 74 72 69 6e 67 20 2a 2f 0a ormat string */.
a53c0 7d 20 2f 2a 20 45 6e 64 20 6f 66 20 66 75 6e 63 } /* End of func
a53d0 74 69 6f 6e 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 tion */../*.** A
a53e0 70 70 65 6e 64 20 4e 20 62 79 74 65 73 20 6f 66 ppend N bytes of
a53f0 20 74 65 78 74 20 66 72 6f 6d 20 7a 20 74 6f 20 text from z to
a5400 74 68 65 20 53 74 72 41 63 63 75 6d 20 6f 62 6a the StrAccum obj
a5410 65 63 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ect..*/.SQLITE_P
a5420 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
a5430 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e te3StrAccumAppen
a5440 64 28 53 74 72 41 63 63 75 6d 20 2a 70 2c 20 63 d(StrAccum *p, c
a5450 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e onst char *z, in
a5460 74 20 4e 29 7b 0a 20 20 61 73 73 65 72 74 28 20 t N){. assert(
a5470 7a 21 3d 30 20 7c 7c 20 4e 3d 3d 30 20 29 3b 0a z!=0 || N==0 );.
a5480 20 20 69 66 28 20 70 2d 3e 74 6f 6f 42 69 67 20 if( p->tooBig
a5490 7c 20 70 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 | p->mallocFaile
a54a0 64 20 29 7b 0a 20 20 20 20 74 65 73 74 63 61 73 d ){. testcas
a54b0 65 28 70 2d 3e 74 6f 6f 42 69 67 29 3b 0a 20 20 e(p->tooBig);.
a54c0 20 20 74 65 73 74 63 61 73 65 28 70 2d 3e 6d 61 testcase(p->ma
a54d0 6c 6c 6f 63 46 61 69 6c 65 64 29 3b 0a 20 20 20 llocFailed);.
a54e0 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 return;. }. i
a54f0 66 28 20 4e 3c 30 20 29 7b 0a 20 20 20 20 4e 20 f( N<0 ){. N
a5500 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 = sqlite3Strlen3
a5510 30 28 7a 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 0(z);. }. if(
a5520 4e 3d 3d 30 20 7c 7c 20 4e 45 56 45 52 28 7a 3d N==0 || NEVER(z=
a5530 3d 30 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 =0) ){. retur
a5540 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e n;. }. if( p->
a5550 6e 43 68 61 72 2b 4e 20 3e 3d 20 70 2d 3e 6e 41 nChar+N >= p->nA
a5560 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 63 68 61 72 lloc ){. char
a5570 20 2a 7a 4e 65 77 3b 0a 20 20 20 20 69 66 28 20 *zNew;. if(
a5580 21 70 2d 3e 75 73 65 4d 61 6c 6c 6f 63 20 29 7b !p->useMalloc ){
a5590 0a 20 20 20 20 20 20 70 2d 3e 74 6f 6f 42 69 67 . p->tooBig
a55a0 20 3d 20 31 3b 0a 20 20 20 20 20 20 4e 20 3d 20 = 1;. N =
a55b0 70 2d 3e 6e 41 6c 6c 6f 63 20 2d 20 70 2d 3e 6e p->nAlloc - p->n
a55c0 43 68 61 72 20 2d 20 31 3b 0a 20 20 20 20 20 20 Char - 1;.
a55d0 69 66 28 20 4e 3c 3d 30 20 29 7b 0a 20 20 20 20 if( N<=0 ){.
a55e0 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 return;.
a55f0 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }. }else{.
a5600 20 20 20 20 20 69 36 34 20 73 7a 4e 65 77 20 3d i64 szNew =
a5610 20 70 2d 3e 6e 43 68 61 72 3b 0a 20 20 20 20 20 p->nChar;.
a5620 20 73 7a 4e 65 77 20 2b 3d 20 4e 20 2b 20 31 3b szNew += N + 1;
a5630 0a 20 20 20 20 20 20 69 66 28 20 73 7a 4e 65 77 . if( szNew
a5640 20 3e 20 70 2d 3e 6d 78 41 6c 6c 6f 63 20 29 7b > p->mxAlloc ){
a5650 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
a5660 53 74 72 41 63 63 75 6d 52 65 73 65 74 28 70 29 StrAccumReset(p)
a5670 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 74 6f 6f ;. p->too
a5680 42 69 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 Big = 1;.
a5690 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d return;. }
a56a0 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 2d else{. p-
a56b0 3e 6e 41 6c 6c 6f 63 20 3d 20 28 69 6e 74 29 73 >nAlloc = (int)s
a56c0 7a 4e 65 77 3b 0a 20 20 20 20 20 20 7d 0a 20 20 zNew;. }.
a56d0 20 20 20 20 7a 4e 65 77 20 3d 20 73 71 6c 69 74 zNew = sqlit
a56e0 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 70 2d e3DbMallocRaw(p-
a56f0 3e 64 62 2c 20 70 2d 3e 6e 41 6c 6c 6f 63 20 29 >db, p->nAlloc )
a5700 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 4e 65 77 ;. if( zNew
a5710 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 ){. memc
a5720 70 79 28 7a 4e 65 77 2c 20 70 2d 3e 7a 54 65 78 py(zNew, p->zTex
a5730 74 2c 20 70 2d 3e 6e 43 68 61 72 29 3b 0a 20 20 t, p->nChar);.
a5740 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 sqlite3Str
a5750 41 63 63 75 6d 52 65 73 65 74 28 70 29 3b 0a 20 AccumReset(p);.
a5760 20 20 20 20 20 20 20 70 2d 3e 7a 54 65 78 74 20 p->zText
a5770 3d 20 7a 4e 65 77 3b 0a 20 20 20 20 20 20 7d 65 = zNew;. }e
a5780 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e lse{. p->
a5790 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 mallocFailed = 1
a57a0 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 ;. sqlite
a57b0 33 53 74 72 41 63 63 75 6d 52 65 73 65 74 28 70 3StrAccumReset(p
a57c0 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 );. retur
a57d0 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d n;. }. }
a57e0 0a 20 20 7d 0a 20 20 6d 65 6d 63 70 79 28 26 70 . }. memcpy(&p
a57f0 2d 3e 7a 54 65 78 74 5b 70 2d 3e 6e 43 68 61 72 ->zText[p->nChar
a5800 5d 2c 20 7a 2c 20 4e 29 3b 0a 20 20 70 2d 3e 6e ], z, N);. p->n
a5810 43 68 61 72 20 2b 3d 20 4e 3b 0a 7d 0a 0a 2f 2a Char += N;.}../*
a5820 0a 2a 2a 20 46 69 6e 69 73 68 20 6f 66 66 20 61 .** Finish off a
a5830 20 73 74 72 69 6e 67 20 62 79 20 6d 61 6b 69 6e string by makin
a5840 67 20 73 75 72 65 20 69 74 20 69 73 20 7a 65 72 g sure it is zer
a5850 6f 2d 74 65 72 6d 69 6e 61 74 65 64 2e 0a 2a 2a o-terminated..**
a5860 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 Return a pointe
a5870 72 20 74 6f 20 74 68 65 20 72 65 73 75 6c 74 69 r to the resulti
a5880 6e 67 20 73 74 72 69 6e 67 2e 20 20 52 65 74 75 ng string. Retu
a5890 72 6e 20 61 20 4e 55 4c 4c 0a 2a 2a 20 70 6f 69 rn a NULL.** poi
a58a0 6e 74 65 72 20 69 66 20 61 6e 79 20 6b 69 6e 64 nter if any kind
a58b0 20 6f 66 20 65 72 72 6f 72 20 77 61 73 20 65 6e of error was en
a58c0 63 6f 75 6e 74 65 72 65 64 2e 0a 2a 2f 0a 53 51 countered..*/.SQ
a58d0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 LITE_PRIVATE cha
a58e0 72 20 2a 73 71 6c 69 74 65 33 53 74 72 41 63 63 r *sqlite3StrAcc
a58f0 75 6d 46 69 6e 69 73 68 28 53 74 72 41 63 63 75 umFinish(StrAccu
a5900 6d 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e m *p){. if( p->
a5910 7a 54 65 78 74 20 29 7b 0a 20 20 20 20 70 2d 3e zText ){. p->
a5920 7a 54 65 78 74 5b 70 2d 3e 6e 43 68 61 72 5d 20 zText[p->nChar]
a5930 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e = 0;. if( p->
a5940 75 73 65 4d 61 6c 6c 6f 63 20 26 26 20 70 2d 3e useMalloc && p->
a5950 7a 54 65 78 74 3d 3d 70 2d 3e 7a 42 61 73 65 20 zText==p->zBase
a5960 29 7b 0a 20 20 20 20 20 20 70 2d 3e 7a 54 65 78 ){. p->zTex
a5970 74 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c t = sqlite3DbMal
a5980 6c 6f 63 52 61 77 28 70 2d 3e 64 62 2c 20 70 2d locRaw(p->db, p-
a5990 3e 6e 43 68 61 72 2b 31 20 29 3b 0a 20 20 20 20 >nChar+1 );.
a59a0 20 20 69 66 28 20 70 2d 3e 7a 54 65 78 74 20 29 if( p->zText )
a59b0 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 {. memcpy
a59c0 28 70 2d 3e 7a 54 65 78 74 2c 20 70 2d 3e 7a 42 (p->zText, p->zB
a59d0 61 73 65 2c 20 70 2d 3e 6e 43 68 61 72 2b 31 29 ase, p->nChar+1)
a59e0 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ;. }else{.
a59f0 20 20 20 20 20 20 20 70 2d 3e 6d 61 6c 6c 6f 63 p->malloc
a5a00 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 Failed = 1;.
a5a10 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 }. }. }.
a5a20 72 65 74 75 72 6e 20 70 2d 3e 7a 54 65 78 74 3b return p->zText;
a5a30 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 65 74 20 .}../*.** Reset
a5a40 61 6e 20 53 74 72 41 63 63 75 6d 20 73 74 72 69 an StrAccum stri
a5a50 6e 67 2e 20 20 52 65 63 6c 61 69 6d 20 61 6c 6c ng. Reclaim all
a5a60 20 6d 61 6c 6c 6f 63 65 64 20 6d 65 6d 6f 72 79 malloced memory
a5a70 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
a5a80 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
a5a90 53 74 72 41 63 63 75 6d 52 65 73 65 74 28 53 74 StrAccumReset(St
a5aa0 72 41 63 63 75 6d 20 2a 70 29 7b 0a 20 20 69 66 rAccum *p){. if
a5ab0 28 20 70 2d 3e 7a 54 65 78 74 21 3d 70 2d 3e 7a ( p->zText!=p->z
a5ac0 42 61 73 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 Base ){. sqli
a5ad0 74 65 33 44 62 46 72 65 65 28 70 2d 3e 64 62 2c te3DbFree(p->db,
a5ae0 20 70 2d 3e 7a 54 65 78 74 29 3b 0a 20 20 7d 0a p->zText);. }.
a5af0 20 20 70 2d 3e 7a 54 65 78 74 20 3d 20 30 3b 0a p->zText = 0;.
a5b00 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c }../*.** Initial
a5b10 69 7a 65 20 61 20 73 74 72 69 6e 67 20 61 63 63 ize a string acc
a5b20 75 6d 75 6c 61 74 6f 72 0a 2a 2f 0a 53 51 4c 49 umulator.*/.SQLI
a5b30 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
a5b40 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 49 sqlite3StrAccumI
a5b50 6e 69 74 28 53 74 72 41 63 63 75 6d 20 2a 70 2c nit(StrAccum *p,
a5b60 20 63 68 61 72 20 2a 7a 42 61 73 65 2c 20 69 6e char *zBase, in
a5b70 74 20 6e 2c 20 69 6e 74 20 6d 78 29 7b 0a 20 20 t n, int mx){.
a5b80 70 2d 3e 7a 54 65 78 74 20 3d 20 70 2d 3e 7a 42 p->zText = p->zB
a5b90 61 73 65 20 3d 20 7a 42 61 73 65 3b 0a 20 20 70 ase = zBase;. p
a5ba0 2d 3e 64 62 20 3d 20 30 3b 0a 20 20 70 2d 3e 6e ->db = 0;. p->n
a5bb0 43 68 61 72 20 3d 20 30 3b 0a 20 20 70 2d 3e 6e Char = 0;. p->n
a5bc0 41 6c 6c 6f 63 20 3d 20 6e 3b 0a 20 20 70 2d 3e Alloc = n;. p->
a5bd0 6d 78 41 6c 6c 6f 63 20 3d 20 6d 78 3b 0a 20 20 mxAlloc = mx;.
a5be0 70 2d 3e 75 73 65 4d 61 6c 6c 6f 63 20 3d 20 31 p->useMalloc = 1
a5bf0 3b 0a 20 20 70 2d 3e 74 6f 6f 42 69 67 20 3d 20 ;. p->tooBig =
a5c00 30 3b 0a 20 20 70 2d 3e 6d 61 6c 6c 6f 63 46 61 0;. p->mallocFa
a5c10 69 6c 65 64 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a iled = 0;.}../*.
a5c20 2a 2a 20 50 72 69 6e 74 20 69 6e 74 6f 20 6d 65 ** Print into me
a5c30 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 mory obtained fr
a5c40 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 om sqliteMalloc(
a5c50 29 2e 20 20 55 73 65 20 74 68 65 20 69 6e 74 65 ). Use the inte
a5c60 72 6e 61 6c 0a 2a 2a 20 25 2d 63 6f 6e 76 65 72 rnal.** %-conver
a5c70 73 69 6f 6e 20 65 78 74 65 6e 73 69 6f 6e 73 2e sion extensions.
a5c80 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
a5c90 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 TE char *sqlite3
a5ca0 56 4d 50 72 69 6e 74 66 28 73 71 6c 69 74 65 33 VMPrintf(sqlite3
a5cb0 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 *db, const char
a5cc0 20 2a 7a 46 6f 72 6d 61 74 2c 20 76 61 5f 6c 69 *zFormat, va_li
a5cd0 73 74 20 61 70 29 7b 0a 20 20 63 68 61 72 20 2a st ap){. char *
a5ce0 7a 3b 0a 20 20 63 68 61 72 20 7a 42 61 73 65 5b z;. char zBase[
a5cf0 53 51 4c 49 54 45 5f 50 52 49 4e 54 5f 42 55 46 SQLITE_PRINT_BUF
a5d00 5f 53 49 5a 45 5d 3b 0a 20 20 53 74 72 41 63 63 _SIZE];. StrAcc
a5d10 75 6d 20 61 63 63 3b 0a 20 20 61 73 73 65 72 74 um acc;. assert
a5d20 28 20 64 62 21 3d 30 20 29 3b 0a 20 20 73 71 6c ( db!=0 );. sql
a5d30 69 74 65 33 53 74 72 41 63 63 75 6d 49 6e 69 74 ite3StrAccumInit
a5d40 28 26 61 63 63 2c 20 7a 42 61 73 65 2c 20 73 69 (&acc, zBase, si
a5d50 7a 65 6f 66 28 7a 42 61 73 65 29 2c 0a 20 20 20 zeof(zBase),.
a5d60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a5d70 20 20 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 db->aLimit[SQ
a5d80 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 LITE_LIMIT_LENGT
a5d90 48 5d 29 3b 0a 20 20 61 63 63 2e 64 62 20 3d 20 H]);. acc.db =
a5da0 64 62 3b 0a 20 20 73 71 6c 69 74 65 33 56 58 50 db;. sqlite3VXP
a5db0 72 69 6e 74 66 28 26 61 63 63 2c 20 31 2c 20 7a rintf(&acc, 1, z
a5dc0 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 7a Format, ap);. z
a5dd0 20 3d 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 = sqlite3StrAcc
a5de0 75 6d 46 69 6e 69 73 68 28 26 61 63 63 29 3b 0a umFinish(&acc);.
a5df0 20 20 69 66 28 20 61 63 63 2e 6d 61 6c 6c 6f 63 if( acc.malloc
a5e00 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 64 62 Failed ){. db
a5e10 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d ->mallocFailed =
a5e20 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 1;. }. return
a5e30 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 69 z;.}../*.** Pri
a5e40 6e 74 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f nt into memory o
a5e50 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c btained from sql
a5e60 69 74 65 4d 61 6c 6c 6f 63 28 29 2e 20 20 55 73 iteMalloc(). Us
a5e70 65 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 0a 2a e the internal.*
a5e80 2a 20 25 2d 63 6f 6e 76 65 72 73 69 6f 6e 20 65 * %-conversion e
a5e90 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 xtensions..*/.SQ
a5ea0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 LITE_PRIVATE cha
a5eb0 72 20 2a 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 r *sqlite3MPrint
a5ec0 66 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 f(sqlite3 *db, c
a5ed0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d onst char *zForm
a5ee0 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c at, ...){. va_l
a5ef0 69 73 74 20 61 70 3b 0a 20 20 63 68 61 72 20 2a ist ap;. char *
a5f00 7a 3b 0a 20 20 76 61 5f 73 74 61 72 74 28 61 70 z;. va_start(ap
a5f10 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 7a 20 , zFormat);. z
a5f20 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 = sqlite3VMPrint
a5f30 66 28 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 f(db, zFormat, a
a5f40 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 p);. va_end(ap)
a5f50 3b 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a ;. return z;.}.
a5f60 0a 2f 2a 0a 2a 2a 20 4c 69 6b 65 20 73 71 6c 69 ./*.** Like sqli
a5f70 74 65 33 4d 50 72 69 6e 74 66 28 29 2c 20 62 75 te3MPrintf(), bu
a5f80 74 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 44 62 t call sqlite3Db
a5f90 46 72 65 65 28 29 20 6f 6e 20 7a 53 74 72 20 61 Free() on zStr a
a5fa0 66 74 65 72 20 66 6f 72 6d 61 74 74 69 6e 67 0a fter formatting.
a5fb0 2a 2a 20 74 68 65 20 73 74 72 69 6e 67 20 61 6e ** the string an
a5fc0 64 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 6e d before returnn
a5fd0 69 6e 67 2e 20 20 54 68 69 73 20 72 6f 75 74 69 ing. This routi
a5fe0 6e 65 20 69 73 20 69 6e 74 65 6e 64 65 64 20 74 ne is intended t
a5ff0 6f 20 62 65 20 75 73 65 64 0a 2a 2a 20 74 6f 20 o be used.** to
a6000 6d 6f 64 69 66 79 20 61 6e 20 65 78 69 73 74 69 modify an existi
a6010 6e 67 20 73 74 72 69 6e 67 2e 20 20 46 6f 72 20 ng string. For
a6020 65 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 example:.**.**
a6030 20 20 20 20 20 78 20 3d 20 73 71 6c 69 74 65 33 x = sqlite3
a6040 4d 50 72 69 6e 74 66 28 64 62 2c 20 78 2c 20 22 MPrintf(db, x, "
a6050 70 72 65 66 69 78 20 25 73 20 73 75 66 66 69 78 prefix %s suffix
a6060 22 2c 20 78 29 3b 0a 2a 2a 0a 2a 2f 0a 53 51 4c ", x);.**.*/.SQL
a6070 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 ITE_PRIVATE char
a6080 20 2a 73 71 6c 69 74 65 33 4d 41 70 70 65 6e 64 *sqlite3MAppend
a6090 66 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 f(sqlite3 *db, c
a60a0 68 61 72 20 2a 7a 53 74 72 2c 20 63 6f 6e 73 74 har *zStr, const
a60b0 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 char *zFormat,
a60c0 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 ...){. va_list
a60d0 61 70 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 ap;. char *z;.
a60e0 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 va_start(ap, zF
a60f0 6f 72 6d 61 74 29 3b 0a 20 20 7a 20 3d 20 73 71 ormat);. z = sq
a6100 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 64 62 lite3VMPrintf(db
a6110 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a , zFormat, ap);.
a6120 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 va_end(ap);.
a6130 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
a6140 2c 20 7a 53 74 72 29 3b 0a 20 20 72 65 74 75 72 , zStr);. retur
a6150 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 n z;.}../*.** Pr
a6160 69 6e 74 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 int into memory
a6170 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 obtained from sq
a6180 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e 20 lite3_malloc().
a6190 20 4f 6d 69 74 20 74 68 65 20 69 6e 74 65 72 6e Omit the intern
a61a0 61 6c 0a 2a 2a 20 25 2d 63 6f 6e 76 65 72 73 69 al.** %-conversi
a61b0 6f 6e 20 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a on extensions..*
a61c0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 68 61 /.SQLITE_API cha
a61d0 72 20 2a 73 71 6c 69 74 65 33 5f 76 6d 70 72 69 r *sqlite3_vmpri
a61e0 6e 74 66 28 63 6f 6e 73 74 20 63 68 61 72 20 2a ntf(const char *
a61f0 7a 46 6f 72 6d 61 74 2c 20 76 61 5f 6c 69 73 74 zFormat, va_list
a6200 20 61 70 29 7b 0a 20 20 63 68 61 72 20 2a 7a 3b ap){. char *z;
a6210 0a 20 20 63 68 61 72 20 7a 42 61 73 65 5b 53 51 . char zBase[SQ
a6220 4c 49 54 45 5f 50 52 49 4e 54 5f 42 55 46 5f 53 LITE_PRINT_BUF_S
a6230 49 5a 45 5d 3b 0a 20 20 53 74 72 41 63 63 75 6d IZE];. StrAccum
a6240 20 61 63 63 3b 0a 23 69 66 6e 64 65 66 20 53 51 acc;.#ifndef SQ
a6250 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e LITE_OMIT_AUTOIN
a6260 49 54 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 IT. if( sqlite3
a6270 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 29 20 _initialize() )
a6280 72 65 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66 return 0;.#endif
a6290 0a 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 . sqlite3StrAcc
a62a0 75 6d 49 6e 69 74 28 26 61 63 63 2c 20 7a 42 61 umInit(&acc, zBa
a62b0 73 65 2c 20 73 69 7a 65 6f 66 28 7a 42 61 73 65 se, sizeof(zBase
a62c0 29 2c 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 ), SQLITE_MAX_LE
a62d0 4e 47 54 48 29 3b 0a 20 20 73 71 6c 69 74 65 33 NGTH);. sqlite3
a62e0 56 58 50 72 69 6e 74 66 28 26 61 63 63 2c 20 30 VXPrintf(&acc, 0
a62f0 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a , zFormat, ap);.
a6300 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 53 74 72 z = sqlite3Str
a6310 41 63 63 75 6d 46 69 6e 69 73 68 28 26 61 63 63 AccumFinish(&acc
a6320 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d );. return z;.}
a6330 0a 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 69 6e ../*.** Print in
a6340 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e to memory obtain
a6350 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f ed from sqlite3_
a6360 6d 61 6c 6c 6f 63 28 29 28 29 2e 20 20 4f 6d 69 malloc()(). Omi
a6370 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 0a 2a t the internal.*
a6380 2a 20 25 2d 63 6f 6e 76 65 72 73 69 6f 6e 20 65 * %-conversion e
a6390 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 xtensions..*/.SQ
a63a0 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 2a 73 LITE_API char *s
a63b0 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 63 qlite3_mprintf(c
a63c0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d onst char *zForm
a63d0 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c at, ...){. va_l
a63e0 69 73 74 20 61 70 3b 0a 20 20 63 68 61 72 20 2a ist ap;. char *
a63f0 7a 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 z;.#ifndef SQLIT
a6400 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a E_OMIT_AUTOINIT.
a6410 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 69 6e if( sqlite3_in
a6420 69 74 69 61 6c 69 7a 65 28 29 20 29 20 72 65 74 itialize() ) ret
a6430 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 20 20 urn 0;.#endif.
a6440 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f va_start(ap, zFo
a6450 72 6d 61 74 29 3b 0a 20 20 7a 20 3d 20 73 71 6c rmat);. z = sql
a6460 69 74 65 33 5f 76 6d 70 72 69 6e 74 66 28 7a 46 ite3_vmprintf(zF
a6470 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 ormat, ap);. va
a6480 5f 65 6e 64 28 61 70 29 3b 0a 20 20 72 65 74 75 _end(ap);. retu
a6490 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 73 rn z;.}../*.** s
a64a0 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
a64b0 29 20 77 6f 72 6b 73 20 6c 69 6b 65 20 73 6e 70 ) works like snp
a64c0 72 69 6e 74 66 28 29 20 65 78 63 65 70 74 20 74 rintf() except t
a64d0 68 61 74 20 69 74 20 69 67 6e 6f 72 65 73 20 74 hat it ignores t
a64e0 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 6c 6f he.** current lo
a64f0 63 61 6c 65 20 73 65 74 74 69 6e 67 73 2e 20 20 cale settings.
a6500 54 68 69 73 20 69 73 20 69 6d 70 6f 72 74 61 6e This is importan
a6510 74 20 66 6f 72 20 53 51 4c 69 74 65 20 62 65 63 t for SQLite bec
a6520 61 75 73 65 20 77 65 0a 2a 2a 20 61 72 65 20 6e ause we.** are n
a6530 6f 74 20 61 62 6c 65 20 74 6f 20 75 73 65 20 61 ot able to use a
a6540 20 22 2c 22 20 61 73 20 74 68 65 20 64 65 63 69 "," as the deci
a6550 6d 61 6c 20 70 6f 69 6e 74 20 69 6e 20 70 6c 61 mal point in pla
a6560 63 65 20 6f 66 20 22 2e 22 20 61 73 0a 2a 2a 20 ce of "." as.**
a6570 73 70 65 63 69 66 69 65 64 20 62 79 20 73 6f 6d specified by som
a6580 65 20 6c 6f 63 61 6c 65 73 2e 0a 2a 2f 0a 53 51 e locales..*/.SQ
a6590 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 2a 73 LITE_API char *s
a65a0 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
a65b0 69 6e 74 20 6e 2c 20 63 68 61 72 20 2a 7a 42 75 int n, char *zBu
a65c0 66 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a f, const char *z
a65d0 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 Format, ...){.
a65e0 63 68 61 72 20 2a 7a 3b 0a 20 20 76 61 5f 6c 69 char *z;. va_li
a65f0 73 74 20 61 70 3b 0a 20 20 53 74 72 41 63 63 75 st ap;. StrAccu
a6600 6d 20 61 63 63 3b 0a 0a 20 20 69 66 28 20 6e 3c m acc;.. if( n<
a6610 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e =0 ){. return
a6620 20 7a 42 75 66 3b 0a 20 20 7d 0a 20 20 73 71 6c zBuf;. }. sql
a6630 69 74 65 33 53 74 72 41 63 63 75 6d 49 6e 69 74 ite3StrAccumInit
a6640 28 26 61 63 63 2c 20 7a 42 75 66 2c 20 6e 2c 20 (&acc, zBuf, n,
a6650 30 29 3b 0a 20 20 61 63 63 2e 75 73 65 4d 61 6c 0);. acc.useMal
a6660 6c 6f 63 20 3d 20 30 3b 0a 20 20 76 61 5f 73 74 loc = 0;. va_st
a6670 61 72 74 28 61 70 2c 7a 46 6f 72 6d 61 74 29 3b art(ap,zFormat);
a6680 0a 20 20 73 71 6c 69 74 65 33 56 58 50 72 69 6e . sqlite3VXPrin
a6690 74 66 28 26 61 63 63 2c 20 30 2c 20 7a 46 6f 72 tf(&acc, 0, zFor
a66a0 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65 mat, ap);. va_e
a66b0 6e 64 28 61 70 29 3b 0a 20 20 7a 20 3d 20 73 71 nd(ap);. z = sq
a66c0 6c 69 74 65 33 53 74 72 41 63 63 75 6d 46 69 6e lite3StrAccumFin
a66d0 69 73 68 28 26 61 63 63 29 3b 0a 20 20 72 65 74 ish(&acc);. ret
a66e0 75 72 6e 20 7a 3b 0a 7d 0a 0a 23 69 66 20 64 65 urn z;.}..#if de
a66f0 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 fined(SQLITE_DEB
a6700 55 47 29 0a 2f 2a 0a 2a 2a 20 41 20 76 65 72 73 UG)./*.** A vers
a6710 69 6f 6e 20 6f 66 20 70 72 69 6e 74 66 28 29 20 ion of printf()
a6720 74 68 61 74 20 75 6e 64 65 72 73 74 61 6e 64 73 that understands
a6730 20 25 6c 6c 64 2e 20 20 55 73 65 64 20 66 6f 72 %lld. Used for
a6740 20 64 65 62 75 67 67 69 6e 67 2e 0a 2a 2a 20 54 debugging..** T
a6750 68 65 20 70 72 69 6e 74 66 28 29 20 62 75 69 6c he printf() buil
a6760 74 20 69 6e 74 6f 20 73 6f 6d 65 20 76 65 72 73 t into some vers
a6770 69 6f 6e 73 20 6f 66 20 77 69 6e 64 6f 77 73 20 ions of windows
a6780 64 6f 65 73 20 6e 6f 74 20 75 6e 64 65 72 73 74 does not underst
a6790 61 6e 64 20 25 6c 6c 64 0a 2a 2a 20 61 6e 64 20 and %lld.** and
a67a0 73 65 67 66 61 75 6c 74 73 20 69 66 20 79 6f 75 segfaults if you
a67b0 20 67 69 76 65 20 69 74 20 61 20 6c 6f 6e 67 20 give it a long
a67c0 6c 6f 6e 67 20 69 6e 74 2e 0a 2a 2f 0a 53 51 4c long int..*/.SQL
a67d0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
a67e0 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 sqlite3DebugPri
a67f0 6e 74 66 28 63 6f 6e 73 74 20 63 68 61 72 20 2a ntf(const char *
a6800 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 zFormat, ...){.
a6810 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 53 va_list ap;. S
a6820 74 72 41 63 63 75 6d 20 61 63 63 3b 0a 20 20 63 trAccum acc;. c
a6830 68 61 72 20 7a 42 75 66 5b 35 30 30 5d 3b 0a 20 har zBuf[500];.
a6840 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d sqlite3StrAccum
a6850 49 6e 69 74 28 26 61 63 63 2c 20 7a 42 75 66 2c Init(&acc, zBuf,
a6860 20 73 69 7a 65 6f 66 28 7a 42 75 66 29 2c 20 30 sizeof(zBuf), 0
a6870 29 3b 0a 20 20 61 63 63 2e 75 73 65 4d 61 6c 6c );. acc.useMall
a6880 6f 63 20 3d 20 30 3b 0a 20 20 76 61 5f 73 74 61 oc = 0;. va_sta
a6890 72 74 28 61 70 2c 7a 46 6f 72 6d 61 74 29 3b 0a rt(ap,zFormat);.
a68a0 20 20 73 71 6c 69 74 65 33 56 58 50 72 69 6e 74 sqlite3VXPrint
a68b0 66 28 26 61 63 63 2c 20 30 2c 20 7a 46 6f 72 6d f(&acc, 0, zForm
a68c0 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e at, ap);. va_en
a68d0 64 28 61 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 d(ap);. sqlite3
a68e0 53 74 72 41 63 63 75 6d 46 69 6e 69 73 68 28 26 StrAccumFinish(&
a68f0 61 63 63 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 acc);. fprintf(
a6900 73 74 64 6f 75 74 2c 22 25 73 22 2c 20 7a 42 75 stdout,"%s", zBu
a6910 66 29 3b 0a 20 20 66 66 6c 75 73 68 28 73 74 64 f);. fflush(std
a6920 6f 75 74 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a out);.}.#endif..
a6930 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
a6940 45 6e 64 20 6f 66 20 70 72 69 6e 74 66 2e 63 20 End of printf.c
a6950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a6960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a6970 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
a6980 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
a6990 42 65 67 69 6e 20 66 69 6c 65 20 72 61 6e 64 6f Begin file rando
a69a0 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a m.c ************
a69b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a69c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
a69d0 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 /*.** 2001 Septe
a69e0 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 mber 15.**.** Th
a69f0 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 e author disclai
a6a00 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 ms copyright to
a6a10 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 this source code
a6a20 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a . In place of.*
a6a30 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 * a legal notice
a6a40 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 , here is a bles
a6a50 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d sing:.**.** M
a6a60 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 ay you do good a
a6a70 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 nd not evil..**
a6a80 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 May you find
a6a90 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 forgiveness for
a6aa0 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 yourself and for
a6ab0 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 give others..**
a6ac0 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 May you share
a6ad0 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 freely, never t
a6ae0 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 aking more than
a6af0 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a you give..**.***
a6b00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a6b10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a6b20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a6b30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a6b40 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 ******.** This f
a6b50 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 ile contains cod
a6b60 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 61 e to implement a
a6b70 20 70 73 65 75 64 6f 2d 72 61 6e 64 6f 6d 20 6e pseudo-random n
a6b80 75 6d 62 65 72 0a 2a 2a 20 67 65 6e 65 72 61 74 umber.** generat
a6b90 6f 72 20 28 50 52 4e 47 29 20 66 6f 72 20 53 51 or (PRNG) for SQ
a6ba0 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 52 61 6e 64 Lite..**.** Rand
a6bb0 6f 6d 20 6e 75 6d 62 65 72 73 20 61 72 65 20 75 om numbers are u
a6bc0 73 65 64 20 62 79 20 73 6f 6d 65 20 6f 66 20 74 sed by some of t
a6bd0 68 65 20 64 61 74 61 62 61 73 65 20 62 61 63 6b he database back
a6be0 65 6e 64 73 20 69 6e 20 6f 72 64 65 72 0a 2a 2a ends in order.**
a6bf0 20 74 6f 20 67 65 6e 65 72 61 74 65 20 72 61 6e to generate ran
a6c00 64 6f 6d 20 69 6e 74 65 67 65 72 20 6b 65 79 73 dom integer keys
a6c10 20 66 6f 72 20 74 61 62 6c 65 73 20 6f 72 20 72 for tables or r
a6c20 61 6e 64 6f 6d 20 66 69 6c 65 6e 61 6d 65 73 2e andom filenames.
a6c30 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 72 61 6e 64 .**.** $Id: rand
a6c40 6f 6d 2e 63 2c 76 20 31 2e 32 39 20 32 30 30 38 om.c,v 1.29 2008
a6c50 2f 31 32 2f 31 30 20 31 39 3a 32 36 3a 32 34 20 /12/10 19:26:24
a6c60 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a 2f drh Exp $.*/.../
a6c70 2a 20 41 6c 6c 20 74 68 72 65 61 64 73 20 73 68 * All threads sh
a6c80 61 72 65 20 61 20 73 69 6e 67 6c 65 20 72 61 6e are a single ran
a6c90 64 6f 6d 20 6e 75 6d 62 65 72 20 67 65 6e 65 72 dom number gener
a6ca0 61 74 6f 72 2e 0a 2a 2a 20 54 68 69 73 20 73 74 ator..** This st
a6cb0 72 75 63 74 75 72 65 20 69 73 20 74 68 65 20 63 ructure is the c
a6cc0 75 72 72 65 6e 74 20 73 74 61 74 65 20 6f 66 20 urrent state of
a6cd0 74 68 65 20 67 65 6e 65 72 61 74 6f 72 2e 0a 2a the generator..*
a6ce0 2f 0a 73 74 61 74 69 63 20 53 51 4c 49 54 45 5f /.static SQLITE_
a6cf0 57 53 44 20 73 74 72 75 63 74 20 73 71 6c 69 74 WSD struct sqlit
a6d00 65 33 50 72 6e 67 54 79 70 65 20 7b 0a 20 20 75 e3PrngType {. u
a6d10 6e 73 69 67 6e 65 64 20 63 68 61 72 20 69 73 49 nsigned char isI
a6d20 6e 69 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a nit; /*
a6d30 20 54 72 75 65 20 69 66 20 69 6e 69 74 69 61 6c True if initial
a6d40 69 7a 65 64 20 2a 2f 0a 20 20 75 6e 73 69 67 6e ized */. unsign
a6d50 65 64 20 63 68 61 72 20 69 2c 20 6a 3b 20 20 20 ed char i, j;
a6d60 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61 74 /* Stat
a6d70 65 20 76 61 72 69 61 62 6c 65 73 20 2a 2f 0a 20 e variables */.
a6d80 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 73 unsigned char s
a6d90 5b 32 35 36 5d 3b 20 20 20 20 20 20 20 20 20 20 [256];
a6da0 2f 2a 20 53 74 61 74 65 20 76 61 72 69 61 62 6c /* State variabl
a6db0 65 73 20 2a 2f 0a 7d 20 73 71 6c 69 74 65 33 50 es */.} sqlite3P
a6dc0 72 6e 67 3b 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20 rng;../*.** Get
a6dd0 61 20 73 69 6e 67 6c 65 20 38 2d 62 69 74 20 72 a single 8-bit r
a6de0 61 6e 64 6f 6d 20 76 61 6c 75 65 20 66 72 6f 6d andom value from
a6df0 20 74 68 65 20 52 43 34 20 50 52 4e 47 2e 20 20 the RC4 PRNG.
a6e00 54 68 65 20 4d 75 74 65 78 0a 2a 2a 20 6d 75 73 The Mutex.** mus
a6e10 74 20 62 65 20 68 65 6c 64 20 77 68 69 6c 65 20 t be held while
a6e20 65 78 65 63 75 74 69 6e 67 20 74 68 69 73 20 72 executing this r
a6e30 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 outine..**.** Wh
a6e40 79 20 6e 6f 74 20 6a 75 73 74 20 75 73 65 20 61 y not just use a
a6e50 20 6c 69 62 72 61 72 79 20 72 61 6e 64 6f 6d 20 library random
a6e60 67 65 6e 65 72 61 74 6f 72 20 6c 69 6b 65 20 6c generator like l
a6e70 72 61 6e 64 34 38 28 29 20 66 6f 72 20 74 68 69 rand48() for thi
a6e80 73 3f 0a 2a 2a 20 42 65 63 61 75 73 65 20 74 68 s?.** Because th
a6e90 65 20 4f 50 5f 4e 65 77 52 6f 77 69 64 20 6f 70 e OP_NewRowid op
a6ea0 63 6f 64 65 20 69 6e 20 74 68 65 20 56 44 42 45 code in the VDBE
a6eb0 20 64 65 70 65 6e 64 73 20 6f 6e 20 68 61 76 69 depends on havi
a6ec0 6e 67 20 61 20 76 65 72 79 0a 2a 2a 20 67 6f 6f ng a very.** goo
a6ed0 64 20 73 6f 75 72 63 65 20 6f 66 20 72 61 6e 64 d source of rand
a6ee0 6f 6d 20 6e 75 6d 62 65 72 73 2e 20 20 54 68 65 om numbers. The
a6ef0 20 6c 72 61 6e 64 34 38 28 29 20 6c 69 62 72 61 lrand48() libra
a6f00 72 79 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 0a ry function may.
a6f10 2a 2a 20 77 65 6c 6c 20 62 65 20 67 6f 6f 64 20 ** well be good
a6f20 65 6e 6f 75 67 68 2e 20 20 42 75 74 20 6d 61 79 enough. But may
a6f30 62 65 20 6e 6f 74 2e 20 20 4f 72 20 6d 61 79 62 be not. Or mayb
a6f40 65 20 6c 72 61 6e 64 34 38 28 29 20 68 61 73 20 e lrand48() has
a6f50 73 6f 6d 65 0a 2a 2a 20 73 75 62 74 6c 65 20 70 some.** subtle p
a6f60 72 6f 62 6c 65 6d 73 20 6f 6e 20 73 6f 6d 65 20 roblems on some
a6f70 73 79 73 74 65 6d 73 20 74 68 61 74 20 63 6f 75 systems that cou
a6f80 6c 64 20 63 61 75 73 65 20 70 72 6f 62 6c 65 6d ld cause problem
a6f90 73 2e 20 20 49 74 20 69 73 20 68 61 72 64 0a 2a s. It is hard.*
a6fa0 2a 20 74 6f 20 6b 6e 6f 77 2e 20 20 54 6f 20 6d * to know. To m
a6fb0 69 6e 69 6d 69 7a 65 20 74 68 65 20 72 69 73 6b inimize the risk
a6fc0 20 6f 66 20 70 72 6f 62 6c 65 6d 73 20 64 75 65 of problems due
a6fd0 20 74 6f 20 62 61 64 20 6c 72 61 6e 64 34 38 28 to bad lrand48(
a6fe0 29 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 ).** implementat
a6ff0 69 6f 6e 73 2c 20 53 51 4c 69 74 65 20 75 73 65 ions, SQLite use
a7000 73 20 74 68 69 73 20 72 61 6e 64 6f 6d 20 6e 75 s this random nu
a7010 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72 20 62 mber generator b
a7020 61 73 65 64 0a 2a 2a 20 6f 6e 20 52 43 34 2c 20 ased.** on RC4,
a7030 77 68 69 63 68 20 77 65 20 6b 6e 6f 77 20 77 6f which we know wo
a7040 72 6b 73 20 76 65 72 79 20 77 65 6c 6c 2e 0a 2a rks very well..*
a7050 2a 0a 2a 2a 20 28 4c 61 74 65 72 29 3a 20 20 41 *.** (Later): A
a7060 63 74 75 61 6c 6c 79 2c 20 4f 50 5f 4e 65 77 52 ctually, OP_NewR
a7070 6f 77 69 64 20 64 6f 65 73 20 6e 6f 74 20 64 65 owid does not de
a7080 70 65 6e 64 20 6f 6e 20 61 20 67 6f 6f 64 20 73 pend on a good s
a7090 6f 75 72 63 65 20 6f 66 0a 2a 2a 20 72 61 6e 64 ource of.** rand
a70a0 6f 6d 6e 65 73 73 20 61 6e 79 20 6d 6f 72 65 2e omness any more.
a70b0 20 20 42 75 74 20 77 65 20 77 69 6c 6c 20 6c 65 But we will le
a70c0 61 76 65 20 74 68 69 73 20 63 6f 64 65 20 69 6e ave this code in
a70d0 20 61 6c 6c 20 74 68 65 20 73 61 6d 65 2e 0a 2a all the same..*
a70e0 2f 0a 73 74 61 74 69 63 20 75 38 20 72 61 6e 64 /.static u8 rand
a70f0 6f 6d 42 79 74 65 28 76 6f 69 64 29 7b 0a 20 20 omByte(void){.
a7100 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 74 3b unsigned char t;
a7110 0a 0a 0a 20 20 2f 2a 20 54 68 65 20 22 77 73 64 ... /* The "wsd
a7120 50 72 6e 67 22 20 6d 61 63 72 6f 20 77 69 6c 6c Prng" macro will
a7130 20 72 65 73 6f 6c 76 65 20 74 6f 20 74 68 65 20 resolve to the
a7140 70 73 65 75 64 6f 2d 72 61 6e 64 6f 6d 20 6e 75 pseudo-random nu
a7150 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72 0a 20 mber generator.
a7160 20 2a 2a 20 73 74 61 74 65 20 76 65 63 74 6f 72 ** state vector
a7170 2e 20 20 49 66 20 77 72 69 74 61 62 6c 65 20 73 . If writable s
a7180 74 61 74 69 63 20 64 61 74 61 20 69 73 20 75 6e tatic data is un
a7190 73 75 70 70 6f 72 74 65 64 20 6f 6e 20 74 68 65 supported on the
a71a0 20 74 61 72 67 65 74 2c 0a 20 20 2a 2a 20 77 65 target,. ** we
a71b0 20 68 61 76 65 20 74 6f 20 6c 6f 63 61 74 65 20 have to locate
a71c0 74 68 65 20 73 74 61 74 65 20 76 65 63 74 6f 72 the state vector
a71d0 20 61 74 20 72 75 6e 2d 74 69 6d 65 2e 20 20 49 at run-time. I
a71e0 6e 20 74 68 65 20 6d 6f 72 65 20 63 6f 6d 6d 6f n the more commo
a71f0 6e 0a 20 20 2a 2a 20 63 61 73 65 20 77 68 65 72 n. ** case wher
a7200 65 20 77 72 69 74 61 62 6c 65 20 73 74 61 74 69 e writable stati
a7210 63 20 64 61 74 61 20 69 73 20 73 75 70 70 6f 72 c data is suppor
a7220 74 65 64 2c 20 77 73 64 50 72 6e 67 20 63 61 6e ted, wsdPrng can
a7230 20 72 65 66 65 72 20 64 69 72 65 63 74 6c 79 0a refer directly.
a7240 20 20 2a 2a 20 74 6f 20 74 68 65 20 22 73 71 6c ** to the "sql
a7250 69 74 65 33 50 72 6e 67 22 20 73 74 61 74 65 20 ite3Prng" state
a7260 76 65 63 74 6f 72 20 64 65 63 6c 61 72 65 64 20 vector declared
a7270 61 62 6f 76 65 2e 0a 20 20 2a 2f 0a 23 69 66 64 above.. */.#ifd
a7280 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 ef SQLITE_OMIT_W
a7290 53 44 0a 20 20 73 74 72 75 63 74 20 73 71 6c 69 SD. struct sqli
a72a0 74 65 33 50 72 6e 67 54 79 70 65 20 2a 70 20 3d te3PrngType *p =
a72b0 20 26 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 &GLOBAL(struct
a72c0 73 71 6c 69 74 65 33 50 72 6e 67 54 79 70 65 2c sqlite3PrngType,
a72d0 20 73 71 6c 69 74 65 33 50 72 6e 67 29 3b 0a 23 sqlite3Prng);.#
a72e0 20 64 65 66 69 6e 65 20 77 73 64 50 72 6e 67 20 define wsdPrng
a72f0 70 5b 30 5d 0a 23 65 6c 73 65 0a 23 20 64 65 66 p[0].#else.# def
a7300 69 6e 65 20 77 73 64 50 72 6e 67 20 73 71 6c 69 ine wsdPrng sqli
a7310 74 65 33 50 72 6e 67 0a 23 65 6e 64 69 66 0a 0a te3Prng.#endif..
a7320 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 . /* Initialize
a7330 20 74 68 65 20 73 74 61 74 65 20 6f 66 20 74 68 the state of th
a7340 65 20 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 e random number
a7350 67 65 6e 65 72 61 74 6f 72 20 6f 6e 63 65 2c 0a generator once,.
a7360 20 20 2a 2a 20 74 68 65 20 66 69 72 73 74 20 74 ** the first t
a7370 69 6d 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 ime this routine
a7380 20 69 73 20 63 61 6c 6c 65 64 2e 20 20 54 68 65 is called. The
a7390 20 73 65 65 64 20 76 61 6c 75 65 20 64 6f 65 73 seed value does
a73a0 0a 20 20 2a 2a 20 6e 6f 74 20 6e 65 65 64 20 74 . ** not need t
a73b0 6f 20 63 6f 6e 74 61 69 6e 20 61 20 6c 6f 74 20 o contain a lot
a73c0 6f 66 20 72 61 6e 64 6f 6d 6e 65 73 73 20 73 69 of randomness si
a73d0 6e 63 65 20 77 65 20 61 72 65 20 6e 6f 74 0a 20 nce we are not.
a73e0 20 2a 2a 20 74 72 79 69 6e 67 20 74 6f 20 64 6f ** trying to do
a73f0 20 73 65 63 75 72 65 20 65 6e 63 72 79 70 74 69 secure encrypti
a7400 6f 6e 20 6f 72 20 61 6e 79 74 68 69 6e 67 20 6c on or anything l
a7410 69 6b 65 20 74 68 61 74 2e 2e 2e 0a 20 20 2a 2a ike that.... **
a7420 0a 20 20 2a 2a 20 4e 6f 74 68 69 6e 67 20 69 6e . ** Nothing in
a7430 20 74 68 69 73 20 66 69 6c 65 20 6f 72 20 61 6e this file or an
a7440 79 77 68 65 72 65 20 65 6c 73 65 20 69 6e 20 53 ywhere else in S
a7450 51 4c 69 74 65 20 64 6f 65 73 20 61 6e 79 20 6b QLite does any k
a7460 69 6e 64 20 6f 66 0a 20 20 2a 2a 20 65 6e 63 72 ind of. ** encr
a7470 79 70 74 69 6f 6e 2e 20 20 54 68 65 20 52 43 34 yption. The RC4
a7480 20 61 6c 67 6f 72 69 74 68 6d 20 69 73 20 62 65 algorithm is be
a7490 69 6e 67 20 75 73 65 64 20 61 73 20 61 20 50 52 ing used as a PR
a74a0 4e 47 20 28 70 73 65 75 64 6f 2d 72 61 6e 64 6f NG (pseudo-rando
a74b0 6d 0a 20 20 2a 2a 20 6e 75 6d 62 65 72 20 67 65 m. ** number ge
a74c0 6e 65 72 61 74 6f 72 29 20 6e 6f 74 20 61 73 20 nerator) not as
a74d0 61 6e 20 65 6e 63 72 79 70 74 69 6f 6e 20 64 65 an encryption de
a74e0 76 69 63 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 vice.. */. if(
a74f0 20 21 77 73 64 50 72 6e 67 2e 69 73 49 6e 69 74 !wsdPrng.isInit
a7500 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 ){. int i;.
a7510 20 20 20 63 68 61 72 20 6b 5b 32 35 36 5d 3b 0a char k[256];.
a7520 20 20 20 20 77 73 64 50 72 6e 67 2e 6a 20 3d 20 wsdPrng.j =
a7530 30 3b 0a 20 20 20 20 77 73 64 50 72 6e 67 2e 69 0;. wsdPrng.i
a7540 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 = 0;. sqlite
a7550 33 4f 73 52 61 6e 64 6f 6d 6e 65 73 73 28 73 71 3OsRandomness(sq
a7560 6c 69 74 65 33 5f 76 66 73 5f 66 69 6e 64 28 30 lite3_vfs_find(0
a7570 29 2c 20 32 35 36 2c 20 6b 29 3b 0a 20 20 20 20 ), 256, k);.
a7580 66 6f 72 28 69 3d 30 3b 20 69 3c 32 35 36 3b 20 for(i=0; i<256;
a7590 69 2b 2b 29 7b 0a 20 20 20 20 20 20 77 73 64 50 i++){. wsdP
a75a0 72 6e 67 2e 73 5b 69 5d 20 3d 20 28 75 38 29 69 rng.s[i] = (u8)i
a75b0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 ;. }. for(
a75c0 69 3d 30 3b 20 69 3c 32 35 36 3b 20 69 2b 2b 29 i=0; i<256; i++)
a75d0 7b 0a 20 20 20 20 20 20 77 73 64 50 72 6e 67 2e {. wsdPrng.
a75e0 6a 20 2b 3d 20 77 73 64 50 72 6e 67 2e 73 5b 69 j += wsdPrng.s[i
a75f0 5d 20 2b 20 6b 5b 69 5d 3b 0a 20 20 20 20 20 20 ] + k[i];.
a7600 74 20 3d 20 77 73 64 50 72 6e 67 2e 73 5b 77 73 t = wsdPrng.s[ws
a7610 64 50 72 6e 67 2e 6a 5d 3b 0a 20 20 20 20 20 20 dPrng.j];.
a7620 77 73 64 50 72 6e 67 2e 73 5b 77 73 64 50 72 6e wsdPrng.s[wsdPrn
a7630 67 2e 6a 5d 20 3d 20 77 73 64 50 72 6e 67 2e 73 g.j] = wsdPrng.s
a7640 5b 69 5d 3b 0a 20 20 20 20 20 20 77 73 64 50 72 [i];. wsdPr
a7650 6e 67 2e 73 5b 69 5d 20 3d 20 74 3b 0a 20 20 20 ng.s[i] = t;.
a7660 20 7d 0a 20 20 20 20 77 73 64 50 72 6e 67 2e 69 }. wsdPrng.i
a7670 73 49 6e 69 74 20 3d 20 31 3b 0a 20 20 7d 0a 0a sInit = 1;. }..
a7680 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 61 6e /* Generate an
a7690 64 20 72 65 74 75 72 6e 20 73 69 6e 67 6c 65 20 d return single
a76a0 72 61 6e 64 6f 6d 20 62 79 74 65 0a 20 20 2a 2f random byte. */
a76b0 0a 20 20 77 73 64 50 72 6e 67 2e 69 2b 2b 3b 0a . wsdPrng.i++;.
a76c0 20 20 74 20 3d 20 77 73 64 50 72 6e 67 2e 73 5b t = wsdPrng.s[
a76d0 77 73 64 50 72 6e 67 2e 69 5d 3b 0a 20 20 77 73 wsdPrng.i];. ws
a76e0 64 50 72 6e 67 2e 6a 20 2b 3d 20 74 3b 0a 20 20 dPrng.j += t;.
a76f0 77 73 64 50 72 6e 67 2e 73 5b 77 73 64 50 72 6e wsdPrng.s[wsdPrn
a7700 67 2e 69 5d 20 3d 20 77 73 64 50 72 6e 67 2e 73 g.i] = wsdPrng.s
a7710 5b 77 73 64 50 72 6e 67 2e 6a 5d 3b 0a 20 20 77 [wsdPrng.j];. w
a7720 73 64 50 72 6e 67 2e 73 5b 77 73 64 50 72 6e 67 sdPrng.s[wsdPrng
a7730 2e 6a 5d 20 3d 20 74 3b 0a 20 20 74 20 2b 3d 20 .j] = t;. t +=
a7740 77 73 64 50 72 6e 67 2e 73 5b 77 73 64 50 72 6e wsdPrng.s[wsdPrn
a7750 67 2e 69 5d 3b 0a 20 20 72 65 74 75 72 6e 20 77 g.i];. return w
a7760 73 64 50 72 6e 67 2e 73 5b 74 5d 3b 0a 7d 0a 0a sdPrng.s[t];.}..
a7770 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 4e 20 72 /*.** Return N r
a7780 61 6e 64 6f 6d 20 62 79 74 65 73 2e 0a 2a 2f 0a andom bytes..*/.
a7790 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 SQLITE_API void
a77a0 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 sqlite3_randomne
a77b0 73 73 28 69 6e 74 20 4e 2c 20 76 6f 69 64 20 2a ss(int N, void *
a77c0 70 42 75 66 29 7b 0a 20 20 75 6e 73 69 67 6e 65 pBuf){. unsigne
a77d0 64 20 63 68 61 72 20 2a 7a 42 75 66 20 3d 20 70 d char *zBuf = p
a77e0 42 75 66 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f Buf;.#if SQLITE_
a77f0 54 48 52 45 41 44 53 41 46 45 0a 20 20 73 71 6c THREADSAFE. sql
a7800 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 ite3_mutex *mute
a7810 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 x = sqlite3Mutex
a7820 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 Alloc(SQLITE_MUT
a7830 45 58 5f 53 54 41 54 49 43 5f 50 52 4e 47 29 3b EX_STATIC_PRNG);
a7840 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 .#endif. sqlite
a7850 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 3_mutex_enter(mu
a7860 74 65 78 29 3b 0a 20 20 77 68 69 6c 65 28 20 4e tex);. while( N
a7870 2d 2d 20 29 7b 0a 20 20 20 20 2a 28 7a 42 75 66 -- ){. *(zBuf
a7880 2b 2b 29 20 3d 20 72 61 6e 64 6f 6d 42 79 74 65 ++) = randomByte
a7890 28 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 ();. }. sqlite
a78a0 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 3_mutex_leave(mu
a78b0 74 65 78 29 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 tex);.}..#ifndef
a78c0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49 SQLITE_OMIT_BUI
a78d0 4c 54 49 4e 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 LTIN_TEST./*.**
a78e0 46 6f 72 20 74 65 73 74 69 6e 67 20 70 75 72 70 For testing purp
a78f0 6f 73 65 73 2c 20 77 65 20 73 6f 6d 65 74 69 6d oses, we sometim
a7900 65 73 20 77 61 6e 74 20 74 6f 20 70 72 65 73 65 es want to prese
a7910 72 76 65 20 74 68 65 20 73 74 61 74 65 20 6f 66 rve the state of
a7920 0a 2a 2a 20 50 52 4e 47 20 61 6e 64 20 72 65 73 .** PRNG and res
a7930 74 6f 72 65 20 74 68 65 20 50 52 4e 47 20 74 6f tore the PRNG to
a7940 20 69 74 73 20 73 61 76 65 64 20 73 74 61 74 65 its saved state
a7950 20 61 74 20 61 20 6c 61 74 65 72 20 74 69 6d 65 at a later time
a7960 2c 20 6f 72 0a 2a 2a 20 74 6f 20 72 65 73 65 74 , or.** to reset
a7970 20 74 68 65 20 50 52 4e 47 20 74 6f 20 69 74 73 the PRNG to its
a7980 20 69 6e 69 74 69 61 6c 20 73 74 61 74 65 2e 20 initial state.
a7990 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 These routines
a79a0 61 63 63 6f 6d 70 6c 69 73 68 0a 2a 2a 20 74 68 accomplish.** th
a79b0 6f 73 65 20 74 61 73 6b 73 2e 0a 2a 2a 0a 2a 2a ose tasks..**.**
a79c0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 74 65 73 The sqlite3_tes
a79d0 74 5f 63 6f 6e 74 72 6f 6c 28 29 20 69 6e 74 65 t_control() inte
a79e0 72 66 61 63 65 20 63 61 6c 6c 73 20 74 68 65 73 rface calls thes
a79f0 65 20 72 6f 75 74 69 6e 65 73 20 74 6f 0a 2a 2a e routines to.**
a7a00 20 63 6f 6e 74 72 6f 6c 20 74 68 65 20 50 52 4e control the PRN
a7a10 47 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 53 51 4c G..*/.static SQL
a7a20 49 54 45 5f 57 53 44 20 73 74 72 75 63 74 20 73 ITE_WSD struct s
a7a30 71 6c 69 74 65 33 50 72 6e 67 54 79 70 65 20 73 qlite3PrngType s
a7a40 71 6c 69 74 65 33 53 61 76 65 64 50 72 6e 67 3b qlite3SavedPrng;
a7a50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
a7a60 76 6f 69 64 20 73 71 6c 69 74 65 33 50 72 6e 67 void sqlite3Prng
a7a70 53 61 76 65 53 74 61 74 65 28 76 6f 69 64 29 7b SaveState(void){
a7a80 0a 20 20 6d 65 6d 63 70 79 28 0a 20 20 20 20 26 . memcpy(. &
a7a90 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 73 71 GLOBAL(struct sq
a7aa0 6c 69 74 65 33 50 72 6e 67 54 79 70 65 2c 20 73 lite3PrngType, s
a7ab0 71 6c 69 74 65 33 53 61 76 65 64 50 72 6e 67 29 qlite3SavedPrng)
a7ac0 2c 0a 20 20 20 20 26 47 4c 4f 42 41 4c 28 73 74 ,. &GLOBAL(st
a7ad0 72 75 63 74 20 73 71 6c 69 74 65 33 50 72 6e 67 ruct sqlite3Prng
a7ae0 54 79 70 65 2c 20 73 71 6c 69 74 65 33 50 72 6e Type, sqlite3Prn
a7af0 67 29 2c 0a 20 20 20 20 73 69 7a 65 6f 66 28 73 g),. sizeof(s
a7b00 71 6c 69 74 65 33 50 72 6e 67 29 0a 20 20 29 3b qlite3Prng). );
a7b10 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 .}.SQLITE_PRIVAT
a7b20 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 72 E void sqlite3Pr
a7b30 6e 67 52 65 73 74 6f 72 65 53 74 61 74 65 28 76 ngRestoreState(v
a7b40 6f 69 64 29 7b 0a 20 20 6d 65 6d 63 70 79 28 0a oid){. memcpy(.
a7b50 20 20 20 20 26 47 4c 4f 42 41 4c 28 73 74 72 75 &GLOBAL(stru
a7b60 63 74 20 73 71 6c 69 74 65 33 50 72 6e 67 54 79 ct sqlite3PrngTy
a7b70 70 65 2c 20 73 71 6c 69 74 65 33 50 72 6e 67 29 pe, sqlite3Prng)
a7b80 2c 0a 20 20 20 20 26 47 4c 4f 42 41 4c 28 73 74 ,. &GLOBAL(st
a7b90 72 75 63 74 20 73 71 6c 69 74 65 33 50 72 6e 67 ruct sqlite3Prng
a7ba0 54 79 70 65 2c 20 73 71 6c 69 74 65 33 53 61 76 Type, sqlite3Sav
a7bb0 65 64 50 72 6e 67 29 2c 0a 20 20 20 20 73 69 7a edPrng),. siz
a7bc0 65 6f 66 28 73 71 6c 69 74 65 33 50 72 6e 67 29 eof(sqlite3Prng)
a7bd0 0a 20 20 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 . );.}.SQLITE_P
a7be0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
a7bf0 74 65 33 50 72 6e 67 52 65 73 65 74 53 74 61 74 te3PrngResetStat
a7c00 65 28 76 6f 69 64 29 7b 0a 20 20 47 4c 4f 42 41 e(void){. GLOBA
a7c10 4c 28 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 L(struct sqlite3
a7c20 50 72 6e 67 54 79 70 65 2c 20 73 71 6c 69 74 65 PrngType, sqlite
a7c30 33 50 72 6e 67 29 2e 69 73 49 6e 69 74 20 3d 20 3Prng).isInit =
a7c40 30 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 0;.}.#endif /* S
a7c50 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 QLITE_OMIT_BUILT
a7c60 49 4e 5f 54 45 53 54 20 2a 2f 0a 0a 2f 2a 2a 2a IN_TEST */../***
a7c70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
a7c80 6f 66 20 72 61 6e 64 6f 6d 2e 63 20 2a 2a 2a 2a of random.c ****
a7c90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a7ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a7cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
a7cc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
a7cd0 6e 20 66 69 6c 65 20 75 74 66 2e 63 20 2a 2a 2a n file utf.c ***
a7ce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a7cf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a7d00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a **********/./*.*
a7d10 2a 20 32 30 30 34 20 41 70 72 69 6c 20 31 33 0a * 2004 April 13.
a7d20 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 **.** The author
a7d30 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 disclaims copyr
a7d40 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 ight to this sou
a7d50 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c rce code. In pl
a7d60 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 ace of.** a lega
a7d70 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 l notice, here i
a7d80 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a s a blessing:.**
a7d90 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 .** May you d
a7da0 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 o good and not e
a7db0 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 vil..** May y
a7dc0 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e ou find forgiven
a7dd0 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 ess for yourself
a7de0 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 and forgive oth
a7df0 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 ers..** May y
a7e00 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c ou share freely,
a7e10 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f never taking mo
a7e20 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 re than you give
a7e30 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ..**.***********
a7e40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a7e50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a7e60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a7e70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
a7e80 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 * This file cont
a7e90 61 69 6e 73 20 72 6f 75 74 69 6e 65 73 20 75 73 ains routines us
a7ea0 65 64 20 74 6f 20 74 72 61 6e 73 6c 61 74 65 20 ed to translate
a7eb0 62 65 74 77 65 65 6e 20 55 54 46 2d 38 2c 20 0a between UTF-8, .
a7ec0 2a 2a 20 55 54 46 2d 31 36 2c 20 55 54 46 2d 31 ** UTF-16, UTF-1
a7ed0 36 42 45 2c 20 61 6e 64 20 55 54 46 2d 31 36 4c 6BE, and UTF-16L
a7ee0 45 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 75 74 E..**.** $Id: ut
a7ef0 66 2e 63 2c 76 20 31 2e 37 33 20 32 30 30 39 2f f.c,v 1.73 2009/
a7f00 30 34 2f 30 31 20 31 38 3a 34 30 3a 33 32 20 64 04/01 18:40:32 d
a7f10 72 68 20 45 78 70 20 24 0a 2a 2a 0a 2a 2a 20 4e rh Exp $.**.** N
a7f20 6f 74 65 73 20 6f 6e 20 55 54 46 2d 38 3a 0a 2a otes on UTF-8:.*
a7f30 2a 0a 2a 2a 20 20 20 42 79 74 65 2d 30 20 20 20 *.** Byte-0
a7f40 20 42 79 74 65 2d 31 20 20 20 20 42 79 74 65 2d Byte-1 Byte-
a7f50 32 20 20 20 20 42 79 74 65 2d 33 20 20 20 20 56 2 Byte-3 V
a7f60 61 6c 75 65 0a 2a 2a 20 20 30 78 78 78 78 78 78 alue.** 0xxxxxx
a7f70 78 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 x
a7f80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a7f90 20 20 30 30 30 30 30 30 30 30 20 30 30 30 30 30 00000000 00000
a7fa0 30 30 30 20 30 78 78 78 78 78 78 78 0a 2a 2a 20 000 0xxxxxxx.**
a7fb0 20 31 31 30 79 79 79 79 79 20 20 31 30 78 78 78 110yyyyy 10xxx
a7fc0 78 78 78 20 20 20 20 20 20 20 20 20 20 20 20 20 xxx
a7fd0 20 20 20 20 20 20 20 20 20 20 30 30 30 30 30 30 000000
a7fe0 30 30 20 30 30 30 30 30 79 79 79 20 79 79 78 78 00 00000yyy yyxx
a7ff0 78 78 78 78 0a 2a 2a 20 20 31 31 31 30 7a 7a 7a xxxx.** 1110zzz
a8000 7a 20 20 31 30 79 79 79 79 79 79 20 20 31 30 78 z 10yyyyyy 10x
a8010 78 78 78 78 78 20 20 20 20 20 20 20 20 20 20 20 xxxxx
a8020 20 20 30 30 30 30 30 30 30 30 20 7a 7a 7a 7a 79 00000000 zzzzy
a8030 79 79 79 20 79 79 78 78 78 78 78 78 0a 2a 2a 20 yyy yyxxxxxx.**
a8040 20 31 31 31 31 30 75 75 75 20 20 31 30 75 75 7a 11110uuu 10uuz
a8050 7a 7a 7a 20 20 31 30 79 79 79 79 79 79 20 20 31 zzz 10yyyyyy 1
a8060 30 78 78 78 78 78 78 20 20 20 30 30 30 75 75 75 0xxxxxx 000uuu
a8070 75 75 20 7a 7a 7a 7a 79 79 79 79 20 79 79 78 78 uu zzzzyyyy yyxx
a8080 78 78 78 78 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 4e 6f xxxx.**.**.** No
a8090 74 65 73 20 6f 6e 20 55 54 46 2d 31 36 3a 20 20 tes on UTF-16:
a80a0 28 77 69 74 68 20 77 77 77 77 2b 31 3d 3d 75 75 (with wwww+1==uu
a80b0 75 75 75 29 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 uuu).**.**
a80c0 57 6f 72 64 2d 30 20 20 20 20 20 20 20 20 20 20 Word-0
a80d0 20 20 20 20 20 57 6f 72 64 2d 31 20 20 20 20 20 Word-1
a80e0 20 20 20 20 20 56 61 6c 75 65 0a 2a 2a 20 20 31 Value.** 1
a80f0 31 30 31 31 30 77 77 20 77 77 7a 7a 7a 7a 79 79 10110ww wwzzzzyy
a8100 20 20 20 31 31 30 31 31 31 79 79 20 79 79 78 78 110111yy yyxx
a8110 78 78 78 78 20 20 20 20 30 30 30 75 75 75 75 75 xxxx 000uuuuu
a8120 20 7a 7a 7a 7a 79 79 79 79 20 79 79 78 78 78 78 zzzzyyyy yyxxxx
a8130 78 78 0a 2a 2a 20 20 7a 7a 7a 7a 79 79 79 79 20 xx.** zzzzyyyy
a8140 79 79 78 78 78 78 78 78 20 20 20 20 20 20 20 20 yyxxxxxx
a8150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a8160 30 30 30 30 30 30 30 30 20 7a 7a 7a 7a 79 79 79 00000000 zzzzyyy
a8170 79 20 79 79 78 78 78 78 78 78 0a 2a 2a 0a 2a 2a y yyxxxxxx.**.**
a8180 0a 2a 2a 20 42 4f 4d 20 6f 72 20 42 79 74 65 20 .** BOM or Byte
a8190 4f 72 64 65 72 20 4d 61 72 6b 3a 0a 2a 2a 20 20 Order Mark:.**
a81a0 20 20 20 30 78 66 66 20 30 78 66 65 20 20 20 6c 0xff 0xfe l
a81b0 69 74 74 6c 65 2d 65 6e 64 69 61 6e 20 75 74 66 ittle-endian utf
a81c0 2d 31 36 20 66 6f 6c 6c 6f 77 73 0a 2a 2a 20 20 -16 follows.**
a81d0 20 20 20 30 78 66 65 20 30 78 66 66 20 20 20 62 0xfe 0xff b
a81e0 69 67 2d 65 6e 64 69 61 6e 20 75 74 66 2d 31 36 ig-endian utf-16
a81f0 20 66 6f 6c 6c 6f 77 73 0a 2a 2a 0a 2a 2f 0a 2f follows.**.*/./
a8200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 ************** I
a8210 6e 63 6c 75 64 65 20 76 64 62 65 49 6e 74 2e 68 nclude vdbeInt.h
a8220 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f in the middle o
a8230 66 20 75 74 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a f utf.c ********
a8240 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
a8250 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 ************** B
a8260 65 67 69 6e 20 66 69 6c 65 20 76 64 62 65 49 6e egin file vdbeIn
a8270 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a t.h ************
a8280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a8290 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
a82a0 2a 0a 2a 2a 20 32 30 30 33 20 53 65 70 74 65 6d *.** 2003 Septem
a82b0 62 65 72 20 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ber 6.**.** The
a82c0 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 author disclaims
a82d0 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 copyright to th
a82e0 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 is source code.
a82f0 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 In place of.**
a8300 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 a legal notice,
a8310 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 here is a blessi
a8320 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 ng:.**.** May
a8330 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 you do good and
a8340 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 not evil..**
a8350 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f May you find fo
a8360 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f rgiveness for yo
a8370 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 urself and forgi
a8380 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 ve others..**
a8390 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 May you share f
a83a0 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b reely, never tak
a83b0 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f ing more than yo
a83c0 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a u give..**.*****
a83d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a83e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a83f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a8400 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a8410 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 ****.** This is
a8420 74 68 65 20 68 65 61 64 65 72 20 66 69 6c 65 20 the header file
a8430 66 6f 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 for information
a8440 74 68 61 74 20 69 73 20 70 72 69 76 61 74 65 20 that is private
a8450 74 6f 20 74 68 65 0a 2a 2a 20 56 44 42 45 2e 20 to the.** VDBE.
a8460 20 54 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f This informatio
a8470 6e 20 75 73 65 64 20 74 6f 20 61 6c 6c 20 62 65 n used to all be
a8480 20 61 74 20 74 68 65 20 74 6f 70 20 6f 66 20 74 at the top of t
a8490 68 65 20 73 69 6e 67 6c 65 0a 2a 2a 20 73 6f 75 he single.** sou
a84a0 72 63 65 20 63 6f 64 65 20 66 69 6c 65 20 22 76 rce code file "v
a84b0 64 62 65 2e 63 22 2e 20 20 57 68 65 6e 20 74 68 dbe.c". When th
a84c0 61 74 20 66 69 6c 65 20 62 65 63 61 6d 65 20 74 at file became t
a84d0 6f 6f 20 62 69 67 20 28 6f 76 65 72 0a 2a 2a 20 oo big (over.**
a84e0 36 30 30 30 20 6c 69 6e 65 73 20 6c 6f 6e 67 29 6000 lines long)
a84f0 20 69 74 20 77 61 73 20 73 70 6c 69 74 20 75 70 it was split up
a8500 20 69 6e 74 6f 20 73 65 76 65 72 61 6c 20 73 6d into several sm
a8510 61 6c 6c 65 72 20 66 69 6c 65 73 20 61 6e 64 0a aller files and.
a8520 2a 2a 20 74 68 69 73 20 68 65 61 64 65 72 20 69 ** this header i
a8530 6e 66 6f 72 6d 61 74 69 6f 6e 20 77 61 73 20 66 nformation was f
a8540 61 63 74 6f 72 65 64 20 6f 75 74 2e 0a 2a 2a 0a actored out..**.
a8550 2a 2a 20 24 49 64 3a 20 76 64 62 65 49 6e 74 2e ** $Id: vdbeInt.
a8560 68 2c 76 20 31 2e 31 37 34 20 32 30 30 39 2f 30 h,v 1.174 2009/0
a8570 36 2f 32 33 20 31 34 3a 31 35 3a 30 34 20 64 72 6/23 14:15:04 dr
a8580 68 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 h Exp $.*/.#ifnd
a8590 65 66 20 5f 56 44 42 45 49 4e 54 5f 48 5f 0a 23 ef _VDBEINT_H_.#
a85a0 64 65 66 69 6e 65 20 5f 56 44 42 45 49 4e 54 5f define _VDBEINT_
a85b0 48 5f 0a 0a 2f 2a 0a 2a 2a 20 53 51 4c 20 69 73 H_../*.** SQL is
a85c0 20 74 72 61 6e 73 6c 61 74 65 64 20 69 6e 74 6f translated into
a85d0 20 61 20 73 65 71 75 65 6e 63 65 20 6f 66 20 69 a sequence of i
a85e0 6e 73 74 72 75 63 74 69 6f 6e 73 20 74 6f 20 62 nstructions to b
a85f0 65 0a 2a 2a 20 65 78 65 63 75 74 65 64 20 62 79 e.** executed by
a8600 20 61 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 a virtual machi
a8610 6e 65 2e 20 20 45 61 63 68 20 69 6e 73 74 72 75 ne. Each instru
a8620 63 74 69 6f 6e 20 69 73 20 61 6e 20 69 6e 73 74 ction is an inst
a8630 61 6e 63 65 0a 2a 2a 20 6f 66 20 74 68 65 20 66 ance.** of the f
a8640 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 ollowing structu
a8650 72 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 re..*/.typedef s
a8660 74 72 75 63 74 20 56 64 62 65 4f 70 20 4f 70 3b truct VdbeOp Op;
a8670 0a 0a 2f 2a 0a 2a 2a 20 42 6f 6f 6c 65 61 6e 20 ../*.** Boolean
a8680 76 61 6c 75 65 73 0a 2a 2f 0a 74 79 70 65 64 65 values.*/.typede
a8690 66 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 f unsigned char
a86a0 42 6f 6f 6c 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 63 Bool;../*.** A c
a86b0 75 72 73 6f 72 20 69 73 20 61 20 70 6f 69 6e 74 ursor is a point
a86c0 65 72 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 er into a single
a86d0 20 42 54 72 65 65 20 77 69 74 68 69 6e 20 61 20 BTree within a
a86e0 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a database file..*
a86f0 2a 20 54 68 65 20 63 75 72 73 6f 72 20 63 61 6e * The cursor can
a8700 20 73 65 65 6b 20 74 6f 20 61 20 42 54 72 65 65 seek to a BTree
a8710 20 65 6e 74 72 79 20 77 69 74 68 20 61 20 70 61 entry with a pa
a8720 72 74 69 63 75 6c 61 72 20 6b 65 79 2c 20 6f 72 rticular key, or
a8730 0a 2a 2a 20 6c 6f 6f 70 20 6f 76 65 72 20 61 6c .** loop over al
a8740 6c 20 65 6e 74 72 69 65 73 20 6f 66 20 74 68 65 l entries of the
a8750 20 42 74 72 65 65 2e 20 20 59 6f 75 20 63 61 6e Btree. You can
a8760 20 61 6c 73 6f 20 69 6e 73 65 72 74 20 6e 65 77 also insert new
a8770 20 42 54 72 65 65 0a 2a 2a 20 65 6e 74 72 69 65 BTree.** entrie
a8780 73 20 6f 72 20 72 65 74 72 69 65 76 65 20 74 68 s or retrieve th
a8790 65 20 6b 65 79 20 6f 72 20 64 61 74 61 20 66 72 e key or data fr
a87a0 6f 6d 20 74 68 65 20 65 6e 74 72 79 20 74 68 61 om the entry tha
a87b0 74 20 74 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 t the cursor.**
a87c0 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 is currently poi
a87d0 6e 74 69 6e 67 20 74 6f 2e 0a 2a 2a 20 0a 2a 2a nting to..** .**
a87e0 20 45 76 65 72 79 20 63 75 72 73 6f 72 20 74 68 Every cursor th
a87f0 61 74 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d at the virtual m
a8800 61 63 68 69 6e 65 20 68 61 73 20 6f 70 65 6e 20 achine has open
a8810 69 73 20 72 65 70 72 65 73 65 6e 74 65 64 20 62 is represented b
a8820 79 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 y an.** instance
a8830 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e of the followin
a8840 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a g structure..**.
a8850 2a 2a 20 49 66 20 74 68 65 20 56 64 62 65 43 75 ** If the VdbeCu
a8860 72 73 6f 72 2e 69 73 54 72 69 67 67 65 72 52 6f rsor.isTriggerRo
a8870 77 20 66 6c 61 67 20 69 73 20 73 65 74 20 69 74 w flag is set it
a8880 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 69 73 means that this
a8890 20 63 75 72 73 6f 72 20 69 73 0a 2a 2a 20 72 65 cursor is.** re
a88a0 61 6c 6c 79 20 61 20 73 69 6e 67 6c 65 20 72 6f ally a single ro
a88b0 77 20 74 68 61 74 20 72 65 70 72 65 73 65 6e 74 w that represent
a88c0 73 20 74 68 65 20 4e 45 57 20 6f 72 20 4f 4c 44 s the NEW or OLD
a88d0 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 6f 66 pseudo-table of
a88e0 0a 2a 2a 20 61 20 72 6f 77 20 74 72 69 67 67 65 .** a row trigge
a88f0 72 2e 20 20 54 68 65 20 64 61 74 61 20 66 6f 72 r. The data for
a8900 20 74 68 65 20 72 6f 77 20 69 73 20 73 74 6f 72 the row is stor
a8910 65 64 20 69 6e 20 56 64 62 65 43 75 72 73 6f 72 ed in VdbeCursor
a8920 2e 70 44 61 74 61 20 61 6e 64 0a 2a 2a 20 74 68 .pData and.** th
a8930 65 20 72 6f 77 69 64 20 69 73 20 69 6e 20 56 64 e rowid is in Vd
a8940 62 65 43 75 72 73 6f 72 2e 69 4b 65 79 2e 0a 2a beCursor.iKey..*
a8950 2f 0a 73 74 72 75 63 74 20 56 64 62 65 43 75 72 /.struct VdbeCur
a8960 73 6f 72 20 7b 0a 20 20 42 74 43 75 72 73 6f 72 sor {. BtCursor
a8970 20 2a 70 43 75 72 73 6f 72 3b 20 20 20 20 2f 2a *pCursor; /*
a8980 20 54 68 65 20 63 75 72 73 6f 72 20 73 74 72 75 The cursor stru
a8990 63 74 75 72 65 20 6f 66 20 74 68 65 20 62 61 63 cture of the bac
a89a0 6b 65 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 69 44 kend */. int iD
a89b0 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 b;
a89c0 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 75 72 73 /* Index of curs
a89d0 6f 72 20 64 61 74 61 62 61 73 65 20 69 6e 20 64 or database in d
a89e0 62 2d 3e 61 44 62 5b 5d 20 28 6f 72 20 2d 31 29 b->aDb[] (or -1)
a89f0 20 2a 2f 0a 20 20 69 36 34 20 6c 61 73 74 52 6f */. i64 lastRo
a8a00 77 69 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c wid; /* L
a8a10 61 73 74 20 72 6f 77 69 64 20 66 72 6f 6d 20 61 ast rowid from a
a8a20 20 4e 65 78 74 20 6f 72 20 4e 65 78 74 49 64 78 Next or NextIdx
a8a30 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a 20 20 operation */.
a8a40 42 6f 6f 6c 20 7a 65 72 6f 65 64 3b 20 20 20 20 Bool zeroed;
a8a50 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
a8a60 20 7a 65 72 6f 65 64 20 6f 75 74 20 61 6e 64 20 zeroed out and
a8a70 72 65 61 64 79 20 66 6f 72 20 72 65 75 73 65 20 ready for reuse
a8a80 2a 2f 0a 20 20 42 6f 6f 6c 20 72 6f 77 69 64 49 */. Bool rowidI
a8a90 73 56 61 6c 69 64 3b 20 20 20 20 2f 2a 20 54 72 sValid; /* Tr
a8aa0 75 65 20 69 66 20 6c 61 73 74 52 6f 77 69 64 20 ue if lastRowid
a8ab0 69 73 20 76 61 6c 69 64 20 2a 2f 0a 20 20 42 6f is valid */. Bo
a8ac0 6f 6c 20 61 74 46 69 72 73 74 3b 20 20 20 20 20 ol atFirst;
a8ad0 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 70 /* True if p
a8ae0 6f 69 6e 74 69 6e 67 20 74 6f 20 66 69 72 73 74 ointing to first
a8af0 20 65 6e 74 72 79 20 2a 2f 0a 20 20 42 6f 6f 6c entry */. Bool
a8b00 20 75 73 65 52 61 6e 64 6f 6d 52 6f 77 69 64 3b useRandomRowid;
a8b10 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 6e 65 /* Generate ne
a8b20 77 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 73 w record numbers
a8b30 20 73 65 6d 69 2d 72 61 6e 64 6f 6d 6c 79 20 2a semi-randomly *
a8b40 2f 0a 20 20 42 6f 6f 6c 20 6e 75 6c 6c 52 6f 77 /. Bool nullRow
a8b50 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 ; /* Tru
a8b60 65 20 69 66 20 70 6f 69 6e 74 69 6e 67 20 74 6f e if pointing to
a8b70 20 61 20 72 6f 77 20 77 69 74 68 20 6e 6f 20 64 a row with no d
a8b80 61 74 61 20 2a 2f 0a 20 20 42 6f 6f 6c 20 64 65 ata */. Bool de
a8b90 66 65 72 72 65 64 4d 6f 76 65 74 6f 3b 20 20 2f ferredMoveto; /
a8ba0 2a 20 41 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 * A call to sqli
a8bb0 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 28 29 te3BtreeMoveto()
a8bc0 20 69 73 20 6e 65 65 64 65 64 20 2a 2f 0a 20 20 is needed */.
a8bd0 42 6f 6f 6c 20 69 73 54 61 62 6c 65 3b 20 20 20 Bool isTable;
a8be0 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
a8bf0 20 61 20 74 61 62 6c 65 20 72 65 71 75 69 72 69 a table requiri
a8c00 6e 67 20 69 6e 74 65 67 65 72 20 6b 65 79 73 20 ng integer keys
a8c10 2a 2f 0a 20 20 42 6f 6f 6c 20 69 73 49 6e 64 65 */. Bool isInde
a8c20 78 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 x; /* Tr
a8c30 75 65 20 69 66 20 61 6e 20 69 6e 64 65 78 20 63 ue if an index c
a8c40 6f 6e 74 61 69 6e 69 6e 67 20 6b 65 79 73 20 6f ontaining keys o
a8c50 6e 6c 79 20 2d 20 6e 6f 20 64 61 74 61 20 2a 2f nly - no data */
a8c60 0a 20 20 69 36 34 20 6d 6f 76 65 74 6f 54 61 72 . i64 movetoTar
a8c70 67 65 74 3b 20 20 20 20 20 2f 2a 20 41 72 67 75 get; /* Argu
a8c80 6d 65 6e 74 20 74 6f 20 74 68 65 20 64 65 66 65 ment to the defe
a8c90 72 72 65 64 20 73 71 6c 69 74 65 33 42 74 72 65 rred sqlite3Btre
a8ca0 65 4d 6f 76 65 74 6f 28 29 20 2a 2f 0a 20 20 42 eMoveto() */. B
a8cb0 74 72 65 65 20 2a 70 42 74 3b 20 20 20 20 20 20 tree *pBt;
a8cc0 20 20 20 20 20 2f 2a 20 53 65 70 61 72 61 74 65 /* Separate
a8cd0 20 66 69 6c 65 20 68 6f 6c 64 69 6e 67 20 74 65 file holding te
a8ce0 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 2a 2f mporary table */
a8cf0 0a 20 20 69 6e 74 20 70 73 65 75 64 6f 54 61 62 . int pseudoTab
a8d00 6c 65 52 65 67 3b 20 20 20 2f 2a 20 52 65 67 69 leReg; /* Regi
a8d10 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 70 73 65 ster holding pse
a8d20 75 64 6f 74 61 62 6c 65 20 63 6f 6e 74 65 6e 74 udotable content
a8d30 2e 20 2a 2f 0a 20 20 4b 65 79 49 6e 66 6f 20 2a . */. KeyInfo *
a8d40 70 4b 65 79 49 6e 66 6f 3b 20 20 20 20 2f 2a 20 pKeyInfo; /*
a8d50 49 6e 66 6f 20 61 62 6f 75 74 20 69 6e 64 65 78 Info about index
a8d60 20 6b 65 79 73 20 6e 65 65 64 65 64 20 62 79 20 keys needed by
a8d70 69 6e 64 65 78 20 63 75 72 73 6f 72 73 20 2a 2f index cursors */
a8d80 0a 20 20 69 6e 74 20 6e 46 69 65 6c 64 3b 20 20 . int nField;
a8d90 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
a8da0 65 72 20 6f 66 20 66 69 65 6c 64 73 20 69 6e 20 er of fields in
a8db0 74 68 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 the header */.
a8dc0 69 36 34 20 73 65 71 43 6f 75 6e 74 3b 20 20 20 i64 seqCount;
a8dd0 20 20 20 20 20 20 2f 2a 20 53 65 71 75 65 6e 63 /* Sequenc
a8de0 65 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 73 e counter */. s
a8df0 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 qlite3_vtab_curs
a8e00 6f 72 20 2a 70 56 74 61 62 43 75 72 73 6f 72 3b or *pVtabCursor;
a8e10 20 20 2f 2a 20 54 68 65 20 63 75 72 73 6f 72 20 /* The cursor
a8e20 66 6f 72 20 61 20 76 69 72 74 75 61 6c 20 74 61 for a virtual ta
a8e30 62 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 73 ble */. const s
a8e40 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 qlite3_module *p
a8e50 4d 6f 64 75 6c 65 3b 20 20 20 20 20 2f 2a 20 4d Module; /* M
a8e60 6f 64 75 6c 65 20 66 6f 72 20 63 75 72 73 6f 72 odule for cursor
a8e70 20 70 56 74 61 62 43 75 72 73 6f 72 20 2a 2f 0a pVtabCursor */.
a8e80 0a 20 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66 20 . /* Result of
a8e90 6c 61 73 74 20 73 71 6c 69 74 65 33 42 74 72 65 last sqlite3Btre
a8ea0 65 4d 6f 76 65 74 6f 28 29 20 64 6f 6e 65 20 62 eMoveto() done b
a8eb0 79 20 61 6e 20 4f 50 5f 4e 6f 74 45 78 69 73 74 y an OP_NotExist
a8ec0 73 20 6f 72 20 0a 20 20 2a 2a 20 4f 50 5f 49 73 s or . ** OP_Is
a8ed0 55 6e 69 71 75 65 20 6f 70 63 6f 64 65 20 6f 6e Unique opcode on
a8ee0 20 74 68 69 73 20 63 75 72 73 6f 72 2e 20 2a 2f this cursor. */
a8ef0 0a 20 20 69 6e 74 20 73 65 65 6b 52 65 73 75 6c . int seekResul
a8f00 74 3b 0a 0a 20 20 2f 2a 20 43 61 63 68 65 64 20 t;.. /* Cached
a8f10 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 information abou
a8f20 74 20 74 68 65 20 68 65 61 64 65 72 20 66 6f 72 t the header for
a8f30 20 74 68 65 20 64 61 74 61 20 72 65 63 6f 72 64 the data record
a8f40 20 74 68 61 74 20 74 68 65 0a 20 20 2a 2a 20 63 that the. ** c
a8f50 75 72 73 6f 72 20 69 73 20 63 75 72 72 65 6e 74 ursor is current
a8f60 6c 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2e 20 ly pointing to.
a8f70 20 4f 6e 6c 79 20 76 61 6c 69 64 20 69 66 20 63 Only valid if c
a8f80 61 63 68 65 53 74 61 74 75 73 20 6d 61 74 63 68 acheStatus match
a8f90 65 73 0a 20 20 2a 2a 20 56 64 62 65 2e 63 61 63 es. ** Vdbe.cac
a8fa0 68 65 43 74 72 2e 20 20 56 64 62 65 2e 63 61 63 heCtr. Vdbe.cac
a8fb0 68 65 43 74 72 20 77 69 6c 6c 20 6e 65 76 65 72 heCtr will never
a8fc0 20 74 61 6b 65 20 6f 6e 20 74 68 65 20 76 61 6c take on the val
a8fd0 75 65 20 6f 66 0a 20 20 2a 2a 20 43 41 43 48 45 ue of. ** CACHE
a8fe0 5f 53 54 41 4c 45 20 61 6e 64 20 73 6f 20 73 65 _STALE and so se
a8ff0 74 74 69 6e 67 20 63 61 63 68 65 53 74 61 74 75 tting cacheStatu
a9000 73 3d 43 41 43 48 45 5f 53 54 41 4c 45 20 67 75 s=CACHE_STALE gu
a9010 61 72 61 6e 74 65 65 73 20 74 68 61 74 0a 20 20 arantees that.
a9020 2a 2a 20 74 68 65 20 63 61 63 68 65 20 69 73 20 ** the cache is
a9030 6f 75 74 20 6f 66 20 64 61 74 65 2e 0a 20 20 2a out of date.. *
a9040 2a 0a 20 20 2a 2a 20 61 52 6f 77 20 6d 69 67 68 *. ** aRow migh
a9050 74 20 70 6f 69 6e 74 20 74 6f 20 28 65 70 68 65 t point to (ephe
a9060 6d 65 72 61 6c 29 20 64 61 74 61 20 66 6f 72 20 meral) data for
a9070 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 2c the current row,
a9080 20 6f 72 20 69 74 20 6d 69 67 68 74 0a 20 20 2a or it might. *
a9090 2a 20 62 65 20 4e 55 4c 4c 2e 0a 20 20 2a 2f 0a * be NULL.. */.
a90a0 20 20 75 33 32 20 63 61 63 68 65 53 74 61 74 75 u32 cacheStatu
a90b0 73 3b 20 20 20 20 20 20 2f 2a 20 43 61 63 68 65 s; /* Cache
a90c0 20 69 73 20 76 61 6c 69 64 20 69 66 20 74 68 69 is valid if thi
a90d0 73 20 6d 61 74 63 68 65 73 20 56 64 62 65 2e 63 s matches Vdbe.c
a90e0 61 63 68 65 43 74 72 20 2a 2f 0a 20 20 69 6e 74 acheCtr */. int
a90f0 20 70 61 79 6c 6f 61 64 53 69 7a 65 3b 20 20 20 payloadSize;
a9100 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 /* Total numb
a9110 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 er of bytes in t
a9120 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 75 he record */. u
a9130 33 32 20 2a 61 54 79 70 65 3b 20 20 20 20 20 20 32 *aType;
a9140 20 20 20 20 20 2f 2a 20 54 79 70 65 20 76 61 6c /* Type val
a9150 75 65 73 20 66 6f 72 20 61 6c 6c 20 65 6e 74 72 ues for all entr
a9160 69 65 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72 ies in the recor
a9170 64 20 2a 2f 0a 20 20 75 33 32 20 2a 61 4f 66 66 d */. u32 *aOff
a9180 73 65 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 set; /*
a9190 43 61 63 68 65 64 20 6f 66 66 73 65 74 73 20 74 Cached offsets t
a91a0 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 65 o the start of e
a91b0 61 63 68 20 63 6f 6c 75 6d 6e 73 20 64 61 74 61 ach columns data
a91c0 20 2a 2f 0a 20 20 75 38 20 2a 61 52 6f 77 3b 20 */. u8 *aRow;
a91d0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 /* D
a91e0 61 74 61 20 66 6f 72 20 74 68 65 20 63 75 72 72 ata for the curr
a91f0 65 6e 74 20 72 6f 77 2c 20 69 66 20 61 6c 6c 20 ent row, if all
a9200 6f 6e 20 6f 6e 65 20 70 61 67 65 20 2a 2f 0a 7d on one page */.}
a9210 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 ;.typedef struct
a9220 20 56 64 62 65 43 75 72 73 6f 72 20 56 64 62 65 VdbeCursor Vdbe
a9230 43 75 72 73 6f 72 3b 0a 0a 2f 2a 0a 2a 2a 20 57 Cursor;../*.** W
a9240 68 65 6e 20 61 20 73 75 62 2d 70 72 6f 67 72 61 hen a sub-progra
a9250 6d 20 69 73 20 65 78 65 63 75 74 65 64 20 28 4f m is executed (O
a9260 50 5f 50 72 6f 67 72 61 6d 29 2c 20 61 20 73 74 P_Program), a st
a9270 72 75 63 74 75 72 65 20 6f 66 20 74 68 69 73 20 ructure of this
a9280 74 79 70 65 0a 2a 2a 20 69 73 20 61 6c 6c 6f 63 type.** is alloc
a9290 61 74 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68 ated to store th
a92a0 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20 e current value
a92b0 6f 66 20 74 68 65 20 70 72 6f 67 72 61 6d 20 63 of the program c
a92c0 6f 75 6e 74 65 72 2c 20 61 73 0a 2a 2a 20 77 65 ounter, as.** we
a92d0 6c 6c 20 61 73 20 74 68 65 20 63 75 72 72 65 6e ll as the curren
a92e0 74 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 61 72 t memory cell ar
a92f0 72 61 79 20 61 6e 64 20 76 61 72 69 6f 75 73 20 ray and various
a9300 6f 74 68 65 72 20 66 72 61 6d 65 20 73 70 65 63 other frame spec
a9310 69 66 69 63 0a 2a 2a 20 76 61 6c 75 65 73 20 73 ific.** values s
a9320 74 6f 72 65 64 20 69 6e 20 74 68 65 20 56 64 62 tored in the Vdb
a9330 65 20 73 74 72 75 63 74 2e 20 57 68 65 6e 20 74 e struct. When t
a9340 68 65 20 73 75 62 2d 70 72 6f 67 72 61 6d 20 69 he sub-program i
a9350 73 20 66 69 6e 69 73 68 65 64 2c 20 0a 2a 2a 20 s finished, .**
a9360 74 68 65 73 65 20 76 61 6c 75 65 73 20 61 72 65 these values are
a9370 20 63 6f 70 69 65 64 20 62 61 63 6b 20 74 6f 20 copied back to
a9380 74 68 65 20 56 64 62 65 20 66 72 6f 6d 20 74 68 the Vdbe from th
a9390 65 20 56 64 62 65 46 72 61 6d 65 20 73 74 72 75 e VdbeFrame stru
a93a0 63 74 75 72 65 2c 0a 2a 2a 20 72 65 73 74 6f 72 cture,.** restor
a93b0 69 6e 67 20 74 68 65 20 73 74 61 74 65 20 6f 66 ing the state of
a93c0 20 74 68 65 20 56 4d 20 74 6f 20 61 73 20 69 74 the VM to as it
a93d0 20 77 61 73 20 62 65 66 6f 72 65 20 74 68 65 20 was before the
a93e0 73 75 62 2d 70 72 6f 67 72 61 6d 0a 2a 2a 20 62 sub-program.** b
a93f0 65 67 61 6e 20 65 78 65 63 75 74 69 6e 67 2e 0a egan executing..
a9400 2a 2a 0a 2a 2a 20 46 72 61 6d 65 73 20 61 72 65 **.** Frames are
a9410 20 73 74 6f 72 65 64 20 69 6e 20 61 20 6c 69 6e stored in a lin
a9420 6b 65 64 20 6c 69 73 74 20 68 65 61 64 65 64 20 ked list headed
a9430 61 74 20 56 64 62 65 2e 70 50 61 72 65 6e 74 2e at Vdbe.pParent.
a9440 20 56 64 62 65 2e 70 50 61 72 65 6e 74 0a 2a 2a Vdbe.pParent.**
a9450 20 69 73 20 74 68 65 20 70 61 72 65 6e 74 20 6f is the parent o
a9460 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 66 72 f the current fr
a9470 61 6d 65 2c 20 6f 72 20 7a 65 72 6f 20 69 66 20 ame, or zero if
a9480 74 68 65 20 63 75 72 72 65 6e 74 20 66 72 61 6d the current fram
a9490 65 0a 2a 2a 20 69 73 20 74 68 65 20 6d 61 69 6e e.** is the main
a94a0 20 56 64 62 65 20 70 72 6f 67 72 61 6d 2e 0a 2a Vdbe program..*
a94b0 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 /.typedef struct
a94c0 20 56 64 62 65 46 72 61 6d 65 20 56 64 62 65 46 VdbeFrame VdbeF
a94d0 72 61 6d 65 3b 0a 73 74 72 75 63 74 20 56 64 62 rame;.struct Vdb
a94e0 65 46 72 61 6d 65 20 7b 0a 20 20 56 64 62 65 20 eFrame {. Vdbe
a94f0 2a 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 *v;
a9500 20 20 20 2f 2a 20 56 4d 20 74 68 69 73 20 66 72 /* VM this fr
a9510 61 6d 65 20 62 65 6c 6f 6e 67 73 20 74 6f 20 2a ame belongs to *
a9520 2f 0a 20 20 69 6e 74 20 70 63 3b 20 20 20 20 20 /. int pc;
a9530 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 /* P
a9540 72 6f 67 72 61 6d 20 43 6f 75 6e 74 65 72 20 2a rogram Counter *
a9550 2f 0a 20 20 4f 70 20 2a 61 4f 70 3b 20 20 20 20 /. Op *aOp;
a9560 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 /* P
a9570 72 6f 67 72 61 6d 20 69 6e 73 74 72 75 63 74 69 rogram instructi
a9580 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f 70 ons */. int nOp
a9590 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
a95a0 20 2f 2a 20 53 69 7a 65 20 6f 66 20 61 4f 70 20 /* Size of aOp
a95b0 61 72 72 61 79 20 2a 2f 0a 20 20 4d 65 6d 20 2a array */. Mem *
a95c0 61 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 aMem;
a95d0 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 6d /* Array of m
a95e0 65 6d 6f 72 79 20 63 65 6c 6c 73 20 2a 2f 0a 20 emory cells */.
a95f0 20 69 6e 74 20 6e 4d 65 6d 3b 20 20 20 20 20 20 int nMem;
a9600 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
a9610 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e er of entries in
a9620 20 61 4d 65 6d 20 2a 2f 0a 20 20 56 64 62 65 43 aMem */. VdbeC
a9630 75 72 73 6f 72 20 2a 2a 61 70 43 73 72 3b 20 20 ursor **apCsr;
a9640 20 20 20 2f 2a 20 45 6c 65 6d 65 6e 74 20 6f 66 /* Element of
a9650 20 56 64 62 65 20 63 75 72 73 6f 72 73 20 2a 2f Vdbe cursors */
a9660 0a 20 20 75 31 36 20 6e 43 75 72 73 6f 72 3b 20 . u16 nCursor;
a9670 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
a9680 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 mber of entries
a9690 69 6e 20 61 70 43 73 72 20 2a 2f 0a 20 20 76 6f in apCsr */. vo
a96a0 69 64 20 2a 74 6f 6b 65 6e 3b 20 20 20 20 20 20 id *token;
a96b0 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 /* Copy of
a96c0 20 53 75 62 50 72 6f 67 72 61 6d 2e 74 6f 6b 65 SubProgram.toke
a96d0 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 68 69 6c n */. int nChil
a96e0 64 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 2f dMem; /
a96f0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 65 6d 6f * Number of memo
a9700 72 79 20 63 65 6c 6c 73 20 66 6f 72 20 63 68 69 ry cells for chi
a9710 6c 64 20 66 72 61 6d 65 20 2a 2f 0a 20 20 69 6e ld frame */. in
a9720 74 20 6e 43 68 69 6c 64 43 73 72 3b 20 20 20 20 t nChildCsr;
a9730 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
a9740 6f 66 20 63 75 72 73 6f 72 73 20 66 6f 72 20 63 of cursors for c
a9750 68 69 6c 64 20 66 72 61 6d 65 20 2a 2f 0a 20 20 hild frame */.
a9760 69 36 34 20 6c 61 73 74 52 6f 77 69 64 3b 20 20 i64 lastRowid;
a9770 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 /* Last
a9780 69 6e 73 65 72 74 20 72 6f 77 69 64 20 28 73 71 insert rowid (sq
a9790 6c 69 74 65 33 2e 6c 61 73 74 52 6f 77 69 64 29 lite3.lastRowid)
a97a0 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 68 61 6e 67 */. int nChang
a97b0 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a e; /*
a97c0 20 53 74 61 74 65 6d 65 6e 74 20 63 68 61 6e 67 Statement chang
a97d0 65 73 20 28 56 64 62 65 2e 6e 43 68 61 6e 67 65 es (Vdbe.nChange
a97e0 73 29 20 20 20 20 20 2a 2f 0a 20 20 56 64 62 65 s) */. Vdbe
a97f0 46 72 61 6d 65 20 2a 70 50 61 72 65 6e 74 3b 20 Frame *pParent;
a9800 20 20 20 20 2f 2a 20 50 61 72 65 6e 74 20 6f 66 /* Parent of
a9810 20 74 68 69 73 20 66 72 61 6d 65 20 2a 2f 0a 7d this frame */.}
a9820 3b 0a 0a 23 64 65 66 69 6e 65 20 56 64 62 65 46 ;..#define VdbeF
a9830 72 61 6d 65 4d 65 6d 28 70 29 20 28 28 4d 65 6d rameMem(p) ((Mem
a9840 20 2a 29 26 28 28 75 38 20 2a 29 70 29 5b 52 4f *)&((u8 *)p)[RO
a9850 55 4e 44 38 28 73 69 7a 65 6f 66 28 56 64 62 65 UND8(sizeof(Vdbe
a9860 46 72 61 6d 65 29 29 5d 29 0a 0a 2f 2a 0a 2a 2a Frame))])../*.**
a9870 20 41 20 76 61 6c 75 65 20 66 6f 72 20 56 64 62 A value for Vdb
a9880 65 43 75 72 73 6f 72 2e 63 61 63 68 65 56 61 6c eCursor.cacheVal
a9890 69 64 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 id that means th
a98a0 65 20 63 61 63 68 65 20 69 73 20 61 6c 77 61 79 e cache is alway
a98b0 73 20 69 6e 76 61 6c 69 64 2e 0a 2a 2f 0a 23 64 s invalid..*/.#d
a98c0 65 66 69 6e 65 20 43 41 43 48 45 5f 53 54 41 4c efine CACHE_STAL
a98d0 45 20 30 0a 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 E 0../*.** Inter
a98e0 6e 61 6c 6c 79 2c 20 74 68 65 20 76 64 62 65 20 nally, the vdbe
a98f0 6d 61 6e 69 70 75 6c 61 74 65 73 20 6e 65 61 72 manipulates near
a9900 6c 79 20 61 6c 6c 20 53 51 4c 20 76 61 6c 75 65 ly all SQL value
a9910 73 20 61 73 20 4d 65 6d 0a 2a 2a 20 73 74 72 75 s as Mem.** stru
a9920 63 74 75 72 65 73 2e 20 45 61 63 68 20 4d 65 6d ctures. Each Mem
a9930 20 73 74 72 75 63 74 20 6d 61 79 20 63 61 63 68 struct may cach
a9940 65 20 6d 75 6c 74 69 70 6c 65 20 72 65 70 72 65 e multiple repre
a9950 73 65 6e 74 61 74 69 6f 6e 73 20 28 73 74 72 69 sentations (stri
a9960 6e 67 2c 0a 2a 2a 20 69 6e 74 65 67 65 72 20 65 ng,.** integer e
a9970 74 63 2e 29 20 6f 66 20 74 68 65 20 73 61 6d 65 tc.) of the same
a9980 20 76 61 6c 75 65 2e 20 20 41 20 76 61 6c 75 65 value. A value
a9990 20 28 61 6e 64 20 74 68 65 72 65 66 6f 72 65 20 (and therefore
a99a0 4d 65 6d 20 73 74 72 75 63 74 75 72 65 29 0a 2a Mem structure).*
a99b0 2a 20 68 61 73 20 74 68 65 20 66 6f 6c 6c 6f 77 * has the follow
a99c0 69 6e 67 20 70 72 6f 70 65 72 74 69 65 73 3a 0a ing properties:.
a99d0 2a 2a 0a 2a 2a 20 45 61 63 68 20 76 61 6c 75 65 **.** Each value
a99e0 20 68 61 73 20 61 20 6d 61 6e 69 66 65 73 74 20 has a manifest
a99f0 74 79 70 65 2e 20 54 68 65 20 6d 61 6e 69 66 65 type. The manife
a9a00 73 74 20 74 79 70 65 20 6f 66 20 74 68 65 20 76 st type of the v
a9a10 61 6c 75 65 20 73 74 6f 72 65 64 0a 2a 2a 20 69 alue stored.** i
a9a20 6e 20 61 20 4d 65 6d 20 73 74 72 75 63 74 20 69 n a Mem struct i
a9a30 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 s returned by th
a9a40 65 20 4d 65 6d 54 79 70 65 28 4d 65 6d 2a 29 20 e MemType(Mem*)
a9a50 6d 61 63 72 6f 2e 20 54 68 65 20 74 79 70 65 20 macro. The type
a9a60 69 73 0a 2a 2a 20 6f 6e 65 20 6f 66 20 53 51 4c is.** one of SQL
a9a70 49 54 45 5f 4e 55 4c 4c 2c 20 53 51 4c 49 54 45 ITE_NULL, SQLITE
a9a80 5f 49 4e 54 45 47 45 52 2c 20 53 51 4c 49 54 45 _INTEGER, SQLITE
a9a90 5f 52 45 41 4c 2c 20 53 51 4c 49 54 45 5f 54 45 _REAL, SQLITE_TE
a9aa0 58 54 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 45 5f XT or.** SQLITE_
a9ab0 42 4c 4f 42 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 BLOB..*/.struct
a9ac0 4d 65 6d 20 7b 0a 20 20 75 6e 69 6f 6e 20 7b 0a Mem {. union {.
a9ad0 20 20 20 20 69 36 34 20 69 3b 20 20 20 20 20 20 i64 i;
a9ae0 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 74 65 67 /* Integ
a9af0 65 72 20 76 61 6c 75 65 2e 20 2a 2f 0a 20 20 20 er value. */.
a9b00 20 69 6e 74 20 6e 5a 65 72 6f 3b 20 20 20 20 20 int nZero;
a9b10 20 20 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65 /* Used whe
a9b20 6e 20 62 69 74 20 4d 45 4d 5f 5a 65 72 6f 20 69 n bit MEM_Zero i
a9b30 73 20 73 65 74 20 69 6e 20 66 6c 61 67 73 20 2a s set in flags *
a9b40 2f 0a 20 20 20 20 46 75 6e 63 44 65 66 20 2a 70 /. FuncDef *p
a9b50 44 65 66 3b 20 20 20 20 20 20 2f 2a 20 55 73 65 Def; /* Use
a9b60 64 20 6f 6e 6c 79 20 77 68 65 6e 20 66 6c 61 67 d only when flag
a9b70 73 3d 3d 4d 45 4d 5f 41 67 67 20 2a 2f 0a 20 20 s==MEM_Agg */.
a9b80 20 20 52 6f 77 53 65 74 20 2a 70 52 6f 77 53 65 RowSet *pRowSe
a9b90 74 3b 20 20 20 20 2f 2a 20 55 73 65 64 20 6f 6e t; /* Used on
a9ba0 6c 79 20 77 68 65 6e 20 66 6c 61 67 73 3d 3d 4d ly when flags==M
a9bb0 45 4d 5f 52 6f 77 53 65 74 20 2a 2f 0a 20 20 20 EM_RowSet */.
a9bc0 20 56 64 62 65 46 72 61 6d 65 20 2a 70 46 72 61 VdbeFrame *pFra
a9bd0 6d 65 3b 20 20 2f 2a 20 55 73 65 64 20 77 68 65 me; /* Used whe
a9be0 6e 20 66 6c 61 67 73 3d 3d 4d 45 4d 5f 46 72 61 n flags==MEM_Fra
a9bf0 6d 65 20 2a 2f 0a 20 20 7d 20 75 3b 0a 20 20 64 me */. } u;. d
a9c00 6f 75 62 6c 65 20 72 3b 20 20 20 20 20 20 20 20 ouble r;
a9c10 20 20 20 2f 2a 20 52 65 61 6c 20 76 61 6c 75 65 /* Real value
a9c20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 */. sqlite3 *d
a9c30 62 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 b; /* The
a9c40 20 61 73 73 6f 63 69 61 74 65 64 20 64 61 74 61 associated data
a9c50 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
a9c60 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 3b 20 20 20 */. char *z;
a9c70 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 72 69 /* Stri
a9c80 6e 67 20 6f 72 20 42 4c 4f 42 20 76 61 6c 75 65 ng or BLOB value
a9c90 20 2a 2f 0a 20 20 69 6e 74 20 6e 3b 20 20 20 20 */. int n;
a9ca0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
a9cb0 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 65 72 ber of character
a9cc0 73 20 69 6e 20 73 74 72 69 6e 67 20 76 61 6c 75 s in string valu
a9cd0 65 2c 20 65 78 63 6c 75 64 69 6e 67 20 27 5c 30 e, excluding '\0
a9ce0 27 20 2a 2f 0a 20 20 75 31 36 20 66 6c 61 67 73 ' */. u16 flags
a9cf0 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 6f ; /* So
a9d00 6d 65 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f me combination o
a9d10 66 20 4d 45 4d 5f 4e 75 6c 6c 2c 20 4d 45 4d 5f f MEM_Null, MEM_
a9d20 53 74 72 2c 20 4d 45 4d 5f 44 79 6e 2c 20 65 74 Str, MEM_Dyn, et
a9d30 63 2e 20 2a 2f 0a 20 20 75 38 20 20 74 79 70 65 c. */. u8 type
a9d40 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f ; /* O
a9d50 6e 65 20 6f 66 20 53 51 4c 49 54 45 5f 4e 55 4c ne of SQLITE_NUL
a9d60 4c 2c 20 53 51 4c 49 54 45 5f 54 45 58 54 2c 20 L, SQLITE_TEXT,
a9d70 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 2c 20 SQLITE_INTEGER,
a9d80 65 74 63 20 2a 2f 0a 20 20 75 38 20 20 65 6e 63 etc */. u8 enc
a9d90 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
a9da0 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c SQLITE_UTF8, SQL
a9db0 49 54 45 5f 55 54 46 31 36 42 45 2c 20 53 51 4c ITE_UTF16BE, SQL
a9dc0 49 54 45 5f 55 54 46 31 36 4c 45 20 2a 2f 0a 20 ITE_UTF16LE */.
a9dd0 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f void (*xDel)(vo
a9de0 69 64 20 2a 29 3b 20 20 2f 2a 20 49 66 20 6e 6f id *); /* If no
a9df0 74 20 6e 75 6c 6c 2c 20 63 61 6c 6c 20 74 68 69 t null, call thi
a9e00 73 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 64 65 s function to de
a9e10 6c 65 74 65 20 4d 65 6d 2e 7a 20 2a 2f 0a 20 20 lete Mem.z */.
a9e20 63 68 61 72 20 2a 7a 4d 61 6c 6c 6f 63 3b 20 20 char *zMalloc;
a9e30 20 20 20 20 2f 2a 20 44 79 6e 61 6d 69 63 20 62 /* Dynamic b
a9e40 75 66 66 65 72 20 61 6c 6c 6f 63 61 74 65 64 20 uffer allocated
a9e50 62 79 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f by sqlite3_mallo
a9e60 63 28 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 4f 6e c() */.};../* On
a9e70 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74 68 65 e or more of the
a9e80 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6c 61 67 73 following flags
a9e90 20 61 72 65 20 73 65 74 20 74 6f 20 69 6e 64 69 are set to indi
a9ea0 63 61 74 65 20 74 68 65 20 76 61 6c 69 64 4f 4b cate the validOK
a9eb0 0a 2a 2a 20 72 65 70 72 65 73 65 6e 74 61 74 69 .** representati
a9ec0 6f 6e 73 20 6f 66 20 74 68 65 20 76 61 6c 75 65 ons of the value
a9ed0 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 4d stored in the M
a9ee0 65 6d 20 73 74 72 75 63 74 2e 0a 2a 2a 0a 2a 2a em struct..**.**
a9ef0 20 49 66 20 74 68 65 20 4d 45 4d 5f 4e 75 6c 6c If the MEM_Null
a9f00 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 74 68 flag is set, th
a9f10 65 6e 20 74 68 65 20 76 61 6c 75 65 20 69 73 20 en the value is
a9f20 61 6e 20 53 51 4c 20 4e 55 4c 4c 20 76 61 6c 75 an SQL NULL valu
a9f30 65 2e 0a 2a 2a 20 4e 6f 20 6f 74 68 65 72 20 66 e..** No other f
a9f40 6c 61 67 73 20 6d 61 79 20 62 65 20 73 65 74 20 lags may be set
a9f50 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a 2a 2a in this case..**
a9f60 0a 2a 2a 20 49 66 20 74 68 65 20 4d 45 4d 5f 53 .** If the MEM_S
a9f70 74 72 20 66 6c 61 67 20 69 73 20 73 65 74 20 74 tr flag is set t
a9f80 68 65 6e 20 4d 65 6d 2e 7a 20 70 6f 69 6e 74 73 hen Mem.z points
a9f90 20 61 74 20 61 20 73 74 72 69 6e 67 20 72 65 70 at a string rep
a9fa0 72 65 73 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 20 resentation..**
a9fb0 55 73 75 61 6c 6c 79 20 74 68 69 73 20 69 73 20 Usually this is
a9fc0 65 6e 63 6f 64 65 64 20 69 6e 20 74 68 65 20 73 encoded in the s
a9fd0 61 6d 65 20 75 6e 69 63 6f 64 65 20 65 6e 63 6f ame unicode enco
a9fe0 64 69 6e 67 20 61 73 20 74 68 65 20 6d 61 69 6e ding as the main
a9ff0 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 28 73 65 .** database (se
aa000 65 20 62 65 6c 6f 77 20 66 6f 72 20 65 78 63 65 e below for exce
aa010 70 74 69 6f 6e 73 29 2e 20 49 66 20 74 68 65 20 ptions). If the
aa020 4d 45 4d 5f 54 65 72 6d 20 66 6c 61 67 20 69 73 MEM_Term flag is
aa030 20 61 6c 73 6f 0a 2a 2a 20 73 65 74 2c 20 74 68 also.** set, th
aa040 65 6e 20 74 68 65 20 73 74 72 69 6e 67 20 69 73 en the string is
aa050 20 6e 75 6c 20 74 65 72 6d 69 6e 61 74 65 64 2e nul terminated.
aa060 20 54 68 65 20 4d 45 4d 5f 49 6e 74 20 61 6e 64 The MEM_Int and
aa070 20 4d 45 4d 5f 52 65 61 6c 20 0a 2a 2a 20 66 6c MEM_Real .** fl
aa080 61 67 73 20 6d 61 79 20 63 6f 65 78 69 73 74 20 ags may coexist
aa090 77 69 74 68 20 74 68 65 20 4d 45 4d 5f 53 74 72 with the MEM_Str
aa0a0 20 66 6c 61 67 2e 0a 2a 2a 0a 2a 2a 20 4d 75 6c flag..**.** Mul
aa0b0 74 69 70 6c 65 20 6f 66 20 74 68 65 73 65 20 76 tiple of these v
aa0c0 61 6c 75 65 73 20 63 61 6e 20 61 70 70 65 61 72 alues can appear
aa0d0 20 69 6e 20 4d 65 6d 2e 66 6c 61 67 73 2e 20 20 in Mem.flags.
aa0e0 42 75 74 20 6f 6e 6c 79 20 6f 6e 65 0a 2a 2a 20 But only one.**
aa0f0 61 74 20 61 20 74 69 6d 65 20 63 61 6e 20 61 70 at a time can ap
aa100 70 65 61 72 20 69 6e 20 4d 65 6d 2e 74 79 70 65 pear in Mem.type
aa110 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d ..*/.#define MEM
aa120 5f 4e 75 6c 6c 20 20 20 20 20 20 30 78 30 30 30 _Null 0x000
aa130 31 20 20 20 2f 2a 20 56 61 6c 75 65 20 69 73 20 1 /* Value is
aa140 4e 55 4c 4c 20 2a 2f 0a 23 64 65 66 69 6e 65 20 NULL */.#define
aa150 4d 45 4d 5f 53 74 72 20 20 20 20 20 20 20 30 78 MEM_Str 0x
aa160 30 30 30 32 20 20 20 2f 2a 20 56 61 6c 75 65 20 0002 /* Value
aa170 69 73 20 61 20 73 74 72 69 6e 67 20 2a 2f 0a 23 is a string */.#
aa180 64 65 66 69 6e 65 20 4d 45 4d 5f 49 6e 74 20 20 define MEM_Int
aa190 20 20 20 20 20 30 78 30 30 30 34 20 20 20 2f 2a 0x0004 /*
aa1a0 20 56 61 6c 75 65 20 69 73 20 61 6e 20 69 6e 74 Value is an int
aa1b0 65 67 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 eger */.#define
aa1c0 4d 45 4d 5f 52 65 61 6c 20 20 20 20 20 20 30 78 MEM_Real 0x
aa1d0 30 30 30 38 20 20 20 2f 2a 20 56 61 6c 75 65 20 0008 /* Value
aa1e0 69 73 20 61 20 72 65 61 6c 20 6e 75 6d 62 65 72 is a real number
aa1f0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f */.#define MEM_
aa200 42 6c 6f 62 20 20 20 20 20 20 30 78 30 30 31 30 Blob 0x0010
aa210 20 20 20 2f 2a 20 56 61 6c 75 65 20 69 73 20 61 /* Value is a
aa220 20 42 4c 4f 42 20 2a 2f 0a 23 64 65 66 69 6e 65 BLOB */.#define
aa230 20 4d 45 4d 5f 52 6f 77 53 65 74 20 20 20 20 30 MEM_RowSet 0
aa240 78 30 30 32 30 20 20 20 2f 2a 20 56 61 6c 75 65 x0020 /* Value
aa250 20 69 73 20 61 20 52 6f 77 53 65 74 20 6f 62 6a is a RowSet obj
aa260 65 63 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d ect */.#define M
aa270 45 4d 5f 46 72 61 6d 65 20 20 20 20 20 30 78 30 EM_Frame 0x0
aa280 30 34 30 20 20 20 2f 2a 20 56 61 6c 75 65 20 69 040 /* Value i
aa290 73 20 61 20 56 64 62 65 46 72 61 6d 65 20 6f 62 s a VdbeFrame ob
aa2a0 6a 65 63 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 ject */.#define
aa2b0 4d 45 4d 5f 54 79 70 65 4d 61 73 6b 20 20 30 78 MEM_TypeMask 0x
aa2c0 30 30 66 66 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 00ff /* Mask o
aa2d0 66 20 74 79 70 65 20 62 69 74 73 20 2a 2f 0a 0a f type bits */..
aa2e0 2f 2a 20 57 68 65 6e 65 76 65 72 20 4d 65 6d 20 /* Whenever Mem
aa2f0 63 6f 6e 74 61 69 6e 73 20 61 20 76 61 6c 69 64 contains a valid
aa300 20 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 string or blob
aa310 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 2c 20 representation,
aa320 6f 6e 65 20 6f 66 0a 2a 2a 20 74 68 65 20 66 6f one of.** the fo
aa330 6c 6c 6f 77 69 6e 67 20 66 6c 61 67 73 20 6d 75 llowing flags mu
aa340 73 74 20 62 65 20 73 65 74 20 74 6f 20 64 65 74 st be set to det
aa350 65 72 6d 69 6e 65 20 74 68 65 20 6d 65 6d 6f 72 ermine the memor
aa360 79 20 6d 61 6e 61 67 65 6d 65 6e 74 0a 2a 2a 20 y management.**
aa370 70 6f 6c 69 63 79 20 66 6f 72 20 4d 65 6d 2e 7a policy for Mem.z
aa380 2e 20 20 54 68 65 20 4d 45 4d 5f 54 65 72 6d 20 . The MEM_Term
aa390 66 6c 61 67 20 74 65 6c 6c 73 20 75 73 20 77 68 flag tells us wh
aa3a0 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 ether or not the
aa3b0 0a 2a 2a 20 73 74 72 69 6e 67 20 69 73 20 5c 30 .** string is \0
aa3c0 30 30 20 6f 72 20 5c 75 30 30 30 30 20 74 65 72 00 or \u0000 ter
aa3d0 6d 69 6e 61 74 65 64 0a 2a 2f 0a 23 64 65 66 69 minated.*/.#defi
aa3e0 6e 65 20 4d 45 4d 5f 54 65 72 6d 20 20 20 20 20 ne MEM_Term
aa3f0 20 30 78 30 32 30 30 20 20 20 2f 2a 20 53 74 72 0x0200 /* Str
aa400 69 6e 67 20 72 65 70 20 69 73 20 6e 75 6c 20 74 ing rep is nul t
aa410 65 72 6d 69 6e 61 74 65 64 20 2a 2f 0a 23 64 65 erminated */.#de
aa420 66 69 6e 65 20 4d 45 4d 5f 44 79 6e 20 20 20 20 fine MEM_Dyn
aa430 20 20 20 30 78 30 34 30 30 20 20 20 2f 2a 20 4e 0x0400 /* N
aa440 65 65 64 20 74 6f 20 63 61 6c 6c 20 73 71 6c 69 eed to call sqli
aa450 74 65 46 72 65 65 28 29 20 6f 6e 20 4d 65 6d 2e teFree() on Mem.
aa460 7a 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d z */.#define MEM
aa470 5f 53 74 61 74 69 63 20 20 20 20 30 78 30 38 30 _Static 0x080
aa480 30 20 20 20 2f 2a 20 4d 65 6d 2e 7a 20 70 6f 69 0 /* Mem.z poi
aa490 6e 74 73 20 74 6f 20 61 20 73 74 61 74 69 63 20 nts to a static
aa4a0 73 74 72 69 6e 67 20 2a 2f 0a 23 64 65 66 69 6e string */.#defin
aa4b0 65 20 4d 45 4d 5f 45 70 68 65 6d 20 20 20 20 20 e MEM_Ephem
aa4c0 30 78 31 30 30 30 20 20 20 2f 2a 20 4d 65 6d 2e 0x1000 /* Mem.
aa4d0 7a 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e 20 65 z points to an e
aa4e0 70 68 65 6d 65 72 61 6c 20 73 74 72 69 6e 67 20 phemeral string
aa4f0 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f 41 */.#define MEM_A
aa500 67 67 20 20 20 20 20 20 20 30 78 32 30 30 30 20 gg 0x2000
aa510 20 20 2f 2a 20 4d 65 6d 2e 7a 20 70 6f 69 6e 74 /* Mem.z point
aa520 73 20 74 6f 20 61 6e 20 61 67 67 20 66 75 6e 63 s to an agg func
aa530 74 69 6f 6e 20 63 6f 6e 74 65 78 74 20 2a 2f 0a tion context */.
aa540 23 64 65 66 69 6e 65 20 4d 45 4d 5f 5a 65 72 6f #define MEM_Zero
aa550 20 20 20 20 20 20 30 78 34 30 30 30 20 20 20 2f 0x4000 /
aa560 2a 20 4d 65 6d 2e 69 20 63 6f 6e 74 61 69 6e 73 * Mem.i contains
aa570 20 63 6f 75 6e 74 20 6f 66 20 30 73 20 61 70 70 count of 0s app
aa580 65 6e 64 65 64 20 74 6f 20 62 6c 6f 62 20 2a 2f ended to blob */
aa590 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ..#ifdef SQLITE_
aa5a0 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 OMIT_INCRBLOB.
aa5b0 23 75 6e 64 65 66 20 4d 45 4d 5f 5a 65 72 6f 0a #undef MEM_Zero.
aa5c0 20 20 23 64 65 66 69 6e 65 20 4d 45 4d 5f 5a 65 #define MEM_Ze
aa5d0 72 6f 20 30 78 30 30 30 30 0a 23 65 6e 64 69 66 ro 0x0000.#endif
aa5e0 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 61 .../*.** Clear a
aa5f0 6e 79 20 65 78 69 73 74 69 6e 67 20 74 79 70 65 ny existing type
aa600 20 66 6c 61 67 73 20 66 72 6f 6d 20 61 20 4d 65 flags from a Me
aa610 6d 20 61 6e 64 20 72 65 70 6c 61 63 65 20 74 68 m and replace th
aa620 65 6d 20 77 69 74 68 20 66 0a 2a 2f 0a 23 64 65 em with f.*/.#de
aa630 66 69 6e 65 20 4d 65 6d 53 65 74 54 79 70 65 46 fine MemSetTypeF
aa640 6c 61 67 28 70 2c 20 66 29 20 5c 0a 20 20 20 28 lag(p, f) \. (
aa650 28 70 29 2d 3e 66 6c 61 67 73 20 3d 20 28 28 70 (p)->flags = ((p
aa660 29 2d 3e 66 6c 61 67 73 26 7e 28 4d 45 4d 5f 54 )->flags&~(MEM_T
aa670 79 70 65 4d 61 73 6b 7c 4d 45 4d 5f 5a 65 72 6f ypeMask|MEM_Zero
aa680 29 29 7c 66 29 0a 0a 0a 2f 2a 20 41 20 56 64 62 ))|f).../* A Vdb
aa690 65 46 75 6e 63 20 69 73 20 6a 75 73 74 20 61 20 eFunc is just a
aa6a0 46 75 6e 63 44 65 66 20 28 64 65 66 69 6e 65 64 FuncDef (defined
aa6b0 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 29 in sqliteInt.h)
aa6c0 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 0a 2a that contains.*
aa6d0 2a 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 * additional inf
aa6e0 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 61 ormation about a
aa6f0 75 78 69 6c 69 61 72 79 20 69 6e 66 6f 72 6d 61 uxiliary informa
aa700 74 69 6f 6e 20 62 6f 75 6e 64 20 74 6f 20 61 72 tion bound to ar
aa710 67 75 6d 65 6e 74 73 0a 2a 2a 20 6f 66 20 74 68 guments.** of th
aa720 65 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 69 e function. Thi
aa730 73 20 69 73 20 75 73 65 64 20 74 6f 20 69 6d 70 s is used to imp
aa740 6c 65 6d 65 6e 74 20 74 68 65 20 73 71 6c 69 74 lement the sqlit
aa750 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61 28 29 e3_get_auxdata()
aa760 0a 2a 2a 20 61 6e 64 20 73 71 6c 69 74 65 33 5f .** and sqlite3_
aa770 73 65 74 5f 61 75 78 64 61 74 61 28 29 20 41 50 set_auxdata() AP
aa780 49 73 2e 20 20 54 68 65 20 22 61 75 78 64 61 74 Is. The "auxdat
aa790 61 22 20 69 73 20 73 6f 6d 65 20 61 75 78 69 6c a" is some auxil
aa7a0 69 61 72 79 20 64 61 74 61 0a 2a 2a 20 74 68 61 iary data.** tha
aa7b0 74 20 63 61 6e 20 62 65 20 61 73 73 6f 63 69 61 t can be associa
aa7c0 74 65 64 20 77 69 74 68 20 61 20 63 6f 6e 73 74 ted with a const
aa7d0 61 6e 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 ant argument to
aa7e0 61 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 69 a function. Thi
aa7f0 73 0a 2a 2a 20 61 6c 6c 6f 77 73 20 66 75 6e 63 s.** allows func
aa800 74 69 6f 6e 73 20 73 75 63 68 20 61 73 20 22 72 tions such as "r
aa810 65 67 65 78 70 22 20 74 6f 20 63 6f 6d 70 69 6c egexp" to compil
aa820 65 20 74 68 65 69 72 20 63 6f 6e 73 74 61 6e 74 e their constant
aa830 20 72 65 67 75 6c 61 72 0a 2a 2a 20 65 78 70 72 regular.** expr
aa840 65 73 73 69 6f 6e 20 61 72 67 75 6d 65 6e 74 20 ession argument
aa850 6f 6e 63 65 20 61 6e 64 20 72 65 75 73 65 64 20 once and reused
aa860 74 68 65 20 63 6f 6d 70 69 6c 65 64 20 63 6f 64 the compiled cod
aa870 65 20 66 6f 72 20 6d 75 6c 74 69 70 6c 65 0a 2a e for multiple.*
aa880 2a 20 69 6e 76 6f 63 61 74 69 6f 6e 73 2e 0a 2a * invocations..*
aa890 2f 0a 73 74 72 75 63 74 20 56 64 62 65 46 75 6e /.struct VdbeFun
aa8a0 63 20 7b 0a 20 20 46 75 6e 63 44 65 66 20 2a 70 c {. FuncDef *p
aa8b0 46 75 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20 Func;
aa8c0 20 20 20 20 2f 2a 20 54 68 65 20 64 65 66 69 6e /* The defin
aa8d0 69 74 69 6f 6e 20 6f 66 20 74 68 65 20 66 75 6e ition of the fun
aa8e0 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e ction */. int n
aa8f0 41 75 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 Aux;
aa900 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
aa910 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 61 6c er of entries al
aa920 6c 6f 63 61 74 65 64 20 66 6f 72 20 61 70 41 75 located for apAu
aa930 78 5b 5d 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 x[] */. struct
aa940 41 75 78 44 61 74 61 20 7b 0a 20 20 20 20 76 6f AuxData {. vo
aa950 69 64 20 2a 70 41 75 78 3b 20 20 20 20 20 20 20 id *pAux;
aa960 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 /* A
aa970 75 78 20 64 61 74 61 20 66 6f 72 20 74 68 65 20 ux data for the
aa980 69 2d 74 68 20 61 72 67 75 6d 65 6e 74 20 2a 2f i-th argument */
aa990 0a 20 20 20 20 76 6f 69 64 20 28 2a 78 44 65 6c . void (*xDel
aa9a0 65 74 65 29 28 76 6f 69 64 20 2a 29 3b 20 20 20 ete)(void *);
aa9b0 20 20 20 2f 2a 20 44 65 73 74 72 75 63 74 6f 72 /* Destructor
aa9c0 20 66 6f 72 20 74 68 65 20 61 75 78 20 64 61 74 for the aux dat
aa9d0 61 20 2a 2f 0a 20 20 7d 20 61 70 41 75 78 5b 31 a */. } apAux[1
aa9e0 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ];
aa9f0 20 20 20 20 20 2f 2a 20 4f 6e 65 20 73 6c 6f 74 /* One slot
aaa00 20 66 6f 72 20 65 61 63 68 20 66 75 6e 63 74 69 for each functi
aaa10 6f 6e 20 61 72 67 75 6d 65 6e 74 20 2a 2f 0a 7d on argument */.}
aaa20 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 22 63 6f ;../*.** The "co
aaa30 6e 74 65 78 74 22 20 61 72 67 75 6d 65 6e 74 20 ntext" argument
aaa40 66 6f 72 20 61 20 69 6e 73 74 61 6c 6c 61 62 6c for a installabl
aaa50 65 20 66 75 6e 63 74 69 6f 6e 2e 20 20 41 20 70 e function. A p
aaa60 6f 69 6e 74 65 72 20 74 6f 20 61 6e 0a 2a 2a 20 ointer to an.**
aaa70 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 instance of this
aaa80 20 73 74 72 75 63 74 75 72 65 20 69 73 20 74 68 structure is th
aaa90 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 e first argument
aaaa0 20 74 6f 20 74 68 65 20 72 6f 75 74 69 6e 65 73 to the routines
aaab0 20 75 73 65 64 0a 2a 2a 20 69 6d 70 6c 65 6d 65 used.** impleme
aaac0 6e 74 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74 nt the SQL funct
aaad0 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 ions..**.** Ther
aaae0 65 20 69 73 20 61 20 74 79 70 65 64 65 66 20 66 e is a typedef f
aaaf0 6f 72 20 74 68 69 73 20 73 74 72 75 63 74 75 72 or this structur
aab00 65 20 69 6e 20 73 71 6c 69 74 65 2e 68 2e 20 20 e in sqlite.h.
aab10 53 6f 20 61 6c 6c 20 72 6f 75 74 69 6e 65 73 2c So all routines,
aab20 0a 2a 2a 20 65 76 65 6e 20 74 68 65 20 70 75 62 .** even the pub
aab30 6c 69 63 20 69 6e 74 65 72 66 61 63 65 20 74 6f lic interface to
aab40 20 53 51 4c 69 74 65 2c 20 63 61 6e 20 75 73 65 SQLite, can use
aab50 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 a pointer to th
aab60 69 73 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a is structure..**
aab70 20 42 75 74 20 74 68 69 73 20 66 69 6c 65 20 69 But this file i
aab80 73 20 74 68 65 20 6f 6e 6c 79 20 70 6c 61 63 65 s the only place
aab90 20 77 68 65 72 65 20 74 68 65 20 69 6e 74 65 72 where the inter
aaba0 6e 61 6c 20 64 65 74 61 69 6c 73 20 6f 66 20 74 nal details of t
aabb0 68 69 73 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 his.** structure
aabc0 20 61 72 65 20 6b 6e 6f 77 6e 2e 0a 2a 2a 0a 2a are known..**.*
aabd0 2a 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65 * This structure
aabe0 20 69 73 20 64 65 66 69 6e 65 64 20 69 6e 73 69 is defined insi
aabf0 64 65 20 6f 66 20 76 64 62 65 49 6e 74 2e 68 20 de of vdbeInt.h
aac00 62 65 63 61 75 73 65 20 69 74 20 75 73 65 73 20 because it uses
aac10 73 75 62 73 74 72 75 63 74 75 72 65 73 0a 2a 2a substructures.**
aac20 20 28 4d 65 6d 29 20 77 68 69 63 68 20 61 72 65 (Mem) which are
aac30 20 6f 6e 6c 79 20 64 65 66 69 6e 65 64 20 74 68 only defined th
aac40 65 72 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73 ere..*/.struct s
aac50 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 7b qlite3_context {
aac60 0a 20 20 46 75 6e 63 44 65 66 20 2a 70 46 75 6e . FuncDef *pFun
aac70 63 3b 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e c; /* Poin
aac80 74 65 72 20 74 6f 20 66 75 6e 63 74 69 6f 6e 20 ter to function
aac90 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 20 4d 55 information. MU
aaca0 53 54 20 42 45 20 46 49 52 53 54 20 2a 2f 0a 20 ST BE FIRST */.
aacb0 20 56 64 62 65 46 75 6e 63 20 2a 70 56 64 62 65 VdbeFunc *pVdbe
aacc0 46 75 6e 63 3b 20 20 2f 2a 20 41 75 78 69 6c 61 Func; /* Auxila
aacd0 72 79 20 64 61 74 61 2c 20 69 66 20 63 72 65 61 ry data, if crea
aace0 74 65 64 2e 20 2a 2f 0a 20 20 4d 65 6d 20 73 3b ted. */. Mem s;
aacf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aad00 2f 2a 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 /* The return va
aad10 6c 75 65 20 69 73 20 73 74 6f 72 65 64 20 68 65 lue is stored he
aad20 72 65 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4d 65 re */. Mem *pMe
aad30 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a m; /*
aad40 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 75 73 65 Memory cell use
aad50 64 20 74 6f 20 73 74 6f 72 65 20 61 67 67 72 65 d to store aggre
aad60 67 61 74 65 20 63 6f 6e 74 65 78 74 20 2a 2f 0a gate context */.
aad70 20 20 69 6e 74 20 69 73 45 72 72 6f 72 3b 20 20 int isError;
aad80 20 20 20 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 /* Error
aad90 20 63 6f 64 65 20 72 65 74 75 72 6e 65 64 20 62 code returned b
aada0 79 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e 20 y the function.
aadb0 2a 2f 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 */. CollSeq *pC
aadc0 6f 6c 6c 3b 20 20 20 20 20 20 20 2f 2a 20 43 6f oll; /* Co
aadd0 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 llating sequence
aade0 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 */.};../*.** A
aadf0 53 65 74 20 73 74 72 75 63 74 75 72 65 20 69 73 Set structure is
aae00 20 75 73 65 64 20 66 6f 72 20 71 75 69 63 6b 20 used for quick
aae10 74 65 73 74 69 6e 67 20 74 6f 20 73 65 65 20 69 testing to see i
aae20 66 20 61 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 f a value.** is
aae30 70 61 72 74 20 6f 66 20 61 20 73 6d 61 6c 6c 20 part of a small
aae40 73 65 74 2e 20 20 53 65 74 73 20 61 72 65 20 75 set. Sets are u
aae50 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 sed to implement
aae60 20 63 6f 64 65 20 6c 69 6b 65 0a 2a 2a 20 74 68 code like.** th
aae70 69 73 3a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 is:.**
aae80 20 20 78 2e 79 20 49 4e 20 28 27 68 69 27 2c 27 x.y IN ('hi','
aae90 68 6f 6f 27 2c 27 68 75 6d 27 29 0a 2a 2f 0a 74 hoo','hum').*/.t
aaea0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 53 65 ypedef struct Se
aaeb0 74 20 53 65 74 3b 0a 73 74 72 75 63 74 20 53 65 t Set;.struct Se
aaec0 74 20 7b 0a 20 20 48 61 73 68 20 68 61 73 68 3b t {. Hash hash;
aaed0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
aaee0 41 20 73 65 74 20 69 73 20 6a 75 73 74 20 61 20 A set is just a
aaef0 68 61 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20 hash table */.
aaf00 48 61 73 68 45 6c 65 6d 20 2a 70 72 65 76 3b 20 HashElem *prev;
aaf10 20 20 20 20 20 20 20 2f 2a 20 50 72 65 76 69 6f /* Previo
aaf20 75 73 6c 79 20 61 63 63 65 73 73 65 64 20 68 61 usly accessed ha
aaf30 73 68 20 65 6c 65 6d 65 6e 20 2a 2f 0a 7d 3b 0a sh elemen */.};.
aaf40 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e ./*.** An instan
aaf50 63 65 20 6f 66 20 74 68 65 20 76 69 72 74 75 61 ce of the virtua
aaf60 6c 20 6d 61 63 68 69 6e 65 2e 20 20 54 68 69 73 l machine. This
aaf70 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 structure conta
aaf80 69 6e 73 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 ins the complete
aaf90 0a 2a 2a 20 73 74 61 74 65 20 6f 66 20 74 68 65 .** state of the
aafa0 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 virtual machine
aafb0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 22 73 71 6c ..**.** The "sql
aafc0 69 74 65 33 5f 73 74 6d 74 22 20 73 74 72 75 63 ite3_stmt" struc
aafd0 74 75 72 65 20 70 6f 69 6e 74 65 72 20 74 68 61 ture pointer tha
aafe0 74 20 69 73 20 72 65 74 75 72 6e 65 64 20 62 79 t is returned by
aaff0 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 69 6c 65 sqlite3_compile
ab000 28 29 0a 2a 2a 20 69 73 20 72 65 61 6c 6c 79 20 ().** is really
ab010 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 a pointer to an
ab020 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 instance of this
ab030 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a structure..**.*
ab040 2a 20 54 68 65 20 56 64 62 65 2e 69 6e 56 74 61 * The Vdbe.inVta
ab050 62 4d 65 74 68 6f 64 20 76 61 72 69 61 62 6c 65 bMethod variable
ab060 20 69 73 20 73 65 74 20 74 6f 20 6e 6f 6e 2d 7a is set to non-z
ab070 65 72 6f 20 66 6f 72 20 74 68 65 20 64 75 72 61 ero for the dura
ab080 74 69 6f 6e 20 6f 66 0a 2a 2a 20 61 6e 79 20 76 tion of.** any v
ab090 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 65 74 irtual table met
ab0a0 68 6f 64 20 69 6e 76 6f 63 61 74 69 6f 6e 73 20 hod invocations
ab0b0 6d 61 64 65 20 62 79 20 74 68 65 20 76 64 62 65 made by the vdbe
ab0c0 20 70 72 6f 67 72 61 6d 2e 20 49 74 20 69 73 0a program. It is.
ab0d0 2a 2a 20 73 65 74 20 74 6f 20 32 20 66 6f 72 20 ** set to 2 for
ab0e0 78 44 65 73 74 72 6f 79 20 6d 65 74 68 6f 64 20 xDestroy method
ab0f0 63 61 6c 6c 73 20 61 6e 64 20 31 20 66 6f 72 20 calls and 1 for
ab100 61 6c 6c 20 6f 74 68 65 72 20 6d 65 74 68 6f 64 all other method
ab110 73 2e 20 54 68 69 73 0a 2a 2a 20 76 61 72 69 61 s. This.** varia
ab120 62 6c 65 20 69 73 20 75 73 65 64 20 66 6f 72 20 ble is used for
ab130 74 77 6f 20 70 75 72 70 6f 73 65 73 3a 20 74 6f two purposes: to
ab140 20 61 6c 6c 6f 77 20 78 44 65 73 74 72 6f 79 20 allow xDestroy
ab150 6d 65 74 68 6f 64 73 20 74 6f 20 65 78 65 63 75 methods to execu
ab160 74 65 0a 2a 2a 20 22 44 52 4f 50 20 54 41 42 4c te.** "DROP TABL
ab170 45 22 20 73 74 61 74 65 6d 65 6e 74 73 20 61 6e E" statements an
ab180 64 20 74 6f 20 70 72 65 76 65 6e 74 20 73 6f 6d d to prevent som
ab190 65 20 6e 61 73 74 79 20 73 69 64 65 20 65 66 66 e nasty side eff
ab1a0 65 63 74 73 20 6f 66 0a 2a 2a 20 6d 61 6c 6c 6f ects of.** mallo
ab1b0 63 20 66 61 69 6c 75 72 65 20 77 68 65 6e 20 53 c failure when S
ab1c0 51 4c 69 74 65 20 69 73 20 69 6e 76 6f 6b 65 64 QLite is invoked
ab1d0 20 72 65 63 75 72 73 69 76 65 6c 79 20 62 79 20 recursively by
ab1e0 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 a virtual table
ab1f0 0a 2a 2a 20 6d 65 74 68 6f 64 20 66 75 6e 63 74 .** method funct
ab200 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 56 ion..*/.struct V
ab210 64 62 65 20 7b 0a 20 20 73 71 6c 69 74 65 33 20 dbe {. sqlite3
ab220 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 *db;
ab230 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 /* The database
ab240 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74 20 connection that
ab250 6f 77 6e 73 20 74 68 69 73 20 73 74 61 74 65 6d owns this statem
ab260 65 6e 74 20 2a 2f 0a 20 20 56 64 62 65 20 2a 70 ent */. Vdbe *p
ab270 50 72 65 76 2c 2a 70 4e 65 78 74 3b 20 20 20 20 Prev,*pNext;
ab280 20 2f 2a 20 4c 69 6e 6b 65 64 20 6c 69 73 74 20 /* Linked list
ab290 6f 66 20 56 44 42 45 73 20 77 69 74 68 20 74 68 of VDBEs with th
ab2a0 65 20 73 61 6d 65 20 56 64 62 65 2e 64 62 20 2a e same Vdbe.db *
ab2b0 2f 0a 20 20 69 6e 74 20 6e 4f 70 3b 20 20 20 20 /. int nOp;
ab2c0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
ab2d0 75 6d 62 65 72 20 6f 66 20 69 6e 73 74 72 75 63 umber of instruc
ab2e0 74 69 6f 6e 73 20 69 6e 20 74 68 65 20 70 72 6f tions in the pro
ab2f0 67 72 61 6d 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f gram */. int nO
ab300 70 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20 20 pAlloc;
ab310 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 /* Number of s
ab320 6c 6f 74 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 lots allocated f
ab330 6f 72 20 61 4f 70 5b 5d 20 2a 2f 0a 20 20 4f 70 or aOp[] */. Op
ab340 20 2a 61 4f 70 3b 20 20 20 20 20 20 20 20 20 20 *aOp;
ab350 20 20 20 20 20 20 2f 2a 20 53 70 61 63 65 20 74 /* Space t
ab360 6f 20 68 6f 6c 64 20 74 68 65 20 76 69 72 74 75 o hold the virtu
ab370 61 6c 20 6d 61 63 68 69 6e 65 27 73 20 70 72 6f al machine's pro
ab380 67 72 61 6d 20 2a 2f 0a 20 20 69 6e 74 20 6e 4c gram */. int nL
ab390 61 62 65 6c 3b 20 20 20 20 20 20 20 20 20 20 20 abel;
ab3a0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6c /* Number of l
ab3b0 61 62 65 6c 73 20 75 73 65 64 20 2a 2f 0a 20 20 abels used */.
ab3c0 69 6e 74 20 6e 4c 61 62 65 6c 41 6c 6c 6f 63 3b int nLabelAlloc;
ab3d0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
ab3e0 72 20 6f 66 20 73 6c 6f 74 73 20 61 6c 6c 6f 63 r of slots alloc
ab3f0 61 74 65 64 20 69 6e 20 61 4c 61 62 65 6c 5b 5d ated in aLabel[]
ab400 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 4c 61 62 65 */. int *aLabe
ab410 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a l; /*
ab420 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 Space to hold t
ab430 68 65 20 6c 61 62 65 6c 73 20 2a 2f 0a 20 20 4d he labels */. M
ab440 65 6d 20 2a 2a 61 70 41 72 67 3b 20 20 20 20 20 em **apArg;
ab450 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 /* Argume
ab460 6e 74 73 20 74 6f 20 63 75 72 72 65 6e 74 6c 79 nts to currently
ab470 20 65 78 65 63 75 74 69 6e 67 20 75 73 65 72 20 executing user
ab480 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 4d 65 function */. Me
ab490 6d 20 2a 61 43 6f 6c 4e 61 6d 65 3b 20 20 20 20 m *aColName;
ab4a0 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 /* Column
ab4b0 6e 61 6d 65 73 20 74 6f 20 72 65 74 75 72 6e 20 names to return
ab4c0 2a 2f 0a 20 20 4d 65 6d 20 2a 70 52 65 73 75 6c */. Mem *pResul
ab4d0 74 53 65 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 tSet; /*
ab4e0 50 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 61 72 Pointer to an ar
ab4f0 72 61 79 20 6f 66 20 72 65 73 75 6c 74 73 20 2a ray of results *
ab500 2f 0a 20 20 75 31 36 20 6e 52 65 73 43 6f 6c 75 /. u16 nResColu
ab510 6d 6e 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e mn; /* N
ab520 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 umber of columns
ab530 20 69 6e 20 6f 6e 65 20 72 6f 77 20 6f 66 20 74 in one row of t
ab540 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f he result set */
ab550 0a 20 20 75 31 36 20 6e 43 75 72 73 6f 72 3b 20 . u16 nCursor;
ab560 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
ab570 6d 62 65 72 20 6f 66 20 73 6c 6f 74 73 20 69 6e mber of slots in
ab580 20 61 70 43 73 72 5b 5d 20 2a 2f 0a 20 20 56 64 apCsr[] */. Vd
ab590 62 65 43 75 72 73 6f 72 20 2a 2a 61 70 43 73 72 beCursor **apCsr
ab5a0 3b 20 20 20 20 20 2f 2a 20 4f 6e 65 20 65 6c 65 ; /* One ele
ab5b0 6d 65 6e 74 20 6f 66 20 74 68 69 73 20 61 72 72 ment of this arr
ab5c0 61 79 20 66 6f 72 20 65 61 63 68 20 6f 70 65 6e ay for each open
ab5d0 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 75 38 20 cursor */. u8
ab5e0 65 72 72 6f 72 41 63 74 69 6f 6e 3b 20 20 20 20 errorAction;
ab5f0 20 20 20 20 20 2f 2a 20 52 65 63 6f 76 65 72 79 /* Recovery
ab600 20 61 63 74 69 6f 6e 20 74 6f 20 64 6f 20 69 6e action to do in
ab610 20 63 61 73 65 20 6f 66 20 61 6e 20 65 72 72 6f case of an erro
ab620 72 20 2a 2f 0a 20 20 75 38 20 6f 6b 56 61 72 3b r */. u8 okVar;
ab630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
ab640 2a 20 54 72 75 65 20 69 66 20 61 7a 56 61 72 5b * True if azVar[
ab650 5d 20 68 61 73 20 62 65 65 6e 20 69 6e 69 74 69 ] has been initi
ab660 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 75 31 36 20 alized */. u16
ab670 6e 56 61 72 3b 20 20 20 20 20 20 20 20 20 20 20 nVar;
ab680 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
ab690 20 65 6e 74 72 69 65 73 20 69 6e 20 61 56 61 72 entries in aVar
ab6a0 5b 5d 20 2a 2f 0a 20 20 4d 65 6d 20 2a 61 56 61 [] */. Mem *aVa
ab6b0 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 r;
ab6c0 2f 2a 20 56 61 6c 75 65 73 20 66 6f 72 20 74 68 /* Values for th
ab6d0 65 20 4f 50 5f 56 61 72 69 61 62 6c 65 20 6f 70 e OP_Variable op
ab6e0 63 6f 64 65 2e 20 2a 2f 0a 20 20 63 68 61 72 20 code. */. char
ab6f0 2a 2a 61 7a 56 61 72 3b 20 20 20 20 20 20 20 20 **azVar;
ab700 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 76 61 /* Name of va
ab710 72 69 61 62 6c 65 73 20 2a 2f 0a 20 20 75 33 32 riables */. u32
ab720 20 6d 61 67 69 63 3b 20 20 20 20 20 20 20 20 20 magic;
ab730 20 20 20 20 20 2f 2a 20 4d 61 67 69 63 20 6e 75 /* Magic nu
ab740 6d 62 65 72 20 66 6f 72 20 73 61 6e 69 74 79 20 mber for sanity
ab750 63 68 65 63 6b 69 6e 67 20 2a 2f 0a 20 20 69 6e checking */. in
ab760 74 20 6e 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 t nMem;
ab770 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
ab780 6f 66 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 of memory locati
ab790 6f 6e 73 20 63 75 72 72 65 6e 74 6c 79 20 61 6c ons currently al
ab7a0 6c 6f 63 61 74 65 64 20 2a 2f 0a 20 20 4d 65 6d located */. Mem
ab7b0 20 2a 61 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 *aMem;
ab7c0 20 20 20 20 20 2f 2a 20 54 68 65 20 6d 65 6d 6f /* The memo
ab7d0 72 79 20 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a ry locations */.
ab7e0 20 20 75 33 32 20 63 61 63 68 65 43 74 72 3b 20 u32 cacheCtr;
ab7f0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 64 62 /* Vdb
ab800 65 43 75 72 73 6f 72 20 72 6f 77 20 63 61 63 68 eCursor row cach
ab810 65 20 67 65 6e 65 72 61 74 69 6f 6e 20 63 6f 75 e generation cou
ab820 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 70 63 nter */. int pc
ab830 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
ab840 20 20 2f 2a 20 54 68 65 20 70 72 6f 67 72 61 6d /* The program
ab850 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e counter */. in
ab860 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 t rc;
ab870 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 /* Value t
ab880 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20 63 68 o return */. ch
ab890 61 72 20 2a 7a 45 72 72 4d 73 67 3b 20 20 20 20 ar *zErrMsg;
ab8a0 20 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 6d /* Error m
ab8b0 65 73 73 61 67 65 20 77 72 69 74 74 65 6e 20 68 essage written h
ab8c0 65 72 65 20 2a 2f 0a 20 20 75 38 20 65 78 70 6c ere */. u8 expl
ab8d0 61 69 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 ain;
ab8e0 20 2f 2a 20 54 72 75 65 20 69 66 20 45 58 50 4c /* True if EXPL
ab8f0 41 49 4e 20 70 72 65 73 65 6e 74 20 6f 6e 20 53 AIN present on S
ab900 51 4c 20 63 6f 6d 6d 61 6e 64 20 2a 2f 0a 20 20 QL command */.
ab910 75 38 20 63 68 61 6e 67 65 43 6e 74 4f 6e 3b 20 u8 changeCntOn;
ab920 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
ab930 74 6f 20 75 70 64 61 74 65 20 74 68 65 20 63 68 to update the ch
ab940 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20 2a 2f 0a ange-counter */.
ab950 20 20 75 38 20 65 78 70 69 72 65 64 3b 20 20 20 u8 expired;
ab960 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 /* Tru
ab970 65 20 69 66 20 74 68 65 20 56 4d 20 6e 65 65 64 e if the VM need
ab980 73 20 74 6f 20 62 65 20 72 65 63 6f 6d 70 69 6c s to be recompil
ab990 65 64 20 2a 2f 0a 20 20 75 38 20 6d 69 6e 57 72 ed */. u8 minWr
ab9a0 69 74 65 46 69 6c 65 46 6f 72 6d 61 74 3b 20 20 iteFileFormat;
ab9b0 2f 2a 20 4d 69 6e 69 6d 75 6d 20 66 69 6c 65 20 /* Minimum file
ab9c0 66 6f 72 6d 61 74 20 66 6f 72 20 77 72 69 74 61 format for writa
ab9d0 62 6c 65 20 64 61 74 61 62 61 73 65 20 66 69 6c ble database fil
ab9e0 65 73 20 2a 2f 0a 20 20 75 38 20 69 6e 56 74 61 es */. u8 inVta
ab9f0 62 4d 65 74 68 6f 64 3b 20 20 20 20 20 20 20 20 bMethod;
aba00 2f 2a 20 53 65 65 20 63 6f 6d 6d 65 6e 74 73 20 /* See comments
aba10 61 62 6f 76 65 20 2a 2f 0a 20 20 75 38 20 75 73 above */. u8 us
aba20 65 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 3b 20 20 esStmtJournal;
aba30 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 75 73 /* True if us
aba40 65 73 20 61 20 73 74 61 74 65 6d 65 6e 74 20 6a es a statement j
aba50 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 75 38 20 72 ournal */. u8 r
aba60 65 61 64 4f 6e 6c 79 3b 20 20 20 20 20 20 20 20 eadOnly;
aba70 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 /* True for
aba80 72 65 61 64 2d 6f 6e 6c 79 20 73 74 61 74 65 6d read-only statem
aba90 65 6e 74 73 20 2a 2f 0a 20 20 75 38 20 69 73 50 ents */. u8 isP
abaa0 72 65 70 61 72 65 56 32 3b 20 20 20 20 20 20 20 repareV2;
abab0 20 20 2f 2a 20 54 72 75 65 20 69 66 20 70 72 65 /* True if pre
abac0 70 61 72 65 64 20 77 69 74 68 20 70 72 65 70 61 pared with prepa
abad0 72 65 5f 76 32 28 29 20 2a 2f 0a 20 20 69 6e 74 re_v2() */. int
abae0 20 6e 43 68 61 6e 67 65 3b 20 20 20 20 20 20 20 nChange;
abaf0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
abb00 66 20 64 62 20 63 68 61 6e 67 65 73 20 6d 61 64 f db changes mad
abb10 65 20 73 69 6e 63 65 20 6c 61 73 74 20 72 65 73 e since last res
abb20 65 74 20 2a 2f 0a 20 20 69 6e 74 20 62 74 72 65 et */. int btre
abb30 65 4d 61 73 6b 3b 20 20 20 20 20 20 20 20 20 20 eMask;
abb40 2f 2a 20 42 69 74 6d 61 73 6b 20 6f 66 20 64 62 /* Bitmask of db
abb50 2d 3e 61 44 62 5b 5d 20 65 6e 74 72 69 65 73 20 ->aDb[] entries
abb60 72 65 66 65 72 65 6e 63 65 64 20 2a 2f 0a 20 20 referenced */.
abb70 69 36 34 20 73 74 61 72 74 54 69 6d 65 3b 20 20 i64 startTime;
abb80 20 20 20 20 20 20 20 20 2f 2a 20 54 69 6d 65 20 /* Time
abb90 77 68 65 6e 20 71 75 65 72 79 20 73 74 61 72 74 when query start
abba0 65 64 20 2d 20 75 73 65 64 20 66 6f 72 20 70 72 ed - used for pr
abbb0 6f 66 69 6c 69 6e 67 20 2a 2f 0a 20 20 42 74 72 ofiling */. Btr
abbc0 65 65 4d 75 74 65 78 41 72 72 61 79 20 61 4d 75 eeMutexArray aMu
abbd0 74 65 78 3b 20 2f 2a 20 41 6e 20 61 72 72 61 79 tex; /* An array
abbe0 20 6f 66 20 42 74 72 65 65 20 75 73 65 64 20 68 of Btree used h
abbf0 65 72 65 20 61 6e 64 20 6e 65 65 64 69 6e 67 20 ere and needing
abc00 6c 6f 63 6b 73 20 2a 2f 0a 20 20 69 6e 74 20 61 locks */. int a
abc10 43 6f 75 6e 74 65 72 5b 32 5d 3b 20 20 20 20 20 Counter[2];
abc20 20 20 20 2f 2a 20 43 6f 75 6e 74 65 72 73 20 75 /* Counters u
abc30 73 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 73 sed by sqlite3_s
abc40 74 6d 74 5f 73 74 61 74 75 73 28 29 20 2a 2f 0a tmt_status() */.
abc50 20 20 63 68 61 72 20 2a 7a 53 71 6c 3b 20 20 20 char *zSql;
abc60 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 78 /* Tex
abc70 74 20 6f 66 20 74 68 65 20 53 51 4c 20 73 74 61 t of the SQL sta
abc80 74 65 6d 65 6e 74 20 74 68 61 74 20 67 65 6e 65 tement that gene
abc90 72 61 74 65 64 20 74 68 69 73 20 2a 2f 0a 20 20 rated this */.
abca0 76 6f 69 64 20 2a 70 46 72 65 65 3b 20 20 20 20 void *pFree;
abcb0 20 20 20 20 20 20 20 20 2f 2a 20 46 72 65 65 20 /* Free
abcc0 74 68 69 73 20 77 68 65 6e 20 64 65 6c 65 74 69 this when deleti
abcd0 6e 67 20 74 68 65 20 76 64 62 65 20 2a 2f 0a 20 ng the vdbe */.
abce0 20 69 36 34 20 6e 46 6b 43 6f 6e 73 74 72 61 69 i64 nFkConstrai
abcf0 6e 74 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 nt; /* Numb
abd00 65 72 20 6f 66 20 69 6d 6d 2e 20 46 4b 20 63 6f er of imm. FK co
abd10 6e 73 74 72 61 69 6e 74 73 20 74 68 69 73 20 56 nstraints this V
abd20 4d 20 2a 2f 0a 20 20 69 36 34 20 6e 53 74 6d 74 M */. i64 nStmt
abd30 44 65 66 43 6f 6e 73 3b 20 20 20 20 20 20 20 2f DefCons; /
abd40 2a 20 4e 75 6d 62 65 72 20 6f 66 20 64 65 66 2e * Number of def.
abd50 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 77 68 65 constraints whe
abd60 6e 20 73 74 6d 74 20 73 74 61 72 74 65 64 20 2a n stmt started *
abd70 2f 0a 20 20 69 6e 74 20 69 53 74 61 74 65 6d 65 /. int iStateme
abd80 6e 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53 nt; /* S
abd90 74 61 74 65 6d 65 6e 74 20 6e 75 6d 62 65 72 20 tatement number
abda0 28 6f 72 20 30 20 69 66 20 68 61 73 20 6e 6f 74 (or 0 if has not
abdb0 20 6f 70 65 6e 65 64 20 73 74 6d 74 29 20 2a 2f opened stmt) */
abdc0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 .#ifdef SQLITE_D
abdd0 45 42 55 47 0a 20 20 46 49 4c 45 20 2a 74 72 61 EBUG. FILE *tra
abde0 63 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f ce; /
abdf0 2a 20 57 72 69 74 65 20 61 6e 20 65 78 65 63 75 * Write an execu
abe00 74 69 6f 6e 20 74 72 61 63 65 20 68 65 72 65 2c tion trace here,
abe10 20 69 66 20 6e 6f 74 20 4e 55 4c 4c 20 2a 2f 0a if not NULL */.
abe20 23 65 6e 64 69 66 0a 20 20 56 64 62 65 46 72 61 #endif. VdbeFra
abe30 6d 65 20 2a 70 46 72 61 6d 65 3b 20 20 20 20 20 me *pFrame;
abe40 20 2f 2a 20 50 61 72 65 6e 74 20 66 72 61 6d 65 /* Parent frame
abe50 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 72 61 6d 65 */. int nFrame
abe60 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
abe70 20 4e 75 6d 62 65 72 20 6f 66 20 66 72 61 6d 65 Number of frame
abe80 73 20 69 6e 20 70 46 72 61 6d 65 20 6c 69 73 74 s in pFrame list
abe90 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 */.};../*.** Th
abea0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 e following are
abeb0 61 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73 20 66 allowed values f
abec0 6f 72 20 56 64 62 65 2e 6d 61 67 69 63 0a 2a 2f or Vdbe.magic.*/
abed0 0a 23 64 65 66 69 6e 65 20 56 44 42 45 5f 4d 41 .#define VDBE_MA
abee0 47 49 43 5f 49 4e 49 54 20 20 20 20 20 30 78 32 GIC_INIT 0x2
abef0 36 62 63 65 61 61 35 20 20 20 20 2f 2a 20 42 75 6bceaa5 /* Bu
abf00 69 6c 64 69 6e 67 20 61 20 56 44 42 45 20 70 72 ilding a VDBE pr
abf10 6f 67 72 61 6d 20 2a 2f 0a 23 64 65 66 69 6e 65 ogram */.#define
abf20 20 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 VDBE_MAGIC_RUN
abf30 20 20 20 20 20 30 78 62 64 66 32 30 64 61 33 20 0xbdf20da3
abf40 20 20 20 2f 2a 20 56 44 42 45 20 69 73 20 72 65 /* VDBE is re
abf50 61 64 79 20 74 6f 20 65 78 65 63 75 74 65 20 2a ady to execute *
abf60 2f 0a 23 64 65 66 69 6e 65 20 56 44 42 45 5f 4d /.#define VDBE_M
abf70 41 47 49 43 5f 48 41 4c 54 20 20 20 20 20 30 78 AGIC_HALT 0x
abf80 35 31 39 63 32 39 37 33 20 20 20 20 2f 2a 20 56 519c2973 /* V
abf90 44 42 45 20 68 61 73 20 63 6f 6d 70 6c 65 74 65 DBE has complete
abfa0 64 20 65 78 65 63 75 74 69 6f 6e 20 2a 2f 0a 23 d execution */.#
abfb0 64 65 66 69 6e 65 20 56 44 42 45 5f 4d 41 47 49 define VDBE_MAGI
abfc0 43 5f 44 45 41 44 20 20 20 20 20 30 78 62 36 30 C_DEAD 0xb60
abfd0 36 63 33 63 38 20 20 20 20 2f 2a 20 54 68 65 20 6c3c8 /* The
abfe0 56 44 42 45 20 68 61 73 20 62 65 65 6e 20 64 65 VDBE has been de
abff0 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a 0a 2f 2a allocated */../*
ac000 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 70 72 6f .** Function pro
ac010 74 6f 74 79 70 65 73 0a 2a 2f 0a 53 51 4c 49 54 totypes.*/.SQLIT
ac020 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
ac030 71 6c 69 74 65 33 56 64 62 65 46 72 65 65 43 75 qlite3VdbeFreeCu
ac040 72 73 6f 72 28 56 64 62 65 20 2a 2c 20 56 64 62 rsor(Vdbe *, Vdb
ac050 65 43 75 72 73 6f 72 2a 29 3b 0a 76 6f 69 64 20 eCursor*);.void
ac060 73 71 6c 69 74 65 56 64 62 65 50 6f 70 53 74 61 sqliteVdbePopSta
ac070 63 6b 28 56 64 62 65 2a 2c 69 6e 74 29 3b 0a 53 ck(Vdbe*,int);.S
ac080 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
ac090 74 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 t sqlite3VdbeCur
ac0a0 73 6f 72 4d 6f 76 65 74 6f 28 56 64 62 65 43 75 sorMoveto(VdbeCu
ac0b0 72 73 6f 72 2a 29 3b 0a 23 69 66 20 64 65 66 69 rsor*);.#if defi
ac0c0 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 ned(SQLITE_DEBUG
ac0d0 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 56 44 42 ) || defined(VDB
ac0e0 45 5f 50 52 4f 46 49 4c 45 29 0a 53 51 4c 49 54 E_PROFILE).SQLIT
ac0f0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
ac100 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e 74 4f qlite3VdbePrintO
ac110 70 28 46 49 4c 45 2a 2c 20 69 6e 74 2c 20 4f 70 p(FILE*, int, Op
ac120 2a 29 3b 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 *);.#endif.SQLIT
ac130 45 5f 50 52 49 56 41 54 45 20 75 33 32 20 73 71 E_PRIVATE u32 sq
ac140 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 lite3VdbeSerialT
ac150 79 70 65 4c 65 6e 28 75 33 32 29 3b 0a 53 51 4c ypeLen(u32);.SQL
ac160 49 54 45 5f 50 52 49 56 41 54 45 20 75 33 32 20 ITE_PRIVATE u32
ac170 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 sqlite3VdbeSeria
ac180 6c 54 79 70 65 28 4d 65 6d 2a 2c 20 69 6e 74 29 lType(Mem*, int)
ac190 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
ac1a0 20 75 33 32 20 73 71 6c 69 74 65 33 56 64 62 65 u32 sqlite3Vdbe
ac1b0 53 65 72 69 61 6c 50 75 74 28 75 6e 73 69 67 6e SerialPut(unsign
ac1c0 65 64 20 63 68 61 72 2a 2c 20 69 6e 74 2c 20 4d ed char*, int, M
ac1d0 65 6d 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 em*, int);.SQLIT
ac1e0 45 5f 50 52 49 56 41 54 45 20 75 33 32 20 73 71 E_PRIVATE u32 sq
ac1f0 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 47 lite3VdbeSerialG
ac200 65 74 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 et(const unsigne
ac210 64 20 63 68 61 72 2a 2c 20 75 33 32 2c 20 4d 65 d char*, u32, Me
ac220 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 m*);.SQLITE_PRIV
ac230 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
ac240 56 64 62 65 44 65 6c 65 74 65 41 75 78 44 61 74 VdbeDeleteAuxDat
ac250 61 28 56 64 62 65 46 75 6e 63 2a 2c 20 69 6e 74 a(VdbeFunc*, int
ac260 29 3b 0a 0a 69 6e 74 20 73 71 6c 69 74 65 32 42 );..int sqlite2B
ac270 74 72 65 65 4b 65 79 43 6f 6d 70 61 72 65 28 42 treeKeyCompare(B
ac280 74 43 75 72 73 6f 72 20 2a 2c 20 63 6f 6e 73 74 tCursor *, const
ac290 20 76 6f 69 64 20 2a 2c 20 69 6e 74 2c 20 69 6e void *, int, in
ac2a0 74 2c 20 69 6e 74 20 2a 29 3b 0a 53 51 4c 49 54 t, int *);.SQLIT
ac2b0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
ac2c0 6c 69 74 65 33 56 64 62 65 49 64 78 4b 65 79 43 lite3VdbeIdxKeyC
ac2d0 6f 6d 70 61 72 65 28 56 64 62 65 43 75 72 73 6f ompare(VdbeCurso
ac2e0 72 2a 2c 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 r*,UnpackedRecor
ac2f0 64 2a 2c 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 d*,int*);.SQLITE
ac300 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
ac310 69 74 65 33 56 64 62 65 49 64 78 52 6f 77 69 64 ite3VdbeIdxRowid
ac320 28 73 71 6c 69 74 65 33 2a 2c 20 42 74 43 75 72 (sqlite3*, BtCur
ac330 73 6f 72 20 2a 2c 20 69 36 34 20 2a 29 3b 0a 53 sor *, i64 *);.S
ac340 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
ac350 74 20 73 71 6c 69 74 65 33 4d 65 6d 43 6f 6d 70 t sqlite3MemComp
ac360 61 72 65 28 63 6f 6e 73 74 20 4d 65 6d 2a 2c 20 are(const Mem*,
ac370 63 6f 6e 73 74 20 4d 65 6d 2a 2c 20 63 6f 6e 73 const Mem*, cons
ac380 74 20 43 6f 6c 6c 53 65 71 2a 29 3b 0a 53 51 4c t CollSeq*);.SQL
ac390 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
ac3a0 73 71 6c 69 74 65 33 56 64 62 65 45 78 65 63 28 sqlite3VdbeExec(
ac3b0 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 Vdbe*);.SQLITE_P
ac3c0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
ac3d0 65 33 56 64 62 65 4c 69 73 74 28 56 64 62 65 2a e3VdbeList(Vdbe*
ac3e0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
ac3f0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
ac400 65 48 61 6c 74 28 56 64 62 65 2a 29 3b 0a 53 51 eHalt(Vdbe*);.SQ
ac410 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
ac420 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e sqlite3VdbeChan
ac430 67 65 45 6e 63 6f 64 69 6e 67 28 4d 65 6d 20 2a geEncoding(Mem *
ac440 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
ac450 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
ac460 65 33 56 64 62 65 4d 65 6d 54 6f 6f 42 69 67 28 e3VdbeMemTooBig(
ac470 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 Mem*);.SQLITE_PR
ac480 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
ac490 33 56 64 62 65 4d 65 6d 43 6f 70 79 28 4d 65 6d 3VdbeMemCopy(Mem
ac4a0 2a 2c 20 63 6f 6e 73 74 20 4d 65 6d 2a 29 3b 0a *, const Mem*);.
ac4b0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
ac4c0 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d oid sqlite3VdbeM
ac4d0 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28 4d 65 emShallowCopy(Me
ac4e0 6d 2a 2c 20 63 6f 6e 73 74 20 4d 65 6d 2a 2c 20 m*, const Mem*,
ac4f0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
ac500 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
ac510 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28 4d 65 6d 3VdbeMemMove(Mem
ac520 2a 2c 20 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 *, Mem*);.SQLITE
ac530 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
ac540 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 6c 54 65 ite3VdbeMemNulTe
ac550 72 6d 69 6e 61 74 65 28 4d 65 6d 2a 29 3b 0a 53 rminate(Mem*);.S
ac560 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
ac570 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d t sqlite3VdbeMem
ac580 53 65 74 53 74 72 28 4d 65 6d 2a 2c 20 63 6f 6e SetStr(Mem*, con
ac590 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 2c 20 75 st char*, int, u
ac5a0 38 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 8, void(*)(void*
ac5b0 29 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ));.SQLITE_PRIVA
ac5c0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
ac5d0 64 62 65 4d 65 6d 53 65 74 49 6e 74 36 34 28 4d dbeMemSetInt64(M
ac5e0 65 6d 2a 2c 20 69 36 34 29 3b 0a 53 51 4c 49 54 em*, i64);.SQLIT
ac5f0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
ac600 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 qlite3VdbeMemSet
ac610 44 6f 75 62 6c 65 28 4d 65 6d 2a 2c 20 64 6f 75 Double(Mem*, dou
ac620 62 6c 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ble);.SQLITE_PRI
ac630 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
ac640 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 3VdbeMemSetNull(
ac650 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 Mem*);.SQLITE_PR
ac660 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
ac670 65 33 56 64 62 65 4d 65 6d 53 65 74 5a 65 72 6f e3VdbeMemSetZero
ac680 42 6c 6f 62 28 4d 65 6d 2a 2c 69 6e 74 29 3b 0a Blob(Mem*,int);.
ac690 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
ac6a0 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d oid sqlite3VdbeM
ac6b0 65 6d 53 65 74 52 6f 77 53 65 74 28 4d 65 6d 2a emSetRowSet(Mem*
ac6c0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
ac6d0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
ac6e0 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61 62 6c eMemMakeWriteabl
ac6f0 65 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f e(Mem*);.SQLITE_
ac700 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
ac710 74 65 33 56 64 62 65 4d 65 6d 53 74 72 69 6e 67 te3VdbeMemString
ac720 69 66 79 28 4d 65 6d 2a 2c 20 69 6e 74 29 3b 0a ify(Mem*, int);.
ac730 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
ac740 36 34 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 64 sqlite3VdbeIn
ac750 74 56 61 6c 75 65 28 4d 65 6d 2a 29 3b 0a 53 51 tValue(Mem*);.SQ
ac760 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
ac770 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 sqlite3VdbeMemI
ac780 6e 74 65 67 65 72 69 66 79 28 4d 65 6d 2a 29 3b ntegerify(Mem*);
ac790 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
ac7a0 64 6f 75 62 6c 65 20 73 71 6c 69 74 65 33 56 64 double sqlite3Vd
ac7b0 62 65 52 65 61 6c 56 61 6c 75 65 28 4d 65 6d 2a beRealValue(Mem*
ac7c0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
ac7d0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 E void sqlite3Vd
ac7e0 62 65 49 6e 74 65 67 65 72 41 66 66 69 6e 69 74 beIntegerAffinit
ac7f0 79 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f y(Mem*);.SQLITE_
ac800 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
ac810 74 65 33 56 64 62 65 4d 65 6d 52 65 61 6c 69 66 te3VdbeMemRealif
ac820 79 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f y(Mem*);.SQLITE_
ac830 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
ac840 74 65 33 56 64 62 65 4d 65 6d 4e 75 6d 65 72 69 te3VdbeMemNumeri
ac850 66 79 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 fy(Mem*);.SQLITE
ac860 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
ac870 69 74 65 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42 ite3VdbeMemFromB
ac880 74 72 65 65 28 42 74 43 75 72 73 6f 72 2a 2c 69 tree(BtCursor*,i
ac890 6e 74 2c 69 6e 74 2c 69 6e 74 2c 4d 65 6d 2a 29 nt,int,int,Mem*)
ac8a0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
ac8b0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
ac8c0 65 4d 65 6d 52 65 6c 65 61 73 65 28 4d 65 6d 20 eMemRelease(Mem
ac8d0 2a 70 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 *p);.SQLITE_PRIV
ac8e0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
ac8f0 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 45 78 VdbeMemReleaseEx
ac900 74 65 72 6e 61 6c 28 4d 65 6d 20 2a 70 29 3b 0a ternal(Mem *p);.
ac910 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
ac920 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 nt sqlite3VdbeMe
ac930 6d 46 69 6e 61 6c 69 7a 65 28 4d 65 6d 2a 2c 20 mFinalize(Mem*,
ac940 46 75 6e 63 44 65 66 2a 29 3b 0a 53 51 4c 49 54 FuncDef*);.SQLIT
ac950 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 E_PRIVATE const
ac960 63 68 61 72 20 2a 73 71 6c 69 74 65 33 4f 70 63 char *sqlite3Opc
ac970 6f 64 65 4e 61 6d 65 28 69 6e 74 29 3b 0a 53 51 odeName(int);.SQ
ac980 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
ac990 20 73 71 6c 69 74 65 33 56 64 62 65 4f 70 63 6f sqlite3VdbeOpco
ac9a0 64 65 48 61 73 50 72 6f 70 65 72 74 79 28 69 6e deHasProperty(in
ac9b0 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f t, int);.SQLITE_
ac9c0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
ac9d0 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 4d te3VdbeMemGrow(M
ac9e0 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74 20 6e 2c em *pMem, int n,
ac9f0 20 69 6e 74 20 70 72 65 73 65 72 76 65 29 3b 0a int preserve);.
aca00 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
aca10 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 43 6c nt sqlite3VdbeCl
aca20 6f 73 65 53 74 61 74 65 6d 65 6e 74 28 56 64 62 oseStatement(Vdb
aca30 65 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 e *, int);.SQLIT
aca40 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
aca50 71 6c 69 74 65 33 56 64 62 65 46 72 61 6d 65 44 qlite3VdbeFrameD
aca60 65 6c 65 74 65 28 56 64 62 65 46 72 61 6d 65 2a elete(VdbeFrame*
aca70 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
aca80 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
aca90 65 46 72 61 6d 65 52 65 73 74 6f 72 65 28 56 64 eFrameRestore(Vd
acaa0 62 65 46 72 61 6d 65 20 2a 29 3b 0a 23 69 66 64 beFrame *);.#ifd
acab0 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 ef SQLITE_ENABLE
acac0 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 _MEMORY_MANAGEME
acad0 4e 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 NT.SQLITE_PRIVAT
acae0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
acaf0 65 52 65 6c 65 61 73 65 42 75 66 66 65 72 73 28 eReleaseBuffers(
acb00 56 64 62 65 20 2a 70 29 3b 0a 23 65 6e 64 69 66 Vdbe *p);.#endif
acb10 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
acb20 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 _OMIT_FOREIGN_KE
acb30 59 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 Y.SQLITE_PRIVATE
acb40 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 int sqlite3Vdbe
acb50 43 68 65 63 6b 46 6b 28 56 64 62 65 20 2a 2c 20 CheckFk(Vdbe *,
acb60 69 6e 74 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 int);.#else.# de
acb70 66 69 6e 65 20 73 71 6c 69 74 65 33 56 64 62 65 fine sqlite3Vdbe
acb80 43 68 65 63 6b 46 6b 28 70 2c 69 29 20 30 0a 23 CheckFk(p,i) 0.#
acb90 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 endif..#ifndef S
acba0 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 QLITE_OMIT_SHARE
acbb0 44 5f 43 41 43 48 45 0a 53 51 4c 49 54 45 5f 50 D_CACHE.SQLITE_P
acbc0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
acbd0 74 65 33 56 64 62 65 4d 75 74 65 78 41 72 72 61 te3VdbeMutexArra
acbe0 79 45 6e 74 65 72 28 56 64 62 65 20 2a 70 29 3b yEnter(Vdbe *p);
acbf0 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 .#else.# define
acc00 73 71 6c 69 74 65 33 56 64 62 65 4d 75 74 65 78 sqlite3VdbeMutex
acc10 41 72 72 61 79 45 6e 74 65 72 28 70 29 0a 23 65 ArrayEnter(p).#e
acc20 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49 ndif..SQLITE_PRI
acc30 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
acc40 56 64 62 65 4d 65 6d 54 72 61 6e 73 6c 61 74 65 VdbeMemTranslate
acc50 28 4d 65 6d 2a 2c 20 75 38 29 3b 0a 23 69 66 64 (Mem*, u8);.#ifd
acc60 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a ef SQLITE_DEBUG.
acc70 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
acc80 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
acc90 65 50 72 69 6e 74 53 71 6c 28 56 64 62 65 2a 29 ePrintSql(Vdbe*)
acca0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
accb0 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 void sqlite3V
accc0 64 62 65 4d 65 6d 50 72 65 74 74 79 50 72 69 6e dbeMemPrettyPrin
accd0 74 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 63 68 61 t(Mem *pMem, cha
acce0 72 20 2a 7a 42 75 66 29 3b 0a 23 65 6e 64 69 66 r *zBuf);.#endif
accf0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
acd00 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d int sqlite3VdbeM
acd10 65 6d 48 61 6e 64 6c 65 42 6f 6d 28 4d 65 6d 20 emHandleBom(Mem
acd20 2a 70 4d 65 6d 29 3b 0a 0a 23 69 66 6e 64 65 66 *pMem);..#ifndef
acd30 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 SQLITE_OMIT_INC
acd40 52 42 4c 4f 42 0a 53 51 4c 49 54 45 5f 50 52 49 RBLOB.SQLITE_PRI
acd50 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 VATE int sqlit
acd60 65 33 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 e3VdbeMemExpandB
acd70 6c 6f 62 28 4d 65 6d 20 2a 29 3b 0a 23 65 6c 73 lob(Mem *);.#els
acd80 65 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 e. #define sqli
acd90 74 65 33 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 te3VdbeMemExpand
acda0 42 6c 6f 62 28 78 29 20 53 51 4c 49 54 45 5f 4f Blob(x) SQLITE_O
acdb0 4b 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 K.#endif..#endif
acdc0 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 5f 56 44 /* !defined(_VD
acdd0 42 45 49 4e 54 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a BEINT_H_) */../*
acde0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e ************* En
acdf0 64 20 6f 66 20 76 64 62 65 49 6e 74 2e 68 20 2a d of vdbeInt.h *
ace00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ace10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ace20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
ace30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f ************* Co
ace40 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 ntinuing where w
ace50 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 75 74 e left off in ut
ace60 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a f.c ************
ace70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 23 ************/..#
ace80 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 41 4d ifndef SQLITE_AM
ace90 41 4c 47 41 4d 41 54 49 4f 4e 0a 2f 2a 0a 2a 2a ALGAMATION./*.**
acea0 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 The following c
aceb0 6f 6e 73 74 61 6e 74 20 76 61 6c 75 65 20 69 73 onstant value is
acec0 20 75 73 65 64 20 62 79 20 74 68 65 20 53 51 4c used by the SQL
aced0 49 54 45 5f 42 49 47 45 4e 44 49 41 4e 20 61 6e ITE_BIGENDIAN an
acee0 64 0a 2a 2a 20 53 51 4c 49 54 45 5f 4c 49 54 54 d.** SQLITE_LITT
acef0 4c 45 45 4e 44 49 41 4e 20 6d 61 63 72 6f 73 2e LEENDIAN macros.
acf00 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
acf10 54 45 20 63 6f 6e 73 74 20 69 6e 74 20 73 71 6c TE const int sql
acf20 69 74 65 33 6f 6e 65 20 3d 20 31 3b 0a 23 65 6e ite3one = 1;.#en
acf30 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 41 4d dif /* SQLITE_AM
acf40 41 4c 47 41 4d 41 54 49 4f 4e 20 2a 2f 0a 0a 2f ALGAMATION */../
acf50 2a 0a 2a 2a 20 54 68 69 73 20 6c 6f 6f 6b 75 70 *.** This lookup
acf60 20 74 61 62 6c 65 20 69 73 20 75 73 65 64 20 74 table is used t
acf70 6f 20 68 65 6c 70 20 64 65 63 6f 64 65 20 74 68 o help decode th
acf80 65 20 66 69 72 73 74 20 62 79 74 65 20 6f 66 0a e first byte of.
acf90 2a 2a 20 61 20 6d 75 6c 74 69 2d 62 79 74 65 20 ** a multi-byte
acfa0 55 54 46 38 20 63 68 61 72 61 63 74 65 72 2e 0a UTF8 character..
acfb0 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 */.static const
acfc0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 73 71 unsigned char sq
acfd0 6c 69 74 65 33 55 74 66 38 54 72 61 6e 73 31 5b lite3Utf8Trans1[
acfe0 5d 20 3d 20 7b 0a 20 20 30 78 30 30 2c 20 30 78 ] = {. 0x00, 0x
acff0 30 31 2c 20 30 78 30 32 2c 20 30 78 30 33 2c 20 01, 0x02, 0x03,
ad000 30 78 30 34 2c 20 30 78 30 35 2c 20 30 78 30 36 0x04, 0x05, 0x06
ad010 2c 20 30 78 30 37 2c 0a 20 20 30 78 30 38 2c 20 , 0x07,. 0x08,
ad020 30 78 30 39 2c 20 30 78 30 61 2c 20 30 78 30 62 0x09, 0x0a, 0x0b
ad030 2c 20 30 78 30 63 2c 20 30 78 30 64 2c 20 30 78 , 0x0c, 0x0d, 0x
ad040 30 65 2c 20 30 78 30 66 2c 0a 20 20 30 78 31 30 0e, 0x0f,. 0x10
ad050 2c 20 30 78 31 31 2c 20 30 78 31 32 2c 20 30 78 , 0x11, 0x12, 0x
ad060 31 33 2c 20 30 78 31 34 2c 20 30 78 31 35 2c 20 13, 0x14, 0x15,
ad070 30 78 31 36 2c 20 30 78 31 37 2c 0a 20 20 30 78 0x16, 0x17,. 0x
ad080 31 38 2c 20 30 78 31 39 2c 20 30 78 31 61 2c 20 18, 0x19, 0x1a,
ad090 30 78 31 62 2c 20 30 78 31 63 2c 20 30 78 31 64 0x1b, 0x1c, 0x1d
ad0a0 2c 20 30 78 31 65 2c 20 30 78 31 66 2c 0a 20 20 , 0x1e, 0x1f,.
ad0b0 30 78 30 30 2c 20 30 78 30 31 2c 20 30 78 30 32 0x00, 0x01, 0x02
ad0c0 2c 20 30 78 30 33 2c 20 30 78 30 34 2c 20 30 78 , 0x03, 0x04, 0x
ad0d0 30 35 2c 20 30 78 30 36 2c 20 30 78 30 37 2c 0a 05, 0x06, 0x07,.
ad0e0 20 20 30 78 30 38 2c 20 30 78 30 39 2c 20 30 78 0x08, 0x09, 0x
ad0f0 30 61 2c 20 30 78 30 62 2c 20 30 78 30 63 2c 20 0a, 0x0b, 0x0c,
ad100 30 78 30 64 2c 20 30 78 30 65 2c 20 30 78 30 66 0x0d, 0x0e, 0x0f
ad110 2c 0a 20 20 30 78 30 30 2c 20 30 78 30 31 2c 20 ,. 0x00, 0x01,
ad120 30 78 30 32 2c 20 30 78 30 33 2c 20 30 78 30 34 0x02, 0x03, 0x04
ad130 2c 20 30 78 30 35 2c 20 30 78 30 36 2c 20 30 78 , 0x05, 0x06, 0x
ad140 30 37 2c 0a 20 20 30 78 30 30 2c 20 30 78 30 31 07,. 0x00, 0x01
ad150 2c 20 30 78 30 32 2c 20 30 78 30 33 2c 20 30 78 , 0x02, 0x03, 0x
ad160 30 30 2c 20 30 78 30 31 2c 20 30 78 30 30 2c 20 00, 0x01, 0x00,
ad170 30 78 30 30 2c 0a 7d 3b 0a 0a 0a 23 64 65 66 69 0x00,.};...#defi
ad180 6e 65 20 57 52 49 54 45 5f 55 54 46 38 28 7a 4f ne WRITE_UTF8(zO
ad190 75 74 2c 20 63 29 20 7b 20 20 20 20 20 20 20 20 ut, c) {
ad1a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad1b0 20 20 5c 0a 20 20 69 66 28 20 63 3c 30 78 30 30 \. if( c<0x00
ad1c0 30 38 30 20 29 7b 20 20 20 20 20 20 20 20 20 20 080 ){
ad1d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad1e0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
ad1f0 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 *zOut++ = (u8)(
ad200 63 26 30 78 46 46 29 3b 20 20 20 20 20 20 20 20 c&0xFF);
ad210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad220 20 20 20 20 5c 0a 20 20 7d 20 20 20 20 20 20 20 \. }
ad230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad250 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
ad260 20 65 6c 73 65 20 69 66 28 20 63 3c 30 78 30 30 else if( c<0x00
ad270 38 30 30 20 29 7b 20 20 20 20 20 20 20 20 20 20 800 ){
ad280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad290 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 \. *zOu
ad2a0 74 2b 2b 20 3d 20 30 78 43 30 20 2b 20 28 75 38 t++ = 0xC0 + (u8
ad2b0 29 28 28 63 3e 3e 36 29 26 30 78 31 46 29 3b 20 )((c>>6)&0x1F);
ad2c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
ad2d0 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 . *zOut++ = 0
ad2e0 78 38 30 20 2b 20 28 75 38 29 28 63 20 26 20 30 x80 + (u8)(c & 0
ad2f0 78 33 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 x3F);
ad300 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 20 20 20 \. }
ad310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad340 20 5c 0a 20 20 65 6c 73 65 20 69 66 28 20 63 3c \. else if( c<
ad350 30 78 31 30 30 30 30 20 29 7b 20 20 20 20 20 20 0x10000 ){
ad360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad370 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 \.
ad380 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 45 30 20 2b *zOut++ = 0xE0 +
ad390 20 28 75 38 29 28 28 63 3e 3e 31 32 29 26 30 78 (u8)((c>>12)&0x
ad3a0 30 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 0F);
ad3b0 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b \. *zOut++
ad3c0 20 3d 20 30 78 38 30 20 2b 20 28 75 38 29 28 28 = 0x80 + (u8)((
ad3d0 63 3e 3e 36 29 20 26 20 30 78 33 46 29 3b 20 20 c>>6) & 0x3F);
ad3e0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
ad3f0 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 38 30 *zOut++ = 0x80
ad400 20 2b 20 28 75 38 29 28 63 20 26 20 30 78 33 46 + (u8)(c & 0x3F
ad410 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 );
ad420 20 20 20 20 20 5c 0a 20 20 7d 65 6c 73 65 7b 20 \. }else{
ad430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
ad460 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 *zOut++ = 0x
ad470 46 30 20 2b 20 28 75 38 29 28 28 63 3e 3e 31 38 F0 + (u8)((c>>18
ad480 29 20 26 20 30 78 30 37 29 3b 20 20 20 20 20 20 ) & 0x07);
ad490 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f \. *zO
ad4a0 75 74 2b 2b 20 3d 20 30 78 38 30 20 2b 20 28 75 ut++ = 0x80 + (u
ad4b0 38 29 28 28 63 3e 3e 31 32 29 20 26 20 30 78 33 8)((c>>12) & 0x3
ad4c0 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 F);
ad4d0 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 \. *zOut++ =
ad4e0 30 78 38 30 20 2b 20 28 75 38 29 28 28 63 3e 3e 0x80 + (u8)((c>>
ad4f0 36 29 20 26 20 30 78 33 46 29 3b 20 20 20 20 20 6) & 0x3F);
ad500 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a \. *
ad510 7a 4f 75 74 2b 2b 20 3d 20 30 78 38 30 20 2b 20 zOut++ = 0x80 +
ad520 28 75 38 29 28 63 20 26 20 30 78 33 46 29 3b 20 (u8)(c & 0x3F);
ad530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad540 20 20 5c 0a 20 20 7d 20 20 20 20 20 20 20 20 20 \. }
ad550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad570 20 20 20 20 20 20 20 20 20 20 20 5c 0a 7d 0a 0a \.}..
ad580 23 64 65 66 69 6e 65 20 57 52 49 54 45 5f 55 54 #define WRITE_UT
ad590 46 31 36 4c 45 28 7a 4f 75 74 2c 20 63 29 20 7b F16LE(zOut, c) {
ad5a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad5b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad5c0 20 20 20 20 5c 0a 20 20 69 66 28 20 63 3c 3d 30 \. if( c<=0
ad5d0 78 46 46 46 46 20 29 7b 20 20 20 20 20 20 20 20 xFFFF ){
ad5e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad5f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad600 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 \.
ad610 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 63 *zOut++ = (u8)(c
ad620 26 30 78 30 30 46 46 29 3b 20 20 20 20 20 20 20 &0x00FF);
ad630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad650 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 \. *zOut++ =
ad660 28 75 38 29 28 28 63 3e 3e 38 29 26 30 78 30 30 (u8)((c>>8)&0x00
ad670 46 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 FF);
ad680 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad690 20 20 20 20 20 20 5c 0a 20 20 7d 65 6c 73 65 7b \. }else{
ad6a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad6b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad6c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad6d0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
ad6e0 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 *zOut++ = (u8)
ad6f0 28 28 28 63 3e 3e 31 30 29 26 30 78 30 30 33 46 (((c>>10)&0x003F
ad700 29 20 2b 20 28 28 28 63 2d 30 78 31 30 30 30 30 ) + (((c-0x10000
ad710 29 3e 3e 31 30 29 26 30 78 30 30 43 30 29 29 3b )>>10)&0x00C0));
ad720 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 \. *zOut++
ad730 3d 20 28 75 38 29 28 30 78 30 30 44 38 20 2b 20 = (u8)(0x00D8 +
ad740 28 28 28 63 2d 30 78 31 30 30 30 30 29 3e 3e 31 (((c-0x10000)>>1
ad750 38 29 26 30 78 30 33 29 29 3b 20 20 20 20 20 20 8)&0x03));
ad760 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a \. *z
ad770 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 63 26 30 Out++ = (u8)(c&0
ad780 78 30 30 46 46 29 3b 20 20 20 20 20 20 20 20 20 x00FF);
ad790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad7a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
ad7b0 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 *zOut++ = (u
ad7c0 38 29 28 30 78 30 30 44 43 20 2b 20 28 28 63 3e 8)(0x00DC + ((c>
ad7d0 3e 38 29 26 30 78 30 33 29 29 3b 20 20 20 20 20 >8)&0x03));
ad7e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad7f0 20 20 20 20 5c 0a 20 20 7d 20 20 20 20 20 20 20 \. }
ad800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad830 20 20 20 20 20 20 20 20 20 20 5c 0a 7d 0a 0a 23 \.}..#
ad840 64 65 66 69 6e 65 20 57 52 49 54 45 5f 55 54 46 define WRITE_UTF
ad850 31 36 42 45 28 7a 4f 75 74 2c 20 63 29 20 7b 20 16BE(zOut, c) {
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 20 20 20 20 20 20
ad880 20 20 20 5c 0a 20 20 69 66 28 20 63 3c 3d 30 78 \. if( c<=0x
ad890 46 46 46 46 20 29 7b 20 20 20 20 20 20 20 20 20 FFFF ){
ad8a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad8b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad8c0 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a \. *
ad8d0 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 28 63 zOut++ = (u8)((c
ad8e0 3e 3e 38 29 26 30 78 30 30 46 46 29 3b 20 20 20 >>8)&0x00FF);
ad8f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
ad910 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 . *zOut++ = (
ad920 75 38 29 28 63 26 30 78 30 30 46 46 29 3b 20 20 u8)(c&0x00FF);
ad930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad950 20 20 20 20 20 5c 0a 20 20 7d 65 6c 73 65 7b 20 \. }else{
ad960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
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 20 20 20 20 5c 0a 20 20 20 \.
ad9a0 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 *zOut++ = (u8)(
ad9b0 30 78 30 30 44 38 20 2b 20 28 28 28 63 2d 30 78 0x00D8 + (((c-0x
ad9c0 31 30 30 30 30 29 3e 3e 31 38 29 26 30 78 30 33 10000)>>18)&0x03
ad9d0 29 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ));
ad9e0 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d \. *zOut++ =
ad9f0 20 28 75 38 29 28 28 28 63 3e 3e 31 30 29 26 30 (u8)(((c>>10)&0
ada00 78 30 30 33 46 29 20 2b 20 28 28 28 63 2d 30 78 x003F) + (((c-0x
ada10 31 30 30 30 30 29 3e 3e 31 30 29 26 30 78 30 30 10000)>>10)&0x00
ada20 43 30 29 29 3b 20 20 5c 0a 20 20 20 20 2a 7a 4f C0)); \. *zO
ada30 75 74 2b 2b 20 3d 20 28 75 38 29 28 30 78 30 30 ut++ = (u8)(0x00
ada40 44 43 20 2b 20 28 28 63 3e 3e 38 29 26 30 78 30 DC + ((c>>8)&0x0
ada50 33 29 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 3));
ada60 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
ada70 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 *zOut++ = (u8
ada80 29 28 63 26 30 78 30 30 46 46 29 3b 20 20 20 20 )(c&0x00FF);
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 5c 0a 20 20 7d 20 20 20 20 20 20 20 20 \. }
adac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
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 5c 0a 7d 0a 0a 23 64 \.}..#d
adb00 65 66 69 6e 65 20 52 45 41 44 5f 55 54 46 31 36 efine READ_UTF16
adb10 4c 45 28 7a 49 6e 2c 20 63 29 7b 20 20 20 20 20 LE(zIn, c){
adb20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adb30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adb40 20 20 20 20 5c 0a 20 20 63 20 3d 20 28 2a 7a 49 \. c = (*zI
adb50 6e 2b 2b 29 3b 20 20 20 20 20 20 20 20 20 20 20 n++);
adb60 20 20 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 20 20 20 5c 0a 20 20 \.
adb90 63 20 2b 3d 20 28 28 2a 7a 49 6e 2b 2b 29 3c 3c c += ((*zIn++)<<
adba0 38 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 8);
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 20
adbd0 20 20 20 20 5c 0a 20 20 69 66 28 20 63 3e 3d 30 \. if( c>=0
adbe0 78 44 38 30 30 20 26 26 20 63 3c 30 78 45 30 30 xD800 && c<0xE00
adbf0 30 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 20 0 ){
adc00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adc10 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
adc20 20 20 69 6e 74 20 63 32 20 3d 20 28 2a 7a 49 6e int c2 = (*zIn
adc30 2b 2b 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 ++);
adc40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adc50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adc60 20 20 20 20 5c 0a 20 20 20 20 63 32 20 2b 3d 20 \. c2 +=
adc70 28 28 2a 7a 49 6e 2b 2b 29 3c 3c 38 29 3b 20 20 ((*zIn++)<<8);
adc80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adc90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adca0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
adcb0 20 20 63 20 3d 20 28 63 32 26 30 78 30 33 46 46 c = (c2&0x03FF
adcc0 29 20 2b 20 28 28 63 26 30 78 30 30 33 46 29 3c ) + ((c&0x003F)<
adcd0 3c 31 30 29 20 2b 20 28 28 28 63 26 30 78 30 33 <10) + (((c&0x03
adce0 43 30 29 2b 30 78 30 30 34 30 29 3c 3c 31 30 29 C0)+0x0040)<<10)
adcf0 3b 20 20 20 5c 0a 20 20 7d 20 20 20 20 20 20 20 ; \. }
add00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
add10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
add20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
add30 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 7d 0a \.}.
add40 0a 23 64 65 66 69 6e 65 20 52 45 41 44 5f 55 54 .#define READ_UT
add50 46 31 36 42 45 28 7a 49 6e 2c 20 63 29 7b 20 20 F16BE(zIn, c){
add60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
add70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
add80 20 20 20 20 20 20 20 5c 0a 20 20 63 20 3d 20 28 \. c = (
add90 28 2a 7a 49 6e 2b 2b 29 3c 3c 38 29 3b 20 20 20 (*zIn++)<<8);
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 20 20 20 20
addc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
addd0 0a 20 20 63 20 2b 3d 20 28 2a 7a 49 6e 2b 2b 29 . c += (*zIn++)
adde0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
addf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ade00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ade10 20 20 20 20 20 20 20 5c 0a 20 20 69 66 28 20 63 \. if( c
ade20 3e 3d 30 78 44 38 30 30 20 26 26 20 63 3c 30 78 >=0xD800 && c<0x
ade30 45 30 30 30 20 29 7b 20 20 20 20 20 20 20 20 20 E000 ){
ade40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ade50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
ade60 0a 20 20 20 20 69 6e 74 20 63 32 20 3d 20 28 28 . int c2 = ((
ade70 2a 7a 49 6e 2b 2b 29 3c 3c 38 29 3b 20 20 20 20 *zIn++)<<8);
ade80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ade90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adea0 20 20 20 20 20 20 20 5c 0a 20 20 20 20 63 32 20 \. c2
adeb0 2b 3d 20 28 2a 7a 49 6e 2b 2b 29 3b 20 20 20 20 += (*zIn++);
adec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aded0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
adef0 0a 20 20 20 20 63 20 3d 20 28 63 32 26 30 78 30 . c = (c2&0x0
adf00 33 46 46 29 20 2b 20 28 28 63 26 30 78 30 30 33 3FF) + ((c&0x003
adf10 46 29 3c 3c 31 30 29 20 2b 20 28 28 28 63 26 30 F)<<10) + (((c&0
adf20 78 30 33 43 30 29 2b 30 78 30 30 34 30 29 3c 3c x03C0)+0x0040)<<
adf30 31 30 29 3b 20 20 20 5c 0a 20 20 7d 20 20 20 20 10); \. }
adf40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adf50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adf60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adf70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
adf80 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 61 6e 73 6c .}../*.** Transl
adf90 61 74 65 20 61 20 73 69 6e 67 6c 65 20 55 54 46 ate a single UTF
adfa0 2d 38 20 63 68 61 72 61 63 74 65 72 2e 20 20 52 -8 character. R
adfb0 65 74 75 72 6e 20 74 68 65 20 75 6e 69 63 6f 64 eturn the unicod
adfc0 65 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 44 e value..**.** D
adfd0 75 72 69 6e 67 20 74 72 61 6e 73 6c 61 74 69 6f uring translatio
adfe0 6e 2c 20 61 73 73 75 6d 65 20 74 68 61 74 20 74 n, assume that t
adff0 68 65 20 62 79 74 65 20 74 68 61 74 20 7a 54 65 he byte that zTe
ae000 72 6d 20 70 6f 69 6e 74 73 0a 2a 2a 20 69 73 20 rm points.** is
ae010 61 20 30 78 30 30 2e 0a 2a 2a 0a 2a 2a 20 57 72 a 0x00..**.** Wr
ae020 69 74 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f ite a pointer to
ae030 20 74 68 65 20 6e 65 78 74 20 75 6e 72 65 61 64 the next unread
ae040 20 62 79 74 65 20 62 61 63 6b 20 69 6e 74 6f 20 byte back into
ae050 2a 70 7a 4e 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 4e *pzNext..**.** N
ae060 6f 74 65 73 20 4f 6e 20 49 6e 76 61 6c 69 64 20 otes On Invalid
ae070 55 54 46 2d 38 3a 0a 2a 2a 0a 2a 2a 20 20 2a 20 UTF-8:.**.** *
ae080 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6e 65 This routine ne
ae090 76 65 72 20 61 6c 6c 6f 77 73 20 61 20 37 2d 62 ver allows a 7-b
ae0a0 69 74 20 63 68 61 72 61 63 74 65 72 20 28 30 78 it character (0x
ae0b0 30 30 20 74 68 72 6f 75 67 68 20 30 78 37 66 29 00 through 0x7f)
ae0c0 20 74 6f 0a 2a 2a 20 20 20 20 20 62 65 20 65 6e to.** be en
ae0d0 63 6f 64 65 64 20 61 73 20 61 20 6d 75 6c 74 69 coded as a multi
ae0e0 2d 62 79 74 65 20 63 68 61 72 61 63 74 65 72 2e -byte character.
ae0f0 20 20 41 6e 79 20 6d 75 6c 74 69 2d 62 79 74 65 Any multi-byte
ae100 20 63 68 61 72 61 63 74 65 72 20 74 68 61 74 0a character that.
ae110 2a 2a 20 20 20 20 20 61 74 74 65 6d 70 74 73 20 ** attempts
ae120 74 6f 20 65 6e 63 6f 64 65 20 61 20 76 61 6c 75 to encode a valu
ae130 65 20 62 65 74 77 65 65 6e 20 30 78 30 30 20 61 e between 0x00 a
ae140 6e 64 20 30 78 37 66 20 69 73 20 72 65 6e 64 65 nd 0x7f is rende
ae150 72 65 64 20 61 73 20 30 78 66 66 66 64 2e 0a 2a red as 0xfffd..*
ae160 2a 0a 2a 2a 20 20 2a 20 20 54 68 69 73 20 72 6f *.** * This ro
ae170 75 74 69 6e 65 20 6e 65 76 65 72 20 61 6c 6c 6f utine never allo
ae180 77 73 20 61 20 55 54 46 31 36 20 73 75 72 72 6f ws a UTF16 surro
ae190 67 61 74 65 20 76 61 6c 75 65 20 74 6f 20 62 65 gate value to be
ae1a0 20 65 6e 63 6f 64 65 64 2e 0a 2a 2a 20 20 20 20 encoded..**
ae1b0 20 49 66 20 61 20 6d 75 6c 74 69 2d 62 79 74 65 If a multi-byte
ae1c0 20 63 68 61 72 61 63 74 65 72 20 61 74 74 65 6d character attem
ae1d0 70 74 73 20 74 6f 20 65 6e 63 6f 64 65 20 61 20 pts to encode a
ae1e0 76 61 6c 75 65 20 62 65 74 77 65 65 6e 0a 2a 2a value between.**
ae1f0 20 20 20 20 20 30 78 64 38 30 30 20 61 6e 64 20 0xd800 and
ae200 30 78 65 30 30 30 20 74 68 65 6e 20 69 74 20 69 0xe000 then it i
ae210 73 20 72 65 6e 64 65 72 65 64 20 61 73 20 30 78 s rendered as 0x
ae220 66 66 66 64 2e 0a 2a 2a 0a 2a 2a 20 20 2a 20 20 fffd..**.** *
ae230 42 79 74 65 73 20 69 6e 20 74 68 65 20 72 61 6e Bytes in the ran
ae240 67 65 20 6f 66 20 30 78 38 30 20 74 68 72 6f 75 ge of 0x80 throu
ae250 67 68 20 30 78 62 66 20 77 68 69 63 68 20 6f 63 gh 0xbf which oc
ae260 63 75 72 20 61 73 20 74 68 65 20 66 69 72 73 74 cur as the first
ae270 0a 2a 2a 20 20 20 20 20 62 79 74 65 20 6f 66 20 .** byte of
ae280 61 20 63 68 61 72 61 63 74 65 72 20 61 72 65 20 a character are
ae290 69 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 73 interpreted as s
ae2a0 69 6e 67 6c 65 2d 62 79 74 65 20 63 68 61 72 61 ingle-byte chara
ae2b0 63 74 65 72 73 0a 2a 2a 20 20 20 20 20 61 6e 64 cters.** and
ae2c0 20 72 65 6e 64 65 72 65 64 20 61 73 20 74 68 65 rendered as the
ae2d0 6d 73 65 6c 76 65 73 20 65 76 65 6e 20 74 68 6f mselves even tho
ae2e0 75 67 68 20 74 68 65 79 20 61 72 65 20 74 65 63 ugh they are tec
ae2f0 68 6e 69 63 61 6c 6c 79 0a 2a 2a 20 20 20 20 20 hnically.**
ae300 69 6e 76 61 6c 69 64 20 63 68 61 72 61 63 74 65 invalid characte
ae310 72 73 2e 0a 2a 2a 0a 2a 2a 20 20 2a 20 20 54 68 rs..**.** * Th
ae320 69 73 20 72 6f 75 74 69 6e 65 20 61 63 63 65 70 is routine accep
ae330 74 73 20 61 6e 20 69 6e 66 69 6e 69 74 65 20 6e ts an infinite n
ae340 75 6d 62 65 72 20 6f 66 20 64 69 66 66 65 72 65 umber of differe
ae350 6e 74 20 55 54 46 38 20 65 6e 63 6f 64 69 6e 67 nt UTF8 encoding
ae360 73 0a 2a 2a 20 20 20 20 20 66 6f 72 20 75 6e 69 s.** for uni
ae370 63 6f 64 65 20 76 61 6c 75 65 73 20 30 78 38 30 code values 0x80
ae380 20 61 6e 64 20 67 72 65 61 74 65 72 2e 20 20 49 and greater. I
ae390 74 20 64 6f 20 6e 6f 74 20 63 68 61 6e 67 65 20 t do not change
ae3a0 6f 76 65 72 2d 6c 65 6e 67 74 68 0a 2a 2a 20 20 over-length.**
ae3b0 20 20 20 65 6e 63 6f 64 69 6e 67 73 20 74 6f 20 encodings to
ae3c0 30 78 66 66 66 64 20 61 73 20 73 6f 6d 65 20 73 0xfffd as some s
ae3d0 79 73 74 65 6d 73 20 72 65 63 6f 6d 6d 65 6e 64 ystems recommend
ae3e0 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 52 45 41 ..*/.#define REA
ae3f0 44 5f 55 54 46 38 28 7a 49 6e 2c 20 7a 54 65 72 D_UTF8(zIn, zTer
ae400 6d 2c 20 63 29 20 20 20 20 20 20 20 20 20 20 20 m, c)
ae410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae420 5c 0a 20 20 63 20 3d 20 2a 28 7a 49 6e 2b 2b 29 \. c = *(zIn++)
ae430 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
ae440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae450 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
ae460 20 69 66 28 20 63 3e 3d 30 78 63 30 20 29 7b 20 if( c>=0xc0 ){
ae470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae490 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 \.
ae4a0 63 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 54 c = sqlite3Utf8T
ae4b0 72 61 6e 73 31 5b 63 2d 30 78 63 30 5d 3b 20 20 rans1[c-0xc0];
ae4c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae4d0 20 20 20 20 20 20 20 5c 0a 20 20 20 20 77 68 69 \. whi
ae4e0 6c 65 28 20 7a 49 6e 21 3d 7a 54 65 72 6d 20 26 le( zIn!=zTerm &
ae4f0 26 20 28 2a 7a 49 6e 20 26 20 30 78 63 30 29 3d & (*zIn & 0xc0)=
ae500 3d 30 78 38 30 20 29 7b 20 20 20 20 20 20 20 20 =0x80 ){
ae510 20 20 20 20 5c 0a 20 20 20 20 20 20 63 20 3d 20 \. c =
ae520 28 63 3c 3c 36 29 20 2b 20 28 30 78 33 66 20 26 (c<<6) + (0x3f &
ae530 20 2a 28 7a 49 6e 2b 2b 29 29 3b 20 20 20 20 20 *(zIn++));
ae540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae550 20 5c 0a 20 20 20 20 7d 20 20 20 20 20 20 20 20 \. }
ae560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
ae590 20 20 20 20 69 66 28 20 63 3c 30 78 38 30 20 20 if( c<0x80
ae5a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae5b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae5c0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
ae5d0 20 20 20 20 20 7c 7c 20 28 63 26 30 78 46 46 46 || (c&0xFFF
ae5e0 46 46 38 30 30 29 3d 3d 30 78 44 38 30 30 20 20 FF800)==0xD800
ae5f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae600 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 20 20 \.
ae610 20 20 7c 7c 20 28 63 26 30 78 46 46 46 46 46 46 || (c&0xFFFFFF
ae620 46 45 29 3d 3d 30 78 46 46 46 45 20 29 7b 20 20 FE)==0xFFFE ){
ae630 63 20 3d 20 30 78 46 46 46 44 3b 20 7d 20 20 20 c = 0xFFFD; }
ae640 20 20 20 20 20 5c 0a 20 20 7d 0a 53 51 4c 49 54 \. }.SQLIT
ae650 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
ae660 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 0a 20 lite3Utf8Read(.
ae670 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 const unsigned
ae680 63 68 61 72 20 2a 7a 49 6e 2c 20 20 20 20 20 20 char *zIn,
ae690 20 2f 2a 20 46 69 72 73 74 20 62 79 74 65 20 6f /* First byte o
ae6a0 66 20 55 54 46 2d 38 20 63 68 61 72 61 63 74 65 f UTF-8 characte
ae6b0 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 75 6e 73 r */. const uns
ae6c0 69 67 6e 65 64 20 63 68 61 72 20 2a 2a 70 7a 4e igned char **pzN
ae6d0 65 78 74 20 20 20 20 2f 2a 20 57 72 69 74 65 20 ext /* Write
ae6e0 66 69 72 73 74 20 62 79 74 65 20 70 61 73 74 20 first byte past
ae6f0 55 54 46 2d 38 20 63 68 61 72 20 68 65 72 65 20 UTF-8 char here
ae700 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 63 3b 0a 0a */.){. int c;..
ae710 20 20 2f 2a 20 53 61 6d 65 20 61 73 20 52 45 41 /* Same as REA
ae720 44 5f 55 54 46 38 28 29 20 61 62 6f 76 65 20 62 D_UTF8() above b
ae730 75 74 20 77 69 74 68 6f 75 74 20 74 68 65 20 7a ut without the z
ae740 54 65 72 6d 20 70 61 72 61 6d 65 74 65 72 2e 0a Term parameter..
ae750 20 20 2a 2a 20 46 6f 72 20 74 68 69 73 20 72 6f ** For this ro
ae760 75 74 69 6e 65 2c 20 77 65 20 61 73 73 75 6d 65 utine, we assume
ae770 20 74 68 65 20 55 54 46 38 20 73 74 72 69 6e 67 the UTF8 string
ae780 20 69 73 20 61 6c 77 61 79 73 20 7a 65 72 6f 2d is always zero-
ae790 74 65 72 6d 69 6e 61 74 65 64 2e 0a 20 20 2a 2f terminated.. */
ae7a0 0a 20 20 63 20 3d 20 2a 28 7a 49 6e 2b 2b 29 3b . c = *(zIn++);
ae7b0 0a 20 20 69 66 28 20 63 3e 3d 30 78 63 30 20 29 . if( c>=0xc0 )
ae7c0 7b 0a 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 {. c = sqlite
ae7d0 33 55 74 66 38 54 72 61 6e 73 31 5b 63 2d 30 78 3Utf8Trans1[c-0x
ae7e0 63 30 5d 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 c0];. while(
ae7f0 28 2a 7a 49 6e 20 26 20 30 78 63 30 29 3d 3d 30 (*zIn & 0xc0)==0
ae800 78 38 30 20 29 7b 0a 20 20 20 20 20 20 63 20 3d x80 ){. c =
ae810 20 28 63 3c 3c 36 29 20 2b 20 28 30 78 33 66 20 (c<<6) + (0x3f
ae820 26 20 2a 28 7a 49 6e 2b 2b 29 29 3b 0a 20 20 20 & *(zIn++));.
ae830 20 7d 0a 20 20 20 20 69 66 28 20 63 3c 30 78 38 }. if( c<0x8
ae840 30 0a 20 20 20 20 20 20 20 20 7c 7c 20 28 63 26 0. || (c&
ae850 30 78 46 46 46 46 46 38 30 30 29 3d 3d 30 78 44 0xFFFFF800)==0xD
ae860 38 30 30 0a 20 20 20 20 20 20 20 20 7c 7c 20 28 800. || (
ae870 63 26 30 78 46 46 46 46 46 46 46 45 29 3d 3d 30 c&0xFFFFFFFE)==0
ae880 78 46 46 46 45 20 29 7b 20 20 63 20 3d 20 30 78 xFFFE ){ c = 0x
ae890 46 46 46 44 3b 20 7d 0a 20 20 7d 0a 20 20 2a 70 FFFD; }. }. *p
ae8a0 7a 4e 65 78 74 20 3d 20 7a 49 6e 3b 0a 20 20 72 zNext = zIn;. r
ae8b0 65 74 75 72 6e 20 63 3b 0a 7d 0a 0a 0a 0a 0a 2f eturn c;.}...../
ae8c0 2a 0a 2a 2a 20 49 66 20 74 68 65 20 54 52 41 4e *.** If the TRAN
ae8d0 53 4c 41 54 45 5f 54 52 41 43 45 20 6d 61 63 72 SLATE_TRACE macr
ae8e0 6f 20 69 73 20 64 65 66 69 6e 65 64 2c 20 74 68 o is defined, th
ae8f0 65 20 76 61 6c 75 65 20 6f 66 20 65 61 63 68 20 e value of each
ae900 4d 65 6d 20 69 73 0a 2a 2a 20 70 72 69 6e 74 65 Mem is.** printe
ae910 64 20 6f 6e 20 73 74 64 65 72 72 20 6f 6e 20 74 d on stderr on t
ae920 68 65 20 77 61 79 20 69 6e 74 6f 20 61 6e 64 20 he way into and
ae930 6f 75 74 20 6f 66 20 73 71 6c 69 74 65 33 56 64 out of sqlite3Vd
ae940 62 65 4d 65 6d 54 72 61 6e 73 6c 61 74 65 28 29 beMemTranslate()
ae950 2e 0a 2a 2f 20 0a 2f 2a 20 23 64 65 66 69 6e 65 ..*/ ./* #define
ae960 20 54 52 41 4e 53 4c 41 54 45 5f 54 52 41 43 45 TRANSLATE_TRACE
ae970 20 31 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 1 */..#ifndef S
ae980 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 QLITE_OMIT_UTF16
ae990 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 ./*.** This rout
ae9a0 69 6e 65 20 74 72 61 6e 73 66 6f 72 6d 73 20 74 ine transforms t
ae9b0 68 65 20 69 6e 74 65 72 6e 61 6c 20 74 65 78 74 he internal text
ae9c0 20 65 6e 63 6f 64 69 6e 67 20 75 73 65 64 20 62 encoding used b
ae9d0 79 20 70 4d 65 6d 20 74 6f 0a 2a 2a 20 64 65 73 y pMem to.** des
ae9e0 69 72 65 64 45 6e 63 2e 20 49 74 20 69 73 20 61 iredEnc. It is a
ae9f0 6e 20 65 72 72 6f 72 20 69 66 20 74 68 65 20 73 n error if the s
aea00 74 72 69 6e 67 20 69 73 20 61 6c 72 65 61 64 79 tring is already
aea10 20 6f 66 20 74 68 65 20 64 65 73 69 72 65 64 0a of the desired.
aea20 2a 2a 20 65 6e 63 6f 64 69 6e 67 2c 20 6f 72 20 ** encoding, or
aea30 69 66 20 2a 70 4d 65 6d 20 64 6f 65 73 20 6e 6f if *pMem does no
aea40 74 20 63 6f 6e 74 61 69 6e 20 61 20 73 74 72 69 t contain a stri
aea50 6e 67 20 76 61 6c 75 65 2e 0a 2a 2f 0a 53 51 4c ng value..*/.SQL
aea60 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
aea70 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 54 72 sqlite3VdbeMemTr
aea80 61 6e 73 6c 61 74 65 28 4d 65 6d 20 2a 70 4d 65 anslate(Mem *pMe
aea90 6d 2c 20 75 38 20 64 65 73 69 72 65 64 45 6e 63 m, u8 desiredEnc
aeaa0 29 7b 0a 20 20 69 6e 74 20 6c 65 6e 3b 20 20 20 ){. int len;
aeab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aeac0 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 /* Maximum leng
aead0 74 68 20 6f 66 20 6f 75 74 70 75 74 20 73 74 72 th of output str
aeae0 69 6e 67 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a ing in bytes */.
aeaf0 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 unsigned char
aeb00 2a 7a 4f 75 74 3b 20 20 20 20 20 20 20 20 20 20 *zOut;
aeb10 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 74 70 75 /* Outpu
aeb20 74 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 75 6e t buffer */. un
aeb30 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 49 6e signed char *zIn
aeb40 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
aeb50 20 20 20 20 2f 2a 20 49 6e 70 75 74 20 69 74 65 /* Input ite
aeb60 72 61 74 6f 72 20 2a 2f 0a 20 20 75 6e 73 69 67 rator */. unsig
aeb70 6e 65 64 20 63 68 61 72 20 2a 7a 54 65 72 6d 3b ned char *zTerm;
aeb80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aeb90 20 2f 2a 20 45 6e 64 20 6f 66 20 69 6e 70 75 74 /* End of input
aeba0 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 */. unsigned c
aebb0 68 61 72 20 2a 7a 3b 20 20 20 20 20 20 20 20 20 har *z;
aebc0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f /* O
aebd0 75 74 70 75 74 20 69 74 65 72 61 74 6f 72 20 2a utput iterator *
aebe0 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 /. unsigned int
aebf0 20 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 c;.. assert( p
aec00 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 Mem->db==0 || sq
aec10 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
aec20 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 (pMem->db->mutex
aec30 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 ) );. assert( p
aec40 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 Mem->flags&MEM_S
aec50 74 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 tr );. assert(
aec60 70 4d 65 6d 2d 3e 65 6e 63 21 3d 64 65 73 69 72 pMem->enc!=desir
aec70 65 64 45 6e 63 20 29 3b 0a 20 20 61 73 73 65 72 edEnc );. asser
aec80 74 28 20 70 4d 65 6d 2d 3e 65 6e 63 21 3d 30 20 t( pMem->enc!=0
aec90 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 );. assert( pMe
aeca0 6d 2d 3e 6e 3e 3d 30 20 29 3b 0a 0a 23 69 66 20 m->n>=0 );..#if
aecb0 64 65 66 69 6e 65 64 28 54 52 41 4e 53 4c 41 54 defined(TRANSLAT
aecc0 45 5f 54 52 41 43 45 29 20 26 26 20 64 65 66 69 E_TRACE) && defi
aecd0 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 ned(SQLITE_DEBUG
aece0 29 0a 20 20 7b 0a 20 20 20 20 63 68 61 72 20 7a ). {. char z
aecf0 42 75 66 5b 31 30 30 5d 3b 0a 20 20 20 20 73 71 Buf[100];. sq
aed00 6c 69 74 65 33 56 64 62 65 4d 65 6d 50 72 65 74 lite3VdbeMemPret
aed10 74 79 50 72 69 6e 74 28 70 4d 65 6d 2c 20 7a 42 tyPrint(pMem, zB
aed20 75 66 29 3b 0a 20 20 20 20 66 70 72 69 6e 74 66 uf);. fprintf
aed30 28 73 74 64 65 72 72 2c 20 22 49 4e 50 55 54 3a (stderr, "INPUT:
aed40 20 20 25 73 5c 6e 22 2c 20 7a 42 75 66 29 3b 0a %s\n", zBuf);.
aed50 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a }.#endif.. /*
aed60 20 49 66 20 74 68 65 20 74 72 61 6e 73 6c 61 74 If the translat
aed70 69 6f 6e 20 69 73 20 62 65 74 77 65 65 6e 20 55 ion is between U
aed80 54 46 2d 31 36 20 6c 69 74 74 6c 65 20 61 6e 64 TF-16 little and
aed90 20 62 69 67 20 65 6e 64 69 61 6e 2c 20 74 68 65 big endian, the
aeda0 6e 20 0a 20 20 2a 2a 20 61 6c 6c 20 74 68 61 74 n . ** all that
aedb0 20 69 73 20 72 65 71 75 69 72 65 64 20 69 73 20 is required is
aedc0 74 6f 20 73 77 61 70 20 74 68 65 20 62 79 74 65 to swap the byte
aedd0 20 6f 72 64 65 72 2e 20 54 68 69 73 20 63 61 73 order. This cas
aede0 65 20 69 73 20 68 61 6e 64 6c 65 64 0a 20 20 2a e is handled. *
aedf0 2a 20 64 69 66 66 65 72 65 6e 74 6c 79 20 66 72 * differently fr
aee00 6f 6d 20 74 68 65 20 6f 74 68 65 72 73 2e 0a 20 om the others..
aee10 20 2a 2f 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e */. if( pMem->
aee20 65 6e 63 21 3d 53 51 4c 49 54 45 5f 55 54 46 38 enc!=SQLITE_UTF8
aee30 20 26 26 20 64 65 73 69 72 65 64 45 6e 63 21 3d && desiredEnc!=
aee40 53 51 4c 49 54 45 5f 55 54 46 38 20 29 7b 0a 20 SQLITE_UTF8 ){.
aee50 20 20 20 75 38 20 74 65 6d 70 3b 0a 20 20 20 20 u8 temp;.
aee60 69 6e 74 20 72 63 3b 0a 20 20 20 20 72 63 20 3d int rc;. rc =
aee70 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d sqlite3VdbeMemM
aee80 61 6b 65 57 72 69 74 65 61 62 6c 65 28 70 4d 65 akeWriteable(pMe
aee90 6d 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d m);. if( rc!=
aeea0 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
aeeb0 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 assert( rc==S
aeec0 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 QLITE_NOMEM );.
aeed0 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
aeee0 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a TE_NOMEM;. }.
aeef0 20 20 20 20 7a 49 6e 20 3d 20 28 75 38 2a 29 70 zIn = (u8*)p
aef00 4d 65 6d 2d 3e 7a 3b 0a 20 20 20 20 7a 54 65 72 Mem->z;. zTer
aef10 6d 20 3d 20 26 7a 49 6e 5b 70 4d 65 6d 2d 3e 6e m = &zIn[pMem->n
aef20 26 7e 31 5d 3b 0a 20 20 20 20 77 68 69 6c 65 28 &~1];. while(
aef30 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a 20 20 zIn<zTerm ){.
aef40 20 20 20 20 74 65 6d 70 20 3d 20 2a 7a 49 6e 3b temp = *zIn;
aef50 0a 20 20 20 20 20 20 2a 7a 49 6e 20 3d 20 2a 28 . *zIn = *(
aef60 7a 49 6e 2b 31 29 3b 0a 20 20 20 20 20 20 7a 49 zIn+1);. zI
aef70 6e 2b 2b 3b 0a 20 20 20 20 20 20 2a 7a 49 6e 2b n++;. *zIn+
aef80 2b 20 3d 20 74 65 6d 70 3b 0a 20 20 20 20 7d 0a + = temp;. }.
aef90 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 pMem->enc =
aefa0 64 65 73 69 72 65 64 45 6e 63 3b 0a 20 20 20 20 desiredEnc;.
aefb0 67 6f 74 6f 20 74 72 61 6e 73 6c 61 74 65 5f 6f goto translate_o
aefc0 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 ut;. }.. /* Se
aefd0 74 20 6c 65 6e 20 74 6f 20 74 68 65 20 6d 61 78 t len to the max
aefe0 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 62 imum number of b
aeff0 79 74 65 73 20 72 65 71 75 69 72 65 64 20 69 6e ytes required in
af000 20 74 68 65 20 6f 75 74 70 75 74 20 62 75 66 66 the output buff
af010 65 72 2e 20 2a 2f 0a 20 20 69 66 28 20 64 65 73 er. */. if( des
af020 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f iredEnc==SQLITE_
af030 55 54 46 38 20 29 7b 0a 20 20 20 20 2f 2a 20 57 UTF8 ){. /* W
af040 68 65 6e 20 63 6f 6e 76 65 72 74 69 6e 67 20 66 hen converting f
af050 72 6f 6d 20 55 54 46 2d 31 36 2c 20 74 68 65 20 rom UTF-16, the
af060 6d 61 78 69 6d 75 6d 20 67 72 6f 77 74 68 20 72 maximum growth r
af070 65 73 75 6c 74 73 20 66 72 6f 6d 0a 20 20 20 20 esults from.
af080 2a 2a 20 74 72 61 6e 73 6c 61 74 69 6e 67 20 61 ** translating a
af090 20 32 2d 62 79 74 65 20 63 68 61 72 61 63 74 65 2-byte characte
af0a0 72 20 74 6f 20 61 20 34 2d 62 79 74 65 20 55 54 r to a 4-byte UT
af0b0 46 2d 38 20 63 68 61 72 61 63 74 65 72 2e 0a 20 F-8 character..
af0c0 20 20 20 2a 2a 20 41 20 73 69 6e 67 6c 65 20 62 ** A single b
af0d0 79 74 65 20 69 73 20 72 65 71 75 69 72 65 64 20 yte is required
af0e0 66 6f 72 20 74 68 65 20 6f 75 74 70 75 74 20 73 for the output s
af0f0 74 72 69 6e 67 0a 20 20 20 20 2a 2a 20 6e 75 6c tring. ** nul
af100 2d 74 65 72 6d 69 6e 61 74 6f 72 2e 0a 20 20 20 -terminator..
af110 20 2a 2f 0a 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 */. pMem->n
af120 26 3d 20 7e 31 3b 0a 20 20 20 20 6c 65 6e 20 3d &= ~1;. len =
af130 20 70 4d 65 6d 2d 3e 6e 20 2a 20 32 20 2b 20 31 pMem->n * 2 + 1
af140 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f ;. }else{. /
af150 2a 20 57 68 65 6e 20 63 6f 6e 76 65 72 74 69 6e * When convertin
af160 67 20 66 72 6f 6d 20 55 54 46 2d 38 20 74 6f 20 g from UTF-8 to
af170 55 54 46 2d 31 36 20 74 68 65 20 6d 61 78 69 6d UTF-16 the maxim
af180 75 6d 20 67 72 6f 77 74 68 20 69 73 20 63 61 75 um growth is cau
af190 73 65 64 0a 20 20 20 20 2a 2a 20 77 68 65 6e 20 sed. ** when
af1a0 61 20 31 2d 62 79 74 65 20 55 54 46 2d 38 20 63 a 1-byte UTF-8 c
af1b0 68 61 72 61 63 74 65 72 20 69 73 20 74 72 61 6e haracter is tran
af1c0 73 6c 61 74 65 64 20 69 6e 74 6f 20 61 20 32 2d slated into a 2-
af1d0 62 79 74 65 20 55 54 46 2d 31 36 0a 20 20 20 20 byte UTF-16.
af1e0 2a 2a 20 63 68 61 72 61 63 74 65 72 2e 20 54 77 ** character. Tw
af1f0 6f 20 62 79 74 65 73 20 61 72 65 20 72 65 71 75 o bytes are requ
af200 69 72 65 64 20 69 6e 20 74 68 65 20 6f 75 74 70 ired in the outp
af210 75 74 20 62 75 66 66 65 72 20 66 6f 72 20 74 68 ut buffer for th
af220 65 0a 20 20 20 20 2a 2a 20 6e 75 6c 2d 74 65 72 e. ** nul-ter
af230 6d 69 6e 61 74 6f 72 2e 0a 20 20 20 20 2a 2f 0a minator.. */.
af240 20 20 20 20 6c 65 6e 20 3d 20 70 4d 65 6d 2d 3e len = pMem->
af250 6e 20 2a 20 32 20 2b 20 32 3b 0a 20 20 7d 0a 0a n * 2 + 2;. }..
af260 20 20 2f 2a 20 53 65 74 20 7a 49 6e 20 74 6f 20 /* Set zIn to
af270 70 6f 69 6e 74 20 61 74 20 74 68 65 20 73 74 61 point at the sta
af280 72 74 20 6f 66 20 74 68 65 20 69 6e 70 75 74 20 rt of the input
af290 62 75 66 66 65 72 20 61 6e 64 20 7a 54 65 72 6d buffer and zTerm
af2a0 20 74 6f 20 70 6f 69 6e 74 20 31 0a 20 20 2a 2a to point 1. **
af2b0 20 62 79 74 65 20 70 61 73 74 20 74 68 65 20 65 byte past the e
af2c0 6e 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 56 61 nd.. **. ** Va
af2d0 72 69 61 62 6c 65 20 7a 4f 75 74 20 69 73 20 73 riable zOut is s
af2e0 65 74 20 74 6f 20 70 6f 69 6e 74 20 61 74 20 74 et to point at t
af2f0 68 65 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 he output buffer
af300 2c 20 73 70 61 63 65 20 6f 62 74 61 69 6e 65 64 , space obtained
af310 0a 20 20 2a 2a 20 66 72 6f 6d 20 73 71 6c 69 74 . ** from sqlit
af320 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e 0a 20 20 2a e3_malloc().. *
af330 2f 0a 20 20 7a 49 6e 20 3d 20 28 75 38 2a 29 70 /. zIn = (u8*)p
af340 4d 65 6d 2d 3e 7a 3b 0a 20 20 7a 54 65 72 6d 20 Mem->z;. zTerm
af350 3d 20 26 7a 49 6e 5b 70 4d 65 6d 2d 3e 6e 5d 3b = &zIn[pMem->n];
af360 0a 20 20 7a 4f 75 74 20 3d 20 73 71 6c 69 74 65 . zOut = sqlite
af370 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 70 4d 65 3DbMallocRaw(pMe
af380 6d 2d 3e 64 62 2c 20 6c 65 6e 29 3b 0a 20 20 69 m->db, len);. i
af390 66 28 20 21 7a 4f 75 74 20 29 7b 0a 20 20 20 20 f( !zOut ){.
af3a0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f return SQLITE_NO
af3b0 4d 45 4d 3b 0a 20 20 7d 0a 20 20 7a 20 3d 20 7a MEM;. }. z = z
af3c0 4f 75 74 3b 0a 0a 20 20 69 66 28 20 70 4d 65 6d Out;.. if( pMem
af3d0 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 ->enc==SQLITE_UT
af3e0 46 38 20 29 7b 0a 20 20 20 20 69 66 28 20 64 65 F8 ){. if( de
af3f0 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45 siredEnc==SQLITE
af400 5f 55 54 46 31 36 4c 45 20 29 7b 0a 20 20 20 20 _UTF16LE ){.
af410 20 20 2f 2a 20 55 54 46 2d 38 20 2d 3e 20 55 54 /* UTF-8 -> UT
af420 46 2d 31 36 20 4c 69 74 74 6c 65 2d 65 6e 64 69 F-16 Little-endi
af430 61 6e 20 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c an */. whil
af440 65 28 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a e( zIn<zTerm ){.
af450 20 20 20 20 20 20 20 20 2f 2a 20 63 20 3d 20 73 /* c = s
af460 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a qlite3Utf8Read(z
af470 49 6e 2c 20 7a 54 65 72 6d 2c 20 28 63 6f 6e 73 In, zTerm, (cons
af480 74 20 75 38 2a 2a 29 26 7a 49 6e 29 3b 20 2a 2f t u8**)&zIn); */
af490 0a 20 20 20 20 20 20 20 20 52 45 41 44 5f 55 54 . READ_UT
af4a0 46 38 28 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20 63 F8(zIn, zTerm, c
af4b0 29 3b 0a 20 20 20 20 20 20 20 20 57 52 49 54 45 );. WRITE
af4c0 5f 55 54 46 31 36 4c 45 28 7a 2c 20 63 29 3b 0a _UTF16LE(z, c);.
af4d0 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 }. }els
af4e0 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 e{. assert(
af4f0 20 64 65 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c desiredEnc==SQL
af500 49 54 45 5f 55 54 46 31 36 42 45 20 29 3b 0a 20 ITE_UTF16BE );.
af510 20 20 20 20 20 2f 2a 20 55 54 46 2d 38 20 2d 3e /* UTF-8 ->
af520 20 55 54 46 2d 31 36 20 42 69 67 2d 65 6e 64 69 UTF-16 Big-endi
af530 61 6e 20 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c an */. whil
af540 65 28 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a e( zIn<zTerm ){.
af550 20 20 20 20 20 20 20 20 2f 2a 20 63 20 3d 20 73 /* c = s
af560 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a qlite3Utf8Read(z
af570 49 6e 2c 20 7a 54 65 72 6d 2c 20 28 63 6f 6e 73 In, zTerm, (cons
af580 74 20 75 38 2a 2a 29 26 7a 49 6e 29 3b 20 2a 2f t u8**)&zIn); */
af590 0a 20 20 20 20 20 20 20 20 52 45 41 44 5f 55 54 . READ_UT
af5a0 46 38 28 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20 63 F8(zIn, zTerm, c
af5b0 29 3b 0a 20 20 20 20 20 20 20 20 57 52 49 54 45 );. WRITE
af5c0 5f 55 54 46 31 36 42 45 28 7a 2c 20 63 29 3b 0a _UTF16BE(z, c);.
af5d0 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
af5e0 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 28 69 6e 74 pMem->n = (int
af5f0 29 28 7a 20 2d 20 7a 4f 75 74 29 3b 0a 20 20 20 )(z - zOut);.
af600 20 2a 7a 2b 2b 20 3d 20 30 3b 0a 20 20 7d 65 6c *z++ = 0;. }el
af610 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 se{. assert(
af620 64 65 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49 desiredEnc==SQLI
af630 54 45 5f 55 54 46 38 20 29 3b 0a 20 20 20 20 69 TE_UTF8 );. i
af640 66 28 20 70 4d 65 6d 2d 3e 65 6e 63 3d 3d 53 51 f( pMem->enc==SQ
af650 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 29 7b 0a LITE_UTF16LE ){.
af660 20 20 20 20 20 20 2f 2a 20 55 54 46 2d 31 36 20 /* UTF-16
af670 4c 69 74 74 6c 65 2d 65 6e 64 69 61 6e 20 2d 3e Little-endian ->
af680 20 55 54 46 2d 38 20 2a 2f 0a 20 20 20 20 20 20 UTF-8 */.
af690 77 68 69 6c 65 28 20 7a 49 6e 3c 7a 54 65 72 6d while( zIn<zTerm
af6a0 20 29 7b 0a 20 20 20 20 20 20 20 20 52 45 41 44 ){. READ
af6b0 5f 55 54 46 31 36 4c 45 28 7a 49 6e 2c 20 63 29 _UTF16LE(zIn, c)
af6c0 3b 20 0a 20 20 20 20 20 20 20 20 57 52 49 54 45 ; . WRITE
af6d0 5f 55 54 46 38 28 7a 2c 20 63 29 3b 0a 20 20 20 _UTF8(z, c);.
af6e0 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a }. }else{.
af6f0 20 20 20 20 20 20 2f 2a 20 55 54 46 2d 31 36 20 /* UTF-16
af700 42 69 67 2d 65 6e 64 69 61 6e 20 2d 3e 20 55 54 Big-endian -> UT
af710 46 2d 38 20 2a 2f 0a 20 20 20 20 20 20 77 68 69 F-8 */. whi
af720 6c 65 28 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b le( zIn<zTerm ){
af730 0a 20 20 20 20 20 20 20 20 52 45 41 44 5f 55 54 . READ_UT
af740 46 31 36 42 45 28 7a 49 6e 2c 20 63 29 3b 20 0a F16BE(zIn, c); .
af750 20 20 20 20 20 20 20 20 57 52 49 54 45 5f 55 54 WRITE_UT
af760 46 38 28 7a 2c 20 63 29 3b 0a 20 20 20 20 20 20 F8(z, c);.
af770 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d 65 6d }. }. pMem
af780 2d 3e 6e 20 3d 20 28 69 6e 74 29 28 7a 20 2d 20 ->n = (int)(z -
af790 7a 4f 75 74 29 3b 0a 20 20 7d 0a 20 20 2a 7a 20 zOut);. }. *z
af7a0 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 28 = 0;. assert( (
af7b0 70 4d 65 6d 2d 3e 6e 2b 28 64 65 73 69 72 65 64 pMem->n+(desired
af7c0 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 Enc==SQLITE_UTF8
af7d0 3f 31 3a 32 29 29 3c 3d 6c 65 6e 20 29 3b 0a 0a ?1:2))<=len );..
af7e0 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
af7f0 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 Release(pMem);.
af800 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 3d 20 pMem->flags &=
af810 7e 28 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d ~(MEM_Static|MEM
af820 5f 44 79 6e 7c 4d 45 4d 5f 45 70 68 65 6d 29 3b _Dyn|MEM_Ephem);
af830 0a 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 64 . pMem->enc = d
af840 65 73 69 72 65 64 45 6e 63 3b 0a 20 20 70 4d 65 esiredEnc;. pMe
af850 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 28 4d 45 4d m->flags |= (MEM
af860 5f 54 65 72 6d 7c 4d 45 4d 5f 44 79 6e 29 3b 0a _Term|MEM_Dyn);.
af870 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 28 63 68 61 pMem->z = (cha
af880 72 2a 29 7a 4f 75 74 3b 0a 20 20 70 4d 65 6d 2d r*)zOut;. pMem-
af890 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 70 4d 65 6d 2d >zMalloc = pMem-
af8a0 3e 7a 3b 0a 0a 74 72 61 6e 73 6c 61 74 65 5f 6f >z;..translate_o
af8b0 75 74 3a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 ut:.#if defined(
af8c0 54 52 41 4e 53 4c 41 54 45 5f 54 52 41 43 45 29 TRANSLATE_TRACE)
af8d0 20 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49 && defined(SQLI
af8e0 54 45 5f 44 45 42 55 47 29 0a 20 20 7b 0a 20 20 TE_DEBUG). {.
af8f0 20 20 63 68 61 72 20 7a 42 75 66 5b 31 30 30 5d char zBuf[100]
af900 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 ;. sqlite3Vdb
af910 65 4d 65 6d 50 72 65 74 74 79 50 72 69 6e 74 28 eMemPrettyPrint(
af920 70 4d 65 6d 2c 20 7a 42 75 66 29 3b 0a 20 20 20 pMem, zBuf);.
af930 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c fprintf(stderr,
af940 20 22 4f 55 54 50 55 54 3a 20 25 73 5c 6e 22 2c "OUTPUT: %s\n",
af950 20 7a 42 75 66 29 3b 0a 20 20 7d 0a 23 65 6e 64 zBuf);. }.#end
af960 69 66 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 if. return SQLI
af970 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 TE_OK;.}../*.**
af980 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65 This routine che
af990 63 6b 73 20 66 6f 72 20 61 20 62 79 74 65 2d 6f cks for a byte-o
af9a0 72 64 65 72 20 6d 61 72 6b 20 61 74 20 74 68 65 rder mark at the
af9b0 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 beginning of th
af9c0 65 20 0a 2a 2a 20 55 54 46 2d 31 36 20 73 74 72 e .** UTF-16 str
af9d0 69 6e 67 20 73 74 6f 72 65 64 20 69 6e 20 2a 70 ing stored in *p
af9e0 4d 65 6d 2e 20 49 66 20 6f 6e 65 20 69 73 20 70 Mem. If one is p
af9f0 72 65 73 65 6e 74 2c 20 69 74 20 69 73 20 72 65 resent, it is re
afa00 6d 6f 76 65 64 20 61 6e 64 0a 2a 2a 20 74 68 65 moved and.** the
afa10 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 74 68 65 encoding of the
afa20 20 4d 65 6d 20 61 64 6a 75 73 74 65 64 2e 20 54 Mem adjusted. T
afa30 68 69 73 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 his routine does
afa40 20 6e 6f 74 20 64 6f 20 61 6e 79 0a 2a 2a 20 62 not do any.** b
afa50 79 74 65 2d 73 77 61 70 70 69 6e 67 2c 20 69 74 yte-swapping, it
afa60 20 6a 75 73 74 20 73 65 74 73 20 4d 65 6d 2e 65 just sets Mem.e
afa70 6e 63 20 61 70 70 72 6f 70 72 69 61 74 65 6c 79 nc appropriately
afa80 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 6c 6c 6f ..**.** The allo
afa90 63 61 74 69 6f 6e 20 28 73 74 61 74 69 63 2c 20 cation (static,
afaa0 64 79 6e 61 6d 69 63 20 65 74 63 2e 29 20 61 6e dynamic etc.) an
afab0 64 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 74 68 d encoding of th
afac0 65 20 4d 65 6d 20 6d 61 79 20 62 65 0a 2a 2a 20 e Mem may be.**
afad0 63 68 61 6e 67 65 64 20 62 79 20 74 68 69 73 20 changed by this
afae0 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c function..*/.SQL
afaf0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
afb00 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 48 61 sqlite3VdbeMemHa
afb10 6e 64 6c 65 42 6f 6d 28 4d 65 6d 20 2a 70 4d 65 ndleBom(Mem *pMe
afb20 6d 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 m){. int rc = S
afb30 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 38 20 62 QLITE_OK;. u8 b
afb40 6f 6d 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 om = 0;.. asser
afb50 74 28 20 70 4d 65 6d 2d 3e 6e 3e 3d 30 20 29 3b t( pMem->n>=0 );
afb60 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 6e 3e 31 . if( pMem->n>1
afb70 20 29 7b 0a 20 20 20 20 75 38 20 62 31 20 3d 20 ){. u8 b1 =
afb80 2a 28 75 38 20 2a 29 70 4d 65 6d 2d 3e 7a 3b 0a *(u8 *)pMem->z;.
afb90 20 20 20 20 75 38 20 62 32 20 3d 20 2a 28 28 28 u8 b2 = *(((
afba0 75 38 20 2a 29 70 4d 65 6d 2d 3e 7a 29 20 2b 20 u8 *)pMem->z) +
afbb0 31 29 3b 0a 20 20 20 20 69 66 28 20 62 31 3d 3d 1);. if( b1==
afbc0 30 78 46 45 20 26 26 20 62 32 3d 3d 30 78 46 46 0xFE && b2==0xFF
afbd0 20 29 7b 0a 20 20 20 20 20 20 62 6f 6d 20 3d 20 ){. bom =
afbe0 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 3b 0a SQLITE_UTF16BE;.
afbf0 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 62 31 }. if( b1
afc00 3d 3d 30 78 46 46 20 26 26 20 62 32 3d 3d 30 78 ==0xFF && b2==0x
afc10 46 45 20 29 7b 0a 20 20 20 20 20 20 62 6f 6d 20 FE ){. bom
afc20 3d 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 = SQLITE_UTF16LE
afc30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0a 20 ;. }. }. .
afc40 20 69 66 28 20 62 6f 6d 20 29 7b 0a 20 20 20 20 if( bom ){.
afc50 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 rc = sqlite3Vdbe
afc60 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 MemMakeWriteable
afc70 28 70 4d 65 6d 29 3b 0a 20 20 20 20 69 66 28 20 (pMem);. if(
afc80 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
afc90 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 2d . pMem->n -
afca0 3d 20 32 3b 0a 20 20 20 20 20 20 6d 65 6d 6d 6f = 2;. memmo
afcb0 76 65 28 70 4d 65 6d 2d 3e 7a 2c 20 26 70 4d 65 ve(pMem->z, &pMe
afcc0 6d 2d 3e 7a 5b 32 5d 2c 20 70 4d 65 6d 2d 3e 6e m->z[2], pMem->n
afcd0 29 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a );. pMem->z
afce0 5b 70 4d 65 6d 2d 3e 6e 5d 20 3d 20 27 5c 30 27 [pMem->n] = '\0'
afcf0 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 5b ;. pMem->z[
afd00 70 4d 65 6d 2d 3e 6e 2b 31 5d 20 3d 20 27 5c 30 pMem->n+1] = '\0
afd10 27 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 ';. pMem->f
afd20 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d lags |= MEM_Term
afd30 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 65 6e ;. pMem->en
afd40 63 20 3d 20 62 6f 6d 3b 0a 20 20 20 20 7d 0a 20 c = bom;. }.
afd50 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
afd60 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 }.#endif /* SQLI
afd70 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f TE_OMIT_UTF16 */
afd80 0a 0a 2f 2a 0a 2a 2a 20 70 5a 20 69 73 20 61 20 ../*.** pZ is a
afd90 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 75 6e UTF-8 encoded un
afda0 69 63 6f 64 65 20 73 74 72 69 6e 67 2e 20 49 66 icode string. If
afdb0 20 6e 42 79 74 65 20 69 73 20 6c 65 73 73 20 74 nByte is less t
afdc0 68 61 6e 20 7a 65 72 6f 2c 0a 2a 2a 20 72 65 74 han zero,.** ret
afdd0 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f urn the number o
afde0 66 20 75 6e 69 63 6f 64 65 20 63 68 61 72 61 63 f unicode charac
afdf0 74 65 72 73 20 69 6e 20 70 5a 20 75 70 20 74 6f ters in pZ up to
afe00 20 28 62 75 74 20 6e 6f 74 20 69 6e 63 6c 75 64 (but not includ
afe10 69 6e 67 29 0a 2a 2a 20 74 68 65 20 66 69 72 73 ing).** the firs
afe20 74 20 30 78 30 30 20 62 79 74 65 2e 20 49 66 20 t 0x00 byte. If
afe30 6e 42 79 74 65 20 69 73 20 6e 6f 74 20 6c 65 73 nByte is not les
afe40 73 20 74 68 61 6e 20 7a 65 72 6f 2c 20 72 65 74 s than zero, ret
afe50 75 72 6e 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 urn the.** numbe
afe60 72 20 6f 66 20 75 6e 69 63 6f 64 65 20 63 68 61 r of unicode cha
afe70 72 61 63 74 65 72 73 20 69 6e 20 74 68 65 20 66 racters in the f
afe80 69 72 73 74 20 6e 42 79 74 65 20 6f 66 20 70 5a irst nByte of pZ
afe90 20 28 6f 72 20 75 70 20 74 6f 20 0a 2a 2a 20 74 (or up to .** t
afea0 68 65 20 66 69 72 73 74 20 30 78 30 30 2c 20 77 he first 0x00, w
afeb0 68 69 63 68 65 76 65 72 20 63 6f 6d 65 73 20 66 hichever comes f
afec0 69 72 73 74 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 irst)..*/.SQLITE
afed0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
afee0 69 74 65 33 55 74 66 38 43 68 61 72 4c 65 6e 28 ite3Utf8CharLen(
afef0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49 6e 2c const char *zIn,
aff00 20 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 69 int nByte){. i
aff10 6e 74 20 72 20 3d 20 30 3b 0a 20 20 63 6f 6e 73 nt r = 0;. cons
aff20 74 20 75 38 20 2a 7a 20 3d 20 28 63 6f 6e 73 74 t u8 *z = (const
aff30 20 75 38 2a 29 7a 49 6e 3b 0a 20 20 63 6f 6e 73 u8*)zIn;. cons
aff40 74 20 75 38 20 2a 7a 54 65 72 6d 3b 0a 20 20 69 t u8 *zTerm;. i
aff50 66 28 20 6e 42 79 74 65 3e 3d 30 20 29 7b 0a 20 f( nByte>=0 ){.
aff60 20 20 20 7a 54 65 72 6d 20 3d 20 26 7a 5b 6e 42 zTerm = &z[nB
aff70 79 74 65 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 yte];. }else{.
aff80 20 20 20 7a 54 65 72 6d 20 3d 20 28 63 6f 6e 73 zTerm = (cons
aff90 74 20 75 38 2a 29 28 2d 31 29 3b 0a 20 20 7d 0a t u8*)(-1);. }.
affa0 20 20 61 73 73 65 72 74 28 20 7a 3c 3d 7a 54 65 assert( z<=zTe
affb0 72 6d 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 2a rm );. while( *
affc0 7a 21 3d 30 20 26 26 20 7a 3c 7a 54 65 72 6d 20 z!=0 && z<zTerm
affd0 29 7b 0a 20 20 20 20 53 51 4c 49 54 45 5f 53 4b ){. SQLITE_SK
affe0 49 50 5f 55 54 46 38 28 7a 29 3b 0a 20 20 20 20 IP_UTF8(z);.
afff0 72 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 r++;. }. retur
b0000 6e 20 72 3b 0a 7d 0a 0a 2f 2a 20 54 68 69 73 20 n r;.}../* This
b0010 74 65 73 74 20 66 75 6e 63 74 69 6f 6e 20 69 73 test function is
b0020 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 75 not currently u
b0030 73 65 64 20 62 79 20 74 68 65 20 61 75 74 6f 6d sed by the autom
b0040 61 74 65 64 20 74 65 73 74 2d 73 75 69 74 65 2e ated test-suite.
b0050 20 0a 2a 2a 20 48 65 6e 63 65 20 69 74 20 69 73 .** Hence it is
b0060 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 only available
b0070 69 6e 20 64 65 62 75 67 20 62 75 69 6c 64 73 2e in debug builds.
b0080 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 .*/.#if defined(
b0090 53 51 4c 49 54 45 5f 54 45 53 54 29 20 26 26 20 SQLITE_TEST) &&
b00a0 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 defined(SQLITE_D
b00b0 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20 54 72 61 6e EBUG)./*.** Tran
b00c0 73 6c 61 74 65 20 55 54 46 2d 38 20 74 6f 20 55 slate UTF-8 to U
b00d0 54 46 2d 38 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 TF-8..**.** This
b00e0 20 68 61 73 20 74 68 65 20 65 66 66 65 63 74 20 has the effect
b00f0 6f 66 20 6d 61 6b 69 6e 67 20 73 75 72 65 20 74 of making sure t
b0100 68 61 74 20 74 68 65 20 73 74 72 69 6e 67 20 69 hat the string i
b0110 73 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 0a 2a 2a s well-formed.**
b0120 20 55 54 46 2d 38 2e 20 20 4d 69 73 63 6f 64 65 UTF-8. Miscode
b0130 64 20 63 68 61 72 61 63 74 65 72 73 20 61 72 65 d characters are
b0140 20 72 65 6d 6f 76 65 64 2e 0a 2a 2a 0a 2a 2a 20 removed..**.**
b0150 54 68 65 20 74 72 61 6e 73 6c 61 74 69 6f 6e 20 The translation
b0160 69 73 20 64 6f 6e 65 20 69 6e 2d 70 6c 61 63 65 is done in-place
b0170 20 28 73 69 6e 63 65 20 69 74 20 69 73 20 69 6d (since it is im
b0180 70 6f 73 73 69 62 6c 65 20 66 6f 72 20 74 68 65 possible for the
b0190 0a 2a 2a 20 63 6f 72 72 65 63 74 20 55 54 46 2d .** correct UTF-
b01a0 38 20 65 6e 63 6f 64 69 6e 67 20 74 6f 20 62 65 8 encoding to be
b01b0 20 6c 6f 6e 67 65 72 20 74 68 61 6e 20 61 20 6d longer than a m
b01c0 61 6c 66 6f 72 6d 65 64 20 65 6e 63 6f 64 69 6e alformed encodin
b01d0 67 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 g)..*/.SQLITE_PR
b01e0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
b01f0 33 55 74 66 38 54 6f 38 28 75 6e 73 69 67 6e 65 3Utf8To8(unsigne
b0200 64 20 63 68 61 72 20 2a 7a 49 6e 29 7b 0a 20 20 d char *zIn){.
b0210 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a unsigned char *z
b0220 4f 75 74 20 3d 20 7a 49 6e 3b 0a 20 20 75 6e 73 Out = zIn;. uns
b0230 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 53 74 61 igned char *zSta
b0240 72 74 20 3d 20 7a 49 6e 3b 0a 20 20 75 33 32 20 rt = zIn;. u32
b0250 63 3b 0a 0a 20 20 77 68 69 6c 65 28 20 7a 49 6e c;.. while( zIn
b0260 5b 30 5d 20 29 7b 0a 20 20 20 20 63 20 3d 20 73 [0] ){. c = s
b0270 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a qlite3Utf8Read(z
b0280 49 6e 2c 20 28 63 6f 6e 73 74 20 75 38 2a 2a 29 In, (const u8**)
b0290 26 7a 49 6e 29 3b 0a 20 20 20 20 69 66 28 20 63 &zIn);. if( c
b02a0 21 3d 30 78 66 66 66 64 20 29 7b 0a 20 20 20 20 !=0xfffd ){.
b02b0 20 20 57 52 49 54 45 5f 55 54 46 38 28 7a 4f 75 WRITE_UTF8(zOu
b02c0 74 2c 20 63 29 3b 0a 20 20 20 20 7d 0a 20 20 7d t, c);. }. }
b02d0 0a 20 20 2a 7a 4f 75 74 20 3d 20 30 3b 0a 20 20 . *zOut = 0;.
b02e0 72 65 74 75 72 6e 20 28 69 6e 74 29 28 7a 4f 75 return (int)(zOu
b02f0 74 20 2d 20 7a 53 74 61 72 74 29 3b 0a 7d 0a 23 t - zStart);.}.#
b0300 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 endif..#ifndef S
b0310 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 QLITE_OMIT_UTF16
b0320 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 ./*.** Convert a
b0330 20 55 54 46 2d 31 36 20 73 74 72 69 6e 67 20 69 UTF-16 string i
b0340 6e 20 74 68 65 20 6e 61 74 69 76 65 20 65 6e 63 n the native enc
b0350 6f 64 69 6e 67 20 69 6e 74 6f 20 61 20 55 54 46 oding into a UTF
b0360 2d 38 20 73 74 72 69 6e 67 2e 0a 2a 2a 20 4d 65 -8 string..** Me
b0370 6d 6f 72 79 20 74 6f 20 68 6f 6c 64 20 74 68 65 mory to hold the
b0380 20 55 54 46 2d 38 20 73 74 72 69 6e 67 20 69 73 UTF-8 string is
b0390 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 obtained from s
b03a0 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 20 61 6e qlite3_malloc an
b03b0 64 20 6d 75 73 74 0a 2a 2a 20 62 65 20 66 72 65 d must.** be fre
b03c0 65 64 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e ed by the callin
b03d0 67 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a g function..**.*
b03e0 2a 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e * NULL is return
b03f0 65 64 20 69 66 20 74 68 65 72 65 20 69 73 20 61 ed if there is a
b0400 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 n allocation err
b0410 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 or..*/.SQLITE_PR
b0420 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 IVATE char *sqli
b0430 74 65 33 55 74 66 31 36 74 6f 38 28 73 71 6c 69 te3Utf16to8(sqli
b0440 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 76 te3 *db, const v
b0450 6f 69 64 20 2a 7a 2c 20 69 6e 74 20 6e 42 79 74 oid *z, int nByt
b0460 65 29 7b 0a 20 20 4d 65 6d 20 6d 3b 0a 20 20 6d e){. Mem m;. m
b0470 65 6d 73 65 74 28 26 6d 2c 20 30 2c 20 73 69 7a emset(&m, 0, siz
b0480 65 6f 66 28 6d 29 29 3b 0a 20 20 6d 2e 64 62 20 eof(m));. m.db
b0490 3d 20 64 62 3b 0a 20 20 73 71 6c 69 74 65 33 56 = db;. sqlite3V
b04a0 64 62 65 4d 65 6d 53 65 74 53 74 72 28 26 6d 2c dbeMemSetStr(&m,
b04b0 20 7a 2c 20 6e 42 79 74 65 2c 20 53 51 4c 49 54 z, nByte, SQLIT
b04c0 45 5f 55 54 46 31 36 4e 41 54 49 56 45 2c 20 53 E_UTF16NATIVE, S
b04d0 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 QLITE_STATIC);.
b04e0 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e sqlite3VdbeChan
b04f0 67 65 45 6e 63 6f 64 69 6e 67 28 26 6d 2c 20 53 geEncoding(&m, S
b0500 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a 20 20 69 QLITE_UTF8);. i
b0510 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 f( db->mallocFai
b0520 6c 65 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 led ){. sqlit
b0530 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 e3VdbeMemRelease
b0540 28 26 6d 29 3b 0a 20 20 20 20 6d 2e 7a 20 3d 20 (&m);. m.z =
b0550 30 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 0;. }. assert(
b0560 20 28 6d 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f (m.flags & MEM_
b0570 54 65 72 6d 29 21 3d 30 20 7c 7c 20 64 62 2d 3e Term)!=0 || db->
b0580 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a mallocFailed );.
b0590 20 20 61 73 73 65 72 74 28 20 28 6d 2e 66 6c 61 assert( (m.fla
b05a0 67 73 20 26 20 4d 45 4d 5f 53 74 72 29 21 3d 30 gs & MEM_Str)!=0
b05b0 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 || db->mallocFa
b05c0 69 6c 65 64 20 29 3b 0a 20 20 72 65 74 75 72 6e iled );. return
b05d0 20 28 6d 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f (m.flags & MEM_
b05e0 44 79 6e 29 21 3d 30 20 3f 20 6d 2e 7a 20 3a 20 Dyn)!=0 ? m.z :
b05f0 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 sqlite3DbStrDup(
b0600 64 62 2c 20 6d 2e 7a 29 3b 0a 7d 0a 0a 2f 2a 0a db, m.z);.}../*.
b0610 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20 55 54 46 ** Convert a UTF
b0620 2d 38 20 73 74 72 69 6e 67 20 74 6f 20 74 68 65 -8 string to the
b0630 20 55 54 46 2d 31 36 20 65 6e 63 6f 64 69 6e 67 UTF-16 encoding
b0640 20 73 70 65 63 69 66 69 65 64 20 62 79 20 70 61 specified by pa
b0650 72 61 6d 65 74 65 72 0a 2a 2a 20 65 6e 63 2e 20 rameter.** enc.
b0660 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 A pointer to the
b0670 20 6e 65 77 20 73 74 72 69 6e 67 20 69 73 20 72 new string is r
b0680 65 74 75 72 6e 65 64 2c 20 61 6e 64 20 74 68 65 eturned, and the
b0690 20 76 61 6c 75 65 20 6f 66 20 2a 70 6e 4f 75 74 value of *pnOut
b06a0 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 74 68 .** is set to th
b06b0 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 e length of the
b06c0 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20 returned string
b06d0 69 6e 20 62 79 74 65 73 2e 20 54 68 65 20 63 61 in bytes. The ca
b06e0 6c 6c 20 73 68 6f 75 6c 64 0a 2a 2a 20 61 72 72 ll should.** arr
b06f0 61 6e 67 65 20 74 6f 20 63 61 6c 6c 20 73 71 6c ange to call sql
b0700 69 74 65 33 44 62 46 72 65 65 28 29 20 6f 6e 20 ite3DbFree() on
b0710 74 68 65 20 72 65 74 75 72 6e 65 64 20 70 6f 69 the returned poi
b0720 6e 74 65 72 20 77 68 65 6e 20 69 74 20 69 73 0a nter when it is.
b0730 2a 2a 20 6e 6f 20 6c 6f 6e 67 65 72 20 72 65 71 ** no longer req
b0740 75 69 72 65 64 2e 0a 2a 2a 20 0a 2a 2a 20 49 66 uired..** .** If
b0750 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 a malloc failur
b0760 65 20 6f 63 63 75 72 73 2c 20 4e 55 4c 4c 20 69 e occurs, NULL i
b0770 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 74 s returned and t
b0780 68 65 20 64 62 2e 6d 61 6c 6c 6f 63 46 61 69 6c he db.mallocFail
b0790 65 64 0a 2a 2a 20 66 6c 61 67 20 73 65 74 2e 0a ed.** flag set..
b07a0 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
b07b0 5f 45 4e 41 42 4c 45 5f 53 54 41 54 32 0a 53 51 _ENABLE_STAT2.SQ
b07c0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 LITE_PRIVATE cha
b07d0 72 20 2a 73 71 6c 69 74 65 33 55 74 66 38 74 6f r *sqlite3Utf8to
b07e0 31 36 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 16(sqlite3 *db,
b07f0 75 38 20 65 6e 63 2c 20 63 68 61 72 20 2a 7a 2c u8 enc, char *z,
b0800 20 69 6e 74 20 6e 2c 20 69 6e 74 20 2a 70 6e 4f int n, int *pnO
b0810 75 74 29 7b 0a 20 20 4d 65 6d 20 6d 3b 0a 20 20 ut){. Mem m;.
b0820 6d 65 6d 73 65 74 28 26 6d 2c 20 30 2c 20 73 69 memset(&m, 0, si
b0830 7a 65 6f 66 28 6d 29 29 3b 0a 20 20 6d 2e 64 62 zeof(m));. m.db
b0840 20 3d 20 64 62 3b 0a 20 20 73 71 6c 69 74 65 33 = db;. sqlite3
b0850 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 26 6d VdbeMemSetStr(&m
b0860 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55 , z, n, SQLITE_U
b0870 54 46 38 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 TF8, SQLITE_STAT
b0880 49 43 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 IC);. if( sqlit
b0890 65 33 56 64 62 65 4d 65 6d 54 72 61 6e 73 6c 61 e3VdbeMemTransla
b08a0 74 65 28 26 6d 2c 20 65 6e 63 29 20 29 7b 0a 20 te(&m, enc) ){.
b08b0 20 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6d assert( db->m
b08c0 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 allocFailed );.
b08d0 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
b08e0 0a 20 20 61 73 73 65 72 74 28 20 6d 2e 7a 3d 3d . assert( m.z==
b08f0 6d 2e 7a 4d 61 6c 6c 6f 63 20 29 3b 0a 20 20 2a m.zMalloc );. *
b0900 70 6e 4f 75 74 20 3d 20 6d 2e 6e 3b 0a 20 20 72 pnOut = m.n;. r
b0910 65 74 75 72 6e 20 6d 2e 7a 3b 0a 7d 0a 23 65 6e eturn m.z;.}.#en
b0920 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 70 5a 20 69 73 dif../*.** pZ is
b0930 20 61 20 55 54 46 2d 31 36 20 65 6e 63 6f 64 65 a UTF-16 encode
b0940 64 20 75 6e 69 63 6f 64 65 20 73 74 72 69 6e 67 d unicode string
b0950 20 61 74 20 6c 65 61 73 74 20 6e 43 68 61 72 20 at least nChar
b0960 63 68 61 72 61 63 74 65 72 73 20 6c 6f 6e 67 2e characters long.
b0970 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e .** Return the n
b0980 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 umber of bytes i
b0990 6e 20 74 68 65 20 66 69 72 73 74 20 6e 43 68 61 n the first nCha
b09a0 72 20 75 6e 69 63 6f 64 65 20 63 68 61 72 61 63 r unicode charac
b09b0 74 65 72 73 0a 2a 2a 20 69 6e 20 70 5a 2e 20 20 ters.** in pZ.
b09c0 6e 43 68 61 72 20 6d 75 73 74 20 62 65 20 6e 6f nChar must be no
b09d0 6e 2d 6e 65 67 61 74 69 76 65 2e 0a 2a 2f 0a 53 n-negative..*/.S
b09e0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
b09f0 74 20 73 71 6c 69 74 65 33 55 74 66 31 36 42 79 t sqlite3Utf16By
b0a00 74 65 4c 65 6e 28 63 6f 6e 73 74 20 76 6f 69 64 teLen(const void
b0a10 20 2a 7a 49 6e 2c 20 69 6e 74 20 6e 43 68 61 72 *zIn, int nChar
b0a20 29 7b 0a 20 20 69 6e 74 20 63 3b 0a 20 20 75 6e ){. int c;. un
b0a30 73 69 67 6e 65 64 20 63 68 61 72 20 63 6f 6e 73 signed char cons
b0a40 74 20 2a 7a 20 3d 20 7a 49 6e 3b 0a 20 20 69 6e t *z = zIn;. in
b0a50 74 20 6e 20 3d 20 30 3b 0a 20 20 69 66 28 20 53 t n = 0;. if( S
b0a60 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 QLITE_UTF16NATIV
b0a70 45 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 42 E==SQLITE_UTF16B
b0a80 45 20 29 7b 0a 20 20 20 20 2f 2a 20 55 73 69 6e E ){. /* Usin
b0a90 67 20 61 6e 20 22 69 66 20 28 53 51 4c 49 54 45 g an "if (SQLITE
b0aa0 5f 55 54 46 31 36 4e 41 54 49 56 45 3d 3d 53 51 _UTF16NATIVE==SQ
b0ab0 4c 49 54 45 5f 55 54 46 31 36 42 45 29 22 20 63 LITE_UTF16BE)" c
b0ac0 6f 6e 73 74 72 75 63 74 20 68 65 72 65 0a 20 20 onstruct here.
b0ad0 20 20 2a 2a 20 61 6e 64 20 69 6e 20 6f 74 68 65 ** and in othe
b0ae0 72 20 70 61 72 74 73 20 6f 66 20 74 68 69 73 20 r parts of this
b0af0 66 69 6c 65 20 6d 65 61 6e 73 20 74 68 61 74 20 file means that
b0b00 61 74 20 6f 6e 65 20 62 72 61 6e 63 68 20 77 69 at one branch wi
b0b10 6c 6c 0a 20 20 20 20 2a 2a 20 6e 6f 74 20 62 65 ll. ** not be
b0b20 20 63 6f 76 65 72 65 64 20 62 79 20 63 6f 76 65 covered by cove
b0b30 72 61 67 65 20 74 65 73 74 69 6e 67 20 6f 6e 20 rage testing on
b0b40 61 6e 79 20 73 69 6e 67 6c 65 20 68 6f 73 74 2e any single host.
b0b50 20 42 75 74 20 63 6f 76 65 72 61 67 65 0a 20 20 But coverage.
b0b60 20 20 2a 2a 20 77 69 6c 6c 20 62 65 20 63 6f 6d ** will be com
b0b70 70 6c 65 74 65 20 69 66 20 74 68 65 20 74 65 73 plete if the tes
b0b80 74 73 20 61 72 65 20 72 75 6e 20 6f 6e 20 62 6f ts are run on bo
b0b90 74 68 20 61 20 6c 69 74 74 6c 65 2d 65 6e 64 69 th a little-endi
b0ba0 61 6e 20 61 6e 64 20 0a 20 20 20 20 2a 2a 20 62 an and . ** b
b0bb0 69 67 2d 65 6e 64 69 61 6e 20 68 6f 73 74 2e 20 ig-endian host.
b0bc0 42 65 63 61 75 73 65 20 62 6f 74 68 20 74 68 65 Because both the
b0bd0 20 55 54 46 31 36 4e 41 54 49 56 45 20 61 6e 64 UTF16NATIVE and
b0be0 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 0a SQLITE_UTF16BE.
b0bf0 20 20 20 20 2a 2a 20 6d 61 63 72 6f 73 20 61 72 ** macros ar
b0c00 65 20 63 6f 6e 73 74 61 6e 74 20 61 74 20 63 6f e constant at co
b0c10 6d 70 69 6c 65 20 74 69 6d 65 20 74 68 65 20 63 mpile time the c
b0c20 6f 6d 70 69 6c 65 72 20 63 61 6e 20 64 65 74 65 ompiler can dete
b0c30 72 6d 69 6e 65 0a 20 20 20 20 2a 2a 20 77 68 69 rmine. ** whi
b0c40 63 68 20 62 72 61 6e 63 68 20 77 69 6c 6c 20 62 ch branch will b
b0c50 65 20 66 6f 6c 6c 6f 77 65 64 2e 20 49 74 20 69 e followed. It i
b0c60 73 20 74 68 65 72 65 66 6f 72 65 20 61 73 73 75 s therefore assu
b0c70 6d 65 64 20 74 68 61 74 20 6e 6f 20 72 75 6e 74 med that no runt
b0c80 69 6d 65 0a 20 20 20 20 2a 2a 20 70 65 6e 61 6c ime. ** penal
b0c90 74 79 20 69 73 20 70 61 69 64 20 66 6f 72 20 74 ty is paid for t
b0ca0 68 69 73 20 22 69 66 22 20 73 74 61 74 65 6d 65 his "if" stateme
b0cb0 6e 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 77 nt.. */. w
b0cc0 68 69 6c 65 28 20 6e 3c 6e 43 68 61 72 20 29 7b hile( n<nChar ){
b0cd0 0a 20 20 20 20 20 20 52 45 41 44 5f 55 54 46 31 . READ_UTF1
b0ce0 36 42 45 28 7a 2c 20 63 29 3b 0a 20 20 20 20 20 6BE(z, c);.
b0cf0 20 6e 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 n++;. }. }e
b0d00 6c 73 65 7b 0a 20 20 20 20 77 68 69 6c 65 28 20 lse{. while(
b0d10 6e 3c 6e 43 68 61 72 20 29 7b 0a 20 20 20 20 20 n<nChar ){.
b0d20 20 52 45 41 44 5f 55 54 46 31 36 4c 45 28 7a 2c READ_UTF16LE(z,
b0d30 20 63 29 3b 0a 20 20 20 20 20 20 6e 2b 2b 3b 0a c);. n++;.
b0d40 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
b0d50 72 6e 20 28 69 6e 74 29 28 7a 2d 28 75 6e 73 69 rn (int)(z-(unsi
b0d60 67 6e 65 64 20 63 68 61 72 20 63 6f 6e 73 74 20 gned char const
b0d70 2a 29 7a 49 6e 29 3b 0a 7d 0a 0a 23 69 66 20 64 *)zIn);.}..#if d
b0d80 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 efined(SQLITE_TE
b0d90 53 54 29 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 ST)./*.** This r
b0da0 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 outine is called
b0db0 20 66 72 6f 6d 20 74 68 65 20 54 43 4c 20 74 65 from the TCL te
b0dc0 73 74 20 66 75 6e 63 74 69 6f 6e 20 22 74 72 61 st function "tra
b0dd0 6e 73 6c 61 74 65 5f 73 65 6c 66 74 65 73 74 22 nslate_selftest"
b0de0 2e 0a 2a 2a 20 49 74 20 63 68 65 63 6b 73 20 74 ..** It checks t
b0df0 68 61 74 20 74 68 65 20 70 72 69 6d 69 74 69 76 hat the primitiv
b0e00 65 73 20 66 6f 72 20 73 65 72 69 61 6c 69 7a 69 es for serializi
b0e10 6e 67 20 61 6e 64 20 64 65 73 65 72 69 61 6c 69 ng and deseriali
b0e20 7a 69 6e 67 0a 2a 2a 20 63 68 61 72 61 63 74 65 zing.** characte
b0e30 72 73 20 69 6e 20 65 61 63 68 20 65 6e 63 6f 64 rs in each encod
b0e40 69 6e 67 20 61 72 65 20 69 6e 76 65 72 73 65 73 ing are inverses
b0e50 20 6f 66 20 65 61 63 68 20 6f 74 68 65 72 2e 0a of each other..
b0e60 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
b0e70 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 55 74 E void sqlite3Ut
b0e80 66 53 65 6c 66 54 65 73 74 28 76 6f 69 64 29 7b fSelfTest(void){
b0e90 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 . unsigned int
b0ea0 69 2c 20 74 3b 0a 20 20 75 6e 73 69 67 6e 65 64 i, t;. unsigned
b0eb0 20 63 68 61 72 20 7a 42 75 66 5b 32 30 5d 3b 0a char zBuf[20];.
b0ec0 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 unsigned char
b0ed0 2a 7a 3b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 75 *z;. int n;. u
b0ee0 6e 73 69 67 6e 65 64 20 69 6e 74 20 63 3b 0a 0a nsigned int c;..
b0ef0 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 30 78 30 for(i=0; i<0x0
b0f00 30 31 31 30 30 30 30 3b 20 69 2b 2b 29 7b 0a 20 0110000; i++){.
b0f10 20 20 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 20 z = zBuf;.
b0f20 20 57 52 49 54 45 5f 55 54 46 38 28 7a 2c 20 69 WRITE_UTF8(z, i
b0f30 29 3b 0a 20 20 20 20 6e 20 3d 20 28 69 6e 74 29 );. n = (int)
b0f40 28 7a 2d 7a 42 75 66 29 3b 0a 20 20 20 20 61 73 (z-zBuf);. as
b0f50 73 65 72 74 28 20 6e 3e 30 20 26 26 20 6e 3c 3d sert( n>0 && n<=
b0f60 34 20 29 3b 0a 20 20 20 20 7a 5b 30 5d 20 3d 20 4 );. z[0] =
b0f70 30 3b 0a 20 20 20 20 7a 20 3d 20 7a 42 75 66 3b 0;. z = zBuf;
b0f80 0a 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33 . c = sqlite3
b0f90 55 74 66 38 52 65 61 64 28 7a 2c 20 28 63 6f 6e Utf8Read(z, (con
b0fa0 73 74 20 75 38 2a 2a 29 26 7a 29 3b 0a 20 20 20 st u8**)&z);.
b0fb0 20 74 20 3d 20 69 3b 0a 20 20 20 20 69 66 28 20 t = i;. if(
b0fc0 69 3e 3d 30 78 44 38 30 30 20 26 26 20 69 3c 3d i>=0xD800 && i<=
b0fd0 30 78 44 46 46 46 20 29 20 74 20 3d 20 30 78 46 0xDFFF ) t = 0xF
b0fe0 46 46 44 3b 0a 20 20 20 20 69 66 28 20 28 69 26 FFD;. if( (i&
b0ff0 30 78 46 46 46 46 46 46 46 45 29 3d 3d 30 78 46 0xFFFFFFFE)==0xF
b1000 46 46 45 20 29 20 74 20 3d 20 30 78 46 46 46 44 FFE ) t = 0xFFFD
b1010 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 63 3d ;. assert( c=
b1020 3d 74 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 =t );. assert
b1030 28 20 28 7a 2d 7a 42 75 66 29 3d 3d 6e 20 29 3b ( (z-zBuf)==n );
b1040 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 . }. for(i=0;
b1050 69 3c 30 78 30 30 31 31 30 30 30 30 3b 20 69 2b i<0x00110000; i+
b1060 2b 29 7b 0a 20 20 20 20 69 66 28 20 69 3e 3d 30 +){. if( i>=0
b1070 78 44 38 30 30 20 26 26 20 69 3c 30 78 45 30 30 xD800 && i<0xE00
b1080 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 0 ) continue;.
b1090 20 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 20 20 z = zBuf;.
b10a0 57 52 49 54 45 5f 55 54 46 31 36 4c 45 28 7a 2c WRITE_UTF16LE(z,
b10b0 20 69 29 3b 0a 20 20 20 20 6e 20 3d 20 28 69 6e i);. n = (in
b10c0 74 29 28 7a 2d 7a 42 75 66 29 3b 0a 20 20 20 20 t)(z-zBuf);.
b10d0 61 73 73 65 72 74 28 20 6e 3e 30 20 26 26 20 6e assert( n>0 && n
b10e0 3c 3d 34 20 29 3b 0a 20 20 20 20 7a 5b 30 5d 20 <=4 );. z[0]
b10f0 3d 20 30 3b 0a 20 20 20 20 7a 20 3d 20 7a 42 75 = 0;. z = zBu
b1100 66 3b 0a 20 20 20 20 52 45 41 44 5f 55 54 46 31 f;. READ_UTF1
b1110 36 4c 45 28 7a 2c 20 63 29 3b 0a 20 20 20 20 61 6LE(z, c);. a
b1120 73 73 65 72 74 28 20 63 3d 3d 69 20 29 3b 0a 20 ssert( c==i );.
b1130 20 20 20 61 73 73 65 72 74 28 20 28 7a 2d 7a 42 assert( (z-zB
b1140 75 66 29 3d 3d 6e 20 29 3b 0a 20 20 7d 0a 20 20 uf)==n );. }.
b1150 66 6f 72 28 69 3d 30 3b 20 69 3c 30 78 30 30 31 for(i=0; i<0x001
b1160 31 30 30 30 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 10000; i++){.
b1170 20 69 66 28 20 69 3e 3d 30 78 44 38 30 30 20 26 if( i>=0xD800 &
b1180 26 20 69 3c 30 78 45 30 30 30 20 29 20 63 6f 6e & i<0xE000 ) con
b1190 74 69 6e 75 65 3b 0a 20 20 20 20 7a 20 3d 20 7a tinue;. z = z
b11a0 42 75 66 3b 0a 20 20 20 20 57 52 49 54 45 5f 55 Buf;. WRITE_U
b11b0 54 46 31 36 42 45 28 7a 2c 20 69 29 3b 0a 20 20 TF16BE(z, i);.
b11c0 20 20 6e 20 3d 20 28 69 6e 74 29 28 7a 2d 7a 42 n = (int)(z-zB
b11d0 75 66 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 uf);. assert(
b11e0 20 6e 3e 30 20 26 26 20 6e 3c 3d 34 20 29 3b 0a n>0 && n<=4 );.
b11f0 20 20 20 20 7a 5b 30 5d 20 3d 20 30 3b 0a 20 20 z[0] = 0;.
b1200 20 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 20 20 z = zBuf;.
b1210 52 45 41 44 5f 55 54 46 31 36 42 45 28 7a 2c 20 READ_UTF16BE(z,
b1220 63 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 c);. assert(
b1230 63 3d 3d 69 20 29 3b 0a 20 20 20 20 61 73 73 65 c==i );. asse
b1240 72 74 28 20 28 7a 2d 7a 42 75 66 29 3d 3d 6e 20 rt( (z-zBuf)==n
b1250 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 );. }.}.#endif
b1260 2f 2a 20 53 51 4c 49 54 45 5f 54 45 53 54 20 2a /* SQLITE_TEST *
b1270 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 /.#endif /* SQLI
b1280 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f TE_OMIT_UTF16 */
b1290 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
b12a0 2a 20 45 6e 64 20 6f 66 20 75 74 66 2e 63 20 2a * End of utf.c *
b12b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b12c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b12d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b12e0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
b12f0 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 75 74 69 * Begin file uti
b1300 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a l.c ************
b1310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b1320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b1330 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 /./*.** 2001 Sep
b1340 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 tember 15.**.**
b1350 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c The author discl
b1360 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 aims copyright t
b1370 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f o this source co
b1380 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 de. In place of
b1390 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 .** a legal noti
b13a0 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c ce, here is a bl
b13b0 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 essing:.**.**
b13c0 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 May you do good
b13d0 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a and not evil..*
b13e0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e * May you fin
b13f0 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f d forgiveness fo
b1400 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 r yourself and f
b1410 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a orgive others..*
b1420 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 * May you sha
b1430 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 re freely, never
b1440 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 taking more tha
b1450 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a n you give..**.*
b1460 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b1470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b1480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b1490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b14a0 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 55 74 69 6c ********.** Util
b14b0 69 74 79 20 66 75 6e 63 74 69 6f 6e 73 20 75 73 ity functions us
b14c0 65 64 20 74 68 72 6f 75 67 68 6f 75 74 20 73 71 ed throughout sq
b14d0 6c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 lite..**.** This
b14e0 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 66 file contains f
b14f0 75 6e 63 74 69 6f 6e 73 20 66 6f 72 20 61 6c 6c unctions for all
b1500 6f 63 61 74 69 6e 67 20 6d 65 6d 6f 72 79 2c 20 ocating memory,
b1510 63 6f 6d 70 61 72 69 6e 67 0a 2a 2a 20 73 74 72 comparing.** str
b1520 69 6e 67 73 2c 20 61 6e 64 20 73 74 75 66 66 20 ings, and stuff
b1530 6c 69 6b 65 20 74 68 61 74 2e 0a 2a 2a 0a 2a 2f like that..**.*/
b1540 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48 .#ifdef SQLITE_H
b1550 41 56 45 5f 49 53 4e 41 4e 0a 23 20 69 6e 63 6c AVE_ISNAN.# incl
b1560 75 64 65 20 3c 6d 61 74 68 2e 68 3e 0a 23 65 6e ude <math.h>.#en
b1570 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 74 69 dif../*.** Routi
b1580 6e 65 20 6e 65 65 64 65 64 20 74 6f 20 73 75 70 ne needed to sup
b1590 70 6f 72 74 20 74 68 65 20 74 65 73 74 63 61 73 port the testcas
b15a0 65 28 29 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 23 69 e() macro..*/.#i
b15b0 66 64 65 66 20 53 51 4c 49 54 45 5f 43 4f 56 45 fdef SQLITE_COVE
b15c0 52 41 47 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 RAGE_TEST.SQLITE
b15d0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
b15e0 6c 69 74 65 33 43 6f 76 65 72 61 67 65 28 69 6e lite3Coverage(in
b15f0 74 20 78 29 7b 0a 20 20 73 74 61 74 69 63 20 69 t x){. static i
b1600 6e 74 20 64 75 6d 6d 79 20 3d 20 30 3b 0a 20 20 nt dummy = 0;.
b1610 64 75 6d 6d 79 20 2b 3d 20 78 3b 0a 7d 0a 23 65 dummy += x;.}.#e
b1620 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 ndif../*.** Retu
b1630 72 6e 20 74 72 75 65 20 69 66 20 74 68 65 20 66 rn true if the f
b1640 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 loating point va
b1650 6c 75 65 20 69 73 20 4e 6f 74 20 61 20 4e 75 6d lue is Not a Num
b1660 62 65 72 20 28 4e 61 4e 29 2e 0a 2a 2a 0a 2a 2a ber (NaN)..**.**
b1670 20 55 73 65 20 74 68 65 20 6d 61 74 68 20 6c 69 Use the math li
b1680 62 72 61 72 79 20 69 73 6e 61 6e 28 29 20 66 75 brary isnan() fu
b1690 6e 63 74 69 6f 6e 20 69 66 20 63 6f 6d 70 69 6c nction if compil
b16a0 65 64 20 77 69 74 68 20 53 51 4c 49 54 45 5f 48 ed with SQLITE_H
b16b0 41 56 45 5f 49 53 4e 41 4e 2e 0a 2a 2a 20 4f 74 AVE_ISNAN..** Ot
b16c0 68 65 72 77 69 73 65 2c 20 77 65 20 68 61 76 65 herwise, we have
b16d0 20 6f 75 72 20 6f 77 6e 20 69 6d 70 6c 65 6d 65 our own impleme
b16e0 6e 74 61 74 69 6f 6e 20 74 68 61 74 20 77 6f 72 ntation that wor
b16f0 6b 73 20 6f 6e 20 6d 6f 73 74 20 73 79 73 74 65 ks on most syste
b1700 6d 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ms..*/.SQLITE_PR
b1710 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
b1720 33 49 73 4e 61 4e 28 64 6f 75 62 6c 65 20 78 29 3IsNaN(double x)
b1730 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 2f 2a {. int rc; /*
b1740 20 54 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 The value retur
b1750 6e 20 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 n */.#if !define
b1760 64 28 53 51 4c 49 54 45 5f 48 41 56 45 5f 49 53 d(SQLITE_HAVE_IS
b1770 4e 41 4e 29 0a 20 20 2f 2a 0a 20 20 2a 2a 20 53 NAN). /*. ** S
b1780 79 73 74 65 6d 73 20 74 68 61 74 20 73 75 70 70 ystems that supp
b1790 6f 72 74 20 74 68 65 20 69 73 6e 61 6e 28 29 20 ort the isnan()
b17a0 6c 69 62 72 61 72 79 20 66 75 6e 63 74 69 6f 6e library function
b17b0 20 73 68 6f 75 6c 64 20 70 72 6f 62 61 62 6c 79 should probably
b17c0 0a 20 20 2a 2a 20 6d 61 6b 65 20 75 73 65 20 6f . ** make use o
b17d0 66 20 69 74 20 62 79 20 63 6f 6d 70 69 6c 69 6e f it by compilin
b17e0 67 20 77 69 74 68 20 2d 44 53 51 4c 49 54 45 5f g with -DSQLITE_
b17f0 48 41 56 45 5f 49 53 4e 41 4e 2e 20 20 42 75 74 HAVE_ISNAN. But
b1800 20 77 65 20 68 61 76 65 0a 20 20 2a 2a 20 66 6f we have. ** fo
b1810 75 6e 64 20 74 68 61 74 20 6d 61 6e 79 20 73 79 und that many sy
b1820 73 74 65 6d 73 20 64 6f 20 6e 6f 74 20 68 61 76 stems do not hav
b1830 65 20 61 20 77 6f 72 6b 69 6e 67 20 69 73 6e 61 e a working isna
b1840 6e 28 29 20 66 75 6e 63 74 69 6f 6e 20 73 6f 0a n() function so.
b1850 20 20 2a 2a 20 74 68 69 73 20 69 6d 70 6c 65 6d ** this implem
b1860 65 6e 74 61 74 69 6f 6e 20 69 73 20 70 72 6f 76 entation is prov
b1870 69 64 65 64 20 61 73 20 61 6e 20 61 6c 74 65 72 ided as an alter
b1880 6e 61 74 69 76 65 2e 0a 20 20 2a 2a 0a 20 20 2a native.. **. *
b1890 2a 20 54 68 69 73 20 4e 61 4e 20 74 65 73 74 20 * This NaN test
b18a0 73 6f 6d 65 74 69 6d 65 73 20 66 61 69 6c 73 20 sometimes fails
b18b0 69 66 20 63 6f 6d 70 69 6c 65 64 20 6f 6e 20 47 if compiled on G
b18c0 43 43 20 77 69 74 68 20 2d 66 66 61 73 74 2d 6d CC with -ffast-m
b18d0 61 74 68 2e 0a 20 20 2a 2a 20 4f 6e 20 74 68 65 ath.. ** On the
b18e0 20 6f 74 68 65 72 20 68 61 6e 64 2c 20 74 68 65 other hand, the
b18f0 20 75 73 65 20 6f 66 20 2d 66 66 61 73 74 2d 6d use of -ffast-m
b1900 61 74 68 20 63 6f 6d 65 73 20 77 69 74 68 20 74 ath comes with t
b1910 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 2a he following. *
b1920 2a 20 77 61 72 6e 69 6e 67 3a 0a 20 20 2a 2a 0a * warning:. **.
b1930 20 20 2a 2a 20 20 20 20 20 20 54 68 69 73 20 6f ** This o
b1940 70 74 69 6f 6e 20 5b 2d 66 66 61 73 74 2d 6d 61 ption [-ffast-ma
b1950 74 68 5d 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 th] should never
b1960 20 62 65 20 74 75 72 6e 65 64 20 6f 6e 20 62 79 be turned on by
b1970 20 61 6e 79 0a 20 20 2a 2a 20 20 20 20 20 20 2d any. ** -
b1980 4f 20 6f 70 74 69 6f 6e 20 73 69 6e 63 65 20 69 O option since i
b1990 74 20 63 61 6e 20 72 65 73 75 6c 74 20 69 6e 20 t can result in
b19a0 69 6e 63 6f 72 72 65 63 74 20 6f 75 74 70 75 74 incorrect output
b19b0 20 66 6f 72 20 70 72 6f 67 72 61 6d 73 0a 20 20 for programs.
b19c0 2a 2a 20 20 20 20 20 20 77 68 69 63 68 20 64 65 ** which de
b19d0 70 65 6e 64 20 6f 6e 20 61 6e 20 65 78 61 63 74 pend on an exact
b19e0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
b19f0 6f 66 20 49 45 45 45 20 6f 72 20 49 53 4f 20 0a of IEEE or ISO .
b1a00 20 20 2a 2a 20 20 20 20 20 20 72 75 6c 65 73 2f ** rules/
b1a10 73 70 65 63 69 66 69 63 61 74 69 6f 6e 73 20 66 specifications f
b1a20 6f 72 20 6d 61 74 68 20 66 75 6e 63 74 69 6f 6e or math function
b1a30 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 55 6e 64 s.. **. ** Und
b1a40 65 72 20 4d 53 56 43 2c 20 74 68 69 73 20 4e 61 er MSVC, this Na
b1a50 4e 20 74 65 73 74 20 6d 61 79 20 66 61 69 6c 20 N test may fail
b1a60 69 66 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 if compiled with
b1a70 20 61 20 66 6c 6f 61 74 69 6e 67 2d 0a 20 20 2a a floating-. *
b1a80 2a 20 70 6f 69 6e 74 20 70 72 65 63 69 73 69 6f * point precisio
b1a90 6e 20 6d 6f 64 65 20 6f 74 68 65 72 20 74 68 61 n mode other tha
b1aa0 6e 20 2f 66 70 3a 70 72 65 63 69 73 65 2e 20 20 n /fp:precise.
b1ab0 46 72 6f 6d 20 74 68 65 20 4d 53 44 4e 20 0a 20 From the MSDN .
b1ac0 20 2a 2a 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f ** documentatio
b1ad0 6e 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 n:. **. **
b1ae0 20 20 54 68 65 20 63 6f 6d 70 69 6c 65 72 20 5b The compiler [
b1af0 77 69 74 68 20 2f 66 70 3a 70 72 65 63 69 73 65 with /fp:precise
b1b00 5d 20 77 69 6c 6c 20 70 72 6f 70 65 72 6c 79 20 ] will properly
b1b10 68 61 6e 64 6c 65 20 63 6f 6d 70 61 72 69 73 6f handle compariso
b1b20 6e 73 20 0a 20 20 2a 2a 20 20 20 20 20 20 69 6e ns . ** in
b1b30 76 6f 6c 76 69 6e 67 20 4e 61 4e 2e 20 46 6f 72 volving NaN. For
b1b40 20 65 78 61 6d 70 6c 65 2c 20 78 20 21 3d 20 78 example, x != x
b1b50 20 65 76 61 6c 75 61 74 65 73 20 74 6f 20 74 72 evaluates to tr
b1b60 75 65 20 69 66 20 78 20 69 73 20 4e 61 4e 20 0a ue if x is NaN .
b1b70 20 20 2a 2a 20 20 20 20 20 20 2e 2e 2e 0a 20 20 ** ....
b1b80 2a 2f 0a 23 69 66 64 65 66 20 5f 5f 46 41 53 54 */.#ifdef __FAST
b1b90 5f 4d 41 54 48 5f 5f 0a 23 20 65 72 72 6f 72 20 _MATH__.# error
b1ba0 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 6f 74 20 SQLite will not
b1bb0 77 6f 72 6b 20 63 6f 72 72 65 63 74 6c 79 20 77 work correctly w
b1bc0 69 74 68 20 74 68 65 20 2d 66 66 61 73 74 2d 6d ith the -ffast-m
b1bd0 61 74 68 20 6f 70 74 69 6f 6e 20 6f 66 20 47 43 ath option of GC
b1be0 43 2e 0a 23 65 6e 64 69 66 0a 20 20 76 6f 6c 61 C..#endif. vola
b1bf0 74 69 6c 65 20 64 6f 75 62 6c 65 20 79 20 3d 20 tile double y =
b1c00 78 3b 0a 20 20 76 6f 6c 61 74 69 6c 65 20 64 6f x;. volatile do
b1c10 75 62 6c 65 20 7a 20 3d 20 79 3b 0a 20 20 72 63 uble z = y;. rc
b1c20 20 3d 20 28 79 21 3d 7a 29 3b 0a 23 65 6c 73 65 = (y!=z);.#else
b1c30 20 20 2f 2a 20 69 66 20 64 65 66 69 6e 65 64 28 /* if defined(
b1c40 53 51 4c 49 54 45 5f 48 41 56 45 5f 49 53 4e 41 SQLITE_HAVE_ISNA
b1c50 4e 29 20 2a 2f 0a 20 20 72 63 20 3d 20 69 73 6e N) */. rc = isn
b1c60 61 6e 28 78 29 3b 0a 23 65 6e 64 69 66 20 2f 2a an(x);.#endif /*
b1c70 20 53 51 4c 49 54 45 5f 48 41 56 45 5f 49 53 4e SQLITE_HAVE_ISN
b1c80 41 4e 20 2a 2f 0a 20 20 74 65 73 74 63 61 73 65 AN */. testcase
b1c90 28 20 72 63 20 29 3b 0a 20 20 72 65 74 75 72 6e ( rc );. return
b1ca0 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f rc;.}../*.** Co
b1cb0 6d 70 75 74 65 20 61 20 73 74 72 69 6e 67 20 6c mpute a string l
b1cc0 65 6e 67 74 68 20 74 68 61 74 20 69 73 20 6c 69 ength that is li
b1cd0 6d 69 74 65 64 20 74 6f 20 77 68 61 74 20 63 61 mited to what ca
b1ce0 6e 20 62 65 20 73 74 6f 72 65 64 20 69 6e 0a 2a n be stored in.*
b1cf0 2a 20 6c 6f 77 65 72 20 33 30 20 62 69 74 73 20 * lower 30 bits
b1d00 6f 66 20 61 20 33 32 2d 62 69 74 20 73 69 67 6e of a 32-bit sign
b1d10 65 64 20 69 6e 74 65 67 65 72 2e 0a 2a 2a 0a 2a ed integer..**.*
b1d20 2a 20 54 68 65 20 76 61 6c 75 65 20 72 65 74 75 * The value retu
b1d30 72 6e 65 64 20 77 69 6c 6c 20 6e 65 76 65 72 20 rned will never
b1d40 62 65 20 6e 65 67 61 74 69 76 65 2e 20 20 4e 6f be negative. No
b1d50 72 20 77 69 6c 6c 20 69 74 20 65 76 65 72 20 62 r will it ever b
b1d60 65 20 67 72 65 61 74 65 72 0a 2a 2a 20 74 68 61 e greater.** tha
b1d70 6e 20 74 68 65 20 61 63 74 75 61 6c 20 6c 65 6e n the actual len
b1d80 67 74 68 20 6f 66 20 74 68 65 20 73 74 72 69 6e gth of the strin
b1d90 67 2e 20 20 46 6f 72 20 76 65 72 79 20 6c 6f 6e g. For very lon
b1da0 67 20 73 74 72 69 6e 67 73 20 28 67 72 65 61 74 g strings (great
b1db0 65 72 0a 2a 2a 20 74 68 61 6e 20 31 47 69 42 29 er.** than 1GiB)
b1dc0 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 the value retur
b1dd0 6e 65 64 20 6d 69 67 68 74 20 62 65 20 6c 65 73 ned might be les
b1de0 73 20 74 68 61 6e 20 74 68 65 20 74 72 75 65 20 s than the true
b1df0 73 74 72 69 6e 67 20 6c 65 6e 67 74 68 2e 0a 2a string length..*
b1e00 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
b1e10 20 69 6e 74 20 73 71 6c 69 74 65 33 53 74 72 6c int sqlite3Strl
b1e20 65 6e 33 30 28 63 6f 6e 73 74 20 63 68 61 72 20 en30(const char
b1e30 2a 7a 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 *z){. const cha
b1e40 72 20 2a 7a 32 20 3d 20 7a 3b 0a 20 20 69 66 28 r *z2 = z;. if(
b1e50 20 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 z==0 ) return 0
b1e60 3b 0a 20 20 77 68 69 6c 65 28 20 2a 7a 32 20 29 ;. while( *z2 )
b1e70 7b 20 7a 32 2b 2b 3b 20 7d 0a 20 20 72 65 74 75 { z2++; }. retu
b1e80 72 6e 20 30 78 33 66 66 66 66 66 66 66 20 26 20 rn 0x3fffffff &
b1e90 28 69 6e 74 29 28 7a 32 20 2d 20 7a 29 3b 0a 7d (int)(z2 - z);.}
b1ea0 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 ../*.** Set the
b1eb0 6d 6f 73 74 20 72 65 63 65 6e 74 20 65 72 72 6f most recent erro
b1ec0 72 20 63 6f 64 65 20 61 6e 64 20 65 72 72 6f 72 r code and error
b1ed0 20 73 74 72 69 6e 67 20 66 6f 72 20 74 68 65 20 string for the
b1ee0 73 71 6c 69 74 65 0a 2a 2a 20 68 61 6e 64 6c 65 sqlite.** handle
b1ef0 20 22 64 62 22 2e 20 54 68 65 20 65 72 72 6f 72 "db". The error
b1f00 20 63 6f 64 65 20 69 73 20 73 65 74 20 74 6f 20 code is set to
b1f10 22 65 72 72 5f 63 6f 64 65 22 2e 0a 2a 2a 0a 2a "err_code"..**.*
b1f20 2a 20 49 66 20 69 74 20 69 73 20 6e 6f 74 20 4e * If it is not N
b1f30 55 4c 4c 2c 20 73 74 72 69 6e 67 20 7a 46 6f 72 ULL, string zFor
b1f40 6d 61 74 20 73 70 65 63 69 66 69 65 73 20 74 68 mat specifies th
b1f50 65 20 66 6f 72 6d 61 74 20 6f 66 20 74 68 65 0a e format of the.
b1f60 2a 2a 20 65 72 72 6f 72 20 73 74 72 69 6e 67 20 ** error string
b1f70 69 6e 20 74 68 65 20 73 74 79 6c 65 20 6f 66 20 in the style of
b1f80 74 68 65 20 70 72 69 6e 74 66 20 66 75 6e 63 74 the printf funct
b1f90 69 6f 6e 73 3a 20 54 68 65 20 66 6f 6c 6c 6f 77 ions: The follow
b1fa0 69 6e 67 0a 2a 2a 20 66 6f 72 6d 61 74 20 63 68 ing.** format ch
b1fb0 61 72 61 63 74 65 72 73 20 61 72 65 20 61 6c 6c aracters are all
b1fc0 6f 77 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 owed:.**.**
b1fd0 20 25 73 20 20 20 20 20 20 49 6e 73 65 72 74 20 %s Insert
b1fe0 61 20 73 74 72 69 6e 67 0a 2a 2a 20 20 20 20 20 a string.**
b1ff0 20 25 7a 20 20 20 20 20 20 41 20 73 74 72 69 6e %z A strin
b2000 67 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 g that should be
b2010 20 66 72 65 65 64 20 61 66 74 65 72 20 75 73 65 freed after use
b2020 0a 2a 2a 20 20 20 20 20 20 25 64 20 20 20 20 20 .** %d
b2030 20 49 6e 73 65 72 74 20 61 6e 20 69 6e 74 65 67 Insert an integ
b2040 65 72 0a 2a 2a 20 20 20 20 20 20 25 54 20 20 20 er.** %T
b2050 20 20 20 49 6e 73 65 72 74 20 61 20 74 6f 6b 65 Insert a toke
b2060 6e 0a 2a 2a 20 20 20 20 20 20 25 53 20 20 20 20 n.** %S
b2070 20 20 49 6e 73 65 72 74 20 74 68 65 20 66 69 72 Insert the fir
b2080 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20 61 20 st element of a
b2090 53 72 63 4c 69 73 74 0a 2a 2a 0a 2a 2a 20 7a 46 SrcList.**.** zF
b20a0 6f 72 6d 61 74 20 61 6e 64 20 61 6e 79 20 73 74 ormat and any st
b20b0 72 69 6e 67 20 74 6f 6b 65 6e 73 20 74 68 61 74 ring tokens that
b20c0 20 66 6f 6c 6c 6f 77 20 69 74 20 61 72 65 20 61 follow it are a
b20d0 73 73 75 6d 65 64 20 74 6f 20 62 65 0a 2a 2a 20 ssumed to be.**
b20e0 65 6e 63 6f 64 65 64 20 69 6e 20 55 54 46 2d 38 encoded in UTF-8
b20f0 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 63 6c 65 61 72 ..**.** To clear
b2100 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 the most recent
b2110 20 65 72 72 6f 72 20 66 6f 72 20 73 71 6c 69 74 error for sqlit
b2120 65 20 68 61 6e 64 6c 65 20 22 64 62 22 2c 20 73 e handle "db", s
b2130 71 6c 69 74 65 33 45 72 72 6f 72 0a 2a 2a 20 73 qlite3Error.** s
b2140 68 6f 75 6c 64 20 62 65 20 63 61 6c 6c 65 64 20 hould be called
b2150 77 69 74 68 20 65 72 72 5f 63 6f 64 65 20 73 65 with err_code se
b2160 74 20 74 6f 20 53 51 4c 49 54 45 5f 4f 4b 20 61 t to SQLITE_OK a
b2170 6e 64 20 7a 46 6f 72 6d 61 74 20 73 65 74 0a 2a nd zFormat set.*
b2180 2a 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 * to NULL..*/.SQ
b2190 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
b21a0 64 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 73 d sqlite3Error(s
b21b0 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 qlite3 *db, int
b21c0 65 72 72 5f 63 6f 64 65 2c 20 63 6f 6e 73 74 20 err_code, const
b21d0 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e char *zFormat, .
b21e0 2e 2e 29 7b 0a 20 20 69 66 28 20 64 62 20 26 26 ..){. if( db &&
b21f0 20 28 64 62 2d 3e 70 45 72 72 20 7c 7c 20 28 64 (db->pErr || (d
b2200 62 2d 3e 70 45 72 72 20 3d 20 73 71 6c 69 74 65 b->pErr = sqlite
b2210 33 56 61 6c 75 65 4e 65 77 28 64 62 29 29 21 3d 3ValueNew(db))!=
b2220 30 29 20 29 7b 0a 20 20 20 20 64 62 2d 3e 65 72 0) ){. db->er
b2230 72 43 6f 64 65 20 3d 20 65 72 72 5f 63 6f 64 65 rCode = err_code
b2240 3b 0a 20 20 20 20 69 66 28 20 7a 46 6f 72 6d 61 ;. if( zForma
b2250 74 20 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20 t ){. char
b2260 2a 7a 3b 0a 20 20 20 20 20 20 76 61 5f 6c 69 73 *z;. va_lis
b2270 74 20 61 70 3b 0a 20 20 20 20 20 20 76 61 5f 73 t ap;. va_s
b2280 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 tart(ap, zFormat
b2290 29 3b 0a 20 20 20 20 20 20 7a 20 3d 20 73 71 6c );. z = sql
b22a0 69 74 65 33 56 4d 50 72 69 6e 74 66 28 64 62 2c ite3VMPrintf(db,
b22b0 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 zFormat, ap);.
b22c0 20 20 20 20 20 76 61 5f 65 6e 64 28 61 70 29 3b va_end(ap);
b22d0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 61 . sqlite3Va
b22e0 6c 75 65 53 65 74 53 74 72 28 64 62 2d 3e 70 45 lueSetStr(db->pE
b22f0 72 72 2c 20 2d 31 2c 20 7a 2c 20 53 51 4c 49 54 rr, -1, z, SQLIT
b2300 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 44 E_UTF8, SQLITE_D
b2310 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 7d 65 6c YNAMIC);. }el
b2320 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 se{. sqlite
b2330 33 56 61 6c 75 65 53 65 74 53 74 72 28 64 62 2d 3ValueSetStr(db-
b2340 3e 70 45 72 72 2c 20 30 2c 20 30 2c 20 53 51 4c >pErr, 0, 0, SQL
b2350 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 ITE_UTF8, SQLITE
b2360 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 7d 0a _STATIC);. }.
b2370 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 }.}../*.** Add
b2380 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 an error messag
b2390 65 20 74 6f 20 70 50 61 72 73 65 2d 3e 7a 45 72 e to pParse->zEr
b23a0 72 4d 73 67 20 61 6e 64 20 69 6e 63 72 65 6d 65 rMsg and increme
b23b0 6e 74 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 2e nt pParse->nErr.
b23c0 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e .** The followin
b23d0 67 20 66 6f 72 6d 61 74 74 69 6e 67 20 63 68 61 g formatting cha
b23e0 72 61 63 74 65 72 73 20 61 72 65 20 61 6c 6c 6f racters are allo
b23f0 77 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 wed:.**.**
b2400 25 73 20 20 20 20 20 20 49 6e 73 65 72 74 20 61 %s Insert a
b2410 20 73 74 72 69 6e 67 0a 2a 2a 20 20 20 20 20 20 string.**
b2420 25 7a 20 20 20 20 20 20 41 20 73 74 72 69 6e 67 %z A string
b2430 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 that should be
b2440 66 72 65 65 64 20 61 66 74 65 72 20 75 73 65 0a freed after use.
b2450 2a 2a 20 20 20 20 20 20 25 64 20 20 20 20 20 20 ** %d
b2460 49 6e 73 65 72 74 20 61 6e 20 69 6e 74 65 67 65 Insert an intege
b2470 72 0a 2a 2a 20 20 20 20 20 20 25 54 20 20 20 20 r.** %T
b2480 20 20 49 6e 73 65 72 74 20 61 20 74 6f 6b 65 6e Insert a token
b2490 0a 2a 2a 20 20 20 20 20 20 25 53 20 20 20 20 20 .** %S
b24a0 20 49 6e 73 65 72 74 20 74 68 65 20 66 69 72 73 Insert the firs
b24b0 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20 61 20 53 t element of a S
b24c0 72 63 4c 69 73 74 0a 2a 2a 0a 2a 2a 20 54 68 69 rcList.**.** Thi
b24d0 73 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c s function shoul
b24e0 64 20 62 65 20 75 73 65 64 20 74 6f 20 72 65 70 d be used to rep
b24f0 6f 72 74 20 61 6e 79 20 65 72 72 6f 72 20 74 68 ort any error th
b2500 61 74 20 6f 63 63 75 72 73 20 77 68 69 6c 73 74 at occurs whilst
b2510 0a 2a 2a 20 63 6f 6d 70 69 6c 69 6e 67 20 61 6e .** compiling an
b2520 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 28 SQL statement (
b2530 69 2e 65 2e 20 77 69 74 68 69 6e 20 73 71 6c 69 i.e. within sqli
b2540 74 65 33 5f 70 72 65 70 61 72 65 28 29 29 2e 20 te3_prepare()).
b2550 54 68 65 0a 2a 2a 20 6c 61 73 74 20 74 68 69 6e The.** last thin
b2560 67 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 72 g the sqlite3_pr
b2570 65 70 61 72 65 28 29 20 66 75 6e 63 74 69 6f 6e epare() function
b2580 20 64 6f 65 73 20 69 73 20 63 6f 70 79 20 74 68 does is copy th
b2590 65 20 65 72 72 6f 72 0a 2a 2a 20 73 74 6f 72 65 e error.** store
b25a0 64 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69 d by this functi
b25b0 6f 6e 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 on into the data
b25c0 62 61 73 65 20 68 61 6e 64 6c 65 20 75 73 69 6e base handle usin
b25d0 67 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 29 g sqlite3Error()
b25e0 2e 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 73 71 ..** Function sq
b25f0 6c 69 74 65 33 45 72 72 6f 72 28 29 20 73 68 6f lite3Error() sho
b2600 75 6c 64 20 62 65 20 75 73 65 64 20 64 75 72 69 uld be used duri
b2610 6e 67 20 73 74 61 74 65 6d 65 6e 74 20 65 78 65 ng statement exe
b2620 63 75 74 69 6f 6e 0a 2a 2a 20 28 73 71 6c 69 74 cution.** (sqlit
b2630 65 33 5f 73 74 65 70 28 29 20 65 74 63 2e 29 2e e3_step() etc.).
b2640 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
b2650 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 TE void sqlite3E
b2660 72 72 6f 72 4d 73 67 28 50 61 72 73 65 20 2a 70 rrorMsg(Parse *p
b2670 50 61 72 73 65 2c 20 63 6f 6e 73 74 20 63 68 61 Parse, const cha
b2680 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 r *zFormat, ...)
b2690 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a {. va_list ap;.
b26a0 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 sqlite3 *db =
b26b0 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 70 50 pParse->db;. pP
b26c0 61 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 arse->nErr++;.
b26d0 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
b26e0 2c 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 , pParse->zErrMs
b26f0 67 29 3b 0a 20 20 76 61 5f 73 74 61 72 74 28 61 g);. va_start(a
b2700 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 70 p, zFormat);. p
b2710 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20 3d Parse->zErrMsg =
b2720 20 73 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 sqlite3VMPrintf
b2730 28 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 (db, zFormat, ap
b2740 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b );. va_end(ap);
b2750 0a 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 . pParse->rc =
b2760 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 7d 0a SQLITE_ERROR;.}.
b2770 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65 ./*.** Clear the
b2780 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 error message i
b2790 6e 20 70 50 61 72 73 65 2c 20 69 66 20 61 6e 79 n pParse, if any
b27a0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
b27b0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 TE void sqlite3E
b27c0 72 72 6f 72 43 6c 65 61 72 28 50 61 72 73 65 20 rrorClear(Parse
b27d0 2a 70 50 61 72 73 65 29 7b 0a 20 20 73 71 6c 69 *pParse){. sqli
b27e0 74 65 33 44 62 46 72 65 65 28 70 50 61 72 73 65 te3DbFree(pParse
b27f0 2d 3e 64 62 2c 20 70 50 61 72 73 65 2d 3e 7a 45 ->db, pParse->zE
b2800 72 72 4d 73 67 29 3b 0a 20 20 70 50 61 72 73 65 rrMsg);. pParse
b2810 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 ->zErrMsg = 0;.
b2820 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 3d 20 pParse->nErr =
b2830 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 0;.}../*.** Conv
b2840 65 72 74 20 61 6e 20 53 51 4c 2d 73 74 79 6c 65 ert an SQL-style
b2850 20 71 75 6f 74 65 64 20 73 74 72 69 6e 67 20 69 quoted string i
b2860 6e 74 6f 20 61 20 6e 6f 72 6d 61 6c 20 73 74 72 nto a normal str
b2870 69 6e 67 20 62 79 20 72 65 6d 6f 76 69 6e 67 0a ing by removing.
b2880 2a 2a 20 74 68 65 20 71 75 6f 74 65 20 63 68 61 ** the quote cha
b2890 72 61 63 74 65 72 73 2e 20 20 54 68 65 20 63 6f racters. The co
b28a0 6e 76 65 72 73 69 6f 6e 20 69 73 20 64 6f 6e 65 nversion is done
b28b0 20 69 6e 2d 70 6c 61 63 65 2e 20 20 49 66 20 74 in-place. If t
b28c0 68 65 0a 2a 2a 20 69 6e 70 75 74 20 64 6f 65 73 he.** input does
b28d0 20 6e 6f 74 20 62 65 67 69 6e 20 77 69 74 68 20 not begin with
b28e0 61 20 71 75 6f 74 65 20 63 68 61 72 61 63 74 65 a quote characte
b28f0 72 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 r, then this rou
b2900 74 69 6e 65 0a 2a 2a 20 69 73 20 61 20 6e 6f 2d tine.** is a no-
b2910 6f 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e op..**.** The in
b2920 70 75 74 20 73 74 72 69 6e 67 20 6d 75 73 74 20 put string must
b2930 62 65 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 be zero-terminat
b2940 65 64 2e 20 20 41 20 6e 65 77 20 7a 65 72 6f 2d ed. A new zero-
b2950 74 65 72 6d 69 6e 61 74 6f 72 0a 2a 2a 20 69 73 terminator.** is
b2960 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 64 65 added to the de
b2970 71 75 6f 74 65 64 20 73 74 72 69 6e 67 2e 0a 2a quoted string..*
b2980 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 20 *.** The return
b2990 76 61 6c 75 65 20 69 73 20 2d 31 20 69 66 20 6e value is -1 if n
b29a0 6f 20 64 65 71 75 6f 74 69 6e 67 20 6f 63 63 75 o dequoting occu
b29b0 72 73 20 6f 72 20 74 68 65 20 6c 65 6e 67 74 68 rs or the length
b29c0 20 6f 66 20 74 68 65 0a 2a 2a 20 64 65 71 75 6f of the.** dequo
b29d0 74 65 64 20 73 74 72 69 6e 67 2c 20 65 78 63 6c ted string, excl
b29e0 75 73 69 76 65 20 6f 66 20 74 68 65 20 7a 65 72 usive of the zer
b29f0 6f 20 74 65 72 6d 69 6e 61 74 6f 72 2c 20 69 66 o terminator, if
b2a00 20 64 65 71 75 6f 74 69 6e 67 20 64 6f 65 73 0a dequoting does.
b2a10 2a 2a 20 6f 63 63 75 72 2e 0a 2a 2a 0a 2a 2a 20 ** occur..**.**
b2a20 32 30 30 32 2d 46 65 62 2d 31 34 3a 20 54 68 69 2002-Feb-14: Thi
b2a30 73 20 72 6f 75 74 69 6e 65 20 69 73 20 65 78 74 s routine is ext
b2a40 65 6e 64 65 64 20 74 6f 20 72 65 6d 6f 76 65 20 ended to remove
b2a50 4d 53 2d 41 63 63 65 73 73 20 73 74 79 6c 65 0a MS-Access style.
b2a60 2a 2a 20 62 72 61 63 6b 65 74 73 20 66 72 6f 6d ** brackets from
b2a70 20 61 72 6f 75 6e 64 20 69 64 65 6e 74 69 66 65 around identife
b2a80 72 73 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 rs. For example
b2a90 3a 20 20 22 5b 61 2d 62 2d 63 5d 22 20 62 65 63 : "[a-b-c]" bec
b2aa0 6f 6d 65 73 0a 2a 2a 20 22 61 2d 62 2d 63 22 2e omes.** "a-b-c".
b2ab0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
b2ac0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 44 65 TE int sqlite3De
b2ad0 71 75 6f 74 65 28 63 68 61 72 20 2a 7a 29 7b 0a quote(char *z){.
b2ae0 20 20 63 68 61 72 20 71 75 6f 74 65 3b 0a 20 20 char quote;.
b2af0 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 69 66 28 20 int i, j;. if(
b2b00 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 2d 31 z==0 ) return -1
b2b10 3b 0a 20 20 71 75 6f 74 65 20 3d 20 7a 5b 30 5d ;. quote = z[0]
b2b20 3b 0a 20 20 73 77 69 74 63 68 28 20 71 75 6f 74 ;. switch( quot
b2b30 65 20 29 7b 0a 20 20 20 20 63 61 73 65 20 27 5c e ){. case '\
b2b40 27 27 3a 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 '': break;.
b2b50 63 61 73 65 20 27 22 27 3a 20 20 20 62 72 65 61 case '"': brea
b2b60 6b 3b 0a 20 20 20 20 63 61 73 65 20 27 60 27 3a k;. case '`':
b2b70 20 20 20 62 72 65 61 6b 3b 20 20 20 20 20 20 20 break;
b2b80 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 /* For
b2b90 4d 79 53 51 4c 20 63 6f 6d 70 61 74 69 62 69 6c MySQL compatibil
b2ba0 69 74 79 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 ity */. case
b2bb0 27 5b 27 3a 20 20 20 71 75 6f 74 65 20 3d 20 27 '[': quote = '
b2bc0 5d 27 3b 20 20 62 72 65 61 6b 3b 20 20 2f 2a 20 ]'; break; /*
b2bd0 46 6f 72 20 4d 53 20 53 71 6c 53 65 72 76 65 72 For MS SqlServer
b2be0 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 2a compatibility *
b2bf0 2f 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 20 /. default:
b2c00 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 7d return -1;. }
b2c10 0a 20 20 66 6f 72 28 69 3d 31 2c 20 6a 3d 30 3b . for(i=1, j=0;
b2c20 20 41 4c 57 41 59 53 28 7a 5b 69 5d 29 3b 20 69 ALWAYS(z[i]); i
b2c30 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 7a 5b 69 ++){. if( z[i
b2c40 5d 3d 3d 71 75 6f 74 65 20 29 7b 0a 20 20 20 20 ]==quote ){.
b2c50 20 20 69 66 28 20 7a 5b 69 2b 31 5d 3d 3d 71 75 if( z[i+1]==qu
b2c60 6f 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20 7a ote ){. z
b2c70 5b 6a 2b 2b 5d 20 3d 20 71 75 6f 74 65 3b 0a 20 [j++] = quote;.
b2c80 20 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 i++;.
b2c90 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
b2ca0 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a break;. }.
b2cb0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
b2cc0 20 7a 5b 6a 2b 2b 5d 20 3d 20 7a 5b 69 5d 3b 0a z[j++] = z[i];.
b2cd0 20 20 20 20 7d 0a 20 20 7d 0a 20 20 7a 5b 6a 5d }. }. z[j]
b2ce0 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 6a = 0;. return j
b2cf0 3b 0a 7d 0a 0a 2f 2a 20 43 6f 6e 76 65 6e 69 65 ;.}../* Convenie
b2d00 6e 74 20 73 68 6f 72 74 2d 68 61 6e 64 20 2a 2f nt short-hand */
b2d10 0a 23 64 65 66 69 6e 65 20 55 70 70 65 72 54 6f .#define UpperTo
b2d20 4c 6f 77 65 72 20 73 71 6c 69 74 65 33 55 70 70 Lower sqlite3Upp
b2d30 65 72 54 6f 4c 6f 77 65 72 0a 0a 2f 2a 0a 2a 2a erToLower../*.**
b2d40 20 53 6f 6d 65 20 73 79 73 74 65 6d 73 20 68 61 Some systems ha
b2d50 76 65 20 73 74 72 69 63 6d 70 28 29 2e 20 20 4f ve stricmp(). O
b2d60 74 68 65 72 73 20 68 61 76 65 20 73 74 72 63 61 thers have strca
b2d70 73 65 63 6d 70 28 29 2e 20 20 42 65 63 61 75 73 secmp(). Becaus
b2d80 65 0a 2a 2a 20 74 68 65 72 65 20 69 73 20 6e 6f e.** there is no
b2d90 20 63 6f 6e 73 69 73 74 65 6e 63 79 2c 20 77 65 consistency, we
b2da0 20 77 69 6c 6c 20 64 65 66 69 6e 65 20 6f 75 72 will define our
b2db0 20 6f 77 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f own..*/.SQLITE_
b2dc0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
b2dd0 74 65 33 53 74 72 49 43 6d 70 28 63 6f 6e 73 74 te3StrICmp(const
b2de0 20 63 68 61 72 20 2a 7a 4c 65 66 74 2c 20 63 6f char *zLeft, co
b2df0 6e 73 74 20 63 68 61 72 20 2a 7a 52 69 67 68 74 nst char *zRight
b2e00 29 7b 0a 20 20 72 65 67 69 73 74 65 72 20 75 6e ){. register un
b2e10 73 69 67 6e 65 64 20 63 68 61 72 20 2a 61 2c 20 signed char *a,
b2e20 2a 62 3b 0a 20 20 61 20 3d 20 28 75 6e 73 69 67 *b;. a = (unsig
b2e30 6e 65 64 20 63 68 61 72 20 2a 29 7a 4c 65 66 74 ned char *)zLeft
b2e40 3b 0a 20 20 62 20 3d 20 28 75 6e 73 69 67 6e 65 ;. b = (unsigne
b2e50 64 20 63 68 61 72 20 2a 29 7a 52 69 67 68 74 3b d char *)zRight;
b2e60 0a 20 20 77 68 69 6c 65 28 20 2a 61 21 3d 30 20 . while( *a!=0
b2e70 26 26 20 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b && UpperToLower[
b2e80 2a 61 5d 3d 3d 55 70 70 65 72 54 6f 4c 6f 77 65 *a]==UpperToLowe
b2e90 72 5b 2a 62 5d 29 7b 20 61 2b 2b 3b 20 62 2b 2b r[*b]){ a++; b++
b2ea0 3b 20 7d 0a 20 20 72 65 74 75 72 6e 20 55 70 70 ; }. return Upp
b2eb0 65 72 54 6f 4c 6f 77 65 72 5b 2a 61 5d 20 2d 20 erToLower[*a] -
b2ec0 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 62 5d UpperToLower[*b]
b2ed0 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 ;.}.SQLITE_API i
b2ee0 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 72 6e 69 nt sqlite3_strni
b2ef0 63 6d 70 28 63 6f 6e 73 74 20 63 68 61 72 20 2a cmp(const char *
b2f00 7a 4c 65 66 74 2c 20 63 6f 6e 73 74 20 63 68 61 zLeft, const cha
b2f10 72 20 2a 7a 52 69 67 68 74 2c 20 69 6e 74 20 4e r *zRight, int N
b2f20 29 7b 0a 20 20 72 65 67 69 73 74 65 72 20 75 6e ){. register un
b2f30 73 69 67 6e 65 64 20 63 68 61 72 20 2a 61 2c 20 signed char *a,
b2f40 2a 62 3b 0a 20 20 61 20 3d 20 28 75 6e 73 69 67 *b;. a = (unsig
b2f50 6e 65 64 20 63 68 61 72 20 2a 29 7a 4c 65 66 74 ned char *)zLeft
b2f60 3b 0a 20 20 62 20 3d 20 28 75 6e 73 69 67 6e 65 ;. b = (unsigne
b2f70 64 20 63 68 61 72 20 2a 29 7a 52 69 67 68 74 3b d char *)zRight;
b2f80 0a 20 20 77 68 69 6c 65 28 20 4e 2d 2d 20 3e 20 . while( N-- >
b2f90 30 20 26 26 20 2a 61 21 3d 30 20 26 26 20 55 70 0 && *a!=0 && Up
b2fa0 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 61 5d 3d 3d perToLower[*a]==
b2fb0 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 62 5d UpperToLower[*b]
b2fc0 29 7b 20 61 2b 2b 3b 20 62 2b 2b 3b 20 7d 0a 20 ){ a++; b++; }.
b2fd0 20 72 65 74 75 72 6e 20 4e 3c 30 20 3f 20 30 20 return N<0 ? 0
b2fe0 3a 20 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a : UpperToLower[*
b2ff0 61 5d 20 2d 20 55 70 70 65 72 54 6f 4c 6f 77 65 a] - UpperToLowe
b3000 72 5b 2a 62 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 r[*b];.}../*.**
b3010 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 7a Return TRUE if z
b3020 20 69 73 20 61 20 70 75 72 65 20 6e 75 6d 65 72 is a pure numer
b3030 69 63 20 73 74 72 69 6e 67 2e 20 20 52 65 74 75 ic string. Retu
b3040 72 6e 20 46 41 4c 53 45 20 61 6e 64 20 6c 65 61 rn FALSE and lea
b3050 76 65 0a 2a 2a 20 2a 72 65 61 6c 6e 75 6d 20 75 ve.** *realnum u
b3060 6e 63 68 61 6e 67 65 64 20 69 66 20 74 68 65 20 nchanged if the
b3070 73 74 72 69 6e 67 20 63 6f 6e 74 61 69 6e 73 20 string contains
b3080 61 6e 79 20 63 68 61 72 61 63 74 65 72 20 77 68 any character wh
b3090 69 63 68 20 69 73 20 6e 6f 74 0a 2a 2a 20 70 61 ich is not.** pa
b30a0 72 74 20 6f 66 20 61 20 6e 75 6d 62 65 72 2e 0a rt of a number..
b30b0 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 73 74 72 **.** If the str
b30c0 69 6e 67 20 69 73 20 70 75 72 65 20 6e 75 6d 65 ing is pure nume
b30d0 72 69 63 2c 20 73 65 74 20 2a 72 65 61 6c 6e 75 ric, set *realnu
b30e0 6d 20 74 6f 20 54 52 55 45 20 69 66 20 74 68 65 m to TRUE if the
b30f0 20 73 74 72 69 6e 67 0a 2a 2a 20 63 6f 6e 74 61 string.** conta
b3100 69 6e 73 20 74 68 65 20 27 2e 27 20 63 68 61 72 ins the '.' char
b3110 61 63 74 65 72 20 6f 72 20 61 6e 20 22 45 2b 30 acter or an "E+0
b3120 30 30 22 20 73 74 79 6c 65 20 65 78 70 6f 6e 65 00" style expone
b3130 6e 74 69 61 74 69 6f 6e 20 73 75 66 66 69 78 2e ntiation suffix.
b3140 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 20 73 65 .** Otherwise se
b3150 74 20 2a 72 65 61 6c 6e 75 6d 20 74 6f 20 46 41 t *realnum to FA
b3160 4c 53 45 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 LSE. Note that
b3170 6a 75 73 74 20 62 65 63 61 75 65 20 2a 72 65 61 just becaue *rea
b3180 6c 6e 75 6d 20 69 73 0a 2a 2a 20 66 61 6c 73 65 lnum is.** false
b3190 20 64 6f 65 73 20 6e 6f 74 20 6d 65 61 6e 20 74 does not mean t
b31a0 68 61 74 20 74 68 65 20 6e 75 6d 62 65 72 20 63 hat the number c
b31b0 61 6e 20 62 65 20 73 75 63 63 65 73 73 66 75 6c an be successful
b31c0 6c 79 20 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 ly converted int
b31d0 6f 0a 2a 2a 20 61 6e 20 69 6e 74 65 67 65 72 20 o.** an integer
b31e0 2d 20 69 74 20 6d 69 67 68 74 20 62 65 20 74 6f - it might be to
b31f0 6f 20 62 69 67 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 o big..**.** An
b3200 65 6d 70 74 79 20 73 74 72 69 6e 67 20 69 73 20 empty string is
b3210 63 6f 6e 73 69 64 65 72 65 64 20 6e 6f 6e 2d 6e considered non-n
b3220 75 6d 65 72 69 63 2e 0a 2a 2f 0a 53 51 4c 49 54 umeric..*/.SQLIT
b3230 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
b3240 6c 69 74 65 33 49 73 4e 75 6d 62 65 72 28 63 6f lite3IsNumber(co
b3250 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 nst char *z, int
b3260 20 2a 72 65 61 6c 6e 75 6d 2c 20 75 38 20 65 6e *realnum, u8 en
b3270 63 29 7b 0a 20 20 69 6e 74 20 69 6e 63 72 20 3d c){. int incr =
b3280 20 28 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 (enc==SQLITE_UT
b3290 46 38 3f 31 3a 32 29 3b 0a 20 20 69 66 28 20 65 F8?1:2);. if( e
b32a0 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 nc==SQLITE_UTF16
b32b0 42 45 20 29 20 7a 2b 2b 3b 0a 20 20 69 66 28 20 BE ) z++;. if(
b32c0 2a 7a 3d 3d 27 2d 27 20 7c 7c 20 2a 7a 3d 3d 27 *z=='-' || *z=='
b32d0 2b 27 20 29 20 7a 20 2b 3d 20 69 6e 63 72 3b 0a +' ) z += incr;.
b32e0 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 49 73 if( !sqlite3Is
b32f0 64 69 67 69 74 28 2a 7a 29 20 29 7b 0a 20 20 20 digit(*z) ){.
b3300 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 return 0;. }.
b3310 20 7a 20 2b 3d 20 69 6e 63 72 3b 0a 20 20 2a 72 z += incr;. *r
b3320 65 61 6c 6e 75 6d 20 3d 20 30 3b 0a 20 20 77 68 ealnum = 0;. wh
b3330 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73 64 69 ile( sqlite3Isdi
b3340 67 69 74 28 2a 7a 29 20 29 7b 20 7a 20 2b 3d 20 git(*z) ){ z +=
b3350 69 6e 63 72 3b 20 7d 0a 20 20 69 66 28 20 2a 7a incr; }. if( *z
b3360 3d 3d 27 2e 27 20 29 7b 0a 20 20 20 20 7a 20 2b =='.' ){. z +
b3370 3d 20 69 6e 63 72 3b 0a 20 20 20 20 69 66 28 20 = incr;. if(
b3380 21 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 !sqlite3Isdigit(
b3390 2a 7a 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a *z) ) return 0;.
b33a0 20 20 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74 while( sqlit
b33b0 65 33 49 73 64 69 67 69 74 28 2a 7a 29 20 29 7b e3Isdigit(*z) ){
b33c0 20 7a 20 2b 3d 20 69 6e 63 72 3b 20 7d 0a 20 20 z += incr; }.
b33d0 20 20 2a 72 65 61 6c 6e 75 6d 20 3d 20 31 3b 0a *realnum = 1;.
b33e0 20 20 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d 27 65 }. if( *z=='e
b33f0 27 20 7c 7c 20 2a 7a 3d 3d 27 45 27 20 29 7b 0a ' || *z=='E' ){.
b3400 20 20 20 20 7a 20 2b 3d 20 69 6e 63 72 3b 0a 20 z += incr;.
b3410 20 20 20 69 66 28 20 2a 7a 3d 3d 27 2b 27 20 7c if( *z=='+' |
b3420 7c 20 2a 7a 3d 3d 27 2d 27 20 29 20 7a 20 2b 3d | *z=='-' ) z +=
b3430 20 69 6e 63 72 3b 0a 20 20 20 20 69 66 28 20 21 incr;. if( !
b3440 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 2a sqlite3Isdigit(*
b3450 7a 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 z) ) return 0;.
b3460 20 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 while( sqlite
b3470 33 49 73 64 69 67 69 74 28 2a 7a 29 20 29 7b 20 3Isdigit(*z) ){
b3480 7a 20 2b 3d 20 69 6e 63 72 3b 20 7d 0a 20 20 20 z += incr; }.
b3490 20 2a 72 65 61 6c 6e 75 6d 20 3d 20 31 3b 0a 20 *realnum = 1;.
b34a0 20 7d 0a 20 20 72 65 74 75 72 6e 20 2a 7a 3d 3d }. return *z==
b34b0 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 0;.}../*.** The
b34c0 73 74 72 69 6e 67 20 7a 5b 5d 20 69 73 20 61 6e string z[] is an
b34d0 20 41 53 43 49 49 20 72 65 70 72 65 73 65 6e 74 ASCII represent
b34e0 61 74 69 6f 6e 20 6f 66 20 61 20 72 65 61 6c 20 ation of a real
b34f0 6e 75 6d 62 65 72 2e 0a 2a 2a 20 43 6f 6e 76 65 number..** Conve
b3500 72 74 20 74 68 69 73 20 73 74 72 69 6e 67 20 74 rt this string t
b3510 6f 20 61 20 64 6f 75 62 6c 65 2e 0a 2a 2a 0a 2a o a double..**.*
b3520 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 * This routine a
b3530 73 73 75 6d 65 73 20 74 68 61 74 20 7a 5b 5d 20 ssumes that z[]
b3540 72 65 61 6c 6c 79 20 69 73 20 61 20 76 61 6c 69 really is a vali
b3550 64 20 6e 75 6d 62 65 72 2e 20 20 49 66 20 69 74 d number. If it
b3560 0a 2a 2a 20 69 73 20 6e 6f 74 2c 20 74 68 65 20 .** is not, the
b3570 72 65 73 75 6c 74 20 69 73 20 75 6e 64 65 66 69 result is undefi
b3580 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ned..**.** This
b3590 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 routine is used
b35a0 69 6e 73 74 65 61 64 20 6f 66 20 74 68 65 20 6c instead of the l
b35b0 69 62 72 61 72 79 20 61 74 6f 66 28 29 20 66 75 ibrary atof() fu
b35c0 6e 63 74 69 6f 6e 20 62 65 63 61 75 73 65 0a 2a nction because.*
b35d0 2a 20 74 68 65 20 6c 69 62 72 61 72 79 20 61 74 * the library at
b35e0 6f 66 28 29 20 6d 69 67 68 74 20 77 61 6e 74 20 of() might want
b35f0 74 6f 20 75 73 65 20 22 2c 22 20 61 73 20 74 68 to use "," as th
b3600 65 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20 e decimal point
b3610 69 6e 73 74 65 61 64 0a 2a 2a 20 6f 66 20 22 2e instead.** of ".
b3620 22 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 68 " depending on h
b3630 6f 77 20 6c 6f 63 61 6c 65 20 69 73 20 73 65 74 ow locale is set
b3640 2e 20 20 42 75 74 20 74 68 61 74 20 77 6f 75 6c . But that woul
b3650 64 20 63 61 75 73 65 20 70 72 6f 62 6c 65 6d 73 d cause problems
b3660 0a 2a 2a 20 66 6f 72 20 53 51 4c 2e 20 20 53 6f .** for SQL. So
b3670 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 61 6c this routine al
b3680 77 61 79 73 20 75 73 65 73 20 22 2e 22 20 72 65 ways uses "." re
b3690 67 61 72 64 6c 65 73 73 20 6f 66 20 6c 6f 63 61 gardless of loca
b36a0 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 le..*/.SQLITE_PR
b36b0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
b36c0 33 41 74 6f 46 28 63 6f 6e 73 74 20 63 68 61 72 3AtoF(const char
b36d0 20 2a 7a 2c 20 64 6f 75 62 6c 65 20 2a 70 52 65 *z, double *pRe
b36e0 73 75 6c 74 29 7b 0a 23 69 66 6e 64 65 66 20 53 sult){.#ifndef S
b36f0 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 QLITE_OMIT_FLOAT
b3700 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 63 6f 6e 73 ING_POINT. cons
b3710 74 20 63 68 61 72 20 2a 7a 42 65 67 69 6e 20 3d t char *zBegin =
b3720 20 7a 3b 0a 20 20 2f 2a 20 73 69 67 6e 20 2a 20 z;. /* sign *
b3730 73 69 67 6e 69 66 69 63 61 6e 64 20 2a 20 28 31 significand * (1
b3740 30 20 5e 20 28 65 73 69 67 6e 20 2a 20 65 78 70 0 ^ (esign * exp
b3750 6f 6e 65 6e 74 29 29 20 2a 2f 0a 20 20 69 6e 74 onent)) */. int
b3760 20 73 69 67 6e 20 3d 20 31 3b 20 20 20 2f 2a 20 sign = 1; /*
b3770 73 69 67 6e 20 6f 66 20 73 69 67 6e 69 66 69 63 sign of signific
b3780 61 6e 64 20 2a 2f 0a 20 20 69 36 34 20 73 20 3d and */. i64 s =
b3790 20 30 3b 20 20 20 20 20 20 2f 2a 20 73 69 67 6e 0; /* sign
b37a0 69 66 69 63 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 ificand */. int
b37b0 20 64 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 d = 0; /*
b37c0 61 64 6a 75 73 74 20 65 78 70 6f 6e 65 6e 74 20 adjust exponent
b37d0 66 6f 72 20 73 68 69 66 74 69 6e 67 20 64 65 63 for shifting dec
b37e0 69 6d 61 6c 20 70 6f 69 6e 74 20 2a 2f 0a 20 20 imal point */.
b37f0 69 6e 74 20 65 73 69 67 6e 20 3d 20 31 3b 20 20 int esign = 1;
b3800 2f 2a 20 73 69 67 6e 20 6f 66 20 65 78 70 6f 6e /* sign of expon
b3810 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 65 20 3d ent */. int e =
b3820 20 30 3b 20 20 20 20 20 20 2f 2a 20 65 78 70 6f 0; /* expo
b3830 6e 65 6e 74 20 2a 2f 0a 20 20 64 6f 75 62 6c 65 nent */. double
b3840 20 72 65 73 75 6c 74 3b 0a 20 20 69 6e 74 20 6e result;. int n
b3850 44 69 67 69 74 73 20 3d 20 30 3b 0a 0a 20 20 2f Digits = 0;.. /
b3860 2a 20 73 6b 69 70 20 6c 65 61 64 69 6e 67 20 73 * skip leading s
b3870 70 61 63 65 73 20 2a 2f 0a 20 20 77 68 69 6c 65 paces */. while
b3880 28 20 73 71 6c 69 74 65 33 49 73 73 70 61 63 65 ( sqlite3Isspace
b3890 28 2a 7a 29 20 29 20 7a 2b 2b 3b 0a 20 20 2f 2a (*z) ) z++;. /*
b38a0 20 67 65 74 20 73 69 67 6e 20 6f 66 20 73 69 67 get sign of sig
b38b0 6e 69 66 69 63 61 6e 64 20 2a 2f 0a 20 20 69 66 nificand */. if
b38c0 28 20 2a 7a 3d 3d 27 2d 27 20 29 7b 0a 20 20 20 ( *z=='-' ){.
b38d0 20 73 69 67 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 sign = -1;.
b38e0 7a 2b 2b 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 z++;. }else if(
b38f0 20 2a 7a 3d 3d 27 2b 27 20 29 7b 0a 20 20 20 20 *z=='+' ){.
b3900 7a 2b 2b 3b 0a 20 20 7d 0a 20 20 2f 2a 20 73 6b z++;. }. /* sk
b3910 69 70 20 6c 65 61 64 69 6e 67 20 7a 65 72 6f 65 ip leading zeroe
b3920 73 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 7a 5b s */. while( z[
b3930 30 5d 3d 3d 27 30 27 20 29 20 7a 2b 2b 2c 20 6e 0]=='0' ) z++, n
b3940 44 69 67 69 74 73 2b 2b 3b 0a 0a 20 20 2f 2a 20 Digits++;.. /*
b3950 63 6f 70 79 20 6d 61 78 20 73 69 67 6e 69 66 69 copy max signifi
b3960 63 61 6e 74 20 64 69 67 69 74 73 20 74 6f 20 73 cant digits to s
b3970 69 67 6e 69 66 69 63 61 6e 64 20 2a 2f 0a 20 20 ignificand */.
b3980 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73 while( sqlite3Is
b3990 64 69 67 69 74 28 2a 7a 29 20 26 26 20 73 3c 28 digit(*z) && s<(
b39a0 28 4c 41 52 47 45 53 54 5f 49 4e 54 36 34 2d 39 (LARGEST_INT64-9
b39b0 29 2f 31 30 29 20 29 7b 0a 20 20 20 20 73 20 3d )/10) ){. s =
b39c0 20 73 2a 31 30 20 2b 20 28 2a 7a 20 2d 20 27 30 s*10 + (*z - '0
b39d0 27 29 3b 0a 20 20 20 20 7a 2b 2b 2c 20 6e 44 69 ');. z++, nDi
b39e0 67 69 74 73 2b 2b 3b 0a 20 20 7d 0a 20 20 2f 2a gits++;. }. /*
b39f0 20 73 6b 69 70 20 6e 6f 6e 2d 73 69 67 6e 69 66 skip non-signif
b3a00 69 63 61 6e 74 20 73 69 67 6e 69 66 69 63 61 6e icant significan
b3a10 64 20 64 69 67 69 74 73 0a 20 20 2a 2a 20 28 69 d digits. ** (i
b3a20 6e 63 72 65 61 73 65 20 65 78 70 6f 6e 65 6e 74 ncrease exponent
b3a30 20 62 79 20 64 20 74 6f 20 73 68 69 66 74 20 64 by d to shift d
b3a40 65 63 69 6d 61 6c 20 6c 65 66 74 29 20 2a 2f 0a ecimal left) */.
b3a50 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33 while( sqlite3
b3a60 49 73 64 69 67 69 74 28 2a 7a 29 20 29 20 7a 2b Isdigit(*z) ) z+
b3a70 2b 2c 20 6e 44 69 67 69 74 73 2b 2b 2c 20 64 2b +, nDigits++, d+
b3a80 2b 3b 0a 0a 20 20 2f 2a 20 69 66 20 64 65 63 69 +;.. /* if deci
b3a90 6d 61 6c 20 70 6f 69 6e 74 20 69 73 20 70 72 65 mal point is pre
b3aa0 73 65 6e 74 20 2a 2f 0a 20 20 69 66 28 20 2a 7a sent */. if( *z
b3ab0 3d 3d 27 2e 27 20 29 7b 0a 20 20 20 20 7a 2b 2b =='.' ){. z++
b3ac0 3b 0a 20 20 20 20 2f 2a 20 63 6f 70 79 20 64 69 ;. /* copy di
b3ad0 67 69 74 73 20 66 72 6f 6d 20 61 66 74 65 72 20 gits from after
b3ae0 64 65 63 69 6d 61 6c 20 74 6f 20 73 69 67 6e 69 decimal to signi
b3af0 66 69 63 61 6e 64 0a 20 20 20 20 2a 2a 20 28 64 ficand. ** (d
b3b00 65 63 72 65 61 73 65 20 65 78 70 6f 6e 65 6e 74 ecrease exponent
b3b10 20 62 79 20 64 20 74 6f 20 73 68 69 66 74 20 64 by d to shift d
b3b20 65 63 69 6d 61 6c 20 72 69 67 68 74 29 20 2a 2f ecimal right) */
b3b30 0a 20 20 20 20 77 68 69 6c 65 28 20 73 71 6c 69 . while( sqli
b3b40 74 65 33 49 73 64 69 67 69 74 28 2a 7a 29 20 26 te3Isdigit(*z) &
b3b50 26 20 73 3c 28 28 4c 41 52 47 45 53 54 5f 49 4e & s<((LARGEST_IN
b3b60 54 36 34 2d 39 29 2f 31 30 29 20 29 7b 0a 20 20 T64-9)/10) ){.
b3b70 20 20 20 20 73 20 3d 20 73 2a 31 30 20 2b 20 28 s = s*10 + (
b3b80 2a 7a 20 2d 20 27 30 27 29 3b 0a 20 20 20 20 20 *z - '0');.
b3b90 20 7a 2b 2b 2c 20 6e 44 69 67 69 74 73 2b 2b 2c z++, nDigits++,
b3ba0 20 64 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 d--;. }.
b3bb0 2f 2a 20 73 6b 69 70 20 6e 6f 6e 2d 73 69 67 6e /* skip non-sign
b3bc0 69 66 69 63 61 6e 74 20 64 69 67 69 74 73 20 2a ificant digits *
b3bd0 2f 0a 20 20 20 20 77 68 69 6c 65 28 20 73 71 6c /. while( sql
b3be0 69 74 65 33 49 73 64 69 67 69 74 28 2a 7a 29 20 ite3Isdigit(*z)
b3bf0 29 20 7a 2b 2b 2c 20 6e 44 69 67 69 74 73 2b 2b ) z++, nDigits++
b3c00 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 69 66 20 65 ;. }.. /* if e
b3c10 78 70 6f 6e 65 6e 74 20 69 73 20 70 72 65 73 65 xponent is prese
b3c20 6e 74 20 2a 2f 0a 20 20 69 66 28 20 2a 7a 3d 3d nt */. if( *z==
b3c30 27 65 27 20 7c 7c 20 2a 7a 3d 3d 27 45 27 20 29 'e' || *z=='E' )
b3c40 7b 0a 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 2f {. z++;. /
b3c50 2a 20 67 65 74 20 73 69 67 6e 20 6f 66 20 65 78 * get sign of ex
b3c60 70 6f 6e 65 6e 74 20 2a 2f 0a 20 20 20 20 69 66 ponent */. if
b3c70 28 20 2a 7a 3d 3d 27 2d 27 20 29 7b 0a 20 20 20 ( *z=='-' ){.
b3c80 20 20 20 65 73 69 67 6e 20 3d 20 2d 31 3b 0a 20 esign = -1;.
b3c90 20 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 7d 65 z++;. }e
b3ca0 6c 73 65 20 69 66 28 20 2a 7a 3d 3d 27 2b 27 20 lse if( *z=='+'
b3cb0 29 7b 0a 20 20 20 20 20 20 7a 2b 2b 3b 0a 20 20 ){. z++;.
b3cc0 20 20 7d 0a 20 20 20 20 2f 2a 20 63 6f 70 79 20 }. /* copy
b3cd0 64 69 67 69 74 73 20 74 6f 20 65 78 70 6f 6e 65 digits to expone
b3ce0 6e 74 20 2a 2f 0a 20 20 20 20 77 68 69 6c 65 28 nt */. while(
b3cf0 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 sqlite3Isdigit(
b3d00 2a 7a 29 20 29 7b 0a 20 20 20 20 20 20 65 20 3d *z) ){. e =
b3d10 20 65 2a 31 30 20 2b 20 28 2a 7a 20 2d 20 27 30 e*10 + (*z - '0
b3d20 27 29 3b 0a 20 20 20 20 20 20 7a 2b 2b 3b 0a 20 ');. z++;.
b3d30 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 61 }. }.. /* a
b3d40 64 6a 75 73 74 20 65 78 70 6f 6e 65 6e 74 20 62 djust exponent b
b3d50 79 20 64 2c 20 61 6e 64 20 75 70 64 61 74 65 20 y d, and update
b3d60 73 69 67 6e 20 2a 2f 0a 20 20 65 20 3d 20 28 65 sign */. e = (e
b3d70 2a 65 73 69 67 6e 29 20 2b 20 64 3b 0a 20 20 69 *esign) + d;. i
b3d80 66 28 20 65 3c 30 20 29 20 7b 0a 20 20 20 20 65 f( e<0 ) {. e
b3d90 73 69 67 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 65 sign = -1;. e
b3da0 20 2a 3d 20 2d 31 3b 0a 20 20 7d 20 65 6c 73 65 *= -1;. } else
b3db0 20 7b 0a 20 20 20 20 65 73 69 67 6e 20 3d 20 31 {. esign = 1
b3dc0 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 69 66 20 30 ;. }.. /* if 0
b3dd0 20 73 69 67 6e 69 66 69 63 61 6e 64 20 2a 2f 0a significand */.
b3de0 20 20 69 66 28 20 21 73 20 29 20 7b 0a 20 20 20 if( !s ) {.
b3df0 20 2f 2a 20 49 6e 20 74 68 65 20 49 45 45 45 20 /* In the IEEE
b3e00 37 35 34 20 73 74 61 6e 64 61 72 64 2c 20 7a 65 754 standard, ze
b3e10 72 6f 20 69 73 20 73 69 67 6e 65 64 2e 0a 20 20 ro is signed..
b3e20 20 20 2a 2a 20 41 64 64 20 74 68 65 20 73 69 67 ** Add the sig
b3e30 6e 20 69 66 20 77 65 27 76 65 20 73 65 65 6e 20 n if we've seen
b3e40 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 64 69 67 at least one dig
b3e50 69 74 20 2a 2f 0a 20 20 20 20 72 65 73 75 6c 74 it */. result
b3e60 20 3d 20 28 73 69 67 6e 3c 30 20 26 26 20 6e 44 = (sign<0 && nD
b3e70 69 67 69 74 73 29 20 3f 20 2d 28 64 6f 75 62 6c igits) ? -(doubl
b3e80 65 29 30 20 3a 20 28 64 6f 75 62 6c 65 29 30 3b e)0 : (double)0;
b3e90 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 . } else {.
b3ea0 2f 2a 20 61 74 74 65 6d 70 74 20 74 6f 20 72 65 /* attempt to re
b3eb0 64 75 63 65 20 65 78 70 6f 6e 65 6e 74 20 2a 2f duce exponent */
b3ec0 0a 20 20 20 20 69 66 28 20 65 73 69 67 6e 3e 30 . if( esign>0
b3ed0 20 29 7b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 ){. while(
b3ee0 20 73 3c 28 4c 41 52 47 45 53 54 5f 49 4e 54 36 s<(LARGEST_INT6
b3ef0 34 2f 31 30 29 20 26 26 20 65 3e 30 20 29 20 65 4/10) && e>0 ) e
b3f00 2d 2d 2c 73 2a 3d 31 30 3b 0a 20 20 20 20 7d 65 --,s*=10;. }e
b3f10 6c 73 65 7b 0a 20 20 20 20 20 20 77 68 69 6c 65 lse{. while
b3f20 28 20 21 28 73 25 31 30 29 20 26 26 20 65 3e 30 ( !(s%10) && e>0
b3f30 20 29 20 65 2d 2d 2c 73 2f 3d 31 30 3b 0a 20 20 ) e--,s/=10;.
b3f40 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 61 64 6a 75 }.. /* adju
b3f50 73 74 20 74 68 65 20 73 69 67 6e 20 6f 66 20 73 st the sign of s
b3f60 69 67 6e 69 66 69 63 61 6e 64 20 2a 2f 0a 20 20 ignificand */.
b3f70 20 20 73 20 3d 20 73 69 67 6e 3c 30 20 3f 20 2d s = sign<0 ? -
b3f80 73 20 3a 20 73 3b 0a 0a 20 20 20 20 2f 2a 20 69 s : s;.. /* i
b3f90 66 20 65 78 70 6f 6e 65 6e 74 2c 20 73 63 61 6c f exponent, scal
b3fa0 65 20 73 69 67 6e 69 66 69 63 61 6e 64 20 61 73 e significand as
b3fb0 20 61 70 70 72 6f 70 72 69 61 74 65 0a 20 20 20 appropriate.
b3fc0 20 2a 2a 20 61 6e 64 20 73 74 6f 72 65 20 69 6e ** and store in
b3fd0 20 72 65 73 75 6c 74 2e 20 2a 2f 0a 20 20 20 20 result. */.
b3fe0 69 66 28 20 65 20 29 7b 0a 20 20 20 20 20 20 64 if( e ){. d
b3ff0 6f 75 62 6c 65 20 73 63 61 6c 65 20 3d 20 31 2e ouble scale = 1.
b4000 30 3b 0a 20 20 20 20 20 20 2f 2a 20 61 74 74 65 0;. /* atte
b4010 6d 70 74 20 74 6f 20 68 61 6e 64 6c 65 20 65 78 mpt to handle ex
b4020 74 72 65 6d 65 6c 79 20 73 6d 61 6c 6c 2f 6c 61 tremely small/la
b4030 72 67 65 20 6e 75 6d 62 65 72 73 20 62 65 74 74 rge numbers bett
b4040 65 72 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 er */. if(
b4050 65 3e 33 30 37 20 26 26 20 65 3c 33 34 32 20 29 e>307 && e<342 )
b4060 7b 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 {. while(
b4070 20 65 25 33 30 38 20 29 20 7b 20 73 63 61 6c 65 e%308 ) { scale
b4080 20 2a 3d 20 31 2e 30 65 2b 31 3b 20 65 20 2d 3d *= 1.0e+1; e -=
b4090 20 31 3b 20 7d 0a 20 20 20 20 20 20 20 20 69 66 1; }. if
b40a0 28 20 65 73 69 67 6e 3c 30 20 29 7b 0a 20 20 20 ( esign<0 ){.
b40b0 20 20 20 20 20 20 20 72 65 73 75 6c 74 20 3d 20 result =
b40c0 73 20 2f 20 73 63 61 6c 65 3b 0a 20 20 20 20 20 s / scale;.
b40d0 20 20 20 20 20 72 65 73 75 6c 74 20 2f 3d 20 31 result /= 1
b40e0 2e 30 65 2b 33 30 38 3b 0a 20 20 20 20 20 20 20 .0e+308;.
b40f0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
b4100 20 20 72 65 73 75 6c 74 20 3d 20 73 20 2a 20 73 result = s * s
b4110 63 61 6c 65 3b 0a 20 20 20 20 20 20 20 20 20 20 cale;.
b4120 72 65 73 75 6c 74 20 2a 3d 20 31 2e 30 65 2b 33 result *= 1.0e+3
b4130 30 38 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 08;. }.
b4140 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
b4150 20 20 20 2f 2a 20 31 2e 30 65 2b 32 32 20 69 73 /* 1.0e+22 is
b4160 20 74 68 65 20 6c 61 72 67 65 73 74 20 70 6f 77 the largest pow
b4170 65 72 20 6f 66 20 31 30 20 74 68 61 6e 20 63 61 er of 10 than ca
b4180 6e 20 62 65 20 0a 20 20 20 20 20 20 20 20 2a 2a n be . **
b4190 20 72 65 70 72 65 73 65 6e 74 65 64 20 65 78 61 represented exa
b41a0 63 74 6c 79 2e 20 2a 2f 0a 20 20 20 20 20 20 20 ctly. */.
b41b0 20 77 68 69 6c 65 28 20 65 25 32 32 20 29 20 7b while( e%22 ) {
b41c0 20 73 63 61 6c 65 20 2a 3d 20 31 2e 30 65 2b 31 scale *= 1.0e+1
b41d0 3b 20 65 20 2d 3d 20 31 3b 20 7d 0a 20 20 20 20 ; e -= 1; }.
b41e0 20 20 20 20 77 68 69 6c 65 28 20 65 3e 30 20 29 while( e>0 )
b41f0 20 7b 20 73 63 61 6c 65 20 2a 3d 20 31 2e 30 65 { scale *= 1.0e
b4200 2b 32 32 3b 20 65 20 2d 3d 20 32 32 3b 20 7d 0a +22; e -= 22; }.
b4210 20 20 20 20 20 20 20 20 69 66 28 20 65 73 69 67 if( esig
b4220 6e 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 n<0 ){.
b4230 20 72 65 73 75 6c 74 20 3d 20 73 20 2f 20 73 63 result = s / sc
b4240 61 6c 65 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c ale;. }el
b4250 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 se{. re
b4260 73 75 6c 74 20 3d 20 73 20 2a 20 73 63 61 6c 65 sult = s * scale
b4270 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
b4280 20 20 7d 0a 20 20 20 20 7d 20 65 6c 73 65 20 7b }. } else {
b4290 0a 20 20 20 20 20 20 72 65 73 75 6c 74 20 3d 20 . result =
b42a0 28 64 6f 75 62 6c 65 29 73 3b 0a 20 20 20 20 7d (double)s;. }
b42b0 0a 20 20 7d 0a 0a 20 20 2f 2a 20 73 74 6f 72 65 . }.. /* store
b42c0 20 74 68 65 20 72 65 73 75 6c 74 20 2a 2f 0a 20 the result */.
b42d0 20 2a 70 52 65 73 75 6c 74 20 3d 20 72 65 73 75 *pResult = resu
b42e0 6c 74 3b 0a 0a 20 20 2f 2a 20 72 65 74 75 72 6e lt;.. /* return
b42f0 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 number of chara
b4300 63 74 65 72 73 20 75 73 65 64 20 2a 2f 0a 20 20 cters used */.
b4310 72 65 74 75 72 6e 20 28 69 6e 74 29 28 7a 20 2d return (int)(z -
b4320 20 7a 42 65 67 69 6e 29 3b 0a 23 65 6c 73 65 0a zBegin);.#else.
b4330 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 return sqlite3
b4340 41 74 6f 69 36 34 28 7a 2c 20 70 52 65 73 75 6c Atoi64(z, pResul
b4350 74 29 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 t);.#endif /* SQ
b4360 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 LITE_OMIT_FLOATI
b4370 4e 47 5f 50 4f 49 4e 54 20 2a 2f 0a 7d 0a 0a 2f NG_POINT */.}../
b4380 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65 20 74 68 65 *.** Compare the
b4390 20 31 39 2d 63 68 61 72 61 63 74 65 72 20 73 74 19-character st
b43a0 72 69 6e 67 20 7a 4e 75 6d 20 61 67 61 69 6e 73 ring zNum agains
b43b0 74 20 74 68 65 20 74 65 78 74 20 72 65 70 72 65 t the text repre
b43c0 73 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 76 61 6c sentation.** val
b43d0 75 65 20 32 5e 36 33 3a 20 20 39 32 32 33 33 37 ue 2^63: 922337
b43e0 32 30 33 36 38 35 34 37 37 35 38 30 38 2e 20 20 2036854775808.
b43f0 52 65 74 75 72 6e 20 6e 65 67 61 74 69 76 65 2c Return negative,
b4400 20 7a 65 72 6f 2c 20 6f 72 20 70 6f 73 69 74 69 zero, or positi
b4410 76 65 0a 2a 2a 20 69 66 20 7a 4e 75 6d 20 69 73 ve.** if zNum is
b4420 20 6c 65 73 73 20 74 68 61 6e 2c 20 65 71 75 61 less than, equa
b4430 6c 20 74 6f 2c 20 6f 72 20 67 72 65 61 74 65 72 l to, or greater
b4440 20 74 68 61 6e 20 74 68 65 20 73 74 72 69 6e 67 than the string
b4450 2e 0a 2a 2a 0a 2a 2a 20 55 6e 6c 69 6b 65 20 6d ..**.** Unlike m
b4460 65 6d 63 6d 70 28 29 20 74 68 69 73 20 72 6f 75 emcmp() this rou
b4470 74 69 6e 65 20 69 73 20 67 75 61 72 61 6e 74 65 tine is guarante
b4480 65 64 20 74 6f 20 72 65 74 75 72 6e 20 74 68 65 ed to return the
b4490 20 64 69 66 66 65 72 65 6e 63 65 0a 2a 2a 20 69 difference.** i
b44a0 6e 20 74 68 65 20 76 61 6c 75 65 73 20 6f 66 20 n the values of
b44b0 74 68 65 20 6c 61 73 74 20 64 69 67 69 74 20 69 the last digit i
b44c0 66 20 74 68 65 20 6f 6e 6c 79 20 64 69 66 66 65 f the only diffe
b44d0 72 65 6e 63 65 20 69 73 20 69 6e 20 74 68 65 0a rence is in the.
b44e0 2a 2a 20 6c 61 73 74 20 64 69 67 69 74 2e 20 20 ** last digit.
b44f0 53 6f 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c So, for example,
b4500 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 63 6f 6d 70 .**.** comp
b4510 61 72 65 32 70 6f 77 36 33 28 22 39 32 32 33 33 are2pow63("92233
b4520 37 32 30 33 36 38 35 34 37 37 35 38 30 30 22 29 72036854775800")
b4530 0a 2a 2a 0a 2a 2a 20 77 69 6c 6c 20 72 65 74 75 .**.** will retu
b4540 72 6e 20 2d 38 2e 0a 2a 2f 0a 73 74 61 74 69 63 rn -8..*/.static
b4550 20 69 6e 74 20 63 6f 6d 70 61 72 65 32 70 6f 77 int compare2pow
b4560 36 33 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 63(const char *z
b4570 4e 75 6d 29 7b 0a 20 20 69 6e 74 20 63 3b 0a 20 Num){. int c;.
b4580 20 63 20 3d 20 6d 65 6d 63 6d 70 28 7a 4e 75 6d c = memcmp(zNum
b4590 2c 22 39 32 32 33 33 37 32 30 33 36 38 35 34 37 ,"92233720368547
b45a0 37 35 38 30 22 2c 31 38 29 2a 31 30 3b 0a 20 20 7580",18)*10;.
b45b0 69 66 28 20 63 3d 3d 30 20 29 7b 0a 20 20 20 20 if( c==0 ){.
b45c0 63 20 3d 20 7a 4e 75 6d 5b 31 38 5d 20 2d 20 27 c = zNum[18] - '
b45d0 38 27 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 8';. }. return
b45e0 20 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 c;.}.../*.** Re
b45f0 74 75 72 6e 20 54 52 55 45 20 69 66 20 7a 4e 75 turn TRUE if zNu
b4600 6d 20 69 73 20 61 20 36 34 2d 62 69 74 20 73 69 m is a 64-bit si
b4610 67 6e 65 64 20 69 6e 74 65 67 65 72 20 61 6e 64 gned integer and
b4620 20 77 72 69 74 65 0a 2a 2a 20 74 68 65 20 76 61 write.** the va
b4630 6c 75 65 20 6f 66 20 74 68 65 20 69 6e 74 65 67 lue of the integ
b4640 65 72 20 69 6e 74 6f 20 2a 70 4e 75 6d 2e 20 20 er into *pNum.
b4650 49 66 20 7a 4e 75 6d 20 69 73 20 6e 6f 74 20 61 If zNum is not a
b4660 6e 20 69 6e 74 65 67 65 72 0a 2a 2a 20 6f 72 20 n integer.** or
b4670 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 74 68 is an integer th
b4680 61 74 20 69 73 20 74 6f 6f 20 6c 61 72 67 65 20 at is too large
b4690 74 6f 20 62 65 20 65 78 70 72 65 73 73 65 64 20 to be expressed
b46a0 77 69 74 68 20 36 34 20 62 69 74 73 2c 0a 2a 2a with 64 bits,.**
b46b0 20 74 68 65 6e 20 72 65 74 75 72 6e 20 66 61 6c then return fal
b46c0 73 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 se..**.** When t
b46d0 68 69 73 20 72 6f 75 74 69 6e 65 20 77 61 73 20 his routine was
b46e0 6f 72 69 67 69 6e 61 6c 6c 79 20 77 72 69 74 74 originally writt
b46f0 65 6e 20 69 74 20 64 65 61 6c 74 20 77 69 74 68 en it dealt with
b4700 20 6f 6e 6c 79 0a 2a 2a 20 33 32 2d 62 69 74 20 only.** 32-bit
b4710 6e 75 6d 62 65 72 73 2e 20 20 41 74 20 74 68 61 numbers. At tha
b4720 74 20 74 69 6d 65 2c 20 69 74 20 77 61 73 20 6d t time, it was m
b4730 75 63 68 20 66 61 73 74 65 72 20 74 68 61 6e 20 uch faster than
b4740 74 68 65 0a 2a 2a 20 61 74 6f 69 28 29 20 6c 69 the.** atoi() li
b4750 62 72 61 72 79 20 72 6f 75 74 69 6e 65 20 69 6e brary routine in
b4760 20 52 65 64 48 61 74 20 37 2e 32 2e 0a 2a 2f 0a RedHat 7.2..*/.
b4770 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
b4780 6e 74 20 73 71 6c 69 74 65 33 41 74 6f 69 36 34 nt sqlite3Atoi64
b4790 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 75 (const char *zNu
b47a0 6d 2c 20 69 36 34 20 2a 70 4e 75 6d 29 7b 0a 20 m, i64 *pNum){.
b47b0 20 69 36 34 20 76 20 3d 20 30 3b 0a 20 20 69 6e i64 v = 0;. in
b47c0 74 20 6e 65 67 3b 0a 20 20 69 6e 74 20 69 2c 20 t neg;. int i,
b47d0 63 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 c;. const char
b47e0 2a 7a 53 74 61 72 74 3b 0a 20 20 77 68 69 6c 65 *zStart;. while
b47f0 28 20 73 71 6c 69 74 65 33 49 73 73 70 61 63 65 ( sqlite3Isspace
b4800 28 2a 7a 4e 75 6d 29 20 29 20 7a 4e 75 6d 2b 2b (*zNum) ) zNum++
b4810 3b 0a 20 20 69 66 28 20 2a 7a 4e 75 6d 3d 3d 27 ;. if( *zNum=='
b4820 2d 27 20 29 7b 0a 20 20 20 20 6e 65 67 20 3d 20 -' ){. neg =
b4830 31 3b 0a 20 20 20 20 7a 4e 75 6d 2b 2b 3b 0a 20 1;. zNum++;.
b4840 20 7d 65 6c 73 65 20 69 66 28 20 2a 7a 4e 75 6d }else if( *zNum
b4850 3d 3d 27 2b 27 20 29 7b 0a 20 20 20 20 6e 65 67 =='+' ){. neg
b4860 20 3d 20 30 3b 0a 20 20 20 20 7a 4e 75 6d 2b 2b = 0;. zNum++
b4870 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e ;. }else{. n
b4880 65 67 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 7a 53 eg = 0;. }. zS
b4890 74 61 72 74 20 3d 20 7a 4e 75 6d 3b 0a 20 20 77 tart = zNum;. w
b48a0 68 69 6c 65 28 20 7a 4e 75 6d 5b 30 5d 3d 3d 27 hile( zNum[0]=='
b48b0 30 27 20 29 7b 20 7a 4e 75 6d 2b 2b 3b 20 7d 20 0' ){ zNum++; }
b48c0 2f 2a 20 53 6b 69 70 20 6f 76 65 72 20 6c 65 61 /* Skip over lea
b48d0 64 69 6e 67 20 7a 65 72 6f 73 2e 20 54 69 63 6b ding zeros. Tick
b48e0 65 74 20 23 32 34 35 34 20 2a 2f 0a 20 20 66 6f et #2454 */. fo
b48f0 72 28 69 3d 30 3b 20 28 63 3d 7a 4e 75 6d 5b 69 r(i=0; (c=zNum[i
b4900 5d 29 3e 3d 27 30 27 20 26 26 20 63 3c 3d 27 39 ])>='0' && c<='9
b4910 27 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 76 20 3d '; i++){. v =
b4920 20 76 2a 31 30 20 2b 20 63 20 2d 20 27 30 27 3b v*10 + c - '0';
b4930 0a 20 20 7d 0a 20 20 2a 70 4e 75 6d 20 3d 20 6e . }. *pNum = n
b4940 65 67 20 3f 20 2d 76 20 3a 20 76 3b 0a 20 20 69 eg ? -v : v;. i
b4950 66 28 20 63 21 3d 30 20 7c 7c 20 28 69 3d 3d 30 f( c!=0 || (i==0
b4960 20 26 26 20 7a 53 74 61 72 74 3d 3d 7a 4e 75 6d && zStart==zNum
b4970 29 20 7c 7c 20 69 3e 31 39 20 29 7b 0a 20 20 20 ) || i>19 ){.
b4980 20 2f 2a 20 7a 4e 75 6d 20 69 73 20 65 6d 70 74 /* zNum is empt
b4990 79 20 6f 72 20 63 6f 6e 74 61 69 6e 73 20 6e 6f y or contains no
b49a0 6e 2d 6e 75 6d 65 72 69 63 20 74 65 78 74 20 6f n-numeric text o
b49b0 72 20 69 73 20 6c 6f 6e 67 65 72 0a 20 20 20 20 r is longer.
b49c0 2a 2a 20 74 68 61 6e 20 31 39 20 64 69 67 69 74 ** than 19 digit
b49d0 73 20 28 74 68 75 73 20 67 75 61 72 61 6e 74 69 s (thus guaranti
b49e0 6e 67 20 74 68 61 74 20 69 74 20 69 73 20 74 6f ng that it is to
b49f0 6f 20 6c 61 72 67 65 29 20 2a 2f 0a 20 20 20 20 o large) */.
b4a00 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65 6c 73 return 0;. }els
b4a10 65 20 69 66 28 20 69 3c 31 39 20 29 7b 0a 20 20 e if( i<19 ){.
b4a20 20 20 2f 2a 20 4c 65 73 73 20 74 68 61 6e 20 31 /* Less than 1
b4a30 39 20 64 69 67 69 74 73 2c 20 73 6f 20 77 65 20 9 digits, so we
b4a40 6b 6e 6f 77 20 74 68 61 74 20 69 74 20 66 69 74 know that it fit
b4a50 73 20 69 6e 20 36 34 20 62 69 74 73 20 2a 2f 0a s in 64 bits */.
b4a60 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 return 1;.
b4a70 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 31 39 }else{. /* 19
b4a80 2d 64 69 67 69 74 20 6e 75 6d 62 65 72 73 20 6d -digit numbers m
b4a90 75 73 74 20 62 65 20 6e 6f 20 6c 61 72 67 65 72 ust be no larger
b4aa0 20 74 68 61 6e 20 39 32 32 33 33 37 32 30 33 36 than 9223372036
b4ab0 38 35 34 37 37 35 38 30 37 20 69 66 20 70 6f 73 854775807 if pos
b4ac0 69 74 69 76 65 0a 20 20 20 20 2a 2a 20 6f 72 20 itive. ** or
b4ad0 39 32 32 33 33 37 32 30 33 36 38 35 34 37 37 35 9223372036854775
b4ae0 38 30 38 20 69 66 20 6e 65 67 61 74 69 76 65 2e 808 if negative.
b4af0 20 20 4e 6f 74 65 20 74 68 61 74 20 39 32 32 33 Note that 9223
b4b00 33 37 32 30 33 36 38 35 34 36 36 35 38 30 38 0a 372036854665808.
b4b10 20 20 20 20 2a 2a 20 69 73 20 32 5e 36 33 2e 20 ** is 2^63.
b4b20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 63 6f */. return co
b4b30 6d 70 61 72 65 32 70 6f 77 36 33 28 7a 4e 75 6d mpare2pow63(zNum
b4b40 29 3c 6e 65 67 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a )<neg;. }.}../*
b4b50 0a 2a 2a 20 54 68 65 20 73 74 72 69 6e 67 20 7a .** The string z
b4b60 4e 75 6d 20 72 65 70 72 65 73 65 6e 74 73 20 61 Num represents a
b4b70 6e 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 n unsigned integ
b4b80 65 72 2e 20 20 54 68 65 20 7a 4e 75 6d 20 73 74 er. The zNum st
b4b90 72 69 6e 67 0a 2a 2a 20 63 6f 6e 73 69 73 74 73 ring.** consists
b4ba0 20 6f 66 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 of one or more
b4bb0 64 69 67 69 74 20 63 68 61 72 61 63 74 65 72 73 digit characters
b4bc0 20 61 6e 64 20 69 73 20 74 65 72 6d 69 6e 61 74 and is terminat
b4bd0 65 64 20 62 79 0a 2a 2a 20 61 20 7a 65 72 6f 20 ed by.** a zero
b4be0 63 68 61 72 61 63 74 65 72 2e 20 20 41 6e 79 20 character. Any
b4bf0 73 74 72 61 79 20 63 68 61 72 61 63 74 65 72 73 stray characters
b4c00 20 69 6e 20 7a 4e 75 6d 20 72 65 73 75 6c 74 20 in zNum result
b4c10 69 6e 20 75 6e 64 65 66 69 6e 65 64 0a 2a 2a 20 in undefined.**
b4c20 62 65 68 61 76 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20 behavior..**.**
b4c30 49 66 20 74 68 65 20 75 6e 73 69 67 6e 65 64 20 If the unsigned
b4c40 69 6e 74 65 67 65 72 20 74 68 61 74 20 7a 4e 75 integer that zNu
b4c50 6d 20 72 65 70 72 65 73 65 6e 74 73 20 77 69 6c m represents wil
b4c60 6c 20 66 69 74 20 69 6e 20 61 0a 2a 2a 20 36 34 l fit in a.** 64
b4c70 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65 -bit signed inte
b4c80 67 65 72 2c 20 72 65 74 75 72 6e 20 54 52 55 45 ger, return TRUE
b4c90 2e 20 20 4f 74 68 65 72 77 69 73 65 20 72 65 74 . Otherwise ret
b4ca0 75 72 6e 20 46 41 4c 53 45 2e 0a 2a 2a 0a 2a 2a urn FALSE..**.**
b4cb0 20 49 66 20 74 68 65 20 6e 65 67 46 6c 61 67 20 If the negFlag
b4cc0 70 61 72 61 6d 65 74 65 72 20 69 73 20 74 72 75 parameter is tru
b4cd0 65 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 e, that means th
b4ce0 61 74 20 7a 4e 75 6d 20 72 65 61 6c 6c 79 20 72 at zNum really r
b4cf0 65 70 72 65 73 65 6e 74 73 0a 2a 2a 20 61 20 6e epresents.** a n
b4d00 65 67 61 74 69 76 65 20 6e 75 6d 62 65 72 2e 20 egative number.
b4d10 20 28 54 68 65 20 6c 65 61 64 69 6e 67 20 22 2d (The leading "-
b4d20 22 20 69 73 20 6f 6d 69 74 74 65 64 20 66 72 6f " is omitted fro
b4d30 6d 20 7a 4e 75 6d 2e 29 20 20 54 68 69 73 0a 2a m zNum.) This.*
b4d40 2a 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e * parameter is n
b4d50 65 65 64 65 64 20 74 6f 20 64 65 74 65 72 6d 69 eeded to determi
b4d60 6e 65 20 61 20 62 6f 75 6e 64 61 72 79 20 63 61 ne a boundary ca
b4d70 73 65 2e 20 20 41 20 73 74 72 69 6e 67 0a 2a 2a se. A string.**
b4d80 20 6f 66 20 22 39 32 32 33 33 37 33 30 33 36 38 of "92233730368
b4d90 35 34 37 37 35 38 30 38 22 20 72 65 74 75 72 6e 54775808" return
b4da0 73 20 66 61 6c 73 65 20 69 66 20 6e 65 67 46 6c s false if negFl
b4db0 61 67 20 69 73 20 66 61 6c 73 65 20 6f 72 20 74 ag is false or t
b4dc0 72 75 65 0a 2a 2a 20 69 66 20 6e 65 67 46 6c 61 rue.** if negFla
b4dd0 67 20 69 73 20 74 72 75 65 2e 0a 2a 2a 0a 2a 2a g is true..**.**
b4de0 20 4c 65 61 64 69 6e 67 20 7a 65 72 6f 73 20 61 Leading zeros a
b4df0 72 65 20 69 67 6e 6f 72 65 64 2e 0a 2a 2f 0a 53 re ignored..*/.S
b4e00 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
b4e10 74 20 73 71 6c 69 74 65 33 46 69 74 73 49 6e 36 t sqlite3FitsIn6
b4e20 34 42 69 74 73 28 63 6f 6e 73 74 20 63 68 61 72 4Bits(const char
b4e30 20 2a 7a 4e 75 6d 2c 20 69 6e 74 20 6e 65 67 46 *zNum, int negF
b4e40 6c 61 67 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 lag){. int i;.
b4e50 20 69 6e 74 20 6e 65 67 20 3d 20 30 3b 0a 0a 20 int neg = 0;..
b4e60 20 61 73 73 65 72 74 28 20 7a 4e 75 6d 5b 30 5d assert( zNum[0]
b4e70 3e 3d 27 30 27 20 26 26 20 7a 4e 75 6d 5b 30 5d >='0' && zNum[0]
b4e80 3c 3d 27 39 27 20 29 3b 20 2f 2a 20 7a 4e 75 6d <='9' ); /* zNum
b4e90 20 69 73 20 61 6e 20 75 6e 73 69 67 6e 65 64 20 is an unsigned
b4ea0 6e 75 6d 62 65 72 20 2a 2f 0a 0a 20 20 69 66 28 number */.. if(
b4eb0 20 6e 65 67 46 6c 61 67 20 29 20 6e 65 67 20 3d negFlag ) neg =
b4ec0 20 31 2d 6e 65 67 3b 0a 20 20 77 68 69 6c 65 28 1-neg;. while(
b4ed0 20 2a 7a 4e 75 6d 3d 3d 27 30 27 20 29 7b 0a 20 *zNum=='0' ){.
b4ee0 20 20 20 7a 4e 75 6d 2b 2b 3b 20 20 20 2f 2a 20 zNum++; /*
b4ef0 53 6b 69 70 20 6c 65 61 64 69 6e 67 20 7a 65 72 Skip leading zer
b4f00 6f 73 2e 20 20 54 69 63 6b 65 74 20 23 32 34 35 os. Ticket #245
b4f10 34 20 2a 2f 0a 20 20 7d 0a 20 20 66 6f 72 28 69 4 */. }. for(i
b4f20 3d 30 3b 20 7a 4e 75 6d 5b 69 5d 3b 20 69 2b 2b =0; zNum[i]; i++
b4f30 29 7b 20 61 73 73 65 72 74 28 20 7a 4e 75 6d 5b ){ assert( zNum[
b4f40 69 5d 3e 3d 27 30 27 20 26 26 20 7a 4e 75 6d 5b i]>='0' && zNum[
b4f50 69 5d 3c 3d 27 39 27 20 29 3b 20 7d 0a 20 20 69 i]<='9' ); }. i
b4f60 66 28 20 69 3c 31 39 20 29 7b 0a 20 20 20 20 2f f( i<19 ){. /
b4f70 2a 20 47 75 61 72 61 6e 74 65 65 64 20 74 6f 20 * Guaranteed to
b4f80 66 69 74 20 69 66 20 6c 65 73 73 20 74 68 61 6e fit if less than
b4f90 20 31 39 20 64 69 67 69 74 73 20 2a 2f 0a 20 20 19 digits */.
b4fa0 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 65 return 1;. }e
b4fb0 6c 73 65 20 69 66 28 20 69 3e 31 39 20 29 7b 0a lse if( i>19 ){.
b4fc0 20 20 20 20 2f 2a 20 47 75 61 72 61 6e 74 65 65 /* Guarantee
b4fd0 64 20 74 6f 20 62 65 20 74 6f 6f 20 62 69 67 20 d to be too big
b4fe0 69 66 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 if greater than
b4ff0 31 39 20 64 69 67 69 74 73 20 2a 2f 0a 20 20 20 19 digits */.
b5000 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65 6c return 0;. }el
b5010 73 65 7b 0a 20 20 20 20 2f 2a 20 43 6f 6d 70 61 se{. /* Compa
b5020 72 65 20 61 67 61 69 6e 73 74 20 32 5e 36 33 2e re against 2^63.
b5030 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 63 */. return c
b5040 6f 6d 70 61 72 65 32 70 6f 77 36 33 28 7a 4e 75 ompare2pow63(zNu
b5050 6d 29 3c 6e 65 67 3b 0a 20 20 7d 0a 7d 0a 0a 2f m)<neg;. }.}../
b5060 2a 0a 2a 2a 20 49 66 20 7a 4e 75 6d 20 72 65 70 *.** If zNum rep
b5070 72 65 73 65 6e 74 73 20 61 6e 20 69 6e 74 65 67 resents an integ
b5080 65 72 20 74 68 61 74 20 77 69 6c 6c 20 66 69 74 er that will fit
b5090 20 69 6e 20 33 32 2d 62 69 74 73 2c 20 74 68 65 in 32-bits, the
b50a0 6e 20 73 65 74 0a 2a 2a 20 2a 70 56 61 6c 75 65 n set.** *pValue
b50b0 20 74 6f 20 74 68 61 74 20 69 6e 74 65 67 65 72 to that integer
b50c0 20 61 6e 64 20 72 65 74 75 72 6e 20 74 72 75 65 and return true
b50d0 2e 20 20 4f 74 68 65 72 77 69 73 65 20 72 65 74 . Otherwise ret
b50e0 75 72 6e 20 66 61 6c 73 65 2e 0a 2a 2a 0a 2a 2a urn false..**.**
b50f0 20 41 6e 79 20 6e 6f 6e 2d 6e 75 6d 65 72 69 63 Any non-numeric
b5100 20 63 68 61 72 61 63 74 65 72 73 20 74 68 61 74 characters that
b5110 20 66 6f 6c 6c 6f 77 69 6e 67 20 7a 4e 75 6d 20 following zNum
b5120 61 72 65 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a 20 are ignored..**
b5130 54 68 69 73 20 69 73 20 64 69 66 66 65 72 65 6e This is differen
b5140 74 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 41 74 t from sqlite3At
b5150 6f 69 36 34 28 29 20 77 68 69 63 68 20 72 65 71 oi64() which req
b5160 75 69 72 65 73 20 74 68 65 0a 2a 2a 20 69 6e 70 uires the.** inp
b5170 75 74 20 6e 75 6d 62 65 72 20 74 6f 20 62 65 20 ut number to be
b5180 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 2e zero-terminated.
b5190 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
b51a0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 47 65 TE int sqlite3Ge
b51b0 74 49 6e 74 33 32 28 63 6f 6e 73 74 20 63 68 61 tInt32(const cha
b51c0 72 20 2a 7a 4e 75 6d 2c 20 69 6e 74 20 2a 70 56 r *zNum, int *pV
b51d0 61 6c 75 65 29 7b 0a 20 20 73 71 6c 69 74 65 5f alue){. sqlite_
b51e0 69 6e 74 36 34 20 76 20 3d 20 30 3b 0a 20 20 69 int64 v = 0;. i
b51f0 6e 74 20 69 2c 20 63 3b 0a 20 20 69 6e 74 20 6e nt i, c;. int n
b5200 65 67 20 3d 20 30 3b 0a 20 20 69 66 28 20 7a 4e eg = 0;. if( zN
b5210 75 6d 5b 30 5d 3d 3d 27 2d 27 20 29 7b 0a 20 20 um[0]=='-' ){.
b5220 20 20 6e 65 67 20 3d 20 31 3b 0a 20 20 20 20 7a neg = 1;. z
b5230 4e 75 6d 2b 2b 3b 0a 20 20 7d 65 6c 73 65 20 69 Num++;. }else i
b5240 66 28 20 7a 4e 75 6d 5b 30 5d 3d 3d 27 2b 27 20 f( zNum[0]=='+'
b5250 29 7b 0a 20 20 20 20 7a 4e 75 6d 2b 2b 3b 0a 20 ){. zNum++;.
b5260 20 7d 0a 20 20 77 68 69 6c 65 28 20 7a 4e 75 6d }. while( zNum
b5270 5b 30 5d 3d 3d 27 30 27 20 29 20 7a 4e 75 6d 2b [0]=='0' ) zNum+
b5280 2b 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c +;. for(i=0; i<
b5290 31 31 20 26 26 20 28 63 20 3d 20 7a 4e 75 6d 5b 11 && (c = zNum[
b52a0 69 5d 20 2d 20 27 30 27 29 3e 3d 30 20 26 26 20 i] - '0')>=0 &&
b52b0 63 3c 3d 39 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 c<=9; i++){.
b52c0 76 20 3d 20 76 2a 31 30 20 2b 20 63 3b 0a 20 20 v = v*10 + c;.
b52d0 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 6c 6f 6e 67 }.. /* The long
b52e0 65 73 74 20 64 65 63 69 6d 61 6c 20 72 65 70 72 est decimal repr
b52f0 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 20 esentation of a
b5300 33 32 20 62 69 74 20 69 6e 74 65 67 65 72 20 69 32 bit integer i
b5310 73 20 31 30 20 64 69 67 69 74 73 3a 0a 20 20 2a s 10 digits:. *
b5320 2a 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 *. **
b5330 20 20 20 31 32 33 34 35 36 37 38 39 30 0a 20 20 1234567890.
b5340 2a 2a 20 20 20 20 20 32 5e 33 31 20 2d 3e 20 32 ** 2^31 -> 2
b5350 31 34 37 34 38 33 36 34 38 0a 20 20 2a 2f 0a 20 147483648. */.
b5360 20 69 66 28 20 69 3e 31 30 20 29 7b 0a 20 20 20 if( i>10 ){.
b5370 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 return 0;. }.
b5380 20 69 66 28 20 76 2d 6e 65 67 3e 32 31 34 37 34 if( v-neg>21474
b5390 38 33 36 34 37 20 29 7b 0a 20 20 20 20 72 65 74 83647 ){. ret
b53a0 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 urn 0;. }. if(
b53b0 20 6e 65 67 20 29 7b 0a 20 20 20 20 76 20 3d 20 neg ){. v =
b53c0 2d 76 3b 0a 20 20 7d 0a 20 20 2a 70 56 61 6c 75 -v;. }. *pValu
b53d0 65 20 3d 20 28 69 6e 74 29 76 3b 0a 20 20 72 65 e = (int)v;. re
b53e0 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a turn 1;.}../*.**
b53f0 20 54 68 65 20 76 61 72 69 61 62 6c 65 2d 6c 65 The variable-le
b5400 6e 67 74 68 20 69 6e 74 65 67 65 72 20 65 6e 63 ngth integer enc
b5410 6f 64 69 6e 67 20 69 73 20 61 73 20 66 6f 6c 6c oding is as foll
b5420 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 4b 45 59 3a 0a ows:.**.** KEY:.
b5430 2a 2a 20 20 20 20 20 20 20 20 20 41 20 3d 20 30 ** A = 0
b5440 78 78 78 78 78 78 78 20 20 20 20 37 20 62 69 74 xxxxxxx 7 bit
b5450 73 20 6f 66 20 64 61 74 61 20 61 6e 64 20 6f 6e s of data and on
b5460 65 20 66 6c 61 67 20 62 69 74 0a 2a 2a 20 20 20 e flag bit.**
b5470 20 20 20 20 20 20 42 20 3d 20 31 78 78 78 78 78 B = 1xxxxx
b5480 78 78 20 20 20 20 37 20 62 69 74 73 20 6f 66 20 xx 7 bits of
b5490 64 61 74 61 20 61 6e 64 20 6f 6e 65 20 66 6c 61 data and one fla
b54a0 67 20 62 69 74 0a 2a 2a 20 20 20 20 20 20 20 20 g bit.**
b54b0 20 43 20 3d 20 78 78 78 78 78 78 78 78 20 20 20 C = xxxxxxxx
b54c0 20 38 20 62 69 74 73 20 6f 66 20 64 61 74 61 0a 8 bits of data.
b54d0 2a 2a 0a 2a 2a 20 20 37 20 62 69 74 73 20 2d 20 **.** 7 bits -
b54e0 41 0a 2a 2a 20 31 34 20 62 69 74 73 20 2d 20 42 A.** 14 bits - B
b54f0 41 0a 2a 2a 20 32 31 20 62 69 74 73 20 2d 20 42 A.** 21 bits - B
b5500 42 41 0a 2a 2a 20 32 38 20 62 69 74 73 20 2d 20 BA.** 28 bits -
b5510 42 42 42 41 0a 2a 2a 20 33 35 20 62 69 74 73 20 BBBA.** 35 bits
b5520 2d 20 42 42 42 42 41 0a 2a 2a 20 34 32 20 62 69 - BBBBA.** 42 bi
b5530 74 73 20 2d 20 42 42 42 42 42 41 0a 2a 2a 20 34 ts - BBBBBA.** 4
b5540 39 20 62 69 74 73 20 2d 20 42 42 42 42 42 42 41 9 bits - BBBBBBA
b5550 0a 2a 2a 20 35 36 20 62 69 74 73 20 2d 20 42 42 .** 56 bits - BB
b5560 42 42 42 42 42 41 0a 2a 2a 20 36 34 20 62 69 74 BBBBBA.** 64 bit
b5570 73 20 2d 20 42 42 42 42 42 42 42 42 43 0a 2a 2f s - BBBBBBBBC.*/
b5580 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 61 20 ../*.** Write a
b5590 36 34 2d 62 69 74 20 76 61 72 69 61 62 6c 65 2d 64-bit variable-
b55a0 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 20 74 length integer t
b55b0 6f 20 6d 65 6d 6f 72 79 20 73 74 61 72 74 69 6e o memory startin
b55c0 67 20 61 74 20 70 5b 30 5d 2e 0a 2a 2a 20 54 68 g at p[0]..** Th
b55d0 65 20 6c 65 6e 67 74 68 20 6f 66 20 64 61 74 61 e length of data
b55e0 20 77 72 69 74 65 20 77 69 6c 6c 20 62 65 20 62 write will be b
b55f0 65 74 77 65 65 6e 20 31 20 61 6e 64 20 39 20 62 etween 1 and 9 b
b5600 79 74 65 73 2e 20 20 54 68 65 20 6e 75 6d 62 65 ytes. The numbe
b5610 72 0a 2a 2a 20 6f 66 20 62 79 74 65 73 20 77 72 r.** of bytes wr
b5620 69 74 74 65 6e 20 69 73 20 72 65 74 75 72 6e 65 itten is returne
b5630 64 2e 0a 2a 2a 0a 2a 2a 20 41 20 76 61 72 69 61 d..**.** A varia
b5640 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 ble-length integ
b5650 65 72 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 74 er consists of t
b5660 68 65 20 6c 6f 77 65 72 20 37 20 62 69 74 73 20 he lower 7 bits
b5670 6f 66 20 65 61 63 68 20 62 79 74 65 0a 2a 2a 20 of each byte.**
b5680 66 6f 72 20 61 6c 6c 20 62 79 74 65 73 20 74 68 for all bytes th
b5690 61 74 20 68 61 76 65 20 74 68 65 20 38 74 68 20 at have the 8th
b56a0 62 69 74 20 73 65 74 20 61 6e 64 20 6f 6e 65 20 bit set and one
b56b0 62 79 74 65 20 77 69 74 68 20 74 68 65 20 38 74 byte with the 8t
b56c0 68 0a 2a 2a 20 62 69 74 20 63 6c 65 61 72 2e 20 h.** bit clear.
b56d0 20 45 78 63 65 70 74 2c 20 69 66 20 77 65 20 67 Except, if we g
b56e0 65 74 20 74 6f 20 74 68 65 20 39 74 68 20 62 79 et to the 9th by
b56f0 74 65 2c 20 69 74 20 73 74 6f 72 65 73 20 74 68 te, it stores th
b5700 65 20 66 75 6c 6c 0a 2a 2a 20 38 20 62 69 74 73 e full.** 8 bits
b5710 20 61 6e 64 20 69 73 20 74 68 65 20 6c 61 73 74 and is the last
b5720 20 62 79 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 byte..*/.SQLITE
b5730 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
b5740 69 74 65 33 50 75 74 56 61 72 69 6e 74 28 75 6e ite3PutVarint(un
b5750 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 2c 20 signed char *p,
b5760 75 36 34 20 76 29 7b 0a 20 20 69 6e 74 20 69 2c u64 v){. int i,
b5770 20 6a 2c 20 6e 3b 0a 20 20 75 38 20 62 75 66 5b j, n;. u8 buf[
b5780 31 30 5d 3b 0a 20 20 69 66 28 20 76 20 26 20 28 10];. if( v & (
b5790 28 28 75 36 34 29 30 78 66 66 30 30 30 30 30 30 ((u64)0xff000000
b57a0 29 3c 3c 33 32 29 20 29 7b 0a 20 20 20 20 70 5b )<<32) ){. p[
b57b0 38 5d 20 3d 20 28 75 38 29 76 3b 0a 20 20 20 20 8] = (u8)v;.
b57c0 76 20 3e 3e 3d 20 38 3b 0a 20 20 20 20 66 6f 72 v >>= 8;. for
b57d0 28 69 3d 37 3b 20 69 3e 3d 30 3b 20 69 2d 2d 29 (i=7; i>=0; i--)
b57e0 7b 0a 20 20 20 20 20 20 70 5b 69 5d 20 3d 20 28 {. p[i] = (
b57f0 75 38 29 28 28 76 20 26 20 30 78 37 66 29 20 7c u8)((v & 0x7f) |
b5800 20 30 78 38 30 29 3b 0a 20 20 20 20 20 20 76 20 0x80);. v
b5810 3e 3e 3d 20 37 3b 0a 20 20 20 20 7d 0a 20 20 20 >>= 7;. }.
b5820 20 72 65 74 75 72 6e 20 39 3b 0a 20 20 7d 20 20 return 9;. }
b5830 20 20 0a 20 20 6e 20 3d 20 30 3b 0a 20 20 64 6f . n = 0;. do
b5840 7b 0a 20 20 20 20 62 75 66 5b 6e 2b 2b 5d 20 3d {. buf[n++] =
b5850 20 28 75 38 29 28 28 76 20 26 20 30 78 37 66 29 (u8)((v & 0x7f)
b5860 20 7c 20 30 78 38 30 29 3b 0a 20 20 20 20 76 20 | 0x80);. v
b5870 3e 3e 3d 20 37 3b 0a 20 20 7d 77 68 69 6c 65 28 >>= 7;. }while(
b5880 20 76 21 3d 30 20 29 3b 0a 20 20 62 75 66 5b 30 v!=0 );. buf[0
b5890 5d 20 26 3d 20 30 78 37 66 3b 0a 20 20 61 73 73 ] &= 0x7f;. ass
b58a0 65 72 74 28 20 6e 3c 3d 39 20 29 3b 0a 20 20 66 ert( n<=9 );. f
b58b0 6f 72 28 69 3d 30 2c 20 6a 3d 6e 2d 31 3b 20 6a or(i=0, j=n-1; j
b58c0 3e 3d 30 3b 20 6a 2d 2d 2c 20 69 2b 2b 29 7b 0a >=0; j--, i++){.
b58d0 20 20 20 20 70 5b 69 5d 20 3d 20 62 75 66 5b 6a p[i] = buf[j
b58e0 5d 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 ];. }. return
b58f0 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 n;.}../*.** This
b5900 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 66 61 routine is a fa
b5910 73 74 65 72 20 76 65 72 73 69 6f 6e 20 6f 66 20 ster version of
b5920 73 71 6c 69 74 65 33 50 75 74 56 61 72 69 6e 74 sqlite3PutVarint
b5930 28 29 20 74 68 61 74 20 6f 6e 6c 79 0a 2a 2a 20 () that only.**
b5940 77 6f 72 6b 73 20 66 6f 72 20 33 32 2d 62 69 74 works for 32-bit
b5950 20 70 6f 73 69 74 69 76 65 20 69 6e 74 65 67 65 positive intege
b5960 72 73 20 61 6e 64 20 77 68 69 63 68 20 69 73 20 rs and which is
b5970 6f 70 74 69 6d 69 7a 65 64 20 66 6f 72 0a 2a 2a optimized for.**
b5980 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 63 61 73 65 the common case
b5990 20 6f 66 20 73 6d 61 6c 6c 20 69 6e 74 65 67 65 of small intege
b59a0 72 73 2e 20 20 41 20 4d 41 43 52 4f 20 76 65 72 rs. A MACRO ver
b59b0 73 69 6f 6e 2c 20 70 75 74 56 61 72 69 6e 74 33 sion, putVarint3
b59c0 32 2c 0a 2a 2a 20 69 73 20 70 72 6f 76 69 64 65 2,.** is provide
b59d0 64 20 77 68 69 63 68 20 69 6e 6c 69 6e 65 73 20 d which inlines
b59e0 74 68 65 20 73 69 6e 67 6c 65 2d 62 79 74 65 20 the single-byte
b59f0 63 61 73 65 2e 20 20 41 6c 6c 20 63 6f 64 65 20 case. All code
b5a00 73 68 6f 75 6c 64 20 75 73 65 0a 2a 2a 20 74 68 should use.** th
b5a10 65 20 4d 41 43 52 4f 20 76 65 72 73 69 6f 6e 20 e MACRO version
b5a20 61 73 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e as this function
b5a30 20 61 73 73 75 6d 65 73 20 74 68 65 20 73 69 6e assumes the sin
b5a40 67 6c 65 2d 62 79 74 65 20 63 61 73 65 20 68 61 gle-byte case ha
b5a50 73 0a 2a 2a 20 61 6c 72 65 61 64 79 20 62 65 65 s.** already bee
b5a60 6e 20 68 61 6e 64 6c 65 64 2e 0a 2a 2f 0a 53 51 n handled..*/.SQ
b5a70 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
b5a80 20 73 71 6c 69 74 65 33 50 75 74 56 61 72 69 6e sqlite3PutVarin
b5a90 74 33 32 28 75 6e 73 69 67 6e 65 64 20 63 68 61 t32(unsigned cha
b5aa0 72 20 2a 70 2c 20 75 33 32 20 76 29 7b 0a 23 69 r *p, u32 v){.#i
b5ab0 66 6e 64 65 66 20 70 75 74 56 61 72 69 6e 74 33 fndef putVarint3
b5ac0 32 0a 20 20 69 66 28 20 28 76 20 26 20 7e 30 78 2. if( (v & ~0x
b5ad0 37 66 29 3d 3d 30 20 29 7b 0a 20 20 20 20 70 5b 7f)==0 ){. p[
b5ae0 30 5d 20 3d 20 76 3b 0a 20 20 20 20 72 65 74 75 0] = v;. retu
b5af0 72 6e 20 31 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 rn 1;. }.#endif
b5b00 0a 20 20 69 66 28 20 28 76 20 26 20 7e 30 78 33 . if( (v & ~0x3
b5b10 66 66 66 29 3d 3d 30 20 29 7b 0a 20 20 20 20 70 fff)==0 ){. p
b5b20 5b 30 5d 20 3d 20 28 75 38 29 28 28 76 3e 3e 37 [0] = (u8)((v>>7
b5b30 29 20 7c 20 30 78 38 30 29 3b 0a 20 20 20 20 70 ) | 0x80);. p
b5b40 5b 31 5d 20 3d 20 28 75 38 29 28 76 20 26 20 30 [1] = (u8)(v & 0
b5b50 78 37 66 29 3b 0a 20 20 20 20 72 65 74 75 72 6e x7f);. return
b5b60 20 32 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 2;. }. return
b5b70 20 73 71 6c 69 74 65 33 50 75 74 56 61 72 69 6e sqlite3PutVarin
b5b80 74 28 70 2c 20 76 29 3b 0a 7d 0a 0a 2f 2a 0a 2a t(p, v);.}../*.*
b5b90 2a 20 52 65 61 64 20 61 20 36 34 2d 62 69 74 20 * Read a 64-bit
b5ba0 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 variable-length
b5bb0 69 6e 74 65 67 65 72 20 66 72 6f 6d 20 6d 65 6d integer from mem
b5bc0 6f 72 79 20 73 74 61 72 74 69 6e 67 20 61 74 20 ory starting at
b5bd0 70 5b 30 5d 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 p[0]..** Return
b5be0 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 the number of by
b5bf0 74 65 73 20 72 65 61 64 2e 20 20 54 68 65 20 76 tes read. The v
b5c00 61 6c 75 65 20 69 73 20 73 74 6f 72 65 64 20 69 alue is stored i
b5c10 6e 20 2a 76 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f n *v..*/.SQLITE_
b5c20 50 52 49 56 41 54 45 20 75 38 20 73 71 6c 69 74 PRIVATE u8 sqlit
b5c30 65 33 47 65 74 56 61 72 69 6e 74 28 63 6f 6e 73 e3GetVarint(cons
b5c40 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 t unsigned char
b5c50 2a 70 2c 20 75 36 34 20 2a 76 29 7b 0a 20 20 75 *p, u64 *v){. u
b5c60 33 32 20 61 2c 62 2c 73 3b 0a 0a 20 20 61 20 3d 32 a,b,s;.. a =
b5c70 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a 20 70 30 20 *p;. /* a: p0
b5c80 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 (unmasked) */.
b5c90 69 66 20 28 21 28 61 26 30 78 38 30 29 29 0a 20 if (!(a&0x80)).
b5ca0 20 7b 0a 20 20 20 20 2a 76 20 3d 20 61 3b 0a 20 {. *v = a;.
b5cb0 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d return 1;. }
b5cc0 0a 0a 20 20 70 2b 2b 3b 0a 20 20 62 20 3d 20 2a .. p++;. b = *
b5cd0 70 3b 0a 20 20 2f 2a 20 62 3a 20 70 31 20 28 75 p;. /* b: p1 (u
b5ce0 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 nmasked) */. if
b5cf0 20 28 21 28 62 26 30 78 38 30 29 29 0a 20 20 7b (!(b&0x80)). {
b5d00 0a 20 20 20 20 61 20 26 3d 20 30 78 37 66 3b 0a . a &= 0x7f;.
b5d10 20 20 20 20 61 20 3d 20 61 3c 3c 37 3b 0a 20 20 a = a<<7;.
b5d20 20 20 61 20 7c 3d 20 62 3b 0a 20 20 20 20 2a 76 a |= b;. *v
b5d30 20 3d 20 61 3b 0a 20 20 20 20 72 65 74 75 72 6e = a;. return
b5d40 20 32 3b 0a 20 20 7d 0a 0a 20 20 70 2b 2b 3b 0a 2;. }.. p++;.
b5d50 20 20 61 20 3d 20 61 3c 3c 31 34 3b 0a 20 20 61 a = a<<14;. a
b5d60 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a 20 |= *p;. /* a:
b5d70 70 30 3c 3c 31 34 20 7c 20 70 32 20 28 75 6e 6d p0<<14 | p2 (unm
b5d80 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 asked) */. if (
b5d90 21 28 61 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 !(a&0x80)). {.
b5da0 20 20 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 a &= (0x7f<<1
b5db0 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 62 4)|(0x7f);. b
b5dc0 20 26 3d 20 30 78 37 66 3b 0a 20 20 20 20 62 20 &= 0x7f;. b
b5dd0 3d 20 62 3c 3c 37 3b 0a 20 20 20 20 61 20 7c 3d = b<<7;. a |=
b5de0 20 62 3b 0a 20 20 20 20 2a 76 20 3d 20 61 3b 0a b;. *v = a;.
b5df0 20 20 20 20 72 65 74 75 72 6e 20 33 3b 0a 20 20 return 3;.
b5e00 7d 0a 0a 20 20 2f 2a 20 43 53 45 31 20 66 72 6f }.. /* CSE1 fro
b5e10 6d 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 61 20 26 m below */. a &
b5e20 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 = (0x7f<<14)|(0x
b5e30 37 66 29 3b 0a 20 20 70 2b 2b 3b 0a 20 20 62 20 7f);. p++;. b
b5e40 3d 20 62 3c 3c 31 34 3b 0a 20 20 62 20 7c 3d 20 = b<<14;. b |=
b5e50 2a 70 3b 0a 20 20 2f 2a 20 62 3a 20 70 31 3c 3c *p;. /* b: p1<<
b5e60 31 34 20 7c 20 70 33 20 28 75 6e 6d 61 73 6b 65 14 | p3 (unmaske
b5e70 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 62 26 d) */. if (!(b&
b5e80 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 62 0x80)). {. b
b5e90 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 &= (0x7f<<14)|(
b5ea0 30 78 37 66 29 3b 0a 20 20 20 20 2f 2a 20 6d 6f 0x7f);. /* mo
b5eb0 76 65 64 20 43 53 45 31 20 75 70 20 2a 2f 0a 20 ved CSE1 up */.
b5ec0 20 20 20 2f 2a 20 61 20 26 3d 20 28 30 78 37 66 /* a &= (0x7f
b5ed0 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 20 2a 2f <<14)|(0x7f); */
b5ee0 0a 20 20 20 20 61 20 3d 20 61 3c 3c 37 3b 0a 20 . a = a<<7;.
b5ef0 20 20 20 61 20 7c 3d 20 62 3b 0a 20 20 20 20 2a a |= b;. *
b5f00 76 20 3d 20 61 3b 0a 20 20 20 20 72 65 74 75 72 v = a;. retur
b5f10 6e 20 34 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 61 n 4;. }.. /* a
b5f20 3a 20 70 30 3c 3c 31 34 20 7c 20 70 32 20 28 6d : p0<<14 | p2 (m
b5f30 61 73 6b 65 64 29 20 2a 2f 0a 20 20 2f 2a 20 62 asked) */. /* b
b5f40 3a 20 70 31 3c 3c 31 34 20 7c 20 70 33 20 28 75 : p1<<14 | p3 (u
b5f50 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 2f 2a nmasked) */. /*
b5f60 20 31 3a 73 61 76 65 20 6f 66 66 20 70 30 3c 3c 1:save off p0<<
b5f70 32 31 20 7c 20 70 31 3c 3c 31 34 20 7c 20 70 32 21 | p1<<14 | p2
b5f80 3c 3c 37 20 7c 20 70 33 20 28 6d 61 73 6b 65 64 <<7 | p3 (masked
b5f90 29 20 2a 2f 0a 20 20 2f 2a 20 6d 6f 76 65 64 20 ) */. /* moved
b5fa0 43 53 45 31 20 75 70 20 2a 2f 0a 20 20 2f 2a 20 CSE1 up */. /*
b5fb0 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c a &= (0x7f<<14)|
b5fc0 28 30 78 37 66 29 3b 20 2a 2f 0a 20 20 62 20 26 (0x7f); */. b &
b5fd0 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 = (0x7f<<14)|(0x
b5fe0 37 66 29 3b 0a 20 20 73 20 3d 20 61 3b 0a 20 20 7f);. s = a;.
b5ff0 2f 2a 20 73 3a 20 70 30 3c 3c 31 34 20 7c 20 70 /* s: p0<<14 | p
b6000 32 20 28 6d 61 73 6b 65 64 29 20 2a 2f 0a 0a 20 2 (masked) */..
b6010 20 70 2b 2b 3b 0a 20 20 61 20 3d 20 61 3c 3c 31 p++;. a = a<<1
b6020 34 3b 0a 20 20 61 20 7c 3d 20 2a 70 3b 0a 20 20 4;. a |= *p;.
b6030 2f 2a 20 61 3a 20 70 30 3c 3c 32 38 20 7c 20 70 /* a: p0<<28 | p
b6040 32 3c 3c 31 34 20 7c 20 70 34 20 28 75 6e 6d 61 2<<14 | p4 (unma
b6050 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 sked) */. if (!
b6060 28 61 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 (a&0x80)). {.
b6070 20 20 2f 2a 20 77 65 20 63 61 6e 20 73 6b 69 70 /* we can skip
b6080 20 74 68 65 73 65 20 63 61 75 73 65 20 74 68 65 these cause the
b6090 79 20 77 65 72 65 20 28 65 66 66 65 63 74 69 76 y were (effectiv
b60a0 65 6c 79 29 20 64 6f 6e 65 20 61 62 6f 76 65 20 ely) done above
b60b0 69 6e 20 63 61 6c 63 27 69 6e 67 20 73 20 2a 2f in calc'ing s */
b60c0 0a 20 20 20 20 2f 2a 20 61 20 26 3d 20 28 30 78 . /* a &= (0x
b60d0 37 66 3c 3c 32 38 29 7c 28 30 78 37 66 3c 3c 31 7f<<28)|(0x7f<<1
b60e0 34 29 7c 28 30 78 37 66 29 3b 20 2a 2f 0a 20 20 4)|(0x7f); */.
b60f0 20 20 2f 2a 20 62 20 26 3d 20 28 30 78 37 66 3c /* b &= (0x7f<
b6100 3c 31 34 29 7c 28 30 78 37 66 29 3b 20 2a 2f 0a <14)|(0x7f); */.
b6110 20 20 20 20 62 20 3d 20 62 3c 3c 37 3b 0a 20 20 b = b<<7;.
b6120 20 20 61 20 7c 3d 20 62 3b 0a 20 20 20 20 73 20 a |= b;. s
b6130 3d 20 73 3e 3e 31 38 3b 0a 20 20 20 20 2a 76 20 = s>>18;. *v
b6140 3d 20 28 28 75 36 34 29 73 29 3c 3c 33 32 20 7c = ((u64)s)<<32 |
b6150 20 61 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 35 a;. return 5
b6160 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 32 3a 73 61 ;. }.. /* 2:sa
b6170 76 65 20 6f 66 66 20 70 30 3c 3c 32 31 20 7c 20 ve off p0<<21 |
b6180 70 31 3c 3c 31 34 20 7c 20 70 32 3c 3c 37 20 7c p1<<14 | p2<<7 |
b6190 20 70 33 20 28 6d 61 73 6b 65 64 29 20 2a 2f 0a p3 (masked) */.
b61a0 20 20 73 20 3d 20 73 3c 3c 37 3b 0a 20 20 73 20 s = s<<7;. s
b61b0 7c 3d 20 62 3b 0a 20 20 2f 2a 20 73 3a 20 70 30 |= b;. /* s: p0
b61c0 3c 3c 32 31 20 7c 20 70 31 3c 3c 31 34 20 7c 20 <<21 | p1<<14 |
b61d0 70 32 3c 3c 37 20 7c 20 70 33 20 28 6d 61 73 6b p2<<7 | p3 (mask
b61e0 65 64 29 20 2a 2f 0a 0a 20 20 70 2b 2b 3b 0a 20 ed) */.. p++;.
b61f0 20 62 20 3d 20 62 3c 3c 31 34 3b 0a 20 20 62 20 b = b<<14;. b
b6200 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 62 3a 20 70 |= *p;. /* b: p
b6210 31 3c 3c 32 38 20 7c 20 70 33 3c 3c 31 34 20 7c 1<<28 | p3<<14 |
b6220 20 70 35 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a p5 (unmasked) *
b6230 2f 0a 20 20 69 66 20 28 21 28 62 26 30 78 38 30 /. if (!(b&0x80
b6240 29 29 0a 20 20 7b 0a 20 20 20 20 2f 2a 20 77 65 )). {. /* we
b6250 20 63 61 6e 20 73 6b 69 70 20 74 68 69 73 20 63 can skip this c
b6260 61 75 73 65 20 69 74 20 77 61 73 20 28 65 66 66 ause it was (eff
b6270 65 63 74 69 76 65 6c 79 29 20 64 6f 6e 65 20 61 ectively) done a
b6280 62 6f 76 65 20 69 6e 20 63 61 6c 63 27 69 6e 67 bove in calc'ing
b6290 20 73 20 2a 2f 0a 20 20 20 20 2f 2a 20 62 20 26 s */. /* b &
b62a0 3d 20 28 30 78 37 66 3c 3c 32 38 29 7c 28 30 78 = (0x7f<<28)|(0x
b62b0 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 20 7f<<14)|(0x7f);
b62c0 2a 2f 0a 20 20 20 20 61 20 26 3d 20 28 30 78 37 */. a &= (0x7
b62d0 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 f<<14)|(0x7f);.
b62e0 20 20 20 61 20 3d 20 61 3c 3c 37 3b 0a 20 20 20 a = a<<7;.
b62f0 20 61 20 7c 3d 20 62 3b 0a 20 20 20 20 73 20 3d a |= b;. s =
b6300 20 73 3e 3e 31 38 3b 0a 20 20 20 20 2a 76 20 3d s>>18;. *v =
b6310 20 28 28 75 36 34 29 73 29 3c 3c 33 32 20 7c 20 ((u64)s)<<32 |
b6320 61 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 36 3b a;. return 6;
b6330 0a 20 20 7d 0a 0a 20 20 70 2b 2b 3b 0a 20 20 61 . }.. p++;. a
b6340 20 3d 20 61 3c 3c 31 34 3b 0a 20 20 61 20 7c 3d = a<<14;. a |=
b6350 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a 20 70 32 3c *p;. /* a: p2<
b6360 3c 32 38 20 7c 20 70 34 3c 3c 31 34 20 7c 20 70 <28 | p4<<14 | p
b6370 36 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 6 (unmasked) */.
b6380 20 20 69 66 20 28 21 28 61 26 30 78 38 30 29 29 if (!(a&0x80))
b6390 0a 20 20 7b 0a 20 20 20 20 61 20 26 3d 20 28 30 . {. a &= (0
b63a0 78 31 66 3c 3c 32 38 29 7c 28 30 78 37 66 3c 3c x1f<<28)|(0x7f<<
b63b0 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 14)|(0x7f);.
b63c0 62 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c b &= (0x7f<<14)|
b63d0 28 30 78 37 66 29 3b 0a 20 20 20 20 62 20 3d 20 (0x7f);. b =
b63e0 62 3c 3c 37 3b 0a 20 20 20 20 61 20 7c 3d 20 62 b<<7;. a |= b
b63f0 3b 0a 20 20 20 20 73 20 3d 20 73 3e 3e 31 31 3b ;. s = s>>11;
b6400 0a 20 20 20 20 2a 76 20 3d 20 28 28 75 36 34 29 . *v = ((u64)
b6410 73 29 3c 3c 33 32 20 7c 20 61 3b 0a 20 20 20 20 s)<<32 | a;.
b6420 72 65 74 75 72 6e 20 37 3b 0a 20 20 7d 0a 0a 20 return 7;. }..
b6430 20 2f 2a 20 43 53 45 32 20 66 72 6f 6d 20 62 65 /* CSE2 from be
b6440 6c 6f 77 20 2a 2f 0a 20 20 61 20 26 3d 20 28 30 low */. a &= (0
b6450 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b x7f<<14)|(0x7f);
b6460 0a 20 20 70 2b 2b 3b 0a 20 20 62 20 3d 20 62 3c . p++;. b = b<
b6470 3c 31 34 3b 0a 20 20 62 20 7c 3d 20 2a 70 3b 0a <14;. b |= *p;.
b6480 20 20 2f 2a 20 62 3a 20 70 33 3c 3c 32 38 20 7c /* b: p3<<28 |
b6490 20 70 35 3c 3c 31 34 20 7c 20 70 37 20 28 75 6e p5<<14 | p7 (un
b64a0 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 masked) */. if
b64b0 28 21 28 62 26 30 78 38 30 29 29 0a 20 20 7b 0a (!(b&0x80)). {.
b64c0 20 20 20 20 62 20 26 3d 20 28 30 78 31 66 3c 3c b &= (0x1f<<
b64d0 32 38 29 7c 28 30 78 37 66 3c 3c 31 34 29 7c 28 28)|(0x7f<<14)|(
b64e0 30 78 37 66 29 3b 0a 20 20 20 20 2f 2a 20 6d 6f 0x7f);. /* mo
b64f0 76 65 64 20 43 53 45 32 20 75 70 20 2a 2f 0a 20 ved CSE2 up */.
b6500 20 20 20 2f 2a 20 61 20 26 3d 20 28 30 78 37 66 /* a &= (0x7f
b6510 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 20 2a 2f <<14)|(0x7f); */
b6520 0a 20 20 20 20 61 20 3d 20 61 3c 3c 37 3b 0a 20 . a = a<<7;.
b6530 20 20 20 61 20 7c 3d 20 62 3b 0a 20 20 20 20 73 a |= b;. s
b6540 20 3d 20 73 3e 3e 34 3b 0a 20 20 20 20 2a 76 20 = s>>4;. *v
b6550 3d 20 28 28 75 36 34 29 73 29 3c 3c 33 32 20 7c = ((u64)s)<<32 |
b6560 20 61 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 38 a;. return 8
b6570 3b 0a 20 20 7d 0a 0a 20 20 70 2b 2b 3b 0a 20 20 ;. }.. p++;.
b6580 61 20 3d 20 61 3c 3c 31 35 3b 0a 20 20 61 20 7c a = a<<15;. a |
b6590 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a 20 70 34 = *p;. /* a: p4
b65a0 3c 3c 32 39 20 7c 20 70 36 3c 3c 31 35 20 7c 20 <<29 | p6<<15 |
b65b0 70 38 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f p8 (unmasked) */
b65c0 0a 0a 20 20 2f 2a 20 6d 6f 76 65 64 20 43 53 45 .. /* moved CSE
b65d0 32 20 75 70 20 2a 2f 0a 20 20 2f 2a 20 61 20 26 2 up */. /* a &
b65e0 3d 20 28 30 78 37 66 3c 3c 32 39 29 7c 28 30 78 = (0x7f<<29)|(0x
b65f0 37 66 3c 3c 31 35 29 7c 28 30 78 66 66 29 3b 20 7f<<15)|(0xff);
b6600 2a 2f 0a 20 20 62 20 26 3d 20 28 30 78 37 66 3c */. b &= (0x7f<
b6610 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 62 <14)|(0x7f);. b
b6620 20 3d 20 62 3c 3c 38 3b 0a 20 20 61 20 7c 3d 20 = b<<8;. a |=
b6630 62 3b 0a 0a 20 20 73 20 3d 20 73 3c 3c 34 3b 0a b;.. s = s<<4;.
b6640 20 20 62 20 3d 20 70 5b 2d 34 5d 3b 0a 20 20 62 b = p[-4];. b
b6650 20 26 3d 20 30 78 37 66 3b 0a 20 20 62 20 3d 20 &= 0x7f;. b =
b6660 62 3e 3e 33 3b 0a 20 20 73 20 7c 3d 20 62 3b 0a b>>3;. s |= b;.
b6670 0a 20 20 2a 76 20 3d 20 28 28 75 36 34 29 73 29 . *v = ((u64)s)
b6680 3c 3c 33 32 20 7c 20 61 3b 0a 0a 20 20 72 65 74 <<32 | a;.. ret
b6690 75 72 6e 20 39 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 urn 9;.}../*.**
b66a0 52 65 61 64 20 61 20 33 32 2d 62 69 74 20 76 61 Read a 32-bit va
b66b0 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e riable-length in
b66c0 74 65 67 65 72 20 66 72 6f 6d 20 6d 65 6d 6f 72 teger from memor
b66d0 79 20 73 74 61 72 74 69 6e 67 20 61 74 20 70 5b y starting at p[
b66e0 30 5d 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 0]..** Return th
b66f0 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 e number of byte
b6700 73 20 72 65 61 64 2e 20 20 54 68 65 20 76 61 6c s read. The val
b6710 75 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 ue is stored in
b6720 2a 76 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 *v..**.** If the
b6730 20 76 61 72 69 6e 74 20 73 74 6f 72 65 64 20 69 varint stored i
b6740 6e 20 70 5b 30 5d 20 69 73 20 6c 61 72 67 65 72 n p[0] is larger
b6750 20 74 68 61 6e 20 63 61 6e 20 66 69 74 20 69 6e than can fit in
b6760 20 61 20 33 32 2d 62 69 74 20 75 6e 73 69 67 6e a 32-bit unsign
b6770 65 64 0a 2a 2a 20 69 6e 74 65 67 65 72 2c 20 74 ed.** integer, t
b6780 68 65 6e 20 73 65 74 20 2a 76 20 74 6f 20 30 78 hen set *v to 0x
b6790 66 66 66 66 66 66 66 66 2e 0a 2a 2a 0a 2a 2a 20 ffffffff..**.**
b67a0 41 20 4d 41 43 52 4f 20 76 65 72 73 69 6f 6e 2c A MACRO version,
b67b0 20 67 65 74 56 61 72 69 6e 74 33 32 2c 20 69 73 getVarint32, is
b67c0 20 70 72 6f 76 69 64 65 64 20 77 68 69 63 68 20 provided which
b67d0 69 6e 6c 69 6e 65 73 20 74 68 65 20 0a 2a 2a 20 inlines the .**
b67e0 73 69 6e 67 6c 65 2d 62 79 74 65 20 63 61 73 65 single-byte case
b67f0 2e 20 20 41 6c 6c 20 63 6f 64 65 20 73 68 6f 75 . All code shou
b6800 6c 64 20 75 73 65 20 74 68 65 20 4d 41 43 52 4f ld use the MACRO
b6810 20 76 65 72 73 69 6f 6e 20 61 73 20 0a 2a 2a 20 version as .**
b6820 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 73 this function as
b6830 73 75 6d 65 73 20 74 68 65 20 73 69 6e 67 6c 65 sumes the single
b6840 2d 62 79 74 65 20 63 61 73 65 20 68 61 73 20 61 -byte case has a
b6850 6c 72 65 61 64 79 20 62 65 65 6e 20 68 61 6e 64 lready been hand
b6860 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 led..*/.SQLITE_P
b6870 52 49 56 41 54 45 20 75 38 20 73 71 6c 69 74 65 RIVATE u8 sqlite
b6880 33 47 65 74 56 61 72 69 6e 74 33 32 28 63 6f 6e 3GetVarint32(con
b6890 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 st unsigned char
b68a0 20 2a 70 2c 20 75 33 32 20 2a 76 29 7b 0a 20 20 *p, u32 *v){.
b68b0 75 33 32 20 61 2c 62 3b 0a 0a 20 20 2f 2a 20 54 u32 a,b;.. /* T
b68c0 68 65 20 31 2d 62 79 74 65 20 63 61 73 65 2e 20 he 1-byte case.
b68d0 20 4f 76 65 72 77 68 65 6c 6d 69 6e 67 6c 79 20 Overwhelmingly
b68e0 74 68 65 20 6d 6f 73 74 20 63 6f 6d 6d 6f 6e 2e the most common.
b68f0 20 20 48 61 6e 64 6c 65 64 20 69 6e 6c 69 6e 65 Handled inline
b6900 0a 20 20 2a 2a 20 62 79 20 74 68 65 20 67 65 74 . ** by the get
b6910 56 61 72 69 6e 33 32 28 29 20 6d 61 63 72 6f 20 Varin32() macro
b6920 2a 2f 0a 20 20 61 20 3d 20 2a 70 3b 0a 20 20 2f */. a = *p;. /
b6930 2a 20 61 3a 20 70 30 20 28 75 6e 6d 61 73 6b 65 * a: p0 (unmaske
b6940 64 29 20 2a 2f 0a 23 69 66 6e 64 65 66 20 67 65 d) */.#ifndef ge
b6950 74 56 61 72 69 6e 74 33 32 0a 20 20 69 66 20 28 tVarint32. if (
b6960 21 28 61 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 !(a&0x80)). {.
b6970 20 20 20 2f 2a 20 56 61 6c 75 65 73 20 62 65 74 /* Values bet
b6980 77 65 65 6e 20 30 20 61 6e 64 20 31 32 37 20 2a ween 0 and 127 *
b6990 2f 0a 20 20 20 20 2a 76 20 3d 20 61 3b 0a 20 20 /. *v = a;.
b69a0 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a return 1;. }.
b69b0 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 54 68 65 #endif.. /* The
b69c0 20 32 2d 62 79 74 65 20 63 61 73 65 20 2a 2f 0a 2-byte case */.
b69d0 20 20 70 2b 2b 3b 0a 20 20 62 20 3d 20 2a 70 3b p++;. b = *p;
b69e0 0a 20 20 2f 2a 20 62 3a 20 70 31 20 28 75 6e 6d . /* b: p1 (unm
b69f0 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 asked) */. if (
b6a00 21 28 62 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 !(b&0x80)). {.
b6a10 20 20 20 2f 2a 20 56 61 6c 75 65 73 20 62 65 74 /* Values bet
b6a20 77 65 65 6e 20 31 32 38 20 61 6e 64 20 31 36 33 ween 128 and 163
b6a30 38 33 20 2a 2f 0a 20 20 20 20 61 20 26 3d 20 30 83 */. a &= 0
b6a40 78 37 66 3b 0a 20 20 20 20 61 20 3d 20 61 3c 3c x7f;. a = a<<
b6a50 37 3b 0a 20 20 20 20 2a 76 20 3d 20 61 20 7c 20 7;. *v = a |
b6a60 62 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 32 3b b;. return 2;
b6a70 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 33 . }.. /* The 3
b6a80 2d 62 79 74 65 20 63 61 73 65 20 2a 2f 0a 20 20 -byte case */.
b6a90 70 2b 2b 3b 0a 20 20 61 20 3d 20 61 3c 3c 31 34 p++;. a = a<<14
b6aa0 3b 0a 20 20 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f ;. a |= *p;. /
b6ab0 2a 20 61 3a 20 70 30 3c 3c 31 34 20 7c 20 70 32 * a: p0<<14 | p2
b6ac0 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 (unmasked) */.
b6ad0 20 69 66 20 28 21 28 61 26 30 78 38 30 29 29 0a if (!(a&0x80)).
b6ae0 20 20 7b 0a 20 20 20 20 2f 2a 20 56 61 6c 75 65 {. /* Value
b6af0 73 20 62 65 74 77 65 65 6e 20 31 36 33 38 34 20 s between 16384
b6b00 61 6e 64 20 32 30 39 37 31 35 31 20 2a 2f 0a 20 and 2097151 */.
b6b10 20 20 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 a &= (0x7f<<1
b6b20 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 62 4)|(0x7f);. b
b6b30 20 26 3d 20 30 78 37 66 3b 0a 20 20 20 20 62 20 &= 0x7f;. b
b6b40 3d 20 62 3c 3c 37 3b 0a 20 20 20 20 2a 76 20 3d = b<<7;. *v =
b6b50 20 61 20 7c 20 62 3b 0a 20 20 20 20 72 65 74 75 a | b;. retu
b6b60 72 6e 20 33 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 rn 3;. }.. /*
b6b70 41 20 33 32 2d 62 69 74 20 76 61 72 69 6e 74 20 A 32-bit varint
b6b80 69 73 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 is used to store
b6b90 20 73 69 7a 65 20 69 6e 66 6f 72 6d 61 74 69 6f size informatio
b6ba0 6e 20 69 6e 20 62 74 72 65 65 73 2e 0a 20 20 2a n in btrees.. *
b6bb0 2a 20 4f 62 6a 65 63 74 73 20 61 72 65 20 72 61 * Objects are ra
b6bc0 72 65 6c 79 20 6c 61 72 67 65 72 20 74 68 61 6e rely larger than
b6bd0 20 32 4d 69 42 20 6c 69 6d 69 74 20 6f 66 20 61 2MiB limit of a
b6be0 20 33 2d 62 79 74 65 20 76 61 72 69 6e 74 2e 0a 3-byte varint..
b6bf0 20 20 2a 2a 20 41 20 33 2d 62 79 74 65 20 76 61 ** A 3-byte va
b6c00 72 69 6e 74 20 69 73 20 73 75 66 66 69 63 69 65 rint is sufficie
b6c10 6e 74 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c nt, for example,
b6c20 20 74 6f 20 72 65 63 6f 72 64 20 74 68 65 20 73 to record the s
b6c30 69 7a 65 0a 20 20 2a 2a 20 6f 66 20 61 20 31 30 ize. ** of a 10
b6c40 34 38 35 36 39 2d 62 79 74 65 20 42 4c 4f 42 20 48569-byte BLOB
b6c50 6f 72 20 73 74 72 69 6e 67 2e 0a 20 20 2a 2a 0a or string.. **.
b6c60 20 20 2a 2a 20 57 65 20 6f 6e 6c 79 20 75 6e 72 ** We only unr
b6c70 6f 6c 6c 20 74 68 65 20 66 69 72 73 74 20 31 2d oll the first 1-
b6c80 2c 20 32 2d 2c 20 61 6e 64 20 33 2d 20 62 79 74 , 2-, and 3- byt
b6c90 65 20 63 61 73 65 73 2e 20 20 54 68 65 20 76 65 e cases. The ve
b6ca0 72 79 0a 20 20 2a 2a 20 72 61 72 65 20 6c 61 72 ry. ** rare lar
b6cb0 67 65 72 20 63 61 73 65 73 20 63 61 6e 20 62 65 ger cases can be
b6cc0 20 68 61 6e 64 6c 65 64 20 62 79 20 74 68 65 20 handled by the
b6cd0 73 6c 6f 77 65 72 20 36 34 2d 62 69 74 20 76 61 slower 64-bit va
b6ce0 72 69 6e 74 0a 20 20 2a 2a 20 72 6f 75 74 69 6e rint. ** routin
b6cf0 65 2e 0a 20 20 2a 2f 0a 23 69 66 20 31 0a 20 20 e.. */.#if 1.
b6d00 7b 0a 20 20 20 20 75 36 34 20 76 36 34 3b 0a 20 {. u64 v64;.
b6d10 20 20 20 75 38 20 6e 3b 0a 0a 20 20 20 20 70 20 u8 n;.. p
b6d20 2d 3d 20 32 3b 0a 20 20 20 20 6e 20 3d 20 73 71 -= 2;. n = sq
b6d30 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74 28 70 lite3GetVarint(p
b6d40 2c 20 26 76 36 34 29 3b 0a 20 20 20 20 61 73 73 , &v64);. ass
b6d50 65 72 74 28 20 6e 3e 33 20 26 26 20 6e 3c 3d 39 ert( n>3 && n<=9
b6d60 20 29 3b 0a 20 20 20 20 69 66 28 20 28 76 36 34 );. if( (v64
b6d70 20 26 20 53 51 4c 49 54 45 5f 4d 41 58 5f 55 33 & SQLITE_MAX_U3
b6d80 32 29 21 3d 76 36 34 20 29 7b 0a 20 20 20 20 20 2)!=v64 ){.
b6d90 20 2a 76 20 3d 20 30 78 66 66 66 66 66 66 66 66 *v = 0xffffffff
b6da0 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
b6db0 20 20 20 2a 76 20 3d 20 28 75 33 32 29 76 36 34 *v = (u32)v64
b6dc0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 ;. }. retu
b6dd0 72 6e 20 6e 3b 0a 20 20 7d 0a 0a 23 65 6c 73 65 rn n;. }..#else
b6de0 0a 20 20 2f 2a 20 46 6f 72 20 66 6f 6c 6c 6f 77 . /* For follow
b6df0 69 6e 67 20 63 6f 64 65 20 28 6b 65 70 74 20 66 ing code (kept f
b6e00 6f 72 20 68 69 73 74 6f 72 69 63 61 6c 20 72 65 or historical re
b6e10 63 6f 72 64 20 6f 6e 6c 79 29 20 73 68 6f 77 73 cord only) shows
b6e20 20 61 6e 0a 20 20 2a 2a 20 75 6e 72 6f 6c 6c 69 an. ** unrolli
b6e30 6e 67 20 66 6f 72 20 74 68 65 20 33 2d 20 61 6e ng for the 3- an
b6e40 64 20 34 2d 62 79 74 65 20 76 61 72 69 6e 74 20 d 4-byte varint
b6e50 63 61 73 65 73 2e 20 20 54 68 69 73 20 63 6f 64 cases. This cod
b6e60 65 20 69 73 0a 20 20 2a 2a 20 73 6c 69 67 68 74 e is. ** slight
b6e70 6c 79 20 66 61 73 74 65 72 2c 20 62 75 74 20 69 ly faster, but i
b6e80 74 20 69 73 20 61 6c 73 6f 20 6c 61 72 67 65 72 t is also larger
b6e90 20 61 6e 64 20 6d 75 63 68 20 68 61 72 64 65 72 and much harder
b6ea0 20 74 6f 20 74 65 73 74 2e 0a 20 20 2a 2f 0a 20 to test.. */.
b6eb0 20 70 2b 2b 3b 0a 20 20 62 20 3d 20 62 3c 3c 31 p++;. b = b<<1
b6ec0 34 3b 0a 20 20 62 20 7c 3d 20 2a 70 3b 0a 20 20 4;. b |= *p;.
b6ed0 2f 2a 20 62 3a 20 70 31 3c 3c 31 34 20 7c 20 70 /* b: p1<<14 | p
b6ee0 33 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 3 (unmasked) */.
b6ef0 20 20 69 66 20 28 21 28 62 26 30 78 38 30 29 29 if (!(b&0x80))
b6f00 0a 20 20 7b 0a 20 20 20 20 2f 2a 20 56 61 6c 75 . {. /* Valu
b6f10 65 73 20 62 65 74 77 65 65 6e 20 32 30 39 37 31 es between 20971
b6f20 35 32 20 61 6e 64 20 32 36 38 34 33 35 34 35 35 52 and 268435455
b6f30 20 2a 2f 0a 20 20 20 20 62 20 26 3d 20 28 30 78 */. b &= (0x
b6f40 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 7f<<14)|(0x7f);.
b6f50 20 20 20 20 61 20 26 3d 20 28 30 78 37 66 3c 3c a &= (0x7f<<
b6f60 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 14)|(0x7f);.
b6f70 61 20 3d 20 61 3c 3c 37 3b 0a 20 20 20 20 2a 76 a = a<<7;. *v
b6f80 20 3d 20 61 20 7c 20 62 3b 0a 20 20 20 20 72 65 = a | b;. re
b6f90 74 75 72 6e 20 34 3b 0a 20 20 7d 0a 0a 20 20 70 turn 4;. }.. p
b6fa0 2b 2b 3b 0a 20 20 61 20 3d 20 61 3c 3c 31 34 3b ++;. a = a<<14;
b6fb0 0a 20 20 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a . a |= *p;. /*
b6fc0 20 61 3a 20 70 30 3c 3c 32 38 20 7c 20 70 32 3c a: p0<<28 | p2<
b6fd0 3c 31 34 20 7c 20 70 34 20 28 75 6e 6d 61 73 6b <14 | p4 (unmask
b6fe0 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 61 ed) */. if (!(a
b6ff0 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 &0x80)). {.
b7000 2f 2a 20 57 61 6c 75 65 73 20 20 62 65 74 77 65 /* Walues betwe
b7010 65 6e 20 32 36 38 34 33 35 34 35 36 20 61 6e 64 en 268435456 and
b7020 20 33 34 33 35 39 37 33 38 33 36 37 20 2a 2f 0a 34359738367 */.
b7030 20 20 20 20 61 20 26 3d 20 28 30 78 31 66 3c 3c a &= (0x1f<<
b7040 32 38 29 7c 28 30 78 37 66 3c 3c 31 34 29 7c 28 28)|(0x7f<<14)|(
b7050 30 78 37 66 29 3b 0a 20 20 20 20 62 20 26 3d 20 0x7f);. b &=
b7060 28 30 78 31 66 3c 3c 32 38 29 7c 28 30 78 37 66 (0x1f<<28)|(0x7f
b7070 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 <<14)|(0x7f);.
b7080 20 20 62 20 3d 20 62 3c 3c 37 3b 0a 20 20 20 20 b = b<<7;.
b7090 2a 76 20 3d 20 61 20 7c 20 62 3b 0a 20 20 20 20 *v = a | b;.
b70a0 72 65 74 75 72 6e 20 35 3b 0a 20 20 7d 0a 0a 20 return 5;. }..
b70b0 20 2f 2a 20 57 65 20 63 61 6e 20 6f 6e 6c 79 20 /* We can only
b70c0 72 65 61 63 68 20 74 68 69 73 20 70 6f 69 6e 74 reach this point
b70d0 20 77 68 65 6e 20 72 65 61 64 69 6e 67 20 61 20 when reading a
b70e0 63 6f 72 72 75 70 74 20 64 61 74 61 62 61 73 65 corrupt database
b70f0 0a 20 20 2a 2a 20 66 69 6c 65 2e 20 20 49 6e 20 . ** file. In
b7100 74 68 61 74 20 63 61 73 65 20 77 65 20 61 72 65 that case we are
b7110 20 6e 6f 74 20 69 6e 20 61 6e 79 20 68 75 72 72 not in any hurr
b7120 79 2e 20 20 55 73 65 20 74 68 65 20 28 72 65 6c y. Use the (rel
b7130 61 74 69 76 65 6c 79 0a 20 20 2a 2a 20 73 6c 6f atively. ** slo
b7140 77 29 20 67 65 6e 65 72 61 6c 2d 70 75 72 70 6f w) general-purpo
b7150 73 65 20 73 71 6c 69 74 65 33 47 65 74 56 61 72 se sqlite3GetVar
b7160 69 6e 74 28 29 20 72 6f 75 74 69 6e 65 20 74 6f int() routine to
b7170 20 65 78 74 72 61 63 74 20 74 68 65 0a 20 20 2a extract the. *
b7180 2a 20 76 61 6c 75 65 2e 20 2a 2f 0a 20 20 7b 0a * value. */. {.
b7190 20 20 20 20 75 36 34 20 76 36 34 3b 0a 20 20 20 u64 v64;.
b71a0 20 75 38 20 6e 3b 0a 0a 20 20 20 20 70 20 2d 3d u8 n;.. p -=
b71b0 20 34 3b 0a 20 20 20 20 6e 20 3d 20 73 71 6c 69 4;. n = sqli
b71c0 74 65 33 47 65 74 56 61 72 69 6e 74 28 70 2c 20 te3GetVarint(p,
b71d0 26 76 36 34 29 3b 0a 20 20 20 20 61 73 73 65 72 &v64);. asser
b71e0 74 28 20 6e 3e 35 20 26 26 20 6e 3c 3d 39 20 29 t( n>5 && n<=9 )
b71f0 3b 0a 20 20 20 20 2a 76 20 3d 20 28 75 33 32 29 ;. *v = (u32)
b7200 76 36 34 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 v64;. return
b7210 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a n;. }.#endif.}.
b7220 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ./*.** Return th
b7230 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 e number of byte
b7240 73 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 6e s that will be n
b7250 65 65 64 65 64 20 74 6f 20 73 74 6f 72 65 20 74 eeded to store t
b7260 68 65 20 67 69 76 65 6e 0a 2a 2a 20 36 34 2d 62 he given.** 64-b
b7270 69 74 20 69 6e 74 65 67 65 72 2e 0a 2a 2f 0a 53 it integer..*/.S
b7280 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
b7290 74 20 73 71 6c 69 74 65 33 56 61 72 69 6e 74 4c t sqlite3VarintL
b72a0 65 6e 28 75 36 34 20 76 29 7b 0a 20 20 69 6e 74 en(u64 v){. int
b72b0 20 69 20 3d 20 30 3b 0a 20 20 64 6f 7b 0a 20 20 i = 0;. do{.
b72c0 20 20 69 2b 2b 3b 0a 20 20 20 20 76 20 3e 3e 3d i++;. v >>=
b72d0 20 37 3b 0a 20 20 7d 77 68 69 6c 65 28 20 76 21 7;. }while( v!
b72e0 3d 30 20 26 26 20 41 4c 57 41 59 53 28 69 3c 39 =0 && ALWAYS(i<9
b72f0 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 3b ) );. return i;
b7300 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 .}.../*.** Read
b7310 6f 72 20 77 72 69 74 65 20 61 20 66 6f 75 72 2d or write a four-
b7320 62 79 74 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 byte big-endian
b7330 69 6e 74 65 67 65 72 20 76 61 6c 75 65 2e 0a 2a integer value..*
b7340 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
b7350 20 75 33 32 20 73 71 6c 69 74 65 33 47 65 74 34 u32 sqlite3Get4
b7360 62 79 74 65 28 63 6f 6e 73 74 20 75 38 20 2a 70 byte(const u8 *p
b7370 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 70 5b 30 ){. return (p[0
b7380 5d 3c 3c 32 34 29 20 7c 20 28 70 5b 31 5d 3c 3c ]<<24) | (p[1]<<
b7390 31 36 29 20 7c 20 28 70 5b 32 5d 3c 3c 38 29 20 16) | (p[2]<<8)
b73a0 7c 20 70 5b 33 5d 3b 0a 7d 0a 53 51 4c 49 54 45 | p[3];.}.SQLITE
b73b0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
b73c0 6c 69 74 65 33 50 75 74 34 62 79 74 65 28 75 6e lite3Put4byte(un
b73d0 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 2c 20 signed char *p,
b73e0 75 33 32 20 76 29 7b 0a 20 20 70 5b 30 5d 20 3d u32 v){. p[0] =
b73f0 20 28 75 38 29 28 76 3e 3e 32 34 29 3b 0a 20 20 (u8)(v>>24);.
b7400 70 5b 31 5d 20 3d 20 28 75 38 29 28 76 3e 3e 31 p[1] = (u8)(v>>1
b7410 36 29 3b 0a 20 20 70 5b 32 5d 20 3d 20 28 75 38 6);. p[2] = (u8
b7420 29 28 76 3e 3e 38 29 3b 0a 20 20 70 5b 33 5d 20 )(v>>8);. p[3]
b7430 3d 20 28 75 38 29 76 3b 0a 7d 0a 0a 0a 0a 23 69 = (u8)v;.}....#i
b7440 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 f !defined(SQLIT
b7450 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 E_OMIT_BLOB_LITE
b7460 52 41 4c 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 RAL) || defined(
b7470 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 SQLITE_HAS_CODEC
b7480 29 0a 2f 2a 0a 2a 2a 20 54 72 61 6e 73 6c 61 74 )./*.** Translat
b7490 65 20 61 20 73 69 6e 67 6c 65 20 62 79 74 65 20 e a single byte
b74a0 6f 66 20 48 65 78 20 69 6e 74 6f 20 61 6e 20 69 of Hex into an i
b74b0 6e 74 65 67 65 72 2e 0a 2a 2a 20 54 68 69 73 20 nteger..** This
b74c0 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 77 6f 72 routine only wor
b74d0 6b 73 20 69 66 20 68 20 72 65 61 6c 6c 79 20 69 ks if h really i
b74e0 73 20 61 20 76 61 6c 69 64 20 68 65 78 61 64 65 s a valid hexade
b74f0 63 69 6d 61 6c 0a 2a 2a 20 63 68 61 72 61 63 74 cimal.** charact
b7500 65 72 3a 20 20 30 2e 2e 39 61 2e 2e 66 41 2e 2e er: 0..9a..fA..
b7510 46 0a 2a 2f 0a 73 74 61 74 69 63 20 75 38 20 68 F.*/.static u8 h
b7520 65 78 54 6f 49 6e 74 28 69 6e 74 20 68 29 7b 0a exToInt(int h){.
b7530 20 20 61 73 73 65 72 74 28 20 28 68 3e 3d 27 30 assert( (h>='0
b7540 27 20 26 26 20 68 3c 3d 27 39 27 29 20 7c 7c 20 ' && h<='9') ||
b7550 20 28 68 3e 3d 27 61 27 20 26 26 20 68 3c 3d 27 (h>='a' && h<='
b7560 66 27 29 20 7c 7c 20 20 28 68 3e 3d 27 41 27 20 f') || (h>='A'
b7570 26 26 20 68 3c 3d 27 46 27 29 20 29 3b 0a 23 69 && h<='F') );.#i
b7580 66 64 65 66 20 53 51 4c 49 54 45 5f 41 53 43 49 fdef SQLITE_ASCI
b7590 49 0a 20 20 68 20 2b 3d 20 39 2a 28 31 26 28 68 I. h += 9*(1&(h
b75a0 3e 3e 36 29 29 3b 0a 23 65 6e 64 69 66 0a 23 69 >>6));.#endif.#i
b75b0 66 64 65 66 20 53 51 4c 49 54 45 5f 45 42 43 44 fdef SQLITE_EBCD
b75c0 49 43 0a 20 20 68 20 2b 3d 20 39 2a 28 31 26 7e IC. h += 9*(1&~
b75d0 28 68 3e 3e 34 29 29 3b 0a 23 65 6e 64 69 66 0a (h>>4));.#endif.
b75e0 20 20 72 65 74 75 72 6e 20 28 75 38 29 28 68 20 return (u8)(h
b75f0 26 20 30 78 66 29 3b 0a 7d 0a 23 65 6e 64 69 66 & 0xf);.}.#endif
b7600 20 2f 2a 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54 /* !SQLITE_OMIT
b7610 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 4c 20 7c 7c _BLOB_LITERAL ||
b7620 20 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 SQLITE_HAS_CODE
b7630 43 20 2a 2f 0a 0a 23 69 66 20 21 64 65 66 69 6e C */..#if !defin
b7640 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 ed(SQLITE_OMIT_B
b7650 4c 4f 42 5f 4c 49 54 45 52 41 4c 29 20 7c 7c 20 LOB_LITERAL) ||
b7660 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 48 defined(SQLITE_H
b7670 41 53 5f 43 4f 44 45 43 29 0a 2f 2a 0a 2a 2a 20 AS_CODEC)./*.**
b7680 43 6f 6e 76 65 72 74 20 61 20 42 4c 4f 42 20 6c Convert a BLOB l
b7690 69 74 65 72 61 6c 20 6f 66 20 74 68 65 20 66 6f iteral of the fo
b76a0 72 6d 20 22 78 27 68 68 68 68 68 68 27 22 20 69 rm "x'hhhhhh'" i
b76b0 6e 74 6f 20 69 74 73 20 62 69 6e 61 72 79 0a 2a nto its binary.*
b76c0 2a 20 76 61 6c 75 65 2e 20 20 52 65 74 75 72 6e * value. Return
b76d0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 a pointer to it
b76e0 73 20 62 69 6e 61 72 79 20 76 61 6c 75 65 2e 20 s binary value.
b76f0 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 Space to hold t
b7700 68 65 0a 2a 2a 20 62 69 6e 61 72 79 20 76 61 6c he.** binary val
b7710 75 65 20 68 61 73 20 62 65 65 6e 20 6f 62 74 61 ue has been obta
b7720 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 ined from malloc
b7730 20 61 6e 64 20 6d 75 73 74 20 62 65 20 66 72 65 and must be fre
b7740 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 63 61 6c ed by.** the cal
b7750 6c 69 6e 67 20 72 6f 75 74 69 6e 65 2e 0a 2a 2f ling routine..*/
b7760 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
b7770 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 48 65 78 void *sqlite3Hex
b7780 54 6f 42 6c 6f 62 28 73 71 6c 69 74 65 33 20 2a ToBlob(sqlite3 *
b7790 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a db, const char *
b77a0 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 63 68 61 z, int n){. cha
b77b0 72 20 2a 7a 42 6c 6f 62 3b 0a 20 20 69 6e 74 20 r *zBlob;. int
b77c0 69 3b 0a 0a 20 20 7a 42 6c 6f 62 20 3d 20 28 63 i;.. zBlob = (c
b77d0 68 61 72 20 2a 29 73 71 6c 69 74 65 33 44 62 4d har *)sqlite3DbM
b77e0 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 6e 2f 32 allocRaw(db, n/2
b77f0 20 2b 20 31 29 3b 0a 20 20 6e 2d 2d 3b 0a 20 20 + 1);. n--;.
b7800 69 66 28 20 7a 42 6c 6f 62 20 29 7b 0a 20 20 20 if( zBlob ){.
b7810 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 3b 20 69 for(i=0; i<n; i
b7820 2b 3d 32 29 7b 0a 20 20 20 20 20 20 7a 42 6c 6f +=2){. zBlo
b7830 62 5b 69 2f 32 5d 20 3d 20 28 68 65 78 54 6f 49 b[i/2] = (hexToI
b7840 6e 74 28 7a 5b 69 5d 29 3c 3c 34 29 20 7c 20 68 nt(z[i])<<4) | h
b7850 65 78 54 6f 49 6e 74 28 7a 5b 69 2b 31 5d 29 3b exToInt(z[i+1]);
b7860 0a 20 20 20 20 7d 0a 20 20 20 20 7a 42 6c 6f 62 . }. zBlob
b7870 5b 69 2f 32 5d 20 3d 20 30 3b 0a 20 20 7d 0a 20 [i/2] = 0;. }.
b7880 20 72 65 74 75 72 6e 20 7a 42 6c 6f 62 3b 0a 7d return zBlob;.}
b7890 0a 23 65 6e 64 69 66 20 2f 2a 20 21 53 51 4c 49 .#endif /* !SQLI
b78a0 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 TE_OMIT_BLOB_LIT
b78b0 45 52 41 4c 20 7c 7c 20 53 51 4c 49 54 45 5f 48 ERAL || SQLITE_H
b78c0 41 53 5f 43 4f 44 45 43 20 2a 2f 0a 0a 0a 2f 2a AS_CODEC */.../*
b78d0 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 73 .** Change the s
b78e0 71 6c 69 74 65 2e 6d 61 67 69 63 20 66 72 6f 6d qlite.magic from
b78f0 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 SQLITE_MAGIC_OP
b7900 45 4e 20 74 6f 20 53 51 4c 49 54 45 5f 4d 41 47 EN to SQLITE_MAG
b7910 49 43 5f 42 55 53 59 2e 0a 2a 2a 20 52 65 74 75 IC_BUSY..** Retu
b7920 72 6e 20 61 6e 20 65 72 72 6f 72 20 28 6e 6f 6e rn an error (non
b7930 2d 7a 65 72 6f 29 20 69 66 20 74 68 65 20 6d 61 -zero) if the ma
b7940 67 69 63 20 77 61 73 20 6e 6f 74 20 53 51 4c 49 gic was not SQLI
b7950 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 0a 2a 2a TE_MAGIC_OPEN.**
b7960 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 when this routi
b7970 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a ne is called..**
b7980 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
b7990 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 is called when
b79a0 65 6e 74 65 72 69 6e 67 20 61 6e 20 53 51 4c 69 entering an SQLi
b79b0 74 65 20 41 50 49 2e 20 20 54 68 65 20 53 51 4c te API. The SQL
b79c0 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 0a 2a ITE_MAGIC_OPEN.*
b79d0 2a 20 76 61 6c 75 65 20 69 6e 64 69 63 61 74 65 * value indicate
b79e0 73 20 74 68 61 74 20 74 68 65 20 64 61 74 61 62 s that the datab
b79f0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 ase connection p
b7a00 61 73 73 65 64 20 69 6e 74 6f 20 74 68 65 20 41 assed into the A
b7a10 50 49 20 69 73 0a 2a 2a 20 6f 70 65 6e 20 61 6e PI is.** open an
b7a20 64 20 69 73 20 6e 6f 74 20 62 65 69 6e 67 20 75 d is not being u
b7a30 73 65 64 20 62 79 20 61 6e 6f 74 68 65 72 20 74 sed by another t
b7a40 68 72 65 61 64 2e 20 20 42 79 20 63 68 61 6e 67 hread. By chang
b7a50 69 6e 67 20 74 68 65 20 76 61 6c 75 65 0a 2a 2a ing the value.**
b7a60 20 74 6f 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 to SQLITE_MAGIC
b7a70 5f 42 55 53 59 20 77 65 20 69 6e 64 69 63 61 74 _BUSY we indicat
b7a80 65 20 74 68 61 74 20 74 68 65 20 63 6f 6e 6e 65 e that the conne
b7a90 63 74 69 6f 6e 20 69 73 20 69 6e 20 75 73 65 2e ction is in use.
b7aa0 0a 2a 2a 20 73 71 6c 69 74 65 33 53 61 66 65 74 .** sqlite3Safet
b7ab0 79 4f 66 66 28 29 20 62 65 6c 6f 77 20 77 69 6c yOff() below wil
b7ac0 6c 20 63 68 61 6e 67 65 20 74 68 65 20 76 61 6c l change the val
b7ad0 75 65 20 62 61 63 6b 20 74 6f 20 53 51 4c 49 54 ue back to SQLIT
b7ae0 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 0a 2a 2a 20 E_MAGIC_OPEN.**
b7af0 77 68 65 6e 20 74 68 65 20 41 50 49 20 65 78 69 when the API exi
b7b00 74 73 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ts. .**.** This
b7b10 72 6f 75 74 69 6e 65 20 69 73 20 61 20 61 74 74 routine is a att
b7b20 65 6d 70 74 20 74 6f 20 64 65 74 65 63 74 20 69 empt to detect i
b7b30 66 20 74 77 6f 20 74 68 72 65 61 64 73 20 75 73 f two threads us
b7b40 65 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 73 71 e the.** same sq
b7b50 6c 69 74 65 2a 20 70 6f 69 6e 74 65 72 20 61 74 lite* pointer at
b7b60 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 2e 20 the same time.
b7b70 20 54 68 65 72 65 20 69 73 20 61 20 72 61 63 65 There is a race
b7b80 20 0a 2a 2a 20 63 6f 6e 64 69 74 69 6f 6e 20 73 .** condition s
b7b90 6f 20 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65 o it is possible
b7ba0 20 74 68 61 74 20 74 68 65 20 65 72 72 6f 72 20 that the error
b7bb0 69 73 20 6e 6f 74 20 64 65 74 65 63 74 65 64 2e is not detected.
b7bc0 0a 2a 2a 20 42 75 74 20 75 73 75 61 6c 6c 79 20 .** But usually
b7bd0 74 68 65 20 70 72 6f 62 6c 65 6d 20 77 69 6c 6c the problem will
b7be0 20 62 65 20 73 65 65 6e 2e 20 20 54 68 65 20 72 be seen. The r
b7bf0 65 73 75 6c 74 20 77 69 6c 6c 20 62 65 20 61 6e esult will be an
b7c00 0a 2a 2a 20 65 72 72 6f 72 20 77 68 69 63 68 20 .** error which
b7c10 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 64 can be used to d
b7c20 65 62 75 67 20 74 68 65 20 61 70 70 6c 69 63 61 ebug the applica
b7c30 74 69 6f 6e 20 74 68 61 74 20 69 73 0a 2a 2a 20 tion that is.**
b7c40 75 73 69 6e 67 20 53 51 4c 69 74 65 20 69 6e 63 using SQLite inc
b7c50 6f 72 72 65 63 74 6c 79 2e 0a 2a 2a 0a 2a 2a 20 orrectly..**.**
b7c60 54 69 63 6b 65 74 20 23 32 30 32 3a 20 20 49 66 Ticket #202: If
b7c70 20 64 62 2d 3e 6d 61 67 69 63 20 69 73 20 6e 6f db->magic is no
b7c80 74 20 61 20 76 61 6c 69 64 20 6f 70 65 6e 20 76 t a valid open v
b7c90 61 6c 75 65 2c 20 74 61 6b 65 20 63 61 72 65 20 alue, take care
b7ca0 6e 6f 74 0a 2a 2a 20 74 6f 20 6d 6f 64 69 66 79 not.** to modify
b7cb0 20 74 68 65 20 64 62 20 73 74 72 75 63 74 75 72 the db structur
b7cc0 65 20 61 74 20 61 6c 6c 2e 20 20 49 74 20 63 6f e at all. It co
b7cd0 75 6c 64 20 62 65 20 74 68 61 74 20 64 62 20 69 uld be that db i
b7ce0 73 20 61 20 73 74 61 6c 65 0a 2a 2a 20 70 6f 69 s a stale.** poi
b7cf0 6e 74 65 72 2e 20 20 49 6e 20 6f 74 68 65 72 20 nter. In other
b7d00 77 6f 72 64 73 2c 20 69 74 20 63 6f 75 6c 64 20 words, it could
b7d10 62 65 20 74 68 61 74 20 74 68 65 72 65 20 68 61 be that there ha
b7d20 73 20 62 65 65 6e 20 61 20 70 72 69 6f 72 0a 2a s been a prior.*
b7d30 2a 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 * call to sqlite
b7d40 33 5f 63 6c 6f 73 65 28 64 62 29 20 61 6e 64 20 3_close(db) and
b7d50 64 62 20 68 61 73 20 62 65 65 6e 20 64 65 61 6c db has been deal
b7d60 6c 6f 63 61 74 65 64 2e 20 20 41 6e 64 20 77 65 located. And we
b7d70 20 64 6f 0a 2a 2a 20 6e 6f 74 20 77 61 6e 74 20 do.** not want
b7d80 74 6f 20 77 72 69 74 65 20 69 6e 74 6f 20 64 65 to write into de
b7d90 61 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72 79 allocated memory
b7da0 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 ..*/.#ifdef SQLI
b7db0 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 5f TE_DEBUG.SQLITE_
b7dc0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
b7dd0 74 65 33 53 61 66 65 74 79 4f 6e 28 73 71 6c 69 te3SafetyOn(sqli
b7de0 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 66 28 20 te3 *db){. if(
b7df0 64 62 2d 3e 6d 61 67 69 63 3d 3d 53 51 4c 49 54 db->magic==SQLIT
b7e00 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 20 29 7b 0a E_MAGIC_OPEN ){.
b7e10 20 20 20 20 64 62 2d 3e 6d 61 67 69 63 20 3d 20 db->magic =
b7e20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 SQLITE_MAGIC_BUS
b7e30 59 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 Y;. assert( s
b7e40 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
b7e50 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a d(db->mutex) );.
b7e60 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 return 0;.
b7e70 7d 65 6c 73 65 20 69 66 28 20 64 62 2d 3e 6d 61 }else if( db->ma
b7e80 67 69 63 3d 3d 53 51 4c 49 54 45 5f 4d 41 47 49 gic==SQLITE_MAGI
b7e90 43 5f 42 55 53 59 20 29 7b 0a 20 20 20 20 64 62 C_BUSY ){. db
b7ea0 2d 3e 6d 61 67 69 63 20 3d 20 53 51 4c 49 54 45 ->magic = SQLITE
b7eb0 5f 4d 41 47 49 43 5f 45 52 52 4f 52 3b 0a 20 20 _MAGIC_ERROR;.
b7ec0 20 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72 db->u1.isInter
b7ed0 72 75 70 74 65 64 20 3d 20 31 3b 0a 20 20 7d 0a rupted = 1;. }.
b7ee0 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 return 1;.}.#e
b7ef0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e ndif../*.** Chan
b7f00 67 65 20 74 68 65 20 6d 61 67 69 63 20 66 72 6f ge the magic fro
b7f10 6d 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 m SQLITE_MAGIC_B
b7f20 55 53 59 20 74 6f 20 53 51 4c 49 54 45 5f 4d 41 USY to SQLITE_MA
b7f30 47 49 43 5f 4f 50 45 4e 2e 0a 2a 2a 20 52 65 74 GIC_OPEN..** Ret
b7f40 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 28 6e 6f urn an error (no
b7f50 6e 2d 7a 65 72 6f 29 20 69 66 20 74 68 65 20 6d n-zero) if the m
b7f60 61 67 69 63 20 77 61 73 20 6e 6f 74 20 53 51 4c agic was not SQL
b7f70 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 0a 2a ITE_MAGIC_BUSY.*
b7f80 2a 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 * when this rout
b7f90 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a ine is called..*
b7fa0 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
b7fb0 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 DEBUG.SQLITE_PRI
b7fc0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
b7fd0 53 61 66 65 74 79 4f 66 66 28 73 71 6c 69 74 65 SafetyOff(sqlite
b7fe0 33 20 2a 64 62 29 7b 0a 20 20 69 66 28 20 64 62 3 *db){. if( db
b7ff0 2d 3e 6d 61 67 69 63 3d 3d 53 51 4c 49 54 45 5f ->magic==SQLITE_
b8000 4d 41 47 49 43 5f 42 55 53 59 20 29 7b 0a 20 20 MAGIC_BUSY ){.
b8010 20 20 64 62 2d 3e 6d 61 67 69 63 20 3d 20 53 51 db->magic = SQ
b8020 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 3b LITE_MAGIC_OPEN;
b8030 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c . assert( sql
b8040 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
b8050 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 db->mutex) );.
b8060 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65 return 0;. }e
b8070 6c 73 65 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 67 lse{. db->mag
b8080 69 63 20 3d 20 53 51 4c 49 54 45 5f 4d 41 47 49 ic = SQLITE_MAGI
b8090 43 5f 45 52 52 4f 52 3b 0a 20 20 20 20 64 62 2d C_ERROR;. db-
b80a0 3e 75 31 2e 69 73 49 6e 74 65 72 72 75 70 74 65 >u1.isInterrupte
b80b0 64 20 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72 d = 1;. retur
b80c0 6e 20 31 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 n 1;. }.}.#endi
b80d0 66 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 f../*.** Check t
b80e0 6f 20 6d 61 6b 65 20 73 75 72 65 20 77 65 20 68 o make sure we h
b80f0 61 76 65 20 61 20 76 61 6c 69 64 20 64 62 20 70 ave a valid db p
b8100 6f 69 6e 74 65 72 2e 20 20 54 68 69 73 20 74 65 ointer. This te
b8110 73 74 20 69 73 20 6e 6f 74 0a 2a 2a 20 66 6f 6f st is not.** foo
b8120 6c 70 72 6f 6f 66 20 62 75 74 20 69 74 20 64 6f lproof but it do
b8130 65 73 20 70 72 6f 76 69 64 65 20 73 6f 6d 65 20 es provide some
b8140 6d 65 61 73 75 72 65 20 6f 66 20 70 72 6f 74 65 measure of prote
b8150 63 74 69 6f 6e 20 61 67 61 69 6e 73 74 0a 2a 2a ction against.**
b8160 20 6d 69 73 75 73 65 20 6f 66 20 74 68 65 20 69 misuse of the i
b8170 6e 74 65 72 66 61 63 65 20 73 75 63 68 20 61 73 nterface such as
b8180 20 70 61 73 73 69 6e 67 20 69 6e 20 64 62 20 70 passing in db p
b8190 6f 69 6e 74 65 72 73 20 74 68 61 74 20 61 72 65 ointers that are
b81a0 0a 2a 2a 20 4e 55 4c 4c 20 6f 72 20 77 68 69 63 .** NULL or whic
b81b0 68 20 68 61 76 65 20 62 65 65 6e 20 70 72 65 76 h have been prev
b81c0 69 6f 75 73 6c 79 20 63 6c 6f 73 65 64 2e 20 20 iously closed.
b81d0 49 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 If this routine
b81e0 72 65 74 75 72 6e 73 0a 2a 2a 20 31 20 69 74 20 returns.** 1 it
b81f0 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 64 means that the d
b8200 62 20 70 6f 69 6e 74 65 72 20 69 73 20 76 61 6c b pointer is val
b8210 69 64 20 61 6e 64 20 30 20 69 66 20 69 74 20 73 id and 0 if it s
b8220 68 6f 75 6c 64 20 6e 6f 74 20 62 65 0a 2a 2a 20 hould not be.**
b8230 64 65 72 65 66 65 72 65 6e 63 65 64 20 66 6f 72 dereferenced for
b8240 20 61 6e 79 20 72 65 61 73 6f 6e 2e 20 20 54 68 any reason. Th
b8250 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 e calling functi
b8260 6f 6e 20 73 68 6f 75 6c 64 20 69 6e 76 6f 6b 65 on should invoke
b8270 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 .** SQLITE_MISUS
b8280 45 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 0a 2a E immediately..*
b8290 2a 0a 2a 2a 20 73 71 6c 69 74 65 33 53 61 66 65 *.** sqlite3Safe
b82a0 74 79 43 68 65 63 6b 4f 6b 28 29 20 72 65 71 75 tyCheckOk() requ
b82b0 69 72 65 73 20 74 68 61 74 20 74 68 65 20 64 62 ires that the db
b82c0 20 70 6f 69 6e 74 65 72 20 62 65 20 76 61 6c 69 pointer be vali
b82d0 64 20 66 6f 72 0a 2a 2a 20 75 73 65 2e 20 20 73 d for.** use. s
b82e0 71 6c 69 74 65 33 53 61 66 65 74 79 43 68 65 63 qlite3SafetyChec
b82f0 6b 53 69 63 6b 4f 72 4f 6b 28 29 20 61 6c 6c 6f kSickOrOk() allo
b8300 77 73 20 61 20 64 62 20 70 6f 69 6e 74 65 72 20 ws a db pointer
b8310 74 68 61 74 20 66 61 69 6c 65 64 20 74 6f 0a 2a that failed to.*
b8320 2a 20 6f 70 65 6e 20 70 72 6f 70 65 72 6c 79 20 * open properly
b8330 61 6e 64 20 69 73 20 6e 6f 74 20 66 69 74 20 66 and is not fit f
b8340 6f 72 20 67 65 6e 65 72 61 6c 20 75 73 65 20 62 or general use b
b8350 75 74 20 77 68 69 63 68 20 63 61 6e 20 62 65 0a ut which can be.
b8360 2a 2a 20 75 73 65 64 20 61 73 20 61 6e 20 61 72 ** used as an ar
b8370 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 gument to sqlite
b8380 33 5f 65 72 72 6d 73 67 28 29 20 6f 72 20 73 71 3_errmsg() or sq
b8390 6c 69 74 65 33 5f 63 6c 6f 73 65 28 29 2e 0a 2a lite3_close()..*
b83a0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
b83b0 20 69 6e 74 20 73 71 6c 69 74 65 33 53 61 66 65 int sqlite3Safe
b83c0 74 79 43 68 65 63 6b 4f 6b 28 73 71 6c 69 74 65 tyCheckOk(sqlite
b83d0 33 20 2a 64 62 29 7b 0a 20 20 75 33 32 20 6d 61 3 *db){. u32 ma
b83e0 67 69 63 3b 0a 20 20 69 66 28 20 64 62 3d 3d 30 gic;. if( db==0
b83f0 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 6d ) return 0;. m
b8400 61 67 69 63 20 3d 20 64 62 2d 3e 6d 61 67 69 63 agic = db->magic
b8410 3b 0a 20 20 69 66 28 20 6d 61 67 69 63 21 3d 53 ;. if( magic!=S
b8420 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e QLITE_MAGIC_OPEN
b8430 20 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f .#ifdef SQLITE_
b8440 44 45 42 55 47 0a 20 20 20 20 20 26 26 20 6d 61 DEBUG. && ma
b8450 67 69 63 21 3d 53 51 4c 49 54 45 5f 4d 41 47 49 gic!=SQLITE_MAGI
b8460 43 5f 42 55 53 59 0a 23 65 6e 64 69 66 0a 20 20 C_BUSY.#endif.
b8470 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b ){. return 0;
b8480 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 . }else{. re
b8490 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 7d 0a 53 51 turn 1;. }.}.SQ
b84a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
b84b0 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 43 68 sqlite3SafetyCh
b84c0 65 63 6b 53 69 63 6b 4f 72 4f 6b 28 73 71 6c 69 eckSickOrOk(sqli
b84d0 74 65 33 20 2a 64 62 29 7b 0a 20 20 75 33 32 20 te3 *db){. u32
b84e0 6d 61 67 69 63 3b 0a 20 20 6d 61 67 69 63 20 3d magic;. magic =
b84f0 20 64 62 2d 3e 6d 61 67 69 63 3b 0a 20 20 69 66 db->magic;. if
b8500 28 20 6d 61 67 69 63 21 3d 53 51 4c 49 54 45 5f ( magic!=SQLITE_
b8510 4d 41 47 49 43 5f 53 49 43 4b 20 26 26 0a 20 20 MAGIC_SICK &&.
b8520 20 20 20 20 6d 61 67 69 63 21 3d 53 51 4c 49 54 magic!=SQLIT
b8530 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 20 26 26 0a E_MAGIC_OPEN &&.
b8540 20 20 20 20 20 20 6d 61 67 69 63 21 3d 53 51 4c magic!=SQL
b8550 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 20 29 ITE_MAGIC_BUSY )
b8560 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 72 65 74 return 0;. ret
b8570 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a urn 1;.}../*****
b8580 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 ********* End of
b8590 20 75 74 69 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a util.c ********
b85a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b85b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b85c0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
b85d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 ********* Begin
b85e0 66 69 6c 65 20 68 61 73 68 2e 63 20 2a 2a 2a 2a file hash.c ****
b85f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b8600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b8610 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 ********/./*.**
b8620 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 32 2001 September 2
b8630 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 2.**.** The auth
b8640 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
b8650 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
b8660 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
b8670 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
b8680 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
b8690 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
b86a0 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
b86b0 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
b86c0 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
b86d0 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
b86e0 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
b86f0 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
b8700 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
b8710 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
b8720 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
b8730 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
b8740 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
b8750 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b8760 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b8770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b8780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b8790 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 .** This is the
b87a0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f implementation o
b87b0 66 20 67 65 6e 65 72 69 63 20 68 61 73 68 2d 74 f generic hash-t
b87c0 61 62 6c 65 73 0a 2a 2a 20 75 73 65 64 20 69 6e ables.** used in
b87d0 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 24 SQLite..**.** $
b87e0 49 64 3a 20 68 61 73 68 2e 63 2c 76 20 31 2e 33 Id: hash.c,v 1.3
b87f0 38 20 32 30 30 39 2f 30 35 2f 30 39 20 32 33 3a 8 2009/05/09 23:
b8800 32 39 3a 31 32 20 64 72 68 20 45 78 70 20 24 0a 29:12 drh Exp $.
b8810 2a 2f 0a 0a 2f 2a 20 54 75 72 6e 20 62 75 6c 6b */../* Turn bulk
b8820 20 6d 65 6d 6f 72 79 20 69 6e 74 6f 20 61 20 68 memory into a h
b8830 61 73 68 20 74 61 62 6c 65 20 6f 62 6a 65 63 74 ash table object
b8840 20 62 79 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 by initializing
b8850 20 74 68 65 0a 2a 2a 20 66 69 65 6c 64 73 20 6f the.** fields o
b8860 66 20 74 68 65 20 48 61 73 68 20 73 74 72 75 63 f the Hash struc
b8870 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 22 70 4e 65 ture..**.** "pNe
b8880 77 22 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 w" is a pointer
b8890 74 6f 20 74 68 65 20 68 61 73 68 20 74 61 62 6c to the hash tabl
b88a0 65 20 74 68 61 74 20 69 73 20 74 6f 20 62 65 20 e that is to be
b88b0 69 6e 69 74 69 61 6c 69 7a 65 64 2e 0a 2a 2f 0a initialized..*/.
b88c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
b88d0 6f 69 64 20 73 71 6c 69 74 65 33 48 61 73 68 49 oid sqlite3HashI
b88e0 6e 69 74 28 48 61 73 68 20 2a 70 4e 65 77 29 7b nit(Hash *pNew){
b88f0 0a 20 20 61 73 73 65 72 74 28 20 70 4e 65 77 21 . assert( pNew!
b8900 3d 30 20 29 3b 0a 20 20 70 4e 65 77 2d 3e 66 69 =0 );. pNew->fi
b8910 72 73 74 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d rst = 0;. pNew-
b8920 3e 63 6f 75 6e 74 20 3d 20 30 3b 0a 20 20 70 4e >count = 0;. pN
b8930 65 77 2d 3e 68 74 73 69 7a 65 20 3d 20 30 3b 0a ew->htsize = 0;.
b8940 20 20 70 4e 65 77 2d 3e 68 74 20 3d 20 30 3b 0a pNew->ht = 0;.
b8950 7d 0a 0a 2f 2a 20 52 65 6d 6f 76 65 20 61 6c 6c }../* Remove all
b8960 20 65 6e 74 72 69 65 73 20 66 72 6f 6d 20 61 20 entries from a
b8970 68 61 73 68 20 74 61 62 6c 65 2e 20 20 52 65 63 hash table. Rec
b8980 6c 61 69 6d 20 61 6c 6c 20 6d 65 6d 6f 72 79 2e laim all memory.
b8990 0a 2a 2a 20 43 61 6c 6c 20 74 68 69 73 20 72 6f .** Call this ro
b89a0 75 74 69 6e 65 20 74 6f 20 64 65 6c 65 74 65 20 utine to delete
b89b0 61 20 68 61 73 68 20 74 61 62 6c 65 20 6f 72 20 a hash table or
b89c0 74 6f 20 72 65 73 65 74 20 61 20 68 61 73 68 20 to reset a hash
b89d0 74 61 62 6c 65 0a 2a 2a 20 74 6f 20 74 68 65 20 table.** to the
b89e0 65 6d 70 74 79 20 73 74 61 74 65 2e 0a 2a 2f 0a empty state..*/.
b89f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
b8a00 6f 69 64 20 73 71 6c 69 74 65 33 48 61 73 68 43 oid sqlite3HashC
b8a10 6c 65 61 72 28 48 61 73 68 20 2a 70 48 29 7b 0a lear(Hash *pH){.
b8a20 20 20 48 61 73 68 45 6c 65 6d 20 2a 65 6c 65 6d HashElem *elem
b8a30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 ; /* For
b8a40 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 61 6c looping over al
b8a50 6c 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 l elements of th
b8a60 65 20 74 61 62 6c 65 20 2a 2f 0a 0a 20 20 61 73 e table */.. as
b8a70 73 65 72 74 28 20 70 48 21 3d 30 20 29 3b 0a 20 sert( pH!=0 );.
b8a80 20 65 6c 65 6d 20 3d 20 70 48 2d 3e 66 69 72 73 elem = pH->firs
b8a90 74 3b 0a 20 20 70 48 2d 3e 66 69 72 73 74 20 3d t;. pH->first =
b8aa0 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 0;. sqlite3_fr
b8ab0 65 65 28 70 48 2d 3e 68 74 29 3b 0a 20 20 70 48 ee(pH->ht);. pH
b8ac0 2d 3e 68 74 20 3d 20 30 3b 0a 20 20 70 48 2d 3e ->ht = 0;. pH->
b8ad0 68 74 73 69 7a 65 20 3d 20 30 3b 0a 20 20 77 68 htsize = 0;. wh
b8ae0 69 6c 65 28 20 65 6c 65 6d 20 29 7b 0a 20 20 20 ile( elem ){.
b8af0 20 48 61 73 68 45 6c 65 6d 20 2a 6e 65 78 74 5f HashElem *next_
b8b00 65 6c 65 6d 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 elem = elem->nex
b8b10 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 t;. sqlite3_f
b8b20 72 65 65 28 65 6c 65 6d 29 3b 0a 20 20 20 20 65 ree(elem);. e
b8b30 6c 65 6d 20 3d 20 6e 65 78 74 5f 65 6c 65 6d 3b lem = next_elem;
b8b40 0a 20 20 7d 0a 20 20 70 48 2d 3e 63 6f 75 6e 74 . }. pH->count
b8b50 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 = 0;.}../*.** T
b8b60 68 65 20 68 61 73 68 69 6e 67 20 66 75 6e 63 74 he hashing funct
b8b70 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 75 ion..*/.static u
b8b80 6e 73 69 67 6e 65 64 20 69 6e 74 20 73 74 72 48 nsigned int strH
b8b90 61 73 68 28 63 6f 6e 73 74 20 63 68 61 72 20 2a ash(const char *
b8ba0 7a 2c 20 69 6e 74 20 6e 4b 65 79 29 7b 0a 20 20 z, int nKey){.
b8bb0 69 6e 74 20 68 20 3d 20 30 3b 0a 20 20 61 73 73 int h = 0;. ass
b8bc0 65 72 74 28 20 6e 4b 65 79 3e 3d 30 20 29 3b 0a ert( nKey>=0 );.
b8bd0 20 20 77 68 69 6c 65 28 20 6e 4b 65 79 20 3e 20 while( nKey >
b8be0 30 20 20 29 7b 0a 20 20 20 20 68 20 3d 20 28 68 0 ){. h = (h
b8bf0 3c 3c 33 29 20 5e 20 68 20 5e 20 73 71 6c 69 74 <<3) ^ h ^ sqlit
b8c00 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 28 e3UpperToLower[(
b8c10 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 2a 7a unsigned char)*z
b8c20 2b 2b 5d 3b 0a 20 20 20 20 6e 4b 65 79 2d 2d 3b ++];. nKey--;
b8c30 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 68 3b . }. return h;
b8c40 0a 7d 0a 0a 0a 2f 2a 20 4c 69 6e 6b 20 70 4e 65 .}.../* Link pNe
b8c50 77 20 65 6c 65 6d 65 6e 74 20 69 6e 74 6f 20 74 w element into t
b8c60 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 70 48 he hash table pH
b8c70 2e 20 20 49 66 20 70 45 6e 74 72 79 21 3d 30 20 . If pEntry!=0
b8c80 74 68 65 6e 20 61 6c 73 6f 0a 2a 2a 20 69 6e 73 then also.** ins
b8c90 65 72 74 20 70 4e 65 77 20 69 6e 74 6f 20 74 68 ert pNew into th
b8ca0 65 20 70 45 6e 74 72 79 20 68 61 73 68 20 62 75 e pEntry hash bu
b8cb0 63 6b 65 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 cket..*/.static
b8cc0 76 6f 69 64 20 69 6e 73 65 72 74 45 6c 65 6d 65 void insertEleme
b8cd0 6e 74 28 0a 20 20 48 61 73 68 20 2a 70 48 2c 20 nt(. Hash *pH,
b8ce0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
b8cf0 54 68 65 20 63 6f 6d 70 6c 65 74 65 20 68 61 73 The complete has
b8d00 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20 73 74 72 h table */. str
b8d10 75 63 74 20 5f 68 74 20 2a 70 45 6e 74 72 79 2c uct _ht *pEntry,
b8d20 20 20 20 20 2f 2a 20 54 68 65 20 65 6e 74 72 79 /* The entry
b8d30 20 69 6e 74 6f 20 77 68 69 63 68 20 70 4e 65 77 into which pNew
b8d40 20 69 73 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a is inserted */.
b8d50 20 20 48 61 73 68 45 6c 65 6d 20 2a 70 4e 65 77 HashElem *pNew
b8d60 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
b8d70 65 6c 65 6d 65 6e 74 20 74 6f 20 62 65 20 69 6e element to be in
b8d80 73 65 72 74 65 64 20 2a 2f 0a 29 7b 0a 20 20 48 serted */.){. H
b8d90 61 73 68 45 6c 65 6d 20 2a 70 48 65 61 64 3b 20 ashElem *pHead;
b8da0 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 65 /* First e
b8db0 6c 65 6d 65 6e 74 20 61 6c 72 65 61 64 79 20 69 lement already i
b8dc0 6e 20 70 45 6e 74 72 79 20 2a 2f 0a 20 20 69 66 n pEntry */. if
b8dd0 28 20 70 45 6e 74 72 79 20 29 7b 0a 20 20 20 20 ( pEntry ){.
b8de0 70 48 65 61 64 20 3d 20 70 45 6e 74 72 79 2d 3e pHead = pEntry->
b8df0 63 6f 75 6e 74 20 3f 20 70 45 6e 74 72 79 2d 3e count ? pEntry->
b8e00 63 68 61 69 6e 20 3a 20 30 3b 0a 20 20 20 20 70 chain : 0;. p
b8e10 45 6e 74 72 79 2d 3e 63 6f 75 6e 74 2b 2b 3b 0a Entry->count++;.
b8e20 20 20 20 20 70 45 6e 74 72 79 2d 3e 63 68 61 69 pEntry->chai
b8e30 6e 20 3d 20 70 4e 65 77 3b 0a 20 20 7d 65 6c 73 n = pNew;. }els
b8e40 65 7b 0a 20 20 20 20 70 48 65 61 64 20 3d 20 30 e{. pHead = 0
b8e50 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 48 65 61 ;. }. if( pHea
b8e60 64 20 29 7b 0a 20 20 20 20 70 4e 65 77 2d 3e 6e d ){. pNew->n
b8e70 65 78 74 20 3d 20 70 48 65 61 64 3b 0a 20 20 20 ext = pHead;.
b8e80 20 70 4e 65 77 2d 3e 70 72 65 76 20 3d 20 70 48 pNew->prev = pH
b8e90 65 61 64 2d 3e 70 72 65 76 3b 0a 20 20 20 20 69 ead->prev;. i
b8ea0 66 28 20 70 48 65 61 64 2d 3e 70 72 65 76 20 29 f( pHead->prev )
b8eb0 7b 20 70 48 65 61 64 2d 3e 70 72 65 76 2d 3e 6e { pHead->prev->n
b8ec0 65 78 74 20 3d 20 70 4e 65 77 3b 20 7d 0a 20 20 ext = pNew; }.
b8ed0 20 20 65 6c 73 65 20 20 20 20 20 20 20 20 20 20 else
b8ee0 20 20 20 7b 20 70 48 2d 3e 66 69 72 73 74 20 3d { pH->first =
b8ef0 20 70 4e 65 77 3b 20 7d 0a 20 20 20 20 70 48 65 pNew; }. pHe
b8f00 61 64 2d 3e 70 72 65 76 20 3d 20 70 4e 65 77 3b ad->prev = pNew;
b8f10 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4e . }else{. pN
b8f20 65 77 2d 3e 6e 65 78 74 20 3d 20 70 48 2d 3e 66 ew->next = pH->f
b8f30 69 72 73 74 3b 0a 20 20 20 20 69 66 28 20 70 48 irst;. if( pH
b8f40 2d 3e 66 69 72 73 74 20 29 7b 20 70 48 2d 3e 66 ->first ){ pH->f
b8f50 69 72 73 74 2d 3e 70 72 65 76 20 3d 20 70 4e 65 irst->prev = pNe
b8f60 77 3b 20 7d 0a 20 20 20 20 70 4e 65 77 2d 3e 70 w; }. pNew->p
b8f70 72 65 76 20 3d 20 30 3b 0a 20 20 20 20 70 48 2d rev = 0;. pH-
b8f80 3e 66 69 72 73 74 20 3d 20 70 4e 65 77 3b 0a 20 >first = pNew;.
b8f90 20 7d 0a 7d 0a 0a 0a 2f 2a 20 52 65 73 69 7a 65 }.}.../* Resize
b8fa0 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 the hash table
b8fb0 73 6f 20 74 68 61 74 20 69 74 20 63 61 6e 74 61 so that it canta
b8fc0 69 6e 73 20 22 6e 65 77 5f 73 69 7a 65 22 20 62 ins "new_size" b
b8fd0 75 63 6b 65 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 uckets..**.** Th
b8fe0 65 20 68 61 73 68 20 74 61 62 6c 65 20 6d 69 67 e hash table mig
b8ff0 68 74 20 66 61 69 6c 20 74 6f 20 72 65 73 69 7a ht fail to resiz
b9000 65 20 69 66 20 73 71 6c 69 74 65 33 5f 6d 61 6c e if sqlite3_mal
b9010 6c 6f 63 28 29 20 66 61 69 6c 73 20 6f 72 0a 2a loc() fails or.*
b9020 2a 20 69 66 20 74 68 65 20 6e 65 77 20 73 69 7a * if the new siz
b9030 65 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 e is the same as
b9040 20 74 68 65 20 70 72 69 6f 72 20 73 69 7a 65 2e the prior size.
b9050 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 .** Return TRUE
b9060 69 66 20 74 68 65 20 72 65 73 69 7a 65 20 6f 63 if the resize oc
b9070 63 75 72 73 20 61 6e 64 20 66 61 6c 73 65 20 69 curs and false i
b9080 66 20 6e 6f 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 f not..*/.static
b9090 20 69 6e 74 20 72 65 68 61 73 68 28 48 61 73 68 int rehash(Hash
b90a0 20 2a 70 48 2c 20 75 6e 73 69 67 6e 65 64 20 69 *pH, unsigned i
b90b0 6e 74 20 6e 65 77 5f 73 69 7a 65 29 7b 0a 20 20 nt new_size){.
b90c0 73 74 72 75 63 74 20 5f 68 74 20 2a 6e 65 77 5f struct _ht *new_
b90d0 68 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f ht; /
b90e0 2a 20 54 68 65 20 6e 65 77 20 68 61 73 68 20 74 * The new hash t
b90f0 61 62 6c 65 20 2a 2f 0a 20 20 48 61 73 68 45 6c able */. HashEl
b9100 65 6d 20 2a 65 6c 65 6d 2c 20 2a 6e 65 78 74 5f em *elem, *next_
b9110 65 6c 65 6d 3b 20 20 20 20 2f 2a 20 46 6f 72 20 elem; /* For
b9120 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 65 78 69 looping over exi
b9130 73 74 69 6e 67 20 65 6c 65 6d 65 6e 74 73 20 2a sting elements *
b9140 2f 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 /..#if SQLITE_MA
b9150 4c 4c 4f 43 5f 53 4f 46 54 5f 4c 49 4d 49 54 3e LLOC_SOFT_LIMIT>
b9160 30 0a 20 20 69 66 28 20 6e 65 77 5f 73 69 7a 65 0. if( new_size
b9170 2a 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20 5f *sizeof(struct _
b9180 68 74 29 3e 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f ht)>SQLITE_MALLO
b9190 43 5f 53 4f 46 54 5f 4c 49 4d 49 54 20 29 7b 0a C_SOFT_LIMIT ){.
b91a0 20 20 20 20 6e 65 77 5f 73 69 7a 65 20 3d 20 53 new_size = S
b91b0 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 5f 53 4f 46 QLITE_MALLOC_SOF
b91c0 54 5f 4c 49 4d 49 54 2f 73 69 7a 65 6f 66 28 73 T_LIMIT/sizeof(s
b91d0 74 72 75 63 74 20 5f 68 74 29 3b 0a 20 20 7d 0a truct _ht);. }.
b91e0 20 20 69 66 28 20 6e 65 77 5f 73 69 7a 65 3d 3d if( new_size==
b91f0 70 48 2d 3e 68 74 73 69 7a 65 20 29 20 72 65 74 pH->htsize ) ret
b9200 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 20 urn 0;.#endif..
b9210 20 2f 2a 20 54 68 65 20 69 6e 61 62 69 6c 69 74 /* The inabilit
b9220 79 20 74 6f 20 61 6c 6c 6f 63 61 74 65 73 20 73 y to allocates s
b9230 70 61 63 65 20 66 6f 72 20 61 20 6c 61 72 67 65 pace for a large
b9240 72 20 68 61 73 68 20 74 61 62 6c 65 20 69 73 0a r hash table is.
b9250 20 20 2a 2a 20 61 20 70 65 72 66 6f 72 6d 61 6e ** a performan
b9260 63 65 20 68 69 74 20 62 75 74 20 69 74 20 69 73 ce hit but it is
b9270 20 6e 6f 74 20 61 20 66 61 74 61 6c 20 65 72 72 not a fatal err
b9280 6f 72 2e 20 20 53 6f 20 6d 61 72 6b 20 74 68 65 or. So mark the
b9290 0a 20 20 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e . ** allocation
b92a0 20 61 73 20 61 20 62 65 6e 69 67 6e 2e 0a 20 20 as a benign..
b92b0 2a 2f 0a 20 20 73 71 6c 69 74 65 33 42 65 67 69 */. sqlite3Begi
b92c0 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b nBenignMalloc();
b92d0 0a 20 20 6e 65 77 5f 68 74 20 3d 20 28 73 74 72 . new_ht = (str
b92e0 75 63 74 20 5f 68 74 20 2a 29 73 71 6c 69 74 65 uct _ht *)sqlite
b92f0 33 4d 61 6c 6c 6f 63 28 20 6e 65 77 5f 73 69 7a 3Malloc( new_siz
b9300 65 2a 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20 e*sizeof(struct
b9310 5f 68 74 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 _ht) );. sqlite
b9320 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 3EndBenignMalloc
b9330 28 29 3b 0a 0a 20 20 69 66 28 20 6e 65 77 5f 68 ();.. if( new_h
b9340 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b t==0 ) return 0;
b9350 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 . sqlite3_free(
b9360 70 48 2d 3e 68 74 29 3b 0a 20 20 70 48 2d 3e 68 pH->ht);. pH->h
b9370 74 20 3d 20 6e 65 77 5f 68 74 3b 0a 20 20 70 48 t = new_ht;. pH
b9380 2d 3e 68 74 73 69 7a 65 20 3d 20 6e 65 77 5f 73 ->htsize = new_s
b9390 69 7a 65 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c ize = sqlite3Mal
b93a0 6c 6f 63 53 69 7a 65 28 6e 65 77 5f 68 74 29 2f locSize(new_ht)/
b93b0 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20 5f 68 sizeof(struct _h
b93c0 74 29 3b 0a 20 20 6d 65 6d 73 65 74 28 6e 65 77 t);. memset(new
b93d0 5f 68 74 2c 20 30 2c 20 6e 65 77 5f 73 69 7a 65 _ht, 0, new_size
b93e0 2a 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20 5f *sizeof(struct _
b93f0 68 74 29 29 3b 0a 20 20 66 6f 72 28 65 6c 65 6d ht));. for(elem
b9400 3d 70 48 2d 3e 66 69 72 73 74 2c 20 70 48 2d 3e =pH->first, pH->
b9410 66 69 72 73 74 3d 30 3b 20 65 6c 65 6d 3b 20 65 first=0; elem; e
b9420 6c 65 6d 20 3d 20 6e 65 78 74 5f 65 6c 65 6d 29 lem = next_elem)
b9430 7b 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 69 {. unsigned i
b9440 6e 74 20 68 20 3d 20 73 74 72 48 61 73 68 28 65 nt h = strHash(e
b9450 6c 65 6d 2d 3e 70 4b 65 79 2c 20 65 6c 65 6d 2d lem->pKey, elem-
b9460 3e 6e 4b 65 79 29 20 25 20 6e 65 77 5f 73 69 7a >nKey) % new_siz
b9470 65 3b 0a 20 20 20 20 6e 65 78 74 5f 65 6c 65 6d e;. next_elem
b9480 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20 = elem->next;.
b9490 20 20 20 69 6e 73 65 72 74 45 6c 65 6d 65 6e 74 insertElement
b94a0 28 70 48 2c 20 26 6e 65 77 5f 68 74 5b 68 5d 2c (pH, &new_ht[h],
b94b0 20 65 6c 65 6d 29 3b 0a 20 20 7d 0a 20 20 72 65 elem);. }. re
b94c0 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 20 54 68 turn 1;.}../* Th
b94d0 69 73 20 66 75 6e 63 74 69 6f 6e 20 28 66 6f 72 is function (for
b94e0 20 69 6e 74 65 72 6e 61 6c 20 75 73 65 20 6f 6e internal use on
b94f0 6c 79 29 20 6c 6f 63 61 74 65 73 20 61 6e 20 65 ly) locates an e
b9500 6c 65 6d 65 6e 74 20 69 6e 20 61 6e 0a 2a 2a 20 lement in an.**
b9510 68 61 73 68 20 74 61 62 6c 65 20 74 68 61 74 20 hash table that
b9520 6d 61 74 63 68 65 73 20 74 68 65 20 67 69 76 65 matches the give
b9530 6e 20 6b 65 79 2e 20 20 54 68 65 20 68 61 73 68 n key. The hash
b9540 20 66 6f 72 20 74 68 69 73 20 6b 65 79 20 68 61 for this key ha
b9550 73 0a 2a 2a 20 61 6c 72 65 61 64 79 20 62 65 65 s.** already bee
b9560 6e 20 63 6f 6d 70 75 74 65 64 20 61 6e 64 20 69 n computed and i
b9570 73 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 s passed as the
b9580 34 74 68 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 4th parameter..*
b9590 2f 0a 73 74 61 74 69 63 20 48 61 73 68 45 6c 65 /.static HashEle
b95a0 6d 20 2a 66 69 6e 64 45 6c 65 6d 65 6e 74 47 69 m *findElementGi
b95b0 76 65 6e 48 61 73 68 28 0a 20 20 63 6f 6e 73 74 venHash(. const
b95c0 20 48 61 73 68 20 2a 70 48 2c 20 20 20 20 20 2f Hash *pH, /
b95d0 2a 20 54 68 65 20 70 48 20 74 6f 20 62 65 20 73 * The pH to be s
b95e0 65 61 72 63 68 65 64 20 2a 2f 0a 20 20 63 6f 6e earched */. con
b95f0 73 74 20 63 68 61 72 20 2a 70 4b 65 79 2c 20 20 st char *pKey,
b9600 20 2f 2a 20 54 68 65 20 6b 65 79 20 77 65 20 61 /* The key we a
b9610 72 65 20 73 65 61 72 63 68 69 6e 67 20 66 6f 72 re searching for
b9620 20 2a 2f 0a 20 20 69 6e 74 20 6e 4b 65 79 2c 20 */. int nKey,
b9630 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 /* Byt
b9640 65 73 20 69 6e 20 6b 65 79 20 28 6e 6f 74 20 63 es in key (not c
b9650 6f 75 6e 74 69 6e 67 20 7a 65 72 6f 20 74 65 72 ounting zero ter
b9660 6d 69 6e 61 74 6f 72 29 20 2a 2f 0a 20 20 75 6e minator) */. un
b9670 73 69 67 6e 65 64 20 69 6e 74 20 68 20 20 20 20 signed int h
b9680 20 20 2f 2a 20 54 68 65 20 68 61 73 68 20 66 6f /* The hash fo
b9690 72 20 74 68 69 73 20 6b 65 79 2e 20 2a 2f 0a 29 r this key. */.)
b96a0 7b 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 65 6c {. HashElem *el
b96b0 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 em;
b96c0 20 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 6c 6f /* Used to lo
b96d0 6f 70 20 74 68 72 75 20 74 68 65 20 65 6c 65 6d op thru the elem
b96e0 65 6e 74 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e ent list */. in
b96f0 74 20 63 6f 75 6e 74 3b 20 20 20 20 20 20 20 20 t count;
b9700 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
b9710 4e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e Number of elemen
b9720 74 73 20 6c 65 66 74 20 74 6f 20 74 65 73 74 20 ts left to test
b9730 2a 2f 0a 0a 20 20 69 66 28 20 70 48 2d 3e 68 74 */.. if( pH->ht
b9740 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 5f ){. struct _
b9750 68 74 20 2a 70 45 6e 74 72 79 20 3d 20 26 70 48 ht *pEntry = &pH
b9760 2d 3e 68 74 5b 68 5d 3b 0a 20 20 20 20 65 6c 65 ->ht[h];. ele
b9770 6d 20 3d 20 70 45 6e 74 72 79 2d 3e 63 68 61 69 m = pEntry->chai
b9780 6e 3b 0a 20 20 20 20 63 6f 75 6e 74 20 3d 20 70 n;. count = p
b9790 45 6e 74 72 79 2d 3e 63 6f 75 6e 74 3b 0a 20 20 Entry->count;.
b97a0 7d 65 6c 73 65 7b 0a 20 20 20 20 65 6c 65 6d 20 }else{. elem
b97b0 3d 20 70 48 2d 3e 66 69 72 73 74 3b 0a 20 20 20 = pH->first;.
b97c0 20 63 6f 75 6e 74 20 3d 20 70 48 2d 3e 63 6f 75 count = pH->cou
b97d0 6e 74 3b 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28 nt;. }. while(
b97e0 20 63 6f 75 6e 74 2d 2d 20 26 26 20 41 4c 57 41 count-- && ALWA
b97f0 59 53 28 65 6c 65 6d 29 20 29 7b 0a 20 20 20 20 YS(elem) ){.
b9800 69 66 28 20 65 6c 65 6d 2d 3e 6e 4b 65 79 3d 3d if( elem->nKey==
b9810 6e 4b 65 79 20 26 26 20 73 71 6c 69 74 65 33 53 nKey && sqlite3S
b9820 74 72 4e 49 43 6d 70 28 65 6c 65 6d 2d 3e 70 4b trNICmp(elem->pK
b9830 65 79 2c 70 4b 65 79 2c 6e 4b 65 79 29 3d 3d 30 ey,pKey,nKey)==0
b9840 20 29 7b 20 0a 20 20 20 20 20 20 72 65 74 75 72 ){ . retur
b9850 6e 20 65 6c 65 6d 3b 0a 20 20 20 20 7d 0a 20 20 n elem;. }.
b9860 20 20 65 6c 65 6d 20 3d 20 65 6c 65 6d 2d 3e 6e elem = elem->n
b9870 65 78 74 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 ext;. }. retur
b9880 6e 20 30 3b 0a 7d 0a 0a 2f 2a 20 52 65 6d 6f 76 n 0;.}../* Remov
b9890 65 20 61 20 73 69 6e 67 6c 65 20 65 6e 74 72 79 e a single entry
b98a0 20 66 72 6f 6d 20 74 68 65 20 68 61 73 68 20 74 from the hash t
b98b0 61 62 6c 65 20 67 69 76 65 6e 20 61 20 70 6f 69 able given a poi
b98c0 6e 74 65 72 20 74 6f 20 74 68 61 74 0a 2a 2a 20 nter to that.**
b98d0 65 6c 65 6d 65 6e 74 20 61 6e 64 20 61 20 68 61 element and a ha
b98e0 73 68 20 6f 6e 20 74 68 65 20 65 6c 65 6d 65 6e sh on the elemen
b98f0 74 27 73 20 6b 65 79 2e 0a 2a 2f 0a 73 74 61 74 t's key..*/.stat
b9900 69 63 20 76 6f 69 64 20 72 65 6d 6f 76 65 45 6c ic void removeEl
b9910 65 6d 65 6e 74 47 69 76 65 6e 48 61 73 68 28 0a ementGivenHash(.
b9920 20 20 48 61 73 68 20 2a 70 48 2c 20 20 20 20 20 Hash *pH,
b9930 20 20 20 20 2f 2a 20 54 68 65 20 70 48 20 63 6f /* The pH co
b9940 6e 74 61 69 6e 69 6e 67 20 22 65 6c 65 6d 22 20 ntaining "elem"
b9950 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d 2a 20 65 */. HashElem* e
b9960 6c 65 6d 2c 20 20 20 2f 2a 20 54 68 65 20 65 6c lem, /* The el
b9970 65 6d 65 6e 74 20 74 6f 20 62 65 20 72 65 6d 6f ement to be remo
b9980 76 65 64 20 66 72 6f 6d 20 74 68 65 20 70 48 20 ved from the pH
b9990 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e */. unsigned in
b99a0 74 20 68 20 20 20 20 2f 2a 20 48 61 73 68 20 76 t h /* Hash v
b99b0 61 6c 75 65 20 66 6f 72 20 74 68 65 20 65 6c 65 alue for the ele
b99c0 6d 65 6e 74 20 2a 2f 0a 29 7b 0a 20 20 73 74 72 ment */.){. str
b99d0 75 63 74 20 5f 68 74 20 2a 70 45 6e 74 72 79 3b uct _ht *pEntry;
b99e0 0a 20 20 69 66 28 20 65 6c 65 6d 2d 3e 70 72 65 . if( elem->pre
b99f0 76 20 29 7b 0a 20 20 20 20 65 6c 65 6d 2d 3e 70 v ){. elem->p
b9a00 72 65 76 2d 3e 6e 65 78 74 20 3d 20 65 6c 65 6d rev->next = elem
b9a10 2d 3e 6e 65 78 74 3b 20 0a 20 20 7d 65 6c 73 65 ->next; . }else
b9a20 7b 0a 20 20 20 20 70 48 2d 3e 66 69 72 73 74 20 {. pH->first
b9a30 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20 = elem->next;.
b9a40 7d 0a 20 20 69 66 28 20 65 6c 65 6d 2d 3e 6e 65 }. if( elem->ne
b9a50 78 74 20 29 7b 0a 20 20 20 20 65 6c 65 6d 2d 3e xt ){. elem->
b9a60 6e 65 78 74 2d 3e 70 72 65 76 20 3d 20 65 6c 65 next->prev = ele
b9a70 6d 2d 3e 70 72 65 76 3b 0a 20 20 7d 0a 20 20 69 m->prev;. }. i
b9a80 66 28 20 70 48 2d 3e 68 74 20 29 7b 0a 20 20 20 f( pH->ht ){.
b9a90 20 70 45 6e 74 72 79 20 3d 20 26 70 48 2d 3e 68 pEntry = &pH->h
b9aa0 74 5b 68 5d 3b 0a 20 20 20 20 69 66 28 20 70 45 t[h];. if( pE
b9ab0 6e 74 72 79 2d 3e 63 68 61 69 6e 3d 3d 65 6c 65 ntry->chain==ele
b9ac0 6d 20 29 7b 0a 20 20 20 20 20 20 70 45 6e 74 72 m ){. pEntr
b9ad0 79 2d 3e 63 68 61 69 6e 20 3d 20 65 6c 65 6d 2d y->chain = elem-
b9ae0 3e 6e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 >next;. }.
b9af0 20 70 45 6e 74 72 79 2d 3e 63 6f 75 6e 74 2d 2d pEntry->count--
b9b00 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 45 ;. assert( pE
b9b10 6e 74 72 79 2d 3e 63 6f 75 6e 74 3e 3d 30 20 29 ntry->count>=0 )
b9b20 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f ;. }. sqlite3_
b9b30 66 72 65 65 28 20 65 6c 65 6d 20 29 3b 0a 20 20 free( elem );.
b9b40 70 48 2d 3e 63 6f 75 6e 74 2d 2d 3b 0a 20 20 69 pH->count--;. i
b9b50 66 28 20 70 48 2d 3e 63 6f 75 6e 74 3c 3d 30 20 f( pH->count<=0
b9b60 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 ){. assert( p
b9b70 48 2d 3e 66 69 72 73 74 3d 3d 30 20 29 3b 0a 20 H->first==0 );.
b9b80 20 20 20 61 73 73 65 72 74 28 20 70 48 2d 3e 63 assert( pH->c
b9b90 6f 75 6e 74 3d 3d 30 20 29 3b 0a 20 20 20 20 73 ount==0 );. s
b9ba0 71 6c 69 74 65 33 48 61 73 68 43 6c 65 61 72 28 qlite3HashClear(
b9bb0 70 48 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 41 pH);. }.}../* A
b9bc0 74 74 65 6d 70 74 20 74 6f 20 6c 6f 63 61 74 65 ttempt to locate
b9bd0 20 61 6e 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 an element of t
b9be0 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 70 48 he hash table pH
b9bf0 20 77 69 74 68 20 61 20 6b 65 79 0a 2a 2a 20 74 with a key.** t
b9c00 68 61 74 20 6d 61 74 63 68 65 73 20 70 4b 65 79 hat matches pKey
b9c10 2c 6e 4b 65 79 2e 20 20 52 65 74 75 72 6e 20 74 ,nKey. Return t
b9c20 68 65 20 64 61 74 61 20 66 6f 72 20 74 68 69 73 he data for this
b9c30 20 65 6c 65 6d 65 6e 74 20 69 66 20 69 74 20 69 element if it i
b9c40 73 0a 2a 2a 20 66 6f 75 6e 64 2c 20 6f 72 20 4e s.** found, or N
b9c50 55 4c 4c 20 69 66 20 74 68 65 72 65 20 69 73 20 ULL if there is
b9c60 6e 6f 20 6d 61 74 63 68 2e 0a 2a 2f 0a 53 51 4c no match..*/.SQL
b9c70 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
b9c80 20 2a 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e *sqlite3HashFin
b9c90 64 28 63 6f 6e 73 74 20 48 61 73 68 20 2a 70 48 d(const Hash *pH
b9ca0 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 4b , const char *pK
b9cb0 65 79 2c 20 69 6e 74 20 6e 4b 65 79 29 7b 0a 20 ey, int nKey){.
b9cc0 20 48 61 73 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b HashElem *elem;
b9cd0 20 20 20 20 2f 2a 20 54 68 65 20 65 6c 65 6d 65 /* The eleme
b9ce0 6e 74 20 74 68 61 74 20 6d 61 74 63 68 65 73 20 nt that matches
b9cf0 6b 65 79 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 key */. unsigne
b9d00 64 20 69 6e 74 20 68 3b 20 20 20 20 2f 2a 20 41 d int h; /* A
b9d10 20 68 61 73 68 20 6f 6e 20 6b 65 79 20 2a 2f 0a hash on key */.
b9d20 0a 20 20 61 73 73 65 72 74 28 20 70 48 21 3d 30 . assert( pH!=0
b9d30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4b );. assert( pK
b9d40 65 79 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 ey!=0 );. asser
b9d50 74 28 20 6e 4b 65 79 3e 3d 30 20 29 3b 0a 20 20 t( nKey>=0 );.
b9d60 69 66 28 20 70 48 2d 3e 68 74 20 29 7b 0a 20 20 if( pH->ht ){.
b9d70 20 20 68 20 3d 20 73 74 72 48 61 73 68 28 70 4b h = strHash(pK
b9d80 65 79 2c 20 6e 4b 65 79 29 20 25 20 70 48 2d 3e ey, nKey) % pH->
b9d90 68 74 73 69 7a 65 3b 0a 20 20 7d 65 6c 73 65 7b htsize;. }else{
b9da0 0a 20 20 20 20 68 20 3d 20 30 3b 0a 20 20 7d 0a . h = 0;. }.
b9db0 20 20 65 6c 65 6d 20 3d 20 66 69 6e 64 45 6c 65 elem = findEle
b9dc0 6d 65 6e 74 47 69 76 65 6e 48 61 73 68 28 70 48 mentGivenHash(pH
b9dd0 2c 20 70 4b 65 79 2c 20 6e 4b 65 79 2c 20 68 29 , pKey, nKey, h)
b9de0 3b 0a 20 20 72 65 74 75 72 6e 20 65 6c 65 6d 20 ;. return elem
b9df0 3f 20 65 6c 65 6d 2d 3e 64 61 74 61 20 3a 20 30 ? elem->data : 0
b9e00 3b 0a 7d 0a 0a 2f 2a 20 49 6e 73 65 72 74 20 61 ;.}../* Insert a
b9e10 6e 20 65 6c 65 6d 65 6e 74 20 69 6e 74 6f 20 74 n element into t
b9e20 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 70 48 he hash table pH
b9e30 2e 20 20 54 68 65 20 6b 65 79 20 69 73 20 70 4b . The key is pK
b9e40 65 79 2c 6e 4b 65 79 0a 2a 2a 20 61 6e 64 20 74 ey,nKey.** and t
b9e50 68 65 20 64 61 74 61 20 69 73 20 22 64 61 74 61 he data is "data
b9e60 22 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 20 65 "..**.** If no e
b9e70 6c 65 6d 65 6e 74 20 65 78 69 73 74 73 20 77 69 lement exists wi
b9e80 74 68 20 61 20 6d 61 74 63 68 69 6e 67 20 6b 65 th a matching ke
b9e90 79 2c 20 74 68 65 6e 20 61 20 6e 65 77 0a 2a 2a y, then a new.**
b9ea0 20 65 6c 65 6d 65 6e 74 20 69 73 20 63 72 65 61 element is crea
b9eb0 74 65 64 20 61 6e 64 20 4e 55 4c 4c 20 69 73 20 ted and NULL is
b9ec0 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 returned..**.**
b9ed0 49 66 20 61 6e 6f 74 68 65 72 20 65 6c 65 6d 65 If another eleme
b9ee0 6e 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 nt already exist
b9ef0 73 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 s with the same
b9f00 6b 65 79 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a key, then the.**
b9f10 20 6e 65 77 20 64 61 74 61 20 72 65 70 6c 61 63 new data replac
b9f20 65 73 20 74 68 65 20 6f 6c 64 20 64 61 74 61 20 es the old data
b9f30 61 6e 64 20 74 68 65 20 6f 6c 64 20 64 61 74 61 and the old data
b9f40 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a is returned..**
b9f50 20 54 68 65 20 6b 65 79 20 69 73 20 6e 6f 74 20 The key is not
b9f60 63 6f 70 69 65 64 20 69 6e 20 74 68 69 73 20 69 copied in this i
b9f70 6e 73 74 61 6e 63 65 2e 20 20 49 66 20 61 20 6d nstance. If a m
b9f80 61 6c 6c 6f 63 20 66 61 69 6c 73 2c 20 74 68 65 alloc fails, the
b9f90 6e 0a 2a 2a 20 74 68 65 20 6e 65 77 20 64 61 74 n.** the new dat
b9fa0 61 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e a is returned an
b9fb0 64 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 d the hash table
b9fc0 20 69 73 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a is unchanged..*
b9fd0 2a 0a 2a 2a 20 49 66 20 74 68 65 20 22 64 61 74 *.** If the "dat
b9fe0 61 22 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 a" parameter to
b9ff0 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 this function is
ba000 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65 0a NULL, then the.
ba010 2a 2a 20 65 6c 65 6d 65 6e 74 20 63 6f 72 72 65 ** element corre
ba020 73 70 6f 6e 64 69 6e 67 20 74 6f 20 22 6b 65 79 sponding to "key
ba030 22 20 69 73 20 72 65 6d 6f 76 65 64 20 66 72 6f " is removed fro
ba040 6d 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 m the hash table
ba050 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
ba060 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 ATE void *sqlite
ba070 33 48 61 73 68 49 6e 73 65 72 74 28 48 61 73 68 3HashInsert(Hash
ba080 20 2a 70 48 2c 20 63 6f 6e 73 74 20 63 68 61 72 *pH, const char
ba090 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79 *pKey, int nKey
ba0a0 2c 20 76 6f 69 64 20 2a 64 61 74 61 29 7b 0a 20 , void *data){.
ba0b0 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68 3b unsigned int h;
ba0c0 20 20 20 20 20 20 20 2f 2a 20 74 68 65 20 68 61 /* the ha
ba0d0 73 68 20 6f 66 20 74 68 65 20 6b 65 79 20 6d 6f sh of the key mo
ba0e0 64 75 6c 6f 20 68 61 73 68 20 74 61 62 6c 65 20 dulo hash table
ba0f0 73 69 7a 65 20 2a 2f 0a 20 20 48 61 73 68 45 6c size */. HashEl
ba100 65 6d 20 2a 65 6c 65 6d 3b 20 20 20 20 20 20 20 em *elem;
ba110 2f 2a 20 55 73 65 64 20 74 6f 20 6c 6f 6f 70 20 /* Used to loop
ba120 74 68 72 75 20 74 68 65 20 65 6c 65 6d 65 6e 74 thru the element
ba130 20 6c 69 73 74 20 2a 2f 0a 20 20 48 61 73 68 45 list */. HashE
ba140 6c 65 6d 20 2a 6e 65 77 5f 65 6c 65 6d 3b 20 20 lem *new_elem;
ba150 20 2f 2a 20 4e 65 77 20 65 6c 65 6d 65 6e 74 20 /* New element
ba160 61 64 64 65 64 20 74 6f 20 74 68 65 20 70 48 20 added to the pH
ba170 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 48 */.. assert( pH
ba180 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 !=0 );. assert(
ba190 20 70 4b 65 79 21 3d 30 20 29 3b 0a 20 20 61 73 pKey!=0 );. as
ba1a0 73 65 72 74 28 20 6e 4b 65 79 3e 3d 30 20 29 3b sert( nKey>=0 );
ba1b0 0a 20 20 69 66 28 20 70 48 2d 3e 68 74 73 69 7a . if( pH->htsiz
ba1c0 65 20 29 7b 0a 20 20 20 20 68 20 3d 20 73 74 72 e ){. h = str
ba1d0 48 61 73 68 28 70 4b 65 79 2c 20 6e 4b 65 79 29 Hash(pKey, nKey)
ba1e0 20 25 20 70 48 2d 3e 68 74 73 69 7a 65 3b 0a 20 % pH->htsize;.
ba1f0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 68 20 3d 20 }else{. h =
ba200 30 3b 0a 20 20 7d 0a 20 20 65 6c 65 6d 20 3d 20 0;. }. elem =
ba210 66 69 6e 64 45 6c 65 6d 65 6e 74 47 69 76 65 6e findElementGiven
ba220 48 61 73 68 28 70 48 2c 70 4b 65 79 2c 6e 4b 65 Hash(pH,pKey,nKe
ba230 79 2c 68 29 3b 0a 20 20 69 66 28 20 65 6c 65 6d y,h);. if( elem
ba240 20 29 7b 0a 20 20 20 20 76 6f 69 64 20 2a 6f 6c ){. void *ol
ba250 64 5f 64 61 74 61 20 3d 20 65 6c 65 6d 2d 3e 64 d_data = elem->d
ba260 61 74 61 3b 0a 20 20 20 20 69 66 28 20 64 61 74 ata;. if( dat
ba270 61 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 a==0 ){. re
ba280 6d 6f 76 65 45 6c 65 6d 65 6e 74 47 69 76 65 6e moveElementGiven
ba290 48 61 73 68 28 70 48 2c 65 6c 65 6d 2c 68 29 3b Hash(pH,elem,h);
ba2a0 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
ba2b0 20 20 65 6c 65 6d 2d 3e 64 61 74 61 20 3d 20 64 elem->data = d
ba2c0 61 74 61 3b 0a 20 20 20 20 20 20 65 6c 65 6d 2d ata;. elem-
ba2d0 3e 70 4b 65 79 20 3d 20 70 4b 65 79 3b 0a 20 20 >pKey = pKey;.
ba2e0 20 20 20 20 61 73 73 65 72 74 28 6e 4b 65 79 3d assert(nKey=
ba2f0 3d 65 6c 65 6d 2d 3e 6e 4b 65 79 29 3b 0a 20 20 =elem->nKey);.
ba300 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 6f }. return o
ba310 6c 64 5f 64 61 74 61 3b 0a 20 20 7d 0a 20 20 69 ld_data;. }. i
ba320 66 28 20 64 61 74 61 3d 3d 30 20 29 20 72 65 74 f( data==0 ) ret
ba330 75 72 6e 20 30 3b 0a 20 20 6e 65 77 5f 65 6c 65 urn 0;. new_ele
ba340 6d 20 3d 20 28 48 61 73 68 45 6c 65 6d 2a 29 73 m = (HashElem*)s
ba350 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20 73 69 qlite3Malloc( si
ba360 7a 65 6f 66 28 48 61 73 68 45 6c 65 6d 29 20 29 zeof(HashElem) )
ba370 3b 0a 20 20 69 66 28 20 6e 65 77 5f 65 6c 65 6d ;. if( new_elem
ba380 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 64 61 74 ==0 ) return dat
ba390 61 3b 0a 20 20 6e 65 77 5f 65 6c 65 6d 2d 3e 70 a;. new_elem->p
ba3a0 4b 65 79 20 3d 20 70 4b 65 79 3b 0a 20 20 6e 65 Key = pKey;. ne
ba3b0 77 5f 65 6c 65 6d 2d 3e 6e 4b 65 79 20 3d 20 6e w_elem->nKey = n
ba3c0 4b 65 79 3b 0a 20 20 6e 65 77 5f 65 6c 65 6d 2d Key;. new_elem-
ba3d0 3e 64 61 74 61 20 3d 20 64 61 74 61 3b 0a 20 20 >data = data;.
ba3e0 70 48 2d 3e 63 6f 75 6e 74 2b 2b 3b 0a 20 20 69 pH->count++;. i
ba3f0 66 28 20 70 48 2d 3e 63 6f 75 6e 74 3e 3d 31 30 f( pH->count>=10
ba400 20 26 26 20 70 48 2d 3e 63 6f 75 6e 74 20 3e 20 && pH->count >
ba410 32 2a 70 48 2d 3e 68 74 73 69 7a 65 20 29 7b 0a 2*pH->htsize ){.
ba420 20 20 20 20 69 66 28 20 72 65 68 61 73 68 28 70 if( rehash(p
ba430 48 2c 20 70 48 2d 3e 63 6f 75 6e 74 2a 32 29 20 H, pH->count*2)
ba440 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 ){. assert(
ba450 20 70 48 2d 3e 68 74 73 69 7a 65 3e 30 20 29 3b pH->htsize>0 );
ba460 0a 20 20 20 20 20 20 68 20 3d 20 73 74 72 48 61 . h = strHa
ba470 73 68 28 70 4b 65 79 2c 20 6e 4b 65 79 29 20 25 sh(pKey, nKey) %
ba480 20 70 48 2d 3e 68 74 73 69 7a 65 3b 0a 20 20 20 pH->htsize;.
ba490 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 48 2d }. }. if( pH-
ba4a0 3e 68 74 20 29 7b 0a 20 20 20 20 69 6e 73 65 72 >ht ){. inser
ba4b0 74 45 6c 65 6d 65 6e 74 28 70 48 2c 20 26 70 48 tElement(pH, &pH
ba4c0 2d 3e 68 74 5b 68 5d 2c 20 6e 65 77 5f 65 6c 65 ->ht[h], new_ele
ba4d0 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 m);. }else{.
ba4e0 20 69 6e 73 65 72 74 45 6c 65 6d 65 6e 74 28 70 insertElement(p
ba4f0 48 2c 20 30 2c 20 6e 65 77 5f 65 6c 65 6d 29 3b H, 0, new_elem);
ba500 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b . }. return 0;
ba510 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .}../***********
ba520 2a 2a 2a 20 45 6e 64 20 6f 66 20 68 61 73 68 2e *** End of hash.
ba530 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
ba540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ba550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ba560 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
ba570 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f *** Begin file o
ba580 70 63 6f 64 65 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a pcodes.c *******
ba590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ba5a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ba5b0 2a 2a 2f 0a 2f 2a 20 41 75 74 6f 6d 61 74 69 63 **/./* Automatic
ba5c0 61 6c 6c 79 20 67 65 6e 65 72 61 74 65 64 2e 20 ally generated.
ba5d0 20 44 6f 20 6e 6f 74 20 65 64 69 74 20 2a 2f 0a Do not edit */.
ba5e0 2f 2a 20 53 65 65 20 74 68 65 20 6d 6b 6f 70 63 /* See the mkopc
ba5f0 6f 64 65 63 2e 61 77 6b 20 73 63 72 69 70 74 20 odec.awk script
ba600 66 6f 72 20 64 65 74 61 69 6c 73 2e 20 2a 2f 0a for details. */.
ba610 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c #if !defined(SQL
ba620 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e ITE_OMIT_EXPLAIN
ba630 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 4e 44 ) || !defined(ND
ba640 45 42 55 47 29 20 7c 7c 20 64 65 66 69 6e 65 64 EBUG) || defined
ba650 28 56 44 42 45 5f 50 52 4f 46 49 4c 45 29 20 7c (VDBE_PROFILE) |
ba660 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 | defined(SQLITE
ba670 5f 44 45 42 55 47 29 0a 53 51 4c 49 54 45 5f 50 _DEBUG).SQLITE_P
ba680 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 RIVATE const cha
ba690 72 20 2a 73 71 6c 69 74 65 33 4f 70 63 6f 64 65 r *sqlite3Opcode
ba6a0 4e 61 6d 65 28 69 6e 74 20 69 29 7b 0a 20 73 74 Name(int i){. st
ba6b0 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 atic const char
ba6c0 2a 63 6f 6e 73 74 20 61 7a 4e 61 6d 65 5b 5d 20 *const azName[]
ba6d0 3d 20 7b 20 22 3f 22 2c 0a 20 20 20 20 20 2f 2a = { "?",. /*
ba6e0 20 20 20 31 20 2a 2f 20 22 56 4e 65 78 74 22 2c 1 */ "VNext",
ba6f0 0a 20 20 20 20 20 2f 2a 20 20 20 32 20 2a 2f 20 . /* 2 */
ba700 22 41 66 66 69 6e 69 74 79 22 2c 0a 20 20 20 20 "Affinity",.
ba710 20 2f 2a 20 20 20 33 20 2a 2f 20 22 43 6f 6c 75 /* 3 */ "Colu
ba720 6d 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20 34 mn",. /* 4
ba730 20 2a 2f 20 22 53 65 74 43 6f 6f 6b 69 65 22 2c */ "SetCookie",
ba740 0a 20 20 20 20 20 2f 2a 20 20 20 35 20 2a 2f 20 . /* 5 */
ba750 22 53 65 65 6b 22 2c 0a 20 20 20 20 20 2f 2a 20 "Seek",. /*
ba760 20 20 36 20 2a 2f 20 22 53 65 71 75 65 6e 63 65 6 */ "Sequence
ba770 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20 37 20 2a ",. /* 7 *
ba780 2f 20 22 53 61 76 65 70 6f 69 6e 74 22 2c 0a 20 / "Savepoint",.
ba790 20 20 20 20 2f 2a 20 20 20 38 20 2a 2f 20 22 52 /* 8 */ "R
ba7a0 6f 77 4b 65 79 22 2c 0a 20 20 20 20 20 2f 2a 20 owKey",. /*
ba7b0 20 20 39 20 2a 2f 20 22 53 43 6f 70 79 22 2c 0a 9 */ "SCopy",.
ba7c0 20 20 20 20 20 2f 2a 20 20 31 30 20 2a 2f 20 22 /* 10 */ "
ba7d0 4f 70 65 6e 57 72 69 74 65 22 2c 0a 20 20 20 20 OpenWrite",.
ba7e0 20 2f 2a 20 20 31 31 20 2a 2f 20 22 49 66 22 2c /* 11 */ "If",
ba7f0 0a 20 20 20 20 20 2f 2a 20 20 31 32 20 2a 2f 20 . /* 12 */
ba800 22 43 6f 6c 6c 53 65 71 22 2c 0a 20 20 20 20 20 "CollSeq",.
ba810 2f 2a 20 20 31 33 20 2a 2f 20 22 4f 70 65 6e 52 /* 13 */ "OpenR
ba820 65 61 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31 ead",. /* 1
ba830 34 20 2a 2f 20 22 45 78 70 69 72 65 22 2c 0a 20 4 */ "Expire",.
ba840 20 20 20 20 2f 2a 20 20 31 35 20 2a 2f 20 22 41 /* 15 */ "A
ba850 75 74 6f 43 6f 6d 6d 69 74 22 2c 0a 20 20 20 20 utoCommit",.
ba860 20 2f 2a 20 20 31 36 20 2a 2f 20 22 50 61 67 65 /* 16 */ "Page
ba870 63 6f 75 6e 74 22 2c 0a 20 20 20 20 20 2f 2a 20 count",. /*
ba880 20 31 37 20 2a 2f 20 22 49 6e 74 65 67 72 69 74 17 */ "Integrit
ba890 79 43 6b 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31 yCk",. /* 1
ba8a0 38 20 2a 2f 20 22 53 6f 72 74 22 2c 0a 20 20 20 8 */ "Sort",.
ba8b0 20 20 2f 2a 20 20 31 39 20 2a 2f 20 22 4e 6f 74 /* 19 */ "Not
ba8c0 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 30 20 2a ",. /* 20 *
ba8d0 2f 20 22 43 6f 70 79 22 2c 0a 20 20 20 20 20 2f / "Copy",. /
ba8e0 2a 20 20 32 31 20 2a 2f 20 22 54 72 61 63 65 22 * 21 */ "Trace"
ba8f0 2c 0a 20 20 20 20 20 2f 2a 20 20 32 32 20 2a 2f ,. /* 22 */
ba900 20 22 46 75 6e 63 74 69 6f 6e 22 2c 0a 20 20 20 "Function",.
ba910 20 20 2f 2a 20 20 32 33 20 2a 2f 20 22 49 66 4e /* 23 */ "IfN
ba920 65 67 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 34 eg",. /* 24
ba930 20 2a 2f 20 22 4e 6f 6f 70 22 2c 0a 20 20 20 20 */ "Noop",.
ba940 20 2f 2a 20 20 32 35 20 2a 2f 20 22 50 72 6f 67 /* 25 */ "Prog
ba950 72 61 6d 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 ram",. /* 2
ba960 36 20 2a 2f 20 22 52 65 74 75 72 6e 22 2c 0a 20 6 */ "Return",.
ba970 20 20 20 20 2f 2a 20 20 32 37 20 2a 2f 20 22 4e /* 27 */ "N
ba980 65 77 52 6f 77 69 64 22 2c 0a 20 20 20 20 20 2f ewRowid",. /
ba990 2a 20 20 32 38 20 2a 2f 20 22 46 6b 43 6f 75 6e * 28 */ "FkCoun
ba9a0 74 65 72 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 ter",. /* 2
ba9b0 39 20 2a 2f 20 22 56 61 72 69 61 62 6c 65 22 2c 9 */ "Variable",
ba9c0 0a 20 20 20 20 20 2f 2a 20 20 33 30 20 2a 2f 20 . /* 30 */
ba9d0 22 53 74 72 69 6e 67 22 2c 0a 20 20 20 20 20 2f "String",. /
ba9e0 2a 20 20 33 31 20 2a 2f 20 22 52 65 61 6c 41 66 * 31 */ "RealAf
ba9f0 66 69 6e 69 74 79 22 2c 0a 20 20 20 20 20 2f 2a finity",. /*
baa00 20 20 33 32 20 2a 2f 20 22 56 52 65 6e 61 6d 65 32 */ "VRename
baa10 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 33 20 2a ",. /* 33 *
baa20 2f 20 22 50 61 72 73 65 53 63 68 65 6d 61 22 2c / "ParseSchema",
baa30 0a 20 20 20 20 20 2f 2a 20 20 33 34 20 2a 2f 20 . /* 34 */
baa40 22 56 4f 70 65 6e 22 2c 0a 20 20 20 20 20 2f 2a "VOpen",. /*
baa50 20 20 33 35 20 2a 2f 20 22 43 6c 6f 73 65 22 2c 35 */ "Close",
baa60 0a 20 20 20 20 20 2f 2a 20 20 33 36 20 2a 2f 20 . /* 36 */
baa70 22 43 72 65 61 74 65 49 6e 64 65 78 22 2c 0a 20 "CreateIndex",.
baa80 20 20 20 20 2f 2a 20 20 33 37 20 2a 2f 20 22 49 /* 37 */ "I
baa90 73 55 6e 69 71 75 65 22 2c 0a 20 20 20 20 20 2f sUnique",. /
baaa0 2a 20 20 33 38 20 2a 2f 20 22 4e 6f 74 46 6f 75 * 38 */ "NotFou
baab0 6e 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 39 nd",. /* 39
baac0 20 2a 2f 20 22 49 6e 74 36 34 22 2c 0a 20 20 20 */ "Int64",.
baad0 20 20 2f 2a 20 20 34 30 20 2a 2f 20 22 4d 75 73 /* 40 */ "Mus
baae0 74 42 65 49 6e 74 22 2c 0a 20 20 20 20 20 2f 2a tBeInt",. /*
baaf0 20 20 34 31 20 2a 2f 20 22 48 61 6c 74 22 2c 0a 41 */ "Halt",.
bab00 20 20 20 20 20 2f 2a 20 20 34 32 20 2a 2f 20 22 /* 42 */ "
bab10 52 6f 77 69 64 22 2c 0a 20 20 20 20 20 2f 2a 20 Rowid",. /*
bab20 20 34 33 20 2a 2f 20 22 49 64 78 4c 54 22 2c 0a 43 */ "IdxLT",.
bab30 20 20 20 20 20 2f 2a 20 20 34 34 20 2a 2f 20 22 /* 44 */ "
bab40 41 64 64 49 6d 6d 22 2c 0a 20 20 20 20 20 2f 2a AddImm",. /*
bab50 20 20 34 35 20 2a 2f 20 22 52 6f 77 44 61 74 61 45 */ "RowData
bab60 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34 36 20 2a ",. /* 46 *
bab70 2f 20 22 4d 65 6d 4d 61 78 22 2c 0a 20 20 20 20 / "MemMax",.
bab80 20 2f 2a 20 20 34 37 20 2a 2f 20 22 4e 6f 74 45 /* 47 */ "NotE
bab90 78 69 73 74 73 22 2c 0a 20 20 20 20 20 2f 2a 20 xists",. /*
baba0 20 34 38 20 2a 2f 20 22 47 6f 73 75 62 22 2c 0a 48 */ "Gosub",.
babb0 20 20 20 20 20 2f 2a 20 20 34 39 20 2a 2f 20 22 /* 49 */ "
babc0 49 6e 74 65 67 65 72 22 2c 0a 20 20 20 20 20 2f Integer",. /
babd0 2a 20 20 35 30 20 2a 2f 20 22 50 72 65 76 22 2c * 50 */ "Prev",
babe0 0a 20 20 20 20 20 2f 2a 20 20 35 31 20 2a 2f 20 . /* 51 */
babf0 22 52 6f 77 53 65 74 52 65 61 64 22 2c 0a 20 20 "RowSetRead",.
bac00 20 20 20 2f 2a 20 20 35 32 20 2a 2f 20 22 52 6f /* 52 */ "Ro
bac10 77 53 65 74 41 64 64 22 2c 0a 20 20 20 20 20 2f wSetAdd",. /
bac20 2a 20 20 35 33 20 2a 2f 20 22 56 43 6f 6c 75 6d * 53 */ "VColum
bac30 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 34 20 n",. /* 54
bac40 2a 2f 20 22 43 72 65 61 74 65 54 61 62 6c 65 22 */ "CreateTable"
bac50 2c 0a 20 20 20 20 20 2f 2a 20 20 35 35 20 2a 2f ,. /* 55 */
bac60 20 22 4c 61 73 74 22 2c 0a 20 20 20 20 20 2f 2a "Last",. /*
bac70 20 20 35 36 20 2a 2f 20 22 53 65 65 6b 4c 65 22 56 */ "SeekLe"
bac80 2c 0a 20 20 20 20 20 2f 2a 20 20 35 37 20 2a 2f ,. /* 57 */
bac90 20 22 49 6e 63 72 56 61 63 75 75 6d 22 2c 0a 20 "IncrVacuum",.
baca0 20 20 20 20 2f 2a 20 20 35 38 20 2a 2f 20 22 49 /* 58 */ "I
bacb0 64 78 52 6f 77 69 64 22 2c 0a 20 20 20 20 20 2f dxRowid",. /
bacc0 2a 20 20 35 39 20 2a 2f 20 22 52 65 73 65 74 43 * 59 */ "ResetC
bacd0 6f 75 6e 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 ount",. /*
bace0 36 30 20 2a 2f 20 22 59 69 65 6c 64 22 2c 0a 20 60 */ "Yield",.
bacf0 20 20 20 20 2f 2a 20 20 36 31 20 2a 2f 20 22 44 /* 61 */ "D
bad00 72 6f 70 54 72 69 67 67 65 72 22 2c 0a 20 20 20 ropTrigger",.
bad10 20 20 2f 2a 20 20 36 32 20 2a 2f 20 22 44 72 6f /* 62 */ "Dro
bad20 70 49 6e 64 65 78 22 2c 0a 20 20 20 20 20 2f 2a pIndex",. /*
bad30 20 20 36 33 20 2a 2f 20 22 50 61 72 61 6d 22 2c 63 */ "Param",
bad40 0a 20 20 20 20 20 2f 2a 20 20 36 34 20 2a 2f 20 . /* 64 */
bad50 22 49 64 78 47 45 22 2c 0a 20 20 20 20 20 2f 2a "IdxGE",. /*
bad60 20 20 36 35 20 2a 2f 20 22 49 64 78 44 65 6c 65 65 */ "IdxDele
bad70 74 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36 36 te",. /* 66
bad80 20 2a 2f 20 22 56 61 63 75 75 6d 22 2c 0a 20 20 */ "Vacuum",.
bad90 20 20 20 2f 2a 20 20 36 37 20 2a 2f 20 22 49 66 /* 67 */ "If
bada0 4e 6f 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36 Not",. /* 6
badb0 38 20 2a 2f 20 22 4f 72 22 2c 0a 20 20 20 20 20 8 */ "Or",.
badc0 2f 2a 20 20 36 39 20 2a 2f 20 22 41 6e 64 22 2c /* 69 */ "And",
badd0 0a 20 20 20 20 20 2f 2a 20 20 37 30 20 2a 2f 20 . /* 70 */
bade0 22 44 72 6f 70 54 61 62 6c 65 22 2c 0a 20 20 20 "DropTable",.
badf0 20 20 2f 2a 20 20 37 31 20 2a 2f 20 22 53 65 65 /* 71 */ "See
bae00 6b 4c 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 kLt",. /* 7
bae10 32 20 2a 2f 20 22 4d 61 6b 65 52 65 63 6f 72 64 2 */ "MakeRecord
bae20 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 33 20 2a ",. /* 73 *
bae30 2f 20 22 49 73 4e 75 6c 6c 22 2c 0a 20 20 20 20 / "IsNull",.
bae40 20 2f 2a 20 20 37 34 20 2a 2f 20 22 4e 6f 74 4e /* 74 */ "NotN
bae50 75 6c 6c 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 ull",. /* 7
bae60 35 20 2a 2f 20 22 4e 65 22 2c 0a 20 20 20 20 20 5 */ "Ne",.
bae70 2f 2a 20 20 37 36 20 2a 2f 20 22 45 71 22 2c 0a /* 76 */ "Eq",.
bae80 20 20 20 20 20 2f 2a 20 20 37 37 20 2a 2f 20 22 /* 77 */ "
bae90 47 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 38 Gt",. /* 78
baea0 20 2a 2f 20 22 4c 65 22 2c 0a 20 20 20 20 20 2f */ "Le",. /
baeb0 2a 20 20 37 39 20 2a 2f 20 22 4c 74 22 2c 0a 20 * 79 */ "Lt",.
baec0 20 20 20 20 2f 2a 20 20 38 30 20 2a 2f 20 22 47 /* 80 */ "G
baed0 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38 31 20 e",. /* 81
baee0 2a 2f 20 22 52 65 73 75 6c 74 52 6f 77 22 2c 0a */ "ResultRow",.
baef0 20 20 20 20 20 2f 2a 20 20 38 32 20 2a 2f 20 22 /* 82 */ "
baf00 42 69 74 41 6e 64 22 2c 0a 20 20 20 20 20 2f 2a BitAnd",. /*
baf10 20 20 38 33 20 2a 2f 20 22 42 69 74 4f 72 22 2c 83 */ "BitOr",
baf20 0a 20 20 20 20 20 2f 2a 20 20 38 34 20 2a 2f 20 . /* 84 */
baf30 22 53 68 69 66 74 4c 65 66 74 22 2c 0a 20 20 20 "ShiftLeft",.
baf40 20 20 2f 2a 20 20 38 35 20 2a 2f 20 22 53 68 69 /* 85 */ "Shi
baf50 66 74 52 69 67 68 74 22 2c 0a 20 20 20 20 20 2f ftRight",. /
baf60 2a 20 20 38 36 20 2a 2f 20 22 41 64 64 22 2c 0a * 86 */ "Add",.
baf70 20 20 20 20 20 2f 2a 20 20 38 37 20 2a 2f 20 22 /* 87 */ "
baf80 53 75 62 74 72 61 63 74 22 2c 0a 20 20 20 20 20 Subtract",.
baf90 2f 2a 20 20 38 38 20 2a 2f 20 22 4d 75 6c 74 69 /* 88 */ "Multi
bafa0 70 6c 79 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38 ply",. /* 8
bafb0 39 20 2a 2f 20 22 44 69 76 69 64 65 22 2c 0a 20 9 */ "Divide",.
bafc0 20 20 20 20 2f 2a 20 20 39 30 20 2a 2f 20 22 52 /* 90 */ "R
bafd0 65 6d 61 69 6e 64 65 72 22 2c 0a 20 20 20 20 20 emainder",.
bafe0 2f 2a 20 20 39 31 20 2a 2f 20 22 43 6f 6e 63 61 /* 91 */ "Conca
baff0 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 32 20 t",. /* 92
bb000 2a 2f 20 22 44 65 6c 65 74 65 22 2c 0a 20 20 20 */ "Delete",.
bb010 20 20 2f 2a 20 20 39 33 20 2a 2f 20 22 42 69 74 /* 93 */ "Bit
bb020 4e 6f 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 Not",. /* 9
bb030 34 20 2a 2f 20 22 53 74 72 69 6e 67 38 22 2c 0a 4 */ "String8",.
bb040 20 20 20 20 20 2f 2a 20 20 39 35 20 2a 2f 20 22 /* 95 */ "
bb050 41 67 67 46 69 6e 61 6c 22 2c 0a 20 20 20 20 20 AggFinal",.
bb060 2f 2a 20 20 39 36 20 2a 2f 20 22 43 6f 6d 70 61 /* 96 */ "Compa
bb070 72 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 37 re",. /* 97
bb080 20 2a 2f 20 22 47 6f 74 6f 22 2c 0a 20 20 20 20 */ "Goto",.
bb090 20 2f 2a 20 20 39 38 20 2a 2f 20 22 54 61 62 6c /* 98 */ "Tabl
bb0a0 65 4c 6f 63 6b 22 2c 0a 20 20 20 20 20 2f 2a 20 eLock",. /*
bb0b0 20 39 39 20 2a 2f 20 22 43 6c 65 61 72 22 2c 0a 99 */ "Clear",.
bb0c0 20 20 20 20 20 2f 2a 20 31 30 30 20 2a 2f 20 22 /* 100 */ "
bb0d0 56 65 72 69 66 79 43 6f 6f 6b 69 65 22 2c 0a 20 VerifyCookie",.
bb0e0 20 20 20 20 2f 2a 20 31 30 31 20 2a 2f 20 22 41 /* 101 */ "A
bb0f0 67 67 53 74 65 70 22 2c 0a 20 20 20 20 20 2f 2a ggStep",. /*
bb100 20 31 30 32 20 2a 2f 20 22 54 72 61 6e 73 61 63 102 */ "Transac
bb110 74 69 6f 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 31 tion",. /* 1
bb120 30 33 20 2a 2f 20 22 56 46 69 6c 74 65 72 22 2c 03 */ "VFilter",
bb130 0a 20 20 20 20 20 2f 2a 20 31 30 34 20 2a 2f 20 . /* 104 */
bb140 22 56 44 65 73 74 72 6f 79 22 2c 0a 20 20 20 20 "VDestroy",.
bb150 20 2f 2a 20 31 30 35 20 2a 2f 20 22 4e 65 78 74 /* 105 */ "Next
bb160 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30 36 20 2a ",. /* 106 *
bb170 2f 20 22 43 6f 75 6e 74 22 2c 0a 20 20 20 20 20 / "Count",.
bb180 2f 2a 20 31 30 37 20 2a 2f 20 22 49 64 78 49 6e /* 107 */ "IdxIn
bb190 73 65 72 74 22 2c 0a 20 20 20 20 20 2f 2a 20 31 sert",. /* 1
bb1a0 30 38 20 2a 2f 20 22 46 6b 49 66 5a 65 72 6f 22 08 */ "FkIfZero"
bb1b0 2c 0a 20 20 20 20 20 2f 2a 20 31 30 39 20 2a 2f ,. /* 109 */
bb1c0 20 22 53 65 65 6b 47 65 22 2c 0a 20 20 20 20 20 "SeekGe",.
bb1d0 2f 2a 20 31 31 30 20 2a 2f 20 22 49 6e 73 65 72 /* 110 */ "Inser
bb1e0 74 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 31 20 t",. /* 111
bb1f0 2a 2f 20 22 44 65 73 74 72 6f 79 22 2c 0a 20 20 */ "Destroy",.
bb200 20 20 20 2f 2a 20 31 31 32 20 2a 2f 20 22 52 65 /* 112 */ "Re
bb210 61 64 43 6f 6f 6b 69 65 22 2c 0a 20 20 20 20 20 adCookie",.
bb220 2f 2a 20 31 31 33 20 2a 2f 20 22 52 6f 77 53 65 /* 113 */ "RowSe
bb230 74 54 65 73 74 22 2c 0a 20 20 20 20 20 2f 2a 20 tTest",. /*
bb240 31 31 34 20 2a 2f 20 22 4c 6f 61 64 41 6e 61 6c 114 */ "LoadAnal
bb250 79 73 69 73 22 2c 0a 20 20 20 20 20 2f 2a 20 31 ysis",. /* 1
bb260 31 35 20 2a 2f 20 22 45 78 70 6c 61 69 6e 22 2c 15 */ "Explain",
bb270 0a 20 20 20 20 20 2f 2a 20 31 31 36 20 2a 2f 20 . /* 116 */
bb280 22 48 61 6c 74 49 66 4e 75 6c 6c 22 2c 0a 20 20 "HaltIfNull",.
bb290 20 20 20 2f 2a 20 31 31 37 20 2a 2f 20 22 4f 70 /* 117 */ "Op
bb2a0 65 6e 50 73 65 75 64 6f 22 2c 0a 20 20 20 20 20 enPseudo",.
bb2b0 2f 2a 20 31 31 38 20 2a 2f 20 22 4f 70 65 6e 45 /* 118 */ "OpenE
bb2c0 70 68 65 6d 65 72 61 6c 22 2c 0a 20 20 20 20 20 phemeral",.
bb2d0 2f 2a 20 31 31 39 20 2a 2f 20 22 4e 75 6c 6c 22 /* 119 */ "Null"
bb2e0 2c 0a 20 20 20 20 20 2f 2a 20 31 32 30 20 2a 2f ,. /* 120 */
bb2f0 20 22 4d 6f 76 65 22 2c 0a 20 20 20 20 20 2f 2a "Move",. /*
bb300 20 31 32 31 20 2a 2f 20 22 42 6c 6f 62 22 2c 0a 121 */ "Blob",.
bb310 20 20 20 20 20 2f 2a 20 31 32 32 20 2a 2f 20 22 /* 122 */ "
bb320 52 65 77 69 6e 64 22 2c 0a 20 20 20 20 20 2f 2a Rewind",. /*
bb330 20 31 32 33 20 2a 2f 20 22 53 65 65 6b 47 74 22 123 */ "SeekGt"
bb340 2c 0a 20 20 20 20 20 2f 2a 20 31 32 34 20 2a 2f ,. /* 124 */
bb350 20 22 56 42 65 67 69 6e 22 2c 0a 20 20 20 20 20 "VBegin",.
bb360 2f 2a 20 31 32 35 20 2a 2f 20 22 56 55 70 64 61 /* 125 */ "VUpda
bb370 74 65 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32 36 te",. /* 126
bb380 20 2a 2f 20 22 49 66 5a 65 72 6f 22 2c 0a 20 20 */ "IfZero",.
bb390 20 20 20 2f 2a 20 31 32 37 20 2a 2f 20 22 56 43 /* 127 */ "VC
bb3a0 72 65 61 74 65 22 2c 0a 20 20 20 20 20 2f 2a 20 reate",. /*
bb3b0 31 32 38 20 2a 2f 20 22 46 6f 75 6e 64 22 2c 0a 128 */ "Found",.
bb3c0 20 20 20 20 20 2f 2a 20 31 32 39 20 2a 2f 20 22 /* 129 */ "
bb3d0 49 66 50 6f 73 22 2c 0a 20 20 20 20 20 2f 2a 20 IfPos",. /*
bb3e0 31 33 30 20 2a 2f 20 22 52 65 61 6c 22 2c 0a 20 130 */ "Real",.
bb3f0 20 20 20 20 2f 2a 20 31 33 31 20 2a 2f 20 22 4e /* 131 */ "N
bb400 75 6c 6c 52 6f 77 22 2c 0a 20 20 20 20 20 2f 2a ullRow",. /*
bb410 20 31 33 32 20 2a 2f 20 22 4a 75 6d 70 22 2c 0a 132 */ "Jump",.
bb420 20 20 20 20 20 2f 2a 20 31 33 33 20 2a 2f 20 22 /* 133 */ "
bb430 50 65 72 6d 75 74 61 74 69 6f 6e 22 2c 0a 20 20 Permutation",.
bb440 20 20 20 2f 2a 20 31 33 34 20 2a 2f 20 22 4e 6f /* 134 */ "No
bb450 74 55 73 65 64 5f 31 33 34 22 2c 0a 20 20 20 20 tUsed_134",.
bb460 20 2f 2a 20 31 33 35 20 2a 2f 20 22 4e 6f 74 55 /* 135 */ "NotU
bb470 73 65 64 5f 31 33 35 22 2c 0a 20 20 20 20 20 2f sed_135",. /
bb480 2a 20 31 33 36 20 2a 2f 20 22 4e 6f 74 55 73 65 * 136 */ "NotUse
bb490 64 5f 31 33 36 22 2c 0a 20 20 20 20 20 2f 2a 20 d_136",. /*
bb4a0 31 33 37 20 2a 2f 20 22 4e 6f 74 55 73 65 64 5f 137 */ "NotUsed_
bb4b0 31 33 37 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33 137",. /* 13
bb4c0 38 20 2a 2f 20 22 4e 6f 74 55 73 65 64 5f 31 33 8 */ "NotUsed_13
bb4d0 38 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33 39 20 8",. /* 139
bb4e0 2a 2f 20 22 4e 6f 74 55 73 65 64 5f 31 33 39 22 */ "NotUsed_139"
bb4f0 2c 0a 20 20 20 20 20 2f 2a 20 31 34 30 20 2a 2f ,. /* 140 */
bb500 20 22 4e 6f 74 55 73 65 64 5f 31 34 30 22 2c 0a "NotUsed_140",.
bb510 20 20 20 20 20 2f 2a 20 31 34 31 20 2a 2f 20 22 /* 141 */ "
bb520 54 6f 54 65 78 74 22 2c 0a 20 20 20 20 20 2f 2a ToText",. /*
bb530 20 31 34 32 20 2a 2f 20 22 54 6f 42 6c 6f 62 22 142 */ "ToBlob"
bb540 2c 0a 20 20 20 20 20 2f 2a 20 31 34 33 20 2a 2f ,. /* 143 */
bb550 20 22 54 6f 4e 75 6d 65 72 69 63 22 2c 0a 20 20 "ToNumeric",.
bb560 20 20 20 2f 2a 20 31 34 34 20 2a 2f 20 22 54 6f /* 144 */ "To
bb570 49 6e 74 22 2c 0a 20 20 20 20 20 2f 2a 20 31 34 Int",. /* 14
bb580 35 20 2a 2f 20 22 54 6f 52 65 61 6c 22 2c 0a 20 5 */ "ToReal",.
bb590 20 7d 3b 0a 20 20 72 65 74 75 72 6e 20 61 7a 4e };. return azN
bb5a0 61 6d 65 5b 69 5d 3b 0a 7d 0a 23 65 6e 64 69 66 ame[i];.}.#endif
bb5b0 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
bb5c0 2a 20 45 6e 64 20 6f 66 20 6f 70 63 6f 64 65 73 * End of opcodes
bb5d0 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
bb5e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bb5f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bb600 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
bb610 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 5f * Begin file os_
bb620 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a os2.c **********
bb630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bb640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bb650 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 36 20 46 65 62 /./*.** 2006 Feb
bb660 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 14.**.** The au
bb670 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 thor disclaims c
bb680 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 opyright to this
bb690 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 source code. I
bb6a0 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 n place of.** a
bb6b0 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 legal notice, he
bb6c0 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 re is a blessing
bb6d0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 :.**.** May y
bb6e0 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e ou do good and n
bb6f0 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d ot evil..** M
bb700 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 ay you find forg
bb710 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 iveness for your
bb720 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 self and forgive
bb730 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d others..** M
bb740 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 ay you share fre
bb750 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e ely, never takin
bb760 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 g more than you
bb770 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a give..**.*******
bb780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bb790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bb7a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bb7b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bb7c0 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 *******.**.** Th
bb7d0 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 is file contains
bb7e0 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 73 70 code that is sp
bb7f0 65 63 69 66 69 63 20 74 6f 20 4f 53 2f 32 2e 0a ecific to OS/2..
bb800 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6f 73 5f 6f 73 **.** $Id: os_os
bb810 32 2e 63 2c 76 20 31 2e 36 33 20 32 30 30 38 2f 2.c,v 1.63 2008/
bb820 31 32 2f 31 30 20 31 39 3a 32 36 3a 32 34 20 64 12/10 19:26:24 d
bb830 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a 23 69 rh Exp $.*/...#i
bb840 66 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 0a f SQLITE_OS_OS2.
bb850 0a 2f 2a 0a 2a 2a 20 41 20 4e 6f 74 65 20 41 62 ./*.** A Note Ab
bb860 6f 75 74 20 4d 65 6d 6f 72 79 20 41 6c 6c 6f 63 out Memory Alloc
bb870 61 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 54 68 69 ation:.**.** Thi
bb880 73 20 64 72 69 76 65 72 20 75 73 65 73 20 6d 61 s driver uses ma
bb890 6c 6c 6f 63 28 29 2f 66 72 65 65 28 29 20 64 69 lloc()/free() di
bb8a0 72 65 63 74 6c 79 20 72 61 74 68 65 72 20 74 68 rectly rather th
bb8b0 61 6e 20 67 6f 69 6e 67 20 74 68 72 6f 75 67 68 an going through
bb8c0 0a 2a 2a 20 74 68 65 20 53 51 4c 69 74 65 2d 77 .** the SQLite-w
bb8d0 72 61 70 70 65 72 73 20 73 71 6c 69 74 65 33 5f rappers sqlite3_
bb8e0 6d 61 6c 6c 6f 63 28 29 2f 73 71 6c 69 74 65 33 malloc()/sqlite3
bb8f0 5f 66 72 65 65 28 29 2e 20 20 54 68 6f 73 65 20 _free(). Those
bb900 77 72 61 70 70 65 72 73 0a 2a 2a 20 61 72 65 20 wrappers.** are
bb910 64 65 73 69 67 6e 65 64 20 66 6f 72 20 75 73 65 designed for use
bb920 20 6f 6e 20 65 6d 62 65 64 64 65 64 20 73 79 73 on embedded sys
bb930 74 65 6d 73 20 77 68 65 72 65 20 6d 65 6d 6f 72 tems where memor
bb940 79 20 69 73 20 73 63 61 72 63 65 20 61 6e 64 0a y is scarce and.
bb950 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 ** malloc failur
bb960 65 73 20 68 61 70 70 65 6e 20 66 72 65 71 75 65 es happen freque
bb970 6e 74 6c 79 2e 20 20 4f 53 2f 32 20 64 6f 65 73 ntly. OS/2 does
bb980 20 6e 6f 74 20 74 79 70 69 63 61 6c 6c 79 20 72 not typically r
bb990 75 6e 20 6f 6e 0a 2a 2a 20 65 6d 62 65 64 64 65 un on.** embedde
bb9a0 64 20 73 79 73 74 65 6d 73 2c 20 61 6e 64 20 77 d systems, and w
bb9b0 68 65 6e 20 69 74 20 64 6f 65 73 20 74 68 65 20 hen it does the
bb9c0 64 65 76 65 6c 6f 70 65 72 73 20 6e 6f 72 6d 61 developers norma
bb9d0 6c 6c 79 20 68 61 76 65 20 62 69 67 67 65 72 0a lly have bigger.
bb9e0 2a 2a 20 70 72 6f 62 6c 65 6d 73 20 74 6f 20 77 ** problems to w
bb9f0 6f 72 72 79 20 61 62 6f 75 74 20 74 68 61 6e 20 orry about than
bba00 72 75 6e 6e 69 6e 67 20 6f 75 74 20 6f 66 20 6d running out of m
bba10 65 6d 6f 72 79 2e 20 20 53 6f 20 74 68 65 72 65 emory. So there
bba20 20 69 73 20 6e 6f 74 0a 2a 2a 20 61 20 63 6f 6d is not.** a com
bba30 70 65 6c 6c 69 6e 67 20 6e 65 65 64 20 74 6f 20 pelling need to
bba40 75 73 65 20 74 68 65 20 77 72 61 70 70 65 72 73 use the wrappers
bba50 2e 0a 2a 2a 0a 2a 2a 20 42 75 74 20 74 68 65 72 ..**.** But ther
bba60 65 20 69 73 20 61 20 67 6f 6f 64 20 72 65 61 73 e is a good reas
bba70 6f 6e 20 74 6f 20 6e 6f 74 20 75 73 65 20 74 68 on to not use th
bba80 65 20 77 72 61 70 70 65 72 73 2e 20 20 49 66 20 e wrappers. If
bba90 77 65 20 75 73 65 20 74 68 65 0a 2a 2a 20 77 72 we use the.** wr
bbaa0 61 70 70 65 72 73 20 74 68 65 6e 20 77 65 20 77 appers then we w
bbab0 69 6c 6c 20 67 65 74 20 73 69 6d 75 6c 61 74 65 ill get simulate
bbac0 64 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 75 d malloc() failu
bbad0 72 65 73 20 77 69 74 68 69 6e 20 74 68 69 73 0a res within this.
bbae0 2a 2a 20 64 72 69 76 65 72 2e 20 20 41 6e 64 20 ** driver. And
bbaf0 74 68 61 74 20 63 61 75 73 65 73 20 61 6c 6c 20 that causes all
bbb00 6b 69 6e 64 73 20 6f 66 20 70 72 6f 62 6c 65 6d kinds of problem
bbb10 73 20 66 6f 72 20 6f 75 72 20 74 65 73 74 73 2e s for our tests.
bbb20 20 20 57 65 0a 2a 2a 20 63 6f 75 6c 64 20 65 6e We.** could en
bbb30 68 61 6e 63 65 20 53 51 4c 69 74 65 20 74 6f 20 hance SQLite to
bbb40 64 65 61 6c 20 77 69 74 68 20 73 69 6d 75 6c 61 deal with simula
bbb50 74 65 64 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 ted malloc failu
bbb60 72 65 73 20 77 69 74 68 69 6e 0a 2a 2a 20 74 68 res within.** th
bbb70 65 20 4f 53 20 64 72 69 76 65 72 2c 20 62 75 74 e OS driver, but
bbb80 20 74 68 65 20 63 6f 64 65 20 74 6f 20 64 65 61 the code to dea
bbb90 6c 20 77 69 74 68 20 74 68 6f 73 65 20 66 61 69 l with those fai
bbba0 6c 75 72 65 20 77 6f 75 6c 64 20 6e 6f 74 0a 2a lure would not.*
bbbb0 2a 20 62 65 20 65 78 65 72 63 69 73 65 64 20 6f * be exercised o
bbbc0 6e 20 4c 69 6e 75 78 20 28 77 68 69 63 68 20 64 n Linux (which d
bbbd0 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 oes not need to
bbbe0 6d 61 6c 6c 6f 63 28 29 20 69 6e 20 74 68 65 20 malloc() in the
bbbf0 64 72 69 76 65 72 29 0a 2a 2a 20 61 6e 64 20 73 driver).** and s
bbc00 6f 20 77 65 20 77 6f 75 6c 64 20 68 61 76 65 20 o we would have
bbc10 64 69 66 66 69 63 75 6c 74 79 20 77 72 69 74 69 difficulty writi
bbc20 6e 67 20 63 6f 76 65 72 61 67 65 20 74 65 73 74 ng coverage test
bbc30 73 20 66 6f 72 20 74 68 61 74 0a 2a 2a 20 63 6f s for that.** co
bbc40 64 65 2e 20 20 42 65 74 74 65 72 20 74 6f 20 6c de. Better to l
bbc50 65 61 76 65 20 74 68 65 20 63 6f 64 65 20 6f 75 eave the code ou
bbc60 74 2c 20 77 65 20 74 68 69 6e 6b 2e 0a 2a 2a 0a t, we think..**.
bbc70 2a 2a 20 54 68 65 20 70 6f 69 6e 74 20 6f 66 20 ** The point of
bbc80 74 68 69 73 20 64 69 73 63 75 73 73 69 6f 6e 20 this discussion
bbc90 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 20 20 is as follows:
bbca0 57 68 65 6e 20 63 72 65 61 74 69 6e 67 20 61 20 When creating a
bbcb0 6e 65 77 0a 2a 2a 20 4f 53 20 6c 61 79 65 72 20 new.** OS layer
bbcc0 66 6f 72 20 61 6e 20 65 6d 62 65 64 64 65 64 20 for an embedded
bbcd0 73 79 73 74 65 6d 2c 20 69 66 20 79 6f 75 20 75 system, if you u
bbce0 73 65 20 74 68 69 73 20 66 69 6c 65 20 61 73 20 se this file as
bbcf0 61 6e 20 65 78 61 6d 70 6c 65 2c 0a 2a 2a 20 61 an example,.** a
bbd00 76 6f 69 64 20 74 68 65 20 75 73 65 20 6f 66 20 void the use of
bbd10 6d 61 6c 6c 6f 63 28 29 2f 66 72 65 65 28 29 2e malloc()/free().
bbd20 20 20 54 68 6f 73 65 20 72 6f 75 74 69 6e 65 73 Those routines
bbd30 20 77 6f 72 6b 20 6f 6b 20 6f 6e 20 4f 53 2f 32 work ok on OS/2
bbd40 0a 2a 2a 20 64 65 73 6b 74 6f 70 73 20 62 75 74 .** desktops but
bbd50 20 6e 6f 74 20 73 6f 20 77 65 6c 6c 20 69 6e 20 not so well in
bbd60 65 6d 62 65 64 64 65 64 20 73 79 73 74 65 6d 73 embedded systems
bbd70 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 ..*/../*.** Macr
bbd80 6f 73 20 75 73 65 64 20 74 6f 20 64 65 74 65 72 os used to deter
bbd90 6d 69 6e 65 20 77 68 65 74 68 65 72 20 6f 72 20 mine whether or
bbda0 6e 6f 74 20 74 6f 20 75 73 65 20 74 68 72 65 61 not to use threa
bbdb0 64 73 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e ds..*/.#if defin
bbdc0 65 64 28 53 51 4c 49 54 45 5f 54 48 52 45 41 44 ed(SQLITE_THREAD
bbdd0 53 41 46 45 29 20 26 26 20 53 51 4c 49 54 45 5f SAFE) && SQLITE_
bbde0 54 48 52 45 41 44 53 41 46 45 0a 23 20 64 65 66 THREADSAFE.# def
bbdf0 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 32 5f 54 ine SQLITE_OS2_T
bbe00 48 52 45 41 44 53 20 31 0a 23 65 6e 64 69 66 0a HREADS 1.#endif.
bbe10 0a 2f 2a 0a 2a 2a 20 49 6e 63 6c 75 64 65 20 63 ./*.** Include c
bbe20 6f 64 65 20 74 68 61 74 20 69 73 20 63 6f 6d 6d ode that is comm
bbe30 6f 6e 20 74 6f 20 61 6c 6c 20 6f 73 5f 2a 2e 63 on to all os_*.c
bbe40 20 66 69 6c 65 73 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a files.*/./*****
bbe50 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 ********* Includ
bbe60 65 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 69 6e e os_common.h in
bbe70 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 6f the middle of o
bbe80 73 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a s_os2.c ********
bbe90 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
bbea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 ********* Begin
bbeb0 66 69 6c 65 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 file os_common.h
bbec0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
bbed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bbee0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 ********/./*.**
bbef0 32 30 30 34 20 4d 61 79 20 32 32 0a 2a 2a 0a 2a 2004 May 22.**.*
bbf00 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 * The author dis
bbf10 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 claims copyright
bbf20 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 to this source
bbf30 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 code. In place
bbf40 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f of.** a legal no
bbf50 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 tice, here is a
bbf60 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 blessing:.**.**
bbf70 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f May you do go
bbf80 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e od and not evil.
bbf90 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 .** May you f
bbfa0 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 ind forgiveness
bbfb0 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 for yourself and
bbfc0 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e forgive others.
bbfd0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 .** May you s
bbfe0 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 hare freely, nev
bbff0 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 er taking more t
bc000 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a han you give..**
bc010 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
bc020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bc030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bc040 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bc050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
bc060 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 **.** This file
bc070 63 6f 6e 74 61 69 6e 73 20 6d 61 63 72 6f 73 20 contains macros
bc080 61 6e 64 20 61 20 6c 69 74 74 6c 65 20 62 69 74 and a little bit
bc090 20 6f 66 20 63 6f 64 65 20 74 68 61 74 20 69 73 of code that is
bc0a0 20 63 6f 6d 6d 6f 6e 20 74 6f 0a 2a 2a 20 61 6c common to.** al
bc0b0 6c 20 6f 66 20 74 68 65 20 70 6c 61 74 66 6f 72 l of the platfor
bc0c0 6d 2d 73 70 65 63 69 66 69 63 20 66 69 6c 65 73 m-specific files
bc0d0 20 28 6f 73 5f 2a 2e 63 29 20 61 6e 64 20 69 73 (os_*.c) and is
bc0e0 20 23 69 6e 63 6c 75 64 65 64 20 69 6e 74 6f 20 #included into
bc0f0 74 68 6f 73 65 0a 2a 2a 20 66 69 6c 65 73 2e 0a those.** files..
bc100 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 **.** This file
bc110 73 68 6f 75 6c 64 20 62 65 20 23 69 6e 63 6c 75 should be #inclu
bc120 64 65 64 20 62 79 20 74 68 65 20 6f 73 5f 2a 2e ded by the os_*.
bc130 63 20 66 69 6c 65 73 20 6f 6e 6c 79 2e 20 20 49 c files only. I
bc140 74 20 69 73 20 6e 6f 74 20 61 0a 2a 2a 20 67 65 t is not a.** ge
bc150 6e 65 72 61 6c 20 70 75 72 70 6f 73 65 20 68 65 neral purpose he
bc160 61 64 65 72 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a ader file..**.**
bc170 20 24 49 64 3a 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e $Id: os_common.
bc180 68 2c 76 20 31 2e 33 38 20 32 30 30 39 2f 30 32 h,v 1.38 2009/02
bc190 2f 32 34 20 31 38 3a 34 30 3a 35 30 20 64 61 6e /24 18:40:50 dan
bc1a0 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a ielk1977 Exp $.*
bc1b0 2f 0a 23 69 66 6e 64 65 66 20 5f 4f 53 5f 43 4f /.#ifndef _OS_CO
bc1c0 4d 4d 4f 4e 5f 48 5f 0a 23 64 65 66 69 6e 65 20 MMON_H_.#define
bc1d0 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a 0a 2f _OS_COMMON_H_../
bc1e0 2a 0a 2a 2a 20 41 74 20 6c 65 61 73 74 20 74 77 *.** At least tw
bc1f0 6f 20 62 75 67 73 20 68 61 76 65 20 73 6c 69 70 o bugs have slip
bc200 70 65 64 20 69 6e 20 62 65 63 61 75 73 65 20 77 ped in because w
bc210 65 20 63 68 61 6e 67 65 64 20 74 68 65 20 4d 45 e changed the ME
bc220 4d 4f 52 59 5f 44 45 42 55 47 0a 2a 2a 20 6d 61 MORY_DEBUG.** ma
bc230 63 72 6f 20 74 6f 20 53 51 4c 49 54 45 5f 44 45 cro to SQLITE_DE
bc240 42 55 47 20 61 6e 64 20 73 6f 6d 65 20 6f 6c 64 BUG and some old
bc250 65 72 20 6d 61 6b 65 66 69 6c 65 73 20 68 61 76 er makefiles hav
bc260 65 20 6e 6f 74 20 79 65 74 20 6d 61 64 65 20 74 e not yet made t
bc270 68 65 0a 2a 2a 20 73 77 69 74 63 68 2e 20 20 54 he.** switch. T
bc280 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64 he following cod
bc290 65 20 73 68 6f 75 6c 64 20 63 61 74 63 68 20 74 e should catch t
bc2a0 68 69 73 20 70 72 6f 62 6c 65 6d 20 61 74 20 63 his problem at c
bc2b0 6f 6d 70 69 6c 65 2d 74 69 6d 65 2e 0a 2a 2f 0a ompile-time..*/.
bc2c0 23 69 66 64 65 66 20 4d 45 4d 4f 52 59 5f 44 45 #ifdef MEMORY_DE
bc2d0 42 55 47 0a 23 20 65 72 72 6f 72 20 22 54 68 65 BUG.# error "The
bc2e0 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47 20 6d 61 MEMORY_DEBUG ma
bc2f0 63 72 6f 20 69 73 20 6f 62 73 6f 6c 65 74 65 2e cro is obsolete.
bc300 20 20 55 73 65 20 53 51 4c 49 54 45 5f 44 45 42 Use SQLITE_DEB
bc310 55 47 20 69 6e 73 74 65 61 64 2e 22 0a 23 65 6e UG instead.".#en
bc320 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 dif..#ifdef SQLI
bc330 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 5f TE_DEBUG.SQLITE_
bc340 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
bc350 74 65 33 4f 53 54 72 61 63 65 20 3d 20 30 3b 0a te3OSTrace = 0;.
bc360 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 31 #define OSTRACE1
bc370 28 58 29 20 20 20 20 20 20 20 20 20 69 66 28 20 (X) if(
bc380 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 sqlite3OSTrace )
bc390 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 sqlite3DebugPri
bc3a0 6e 74 66 28 58 29 0a 23 64 65 66 69 6e 65 20 4f ntf(X).#define O
bc3b0 53 54 52 41 43 45 32 28 58 2c 59 29 20 20 20 20 STRACE2(X,Y)
bc3c0 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f 53 if( sqlite3OS
bc3d0 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 44 Trace ) sqlite3D
bc3e0 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 29 0a ebugPrintf(X,Y).
bc3f0 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 33 #define OSTRACE3
bc400 28 58 2c 59 2c 5a 29 20 20 20 20 20 69 66 28 20 (X,Y,Z) if(
bc410 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 sqlite3OSTrace )
bc420 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 sqlite3DebugPri
bc430 6e 74 66 28 58 2c 59 2c 5a 29 0a 23 64 65 66 69 ntf(X,Y,Z).#defi
bc440 6e 65 20 4f 53 54 52 41 43 45 34 28 58 2c 59 2c ne OSTRACE4(X,Y,
bc450 5a 2c 41 29 20 20 20 69 66 28 20 73 71 6c 69 74 Z,A) if( sqlit
bc460 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69 e3OSTrace ) sqli
bc470 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 te3DebugPrintf(X
bc480 2c 59 2c 5a 2c 41 29 0a 23 64 65 66 69 6e 65 20 ,Y,Z,A).#define
bc490 4f 53 54 52 41 43 45 35 28 58 2c 59 2c 5a 2c 41 OSTRACE5(X,Y,Z,A
bc4a0 2c 42 29 20 69 66 28 20 73 71 6c 69 74 65 33 4f ,B) if( sqlite3O
bc4b0 53 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 STrace ) sqlite3
bc4c0 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c DebugPrintf(X,Y,
bc4d0 5a 2c 41 2c 42 29 0a 23 64 65 66 69 6e 65 20 4f Z,A,B).#define O
bc4e0 53 54 52 41 43 45 36 28 58 2c 59 2c 5a 2c 41 2c STRACE6(X,Y,Z,A,
bc4f0 42 2c 43 29 20 5c 0a 20 20 20 20 69 66 28 73 71 B,C) \. if(sq
bc500 6c 69 74 65 33 4f 53 54 72 61 63 65 29 20 73 71 lite3OSTrace) sq
bc510 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 lite3DebugPrintf
bc520 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 0a 23 64 (X,Y,Z,A,B,C).#d
bc530 65 66 69 6e 65 20 4f 53 54 52 41 43 45 37 28 58 efine OSTRACE7(X
bc540 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 20 5c 0a ,Y,Z,A,B,C,D) \.
bc550 20 20 20 20 69 66 28 73 71 6c 69 74 65 33 4f 53 if(sqlite3OS
bc560 54 72 61 63 65 29 20 73 71 6c 69 74 65 33 44 65 Trace) sqlite3De
bc570 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c bugPrintf(X,Y,Z,
bc580 41 2c 42 2c 43 2c 44 29 0a 23 65 6c 73 65 0a 23 A,B,C,D).#else.#
bc590 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 31 28 define OSTRACE1(
bc5a0 58 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 X).#define OSTRA
bc5b0 43 45 32 28 58 2c 59 29 0a 23 64 65 66 69 6e 65 CE2(X,Y).#define
bc5c0 20 4f 53 54 52 41 43 45 33 28 58 2c 59 2c 5a 29 OSTRACE3(X,Y,Z)
bc5d0 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 .#define OSTRACE
bc5e0 34 28 58 2c 59 2c 5a 2c 41 29 0a 23 64 65 66 69 4(X,Y,Z,A).#defi
bc5f0 6e 65 20 4f 53 54 52 41 43 45 35 28 58 2c 59 2c ne OSTRACE5(X,Y,
bc600 5a 2c 41 2c 42 29 0a 23 64 65 66 69 6e 65 20 4f Z,A,B).#define O
bc610 53 54 52 41 43 45 36 28 58 2c 59 2c 5a 2c 41 2c STRACE6(X,Y,Z,A,
bc620 42 2c 43 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 B,C).#define OST
bc630 52 41 43 45 37 28 58 2c 59 2c 5a 2c 41 2c 42 2c RACE7(X,Y,Z,A,B,
bc640 43 2c 44 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a C,D).#endif../*.
bc650 2a 2a 20 4d 61 63 72 6f 73 20 66 6f 72 20 70 65 ** Macros for pe
bc660 72 66 6f 72 6d 61 6e 63 65 20 74 72 61 63 69 6e rformance tracin
bc670 67 2e 20 20 4e 6f 72 6d 61 6c 6c 79 20 74 75 72 g. Normally tur
bc680 6e 65 64 20 6f 66 66 2e 20 20 4f 6e 6c 79 20 77 ned off. Only w
bc690 6f 72 6b 73 0a 2a 2a 20 6f 6e 20 69 34 38 36 20 orks.** on i486
bc6a0 68 61 72 64 77 61 72 65 2e 0a 2a 2f 0a 23 69 66 hardware..*/.#if
bc6b0 64 65 66 20 53 51 4c 49 54 45 5f 50 45 52 46 4f def SQLITE_PERFO
bc6c0 52 4d 41 4e 43 45 5f 54 52 41 43 45 0a 0a 2f 2a RMANCE_TRACE../*
bc6d0 20 0a 2a 2a 20 68 77 74 69 6d 65 2e 68 20 63 6f .** hwtime.h co
bc6e0 6e 74 61 69 6e 73 20 69 6e 6c 69 6e 65 20 61 73 ntains inline as
bc6f0 73 65 6d 62 6c 65 72 20 63 6f 64 65 20 66 6f 72 sembler code for
bc700 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 0a 2a implementing .*
bc710 2a 20 68 69 67 68 2d 70 65 72 66 6f 72 6d 61 6e * high-performan
bc720 63 65 20 74 69 6d 69 6e 67 20 72 6f 75 74 69 6e ce timing routin
bc730 65 73 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a es..*/./********
bc740 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 68 ****** Include h
bc750 77 74 69 6d 65 2e 68 20 69 6e 20 74 68 65 20 6d wtime.h in the m
bc760 69 64 64 6c 65 20 6f 66 20 6f 73 5f 63 6f 6d 6d iddle of os_comm
bc770 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a on.h ***********
bc780 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
bc790 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c ****** Begin fil
bc7a0 65 20 68 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a e hwtime.h *****
bc7b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bc7c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bc7d0 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 *****/./*.** 200
bc7e0 38 20 4d 61 79 20 32 37 0a 2a 2a 0a 2a 2a 20 54 8 May 27.**.** T
bc7f0 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 he author discla
bc800 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f ims copyright to
bc810 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 this source cod
bc820 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a e. In place of.
bc830 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 ** a legal notic
bc840 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 e, here is a ble
bc850 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 ssing:.**.**
bc860 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 May you do good
bc870 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a and not evil..**
bc880 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 May you find
bc890 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 forgiveness for
bc8a0 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f yourself and fo
bc8b0 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a rgive others..**
bc8c0 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 May you shar
bc8d0 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 e freely, never
bc8e0 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e taking more than
bc8f0 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a you give..**.**
bc900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bc910 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bc920 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bc930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bc940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a ************.**.
bc950 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e ** This file con
bc960 74 61 69 6e 73 20 69 6e 6c 69 6e 65 20 61 73 6d tains inline asm
bc970 20 63 6f 64 65 20 66 6f 72 20 72 65 74 72 69 65 code for retrie
bc980 76 69 6e 67 20 22 68 69 67 68 2d 70 65 72 66 6f ving "high-perfo
bc990 72 6d 61 6e 63 65 22 0a 2a 2a 20 63 6f 75 6e 74 rmance".** count
bc9a0 65 72 73 20 66 6f 72 20 78 38 36 20 63 6c 61 73 ers for x86 clas
bc9b0 73 20 43 50 55 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 s CPUs..**.** $I
bc9c0 64 3a 20 68 77 74 69 6d 65 2e 68 2c 76 20 31 2e d: hwtime.h,v 1.
bc9d0 33 20 32 30 30 38 2f 30 38 2f 30 31 20 31 34 3a 3 2008/08/01 14:
bc9e0 33 33 3a 31 35 20 73 68 61 6e 65 20 45 78 70 20 33:15 shane Exp
bc9f0 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 48 57 $.*/.#ifndef _HW
bca00 54 49 4d 45 5f 48 5f 0a 23 64 65 66 69 6e 65 20 TIME_H_.#define
bca10 5f 48 57 54 49 4d 45 5f 48 5f 0a 0a 2f 2a 0a 2a _HWTIME_H_../*.*
bca20 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * The following
bca30 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 77 6f 72 routine only wor
bca40 6b 73 20 6f 6e 20 70 65 6e 74 69 75 6d 2d 63 6c ks on pentium-cl
bca50 61 73 73 20 28 6f 72 20 6e 65 77 65 72 29 20 70 ass (or newer) p
bca60 72 6f 63 65 73 73 6f 72 73 2e 0a 2a 2a 20 49 74 rocessors..** It
bca70 20 75 73 65 73 20 74 68 65 20 52 44 54 53 43 20 uses the RDTSC
bca80 6f 70 63 6f 64 65 20 74 6f 20 72 65 61 64 20 74 opcode to read t
bca90 68 65 20 63 79 63 6c 65 20 63 6f 75 6e 74 20 76 he cycle count v
bcaa0 61 6c 75 65 20 6f 75 74 20 6f 66 20 74 68 65 0a alue out of the.
bcab0 2a 2a 20 70 72 6f 63 65 73 73 6f 72 20 61 6e 64 ** processor and
bcac0 20 72 65 74 75 72 6e 73 20 74 68 61 74 20 76 61 returns that va
bcad0 6c 75 65 2e 20 20 54 68 69 73 20 63 61 6e 20 62 lue. This can b
bcae0 65 20 75 73 65 64 20 66 6f 72 20 68 69 67 68 2d e used for high-
bcaf0 72 65 73 0a 2a 2a 20 70 72 6f 66 69 6c 69 6e 67 res.** profiling
bcb00 2e 0a 2a 2f 0a 23 69 66 20 28 64 65 66 69 6e 65 ..*/.#if (define
bcb10 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 7c 7c 20 64 d(__GNUC__) || d
bcb20 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 efined(_MSC_VER)
bcb30 29 20 26 26 20 5c 0a 20 20 20 20 20 20 28 64 65 ) && \. (de
bcb40 66 69 6e 65 64 28 69 33 38 36 29 20 7c 7c 20 64 fined(i386) || d
bcb50 65 66 69 6e 65 64 28 5f 5f 69 33 38 36 5f 5f 29 efined(__i386__)
bcb60 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4d 5f 49 || defined(_M_I
bcb70 58 38 36 29 29 0a 0a 20 20 23 69 66 20 64 65 66 X86)).. #if def
bcb80 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 0a 0a ined(__GNUC__)..
bcb90 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c __inline__ sql
bcba0 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 ite_uint64 sqlit
bcbb0 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a e3Hwtime(void){.
bcbc0 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e unsigned in
bcbd0 74 20 6c 6f 2c 20 68 69 3b 0a 20 20 20 20 20 5f t lo, hi;. _
bcbe0 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c _asm__ __volatil
bcbf0 65 5f 5f 20 28 22 72 64 74 73 63 22 20 3a 20 22 e__ ("rdtsc" : "
bcc00 3d 61 22 20 28 6c 6f 29 2c 20 22 3d 64 22 20 28 =a" (lo), "=d" (
bcc10 68 69 29 29 3b 0a 20 20 20 20 20 72 65 74 75 72 hi));. retur
bcc20 6e 20 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 n (sqlite_uint64
bcc30 29 68 69 20 3c 3c 20 33 32 20 7c 20 6c 6f 3b 0a )hi << 32 | lo;.
bcc40 20 20 7d 0a 0a 20 20 23 65 6c 69 66 20 64 65 66 }.. #elif def
bcc50 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 0a 0a ined(_MSC_VER)..
bcc60 20 20 5f 5f 64 65 63 6c 73 70 65 63 28 6e 61 6b __declspec(nak
bcc70 65 64 29 20 5f 5f 69 6e 6c 69 6e 65 20 73 71 6c ed) __inline sql
bcc80 69 74 65 5f 75 69 6e 74 36 34 20 5f 5f 63 64 65 ite_uint64 __cde
bcc90 63 6c 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 cl sqlite3Hwtime
bcca0 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 5f 5f 61 (void){. __a
bccb0 73 6d 20 7b 0a 20 20 20 20 20 20 20 20 72 64 74 sm {. rdt
bccc0 73 63 0a 20 20 20 20 20 20 20 20 72 65 74 20 20 sc. ret
bccd0 20 20 20 20 20 3b 20 72 65 74 75 72 6e 20 76 61 ; return va
bcce0 6c 75 65 20 61 74 20 45 44 58 3a 45 41 58 0a 20 lue at EDX:EAX.
bccf0 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 23 65 6e }. }.. #en
bcd00 64 69 66 0a 0a 23 65 6c 69 66 20 28 64 65 66 69 dif..#elif (defi
bcd10 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 ned(__GNUC__) &&
bcd20 20 64 65 66 69 6e 65 64 28 5f 5f 78 38 36 5f 36 defined(__x86_6
bcd30 34 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 4__)).. __inlin
bcd40 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 e__ sqlite_uint6
bcd50 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 4 sqlite3Hwtime(
bcd60 76 6f 69 64 29 7b 0a 20 20 20 20 20 20 75 6e 73 void){. uns
bcd70 69 67 6e 65 64 20 6c 6f 6e 67 20 76 61 6c 3b 0a igned long val;.
bcd80 20 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f __asm__ __
bcd90 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 72 64 74 volatile__ ("rdt
bcda0 73 63 22 20 3a 20 22 3d 41 22 20 28 76 61 6c 29 sc" : "=A" (val)
bcdb0 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 );. return
bcdc0 76 61 6c 3b 0a 20 20 7d 0a 20 0a 23 65 6c 69 66 val;. }. .#elif
bcdd0 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 (defined(__GNUC
bcde0 5f 5f 29 20 26 26 20 64 65 66 69 6e 65 64 28 5f __) && defined(_
bcdf0 5f 70 70 63 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e _ppc__)).. __in
bce00 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 line__ sqlite_ui
bce10 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 nt64 sqlite3Hwti
bce20 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 20 me(void){.
bce30 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f unsigned long lo
bce40 6e 67 20 72 65 74 76 61 6c 3b 0a 20 20 20 20 20 ng retval;.
bce50 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6a unsigned long j
bce60 75 6e 6b 3b 0a 20 20 20 20 20 20 5f 5f 61 73 6d unk;. __asm
bce70 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 __ __volatile__
bce80 28 22 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 ("\n\.
bce90 31 3a 20 20 20 20 20 20 6d 66 74 62 75 20 20 20 1: mftbu
bcea0 25 31 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 %1\n\.
bceb0 20 20 20 20 20 20 20 20 6d 66 74 62 20 20 20 20 mftb
bcec0 25 4c 30 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 %L0\n\.
bced0 20 20 20 20 20 20 20 20 20 6d 66 74 62 75 20 20 mftbu
bcee0 20 25 30 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 %0\n\.
bcef0 20 20 20 20 20 20 20 20 20 63 6d 70 77 20 20 20 cmpw
bcf00 20 25 30 2c 25 31 5c 6e 5c 0a 20 20 20 20 20 20 %0,%1\n\.
bcf10 20 20 20 20 20 20 20 20 20 20 20 20 62 6e 65 20 bne
bcf20 20 20 20 20 31 62 22 0a 20 20 20 20 20 20 20 20 1b".
bcf30 20 20 20 20 20 20 20 20 20 20 3a 20 22 3d 72 22 : "=r"
bcf40 20 28 72 65 74 76 61 6c 29 2c 20 22 3d 72 22 20 (retval), "=r"
bcf50 28 6a 75 6e 6b 29 29 3b 0a 20 20 20 20 20 20 72 (junk));. r
bcf60 65 74 75 72 6e 20 72 65 74 76 61 6c 3b 0a 20 20 eturn retval;.
bcf70 7d 0a 0a 23 65 6c 73 65 0a 0a 20 20 23 65 72 72 }..#else.. #err
bcf80 6f 72 20 4e 65 65 64 20 69 6d 70 6c 65 6d 65 6e or Need implemen
bcf90 74 61 74 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65 tation of sqlite
bcfa0 33 48 77 74 69 6d 65 28 29 20 66 6f 72 20 79 6f 3Hwtime() for yo
bcfb0 75 72 20 70 6c 61 74 66 6f 72 6d 2e 0a 0a 20 20 ur platform...
bcfc0 2f 2a 0a 20 20 2a 2a 20 54 6f 20 63 6f 6d 70 69 /*. ** To compi
bcfd0 6c 65 20 77 69 74 68 6f 75 74 20 69 6d 70 6c 65 le without imple
bcfe0 6d 65 6e 74 69 6e 67 20 73 71 6c 69 74 65 33 48 menting sqlite3H
bcff0 77 74 69 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 wtime() for your
bd000 20 70 6c 61 74 66 6f 72 6d 2c 0a 20 20 2a 2a 20 platform,. **
bd010 79 6f 75 20 63 61 6e 20 72 65 6d 6f 76 65 20 74 you can remove t
bd020 68 65 20 61 62 6f 76 65 20 23 65 72 72 6f 72 20 he above #error
bd030 61 6e 64 20 75 73 65 20 74 68 65 20 66 6f 6c 6c and use the foll
bd040 6f 77 69 6e 67 0a 20 20 2a 2a 20 73 74 75 62 20 owing. ** stub
bd050 66 75 6e 63 74 69 6f 6e 2e 20 20 59 6f 75 20 77 function. You w
bd060 69 6c 6c 20 6c 6f 73 65 20 74 69 6d 69 6e 67 20 ill lose timing
bd070 73 75 70 70 6f 72 74 20 66 6f 72 20 6d 61 6e 79 support for many
bd080 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 64 65 62 . ** of the deb
bd090 75 67 67 69 6e 67 20 61 6e 64 20 74 65 73 74 69 ugging and testi
bd0a0 6e 67 20 75 74 69 6c 69 74 69 65 73 2c 20 62 75 ng utilities, bu
bd0b0 74 20 69 74 20 73 68 6f 75 6c 64 20 61 74 0a 20 t it should at.
bd0c0 20 2a 2a 20 6c 65 61 73 74 20 63 6f 6d 70 69 6c ** least compil
bd0d0 65 20 61 6e 64 20 72 75 6e 2e 0a 20 20 2a 2f 0a e and run.. */.
bd0e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
bd0f0 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 sqlite_uint64 s
bd100 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 qlite3Hwtime(voi
bd110 64 29 7b 20 72 65 74 75 72 6e 20 28 28 73 71 6c d){ return ((sql
bd120 69 74 65 5f 75 69 6e 74 36 34 29 30 29 3b 20 7d ite_uint64)0); }
bd130 0a 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 ..#endif..#endif
bd140 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 5f 48 57 /* !defined(_HW
bd150 54 49 4d 45 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a TIME_H_) */../**
bd160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
bd170 20 6f 66 20 68 77 74 69 6d 65 2e 68 20 2a 2a 2a of hwtime.h ***
bd180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bd190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bd1a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
bd1b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e ************ Con
bd1c0 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 tinuing where we
bd1d0 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 6f 73 5f left off in os_
bd1e0 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a common.h *******
bd1f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 73 74 ***********/..st
bd200 61 74 69 63 20 73 71 6c 69 74 65 5f 75 69 6e 74 atic sqlite_uint
bd210 36 34 20 67 5f 73 74 61 72 74 3b 0a 73 74 61 74 64 g_start;.stat
bd220 69 63 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 ic sqlite_uint64
bd230 20 67 5f 65 6c 61 70 73 65 64 3b 0a 23 64 65 66 g_elapsed;.#def
bd240 69 6e 65 20 54 49 4d 45 52 5f 53 54 41 52 54 20 ine TIMER_START
bd250 20 20 20 20 20 20 67 5f 73 74 61 72 74 3d 73 71 g_start=sq
bd260 6c 69 74 65 33 48 77 74 69 6d 65 28 29 0a 23 64 lite3Hwtime().#d
bd270 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4e 44 20 efine TIMER_END
bd280 20 20 20 20 20 20 20 20 67 5f 65 6c 61 70 73 65 g_elapse
bd290 64 3d 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 d=sqlite3Hwtime(
bd2a0 29 2d 67 5f 73 74 61 72 74 0a 23 64 65 66 69 6e )-g_start.#defin
bd2b0 65 20 54 49 4d 45 52 5f 45 4c 41 50 53 45 44 20 e TIMER_ELAPSED
bd2c0 20 20 20 20 67 5f 65 6c 61 70 73 65 64 0a 23 65 g_elapsed.#e
bd2d0 6c 73 65 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 lse.#define TIME
bd2e0 52 5f 53 54 41 52 54 0a 23 64 65 66 69 6e 65 20 R_START.#define
bd2f0 54 49 4d 45 52 5f 45 4e 44 0a 23 64 65 66 69 6e TIMER_END.#defin
bd300 65 20 54 49 4d 45 52 5f 45 4c 41 50 53 45 44 20 e TIMER_ELAPSED
bd310 20 20 20 20 28 28 73 71 6c 69 74 65 5f 75 69 6e ((sqlite_uin
bd320 74 36 34 29 30 29 0a 23 65 6e 64 69 66 0a 0a 2f t64)0).#endif../
bd330 2a 0a 2a 2a 20 49 66 20 77 65 20 63 6f 6d 70 69 *.** If we compi
bd340 6c 65 20 77 69 74 68 20 74 68 65 20 53 51 4c 49 le with the SQLI
bd350 54 45 5f 54 45 53 54 20 6d 61 63 72 6f 20 73 65 TE_TEST macro se
bd360 74 2c 20 74 68 65 6e 20 74 68 65 20 66 6f 6c 6c t, then the foll
bd370 6f 77 69 6e 67 20 62 6c 6f 63 6b 0a 2a 2a 20 6f owing block.** o
bd380 66 20 63 6f 64 65 20 77 69 6c 6c 20 67 69 76 65 f code will give
bd390 20 75 73 20 74 68 65 20 61 62 69 6c 69 74 79 20 us the ability
bd3a0 74 6f 20 73 69 6d 75 6c 61 74 65 20 61 20 64 69 to simulate a di
bd3b0 73 6b 20 49 2f 4f 20 65 72 72 6f 72 2e 20 20 54 sk I/O error. T
bd3c0 68 69 73 0a 2a 2a 20 69 73 20 75 73 65 64 20 66 his.** is used f
bd3d0 6f 72 20 74 65 73 74 69 6e 67 20 74 68 65 20 49 or testing the I
bd3e0 2f 4f 20 72 65 63 6f 76 65 72 79 20 6c 6f 67 69 /O recovery logi
bd3f0 63 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c c..*/.#ifdef SQL
bd400 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f ITE_TEST.SQLITE_
bd410 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
bd420 69 6f 5f 65 72 72 6f 72 5f 68 69 74 20 3d 20 30 io_error_hit = 0
bd430 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
bd440 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 Total number of
bd450 49 2f 4f 20 45 72 72 6f 72 73 20 2a 2f 0a 53 51 I/O Errors */.SQ
bd460 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
bd470 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 61 ite3_io_error_ha
bd480 72 64 68 69 74 20 3d 20 30 3b 20 20 20 20 20 20 rdhit = 0;
bd490 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e /* Number of n
bd4a0 6f 6e 2d 62 65 6e 69 67 6e 20 65 72 72 6f 72 73 on-benign errors
bd4b0 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 */.SQLITE_API i
bd4c0 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 nt sqlite3_io_er
bd4d0 72 6f 72 5f 70 65 6e 64 69 6e 67 20 3d 20 30 3b ror_pending = 0;
bd4e0 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 75 6e 74 /* Count
bd4f0 20 64 6f 77 6e 20 74 6f 20 66 69 72 73 74 20 49 down to first I
bd500 2f 4f 20 65 72 72 6f 72 20 2a 2f 0a 53 51 4c 49 /O error */.SQLI
bd510 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
bd520 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 72 73 e3_io_error_pers
bd530 69 73 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 ist = 0;
bd540 2f 2a 20 54 72 75 65 20 69 66 20 49 2f 4f 20 65 /* True if I/O e
bd550 72 72 6f 72 73 20 70 65 72 73 69 73 74 20 2a 2f rrors persist */
bd560 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
bd570 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 sqlite3_io_error
bd580 5f 62 65 6e 69 67 6e 20 3d 20 30 3b 20 20 20 20 _benign = 0;
bd590 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
bd5a0 65 72 72 6f 72 73 20 61 72 65 20 62 65 6e 69 67 errors are benig
bd5b0 6e 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 n */.SQLITE_API
bd5c0 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 69 73 6b int sqlite3_disk
bd5d0 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 3d 20 30 full_pending = 0
bd5e0 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 ;.SQLITE_API int
bd5f0 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c sqlite3_diskful
bd600 6c 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 53 l = 0;.#define S
bd610 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 imulateIOErrorBe
bd620 6e 69 67 6e 28 58 29 20 73 71 6c 69 74 65 33 5f nign(X) sqlite3_
bd630 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67 6e 3d io_error_benign=
bd640 28 58 29 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 (X).#define Simu
bd650 6c 61 74 65 49 4f 45 72 72 6f 72 28 43 4f 44 45 lateIOError(CODE
bd660 29 20 20 5c 0a 20 20 69 66 28 20 28 73 71 6c 69 ) \. if( (sqli
bd670 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 72 te3_io_error_per
bd680 73 69 73 74 20 26 26 20 73 71 6c 69 74 65 33 5f sist && sqlite3_
bd690 69 6f 5f 65 72 72 6f 72 5f 68 69 74 29 20 5c 0a io_error_hit) \.
bd6a0 20 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 || sqlite
bd6b0 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 3_io_error_pendi
bd6c0 6e 67 2d 2d 20 3d 3d 20 31 20 29 20 20 5c 0a 20 ng-- == 1 ) \.
bd6d0 20 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 6c { l
bd6e0 6f 63 61 6c 5f 69 6f 65 72 72 28 29 3b 20 43 4f ocal_ioerr(); CO
bd6f0 44 45 3b 20 7d 0a 73 74 61 74 69 63 20 76 6f 69 DE; }.static voi
bd700 64 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 29 7b d local_ioerr(){
bd710 0a 20 20 49 4f 54 52 41 43 45 28 28 22 49 4f 45 . IOTRACE(("IOE
bd720 52 52 5c 6e 22 29 29 3b 0a 20 20 73 71 6c 69 74 RR\n"));. sqlit
bd730 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 2b e3_io_error_hit+
bd740 2b 3b 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 +;. if( !sqlite
bd750 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67 3_io_error_benig
bd760 6e 20 29 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 n ) sqlite3_io_e
bd770 72 72 6f 72 5f 68 61 72 64 68 69 74 2b 2b 3b 0a rror_hardhit++;.
bd780 7d 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 }.#define Simula
bd790 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 teDiskfullError(
bd7a0 43 4f 44 45 29 20 5c 0a 20 20 20 69 66 28 20 73 CODE) \. if( s
bd7b0 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f qlite3_diskfull_
bd7c0 70 65 6e 64 69 6e 67 20 29 7b 20 5c 0a 20 20 20 pending ){ \.
bd7d0 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 64 69 if( sqlite3_di
bd7e0 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 3d skfull_pending =
bd7f0 3d 20 31 20 29 7b 20 5c 0a 20 20 20 20 20 20 20 = 1 ){ \.
bd800 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 29 3b 20 5c local_ioerr(); \
bd810 0a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f . sqlite3_
bd820 64 69 73 6b 66 75 6c 6c 20 3d 20 31 3b 20 5c 0a diskfull = 1; \.
bd830 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 sqlite3_i
bd840 6f 5f 65 72 72 6f 72 5f 68 69 74 20 3d 20 31 3b o_error_hit = 1;
bd850 20 5c 0a 20 20 20 20 20 20 20 43 4f 44 45 3b 20 \. CODE;
bd860 5c 0a 20 20 20 20 20 7d 65 6c 73 65 7b 20 5c 0a \. }else{ \.
bd870 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 64 sqlite3_d
bd880 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 2d iskfull_pending-
bd890 2d 3b 20 5c 0a 20 20 20 20 20 7d 20 5c 0a 20 20 -; \. } \.
bd8a0 20 7d 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 }.#else.#define
bd8b0 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 SimulateIOError
bd8c0 42 65 6e 69 67 6e 28 58 29 0a 23 64 65 66 69 6e Benign(X).#defin
bd8d0 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f e SimulateIOErro
bd8e0 72 28 41 29 0a 23 64 65 66 69 6e 65 20 53 69 6d r(A).#define Sim
bd8f0 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 ulateDiskfullErr
bd900 6f 72 28 41 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a or(A).#endif../*
bd910 0a 2a 2a 20 57 68 65 6e 20 74 65 73 74 69 6e 67 .** When testing
bd920 2c 20 6b 65 65 70 20 61 20 63 6f 75 6e 74 20 6f , keep a count o
bd930 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 f the number of
bd940 6f 70 65 6e 20 66 69 6c 65 73 2e 0a 2a 2f 0a 23 open files..*/.#
bd950 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 ifdef SQLITE_TES
bd960 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 T.SQLITE_API int
bd970 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 66 69 sqlite3_open_fi
bd980 6c 65 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 64 le_count = 0;.#d
bd990 65 66 69 6e 65 20 4f 70 65 6e 43 6f 75 6e 74 65 efine OpenCounte
bd9a0 72 28 58 29 20 20 73 71 6c 69 74 65 33 5f 6f 70 r(X) sqlite3_op
bd9b0 65 6e 5f 66 69 6c 65 5f 63 6f 75 6e 74 2b 3d 28 en_file_count+=(
bd9c0 58 29 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 X).#else.#define
bd9d0 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 58 29 0a OpenCounter(X).
bd9e0 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f #endif..#endif /
bd9f0 2a 20 21 64 65 66 69 6e 65 64 28 5f 4f 53 5f 43 * !defined(_OS_C
bda00 4f 4d 4d 4f 4e 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a OMMON_H_) */../*
bda10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e ************* En
bda20 64 20 6f 66 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 d of os_common.h
bda30 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
bda40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bda50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
bda60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f ************* Co
bda70 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 ntinuing where w
bda80 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 6f 73 e left off in os
bda90 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a _os2.c *********
bdaa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f ************/../
bdab0 2a 0a 2a 2a 20 54 68 65 20 6f 73 32 46 69 6c 65 *.** The os2File
bdac0 20 73 74 72 75 63 74 75 72 65 20 69 73 20 73 75 structure is su
bdad0 62 63 6c 61 73 73 20 6f 66 20 73 71 6c 69 74 65 bclass of sqlite
bdae0 33 5f 66 69 6c 65 20 73 70 65 63 69 66 69 63 20 3_file specific
bdaf0 66 6f 72 20 74 68 65 20 4f 53 2f 32 0a 2a 2a 20 for the OS/2.**
bdb00 70 72 6f 74 61 62 69 6c 69 74 79 20 6c 61 79 65 protability laye
bdb10 72 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 r..*/.typedef st
bdb20 72 75 63 74 20 6f 73 32 46 69 6c 65 20 6f 73 32 ruct os2File os2
bdb30 46 69 6c 65 3b 0a 73 74 72 75 63 74 20 6f 73 32 File;.struct os2
bdb40 46 69 6c 65 20 7b 0a 20 20 63 6f 6e 73 74 20 73 File {. const s
bdb50 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 qlite3_io_method
bdb60 73 20 2a 70 4d 65 74 68 6f 64 3b 20 20 2f 2a 20 s *pMethod; /*
bdb70 41 6c 77 61 79 73 20 74 68 65 20 66 69 72 73 74 Always the first
bdb80 20 65 6e 74 72 79 20 2a 2f 0a 20 20 48 46 49 4c entry */. HFIL
bdb90 45 20 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 E h;
bdba0 20 20 20 20 20 20 2f 2a 20 48 61 6e 64 6c 65 20 /* Handle
bdbb0 66 6f 72 20 61 63 63 65 73 73 69 6e 67 20 74 68 for accessing th
bdbc0 65 20 66 69 6c 65 20 2a 2f 0a 20 20 63 68 61 72 e file */. char
bdbd0 2a 20 70 61 74 68 54 6f 44 65 6c 3b 20 20 20 20 * pathToDel;
bdbe0 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 /* Name of
bdbf0 20 66 69 6c 65 20 74 6f 20 64 65 6c 65 74 65 20 file to delete
bdc00 6f 6e 20 63 6c 6f 73 65 2c 20 4e 55 4c 4c 20 69 on close, NULL i
bdc10 66 20 6e 6f 74 20 2a 2f 0a 20 20 75 6e 73 69 67 f not */. unsig
bdc20 6e 65 64 20 63 68 61 72 20 6c 6f 63 6b 74 79 70 ned char locktyp
bdc30 65 3b 20 20 20 2f 2a 20 54 79 70 65 20 6f 66 20 e; /* Type of
bdc40 6c 6f 63 6b 20 63 75 72 72 65 6e 74 6c 79 20 68 lock currently h
bdc50 65 6c 64 20 6f 6e 20 74 68 69 73 20 66 69 6c 65 eld on this file
bdc60 20 2a 2f 0a 7d 3b 0a 0a 23 64 65 66 69 6e 65 20 */.};..#define
bdc70 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 20 31 30 4c LOCK_TIMEOUT 10L
bdc80 20 2f 2a 20 74 68 65 20 64 65 66 61 75 6c 74 20 /* the default
bdc90 6c 6f 63 6b 69 6e 67 20 74 69 6d 65 6f 75 74 20 locking timeout
bdca0 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../***********
bdcb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bdcc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bdcd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bdce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bdcf0 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 67 **.** The next g
bdd00 72 6f 75 70 20 6f 66 20 72 6f 75 74 69 6e 65 73 roup of routines
bdd10 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 49 implement the I
bdd20 2f 4f 20 6d 65 74 68 6f 64 73 20 73 70 65 63 69 /O methods speci
bdd30 66 69 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 73 fied.** by the s
bdd40 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 qlite3_io_method
bdd50 73 20 6f 62 6a 65 63 74 2e 0a 2a 2a 2a 2a 2a 2a s object..******
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 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bdd90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bdda0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a ********/../*.**
bddb0 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 2e 0a 2a Close a file..*
bddc0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 /.static int os2
bddd0 43 6c 6f 73 65 28 20 73 71 6c 69 74 65 33 5f 66 Close( sqlite3_f
bdde0 69 6c 65 20 2a 69 64 20 29 7b 0a 20 20 41 50 49 ile *id ){. API
bddf0 52 45 54 20 72 63 20 3d 20 4e 4f 5f 45 52 52 4f RET rc = NO_ERRO
bde00 52 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70 46 R;. os2File *pF
bde10 69 6c 65 3b 0a 20 20 69 66 28 20 69 64 20 26 26 ile;. if( id &&
bde20 20 28 70 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 (pFile = (os2Fi
bde30 6c 65 2a 29 69 64 29 20 21 3d 20 30 20 29 7b 0a le*)id) != 0 ){.
bde40 20 20 20 20 4f 53 54 52 41 43 45 32 28 20 22 43 OSTRACE2( "C
bde50 4c 4f 53 45 20 25 64 5c 6e 22 2c 20 70 46 69 6c LOSE %d\n", pFil
bde60 65 2d 3e 68 20 29 3b 0a 20 20 20 20 72 63 20 3d e->h );. rc =
bde70 20 44 6f 73 43 6c 6f 73 65 28 20 70 46 69 6c 65 DosClose( pFile
bde80 2d 3e 68 20 29 3b 0a 20 20 20 20 70 46 69 6c 65 ->h );. pFile
bde90 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 4e 4f 5f ->locktype = NO_
bdea0 4c 4f 43 4b 3b 0a 20 20 20 20 69 66 28 20 70 46 LOCK;. if( pF
bdeb0 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c 20 21 ile->pathToDel !
bdec0 3d 20 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20 = NULL ){.
bded0 72 63 20 3d 20 44 6f 73 46 6f 72 63 65 44 65 6c rc = DosForceDel
bdee0 65 74 65 28 20 28 50 53 5a 29 70 46 69 6c 65 2d ete( (PSZ)pFile-
bdef0 3e 70 61 74 68 54 6f 44 65 6c 20 29 3b 0a 20 20 >pathToDel );.
bdf00 20 20 20 20 66 72 65 65 28 20 70 46 69 6c 65 2d free( pFile-
bdf10 3e 70 61 74 68 54 6f 44 65 6c 20 29 3b 0a 20 20 >pathToDel );.
bdf20 20 20 20 20 70 46 69 6c 65 2d 3e 70 61 74 68 54 pFile->pathT
bdf30 6f 44 65 6c 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 oDel = NULL;.
bdf40 20 7d 0a 20 20 20 20 69 64 20 3d 20 30 3b 0a 20 }. id = 0;.
bdf50 20 20 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 20 OpenCounter(
bdf60 2d 31 20 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 -1 );. }.. ret
bdf70 75 72 6e 20 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 urn rc == NO_ERR
bdf80 4f 52 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a OR ? SQLITE_OK :
bdf90 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 7d SQLITE_IOERR;.}
bdfa0 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 64 61 74 ../*.** Read dat
bdfb0 61 20 66 72 6f 6d 20 61 20 66 69 6c 65 20 69 6e a from a file in
bdfc0 74 6f 20 61 20 62 75 66 66 65 72 2e 20 20 52 65 to a buffer. Re
bdfd0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 turn SQLITE_OK i
bdfe0 66 20 61 6c 6c 0a 2a 2a 20 62 79 74 65 73 20 77 f all.** bytes w
bdff0 65 72 65 20 72 65 61 64 20 73 75 63 63 65 73 73 ere read success
be000 66 75 6c 6c 79 20 61 6e 64 20 53 51 4c 49 54 45 fully and SQLITE
be010 5f 49 4f 45 52 52 20 69 66 20 61 6e 79 74 68 69 _IOERR if anythi
be020 6e 67 20 67 6f 65 73 0a 2a 2a 20 77 72 6f 6e 67 ng goes.** wrong
be030 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
be040 6f 73 32 52 65 61 64 28 0a 20 20 73 71 6c 69 74 os2Read(. sqlit
be050 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 20 20 20 e3_file *id,
be060 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 /* Fi
be070 6c 65 20 74 6f 20 72 65 61 64 20 66 72 6f 6d 20 le to read from
be080 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 42 75 66 2c */. void *pBuf,
be090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
be0a0 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 63 6f /* Write co
be0b0 6e 74 65 6e 74 20 69 6e 74 6f 20 74 68 69 73 20 ntent into this
be0c0 62 75 66 66 65 72 20 2a 2f 0a 20 20 69 6e 74 20 buffer */. int
be0d0 61 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 amt,
be0e0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
be0f0 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 74 umber of bytes t
be100 6f 20 72 65 61 64 20 2a 2f 0a 20 20 73 71 6c 69 o read */. sqli
be110 74 65 33 5f 69 6e 74 36 34 20 6f 66 66 73 65 74 te3_int64 offset
be120 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 /* B
be130 65 67 69 6e 20 72 65 61 64 69 6e 67 20 61 74 20 egin reading at
be140 74 68 69 73 20 6f 66 66 73 65 74 20 2a 2f 0a 29 this offset */.)
be150 7b 0a 20 20 55 4c 4f 4e 47 20 66 69 6c 65 4c 6f {. ULONG fileLo
be160 63 61 74 69 6f 6e 20 3d 20 30 4c 3b 0a 20 20 55 cation = 0L;. U
be170 4c 4f 4e 47 20 67 6f 74 3b 0a 20 20 6f 73 32 46 LONG got;. os2F
be180 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 6f 73 ile *pFile = (os
be190 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20 61 73 73 2File*)id;. ass
be1a0 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 ert( id!=0 );.
be1b0 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 SimulateIOError(
be1c0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 return SQLITE_I
be1d0 4f 45 52 52 5f 52 45 41 44 20 29 3b 0a 20 20 4f OERR_READ );. O
be1e0 53 54 52 41 43 45 33 28 20 22 52 45 41 44 20 25 STRACE3( "READ %
be1f0 64 20 6c 6f 63 6b 3d 25 64 5c 6e 22 2c 20 70 46 d lock=%d\n", pF
be200 69 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c ile->h, pFile->l
be210 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 69 66 28 ocktype );. if(
be220 20 44 6f 73 53 65 74 46 69 6c 65 50 74 72 28 70 DosSetFilePtr(p
be230 46 69 6c 65 2d 3e 68 2c 20 6f 66 66 73 65 74 2c File->h, offset,
be240 20 46 49 4c 45 5f 42 45 47 49 4e 2c 20 26 66 69 FILE_BEGIN, &fi
be250 6c 65 4c 6f 63 61 74 69 6f 6e 29 20 21 3d 20 4e leLocation) != N
be260 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 72 O_ERROR ){. r
be270 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 eturn SQLITE_IOE
be280 52 52 3b 0a 20 20 7d 0a 20 20 69 66 28 20 44 6f RR;. }. if( Do
be290 73 52 65 61 64 28 20 70 46 69 6c 65 2d 3e 68 2c sRead( pFile->h,
be2a0 20 70 42 75 66 2c 20 61 6d 74 2c 20 26 67 6f 74 pBuf, amt, &got
be2b0 20 29 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 ) != NO_ERROR )
be2c0 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
be2d0 49 54 45 5f 49 4f 45 52 52 5f 52 45 41 44 3b 0a ITE_IOERR_READ;.
be2e0 20 20 7d 0a 20 20 69 66 28 20 67 6f 74 20 3d 3d }. if( got ==
be2f0 20 28 55 4c 4f 4e 47 29 61 6d 74 20 29 0a 20 20 (ULONG)amt ).
be300 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
be310 4f 4b 3b 0a 20 20 65 6c 73 65 20 7b 0a 20 20 20 OK;. else {.
be320 20 2f 2a 20 55 6e 72 65 61 64 20 70 6f 72 74 69 /* Unread porti
be330 6f 6e 73 20 6f 66 20 74 68 65 20 69 6e 70 75 74 ons of the input
be340 20 62 75 66 66 65 72 20 6d 75 73 74 20 62 65 20 buffer must be
be350 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 2a 2f 0a 20 zero-filled */.
be360 20 20 20 6d 65 6d 73 65 74 28 26 28 28 63 68 61 memset(&((cha
be370 72 2a 29 70 42 75 66 29 5b 67 6f 74 5d 2c 20 30 r*)pBuf)[got], 0
be380 2c 20 61 6d 74 2d 67 6f 74 29 3b 0a 20 20 20 20 , amt-got);.
be390 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f return SQLITE_IO
be3a0 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 3b 0a ERR_SHORT_READ;.
be3b0 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 }.}../*.** Wri
be3c0 74 65 20 64 61 74 61 20 66 72 6f 6d 20 61 20 62 te data from a b
be3d0 75 66 66 65 72 20 69 6e 74 6f 20 61 20 66 69 6c uffer into a fil
be3e0 65 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54 e. Return SQLIT
be3f0 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 0a E_OK on success.
be400 2a 2a 20 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 ** or some other
be410 20 65 72 72 6f 72 20 63 6f 64 65 20 6f 6e 20 66 error code on f
be420 61 69 6c 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 ailure..*/.stati
be430 63 20 69 6e 74 20 6f 73 32 57 72 69 74 65 28 0a c int os2Write(.
be440 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a sqlite3_file *
be450 69 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 id,
be460 20 20 2f 2a 20 46 69 6c 65 20 74 6f 20 77 72 69 /* File to wri
be470 74 65 20 69 6e 74 6f 20 2a 2f 0a 20 20 63 6f 6e te into */. con
be480 73 74 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 20 st void *pBuf,
be490 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
be4a0 54 68 65 20 62 79 74 65 73 20 74 6f 20 62 65 20 The bytes to be
be4b0 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20 69 6e 74 written */. int
be4c0 20 61 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20 amt,
be4d0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
be4e0 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 Number of bytes
be4f0 74 6f 20 77 72 69 74 65 20 2a 2f 0a 20 20 73 71 to write */. sq
be500 6c 69 74 65 33 5f 69 6e 74 36 34 20 6f 66 66 73 lite3_int64 offs
be510 65 74 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a et /*
be520 20 4f 66 66 73 65 74 20 69 6e 74 6f 20 74 68 65 Offset into the
be530 20 66 69 6c 65 20 74 6f 20 62 65 67 69 6e 20 77 file to begin w
be540 72 69 74 69 6e 67 20 61 74 20 2a 2f 0a 29 7b 0a riting at */.){.
be550 20 20 55 4c 4f 4e 47 20 66 69 6c 65 4c 6f 63 61 ULONG fileLoca
be560 74 69 6f 6e 20 3d 20 30 4c 3b 0a 20 20 41 50 49 tion = 0L;. API
be570 52 45 54 20 72 63 20 3d 20 4e 4f 5f 45 52 52 4f RET rc = NO_ERRO
be580 52 3b 0a 20 20 55 4c 4f 4e 47 20 77 72 6f 74 65 R;. ULONG wrote
be590 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70 46 69 ;. os2File *pFi
be5a0 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29 69 le = (os2File*)i
be5b0 64 3b 0a 20 20 61 73 73 65 72 74 28 20 69 64 21 d;. assert( id!
be5c0 3d 30 20 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 =0 );. Simulate
be5d0 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 IOError( return
be5e0 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 57 52 49 SQLITE_IOERR_WRI
be5f0 54 45 20 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 TE );. Simulate
be600 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 20 72 DiskfullError( r
be610 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c eturn SQLITE_FUL
be620 4c 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 L );. OSTRACE3(
be630 20 22 57 52 49 54 45 20 25 64 20 6c 6f 63 6b 3d "WRITE %d lock=
be640 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c %d\n", pFile->h,
be650 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
be660 20 29 3b 0a 20 20 69 66 28 20 44 6f 73 53 65 74 );. if( DosSet
be670 46 69 6c 65 50 74 72 28 70 46 69 6c 65 2d 3e 68 FilePtr(pFile->h
be680 2c 20 6f 66 66 73 65 74 2c 20 46 49 4c 45 5f 42 , offset, FILE_B
be690 45 47 49 4e 2c 20 26 66 69 6c 65 4c 6f 63 61 74 EGIN, &fileLocat
be6a0 69 6f 6e 29 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 ion) != NO_ERROR
be6b0 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
be6c0 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 20 20 7d QLITE_IOERR;. }
be6d0 0a 20 20 61 73 73 65 72 74 28 20 61 6d 74 3e 30 . assert( amt>0
be6e0 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 61 6d 74 );. while( amt
be6f0 20 3e 20 30 20 26 26 0a 20 20 20 20 20 20 20 20 > 0 &&.
be700 20 28 20 72 63 20 3d 20 44 6f 73 57 72 69 74 65 ( rc = DosWrite
be710 28 20 70 46 69 6c 65 2d 3e 68 2c 20 28 50 56 4f ( pFile->h, (PVO
be720 49 44 29 70 42 75 66 2c 20 61 6d 74 2c 20 26 77 ID)pBuf, amt, &w
be730 72 6f 74 65 20 29 20 29 20 3d 3d 20 4e 4f 5f 45 rote ) ) == NO_E
be740 52 52 4f 52 20 26 26 0a 20 20 20 20 20 20 20 20 RROR &&.
be750 20 77 72 6f 74 65 20 3e 20 30 0a 20 20 29 7b 0a wrote > 0. ){.
be760 20 20 20 20 61 6d 74 20 2d 3d 20 77 72 6f 74 65 amt -= wrote
be770 3b 0a 20 20 20 20 70 42 75 66 20 3d 20 26 28 28 ;. pBuf = &((
be780 63 68 61 72 2a 29 70 42 75 66 29 5b 77 72 6f 74 char*)pBuf)[wrot
be790 65 5d 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 e];. }.. retur
be7a0 6e 20 28 20 72 63 20 21 3d 20 4e 4f 5f 45 52 52 n ( rc != NO_ERR
be7b0 4f 52 20 7c 7c 20 61 6d 74 20 3e 20 28 69 6e 74 OR || amt > (int
be7c0 29 77 72 6f 74 65 20 29 20 3f 20 53 51 4c 49 54 )wrote ) ? SQLIT
be7d0 45 5f 46 55 4c 4c 20 3a 20 53 51 4c 49 54 45 5f E_FULL : SQLITE_
be7e0 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 75 OK;.}../*.** Tru
be7f0 6e 63 61 74 65 20 61 6e 20 6f 70 65 6e 20 66 69 ncate an open fi
be800 6c 65 20 74 6f 20 61 20 73 70 65 63 69 66 69 65 le to a specifie
be810 64 20 73 69 7a 65 0a 2a 2f 0a 73 74 61 74 69 63 d size.*/.static
be820 20 69 6e 74 20 6f 73 32 54 72 75 6e 63 61 74 65 int os2Truncate
be830 28 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a ( sqlite3_file *
be840 69 64 2c 20 69 36 34 20 6e 42 79 74 65 20 29 7b id, i64 nByte ){
be850 0a 20 20 41 50 49 52 45 54 20 72 63 20 3d 20 4e . APIRET rc = N
be860 4f 5f 45 52 52 4f 52 3b 0a 20 20 6f 73 32 46 69 O_ERROR;. os2Fi
be870 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 6f 73 32 le *pFile = (os2
be880 46 69 6c 65 2a 29 69 64 3b 0a 20 20 4f 53 54 52 File*)id;. OSTR
be890 41 43 45 33 28 20 22 54 52 55 4e 43 41 54 45 20 ACE3( "TRUNCATE
be8a0 25 64 20 25 6c 6c 64 5c 6e 22 2c 20 70 46 69 6c %d %lld\n", pFil
be8b0 65 2d 3e 68 2c 20 6e 42 79 74 65 20 29 3b 0a 20 e->h, nByte );.
be8c0 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 SimulateIOError
be8d0 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f ( return SQLITE_
be8e0 49 4f 45 52 52 5f 54 52 55 4e 43 41 54 45 20 29 IOERR_TRUNCATE )
be8f0 3b 0a 20 20 72 63 20 3d 20 44 6f 73 53 65 74 46 ;. rc = DosSetF
be900 69 6c 65 53 69 7a 65 28 20 70 46 69 6c 65 2d 3e ileSize( pFile->
be910 68 2c 20 6e 42 79 74 65 20 29 3b 0a 20 20 72 65 h, nByte );. re
be920 74 75 72 6e 20 72 63 20 3d 3d 20 4e 4f 5f 45 52 turn rc == NO_ER
be930 52 4f 52 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 ROR ? SQLITE_OK
be940 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 54 : SQLITE_IOERR_T
be950 52 55 4e 43 41 54 45 3b 0a 7d 0a 0a 23 69 66 64 RUNCATE;.}..#ifd
be960 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 2f ef SQLITE_TEST./
be970 2a 0a 2a 2a 20 43 6f 75 6e 74 20 74 68 65 20 6e *.** Count the n
be980 75 6d 62 65 72 20 6f 66 20 66 75 6c 6c 73 79 6e umber of fullsyn
be990 63 73 20 61 6e 64 20 6e 6f 72 6d 61 6c 20 73 79 cs and normal sy
be9a0 6e 63 73 2e 20 20 54 68 69 73 20 69 73 20 75 73 ncs. This is us
be9b0 65 64 20 74 6f 20 74 65 73 74 0a 2a 2a 20 74 68 ed to test.** th
be9c0 61 74 20 73 79 6e 63 73 20 61 6e 64 20 66 75 6c at syncs and ful
be9d0 6c 73 79 6e 63 73 20 61 72 65 20 6f 63 63 75 72 lsyncs are occur
be9e0 69 6e 67 20 61 74 20 74 68 65 20 72 69 67 68 74 ing at the right
be9f0 20 74 69 6d 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 times..*/.SQLIT
bea00 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
bea10 33 5f 73 79 6e 63 5f 63 6f 75 6e 74 20 3d 20 30 3_sync_count = 0
bea20 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 ;.SQLITE_API int
bea30 20 73 71 6c 69 74 65 33 5f 66 75 6c 6c 73 79 6e sqlite3_fullsyn
bea40 63 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 6e c_count = 0;.#en
bea50 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 dif../*.** Make
bea60 73 75 72 65 20 61 6c 6c 20 77 72 69 74 65 73 20 sure all writes
bea70 74 6f 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 to a particular
bea80 66 69 6c 65 20 61 72 65 20 63 6f 6d 6d 69 74 74 file are committ
bea90 65 64 20 74 6f 20 64 69 73 6b 2e 0a 2a 2f 0a 73 ed to disk..*/.s
beaa0 74 61 74 69 63 20 69 6e 74 20 6f 73 32 53 79 6e tatic int os2Syn
beab0 63 28 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 c( sqlite3_file
beac0 2a 69 64 2c 20 69 6e 74 20 66 6c 61 67 73 20 29 *id, int flags )
bead0 7b 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70 46 69 {. os2File *pFi
beae0 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29 69 le = (os2File*)i
beaf0 64 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 20 22 d;. OSTRACE3( "
beb00 53 59 4e 43 20 25 64 20 6c 6f 63 6b 3d 25 64 5c SYNC %d lock=%d\
beb10 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 70 46 n", pFile->h, pF
beb20 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 3b ile->locktype );
beb30 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 .#ifdef SQLITE_T
beb40 45 53 54 0a 20 20 69 66 28 20 66 6c 61 67 73 20 EST. if( flags
beb50 26 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55 & SQLITE_SYNC_FU
beb60 4c 4c 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 LL){. sqlite3
beb70 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74 2b _fullsync_count+
beb80 2b 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 +;. }. sqlite3
beb90 5f 73 79 6e 63 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 _sync_count++;.#
beba0 65 6e 64 69 66 0a 20 20 2f 2a 20 49 66 20 77 65 endif. /* If we
bebb0 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74 compiled with t
bebc0 68 65 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e he SQLITE_NO_SYN
bebd0 43 20 66 6c 61 67 2c 20 74 68 65 6e 20 73 79 6e C flag, then syn
bebe0 63 69 6e 67 20 69 73 20 61 0a 20 20 2a 2a 20 6e cing is a. ** n
bebf0 6f 2d 6f 70 0a 20 20 2a 2f 0a 23 69 66 64 65 66 o-op. */.#ifdef
bec00 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 0a SQLITE_NO_SYNC.
bec10 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
bec20 45 52 28 70 46 69 6c 65 29 3b 0a 20 20 72 65 74 ER(pFile);. ret
bec30 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 23 urn SQLITE_OK;.#
bec40 65 6c 73 65 0a 20 20 72 65 74 75 72 6e 20 44 6f else. return Do
bec50 73 52 65 73 65 74 42 75 66 66 65 72 28 20 70 46 sResetBuffer( pF
bec60 69 6c 65 2d 3e 68 20 29 20 3d 3d 20 4e 4f 5f 45 ile->h ) == NO_E
bec70 52 52 4f 52 20 3f 20 53 51 4c 49 54 45 5f 4f 4b RROR ? SQLITE_OK
bec80 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b : SQLITE_IOERR;
bec90 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a .#endif.}../*.**
beca0 20 44 65 74 65 72 6d 69 6e 65 20 74 68 65 20 63 Determine the c
becb0 75 72 72 65 6e 74 20 73 69 7a 65 20 6f 66 20 61 urrent size of a
becc0 20 66 69 6c 65 20 69 6e 20 62 79 74 65 73 0a 2a file in bytes.*
becd0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 /.static int os2
bece0 46 69 6c 65 53 69 7a 65 28 20 73 71 6c 69 74 65 FileSize( sqlite
becf0 33 5f 66 69 6c 65 20 2a 69 64 2c 20 73 71 6c 69 3_file *id, sqli
bed00 74 65 33 5f 69 6e 74 36 34 20 2a 70 53 69 7a 65 te3_int64 *pSize
bed10 20 29 7b 0a 20 20 41 50 49 52 45 54 20 72 63 20 ){. APIRET rc
bed20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 46 49 = NO_ERROR;. FI
bed30 4c 45 53 54 41 54 55 53 33 20 66 73 74 73 33 46 LESTATUS3 fsts3F
bed40 69 6c 65 49 6e 66 6f 3b 0a 20 20 6d 65 6d 73 65 ileInfo;. memse
bed50 74 28 26 66 73 74 73 33 46 69 6c 65 49 6e 66 6f t(&fsts3FileInfo
bed60 2c 20 30 2c 20 73 69 7a 65 6f 66 28 66 73 74 73 , 0, sizeof(fsts
bed70 33 46 69 6c 65 49 6e 66 6f 29 29 3b 0a 20 20 61 3FileInfo));. a
bed80 73 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a ssert( id!=0 );.
bed90 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f SimulateIOErro
beda0 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 r( return SQLITE
bedb0 5f 49 4f 45 52 52 5f 46 53 54 41 54 20 29 3b 0a _IOERR_FSTAT );.
bedc0 20 20 72 63 20 3d 20 44 6f 73 51 75 65 72 79 46 rc = DosQueryF
bedd0 69 6c 65 49 6e 66 6f 28 20 28 28 6f 73 32 46 69 ileInfo( ((os2Fi
bede0 6c 65 2a 29 69 64 29 2d 3e 68 2c 20 46 49 4c 5f le*)id)->h, FIL_
bedf0 53 54 41 4e 44 41 52 44 2c 20 26 66 73 74 73 33 STANDARD, &fsts3
bee00 46 69 6c 65 49 6e 66 6f 2c 20 73 69 7a 65 6f 66 FileInfo, sizeof
bee10 28 46 49 4c 45 53 54 41 54 55 53 33 29 20 29 3b (FILESTATUS3) );
bee20 0a 20 20 69 66 28 20 72 63 20 3d 3d 20 4e 4f 5f . if( rc == NO_
bee30 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 2a 70 53 ERROR ){. *pS
bee40 69 7a 65 20 3d 20 66 73 74 73 33 46 69 6c 65 49 ize = fsts3FileI
bee50 6e 66 6f 2e 63 62 46 69 6c 65 3b 0a 20 20 20 20 nfo.cbFile;.
bee60 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
bee70 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 ;. }else{. r
bee80 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 eturn SQLITE_IOE
bee90 52 52 5f 46 53 54 41 54 3b 0a 20 20 7d 0a 7d 0a RR_FSTAT;. }.}.
beea0 0a 2f 2a 0a 2a 2a 20 41 63 71 75 69 72 65 20 61 ./*.** Acquire a
beeb0 20 72 65 61 64 65 72 20 6c 6f 63 6b 2e 0a 2a 2f reader lock..*/
beec0 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74 52 .static int getR
beed0 65 61 64 4c 6f 63 6b 28 20 6f 73 32 46 69 6c 65 eadLock( os2File
beee0 20 2a 70 46 69 6c 65 20 29 7b 0a 20 20 46 49 4c *pFile ){. FIL
beef0 45 4c 4f 43 4b 20 20 4c 6f 63 6b 41 72 65 61 2c ELOCK LockArea,
bef00 0a 20 20 20 20 20 20 20 20 20 20 20 20 55 6e 6c . Unl
bef10 6f 63 6b 41 72 65 61 3b 0a 20 20 41 50 49 52 45 ockArea;. APIRE
bef20 54 20 72 65 73 3b 0a 20 20 6d 65 6d 73 65 74 28 T res;. memset(
bef30 26 4c 6f 63 6b 41 72 65 61 2c 20 30 2c 20 73 69 &LockArea, 0, si
bef40 7a 65 6f 66 28 4c 6f 63 6b 41 72 65 61 29 29 3b zeof(LockArea));
bef50 0a 20 20 6d 65 6d 73 65 74 28 26 55 6e 6c 6f 63 . memset(&Unloc
bef60 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 kArea, 0, sizeof
bef70 28 55 6e 6c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 (UnlockArea));.
bef80 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 LockArea.lOffse
bef90 74 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54 t = SHARED_FIRST
befa0 3b 0a 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 ;. LockArea.lRa
befb0 6e 67 65 20 3d 20 53 48 41 52 45 44 5f 53 49 5a nge = SHARED_SIZ
befc0 45 3b 0a 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e E;. UnlockArea.
befd0 6c 4f 66 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 lOffset = 0L;.
befe0 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 UnlockArea.lRang
beff0 65 20 3d 20 30 4c 3b 0a 20 20 72 65 73 20 3d 20 e = 0L;. res =
bf000 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 DosSetFileLocks(
bf010 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f pFile->h, &Unlo
bf020 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 ckArea, &LockAre
bf030 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c a, LOCK_TIMEOUT,
bf040 20 31 4c 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 1L );. OSTRACE
bf050 33 28 20 22 47 45 54 52 45 41 44 4c 4f 43 4b 20 3( "GETREADLOCK
bf060 25 64 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70 46 %d res=%d\n", pF
bf070 69 6c 65 2d 3e 68 2c 20 72 65 73 20 29 3b 0a 20 ile->h, res );.
bf080 20 72 65 74 75 72 6e 20 72 65 73 3b 0a 7d 0a 0a return res;.}..
bf090 2f 2a 0a 2a 2a 20 55 6e 64 6f 20 61 20 72 65 61 /*.** Undo a rea
bf0a0 64 6c 6f 63 6b 0a 2a 2f 0a 73 74 61 74 69 63 20 dlock.*/.static
bf0b0 69 6e 74 20 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f int unlockReadLo
bf0c0 63 6b 28 20 6f 73 32 46 69 6c 65 20 2a 69 64 20 ck( os2File *id
bf0d0 29 7b 0a 20 20 46 49 4c 45 4c 4f 43 4b 20 20 4c ){. FILELOCK L
bf0e0 6f 63 6b 41 72 65 61 2c 0a 20 20 20 20 20 20 20 ockArea,.
bf0f0 20 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 3b UnlockArea;
bf100 0a 20 20 41 50 49 52 45 54 20 72 65 73 3b 0a 20 . APIRET res;.
bf110 20 6d 65 6d 73 65 74 28 26 4c 6f 63 6b 41 72 65 memset(&LockAre
bf120 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 4c 6f 63 a, 0, sizeof(Loc
bf130 6b 41 72 65 61 29 29 3b 0a 20 20 6d 65 6d 73 65 kArea));. memse
bf140 74 28 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 30 t(&UnlockArea, 0
bf150 2c 20 73 69 7a 65 6f 66 28 55 6e 6c 6f 63 6b 41 , sizeof(UnlockA
bf160 72 65 61 29 29 3b 0a 20 20 4c 6f 63 6b 41 72 65 rea));. LockAre
bf170 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c 3b 0a a.lOffset = 0L;.
bf180 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 LockArea.lRang
bf190 65 20 3d 20 30 4c 3b 0a 20 20 55 6e 6c 6f 63 6b e = 0L;. Unlock
bf1a0 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 53 Area.lOffset = S
bf1b0 48 41 52 45 44 5f 46 49 52 53 54 3b 0a 20 20 55 HARED_FIRST;. U
bf1c0 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 nlockArea.lRange
bf1d0 20 3d 20 53 48 41 52 45 44 5f 53 49 5a 45 3b 0a = SHARED_SIZE;.
bf1e0 20 20 72 65 73 20 3d 20 44 6f 73 53 65 74 46 69 res = DosSetFi
bf1f0 6c 65 4c 6f 63 6b 73 28 20 69 64 2d 3e 68 2c 20 leLocks( id->h,
bf200 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f &UnlockArea, &Lo
bf210 63 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d ckArea, LOCK_TIM
bf220 45 4f 55 54 2c 20 31 4c 20 29 3b 0a 20 20 4f 53 EOUT, 1L );. OS
bf230 54 52 41 43 45 33 28 20 22 55 4e 4c 4f 43 4b 2d TRACE3( "UNLOCK-
bf240 52 45 41 44 4c 4f 43 4b 20 66 69 6c 65 20 68 61 READLOCK file ha
bf250 6e 64 6c 65 3d 25 64 20 72 65 73 3d 25 64 3f 5c ndle=%d res=%d?\
bf260 6e 22 2c 20 69 64 2d 3e 68 2c 20 72 65 73 20 29 n", id->h, res )
bf270 3b 0a 20 20 72 65 74 75 72 6e 20 72 65 73 3b 0a ;. return res;.
bf280 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 }../*.** Lock th
bf290 65 20 66 69 6c 65 20 77 69 74 68 20 74 68 65 20 e file with the
bf2a0 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 64 20 62 lock specified b
bf2b0 79 20 70 61 72 61 6d 65 74 65 72 20 6c 6f 63 6b y parameter lock
bf2c0 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 type - one.** of
bf2d0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a the following:.
bf2e0 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 53 48 **.** (1) SH
bf2f0 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 ARED_LOCK.**
bf300 20 28 32 29 20 52 45 53 45 52 56 45 44 5f 4c 4f (2) RESERVED_LO
bf310 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 50 45 CK.** (3) PE
bf320 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 NDING_LOCK.**
bf330 20 20 28 34 29 20 45 58 43 4c 55 53 49 56 45 5f (4) EXCLUSIVE_
bf340 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 LOCK.**.** Somet
bf350 69 6d 65 73 20 77 68 65 6e 20 72 65 71 75 65 73 imes when reques
bf360 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 74 ting one lock st
bf370 61 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c 20 ate, additional
bf380 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20 61 lock states.** a
bf390 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e 20 62 re inserted in b
bf3a0 65 74 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f 63 etween. The loc
bf3b0 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 69 6c 20 king might fail
bf3c0 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c 61 on one of the la
bf3d0 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f ter.** transitio
bf3e0 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 65 20 6c ns leaving the l
bf3f0 6f 63 6b 20 73 74 61 74 65 20 64 69 66 66 65 72 ock state differ
bf400 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 69 74 ent from what it
bf410 20 73 74 61 72 74 65 64 20 62 75 74 0a 2a 2a 20 started but.**
bf420 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20 69 still short of i
bf430 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 66 6f ts goal. The fo
bf440 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 73 68 llowing chart sh
bf450 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 64 0a ows the allowed.
bf460 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 61 ** transitions a
bf470 6e 64 20 74 68 65 20 69 6e 73 65 72 74 65 64 20 nd the inserted
bf480 69 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74 61 intermediate sta
bf490 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e tes:.**.** UN
bf4a0 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 45 44 LOCKED -> SHARED
bf4b0 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e .** SHARED ->
bf4c0 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 20 20 RESERVED.**
bf4d0 53 48 41 52 45 44 20 2d 3e 20 28 50 45 4e 44 49 SHARED -> (PENDI
bf4e0 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 NG) -> EXCLUSIVE
bf4f0 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 45 44 20 .** RESERVED
bf500 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 -> (PENDING) ->
bf510 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 EXCLUSIVE.**
bf520 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c 55 PENDING -> EXCLU
bf530 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 SIVE.**.** This
bf540 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c routine will onl
bf550 79 20 69 6e 63 72 65 61 73 65 20 61 20 6c 6f 63 y increase a loc
bf560 6b 2e 20 20 54 68 65 20 6f 73 32 55 6e 6c 6f 63 k. The os2Unloc
bf570 6b 28 29 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 65 k() routine.** e
bf580 72 61 73 65 73 20 61 6c 6c 20 6c 6f 63 6b 73 20 rases all locks
bf590 61 74 20 6f 6e 63 65 20 61 6e 64 20 72 65 74 75 at once and retu
bf5a0 72 6e 73 20 75 73 20 69 6d 6d 65 64 69 61 74 65 rns us immediate
bf5b0 6c 79 20 74 6f 20 6c 6f 63 6b 69 6e 67 20 6c 65 ly to locking le
bf5c0 76 65 6c 20 30 2e 0a 2a 2a 20 49 74 20 69 73 20 vel 0..** It is
bf5d0 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 not possible to
bf5e0 6c 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e lower the lockin
bf5f0 67 20 6c 65 76 65 6c 20 6f 6e 65 20 73 74 65 70 g level one step
bf600 20 61 74 20 61 20 74 69 6d 65 2e 20 20 59 6f 75 at a time. You
bf610 0a 2a 2a 20 6d 75 73 74 20 67 6f 20 73 74 72 61 .** must go stra
bf620 69 67 68 74 20 74 6f 20 6c 6f 63 6b 69 6e 67 20 ight to locking
bf630 6c 65 76 65 6c 20 30 2e 0a 2a 2f 0a 73 74 61 74 level 0..*/.stat
bf640 69 63 20 69 6e 74 20 6f 73 32 4c 6f 63 6b 28 20 ic int os2Lock(
bf650 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
bf660 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 20 29 , int locktype )
bf670 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c {. int rc = SQL
bf680 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 2f 2a ITE_OK; /*
bf690 20 52 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f Return code fro
bf6a0 6d 20 73 75 62 72 6f 75 74 69 6e 65 73 20 2a 2f m subroutines */
bf6b0 0a 20 20 41 50 49 52 45 54 20 72 65 73 20 3d 20 . APIRET res =
bf6c0 4e 4f 5f 45 52 52 4f 52 3b 20 20 20 20 2f 2a 20 NO_ERROR; /*
bf6d0 52 65 73 75 6c 74 20 6f 66 20 61 6e 20 4f 53 2f Result of an OS/
bf6e0 32 20 6c 6f 63 6b 20 63 61 6c 6c 20 2a 2f 0a 20 2 lock call */.
bf6f0 20 69 6e 74 20 6e 65 77 4c 6f 63 6b 74 79 70 65 int newLocktype
bf700 3b 20 20 20 20 20 20 20 2f 2a 20 53 65 74 20 70 ; /* Set p
bf710 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 74 File->locktype t
bf720 6f 20 74 68 69 73 20 76 61 6c 75 65 20 62 65 66 o this value bef
bf730 6f 72 65 20 65 78 69 74 69 6e 67 20 2a 2f 0a 20 ore exiting */.
bf740 20 69 6e 74 20 67 6f 74 50 65 6e 64 69 6e 67 4c int gotPendingL
bf750 6f 63 6b 20 3d 20 30 3b 2f 2a 20 54 72 75 65 20 ock = 0;/* True
bf760 69 66 20 77 65 20 61 63 71 75 69 72 65 64 20 61 if we acquired a
bf770 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 74 68 PENDING lock th
bf780 69 73 20 74 69 6d 65 20 2a 2f 0a 20 20 46 49 4c is time */. FIL
bf790 45 4c 4f 43 4b 20 20 4c 6f 63 6b 41 72 65 61 2c ELOCK LockArea,
bf7a0 0a 20 20 20 20 20 20 20 20 20 20 20 20 55 6e 6c . Unl
bf7b0 6f 63 6b 41 72 65 61 3b 0a 20 20 6f 73 32 46 69 ockArea;. os2Fi
bf7c0 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 6f 73 32 le *pFile = (os2
bf7d0 46 69 6c 65 2a 29 69 64 3b 0a 20 20 6d 65 6d 73 File*)id;. mems
bf7e0 65 74 28 26 4c 6f 63 6b 41 72 65 61 2c 20 30 2c et(&LockArea, 0,
bf7f0 20 73 69 7a 65 6f 66 28 4c 6f 63 6b 41 72 65 61 sizeof(LockArea
bf800 29 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26 55 6e ));. memset(&Un
bf810 6c 6f 63 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a lockArea, 0, siz
bf820 65 6f 66 28 55 6e 6c 6f 63 6b 41 72 65 61 29 29 eof(UnlockArea))
bf830 3b 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c ;. assert( pFil
bf840 65 21 3d 30 20 29 3b 0a 20 20 4f 53 54 52 41 43 e!=0 );. OSTRAC
bf850 45 34 28 20 22 4c 4f 43 4b 20 25 64 20 25 64 20 E4( "LOCK %d %d
bf860 77 61 73 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 was %d\n", pFile
bf870 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c 20 70 ->h, locktype, p
bf880 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 File->locktype )
bf890 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 ;.. /* If there
bf8a0 20 69 73 20 61 6c 72 65 61 64 79 20 61 20 6c 6f is already a lo
bf8b0 63 6b 20 6f 66 20 74 68 69 73 20 74 79 70 65 20 ck of this type
bf8c0 6f 72 20 6d 6f 72 65 20 72 65 73 74 72 69 63 74 or more restrict
bf8d0 69 76 65 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 ive on the. **
bf8e0 6f 73 32 46 69 6c 65 2c 20 64 6f 20 6e 6f 74 68 os2File, do noth
bf8f0 69 6e 67 2e 20 44 6f 6e 27 74 20 75 73 65 20 74 ing. Don't use t
bf900 68 65 20 65 6e 64 5f 6c 6f 63 6b 3a 20 65 78 69 he end_lock: exi
bf910 74 20 70 61 74 68 2c 20 61 73 0a 20 20 2a 2a 20 t path, as. **
bf920 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e sqlite3_mutex_en
bf930 74 65 72 28 29 20 68 61 73 6e 27 74 20 62 65 65 ter() hasn't bee
bf940 6e 20 63 61 6c 6c 65 64 20 79 65 74 2e 0a 20 20 n called yet..
bf950 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e */. if( pFile->
bf960 6c 6f 63 6b 74 79 70 65 3e 3d 6c 6f 63 6b 74 79 locktype>=lockty
bf970 70 65 20 29 7b 0a 20 20 20 20 4f 53 54 52 41 43 pe ){. OSTRAC
bf980 45 33 28 20 22 4c 4f 43 4b 20 25 64 20 25 64 20 E3( "LOCK %d %d
bf990 6f 6b 20 28 61 6c 72 65 61 64 79 20 68 65 6c 64 ok (already held
bf9a0 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 )\n", pFile->h,
bf9b0 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 20 20 locktype );.
bf9c0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
bf9d0 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 ;. }.. /* Make
bf9e0 20 73 75 72 65 20 74 68 65 20 6c 6f 63 6b 69 6e sure the lockin
bf9f0 67 20 73 65 71 75 65 6e 63 65 20 69 73 20 63 6f g sequence is co
bfa00 72 72 65 63 74 0a 20 20 2a 2f 0a 20 20 61 73 73 rrect. */. ass
bfa10 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b ert( pFile->lock
bfa20 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 7c 7c type!=NO_LOCK ||
bfa30 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 locktype==SHARE
bfa40 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 D_LOCK );. asse
bfa50 72 74 28 20 6c 6f 63 6b 74 79 70 65 21 3d 50 45 rt( locktype!=PE
bfa60 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 3b 0a 20 20 NDING_LOCK );.
bfa70 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 assert( locktype
bfa80 21 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 !=RESERVED_LOCK
bfa90 7c 7c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 || pFile->lockty
bfaa0 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 pe==SHARED_LOCK
bfab0 29 3b 0a 0a 20 20 2f 2a 20 4c 6f 63 6b 20 74 68 );.. /* Lock th
bfac0 65 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 62 e PENDING_LOCK b
bfad0 79 74 65 20 69 66 20 77 65 20 6e 65 65 64 20 74 yte if we need t
bfae0 6f 20 61 63 71 75 69 72 65 20 61 20 50 45 4e 44 o acquire a PEND
bfaf0 49 4e 47 20 6c 6f 63 6b 20 6f 72 0a 20 20 2a 2a ING lock or. **
bfb00 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 2e 20 a SHARED lock.
bfb10 20 49 66 20 77 65 20 61 72 65 20 61 63 71 75 69 If we are acqui
bfb20 72 69 6e 67 20 61 20 53 48 41 52 45 44 20 6c 6f ring a SHARED lo
bfb30 63 6b 2c 20 74 68 65 20 61 63 71 75 69 73 69 74 ck, the acquisit
bfb40 69 6f 6e 20 6f 66 0a 20 20 2a 2a 20 74 68 65 20 ion of. ** the
bfb50 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 62 79 74 PENDING_LOCK byt
bfb60 65 20 69 73 20 74 65 6d 70 6f 72 61 72 79 2e 0a e is temporary..
bfb70 20 20 2a 2f 0a 20 20 6e 65 77 4c 6f 63 6b 74 79 */. newLockty
bfb80 70 65 20 3d 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b pe = pFile->lock
bfb90 74 79 70 65 3b 0a 20 20 69 66 28 20 70 46 69 6c type;. if( pFil
bfba0 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f e->locktype==NO_
bfbb0 4c 4f 43 4b 0a 20 20 20 20 20 20 7c 7c 20 28 6c LOCK. || (l
bfbc0 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 ocktype==EXCLUSI
bfbd0 56 45 5f 4c 4f 43 4b 20 26 26 20 70 46 69 6c 65 VE_LOCK && pFile
bfbe0 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 52 45 53 45 ->locktype==RESE
bfbf0 52 56 45 44 5f 4c 4f 43 4b 29 0a 20 20 29 7b 0a RVED_LOCK). ){.
bfc00 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 LockArea.lOf
bfc10 66 73 65 74 20 3d 20 50 45 4e 44 49 4e 47 5f 42 fset = PENDING_B
bfc20 59 54 45 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 YTE;. LockAre
bfc30 61 2e 6c 52 61 6e 67 65 20 3d 20 31 4c 3b 0a 20 a.lRange = 1L;.
bfc40 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f UnlockArea.lO
bfc50 66 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 ffset = 0L;.
bfc60 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 UnlockArea.lRang
bfc70 65 20 3d 20 30 4c 3b 0a 0a 20 20 20 20 2f 2a 20 e = 0L;.. /*
bfc80 77 61 69 74 20 6c 6f 6e 67 65 72 20 74 68 61 6e wait longer than
bfc90 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 20 68 65 LOCK_TIMEOUT he
bfca0 72 65 20 6e 6f 74 20 74 6f 20 68 61 76 65 20 74 re not to have t
bfcb0 6f 20 74 72 79 20 6d 75 6c 74 69 70 6c 65 20 74 o try multiple t
bfcc0 69 6d 65 73 20 2a 2f 0a 20 20 20 20 72 65 73 20 imes */. res
bfcd0 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b = DosSetFileLock
bfce0 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e s( pFile->h, &Un
bfcf0 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 lockArea, &LockA
bfd00 72 65 61 2c 20 31 30 30 4c 2c 20 30 4c 20 29 3b rea, 100L, 0L );
bfd10 0a 20 20 20 20 69 66 28 20 72 65 73 20 3d 3d 20 . if( res ==
bfd20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 NO_ERROR ){.
bfd30 20 20 67 6f 74 50 65 6e 64 69 6e 67 4c 6f 63 6b gotPendingLock
bfd40 20 3d 20 31 3b 0a 20 20 20 20 20 20 4f 53 54 52 = 1;. OSTR
bfd50 41 43 45 33 28 20 22 4c 4f 43 4b 20 25 64 20 70 ACE3( "LOCK %d p
bfd60 65 6e 64 69 6e 67 20 6c 6f 63 6b 20 62 6f 6f 6c ending lock bool
bfd70 65 61 6e 20 73 65 74 2e 20 20 72 65 73 3d 25 64 ean set. res=%d
bfd80 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 \n", pFile->h, r
bfd90 65 73 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a es );. }. }.
bfda0 0a 20 20 2f 2a 20 41 63 71 75 69 72 65 20 61 20 . /* Acquire a
bfdb0 73 68 61 72 65 64 20 6c 6f 63 6b 0a 20 20 2a 2f shared lock. */
bfdc0 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d . if( locktype=
bfdd0 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 26 26 20 =SHARED_LOCK &&
bfde0 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 res == NO_ERROR
bfdf0 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 ){. assert( p
bfe00 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d File->locktype==
bfe10 4e 4f 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 72 NO_LOCK );. r
bfe20 65 73 20 3d 20 67 65 74 52 65 61 64 4c 6f 63 6b es = getReadLock
bfe30 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 69 66 28 (pFile);. if(
bfe40 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 res == NO_ERROR
bfe50 20 29 7b 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 ){. newLoc
bfe60 6b 74 79 70 65 20 3d 20 53 48 41 52 45 44 5f 4c ktype = SHARED_L
bfe70 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 4f OCK;. }. O
bfe80 53 54 52 41 43 45 33 28 20 22 4c 4f 43 4b 20 25 STRACE3( "LOCK %
bfe90 64 20 61 63 71 75 69 72 65 20 73 68 61 72 65 64 d acquire shared
bfea0 20 6c 6f 63 6b 2e 20 72 65 73 3d 25 64 5c 6e 22 lock. res=%d\n"
bfeb0 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20 , pFile->h, res
bfec0 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 );. }.. /* Acq
bfed0 75 69 72 65 20 61 20 52 45 53 45 52 56 45 44 20 uire a RESERVED
bfee0 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 lock. */. if(
bfef0 6c 6f 63 6b 74 79 70 65 3d 3d 52 45 53 45 52 56 locktype==RESERV
bff00 45 44 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 3d ED_LOCK && res =
bff10 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 = NO_ERROR ){.
bff20 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d assert( pFile-
bff30 3e 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 >locktype==SHARE
bff40 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 4c 6f D_LOCK );. Lo
bff50 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d ckArea.lOffset =
bff60 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 3b 0a RESERVED_BYTE;.
bff70 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 LockArea.lRa
bff80 6e 67 65 20 3d 20 31 4c 3b 0a 20 20 20 20 55 6e nge = 1L;. Un
bff90 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 lockArea.lOffset
bffa0 20 3d 20 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 = 0L;. Unloc
bffb0 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 kArea.lRange = 0
bffc0 4c 3b 0a 20 20 20 20 72 65 73 20 3d 20 44 6f 73 L;. res = Dos
bffd0 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70 46 SetFileLocks( pF
bffe0 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41 ile->h, &UnlockA
bfff0 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20 rea, &LockArea,
c0000 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 30 4c LOCK_TIMEOUT, 0L
c0010 20 29 3b 0a 20 20 20 20 69 66 28 20 72 65 73 20 );. if( res
c0020 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 == NO_ERROR ){.
c0030 20 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65 newLocktype
c0040 20 3d 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b = RESERVED_LOCK
c0050 3b 0a 20 20 20 20 7d 0a 20 20 20 20 4f 53 54 52 ;. }. OSTR
c0060 41 43 45 33 28 20 22 4c 4f 43 4b 20 25 64 20 61 ACE3( "LOCK %d a
c0070 63 71 75 69 72 65 20 72 65 73 65 72 76 65 64 20 cquire reserved
c0080 6c 6f 63 6b 2e 20 72 65 73 3d 25 64 5c 6e 22 2c lock. res=%d\n",
c0090 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20 29 pFile->h, res )
c00a0 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 ;. }.. /* Acqu
c00b0 69 72 65 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f ire a PENDING lo
c00c0 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f ck. */. if( lo
c00d0 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 cktype==EXCLUSIV
c00e0 45 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 3d 3d E_LOCK && res ==
c00f0 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 NO_ERROR ){.
c0100 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20 3d 20 50 newLocktype = P
c0110 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3b 0a 20 20 20 ENDING_LOCK;.
c0120 20 67 6f 74 50 65 6e 64 69 6e 67 4c 6f 63 6b 20 gotPendingLock
c0130 3d 20 30 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 = 0;. OSTRACE
c0140 32 28 20 22 4c 4f 43 4b 20 25 64 20 61 63 71 75 2( "LOCK %d acqu
c0150 69 72 65 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b ire pending lock
c0160 2e 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 20 62 . pending lock b
c0170 6f 6f 6c 65 61 6e 20 75 6e 73 65 74 2e 5c 6e 22 oolean unset.\n"
c0180 2c 20 70 46 69 6c 65 2d 3e 68 20 29 3b 0a 20 20 , pFile->h );.
c0190 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72 65 20 }.. /* Acquire
c01a0 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 an EXCLUSIVE loc
c01b0 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 k. */. if( loc
c01c0 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 ktype==EXCLUSIVE
c01d0 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 3d 3d 20 _LOCK && res ==
c01e0 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 NO_ERROR ){.
c01f0 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c assert( pFile->l
c0200 6f 63 6b 74 79 70 65 3e 3d 53 48 41 52 45 44 5f ocktype>=SHARED_
c0210 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 72 65 73 20 LOCK );. res
c0220 3d 20 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f 63 6b = unlockReadLock
c0230 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 4f 53 54 (pFile);. OST
c0240 52 41 43 45 32 28 20 22 75 6e 72 65 61 64 6c 6f RACE2( "unreadlo
c0250 63 6b 20 3d 20 25 64 5c 6e 22 2c 20 72 65 73 20 ck = %d\n", res
c0260 29 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e );. LockArea.
c0270 6c 4f 66 66 73 65 74 20 3d 20 53 48 41 52 45 44 lOffset = SHARED
c0280 5f 46 49 52 53 54 3b 0a 20 20 20 20 4c 6f 63 6b _FIRST;. Lock
c0290 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 53 48 Area.lRange = SH
c02a0 41 52 45 44 5f 53 49 5a 45 3b 0a 20 20 20 20 55 ARED_SIZE;. U
c02b0 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 nlockArea.lOffse
c02c0 74 20 3d 20 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f t = 0L;. Unlo
c02d0 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 ckArea.lRange =
c02e0 30 4c 3b 0a 20 20 20 20 72 65 73 20 3d 20 44 6f 0L;. res = Do
c02f0 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70 sSetFileLocks( p
c0300 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b File->h, &Unlock
c0310 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c Area, &LockArea,
c0320 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 30 LOCK_TIMEOUT, 0
c0330 4c 20 29 3b 0a 20 20 20 20 69 66 28 20 72 65 73 L );. if( res
c0340 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a == NO_ERROR ){.
c0350 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79 70 newLocktyp
c0360 65 20 3d 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f e = EXCLUSIVE_LO
c0370 43 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 CK;. }else{.
c0380 20 20 20 20 20 4f 53 54 52 41 43 45 32 28 20 22 OSTRACE2( "
c0390 4f 53 2f 32 20 65 72 72 6f 72 2d 63 6f 64 65 20 OS/2 error-code
c03a0 3d 20 25 64 5c 6e 22 2c 20 72 65 73 20 29 3b 0a = %d\n", res );.
c03b0 20 20 20 20 20 20 67 65 74 52 65 61 64 4c 6f 63 getReadLoc
c03c0 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 7d 0a k(pFile);. }.
c03d0 20 20 20 20 4f 53 54 52 41 43 45 33 28 20 22 4c OSTRACE3( "L
c03e0 4f 43 4b 20 25 64 20 61 63 71 75 69 72 65 20 65 OCK %d acquire e
c03f0 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 2e 20 20 xclusive lock.
c0400 72 65 73 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 res=%d\n", pFile
c0410 2d 3e 68 2c 20 72 65 73 20 29 3b 0a 20 20 7d 0a ->h, res );. }.
c0420 0a 20 20 2f 2a 20 49 66 20 77 65 20 61 72 65 20 . /* If we are
c0430 68 6f 6c 64 69 6e 67 20 61 20 50 45 4e 44 49 4e holding a PENDIN
c0440 47 20 6c 6f 63 6b 20 74 68 61 74 20 6f 75 67 68 G lock that ough
c0450 74 20 74 6f 20 62 65 20 72 65 6c 65 61 73 65 64 t to be released
c0460 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 72 65 6c 65 , then. ** rele
c0470 61 73 65 20 69 74 20 6e 6f 77 2e 0a 20 20 2a 2f ase it now.. */
c0480 0a 20 20 69 66 28 20 67 6f 74 50 65 6e 64 69 6e . if( gotPendin
c0490 67 4c 6f 63 6b 20 26 26 20 6c 6f 63 6b 74 79 70 gLock && locktyp
c04a0 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 e==SHARED_LOCK )
c04b0 7b 0a 20 20 20 20 69 6e 74 20 72 3b 0a 20 20 20 {. int r;.
c04c0 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 LockArea.lOffse
c04d0 74 20 3d 20 30 4c 3b 0a 20 20 20 20 4c 6f 63 6b t = 0L;. Lock
c04e0 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c Area.lRange = 0L
c04f0 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 ;. UnlockArea
c0500 2e 6c 4f 66 66 73 65 74 20 3d 20 50 45 4e 44 49 .lOffset = PENDI
c0510 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20 55 6e 6c NG_BYTE;. Unl
c0520 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d ockArea.lRange =
c0530 20 31 4c 3b 0a 20 20 20 20 72 20 3d 20 44 6f 73 1L;. r = Dos
c0540 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70 46 SetFileLocks( pF
c0550 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41 ile->h, &UnlockA
c0560 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20 rea, &LockArea,
c0570 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 30 4c LOCK_TIMEOUT, 0L
c0580 20 29 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 33 );. OSTRACE3
c0590 28 20 22 4c 4f 43 4b 20 25 64 20 75 6e 6c 6f 63 ( "LOCK %d unloc
c05a0 6b 69 6e 67 20 70 65 6e 64 69 6e 67 2f 69 73 20 king pending/is
c05b0 73 68 61 72 65 64 2e 20 72 3d 25 64 5c 6e 22 2c shared. r=%d\n",
c05c0 20 70 46 69 6c 65 2d 3e 68 2c 20 72 20 29 3b 0a pFile->h, r );.
c05d0 20 20 7d 0a 0a 20 20 2f 2a 20 55 70 64 61 74 65 }.. /* Update
c05e0 20 74 68 65 20 73 74 61 74 65 20 6f 66 20 74 68 the state of th
c05f0 65 20 6c 6f 63 6b 20 68 61 73 20 68 65 6c 64 20 e lock has held
c0600 69 6e 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 in the file desc
c0610 72 69 70 74 6f 72 20 74 68 65 6e 0a 20 20 2a 2a riptor then. **
c0620 20 72 65 74 75 72 6e 20 74 68 65 20 61 70 70 72 return the appr
c0630 6f 70 72 69 61 74 65 20 72 65 73 75 6c 74 20 63 opriate result c
c0640 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 ode.. */. if(
c0650 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 res == NO_ERROR
c0660 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 ){. rc = SQLI
c0670 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a TE_OK;. }else{.
c0680 20 20 20 20 4f 53 54 52 41 43 45 34 28 20 22 4c OSTRACE4( "L
c0690 4f 43 4b 20 46 41 49 4c 45 44 20 25 64 20 74 72 OCK FAILED %d tr
c06a0 79 69 6e 67 20 66 6f 72 20 25 64 20 62 75 74 20 ying for %d but
c06b0 67 6f 74 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 got %d\n", pFile
c06c0 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20 ->h,.
c06d0 20 20 20 6c 6f 63 6b 74 79 70 65 2c 20 6e 65 77 locktype, new
c06e0 4c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 20 20 Locktype );.
c06f0 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 rc = SQLITE_BUSY
c0700 3b 0a 20 20 7d 0a 20 20 70 46 69 6c 65 2d 3e 6c ;. }. pFile->l
c0710 6f 63 6b 74 79 70 65 20 3d 20 6e 65 77 4c 6f 63 ocktype = newLoc
c0720 6b 74 79 70 65 3b 0a 20 20 4f 53 54 52 41 43 45 ktype;. OSTRACE
c0730 33 28 20 22 4c 4f 43 4b 20 25 64 20 6e 6f 77 20 3( "LOCK %d now
c0740 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c %d\n", pFile->h,
c0750 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
c0760 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b );. return rc;
c0770 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 .}../*.** This r
c0780 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20 69 66 outine checks if
c0790 20 74 68 65 72 65 20 69 73 20 61 20 52 45 53 45 there is a RESE
c07a0 52 56 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20 6f RVED lock held o
c07b0 6e 20 74 68 65 20 73 70 65 63 69 66 69 65 64 0a n the specified.
c07c0 2a 2a 20 66 69 6c 65 20 62 79 20 74 68 69 73 20 ** file by this
c07d0 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 70 72 6f or any other pro
c07e0 63 65 73 73 2e 20 49 66 20 73 75 63 68 20 61 20 cess. If such a
c07f0 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20 72 65 lock is held, re
c0800 74 75 72 6e 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f turn.** non-zero
c0810 2c 20 6f 74 68 65 72 77 69 73 65 20 7a 65 72 6f , otherwise zero
c0820 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
c0830 6f 73 32 43 68 65 63 6b 52 65 73 65 72 76 65 64 os2CheckReserved
c0840 4c 6f 63 6b 28 20 73 71 6c 69 74 65 33 5f 66 69 Lock( sqlite3_fi
c0850 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 4f 75 le *id, int *pOu
c0860 74 20 29 7b 0a 20 20 69 6e 74 20 72 20 3d 20 30 t ){. int r = 0
c0870 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70 46 69 ;. os2File *pFi
c0880 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29 69 le = (os2File*)i
c0890 64 3b 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 d;. assert( pFi
c08a0 6c 65 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 le!=0 );. if( p
c08b0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d File->locktype>=
c08c0 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 29 7b RESERVED_LOCK ){
c08d0 0a 20 20 20 20 72 20 3d 20 31 3b 0a 20 20 20 20 . r = 1;.
c08e0 4f 53 54 52 41 43 45 33 28 20 22 54 45 53 54 20 OSTRACE3( "TEST
c08f0 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20 28 6c WR-LOCK %d %d (l
c0900 6f 63 61 6c 29 5c 6e 22 2c 20 70 46 69 6c 65 2d ocal)\n", pFile-
c0910 3e 68 2c 20 72 20 29 3b 0a 20 20 7d 65 6c 73 65 >h, r );. }else
c0920 7b 0a 20 20 20 20 46 49 4c 45 4c 4f 43 4b 20 20 {. FILELOCK
c0930 4c 6f 63 6b 41 72 65 61 2c 0a 20 20 20 20 20 20 LockArea,.
c0940 20 20 20 20 20 20 20 20 55 6e 6c 6f 63 6b 41 72 UnlockAr
c0950 65 61 3b 0a 20 20 20 20 41 50 49 52 45 54 20 72 ea;. APIRET r
c0960 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 c = NO_ERROR;.
c0970 20 20 6d 65 6d 73 65 74 28 26 4c 6f 63 6b 41 72 memset(&LockAr
c0980 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 4c 6f ea, 0, sizeof(Lo
c0990 63 6b 41 72 65 61 29 29 3b 0a 20 20 20 20 6d 65 ckArea));. me
c09a0 6d 73 65 74 28 26 55 6e 6c 6f 63 6b 41 72 65 61 mset(&UnlockArea
c09b0 2c 20 30 2c 20 73 69 7a 65 6f 66 28 55 6e 6c 6f , 0, sizeof(Unlo
c09c0 63 6b 41 72 65 61 29 29 3b 0a 20 20 20 20 4c 6f ckArea));. Lo
c09d0 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d ckArea.lOffset =
c09e0 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 3b 0a RESERVED_BYTE;.
c09f0 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 LockArea.lRa
c0a00 6e 67 65 20 3d 20 31 4c 3b 0a 20 20 20 20 55 6e nge = 1L;. Un
c0a10 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 lockArea.lOffset
c0a20 20 3d 20 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 = 0L;. Unloc
c0a30 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 kArea.lRange = 0
c0a40 4c 3b 0a 20 20 20 20 72 63 20 3d 20 44 6f 73 53 L;. rc = DosS
c0a50 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70 46 69 etFileLocks( pFi
c0a60 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41 72 le->h, &UnlockAr
c0a70 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20 4c ea, &LockArea, L
c0a80 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 30 4c 20 OCK_TIMEOUT, 0L
c0a90 29 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28 );. OSTRACE3(
c0aa0 20 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 "TEST WR-LOCK %
c0ab0 64 20 6c 6f 63 6b 20 72 65 73 65 72 76 65 64 20 d lock reserved
c0ac0 62 79 74 65 20 72 63 3d 25 64 5c 6e 22 2c 20 70 byte rc=%d\n", p
c0ad0 46 69 6c 65 2d 3e 68 2c 20 72 63 20 29 3b 0a 20 File->h, rc );.
c0ae0 20 20 20 69 66 28 20 72 63 20 3d 3d 20 4e 4f 5f if( rc == NO_
c0af0 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 20 20 41 ERROR ){. A
c0b00 50 49 52 45 54 20 72 63 75 20 3d 20 4e 4f 5f 45 PIRET rcu = NO_E
c0b10 52 52 4f 52 3b 20 2f 2a 20 72 65 74 75 72 6e 20 RROR; /* return
c0b20 63 6f 64 65 20 66 6f 72 20 75 6e 6c 6f 63 6b 69 code for unlocki
c0b30 6e 67 20 2a 2f 0a 20 20 20 20 20 20 4c 6f 63 6b ng */. Lock
c0b40 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 Area.lOffset = 0
c0b50 4c 3b 0a 20 20 20 20 20 20 4c 6f 63 6b 41 72 65 L;. LockAre
c0b60 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 a.lRange = 0L;.
c0b70 20 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e UnlockArea.
c0b80 6c 4f 66 66 73 65 74 20 3d 20 52 45 53 45 52 56 lOffset = RESERV
c0b90 45 44 5f 42 59 54 45 3b 0a 20 20 20 20 20 20 55 ED_BYTE;. U
c0ba0 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 nlockArea.lRange
c0bb0 20 3d 20 31 4c 3b 0a 20 20 20 20 20 20 72 63 75 = 1L;. rcu
c0bc0 20 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 = DosSetFileLoc
c0bd0 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 ks( pFile->h, &U
c0be0 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b nlockArea, &Lock
c0bf0 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f Area, LOCK_TIMEO
c0c00 55 54 2c 20 30 4c 20 29 3b 0a 20 20 20 20 20 20 UT, 0L );.
c0c10 4f 53 54 52 41 43 45 33 28 20 22 54 45 53 54 20 OSTRACE3( "TEST
c0c20 57 52 2d 4c 4f 43 4b 20 25 64 20 75 6e 6c 6f 63 WR-LOCK %d unloc
c0c30 6b 20 72 65 73 65 72 76 65 64 20 62 79 74 65 20 k reserved byte
c0c40 72 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e r=%d\n", pFile->
c0c50 68 2c 20 72 63 75 20 29 3b 0a 20 20 20 20 7d 0a h, rcu );. }.
c0c60 20 20 20 20 72 20 3d 20 21 28 72 63 20 3d 3d 20 r = !(rc ==
c0c70 4e 4f 5f 45 52 52 4f 52 29 3b 0a 20 20 20 20 4f NO_ERROR);. O
c0c80 53 54 52 41 43 45 33 28 20 22 54 45 53 54 20 57 STRACE3( "TEST W
c0c90 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20 28 72 65 R-LOCK %d %d (re
c0ca0 6d 6f 74 65 29 5c 6e 22 2c 20 70 46 69 6c 65 2d mote)\n", pFile-
c0cb0 3e 68 2c 20 72 20 29 3b 0a 20 20 7d 0a 20 20 2a >h, r );. }. *
c0cc0 70 4f 75 74 20 3d 20 72 3b 0a 20 20 72 65 74 75 pOut = r;. retu
c0cd0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
c0ce0 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65 ./*.** Lower the
c0cf0 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f locking level o
c0d00 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f n file descripto
c0d10 72 20 69 64 20 74 6f 20 6c 6f 63 6b 74 79 70 65 r id to locktype
c0d20 2e 20 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d . locktype.** m
c0d30 75 73 74 20 62 65 20 65 69 74 68 65 72 20 4e 4f ust be either NO
c0d40 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45 44 5f _LOCK or SHARED_
c0d50 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 LOCK..**.** If t
c0d60 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c he locking level
c0d70 20 6f 66 20 74 68 65 20 66 69 6c 65 20 64 65 73 of the file des
c0d80 63 72 69 70 74 6f 72 20 69 73 20 61 6c 72 65 61 criptor is alrea
c0d90 64 79 20 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a dy at or below.*
c0da0 2a 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 * the requested
c0db0 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74 locking level, t
c0dc0 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 his routine is a
c0dd0 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 74 no-op..**.** It
c0de0 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 is not possible
c0df0 20 66 6f 72 20 74 68 69 73 20 72 6f 75 74 69 6e for this routin
c0e00 65 20 74 6f 20 66 61 69 6c 20 69 66 20 74 68 65 e to fail if the
c0e10 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 second argument
c0e20 0a 2a 2a 20 69 73 20 4e 4f 5f 4c 4f 43 4b 2e 20 .** is NO_LOCK.
c0e30 20 49 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 If the second a
c0e40 72 67 75 6d 65 6e 74 20 69 73 20 53 48 41 52 45 rgument is SHARE
c0e50 44 5f 4c 4f 43 4b 20 74 68 65 6e 20 74 68 69 73 D_LOCK then this
c0e60 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 6d 69 67 68 routine.** migh
c0e70 74 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f t return SQLITE_
c0e80 49 4f 45 52 52 3b 0a 2a 2f 0a 73 74 61 74 69 63 IOERR;.*/.static
c0e90 20 69 6e 74 20 6f 73 32 55 6e 6c 6f 63 6b 28 20 int os2Unlock(
c0ea0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
c0eb0 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 20 29 , int locktype )
c0ec0 7b 0a 20 20 69 6e 74 20 74 79 70 65 3b 0a 20 20 {. int type;.
c0ed0 6f 73 32 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d os2File *pFile =
c0ee0 20 28 6f 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 (os2File*)id;.
c0ef0 20 41 50 49 52 45 54 20 72 63 20 3d 20 53 51 4c APIRET rc = SQL
c0f00 49 54 45 5f 4f 4b 3b 0a 20 20 41 50 49 52 45 54 ITE_OK;. APIRET
c0f10 20 72 65 73 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b res = NO_ERROR;
c0f20 0a 20 20 46 49 4c 45 4c 4f 43 4b 20 20 4c 6f 63 . FILELOCK Loc
c0f30 6b 41 72 65 61 2c 0a 20 20 20 20 20 20 20 20 20 kArea,.
c0f40 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 3b 0a 20 UnlockArea;.
c0f50 20 6d 65 6d 73 65 74 28 26 4c 6f 63 6b 41 72 65 memset(&LockAre
c0f60 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 4c 6f 63 a, 0, sizeof(Loc
c0f70 6b 41 72 65 61 29 29 3b 0a 20 20 6d 65 6d 73 65 kArea));. memse
c0f80 74 28 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 30 t(&UnlockArea, 0
c0f90 2c 20 73 69 7a 65 6f 66 28 55 6e 6c 6f 63 6b 41 , sizeof(UnlockA
c0fa0 72 65 61 29 29 3b 0a 20 20 61 73 73 65 72 74 28 rea));. assert(
c0fb0 20 70 46 69 6c 65 21 3d 30 20 29 3b 0a 20 20 61 pFile!=0 );. a
c0fc0 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 3c ssert( locktype<
c0fd0 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a =SHARED_LOCK );.
c0fe0 20 20 4f 53 54 52 41 43 45 34 28 20 22 55 4e 4c OSTRACE4( "UNL
c0ff0 4f 43 4b 20 25 64 20 74 6f 20 25 64 20 77 61 73 OCK %d to %d was
c1000 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 %d\n", pFile->h
c1010 2c 20 6c 6f 63 6b 74 79 70 65 2c 20 70 46 69 6c , locktype, pFil
c1020 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 e->locktype );.
c1030 20 74 79 70 65 20 3d 20 70 46 69 6c 65 2d 3e 6c type = pFile->l
c1040 6f 63 6b 74 79 70 65 3b 0a 20 20 69 66 28 20 74 ocktype;. if( t
c1050 79 70 65 3e 3d 45 58 43 4c 55 53 49 56 45 5f 4c ype>=EXCLUSIVE_L
c1060 4f 43 4b 20 29 7b 0a 20 20 20 20 4c 6f 63 6b 41 OCK ){. LockA
c1070 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c rea.lOffset = 0L
c1080 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c ;. LockArea.l
c1090 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 20 20 Range = 0L;.
c10a0 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 UnlockArea.lOffs
c10b0 65 74 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53 et = SHARED_FIRS
c10c0 54 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 T;. UnlockAre
c10d0 61 2e 6c 52 61 6e 67 65 20 3d 20 53 48 41 52 45 a.lRange = SHARE
c10e0 44 5f 53 49 5a 45 3b 0a 20 20 20 20 72 65 73 20 D_SIZE;. res
c10f0 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b = DosSetFileLock
c1100 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e s( pFile->h, &Un
c1110 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 lockArea, &LockA
c1120 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 rea, LOCK_TIMEOU
c1130 54 2c 20 30 4c 20 29 3b 0a 20 20 20 20 4f 53 54 T, 0L );. OST
c1140 52 41 43 45 33 28 20 22 55 4e 4c 4f 43 4b 20 25 RACE3( "UNLOCK %
c1150 64 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b d exclusive lock
c1160 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70 46 69 6c res=%d\n", pFil
c1170 65 2d 3e 68 2c 20 72 65 73 20 29 3b 0a 20 20 20 e->h, res );.
c1180 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 if( locktype==S
c1190 48 41 52 45 44 5f 4c 4f 43 4b 20 26 26 20 67 65 HARED_LOCK && ge
c11a0 74 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c 65 29 tReadLock(pFile)
c11b0 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a != NO_ERROR ){.
c11c0 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 73 68 /* This sh
c11d0 6f 75 6c 64 20 6e 65 76 65 72 20 68 61 70 70 65 ould never happe
c11e0 6e 2e 20 20 57 65 20 73 68 6f 75 6c 64 20 61 6c n. We should al
c11f0 77 61 79 73 20 62 65 20 61 62 6c 65 20 74 6f 0a ways be able to.
c1200 20 20 20 20 20 20 2a 2a 20 72 65 61 63 71 75 69 ** reacqui
c1210 72 65 20 74 68 65 20 72 65 61 64 20 6c 6f 63 6b re the read lock
c1220 20 2a 2f 0a 20 20 20 20 20 20 4f 53 54 52 41 43 */. OSTRAC
c1230 45 33 28 20 22 55 4e 4c 4f 43 4b 20 25 64 20 74 E3( "UNLOCK %d t
c1240 6f 20 25 64 20 67 65 74 52 65 61 64 4c 6f 63 6b o %d getReadLock
c1250 28 29 20 66 61 69 6c 65 64 5c 6e 22 2c 20 70 46 () failed\n", pF
c1260 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 ile->h, locktype
c1270 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 );. rc = S
c1280 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f QLITE_IOERR_UNLO
c1290 43 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 CK;. }. }.
c12a0 69 66 28 20 74 79 70 65 3e 3d 52 45 53 45 52 56 if( type>=RESERV
c12b0 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 4c ED_LOCK ){. L
c12c0 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 ockArea.lOffset
c12d0 3d 20 30 4c 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 = 0L;. LockAr
c12e0 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0a ea.lRange = 0L;.
c12f0 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c UnlockArea.l
c1300 4f 66 66 73 65 74 20 3d 20 52 45 53 45 52 56 45 Offset = RESERVE
c1310 44 5f 42 59 54 45 3b 0a 20 20 20 20 55 6e 6c 6f D_BYTE;. Unlo
c1320 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 ckArea.lRange =
c1330 31 4c 3b 0a 20 20 20 20 72 65 73 20 3d 20 44 6f 1L;. res = Do
c1340 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70 sSetFileLocks( p
c1350 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b File->h, &Unlock
c1360 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c Area, &LockArea,
c1370 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 30 LOCK_TIMEOUT, 0
c1380 4c 20 29 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 L );. OSTRACE
c1390 33 28 20 22 55 4e 4c 4f 43 4b 20 25 64 20 72 65 3( "UNLOCK %d re
c13a0 73 65 72 76 65 64 20 72 65 73 3d 25 64 5c 6e 22 served res=%d\n"
c13b0 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20 , pFile->h, res
c13c0 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6c 6f 63 );. }. if( loc
c13d0 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 26 ktype==NO_LOCK &
c13e0 26 20 74 79 70 65 3e 3d 53 48 41 52 45 44 5f 4c & type>=SHARED_L
c13f0 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 73 20 3d OCK ){. res =
c1400 20 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f 63 6b 28 unlockReadLock(
c1410 70 46 69 6c 65 29 3b 0a 20 20 20 20 4f 53 54 52 pFile);. OSTR
c1420 41 43 45 35 28 20 22 55 4e 4c 4f 43 4b 20 25 64 ACE5( "UNLOCK %d
c1430 20 69 73 20 25 64 20 77 61 6e 74 20 25 64 20 72 is %d want %d r
c1440 65 73 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d es=%d\n", pFile-
c1450 3e 68 2c 20 74 79 70 65 2c 20 6c 6f 63 6b 74 79 >h, type, lockty
c1460 70 65 2c 20 72 65 73 20 29 3b 0a 20 20 7d 0a 20 pe, res );. }.
c1470 20 69 66 28 20 74 79 70 65 3e 3d 50 45 4e 44 49 if( type>=PENDI
c1480 4e 47 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 4c NG_LOCK ){. L
c1490 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 ockArea.lOffset
c14a0 3d 20 30 4c 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 = 0L;. LockAr
c14b0 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0a ea.lRange = 0L;.
c14c0 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c UnlockArea.l
c14d0 4f 66 66 73 65 74 20 3d 20 50 45 4e 44 49 4e 47 Offset = PENDING
c14e0 5f 42 59 54 45 3b 0a 20 20 20 20 55 6e 6c 6f 63 _BYTE;. Unloc
c14f0 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 31 kArea.lRange = 1
c1500 4c 3b 0a 20 20 20 20 72 65 73 20 3d 20 44 6f 73 L;. res = Dos
c1510 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70 46 SetFileLocks( pF
c1520 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41 ile->h, &UnlockA
c1530 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20 rea, &LockArea,
c1540 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 30 4c LOCK_TIMEOUT, 0L
c1550 20 29 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 33 );. OSTRACE3
c1560 28 20 22 55 4e 4c 4f 43 4b 20 25 64 20 70 65 6e ( "UNLOCK %d pen
c1570 64 69 6e 67 20 72 65 73 3d 25 64 5c 6e 22 2c 20 ding res=%d\n",
c1580 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20 29 3b pFile->h, res );
c1590 0a 20 20 7d 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f . }. pFile->lo
c15a0 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 cktype = locktyp
c15b0 65 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 20 22 e;. OSTRACE3( "
c15c0 55 4e 4c 4f 43 4b 20 25 64 20 6e 6f 77 20 25 64 UNLOCK %d now %d
c15d0 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 70 \n", pFile->h, p
c15e0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 File->locktype )
c15f0 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
c1600 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 74 72 6f 6c 20 ../*.** Control
c1610 61 6e 64 20 71 75 65 72 79 20 6f 66 20 74 68 65 and query of the
c1620 20 6f 70 65 6e 20 66 69 6c 65 20 68 61 6e 64 6c open file handl
c1630 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
c1640 20 6f 73 32 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 os2FileControl(
c1650 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
c1660 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69 64 20 2a , int op, void *
c1670 70 41 72 67 29 7b 0a 20 20 73 77 69 74 63 68 28 pArg){. switch(
c1680 20 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20 op ){. case
c1690 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43 SQLITE_FCNTL_LOC
c16a0 4b 53 54 41 54 45 3a 20 7b 0a 20 20 20 20 20 20 KSTATE: {.
c16b0 2a 28 69 6e 74 2a 29 70 41 72 67 20 3d 20 28 28 *(int*)pArg = ((
c16c0 6f 73 32 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 6f os2File*)id)->lo
c16d0 63 6b 74 79 70 65 3b 0a 20 20 20 20 20 20 4f 53 cktype;. OS
c16e0 54 52 41 43 45 33 28 20 22 46 43 4e 54 4c 5f 4c TRACE3( "FCNTL_L
c16f0 4f 43 4b 53 54 41 54 45 20 25 64 20 6c 6f 63 6b OCKSTATE %d lock
c1700 3d 25 64 5c 6e 22 2c 20 28 28 6f 73 32 46 69 6c =%d\n", ((os2Fil
c1710 65 2a 29 69 64 29 2d 3e 68 2c 20 28 28 6f 73 32 e*)id)->h, ((os2
c1720 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 6f 63 6b 74 File*)id)->lockt
c1730 79 70 65 20 29 3b 0a 20 20 20 20 20 20 72 65 74 ype );. ret
c1740 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
c1750 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
c1760 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a n SQLITE_ERROR;.
c1770 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
c1780 74 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20 the sector size
c1790 69 6e 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 in bytes of the
c17a0 75 6e 64 65 72 6c 79 69 6e 67 20 62 6c 6f 63 6b underlying block
c17b0 20 64 65 76 69 63 65 20 66 6f 72 0a 2a 2a 20 74 device for.** t
c17c0 68 65 20 73 70 65 63 69 66 69 65 64 20 66 69 6c he specified fil
c17d0 65 2e 20 54 68 69 73 20 69 73 20 61 6c 6d 6f 73 e. This is almos
c17e0 74 20 61 6c 77 61 79 73 20 35 31 32 20 62 79 74 t always 512 byt
c17f0 65 73 2c 20 62 75 74 20 6d 61 79 20 62 65 0a 2a es, but may be.*
c1800 2a 20 6c 61 72 67 65 72 20 66 6f 72 20 73 6f 6d * larger for som
c1810 65 20 64 65 76 69 63 65 73 2e 0a 2a 2a 0a 2a 2a e devices..**.**
c1820 20 53 51 4c 69 74 65 20 63 6f 64 65 20 61 73 73 SQLite code ass
c1830 75 6d 65 73 20 74 68 69 73 20 66 75 6e 63 74 69 umes this functi
c1840 6f 6e 20 63 61 6e 6e 6f 74 20 66 61 69 6c 2e 20 on cannot fail.
c1850 49 74 20 61 6c 73 6f 20 61 73 73 75 6d 65 73 20 It also assumes
c1860 74 68 61 74 0a 2a 2a 20 69 66 20 74 77 6f 20 66 that.** if two f
c1870 69 6c 65 73 20 61 72 65 20 63 72 65 61 74 65 64 iles are created
c1880 20 69 6e 20 74 68 65 20 73 61 6d 65 20 66 69 6c in the same fil
c1890 65 2d 73 79 73 74 65 6d 20 64 69 72 65 63 74 6f e-system directo
c18a0 72 79 20 28 69 2e 65 2e 0a 2a 2a 20 61 20 64 61 ry (i.e..** a da
c18b0 74 61 62 61 73 65 20 61 6e 64 20 69 74 73 20 6a tabase and its j
c18c0 6f 75 72 6e 61 6c 20 66 69 6c 65 29 20 74 68 61 ournal file) tha
c18d0 74 20 74 68 65 20 73 65 63 74 6f 72 20 73 69 7a t the sector siz
c18e0 65 20 77 69 6c 6c 20 62 65 20 74 68 65 0a 2a 2a e will be the.**
c18f0 20 73 61 6d 65 20 66 6f 72 20 62 6f 74 68 2e 0a same for both..
c1900 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 */.static int os
c1910 32 53 65 63 74 6f 72 53 69 7a 65 28 73 71 6c 69 2SectorSize(sqli
c1920 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20 te3_file *id){.
c1930 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 return SQLITE_D
c1940 45 46 41 55 4c 54 5f 53 45 43 54 4f 52 5f 53 49 EFAULT_SECTOR_SI
c1950 5a 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 ZE;.}../*.** Ret
c1960 75 72 6e 20 61 20 76 65 63 74 6f 72 20 6f 66 20 urn a vector of
c1970 64 65 76 69 63 65 20 63 68 61 72 61 63 74 65 72 device character
c1980 69 73 74 69 63 73 2e 0a 2a 2f 0a 73 74 61 74 69 istics..*/.stati
c1990 63 20 69 6e 74 20 6f 73 32 44 65 76 69 63 65 43 c int os2DeviceC
c19a0 68 61 72 61 63 74 65 72 69 73 74 69 63 73 28 73 haracteristics(s
c19b0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 qlite3_file *id)
c19c0 7b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a {. return 0;.}.
c19d0 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 72 61 63 74 65 ../*.** Characte
c19e0 72 20 73 65 74 20 63 6f 6e 76 65 72 73 69 6f 6e r set conversion
c19f0 20 6f 62 6a 65 63 74 73 20 75 73 65 64 20 62 79 objects used by
c1a00 20 63 6f 6e 76 65 72 73 69 6f 6e 20 72 6f 75 74 conversion rout
c1a10 69 6e 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ines..*/.static
c1a20 55 63 6f 6e 76 4f 62 6a 65 63 74 20 75 63 55 74 UconvObject ucUt
c1a30 66 38 20 3d 20 4e 55 4c 4c 3b 20 2f 2a 20 63 6f f8 = NULL; /* co
c1a40 6e 76 65 72 74 20 62 65 74 77 65 65 6e 20 55 54 nvert between UT
c1a50 46 2d 38 20 61 6e 64 20 55 43 53 2d 32 20 2a 2f F-8 and UCS-2 */
c1a60 0a 73 74 61 74 69 63 20 55 63 6f 6e 76 4f 62 6a .static UconvObj
c1a70 65 63 74 20 75 63 6c 43 70 20 3d 20 4e 55 4c 4c ect uclCp = NULL
c1a80 3b 20 20 2f 2a 20 63 6f 6e 76 65 72 74 20 62 65 ; /* convert be
c1a90 74 77 65 65 6e 20 6c 6f 63 61 6c 20 63 6f 64 65 tween local code
c1aa0 70 61 67 65 20 61 6e 64 20 55 43 53 2d 32 20 2a page and UCS-2 *
c1ab0 2f 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c 70 65 72 20 /../*.** Helper
c1ac0 66 75 6e 63 74 69 6f 6e 20 74 6f 20 69 6e 69 74 function to init
c1ad0 69 61 6c 69 7a 65 20 74 68 65 20 63 6f 6e 76 65 ialize the conve
c1ae0 72 73 69 6f 6e 20 6f 62 6a 65 63 74 73 20 66 72 rsion objects fr
c1af0 6f 6d 20 61 6e 64 20 74 6f 20 55 54 46 2d 38 2e om and to UTF-8.
c1b00 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
c1b10 69 6e 69 74 55 63 6f 6e 76 4f 62 6a 65 63 74 73 initUconvObjects
c1b20 28 20 76 6f 69 64 20 29 7b 0a 20 20 69 66 28 20 ( void ){. if(
c1b30 55 6e 69 43 72 65 61 74 65 55 63 6f 6e 76 4f 62 UniCreateUconvOb
c1b40 6a 65 63 74 28 20 55 54 46 5f 38 2c 20 26 75 63 ject( UTF_8, &uc
c1b50 55 74 66 38 20 29 20 21 3d 20 55 4c 53 5f 53 55 Utf8 ) != ULS_SU
c1b60 43 43 45 53 53 20 29 0a 20 20 20 20 75 63 55 74 CCESS ). ucUt
c1b70 66 38 20 3d 20 4e 55 4c 4c 3b 0a 20 20 69 66 20 f8 = NULL;. if
c1b80 28 20 55 6e 69 43 72 65 61 74 65 55 63 6f 6e 76 ( UniCreateUconv
c1b90 4f 62 6a 65 63 74 28 20 28 55 6e 69 43 68 61 72 Object( (UniChar
c1ba0 20 2a 29 4c 22 40 70 61 74 68 3d 79 65 73 22 2c *)L"@path=yes",
c1bb0 20 26 75 63 6c 43 70 20 29 20 21 3d 20 55 4c 53 &uclCp ) != ULS
c1bc0 5f 53 55 43 43 45 53 53 20 29 0a 20 20 20 20 75 _SUCCESS ). u
c1bd0 63 6c 43 70 20 3d 20 4e 55 4c 4c 3b 0a 7d 0a 0a clCp = NULL;.}..
c1be0 2f 2a 0a 2a 2a 20 48 65 6c 70 65 72 20 66 75 6e /*.** Helper fun
c1bf0 63 74 69 6f 6e 20 74 6f 20 66 72 65 65 20 74 68 ction to free th
c1c00 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 6f 62 6a e conversion obj
c1c10 65 63 74 73 20 66 72 6f 6d 20 61 6e 64 20 74 6f ects from and to
c1c20 20 55 54 46 2d 38 2e 0a 2a 2f 0a 73 74 61 74 69 UTF-8..*/.stati
c1c30 63 20 76 6f 69 64 20 66 72 65 65 55 63 6f 6e 76 c void freeUconv
c1c40 4f 62 6a 65 63 74 73 28 20 76 6f 69 64 20 29 7b Objects( void ){
c1c50 0a 20 20 69 66 20 28 20 75 63 55 74 66 38 20 29 . if ( ucUtf8 )
c1c60 0a 20 20 20 20 55 6e 69 46 72 65 65 55 63 6f 6e . UniFreeUcon
c1c70 76 4f 62 6a 65 63 74 28 20 75 63 55 74 66 38 20 vObject( ucUtf8
c1c80 29 3b 0a 20 20 69 66 20 28 20 75 63 6c 43 70 20 );. if ( uclCp
c1c90 29 0a 20 20 20 20 55 6e 69 46 72 65 65 55 63 6f ). UniFreeUco
c1ca0 6e 76 4f 62 6a 65 63 74 28 20 75 63 6c 43 70 20 nvObject( uclCp
c1cb0 29 3b 0a 20 20 75 63 55 74 66 38 20 3d 20 4e 55 );. ucUtf8 = NU
c1cc0 4c 4c 3b 0a 20 20 75 63 6c 43 70 20 3d 20 4e 55 LL;. uclCp = NU
c1cd0 4c 4c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c LL;.}../*.** Hel
c1ce0 70 65 72 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 per function to
c1cf0 63 6f 6e 76 65 72 74 20 55 54 46 2d 38 20 66 69 convert UTF-8 fi
c1d00 6c 65 6e 61 6d 65 73 20 74 6f 20 6c 6f 63 61 6c lenames to local
c1d10 20 4f 53 2f 32 20 63 6f 64 65 70 61 67 65 2e 0a OS/2 codepage..
c1d20 2a 2a 20 54 68 65 20 74 77 6f 2d 73 74 65 70 20 ** The two-step
c1d30 70 72 6f 63 65 73 73 3a 20 66 69 72 73 74 20 63 process: first c
c1d40 6f 6e 76 65 72 74 20 74 68 65 20 69 6e 63 6f 6d onvert the incom
c1d50 69 6e 67 20 55 54 46 2d 38 20 73 74 72 69 6e 67 ing UTF-8 string
c1d60 0a 2a 2a 20 69 6e 74 6f 20 55 43 53 2d 32 20 61 .** into UCS-2 a
c1d70 6e 64 20 74 68 65 6e 20 66 72 6f 6d 20 55 43 53 nd then from UCS
c1d80 2d 32 20 74 6f 20 74 68 65 20 63 75 72 72 65 6e -2 to the curren
c1d90 74 20 63 6f 64 65 70 61 67 65 2e 0a 2a 2a 20 54 t codepage..** T
c1da0 68 65 20 72 65 74 75 72 6e 65 64 20 63 68 61 72 he returned char
c1db0 20 70 6f 69 6e 74 65 72 20 68 61 73 20 74 6f 20 pointer has to
c1dc0 62 65 20 66 72 65 65 64 2e 0a 2a 2f 0a 73 74 61 be freed..*/.sta
c1dd0 74 69 63 20 63 68 61 72 20 2a 63 6f 6e 76 65 72 tic char *conver
c1de0 74 55 74 66 38 50 61 74 68 54 6f 43 70 28 20 63 tUtf8PathToCp( c
c1df0 6f 6e 73 74 20 63 68 61 72 20 2a 69 6e 20 29 7b onst char *in ){
c1e00 0a 20 20 55 6e 69 43 68 61 72 20 74 65 6d 70 50 . UniChar tempP
c1e10 61 74 68 5b 43 43 48 4d 41 58 50 41 54 48 5d 3b ath[CCHMAXPATH];
c1e20 0a 20 20 63 68 61 72 20 2a 6f 75 74 20 3d 20 28 . char *out = (
c1e30 63 68 61 72 20 2a 29 63 61 6c 6c 6f 63 28 20 43 char *)calloc( C
c1e40 43 48 4d 41 58 50 41 54 48 2c 20 31 20 29 3b 0a CHMAXPATH, 1 );.
c1e50 0a 20 20 69 66 28 20 21 6f 75 74 20 29 0a 20 20 . if( !out ).
c1e60 20 20 72 65 74 75 72 6e 20 4e 55 4c 4c 3b 0a 0a return NULL;..
c1e70 20 20 69 66 28 20 21 75 63 55 74 66 38 20 7c 7c if( !ucUtf8 ||
c1e80 20 21 75 63 6c 43 70 20 29 0a 20 20 20 20 69 6e !uclCp ). in
c1e90 69 74 55 63 6f 6e 76 4f 62 6a 65 63 74 73 28 29 itUconvObjects()
c1ea0 3b 0a 0a 20 20 2f 2a 20 64 65 74 65 72 6d 69 6e ;.. /* determin
c1eb0 65 20 73 74 72 69 6e 67 20 66 6f 72 20 74 68 65 e string for the
c1ec0 20 63 6f 6e 76 65 72 73 69 6f 6e 20 6f 66 20 55 conversion of U
c1ed0 54 46 2d 38 20 77 68 69 63 68 20 69 73 20 43 50 TF-8 which is CP
c1ee0 31 32 30 38 20 2a 2f 0a 20 20 69 66 28 20 55 6e 1208 */. if( Un
c1ef0 69 53 74 72 54 6f 55 63 73 28 20 75 63 55 74 66 iStrToUcs( ucUtf
c1f00 38 2c 20 74 65 6d 70 50 61 74 68 2c 20 28 63 68 8, tempPath, (ch
c1f10 61 72 20 2a 29 69 6e 2c 20 43 43 48 4d 41 58 50 ar *)in, CCHMAXP
c1f20 41 54 48 20 29 20 21 3d 20 55 4c 53 5f 53 55 43 ATH ) != ULS_SUC
c1f30 43 45 53 53 20 29 0a 20 20 20 20 72 65 74 75 72 CESS ). retur
c1f40 6e 20 6f 75 74 3b 20 2f 2a 20 69 66 20 63 6f 6e n out; /* if con
c1f50 76 65 72 73 69 6f 6e 20 66 61 69 6c 73 2c 20 72 version fails, r
c1f60 65 74 75 72 6e 20 74 68 65 20 65 6d 70 74 79 20 eturn the empty
c1f70 73 74 72 69 6e 67 20 2a 2f 0a 0a 20 20 2f 2a 20 string */.. /*
c1f80 63 6f 6e 76 65 72 73 69 6f 6e 20 66 6f 72 20 63 conversion for c
c1f90 75 72 72 65 6e 74 20 63 6f 64 65 70 61 67 65 20 urrent codepage
c1fa0 77 68 69 63 68 20 63 61 6e 20 62 65 20 75 73 65 which can be use
c1fb0 64 20 66 6f 72 20 70 61 74 68 73 20 2a 2f 0a 20 d for paths */.
c1fc0 20 55 6e 69 53 74 72 46 72 6f 6d 55 63 73 28 20 UniStrFromUcs(
c1fd0 75 63 6c 43 70 2c 20 6f 75 74 2c 20 74 65 6d 70 uclCp, out, temp
c1fe0 50 61 74 68 2c 20 43 43 48 4d 41 58 50 41 54 48 Path, CCHMAXPATH
c1ff0 20 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 6f 75 );.. return ou
c2000 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c 70 t;.}../*.** Help
c2010 65 72 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 63 er function to c
c2020 6f 6e 76 65 72 74 20 66 69 6c 65 6e 61 6d 65 73 onvert filenames
c2030 20 66 72 6f 6d 20 6c 6f 63 61 6c 20 63 6f 64 65 from local code
c2040 70 61 67 65 20 74 6f 20 55 54 46 2d 38 2e 0a 2a page to UTF-8..*
c2050 2a 20 54 68 65 20 74 77 6f 2d 73 74 65 70 20 70 * The two-step p
c2060 72 6f 63 65 73 73 3a 20 66 69 72 73 74 20 63 6f rocess: first co
c2070 6e 76 65 72 74 20 74 68 65 20 69 6e 63 6f 6d 69 nvert the incomi
c2080 6e 67 20 63 6f 64 65 70 61 67 65 2d 73 70 65 63 ng codepage-spec
c2090 69 66 69 63 0a 2a 2a 20 73 74 72 69 6e 67 20 69 ific.** string i
c20a0 6e 74 6f 20 55 43 53 2d 32 20 61 6e 64 20 74 68 nto UCS-2 and th
c20b0 65 6e 20 66 72 6f 6d 20 55 43 53 2d 32 20 74 6f en from UCS-2 to
c20c0 20 74 68 65 20 63 6f 64 65 70 61 67 65 20 6f 66 the codepage of
c20d0 20 55 54 46 2d 38 2e 0a 2a 2a 20 54 68 65 20 72 UTF-8..** The r
c20e0 65 74 75 72 6e 65 64 20 63 68 61 72 20 70 6f 69 eturned char poi
c20f0 6e 74 65 72 20 68 61 73 20 74 6f 20 62 65 20 66 nter has to be f
c2100 72 65 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 reed..**.** This
c2110 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 6e function is non
c2120 2d 73 74 61 74 69 63 20 74 6f 20 62 65 20 61 62 -static to be ab
c2130 6c 65 20 74 6f 20 75 73 65 20 74 68 69 73 20 69 le to use this i
c2140 6e 20 73 68 65 6c 6c 2e 63 20 61 6e 64 0a 2a 2a n shell.c and.**
c2150 20 73 69 6d 69 6c 61 72 20 61 70 70 6c 69 63 61 similar applica
c2160 74 69 6f 6e 73 20 74 68 61 74 20 74 61 6b 65 20 tions that take
c2170 63 6f 6d 6d 61 6e 64 20 6c 69 6e 65 20 61 72 67 command line arg
c2180 75 6d 65 6e 74 73 2e 0a 2a 2f 0a 63 68 61 72 20 uments..*/.char
c2190 2a 63 6f 6e 76 65 72 74 43 70 50 61 74 68 54 6f *convertCpPathTo
c21a0 55 74 66 38 28 20 63 6f 6e 73 74 20 63 68 61 72 Utf8( const char
c21b0 20 2a 69 6e 20 29 7b 0a 20 20 55 6e 69 43 68 61 *in ){. UniCha
c21c0 72 20 74 65 6d 70 50 61 74 68 5b 43 43 48 4d 41 r tempPath[CCHMA
c21d0 58 50 41 54 48 5d 3b 0a 20 20 63 68 61 72 20 2a XPATH];. char *
c21e0 6f 75 74 20 3d 20 28 63 68 61 72 20 2a 29 63 61 out = (char *)ca
c21f0 6c 6c 6f 63 28 20 43 43 48 4d 41 58 50 41 54 48 lloc( CCHMAXPATH
c2200 2c 20 31 20 29 3b 0a 0a 20 20 69 66 28 20 21 6f , 1 );.. if( !o
c2210 75 74 20 29 0a 20 20 20 20 72 65 74 75 72 6e 20 ut ). return
c2220 4e 55 4c 4c 3b 0a 0a 20 20 69 66 28 20 21 75 63 NULL;.. if( !uc
c2230 55 74 66 38 20 7c 7c 20 21 75 63 6c 43 70 20 29 Utf8 || !uclCp )
c2240 0a 20 20 20 20 69 6e 69 74 55 63 6f 6e 76 4f 62 . initUconvOb
c2250 6a 65 63 74 73 28 29 3b 0a 0a 20 20 2f 2a 20 63 jects();.. /* c
c2260 6f 6e 76 65 72 73 69 6f 6e 20 66 6f 72 20 63 75 onversion for cu
c2270 72 72 65 6e 74 20 63 6f 64 65 70 61 67 65 20 77 rrent codepage w
c2280 68 69 63 68 20 63 61 6e 20 62 65 20 75 73 65 64 hich can be used
c2290 20 66 6f 72 20 70 61 74 68 73 20 2a 2f 0a 20 20 for paths */.
c22a0 69 66 28 20 55 6e 69 53 74 72 54 6f 55 63 73 28 if( UniStrToUcs(
c22b0 20 75 63 6c 43 70 2c 20 74 65 6d 70 50 61 74 68 uclCp, tempPath
c22c0 2c 20 28 63 68 61 72 20 2a 29 69 6e 2c 20 43 43 , (char *)in, CC
c22d0 48 4d 41 58 50 41 54 48 20 29 20 21 3d 20 55 4c HMAXPATH ) != UL
c22e0 53 5f 53 55 43 43 45 53 53 20 29 0a 20 20 20 20 S_SUCCESS ).
c22f0 72 65 74 75 72 6e 20 6f 75 74 3b 20 2f 2a 20 69 return out; /* i
c2300 66 20 63 6f 6e 76 65 72 73 69 6f 6e 20 66 61 69 f conversion fai
c2310 6c 73 2c 20 72 65 74 75 72 6e 20 74 68 65 20 65 ls, return the e
c2320 6d 70 74 79 20 73 74 72 69 6e 67 20 2a 2f 0a 0a mpty string */..
c2330 20 20 2f 2a 20 64 65 74 65 72 6d 69 6e 65 20 73 /* determine s
c2340 74 72 69 6e 67 20 66 6f 72 20 74 68 65 20 63 6f tring for the co
c2350 6e 76 65 72 73 69 6f 6e 20 6f 66 20 55 54 46 2d nversion of UTF-
c2360 38 20 77 68 69 63 68 20 69 73 20 43 50 31 32 30 8 which is CP120
c2370 38 20 2a 2f 0a 20 20 55 6e 69 53 74 72 46 72 6f 8 */. UniStrFro
c2380 6d 55 63 73 28 20 75 63 55 74 66 38 2c 20 6f 75 mUcs( ucUtf8, ou
c2390 74 2c 20 74 65 6d 70 50 61 74 68 2c 20 43 43 48 t, tempPath, CCH
c23a0 4d 41 58 50 41 54 48 20 29 3b 0a 0a 20 20 72 65 MAXPATH );.. re
c23b0 74 75 72 6e 20 6f 75 74 3b 0a 7d 0a 0a 2f 2a 0a turn out;.}../*.
c23c0 2a 2a 20 54 68 69 73 20 76 65 63 74 6f 72 20 64 ** This vector d
c23d0 65 66 69 6e 65 73 20 61 6c 6c 20 74 68 65 20 6d efines all the m
c23e0 65 74 68 6f 64 73 20 74 68 61 74 20 63 61 6e 20 ethods that can
c23f0 6f 70 65 72 61 74 65 20 6f 6e 20 61 6e 0a 2a 2a operate on an.**
c2400 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 66 6f sqlite3_file fo
c2410 72 20 6f 73 32 2e 0a 2a 2f 0a 73 74 61 74 69 63 r os2..*/.static
c2420 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 const sqlite3_i
c2430 6f 5f 6d 65 74 68 6f 64 73 20 6f 73 32 49 6f 4d o_methods os2IoM
c2440 65 74 68 6f 64 20 3d 20 7b 0a 20 20 31 2c 20 20 ethod = {. 1,
c2450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c2460 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f /* iVersio
c2470 6e 20 2a 2f 0a 20 20 6f 73 32 43 6c 6f 73 65 2c n */. os2Close,
c2480 0a 20 20 6f 73 32 52 65 61 64 2c 0a 20 20 6f 73 . os2Read,. os
c2490 32 57 72 69 74 65 2c 0a 20 20 6f 73 32 54 72 75 2Write,. os2Tru
c24a0 6e 63 61 74 65 2c 0a 20 20 6f 73 32 53 79 6e 63 ncate,. os2Sync
c24b0 2c 0a 20 20 6f 73 32 46 69 6c 65 53 69 7a 65 2c ,. os2FileSize,
c24c0 0a 20 20 6f 73 32 4c 6f 63 6b 2c 0a 20 20 6f 73 . os2Lock,. os
c24d0 32 55 6e 6c 6f 63 6b 2c 0a 20 20 6f 73 32 43 68 2Unlock,. os2Ch
c24e0 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 2c eckReservedLock,
c24f0 0a 20 20 6f 73 32 46 69 6c 65 43 6f 6e 74 72 6f . os2FileContro
c2500 6c 2c 0a 20 20 6f 73 32 53 65 63 74 6f 72 53 69 l,. os2SectorSi
c2510 7a 65 2c 0a 20 20 6f 73 32 44 65 76 69 63 65 43 ze,. os2DeviceC
c2520 68 61 72 61 63 74 65 72 69 73 74 69 63 73 0a 7d haracteristics.}
c2530 3b 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ;../************
c2540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c2550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c2560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c2570 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
c2580 2a 2a 20 48 65 72 65 20 65 6e 64 73 20 74 68 65 ** Here ends the
c2590 20 49 2f 4f 20 6d 65 74 68 6f 64 73 20 74 68 61 I/O methods tha
c25a0 74 20 66 6f 72 6d 20 74 68 65 20 73 71 6c 69 74 t form the sqlit
c25b0 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 e3_io_methods ob
c25c0 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ject..**.** The
c25d0 6e 65 78 74 20 62 6c 6f 63 6b 20 6f 66 20 63 6f next block of co
c25e0 64 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 de implements th
c25f0 65 20 56 46 53 20 6d 65 74 68 6f 64 73 2e 0a 2a e VFS methods..*
c2600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c2610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c2620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c2630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c2640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a ***********/../*
c2650 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 74 65 6d .** Create a tem
c2660 70 6f 72 61 72 79 20 66 69 6c 65 20 6e 61 6d 65 porary file name
c2670 20 69 6e 20 7a 42 75 66 2e 20 20 7a 42 75 66 20 in zBuf. zBuf
c2680 6d 75 73 74 20 62 65 20 62 69 67 20 65 6e 6f 75 must be big enou
c2690 67 68 20 74 6f 0a 2a 2a 20 68 6f 6c 64 20 61 74 gh to.** hold at
c26a0 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d pVfs->mxPathnam
c26b0 65 20 63 68 61 72 61 63 74 65 72 73 2e 0a 2a 2f e characters..*/
c26c0 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74 54 .static int getT
c26d0 65 6d 70 6e 61 6d 65 28 69 6e 74 20 6e 42 75 66 empname(int nBuf
c26e0 2c 20 63 68 61 72 20 2a 7a 42 75 66 20 29 7b 0a , char *zBuf ){.
c26f0 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 static const u
c2700 6e 73 69 67 6e 65 64 20 63 68 61 72 20 7a 43 68 nsigned char zCh
c2710 61 72 73 5b 5d 20 3d 0a 20 20 20 20 22 61 62 63 ars[] =. "abc
c2720 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 71 72 73 defghijklmnopqrs
c2730 74 75 76 77 78 79 7a 22 0a 20 20 20 20 22 41 42 tuvwxyz". "AB
c2740 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50 51 52 CDEFGHIJKLMNOPQR
c2750 53 54 55 56 57 58 59 5a 22 0a 20 20 20 20 22 30 STUVWXYZ". "0
c2760 31 32 33 34 35 36 37 38 39 22 3b 0a 20 20 69 6e 123456789";. in
c2770 74 20 69 2c 20 6a 3b 0a 20 20 63 68 61 72 20 7a t i, j;. char z
c2780 54 65 6d 70 50 61 74 68 42 75 66 5b 33 5d 3b 0a TempPathBuf[3];.
c2790 20 20 50 53 5a 20 7a 54 65 6d 70 50 61 74 68 20 PSZ zTempPath
c27a0 3d 20 28 50 53 5a 29 26 7a 54 65 6d 70 50 61 74 = (PSZ)&zTempPat
c27b0 68 42 75 66 3b 0a 20 20 69 66 28 20 73 71 6c 69 hBuf;. if( sqli
c27c0 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f te3_temp_directo
c27d0 72 79 20 29 7b 0a 20 20 20 20 7a 54 65 6d 70 50 ry ){. zTempP
c27e0 61 74 68 20 3d 20 73 71 6c 69 74 65 33 5f 74 65 ath = sqlite3_te
c27f0 6d 70 5f 64 69 72 65 63 74 6f 72 79 3b 0a 20 20 mp_directory;.
c2800 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66 28 20 44 }else{. if( D
c2810 6f 73 53 63 61 6e 45 6e 76 28 20 28 50 53 5a 29 osScanEnv( (PSZ)
c2820 22 54 45 4d 50 22 2c 20 26 7a 54 65 6d 70 50 61 "TEMP", &zTempPa
c2830 74 68 20 29 20 29 7b 0a 20 20 20 20 20 20 69 66 th ) ){. if
c2840 28 20 44 6f 73 53 63 61 6e 45 6e 76 28 20 28 50 ( DosScanEnv( (P
c2850 53 5a 29 22 54 4d 50 22 2c 20 26 7a 54 65 6d 70 SZ)"TMP", &zTemp
c2860 50 61 74 68 20 29 20 29 7b 0a 20 20 20 20 20 20 Path ) ){.
c2870 20 20 69 66 28 20 44 6f 73 53 63 61 6e 45 6e 76 if( DosScanEnv
c2880 28 20 28 50 53 5a 29 22 54 4d 50 44 49 52 22 2c ( (PSZ)"TMPDIR",
c2890 20 26 7a 54 65 6d 70 50 61 74 68 20 29 20 29 7b &zTempPath ) ){
c28a0 0a 20 20 20 20 20 20 20 20 20 20 20 55 4c 4f 4e . ULON
c28b0 47 20 75 6c 44 72 69 76 65 4e 75 6d 20 3d 20 30 G ulDriveNum = 0
c28c0 2c 20 75 6c 44 72 69 76 65 4d 61 70 20 3d 20 30 , ulDriveMap = 0
c28d0 3b 0a 20 20 20 20 20 20 20 20 20 20 20 44 6f 73 ;. Dos
c28e0 51 75 65 72 79 43 75 72 72 65 6e 74 44 69 73 6b QueryCurrentDisk
c28f0 28 20 26 75 6c 44 72 69 76 65 4e 75 6d 2c 20 26 ( &ulDriveNum, &
c2900 75 6c 44 72 69 76 65 4d 61 70 20 29 3b 0a 20 20 ulDriveMap );.
c2910 20 20 20 20 20 20 20 20 20 73 70 72 69 6e 74 66 sprintf
c2920 28 20 28 63 68 61 72 2a 29 7a 54 65 6d 70 50 61 ( (char*)zTempPa
c2930 74 68 2c 20 22 25 63 3a 22 2c 20 28 63 68 61 72 th, "%c:", (char
c2940 29 28 20 27 41 27 20 2b 20 75 6c 44 72 69 76 65 )( 'A' + ulDrive
c2950 4e 75 6d 20 2d 20 31 20 29 20 29 3b 0a 20 20 20 Num - 1 ) );.
c2960 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 }. }.
c2970 20 20 20 7d 0a 20 20 7d 0a 20 20 2f 2a 20 53 74 }. }. /* St
c2980 72 69 70 20 6f 66 66 20 61 20 74 72 61 69 6c 69 rip off a traili
c2990 6e 67 20 73 6c 61 73 68 65 73 20 6f 72 20 62 61 ng slashes or ba
c29a0 63 6b 73 6c 61 73 68 65 73 2c 20 6f 74 68 65 72 ckslashes, other
c29b0 77 69 73 65 20 77 65 20 77 6f 75 6c 64 20 67 65 wise we would ge
c29c0 74 20 2a 0a 20 20 20 2a 20 6d 75 6c 74 69 70 6c t *. * multipl
c29d0 65 20 28 62 61 63 6b 29 73 6c 61 73 68 65 73 20 e (back)slashes
c29e0 77 68 69 63 68 20 63 61 75 73 65 73 20 44 6f 73 which causes Dos
c29f0 4f 70 65 6e 28 29 20 74 6f 20 66 61 69 6c 2e 20 Open() to fail.
c2a00 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 0a 20 *.
c2a10 20 20 2a 20 54 72 61 69 6c 69 6e 67 20 73 70 61 * Trailing spa
c2a20 63 65 73 20 61 72 65 20 6e 6f 74 20 61 6c 6c 6f ces are not allo
c2a30 77 65 64 2c 20 65 69 74 68 65 72 2e 20 20 20 20 wed, either.
c2a40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c2a50 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 6a 20 3d */. j =
c2a60 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 sqlite3Strlen30
c2a70 28 7a 54 65 6d 70 50 61 74 68 29 3b 0a 20 20 77 (zTempPath);. w
c2a80 68 69 6c 65 28 20 6a 20 3e 20 30 20 26 26 20 28 hile( j > 0 && (
c2a90 20 7a 54 65 6d 70 50 61 74 68 5b 6a 2d 31 5d 20 zTempPath[j-1]
c2aa0 3d 3d 20 27 5c 5c 27 20 7c 7c 20 7a 54 65 6d 70 == '\\' || zTemp
c2ab0 50 61 74 68 5b 6a 2d 31 5d 20 3d 3d 20 27 2f 27 Path[j-1] == '/'
c2ac0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
c2ad0 20 20 20 20 20 7c 7c 20 7a 54 65 6d 70 50 61 74 || zTempPat
c2ae0 68 5b 6a 2d 31 5d 20 3d 3d 20 27 20 27 20 29 20 h[j-1] == ' ' )
c2af0 29 7b 0a 20 20 20 20 6a 2d 2d 3b 0a 20 20 7d 0a ){. j--;. }.
c2b00 20 20 7a 54 65 6d 70 50 61 74 68 5b 6a 5d 20 3d zTempPath[j] =
c2b10 20 27 5c 30 27 3b 0a 20 20 69 66 28 20 21 73 71 '\0';. if( !sq
c2b20 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 63 lite3_temp_direc
c2b30 74 6f 72 79 20 29 7b 0a 20 20 20 20 63 68 61 72 tory ){. char
c2b40 20 2a 7a 54 65 6d 70 50 61 74 68 55 54 46 20 3d *zTempPathUTF =
c2b50 20 63 6f 6e 76 65 72 74 43 70 50 61 74 68 54 6f convertCpPathTo
c2b60 55 74 66 38 28 20 7a 54 65 6d 70 50 61 74 68 20 Utf8( zTempPath
c2b70 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 );. sqlite3_s
c2b80 6e 70 72 69 6e 74 66 28 20 6e 42 75 66 2d 33 30 nprintf( nBuf-30
c2b90 2c 20 7a 42 75 66 2c 0a 20 20 20 20 20 20 20 20 , zBuf,.
c2ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 25 "%
c2bb0 73 5c 5c 22 53 51 4c 49 54 45 5f 54 45 4d 50 5f s\\"SQLITE_TEMP_
c2bc0 46 49 4c 45 5f 50 52 45 46 49 58 2c 20 7a 54 65 FILE_PREFIX, zTe
c2bd0 6d 70 50 61 74 68 55 54 46 20 29 3b 0a 20 20 20 mpPathUTF );.
c2be0 20 66 72 65 65 28 20 7a 54 65 6d 70 50 61 74 68 free( zTempPath
c2bf0 55 54 46 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a UTF );. }else{.
c2c00 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 sqlite3_snpr
c2c10 69 6e 74 66 28 20 6e 42 75 66 2d 33 30 2c 20 7a intf( nBuf-30, z
c2c20 42 75 66 2c 0a 20 20 20 20 20 20 20 20 20 20 20 Buf,.
c2c30 20 20 20 20 20 20 20 20 20 20 20 22 25 73 5c 5c "%s\\
c2c40 22 53 51 4c 49 54 45 5f 54 45 4d 50 5f 46 49 4c "SQLITE_TEMP_FIL
c2c50 45 5f 50 52 45 46 49 58 2c 20 7a 54 65 6d 70 50 E_PREFIX, zTempP
c2c60 61 74 68 20 29 3b 0a 20 20 7d 0a 20 20 6a 20 3d ath );. }. j =
c2c70 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 sqlite3Strlen30
c2c80 28 20 7a 42 75 66 20 29 3b 0a 20 20 73 71 6c 69 ( zBuf );. sqli
c2c90 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 20 te3_randomness(
c2ca0 32 30 2c 20 26 7a 42 75 66 5b 6a 5d 20 29 3b 0a 20, &zBuf[j] );.
c2cb0 20 20 66 6f 72 28 20 69 20 3d 20 30 3b 20 69 20 for( i = 0; i
c2cc0 3c 20 32 30 3b 20 69 2b 2b 2c 20 6a 2b 2b 20 29 < 20; i++, j++ )
c2cd0 7b 0a 20 20 20 20 7a 42 75 66 5b 6a 5d 20 3d 20 {. zBuf[j] =
c2ce0 28 63 68 61 72 29 7a 43 68 61 72 73 5b 20 28 28 (char)zChars[ ((
c2cf0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 7a 42 unsigned char)zB
c2d00 75 66 5b 6a 5d 29 25 28 73 69 7a 65 6f 66 28 7a uf[j])%(sizeof(z
c2d10 43 68 61 72 73 29 2d 31 29 20 5d 3b 0a 20 20 7d Chars)-1) ];. }
c2d20 0a 20 20 7a 42 75 66 5b 6a 5d 20 3d 20 30 3b 0a . zBuf[j] = 0;.
c2d30 20 20 4f 53 54 52 41 43 45 32 28 20 22 54 45 4d OSTRACE2( "TEM
c2d40 50 20 46 49 4c 45 4e 41 4d 45 3a 20 25 73 5c 6e P FILENAME: %s\n
c2d50 22 2c 20 7a 42 75 66 20 29 3b 0a 20 20 72 65 74 ", zBuf );. ret
c2d60 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
c2d70 0a 0a 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20 61 20 .../*.** Turn a
c2d80 72 65 6c 61 74 69 76 65 20 70 61 74 68 6e 61 6d relative pathnam
c2d90 65 20 69 6e 74 6f 20 61 20 66 75 6c 6c 20 70 61 e into a full pa
c2da0 74 68 6e 61 6d 65 2e 20 20 57 72 69 74 65 20 74 thname. Write t
c2db0 68 65 20 66 75 6c 6c 0a 2a 2a 20 70 61 74 68 6e he full.** pathn
c2dc0 61 6d 65 20 69 6e 74 6f 20 7a 46 75 6c 6c 5b 5d ame into zFull[]
c2dd0 2e 20 20 7a 46 75 6c 6c 5b 5d 20 77 69 6c 6c 20 . zFull[] will
c2de0 62 65 20 61 74 20 6c 65 61 73 74 20 70 56 66 73 be at least pVfs
c2df0 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 0a 2a 2a 20 ->mxPathname.**
c2e00 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2e 0a 2a bytes in size..*
c2e10 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 /.static int os2
c2e20 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 0a 20 20 FullPathname(.
c2e30 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 sqlite3_vfs *pVf
c2e40 73 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 s, /* P
c2e50 6f 69 6e 74 65 72 20 74 6f 20 76 66 73 20 6f 62 ointer to vfs ob
c2e60 6a 65 63 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 ject */. const
c2e70 63 68 61 72 20 2a 7a 52 65 6c 61 74 69 76 65 2c char *zRelative,
c2e80 20 20 20 20 20 20 2f 2a 20 50 6f 73 73 69 62 6c /* Possibl
c2e90 79 20 72 65 6c 61 74 69 76 65 20 69 6e 70 75 74 y relative input
c2ea0 20 70 61 74 68 20 2a 2f 0a 20 20 69 6e 74 20 6e path */. int n
c2eb0 46 75 6c 6c 2c 20 20 20 20 20 20 20 20 20 20 20 Full,
c2ec0 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f /* Size o
c2ed0 66 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 20 f output buffer
c2ee0 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 63 68 in bytes */. ch
c2ef0 61 72 20 2a 7a 46 75 6c 6c 20 20 20 20 20 20 20 ar *zFull
c2f00 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 74 /* Out
c2f10 70 75 74 20 62 75 66 66 65 72 20 2a 2f 0a 29 7b put buffer */.){
c2f20 0a 20 20 63 68 61 72 20 2a 7a 52 65 6c 61 74 69 . char *zRelati
c2f30 76 65 43 70 20 3d 20 63 6f 6e 76 65 72 74 55 74 veCp = convertUt
c2f40 66 38 50 61 74 68 54 6f 43 70 28 20 7a 52 65 6c f8PathToCp( zRel
c2f50 61 74 69 76 65 20 29 3b 0a 20 20 63 68 61 72 20 ative );. char
c2f60 7a 46 75 6c 6c 43 70 5b 43 43 48 4d 41 58 50 41 zFullCp[CCHMAXPA
c2f70 54 48 5d 20 3d 20 22 5c 30 22 3b 0a 20 20 63 68 TH] = "\0";. ch
c2f80 61 72 20 2a 7a 46 75 6c 6c 55 54 46 3b 0a 20 20 ar *zFullUTF;.
c2f90 41 50 49 52 45 54 20 72 63 20 3d 20 44 6f 73 51 APIRET rc = DosQ
c2fa0 75 65 72 79 50 61 74 68 49 6e 66 6f 28 20 7a 52 ueryPathInfo( zR
c2fb0 65 6c 61 74 69 76 65 43 70 2c 20 46 49 4c 5f 51 elativeCp, FIL_Q
c2fc0 55 45 52 59 46 55 4c 4c 4e 41 4d 45 2c 20 7a 46 UERYFULLNAME, zF
c2fd0 75 6c 6c 43 70 2c 0a 20 20 20 20 20 20 20 20 20 ullCp,.
c2fe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c2ff0 20 20 20 20 20 20 20 43 43 48 4d 41 58 50 41 54 CCHMAXPAT
c3000 48 20 29 3b 0a 20 20 66 72 65 65 28 20 7a 52 65 H );. free( zRe
c3010 6c 61 74 69 76 65 43 70 20 29 3b 0a 20 20 7a 46 lativeCp );. zF
c3020 75 6c 6c 55 54 46 20 3d 20 63 6f 6e 76 65 72 74 ullUTF = convert
c3030 43 70 50 61 74 68 54 6f 55 74 66 38 28 20 7a 46 CpPathToUtf8( zF
c3040 75 6c 6c 43 70 20 29 3b 0a 20 20 73 71 6c 69 74 ullCp );. sqlit
c3050 65 33 5f 73 6e 70 72 69 6e 74 66 28 20 6e 46 75 e3_snprintf( nFu
c3060 6c 6c 2c 20 7a 46 75 6c 6c 2c 20 7a 46 75 6c 6c ll, zFull, zFull
c3070 55 54 46 20 29 3b 0a 20 20 66 72 65 65 28 20 7a UTF );. free( z
c3080 46 75 6c 6c 55 54 46 20 29 3b 0a 20 20 72 65 74 FullUTF );. ret
c3090 75 72 6e 20 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 urn rc == NO_ERR
c30a0 4f 52 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a OR ? SQLITE_OK :
c30b0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 7d SQLITE_IOERR;.}
c30c0 0a 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 .../*.** Open a
c30d0 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 file..*/.static
c30e0 69 6e 74 20 6f 73 32 4f 70 65 6e 28 0a 20 20 73 int os2Open(. s
c30f0 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 qlite3_vfs *pVfs
c3100 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 , /*
c3110 4e 6f 74 20 75 73 65 64 20 2a 2f 0a 20 20 63 6f Not used */. co
c3120 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c nst char *zName,
c3130 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
c3140 61 6d 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 ame of the file
c3150 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c */. sqlite3_fil
c3160 65 20 2a 69 64 2c 20 20 20 20 20 20 20 20 20 20 e *id,
c3170 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 /* Write the
c3180 53 51 4c 69 74 65 20 66 69 6c 65 20 68 61 6e 64 SQLite file hand
c3190 6c 65 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 le here */. int
c31a0 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 flags,
c31b0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 /* Op
c31c0 65 6e 20 6d 6f 64 65 20 66 6c 61 67 73 20 2a 2f en mode flags */
c31d0 0a 20 20 69 6e 74 20 2a 70 4f 75 74 46 6c 61 67 . int *pOutFlag
c31e0 73 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 s
c31f0 20 2f 2a 20 53 74 61 74 75 73 20 72 65 74 75 72 /* Status retur
c3200 6e 20 66 6c 61 67 73 20 2a 2f 0a 29 7b 0a 20 20 n flags */.){.
c3210 48 46 49 4c 45 20 68 3b 0a 20 20 55 4c 4f 4e 47 HFILE h;. ULONG
c3220 20 75 6c 46 69 6c 65 41 74 74 72 69 62 75 74 65 ulFileAttribute
c3230 20 3d 20 46 49 4c 45 5f 4e 4f 52 4d 41 4c 3b 0a = FILE_NORMAL;.
c3240 20 20 55 4c 4f 4e 47 20 75 6c 4f 70 65 6e 46 6c ULONG ulOpenFl
c3250 61 67 73 20 3d 20 30 3b 0a 20 20 55 4c 4f 4e 47 ags = 0;. ULONG
c3260 20 75 6c 4f 70 65 6e 4d 6f 64 65 20 3d 20 30 3b ulOpenMode = 0;
c3270 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70 46 69 6c . os2File *pFil
c3280 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29 69 64 e = (os2File*)id
c3290 3b 0a 20 20 41 50 49 52 45 54 20 72 63 20 3d 20 ;. APIRET rc =
c32a0 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 55 4c 4f 4e NO_ERROR;. ULON
c32b0 47 20 75 6c 41 63 74 69 6f 6e 3b 0a 20 20 63 68 G ulAction;. ch
c32c0 61 72 20 2a 7a 4e 61 6d 65 43 70 3b 0a 20 20 63 ar *zNameCp;. c
c32d0 68 61 72 20 7a 54 6d 70 6e 61 6d 65 5b 43 43 48 har zTmpname[CCH
c32e0 4d 41 58 50 41 54 48 2b 31 5d 3b 20 20 20 20 2f MAXPATH+1]; /
c32f0 2a 20 42 75 66 66 65 72 20 74 6f 20 68 6f 6c 64 * Buffer to hold
c3300 20 6e 61 6d 65 20 6f 66 20 74 65 6d 70 20 66 69 name of temp fi
c3310 6c 65 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 le */.. /* If t
c3320 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 he second argume
c3330 6e 74 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 nt to this funct
c3340 69 6f 6e 20 69 73 20 4e 55 4c 4c 2c 20 67 65 6e ion is NULL, gen
c3350 65 72 61 74 65 20 61 20 0a 20 20 2a 2a 20 74 65 erate a . ** te
c3360 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 6e 61 6d mporary file nam
c3370 65 20 74 6f 20 75 73 65 20 0a 20 20 2a 2f 0a 20 e to use . */.
c3380 20 69 66 28 20 21 7a 4e 61 6d 65 20 29 7b 0a 20 if( !zName ){.
c3390 20 20 20 69 6e 74 20 72 63 20 3d 20 67 65 74 54 int rc = getT
c33a0 65 6d 70 6e 61 6d 65 28 43 43 48 4d 41 58 50 41 empname(CCHMAXPA
c33b0 54 48 2b 31 2c 20 7a 54 6d 70 6e 61 6d 65 29 3b TH+1, zTmpname);
c33c0 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c . if( rc!=SQL
c33d0 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
c33e0 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d return rc;. }
c33f0 0a 20 20 20 20 7a 4e 61 6d 65 20 3d 20 7a 54 6d . zName = zTm
c3400 70 6e 61 6d 65 3b 0a 20 20 7d 0a 0a 0a 20 20 6d pname;. }... m
c3410 65 6d 73 65 74 28 20 70 46 69 6c 65 2c 20 30 2c emset( pFile, 0,
c3420 20 73 69 7a 65 6f 66 28 2a 70 46 69 6c 65 29 20 sizeof(*pFile)
c3430 29 3b 0a 0a 20 20 4f 53 54 52 41 43 45 32 28 20 );.. OSTRACE2(
c3440 22 4f 50 45 4e 20 77 61 6e 74 20 25 64 5c 6e 22 "OPEN want %d\n"
c3450 2c 20 66 6c 61 67 73 20 29 3b 0a 0a 20 20 69 66 , flags );.. if
c3460 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 ( flags & SQLITE
c3470 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20 _OPEN_READWRITE
c3480 29 7b 0a 20 20 20 20 75 6c 4f 70 65 6e 4d 6f 64 ){. ulOpenMod
c3490 65 20 7c 3d 20 4f 50 45 4e 5f 41 43 43 45 53 53 e |= OPEN_ACCESS
c34a0 5f 52 45 41 44 57 52 49 54 45 3b 0a 20 20 20 20 _READWRITE;.
c34b0 4f 53 54 52 41 43 45 31 28 20 22 4f 50 45 4e 20 OSTRACE1( "OPEN
c34c0 72 65 61 64 2f 77 72 69 74 65 5c 6e 22 20 29 3b read/write\n" );
c34d0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 6c . }else{. ul
c34e0 4f 70 65 6e 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e OpenMode |= OPEN
c34f0 5f 41 43 43 45 53 53 5f 52 45 41 44 4f 4e 4c 59 _ACCESS_READONLY
c3500 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 31 28 20 ;. OSTRACE1(
c3510 22 4f 50 45 4e 20 72 65 61 64 20 6f 6e 6c 79 5c "OPEN read only\
c3520 6e 22 20 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 n" );. }.. if(
c3530 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f flags & SQLITE_
c3540 4f 50 45 4e 5f 43 52 45 41 54 45 20 29 7b 0a 20 OPEN_CREATE ){.
c3550 20 20 20 75 6c 4f 70 65 6e 46 6c 61 67 73 20 7c ulOpenFlags |
c3560 3d 20 4f 50 45 4e 5f 41 43 54 49 4f 4e 5f 4f 50 = OPEN_ACTION_OP
c3570 45 4e 5f 49 46 5f 45 58 49 53 54 53 20 7c 20 4f EN_IF_EXISTS | O
c3580 50 45 4e 5f 41 43 54 49 4f 4e 5f 43 52 45 41 54 PEN_ACTION_CREAT
c3590 45 5f 49 46 5f 4e 45 57 3b 0a 20 20 20 20 4f 53 E_IF_NEW;. OS
c35a0 54 52 41 43 45 31 28 20 22 4f 50 45 4e 20 6f 70 TRACE1( "OPEN op
c35b0 65 6e 20 6e 65 77 2f 63 72 65 61 74 65 5c 6e 22 en new/create\n"
c35c0 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 );. }else{.
c35d0 20 75 6c 4f 70 65 6e 46 6c 61 67 73 20 7c 3d 20 ulOpenFlags |=
c35e0 4f 50 45 4e 5f 41 43 54 49 4f 4e 5f 4f 50 45 4e OPEN_ACTION_OPEN
c35f0 5f 49 46 5f 45 58 49 53 54 53 20 7c 20 4f 50 45 _IF_EXISTS | OPE
c3600 4e 5f 41 43 54 49 4f 4e 5f 46 41 49 4c 5f 49 46 N_ACTION_FAIL_IF
c3610 5f 4e 45 57 3b 0a 20 20 20 20 4f 53 54 52 41 43 _NEW;. OSTRAC
c3620 45 31 28 20 22 4f 50 45 4e 20 6f 70 65 6e 20 65 E1( "OPEN open e
c3630 78 69 73 74 69 6e 67 5c 6e 22 20 29 3b 0a 20 20 xisting\n" );.
c3640 7d 0a 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26 }.. if( flags &
c3650 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 SQLITE_OPEN_MAI
c3660 4e 5f 44 42 20 29 7b 0a 20 20 20 20 75 6c 4f 70 N_DB ){. ulOp
c3670 65 6e 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 53 enMode |= OPEN_S
c3680 48 41 52 45 5f 44 45 4e 59 4e 4f 4e 45 3b 0a 20 HARE_DENYNONE;.
c3690 20 20 20 4f 53 54 52 41 43 45 31 28 20 22 4f 50 OSTRACE1( "OP
c36a0 45 4e 20 73 68 61 72 65 20 72 65 61 64 2f 77 72 EN share read/wr
c36b0 69 74 65 5c 6e 22 20 29 3b 0a 20 20 7d 65 6c 73 ite\n" );. }els
c36c0 65 7b 0a 20 20 20 20 75 6c 4f 70 65 6e 4d 6f 64 e{. ulOpenMod
c36d0 65 20 7c 3d 20 4f 50 45 4e 5f 53 48 41 52 45 5f e |= OPEN_SHARE_
c36e0 44 45 4e 59 57 52 49 54 45 3b 0a 20 20 20 20 4f DENYWRITE;. O
c36f0 53 54 52 41 43 45 31 28 20 22 4f 50 45 4e 20 73 STRACE1( "OPEN s
c3700 68 61 72 65 20 72 65 61 64 20 6f 6e 6c 79 5c 6e hare read only\n
c3710 22 20 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 " );. }.. if(
c3720 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f flags & SQLITE_O
c3730 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 PEN_DELETEONCLOS
c3740 45 20 29 7b 0a 20 20 20 20 63 68 61 72 20 70 61 E ){. char pa
c3750 74 68 55 74 66 38 5b 43 43 48 4d 41 58 50 41 54 thUtf8[CCHMAXPAT
c3760 48 5d 3b 0a 23 69 66 64 65 66 20 4e 44 45 42 55 H];.#ifdef NDEBU
c3770 47 20 2f 2a 20 77 68 65 6e 20 64 65 62 75 67 67 G /* when debugg
c3780 69 6e 67 20 77 65 20 77 61 6e 74 20 74 6f 20 6d ing we want to m
c3790 61 6b 65 20 73 75 72 65 20 69 74 20 69 73 20 64 ake sure it is d
c37a0 65 6c 65 74 65 64 20 2a 2f 0a 20 20 20 20 75 6c eleted */. ul
c37b0 46 69 6c 65 41 74 74 72 69 62 75 74 65 20 3d 20 FileAttribute =
c37c0 46 49 4c 45 5f 48 49 44 44 45 4e 3b 0a 23 65 6e FILE_HIDDEN;.#en
c37d0 64 69 66 0a 20 20 20 20 6f 73 32 46 75 6c 6c 50 dif. os2FullP
c37e0 61 74 68 6e 61 6d 65 28 20 70 56 66 73 2c 20 7a athname( pVfs, z
c37f0 4e 61 6d 65 2c 20 43 43 48 4d 41 58 50 41 54 48 Name, CCHMAXPATH
c3800 2c 20 70 61 74 68 55 74 66 38 20 29 3b 0a 20 20 , pathUtf8 );.
c3810 20 20 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44 pFile->pathToD
c3820 65 6c 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 el = convertUtf8
c3830 50 61 74 68 54 6f 43 70 28 20 70 61 74 68 55 74 PathToCp( pathUt
c3840 66 38 20 29 3b 0a 20 20 20 20 4f 53 54 52 41 43 f8 );. OSTRAC
c3850 45 31 28 20 22 4f 50 45 4e 20 68 69 64 64 65 6e E1( "OPEN hidden
c3860 2f 64 65 6c 65 74 65 20 6f 6e 20 63 6c 6f 73 65 /delete on close
c3870 20 66 69 6c 65 20 61 74 74 72 69 62 75 74 65 73 file attributes
c3880 5c 6e 22 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a \n" );. }else{.
c3890 20 20 20 20 70 46 69 6c 65 2d 3e 70 61 74 68 54 pFile->pathT
c38a0 6f 44 65 6c 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 oDel = NULL;.
c38b0 20 4f 53 54 52 41 43 45 31 28 20 22 4f 50 45 4e OSTRACE1( "OPEN
c38c0 20 6e 6f 72 6d 61 6c 20 66 69 6c 65 20 61 74 74 normal file att
c38d0 72 69 62 75 74 65 5c 6e 22 20 29 3b 0a 20 20 7d ribute\n" );. }
c38e0 0a 0a 20 20 2f 2a 20 61 6c 77 61 79 73 20 6f 70 .. /* always op
c38f0 65 6e 20 69 6e 20 72 61 6e 64 6f 6d 20 61 63 63 en in random acc
c3900 65 73 73 20 6d 6f 64 65 20 66 6f 72 20 70 6f 73 ess mode for pos
c3910 73 69 62 6c 79 20 62 65 74 74 65 72 20 73 70 65 sibly better spe
c3920 65 64 20 2a 2f 0a 20 20 75 6c 4f 70 65 6e 4d 6f ed */. ulOpenMo
c3930 64 65 20 7c 3d 20 4f 50 45 4e 5f 46 4c 41 47 53 de |= OPEN_FLAGS
c3940 5f 52 41 4e 44 4f 4d 3b 0a 20 20 75 6c 4f 70 65 _RANDOM;. ulOpe
c3950 6e 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 46 4c nMode |= OPEN_FL
c3960 41 47 53 5f 46 41 49 4c 5f 4f 4e 5f 45 52 52 4f AGS_FAIL_ON_ERRO
c3970 52 3b 0a 20 20 75 6c 4f 70 65 6e 4d 6f 64 65 20 R;. ulOpenMode
c3980 7c 3d 20 4f 50 45 4e 5f 46 4c 41 47 53 5f 4e 4f |= OPEN_FLAGS_NO
c3990 49 4e 48 45 52 49 54 3b 0a 0a 20 20 7a 4e 61 6d INHERIT;.. zNam
c39a0 65 43 70 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 eCp = convertUtf
c39b0 38 50 61 74 68 54 6f 43 70 28 20 7a 4e 61 6d 65 8PathToCp( zName
c39c0 20 29 3b 0a 20 20 72 63 20 3d 20 44 6f 73 4f 70 );. rc = DosOp
c39d0 65 6e 28 20 28 50 53 5a 29 7a 4e 61 6d 65 43 70 en( (PSZ)zNameCp
c39e0 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
c39f0 20 20 26 68 2c 0a 20 20 20 20 20 20 20 20 20 20 &h,.
c3a00 20 20 20 20 20 20 26 75 6c 41 63 74 69 6f 6e 2c &ulAction,
c3a10 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
c3a20 20 30 4c 2c 0a 20 20 20 20 20 20 20 20 20 20 20 0L,.
c3a30 20 20 20 20 20 75 6c 46 69 6c 65 41 74 74 72 69 ulFileAttri
c3a40 62 75 74 65 2c 0a 20 20 20 20 20 20 20 20 20 20 bute,.
c3a50 20 20 20 20 20 20 75 6c 4f 70 65 6e 46 6c 61 67 ulOpenFlag
c3a60 73 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 s,.
c3a70 20 20 20 75 6c 4f 70 65 6e 4d 6f 64 65 2c 0a 20 ulOpenMode,.
c3a80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 (
c3a90 50 45 41 4f 50 32 29 4e 55 4c 4c 20 29 3b 0a 20 PEAOP2)NULL );.
c3aa0 20 66 72 65 65 28 20 7a 4e 61 6d 65 43 70 20 29 free( zNameCp )
c3ab0 3b 0a 20 20 69 66 28 20 72 63 20 21 3d 20 4e 4f ;. if( rc != NO
c3ac0 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 4f 53 _ERROR ){. OS
c3ad0 54 52 41 43 45 37 28 20 22 4f 50 45 4e 20 49 6e TRACE7( "OPEN In
c3ae0 76 61 6c 69 64 20 68 61 6e 64 6c 65 20 72 63 3d valid handle rc=
c3af0 25 64 3a 20 7a 4e 61 6d 65 3d 25 73 2c 20 75 6c %d: zName=%s, ul
c3b00 41 63 74 69 6f 6e 3d 25 23 6c 78 2c 20 75 6c 41 Action=%#lx, ulA
c3b10 74 74 72 3d 25 23 6c 78 2c 20 75 6c 46 6c 61 67 ttr=%#lx, ulFlag
c3b20 73 3d 25 23 6c 78 2c 20 75 6c 4d 6f 64 65 3d 25 s=%#lx, ulMode=%
c3b30 23 6c 78 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 #lx\n",.
c3b40 20 20 20 20 20 20 72 63 2c 20 7a 4e 61 6d 65 2c rc, zName,
c3b50 20 75 6c 41 63 74 69 6f 6e 2c 20 75 6c 46 69 6c ulAction, ulFil
c3b60 65 41 74 74 72 69 62 75 74 65 2c 20 75 6c 4f 70 eAttribute, ulOp
c3b70 65 6e 46 6c 61 67 73 2c 20 75 6c 4f 70 65 6e 4d enFlags, ulOpenM
c3b80 6f 64 65 20 29 3b 0a 20 20 20 20 69 66 28 20 70 ode );. if( p
c3b90 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c 20 File->pathToDel
c3ba0 29 0a 20 20 20 20 20 20 66 72 65 65 28 20 70 46 ). free( pF
c3bb0 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c 20 29 ile->pathToDel )
c3bc0 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 70 61 74 ;. pFile->pat
c3bd0 68 54 6f 44 65 6c 20 3d 20 4e 55 4c 4c 3b 0a 20 hToDel = NULL;.
c3be0 20 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 53 if( flags & S
c3bf0 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 QLITE_OPEN_READW
c3c00 52 49 54 45 20 29 7b 0a 20 20 20 20 20 20 4f 53 RITE ){. OS
c3c10 54 52 41 43 45 32 28 20 22 4f 50 45 4e 20 25 64 TRACE2( "OPEN %d
c3c20 20 49 6e 76 61 6c 69 64 20 68 61 6e 64 6c 65 5c Invalid handle\
c3c30 6e 22 2c 20 28 28 66 6c 61 67 73 20 7c 20 53 51 n", ((flags | SQ
c3c40 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e LITE_OPEN_READON
c3c50 4c 59 29 20 26 20 7e 53 51 4c 49 54 45 5f 4f 50 LY) & ~SQLITE_OP
c3c60 45 4e 5f 52 45 41 44 57 52 49 54 45 29 20 29 3b EN_READWRITE) );
c3c70 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 6f 73 . return os
c3c80 32 4f 70 65 6e 28 20 70 56 66 73 2c 20 7a 4e 61 2Open( pVfs, zNa
c3c90 6d 65 2c 20 69 64 2c 0a 20 20 20 20 20 20 20 20 me, id,.
c3ca0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 28 ((
c3cb0 66 6c 61 67 73 20 7c 20 53 51 4c 49 54 45 5f 4f flags | SQLITE_O
c3cc0 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 29 20 26 20 PEN_READONLY) &
c3cd0 7e 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 ~SQLITE_OPEN_REA
c3ce0 44 57 52 49 54 45 29 2c 0a 20 20 20 20 20 20 20 DWRITE),.
c3cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 p
c3d00 4f 75 74 46 6c 61 67 73 20 29 3b 0a 20 20 20 20 OutFlags );.
c3d10 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 65 74 }else{. ret
c3d20 75 72 6e 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f urn SQLITE_CANTO
c3d30 50 45 4e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a PEN;. }. }..
c3d40 20 20 69 66 28 20 70 4f 75 74 46 6c 61 67 73 20 if( pOutFlags
c3d50 29 7b 0a 20 20 20 20 2a 70 4f 75 74 46 6c 61 67 ){. *pOutFlag
c3d60 73 20 3d 20 66 6c 61 67 73 20 26 20 53 51 4c 49 s = flags & SQLI
c3d70 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 TE_OPEN_READWRIT
c3d80 45 20 3f 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f E ? SQLITE_OPEN_
c3d90 52 45 41 44 57 52 49 54 45 20 3a 20 53 51 4c 49 READWRITE : SQLI
c3da0 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 TE_OPEN_READONLY
c3db0 3b 0a 20 20 7d 0a 0a 20 20 70 46 69 6c 65 2d 3e ;. }.. pFile->
c3dc0 70 4d 65 74 68 6f 64 20 3d 20 26 6f 73 32 49 6f pMethod = &os2Io
c3dd0 4d 65 74 68 6f 64 3b 0a 20 20 70 46 69 6c 65 2d Method;. pFile-
c3de0 3e 68 20 3d 20 68 3b 0a 20 20 4f 70 65 6e 43 6f >h = h;. OpenCo
c3df0 75 6e 74 65 72 28 2b 31 29 3b 0a 20 20 4f 53 54 unter(+1);. OST
c3e00 52 41 43 45 33 28 20 22 4f 50 45 4e 20 25 64 20 RACE3( "OPEN %d
c3e10 70 4f 75 74 46 6c 61 67 73 3d 25 64 5c 6e 22 2c pOutFlags=%d\n",
c3e20 20 70 46 69 6c 65 2d 3e 68 2c 20 70 4f 75 74 46 pFile->h, pOutF
c3e30 6c 61 67 73 20 29 3b 0a 20 20 72 65 74 75 72 6e lags );. return
c3e40 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f SQLITE_OK;.}../
c3e50 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 74 68 65 20 *.** Delete the
c3e60 6e 61 6d 65 64 20 66 69 6c 65 2e 0a 2a 2f 0a 73 named file..*/.s
c3e70 74 61 74 69 63 20 69 6e 74 20 6f 73 32 44 65 6c tatic int os2Del
c3e80 65 74 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 ete(. sqlite3_v
c3e90 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20 20 fs *pVfs,
c3ea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
c3eb0 20 4e 6f 74 20 75 73 65 64 20 6f 6e 20 6f 73 32 Not used on os2
c3ec0 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 */. const char
c3ed0 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 20 20 *zFilename,
c3ee0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
c3ef0 4e 61 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 Name of file to
c3f00 64 65 6c 65 74 65 20 2a 2f 0a 20 20 69 6e 74 20 delete */. int
c3f10 73 79 6e 63 44 69 72 20 20 20 20 20 20 20 20 20 syncDir
c3f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c3f30 20 20 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20 6f /* Not used o
c3f40 6e 20 6f 73 32 20 2a 2f 0a 29 7b 0a 20 20 41 50 n os2 */.){. AP
c3f50 49 52 45 54 20 72 63 20 3d 20 4e 4f 5f 45 52 52 IRET rc = NO_ERR
c3f60 4f 52 3b 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c OR;. char *zFil
c3f70 65 6e 61 6d 65 43 70 20 3d 20 63 6f 6e 76 65 72 enameCp = conver
c3f80 74 55 74 66 38 50 61 74 68 54 6f 43 70 28 20 7a tUtf8PathToCp( z
c3f90 46 69 6c 65 6e 61 6d 65 20 29 3b 0a 20 20 53 69 Filename );. Si
c3fa0 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72 mulateIOError( r
c3fb0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 eturn SQLITE_IOE
c3fc0 52 52 5f 44 45 4c 45 54 45 20 29 3b 0a 20 20 72 RR_DELETE );. r
c3fd0 63 20 3d 20 44 6f 73 44 65 6c 65 74 65 28 20 28 c = DosDelete( (
c3fe0 50 53 5a 29 7a 46 69 6c 65 6e 61 6d 65 43 70 20 PSZ)zFilenameCp
c3ff0 29 3b 0a 20 20 66 72 65 65 28 20 7a 46 69 6c 65 );. free( zFile
c4000 6e 61 6d 65 43 70 20 29 3b 0a 20 20 4f 53 54 52 nameCp );. OSTR
c4010 41 43 45 32 28 20 22 44 45 4c 45 54 45 20 5c 22 ACE2( "DELETE \"
c4020 25 73 5c 22 5c 6e 22 2c 20 7a 46 69 6c 65 6e 61 %s\"\n", zFilena
c4030 6d 65 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 me );. return r
c4040 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 3f 20 c == NO_ERROR ?
c4050 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 SQLITE_OK : SQLI
c4060 54 45 5f 49 4f 45 52 52 5f 44 45 4c 45 54 45 3b TE_IOERR_DELETE;
c4070 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 .}../*.** Check
c4080 74 68 65 20 65 78 69 73 74 61 6e 63 65 20 61 6e the existance an
c4090 64 20 73 74 61 74 75 73 20 6f 66 20 61 20 66 69 d status of a fi
c40a0 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e le..*/.static in
c40b0 74 20 6f 73 32 41 63 63 65 73 73 28 0a 20 20 73 t os2Access(. s
c40c0 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 qlite3_vfs *pVfs
c40d0 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 , /* Not
c40e0 75 73 65 64 20 6f 6e 20 6f 73 32 20 2a 2f 0a 20 used on os2 */.
c40f0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 const char *zFi
c4100 6c 65 6e 61 6d 65 2c 20 20 20 20 2f 2a 20 4e 61 lename, /* Na
c4110 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 63 68 me of file to ch
c4120 65 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 eck */. int fla
c4130 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 gs,
c4140 20 20 20 2f 2a 20 54 79 70 65 20 6f 66 20 74 65 /* Type of te
c4150 73 74 20 74 6f 20 6d 61 6b 65 20 6f 6e 20 74 68 st to make on th
c4160 69 73 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 is file */. int
c4170 20 2a 70 4f 75 74 20 20 20 20 20 20 20 20 20 20 *pOut
c4180 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 /* Write
c4190 72 65 73 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a results here */.
c41a0 29 7b 0a 20 20 46 49 4c 45 53 54 41 54 55 53 33 ){. FILESTATUS3
c41b0 20 66 73 74 73 33 43 6f 6e 66 69 67 49 6e 66 6f fsts3ConfigInfo
c41c0 3b 0a 20 20 41 50 49 52 45 54 20 72 63 20 3d 20 ;. APIRET rc =
c41d0 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 63 68 61 72 NO_ERROR;. char
c41e0 20 2a 7a 46 69 6c 65 6e 61 6d 65 43 70 20 3d 20 *zFilenameCp =
c41f0 63 6f 6e 76 65 72 74 55 74 66 38 50 61 74 68 54 convertUtf8PathT
c4200 6f 43 70 28 20 7a 46 69 6c 65 6e 61 6d 65 20 29 oCp( zFilename )
c4210 3b 0a 0a 20 20 6d 65 6d 73 65 74 28 20 26 66 73 ;.. memset( &fs
c4220 74 73 33 43 6f 6e 66 69 67 49 6e 66 6f 2c 20 30 ts3ConfigInfo, 0
c4230 2c 20 73 69 7a 65 6f 66 28 66 73 74 73 33 43 6f , sizeof(fsts3Co
c4240 6e 66 69 67 49 6e 66 6f 29 20 29 3b 0a 20 20 72 nfigInfo) );. r
c4250 63 20 3d 20 44 6f 73 51 75 65 72 79 50 61 74 68 c = DosQueryPath
c4260 49 6e 66 6f 28 20 28 50 53 5a 29 7a 46 69 6c 65 Info( (PSZ)zFile
c4270 6e 61 6d 65 43 70 2c 20 46 49 4c 5f 53 54 41 4e nameCp, FIL_STAN
c4280 44 41 52 44 2c 0a 20 20 20 20 20 20 20 20 20 20 DARD,.
c4290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 &
c42a0 66 73 74 73 33 43 6f 6e 66 69 67 49 6e 66 6f 2c fsts3ConfigInfo,
c42b0 20 73 69 7a 65 6f 66 28 46 49 4c 45 53 54 41 54 sizeof(FILESTAT
c42c0 55 53 33 29 20 29 3b 0a 20 20 66 72 65 65 28 20 US3) );. free(
c42d0 7a 46 69 6c 65 6e 61 6d 65 43 70 20 29 3b 0a 20 zFilenameCp );.
c42e0 20 4f 53 54 52 41 43 45 34 28 20 22 41 43 43 45 OSTRACE4( "ACCE
c42f0 53 53 20 66 73 74 73 33 43 6f 6e 66 69 67 49 6e SS fsts3ConfigIn
c4300 66 6f 2e 61 74 74 72 46 69 6c 65 3d 25 64 20 66 fo.attrFile=%d f
c4310 6c 61 67 73 3d 25 64 20 72 63 3d 25 64 5c 6e 22 lags=%d rc=%d\n"
c4320 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 66 73 ,. fs
c4330 74 73 33 43 6f 6e 66 69 67 49 6e 66 6f 2e 61 74 ts3ConfigInfo.at
c4340 74 72 46 69 6c 65 2c 20 66 6c 61 67 73 2c 20 72 trFile, flags, r
c4350 63 20 29 3b 0a 20 20 73 77 69 74 63 68 28 20 66 c );. switch( f
c4360 6c 61 67 73 20 29 7b 0a 20 20 20 20 63 61 73 65 lags ){. case
c4370 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 SQLITE_ACCESS_R
c4380 45 41 44 3a 0a 20 20 20 20 63 61 73 65 20 53 51 EAD:. case SQ
c4390 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58 49 53 LITE_ACCESS_EXIS
c43a0 54 53 3a 0a 20 20 20 20 20 20 72 63 20 3d 20 28 TS:. rc = (
c43b0 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 29 3b rc == NO_ERROR);
c43c0 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 33 28 . OSTRACE3(
c43d0 20 22 41 43 43 45 53 53 20 25 73 20 61 63 63 65 "ACCESS %s acce
c43e0 73 73 20 6f 66 20 72 65 61 64 20 61 6e 64 20 65 ss of read and e
c43f0 78 69 73 74 73 20 20 72 63 3d 25 64 5c 6e 22 2c xists rc=%d\n",
c4400 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 72 63 20 29 zFilename, rc )
c4410 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
c4420 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 41 case SQLITE_A
c4430 43 43 45 53 53 5f 52 45 41 44 57 52 49 54 45 3a CCESS_READWRITE:
c4440 0a 20 20 20 20 20 20 72 63 20 3d 20 28 72 63 20 . rc = (rc
c4450 3d 3d 20 4e 4f 5f 45 52 52 4f 52 29 20 26 26 20 == NO_ERROR) &&
c4460 28 20 28 66 73 74 73 33 43 6f 6e 66 69 67 49 6e ( (fsts3ConfigIn
c4470 66 6f 2e 61 74 74 72 46 69 6c 65 20 26 20 46 49 fo.attrFile & FI
c4480 4c 45 5f 52 45 41 44 4f 4e 4c 59 29 20 3d 3d 20 LE_READONLY) ==
c4490 30 20 29 3b 0a 20 20 20 20 20 20 4f 53 54 52 41 0 );. OSTRA
c44a0 43 45 33 28 20 22 41 43 43 45 53 53 20 25 73 20 CE3( "ACCESS %s
c44b0 61 63 63 65 73 73 20 6f 66 20 72 65 61 64 2f 77 access of read/w
c44c0 72 69 74 65 20 20 72 63 3d 25 64 5c 6e 22 2c 20 rite rc=%d\n",
c44d0 7a 46 69 6c 65 6e 61 6d 65 2c 20 72 63 20 29 3b zFilename, rc );
c44e0 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
c44f0 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20 20 20 default:.
c4500 20 61 73 73 65 72 74 28 20 21 22 49 6e 76 61 6c assert( !"Inval
c4510 69 64 20 66 6c 61 67 73 20 61 72 67 75 6d 65 6e id flags argumen
c4520 74 22 20 29 3b 0a 20 20 7d 0a 20 20 2a 70 4f 75 t" );. }. *pOu
c4530 74 20 3d 20 72 63 3b 0a 20 20 72 65 74 75 72 6e t = rc;. return
c4540 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a SQLITE_OK;.}...
c4550 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
c4560 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 MIT_LOAD_EXTENSI
c4570 4f 4e 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 66 61 ON./*.** Interfa
c4580 63 65 73 20 66 6f 72 20 6f 70 65 6e 69 6e 67 20 ces for opening
c4590 61 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 a shared library
c45a0 2c 20 66 69 6e 64 69 6e 67 20 65 6e 74 72 79 20 , finding entry
c45b0 70 6f 69 6e 74 73 0a 2a 2a 20 77 69 74 68 69 6e points.** within
c45c0 20 74 68 65 20 73 68 61 72 65 64 20 6c 69 62 72 the shared libr
c45d0 61 72 79 2c 20 61 6e 64 20 63 6c 6f 73 69 6e 67 ary, and closing
c45e0 20 74 68 65 20 73 68 61 72 65 64 20 6c 69 62 72 the shared libr
c45f0 61 72 79 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a 20 49 6e ary..*/./*.** In
c4600 74 65 72 66 61 63 65 73 20 66 6f 72 20 6f 70 65 terfaces for ope
c4610 6e 69 6e 67 20 61 20 73 68 61 72 65 64 20 6c 69 ning a shared li
c4620 62 72 61 72 79 2c 20 66 69 6e 64 69 6e 67 20 65 brary, finding e
c4630 6e 74 72 79 20 70 6f 69 6e 74 73 0a 2a 2a 20 77 ntry points.** w
c4640 69 74 68 69 6e 20 74 68 65 20 73 68 61 72 65 64 ithin the shared
c4650 20 6c 69 62 72 61 72 79 2c 20 61 6e 64 20 63 6c library, and cl
c4660 6f 73 69 6e 67 20 74 68 65 20 73 68 61 72 65 64 osing the shared
c4670 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 73 74 61 library..*/.sta
c4680 74 69 63 20 76 6f 69 64 20 2a 6f 73 32 44 6c 4f tic void *os2DlO
c4690 70 65 6e 28 73 71 6c 69 74 65 33 5f 76 66 73 20 pen(sqlite3_vfs
c46a0 2a 70 56 66 73 2c 20 63 6f 6e 73 74 20 63 68 61 *pVfs, const cha
c46b0 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 r *zFilename){.
c46c0 20 55 43 48 41 52 20 6c 6f 61 64 45 72 72 5b 32 UCHAR loadErr[2
c46d0 35 36 5d 3b 0a 20 20 48 4d 4f 44 55 4c 45 20 68 56];. HMODULE h
c46e0 6d 6f 64 3b 0a 20 20 41 50 49 52 45 54 20 72 63 mod;. APIRET rc
c46f0 3b 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e ;. char *zFilen
c4700 61 6d 65 43 70 20 3d 20 63 6f 6e 76 65 72 74 55 ameCp = convertU
c4710 74 66 38 50 61 74 68 54 6f 43 70 28 7a 46 69 6c tf8PathToCp(zFil
c4720 65 6e 61 6d 65 29 3b 0a 20 20 72 63 20 3d 20 44 ename);. rc = D
c4730 6f 73 4c 6f 61 64 4d 6f 64 75 6c 65 28 28 50 53 osLoadModule((PS
c4740 5a 29 6c 6f 61 64 45 72 72 2c 20 73 69 7a 65 6f Z)loadErr, sizeo
c4750 66 28 6c 6f 61 64 45 72 72 29 2c 20 7a 46 69 6c f(loadErr), zFil
c4760 65 6e 61 6d 65 43 70 2c 20 26 68 6d 6f 64 29 3b enameCp, &hmod);
c4770 0a 20 20 66 72 65 65 28 7a 46 69 6c 65 6e 61 6d . free(zFilenam
c4780 65 43 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 eCp);. return r
c4790 63 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 3f 20 c != NO_ERROR ?
c47a0 30 20 3a 20 28 76 6f 69 64 2a 29 68 6d 6f 64 3b 0 : (void*)hmod;
c47b0 0a 7d 0a 2f 2a 0a 2a 2a 20 41 20 6e 6f 2d 6f 70 .}./*.** A no-op
c47c0 20 73 69 6e 63 65 20 74 68 65 20 65 72 72 6f 72 since the error
c47d0 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 code is returne
c47e0 64 20 6f 6e 20 74 68 65 20 44 6f 73 4c 6f 61 64 d on the DosLoad
c47f0 4d 6f 64 75 6c 65 20 63 61 6c 6c 2e 0a 2a 2a 20 Module call..**
c4800 6f 73 32 44 6c 6f 70 65 6e 20 72 65 74 75 72 6e os2Dlopen return
c4810 73 20 7a 65 72 6f 20 69 66 20 44 6f 73 4c 6f 61 s zero if DosLoa
c4820 64 4d 6f 64 75 6c 65 20 69 73 20 6e 6f 74 20 73 dModule is not s
c4830 75 63 63 65 73 73 66 75 6c 2e 0a 2a 2f 0a 73 74 uccessful..*/.st
c4840 61 74 69 63 20 76 6f 69 64 20 6f 73 32 44 6c 45 atic void os2DlE
c4850 72 72 6f 72 28 73 71 6c 69 74 65 33 5f 76 66 73 rror(sqlite3_vfs
c4860 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42 75 66 *pVfs, int nBuf
c4870 2c 20 63 68 61 72 20 2a 7a 42 75 66 4f 75 74 29 , char *zBufOut)
c4880 7b 0a 2f 2a 20 6e 6f 2d 6f 70 20 2a 2f 0a 7d 0a {./* no-op */.}.
c4890 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6f 73 32 static void *os2
c48a0 44 6c 53 79 6d 28 73 71 6c 69 74 65 33 5f 76 66 DlSym(sqlite3_vf
c48b0 73 20 2a 70 56 66 73 2c 20 76 6f 69 64 20 2a 70 s *pVfs, void *p
c48c0 48 61 6e 64 6c 65 2c 20 63 6f 6e 73 74 20 63 68 Handle, const ch
c48d0 61 72 20 2a 7a 53 79 6d 62 6f 6c 29 7b 0a 20 20 ar *zSymbol){.
c48e0 50 46 4e 20 70 66 6e 3b 0a 20 20 41 50 49 52 45 PFN pfn;. APIRE
c48f0 54 20 72 63 3b 0a 20 20 72 63 20 3d 20 44 6f 73 T rc;. rc = Dos
c4900 51 75 65 72 79 50 72 6f 63 41 64 64 72 28 28 48 QueryProcAddr((H
c4910 4d 4f 44 55 4c 45 29 70 48 61 6e 64 6c 65 2c 20 MODULE)pHandle,
c4920 30 4c 2c 20 7a 53 79 6d 62 6f 6c 2c 20 26 70 66 0L, zSymbol, &pf
c4930 6e 29 3b 0a 20 20 69 66 28 20 72 63 20 21 3d 20 n);. if( rc !=
c4940 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 NO_ERROR ){.
c4950 2f 2a 20 69 66 20 74 68 65 20 73 79 6d 62 6f 6c /* if the symbol
c4960 20 69 74 73 65 6c 66 20 77 61 73 20 6e 6f 74 20 itself was not
c4970 66 6f 75 6e 64 2c 20 73 65 61 72 63 68 20 61 67 found, search ag
c4980 61 69 6e 20 66 6f 72 20 74 68 65 20 73 61 6d 65 ain for the same
c4990 0a 20 20 20 20 20 2a 20 73 79 6d 62 6f 6c 20 77 . * symbol w
c49a0 69 74 68 20 61 6e 20 65 78 74 72 61 20 75 6e 64 ith an extra und
c49b0 65 72 73 63 6f 72 65 2c 20 74 68 61 74 20 6d 69 erscore, that mi
c49c0 67 68 74 20 62 65 20 6e 65 65 64 65 64 20 64 65 ght be needed de
c49d0 70 65 6e 64 69 6e 67 0a 20 20 20 20 20 2a 20 6f pending. * o
c49e0 6e 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 63 6f n the calling co
c49f0 6e 76 65 6e 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 nvention */.
c4a00 63 68 61 72 20 5f 7a 53 79 6d 62 6f 6c 5b 32 35 char _zSymbol[25
c4a10 36 5d 20 3d 20 22 5f 22 3b 0a 20 20 20 20 73 74 6] = "_";. st
c4a20 72 6e 63 61 74 28 5f 7a 53 79 6d 62 6f 6c 2c 20 rncat(_zSymbol,
c4a30 7a 53 79 6d 62 6f 6c 2c 20 32 35 35 29 3b 0a 20 zSymbol, 255);.
c4a40 20 20 20 72 63 20 3d 20 44 6f 73 51 75 65 72 79 rc = DosQuery
c4a50 50 72 6f 63 41 64 64 72 28 28 48 4d 4f 44 55 4c ProcAddr((HMODUL
c4a60 45 29 70 48 61 6e 64 6c 65 2c 20 30 4c 2c 20 5f E)pHandle, 0L, _
c4a70 7a 53 79 6d 62 6f 6c 2c 20 26 70 66 6e 29 3b 0a zSymbol, &pfn);.
c4a80 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 20 }. return rc
c4a90 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 3f 20 30 20 != NO_ERROR ? 0
c4aa0 3a 20 28 76 6f 69 64 2a 29 70 66 6e 3b 0a 7d 0a : (void*)pfn;.}.
c4ab0 73 74 61 74 69 63 20 76 6f 69 64 20 6f 73 32 44 static void os2D
c4ac0 6c 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 76 lClose(sqlite3_v
c4ad0 66 73 20 2a 70 56 66 73 2c 20 76 6f 69 64 20 2a fs *pVfs, void *
c4ae0 70 48 61 6e 64 6c 65 29 7b 0a 20 20 44 6f 73 46 pHandle){. DosF
c4af0 72 65 65 4d 6f 64 75 6c 65 28 28 48 4d 4f 44 55 reeModule((HMODU
c4b00 4c 45 29 70 48 61 6e 64 6c 65 29 3b 0a 7d 0a 23 LE)pHandle);.}.#
c4b10 65 6c 73 65 20 2f 2a 20 69 66 20 53 51 4c 49 54 else /* if SQLIT
c4b20 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 E_OMIT_LOAD_EXTE
c4b30 4e 53 49 4f 4e 20 69 73 20 64 65 66 69 6e 65 64 NSION is defined
c4b40 3a 20 2a 2f 0a 20 20 23 64 65 66 69 6e 65 20 6f : */. #define o
c4b50 73 32 44 6c 4f 70 65 6e 20 30 0a 20 20 23 64 65 s2DlOpen 0. #de
c4b60 66 69 6e 65 20 6f 73 32 44 6c 45 72 72 6f 72 20 fine os2DlError
c4b70 30 0a 20 20 23 64 65 66 69 6e 65 20 6f 73 32 44 0. #define os2D
c4b80 6c 53 79 6d 20 30 0a 20 20 23 64 65 66 69 6e 65 lSym 0. #define
c4b90 20 6f 73 32 44 6c 43 6c 6f 73 65 20 30 0a 23 65 os2DlClose 0.#e
c4ba0 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 ndif.../*.** Wri
c4bb0 74 65 20 75 70 20 74 6f 20 6e 42 75 66 20 62 79 te up to nBuf by
c4bc0 74 65 73 20 6f 66 20 72 61 6e 64 6f 6d 6e 65 73 tes of randomnes
c4bd0 73 20 69 6e 74 6f 20 7a 42 75 66 2e 0a 2a 2f 0a s into zBuf..*/.
c4be0 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 52 61 static int os2Ra
c4bf0 6e 64 6f 6d 6e 65 73 73 28 73 71 6c 69 74 65 33 ndomness(sqlite3
c4c00 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 _vfs *pVfs, int
c4c10 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 nBuf, char *zBuf
c4c20 20 29 7b 0a 20 20 69 6e 74 20 6e 20 3d 20 30 3b ){. int n = 0;
c4c30 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c .#if defined(SQL
c4c40 49 54 45 5f 54 45 53 54 29 0a 20 20 6e 20 3d 20 ITE_TEST). n =
c4c50 6e 42 75 66 3b 0a 20 20 6d 65 6d 73 65 74 28 7a nBuf;. memset(z
c4c60 42 75 66 2c 20 30 2c 20 6e 42 75 66 29 3b 0a 23 Buf, 0, nBuf);.#
c4c70 65 6c 73 65 0a 20 20 69 6e 74 20 73 69 7a 65 6f else. int sizeo
c4c80 66 55 4c 6f 6e 67 20 3d 20 73 69 7a 65 6f 66 28 fULong = sizeof(
c4c90 55 4c 4f 4e 47 29 3b 0a 20 20 69 66 28 20 28 69 ULONG);. if( (i
c4ca0 6e 74 29 73 69 7a 65 6f 66 28 44 41 54 45 54 49 nt)sizeof(DATETI
c4cb0 4d 45 29 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 ME) <= nBuf - n
c4cc0 29 7b 0a 20 20 20 20 44 41 54 45 54 49 4d 45 20 ){. DATETIME
c4cd0 78 3b 0a 20 20 20 20 44 6f 73 47 65 74 44 61 74 x;. DosGetDat
c4ce0 65 54 69 6d 65 28 26 78 29 3b 0a 20 20 20 20 6d eTime(&x);. m
c4cf0 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 emcpy(&zBuf[n],
c4d00 26 78 2c 20 73 69 7a 65 6f 66 28 78 29 29 3b 0a &x, sizeof(x));.
c4d10 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 28 n += sizeof(
c4d20 78 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 73 x);. }.. if( s
c4d30 69 7a 65 6f 66 55 4c 6f 6e 67 20 3c 3d 20 6e 42 izeofULong <= nB
c4d40 75 66 20 2d 20 6e 20 29 7b 0a 20 20 20 20 50 50 uf - n ){. PP
c4d50 49 42 20 70 70 69 62 3b 0a 20 20 20 20 44 6f 73 IB ppib;. Dos
c4d60 47 65 74 49 6e 66 6f 42 6c 6f 63 6b 73 28 4e 55 GetInfoBlocks(NU
c4d70 4c 4c 2c 20 26 70 70 69 62 29 3b 0a 20 20 20 20 LL, &ppib);.
c4d80 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d 2c memcpy(&zBuf[n],
c4d90 20 26 70 70 69 62 2d 3e 70 69 62 5f 75 6c 70 69 &ppib->pib_ulpi
c4da0 64 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 29 3b d, sizeofULong);
c4db0 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 . n += sizeof
c4dc0 55 4c 6f 6e 67 3b 0a 20 20 7d 0a 0a 20 20 69 66 ULong;. }.. if
c4dd0 28 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 20 3c 3d ( sizeofULong <=
c4de0 20 6e 42 75 66 20 2d 20 6e 20 29 7b 0a 20 20 20 nBuf - n ){.
c4df0 20 50 54 49 42 20 70 74 69 62 3b 0a 20 20 20 20 PTIB ptib;.
c4e00 44 6f 73 47 65 74 49 6e 66 6f 42 6c 6f 63 6b 73 DosGetInfoBlocks
c4e10 28 26 70 74 69 62 2c 20 4e 55 4c 4c 29 3b 0a 20 (&ptib, NULL);.
c4e20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b memcpy(&zBuf[
c4e30 6e 5d 2c 20 26 70 74 69 62 2d 3e 74 69 62 5f 70 n], &ptib->tib_p
c4e40 74 69 62 32 2d 3e 74 69 62 32 5f 75 6c 74 69 64 tib2->tib2_ultid
c4e50 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 29 3b 0a , sizeofULong);.
c4e60 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 55 n += sizeofU
c4e70 4c 6f 6e 67 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 Long;. }.. /*
c4e80 69 66 20 77 65 20 73 74 69 6c 6c 20 68 61 76 65 if we still have
c4e90 6e 27 74 20 66 69 6c 6c 65 64 20 74 68 65 20 62 n't filled the b
c4ea0 75 66 66 65 72 20 79 65 74 20 74 68 65 20 66 6f uffer yet the fo
c4eb0 6c 6c 6f 77 69 6e 67 20 77 69 6c 6c 20 2a 2f 0a llowing will */.
c4ec0 20 20 2f 2a 20 67 72 61 62 20 65 76 65 72 79 74 /* grab everyt
c4ed0 68 69 6e 67 20 6f 6e 63 65 20 69 6e 73 74 65 61 hing once instea
c4ee0 64 20 6f 66 20 6d 61 6b 69 6e 67 20 73 65 76 65 d of making seve
c4ef0 72 61 6c 20 63 61 6c 6c 73 20 66 6f 72 20 61 20 ral calls for a
c4f00 73 69 6e 67 6c 65 20 69 74 65 6d 20 2a 2f 0a 20 single item */.
c4f10 20 69 66 28 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 if( sizeofULong
c4f20 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29 7b 0a <= nBuf - n ){.
c4f30 20 20 20 20 55 4c 4f 4e 47 20 75 6c 53 79 73 49 ULONG ulSysI
c4f40 6e 66 6f 5b 51 53 56 5f 4d 41 58 5d 3b 0a 20 20 nfo[QSV_MAX];.
c4f50 20 20 44 6f 73 51 75 65 72 79 53 79 73 49 6e 66 DosQuerySysInf
c4f60 6f 28 31 4c 2c 20 51 53 56 5f 4d 41 58 2c 20 75 o(1L, QSV_MAX, u
c4f70 6c 53 79 73 49 6e 66 6f 2c 20 73 69 7a 65 6f 66 lSysInfo, sizeof
c4f80 55 4c 6f 6e 67 20 2a 20 51 53 56 5f 4d 41 58 29 ULong * QSV_MAX)
c4f90 3b 0a 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 7a ;.. memcpy(&z
c4fa0 42 75 66 5b 6e 5d 2c 20 26 75 6c 53 79 73 49 6e Buf[n], &ulSysIn
c4fb0 66 6f 5b 51 53 56 5f 4d 53 5f 43 4f 55 4e 54 20 fo[QSV_MS_COUNT
c4fc0 2d 20 31 5d 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e - 1], sizeofULon
c4fd0 67 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a g);. n += siz
c4fe0 65 6f 66 55 4c 6f 6e 67 3b 0a 0a 20 20 20 20 69 eofULong;.. i
c4ff0 66 28 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 20 3c f( sizeofULong <
c5000 3d 20 6e 42 75 66 20 2d 20 6e 20 29 7b 0a 20 20 = nBuf - n ){.
c5010 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 memcpy(&zBuf
c5020 5b 6e 5d 2c 20 26 75 6c 53 79 73 49 6e 66 6f 5b [n], &ulSysInfo[
c5030 51 53 56 5f 54 49 4d 45 52 5f 49 4e 54 45 52 56 QSV_TIMER_INTERV
c5040 41 4c 20 2d 20 31 5d 2c 20 73 69 7a 65 6f 66 55 AL - 1], sizeofU
c5050 4c 6f 6e 67 29 3b 0a 20 20 20 20 20 20 6e 20 2b Long);. n +
c5060 3d 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 3b 0a 20 = sizeofULong;.
c5070 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 69 7a }. if( siz
c5080 65 6f 66 55 4c 6f 6e 67 20 3c 3d 20 6e 42 75 66 eofULong <= nBuf
c5090 20 2d 20 6e 20 29 7b 0a 20 20 20 20 20 20 6d 65 - n ){. me
c50a0 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 mcpy(&zBuf[n], &
c50b0 75 6c 53 79 73 49 6e 66 6f 5b 51 53 56 5f 54 49 ulSysInfo[QSV_TI
c50c0 4d 45 5f 4c 4f 57 20 2d 20 31 5d 2c 20 73 69 7a ME_LOW - 1], siz
c50d0 65 6f 66 55 4c 6f 6e 67 29 3b 0a 20 20 20 20 20 eofULong);.
c50e0 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 55 4c 6f 6e n += sizeofULon
c50f0 67 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 g;. }. if(
c5100 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 20 3c 3d 20 sizeofULong <=
c5110 6e 42 75 66 20 2d 20 6e 20 29 7b 0a 20 20 20 20 nBuf - n ){.
c5120 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e memcpy(&zBuf[n
c5130 5d 2c 20 26 75 6c 53 79 73 49 6e 66 6f 5b 51 53 ], &ulSysInfo[QS
c5140 56 5f 54 49 4d 45 5f 48 49 47 48 20 2d 20 31 5d V_TIME_HIGH - 1]
c5150 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 29 3b 0a , sizeofULong);.
c5160 20 20 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f n += sizeo
c5170 66 55 4c 6f 6e 67 3b 0a 20 20 20 20 7d 0a 20 20 fULong;. }.
c5180 20 20 69 66 28 20 73 69 7a 65 6f 66 55 4c 6f 6e if( sizeofULon
c5190 67 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29 7b g <= nBuf - n ){
c51a0 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 7a . memcpy(&z
c51b0 42 75 66 5b 6e 5d 2c 20 26 75 6c 53 79 73 49 6e Buf[n], &ulSysIn
c51c0 66 6f 5b 51 53 56 5f 54 4f 54 41 56 41 49 4c 4d fo[QSV_TOTAVAILM
c51d0 45 4d 20 2d 20 31 5d 2c 20 73 69 7a 65 6f 66 55 EM - 1], sizeofU
c51e0 4c 6f 6e 67 29 3b 0a 20 20 20 20 20 20 6e 20 2b Long);. n +
c51f0 3d 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 3b 0a 20 = sizeofULong;.
c5200 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a }. }.#endif.
c5210 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a . return n;.}..
c5220 2f 2a 0a 2a 2a 20 53 6c 65 65 70 20 66 6f 72 20 /*.** Sleep for
c5230 61 20 6c 69 74 74 6c 65 20 77 68 69 6c 65 2e 20 a little while.
c5240 20 52 65 74 75 72 6e 20 74 68 65 20 61 6d 6f 75 Return the amou
c5250 6e 74 20 6f 66 20 74 69 6d 65 20 73 6c 65 70 74 nt of time slept
c5260 2e 0a 2a 2a 20 54 68 65 20 61 72 67 75 6d 65 6e ..** The argumen
c5270 74 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 t is the number
c5280 6f 66 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73 20 of microseconds
c5290 77 65 20 77 61 6e 74 20 74 6f 20 73 6c 65 65 70 we want to sleep
c52a0 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 20 ..** The return
c52b0 76 61 6c 75 65 20 69 73 20 74 68 65 20 6e 75 6d value is the num
c52c0 62 65 72 20 6f 66 20 6d 69 63 72 6f 73 65 63 6f ber of microseco
c52d0 6e 64 73 20 6f 66 20 73 6c 65 65 70 20 61 63 74 nds of sleep act
c52e0 75 61 6c 6c 79 0a 2a 2a 20 72 65 71 75 65 73 74 ually.** request
c52f0 65 64 20 66 72 6f 6d 20 74 68 65 20 75 6e 64 65 ed from the unde
c5300 72 6c 79 69 6e 67 20 6f 70 65 72 61 74 69 6e 67 rlying operating
c5310 20 73 79 73 74 65 6d 2c 20 61 20 6e 75 6d 62 65 system, a numbe
c5320 72 20 77 68 69 63 68 0a 2a 2a 20 6d 69 67 68 74 r which.** might
c5330 20 62 65 20 67 72 65 61 74 65 72 20 74 68 61 6e be greater than
c5340 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65 or equal to the
c5350 20 61 72 67 75 6d 65 6e 74 2c 20 62 75 74 20 6e argument, but n
c5360 6f 74 20 6c 65 73 73 0a 2a 2a 20 74 68 61 6e 20 ot less.** than
c5370 74 68 65 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f the argument..*/
c5380 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 53 .static int os2S
c5390 6c 65 65 70 28 20 73 71 6c 69 74 65 33 5f 76 66 leep( sqlite3_vf
c53a0 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6d 69 63 s *pVfs, int mic
c53b0 72 6f 73 65 63 20 29 7b 0a 20 20 44 6f 73 53 6c rosec ){. DosSl
c53c0 65 65 70 28 20 28 6d 69 63 72 6f 73 65 63 2f 31 eep( (microsec/1
c53d0 30 30 30 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 000) );. return
c53e0 20 6d 69 63 72 6f 73 65 63 3b 0a 7d 0a 0a 2f 2a microsec;.}../*
c53f0 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e .** The followin
c5400 67 20 76 61 72 69 61 62 6c 65 2c 20 69 66 20 73 g variable, if s
c5410 65 74 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f et to a non-zero
c5420 20 76 61 6c 75 65 2c 20 62 65 63 6f 6d 65 73 20 value, becomes
c5430 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 72 65 the result.** re
c5440 74 75 72 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 turned from sqli
c5450 74 65 33 4f 73 43 75 72 72 65 6e 74 54 69 6d 65 te3OsCurrentTime
c5460 28 29 2e 20 20 54 68 69 73 20 69 73 20 75 73 65 (). This is use
c5470 64 20 66 6f 72 20 74 65 73 74 69 6e 67 2e 0a 2a d for testing..*
c5480 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
c5490 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 TEST.SQLITE_API
c54a0 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 75 72 72 int sqlite3_curr
c54b0 65 6e 74 5f 74 69 6d 65 20 3d 20 30 3b 0a 23 65 ent_time = 0;.#e
c54c0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 ndif../*.** Find
c54d0 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 69 6d the current tim
c54e0 65 20 28 69 6e 20 55 6e 69 76 65 72 73 61 6c 20 e (in Universal
c54f0 43 6f 6f 72 64 69 6e 61 74 65 64 20 54 69 6d 65 Coordinated Time
c5500 29 2e 20 20 57 72 69 74 65 20 74 68 65 0a 2a 2a ). Write the.**
c5510 20 63 75 72 72 65 6e 74 20 74 69 6d 65 20 61 6e current time an
c5520 64 20 64 61 74 65 20 61 73 20 61 20 4a 75 6c 69 d date as a Juli
c5530 61 6e 20 44 61 79 20 6e 75 6d 62 65 72 20 69 6e an Day number in
c5540 74 6f 20 2a 70 72 4e 6f 77 20 61 6e 64 0a 2a 2a to *prNow and.**
c5550 20 72 65 74 75 72 6e 20 30 2e 20 20 52 65 74 75 return 0. Retu
c5560 72 6e 20 31 20 69 66 20 74 68 65 20 74 69 6d 65 rn 1 if the time
c5570 20 61 6e 64 20 64 61 74 65 20 63 61 6e 6e 6f 74 and date cannot
c5580 20 62 65 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 69 6e be found..*/.in
c5590 74 20 6f 73 32 43 75 72 72 65 6e 74 54 69 6d 65 t os2CurrentTime
c55a0 28 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 ( sqlite3_vfs *p
c55b0 56 66 73 2c 20 64 6f 75 62 6c 65 20 2a 70 72 4e Vfs, double *prN
c55c0 6f 77 20 29 7b 0a 20 20 64 6f 75 62 6c 65 20 6e ow ){. double n
c55d0 6f 77 3b 0a 20 20 53 48 4f 52 54 20 6d 69 6e 75 ow;. SHORT minu
c55e0 74 65 3b 20 2f 2a 20 6e 65 65 64 73 20 74 6f 20 te; /* needs to
c55f0 62 65 20 61 62 6c 65 20 74 6f 20 63 6f 70 65 20 be able to cope
c5600 77 69 74 68 20 6e 65 67 61 74 69 76 65 20 74 69 with negative ti
c5610 6d 65 7a 6f 6e 65 20 6f 66 66 73 65 74 20 2a 2f mezone offset */
c5620 0a 20 20 55 53 48 4f 52 54 20 73 65 63 6f 6e 64 . USHORT second
c5630 2c 20 68 6f 75 72 2c 0a 20 20 20 20 20 20 20 20 , hour,.
c5640 20 64 61 79 2c 20 6d 6f 6e 74 68 2c 20 79 65 61 day, month, yea
c5650 72 3b 0a 20 20 44 41 54 45 54 49 4d 45 20 64 74 r;. DATETIME dt
c5660 3b 0a 20 20 44 6f 73 47 65 74 44 61 74 65 54 69 ;. DosGetDateTi
c5670 6d 65 28 20 26 64 74 20 29 3b 0a 20 20 73 65 63 me( &dt );. sec
c5680 6f 6e 64 20 3d 20 28 55 53 48 4f 52 54 29 64 74 ond = (USHORT)dt
c5690 2e 73 65 63 6f 6e 64 73 3b 0a 20 20 6d 69 6e 75 .seconds;. minu
c56a0 74 65 20 3d 20 28 53 48 4f 52 54 29 64 74 2e 6d te = (SHORT)dt.m
c56b0 69 6e 75 74 65 73 20 2b 20 64 74 2e 74 69 6d 65 inutes + dt.time
c56c0 7a 6f 6e 65 3b 0a 20 20 68 6f 75 72 20 3d 20 28 zone;. hour = (
c56d0 55 53 48 4f 52 54 29 64 74 2e 68 6f 75 72 73 3b USHORT)dt.hours;
c56e0 0a 20 20 64 61 79 20 3d 20 28 55 53 48 4f 52 54 . day = (USHORT
c56f0 29 64 74 2e 64 61 79 3b 0a 20 20 6d 6f 6e 74 68 )dt.day;. month
c5700 20 3d 20 28 55 53 48 4f 52 54 29 64 74 2e 6d 6f = (USHORT)dt.mo
c5710 6e 74 68 3b 0a 20 20 79 65 61 72 20 3d 20 28 55 nth;. year = (U
c5720 53 48 4f 52 54 29 64 74 2e 79 65 61 72 3b 0a 0a SHORT)dt.year;..
c5730 20 20 2f 2a 20 43 61 6c 63 75 6c 61 74 69 6f 6e /* Calculation
c5740 73 20 66 72 6f 6d 20 68 74 74 70 3a 2f 2f 77 77 s from http://ww
c5750 77 2e 61 73 74 72 6f 2e 6b 65 65 6c 65 2e 61 63 w.astro.keele.ac
c5760 2e 75 6b 2f 7e 72 6e 6f 2f 41 73 74 72 6f 6e 6f .uk/~rno/Astrono
c5770 6d 79 2f 68 6a 64 2e 68 74 6d 6c 0a 20 20 20 20 my/hjd.html.
c5780 20 68 74 74 70 3a 2f 2f 77 77 77 2e 61 73 74 72 http://www.astr
c5790 6f 2e 6b 65 65 6c 65 2e 61 63 2e 75 6b 2f 7e 72 o.keele.ac.uk/~r
c57a0 6e 6f 2f 41 73 74 72 6f 6e 6f 6d 79 2f 68 6a 64 no/Astronomy/hjd
c57b0 2d 30 2e 31 2e 63 20 2a 2f 0a 20 20 2f 2a 20 43 -0.1.c */. /* C
c57c0 61 6c 63 75 6c 61 74 65 20 74 68 65 20 4a 75 6c alculate the Jul
c57d0 69 61 6e 20 64 61 79 73 20 2a 2f 0a 20 20 6e 6f ian days */. no
c57e0 77 20 3d 20 64 61 79 20 2d 20 33 32 30 37 36 20 w = day - 32076
c57f0 2b 0a 20 20 20 20 31 34 36 31 2a 28 79 65 61 72 +. 1461*(year
c5800 20 2b 20 34 38 30 30 20 2b 20 28 6d 6f 6e 74 68 + 4800 + (month
c5810 20 2d 20 31 34 29 2f 31 32 29 2f 34 20 2b 0a 20 - 14)/12)/4 +.
c5820 20 20 20 33 36 37 2a 28 6d 6f 6e 74 68 20 2d 20 367*(month -
c5830 32 20 2d 20 28 6d 6f 6e 74 68 20 2d 20 31 34 29 2 - (month - 14)
c5840 2f 31 32 2a 31 32 29 2f 31 32 20 2d 0a 20 20 20 /12*12)/12 -.
c5850 20 33 2a 28 28 79 65 61 72 20 2b 20 34 39 30 30 3*((year + 4900
c5860 20 2b 20 28 6d 6f 6e 74 68 20 2d 20 31 34 29 2f + (month - 14)/
c5870 31 32 29 2f 31 30 30 29 2f 34 3b 0a 0a 20 20 2f 12)/100)/4;.. /
c5880 2a 20 41 64 64 20 74 68 65 20 66 72 61 63 74 69 * Add the fracti
c5890 6f 6e 61 6c 20 68 6f 75 72 73 2c 20 6d 69 6e 73 onal hours, mins
c58a0 20 61 6e 64 20 73 65 63 6f 6e 64 73 20 2a 2f 0a and seconds */.
c58b0 20 20 6e 6f 77 20 2b 3d 20 28 68 6f 75 72 20 2b now += (hour +
c58c0 20 31 32 2e 30 29 2f 32 34 2e 30 3b 0a 20 20 6e 12.0)/24.0;. n
c58d0 6f 77 20 2b 3d 20 6d 69 6e 75 74 65 2f 31 34 34 ow += minute/144
c58e0 30 2e 30 3b 0a 20 20 6e 6f 77 20 2b 3d 20 73 65 0.0;. now += se
c58f0 63 6f 6e 64 2f 38 36 34 30 30 2e 30 3b 0a 20 20 cond/86400.0;.
c5900 2a 70 72 4e 6f 77 20 3d 20 6e 6f 77 3b 0a 23 69 *prNow = now;.#i
c5910 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 fdef SQLITE_TEST
c5920 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 63 . if( sqlite3_c
c5930 75 72 72 65 6e 74 5f 74 69 6d 65 20 29 7b 0a 20 urrent_time ){.
c5940 20 20 20 2a 70 72 4e 6f 77 20 3d 20 73 71 6c 69 *prNow = sqli
c5950 74 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 te3_current_time
c5960 2f 38 36 34 30 30 2e 30 20 2b 20 32 34 34 30 35 /86400.0 + 24405
c5970 38 37 2e 35 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 87.5;. }.#endif
c5980 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a . return 0;.}..
c5990 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 47 65 static int os2Ge
c59a0 74 4c 61 73 74 45 72 72 6f 72 28 73 71 6c 69 74 tLastError(sqlit
c59b0 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e e3_vfs *pVfs, in
c59c0 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 t nBuf, char *zB
c59d0 75 66 29 7b 0a 20 20 72 65 74 75 72 6e 20 30 3b uf){. return 0;
c59e0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 .}../*.** Initia
c59f0 6c 69 7a 65 20 61 6e 64 20 64 65 69 6e 69 74 69 lize and deiniti
c5a00 61 6c 69 7a 65 20 74 68 65 20 6f 70 65 72 61 74 alize the operat
c5a10 69 6e 67 20 73 79 73 74 65 6d 20 69 6e 74 65 72 ing system inter
c5a20 66 61 63 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f face..*/.SQLITE_
c5a30 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
c5a40 6f 73 5f 69 6e 69 74 28 76 6f 69 64 29 7b 0a 20 os_init(void){.
c5a50 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f static sqlite3_
c5a60 76 66 73 20 6f 73 32 56 66 73 20 3d 20 7b 0a 20 vfs os2Vfs = {.
c5a70 20 20 20 31 2c 20 20 20 20 20 20 20 20 20 20 20 1,
c5a80 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f /* iVersio
c5a90 6e 20 2a 2f 0a 20 20 20 20 73 69 7a 65 6f 66 28 n */. sizeof(
c5aa0 6f 73 32 46 69 6c 65 29 2c 20 20 20 2f 2a 20 73 os2File), /* s
c5ab0 7a 4f 73 46 69 6c 65 20 2a 2f 0a 20 20 20 20 43 zOsFile */. C
c5ac0 43 48 4d 41 58 50 41 54 48 2c 20 20 20 20 20 20 CHMAXPATH,
c5ad0 20 20 2f 2a 20 6d 78 50 61 74 68 6e 61 6d 65 20 /* mxPathname
c5ae0 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 */. 0,
c5af0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 4e 65 /* pNe
c5b00 78 74 20 2a 2f 0a 20 20 20 20 22 6f 73 32 22 2c xt */. "os2",
c5b10 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
c5b20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 20 20 30 2c 20 zName */. 0,
c5b30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c5b40 2f 2a 20 70 41 70 70 44 61 74 61 20 2a 2f 0a 0a /* pAppData */..
c5b50 20 20 20 20 6f 73 32 4f 70 65 6e 2c 20 20 20 20 os2Open,
c5b60 20 20 20 20 20 20 20 2f 2a 20 78 4f 70 65 6e 20 /* xOpen
c5b70 2a 2f 0a 20 20 20 20 6f 73 32 44 65 6c 65 74 65 */. os2Delete
c5b80 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 65 , /* xDe
c5b90 6c 65 74 65 20 2a 2f 0a 20 20 20 20 6f 73 32 41 lete */. os2A
c5ba0 63 63 65 73 73 2c 20 20 20 20 20 20 20 20 20 2f ccess, /
c5bb0 2a 20 78 41 63 63 65 73 73 20 2a 2f 0a 20 20 20 * xAccess */.
c5bc0 20 6f 73 32 46 75 6c 6c 50 61 74 68 6e 61 6d 65 os2FullPathname
c5bd0 2c 20 20 20 2f 2a 20 78 46 75 6c 6c 50 61 74 68 , /* xFullPath
c5be0 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 6f 73 32 44 name */. os2D
c5bf0 6c 4f 70 65 6e 2c 20 20 20 20 20 20 20 20 20 2f lOpen, /
c5c00 2a 20 78 44 6c 4f 70 65 6e 20 2a 2f 0a 20 20 20 * xDlOpen */.
c5c10 20 6f 73 32 44 6c 45 72 72 6f 72 2c 20 20 20 20 os2DlError,
c5c20 20 20 20 20 2f 2a 20 78 44 6c 45 72 72 6f 72 20 /* xDlError
c5c30 2a 2f 0a 20 20 20 20 6f 73 32 44 6c 53 79 6d 2c */. os2DlSym,
c5c40 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c /* xDl
c5c50 53 79 6d 20 2a 2f 0a 20 20 20 20 6f 73 32 44 6c Sym */. os2Dl
c5c60 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 2f 2a Close, /*
c5c70 20 78 44 6c 43 6c 6f 73 65 20 2a 2f 0a 20 20 20 xDlClose */.
c5c80 20 6f 73 32 52 61 6e 64 6f 6d 6e 65 73 73 2c 20 os2Randomness,
c5c90 20 20 20 20 2f 2a 20 78 52 61 6e 64 6f 6d 6e 65 /* xRandomne
c5ca0 73 73 20 2a 2f 0a 20 20 20 20 6f 73 32 53 6c 65 ss */. os2Sle
c5cb0 65 70 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ep, /*
c5cc0 78 53 6c 65 65 70 20 2a 2f 0a 20 20 20 20 6f 73 xSleep */. os
c5cd0 32 43 75 72 72 65 6e 74 54 69 6d 65 2c 20 20 20 2CurrentTime,
c5ce0 20 2f 2a 20 78 43 75 72 72 65 6e 74 54 69 6d 65 /* xCurrentTime
c5cf0 20 2a 2f 0a 20 20 20 20 6f 73 32 47 65 74 4c 61 */. os2GetLa
c5d00 73 74 45 72 72 6f 72 20 20 20 20 2f 2a 20 78 47 stError /* xG
c5d10 65 74 4c 61 73 74 45 72 72 6f 72 20 2a 2f 0a 20 etLastError */.
c5d20 20 7d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 };. sqlite3_vf
c5d30 73 5f 72 65 67 69 73 74 65 72 28 26 6f 73 32 56 s_register(&os2V
c5d40 66 73 2c 20 31 29 3b 0a 20 20 69 6e 69 74 55 63 fs, 1);. initUc
c5d50 6f 6e 76 4f 62 6a 65 63 74 73 28 29 3b 0a 20 20 onvObjects();.
c5d60 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
c5d70 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 ;.}.SQLITE_API i
c5d80 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e nt sqlite3_os_en
c5d90 64 28 76 6f 69 64 29 7b 0a 20 20 66 72 65 65 55 d(void){. freeU
c5da0 63 6f 6e 76 4f 62 6a 65 63 74 73 28 29 3b 0a 20 convObjects();.
c5db0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
c5dc0 4b 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 K;.}..#endif /*
c5dd0 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 20 2a 2f SQLITE_OS_OS2 */
c5de0 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
c5df0 2a 20 45 6e 64 20 6f 66 20 6f 73 5f 6f 73 32 2e * End of os_os2.
c5e00 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
c5e10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c5e20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c5e30 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
c5e40 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 5f * Begin file os_
c5e50 75 6e 69 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a unix.c *********
c5e60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c5e70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c5e80 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79 /./*.** 2004 May
c5e90 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 22.**.** The au
c5ea0 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 thor disclaims c
c5eb0 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 opyright to this
c5ec0 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 source code. I
c5ed0 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 n place of.** a
c5ee0 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 legal notice, he
c5ef0 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 re is a blessing
c5f00 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 :.**.** May y
c5f10 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e ou do good and n
c5f20 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d ot evil..** M
c5f30 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 ay you find forg
c5f40 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 iveness for your
c5f50 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 self and forgive
c5f60 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d others..** M
c5f70 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 ay you share fre
c5f80 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e ely, never takin
c5f90 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 g more than you
c5fa0 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a give..**.*******
c5fb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c5fc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c5fd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c5fe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c5ff0 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 *******.**.** Th
c6000 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 is file contains
c6010 20 74 68 65 20 56 46 53 20 69 6d 70 6c 65 6d 65 the VFS impleme
c6020 6e 74 61 74 69 6f 6e 20 66 6f 72 20 75 6e 69 78 ntation for unix
c6030 2d 6c 69 6b 65 20 6f 70 65 72 61 74 69 6e 67 20 -like operating
c6040 73 79 73 74 65 6d 73 0a 2a 2a 20 69 6e 63 6c 75 systems.** inclu
c6050 64 65 20 4c 69 6e 75 78 2c 20 4d 61 63 4f 53 58 de Linux, MacOSX
c6060 2c 20 2a 42 53 44 2c 20 51 4e 58 2c 20 56 78 57 , *BSD, QNX, VxW
c6070 6f 72 6b 73 2c 20 41 49 58 2c 20 48 50 55 58 2c orks, AIX, HPUX,
c6080 20 61 6e 64 20 6f 74 68 65 72 73 2e 0a 2a 2a 0a and others..**.
c6090 2a 2a 20 54 68 65 72 65 20 61 72 65 20 61 63 74 ** There are act
c60a0 75 61 6c 6c 79 20 73 65 76 65 72 61 6c 20 64 69 ually several di
c60b0 66 66 65 72 65 6e 74 20 56 46 53 20 69 6d 70 6c fferent VFS impl
c60c0 65 6d 65 6e 74 61 74 69 6f 6e 73 20 69 6e 20 74 ementations in t
c60d0 68 69 73 20 66 69 6c 65 2e 0a 2a 2a 20 54 68 65 his file..** The
c60e0 20 64 69 66 66 65 72 65 6e 63 65 73 20 61 72 65 differences are
c60f0 20 69 6e 20 74 68 65 20 77 61 79 20 74 68 61 74 in the way that
c6100 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 69 73 file locking is
c6110 20 64 6f 6e 65 2e 20 20 54 68 65 20 64 65 66 61 done. The defa
c6120 75 6c 74 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 ult.** implement
c6130 61 74 69 6f 6e 20 75 73 65 73 20 50 6f 73 69 78 ation uses Posix
c6140 20 41 64 76 69 73 6f 72 79 20 4c 6f 63 6b 73 2e Advisory Locks.
c6150 20 20 41 6c 74 65 72 6e 61 74 69 76 65 20 69 6d Alternative im
c6160 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 0a 2a 2a plementations.**
c6170 20 75 73 65 20 66 6c 6f 63 6b 28 29 2c 20 64 6f use flock(), do
c6180 74 2d 66 69 6c 65 73 2c 20 76 61 72 69 6f 75 73 t-files, various
c6190 20 70 72 6f 70 72 69 65 74 61 72 79 20 6c 6f 63 proprietary loc
c61a0 6b 69 6e 67 20 73 63 68 65 6d 61 73 2c 20 6f 72 king schemas, or
c61b0 20 73 69 6d 70 6c 79 0a 2a 2a 20 73 6b 69 70 20 simply.** skip
c61c0 6c 6f 63 6b 69 6e 67 20 61 6c 6c 20 74 6f 67 65 locking all toge
c61d0 74 68 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 ther..**.** This
c61e0 20 73 6f 75 72 63 65 20 66 69 6c 65 20 69 73 20 source file is
c61f0 6f 72 67 61 6e 69 7a 65 64 20 69 6e 74 6f 20 64 organized into d
c6200 69 76 69 73 69 6f 6e 73 20 77 68 65 72 65 20 74 ivisions where t
c6210 68 65 20 6c 6f 67 69 63 20 66 6f 72 20 76 61 72 he logic for var
c6220 69 6f 75 73 0a 2a 2a 20 73 75 62 66 75 6e 63 74 ious.** subfunct
c6230 69 6f 6e 73 20 69 73 20 63 6f 6e 74 61 69 6e 65 ions is containe
c6240 64 20 77 69 74 68 69 6e 20 74 68 65 20 61 70 70 d within the app
c6250 72 6f 70 72 69 61 74 65 20 64 69 76 69 73 69 6f ropriate divisio
c6260 6e 2e 20 20 50 4c 45 41 53 45 0a 2a 2a 20 4b 45 n. PLEASE.** KE
c6270 45 50 20 54 48 45 20 53 54 52 55 43 54 55 52 45 EP THE STRUCTURE
c6280 20 4f 46 20 54 48 49 53 20 46 49 4c 45 20 49 4e OF THIS FILE IN
c6290 54 41 43 54 2e 20 20 4e 65 77 20 63 6f 64 65 20 TACT. New code
c62a0 73 68 6f 75 6c 64 20 62 65 20 70 6c 61 63 65 64 should be placed
c62b0 0a 2a 2a 20 69 6e 20 74 68 65 20 63 6f 72 72 65 .** in the corre
c62c0 63 74 20 64 69 76 69 73 69 6f 6e 20 61 6e 64 20 ct division and
c62d0 73 68 6f 75 6c 64 20 62 65 20 63 6c 65 61 72 6c should be clearl
c62e0 79 20 6c 61 62 65 6c 65 64 2e 0a 2a 2a 0a 2a 2a y labeled..**.**
c62f0 20 54 68 65 20 6c 61 79 6f 75 74 20 6f 66 20 64 The layout of d
c6300 69 76 69 73 69 6f 6e 73 20 69 73 20 61 73 20 66 ivisions is as f
c6310 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 ollows:.**.**
c6320 2a 20 20 47 65 6e 65 72 61 6c 2d 70 75 72 70 6f * General-purpo
c6330 73 65 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 se declarations
c6340 61 6e 64 20 75 74 69 6c 69 74 79 20 66 75 6e 63 and utility func
c6350 74 69 6f 6e 73 2e 0a 2a 2a 20 20 20 2a 20 20 55 tions..** * U
c6360 6e 69 71 75 65 20 66 69 6c 65 20 49 44 20 6c 6f nique file ID lo
c6370 67 69 63 20 75 73 65 64 20 62 79 20 56 78 57 6f gic used by VxWo
c6380 72 6b 73 2e 0a 2a 2a 20 20 20 2a 20 20 56 61 72 rks..** * Var
c6390 69 6f 75 73 20 6c 6f 63 6b 69 6e 67 20 70 72 69 ious locking pri
c63a0 6d 69 74 69 76 65 20 69 6d 70 6c 65 6d 65 6e 74 mitive implement
c63b0 61 74 69 6f 6e 73 20 28 61 6c 6c 20 65 78 63 65 ations (all exce
c63c0 70 74 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 pt proxy locking
c63d0 29 3a 0a 2a 2a 20 20 20 20 20 20 2b 20 66 6f 72 ):.** + for
c63e0 20 50 6f 73 69 78 20 41 64 76 69 73 6f 72 79 20 Posix Advisory
c63f0 4c 6f 63 6b 73 0a 2a 2a 20 20 20 20 20 20 2b 20 Locks.** +
c6400 66 6f 72 20 6e 6f 2d 6f 70 20 6c 6f 63 6b 73 0a for no-op locks.
c6410 2a 2a 20 20 20 20 20 20 2b 20 66 6f 72 20 64 6f ** + for do
c6420 74 2d 66 69 6c 65 20 6c 6f 63 6b 73 0a 2a 2a 20 t-file locks.**
c6430 20 20 20 20 20 2b 20 66 6f 72 20 66 6c 6f 63 6b + for flock
c6440 28 29 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 20 20 () locking.**
c6450 20 20 20 2b 20 66 6f 72 20 6e 61 6d 65 64 20 73 + for named s
c6460 65 6d 61 70 68 6f 72 65 20 6c 6f 63 6b 73 20 28 emaphore locks (
c6470 56 78 57 6f 72 6b 73 20 6f 6e 6c 79 29 0a 2a 2a VxWorks only).**
c6480 20 20 20 20 20 20 2b 20 66 6f 72 20 41 46 50 20 + for AFP
c6490 66 69 6c 65 73 79 73 74 65 6d 20 6c 6f 63 6b 73 filesystem locks
c64a0 20 28 4d 61 63 4f 53 58 20 6f 6e 6c 79 29 0a 2a (MacOSX only).*
c64b0 2a 20 20 20 2a 20 20 73 71 6c 69 74 65 33 5f 66 * * sqlite3_f
c64c0 69 6c 65 20 6d 65 74 68 6f 64 73 20 6e 6f 74 20 ile methods not
c64d0 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
c64e0 6c 6f 63 6b 69 6e 67 2e 0a 2a 2a 20 20 20 2a 20 locking..** *
c64f0 20 44 65 66 69 6e 69 74 69 6f 6e 73 20 6f 66 20 Definitions of
c6500 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f sqlite3_io_metho
c6510 64 73 20 6f 62 6a 65 63 74 73 20 66 6f 72 20 61 ds objects for a
c6520 6c 6c 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 20 20 ll locking.**
c6530 20 20 20 6d 65 74 68 6f 64 73 20 70 6c 75 73 20 methods plus
c6540 22 66 69 6e 64 65 72 22 20 66 75 6e 63 74 69 6f "finder" functio
c6550 6e 73 20 66 6f 72 20 65 61 63 68 20 6c 6f 63 6b ns for each lock
c6560 69 6e 67 20 6d 65 74 68 6f 64 2e 0a 2a 2a 20 20 ing method..**
c6570 20 2a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 * sqlite3_vfs
c6580 6d 65 74 68 6f 64 20 69 6d 70 6c 65 6d 65 6e 74 method implement
c6590 61 74 69 6f 6e 73 2e 0a 2a 2a 20 20 20 2a 20 20 ations..** *
c65a0 4c 6f 63 6b 69 6e 67 20 70 72 69 6d 69 74 69 76 Locking primitiv
c65b0 65 73 20 66 6f 72 20 74 68 65 20 70 72 6f 78 79 es for the proxy
c65c0 20 75 62 65 72 2d 6c 6f 63 6b 69 6e 67 2d 6d 65 uber-locking-me
c65d0 74 68 6f 64 2e 20 28 4d 61 63 4f 53 58 20 6f 6e thod. (MacOSX on
c65e0 6c 79 29 0a 2a 2a 20 20 20 2a 20 20 44 65 66 69 ly).** * Defi
c65f0 6e 69 74 69 6f 6e 73 20 6f 66 20 73 71 6c 69 74 nitions of sqlit
c6600 65 33 5f 76 66 73 20 6f 62 6a 65 63 74 73 20 66 e3_vfs objects f
c6610 6f 72 20 61 6c 6c 20 6c 6f 63 6b 69 6e 67 20 6d or all locking m
c6620 65 74 68 6f 64 73 0a 2a 2a 20 20 20 20 20 20 70 ethods.** p
c6630 6c 75 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 lus implementati
c6640 6f 6e 73 20 6f 66 20 73 71 6c 69 74 65 33 5f 6f ons of sqlite3_o
c6650 73 5f 69 6e 69 74 28 29 20 61 6e 64 20 73 71 6c s_init() and sql
c6660 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29 2e 0a 2a ite3_os_end()..*
c6670 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f /.#if SQLITE_OS_
c6680 55 4e 49 58 20 20 20 20 20 20 20 20 20 20 20 20 UNIX
c6690 20 20 2f 2a 20 54 68 69 73 20 66 69 6c 65 20 69 /* This file i
c66a0 73 20 75 73 65 64 20 6f 6e 20 75 6e 69 78 20 6f s used on unix o
c66b0 6e 6c 79 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 nly */../*.** Th
c66c0 65 72 65 20 61 72 65 20 76 61 72 69 6f 75 73 20 ere are various
c66d0 6d 65 74 68 6f 64 73 20 66 6f 72 20 66 69 6c 65 methods for file
c66e0 20 6c 6f 63 6b 69 6e 67 20 75 73 65 64 20 66 6f locking used fo
c66f0 72 20 63 6f 6e 63 75 72 72 65 6e 63 79 0a 2a 2a r concurrency.**
c6700 20 63 6f 6e 74 72 6f 6c 3a 0a 2a 2a 0a 2a 2a 20 control:.**.**
c6710 20 20 31 2e 20 50 4f 53 49 58 20 6c 6f 63 6b 69 1. POSIX locki
c6720 6e 67 20 28 74 68 65 20 64 65 66 61 75 6c 74 29 ng (the default)
c6730 2c 0a 2a 2a 20 20 20 32 2e 20 4e 6f 20 6c 6f 63 ,.** 2. No loc
c6740 6b 69 6e 67 2c 0a 2a 2a 20 20 20 33 2e 20 44 6f king,.** 3. Do
c6750 74 2d 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 2c 0a t-file locking,.
c6760 2a 2a 20 20 20 34 2e 20 66 6c 6f 63 6b 28 29 20 ** 4. flock()
c6770 6c 6f 63 6b 69 6e 67 2c 0a 2a 2a 20 20 20 35 2e locking,.** 5.
c6780 20 41 46 50 20 6c 6f 63 6b 69 6e 67 20 28 4f 53 AFP locking (OS
c6790 58 20 6f 6e 6c 79 29 2c 0a 2a 2a 20 20 20 36 2e X only),.** 6.
c67a0 20 4e 61 6d 65 64 20 50 4f 53 49 58 20 73 65 6d Named POSIX sem
c67b0 61 70 68 6f 72 65 73 20 28 56 58 57 6f 72 6b 73 aphores (VXWorks
c67c0 20 6f 6e 6c 79 29 2c 0a 2a 2a 20 20 20 37 2e 20 only),.** 7.
c67d0 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 2e 20 28 proxy locking. (
c67e0 4f 53 58 20 6f 6e 6c 79 29 0a 2a 2a 0a 2a 2a 20 OSX only).**.**
c67f0 53 74 79 6c 65 73 20 34 2c 20 35 2c 20 61 6e 64 Styles 4, 5, and
c6800 20 37 20 61 72 65 20 6f 6e 6c 79 20 61 76 61 69 7 are only avai
c6810 6c 61 62 6c 65 20 6f 66 20 53 51 4c 49 54 45 5f lable of SQLITE_
c6820 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 ENABLE_LOCKING_S
c6830 54 59 4c 45 0a 2a 2a 20 69 73 20 64 65 66 69 6e TYLE.** is defin
c6840 65 64 20 74 6f 20 31 2e 20 20 54 68 65 20 53 51 ed to 1. The SQ
c6850 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b LITE_ENABLE_LOCK
c6860 49 4e 47 5f 53 54 59 4c 45 20 61 6c 73 6f 20 65 ING_STYLE also e
c6870 6e 61 62 6c 65 73 20 61 75 74 6f 6d 61 74 69 63 nables automatic
c6880 0a 2a 2a 20 73 65 6c 65 63 74 69 6f 6e 20 6f 66 .** selection of
c6890 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 the appropriate
c68a0 20 6c 6f 63 6b 69 6e 67 20 73 74 79 6c 65 20 62 locking style b
c68b0 61 73 65 64 20 6f 6e 20 74 68 65 20 66 69 6c 65 ased on the file
c68c0 73 79 73 74 65 6d 0a 2a 2a 20 77 68 65 72 65 20 system.** where
c68d0 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 the database is
c68e0 6c 6f 63 61 74 65 64 2e 20 20 0a 2a 2f 0a 23 69 located. .*/.#i
c68f0 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 f !defined(SQLIT
c6900 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 E_ENABLE_LOCKING
c6910 5f 53 54 59 4c 45 29 0a 23 20 20 69 66 20 64 65 _STYLE).# if de
c6920 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 fined(__APPLE__)
c6930 0a 23 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c .# define SQL
c6940 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 ITE_ENABLE_LOCKI
c6950 4e 47 5f 53 54 59 4c 45 20 31 0a 23 20 20 65 6c NG_STYLE 1.# el
c6960 73 65 0a 23 20 20 20 20 64 65 66 69 6e 65 20 53 se.# define S
c6970 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 QLITE_ENABLE_LOC
c6980 4b 49 4e 47 5f 53 54 59 4c 45 20 30 0a 23 20 20 KING_STYLE 0.#
c6990 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a 2f 2a endif.#endif../*
c69a0 0a 2a 2a 20 44 65 66 69 6e 65 20 74 68 65 20 4f .** Define the O
c69b0 53 5f 56 58 57 4f 52 4b 53 20 70 72 65 2d 70 72 S_VXWORKS pre-pr
c69c0 6f 63 65 73 73 6f 72 20 6d 61 63 72 6f 20 74 6f ocessor macro to
c69d0 20 31 20 69 66 20 62 75 69 6c 64 69 6e 67 20 6f 1 if building o
c69e0 6e 20 0a 2a 2a 20 76 78 77 6f 72 6b 73 2c 20 6f n .** vxworks, o
c69f0 72 20 30 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a r 0 otherwise..*
c6a00 2f 0a 23 69 66 6e 64 65 66 20 4f 53 5f 56 58 57 /.#ifndef OS_VXW
c6a10 4f 52 4b 53 0a 23 20 20 69 66 20 64 65 66 69 6e ORKS.# if defin
c6a20 65 64 28 5f 5f 52 54 50 5f 5f 29 20 7c 7c 20 64 ed(__RTP__) || d
c6a30 65 66 69 6e 65 64 28 5f 57 52 53 5f 4b 45 52 4e efined(_WRS_KERN
c6a40 45 4c 29 0a 23 20 20 20 20 64 65 66 69 6e 65 20 EL).# define
c6a50 4f 53 5f 56 58 57 4f 52 4b 53 20 31 0a 23 20 20 OS_VXWORKS 1.#
c6a60 65 6c 73 65 0a 23 20 20 20 20 64 65 66 69 6e 65 else.# define
c6a70 20 4f 53 5f 56 58 57 4f 52 4b 53 20 30 0a 23 20 OS_VXWORKS 0.#
c6a80 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a 2f endif.#endif../
c6a90 2a 0a 2a 2a 20 54 68 65 73 65 20 23 64 65 66 69 *.** These #defi
c6aa0 6e 65 73 20 73 68 6f 75 6c 64 20 65 6e 61 62 6c nes should enabl
c6ab0 65 20 3e 32 47 42 20 66 69 6c 65 20 73 75 70 70 e >2GB file supp
c6ac0 6f 72 74 20 6f 6e 20 50 6f 73 69 78 20 69 66 20 ort on Posix if
c6ad0 74 68 65 0a 2a 2a 20 75 6e 64 65 72 6c 79 69 6e the.** underlyin
c6ae0 67 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 g operating syst
c6af0 65 6d 20 73 75 70 70 6f 72 74 73 20 69 74 2e 20 em supports it.
c6b00 20 49 66 20 74 68 65 20 4f 53 20 6c 61 63 6b 73 If the OS lacks
c6b10 0a 2a 2a 20 6c 61 72 67 65 20 66 69 6c 65 20 73 .** large file s
c6b20 75 70 70 6f 72 74 2c 20 74 68 65 73 65 20 73 68 upport, these sh
c6b30 6f 75 6c 64 20 62 65 20 6e 6f 2d 6f 70 73 2e 0a ould be no-ops..
c6b40 2a 2a 0a 2a 2a 20 4c 61 72 67 65 20 66 69 6c 65 **.** Large file
c6b50 20 73 75 70 70 6f 72 74 20 63 61 6e 20 62 65 20 support can be
c6b60 64 69 73 61 62 6c 65 64 20 75 73 69 6e 67 20 74 disabled using t
c6b70 68 65 20 2d 44 53 51 4c 49 54 45 5f 44 49 53 41 he -DSQLITE_DISA
c6b80 42 4c 45 5f 4c 46 53 20 73 77 69 74 63 68 0a 2a BLE_LFS switch.*
c6b90 2a 20 6f 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65 * on the compile
c6ba0 72 20 63 6f 6d 6d 61 6e 64 20 6c 69 6e 65 2e 20 r command line.
c6bb0 20 54 68 69 73 20 69 73 20 6e 65 63 65 73 73 61 This is necessa
c6bc0 72 79 20 69 66 20 79 6f 75 20 61 72 65 20 63 6f ry if you are co
c6bd0 6d 70 69 6c 69 6e 67 0a 2a 2a 20 6f 6e 20 61 20 mpiling.** on a
c6be0 72 65 63 65 6e 74 20 6d 61 63 68 69 6e 65 20 28 recent machine (
c6bf0 65 78 3a 20 52 65 64 48 61 74 20 37 2e 32 29 20 ex: RedHat 7.2)
c6c00 62 75 74 20 79 6f 75 20 77 61 6e 74 20 79 6f 75 but you want you
c6c10 72 20 63 6f 64 65 20 74 6f 20 77 6f 72 6b 0a 2a r code to work.*
c6c20 2a 20 6f 6e 20 61 6e 20 6f 6c 64 65 72 20 6d 61 * on an older ma
c6c30 63 68 69 6e 65 20 28 65 78 3a 20 52 65 64 48 61 chine (ex: RedHa
c6c40 74 20 36 2e 30 29 2e 20 20 49 66 20 79 6f 75 20 t 6.0). If you
c6c50 63 6f 6d 70 69 6c 65 20 6f 6e 20 52 65 64 48 61 compile on RedHa
c6c60 74 20 37 2e 32 0a 2a 2a 20 77 69 74 68 6f 75 74 t 7.2.** without
c6c70 20 74 68 69 73 20 6f 70 74 69 6f 6e 2c 20 4c 46 this option, LF
c6c80 53 20 69 73 20 65 6e 61 62 6c 65 2e 20 20 42 75 S is enable. Bu
c6c90 74 20 4c 46 53 20 64 6f 65 73 20 6e 6f 74 20 65 t LFS does not e
c6ca0 78 69 73 74 20 69 6e 20 74 68 65 20 6b 65 72 6e xist in the kern
c6cb0 65 6c 0a 2a 2a 20 69 6e 20 52 65 64 48 61 74 20 el.** in RedHat
c6cc0 36 2e 30 2c 20 73 6f 20 74 68 65 20 63 6f 64 65 6.0, so the code
c6cd0 20 77 6f 6e 27 74 20 77 6f 72 6b 2e 20 20 48 65 won't work. He
c6ce0 6e 63 65 2c 20 66 6f 72 20 6d 61 78 69 6d 75 6d nce, for maximum
c6cf0 20 62 69 6e 61 72 79 0a 2a 2a 20 70 6f 72 74 61 binary.** porta
c6d00 62 69 6c 69 74 79 20 79 6f 75 20 73 68 6f 75 6c bility you shoul
c6d10 64 20 6f 6d 69 74 20 4c 46 53 2e 0a 2a 2a 0a 2a d omit LFS..**.*
c6d20 2a 20 54 68 65 20 70 72 65 76 69 6f 75 73 20 70 * The previous p
c6d30 61 72 61 67 72 61 70 68 20 77 61 73 20 77 72 69 aragraph was wri
c6d40 74 74 65 6e 20 69 6e 20 32 30 30 35 2e 20 20 28 tten in 2005. (
c6d50 54 68 69 73 20 70 61 72 61 67 72 61 70 68 20 69 This paragraph i
c6d60 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20 6f 6e 20 s written.** on
c6d70 32 30 30 38 2d 31 31 2d 32 38 2e 29 20 54 68 65 2008-11-28.) The
c6d80 73 65 20 64 61 79 73 2c 20 61 6c 6c 20 4c 69 6e se days, all Lin
c6d90 75 78 20 6b 65 72 6e 65 6c 73 20 73 75 70 70 6f ux kernels suppo
c6da0 72 74 20 6c 61 72 67 65 20 66 69 6c 65 73 2c 20 rt large files,
c6db0 73 6f 0a 2a 2a 20 79 6f 75 20 73 68 6f 75 6c 64 so.** you should
c6dc0 20 70 72 6f 62 61 62 6c 79 20 6c 65 61 76 65 20 probably leave
c6dd0 4c 46 53 20 65 6e 61 62 6c 65 64 2e 20 20 42 75 LFS enabled. Bu
c6de0 74 20 73 6f 6d 65 20 65 6d 62 65 64 64 65 64 20 t some embedded
c6df0 70 6c 61 74 66 6f 72 6d 73 20 6d 69 67 68 74 0a platforms might.
c6e00 2a 2a 20 6c 61 63 6b 20 4c 46 53 20 69 6e 20 77 ** lack LFS in w
c6e10 68 69 63 68 20 63 61 73 65 20 74 68 65 20 53 51 hich case the SQ
c6e20 4c 49 54 45 5f 44 49 53 41 42 4c 45 5f 4c 46 53 LITE_DISABLE_LFS
c6e30 20 6d 61 63 72 6f 20 6d 69 67 68 74 20 73 74 69 macro might sti
c6e40 6c 6c 20 62 65 20 75 73 65 66 75 6c 2e 0a 2a 2f ll be useful..*/
c6e50 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
c6e60 44 49 53 41 42 4c 45 5f 4c 46 53 0a 23 20 64 65 DISABLE_LFS.# de
c6e70 66 69 6e 65 20 5f 4c 41 52 47 45 5f 46 49 4c 45 fine _LARGE_FILE
c6e80 20 20 20 20 20 20 20 31 0a 23 20 69 66 6e 64 65 1.# ifnde
c6e90 66 20 5f 46 49 4c 45 5f 4f 46 46 53 45 54 5f 42 f _FILE_OFFSET_B
c6ea0 49 54 53 0a 23 20 20 20 64 65 66 69 6e 65 20 5f ITS.# define _
c6eb0 46 49 4c 45 5f 4f 46 46 53 45 54 5f 42 49 54 53 FILE_OFFSET_BITS
c6ec0 20 36 34 0a 23 20 65 6e 64 69 66 0a 23 20 64 65 64.# endif.# de
c6ed0 66 69 6e 65 20 5f 4c 41 52 47 45 46 49 4c 45 5f fine _LARGEFILE_
c6ee0 53 4f 55 52 43 45 20 31 0a 23 65 6e 64 69 66 0a SOURCE 1.#endif.
c6ef0 0a 2f 2a 0a 2a 2a 20 73 74 61 6e 64 61 72 64 20 ./*.** standard
c6f00 69 6e 63 6c 75 64 65 20 66 69 6c 65 73 2e 0a 2a include files..*
c6f10 2f 0a 23 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f /.#include <sys/
c6f20 74 79 70 65 73 2e 68 3e 0a 23 69 6e 63 6c 75 64 types.h>.#includ
c6f30 65 20 3c 73 79 73 2f 73 74 61 74 2e 68 3e 0a 23 e <sys/stat.h>.#
c6f40 69 6e 63 6c 75 64 65 20 3c 66 63 6e 74 6c 2e 68 include <fcntl.h
c6f50 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 75 6e 69 73 >.#include <unis
c6f60 74 64 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c td.h>.#include <
c6f70 73 79 73 2f 74 69 6d 65 2e 68 3e 0a 23 69 6e 63 sys/time.h>.#inc
c6f80 6c 75 64 65 20 3c 65 72 72 6e 6f 2e 68 3e 0a 0a lude <errno.h>..
c6f90 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c #if SQLITE_ENABL
c6fa0 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a E_LOCKING_STYLE.
c6fb0 23 20 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 69 # include <sys/i
c6fc0 6f 63 74 6c 2e 68 3e 0a 23 20 69 66 20 4f 53 5f octl.h>.# if OS_
c6fd0 56 58 57 4f 52 4b 53 0a 23 20 20 69 6e 63 6c 75 VXWORKS.# inclu
c6fe0 64 65 20 3c 73 65 6d 61 70 68 6f 72 65 2e 68 3e de <semaphore.h>
c6ff0 0a 23 20 20 69 6e 63 6c 75 64 65 20 3c 6c 69 6d .# include <lim
c7000 69 74 73 2e 68 3e 0a 23 20 65 6c 73 65 0a 23 20 its.h>.# else.#
c7010 20 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 66 69 include <sys/fi
c7020 6c 65 2e 68 3e 0a 23 20 20 69 6e 63 6c 75 64 65 le.h>.# include
c7030 20 3c 73 79 73 2f 70 61 72 61 6d 2e 68 3e 0a 23 <sys/param.h>.#
c7040 20 20 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 6d include <sys/m
c7050 6f 75 6e 74 2e 68 3e 0a 23 20 65 6e 64 69 66 0a ount.h>.# endif.
c7060 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
c7070 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f _ENABLE_LOCKING_
c7080 53 54 59 4c 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 STYLE */../*.**
c7090 49 66 20 77 65 20 61 72 65 20 74 6f 20 62 65 20 If we are to be
c70a0 74 68 72 65 61 64 2d 73 61 66 65 2c 20 69 6e 63 thread-safe, inc
c70b0 6c 75 64 65 20 74 68 65 20 70 74 68 72 65 61 64 lude the pthread
c70c0 73 20 68 65 61 64 65 72 20 61 6e 64 20 64 65 66 s header and def
c70d0 69 6e 65 0a 2a 2a 20 74 68 65 20 53 51 4c 49 54 ine.** the SQLIT
c70e0 45 5f 55 4e 49 58 5f 54 48 52 45 41 44 53 20 6d E_UNIX_THREADS m
c70f0 61 63 72 6f 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c acro..*/.#if SQL
c7100 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a 23 ITE_THREADSAFE.#
c7110 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 55 define SQLITE_U
c7120 4e 49 58 5f 54 48 52 45 41 44 53 20 31 0a 23 65 NIX_THREADS 1.#e
c7130 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 61 ndif../*.** Defa
c7140 75 6c 74 20 70 65 72 6d 69 73 73 69 6f 6e 73 20 ult permissions
c7150 77 68 65 6e 20 63 72 65 61 74 69 6e 67 20 61 20 when creating a
c7160 6e 65 77 20 66 69 6c 65 0a 2a 2f 0a 23 69 66 6e new file.*/.#ifn
c7170 64 65 66 20 53 51 4c 49 54 45 5f 44 45 46 41 55 def SQLITE_DEFAU
c7180 4c 54 5f 46 49 4c 45 5f 50 45 52 4d 49 53 53 49 LT_FILE_PERMISSI
c7190 4f 4e 53 0a 23 20 64 65 66 69 6e 65 20 53 51 4c ONS.# define SQL
c71a0 49 54 45 5f 44 45 46 41 55 4c 54 5f 46 49 4c 45 ITE_DEFAULT_FILE
c71b0 5f 50 45 52 4d 49 53 53 49 4f 4e 53 20 30 36 34 _PERMISSIONS 064
c71c0 34 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 20 2a 2a 4.#endif../*. **
c71d0 20 44 65 66 61 75 6c 74 20 70 65 72 6d 69 73 73 Default permiss
c71e0 69 6f 6e 73 20 77 68 65 6e 20 63 72 65 61 74 69 ions when creati
c71f0 6e 67 20 61 75 74 6f 20 70 72 6f 78 79 20 64 69 ng auto proxy di
c7200 72 0a 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 r. */.#ifndef SQ
c7210 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 52 4f LITE_DEFAULT_PRO
c7220 58 59 44 49 52 5f 50 45 52 4d 49 53 53 49 4f 4e XYDIR_PERMISSION
c7230 53 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 S.# define SQLIT
c7240 45 5f 44 45 46 41 55 4c 54 5f 50 52 4f 58 59 44 E_DEFAULT_PROXYD
c7250 49 52 5f 50 45 52 4d 49 53 53 49 4f 4e 53 20 30 IR_PERMISSIONS 0
c7260 37 35 35 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 755.#endif../*.*
c7270 2a 20 4d 61 78 69 6d 75 6d 20 73 75 70 70 6f 72 * Maximum suppor
c7280 74 65 64 20 70 61 74 68 2d 6c 65 6e 67 74 68 2e ted path-length.
c7290 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 41 58 5f .*/.#define MAX_
c72a0 50 41 54 48 4e 41 4d 45 20 35 31 32 0a 0a 2f 2a PATHNAME 512../*
c72b0 0a 2a 2a 20 4f 6e 6c 79 20 73 65 74 20 74 68 65 .** Only set the
c72c0 20 6c 61 73 74 45 72 72 6e 6f 20 69 66 20 74 68 lastErrno if th
c72d0 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 e error code is
c72e0 61 20 72 65 61 6c 20 65 72 72 6f 72 20 61 6e 64 a real error and
c72f0 20 6e 6f 74 20 0a 2a 2a 20 61 20 6e 6f 72 6d 61 not .** a norma
c7300 6c 20 65 78 70 65 63 74 65 64 20 72 65 74 75 72 l expected retur
c7310 6e 20 63 6f 64 65 20 6f 66 20 53 51 4c 49 54 45 n code of SQLITE
c7320 5f 42 55 53 59 20 6f 72 20 53 51 4c 49 54 45 5f _BUSY or SQLITE_
c7330 4f 4b 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 49 53 OK.*/.#define IS
c7340 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 78 29 20 20 _LOCK_ERROR(x)
c7350 28 28 78 20 21 3d 20 53 51 4c 49 54 45 5f 4f 4b ((x != SQLITE_OK
c7360 29 20 26 26 20 28 78 20 21 3d 20 53 51 4c 49 54 ) && (x != SQLIT
c7370 45 5f 42 55 53 59 29 29 0a 0a 0a 2f 2a 0a 2a 2a E_BUSY)).../*.**
c7380 20 53 6f 6d 65 74 69 6d 65 73 2c 20 61 66 74 65 Sometimes, afte
c7390 72 20 61 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 r a file handle
c73a0 69 73 20 63 6c 6f 73 65 64 20 62 79 20 53 51 4c is closed by SQL
c73b0 69 74 65 2c 20 74 68 65 20 66 69 6c 65 20 64 65 ite, the file de
c73c0 73 63 72 69 70 74 6f 72 0a 2a 2a 20 63 61 6e 6e scriptor.** cann
c73d0 6f 74 20 62 65 20 63 6c 6f 73 65 64 20 69 6d 6d ot be closed imm
c73e0 65 64 69 61 74 65 6c 79 2e 20 49 6e 20 74 68 65 ediately. In the
c73f0 73 65 20 63 61 73 65 73 2c 20 69 6e 73 74 61 6e se cases, instan
c7400 63 65 73 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f ces of the follo
c7410 77 69 6e 67 0a 2a 2a 20 73 74 72 75 63 74 75 72 wing.** structur
c7420 65 20 61 72 65 20 75 73 65 64 20 74 6f 20 73 74 e are used to st
c7430 6f 72 65 20 74 68 65 20 66 69 6c 65 20 64 65 73 ore the file des
c7440 63 72 69 70 74 6f 72 20 77 68 69 6c 65 20 77 61 criptor while wa
c7450 69 74 69 6e 67 20 66 6f 72 20 61 6e 0a 2a 2a 20 iting for an.**
c7460 6f 70 70 6f 72 74 75 6e 69 74 79 20 74 6f 20 65 opportunity to e
c7470 69 74 68 65 72 20 63 6c 6f 73 65 20 6f 72 20 72 ither close or r
c7480 65 75 73 65 20 69 74 2e 0a 2a 2f 0a 74 79 70 65 euse it..*/.type
c7490 64 65 66 20 73 74 72 75 63 74 20 55 6e 69 78 55 def struct UnixU
c74a0 6e 75 73 65 64 46 64 20 55 6e 69 78 55 6e 75 73 nusedFd UnixUnus
c74b0 65 64 46 64 3b 0a 73 74 72 75 63 74 20 55 6e 69 edFd;.struct Uni
c74c0 78 55 6e 75 73 65 64 46 64 20 7b 0a 20 20 69 6e xUnusedFd {. in
c74d0 74 20 66 64 3b 20 20 20 20 20 20 20 20 20 20 20 t fd;
c74e0 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 /* File
c74f0 64 65 73 63 72 69 70 74 6f 72 20 74 6f 20 63 6c descriptor to cl
c7500 6f 73 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 ose */. int fla
c7510 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 gs;
c7520 20 20 20 2f 2a 20 46 6c 61 67 73 20 74 68 69 73 /* Flags this
c7530 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 file descriptor
c7540 20 77 61 73 20 6f 70 65 6e 65 64 20 77 69 74 68 was opened with
c7550 20 2a 2f 0a 20 20 55 6e 69 78 55 6e 75 73 65 64 */. UnixUnused
c7560 46 64 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 Fd *pNext;
c7570 2f 2a 20 4e 65 78 74 20 75 6e 75 73 65 64 20 66 /* Next unused f
c7580 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 6f ile descriptor o
c7590 6e 20 73 61 6d 65 20 66 69 6c 65 20 2a 2f 0a 7d n same file */.}
c75a0 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 75 6e 69 ;../*.** The uni
c75b0 78 46 69 6c 65 20 73 74 72 75 63 74 75 72 65 20 xFile structure
c75c0 69 73 20 73 75 62 63 6c 61 73 73 20 6f 66 20 73 is subclass of s
c75d0 71 6c 69 74 65 33 5f 66 69 6c 65 20 73 70 65 63 qlite3_file spec
c75e0 69 66 69 63 20 74 6f 20 74 68 65 20 75 6e 69 78 ific to the unix
c75f0 0a 2a 2a 20 56 46 53 20 69 6d 70 6c 65 6d 65 6e .** VFS implemen
c7600 74 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 74 79 70 65 tations..*/.type
c7610 64 65 66 20 73 74 72 75 63 74 20 75 6e 69 78 46 def struct unixF
c7620 69 6c 65 20 75 6e 69 78 46 69 6c 65 3b 0a 73 74 ile unixFile;.st
c7630 72 75 63 74 20 75 6e 69 78 46 69 6c 65 20 7b 0a ruct unixFile {.
c7640 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 sqlite3_io_met
c7650 68 6f 64 73 20 63 6f 6e 73 74 20 2a 70 4d 65 74 hods const *pMet
c7660 68 6f 64 3b 20 20 2f 2a 20 41 6c 77 61 79 73 20 hod; /* Always
c7670 74 68 65 20 66 69 72 73 74 20 65 6e 74 72 79 20 the first entry
c7680 2a 2f 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 */. struct unix
c7690 4f 70 65 6e 43 6e 74 20 2a 70 4f 70 65 6e 3b 20 OpenCnt *pOpen;
c76a0 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f 20 61 62 /* Info ab
c76b0 6f 75 74 20 61 6c 6c 20 6f 70 65 6e 20 66 64 27 out all open fd'
c76c0 73 20 6f 6e 20 74 68 69 73 20 69 6e 6f 64 65 20 s on this inode
c76d0 2a 2f 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 */. struct unix
c76e0 4c 6f 63 6b 49 6e 66 6f 20 2a 70 4c 6f 63 6b 3b LockInfo *pLock;
c76f0 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f 20 61 62 /* Info ab
c7700 6f 75 74 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 69 out locks on thi
c7710 73 20 69 6e 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 s inode */. int
c7720 20 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 h;
c7730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
c7740 20 54 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 The file descri
c7750 70 74 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 64 69 ptor */. int di
c7760 72 66 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 rfd;
c7770 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 /* Fi
c7780 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 66 6f le descriptor fo
c7790 72 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 r the directory
c77a0 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 */. unsigned ch
c77b0 61 72 20 6c 6f 63 6b 74 79 70 65 3b 20 20 20 20 ar locktype;
c77c0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 79 70 /* The typ
c77d0 65 20 6f 66 20 6c 6f 63 6b 20 68 65 6c 64 20 6f e of lock held o
c77e0 6e 20 74 68 69 73 20 66 64 20 2a 2f 0a 20 20 69 n this fd */. i
c77f0 6e 74 20 6c 61 73 74 45 72 72 6e 6f 3b 20 20 20 nt lastErrno;
c7800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c7810 2f 2a 20 54 68 65 20 75 6e 69 78 20 65 72 72 6e /* The unix errn
c7820 6f 20 66 72 6f 6d 20 74 68 65 20 6c 61 73 74 20 o from the last
c7830 49 2f 4f 20 65 72 72 6f 72 20 2a 2f 0a 20 20 76 I/O error */. v
c7840 6f 69 64 20 2a 6c 6f 63 6b 69 6e 67 43 6f 6e 74 oid *lockingCont
c7850 65 78 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 ext;
c7860 2f 2a 20 4c 6f 63 6b 69 6e 67 20 73 74 79 6c 65 /* Locking style
c7870 20 73 70 65 63 69 66 69 63 20 73 74 61 74 65 20 specific state
c7880 2a 2f 0a 20 20 55 6e 69 78 55 6e 75 73 65 64 46 */. UnixUnusedF
c7890 64 20 2a 70 55 6e 75 73 65 64 3b 20 20 20 20 20 d *pUnused;
c78a0 20 20 20 20 20 20 2f 2a 20 50 72 65 2d 61 6c 6c /* Pre-all
c78b0 6f 63 61 74 65 64 20 55 6e 69 78 55 6e 75 73 65 ocated UnixUnuse
c78c0 64 46 64 20 2a 2f 0a 20 20 69 6e 74 20 66 69 6c dFd */. int fil
c78d0 65 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 eFlags;
c78e0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 69 73 /* Mis
c78f0 63 65 6c 6c 61 6e 6f 75 73 20 66 6c 61 67 73 20 cellanous flags
c7900 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e */.#if SQLITE_EN
c7910 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 ABLE_LOCKING_STY
c7920 4c 45 0a 20 20 69 6e 74 20 6f 70 65 6e 46 6c 61 LE. int openFla
c7930 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 gs;
c7940 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 6c 61 /* The fla
c7950 67 73 20 73 70 65 63 69 66 69 65 64 20 61 74 20 gs specified at
c7960 6f 70 65 6e 28 29 20 2a 2f 0a 23 65 6e 64 69 66 open() */.#endif
c7970 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 .#if SQLITE_THRE
c7980 41 44 53 41 46 45 20 26 26 20 64 65 66 69 6e 65 ADSAFE && define
c7990 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 0a 20 20 70 d(__linux__). p
c79a0 74 68 72 65 61 64 5f 74 20 74 69 64 3b 20 20 20 thread_t tid;
c79b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c79c0 2f 2a 20 54 68 65 20 74 68 72 65 61 64 20 74 68 /* The thread th
c79d0 61 74 20 22 6f 77 6e 73 22 20 74 68 69 73 20 75 at "owns" this u
c79e0 6e 69 78 46 69 6c 65 20 2a 2f 0a 23 65 6e 64 69 nixFile */.#endi
c79f0 66 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 f.#if OS_VXWORKS
c7a00 0a 20 20 69 6e 74 20 69 73 44 65 6c 65 74 65 3b . int isDelete;
c7a10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c7a20 20 20 20 20 2f 2a 20 44 65 6c 65 74 65 20 6f 6e /* Delete on
c7a30 20 63 6c 6f 73 65 20 69 66 20 74 72 75 65 20 2a close if true *
c7a40 2f 0a 20 20 73 74 72 75 63 74 20 76 78 77 6f 72 /. struct vxwor
c7a50 6b 73 46 69 6c 65 49 64 20 2a 70 49 64 3b 20 20 ksFileId *pId;
c7a60 20 20 20 20 20 2f 2a 20 55 6e 69 71 75 65 20 66 /* Unique f
c7a70 69 6c 65 20 49 44 20 2a 2f 0a 23 65 6e 64 69 66 ile ID */.#endif
c7a80 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a .#ifndef NDEBUG.
c7a90 20 20 2f 2a 20 54 68 65 20 6e 65 78 74 20 67 72 /* The next gr
c7aa0 6f 75 70 20 6f 66 20 76 61 72 69 61 62 6c 65 73 oup of variables
c7ab0 20 61 72 65 20 75 73 65 64 20 74 6f 20 74 72 61 are used to tra
c7ac0 63 6b 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f ck whether or no
c7ad0 74 20 74 68 65 0a 20 20 2a 2a 20 74 72 61 6e 73 t the. ** trans
c7ae0 61 63 74 69 6f 6e 20 63 6f 75 6e 74 65 72 20 69 action counter i
c7af0 6e 20 62 79 74 65 73 20 32 34 2d 32 37 20 6f 66 n bytes 24-27 of
c7b00 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 73 20 database files
c7b10 61 72 65 20 75 70 64 61 74 65 64 0a 20 20 2a 2a are updated. **
c7b20 20 77 68 65 6e 65 76 65 72 20 61 6e 79 20 70 61 whenever any pa
c7b30 72 74 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 rt of the databa
c7b40 73 65 20 63 68 61 6e 67 65 73 2e 20 20 41 6e 20 se changes. An
c7b50 61 73 73 65 72 74 69 6f 6e 20 66 61 75 6c 74 20 assertion fault
c7b60 77 69 6c 6c 0a 20 20 2a 2a 20 6f 63 63 75 72 20 will. ** occur
c7b70 69 66 20 61 20 66 69 6c 65 20 69 73 20 75 70 64 if a file is upd
c7b80 61 74 65 64 20 77 69 74 68 6f 75 74 20 61 6c 73 ated without als
c7b90 6f 20 75 70 64 61 74 69 6e 67 20 74 68 65 20 74 o updating the t
c7ba0 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20 2a 2a 20 ransaction. **
c7bb0 63 6f 75 6e 74 65 72 2e 20 20 54 68 69 73 20 74 counter. This t
c7bc0 65 73 74 20 69 73 20 6d 61 64 65 20 74 6f 20 61 est is made to a
c7bd0 76 6f 69 64 20 6e 65 77 20 70 72 6f 62 6c 65 6d void new problem
c7be0 73 20 73 69 6d 69 6c 61 72 20 74 6f 20 74 68 65 s similar to the
c7bf0 0a 20 20 2a 2a 20 6f 6e 65 20 64 65 73 63 72 69 . ** one descri
c7c00 62 65 64 20 62 79 20 74 69 63 6b 65 74 20 23 33 bed by ticket #3
c7c10 35 38 34 2e 20 0a 20 20 2a 2f 0a 20 20 75 6e 73 584. . */. uns
c7c20 69 67 6e 65 64 20 63 68 61 72 20 74 72 61 6e 73 igned char trans
c7c30 43 6e 74 72 43 68 6e 67 3b 20 20 20 2f 2a 20 54 CntrChng; /* T
c7c40 72 75 65 20 69 66 20 74 68 65 20 74 72 61 6e 73 rue if the trans
c7c50 61 63 74 69 6f 6e 20 63 6f 75 6e 74 65 72 20 63 action counter c
c7c60 68 61 6e 67 65 64 20 2a 2f 0a 20 20 75 6e 73 69 hanged */. unsi
c7c70 67 6e 65 64 20 63 68 61 72 20 64 62 55 70 64 61 gned char dbUpda
c7c80 74 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72 te; /* Tr
c7c90 75 65 20 69 66 20 61 6e 79 20 70 61 72 74 20 6f ue if any part o
c7ca0 66 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 f database file
c7cb0 63 68 61 6e 67 65 64 20 2a 2f 0a 20 20 75 6e 73 changed */. uns
c7cc0 69 67 6e 65 64 20 63 68 61 72 20 69 6e 4e 6f 72 igned char inNor
c7cd0 6d 61 6c 57 72 69 74 65 3b 20 20 20 2f 2a 20 54 malWrite; /* T
c7ce0 72 75 65 20 69 66 20 69 6e 20 61 20 6e 6f 72 6d rue if in a norm
c7cf0 61 6c 20 77 72 69 74 65 20 6f 70 65 72 61 74 69 al write operati
c7d00 6f 6e 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 on */.#endif.#if
c7d10 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a def SQLITE_TEST.
c7d20 20 20 2f 2a 20 49 6e 20 74 65 73 74 20 6d 6f 64 /* In test mod
c7d30 65 2c 20 69 6e 63 72 65 61 73 65 20 74 68 65 20 e, increase the
c7d40 73 69 7a 65 20 6f 66 20 74 68 69 73 20 73 74 72 size of this str
c7d50 75 63 74 75 72 65 20 61 20 62 69 74 20 73 6f 20 ucture a bit so
c7d60 74 68 61 74 20 0a 20 20 2a 2a 20 69 74 20 69 73 that . ** it is
c7d70 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 65 larger than the
c7d80 20 73 74 72 75 63 74 20 43 72 61 73 68 46 69 6c struct CrashFil
c7d90 65 20 64 65 66 69 6e 65 64 20 69 6e 20 74 65 73 e defined in tes
c7da0 74 36 2e 63 2e 0a 20 20 2a 2f 0a 20 20 63 68 61 t6.c.. */. cha
c7db0 72 20 61 50 61 64 64 69 6e 67 5b 33 32 5d 3b 0a r aPadding[32];.
c7dc0 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a #endif.};../*.**
c7dd0 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d The following m
c7de0 61 63 72 6f 73 20 64 65 66 69 6e 65 20 62 69 74 acros define bit
c7df0 73 20 69 6e 20 75 6e 69 78 46 69 6c 65 2e 66 69 s in unixFile.fi
c7e00 6c 65 46 6c 61 67 73 0a 2a 2f 0a 23 64 65 66 69 leFlags.*/.#defi
c7e10 6e 65 20 53 51 4c 49 54 45 5f 57 48 4f 4c 45 5f ne SQLITE_WHOLE_
c7e20 46 49 4c 45 5f 4c 4f 43 4b 49 4e 47 20 20 30 78 FILE_LOCKING 0x
c7e30 30 30 30 31 20 20 20 2f 2a 20 55 73 65 20 77 68 0001 /* Use wh
c7e40 6f 6c 65 2d 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 ole-file locking
c7e50 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 6c 75 */../*.** Inclu
c7e60 64 65 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 de code that is
c7e70 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 6f 73 common to all os
c7e80 5f 2a 2e 63 20 66 69 6c 65 73 0a 2a 2f 0a 2f 2a _*.c files.*/./*
c7e90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e ************* In
c7ea0 63 6c 75 64 65 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e clude os_common.
c7eb0 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 h in the middle
c7ec0 6f 66 20 6f 73 5f 75 6e 69 78 2e 63 20 2a 2a 2a of os_unix.c ***
c7ed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
c7ee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 ************* Be
c7ef0 67 69 6e 20 66 69 6c 65 20 6f 73 5f 63 6f 6d 6d gin file os_comm
c7f00 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a on.h ***********
c7f10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c7f20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
c7f30 0a 2a 2a 20 32 30 30 34 20 4d 61 79 20 32 32 0a .** 2004 May 22.
c7f40 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 **.** The author
c7f50 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 disclaims copyr
c7f60 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 ight to this sou
c7f70 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c rce code. In pl
c7f80 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 ace of.** a lega
c7f90 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 l notice, here i
c7fa0 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a s a blessing:.**
c7fb0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 .** May you d
c7fc0 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 o good and not e
c7fd0 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 vil..** May y
c7fe0 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e ou find forgiven
c7ff0 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 ess for yourself
c8000 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 and forgive oth
c8010 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 ers..** May y
c8020 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c ou share freely,
c8030 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f never taking mo
c8040 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 re than you give
c8050 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ..**.***********
c8060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8090 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c80a0 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 ***.**.** This f
c80b0 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 6d 61 63 ile contains mac
c80c0 72 6f 73 20 61 6e 64 20 61 20 6c 69 74 74 6c 65 ros and a little
c80d0 20 62 69 74 20 6f 66 20 63 6f 64 65 20 74 68 61 bit of code tha
c80e0 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 0a 2a t is common to.*
c80f0 2a 20 61 6c 6c 20 6f 66 20 74 68 65 20 70 6c 61 * all of the pla
c8100 74 66 6f 72 6d 2d 73 70 65 63 69 66 69 63 20 66 tform-specific f
c8110 69 6c 65 73 20 28 6f 73 5f 2a 2e 63 29 20 61 6e iles (os_*.c) an
c8120 64 20 69 73 20 23 69 6e 63 6c 75 64 65 64 20 69 d is #included i
c8130 6e 74 6f 20 74 68 6f 73 65 0a 2a 2a 20 66 69 6c nto those.** fil
c8140 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 es..**.** This f
c8150 69 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 23 69 ile should be #i
c8160 6e 63 6c 75 64 65 64 20 62 79 20 74 68 65 20 6f ncluded by the o
c8170 73 5f 2a 2e 63 20 66 69 6c 65 73 20 6f 6e 6c 79 s_*.c files only
c8180 2e 20 20 49 74 20 69 73 20 6e 6f 74 20 61 0a 2a . It is not a.*
c8190 2a 20 67 65 6e 65 72 61 6c 20 70 75 72 70 6f 73 * general purpos
c81a0 65 20 68 65 61 64 65 72 20 66 69 6c 65 2e 0a 2a e header file..*
c81b0 2a 0a 2a 2a 20 24 49 64 3a 20 6f 73 5f 63 6f 6d *.** $Id: os_com
c81c0 6d 6f 6e 2e 68 2c 76 20 31 2e 33 38 20 32 30 30 mon.h,v 1.38 200
c81d0 39 2f 30 32 2f 32 34 20 31 38 3a 34 30 3a 35 30 9/02/24 18:40:50
c81e0 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 danielk1977 Exp
c81f0 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 4f $.*/.#ifndef _O
c8200 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a 23 64 65 66 S_COMMON_H_.#def
c8210 69 6e 65 20 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 ine _OS_COMMON_H
c8220 5f 0a 0a 2f 2a 0a 2a 2a 20 41 74 20 6c 65 61 73 _../*.** At leas
c8230 74 20 74 77 6f 20 62 75 67 73 20 68 61 76 65 20 t two bugs have
c8240 73 6c 69 70 70 65 64 20 69 6e 20 62 65 63 61 75 slipped in becau
c8250 73 65 20 77 65 20 63 68 61 6e 67 65 64 20 74 68 se we changed th
c8260 65 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47 0a 2a e MEMORY_DEBUG.*
c8270 2a 20 6d 61 63 72 6f 20 74 6f 20 53 51 4c 49 54 * macro to SQLIT
c8280 45 5f 44 45 42 55 47 20 61 6e 64 20 73 6f 6d 65 E_DEBUG and some
c8290 20 6f 6c 64 65 72 20 6d 61 6b 65 66 69 6c 65 73 older makefiles
c82a0 20 68 61 76 65 20 6e 6f 74 20 79 65 74 20 6d 61 have not yet ma
c82b0 64 65 20 74 68 65 0a 2a 2a 20 73 77 69 74 63 68 de the.** switch
c82c0 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 . The following
c82d0 20 63 6f 64 65 20 73 68 6f 75 6c 64 20 63 61 74 code should cat
c82e0 63 68 20 74 68 69 73 20 70 72 6f 62 6c 65 6d 20 ch this problem
c82f0 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 2e at compile-time.
c8300 0a 2a 2f 0a 23 69 66 64 65 66 20 4d 45 4d 4f 52 .*/.#ifdef MEMOR
c8310 59 5f 44 45 42 55 47 0a 23 20 65 72 72 6f 72 20 Y_DEBUG.# error
c8320 22 54 68 65 20 4d 45 4d 4f 52 59 5f 44 45 42 55 "The MEMORY_DEBU
c8330 47 20 6d 61 63 72 6f 20 69 73 20 6f 62 73 6f 6c G macro is obsol
c8340 65 74 65 2e 20 20 55 73 65 20 53 51 4c 49 54 45 ete. Use SQLITE
c8350 5f 44 45 42 55 47 20 69 6e 73 74 65 61 64 2e 22 _DEBUG instead."
c8360 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 .#endif..#ifdef
c8370 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 4c SQLITE_DEBUG.SQL
c8380 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
c8390 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 3d sqlite3OSTrace =
c83a0 20 30 3b 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 0;.#define OSTR
c83b0 41 43 45 31 28 58 29 20 20 20 20 20 20 20 20 20 ACE1(X)
c83c0 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 if( sqlite3OSTra
c83d0 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 ce ) sqlite3Debu
c83e0 67 50 72 69 6e 74 66 28 58 29 0a 23 64 65 66 69 gPrintf(X).#defi
c83f0 6e 65 20 4f 53 54 52 41 43 45 32 28 58 2c 59 29 ne OSTRACE2(X,Y)
c8400 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 if( sqlit
c8410 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69 e3OSTrace ) sqli
c8420 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 te3DebugPrintf(X
c8430 2c 59 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 ,Y).#define OSTR
c8440 41 43 45 33 28 58 2c 59 2c 5a 29 20 20 20 20 20 ACE3(X,Y,Z)
c8450 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 if( sqlite3OSTra
c8460 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 ce ) sqlite3Debu
c8470 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 29 0a 23 gPrintf(X,Y,Z).#
c8480 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 34 28 define OSTRACE4(
c8490 58 2c 59 2c 5a 2c 41 29 20 20 20 69 66 28 20 73 X,Y,Z,A) if( s
c84a0 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 qlite3OSTrace )
c84b0 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e sqlite3DebugPrin
c84c0 74 66 28 58 2c 59 2c 5a 2c 41 29 0a 23 64 65 66 tf(X,Y,Z,A).#def
c84d0 69 6e 65 20 4f 53 54 52 41 43 45 35 28 58 2c 59 ine OSTRACE5(X,Y
c84e0 2c 5a 2c 41 2c 42 29 20 69 66 28 20 73 71 6c 69 ,Z,A,B) if( sqli
c84f0 74 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c te3OSTrace ) sql
c8500 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 ite3DebugPrintf(
c8510 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23 64 65 66 69 X,Y,Z,A,B).#defi
c8520 6e 65 20 4f 53 54 52 41 43 45 36 28 58 2c 59 2c ne OSTRACE6(X,Y,
c8530 5a 2c 41 2c 42 2c 43 29 20 5c 0a 20 20 20 20 69 Z,A,B,C) \. i
c8540 66 28 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 f(sqlite3OSTrace
c8550 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 ) sqlite3DebugPr
c8560 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 intf(X,Y,Z,A,B,C
c8570 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 ).#define OSTRAC
c8580 45 37 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 E7(X,Y,Z,A,B,C,D
c8590 29 20 5c 0a 20 20 20 20 69 66 28 73 71 6c 69 74 ) \. if(sqlit
c85a0 65 33 4f 53 54 72 61 63 65 29 20 73 71 6c 69 74 e3OSTrace) sqlit
c85b0 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c e3DebugPrintf(X,
c85c0 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 0a 23 65 6c Y,Z,A,B,C,D).#el
c85d0 73 65 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 se.#define OSTRA
c85e0 43 45 31 28 58 29 0a 23 64 65 66 69 6e 65 20 4f CE1(X).#define O
c85f0 53 54 52 41 43 45 32 28 58 2c 59 29 0a 23 64 65 STRACE2(X,Y).#de
c8600 66 69 6e 65 20 4f 53 54 52 41 43 45 33 28 58 2c fine OSTRACE3(X,
c8610 59 2c 5a 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 Y,Z).#define OST
c8620 52 41 43 45 34 28 58 2c 59 2c 5a 2c 41 29 0a 23 RACE4(X,Y,Z,A).#
c8630 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 35 28 define OSTRACE5(
c8640 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23 64 65 66 69 X,Y,Z,A,B).#defi
c8650 6e 65 20 4f 53 54 52 41 43 45 36 28 58 2c 59 2c ne OSTRACE6(X,Y,
c8660 5a 2c 41 2c 42 2c 43 29 0a 23 64 65 66 69 6e 65 Z,A,B,C).#define
c8670 20 4f 53 54 52 41 43 45 37 28 58 2c 59 2c 5a 2c OSTRACE7(X,Y,Z,
c8680 41 2c 42 2c 43 2c 44 29 0a 23 65 6e 64 69 66 0a A,B,C,D).#endif.
c8690 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 66 6f ./*.** Macros fo
c86a0 72 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 72 r performance tr
c86b0 61 63 69 6e 67 2e 20 20 4e 6f 72 6d 61 6c 6c 79 acing. Normally
c86c0 20 74 75 72 6e 65 64 20 6f 66 66 2e 20 20 4f 6e turned off. On
c86d0 6c 79 20 77 6f 72 6b 73 0a 2a 2a 20 6f 6e 20 69 ly works.** on i
c86e0 34 38 36 20 68 61 72 64 77 61 72 65 2e 0a 2a 2f 486 hardware..*/
c86f0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 50 .#ifdef SQLITE_P
c8700 45 52 46 4f 52 4d 41 4e 43 45 5f 54 52 41 43 45 ERFORMANCE_TRACE
c8710 0a 0a 2f 2a 20 0a 2a 2a 20 68 77 74 69 6d 65 2e ../* .** hwtime.
c8720 68 20 63 6f 6e 74 61 69 6e 73 20 69 6e 6c 69 6e h contains inlin
c8730 65 20 61 73 73 65 6d 62 6c 65 72 20 63 6f 64 65 e assembler code
c8740 20 66 6f 72 20 69 6d 70 6c 65 6d 65 6e 74 69 6e for implementin
c8750 67 20 0a 2a 2a 20 68 69 67 68 2d 70 65 72 66 6f g .** high-perfo
c8760 72 6d 61 6e 63 65 20 74 69 6d 69 6e 67 20 72 6f rmance timing ro
c8770 75 74 69 6e 65 73 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a utines..*/./****
c8780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 ********** Inclu
c8790 64 65 20 68 77 74 69 6d 65 2e 68 20 69 6e 20 74 de hwtime.h in t
c87a0 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 6f 73 5f he middle of os_
c87b0 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a common.h *******
c87c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
c87d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
c87e0 20 66 69 6c 65 20 68 77 74 69 6d 65 2e 68 20 2a file hwtime.h *
c87f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a *********/./*.**
c8820 20 32 30 30 38 20 4d 61 79 20 32 37 0a 2a 2a 0a 2008 May 27.**.
c8830 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 ** The author di
c8840 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 sclaims copyrigh
c8850 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 t to this source
c8860 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 code. In place
c8870 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e of.** a legal n
c8880 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 otice, here is a
c8890 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a blessing:.**.**
c88a0 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 May you do g
c88b0 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c ood and not evil
c88c0 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
c88d0 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 find forgiveness
c88e0 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e for yourself an
c88f0 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 d forgive others
c8900 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
c8910 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 share freely, ne
c8920 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 ver taking more
c8930 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a than you give..*
c8940 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *.**************
c8950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8970 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8990 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 .**.** This file
c89a0 20 63 6f 6e 74 61 69 6e 73 20 69 6e 6c 69 6e 65 contains inline
c89b0 20 61 73 6d 20 63 6f 64 65 20 66 6f 72 20 72 65 asm code for re
c89c0 74 72 69 65 76 69 6e 67 20 22 68 69 67 68 2d 70 trieving "high-p
c89d0 65 72 66 6f 72 6d 61 6e 63 65 22 0a 2a 2a 20 63 erformance".** c
c89e0 6f 75 6e 74 65 72 73 20 66 6f 72 20 78 38 36 20 ounters for x86
c89f0 63 6c 61 73 73 20 43 50 55 73 2e 0a 2a 2a 0a 2a class CPUs..**.*
c8a00 2a 20 24 49 64 3a 20 68 77 74 69 6d 65 2e 68 2c * $Id: hwtime.h,
c8a10 76 20 31 2e 33 20 32 30 30 38 2f 30 38 2f 30 31 v 1.3 2008/08/01
c8a20 20 31 34 3a 33 33 3a 31 35 20 73 68 61 6e 65 20 14:33:15 shane
c8a30 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 Exp $.*/.#ifndef
c8a40 20 5f 48 57 54 49 4d 45 5f 48 5f 0a 23 64 65 66 _HWTIME_H_.#def
c8a50 69 6e 65 20 5f 48 57 54 49 4d 45 5f 48 5f 0a 0a ine _HWTIME_H_..
c8a60 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 /*.** The follow
c8a70 69 6e 67 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 ing routine only
c8a80 20 77 6f 72 6b 73 20 6f 6e 20 70 65 6e 74 69 75 works on pentiu
c8a90 6d 2d 63 6c 61 73 73 20 28 6f 72 20 6e 65 77 65 m-class (or newe
c8aa0 72 29 20 70 72 6f 63 65 73 73 6f 72 73 2e 0a 2a r) processors..*
c8ab0 2a 20 49 74 20 75 73 65 73 20 74 68 65 20 52 44 * It uses the RD
c8ac0 54 53 43 20 6f 70 63 6f 64 65 20 74 6f 20 72 65 TSC opcode to re
c8ad0 61 64 20 74 68 65 20 63 79 63 6c 65 20 63 6f 75 ad the cycle cou
c8ae0 6e 74 20 76 61 6c 75 65 20 6f 75 74 20 6f 66 20 nt value out of
c8af0 74 68 65 0a 2a 2a 20 70 72 6f 63 65 73 73 6f 72 the.** processor
c8b00 20 61 6e 64 20 72 65 74 75 72 6e 73 20 74 68 61 and returns tha
c8b10 74 20 76 61 6c 75 65 2e 20 20 54 68 69 73 20 63 t value. This c
c8b20 61 6e 20 62 65 20 75 73 65 64 20 66 6f 72 20 68 an be used for h
c8b30 69 67 68 2d 72 65 73 0a 2a 2a 20 70 72 6f 66 69 igh-res.** profi
c8b40 6c 69 6e 67 2e 0a 2a 2f 0a 23 69 66 20 28 64 65 ling..*/.#if (de
c8b50 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 fined(__GNUC__)
c8b60 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f || defined(_MSC_
c8b70 56 45 52 29 29 20 26 26 20 5c 0a 20 20 20 20 20 VER)) && \.
c8b80 20 28 64 65 66 69 6e 65 64 28 69 33 38 36 29 20 (defined(i386)
c8b90 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f 69 33 38 || defined(__i38
c8ba0 36 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 6__) || defined(
c8bb0 5f 4d 5f 49 58 38 36 29 29 0a 0a 20 20 23 69 66 _M_IX86)).. #if
c8bc0 20 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f defined(__GNUC_
c8bd0 5f 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f _).. __inline__
c8be0 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 sqlite_uint64 s
c8bf0 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 qlite3Hwtime(voi
c8c00 64 29 7b 0a 20 20 20 20 20 75 6e 73 69 67 6e 65 d){. unsigne
c8c10 64 20 69 6e 74 20 6c 6f 2c 20 68 69 3b 0a 20 20 d int lo, hi;.
c8c20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c __asm__ __vol
c8c30 61 74 69 6c 65 5f 5f 20 28 22 72 64 74 73 63 22 atile__ ("rdtsc"
c8c40 20 3a 20 22 3d 61 22 20 28 6c 6f 29 2c 20 22 3d : "=a" (lo), "=
c8c50 64 22 20 28 68 69 29 29 3b 0a 20 20 20 20 20 72 d" (hi));. r
c8c60 65 74 75 72 6e 20 28 73 71 6c 69 74 65 5f 75 69 eturn (sqlite_ui
c8c70 6e 74 36 34 29 68 69 20 3c 3c 20 33 32 20 7c 20 nt64)hi << 32 |
c8c80 6c 6f 3b 0a 20 20 7d 0a 0a 20 20 23 65 6c 69 66 lo;. }.. #elif
c8c90 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45 defined(_MSC_VE
c8ca0 52 29 0a 0a 20 20 5f 5f 64 65 63 6c 73 70 65 63 R).. __declspec
c8cb0 28 6e 61 6b 65 64 29 20 5f 5f 69 6e 6c 69 6e 65 (naked) __inline
c8cc0 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 5f sqlite_uint64 _
c8cd0 5f 63 64 65 63 6c 20 73 71 6c 69 74 65 33 48 77 _cdecl sqlite3Hw
c8ce0 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 time(void){.
c8cf0 20 5f 5f 61 73 6d 20 7b 0a 20 20 20 20 20 20 20 __asm {.
c8d00 20 72 64 74 73 63 0a 20 20 20 20 20 20 20 20 72 rdtsc. r
c8d10 65 74 20 20 20 20 20 20 20 3b 20 72 65 74 75 72 et ; retur
c8d20 6e 20 76 61 6c 75 65 20 61 74 20 45 44 58 3a 45 n value at EDX:E
c8d30 41 58 0a 20 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 AX. }. }..
c8d40 20 23 65 6e 64 69 66 0a 0a 23 65 6c 69 66 20 28 #endif..#elif (
c8d50 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f defined(__GNUC__
c8d60 29 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 78 ) && defined(__x
c8d70 38 36 5f 36 34 5f 5f 29 29 0a 0a 20 20 5f 5f 69 86_64__)).. __i
c8d80 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 nline__ sqlite_u
c8d90 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 int64 sqlite3Hwt
c8da0 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 ime(void){.
c8db0 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 76 unsigned long v
c8dc0 61 6c 3b 0a 20 20 20 20 20 20 5f 5f 61 73 6d 5f al;. __asm_
c8dd0 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 _ __volatile__ (
c8de0 22 72 64 74 73 63 22 20 3a 20 22 3d 41 22 20 28 "rdtsc" : "=A" (
c8df0 76 61 6c 29 29 3b 0a 20 20 20 20 20 20 72 65 74 val));. ret
c8e00 75 72 6e 20 76 61 6c 3b 0a 20 20 7d 0a 20 0a 23 urn val;. }. .#
c8e10 65 6c 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f elif (defined(__
c8e20 47 4e 55 43 5f 5f 29 20 26 26 20 64 65 66 69 6e GNUC__) && defin
c8e30 65 64 28 5f 5f 70 70 63 5f 5f 29 29 0a 0a 20 20 ed(__ppc__))..
c8e40 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 __inline__ sqlit
c8e50 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 e_uint64 sqlite3
c8e60 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 Hwtime(void){.
c8e70 20 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e unsigned lon
c8e80 67 20 6c 6f 6e 67 20 72 65 74 76 61 6c 3b 0a 20 g long retval;.
c8e90 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f unsigned lo
c8ea0 6e 67 20 6a 75 6e 6b 3b 0a 20 20 20 20 20 20 5f ng junk;. _
c8eb0 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c _asm__ __volatil
c8ec0 65 5f 5f 20 28 22 5c 6e 5c 0a 20 20 20 20 20 20 e__ ("\n\.
c8ed0 20 20 20 20 31 3a 20 20 20 20 20 20 6d 66 74 62 1: mftb
c8ee0 75 20 20 20 25 31 5c 6e 5c 0a 20 20 20 20 20 20 u %1\n\.
c8ef0 20 20 20 20 20 20 20 20 20 20 20 20 6d 66 74 62 mftb
c8f00 20 20 20 20 25 4c 30 5c 6e 5c 0a 20 20 20 20 20 %L0\n\.
c8f10 20 20 20 20 20 20 20 20 20 20 20 20 20 6d 66 74 mft
c8f20 62 75 20 20 20 25 30 5c 6e 5c 0a 20 20 20 20 20 bu %0\n\.
c8f30 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6d 70 cmp
c8f40 77 20 20 20 20 25 30 2c 25 31 5c 6e 5c 0a 20 20 w %0,%1\n\.
c8f50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c8f60 62 6e 65 20 20 20 20 20 31 62 22 0a 20 20 20 20 bne 1b".
c8f70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 3a 20 :
c8f80 22 3d 72 22 20 28 72 65 74 76 61 6c 29 2c 20 22 "=r" (retval), "
c8f90 3d 72 22 20 28 6a 75 6e 6b 29 29 3b 0a 20 20 20 =r" (junk));.
c8fa0 20 20 20 72 65 74 75 72 6e 20 72 65 74 76 61 6c return retval
c8fb0 3b 0a 20 20 7d 0a 0a 23 65 6c 73 65 0a 0a 20 20 ;. }..#else..
c8fc0 23 65 72 72 6f 72 20 4e 65 65 64 20 69 6d 70 6c #error Need impl
c8fd0 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 73 71 ementation of sq
c8fe0 6c 69 74 65 33 48 77 74 69 6d 65 28 29 20 66 6f lite3Hwtime() fo
c8ff0 72 20 79 6f 75 72 20 70 6c 61 74 66 6f 72 6d 2e r your platform.
c9000 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 6f 20 63 .. /*. ** To c
c9010 6f 6d 70 69 6c 65 20 77 69 74 68 6f 75 74 20 69 ompile without i
c9020 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 73 71 6c 69 mplementing sqli
c9030 74 65 33 48 77 74 69 6d 65 28 29 20 66 6f 72 20 te3Hwtime() for
c9040 79 6f 75 72 20 70 6c 61 74 66 6f 72 6d 2c 0a 20 your platform,.
c9050 20 2a 2a 20 79 6f 75 20 63 61 6e 20 72 65 6d 6f ** you can remo
c9060 76 65 20 74 68 65 20 61 62 6f 76 65 20 23 65 72 ve the above #er
c9070 72 6f 72 20 61 6e 64 20 75 73 65 20 74 68 65 20 ror and use the
c9080 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 2a 2a 20 73 following. ** s
c9090 74 75 62 20 66 75 6e 63 74 69 6f 6e 2e 20 20 59 tub function. Y
c90a0 6f 75 20 77 69 6c 6c 20 6c 6f 73 65 20 74 69 6d ou will lose tim
c90b0 69 6e 67 20 73 75 70 70 6f 72 74 20 66 6f 72 20 ing support for
c90c0 6d 61 6e 79 0a 20 20 2a 2a 20 6f 66 20 74 68 65 many. ** of the
c90d0 20 64 65 62 75 67 67 69 6e 67 20 61 6e 64 20 74 debugging and t
c90e0 65 73 74 69 6e 67 20 75 74 69 6c 69 74 69 65 73 esting utilities
c90f0 2c 20 62 75 74 20 69 74 20 73 68 6f 75 6c 64 20 , but it should
c9100 61 74 0a 20 20 2a 2a 20 6c 65 61 73 74 20 63 6f at. ** least co
c9110 6d 70 69 6c 65 20 61 6e 64 20 72 75 6e 2e 0a 20 mpile and run..
c9120 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 */.SQLITE_PRIVA
c9130 54 45 20 20 20 73 71 6c 69 74 65 5f 75 69 6e 74 TE sqlite_uint
c9140 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 64 sqlite3Hwtime
c9150 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 28 (void){ return (
c9160 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 30 (sqlite_uint64)0
c9170 29 3b 20 7d 0a 0a 23 65 6e 64 69 66 0a 0a 23 65 ); }..#endif..#e
c9180 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 ndif /* !defined
c9190 28 5f 48 57 54 49 4d 45 5f 48 5f 29 20 2a 2f 0a (_HWTIME_H_) */.
c91a0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
c91b0 20 45 6e 64 20 6f 66 20 68 77 74 69 6d 65 2e 68 End of hwtime.h
c91c0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
c91d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c91e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
c91f0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
c9200 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 Continuing wher
c9210 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e e we left off in
c9220 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a os_common.h ***
c9230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
c9240 0a 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 5f ..static sqlite_
c9250 75 69 6e 74 36 34 20 67 5f 73 74 61 72 74 3b 0a uint64 g_start;.
c9260 73 74 61 74 69 63 20 73 71 6c 69 74 65 5f 75 69 static sqlite_ui
c9270 6e 74 36 34 20 67 5f 65 6c 61 70 73 65 64 3b 0a nt64 g_elapsed;.
c9280 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 53 54 #define TIMER_ST
c9290 41 52 54 20 20 20 20 20 20 20 67 5f 73 74 61 72 ART g_star
c92a0 74 3d 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 t=sqlite3Hwtime(
c92b0 29 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f ).#define TIMER_
c92c0 45 4e 44 20 20 20 20 20 20 20 20 20 67 5f 65 6c END g_el
c92d0 61 70 73 65 64 3d 73 71 6c 69 74 65 33 48 77 74 apsed=sqlite3Hwt
c92e0 69 6d 65 28 29 2d 67 5f 73 74 61 72 74 0a 23 64 ime()-g_start.#d
c92f0 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4c 41 50 efine TIMER_ELAP
c9300 53 45 44 20 20 20 20 20 67 5f 65 6c 61 70 73 65 SED g_elapse
c9310 64 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 d.#else.#define
c9320 54 49 4d 45 52 5f 53 54 41 52 54 0a 23 64 65 66 TIMER_START.#def
c9330 69 6e 65 20 54 49 4d 45 52 5f 45 4e 44 0a 23 64 ine TIMER_END.#d
c9340 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4c 41 50 efine TIMER_ELAP
c9350 53 45 44 20 20 20 20 20 28 28 73 71 6c 69 74 65 SED ((sqlite
c9360 5f 75 69 6e 74 36 34 29 30 29 0a 23 65 6e 64 69 _uint64)0).#endi
c9370 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 63 f../*.** If we c
c9380 6f 6d 70 69 6c 65 20 77 69 74 68 20 74 68 65 20 ompile with the
c9390 53 51 4c 49 54 45 5f 54 45 53 54 20 6d 61 63 72 SQLITE_TEST macr
c93a0 6f 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20 o set, then the
c93b0 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 0a following block.
c93c0 2a 2a 20 6f 66 20 63 6f 64 65 20 77 69 6c 6c 20 ** of code will
c93d0 67 69 76 65 20 75 73 20 74 68 65 20 61 62 69 6c give us the abil
c93e0 69 74 79 20 74 6f 20 73 69 6d 75 6c 61 74 65 20 ity to simulate
c93f0 61 20 64 69 73 6b 20 49 2f 4f 20 65 72 72 6f 72 a disk I/O error
c9400 2e 20 20 54 68 69 73 0a 2a 2a 20 69 73 20 75 73 . This.** is us
c9410 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 20 74 ed for testing t
c9420 68 65 20 49 2f 4f 20 72 65 63 6f 76 65 72 79 20 he I/O recovery
c9430 6c 6f 67 69 63 2e 0a 2a 2f 0a 23 69 66 64 65 66 logic..*/.#ifdef
c9440 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c SQLITE_TEST.SQL
c9450 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
c9460 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 te3_io_error_hit
c9470 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 = 0;
c9480 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 /* Total number
c9490 20 6f 66 20 49 2f 4f 20 45 72 72 6f 72 73 20 2a of I/O Errors *
c94a0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
c94b0 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f sqlite3_io_erro
c94c0 72 5f 68 61 72 64 68 69 74 20 3d 20 30 3b 20 20 r_hardhit = 0;
c94d0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
c94e0 6f 66 20 6e 6f 6e 2d 62 65 6e 69 67 6e 20 65 72 of non-benign er
c94f0 72 6f 72 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 rors */.SQLITE_A
c9500 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 PI int sqlite3_i
c9510 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 20 o_error_pending
c9520 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 = 0; /* C
c9530 6f 75 6e 74 20 64 6f 77 6e 20 74 6f 20 66 69 72 ount down to fir
c9540 73 74 20 49 2f 4f 20 65 72 72 6f 72 20 2a 2f 0a st I/O error */.
c9550 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
c9560 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f qlite3_io_error_
c9570 70 65 72 73 69 73 74 20 3d 20 30 3b 20 20 20 20 persist = 0;
c9580 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 49 /* True if I
c9590 2f 4f 20 65 72 72 6f 72 73 20 70 65 72 73 69 73 /O errors persis
c95a0 74 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 t */.SQLITE_API
c95b0 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 int sqlite3_io_e
c95c0 72 72 6f 72 5f 62 65 6e 69 67 6e 20 3d 20 30 3b rror_benign = 0;
c95d0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
c95e0 20 69 66 20 65 72 72 6f 72 73 20 61 72 65 20 62 if errors are b
c95f0 65 6e 69 67 6e 20 2a 2f 0a 53 51 4c 49 54 45 5f enign */.SQLITE_
c9600 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
c9610 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 diskfull_pending
c9620 20 3d 20 30 3b 0a 53 51 4c 49 54 45 5f 41 50 49 = 0;.SQLITE_API
c9630 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 69 73 int sqlite3_dis
c9640 6b 66 75 6c 6c 20 3d 20 30 3b 0a 23 64 65 66 69 kfull = 0;.#defi
c9650 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 ne SimulateIOErr
c9660 6f 72 42 65 6e 69 67 6e 28 58 29 20 73 71 6c 69 orBenign(X) sqli
c9670 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e te3_io_error_ben
c9680 69 67 6e 3d 28 58 29 0a 23 64 65 66 69 6e 65 20 ign=(X).#define
c9690 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 SimulateIOError(
c96a0 43 4f 44 45 29 20 20 5c 0a 20 20 69 66 28 20 28 CODE) \. if( (
c96b0 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 sqlite3_io_error
c96c0 5f 70 65 72 73 69 73 74 20 26 26 20 73 71 6c 69 _persist && sqli
c96d0 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 te3_io_error_hit
c96e0 29 20 5c 0a 20 20 20 20 20 20 20 7c 7c 20 73 71 ) \. || sq
c96f0 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 lite3_io_error_p
c9700 65 6e 64 69 6e 67 2d 2d 20 3d 3d 20 31 20 29 20 ending-- == 1 )
c9710 20 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 \.
c9720 20 7b 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 29 { local_ioerr()
c9730 3b 20 43 4f 44 45 3b 20 7d 0a 73 74 61 74 69 63 ; CODE; }.static
c9740 20 76 6f 69 64 20 6c 6f 63 61 6c 5f 69 6f 65 72 void local_ioer
c9750 72 28 29 7b 0a 20 20 49 4f 54 52 41 43 45 28 28 r(){. IOTRACE((
c9760 22 49 4f 45 52 52 5c 6e 22 29 29 3b 0a 20 20 73 "IOERR\n"));. s
c9770 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f qlite3_io_error_
c9780 68 69 74 2b 2b 3b 0a 20 20 69 66 28 20 21 73 71 hit++;. if( !sq
c9790 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 lite3_io_error_b
c97a0 65 6e 69 67 6e 20 29 20 73 71 6c 69 74 65 33 5f enign ) sqlite3_
c97b0 69 6f 5f 65 72 72 6f 72 5f 68 61 72 64 68 69 74 io_error_hardhit
c97c0 2b 2b 3b 0a 7d 0a 23 64 65 66 69 6e 65 20 53 69 ++;.}.#define Si
c97d0 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 72 mulateDiskfullEr
c97e0 72 6f 72 28 43 4f 44 45 29 20 5c 0a 20 20 20 69 ror(CODE) \. i
c97f0 66 28 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 f( sqlite3_diskf
c9800 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 29 7b 20 5c ull_pending ){ \
c9810 0a 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 . if( sqlite
c9820 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 3_diskfull_pendi
c9830 6e 67 20 3d 3d 20 31 20 29 7b 20 5c 0a 20 20 20 ng == 1 ){ \.
c9840 20 20 20 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 local_ioerr(
c9850 29 3b 20 5c 0a 20 20 20 20 20 20 20 73 71 6c 69 ); \. sqli
c9860 74 65 33 5f 64 69 73 6b 66 75 6c 6c 20 3d 20 31 te3_diskfull = 1
c9870 3b 20 5c 0a 20 20 20 20 20 20 20 73 71 6c 69 74 ; \. sqlit
c9880 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 20 e3_io_error_hit
c9890 3d 20 31 3b 20 5c 0a 20 20 20 20 20 20 20 43 4f = 1; \. CO
c98a0 44 45 3b 20 5c 0a 20 20 20 20 20 7d 65 6c 73 65 DE; \. }else
c98b0 7b 20 5c 0a 20 20 20 20 20 20 20 73 71 6c 69 74 { \. sqlit
c98c0 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 e3_diskfull_pend
c98d0 69 6e 67 2d 2d 3b 20 5c 0a 20 20 20 20 20 7d 20 ing--; \. }
c98e0 5c 0a 20 20 20 7d 0a 23 65 6c 73 65 0a 23 64 65 \. }.#else.#de
c98f0 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 fine SimulateIOE
c9900 72 72 6f 72 42 65 6e 69 67 6e 28 58 29 0a 23 64 rrorBenign(X).#d
c9910 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f efine SimulateIO
c9920 45 72 72 6f 72 28 41 29 0a 23 64 65 66 69 6e 65 Error(A).#define
c9930 20 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c SimulateDiskful
c9940 6c 45 72 72 6f 72 28 41 29 0a 23 65 6e 64 69 66 lError(A).#endif
c9950 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 74 65 73 ../*.** When tes
c9960 74 69 6e 67 2c 20 6b 65 65 70 20 61 20 63 6f 75 ting, keep a cou
c9970 6e 74 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 nt of the number
c9980 20 6f 66 20 6f 70 65 6e 20 66 69 6c 65 73 2e 0a of open files..
c9990 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
c99a0 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 _TEST.SQLITE_API
c99b0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 70 65 int sqlite3_ope
c99c0 6e 5f 66 69 6c 65 5f 63 6f 75 6e 74 20 3d 20 30 n_file_count = 0
c99d0 3b 0a 23 64 65 66 69 6e 65 20 4f 70 65 6e 43 6f ;.#define OpenCo
c99e0 75 6e 74 65 72 28 58 29 20 20 73 71 6c 69 74 65 unter(X) sqlite
c99f0 33 5f 6f 70 65 6e 5f 66 69 6c 65 5f 63 6f 75 6e 3_open_file_coun
c9a00 74 2b 3d 28 58 29 0a 23 65 6c 73 65 0a 23 64 65 t+=(X).#else.#de
c9a10 66 69 6e 65 20 4f 70 65 6e 43 6f 75 6e 74 65 72 fine OpenCounter
c9a20 28 58 29 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 (X).#endif..#end
c9a30 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 5f if /* !defined(_
c9a40 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 29 20 2a 2f OS_COMMON_H_) */
c9a50 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
c9a60 2a 20 45 6e 64 20 6f 66 20 6f 73 5f 63 6f 6d 6d * End of os_comm
c9a70 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a on.h ***********
c9a80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c9a90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c9aa0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
c9ab0 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 * Continuing whe
c9ac0 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 re we left off i
c9ad0 6e 20 6f 73 5f 75 6e 69 78 2e 63 20 2a 2a 2a 2a n os_unix.c ****
c9ae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c9af0 2f 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 69 6e 65 20 /../*.** Define
c9b00 76 61 72 69 6f 75 73 20 6d 61 63 72 6f 73 20 74 various macros t
c9b10 68 61 74 20 61 72 65 20 6d 69 73 73 69 6e 67 20 hat are missing
c9b20 66 72 6f 6d 20 73 6f 6d 65 20 73 79 73 74 65 6d from some system
c9b30 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4f 5f s..*/.#ifndef O_
c9b40 4c 41 52 47 45 46 49 4c 45 0a 23 20 64 65 66 69 LARGEFILE.# defi
c9b50 6e 65 20 4f 5f 4c 41 52 47 45 46 49 4c 45 20 30 ne O_LARGEFILE 0
c9b60 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 .#endif.#ifdef S
c9b70 51 4c 49 54 45 5f 44 49 53 41 42 4c 45 5f 4c 46 QLITE_DISABLE_LF
c9b80 53 0a 23 20 75 6e 64 65 66 20 4f 5f 4c 41 52 47 S.# undef O_LARG
c9b90 45 46 49 4c 45 0a 23 20 64 65 66 69 6e 65 20 4f EFILE.# define O
c9ba0 5f 4c 41 52 47 45 46 49 4c 45 20 30 0a 23 65 6e _LARGEFILE 0.#en
c9bb0 64 69 66 0a 23 69 66 6e 64 65 66 20 4f 5f 4e 4f dif.#ifndef O_NO
c9bc0 46 4f 4c 4c 4f 57 0a 23 20 64 65 66 69 6e 65 20 FOLLOW.# define
c9bd0 4f 5f 4e 4f 46 4f 4c 4c 4f 57 20 30 0a 23 65 6e O_NOFOLLOW 0.#en
c9be0 64 69 66 0a 23 69 66 6e 64 65 66 20 4f 5f 42 49 dif.#ifndef O_BI
c9bf0 4e 41 52 59 0a 23 20 64 65 66 69 6e 65 20 4f 5f NARY.# define O_
c9c00 42 49 4e 41 52 59 20 30 0a 23 65 6e 64 69 66 0a BINARY 0.#endif.
c9c10 0a 2f 2a 0a 2a 2a 20 54 68 65 20 44 4a 47 50 50 ./*.** The DJGPP
c9c20 20 63 6f 6d 70 69 6c 65 72 20 65 6e 76 69 72 6f compiler enviro
c9c30 6e 6d 65 6e 74 20 6c 6f 6f 6b 73 20 6d 6f 73 74 nment looks most
c9c40 6c 79 20 6c 69 6b 65 20 55 6e 69 78 2c 20 62 75 ly like Unix, bu
c9c50 74 20 69 74 0a 2a 2a 20 6c 61 63 6b 73 20 74 68 t it.** lacks th
c9c60 65 20 66 63 6e 74 6c 28 29 20 73 79 73 74 65 6d e fcntl() system
c9c70 20 63 61 6c 6c 2e 20 20 53 6f 20 72 65 64 65 66 call. So redef
c9c80 69 6e 65 20 66 63 6e 74 6c 28 29 20 74 6f 20 62 ine fcntl() to b
c9c90 65 20 73 6f 6d 65 74 68 69 6e 67 0a 2a 2a 20 74 e something.** t
c9ca0 68 61 74 20 61 6c 77 61 79 73 20 73 75 63 63 65 hat always succe
c9cb0 65 64 73 2e 20 20 54 68 69 73 20 6d 65 61 6e 73 eds. This means
c9cc0 20 74 68 61 74 20 6c 6f 63 6b 69 6e 67 20 64 6f that locking do
c9cd0 65 73 20 6e 6f 74 20 6f 63 63 75 72 20 75 6e 64 es not occur und
c9ce0 65 72 0a 2a 2a 20 44 4a 47 50 50 2e 20 20 42 75 er.** DJGPP. Bu
c9cf0 74 20 69 74 20 69 73 20 44 4f 53 20 2d 20 77 68 t it is DOS - wh
c9d00 61 74 20 64 69 64 20 79 6f 75 20 65 78 70 65 63 at did you expec
c9d10 74 3f 0a 2a 2f 0a 23 69 66 64 65 66 20 5f 5f 44 t?.*/.#ifdef __D
c9d20 4a 47 50 50 5f 5f 0a 23 20 64 65 66 69 6e 65 20 JGPP__.# define
c9d30 66 63 6e 74 6c 28 41 2c 42 2c 43 29 20 30 0a 23 fcntl(A,B,C) 0.#
c9d40 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 endif../*.** The
c9d50 20 74 68 72 65 61 64 69 64 20 6d 61 63 72 6f 20 threadid macro
c9d60 72 65 73 6f 6c 76 65 73 20 74 6f 20 74 68 65 20 resolves to the
c9d70 74 68 72 65 61 64 2d 69 64 20 6f 72 20 74 6f 20 thread-id or to
c9d80 30 2e 20 20 55 73 65 64 20 66 6f 72 0a 2a 2a 20 0. Used for.**
c9d90 74 65 73 74 69 6e 67 20 61 6e 64 20 64 65 62 75 testing and debu
c9da0 67 67 69 6e 67 20 6f 6e 6c 79 2e 0a 2a 2f 0a 23 gging only..*/.#
c9db0 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 if SQLITE_THREAD
c9dc0 53 41 46 45 0a 23 64 65 66 69 6e 65 20 74 68 72 SAFE.#define thr
c9dd0 65 61 64 69 64 20 70 74 68 72 65 61 64 5f 73 65 eadid pthread_se
c9de0 6c 66 28 29 0a 23 65 6c 73 65 0a 23 64 65 66 69 lf().#else.#defi
c9df0 6e 65 20 74 68 72 65 61 64 69 64 20 30 0a 23 65 ne threadid 0.#e
c9e00 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c ndif.../*.** Hel
c9e10 70 65 72 20 66 75 6e 63 74 69 6f 6e 73 20 74 6f per functions to
c9e20 20 6f 62 74 61 69 6e 20 61 6e 64 20 72 65 6c 69 obtain and reli
c9e30 6e 71 75 69 73 68 20 74 68 65 20 67 6c 6f 62 61 nquish the globa
c9e40 6c 20 6d 75 74 65 78 2e 20 54 68 65 0a 2a 2a 20 l mutex. The.**
c9e50 67 6c 6f 62 61 6c 20 6d 75 74 65 78 20 69 73 20 global mutex is
c9e60 75 73 65 64 20 74 6f 20 70 72 6f 74 65 63 74 20 used to protect
c9e70 74 68 65 20 75 6e 69 78 4f 70 65 6e 43 6e 74 2c the unixOpenCnt,
c9e80 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 61 6e unixLockInfo an
c9e90 64 0a 2a 2a 20 76 78 77 6f 72 6b 73 46 69 6c 65 d.** vxworksFile
c9ea0 49 64 20 6f 62 6a 65 63 74 73 20 75 73 65 64 20 Id objects used
c9eb0 62 79 20 74 68 69 73 20 66 69 6c 65 2c 20 61 6c by this file, al
c9ec0 6c 20 6f 66 20 77 68 69 63 68 20 6d 61 79 20 62 l of which may b
c9ed0 65 20 0a 2a 2a 20 73 68 61 72 65 64 20 62 79 20 e .** shared by
c9ee0 6d 75 6c 74 69 70 6c 65 20 74 68 72 65 61 64 73 multiple threads
c9ef0 2e 0a 2a 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e ..**.** Function
c9f00 20 75 6e 69 78 4d 75 74 65 78 48 65 6c 64 28 29 unixMutexHeld()
c9f10 20 69 73 20 75 73 65 64 20 74 6f 20 61 73 73 65 is used to asse
c9f20 72 74 28 29 20 74 68 61 74 20 74 68 65 20 67 6c rt() that the gl
c9f30 6f 62 61 6c 20 6d 75 74 65 78 20 0a 2a 2a 20 69 obal mutex .** i
c9f40 73 20 68 65 6c 64 20 77 68 65 6e 20 72 65 71 75 s held when requ
c9f50 69 72 65 64 2e 20 54 68 69 73 20 66 75 6e 63 74 ired. This funct
c9f60 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 ion is only used
c9f70 20 61 73 20 70 61 72 74 20 6f 66 20 61 73 73 65 as part of asse
c9f80 72 74 28 29 20 0a 2a 2a 20 73 74 61 74 65 6d 65 rt() .** stateme
c9f90 6e 74 73 2e 20 65 2e 67 2e 0a 2a 2a 0a 2a 2a 20 nts. e.g..**.**
c9fa0 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 unixEnterMutex
c9fb0 28 29 0a 2a 2a 20 20 20 20 20 61 73 73 65 72 74 ().** assert
c9fc0 28 20 75 6e 69 78 4d 75 74 65 78 48 65 6c 64 28 ( unixMutexHeld(
c9fd0 29 20 29 3b 0a 2a 2a 20 20 20 75 6e 69 78 45 6e ) );.** unixEn
c9fe0 74 65 72 4c 65 61 76 65 28 29 0a 2a 2f 0a 73 74 terLeave().*/.st
c9ff0 61 74 69 63 20 76 6f 69 64 20 75 6e 69 78 45 6e atic void unixEn
ca000 74 65 72 4d 75 74 65 78 28 76 6f 69 64 29 7b 0a terMutex(void){.
ca010 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
ca020 65 6e 74 65 72 28 73 71 6c 69 74 65 33 4d 75 74 enter(sqlite3Mut
ca030 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d exAlloc(SQLITE_M
ca040 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 UTEX_STATIC_MAST
ca050 45 52 29 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 ER));.}.static v
ca060 6f 69 64 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 oid unixLeaveMut
ca070 65 78 28 76 6f 69 64 29 7b 0a 20 20 73 71 6c 69 ex(void){. sqli
ca080 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
ca090 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f sqlite3MutexAllo
ca0a0 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 c(SQLITE_MUTEX_S
ca0b0 54 41 54 49 43 5f 4d 41 53 54 45 52 29 29 3b 0a TATIC_MASTER));.
ca0c0 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f }.#ifdef SQLITE_
ca0d0 44 45 42 55 47 0a 73 74 61 74 69 63 20 69 6e 74 DEBUG.static int
ca0e0 20 75 6e 69 78 4d 75 74 65 78 48 65 6c 64 28 76 unixMutexHeld(v
ca0f0 6f 69 64 29 20 7b 0a 20 20 72 65 74 75 72 6e 20 oid) {. return
ca100 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
ca110 6c 64 28 73 71 6c 69 74 65 33 4d 75 74 65 78 41 ld(sqlite3MutexA
ca120 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 lloc(SQLITE_MUTE
ca130 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 X_STATIC_MASTER)
ca140 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 23 69 );.}.#endif...#i
ca150 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 fdef SQLITE_DEBU
ca160 47 0a 2f 2a 0a 2a 2a 20 48 65 6c 70 65 72 20 66 G./*.** Helper f
ca170 75 6e 63 74 69 6f 6e 20 66 6f 72 20 70 72 69 6e unction for prin
ca180 74 69 6e 67 20 6f 75 74 20 74 72 61 63 65 20 69 ting out trace i
ca190 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 nformation from
ca1a0 64 65 62 75 67 67 69 6e 67 0a 2a 2a 20 62 69 6e debugging.** bin
ca1b0 61 72 69 65 73 2e 20 54 68 69 73 20 72 65 74 75 aries. This retu
ca1c0 72 6e 73 20 74 68 65 20 73 74 72 69 6e 67 20 72 rns the string r
ca1d0 65 70 72 65 73 65 74 61 74 69 6f 6e 20 6f 66 20 epresetation of
ca1e0 74 68 65 20 73 75 70 70 6c 69 65 64 0a 2a 2a 20 the supplied.**
ca1f0 69 6e 74 65 67 65 72 20 6c 6f 63 6b 2d 74 79 70 integer lock-typ
ca200 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e e..*/.static con
ca210 73 74 20 63 68 61 72 20 2a 6c 6f 63 6b 74 79 70 st char *locktyp
ca220 65 4e 61 6d 65 28 69 6e 74 20 6c 6f 63 6b 74 79 eName(int lockty
ca230 70 65 29 7b 0a 20 20 73 77 69 74 63 68 28 20 6c pe){. switch( l
ca240 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 63 ocktype ){. c
ca250 61 73 65 20 4e 4f 5f 4c 4f 43 4b 3a 20 72 65 74 ase NO_LOCK: ret
ca260 75 72 6e 20 22 4e 4f 4e 45 22 3b 0a 20 20 20 20 urn "NONE";.
ca270 63 61 73 65 20 53 48 41 52 45 44 5f 4c 4f 43 4b case SHARED_LOCK
ca280 3a 20 72 65 74 75 72 6e 20 22 53 48 41 52 45 44 : return "SHARED
ca290 22 3b 0a 20 20 20 20 63 61 73 65 20 52 45 53 45 ";. case RESE
ca2a0 52 56 45 44 5f 4c 4f 43 4b 3a 20 72 65 74 75 72 RVED_LOCK: retur
ca2b0 6e 20 22 52 45 53 45 52 56 45 44 22 3b 0a 20 20 n "RESERVED";.
ca2c0 20 20 63 61 73 65 20 50 45 4e 44 49 4e 47 5f 4c case PENDING_L
ca2d0 4f 43 4b 3a 20 72 65 74 75 72 6e 20 22 50 45 4e OCK: return "PEN
ca2e0 44 49 4e 47 22 3b 0a 20 20 20 20 63 61 73 65 20 DING";. case
ca2f0 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 3a 20 EXCLUSIVE_LOCK:
ca300 72 65 74 75 72 6e 20 22 45 58 43 4c 55 53 49 56 return "EXCLUSIV
ca310 45 22 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e E";. }. return
ca320 20 22 45 52 52 4f 52 22 3b 0a 7d 0a 23 65 6e 64 "ERROR";.}.#end
ca330 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 if..#ifdef SQLIT
ca340 45 5f 4c 4f 43 4b 5f 54 52 41 43 45 0a 2f 2a 0a E_LOCK_TRACE./*.
ca350 2a 2a 20 50 72 69 6e 74 20 6f 75 74 20 69 6e 66 ** Print out inf
ca360 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 61 ormation about a
ca370 6c 6c 20 6c 6f 63 6b 69 6e 67 20 6f 70 65 72 61 ll locking opera
ca380 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 tions..**.** Thi
ca390 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 s routine is use
ca3a0 64 20 66 6f 72 20 74 72 6f 75 62 6c 65 73 68 6f d for troublesho
ca3b0 6f 74 69 6e 67 20 6c 6f 63 6b 73 20 6f 6e 20 6d oting locks on m
ca3c0 75 6c 74 69 74 68 72 65 61 64 65 64 0a 2a 2a 20 ultithreaded.**
ca3d0 70 6c 61 74 66 6f 72 6d 73 2e 20 20 45 6e 61 62 platforms. Enab
ca3e0 6c 65 20 62 79 20 63 6f 6d 70 69 6c 69 6e 67 20 le by compiling
ca3f0 77 69 74 68 20 74 68 65 20 2d 44 53 51 4c 49 54 with the -DSQLIT
ca400 45 5f 4c 4f 43 4b 5f 54 52 41 43 45 0a 2a 2a 20 E_LOCK_TRACE.**
ca410 63 6f 6d 6d 61 6e 64 2d 6c 69 6e 65 20 6f 70 74 command-line opt
ca420 69 6f 6e 20 6f 6e 20 74 68 65 20 63 6f 6d 70 69 ion on the compi
ca430 6c 65 72 2e 20 20 54 68 69 73 20 63 6f 64 65 20 ler. This code
ca440 69 73 20 6e 6f 72 6d 61 6c 6c 79 0a 2a 2a 20 74 is normally.** t
ca450 75 72 6e 65 64 20 6f 66 66 2e 0a 2a 2f 0a 73 74 urned off..*/.st
ca460 61 74 69 63 20 69 6e 74 20 6c 6f 63 6b 54 72 61 atic int lockTra
ca470 63 65 28 69 6e 74 20 66 64 2c 20 69 6e 74 20 6f ce(int fd, int o
ca480 70 2c 20 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 p, struct flock
ca490 2a 70 29 7b 0a 20 20 63 68 61 72 20 2a 7a 4f 70 *p){. char *zOp
ca4a0 4e 61 6d 65 2c 20 2a 7a 54 79 70 65 3b 0a 20 20 Name, *zType;.
ca4b0 69 6e 74 20 73 3b 0a 20 20 69 6e 74 20 73 61 76 int s;. int sav
ca4c0 65 64 45 72 72 6e 6f 3b 0a 20 20 69 66 28 20 6f edErrno;. if( o
ca4d0 70 3d 3d 46 5f 47 45 54 4c 4b 20 29 7b 0a 20 20 p==F_GETLK ){.
ca4e0 20 20 7a 4f 70 4e 61 6d 65 20 3d 20 22 47 45 54 zOpName = "GET
ca4f0 4c 4b 22 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 LK";. }else if(
ca500 20 6f 70 3d 3d 46 5f 53 45 54 4c 4b 20 29 7b 0a op==F_SETLK ){.
ca510 20 20 20 20 7a 4f 70 4e 61 6d 65 20 3d 20 22 53 zOpName = "S
ca520 45 54 4c 4b 22 3b 0a 20 20 7d 65 6c 73 65 7b 0a ETLK";. }else{.
ca530 20 20 20 20 73 20 3d 20 66 63 6e 74 6c 28 66 64 s = fcntl(fd
ca540 2c 20 6f 70 2c 20 70 29 3b 0a 20 20 20 20 73 71 , op, p);. sq
ca550 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 lite3DebugPrintf
ca560 28 22 66 63 6e 74 6c 20 75 6e 6b 6e 6f 77 6e 20 ("fcntl unknown
ca570 25 64 20 25 64 20 25 64 5c 6e 22 2c 20 66 64 2c %d %d %d\n", fd,
ca580 20 6f 70 2c 20 73 29 3b 0a 20 20 20 20 72 65 74 op, s);. ret
ca590 75 72 6e 20 73 3b 0a 20 20 7d 0a 20 20 69 66 28 urn s;. }. if(
ca5a0 20 70 2d 3e 6c 5f 74 79 70 65 3d 3d 46 5f 52 44 p->l_type==F_RD
ca5b0 4c 43 4b 20 29 7b 0a 20 20 20 20 7a 54 79 70 65 LCK ){. zType
ca5c0 20 3d 20 22 52 44 4c 43 4b 22 3b 0a 20 20 7d 65 = "RDLCK";. }e
ca5d0 6c 73 65 20 69 66 28 20 70 2d 3e 6c 5f 74 79 70 lse if( p->l_typ
ca5e0 65 3d 3d 46 5f 57 52 4c 43 4b 20 29 7b 0a 20 20 e==F_WRLCK ){.
ca5f0 20 20 7a 54 79 70 65 20 3d 20 22 57 52 4c 43 4b zType = "WRLCK
ca600 22 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 ";. }else if( p
ca610 2d 3e 6c 5f 74 79 70 65 3d 3d 46 5f 55 4e 4c 43 ->l_type==F_UNLC
ca620 4b 20 29 7b 0a 20 20 20 20 7a 54 79 70 65 20 3d K ){. zType =
ca630 20 22 55 4e 4c 43 4b 22 3b 0a 20 20 7d 65 6c 73 "UNLCK";. }els
ca640 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 30 e{. assert( 0
ca650 20 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 );. }. assert
ca660 28 20 70 2d 3e 6c 5f 77 68 65 6e 63 65 3d 3d 53 ( p->l_whence==S
ca670 45 45 4b 5f 53 45 54 20 29 3b 0a 20 20 73 20 3d EEK_SET );. s =
ca680 20 66 63 6e 74 6c 28 66 64 2c 20 6f 70 2c 20 70 fcntl(fd, op, p
ca690 29 3b 0a 20 20 73 61 76 65 64 45 72 72 6e 6f 20 );. savedErrno
ca6a0 3d 20 65 72 72 6e 6f 3b 0a 20 20 73 71 6c 69 74 = errno;. sqlit
ca6b0 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 66 e3DebugPrintf("f
ca6c0 63 6e 74 6c 20 25 64 20 25 64 20 25 73 20 25 73 cntl %d %d %s %s
ca6d0 20 25 64 20 25 64 20 25 64 20 25 64 5c 6e 22 2c %d %d %d %d\n",
ca6e0 0a 20 20 20 20 20 74 68 72 65 61 64 69 64 2c 20 . threadid,
ca6f0 66 64 2c 20 7a 4f 70 4e 61 6d 65 2c 20 7a 54 79 fd, zOpName, zTy
ca700 70 65 2c 20 28 69 6e 74 29 70 2d 3e 6c 5f 73 74 pe, (int)p->l_st
ca710 61 72 74 2c 20 28 69 6e 74 29 70 2d 3e 6c 5f 6c art, (int)p->l_l
ca720 65 6e 2c 0a 20 20 20 20 20 28 69 6e 74 29 70 2d en,. (int)p-
ca730 3e 6c 5f 70 69 64 2c 20 73 29 3b 0a 20 20 69 66 >l_pid, s);. if
ca740 28 20 73 3d 3d 28 2d 31 29 20 26 26 20 6f 70 3d ( s==(-1) && op=
ca750 3d 46 5f 53 45 54 4c 4b 20 26 26 20 28 70 2d 3e =F_SETLK && (p->
ca760 6c 5f 74 79 70 65 3d 3d 46 5f 52 44 4c 43 4b 20 l_type==F_RDLCK
ca770 7c 7c 20 70 2d 3e 6c 5f 74 79 70 65 3d 3d 46 5f || p->l_type==F_
ca780 57 52 4c 43 4b 29 20 29 7b 0a 20 20 20 20 73 74 WRLCK) ){. st
ca790 72 75 63 74 20 66 6c 6f 63 6b 20 6c 32 3b 0a 20 ruct flock l2;.
ca7a0 20 20 20 6c 32 20 3d 20 2a 70 3b 0a 20 20 20 20 l2 = *p;.
ca7b0 66 63 6e 74 6c 28 66 64 2c 20 46 5f 47 45 54 4c fcntl(fd, F_GETL
ca7c0 4b 2c 20 26 6c 32 29 3b 0a 20 20 20 20 69 66 28 K, &l2);. if(
ca7d0 20 6c 32 2e 6c 5f 74 79 70 65 3d 3d 46 5f 52 44 l2.l_type==F_RD
ca7e0 4c 43 4b 20 29 7b 0a 20 20 20 20 20 20 7a 54 79 LCK ){. zTy
ca7f0 70 65 20 3d 20 22 52 44 4c 43 4b 22 3b 0a 20 20 pe = "RDLCK";.
ca800 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 32 2e 6c }else if( l2.l
ca810 5f 74 79 70 65 3d 3d 46 5f 57 52 4c 43 4b 20 29 _type==F_WRLCK )
ca820 7b 0a 20 20 20 20 20 20 7a 54 79 70 65 20 3d 20 {. zType =
ca830 22 57 52 4c 43 4b 22 3b 0a 20 20 20 20 7d 65 6c "WRLCK";. }el
ca840 73 65 20 69 66 28 20 6c 32 2e 6c 5f 74 79 70 65 se if( l2.l_type
ca850 3d 3d 46 5f 55 4e 4c 43 4b 20 29 7b 0a 20 20 20 ==F_UNLCK ){.
ca860 20 20 20 7a 54 79 70 65 20 3d 20 22 55 4e 4c 43 zType = "UNLC
ca870 4b 22 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 K";. }else{.
ca880 20 20 20 20 20 61 73 73 65 72 74 28 20 30 20 29 assert( 0 )
ca890 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 ;. }. sqli
ca8a0 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 te3DebugPrintf("
ca8b0 66 63 6e 74 6c 2d 66 61 69 6c 75 72 65 2d 72 65 fcntl-failure-re
ca8c0 61 73 6f 6e 3a 20 25 73 20 25 64 20 25 64 20 25 ason: %s %d %d %
ca8d0 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 7a 54 79 d\n",. zTy
ca8e0 70 65 2c 20 28 69 6e 74 29 6c 32 2e 6c 5f 73 74 pe, (int)l2.l_st
ca8f0 61 72 74 2c 20 28 69 6e 74 29 6c 32 2e 6c 5f 6c art, (int)l2.l_l
ca900 65 6e 2c 20 28 69 6e 74 29 6c 32 2e 6c 5f 70 69 en, (int)l2.l_pi
ca910 64 29 3b 0a 20 20 7d 0a 20 20 65 72 72 6e 6f 20 d);. }. errno
ca920 3d 20 73 61 76 65 64 45 72 72 6e 6f 3b 0a 20 20 = savedErrno;.
ca930 72 65 74 75 72 6e 20 73 3b 0a 7d 0a 23 64 65 66 return s;.}.#def
ca940 69 6e 65 20 66 63 6e 74 6c 20 6c 6f 63 6b 54 72 ine fcntl lockTr
ca950 61 63 65 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 ace.#endif /* SQ
ca960 4c 49 54 45 5f 4c 4f 43 4b 5f 54 52 41 43 45 20 LITE_LOCK_TRACE
ca970 2a 2f 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 */..../*.** This
ca980 20 72 6f 75 74 69 6e 65 20 74 72 61 6e 73 6c 61 routine transla
ca990 74 65 73 20 61 20 73 74 61 6e 64 61 72 64 20 50 tes a standard P
ca9a0 4f 53 49 58 20 65 72 72 6e 6f 20 63 6f 64 65 20 OSIX errno code
ca9b0 69 6e 74 6f 20 73 6f 6d 65 74 68 69 6e 67 0a 2a into something.*
ca9c0 2a 20 75 73 65 66 75 6c 20 74 6f 20 74 68 65 20 * useful to the
ca9d0 63 6c 69 65 6e 74 73 20 6f 66 20 74 68 65 20 73 clients of the s
ca9e0 71 6c 69 74 65 33 20 66 75 6e 63 74 69 6f 6e 73 qlite3 functions
ca9f0 2e 20 20 53 70 65 63 69 66 69 63 61 6c 6c 79 2c . Specifically,
caa00 20 69 74 20 69 73 0a 2a 2a 20 69 6e 74 65 6e 64 it is.** intend
caa10 65 64 20 74 6f 20 74 72 61 6e 73 6c 61 74 65 20 ed to translate
caa20 61 20 76 61 72 69 65 74 79 20 6f 66 20 22 74 72 a variety of "tr
caa30 79 20 61 67 61 69 6e 22 20 65 72 72 6f 72 73 20 y again" errors
caa40 69 6e 74 6f 20 53 51 4c 49 54 45 5f 42 55 53 59 into SQLITE_BUSY
caa50 0a 2a 2a 20 61 6e 64 20 61 20 76 61 72 69 65 74 .** and a variet
caa60 79 20 6f 66 20 22 70 6c 65 61 73 65 20 63 6c 6f y of "please clo
caa70 73 65 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 se the file desc
caa80 72 69 70 74 6f 72 20 4e 4f 57 22 20 65 72 72 6f riptor NOW" erro
caa90 72 73 20 69 6e 74 6f 20 0a 2a 2a 20 53 51 4c 49 rs into .** SQLI
caaa0 54 45 5f 49 4f 45 52 52 0a 2a 2a 20 0a 2a 2a 20 TE_IOERR.** .**
caab0 45 72 72 6f 72 73 20 64 75 72 69 6e 67 20 69 6e Errors during in
caac0 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 6f 66 20 itialization of
caad0 6c 6f 63 6b 73 2c 20 6f 72 20 66 69 6c 65 20 73 locks, or file s
caae0 79 73 74 65 6d 20 73 75 70 70 6f 72 74 20 66 6f ystem support fo
caaf0 72 20 6c 6f 63 6b 73 2c 0a 2a 2a 20 73 68 6f 75 r locks,.** shou
cab00 6c 64 20 68 61 6e 64 6c 65 20 45 4e 4f 4c 43 4b ld handle ENOLCK
cab10 2c 20 45 4e 4f 54 53 55 50 2c 20 45 4f 50 4e 4f , ENOTSUP, EOPNO
cab20 54 53 55 50 50 20 73 65 70 61 72 61 74 65 6c 79 TSUPP separately
cab30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
cab40 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 sqliteErrorFromP
cab50 6f 73 69 78 45 72 72 6f 72 28 69 6e 74 20 70 6f osixError(int po
cab60 73 69 78 45 72 72 6f 72 2c 20 69 6e 74 20 73 71 sixError, int sq
cab70 6c 69 74 65 49 4f 45 72 72 29 20 7b 0a 20 20 73 liteIOErr) {. s
cab80 77 69 74 63 68 20 28 70 6f 73 69 78 45 72 72 6f witch (posixErro
cab90 72 29 20 7b 0a 20 20 63 61 73 65 20 30 3a 20 0a r) {. case 0: .
caba0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
cabb0 45 5f 4f 4b 3b 0a 20 20 20 20 0a 20 20 63 61 73 E_OK;. . cas
cabc0 65 20 45 41 47 41 49 4e 3a 0a 20 20 63 61 73 65 e EAGAIN:. case
cabd0 20 45 54 49 4d 45 44 4f 55 54 3a 0a 20 20 63 61 ETIMEDOUT:. ca
cabe0 73 65 20 45 42 55 53 59 3a 0a 20 20 63 61 73 65 se EBUSY:. case
cabf0 20 45 49 4e 54 52 3a 0a 20 20 63 61 73 65 20 45 EINTR:. case E
cac00 4e 4f 4c 43 4b 3a 20 20 0a 20 20 20 20 2f 2a 20 NOLCK: . /*
cac10 72 61 6e 64 6f 6d 20 4e 46 53 20 72 65 74 72 79 random NFS retry
cac20 20 65 72 72 6f 72 2c 20 75 6e 6c 65 73 73 20 64 error, unless d
cac30 75 72 69 6e 67 20 66 69 6c 65 20 73 79 73 74 65 uring file syste
cac40 6d 20 73 75 70 70 6f 72 74 20 0a 20 20 20 20 20 m support .
cac50 2a 20 69 6e 74 72 6f 73 70 65 63 74 69 6f 6e 2c * introspection,
cac60 20 69 6e 20 77 68 69 63 68 20 69 74 20 61 63 74 in which it act
cac70 75 61 6c 6c 79 20 6d 65 61 6e 73 20 77 68 61 74 ually means what
cac80 20 69 74 20 73 61 79 73 20 2a 2f 0a 20 20 20 20 it says */.
cac90 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 return SQLITE_BU
caca0 53 59 3b 0a 20 20 20 20 0a 20 20 63 61 73 65 20 SY;. . case
cacb0 45 41 43 43 45 53 3a 20 0a 20 20 20 20 2f 2a 20 EACCES: . /*
cacc0 45 41 43 43 45 53 20 69 73 20 6c 69 6b 65 20 45 EACCES is like E
cacd0 41 47 41 49 4e 20 64 75 72 69 6e 67 20 6c 6f 63 AGAIN during loc
cace0 6b 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e 73 2c king operations,
cacf0 20 62 75 74 20 6e 6f 74 20 61 6e 79 20 6f 74 68 but not any oth
cad00 65 72 20 74 69 6d 65 2a 2f 0a 20 20 20 20 69 66 er time*/. if
cad10 28 20 28 73 71 6c 69 74 65 49 4f 45 72 72 20 3d ( (sqliteIOErr =
cad20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c = SQLITE_IOERR_L
cad30 4f 43 4b 29 20 7c 7c 20 0a 09 28 73 71 6c 69 74 OCK) || ..(sqlit
cad40 65 49 4f 45 72 72 20 3d 3d 20 53 51 4c 49 54 45 eIOErr == SQLITE
cad50 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 20 7c _IOERR_UNLOCK) |
cad60 7c 20 0a 09 28 73 71 6c 69 74 65 49 4f 45 72 72 | ..(sqliteIOErr
cad70 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 == SQLITE_IOERR
cad80 5f 52 44 4c 4f 43 4b 29 20 7c 7c 0a 09 28 73 71 _RDLOCK) ||..(sq
cad90 6c 69 74 65 49 4f 45 72 72 20 3d 3d 20 53 51 4c liteIOErr == SQL
cada0 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 ITE_IOERR_CHECKR
cadb0 45 53 45 52 56 45 44 4c 4f 43 4b 29 20 29 7b 0a ESERVEDLOCK) ){.
cadc0 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
cadd0 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d 0a ITE_BUSY;. }.
cade0 20 20 20 20 2f 2a 20 65 6c 73 65 20 66 61 6c 6c /* else fall
cadf0 20 74 68 72 6f 75 67 68 20 2a 2f 0a 20 20 63 61 through */. ca
cae00 73 65 20 45 50 45 52 4d 3a 20 0a 20 20 20 20 72 se EPERM: . r
cae10 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 50 45 52 eturn SQLITE_PER
cae20 4d 3b 0a 20 20 20 20 0a 20 20 63 61 73 65 20 45 M;. . case E
cae30 44 45 41 44 4c 4b 3a 0a 20 20 20 20 72 65 74 75 DEADLK:. retu
cae40 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f rn SQLITE_IOERR_
cae50 42 4c 4f 43 4b 45 44 3b 0a 20 20 20 20 0a 23 69 BLOCKED;. .#i
cae60 66 20 45 4f 50 4e 4f 54 53 55 50 50 21 3d 45 4e f EOPNOTSUPP!=EN
cae70 4f 54 53 55 50 0a 20 20 63 61 73 65 20 45 4f 50 OTSUP. case EOP
cae80 4e 4f 54 53 55 50 50 3a 20 0a 20 20 20 20 2f 2a NOTSUPP: . /*
cae90 20 73 6f 6d 65 74 68 69 6e 67 20 77 65 6e 74 20 something went
caea0 74 65 72 72 69 62 6c 79 20 61 77 72 79 2c 20 75 terribly awry, u
caeb0 6e 6c 65 73 73 20 64 75 72 69 6e 67 20 66 69 6c nless during fil
caec0 65 20 73 79 73 74 65 6d 20 73 75 70 70 6f 72 74 e system support
caed0 20 0a 20 20 20 20 20 2a 20 69 6e 74 72 6f 73 70 . * introsp
caee0 65 63 74 69 6f 6e 2c 20 69 6e 20 77 68 69 63 68 ection, in which
caef0 20 69 74 20 61 63 74 75 61 6c 6c 79 20 6d 65 61 it actually mea
caf00 6e 73 20 77 68 61 74 20 69 74 20 73 61 79 73 20 ns what it says
caf10 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 */.#endif.#ifdef
caf20 20 45 4e 4f 54 53 55 50 0a 20 20 63 61 73 65 20 ENOTSUP. case
caf30 45 4e 4f 54 53 55 50 3a 20 0a 20 20 20 20 2f 2a ENOTSUP: . /*
caf40 20 69 6e 76 61 6c 69 64 20 66 64 2c 20 75 6e 6c invalid fd, unl
caf50 65 73 73 20 64 75 72 69 6e 67 20 66 69 6c 65 20 ess during file
caf60 73 79 73 74 65 6d 20 73 75 70 70 6f 72 74 20 69 system support i
caf70 6e 74 72 6f 73 70 65 63 74 69 6f 6e 2c 20 69 6e ntrospection, in
caf80 20 77 68 69 63 68 20 0a 20 20 20 20 20 2a 20 69 which . * i
caf90 74 20 61 63 74 75 61 6c 6c 79 20 6d 65 61 6e 73 t actually means
cafa0 20 77 68 61 74 20 69 74 20 73 61 79 73 20 2a 2f what it says */
cafb0 0a 23 65 6e 64 69 66 0a 20 20 63 61 73 65 20 45 .#endif. case E
cafc0 49 4f 3a 0a 20 20 63 61 73 65 20 45 42 41 44 46 IO:. case EBADF
cafd0 3a 0a 20 20 63 61 73 65 20 45 49 4e 56 41 4c 3a :. case EINVAL:
cafe0 0a 20 20 63 61 73 65 20 45 4e 4f 54 43 4f 4e 4e . case ENOTCONN
caff0 3a 0a 20 20 63 61 73 65 20 45 4e 4f 44 45 56 3a :. case ENODEV:
cb000 0a 20 20 63 61 73 65 20 45 4e 58 49 4f 3a 0a 20 . case ENXIO:.
cb010 20 63 61 73 65 20 45 4e 4f 45 4e 54 3a 0a 20 20 case ENOENT:.
cb020 63 61 73 65 20 45 53 54 41 4c 45 3a 0a 20 20 63 case ESTALE:. c
cb030 61 73 65 20 45 4e 4f 53 59 53 3a 0a 20 20 20 20 ase ENOSYS:.
cb040 2f 2a 20 74 68 65 73 65 20 73 68 6f 75 6c 64 20 /* these should
cb050 66 6f 72 63 65 20 74 68 65 20 63 6c 69 65 6e 74 force the client
cb060 20 74 6f 20 63 6c 6f 73 65 20 74 68 65 20 66 69 to close the fi
cb070 6c 65 20 61 6e 64 20 72 65 63 6f 6e 6e 65 63 74 le and reconnect
cb080 20 2a 2f 0a 20 20 20 20 0a 20 20 64 65 66 61 75 */. . defau
cb090 6c 74 3a 20 0a 20 20 20 20 72 65 74 75 72 6e 20 lt: . return
cb0a0 73 71 6c 69 74 65 49 4f 45 72 72 3b 0a 20 20 7d sqliteIOErr;. }
cb0b0 0a 7d 0a 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a .}..../*********
cb0c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cb0d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cb0e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cb0f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cb100 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *****.**********
cb110 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 55 ******** Begin U
cb120 6e 69 71 75 65 20 46 69 6c 65 20 49 44 20 55 74 nique File ID Ut
cb130 69 6c 69 74 79 20 55 73 65 64 20 42 79 20 56 78 ility Used By Vx
cb140 57 6f 72 6b 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a Works **********
cb150 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 4f 6e 20 6d *****.**.** On m
cb160 6f 73 74 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 ost versions of
cb170 75 6e 69 78 2c 20 77 65 20 63 61 6e 20 67 65 74 unix, we can get
cb180 20 61 20 75 6e 69 71 75 65 20 49 44 20 66 6f 72 a unique ID for
cb190 20 61 20 66 69 6c 65 20 62 79 20 63 6f 6e 63 61 a file by conca
cb1a0 74 65 6e 61 74 69 6e 67 0a 2a 2a 20 74 68 65 20 tenating.** the
cb1b0 64 65 76 69 63 65 20 6e 75 6d 62 65 72 20 61 6e device number an
cb1c0 64 20 74 68 65 20 69 6e 6f 64 65 20 6e 75 6d 62 d the inode numb
cb1d0 65 72 2e 20 20 42 75 74 20 74 68 69 73 20 64 6f er. But this do
cb1e0 65 73 20 6e 6f 74 20 77 6f 72 6b 20 6f 6e 20 56 es not work on V
cb1f0 78 57 6f 72 6b 73 2e 0a 2a 2a 20 4f 6e 20 56 78 xWorks..** On Vx
cb200 57 6f 72 6b 73 2c 20 61 20 75 6e 69 71 75 65 20 Works, a unique
cb210 66 69 6c 65 20 69 64 20 6d 75 73 74 20 62 65 20 file id must be
cb220 62 61 73 65 64 20 6f 6e 20 74 68 65 20 63 61 6e based on the can
cb230 6f 6e 69 63 61 6c 20 66 69 6c 65 6e 61 6d 65 2e onical filename.
cb240 0a 2a 2a 0a 2a 2a 20 41 20 70 6f 69 6e 74 65 72 .**.** A pointer
cb250 20 74 6f 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 to an instance
cb260 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 of the following
cb270 20 73 74 72 75 63 74 75 72 65 20 63 61 6e 20 62 structure can b
cb280 65 20 75 73 65 64 20 61 73 20 61 0a 2a 2a 20 75 e used as a.** u
cb290 6e 69 71 75 65 20 66 69 6c 65 20 49 44 20 69 6e nique file ID in
cb2a0 20 56 78 57 6f 72 6b 73 2e 20 20 45 61 63 68 20 VxWorks. Each
cb2b0 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 instance of this
cb2c0 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 structure conta
cb2d0 69 6e 73 0a 2a 2a 20 61 20 63 6f 70 79 20 6f 66 ins.** a copy of
cb2e0 20 74 68 65 20 63 61 6e 6f 6e 69 63 61 6c 20 66 the canonical f
cb2f0 69 6c 65 6e 61 6d 65 2e 20 20 54 68 65 72 65 20 ilename. There
cb300 69 73 20 61 6c 73 6f 20 61 20 72 65 66 65 72 65 is also a refere
cb310 6e 63 65 20 63 6f 75 6e 74 2e 20 20 0a 2a 2a 20 nce count. .**
cb320 54 68 65 20 73 74 72 75 63 74 75 72 65 20 69 73 The structure is
cb330 20 72 65 63 6c 61 69 6d 65 64 20 77 68 65 6e 20 reclaimed when
cb340 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 6f the number of po
cb350 69 6e 74 65 72 73 20 74 6f 20 69 74 20 64 72 6f inters to it dro
cb360 70 73 20 74 6f 0a 2a 2a 20 7a 65 72 6f 2e 0a 2a ps to.** zero..*
cb370 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 6e *.** There are n
cb380 65 76 65 72 20 76 65 72 79 20 6d 61 6e 79 20 66 ever very many f
cb390 69 6c 65 73 20 6f 70 65 6e 20 61 74 20 6f 6e 65 iles open at one
cb3a0 20 74 69 6d 65 20 61 6e 64 20 6c 6f 6f 6b 75 70 time and lookup
cb3b0 73 20 61 72 65 20 6e 6f 74 0a 2a 2a 20 61 20 70 s are not.** a p
cb3c0 65 72 66 6f 72 6d 61 6e 63 65 2d 63 72 69 74 69 erformance-criti
cb3d0 63 61 6c 20 70 61 74 68 2c 20 73 6f 20 69 74 20 cal path, so it
cb3e0 69 73 20 73 75 66 66 69 63 69 65 6e 74 20 74 6f is sufficient to
cb3f0 20 70 75 74 20 74 68 65 73 65 0a 2a 2a 20 73 74 put these.** st
cb400 72 75 63 74 75 72 65 73 20 6f 6e 20 61 20 6c 69 ructures on a li
cb410 6e 6b 65 64 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 nked list..*/.st
cb420 72 75 63 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 ruct vxworksFile
cb430 49 64 20 7b 0a 20 20 73 74 72 75 63 74 20 76 78 Id {. struct vx
cb440 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a 70 4e 65 worksFileId *pNe
cb450 78 74 3b 20 20 2f 2a 20 4e 65 78 74 20 69 6e 20 xt; /* Next in
cb460 61 20 6c 69 73 74 20 6f 66 20 74 68 65 6d 20 61 a list of them a
cb470 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66 ll */. int nRef
cb480 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
cb490 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
cb4a0 6f 66 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f of references to
cb4b0 20 74 68 69 73 20 6f 6e 65 20 2a 2f 0a 20 20 69 this one */. i
cb4c0 6e 74 20 6e 4e 61 6d 65 3b 20 20 20 20 20 20 20 nt nName;
cb4d0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
cb4e0 4c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 7a 43 Length of the zC
cb4f0 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 5b 5d 20 73 anonicalName[] s
cb500 74 72 69 6e 67 20 2a 2f 0a 20 20 63 68 61 72 20 tring */. char
cb510 2a 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 3b *zCanonicalName;
cb520 20 20 20 20 20 20 20 20 20 2f 2a 20 43 61 6e 6f /* Cano
cb530 6e 69 63 61 6c 20 66 69 6c 65 6e 61 6d 65 20 2a nical filename *
cb540 2f 0a 7d 3b 0a 0a 23 69 66 20 4f 53 5f 56 58 57 /.};..#if OS_VXW
cb550 4f 52 4b 53 0a 2f 2a 20 0a 2a 2a 20 41 6c 6c 20 ORKS./* .** All
cb560 75 6e 69 71 75 65 20 66 69 6c 65 6e 61 6d 65 73 unique filenames
cb570 20 61 72 65 20 68 65 6c 64 20 6f 6e 20 61 20 6c are held on a l
cb580 69 6e 6b 65 64 20 6c 69 73 74 20 68 65 61 64 65 inked list heade
cb590 64 20 62 79 20 74 68 69 73 0a 2a 2a 20 76 61 72 d by this.** var
cb5a0 69 61 62 6c 65 3a 0a 2a 2f 0a 73 74 61 74 69 63 iable:.*/.static
cb5b0 20 73 74 72 75 63 74 20 76 78 77 6f 72 6b 73 46 struct vxworksF
cb5c0 69 6c 65 49 64 20 2a 76 78 77 6f 72 6b 73 46 69 ileId *vxworksFi
cb5d0 6c 65 4c 69 73 74 20 3d 20 30 3b 0a 0a 2f 2a 0a leList = 0;../*.
cb5e0 2a 2a 20 53 69 6d 70 6c 69 66 79 20 61 20 66 69 ** Simplify a fi
cb5f0 6c 65 6e 61 6d 65 20 69 6e 74 6f 20 69 74 73 20 lename into its
cb600 63 61 6e 6f 6e 69 63 61 6c 20 66 6f 72 6d 0a 2a canonical form.*
cb610 2a 20 62 79 20 6d 61 6b 69 6e 67 20 74 68 65 20 * by making the
cb620 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 6e 67 65 following change
cb630 73 3a 0a 2a 2a 0a 2a 2a 20 20 2a 20 72 65 6d 6f s:.**.** * remo
cb640 76 69 6e 67 20 61 6e 79 20 74 72 61 69 6c 69 6e ving any trailin
cb650 67 20 61 6e 64 20 64 75 70 6c 69 63 61 74 65 20 g and duplicate
cb660 2f 0a 2a 2a 20 20 2a 20 63 6f 6e 76 65 72 74 20 /.** * convert
cb670 2f 2e 2f 20 69 6e 74 6f 20 6a 75 73 74 20 2f 0a /./ into just /.
cb680 2a 2a 20 20 2a 20 63 6f 6e 76 65 72 74 20 2f 41 ** * convert /A
cb690 2f 2e 2e 2f 20 77 68 65 72 65 20 41 20 69 73 20 /../ where A is
cb6a0 61 6e 79 20 73 69 6d 70 6c 65 20 6e 61 6d 65 20 any simple name
cb6b0 69 6e 74 6f 20 6a 75 73 74 20 2f 0a 2a 2a 0a 2a into just /.**.*
cb6c0 2a 20 43 68 61 6e 67 65 73 20 61 72 65 20 6d 61 * Changes are ma
cb6d0 64 65 20 69 6e 2d 70 6c 61 63 65 2e 20 20 52 65 de in-place. Re
cb6e0 74 75 72 6e 20 74 68 65 20 6e 65 77 20 6e 61 6d turn the new nam
cb6f0 65 20 6c 65 6e 67 74 68 2e 0a 2a 2a 0a 2a 2a 20 e length..**.**
cb700 54 68 65 20 6f 72 69 67 69 6e 61 6c 20 66 69 6c The original fil
cb710 65 6e 61 6d 65 20 69 73 20 69 6e 20 7a 5b 30 2e ename is in z[0.
cb720 2e 6e 2d 31 5d 2e 20 20 52 65 74 75 72 6e 20 74 .n-1]. Return t
cb730 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 he number of.**
cb740 63 68 61 72 61 63 74 65 72 73 20 69 6e 20 74 68 characters in th
cb750 65 20 73 69 6d 70 6c 69 66 69 65 64 20 6e 61 6d e simplified nam
cb760 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
cb770 20 76 78 77 6f 72 6b 73 53 69 6d 70 6c 69 66 79 vxworksSimplify
cb780 4e 61 6d 65 28 63 68 61 72 20 2a 7a 2c 20 69 6e Name(char *z, in
cb790 74 20 6e 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a t n){. int i, j
cb7a0 3b 0a 20 20 77 68 69 6c 65 28 20 6e 3e 31 20 26 ;. while( n>1 &
cb7b0 26 20 7a 5b 6e 2d 31 5d 3d 3d 27 2f 27 20 29 7b & z[n-1]=='/' ){
cb7c0 20 6e 2d 2d 3b 20 7d 0a 20 20 66 6f 72 28 69 3d n--; }. for(i=
cb7d0 6a 3d 30 3b 20 69 3c 6e 3b 20 69 2b 2b 29 7b 0a j=0; i<n; i++){.
cb7e0 20 20 20 20 69 66 28 20 7a 5b 69 5d 3d 3d 27 2f if( z[i]=='/
cb7f0 27 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 7a ' ){. if( z
cb800 5b 69 2b 31 5d 3d 3d 27 2f 27 20 29 20 63 6f 6e [i+1]=='/' ) con
cb810 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 69 66 28 tinue;. if(
cb820 20 7a 5b 69 2b 31 5d 3d 3d 27 2e 27 20 26 26 20 z[i+1]=='.' &&
cb830 69 2b 32 3c 6e 20 26 26 20 7a 5b 69 2b 32 5d 3d i+2<n && z[i+2]=
cb840 3d 27 2f 27 20 29 7b 0a 20 20 20 20 20 20 20 20 ='/' ){.
cb850 69 20 2b 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 i += 1;.
cb860 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 continue;.
cb870 7d 0a 20 20 20 20 20 20 69 66 28 20 7a 5b 69 2b }. if( z[i+
cb880 31 5d 3d 3d 27 2e 27 20 26 26 20 69 2b 33 3c 6e 1]=='.' && i+3<n
cb890 20 26 26 20 7a 5b 69 2b 32 5d 3d 3d 27 2e 27 20 && z[i+2]=='.'
cb8a0 26 26 20 7a 5b 69 2b 33 5d 3d 3d 27 2f 27 20 29 && z[i+3]=='/' )
cb8b0 7b 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 {. while(
cb8c0 20 6a 3e 30 20 26 26 20 7a 5b 6a 2d 31 5d 21 3d j>0 && z[j-1]!=
cb8d0 27 2f 27 20 29 7b 20 6a 2d 2d 3b 20 7d 0a 20 20 '/' ){ j--; }.
cb8e0 20 20 20 20 20 20 69 66 28 20 6a 3e 30 20 29 7b if( j>0 ){
cb8f0 20 6a 2d 2d 3b 20 7d 0a 20 20 20 20 20 20 20 20 j--; }.
cb900 69 20 2b 3d 20 32 3b 0a 20 20 20 20 20 20 20 20 i += 2;.
cb910 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 continue;.
cb920 7d 0a 20 20 20 20 7d 0a 20 20 20 20 7a 5b 6a 2b }. }. z[j+
cb930 2b 5d 20 3d 20 7a 5b 69 5d 3b 0a 20 20 7d 0a 20 +] = z[i];. }.
cb940 20 7a 5b 6a 5d 20 3d 20 30 3b 0a 20 20 72 65 74 z[j] = 0;. ret
cb950 75 72 6e 20 6a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 urn j;.}../*.**
cb960 46 69 6e 64 20 61 20 75 6e 69 71 75 65 20 66 69 Find a unique fi
cb970 6c 65 20 49 44 20 66 6f 72 20 74 68 65 20 67 69 le ID for the gi
cb980 76 65 6e 20 61 62 73 6f 6c 75 74 65 20 70 61 74 ven absolute pat
cb990 68 6e 61 6d 65 2e 20 20 52 65 74 75 72 6e 0a 2a hname. Return.*
cb9a0 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 * a pointer to t
cb9b0 68 65 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 he vxworksFileId
cb9c0 20 6f 62 6a 65 63 74 2e 20 20 54 68 69 73 20 70 object. This p
cb9d0 6f 69 6e 74 65 72 20 69 73 20 74 68 65 20 75 6e ointer is the un
cb9e0 69 71 75 65 0a 2a 2a 20 66 69 6c 65 20 49 44 2e ique.** file ID.
cb9f0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 52 65 66 20 .**.** The nRef
cba00 66 69 65 6c 64 20 6f 66 20 74 68 65 20 76 78 77 field of the vxw
cba10 6f 72 6b 73 46 69 6c 65 49 64 20 6f 62 6a 65 63 orksFileId objec
cba20 74 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 t is incremented
cba30 20 62 65 66 6f 72 65 0a 2a 2a 20 74 68 65 20 6f before.** the o
cba40 62 6a 65 63 74 20 69 73 20 72 65 74 75 72 6e 65 bject is returne
cba50 64 2e 20 20 41 20 6e 65 77 20 76 78 77 6f 72 6b d. A new vxwork
cba60 73 46 69 6c 65 49 64 20 6f 62 6a 65 63 74 20 69 sFileId object i
cba70 73 20 63 72 65 61 74 65 64 0a 2a 2a 20 61 6e 64 s created.** and
cba80 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 67 6c added to the gl
cba90 6f 62 61 6c 20 6c 69 73 74 20 69 66 20 6e 65 63 obal list if nec
cbaa0 65 73 73 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 essary..**.** If
cbab0 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 a memory alloca
cbac0 74 69 6f 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 tion error occur
cbad0 73 2c 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a s, return NULL..
cbae0 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75 63 74 */.static struct
cbaf0 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a vxworksFileId *
cbb00 76 78 77 6f 72 6b 73 46 69 6e 64 46 69 6c 65 49 vxworksFindFileI
cbb10 64 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 41 d(const char *zA
cbb20 62 73 6f 6c 75 74 65 4e 61 6d 65 29 7b 0a 20 20 bsoluteName){.
cbb30 73 74 72 75 63 74 20 76 78 77 6f 72 6b 73 46 69 struct vxworksFi
cbb40 6c 65 49 64 20 2a 70 4e 65 77 3b 20 20 20 20 20 leId *pNew;
cbb50 20 20 20 20 2f 2a 20 73 65 61 72 63 68 20 6b 65 /* search ke
cbb60 79 20 61 6e 64 20 6e 65 77 20 66 69 6c 65 20 49 y and new file I
cbb70 44 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 76 78 D */. struct vx
cbb80 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a 70 43 61 worksFileId *pCa
cbb90 6e 64 69 64 61 74 65 3b 20 20 20 2f 2a 20 46 6f ndidate; /* Fo
cbba0 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 65 r looping over e
cbbb0 78 69 73 74 69 6e 67 20 66 69 6c 65 20 49 44 73 xisting file IDs
cbbc0 20 2a 2f 0a 20 20 69 6e 74 20 6e 3b 20 20 20 20 */. int n;
cbbd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
cbbe0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e /* Len
cbbf0 67 74 68 20 6f 66 20 7a 41 62 73 6f 6c 75 74 65 gth of zAbsolute
cbc00 4e 61 6d 65 20 73 74 72 69 6e 67 20 2a 2f 0a 0a Name string */..
cbc10 20 20 61 73 73 65 72 74 28 20 7a 41 62 73 6f 6c assert( zAbsol
cbc20 75 74 65 4e 61 6d 65 5b 30 5d 3d 3d 27 2f 27 20 uteName[0]=='/'
cbc30 29 3b 0a 20 20 6e 20 3d 20 28 69 6e 74 29 73 74 );. n = (int)st
cbc40 72 6c 65 6e 28 7a 41 62 73 6f 6c 75 74 65 4e 61 rlen(zAbsoluteNa
cbc50 6d 65 29 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 me);. pNew = sq
cbc60 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 73 69 lite3_malloc( si
cbc70 7a 65 6f 66 28 2a 70 4e 65 77 29 20 2b 20 28 6e zeof(*pNew) + (n
cbc80 2b 31 29 20 29 3b 0a 20 20 69 66 28 20 70 4e 65 +1) );. if( pNe
cbc90 77 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b w==0 ) return 0;
cbca0 0a 20 20 70 4e 65 77 2d 3e 7a 43 61 6e 6f 6e 69 . pNew->zCanoni
cbcb0 63 61 6c 4e 61 6d 65 20 3d 20 28 63 68 61 72 2a calName = (char*
cbcc0 29 26 70 4e 65 77 5b 31 5d 3b 0a 20 20 6d 65 6d )&pNew[1];. mem
cbcd0 63 70 79 28 70 4e 65 77 2d 3e 7a 43 61 6e 6f 6e cpy(pNew->zCanon
cbce0 69 63 61 6c 4e 61 6d 65 2c 20 7a 41 62 73 6f 6c icalName, zAbsol
cbcf0 75 74 65 4e 61 6d 65 2c 20 6e 2b 31 29 3b 0a 20 uteName, n+1);.
cbd00 20 6e 20 3d 20 76 78 77 6f 72 6b 73 53 69 6d 70 n = vxworksSimp
cbd10 6c 69 66 79 4e 61 6d 65 28 70 4e 65 77 2d 3e 7a lifyName(pNew->z
cbd20 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 2c 20 6e CanonicalName, n
cbd30 29 3b 0a 0a 20 20 2f 2a 20 53 65 61 72 63 68 20 );.. /* Search
cbd40 66 6f 72 20 61 6e 20 65 78 69 73 74 69 6e 67 20 for an existing
cbd50 65 6e 74 72 79 20 74 68 61 74 20 6d 61 74 63 68 entry that match
cbd60 69 6e 67 20 74 68 65 20 63 61 6e 6f 6e 69 63 61 ing the canonica
cbd70 6c 20 6e 61 6d 65 2e 0a 20 20 2a 2a 20 49 66 20 l name.. ** If
cbd80 66 6f 75 6e 64 2c 20 69 6e 63 72 65 6d 65 6e 74 found, increment
cbd90 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 63 the reference c
cbda0 6f 75 6e 74 20 61 6e 64 20 72 65 74 75 72 6e 20 ount and return
cbdb0 61 20 70 6f 69 6e 74 65 72 20 74 6f 0a 20 20 2a a pointer to. *
cbdc0 2a 20 74 68 65 20 65 78 69 73 74 69 6e 67 20 66 * the existing f
cbdd0 69 6c 65 20 49 44 2e 0a 20 20 2a 2f 0a 20 20 75 ile ID.. */. u
cbde0 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b nixEnterMutex();
cbdf0 0a 20 20 66 6f 72 28 70 43 61 6e 64 69 64 61 74 . for(pCandidat
cbe00 65 3d 76 78 77 6f 72 6b 73 46 69 6c 65 4c 69 73 e=vxworksFileLis
cbe10 74 3b 20 70 43 61 6e 64 69 64 61 74 65 3b 20 70 t; pCandidate; p
cbe20 43 61 6e 64 69 64 61 74 65 3d 70 43 61 6e 64 69 Candidate=pCandi
cbe30 64 61 74 65 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 date->pNext){.
cbe40 20 20 69 66 28 20 70 43 61 6e 64 69 64 61 74 65 if( pCandidate
cbe50 2d 3e 6e 4e 61 6d 65 3d 3d 6e 20 0a 20 20 20 20 ->nName==n .
cbe60 20 26 26 20 6d 65 6d 63 6d 70 28 70 43 61 6e 64 && memcmp(pCand
cbe70 69 64 61 74 65 2d 3e 7a 43 61 6e 6f 6e 69 63 61 idate->zCanonica
cbe80 6c 4e 61 6d 65 2c 20 70 4e 65 77 2d 3e 7a 43 61 lName, pNew->zCa
cbe90 6e 6f 6e 69 63 61 6c 4e 61 6d 65 2c 20 6e 29 3d nonicalName, n)=
cbea0 3d 30 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 =0. ){.
cbeb0 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4e sqlite3_free(pN
cbec0 65 77 29 3b 0a 20 20 20 20 20 20 20 70 43 61 6e ew);. pCan
cbed0 64 69 64 61 74 65 2d 3e 6e 52 65 66 2b 2b 3b 0a didate->nRef++;.
cbee0 20 20 20 20 20 20 20 75 6e 69 78 4c 65 61 76 65 unixLeave
cbef0 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 20 20 20 Mutex();.
cbf00 72 65 74 75 72 6e 20 70 43 61 6e 64 69 64 61 74 return pCandidat
cbf10 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 e;. }. }..
cbf20 2f 2a 20 4e 6f 20 6d 61 74 63 68 20 77 61 73 20 /* No match was
cbf30 66 6f 75 6e 64 2e 20 20 57 65 20 77 69 6c 6c 20 found. We will
cbf40 6d 61 6b 65 20 61 20 6e 65 77 20 66 69 6c 65 20 make a new file
cbf50 49 44 20 2a 2f 0a 20 20 70 4e 65 77 2d 3e 6e 52 ID */. pNew->nR
cbf60 65 66 20 3d 20 31 3b 0a 20 20 70 4e 65 77 2d 3e ef = 1;. pNew->
cbf70 6e 4e 61 6d 65 20 3d 20 6e 3b 0a 20 20 70 4e 65 nName = n;. pNe
cbf80 77 2d 3e 70 4e 65 78 74 20 3d 20 76 78 77 6f 72 w->pNext = vxwor
cbf90 6b 73 46 69 6c 65 4c 69 73 74 3b 0a 20 20 76 78 ksFileList;. vx
cbfa0 77 6f 72 6b 73 46 69 6c 65 4c 69 73 74 20 3d 20 worksFileList =
cbfb0 70 4e 65 77 3b 0a 20 20 75 6e 69 78 4c 65 61 76 pNew;. unixLeav
cbfc0 65 4d 75 74 65 78 28 29 3b 0a 20 20 72 65 74 75 eMutex();. retu
cbfd0 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a rn pNew;.}../*.*
cbfe0 2a 20 44 65 63 72 65 6d 65 6e 74 20 74 68 65 20 * Decrement the
cbff0 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 reference count
cc000 6f 6e 20 61 20 76 78 77 6f 72 6b 73 46 69 6c 65 on a vxworksFile
cc010 49 64 20 6f 62 6a 65 63 74 2e 20 20 46 72 65 65 Id object. Free
cc020 0a 2a 2a 20 74 68 65 20 6f 62 6a 65 63 74 20 77 .** the object w
cc030 68 65 6e 20 74 68 65 20 72 65 66 65 72 65 6e 63 hen the referenc
cc040 65 20 63 6f 75 6e 74 20 72 65 61 63 68 65 73 20 e count reaches
cc050 7a 65 72 6f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 zero..*/.static
cc060 76 6f 69 64 20 76 78 77 6f 72 6b 73 52 65 6c 65 void vxworksRele
cc070 61 73 65 46 69 6c 65 49 64 28 73 74 72 75 63 74 aseFileId(struct
cc080 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a vxworksFileId *
cc090 70 49 64 29 7b 0a 20 20 75 6e 69 78 45 6e 74 65 pId){. unixEnte
cc0a0 72 4d 75 74 65 78 28 29 3b 0a 20 20 61 73 73 65 rMutex();. asse
cc0b0 72 74 28 20 70 49 64 2d 3e 6e 52 65 66 3e 30 20 rt( pId->nRef>0
cc0c0 29 3b 0a 20 20 70 49 64 2d 3e 6e 52 65 66 2d 2d );. pId->nRef--
cc0d0 3b 0a 20 20 69 66 28 20 70 49 64 2d 3e 6e 52 65 ;. if( pId->nRe
cc0e0 66 3d 3d 30 20 29 7b 0a 20 20 20 20 73 74 72 75 f==0 ){. stru
cc0f0 63 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 ct vxworksFileId
cc100 20 2a 2a 70 70 3b 0a 20 20 20 20 66 6f 72 28 70 **pp;. for(p
cc110 70 3d 26 76 78 77 6f 72 6b 73 46 69 6c 65 4c 69 p=&vxworksFileLi
cc120 73 74 3b 20 2a 70 70 20 26 26 20 2a 70 70 21 3d st; *pp && *pp!=
cc130 70 49 64 3b 20 70 70 20 3d 20 26 28 28 2a 70 70 pId; pp = &((*pp
cc140 29 2d 3e 70 4e 65 78 74 29 29 7b 7d 0a 20 20 20 )->pNext)){}.
cc150 20 61 73 73 65 72 74 28 20 2a 70 70 3d 3d 70 49 assert( *pp==pI
cc160 64 20 29 3b 0a 20 20 20 20 2a 70 70 20 3d 20 70 d );. *pp = p
cc170 49 64 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 73 Id->pNext;. s
cc180 71 6c 69 74 65 33 5f 66 72 65 65 28 70 49 64 29 qlite3_free(pId)
cc190 3b 0a 20 20 7d 0a 20 20 75 6e 69 78 4c 65 61 76 ;. }. unixLeav
cc1a0 65 4d 75 74 65 78 28 29 3b 0a 7d 0a 23 65 6e 64 eMutex();.}.#end
cc1b0 69 66 20 2f 2a 20 4f 53 5f 56 58 57 4f 52 4b 53 if /* OS_VXWORKS
cc1c0 20 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./***********
cc1d0 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 55 6e 69 71 **** End of Uniq
cc1e0 75 65 20 46 69 6c 65 20 49 44 20 55 74 69 6c 69 ue File ID Utili
cc1f0 74 79 20 55 73 65 64 20 42 79 20 56 78 57 6f 72 ty Used By VxWor
cc200 6b 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ks *************
cc210 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***.************
cc220 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc240 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc250 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc260 2a 2a 2f 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a **/.../*********
cc270 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc290 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc2a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc2b0 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *****.**********
cc2c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc2d0 2a 20 50 6f 73 69 78 20 41 64 76 69 73 6f 72 79 * Posix Advisory
cc2e0 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a 2a 2a 2a 2a Locking *******
cc2f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc300 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 50 4f 53 49 *****.**.** POSI
cc310 58 20 61 64 76 69 73 6f 72 79 20 6c 6f 63 6b 73 X advisory locks
cc320 20 61 72 65 20 62 72 6f 6b 65 6e 20 62 79 20 64 are broken by d
cc330 65 73 69 67 6e 2e 20 20 41 4e 53 49 20 53 54 44 esign. ANSI STD
cc340 20 31 30 30 33 2e 31 20 28 31 39 39 36 29 0a 2a 1003.1 (1996).*
cc350 2a 20 73 65 63 74 69 6f 6e 20 36 2e 35 2e 32 2e * section 6.5.2.
cc360 32 20 6c 69 6e 65 73 20 34 38 33 20 74 68 72 6f 2 lines 483 thro
cc370 75 67 68 20 34 39 30 20 73 70 65 63 69 66 79 20 ugh 490 specify
cc380 74 68 61 74 20 77 68 65 6e 20 61 20 70 72 6f 63 that when a proc
cc390 65 73 73 0a 2a 2a 20 73 65 74 73 20 6f 72 20 63 ess.** sets or c
cc3a0 6c 65 61 72 73 20 61 20 6c 6f 63 6b 2c 20 74 68 lears a lock, th
cc3b0 61 74 20 6f 70 65 72 61 74 69 6f 6e 20 6f 76 65 at operation ove
cc3c0 72 72 69 64 65 73 20 61 6e 79 20 70 72 69 6f 72 rrides any prior
cc3d0 20 6c 6f 63 6b 73 20 73 65 74 0a 2a 2a 20 62 79 locks set.** by
cc3e0 20 74 68 65 20 73 61 6d 65 20 70 72 6f 63 65 73 the same proces
cc3f0 73 2e 20 20 49 74 20 64 6f 65 73 20 6e 6f 74 20 s. It does not
cc400 65 78 70 6c 69 63 69 74 6c 79 20 73 61 79 20 73 explicitly say s
cc410 6f 2c 20 62 75 74 20 74 68 69 73 20 69 6d 70 6c o, but this impl
cc420 69 65 73 0a 2a 2a 20 74 68 61 74 20 69 74 20 6f ies.** that it o
cc430 76 65 72 72 69 64 65 73 20 6c 6f 63 6b 73 20 73 verrides locks s
cc440 65 74 20 62 79 20 74 68 65 20 73 61 6d 65 20 70 et by the same p
cc450 72 6f 63 65 73 73 20 75 73 69 6e 67 20 61 20 64 rocess using a d
cc460 69 66 66 65 72 65 6e 74 0a 2a 2a 20 66 69 6c 65 ifferent.** file
cc470 20 64 65 73 63 72 69 70 74 6f 72 2e 20 20 43 6f descriptor. Co
cc480 6e 73 69 64 65 72 20 74 68 69 73 20 74 65 73 74 nsider this test
cc490 20 63 61 73 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 case:.**.**
cc4a0 20 20 20 69 6e 74 20 66 64 31 20 3d 20 6f 70 65 int fd1 = ope
cc4b0 6e 28 22 2e 2f 66 69 6c 65 31 22 2c 20 4f 5f 52 n("./file1", O_R
cc4c0 44 57 52 7c 4f 5f 43 52 45 41 54 2c 20 30 36 34 DWR|O_CREAT, 064
cc4d0 34 29 3b 0a 2a 2a 20 20 20 20 20 20 20 69 6e 74 4);.** int
cc4e0 20 66 64 32 20 3d 20 6f 70 65 6e 28 22 2e 2f 66 fd2 = open("./f
cc4f0 69 6c 65 32 22 2c 20 4f 5f 52 44 57 52 7c 4f 5f ile2", O_RDWR|O_
cc500 43 52 45 41 54 2c 20 30 36 34 34 29 3b 0a 2a 2a CREAT, 0644);.**
cc510 0a 2a 2a 20 53 75 70 70 6f 73 65 20 2e 2f 66 69 .** Suppose ./fi
cc520 6c 65 31 20 61 6e 64 20 2e 2f 66 69 6c 65 32 20 le1 and ./file2
cc530 61 72 65 20 72 65 61 6c 6c 79 20 74 68 65 20 73 are really the s
cc540 61 6d 65 20 66 69 6c 65 20 28 62 65 63 61 75 73 ame file (becaus
cc550 65 0a 2a 2a 20 6f 6e 65 20 69 73 20 61 20 68 61 e.** one is a ha
cc560 72 64 20 6f 72 20 73 79 6d 62 6f 6c 69 63 20 6c rd or symbolic l
cc570 69 6e 6b 20 74 6f 20 74 68 65 20 6f 74 68 65 72 ink to the other
cc580 29 20 74 68 65 6e 20 69 66 20 79 6f 75 20 73 65 ) then if you se
cc590 74 0a 2a 2a 20 61 6e 20 65 78 63 6c 75 73 69 76 t.** an exclusiv
cc5a0 65 20 6c 6f 63 6b 20 6f 6e 20 66 64 31 2c 20 74 e lock on fd1, t
cc5b0 68 65 6e 20 74 72 79 20 74 6f 20 67 65 74 20 61 hen try to get a
cc5c0 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b n exclusive lock
cc5d0 0a 2a 2a 20 6f 6e 20 66 64 32 2c 20 69 74 20 77 .** on fd2, it w
cc5e0 6f 72 6b 73 2e 20 20 49 20 77 6f 75 6c 64 20 68 orks. I would h
cc5f0 61 76 65 20 65 78 70 65 63 74 65 64 20 74 68 65 ave expected the
cc600 20 73 65 63 6f 6e 64 20 6c 6f 63 6b 20 74 6f 0a second lock to.
cc610 2a 2a 20 66 61 69 6c 20 73 69 6e 63 65 20 74 68 ** fail since th
cc620 65 72 65 20 77 61 73 20 61 6c 72 65 61 64 79 20 ere was already
cc630 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 66 69 a lock on the fi
cc640 6c 65 20 64 75 65 20 74 6f 20 66 64 31 2e 0a 2a le due to fd1..*
cc650 2a 20 42 75 74 20 6e 6f 74 20 73 6f 2e 20 20 53 * But not so. S
cc660 69 6e 63 65 20 62 6f 74 68 20 6c 6f 63 6b 73 20 ince both locks
cc670 63 61 6d 65 20 66 72 6f 6d 20 74 68 65 20 73 61 came from the sa
cc680 6d 65 20 70 72 6f 63 65 73 73 2c 20 74 68 65 0a me process, the.
cc690 2a 2a 20 73 65 63 6f 6e 64 20 6f 76 65 72 72 69 ** second overri
cc6a0 64 65 73 20 74 68 65 20 66 69 72 73 74 2c 20 65 des the first, e
cc6b0 76 65 6e 20 74 68 6f 75 67 68 20 74 68 65 79 20 ven though they
cc6c0 77 65 72 65 20 6f 6e 20 64 69 66 66 65 72 65 6e were on differen
cc6d0 74 0a 2a 2a 20 66 69 6c 65 20 64 65 73 63 72 69 t.** file descri
cc6e0 70 74 6f 72 73 20 6f 70 65 6e 65 64 20 6f 6e 20 ptors opened on
cc6f0 64 69 66 66 65 72 65 6e 74 20 66 69 6c 65 20 6e different file n
cc700 61 6d 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 ames..**.** This
cc710 20 6d 65 61 6e 73 20 74 68 61 74 20 77 65 20 63 means that we c
cc720 61 6e 6e 6f 74 20 75 73 65 20 50 4f 53 49 58 20 annot use POSIX
cc730 6c 6f 63 6b 73 20 74 6f 20 73 79 6e 63 68 72 6f locks to synchro
cc740 6e 69 7a 65 20 66 69 6c 65 20 61 63 63 65 73 73 nize file access
cc750 0a 2a 2a 20 61 6d 6f 6e 67 20 63 6f 6d 70 65 74 .** among compet
cc760 69 6e 67 20 74 68 72 65 61 64 73 20 6f 66 20 74 ing threads of t
cc770 68 65 20 73 61 6d 65 20 70 72 6f 63 65 73 73 2e he same process.
cc780 20 20 50 4f 53 49 58 20 6c 6f 63 6b 73 20 77 69 POSIX locks wi
cc790 6c 6c 20 77 6f 72 6b 20 66 69 6e 65 0a 2a 2a 20 ll work fine.**
cc7a0 74 6f 20 73 79 6e 63 68 72 6f 6e 69 7a 65 20 61 to synchronize a
cc7b0 63 63 65 73 73 20 66 6f 72 20 74 68 72 65 61 64 ccess for thread
cc7c0 73 20 69 6e 20 73 65 70 61 72 61 74 65 20 70 72 s in separate pr
cc7d0 6f 63 65 73 73 65 73 2c 20 62 75 74 20 6e 6f 74 ocesses, but not
cc7e0 0a 2a 2a 20 74 68 72 65 61 64 73 20 77 69 74 68 .** threads with
cc7f0 69 6e 20 74 68 65 20 73 61 6d 65 20 70 72 6f 63 in the same proc
cc800 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 77 6f ess..**.** To wo
cc810 72 6b 20 61 72 6f 75 6e 64 20 74 68 65 20 70 72 rk around the pr
cc820 6f 62 6c 65 6d 2c 20 53 51 4c 69 74 65 20 68 61 oblem, SQLite ha
cc830 73 20 74 6f 20 6d 61 6e 61 67 65 20 66 69 6c 65 s to manage file
cc840 20 6c 6f 63 6b 73 20 69 6e 74 65 72 6e 61 6c 6c locks internall
cc850 79 0a 2a 2a 20 6f 6e 20 69 74 73 20 6f 77 6e 2e y.** on its own.
cc860 20 20 57 68 65 6e 65 76 65 72 20 61 20 6e 65 77 Whenever a new
cc870 20 64 61 74 61 62 61 73 65 20 69 73 20 6f 70 65 database is ope
cc880 6e 65 64 2c 20 77 65 20 68 61 76 65 20 74 6f 20 ned, we have to
cc890 66 69 6e 64 20 74 68 65 0a 2a 2a 20 73 70 65 63 find the.** spec
cc8a0 69 66 69 63 20 69 6e 6f 64 65 20 6f 66 20 74 68 ific inode of th
cc8b0 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
cc8c0 28 74 68 65 20 69 6e 6f 64 65 20 69 73 20 64 65 (the inode is de
cc8d0 74 65 72 6d 69 6e 65 64 20 62 79 20 74 68 65 0a termined by the.
cc8e0 2a 2a 20 73 74 5f 64 65 76 20 61 6e 64 20 73 74 ** st_dev and st
cc8f0 5f 69 6e 6f 20 66 69 65 6c 64 73 20 6f 66 20 74 _ino fields of t
cc900 68 65 20 73 74 61 74 20 73 74 72 75 63 74 75 72 he stat structur
cc910 65 20 74 68 61 74 20 66 73 74 61 74 28 29 20 66 e that fstat() f
cc920 69 6c 6c 73 20 69 6e 29 0a 2a 2a 20 61 6e 64 20 ills in).** and
cc930 63 68 65 63 6b 20 66 6f 72 20 6c 6f 63 6b 73 20 check for locks
cc940 61 6c 72 65 61 64 79 20 65 78 69 73 74 69 6e 67 already existing
cc950 20 6f 6e 20 74 68 61 74 20 69 6e 6f 64 65 2e 20 on that inode.
cc960 20 57 68 65 6e 20 6c 6f 63 6b 73 20 61 72 65 0a When locks are.
cc970 2a 2a 20 63 72 65 61 74 65 64 20 6f 72 20 72 65 ** created or re
cc980 6d 6f 76 65 64 2c 20 77 65 20 68 61 76 65 20 74 moved, we have t
cc990 6f 20 6c 6f 6f 6b 20 61 74 20 6f 75 72 20 6f 77 o look at our ow
cc9a0 6e 20 69 6e 74 65 72 6e 61 6c 20 72 65 63 6f 72 n internal recor
cc9b0 64 20 6f 66 20 74 68 65 0a 2a 2a 20 6c 6f 63 6b d of the.** lock
cc9c0 73 20 74 6f 20 73 65 65 20 69 66 20 61 6e 6f 74 s to see if anot
cc9d0 68 65 72 20 74 68 72 65 61 64 20 68 61 73 20 70 her thread has p
cc9e0 72 65 76 69 6f 75 73 6c 79 20 73 65 74 20 61 20 reviously set a
cc9f0 6c 6f 63 6b 20 6f 6e 20 74 68 61 74 20 73 61 6d lock on that sam
cca00 65 0a 2a 2a 20 69 6e 6f 64 65 2e 0a 2a 2a 0a 2a e.** inode..**.*
cca10 2a 20 28 41 73 69 64 65 3a 20 54 68 65 20 75 73 * (Aside: The us
cca20 65 20 6f 66 20 69 6e 6f 64 65 20 6e 75 6d 62 65 e of inode numbe
cca30 72 73 20 61 73 20 75 6e 69 71 75 65 20 49 44 73 rs as unique IDs
cca40 20 64 6f 65 73 20 6e 6f 74 20 77 6f 72 6b 20 6f does not work o
cca50 6e 20 56 78 57 6f 72 6b 73 2e 0a 2a 2a 20 46 6f n VxWorks..** Fo
cca60 72 20 56 78 57 6f 72 6b 73 2c 20 77 65 20 68 61 r VxWorks, we ha
cca70 76 65 20 74 6f 20 75 73 65 20 74 68 65 20 61 6c ve to use the al
cca80 74 65 72 6e 61 74 69 76 65 20 75 6e 69 71 75 65 ternative unique
cca90 20 49 44 20 73 79 73 74 65 6d 20 62 61 73 65 64 ID system based
ccaa0 20 6f 6e 0a 2a 2a 20 63 61 6e 6f 6e 69 63 61 6c on.** canonical
ccab0 20 66 69 6c 65 6e 61 6d 65 20 61 6e 64 20 69 6d filename and im
ccac0 70 6c 65 6d 65 6e 74 65 64 20 69 6e 20 74 68 65 plemented in the
ccad0 20 70 72 65 76 69 6f 75 73 20 64 69 76 69 73 69 previous divisi
ccae0 6f 6e 2e 29 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 on.).**.** The s
ccaf0 71 6c 69 74 65 33 5f 66 69 6c 65 20 73 74 72 75 qlite3_file stru
ccb00 63 74 75 72 65 20 66 6f 72 20 50 4f 53 49 58 20 cture for POSIX
ccb10 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 6a 75 73 is no longer jus
ccb20 74 20 61 6e 20 69 6e 74 65 67 65 72 20 66 69 6c t an integer fil
ccb30 65 0a 2a 2a 20 64 65 73 63 72 69 70 74 6f 72 2e e.** descriptor.
ccb40 20 20 49 74 20 69 73 20 6e 6f 77 20 61 20 73 74 It is now a st
ccb50 72 75 63 74 75 72 65 20 74 68 61 74 20 68 6f 6c ructure that hol
ccb60 64 73 20 74 68 65 20 69 6e 74 65 67 65 72 20 66 ds the integer f
ccb70 69 6c 65 0a 2a 2a 20 64 65 73 63 72 69 70 74 6f ile.** descripto
ccb80 72 20 61 6e 64 20 61 20 70 6f 69 6e 74 65 72 20 r and a pointer
ccb90 74 6f 20 61 20 73 74 72 75 63 74 75 72 65 20 74 to a structure t
ccba0 68 61 74 20 64 65 73 63 72 69 62 65 73 20 74 68 hat describes th
ccbb0 65 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 6c 6f e internal.** lo
ccbc0 63 6b 73 20 6f 6e 20 74 68 65 20 63 6f 72 72 65 cks on the corre
ccbd0 73 70 6f 6e 64 69 6e 67 20 69 6e 6f 64 65 2e 20 sponding inode.
ccbe0 20 54 68 65 72 65 20 69 73 20 6f 6e 65 20 6c 6f There is one lo
ccbf0 63 6b 69 6e 67 20 73 74 72 75 63 74 75 72 65 0a cking structure.
ccc00 2a 2a 20 70 65 72 20 69 6e 6f 64 65 2c 20 73 6f ** per inode, so
ccc10 20 69 66 20 74 68 65 20 73 61 6d 65 20 69 6e 6f if the same ino
ccc20 64 65 20 69 73 20 6f 70 65 6e 65 64 20 74 77 69 de is opened twi
ccc30 63 65 2c 20 62 6f 74 68 20 75 6e 69 78 46 69 6c ce, both unixFil
ccc40 65 20 73 74 72 75 63 74 75 72 65 73 0a 2a 2a 20 e structures.**
ccc50 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 73 61 6d point to the sam
ccc60 65 20 6c 6f 63 6b 69 6e 67 20 73 74 72 75 63 74 e locking struct
ccc70 75 72 65 2e 20 20 54 68 65 20 6c 6f 63 6b 69 6e ure. The lockin
ccc80 67 20 73 74 72 75 63 74 75 72 65 20 6b 65 65 70 g structure keep
ccc90 73 0a 2a 2a 20 61 20 72 65 66 65 72 65 6e 63 65 s.** a reference
ccca0 20 63 6f 75 6e 74 20 28 73 6f 20 77 65 20 77 69 count (so we wi
cccb0 6c 6c 20 6b 6e 6f 77 20 77 68 65 6e 20 74 6f 20 ll know when to
cccc0 64 65 6c 65 74 65 20 69 74 29 20 61 6e 64 20 61 delete it) and a
cccd0 20 22 63 6e 74 22 0a 2a 2a 20 66 69 65 6c 64 20 "cnt".** field
ccce0 74 68 61 74 20 74 65 6c 6c 73 20 75 73 20 69 74 that tells us it
cccf0 73 20 69 6e 74 65 72 6e 61 6c 20 6c 6f 63 6b 20 s internal lock
ccd00 73 74 61 74 75 73 2e 20 20 63 6e 74 3d 3d 30 20 status. cnt==0
ccd10 6d 65 61 6e 73 20 74 68 65 0a 2a 2a 20 66 69 6c means the.** fil
ccd20 65 20 69 73 20 75 6e 6c 6f 63 6b 65 64 2e 20 20 e is unlocked.
ccd30 63 6e 74 3d 3d 2d 31 20 6d 65 61 6e 73 20 74 68 cnt==-1 means th
ccd40 65 20 66 69 6c 65 20 68 61 73 20 61 6e 20 65 78 e file has an ex
ccd50 63 6c 75 73 69 76 65 20 6c 6f 63 6b 2e 0a 2a 2a clusive lock..**
ccd60 20 63 6e 74 3e 30 20 6d 65 61 6e 73 20 74 68 65 cnt>0 means the
ccd70 72 65 20 61 72 65 20 63 6e 74 20 73 68 61 72 65 re are cnt share
ccd80 64 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 65 20 66 d locks on the f
ccd90 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 6e 79 20 61 ile..**.** Any a
ccda0 74 74 65 6d 70 74 20 74 6f 20 6c 6f 63 6b 20 6f ttempt to lock o
ccdb0 72 20 75 6e 6c 6f 63 6b 20 61 20 66 69 6c 65 20 r unlock a file
ccdc0 66 69 72 73 74 20 63 68 65 63 6b 73 20 74 68 65 first checks the
ccdd0 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 73 74 72 75 locking.** stru
ccde0 63 74 75 72 65 2e 20 20 54 68 65 20 66 63 6e 74 cture. The fcnt
ccdf0 6c 28 29 20 73 79 73 74 65 6d 20 63 61 6c 6c 20 l() system call
cce00 69 73 20 6f 6e 6c 79 20 69 6e 76 6f 6b 65 64 20 is only invoked
cce10 74 6f 20 73 65 74 20 61 20 0a 2a 2a 20 50 4f 53 to set a .** POS
cce20 49 58 20 6c 6f 63 6b 20 69 66 20 74 68 65 20 69 IX lock if the i
cce30 6e 74 65 72 6e 61 6c 20 6c 6f 63 6b 20 73 74 72 nternal lock str
cce40 75 63 74 75 72 65 20 74 72 61 6e 73 69 74 69 6f ucture transitio
cce50 6e 73 20 62 65 74 77 65 65 6e 0a 2a 2a 20 61 20 ns between.** a
cce60 6c 6f 63 6b 65 64 20 61 6e 64 20 61 6e 20 75 6e locked and an un
cce70 6c 6f 63 6b 65 64 20 73 74 61 74 65 2e 0a 2a 2a locked state..**
cce80 0a 2a 2a 20 42 75 74 20 77 61 69 74 3a 20 20 74 .** But wait: t
cce90 68 65 72 65 20 61 72 65 20 79 65 74 20 6d 6f 72 here are yet mor
ccea0 65 20 70 72 6f 62 6c 65 6d 73 20 77 69 74 68 20 e problems with
cceb0 50 4f 53 49 58 20 61 64 76 69 73 6f 72 79 20 6c POSIX advisory l
ccec0 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 79 ocks..**.** If y
cced0 6f 75 20 63 6c 6f 73 65 20 61 20 66 69 6c 65 20 ou close a file
ccee0 64 65 73 63 72 69 70 74 6f 72 20 74 68 61 74 20 descriptor that
ccef0 70 6f 69 6e 74 73 20 74 6f 20 61 20 66 69 6c 65 points to a file
ccf00 20 74 68 61 74 20 68 61 73 20 6c 6f 63 6b 73 2c that has locks,
ccf10 0a 2a 2a 20 61 6c 6c 20 6c 6f 63 6b 73 20 6f 6e .** all locks on
ccf20 20 74 68 61 74 20 66 69 6c 65 20 74 68 61 74 20 that file that
ccf30 61 72 65 20 6f 77 6e 65 64 20 62 79 20 74 68 65 are owned by the
ccf40 20 63 75 72 72 65 6e 74 20 70 72 6f 63 65 73 73 current process
ccf50 20 61 72 65 0a 2a 2a 20 72 65 6c 65 61 73 65 64 are.** released
ccf60 2e 20 20 54 6f 20 77 6f 72 6b 20 61 72 6f 75 6e . To work aroun
ccf70 64 20 74 68 69 73 20 70 72 6f 62 6c 65 6d 2c 20 d this problem,
ccf80 65 61 63 68 20 75 6e 69 78 46 69 6c 65 20 73 74 each unixFile st
ccf90 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 ructure contains
ccfa0 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f .** a pointer to
ccfb0 20 61 6e 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 an unixOpenCnt
ccfc0 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 72 structure. Ther
ccfd0 65 20 69 73 20 6f 6e 65 20 75 6e 69 78 4f 70 65 e is one unixOpe
ccfe0 6e 43 6e 74 20 73 74 72 75 63 74 75 72 65 0a 2a nCnt structure.*
ccff0 2a 20 70 65 72 20 6f 70 65 6e 20 69 6e 6f 64 65 * per open inode
cd000 2c 20 77 68 69 63 68 20 6d 65 61 6e 73 20 74 68 , which means th
cd010 61 74 20 6d 75 6c 74 69 70 6c 65 20 75 6e 69 78 at multiple unix
cd020 46 69 6c 65 20 63 61 6e 20 70 6f 69 6e 74 20 74 File can point t
cd030 6f 20 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 75 6e o a single.** un
cd040 69 78 4f 70 65 6e 43 6e 74 2e 20 20 57 68 65 6e ixOpenCnt. When
cd050 20 61 6e 20 61 74 74 65 6d 70 74 20 69 73 20 6d an attempt is m
cd060 61 64 65 20 74 6f 20 63 6c 6f 73 65 20 61 6e 20 ade to close an
cd070 75 6e 69 78 46 69 6c 65 2c 20 69 66 20 74 68 65 unixFile, if the
cd080 72 65 20 61 72 65 0a 2a 2a 20 6f 74 68 65 72 20 re are.** other
cd090 75 6e 69 78 46 69 6c 65 20 6f 70 65 6e 20 6f 6e unixFile open on
cd0a0 20 74 68 65 20 73 61 6d 65 20 69 6e 6f 64 65 20 the same inode
cd0b0 74 68 61 74 20 61 72 65 20 68 6f 6c 64 69 6e 67 that are holding
cd0c0 20 6c 6f 63 6b 73 2c 20 74 68 65 20 63 61 6c 6c locks, the call
cd0d0 0a 2a 2a 20 74 6f 20 63 6c 6f 73 65 28 29 20 74 .** to close() t
cd0e0 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 he file descript
cd0f0 6f 72 20 69 73 20 64 65 66 65 72 72 65 64 20 75 or is deferred u
cd100 6e 74 69 6c 20 61 6c 6c 20 6f 66 20 74 68 65 20 ntil all of the
cd110 6c 6f 63 6b 73 20 63 6c 65 61 72 2e 0a 2a 2a 20 locks clear..**
cd120 54 68 65 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 The unixOpenCnt
cd130 73 74 72 75 63 74 75 72 65 20 6b 65 65 70 73 20 structure keeps
cd140 61 20 6c 69 73 74 20 6f 66 20 66 69 6c 65 20 64 a list of file d
cd150 65 73 63 72 69 70 74 6f 72 73 20 74 68 61 74 20 escriptors that
cd160 6e 65 65 64 20 74 6f 0a 2a 2a 20 62 65 20 63 6c need to.** be cl
cd170 6f 73 65 64 20 61 6e 64 20 74 68 61 74 20 6c 69 osed and that li
cd180 73 74 20 69 73 20 77 61 6c 6b 65 64 20 28 61 6e st is walked (an
cd190 64 20 63 6c 65 61 72 65 64 29 20 77 68 65 6e 20 d cleared) when
cd1a0 74 68 65 20 6c 61 73 74 20 6c 6f 63 6b 0a 2a 2a the last lock.**
cd1b0 20 63 6c 65 61 72 73 2e 0a 2a 2a 0a 2a 2a 20 59 clears..**.** Y
cd1c0 65 74 20 61 6e 6f 74 68 65 72 20 70 72 6f 62 6c et another probl
cd1d0 65 6d 3a 20 20 4c 69 6e 75 78 54 68 72 65 61 64 em: LinuxThread
cd1e0 73 20 64 6f 20 6e 6f 74 20 70 6c 61 79 20 77 65 s do not play we
cd1f0 6c 6c 20 77 69 74 68 20 70 6f 73 69 78 20 6c 6f ll with posix lo
cd200 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 4d 61 6e 79 20 cks..**.** Many
cd210 6f 6c 64 65 72 20 76 65 72 73 69 6f 6e 73 20 6f older versions o
cd220 66 20 6c 69 6e 75 78 20 75 73 65 20 74 68 65 20 f linux use the
cd230 4c 69 6e 75 78 54 68 72 65 61 64 73 20 6c 69 62 LinuxThreads lib
cd240 72 61 72 79 20 77 68 69 63 68 20 69 73 0a 2a 2a rary which is.**
cd250 20 6e 6f 74 20 70 6f 73 69 78 20 63 6f 6d 70 6c not posix compl
cd260 69 61 6e 74 2e 20 20 55 6e 64 65 72 20 4c 69 6e iant. Under Lin
cd270 75 78 54 68 72 65 61 64 73 2c 20 61 20 6c 6f 63 uxThreads, a loc
cd280 6b 20 63 72 65 61 74 65 64 20 62 79 20 74 68 72 k created by thr
cd290 65 61 64 0a 2a 2a 20 41 20 63 61 6e 6e 6f 74 20 ead.** A cannot
cd2a0 62 65 20 6d 6f 64 69 66 69 65 64 20 6f 72 20 6f be modified or o
cd2b0 76 65 72 72 69 64 64 65 6e 20 62 79 20 61 20 64 verridden by a d
cd2c0 69 66 66 65 72 65 6e 74 20 74 68 72 65 61 64 20 ifferent thread
cd2d0 42 2e 0a 2a 2a 20 4f 6e 6c 79 20 74 68 72 65 61 B..** Only threa
cd2e0 64 20 41 20 63 61 6e 20 6d 6f 64 69 66 79 20 74 d A can modify t
cd2f0 68 65 20 6c 6f 63 6b 2e 20 20 4c 6f 63 6b 69 6e he lock. Lockin
cd300 67 20 62 65 68 61 76 69 6f 72 20 69 73 20 63 6f g behavior is co
cd310 72 72 65 63 74 0a 2a 2a 20 69 66 20 74 68 65 20 rrect.** if the
cd320 61 70 70 6c 69 61 74 69 6f 6e 20 75 73 65 73 20 appliation uses
cd330 74 68 65 20 6e 65 77 65 72 20 4e 61 74 69 76 65 the newer Native
cd340 20 50 6f 73 69 78 20 54 68 72 65 61 64 20 4c 69 Posix Thread Li
cd350 62 72 61 72 79 20 28 4e 50 54 4c 29 0a 2a 2a 20 brary (NPTL).**
cd360 6f 6e 20 6c 69 6e 75 78 20 2d 20 77 69 74 68 20 on linux - with
cd370 4e 50 54 4c 20 61 20 6c 6f 63 6b 20 63 72 65 61 NPTL a lock crea
cd380 74 65 64 20 62 79 20 74 68 72 65 61 64 20 41 20 ted by thread A
cd390 63 61 6e 20 6f 76 65 72 72 69 64 65 20 6c 6f 63 can override loc
cd3a0 6b 73 0a 2a 2a 20 69 6e 20 74 68 72 65 61 64 20 ks.** in thread
cd3b0 42 2e 20 20 42 75 74 20 74 68 65 72 65 20 69 73 B. But there is
cd3c0 20 6e 6f 20 77 61 79 20 74 6f 20 6b 6e 6f 77 20 no way to know
cd3d0 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 at compile-time
cd3e0 77 68 69 63 68 0a 2a 2a 20 74 68 72 65 61 64 69 which.** threadi
cd3f0 6e 67 20 6c 69 62 72 61 72 79 20 69 73 20 62 65 ng library is be
cd400 69 6e 67 20 75 73 65 64 2e 20 20 53 6f 20 74 68 ing used. So th
cd410 65 72 65 20 69 73 20 6e 6f 20 77 61 79 20 74 6f ere is no way to
cd420 20 6b 6e 6f 77 20 61 74 0a 2a 2a 20 63 6f 6d 70 know at.** comp
cd430 69 6c 65 2d 74 69 6d 65 20 77 68 65 74 68 65 72 ile-time whether
cd440 20 6f 72 20 6e 6f 74 20 74 68 72 65 61 64 20 41 or not thread A
cd450 20 63 61 6e 20 6f 76 65 72 72 69 64 65 20 6c 6f can override lo
cd460 63 6b 73 20 6f 6e 20 74 68 72 65 61 64 20 42 2e cks on thread B.
cd470 0a 2a 2a 20 57 65 20 68 61 76 65 20 74 6f 20 64 .** We have to d
cd480 6f 20 61 20 72 75 6e 2d 74 69 6d 65 20 63 68 65 o a run-time che
cd490 63 6b 20 74 6f 20 64 69 73 63 6f 76 65 72 20 74 ck to discover t
cd4a0 68 65 20 62 65 68 61 76 69 6f 72 20 6f 66 20 74 he behavior of t
cd4b0 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 70 72 he.** current pr
cd4c0 6f 63 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 ocess..**.** On
cd4d0 73 79 73 74 65 6d 73 20 77 68 65 72 65 20 74 68 systems where th
cd4e0 72 65 61 64 20 41 20 69 73 20 75 6e 61 62 6c 65 read A is unable
cd4f0 20 74 6f 20 6d 6f 64 69 66 79 20 6c 6f 63 6b 73 to modify locks
cd500 20 63 72 65 61 74 65 64 20 62 79 0a 2a 2a 20 74 created by.** t
cd510 68 72 65 61 64 20 42 2c 20 77 65 20 68 61 76 65 hread B, we have
cd520 20 74 6f 20 6b 65 65 70 20 74 72 61 63 6b 20 6f to keep track o
cd530 66 20 77 68 69 63 68 20 74 68 72 65 61 64 20 63 f which thread c
cd540 72 65 61 74 65 64 20 65 61 63 68 0a 2a 2a 20 6c reated each.** l
cd550 6f 63 6b 2e 20 20 48 65 6e 63 65 20 74 68 65 72 ock. Hence ther
cd560 65 20 69 73 20 61 6e 20 65 78 74 72 61 20 66 69 e is an extra fi
cd570 65 6c 64 20 69 6e 20 74 68 65 20 6b 65 79 20 74 eld in the key t
cd580 6f 20 74 68 65 20 75 6e 69 78 4c 6f 63 6b 49 6e o the unixLockIn
cd590 66 6f 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20 fo.** structure
cd5a0 74 6f 20 72 65 63 6f 72 64 20 74 68 69 73 20 69 to record this i
cd5b0 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 20 41 6e 64 nformation. And
cd5c0 20 6f 6e 20 74 68 6f 73 65 20 73 79 73 74 65 6d on those system
cd5d0 73 20 69 74 0a 2a 2a 20 69 73 20 69 6c 6c 65 67 s it.** is illeg
cd5e0 61 6c 20 74 6f 20 62 65 67 69 6e 20 61 20 74 72 al to begin a tr
cd5f0 61 6e 73 61 63 74 69 6f 6e 20 69 6e 20 6f 6e 65 ansaction in one
cd600 20 74 68 72 65 61 64 20 61 6e 64 20 66 69 6e 69 thread and fini
cd610 73 68 20 69 74 0a 2a 2a 20 69 6e 20 61 6e 6f 74 sh it.** in anot
cd620 68 65 72 2e 20 20 46 6f 72 20 74 68 69 73 20 6c her. For this l
cd630 61 74 74 65 72 20 72 65 73 74 72 69 63 74 69 6f atter restrictio
cd640 6e 2c 20 74 68 65 72 65 20 69 73 20 6e 6f 20 77 n, there is no w
cd650 6f 72 6b 2d 61 72 6f 75 6e 64 2e 0a 2a 2a 20 49 ork-around..** I
cd660 74 20 69 73 20 61 20 6c 69 6d 69 74 61 74 69 6f t is a limitatio
cd670 6e 20 6f 66 20 4c 69 6e 75 78 54 68 72 65 61 64 n of LinuxThread
cd680 73 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 s..*/../*.** Set
cd690 20 6f 72 20 63 68 65 63 6b 20 74 68 65 20 75 6e or check the un
cd6a0 69 78 46 69 6c 65 2e 74 69 64 20 66 69 65 6c 64 ixFile.tid field
cd6b0 2e 20 20 54 68 69 73 20 66 69 65 6c 64 20 69 73 . This field is
cd6c0 20 73 65 74 20 77 68 65 6e 20 61 6e 20 75 6e 69 set when an uni
cd6d0 78 46 69 6c 65 0a 2a 2a 20 69 73 20 66 69 72 73 xFile.** is firs
cd6e0 74 20 6f 70 65 6e 65 64 2e 20 20 41 6c 6c 20 73 t opened. All s
cd6f0 75 62 73 65 71 75 65 6e 74 20 75 73 65 73 20 6f ubsequent uses o
cd700 66 20 74 68 65 20 75 6e 69 78 46 69 6c 65 20 76 f the unixFile v
cd710 65 72 69 66 79 20 74 68 61 74 20 74 68 65 0a 2a erify that the.*
cd720 2a 20 73 61 6d 65 20 74 68 72 65 61 64 20 69 73 * same thread is
cd730 20 6f 70 65 72 61 74 69 6e 67 20 6f 6e 20 74 68 operating on th
cd740 65 20 75 6e 69 78 46 69 6c 65 2e 20 20 53 6f 6d e unixFile. Som
cd750 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 e operating syst
cd760 65 6d 73 20 64 6f 0a 2a 2a 20 6e 6f 74 20 61 6c ems do.** not al
cd770 6c 6f 77 20 6c 6f 63 6b 73 20 74 6f 20 62 65 20 low locks to be
cd780 6f 76 65 72 72 69 64 64 65 6e 20 62 79 20 6f 74 overridden by ot
cd790 68 65 72 20 74 68 72 65 61 64 73 20 61 6e 64 20 her threads and
cd7a0 74 68 61 74 20 72 65 73 74 72 69 63 74 69 6f 6e that restriction
cd7b0 0a 2a 2a 20 6d 65 61 6e 73 20 74 68 61 74 20 73 .** means that s
cd7c0 71 6c 69 74 65 33 2a 20 64 61 74 61 62 61 73 65 qlite3* database
cd7d0 20 68 61 6e 64 6c 65 73 20 63 61 6e 6e 6f 74 20 handles cannot
cd7e0 62 65 20 6d 6f 76 65 64 20 66 72 6f 6d 20 6f 6e be moved from on
cd7f0 65 20 74 68 72 65 61 64 0a 2a 2a 20 74 6f 20 61 e thread.** to a
cd800 6e 6f 74 68 65 72 20 77 68 69 6c 65 20 6c 6f 63 nother while loc
cd810 6b 73 20 61 72 65 20 68 65 6c 64 2e 0a 2a 2a 0a ks are held..**.
cd820 2a 2a 20 56 65 72 73 69 6f 6e 20 33 2e 33 2e 31 ** Version 3.3.1
cd830 20 28 32 30 30 36 2d 30 31 2d 31 35 29 3a 20 20 (2006-01-15):
cd840 75 6e 69 78 46 69 6c 65 20 63 61 6e 20 62 65 20 unixFile can be
cd850 6d 6f 76 65 64 20 66 72 6f 6d 20 6f 6e 65 20 74 moved from one t
cd860 68 72 65 61 64 20 74 6f 0a 2a 2a 20 61 6e 6f 74 hread to.** anot
cd870 68 65 72 20 61 73 20 6c 6f 6e 67 20 61 73 20 77 her as long as w
cd880 65 20 61 72 65 20 72 75 6e 6e 69 6e 67 20 6f 6e e are running on
cd890 20 61 20 73 79 73 74 65 6d 20 74 68 61 74 20 73 a system that s
cd8a0 75 70 70 6f 72 74 73 20 74 68 72 65 61 64 73 0a upports threads.
cd8b0 2a 2a 20 6f 76 65 72 72 69 64 69 6e 67 20 65 61 ** overriding ea
cd8c0 63 68 20 6f 74 68 65 72 73 20 6c 6f 63 6b 73 20 ch others locks
cd8d0 28 77 68 69 63 68 20 69 73 20 6e 6f 77 20 74 68 (which is now th
cd8e0 65 20 6d 6f 73 74 20 63 6f 6d 6d 6f 6e 20 62 65 e most common be
cd8f0 68 61 76 69 6f 72 29 0a 2a 2a 20 6f 72 20 69 66 havior).** or if
cd900 20 6e 6f 20 6c 6f 63 6b 73 20 61 72 65 20 68 65 no locks are he
cd910 6c 64 2e 20 20 42 75 74 20 74 68 65 20 75 6e 69 ld. But the uni
cd920 78 46 69 6c 65 2e 70 4c 6f 63 6b 20 66 69 65 6c xFile.pLock fiel
cd930 64 20 6e 65 65 64 73 20 74 6f 20 62 65 0a 2a 2a d needs to be.**
cd940 20 72 65 63 6f 6d 70 75 74 65 64 20 62 65 63 61 recomputed beca
cd950 75 73 65 20 69 74 73 20 6b 65 79 20 69 6e 63 6c use its key incl
cd960 75 64 65 73 20 74 68 65 20 74 68 72 65 61 64 2d udes the thread-
cd970 69 64 2e 20 20 53 65 65 20 74 68 65 20 0a 2a 2a id. See the .**
cd980 20 74 72 61 6e 73 66 65 72 4f 77 6e 65 72 73 68 transferOwnersh
cd990 69 70 28 29 20 66 75 6e 63 74 69 6f 6e 20 62 65 ip() function be
cd9a0 6c 6f 77 20 66 6f 72 20 61 64 64 69 74 69 6f 6e low for addition
cd9b0 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a al information.*
cd9c0 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 /.#if SQLITE_THR
cd9d0 45 41 44 53 41 46 45 20 26 26 20 64 65 66 69 6e EADSAFE && defin
cd9e0 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 0a 23 20 ed(__linux__).#
cd9f0 64 65 66 69 6e 65 20 53 45 54 5f 54 48 52 45 41 define SET_THREA
cda00 44 49 44 28 58 29 20 20 20 28 58 29 2d 3e 74 69 DID(X) (X)->ti
cda10 64 20 3d 20 70 74 68 72 65 61 64 5f 73 65 6c 66 d = pthread_self
cda20 28 29 0a 23 20 64 65 66 69 6e 65 20 43 48 45 43 ().# define CHEC
cda30 4b 5f 54 48 52 45 41 44 49 44 28 58 29 20 28 74 K_THREADID(X) (t
cda40 68 72 65 61 64 73 4f 76 65 72 72 69 64 65 45 61 hreadsOverrideEa
cda50 63 68 4f 74 68 65 72 73 4c 6f 63 6b 73 3d 3d 30 chOthersLocks==0
cda60 20 26 26 20 5c 0a 20 20 20 20 20 20 20 20 20 20 && \.
cda70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
cda80 20 20 21 70 74 68 72 65 61 64 5f 65 71 75 61 6c !pthread_equal
cda90 28 28 58 29 2d 3e 74 69 64 2c 20 70 74 68 72 65 ((X)->tid, pthre
cdaa0 61 64 5f 73 65 6c 66 28 29 29 29 0a 23 65 6c 73 ad_self())).#els
cdab0 65 0a 23 20 64 65 66 69 6e 65 20 53 45 54 5f 54 e.# define SET_T
cdac0 48 52 45 41 44 49 44 28 58 29 0a 23 20 64 65 66 HREADID(X).# def
cdad0 69 6e 65 20 43 48 45 43 4b 5f 54 48 52 45 41 44 ine CHECK_THREAD
cdae0 49 44 28 58 29 20 30 0a 23 65 6e 64 69 66 0a 0a ID(X) 0.#endif..
cdaf0 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 /*.** An instanc
cdb00 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 e of the followi
cdb10 6e 67 20 73 74 72 75 63 74 75 72 65 20 73 65 72 ng structure ser
cdb20 76 65 73 20 61 73 20 74 68 65 20 6b 65 79 20 75 ves as the key u
cdb30 73 65 64 0a 2a 2a 20 74 6f 20 6c 6f 63 61 74 65 sed.** to locate
cdb40 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 75 6e a particular un
cdb50 69 78 4f 70 65 6e 43 6e 74 20 73 74 72 75 63 74 ixOpenCnt struct
cdb60 75 72 65 20 67 69 76 65 6e 20 69 74 73 20 69 6e ure given its in
cdb70 6f 64 65 2e 20 20 54 68 69 73 0a 2a 2a 20 69 73 ode. This.** is
cdb80 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 68 65 the same as the
cdb90 20 75 6e 69 78 4c 6f 63 6b 4b 65 79 20 65 78 63 unixLockKey exc
cdba0 65 70 74 20 74 68 61 74 20 74 68 65 20 74 68 72 ept that the thr
cdbb0 65 61 64 20 49 44 20 69 73 20 6f 6d 69 74 74 65 ead ID is omitte
cdbc0 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 75 6e 69 d..*/.struct uni
cdbd0 78 46 69 6c 65 49 64 20 7b 0a 20 20 64 65 76 5f xFileId {. dev_
cdbe0 74 20 64 65 76 3b 20 20 20 20 20 20 20 20 20 20 t dev;
cdbf0 20 20 20 20 20 20 20 20 2f 2a 20 44 65 76 69 63 /* Devic
cdc00 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 23 69 66 20 e number */.#if
cdc10 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 73 74 72 OS_VXWORKS. str
cdc20 75 63 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 uct vxworksFileI
cdc30 64 20 2a 70 49 64 3b 20 20 2f 2a 20 55 6e 69 71 d *pId; /* Uniq
cdc40 75 65 20 66 69 6c 65 20 49 44 20 66 6f 72 20 76 ue file ID for v
cdc50 78 77 6f 72 6b 73 2e 20 2a 2f 0a 23 65 6c 73 65 xworks. */.#else
cdc60 0a 20 20 69 6e 6f 5f 74 20 69 6e 6f 3b 20 20 20 . ino_t ino;
cdc70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
cdc80 2a 20 49 6e 6f 64 65 20 6e 75 6d 62 65 72 20 2a * Inode number *
cdc90 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a /.#endif.};../*.
cdca0 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f ** An instance o
cdcb0 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
cdcc0 73 74 72 75 63 74 75 72 65 20 73 65 72 76 65 73 structure serves
cdcd0 20 61 73 20 74 68 65 20 6b 65 79 20 75 73 65 64 as the key used
cdce0 0a 2a 2a 20 74 6f 20 6c 6f 63 61 74 65 20 61 20 .** to locate a
cdcf0 70 61 72 74 69 63 75 6c 61 72 20 75 6e 69 78 4c particular unixL
cdd00 6f 63 6b 49 6e 66 6f 20 73 74 72 75 63 74 75 72 ockInfo structur
cdd10 65 20 67 69 76 65 6e 20 69 74 73 20 69 6e 6f 64 e given its inod
cdd20 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 72 65 e..**.** If thre
cdd30 61 64 73 20 63 61 6e 6e 6f 74 20 6f 76 65 72 72 ads cannot overr
cdd40 69 64 65 20 65 61 63 68 20 6f 74 68 65 72 73 20 ide each others
cdd50 6c 6f 63 6b 73 20 28 4c 69 6e 75 78 54 68 72 65 locks (LinuxThre
cdd60 61 64 73 29 2c 20 74 68 65 6e 20 77 65 0a 2a 2a ads), then we.**
cdd70 20 73 65 74 20 74 68 65 20 75 6e 69 78 4c 6f 63 set the unixLoc
cdd80 6b 4b 65 79 2e 74 69 64 20 66 69 65 6c 64 20 74 kKey.tid field t
cdd90 6f 20 74 68 65 20 74 68 72 65 61 64 20 49 44 2e o the thread ID.
cdda0 20 20 49 66 20 74 68 72 65 61 64 73 20 63 61 6e If threads can
cddb0 20 6f 76 65 72 72 69 64 65 0a 2a 2a 20 65 61 63 override.** eac
cddc0 68 20 6f 74 68 65 72 73 20 6c 6f 63 6b 73 20 28 h others locks (
cddd0 50 6f 73 69 78 20 61 6e 64 20 4e 50 54 4c 29 20 Posix and NPTL)
cdde0 74 68 65 6e 20 74 69 64 20 69 73 20 61 6c 77 61 then tid is alwa
cddf0 79 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e 0a ys set to zero..
cde00 2a 2a 20 74 69 64 20 69 73 20 6f 6d 69 74 74 65 ** tid is omitte
cde10 64 20 69 66 20 77 65 20 63 6f 6d 70 69 6c 65 20 d if we compile
cde20 77 69 74 68 6f 75 74 20 74 68 72 65 61 64 69 6e without threadin
cde30 67 20 73 75 70 70 6f 72 74 20 6f 72 20 6f 6e 20 g support or on
cde40 61 6e 20 4f 53 0a 2a 2a 20 6f 74 68 65 72 20 74 an OS.** other t
cde50 68 61 6e 20 6c 69 6e 75 78 2e 0a 2a 2f 0a 73 74 han linux..*/.st
cde60 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b 4b 65 79 ruct unixLockKey
cde70 20 7b 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 {. struct unix
cde80 46 69 6c 65 49 64 20 66 69 64 3b 20 20 2f 2a 20 FileId fid; /*
cde90 55 6e 69 71 75 65 20 69 64 65 6e 74 69 66 69 65 Unique identifie
cdea0 72 20 66 6f 72 20 74 68 65 20 66 69 6c 65 20 2a r for the file *
cdeb0 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 /.#if SQLITE_THR
cdec0 45 41 44 53 41 46 45 20 26 26 20 64 65 66 69 6e EADSAFE && defin
cded0 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 0a 20 20 ed(__linux__).
cdee0 70 74 68 72 65 61 64 5f 74 20 74 69 64 3b 20 20 pthread_t tid;
cdef0 2f 2a 20 54 68 72 65 61 64 20 49 44 20 6f 66 20 /* Thread ID of
cdf00 6c 6f 63 6b 20 6f 77 6e 65 72 2e 20 5a 65 72 6f lock owner. Zero
cdf10 20 69 66 20 6e 6f 74 20 75 73 69 6e 67 20 4c 69 if not using Li
cdf20 6e 75 78 54 68 72 65 61 64 73 20 2a 2f 0a 23 65 nuxThreads */.#e
cdf30 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 ndif.};../*.** A
cdf40 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 n instance of th
cdf50 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 e following stru
cdf60 63 74 75 72 65 20 69 73 20 61 6c 6c 6f 63 61 74 cture is allocat
cdf70 65 64 20 66 6f 72 20 65 61 63 68 20 6f 70 65 6e ed for each open
cdf80 0a 2a 2a 20 69 6e 6f 64 65 2e 20 20 4f 72 2c 20 .** inode. Or,
cdf90 6f 6e 20 4c 69 6e 75 78 54 68 72 65 61 64 73 2c on LinuxThreads,
cdfa0 20 74 68 65 72 65 20 69 73 20 6f 6e 65 20 6f 66 there is one of
cdfb0 20 74 68 65 73 65 20 73 74 72 75 63 74 75 72 65 these structure
cdfc0 73 20 66 6f 72 0a 2a 2a 20 65 61 63 68 20 69 6e s for.** each in
cdfd0 6f 64 65 20 6f 70 65 6e 65 64 20 62 79 20 65 61 ode opened by ea
cdfe0 63 68 20 74 68 72 65 61 64 2e 0a 2a 2a 0a 2a 2a ch thread..**.**
cdff0 20 41 20 73 69 6e 67 6c 65 20 69 6e 6f 64 65 20 A single inode
ce000 63 61 6e 20 68 61 76 65 20 6d 75 6c 74 69 70 6c can have multipl
ce010 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f e file descripto
ce020 72 73 2c 20 73 6f 20 65 61 63 68 20 75 6e 69 78 rs, so each unix
ce030 46 69 6c 65 0a 2a 2a 20 73 74 72 75 63 74 75 72 File.** structur
ce040 65 20 63 6f 6e 74 61 69 6e 73 20 61 20 70 6f 69 e contains a poi
ce050 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 73 74 61 nter to an insta
ce060 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 nce of this obje
ce070 63 74 20 61 6e 64 20 74 68 69 73 0a 2a 2a 20 6f ct and this.** o
ce080 62 6a 65 63 74 20 6b 65 65 70 73 20 61 20 63 6f bject keeps a co
ce090 75 6e 74 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 unt of the numbe
ce0a0 72 20 6f 66 20 75 6e 69 78 46 69 6c 65 20 70 6f r of unixFile po
ce0b0 69 6e 74 69 6e 67 20 74 6f 20 69 74 2e 0a 2a 2f inting to it..*/
ce0c0 0a 73 74 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b .struct unixLock
ce0d0 49 6e 66 6f 20 7b 0a 20 20 73 74 72 75 63 74 20 Info {. struct
ce0e0 75 6e 69 78 4c 6f 63 6b 4b 65 79 20 6c 6f 63 6b unixLockKey lock
ce0f0 4b 65 79 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 Key; /* The
ce100 6c 6f 6f 6b 75 70 20 6b 65 79 20 2a 2f 0a 20 20 lookup key */.
ce110 69 6e 74 20 63 6e 74 3b 20 20 20 20 20 20 20 20 int cnt;
ce120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ce130 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 53 48 41 /* Number of SHA
ce140 52 45 44 20 6c 6f 63 6b 73 20 68 65 6c 64 20 2a RED locks held *
ce150 2f 0a 20 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 /. int locktype
ce160 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
ce170 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 53 48 /* One of SH
ce180 41 52 45 44 5f 4c 4f 43 4b 2c 20 52 45 53 45 52 ARED_LOCK, RESER
ce190 56 45 44 5f 4c 4f 43 4b 20 65 74 63 2e 20 2a 2f VED_LOCK etc. */
ce1a0 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 . int nRef;
ce1b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ce1c0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
ce1d0 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 69 73 pointers to this
ce1e0 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 structure */.
ce1f0 73 74 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b 49 struct unixLockI
ce200 6e 66 6f 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 nfo *pNext;
ce210 2f 2a 20 4c 69 73 74 20 6f 66 20 61 6c 6c 20 75 /* List of all u
ce220 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 6f 62 6a 65 nixLockInfo obje
ce230 63 74 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 cts */. struct
ce240 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 2a 70 50 unixLockInfo *pP
ce250 72 65 76 3b 20 20 20 20 20 2f 2a 20 20 20 20 2e rev; /* .
ce260 2e 2e 2e 20 64 6f 75 62 6c 79 20 6c 69 6e 6b 65 ... doubly linke
ce270 64 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 d */.};../*.** A
ce280 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 n instance of th
ce290 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 e following stru
ce2a0 63 74 75 72 65 20 69 73 20 61 6c 6c 6f 63 61 74 cture is allocat
ce2b0 65 64 20 66 6f 72 20 65 61 63 68 20 6f 70 65 6e ed for each open
ce2c0 0a 2a 2a 20 69 6e 6f 64 65 2e 20 20 54 68 69 73 .** inode. This
ce2d0 20 73 74 72 75 63 74 75 72 65 20 6b 65 65 70 73 structure keeps
ce2e0 20 74 72 61 63 6b 20 6f 66 20 74 68 65 20 6e 75 track of the nu
ce2f0 6d 62 65 72 20 6f 66 20 6c 6f 63 6b 73 20 6f 6e mber of locks on
ce300 20 74 68 61 74 0a 2a 2a 20 69 6e 6f 64 65 2e 20 that.** inode.
ce310 20 49 66 20 61 20 63 6c 6f 73 65 20 69 73 20 61 If a close is a
ce320 74 74 65 6d 70 74 65 64 20 61 67 61 69 6e 73 74 ttempted against
ce330 20 61 6e 20 69 6e 6f 64 65 20 74 68 61 74 20 69 an inode that i
ce340 73 20 68 6f 6c 64 69 6e 67 0a 2a 2a 20 6c 6f 63 s holding.** loc
ce350 6b 73 2c 20 74 68 65 20 63 6c 6f 73 65 20 69 73 ks, the close is
ce360 20 64 65 66 65 72 72 65 64 20 75 6e 74 69 6c 20 deferred until
ce370 61 6c 6c 20 6c 6f 63 6b 73 20 63 6c 65 61 72 20 all locks clear
ce380 62 79 20 61 64 64 69 6e 67 20 74 68 65 0a 2a 2a by adding the.**
ce390 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 file descriptor
ce3a0 20 74 6f 20 62 65 20 63 6c 6f 73 65 64 20 74 6f to be closed to
ce3b0 20 74 68 65 20 70 65 6e 64 69 6e 67 20 6c 69 73 the pending lis
ce3c0 74 2e 0a 2a 2a 0a 2a 2a 20 54 4f 44 4f 3a 20 20 t..**.** TODO:
ce3d0 43 6f 6e 73 69 64 65 72 20 63 68 61 6e 67 69 6e Consider changin
ce3e0 67 20 74 68 69 73 20 73 6f 20 74 68 61 74 20 74 g this so that t
ce3f0 68 65 72 65 20 69 73 20 6f 6e 6c 79 20 61 20 73 here is only a s
ce400 69 6e 67 6c 65 20 66 69 6c 65 0a 2a 2a 20 64 65 ingle file.** de
ce410 73 63 72 69 70 74 6f 72 20 66 6f 72 20 65 61 63 scriptor for eac
ce420 68 20 6f 70 65 6e 20 66 69 6c 65 2c 20 65 76 65 h open file, eve
ce430 6e 20 77 68 65 6e 20 69 74 20 69 73 20 6f 70 65 n when it is ope
ce440 6e 65 64 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d ned multiple tim
ce450 65 73 2e 0a 2a 2a 20 54 68 65 20 63 6c 6f 73 65 es..** The close
ce460 28 29 20 73 79 73 74 65 6d 20 63 61 6c 6c 20 77 () system call w
ce470 6f 75 6c 64 20 6f 6e 6c 79 20 6f 63 63 75 72 20 ould only occur
ce480 77 68 65 6e 20 74 68 65 20 6c 61 73 74 20 64 61 when the last da
ce490 74 61 62 61 73 65 0a 2a 2a 20 75 73 69 6e 67 20 tabase.** using
ce4a0 74 68 65 20 66 69 6c 65 20 63 6c 6f 73 65 73 2e the file closes.
ce4b0 0a 2a 2f 0a 73 74 72 75 63 74 20 75 6e 69 78 4f .*/.struct unixO
ce4c0 70 65 6e 43 6e 74 20 7b 0a 20 20 73 74 72 75 63 penCnt {. struc
ce4d0 74 20 75 6e 69 78 46 69 6c 65 49 64 20 66 69 6c t unixFileId fil
ce4e0 65 49 64 3b 20 20 20 2f 2a 20 54 68 65 20 6c 6f eId; /* The lo
ce4f0 6f 6b 75 70 20 6b 65 79 20 2a 2f 0a 20 20 69 6e okup key */. in
ce500 74 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20 t nRef;
ce510 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
ce520 62 65 72 20 6f 66 20 70 6f 69 6e 74 65 72 73 20 ber of pointers
ce530 74 6f 20 74 68 69 73 20 73 74 72 75 63 74 75 72 to this structur
ce540 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 4c 6f 63 6b e */. int nLock
ce550 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
ce560 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
ce570 6f 75 74 73 74 61 6e 64 69 6e 67 20 6c 6f 63 6b outstanding lock
ce580 73 20 2a 2f 0a 20 20 55 6e 69 78 55 6e 75 73 65 s */. UnixUnuse
ce590 64 46 64 20 2a 70 55 6e 75 73 65 64 3b 20 20 20 dFd *pUnused;
ce5a0 20 20 20 2f 2a 20 55 6e 75 73 65 64 20 66 69 6c /* Unused fil
ce5b0 65 20 64 65 73 63 72 69 70 74 6f 72 73 20 74 6f e descriptors to
ce5c0 20 63 6c 6f 73 65 20 2a 2f 0a 23 69 66 20 4f 53 close */.#if OS
ce5d0 5f 56 58 57 4f 52 4b 53 0a 20 20 73 65 6d 5f 74 _VXWORKS. sem_t
ce5e0 20 2a 70 53 65 6d 3b 20 20 20 20 20 20 20 20 20 *pSem;
ce5f0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
ce600 61 6d 65 64 20 50 4f 53 49 58 20 73 65 6d 61 70 amed POSIX semap
ce610 68 6f 72 65 20 2a 2f 0a 20 20 63 68 61 72 20 61 hore */. char a
ce620 53 65 6d 4e 61 6d 65 5b 4d 41 58 5f 50 41 54 48 SemName[MAX_PATH
ce630 4e 41 4d 45 2b 32 5d 3b 20 20 20 2f 2a 20 4e 61 NAME+2]; /* Na
ce640 6d 65 20 6f 66 20 74 68 61 74 20 73 65 6d 61 70 me of that semap
ce650 68 6f 72 65 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 hore */.#endif.
ce660 20 73 74 72 75 63 74 20 75 6e 69 78 4f 70 65 6e struct unixOpen
ce670 43 6e 74 20 2a 70 4e 65 78 74 2c 20 2a 70 50 72 Cnt *pNext, *pPr
ce680 65 76 3b 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 ev; /* List of
ce690 20 61 6c 6c 20 75 6e 69 78 4f 70 65 6e 43 6e 74 all unixOpenCnt
ce6a0 20 6f 62 6a 65 63 74 73 20 2a 2f 0a 7d 3b 0a 0a objects */.};..
ce6b0 2f 2a 0a 2a 2a 20 4c 69 73 74 73 20 6f 66 20 61 /*.** Lists of a
ce6c0 6c 6c 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 ll unixLockInfo
ce6d0 61 6e 64 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 and unixOpenCnt
ce6e0 6f 62 6a 65 63 74 73 2e 20 20 54 68 65 73 65 20 objects. These
ce6f0 75 73 65 64 20 74 6f 20 62 65 20 68 61 73 68 0a used to be hash.
ce700 2a 2a 20 74 61 62 6c 65 73 2e 20 20 42 75 74 20 ** tables. But
ce710 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6f 62 the number of ob
ce720 6a 65 63 74 73 20 69 73 20 72 61 72 65 6c 79 20 jects is rarely
ce730 6d 6f 72 65 20 74 68 61 6e 20 61 20 64 6f 7a 65 more than a doze
ce740 6e 20 61 6e 64 0a 2a 2a 20 6e 65 76 65 72 20 65 n and.** never e
ce750 78 63 65 65 64 73 20 61 20 66 65 77 20 74 68 6f xceeds a few tho
ce760 75 73 61 6e 64 2e 20 20 41 6e 64 20 6c 6f 6f 6b usand. And look
ce770 75 70 20 69 73 20 6e 6f 74 20 6f 6e 20 61 20 63 up is not on a c
ce780 72 69 74 69 63 61 6c 0a 2a 2a 20 70 61 74 68 20 ritical.** path
ce790 73 6f 20 61 20 73 69 6d 70 6c 65 20 6c 69 6e 6b so a simple link
ce7a0 65 64 20 6c 69 73 74 20 77 69 6c 6c 20 73 75 66 ed list will suf
ce7b0 66 69 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 fice..*/.static
ce7c0 73 74 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b 49 struct unixLockI
ce7d0 6e 66 6f 20 2a 6c 6f 63 6b 4c 69 73 74 20 3d 20 nfo *lockList =
ce7e0 30 3b 0a 73 74 61 74 69 63 20 73 74 72 75 63 74 0;.static struct
ce7f0 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 2a 6f 70 unixOpenCnt *op
ce800 65 6e 4c 69 73 74 20 3d 20 30 3b 0a 0a 2f 2a 0a enList = 0;../*.
ce810 2a 2a 20 54 68 69 73 20 76 61 72 69 61 62 6c 65 ** This variable
ce820 20 72 65 6d 65 6d 62 65 72 73 20 77 68 65 74 68 remembers wheth
ce830 65 72 20 6f 72 20 6e 6f 74 20 74 68 72 65 61 64 er or not thread
ce840 73 20 63 61 6e 20 6f 76 65 72 72 69 64 65 20 65 s can override e
ce850 61 63 68 20 6f 74 68 65 72 73 0a 2a 2a 20 6c 6f ach others.** lo
ce860 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 30 3a cks..**.** 0:
ce870 20 20 4e 6f 2e 20 20 54 68 72 65 61 64 73 20 63 No. Threads c
ce880 61 6e 6e 6f 74 20 6f 76 65 72 72 69 64 65 20 65 annot override e
ce890 61 63 68 20 6f 74 68 65 72 73 20 6c 6f 63 6b 73 ach others locks
ce8a0 2e 20 20 28 4c 69 6e 75 78 54 68 72 65 61 64 73 . (LinuxThreads
ce8b0 29 0a 2a 2a 20 20 20 20 31 3a 20 20 59 65 73 2e ).** 1: Yes.
ce8c0 20 20 54 68 72 65 61 64 73 20 63 61 6e 20 6f 76 Threads can ov
ce8d0 65 72 72 69 64 65 20 65 61 63 68 20 6f 74 68 65 erride each othe
ce8e0 72 73 20 6c 6f 63 6b 73 2e 20 20 28 50 6f 73 69 rs locks. (Posi
ce8f0 78 20 26 20 4e 4c 50 54 29 0a 2a 2a 20 20 20 2d x & NLPT).** -
ce900 31 3a 20 20 57 65 20 64 6f 6e 27 74 20 6b 6e 6f 1: We don't kno
ce910 77 20 79 65 74 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 w yet..**.** On
ce920 73 6f 6d 65 20 73 79 73 74 65 6d 73 2c 20 77 65 some systems, we
ce930 20 6b 6e 6f 77 20 61 74 20 63 6f 6d 70 69 6c 65 know at compile
ce940 2d 74 69 6d 65 20 69 66 20 74 68 72 65 61 64 73 -time if threads
ce950 20 63 61 6e 20 6f 76 65 72 72 69 64 65 20 65 61 can override ea
ce960 63 68 0a 2a 2a 20 6f 74 68 65 72 73 20 6c 6f 63 ch.** others loc
ce970 6b 73 2e 20 20 4f 6e 20 74 68 6f 73 65 20 73 79 ks. On those sy
ce980 73 74 65 6d 73 2c 20 74 68 65 20 53 51 4c 49 54 stems, the SQLIT
ce990 45 5f 54 48 52 45 41 44 5f 4f 56 45 52 52 49 44 E_THREAD_OVERRID
ce9a0 45 5f 4c 4f 43 4b 20 6d 61 63 72 6f 0a 2a 2a 20 E_LOCK macro.**
ce9b0 77 69 6c 6c 20 62 65 20 73 65 74 20 61 70 70 72 will be set appr
ce9c0 6f 70 72 69 61 74 65 6c 79 2e 20 20 4f 6e 20 6f opriately. On o
ce9d0 74 68 65 72 20 73 79 73 74 65 6d 73 2c 20 77 65 ther systems, we
ce9e0 20 68 61 76 65 20 74 6f 20 63 68 65 63 6b 20 61 have to check a
ce9f0 74 0a 2a 2a 20 72 75 6e 74 69 6d 65 2e 20 20 4f t.** runtime. O
cea00 6e 20 74 68 65 73 65 20 6c 61 74 74 65 72 20 73 n these latter s
cea10 79 73 74 65 6d 73 2c 20 53 51 4c 54 49 45 5f 54 ystems, SQLTIE_T
cea20 48 52 45 41 44 5f 4f 56 45 52 52 49 44 45 5f 4c HREAD_OVERRIDE_L
cea30 4f 43 4b 20 69 73 0a 2a 2a 20 75 6e 64 65 66 69 OCK is.** undefi
cea40 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ned..**.** This
cea50 76 61 72 69 61 62 6c 65 20 6e 6f 72 6d 61 6c 6c variable normall
cea60 79 20 68 61 73 20 66 69 6c 65 20 73 63 6f 70 65 y has file scope
cea70 20 6f 6e 6c 79 2e 20 20 42 75 74 20 64 75 72 69 only. But duri
cea80 6e 67 20 74 65 73 74 69 6e 67 2c 20 77 65 20 6d ng testing, we m
cea90 61 6b 65 0a 2a 2a 20 69 74 20 61 20 67 6c 6f 62 ake.** it a glob
ceaa0 61 6c 20 73 6f 20 74 68 61 74 20 74 68 65 20 74 al so that the t
ceab0 65 73 74 20 63 6f 64 65 20 63 61 6e 20 63 68 61 est code can cha
ceac0 6e 67 65 20 69 74 73 20 76 61 6c 75 65 20 69 6e nge its value in
cead0 20 6f 72 64 65 72 20 74 6f 20 76 65 72 69 66 79 order to verify
ceae0 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 72 69 67 .** that the rig
ceaf0 68 74 20 73 74 75 66 66 20 68 61 70 70 65 6e 73 ht stuff happens
ceb00 20 69 6e 20 65 69 74 68 65 72 20 63 61 73 65 2e in either case.
ceb10 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 .*/.#if SQLITE_T
ceb20 48 52 45 41 44 53 41 46 45 20 26 26 20 64 65 66 HREADSAFE && def
ceb30 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 0a ined(__linux__).
ceb40 23 20 20 69 66 6e 64 65 66 20 53 51 4c 49 54 45 # ifndef SQLITE
ceb50 5f 54 48 52 45 41 44 5f 4f 56 45 52 52 49 44 45 _THREAD_OVERRIDE
ceb60 5f 4c 4f 43 4b 0a 23 20 20 20 20 64 65 66 69 6e _LOCK.# defin
ceb70 65 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 5f e SQLITE_THREAD_
ceb80 4f 56 45 52 52 49 44 45 5f 4c 4f 43 4b 20 2d 31 OVERRIDE_LOCK -1
ceb90 0a 23 20 20 65 6e 64 69 66 0a 23 20 20 69 66 64 .# endif.# ifd
ceba0 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 69 ef SQLITE_TEST.i
cebb0 6e 74 20 74 68 72 65 61 64 73 4f 76 65 72 72 69 nt threadsOverri
cebc0 64 65 45 61 63 68 4f 74 68 65 72 73 4c 6f 63 6b deEachOthersLock
cebd0 73 20 3d 20 53 51 4c 49 54 45 5f 54 48 52 45 41 s = SQLITE_THREA
cebe0 44 5f 4f 56 45 52 52 49 44 45 5f 4c 4f 43 4b 3b D_OVERRIDE_LOCK;
cebf0 0a 23 20 20 65 6c 73 65 0a 73 74 61 74 69 63 20 .# else.static
cec00 69 6e 74 20 74 68 72 65 61 64 73 4f 76 65 72 72 int threadsOverr
cec10 69 64 65 45 61 63 68 4f 74 68 65 72 73 4c 6f 63 ideEachOthersLoc
cec20 6b 73 20 3d 20 53 51 4c 49 54 45 5f 54 48 52 45 ks = SQLITE_THRE
cec30 41 44 5f 4f 56 45 52 52 49 44 45 5f 4c 4f 43 4b AD_OVERRIDE_LOCK
cec40 3b 0a 23 20 20 65 6e 64 69 66 0a 23 65 6e 64 69 ;.# endif.#endi
cec50 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 73 74 f../*.** This st
cec60 72 75 63 74 75 72 65 20 68 6f 6c 64 73 20 69 6e ructure holds in
cec70 66 6f 72 6d 61 74 69 6f 6e 20 70 61 73 73 65 64 formation passed
cec80 20 69 6e 74 6f 20 69 6e 64 69 76 69 64 75 61 6c into individual
cec90 20 74 65 73 74 0a 2a 2a 20 74 68 72 65 61 64 73 test.** threads
ceca0 20 62 79 20 74 68 65 20 74 65 73 74 54 68 72 65 by the testThre
cecb0 61 64 4c 6f 63 6b 69 6e 67 42 65 68 61 76 69 6f adLockingBehavio
cecc0 72 28 29 20 72 6f 75 74 69 6e 65 2e 0a 2a 2f 0a r() routine..*/.
cecd0 73 74 72 75 63 74 20 74 68 72 65 61 64 54 65 73 struct threadTes
cece0 74 44 61 74 61 20 7b 0a 20 20 69 6e 74 20 66 64 tData {. int fd
cecf0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
ced00 20 2f 2a 20 46 69 6c 65 20 74 6f 20 62 65 20 6c /* File to be l
ced10 6f 63 6b 65 64 20 2a 2f 0a 20 20 73 74 72 75 63 ocked */. struc
ced20 74 20 66 6c 6f 63 6b 20 6c 6f 63 6b 3b 20 20 20 t flock lock;
ced30 20 20 2f 2a 20 54 68 65 20 6c 6f 63 6b 69 6e 67 /* The locking
ced40 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a 20 20 operation */.
ced50 69 6e 74 20 72 65 73 75 6c 74 3b 20 20 20 20 20 int result;
ced60 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 /* Result
ced70 20 6f 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 of the locking
ced80 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a 7d 3b 0a operation */.};.
ced90 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 .#if SQLITE_THRE
ceda0 41 44 53 41 46 45 20 26 26 20 64 65 66 69 6e 65 ADSAFE && define
cedb0 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 0a 2f 2a 0a d(__linux__)./*.
cedc0 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
cedd0 20 69 73 20 75 73 65 64 20 61 73 20 74 68 65 20 is used as the
cede0 6d 61 69 6e 20 72 6f 75 74 69 6e 65 20 66 6f 72 main routine for
cedf0 20 61 20 74 68 72 65 61 64 20 6c 61 75 6e 63 68 a thread launch
cee00 65 64 20 62 79 0a 2a 2a 20 74 65 73 74 54 68 72 ed by.** testThr
cee10 65 61 64 4c 6f 63 6b 69 6e 67 42 65 68 61 76 69 eadLockingBehavi
cee20 6f 72 28 29 2e 20 49 74 20 74 65 73 74 73 20 77 or(). It tests w
cee30 68 65 74 68 65 72 20 74 68 65 20 73 68 61 72 65 hether the share
cee40 64 2d 6c 6f 63 6b 20 6f 62 74 61 69 6e 65 64 0a d-lock obtained.
cee50 2a 2a 20 62 79 20 74 68 65 20 6d 61 69 6e 20 74 ** by the main t
cee60 68 72 65 61 64 20 69 6e 20 74 65 73 74 54 68 72 hread in testThr
cee70 65 61 64 4c 6f 63 6b 69 6e 67 42 65 68 61 76 69 eadLockingBehavi
cee80 6f 72 28 29 20 63 6f 6e 66 6c 69 63 74 73 20 77 or() conflicts w
cee90 69 74 68 20 61 0a 2a 2a 20 68 79 70 6f 74 68 65 ith a.** hypothe
ceea0 74 69 63 61 6c 20 77 72 69 74 65 2d 6c 6f 63 6b tical write-lock
ceeb0 20 6f 62 74 61 69 6e 65 64 20 62 79 20 74 68 69 obtained by thi
ceec0 73 20 74 68 72 65 61 64 20 6f 6e 20 74 68 65 20 s thread on the
ceed0 73 61 6d 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a same file..**.**
ceee0 20 54 68 65 20 77 72 69 74 65 2d 6c 6f 63 6b 20 The write-lock
ceef0 69 73 20 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20 is not actually
cef00 61 63 71 75 69 72 65 64 2c 20 61 73 20 74 68 69 acquired, as thi
cef10 73 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c s is not possibl
cef20 65 20 69 66 20 0a 2a 2a 20 74 68 65 20 66 69 6c e if .** the fil
cef30 65 20 69 73 20 6f 70 65 6e 20 69 6e 20 72 65 61 e is open in rea
cef40 64 2d 6f 6e 6c 79 20 6d 6f 64 65 20 28 73 65 65 d-only mode (see
cef50 20 74 69 63 6b 65 74 20 23 33 34 37 32 29 2e 0a ticket #3472)..
cef60 2a 2f 20 0a 73 74 61 74 69 63 20 76 6f 69 64 20 */ .static void
cef70 2a 74 68 72 65 61 64 4c 6f 63 6b 69 6e 67 54 65 *threadLockingTe
cef80 73 74 28 76 6f 69 64 20 2a 70 41 72 67 29 7b 0a st(void *pArg){.
cef90 20 20 73 74 72 75 63 74 20 74 68 72 65 61 64 54 struct threadT
cefa0 65 73 74 44 61 74 61 20 2a 70 44 61 74 61 20 3d estData *pData =
cefb0 20 28 73 74 72 75 63 74 20 74 68 72 65 61 64 54 (struct threadT
cefc0 65 73 74 44 61 74 61 2a 29 70 41 72 67 3b 0a 20 estData*)pArg;.
cefd0 20 70 44 61 74 61 2d 3e 72 65 73 75 6c 74 20 3d pData->result =
cefe0 20 66 63 6e 74 6c 28 70 44 61 74 61 2d 3e 66 64 fcntl(pData->fd
ceff0 2c 20 46 5f 47 45 54 4c 4b 2c 20 26 70 44 61 74 , F_GETLK, &pDat
cf000 61 2d 3e 6c 6f 63 6b 29 3b 0a 20 20 72 65 74 75 a->lock);. retu
cf010 72 6e 20 70 41 72 67 3b 0a 7d 0a 23 65 6e 64 69 rn pArg;.}.#endi
cf020 66 20 2f 2a 20 53 51 4c 49 54 45 5f 54 48 52 45 f /* SQLITE_THRE
cf030 41 44 53 41 46 45 20 26 26 20 64 65 66 69 6e 65 ADSAFE && define
cf040 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 20 2a 2f 0a d(__linux__) */.
cf050 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 ..#if SQLITE_THR
cf060 45 41 44 53 41 46 45 20 26 26 20 64 65 66 69 6e EADSAFE && defin
cf070 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 0a 2f 2a ed(__linux__)./*
cf080 0a 2a 2a 20 54 68 69 73 20 70 72 6f 63 65 64 75 .** This procedu
cf090 72 65 20 61 74 74 65 6d 70 74 73 20 74 6f 20 64 re attempts to d
cf0a0 65 74 65 72 6d 69 6e 65 20 77 68 65 74 68 65 72 etermine whether
cf0b0 20 6f 72 20 6e 6f 74 20 74 68 72 65 61 64 73 0a or not threads.
cf0c0 2a 2a 20 63 61 6e 20 6f 76 65 72 72 69 64 65 20 ** can override
cf0d0 65 61 63 68 20 6f 74 68 65 72 73 20 6c 6f 63 6b each others lock
cf0e0 73 20 74 68 65 6e 20 73 65 74 73 20 74 68 65 20 s then sets the
cf0f0 0a 2a 2a 20 74 68 72 65 61 64 73 4f 76 65 72 72 .** threadsOverr
cf100 69 64 65 45 61 63 68 4f 74 68 65 72 73 4c 6f 63 ideEachOthersLoc
cf110 6b 73 20 76 61 72 69 61 62 6c 65 20 61 70 70 72 ks variable appr
cf120 6f 70 72 69 61 74 65 6c 79 2e 0a 2a 2f 0a 73 74 opriately..*/.st
cf130 61 74 69 63 20 76 6f 69 64 20 74 65 73 74 54 68 atic void testTh
cf140 72 65 61 64 4c 6f 63 6b 69 6e 67 42 65 68 61 76 readLockingBehav
cf150 69 6f 72 28 69 6e 74 20 66 64 5f 6f 72 69 67 29 ior(int fd_orig)
cf160 7b 0a 20 20 69 6e 74 20 66 64 3b 0a 20 20 69 6e {. int fd;. in
cf170 74 20 72 63 3b 0a 20 20 73 74 72 75 63 74 20 74 t rc;. struct t
cf180 68 72 65 61 64 54 65 73 74 44 61 74 61 20 64 3b hreadTestData d;
cf190 0a 20 20 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 . struct flock
cf1a0 6c 3b 0a 20 20 70 74 68 72 65 61 64 5f 74 20 74 l;. pthread_t t
cf1b0 3b 0a 0a 20 20 66 64 20 3d 20 64 75 70 28 66 64 ;.. fd = dup(fd
cf1c0 5f 6f 72 69 67 29 3b 0a 20 20 69 66 28 20 66 64 _orig);. if( fd
cf1d0 3c 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 6d <0 ) return;. m
cf1e0 65 6d 73 65 74 28 26 6c 2c 20 30 2c 20 73 69 7a emset(&l, 0, siz
cf1f0 65 6f 66 28 6c 29 29 3b 0a 20 20 6c 2e 6c 5f 74 eof(l));. l.l_t
cf200 79 70 65 20 3d 20 46 5f 52 44 4c 43 4b 3b 0a 20 ype = F_RDLCK;.
cf210 20 6c 2e 6c 5f 6c 65 6e 20 3d 20 31 3b 0a 20 20 l.l_len = 1;.
cf220 6c 2e 6c 5f 73 74 61 72 74 20 3d 20 30 3b 0a 20 l.l_start = 0;.
cf230 20 6c 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45 l.l_whence = SE
cf240 45 4b 5f 53 45 54 3b 0a 20 20 72 63 20 3d 20 66 EK_SET;. rc = f
cf250 63 6e 74 6c 28 66 64 5f 6f 72 69 67 2c 20 46 5f cntl(fd_orig, F_
cf260 53 45 54 4c 4b 2c 20 26 6c 29 3b 0a 20 20 69 66 SETLK, &l);. if
cf270 28 20 72 63 21 3d 30 20 29 20 72 65 74 75 72 6e ( rc!=0 ) return
cf280 3b 0a 20 20 6d 65 6d 73 65 74 28 26 64 2c 20 30 ;. memset(&d, 0
cf290 2c 20 73 69 7a 65 6f 66 28 64 29 29 3b 0a 20 20 , sizeof(d));.
cf2a0 64 2e 66 64 20 3d 20 66 64 3b 0a 20 20 64 2e 6c d.fd = fd;. d.l
cf2b0 6f 63 6b 20 3d 20 6c 3b 0a 20 20 64 2e 6c 6f 63 ock = l;. d.loc
cf2c0 6b 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 57 52 4c k.l_type = F_WRL
cf2d0 43 4b 3b 0a 20 20 69 66 28 20 70 74 68 72 65 61 CK;. if( pthrea
cf2e0 64 5f 63 72 65 61 74 65 28 26 74 2c 20 30 2c 20 d_create(&t, 0,
cf2f0 74 68 72 65 61 64 4c 6f 63 6b 69 6e 67 54 65 73 threadLockingTes
cf300 74 2c 20 26 64 29 3d 3d 30 20 29 7b 0a 20 20 20 t, &d)==0 ){.
cf310 20 70 74 68 72 65 61 64 5f 6a 6f 69 6e 28 74 2c pthread_join(t,
cf320 20 30 29 3b 0a 20 20 7d 0a 20 20 63 6c 6f 73 65 0);. }. close
cf330 28 66 64 29 3b 0a 20 20 69 66 28 20 64 2e 72 65 (fd);. if( d.re
cf340 73 75 6c 74 21 3d 30 20 29 20 72 65 74 75 72 6e sult!=0 ) return
cf350 3b 0a 20 20 74 68 72 65 61 64 73 4f 76 65 72 72 ;. threadsOverr
cf360 69 64 65 45 61 63 68 4f 74 68 65 72 73 4c 6f 63 ideEachOthersLoc
cf370 6b 73 20 3d 20 28 64 2e 6c 6f 63 6b 2e 6c 5f 74 ks = (d.lock.l_t
cf380 79 70 65 3d 3d 46 5f 55 4e 4c 43 4b 29 3b 0a 7d ype==F_UNLCK);.}
cf390 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
cf3a0 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 20 E_THREADSAFE &&
cf3b0 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f defined(__linux_
cf3c0 5f 29 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c _) */../*.** Rel
cf3d0 65 61 73 65 20 61 20 75 6e 69 78 4c 6f 63 6b 49 ease a unixLockI
cf3e0 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 70 72 nfo structure pr
cf3f0 65 76 69 6f 75 73 6c 79 20 61 6c 6c 6f 63 61 74 eviously allocat
cf400 65 64 20 62 79 20 66 69 6e 64 4c 6f 63 6b 49 6e ed by findLockIn
cf410 66 6f 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 fo()..**.** The
cf420 6d 75 74 65 78 20 65 6e 74 65 72 65 64 20 75 73 mutex entered us
cf430 69 6e 67 20 74 68 65 20 75 6e 69 78 45 6e 74 65 ing the unixEnte
cf440 72 4d 75 74 65 78 28 29 20 66 75 6e 63 74 69 6f rMutex() functio
cf450 6e 20 6d 75 73 74 20 62 65 20 68 65 6c 64 0a 2a n must be held.*
cf460 2a 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 * when this func
cf470 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2e 0a tion is called..
cf480 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 */.static void r
cf490 65 6c 65 61 73 65 4c 6f 63 6b 49 6e 66 6f 28 73 eleaseLockInfo(s
cf4a0 74 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b 49 6e truct unixLockIn
cf4b0 66 6f 20 2a 70 4c 6f 63 6b 29 7b 0a 20 20 61 73 fo *pLock){. as
cf4c0 73 65 72 74 28 20 75 6e 69 78 4d 75 74 65 78 48 sert( unixMutexH
cf4d0 65 6c 64 28 29 20 29 3b 0a 20 20 69 66 28 20 70 eld() );. if( p
cf4e0 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 70 4c 6f 63 Lock ){. pLoc
cf4f0 6b 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 20 20 69 k->nRef--;. i
cf500 66 28 20 70 4c 6f 63 6b 2d 3e 6e 52 65 66 3d 3d f( pLock->nRef==
cf510 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 0 ){. if( p
cf520 4c 6f 63 6b 2d 3e 70 50 72 65 76 20 29 7b 0a 20 Lock->pPrev ){.
cf530 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 assert( p
cf540 4c 6f 63 6b 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 Lock->pPrev->pNe
cf550 78 74 3d 3d 70 4c 6f 63 6b 20 29 3b 0a 20 20 20 xt==pLock );.
cf560 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 70 50 72 65 pLock->pPre
cf570 76 2d 3e 70 4e 65 78 74 20 3d 20 70 4c 6f 63 6b v->pNext = pLock
cf580 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d ->pNext;. }
cf590 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61 73 else{. as
cf5a0 73 65 72 74 28 20 6c 6f 63 6b 4c 69 73 74 3d 3d sert( lockList==
cf5b0 70 4c 6f 63 6b 20 29 3b 0a 20 20 20 20 20 20 20 pLock );.
cf5c0 20 6c 6f 63 6b 4c 69 73 74 20 3d 20 70 4c 6f 63 lockList = pLoc
cf5d0 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 k->pNext;.
cf5e0 7d 0a 20 20 20 20 20 20 69 66 28 20 70 4c 6f 63 }. if( pLoc
cf5f0 6b 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20 20 k->pNext ){.
cf600 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 6f 63 assert( pLoc
cf610 6b 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76 3d k->pNext->pPrev=
cf620 3d 70 4c 6f 63 6b 20 29 3b 0a 20 20 20 20 20 20 =pLock );.
cf630 20 20 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 2d 3e pLock->pNext->
cf640 70 50 72 65 76 20 3d 20 70 4c 6f 63 6b 2d 3e 70 pPrev = pLock->p
cf650 50 72 65 76 3b 0a 20 20 20 20 20 20 7d 0a 20 20 Prev;. }.
cf660 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 sqlite3_free
cf670 28 70 4c 6f 63 6b 29 3b 0a 20 20 20 20 7d 0a 20 (pLock);. }.
cf680 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 }.}../*.** Rele
cf690 61 73 65 20 61 20 75 6e 69 78 4f 70 65 6e 43 6e ase a unixOpenCn
cf6a0 74 20 73 74 72 75 63 74 75 72 65 20 70 72 65 76 t structure prev
cf6b0 69 6f 75 73 6c 79 20 61 6c 6c 6f 63 61 74 65 64 iously allocated
cf6c0 20 62 79 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f by findLockInfo
cf6d0 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 75 ()..**.** The mu
cf6e0 74 65 78 20 65 6e 74 65 72 65 64 20 75 73 69 6e tex entered usin
cf6f0 67 20 74 68 65 20 75 6e 69 78 45 6e 74 65 72 4d g the unixEnterM
cf700 75 74 65 78 28 29 20 66 75 6e 63 74 69 6f 6e 20 utex() function
cf710 6d 75 73 74 20 62 65 20 68 65 6c 64 0a 2a 2a 20 must be held.**
cf720 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 when this functi
cf730 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f on is called..*/
cf740 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 6c .static void rel
cf750 65 61 73 65 4f 70 65 6e 43 6e 74 28 73 74 72 75 easeOpenCnt(stru
cf760 63 74 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 2a ct unixOpenCnt *
cf770 70 4f 70 65 6e 29 7b 0a 20 20 61 73 73 65 72 74 pOpen){. assert
cf780 28 20 75 6e 69 78 4d 75 74 65 78 48 65 6c 64 28 ( unixMutexHeld(
cf790 29 20 29 3b 0a 20 20 69 66 28 20 70 4f 70 65 6e ) );. if( pOpen
cf7a0 20 29 7b 0a 20 20 20 20 70 4f 70 65 6e 2d 3e 6e ){. pOpen->n
cf7b0 52 65 66 2d 2d 3b 0a 20 20 20 20 69 66 28 20 70 Ref--;. if( p
cf7c0 4f 70 65 6e 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b Open->nRef==0 ){
cf7d0 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70 65 6e . if( pOpen
cf7e0 2d 3e 70 50 72 65 76 20 29 7b 0a 20 20 20 20 20 ->pPrev ){.
cf7f0 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 65 6e assert( pOpen
cf800 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 3d 3d ->pPrev->pNext==
cf810 70 4f 70 65 6e 20 29 3b 0a 20 20 20 20 20 20 20 pOpen );.
cf820 20 70 4f 70 65 6e 2d 3e 70 50 72 65 76 2d 3e 70 pOpen->pPrev->p
cf830 4e 65 78 74 20 3d 20 70 4f 70 65 6e 2d 3e 70 4e Next = pOpen->pN
cf840 65 78 74 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 ext;. }else
cf850 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 {. assert
cf860 28 20 6f 70 65 6e 4c 69 73 74 3d 3d 70 4f 70 65 ( openList==pOpe
cf870 6e 20 29 3b 0a 20 20 20 20 20 20 20 20 6f 70 65 n );. ope
cf880 6e 4c 69 73 74 20 3d 20 70 4f 70 65 6e 2d 3e 70 nList = pOpen->p
cf890 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 Next;. }.
cf8a0 20 20 20 20 69 66 28 20 70 4f 70 65 6e 2d 3e 70 if( pOpen->p
cf8b0 4e 65 78 74 20 29 7b 0a 20 20 20 20 20 20 20 20 Next ){.
cf8c0 61 73 73 65 72 74 28 20 70 4f 70 65 6e 2d 3e 70 assert( pOpen->p
cf8d0 4e 65 78 74 2d 3e 70 50 72 65 76 3d 3d 70 4f 70 Next->pPrev==pOp
cf8e0 65 6e 20 29 3b 0a 20 20 20 20 20 20 20 20 70 4f en );. pO
cf8f0 70 65 6e 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 pen->pNext->pPre
cf900 76 20 3d 20 70 4f 70 65 6e 2d 3e 70 50 72 65 76 v = pOpen->pPrev
cf910 3b 0a 20 20 20 20 20 20 7d 0a 23 69 66 20 53 51 ;. }.#if SQ
cf920 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 LITE_THREADSAFE
cf930 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e && defined(__lin
cf940 75 78 5f 5f 29 0a 20 20 20 20 20 20 61 73 73 65 ux__). asse
cf950 72 74 28 20 21 70 4f 70 65 6e 2d 3e 70 55 6e 75 rt( !pOpen->pUnu
cf960 73 65 64 20 7c 7c 20 74 68 72 65 61 64 73 4f 76 sed || threadsOv
cf970 65 72 72 69 64 65 45 61 63 68 4f 74 68 65 72 73 errideEachOthers
cf980 4c 6f 63 6b 73 3d 3d 30 20 29 3b 0a 23 65 6e 64 Locks==0 );.#end
cf990 69 66 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 if.. /* If
cf9a0 70 4f 70 65 6e 2d 3e 70 55 6e 75 73 65 64 20 69 pOpen->pUnused i
cf9b0 73 20 6e 6f 74 20 6e 75 6c 6c 2c 20 74 68 65 6e s not null, then
cf9c0 20 6d 65 6d 6f 72 79 20 61 6e 64 20 66 69 6c 65 memory and file
cf9d0 2d 64 65 73 63 72 69 70 74 6f 72 73 0a 20 20 20 -descriptors.
cf9e0 20 20 20 2a 2a 20 61 72 65 20 6c 65 61 6b 65 64 ** are leaked
cf9f0 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 .. **.
cfa00 20 2a 2a 20 54 68 69 73 20 77 69 6c 6c 20 6f 6e ** This will on
cfa10 6c 79 20 68 61 70 70 65 6e 20 69 66 2c 20 75 6e ly happen if, un
cfa20 64 65 72 20 4c 69 6e 75 78 74 68 72 65 61 64 73 der Linuxthreads
cfa30 2c 20 74 68 65 20 75 73 65 72 20 68 61 73 20 6f , the user has o
cfa40 70 65 6e 65 64 0a 20 20 20 20 20 20 2a 2a 20 61 pened. ** a
cfa50 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 6e 20 transaction in
cfa60 6f 6e 65 20 74 68 72 65 61 64 2c 20 74 68 65 6e one thread, then
cfa70 20 61 74 74 65 6d 70 74 73 20 74 6f 20 63 6c 6f attempts to clo
cfa80 73 65 20 74 68 65 20 64 61 74 61 62 61 73 65 0a se the database.
cfa90 20 20 20 20 20 20 2a 2a 20 68 61 6e 64 6c 65 20 ** handle
cfaa0 66 72 6f 6d 20 61 6e 6f 74 68 65 72 20 74 68 72 from another thr
cfab0 65 61 64 20 28 77 69 74 68 6f 75 74 20 66 69 72 ead (without fir
cfac0 73 74 20 75 6e 6c 6f 63 6b 69 6e 67 20 74 68 65 st unlocking the
cfad0 20 64 62 20 66 69 6c 65 29 2e 0a 20 20 20 20 20 db file)..
cfae0 20 2a 2a 20 54 68 69 73 20 69 73 20 61 20 6d 69 ** This is a mi
cfaf0 73 75 73 65 2e 20 20 2a 2f 0a 20 20 20 20 20 20 suse. */.
cfb00 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4f 70 sqlite3_free(pOp
cfb10 65 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d en);. }. }.}
cfb20 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 20 ../*.** Given a
cfb30 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 2c file descriptor,
cfb40 20 6c 6f 63 61 74 65 20 75 6e 69 78 4c 6f 63 6b locate unixLock
cfb50 49 6e 66 6f 20 61 6e 64 20 75 6e 69 78 4f 70 65 Info and unixOpe
cfb60 6e 43 6e 74 20 73 74 72 75 63 74 75 72 65 73 20 nCnt structures
cfb70 74 68 61 74 0a 2a 2a 20 64 65 73 63 72 69 62 65 that.** describe
cfb80 73 20 74 68 61 74 20 66 69 6c 65 20 64 65 73 63 s that file desc
cfb90 72 69 70 74 6f 72 2e 20 20 43 72 65 61 74 65 20 riptor. Create
cfba0 6e 65 77 20 6f 6e 65 73 20 69 66 20 6e 65 63 65 new ones if nece
cfbb0 73 73 61 72 79 2e 20 20 54 68 65 0a 2a 2a 20 72 ssary. The.** r
cfbc0 65 74 75 72 6e 20 76 61 6c 75 65 73 20 6d 69 67 eturn values mig
cfbd0 68 74 20 62 65 20 75 6e 69 6e 69 74 69 61 6c 69 ht be uninitiali
cfbe0 7a 65 64 20 69 66 20 61 6e 20 65 72 72 6f 72 20 zed if an error
cfbf0 6f 63 63 75 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 occurs..**.** Th
cfc00 65 20 6d 75 74 65 78 20 65 6e 74 65 72 65 64 20 e mutex entered
cfc10 75 73 69 6e 67 20 74 68 65 20 75 6e 69 78 45 6e using the unixEn
cfc20 74 65 72 4d 75 74 65 78 28 29 20 66 75 6e 63 74 terMutex() funct
cfc30 69 6f 6e 20 6d 75 73 74 20 62 65 20 68 65 6c 64 ion must be held
cfc40 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73 20 66 75 .** when this fu
cfc50 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 nction is called
cfc60 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 ..**.** Return a
cfc70 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20 65 72 n appropriate er
cfc80 72 6f 72 20 63 6f 64 65 2e 0a 2a 2f 0a 73 74 61 ror code..*/.sta
cfc90 74 69 63 20 69 6e 74 20 66 69 6e 64 4c 6f 63 6b tic int findLock
cfca0 49 6e 66 6f 28 0a 20 20 75 6e 69 78 46 69 6c 65 Info(. unixFile
cfcb0 20 2a 70 46 69 6c 65 2c 20 20 20 20 20 20 20 20 *pFile,
cfcc0 20 20 20 20 20 20 20 2f 2a 20 55 6e 69 78 20 66 /* Unix f
cfcd0 69 6c 65 20 77 69 74 68 20 66 69 6c 65 20 64 65 ile with file de
cfce0 73 63 20 75 73 65 64 20 69 6e 20 74 68 65 20 6b sc used in the k
cfcf0 65 79 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 75 ey */. struct u
cfd00 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 2a 2a 70 70 nixLockInfo **pp
cfd10 4c 6f 63 6b 2c 20 20 2f 2a 20 52 65 74 75 72 6e Lock, /* Return
cfd20 20 74 68 65 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 the unixLockInf
cfd30 6f 20 73 74 72 75 63 74 75 72 65 20 68 65 72 65 o structure here
cfd40 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 75 6e 69 */. struct uni
cfd50 78 4f 70 65 6e 43 6e 74 20 2a 2a 70 70 4f 70 65 xOpenCnt **ppOpe
cfd60 6e 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 74 n /* Return t
cfd70 68 65 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 73 he unixOpenCnt s
cfd80 74 72 75 63 74 75 72 65 20 68 65 72 65 20 2a 2f tructure here */
cfd90 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 .){. int rc;
cfda0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
cfdb0 20 20 20 20 20 2f 2a 20 53 79 73 74 65 6d 20 63 /* System c
cfdc0 61 6c 6c 20 72 65 74 75 72 6e 20 63 6f 64 65 20 all return code
cfdd0 2a 2f 0a 20 20 69 6e 74 20 66 64 3b 20 20 20 20 */. int fd;
cfde0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
cfdf0 20 20 20 20 2f 2a 20 54 68 65 20 66 69 6c 65 20 /* The file
cfe00 64 65 73 63 72 69 70 74 6f 72 20 66 6f 72 20 70 descriptor for p
cfe10 46 69 6c 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 File */. struct
cfe20 20 75 6e 69 78 4c 6f 63 6b 4b 65 79 20 6c 6f 63 unixLockKey loc
cfe30 6b 4b 65 79 3b 20 20 20 20 2f 2a 20 4c 6f 6f 6b kKey; /* Look
cfe40 75 70 20 6b 65 79 20 66 6f 72 20 74 68 65 20 75 up key for the u
cfe50 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 73 74 72 75 nixLockInfo stru
cfe60 63 74 75 72 65 20 2a 2f 0a 20 20 73 74 72 75 63 cture */. struc
cfe70 74 20 75 6e 69 78 46 69 6c 65 49 64 20 66 69 6c t unixFileId fil
cfe80 65 49 64 3b 20 20 20 20 20 20 2f 2a 20 4c 6f 6f eId; /* Loo
cfe90 6b 75 70 20 6b 65 79 20 66 6f 72 20 74 68 65 20 kup key for the
cfea0 75 6e 69 78 4f 70 65 6e 43 6e 74 20 73 74 72 75 unixOpenCnt stru
cfeb0 63 74 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 73 ct */. struct s
cfec0 74 61 74 20 73 74 61 74 62 75 66 3b 20 20 20 20 tat statbuf;
cfed0 20 20 20 20 20 20 20 2f 2a 20 4c 6f 77 2d 6c 65 /* Low-le
cfee0 76 65 6c 20 66 69 6c 65 20 69 6e 66 6f 72 6d 61 vel file informa
cfef0 74 69 6f 6e 20 2a 2f 0a 20 20 73 74 72 75 63 74 tion */. struct
cff00 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 2a 70 unixLockInfo *p
cff10 4c 6f 63 6b 20 3d 20 30 3b 2f 2a 20 43 61 6e 64 Lock = 0;/* Cand
cff20 69 64 61 74 65 20 75 6e 69 78 4c 6f 63 6b 49 6e idate unixLockIn
cff30 66 6f 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 73 fo object */. s
cff40 74 72 75 63 74 20 75 6e 69 78 4f 70 65 6e 43 6e truct unixOpenCn
cff50 74 20 2a 70 4f 70 65 6e 3b 20 20 20 20 20 2f 2a t *pOpen; /*
cff60 20 43 61 6e 64 69 64 61 74 65 20 75 6e 69 78 4f Candidate unixO
cff70 70 65 6e 43 6e 74 20 6f 62 6a 65 63 74 20 2a 2f penCnt object */
cff80 0a 0a 20 20 61 73 73 65 72 74 28 20 75 6e 69 78 .. assert( unix
cff90 4d 75 74 65 78 48 65 6c 64 28 29 20 29 3b 0a 0a MutexHeld() );..
cffa0 20 20 2f 2a 20 47 65 74 20 6c 6f 77 2d 6c 65 76 /* Get low-lev
cffb0 65 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 el information a
cffc0 62 6f 75 74 20 74 68 65 20 66 69 6c 65 20 74 68 bout the file th
cffd0 61 74 20 77 65 20 63 61 6e 20 75 73 65 64 20 74 at we can used t
cffe0 6f 0a 20 20 2a 2a 20 63 72 65 61 74 65 20 61 20 o. ** create a
cfff0 75 6e 69 71 75 65 20 6e 61 6d 65 20 66 6f 72 20 unique name for
d0000 74 68 65 20 66 69 6c 65 2e 0a 20 20 2a 2f 0a 20 the file.. */.
d0010 20 66 64 20 3d 20 70 46 69 6c 65 2d 3e 68 3b 0a fd = pFile->h;.
d0020 20 20 72 63 20 3d 20 66 73 74 61 74 28 66 64 2c rc = fstat(fd,
d0030 20 26 73 74 61 74 62 75 66 29 3b 0a 20 20 69 66 &statbuf);. if
d0040 28 20 72 63 21 3d 30 20 29 7b 0a 20 20 20 20 70 ( rc!=0 ){. p
d0050 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 File->lastErrno
d0060 3d 20 65 72 72 6e 6f 3b 0a 23 69 66 64 65 66 20 = errno;.#ifdef
d0070 45 4f 56 45 52 46 4c 4f 57 0a 20 20 20 20 69 66 EOVERFLOW. if
d0080 28 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 ( pFile->lastErr
d0090 6e 6f 3d 3d 45 4f 56 45 52 46 4c 4f 57 20 29 20 no==EOVERFLOW )
d00a0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f return SQLITE_NO
d00b0 4c 46 53 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 LFS;.#endif.
d00c0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f return SQLITE_IO
d00d0 45 52 52 3b 0a 20 20 7d 0a 0a 23 69 66 64 65 66 ERR;. }..#ifdef
d00e0 20 5f 5f 41 50 50 4c 45 5f 5f 0a 20 20 2f 2a 20 __APPLE__. /*
d00f0 4f 6e 20 4f 53 20 58 20 6f 6e 20 61 6e 20 6d 73 On OS X on an ms
d0100 64 6f 73 20 66 69 6c 65 73 79 73 74 65 6d 2c 20 dos filesystem,
d0110 74 68 65 20 69 6e 6f 64 65 20 6e 75 6d 62 65 72 the inode number
d0120 20 69 73 20 72 65 70 6f 72 74 65 64 0a 20 20 2a is reported. *
d0130 2a 20 69 6e 63 6f 72 72 65 63 74 6c 79 20 66 6f * incorrectly fo
d0140 72 20 7a 65 72 6f 2d 73 69 7a 65 20 66 69 6c 65 r zero-size file
d0150 73 2e 20 20 53 65 65 20 74 69 63 6b 65 74 20 23 s. See ticket #
d0160 33 32 36 30 2e 20 20 54 6f 20 77 6f 72 6b 0a 20 3260. To work.
d0170 20 2a 2a 20 61 72 6f 75 6e 64 20 74 68 69 73 20 ** around this
d0180 70 72 6f 62 6c 65 6d 20 28 77 65 20 63 6f 6e 73 problem (we cons
d0190 69 64 65 72 20 69 74 20 61 20 62 75 67 20 69 6e ider it a bug in
d01a0 20 4f 53 20 58 2c 20 6e 6f 74 20 53 51 4c 69 74 OS X, not SQLit
d01b0 65 29 0a 20 20 2a 2a 20 77 65 20 61 6c 77 61 79 e). ** we alway
d01c0 73 20 69 6e 63 72 65 61 73 65 20 74 68 65 20 66 s increase the f
d01d0 69 6c 65 20 73 69 7a 65 20 74 6f 20 31 20 62 79 ile size to 1 by
d01e0 20 77 72 69 74 69 6e 67 20 61 20 73 69 6e 67 6c writing a singl
d01f0 65 20 62 79 74 65 0a 20 20 2a 2a 20 70 72 69 6f e byte. ** prio
d0200 72 20 74 6f 20 61 63 63 65 73 73 69 6e 67 20 74 r to accessing t
d0210 68 65 20 69 6e 6f 64 65 20 6e 75 6d 62 65 72 2e he inode number.
d0220 20 20 54 68 65 20 6f 6e 65 20 62 79 74 65 20 77 The one byte w
d0230 72 69 74 74 65 6e 20 69 73 0a 20 20 2a 2a 20 61 ritten is. ** a
d0240 6e 20 41 53 43 49 49 20 27 53 27 20 63 68 61 72 n ASCII 'S' char
d0250 61 63 74 65 72 20 77 68 69 63 68 20 61 6c 73 6f acter which also
d0260 20 68 61 70 70 65 6e 73 20 74 6f 20 62 65 20 74 happens to be t
d0270 68 65 20 66 69 72 73 74 20 62 79 74 65 0a 20 20 he first byte.
d0280 2a 2a 20 69 6e 20 74 68 65 20 68 65 61 64 65 72 ** in the header
d0290 20 6f 66 20 65 76 65 72 79 20 53 51 4c 69 74 65 of every SQLite
d02a0 20 64 61 74 61 62 61 73 65 2e 20 20 49 6e 20 74 database. In t
d02b0 68 69 73 20 77 61 79 2c 20 69 66 20 74 68 65 72 his way, if ther
d02c0 65 0a 20 20 2a 2a 20 69 73 20 61 20 72 61 63 65 e. ** is a race
d02d0 20 63 6f 6e 64 69 74 69 6f 6e 20 73 75 63 68 20 condition such
d02e0 74 68 61 74 20 61 6e 6f 74 68 65 72 20 74 68 72 that another thr
d02f0 65 61 64 20 68 61 73 20 61 6c 72 65 61 64 79 20 ead has already
d0300 70 6f 70 75 6c 61 74 65 64 0a 20 20 2a 2a 20 74 populated. ** t
d0310 68 65 20 66 69 72 73 74 20 70 61 67 65 20 6f 66 he first page of
d0320 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20 6e the database, n
d0330 6f 20 64 61 6d 61 67 65 20 69 73 20 64 6f 6e 65 o damage is done
d0340 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 74 61 .. */. if( sta
d0350 74 62 75 66 2e 73 74 5f 73 69 7a 65 3d 3d 30 20 tbuf.st_size==0
d0360 29 7b 0a 20 20 20 20 72 63 20 3d 20 77 72 69 74 ){. rc = writ
d0370 65 28 66 64 2c 20 22 53 22 2c 20 31 29 3b 0a 20 e(fd, "S", 1);.
d0380 20 20 20 69 66 28 20 72 63 21 3d 31 20 29 7b 0a if( rc!=1 ){.
d0390 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
d03a0 49 54 45 5f 49 4f 45 52 52 3b 0a 20 20 20 20 7d ITE_IOERR;. }
d03b0 0a 20 20 20 20 72 63 20 3d 20 66 73 74 61 74 28 . rc = fstat(
d03c0 66 64 2c 20 26 73 74 61 74 62 75 66 29 3b 0a 20 fd, &statbuf);.
d03d0 20 20 20 69 66 28 20 72 63 21 3d 30 20 29 7b 0a if( rc!=0 ){.
d03e0 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 pFile->las
d03f0 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a tErrno = errno;.
d0400 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
d0410 49 54 45 5f 49 4f 45 52 52 3b 0a 20 20 20 20 7d ITE_IOERR;. }
d0420 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 6d . }.#endif.. m
d0430 65 6d 73 65 74 28 26 6c 6f 63 6b 4b 65 79 2c 20 emset(&lockKey,
d0440 30 2c 20 73 69 7a 65 6f 66 28 6c 6f 63 6b 4b 65 0, sizeof(lockKe
d0450 79 29 29 3b 0a 20 20 6c 6f 63 6b 4b 65 79 2e 66 y));. lockKey.f
d0460 69 64 2e 64 65 76 20 3d 20 73 74 61 74 62 75 66 id.dev = statbuf
d0470 2e 73 74 5f 64 65 76 3b 0a 23 69 66 20 4f 53 5f .st_dev;.#if OS_
d0480 56 58 57 4f 52 4b 53 0a 20 20 6c 6f 63 6b 4b 65 VXWORKS. lockKe
d0490 79 2e 66 69 64 2e 70 49 64 20 3d 20 70 46 69 6c y.fid.pId = pFil
d04a0 65 2d 3e 70 49 64 3b 0a 23 65 6c 73 65 0a 20 20 e->pId;.#else.
d04b0 6c 6f 63 6b 4b 65 79 2e 66 69 64 2e 69 6e 6f 20 lockKey.fid.ino
d04c0 3d 20 73 74 61 74 62 75 66 2e 73 74 5f 69 6e 6f = statbuf.st_ino
d04d0 3b 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c ;.#endif.#if SQL
d04e0 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 ITE_THREADSAFE &
d04f0 26 20 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 & defined(__linu
d0500 78 5f 5f 29 0a 20 20 69 66 28 20 74 68 72 65 61 x__). if( threa
d0510 64 73 4f 76 65 72 72 69 64 65 45 61 63 68 4f 74 dsOverrideEachOt
d0520 68 65 72 73 4c 6f 63 6b 73 3c 30 20 29 7b 0a 20 hersLocks<0 ){.
d0530 20 20 20 74 65 73 74 54 68 72 65 61 64 4c 6f 63 testThreadLoc
d0540 6b 69 6e 67 42 65 68 61 76 69 6f 72 28 66 64 29 kingBehavior(fd)
d0550 3b 0a 20 20 7d 0a 20 20 6c 6f 63 6b 4b 65 79 2e ;. }. lockKey.
d0560 74 69 64 20 3d 20 74 68 72 65 61 64 73 4f 76 65 tid = threadsOve
d0570 72 72 69 64 65 45 61 63 68 4f 74 68 65 72 73 4c rrideEachOthersL
d0580 6f 63 6b 73 20 3f 20 30 20 3a 20 70 74 68 72 65 ocks ? 0 : pthre
d0590 61 64 5f 73 65 6c 66 28 29 3b 0a 23 65 6e 64 69 ad_self();.#endi
d05a0 66 0a 20 20 66 69 6c 65 49 64 20 3d 20 6c 6f 63 f. fileId = loc
d05b0 6b 4b 65 79 2e 66 69 64 3b 0a 20 20 69 66 28 20 kKey.fid;. if(
d05c0 70 70 4c 6f 63 6b 21 3d 30 20 29 7b 0a 20 20 20 ppLock!=0 ){.
d05d0 20 70 4c 6f 63 6b 20 3d 20 6c 6f 63 6b 4c 69 73 pLock = lockLis
d05e0 74 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 70 4c t;. while( pL
d05f0 6f 63 6b 20 26 26 20 6d 65 6d 63 6d 70 28 26 6c ock && memcmp(&l
d0600 6f 63 6b 4b 65 79 2c 20 26 70 4c 6f 63 6b 2d 3e ockKey, &pLock->
d0610 6c 6f 63 6b 4b 65 79 2c 20 73 69 7a 65 6f 66 28 lockKey, sizeof(
d0620 6c 6f 63 6b 4b 65 79 29 29 20 29 7b 0a 20 20 20 lockKey)) ){.
d0630 20 20 20 70 4c 6f 63 6b 20 3d 20 70 4c 6f 63 6b pLock = pLock
d0640 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 ->pNext;. }.
d0650 20 20 20 69 66 28 20 70 4c 6f 63 6b 3d 3d 30 20 if( pLock==0
d0660 29 7b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 20 3d ){. pLock =
d0670 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 sqlite3_malloc(
d0680 20 73 69 7a 65 6f 66 28 2a 70 4c 6f 63 6b 29 20 sizeof(*pLock)
d0690 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4c 6f );. if( pLo
d06a0 63 6b 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 ck==0 ){.
d06b0 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d rc = SQLITE_NOM
d06c0 45 4d 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f EM;. goto
d06d0 20 65 78 69 74 5f 66 69 6e 64 6c 6f 63 6b 69 6e exit_findlockin
d06e0 66 6f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 fo;. }.
d06f0 20 20 6d 65 6d 63 70 79 28 26 70 4c 6f 63 6b 2d memcpy(&pLock-
d0700 3e 6c 6f 63 6b 4b 65 79 2c 26 6c 6f 63 6b 4b 65 >lockKey,&lockKe
d0710 79 2c 73 69 7a 65 6f 66 28 6c 6f 63 6b 4b 65 79 y,sizeof(lockKey
d0720 29 29 3b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 2d ));. pLock-
d0730 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20 20 20 20 >nRef = 1;.
d0740 20 70 4c 6f 63 6b 2d 3e 63 6e 74 20 3d 20 30 3b pLock->cnt = 0;
d0750 0a 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f . pLock->lo
d0760 63 6b 74 79 70 65 20 3d 20 30 3b 0a 20 20 20 20 cktype = 0;.
d0770 20 20 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 20 3d pLock->pNext =
d0780 20 6c 6f 63 6b 4c 69 73 74 3b 0a 20 20 20 20 20 lockList;.
d0790 20 70 4c 6f 63 6b 2d 3e 70 50 72 65 76 20 3d 20 pLock->pPrev =
d07a0 30 3b 0a 20 20 20 20 20 20 69 66 28 20 6c 6f 63 0;. if( loc
d07b0 6b 4c 69 73 74 20 29 20 6c 6f 63 6b 4c 69 73 74 kList ) lockList
d07c0 2d 3e 70 50 72 65 76 20 3d 20 70 4c 6f 63 6b 3b ->pPrev = pLock;
d07d0 0a 20 20 20 20 20 20 6c 6f 63 6b 4c 69 73 74 20 . lockList
d07e0 3d 20 70 4c 6f 63 6b 3b 0a 20 20 20 20 7d 65 6c = pLock;. }el
d07f0 73 65 7b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 2d se{. pLock-
d0800 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 7d 0a 20 >nRef++;. }.
d0810 20 20 20 2a 70 70 4c 6f 63 6b 20 3d 20 70 4c 6f *ppLock = pLo
d0820 63 6b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 70 ck;. }. if( pp
d0830 4f 70 65 6e 21 3d 30 20 29 7b 0a 20 20 20 20 70 Open!=0 ){. p
d0840 4f 70 65 6e 20 3d 20 6f 70 65 6e 4c 69 73 74 3b Open = openList;
d0850 0a 20 20 20 20 77 68 69 6c 65 28 20 70 4f 70 65 . while( pOpe
d0860 6e 20 26 26 20 6d 65 6d 63 6d 70 28 26 66 69 6c n && memcmp(&fil
d0870 65 49 64 2c 20 26 70 4f 70 65 6e 2d 3e 66 69 6c eId, &pOpen->fil
d0880 65 49 64 2c 20 73 69 7a 65 6f 66 28 66 69 6c 65 eId, sizeof(file
d0890 49 64 29 29 20 29 7b 0a 20 20 20 20 20 20 70 4f Id)) ){. pO
d08a0 70 65 6e 20 3d 20 70 4f 70 65 6e 2d 3e 70 4e 65 pen = pOpen->pNe
d08b0 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 xt;. }. if
d08c0 28 20 70 4f 70 65 6e 3d 3d 30 20 29 7b 0a 20 20 ( pOpen==0 ){.
d08d0 20 20 20 20 70 4f 70 65 6e 20 3d 20 73 71 6c 69 pOpen = sqli
d08e0 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 73 69 7a 65 te3_malloc( size
d08f0 6f 66 28 2a 70 4f 70 65 6e 29 20 29 3b 0a 20 20 of(*pOpen) );.
d0900 20 20 20 20 69 66 28 20 70 4f 70 65 6e 3d 3d 30 if( pOpen==0
d0910 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 6c 65 ){. rele
d0920 61 73 65 4c 6f 63 6b 49 6e 66 6f 28 70 4c 6f 63 aseLockInfo(pLoc
d0930 6b 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d k);. rc =
d0940 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
d0950 20 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74 goto exit
d0960 5f 66 69 6e 64 6c 6f 63 6b 69 6e 66 6f 3b 0a 20 _findlockinfo;.
d0970 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6d 65 6d }. mem
d0980 73 65 74 28 70 4f 70 65 6e 2c 20 30 2c 20 73 69 set(pOpen, 0, si
d0990 7a 65 6f 66 28 2a 70 4f 70 65 6e 29 29 3b 0a 20 zeof(*pOpen));.
d09a0 20 20 20 20 20 70 4f 70 65 6e 2d 3e 66 69 6c 65 pOpen->file
d09b0 49 64 20 3d 20 66 69 6c 65 49 64 3b 0a 20 20 20 Id = fileId;.
d09c0 20 20 20 70 4f 70 65 6e 2d 3e 6e 52 65 66 20 3d pOpen->nRef =
d09d0 20 31 3b 0a 20 20 20 20 20 20 70 4f 70 65 6e 2d 1;. pOpen-
d09e0 3e 70 4e 65 78 74 20 3d 20 6f 70 65 6e 4c 69 73 >pNext = openLis
d09f0 74 3b 0a 20 20 20 20 20 20 69 66 28 20 6f 70 65 t;. if( ope
d0a00 6e 4c 69 73 74 20 29 20 6f 70 65 6e 4c 69 73 74 nList ) openList
d0a10 2d 3e 70 50 72 65 76 20 3d 20 70 4f 70 65 6e 3b ->pPrev = pOpen;
d0a20 0a 20 20 20 20 20 20 6f 70 65 6e 4c 69 73 74 20 . openList
d0a30 3d 20 70 4f 70 65 6e 3b 0a 20 20 20 20 7d 65 6c = pOpen;. }el
d0a40 73 65 7b 0a 20 20 20 20 20 20 70 4f 70 65 6e 2d se{. pOpen-
d0a50 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 7d 0a 20 >nRef++;. }.
d0a60 20 20 20 2a 70 70 4f 70 65 6e 20 3d 20 70 4f 70 *ppOpen = pOp
d0a70 65 6e 3b 0a 20 20 7d 0a 0a 65 78 69 74 5f 66 69 en;. }..exit_fi
d0a80 6e 64 6c 6f 63 6b 69 6e 66 6f 3a 0a 20 20 72 65 ndlockinfo:. re
d0a90 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
d0aa0 2a 20 49 66 20 77 65 20 61 72 65 20 63 75 72 72 * If we are curr
d0ab0 65 6e 74 6c 79 20 69 6e 20 61 20 64 69 66 66 65 ently in a diffe
d0ac0 72 65 6e 74 20 74 68 72 65 61 64 20 74 68 61 6e rent thread than
d0ad0 20 74 68 65 20 74 68 72 65 61 64 20 74 68 61 74 the thread that
d0ae0 20 74 68 65 0a 2a 2a 20 75 6e 69 78 46 69 6c 65 the.** unixFile
d0af0 20 61 72 67 75 6d 65 6e 74 20 62 65 6c 6f 6e 67 argument belong
d0b00 73 20 74 6f 2c 20 74 68 65 6e 20 74 72 61 6e 73 s to, then trans
d0b10 66 65 72 20 6f 77 6e 65 72 73 68 69 70 20 6f 66 fer ownership of
d0b20 20 74 68 65 20 75 6e 69 78 46 69 6c 65 0a 2a 2a the unixFile.**
d0b30 20 6f 76 65 72 20 74 6f 20 74 68 65 20 63 75 72 over to the cur
d0b40 72 65 6e 74 20 74 68 72 65 61 64 2e 0a 2a 2a 0a rent thread..**.
d0b50 2a 2a 20 41 20 75 6e 69 78 46 69 6c 65 20 69 73 ** A unixFile is
d0b60 20 6f 6e 6c 79 20 6f 77 6e 65 64 20 62 79 20 61 only owned by a
d0b70 20 74 68 72 65 61 64 20 6f 6e 20 73 79 73 74 65 thread on syste
d0b80 6d 73 20 74 68 61 74 20 75 73 65 20 4c 69 6e 75 ms that use Linu
d0b90 78 54 68 72 65 61 64 73 2e 0a 2a 2a 0a 2a 2a 20 xThreads..**.**
d0ba0 4f 77 6e 65 72 73 68 69 70 20 74 72 61 6e 73 66 Ownership transf
d0bb0 65 72 20 69 73 20 6f 6e 6c 79 20 61 6c 6c 6f 77 er is only allow
d0bc0 65 64 20 69 66 20 74 68 65 20 75 6e 69 78 46 69 ed if the unixFi
d0bd0 6c 65 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 le is currently
d0be0 75 6e 6c 6f 63 6b 65 64 2e 0a 2a 2a 20 49 66 20 unlocked..** If
d0bf0 74 68 65 20 75 6e 69 78 46 69 6c 65 20 69 73 20 the unixFile is
d0c00 6c 6f 63 6b 65 64 20 61 6e 64 20 61 6e 20 6f 77 locked and an ow
d0c10 6e 65 72 73 68 69 70 20 69 73 20 77 72 6f 6e 67 nership is wrong
d0c20 2c 20 74 68 65 6e 20 72 65 74 75 72 6e 0a 2a 2a , then return.**
d0c30 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 2e 20 SQLITE_MISUSE.
d0c40 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 SQLITE_OK is re
d0c50 74 75 72 6e 65 64 20 69 66 20 65 76 65 72 79 74 turned if everyt
d0c60 68 69 6e 67 20 77 6f 72 6b 73 2e 0a 2a 2f 0a 23 hing works..*/.#
d0c70 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 if SQLITE_THREAD
d0c80 53 41 46 45 20 26 26 20 64 65 66 69 6e 65 64 28 SAFE && defined(
d0c90 5f 5f 6c 69 6e 75 78 5f 5f 29 0a 73 74 61 74 69 __linux__).stati
d0ca0 63 20 69 6e 74 20 74 72 61 6e 73 66 65 72 4f 77 c int transferOw
d0cb0 6e 65 72 73 68 69 70 28 75 6e 69 78 46 69 6c 65 nership(unixFile
d0cc0 20 2a 70 46 69 6c 65 29 7b 0a 20 20 69 6e 74 20 *pFile){. int
d0cd0 72 63 3b 0a 20 20 70 74 68 72 65 61 64 5f 74 20 rc;. pthread_t
d0ce0 68 53 65 6c 66 3b 0a 20 20 69 66 28 20 74 68 72 hSelf;. if( thr
d0cf0 65 61 64 73 4f 76 65 72 72 69 64 65 45 61 63 68 eadsOverrideEach
d0d00 4f 74 68 65 72 73 4c 6f 63 6b 73 20 29 7b 0a 20 OthersLocks ){.
d0d10 20 20 20 2f 2a 20 4f 77 6e 65 72 73 68 69 70 20 /* Ownership
d0d20 74 72 61 6e 73 66 65 72 73 20 6e 6f 74 20 6e 65 transfers not ne
d0d30 65 64 65 64 20 6f 6e 20 74 68 69 73 20 73 79 73 eded on this sys
d0d40 74 65 6d 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 tem */. retur
d0d50 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d n SQLITE_OK;. }
d0d60 0a 20 20 68 53 65 6c 66 20 3d 20 70 74 68 72 65 . hSelf = pthre
d0d70 61 64 5f 73 65 6c 66 28 29 3b 0a 20 20 69 66 28 ad_self();. if(
d0d80 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28 70 pthread_equal(p
d0d90 46 69 6c 65 2d 3e 74 69 64 2c 20 68 53 65 6c 66 File->tid, hSelf
d0da0 29 20 29 7b 0a 20 20 20 20 2f 2a 20 57 65 20 61 ) ){. /* We a
d0db0 72 65 20 73 74 69 6c 6c 20 69 6e 20 74 68 65 20 re still in the
d0dc0 73 61 6d 65 20 74 68 72 65 61 64 20 2a 2f 0a 20 same thread */.
d0dd0 20 20 20 4f 53 54 52 41 43 45 31 28 22 4e 6f 2d OSTRACE1("No-
d0de0 74 72 61 6e 73 66 65 72 2c 20 73 61 6d 65 20 74 transfer, same t
d0df0 68 72 65 61 64 5c 6e 22 29 3b 0a 20 20 20 20 72 hread\n");. r
d0e00 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
d0e10 0a 20 20 7d 0a 20 20 69 66 28 20 70 46 69 6c 65 . }. if( pFile
d0e20 2d 3e 6c 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c ->locktype!=NO_L
d0e30 4f 43 4b 20 29 7b 0a 20 20 20 20 2f 2a 20 57 65 OCK ){. /* We
d0e40 20 63 61 6e 6e 6f 74 20 63 68 61 6e 67 65 20 6f cannot change o
d0e50 77 6e 65 72 73 68 69 70 20 77 68 69 6c 65 20 77 wnership while w
d0e60 65 20 61 72 65 20 68 6f 6c 64 69 6e 67 20 61 20 e are holding a
d0e70 6c 6f 63 6b 21 20 2a 2f 0a 20 20 20 20 72 65 74 lock! */. ret
d0e80 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 urn SQLITE_MISUS
d0e90 45 3b 0a 20 20 7d 0a 20 20 4f 53 54 52 41 43 45 E;. }. OSTRACE
d0ea0 34 28 22 54 72 61 6e 73 66 65 72 20 6f 77 6e 65 4("Transfer owne
d0eb0 72 73 68 69 70 20 6f 66 20 25 64 20 66 72 6f 6d rship of %d from
d0ec0 20 25 64 20 74 6f 20 25 64 5c 6e 22 2c 0a 20 20 %d to %d\n",.
d0ed0 20 20 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d pFile-
d0ee0 3e 68 2c 20 70 46 69 6c 65 2d 3e 74 69 64 2c 20 >h, pFile->tid,
d0ef0 68 53 65 6c 66 29 3b 0a 20 20 70 46 69 6c 65 2d hSelf);. pFile-
d0f00 3e 74 69 64 20 3d 20 68 53 65 6c 66 3b 0a 20 20 >tid = hSelf;.
d0f10 69 66 20 28 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b if (pFile->pLock
d0f20 20 21 3d 20 4e 55 4c 4c 29 20 7b 0a 20 20 20 20 != NULL) {.
d0f30 72 65 6c 65 61 73 65 4c 6f 63 6b 49 6e 66 6f 28 releaseLockInfo(
d0f40 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 29 3b 0a 20 pFile->pLock);.
d0f50 20 20 20 72 63 20 3d 20 66 69 6e 64 4c 6f 63 6b rc = findLock
d0f60 49 6e 66 6f 28 70 46 69 6c 65 2c 20 26 70 46 69 Info(pFile, &pFi
d0f70 6c 65 2d 3e 70 4c 6f 63 6b 2c 20 30 29 3b 0a 20 le->pLock, 0);.
d0f80 20 20 20 4f 53 54 52 41 43 45 35 28 22 4c 4f 43 OSTRACE5("LOC
d0f90 4b 20 20 20 20 25 64 20 69 73 20 6e 6f 77 20 25 K %d is now %
d0fa0 73 28 25 73 2c 25 64 29 5c 6e 22 2c 20 70 46 69 s(%s,%d)\n", pFi
d0fb0 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 20 le->h,.
d0fc0 20 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 70 locktypeName(p
d0fd0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 29 2c File->locktype),
d0fe0 0a 20 20 20 20 20 20 20 20 20 20 20 6c 6f 63 6b . lock
d0ff0 74 79 70 65 4e 61 6d 65 28 70 46 69 6c 65 2d 3e typeName(pFile->
d1000 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 29 pLock->locktype)
d1010 2c 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e , pFile->pLock->
d1020 63 6e 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e cnt);. return
d1030 20 72 63 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a rc;. } else {.
d1040 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
d1050 45 5f 4f 4b 3b 0a 20 20 7d 0a 7d 0a 23 65 6c 73 E_OK;. }.}.#els
d1060 65 20 20 2f 2a 20 69 66 20 6e 6f 74 20 53 51 4c e /* if not SQL
d1070 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 2a ITE_THREADSAFE *
d1080 2f 0a 20 20 2f 2a 20 4f 6e 20 73 69 6e 67 6c 65 /. /* On single
d1090 2d 74 68 72 65 61 64 65 64 20 62 75 69 6c 64 73 -threaded builds
d10a0 2c 20 6f 77 6e 65 72 73 68 69 70 20 74 72 61 6e , ownership tran
d10b0 73 66 65 72 20 69 73 20 61 20 6e 6f 2d 6f 70 20 sfer is a no-op
d10c0 2a 2f 0a 23 20 64 65 66 69 6e 65 20 74 72 61 6e */.# define tran
d10d0 73 66 65 72 4f 77 6e 65 72 73 68 69 70 28 58 29 sferOwnership(X)
d10e0 20 53 51 4c 49 54 45 5f 4f 4b 0a 23 65 6e 64 69 SQLITE_OK.#endi
d10f0 66 20 2f 2a 20 53 51 4c 49 54 45 5f 54 48 52 45 f /* SQLITE_THRE
d1100 41 44 53 41 46 45 20 2a 2f 0a 0a 0a 2f 2a 0a 2a ADSAFE */.../*.*
d1110 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 * This routine c
d1120 68 65 63 6b 73 20 69 66 20 74 68 65 72 65 20 69 hecks if there i
d1130 73 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 s a RESERVED loc
d1140 6b 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 73 70 k held on the sp
d1150 65 63 69 66 69 65 64 0a 2a 2a 20 66 69 6c 65 20 ecified.** file
d1160 62 79 20 74 68 69 73 20 6f 72 20 61 6e 79 20 6f by this or any o
d1170 74 68 65 72 20 70 72 6f 63 65 73 73 2e 20 49 66 ther process. If
d1180 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 69 73 20 such a lock is
d1190 68 65 6c 64 2c 20 73 65 74 20 2a 70 52 65 73 4f held, set *pResO
d11a0 75 74 0a 2a 2a 20 74 6f 20 61 20 6e 6f 6e 2d 7a ut.** to a non-z
d11b0 65 72 6f 20 76 61 6c 75 65 20 6f 74 68 65 72 77 ero value otherw
d11c0 69 73 65 20 2a 70 52 65 73 4f 75 74 20 69 73 20 ise *pResOut is
d11d0 73 65 74 20 74 6f 20 7a 65 72 6f 2e 20 20 54 68 set to zero. Th
d11e0 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a e return value.*
d11f0 2a 20 69 73 20 73 65 74 20 74 6f 20 53 51 4c 49 * is set to SQLI
d1200 54 45 5f 4f 4b 20 75 6e 6c 65 73 73 20 61 6e 20 TE_OK unless an
d1210 49 2f 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 I/O error occurs
d1220 20 64 75 72 69 6e 67 20 6c 6f 63 6b 20 63 68 65 during lock che
d1230 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 cking..*/.static
d1240 20 69 6e 74 20 75 6e 69 78 43 68 65 63 6b 52 65 int unixCheckRe
d1250 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 servedLock(sqlit
d1260 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 e3_file *id, int
d1270 20 2a 70 52 65 73 4f 75 74 29 7b 0a 20 20 69 6e *pResOut){. in
d1280 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
d1290 3b 0a 20 20 69 6e 74 20 72 65 73 65 72 76 65 64 ;. int reserved
d12a0 20 3d 20 30 3b 0a 20 20 75 6e 69 78 46 69 6c 65 = 0;. unixFile
d12b0 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 *pFile = (unixF
d12c0 69 6c 65 2a 29 69 64 3b 0a 0a 20 20 53 69 6d 75 ile*)id;.. Simu
d12d0 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 lateIOError( ret
d12e0 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 urn SQLITE_IOERR
d12f0 5f 43 48 45 43 4b 52 45 53 45 52 56 45 44 4c 4f _CHECKRESERVEDLO
d1300 43 4b 3b 20 29 3b 0a 0a 20 20 61 73 73 65 72 74 CK; );.. assert
d1310 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 75 6e 69 ( pFile );. uni
d1320 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 20 2f xEnterMutex(); /
d1330 2a 20 42 65 63 61 75 73 65 20 70 46 69 6c 65 2d * Because pFile-
d1340 3e 70 4c 6f 63 6b 20 69 73 20 73 68 61 72 65 64 >pLock is shared
d1350 20 61 63 72 6f 73 73 20 74 68 72 65 61 64 73 20 across threads
d1360 2a 2f 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69 */.. /* Check i
d1370 66 20 61 20 74 68 72 65 61 64 20 69 6e 20 74 68 f a thread in th
d1380 69 73 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 is process holds
d1390 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 2a 2f 0a such a lock */.
d13a0 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 70 4c 6f if( pFile->pLo
d13b0 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 3e 53 48 41 ck->locktype>SHA
d13c0 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 RED_LOCK ){.
d13d0 72 65 73 65 72 76 65 64 20 3d 20 31 3b 0a 20 20 reserved = 1;.
d13e0 7d 0a 0a 20 20 2f 2a 20 4f 74 68 65 72 77 69 73 }.. /* Otherwis
d13f0 65 20 73 65 65 20 69 66 20 73 6f 6d 65 20 6f 74 e see if some ot
d1400 68 65 72 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 her process hold
d1410 73 20 69 74 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64 s it.. */.#ifnd
d1420 65 66 20 5f 5f 44 4a 47 50 50 5f 5f 0a 20 20 69 ef __DJGPP__. i
d1430 66 28 20 21 72 65 73 65 72 76 65 64 20 29 7b 0a f( !reserved ){.
d1440 20 20 20 20 73 74 72 75 63 74 20 66 6c 6f 63 6b struct flock
d1450 20 6c 6f 63 6b 3b 0a 20 20 20 20 6c 6f 63 6b 2e lock;. lock.
d1460 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f l_whence = SEEK_
d1470 53 45 54 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f SET;. lock.l_
d1480 73 74 61 72 74 20 3d 20 52 45 53 45 52 56 45 44 start = RESERVED
d1490 5f 42 59 54 45 3b 0a 20 20 20 20 6c 6f 63 6b 2e _BYTE;. lock.
d14a0 6c 5f 6c 65 6e 20 3d 20 31 3b 0a 20 20 20 20 6c l_len = 1;. l
d14b0 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 57 ock.l_type = F_W
d14c0 52 4c 43 4b 3b 0a 20 20 20 20 69 66 20 28 2d 31 RLCK;. if (-1
d14d0 20 3d 3d 20 66 63 6e 74 6c 28 70 46 69 6c 65 2d == fcntl(pFile-
d14e0 3e 68 2c 20 46 5f 47 45 54 4c 4b 2c 20 26 6c 6f >h, F_GETLK, &lo
d14f0 63 6b 29 29 20 7b 0a 20 20 20 20 20 20 69 6e 74 ck)) {. int
d1500 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b tErrno = errno;
d1510 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
d1520 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 teErrorFromPosix
d1530 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 Error(tErrno, SQ
d1540 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b LITE_IOERR_CHECK
d1550 52 45 53 45 52 56 45 44 4c 4f 43 4b 29 3b 0a 20 RESERVEDLOCK);.
d1560 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 pFile->last
d1570 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a Errno = tErrno;.
d1580 20 20 20 20 7d 20 65 6c 73 65 20 69 66 28 20 6c } else if( l
d1590 6f 63 6b 2e 6c 5f 74 79 70 65 21 3d 46 5f 55 4e ock.l_type!=F_UN
d15a0 4c 43 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 73 LCK ){. res
d15b0 65 72 76 65 64 20 3d 20 31 3b 0a 20 20 20 20 7d erved = 1;. }
d15c0 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 0a 20 . }.#endif. .
d15d0 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 unixLeaveMutex(
d15e0 29 3b 0a 20 20 4f 53 54 52 41 43 45 34 28 22 54 );. OSTRACE4("T
d15f0 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 EST WR-LOCK %d %
d1600 64 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e d %d\n", pFile->
d1610 68 2c 20 72 63 2c 20 72 65 73 65 72 76 65 64 29 h, rc, reserved)
d1620 3b 0a 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d 20 ;.. *pResOut =
d1630 72 65 73 65 72 76 65 64 3b 0a 20 20 72 65 74 75 reserved;. retu
d1640 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
d1650 50 65 72 66 6f 72 6d 20 61 20 66 69 6c 65 20 6c Perform a file l
d1660 6f 63 6b 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e ocking operation
d1670 20 6f 6e 20 61 20 72 61 6e 67 65 20 6f 66 20 62 on a range of b
d1680 79 74 65 73 20 69 6e 20 61 20 66 69 6c 65 2e 0a ytes in a file..
d1690 2a 2a 20 54 68 65 20 22 6f 70 22 20 70 61 72 61 ** The "op" para
d16a0 6d 65 74 65 72 20 73 68 6f 75 6c 64 20 62 65 20 meter should be
d16b0 6f 6e 65 20 6f 66 20 46 5f 52 44 4c 43 4b 2c 20 one of F_RDLCK,
d16c0 46 5f 57 52 4c 43 4b 2c 20 6f 72 20 46 5f 55 4e F_WRLCK, or F_UN
d16d0 4c 43 4b 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 30 LCK..** Return 0
d16e0 20 6f 6e 20 73 75 63 63 65 73 73 20 6f 72 20 2d on success or -
d16f0 31 20 66 6f 72 20 66 61 69 6c 75 72 65 2e 20 20 1 for failure.
d1700 4f 6e 20 66 61 69 6c 75 72 65 2c 20 77 72 69 74 On failure, writ
d1710 65 20 74 68 65 20 65 72 72 6f 72 0a 2a 2a 20 63 e the error.** c
d1720 6f 64 65 20 69 6e 74 6f 20 2a 70 45 72 72 63 6f ode into *pErrco
d1730 64 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 de..**.** If the
d1740 20 53 51 4c 49 54 45 5f 57 48 4f 4c 45 5f 46 49 SQLITE_WHOLE_FI
d1750 4c 45 5f 4c 4f 43 4b 49 4e 47 20 62 69 74 20 69 LE_LOCKING bit i
d1760 73 20 63 6c 65 61 72 2c 20 74 68 65 6e 20 6f 6e s clear, then on
d1770 6c 79 20 6c 6f 63 6b 0a 2a 2a 20 74 68 65 20 72 ly lock.** the r
d1780 61 6e 67 65 20 6f 66 20 62 79 74 65 73 20 6f 6e ange of bytes on
d1790 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 70 61 67 the locking pag
d17a0 65 20 62 65 74 77 65 65 6e 20 53 48 41 52 45 44 e between SHARED
d17b0 5f 46 49 52 53 54 20 61 6e 64 0a 2a 2a 20 53 48 _FIRST and.** SH
d17c0 41 52 45 44 5f 53 49 5a 45 2e 20 20 49 66 20 53 ARED_SIZE. If S
d17d0 51 4c 49 54 45 5f 57 48 4f 4c 45 5f 46 49 4c 45 QLITE_WHOLE_FILE
d17e0 5f 4c 4f 43 4b 49 4e 47 20 69 73 20 73 65 74 2c _LOCKING is set,
d17f0 20 74 68 65 6e 20 6c 6f 63 6b 20 61 6c 6c 0a 2a then lock all.*
d1800 2a 20 62 79 74 65 73 20 66 72 6f 6d 20 30 20 75 * bytes from 0 u
d1810 70 20 74 6f 20 62 75 74 20 6e 6f 74 20 69 6e 63 p to but not inc
d1820 6c 75 64 69 6e 67 20 50 45 4e 44 49 4e 47 5f 42 luding PENDING_B
d1830 59 54 45 2c 20 61 6e 64 20 61 6c 6c 20 62 79 74 YTE, and all byt
d1840 65 73 0a 2a 2a 20 74 68 61 74 20 66 6f 6c 6c 6f es.** that follo
d1850 77 20 53 48 41 52 45 44 5f 46 49 52 53 54 2e 0a w SHARED_FIRST..
d1860 2a 2a 0a 2a 2a 20 49 6e 20 6f 74 68 65 72 20 77 **.** In other w
d1870 6f 72 64 73 2c 20 6f 66 20 53 51 4c 49 54 45 5f ords, of SQLITE_
d1880 57 48 4f 4c 45 5f 46 49 4c 45 5f 4c 4f 43 4b 49 WHOLE_FILE_LOCKI
d1890 4e 47 20 69 66 20 66 61 6c 73 65 20 28 74 68 65 NG if false (the
d18a0 20 68 69 73 74 6f 72 69 63 61 6c 0a 2a 2a 20 64 historical.** d
d18b0 65 66 61 75 6c 74 20 63 61 73 65 29 20 74 68 65 efault case) the
d18c0 6e 20 6f 6e 6c 79 20 6c 6f 63 6b 20 61 20 73 6d n only lock a sm
d18d0 61 6c 6c 20 72 61 6e 67 65 20 6f 66 20 62 79 74 all range of byt
d18e0 65 73 20 66 72 6f 6d 20 53 48 41 52 45 44 5f 46 es from SHARED_F
d18f0 49 52 53 54 0a 2a 2a 20 74 68 72 6f 75 67 68 20 IRST.** through
d1900 53 48 41 52 45 44 5f 46 49 52 53 54 2b 53 48 41 SHARED_FIRST+SHA
d1910 52 45 44 5f 53 49 5a 45 2d 31 2e 20 20 42 75 74 RED_SIZE-1. But
d1920 20 69 66 20 53 51 4c 49 54 45 5f 57 48 4f 4c 45 if SQLITE_WHOLE
d1930 5f 46 49 4c 45 5f 4c 4f 43 4b 49 4e 47 20 69 73 _FILE_LOCKING is
d1940 0a 2a 2a 20 74 72 75 65 20 74 68 65 6e 20 6c 6f .** true then lo
d1950 63 6b 20 65 76 65 72 79 20 62 79 74 65 20 69 6e ck every byte in
d1960 20 74 68 65 20 66 69 6c 65 20 65 78 63 65 70 74 the file except
d1970 20 66 6f 72 20 50 45 4e 44 49 4e 47 5f 42 59 54 for PENDING_BYT
d1980 45 20 61 6e 64 0a 2a 2a 20 52 45 53 45 52 56 45 E and.** RESERVE
d1990 44 5f 42 59 54 45 2e 0a 2a 2a 0a 2a 2a 20 53 51 D_BYTE..**.** SQ
d19a0 4c 49 54 45 5f 57 48 4f 4c 45 5f 46 49 4c 45 5f LITE_WHOLE_FILE_
d19b0 4c 4f 43 4b 49 4e 47 3d 74 72 75 65 20 6f 76 65 LOCKING=true ove
d19c0 72 6c 61 70 73 20 53 51 4c 49 54 45 5f 57 48 4f rlaps SQLITE_WHO
d19d0 4c 45 5f 46 49 4c 45 5f 4c 4f 43 4b 49 4e 47 3d LE_FILE_LOCKING=
d19e0 66 61 6c 73 65 0a 2a 2a 20 61 6e 64 20 73 6f 20 false.** and so
d19f0 74 68 65 20 6c 6f 63 6b 69 6e 67 20 73 63 68 65 the locking sche
d1a00 6d 65 73 20 61 72 65 20 63 6f 6d 70 61 74 69 62 mes are compatib
d1a10 6c 65 2e 20 20 4f 6e 65 20 74 79 70 65 20 6f 66 le. One type of
d1a20 20 6c 6f 63 6b 20 77 69 6c 6c 0a 2a 2a 20 65 66 lock will.** ef
d1a30 66 65 63 74 69 76 65 6c 79 20 65 78 63 6c 75 64 fectively exclud
d1a40 65 20 74 68 65 20 6f 74 68 65 72 20 74 79 70 65 e the other type
d1a50 2e 20 20 54 68 65 20 72 65 61 73 6f 6e 20 66 6f . The reason fo
d1a60 72 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 53 r using the.** S
d1a70 51 4c 49 54 45 5f 57 48 4f 4c 45 5f 46 49 4c 45 QLITE_WHOLE_FILE
d1a80 5f 4c 4f 43 4b 49 4e 47 3d 74 72 75 65 20 69 73 _LOCKING=true is
d1a90 20 74 68 61 74 20 62 79 20 69 6e 64 69 63 61 74 that by indicat
d1aa0 69 6e 67 20 74 68 65 20 66 75 6c 6c 20 72 61 6e ing the full ran
d1ab0 67 65 0a 2a 2a 20 6f 66 20 62 79 74 65 73 20 74 ge.** of bytes t
d1ac0 6f 20 62 65 20 72 65 61 64 20 6f 72 20 77 72 69 o be read or wri
d1ad0 74 74 65 6e 2c 20 77 65 20 67 69 76 65 20 68 69 tten, we give hi
d1ae0 6e 74 73 20 74 6f 20 4e 46 53 20 74 6f 20 68 65 nts to NFS to he
d1af0 6c 70 20 69 74 0a 2a 2a 20 6d 61 69 6e 74 61 69 lp it.** maintai
d1b00 6e 20 63 61 63 68 65 20 63 6f 68 65 72 65 6e 63 n cache coherenc
d1b10 79 2e 20 20 4f 6e 20 74 68 65 20 6f 74 68 65 72 y. On the other
d1b20 20 68 61 6e 64 2c 20 77 68 6f 6c 65 20 66 69 6c hand, whole fil
d1b30 65 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 69 73 20 e locking.** is
d1b40 73 6c 6f 77 65 72 2c 20 73 6f 20 77 65 20 64 6f slower, so we do
d1b50 6e 27 74 20 77 61 6e 74 20 74 6f 20 75 73 65 20 n't want to use
d1b60 69 74 20 65 78 63 65 70 74 20 66 6f 72 20 4e 46 it except for NF
d1b70 53 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 S..*/.static int
d1b80 20 72 61 6e 67 65 4c 6f 63 6b 28 75 6e 69 78 46 rangeLock(unixF
d1b90 69 6c 65 20 2a 70 46 69 6c 65 2c 20 69 6e 74 20 ile *pFile, int
d1ba0 6f 70 2c 20 69 6e 74 20 2a 70 45 72 72 63 6f 64 op, int *pErrcod
d1bb0 65 29 7b 0a 20 20 73 74 72 75 63 74 20 66 6c 6f e){. struct flo
d1bc0 63 6b 20 6c 6f 63 6b 3b 0a 20 20 69 6e 74 20 72 ck lock;. int r
d1bd0 63 3b 0a 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 c;. lock.l_type
d1be0 20 3d 20 6f 70 3b 0a 20 20 6c 6f 63 6b 2e 6c 5f = op;. lock.l_
d1bf0 73 74 61 72 74 20 3d 20 53 48 41 52 45 44 5f 46 start = SHARED_F
d1c00 49 52 53 54 3b 0a 20 20 6c 6f 63 6b 2e 6c 5f 77 IRST;. lock.l_w
d1c10 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45 54 hence = SEEK_SET
d1c20 3b 0a 20 20 69 66 28 20 28 70 46 69 6c 65 2d 3e ;. if( (pFile->
d1c30 66 69 6c 65 46 6c 61 67 73 20 26 20 53 51 4c 49 fileFlags & SQLI
d1c40 54 45 5f 57 48 4f 4c 45 5f 46 49 4c 45 5f 4c 4f TE_WHOLE_FILE_LO
d1c50 43 4b 49 4e 47 29 3d 3d 30 20 29 7b 0a 20 20 20 CKING)==0 ){.
d1c60 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 53 48 lock.l_len = SH
d1c70 41 52 45 44 5f 53 49 5a 45 3b 0a 20 20 20 20 72 ARED_SIZE;. r
d1c80 63 20 3d 20 66 63 6e 74 6c 28 70 46 69 6c 65 2d c = fcntl(pFile-
d1c90 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f >h, F_SETLK, &lo
d1ca0 63 6b 29 3b 0a 20 20 20 20 2a 70 45 72 72 63 6f ck);. *pErrco
d1cb0 64 65 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 7d 65 de = errno;. }e
d1cc0 6c 73 65 7b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f lse{. lock.l_
d1cd0 6c 65 6e 20 3d 20 30 3b 0a 20 20 20 20 72 63 20 len = 0;. rc
d1ce0 3d 20 66 63 6e 74 6c 28 70 46 69 6c 65 2d 3e 68 = fcntl(pFile->h
d1cf0 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b , F_SETLK, &lock
d1d00 29 3b 0a 20 20 20 20 2a 70 45 72 72 63 6f 64 65 );. *pErrcode
d1d10 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 69 66 = errno;. if
d1d20 28 20 4e 45 56 45 52 28 6f 70 3d 3d 46 5f 55 4e ( NEVER(op==F_UN
d1d30 4c 43 4b 29 20 7c 7c 20 72 63 21 3d 28 2d 31 29 LCK) || rc!=(-1)
d1d40 20 29 7b 0a 20 20 20 20 20 20 6c 6f 63 6b 2e 6c ){. lock.l
d1d50 5f 73 74 61 72 74 20 3d 20 30 3b 0a 20 20 20 20 _start = 0;.
d1d60 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 50 lock.l_len = P
d1d70 45 4e 44 49 4e 47 5f 42 59 54 45 3b 0a 20 20 20 ENDING_BYTE;.
d1d80 20 20 20 72 63 20 3d 20 66 63 6e 74 6c 28 70 46 rc = fcntl(pF
d1d90 69 6c 65 2d 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c ile->h, F_SETLK,
d1da0 20 26 6c 6f 63 6b 29 3b 0a 20 20 20 20 20 20 69 &lock);. i
d1db0 66 28 20 41 4c 57 41 59 53 28 6f 70 21 3d 46 5f f( ALWAYS(op!=F_
d1dc0 55 4e 4c 43 4b 29 20 26 26 20 72 63 3d 3d 28 2d UNLCK) && rc==(-
d1dd0 31 29 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 70 1) ){. *p
d1de0 45 72 72 63 6f 64 65 20 3d 20 65 72 72 6e 6f 3b Errcode = errno;
d1df0 0a 20 20 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f . lock.l_
d1e00 74 79 70 65 20 3d 20 46 5f 55 4e 4c 43 4b 3b 0a type = F_UNLCK;.
d1e10 20 20 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 lock.l_s
d1e20 74 61 72 74 20 3d 20 53 48 41 52 45 44 5f 46 49 tart = SHARED_FI
d1e30 52 53 54 3b 0a 20 20 20 20 20 20 20 20 6c 6f 63 RST;. loc
d1e40 6b 2e 6c 5f 6c 65 6e 20 3d 20 30 3b 0a 20 20 20 k.l_len = 0;.
d1e50 20 20 20 20 20 66 63 6e 74 6c 28 70 46 69 6c 65 fcntl(pFile
d1e60 2d 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c ->h, F_SETLK, &l
d1e70 6f 63 6b 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 ock);. }.
d1e80 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e }. }. return
d1e90 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f rc;.}../*.** Lo
d1ea0 63 6b 20 74 68 65 20 66 69 6c 65 20 77 69 74 68 ck the file with
d1eb0 20 74 68 65 20 6c 6f 63 6b 20 73 70 65 63 69 66 the lock specif
d1ec0 69 65 64 20 62 79 20 70 61 72 61 6d 65 74 65 72 ied by parameter
d1ed0 20 6c 6f 63 6b 74 79 70 65 20 2d 20 6f 6e 65 0a locktype - one.
d1ee0 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 ** of the follow
d1ef0 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 ing:.**.** (
d1f00 31 29 20 53 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 1) SHARED_LOCK.*
d1f10 2a 20 20 20 20 20 28 32 29 20 52 45 53 45 52 56 * (2) RESERV
d1f20 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 ED_LOCK.** (
d1f30 33 29 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 3) PENDING_LOCK.
d1f40 2a 2a 20 20 20 20 20 28 34 29 20 45 58 43 4c 55 ** (4) EXCLU
d1f50 53 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 SIVE_LOCK.**.**
d1f60 53 6f 6d 65 74 69 6d 65 73 20 77 68 65 6e 20 72 Sometimes when r
d1f70 65 71 75 65 73 74 69 6e 67 20 6f 6e 65 20 6c 6f equesting one lo
d1f80 63 6b 20 73 74 61 74 65 2c 20 61 64 64 69 74 69 ck state, additi
d1f90 6f 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 74 65 73 onal lock states
d1fa0 0a 2a 2a 20 61 72 65 20 69 6e 73 65 72 74 65 64 .** are inserted
d1fb0 20 69 6e 20 62 65 74 77 65 65 6e 2e 20 20 54 68 in between. Th
d1fc0 65 20 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 74 20 e locking might
d1fd0 66 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 fail on one of t
d1fe0 68 65 20 6c 61 74 65 72 0a 2a 2a 20 74 72 61 6e he later.** tran
d1ff0 73 69 74 69 6f 6e 73 20 6c 65 61 76 69 6e 67 20 sitions leaving
d2000 74 68 65 20 6c 6f 63 6b 20 73 74 61 74 65 20 64 the lock state d
d2010 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 77 68 ifferent from wh
d2020 61 74 20 69 74 20 73 74 61 72 74 65 64 20 62 75 at it started bu
d2030 74 0a 2a 2a 20 73 74 69 6c 6c 20 73 68 6f 72 74 t.** still short
d2040 20 6f 66 20 69 74 73 20 67 6f 61 6c 2e 20 20 54 of its goal. T
d2050 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 he following cha
d2060 72 74 20 73 68 6f 77 73 20 74 68 65 20 61 6c 6c rt shows the all
d2070 6f 77 65 64 0a 2a 2a 20 74 72 61 6e 73 69 74 69 owed.** transiti
d2080 6f 6e 73 20 61 6e 64 20 74 68 65 20 69 6e 73 65 ons and the inse
d2090 72 74 65 64 20 69 6e 74 65 72 6d 65 64 69 61 74 rted intermediat
d20a0 65 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 e states:.**.**
d20b0 20 20 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 UNLOCKED -> S
d20c0 48 41 52 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 HARED.** SHAR
d20d0 45 44 20 2d 3e 20 52 45 53 45 52 56 45 44 0a 2a ED -> RESERVED.*
d20e0 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20 28 * SHARED -> (
d20f0 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c PENDING) -> EXCL
d2100 55 53 49 56 45 0a 2a 2a 20 20 20 20 52 45 53 45 USIVE.** RESE
d2110 52 56 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 RVED -> (PENDING
d2120 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a ) -> EXCLUSIVE.*
d2130 2a 20 20 20 20 50 45 4e 44 49 4e 47 20 2d 3e 20 * PENDING ->
d2140 45 58 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 EXCLUSIVE.**.**
d2150 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c This routine wil
d2160 6c 20 6f 6e 6c 79 20 69 6e 63 72 65 61 73 65 20 l only increase
d2170 61 20 6c 6f 63 6b 2e 20 20 55 73 65 20 74 68 65 a lock. Use the
d2180 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b sqlite3OsUnlock
d2190 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 74 6f ().** routine to
d21a0 20 6c 6f 77 65 72 20 61 20 6c 6f 63 6b 69 6e 67 lower a locking
d21b0 20 6c 65 76 65 6c 2e 0a 2a 2f 0a 73 74 61 74 69 level..*/.stati
d21c0 63 20 69 6e 74 20 75 6e 69 78 4c 6f 63 6b 28 73 c int unixLock(s
d21d0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c qlite3_file *id,
d21e0 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 7b 0a int locktype){.
d21f0 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 /* The followi
d2200 6e 67 20 64 65 73 63 72 69 62 65 73 20 74 68 65 ng describes the
d2210 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
d2220 6f 66 20 74 68 65 20 76 61 72 69 6f 75 73 20 6c of the various l
d2230 6f 63 6b 73 20 61 6e 64 0a 20 20 2a 2a 20 6c 6f ocks and. ** lo
d2240 63 6b 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 69 ck transitions i
d2250 6e 20 74 65 72 6d 73 20 6f 66 20 74 68 65 20 50 n terms of the P
d2260 4f 53 49 58 20 61 64 76 69 73 6f 72 79 20 73 68 OSIX advisory sh
d2270 61 72 65 64 20 61 6e 64 20 65 78 63 6c 75 73 69 ared and exclusi
d2280 76 65 0a 20 20 2a 2a 20 6c 6f 63 6b 20 70 72 69 ve. ** lock pri
d2290 6d 69 74 69 76 65 73 20 28 63 61 6c 6c 65 64 20 mitives (called
d22a0 72 65 61 64 2d 6c 6f 63 6b 73 20 61 6e 64 20 77 read-locks and w
d22b0 72 69 74 65 2d 6c 6f 63 6b 73 20 62 65 6c 6f 77 rite-locks below
d22c0 2c 20 74 6f 20 61 76 6f 69 64 0a 20 20 2a 2a 20 , to avoid. **
d22d0 63 6f 6e 66 75 73 69 6f 6e 20 77 69 74 68 20 53 confusion with S
d22e0 51 4c 69 74 65 20 6c 6f 63 6b 20 6e 61 6d 65 73 QLite lock names
d22f0 29 2e 20 54 68 65 20 61 6c 67 6f 72 69 74 68 6d ). The algorithm
d2300 73 20 61 72 65 20 63 6f 6d 70 6c 69 63 61 74 65 s are complicate
d2310 64 0a 20 20 2a 2a 20 73 6c 69 67 68 74 6c 79 20 d. ** slightly
d2320 69 6e 20 6f 72 64 65 72 20 74 6f 20 62 65 20 63 in order to be c
d2330 6f 6d 70 61 74 69 62 6c 65 20 77 69 74 68 20 77 ompatible with w
d2340 69 6e 64 6f 77 73 20 73 79 73 74 65 6d 73 20 73 indows systems s
d2350 69 6d 75 6c 74 61 6e 65 6f 75 73 6c 79 0a 20 20 imultaneously.
d2360 2a 2a 20 61 63 63 65 73 73 69 6e 67 20 74 68 65 ** accessing the
d2370 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 66 same database f
d2380 69 6c 65 2c 20 69 6e 20 63 61 73 65 20 74 68 61 ile, in case tha
d2390 74 20 69 73 20 65 76 65 72 20 72 65 71 75 69 72 t is ever requir
d23a0 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 53 79 ed.. **. ** Sy
d23b0 6d 62 6f 6c 73 20 64 65 66 69 6e 65 64 20 69 6e mbols defined in
d23c0 20 6f 73 2e 68 20 69 6e 64 65 6e 74 69 66 79 20 os.h indentify
d23d0 74 68 65 20 27 70 65 6e 64 69 6e 67 20 62 79 74 the 'pending byt
d23e0 65 27 20 61 6e 64 20 74 68 65 20 27 72 65 73 65 e' and the 'rese
d23f0 72 76 65 64 0a 20 20 2a 2a 20 62 79 74 65 27 2c rved. ** byte',
d2400 20 65 61 63 68 20 73 69 6e 67 6c 65 20 62 79 74 each single byt
d2410 65 73 20 61 74 20 77 65 6c 6c 20 6b 6e 6f 77 6e es at well known
d2420 20 6f 66 66 73 65 74 73 2c 20 61 6e 64 20 74 68 offsets, and th
d2430 65 20 27 73 68 61 72 65 64 20 62 79 74 65 0a 20 e 'shared byte.
d2440 20 2a 2a 20 72 61 6e 67 65 27 2c 20 61 20 72 61 ** range', a ra
d2450 6e 67 65 20 6f 66 20 35 31 30 20 62 79 74 65 73 nge of 510 bytes
d2460 20 61 74 20 61 20 77 65 6c 6c 20 6b 6e 6f 77 6e at a well known
d2470 20 6f 66 66 73 65 74 2e 0a 20 20 2a 2a 0a 20 20 offset.. **.
d2480 2a 2a 20 54 6f 20 6f 62 74 61 69 6e 20 61 20 53 ** To obtain a S
d2490 48 41 52 45 44 20 6c 6f 63 6b 2c 20 61 20 72 65 HARED lock, a re
d24a0 61 64 2d 6c 6f 63 6b 20 69 73 20 6f 62 74 61 69 ad-lock is obtai
d24b0 6e 65 64 20 6f 6e 20 74 68 65 20 27 70 65 6e 64 ned on the 'pend
d24c0 69 6e 67 0a 20 20 2a 2a 20 62 79 74 65 27 2e 20 ing. ** byte'.
d24d0 20 49 66 20 74 68 69 73 20 69 73 20 73 75 63 63 If this is succ
d24e0 65 73 73 66 75 6c 2c 20 61 20 72 61 6e 64 6f 6d essful, a random
d24f0 20 62 79 74 65 20 66 72 6f 6d 20 74 68 65 20 27 byte from the '
d2500 73 68 61 72 65 64 20 62 79 74 65 0a 20 20 2a 2a shared byte. **
d2510 20 72 61 6e 67 65 27 20 69 73 20 72 65 61 64 2d range' is read-
d2520 6c 6f 63 6b 65 64 20 61 6e 64 20 74 68 65 20 6c locked and the l
d2530 6f 63 6b 20 6f 6e 20 74 68 65 20 27 70 65 6e 64 ock on the 'pend
d2540 69 6e 67 20 62 79 74 65 27 20 72 65 6c 65 61 73 ing byte' releas
d2550 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 20 ed.. **. ** A
d2560 70 72 6f 63 65 73 73 20 6d 61 79 20 6f 6e 6c 79 process may only
d2570 20 6f 62 74 61 69 6e 20 61 20 52 45 53 45 52 56 obtain a RESERV
d2580 45 44 20 6c 6f 63 6b 20 61 66 74 65 72 20 69 74 ED lock after it
d2590 20 68 61 73 20 61 20 53 48 41 52 45 44 20 6c 6f has a SHARED lo
d25a0 63 6b 2e 0a 20 20 2a 2a 20 41 20 52 45 53 45 52 ck.. ** A RESER
d25b0 56 45 44 20 6c 6f 63 6b 20 69 73 20 69 6d 70 6c VED lock is impl
d25c0 65 6d 65 6e 74 65 64 20 62 79 20 67 72 61 62 62 emented by grabb
d25d0 69 6e 67 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b ing a write-lock
d25e0 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 27 72 65 on the. ** 're
d25f0 73 65 72 76 65 64 20 62 79 74 65 27 2e 20 0a 20 served byte'. .
d2600 20 2a 2a 0a 20 20 2a 2a 20 41 20 70 72 6f 63 65 **. ** A proce
d2610 73 73 20 6d 61 79 20 6f 6e 6c 79 20 6f 62 74 61 ss may only obta
d2620 69 6e 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 in a PENDING loc
d2630 6b 20 61 66 74 65 72 20 69 74 20 68 61 73 20 6f k after it has o
d2640 62 74 61 69 6e 65 64 20 61 0a 20 20 2a 2a 20 53 btained a. ** S
d2650 48 41 52 45 44 20 6c 6f 63 6b 2e 20 41 20 50 45 HARED lock. A PE
d2660 4e 44 49 4e 47 20 6c 6f 63 6b 20 69 73 20 69 6d NDING lock is im
d2670 70 6c 65 6d 65 6e 74 65 64 20 62 79 20 6f 62 74 plemented by obt
d2680 61 69 6e 69 6e 67 20 61 20 77 72 69 74 65 2d 6c aining a write-l
d2690 6f 63 6b 0a 20 20 2a 2a 20 6f 6e 20 74 68 65 20 ock. ** on the
d26a0 27 70 65 6e 64 69 6e 67 20 62 79 74 65 27 2e 20 'pending byte'.
d26b0 54 68 69 73 20 65 6e 73 75 72 65 73 20 74 68 61 This ensures tha
d26c0 74 20 6e 6f 20 6e 65 77 20 53 48 41 52 45 44 20 t no new SHARED
d26d0 6c 6f 63 6b 73 20 63 61 6e 20 62 65 0a 20 20 2a locks can be. *
d26e0 2a 20 6f 62 74 61 69 6e 65 64 2c 20 62 75 74 20 * obtained, but
d26f0 65 78 69 73 74 69 6e 67 20 53 48 41 52 45 44 20 existing SHARED
d2700 6c 6f 63 6b 73 20 61 72 65 20 61 6c 6c 6f 77 65 locks are allowe
d2710 64 20 74 6f 20 70 65 72 73 69 73 74 2e 20 41 20 d to persist. A
d2720 70 72 6f 63 65 73 73 0a 20 20 2a 2a 20 64 6f 65 process. ** doe
d2730 73 20 6e 6f 74 20 68 61 76 65 20 74 6f 20 6f 62 s not have to ob
d2740 74 61 69 6e 20 61 20 52 45 53 45 52 56 45 44 20 tain a RESERVED
d2750 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 77 61 79 20 lock on the way
d2760 74 6f 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 to a PENDING loc
d2770 6b 2e 0a 20 20 2a 2a 20 54 68 69 73 20 70 72 6f k.. ** This pro
d2780 70 65 72 74 79 20 69 73 20 75 73 65 64 20 62 79 perty is used by
d2790 20 74 68 65 20 61 6c 67 6f 72 69 74 68 6d 20 66 the algorithm f
d27a0 6f 72 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 or rolling back
d27b0 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 20 a journal file.
d27c0 20 2a 2a 20 61 66 74 65 72 20 61 20 63 72 61 73 ** after a cras
d27d0 68 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 6e 20 h.. **. ** An
d27e0 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2c 20 EXCLUSIVE lock,
d27f0 6f 62 74 61 69 6e 65 64 20 61 66 74 65 72 20 61 obtained after a
d2800 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 69 73 PENDING lock is
d2810 20 68 65 6c 64 2c 20 69 73 0a 20 20 2a 2a 20 69 held, is. ** i
d2820 6d 70 6c 65 6d 65 6e 74 65 64 20 62 79 20 6f 62 mplemented by ob
d2830 74 61 69 6e 69 6e 67 20 61 20 77 72 69 74 65 2d taining a write-
d2840 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 65 6e 74 69 lock on the enti
d2850 72 65 20 27 73 68 61 72 65 64 20 62 79 74 65 0a re 'shared byte.
d2860 20 20 2a 2a 20 72 61 6e 67 65 27 2e 20 53 69 6e ** range'. Sin
d2870 63 65 20 61 6c 6c 20 6f 74 68 65 72 20 6c 6f 63 ce all other loc
d2880 6b 73 20 72 65 71 75 69 72 65 20 61 20 72 65 61 ks require a rea
d2890 64 2d 6c 6f 63 6b 20 6f 6e 20 6f 6e 65 20 6f 66 d-lock on one of
d28a0 20 74 68 65 20 62 79 74 65 73 0a 20 20 2a 2a 20 the bytes. **
d28b0 77 69 74 68 69 6e 20 74 68 69 73 20 72 61 6e 67 within this rang
d28c0 65 2c 20 74 68 69 73 20 65 6e 73 75 72 65 73 20 e, this ensures
d28d0 74 68 61 74 20 6e 6f 20 6f 74 68 65 72 20 6c 6f that no other lo
d28e0 63 6b 73 20 61 72 65 20 68 65 6c 64 20 6f 6e 20 cks are held on
d28f0 74 68 65 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 the. ** databas
d2900 65 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 e. . **. ** Th
d2910 65 20 72 65 61 73 6f 6e 20 61 20 73 69 6e 67 6c e reason a singl
d2920 65 20 62 79 74 65 20 63 61 6e 6e 6f 74 20 62 65 e byte cannot be
d2930 20 75 73 65 64 20 69 6e 73 74 65 61 64 20 6f 66 used instead of
d2940 20 74 68 65 20 27 73 68 61 72 65 64 20 62 79 74 the 'shared byt
d2950 65 0a 20 20 2a 2a 20 72 61 6e 67 65 27 20 69 73 e. ** range' is
d2960 20 74 68 61 74 20 73 6f 6d 65 20 76 65 72 73 69 that some versi
d2970 6f 6e 73 20 6f 66 20 77 69 6e 64 6f 77 73 20 64 ons of windows d
d2980 6f 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 72 65 o not support re
d2990 61 64 2d 6c 6f 63 6b 73 2e 20 42 79 0a 20 20 2a ad-locks. By. *
d29a0 2a 20 6c 6f 63 6b 69 6e 67 20 61 20 72 61 6e 64 * locking a rand
d29b0 6f 6d 20 62 79 74 65 20 66 72 6f 6d 20 61 20 72 om byte from a r
d29c0 61 6e 67 65 2c 20 63 6f 6e 63 75 72 72 65 6e 74 ange, concurrent
d29d0 20 53 48 41 52 45 44 20 6c 6f 63 6b 73 20 6d 61 SHARED locks ma
d29e0 79 20 65 78 69 73 74 0a 20 20 2a 2a 20 65 76 65 y exist. ** eve
d29f0 6e 20 69 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 n if the locking
d2a00 20 70 72 69 6d 69 74 69 76 65 20 75 73 65 64 20 primitive used
d2a10 69 73 20 61 6c 77 61 79 73 20 61 20 77 72 69 74 is always a writ
d2a20 65 2d 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 e-lock.. */. i
d2a30 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
d2a40 4b 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 K;. unixFile *p
d2a50 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 File = (unixFile
d2a60 2a 29 69 64 3b 0a 20 20 73 74 72 75 63 74 20 75 *)id;. struct u
d2a70 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 2a 70 4c 6f nixLockInfo *pLo
d2a80 63 6b 20 3d 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 ck = pFile->pLoc
d2a90 6b 3b 0a 20 20 73 74 72 75 63 74 20 66 6c 6f 63 k;. struct floc
d2aa0 6b 20 6c 6f 63 6b 3b 0a 20 20 69 6e 74 20 73 20 k lock;. int s
d2ab0 3d 20 30 3b 0a 20 20 69 6e 74 20 74 45 72 72 6e = 0;. int tErrn
d2ac0 6f 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 46 o;.. assert( pF
d2ad0 69 6c 65 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 ile );. OSTRACE
d2ae0 37 28 22 4c 4f 43 4b 20 20 20 20 25 64 20 25 73 7("LOCK %d %s
d2af0 20 77 61 73 20 25 73 28 25 73 2c 25 64 29 20 70 was %s(%s,%d) p
d2b00 69 64 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d id=%d\n", pFile-
d2b10 3e 68 2c 0a 20 20 20 20 20 20 6c 6f 63 6b 74 79 >h,. lockty
d2b20 70 65 4e 61 6d 65 28 6c 6f 63 6b 74 79 70 65 29 peName(locktype)
d2b30 2c 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 70 , locktypeName(p
d2b40 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 29 2c File->locktype),
d2b50 0a 20 20 20 20 20 20 6c 6f 63 6b 74 79 70 65 4e . locktypeN
d2b60 61 6d 65 28 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 ame(pLock->lockt
d2b70 79 70 65 29 2c 20 70 4c 6f 63 6b 2d 3e 63 6e 74 ype), pLock->cnt
d2b80 20 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 0a 20 , getpid());..
d2b90 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 /* If there is
d2ba0 61 6c 72 65 61 64 79 20 61 20 6c 6f 63 6b 20 6f already a lock o
d2bb0 66 20 74 68 69 73 20 74 79 70 65 20 6f 72 20 6d f this type or m
d2bc0 6f 72 65 20 72 65 73 74 72 69 63 74 69 76 65 20 ore restrictive
d2bd0 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 75 6e 69 78 on the. ** unix
d2be0 46 69 6c 65 2c 20 64 6f 20 6e 6f 74 68 69 6e 67 File, do nothing
d2bf0 2e 20 44 6f 6e 27 74 20 75 73 65 20 74 68 65 20 . Don't use the
d2c00 65 6e 64 5f 6c 6f 63 6b 3a 20 65 78 69 74 20 70 end_lock: exit p
d2c10 61 74 68 2c 20 61 73 0a 20 20 2a 2a 20 75 6e 69 ath, as. ** uni
d2c20 78 45 6e 74 65 72 4d 75 74 65 78 28 29 20 68 61 xEnterMutex() ha
d2c30 73 6e 27 74 20 62 65 65 6e 20 63 61 6c 6c 65 64 sn't been called
d2c40 20 79 65 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 yet.. */. if(
d2c50 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
d2c60 3e 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 >=locktype ){.
d2c70 20 20 4f 53 54 52 41 43 45 33 28 22 4c 4f 43 4b OSTRACE3("LOCK
d2c80 20 20 20 20 25 64 20 25 73 20 6f 6b 20 28 61 6c %d %s ok (al
d2c90 72 65 61 64 79 20 68 65 6c 64 29 5c 6e 22 2c 20 ready held)\n",
d2ca0 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 pFile->h,.
d2cb0 20 20 20 20 20 20 6c 6f 63 6b 74 79 70 65 4e 61 locktypeNa
d2cc0 6d 65 28 6c 6f 63 6b 74 79 70 65 29 29 3b 0a 20 me(locktype));.
d2cd0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
d2ce0 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d _OK;. }.. /* M
d2cf0 61 6b 65 20 73 75 72 65 20 74 68 65 20 6c 6f 63 ake sure the loc
d2d00 6b 69 6e 67 20 73 65 71 75 65 6e 63 65 20 69 73 king sequence is
d2d10 20 63 6f 72 72 65 63 74 2e 0a 20 20 2a 2a 20 20 correct.. **
d2d20 28 31 29 20 57 65 20 6e 65 76 65 72 20 6d 6f 76 (1) We never mov
d2d30 65 20 66 72 6f 6d 20 75 6e 6c 6f 63 6b 65 64 20 e from unlocked
d2d40 74 6f 20 61 6e 79 74 68 69 6e 67 20 68 69 67 68 to anything high
d2d50 65 72 20 74 68 61 6e 20 73 68 61 72 65 64 20 6c er than shared l
d2d60 6f 63 6b 2e 0a 20 20 2a 2a 20 20 28 32 29 20 53 ock.. ** (2) S
d2d70 51 4c 69 74 65 20 6e 65 76 65 72 20 65 78 70 6c QLite never expl
d2d80 69 63 69 74 6c 79 20 72 65 71 75 65 73 74 73 20 icitly requests
d2d90 61 20 70 65 6e 64 69 67 20 6c 6f 63 6b 2e 0a 20 a pendig lock..
d2da0 20 2a 2a 20 20 28 33 29 20 41 20 73 68 61 72 65 ** (3) A share
d2db0 64 20 6c 6f 63 6b 20 69 73 20 61 6c 77 61 79 73 d lock is always
d2dc0 20 68 65 6c 64 20 77 68 65 6e 20 61 20 72 65 73 held when a res
d2dd0 65 72 76 65 20 6c 6f 63 6b 20 69 73 20 72 65 71 erve lock is req
d2de0 75 65 73 74 65 64 2e 0a 20 20 2a 2f 0a 20 20 61 uested.. */. a
d2df0 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c 6f ssert( pFile->lo
d2e00 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 cktype!=NO_LOCK
d2e10 7c 7c 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 || locktype==SHA
d2e20 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 RED_LOCK );. as
d2e30 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 21 3d sert( locktype!=
d2e40 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 3b 0a PENDING_LOCK );.
d2e50 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 assert( lockty
d2e60 70 65 21 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 pe!=RESERVED_LOC
d2e70 4b 20 7c 7c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b K || pFile->lock
d2e80 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 type==SHARED_LOC
d2e90 4b 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 K );.. /* This
d2ea0 6d 75 74 65 78 20 69 73 20 6e 65 65 64 65 64 20 mutex is needed
d2eb0 62 65 63 61 75 73 65 20 70 46 69 6c 65 2d 3e 70 because pFile->p
d2ec0 4c 6f 63 6b 20 69 73 20 73 68 61 72 65 64 20 61 Lock is shared a
d2ed0 63 72 6f 73 73 20 74 68 72 65 61 64 73 0a 20 20 cross threads.
d2ee0 2a 2f 0a 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 */. unixEnterMu
d2ef0 74 65 78 28 29 3b 0a 0a 20 20 2f 2a 20 4d 61 6b tex();.. /* Mak
d2f00 65 20 73 75 72 65 20 74 68 65 20 63 75 72 72 65 e sure the curre
d2f10 6e 74 20 74 68 72 65 61 64 20 6f 77 6e 73 20 74 nt thread owns t
d2f20 68 65 20 70 46 69 6c 65 2e 0a 20 20 2a 2f 0a 20 he pFile.. */.
d2f30 20 72 63 20 3d 20 74 72 61 6e 73 66 65 72 4f 77 rc = transferOw
d2f40 6e 65 72 73 68 69 70 28 70 46 69 6c 65 29 3b 0a nership(pFile);.
d2f50 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
d2f60 5f 4f 4b 20 29 7b 0a 20 20 20 20 75 6e 69 78 4c _OK ){. unixL
d2f70 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 20 eaveMutex();.
d2f80 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a return rc;. }.
d2f90 20 20 70 4c 6f 63 6b 20 3d 20 70 46 69 6c 65 2d pLock = pFile-
d2fa0 3e 70 4c 6f 63 6b 3b 0a 0a 20 20 2f 2a 20 49 66 >pLock;.. /* If
d2fb0 20 73 6f 6d 65 20 74 68 72 65 61 64 20 75 73 69 some thread usi
d2fc0 6e 67 20 74 68 69 73 20 50 49 44 20 68 61 73 20 ng this PID has
d2fd0 61 20 6c 6f 63 6b 20 76 69 61 20 61 20 64 69 66 a lock via a dif
d2fe0 66 65 72 65 6e 74 20 75 6e 69 78 46 69 6c 65 2a ferent unixFile*
d2ff0 0a 20 20 2a 2a 20 68 61 6e 64 6c 65 20 74 68 61 . ** handle tha
d3000 74 20 70 72 65 63 6c 75 64 65 73 20 74 68 65 20 t precludes the
d3010 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 2c 20 requested lock,
d3020 72 65 74 75 72 6e 20 42 55 53 59 2e 0a 20 20 2a return BUSY.. *
d3030 2f 0a 20 20 69 66 28 20 28 70 46 69 6c 65 2d 3e /. if( (pFile->
d3040 6c 6f 63 6b 74 79 70 65 21 3d 70 4c 6f 63 6b 2d locktype!=pLock-
d3050 3e 6c 6f 63 6b 74 79 70 65 20 26 26 20 0a 20 20 >locktype && .
d3060 20 20 20 20 20 20 20 20 28 70 4c 6f 63 6b 2d 3e (pLock->
d3070 6c 6f 63 6b 74 79 70 65 3e 3d 50 45 4e 44 49 4e locktype>=PENDIN
d3080 47 5f 4c 4f 43 4b 20 7c 7c 20 6c 6f 63 6b 74 79 G_LOCK || lockty
d3090 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b 29 29 pe>SHARED_LOCK))
d30a0 0a 20 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 . ){. rc = S
d30b0 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 QLITE_BUSY;.
d30c0 67 6f 74 6f 20 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 goto end_lock;.
d30d0 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 61 20 53 48 }.. /* If a SH
d30e0 41 52 45 44 20 6c 6f 63 6b 20 69 73 20 72 65 71 ARED lock is req
d30f0 75 65 73 74 65 64 2c 20 61 6e 64 20 73 6f 6d 65 uested, and some
d3100 20 74 68 72 65 61 64 20 75 73 69 6e 67 20 74 68 thread using th
d3110 69 73 20 50 49 44 20 61 6c 72 65 61 64 79 0a 20 is PID already.
d3120 20 2a 2a 20 68 61 73 20 61 20 53 48 41 52 45 44 ** has a SHARED
d3130 20 6f 72 20 52 45 53 45 52 56 45 44 20 6c 6f 63 or RESERVED loc
d3140 6b 2c 20 74 68 65 6e 20 69 6e 63 72 65 6d 65 6e k, then incremen
d3150 74 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e t reference coun
d3160 74 73 20 61 6e 64 0a 20 20 2a 2a 20 72 65 74 75 ts and. ** retu
d3170 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 20 20 rn SQLITE_OK..
d3180 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 */. if( locktyp
d3190 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 26 e==SHARED_LOCK &
d31a0 26 20 0a 20 20 20 20 20 20 28 70 4c 6f 63 6b 2d & . (pLock-
d31b0 3e 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 >locktype==SHARE
d31c0 44 5f 4c 4f 43 4b 20 7c 7c 20 70 4c 6f 63 6b 2d D_LOCK || pLock-
d31d0 3e 6c 6f 63 6b 74 79 70 65 3d 3d 52 45 53 45 52 >locktype==RESER
d31e0 56 45 44 5f 4c 4f 43 4b 29 20 29 7b 0a 20 20 20 VED_LOCK) ){.
d31f0 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 assert( locktyp
d3200 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 e==SHARED_LOCK )
d3210 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 46 ;. assert( pF
d3220 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 30 ile->locktype==0
d3230 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
d3240 70 4c 6f 63 6b 2d 3e 63 6e 74 3e 30 20 29 3b 0a pLock->cnt>0 );.
d3250 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 pFile->lockt
d3260 79 70 65 20 3d 20 53 48 41 52 45 44 5f 4c 4f 43 ype = SHARED_LOC
d3270 4b 3b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 63 6e K;. pLock->cn
d3280 74 2b 2b 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e t++;. pFile->
d3290 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 2b 2b 3b 0a pOpen->nLock++;.
d32a0 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 6c 6f 63 goto end_loc
d32b0 6b 3b 0a 20 20 7d 0a 0a 0a 20 20 2f 2a 20 41 20 k;. }... /* A
d32c0 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 69 73 20 PENDING lock is
d32d0 6e 65 65 64 65 64 20 62 65 66 6f 72 65 20 61 63 needed before ac
d32e0 71 75 69 72 69 6e 67 20 61 20 53 48 41 52 45 44 quiring a SHARED
d32f0 20 6c 6f 63 6b 20 61 6e 64 20 62 65 66 6f 72 65 lock and before
d3300 0a 20 20 2a 2a 20 61 63 71 75 69 72 69 6e 67 20 . ** acquiring
d3310 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 an EXCLUSIVE loc
d3320 6b 2e 20 20 46 6f 72 20 74 68 65 20 53 48 41 52 k. For the SHAR
d3330 45 44 20 6c 6f 63 6b 2c 20 74 68 65 20 50 45 4e ED lock, the PEN
d3340 44 49 4e 47 20 77 69 6c 6c 0a 20 20 2a 2a 20 62 DING will. ** b
d3350 65 20 72 65 6c 65 61 73 65 64 2e 0a 20 20 2a 2f e released.. */
d3360 0a 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 . lock.l_len =
d3370 31 4c 3b 0a 20 20 6c 6f 63 6b 2e 6c 5f 77 68 65 1L;. lock.l_whe
d3380 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a nce = SEEK_SET;.
d3390 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d if( locktype==
d33a0 53 48 41 52 45 44 5f 4c 4f 43 4b 20 0a 20 20 20 SHARED_LOCK .
d33b0 20 20 20 7c 7c 20 28 6c 6f 63 6b 74 79 70 65 3d || (locktype=
d33c0 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 =EXCLUSIVE_LOCK
d33d0 26 26 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 && pFile->lockty
d33e0 70 65 3c 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 29 pe<PENDING_LOCK)
d33f0 0a 20 20 29 7b 0a 20 20 20 20 6c 6f 63 6b 2e 6c . ){. lock.l
d3400 5f 74 79 70 65 20 3d 20 28 6c 6f 63 6b 74 79 70 _type = (locktyp
d3410 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 3f 46 e==SHARED_LOCK?F
d3420 5f 52 44 4c 43 4b 3a 46 5f 57 52 4c 43 4b 29 3b _RDLCK:F_WRLCK);
d3430 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 . lock.l_star
d3440 74 20 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 t = PENDING_BYTE
d3450 3b 0a 20 20 20 20 73 20 3d 20 66 63 6e 74 6c 28 ;. s = fcntl(
d3460 70 46 69 6c 65 2d 3e 68 2c 20 46 5f 53 45 54 4c pFile->h, F_SETL
d3470 4b 2c 20 26 6c 6f 63 6b 29 3b 0a 20 20 20 20 69 K, &lock);. i
d3480 66 28 20 73 3d 3d 28 2d 31 29 20 29 7b 0a 20 20 f( s==(-1) ){.
d3490 20 20 20 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 tErrno = err
d34a0 6e 6f 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 no;. rc = s
d34b0 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f qliteErrorFromPo
d34c0 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c sixError(tErrno,
d34d0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f SQLITE_IOERR_LO
d34e0 43 4b 29 3b 0a 20 20 20 20 20 20 69 66 28 20 49 CK);. if( I
d34f0 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 S_LOCK_ERROR(rc)
d3500 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c ){. pFil
d3510 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 e->lastErrno = t
d3520 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a 20 Errno;. }.
d3530 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 6c 6f goto end_lo
d3540 63 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 0a ck;. }. }...
d3550 20 20 2f 2a 20 49 66 20 63 6f 6e 74 72 6f 6c 20 /* If control
d3560 67 65 74 73 20 74 6f 20 74 68 69 73 20 70 6f 69 gets to this poi
d3570 6e 74 2c 20 74 68 65 6e 20 61 63 74 75 61 6c 6c nt, then actuall
d3580 79 20 67 6f 20 61 68 65 61 64 20 61 6e 64 20 6d y go ahead and m
d3590 61 6b 65 0a 20 20 2a 2a 20 6f 70 65 72 61 74 69 ake. ** operati
d35a0 6e 67 20 73 79 73 74 65 6d 20 63 61 6c 6c 73 20 ng system calls
d35b0 66 6f 72 20 74 68 65 20 73 70 65 63 69 66 69 65 for the specifie
d35c0 64 20 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 d lock.. */. i
d35d0 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 f( locktype==SHA
d35e0 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 RED_LOCK ){.
d35f0 61 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 63 assert( pLock->c
d3600 6e 74 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 nt==0 );. ass
d3610 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b ert( pLock->lock
d3620 74 79 70 65 3d 3d 30 20 29 3b 0a 0a 20 20 20 20 type==0 );..
d3630 2f 2a 20 4e 6f 77 20 67 65 74 20 74 68 65 20 72 /* Now get the r
d3640 65 61 64 2d 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 ead-lock */.
d3650 73 20 3d 20 72 61 6e 67 65 4c 6f 63 6b 28 70 46 s = rangeLock(pF
d3660 69 6c 65 2c 20 46 5f 52 44 4c 43 4b 2c 20 26 74 ile, F_RDLCK, &t
d3670 45 72 72 6e 6f 29 3b 0a 0a 20 20 20 20 2f 2a 20 Errno);.. /*
d3680 44 72 6f 70 20 74 68 65 20 74 65 6d 70 6f 72 61 Drop the tempora
d3690 72 79 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 ry PENDING lock
d36a0 2a 2f 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 */. lock.l_st
d36b0 61 72 74 20 3d 20 50 45 4e 44 49 4e 47 5f 42 59 art = PENDING_BY
d36c0 54 45 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c TE;. lock.l_l
d36d0 65 6e 20 3d 20 31 4c 3b 0a 20 20 20 20 6c 6f 63 en = 1L;. loc
d36e0 6b 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 55 4e 4c k.l_type = F_UNL
d36f0 43 4b 3b 0a 20 20 20 20 69 66 28 20 66 63 6e 74 CK;. if( fcnt
d3700 6c 28 70 46 69 6c 65 2d 3e 68 2c 20 46 5f 53 45 l(pFile->h, F_SE
d3710 54 4c 4b 2c 20 26 6c 6f 63 6b 29 21 3d 30 20 29 TLK, &lock)!=0 )
d3720 7b 0a 20 20 20 20 20 20 69 66 28 20 73 20 21 3d {. if( s !=
d3730 20 2d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 2f -1 ){. /
d3740 2a 20 54 68 69 73 20 63 6f 75 6c 64 20 68 61 70 * This could hap
d3750 70 65 6e 20 77 69 74 68 20 61 20 6e 65 74 77 6f pen with a netwo
d3760 72 6b 20 6d 6f 75 6e 74 20 2a 2f 0a 20 20 20 20 rk mount */.
d3770 20 20 20 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 tErrno = err
d3780 6e 6f 3b 20 0a 20 20 20 20 20 20 20 20 72 63 20 no; . rc
d3790 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f = sqliteErrorFro
d37a0 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 mPosixError(tErr
d37b0 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 no, SQLITE_IOERR
d37c0 5f 55 4e 4c 4f 43 4b 29 3b 20 0a 20 20 20 20 20 _UNLOCK); .
d37d0 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 if( IS_LOCK_E
d37e0 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 20 RROR(rc) ){.
d37f0 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 pFile->las
d3800 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b tErrno = tErrno;
d3810 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
d3820 20 20 20 67 6f 74 6f 20 65 6e 64 5f 6c 6f 63 6b goto end_lock
d3830 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
d3840 20 20 20 20 69 66 28 20 73 3d 3d 28 2d 31 29 20 if( s==(-1)
d3850 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 ){. rc = sq
d3860 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 liteErrorFromPos
d3870 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 ixError(tErrno,
d3880 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 SQLITE_IOERR_LOC
d3890 4b 29 3b 0a 20 20 20 20 20 20 69 66 28 20 49 53 K);. if( IS
d38a0 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 _LOCK_ERROR(rc)
d38b0 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 ){. pFile
d38c0 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 ->lastErrno = tE
d38d0 72 72 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 rrno;. }.
d38e0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 }else{. p
d38f0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d File->locktype =
d3900 20 53 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20 SHARED_LOCK;.
d3910 20 20 20 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e pFile->pOpen
d3920 2d 3e 6e 4c 6f 63 6b 2b 2b 3b 0a 20 20 20 20 20 ->nLock++;.
d3930 20 70 4c 6f 63 6b 2d 3e 63 6e 74 20 3d 20 31 3b pLock->cnt = 1;
d3940 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 . }. }else i
d3950 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 f( locktype==EXC
d3960 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 70 LUSIVE_LOCK && p
d3970 4c 6f 63 6b 2d 3e 63 6e 74 3e 31 20 29 7b 0a 20 Lock->cnt>1 ){.
d3980 20 20 20 2f 2a 20 57 65 20 61 72 65 20 74 72 79 /* We are try
d3990 69 6e 67 20 66 6f 72 20 61 6e 20 65 78 63 6c 75 ing for an exclu
d39a0 73 69 76 65 20 6c 6f 63 6b 20 62 75 74 20 61 6e sive lock but an
d39b0 6f 74 68 65 72 20 74 68 72 65 61 64 20 69 6e 20 other thread in
d39c0 74 68 69 73 0a 20 20 20 20 2a 2a 20 73 61 6d 65 this. ** same
d39d0 20 70 72 6f 63 65 73 73 20 69 73 20 73 74 69 6c process is stil
d39e0 6c 20 68 6f 6c 64 69 6e 67 20 61 20 73 68 61 72 l holding a shar
d39f0 65 64 20 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 20 20 ed lock. */.
d3a00 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 rc = SQLITE_BUSY
d3a10 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f ;. }else{. /
d3a20 2a 20 54 68 65 20 72 65 71 75 65 73 74 20 77 61 * The request wa
d3a30 73 20 66 6f 72 20 61 20 52 45 53 45 52 56 45 44 s for a RESERVED
d3a40 20 6f 72 20 45 58 43 4c 55 53 49 56 45 20 6c 6f or EXCLUSIVE lo
d3a50 63 6b 2e 20 20 49 74 20 69 73 0a 20 20 20 20 2a ck. It is. *
d3a60 2a 20 61 73 73 75 6d 65 64 20 74 68 61 74 20 74 * assumed that t
d3a70 68 65 72 65 20 69 73 20 61 20 53 48 41 52 45 44 here is a SHARED
d3a80 20 6f 72 20 67 72 65 61 74 65 72 20 6c 6f 63 6b or greater lock
d3a90 20 6f 6e 20 74 68 65 20 66 69 6c 65 0a 20 20 20 on the file.
d3aa0 20 2a 2a 20 61 6c 72 65 61 64 79 2e 0a 20 20 20 ** already..
d3ab0 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 */. assert(
d3ac0 30 21 3d 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 0!=pFile->lockty
d3ad0 70 65 20 29 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c pe );. lock.l
d3ae0 5f 74 79 70 65 20 3d 20 46 5f 57 52 4c 43 4b 3b _type = F_WRLCK;
d3af0 0a 20 20 20 20 73 77 69 74 63 68 28 20 6c 6f 63 . switch( loc
d3b00 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 20 20 63 ktype ){. c
d3b10 61 73 65 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 ase RESERVED_LOC
d3b20 4b 3a 0a 20 20 20 20 20 20 20 20 6c 6f 63 6b 2e K:. lock.
d3b30 6c 5f 73 74 61 72 74 20 3d 20 52 45 53 45 52 56 l_start = RESERV
d3b40 45 44 5f 42 59 54 45 3b 0a 20 20 20 20 20 20 20 ED_BYTE;.
d3b50 20 73 20 3d 20 66 63 6e 74 6c 28 70 46 69 6c 65 s = fcntl(pFile
d3b60 2d 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c ->h, F_SETLK, &l
d3b70 6f 63 6b 29 3b 0a 20 20 20 20 20 20 20 20 74 45 ock);. tE
d3b80 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 rrno = errno;.
d3b90 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
d3ba0 20 20 20 63 61 73 65 20 45 58 43 4c 55 53 49 56 case EXCLUSIV
d3bb0 45 5f 4c 4f 43 4b 3a 0a 20 20 20 20 20 20 20 20 E_LOCK:.
d3bc0 73 20 3d 20 72 61 6e 67 65 4c 6f 63 6b 28 70 46 s = rangeLock(pF
d3bd0 69 6c 65 2c 20 46 5f 57 52 4c 43 4b 2c 20 26 74 ile, F_WRLCK, &t
d3be0 45 72 72 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20 Errno);.
d3bf0 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 64 65 66 break;. def
d3c00 61 75 6c 74 3a 0a 20 20 20 20 20 20 20 20 61 73 ault:. as
d3c10 73 65 72 74 28 30 29 3b 0a 20 20 20 20 7d 0a 20 sert(0);. }.
d3c20 20 20 20 69 66 28 20 73 3d 3d 28 2d 31 29 20 29 if( s==(-1) )
d3c30 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c {. rc = sql
d3c40 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 iteErrorFromPosi
d3c50 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 xError(tErrno, S
d3c60 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b QLITE_IOERR_LOCK
d3c70 29 3b 0a 20 20 20 20 20 20 69 66 28 20 49 53 5f );. if( IS_
d3c80 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 LOCK_ERROR(rc) )
d3c90 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d {. pFile-
d3ca0 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 >lastErrno = tEr
d3cb0 72 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 rno;. }.
d3cc0 20 7d 0a 20 20 7d 0a 20 20 0a 0a 23 69 66 6e 64 }. }. ..#ifnd
d3cd0 65 66 20 4e 44 45 42 55 47 0a 20 20 2f 2a 20 53 ef NDEBUG. /* S
d3ce0 65 74 20 75 70 20 74 68 65 20 74 72 61 6e 73 61 et up the transa
d3cf0 63 74 69 6f 6e 2d 63 6f 75 6e 74 65 72 20 63 68 ction-counter ch
d3d00 61 6e 67 65 20 63 68 65 63 6b 69 6e 67 20 66 6c ange checking fl
d3d10 61 67 73 20 77 68 65 6e 0a 20 20 2a 2a 20 74 72 ags when. ** tr
d3d20 61 6e 73 69 74 69 6f 6e 69 6e 67 20 66 72 6f 6d ansitioning from
d3d30 20 61 20 53 48 41 52 45 44 20 74 6f 20 61 20 52 a SHARED to a R
d3d40 45 53 45 52 56 45 44 20 6c 6f 63 6b 2e 20 20 54 ESERVED lock. T
d3d50 68 65 20 63 68 61 6e 67 65 0a 20 20 2a 2a 20 66 he change. ** f
d3d60 72 6f 6d 20 53 48 41 52 45 44 20 74 6f 20 52 45 rom SHARED to RE
d3d70 53 45 52 56 45 44 20 6d 61 72 6b 73 20 74 68 65 SERVED marks the
d3d80 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 61 20 beginning of a
d3d90 6e 6f 72 6d 61 6c 0a 20 20 2a 2a 20 77 72 69 74 normal. ** writ
d3da0 65 20 6f 70 65 72 61 74 69 6f 6e 20 28 6e 6f 74 e operation (not
d3db0 20 61 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 20 72 a hot journal r
d3dc0 6f 6c 6c 62 61 63 6b 29 2e 0a 20 20 2a 2f 0a 20 ollback).. */.
d3dd0 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
d3de0 4f 4b 0a 20 20 20 26 26 20 70 46 69 6c 65 2d 3e OK. && pFile->
d3df0 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 41 52 45 44 locktype<=SHARED
d3e00 5f 4c 4f 43 4b 0a 20 20 20 26 26 20 6c 6f 63 6b _LOCK. && lock
d3e10 74 79 70 65 3d 3d 52 45 53 45 52 56 45 44 5f 4c type==RESERVED_L
d3e20 4f 43 4b 0a 20 20 29 7b 0a 20 20 20 20 70 46 69 OCK. ){. pFi
d3e30 6c 65 2d 3e 74 72 61 6e 73 43 6e 74 72 43 68 6e le->transCntrChn
d3e40 67 20 3d 20 30 3b 0a 20 20 20 20 70 46 69 6c 65 g = 0;. pFile
d3e50 2d 3e 64 62 55 70 64 61 74 65 20 3d 20 30 3b 0a ->dbUpdate = 0;.
d3e60 20 20 20 20 70 46 69 6c 65 2d 3e 69 6e 4e 6f 72 pFile->inNor
d3e70 6d 61 6c 57 72 69 74 65 20 3d 20 31 3b 0a 20 20 malWrite = 1;.
d3e80 7d 0a 23 65 6e 64 69 66 0a 0a 0a 20 20 69 66 28 }.#endif... if(
d3e90 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
d3ea0 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 {. pFile->loc
d3eb0 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 ktype = locktype
d3ec0 3b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 ;. pLock->loc
d3ed0 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 ktype = locktype
d3ee0 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 6f ;. }else if( lo
d3ef0 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 cktype==EXCLUSIV
d3f00 45 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 70 46 E_LOCK ){. pF
d3f10 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 ile->locktype =
d3f20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3b 0a 20 20 PENDING_LOCK;.
d3f30 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 pLock->locktyp
d3f40 65 20 3d 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b e = PENDING_LOCK
d3f50 3b 0a 20 20 7d 0a 0a 65 6e 64 5f 6c 6f 63 6b 3a ;. }..end_lock:
d3f60 0a 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 . unixLeaveMute
d3f70 78 28 29 3b 0a 20 20 4f 53 54 52 41 43 45 34 28 x();. OSTRACE4(
d3f80 22 4c 4f 43 4b 20 20 20 20 25 64 20 25 73 20 25 "LOCK %d %s %
d3f90 73 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 s\n", pFile->h,
d3fa0 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 6c 6f 63 locktypeName(loc
d3fb0 6b 74 79 70 65 29 2c 20 0a 20 20 20 20 20 20 72 ktype), . r
d3fc0 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f 20 22 c==SQLITE_OK ? "
d3fd0 6f 6b 22 20 3a 20 22 66 61 69 6c 65 64 22 29 3b ok" : "failed");
d3fe0 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
d3ff0 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 6c 6c ./*.** Close all
d4000 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 file descriptor
d4010 73 20 61 63 63 75 6d 75 61 74 65 64 20 69 6e 20 s accumuated in
d4020 74 68 65 20 75 6e 69 78 4f 70 65 6e 43 6e 74 2d the unixOpenCnt-
d4030 3e 70 55 6e 75 73 65 64 20 6c 69 73 74 2e 0a 2a >pUnused list..*
d4040 2a 20 49 66 20 61 6c 6c 20 73 75 63 68 20 66 69 * If all such fi
d4050 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73 20 61 le descriptors a
d4060 72 65 20 63 6c 6f 73 65 64 20 77 69 74 68 6f 75 re closed withou
d4070 74 20 65 72 72 6f 72 2c 20 74 68 65 20 6c 69 73 t error, the lis
d4080 74 20 69 73 0a 2a 2a 20 63 6c 65 61 72 65 64 20 t is.** cleared
d4090 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 and SQLITE_OK re
d40a0 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 4f 74 turned..**.** Ot
d40b0 68 65 72 77 69 73 65 2c 20 69 66 20 61 6e 20 65 herwise, if an e
d40c0 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 74 68 65 rror occurs, the
d40d0 6e 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 63 n successfully c
d40e0 6c 6f 73 65 64 20 66 69 6c 65 20 64 65 73 63 72 losed file descr
d40f0 69 70 74 6f 72 0a 2a 2a 20 65 6e 74 72 69 65 73 iptor.** entries
d4100 20 61 72 65 20 72 65 6d 6f 76 65 64 20 66 72 6f are removed fro
d4110 6d 20 74 68 65 20 6c 69 73 74 2c 20 61 6e 64 20 m the list, and
d4120 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 4c 4f SQLITE_IOERR_CLO
d4130 53 45 20 72 65 74 75 72 6e 65 64 2e 20 0a 2a 2a SE returned. .**
d4140 20 6e 6f 74 20 64 65 6c 65 74 65 64 20 61 6e 64 not deleted and
d4150 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 4c SQLITE_IOERR_CL
d4160 4f 53 45 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f OSE returned..*/
d4170 20 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6c 6f .static int clo
d4180 73 65 50 65 6e 64 69 6e 67 46 64 73 28 75 6e 69 sePendingFds(uni
d4190 78 46 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 xFile *pFile){.
d41a0 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
d41b0 5f 4f 4b 3b 0a 20 20 73 74 72 75 63 74 20 75 6e _OK;. struct un
d41c0 69 78 4f 70 65 6e 43 6e 74 20 2a 70 4f 70 65 6e ixOpenCnt *pOpen
d41d0 20 3d 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 3b = pFile->pOpen;
d41e0 0a 20 20 55 6e 69 78 55 6e 75 73 65 64 46 64 20 . UnixUnusedFd
d41f0 2a 70 45 72 72 6f 72 20 3d 20 30 3b 0a 20 20 55 *pError = 0;. U
d4200 6e 69 78 55 6e 75 73 65 64 46 64 20 2a 70 3b 0a nixUnusedFd *p;.
d4210 20 20 55 6e 69 78 55 6e 75 73 65 64 46 64 20 2a UnixUnusedFd *
d4220 70 4e 65 78 74 3b 0a 20 20 66 6f 72 28 70 3d 70 pNext;. for(p=p
d4230 4f 70 65 6e 2d 3e 70 55 6e 75 73 65 64 3b 20 70 Open->pUnused; p
d4240 3b 20 70 3d 70 4e 65 78 74 29 7b 0a 20 20 20 20 ; p=pNext){.
d4250 70 4e 65 78 74 20 3d 20 70 2d 3e 70 4e 65 78 74 pNext = p->pNext
d4260 3b 0a 20 20 20 20 69 66 28 20 63 6c 6f 73 65 28 ;. if( close(
d4270 70 2d 3e 66 64 29 20 29 7b 0a 20 20 20 20 20 20 p->fd) ){.
d4280 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f pFile->lastErrno
d4290 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 = errno;.
d42a0 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 rc = SQLITE_IOER
d42b0 52 5f 43 4c 4f 53 45 3b 0a 20 20 20 20 20 20 70 R_CLOSE;. p
d42c0 2d 3e 70 4e 65 78 74 20 3d 20 70 45 72 72 6f 72 ->pNext = pError
d42d0 3b 0a 20 20 20 20 20 20 70 45 72 72 6f 72 20 3d ;. pError =
d42e0 20 70 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 p;. }else{.
d42f0 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 sqlite3_fre
d4300 65 28 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a e(p);. }. }.
d4310 20 20 70 4f 70 65 6e 2d 3e 70 55 6e 75 73 65 64 pOpen->pUnused
d4320 20 3d 20 70 45 72 72 6f 72 3b 0a 20 20 72 65 74 = pError;. ret
d4330 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
d4340 20 41 64 64 20 74 68 65 20 66 69 6c 65 20 64 65 Add the file de
d4350 73 63 72 69 70 74 6f 72 20 75 73 65 64 20 62 79 scriptor used by
d4360 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 70 46 69 file handle pFi
d4370 6c 65 20 74 6f 20 74 68 65 20 63 6f 72 72 65 73 le to the corres
d4380 70 6f 6e 64 69 6e 67 0a 2a 2a 20 70 55 6e 75 73 ponding.** pUnus
d4390 65 64 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 ed list..*/.stat
d43a0 69 63 20 76 6f 69 64 20 73 65 74 50 65 6e 64 69 ic void setPendi
d43b0 6e 67 46 64 28 75 6e 69 78 46 69 6c 65 20 2a 70 ngFd(unixFile *p
d43c0 46 69 6c 65 29 7b 0a 20 20 73 74 72 75 63 74 20 File){. struct
d43d0 75 6e 69 78 4f 70 65 6e 43 6e 74 20 2a 70 4f 70 unixOpenCnt *pOp
d43e0 65 6e 20 3d 20 70 46 69 6c 65 2d 3e 70 4f 70 65 en = pFile->pOpe
d43f0 6e 3b 0a 20 20 55 6e 69 78 55 6e 75 73 65 64 46 n;. UnixUnusedF
d4400 64 20 2a 70 20 3d 20 70 46 69 6c 65 2d 3e 70 55 d *p = pFile->pU
d4410 6e 75 73 65 64 3b 0a 20 20 70 2d 3e 70 4e 65 78 nused;. p->pNex
d4420 74 20 3d 20 70 4f 70 65 6e 2d 3e 70 55 6e 75 73 t = pOpen->pUnus
d4430 65 64 3b 0a 20 20 70 4f 70 65 6e 2d 3e 70 55 6e ed;. pOpen->pUn
d4440 75 73 65 64 20 3d 20 70 3b 0a 20 20 70 46 69 6c used = p;. pFil
d4450 65 2d 3e 68 20 3d 20 2d 31 3b 0a 20 20 70 46 69 e->h = -1;. pFi
d4460 6c 65 2d 3e 70 55 6e 75 73 65 64 20 3d 20 30 3b le->pUnused = 0;
d4470 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20 .}../*.** Lower
d4480 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 the locking leve
d4490 6c 20 6f 6e 20 66 69 6c 65 20 64 65 73 63 72 69 l on file descri
d44a0 70 74 6f 72 20 70 46 69 6c 65 20 74 6f 20 6c 6f ptor pFile to lo
d44b0 63 6b 74 79 70 65 2e 20 20 6c 6f 63 6b 74 79 70 cktype. locktyp
d44c0 65 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65 69 74 e.** must be eit
d44d0 68 65 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 her NO_LOCK or S
d44e0 48 41 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a HARED_LOCK..**.*
d44f0 2a 20 49 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 * If the locking
d4500 20 6c 65 76 65 6c 20 6f 66 20 74 68 65 20 66 69 level of the fi
d4510 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 69 73 le descriptor is
d4520 20 61 6c 72 65 61 64 79 20 61 74 20 6f 72 20 62 already at or b
d4530 65 6c 6f 77 0a 2a 2a 20 74 68 65 20 72 65 71 75 elow.** the requ
d4540 65 73 74 65 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 ested locking le
d4550 76 65 6c 2c 20 74 68 69 73 20 72 6f 75 74 69 6e vel, this routin
d4560 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f e is a no-op..*/
d4570 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 .static int unix
d4580 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 Unlock(sqlite3_f
d4590 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 ile *id, int loc
d45a0 6b 74 79 70 65 29 7b 0a 20 20 75 6e 69 78 46 69 ktype){. unixFi
d45b0 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 le *pFile = (uni
d45c0 78 46 69 6c 65 2a 29 69 64 3b 20 2f 2a 20 54 68 xFile*)id; /* Th
d45d0 65 20 6f 70 65 6e 20 66 69 6c 65 20 2a 2f 0a 20 e open file */.
d45e0 20 73 74 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b struct unixLock
d45f0 49 6e 66 6f 20 2a 70 4c 6f 63 6b 3b 20 20 20 20 Info *pLock;
d4600 20 20 2f 2a 20 53 74 72 75 63 74 75 72 65 20 64 /* Structure d
d4610 65 73 63 72 69 62 69 6e 67 20 63 75 72 72 65 6e escribing curren
d4620 74 20 6c 6f 63 6b 20 73 74 61 74 65 20 2a 2f 0a t lock state */.
d4630 20 20 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c struct flock l
d4640 6f 63 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 ock;
d4650 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f /* Informatio
d4660 6e 20 70 61 73 73 65 64 20 69 6e 74 6f 20 66 63 n passed into fc
d4670 6e 74 6c 28 29 20 2a 2f 0a 20 20 69 6e 74 20 72 ntl() */. int r
d4680 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 c = SQLITE_OK;
d4690 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 /* R
d46a0 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 eturn code from
d46b0 74 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 2a this interface *
d46c0 2f 0a 20 20 69 6e 74 20 68 3b 20 20 20 20 20 20 /. int h;
d46d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d46e0 20 20 20 20 20 2f 2a 20 54 68 65 20 75 6e 64 65 /* The unde
d46f0 72 6c 79 69 6e 67 20 66 69 6c 65 20 64 65 73 63 rlying file desc
d4700 72 69 70 74 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 riptor */. int
d4710 74 45 72 72 6e 6f 3b 20 20 20 20 20 20 20 20 20 tErrno;
d4720 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
d4730 45 72 72 6f 72 20 63 6f 64 65 20 66 72 6f 6d 20 Error code from
d4740 73 79 73 74 65 6d 20 63 61 6c 6c 20 65 72 72 6f system call erro
d4750 72 73 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 rs */.. assert(
d4760 20 70 46 69 6c 65 20 29 3b 0a 20 20 4f 53 54 52 pFile );. OSTR
d4770 41 43 45 37 28 22 55 4e 4c 4f 43 4b 20 20 25 64 ACE7("UNLOCK %d
d4780 20 25 64 20 77 61 73 20 25 64 28 25 64 2c 25 64 %d was %d(%d,%d
d4790 29 20 70 69 64 3d 25 64 5c 6e 22 2c 20 70 46 69 ) pid=%d\n", pFi
d47a0 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c le->h, locktype,
d47b0 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f . pFile->lo
d47c0 63 6b 74 79 70 65 2c 20 70 46 69 6c 65 2d 3e 70 cktype, pFile->p
d47d0 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 Lock->locktype,
d47e0 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e 63 6e pFile->pLock->cn
d47f0 74 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 0a 20 t, getpid());..
d4800 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 assert( locktyp
d4810 65 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 e<=SHARED_LOCK )
d4820 3b 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c ;. if( pFile->l
d4830 6f 63 6b 74 79 70 65 3c 3d 6c 6f 63 6b 74 79 70 ocktype<=locktyp
d4840 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 e ){. return
d4850 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 SQLITE_OK;. }.
d4860 20 69 66 28 20 43 48 45 43 4b 5f 54 48 52 45 41 if( CHECK_THREA
d4870 44 49 44 28 70 46 69 6c 65 29 20 29 7b 0a 20 20 DID(pFile) ){.
d4880 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
d4890 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 75 6e MISUSE;. }. un
d48a0 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a ixEnterMutex();.
d48b0 20 20 68 20 3d 20 70 46 69 6c 65 2d 3e 68 3b 0a h = pFile->h;.
d48c0 20 20 70 4c 6f 63 6b 20 3d 20 70 46 69 6c 65 2d pLock = pFile-
d48d0 3e 70 4c 6f 63 6b 3b 0a 20 20 61 73 73 65 72 74 >pLock;. assert
d48e0 28 20 70 4c 6f 63 6b 2d 3e 63 6e 74 21 3d 30 20 ( pLock->cnt!=0
d48f0 29 3b 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e );. if( pFile->
d4900 6c 6f 63 6b 74 79 70 65 3e 53 48 41 52 45 44 5f locktype>SHARED_
d4910 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 61 73 73 65 LOCK ){. asse
d4920 72 74 28 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 rt( pLock->lockt
d4930 79 70 65 3d 3d 70 46 69 6c 65 2d 3e 6c 6f 63 6b ype==pFile->lock
d4940 74 79 70 65 20 29 3b 0a 20 20 20 20 53 69 6d 75 type );. Simu
d4950 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 lateIOErrorBenig
d4960 6e 28 31 29 3b 0a 20 20 20 20 53 69 6d 75 6c 61 n(1);. Simula
d4970 74 65 49 4f 45 72 72 6f 72 28 20 68 3d 28 2d 31 teIOError( h=(-1
d4980 29 20 29 0a 20 20 20 20 53 69 6d 75 6c 61 74 65 ) ). Simulate
d4990 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 30 29 IOErrorBenign(0)
d49a0 3b 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 ;..#ifndef NDEBU
d49b0 47 0a 20 20 20 20 2f 2a 20 57 68 65 6e 20 72 65 G. /* When re
d49c0 64 75 63 69 6e 67 20 61 20 6c 6f 63 6b 20 73 75 ducing a lock su
d49d0 63 68 20 74 68 61 74 20 6f 74 68 65 72 20 70 72 ch that other pr
d49e0 6f 63 65 73 73 65 73 20 63 61 6e 20 73 74 61 72 ocesses can star
d49f0 74 0a 20 20 20 20 2a 2a 20 72 65 61 64 69 6e 67 t. ** reading
d4a00 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
d4a10 6c 65 20 61 67 61 69 6e 2c 20 6d 61 6b 65 20 73 le again, make s
d4a20 75 72 65 20 74 68 61 74 20 74 68 65 0a 20 20 20 ure that the.
d4a30 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 ** transaction
d4a40 63 6f 75 6e 74 65 72 20 77 61 73 20 75 70 64 61 counter was upda
d4a50 74 65 64 20 69 66 20 61 6e 79 20 70 61 72 74 20 ted if any part
d4a60 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 0a of the database.
d4a70 20 20 20 20 2a 2a 20 66 69 6c 65 20 63 68 61 6e ** file chan
d4a80 67 65 64 2e 20 20 49 66 20 74 68 65 20 74 72 61 ged. If the tra
d4a90 6e 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74 65 72 nsaction counter
d4aa0 20 69 73 20 6e 6f 74 20 75 70 64 61 74 65 64 2c is not updated,
d4ab0 0a 20 20 20 20 2a 2a 20 6f 74 68 65 72 20 63 6f . ** other co
d4ac0 6e 6e 65 63 74 69 6f 6e 73 20 74 6f 20 74 68 65 nnections to the
d4ad0 20 73 61 6d 65 20 66 69 6c 65 20 6d 69 67 68 74 same file might
d4ae0 20 6e 6f 74 20 72 65 61 6c 69 7a 65 20 74 68 61 not realize tha
d4af0 74 0a 20 20 20 20 2a 2a 20 74 68 65 20 66 69 6c t. ** the fil
d4b00 65 20 68 61 73 20 63 68 61 6e 67 65 64 20 61 6e e has changed an
d4b10 64 20 68 65 6e 63 65 20 6d 69 67 68 74 20 6e 6f d hence might no
d4b20 74 20 6b 6e 6f 77 20 74 6f 20 66 6c 75 73 68 20 t know to flush
d4b30 74 68 65 69 72 0a 20 20 20 20 2a 2a 20 63 61 63 their. ** cac
d4b40 68 65 2e 20 20 54 68 65 20 75 73 65 20 6f 66 20 he. The use of
d4b50 61 20 73 74 61 6c 65 20 63 61 63 68 65 20 63 61 a stale cache ca
d4b60 6e 20 6c 65 61 64 20 74 6f 20 64 61 74 61 62 61 n lead to databa
d4b70 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 2e 0a 20 se corruption..
d4b80 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 */. assert
d4b90 28 20 70 46 69 6c 65 2d 3e 69 6e 4e 6f 72 6d 61 ( pFile->inNorma
d4ba0 6c 57 72 69 74 65 3d 3d 30 0a 20 20 20 20 20 20 lWrite==0.
d4bb0 20 20 20 7c 7c 20 70 46 69 6c 65 2d 3e 64 62 55 || pFile->dbU
d4bc0 70 64 61 74 65 3d 3d 30 0a 20 20 20 20 20 20 20 pdate==0.
d4bd0 20 20 7c 7c 20 70 46 69 6c 65 2d 3e 74 72 61 6e || pFile->tran
d4be0 73 43 6e 74 72 43 68 6e 67 3d 3d 31 20 29 3b 0a sCntrChng==1 );.
d4bf0 20 20 20 20 70 46 69 6c 65 2d 3e 69 6e 4e 6f 72 pFile->inNor
d4c00 6d 61 6c 57 72 69 74 65 20 3d 20 30 3b 0a 23 65 malWrite = 0;.#e
d4c10 6e 64 69 66 0a 0a 0a 20 20 20 20 69 66 28 20 6c ndif... if( l
d4c20 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f ocktype==SHARED_
d4c30 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 20 20 69 66 LOCK ){. if
d4c40 28 20 72 61 6e 67 65 4c 6f 63 6b 28 70 46 69 6c ( rangeLock(pFil
d4c50 65 2c 20 46 5f 52 44 4c 43 4b 2c 20 26 74 45 72 e, F_RDLCK, &tEr
d4c60 72 6e 6f 29 3d 3d 28 2d 31 29 20 29 7b 0a 20 20 rno)==(-1) ){.
d4c70 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
d4c80 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 eErrorFromPosixE
d4c90 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c rror(tErrno, SQL
d4ca0 49 54 45 5f 49 4f 45 52 52 5f 52 44 4c 4f 43 4b ITE_IOERR_RDLOCK
d4cb0 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 49 );. if( I
d4cc0 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 S_LOCK_ERROR(rc)
d4cd0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 46 ){. pF
d4ce0 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d ile->lastErrno =
d4cf0 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 tErrno;.
d4d00 20 7d 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 }. goto
d4d10 65 6e 64 5f 75 6e 6c 6f 63 6b 3b 0a 20 20 20 20 end_unlock;.
d4d20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 6c 6f }. }. lo
d4d30 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 55 4e ck.l_type = F_UN
d4d40 4c 43 4b 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f LCK;. lock.l_
d4d50 77 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45 whence = SEEK_SE
d4d60 54 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 T;. lock.l_st
d4d70 61 72 74 20 3d 20 50 45 4e 44 49 4e 47 5f 42 59 art = PENDING_BY
d4d80 54 45 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c TE;. lock.l_l
d4d90 65 6e 20 3d 20 32 4c 3b 20 20 61 73 73 65 72 74 en = 2L; assert
d4da0 28 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 2b 31 ( PENDING_BYTE+1
d4db0 3d 3d 52 45 53 45 52 56 45 44 5f 42 59 54 45 20 ==RESERVED_BYTE
d4dc0 29 3b 0a 20 20 20 20 69 66 28 20 66 63 6e 74 6c );. if( fcntl
d4dd0 28 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f (h, F_SETLK, &lo
d4de0 63 6b 29 21 3d 28 2d 31 29 20 29 7b 0a 20 20 20 ck)!=(-1) ){.
d4df0 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 pLock->lockty
d4e00 70 65 20 3d 20 53 48 41 52 45 44 5f 4c 4f 43 4b pe = SHARED_LOCK
d4e10 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
d4e20 20 20 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e tErrno = errn
d4e30 6f 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 o;. rc = sq
d4e40 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 liteErrorFromPos
d4e50 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 ixError(tErrno,
d4e60 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c SQLITE_IOERR_UNL
d4e70 4f 43 4b 29 3b 0a 20 20 20 20 20 20 69 66 28 20 OCK);. if(
d4e80 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 IS_LOCK_ERROR(rc
d4e90 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69 ) ){. pFi
d4ea0 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 le->lastErrno =
d4eb0 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a tErrno;. }.
d4ec0 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 75 goto end_u
d4ed0 6e 6c 6f 63 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d nlock;. }. }
d4ee0 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d . if( locktype=
d4ef0 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 =NO_LOCK ){.
d4f00 73 74 72 75 63 74 20 75 6e 69 78 4f 70 65 6e 43 struct unixOpenC
d4f10 6e 74 20 2a 70 4f 70 65 6e 3b 0a 0a 20 20 20 20 nt *pOpen;..
d4f20 2f 2a 20 44 65 63 72 65 6d 65 6e 74 20 74 68 65 /* Decrement the
d4f30 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 63 6f 75 shared lock cou
d4f40 6e 74 65 72 2e 20 20 52 65 6c 65 61 73 65 20 74 nter. Release t
d4f50 68 65 20 6c 6f 63 6b 20 75 73 69 6e 67 20 61 6e he lock using an
d4f60 0a 20 20 20 20 2a 2a 20 4f 53 20 63 61 6c 6c 20 . ** OS call
d4f70 6f 6e 6c 79 20 77 68 65 6e 20 61 6c 6c 20 74 68 only when all th
d4f80 72 65 61 64 73 20 69 6e 20 74 68 69 73 20 73 61 reads in this sa
d4f90 6d 65 20 70 72 6f 63 65 73 73 20 68 61 76 65 20 me process have
d4fa0 72 65 6c 65 61 73 65 64 0a 20 20 20 20 2a 2a 20 released. **
d4fb0 74 68 65 20 6c 6f 63 6b 2e 0a 20 20 20 20 2a 2f the lock.. */
d4fc0 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 63 6e 74 2d . pLock->cnt-
d4fd0 2d 3b 0a 20 20 20 20 69 66 28 20 70 4c 6f 63 6b -;. if( pLock
d4fe0 2d 3e 63 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20 ->cnt==0 ){.
d4ff0 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 lock.l_type =
d5000 46 5f 55 4e 4c 43 4b 3b 0a 20 20 20 20 20 20 6c F_UNLCK;. l
d5010 6f 63 6b 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 53 ock.l_whence = S
d5020 45 45 4b 5f 53 45 54 3b 0a 20 20 20 20 20 20 6c EEK_SET;. l
d5030 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 6c 6f ock.l_start = lo
d5040 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 30 4c 3b 0a 20 ck.l_len = 0L;.
d5050 20 20 20 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 SimulateIOE
d5060 72 72 6f 72 42 65 6e 69 67 6e 28 31 29 3b 0a 20 rrorBenign(1);.
d5070 20 20 20 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 SimulateIOE
d5080 72 72 6f 72 28 20 68 3d 28 2d 31 29 20 29 0a 20 rror( h=(-1) ).
d5090 20 20 20 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 SimulateIOE
d50a0 72 72 6f 72 42 65 6e 69 67 6e 28 30 29 3b 0a 20 rrorBenign(0);.
d50b0 20 20 20 20 20 69 66 28 20 66 63 6e 74 6c 28 68 if( fcntl(h
d50c0 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b , F_SETLK, &lock
d50d0 29 21 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 20 )!=(-1) ){.
d50e0 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 pLock->lockty
d50f0 70 65 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 pe = NO_LOCK;.
d5100 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
d5110 20 20 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e tErrno = errn
d5120 6f 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 o;. rc =
d5130 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 sqliteErrorFromP
d5140 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f osixError(tErrno
d5150 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 , SQLITE_IOERR_U
d5160 4e 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 20 20 NLOCK);.
d5170 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f if( IS_LOCK_ERRO
d5180 52 28 72 63 29 20 29 7b 0a 20 20 20 20 20 20 20 R(rc) ){.
d5190 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 pFile->lastEr
d51a0 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 rno = tErrno;.
d51b0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
d51c0 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 20 pLock->locktype
d51d0 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 = NO_LOCK;.
d51e0 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 pFile->lockty
d51f0 70 65 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 pe = NO_LOCK;.
d5200 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 }. }..
d5210 20 2f 2a 20 44 65 63 72 65 6d 65 6e 74 20 74 68 /* Decrement th
d5220 65 20 63 6f 75 6e 74 20 6f 66 20 6c 6f 63 6b 73 e count of locks
d5230 20 61 67 61 69 6e 73 74 20 74 68 69 73 20 73 61 against this sa
d5240 6d 65 20 66 69 6c 65 2e 20 20 57 68 65 6e 20 74 me file. When t
d5250 68 65 0a 20 20 20 20 2a 2a 20 63 6f 75 6e 74 20 he. ** count
d5260 72 65 61 63 68 65 73 20 7a 65 72 6f 2c 20 63 6c reaches zero, cl
d5270 6f 73 65 20 61 6e 79 20 6f 74 68 65 72 20 66 69 ose any other fi
d5280 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73 20 77 le descriptors w
d5290 68 6f 73 65 20 63 6c 6f 73 65 0a 20 20 20 20 2a hose close. *
d52a0 2a 20 77 61 73 20 64 65 66 65 72 72 65 64 20 62 * was deferred b
d52b0 65 63 61 75 73 65 20 6f 66 20 6f 75 74 73 74 61 ecause of outsta
d52c0 6e 64 69 6e 67 20 6c 6f 63 6b 73 2e 0a 20 20 20 nding locks..
d52d0 20 2a 2f 0a 20 20 20 20 70 4f 70 65 6e 20 3d 20 */. pOpen =
d52e0 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 3b 0a 20 20 pFile->pOpen;.
d52f0 20 20 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 2d 2d pOpen->nLock--
d5300 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4f ;. assert( pO
d5310 70 65 6e 2d 3e 6e 4c 6f 63 6b 3e 3d 30 20 29 3b pen->nLock>=0 );
d5320 0a 20 20 20 20 69 66 28 20 70 4f 70 65 6e 2d 3e . if( pOpen->
d5330 6e 4c 6f 63 6b 3d 3d 30 20 29 7b 0a 20 20 20 20 nLock==0 ){.
d5340 20 20 69 6e 74 20 72 63 32 20 3d 20 63 6c 6f 73 int rc2 = clos
d5350 65 50 65 6e 64 69 6e 67 46 64 73 28 70 46 69 6c ePendingFds(pFil
d5360 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 e);. if( rc
d5370 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
d5380 20 20 20 20 20 20 20 72 63 20 3d 20 72 63 32 3b rc = rc2;
d5390 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
d53a0 20 7d 0a 09 0a 65 6e 64 5f 75 6e 6c 6f 63 6b 3a }...end_unlock:
d53b0 0a 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 . unixLeaveMute
d53c0 78 28 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 x();. if( rc==S
d53d0 51 4c 49 54 45 5f 4f 4b 20 29 20 70 46 69 6c 65 QLITE_OK ) pFile
d53e0 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 ->locktype = loc
d53f0 6b 74 79 70 65 3b 0a 20 20 72 65 74 75 72 6e 20 ktype;. return
d5400 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 rc;.}../*.** Thi
d5410 73 20 66 75 6e 63 74 69 6f 6e 20 70 65 72 66 6f s function perfo
d5420 72 6d 73 20 74 68 65 20 70 61 72 74 73 20 6f 66 rms the parts of
d5430 20 74 68 65 20 22 63 6c 6f 73 65 20 66 69 6c 65 the "close file
d5440 22 20 6f 70 65 72 61 74 69 6f 6e 20 0a 2a 2a 20 " operation .**
d5450 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 6c 6f common to all lo
d5460 63 6b 69 6e 67 20 73 63 68 65 6d 65 73 2e 20 49 cking schemes. I
d5470 74 20 63 6c 6f 73 65 73 20 74 68 65 20 64 69 72 t closes the dir
d5480 65 63 74 6f 72 79 20 61 6e 64 20 66 69 6c 65 0a ectory and file.
d5490 2a 2a 20 68 61 6e 64 6c 65 73 2c 20 69 66 20 74 ** handles, if t
d54a0 68 65 79 20 61 72 65 20 76 61 6c 69 64 2c 20 61 hey are valid, a
d54b0 6e 64 20 73 65 74 73 20 61 6c 6c 20 66 69 65 6c nd sets all fiel
d54c0 64 73 20 6f 66 20 74 68 65 20 75 6e 69 78 46 69 ds of the unixFi
d54d0 6c 65 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20 le.** structure
d54e0 74 6f 20 30 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 to 0..**.** It i
d54f0 73 20 2a 6e 6f 74 2a 20 6e 65 63 65 73 73 61 72 s *not* necessar
d5500 79 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 6d 75 y to hold the mu
d5510 74 65 78 20 77 68 65 6e 20 74 68 69 73 20 72 6f tex when this ro
d5520 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c utine is called,
d5530 0a 2a 2a 20 65 76 65 6e 20 6f 6e 20 56 78 57 6f .** even on VxWo
d5540 72 6b 73 2e 20 20 41 20 6d 75 74 65 78 20 77 69 rks. A mutex wi
d5550 6c 6c 20 62 65 20 61 63 71 75 69 72 65 64 20 6f ll be acquired o
d5560 6e 20 56 78 57 6f 72 6b 73 20 62 79 20 74 68 65 n VxWorks by the
d5570 0a 2a 2a 20 76 78 77 6f 72 6b 73 52 65 6c 65 61 .** vxworksRelea
d5580 73 65 46 69 6c 65 49 64 28 29 20 72 6f 75 74 69 seFileId() routi
d5590 6e 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ne..*/.static in
d55a0 74 20 63 6c 6f 73 65 55 6e 69 78 46 69 6c 65 28 t closeUnixFile(
d55b0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
d55c0 29 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 ){. unixFile *p
d55d0 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 File = (unixFile
d55e0 2a 29 69 64 3b 0a 20 20 69 66 28 20 70 46 69 6c *)id;. if( pFil
d55f0 65 20 29 7b 0a 20 20 20 20 69 66 28 20 70 46 69 e ){. if( pFi
d5600 6c 65 2d 3e 64 69 72 66 64 3e 3d 30 20 29 7b 0a le->dirfd>=0 ){.
d5610 20 20 20 20 20 20 69 6e 74 20 65 72 72 20 3d 20 int err =
d5620 63 6c 6f 73 65 28 70 46 69 6c 65 2d 3e 64 69 72 close(pFile->dir
d5630 66 64 29 3b 0a 20 20 20 20 20 20 69 66 28 20 65 fd);. if( e
d5640 72 72 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 rr ){. pF
d5650 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d ile->lastErrno =
d5660 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 errno;.
d5670 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f return SQLITE_IO
d5680 45 52 52 5f 44 49 52 5f 43 4c 4f 53 45 3b 0a 20 ERR_DIR_CLOSE;.
d5690 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
d56a0 20 20 20 20 70 46 69 6c 65 2d 3e 64 69 72 66 64 pFile->dirfd
d56b0 3d 2d 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 =-1;. }.
d56c0 20 7d 0a 20 20 20 20 69 66 28 20 70 46 69 6c 65 }. if( pFile
d56d0 2d 3e 68 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 ->h>=0 ){.
d56e0 69 6e 74 20 65 72 72 20 3d 20 63 6c 6f 73 65 28 int err = close(
d56f0 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 20 20 20 20 pFile->h);.
d5700 20 69 66 28 20 65 72 72 20 29 7b 0a 20 20 20 20 if( err ){.
d5710 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 pFile->lastE
d5720 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 rrno = errno;.
d5730 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
d5740 49 54 45 5f 49 4f 45 52 52 5f 43 4c 4f 53 45 3b ITE_IOERR_CLOSE;
d5750 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 . }. }.#
d5760 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 if OS_VXWORKS.
d5770 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 70 49 64 if( pFile->pId
d5780 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 46 ){. if( pF
d5790 69 6c 65 2d 3e 69 73 44 65 6c 65 74 65 20 29 7b ile->isDelete ){
d57a0 0a 20 20 20 20 20 20 20 20 75 6e 6c 69 6e 6b 28 . unlink(
d57b0 70 46 69 6c 65 2d 3e 70 49 64 2d 3e 7a 43 61 6e pFile->pId->zCan
d57c0 6f 6e 69 63 61 6c 4e 61 6d 65 29 3b 0a 20 20 20 onicalName);.
d57d0 20 20 20 7d 0a 20 20 20 20 20 20 76 78 77 6f 72 }. vxwor
d57e0 6b 73 52 65 6c 65 61 73 65 46 69 6c 65 49 64 28 ksReleaseFileId(
d57f0 70 46 69 6c 65 2d 3e 70 49 64 29 3b 0a 20 20 20 pFile->pId);.
d5800 20 20 20 70 46 69 6c 65 2d 3e 70 49 64 20 3d 20 pFile->pId =
d5810 30 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0;. }.#endif.
d5820 20 20 20 20 4f 53 54 52 41 43 45 32 28 22 43 4c OSTRACE2("CL
d5830 4f 53 45 20 20 20 25 2d 33 64 5c 6e 22 2c 20 70 OSE %-3d\n", p
d5840 46 69 6c 65 2d 3e 68 29 3b 0a 20 20 20 20 4f 70 File->h);. Op
d5850 65 6e 43 6f 75 6e 74 65 72 28 2d 31 29 3b 0a 20 enCounter(-1);.
d5860 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 sqlite3_free(
d5870 70 46 69 6c 65 2d 3e 70 55 6e 75 73 65 64 29 3b pFile->pUnused);
d5880 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 46 69 6c . memset(pFil
d5890 65 2c 20 30 2c 20 73 69 7a 65 6f 66 28 75 6e 69 e, 0, sizeof(uni
d58a0 78 46 69 6c 65 29 29 3b 0a 20 20 7d 0a 20 20 72 xFile));. }. r
d58b0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
d58c0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 .}../*.** Close
d58d0 61 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 a file..*/.stati
d58e0 63 20 69 6e 74 20 75 6e 69 78 43 6c 6f 73 65 28 c int unixClose(
d58f0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
d5900 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 ){. int rc = SQ
d5910 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 69 LITE_OK;. if( i
d5920 64 20 29 7b 0a 20 20 20 20 75 6e 69 78 46 69 6c d ){. unixFil
d5930 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 e *pFile = (unix
d5940 46 69 6c 65 20 2a 29 69 64 3b 0a 20 20 20 20 75 File *)id;. u
d5950 6e 69 78 55 6e 6c 6f 63 6b 28 69 64 2c 20 4e 4f nixUnlock(id, NO
d5960 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 75 6e 69 78 _LOCK);. unix
d5970 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 EnterMutex();.
d5980 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 70 4f 70 if( pFile->pOp
d5990 65 6e 20 26 26 20 70 46 69 6c 65 2d 3e 70 4f 70 en && pFile->pOp
d59a0 65 6e 2d 3e 6e 4c 6f 63 6b 20 29 7b 0a 20 20 20 en->nLock ){.
d59b0 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61 /* If there a
d59c0 72 65 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 6c re outstanding l
d59d0 6f 63 6b 73 2c 20 64 6f 20 6e 6f 74 20 61 63 74 ocks, do not act
d59e0 75 61 6c 6c 79 20 63 6c 6f 73 65 20 74 68 65 20 ually close the
d59f0 66 69 6c 65 20 6a 75 73 74 0a 20 20 20 20 20 20 file just.
d5a00 2a 2a 20 79 65 74 20 62 65 63 61 75 73 65 20 74 ** yet because t
d5a10 68 61 74 20 77 6f 75 6c 64 20 63 6c 65 61 72 20 hat would clear
d5a20 74 68 6f 73 65 20 6c 6f 63 6b 73 2e 20 20 49 6e those locks. In
d5a30 73 74 65 61 64 2c 20 61 64 64 20 74 68 65 20 66 stead, add the f
d5a40 69 6c 65 0a 20 20 20 20 20 20 2a 2a 20 64 65 73 ile. ** des
d5a50 63 72 69 70 74 6f 72 20 74 6f 20 70 4f 70 65 6e criptor to pOpen
d5a60 2d 3e 70 55 6e 75 73 65 64 20 6c 69 73 74 2e 20 ->pUnused list.
d5a70 20 49 74 20 77 69 6c 6c 20 62 65 20 61 75 74 6f It will be auto
d5a80 6d 61 74 69 63 61 6c 6c 79 20 63 6c 6f 73 65 64 matically closed
d5a90 20 0a 20 20 20 20 20 20 2a 2a 20 77 68 65 6e 20 . ** when
d5aa0 74 68 65 20 6c 61 73 74 20 6c 6f 63 6b 20 69 73 the last lock is
d5ab0 20 63 6c 65 61 72 65 64 2e 0a 20 20 20 20 20 20 cleared..
d5ac0 2a 2f 0a 20 20 20 20 20 20 73 65 74 50 65 6e 64 */. setPend
d5ad0 69 6e 67 46 64 28 70 46 69 6c 65 29 3b 0a 20 20 ingFd(pFile);.
d5ae0 20 20 7d 0a 20 20 20 20 72 65 6c 65 61 73 65 4c }. releaseL
d5af0 6f 63 6b 49 6e 66 6f 28 70 46 69 6c 65 2d 3e 70 ockInfo(pFile->p
d5b00 4c 6f 63 6b 29 3b 0a 20 20 20 20 72 65 6c 65 61 Lock);. relea
d5b10 73 65 4f 70 65 6e 43 6e 74 28 70 46 69 6c 65 2d seOpenCnt(pFile-
d5b20 3e 70 4f 70 65 6e 29 3b 0a 20 20 20 20 72 63 20 >pOpen);. rc
d5b30 3d 20 63 6c 6f 73 65 55 6e 69 78 46 69 6c 65 28 = closeUnixFile(
d5b40 69 64 29 3b 0a 20 20 20 20 75 6e 69 78 4c 65 61 id);. unixLea
d5b50 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 7d 0a 20 veMutex();. }.
d5b60 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
d5b70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
d5b80 6e 64 20 6f 66 20 74 68 65 20 70 6f 73 69 78 20 nd of the posix
d5b90 61 64 76 69 73 6f 72 79 20 6c 6f 63 6b 20 69 6d advisory lock im
d5ba0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a plementation ***
d5bb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
d5bc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5bd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5be0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5bf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5c00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a *************/..
d5c10 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /***************
d5c20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5c30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5c40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5c50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
d5c60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5c70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 4e ************** N
d5c80 6f 2d 6f 70 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a o-op Locking ***
d5c90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
d5cb0 2a 2a 0a 2a 2a 20 4f 66 20 74 68 65 20 76 61 72 **.** Of the var
d5cc0 69 6f 75 73 20 6c 6f 63 6b 69 6e 67 20 69 6d 70 ious locking imp
d5cd0 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 61 76 61 lementations ava
d5ce0 69 6c 61 62 6c 65 2c 20 74 68 69 73 20 69 73 20 ilable, this is
d5cf0 62 79 20 66 61 72 20 74 68 65 0a 2a 2a 20 73 69 by far the.** si
d5d00 6d 70 6c 65 73 74 3a 20 20 6c 6f 63 6b 69 6e 67 mplest: locking
d5d10 20 69 73 20 69 67 6e 6f 72 65 64 2e 20 20 4e 6f is ignored. No
d5d20 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65 attempt is made
d5d30 20 74 6f 20 6c 6f 63 6b 20 74 68 65 20 64 61 74 to lock the dat
d5d40 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 20 66 6f abase.** file fo
d5d50 72 20 72 65 61 64 69 6e 67 20 6f 72 20 77 72 69 r reading or wri
d5d60 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 ting..**.** This
d5d70 20 6c 6f 63 6b 69 6e 67 20 6d 6f 64 65 20 69 73 locking mode is
d5d80 20 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72 appropriate for
d5d90 20 75 73 65 20 6f 6e 20 72 65 61 64 2d 6f 6e 6c use on read-onl
d5da0 79 20 64 61 74 61 62 61 73 65 73 0a 2a 2a 20 28 y databases.** (
d5db0 65 78 3a 20 64 61 74 61 62 61 73 65 73 20 74 68 ex: databases th
d5dc0 61 74 20 61 72 65 20 62 75 72 6e 65 64 20 69 6e at are burned in
d5dd0 74 6f 20 43 44 2d 52 4f 4d 2c 20 66 6f 72 20 65 to CD-ROM, for e
d5de0 78 61 6d 70 6c 65 2e 29 20 20 49 74 20 63 61 6e xample.) It can
d5df0 0a 2a 2a 20 61 6c 73 6f 20 62 65 20 75 73 65 64 .** also be used
d5e00 20 69 66 20 74 68 65 20 61 70 70 6c 69 63 61 74 if the applicat
d5e10 69 6f 6e 20 65 6d 70 6c 6f 79 73 20 73 6f 6d 65 ion employs some
d5e20 20 65 78 74 65 72 6e 61 6c 20 6d 65 63 68 61 6e external mechan
d5e30 69 73 6d 20 74 6f 0a 2a 2a 20 70 72 65 76 65 6e ism to.** preven
d5e40 74 20 73 69 6d 75 6c 74 61 6e 65 6f 75 73 20 61 t simultaneous a
d5e50 63 63 65 73 73 20 6f 66 20 74 68 65 20 73 61 6d ccess of the sam
d5e60 65 20 64 61 74 61 62 61 73 65 20 62 79 20 74 77 e database by tw
d5e70 6f 20 6f 72 20 6d 6f 72 65 0a 2a 2a 20 64 61 74 o or more.** dat
d5e80 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
d5e90 73 2e 20 20 42 75 74 20 74 68 65 72 65 20 69 73 s. But there is
d5ea0 20 61 20 73 65 72 69 6f 75 73 20 72 69 73 6b 20 a serious risk
d5eb0 6f 66 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 of database.** c
d5ec0 6f 72 72 75 70 74 69 6f 6e 20 69 66 20 74 68 69 orruption if thi
d5ed0 73 20 6c 6f 63 6b 69 6e 67 20 6d 6f 64 65 20 69 s locking mode i
d5ee0 73 20 75 73 65 64 20 69 6e 20 73 69 74 75 61 74 s used in situat
d5ef0 69 6f 6e 73 20 77 68 65 72 65 20 6d 75 6c 74 69 ions where multi
d5f00 70 6c 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 ple.** database
d5f10 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 61 72 65 20 connections are
d5f20 61 63 63 65 73 73 69 6e 67 20 74 68 65 20 73 61 accessing the sa
d5f30 6d 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 me database file
d5f40 20 61 74 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 at the same.**
d5f50 74 69 6d 65 20 61 6e 64 20 6f 6e 65 20 6f 72 20 time and one or
d5f60 6d 6f 72 65 20 6f 66 20 74 68 6f 73 65 20 63 6f more of those co
d5f70 6e 6e 65 63 74 69 6f 6e 73 20 61 72 65 20 77 72 nnections are wr
d5f80 69 74 69 6e 67 2e 0a 2a 2f 0a 0a 73 74 61 74 69 iting..*/..stati
d5f90 63 20 69 6e 74 20 6e 6f 6c 6f 63 6b 43 68 65 63 c int nolockChec
d5fa0 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 kReservedLock(sq
d5fb0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 4e 6f 74 55 lite3_file *NotU
d5fc0 73 65 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 sed, int *pResOu
d5fd0 74 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 t){. UNUSED_PAR
d5fe0 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b AMETER(NotUsed);
d5ff0 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d 20 30 3b . *pResOut = 0;
d6000 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
d6010 5f 4f 4b 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e _OK;.}.static in
d6020 74 20 6e 6f 6c 6f 63 6b 4c 6f 63 6b 28 73 71 6c t nolockLock(sql
d6030 69 74 65 33 5f 66 69 6c 65 20 2a 4e 6f 74 55 73 ite3_file *NotUs
d6040 65 64 2c 20 69 6e 74 20 4e 6f 74 55 73 65 64 32 ed, int NotUsed2
d6050 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 ){. UNUSED_PARA
d6060 4d 45 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 METER2(NotUsed,
d6070 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 72 65 74 NotUsed2);. ret
d6080 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
d6090 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 6f 6c 6f .static int nolo
d60a0 63 6b 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 ckUnlock(sqlite3
d60b0 5f 66 69 6c 65 20 2a 4e 6f 74 55 73 65 64 2c 20 _file *NotUsed,
d60c0 69 6e 74 20 4e 6f 74 55 73 65 64 32 29 7b 0a 20 int NotUsed2){.
d60d0 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
d60e0 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 R2(NotUsed, NotU
d60f0 73 65 64 32 29 3b 0a 20 20 72 65 74 75 72 6e 20 sed2);. return
d6100 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a SQLITE_OK;.}../*
d6110 0a 2a 2a 20 43 6c 6f 73 65 20 74 68 65 20 66 69 .** Close the fi
d6120 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e le..*/.static in
d6130 74 20 6e 6f 6c 6f 63 6b 43 6c 6f 73 65 28 73 71 t nolockClose(sq
d6140 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 20 lite3_file *id)
d6150 7b 0a 20 20 72 65 74 75 72 6e 20 63 6c 6f 73 65 {. return close
d6160 55 6e 69 78 46 69 6c 65 28 69 64 29 3b 0a 7d 0a UnixFile(id);.}.
d6170 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
d6180 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 74 68 65 ***** End of the
d6190 20 6e 6f 2d 6f 70 20 6c 6f 63 6b 20 69 6d 70 6c no-op lock impl
d61a0 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a ementation *****
d61b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d61c0 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
d61d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d61e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d61f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d6200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
d6210 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
d6220 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d6230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d6240 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d6250 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d6260 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *.**************
d6270 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
d6280 6e 20 64 6f 74 2d 66 69 6c 65 20 4c 6f 63 6b 69 n dot-file Locki
d6290 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ng *************
d62a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d62b0 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 6f 74 66 *.**.** The dotf
d62c0 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 69 6d 70 6c ile locking impl
d62d0 65 6d 65 6e 74 61 74 69 6f 6e 20 75 73 65 73 20 ementation uses
d62e0 74 68 65 20 65 78 69 73 74 61 6e 63 65 20 6f 66 the existance of
d62f0 20 73 65 70 61 72 61 74 65 20 6c 6f 63 6b 0a 2a separate lock.*
d6300 2a 20 66 69 6c 65 73 20 69 6e 20 6f 72 64 65 72 * files in order
d6310 20 74 6f 20 63 6f 6e 74 72 6f 6c 20 61 63 63 65 to control acce
d6320 73 73 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 ss to the databa
d6330 73 65 2e 20 20 54 68 69 73 20 77 6f 72 6b 73 20 se. This works
d6340 6f 6e 20 6a 75 73 74 0a 2a 2a 20 61 62 6f 75 74 on just.** about
d6350 20 65 76 65 72 79 20 66 69 6c 65 73 79 73 74 65 every filesyste
d6360 6d 20 69 6d 61 67 69 6e 61 62 6c 65 2e 20 20 42 m imaginable. B
d6370 75 74 20 74 68 65 72 65 20 61 72 65 20 73 65 72 ut there are ser
d6380 69 6f 75 73 20 64 6f 77 6e 73 69 64 65 73 3a 0a ious downsides:.
d6390 2a 2a 0a 2a 2a 20 20 20 20 28 31 29 20 20 54 68 **.** (1) Th
d63a0 65 72 65 20 69 73 20 7a 65 72 6f 20 63 6f 6e 63 ere is zero conc
d63b0 75 72 72 65 6e 63 79 2e 20 20 41 20 73 69 6e 67 urrency. A sing
d63c0 6c 65 20 72 65 61 64 65 72 20 62 6c 6f 63 6b 73 le reader blocks
d63d0 20 61 6c 6c 20 6f 74 68 65 72 0a 2a 2a 20 20 20 all other.**
d63e0 20 20 20 20 20 20 63 6f 6e 6e 65 63 74 69 6f 6e connection
d63f0 73 20 66 72 6f 6d 20 72 65 61 64 69 6e 67 20 6f s from reading o
d6400 72 20 77 72 69 74 69 6e 67 20 74 68 65 20 64 61 r writing the da
d6410 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 tabase..**.**
d6420 20 28 32 29 20 20 41 6e 20 61 70 70 6c 69 63 61 (2) An applica
d6430 74 69 6f 6e 20 63 72 61 73 68 20 6f 72 20 70 6f tion crash or po
d6440 77 65 72 20 6c 6f 73 73 20 63 61 6e 20 6c 65 61 wer loss can lea
d6450 76 65 20 73 74 61 6c 65 20 6c 6f 63 6b 20 66 69 ve stale lock fi
d6460 6c 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 73 les.** s
d6470 69 74 74 69 6e 67 20 61 72 6f 75 6e 64 20 74 68 itting around th
d6480 61 74 20 6e 65 65 64 20 74 6f 20 62 65 20 63 6c at need to be cl
d6490 65 61 72 65 64 20 6d 61 6e 75 61 6c 6c 79 2e 0a eared manually..
d64a0 2a 2a 0a 2a 2a 20 4e 65 76 65 72 74 68 65 6c 65 **.** Neverthele
d64b0 73 73 2c 20 61 20 64 6f 74 6c 6f 63 6b 20 69 73 ss, a dotlock is
d64c0 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20 an appropriate
d64d0 6c 6f 63 6b 69 6e 67 20 6d 6f 64 65 20 66 6f 72 locking mode for
d64e0 20 75 73 65 20 69 66 20 6e 6f 0a 2a 2a 20 6f 74 use if no.** ot
d64f0 68 65 72 20 6c 6f 63 6b 69 6e 67 20 73 74 72 61 her locking stra
d6500 74 65 67 79 20 69 73 20 61 76 61 69 6c 61 62 6c tegy is availabl
d6510 65 2e 0a 2a 2a 0a 2a 2a 20 44 6f 74 66 69 6c 65 e..**.** Dotfile
d6520 20 6c 6f 63 6b 69 6e 67 20 77 6f 72 6b 73 20 62 locking works b
d6530 79 20 63 72 65 61 74 69 6e 67 20 61 20 66 69 6c y creating a fil
d6540 65 20 69 6e 20 74 68 65 20 73 61 6d 65 20 64 69 e in the same di
d6550 72 65 63 74 6f 72 79 20 61 73 20 74 68 65 0a 2a rectory as the.*
d6560 2a 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 77 * database and w
d6570 69 74 68 20 74 68 65 20 73 61 6d 65 20 6e 61 6d ith the same nam
d6580 65 20 62 75 74 20 77 69 74 68 20 61 20 22 2e 6c e but with a ".l
d6590 6f 63 6b 22 20 65 78 74 65 6e 73 69 6f 6e 20 61 ock" extension a
d65a0 64 64 65 64 2e 0a 2a 2a 20 54 68 65 20 65 78 69 dded..** The exi
d65b0 73 74 61 6e 63 65 20 6f 66 20 61 20 6c 6f 63 6b stance of a lock
d65c0 20 66 69 6c 65 20 69 6d 70 6c 69 65 73 20 61 6e file implies an
d65d0 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e EXCLUSIVE lock.
d65e0 20 20 41 6c 6c 20 6f 74 68 65 72 20 6c 6f 63 6b All other lock
d65f0 0a 2a 2a 20 74 79 70 65 73 20 28 53 48 41 52 45 .** types (SHARE
d6600 44 2c 20 52 45 53 45 52 56 45 44 2c 20 50 45 4e D, RESERVED, PEN
d6610 44 49 4e 47 29 20 61 72 65 20 6d 61 70 70 65 64 DING) are mapped
d6620 20 69 6e 74 6f 20 45 58 43 4c 55 53 49 56 45 2e into EXCLUSIVE.
d6630 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 .*/../*.** The f
d6640 69 6c 65 20 73 75 66 66 69 78 20 61 64 64 65 64 ile suffix added
d6650 20 74 6f 20 74 68 65 20 64 61 74 61 20 62 61 73 to the data bas
d6660 65 20 66 69 6c 65 6e 61 6d 65 20 69 6e 20 6f 72 e filename in or
d6670 64 65 72 20 74 6f 20 63 72 65 61 74 65 20 74 68 der to create th
d6680 65 0a 2a 2a 20 6c 6f 63 6b 20 66 69 6c 65 2e 0a e.** lock file..
d6690 2a 2f 0a 23 64 65 66 69 6e 65 20 44 4f 54 4c 4f */.#define DOTLO
d66a0 43 4b 5f 53 55 46 46 49 58 20 22 2e 6c 6f 63 6b CK_SUFFIX ".lock
d66b0 22 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f "../*.** This ro
d66c0 75 74 69 6e 65 20 63 68 65 63 6b 73 20 69 66 20 utine checks if
d66d0 74 68 65 72 65 20 69 73 20 61 20 52 45 53 45 52 there is a RESER
d66e0 56 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e VED lock held on
d66f0 20 74 68 65 20 73 70 65 63 69 66 69 65 64 0a 2a the specified.*
d6700 2a 20 66 69 6c 65 20 62 79 20 74 68 69 73 20 6f * file by this o
d6710 72 20 61 6e 79 20 6f 74 68 65 72 20 70 72 6f 63 r any other proc
d6720 65 73 73 2e 20 49 66 20 73 75 63 68 20 61 20 6c ess. If such a l
d6730 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20 73 65 74 ock is held, set
d6740 20 2a 70 52 65 73 4f 75 74 0a 2a 2a 20 74 6f 20 *pResOut.** to
d6750 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 a non-zero value
d6760 20 6f 74 68 65 72 77 69 73 65 20 2a 70 52 65 73 otherwise *pRes
d6770 4f 75 74 20 69 73 20 73 65 74 20 74 6f 20 7a 65 Out is set to ze
d6780 72 6f 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20 ro. The return
d6790 76 61 6c 75 65 0a 2a 2a 20 69 73 20 73 65 74 20 value.** is set
d67a0 74 6f 20 53 51 4c 49 54 45 5f 4f 4b 20 75 6e 6c to SQLITE_OK unl
d67b0 65 73 73 20 61 6e 20 49 2f 4f 20 65 72 72 6f 72 ess an I/O error
d67c0 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 6c occurs during l
d67d0 6f 63 6b 20 63 68 65 63 6b 69 6e 67 2e 0a 2a 2a ock checking..**
d67e0 0a 2a 2a 20 49 6e 20 64 6f 74 66 69 6c 65 20 6c .** In dotfile l
d67f0 6f 63 6b 69 6e 67 2c 20 65 69 74 68 65 72 20 61 ocking, either a
d6800 20 6c 6f 63 6b 20 65 78 69 73 74 73 20 6f 72 20 lock exists or
d6810 69 74 20 64 6f 65 73 20 6e 6f 74 2e 20 20 53 6f it does not. So
d6820 20 69 6e 20 74 68 69 73 0a 2a 2a 20 76 61 72 69 in this.** vari
d6830 61 74 69 6f 6e 20 6f 66 20 43 68 65 63 6b 52 65 ation of CheckRe
d6840 73 65 72 76 65 64 4c 6f 63 6b 28 29 2c 20 2a 70 servedLock(), *p
d6850 52 65 73 4f 75 74 20 69 73 20 73 65 74 20 74 6f ResOut is set to
d6860 20 74 72 75 65 20 69 66 20 61 6e 79 20 6c 6f 63 true if any loc
d6870 6b 0a 2a 2a 20 69 73 20 68 65 6c 64 20 6f 6e 20 k.** is held on
d6880 74 68 65 20 66 69 6c 65 20 61 6e 64 20 66 61 6c the file and fal
d6890 73 65 20 69 66 20 74 68 65 20 66 69 6c 65 20 69 se if the file i
d68a0 73 20 75 6e 6c 6f 63 6b 65 64 2e 0a 2a 2f 0a 73 s unlocked..*/.s
d68b0 74 61 74 69 63 20 69 6e 74 20 64 6f 74 6c 6f 63 tatic int dotloc
d68c0 6b 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f kCheckReservedLo
d68d0 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 ck(sqlite3_file
d68e0 2a 69 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 *id, int *pResOu
d68f0 74 29 20 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 t) {. int rc =
d6900 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 SQLITE_OK;. int
d6910 20 72 65 73 65 72 76 65 64 20 3d 20 30 3b 0a 20 reserved = 0;.
d6920 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 unixFile *pFile
d6930 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 = (unixFile*)id
d6940 3b 0a 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 ;.. SimulateIOE
d6950 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c rror( return SQL
d6960 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 ITE_IOERR_CHECKR
d6970 45 53 45 52 56 45 44 4c 4f 43 4b 3b 20 29 3b 0a ESERVEDLOCK; );.
d6980 20 20 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 . assert( pFi
d6990 6c 65 20 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 le );.. /* Chec
d69a0 6b 20 69 66 20 61 20 74 68 72 65 61 64 20 69 6e k if a thread in
d69b0 20 74 68 69 73 20 70 72 6f 63 65 73 73 20 68 6f this process ho
d69c0 6c 64 73 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 lds such a lock
d69d0 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e */. if( pFile->
d69e0 6c 6f 63 6b 74 79 70 65 3e 53 48 41 52 45 44 5f locktype>SHARED_
d69f0 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 2f 2a 20 45 LOCK ){. /* E
d6a00 69 74 68 65 72 20 74 68 69 73 20 63 6f 6e 6e 65 ither this conne
d6a10 63 74 69 6f 6e 20 6f 72 20 73 6f 6d 65 20 6f 74 ction or some ot
d6a20 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 her connection i
d6a30 6e 20 74 68 65 20 73 61 6d 65 20 70 72 6f 63 65 n the same proce
d6a40 73 73 0a 20 20 20 20 2a 2a 20 68 6f 6c 64 73 20 ss. ** holds
d6a50 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 66 69 a lock on the fi
d6a60 6c 65 2e 20 20 4e 6f 20 6e 65 65 64 20 74 6f 20 le. No need to
d6a70 63 68 65 63 6b 20 66 75 72 74 68 65 72 2e 20 2a check further. *
d6a80 2f 0a 20 20 20 20 72 65 73 65 72 76 65 64 20 3d /. reserved =
d6a90 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 1;. }else{.
d6aa0 20 2f 2a 20 54 68 65 20 6c 6f 63 6b 20 69 73 20 /* The lock is
d6ab0 68 65 6c 64 20 69 66 20 61 6e 64 20 6f 6e 6c 79 held if and only
d6ac0 20 69 66 20 74 68 65 20 6c 6f 63 6b 66 69 6c 65 if the lockfile
d6ad0 20 65 78 69 73 74 73 20 2a 2f 0a 20 20 20 20 63 exists */. c
d6ae0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4c 6f 63 6b onst char *zLock
d6af0 46 69 6c 65 20 3d 20 28 63 6f 6e 73 74 20 63 68 File = (const ch
d6b00 61 72 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 ar*)pFile->locki
d6b10 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 72 ngContext;. r
d6b20 65 73 65 72 76 65 64 20 3d 20 61 63 63 65 73 73 eserved = access
d6b30 28 7a 4c 6f 63 6b 46 69 6c 65 2c 20 30 29 3d 3d (zLockFile, 0)==
d6b40 30 3b 0a 20 20 7d 0a 20 20 4f 53 54 52 41 43 45 0;. }. OSTRACE
d6b50 34 28 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 4("TEST WR-LOCK
d6b60 25 64 20 25 64 20 25 64 5c 6e 22 2c 20 70 46 69 %d %d %d\n", pFi
d6b70 6c 65 2d 3e 68 2c 20 72 63 2c 20 72 65 73 65 72 le->h, rc, reser
d6b80 76 65 64 29 3b 0a 20 20 2a 70 52 65 73 4f 75 74 ved);. *pResOut
d6b90 20 3d 20 72 65 73 65 72 76 65 64 3b 0a 20 20 72 = reserved;. r
d6ba0 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
d6bb0 2a 2a 20 4c 6f 63 6b 20 74 68 65 20 66 69 6c 65 ** Lock the file
d6bc0 20 77 69 74 68 20 74 68 65 20 6c 6f 63 6b 20 73 with the lock s
d6bd0 70 65 63 69 66 69 65 64 20 62 79 20 70 61 72 61 pecified by para
d6be0 6d 65 74 65 72 20 6c 6f 63 6b 74 79 70 65 20 2d meter locktype -
d6bf0 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 65 20 66 one.** of the f
d6c00 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 ollowing:.**.**
d6c10 20 20 20 20 28 31 29 20 53 48 41 52 45 44 5f 4c (1) SHARED_L
d6c20 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 32 29 20 52 OCK.** (2) R
d6c30 45 53 45 52 56 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 ESERVED_LOCK.**
d6c40 20 20 20 20 28 33 29 20 50 45 4e 44 49 4e 47 5f (3) PENDING_
d6c50 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 34 29 20 LOCK.** (4)
d6c60 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 0a 2a EXCLUSIVE_LOCK.*
d6c70 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 20 77 *.** Sometimes w
d6c80 68 65 6e 20 72 65 71 75 65 73 74 69 6e 67 20 6f hen requesting o
d6c90 6e 65 20 6c 6f 63 6b 20 73 74 61 74 65 2c 20 61 ne lock state, a
d6ca0 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b 20 73 dditional lock s
d6cb0 74 61 74 65 73 0a 2a 2a 20 61 72 65 20 69 6e 73 tates.** are ins
d6cc0 65 72 74 65 64 20 69 6e 20 62 65 74 77 65 65 6e erted in between
d6cd0 2e 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20 6d . The locking m
d6ce0 69 67 68 74 20 66 61 69 6c 20 6f 6e 20 6f 6e 65 ight fail on one
d6cf0 20 6f 66 20 74 68 65 20 6c 61 74 65 72 0a 2a 2a of the later.**
d6d00 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 6c 65 61 transitions lea
d6d10 76 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 73 74 ving the lock st
d6d20 61 74 65 20 64 69 66 66 65 72 65 6e 74 20 66 72 ate different fr
d6d30 6f 6d 20 77 68 61 74 20 69 74 20 73 74 61 72 74 om what it start
d6d40 65 64 20 62 75 74 0a 2a 2a 20 73 74 69 6c 6c 20 ed but.** still
d6d50 73 68 6f 72 74 20 6f 66 20 69 74 73 20 67 6f 61 short of its goa
d6d60 6c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e l. The followin
d6d70 67 20 63 68 61 72 74 20 73 68 6f 77 73 20 74 68 g chart shows th
d6d80 65 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74 72 61 e allowed.** tra
d6d90 6e 73 69 74 69 6f 6e 73 20 61 6e 64 20 74 68 65 nsitions and the
d6da0 20 69 6e 73 65 72 74 65 64 20 69 6e 74 65 72 6d inserted interm
d6db0 65 64 69 61 74 65 20 73 74 61 74 65 73 3a 0a 2a ediate states:.*
d6dc0 2a 0a 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b 45 44 *.** UNLOCKED
d6dd0 20 2d 3e 20 53 48 41 52 45 44 0a 2a 2a 20 20 20 -> SHARED.**
d6de0 20 53 48 41 52 45 44 20 2d 3e 20 52 45 53 45 52 SHARED -> RESER
d6df0 56 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 VED.** SHARED
d6e00 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e -> (PENDING) ->
d6e10 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 EXCLUSIVE.**
d6e20 20 52 45 53 45 52 56 45 44 20 2d 3e 20 28 50 45 RESERVED -> (PE
d6e30 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 NDING) -> EXCLUS
d6e40 49 56 45 0a 2a 2a 20 20 20 20 50 45 4e 44 49 4e IVE.** PENDIN
d6e50 47 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a G -> EXCLUSIVE.*
d6e60 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
d6e70 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 69 6e 63 72 e will only incr
d6e80 65 61 73 65 20 61 20 6c 6f 63 6b 2e 20 20 55 73 ease a lock. Us
d6e90 65 20 74 68 65 20 73 71 6c 69 74 65 33 4f 73 55 e the sqlite3OsU
d6ea0 6e 6c 6f 63 6b 28 29 0a 2a 2a 20 72 6f 75 74 69 nlock().** routi
d6eb0 6e 65 20 74 6f 20 6c 6f 77 65 72 20 61 20 6c 6f ne to lower a lo
d6ec0 63 6b 69 6e 67 20 6c 65 76 65 6c 2e 0a 2a 2a 0a cking level..**.
d6ed0 2a 2a 20 57 69 74 68 20 64 6f 74 66 69 6c 65 20 ** With dotfile
d6ee0 6c 6f 63 6b 69 6e 67 2c 20 77 65 20 72 65 61 6c locking, we real
d6ef0 6c 79 20 6f 6e 6c 79 20 73 75 70 70 6f 72 74 20 ly only support
d6f00 73 74 61 74 65 20 28 34 29 3a 20 45 58 43 4c 55 state (4): EXCLU
d6f10 53 49 56 45 2e 0a 2a 2a 20 42 75 74 20 77 65 20 SIVE..** But we
d6f20 74 72 61 63 6b 20 74 68 65 20 6f 74 68 65 72 20 track the other
d6f30 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 73 20 69 locking levels i
d6f40 6e 74 65 72 6e 61 6c 6c 79 2e 0a 2a 2f 0a 73 74 nternally..*/.st
d6f50 61 74 69 63 20 69 6e 74 20 64 6f 74 6c 6f 63 6b atic int dotlock
d6f60 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c Lock(sqlite3_fil
d6f70 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 e *id, int lockt
d6f80 79 70 65 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c ype) {. unixFil
d6f90 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 e *pFile = (unix
d6fa0 46 69 6c 65 2a 29 69 64 3b 0a 20 20 69 6e 74 20 File*)id;. int
d6fb0 66 64 3b 0a 20 20 63 68 61 72 20 2a 7a 4c 6f 63 fd;. char *zLoc
d6fc0 6b 46 69 6c 65 20 3d 20 28 63 68 61 72 20 2a 29 kFile = (char *)
d6fd0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f pFile->lockingCo
d6fe0 6e 74 65 78 74 3b 0a 20 20 69 6e 74 20 72 63 20 ntext;. int rc
d6ff0 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 0a 20 = SQLITE_OK;...
d7000 20 2f 2a 20 49 66 20 77 65 20 68 61 76 65 20 61 /* If we have a
d7010 6e 79 20 6c 6f 63 6b 2c 20 74 68 65 6e 20 74 68 ny lock, then th
d7020 65 20 6c 6f 63 6b 20 66 69 6c 65 20 61 6c 72 65 e lock file alre
d7030 61 64 79 20 65 78 69 73 74 73 2e 20 20 41 6c 6c ady exists. All
d7040 20 77 65 20 68 61 76 65 0a 20 20 2a 2a 20 74 6f we have. ** to
d7050 20 64 6f 20 69 73 20 61 64 6a 75 73 74 20 6f 75 do is adjust ou
d7060 72 20 69 6e 74 65 72 6e 61 6c 20 72 65 63 6f 72 r internal recor
d7070 64 20 6f 66 20 74 68 65 20 6c 6f 63 6b 20 6c 65 d of the lock le
d7080 76 65 6c 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 vel.. */. if(
d7090 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 pFile->locktype
d70a0 3e 20 4e 4f 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 > NO_LOCK ){.
d70b0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
d70c0 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 23 69 66 = locktype;.#if
d70d0 20 21 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 20 !OS_VXWORKS.
d70e0 20 2f 2a 20 41 6c 77 61 79 73 20 75 70 64 61 74 /* Always updat
d70f0 65 20 74 68 65 20 74 69 6d 65 73 74 61 6d 70 20 e the timestamp
d7100 6f 6e 20 74 68 65 20 6f 6c 64 20 66 69 6c 65 20 on the old file
d7110 2a 2f 0a 20 20 20 20 75 74 69 6d 65 73 28 7a 4c */. utimes(zL
d7120 6f 63 6b 46 69 6c 65 2c 20 4e 55 4c 4c 29 3b 0a ockFile, NULL);.
d7130 23 65 6e 64 69 66 0a 20 20 20 20 72 65 74 75 72 #endif. retur
d7140 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d n SQLITE_OK;. }
d7150 0a 20 20 0a 20 20 2f 2a 20 67 72 61 62 20 61 6e . . /* grab an
d7160 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 exclusive lock
d7170 2a 2f 0a 20 20 66 64 20 3d 20 6f 70 65 6e 28 7a */. fd = open(z
d7180 4c 6f 63 6b 46 69 6c 65 2c 4f 5f 52 44 4f 4e 4c LockFile,O_RDONL
d7190 59 7c 4f 5f 43 52 45 41 54 7c 4f 5f 45 58 43 4c Y|O_CREAT|O_EXCL
d71a0 2c 30 36 30 30 29 3b 0a 20 20 69 66 28 20 66 64 ,0600);. if( fd
d71b0 3c 30 20 29 7b 0a 20 20 20 20 2f 2a 20 66 61 69 <0 ){. /* fai
d71c0 6c 65 64 20 74 6f 20 6f 70 65 6e 2f 63 72 65 61 led to open/crea
d71d0 74 65 20 74 68 65 20 66 69 6c 65 2c 20 73 6f 6d te the file, som
d71e0 65 6f 6e 65 20 65 6c 73 65 20 6d 61 79 20 68 61 eone else may ha
d71f0 76 65 20 73 74 6f 6c 65 6e 20 74 68 65 20 6c 6f ve stolen the lo
d7200 63 6b 20 2a 2f 0a 20 20 20 20 69 6e 74 20 74 45 ck */. int tE
d7210 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 rrno = errno;.
d7220 20 20 69 66 28 20 45 45 58 49 53 54 20 3d 3d 20 if( EEXIST ==
d7230 74 45 72 72 6e 6f 20 29 7b 0a 20 20 20 20 20 20 tErrno ){.
d7240 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 rc = SQLITE_BUSY
d7250 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 ;. } else {.
d7260 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
d7270 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 ErrorFromPosixEr
d7280 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 ror(tErrno, SQLI
d7290 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 29 3b 0a TE_IOERR_LOCK);.
d72a0 20 20 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 if( IS_LOC
d72b0 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 K_ERROR(rc) ){.
d72c0 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 pFile->la
d72d0 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f stErrno = tErrno
d72e0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
d72f0 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
d7300 20 7d 20 0a 20 20 69 66 28 20 63 6c 6f 73 65 28 } . if( close(
d7310 66 64 29 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 fd) ){. pFile
d7320 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 ->lastErrno = er
d7330 72 6e 6f 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 rno;. rc = SQ
d7340 4c 49 54 45 5f 49 4f 45 52 52 5f 43 4c 4f 53 45 LITE_IOERR_CLOSE
d7350 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 67 6f ;. }. . /* go
d7360 74 20 69 74 2c 20 73 65 74 20 74 68 65 20 74 79 t it, set the ty
d7370 70 65 20 61 6e 64 20 72 65 74 75 72 6e 20 6f 6b pe and return ok
d7380 20 2a 2f 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 */. pFile->loc
d7390 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 ktype = locktype
d73a0 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
d73b0 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 ../*.** Lower th
d73c0 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 e locking level
d73d0 6f 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 on file descript
d73e0 6f 72 20 70 46 69 6c 65 20 74 6f 20 6c 6f 63 6b or pFile to lock
d73f0 74 79 70 65 2e 20 20 6c 6f 63 6b 74 79 70 65 0a type. locktype.
d7400 2a 2a 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 ** must be eithe
d7410 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 r NO_LOCK or SHA
d7420 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 RED_LOCK..**.**
d7430 49 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c If the locking l
d7440 65 76 65 6c 20 6f 66 20 74 68 65 20 66 69 6c 65 evel of the file
d7450 20 64 65 73 63 72 69 70 74 6f 72 20 69 73 20 61 descriptor is a
d7460 6c 72 65 61 64 79 20 61 74 20 6f 72 20 62 65 6c lready at or bel
d7470 6f 77 0a 2a 2a 20 74 68 65 20 72 65 71 75 65 73 ow.** the reques
d7480 74 65 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 ted locking leve
d7490 6c 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 l, this routine
d74a0 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a is a no-op..**.*
d74b0 2a 20 57 68 65 6e 20 74 68 65 20 6c 6f 63 6b 69 * When the locki
d74c0 6e 67 20 6c 65 76 65 6c 20 72 65 61 63 68 65 73 ng level reaches
d74d0 20 4e 4f 5f 4c 4f 43 4b 2c 20 64 65 6c 65 74 65 NO_LOCK, delete
d74e0 20 74 68 65 20 6c 6f 63 6b 20 66 69 6c 65 2e 0a the lock file..
d74f0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64 6f */.static int do
d7500 74 6c 6f 63 6b 55 6e 6c 6f 63 6b 28 73 71 6c 69 tlockUnlock(sqli
d7510 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e te3_file *id, in
d7520 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20 20 t locktype) {.
d7530 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 unixFile *pFile
d7540 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b = (unixFile*)id;
d7550 0a 20 20 63 68 61 72 20 2a 7a 4c 6f 63 6b 46 69 . char *zLockFi
d7560 6c 65 20 3d 20 28 63 68 61 72 20 2a 29 70 46 69 le = (char *)pFi
d7570 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 le->lockingConte
d7580 78 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 xt;.. assert( p
d7590 46 69 6c 65 20 29 3b 0a 20 20 4f 53 54 52 41 43 File );. OSTRAC
d75a0 45 35 28 22 55 4e 4c 4f 43 4b 20 20 25 64 20 25 E5("UNLOCK %d %
d75b0 64 20 77 61 73 20 25 64 20 70 69 64 3d 25 64 5c d was %d pid=%d\
d75c0 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f n", pFile->h, lo
d75d0 63 6b 74 79 70 65 2c 0a 09 20 20 20 70 46 69 6c cktype,.. pFil
d75e0 65 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 67 65 74 e->locktype, get
d75f0 70 69 64 28 29 29 3b 0a 20 20 61 73 73 65 72 74 pid());. assert
d7600 28 20 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 41 52 ( locktype<=SHAR
d7610 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 0a 20 20 ED_LOCK );. .
d7620 2f 2a 20 6e 6f 2d 6f 70 20 69 66 20 70 6f 73 73 /* no-op if poss
d7630 69 62 6c 65 20 2a 2f 0a 20 20 69 66 28 20 70 46 ible */. if( pF
d7640 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 6c ile->locktype==l
d7650 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 72 ocktype ){. r
d7660 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
d7670 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 6f 20 64 6f . }.. /* To do
d7680 77 6e 67 72 61 64 65 20 74 6f 20 73 68 61 72 65 wngrade to share
d7690 64 2c 20 73 69 6d 70 6c 79 20 75 70 64 61 74 65 d, simply update
d76a0 20 6f 75 72 20 69 6e 74 65 72 6e 61 6c 20 6e 6f our internal no
d76b0 74 69 6f 6e 20 6f 66 20 74 68 65 0a 20 20 2a 2a tion of the. **
d76c0 20 6c 6f 63 6b 20 73 74 61 74 65 2e 20 20 4e 6f lock state. No
d76d0 20 6e 65 65 64 20 74 6f 20 6d 65 73 73 20 77 69 need to mess wi
d76e0 74 68 20 74 68 65 20 66 69 6c 65 20 6f 6e 20 64 th the file on d
d76f0 69 73 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 isk.. */. if(
d7700 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 locktype==SHARED
d7710 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 70 46 69 _LOCK ){. pFi
d7720 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 53 le->locktype = S
d7730 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 HARED_LOCK;.
d7740 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
d7750 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 54 6f ;. }. . /* To
d7760 20 66 75 6c 6c 79 20 75 6e 6c 6f 63 6b 20 74 68 fully unlock th
d7770 65 20 64 61 74 61 62 61 73 65 2c 20 64 65 6c 65 e database, dele
d7780 74 65 20 74 68 65 20 6c 6f 63 6b 20 66 69 6c 65 te the lock file
d7790 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 6c 6f */. assert( lo
d77a0 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 cktype==NO_LOCK
d77b0 29 3b 0a 20 20 69 66 28 20 75 6e 6c 69 6e 6b 28 );. if( unlink(
d77c0 7a 4c 6f 63 6b 46 69 6c 65 29 20 29 7b 0a 20 20 zLockFile) ){.
d77d0 20 20 69 6e 74 20 72 63 20 3d 20 30 3b 0a 20 20 int rc = 0;.
d77e0 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 int tErrno = e
d77f0 72 72 6e 6f 3b 0a 20 20 20 20 69 66 28 20 45 4e rrno;. if( EN
d7800 4f 45 4e 54 20 21 3d 20 74 45 72 72 6e 6f 20 29 OENT != tErrno )
d7810 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c {. rc = sql
d7820 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 iteErrorFromPosi
d7830 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 xError(tErrno, S
d7840 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f QLITE_IOERR_UNLO
d7850 43 4b 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 CK);. }. i
d7860 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 f( IS_LOCK_ERROR
d7870 28 72 63 29 20 29 7b 0a 20 20 20 20 20 20 70 46 (rc) ){. pF
d7880 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d ile->lastErrno =
d7890 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 7d 0a 20 tErrno;. }.
d78a0 20 20 20 72 65 74 75 72 6e 20 72 63 3b 20 0a 20 return rc; .
d78b0 20 7d 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b }. pFile->lock
d78c0 74 79 70 65 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a type = NO_LOCK;.
d78d0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
d78e0 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f OK;.}../*.** Clo
d78f0 73 65 20 61 20 66 69 6c 65 2e 20 20 4d 61 6b 65 se a file. Make
d7900 20 73 75 72 65 20 74 68 65 20 6c 6f 63 6b 20 68 sure the lock h
d7910 61 73 20 62 65 65 6e 20 72 65 6c 65 61 73 65 64 as been released
d7920 20 62 65 66 6f 72 65 20 63 6c 6f 73 69 6e 67 2e before closing.
d7930 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64 .*/.static int d
d7940 6f 74 6c 6f 63 6b 43 6c 6f 73 65 28 73 71 6c 69 otlockClose(sqli
d7950 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 20 7b 0a te3_file *id) {.
d7960 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 66 28 20 int rc;. if(
d7970 69 64 20 29 7b 0a 20 20 20 20 75 6e 69 78 46 69 id ){. unixFi
d7980 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 le *pFile = (uni
d7990 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 20 20 64 xFile*)id;. d
d79a0 6f 74 6c 6f 63 6b 55 6e 6c 6f 63 6b 28 69 64 2c otlockUnlock(id,
d79b0 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 73 NO_LOCK);. s
d79c0 71 6c 69 74 65 33 5f 66 72 65 65 28 70 46 69 6c qlite3_free(pFil
d79d0 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 e->lockingContex
d79e0 74 29 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 63 t);. }. rc = c
d79f0 6c 6f 73 65 55 6e 69 78 46 69 6c 65 28 69 64 29 loseUnixFile(id)
d7a00 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
d7a10 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
d7a20 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 **** End of the
d7a30 64 6f 74 2d 66 69 6c 65 20 6c 6f 63 6b 20 69 6d dot-file lock im
d7a40 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a plementation ***
d7a50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d7a60 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
d7a70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d7a80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d7a90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d7aa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
d7ab0 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
d7ac0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d7ad0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d7ae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d7af0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d7b00 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *.**************
d7b10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 ************ Beg
d7b20 69 6e 20 66 6c 6f 63 6b 20 4c 6f 63 6b 69 6e 67 in flock Locking
d7b30 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
d7b40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d7b50 2a 0a 2a 2a 0a 2a 2a 20 55 73 65 20 74 68 65 20 *.**.** Use the
d7b60 66 6c 6f 63 6b 28 29 20 73 79 73 74 65 6d 20 63 flock() system c
d7b70 61 6c 6c 20 74 6f 20 64 6f 20 66 69 6c 65 20 6c all to do file l
d7b80 6f 63 6b 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 66 6c ocking..**.** fl
d7b90 6f 63 6b 28 29 20 6c 6f 63 6b 69 6e 67 20 69 73 ock() locking is
d7ba0 20 6c 69 6b 65 20 64 6f 74 2d 66 69 6c 65 20 6c like dot-file l
d7bb0 6f 63 6b 69 6e 67 20 69 6e 20 74 68 61 74 20 74 ocking in that t
d7bc0 68 65 20 76 61 72 69 6f 75 73 0a 2a 2a 20 66 69 he various.** fi
d7bd0 6e 65 2d 67 72 61 69 6e 20 6c 6f 63 6b 69 6e 67 ne-grain locking
d7be0 20 6c 65 76 65 6c 73 20 73 75 70 70 6f 72 74 65 levels supporte
d7bf0 64 20 62 79 20 53 51 4c 69 74 65 20 61 72 65 20 d by SQLite are
d7c00 63 6f 6c 6c 61 70 73 65 64 20 69 6e 74 6f 0a 2a collapsed into.*
d7c10 2a 20 61 20 73 69 6e 67 6c 65 20 65 78 63 6c 75 * a single exclu
d7c20 73 69 76 65 20 6c 6f 63 6b 2e 20 20 49 6e 20 6f sive lock. In o
d7c30 74 68 65 72 20 77 6f 72 64 73 2c 20 53 48 41 52 ther words, SHAR
d7c40 45 44 2c 20 52 45 53 45 52 56 45 44 2c 20 61 6e ED, RESERVED, an
d7c50 64 0a 2a 2a 20 50 45 4e 44 49 4e 47 20 6c 6f 63 d.** PENDING loc
d7c60 6b 73 20 61 72 65 20 74 68 65 20 73 61 6d 65 20 ks are the same
d7c70 74 68 69 6e 67 20 61 73 20 61 6e 20 45 58 43 4c thing as an EXCL
d7c80 55 53 49 56 45 20 6c 6f 63 6b 2e 20 20 53 51 4c USIVE lock. SQL
d7c90 69 74 65 0a 2a 2a 20 73 74 69 6c 6c 20 77 6f 72 ite.** still wor
d7ca0 6b 73 20 77 68 65 6e 20 79 6f 75 20 64 6f 20 74 ks when you do t
d7cb0 68 69 73 2c 20 62 75 74 20 63 6f 6e 63 75 72 72 his, but concurr
d7cc0 65 6e 63 79 20 69 73 20 72 65 64 75 63 65 64 20 ency is reduced
d7cd0 73 69 6e 63 65 0a 2a 2a 20 6f 6e 6c 79 20 61 20 since.** only a
d7ce0 73 69 6e 67 6c 65 20 70 72 6f 63 65 73 73 20 63 single process c
d7cf0 61 6e 20 62 65 20 72 65 61 64 69 6e 67 20 74 68 an be reading th
d7d00 65 20 64 61 74 61 62 61 73 65 20 61 74 20 61 20 e database at a
d7d10 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 4f 6d 69 74 time..**.** Omit
d7d20 20 74 68 69 73 20 73 65 63 74 69 6f 6e 20 69 66 this section if
d7d30 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c SQLITE_ENABLE_L
d7d40 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 69 73 20 OCKING_STYLE is
d7d50 74 75 72 6e 65 64 20 6f 66 66 20 6f 72 20 69 66 turned off or if
d7d60 0a 2a 2a 20 63 6f 6d 70 69 6c 69 6e 67 20 66 6f .** compiling fo
d7d70 72 20 56 58 57 4f 52 4b 53 2e 0a 2a 2f 0a 23 69 r VXWORKS..*/.#i
d7d80 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f f SQLITE_ENABLE_
d7d90 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 26 26 LOCKING_STYLE &&
d7da0 20 21 4f 53 5f 56 58 57 4f 52 4b 53 0a 0a 2f 2a !OS_VXWORKS../*
d7db0 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
d7dc0 20 63 68 65 63 6b 73 20 69 66 20 74 68 65 72 65 checks if there
d7dd0 20 69 73 20 61 20 52 45 53 45 52 56 45 44 20 6c is a RESERVED l
d7de0 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 ock held on the
d7df0 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 66 69 6c specified.** fil
d7e00 65 20 62 79 20 74 68 69 73 20 6f 72 20 61 6e 79 e by this or any
d7e10 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 2e 20 other process.
d7e20 49 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 69 If such a lock i
d7e30 73 20 68 65 6c 64 2c 20 73 65 74 20 2a 70 52 65 s held, set *pRe
d7e40 73 4f 75 74 0a 2a 2a 20 74 6f 20 61 20 6e 6f 6e sOut.** to a non
d7e50 2d 7a 65 72 6f 20 76 61 6c 75 65 20 6f 74 68 65 -zero value othe
d7e60 72 77 69 73 65 20 2a 70 52 65 73 4f 75 74 20 69 rwise *pResOut i
d7e70 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e 20 20 s set to zero.
d7e80 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 The return value
d7e90 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 53 51 .** is set to SQ
d7ea0 4c 49 54 45 5f 4f 4b 20 75 6e 6c 65 73 73 20 61 LITE_OK unless a
d7eb0 6e 20 49 2f 4f 20 65 72 72 6f 72 20 6f 63 63 75 n I/O error occu
d7ec0 72 73 20 64 75 72 69 6e 67 20 6c 6f 63 6b 20 63 rs during lock c
d7ed0 68 65 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 hecking..*/.stat
d7ee0 69 63 20 69 6e 74 20 66 6c 6f 63 6b 43 68 65 63 ic int flockChec
d7ef0 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 kReservedLock(sq
d7f00 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 lite3_file *id,
d7f10 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 7b 0a 20 int *pResOut){.
d7f20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
d7f30 5f 4f 4b 3b 0a 20 20 69 6e 74 20 72 65 73 65 72 _OK;. int reser
d7f40 76 65 64 20 3d 20 30 3b 0a 20 20 75 6e 69 78 46 ved = 0;. unixF
d7f50 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e ile *pFile = (un
d7f60 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 0a 20 ixFile*)id;. .
d7f70 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 SimulateIOError
d7f80 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f ( return SQLITE_
d7f90 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 45 52 IOERR_CHECKRESER
d7fa0 56 45 44 4c 4f 43 4b 3b 20 29 3b 0a 20 20 0a 20 VEDLOCK; );. .
d7fb0 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 assert( pFile )
d7fc0 3b 0a 20 20 0a 20 20 2f 2a 20 43 68 65 63 6b 20 ;. . /* Check
d7fd0 69 66 20 61 20 74 68 72 65 61 64 20 69 6e 20 74 if a thread in t
d7fe0 68 69 73 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 his process hold
d7ff0 73 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 2a 2f s such a lock */
d8000 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f . if( pFile->lo
d8010 63 6b 74 79 70 65 3e 53 48 41 52 45 44 5f 4c 4f cktype>SHARED_LO
d8020 43 4b 20 29 7b 0a 20 20 20 20 72 65 73 65 72 76 CK ){. reserv
d8030 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 0a 20 ed = 1;. }. .
d8040 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 20 73 65 /* Otherwise se
d8050 65 20 69 66 20 73 6f 6d 65 20 6f 74 68 65 72 20 e if some other
d8060 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 69 74 process holds it
d8070 2e 20 2a 2f 0a 20 20 69 66 28 20 21 72 65 73 65 . */. if( !rese
d8080 72 76 65 64 20 29 7b 0a 20 20 20 20 2f 2a 20 61 rved ){. /* a
d8090 74 74 65 6d 70 74 20 74 6f 20 67 65 74 20 74 68 ttempt to get th
d80a0 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 69 6e e lock */. in
d80b0 74 20 6c 72 63 20 3d 20 66 6c 6f 63 6b 28 70 46 t lrc = flock(pF
d80c0 69 6c 65 2d 3e 68 2c 20 4c 4f 43 4b 5f 45 58 20 ile->h, LOCK_EX
d80d0 7c 20 4c 4f 43 4b 5f 4e 42 29 3b 0a 20 20 20 20 | LOCK_NB);.
d80e0 69 66 28 20 21 6c 72 63 20 29 7b 0a 20 20 20 20 if( !lrc ){.
d80f0 20 20 2f 2a 20 67 6f 74 20 74 68 65 20 6c 6f 63 /* got the loc
d8100 6b 2c 20 75 6e 6c 6f 63 6b 20 69 74 20 2a 2f 0a k, unlock it */.
d8110 20 20 20 20 20 20 6c 72 63 20 3d 20 66 6c 6f 63 lrc = floc
d8120 6b 28 70 46 69 6c 65 2d 3e 68 2c 20 4c 4f 43 4b k(pFile->h, LOCK
d8130 5f 55 4e 29 3b 0a 20 20 20 20 20 20 69 66 20 28 _UN);. if (
d8140 20 6c 72 63 20 29 20 7b 0a 20 20 20 20 20 20 20 lrc ) {.
d8150 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 int tErrno = er
d8160 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20 rno;. /*
d8170 75 6e 6c 6f 63 6b 20 66 61 69 6c 65 64 20 77 69 unlock failed wi
d8180 74 68 20 61 6e 20 65 72 72 6f 72 20 2a 2f 0a 20 th an error */.
d8190 20 20 20 20 20 20 20 6c 72 63 20 3d 20 73 71 6c lrc = sql
d81a0 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 iteErrorFromPosi
d81b0 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 xError(tErrno, S
d81c0 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f QLITE_IOERR_UNLO
d81d0 43 4b 29 3b 20 0a 20 20 20 20 20 20 20 20 69 66 CK); . if
d81e0 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 ( IS_LOCK_ERROR(
d81f0 6c 72 63 29 20 29 7b 0a 20 20 20 20 20 20 20 20 lrc) ){.
d8200 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 pFile->lastErr
d8210 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 no = tErrno;.
d8220 20 20 20 20 20 20 20 72 63 20 3d 20 6c 72 63 3b rc = lrc;
d8230 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
d8240 20 7d 0a 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a }. } else {.
d8250 20 20 20 20 20 20 69 6e 74 20 74 45 72 72 6e 6f int tErrno
d8260 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 = errno;.
d8270 72 65 73 65 72 76 65 64 20 3d 20 31 3b 0a 20 20 reserved = 1;.
d8280 20 20 20 20 2f 2a 20 73 6f 6d 65 6f 6e 65 20 65 /* someone e
d8290 6c 73 65 20 6d 69 67 68 74 20 68 61 76 65 20 69 lse might have i
d82a0 74 20 72 65 73 65 72 76 65 64 20 2a 2f 0a 20 20 t reserved */.
d82b0 20 20 20 20 6c 72 63 20 3d 20 73 71 6c 69 74 65 lrc = sqlite
d82c0 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 ErrorFromPosixEr
d82d0 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 ror(tErrno, SQLI
d82e0 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 29 3b 20 TE_IOERR_LOCK);
d82f0 0a 20 20 20 20 20 20 69 66 28 20 49 53 5f 4c 4f . if( IS_LO
d8300 43 4b 5f 45 52 52 4f 52 28 6c 72 63 29 20 29 7b CK_ERROR(lrc) ){
d8310 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e . pFile->
d8320 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 lastErrno = tErr
d8330 6e 6f 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d no;. rc =
d8340 20 6c 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 lrc;. }.
d8350 20 20 7d 0a 20 20 7d 0a 20 20 4f 53 54 52 41 43 }. }. OSTRAC
d8360 45 34 28 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b E4("TEST WR-LOCK
d8370 20 25 64 20 25 64 20 25 64 5c 6e 22 2c 20 70 46 %d %d %d\n", pF
d8380 69 6c 65 2d 3e 68 2c 20 72 63 2c 20 72 65 73 65 ile->h, rc, rese
d8390 72 76 65 64 29 3b 0a 0a 23 69 66 64 65 66 20 53 rved);..#ifdef S
d83a0 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5f 46 4c 4f QLITE_IGNORE_FLO
d83b0 43 4b 5f 4c 4f 43 4b 5f 45 52 52 4f 52 53 0a 20 CK_LOCK_ERRORS.
d83c0 20 69 66 28 20 28 72 63 20 26 20 53 51 4c 49 54 if( (rc & SQLIT
d83d0 45 5f 49 4f 45 52 52 29 20 3d 3d 20 53 51 4c 49 E_IOERR) == SQLI
d83e0 54 45 5f 49 4f 45 52 52 20 29 7b 0a 20 20 20 20 TE_IOERR ){.
d83f0 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
d8400 20 20 20 20 72 65 73 65 72 76 65 64 3d 31 3b 0a reserved=1;.
d8410 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 }.#endif /* SQ
d8420 4c 49 54 45 5f 49 47 4e 4f 52 45 5f 46 4c 4f 43 LITE_IGNORE_FLOC
d8430 4b 5f 4c 4f 43 4b 5f 45 52 52 4f 52 53 20 2a 2f K_LOCK_ERRORS */
d8440 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d 20 72 65 . *pResOut = re
d8450 73 65 72 76 65 64 3b 0a 20 20 72 65 74 75 72 6e served;. return
d8460 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f rc;.}../*.** Lo
d8470 63 6b 20 74 68 65 20 66 69 6c 65 20 77 69 74 68 ck the file with
d8480 20 74 68 65 20 6c 6f 63 6b 20 73 70 65 63 69 66 the lock specif
d8490 69 65 64 20 62 79 20 70 61 72 61 6d 65 74 65 72 ied by parameter
d84a0 20 6c 6f 63 6b 74 79 70 65 20 2d 20 6f 6e 65 0a locktype - one.
d84b0 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 ** of the follow
d84c0 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 ing:.**.** (
d84d0 31 29 20 53 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 1) SHARED_LOCK.*
d84e0 2a 20 20 20 20 20 28 32 29 20 52 45 53 45 52 56 * (2) RESERV
d84f0 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 ED_LOCK.** (
d8500 33 29 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 3) PENDING_LOCK.
d8510 2a 2a 20 20 20 20 20 28 34 29 20 45 58 43 4c 55 ** (4) EXCLU
d8520 53 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 SIVE_LOCK.**.**
d8530 53 6f 6d 65 74 69 6d 65 73 20 77 68 65 6e 20 72 Sometimes when r
d8540 65 71 75 65 73 74 69 6e 67 20 6f 6e 65 20 6c 6f equesting one lo
d8550 63 6b 20 73 74 61 74 65 2c 20 61 64 64 69 74 69 ck state, additi
d8560 6f 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 74 65 73 onal lock states
d8570 0a 2a 2a 20 61 72 65 20 69 6e 73 65 72 74 65 64 .** are inserted
d8580 20 69 6e 20 62 65 74 77 65 65 6e 2e 20 20 54 68 in between. Th
d8590 65 20 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 74 20 e locking might
d85a0 66 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 fail on one of t
d85b0 68 65 20 6c 61 74 65 72 0a 2a 2a 20 74 72 61 6e he later.** tran
d85c0 73 69 74 69 6f 6e 73 20 6c 65 61 76 69 6e 67 20 sitions leaving
d85d0 74 68 65 20 6c 6f 63 6b 20 73 74 61 74 65 20 64 the lock state d
d85e0 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 77 68 ifferent from wh
d85f0 61 74 20 69 74 20 73 74 61 72 74 65 64 20 62 75 at it started bu
d8600 74 0a 2a 2a 20 73 74 69 6c 6c 20 73 68 6f 72 74 t.** still short
d8610 20 6f 66 20 69 74 73 20 67 6f 61 6c 2e 20 20 54 of its goal. T
d8620 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 he following cha
d8630 72 74 20 73 68 6f 77 73 20 74 68 65 20 61 6c 6c rt shows the all
d8640 6f 77 65 64 0a 2a 2a 20 74 72 61 6e 73 69 74 69 owed.** transiti
d8650 6f 6e 73 20 61 6e 64 20 74 68 65 20 69 6e 73 65 ons and the inse
d8660 72 74 65 64 20 69 6e 74 65 72 6d 65 64 69 61 74 rted intermediat
d8670 65 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 e states:.**.**
d8680 20 20 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 UNLOCKED -> S
d8690 48 41 52 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 HARED.** SHAR
d86a0 45 44 20 2d 3e 20 52 45 53 45 52 56 45 44 0a 2a ED -> RESERVED.*
d86b0 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20 28 * SHARED -> (
d86c0 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c PENDING) -> EXCL
d86d0 55 53 49 56 45 0a 2a 2a 20 20 20 20 52 45 53 45 USIVE.** RESE
d86e0 52 56 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 RVED -> (PENDING
d86f0 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a ) -> EXCLUSIVE.*
d8700 2a 20 20 20 20 50 45 4e 44 49 4e 47 20 2d 3e 20 * PENDING ->
d8710 45 58 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 EXCLUSIVE.**.**
d8720 66 6c 6f 63 6b 28 29 20 6f 6e 6c 79 20 72 65 61 flock() only rea
d8730 6c 6c 79 20 73 75 70 70 6f 72 74 20 45 58 43 4c lly support EXCL
d8740 55 53 49 56 45 20 6c 6f 63 6b 73 2e 20 20 57 65 USIVE locks. We
d8750 20 74 72 61 63 6b 20 69 6e 74 65 72 6d 65 64 69 track intermedi
d8760 61 74 65 0a 2a 2a 20 6c 6f 63 6b 20 73 74 61 74 ate.** lock stat
d8770 65 73 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 es in the sqlite
d8780 33 5f 66 69 6c 65 20 73 74 72 75 63 74 75 72 65 3_file structure
d8790 2c 20 62 75 74 20 61 6c 6c 20 6c 6f 63 6b 73 20 , but all locks
d87a0 53 48 41 52 45 44 20 6f 72 0a 2a 2a 20 61 62 6f SHARED or.** abo
d87b0 76 65 20 61 72 65 20 72 65 61 6c 6c 79 20 45 58 ve are really EX
d87c0 43 4c 55 53 49 56 45 20 6c 6f 63 6b 73 20 61 6e CLUSIVE locks an
d87d0 64 20 65 78 63 6c 75 64 65 20 61 6c 6c 20 6f 74 d exclude all ot
d87e0 68 65 72 20 70 72 6f 63 65 73 73 65 73 20 66 72 her processes fr
d87f0 6f 6d 0a 2a 2a 20 61 63 63 65 73 73 20 74 68 65 om.** access the
d8800 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 file..**.** Thi
d8810 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f s routine will o
d8820 6e 6c 79 20 69 6e 63 72 65 61 73 65 20 61 20 6c nly increase a l
d8830 6f 63 6b 2e 20 20 55 73 65 20 74 68 65 20 73 71 ock. Use the sq
d8840 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 0a lite3OsUnlock().
d8850 2a 2a 20 72 6f 75 74 69 6e 65 20 74 6f 20 6c 6f ** routine to lo
d8860 77 65 72 20 61 20 6c 6f 63 6b 69 6e 67 20 6c 65 wer a locking le
d8870 76 65 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 vel..*/.static i
d8880 6e 74 20 66 6c 6f 63 6b 4c 6f 63 6b 28 73 71 6c nt flockLock(sql
d8890 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 ite3_file *id, i
d88a0 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20 nt locktype) {.
d88b0 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
d88c0 5f 4f 4b 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 _OK;. unixFile
d88d0 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 *pFile = (unixFi
d88e0 6c 65 2a 29 69 64 3b 0a 0a 20 20 61 73 73 65 72 le*)id;.. asser
d88f0 74 28 20 70 46 69 6c 65 20 29 3b 0a 0a 20 20 2f t( pFile );.. /
d8900 2a 20 69 66 20 77 65 20 61 6c 72 65 61 64 79 20 * if we already
d8910 68 61 76 65 20 61 20 6c 6f 63 6b 2c 20 69 74 20 have a lock, it
d8920 69 73 20 65 78 63 6c 75 73 69 76 65 2e 20 20 0a is exclusive. .
d8930 20 20 2a 2a 20 4a 75 73 74 20 61 64 6a 75 73 74 ** Just adjust
d8940 20 6c 65 76 65 6c 20 61 6e 64 20 70 75 6e 74 20 level and punt
d8950 6f 6e 20 6f 75 74 74 61 20 68 65 72 65 2e 20 2a on outta here. *
d8960 2f 0a 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c /. if (pFile->l
d8970 6f 63 6b 74 79 70 65 20 3e 20 4e 4f 5f 4c 4f 43 ocktype > NO_LOC
d8980 4b 29 20 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e K) {. pFile->
d8990 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 locktype = lockt
d89a0 79 70 65 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 ype;. return
d89b0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 SQLITE_OK;. }.
d89c0 20 0a 20 20 2f 2a 20 67 72 61 62 20 61 6e 20 65 . /* grab an e
d89d0 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 2a 2f xclusive lock */
d89e0 0a 20 20 0a 20 20 69 66 20 28 66 6c 6f 63 6b 28 . . if (flock(
d89f0 70 46 69 6c 65 2d 3e 68 2c 20 4c 4f 43 4b 5f 45 pFile->h, LOCK_E
d8a00 58 20 7c 20 4c 4f 43 4b 5f 4e 42 29 29 20 7b 0a X | LOCK_NB)) {.
d8a10 20 20 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d int tErrno =
d8a20 20 65 72 72 6e 6f 3b 0a 20 20 20 20 2f 2a 20 64 errno;. /* d
d8a30 69 64 6e 27 74 20 67 65 74 2c 20 6d 75 73 74 20 idn't get, must
d8a40 62 65 20 62 75 73 79 20 2a 2f 0a 20 20 20 20 72 be busy */. r
d8a50 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46 c = sqliteErrorF
d8a60 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45 romPosixError(tE
d8a70 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 rrno, SQLITE_IOE
d8a80 52 52 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 69 66 RR_LOCK);. if
d8a90 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 ( IS_LOCK_ERROR(
d8aa0 72 63 29 20 29 7b 0a 20 20 20 20 20 20 70 46 69 rc) ){. pFi
d8ab0 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 le->lastErrno =
d8ac0 74 45 72 72 6e 6f 3b 0a 20 20 20 20 7d 0a 20 20 tErrno;. }.
d8ad0 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 2f 2a 20 } else {. /*
d8ae0 67 6f 74 20 69 74 2c 20 73 65 74 20 74 68 65 20 got it, set the
d8af0 74 79 70 65 20 61 6e 64 20 72 65 74 75 72 6e 20 type and return
d8b00 6f 6b 20 2a 2f 0a 20 20 20 20 70 46 69 6c 65 2d ok */. pFile-
d8b10 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b >locktype = lock
d8b20 74 79 70 65 3b 0a 20 20 7d 0a 20 20 4f 53 54 52 type;. }. OSTR
d8b30 41 43 45 34 28 22 4c 4f 43 4b 20 20 20 20 25 64 ACE4("LOCK %d
d8b40 20 25 73 20 25 73 5c 6e 22 2c 20 70 46 69 6c 65 %s %s\n", pFile
d8b50 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 4e 61 6d ->h, locktypeNam
d8b60 65 28 6c 6f 63 6b 74 79 70 65 29 2c 20 0a 20 20 e(locktype), .
d8b70 20 20 20 20 20 20 20 20 20 72 63 3d 3d 53 51 4c rc==SQL
d8b80 49 54 45 5f 4f 4b 20 3f 20 22 6f 6b 22 20 3a 20 ITE_OK ? "ok" :
d8b90 22 66 61 69 6c 65 64 22 29 3b 0a 23 69 66 64 65 "failed");.#ifde
d8ba0 66 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5f f SQLITE_IGNORE_
d8bb0 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45 52 52 4f 52 FLOCK_LOCK_ERROR
d8bc0 53 0a 20 20 69 66 28 20 28 72 63 20 26 20 53 51 S. if( (rc & SQ
d8bd0 4c 49 54 45 5f 49 4f 45 52 52 29 20 3d 3d 20 53 LITE_IOERR) == S
d8be0 51 4c 49 54 45 5f 49 4f 45 52 52 20 29 7b 0a 20 QLITE_IOERR ){.
d8bf0 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 rc = SQLITE_B
d8c00 55 53 59 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 USY;. }.#endif
d8c10 2f 2a 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 /* SQLITE_IGNORE
d8c20 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45 52 52 4f _FLOCK_LOCK_ERRO
d8c30 52 53 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 72 RS */. return r
d8c40 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 c;.}.../*.** Low
d8c50 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c er the locking l
d8c60 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64 65 73 evel on file des
d8c70 63 72 69 70 74 6f 72 20 70 46 69 6c 65 20 74 6f criptor pFile to
d8c80 20 6c 6f 63 6b 74 79 70 65 2e 20 20 6c 6f 63 6b locktype. lock
d8c90 74 79 70 65 0a 2a 2a 20 6d 75 73 74 20 62 65 20 type.** must be
d8ca0 65 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b 20 6f either NO_LOCK o
d8cb0 72 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2e 0a 2a r SHARED_LOCK..*
d8cc0 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f 63 6b *.** If the lock
d8cd0 69 6e 67 20 6c 65 76 65 6c 20 6f 66 20 74 68 65 ing level of the
d8ce0 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 file descriptor
d8cf0 20 69 73 20 61 6c 72 65 61 64 79 20 61 74 20 6f is already at o
d8d00 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 68 65 20 72 r below.** the r
d8d10 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 69 6e 67 equested locking
d8d20 20 6c 65 76 65 6c 2c 20 74 68 69 73 20 72 6f 75 level, this rou
d8d30 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e tine is a no-op.
d8d40 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 .*/.static int f
d8d50 6c 6f 63 6b 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 lockUnlock(sqlit
d8d60 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 e3_file *id, int
d8d70 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20 20 75 locktype) {. u
d8d80 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d nixFile *pFile =
d8d90 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a (unixFile*)id;.
d8da0 20 20 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 . assert( pFi
d8db0 6c 65 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 35 le );. OSTRACE5
d8dc0 28 22 55 4e 4c 4f 43 4b 20 20 25 64 20 25 64 20 ("UNLOCK %d %d
d8dd0 77 61 73 20 25 64 20 70 69 64 3d 25 64 5c 6e 22 was %d pid=%d\n"
d8de0 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b , pFile->h, lock
d8df0 74 79 70 65 2c 0a 20 20 20 20 20 20 20 20 20 20 type,.
d8e00 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
d8e10 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 20 20 61 , getpid());. a
d8e20 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 3c ssert( locktype<
d8e30 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a =SHARED_LOCK );.
d8e40 20 20 0a 20 20 2f 2a 20 6e 6f 2d 6f 70 20 69 66 . /* no-op if
d8e50 20 70 6f 73 73 69 62 6c 65 20 2a 2f 0a 20 20 69 possible */. i
d8e60 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 f( pFile->lockty
d8e70 70 65 3d 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a pe==locktype ){.
d8e80 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
d8e90 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f E_OK;. }. . /
d8ea0 2a 20 73 68 61 72 65 64 20 63 61 6e 20 6a 75 73 * shared can jus
d8eb0 74 20 62 65 20 73 65 74 20 62 65 63 61 75 73 65 t be set because
d8ec0 20 77 65 20 61 6c 77 61 79 73 20 68 61 76 65 20 we always have
d8ed0 61 6e 20 65 78 63 6c 75 73 69 76 65 20 2a 2f 0a an exclusive */.
d8ee0 20 20 69 66 20 28 6c 6f 63 6b 74 79 70 65 3d 3d if (locktype==
d8ef0 53 48 41 52 45 44 5f 4c 4f 43 4b 29 20 7b 0a 20 SHARED_LOCK) {.
d8f00 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 pFile->lockty
d8f10 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 pe = locktype;.
d8f20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
d8f30 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a _OK;. }. . /*
d8f40 20 6e 6f 2c 20 72 65 61 6c 6c 79 2c 20 75 6e 6c no, really, unl
d8f50 6f 63 6b 2e 20 2a 2f 0a 20 20 69 6e 74 20 72 63 ock. */. int rc
d8f60 20 3d 20 66 6c 6f 63 6b 28 70 46 69 6c 65 2d 3e = flock(pFile->
d8f70 68 2c 20 4c 4f 43 4b 5f 55 4e 29 3b 0a 20 20 69 h, LOCK_UN);. i
d8f80 66 20 28 72 63 29 20 7b 0a 20 20 20 20 69 6e 74 f (rc) {. int
d8f90 20 72 2c 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 r, tErrno = err
d8fa0 6e 6f 3b 0a 20 20 20 20 72 20 3d 20 73 71 6c 69 no;. r = sqli
d8fb0 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 teErrorFromPosix
d8fc0 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 Error(tErrno, SQ
d8fd0 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 LITE_IOERR_UNLOC
d8fe0 4b 29 3b 0a 20 20 20 20 69 66 28 20 49 53 5f 4c K);. if( IS_L
d8ff0 4f 43 4b 5f 45 52 52 4f 52 28 72 29 20 29 7b 0a OCK_ERROR(r) ){.
d9000 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 pFile->las
d9010 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b tErrno = tErrno;
d9020 0a 20 20 20 20 7d 0a 23 69 66 64 65 66 20 53 51 . }.#ifdef SQ
d9030 4c 49 54 45 5f 49 47 4e 4f 52 45 5f 46 4c 4f 43 LITE_IGNORE_FLOC
d9040 4b 5f 4c 4f 43 4b 5f 45 52 52 4f 52 53 0a 20 20 K_LOCK_ERRORS.
d9050 20 20 69 66 28 20 28 72 20 26 20 53 51 4c 49 54 if( (r & SQLIT
d9060 45 5f 49 4f 45 52 52 29 20 3d 3d 20 53 51 4c 49 E_IOERR) == SQLI
d9070 54 45 5f 49 4f 45 52 52 20 29 7b 0a 20 20 20 20 TE_IOERR ){.
d9080 20 20 72 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 r = SQLITE_BUS
d9090 59 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 Y;. }.#endif
d90a0 2f 2a 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 /* SQLITE_IGNORE
d90b0 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45 52 52 4f _FLOCK_LOCK_ERRO
d90c0 52 53 20 2a 2f 0a 20 20 20 20 0a 20 20 20 20 72 RS */. . r
d90d0 65 74 75 72 6e 20 72 3b 0a 20 20 7d 20 65 6c 73 eturn r;. } els
d90e0 65 20 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c e {. pFile->l
d90f0 6f 63 6b 74 79 70 65 20 3d 20 4e 4f 5f 4c 4f 43 ocktype = NO_LOC
d9100 4b 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 K;. return SQ
d9110 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 7d 0a 0a LITE_OK;. }.}..
d9120 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 /*.** Close a fi
d9130 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e le..*/.static in
d9140 74 20 66 6c 6f 63 6b 43 6c 6f 73 65 28 73 71 6c t flockClose(sql
d9150 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 20 7b ite3_file *id) {
d9160 0a 20 20 69 66 28 20 69 64 20 29 7b 0a 20 20 20 . if( id ){.
d9170 20 66 6c 6f 63 6b 55 6e 6c 6f 63 6b 28 69 64 2c flockUnlock(id,
d9180 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 7d 0a 20 NO_LOCK);. }.
d9190 20 72 65 74 75 72 6e 20 63 6c 6f 73 65 55 6e 69 return closeUni
d91a0 78 46 69 6c 65 28 69 64 29 3b 0a 7d 0a 0a 23 65 xFile(id);.}..#e
d91b0 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 45 ndif /* SQLITE_E
d91c0 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 NABLE_LOCKING_ST
d91d0 59 4c 45 20 26 26 20 21 4f 53 5f 56 58 57 4f 52 YLE && !OS_VXWOR
d91e0 4b 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a K */../*********
d91f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
d9200 66 20 74 68 65 20 66 6c 6f 63 6b 20 6c 6f 63 6b f the flock lock
d9210 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
d9220 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d9230 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *****.**********
d9240 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d9250 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d9260 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d9270 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d9280 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a ****/../********
d9290 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d92a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d92b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d92c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d92d0 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ******.*********
d92e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 ***************
d92f0 42 65 67 69 6e 20 4e 61 6d 65 64 20 53 65 6d 61 Begin Named Sema
d9300 70 68 6f 72 65 20 4c 6f 63 6b 69 6e 67 20 2a 2a phore Locking **
d9310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d9320 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 4e 61 6d ******.**.** Nam
d9330 65 64 20 73 65 6d 61 70 68 6f 72 65 20 6c 6f 63 ed semaphore loc
d9340 6b 69 6e 67 20 69 73 20 6f 6e 6c 79 20 73 75 70 king is only sup
d9350 70 6f 72 74 65 64 20 6f 6e 20 56 78 57 6f 72 6b ported on VxWork
d9360 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 6d 61 70 68 6f s..**.** Semapho
d9370 72 65 20 6c 6f 63 6b 69 6e 67 20 69 73 20 6c 69 re locking is li
d9380 6b 65 20 64 6f 74 2d 6c 6f 63 6b 20 61 6e 64 20 ke dot-lock and
d9390 66 6c 6f 63 6b 20 69 6e 20 74 68 61 74 20 69 74 flock in that it
d93a0 20 72 65 61 6c 6c 79 20 6f 6e 6c 79 0a 2a 2a 20 really only.**
d93b0 73 75 70 70 6f 72 74 73 20 45 58 43 4c 55 53 49 supports EXCLUSI
d93c0 56 45 20 6c 6f 63 6b 69 6e 67 2e 20 20 4f 6e 6c VE locking. Onl
d93d0 79 20 61 20 73 69 6e 67 6c 65 20 70 72 6f 63 65 y a single proce
d93e0 73 73 20 63 61 6e 20 72 65 61 64 20 6f 72 20 77 ss can read or w
d93f0 72 69 74 65 0a 2a 2a 20 74 68 65 20 64 61 74 61 rite.** the data
d9400 62 61 73 65 20 66 69 6c 65 20 61 74 20 61 20 74 base file at a t
d9410 69 6d 65 2e 20 20 54 68 69 73 20 72 65 64 75 63 ime. This reduc
d9420 65 73 20 70 6f 74 65 6e 74 69 61 6c 20 63 6f 6e es potential con
d9430 63 75 72 72 65 6e 63 79 2c 20 62 75 74 0a 2a 2a currency, but.**
d9440 20 6d 61 6b 65 73 20 74 68 65 20 6c 6f 63 6b 20 makes the lock
d9450 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6d implementation m
d9460 75 63 68 20 65 61 73 69 65 72 2e 0a 2a 2f 0a 23 uch easier..*/.#
d9470 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 0a 2f if OS_VXWORKS../
d9480 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
d9490 65 20 63 68 65 63 6b 73 20 69 66 20 74 68 65 72 e checks if ther
d94a0 65 20 69 73 20 61 20 52 45 53 45 52 56 45 44 20 e is a RESERVED
d94b0 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74 68 65 lock held on the
d94c0 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 66 69 specified.** fi
d94d0 6c 65 20 62 79 20 74 68 69 73 20 6f 72 20 61 6e le by this or an
d94e0 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 2e y other process.
d94f0 20 49 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 If such a lock
d9500 69 73 20 68 65 6c 64 2c 20 73 65 74 20 2a 70 52 is held, set *pR
d9510 65 73 4f 75 74 0a 2a 2a 20 74 6f 20 61 20 6e 6f esOut.** to a no
d9520 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 20 6f 74 68 n-zero value oth
d9530 65 72 77 69 73 65 20 2a 70 52 65 73 4f 75 74 20 erwise *pResOut
d9540 69 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e 20 is set to zero.
d9550 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 The return valu
d9560 65 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 53 e.** is set to S
d9570 51 4c 49 54 45 5f 4f 4b 20 75 6e 6c 65 73 73 20 QLITE_OK unless
d9580 61 6e 20 49 2f 4f 20 65 72 72 6f 72 20 6f 63 63 an I/O error occ
d9590 75 72 73 20 64 75 72 69 6e 67 20 6c 6f 63 6b 20 urs during lock
d95a0 63 68 65 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61 checking..*/.sta
d95b0 74 69 63 20 69 6e 74 20 73 65 6d 43 68 65 63 6b tic int semCheck
d95c0 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c ReservedLock(sql
d95d0 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 ite3_file *id, i
d95e0 6e 74 20 2a 70 52 65 73 4f 75 74 29 20 7b 0a 20 nt *pResOut) {.
d95f0 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
d9600 5f 4f 4b 3b 0a 20 20 69 6e 74 20 72 65 73 65 72 _OK;. int reser
d9610 76 65 64 20 3d 20 30 3b 0a 20 20 75 6e 69 78 46 ved = 0;. unixF
d9620 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e ile *pFile = (un
d9630 69 78 46 69 6c 65 2a 29 69 64 3b 0a 0a 20 20 53 ixFile*)id;.. S
d9640 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 imulateIOError(
d9650 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f return SQLITE_IO
d9660 45 52 52 5f 43 48 45 43 4b 52 45 53 45 52 56 45 ERR_CHECKRESERVE
d9670 44 4c 4f 43 4b 3b 20 29 3b 0a 20 20 0a 20 20 61 DLOCK; );. . a
d9680 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a ssert( pFile );.
d9690 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 20 61 . /* Check if a
d96a0 20 74 68 72 65 61 64 20 69 6e 20 74 68 69 73 20 thread in this
d96b0 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 73 75 process holds su
d96c0 63 68 20 61 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 ch a lock */. i
d96d0 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 f( pFile->lockty
d96e0 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 pe>SHARED_LOCK )
d96f0 7b 0a 20 20 20 20 72 65 73 65 72 76 65 64 20 3d {. reserved =
d9700 20 31 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 1;. }. . /*
d9710 4f 74 68 65 72 77 69 73 65 20 73 65 65 20 69 66 Otherwise see if
d9720 20 73 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f 63 some other proc
d9730 65 73 73 20 68 6f 6c 64 73 20 69 74 2e 20 2a 2f ess holds it. */
d9740 0a 20 20 69 66 28 20 21 72 65 73 65 72 76 65 64 . if( !reserved
d9750 20 29 7b 0a 20 20 20 20 73 65 6d 5f 74 20 2a 70 ){. sem_t *p
d9760 53 65 6d 20 3d 20 70 46 69 6c 65 2d 3e 70 4f 70 Sem = pFile->pOp
d9770 65 6e 2d 3e 70 53 65 6d 3b 0a 20 20 20 20 73 74 en->pSem;. st
d9780 72 75 63 74 20 73 74 61 74 20 73 74 61 74 42 75 ruct stat statBu
d9790 66 3b 0a 0a 20 20 20 20 69 66 28 20 73 65 6d 5f f;.. if( sem_
d97a0 74 72 79 77 61 69 74 28 70 53 65 6d 29 3d 3d 2d trywait(pSem)==-
d97b0 31 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 74 1 ){. int t
d97c0 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 Errno = errno;.
d97d0 20 20 20 20 20 69 66 28 20 45 41 47 41 49 4e 20 if( EAGAIN
d97e0 21 3d 20 74 45 72 72 6e 6f 20 29 7b 0a 20 20 20 != tErrno ){.
d97f0 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
d9800 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 ErrorFromPosixEr
d9810 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 ror(tErrno, SQLI
d9820 54 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 TE_IOERR_CHECKRE
d9830 53 45 52 56 45 44 4c 4f 43 4b 29 3b 0a 20 20 20 SERVEDLOCK);.
d9840 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 pFile->last
d9850 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a Errno = tErrno;.
d9860 20 20 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 } else {.
d9870 20 20 20 20 20 20 20 2f 2a 20 73 6f 6d 65 6f 6e /* someon
d9880 65 20 65 6c 73 65 20 68 61 73 20 74 68 65 20 6c e else has the l
d9890 6f 63 6b 20 77 68 65 6e 20 77 65 20 61 72 65 20 ock when we are
d98a0 69 6e 20 4e 4f 5f 4c 4f 43 4b 20 2a 2f 0a 20 20 in NO_LOCK */.
d98b0 20 20 20 20 20 20 72 65 73 65 72 76 65 64 20 3d reserved =
d98c0 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 (pFile->locktyp
d98d0 65 20 3c 20 53 48 41 52 45 44 5f 4c 4f 43 4b 29 e < SHARED_LOCK)
d98e0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 ;. }. }e
d98f0 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 77 65 lse{. /* we
d9900 20 63 6f 75 6c 64 20 68 61 76 65 20 69 74 20 69 could have it i
d9910 66 20 77 65 20 77 61 6e 74 20 69 74 20 2a 2f 0a f we want it */.
d9920 20 20 20 20 20 20 73 65 6d 5f 70 6f 73 74 28 70 sem_post(p
d9930 53 65 6d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a Sem);. }. }.
d9940 20 20 4f 53 54 52 41 43 45 34 28 22 54 45 53 54 OSTRACE4("TEST
d9950 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20 25 WR-LOCK %d %d %
d9960 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 d\n", pFile->h,
d9970 72 63 2c 20 72 65 73 65 72 76 65 64 29 3b 0a 0a rc, reserved);..
d9980 20 20 2a 70 52 65 73 4f 75 74 20 3d 20 72 65 73 *pResOut = res
d9990 65 72 76 65 64 3b 0a 20 20 72 65 74 75 72 6e 20 erved;. return
d99a0 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 rc;.}../*.** Loc
d99b0 6b 20 74 68 65 20 66 69 6c 65 20 77 69 74 68 20 k the file with
d99c0 74 68 65 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 the lock specifi
d99d0 65 64 20 62 79 20 70 61 72 61 6d 65 74 65 72 20 ed by parameter
d99e0 6c 6f 63 6b 74 79 70 65 20 2d 20 6f 6e 65 0a 2a locktype - one.*
d99f0 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 * of the followi
d9a00 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 ng:.**.** (1
d9a10 29 20 53 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a ) SHARED_LOCK.**
d9a20 20 20 20 20 20 28 32 29 20 52 45 53 45 52 56 45 (2) RESERVE
d9a30 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33 D_LOCK.** (3
d9a40 29 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a ) PENDING_LOCK.*
d9a50 2a 20 20 20 20 20 28 34 29 20 45 58 43 4c 55 53 * (4) EXCLUS
d9a60 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 IVE_LOCK.**.** S
d9a70 6f 6d 65 74 69 6d 65 73 20 77 68 65 6e 20 72 65 ometimes when re
d9a80 71 75 65 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 questing one loc
d9a90 6b 20 73 74 61 74 65 2c 20 61 64 64 69 74 69 6f k state, additio
d9aa0 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a nal lock states.
d9ab0 2a 2a 20 61 72 65 20 69 6e 73 65 72 74 65 64 20 ** are inserted
d9ac0 69 6e 20 62 65 74 77 65 65 6e 2e 20 20 54 68 65 in between. The
d9ad0 20 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 74 20 66 locking might f
d9ae0 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 ail on one of th
d9af0 65 20 6c 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73 e later.** trans
d9b00 69 74 69 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74 itions leaving t
d9b10 68 65 20 6c 6f 63 6b 20 73 74 61 74 65 20 64 69 he lock state di
d9b20 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 77 68 61 fferent from wha
d9b30 74 20 69 74 20 73 74 61 72 74 65 64 20 62 75 74 t it started but
d9b40 0a 2a 2a 20 73 74 69 6c 6c 20 73 68 6f 72 74 20 .** still short
d9b50 6f 66 20 69 74 73 20 67 6f 61 6c 2e 20 20 54 68 of its goal. Th
d9b60 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72 e following char
d9b70 74 20 73 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f t shows the allo
d9b80 77 65 64 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f wed.** transitio
d9b90 6e 73 20 61 6e 64 20 74 68 65 20 69 6e 73 65 72 ns and the inser
d9ba0 74 65 64 20 69 6e 74 65 72 6d 65 64 69 61 74 65 ted intermediate
d9bb0 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 states:.**.**
d9bc0 20 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 UNLOCKED -> SH
d9bd0 41 52 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 ARED.** SHARE
d9be0 44 20 2d 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a D -> RESERVED.**
d9bf0 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20 28 50 SHARED -> (P
d9c00 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 ENDING) -> EXCLU
d9c10 53 49 56 45 0a 2a 2a 20 20 20 20 52 45 53 45 52 SIVE.** RESER
d9c20 56 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 VED -> (PENDING)
d9c30 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a -> EXCLUSIVE.**
d9c40 20 20 20 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45 PENDING -> E
d9c50 58 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 53 XCLUSIVE.**.** S
d9c60 65 6d 61 70 68 6f 72 65 20 6c 6f 63 6b 73 20 6f emaphore locks o
d9c70 6e 6c 79 20 72 65 61 6c 6c 79 20 73 75 70 70 6f nly really suppo
d9c80 72 74 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 rt EXCLUSIVE loc
d9c90 6b 73 2e 20 20 57 65 20 74 72 61 63 6b 20 69 6e ks. We track in
d9ca0 74 65 72 6d 65 64 69 61 74 65 0a 2a 2a 20 6c 6f termediate.** lo
d9cb0 63 6b 20 73 74 61 74 65 73 20 69 6e 20 74 68 65 ck states in the
d9cc0 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 73 74 sqlite3_file st
d9cd0 72 75 63 74 75 72 65 2c 20 62 75 74 20 61 6c 6c ructure, but all
d9ce0 20 6c 6f 63 6b 73 20 53 48 41 52 45 44 20 6f 72 locks SHARED or
d9cf0 0a 2a 2a 20 61 62 6f 76 65 20 61 72 65 20 72 65 .** above are re
d9d00 61 6c 6c 79 20 45 58 43 4c 55 53 49 56 45 20 6c ally EXCLUSIVE l
d9d10 6f 63 6b 73 20 61 6e 64 20 65 78 63 6c 75 64 65 ocks and exclude
d9d20 20 61 6c 6c 20 6f 74 68 65 72 20 70 72 6f 63 65 all other proce
d9d30 73 73 65 73 20 66 72 6f 6d 0a 2a 2a 20 61 63 63 sses from.** acc
d9d40 65 73 73 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a ess the file..**
d9d50 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
d9d60 20 77 69 6c 6c 20 6f 6e 6c 79 20 69 6e 63 72 65 will only incre
d9d70 61 73 65 20 61 20 6c 6f 63 6b 2e 20 20 55 73 65 ase a lock. Use
d9d80 20 74 68 65 20 73 71 6c 69 74 65 33 4f 73 55 6e the sqlite3OsUn
d9d90 6c 6f 63 6b 28 29 0a 2a 2a 20 72 6f 75 74 69 6e lock().** routin
d9da0 65 20 74 6f 20 6c 6f 77 65 72 20 61 20 6c 6f 63 e to lower a loc
d9db0 6b 69 6e 67 20 6c 65 76 65 6c 2e 0a 2a 2f 0a 73 king level..*/.s
d9dc0 74 61 74 69 63 20 69 6e 74 20 73 65 6d 4c 6f 63 tatic int semLoc
d9dd0 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a k(sqlite3_file *
d9de0 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 id, int locktype
d9df0 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a ) {. unixFile *
d9e00 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c pFile = (unixFil
d9e10 65 2a 29 69 64 3b 0a 20 20 69 6e 74 20 66 64 3b e*)id;. int fd;
d9e20 0a 20 20 73 65 6d 5f 74 20 2a 70 53 65 6d 20 3d . sem_t *pSem =
d9e30 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 2d 3e 70 pFile->pOpen->p
d9e40 53 65 6d 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 Sem;. int rc =
d9e50 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a SQLITE_OK;.. /*
d9e60 20 69 66 20 77 65 20 61 6c 72 65 61 64 79 20 68 if we already h
d9e70 61 76 65 20 61 20 6c 6f 63 6b 2c 20 69 74 20 69 ave a lock, it i
d9e80 73 20 65 78 63 6c 75 73 69 76 65 2e 20 20 0a 20 s exclusive. .
d9e90 20 2a 2a 20 4a 75 73 74 20 61 64 6a 75 73 74 20 ** Just adjust
d9ea0 6c 65 76 65 6c 20 61 6e 64 20 70 75 6e 74 20 6f level and punt o
d9eb0 6e 20 6f 75 74 74 61 20 68 65 72 65 2e 20 2a 2f n outta here. */
d9ec0 0a 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f . if (pFile->lo
d9ed0 63 6b 74 79 70 65 20 3e 20 4e 4f 5f 4c 4f 43 4b cktype > NO_LOCK
d9ee0 29 20 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c ) {. pFile->l
d9ef0 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 ocktype = lockty
d9f00 70 65 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c pe;. rc = SQL
d9f10 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 67 6f 74 6f ITE_OK;. goto
d9f20 20 73 65 6d 5f 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 sem_end_lock;.
d9f30 20 7d 0a 20 20 0a 20 20 2f 2a 20 6c 6f 63 6b 20 }. . /* lock
d9f40 73 65 6d 61 70 68 6f 72 65 20 6e 6f 77 20 62 75 semaphore now bu
d9f50 74 20 62 61 69 6c 20 6f 75 74 20 77 68 65 6e 20 t bail out when
d9f60 61 6c 72 65 61 64 79 20 6c 6f 63 6b 65 64 2e 20 already locked.
d9f70 2a 2f 0a 20 20 69 66 28 20 73 65 6d 5f 74 72 79 */. if( sem_try
d9f80 77 61 69 74 28 70 53 65 6d 29 3d 3d 2d 31 20 29 wait(pSem)==-1 )
d9f90 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 {. rc = SQLIT
d9fa0 45 5f 42 55 53 59 3b 0a 20 20 20 20 67 6f 74 6f E_BUSY;. goto
d9fb0 20 73 65 6d 5f 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 sem_end_lock;.
d9fc0 20 7d 0a 0a 20 20 2f 2a 20 67 6f 74 20 69 74 2c }.. /* got it,
d9fd0 20 73 65 74 20 74 68 65 20 74 79 70 65 20 61 6e set the type an
d9fe0 64 20 72 65 74 75 72 6e 20 6f 6b 20 2a 2f 0a 20 d return ok */.
d9ff0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
da000 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 0a 20 73 = locktype;.. s
da010 65 6d 5f 65 6e 64 5f 6c 6f 63 6b 3a 0a 20 20 72 em_end_lock:. r
da020 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
da030 2a 2a 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f 63 ** Lower the loc
da040 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66 69 king level on fi
da050 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 70 46 le descriptor pF
da060 69 6c 65 20 74 6f 20 6c 6f 63 6b 74 79 70 65 2e ile to locktype.
da070 20 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 locktype.** mu
da080 73 74 20 62 65 20 65 69 74 68 65 72 20 4e 4f 5f st be either NO_
da090 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45 44 5f 4c LOCK or SHARED_L
da0a0 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 OCK..**.** If th
da0b0 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 e locking level
da0c0 6f 66 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 of the file desc
da0d0 72 69 70 74 6f 72 20 69 73 20 61 6c 72 65 61 64 riptor is alread
da0e0 79 20 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a y at or below.**
da0f0 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 6c the requested l
da100 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74 68 ocking level, th
da110 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 is routine is a
da120 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 no-op..*/.static
da130 20 69 6e 74 20 73 65 6d 55 6e 6c 6f 63 6b 28 73 int semUnlock(s
da140 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c qlite3_file *id,
da150 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b int locktype) {
da160 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 . unixFile *pFi
da170 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 le = (unixFile*)
da180 69 64 3b 0a 20 20 73 65 6d 5f 74 20 2a 70 53 65 id;. sem_t *pSe
da190 6d 20 3d 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e m = pFile->pOpen
da1a0 2d 3e 70 53 65 6d 3b 0a 0a 20 20 61 73 73 65 72 ->pSem;.. asser
da1b0 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 61 73 t( pFile );. as
da1c0 73 65 72 74 28 20 70 53 65 6d 20 29 3b 0a 20 20 sert( pSem );.
da1d0 4f 53 54 52 41 43 45 35 28 22 55 4e 4c 4f 43 4b OSTRACE5("UNLOCK
da1e0 20 20 25 64 20 25 64 20 77 61 73 20 25 64 20 70 %d %d was %d p
da1f0 69 64 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d id=%d\n", pFile-
da200 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c 0a 09 20 >h, locktype,..
da210 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 pFile->locktyp
da220 65 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 20 20 e, getpid());.
da230 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 assert( locktype
da240 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b <=SHARED_LOCK );
da250 0a 20 20 0a 20 20 2f 2a 20 6e 6f 2d 6f 70 20 69 . . /* no-op i
da260 66 20 70 6f 73 73 69 62 6c 65 20 2a 2f 0a 20 20 f possible */.
da270 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 if( pFile->lockt
da280 79 70 65 3d 3d 6c 6f 63 6b 74 79 70 65 20 29 7b ype==locktype ){
da290 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
da2a0 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20 TE_OK;. }. .
da2b0 2f 2a 20 73 68 61 72 65 64 20 63 61 6e 20 6a 75 /* shared can ju
da2c0 73 74 20 62 65 20 73 65 74 20 62 65 63 61 75 73 st be set becaus
da2d0 65 20 77 65 20 61 6c 77 61 79 73 20 68 61 76 65 e we always have
da2e0 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 2a 2f an exclusive */
da2f0 0a 20 20 69 66 20 28 6c 6f 63 6b 74 79 70 65 3d . if (locktype=
da300 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 29 20 7b 0a =SHARED_LOCK) {.
da310 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 pFile->lockt
da320 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a ype = locktype;.
da330 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
da340 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f E_OK;. }. . /
da350 2a 20 6e 6f 2c 20 72 65 61 6c 6c 79 20 75 6e 6c * no, really unl
da360 6f 63 6b 2e 20 2a 2f 0a 20 20 69 66 20 28 20 73 ock. */. if ( s
da370 65 6d 5f 70 6f 73 74 28 70 53 65 6d 29 3d 3d 2d em_post(pSem)==-
da380 31 20 29 20 7b 0a 20 20 20 20 69 6e 74 20 72 63 1 ) {. int rc
da390 2c 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f , tErrno = errno
da3a0 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 ;. rc = sqlit
da3b0 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 eErrorFromPosixE
da3c0 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c rror(tErrno, SQL
da3d0 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b ITE_IOERR_UNLOCK
da3e0 29 3b 0a 20 20 20 20 69 66 28 20 49 53 5f 4c 4f );. if( IS_LO
da3f0 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a CK_ERROR(rc) ){.
da400 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 pFile->las
da410 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b tErrno = tErrno;
da420 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 . }. retur
da430 6e 20 72 63 3b 20 0a 20 20 7d 0a 20 20 70 46 69 n rc; . }. pFi
da440 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 4e le->locktype = N
da450 4f 5f 4c 4f 43 4b 3b 0a 20 20 72 65 74 75 72 6e O_LOCK;. return
da460 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f SQLITE_OK;.}../
da470 2a 0a 20 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 *. ** Close a fi
da480 6c 65 2e 0a 20 2a 2f 0a 73 74 61 74 69 63 20 69 le.. */.static i
da490 6e 74 20 73 65 6d 43 6c 6f 73 65 28 73 71 6c 69 nt semClose(sqli
da4a0 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 20 7b 0a te3_file *id) {.
da4b0 20 20 69 66 28 20 69 64 20 29 7b 0a 20 20 20 20 if( id ){.
da4c0 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 unixFile *pFile
da4d0 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b = (unixFile*)id;
da4e0 0a 20 20 20 20 73 65 6d 55 6e 6c 6f 63 6b 28 69 . semUnlock(i
da4f0 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 20 d, NO_LOCK);.
da500 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 assert( pFile )
da510 3b 0a 20 20 20 20 75 6e 69 78 45 6e 74 65 72 4d ;. unixEnterM
da520 75 74 65 78 28 29 3b 0a 20 20 20 20 72 65 6c 65 utex();. rele
da530 61 73 65 4c 6f 63 6b 49 6e 66 6f 28 70 46 69 6c aseLockInfo(pFil
da540 65 2d 3e 70 4c 6f 63 6b 29 3b 0a 20 20 20 20 72 e->pLock);. r
da550 65 6c 65 61 73 65 4f 70 65 6e 43 6e 74 28 70 46 eleaseOpenCnt(pF
da560 69 6c 65 2d 3e 70 4f 70 65 6e 29 3b 0a 20 20 20 ile->pOpen);.
da570 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 unixLeaveMutex(
da580 29 3b 0a 20 20 20 20 63 6c 6f 73 65 55 6e 69 78 );. closeUnix
da590 46 69 6c 65 28 69 64 29 3b 0a 20 20 7d 0a 20 20 File(id);. }.
da5a0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
da5b0 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 4f ;.}..#endif /* O
da5c0 53 5f 56 58 57 4f 52 4b 53 20 2a 2f 0a 2f 2a 0a S_VXWORKS */./*.
da5d0 2a 2a 20 4e 61 6d 65 64 20 73 65 6d 61 70 68 6f ** Named semapho
da5e0 72 65 20 6c 6f 63 6b 69 6e 67 20 69 73 20 6f 6e re locking is on
da5f0 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 ly available on
da600 56 78 57 6f 72 6b 73 2e 0a 2a 2a 0a 2a 2a 2a 2a VxWorks..**.****
da610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
da620 6f 66 20 74 68 65 20 6e 61 6d 65 64 20 73 65 6d of the named sem
da630 61 70 68 6f 72 65 20 6c 6f 63 6b 20 69 6d 70 6c aphore lock impl
da640 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a ementation *****
da650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a ***********.****
da660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
da670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
da680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
da690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
da6a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 0a 2f 2a **********/.../*
da6b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
da6c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
da6d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
da6e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
da6f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
da700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
da710 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 ********* Begin
da720 41 46 50 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a 2a AFP Locking ****
da730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
da740 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
da750 0a 2a 2a 20 41 46 50 20 69 73 20 74 68 65 20 41 .** AFP is the A
da760 70 70 6c 65 20 46 69 6c 69 6e 67 20 50 72 6f 74 pple Filing Prot
da770 6f 63 6f 6c 2e 20 20 41 46 50 20 69 73 20 61 20 ocol. AFP is a
da780 6e 65 74 77 6f 72 6b 20 66 69 6c 65 73 79 73 74 network filesyst
da790 65 6d 20 66 6f 75 6e 64 0a 2a 2a 20 6f 6e 20 41 em found.** on A
da7a0 70 70 6c 65 20 4d 61 63 69 6e 74 6f 73 68 20 63 pple Macintosh c
da7b0 6f 6d 70 75 74 65 72 73 20 2d 20 62 6f 74 68 20 omputers - both
da7c0 4f 53 39 20 61 6e 64 20 4f 53 58 2e 0a 2a 2a 0a OS9 and OSX..**.
da7d0 2a 2a 20 54 68 69 72 64 2d 70 61 72 74 79 20 69 ** Third-party i
da7e0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f mplementations o
da7f0 66 20 41 46 50 20 61 72 65 20 61 76 61 69 6c 61 f AFP are availa
da800 62 6c 65 2e 20 20 42 75 74 20 74 68 69 73 20 63 ble. But this c
da810 6f 64 65 20 68 65 72 65 0a 2a 2a 20 6f 6e 6c 79 ode here.** only
da820 20 77 6f 72 6b 73 20 6f 6e 20 4f 53 58 2e 0a 2a works on OSX..*
da830 2f 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 5f /..#if defined(_
da840 5f 41 50 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c _APPLE__) && SQL
da850 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 ITE_ENABLE_LOCKI
da860 4e 47 5f 53 54 59 4c 45 0a 2f 2a 0a 2a 2a 20 54 NG_STYLE./*.** T
da870 68 65 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e he afpLockingCon
da880 74 65 78 74 20 73 74 72 75 63 74 75 72 65 20 63 text structure c
da890 6f 6e 74 61 69 6e 73 20 61 6c 6c 20 61 66 70 20 ontains all afp
da8a0 6c 6f 63 6b 20 73 70 65 63 69 66 69 63 20 73 74 lock specific st
da8b0 61 74 65 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 ate.*/.typedef s
da8c0 74 72 75 63 74 20 61 66 70 4c 6f 63 6b 69 6e 67 truct afpLocking
da8d0 43 6f 6e 74 65 78 74 20 61 66 70 4c 6f 63 6b 69 Context afpLocki
da8e0 6e 67 43 6f 6e 74 65 78 74 3b 0a 73 74 72 75 63 ngContext;.struc
da8f0 74 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 t afpLockingCont
da900 65 78 74 20 7b 0a 20 20 75 6e 73 69 67 6e 65 64 ext {. unsigned
da910 20 6c 6f 6e 67 20 6c 6f 6e 67 20 73 68 61 72 65 long long share
da920 64 42 79 74 65 3b 0a 20 20 63 6f 6e 73 74 20 63 dByte;. const c
da930 68 61 72 20 2a 64 62 50 61 74 68 3b 20 20 20 20 har *dbPath;
da940 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 /* Name
da950 20 6f 66 20 74 68 65 20 6f 70 65 6e 20 66 69 6c of the open fil
da960 65 20 2a 2f 0a 7d 3b 0a 0a 73 74 72 75 63 74 20 e */.};..struct
da970 42 79 74 65 52 61 6e 67 65 4c 6f 63 6b 50 42 32 ByteRangeLockPB2
da980 0a 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f .{. unsigned lo
da990 6e 67 20 6c 6f 6e 67 20 6f 66 66 73 65 74 3b 20 ng long offset;
da9a0 20 20 20 20 20 20 20 2f 2a 20 6f 66 66 73 65 74 /* offset
da9b0 20 74 6f 20 66 69 72 73 74 20 62 79 74 65 20 74 to first byte t
da9c0 6f 20 6c 6f 63 6b 20 2a 2f 0a 20 20 75 6e 73 69 o lock */. unsi
da9d0 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 6c gned long long l
da9e0 65 6e 67 74 68 3b 20 20 20 20 20 20 20 20 2f 2a ength; /*
da9f0 20 6e 62 72 20 6f 66 20 62 79 74 65 73 20 74 6f nbr of bytes to
daa00 20 6c 6f 63 6b 20 2a 2f 0a 20 20 75 6e 73 69 67 lock */. unsig
daa10 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 72 65 ned long long re
daa20 74 52 61 6e 67 65 53 74 61 72 74 3b 20 2f 2a 20 tRangeStart; /*
daa30 6e 62 72 20 6f 66 20 31 73 74 20 62 79 74 65 20 nbr of 1st byte
daa40 6c 6f 63 6b 65 64 20 69 66 20 73 75 63 63 65 73 locked if succes
daa50 73 66 75 6c 20 2a 2f 0a 20 20 75 6e 73 69 67 6e sful */. unsign
daa60 65 64 20 63 68 61 72 20 75 6e 4c 6f 63 6b 46 6c ed char unLockFl
daa70 61 67 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 31 ag; /* 1
daa80 20 3d 20 75 6e 6c 6f 63 6b 2c 20 30 20 3d 20 6c = unlock, 0 = l
daa90 6f 63 6b 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 ock */. unsigne
daaa0 64 20 63 68 61 72 20 73 74 61 72 74 45 6e 64 46 d char startEndF
daab0 6c 61 67 3b 20 20 20 20 20 20 20 2f 2a 20 31 3d lag; /* 1=
daac0 72 65 6c 20 74 6f 20 65 6e 64 20 6f 66 20 66 6f rel to end of fo
daad0 72 6b 2c 20 30 3d 72 65 6c 20 74 6f 20 73 74 61 rk, 0=rel to sta
daae0 72 74 20 2a 2f 0a 20 20 69 6e 74 20 66 64 3b 20 rt */. int fd;
daaf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
dab00 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66 69 6c /* fil
dab10 65 20 64 65 73 63 20 74 6f 20 61 73 73 6f 63 20 e desc to assoc
dab20 74 68 69 73 20 6c 6f 63 6b 20 77 69 74 68 20 2a this lock with *
dab30 2f 0a 7d 3b 0a 0a 23 64 65 66 69 6e 65 20 61 66 /.};..#define af
dab40 70 66 73 42 79 74 65 52 61 6e 67 65 4c 6f 63 6b pfsByteRangeLock
dab50 32 46 53 43 54 4c 20 20 20 20 20 20 20 20 5f 49 2FSCTL _I
dab60 4f 57 52 28 27 7a 27 2c 20 32 33 2c 20 73 74 72 OWR('z', 23, str
dab70 75 63 74 20 42 79 74 65 52 61 6e 67 65 4c 6f 63 uct ByteRangeLoc
dab80 6b 50 42 32 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 kPB2)../*.** Thi
dab90 73 20 69 73 20 61 20 75 74 69 6c 69 74 79 20 66 s is a utility f
daba0 6f 72 20 73 65 74 74 69 6e 67 20 6f 72 20 63 6c or setting or cl
dabb0 65 61 72 69 6e 67 20 61 20 62 69 74 2d 72 61 6e earing a bit-ran
dabc0 67 65 20 6c 6f 63 6b 20 6f 6e 20 61 6e 0a 2a 2a ge lock on an.**
dabd0 20 41 46 50 20 66 69 6c 65 73 79 73 74 65 6d 2e AFP filesystem.
dabe0 0a 2a 2a 20 0a 2a 2a 20 52 65 74 75 72 6e 20 53 .** .** Return S
dabf0 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 QLITE_OK on succ
dac00 65 73 73 2c 20 53 51 4c 49 54 45 5f 42 55 53 59 ess, SQLITE_BUSY
dac10 20 6f 6e 20 66 61 69 6c 75 72 65 2e 0a 2a 2f 0a on failure..*/.
dac20 73 74 61 74 69 63 20 69 6e 74 20 61 66 70 53 65 static int afpSe
dac30 74 4c 6f 63 6b 28 0a 20 20 63 6f 6e 73 74 20 63 tLock(. const c
dac40 68 61 72 20 2a 70 61 74 68 2c 20 20 20 20 20 20 har *path,
dac50 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 /* Name
dac60 6f 66 20 74 68 65 20 66 69 6c 65 20 74 6f 20 62 of the file to b
dac70 65 20 6c 6f 63 6b 65 64 20 6f 72 20 75 6e 6c 6f e locked or unlo
dac80 63 6b 65 64 20 2a 2f 0a 20 20 75 6e 69 78 46 69 cked */. unixFi
dac90 6c 65 20 2a 70 46 69 6c 65 2c 20 20 20 20 20 20 le *pFile,
daca0 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e /* Open
dacb0 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 file descriptor
dacc0 20 6f 6e 20 70 61 74 68 20 2a 2f 0a 20 20 75 6e on path */. un
dacd0 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 signed long long
dace0 20 6f 66 66 73 65 74 2c 20 20 20 20 20 2f 2a 20 offset, /*
dacf0 46 69 72 73 74 20 62 79 74 65 20 74 6f 20 62 65 First byte to be
dad00 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 75 6e 73 locked */. uns
dad10 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 igned long long
dad20 6c 65 6e 67 74 68 2c 20 20 20 20 20 2f 2a 20 4e length, /* N
dad30 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 74 umber of bytes t
dad40 6f 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 o lock */. int
dad50 73 65 74 4c 6f 63 6b 46 6c 61 67 20 20 20 20 20 setLockFlag
dad60 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 /* Tr
dad70 75 65 20 74 6f 20 73 65 74 20 6c 6f 63 6b 2e 20 ue to set lock.
dad80 20 46 61 6c 73 65 20 74 6f 20 63 6c 65 61 72 20 False to clear
dad90 6c 6f 63 6b 20 2a 2f 0a 29 7b 0a 20 20 73 74 72 lock */.){. str
dada0 75 63 74 20 42 79 74 65 52 61 6e 67 65 4c 6f 63 uct ByteRangeLoc
dadb0 6b 50 42 32 20 70 62 3b 0a 20 20 69 6e 74 20 65 kPB2 pb;. int e
dadc0 72 72 3b 0a 20 20 0a 20 20 70 62 2e 75 6e 4c 6f rr;. . pb.unLo
dadd0 63 6b 46 6c 61 67 20 3d 20 73 65 74 4c 6f 63 6b ckFlag = setLock
dade0 46 6c 61 67 20 3f 20 30 20 3a 20 31 3b 0a 20 20 Flag ? 0 : 1;.
dadf0 70 62 2e 73 74 61 72 74 45 6e 64 46 6c 61 67 20 pb.startEndFlag
dae00 3d 20 30 3b 0a 20 20 70 62 2e 6f 66 66 73 65 74 = 0;. pb.offset
dae10 20 3d 20 6f 66 66 73 65 74 3b 0a 20 20 70 62 2e = offset;. pb.
dae20 6c 65 6e 67 74 68 20 3d 20 6c 65 6e 67 74 68 3b length = length;
dae30 20 0a 20 20 70 62 2e 66 64 20 3d 20 70 46 69 6c . pb.fd = pFil
dae40 65 2d 3e 68 3b 0a 20 20 0a 20 20 4f 53 54 52 41 e->h;. . OSTRA
dae50 43 45 36 28 22 41 46 50 53 45 54 4c 4f 43 4b 20 CE6("AFPSETLOCK
dae60 5b 25 73 5d 20 66 6f 72 20 25 64 25 73 20 69 6e [%s] for %d%s in
dae70 20 72 61 6e 67 65 20 25 6c 6c 78 3a 25 6c 6c 78 range %llx:%llx
dae80 5c 6e 22 2c 20 0a 20 20 20 20 28 73 65 74 4c 6f \n", . (setLo
dae90 63 6b 46 6c 61 67 3f 22 4f 4e 22 3a 22 4f 46 46 ckFlag?"ON":"OFF
daea0 22 29 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 28 70 "), pFile->h, (p
daeb0 62 2e 66 64 3d 3d 2d 31 3f 22 5b 74 65 73 74 76 b.fd==-1?"[testv
daec0 61 6c 2d 31 5d 22 3a 22 22 29 2c 0a 20 20 20 20 al-1]":""),.
daed0 6f 66 66 73 65 74 2c 20 6c 65 6e 67 74 68 29 3b offset, length);
daee0 0a 20 20 65 72 72 20 3d 20 66 73 63 74 6c 28 70 . err = fsctl(p
daef0 61 74 68 2c 20 61 66 70 66 73 42 79 74 65 52 61 ath, afpfsByteRa
daf00 6e 67 65 4c 6f 63 6b 32 46 53 43 54 4c 2c 20 26 ngeLock2FSCTL, &
daf10 70 62 2c 20 30 29 3b 0a 20 20 69 66 20 28 20 65 pb, 0);. if ( e
daf20 72 72 3d 3d 2d 31 20 29 20 7b 0a 20 20 20 20 69 rr==-1 ) {. i
daf30 6e 74 20 72 63 3b 0a 20 20 20 20 69 6e 74 20 74 nt rc;. int t
daf40 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 Errno = errno;.
daf50 20 20 20 4f 53 54 52 41 43 45 34 28 22 41 46 50 OSTRACE4("AFP
daf60 53 45 54 4c 4f 43 4b 20 66 61 69 6c 65 64 20 74 SETLOCK failed t
daf70 6f 20 66 73 63 74 6c 28 29 20 27 25 73 27 20 25 o fsctl() '%s' %
daf80 64 20 25 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20 d %s\n",.
daf90 20 20 20 20 20 20 70 61 74 68 2c 20 74 45 72 72 path, tErr
dafa0 6e 6f 2c 20 73 74 72 65 72 72 6f 72 28 74 45 72 no, strerror(tEr
dafb0 72 6e 6f 29 29 3b 0a 23 69 66 64 65 66 20 53 51 rno));.#ifdef SQ
dafc0 4c 49 54 45 5f 49 47 4e 4f 52 45 5f 41 46 50 5f LITE_IGNORE_AFP_
dafd0 4c 4f 43 4b 5f 45 52 52 4f 52 53 0a 20 20 20 20 LOCK_ERRORS.
dafe0 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 rc = SQLITE_BUSY
daff0 3b 0a 23 65 6c 73 65 0a 20 20 20 20 72 63 20 3d ;.#else. rc =
db000 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d sqliteErrorFrom
db010 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e PosixError(tErrn
db020 6f 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 o,.
db030 20 20 20 20 20 20 20 73 65 74 4c 6f 63 6b 46 6c setLockFl
db040 61 67 20 3f 20 53 51 4c 49 54 45 5f 49 4f 45 52 ag ? SQLITE_IOER
db050 52 5f 4c 4f 43 4b 20 3a 20 53 51 4c 49 54 45 5f R_LOCK : SQLITE_
db060 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b 0a 23 IOERR_UNLOCK);.#
db070 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
db080 49 47 4e 4f 52 45 5f 41 46 50 5f 4c 4f 43 4b 5f IGNORE_AFP_LOCK_
db090 45 52 52 4f 52 53 20 2a 2f 0a 20 20 20 20 69 66 ERRORS */. if
db0a0 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 ( IS_LOCK_ERROR(
db0b0 72 63 29 20 29 7b 0a 20 20 20 20 20 20 70 46 69 rc) ){. pFi
db0c0 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 le->lastErrno =
db0d0 74 45 72 72 6e 6f 3b 0a 20 20 20 20 7d 0a 20 20 tErrno;. }.
db0e0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d return rc;. }
db0f0 20 65 6c 73 65 20 7b 0a 20 20 20 20 72 65 74 75 else {. retu
db100 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
db110 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 }.}../*.** This
db120 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20 69 routine checks i
db130 66 20 74 68 65 72 65 20 69 73 20 61 20 52 45 53 f there is a RES
db140 45 52 56 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20 ERVED lock held
db150 6f 6e 20 74 68 65 20 73 70 65 63 69 66 69 65 64 on the specified
db160 0a 2a 2a 20 66 69 6c 65 20 62 79 20 74 68 69 73 .** file by this
db170 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 70 72 or any other pr
db180 6f 63 65 73 73 2e 20 49 66 20 73 75 63 68 20 61 ocess. If such a
db190 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20 73 lock is held, s
db1a0 65 74 20 2a 70 52 65 73 4f 75 74 0a 2a 2a 20 74 et *pResOut.** t
db1b0 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c o a non-zero val
db1c0 75 65 20 6f 74 68 65 72 77 69 73 65 20 2a 70 52 ue otherwise *pR
db1d0 65 73 4f 75 74 20 69 73 20 73 65 74 20 74 6f 20 esOut is set to
db1e0 7a 65 72 6f 2e 20 20 54 68 65 20 72 65 74 75 72 zero. The retur
db1f0 6e 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 73 65 n value.** is se
db200 74 20 74 6f 20 53 51 4c 49 54 45 5f 4f 4b 20 75 t to SQLITE_OK u
db210 6e 6c 65 73 73 20 61 6e 20 49 2f 4f 20 65 72 72 nless an I/O err
db220 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 or occurs during
db230 20 6c 6f 63 6b 20 63 68 65 63 6b 69 6e 67 2e 0a lock checking..
db240 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 66 */.static int af
db250 70 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f pCheckReservedLo
db260 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 ck(sqlite3_file
db270 2a 69 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 *id, int *pResOu
db280 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 t){. int rc = S
db290 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 QLITE_OK;. int
db2a0 72 65 73 65 72 76 65 64 20 3d 20 30 3b 0a 20 20 reserved = 0;.
db2b0 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 unixFile *pFile
db2c0 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b = (unixFile*)id;
db2d0 0a 20 20 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f . . SimulateIO
db2e0 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 Error( return SQ
db2f0 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b LITE_IOERR_CHECK
db300 52 45 53 45 52 56 45 44 4c 4f 43 4b 3b 20 29 3b RESERVEDLOCK; );
db310 0a 20 20 0a 20 20 61 73 73 65 72 74 28 20 70 46 . . assert( pF
db320 69 6c 65 20 29 3b 0a 20 20 61 66 70 4c 6f 63 6b ile );. afpLock
db330 69 6e 67 43 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 ingContext *cont
db340 65 78 74 20 3d 20 28 61 66 70 4c 6f 63 6b 69 6e ext = (afpLockin
db350 67 43 6f 6e 74 65 78 74 20 2a 29 20 70 46 69 6c gContext *) pFil
db360 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 e->lockingContex
db370 74 3b 0a 20 20 0a 20 20 2f 2a 20 43 68 65 63 6b t;. . /* Check
db380 20 69 66 20 61 20 74 68 72 65 61 64 20 69 6e 20 if a thread in
db390 74 68 69 73 20 70 72 6f 63 65 73 73 20 68 6f 6c this process hol
db3a0 64 73 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 2a ds such a lock *
db3b0 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c /. if( pFile->l
db3c0 6f 63 6b 74 79 70 65 3e 53 48 41 52 45 44 5f 4c ocktype>SHARED_L
db3d0 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 73 65 72 OCK ){. reser
db3e0 76 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 0a ved = 1;. }. .
db3f0 20 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 20 73 /* Otherwise s
db400 65 65 20 69 66 20 73 6f 6d 65 20 6f 74 68 65 72 ee if some other
db410 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 69 process holds i
db420 74 2e 0a 20 20 20 2a 2f 0a 20 20 69 66 28 20 21 t.. */. if( !
db430 72 65 73 65 72 76 65 64 20 29 7b 0a 20 20 20 20 reserved ){.
db440 2f 2a 20 6c 6f 63 6b 20 74 68 65 20 52 45 53 45 /* lock the RESE
db450 52 56 45 44 20 62 79 74 65 20 2a 2f 0a 20 20 20 RVED byte */.
db460 20 69 6e 74 20 6c 72 63 20 3d 20 61 66 70 53 65 int lrc = afpSe
db470 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 tLock(context->d
db480 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 52 45 bPath, pFile, RE
db490 53 45 52 56 45 44 5f 42 59 54 45 2c 20 31 2c 31 SERVED_BYTE, 1,1
db4a0 29 3b 20 20 0a 20 20 20 20 69 66 28 20 53 51 4c ); . if( SQL
db4b0 49 54 45 5f 4f 4b 3d 3d 6c 72 63 20 29 7b 0a 20 ITE_OK==lrc ){.
db4c0 20 20 20 20 20 2f 2a 20 69 66 20 77 65 20 73 75 /* if we su
db4d0 63 63 65 65 64 65 64 20 69 6e 20 74 61 6b 69 6e cceeded in takin
db4e0 67 20 74 68 65 20 72 65 73 65 72 76 65 64 20 6c g the reserved l
db4f0 6f 63 6b 2c 20 75 6e 6c 6f 63 6b 20 69 74 20 74 ock, unlock it t
db500 6f 20 72 65 73 74 6f 72 65 0a 20 20 20 20 20 20 o restore.
db510 2a 2a 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 ** the original
db520 73 74 61 74 65 20 2a 2f 0a 20 20 20 20 20 20 6c state */. l
db530 72 63 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 rc = afpSetLock(
db540 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 68 2c context->dbPath,
db550 20 70 46 69 6c 65 2c 20 52 45 53 45 52 56 45 44 pFile, RESERVED
db560 5f 42 59 54 45 2c 20 31 2c 20 30 29 3b 0a 20 20 _BYTE, 1, 0);.
db570 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 } else {.
db580 20 2f 2a 20 69 66 20 77 65 20 66 61 69 6c 65 64 /* if we failed
db590 20 74 6f 20 67 65 74 20 74 68 65 20 6c 6f 63 6b to get the lock
db5a0 20 74 68 65 6e 20 73 6f 6d 65 6f 6e 65 20 65 6c then someone el
db5b0 73 65 20 6d 75 73 74 20 68 61 76 65 20 69 74 20 se must have it
db5c0 2a 2f 0a 20 20 20 20 20 20 72 65 73 65 72 76 65 */. reserve
db5d0 64 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 d = 1;. }.
db5e0 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 if( IS_LOCK_ERR
db5f0 4f 52 28 6c 72 63 29 20 29 7b 0a 20 20 20 20 20 OR(lrc) ){.
db600 20 72 63 3d 6c 72 63 3b 0a 20 20 20 20 7d 0a 20 rc=lrc;. }.
db610 20 7d 0a 20 20 0a 20 20 4f 53 54 52 41 43 45 34 }. . OSTRACE4
db620 28 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 ("TEST WR-LOCK %
db630 64 20 25 64 20 25 64 5c 6e 22 2c 20 70 46 69 6c d %d %d\n", pFil
db640 65 2d 3e 68 2c 20 72 63 2c 20 72 65 73 65 72 76 e->h, rc, reserv
db650 65 64 29 3b 0a 20 20 0a 20 20 2a 70 52 65 73 4f ed);. . *pResO
db660 75 74 20 3d 20 72 65 73 65 72 76 65 64 3b 0a 20 ut = reserved;.
db670 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
db680 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 65 20 66 69 *.** Lock the fi
db690 6c 65 20 77 69 74 68 20 74 68 65 20 6c 6f 63 6b le with the lock
db6a0 20 73 70 65 63 69 66 69 65 64 20 62 79 20 70 61 specified by pa
db6b0 72 61 6d 65 74 65 72 20 6c 6f 63 6b 74 79 70 65 rameter locktype
db6c0 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 65 - one.** of the
db6d0 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a following:.**.*
db6e0 2a 20 20 20 20 20 28 31 29 20 53 48 41 52 45 44 * (1) SHARED
db6f0 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 32 29 _LOCK.** (2)
db700 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 0a 2a RESERVED_LOCK.*
db710 2a 20 20 20 20 20 28 33 29 20 50 45 4e 44 49 4e * (3) PENDIN
db720 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 34 G_LOCK.** (4
db730 29 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b ) EXCLUSIVE_LOCK
db740 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 .**.** Sometimes
db750 20 77 68 65 6e 20 72 65 71 75 65 73 74 69 6e 67 when requesting
db760 20 6f 6e 65 20 6c 6f 63 6b 20 73 74 61 74 65 2c one lock state,
db770 20 61 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b additional lock
db780 20 73 74 61 74 65 73 0a 2a 2a 20 61 72 65 20 69 states.** are i
db790 6e 73 65 72 74 65 64 20 69 6e 20 62 65 74 77 65 nserted in betwe
db7a0 65 6e 2e 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67 en. The locking
db7b0 20 6d 69 67 68 74 20 66 61 69 6c 20 6f 6e 20 6f might fail on o
db7c0 6e 65 20 6f 66 20 74 68 65 20 6c 61 74 65 72 0a ne of the later.
db7d0 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 6c ** transitions l
db7e0 65 61 76 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 eaving the lock
db7f0 73 74 61 74 65 20 64 69 66 66 65 72 65 6e 74 20 state different
db800 66 72 6f 6d 20 77 68 61 74 20 69 74 20 73 74 61 from what it sta
db810 72 74 65 64 20 62 75 74 0a 2a 2a 20 73 74 69 6c rted but.** stil
db820 6c 20 73 68 6f 72 74 20 6f 66 20 69 74 73 20 67 l short of its g
db830 6f 61 6c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 oal. The follow
db840 69 6e 67 20 63 68 61 72 74 20 73 68 6f 77 73 20 ing chart shows
db850 74 68 65 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74 the allowed.** t
db860 72 61 6e 73 69 74 69 6f 6e 73 20 61 6e 64 20 74 ransitions and t
db870 68 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74 65 he inserted inte
db880 72 6d 65 64 69 61 74 65 20 73 74 61 74 65 73 3a rmediate states:
db890 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b .**.** UNLOCK
db8a0 45 44 20 2d 3e 20 53 48 41 52 45 44 0a 2a 2a 20 ED -> SHARED.**
db8b0 20 20 20 53 48 41 52 45 44 20 2d 3e 20 52 45 53 SHARED -> RES
db8c0 45 52 56 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 ERVED.** SHAR
db8d0 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 ED -> (PENDING)
db8e0 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 -> EXCLUSIVE.**
db8f0 20 20 20 52 45 53 45 52 56 45 44 20 2d 3e 20 28 RESERVED -> (
db900 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c PENDING) -> EXCL
db910 55 53 49 56 45 0a 2a 2a 20 20 20 20 50 45 4e 44 USIVE.** PEND
db920 49 4e 47 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 ING -> EXCLUSIVE
db930 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
db940 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 69 6e ine will only in
db950 63 72 65 61 73 65 20 61 20 6c 6f 63 6b 2e 20 20 crease a lock.
db960 55 73 65 20 74 68 65 20 73 71 6c 69 74 65 33 4f Use the sqlite3O
db970 73 55 6e 6c 6f 63 6b 28 29 0a 2a 2a 20 72 6f 75 sUnlock().** rou
db980 74 69 6e 65 20 74 6f 20 6c 6f 77 65 72 20 61 20 tine to lower a
db990 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2e 0a 2a locking level..*
db9a0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 66 70 /.static int afp
db9b0 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c Lock(sqlite3_fil
db9c0 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 e *id, int lockt
db9d0 79 70 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d ype){. int rc =
db9e0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 6e SQLITE_OK;. un
db9f0 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 ixFile *pFile =
dba00 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 (unixFile*)id;.
dba10 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 afpLockingConte
dba20 78 74 20 2a 63 6f 6e 74 65 78 74 20 3d 20 28 61 xt *context = (a
dba30 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 fpLockingContext
dba40 20 2a 29 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 *) pFile->locki
dba50 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 0a 20 20 ngContext;. .
dba60 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b assert( pFile );
dba70 0a 20 20 4f 53 54 52 41 43 45 35 28 22 4c 4f 43 . OSTRACE5("LOC
dba80 4b 20 20 20 20 25 64 20 25 73 20 77 61 73 20 25 K %d %s was %
dba90 73 20 70 69 64 3d 25 64 5c 6e 22 2c 20 70 46 69 s pid=%d\n", pFi
dbaa0 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 20 le->h,.
dbab0 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 6c 6f 63 locktypeName(loc
dbac0 6b 74 79 70 65 29 2c 20 6c 6f 63 6b 74 79 70 65 ktype), locktype
dbad0 4e 61 6d 65 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b Name(pFile->lock
dbae0 74 79 70 65 29 2c 20 67 65 74 70 69 64 28 29 29 type), getpid())
dbaf0 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 ;.. /* If there
dbb00 20 69 73 20 61 6c 72 65 61 64 79 20 61 20 6c 6f is already a lo
dbb10 63 6b 20 6f 66 20 74 68 69 73 20 74 79 70 65 20 ck of this type
dbb20 6f 72 20 6d 6f 72 65 20 72 65 73 74 72 69 63 74 or more restrict
dbb30 69 76 65 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 ive on the. **
dbb40 75 6e 69 78 46 69 6c 65 2c 20 64 6f 20 6e 6f 74 unixFile, do not
dbb50 68 69 6e 67 2e 20 44 6f 6e 27 74 20 75 73 65 20 hing. Don't use
dbb60 74 68 65 20 61 66 70 5f 65 6e 64 5f 6c 6f 63 6b the afp_end_lock
dbb70 3a 20 65 78 69 74 20 70 61 74 68 2c 20 61 73 0a : exit path, as.
dbb80 20 20 2a 2a 20 75 6e 69 78 45 6e 74 65 72 4d 75 ** unixEnterMu
dbb90 74 65 78 28 29 20 68 61 73 6e 27 74 20 62 65 65 tex() hasn't bee
dbba0 6e 20 63 61 6c 6c 65 64 20 79 65 74 2e 0a 20 20 n called yet..
dbbb0 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e */. if( pFile->
dbbc0 6c 6f 63 6b 74 79 70 65 3e 3d 6c 6f 63 6b 74 79 locktype>=lockty
dbbd0 70 65 20 29 7b 0a 20 20 20 20 4f 53 54 52 41 43 pe ){. OSTRAC
dbbe0 45 33 28 22 4c 4f 43 4b 20 20 20 20 25 64 20 25 E3("LOCK %d %
dbbf0 73 20 6f 6b 20 28 61 6c 72 65 61 64 79 20 68 65 s ok (already he
dbc00 6c 64 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 ld)\n", pFile->h
dbc10 2c 0a 20 20 20 20 20 20 20 20 20 20 20 6c 6f 63 ,. loc
dbc20 6b 74 79 70 65 4e 61 6d 65 28 6c 6f 63 6b 74 79 ktypeName(lockty
dbc30 70 65 29 29 3b 0a 20 20 20 20 72 65 74 75 72 6e pe));. return
dbc40 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a SQLITE_OK;. }.
dbc50 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 . /* Make sure
dbc60 74 68 65 20 6c 6f 63 6b 69 6e 67 20 73 65 71 75 the locking sequ
dbc70 65 6e 63 65 20 69 73 20 63 6f 72 72 65 63 74 0a ence is correct.
dbc80 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 */. assert( p
dbc90 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 21 3d File->locktype!=
dbca0 4e 4f 5f 4c 4f 43 4b 20 7c 7c 20 6c 6f 63 6b 74 NO_LOCK || lockt
dbcb0 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b ype==SHARED_LOCK
dbcc0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6c 6f );. assert( lo
dbcd0 63 6b 74 79 70 65 21 3d 50 45 4e 44 49 4e 47 5f cktype!=PENDING_
dbce0 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 LOCK );. assert
dbcf0 28 20 6c 6f 63 6b 74 79 70 65 21 3d 52 45 53 45 ( locktype!=RESE
dbd00 52 56 45 44 5f 4c 4f 43 4b 20 7c 7c 20 70 46 69 RVED_LOCK || pFi
dbd10 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 le->locktype==SH
dbd20 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 0a ARED_LOCK );. .
dbd30 20 20 2f 2a 20 54 68 69 73 20 6d 75 74 65 78 20 /* This mutex
dbd40 69 73 20 6e 65 65 64 65 64 20 62 65 63 61 75 73 is needed becaus
dbd50 65 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 20 69 e pFile->pLock i
dbd60 73 20 73 68 61 72 65 64 20 61 63 72 6f 73 73 20 s shared across
dbd70 74 68 72 65 61 64 73 0a 20 20 2a 2f 0a 20 20 75 threads. */. u
dbd80 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b nixEnterMutex();
dbd90 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 .. /* Make sure
dbda0 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 68 72 the current thr
dbdb0 65 61 64 20 6f 77 6e 73 20 74 68 65 20 70 46 69 ead owns the pFi
dbdc0 6c 65 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 le.. */. rc =
dbdd0 74 72 61 6e 73 66 65 72 4f 77 6e 65 72 73 68 69 transferOwnershi
dbde0 70 28 70 46 69 6c 65 29 3b 0a 20 20 69 66 28 20 p(pFile);. if(
dbdf0 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
dbe00 0a 20 20 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 . unixLeaveMu
dbe10 74 65 78 28 29 3b 0a 20 20 20 20 72 65 74 75 72 tex();. retur
dbe20 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 20 20 0a 20 n rc;. }. .
dbe30 20 2f 2a 20 41 20 50 45 4e 44 49 4e 47 20 6c 6f /* A PENDING lo
dbe40 63 6b 20 69 73 20 6e 65 65 64 65 64 20 62 65 66 ck is needed bef
dbe50 6f 72 65 20 61 63 71 75 69 72 69 6e 67 20 61 20 ore acquiring a
dbe60 53 48 41 52 45 44 20 6c 6f 63 6b 20 61 6e 64 20 SHARED lock and
dbe70 62 65 66 6f 72 65 0a 20 20 2a 2a 20 61 63 71 75 before. ** acqu
dbe80 69 72 69 6e 67 20 61 6e 20 45 58 43 4c 55 53 49 iring an EXCLUSI
dbe90 56 45 20 6c 6f 63 6b 2e 20 20 46 6f 72 20 74 68 VE lock. For th
dbea0 65 20 53 48 41 52 45 44 20 6c 6f 63 6b 2c 20 74 e SHARED lock, t
dbeb0 68 65 20 50 45 4e 44 49 4e 47 20 77 69 6c 6c 0a he PENDING will.
dbec0 20 20 2a 2a 20 62 65 20 72 65 6c 65 61 73 65 64 ** be released
dbed0 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 .. */. if( loc
dbee0 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f ktype==SHARED_LO
dbef0 43 4b 20 0a 20 20 20 20 20 20 7c 7c 20 28 6c 6f CK . || (lo
dbf00 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 cktype==EXCLUSIV
dbf10 45 5f 4c 4f 43 4b 20 26 26 20 70 46 69 6c 65 2d E_LOCK && pFile-
dbf20 3e 6c 6f 63 6b 74 79 70 65 3c 50 45 4e 44 49 4e >locktype<PENDIN
dbf30 47 5f 4c 4f 43 4b 29 0a 20 20 29 7b 0a 20 20 20 G_LOCK). ){.
dbf40 20 69 6e 74 20 66 61 69 6c 65 64 3b 0a 20 20 20 int failed;.
dbf50 20 66 61 69 6c 65 64 20 3d 20 61 66 70 53 65 74 failed = afpSet
dbf60 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62 Lock(context->db
dbf70 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 50 45 4e Path, pFile, PEN
dbf80 44 49 4e 47 5f 42 59 54 45 2c 20 31 2c 20 31 29 DING_BYTE, 1, 1)
dbf90 3b 0a 20 20 20 20 69 66 20 28 66 61 69 6c 65 64 ;. if (failed
dbfa0 29 20 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 66 ) {. rc = f
dbfb0 61 69 6c 65 64 3b 0a 20 20 20 20 20 20 67 6f 74 ailed;. got
dbfc0 6f 20 61 66 70 5f 65 6e 64 5f 6c 6f 63 6b 3b 0a o afp_end_lock;.
dbfd0 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0a 20 20 2f }. }. . /
dbfe0 2a 20 49 66 20 63 6f 6e 74 72 6f 6c 20 67 65 74 * If control get
dbff0 73 20 74 6f 20 74 68 69 73 20 70 6f 69 6e 74 2c s to this point,
dc000 20 74 68 65 6e 20 61 63 74 75 61 6c 6c 79 20 67 then actually g
dc010 6f 20 61 68 65 61 64 20 61 6e 64 20 6d 61 6b 65 o ahead and make
dc020 0a 20 20 2a 2a 20 6f 70 65 72 61 74 69 6e 67 20 . ** operating
dc030 73 79 73 74 65 6d 20 63 61 6c 6c 73 20 66 6f 72 system calls for
dc040 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 6c the specified l
dc050 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 ock.. */. if(
dc060 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 locktype==SHARED
dc070 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 69 6e 74 _LOCK ){. int
dc080 20 6c 6b 2c 20 6c 72 63 31 2c 20 6c 72 63 32 2c lk, lrc1, lrc2,
dc090 20 6c 72 63 31 45 72 72 6e 6f 3b 0a 20 20 20 20 lrc1Errno;.
dc0a0 0a 20 20 20 20 2f 2a 20 4e 6f 77 20 67 65 74 20 . /* Now get
dc0b0 74 68 65 20 72 65 61 64 2d 6c 6f 63 6b 20 53 48 the read-lock SH
dc0c0 41 52 45 44 5f 4c 4f 43 4b 20 2a 2f 0a 20 20 20 ARED_LOCK */.
dc0d0 20 2f 2a 20 6e 6f 74 65 20 74 68 61 74 20 74 68 /* note that th
dc0e0 65 20 71 75 61 6c 69 74 79 20 6f 66 20 74 68 65 e quality of the
dc0f0 20 72 61 6e 64 6f 6d 6e 65 73 73 20 64 6f 65 73 randomness does
dc100 6e 27 74 20 6d 61 74 74 65 72 20 74 68 61 74 20 n't matter that
dc110 6d 75 63 68 20 2a 2f 0a 20 20 20 20 6c 6b 20 3d much */. lk =
dc120 20 72 61 6e 64 6f 6d 28 29 3b 20 0a 20 20 20 20 random(); .
dc130 63 6f 6e 74 65 78 74 2d 3e 73 68 61 72 65 64 42 context->sharedB
dc140 79 74 65 20 3d 20 28 6c 6b 20 26 20 30 78 37 66 yte = (lk & 0x7f
dc150 66 66 66 66 66 66 29 25 28 53 48 41 52 45 44 5f ffffff)%(SHARED_
dc160 53 49 5a 45 20 2d 20 31 29 3b 0a 20 20 20 20 6c SIZE - 1);. l
dc170 72 63 31 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b rc1 = afpSetLock
dc180 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 68 (context->dbPath
dc190 2c 20 70 46 69 6c 65 2c 20 0a 20 20 20 20 20 20 , pFile, .
dc1a0 20 20 20 20 53 48 41 52 45 44 5f 46 49 52 53 54 SHARED_FIRST
dc1b0 2b 63 6f 6e 74 65 78 74 2d 3e 73 68 61 72 65 64 +context->shared
dc1c0 42 79 74 65 2c 20 31 2c 20 31 29 3b 0a 20 20 20 Byte, 1, 1);.
dc1d0 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 if( IS_LOCK_ERR
dc1e0 4f 52 28 6c 72 63 31 29 20 29 7b 0a 20 20 20 20 OR(lrc1) ){.
dc1f0 20 20 6c 72 63 31 45 72 72 6e 6f 20 3d 20 70 46 lrc1Errno = pF
dc200 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 3b 0a ile->lastErrno;.
dc210 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 44 72 6f }. /* Dro
dc220 70 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 p the temporary
dc230 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 2a 2f 0a PENDING lock */.
dc240 20 20 20 20 6c 72 63 32 20 3d 20 61 66 70 53 65 lrc2 = afpSe
dc250 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 tLock(context->d
dc260 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 50 45 bPath, pFile, PE
dc270 4e 44 49 4e 47 5f 42 59 54 45 2c 20 31 2c 20 30 NDING_BYTE, 1, 0
dc280 29 3b 0a 20 20 20 20 0a 20 20 20 20 69 66 28 20 );. . if(
dc290 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 6c 72 IS_LOCK_ERROR(lr
dc2a0 63 31 29 20 29 20 7b 0a 20 20 20 20 20 20 70 46 c1) ) {. pF
dc2b0 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d ile->lastErrno =
dc2c0 20 6c 72 63 31 45 72 72 6e 6f 3b 0a 20 20 20 20 lrc1Errno;.
dc2d0 20 20 72 63 20 3d 20 6c 72 63 31 3b 0a 20 20 20 rc = lrc1;.
dc2e0 20 20 20 67 6f 74 6f 20 61 66 70 5f 65 6e 64 5f goto afp_end_
dc2f0 6c 6f 63 6b 3b 0a 20 20 20 20 7d 20 65 6c 73 65 lock;. } else
dc300 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 if( IS_LOCK_ERR
dc310 4f 52 28 6c 72 63 32 29 20 29 7b 0a 20 20 20 20 OR(lrc2) ){.
dc320 20 20 72 63 20 3d 20 6c 72 63 32 3b 0a 20 20 20 rc = lrc2;.
dc330 20 20 20 67 6f 74 6f 20 61 66 70 5f 65 6e 64 5f goto afp_end_
dc340 6c 6f 63 6b 3b 0a 20 20 20 20 7d 20 65 6c 73 65 lock;. } else
dc350 20 69 66 28 20 6c 72 63 31 20 21 3d 20 53 51 4c if( lrc1 != SQL
dc360 49 54 45 5f 4f 4b 20 29 20 7b 0a 20 20 20 20 20 ITE_OK ) {.
dc370 20 72 63 20 3d 20 6c 72 63 31 3b 0a 20 20 20 20 rc = lrc1;.
dc380 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 70 } else {. p
dc390 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d File->locktype =
dc3a0 20 53 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20 SHARED_LOCK;.
dc3b0 20 20 20 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e pFile->pOpen
dc3c0 2d 3e 6e 4c 6f 63 6b 2b 2b 3b 0a 20 20 20 20 7d ->nLock++;. }
dc3d0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a . }else{. /*
dc3e0 20 54 68 65 20 72 65 71 75 65 73 74 20 77 61 73 The request was
dc3f0 20 66 6f 72 20 61 20 52 45 53 45 52 56 45 44 20 for a RESERVED
dc400 6f 72 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 or EXCLUSIVE loc
dc410 6b 2e 20 20 49 74 20 69 73 0a 20 20 20 20 2a 2a k. It is. **
dc420 20 61 73 73 75 6d 65 64 20 74 68 61 74 20 74 68 assumed that th
dc430 65 72 65 20 69 73 20 61 20 53 48 41 52 45 44 20 ere is a SHARED
dc440 6f 72 20 67 72 65 61 74 65 72 20 6c 6f 63 6b 20 or greater lock
dc450 6f 6e 20 74 68 65 20 66 69 6c 65 0a 20 20 20 20 on the file.
dc460 2a 2a 20 61 6c 72 65 61 64 79 2e 0a 20 20 20 20 ** already..
dc470 2a 2f 0a 20 20 20 20 69 6e 74 20 66 61 69 6c 65 */. int faile
dc480 64 20 3d 20 30 3b 0a 20 20 20 20 61 73 73 65 72 d = 0;. asser
dc490 74 28 20 30 21 3d 70 46 69 6c 65 2d 3e 6c 6f 63 t( 0!=pFile->loc
dc4a0 6b 74 79 70 65 20 29 3b 0a 20 20 20 20 69 66 20 ktype );. if
dc4b0 28 6c 6f 63 6b 74 79 70 65 20 3e 3d 20 52 45 53 (locktype >= RES
dc4c0 45 52 56 45 44 5f 4c 4f 43 4b 20 26 26 20 70 46 ERVED_LOCK && pF
dc4d0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3c 20 ile->locktype <
dc4e0 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 29 20 7b RESERVED_LOCK) {
dc4f0 0a 20 20 20 20 20 20 20 20 2f 2a 20 41 63 71 75 . /* Acqu
dc500 69 72 65 20 61 20 52 45 53 45 52 56 45 44 20 6c ire a RESERVED l
dc510 6f 63 6b 20 2a 2f 0a 20 20 20 20 20 20 20 20 66 ock */. f
dc520 61 69 6c 65 64 20 3d 20 61 66 70 53 65 74 4c 6f ailed = afpSetLo
dc530 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61 ck(context->dbPa
dc540 74 68 2c 20 70 46 69 6c 65 2c 20 52 45 53 45 52 th, pFile, RESER
dc550 56 45 44 5f 42 59 54 45 2c 20 31 2c 31 29 3b 0a VED_BYTE, 1,1);.
dc560 20 20 20 20 7d 0a 20 20 20 20 69 66 20 28 21 66 }. if (!f
dc570 61 69 6c 65 64 20 26 26 20 6c 6f 63 6b 74 79 70 ailed && locktyp
dc580 65 20 3d 3d 20 45 58 43 4c 55 53 49 56 45 5f 4c e == EXCLUSIVE_L
dc590 4f 43 4b 29 20 7b 0a 20 20 20 20 20 20 2f 2a 20 OCK) {. /*
dc5a0 41 63 71 75 69 72 65 20 61 6e 20 45 58 43 4c 55 Acquire an EXCLU
dc5b0 53 49 56 45 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 SIVE lock */.
dc5c0 20 20 20 20 20 0a 20 20 20 20 20 20 2f 2a 20 52 . /* R
dc5d0 65 6d 6f 76 65 20 74 68 65 20 73 68 61 72 65 64 emove the shared
dc5e0 20 6c 6f 63 6b 20 62 65 66 6f 72 65 20 74 72 79 lock before try
dc5f0 69 6e 67 20 74 68 65 20 72 61 6e 67 65 2e 20 20 ing the range.
dc600 77 65 27 6c 6c 20 6e 65 65 64 20 74 6f 20 0a 20 we'll need to .
dc610 20 20 20 20 20 2a 2a 20 72 65 65 73 74 61 62 6c ** reestabl
dc620 69 73 68 20 74 68 65 20 73 68 61 72 65 64 20 6c ish the shared l
dc630 6f 63 6b 20 69 66 20 77 65 20 63 61 6e 27 74 20 ock if we can't
dc640 67 65 74 20 74 68 65 20 20 61 66 70 55 6e 6c 6f get the afpUnlo
dc650 63 6b 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 ck. */.
dc660 20 20 69 66 28 20 21 28 66 61 69 6c 65 64 20 3d if( !(failed =
dc670 20 61 66 70 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 afpSetLock(cont
dc680 65 78 74 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 ext->dbPath, pFi
dc690 6c 65 2c 20 53 48 41 52 45 44 5f 46 49 52 53 54 le, SHARED_FIRST
dc6a0 20 2b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 +.
dc6b0 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 cont
dc6c0 65 78 74 2d 3e 73 68 61 72 65 64 42 79 74 65 2c ext->sharedByte,
dc6d0 20 31 2c 20 30 29 29 20 29 7b 0a 20 20 20 20 20 1, 0)) ){.
dc6e0 20 20 20 69 6e 74 20 66 61 69 6c 65 64 32 20 3d int failed2 =
dc6f0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 SQLITE_OK;.
dc700 20 20 20 20 2f 2a 20 6e 6f 77 20 61 74 74 65 6d /* now attem
dc710 6d 70 74 20 74 6f 20 67 65 74 20 74 68 65 20 65 mpt to get the e
dc720 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 72 61 xclusive lock ra
dc730 6e 67 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 66 nge */. f
dc740 61 69 6c 65 64 20 3d 20 61 66 70 53 65 74 4c 6f ailed = afpSetLo
dc750 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61 ck(context->dbPa
dc760 74 68 2c 20 70 46 69 6c 65 2c 20 53 48 41 52 45 th, pFile, SHARE
dc770 44 5f 46 49 52 53 54 2c 20 0a 20 20 20 20 20 20 D_FIRST, .
dc780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
dc790 20 20 20 20 20 20 20 20 20 53 48 41 52 45 44 5f SHARED_
dc7a0 53 49 5a 45 2c 20 31 29 3b 0a 20 20 20 20 20 20 SIZE, 1);.
dc7b0 20 20 69 66 28 20 66 61 69 6c 65 64 20 26 26 20 if( failed &&
dc7c0 28 66 61 69 6c 65 64 32 20 3d 20 61 66 70 53 65 (failed2 = afpSe
dc7d0 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 tLock(context->d
dc7e0 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 0a 20 bPath, pFile, .
dc7f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
dc800 20 20 20 20 20 20 53 48 41 52 45 44 5f 46 49 52 SHARED_FIR
dc810 53 54 20 2b 20 63 6f 6e 74 65 78 74 2d 3e 73 68 ST + context->sh
dc820 61 72 65 64 42 79 74 65 2c 20 31 2c 20 31 29 29 aredByte, 1, 1))
dc830 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a ){. /*
dc840 20 43 61 6e 27 74 20 72 65 65 73 74 61 62 6c 69 Can't reestabli
dc850 73 68 20 74 68 65 20 73 68 61 72 65 64 20 6c 6f sh the shared lo
dc860 63 6b 2e 20 20 53 71 6c 69 74 65 20 63 61 6e 27 ck. Sqlite can'
dc870 74 20 64 65 61 6c 2c 20 74 68 69 73 20 69 73 0a t deal, this is.
dc880 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61 20 63 ** a c
dc890 72 69 74 69 63 61 6c 20 49 2f 4f 20 65 72 72 6f ritical I/O erro
dc8a0 72 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 r. */.
dc8b0 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 28 28 rc = ((
dc8c0 66 61 69 6c 65 64 20 26 20 53 51 4c 49 54 45 5f failed & SQLITE_
dc8d0 49 4f 45 52 52 29 20 3d 3d 20 53 51 4c 49 54 45 IOERR) == SQLITE
dc8e0 5f 49 4f 45 52 52 29 20 3f 20 66 61 69 6c 65 64 _IOERR) ? failed
dc8f0 32 20 3a 20 0a 20 20 20 20 20 20 20 20 20 20 20 2 : .
dc900 20 20 20 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 SQLITE_IOERR
dc910 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 20 20 20 _LOCK;.
dc920 20 67 6f 74 6f 20 61 66 70 5f 65 6e 64 5f 6c 6f goto afp_end_lo
dc930 63 6b 3b 0a 20 20 20 20 20 20 20 20 7d 20 0a 20 ck;. } .
dc940 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
dc950 20 20 20 20 72 63 20 3d 20 66 61 69 6c 65 64 3b rc = failed;
dc960 20 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a . }. }.
dc970 20 20 20 20 69 66 28 20 66 61 69 6c 65 64 20 29 if( failed )
dc980 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 66 61 69 {. rc = fai
dc990 6c 65 64 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 led;. }. }.
dc9a0 20 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 . if( rc==SQLI
dc9b0 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 46 69 TE_OK ){. pFi
dc9c0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c le->locktype = l
dc9d0 6f 63 6b 74 79 70 65 3b 0a 20 20 7d 65 6c 73 65 ocktype;. }else
dc9e0 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 45 if( locktype==E
dc9f0 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 29 7b XCLUSIVE_LOCK ){
dca00 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b . pFile->lock
dca10 74 79 70 65 20 3d 20 50 45 4e 44 49 4e 47 5f 4c type = PENDING_L
dca20 4f 43 4b 3b 0a 20 20 7d 0a 20 20 0a 61 66 70 5f OCK;. }. .afp_
dca30 65 6e 64 5f 6c 6f 63 6b 3a 0a 20 20 75 6e 69 78 end_lock:. unix
dca40 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 LeaveMutex();.
dca50 4f 53 54 52 41 43 45 34 28 22 4c 4f 43 4b 20 20 OSTRACE4("LOCK
dca60 20 20 25 64 20 25 73 20 25 73 5c 6e 22 2c 20 70 %d %s %s\n", p
dca70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 File->h, locktyp
dca80 65 4e 61 6d 65 28 6c 6f 63 6b 74 79 70 65 29 2c eName(locktype),
dca90 20 0a 20 20 20 20 20 20 20 20 20 72 63 3d 3d 53 . rc==S
dcaa0 51 4c 49 54 45 5f 4f 4b 20 3f 20 22 6f 6b 22 20 QLITE_OK ? "ok"
dcab0 3a 20 22 66 61 69 6c 65 64 22 29 3b 0a 20 20 72 : "failed");. r
dcac0 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
dcad0 2a 2a 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f 63 ** Lower the loc
dcae0 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66 69 king level on fi
dcaf0 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 70 46 le descriptor pF
dcb00 69 6c 65 20 74 6f 20 6c 6f 63 6b 74 79 70 65 2e ile to locktype.
dcb10 20 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 locktype.** mu
dcb20 73 74 20 62 65 20 65 69 74 68 65 72 20 4e 4f 5f st be either NO_
dcb30 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45 44 5f 4c LOCK or SHARED_L
dcb40 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 OCK..**.** If th
dcb50 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 e locking level
dcb60 6f 66 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 of the file desc
dcb70 72 69 70 74 6f 72 20 69 73 20 61 6c 72 65 61 64 riptor is alread
dcb80 79 20 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a y at or below.**
dcb90 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 6c the requested l
dcba0 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74 68 ocking level, th
dcbb0 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 is routine is a
dcbc0 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 no-op..*/.static
dcbd0 20 69 6e 74 20 61 66 70 55 6e 6c 6f 63 6b 28 73 int afpUnlock(s
dcbe0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c qlite3_file *id,
dcbf0 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b int locktype) {
dcc00 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
dcc10 54 45 5f 4f 4b 3b 0a 20 20 75 6e 69 78 46 69 6c TE_OK;. unixFil
dcc20 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 e *pFile = (unix
dcc30 46 69 6c 65 2a 29 69 64 3b 0a 20 20 61 66 70 4c File*)id;. afpL
dcc40 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70 ockingContext *p
dcc50 43 74 78 20 3d 20 28 61 66 70 4c 6f 63 6b 69 6e Ctx = (afpLockin
dcc60 67 43 6f 6e 74 65 78 74 20 2a 29 20 70 46 69 6c gContext *) pFil
dcc70 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 e->lockingContex
dcc80 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 46 t;.. assert( pF
dcc90 69 6c 65 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 ile );. OSTRACE
dcca0 35 28 22 55 4e 4c 4f 43 4b 20 20 25 64 20 25 64 5("UNLOCK %d %d
dccb0 20 77 61 73 20 25 64 20 70 69 64 3d 25 64 5c 6e was %d pid=%d\n
dccc0 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 ", pFile->h, loc
dccd0 6b 74 79 70 65 2c 0a 20 20 20 20 20 20 20 20 20 ktype,.
dcce0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 2c pFile->locktype,
dccf0 20 67 65 74 70 69 64 28 29 29 3b 0a 0a 20 20 61 getpid());.. a
dcd00 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 3c ssert( locktype<
dcd10 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a =SHARED_LOCK );.
dcd20 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 if( pFile->loc
dcd30 6b 74 79 70 65 3c 3d 6c 6f 63 6b 74 79 70 65 20 ktype<=locktype
dcd40 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 ){. return SQ
dcd50 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 LITE_OK;. }. i
dcd60 66 28 20 43 48 45 43 4b 5f 54 48 52 45 41 44 49 f( CHECK_THREADI
dcd70 44 28 70 46 69 6c 65 29 20 29 7b 0a 20 20 20 20 D(pFile) ){.
dcd80 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 return SQLITE_MI
dcd90 53 55 53 45 3b 0a 20 20 7d 0a 20 20 75 6e 69 78 SUSE;. }. unix
dcda0 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 EnterMutex();.
dcdb0 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 if( pFile->lockt
dcdc0 79 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b 20 ype>SHARED_LOCK
dcdd0 29 7b 0a 20 20 20 20 0a 20 20 20 20 69 66 28 20 ){. . if(
dcde0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d pFile->locktype=
dcdf0 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 =EXCLUSIVE_LOCK
dce00 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 61 66 ){. rc = af
dce10 70 53 65 74 4c 6f 63 6b 28 70 43 74 78 2d 3e 64 pSetLock(pCtx->d
dce20 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 53 48 bPath, pFile, SH
dce30 41 52 45 44 5f 46 49 52 53 54 2c 20 53 48 41 52 ARED_FIRST, SHAR
dce40 45 44 5f 53 49 5a 45 2c 20 30 29 3b 0a 20 20 20 ED_SIZE, 0);.
dce50 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
dce60 45 5f 4f 4b 20 26 26 20 6c 6f 63 6b 74 79 70 65 E_OK && locktype
dce70 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b ==SHARED_LOCK ){
dce80 0a 20 20 20 20 20 20 20 20 2f 2a 20 6f 6e 6c 79 . /* only
dce90 20 72 65 2d 65 73 74 61 62 6c 69 73 68 20 74 68 re-establish th
dcea0 65 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 69 66 e shared lock if
dceb0 20 6e 65 63 65 73 73 61 72 79 20 2a 2f 0a 20 20 necessary */.
dcec0 20 20 20 20 20 20 69 6e 74 20 73 68 61 72 65 64 int shared
dced0 4c 6f 63 6b 42 79 74 65 20 3d 20 53 48 41 52 45 LockByte = SHARE
dcee0 44 5f 46 49 52 53 54 2b 70 43 74 78 2d 3e 73 68 D_FIRST+pCtx->sh
dcef0 61 72 65 64 42 79 74 65 3b 0a 20 20 20 20 20 20 aredByte;.
dcf00 20 20 72 63 20 3d 20 61 66 70 53 65 74 4c 6f 63 rc = afpSetLoc
dcf10 6b 28 70 43 74 78 2d 3e 64 62 50 61 74 68 2c 20 k(pCtx->dbPath,
dcf20 70 46 69 6c 65 2c 20 73 68 61 72 65 64 4c 6f 63 pFile, sharedLoc
dcf30 6b 42 79 74 65 2c 20 31 2c 20 31 29 3b 0a 20 20 kByte, 1, 1);.
dcf40 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 }. }.
dcf50 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
dcf60 4b 20 26 26 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b K && pFile->lock
dcf70 74 79 70 65 3e 3d 50 45 4e 44 49 4e 47 5f 4c 4f type>=PENDING_LO
dcf80 43 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d CK ){. rc =
dcf90 20 61 66 70 53 65 74 4c 6f 63 6b 28 70 43 74 78 afpSetLock(pCtx
dcfa0 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c ->dbPath, pFile,
dcfb0 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c 20 31 PENDING_BYTE, 1
dcfc0 2c 20 30 29 3b 0a 20 20 20 20 7d 20 0a 20 20 20 , 0);. } .
dcfd0 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
dcfe0 4f 4b 20 26 26 20 70 46 69 6c 65 2d 3e 6c 6f 63 OK && pFile->loc
dcff0 6b 74 79 70 65 3e 3d 52 45 53 45 52 56 45 44 5f ktype>=RESERVED_
dd000 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 LOCK ){. rc
dd010 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 70 43 = afpSetLock(pC
dd020 74 78 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c tx->dbPath, pFil
dd030 65 2c 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 e, RESERVED_BYTE
dd040 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 , 1, 0);. }.
dd050 20 7d 65 6c 73 65 20 69 66 28 20 6c 6f 63 6b 74 }else if( lockt
dd060 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b 0a ype==NO_LOCK ){.
dd070 20 20 20 20 2f 2a 20 63 6c 65 61 72 20 74 68 65 /* clear the
dd080 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 2a 2f 0a shared lock */.
dd090 20 20 20 20 69 6e 74 20 73 68 61 72 65 64 4c 6f int sharedLo
dd0a0 63 6b 42 79 74 65 20 3d 20 53 48 41 52 45 44 5f ckByte = SHARED_
dd0b0 46 49 52 53 54 2b 70 43 74 78 2d 3e 73 68 61 72 FIRST+pCtx->shar
dd0c0 65 64 42 79 74 65 3b 0a 20 20 20 20 72 63 20 3d edByte;. rc =
dd0d0 20 61 66 70 53 65 74 4c 6f 63 6b 28 70 43 74 78 afpSetLock(pCtx
dd0e0 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c ->dbPath, pFile,
dd0f0 20 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 65 2c sharedLockByte,
dd100 20 31 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20 20 69 1, 0);. }.. i
dd110 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
dd120 20 29 7b 0a 20 20 20 20 69 66 28 20 6c 6f 63 6b ){. if( lock
dd130 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b type==NO_LOCK ){
dd140 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 75 6e . struct un
dd150 69 78 4f 70 65 6e 43 6e 74 20 2a 70 4f 70 65 6e ixOpenCnt *pOpen
dd160 20 3d 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 3b = pFile->pOpen;
dd170 0a 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 6e 4c . pOpen->nL
dd180 6f 63 6b 2d 2d 3b 0a 20 20 20 20 20 20 61 73 73 ock--;. ass
dd190 65 72 74 28 20 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 ert( pOpen->nLoc
dd1a0 6b 3e 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 66 k>=0 );. if
dd1b0 28 20 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 3d 3d ( pOpen->nLock==
dd1c0 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 0 ){. rc
dd1d0 3d 20 63 6c 6f 73 65 50 65 6e 64 69 6e 67 46 64 = closePendingFd
dd1e0 73 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 20 20 s(pFile);.
dd1f0 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 75 6e }. }. }. un
dd200 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a ixLeaveMutex();.
dd210 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
dd220 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 _OK ){. pFile
dd230 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 ->locktype = loc
dd240 6b 74 79 70 65 3b 0a 20 20 7d 0a 20 20 72 65 74 ktype;. }. ret
dd250 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
dd260 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 20 26 20 Close a file &
dd270 63 6c 65 61 6e 75 70 20 41 46 50 20 73 70 65 63 cleanup AFP spec
dd280 69 66 69 63 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e ific locking con
dd290 74 65 78 74 20 0a 2a 2f 0a 73 74 61 74 69 63 20 text .*/.static
dd2a0 69 6e 74 20 61 66 70 43 6c 6f 73 65 28 73 71 6c int afpClose(sql
dd2b0 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 20 7b ite3_file *id) {
dd2c0 0a 20 20 69 66 28 20 69 64 20 29 7b 0a 20 20 20 . if( id ){.
dd2d0 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 unixFile *pFile
dd2e0 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 = (unixFile*)id
dd2f0 3b 0a 20 20 20 20 61 66 70 55 6e 6c 6f 63 6b 28 ;. afpUnlock(
dd300 69 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 id, NO_LOCK);.
dd310 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 unixEnterMutex
dd320 28 29 3b 0a 20 20 20 20 69 66 28 20 70 46 69 6c ();. if( pFil
dd330 65 2d 3e 70 4f 70 65 6e 20 26 26 20 70 46 69 6c e->pOpen && pFil
dd340 65 2d 3e 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 20 e->pOpen->nLock
dd350 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 ){. /* If t
dd360 68 65 72 65 20 61 72 65 20 6f 75 74 73 74 61 6e here are outstan
dd370 64 69 6e 67 20 6c 6f 63 6b 73 2c 20 64 6f 20 6e ding locks, do n
dd380 6f 74 20 61 63 74 75 61 6c 6c 79 20 63 6c 6f 73 ot actually clos
dd390 65 20 74 68 65 20 66 69 6c 65 20 6a 75 73 74 0a e the file just.
dd3a0 20 20 20 20 20 20 2a 2a 20 79 65 74 20 62 65 63 ** yet bec
dd3b0 61 75 73 65 20 74 68 61 74 20 77 6f 75 6c 64 20 ause that would
dd3c0 63 6c 65 61 72 20 74 68 6f 73 65 20 6c 6f 63 6b clear those lock
dd3d0 73 2e 20 20 49 6e 73 74 65 61 64 2c 20 61 64 64 s. Instead, add
dd3e0 20 74 68 65 20 66 69 6c 65 0a 20 20 20 20 20 20 the file.
dd3f0 2a 2a 20 64 65 73 63 72 69 70 74 6f 72 20 74 6f ** descriptor to
dd400 20 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 6e 67 pOpen->aPending
dd410 2e 20 20 49 74 20 77 69 6c 6c 20 62 65 20 61 75 . It will be au
dd420 74 6f 6d 61 74 69 63 61 6c 6c 79 20 63 6c 6f 73 tomatically clos
dd430 65 64 20 77 68 65 6e 0a 20 20 20 20 20 20 2a 2a ed when. **
dd440 20 74 68 65 20 6c 61 73 74 20 6c 6f 63 6b 20 69 the last lock i
dd450 73 20 63 6c 65 61 72 65 64 2e 0a 20 20 20 20 20 s cleared..
dd460 20 2a 2f 0a 20 20 20 20 20 20 73 65 74 50 65 6e */. setPen
dd470 64 69 6e 67 46 64 28 70 46 69 6c 65 29 3b 0a 20 dingFd(pFile);.
dd480 20 20 20 7d 0a 20 20 20 20 72 65 6c 65 61 73 65 }. release
dd490 4f 70 65 6e 43 6e 74 28 70 46 69 6c 65 2d 3e 70 OpenCnt(pFile->p
dd4a0 4f 70 65 6e 29 3b 0a 20 20 20 20 73 71 6c 69 74 Open);. sqlit
dd4b0 65 33 5f 66 72 65 65 28 70 46 69 6c 65 2d 3e 6c e3_free(pFile->l
dd4c0 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 29 3b 0a ockingContext);.
dd4d0 20 20 20 20 63 6c 6f 73 65 55 6e 69 78 46 69 6c closeUnixFil
dd4e0 65 28 69 64 29 3b 0a 20 20 20 20 75 6e 69 78 4c e(id);. unixL
dd4f0 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 7d eaveMutex();. }
dd500 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
dd510 5f 4f 4b 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f _OK;.}..#endif /
dd520 2a 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c * defined(__APPL
dd530 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 45 5f 45 E__) && SQLITE_E
dd540 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 NABLE_LOCKING_ST
dd550 59 4c 45 20 2a 2f 0a 2f 2a 0a 2a 2a 20 54 68 65 YLE */./*.** The
dd560 20 63 6f 64 65 20 61 62 6f 76 65 20 69 73 20 74 code above is t
dd570 68 65 20 41 46 50 20 6c 6f 63 6b 20 69 6d 70 6c he AFP lock impl
dd580 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 20 54 68 65 ementation. The
dd590 20 63 6f 64 65 20 69 73 20 73 70 65 63 69 66 69 code is specifi
dd5a0 63 0a 2a 2a 20 74 6f 20 4d 61 63 4f 53 58 20 61 c.** to MacOSX a
dd5b0 6e 64 20 64 6f 65 73 20 6e 6f 74 20 77 6f 72 6b nd does not work
dd5c0 20 6f 6e 20 6f 74 68 65 72 20 75 6e 69 78 20 70 on other unix p
dd5d0 6c 61 74 66 6f 72 6d 73 2e 20 20 4e 6f 20 61 6c latforms. No al
dd5e0 74 65 72 6e 61 74 69 76 65 0a 2a 2a 20 69 73 20 ternative.** is
dd5f0 61 76 61 69 6c 61 62 6c 65 2e 20 20 49 66 20 79 available. If y
dd600 6f 75 20 64 6f 6e 27 74 20 63 6f 6d 70 69 6c 65 ou don't compile
dd610 20 66 6f 72 20 61 20 6d 61 63 2c 20 74 68 65 6e for a mac, then
dd620 20 74 68 65 20 22 75 6e 69 78 2d 61 66 70 22 0a the "unix-afp".
dd630 2a 2a 20 56 46 53 20 69 73 20 6e 6f 74 20 61 76 ** VFS is not av
dd640 61 69 6c 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 2a 2a ailable..**.****
dd650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dd660 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 41 46 50 * End of the AFP
dd670 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 lock implementa
dd680 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a tion ***********
dd690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a ***********.****
dd6a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dd6b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dd6c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dd6d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dd6e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 0a 2f 2a **********/.../*
dd6f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dd700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dd710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dd720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dd730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
dd740 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 4e ************** N
dd750 6f 6e 2d 6c 6f 63 6b 69 6e 67 20 73 71 6c 69 74 on-locking sqlit
dd760 65 33 5f 66 69 6c 65 20 6d 65 74 68 6f 64 73 20 e3_file methods
dd770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dd780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
dd790 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 64 69 76 .** The next div
dd7a0 69 73 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 69 ision contains i
dd7b0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 66 mplementations f
dd7c0 6f 72 20 61 6c 6c 20 6d 65 74 68 6f 64 73 20 6f or all methods o
dd7d0 66 20 74 68 65 20 0a 2a 2a 20 73 71 6c 69 74 65 f the .** sqlite
dd7e0 33 5f 66 69 6c 65 20 6f 62 6a 65 63 74 20 6f 74 3_file object ot
dd7f0 68 65 72 20 74 68 61 6e 20 74 68 65 20 6c 6f 63 her than the loc
dd800 6b 69 6e 67 20 6d 65 74 68 6f 64 73 2e 20 20 54 king methods. T
dd810 68 65 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 6d 65 he locking.** me
dd820 74 68 6f 64 73 20 77 65 72 65 20 64 65 66 69 6e thods were defin
dd830 65 64 20 69 6e 20 64 69 76 69 73 69 6f 6e 73 20 ed in divisions
dd840 61 62 6f 76 65 20 28 6f 6e 65 20 6c 6f 63 6b 69 above (one locki
dd850 6e 67 20 6d 65 74 68 6f 64 20 70 65 72 0a 2a 2a ng method per.**
dd860 20 64 69 76 69 73 69 6f 6e 29 2e 20 20 54 68 6f division). Tho
dd870 73 65 20 6d 65 74 68 6f 64 73 20 74 68 61 74 20 se methods that
dd880 61 72 65 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c are common to al
dd890 6c 20 6c 6f 63 6b 69 6e 67 20 6d 6f 64 65 73 0a l locking modes.
dd8a0 2a 2a 20 61 72 65 20 67 61 74 68 65 72 20 74 6f ** are gather to
dd8b0 67 65 74 68 65 72 20 69 6e 74 6f 20 74 68 69 73 gether into this
dd8c0 20 64 69 76 69 73 69 6f 6e 2e 0a 2a 2f 0a 0a 2f division..*/../
dd8d0 2a 0a 2a 2a 20 53 65 65 6b 20 74 6f 20 74 68 65 *.** Seek to the
dd8e0 20 6f 66 66 73 65 74 20 70 61 73 73 65 64 20 61 offset passed a
dd8f0 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 s the second arg
dd900 75 6d 65 6e 74 2c 20 74 68 65 6e 20 72 65 61 64 ument, then read
dd910 20 63 6e 74 20 0a 2a 2a 20 62 79 74 65 73 20 69 cnt .** bytes i
dd920 6e 74 6f 20 70 42 75 66 2e 20 52 65 74 75 72 6e nto pBuf. Return
dd930 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 the number of b
dd940 79 74 65 73 20 61 63 74 75 61 6c 6c 79 20 72 65 ytes actually re
dd950 61 64 2e 0a 2a 2a 0a 2a 2a 20 4e 42 3a 20 20 49 ad..**.** NB: I
dd960 66 20 79 6f 75 20 64 65 66 69 6e 65 20 55 53 45 f you define USE
dd970 5f 50 52 45 41 44 20 6f 72 20 55 53 45 5f 50 52 _PREAD or USE_PR
dd980 45 41 44 36 34 2c 20 74 68 65 6e 20 69 74 20 6d EAD64, then it m
dd990 69 67 68 74 20 61 6c 73 6f 0a 2a 2a 20 62 65 20 ight also.** be
dd9a0 6e 65 63 65 73 73 61 72 79 20 74 6f 20 64 65 66 necessary to def
dd9b0 69 6e 65 20 5f 58 4f 50 45 4e 5f 53 4f 55 52 43 ine _XOPEN_SOURC
dd9c0 45 20 74 6f 20 62 65 20 35 30 30 2e 20 20 54 68 E to be 500. Th
dd9d0 69 73 20 76 61 72 69 65 73 20 66 72 6f 6d 0a 2a is varies from.*
dd9e0 2a 20 6f 6e 65 20 73 79 73 74 65 6d 20 74 6f 20 * one system to
dd9f0 61 6e 6f 74 68 65 72 2e 20 20 53 69 6e 63 65 20 another. Since
dda00 53 51 4c 69 74 65 20 64 6f 65 73 20 6e 6f 74 20 SQLite does not
dda10 64 65 66 69 6e 65 20 55 53 45 5f 50 52 45 41 44 define USE_PREAD
dda20 0a 2a 2a 20 61 6e 79 20 61 6e 79 20 66 6f 72 6d .** any any form
dda30 20 62 79 20 64 65 66 61 75 6c 74 2c 20 77 65 20 by default, we
dda40 77 69 6c 6c 20 6e 6f 74 20 61 74 74 65 6d 70 74 will not attempt
dda50 20 74 6f 20 64 65 66 69 6e 65 20 5f 58 4f 50 45 to define _XOPE
dda60 4e 5f 53 4f 55 52 43 45 2e 0a 2a 2a 20 53 65 65 N_SOURCE..** See
dda70 20 74 69 63 6b 65 74 73 20 23 32 37 34 31 20 61 tickets #2741 a
dda80 6e 64 20 23 32 36 38 31 2e 0a 2a 2a 0a 2a 2a 20 nd #2681..**.**
dda90 54 6f 20 61 76 6f 69 64 20 73 74 6f 6d 70 69 6e To avoid stompin
ddaa0 67 20 74 68 65 20 65 72 72 6e 6f 20 76 61 6c 75 g the errno valu
ddab0 65 20 6f 6e 20 61 20 66 61 69 6c 65 64 20 72 65 e on a failed re
ddac0 61 64 20 74 68 65 20 6c 61 73 74 45 72 72 6e 6f ad the lastErrno
ddad0 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 73 65 74 value.** is set
ddae0 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e before returnin
ddaf0 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 g..*/.static int
ddb00 20 73 65 65 6b 41 6e 64 52 65 61 64 28 75 6e 69 seekAndRead(uni
ddb10 78 46 69 6c 65 20 2a 69 64 2c 20 73 71 6c 69 74 xFile *id, sqlit
ddb20 65 33 5f 69 6e 74 36 34 20 6f 66 66 73 65 74 2c e3_int64 offset,
ddb30 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 69 6e 74 void *pBuf, int
ddb40 20 63 6e 74 29 7b 0a 20 20 69 6e 74 20 67 6f 74 cnt){. int got
ddb50 3b 0a 20 20 69 36 34 20 6e 65 77 4f 66 66 73 65 ;. i64 newOffse
ddb60 74 3b 0a 20 20 54 49 4d 45 52 5f 53 54 41 52 54 t;. TIMER_START
ddb70 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28 55 53 ;.#if defined(US
ddb80 45 5f 50 52 45 41 44 29 0a 20 20 67 6f 74 20 3d E_PREAD). got =
ddb90 20 70 72 65 61 64 28 69 64 2d 3e 68 2c 20 70 42 pread(id->h, pB
ddba0 75 66 2c 20 63 6e 74 2c 20 6f 66 66 73 65 74 29 uf, cnt, offset)
ddbb0 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 ;. SimulateIOEr
ddbc0 72 6f 72 28 20 67 6f 74 20 3d 20 2d 31 20 29 3b ror( got = -1 );
ddbd0 0a 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28 55 .#elif defined(U
ddbe0 53 45 5f 50 52 45 41 44 36 34 29 0a 20 20 67 6f SE_PREAD64). go
ddbf0 74 20 3d 20 70 72 65 61 64 36 34 28 69 64 2d 3e t = pread64(id->
ddc00 68 2c 20 70 42 75 66 2c 20 63 6e 74 2c 20 6f 66 h, pBuf, cnt, of
ddc10 66 73 65 74 29 3b 0a 20 20 53 69 6d 75 6c 61 74 fset);. Simulat
ddc20 65 49 4f 45 72 72 6f 72 28 20 67 6f 74 20 3d 20 eIOError( got =
ddc30 2d 31 20 29 3b 0a 23 65 6c 73 65 0a 20 20 6e 65 -1 );.#else. ne
ddc40 77 4f 66 66 73 65 74 20 3d 20 6c 73 65 65 6b 28 wOffset = lseek(
ddc50 69 64 2d 3e 68 2c 20 6f 66 66 73 65 74 2c 20 53 id->h, offset, S
ddc60 45 45 4b 5f 53 45 54 29 3b 0a 20 20 53 69 6d 75 EEK_SET);. Simu
ddc70 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 6e 65 77 lateIOError( new
ddc80 4f 66 66 73 65 74 2d 2d 20 29 3b 0a 20 20 69 66 Offset-- );. if
ddc90 28 20 6e 65 77 4f 66 66 73 65 74 21 3d 6f 66 66 ( newOffset!=off
ddca0 73 65 74 20 29 7b 0a 20 20 20 20 69 66 28 20 6e set ){. if( n
ddcb0 65 77 4f 66 66 73 65 74 20 3d 3d 20 2d 31 20 29 ewOffset == -1 )
ddcc0 7b 0a 20 20 20 20 20 20 28 28 75 6e 69 78 46 69 {. ((unixFi
ddcd0 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 74 45 72 72 le*)id)->lastErr
ddce0 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 no = errno;.
ddcf0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 28 28 75 }else{. ((u
ddd00 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 nixFile*)id)->la
ddd10 73 74 45 72 72 6e 6f 20 3d 20 30 3b 09 09 09 0a stErrno = 0;....
ddd20 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e }. return
ddd30 20 2d 31 3b 0a 20 20 7d 0a 20 20 67 6f 74 20 3d -1;. }. got =
ddd40 20 72 65 61 64 28 69 64 2d 3e 68 2c 20 70 42 75 read(id->h, pBu
ddd50 66 2c 20 63 6e 74 29 3b 0a 23 65 6e 64 69 66 0a f, cnt);.#endif.
ddd60 20 20 54 49 4d 45 52 5f 45 4e 44 3b 0a 20 20 69 TIMER_END;. i
ddd70 66 28 20 67 6f 74 3c 30 20 29 7b 0a 20 20 20 20 f( got<0 ){.
ddd80 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d ((unixFile*)id)-
ddd90 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 >lastErrno = err
ddda0 6e 6f 3b 0a 20 20 7d 0a 20 20 4f 53 54 52 41 43 no;. }. OSTRAC
dddb0 45 35 28 22 52 45 41 44 20 20 20 20 25 2d 33 64 E5("READ %-3d
dddc0 20 25 35 64 20 25 37 6c 6c 64 20 25 6c 6c 75 5c %5d %7lld %llu\
dddd0 6e 22 2c 20 69 64 2d 3e 68 2c 20 67 6f 74 2c 20 n", id->h, got,
ddde0 6f 66 66 73 65 74 2c 20 54 49 4d 45 52 5f 45 4c offset, TIMER_EL
dddf0 41 50 53 45 44 29 3b 0a 20 20 72 65 74 75 72 6e APSED);. return
dde00 20 67 6f 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 got;.}../*.** R
dde10 65 61 64 20 64 61 74 61 20 66 72 6f 6d 20 61 20 ead data from a
dde20 66 69 6c 65 20 69 6e 74 6f 20 61 20 62 75 66 66 file into a buff
dde30 65 72 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49 er. Return SQLI
dde40 54 45 5f 4f 4b 20 69 66 20 61 6c 6c 0a 2a 2a 20 TE_OK if all.**
dde50 62 79 74 65 73 20 77 65 72 65 20 72 65 61 64 20 bytes were read
dde60 73 75 63 63 65 73 73 66 75 6c 6c 79 20 61 6e 64 successfully and
dde70 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 69 66 SQLITE_IOERR if
dde80 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 0a 2a anything goes.*
dde90 2a 20 77 72 6f 6e 67 2e 0a 2a 2f 0a 73 74 61 74 * wrong..*/.stat
ddea0 69 63 20 69 6e 74 20 75 6e 69 78 52 65 61 64 28 ic int unixRead(
ddeb0 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 . sqlite3_file
ddec0 2a 69 64 2c 20 0a 20 20 76 6f 69 64 20 2a 70 42 *id, . void *pB
dded0 75 66 2c 20 0a 20 20 69 6e 74 20 61 6d 74 2c 0a uf, . int amt,.
ddee0 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 sqlite3_int64
ddef0 6f 66 66 73 65 74 0a 29 7b 0a 20 20 75 6e 69 78 offset.){. unix
ddf00 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 File *pFile = (u
ddf10 6e 69 78 46 69 6c 65 20 2a 29 69 64 3b 0a 20 20 nixFile *)id;.
ddf20 69 6e 74 20 67 6f 74 3b 0a 20 20 61 73 73 65 72 int got;. asser
ddf30 74 28 20 69 64 20 29 3b 0a 0a 20 20 2f 2a 20 49 t( id );.. /* I
ddf40 66 20 74 68 69 73 20 69 73 20 61 20 64 61 74 61 f this is a data
ddf50 62 61 73 65 20 66 69 6c 65 20 28 6e 6f 74 20 61 base file (not a
ddf60 20 6a 6f 75 72 6e 61 6c 2c 20 6d 61 73 74 65 72 journal, master
ddf70 2d 6a 6f 75 72 6e 61 6c 20 6f 72 20 74 65 6d 70 -journal or temp
ddf80 0a 20 20 2a 2a 20 66 69 6c 65 29 2c 20 74 68 65 . ** file), the
ddf90 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 6c 6f bytes in the lo
ddfa0 63 6b 69 6e 67 20 72 61 6e 67 65 20 73 68 6f 75 cking range shou
ddfb0 6c 64 20 6e 65 76 65 72 20 62 65 20 72 65 61 64 ld never be read
ddfc0 20 6f 72 20 77 72 69 74 74 65 6e 2e 20 2a 2f 0a or written. */.
ddfd0 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d assert( pFile-
ddfe0 3e 70 55 6e 75 73 65 64 3d 3d 30 0a 20 20 20 20 >pUnused==0.
ddff0 20 20 20 7c 7c 20 6f 66 66 73 65 74 3e 3d 50 45 || offset>=PE
de000 4e 44 49 4e 47 5f 42 59 54 45 2b 35 31 32 0a 20 NDING_BYTE+512.
de010 20 20 20 20 20 20 7c 7c 20 6f 66 66 73 65 74 2b || offset+
de020 61 6d 74 3c 3d 50 45 4e 44 49 4e 47 5f 42 59 54 amt<=PENDING_BYT
de030 45 20 0a 20 20 29 3b 0a 0a 20 20 67 6f 74 20 3d E . );.. got =
de040 20 73 65 65 6b 41 6e 64 52 65 61 64 28 70 46 69 seekAndRead(pFi
de050 6c 65 2c 20 6f 66 66 73 65 74 2c 20 70 42 75 66 le, offset, pBuf
de060 2c 20 61 6d 74 29 3b 0a 20 20 69 66 28 20 67 6f , amt);. if( go
de070 74 3d 3d 61 6d 74 20 29 7b 0a 20 20 20 20 72 65 t==amt ){. re
de080 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
de090 20 20 7d 65 6c 73 65 20 69 66 28 20 67 6f 74 3c }else if( got<
de0a0 30 20 29 7b 0a 20 20 20 20 2f 2a 20 6c 61 73 74 0 ){. /* last
de0b0 45 72 72 6e 6f 20 73 65 74 20 62 79 20 73 65 65 Errno set by see
de0c0 6b 41 6e 64 52 65 61 64 20 2a 2f 0a 20 20 20 20 kAndRead */.
de0d0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f return SQLITE_IO
de0e0 45 52 52 5f 52 45 41 44 3b 0a 20 20 7d 65 6c 73 ERR_READ;. }els
de0f0 65 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 e{. pFile->la
de100 73 74 45 72 72 6e 6f 20 3d 20 30 3b 20 2f 2a 20 stErrno = 0; /*
de110 6e 6f 74 20 61 20 73 79 73 74 65 6d 20 65 72 72 not a system err
de120 6f 72 20 2a 2f 0a 20 20 20 20 2f 2a 20 55 6e 72 or */. /* Unr
de130 65 61 64 20 70 61 72 74 73 20 6f 66 20 74 68 65 ead parts of the
de140 20 62 75 66 66 65 72 20 6d 75 73 74 20 62 65 20 buffer must be
de150 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 2a 2f 0a 20 zero-filled */.
de160 20 20 20 6d 65 6d 73 65 74 28 26 28 28 63 68 61 memset(&((cha
de170 72 2a 29 70 42 75 66 29 5b 67 6f 74 5d 2c 20 30 r*)pBuf)[got], 0
de180 2c 20 61 6d 74 2d 67 6f 74 29 3b 0a 20 20 20 20 , amt-got);.
de190 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f return SQLITE_IO
de1a0 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 3b 0a ERR_SHORT_READ;.
de1b0 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 65 }.}../*.** See
de1c0 6b 20 74 6f 20 74 68 65 20 6f 66 66 73 65 74 20 k to the offset
de1d0 69 6e 20 69 64 2d 3e 6f 66 66 73 65 74 20 74 68 in id->offset th
de1e0 65 6e 20 72 65 61 64 20 63 6e 74 20 62 79 74 65 en read cnt byte
de1f0 73 20 69 6e 74 6f 20 70 42 75 66 2e 0a 2a 2a 20 s into pBuf..**
de200 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 Return the numbe
de210 72 20 6f 66 20 62 79 74 65 73 20 61 63 74 75 61 r of bytes actua
de220 6c 6c 79 20 72 65 61 64 2e 20 20 55 70 64 61 74 lly read. Updat
de230 65 20 74 68 65 20 6f 66 66 73 65 74 2e 0a 2a 2a e the offset..**
de240 0a 2a 2a 20 54 6f 20 61 76 6f 69 64 20 73 74 6f .** To avoid sto
de250 6d 70 69 6e 67 20 74 68 65 20 65 72 72 6e 6f 20 mping the errno
de260 76 61 6c 75 65 20 6f 6e 20 61 20 66 61 69 6c 65 value on a faile
de270 64 20 77 72 69 74 65 20 74 68 65 20 6c 61 73 74 d write the last
de280 45 72 72 6e 6f 20 76 61 6c 75 65 0a 2a 2a 20 69 Errno value.** i
de290 73 20 73 65 74 20 62 65 66 6f 72 65 20 72 65 74 s set before ret
de2a0 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 urning..*/.stati
de2b0 63 20 69 6e 74 20 73 65 65 6b 41 6e 64 57 72 69 c int seekAndWri
de2c0 74 65 28 75 6e 69 78 46 69 6c 65 20 2a 69 64 2c te(unixFile *id,
de2d0 20 69 36 34 20 6f 66 66 73 65 74 2c 20 63 6f 6e i64 offset, con
de2e0 73 74 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 69 st void *pBuf, i
de2f0 6e 74 20 63 6e 74 29 7b 0a 20 20 69 6e 74 20 67 nt cnt){. int g
de300 6f 74 3b 0a 20 20 69 36 34 20 6e 65 77 4f 66 66 ot;. i64 newOff
de310 73 65 74 3b 0a 20 20 54 49 4d 45 52 5f 53 54 41 set;. TIMER_STA
de320 52 54 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28 RT;.#if defined(
de330 55 53 45 5f 50 52 45 41 44 29 0a 20 20 67 6f 74 USE_PREAD). got
de340 20 3d 20 70 77 72 69 74 65 28 69 64 2d 3e 68 2c = pwrite(id->h,
de350 20 70 42 75 66 2c 20 63 6e 74 2c 20 6f 66 66 73 pBuf, cnt, offs
de360 65 74 29 3b 0a 23 65 6c 69 66 20 64 65 66 69 6e et);.#elif defin
de370 65 64 28 55 53 45 5f 50 52 45 41 44 36 34 29 0a ed(USE_PREAD64).
de380 20 20 67 6f 74 20 3d 20 70 77 72 69 74 65 36 34 got = pwrite64
de390 28 69 64 2d 3e 68 2c 20 70 42 75 66 2c 20 63 6e (id->h, pBuf, cn
de3a0 74 2c 20 6f 66 66 73 65 74 29 3b 0a 23 65 6c 73 t, offset);.#els
de3b0 65 0a 20 20 6e 65 77 4f 66 66 73 65 74 20 3d 20 e. newOffset =
de3c0 6c 73 65 65 6b 28 69 64 2d 3e 68 2c 20 6f 66 66 lseek(id->h, off
de3d0 73 65 74 2c 20 53 45 45 4b 5f 53 45 54 29 3b 0a set, SEEK_SET);.
de3e0 20 20 69 66 28 20 6e 65 77 4f 66 66 73 65 74 21 if( newOffset!
de3f0 3d 6f 66 66 73 65 74 20 29 7b 0a 20 20 20 20 69 =offset ){. i
de400 66 28 20 6e 65 77 4f 66 66 73 65 74 20 3d 3d 20 f( newOffset ==
de410 2d 31 20 29 7b 0a 20 20 20 20 20 20 28 28 75 6e -1 ){. ((un
de420 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 ixFile*)id)->las
de430 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a tErrno = errno;.
de440 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
de450 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 ((unixFile*)id)
de460 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 30 3b ->lastErrno = 0;
de470 09 09 09 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 .... }. re
de480 74 75 72 6e 20 2d 31 3b 0a 20 20 7d 0a 20 20 67 turn -1;. }. g
de490 6f 74 20 3d 20 77 72 69 74 65 28 69 64 2d 3e 68 ot = write(id->h
de4a0 2c 20 70 42 75 66 2c 20 63 6e 74 29 3b 0a 23 65 , pBuf, cnt);.#e
de4b0 6e 64 69 66 0a 20 20 54 49 4d 45 52 5f 45 4e 44 ndif. TIMER_END
de4c0 3b 0a 20 20 69 66 28 20 67 6f 74 3c 30 20 29 7b ;. if( got<0 ){
de4d0 0a 20 20 20 20 28 28 75 6e 69 78 46 69 6c 65 2a . ((unixFile*
de4e0 29 69 64 29 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 )id)->lastErrno
de4f0 3d 20 65 72 72 6e 6f 3b 0a 20 20 7d 0a 0a 20 20 = errno;. }..
de500 4f 53 54 52 41 43 45 35 28 22 57 52 49 54 45 20 OSTRACE5("WRITE
de510 20 20 25 2d 33 64 20 25 35 64 20 25 37 6c 6c 64 %-3d %5d %7lld
de520 20 25 6c 6c 75 5c 6e 22 2c 20 69 64 2d 3e 68 2c %llu\n", id->h,
de530 20 67 6f 74 2c 20 6f 66 66 73 65 74 2c 20 54 49 got, offset, TI
de540 4d 45 52 5f 45 4c 41 50 53 45 44 29 3b 0a 20 20 MER_ELAPSED);.
de550 72 65 74 75 72 6e 20 67 6f 74 3b 0a 7d 0a 0a 0a return got;.}...
de560 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 64 61 74 61 /*.** Write data
de570 20 66 72 6f 6d 20 61 20 62 75 66 66 65 72 20 69 from a buffer i
de580 6e 74 6f 20 61 20 66 69 6c 65 2e 20 20 52 65 74 nto a file. Ret
de590 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e urn SQLITE_OK on
de5a0 20 73 75 63 63 65 73 73 0a 2a 2a 20 6f 72 20 73 success.** or s
de5b0 6f 6d 65 20 6f 74 68 65 72 20 65 72 72 6f 72 20 ome other error
de5c0 63 6f 64 65 20 6f 6e 20 66 61 69 6c 75 72 65 2e code on failure.
de5d0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 .*/.static int u
de5e0 6e 69 78 57 72 69 74 65 28 0a 20 20 73 71 6c 69 nixWrite(. sqli
de5f0 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 0a 20 te3_file *id, .
de600 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 42 75 const void *pBu
de610 66 2c 20 0a 20 20 69 6e 74 20 61 6d 74 2c 0a 20 f, . int amt,.
de620 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6f sqlite3_int64 o
de630 66 66 73 65 74 20 0a 29 7b 0a 20 20 75 6e 69 78 ffset .){. unix
de640 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 File *pFile = (u
de650 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 69 nixFile*)id;. i
de660 6e 74 20 77 72 6f 74 65 20 3d 20 30 3b 0a 20 20 nt wrote = 0;.
de670 61 73 73 65 72 74 28 20 69 64 20 29 3b 0a 20 20 assert( id );.
de680 61 73 73 65 72 74 28 20 61 6d 74 3e 30 20 29 3b assert( amt>0 );
de690 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 .. /* If this i
de6a0 73 20 61 20 64 61 74 61 62 61 73 65 20 66 69 6c s a database fil
de6b0 65 20 28 6e 6f 74 20 61 20 6a 6f 75 72 6e 61 6c e (not a journal
de6c0 2c 20 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c , master-journal
de6d0 20 6f 72 20 74 65 6d 70 0a 20 20 2a 2a 20 66 69 or temp. ** fi
de6e0 6c 65 29 2c 20 74 68 65 20 62 79 74 65 73 20 69 le), the bytes i
de6f0 6e 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 72 61 n the locking ra
de700 6e 67 65 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 nge should never
de710 20 62 65 20 72 65 61 64 20 6f 72 20 77 72 69 74 be read or writ
de720 74 65 6e 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 ten. */. assert
de730 28 20 70 46 69 6c 65 2d 3e 70 55 6e 75 73 65 64 ( pFile->pUnused
de740 3d 3d 30 0a 20 20 20 20 20 20 20 7c 7c 20 6f 66 ==0. || of
de750 66 73 65 74 3e 3d 50 45 4e 44 49 4e 47 5f 42 59 fset>=PENDING_BY
de760 54 45 2b 35 31 32 0a 20 20 20 20 20 20 20 7c 7c TE+512. ||
de770 20 6f 66 66 73 65 74 2b 61 6d 74 3c 3d 50 45 4e offset+amt<=PEN
de780 44 49 4e 47 5f 42 59 54 45 20 0a 20 20 29 3b 0a DING_BYTE . );.
de790 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a .#ifndef NDEBUG.
de7a0 20 20 2f 2a 20 49 66 20 77 65 20 61 72 65 20 64 /* If we are d
de7b0 6f 69 6e 67 20 61 20 6e 6f 72 6d 61 6c 20 77 72 oing a normal wr
de7c0 69 74 65 20 74 6f 20 61 20 64 61 74 61 62 61 73 ite to a databas
de7d0 65 20 66 69 6c 65 20 28 61 73 20 6f 70 70 6f 73 e file (as oppos
de7e0 65 64 20 74 6f 0a 20 20 2a 2a 20 64 6f 69 6e 67 ed to. ** doing
de7f0 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 a hot-journal r
de800 6f 6c 6c 62 61 63 6b 20 6f 72 20 61 20 77 72 69 ollback or a wri
de810 74 65 20 74 6f 20 73 6f 6d 65 20 66 69 6c 65 20 te to some file
de820 6f 74 68 65 72 20 74 68 61 6e 20 61 0a 20 20 2a other than a. *
de830 2a 20 6e 6f 72 6d 61 6c 20 64 61 74 61 62 61 73 * normal databas
de840 65 20 66 69 6c 65 29 20 74 68 65 6e 20 72 65 63 e file) then rec
de850 6f 72 64 20 74 68 65 20 66 61 63 74 20 74 68 61 ord the fact tha
de860 74 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 20 t the database.
de870 20 2a 2a 20 68 61 73 20 63 68 61 6e 67 65 64 2e ** has changed.
de880 20 20 49 66 20 74 68 65 20 74 72 61 6e 73 61 63 If the transac
de890 74 69 6f 6e 20 63 6f 75 6e 74 65 72 20 69 73 20 tion counter is
de8a0 6d 6f 64 69 66 69 65 64 2c 20 72 65 63 6f 72 64 modified, record
de8b0 20 74 68 61 74 0a 20 20 2a 2a 20 66 61 63 74 20 that. ** fact
de8c0 74 6f 6f 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 too.. */. if(
de8d0 70 46 69 6c 65 2d 3e 69 6e 4e 6f 72 6d 61 6c 57 pFile->inNormalW
de8e0 72 69 74 65 20 29 7b 0a 20 20 20 20 70 46 69 6c rite ){. pFil
de8f0 65 2d 3e 64 62 55 70 64 61 74 65 20 3d 20 31 3b e->dbUpdate = 1;
de900 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 /* The databas
de910 65 20 68 61 73 20 62 65 65 6e 20 6d 6f 64 69 66 e has been modif
de920 69 65 64 20 2a 2f 0a 20 20 20 20 69 66 28 20 6f ied */. if( o
de930 66 66 73 65 74 3c 3d 32 34 20 26 26 20 6f 66 66 ffset<=24 && off
de940 73 65 74 2b 61 6d 74 3e 3d 32 37 20 29 7b 0a 20 set+amt>=27 ){.
de950 20 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 int rc;.
de960 20 20 20 63 68 61 72 20 6f 6c 64 43 6e 74 72 5b char oldCntr[
de970 34 5d 3b 0a 20 20 20 20 20 20 53 69 6d 75 6c 61 4];. Simula
de980 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 teIOErrorBenign(
de990 31 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 1);. rc = s
de9a0 65 65 6b 41 6e 64 52 65 61 64 28 70 46 69 6c 65 eekAndRead(pFile
de9b0 2c 20 32 34 2c 20 6f 6c 64 43 6e 74 72 2c 20 34 , 24, oldCntr, 4
de9c0 29 3b 0a 20 20 20 20 20 20 53 69 6d 75 6c 61 74 );. Simulat
de9d0 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 30 eIOErrorBenign(0
de9e0 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 );. if( rc!
de9f0 3d 34 20 7c 7c 20 6d 65 6d 63 6d 70 28 6f 6c 64 =4 || memcmp(old
dea00 43 6e 74 72 2c 20 26 28 28 63 68 61 72 2a 29 70 Cntr, &((char*)p
dea10 42 75 66 29 5b 32 34 2d 6f 66 66 73 65 74 5d 2c Buf)[24-offset],
dea20 20 34 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 4)!=0 ){.
dea30 20 20 70 46 69 6c 65 2d 3e 74 72 61 6e 73 43 6e pFile->transCn
dea40 74 72 43 68 6e 67 20 3d 20 31 3b 20 20 2f 2a 20 trChng = 1; /*
dea50 54 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 The transaction
dea60 63 6f 75 6e 74 65 72 20 68 61 73 20 63 68 61 6e counter has chan
dea70 67 65 64 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 ged */. }.
dea80 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a }. }.#endif.
dea90 0a 20 20 77 68 69 6c 65 28 20 61 6d 74 3e 30 20 . while( amt>0
deaa0 26 26 20 28 77 72 6f 74 65 20 3d 20 73 65 65 6b && (wrote = seek
deab0 41 6e 64 57 72 69 74 65 28 70 46 69 6c 65 2c 20 AndWrite(pFile,
deac0 6f 66 66 73 65 74 2c 20 70 42 75 66 2c 20 61 6d offset, pBuf, am
dead0 74 29 29 3e 30 20 29 7b 0a 20 20 20 20 61 6d 74 t))>0 ){. amt
deae0 20 2d 3d 20 77 72 6f 74 65 3b 0a 20 20 20 20 6f -= wrote;. o
deaf0 66 66 73 65 74 20 2b 3d 20 77 72 6f 74 65 3b 0a ffset += wrote;.
deb00 20 20 20 20 70 42 75 66 20 3d 20 26 28 28 63 68 pBuf = &((ch
deb10 61 72 2a 29 70 42 75 66 29 5b 77 72 6f 74 65 5d ar*)pBuf)[wrote]
deb20 3b 0a 20 20 7d 0a 20 20 53 69 6d 75 6c 61 74 65 ;. }. Simulate
deb30 49 4f 45 72 72 6f 72 28 28 20 77 72 6f 74 65 3d IOError(( wrote=
deb40 28 2d 31 29 2c 20 61 6d 74 3d 31 20 29 29 3b 0a (-1), amt=1 ));.
deb50 20 20 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 SimulateDiskfu
deb60 6c 6c 45 72 72 6f 72 28 28 20 77 72 6f 74 65 3d llError(( wrote=
deb70 30 2c 20 61 6d 74 3d 31 20 29 29 3b 0a 20 20 69 0, amt=1 ));. i
deb80 66 28 20 61 6d 74 3e 30 20 29 7b 0a 20 20 20 20 f( amt>0 ){.
deb90 69 66 28 20 77 72 6f 74 65 3c 30 20 29 7b 0a 20 if( wrote<0 ){.
deba0 20 20 20 20 20 2f 2a 20 6c 61 73 74 45 72 72 6e /* lastErrn
debb0 6f 20 73 65 74 20 62 79 20 73 65 65 6b 41 6e 64 o set by seekAnd
debc0 57 72 69 74 65 20 2a 2f 0a 20 20 20 20 20 20 72 Write */. r
debd0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 eturn SQLITE_IOE
debe0 52 52 5f 57 52 49 54 45 3b 0a 20 20 20 20 7d 65 RR_WRITE;. }e
debf0 6c 73 65 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 lse{. pFile
dec00 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 30 3b ->lastErrno = 0;
dec10 20 2f 2a 20 6e 6f 74 20 61 20 73 79 73 74 65 6d /* not a system
dec20 20 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20 20 20 error */.
dec30 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 46 55 return SQLITE_FU
dec40 4c 4c 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 LL;. }. }.
dec50 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
dec60 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 ;.}..#ifdef SQLI
dec70 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 43 6f TE_TEST./*.** Co
dec80 75 6e 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f unt the number o
dec90 66 20 66 75 6c 6c 73 79 6e 63 73 20 61 6e 64 20 f fullsyncs and
deca0 6e 6f 72 6d 61 6c 20 73 79 6e 63 73 2e 20 20 54 normal syncs. T
decb0 68 69 73 20 69 73 20 75 73 65 64 20 74 6f 20 74 his is used to t
decc0 65 73 74 0a 2a 2a 20 74 68 61 74 20 73 79 6e 63 est.** that sync
decd0 73 20 61 6e 64 20 66 75 6c 6c 73 79 6e 63 73 20 s and fullsyncs
dece0 61 72 65 20 6f 63 63 75 72 72 69 6e 67 20 61 74 are occurring at
decf0 20 74 68 65 20 72 69 67 68 74 20 74 69 6d 65 73 the right times
ded00 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
ded10 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 79 6e 63 int sqlite3_sync
ded20 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 53 51 4c 49 _count = 0;.SQLI
ded30 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
ded40 65 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e e3_fullsync_coun
ded50 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f t = 0;.#endif../
ded60 2a 0a 2a 2a 20 57 65 20 64 6f 20 6e 6f 74 20 74 *.** We do not t
ded70 72 75 73 74 20 73 79 73 74 65 6d 73 20 74 6f 20 rust systems to
ded80 70 72 6f 76 69 64 65 20 61 20 77 6f 72 6b 69 6e provide a workin
ded90 67 20 66 64 61 74 61 73 79 6e 63 28 29 2e 20 20 g fdatasync().
deda0 53 6f 6d 65 20 64 6f 2e 0a 2a 2a 20 4f 74 68 65 Some do..** Othe
dedb0 72 73 20 64 6f 20 6e 6f 2e 20 20 54 6f 20 62 65 rs do no. To be
dedc0 20 73 61 66 65 2c 20 77 65 20 77 69 6c 6c 20 73 safe, we will s
dedd0 74 69 63 6b 20 77 69 74 68 20 74 68 65 20 28 73 tick with the (s
dede0 6c 6f 77 65 72 29 20 66 73 79 6e 63 28 29 2e 0a lower) fsync()..
dedf0 2a 2a 20 49 66 20 79 6f 75 20 6b 6e 6f 77 20 74 ** If you know t
dee00 68 61 74 20 79 6f 75 72 20 73 79 73 74 65 6d 20 hat your system
dee10 64 6f 65 73 20 73 75 70 70 6f 72 74 20 66 64 61 does support fda
dee20 74 61 73 79 6e 63 28 29 20 63 6f 72 72 65 63 74 tasync() correct
dee30 6c 79 2c 0a 2a 2a 20 74 68 65 6e 20 73 69 6d 70 ly,.** then simp
dee40 6c 79 20 63 6f 6d 70 69 6c 65 20 77 69 74 68 20 ly compile with
dee50 2d 44 66 64 61 74 61 73 79 6e 63 3d 66 64 61 74 -Dfdatasync=fdat
dee60 61 73 79 6e 63 0a 2a 2f 0a 23 69 66 20 21 64 65 async.*/.#if !de
dee70 66 69 6e 65 64 28 66 64 61 74 61 73 79 6e 63 29 fined(fdatasync)
dee80 20 26 26 20 21 64 65 66 69 6e 65 64 28 5f 5f 6c && !defined(__l
dee90 69 6e 75 78 5f 5f 29 0a 23 20 64 65 66 69 6e 65 inux__).# define
deea0 20 66 64 61 74 61 73 79 6e 63 20 66 73 79 6e 63 fdatasync fsync
deeb0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 .#endif../*.** D
deec0 65 66 69 6e 65 20 48 41 56 45 5f 46 55 4c 4c 46 efine HAVE_FULLF
deed0 53 59 4e 43 20 74 6f 20 30 20 6f 72 20 31 20 64 SYNC to 0 or 1 d
deee0 65 70 65 6e 64 69 6e 67 20 6f 6e 20 77 68 65 74 epending on whet
deef0 68 65 72 20 6f 72 20 6e 6f 74 0a 2a 2a 20 74 68 her or not.** th
def00 65 20 46 5f 46 55 4c 4c 46 53 59 4e 43 20 6d 61 e F_FULLFSYNC ma
def10 63 72 6f 20 69 73 20 64 65 66 69 6e 65 64 2e 20 cro is defined.
def20 20 46 5f 46 55 4c 4c 46 53 59 4e 43 20 69 73 20 F_FULLFSYNC is
def30 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 6f 6e 6c currently.** onl
def40 79 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 4d y available on M
def50 61 63 20 4f 53 20 58 2e 20 20 42 75 74 20 74 68 ac OS X. But th
def60 61 74 20 63 6f 75 6c 64 20 63 68 61 6e 67 65 2e at could change.
def70 0a 2a 2f 0a 23 69 66 64 65 66 20 46 5f 46 55 4c .*/.#ifdef F_FUL
def80 4c 46 53 59 4e 43 0a 23 20 64 65 66 69 6e 65 20 LFSYNC.# define
def90 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43 20 31 HAVE_FULLFSYNC 1
defa0 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 .#else.# define
defb0 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43 20 30 HAVE_FULLFSYNC 0
defc0 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 .#endif.../*.**
defd0 54 68 65 20 66 73 79 6e 63 28 29 20 73 79 73 74 The fsync() syst
defe0 65 6d 20 63 61 6c 6c 20 64 6f 65 73 20 6e 6f 74 em call does not
deff0 20 77 6f 72 6b 20 61 73 20 61 64 76 65 72 74 69 work as adverti
df000 73 65 64 20 6f 6e 20 6d 61 6e 79 0a 2a 2a 20 75 sed on many.** u
df010 6e 69 78 20 73 79 73 74 65 6d 73 2e 20 20 54 68 nix systems. Th
df020 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 70 72 6f 63 e following proc
df030 65 64 75 72 65 20 69 73 20 61 6e 20 61 74 74 65 edure is an atte
df040 6d 70 74 20 74 6f 20 6d 61 6b 65 0a 2a 2a 20 69 mpt to make.** i
df050 74 20 77 6f 72 6b 20 62 65 74 74 65 72 2e 0a 2a t work better..*
df060 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f *.** The SQLITE_
df070 4e 4f 5f 53 59 4e 43 20 6d 61 63 72 6f 20 64 69 NO_SYNC macro di
df080 73 61 62 6c 65 73 20 61 6c 6c 20 66 73 79 6e 63 sables all fsync
df090 28 29 73 2e 20 20 54 68 69 73 20 69 73 20 75 73 ()s. This is us
df0a0 65 66 75 6c 0a 2a 2a 20 66 6f 72 20 74 65 73 74 eful.** for test
df0b0 69 6e 67 20 77 68 65 6e 20 77 65 20 77 61 6e 74 ing when we want
df0c0 20 74 6f 20 72 75 6e 20 74 68 72 6f 75 67 68 20 to run through
df0d0 74 68 65 20 74 65 73 74 20 73 75 69 74 65 20 71 the test suite q
df0e0 75 69 63 6b 6c 79 2e 0a 2a 2a 20 59 6f 75 20 61 uickly..** You a
df0f0 72 65 20 73 74 72 6f 6e 67 6c 79 20 61 64 76 69 re strongly advi
df100 73 65 64 20 2a 6e 6f 74 2a 20 74 6f 20 64 65 70 sed *not* to dep
df110 6c 6f 79 20 77 69 74 68 20 53 51 4c 49 54 45 5f loy with SQLITE_
df120 4e 4f 5f 53 59 4e 43 0a 2a 2a 20 65 6e 61 62 6c NO_SYNC.** enabl
df130 65 64 2c 20 68 6f 77 65 76 65 72 2c 20 73 69 6e ed, however, sin
df140 63 65 20 77 69 74 68 20 53 51 4c 49 54 45 5f 4e ce with SQLITE_N
df150 4f 5f 53 59 4e 43 20 65 6e 61 62 6c 65 64 2c 20 O_SYNC enabled,
df160 61 6e 20 4f 53 20 63 72 61 73 68 0a 2a 2a 20 6f an OS crash.** o
df170 72 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 20 r power failure
df180 77 69 6c 6c 20 6c 69 6b 65 6c 79 20 63 6f 72 72 will likely corr
df190 75 70 74 20 74 68 65 20 64 61 74 61 62 61 73 65 upt the database
df1a0 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c file..**.** SQL
df1b0 69 74 65 20 73 65 74 73 20 74 68 65 20 64 61 74 ite sets the dat
df1c0 61 4f 6e 6c 79 20 66 6c 61 67 20 69 66 20 74 68 aOnly flag if th
df1d0 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 66 69 e size of the fi
df1e0 6c 65 20 69 73 20 75 6e 63 68 61 6e 67 65 64 2e le is unchanged.
df1f0 0a 2a 2a 20 54 68 65 20 69 64 65 61 20 62 65 68 .** The idea beh
df200 69 6e 64 20 64 61 74 61 4f 6e 6c 79 20 69 73 20 ind dataOnly is
df210 74 68 61 74 20 69 74 20 73 68 6f 75 6c 64 20 6f that it should o
df220 6e 6c 79 20 77 72 69 74 65 20 74 68 65 20 66 69 nly write the fi
df230 6c 65 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20 74 6f le content.** to
df240 20 64 69 73 6b 2c 20 6e 6f 74 20 74 68 65 20 69 disk, not the i
df250 6e 6f 64 65 2e 20 20 57 65 20 6f 6e 6c 79 20 73 node. We only s
df260 65 74 20 64 61 74 61 4f 6e 6c 79 20 69 66 20 74 et dataOnly if t
df270 68 65 20 66 69 6c 65 20 73 69 7a 65 20 69 73 20 he file size is
df280 0a 2a 2a 20 75 6e 63 68 61 6e 67 65 64 20 73 69 .** unchanged si
df290 6e 63 65 20 74 68 65 20 66 69 6c 65 20 73 69 7a nce the file siz
df2a0 65 20 69 73 20 70 61 72 74 20 6f 66 20 74 68 65 e is part of the
df2b0 20 69 6e 6f 64 65 2e 20 20 48 6f 77 65 76 65 72 inode. However
df2c0 2c 20 0a 2a 2a 20 54 65 64 20 54 73 27 6f 20 74 , .** Ted Ts'o t
df2d0 65 6c 6c 73 20 75 73 20 74 68 61 74 20 66 64 61 ells us that fda
df2e0 74 61 73 79 6e 63 28 29 20 77 69 6c 6c 20 61 6c tasync() will al
df2f0 73 6f 20 77 72 69 74 65 20 74 68 65 20 69 6e 6f so write the ino
df300 64 65 20 69 66 20 74 68 65 0a 2a 2a 20 66 69 6c de if the.** fil
df310 65 20 73 69 7a 65 20 68 61 73 20 63 68 61 6e 67 e size has chang
df320 65 64 2e 20 20 54 68 65 20 6f 6e 6c 79 20 72 65 ed. The only re
df330 61 6c 20 64 69 66 66 65 72 65 6e 63 65 20 62 65 al difference be
df340 74 77 65 65 6e 20 66 64 61 74 61 73 79 6e 63 28 tween fdatasync(
df350 29 0a 2a 2a 20 61 6e 64 20 66 73 79 6e 63 28 29 ).** and fsync()
df360 2c 20 54 65 64 20 74 65 6c 6c 73 20 75 73 2c 20 , Ted tells us,
df370 69 73 20 74 68 61 74 20 66 64 61 74 61 73 79 6e is that fdatasyn
df380 63 28 29 20 77 69 6c 6c 20 6e 6f 74 20 66 6c 75 c() will not flu
df390 73 68 20 74 68 65 0a 2a 2a 20 69 6e 6f 64 65 20 sh the.** inode
df3a0 69 66 20 74 68 65 20 6d 74 69 6d 65 20 6f 72 20 if the mtime or
df3b0 6f 77 6e 65 72 20 6f 72 20 6f 74 68 65 72 20 69 owner or other i
df3c0 6e 6f 64 65 20 61 74 74 72 69 62 75 74 65 73 20 node attributes
df3d0 68 61 76 65 20 63 68 61 6e 67 65 64 2e 0a 2a 2a have changed..**
df3e0 20 57 65 20 6f 6e 6c 79 20 63 61 72 65 20 61 62 We only care ab
df3f0 6f 75 74 20 74 68 65 20 66 69 6c 65 20 73 69 7a out the file siz
df400 65 2c 20 6e 6f 74 20 74 68 65 20 6f 74 68 65 72 e, not the other
df410 20 66 69 6c 65 20 61 74 74 72 69 62 75 74 65 73 file attributes
df420 2c 20 73 6f 0a 2a 2a 20 61 73 20 66 61 72 20 61 , so.** as far a
df430 73 20 53 51 4c 69 74 65 20 69 73 20 63 6f 6e 63 s SQLite is conc
df440 65 72 6e 65 64 2c 20 61 6e 20 66 64 61 74 61 73 erned, an fdatas
df450 79 6e 63 28 29 20 69 73 20 61 6c 77 61 79 73 20 ync() is always
df460 61 64 65 71 75 61 74 65 2e 0a 2a 2a 20 53 6f 2c adequate..** So,
df470 20 77 65 20 61 6c 77 61 79 73 20 75 73 65 20 66 we always use f
df480 64 61 74 61 73 79 6e 63 28 29 20 69 66 20 69 74 datasync() if it
df490 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2c 20 72 is available, r
df4a0 65 67 61 72 64 6c 65 73 73 20 6f 66 0a 2a 2a 20 egardless of.**
df4b0 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 the value of the
df4c0 20 64 61 74 61 4f 6e 6c 79 20 66 6c 61 67 2e 0a dataOnly flag..
df4d0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 75 */.static int fu
df4e0 6c 6c 5f 66 73 79 6e 63 28 69 6e 74 20 66 64 2c ll_fsync(int fd,
df4f0 20 69 6e 74 20 66 75 6c 6c 53 79 6e 63 2c 20 69 int fullSync, i
df500 6e 74 20 64 61 74 61 4f 6e 6c 79 29 7b 0a 20 20 nt dataOnly){.
df510 69 6e 74 20 72 63 3b 0a 0a 20 20 2f 2a 20 54 68 int rc;.. /* Th
df520 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 22 69 66 64 e following "ifd
df530 65 66 2f 65 6c 69 66 2f 65 6c 73 65 2f 22 20 62 ef/elif/else/" b
df540 6c 6f 63 6b 20 68 61 73 20 74 68 65 20 73 61 6d lock has the sam
df550 65 20 73 74 72 75 63 74 75 72 65 20 61 73 0a 20 e structure as.
df560 20 2a 2a 20 74 68 65 20 6f 6e 65 20 62 65 6c 6f ** the one belo
df570 77 2e 20 49 74 20 69 73 20 72 65 70 6c 69 63 61 w. It is replica
df580 74 65 64 20 68 65 72 65 20 73 6f 6c 65 6c 79 20 ted here solely
df590 74 6f 20 61 76 6f 69 64 20 63 6c 75 74 74 65 72 to avoid clutter
df5a0 69 6e 67 20 0a 20 20 2a 2a 20 75 70 20 74 68 65 ing . ** up the
df5b0 20 72 65 61 6c 20 63 6f 64 65 20 77 69 74 68 20 real code with
df5c0 74 68 65 20 55 4e 55 53 45 44 5f 50 41 52 41 4d the UNUSED_PARAM
df5d0 45 54 45 52 28 29 20 6d 61 63 72 6f 73 2e 0a 20 ETER() macros..
df5e0 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 */.#ifdef SQLIT
df5f0 45 5f 4e 4f 5f 53 59 4e 43 0a 20 20 55 4e 55 53 E_NO_SYNC. UNUS
df600 45 44 5f 50 41 52 41 4d 45 54 45 52 28 66 64 29 ED_PARAMETER(fd)
df610 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d ;. UNUSED_PARAM
df620 45 54 45 52 28 66 75 6c 6c 53 79 6e 63 29 3b 0a ETER(fullSync);.
df630 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
df640 45 52 28 64 61 74 61 4f 6e 6c 79 29 3b 0a 23 65 ER(dataOnly);.#e
df650 6c 69 66 20 48 41 56 45 5f 46 55 4c 4c 46 53 59 lif HAVE_FULLFSY
df660 4e 43 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 NC. UNUSED_PARA
df670 4d 45 54 45 52 28 64 61 74 61 4f 6e 6c 79 29 3b METER(dataOnly);
df680 0a 23 65 6c 73 65 0a 20 20 55 4e 55 53 45 44 5f .#else. UNUSED_
df690 50 41 52 41 4d 45 54 45 52 28 66 75 6c 6c 53 79 PARAMETER(fullSy
df6a0 6e 63 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 nc);. UNUSED_PA
df6b0 52 41 4d 45 54 45 52 28 64 61 74 61 4f 6e 6c 79 RAMETER(dataOnly
df6c0 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 );.#endif.. /*
df6d0 52 65 63 6f 72 64 20 74 68 65 20 6e 75 6d 62 65 Record the numbe
df6e0 72 20 6f 66 20 74 69 6d 65 73 20 74 68 61 74 20 r of times that
df6f0 77 65 20 64 6f 20 61 20 6e 6f 72 6d 61 6c 20 66 we do a normal f
df700 73 79 6e 63 28 29 20 61 6e 64 20 0a 20 20 2a 2a sync() and . **
df710 20 46 55 4c 4c 53 59 4e 43 2e 20 20 54 68 69 73 FULLSYNC. This
df720 20 69 73 20 75 73 65 64 20 64 75 72 69 6e 67 20 is used during
df730 74 65 73 74 69 6e 67 20 74 6f 20 76 65 72 69 66 testing to verif
df740 79 20 74 68 61 74 20 74 68 69 73 20 70 72 6f 63 y that this proc
df750 65 64 75 72 65 0a 20 20 2a 2a 20 67 65 74 73 20 edure. ** gets
df760 63 61 6c 6c 65 64 20 77 69 74 68 20 74 68 65 20 called with the
df770 63 6f 72 72 65 63 74 20 61 72 67 75 6d 65 6e 74 correct argument
df780 73 2e 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 s.. */.#ifdef S
df790 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 69 66 28 QLITE_TEST. if(
df7a0 20 66 75 6c 6c 53 79 6e 63 20 29 20 73 71 6c 69 fullSync ) sqli
df7b0 74 65 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 te3_fullsync_cou
df7c0 6e 74 2b 2b 3b 0a 20 20 73 71 6c 69 74 65 33 5f nt++;. sqlite3_
df7d0 73 79 6e 63 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65 sync_count++;.#e
df7e0 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 66 20 77 65 ndif.. /* If we
df7f0 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74 compiled with t
df800 68 65 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e he SQLITE_NO_SYN
df810 43 20 66 6c 61 67 2c 20 74 68 65 6e 20 73 79 6e C flag, then syn
df820 63 69 6e 67 20 69 73 20 61 0a 20 20 2a 2a 20 6e cing is a. ** n
df830 6f 2d 6f 70 0a 20 20 2a 2f 0a 23 69 66 64 65 66 o-op. */.#ifdef
df840 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 0a SQLITE_NO_SYNC.
df850 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b rc = SQLITE_OK
df860 3b 0a 23 65 6c 69 66 20 48 41 56 45 5f 46 55 4c ;.#elif HAVE_FUL
df870 4c 46 53 59 4e 43 0a 20 20 69 66 28 20 66 75 6c LFSYNC. if( ful
df880 6c 53 79 6e 63 20 29 7b 0a 20 20 20 20 72 63 20 lSync ){. rc
df890 3d 20 66 63 6e 74 6c 28 66 64 2c 20 46 5f 46 55 = fcntl(fd, F_FU
df8a0 4c 4c 46 53 59 4e 43 2c 20 30 29 3b 0a 20 20 7d LLFSYNC, 0);. }
df8b0 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 31 else{. rc = 1
df8c0 3b 0a 20 20 7d 0a 20 20 2f 2a 20 49 66 20 74 68 ;. }. /* If th
df8d0 65 20 46 55 4c 4c 46 53 59 4e 43 20 66 61 69 6c e FULLFSYNC fail
df8e0 65 64 2c 20 66 61 6c 6c 20 62 61 63 6b 20 74 6f ed, fall back to
df8f0 20 61 74 74 65 6d 70 74 69 6e 67 20 61 6e 20 66 attempting an f
df900 73 79 6e 63 28 29 2e 0a 20 20 2a 2a 20 49 74 20 sync().. ** It
df910 73 68 6f 75 6c 64 6e 27 74 20 62 65 20 70 6f 73 shouldn't be pos
df920 73 69 62 6c 65 20 66 6f 72 20 66 75 6c 6c 66 73 sible for fullfs
df930 79 6e 63 20 74 6f 20 66 61 69 6c 20 6f 6e 20 74 ync to fail on t
df940 68 65 20 6c 6f 63 61 6c 20 0a 20 20 2a 2a 20 66 he local . ** f
df950 69 6c 65 20 73 79 73 74 65 6d 20 28 6f 6e 20 4f ile system (on O
df960 53 58 29 2c 20 73 6f 20 66 61 69 6c 75 72 65 20 SX), so failure
df970 69 6e 64 69 63 61 74 65 73 20 74 68 61 74 20 46 indicates that F
df980 55 4c 4c 46 53 59 4e 43 0a 20 20 2a 2a 20 69 73 ULLFSYNC. ** is
df990 6e 27 74 20 73 75 70 70 6f 72 74 65 64 20 66 6f n't supported fo
df9a0 72 20 74 68 69 73 20 66 69 6c 65 20 73 79 73 74 r this file syst
df9b0 65 6d 2e 20 53 6f 2c 20 61 74 74 65 6d 70 74 20 em. So, attempt
df9c0 61 6e 20 66 73 79 6e 63 20 0a 20 20 2a 2a 20 61 an fsync . ** a
df9d0 6e 64 20 28 66 6f 72 20 6e 6f 77 29 20 69 67 6e nd (for now) ign
df9e0 6f 72 65 20 74 68 65 20 6f 76 65 72 68 65 61 64 ore the overhead
df9f0 20 6f 66 20 61 20 73 75 70 65 72 66 6c 75 6f 75 of a superfluou
dfa00 73 20 66 63 6e 74 6c 20 63 61 6c 6c 2e 20 20 0a s fcntl call. .
dfa10 20 20 2a 2a 20 49 74 27 64 20 62 65 20 62 65 74 ** It'd be bet
dfa20 74 65 72 20 74 6f 20 64 65 74 65 63 74 20 66 75 ter to detect fu
dfa30 6c 6c 66 73 79 6e 63 20 73 75 70 70 6f 72 74 20 llfsync support
dfa40 6f 6e 63 65 20 61 6e 64 20 61 76 6f 69 64 20 0a once and avoid .
dfa50 20 20 2a 2a 20 74 68 65 20 66 63 6e 74 6c 20 63 ** the fcntl c
dfa60 61 6c 6c 20 65 76 65 72 79 20 74 69 6d 65 20 73 all every time s
dfa70 79 6e 63 20 69 73 20 63 61 6c 6c 65 64 2e 0a 20 ync is called..
dfa80 20 2a 2f 0a 20 20 69 66 28 20 72 63 20 29 20 72 */. if( rc ) r
dfa90 63 20 3d 20 66 73 79 6e 63 28 66 64 29 3b 0a 0a c = fsync(fd);..
dfaa0 23 65 6c 73 65 20 0a 20 20 72 63 20 3d 20 66 64 #else . rc = fd
dfab0 61 74 61 73 79 6e 63 28 66 64 29 3b 0a 23 69 66 atasync(fd);.#if
dfac0 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 69 66 OS_VXWORKS. if
dfad0 28 20 72 63 3d 3d 2d 31 20 26 26 20 65 72 72 6e ( rc==-1 && errn
dfae0 6f 3d 3d 45 4e 4f 54 53 55 50 20 29 7b 0a 20 20 o==ENOTSUP ){.
dfaf0 20 20 72 63 20 3d 20 66 73 79 6e 63 28 66 64 29 rc = fsync(fd)
dfb00 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 ;. }.#endif /*
dfb10 4f 53 5f 56 58 57 4f 52 4b 53 20 2a 2f 0a 23 65 OS_VXWORKS */.#e
dfb20 6e 64 69 66 20 2f 2a 20 69 66 64 65 66 20 53 51 ndif /* ifdef SQ
dfb30 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 20 65 6c 69 LITE_NO_SYNC eli
dfb40 66 20 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43 f HAVE_FULLFSYNC
dfb50 20 2a 2f 0a 0a 20 20 69 66 28 20 4f 53 5f 56 58 */.. if( OS_VX
dfb60 57 4f 52 4b 53 20 26 26 20 72 63 21 3d 20 2d 31 WORKS && rc!= -1
dfb70 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 30 3b 0a ){. rc = 0;.
dfb80 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
dfb90 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 .}../*.** Make s
dfba0 75 72 65 20 61 6c 6c 20 77 72 69 74 65 73 20 74 ure all writes t
dfbb0 6f 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 66 o a particular f
dfbc0 69 6c 65 20 61 72 65 20 63 6f 6d 6d 69 74 74 65 ile are committe
dfbd0 64 20 74 6f 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a d to disk..**.**
dfbe0 20 49 66 20 64 61 74 61 4f 6e 6c 79 3d 3d 30 20 If dataOnly==0
dfbf0 74 68 65 6e 20 62 6f 74 68 20 74 68 65 20 66 69 then both the fi
dfc00 6c 65 20 69 74 73 65 6c 66 20 61 6e 64 20 69 74 le itself and it
dfc10 73 20 6d 65 74 61 64 61 74 61 20 28 66 69 6c 65 s metadata (file
dfc20 0a 2a 2a 20 73 69 7a 65 2c 20 61 63 63 65 73 73 .** size, access
dfc30 20 74 69 6d 65 2c 20 65 74 63 29 20 61 72 65 20 time, etc) are
dfc40 73 79 6e 63 65 64 2e 20 20 49 66 20 64 61 74 61 synced. If data
dfc50 4f 6e 6c 79 21 3d 30 20 74 68 65 6e 20 6f 6e 6c Only!=0 then onl
dfc60 79 20 74 68 65 0a 2a 2a 20 66 69 6c 65 20 64 61 y the.** file da
dfc70 74 61 20 69 73 20 73 79 6e 63 65 64 2e 0a 2a 2a ta is synced..**
dfc80 0a 2a 2a 20 55 6e 64 65 72 20 55 6e 69 78 2c 20 .** Under Unix,
dfc90 61 6c 73 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 also make sure t
dfca0 68 61 74 20 74 68 65 20 64 69 72 65 63 74 6f 72 hat the director
dfcb0 79 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65 20 y entry for the
dfcc0 66 69 6c 65 0a 2a 2a 20 68 61 73 20 62 65 65 6e file.** has been
dfcd0 20 63 72 65 61 74 65 64 20 62 79 20 66 73 79 6e created by fsyn
dfce0 63 2d 69 6e 67 20 74 68 65 20 64 69 72 65 63 74 c-ing the direct
dfcf0 6f 72 79 20 74 68 61 74 20 63 6f 6e 74 61 69 6e ory that contain
dfd00 73 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 20 49 s the file..** I
dfd10 66 20 77 65 20 64 6f 20 6e 6f 74 20 64 6f 20 74 f we do not do t
dfd20 68 69 73 20 61 6e 64 20 77 65 20 65 6e 63 6f 75 his and we encou
dfd30 6e 74 65 72 20 61 20 70 6f 77 65 72 20 66 61 69 nter a power fai
dfd40 6c 75 72 65 2c 20 74 68 65 20 64 69 72 65 63 74 lure, the direct
dfd50 6f 72 79 0a 2a 2a 20 65 6e 74 72 79 20 66 6f 72 ory.** entry for
dfd60 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 6d 69 67 the journal mig
dfd70 68 74 20 6e 6f 74 20 65 78 69 73 74 20 61 66 74 ht not exist aft
dfd80 65 72 20 77 65 20 72 65 62 6f 6f 74 2e 20 20 54 er we reboot. T
dfd90 68 65 20 6e 65 78 74 0a 2a 2a 20 53 51 4c 69 74 he next.** SQLit
dfda0 65 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 20 e to access the
dfdb0 66 69 6c 65 20 77 69 6c 6c 20 6e 6f 74 20 6b 6e file will not kn
dfdc0 6f 77 20 74 68 61 74 20 74 68 65 20 6a 6f 75 72 ow that the jour
dfdd0 6e 61 6c 20 65 78 69 73 74 73 20 28 62 65 63 61 nal exists (beca
dfde0 75 73 65 0a 2a 2a 20 74 68 65 20 64 69 72 65 63 use.** the direc
dfdf0 74 6f 72 79 20 65 6e 74 72 79 20 66 6f 72 20 74 tory entry for t
dfe00 68 65 20 6a 6f 75 72 6e 61 6c 20 77 61 73 20 6e he journal was n
dfe10 65 76 65 72 20 63 72 65 61 74 65 64 29 20 61 6e ever created) an
dfe20 64 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f d the transactio
dfe30 6e 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 72 6f n.** will not ro
dfe40 6c 6c 20 62 61 63 6b 20 2d 20 70 6f 73 73 69 62 ll back - possib
dfe50 6c 79 20 6c 65 61 64 69 6e 67 20 74 6f 20 64 61 ly leading to da
dfe60 74 61 62 61 73 65 20 63 6f 72 72 75 70 74 69 6f tabase corruptio
dfe70 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 n..*/.static int
dfe80 20 75 6e 69 78 53 79 6e 63 28 73 71 6c 69 74 65 unixSync(sqlite
dfe90 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 3_file *id, int
dfea0 66 6c 61 67 73 29 7b 0a 20 20 69 6e 74 20 72 63 flags){. int rc
dfeb0 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 ;. unixFile *pF
dfec0 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a ile = (unixFile*
dfed0 29 69 64 3b 0a 0a 20 20 69 6e 74 20 69 73 44 61 )id;.. int isDa
dfee0 74 61 4f 6e 6c 79 20 3d 20 28 66 6c 61 67 73 26 taOnly = (flags&
dfef0 53 51 4c 49 54 45 5f 53 59 4e 43 5f 44 41 54 41 SQLITE_SYNC_DATA
dff00 4f 4e 4c 59 29 3b 0a 20 20 69 6e 74 20 69 73 46 ONLY);. int isF
dff10 75 6c 6c 73 79 6e 63 20 3d 20 28 66 6c 61 67 73 ullsync = (flags
dff20 26 30 78 30 46 29 3d 3d 53 51 4c 49 54 45 5f 53 &0x0F)==SQLITE_S
dff30 59 4e 43 5f 46 55 4c 4c 3b 0a 0a 20 20 2f 2a 20 YNC_FULL;.. /*
dff40 43 68 65 63 6b 20 74 68 61 74 20 6f 6e 65 20 6f Check that one o
dff50 66 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f f SQLITE_SYNC_NO
dff60 52 4d 41 4c 20 6f 72 20 46 55 4c 4c 20 77 61 73 RMAL or FULL was
dff70 20 70 61 73 73 65 64 20 2a 2f 0a 20 20 61 73 73 passed */. ass
dff80 65 72 74 28 28 66 6c 61 67 73 26 30 78 30 46 29 ert((flags&0x0F)
dff90 3d 3d 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f ==SQLITE_SYNC_NO
dffa0 52 4d 41 4c 0a 20 20 20 20 20 20 7c 7c 20 28 66 RMAL. || (f
dffb0 6c 61 67 73 26 30 78 30 46 29 3d 3d 53 51 4c 49 lags&0x0F)==SQLI
dffc0 54 45 5f 53 59 4e 43 5f 46 55 4c 4c 0a 20 20 29 TE_SYNC_FULL. )
dffd0 3b 0a 0a 20 20 2f 2a 20 55 6e 69 78 20 63 61 6e ;.. /* Unix can
dffe0 6e 6f 74 2c 20 62 75 74 20 73 6f 6d 65 20 73 79 not, but some sy
dfff0 73 74 65 6d 73 20 6d 61 79 20 72 65 74 75 72 6e stems may return
e0000 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 66 72 6f SQLITE_FULL fro
e0010 6d 20 68 65 72 65 2e 20 54 68 69 73 0a 20 20 2a m here. This. *
e0020 2a 20 6c 69 6e 65 20 69 73 20 74 6f 20 74 65 73 * line is to tes
e0030 74 20 74 68 61 74 20 64 6f 69 6e 67 20 73 6f 20 t that doing so
e0040 64 6f 65 73 20 6e 6f 74 20 63 61 75 73 65 20 61 does not cause a
e0050 6e 79 20 70 72 6f 62 6c 65 6d 73 2e 0a 20 20 2a ny problems.. *
e0060 2f 0a 20 20 53 69 6d 75 6c 61 74 65 44 69 73 6b /. SimulateDisk
e0070 66 75 6c 6c 45 72 72 6f 72 28 20 72 65 74 75 72 fullError( retur
e0080 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 29 3b n SQLITE_FULL );
e0090 0a 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c .. assert( pFil
e00a0 65 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 32 28 e );. OSTRACE2(
e00b0 22 53 59 4e 43 20 20 20 20 25 2d 33 64 5c 6e 22 "SYNC %-3d\n"
e00c0 2c 20 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 20 72 , pFile->h);. r
e00d0 63 20 3d 20 66 75 6c 6c 5f 66 73 79 6e 63 28 70 c = full_fsync(p
e00e0 46 69 6c 65 2d 3e 68 2c 20 69 73 46 75 6c 6c 73 File->h, isFulls
e00f0 79 6e 63 2c 20 69 73 44 61 74 61 4f 6e 6c 79 29 ync, isDataOnly)
e0100 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 ;. SimulateIOEr
e0110 72 6f 72 28 20 72 63 3d 31 20 29 3b 0a 20 20 69 ror( rc=1 );. i
e0120 66 28 20 72 63 20 29 7b 0a 20 20 20 20 70 46 69 f( rc ){. pFi
e0130 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 le->lastErrno =
e0140 65 72 72 6e 6f 3b 0a 20 20 20 20 72 65 74 75 72 errno;. retur
e0150 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 46 n SQLITE_IOERR_F
e0160 53 59 4e 43 3b 0a 20 20 7d 0a 20 20 69 66 28 20 SYNC;. }. if(
e0170 70 46 69 6c 65 2d 3e 64 69 72 66 64 3e 3d 30 20 pFile->dirfd>=0
e0180 29 7b 0a 20 20 20 20 69 6e 74 20 65 72 72 3b 0a ){. int err;.
e0190 20 20 20 20 4f 53 54 52 41 43 45 34 28 22 44 49 OSTRACE4("DI
e01a0 52 53 59 4e 43 20 25 2d 33 64 20 28 68 61 76 65 RSYNC %-3d (have
e01b0 5f 66 75 6c 6c 66 73 79 6e 63 3d 25 64 20 66 75 _fullfsync=%d fu
e01c0 6c 6c 73 79 6e 63 3d 25 64 29 5c 6e 22 2c 20 70 llsync=%d)\n", p
e01d0 46 69 6c 65 2d 3e 64 69 72 66 64 2c 0a 20 20 20 File->dirfd,.
e01e0 20 20 20 20 20 20 20 20 20 48 41 56 45 5f 46 55 HAVE_FU
e01f0 4c 4c 46 53 59 4e 43 2c 20 69 73 46 75 6c 6c 73 LLFSYNC, isFulls
e0200 79 6e 63 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 ync);.#ifndef SQ
e0210 4c 49 54 45 5f 44 49 53 41 42 4c 45 5f 44 49 52 LITE_DISABLE_DIR
e0220 53 59 4e 43 0a 20 20 20 20 2f 2a 20 54 68 65 20 SYNC. /* The
e0230 64 69 72 65 63 74 6f 72 79 20 73 79 6e 63 20 69 directory sync i
e0240 73 20 6f 6e 6c 79 20 61 74 74 65 6d 70 74 65 64 s only attempted
e0250 20 69 66 20 66 75 6c 6c 5f 66 73 79 6e 63 20 69 if full_fsync i
e0260 73 0a 20 20 20 20 2a 2a 20 74 75 72 6e 65 64 20 s. ** turned
e0270 6f 66 66 20 6f 72 20 75 6e 61 76 61 69 6c 61 62 off or unavailab
e0280 6c 65 2e 20 20 49 66 20 61 20 66 75 6c 6c 5f 66 le. If a full_f
e0290 73 79 6e 63 20 6f 63 63 75 72 72 65 64 20 61 62 sync occurred ab
e02a0 6f 76 65 2c 0a 20 20 20 20 2a 2a 20 74 68 65 6e ove,. ** then
e02b0 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 73 the directory s
e02c0 79 6e 63 20 69 73 20 73 75 70 65 72 66 6c 75 6f ync is superfluo
e02d0 75 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 us.. */. i
e02e0 66 28 20 28 21 48 41 56 45 5f 46 55 4c 4c 46 53 f( (!HAVE_FULLFS
e02f0 59 4e 43 20 7c 7c 20 21 69 73 46 75 6c 6c 73 79 YNC || !isFullsy
e0300 6e 63 29 20 26 26 20 66 75 6c 6c 5f 66 73 79 6e nc) && full_fsyn
e0310 63 28 70 46 69 6c 65 2d 3e 64 69 72 66 64 2c 30 c(pFile->dirfd,0
e0320 2c 30 29 20 29 7b 0a 20 20 20 20 20 20 20 2f 2a ,0) ){. /*
e0330 0a 20 20 20 20 20 20 20 2a 2a 20 57 65 20 68 61 . ** We ha
e0340 76 65 20 72 65 63 65 69 76 65 64 20 6d 75 6c 74 ve received mult
e0350 69 70 6c 65 20 72 65 70 6f 72 74 73 20 6f 66 20 iple reports of
e0360 66 73 79 6e 63 28 29 20 72 65 74 75 72 6e 69 6e fsync() returnin
e0370 67 0a 20 20 20 20 20 20 20 2a 2a 20 65 72 72 6f g. ** erro
e0380 72 73 20 77 68 65 6e 20 61 70 70 6c 69 65 64 20 rs when applied
e0390 74 6f 20 64 69 72 65 63 74 6f 72 69 65 73 20 6f to directories o
e03a0 6e 20 63 65 72 74 61 69 6e 20 66 69 6c 65 20 73 n certain file s
e03b0 79 73 74 65 6d 73 2e 0a 20 20 20 20 20 20 20 2a ystems.. *
e03c0 2a 20 41 20 66 61 69 6c 65 64 20 64 69 72 65 63 * A failed direc
e03d0 74 6f 72 79 20 73 79 6e 63 20 69 73 20 6e 6f 74 tory sync is not
e03e0 20 61 20 62 69 67 20 64 65 61 6c 2e 20 20 53 6f a big deal. So
e03f0 20 69 74 20 73 65 65 6d 73 0a 20 20 20 20 20 20 it seems.
e0400 20 2a 2a 20 62 65 74 74 65 72 20 74 6f 20 69 67 ** better to ig
e0410 6e 6f 72 65 20 74 68 65 20 65 72 72 6f 72 2e 20 nore the error.
e0420 20 54 69 63 6b 65 74 20 23 31 36 35 37 0a 20 20 Ticket #1657.
e0430 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 2f */. /
e0440 2a 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 * pFile->lastErr
e0450 6e 6f 20 3d 20 65 72 72 6e 6f 3b 20 2a 2f 0a 20 no = errno; */.
e0460 20 20 20 20 20 20 2f 2a 20 72 65 74 75 72 6e 20 /* return
e0470 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 20 2a 2f SQLITE_IOERR; */
e0480 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 . }.#endif.
e0490 20 20 65 72 72 20 3d 20 63 6c 6f 73 65 28 70 46 err = close(pF
e04a0 69 6c 65 2d 3e 64 69 72 66 64 29 3b 20 2f 2a 20 ile->dirfd); /*
e04b0 4f 6e 6c 79 20 6e 65 65 64 20 74 6f 20 73 79 6e Only need to syn
e04c0 63 20 6f 6e 63 65 2c 20 73 6f 20 63 6c 6f 73 65 c once, so close
e04d0 20 74 68 65 20 2a 2f 0a 20 20 20 20 69 66 28 20 the */. if(
e04e0 65 72 72 3d 3d 30 20 29 7b 20 20 20 20 20 20 20 err==0 ){
e04f0 20 20 20 20 20 20 20 2f 2a 20 64 69 72 65 63 74 /* direct
e0500 6f 72 79 20 77 68 65 6e 20 77 65 20 61 72 65 20 ory when we are
e0510 64 6f 6e 65 20 2a 2f 0a 20 20 20 20 20 20 70 46 done */. pF
e0520 69 6c 65 2d 3e 64 69 72 66 64 20 3d 20 2d 31 3b ile->dirfd = -1;
e0530 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
e0540 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 pFile->lastErr
e0550 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 no = errno;.
e0560 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f rc = SQLITE_IO
e0570 45 52 52 5f 44 49 52 5f 43 4c 4f 53 45 3b 0a 20 ERR_DIR_CLOSE;.
e0580 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
e0590 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 n rc;.}../*.** T
e05a0 72 75 6e 63 61 74 65 20 61 6e 20 6f 70 65 6e 20 runcate an open
e05b0 66 69 6c 65 20 74 6f 20 61 20 73 70 65 63 69 66 file to a specif
e05c0 69 65 64 20 73 69 7a 65 0a 2a 2f 0a 73 74 61 74 ied size.*/.stat
e05d0 69 63 20 69 6e 74 20 75 6e 69 78 54 72 75 6e 63 ic int unixTrunc
e05e0 61 74 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ate(sqlite3_file
e05f0 20 2a 69 64 2c 20 69 36 34 20 6e 42 79 74 65 29 *id, i64 nByte)
e0600 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 {. int rc;. as
e0610 73 65 72 74 28 20 69 64 20 29 3b 0a 20 20 53 69 sert( id );. Si
e0620 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72 mulateIOError( r
e0630 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 eturn SQLITE_IOE
e0640 52 52 5f 54 52 55 4e 43 41 54 45 20 29 3b 0a 20 RR_TRUNCATE );.
e0650 20 72 63 20 3d 20 66 74 72 75 6e 63 61 74 65 28 rc = ftruncate(
e0660 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d ((unixFile*)id)-
e0670 3e 68 2c 20 28 6f 66 66 5f 74 29 6e 42 79 74 65 >h, (off_t)nByte
e0680 29 3b 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 );. if( rc ){.
e0690 20 20 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 ((unixFile*)i
e06a0 64 29 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 d)->lastErrno =
e06b0 65 72 72 6e 6f 3b 0a 20 20 20 20 72 65 74 75 72 errno;. retur
e06c0 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 54 n SQLITE_IOERR_T
e06d0 52 55 4e 43 41 54 45 3b 0a 20 20 7d 65 6c 73 65 RUNCATE;. }else
e06e0 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
e06f0 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 7d 0a 0a 2f ITE_OK;. }.}../
e0700 2a 0a 2a 2a 20 44 65 74 65 72 6d 69 6e 65 20 74 *.** Determine t
e0710 68 65 20 63 75 72 72 65 6e 74 20 73 69 7a 65 20 he current size
e0720 6f 66 20 61 20 66 69 6c 65 20 69 6e 20 62 79 74 of a file in byt
e0730 65 73 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 es.*/.static int
e0740 20 75 6e 69 78 46 69 6c 65 53 69 7a 65 28 73 71 unixFileSize(sq
e0750 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 lite3_file *id,
e0760 69 36 34 20 2a 70 53 69 7a 65 29 7b 0a 20 20 69 i64 *pSize){. i
e0770 6e 74 20 72 63 3b 0a 20 20 73 74 72 75 63 74 20 nt rc;. struct
e0780 73 74 61 74 20 62 75 66 3b 0a 20 20 61 73 73 65 stat buf;. asse
e0790 72 74 28 20 69 64 20 29 3b 0a 20 20 72 63 20 3d rt( id );. rc =
e07a0 20 66 73 74 61 74 28 28 28 75 6e 69 78 46 69 6c fstat(((unixFil
e07b0 65 2a 29 69 64 29 2d 3e 68 2c 20 26 62 75 66 29 e*)id)->h, &buf)
e07c0 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 ;. SimulateIOEr
e07d0 72 6f 72 28 20 72 63 3d 31 20 29 3b 0a 20 20 69 ror( rc=1 );. i
e07e0 66 28 20 72 63 21 3d 30 20 29 7b 0a 20 20 20 20 f( rc!=0 ){.
e07f0 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d ((unixFile*)id)-
e0800 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 >lastErrno = err
e0810 6e 6f 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 no;. return S
e0820 51 4c 49 54 45 5f 49 4f 45 52 52 5f 46 53 54 41 QLITE_IOERR_FSTA
e0830 54 3b 0a 20 20 7d 0a 20 20 2a 70 53 69 7a 65 20 T;. }. *pSize
e0840 3d 20 62 75 66 2e 73 74 5f 73 69 7a 65 3b 0a 0a = buf.st_size;..
e0850 20 20 2f 2a 20 57 68 65 6e 20 6f 70 65 6e 69 6e /* When openin
e0860 67 20 61 20 7a 65 72 6f 2d 73 69 7a 65 20 64 61 g a zero-size da
e0870 74 61 62 61 73 65 2c 20 74 68 65 20 66 69 6e 64 tabase, the find
e0880 4c 6f 63 6b 49 6e 66 6f 28 29 20 70 72 6f 63 65 LockInfo() proce
e0890 64 75 72 65 0a 20 20 2a 2a 20 77 72 69 74 65 73 dure. ** writes
e08a0 20 61 20 73 69 6e 67 6c 65 20 62 79 74 65 20 69 a single byte i
e08b0 6e 74 6f 20 74 68 61 74 20 66 69 6c 65 20 69 6e nto that file in
e08c0 20 6f 72 64 65 72 20 74 6f 20 77 6f 72 6b 20 61 order to work a
e08d0 72 6f 75 6e 64 20 61 20 62 75 67 0a 20 20 2a 2a round a bug. **
e08e0 20 69 6e 20 74 68 65 20 4f 53 2d 58 20 6d 73 64 in the OS-X msd
e08f0 6f 73 20 66 69 6c 65 73 79 73 74 65 6d 2e 20 20 os filesystem.
e0900 49 6e 20 6f 72 64 65 72 20 74 6f 20 61 76 6f 69 In order to avoi
e0910 64 20 70 72 6f 62 6c 65 6d 73 20 77 69 74 68 20 d problems with
e0920 75 70 70 65 72 0a 20 20 2a 2a 20 6c 61 79 65 72 upper. ** layer
e0930 73 2c 20 77 65 20 6e 65 65 64 20 74 6f 20 72 65 s, we need to re
e0940 70 6f 72 74 20 74 68 69 73 20 66 69 6c 65 20 73 port this file s
e0950 69 7a 65 20 61 73 20 7a 65 72 6f 20 65 76 65 6e ize as zero even
e0960 20 74 68 6f 75 67 68 20 69 74 20 69 73 0a 20 20 though it is.
e0970 2a 2a 20 72 65 61 6c 6c 79 20 31 2e 20 20 20 54 ** really 1. T
e0980 69 63 6b 65 74 20 23 33 32 36 30 2e 0a 20 20 2a icket #3260.. *
e0990 2f 0a 20 20 69 66 28 20 2a 70 53 69 7a 65 3d 3d /. if( *pSize==
e09a0 31 20 29 20 2a 70 53 69 7a 65 20 3d 20 30 3b 0a 1 ) *pSize = 0;.
e09b0 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 .. return SQLIT
e09c0 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 20 53 51 4c E_OK;.}..#if SQL
e09d0 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 ITE_ENABLE_LOCKI
e09e0 4e 47 5f 53 54 59 4c 45 20 26 26 20 64 65 66 69 NG_STYLE && defi
e09f0 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 0a 2f ned(__APPLE__)./
e0a00 2a 0a 2a 2a 20 48 61 6e 64 6c 65 72 20 66 6f 72 *.** Handler for
e0a10 20 70 72 6f 78 79 2d 6c 6f 63 6b 69 6e 67 20 66 proxy-locking f
e0a20 69 6c 65 2d 63 6f 6e 74 72 6f 6c 20 76 65 72 62 ile-control verb
e0a30 73 2e 20 20 44 65 66 69 6e 65 64 20 62 65 6c 6f s. Defined belo
e0a40 77 20 69 6e 20 74 68 65 0a 2a 2a 20 70 72 6f 78 w in the.** prox
e0a50 79 69 6e 67 20 6c 6f 63 6b 69 6e 67 20 64 69 76 ying locking div
e0a60 69 73 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 ision..*/.static
e0a70 20 69 6e 74 20 70 72 6f 78 79 46 69 6c 65 43 6f int proxyFileCo
e0a80 6e 74 72 6f 6c 28 73 71 6c 69 74 65 33 5f 66 69 ntrol(sqlite3_fi
e0a90 6c 65 2a 2c 69 6e 74 2c 76 6f 69 64 2a 29 3b 0a le*,int,void*);.
e0aa0 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 49 #endif.../*.** I
e0ab0 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 6e 64 20 63 nformation and c
e0ac0 6f 6e 74 72 6f 6c 20 6f 66 20 61 6e 20 6f 70 65 ontrol of an ope
e0ad0 6e 20 66 69 6c 65 20 68 61 6e 64 6c 65 2e 0a 2a n file handle..*
e0ae0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 /.static int uni
e0af0 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 73 71 6c xFileControl(sql
e0b00 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 ite3_file *id, i
e0b10 6e 74 20 6f 70 2c 20 76 6f 69 64 20 2a 70 41 72 nt op, void *pAr
e0b20 67 29 7b 0a 20 20 73 77 69 74 63 68 28 20 6f 70 g){. switch( op
e0b30 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c ){. case SQL
e0b40 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 ITE_FCNTL_LOCKST
e0b50 41 54 45 3a 20 7b 0a 20 20 20 20 20 20 2a 28 69 ATE: {. *(i
e0b60 6e 74 2a 29 70 41 72 67 20 3d 20 28 28 75 6e 69 nt*)pArg = ((uni
e0b70 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 6f 63 6b xFile*)id)->lock
e0b80 74 79 70 65 3b 0a 20 20 20 20 20 20 72 65 74 75 type;. retu
e0b90 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
e0ba0 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c }. case SQL
e0bb0 49 54 45 5f 4c 41 53 54 5f 45 52 52 4e 4f 3a 20 ITE_LAST_ERRNO:
e0bc0 7b 0a 20 20 20 20 20 20 2a 28 69 6e 74 2a 29 70 {. *(int*)p
e0bd0 41 72 67 20 3d 20 28 28 75 6e 69 78 46 69 6c 65 Arg = ((unixFile
e0be0 2a 29 69 64 29 2d 3e 6c 61 73 74 45 72 72 6e 6f *)id)->lastErrno
e0bf0 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 ;. return S
e0c00 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a QLITE_OK;. }.
e0c10 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 #ifndef NDEBUG.
e0c20 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65 72 20 /* The pager
e0c30 63 61 6c 6c 73 20 74 68 69 73 20 6d 65 74 68 6f calls this metho
e0c40 64 20 74 6f 20 73 69 67 6e 61 6c 20 74 68 61 74 d to signal that
e0c50 20 69 74 20 68 61 73 20 64 6f 6e 65 0a 20 20 20 it has done.
e0c60 20 2a 2a 20 61 20 72 6f 6c 6c 62 61 63 6b 20 61 ** a rollback a
e0c70 6e 64 20 74 68 61 74 20 74 68 65 20 64 61 74 61 nd that the data
e0c80 62 61 73 65 20 69 73 20 74 68 65 72 65 66 6f 72 base is therefor
e0c90 65 20 75 6e 63 68 61 6e 67 65 64 20 61 6e 64 0a e unchanged and.
e0ca0 20 20 20 20 2a 2a 20 69 74 20 68 65 6e 63 65 20 ** it hence
e0cb0 69 74 20 69 73 20 4f 4b 20 66 6f 72 20 74 68 65 it is OK for the
e0cc0 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 68 61 transaction cha
e0cd0 6e 67 65 20 63 6f 75 6e 74 65 72 20 74 6f 20 62 nge counter to b
e0ce0 65 0a 20 20 20 20 2a 2a 20 75 6e 63 68 61 6e 67 e. ** unchang
e0cf0 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 ed.. */. c
e0d00 61 73 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c ase SQLITE_FCNTL
e0d10 5f 44 42 5f 55 4e 43 48 41 4e 47 45 44 3a 20 7b _DB_UNCHANGED: {
e0d20 0a 20 20 20 20 20 20 28 28 75 6e 69 78 46 69 6c . ((unixFil
e0d30 65 2a 29 69 64 29 2d 3e 64 62 55 70 64 61 74 65 e*)id)->dbUpdate
e0d40 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 65 74 75 = 0;. retu
e0d50 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
e0d60 20 20 7d 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 }.#endif.#if S
e0d70 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 QLITE_ENABLE_LOC
e0d80 4b 49 4e 47 5f 53 54 59 4c 45 20 26 26 20 64 65 KING_STYLE && de
e0d90 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 fined(__APPLE__)
e0da0 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 . case SQLITE
e0db0 5f 53 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 _SET_LOCKPROXYFI
e0dc0 4c 45 3a 0a 20 20 20 20 63 61 73 65 20 53 51 4c LE:. case SQL
e0dd0 49 54 45 5f 47 45 54 5f 4c 4f 43 4b 50 52 4f 58 ITE_GET_LOCKPROX
e0de0 59 46 49 4c 45 3a 20 7b 0a 20 20 20 20 20 20 72 YFILE: {. r
e0df0 65 74 75 72 6e 20 70 72 6f 78 79 46 69 6c 65 43 eturn proxyFileC
e0e00 6f 6e 74 72 6f 6c 28 69 64 2c 6f 70 2c 70 41 72 ontrol(id,op,pAr
e0e10 67 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 g);. }.#endif
e0e20 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c /* SQLITE_ENABL
e0e30 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 E_LOCKING_STYLE
e0e40 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 && defined(__APP
e0e50 4c 45 5f 5f 29 20 2a 2f 0a 20 20 7d 0a 20 20 72 LE__) */. }. r
e0e60 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 eturn SQLITE_ERR
e0e70 4f 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 OR;.}../*.** Ret
e0e80 75 72 6e 20 74 68 65 20 73 65 63 74 6f 72 20 73 urn the sector s
e0e90 69 7a 65 20 69 6e 20 62 79 74 65 73 20 6f 66 20 ize in bytes of
e0ea0 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 62 the underlying b
e0eb0 6c 6f 63 6b 20 64 65 76 69 63 65 20 66 6f 72 0a lock device for.
e0ec0 2a 2a 20 74 68 65 20 73 70 65 63 69 66 69 65 64 ** the specified
e0ed0 20 66 69 6c 65 2e 20 54 68 69 73 20 69 73 20 61 file. This is a
e0ee0 6c 6d 6f 73 74 20 61 6c 77 61 79 73 20 35 31 32 lmost always 512
e0ef0 20 62 79 74 65 73 2c 20 62 75 74 20 6d 61 79 20 bytes, but may
e0f00 62 65 0a 2a 2a 20 6c 61 72 67 65 72 20 66 6f 72 be.** larger for
e0f10 20 73 6f 6d 65 20 64 65 76 69 63 65 73 2e 0a 2a some devices..*
e0f20 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 63 6f 64 65 *.** SQLite code
e0f30 20 61 73 73 75 6d 65 73 20 74 68 69 73 20 66 75 assumes this fu
e0f40 6e 63 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 66 61 nction cannot fa
e0f50 69 6c 2e 20 49 74 20 61 6c 73 6f 20 61 73 73 75 il. It also assu
e0f60 6d 65 73 20 74 68 61 74 0a 2a 2a 20 69 66 20 74 mes that.** if t
e0f70 77 6f 20 66 69 6c 65 73 20 61 72 65 20 63 72 65 wo files are cre
e0f80 61 74 65 64 20 69 6e 20 74 68 65 20 73 61 6d 65 ated in the same
e0f90 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 64 69 72 file-system dir
e0fa0 65 63 74 6f 72 79 20 28 69 2e 65 2e 0a 2a 2a 20 ectory (i.e..**
e0fb0 61 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 69 a database and i
e0fc0 74 73 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 29 ts journal file)
e0fd0 20 74 68 61 74 20 74 68 65 20 73 65 63 74 6f 72 that the sector
e0fe0 20 73 69 7a 65 20 77 69 6c 6c 20 62 65 20 74 68 size will be th
e0ff0 65 0a 2a 2a 20 73 61 6d 65 20 66 6f 72 20 62 6f e.** same for bo
e1000 74 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e th..*/.static in
e1010 74 20 75 6e 69 78 53 65 63 74 6f 72 53 69 7a 65 t unixSectorSize
e1020 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 4e (sqlite3_file *N
e1030 6f 74 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 otUsed){. UNUSE
e1040 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 D_PARAMETER(NotU
e1050 73 65 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 sed);. return S
e1060 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 53 45 QLITE_DEFAULT_SE
e1070 43 54 4f 52 5f 53 49 5a 45 3b 0a 7d 0a 0a 2f 2a CTOR_SIZE;.}../*
e1080 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 64 .** Return the d
e1090 65 76 69 63 65 20 63 68 61 72 61 63 74 65 72 69 evice characteri
e10a0 73 74 69 63 73 20 66 6f 72 20 74 68 65 20 66 69 stics for the fi
e10b0 6c 65 2e 20 54 68 69 73 20 69 73 20 61 6c 77 61 le. This is alwa
e10c0 79 73 20 30 20 66 6f 72 20 75 6e 69 78 2e 0a 2a ys 0 for unix..*
e10d0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 /.static int uni
e10e0 78 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 xDeviceCharacter
e10f0 69 73 74 69 63 73 28 73 71 6c 69 74 65 33 5f 66 istics(sqlite3_f
e1100 69 6c 65 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 ile *NotUsed){.
e1110 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
e1120 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 72 65 R(NotUsed);. re
e1130 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a turn 0;.}../*.**
e1140 20 48 65 72 65 20 65 6e 64 73 20 74 68 65 20 69 Here ends the i
e1150 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 mplementation of
e1160 20 61 6c 6c 20 73 71 6c 69 74 65 33 5f 66 69 6c all sqlite3_fil
e1170 65 20 6d 65 74 68 6f 64 73 2e 0a 2a 2a 0a 2a 2a e methods..**.**
e1180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e1190 2a 2a 2a 2a 20 45 6e 64 20 73 71 6c 69 74 65 33 **** End sqlite3
e11a0 5f 66 69 6c 65 20 4d 65 74 68 6f 64 73 20 2a 2a _file Methods **
e11b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e11c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
e11d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e11e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e11f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e1200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e1210 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f ************/../
e1220 2a 0a 2a 2a 20 54 68 69 73 20 64 69 76 69 73 69 *.** This divisi
e1230 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 64 65 66 69 on contains defi
e1240 6e 69 74 69 6f 6e 73 20 6f 66 20 73 71 6c 69 74 nitions of sqlit
e1250 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 e3_io_methods ob
e1260 6a 65 63 74 73 20 74 68 61 74 0a 2a 2a 20 69 6d jects that.** im
e1270 70 6c 65 6d 65 6e 74 20 76 61 72 69 6f 75 73 20 plement various
e1280 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 73 74 72 file locking str
e1290 61 74 65 67 69 65 73 2e 20 20 49 74 20 61 6c 73 ategies. It als
e12a0 6f 20 63 6f 6e 74 61 69 6e 73 20 64 65 66 69 6e o contains defin
e12b0 69 74 69 6f 6e 73 0a 2a 2a 20 6f 66 20 22 66 69 itions.** of "fi
e12c0 6e 64 65 72 22 20 66 75 6e 63 74 69 6f 6e 73 2e nder" functions.
e12d0 20 20 41 20 66 69 6e 64 65 72 2d 66 75 6e 63 74 A finder-funct
e12e0 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 6c ion is used to l
e12f0 6f 63 61 74 65 20 74 68 65 20 61 70 70 72 6f 70 ocate the approp
e1300 72 69 61 74 65 0a 2a 2a 20 73 71 6c 69 74 65 33 riate.** sqlite3
e1310 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 _io_methods obje
e1320 63 74 20 66 6f 72 20 61 20 70 61 72 74 69 63 75 ct for a particu
e1330 6c 61 72 20 64 61 74 61 62 61 73 65 20 66 69 6c lar database fil
e1340 65 2e 20 20 54 68 65 20 70 41 70 70 44 61 74 61 e. The pAppData
e1350 0a 2a 2a 20 66 69 65 6c 64 20 6f 66 20 74 68 65 .** field of the
e1360 20 73 71 6c 69 74 65 33 5f 76 66 73 20 56 46 53 sqlite3_vfs VFS
e1370 20 6f 62 6a 65 63 74 73 20 61 72 65 20 69 6e 69 objects are ini
e1380 74 69 61 6c 69 7a 65 64 20 74 6f 20 62 65 20 70 tialized to be p
e1390 6f 69 6e 74 65 72 73 20 74 6f 0a 2a 2a 20 74 68 ointers to.** th
e13a0 65 20 63 6f 72 72 65 63 74 20 66 69 6e 64 65 72 e correct finder
e13b0 2d 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 74 68 -function for th
e13c0 61 74 20 56 46 53 2e 0a 2a 2a 0a 2a 2a 20 4d 6f at VFS..**.** Mo
e13d0 73 74 20 66 69 6e 64 65 72 20 66 75 6e 63 74 69 st finder functi
e13e0 6f 6e 73 20 72 65 74 75 72 6e 20 61 20 70 6f 69 ons return a poi
e13f0 6e 74 65 72 20 74 6f 20 61 20 66 69 78 65 64 20 nter to a fixed
e1400 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f sqlite3_io_metho
e1410 64 73 0a 2a 2a 20 6f 62 6a 65 63 74 2e 20 20 54 ds.** object. T
e1420 68 65 20 6f 6e 6c 79 20 69 6e 74 65 72 65 73 74 he only interest
e1430 69 6e 67 20 66 69 6e 64 65 72 2d 66 75 6e 63 74 ing finder-funct
e1440 69 6f 6e 20 69 73 20 61 75 74 6f 6c 6f 63 6b 49 ion is autolockI
e1450 6f 46 69 6e 64 65 72 2c 20 77 68 69 63 68 0a 2a oFinder, which.*
e1460 2a 20 6c 6f 6f 6b 73 20 61 74 20 74 68 65 20 66 * looks at the f
e1470 69 6c 65 73 79 73 74 65 6d 20 74 79 70 65 20 61 ilesystem type a
e1480 6e 64 20 74 72 69 65 73 20 74 6f 20 67 75 65 73 nd tries to gues
e1490 73 20 74 68 65 20 62 65 73 74 20 6c 6f 63 6b 69 s the best locki
e14a0 6e 67 0a 2a 2a 20 73 74 72 61 74 65 67 79 20 66 ng.** strategy f
e14b0 72 6f 6d 20 74 68 61 74 2e 0a 2a 2a 0a 2a 2a 20 rom that..**.**
e14c0 46 6f 72 20 66 69 6e 64 65 72 2d 66 75 6e 74 69 For finder-funti
e14d0 6f 6e 20 46 2c 20 74 77 6f 20 6f 62 6a 65 63 74 on F, two object
e14e0 73 20 61 72 65 20 63 72 65 61 74 65 64 3a 0a 2a s are created:.*
e14f0 2a 0a 2a 2a 20 20 20 20 28 31 29 20 54 68 65 20 *.** (1) The
e1500 72 65 61 6c 20 66 69 6e 64 65 72 2d 66 75 6e 63 real finder-func
e1510 74 69 6f 6e 20 6e 61 6d 65 64 20 22 46 49 6d 70 tion named "FImp
e1520 74 28 29 22 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 28 t()"..**.** (
e1530 32 29 20 41 20 63 6f 6e 73 74 61 6e 74 20 70 6f 2) A constant po
e1540 69 6e 74 65 72 20 74 6f 20 74 68 69 73 20 66 75 inter to this fu
e1550 6e 63 74 69 6f 6e 20 6e 61 6d 65 64 20 6a 75 73 nction named jus
e1560 74 20 22 46 22 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 t "F"..**.**.**
e1570 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 A pointer to the
e1580 20 46 20 70 6f 69 6e 74 65 72 20 69 73 20 75 73 F pointer is us
e1590 65 64 20 61 73 20 74 68 65 20 70 41 70 70 44 61 ed as the pAppDa
e15a0 74 61 20 76 61 6c 75 65 20 66 6f 72 20 56 46 53 ta value for VFS
e15b0 0a 2a 2a 20 6f 62 6a 65 63 74 73 2e 20 20 57 65 .** objects. We
e15c0 20 68 61 76 65 20 74 6f 20 64 6f 20 74 68 69 73 have to do this
e15d0 20 69 6e 73 74 65 61 64 20 6f 66 20 6c 65 74 74 instead of lett
e15e0 69 6e 67 20 70 41 70 70 44 61 74 61 20 70 6f 69 ing pAppData poi
e15f0 6e 74 0a 2a 2a 20 64 69 72 65 63 74 6c 79 20 61 nt.** directly a
e1600 74 20 74 68 65 20 66 69 6e 64 65 72 2d 66 75 6e t the finder-fun
e1610 63 74 69 6f 6e 20 73 69 6e 63 65 20 43 39 30 20 ction since C90
e1620 72 75 6c 65 73 20 70 72 65 76 65 6e 74 20 61 20 rules prevent a
e1630 76 6f 69 64 2a 0a 2a 2a 20 66 72 6f 6d 20 62 65 void*.** from be
e1640 20 63 61 73 74 20 69 6e 74 6f 20 61 20 66 75 6e cast into a fun
e1650 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 2e 0a 2a ction pointer..*
e1660 2a 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 69 6e 73 *.**.** Each ins
e1670 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6d 61 tance of this ma
e1680 63 72 6f 20 67 65 6e 65 72 61 74 65 73 20 74 77 cro generates tw
e1690 6f 20 6f 62 6a 65 63 74 73 3a 0a 2a 2a 0a 2a 2a o objects:.**.**
e16a0 20 20 20 2a 20 20 41 20 63 6f 6e 73 74 61 6e 74 * A constant
e16b0 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 sqlite3_io_meth
e16c0 6f 64 73 20 6f 62 6a 65 63 74 20 63 61 6c 6c 20 ods object call
e16d0 4d 45 54 48 4f 44 20 74 68 61 74 20 68 61 73 20 METHOD that has
e16e0 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 20 20 20 20 20 locking.**
e16f0 6d 65 74 68 6f 64 73 20 43 4c 4f 53 45 2c 20 4c methods CLOSE, L
e1700 4f 43 4b 2c 20 55 4e 4c 4f 43 4b 2c 20 43 4b 52 OCK, UNLOCK, CKR
e1710 45 53 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 20 20 ESLOCK..**.**
e1720 2a 20 20 41 6e 20 49 2f 4f 20 6d 65 74 68 6f 64 * An I/O method
e1730 20 66 69 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e finder function
e1740 20 63 61 6c 6c 65 64 20 46 49 4e 44 45 52 20 74 called FINDER t
e1750 68 61 74 20 72 65 74 75 72 6e 73 20 61 20 70 6f hat returns a po
e1760 69 6e 74 65 72 0a 2a 2a 20 20 20 20 20 20 74 6f inter.** to
e1770 20 74 68 65 20 4d 45 54 48 4f 44 20 6f 62 6a 65 the METHOD obje
e1780 63 74 20 69 6e 20 74 68 65 20 70 72 65 76 69 6f ct in the previo
e1790 75 73 20 62 75 6c 6c 65 74 2e 0a 2a 2f 0a 23 64 us bullet..*/.#d
e17a0 65 66 69 6e 65 20 49 4f 4d 45 54 48 4f 44 53 28 efine IOMETHODS(
e17b0 46 49 4e 44 45 52 2c 20 4d 45 54 48 4f 44 2c 20 FINDER, METHOD,
e17c0 43 4c 4f 53 45 2c 20 4c 4f 43 4b 2c 20 55 4e 4c CLOSE, LOCK, UNL
e17d0 4f 43 4b 2c 20 43 4b 4c 4f 43 4b 29 20 20 20 20 OCK, CKLOCK)
e17e0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 73 74 61 \.sta
e17f0 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 tic const sqlite
e1800 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 4d 45 54 3_io_methods MET
e1810 48 4f 44 20 3d 20 7b 20 20 20 20 20 20 20 20 20 HOD = {
e1820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1830 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 31 \. 1
e1840 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
e1850 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 56 /* iV
e1860 65 72 73 69 6f 6e 20 2a 2f 20 20 20 20 20 20 20 ersion */
e1870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1880 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 43 4c \. CL
e1890 4f 53 45 2c 20 20 20 20 20 20 20 20 20 20 20 20 OSE,
e18a0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43 6c /* xCl
e18b0 6f 73 65 20 2a 2f 20 20 20 20 20 20 20 20 20 20 ose */
e18c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e18d0 20 20 20 20 20 20 20 20 5c 0a 20 20 20 75 6e 69 \. uni
e18e0 78 52 65 61 64 2c 20 20 20 20 20 20 20 20 20 20 xRead,
e18f0 20 20 20 20 20 20 20 20 20 2f 2a 20 78 52 65 61 /* xRea
e1900 64 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 d */
e1910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1920 20 20 20 20 20 20 20 5c 0a 20 20 20 75 6e 69 78 \. unix
e1930 57 72 69 74 65 2c 20 20 20 20 20 20 20 20 20 20 Write,
e1940 20 20 20 20 20 20 20 20 2f 2a 20 78 57 72 69 74 /* xWrit
e1950 65 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 e */
e1960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1970 20 20 20 20 20 20 5c 0a 20 20 20 75 6e 69 78 54 \. unixT
e1980 72 75 6e 63 61 74 65 2c 20 20 20 20 20 20 20 20 runcate,
e1990 20 20 20 20 20 20 20 2f 2a 20 78 54 72 75 6e 63 /* xTrunc
e19a0 61 74 65 20 2a 2f 20 20 20 20 20 20 20 20 20 20 ate */
e19b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e19c0 20 20 20 20 20 5c 0a 20 20 20 75 6e 69 78 53 79 \. unixSy
e19d0 6e 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 nc,
e19e0 20 20 20 20 20 20 2f 2a 20 78 53 79 6e 63 20 2a /* xSync *
e19f0 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 /
e1a00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1a10 20 20 20 20 5c 0a 20 20 20 75 6e 69 78 46 69 6c \. unixFil
e1a20 65 53 69 7a 65 2c 20 20 20 20 20 20 20 20 20 20 eSize,
e1a30 20 20 20 20 20 2f 2a 20 78 46 69 6c 65 53 69 7a /* xFileSiz
e1a40 65 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 e */
e1a50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1a60 20 20 20 5c 0a 20 20 20 4c 4f 43 4b 2c 20 20 20 \. LOCK,
e1a70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1a80 20 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 2a 2f 20 /* xLock */
e1a90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1aa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1ab0 20 20 5c 0a 20 20 20 55 4e 4c 4f 43 4b 2c 20 20 \. UNLOCK,
e1ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1ad0 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 2a 2f /* xUnlock */
e1ae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1af0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1b00 20 5c 0a 20 20 20 43 4b 4c 4f 43 4b 2c 20 20 20 \. CKLOCK,
e1b10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1b20 20 20 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 72 /* xCheckReser
e1b30 76 65 64 4c 6f 63 6b 20 2a 2f 20 20 20 20 20 20 vedLock */
e1b40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1b50 5c 0a 20 20 20 75 6e 69 78 46 69 6c 65 43 6f 6e \. unixFileCon
e1b60 74 72 6f 6c 2c 20 20 20 20 20 20 20 20 20 20 20 trol,
e1b70 20 2f 2a 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c /* xFileControl
e1b80 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 */
e1b90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
e1ba0 0a 20 20 20 75 6e 69 78 53 65 63 74 6f 72 53 69 . unixSectorSi
e1bb0 7a 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 ze,
e1bc0 2f 2a 20 78 53 65 63 74 6f 72 53 69 7a 65 20 2a /* xSectorSize *
e1bd0 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 /
e1be0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
e1bf0 20 20 20 75 6e 69 78 44 65 76 69 63 65 43 68 61 unixDeviceCha
e1c00 72 61 63 74 65 72 69 73 74 69 63 73 20 20 20 2f racteristics /
e1c10 2a 20 78 44 65 76 69 63 65 43 61 70 61 62 69 6c * xDeviceCapabil
e1c20 69 74 69 65 73 20 2a 2f 20 20 20 20 20 20 20 20 ities */
e1c30 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 7d \.}
e1c40 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
e1c50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1c60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1c70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1c80 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 73 74 \.st
e1c90 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 atic const sqlit
e1ca0 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 46 e3_io_methods *F
e1cb0 49 4e 44 45 52 23 23 49 6d 70 6c 28 63 6f 6e 73 INDER##Impl(cons
e1cc0 74 20 63 68 61 72 20 2a 7a 2c 20 75 6e 69 78 46 t char *z, unixF
e1cd0 69 6c 65 20 2a 70 29 7b 20 20 20 5c 0a 20 20 55 ile *p){ \. U
e1ce0 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
e1cf0 7a 29 3b 20 55 4e 55 53 45 44 5f 50 41 52 41 4d z); UNUSED_PARAM
e1d00 45 54 45 52 28 70 29 3b 20 20 20 20 20 20 20 20 ETER(p);
e1d10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1d20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 72 65 \. re
e1d30 74 75 72 6e 20 26 4d 45 54 48 4f 44 3b 20 20 20 turn &METHOD;
e1d40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1d50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1d60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1d70 20 20 20 20 20 20 20 20 20 5c 0a 7d 20 20 20 20 \.}
e1d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1d90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
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 20 20 20 20 20 20 20 20 20
e1dc0 20 20 20 20 20 20 20 20 5c 0a 73 74 61 74 69 63 \.static
e1dd0 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 const sqlite3_i
e1de0 6f 5f 6d 65 74 68 6f 64 73 20 2a 28 2a 63 6f 6e o_methods *(*con
e1df0 73 74 20 46 49 4e 44 45 52 29 28 63 6f 6e 73 74 st FINDER)(const
e1e00 20 63 68 61 72 2a 2c 75 6e 69 78 46 69 6c 65 20 char*,unixFile
e1e10 2a 70 29 20 20 20 20 5c 0a 20 20 20 20 3d 20 46 *p) \. = F
e1e20 49 4e 44 45 52 23 23 49 6d 70 6c 3b 0a 0a 2f 2a INDER##Impl;../*
e1e30 0a 2a 2a 20 48 65 72 65 20 61 72 65 20 61 6c 6c .** Here are all
e1e40 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f of the sqlite3_
e1e50 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 io_methods objec
e1e60 74 73 20 66 6f 72 20 65 61 63 68 20 6f 66 20 74 ts for each of t
e1e70 68 65 0a 2a 2a 20 6c 6f 63 6b 69 6e 67 20 73 74 he.** locking st
e1e80 72 61 74 65 67 69 65 73 2e 20 20 46 75 6e 63 74 rategies. Funct
e1e90 69 6f 6e 73 20 74 68 61 74 20 72 65 74 75 72 6e ions that return
e1ea0 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65 pointers to the
e1eb0 73 65 20 6d 65 74 68 6f 64 73 0a 2a 2a 20 61 72 se methods.** ar
e1ec0 65 20 61 6c 73 6f 20 63 72 65 61 74 65 64 2e 0a e also created..
e1ed0 2a 2f 0a 49 4f 4d 45 54 48 4f 44 53 28 0a 20 20 */.IOMETHODS(.
e1ee0 70 6f 73 69 78 49 6f 46 69 6e 64 65 72 2c 20 20 posixIoFinder,
e1ef0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6e /* Fin
e1f00 64 65 72 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d der function nam
e1f10 65 20 2a 2f 0a 20 20 70 6f 73 69 78 49 6f 4d 65 e */. posixIoMe
e1f20 74 68 6f 64 73 2c 20 20 20 20 20 20 20 20 20 20 thods,
e1f30 20 2f 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d /* sqlite3_io_m
e1f40 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 6e 61 ethods object na
e1f50 6d 65 20 2a 2f 0a 20 20 75 6e 69 78 43 6c 6f 73 me */. unixClos
e1f60 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e,
e1f70 20 20 2f 2a 20 78 43 6c 6f 73 65 20 6d 65 74 68 /* xClose meth
e1f80 6f 64 20 2a 2f 0a 20 20 75 6e 69 78 4c 6f 63 6b od */. unixLock
e1f90 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
e1fa0 20 20 2f 2a 20 78 4c 6f 63 6b 20 6d 65 74 68 6f /* xLock metho
e1fb0 64 20 2a 2f 0a 20 20 75 6e 69 78 55 6e 6c 6f 63 d */. unixUnloc
e1fc0 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 k,
e1fd0 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 /* xUnlock meth
e1fe0 6f 64 20 2a 2f 0a 20 20 75 6e 69 78 43 68 65 63 od */. unixChec
e1ff0 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 20 20 kReservedLock
e2000 20 20 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 72 /* xCheckReser
e2010 76 65 64 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a vedLock method *
e2020 2f 0a 29 0a 49 4f 4d 45 54 48 4f 44 53 28 0a 20 /.).IOMETHODS(.
e2030 20 6e 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 2c nolockIoFinder,
e2040 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 /* Fi
e2050 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e 20 6e 61 nder function na
e2060 6d 65 20 2a 2f 0a 20 20 6e 6f 6c 6f 63 6b 49 6f me */. nolockIo
e2070 4d 65 74 68 6f 64 73 2c 20 20 20 20 20 20 20 20 Methods,
e2080 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f /* sqlite3_io_
e2090 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 6e methods object n
e20a0 61 6d 65 20 2a 2f 0a 20 20 6e 6f 6c 6f 63 6b 43 ame */. nolockC
e20b0 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 lose,
e20c0 20 20 20 2f 2a 20 78 43 6c 6f 73 65 20 6d 65 74 /* xClose met
e20d0 68 6f 64 20 2a 2f 0a 20 20 6e 6f 6c 6f 63 6b 4c hod */. nolockL
e20e0 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 ock,
e20f0 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 6d 65 74 68 /* xLock meth
e2100 6f 64 20 2a 2f 0a 20 20 6e 6f 6c 6f 63 6b 55 6e od */. nolockUn
e2110 6c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20 lock,
e2120 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 6d 65 74 /* xUnlock met
e2130 68 6f 64 20 2a 2f 0a 20 20 6e 6f 6c 6f 63 6b 43 hod */. nolockC
e2140 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b heckReservedLock
e2150 20 20 20 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 /* xCheckRese
e2160 72 76 65 64 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 rvedLock method
e2170 2a 2f 0a 29 0a 49 4f 4d 45 54 48 4f 44 53 28 0a */.).IOMETHODS(.
e2180 20 20 64 6f 74 6c 6f 63 6b 49 6f 46 69 6e 64 65 dotlockIoFinde
e2190 72 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 r, /* F
e21a0 69 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e 20 6e inder function n
e21b0 61 6d 65 20 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b ame */. dotlock
e21c0 49 6f 4d 65 74 68 6f 64 73 2c 20 20 20 20 20 20 IoMethods,
e21d0 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 69 6f /* sqlite3_io
e21e0 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 _methods object
e21f0 6e 61 6d 65 20 2a 2f 0a 20 20 64 6f 74 6c 6f 63 name */. dotloc
e2200 6b 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 20 kClose,
e2210 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65 20 6d 65 /* xClose me
e2220 74 68 6f 64 20 2a 2f 0a 20 20 64 6f 74 6c 6f 63 thod */. dotloc
e2230 6b 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20 kLock,
e2240 20 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 6d 65 74 /* xLock met
e2250 68 6f 64 20 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b hod */. dotlock
e2260 55 6e 6c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 Unlock,
e2270 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 6d 65 /* xUnlock me
e2280 74 68 6f 64 20 2a 2f 0a 20 20 64 6f 74 6c 6f 63 thod */. dotloc
e2290 6b 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f kCheckReservedLo
e22a0 63 6b 20 20 2f 2a 20 78 43 68 65 63 6b 52 65 73 ck /* xCheckRes
e22b0 65 72 76 65 64 4c 6f 63 6b 20 6d 65 74 68 6f 64 ervedLock method
e22c0 20 2a 2f 0a 29 0a 0a 23 69 66 20 53 51 4c 49 54 */.)..#if SQLIT
e22d0 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 E_ENABLE_LOCKING
e22e0 5f 53 54 59 4c 45 20 26 26 20 21 4f 53 5f 56 58 _STYLE && !OS_VX
e22f0 57 4f 52 4b 53 0a 49 4f 4d 45 54 48 4f 44 53 28 WORKS.IOMETHODS(
e2300 0a 20 20 66 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 . flockIoFinder
e2310 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 , /*
e2320 46 69 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e 20 Finder function
e2330 6e 61 6d 65 20 2a 2f 0a 20 20 66 6c 6f 63 6b 49 name */. flockI
e2340 6f 4d 65 74 68 6f 64 73 2c 20 20 20 20 20 20 20 oMethods,
e2350 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 69 /* sqlite3_i
e2360 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 o_methods object
e2370 20 6e 61 6d 65 20 2a 2f 0a 20 20 66 6c 6f 63 6b name */. flock
e2380 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 20 20 Close,
e2390 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65 20 6d /* xClose m
e23a0 65 74 68 6f 64 20 2a 2f 0a 20 20 66 6c 6f 63 6b ethod */. flock
e23b0 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20 Lock,
e23c0 20 20 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 6d 65 /* xLock me
e23d0 74 68 6f 64 20 2a 2f 0a 20 20 66 6c 6f 63 6b 55 thod */. flockU
e23e0 6e 6c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20 nlock,
e23f0 20 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 6d /* xUnlock m
e2400 65 74 68 6f 64 20 2a 2f 0a 20 20 66 6c 6f 63 6b ethod */. flock
e2410 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 CheckReservedLoc
e2420 6b 20 20 20 20 2f 2a 20 78 43 68 65 63 6b 52 65 k /* xCheckRe
e2430 73 65 72 76 65 64 4c 6f 63 6b 20 6d 65 74 68 6f servedLock metho
e2440 64 20 2a 2f 0a 29 0a 23 65 6e 64 69 66 0a 0a 23 d */.).#endif..#
e2450 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 49 4f if OS_VXWORKS.IO
e2460 4d 45 54 48 4f 44 53 28 0a 20 20 73 65 6d 49 6f METHODS(. semIo
e2470 46 69 6e 64 65 72 2c 20 20 20 20 20 20 20 20 20 Finder,
e2480 20 20 20 20 20 2f 2a 20 46 69 6e 64 65 72 20 66 /* Finder f
e2490 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a unction name */.
e24a0 20 20 73 65 6d 49 6f 4d 65 74 68 6f 64 73 2c 20 semIoMethods,
e24b0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 /* s
e24c0 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 qlite3_io_method
e24d0 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20 2a 2f s object name */
e24e0 0a 20 20 73 65 6d 43 6c 6f 73 65 2c 20 20 20 20 . semClose,
e24f0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
e2500 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 20 2a 2f xClose method */
e2510 0a 20 20 73 65 6d 4c 6f 63 6b 2c 20 20 20 20 20 . semLock,
e2520 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
e2530 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a xLock method */.
e2540 20 20 73 65 6d 55 6e 6c 6f 63 6b 2c 20 20 20 20 semUnlock,
e2550 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 /* x
e2560 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f Unlock method */
e2570 0a 20 20 73 65 6d 43 68 65 63 6b 52 65 73 65 72 . semCheckReser
e2580 76 65 64 4c 6f 63 6b 20 20 20 20 20 20 2f 2a 20 vedLock /*
e2590 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f xCheckReservedLo
e25a0 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 29 0a 23 ck method */.).#
e25b0 65 6e 64 69 66 0a 0a 23 69 66 20 64 65 66 69 6e endif..#if defin
e25c0 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 26 ed(__APPLE__) &&
e25d0 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c SQLITE_ENABLE_L
e25e0 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 49 4f 4d OCKING_STYLE.IOM
e25f0 45 54 48 4f 44 53 28 0a 20 20 61 66 70 49 6f 46 ETHODS(. afpIoF
e2600 69 6e 64 65 72 2c 20 20 20 20 20 20 20 20 20 20 inder,
e2610 20 20 20 20 2f 2a 20 46 69 6e 64 65 72 20 66 75 /* Finder fu
e2620 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 nction name */.
e2630 20 61 66 70 49 6f 4d 65 74 68 6f 64 73 2c 20 20 afpIoMethods,
e2640 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 71 /* sq
e2650 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 lite3_io_methods
e2660 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20 2a 2f 0a object name */.
e2670 20 20 61 66 70 43 6c 6f 73 65 2c 20 20 20 20 20 afpClose,
e2680 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 /* x
e2690 43 6c 6f 73 65 20 6d 65 74 68 6f 64 20 2a 2f 0a Close method */.
e26a0 20 20 61 66 70 4c 6f 63 6b 2c 20 20 20 20 20 20 afpLock,
e26b0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 /* x
e26c0 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 Lock method */.
e26d0 20 61 66 70 55 6e 6c 6f 63 6b 2c 20 20 20 20 20 afpUnlock,
e26e0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 55 /* xU
e26f0 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a nlock method */.
e2700 20 20 61 66 70 43 68 65 63 6b 52 65 73 65 72 76 afpCheckReserv
e2710 65 64 4c 6f 63 6b 20 20 20 20 20 20 2f 2a 20 78 edLock /* x
e2720 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 CheckReservedLoc
e2730 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 29 0a 23 65 k method */.).#e
e2740 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 ndif../*.** The
e2750 22 57 68 6f 6c 65 20 46 69 6c 65 20 4c 6f 63 6b "Whole File Lock
e2760 69 6e 67 22 20 66 69 6e 64 65 72 20 72 65 74 75 ing" finder retu
e2770 72 6e 73 20 74 68 65 20 73 61 6d 65 20 73 65 74 rns the same set
e2780 20 6f 66 20 6d 65 74 68 6f 64 73 20 61 73 0a 2a of methods as.*
e2790 2a 20 74 68 65 20 70 6f 73 69 78 20 6c 6f 63 6b * the posix lock
e27a0 69 6e 67 20 66 69 6e 64 65 72 2e 20 20 42 75 74 ing finder. But
e27b0 20 69 74 20 61 6c 73 6f 20 73 65 74 73 20 74 68 it also sets th
e27c0 65 20 53 51 4c 49 54 45 5f 57 48 4f 4c 45 5f 46 e SQLITE_WHOLE_F
e27d0 49 4c 45 5f 4c 4f 43 4b 49 4e 47 0a 2a 2a 20 66 ILE_LOCKING.** f
e27e0 6c 61 67 20 74 6f 20 66 6f 72 63 65 20 74 68 65 lag to force the
e27f0 20 70 6f 73 69 78 20 61 64 76 69 73 6f 72 79 20 posix advisory
e2800 6c 6f 63 6b 73 20 74 6f 20 63 6f 76 65 72 20 74 locks to cover t
e2810 68 65 20 77 68 6f 6c 65 20 66 69 6c 65 20 69 6e he whole file in
e2820 73 74 65 61 64 0a 2a 2a 20 6f 66 20 6a 75 73 74 stead.** of just
e2830 20 61 20 73 6d 61 6c 6c 20 73 70 61 6e 20 6f 66 a small span of
e2840 20 62 79 74 65 73 20 6e 65 61 72 20 74 68 65 20 bytes near the
e2850 31 47 69 42 20 62 6f 75 6e 64 61 72 79 2e 20 20 1GiB boundary.
e2860 57 68 6f 6c 65 20 46 69 6c 65 20 4c 6f 63 6b 69 Whole File Locki
e2870 6e 67 0a 2a 2a 20 69 73 20 75 73 65 66 75 6c 20 ng.** is useful
e2880 6f 6e 20 4e 46 53 2d 6d 6f 75 6e 74 65 64 20 66 on NFS-mounted f
e2890 69 6c 65 73 20 73 69 6e 63 65 20 69 74 20 68 65 iles since it he
e28a0 6c 70 73 20 4e 46 53 20 74 6f 20 6d 61 69 6e 74 lps NFS to maint
e28b0 61 69 6e 20 63 61 63 68 65 0a 2a 2a 20 63 6f 68 ain cache.** coh
e28c0 65 72 65 6e 63 79 2e 20 20 42 75 74 20 69 74 20 erency. But it
e28d0 69 73 20 61 20 64 65 74 72 69 6d 65 6e 74 20 74 is a detriment t
e28e0 6f 20 6f 74 68 65 72 20 66 69 6c 65 73 79 73 74 o other filesyst
e28f0 65 6d 73 20 73 69 6e 63 65 20 69 74 20 72 75 6e ems since it run
e2900 73 0a 2a 2a 20 73 6c 6f 77 65 72 2e 0a 2a 2f 0a s.** slower..*/.
e2910 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c static const sql
e2920 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 ite3_io_methods
e2930 2a 70 6f 73 69 78 57 66 6c 49 6f 46 69 6e 64 65 *posixWflIoFinde
e2940 72 49 6d 70 6c 28 63 6f 6e 73 74 20 63 68 61 72 rImpl(const char
e2950 2a 7a 2c 20 75 6e 69 78 46 69 6c 65 2a 70 29 7b *z, unixFile*p){
e2960 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
e2970 54 45 52 28 7a 29 3b 0a 20 20 70 2d 3e 66 69 6c TER(z);. p->fil
e2980 65 46 6c 61 67 73 20 3d 20 53 51 4c 49 54 45 5f eFlags = SQLITE_
e2990 57 48 4f 4c 45 5f 46 49 4c 45 5f 4c 4f 43 4b 49 WHOLE_FILE_LOCKI
e29a0 4e 47 3b 0a 20 20 72 65 74 75 72 6e 20 26 70 6f NG;. return &po
e29b0 73 69 78 49 6f 4d 65 74 68 6f 64 73 3b 0a 7d 0a sixIoMethods;.}.
e29c0 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c static const sql
e29d0 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 ite3_io_methods
e29e0 0a 20 20 2a 28 2a 63 6f 6e 73 74 20 70 6f 73 69 . *(*const posi
e29f0 78 57 66 6c 49 6f 46 69 6e 64 65 72 29 28 63 6f xWflIoFinder)(co
e2a00 6e 73 74 20 63 68 61 72 2a 2c 75 6e 69 78 46 69 nst char*,unixFi
e2a10 6c 65 20 2a 70 29 20 3d 20 70 6f 73 69 78 57 66 le *p) = posixWf
e2a20 6c 49 6f 46 69 6e 64 65 72 49 6d 70 6c 3b 0a 0a lIoFinderImpl;..
e2a30 2f 2a 0a 2a 2a 20 54 68 65 20 70 72 6f 78 79 20 /*.** The proxy
e2a40 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f 64 20 69 locking method i
e2a50 73 20 61 20 22 73 75 70 65 72 2d 6d 65 74 68 6f s a "super-metho
e2a60 64 22 20 69 6e 20 74 68 65 20 73 65 6e 73 65 20 d" in the sense
e2a70 74 68 61 74 20 69 74 0a 2a 2a 20 6f 70 65 6e 73 that it.** opens
e2a80 20 73 65 63 6f 6e 64 61 72 79 20 66 69 6c 65 20 secondary file
e2a90 64 65 73 63 72 69 70 74 6f 72 73 20 66 6f 72 20 descriptors for
e2aa0 74 68 65 20 63 6f 6e 63 68 20 61 6e 64 20 6c 6f the conch and lo
e2ab0 63 6b 20 66 69 6c 65 73 20 61 6e 64 0a 2a 2a 20 ck files and.**
e2ac0 69 74 20 75 73 65 73 20 70 72 6f 78 79 2c 20 64 it uses proxy, d
e2ad0 6f 74 2d 66 69 6c 65 2c 20 41 46 50 2c 20 61 6e ot-file, AFP, an
e2ae0 64 20 66 6c 6f 63 6b 28 29 20 6c 6f 63 6b 69 6e d flock() lockin
e2af0 67 20 6d 65 74 68 6f 64 73 20 6f 6e 20 74 68 6f g methods on tho
e2b00 73 65 0a 2a 2a 20 73 65 63 6f 6e 64 61 72 79 20 se.** secondary
e2b10 66 69 6c 65 73 2e 20 20 46 6f 72 20 74 68 69 73 files. For this
e2b20 20 72 65 61 73 6f 6e 2c 20 74 68 65 20 64 69 76 reason, the div
e2b30 69 73 69 6f 6e 20 74 68 61 74 20 69 6d 70 6c 65 ision that imple
e2b40 6d 65 6e 74 73 0a 2a 2a 20 70 72 6f 78 79 20 6c ments.** proxy l
e2b50 6f 63 6b 69 6e 67 20 69 73 20 6c 6f 63 61 74 65 ocking is locate
e2b60 64 20 6d 75 63 68 20 66 75 72 74 68 65 72 20 64 d much further d
e2b70 6f 77 6e 20 69 6e 20 74 68 65 20 66 69 6c 65 2e own in the file.
e2b80 20 20 42 75 74 20 77 65 20 6e 65 65 64 0a 2a 2a But we need.**
e2b90 20 74 6f 20 67 6f 20 61 68 65 61 64 20 61 6e 64 to go ahead and
e2ba0 20 64 65 66 69 6e 65 20 74 68 65 20 73 71 6c 69 define the sqli
e2bb0 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 61 te3_io_methods a
e2bc0 6e 64 20 66 69 6e 64 65 72 20 66 75 6e 63 74 69 nd finder functi
e2bd0 6f 6e 0a 2a 2a 20 66 6f 72 20 70 72 6f 78 79 20 on.** for proxy
e2be0 6c 6f 63 6b 69 6e 67 20 68 65 72 65 2e 20 20 53 locking here. S
e2bf0 6f 20 77 65 20 66 6f 72 77 61 72 64 20 64 65 63 o we forward dec
e2c00 6c 61 72 65 20 74 68 65 20 49 2f 4f 20 6d 65 74 lare the I/O met
e2c10 68 6f 64 73 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 hods..*/.#if def
e2c20 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 ined(__APPLE__)
e2c30 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 && SQLITE_ENABLE
e2c40 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 73 _LOCKING_STYLE.s
e2c50 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 43 tatic int proxyC
e2c60 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c lose(sqlite3_fil
e2c70 65 2a 29 3b 0a 73 74 61 74 69 63 20 69 6e 74 20 e*);.static int
e2c80 70 72 6f 78 79 4c 6f 63 6b 28 73 71 6c 69 74 65 proxyLock(sqlite
e2c90 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a 73 3_file*, int);.s
e2ca0 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 55 tatic int proxyU
e2cb0 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 nlock(sqlite3_fi
e2cc0 6c 65 2a 2c 20 69 6e 74 29 3b 0a 73 74 61 74 69 le*, int);.stati
e2cd0 63 20 69 6e 74 20 70 72 6f 78 79 43 68 65 63 6b c int proxyCheck
e2ce0 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c ReservedLock(sql
e2cf0 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 2a ite3_file*, int*
e2d00 29 3b 0a 49 4f 4d 45 54 48 4f 44 53 28 0a 20 20 );.IOMETHODS(.
e2d10 70 72 6f 78 79 49 6f 46 69 6e 64 65 72 2c 20 20 proxyIoFinder,
e2d20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6e /* Fin
e2d30 64 65 72 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d der function nam
e2d40 65 20 2a 2f 0a 20 20 70 72 6f 78 79 49 6f 4d 65 e */. proxyIoMe
e2d50 74 68 6f 64 73 2c 20 20 20 20 20 20 20 20 20 20 thods,
e2d60 20 2f 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d /* sqlite3_io_m
e2d70 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 6e 61 ethods object na
e2d80 6d 65 20 2a 2f 0a 20 20 70 72 6f 78 79 43 6c 6f me */. proxyClo
e2d90 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 se,
e2da0 20 20 2f 2a 20 78 43 6c 6f 73 65 20 6d 65 74 68 /* xClose meth
e2db0 6f 64 20 2a 2f 0a 20 20 70 72 6f 78 79 4c 6f 63 od */. proxyLoc
e2dc0 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 k,
e2dd0 20 20 2f 2a 20 78 4c 6f 63 6b 20 6d 65 74 68 6f /* xLock metho
e2de0 64 20 2a 2f 0a 20 20 70 72 6f 78 79 55 6e 6c 6f d */. proxyUnlo
e2df0 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 ck,
e2e00 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 /* xUnlock meth
e2e10 6f 64 20 2a 2f 0a 20 20 70 72 6f 78 79 43 68 65 od */. proxyChe
e2e20 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 20 ckReservedLock
e2e30 20 20 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 72 /* xCheckReser
e2e40 76 65 64 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a vedLock method *
e2e50 2f 0a 29 0a 23 65 6e 64 69 66 0a 0a 0a 23 69 66 /.).#endif...#if
e2e60 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 defined(__APPLE
e2e70 5f 5f 29 20 26 26 20 53 51 4c 49 54 45 5f 45 4e __) && SQLITE_EN
e2e80 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 ABLE_LOCKING_STY
e2e90 4c 45 0a 2f 2a 20 0a 2a 2a 20 54 68 69 73 20 22 LE./* .** This "
e2ea0 66 69 6e 64 65 72 22 20 66 75 6e 63 74 69 6f 6e finder" function
e2eb0 20 61 74 74 65 6d 70 74 73 20 74 6f 20 64 65 74 attempts to det
e2ec0 65 72 6d 69 6e 65 20 74 68 65 20 62 65 73 74 20 ermine the best
e2ed0 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74 65 67 79 locking strategy
e2ee0 20 0a 2a 2a 20 66 6f 72 20 74 68 65 20 64 61 74 .** for the dat
e2ef0 61 62 61 73 65 20 66 69 6c 65 20 22 66 69 6c 65 abase file "file
e2f00 50 61 74 68 22 2e 20 20 49 74 20 74 68 65 6e 20 Path". It then
e2f10 72 65 74 75 72 6e 73 20 74 68 65 20 73 71 6c 69 returns the sqli
e2f20 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 0a 2a te3_io_methods.*
e2f30 2a 20 6f 62 6a 65 63 74 20 74 68 61 74 20 69 6d * object that im
e2f40 70 6c 65 6d 65 6e 74 73 20 74 68 61 74 20 73 74 plements that st
e2f50 72 61 74 65 67 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 rategy..**.** Th
e2f60 69 73 20 69 73 20 66 6f 72 20 4d 61 63 4f 53 58 is is for MacOSX
e2f70 20 6f 6e 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 only..*/.static
e2f80 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 const sqlite3_i
e2f90 6f 5f 6d 65 74 68 6f 64 73 20 2a 61 75 74 6f 6c o_methods *autol
e2fa0 6f 63 6b 49 6f 46 69 6e 64 65 72 49 6d 70 6c 28 ockIoFinderImpl(
e2fb0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 66 . const char *f
e2fc0 69 6c 65 50 61 74 68 2c 20 20 20 20 2f 2a 20 6e ilePath, /* n
e2fd0 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 ame of the datab
e2fe0 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 75 6e ase file */. un
e2ff0 69 78 46 69 6c 65 20 2a 70 4e 65 77 20 20 20 20 ixFile *pNew
e3000 20 20 20 20 20 20 20 2f 2a 20 6f 70 65 6e 20 66 /* open f
e3010 69 6c 65 20 6f 62 6a 65 63 74 20 66 6f 72 20 74 ile object for t
e3020 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
e3030 20 2a 2f 0a 29 7b 0a 20 20 73 74 61 74 69 63 20 */.){. static
e3040 63 6f 6e 73 74 20 73 74 72 75 63 74 20 4d 61 70 const struct Map
e3050 70 69 6e 67 20 7b 0a 20 20 20 20 63 6f 6e 73 74 ping {. const
e3060 20 63 68 61 72 20 2a 7a 46 69 6c 65 73 79 73 74 char *zFilesyst
e3070 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 em;
e3080 20 2f 2a 20 46 69 6c 65 73 79 73 74 65 6d 20 74 /* Filesystem t
e3090 79 70 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 ype name */.
e30a0 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f const sqlite3_io
e30b0 5f 6d 65 74 68 6f 64 73 20 2a 70 4d 65 74 68 6f _methods *pMetho
e30c0 64 73 3b 20 20 20 2f 2a 20 41 70 70 72 6f 70 72 ds; /* Appropr
e30d0 69 61 74 65 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 iate locking met
e30e0 68 6f 64 20 2a 2f 0a 20 20 7d 20 61 4d 61 70 5b hod */. } aMap[
e30f0 5d 20 3d 20 7b 0a 20 20 20 20 7b 20 22 68 66 73 ] = {. { "hfs
e3100 22 2c 20 20 20 20 26 70 6f 73 69 78 49 6f 4d 65 ", &posixIoMe
e3110 74 68 6f 64 73 20 7d 2c 0a 20 20 20 20 7b 20 22 thods },. { "
e3120 75 66 73 22 2c 20 20 20 20 26 70 6f 73 69 78 49 ufs", &posixI
e3130 6f 4d 65 74 68 6f 64 73 20 7d 2c 0a 20 20 20 20 oMethods },.
e3140 7b 20 22 61 66 70 66 73 22 2c 20 20 26 61 66 70 { "afpfs", &afp
e3150 49 6f 4d 65 74 68 6f 64 73 20 7d 2c 0a 23 69 66 IoMethods },.#if
e3160 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c def SQLITE_ENABL
e3170 45 5f 41 46 50 5f 4c 4f 43 4b 49 4e 47 5f 53 4d E_AFP_LOCKING_SM
e3180 42 0a 20 20 20 20 7b 20 22 73 6d 62 66 73 22 2c B. { "smbfs",
e3190 20 20 26 61 66 70 49 6f 4d 65 74 68 6f 64 73 20 &afpIoMethods
e31a0 7d 2c 0a 23 65 6c 73 65 0a 20 20 20 20 7b 20 22 },.#else. { "
e31b0 73 6d 62 66 73 22 2c 20 20 26 66 6c 6f 63 6b 49 smbfs", &flockI
e31c0 6f 4d 65 74 68 6f 64 73 20 7d 2c 0a 23 65 6e 64 oMethods },.#end
e31d0 69 66 0a 20 20 20 20 7b 20 22 77 65 62 64 61 76 if. { "webdav
e31e0 22 2c 20 26 6e 6f 6c 6f 63 6b 49 6f 4d 65 74 68 ", &nolockIoMeth
e31f0 6f 64 73 20 7d 2c 0a 20 20 20 20 7b 20 30 2c 20 ods },. { 0,
e3200 30 20 7d 0a 20 20 7d 3b 0a 20 20 69 6e 74 20 69 0 }. };. int i
e3210 3b 0a 20 20 73 74 72 75 63 74 20 73 74 61 74 66 ;. struct statf
e3220 73 20 66 73 49 6e 66 6f 3b 0a 20 20 73 74 72 75 s fsInfo;. stru
e3230 63 74 20 66 6c 6f 63 6b 20 6c 6f 63 6b 49 6e 66 ct flock lockInf
e3240 6f 3b 0a 0a 20 20 69 66 28 20 21 66 69 6c 65 50 o;.. if( !fileP
e3250 61 74 68 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 ath ){. /* If
e3260 20 66 69 6c 65 50 61 74 68 3d 3d 4e 55 4c 4c 20 filePath==NULL
e3270 74 68 61 74 20 6d 65 61 6e 73 20 77 65 20 61 72 that means we ar
e3280 65 20 64 65 61 6c 69 6e 67 20 77 69 74 68 20 61 e dealing with a
e3290 20 74 72 61 6e 73 69 65 6e 74 20 66 69 6c 65 0a transient file.
e32a0 20 20 20 20 2a 2a 20 74 68 61 74 20 64 6f 65 73 ** that does
e32b0 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20 not need to be
e32c0 6c 6f 63 6b 65 64 2e 20 2a 2f 0a 20 20 20 20 72 locked. */. r
e32d0 65 74 75 72 6e 20 26 6e 6f 6c 6f 63 6b 49 6f 4d eturn &nolockIoM
e32e0 65 74 68 6f 64 73 3b 0a 20 20 7d 0a 20 20 69 66 ethods;. }. if
e32f0 28 20 73 74 61 74 66 73 28 66 69 6c 65 50 61 74 ( statfs(filePat
e3300 68 2c 20 26 66 73 49 6e 66 6f 29 20 21 3d 20 2d h, &fsInfo) != -
e3310 31 20 29 7b 0a 20 20 20 20 69 66 28 20 66 73 49 1 ){. if( fsI
e3320 6e 66 6f 2e 66 5f 66 6c 61 67 73 20 26 20 4d 4e nfo.f_flags & MN
e3330 54 5f 52 44 4f 4e 4c 59 20 29 7b 0a 20 20 20 20 T_RDONLY ){.
e3340 20 20 72 65 74 75 72 6e 20 26 6e 6f 6c 6f 63 6b return &nolock
e3350 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 20 20 20 7d IoMethods;. }
e3360 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 61 4d . for(i=0; aM
e3370 61 70 5b 69 5d 2e 7a 46 69 6c 65 73 79 73 74 65 ap[i].zFilesyste
e3380 6d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 m; i++){. i
e3390 66 28 20 73 74 72 63 6d 70 28 66 73 49 6e 66 6f f( strcmp(fsInfo
e33a0 2e 66 5f 66 73 74 79 70 65 6e 61 6d 65 2c 20 61 .f_fstypename, a
e33b0 4d 61 70 5b 69 5d 2e 7a 46 69 6c 65 73 79 73 74 Map[i].zFilesyst
e33c0 65 6d 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 em)==0 ){.
e33d0 20 20 72 65 74 75 72 6e 20 61 4d 61 70 5b 69 5d return aMap[i]
e33e0 2e 70 4d 65 74 68 6f 64 73 3b 0a 20 20 20 20 20 .pMethods;.
e33f0 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 }. }. }..
e3400 2f 2a 20 44 65 66 61 75 6c 74 20 63 61 73 65 2e /* Default case.
e3410 20 48 61 6e 64 6c 65 73 2c 20 61 6d 6f 6e 67 73 Handles, amongs
e3420 74 20 6f 74 68 65 72 73 2c 20 22 6e 66 73 22 2e t others, "nfs".
e3430 0a 20 20 2a 2a 20 54 65 73 74 20 62 79 74 65 2d . ** Test byte-
e3440 72 61 6e 67 65 20 6c 6f 63 6b 20 75 73 69 6e 67 range lock using
e3450 20 66 63 6e 74 6c 28 29 2e 20 49 66 20 74 68 65 fcntl(). If the
e3460 20 63 61 6c 6c 20 73 75 63 63 65 65 64 73 2c 20 call succeeds,
e3470 0a 20 20 2a 2a 20 61 73 73 75 6d 65 20 74 68 61 . ** assume tha
e3480 74 20 74 68 65 20 66 69 6c 65 2d 73 79 73 74 65 t the file-syste
e3490 6d 20 73 75 70 70 6f 72 74 73 20 50 4f 53 49 58 m supports POSIX
e34a0 20 73 74 79 6c 65 20 6c 6f 63 6b 73 2e 20 0a 20 style locks. .
e34b0 20 2a 2f 0a 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c */. lockInfo.l
e34c0 5f 6c 65 6e 20 3d 20 31 3b 0a 20 20 6c 6f 63 6b _len = 1;. lock
e34d0 49 6e 66 6f 2e 6c 5f 73 74 61 72 74 20 3d 20 30 Info.l_start = 0
e34e0 3b 0a 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 77 ;. lockInfo.l_w
e34f0 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45 54 hence = SEEK_SET
e3500 3b 0a 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 74 ;. lockInfo.l_t
e3510 79 70 65 20 3d 20 46 5f 52 44 4c 43 4b 3b 0a 20 ype = F_RDLCK;.
e3520 20 69 66 28 20 66 63 6e 74 6c 28 70 4e 65 77 2d if( fcntl(pNew-
e3530 3e 68 2c 20 46 5f 47 45 54 4c 4b 2c 20 26 6c 6f >h, F_GETLK, &lo
e3540 63 6b 49 6e 66 6f 29 21 3d 2d 31 20 29 20 7b 0a ckInfo)!=-1 ) {.
e3550 20 20 20 20 70 4e 65 77 2d 3e 66 69 6c 65 46 6c pNew->fileFl
e3560 61 67 73 20 3d 20 53 51 4c 49 54 45 5f 57 48 4f ags = SQLITE_WHO
e3570 4c 45 5f 46 49 4c 45 5f 4c 4f 43 4b 49 4e 47 3b LE_FILE_LOCKING;
e3580 0a 20 20 20 20 72 65 74 75 72 6e 20 26 70 6f 73 . return &pos
e3590 69 78 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 20 7d ixIoMethods;. }
e35a0 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e else{. return
e35b0 20 26 64 6f 74 6c 6f 63 6b 49 6f 4d 65 74 68 6f &dotlockIoMetho
e35c0 64 73 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 ds;. }.}.static
e35d0 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 const sqlite3_i
e35e0 6f 5f 6d 65 74 68 6f 64 73 20 0a 20 20 2a 28 2a o_methods . *(*
e35f0 63 6f 6e 73 74 20 61 75 74 6f 6c 6f 63 6b 49 6f const autolockIo
e3600 46 69 6e 64 65 72 29 28 63 6f 6e 73 74 20 63 68 Finder)(const ch
e3610 61 72 2a 2c 75 6e 69 78 46 69 6c 65 2a 29 20 3d ar*,unixFile*) =
e3620 20 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 autolockIoFinde
e3630 72 49 6d 70 6c 3b 0a 0a 23 65 6e 64 69 66 20 2f rImpl;..#endif /
e3640 2a 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c * defined(__APPL
e3650 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 45 5f 45 E__) && SQLITE_E
e3660 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 NABLE_LOCKING_ST
e3670 59 4c 45 20 2a 2f 0a 0a 23 69 66 20 4f 53 5f 56 YLE */..#if OS_V
e3680 58 57 4f 52 4b 53 20 26 26 20 53 51 4c 49 54 45 XWORKS && SQLITE
e3690 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f _ENABLE_LOCKING_
e36a0 53 54 59 4c 45 0a 2f 2a 20 0a 2a 2a 20 54 68 69 STYLE./* .** Thi
e36b0 73 20 22 66 69 6e 64 65 72 22 20 66 75 6e 63 74 s "finder" funct
e36c0 69 6f 6e 20 61 74 74 65 6d 70 74 73 20 74 6f 20 ion attempts to
e36d0 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20 62 65 determine the be
e36e0 73 74 20 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74 st locking strat
e36f0 65 67 79 20 0a 2a 2a 20 66 6f 72 20 74 68 65 20 egy .** for the
e3700 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 22 66 database file "f
e3710 69 6c 65 50 61 74 68 22 2e 20 20 49 74 20 74 68 ilePath". It th
e3720 65 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20 73 en returns the s
e3730 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 qlite3_io_method
e3740 73 0a 2a 2a 20 6f 62 6a 65 63 74 20 74 68 61 74 s.** object that
e3750 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 61 74 implements that
e3760 20 73 74 72 61 74 65 67 79 2e 0a 2a 2a 0a 2a 2a strategy..**.**
e3770 20 54 68 69 73 20 69 73 20 66 6f 72 20 56 58 57 This is for VXW
e3780 6f 72 6b 73 20 6f 6e 6c 79 2e 0a 2a 2f 0a 73 74 orks only..*/.st
e3790 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 atic const sqlit
e37a0 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 61 e3_io_methods *a
e37b0 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 49 utolockIoFinderI
e37c0 6d 70 6c 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 mpl(. const cha
e37d0 72 20 2a 66 69 6c 65 50 61 74 68 2c 20 20 20 20 r *filePath,
e37e0 2f 2a 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 /* name of the d
e37f0 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a atabase file */.
e3800 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 4e 65 77 unixFile *pNew
e3810 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 74 68 /* th
e3820 65 20 6f 70 65 6e 20 66 69 6c 65 20 6f 62 6a 65 e open file obje
e3830 63 74 20 2a 2f 0a 29 7b 0a 20 20 73 74 72 75 63 ct */.){. struc
e3840 74 20 66 6c 6f 63 6b 20 6c 6f 63 6b 49 6e 66 6f t flock lockInfo
e3850 3b 0a 0a 20 20 69 66 28 20 21 66 69 6c 65 50 61 ;.. if( !filePa
e3860 74 68 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 th ){. /* If
e3870 66 69 6c 65 50 61 74 68 3d 3d 4e 55 4c 4c 20 74 filePath==NULL t
e3880 68 61 74 20 6d 65 61 6e 73 20 77 65 20 61 72 65 hat means we are
e3890 20 64 65 61 6c 69 6e 67 20 77 69 74 68 20 61 20 dealing with a
e38a0 74 72 61 6e 73 69 65 6e 74 20 66 69 6c 65 0a 20 transient file.
e38b0 20 20 20 2a 2a 20 74 68 61 74 20 64 6f 65 73 20 ** that does
e38c0 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20 6c not need to be l
e38d0 6f 63 6b 65 64 2e 20 2a 2f 0a 20 20 20 20 72 65 ocked. */. re
e38e0 74 75 72 6e 20 26 6e 6f 6c 6f 63 6b 49 6f 4d 65 turn &nolockIoMe
e38f0 74 68 6f 64 73 3b 0a 20 20 7d 0a 0a 20 20 2f 2a thods;. }.. /*
e3900 20 54 65 73 74 20 69 66 20 66 63 6e 74 6c 28 29 Test if fcntl()
e3910 20 69 73 20 73 75 70 70 6f 72 74 65 64 20 61 6e is supported an
e3920 64 20 75 73 65 20 50 4f 53 49 58 20 73 74 79 6c d use POSIX styl
e3930 65 20 6c 6f 63 6b 73 2e 0a 20 20 2a 2a 20 4f 74 e locks.. ** Ot
e3940 68 65 72 77 69 73 65 20 66 61 6c 6c 20 62 61 63 herwise fall bac
e3950 6b 20 74 6f 20 74 68 65 20 6e 61 6d 65 64 20 73 k to the named s
e3960 65 6d 61 70 68 6f 72 65 20 6d 65 74 68 6f 64 2e emaphore method.
e3970 0a 20 20 2a 2f 0a 20 20 6c 6f 63 6b 49 6e 66 6f . */. lockInfo
e3980 2e 6c 5f 6c 65 6e 20 3d 20 31 3b 0a 20 20 6c 6f .l_len = 1;. lo
e3990 63 6b 49 6e 66 6f 2e 6c 5f 73 74 61 72 74 20 3d ckInfo.l_start =
e39a0 20 30 3b 0a 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 0;. lockInfo.l
e39b0 5f 77 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f 53 _whence = SEEK_S
e39c0 45 54 3b 0a 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c ET;. lockInfo.l
e39d0 5f 74 79 70 65 20 3d 20 46 5f 52 44 4c 43 4b 3b _type = F_RDLCK;
e39e0 0a 20 20 69 66 28 20 66 63 6e 74 6c 28 70 4e 65 . if( fcntl(pNe
e39f0 77 2d 3e 68 2c 20 46 5f 47 45 54 4c 4b 2c 20 26 w->h, F_GETLK, &
e3a00 6c 6f 63 6b 49 6e 66 6f 29 21 3d 2d 31 20 29 20 lockInfo)!=-1 )
e3a10 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 26 70 6f {. return &po
e3a20 73 69 78 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 20 sixIoMethods;.
e3a30 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 }else{. retur
e3a40 6e 20 26 73 65 6d 49 6f 4d 65 74 68 6f 64 73 3b n &semIoMethods;
e3a50 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 63 6f . }.}.static co
e3a60 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d nst sqlite3_io_m
e3a70 65 74 68 6f 64 73 20 0a 20 20 2a 28 2a 63 6f 6e ethods . *(*con
e3a80 73 74 20 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e st autolockIoFin
e3a90 64 65 72 29 28 63 6f 6e 73 74 20 63 68 61 72 2a der)(const char*
e3aa0 2c 75 6e 69 78 46 69 6c 65 2a 29 20 3d 20 61 75 ,unixFile*) = au
e3ab0 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 49 6d tolockIoFinderIm
e3ac0 70 6c 3b 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 4f pl;..#endif /* O
e3ad0 53 5f 56 58 57 4f 52 4b 53 20 26 26 20 53 51 4c S_VXWORKS && SQL
e3ae0 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 ITE_ENABLE_LOCKI
e3af0 4e 47 5f 53 54 59 4c 45 20 2a 2f 0a 0a 2f 2a 0a NG_STYLE */../*.
e3b00 2a 2a 20 41 6e 20 61 62 73 74 72 61 63 74 20 74 ** An abstract t
e3b10 79 70 65 20 66 6f 72 20 61 20 70 6f 69 6e 74 65 ype for a pointe
e3b20 72 20 74 6f 20 61 20 49 4f 20 6d 65 74 68 6f 64 r to a IO method
e3b30 20 66 69 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e finder function
e3b40 3a 0a 2a 2f 0a 74 79 70 65 64 65 66 20 63 6f 6e :.*/.typedef con
e3b50 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 st sqlite3_io_me
e3b60 74 68 6f 64 73 20 2a 28 2a 66 69 6e 64 65 72 5f thods *(*finder_
e3b70 74 79 70 65 29 28 63 6f 6e 73 74 20 63 68 61 72 type)(const char
e3b80 2a 2c 75 6e 69 78 46 69 6c 65 2a 29 3b 0a 0a 0a *,unixFile*);...
e3b90 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /***************
e3ba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e3bb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e3bc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e3bd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
e3be0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e3bf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 73 71 6c 69 74 ********** sqlit
e3c00 65 33 5f 76 66 73 20 6d 65 74 68 6f 64 73 20 2a e3_vfs methods *
e3c10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e3c20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a ***********.**.*
e3c30 2a 20 54 68 69 73 20 64 69 76 69 73 69 6f 6e 20 * This division
e3c40 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 69 6d 70 contains the imp
e3c50 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 6d lementation of m
e3c60 65 74 68 6f 64 73 20 6f 6e 20 74 68 65 0a 2a 2a ethods on the.**
e3c70 20 73 71 6c 69 74 65 33 5f 76 66 73 20 6f 62 6a sqlite3_vfs obj
e3c80 65 63 74 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 ect..*/../*.** I
e3c90 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 63 6f nitialize the co
e3ca0 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 75 6e ntents of the un
e3cb0 69 78 46 69 6c 65 20 73 74 72 75 63 74 75 72 65 ixFile structure
e3cc0 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 70 pointed to by p
e3cd0 49 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e Id..*/.static in
e3ce0 74 20 66 69 6c 6c 49 6e 55 6e 69 78 46 69 6c 65 t fillInUnixFile
e3cf0 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 (. sqlite3_vfs
e3d00 2a 70 56 66 73 2c 20 20 20 20 20 20 2f 2a 20 50 *pVfs, /* P
e3d10 6f 69 6e 74 65 72 20 74 6f 20 76 66 73 20 6f 62 ointer to vfs ob
e3d20 6a 65 63 74 20 2a 2f 0a 20 20 69 6e 74 20 68 2c ject */. int h,
e3d30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e3d40 20 20 2f 2a 20 4f 70 65 6e 20 66 69 6c 65 20 64 /* Open file d
e3d50 65 73 63 72 69 70 74 6f 72 20 6f 66 20 66 69 6c escriptor of fil
e3d60 65 20 62 65 69 6e 67 20 6f 70 65 6e 65 64 20 2a e being opened *
e3d70 2f 0a 20 20 69 6e 74 20 64 69 72 66 64 2c 20 20 /. int dirfd,
e3d80 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 /* D
e3d90 69 72 65 63 74 6f 72 79 20 66 69 6c 65 20 64 65 irectory file de
e3da0 73 63 72 69 70 74 6f 72 20 2a 2f 0a 20 20 73 71 scriptor */. sq
e3db0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 49 64 2c lite3_file *pId,
e3dc0 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 /* Write t
e3dd0 6f 20 74 68 65 20 75 6e 69 78 46 69 6c 65 20 73 o the unixFile s
e3de0 74 72 75 63 74 75 72 65 20 68 65 72 65 20 2a 2f tructure here */
e3df0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
e3e00 46 69 6c 65 6e 61 6d 65 2c 20 20 2f 2a 20 4e 61 Filename, /* Na
e3e10 6d 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 62 me of the file b
e3e20 65 69 6e 67 20 6f 70 65 6e 65 64 20 2a 2f 0a 20 eing opened */.
e3e30 20 69 6e 74 20 6e 6f 4c 6f 63 6b 2c 20 20 20 20 int noLock,
e3e40 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6d 69 74 /* Omit
e3e50 20 6c 6f 63 6b 69 6e 67 20 69 66 20 74 72 75 65 locking if true
e3e60 20 2a 2f 0a 20 20 69 6e 74 20 69 73 44 65 6c 65 */. int isDele
e3e70 74 65 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a te /*
e3e80 20 44 65 6c 65 74 65 20 6f 6e 20 63 6c 6f 73 65 Delete on close
e3e90 20 69 66 20 74 72 75 65 20 2a 2f 0a 29 7b 0a 20 if true */.){.
e3ea0 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 const sqlite3_i
e3eb0 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 4c 6f 63 6b o_methods *pLock
e3ec0 69 6e 67 53 74 79 6c 65 3b 0a 20 20 75 6e 69 78 ingStyle;. unix
e3ed0 46 69 6c 65 20 2a 70 4e 65 77 20 3d 20 28 75 6e File *pNew = (un
e3ee0 69 78 46 69 6c 65 20 2a 29 70 49 64 3b 0a 20 20 ixFile *)pId;.
e3ef0 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
e3f00 4f 4b 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 OK;.. assert( p
e3f10 4e 65 77 2d 3e 70 4c 6f 63 6b 3d 3d 4e 55 4c 4c New->pLock==NULL
e3f20 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4e );. assert( pN
e3f30 65 77 2d 3e 70 4f 70 65 6e 3d 3d 4e 55 4c 4c 20 ew->pOpen==NULL
e3f40 29 3b 0a 0a 20 20 2f 2a 20 50 61 72 61 6d 65 74 );.. /* Paramet
e3f50 65 72 20 69 73 44 65 6c 65 74 65 20 69 73 20 6f er isDelete is o
e3f60 6e 6c 79 20 75 73 65 64 20 6f 6e 20 76 78 77 6f nly used on vxwo
e3f70 72 6b 73 2e 20 45 78 70 72 65 73 73 20 74 68 69 rks. Express thi
e3f80 73 20 65 78 70 6c 69 63 69 74 6c 79 20 0a 20 20 s explicitly .
e3f90 2a 2a 20 68 65 72 65 20 74 6f 20 70 72 65 76 65 ** here to preve
e3fa0 6e 74 20 63 6f 6d 70 69 6c 65 72 20 77 61 72 6e nt compiler warn
e3fb0 69 6e 67 73 20 61 62 6f 75 74 20 75 6e 75 73 65 ings about unuse
e3fc0 64 20 70 61 72 61 6d 65 74 65 72 73 2e 0a 20 20 d parameters..
e3fd0 2a 2f 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 */. UNUSED_PARA
e3fe0 4d 45 54 45 52 28 69 73 44 65 6c 65 74 65 29 3b METER(isDelete);
e3ff0 0a 0a 20 20 4f 53 54 52 41 43 45 33 28 22 4f 50 .. OSTRACE3("OP
e4000 45 4e 20 20 20 20 25 2d 33 64 20 25 73 5c 6e 22 EN %-3d %s\n"
e4010 2c 20 68 2c 20 7a 46 69 6c 65 6e 61 6d 65 29 3b , h, zFilename);
e4020 20 20 20 20 0a 20 20 70 4e 65 77 2d 3e 68 20 3d . pNew->h =
e4030 20 68 3b 0a 20 20 70 4e 65 77 2d 3e 64 69 72 66 h;. pNew->dirf
e4040 64 20 3d 20 64 69 72 66 64 3b 0a 20 20 53 45 54 d = dirfd;. SET
e4050 5f 54 48 52 45 41 44 49 44 28 70 4e 65 77 29 3b _THREADID(pNew);
e4060 0a 20 20 70 4e 65 77 2d 3e 66 69 6c 65 46 6c 61 . pNew->fileFla
e4070 67 73 20 3d 20 30 3b 0a 0a 23 69 66 20 4f 53 5f gs = 0;..#if OS_
e4080 56 58 57 4f 52 4b 53 0a 20 20 70 4e 65 77 2d 3e VXWORKS. pNew->
e4090 70 49 64 20 3d 20 76 78 77 6f 72 6b 73 46 69 6e pId = vxworksFin
e40a0 64 46 69 6c 65 49 64 28 7a 46 69 6c 65 6e 61 6d dFileId(zFilenam
e40b0 65 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 2d 3e e);. if( pNew->
e40c0 70 49 64 3d 3d 30 20 29 7b 0a 20 20 20 20 6e 6f pId==0 ){. no
e40d0 4c 6f 63 6b 20 3d 20 31 3b 0a 20 20 20 20 72 63 Lock = 1;. rc
e40e0 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b = SQLITE_NOMEM;
e40f0 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 69 . }.#endif.. i
e4100 66 28 20 6e 6f 4c 6f 63 6b 20 29 7b 0a 20 20 20 f( noLock ){.
e4110 20 70 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 20 3d pLockingStyle =
e4120 20 26 6e 6f 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 &nolockIoMethod
e4130 73 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 s;. }else{.
e4140 70 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 20 3d 20 pLockingStyle =
e4150 28 2a 2a 28 66 69 6e 64 65 72 5f 74 79 70 65 2a (**(finder_type*
e4160 29 70 56 66 73 2d 3e 70 41 70 70 44 61 74 61 29 )pVfs->pAppData)
e4170 28 7a 46 69 6c 65 6e 61 6d 65 2c 20 70 4e 65 77 (zFilename, pNew
e4180 29 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e );.#if SQLITE_EN
e4190 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 ABLE_LOCKING_STY
e41a0 4c 45 0a 20 20 20 20 2f 2a 20 43 61 63 68 65 20 LE. /* Cache
e41b0 7a 46 69 6c 65 6e 61 6d 65 20 69 6e 20 74 68 65 zFilename in the
e41c0 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 78 74 locking context
e41d0 20 28 41 46 50 20 61 6e 64 20 64 6f 74 6c 6f 63 (AFP and dotloc
e41e0 6b 20 6f 76 65 72 72 69 64 65 29 20 66 6f 72 0a k override) for.
e41f0 20 20 20 20 2a 2a 20 70 72 6f 78 79 4c 6f 63 6b ** proxyLock
e4200 20 61 63 74 69 76 61 74 69 6f 6e 20 69 73 20 70 activation is p
e4210 6f 73 73 69 62 6c 65 20 28 72 65 6d 6f 74 65 20 ossible (remote
e4220 70 72 6f 78 79 20 69 73 20 62 61 73 65 64 20 6f proxy is based o
e4230 6e 20 64 62 20 6e 61 6d 65 29 0a 20 20 20 20 2a n db name). *
e4240 2a 20 7a 46 69 6c 65 6e 61 6d 65 20 72 65 6d 61 * zFilename rema
e4250 69 6e 73 20 76 61 6c 69 64 20 75 6e 74 69 6c 20 ins valid until
e4260 66 69 6c 65 20 69 73 20 63 6c 6f 73 65 64 2c 20 file is closed,
e4270 74 6f 20 73 75 70 70 6f 72 74 20 2a 2f 0a 20 20 to support */.
e4280 20 20 70 4e 65 77 2d 3e 6c 6f 63 6b 69 6e 67 43 pNew->lockingC
e4290 6f 6e 74 65 78 74 20 3d 20 28 76 6f 69 64 2a 29 ontext = (void*)
e42a0 7a 46 69 6c 65 6e 61 6d 65 3b 0a 23 65 6e 64 69 zFilename;.#endi
e42b0 66 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 4c 6f f. }.. if( pLo
e42c0 63 6b 69 6e 67 53 74 79 6c 65 20 3d 3d 20 26 70 ckingStyle == &p
e42d0 6f 73 69 78 49 6f 4d 65 74 68 6f 64 73 20 29 7b osixIoMethods ){
e42e0 0a 20 20 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 . unixEnterMu
e42f0 74 65 78 28 29 3b 0a 20 20 20 20 72 63 20 3d 20 tex();. rc =
e4300 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 70 4e 65 findLockInfo(pNe
e4310 77 2c 20 26 70 4e 65 77 2d 3e 70 4c 6f 63 6b 2c w, &pNew->pLock,
e4320 20 26 70 4e 65 77 2d 3e 70 4f 70 65 6e 29 3b 0a &pNew->pOpen);.
e4330 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
e4340 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 2f TE_OK ){. /
e4350 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 * If an error oc
e4360 63 75 72 65 64 20 69 6e 20 66 69 6e 64 4c 6f 63 cured in findLoc
e4370 6b 49 6e 66 6f 28 29 2c 20 63 6c 6f 73 65 20 74 kInfo(), close t
e4380 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 he file descript
e4390 6f 72 0a 20 20 20 20 20 20 2a 2a 20 69 6d 6d 65 or. ** imme
e43a0 64 69 61 74 65 6c 79 2c 20 62 65 66 6f 72 65 20 diately, before
e43b0 72 65 6c 65 61 73 69 6e 67 20 74 68 65 20 6d 75 releasing the mu
e43c0 74 65 78 2e 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 tex. findLockInf
e43d0 6f 28 29 20 6d 61 79 20 66 61 69 6c 0a 20 20 20 o() may fail.
e43e0 20 20 20 2a 2a 20 69 6e 20 74 77 6f 20 73 63 65 ** in two sce
e43f0 6e 61 72 69 6f 73 3a 0a 20 20 20 20 20 20 2a 2a narios:. **
e4400 0a 20 20 20 20 20 20 2a 2a 20 20 20 28 61 29 20 . ** (a)
e4410 41 20 63 61 6c 6c 20 74 6f 20 66 73 74 61 74 28 A call to fstat(
e4420 29 20 66 61 69 6c 65 64 2e 0a 20 20 20 20 20 20 ) failed..
e4430 2a 2a 20 20 20 28 62 29 20 41 20 6d 61 6c 6c 6f ** (b) A mallo
e4440 63 20 66 61 69 6c 65 64 2e 0a 20 20 20 20 20 20 c failed..
e4450 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 53 63 65 6e **. ** Scen
e4460 61 72 69 6f 20 28 62 29 20 6d 61 79 20 6f 6e 6c ario (b) may onl
e4470 79 20 6f 63 63 75 72 20 69 66 20 74 68 65 20 70 y occur if the p
e4480 72 6f 63 65 73 73 20 69 73 20 68 6f 6c 64 69 6e rocess is holdin
e4490 67 20 6e 6f 20 6f 74 68 65 72 0a 20 20 20 20 20 g no other.
e44a0 20 2a 2a 20 66 69 6c 65 20 64 65 73 63 72 69 70 ** file descrip
e44b0 74 6f 72 73 20 6f 70 65 6e 20 6f 6e 20 74 68 65 tors open on the
e44c0 20 73 61 6d 65 20 66 69 6c 65 2e 20 49 66 20 74 same file. If t
e44d0 68 65 72 65 20 77 65 72 65 20 6f 74 68 65 72 20 here were other
e44e0 66 69 6c 65 0a 20 20 20 20 20 20 2a 2a 20 64 65 file. ** de
e44f0 73 63 72 69 70 74 6f 72 73 20 6f 6e 20 74 68 69 scriptors on thi
e4500 73 20 66 69 6c 65 2c 20 74 68 65 6e 20 6e 6f 20 s file, then no
e4510 6d 61 6c 6c 6f 63 20 77 6f 75 6c 64 20 62 65 20 malloc would be
e4520 72 65 71 75 69 72 65 64 20 62 79 0a 20 20 20 20 required by.
e4530 20 20 2a 2a 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 ** findLockInf
e4540 6f 28 29 2e 20 49 66 20 74 68 69 73 20 69 73 20 o(). If this is
e4550 74 68 65 20 63 61 73 65 2c 20 69 74 20 69 73 20 the case, it is
e4560 71 75 69 74 65 20 73 61 66 65 20 74 6f 20 63 6c quite safe to cl
e4570 6f 73 65 0a 20 20 20 20 20 20 2a 2a 20 68 61 6e ose. ** han
e4580 64 6c 65 20 68 20 2d 20 61 73 20 69 74 20 69 73 dle h - as it is
e4590 20 67 75 61 72 61 6e 74 65 65 64 20 74 68 61 74 guaranteed that
e45a0 20 6e 6f 20 70 6f 73 69 78 20 6c 6f 63 6b 73 20 no posix locks
e45b0 77 69 6c 6c 20 62 65 20 72 65 6c 65 61 73 65 64 will be released
e45c0 0a 20 20 20 20 20 20 2a 2a 20 62 79 20 64 6f 69 . ** by doi
e45d0 6e 67 20 73 6f 2e 0a 20 20 20 20 20 20 2a 2a 0a ng so.. **.
e45e0 20 20 20 20 20 20 2a 2a 20 49 66 20 73 63 65 6e ** If scen
e45f0 61 72 69 6f 20 28 61 29 20 63 61 75 73 65 64 20 ario (a) caused
e4600 74 68 65 20 65 72 72 6f 72 20 74 68 65 6e 20 74 the error then t
e4610 68 69 6e 67 73 20 61 72 65 20 6e 6f 74 20 73 6f hings are not so
e4620 20 73 61 66 65 2e 20 54 68 65 0a 20 20 20 20 20 safe. The.
e4630 20 2a 2a 20 69 6d 70 6c 69 63 69 74 20 61 73 73 ** implicit ass
e4640 75 6d 70 74 69 6f 6e 20 68 65 72 65 20 69 73 20 umption here is
e4650 74 68 61 74 20 69 66 20 66 73 74 61 74 28 29 20 that if fstat()
e4660 66 61 69 6c 73 2c 20 74 68 69 6e 67 73 20 61 72 fails, things ar
e4670 65 20 69 6e 0a 20 20 20 20 20 20 2a 2a 20 73 75 e in. ** su
e4680 63 68 20 62 61 64 20 73 68 61 70 65 20 74 68 61 ch bad shape tha
e4690 74 20 64 72 6f 70 70 69 6e 67 20 61 20 6c 6f 63 t dropping a loc
e46a0 6b 20 6f 72 20 74 77 6f 20 64 6f 65 73 6e 27 74 k or two doesn't
e46b0 20 6d 61 74 74 65 72 20 6d 75 63 68 2e 0a 20 20 matter much..
e46c0 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 63 6c 6f */. clo
e46d0 73 65 28 68 29 3b 0a 20 20 20 20 20 20 68 20 3d se(h);. h =
e46e0 20 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 75 -1;. }. u
e46f0 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b nixLeaveMutex();
e4700 0a 20 20 7d 0a 0a 23 69 66 20 53 51 4c 49 54 45 . }..#if SQLITE
e4710 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f _ENABLE_LOCKING_
e4720 53 54 59 4c 45 20 26 26 20 64 65 66 69 6e 65 64 STYLE && defined
e4730 28 5f 5f 41 50 50 4c 45 5f 5f 29 0a 20 20 65 6c (__APPLE__). el
e4740 73 65 20 69 66 28 20 70 4c 6f 63 6b 69 6e 67 53 se if( pLockingS
e4750 74 79 6c 65 20 3d 3d 20 26 61 66 70 49 6f 4d 65 tyle == &afpIoMe
e4760 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 2f 2a 20 thods ){. /*
e4770 41 46 50 20 6c 6f 63 6b 69 6e 67 20 75 73 65 73 AFP locking uses
e4780 20 74 68 65 20 66 69 6c 65 20 70 61 74 68 20 73 the file path s
e4790 6f 20 69 74 20 6e 65 65 64 73 20 74 6f 20 62 65 o it needs to be
e47a0 20 69 6e 63 6c 75 64 65 64 20 69 6e 0a 20 20 20 included in.
e47b0 20 2a 2a 20 74 68 65 20 61 66 70 4c 6f 63 6b 69 ** the afpLocki
e47c0 6e 67 43 6f 6e 74 65 78 74 2e 0a 20 20 20 20 2a ngContext.. *
e47d0 2f 0a 20 20 20 20 61 66 70 4c 6f 63 6b 69 6e 67 /. afpLocking
e47e0 43 6f 6e 74 65 78 74 20 2a 70 43 74 78 3b 0a 20 Context *pCtx;.
e47f0 20 20 20 70 4e 65 77 2d 3e 6c 6f 63 6b 69 6e 67 pNew->locking
e4800 43 6f 6e 74 65 78 74 20 3d 20 70 43 74 78 20 3d Context = pCtx =
e4810 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 sqlite3_malloc(
e4820 20 73 69 7a 65 6f 66 28 2a 70 43 74 78 29 20 29 sizeof(*pCtx) )
e4830 3b 0a 20 20 20 20 69 66 28 20 70 43 74 78 3d 3d ;. if( pCtx==
e4840 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 0 ){. rc =
e4850 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
e4860 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f }else{. /
e4870 2a 20 4e 42 3a 20 7a 46 69 6c 65 6e 61 6d 65 20 * NB: zFilename
e4880 65 78 69 73 74 73 20 61 6e 64 20 72 65 6d 61 69 exists and remai
e4890 6e 73 20 76 61 6c 69 64 20 75 6e 74 69 6c 20 74 ns valid until t
e48a0 68 65 20 66 69 6c 65 20 69 73 20 63 6c 6f 73 65 he file is close
e48b0 64 0a 20 20 20 20 20 20 2a 2a 20 61 63 63 6f 72 d. ** accor
e48c0 64 69 6e 67 20 74 6f 20 72 65 71 75 69 72 65 6d ding to requirem
e48d0 65 6e 74 20 46 31 31 31 34 31 2e 20 20 53 6f 20 ent F11141. So
e48e0 77 65 20 64 6f 20 6e 6f 74 20 6e 65 65 64 20 74 we do not need t
e48f0 6f 20 6d 61 6b 65 20 61 0a 20 20 20 20 20 20 2a o make a. *
e4900 2a 20 63 6f 70 79 20 6f 66 20 74 68 65 20 66 69 * copy of the fi
e4910 6c 65 6e 61 6d 65 2e 20 2a 2f 0a 20 20 20 20 20 lename. */.
e4920 20 70 43 74 78 2d 3e 64 62 50 61 74 68 20 3d 20 pCtx->dbPath =
e4930 7a 46 69 6c 65 6e 61 6d 65 3b 0a 20 20 20 20 20 zFilename;.
e4940 20 73 72 61 6e 64 6f 6d 64 65 76 28 29 3b 0a 20 srandomdev();.
e4950 20 20 20 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 unixEnterMu
e4960 74 65 78 28 29 3b 0a 20 20 20 20 20 20 72 63 20 tex();. rc
e4970 3d 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 70 = findLockInfo(p
e4980 4e 65 77 2c 20 4e 55 4c 4c 2c 20 26 70 4e 65 77 New, NULL, &pNew
e4990 2d 3e 70 4f 70 65 6e 29 3b 0a 20 20 20 20 20 20 ->pOpen);.
e49a0 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 unixLeaveMutex()
e49b0 3b 20 20 20 20 20 20 20 20 0a 20 20 20 20 7d 0a ; . }.
e49c0 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 65 6c }.#endif.. el
e49d0 73 65 20 69 66 28 20 70 4c 6f 63 6b 69 6e 67 53 se if( pLockingS
e49e0 74 79 6c 65 20 3d 3d 20 26 64 6f 74 6c 6f 63 6b tyle == &dotlock
e49f0 49 6f 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 IoMethods ){.
e4a00 20 2f 2a 20 44 6f 74 66 69 6c 65 20 6c 6f 63 6b /* Dotfile lock
e4a10 69 6e 67 20 75 73 65 73 20 74 68 65 20 66 69 6c ing uses the fil
e4a20 65 20 70 61 74 68 20 73 6f 20 69 74 20 6e 65 65 e path so it nee
e4a30 64 73 20 74 6f 20 62 65 20 69 6e 63 6c 75 64 65 ds to be include
e4a40 64 20 69 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20 d in. ** the
e4a50 64 6f 74 6c 6f 63 6b 4c 6f 63 6b 69 6e 67 43 6f dotlockLockingCo
e4a60 6e 74 65 78 74 20 0a 20 20 20 20 2a 2f 0a 20 20 ntext . */.
e4a70 20 20 63 68 61 72 20 2a 7a 4c 6f 63 6b 46 69 6c char *zLockFil
e4a80 65 3b 0a 20 20 20 20 69 6e 74 20 6e 46 69 6c 65 e;. int nFile
e4a90 6e 61 6d 65 3b 0a 20 20 20 20 6e 46 69 6c 65 6e name;. nFilen
e4aa0 61 6d 65 20 3d 20 28 69 6e 74 29 73 74 72 6c 65 ame = (int)strle
e4ab0 6e 28 7a 46 69 6c 65 6e 61 6d 65 29 20 2b 20 36 n(zFilename) + 6
e4ac0 3b 0a 20 20 20 20 7a 4c 6f 63 6b 46 69 6c 65 20 ;. zLockFile
e4ad0 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 = (char *)sqlite
e4ae0 33 5f 6d 61 6c 6c 6f 63 28 6e 46 69 6c 65 6e 61 3_malloc(nFilena
e4af0 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 7a 4c 6f me);. if( zLo
e4b00 63 6b 46 69 6c 65 3d 3d 30 20 29 7b 0a 20 20 20 ckFile==0 ){.
e4b10 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e rc = SQLITE_N
e4b20 4f 4d 45 4d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b OMEM;. }else{
e4b30 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 . sqlite3_s
e4b40 6e 70 72 69 6e 74 66 28 6e 46 69 6c 65 6e 61 6d nprintf(nFilenam
e4b50 65 2c 20 7a 4c 6f 63 6b 46 69 6c 65 2c 20 22 25 e, zLockFile, "%
e4b60 73 22 20 44 4f 54 4c 4f 43 4b 5f 53 55 46 46 49 s" DOTLOCK_SUFFI
e4b70 58 2c 20 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 X, zFilename);.
e4b80 20 20 20 7d 0a 20 20 20 20 70 4e 65 77 2d 3e 6c }. pNew->l
e4b90 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 3d 20 ockingContext =
e4ba0 7a 4c 6f 63 6b 46 69 6c 65 3b 0a 20 20 7d 0a 0a zLockFile;. }..
e4bb0 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 #if OS_VXWORKS.
e4bc0 20 65 6c 73 65 20 69 66 28 20 70 4c 6f 63 6b 69 else if( pLocki
e4bd0 6e 67 53 74 79 6c 65 20 3d 3d 20 26 73 65 6d 49 ngStyle == &semI
e4be0 6f 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 oMethods ){.
e4bf0 2f 2a 20 4e 61 6d 65 64 20 73 65 6d 61 70 68 6f /* Named semapho
e4c00 72 65 20 6c 6f 63 6b 69 6e 67 20 75 73 65 73 20 re locking uses
e4c10 74 68 65 20 66 69 6c 65 20 70 61 74 68 20 73 6f the file path so
e4c20 20 69 74 20 6e 65 65 64 73 20 74 6f 20 62 65 0a it needs to be.
e4c30 20 20 20 20 2a 2a 20 69 6e 63 6c 75 64 65 64 20 ** included
e4c40 69 6e 20 74 68 65 20 73 65 6d 4c 6f 63 6b 69 6e in the semLockin
e4c50 67 43 6f 6e 74 65 78 74 0a 20 20 20 20 2a 2f 0a gContext. */.
e4c60 20 20 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 unixEnterMut
e4c70 65 78 28 29 3b 0a 20 20 20 20 72 63 20 3d 20 66 ex();. rc = f
e4c80 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 70 4e 65 77 indLockInfo(pNew
e4c90 2c 20 26 70 4e 65 77 2d 3e 70 4c 6f 63 6b 2c 20 , &pNew->pLock,
e4ca0 26 70 4e 65 77 2d 3e 70 4f 70 65 6e 29 3b 0a 20 &pNew->pOpen);.
e4cb0 20 20 20 69 66 28 20 28 72 63 3d 3d 53 51 4c 49 if( (rc==SQLI
e4cc0 54 45 5f 4f 4b 29 20 26 26 20 28 70 4e 65 77 2d TE_OK) && (pNew-
e4cd0 3e 70 4f 70 65 6e 2d 3e 70 53 65 6d 3d 3d 4e 55 >pOpen->pSem==NU
e4ce0 4c 4c 29 20 29 7b 0a 20 20 20 20 20 20 63 68 61 LL) ){. cha
e4cf0 72 20 2a 7a 53 65 6d 4e 61 6d 65 20 3d 20 70 4e r *zSemName = pN
e4d00 65 77 2d 3e 70 4f 70 65 6e 2d 3e 61 53 65 6d 4e ew->pOpen->aSemN
e4d10 61 6d 65 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e ame;. int n
e4d20 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f ;. sqlite3_
e4d30 73 6e 70 72 69 6e 74 66 28 4d 41 58 5f 50 41 54 snprintf(MAX_PAT
e4d40 48 4e 41 4d 45 2c 20 7a 53 65 6d 4e 61 6d 65 2c HNAME, zSemName,
e4d50 20 22 2f 25 73 2e 73 65 6d 22 2c 0a 20 20 20 20 "/%s.sem",.
e4d60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e4d70 20 20 20 70 4e 65 77 2d 3e 70 49 64 2d 3e 7a 43 pNew->pId->zC
e4d80 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 29 3b 0a 20 anonicalName);.
e4d90 20 20 20 20 20 66 6f 72 28 20 6e 3d 31 3b 20 7a for( n=1; z
e4da0 53 65 6d 4e 61 6d 65 5b 6e 5d 3b 20 6e 2b 2b 20 SemName[n]; n++
e4db0 29 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a 53 ). if( zS
e4dc0 65 6d 4e 61 6d 65 5b 6e 5d 3d 3d 27 2f 27 20 29 emName[n]=='/' )
e4dd0 20 7a 53 65 6d 4e 61 6d 65 5b 6e 5d 20 3d 20 27 zSemName[n] = '
e4de0 5f 27 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e _';. pNew->
e4df0 70 4f 70 65 6e 2d 3e 70 53 65 6d 20 3d 20 73 65 pOpen->pSem = se
e4e00 6d 5f 6f 70 65 6e 28 7a 53 65 6d 4e 61 6d 65 2c m_open(zSemName,
e4e10 20 4f 5f 43 52 45 41 54 2c 20 30 36 36 36 2c 20 O_CREAT, 0666,
e4e20 31 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4e 1);. if( pN
e4e30 65 77 2d 3e 70 4f 70 65 6e 2d 3e 70 53 65 6d 20 ew->pOpen->pSem
e4e40 3d 3d 20 53 45 4d 5f 46 41 49 4c 45 44 20 29 7b == SEM_FAILED ){
e4e50 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 . rc = SQ
e4e60 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 LITE_NOMEM;.
e4e70 20 20 20 20 70 4e 65 77 2d 3e 70 4f 70 65 6e 2d pNew->pOpen-
e4e80 3e 61 53 65 6d 4e 61 6d 65 5b 30 5d 20 3d 20 27 >aSemName[0] = '
e4e90 5c 30 27 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 \0';. }.
e4ea0 20 7d 0a 20 20 20 20 75 6e 69 78 4c 65 61 76 65 }. unixLeave
e4eb0 4d 75 74 65 78 28 29 3b 0a 20 20 7d 0a 23 65 6e Mutex();. }.#en
e4ec0 64 69 66 0a 20 20 0a 20 20 70 4e 65 77 2d 3e 6c dif. . pNew->l
e4ed0 61 73 74 45 72 72 6e 6f 20 3d 20 30 3b 0a 23 69 astErrno = 0;.#i
e4ee0 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 69 f OS_VXWORKS. i
e4ef0 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
e4f00 20 29 7b 0a 20 20 20 20 75 6e 6c 69 6e 6b 28 7a ){. unlink(z
e4f10 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 69 Filename);. i
e4f20 73 44 65 6c 65 74 65 20 3d 20 30 3b 0a 20 20 7d sDelete = 0;. }
e4f30 0a 20 20 70 4e 65 77 2d 3e 69 73 44 65 6c 65 74 . pNew->isDelet
e4f40 65 20 3d 20 69 73 44 65 6c 65 74 65 3b 0a 23 65 e = isDelete;.#e
e4f50 6e 64 69 66 0a 20 20 69 66 28 20 72 63 21 3d 53 ndif. if( rc!=S
e4f60 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
e4f70 69 66 28 20 64 69 72 66 64 3e 3d 30 20 29 20 63 if( dirfd>=0 ) c
e4f80 6c 6f 73 65 28 64 69 72 66 64 29 3b 20 2f 2a 20 lose(dirfd); /*
e4f90 73 69 6c 65 6e 74 20 6c 65 61 6b 20 69 66 20 66 silent leak if f
e4fa0 61 69 6c 2c 20 61 6c 72 65 61 64 79 20 69 6e 20 ail, already in
e4fb0 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20 69 66 28 error */. if(
e4fc0 20 68 3e 3d 30 20 29 20 63 6c 6f 73 65 28 68 29 h>=0 ) close(h)
e4fd0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 ;. }else{. p
e4fe0 4e 65 77 2d 3e 70 4d 65 74 68 6f 64 20 3d 20 70 New->pMethod = p
e4ff0 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 3b 0a 20 20 LockingStyle;.
e5000 20 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 2b 31 OpenCounter(+1
e5010 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
e5020 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 rc;.}../*.** Ope
e5030 6e 20 61 20 66 69 6c 65 20 64 65 73 63 72 69 70 n a file descrip
e5040 74 6f 72 20 74 6f 20 74 68 65 20 64 69 72 65 63 tor to the direc
e5050 74 6f 72 79 20 63 6f 6e 74 61 69 6e 69 6e 67 20 tory containing
e5060 66 69 6c 65 20 7a 46 69 6c 65 6e 61 6d 65 2e 0a file zFilename..
e5070 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66 75 6c ** If successful
e5080 2c 20 2a 70 46 64 20 69 73 20 73 65 74 20 74 6f , *pFd is set to
e5090 20 74 68 65 20 6f 70 65 6e 65 64 20 66 69 6c 65 the opened file
e50a0 20 64 65 73 63 72 69 70 74 6f 72 20 61 6e 64 0a descriptor and.
e50b0 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 ** SQLITE_OK is
e50c0 72 65 74 75 72 6e 65 64 2e 20 49 66 20 61 6e 20 returned. If an
e50d0 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 65 69 error occurs, ei
e50e0 74 68 65 72 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 ther SQLITE_NOME
e50f0 4d 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f 43 M.** or SQLITE_C
e5100 41 4e 54 4f 50 45 4e 20 69 73 20 72 65 74 75 72 ANTOPEN is retur
e5110 6e 65 64 20 61 6e 64 20 2a 70 46 64 20 69 73 20 ned and *pFd is
e5120 73 65 74 20 74 6f 20 61 6e 20 75 6e 64 65 66 69 set to an undefi
e5130 6e 65 64 0a 2a 2a 20 76 61 6c 75 65 2e 0a 2a 2a ned.** value..**
e5140 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f 4f 4b .** If SQLITE_OK
e5150 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20 74 68 is returned, th
e5160 65 20 63 61 6c 6c 65 72 20 69 73 20 72 65 73 70 e caller is resp
e5170 6f 6e 73 69 62 6c 65 20 66 6f 72 20 63 6c 6f 73 onsible for clos
e5180 69 6e 67 0a 2a 2a 20 74 68 65 20 66 69 6c 65 20 ing.** the file
e5190 64 65 73 63 72 69 70 74 6f 72 20 2a 70 46 64 20 descriptor *pFd
e51a0 75 73 69 6e 67 20 63 6c 6f 73 65 28 29 2e 0a 2a using close()..*
e51b0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 70 65 /.static int ope
e51c0 6e 44 69 72 65 63 74 6f 72 79 28 63 6f 6e 73 74 nDirectory(const
e51d0 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 char *zFilename
e51e0 2c 20 69 6e 74 20 2a 70 46 64 29 7b 0a 20 20 69 , int *pFd){. i
e51f0 6e 74 20 69 69 3b 0a 20 20 69 6e 74 20 66 64 20 nt ii;. int fd
e5200 3d 20 2d 31 3b 0a 20 20 63 68 61 72 20 7a 44 69 = -1;. char zDi
e5210 72 6e 61 6d 65 5b 4d 41 58 5f 50 41 54 48 4e 41 rname[MAX_PATHNA
e5220 4d 45 2b 31 5d 3b 0a 0a 20 20 73 71 6c 69 74 65 ME+1];.. sqlite
e5230 33 5f 73 6e 70 72 69 6e 74 66 28 4d 41 58 5f 50 3_snprintf(MAX_P
e5240 41 54 48 4e 41 4d 45 2c 20 7a 44 69 72 6e 61 6d ATHNAME, zDirnam
e5250 65 2c 20 22 25 73 22 2c 20 7a 46 69 6c 65 6e 61 e, "%s", zFilena
e5260 6d 65 29 3b 0a 20 20 66 6f 72 28 69 69 3d 28 69 me);. for(ii=(i
e5270 6e 74 29 73 74 72 6c 65 6e 28 7a 44 69 72 6e 61 nt)strlen(zDirna
e5280 6d 65 29 3b 20 69 69 3e 31 20 26 26 20 7a 44 69 me); ii>1 && zDi
e5290 72 6e 61 6d 65 5b 69 69 5d 21 3d 27 2f 27 3b 20 rname[ii]!='/';
e52a0 69 69 2d 2d 29 3b 0a 20 20 69 66 28 20 69 69 3e ii--);. if( ii>
e52b0 30 20 29 7b 0a 20 20 20 20 7a 44 69 72 6e 61 6d 0 ){. zDirnam
e52c0 65 5b 69 69 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 e[ii] = '\0';.
e52d0 20 20 66 64 20 3d 20 6f 70 65 6e 28 7a 44 69 72 fd = open(zDir
e52e0 6e 61 6d 65 2c 20 4f 5f 52 44 4f 4e 4c 59 7c 4f name, O_RDONLY|O
e52f0 5f 42 49 4e 41 52 59 2c 20 30 29 3b 0a 20 20 20 _BINARY, 0);.
e5300 20 69 66 28 20 66 64 3e 3d 30 20 29 7b 0a 23 69 if( fd>=0 ){.#i
e5310 66 64 65 66 20 46 44 5f 43 4c 4f 45 58 45 43 0a fdef FD_CLOEXEC.
e5320 20 20 20 20 20 20 66 63 6e 74 6c 28 66 64 2c 20 fcntl(fd,
e5330 46 5f 53 45 54 46 44 2c 20 66 63 6e 74 6c 28 66 F_SETFD, fcntl(f
e5340 64 2c 20 46 5f 47 45 54 46 44 2c 20 30 29 20 7c d, F_GETFD, 0) |
e5350 20 46 44 5f 43 4c 4f 45 58 45 43 29 3b 0a 23 65 FD_CLOEXEC);.#e
e5360 6e 64 69 66 0a 20 20 20 20 20 20 4f 53 54 52 41 ndif. OSTRA
e5370 43 45 33 28 22 4f 50 45 4e 44 49 52 20 25 2d 33 CE3("OPENDIR %-3
e5380 64 20 25 73 5c 6e 22 2c 20 66 64 2c 20 7a 44 69 d %s\n", fd, zDi
e5390 72 6e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 20 20 rname);. }.
e53a0 7d 0a 20 20 2a 70 46 64 20 3d 20 66 64 3b 0a 20 }. *pFd = fd;.
e53b0 20 72 65 74 75 72 6e 20 28 66 64 3e 3d 30 3f 53 return (fd>=0?S
e53c0 51 4c 49 54 45 5f 4f 4b 3a 53 51 4c 49 54 45 5f QLITE_OK:SQLITE_
e53d0 43 41 4e 54 4f 50 45 4e 29 3b 0a 7d 0a 0a 2f 2a CANTOPEN);.}../*
e53e0 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 74 65 6d .** Create a tem
e53f0 70 6f 72 61 72 79 20 66 69 6c 65 20 6e 61 6d 65 porary file name
e5400 20 69 6e 20 7a 42 75 66 2e 20 20 7a 42 75 66 20 in zBuf. zBuf
e5410 6d 75 73 74 20 62 65 20 61 6c 6c 6f 63 61 74 65 must be allocate
e5420 64 0a 2a 2a 20 62 79 20 74 68 65 20 63 61 6c 6c d.** by the call
e5430 69 6e 67 20 70 72 6f 63 65 73 73 20 61 6e 64 20 ing process and
e5440 6d 75 73 74 20 62 65 20 62 69 67 20 65 6e 6f 75 must be big enou
e5450 67 68 20 74 6f 20 68 6f 6c 64 20 61 74 20 6c 65 gh to hold at le
e5460 61 73 74 0a 2a 2a 20 70 56 66 73 2d 3e 6d 78 50 ast.** pVfs->mxP
e5470 61 74 68 6e 61 6d 65 20 62 79 74 65 73 2e 0a 2a athname bytes..*
e5480 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74 /.static int get
e5490 54 65 6d 70 6e 61 6d 65 28 69 6e 74 20 6e 42 75 Tempname(int nBu
e54a0 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a f, char *zBuf){.
e54b0 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 static const c
e54c0 68 61 72 20 2a 61 7a 44 69 72 73 5b 5d 20 3d 20 har *azDirs[] =
e54d0 7b 0a 20 20 20 20 20 30 2c 0a 20 20 20 20 20 30 {. 0,. 0
e54e0 2c 0a 20 20 20 20 20 22 2f 76 61 72 2f 74 6d 70 ,. "/var/tmp
e54f0 22 2c 0a 20 20 20 20 20 22 2f 75 73 72 2f 74 6d ",. "/usr/tm
e5500 70 22 2c 0a 20 20 20 20 20 22 2f 74 6d 70 22 2c p",. "/tmp",
e5510 0a 20 20 20 20 20 22 2e 22 2c 0a 20 20 7d 3b 0a . ".",. };.
e5520 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 static const u
e5530 6e 73 69 67 6e 65 64 20 63 68 61 72 20 7a 43 68 nsigned char zCh
e5540 61 72 73 5b 5d 20 3d 0a 20 20 20 20 22 61 62 63 ars[] =. "abc
e5550 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 71 72 73 defghijklmnopqrs
e5560 74 75 76 77 78 79 7a 22 0a 20 20 20 20 22 41 42 tuvwxyz". "AB
e5570 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50 51 52 CDEFGHIJKLMNOPQR
e5580 53 54 55 56 57 58 59 5a 22 0a 20 20 20 20 22 30 STUVWXYZ". "0
e5590 31 32 33 34 35 36 37 38 39 22 3b 0a 20 20 75 6e 123456789";. un
e55a0 73 69 67 6e 65 64 20 69 6e 74 20 69 2c 20 6a 3b signed int i, j;
e55b0 0a 20 20 73 74 72 75 63 74 20 73 74 61 74 20 62 . struct stat b
e55c0 75 66 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 uf;. const char
e55d0 20 2a 7a 44 69 72 20 3d 20 22 2e 22 3b 0a 0a 20 *zDir = ".";..
e55e0 20 2f 2a 20 49 74 27 73 20 6f 64 64 20 74 6f 20 /* It's odd to
e55f0 73 69 6d 75 6c 61 74 65 20 61 6e 20 69 6f 2d 65 simulate an io-e
e5600 72 72 6f 72 20 68 65 72 65 2c 20 62 75 74 20 72 rror here, but r
e5610 65 61 6c 6c 79 20 74 68 69 73 20 69 73 20 6a 75 eally this is ju
e5620 73 74 0a 20 20 2a 2a 20 75 73 69 6e 67 20 74 68 st. ** using th
e5630 65 20 69 6f 2d 65 72 72 6f 72 20 69 6e 66 72 61 e io-error infra
e5640 73 74 72 75 63 74 75 72 65 20 74 6f 20 74 65 73 structure to tes
e5650 74 20 74 68 61 74 20 53 51 4c 69 74 65 20 68 61 t that SQLite ha
e5660 6e 64 6c 65 73 20 74 68 69 73 0a 20 20 2a 2a 20 ndles this. **
e5670 66 75 6e 63 74 69 6f 6e 20 66 61 69 6c 69 6e 67 function failing
e5680 2e 20 0a 20 20 2a 2f 0a 20 20 53 69 6d 75 6c 61 . . */. Simula
e5690 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72 teIOError( retur
e56a0 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 29 n SQLITE_IOERR )
e56b0 3b 0a 0a 20 20 61 7a 44 69 72 73 5b 30 5d 20 3d ;.. azDirs[0] =
e56c0 20 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 sqlite3_temp_di
e56d0 72 65 63 74 6f 72 79 3b 0a 20 20 69 66 20 28 4e rectory;. if (N
e56e0 55 4c 4c 20 3d 3d 20 61 7a 44 69 72 73 5b 31 5d ULL == azDirs[1]
e56f0 29 20 7b 0a 20 20 20 20 61 7a 44 69 72 73 5b 31 ) {. azDirs[1
e5700 5d 20 3d 20 67 65 74 65 6e 76 28 22 54 4d 50 44 ] = getenv("TMPD
e5710 49 52 22 29 3b 0a 20 20 7d 0a 20 20 0a 20 20 66 IR");. }. . f
e5720 6f 72 28 69 3d 30 3b 20 69 3c 73 69 7a 65 6f 66 or(i=0; i<sizeof
e5730 28 61 7a 44 69 72 73 29 2f 73 69 7a 65 6f 66 28 (azDirs)/sizeof(
e5740 61 7a 44 69 72 73 5b 30 5d 29 3b 20 69 2b 2b 29 azDirs[0]); i++)
e5750 7b 0a 20 20 20 20 69 66 28 20 61 7a 44 69 72 73 {. if( azDirs
e5760 5b 69 5d 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 [i]==0 ) continu
e5770 65 3b 0a 20 20 20 20 69 66 28 20 73 74 61 74 28 e;. if( stat(
e5780 61 7a 44 69 72 73 5b 69 5d 2c 20 26 62 75 66 29 azDirs[i], &buf)
e5790 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 ) continue;.
e57a0 20 69 66 28 20 21 53 5f 49 53 44 49 52 28 62 75 if( !S_ISDIR(bu
e57b0 66 2e 73 74 5f 6d 6f 64 65 29 20 29 20 63 6f 6e f.st_mode) ) con
e57c0 74 69 6e 75 65 3b 0a 20 20 20 20 69 66 28 20 61 tinue;. if( a
e57d0 63 63 65 73 73 28 61 7a 44 69 72 73 5b 69 5d 2c ccess(azDirs[i],
e57e0 20 30 37 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 07) ) continue;
e57f0 0a 20 20 20 20 7a 44 69 72 20 3d 20 61 7a 44 69 . zDir = azDi
e5800 72 73 5b 69 5d 3b 0a 20 20 20 20 62 72 65 61 6b rs[i];. break
e5810 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 ;. }.. /* Chec
e5820 6b 20 74 68 61 74 20 74 68 65 20 6f 75 74 70 75 k that the outpu
e5830 74 20 62 75 66 66 65 72 20 69 73 20 6c 61 72 67 t buffer is larg
e5840 65 20 65 6e 6f 75 67 68 20 66 6f 72 20 74 68 65 e enough for the
e5850 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 temporary file
e5860 0a 20 20 2a 2a 20 6e 61 6d 65 2e 20 49 66 20 69 . ** name. If i
e5870 74 20 69 73 20 6e 6f 74 2c 20 72 65 74 75 72 6e t is not, return
e5880 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e 0a 20 SQLITE_ERROR..
e5890 20 2a 2f 0a 20 20 69 66 28 20 28 73 74 72 6c 65 */. if( (strle
e58a0 6e 28 7a 44 69 72 29 20 2b 20 73 74 72 6c 65 6e n(zDir) + strlen
e58b0 28 53 51 4c 49 54 45 5f 54 45 4d 50 5f 46 49 4c (SQLITE_TEMP_FIL
e58c0 45 5f 50 52 45 46 49 58 29 20 2b 20 31 37 29 20 E_PREFIX) + 17)
e58d0 3e 3d 20 28 73 69 7a 65 5f 74 29 6e 42 75 66 20 >= (size_t)nBuf
e58e0 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 ){. return SQ
e58f0 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a LITE_ERROR;. }.
e5900 0a 20 20 64 6f 7b 0a 20 20 20 20 73 71 6c 69 74 . do{. sqlit
e5910 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 42 75 66 e3_snprintf(nBuf
e5920 2d 31 37 2c 20 7a 42 75 66 2c 20 22 25 73 2f 22 -17, zBuf, "%s/"
e5930 53 51 4c 49 54 45 5f 54 45 4d 50 5f 46 49 4c 45 SQLITE_TEMP_FILE
e5940 5f 50 52 45 46 49 58 2c 20 7a 44 69 72 29 3b 0a _PREFIX, zDir);.
e5950 20 20 20 20 6a 20 3d 20 28 69 6e 74 29 73 74 72 j = (int)str
e5960 6c 65 6e 28 7a 42 75 66 29 3b 0a 20 20 20 20 73 len(zBuf);. s
e5970 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 qlite3_randomnes
e5980 73 28 31 35 2c 20 26 7a 42 75 66 5b 6a 5d 29 3b s(15, &zBuf[j]);
e5990 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c . for(i=0; i<
e59a0 31 35 3b 20 69 2b 2b 2c 20 6a 2b 2b 29 7b 0a 20 15; i++, j++){.
e59b0 20 20 20 20 20 7a 42 75 66 5b 6a 5d 20 3d 20 28 zBuf[j] = (
e59c0 63 68 61 72 29 7a 43 68 61 72 73 5b 20 28 28 75 char)zChars[ ((u
e59d0 6e 73 69 67 6e 65 64 20 63 68 61 72 29 7a 42 75 nsigned char)zBu
e59e0 66 5b 6a 5d 29 25 28 73 69 7a 65 6f 66 28 7a 43 f[j])%(sizeof(zC
e59f0 68 61 72 73 29 2d 31 29 20 5d 3b 0a 20 20 20 20 hars)-1) ];.
e5a00 7d 0a 20 20 20 20 7a 42 75 66 5b 6a 5d 20 3d 20 }. zBuf[j] =
e5a10 30 3b 0a 20 20 7d 77 68 69 6c 65 28 20 61 63 63 0;. }while( acc
e5a20 65 73 73 28 7a 42 75 66 2c 30 29 3d 3d 30 20 29 ess(zBuf,0)==0 )
e5a30 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
e5a40 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 20 53 51 4c E_OK;.}..#if SQL
e5a50 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 ITE_ENABLE_LOCKI
e5a60 4e 47 5f 53 54 59 4c 45 20 26 26 20 64 65 66 69 NG_STYLE && defi
e5a70 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 0a 2f ned(__APPLE__)./
e5a80 2a 0a 2a 2a 20 52 6f 75 74 69 6e 65 20 74 6f 20 *.** Routine to
e5a90 74 72 61 6e 73 66 6f 72 6d 20 61 20 75 6e 69 78 transform a unix
e5aa0 46 69 6c 65 20 69 6e 74 6f 20 61 20 70 72 6f 78 File into a prox
e5ab0 79 2d 6c 6f 63 6b 69 6e 67 20 75 6e 69 78 46 69 y-locking unixFi
e5ac0 6c 65 2e 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 le..** Implement
e5ad0 61 74 69 6f 6e 20 69 6e 20 74 68 65 20 70 72 6f ation in the pro
e5ae0 78 79 2d 6c 6f 63 6b 20 64 69 76 69 73 69 6f 6e xy-lock division
e5af0 2c 20 62 75 74 20 75 73 65 64 20 62 79 20 75 6e , but used by un
e5b00 69 78 4f 70 65 6e 28 29 0a 2a 2a 20 69 66 20 53 ixOpen().** if S
e5b10 51 4c 49 54 45 5f 50 52 45 46 45 52 5f 50 52 4f QLITE_PREFER_PRO
e5b20 58 59 5f 4c 4f 43 4b 49 4e 47 20 69 73 20 64 65 XY_LOCKING is de
e5b30 66 69 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 fined..*/.static
e5b40 20 69 6e 74 20 70 72 6f 78 79 54 72 61 6e 73 66 int proxyTransf
e5b50 6f 72 6d 55 6e 69 78 46 69 6c 65 28 75 6e 69 78 ormUnixFile(unix
e5b60 46 69 6c 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 File*, const cha
e5b70 72 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a r*);.#endif../*.
e5b80 2a 2a 20 53 65 61 72 63 68 20 66 6f 72 20 61 6e ** Search for an
e5b90 20 75 6e 75 73 65 64 20 66 69 6c 65 20 64 65 73 unused file des
e5ba0 63 72 69 70 74 6f 72 20 74 68 61 74 20 77 61 73 criptor that was
e5bb0 20 6f 70 65 6e 65 64 20 6f 6e 20 74 68 65 20 64 opened on the d
e5bc0 61 74 61 62 61 73 65 20 0a 2a 2a 20 66 69 6c 65 atabase .** file
e5bd0 20 28 6e 6f 74 20 61 20 6a 6f 75 72 6e 61 6c 20 (not a journal
e5be0 6f 72 20 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61 or master-journa
e5bf0 6c 20 66 69 6c 65 29 20 69 64 65 6e 74 69 66 69 l file) identifi
e5c00 65 64 20 62 79 20 70 61 74 68 6e 61 6d 65 0a 2a ed by pathname.*
e5c10 2a 20 7a 50 61 74 68 20 77 69 74 68 20 53 51 4c * zPath with SQL
e5c20 49 54 45 5f 4f 50 45 4e 5f 58 58 58 20 66 6c 61 ITE_OPEN_XXX fla
e5c30 67 73 20 6d 61 74 63 68 69 6e 67 20 74 68 6f 73 gs matching thos
e5c40 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 e passed as the
e5c50 73 65 63 6f 6e 64 0a 2a 2a 20 61 72 67 75 6d 65 second.** argume
e5c60 6e 74 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 nt to this funct
e5c70 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 53 75 63 68 20 ion..**.** Such
e5c80 61 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f a file descripto
e5c90 72 20 6d 61 79 20 65 78 69 73 74 20 69 66 20 61 r may exist if a
e5ca0 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
e5cb0 74 69 6f 6e 20 77 61 73 20 63 6c 6f 73 65 64 0a tion was closed.
e5cc0 2a 2a 20 62 75 74 20 74 68 65 20 61 73 73 6f 63 ** but the assoc
e5cd0 69 61 74 65 64 20 66 69 6c 65 20 64 65 73 63 72 iated file descr
e5ce0 69 70 74 6f 72 20 63 6f 75 6c 64 20 6e 6f 74 20 iptor could not
e5cf0 62 65 20 63 6c 6f 73 65 64 20 62 65 63 61 75 73 be closed becaus
e5d00 65 20 73 6f 6d 65 0a 2a 2a 20 6f 74 68 65 72 20 e some.** other
e5d10 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 file descriptor
e5d20 6f 70 65 6e 20 6f 6e 20 74 68 65 20 73 61 6d 65 open on the same
e5d30 20 66 69 6c 65 20 69 73 20 68 6f 6c 64 69 6e 67 file is holding
e5d40 20 61 20 66 69 6c 65 2d 6c 6f 63 6b 2e 0a 2a 2a a file-lock..**
e5d50 20 52 65 66 65 72 20 74 6f 20 63 6f 6d 6d 65 6e Refer to commen
e5d60 74 73 20 69 6e 20 74 68 65 20 75 6e 69 78 43 6c ts in the unixCl
e5d70 6f 73 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 61 ose() function a
e5d80 6e 64 20 74 68 65 20 6c 65 6e 67 74 68 79 20 63 nd the lengthy c
e5d90 6f 6d 6d 65 6e 74 0a 2a 2a 20 64 65 73 63 72 69 omment.** descri
e5da0 62 69 6e 67 20 22 50 6f 73 69 78 20 41 64 76 69 bing "Posix Advi
e5db0 73 6f 72 79 20 4c 6f 63 6b 69 6e 67 22 20 61 74 sory Locking" at
e5dc0 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 the start of th
e5dd0 69 73 20 66 69 6c 65 20 66 6f 72 20 0a 2a 2a 20 is file for .**
e5de0 66 75 72 74 68 65 72 20 64 65 74 61 69 6c 73 2e further details.
e5df0 20 41 6c 73 6f 2c 20 74 69 63 6b 65 74 20 23 34 Also, ticket #4
e5e00 30 31 38 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 018..**.** If a
e5e10 73 75 69 74 61 62 6c 65 20 66 69 6c 65 20 64 65 suitable file de
e5e20 73 63 72 69 70 74 6f 72 20 69 73 20 66 6f 75 6e scriptor is foun
e5e30 64 2c 20 74 68 65 6e 20 69 74 20 69 73 20 72 65 d, then it is re
e5e40 74 75 72 6e 65 64 2e 20 49 66 20 6e 6f 0a 2a 2a turned. If no.**
e5e50 20 73 75 63 68 20 66 69 6c 65 20 64 65 73 63 72 such file descr
e5e60 69 70 74 6f 72 20 69 73 20 6c 6f 63 61 74 65 64 iptor is located
e5e70 2c 20 2d 31 20 69 73 20 72 65 74 75 72 6e 65 64 , -1 is returned
e5e80 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 55 6e 69 78 ..*/.static Unix
e5e90 55 6e 75 73 65 64 46 64 20 2a 66 69 6e 64 52 65 UnusedFd *findRe
e5ea0 75 73 61 62 6c 65 46 64 28 63 6f 6e 73 74 20 63 usableFd(const c
e5eb0 68 61 72 20 2a 7a 50 61 74 68 2c 20 69 6e 74 20 har *zPath, int
e5ec0 66 6c 61 67 73 29 7b 0a 20 20 55 6e 69 78 55 6e flags){. UnixUn
e5ed0 75 73 65 64 46 64 20 2a 70 55 6e 75 73 65 64 20 usedFd *pUnused
e5ee0 3d 20 30 3b 0a 0a 20 20 2f 2a 20 44 6f 20 6e 6f = 0;.. /* Do no
e5ef0 74 20 73 65 61 72 63 68 20 66 6f 72 20 61 6e 20 t search for an
e5f00 75 6e 75 73 65 64 20 66 69 6c 65 20 64 65 73 63 unused file desc
e5f10 72 69 70 74 6f 72 20 6f 6e 20 76 78 77 6f 72 6b riptor on vxwork
e5f20 73 2e 20 4e 6f 74 20 62 65 63 61 75 73 65 0a 20 s. Not because.
e5f30 20 2a 2a 20 76 78 77 6f 72 6b 73 20 77 6f 75 6c ** vxworks woul
e5f40 64 20 6e 6f 74 20 62 65 6e 65 66 69 74 20 66 72 d not benefit fr
e5f50 6f 6d 20 74 68 65 20 63 68 61 6e 67 65 20 28 69 om the change (i
e5f60 74 20 6d 69 67 68 74 2c 20 77 65 27 72 65 20 6e t might, we're n
e5f70 6f 74 20 73 75 72 65 29 2c 0a 20 20 2a 2a 20 62 ot sure),. ** b
e5f80 75 74 20 62 65 63 61 75 73 65 20 6e 6f 20 77 61 ut because no wa
e5f90 79 20 74 6f 20 74 65 73 74 20 69 74 20 69 73 20 y to test it is
e5fa0 63 75 72 72 65 6e 74 6c 79 20 61 76 61 69 6c 61 currently availa
e5fb0 62 6c 65 2e 20 49 74 20 69 73 20 62 65 74 74 65 ble. It is bette
e5fc0 72 20 0a 20 20 2a 2a 20 6e 6f 74 20 74 6f 20 72 r . ** not to r
e5fd0 69 73 6b 20 62 72 65 61 6b 69 6e 67 20 76 78 77 isk breaking vxw
e5fe0 6f 72 6b 73 20 73 75 70 70 6f 72 74 20 66 6f 72 orks support for
e5ff0 20 74 68 65 20 73 61 6b 65 20 6f 66 20 73 75 63 the sake of suc
e6000 68 20 61 6e 20 6f 62 73 63 75 72 65 20 0a 20 20 h an obscure .
e6010 2a 2a 20 66 65 61 74 75 72 65 2e 20 20 2a 2f 0a ** feature. */.
e6020 23 69 66 20 21 4f 53 5f 56 58 57 4f 52 4b 53 0a #if !OS_VXWORKS.
e6030 20 20 73 74 72 75 63 74 20 73 74 61 74 20 73 53 struct stat sS
e6040 74 61 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 tat;
e6050 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 /* Result
e6060 73 20 6f 66 20 73 74 61 74 28 29 20 63 61 6c 6c s of stat() call
e6070 20 2a 2f 0a 0a 20 20 2f 2a 20 41 20 73 74 61 74 */.. /* A stat
e6080 28 29 20 63 61 6c 6c 20 6d 61 79 20 66 61 69 6c () call may fail
e6090 20 66 6f 72 20 76 61 72 69 6f 75 73 20 72 65 61 for various rea
e60a0 73 6f 6e 73 2e 20 49 66 20 74 68 69 73 20 68 61 sons. If this ha
e60b0 70 70 65 6e 73 2c 20 69 74 20 69 73 0a 20 20 2a ppens, it is. *
e60c0 2a 20 61 6c 6d 6f 73 74 20 63 65 72 74 61 69 6e * almost certain
e60d0 20 74 68 61 74 20 61 6e 20 6f 70 65 6e 28 29 20 that an open()
e60e0 63 61 6c 6c 20 6f 6e 20 74 68 65 20 73 61 6d 65 call on the same
e60f0 20 70 61 74 68 20 77 69 6c 6c 20 61 6c 73 6f 20 path will also
e6100 66 61 69 6c 2e 0a 20 20 2a 2a 20 46 6f 72 20 74 fail.. ** For t
e6110 68 69 73 20 72 65 61 73 6f 6e 2c 20 69 66 20 61 his reason, if a
e6120 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 69 n error occurs i
e6130 6e 20 74 68 65 20 73 74 61 74 28 29 20 63 61 6c n the stat() cal
e6140 6c 20 68 65 72 65 2c 20 69 74 20 69 73 0a 20 20 l here, it is.
e6150 2a 2a 20 69 67 6e 6f 72 65 64 20 61 6e 64 20 2d ** ignored and -
e6160 31 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 54 1 is returned. T
e6170 68 65 20 63 61 6c 6c 65 72 20 77 69 6c 6c 20 74 he caller will t
e6180 72 79 20 74 6f 20 6f 70 65 6e 20 61 20 6e 65 77 ry to open a new
e6190 20 66 69 6c 65 0a 20 20 2a 2a 20 64 65 73 63 72 file. ** descr
e61a0 69 70 74 6f 72 20 6f 6e 20 74 68 65 20 73 61 6d iptor on the sam
e61b0 65 20 70 61 74 68 2c 20 66 61 69 6c 2c 20 61 6e e path, fail, an
e61c0 64 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f d return an erro
e61d0 72 20 74 6f 20 53 51 4c 69 74 65 2e 0a 20 20 2a r to SQLite.. *
e61e0 2a 0a 20 20 2a 2a 20 45 76 65 6e 20 69 66 20 61 *. ** Even if a
e61f0 20 73 75 62 73 65 71 75 65 6e 74 20 6f 70 65 6e subsequent open
e6200 28 29 20 63 61 6c 6c 20 64 6f 65 73 20 73 75 63 () call does suc
e6210 63 65 65 64 2c 20 74 68 65 20 63 6f 6e 73 65 71 ceed, the conseq
e6220 75 65 6e 63 65 73 20 6f 66 0a 20 20 2a 2a 20 6e uences of. ** n
e6230 6f 74 20 73 65 61 72 63 68 69 6e 67 20 66 6f 72 ot searching for
e6240 20 61 20 72 65 73 75 73 61 62 6c 65 20 66 69 6c a resusable fil
e6250 65 20 64 65 73 63 72 69 70 74 6f 72 20 61 72 65 e descriptor are
e6260 20 6e 6f 74 20 64 69 72 65 2e 20 20 2a 2f 0a 20 not dire. */.
e6270 20 69 66 28 20 30 3d 3d 73 74 61 74 28 7a 50 61 if( 0==stat(zPa
e6280 74 68 2c 20 26 73 53 74 61 74 29 20 29 7b 0a 20 th, &sStat) ){.
e6290 20 20 20 73 74 72 75 63 74 20 75 6e 69 78 4f 70 struct unixOp
e62a0 65 6e 43 6e 74 20 2a 70 4f 3b 0a 20 20 20 20 73 enCnt *pO;. s
e62b0 74 72 75 63 74 20 75 6e 69 78 46 69 6c 65 49 64 truct unixFileId
e62c0 20 69 64 3b 0a 20 20 20 20 69 64 2e 64 65 76 20 id;. id.dev
e62d0 3d 20 73 53 74 61 74 2e 73 74 5f 64 65 76 3b 0a = sStat.st_dev;.
e62e0 20 20 20 20 69 64 2e 69 6e 6f 20 3d 20 73 53 74 id.ino = sSt
e62f0 61 74 2e 73 74 5f 69 6e 6f 3b 0a 0a 20 20 20 20 at.st_ino;..
e6300 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 unixEnterMutex()
e6310 3b 0a 20 20 20 20 66 6f 72 28 70 4f 3d 6f 70 65 ;. for(pO=ope
e6320 6e 4c 69 73 74 3b 20 70 4f 20 26 26 20 6d 65 6d nList; pO && mem
e6330 63 6d 70 28 26 69 64 2c 20 26 70 4f 2d 3e 66 69 cmp(&id, &pO->fi
e6340 6c 65 49 64 2c 20 73 69 7a 65 6f 66 28 69 64 29 leId, sizeof(id)
e6350 29 3b 20 70 4f 3d 70 4f 2d 3e 70 4e 65 78 74 29 ); pO=pO->pNext)
e6360 3b 0a 20 20 20 20 69 66 28 20 70 4f 20 29 7b 0a ;. if( pO ){.
e6370 20 20 20 20 20 20 55 6e 69 78 55 6e 75 73 65 64 UnixUnused
e6380 46 64 20 2a 2a 70 70 3b 0a 20 20 20 20 20 20 66 Fd **pp;. f
e6390 6f 72 28 70 70 3d 26 70 4f 2d 3e 70 55 6e 75 73 or(pp=&pO->pUnus
e63a0 65 64 3b 20 2a 70 70 20 26 26 20 28 2a 70 70 29 ed; *pp && (*pp)
e63b0 2d 3e 66 6c 61 67 73 21 3d 66 6c 61 67 73 3b 20 ->flags!=flags;
e63c0 70 70 3d 26 28 28 2a 70 70 29 2d 3e 70 4e 65 78 pp=&((*pp)->pNex
e63d0 74 29 29 3b 0a 20 20 20 20 20 20 70 55 6e 75 73 t));. pUnus
e63e0 65 64 20 3d 20 2a 70 70 3b 0a 20 20 20 20 20 20 ed = *pp;.
e63f0 69 66 28 20 70 55 6e 75 73 65 64 20 29 7b 0a 20 if( pUnused ){.
e6400 20 20 20 20 20 20 20 2a 70 70 20 3d 20 70 55 6e *pp = pUn
e6410 75 73 65 64 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 used->pNext;.
e6420 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 75 }. }. u
e6430 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b nixLeaveMutex();
e6440 0a 20 20 7d 0a 23 65 6e 64 69 66 20 20 20 20 2f . }.#endif /
e6450 2a 20 69 66 20 21 4f 53 5f 56 58 57 4f 52 4b 53 * if !OS_VXWORKS
e6460 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 70 55 6e */. return pUn
e6470 75 73 65 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f used;.}../*.** O
e6480 70 65 6e 20 74 68 65 20 66 69 6c 65 20 7a 50 61 pen the file zPa
e6490 74 68 2e 0a 2a 2a 20 0a 2a 2a 20 50 72 65 76 69 th..** .** Previ
e64a0 6f 75 73 6c 79 2c 20 74 68 65 20 53 51 4c 69 74 ously, the SQLit
e64b0 65 20 4f 53 20 6c 61 79 65 72 20 75 73 65 64 20 e OS layer used
e64c0 74 68 72 65 65 20 66 75 6e 63 74 69 6f 6e 73 20 three functions
e64d0 69 6e 20 70 6c 61 63 65 20 6f 66 20 74 68 69 73 in place of this
e64e0 0a 2a 2a 20 6f 6e 65 3a 0a 2a 2a 0a 2a 2a 20 20 .** one:.**.**
e64f0 20 20 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e sqlite3OsOpen
e6500 52 65 61 64 57 72 69 74 65 28 29 3b 0a 2a 2a 20 ReadWrite();.**
e6510 20 20 20 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 sqlite3OsOpe
e6520 6e 52 65 61 64 4f 6e 6c 79 28 29 3b 0a 2a 2a 20 nReadOnly();.**
e6530 20 20 20 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 sqlite3OsOpe
e6540 6e 45 78 63 6c 75 73 69 76 65 28 29 3b 0a 2a 2a nExclusive();.**
e6550 0a 2a 2a 20 54 68 65 73 65 20 63 61 6c 6c 73 20 .** These calls
e6560 63 6f 72 72 65 73 70 6f 6e 64 20 74 6f 20 74 68 correspond to th
e6570 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6d 62 e following comb
e6580 69 6e 61 74 69 6f 6e 73 20 6f 66 20 66 6c 61 67 inations of flag
e6590 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 52 65 61 s:.**.** Rea
e65a0 64 57 72 69 74 65 28 29 20 2d 3e 20 20 20 20 20 dWrite() ->
e65b0 28 52 45 41 44 57 52 49 54 45 20 7c 20 43 52 45 (READWRITE | CRE
e65c0 41 54 45 29 0a 2a 2a 20 20 20 20 20 52 65 61 64 ATE).** Read
e65d0 4f 6e 6c 79 28 29 20 20 2d 3e 20 20 20 20 20 28 Only() -> (
e65e0 52 45 41 44 4f 4e 4c 59 29 20 0a 2a 2a 20 20 20 READONLY) .**
e65f0 20 20 4f 70 65 6e 45 78 63 6c 75 73 69 76 65 28 OpenExclusive(
e6600 29 20 2d 3e 20 28 52 45 41 44 57 52 49 54 45 20 ) -> (READWRITE
e6610 7c 20 43 52 45 41 54 45 20 7c 20 45 58 43 4c 55 | CREATE | EXCLU
e6620 53 49 56 45 29 0a 2a 2a 0a 2a 2a 20 54 68 65 20 SIVE).**.** The
e6630 6f 6c 64 20 4f 70 65 6e 45 78 63 6c 75 73 69 76 old OpenExclusiv
e6640 65 28 29 20 61 63 63 65 70 74 65 64 20 61 20 62 e() accepted a b
e6650 6f 6f 6c 65 61 6e 20 61 72 67 75 6d 65 6e 74 20 oolean argument
e6660 2d 20 22 64 65 6c 46 6c 61 67 22 2e 20 49 66 0a - "delFlag". If.
e6670 2a 2a 20 74 72 75 65 2c 20 74 68 65 20 66 69 6c ** true, the fil
e6680 65 20 77 61 73 20 63 6f 6e 66 69 67 75 72 65 64 e was configured
e6690 20 74 6f 20 62 65 20 61 75 74 6f 6d 61 74 69 63 to be automatic
e66a0 61 6c 6c 79 20 64 65 6c 65 74 65 64 20 77 68 65 ally deleted whe
e66b0 6e 20 74 68 65 0a 2a 2a 20 66 69 6c 65 20 68 61 n the.** file ha
e66c0 6e 64 6c 65 20 63 6c 6f 73 65 64 2e 20 54 6f 20 ndle closed. To
e66d0 61 63 68 69 65 76 65 20 74 68 65 20 73 61 6d 65 achieve the same
e66e0 20 65 66 66 65 63 74 20 75 73 69 6e 67 20 74 68 effect using th
e66f0 69 73 20 6e 65 77 20 0a 2a 2a 20 69 6e 74 65 72 is new .** inter
e6700 66 61 63 65 2c 20 61 64 64 20 74 68 65 20 44 45 face, add the DE
e6710 4c 45 54 45 4f 4e 43 4c 4f 53 45 20 66 6c 61 67 LETEONCLOSE flag
e6720 20 74 6f 20 74 68 6f 73 65 20 73 70 65 63 69 66 to those specif
e6730 69 65 64 20 61 62 6f 76 65 20 66 6f 72 20 0a 2a ied above for .*
e6740 2a 20 4f 70 65 6e 45 78 63 6c 75 73 69 76 65 28 * OpenExclusive(
e6750 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 )..*/.static int
e6760 20 75 6e 69 78 4f 70 65 6e 28 0a 20 20 73 71 6c unixOpen(. sql
e6770 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 ite3_vfs *pVfs,
e6780 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
e6790 20 56 46 53 20 66 6f 72 20 77 68 69 63 68 20 74 VFS for which t
e67a0 68 69 73 20 69 73 20 74 68 65 20 78 4f 70 65 6e his is the xOpen
e67b0 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 63 6f 6e method */. con
e67c0 73 74 20 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 st char *zPath,
e67d0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 74 /* Pat
e67e0 68 6e 61 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f hname of file to
e67f0 20 62 65 20 6f 70 65 6e 65 64 20 2a 2f 0a 20 20 be opened */.
e6800 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 46 sqlite3_file *pF
e6810 69 6c 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 ile, /*
e6820 54 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 The file descrip
e6830 74 6f 72 20 74 6f 20 62 65 20 66 69 6c 6c 65 64 tor to be filled
e6840 20 69 6e 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 in */. int fla
e6850 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 gs,
e6860 20 20 20 20 20 20 2f 2a 20 49 6e 70 75 74 20 66 /* Input f
e6870 6c 61 67 73 20 74 6f 20 63 6f 6e 74 72 6f 6c 20 lags to control
e6880 74 68 65 20 6f 70 65 6e 69 6e 67 20 2a 2f 0a 20 the opening */.
e6890 20 69 6e 74 20 2a 70 4f 75 74 46 6c 61 67 73 20 int *pOutFlags
e68a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
e68b0 20 4f 75 74 70 75 74 20 66 6c 61 67 73 20 72 65 Output flags re
e68c0 74 75 72 6e 65 64 20 74 6f 20 53 51 4c 69 74 65 turned to SQLite
e68d0 20 63 6f 72 65 20 2a 2f 0a 29 7b 0a 20 20 75 6e core */.){. un
e68e0 69 78 46 69 6c 65 20 2a 70 20 3d 20 28 75 6e 69 ixFile *p = (uni
e68f0 78 46 69 6c 65 20 2a 29 70 46 69 6c 65 3b 0a 20 xFile *)pFile;.
e6900 20 69 6e 74 20 66 64 20 3d 20 2d 31 3b 20 20 20 int fd = -1;
e6910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e6920 2f 2a 20 46 69 6c 65 20 64 65 73 63 72 69 70 74 /* File descript
e6930 6f 72 20 72 65 74 75 72 6e 65 64 20 62 79 20 6f or returned by o
e6940 70 65 6e 28 29 20 2a 2f 0a 20 20 69 6e 74 20 64 pen() */. int d
e6950 69 72 66 64 20 3d 20 2d 31 3b 20 20 20 20 20 20 irfd = -1;
e6960 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 69 72 /* Dir
e6970 65 63 74 6f 72 79 20 66 69 6c 65 20 64 65 73 63 ectory file desc
e6980 72 69 70 74 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 riptor */. int
e6990 6f 70 65 6e 46 6c 61 67 73 20 3d 20 30 3b 20 20 openFlags = 0;
e69a0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c /* Fl
e69b0 61 67 73 20 74 6f 20 70 61 73 73 20 74 6f 20 6f ags to pass to o
e69c0 70 65 6e 28 29 20 2a 2f 0a 20 20 69 6e 74 20 65 pen() */. int e
e69d0 54 79 70 65 20 3d 20 66 6c 61 67 73 26 30 78 46 Type = flags&0xF
e69e0 46 46 46 46 46 30 30 3b 20 20 2f 2a 20 54 79 70 FFFFF00; /* Typ
e69f0 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 6f 70 65 e of file to ope
e6a00 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f 4c 6f 63 n */. int noLoc
e6a10 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 k;
e6a20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f /* True to
e6a30 20 6f 6d 69 74 20 6c 6f 63 6b 69 6e 67 20 70 72 omit locking pr
e6a40 69 6d 69 74 69 76 65 73 20 2a 2f 0a 20 20 69 6e imitives */. in
e6a50 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
e6a60 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
e6a70 46 75 6e 63 74 69 6f 6e 20 52 65 74 75 72 6e 20 Function Return
e6a80 43 6f 64 65 20 2a 2f 0a 0a 20 20 69 6e 74 20 69 Code */.. int i
e6a90 73 45 78 63 6c 75 73 69 76 65 20 20 3d 20 28 66 sExclusive = (f
e6aa0 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 lags & SQLITE_OP
e6ab0 45 4e 5f 45 58 43 4c 55 53 49 56 45 29 3b 0a 20 EN_EXCLUSIVE);.
e6ac0 20 69 6e 74 20 69 73 44 65 6c 65 74 65 20 20 20 int isDelete
e6ad0 20 20 3d 20 28 66 6c 61 67 73 20 26 20 53 51 4c = (flags & SQL
e6ae0 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f ITE_OPEN_DELETEO
e6af0 4e 43 4c 4f 53 45 29 3b 0a 20 20 69 6e 74 20 69 NCLOSE);. int i
e6b00 73 43 72 65 61 74 65 20 20 20 20 20 3d 20 28 66 sCreate = (f
e6b10 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 lags & SQLITE_OP
e6b20 45 4e 5f 43 52 45 41 54 45 29 3b 0a 20 20 69 6e EN_CREATE);. in
e6b30 74 20 69 73 52 65 61 64 6f 6e 6c 79 20 20 20 3d t isReadonly =
e6b40 20 28 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 (flags & SQLITE
e6b50 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 29 3b _OPEN_READONLY);
e6b60 0a 20 20 69 6e 74 20 69 73 52 65 61 64 57 72 69 . int isReadWri
e6b70 74 65 20 20 3d 20 28 66 6c 61 67 73 20 26 20 53 te = (flags & S
e6b80 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 QLITE_OPEN_READW
e6b90 52 49 54 45 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 RITE);.. /* If
e6ba0 63 72 65 61 74 69 6e 67 20 61 20 6d 61 73 74 65 creating a maste
e6bb0 72 20 6f 72 20 6d 61 69 6e 2d 66 69 6c 65 20 6a r or main-file j
e6bc0 6f 75 72 6e 61 6c 2c 20 74 68 69 73 20 66 75 6e ournal, this fun
e6bd0 63 74 69 6f 6e 20 77 69 6c 6c 20 6f 70 65 6e 0a ction will open.
e6be0 20 20 2a 2a 20 61 20 66 69 6c 65 2d 64 65 73 63 ** a file-desc
e6bf0 72 69 70 74 6f 72 20 6f 6e 20 74 68 65 20 64 69 riptor on the di
e6c00 72 65 63 74 6f 72 79 20 74 6f 6f 2e 20 54 68 65 rectory too. The
e6c10 20 66 69 72 73 74 20 74 69 6d 65 20 75 6e 69 78 first time unix
e6c20 53 79 6e 63 28 29 0a 20 20 2a 2a 20 69 73 20 63 Sync(). ** is c
e6c30 61 6c 6c 65 64 20 74 68 65 20 64 69 72 65 63 74 alled the direct
e6c40 6f 72 79 20 66 69 6c 65 20 64 65 73 63 72 69 70 ory file descrip
e6c50 74 6f 72 20 77 69 6c 6c 20 62 65 20 66 73 79 6e tor will be fsyn
e6c60 63 28 29 65 64 20 61 6e 64 20 63 6c 6f 73 65 28 c()ed and close(
e6c70 29 64 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 69 )d.. */. int i
e6c80 73 4f 70 65 6e 44 69 72 65 63 74 6f 72 79 20 3d sOpenDirectory =
e6c90 20 28 69 73 43 72 65 61 74 65 20 26 26 20 0a 20 (isCreate && .
e6ca0 20 20 20 20 20 28 65 54 79 70 65 3d 3d 53 51 4c (eType==SQL
e6cb0 49 54 45 5f 4f 50 45 4e 5f 4d 41 53 54 45 52 5f ITE_OPEN_MASTER_
e6cc0 4a 4f 55 52 4e 41 4c 20 7c 7c 20 65 54 79 70 65 JOURNAL || eType
e6cd0 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 ==SQLITE_OPEN_MA
e6ce0 49 4e 5f 4a 4f 55 52 4e 41 4c 29 0a 20 20 29 3b IN_JOURNAL). );
e6cf0 0a 0a 20 20 2f 2a 20 49 66 20 61 72 67 75 6d 65 .. /* If argume
e6d00 6e 74 20 7a 50 61 74 68 20 69 73 20 61 20 4e 55 nt zPath is a NU
e6d10 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 74 68 69 73 LL pointer, this
e6d20 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 72 65 71 function is req
e6d30 75 69 72 65 64 20 74 6f 20 6f 70 65 6e 0a 20 20 uired to open.
e6d40 2a 2a 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66 ** a temporary f
e6d50 69 6c 65 2e 20 55 73 65 20 74 68 69 73 20 62 75 ile. Use this bu
e6d60 66 66 65 72 20 74 6f 20 73 74 6f 72 65 20 74 68 ffer to store th
e6d70 65 20 66 69 6c 65 20 6e 61 6d 65 20 69 6e 2e 0a e file name in..
e6d80 20 20 2a 2f 0a 20 20 63 68 61 72 20 7a 54 6d 70 */. char zTmp
e6d90 6e 61 6d 65 5b 4d 41 58 5f 50 41 54 48 4e 41 4d name[MAX_PATHNAM
e6da0 45 2b 31 5d 3b 0a 20 20 63 6f 6e 73 74 20 63 68 E+1];. const ch
e6db0 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 7a 50 61 74 ar *zName = zPat
e6dc0 68 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 h;.. /* Check t
e6dd0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 61 he following sta
e6de0 74 65 6d 65 6e 74 73 20 61 72 65 20 74 72 75 65 tements are true
e6df0 3a 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 28 : . **. ** (
e6e00 61 29 20 45 78 61 63 74 6c 79 20 6f 6e 65 20 6f a) Exactly one o
e6e10 66 20 74 68 65 20 52 45 41 44 57 52 49 54 45 20 f the READWRITE
e6e20 61 6e 64 20 52 45 41 44 4f 4e 4c 59 20 66 6c 61 and READONLY fla
e6e30 67 73 20 6d 75 73 74 20 62 65 20 73 65 74 2c 20 gs must be set,
e6e40 61 6e 64 20 0a 20 20 2a 2a 20 20 20 28 62 29 20 and . ** (b)
e6e50 69 66 20 43 52 45 41 54 45 20 69 73 20 73 65 74 if CREATE is set
e6e60 2c 20 74 68 65 6e 20 52 45 41 44 57 52 49 54 45 , then READWRITE
e6e70 20 6d 75 73 74 20 61 6c 73 6f 20 62 65 20 73 65 must also be se
e6e80 74 2c 20 61 6e 64 0a 20 20 2a 2a 20 20 20 28 63 t, and. ** (c
e6e90 29 20 69 66 20 45 58 43 4c 55 53 49 56 45 20 69 ) if EXCLUSIVE i
e6ea0 73 20 73 65 74 2c 20 74 68 65 6e 20 43 52 45 41 s set, then CREA
e6eb0 54 45 20 6d 75 73 74 20 61 6c 73 6f 20 62 65 20 TE must also be
e6ec0 73 65 74 2e 0a 20 20 2a 2a 20 20 20 28 64 29 20 set.. ** (d)
e6ed0 69 66 20 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 if DELETEONCLOSE
e6ee0 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 43 52 is set, then CR
e6ef0 45 41 54 45 20 6d 75 73 74 20 61 6c 73 6f 20 62 EATE must also b
e6f00 65 20 73 65 74 2e 0a 20 20 2a 2f 0a 20 20 61 73 e set.. */. as
e6f10 73 65 72 74 28 28 69 73 52 65 61 64 6f 6e 6c 79 sert((isReadonly
e6f20 3d 3d 30 20 7c 7c 20 69 73 52 65 61 64 57 72 69 ==0 || isReadWri
e6f30 74 65 3d 3d 30 29 20 26 26 20 28 69 73 52 65 61 te==0) && (isRea
e6f40 64 57 72 69 74 65 20 7c 7c 20 69 73 52 65 61 64 dWrite || isRead
e6f50 6f 6e 6c 79 29 29 3b 0a 20 20 61 73 73 65 72 74 only));. assert
e6f60 28 69 73 43 72 65 61 74 65 3d 3d 30 20 7c 7c 20 (isCreate==0 ||
e6f70 69 73 52 65 61 64 57 72 69 74 65 29 3b 0a 20 20 isReadWrite);.
e6f80 61 73 73 65 72 74 28 69 73 45 78 63 6c 75 73 69 assert(isExclusi
e6f90 76 65 3d 3d 30 20 7c 7c 20 69 73 43 72 65 61 74 ve==0 || isCreat
e6fa0 65 29 3b 0a 20 20 61 73 73 65 72 74 28 69 73 44 e);. assert(isD
e6fb0 65 6c 65 74 65 3d 3d 30 20 7c 7c 20 69 73 43 72 elete==0 || isCr
e6fc0 65 61 74 65 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 eate);.. /* The
e6fd0 20 6d 61 69 6e 20 44 42 2c 20 6d 61 69 6e 20 6a main DB, main j
e6fe0 6f 75 72 6e 61 6c 2c 20 61 6e 64 20 6d 61 73 74 ournal, and mast
e6ff0 65 72 20 6a 6f 75 72 6e 61 6c 20 61 72 65 20 6e er journal are n
e7000 65 76 65 72 20 61 75 74 6f 6d 61 74 69 63 61 6c ever automatical
e7010 6c 79 0a 20 20 2a 2a 20 64 65 6c 65 74 65 64 2e ly. ** deleted.
e7020 20 4e 6f 72 20 61 72 65 20 74 68 65 79 20 65 76 Nor are they ev
e7030 65 72 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c er temporary fil
e7040 65 73 2e 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 es. */. assert
e7050 28 20 28 21 69 73 44 65 6c 65 74 65 20 26 26 20 ( (!isDelete &&
e7060 7a 4e 61 6d 65 29 20 7c 7c 20 65 54 79 70 65 21 zName) || eType!
e7070 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 =SQLITE_OPEN_MAI
e7080 4e 5f 44 42 20 29 3b 0a 20 20 61 73 73 65 72 74 N_DB );. assert
e7090 28 20 28 21 69 73 44 65 6c 65 74 65 20 26 26 20 ( (!isDelete &&
e70a0 7a 4e 61 6d 65 29 20 7c 7c 20 65 54 79 70 65 21 zName) || eType!
e70b0 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 =SQLITE_OPEN_MAI
e70c0 4e 5f 4a 4f 55 52 4e 41 4c 20 29 3b 0a 20 20 61 N_JOURNAL );. a
e70d0 73 73 65 72 74 28 20 28 21 69 73 44 65 6c 65 74 ssert( (!isDelet
e70e0 65 20 26 26 20 7a 4e 61 6d 65 29 20 7c 7c 20 65 e && zName) || e
e70f0 54 79 70 65 21 3d 53 51 4c 49 54 45 5f 4f 50 45 Type!=SQLITE_OPE
e7100 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 4e 41 4c N_MASTER_JOURNAL
e7110 20 29 3b 0a 0a 20 20 2f 2a 20 41 73 73 65 72 74 );.. /* Assert
e7120 20 74 68 61 74 20 74 68 65 20 75 70 70 65 72 20 that the upper
e7130 6c 61 79 65 72 20 68 61 73 20 73 65 74 20 6f 6e layer has set on
e7140 65 20 6f 66 20 74 68 65 20 22 66 69 6c 65 2d 74 e of the "file-t
e7150 79 70 65 22 20 66 6c 61 67 73 2e 20 2a 2f 0a 20 ype" flags. */.
e7160 20 61 73 73 65 72 74 28 20 65 54 79 70 65 3d 3d assert( eType==
e7170 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e SQLITE_OPEN_MAIN
e7180 5f 44 42 20 20 20 20 20 20 7c 7c 20 65 54 79 70 _DB || eTyp
e7190 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 e==SQLITE_OPEN_T
e71a0 45 4d 50 5f 44 42 20 0a 20 20 20 20 20 20 20 7c EMP_DB . |
e71b0 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f | eType==SQLITE_
e71c0 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 OPEN_MAIN_JOURNA
e71d0 4c 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49 L || eType==SQLI
e71e0 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 4a 4f 55 TE_OPEN_TEMP_JOU
e71f0 52 4e 41 4c 20 0a 20 20 20 20 20 20 20 7c 7c 20 RNAL . ||
e7200 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 eType==SQLITE_OP
e7210 45 4e 5f 53 55 42 4a 4f 55 52 4e 41 4c 20 20 20 EN_SUBJOURNAL
e7220 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 || eType==SQLITE
e7230 5f 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 _OPEN_MASTER_JOU
e7240 52 4e 41 4c 20 0a 20 20 20 20 20 20 20 7c 7c 20 RNAL . ||
e7250 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 eType==SQLITE_OP
e7260 45 4e 5f 54 52 41 4e 53 49 45 4e 54 5f 44 42 0a EN_TRANSIENT_DB.
e7270 20 20 29 3b 0a 0a 20 20 6d 65 6d 73 65 74 28 70 );.. memset(p
e7280 2c 20 30 2c 20 73 69 7a 65 6f 66 28 75 6e 69 78 , 0, sizeof(unix
e7290 46 69 6c 65 29 29 3b 0a 0a 20 20 69 66 28 20 65 File));.. if( e
e72a0 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 Type==SQLITE_OPE
e72b0 4e 5f 4d 41 49 4e 5f 44 42 20 29 7b 0a 20 20 20 N_MAIN_DB ){.
e72c0 20 55 6e 69 78 55 6e 75 73 65 64 46 64 20 2a 70 UnixUnusedFd *p
e72d0 55 6e 75 73 65 64 3b 0a 20 20 20 20 70 55 6e 75 Unused;. pUnu
e72e0 73 65 64 20 3d 20 66 69 6e 64 52 65 75 73 61 62 sed = findReusab
e72f0 6c 65 46 64 28 7a 4e 61 6d 65 2c 20 66 6c 61 67 leFd(zName, flag
e7300 73 29 3b 0a 20 20 20 20 69 66 28 20 70 55 6e 75 s);. if( pUnu
e7310 73 65 64 20 29 7b 0a 20 20 20 20 20 20 66 64 20 sed ){. fd
e7320 3d 20 70 55 6e 75 73 65 64 2d 3e 66 64 3b 0a 20 = pUnused->fd;.
e7330 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
e7340 70 55 6e 75 73 65 64 20 3d 20 73 71 6c 69 74 65 pUnused = sqlite
e7350 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 3_malloc(sizeof(
e7360 2a 70 55 6e 75 73 65 64 29 29 3b 0a 20 20 20 20 *pUnused));.
e7370 20 20 69 66 28 20 21 70 55 6e 75 73 65 64 20 29 if( !pUnused )
e7380 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e {. return
e7390 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
e73a0 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 }. }.
e73b0 20 70 2d 3e 70 55 6e 75 73 65 64 20 3d 20 70 55 p->pUnused = pU
e73c0 6e 75 73 65 64 3b 0a 20 20 7d 65 6c 73 65 20 69 nused;. }else i
e73d0 66 28 20 21 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 f( !zName ){.
e73e0 20 2f 2a 20 49 66 20 7a 4e 61 6d 65 20 69 73 20 /* If zName is
e73f0 4e 55 4c 4c 2c 20 74 68 65 20 75 70 70 65 72 20 NULL, the upper
e7400 6c 61 79 65 72 20 69 73 20 72 65 71 75 65 73 74 layer is request
e7410 69 6e 67 20 61 20 74 65 6d 70 20 66 69 6c 65 2e ing a temp file.
e7420 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 69 */. assert(i
e7430 73 44 65 6c 65 74 65 20 26 26 20 21 69 73 4f 70 sDelete && !isOp
e7440 65 6e 44 69 72 65 63 74 6f 72 79 29 3b 0a 20 20 enDirectory);.
e7450 20 20 72 63 20 3d 20 67 65 74 54 65 6d 70 6e 61 rc = getTempna
e7460 6d 65 28 4d 41 58 5f 50 41 54 48 4e 41 4d 45 2b me(MAX_PATHNAME+
e7470 31 2c 20 7a 54 6d 70 6e 61 6d 65 29 3b 0a 20 20 1, zTmpname);.
e7480 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
e7490 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 _OK ){. ret
e74a0 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 urn rc;. }.
e74b0 20 20 7a 4e 61 6d 65 20 3d 20 7a 54 6d 70 6e 61 zName = zTmpna
e74c0 6d 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 44 65 me;. }.. /* De
e74d0 74 65 72 6d 69 6e 65 20 74 68 65 20 76 61 6c 75 termine the valu
e74e0 65 20 6f 66 20 74 68 65 20 66 6c 61 67 73 20 70 e of the flags p
e74f0 61 72 61 6d 65 74 65 72 20 70 61 73 73 65 64 20 arameter passed
e7500 74 6f 20 50 4f 53 49 58 20 66 75 6e 63 74 69 6f to POSIX functio
e7510 6e 0a 20 20 2a 2a 20 6f 70 65 6e 28 29 2e 20 54 n. ** open(). T
e7520 68 65 73 65 20 6d 75 73 74 20 62 65 20 63 61 6c hese must be cal
e7530 63 75 6c 61 74 65 64 20 65 76 65 6e 20 69 66 20 culated even if
e7540 6f 70 65 6e 28 29 20 69 73 20 6e 6f 74 20 63 61 open() is not ca
e7550 6c 6c 65 64 2c 20 61 73 0a 20 20 2a 2a 20 74 68 lled, as. ** th
e7560 65 79 20 6d 61 79 20 62 65 20 73 74 6f 72 65 64 ey may be stored
e7570 20 61 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 as part of the
e7580 66 69 6c 65 20 68 61 6e 64 6c 65 20 61 6e 64 20 file handle and
e7590 75 73 65 64 20 62 79 20 74 68 65 20 0a 20 20 2a used by the . *
e75a0 2a 20 27 63 6f 6e 63 68 20 66 69 6c 65 27 20 6c * 'conch file' l
e75b0 6f 63 6b 69 6e 67 20 66 75 6e 63 74 69 6f 6e 73 ocking functions
e75c0 20 6c 61 74 65 72 20 6f 6e 2e 20 20 2a 2f 0a 20 later on. */.
e75d0 20 69 66 28 20 69 73 52 65 61 64 6f 6e 6c 79 20 if( isReadonly
e75e0 29 20 20 6f 70 65 6e 46 6c 61 67 73 20 7c 3d 20 ) openFlags |=
e75f0 4f 5f 52 44 4f 4e 4c 59 3b 0a 20 20 69 66 28 20 O_RDONLY;. if(
e7600 69 73 52 65 61 64 57 72 69 74 65 20 29 20 6f 70 isReadWrite ) op
e7610 65 6e 46 6c 61 67 73 20 7c 3d 20 4f 5f 52 44 57 enFlags |= O_RDW
e7620 52 3b 0a 20 20 69 66 28 20 69 73 43 72 65 61 74 R;. if( isCreat
e7630 65 20 29 20 20 20 20 6f 70 65 6e 46 6c 61 67 73 e ) openFlags
e7640 20 7c 3d 20 4f 5f 43 52 45 41 54 3b 0a 20 20 69 |= O_CREAT;. i
e7650 66 28 20 69 73 45 78 63 6c 75 73 69 76 65 20 29 f( isExclusive )
e7660 20 6f 70 65 6e 46 6c 61 67 73 20 7c 3d 20 28 4f openFlags |= (O
e7670 5f 45 58 43 4c 7c 4f 5f 4e 4f 46 4f 4c 4c 4f 57 _EXCL|O_NOFOLLOW
e7680 29 3b 0a 20 20 6f 70 65 6e 46 6c 61 67 73 20 7c );. openFlags |
e7690 3d 20 28 4f 5f 4c 41 52 47 45 46 49 4c 45 7c 4f = (O_LARGEFILE|O
e76a0 5f 42 49 4e 41 52 59 29 3b 0a 0a 20 20 69 66 28 _BINARY);.. if(
e76b0 20 66 64 3c 30 20 29 7b 0a 20 20 20 20 6d 6f 64 fd<0 ){. mod
e76c0 65 5f 74 20 6f 70 65 6e 4d 6f 64 65 20 3d 20 28 e_t openMode = (
e76d0 69 73 44 65 6c 65 74 65 3f 30 36 30 30 3a 53 51 isDelete?0600:SQ
e76e0 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 46 49 4c LITE_DEFAULT_FIL
e76f0 45 5f 50 45 52 4d 49 53 53 49 4f 4e 53 29 3b 0a E_PERMISSIONS);.
e7700 20 20 20 20 66 64 20 3d 20 6f 70 65 6e 28 7a 4e fd = open(zN
e7710 61 6d 65 2c 20 6f 70 65 6e 46 6c 61 67 73 2c 20 ame, openFlags,
e7720 6f 70 65 6e 4d 6f 64 65 29 3b 0a 20 20 20 20 4f openMode);. O
e7730 53 54 52 41 43 45 34 28 22 4f 50 45 4e 58 20 20 STRACE4("OPENX
e7740 20 25 2d 33 64 20 25 73 20 30 25 6f 5c 6e 22 2c %-3d %s 0%o\n",
e7750 20 66 64 2c 20 7a 4e 61 6d 65 2c 20 6f 70 65 6e fd, zName, open
e7760 46 6c 61 67 73 29 3b 0a 20 20 20 20 69 66 28 20 Flags);. if(
e7770 66 64 3c 30 20 26 26 20 65 72 72 6e 6f 21 3d 45 fd<0 && errno!=E
e7780 49 53 44 49 52 20 26 26 20 69 73 52 65 61 64 57 ISDIR && isReadW
e7790 72 69 74 65 20 26 26 20 21 69 73 45 78 63 6c 75 rite && !isExclu
e77a0 73 69 76 65 20 29 7b 0a 20 20 20 20 20 20 2f 2a sive ){. /*
e77b0 20 46 61 69 6c 65 64 20 74 6f 20 6f 70 65 6e 20 Failed to open
e77c0 74 68 65 20 66 69 6c 65 20 66 6f 72 20 72 65 61 the file for rea
e77d0 64 2f 77 72 69 74 65 20 61 63 63 65 73 73 2e 20 d/write access.
e77e0 54 72 79 20 72 65 61 64 2d 6f 6e 6c 79 2e 20 2a Try read-only. *
e77f0 2f 0a 20 20 20 20 20 20 66 6c 61 67 73 20 26 3d /. flags &=
e7800 20 7e 28 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 ~(SQLITE_OPEN_R
e7810 45 41 44 57 52 49 54 45 7c 53 51 4c 49 54 45 5f EADWRITE|SQLITE_
e7820 4f 50 45 4e 5f 43 52 45 41 54 45 29 3b 0a 20 20 OPEN_CREATE);.
e7830 20 20 20 20 6f 70 65 6e 46 6c 61 67 73 20 26 3d openFlags &=
e7840 20 7e 28 4f 5f 52 44 57 52 7c 4f 5f 43 52 45 41 ~(O_RDWR|O_CREA
e7850 54 29 3b 0a 20 20 20 20 20 20 66 6c 61 67 73 20 T);. flags
e7860 7c 3d 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 |= SQLITE_OPEN_R
e7870 45 41 44 4f 4e 4c 59 3b 0a 20 20 20 20 20 20 6f EADONLY;. o
e7880 70 65 6e 46 6c 61 67 73 20 7c 3d 20 4f 5f 52 44 penFlags |= O_RD
e7890 4f 4e 4c 59 3b 0a 20 20 20 20 20 20 66 64 20 3d ONLY;. fd =
e78a0 20 6f 70 65 6e 28 7a 4e 61 6d 65 2c 20 6f 70 65 open(zName, ope
e78b0 6e 46 6c 61 67 73 2c 20 6f 70 65 6e 4d 6f 64 65 nFlags, openMode
e78c0 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 );. }. if(
e78d0 20 66 64 3c 30 20 29 7b 0a 20 20 20 20 20 20 72 fd<0 ){. r
e78e0 63 20 3d 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f c = SQLITE_CANTO
e78f0 50 45 4e 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 PEN;. goto
e7900 6f 70 65 6e 5f 66 69 6e 69 73 68 65 64 3b 0a 20 open_finished;.
e7910 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72 }. }. asser
e7920 74 28 20 66 64 3e 3d 30 20 29 3b 0a 20 20 69 66 t( fd>=0 );. if
e7930 28 20 70 4f 75 74 46 6c 61 67 73 20 29 7b 0a 20 ( pOutFlags ){.
e7940 20 20 20 2a 70 4f 75 74 46 6c 61 67 73 20 3d 20 *pOutFlags =
e7950 66 6c 61 67 73 3b 0a 20 20 7d 0a 0a 20 20 69 66 flags;. }.. if
e7960 28 20 70 2d 3e 70 55 6e 75 73 65 64 20 29 7b 0a ( p->pUnused ){.
e7970 20 20 20 20 70 2d 3e 70 55 6e 75 73 65 64 2d 3e p->pUnused->
e7980 66 64 20 3d 20 66 64 3b 0a 20 20 20 20 70 2d 3e fd = fd;. p->
e7990 70 55 6e 75 73 65 64 2d 3e 66 6c 61 67 73 20 3d pUnused->flags =
e79a0 20 66 6c 61 67 73 3b 0a 20 20 7d 0a 0a 20 20 69 flags;. }.. i
e79b0 66 28 20 69 73 44 65 6c 65 74 65 20 29 7b 0a 23 f( isDelete ){.#
e79c0 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 if OS_VXWORKS.
e79d0 20 20 7a 50 61 74 68 20 3d 20 7a 4e 61 6d 65 3b zPath = zName;
e79e0 0a 23 65 6c 73 65 0a 20 20 20 20 75 6e 6c 69 6e .#else. unlin
e79f0 6b 28 7a 4e 61 6d 65 29 3b 0a 23 65 6e 64 69 66 k(zName);.#endif
e7a00 0a 20 20 7d 0a 23 69 66 20 53 51 4c 49 54 45 5f . }.#if SQLITE_
e7a10 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 ENABLE_LOCKING_S
e7a20 54 59 4c 45 0a 20 20 65 6c 73 65 7b 0a 20 20 20 TYLE. else{.
e7a30 20 70 2d 3e 6f 70 65 6e 46 6c 61 67 73 20 3d 20 p->openFlags =
e7a40 6f 70 65 6e 46 6c 61 67 73 3b 0a 20 20 7d 0a 23 openFlags;. }.#
e7a50 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 69 73 4f endif.. if( isO
e7a60 70 65 6e 44 69 72 65 63 74 6f 72 79 20 29 7b 0a penDirectory ){.
e7a70 20 20 20 20 72 63 20 3d 20 6f 70 65 6e 44 69 72 rc = openDir
e7a80 65 63 74 6f 72 79 28 7a 50 61 74 68 2c 20 26 64 ectory(zPath, &d
e7a90 69 72 66 64 29 3b 0a 20 20 20 20 69 66 28 20 72 irfd);. if( r
e7aa0 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
e7ab0 20 20 20 20 20 20 2f 2a 20 49 74 20 69 73 20 73 /* It is s
e7ac0 61 66 65 20 74 6f 20 63 6c 6f 73 65 20 66 64 20 afe to close fd
e7ad0 61 74 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 62 at this point, b
e7ae0 65 63 61 75 73 65 20 69 74 20 69 73 20 67 75 61 ecause it is gua
e7af0 72 61 6e 74 65 65 64 20 6e 6f 74 0a 20 20 20 20 ranteed not.
e7b00 20 20 2a 2a 20 74 6f 20 62 65 20 6f 70 65 6e 20 ** to be open
e7b10 6f 6e 20 61 20 64 61 74 61 62 61 73 65 20 66 69 on a database fi
e7b20 6c 65 2e 20 49 66 20 69 74 20 77 65 72 65 20 6f le. If it were o
e7b30 70 65 6e 20 6f 6e 20 61 20 64 61 74 61 62 61 73 pen on a databas
e7b40 65 20 66 69 6c 65 2c 0a 20 20 20 20 20 20 2a 2a e file,. **
e7b50 20 69 74 20 77 6f 75 6c 64 20 6e 6f 74 20 62 65 it would not be
e7b60 20 73 61 66 65 20 74 6f 20 63 6c 6f 73 65 20 61 safe to close a
e7b70 73 20 74 68 69 73 20 77 6f 75 6c 64 20 72 65 6c s this would rel
e7b80 65 61 73 65 20 61 6e 79 20 6c 6f 63 6b 73 20 68 ease any locks h
e7b90 65 6c 64 0a 20 20 20 20 20 20 2a 2a 20 6f 6e 20 eld. ** on
e7ba0 74 68 65 20 66 69 6c 65 20 62 79 20 74 68 69 73 the file by this
e7bb0 20 70 72 6f 63 65 73 73 2e 20 20 2a 2f 0a 20 20 process. */.
e7bc0 20 20 20 20 61 73 73 65 72 74 28 20 65 54 79 70 assert( eTyp
e7bd0 65 21 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d e!=SQLITE_OPEN_M
e7be0 41 49 4e 5f 44 42 20 29 3b 0a 20 20 20 20 20 20 AIN_DB );.
e7bf0 63 6c 6f 73 65 28 66 64 29 3b 20 20 20 20 20 20 close(fd);
e7c00 20 20 20 20 20 20 20 2f 2a 20 73 69 6c 65 6e 74 /* silent
e7c10 6c 79 20 6c 65 61 6b 20 69 66 20 66 61 69 6c 2c ly leak if fail,
e7c20 20 61 6c 72 65 61 64 79 20 69 6e 20 65 72 72 6f already in erro
e7c30 72 20 2a 2f 0a 20 20 20 20 20 20 67 6f 74 6f 20 r */. goto
e7c40 6f 70 65 6e 5f 66 69 6e 69 73 68 65 64 3b 0a 20 open_finished;.
e7c50 20 20 20 7d 0a 20 20 7d 0a 0a 23 69 66 64 65 66 }. }..#ifdef
e7c60 20 46 44 5f 43 4c 4f 45 58 45 43 0a 20 20 66 63 FD_CLOEXEC. fc
e7c70 6e 74 6c 28 66 64 2c 20 46 5f 53 45 54 46 44 2c ntl(fd, F_SETFD,
e7c80 20 66 63 6e 74 6c 28 66 64 2c 20 46 5f 47 45 54 fcntl(fd, F_GET
e7c90 46 44 2c 20 30 29 20 7c 20 46 44 5f 43 4c 4f 45 FD, 0) | FD_CLOE
e7ca0 58 45 43 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 XEC);.#endif..
e7cb0 6e 6f 4c 6f 63 6b 20 3d 20 65 54 79 70 65 21 3d noLock = eType!=
e7cc0 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e SQLITE_OPEN_MAIN
e7cd0 5f 44 42 3b 0a 0a 23 69 66 20 53 51 4c 49 54 45 _DB;..#if SQLITE
e7ce0 5f 50 52 45 46 45 52 5f 50 52 4f 58 59 5f 4c 4f _PREFER_PROXY_LO
e7cf0 43 4b 49 4e 47 0a 20 20 69 66 28 20 7a 50 61 74 CKING. if( zPat
e7d00 68 21 3d 4e 55 4c 4c 20 26 26 20 21 6e 6f 4c 6f h!=NULL && !noLo
e7d10 63 6b 20 26 26 20 70 56 66 73 2d 3e 78 4f 70 65 ck && pVfs->xOpe
e7d20 6e 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 65 n ){. char *e
e7d30 6e 76 66 6f 72 63 65 20 3d 20 67 65 74 65 6e 76 nvforce = getenv
e7d40 28 22 53 51 4c 49 54 45 5f 46 4f 52 43 45 5f 50 ("SQLITE_FORCE_P
e7d50 52 4f 58 59 5f 4c 4f 43 4b 49 4e 47 22 29 3b 0a ROXY_LOCKING");.
e7d60 20 20 20 20 69 6e 74 20 75 73 65 50 72 6f 78 79 int useProxy
e7d70 20 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a 20 53 51 = 0;.. /* SQ
e7d80 4c 49 54 45 5f 46 4f 52 43 45 5f 50 52 4f 58 59 LITE_FORCE_PROXY
e7d90 5f 4c 4f 43 4b 49 4e 47 3d 3d 31 20 6d 65 61 6e _LOCKING==1 mean
e7da0 73 20 66 6f 72 63 65 20 61 6c 77 61 79 73 20 75 s force always u
e7db0 73 65 20 70 72 6f 78 79 2c 20 30 20 6d 65 61 6e se proxy, 0 mean
e7dc0 73 20 0a 20 20 20 20 2a 2a 20 6e 65 76 65 72 20 s . ** never
e7dd0 75 73 65 20 70 72 6f 78 79 2c 20 4e 55 4c 4c 20 use proxy, NULL
e7de0 6d 65 61 6e 73 20 75 73 65 20 70 72 6f 78 79 20 means use proxy
e7df0 66 6f 72 20 6e 6f 6e 2d 6c 6f 63 61 6c 20 66 69 for non-local fi
e7e00 6c 65 73 20 6f 6e 6c 79 2e 20 20 2a 2f 0a 20 20 les only. */.
e7e10 20 20 69 66 28 20 65 6e 76 66 6f 72 63 65 21 3d if( envforce!=
e7e20 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20 75 73 NULL ){. us
e7e30 65 50 72 6f 78 79 20 3d 20 61 74 6f 69 28 65 6e eProxy = atoi(en
e7e40 76 66 6f 72 63 65 29 3e 30 3b 0a 20 20 20 20 7d vforce)>0;. }
e7e50 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 74 72 75 else{. stru
e7e60 63 74 20 73 74 61 74 66 73 20 66 73 49 6e 66 6f ct statfs fsInfo
e7e70 3b 0a 20 20 20 20 20 20 69 66 28 20 73 74 61 74 ;. if( stat
e7e80 66 73 28 7a 50 61 74 68 2c 20 26 66 73 49 6e 66 fs(zPath, &fsInf
e7e90 6f 29 20 3d 3d 20 2d 31 20 29 7b 0a 20 20 20 20 o) == -1 ){.
e7ea0 20 20 20 20 2f 2a 20 49 6e 20 74 68 65 6f 72 79 /* In theory
e7eb0 2c 20 74 68 65 20 63 6c 6f 73 65 28 66 64 29 20 , the close(fd)
e7ec0 63 61 6c 6c 20 69 73 20 73 75 62 2d 6f 70 74 69 call is sub-opti
e7ed0 6d 61 6c 2e 20 49 66 20 74 68 65 20 66 69 6c 65 mal. If the file
e7ee0 20 6f 70 65 6e 65 64 0a 20 20 20 20 20 20 20 20 opened.
e7ef0 2a 2a 20 77 69 74 68 20 66 64 20 69 73 20 61 20 ** with fd is a
e7f00 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 61 database file, a
e7f10 6e 64 20 74 68 65 72 65 20 61 72 65 20 6f 74 68 nd there are oth
e7f20 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 6f er connections o
e7f30 70 65 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f pen. ** o
e7f40 6e 20 74 68 61 74 20 66 69 6c 65 20 74 68 61 74 n that file that
e7f50 20 61 72 65 20 63 75 72 72 65 6e 74 6c 79 20 68 are currently h
e7f60 6f 6c 64 69 6e 67 20 61 64 76 69 73 6f 72 79 20 olding advisory
e7f70 6c 6f 63 6b 73 20 6f 6e 20 69 74 2c 0a 20 20 20 locks on it,.
e7f80 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 74 68 65 ** then the
e7f90 20 63 61 6c 6c 20 74 6f 20 63 6c 6f 73 65 28 29 call to close()
e7fa0 20 77 69 6c 6c 20 63 61 6e 63 65 6c 20 74 68 6f will cancel tho
e7fb0 73 65 20 6c 6f 63 6b 73 2e 20 49 6e 20 70 72 61 se locks. In pra
e7fc0 63 74 69 63 65 2c 0a 20 20 20 20 20 20 20 20 2a ctice,. *
e7fd0 2a 20 77 65 27 72 65 20 61 73 73 75 6d 69 6e 67 * we're assuming
e7fe0 20 74 68 61 74 20 73 74 61 74 66 73 28 29 20 64 that statfs() d
e7ff0 6f 65 73 6e 27 74 20 66 61 69 6c 20 76 65 72 79 oesn't fail very
e8000 20 6f 66 74 65 6e 2e 20 41 74 20 6c 65 61 73 74 often. At least
e8010 0a 20 20 20 20 20 20 20 20 2a 2a 20 6e 6f 74 20 . ** not
e8020 77 68 69 6c 65 20 6f 74 68 65 72 20 66 69 6c 65 while other file
e8030 20 64 65 73 63 72 69 70 74 6f 72 73 20 6f 70 65 descriptors ope
e8040 6e 65 64 20 62 79 20 74 68 65 20 73 61 6d 65 20 ned by the same
e8050 70 72 6f 63 65 73 73 20 6f 6e 0a 20 20 20 20 20 process on.
e8060 20 20 20 2a 2a 20 74 68 65 20 73 61 6d 65 20 66 ** the same f
e8070 69 6c 65 20 61 72 65 20 77 6f 72 6b 69 6e 67 2e ile are working.
e8080 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 2d 3e */. p->
e8090 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e lastErrno = errn
e80a0 6f 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 64 o;. if( d
e80b0 69 72 66 64 3e 3d 30 20 29 7b 0a 20 20 20 20 20 irfd>=0 ){.
e80c0 20 20 20 20 20 63 6c 6f 73 65 28 64 69 72 66 64 close(dirfd
e80d0 29 3b 20 2f 2a 20 73 69 6c 65 6e 74 6c 79 20 6c ); /* silently l
e80e0 65 61 6b 20 69 66 20 66 61 69 6c 2c 20 69 6e 20 eak if fail, in
e80f0 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20 20 20 20 error */.
e8100 20 7d 0a 20 20 20 20 20 20 20 20 63 6c 6f 73 65 }. close
e8110 28 66 64 29 3b 20 2f 2a 20 73 69 6c 65 6e 74 6c (fd); /* silentl
e8120 79 20 6c 65 61 6b 20 69 66 20 66 61 69 6c 2c 20 y leak if fail,
e8130 69 6e 20 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20 in error */.
e8140 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
e8150 49 4f 45 52 52 5f 41 43 43 45 53 53 3b 0a 20 20 IOERR_ACCESS;.
e8160 20 20 20 20 20 20 67 6f 74 6f 20 6f 70 65 6e 5f goto open_
e8170 66 69 6e 69 73 68 65 64 3b 0a 20 20 20 20 20 20 finished;.
e8180 7d 0a 20 20 20 20 20 20 75 73 65 50 72 6f 78 79 }. useProxy
e8190 20 3d 20 21 28 66 73 49 6e 66 6f 2e 66 5f 66 6c = !(fsInfo.f_fl
e81a0 61 67 73 26 4d 4e 54 5f 4c 4f 43 41 4c 29 3b 0a ags&MNT_LOCAL);.
e81b0 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 75 73 }. if( us
e81c0 65 50 72 6f 78 79 20 29 7b 0a 20 20 20 20 20 20 eProxy ){.
e81d0 72 63 20 3d 20 66 69 6c 6c 49 6e 55 6e 69 78 46 rc = fillInUnixF
e81e0 69 6c 65 28 70 56 66 73 2c 20 66 64 2c 20 64 69 ile(pVfs, fd, di
e81f0 72 66 64 2c 20 70 46 69 6c 65 2c 20 7a 50 61 74 rfd, pFile, zPat
e8200 68 2c 20 6e 6f 4c 6f 63 6b 2c 20 69 73 44 65 6c h, noLock, isDel
e8210 65 74 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 ete);. if(
e8220 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
e8230 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 72 . rc = pr
e8240 6f 78 79 54 72 61 6e 73 66 6f 72 6d 55 6e 69 78 oxyTransformUnix
e8250 46 69 6c 65 28 28 75 6e 69 78 46 69 6c 65 2a 29 File((unixFile*)
e8260 70 46 69 6c 65 2c 20 22 3a 61 75 74 6f 3a 22 29 pFile, ":auto:")
e8270 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
e8280 67 6f 74 6f 20 6f 70 65 6e 5f 66 69 6e 69 73 68 goto open_finish
e8290 65 64 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 ed;. }. }.#e
e82a0 6e 64 69 66 0a 20 20 0a 20 20 72 63 20 3d 20 66 ndif. . rc = f
e82b0 69 6c 6c 49 6e 55 6e 69 78 46 69 6c 65 28 70 56 illInUnixFile(pV
e82c0 66 73 2c 20 66 64 2c 20 64 69 72 66 64 2c 20 70 fs, fd, dirfd, p
e82d0 46 69 6c 65 2c 20 7a 50 61 74 68 2c 20 6e 6f 4c File, zPath, noL
e82e0 6f 63 6b 2c 20 69 73 44 65 6c 65 74 65 29 3b 0a ock, isDelete);.
e82f0 6f 70 65 6e 5f 66 69 6e 69 73 68 65 64 3a 0a 20 open_finished:.
e8300 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
e8310 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 OK ){. sqlite
e8320 33 5f 66 72 65 65 28 70 2d 3e 70 55 6e 75 73 65 3_free(p->pUnuse
e8330 64 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e d);. }. return
e8340 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 44 rc;.}.../*.** D
e8350 65 6c 65 74 65 20 74 68 65 20 66 69 6c 65 20 61 elete the file a
e8360 74 20 7a 50 61 74 68 2e 20 49 66 20 74 68 65 20 t zPath. If the
e8370 64 69 72 53 79 6e 63 20 61 72 67 75 6d 65 6e 74 dirSync argument
e8380 20 69 73 20 74 72 75 65 2c 20 66 73 79 6e 63 28 is true, fsync(
e8390 29 0a 2a 2a 20 74 68 65 20 64 69 72 65 63 74 6f ).** the directo
e83a0 72 79 20 61 66 74 65 72 20 64 65 6c 65 74 69 6e ry after deletin
e83b0 67 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 g the file..*/.s
e83c0 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 44 65 tatic int unixDe
e83d0 6c 65 74 65 28 0a 20 20 73 71 6c 69 74 65 33 5f lete(. sqlite3_
e83e0 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20 20 20 vfs *NotUsed,
e83f0 20 20 2f 2a 20 56 46 53 20 63 6f 6e 74 61 69 6e /* VFS contain
e8400 69 6e 67 20 74 68 69 73 20 61 73 20 74 68 65 20 ing this as the
e8410 78 44 65 6c 65 74 65 20 6d 65 74 68 6f 64 20 2a xDelete method *
e8420 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
e8430 7a 50 61 74 68 2c 20 20 20 20 20 20 20 20 2f 2a zPath, /*
e8440 20 4e 61 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f Name of file to
e8450 20 62 65 20 64 65 6c 65 74 65 64 20 2a 2f 0a 20 be deleted */.
e8460 20 69 6e 74 20 64 69 72 53 79 6e 63 20 20 20 20 int dirSync
e8470 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 /* If
e8480 20 74 72 75 65 2c 20 66 73 79 6e 63 28 29 20 64 true, fsync() d
e8490 69 72 65 63 74 6f 72 79 20 61 66 74 65 72 20 64 irectory after d
e84a0 65 6c 65 74 69 6e 67 20 66 69 6c 65 20 2a 2f 0a eleting file */.
e84b0 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 ){. int rc = SQ
e84c0 4c 49 54 45 5f 4f 4b 3b 0a 20 20 55 4e 55 53 45 LITE_OK;. UNUSE
e84d0 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 D_PARAMETER(NotU
e84e0 73 65 64 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 sed);. Simulate
e84f0 49 4f 45 72 72 6f 72 28 72 65 74 75 72 6e 20 53 IOError(return S
e8500 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44 45 4c 45 QLITE_IOERR_DELE
e8510 54 45 29 3b 0a 20 20 75 6e 6c 69 6e 6b 28 7a 50 TE);. unlink(zP
e8520 61 74 68 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 ath);.#ifndef SQ
e8530 4c 49 54 45 5f 44 49 53 41 42 4c 45 5f 44 49 52 LITE_DISABLE_DIR
e8540 53 59 4e 43 0a 20 20 69 66 28 20 64 69 72 53 79 SYNC. if( dirSy
e8550 6e 63 20 29 7b 0a 20 20 20 20 69 6e 74 20 66 64 nc ){. int fd
e8560 3b 0a 20 20 20 20 72 63 20 3d 20 6f 70 65 6e 44 ;. rc = openD
e8570 69 72 65 63 74 6f 72 79 28 7a 50 61 74 68 2c 20 irectory(zPath,
e8580 26 66 64 29 3b 0a 20 20 20 20 69 66 28 20 72 63 &fd);. if( rc
e8590 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 23 ==SQLITE_OK ){.#
e85a0 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 if OS_VXWORKS.
e85b0 20 20 20 20 69 66 28 20 66 73 79 6e 63 28 66 64 if( fsync(fd
e85c0 29 3d 3d 2d 31 20 29 0a 23 65 6c 73 65 0a 20 20 )==-1 ).#else.
e85d0 20 20 20 20 69 66 28 20 66 73 79 6e 63 28 66 64 if( fsync(fd
e85e0 29 20 29 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 ) ).#endif.
e85f0 20 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 {. rc =
e8600 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44 49 52 SQLITE_IOERR_DIR
e8610 5f 46 53 59 4e 43 3b 0a 20 20 20 20 20 20 7d 0a _FSYNC;. }.
e8620 20 20 20 20 20 20 69 66 28 20 63 6c 6f 73 65 28 if( close(
e8630 66 64 29 26 26 21 72 63 20 29 7b 0a 20 20 20 20 fd)&&!rc ){.
e8640 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
e8650 49 4f 45 52 52 5f 44 49 52 5f 43 4c 4f 53 45 3b IOERR_DIR_CLOSE;
e8660 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
e8670 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 }.#endif. retu
e8680 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
e8690 54 65 73 74 20 74 68 65 20 65 78 69 73 74 61 6e Test the existan
e86a0 63 65 20 6f 66 20 6f 72 20 61 63 63 65 73 73 20 ce of or access
e86b0 70 65 72 6d 69 73 73 69 6f 6e 73 20 6f 66 20 66 permissions of f
e86c0 69 6c 65 20 7a 50 61 74 68 2e 20 54 68 65 0a 2a ile zPath. The.*
e86d0 2a 20 74 65 73 74 20 70 65 72 66 6f 72 6d 65 64 * test performed
e86e0 20 64 65 70 65 6e 64 73 20 6f 6e 20 74 68 65 20 depends on the
e86f0 76 61 6c 75 65 20 6f 66 20 66 6c 61 67 73 3a 0a value of flags:.
e8700 2a 2a 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 **.** SQLITE
e8710 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 3a 20 _ACCESS_EXISTS:
e8720 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 20 Return 1 if the
e8730 66 69 6c 65 20 65 78 69 73 74 73 0a 2a 2a 20 20 file exists.**
e8740 20 20 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 SQLITE_ACCESS
e8750 5f 52 45 41 44 57 52 49 54 45 3a 20 52 65 74 75 _READWRITE: Retu
e8760 72 6e 20 31 20 69 66 20 74 68 65 20 66 69 6c 65 rn 1 if the file
e8770 20 69 73 20 72 65 61 64 20 61 6e 64 20 77 72 69 is read and wri
e8780 74 61 62 6c 65 2e 0a 2a 2a 20 20 20 20 20 53 51 table..** SQ
e8790 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 LITE_ACCESS_READ
e87a0 4f 4e 4c 59 3a 20 52 65 74 75 72 6e 20 31 20 69 ONLY: Return 1 i
e87b0 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 72 65 f the file is re
e87c0 61 64 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 4f 74 adable..**.** Ot
e87d0 68 65 72 77 69 73 65 20 72 65 74 75 72 6e 20 30 herwise return 0
e87e0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
e87f0 75 6e 69 78 41 63 63 65 73 73 28 0a 20 20 73 71 unixAccess(. sq
e8800 6c 69 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 lite3_vfs *NotUs
e8810 65 64 2c 20 20 20 2f 2a 20 54 68 65 20 56 46 53 ed, /* The VFS
e8820 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 69 73 containing this
e8830 20 78 41 63 63 65 73 73 20 6d 65 74 68 6f 64 20 xAccess method
e8840 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
e8850 2a 7a 50 61 74 68 2c 20 20 20 20 20 20 2f 2a 20 *zPath, /*
e8860 50 61 74 68 20 6f 66 20 74 68 65 20 66 69 6c 65 Path of the file
e8870 20 74 6f 20 65 78 61 6d 69 6e 65 20 2a 2f 0a 20 to examine */.
e8880 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20 int flags,
e8890 20 20 20 20 20 20 20 20 20 2f 2a 20 57 68 61 74 /* What
e88a0 20 64 6f 20 77 65 20 77 61 6e 74 20 74 6f 20 6c do we want to l
e88b0 65 61 72 6e 20 61 62 6f 75 74 20 74 68 65 20 7a earn about the z
e88c0 50 61 74 68 20 66 69 6c 65 3f 20 2a 2f 0a 20 20 Path file? */.
e88d0 69 6e 74 20 2a 70 52 65 73 4f 75 74 20 20 20 20 int *pResOut
e88e0 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 /* Write
e88f0 20 72 65 73 75 6c 74 20 62 6f 6f 6c 65 61 6e 20 result boolean
e8900 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 here */.){. int
e8910 20 61 6d 6f 64 65 20 3d 20 30 3b 0a 20 20 55 4e amode = 0;. UN
e8920 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e USED_PARAMETER(N
e8930 6f 74 55 73 65 64 29 3b 0a 20 20 53 69 6d 75 6c otUsed);. Simul
e8940 61 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75 ateIOError( retu
e8950 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f rn SQLITE_IOERR_
e8960 41 43 43 45 53 53 3b 20 29 3b 0a 20 20 73 77 69 ACCESS; );. swi
e8970 74 63 68 28 20 66 6c 61 67 73 20 29 7b 0a 20 20 tch( flags ){.
e8980 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 41 43 case SQLITE_AC
e8990 43 45 53 53 5f 45 58 49 53 54 53 3a 0a 20 20 20 CESS_EXISTS:.
e89a0 20 20 20 61 6d 6f 64 65 20 3d 20 46 5f 4f 4b 3b amode = F_OK;
e89b0 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
e89c0 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 41 43 case SQLITE_AC
e89d0 43 45 53 53 5f 52 45 41 44 57 52 49 54 45 3a 0a CESS_READWRITE:.
e89e0 20 20 20 20 20 20 61 6d 6f 64 65 20 3d 20 57 5f amode = W_
e89f0 4f 4b 7c 52 5f 4f 4b 3b 0a 20 20 20 20 20 20 62 OK|R_OK;. b
e8a00 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53 reak;. case S
e8a10 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 QLITE_ACCESS_REA
e8a20 44 3a 0a 20 20 20 20 20 20 61 6d 6f 64 65 20 3d D:. amode =
e8a30 20 52 5f 4f 4b 3b 0a 20 20 20 20 20 20 62 72 65 R_OK;. bre
e8a40 61 6b 3b 0a 0a 20 20 20 20 64 65 66 61 75 6c 74 ak;.. default
e8a50 3a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 21 :. assert(!
e8a60 22 49 6e 76 61 6c 69 64 20 66 6c 61 67 73 20 61 "Invalid flags a
e8a70 72 67 75 6d 65 6e 74 22 29 3b 0a 20 20 7d 0a 20 rgument");. }.
e8a80 20 2a 70 52 65 73 4f 75 74 20 3d 20 28 61 63 63 *pResOut = (acc
e8a90 65 73 73 28 7a 50 61 74 68 2c 20 61 6d 6f 64 65 ess(zPath, amode
e8aa0 29 3d 3d 30 29 3b 0a 20 20 72 65 74 75 72 6e 20 )==0);. return
e8ab0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f SQLITE_OK;.}.../
e8ac0 2a 0a 2a 2a 20 54 75 72 6e 20 61 20 72 65 6c 61 *.** Turn a rela
e8ad0 74 69 76 65 20 70 61 74 68 6e 61 6d 65 20 69 6e tive pathname in
e8ae0 74 6f 20 61 20 66 75 6c 6c 20 70 61 74 68 6e 61 to a full pathna
e8af0 6d 65 2e 20 54 68 65 20 72 65 6c 61 74 69 76 65 me. The relative
e8b00 20 70 61 74 68 0a 2a 2a 20 69 73 20 73 74 6f 72 path.** is stor
e8b10 65 64 20 61 73 20 61 20 6e 75 6c 2d 74 65 72 6d ed as a nul-term
e8b20 69 6e 61 74 65 64 20 73 74 72 69 6e 67 20 69 6e inated string in
e8b30 20 74 68 65 20 62 75 66 66 65 72 20 70 6f 69 6e the buffer poin
e8b40 74 65 64 20 74 6f 20 62 79 0a 2a 2a 20 7a 50 61 ted to by.** zPa
e8b50 74 68 2e 20 0a 2a 2a 0a 2a 2a 20 7a 4f 75 74 20 th. .**.** zOut
e8b60 70 6f 69 6e 74 73 20 74 6f 20 61 20 62 75 66 66 points to a buff
e8b70 65 72 20 6f 66 20 61 74 20 6c 65 61 73 74 20 73 er of at least s
e8b80 71 6c 69 74 65 33 5f 76 66 73 2e 6d 78 50 61 74 qlite3_vfs.mxPat
e8b90 68 6e 61 6d 65 20 62 79 74 65 73 20 0a 2a 2a 20 hname bytes .**
e8ba0 28 69 6e 20 74 68 69 73 20 63 61 73 65 2c 20 4d (in this case, M
e8bb0 41 58 5f 50 41 54 48 4e 41 4d 45 20 62 79 74 65 AX_PATHNAME byte
e8bc0 73 29 2e 20 54 68 65 20 66 75 6c 6c 2d 70 61 74 s). The full-pat
e8bd0 68 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f 0a h is written to.
e8be0 2a 2a 20 74 68 69 73 20 62 75 66 66 65 72 20 62 ** this buffer b
e8bf0 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e efore returning.
e8c00 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 .*/.static int u
e8c10 6e 69 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 nixFullPathname(
e8c20 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a . sqlite3_vfs *
e8c30 70 56 66 73 2c 20 20 20 20 20 20 20 20 20 20 20 pVfs,
e8c40 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 76 /* Pointer to v
e8c50 66 73 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 63 fs object */. c
e8c60 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74 68 onst char *zPath
e8c70 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 , /*
e8c80 50 6f 73 73 69 62 6c 79 20 72 65 6c 61 74 69 76 Possibly relativ
e8c90 65 20 69 6e 70 75 74 20 70 61 74 68 20 2a 2f 0a e input path */.
e8ca0 20 20 69 6e 74 20 6e 4f 75 74 2c 20 20 20 20 20 int nOut,
e8cb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e8cc0 2f 2a 20 53 69 7a 65 20 6f 66 20 6f 75 74 70 75 /* Size of outpu
e8cd0 74 20 62 75 66 66 65 72 20 69 6e 20 62 79 74 65 t buffer in byte
e8ce0 73 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4f 75 s */. char *zOu
e8cf0 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t
e8d00 20 20 20 20 20 2f 2a 20 4f 75 74 70 75 74 20 62 /* Output b
e8d10 75 66 66 65 72 20 2a 2f 0a 29 7b 0a 0a 20 20 2f uffer */.){.. /
e8d20 2a 20 49 74 27 73 20 6f 64 64 20 74 6f 20 73 69 * It's odd to si
e8d30 6d 75 6c 61 74 65 20 61 6e 20 69 6f 2d 65 72 72 mulate an io-err
e8d40 6f 72 20 68 65 72 65 2c 20 62 75 74 20 72 65 61 or here, but rea
e8d50 6c 6c 79 20 74 68 69 73 20 69 73 20 6a 75 73 74 lly this is just
e8d60 0a 20 20 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 . ** using the
e8d70 69 6f 2d 65 72 72 6f 72 20 69 6e 66 72 61 73 74 io-error infrast
e8d80 72 75 63 74 75 72 65 20 74 6f 20 74 65 73 74 20 ructure to test
e8d90 74 68 61 74 20 53 51 4c 69 74 65 20 68 61 6e 64 that SQLite hand
e8da0 6c 65 73 20 74 68 69 73 0a 20 20 2a 2a 20 66 75 les this. ** fu
e8db0 6e 63 74 69 6f 6e 20 66 61 69 6c 69 6e 67 2e 20 nction failing.
e8dc0 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 6f This function co
e8dd0 75 6c 64 20 66 61 69 6c 20 69 66 2c 20 66 6f 72 uld fail if, for
e8de0 20 65 78 61 6d 70 6c 65 2c 20 74 68 65 0a 20 20 example, the.
e8df0 2a 2a 20 63 75 72 72 65 6e 74 20 77 6f 72 6b 69 ** current worki
e8e00 6e 67 20 64 69 72 65 63 74 6f 72 79 20 68 61 73 ng directory has
e8e10 20 62 65 65 6e 20 75 6e 6c 69 6e 6b 65 64 2e 0a been unlinked..
e8e20 20 20 2a 2f 0a 20 20 53 69 6d 75 6c 61 74 65 49 */. SimulateI
e8e30 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 OError( return S
e8e40 51 4c 49 54 45 5f 45 52 52 4f 52 20 29 3b 0a 0a QLITE_ERROR );..
e8e50 20 20 61 73 73 65 72 74 28 20 70 56 66 73 2d 3e assert( pVfs->
e8e60 6d 78 50 61 74 68 6e 61 6d 65 3d 3d 4d 41 58 5f mxPathname==MAX_
e8e70 50 41 54 48 4e 41 4d 45 20 29 3b 0a 20 20 55 4e PATHNAME );. UN
e8e80 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 USED_PARAMETER(p
e8e90 56 66 73 29 3b 0a 0a 20 20 7a 4f 75 74 5b 6e 4f Vfs);.. zOut[nO
e8ea0 75 74 2d 31 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 ut-1] = '\0';.
e8eb0 69 66 28 20 7a 50 61 74 68 5b 30 5d 3d 3d 27 2f if( zPath[0]=='/
e8ec0 27 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 ' ){. sqlite3
e8ed0 5f 73 6e 70 72 69 6e 74 66 28 6e 4f 75 74 2c 20 _snprintf(nOut,
e8ee0 7a 4f 75 74 2c 20 22 25 73 22 2c 20 7a 50 61 74 zOut, "%s", zPat
e8ef0 68 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 h);. }else{.
e8f00 20 69 6e 74 20 6e 43 77 64 3b 0a 20 20 20 20 69 int nCwd;. i
e8f10 66 28 20 67 65 74 63 77 64 28 7a 4f 75 74 2c 20 f( getcwd(zOut,
e8f20 6e 4f 75 74 2d 31 29 3d 3d 30 20 29 7b 0a 20 20 nOut-1)==0 ){.
e8f30 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
e8f40 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20 20 20 20 E_CANTOPEN;.
e8f50 7d 0a 20 20 20 20 6e 43 77 64 20 3d 20 28 69 6e }. nCwd = (in
e8f60 74 29 73 74 72 6c 65 6e 28 7a 4f 75 74 29 3b 0a t)strlen(zOut);.
e8f70 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 sqlite3_snpr
e8f80 69 6e 74 66 28 6e 4f 75 74 2d 6e 43 77 64 2c 20 intf(nOut-nCwd,
e8f90 26 7a 4f 75 74 5b 6e 43 77 64 5d 2c 20 22 2f 25 &zOut[nCwd], "/%
e8fa0 73 22 2c 20 7a 50 61 74 68 29 3b 0a 20 20 7d 0a s", zPath);. }.
e8fb0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
e8fc0 4f 4b 3b 0a 7d 0a 0a 0a 23 69 66 6e 64 65 66 20 OK;.}...#ifndef
e8fd0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 SQLITE_OMIT_LOAD
e8fe0 5f 45 58 54 45 4e 53 49 4f 4e 0a 2f 2a 0a 2a 2a _EXTENSION./*.**
e8ff0 20 49 6e 74 65 72 66 61 63 65 73 20 66 6f 72 20 Interfaces for
e9000 6f 70 65 6e 69 6e 67 20 61 20 73 68 61 72 65 64 opening a shared
e9010 20 6c 69 62 72 61 72 79 2c 20 66 69 6e 64 69 6e library, findin
e9020 67 20 65 6e 74 72 79 20 70 6f 69 6e 74 73 0a 2a g entry points.*
e9030 2a 20 77 69 74 68 69 6e 20 74 68 65 20 73 68 61 * within the sha
e9040 72 65 64 20 6c 69 62 72 61 72 79 2c 20 61 6e 64 red library, and
e9050 20 63 6c 6f 73 69 6e 67 20 74 68 65 20 73 68 61 closing the sha
e9060 72 65 64 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a red library..*/.
e9070 23 69 6e 63 6c 75 64 65 20 3c 64 6c 66 63 6e 2e #include <dlfcn.
e9080 68 3e 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a h>.static void *
e9090 75 6e 69 78 44 6c 4f 70 65 6e 28 73 71 6c 69 74 unixDlOpen(sqlit
e90a0 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c e3_vfs *NotUsed,
e90b0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 const char *zFi
e90c0 6c 65 6e 61 6d 65 29 7b 0a 20 20 55 4e 55 53 45 lename){. UNUSE
e90d0 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 D_PARAMETER(NotU
e90e0 73 65 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 64 sed);. return d
e90f0 6c 6f 70 65 6e 28 7a 46 69 6c 65 6e 61 6d 65 2c lopen(zFilename,
e9100 20 52 54 4c 44 5f 4e 4f 57 20 7c 20 52 54 4c 44 RTLD_NOW | RTLD
e9110 5f 47 4c 4f 42 41 4c 29 3b 0a 7d 0a 0a 2f 2a 0a _GLOBAL);.}../*.
e9120 2a 2a 20 53 51 4c 69 74 65 20 63 61 6c 6c 73 20 ** SQLite calls
e9130 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 6d this function im
e9140 6d 65 64 69 61 74 65 6c 79 20 61 66 74 65 72 20 mediately after
e9150 61 20 63 61 6c 6c 20 74 6f 20 75 6e 69 78 44 6c a call to unixDl
e9160 53 79 6d 28 29 20 6f 72 0a 2a 2a 20 75 6e 69 78 Sym() or.** unix
e9170 44 6c 4f 70 65 6e 28 29 20 66 61 69 6c 73 20 28 DlOpen() fails (
e9180 72 65 74 75 72 6e 73 20 61 20 6e 75 6c 6c 20 70 returns a null p
e9190 6f 69 6e 74 65 72 29 2e 20 49 66 20 61 20 6d 6f ointer). If a mo
e91a0 72 65 20 64 65 74 61 69 6c 65 64 20 65 72 72 6f re detailed erro
e91b0 72 0a 2a 2a 20 6d 65 73 73 61 67 65 20 69 73 20 r.** message is
e91c0 61 76 61 69 6c 61 62 6c 65 2c 20 69 74 20 69 73 available, it is
e91d0 20 77 72 69 74 74 65 6e 20 74 6f 20 7a 42 75 66 written to zBuf
e91e0 4f 75 74 2e 20 49 66 20 6e 6f 20 65 72 72 6f 72 Out. If no error
e91f0 20 6d 65 73 73 61 67 65 0a 2a 2a 20 69 73 20 61 message.** is a
e9200 76 61 69 6c 61 62 6c 65 2c 20 7a 42 75 66 4f 75 vailable, zBufOu
e9210 74 20 69 73 20 6c 65 66 74 20 75 6e 6d 6f 64 69 t is left unmodi
e9220 66 69 65 64 20 61 6e 64 20 53 51 4c 69 74 65 20 fied and SQLite
e9230 75 73 65 73 20 61 20 64 65 66 61 75 6c 74 0a 2a uses a default.*
e9240 2a 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 2e * error message.
e9250 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
e9260 75 6e 69 78 44 6c 45 72 72 6f 72 28 73 71 6c 69 unixDlError(sqli
e9270 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 te3_vfs *NotUsed
e9280 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 , int nBuf, char
e9290 20 2a 7a 42 75 66 4f 75 74 29 7b 0a 20 20 63 68 *zBufOut){. ch
e92a0 61 72 20 2a 7a 45 72 72 3b 0a 20 20 55 4e 55 53 ar *zErr;. UNUS
e92b0 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 ED_PARAMETER(Not
e92c0 55 73 65 64 29 3b 0a 20 20 75 6e 69 78 45 6e 74 Used);. unixEnt
e92d0 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 7a 45 72 erMutex();. zEr
e92e0 72 20 3d 20 64 6c 65 72 72 6f 72 28 29 3b 0a 20 r = dlerror();.
e92f0 20 69 66 28 20 7a 45 72 72 20 29 7b 0a 20 20 20 if( zErr ){.
e9300 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 sqlite3_snprint
e9310 66 28 6e 42 75 66 2c 20 7a 42 75 66 4f 75 74 2c f(nBuf, zBufOut,
e9320 20 22 25 73 22 2c 20 7a 45 72 72 29 3b 0a 20 20 "%s", zErr);.
e9330 7d 0a 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 }. unixLeaveMut
e9340 65 78 28 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 ex();.}.static v
e9350 6f 69 64 20 28 2a 75 6e 69 78 44 6c 53 79 6d 28 oid (*unixDlSym(
e9360 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e 6f 74 sqlite3_vfs *Not
e9370 55 73 65 64 2c 20 76 6f 69 64 20 2a 70 2c 20 63 Used, void *p, c
e9380 6f 6e 73 74 20 63 68 61 72 2a 7a 53 79 6d 29 29 onst char*zSym))
e9390 28 76 6f 69 64 29 7b 0a 20 20 2f 2a 20 0a 20 20 (void){. /* .
e93a0 2a 2a 20 47 43 43 20 77 69 74 68 20 2d 70 65 64 ** GCC with -ped
e93b0 61 6e 74 69 63 2d 65 72 72 6f 72 73 20 73 61 79 antic-errors say
e93c0 73 20 74 68 61 74 20 43 39 30 20 64 6f 65 73 20 s that C90 does
e93d0 6e 6f 74 20 61 6c 6c 6f 77 20 61 20 76 6f 69 64 not allow a void
e93e0 2a 20 74 6f 20 62 65 0a 20 20 2a 2a 20 63 61 73 * to be. ** cas
e93f0 74 20 69 6e 74 6f 20 61 20 70 6f 69 6e 74 65 72 t into a pointer
e9400 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 2e 20 to a function.
e9410 20 41 6e 64 20 79 65 74 20 74 68 65 20 6c 69 62 And yet the lib
e9420 72 61 72 79 20 64 6c 73 79 6d 28 29 20 72 6f 75 rary dlsym() rou
e9430 74 69 6e 65 0a 20 20 2a 2a 20 72 65 74 75 72 6e tine. ** return
e9440 73 20 61 20 76 6f 69 64 2a 20 77 68 69 63 68 20 s a void* which
e9450 69 73 20 72 65 61 6c 6c 79 20 61 20 70 6f 69 6e is really a poin
e9460 74 65 72 20 74 6f 20 61 20 66 75 6e 63 74 69 6f ter to a functio
e9470 6e 2e 20 20 53 6f 20 68 6f 77 20 64 6f 20 77 65 n. So how do we
e9480 0a 20 20 2a 2a 20 75 73 65 20 64 6c 73 79 6d 28 . ** use dlsym(
e9490 29 20 77 69 74 68 20 2d 70 65 64 61 6e 74 69 63 ) with -pedantic
e94a0 2d 65 72 72 6f 72 73 3f 0a 20 20 2a 2a 0a 20 20 -errors?. **.
e94b0 2a 2a 20 56 61 72 69 61 62 6c 65 20 78 20 62 65 ** Variable x be
e94c0 6c 6f 77 20 69 73 20 64 65 66 69 6e 65 64 20 74 low is defined t
e94d0 6f 20 62 65 20 61 20 70 6f 69 6e 74 65 72 20 74 o be a pointer t
e94e0 6f 20 61 20 66 75 6e 63 74 69 6f 6e 20 74 61 6b o a function tak
e94f0 69 6e 67 0a 20 20 2a 2a 20 70 61 72 61 6d 65 74 ing. ** paramet
e9500 65 72 73 20 76 6f 69 64 2a 20 61 6e 64 20 63 6f ers void* and co
e9510 6e 73 74 20 63 68 61 72 2a 20 61 6e 64 20 72 65 nst char* and re
e9520 74 75 72 6e 69 6e 67 20 61 20 70 6f 69 6e 74 65 turning a pointe
e9530 72 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 2e r to a function.
e9540 0a 20 20 2a 2a 20 57 65 20 69 6e 69 74 69 61 6c . ** We initial
e9550 69 7a 65 20 78 20 62 79 20 61 73 73 69 67 6e 69 ize x by assigni
e9560 6e 67 20 69 74 20 61 20 70 6f 69 6e 74 65 72 20 ng it a pointer
e9570 74 6f 20 74 68 65 20 64 6c 73 79 6d 28 29 20 66 to the dlsym() f
e9580 75 6e 63 74 69 6f 6e 2e 0a 20 20 2a 2a 20 28 54 unction.. ** (T
e9590 68 61 74 20 61 73 73 69 67 6e 6d 65 6e 74 20 72 hat assignment r
e95a0 65 71 75 69 72 65 73 20 61 20 63 61 73 74 2e 29 equires a cast.)
e95b0 20 20 54 68 65 6e 20 77 65 20 63 61 6c 6c 20 74 Then we call t
e95c0 68 65 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 he function that
e95d0 0a 20 20 2a 2a 20 78 20 70 6f 69 6e 74 73 20 74 . ** x points t
e95e0 6f 2e 20 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 o. . **. ** T
e95f0 68 69 73 20 77 6f 72 6b 2d 61 72 6f 75 6e 64 20 his work-around
e9600 69 73 20 75 6e 6c 69 6b 65 6c 79 20 74 6f 20 77 is unlikely to w
e9610 6f 72 6b 20 63 6f 72 72 65 63 74 6c 79 20 6f 6e ork correctly on
e9620 20 61 6e 79 20 73 79 73 74 65 6d 20 77 68 65 72 any system wher
e9630 65 0a 20 20 2a 2a 20 79 6f 75 20 72 65 61 6c 6c e. ** you reall
e9640 79 20 63 61 6e 6e 6f 74 20 63 61 73 74 20 61 20 y cannot cast a
e9650 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 function pointer
e9660 20 69 6e 74 6f 20 76 6f 69 64 2a 2e 20 20 42 75 into void*. Bu
e9670 74 20 74 68 65 6e 2c 20 6f 6e 20 74 68 65 0a 20 t then, on the.
e9680 20 2a 2a 20 6f 74 68 65 72 20 68 61 6e 64 2c 20 ** other hand,
e9690 64 6c 73 79 6d 28 29 20 77 69 6c 6c 20 6e 6f 74 dlsym() will not
e96a0 20 77 6f 72 6b 20 6f 6e 20 73 75 63 68 20 61 20 work on such a
e96b0 73 79 73 74 65 6d 20 65 69 74 68 65 72 2c 20 73 system either, s
e96c0 6f 20 77 65 20 68 61 76 65 0a 20 20 2a 2a 20 6e o we have. ** n
e96d0 6f 74 20 72 65 61 6c 6c 79 20 6c 6f 73 74 20 61 ot really lost a
e96e0 6e 79 74 68 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 nything.. */.
e96f0 76 6f 69 64 20 28 2a 28 2a 78 29 28 76 6f 69 64 void (*(*x)(void
e9700 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 29 28 *,const char*))(
e9710 76 6f 69 64 29 3b 0a 20 20 55 4e 55 53 45 44 5f void);. UNUSED_
e9720 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 PARAMETER(NotUse
e9730 64 29 3b 0a 20 20 78 20 3d 20 28 76 6f 69 64 28 d);. x = (void(
e9740 2a 28 2a 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 *(*)(void*,const
e9750 20 63 68 61 72 2a 29 29 28 76 6f 69 64 29 29 64 char*))(void))d
e9760 6c 73 79 6d 3b 0a 20 20 72 65 74 75 72 6e 20 28 lsym;. return (
e9770 2a 78 29 28 70 2c 20 7a 53 79 6d 29 3b 0a 7d 0a *x)(p, zSym);.}.
e9780 73 74 61 74 69 63 20 76 6f 69 64 20 75 6e 69 78 static void unix
e9790 44 6c 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f DlClose(sqlite3_
e97a0 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20 76 6f vfs *NotUsed, vo
e97b0 69 64 20 2a 70 48 61 6e 64 6c 65 29 7b 0a 20 20 id *pHandle){.
e97c0 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
e97d0 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 64 6c 63 (NotUsed);. dlc
e97e0 6c 6f 73 65 28 70 48 61 6e 64 6c 65 29 3b 0a 7d lose(pHandle);.}
e97f0 0a 23 65 6c 73 65 20 2f 2a 20 69 66 20 53 51 4c .#else /* if SQL
e9800 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 ITE_OMIT_LOAD_EX
e9810 54 45 4e 53 49 4f 4e 20 69 73 20 64 65 66 69 6e TENSION is defin
e9820 65 64 3a 20 2a 2f 0a 20 20 23 64 65 66 69 6e 65 ed: */. #define
e9830 20 75 6e 69 78 44 6c 4f 70 65 6e 20 20 30 0a 20 unixDlOpen 0.
e9840 20 23 64 65 66 69 6e 65 20 75 6e 69 78 44 6c 45 #define unixDlE
e9850 72 72 6f 72 20 30 0a 20 20 23 64 65 66 69 6e 65 rror 0. #define
e9860 20 75 6e 69 78 44 6c 53 79 6d 20 20 20 30 0a 20 unixDlSym 0.
e9870 20 23 64 65 66 69 6e 65 20 75 6e 69 78 44 6c 43 #define unixDlC
e9880 6c 6f 73 65 20 30 0a 23 65 6e 64 69 66 0a 0a 2f lose 0.#endif../
e9890 2a 0a 2a 2a 20 57 72 69 74 65 20 6e 42 75 66 20 *.** Write nBuf
e98a0 62 79 74 65 73 20 6f 66 20 72 61 6e 64 6f 6d 20 bytes of random
e98b0 64 61 74 61 20 74 6f 20 74 68 65 20 73 75 70 70 data to the supp
e98c0 6c 69 65 64 20 62 75 66 66 65 72 20 7a 42 75 66 lied buffer zBuf
e98d0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
e98e0 75 6e 69 78 52 61 6e 64 6f 6d 6e 65 73 73 28 73 unixRandomness(s
e98f0 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55 qlite3_vfs *NotU
e9900 73 65 64 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63 sed, int nBuf, c
e9910 68 61 72 20 2a 7a 42 75 66 29 7b 0a 20 20 55 4e har *zBuf){. UN
e9920 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e USED_PARAMETER(N
e9930 6f 74 55 73 65 64 29 3b 0a 20 20 61 73 73 65 72 otUsed);. asser
e9940 74 28 28 73 69 7a 65 5f 74 29 6e 42 75 66 3e 3d t((size_t)nBuf>=
e9950 28 73 69 7a 65 6f 66 28 74 69 6d 65 5f 74 29 2b (sizeof(time_t)+
e9960 73 69 7a 65 6f 66 28 69 6e 74 29 29 29 3b 0a 0a sizeof(int)));..
e9970 20 20 2f 2a 20 57 65 20 68 61 76 65 20 74 6f 20 /* We have to
e9980 69 6e 69 74 69 61 6c 69 7a 65 20 7a 42 75 66 20 initialize zBuf
e9990 74 6f 20 70 72 65 76 65 6e 74 20 76 61 6c 67 72 to prevent valgr
e99a0 69 6e 64 20 66 72 6f 6d 20 72 65 70 6f 72 74 69 ind from reporti
e99b0 6e 67 0a 20 20 2a 2a 20 65 72 72 6f 72 73 2e 20 ng. ** errors.
e99c0 20 54 68 65 20 72 65 70 6f 72 74 73 20 69 73 73 The reports iss
e99d0 75 65 64 20 62 79 20 76 61 6c 67 72 69 6e 64 20 ued by valgrind
e99e0 61 72 65 20 69 6e 63 6f 72 72 65 63 74 20 2d 20 are incorrect -
e99f0 77 65 20 77 6f 75 6c 64 0a 20 20 2a 2a 20 70 72 we would. ** pr
e9a00 65 66 65 72 20 74 68 61 74 20 74 68 65 20 72 61 efer that the ra
e9a10 6e 64 6f 6d 6e 65 73 73 20 62 65 20 69 6e 63 72 ndomness be incr
e9a20 65 61 73 65 64 20 62 79 20 6d 61 6b 69 6e 67 20 eased by making
e9a30 75 73 65 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 use of the. **
e9a40 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 20 73 70 uninitialized sp
e9a50 61 63 65 20 69 6e 20 7a 42 75 66 20 2d 20 62 75 ace in zBuf - bu
e9a60 74 20 76 61 6c 67 72 69 6e 64 20 65 72 72 6f 72 t valgrind error
e9a70 73 20 74 65 6e 64 20 74 6f 20 77 6f 72 72 79 0a s tend to worry.
e9a80 20 20 2a 2a 20 73 6f 6d 65 20 75 73 65 72 73 2e ** some users.
e9a90 20 20 52 61 74 68 65 72 20 74 68 61 6e 20 61 72 Rather than ar
e9aa0 67 75 65 2c 20 69 74 20 73 65 65 6d 73 20 65 61 gue, it seems ea
e9ab0 73 69 65 72 20 6a 75 73 74 20 74 6f 20 69 6e 69 sier just to ini
e9ac0 74 69 61 6c 69 7a 65 0a 20 20 2a 2a 20 74 68 65 tialize. ** the
e9ad0 20 77 68 6f 6c 65 20 61 72 72 61 79 20 61 6e 64 whole array and
e9ae0 20 73 69 6c 65 6e 63 65 20 76 61 6c 67 72 69 6e silence valgrin
e9af0 64 2c 20 65 76 65 6e 20 69 66 20 74 68 61 74 20 d, even if that
e9b00 6d 65 61 6e 73 20 6c 65 73 73 20 72 61 6e 64 6f means less rando
e9b10 6d 6e 65 73 73 0a 20 20 2a 2a 20 69 6e 20 74 68 mness. ** in th
e9b20 65 20 72 61 6e 64 6f 6d 20 73 65 65 64 2e 0a 20 e random seed..
e9b30 20 2a 2a 0a 20 20 2a 2a 20 57 68 65 6e 20 74 65 **. ** When te
e9b40 73 74 69 6e 67 2c 20 69 6e 69 74 69 61 6c 69 7a sting, initializ
e9b50 69 6e 67 20 7a 42 75 66 5b 5d 20 74 6f 20 7a 65 ing zBuf[] to ze
e9b60 72 6f 20 69 73 20 61 6c 6c 20 77 65 20 64 6f 2e ro is all we do.
e9b70 20 20 54 68 61 74 20 6d 65 61 6e 73 0a 20 20 2a That means. *
e9b80 2a 20 74 68 61 74 20 77 65 20 61 6c 77 61 79 73 * that we always
e9b90 20 75 73 65 20 74 68 65 20 73 61 6d 65 20 72 61 use the same ra
e9ba0 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 73 65 71 75 ndom number sequ
e9bb0 65 6e 63 65 2e 20 20 54 68 69 73 20 6d 61 6b 65 ence. This make
e9bc0 73 20 74 68 65 0a 20 20 2a 2a 20 74 65 73 74 73 s the. ** tests
e9bd0 20 72 65 70 65 61 74 61 62 6c 65 2e 0a 20 20 2a repeatable.. *
e9be0 2f 0a 20 20 6d 65 6d 73 65 74 28 7a 42 75 66 2c /. memset(zBuf,
e9bf0 20 30 2c 20 6e 42 75 66 29 3b 0a 23 69 66 20 21 0, nBuf);.#if !
e9c00 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 defined(SQLITE_T
e9c10 45 53 54 29 0a 20 20 7b 0a 20 20 20 20 69 6e 74 EST). {. int
e9c20 20 70 69 64 2c 20 66 64 3b 0a 20 20 20 20 66 64 pid, fd;. fd
e9c30 20 3d 20 6f 70 65 6e 28 22 2f 64 65 76 2f 75 72 = open("/dev/ur
e9c40 61 6e 64 6f 6d 22 2c 20 4f 5f 52 44 4f 4e 4c 59 andom", O_RDONLY
e9c50 29 3b 0a 20 20 20 20 69 66 28 20 66 64 3c 30 20 );. if( fd<0
e9c60 29 7b 0a 20 20 20 20 20 20 74 69 6d 65 5f 74 20 ){. time_t
e9c70 74 3b 0a 20 20 20 20 20 20 74 69 6d 65 28 26 74 t;. time(&t
e9c80 29 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 );. memcpy(
e9c90 7a 42 75 66 2c 20 26 74 2c 20 73 69 7a 65 6f 66 zBuf, &t, sizeof
e9ca0 28 74 29 29 3b 0a 20 20 20 20 20 20 70 69 64 20 (t));. pid
e9cb0 3d 20 67 65 74 70 69 64 28 29 3b 0a 20 20 20 20 = getpid();.
e9cc0 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 73 memcpy(&zBuf[s
e9cd0 69 7a 65 6f 66 28 74 29 5d 2c 20 26 70 69 64 2c izeof(t)], &pid,
e9ce0 20 73 69 7a 65 6f 66 28 70 69 64 29 29 3b 0a 20 sizeof(pid));.
e9cf0 20 20 20 20 20 61 73 73 65 72 74 28 20 73 69 7a assert( siz
e9d00 65 6f 66 28 74 29 2b 73 69 7a 65 6f 66 28 70 69 eof(t)+sizeof(pi
e9d10 64 29 3c 3d 28 73 69 7a 65 5f 74 29 6e 42 75 66 d)<=(size_t)nBuf
e9d20 20 29 3b 0a 20 20 20 20 20 20 6e 42 75 66 20 3d );. nBuf =
e9d30 20 73 69 7a 65 6f 66 28 74 29 20 2b 20 73 69 7a sizeof(t) + siz
e9d40 65 6f 66 28 70 69 64 29 3b 0a 20 20 20 20 7d 65 eof(pid);. }e
e9d50 6c 73 65 7b 0a 20 20 20 20 20 20 6e 42 75 66 20 lse{. nBuf
e9d60 3d 20 72 65 61 64 28 66 64 2c 20 7a 42 75 66 2c = read(fd, zBuf,
e9d70 20 6e 42 75 66 29 3b 0a 20 20 20 20 20 20 63 6c nBuf);. cl
e9d80 6f 73 65 28 66 64 29 3b 0a 20 20 20 20 7d 0a 20 ose(fd);. }.
e9d90 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 }.#endif. retu
e9da0 72 6e 20 6e 42 75 66 3b 0a 7d 0a 0a 0a 2f 2a 0a rn nBuf;.}.../*.
e9db0 2a 2a 20 53 6c 65 65 70 20 66 6f 72 20 61 20 6c ** Sleep for a l
e9dc0 69 74 74 6c 65 20 77 68 69 6c 65 2e 20 20 52 65 ittle while. Re
e9dd0 74 75 72 6e 20 74 68 65 20 61 6d 6f 75 6e 74 20 turn the amount
e9de0 6f 66 20 74 69 6d 65 20 73 6c 65 70 74 2e 0a 2a of time slept..*
e9df0 2a 20 54 68 65 20 61 72 67 75 6d 65 6e 74 20 69 * The argument i
e9e00 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 s the number of
e9e10 6d 69 63 72 6f 73 65 63 6f 6e 64 73 20 77 65 20 microseconds we
e9e20 77 61 6e 74 20 74 6f 20 73 6c 65 65 70 2e 0a 2a want to sleep..*
e9e30 2a 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c * The return val
e9e40 75 65 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 ue is the number
e9e50 20 6f 66 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73 of microseconds
e9e60 20 6f 66 20 73 6c 65 65 70 20 61 63 74 75 61 6c of sleep actual
e9e70 6c 79 0a 2a 2a 20 72 65 71 75 65 73 74 65 64 20 ly.** requested
e9e80 66 72 6f 6d 20 74 68 65 20 75 6e 64 65 72 6c 79 from the underly
e9e90 69 6e 67 20 6f 70 65 72 61 74 69 6e 67 20 73 79 ing operating sy
e9ea0 73 74 65 6d 2c 20 61 20 6e 75 6d 62 65 72 20 77 stem, a number w
e9eb0 68 69 63 68 0a 2a 2a 20 6d 69 67 68 74 20 62 65 hich.** might be
e9ec0 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72 greater than or
e9ed0 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 61 72 equal to the ar
e9ee0 67 75 6d 65 6e 74 2c 20 62 75 74 20 6e 6f 74 20 gument, but not
e9ef0 6c 65 73 73 0a 2a 2a 20 74 68 61 6e 20 74 68 65 less.** than the
e9f00 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 argument..*/.st
e9f10 61 74 69 63 20 69 6e 74 20 75 6e 69 78 53 6c 65 atic int unixSle
e9f20 65 70 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a ep(sqlite3_vfs *
e9f30 4e 6f 74 55 73 65 64 2c 20 69 6e 74 20 6d 69 63 NotUsed, int mic
e9f40 72 6f 73 65 63 6f 6e 64 73 29 7b 0a 23 69 66 20 roseconds){.#if
e9f50 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 73 74 72 OS_VXWORKS. str
e9f60 75 63 74 20 74 69 6d 65 73 70 65 63 20 73 70 3b uct timespec sp;
e9f70 0a 0a 20 20 73 70 2e 74 76 5f 73 65 63 20 3d 20 .. sp.tv_sec =
e9f80 6d 69 63 72 6f 73 65 63 6f 6e 64 73 20 2f 20 31 microseconds / 1
e9f90 30 30 30 30 30 30 3b 0a 20 20 73 70 2e 74 76 5f 000000;. sp.tv_
e9fa0 6e 73 65 63 20 3d 20 28 6d 69 63 72 6f 73 65 63 nsec = (microsec
e9fb0 6f 6e 64 73 20 25 20 31 30 30 30 30 30 30 29 20 onds % 1000000)
e9fc0 2a 20 31 30 30 30 3b 0a 20 20 6e 61 6e 6f 73 6c * 1000;. nanosl
e9fd0 65 65 70 28 26 73 70 2c 20 4e 55 4c 4c 29 3b 0a eep(&sp, NULL);.
e9fe0 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
e9ff0 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 72 ER(NotUsed);. r
ea000 65 74 75 72 6e 20 6d 69 63 72 6f 73 65 63 6f 6e eturn microsecon
ea010 64 73 3b 0a 23 65 6c 69 66 20 64 65 66 69 6e 65 ds;.#elif define
ea020 64 28 48 41 56 45 5f 55 53 4c 45 45 50 29 20 26 d(HAVE_USLEEP) &
ea030 26 20 48 41 56 45 5f 55 53 4c 45 45 50 0a 20 20 & HAVE_USLEEP.
ea040 75 73 6c 65 65 70 28 6d 69 63 72 6f 73 65 63 6f usleep(microseco
ea050 6e 64 73 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 nds);. UNUSED_P
ea060 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 ARAMETER(NotUsed
ea070 29 3b 0a 20 20 72 65 74 75 72 6e 20 6d 69 63 72 );. return micr
ea080 6f 73 65 63 6f 6e 64 73 3b 0a 23 65 6c 73 65 0a oseconds;.#else.
ea090 20 20 69 6e 74 20 73 65 63 6f 6e 64 73 20 3d 20 int seconds =
ea0a0 28 6d 69 63 72 6f 73 65 63 6f 6e 64 73 2b 39 39 (microseconds+99
ea0b0 39 39 39 39 29 2f 31 30 30 30 30 30 30 3b 0a 20 9999)/1000000;.
ea0c0 20 73 6c 65 65 70 28 73 65 63 6f 6e 64 73 29 3b sleep(seconds);
ea0d0 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
ea0e0 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 TER(NotUsed);.
ea0f0 72 65 74 75 72 6e 20 73 65 63 6f 6e 64 73 2a 31 return seconds*1
ea100 30 30 30 30 30 30 3b 0a 23 65 6e 64 69 66 0a 7d 000000;.#endif.}
ea110 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c ../*.** The foll
ea120 6f 77 69 6e 67 20 76 61 72 69 61 62 6c 65 2c 20 owing variable,
ea130 69 66 20 73 65 74 20 74 6f 20 61 20 6e 6f 6e 2d if set to a non-
ea140 7a 65 72 6f 20 76 61 6c 75 65 2c 20 69 73 20 69 zero value, is i
ea150 6e 74 65 72 70 72 65 74 65 64 20 61 73 0a 2a 2a nterpreted as.**
ea160 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 the number of s
ea170 65 63 6f 6e 64 73 20 73 69 6e 63 65 20 31 39 37 econds since 197
ea180 30 20 61 6e 64 20 69 73 20 75 73 65 64 20 74 6f 0 and is used to
ea190 20 73 65 74 20 74 68 65 20 72 65 73 75 6c 74 20 set the result
ea1a0 6f 66 0a 2a 2a 20 73 71 6c 69 74 65 33 4f 73 43 of.** sqlite3OsC
ea1b0 75 72 72 65 6e 74 54 69 6d 65 28 29 20 64 75 72 urrentTime() dur
ea1c0 69 6e 67 20 74 65 73 74 69 6e 67 2e 0a 2a 2f 0a ing testing..*/.
ea1d0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 #ifdef SQLITE_TE
ea1e0 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e ST.SQLITE_API in
ea1f0 74 20 73 71 6c 69 74 65 33 5f 63 75 72 72 65 6e t sqlite3_curren
ea200 74 5f 74 69 6d 65 20 3d 20 30 3b 20 20 2f 2a 20 t_time = 0; /*
ea210 46 61 6b 65 20 73 79 73 74 65 6d 20 74 69 6d 65 Fake system time
ea220 20 69 6e 20 73 65 63 6f 6e 64 73 20 73 69 6e 63 in seconds sinc
ea230 65 20 31 39 37 30 2e 20 2a 2f 0a 23 65 6e 64 69 e 1970. */.#endi
ea240 66 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 74 68 f../*.** Find th
ea250 65 20 63 75 72 72 65 6e 74 20 74 69 6d 65 20 28 e current time (
ea260 69 6e 20 55 6e 69 76 65 72 73 61 6c 20 43 6f 6f in Universal Coo
ea270 72 64 69 6e 61 74 65 64 20 54 69 6d 65 29 2e 20 rdinated Time).
ea280 20 57 72 69 74 65 20 74 68 65 0a 2a 2a 20 63 75 Write the.** cu
ea290 72 72 65 6e 74 20 74 69 6d 65 20 61 6e 64 20 64 rrent time and d
ea2a0 61 74 65 20 61 73 20 61 20 4a 75 6c 69 61 6e 20 ate as a Julian
ea2b0 44 61 79 20 6e 75 6d 62 65 72 20 69 6e 74 6f 20 Day number into
ea2c0 2a 70 72 4e 6f 77 20 61 6e 64 0a 2a 2a 20 72 65 *prNow and.** re
ea2d0 74 75 72 6e 20 30 2e 20 20 52 65 74 75 72 6e 20 turn 0. Return
ea2e0 31 20 69 66 20 74 68 65 20 74 69 6d 65 20 61 6e 1 if the time an
ea2f0 64 20 64 61 74 65 20 63 61 6e 6e 6f 74 20 62 65 d date cannot be
ea300 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 73 74 61 74 69 found..*/.stati
ea310 63 20 69 6e 74 20 75 6e 69 78 43 75 72 72 65 6e c int unixCurren
ea320 74 54 69 6d 65 28 73 71 6c 69 74 65 33 5f 76 66 tTime(sqlite3_vf
ea330 73 20 2a 4e 6f 74 55 73 65 64 2c 20 64 6f 75 62 s *NotUsed, doub
ea340 6c 65 20 2a 70 72 4e 6f 77 29 7b 0a 23 69 66 20 le *prNow){.#if
ea350 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f defined(SQLITE_O
ea360 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 MIT_FLOATING_POI
ea370 4e 54 29 0a 20 20 74 69 6d 65 5f 74 20 74 3b 0a NT). time_t t;.
ea380 20 20 74 69 6d 65 28 26 74 29 3b 0a 20 20 2a 70 time(&t);. *p
ea390 72 4e 6f 77 20 3d 20 28 28 28 73 71 6c 69 74 65 rNow = (((sqlite
ea3a0 33 5f 69 6e 74 36 34 29 74 29 2f 38 36 34 30 20 3_int64)t)/8640
ea3b0 2b 20 32 34 34 30 35 38 37 35 29 2f 31 30 3b 0a + 24405875)/10;.
ea3c0 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28 4e 4f #elif defined(NO
ea3d0 5f 47 45 54 54 4f 44 29 0a 20 20 74 69 6d 65 5f _GETTOD). time_
ea3e0 74 20 74 3b 0a 20 20 74 69 6d 65 28 26 74 29 3b t t;. time(&t);
ea3f0 0a 20 20 2a 70 72 4e 6f 77 20 3d 20 74 2f 38 36 . *prNow = t/86
ea400 34 30 30 2e 30 20 2b 20 32 34 34 30 35 38 37 2e 400.0 + 2440587.
ea410 35 3b 0a 23 65 6c 69 66 20 4f 53 5f 56 58 57 4f 5;.#elif OS_VXWO
ea420 52 4b 53 0a 20 20 73 74 72 75 63 74 20 74 69 6d RKS. struct tim
ea430 65 73 70 65 63 20 73 4e 6f 77 3b 0a 20 20 63 6c espec sNow;. cl
ea440 6f 63 6b 5f 67 65 74 74 69 6d 65 28 43 4c 4f 43 ock_gettime(CLOC
ea450 4b 5f 52 45 41 4c 54 49 4d 45 2c 20 26 73 4e 6f K_REALTIME, &sNo
ea460 77 29 3b 0a 20 20 2a 70 72 4e 6f 77 20 3d 20 32 w);. *prNow = 2
ea470 34 34 30 35 38 37 2e 35 20 2b 20 73 4e 6f 77 2e 440587.5 + sNow.
ea480 74 76 5f 73 65 63 2f 38 36 34 30 30 2e 30 20 2b tv_sec/86400.0 +
ea490 20 73 4e 6f 77 2e 74 76 5f 6e 73 65 63 2f 38 36 sNow.tv_nsec/86
ea4a0 34 30 30 30 30 30 30 30 30 30 30 30 2e 30 3b 0a 400000000000.0;.
ea4b0 23 65 6c 73 65 0a 20 20 73 74 72 75 63 74 20 74 #else. struct t
ea4c0 69 6d 65 76 61 6c 20 73 4e 6f 77 3b 0a 20 20 67 imeval sNow;. g
ea4d0 65 74 74 69 6d 65 6f 66 64 61 79 28 26 73 4e 6f ettimeofday(&sNo
ea4e0 77 2c 20 30 29 3b 0a 20 20 2a 70 72 4e 6f 77 20 w, 0);. *prNow
ea4f0 3d 20 32 34 34 30 35 38 37 2e 35 20 2b 20 73 4e = 2440587.5 + sN
ea500 6f 77 2e 74 76 5f 73 65 63 2f 38 36 34 30 30 2e ow.tv_sec/86400.
ea510 30 20 2b 20 73 4e 6f 77 2e 74 76 5f 75 73 65 63 0 + sNow.tv_usec
ea520 2f 38 36 34 30 30 30 30 30 30 30 30 2e 30 3b 0a /86400000000.0;.
ea530 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 #endif..#ifdef S
ea540 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 69 66 28 QLITE_TEST. if(
ea550 20 73 71 6c 69 74 65 33 5f 63 75 72 72 65 6e 74 sqlite3_current
ea560 5f 74 69 6d 65 20 29 7b 0a 20 20 20 20 2a 70 72 _time ){. *pr
ea570 4e 6f 77 20 3d 20 73 71 6c 69 74 65 33 5f 63 75 Now = sqlite3_cu
ea580 72 72 65 6e 74 5f 74 69 6d 65 2f 38 36 34 30 30 rrent_time/86400
ea590 2e 30 20 2b 20 32 34 34 30 35 38 37 2e 35 3b 0a .0 + 2440587.5;.
ea5a0 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 55 4e 55 }.#endif. UNU
ea5b0 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f SED_PARAMETER(No
ea5c0 74 55 73 65 64 29 3b 0a 20 20 72 65 74 75 72 6e tUsed);. return
ea5d0 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 65 20 0;.}../*.** We
ea5e0 61 64 64 65 64 20 74 68 65 20 78 47 65 74 4c 61 added the xGetLa
ea5f0 73 74 45 72 72 6f 72 28 29 20 6d 65 74 68 6f 64 stError() method
ea600 20 77 69 74 68 20 74 68 65 20 69 6e 74 65 6e 74 with the intent
ea610 69 6f 6e 20 6f 66 20 70 72 6f 76 69 64 69 6e 67 ion of providing
ea620 0a 2a 2a 20 62 65 74 74 65 72 20 6c 6f 77 2d 6c .** better low-l
ea630 65 76 65 6c 20 65 72 72 6f 72 20 6d 65 73 73 61 evel error messa
ea640 67 65 73 20 77 68 65 6e 20 6f 70 65 72 61 74 69 ges when operati
ea650 6e 67 2d 73 79 73 74 65 6d 20 70 72 6f 62 6c 65 ng-system proble
ea660 6d 73 20 63 6f 6d 65 20 75 70 0a 2a 2a 20 64 75 ms come up.** du
ea670 72 69 6e 67 20 53 51 4c 69 74 65 20 6f 70 65 72 ring SQLite oper
ea680 61 74 69 6f 6e 2e 20 20 42 75 74 20 73 6f 20 66 ation. But so f
ea690 61 72 2c 20 6e 6f 6e 65 20 6f 66 20 74 68 61 74 ar, none of that
ea6a0 20 68 61 73 20 62 65 65 6e 20 69 6d 70 6c 65 6d has been implem
ea6b0 65 6e 74 65 64 0a 2a 2a 20 69 6e 20 74 68 65 20 ented.** in the
ea6c0 63 6f 72 65 2e 20 20 53 6f 20 74 68 69 73 20 72 core. So this r
ea6d0 6f 75 74 69 6e 65 20 69 73 20 6e 65 76 65 72 20 outine is never
ea6e0 63 61 6c 6c 65 64 2e 20 20 46 6f 72 20 6e 6f 77 called. For now
ea6f0 2c 20 69 74 20 69 73 20 6d 65 72 65 6c 79 0a 2a , it is merely.*
ea700 2a 20 61 20 70 6c 61 63 65 2d 68 6f 6c 64 65 72 * a place-holder
ea710 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
ea720 75 6e 69 78 47 65 74 4c 61 73 74 45 72 72 6f 72 unixGetLastError
ea730 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e 6f (sqlite3_vfs *No
ea740 74 55 73 65 64 2c 20 69 6e 74 20 4e 6f 74 55 73 tUsed, int NotUs
ea750 65 64 32 2c 20 63 68 61 72 20 2a 4e 6f 74 55 73 ed2, char *NotUs
ea760 65 64 33 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 ed3){. UNUSED_P
ea770 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 ARAMETER(NotUsed
ea780 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 );. UNUSED_PARA
ea790 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 32 29 3b METER(NotUsed2);
ea7a0 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
ea7b0 54 45 52 28 4e 6f 74 55 73 65 64 33 29 3b 0a 20 TER(NotUsed3);.
ea7c0 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a return 0;.}../*
ea7d0 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
ea7e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 ********* End of
ea7f0 20 73 71 6c 69 74 65 33 5f 76 66 73 20 6d 65 74 sqlite3_vfs met
ea800 68 6f 64 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a hods ***********
ea810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ea820 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
ea830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ea840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ea850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ea860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
ea870 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
ea880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ea890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ea8a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ea8b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ea8c0 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *.**************
ea8d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 ************ Beg
ea8e0 69 6e 20 50 72 6f 78 79 20 4c 6f 63 6b 69 6e 67 in Proxy Locking
ea8f0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
ea900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ea910 2a 0a 2a 2a 0a 2a 2a 20 50 72 6f 78 79 20 6c 6f *.**.** Proxy lo
ea920 63 6b 69 6e 67 20 69 73 20 61 20 22 75 62 65 72 cking is a "uber
ea930 2d 6c 6f 63 6b 69 6e 67 2d 6d 65 74 68 6f 64 22 -locking-method"
ea940 20 69 6e 20 74 68 69 73 20 73 65 6e 73 65 3a 20 in this sense:
ea950 20 49 74 20 75 73 65 73 20 74 68 65 0a 2a 2a 20 It uses the.**
ea960 6f 74 68 65 72 20 6c 6f 63 6b 69 6e 67 20 6d 65 other locking me
ea970 74 68 6f 64 73 20 6f 6e 20 73 65 63 6f 6e 64 61 thods on seconda
ea980 72 79 20 6c 6f 63 6b 20 66 69 6c 65 73 2e 20 20 ry lock files.
ea990 50 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 69 73 Proxy locking is
ea9a0 20 61 0a 2a 2a 20 6d 65 74 61 2d 6c 61 79 65 72 a.** meta-layer
ea9b0 20 6f 76 65 72 20 74 6f 70 20 6f 66 20 74 68 65 over top of the
ea9c0 20 70 72 69 6d 69 74 69 76 65 20 6c 6f 63 6b 69 primitive locki
ea9d0 6e 67 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 ng implemented a
ea9e0 62 6f 76 65 2e 20 20 46 6f 72 0a 2a 2a 20 74 68 bove. For.** th
ea9f0 69 73 20 72 65 61 73 6f 6e 2c 20 74 68 65 20 64 is reason, the d
eaa00 69 76 69 73 69 6f 6e 20 74 68 61 74 20 69 6d 70 ivision that imp
eaa10 6c 65 6d 65 6e 74 73 20 6f 66 20 70 72 6f 78 79 lements of proxy
eaa20 20 6c 6f 63 6b 69 6e 67 20 69 73 20 64 65 66 65 locking is defe
eaa30 72 72 65 64 0a 2a 2a 20 75 6e 74 69 6c 20 6c 61 rred.** until la
eaa40 74 65 20 69 6e 20 74 68 65 20 66 69 6c 65 20 28 te in the file (
eaa50 68 65 72 65 29 20 61 66 74 65 72 20 61 6c 6c 20 here) after all
eaa60 6f 66 20 74 68 65 20 6f 74 68 65 72 20 49 2f 4f of the other I/O
eaa70 20 6d 65 74 68 6f 64 73 20 68 61 76 65 0a 2a 2a methods have.**
eaa80 20 62 65 65 6e 20 64 65 66 69 6e 65 64 20 2d 20 been defined -
eaa90 73 6f 20 74 68 61 74 20 74 68 65 20 70 72 69 6d so that the prim
eaaa0 69 74 69 76 65 20 6c 6f 63 6b 69 6e 67 20 6d 65 itive locking me
eaab0 74 68 6f 64 73 20 61 72 65 20 61 76 61 69 6c 61 thods are availa
eaac0 62 6c 65 0a 2a 2a 20 61 73 20 73 65 72 76 69 63 ble.** as servic
eaad0 65 73 20 74 6f 20 68 65 6c 70 20 77 69 74 68 20 es to help with
eaae0 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 the implementati
eaaf0 6f 6e 20 6f 66 20 70 72 6f 78 79 20 6c 6f 63 6b on of proxy lock
eab00 69 6e 67 2e 0a 2a 2a 0a 2a 2a 2a 2a 0a 2a 2a 0a ing..**.****.**.
eab10 2a 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 6c ** The default l
eab20 6f 63 6b 69 6e 67 20 73 63 68 65 6d 65 73 20 69 ocking schemes i
eab30 6e 20 53 51 4c 69 74 65 20 75 73 65 20 62 79 74 n SQLite use byt
eab40 65 2d 72 61 6e 67 65 20 6c 6f 63 6b 73 20 6f 6e e-range locks on
eab50 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 the.** database
eab60 20 66 69 6c 65 20 74 6f 20 63 6f 6f 72 64 69 6e file to coordin
eab70 61 74 65 20 73 61 66 65 2c 20 63 6f 6e 63 75 72 ate safe, concur
eab80 72 65 6e 74 20 61 63 63 65 73 73 20 62 79 20 6d rent access by m
eab90 75 6c 74 69 70 6c 65 20 72 65 61 64 65 72 73 0a ultiple readers.
eaba0 2a 2a 20 61 6e 64 20 77 72 69 74 65 72 73 20 5b ** and writers [
eabb0 68 74 74 70 3a 2f 2f 73 71 6c 69 74 65 2e 6f 72 http://sqlite.or
eabc0 67 2f 6c 6f 63 6b 69 6e 67 76 33 2e 68 74 6d 6c g/lockingv3.html
eabd0 5d 2e 20 20 54 68 65 20 66 69 76 65 20 66 69 6c ]. The five fil
eabe0 65 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 73 74 61 e locking.** sta
eabf0 74 65 73 20 28 55 4e 4c 4f 43 4b 45 44 2c 20 50 tes (UNLOCKED, P
eac00 45 4e 44 49 4e 47 2c 20 53 48 41 52 45 44 2c 20 ENDING, SHARED,
eac10 52 45 53 45 52 56 45 44 2c 20 45 58 43 4c 55 53 RESERVED, EXCLUS
eac20 49 56 45 29 20 61 72 65 20 69 6d 70 6c 65 6d 65 IVE) are impleme
eac30 6e 74 65 64 0a 2a 2a 20 61 73 20 50 4f 53 49 58 nted.** as POSIX
eac40 20 72 65 61 64 20 26 20 77 72 69 74 65 20 6c 6f read & write lo
eac50 63 6b 73 20 6f 76 65 72 20 66 69 78 65 64 20 73 cks over fixed s
eac60 65 74 20 6f 66 20 6c 6f 63 61 74 69 6f 6e 73 20 et of locations
eac70 28 76 69 61 20 66 73 63 74 6c 29 2c 0a 2a 2a 20 (via fsctl),.**
eac80 6f 6e 20 41 46 50 20 61 6e 64 20 53 4d 42 20 6f on AFP and SMB o
eac90 6e 6c 79 20 65 78 63 6c 75 73 69 76 65 20 62 79 nly exclusive by
eaca0 74 65 2d 72 61 6e 67 65 20 6c 6f 63 6b 73 20 61 te-range locks a
eacb0 72 65 20 61 76 61 69 6c 61 62 6c 65 20 76 69 61 re available via
eacc0 20 66 73 63 74 6c 0a 2a 2a 20 77 69 74 68 20 5f fsctl.** with _
eacd0 49 4f 57 52 28 27 7a 27 2c 20 32 33 2c 20 73 74 IOWR('z', 23, st
eace0 72 75 63 74 20 42 79 74 65 52 61 6e 67 65 4c 6f ruct ByteRangeLo
eacf0 63 6b 50 42 32 29 20 74 6f 20 74 72 61 63 6b 20 ckPB2) to track
ead00 74 68 65 20 73 61 6d 65 20 35 20 73 74 61 74 65 the same 5 state
ead10 73 2e 0a 2a 2a 20 54 6f 20 73 69 6d 75 6c 61 74 s..** To simulat
ead20 65 20 61 20 46 5f 52 44 4c 43 4b 20 6f 6e 20 74 e a F_RDLCK on t
ead30 68 65 20 73 68 61 72 65 64 20 72 61 6e 67 65 2c he shared range,
ead40 20 6f 6e 20 41 46 50 20 61 20 72 61 6e 64 6f 6d on AFP a random
ead50 6c 79 20 73 65 6c 65 63 74 65 64 0a 2a 2a 20 61 ly selected.** a
ead60 64 64 72 65 73 73 20 69 6e 20 74 68 65 20 73 68 ddress in the sh
ead70 61 72 65 64 20 72 61 6e 67 65 20 69 73 20 74 61 ared range is ta
ead80 6b 65 6e 20 66 6f 72 20 61 20 53 48 41 52 45 44 ken for a SHARED
ead90 20 6c 6f 63 6b 2c 20 74 68 65 20 65 6e 74 69 72 lock, the entir
eada0 65 0a 2a 2a 20 73 68 61 72 65 64 20 72 61 6e 67 e.** shared rang
eadb0 65 20 69 73 20 74 61 6b 65 6e 20 66 6f 72 20 61 e is taken for a
eadc0 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b n EXCLUSIVE lock
eadd0 29 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 50 45 ):.**.** PE
eade0 4e 44 49 4e 47 5f 42 59 54 45 20 20 20 20 20 20 NDING_BYTE
eadf0 20 20 30 78 34 30 30 30 30 30 30 30 09 09 20 20 0x40000000..
eae00 20 09 0a 2a 2a 20 20 20 20 20 20 52 45 53 45 52 ..** RESER
eae10 56 45 44 5f 42 59 54 45 20 20 20 20 20 20 20 30 VED_BYTE 0
eae20 78 34 30 30 30 30 30 30 31 0a 2a 2a 20 20 20 20 x40000001.**
eae30 20 20 53 48 41 52 45 44 5f 52 41 4e 47 45 20 20 SHARED_RANGE
eae40 20 20 20 20 20 20 30 78 34 30 30 30 30 30 30 32 0x40000002
eae50 20 2d 3e 20 30 78 34 30 30 30 30 32 30 30 0a 2a -> 0x40000200.*
eae60 2a 0a 2a 2a 20 54 68 69 73 20 77 6f 72 6b 73 20 *.** This works
eae70 77 65 6c 6c 20 6f 6e 20 74 68 65 20 6c 6f 63 61 well on the loca
eae80 6c 20 66 69 6c 65 20 73 79 73 74 65 6d 2c 20 62 l file system, b
eae90 75 74 20 73 68 6f 77 73 20 61 20 6e 65 61 72 6c ut shows a nearl
eaea0 79 20 31 30 30 78 0a 2a 2a 20 73 6c 6f 77 64 6f y 100x.** slowdo
eaeb0 77 6e 20 69 6e 20 72 65 61 64 20 70 65 72 66 6f wn in read perfo
eaec0 72 6d 61 6e 63 65 20 6f 6e 20 41 46 50 20 62 65 rmance on AFP be
eaed0 63 61 75 73 65 20 74 68 65 20 41 46 50 20 63 6c cause the AFP cl
eaee0 69 65 6e 74 20 64 69 73 61 62 6c 65 73 0a 2a 2a ient disables.**
eaef0 20 74 68 65 20 72 65 61 64 20 63 61 63 68 65 20 the read cache
eaf00 77 68 65 6e 20 62 79 74 65 2d 72 61 6e 67 65 20 when byte-range
eaf10 6c 6f 63 6b 73 20 61 72 65 20 70 72 65 73 65 6e locks are presen
eaf20 74 2e 20 20 45 6e 61 62 6c 69 6e 67 20 74 68 65 t. Enabling the
eaf30 20 72 65 61 64 0a 2a 2a 20 63 61 63 68 65 20 65 read.** cache e
eaf40 78 70 6f 73 65 73 20 61 20 63 61 63 68 65 20 63 xposes a cache c
eaf50 6f 68 65 72 65 6e 63 79 20 70 72 6f 62 6c 65 6d oherency problem
eaf60 20 74 68 61 74 20 69 73 20 70 72 65 73 65 6e 74 that is present
eaf70 20 6f 6e 20 61 6c 6c 20 4f 53 20 58 0a 2a 2a 20 on all OS X.**
eaf80 73 75 70 70 6f 72 74 65 64 20 6e 65 74 77 6f 72 supported networ
eaf90 6b 20 66 69 6c 65 20 73 79 73 74 65 6d 73 2e 20 k file systems.
eafa0 20 4e 46 53 20 61 6e 64 20 41 46 50 20 62 6f 74 NFS and AFP bot
eafb0 68 20 6f 62 73 65 72 76 65 20 74 68 65 0a 2a 2a h observe the.**
eafc0 20 63 6c 6f 73 65 2d 74 6f 2d 6f 70 65 6e 20 73 close-to-open s
eafd0 65 6d 61 6e 74 69 63 73 20 66 6f 72 20 65 6e 73 emantics for ens
eafe0 75 72 69 6e 67 20 63 61 63 68 65 20 63 6f 68 65 uring cache cohe
eaff0 72 65 6e 63 79 0a 2a 2a 20 5b 68 74 74 70 3a 2f rency.** [http:/
eb000 2f 6e 66 73 2e 73 6f 75 72 63 65 66 6f 72 67 65 /nfs.sourceforge
eb010 2e 6e 65 74 2f 23 66 61 71 5f 61 38 5d 2c 20 77 .net/#faq_a8], w
eb020 68 69 63 68 20 64 6f 65 73 20 6e 6f 74 20 65 66 hich does not ef
eb030 66 65 63 74 69 76 65 6c 79 0a 2a 2a 20 61 64 64 fectively.** add
eb040 72 65 73 73 20 74 68 65 20 72 65 71 75 69 72 65 ress the require
eb050 6d 65 6e 74 73 20 66 6f 72 20 63 6f 6e 63 75 72 ments for concur
eb060 72 65 6e 74 20 64 61 74 61 62 61 73 65 20 61 63 rent database ac
eb070 63 65 73 73 20 62 79 20 6d 75 6c 74 69 70 6c 65 cess by multiple
eb080 0a 2a 2a 20 72 65 61 64 65 72 73 20 61 6e 64 20 .** readers and
eb090 77 72 69 74 65 72 73 0a 2a 2a 20 5b 68 74 74 70 writers.** [http
eb0a0 3a 2f 2f 77 77 77 2e 6e 61 62 62 6c 65 2e 63 6f ://www.nabble.co
eb0b0 6d 2f 53 51 4c 69 74 65 2d 6f 6e 2d 4e 46 53 2d m/SQLite-on-NFS-
eb0c0 63 61 63 68 65 2d 63 6f 68 65 72 65 6e 63 79 2d cache-coherency-
eb0d0 74 64 31 35 36 35 35 37 30 31 2e 68 74 6d 6c 5d td15655701.html]
eb0e0 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 61 64 64 72 65 ..**.** To addre
eb0f0 73 73 20 74 68 65 20 70 65 72 66 6f 72 6d 61 6e ss the performan
eb100 63 65 20 61 6e 64 20 63 61 63 68 65 20 63 6f 68 ce and cache coh
eb110 65 72 65 6e 63 79 20 69 73 73 75 65 73 2c 20 70 erency issues, p
eb120 72 6f 78 79 20 66 69 6c 65 20 6c 6f 63 6b 69 6e roxy file lockin
eb130 67 0a 2a 2a 20 63 68 61 6e 67 65 73 20 74 68 65 g.** changes the
eb140 20 77 61 79 20 64 61 74 61 62 61 73 65 20 61 63 way database ac
eb150 63 65 73 73 20 69 73 20 63 6f 6e 74 72 6f 6c 6c cess is controll
eb160 65 64 20 62 79 20 6c 69 6d 69 74 69 6e 67 20 61 ed by limiting a
eb170 63 63 65 73 73 20 74 6f 20 61 0a 2a 2a 20 73 69 ccess to a.** si
eb180 6e 67 6c 65 20 68 6f 73 74 20 61 74 20 61 20 74 ngle host at a t
eb190 69 6d 65 20 61 6e 64 20 6d 6f 76 69 6e 67 20 66 ime and moving f
eb1a0 69 6c 65 20 6c 6f 63 6b 73 20 6f 66 66 20 6f 66 ile locks off of
eb1b0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
eb1c0 6c 65 0a 2a 2a 20 61 6e 64 20 6f 6e 74 6f 20 61 le.** and onto a
eb1d0 20 70 72 6f 78 79 20 66 69 6c 65 20 6f 6e 20 74 proxy file on t
eb1e0 68 65 20 6c 6f 63 61 6c 20 66 69 6c 65 20 73 79 he local file sy
eb1f0 73 74 65 6d 2e 20 20 0a 2a 2a 0a 2a 2a 0a 2a 2a stem. .**.**.**
eb200 20 55 73 69 6e 67 20 70 72 6f 78 79 20 6c 6f 63 Using proxy loc
eb210 6b 73 0a 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ks.** ----------
eb220 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 43 20 -------.**.** C
eb230 41 50 49 73 0a 2a 2a 0a 2a 2a 20 20 73 71 6c 69 APIs.**.** sqli
eb240 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c te3_file_control
eb250 28 64 62 2c 20 64 62 6e 61 6d 65 2c 20 53 51 4c (db, dbname, SQL
eb260 49 54 45 5f 53 45 54 5f 4c 4f 43 4b 50 52 4f 58 ITE_SET_LOCKPROX
eb270 59 46 49 4c 45 2c 0a 2a 2a 20 20 20 20 20 20 20 YFILE,.**
eb280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
eb290 3c 70 72 6f 78 79 5f 70 61 74 68 3e 20 7c 20 22 <proxy_path> | "
eb2a0 3a 61 75 74 6f 3a 22 29 3b 0a 2a 2a 20 20 73 71 :auto:");.** sq
eb2b0 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 lite3_file_contr
eb2c0 6f 6c 28 64 62 2c 20 64 62 6e 61 6d 65 2c 20 53 ol(db, dbname, S
eb2d0 51 4c 49 54 45 5f 47 45 54 5f 4c 4f 43 4b 50 52 QLITE_GET_LOCKPR
eb2e0 4f 58 59 46 49 4c 45 2c 20 26 3c 70 72 6f 78 79 OXYFILE, &<proxy
eb2f0 5f 70 61 74 68 3e 29 3b 0a 2a 2a 0a 2a 2a 0a 2a _path>);.**.**.*
eb300 2a 20 53 51 4c 20 70 72 61 67 6d 61 73 0a 2a 2a * SQL pragmas.**
eb310 0a 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 74 .** PRAGMA [dat
eb320 61 62 61 73 65 2e 5d 6c 6f 63 6b 5f 70 72 6f 78 abase.]lock_prox
eb330 79 5f 66 69 6c 65 3d 3c 70 72 6f 78 79 5f 70 61 y_file=<proxy_pa
eb340 74 68 3e 20 7c 20 3a 61 75 74 6f 3a 0a 2a 2a 20 th> | :auto:.**
eb350 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 PRAGMA [databas
eb360 65 2e 5d 6c 6f 63 6b 5f 70 72 6f 78 79 5f 66 69 e.]lock_proxy_fi
eb370 6c 65 0a 2a 2a 0a 2a 2a 20 53 70 65 63 69 66 79 le.**.** Specify
eb380 69 6e 67 20 22 3a 61 75 74 6f 3a 22 20 6d 65 61 ing ":auto:" mea
eb390 6e 73 20 74 68 61 74 20 69 66 20 74 68 65 72 65 ns that if there
eb3a0 20 69 73 20 61 20 63 6f 6e 63 68 20 66 69 6c 65 is a conch file
eb3b0 20 77 69 74 68 20 61 20 6d 61 74 63 68 69 6e 67 with a matching
eb3c0 0a 2a 2a 20 68 6f 73 74 20 49 44 20 69 6e 20 69 .** host ID in i
eb3d0 74 2c 20 74 68 65 20 70 72 6f 78 79 20 70 61 74 t, the proxy pat
eb3e0 68 20 69 6e 20 74 68 65 20 63 6f 6e 63 68 20 66 h in the conch f
eb3f0 69 6c 65 20 77 69 6c 6c 20 62 65 20 75 73 65 64 ile will be used
eb400 2c 20 6f 74 68 65 72 77 69 73 65 0a 2a 2a 20 61 , otherwise.** a
eb410 20 70 72 6f 78 79 20 70 61 74 68 20 62 61 73 65 proxy path base
eb420 64 20 6f 6e 20 74 68 65 20 75 73 65 72 27 73 20 d on the user's
eb430 74 65 6d 70 20 64 69 72 0a 2a 2a 20 28 76 69 61 temp dir.** (via
eb440 20 63 6f 6e 66 73 74 72 28 5f 43 53 5f 44 41 52 confstr(_CS_DAR
eb450 57 49 4e 5f 55 53 45 52 5f 54 45 4d 50 5f 44 49 WIN_USER_TEMP_DI
eb460 52 2c 2e 2e 2e 29 29 20 77 69 6c 6c 20 62 65 20 R,...)) will be
eb470 75 73 65 64 20 61 6e 64 20 74 68 65 0a 2a 2a 20 used and the.**
eb480 61 63 74 75 61 6c 20 70 72 6f 78 79 20 66 69 6c actual proxy fil
eb490 65 20 6e 61 6d 65 20 69 73 20 67 65 6e 65 72 61 e name is genera
eb4a0 74 65 64 20 66 72 6f 6d 20 74 68 65 20 6e 61 6d ted from the nam
eb4b0 65 20 61 6e 64 20 70 61 74 68 20 6f 66 20 74 68 e and path of th
eb4c0 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 e.** database fi
eb4d0 6c 65 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 le. For example
eb4e0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 46 6f :.**.** Fo
eb4f0 72 20 64 61 74 61 62 61 73 65 20 70 61 74 68 20 r database path
eb500 22 2f 55 73 65 72 73 2f 6d 65 2f 66 6f 6f 2e 64 "/Users/me/foo.d
eb510 62 22 20 0a 2a 2a 20 20 20 20 20 20 20 54 68 65 b" .** The
eb520 20 6c 6f 63 6b 20 70 61 74 68 20 77 69 6c 6c 20 lock path will
eb530 62 65 20 22 3c 74 6d 70 64 69 72 3e 2f 73 71 6c be "<tmpdir>/sql
eb540 69 74 65 70 6c 6f 63 6b 73 2f 5f 55 73 65 72 73 iteplocks/_Users
eb550 5f 6d 65 5f 66 6f 6f 2e 64 62 3a 61 75 74 6f 3a _me_foo.db:auto:
eb560 22 29 0a 2a 2a 0a 2a 2a 20 4f 6e 63 65 20 61 20 ").**.** Once a
eb570 6c 6f 63 6b 20 70 72 6f 78 79 20 69 73 20 63 6f lock proxy is co
eb580 6e 66 69 67 75 72 65 64 20 66 6f 72 20 61 20 64 nfigured for a d
eb590 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
eb5a0 6f 6e 2c 20 69 74 20 63 61 6e 20 6e 6f 74 0a 2a on, it can not.*
eb5b0 2a 20 62 65 20 72 65 6d 6f 76 65 64 2c 20 68 6f * be removed, ho
eb5c0 77 65 76 65 72 20 69 74 20 6d 61 79 20 62 65 20 wever it may be
eb5d0 73 77 69 74 63 68 65 64 20 74 6f 20 61 20 64 69 switched to a di
eb5e0 66 66 65 72 65 6e 74 20 70 72 6f 78 79 20 70 61 fferent proxy pa
eb5f0 74 68 20 76 69 61 0a 2a 2a 20 74 68 65 20 61 62 th via.** the ab
eb600 6f 76 65 20 41 50 49 73 20 28 61 73 73 75 6d 69 ove APIs (assumi
eb610 6e 67 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c ng the conch fil
eb620 65 20 69 73 20 6e 6f 74 20 62 65 69 6e 67 20 68 e is not being h
eb630 65 6c 64 20 62 79 20 61 6e 6f 74 68 65 72 0a 2a eld by another.*
eb640 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 72 20 * connection or
eb650 70 72 6f 63 65 73 73 29 2e 20 0a 2a 2a 0a 2a 2a process). .**.**
eb660 0a 2a 2a 20 48 6f 77 20 70 72 6f 78 79 20 6c 6f .** How proxy lo
eb670 63 6b 69 6e 67 20 77 6f 72 6b 73 0a 2a 2a 20 2d cking works.** -
eb680 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
eb690 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 50 72 6f ------.**.** Pro
eb6a0 78 79 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 xy file locking
eb6b0 72 65 6c 69 65 73 20 70 72 69 6d 61 72 69 6c 79 relies primarily
eb6c0 20 6f 6e 20 74 77 6f 20 6e 65 77 20 73 75 70 70 on two new supp
eb6d0 6f 72 74 69 6e 67 20 66 69 6c 65 73 3a 20 0a 2a orting files: .*
eb6e0 2a 0a 2a 2a 20 20 20 2a 20 20 63 6f 6e 63 68 20 *.** * conch
eb6f0 66 69 6c 65 20 74 6f 20 6c 69 6d 69 74 20 61 63 file to limit ac
eb700 63 65 73 73 20 74 6f 20 74 68 65 20 64 61 74 61 cess to the data
eb710 62 61 73 65 20 66 69 6c 65 20 74 6f 20 61 20 73 base file to a s
eb720 69 6e 67 6c 65 20 68 6f 73 74 0a 2a 2a 20 20 20 ingle host.**
eb730 20 20 20 61 74 20 61 20 74 69 6d 65 0a 2a 2a 0a at a time.**.
eb740 2a 2a 20 20 20 2a 20 20 70 72 6f 78 79 20 66 69 ** * proxy fi
eb750 6c 65 20 74 6f 20 61 63 74 20 61 73 20 61 20 70 le to act as a p
eb760 72 6f 78 79 20 66 6f 72 20 74 68 65 20 61 64 76 roxy for the adv
eb770 69 73 6f 72 79 20 6c 6f 63 6b 73 20 6e 6f 72 6d isory locks norm
eb780 61 6c 6c 79 0a 2a 2a 20 20 20 20 20 20 74 61 6b ally.** tak
eb790 65 6e 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 en on the databa
eb7a0 73 65 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e se.**.** The con
eb7b0 63 68 20 66 69 6c 65 20 2d 20 74 6f 20 75 73 65 ch file - to use
eb7c0 20 61 20 70 72 6f 78 79 20 66 69 6c 65 2c 20 73 a proxy file, s
eb7d0 71 6c 69 74 65 20 6d 75 73 74 20 66 69 72 73 74 qlite must first
eb7e0 20 22 68 6f 6c 64 20 74 68 65 20 63 6f 6e 63 68 "hold the conch
eb7f0 22 0a 2a 2a 20 62 79 20 74 61 6b 69 6e 67 20 61 ".** by taking a
eb800 6e 20 73 71 6c 69 74 65 2d 73 74 79 6c 65 20 73 n sqlite-style s
eb810 68 61 72 65 64 20 6c 6f 63 6b 20 6f 6e 20 74 68 hared lock on th
eb820 65 20 63 6f 6e 63 68 20 66 69 6c 65 2c 20 72 65 e conch file, re
eb830 61 64 69 6e 67 20 74 68 65 0a 2a 2a 20 63 6f 6e ading the.** con
eb840 74 65 6e 74 73 20 61 6e 64 20 63 6f 6d 70 61 72 tents and compar
eb850 69 6e 67 20 74 68 65 20 68 6f 73 74 27 73 20 75 ing the host's u
eb860 6e 69 71 75 65 20 68 6f 73 74 20 49 44 20 28 73 nique host ID (s
eb870 65 65 20 62 65 6c 6f 77 29 20 61 6e 64 20 6c 6f ee below) and lo
eb880 63 6b 0a 2a 2a 20 70 72 6f 78 79 20 70 61 74 68 ck.** proxy path
eb890 20 61 67 61 69 6e 73 74 20 74 68 65 20 76 61 6c against the val
eb8a0 75 65 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68 ues stored in th
eb8b0 65 20 63 6f 6e 63 68 2e 20 20 54 68 65 20 63 6f e conch. The co
eb8c0 6e 63 68 20 66 69 6c 65 20 69 73 0a 2a 2a 20 73 nch file is.** s
eb8d0 74 6f 72 65 64 20 69 6e 20 74 68 65 20 73 61 6d tored in the sam
eb8e0 65 20 64 69 72 65 63 74 6f 72 79 20 61 73 20 74 e directory as t
eb8f0 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
eb900 20 61 6e 64 20 74 68 65 20 66 69 6c 65 20 6e 61 and the file na
eb910 6d 65 0a 2a 2a 20 69 73 20 70 61 74 74 65 72 6e me.** is pattern
eb920 65 64 20 61 66 74 65 72 20 74 68 65 20 64 61 74 ed after the dat
eb930 61 62 61 73 65 20 66 69 6c 65 20 6e 61 6d 65 20 abase file name
eb940 61 73 20 22 2e 3c 64 61 74 61 62 61 73 65 6e 61 as ".<databasena
eb950 6d 65 3e 2d 63 6f 6e 63 68 22 2e 0a 2a 2a 20 49 me>-conch"..** I
eb960 66 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 f the conch file
eb970 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 2c does not exist,
eb980 20 6f 72 20 69 74 27 73 20 63 6f 6e 74 65 6e 74 or it's content
eb990 73 20 64 6f 20 6e 6f 74 20 6d 61 74 63 68 20 74 s do not match t
eb9a0 68 65 0a 2a 2a 20 68 6f 73 74 20 49 44 20 61 6e he.** host ID an
eb9b0 64 2f 6f 72 20 70 72 6f 78 79 20 70 61 74 68 2c d/or proxy path,
eb9c0 20 74 68 65 6e 20 74 68 65 20 6c 6f 63 6b 20 69 then the lock i
eb9d0 73 20 65 73 63 61 6c 61 74 65 64 20 74 6f 20 61 s escalated to a
eb9e0 6e 20 65 78 63 6c 75 73 69 76 65 0a 2a 2a 20 6c n exclusive.** l
eb9f0 6f 63 6b 20 61 6e 64 20 74 68 65 20 63 6f 6e 63 ock and the conc
eba00 68 20 66 69 6c 65 20 63 6f 6e 74 65 6e 74 73 20 h file contents
eba10 69 73 20 75 70 64 61 74 65 64 20 77 69 74 68 20 is updated with
eba20 74 68 65 20 68 6f 73 74 20 49 44 20 61 6e 64 20 the host ID and
eba30 70 72 6f 78 79 0a 2a 2a 20 70 61 74 68 20 61 6e proxy.** path an
eba40 64 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 64 6f d the lock is do
eba50 77 6e 67 72 61 64 65 64 20 74 6f 20 61 20 73 68 wngraded to a sh
eba60 61 72 65 64 20 6c 6f 63 6b 20 61 67 61 69 6e 2e ared lock again.
eba70 20 20 49 66 20 74 68 65 20 63 6f 6e 63 68 0a 2a If the conch.*
eba80 2a 20 69 73 20 68 65 6c 64 20 62 79 20 61 6e 6f * is held by ano
eba90 74 68 65 72 20 70 72 6f 63 65 73 73 20 28 77 69 ther process (wi
ebaa0 74 68 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b th a shared lock
ebab0 29 2c 20 74 68 65 20 65 78 63 6c 75 73 69 76 65 ), the exclusive
ebac0 20 6c 6f 63 6b 0a 2a 2a 20 77 69 6c 6c 20 66 61 lock.** will fa
ebad0 69 6c 20 61 6e 64 20 53 51 4c 49 54 45 5f 42 55 il and SQLITE_BU
ebae0 53 59 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a SY is returned..
ebaf0 2a 2a 0a 2a 2a 20 54 68 65 20 70 72 6f 78 79 20 **.** The proxy
ebb00 66 69 6c 65 20 2d 20 61 20 73 69 6e 67 6c 65 2d file - a single-
ebb10 62 79 74 65 20 66 69 6c 65 20 75 73 65 64 20 66 byte file used f
ebb20 6f 72 20 61 6c 6c 20 61 64 76 69 73 6f 72 79 20 or all advisory
ebb30 66 69 6c 65 20 6c 6f 63 6b 73 0a 2a 2a 20 6e 6f file locks.** no
ebb40 72 6d 61 6c 6c 79 20 74 61 6b 65 6e 20 6f 6e 20 rmally taken on
ebb50 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
ebb60 65 2e 20 20 20 54 68 69 73 20 61 6c 6c 6f 77 73 e. This allows
ebb70 20 66 6f 72 20 73 61 66 65 20 73 68 61 72 69 6e for safe sharin
ebb80 67 0a 2a 2a 20 6f 66 20 74 68 65 20 64 61 74 61 g.** of the data
ebb90 62 61 73 65 20 66 69 6c 65 20 66 6f 72 20 6d 75 base file for mu
ebba0 6c 74 69 70 6c 65 20 72 65 61 64 65 72 73 20 61 ltiple readers a
ebbb0 6e 64 20 77 72 69 74 65 72 73 20 6f 6e 20 74 68 nd writers on th
ebbc0 65 20 73 61 6d 65 0a 2a 2a 20 68 6f 73 74 20 28 e same.** host (
ebbd0 74 68 65 20 63 6f 6e 63 68 20 65 6e 73 75 72 65 the conch ensure
ebbe0 73 20 74 68 61 74 20 74 68 65 79 20 61 6c 6c 20 s that they all
ebbf0 75 73 65 20 74 68 65 20 73 61 6d 65 20 6c 6f 63 use the same loc
ebc00 61 6c 20 6c 6f 63 6b 20 66 69 6c 65 29 2e 0a 2a al lock file)..*
ebc10 2a 0a 2a 2a 20 54 68 65 72 65 20 69 73 20 61 20 *.** There is a
ebc20 74 68 69 72 64 20 66 69 6c 65 20 2d 20 74 68 65 third file - the
ebc30 20 68 6f 73 74 20 49 44 20 66 69 6c 65 20 2d 20 host ID file -
ebc40 75 73 65 64 20 61 73 20 61 20 70 65 72 73 69 73 used as a persis
ebc50 74 65 6e 74 20 72 65 63 6f 72 64 0a 2a 2a 20 6f tent record.** o
ebc60 66 20 61 20 75 6e 69 71 75 65 20 69 64 65 6e 74 f a unique ident
ebc70 69 66 69 65 72 20 66 6f 72 20 74 68 65 20 68 6f ifier for the ho
ebc80 73 74 2c 20 61 20 31 32 38 2d 62 79 74 65 20 75 st, a 128-byte u
ebc90 6e 69 71 75 65 20 68 6f 73 74 20 69 64 20 66 69 nique host id fi
ebca0 6c 65 0a 2a 2a 20 69 6e 20 74 68 65 20 70 61 74 le.** in the pat
ebcb0 68 20 64 65 66 69 6e 65 64 20 62 79 20 74 68 65 h defined by the
ebcc0 20 48 4f 53 54 49 44 50 41 54 48 20 6d 61 63 72 HOSTIDPATH macr
ebcd0 6f 20 28 64 65 66 61 75 6c 74 20 76 61 6c 75 65 o (default value
ebce0 20 69 73 0a 2a 2a 20 2f 4c 69 62 72 61 72 79 2f is.** /Library/
ebcf0 43 61 63 68 65 73 2f 2e 63 6f 6d 2e 61 70 70 6c Caches/.com.appl
ebd00 65 2e 73 71 6c 69 74 65 43 6f 6e 63 68 48 6f 73 e.sqliteConchHos
ebd10 74 49 64 29 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 tId)..**.** Requ
ebd20 65 73 74 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 esting the lock
ebd30 70 72 6f 78 79 20 64 6f 65 73 20 6e 6f 74 20 69 proxy does not i
ebd40 6d 6d 65 64 69 61 74 65 6c 79 20 74 61 6b 65 20 mmediately take
ebd50 74 68 65 20 63 6f 6e 63 68 2c 20 69 74 20 69 73 the conch, it is
ebd60 0a 2a 2a 20 6f 6e 6c 79 20 74 61 6b 65 6e 20 77 .** only taken w
ebd70 68 65 6e 20 74 68 65 20 66 69 72 73 74 20 72 65 hen the first re
ebd80 71 75 65 73 74 20 74 6f 20 6c 6f 63 6b 20 64 61 quest to lock da
ebd90 74 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 6d tabase file is m
ebda0 61 64 65 2e 20 20 0a 2a 2a 20 54 68 69 73 20 6d ade. .** This m
ebdb0 61 74 63 68 65 73 20 74 68 65 20 73 65 6d 61 6e atches the seman
ebdc0 74 69 63 73 20 6f 66 20 74 68 65 20 74 72 61 64 tics of the trad
ebdd0 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b 69 6e 67 20 itional locking
ebde0 62 65 68 61 76 69 6f 72 2c 20 77 68 65 72 65 0a behavior, where.
ebdf0 2a 2a 20 6f 70 65 6e 69 6e 67 20 61 20 63 6f 6e ** opening a con
ebe00 6e 65 63 74 69 6f 6e 20 74 6f 20 61 20 64 61 74 nection to a dat
ebe10 61 62 61 73 65 20 66 69 6c 65 20 64 6f 65 73 20 abase file does
ebe20 6e 6f 74 20 74 61 6b 65 20 61 20 6c 6f 63 6b 20 not take a lock
ebe30 6f 6e 20 69 74 2e 0a 2a 2a 20 54 68 65 20 73 68 on it..** The sh
ebe40 61 72 65 64 20 6c 6f 63 6b 20 61 6e 64 20 61 6e ared lock and an
ebe50 20 6f 70 65 6e 20 66 69 6c 65 20 64 65 73 63 72 open file descr
ebe60 69 70 74 6f 72 20 61 72 65 20 6d 61 69 6e 74 61 iptor are mainta
ebe70 69 6e 65 64 20 75 6e 74 69 6c 20 0a 2a 2a 20 74 ined until .** t
ebe80 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f he connection to
ebe90 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73 the database is
ebea0 20 63 6c 6f 73 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 closed. .**.**
ebeb0 54 68 65 20 70 72 6f 78 79 20 66 69 6c 65 20 61 The proxy file a
ebec0 6e 64 20 74 68 65 20 6c 6f 63 6b 20 66 69 6c 65 nd the lock file
ebed0 20 61 72 65 20 6e 65 76 65 72 20 64 65 6c 65 74 are never delet
ebee0 65 64 20 73 6f 20 74 68 65 79 20 6f 6e 6c 79 20 ed so they only
ebef0 6e 65 65 64 0a 2a 2a 20 74 6f 20 62 65 20 63 72 need.** to be cr
ebf00 65 61 74 65 64 20 74 68 65 20 66 69 72 73 74 20 eated the first
ebf10 74 69 6d 65 20 74 68 65 79 20 61 72 65 20 75 73 time they are us
ebf20 65 64 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6e 66 69 67 ed..**.** Config
ebf30 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 0a uration options.
ebf40 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ** -------------
ebf50 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 20 --------.**.**
ebf60 53 51 4c 49 54 45 5f 50 52 45 46 45 52 5f 50 52 SQLITE_PREFER_PR
ebf70 4f 58 59 5f 4c 4f 43 4b 49 4e 47 0a 2a 2a 0a 2a OXY_LOCKING.**.*
ebf80 2a 20 20 20 20 20 20 20 44 61 74 61 62 61 73 65 * Database
ebf90 20 66 69 6c 65 73 20 61 63 63 65 73 73 65 64 20 files accessed
ebfa0 6f 6e 20 6e 6f 6e 2d 6c 6f 63 61 6c 20 66 69 6c on non-local fil
ebfb0 65 20 73 79 73 74 65 6d 73 20 61 72 65 0a 2a 2a e systems are.**
ebfc0 20 20 20 20 20 20 20 61 75 74 6f 6d 61 74 69 63 automatic
ebfd0 61 6c 6c 79 20 63 6f 6e 66 69 67 75 72 65 64 20 ally configured
ebfe0 66 6f 72 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e for proxy lockin
ebff0 67 2c 20 6c 6f 63 6b 20 66 69 6c 65 73 20 61 72 g, lock files ar
ec000 65 0a 2a 2a 20 20 20 20 20 20 20 6e 61 6d 65 64 e.** named
ec010 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 75 automatically u
ec020 73 69 6e 67 20 74 68 65 20 73 61 6d 65 20 6c 6f sing the same lo
ec030 67 69 63 20 61 73 0a 2a 2a 20 20 20 20 20 20 20 gic as.**
ec040 50 52 41 47 4d 41 20 6c 6f 63 6b 5f 70 72 6f 78 PRAGMA lock_prox
ec050 79 5f 66 69 6c 65 3d 22 3a 61 75 74 6f 3a 22 0a y_file=":auto:".
ec060 2a 2a 20 20 20 20 0a 2a 2a 20 20 53 51 4c 49 54 ** .** SQLIT
ec070 45 5f 50 52 4f 58 59 5f 44 45 42 55 47 0a 2a 2a E_PROXY_DEBUG.**
ec080 0a 2a 2a 20 20 20 20 20 20 20 45 6e 61 62 6c 65 .** Enable
ec090 73 20 74 68 65 20 6c 6f 67 67 69 6e 67 20 6f 66 s the logging of
ec0a0 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 error messages
ec0b0 64 75 72 69 6e 67 20 68 6f 73 74 20 69 64 20 66 during host id f
ec0c0 69 6c 65 0a 2a 2a 20 20 20 20 20 20 20 72 65 74 ile.** ret
ec0d0 72 69 65 76 61 6c 20 61 6e 64 20 63 72 65 61 74 rieval and creat
ec0e0 69 6f 6e 0a 2a 2a 0a 2a 2a 20 20 48 4f 53 54 49 ion.**.** HOSTI
ec0f0 44 50 41 54 48 0a 2a 2a 0a 2a 2a 20 20 20 20 20 DPATH.**.**
ec100 20 20 4f 76 65 72 72 69 64 65 73 20 74 68 65 20 Overrides the
ec110 64 65 66 61 75 6c 74 20 68 6f 73 74 20 49 44 20 default host ID
ec120 66 69 6c 65 20 70 61 74 68 20 6c 6f 63 61 74 69 file path locati
ec130 6f 6e 0a 2a 2a 0a 2a 2a 20 20 4c 4f 43 4b 50 52 on.**.** LOCKPR
ec140 4f 58 59 44 49 52 0a 2a 2a 0a 2a 2a 20 20 20 20 OXYDIR.**.**
ec150 20 20 20 4f 76 65 72 72 69 64 65 73 20 74 68 65 Overrides the
ec160 20 64 65 66 61 75 6c 74 20 64 69 72 65 63 74 6f default directo
ec170 72 79 20 75 73 65 64 20 66 6f 72 20 6c 6f 63 6b ry used for lock
ec180 20 70 72 6f 78 79 20 66 69 6c 65 73 20 74 68 61 proxy files tha
ec190 74 0a 2a 2a 20 20 20 20 20 20 20 61 72 65 20 6e t.** are n
ec1a0 61 6d 65 64 20 61 75 74 6f 6d 61 74 69 63 61 6c amed automatical
ec1b0 6c 79 20 76 69 61 20 74 68 65 20 22 3a 61 75 74 ly via the ":aut
ec1c0 6f 3a 22 20 73 65 74 74 69 6e 67 0a 2a 2a 0a 2a o:" setting.**.*
ec1d0 2a 20 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c * SQLITE_DEFAUL
ec1e0 54 5f 50 52 4f 58 59 44 49 52 5f 50 45 52 4d 49 T_PROXYDIR_PERMI
ec1f0 53 53 49 4f 4e 53 0a 2a 2a 0a 2a 2a 20 20 20 20 SSIONS.**.**
ec200 20 20 20 50 65 72 6d 69 73 73 69 6f 6e 73 20 74 Permissions t
ec210 6f 20 75 73 65 20 77 68 65 6e 20 63 72 65 61 74 o use when creat
ec220 69 6e 67 20 61 20 64 69 72 65 63 74 6f 72 79 20 ing a directory
ec230 66 6f 72 20 73 74 6f 72 69 6e 67 20 74 68 65 0a for storing the.
ec240 2a 2a 20 20 20 20 20 20 20 6c 6f 63 6b 20 70 72 ** lock pr
ec250 6f 78 79 20 66 69 6c 65 73 2c 20 6f 6e 6c 79 20 oxy files, only
ec260 75 73 65 64 20 77 68 65 6e 20 4c 4f 43 4b 50 52 used when LOCKPR
ec270 4f 58 59 44 49 52 20 69 73 20 6e 6f 74 20 73 65 OXYDIR is not se
ec280 74 2e 0a 2a 2a 20 20 20 20 0a 2a 2a 20 20 20 20 t..** .**
ec290 0a 2a 2a 20 41 73 20 6d 65 6e 74 69 6f 6e 65 64 .** As mentioned
ec2a0 20 61 62 6f 76 65 2c 20 77 68 65 6e 20 63 6f 6d above, when com
ec2b0 70 69 6c 65 64 20 77 69 74 68 20 53 51 4c 49 54 piled with SQLIT
ec2c0 45 5f 50 52 45 46 45 52 5f 50 52 4f 58 59 5f 4c E_PREFER_PROXY_L
ec2d0 4f 43 4b 49 4e 47 2c 0a 2a 2a 20 73 65 74 74 69 OCKING,.** setti
ec2e0 6e 67 20 74 68 65 20 65 6e 76 69 72 6f 6e 6d 65 ng the environme
ec2f0 6e 74 20 76 61 72 69 61 62 6c 65 20 53 51 4c 49 nt variable SQLI
ec300 54 45 5f 46 4f 52 43 45 5f 50 52 4f 58 59 5f 4c TE_FORCE_PROXY_L
ec310 4f 43 4b 49 4e 47 20 74 6f 20 31 20 77 69 6c 6c OCKING to 1 will
ec320 0a 2a 2a 20 66 6f 72 63 65 20 70 72 6f 78 79 20 .** force proxy
ec330 6c 6f 63 6b 69 6e 67 20 74 6f 20 62 65 20 75 73 locking to be us
ec340 65 64 20 66 6f 72 20 65 76 65 72 79 20 64 61 74 ed for every dat
ec350 61 62 61 73 65 20 66 69 6c 65 20 6f 70 65 6e 65 abase file opene
ec360 64 2c 20 61 6e 64 20 30 0a 2a 2a 20 77 69 6c 6c d, and 0.** will
ec370 20 66 6f 72 63 65 20 61 75 74 6f 6d 61 74 69 63 force automatic
ec380 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 74 proxy locking t
ec390 6f 20 62 65 20 64 69 73 61 62 6c 65 64 20 66 6f o be disabled fo
ec3a0 72 20 61 6c 6c 20 64 61 74 61 62 61 73 65 0a 2a r all database.*
ec3b0 2a 20 66 69 6c 65 73 20 28 65 78 70 6c 69 63 69 * files (explici
ec3c0 74 79 20 63 61 6c 6c 69 6e 67 20 74 68 65 20 53 ty calling the S
ec3d0 51 4c 49 54 45 5f 53 45 54 5f 4c 4f 43 4b 50 52 QLITE_SET_LOCKPR
ec3e0 4f 58 59 46 49 4c 45 20 70 72 61 67 6d 61 20 6f OXYFILE pragma o
ec3f0 72 0a 2a 2a 20 73 71 6c 69 74 65 5f 66 69 6c 65 r.** sqlite_file
ec400 5f 63 6f 6e 74 72 6f 6c 20 41 50 49 20 69 73 20 _control API is
ec410 6e 6f 74 20 61 66 66 65 63 74 65 64 20 62 79 20 not affected by
ec420 53 51 4c 49 54 45 5f 46 4f 52 43 45 5f 50 52 4f SQLITE_FORCE_PRO
ec430 58 59 5f 4c 4f 43 4b 49 4e 47 29 2e 0a 2a 2f 0a XY_LOCKING)..*/.
ec440 0a 2f 2a 0a 2a 2a 20 50 72 6f 78 79 20 6c 6f 63 ./*.** Proxy loc
ec450 6b 69 6e 67 20 69 73 20 6f 6e 6c 79 20 61 76 61 king is only ava
ec460 69 6c 61 62 6c 65 20 6f 6e 20 4d 61 63 4f 53 58 ilable on MacOSX
ec470 20 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 .*/.#if defined
ec480 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 26 20 53 (__APPLE__) && S
ec490 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 QLITE_ENABLE_LOC
ec4a0 4b 49 4e 47 5f 53 54 59 4c 45 0a 0a 23 69 66 64 KING_STYLE..#ifd
ec4b0 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 2f ef SQLITE_TEST./
ec4c0 2a 20 73 69 6d 75 6c 61 74 65 20 6d 75 6c 74 69 * simulate multi
ec4d0 70 6c 65 20 68 6f 73 74 73 20 62 79 20 63 72 65 ple hosts by cre
ec4e0 61 74 69 6e 67 20 75 6e 69 71 75 65 20 68 6f 73 ating unique hos
ec4f0 74 69 64 20 66 69 6c 65 20 70 61 74 68 73 20 2a tid file paths *
ec500 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
ec510 20 73 71 6c 69 74 65 33 5f 68 6f 73 74 69 64 5f sqlite3_hostid_
ec520 6e 75 6d 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a num = 0;.#endif.
ec530 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 72 6f 78 79 ./*.** The proxy
ec540 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 68 LockingContext h
ec550 61 73 20 74 68 65 20 70 61 74 68 20 61 6e 64 20 as the path and
ec560 66 69 6c 65 20 73 74 72 75 63 74 75 72 65 73 20 file structures
ec570 66 6f 72 20 74 68 65 20 72 65 6d 6f 74 65 20 0a for the remote .
ec580 2a 2a 20 61 6e 64 20 6c 6f 63 61 6c 20 70 72 6f ** and local pro
ec590 78 79 20 66 69 6c 65 73 20 69 6e 20 69 74 0a 2a xy files in it.*
ec5a0 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 /.typedef struct
ec5b0 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e proxyLockingCon
ec5c0 74 65 78 74 20 70 72 6f 78 79 4c 6f 63 6b 69 6e text proxyLockin
ec5d0 67 43 6f 6e 74 65 78 74 3b 0a 73 74 72 75 63 74 gContext;.struct
ec5e0 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e proxyLockingCon
ec5f0 74 65 78 74 20 7b 0a 20 20 75 6e 69 78 46 69 6c text {. unixFil
ec600 65 20 2a 63 6f 6e 63 68 46 69 6c 65 3b 20 20 20 e *conchFile;
ec610 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 63 6f /* Open co
ec620 6e 63 68 20 66 69 6c 65 20 2a 2f 0a 20 20 63 68 nch file */. ch
ec630 61 72 20 2a 63 6f 6e 63 68 46 69 6c 65 50 61 74 ar *conchFilePat
ec640 68 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 h; /* Na
ec650 6d 65 20 6f 66 20 74 68 65 20 63 6f 6e 63 68 20 me of the conch
ec660 66 69 6c 65 20 2a 2f 0a 20 20 75 6e 69 78 46 69 file */. unixFi
ec670 6c 65 20 2a 6c 6f 63 6b 50 72 6f 78 79 3b 20 20 le *lockProxy;
ec680 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 70 /* Open p
ec690 72 6f 78 79 20 6c 6f 63 6b 20 66 69 6c 65 20 2a roxy lock file *
ec6a0 2f 0a 20 20 63 68 61 72 20 2a 6c 6f 63 6b 50 72 /. char *lockPr
ec6b0 6f 78 79 50 61 74 68 3b 20 20 20 20 20 20 20 20 oxyPath;
ec6c0 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 /* Name of the
ec6d0 70 72 6f 78 79 20 6c 6f 63 6b 20 66 69 6c 65 20 proxy lock file
ec6e0 2a 2f 0a 20 20 63 68 61 72 20 2a 64 62 50 61 74 */. char *dbPat
ec6f0 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 h;
ec700 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 /* Name of the
ec710 20 6f 70 65 6e 20 66 69 6c 65 20 2a 2f 0a 20 20 open file */.
ec720 69 6e 74 20 63 6f 6e 63 68 48 65 6c 64 3b 20 20 int conchHeld;
ec730 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
ec740 54 72 75 65 20 69 66 20 74 68 65 20 63 6f 6e 63 True if the conc
ec750 68 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 68 h is currently h
ec760 65 6c 64 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 6f eld */. void *o
ec770 6c 64 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 ldLockingContext
ec780 3b 20 20 20 20 20 2f 2a 20 4f 72 69 67 69 6e 61 ; /* Origina
ec790 6c 20 6c 6f 63 6b 69 6e 67 63 6f 6e 74 65 78 74 l lockingcontext
ec7a0 20 74 6f 20 72 65 73 74 6f 72 65 20 6f 6e 20 63 to restore on c
ec7b0 6c 6f 73 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 lose */. sqlite
ec7c0 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 63 6f 6e 3_io_methods con
ec7d0 73 74 20 2a 70 4f 6c 64 4d 65 74 68 6f 64 3b 20 st *pOldMethod;
ec7e0 20 20 20 20 2f 2a 20 4f 72 69 67 69 6e 61 6c 20 /* Original
ec7f0 49 2f 4f 20 6d 65 74 68 6f 64 73 20 66 6f 72 20 I/O methods for
ec800 63 6c 6f 73 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 close */.};../*
ec810 48 4f 53 54 49 44 4c 45 4e 20 61 6e 64 20 43 4f HOSTIDLEN and CO
ec820 4e 43 48 4c 45 4e 20 62 6f 74 68 20 69 6e 63 6c NCHLEN both incl
ec830 75 64 65 20 73 70 61 63 65 20 66 6f 72 20 74 68 ude space for th
ec840 65 20 73 74 72 69 6e 67 20 0a 2a 2a 20 74 65 72 e string .** ter
ec850 6d 69 6e 61 74 69 6e 67 20 6e 75 6c 20 0a 2a 2f minating nul .*/
ec860 0a 23 64 65 66 69 6e 65 20 48 4f 53 54 49 44 4c .#define HOSTIDL
ec870 45 4e 20 20 20 20 20 20 20 20 20 31 32 38 0a 23 EN 128.#
ec880 64 65 66 69 6e 65 20 43 4f 4e 43 48 4c 45 4e 20 define CONCHLEN
ec890 20 20 20 20 20 20 20 20 20 28 4d 41 58 50 41 54 (MAXPAT
ec8a0 48 4c 45 4e 2b 48 4f 53 54 49 44 4c 45 4e 2b 31 HLEN+HOSTIDLEN+1
ec8b0 29 0a 23 69 66 6e 64 65 66 20 48 4f 53 54 49 44 ).#ifndef HOSTID
ec8c0 50 41 54 48 0a 23 20 64 65 66 69 6e 65 20 48 4f PATH.# define HO
ec8d0 53 54 49 44 50 41 54 48 20 20 20 20 20 20 20 22 STIDPATH "
ec8e0 2f 4c 69 62 72 61 72 79 2f 43 61 63 68 65 73 2f /Library/Caches/
ec8f0 2e 63 6f 6d 2e 61 70 70 6c 65 2e 73 71 6c 69 74 .com.apple.sqlit
ec900 65 43 6f 6e 63 68 48 6f 73 74 49 64 22 0a 23 65 eConchHostId".#e
ec910 6e 64 69 66 0a 0a 2f 2a 20 62 61 73 69 63 61 6c ndif../* basical
ec920 6c 79 20 61 20 63 6f 70 79 20 6f 66 20 75 6e 69 ly a copy of uni
ec930 78 52 61 6e 64 6f 6d 6e 65 73 73 20 77 69 74 68 xRandomness with
ec940 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 74 65 different.** te
ec950 73 74 20 62 65 68 61 76 69 6f 72 20 62 75 69 6c st behavior buil
ec960 74 20 69 6e 20 2a 2f 0a 73 74 61 74 69 63 20 69 t in */.static i
ec970 6e 74 20 70 72 6f 78 79 47 65 6e 65 72 61 74 65 nt proxyGenerate
ec980 48 6f 73 74 49 44 28 63 68 61 72 20 2a 70 48 6f HostID(char *pHo
ec990 73 74 49 44 29 7b 0a 20 20 69 6e 74 20 70 69 64 stID){. int pid
ec9a0 2c 20 66 64 2c 20 6c 65 6e 3b 0a 20 20 75 6e 73 , fd, len;. uns
ec9b0 69 67 6e 65 64 20 63 68 61 72 20 2a 6b 65 79 20 igned char *key
ec9c0 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 = (unsigned char
ec9d0 20 2a 29 70 48 6f 73 74 49 44 3b 0a 20 20 0a 20 *)pHostID;. .
ec9e0 20 6d 65 6d 73 65 74 28 6b 65 79 2c 20 30 2c 20 memset(key, 0,
ec9f0 48 4f 53 54 49 44 4c 45 4e 29 3b 0a 20 20 6c 65 HOSTIDLEN);. le
eca00 6e 20 3d 20 30 3b 0a 20 20 66 64 20 3d 20 6f 70 n = 0;. fd = op
eca10 65 6e 28 22 2f 64 65 76 2f 75 72 61 6e 64 6f 6d en("/dev/urandom
eca20 22 2c 20 4f 5f 52 44 4f 4e 4c 59 29 3b 0a 20 20 ", O_RDONLY);.
eca30 69 66 28 20 66 64 3e 3d 30 20 29 7b 0a 20 20 20 if( fd>=0 ){.
eca40 20 6c 65 6e 20 3d 20 72 65 61 64 28 66 64 2c 20 len = read(fd,
eca50 6b 65 79 2c 20 48 4f 53 54 49 44 4c 45 4e 29 3b key, HOSTIDLEN);
eca60 0a 20 20 20 20 63 6c 6f 73 65 28 66 64 29 3b 20 . close(fd);
eca70 2f 2a 20 73 69 6c 65 6e 74 6c 79 20 6c 65 61 6b /* silently leak
eca80 20 74 68 65 20 66 64 20 69 66 20 69 74 20 66 61 the fd if it fa
eca90 69 6c 73 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 ils */. }. if(
ecaa0 20 6c 65 6e 20 3c 20 48 4f 53 54 49 44 4c 45 4e len < HOSTIDLEN
ecab0 20 29 7b 0a 20 20 20 20 74 69 6d 65 5f 74 20 74 ){. time_t t
ecac0 3b 0a 20 20 20 20 74 69 6d 65 28 26 74 29 3b 0a ;. time(&t);.
ecad0 20 20 20 20 6d 65 6d 63 70 79 28 6b 65 79 2c 20 memcpy(key,
ecae0 26 74 2c 20 73 69 7a 65 6f 66 28 74 29 29 3b 0a &t, sizeof(t));.
ecaf0 20 20 20 20 70 69 64 20 3d 20 67 65 74 70 69 64 pid = getpid
ecb00 28 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 ();. memcpy(&
ecb10 6b 65 79 5b 73 69 7a 65 6f 66 28 74 29 5d 2c 20 key[sizeof(t)],
ecb20 26 70 69 64 2c 20 73 69 7a 65 6f 66 28 70 69 64 &pid, sizeof(pid
ecb30 29 29 3b 0a 20 20 7d 0a 20 20 0a 23 69 66 64 65 ));. }. .#ifde
ecb40 66 20 4d 41 4b 45 5f 50 52 45 54 54 59 5f 48 4f f MAKE_PRETTY_HO
ecb50 53 54 49 44 0a 20 20 7b 0a 20 20 20 20 69 6e 74 STID. {. int
ecb60 20 69 3b 0a 20 20 20 20 2f 2a 20 66 69 6c 74 65 i;. /* filte
ecb70 72 20 74 68 65 20 62 79 74 65 73 20 69 6e 74 6f r the bytes into
ecb80 20 70 72 69 6e 74 61 62 6c 65 20 61 73 63 69 69 printable ascii
ecb90 20 63 68 61 72 61 63 74 65 72 73 20 61 6e 64 20 characters and
ecba0 4e 55 4c 20 74 65 72 6d 69 6e 61 74 65 20 2a 2f NUL terminate */
ecbb0 0a 20 20 20 20 6b 65 79 5b 28 48 4f 53 54 49 44 . key[(HOSTID
ecbc0 4c 45 4e 2d 31 29 5d 20 3d 20 30 78 30 30 3b 0a LEN-1)] = 0x00;.
ecbd0 20 20 20 20 66 6f 72 28 20 69 3d 30 3b 20 69 3c for( i=0; i<
ecbe0 28 48 4f 53 54 49 44 4c 45 4e 2d 31 29 3b 20 69 (HOSTIDLEN-1); i
ecbf0 2b 2b 20 29 7b 0a 20 20 20 20 20 20 75 6e 73 69 ++ ){. unsi
ecc00 67 6e 65 64 20 63 68 61 72 20 70 61 20 3d 20 6b gned char pa = k
ecc10 65 79 5b 69 5d 26 30 78 37 46 3b 0a 20 20 20 20 ey[i]&0x7F;.
ecc20 20 20 69 66 28 20 70 61 3c 30 78 32 30 20 29 7b if( pa<0x20 ){
ecc30 0a 20 20 20 20 20 20 20 20 6b 65 79 5b 69 5d 20 . key[i]
ecc40 3d 20 28 6b 65 79 5b 69 5d 26 30 78 38 30 20 3d = (key[i]&0x80 =
ecc50 3d 20 30 78 38 30 29 20 3f 20 70 61 2b 30 78 34 = 0x80) ? pa+0x4
ecc60 30 20 3a 20 70 61 2b 30 78 32 30 3b 0a 20 20 20 0 : pa+0x20;.
ecc70 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 61 3d }else if( pa=
ecc80 3d 30 78 37 46 20 29 7b 0a 20 20 20 20 20 20 20 =0x7F ){.
ecc90 20 6b 65 79 5b 69 5d 20 3d 20 28 6b 65 79 5b 69 key[i] = (key[i
ecca0 5d 26 30 78 38 30 20 3d 3d 20 30 78 38 30 29 20 ]&0x80 == 0x80)
eccb0 3f 20 70 61 3d 30 78 32 30 20 3a 20 70 61 2b 30 ? pa=0x20 : pa+0
eccc0 78 37 45 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 x7E;. }.
eccd0 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 }. }.#endif.
ecce0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
eccf0 3b 0a 7d 0a 0a 2f 2a 20 77 72 69 74 65 73 20 74 ;.}../* writes t
ecd00 68 65 20 68 6f 73 74 20 69 64 20 70 61 74 68 20 he host id path
ecd10 74 6f 20 70 61 74 68 2c 20 70 61 74 68 20 73 68 to path, path sh
ecd20 6f 75 6c 64 20 62 65 20 61 6e 20 70 72 65 2d 61 ould be an pre-a
ecd30 6c 6c 6f 63 61 74 65 64 20 62 75 66 66 65 72 0a llocated buffer.
ecd40 2a 2a 20 77 69 74 68 20 65 6e 6f 75 67 68 20 73 ** with enough s
ecd50 70 61 63 65 20 66 6f 72 20 61 20 70 61 74 68 20 pace for a path
ecd60 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
ecd70 70 72 6f 78 79 47 65 74 48 6f 73 74 49 44 50 61 proxyGetHostIDPa
ecd80 74 68 28 63 68 61 72 20 2a 70 61 74 68 2c 20 73 th(char *path, s
ecd90 69 7a 65 5f 74 20 6c 65 6e 29 7b 0a 20 20 73 74 ize_t len){. st
ecda0 72 6c 63 70 79 28 70 61 74 68 2c 20 48 4f 53 54 rlcpy(path, HOST
ecdb0 49 44 50 41 54 48 2c 20 6c 65 6e 29 3b 0a 23 69 IDPATH, len);.#i
ecdc0 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 fdef SQLITE_TEST
ecdd0 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 68 . if( sqlite3_h
ecde0 6f 73 74 69 64 5f 6e 75 6d 3e 30 20 29 7b 0a 20 ostid_num>0 ){.
ecdf0 20 20 20 63 68 61 72 20 73 75 66 66 69 78 5b 32 char suffix[2
ece00 5d 20 3d 20 22 31 22 3b 0a 20 20 20 20 73 75 66 ] = "1";. suf
ece10 66 69 78 5b 30 5d 20 3d 20 73 75 66 66 69 78 5b fix[0] = suffix[
ece20 30 5d 20 2b 20 73 71 6c 69 74 65 33 5f 68 6f 73 0] + sqlite3_hos
ece30 74 69 64 5f 6e 75 6d 3b 0a 20 20 20 20 73 74 72 tid_num;. str
ece40 6c 63 61 74 28 70 61 74 68 2c 20 73 75 66 66 69 lcat(path, suffi
ece50 78 2c 20 6c 65 6e 29 3b 0a 20 20 7d 0a 23 65 6e x, len);. }.#en
ece60 64 69 66 0a 20 20 4f 53 54 52 41 43 45 33 28 22 dif. OSTRACE3("
ece70 47 45 54 48 4f 53 54 49 44 50 41 54 48 20 20 25 GETHOSTIDPATH %
ece80 73 20 70 69 64 3d 25 64 5c 6e 22 2c 20 70 61 74 s pid=%d\n", pat
ece90 68 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 7d 0a h, getpid());.}.
ecea0 0a 2f 2a 20 67 65 74 20 74 68 65 20 68 6f 73 74 ./* get the host
eceb0 20 49 44 20 66 72 6f 6d 20 61 20 73 71 6c 69 74 ID from a sqlit
ecec0 65 20 68 6f 73 74 69 64 20 66 69 6c 65 20 73 74 e hostid file st
eced0 6f 72 65 64 20 69 6e 20 74 68 65 20 0a 2a 2a 20 ored in the .**
ecee0 75 73 65 72 2d 73 70 65 63 69 66 69 63 20 74 6d user-specific tm
ecef0 70 20 64 69 72 65 63 74 6f 72 79 2c 20 63 72 65 p directory, cre
ecf00 61 74 65 20 74 68 65 20 49 44 20 69 66 20 69 74 ate the ID if it
ecf10 27 73 20 6e 6f 74 20 74 68 65 72 65 20 61 6c 72 's not there alr
ecf20 65 61 64 79 20 0a 2a 2f 0a 73 74 61 74 69 63 20 eady .*/.static
ecf30 69 6e 74 20 70 72 6f 78 79 47 65 74 48 6f 73 74 int proxyGetHost
ecf40 49 44 28 63 68 61 72 20 2a 70 48 6f 73 74 49 44 ID(char *pHostID
ecf50 2c 20 69 6e 74 20 2a 70 45 72 72 6f 72 29 7b 0a , int *pError){.
ecf60 20 20 69 6e 74 20 66 64 3b 0a 20 20 63 68 61 72 int fd;. char
ecf70 20 70 61 74 68 5b 4d 41 58 50 41 54 48 4c 45 4e path[MAXPATHLEN
ecf80 5d 3b 20 0a 20 20 73 69 7a 65 5f 74 20 6c 65 6e ]; . size_t len
ecf90 3b 0a 20 20 69 6e 74 20 72 63 3d 53 51 4c 49 54 ;. int rc=SQLIT
ecfa0 45 5f 4f 4b 3b 0a 0a 20 20 70 72 6f 78 79 47 65 E_OK;.. proxyGe
ecfb0 74 48 6f 73 74 49 44 50 61 74 68 28 70 61 74 68 tHostIDPath(path
ecfc0 2c 20 4d 41 58 50 41 54 48 4c 45 4e 29 3b 0a 20 , MAXPATHLEN);.
ecfd0 20 2f 2a 20 74 72 79 20 74 6f 20 63 72 65 61 74 /* try to creat
ecfe0 65 20 74 68 65 20 68 6f 73 74 20 49 44 20 66 69 e the host ID fi
ecff0 6c 65 2c 20 69 66 20 69 74 20 61 6c 72 65 61 64 le, if it alread
ed000 79 20 65 78 69 73 74 73 20 72 65 61 64 20 74 68 y exists read th
ed010 65 20 63 6f 6e 74 65 6e 74 73 20 2a 2f 0a 20 20 e contents */.
ed020 66 64 20 3d 20 6f 70 65 6e 28 70 61 74 68 2c 20 fd = open(path,
ed030 4f 5f 43 52 45 41 54 7c 4f 5f 57 52 4f 4e 4c 59 O_CREAT|O_WRONLY
ed040 7c 4f 5f 45 58 43 4c 2c 20 30 36 34 34 29 3b 0a |O_EXCL, 0644);.
ed050 20 20 69 66 28 20 66 64 3c 30 20 29 7b 0a 20 20 if( fd<0 ){.
ed060 20 20 69 6e 74 20 65 72 72 3d 65 72 72 6e 6f 3b int err=errno;
ed070 0a 09 09 0a 20 20 20 20 69 66 28 20 65 72 72 21 .... if( err!
ed080 3d 45 45 58 49 53 54 20 29 7b 0a 23 69 66 64 65 =EEXIST ){.#ifde
ed090 66 20 53 51 4c 49 54 45 5f 50 52 4f 58 59 5f 44 f SQLITE_PROXY_D
ed0a0 45 42 55 47 20 2f 2a 20 73 65 74 20 74 68 65 20 EBUG /* set the
ed0b0 73 71 6c 69 74 65 20 65 72 72 6f 72 20 6d 65 73 sqlite error mes
ed0c0 73 61 67 65 20 69 6e 73 74 65 61 64 20 2a 2f 0a sage instead */.
ed0d0 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 73 74 fprintf(st
ed0e0 64 65 72 72 2c 20 22 73 71 6c 69 74 65 20 65 72 derr, "sqlite er
ed0f0 72 6f 72 20 63 72 65 61 74 69 6e 67 20 68 6f 73 ror creating hos
ed100 74 20 49 44 20 66 69 6c 65 20 25 73 3a 20 25 73 t ID file %s: %s
ed110 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 \n",.
ed120 20 20 20 70 61 74 68 2c 20 73 74 72 65 72 72 6f path, strerro
ed130 72 28 65 72 72 29 29 3b 0a 23 65 6e 64 69 66 0a r(err));.#endif.
ed140 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
ed150 49 54 45 5f 50 45 52 4d 3b 0a 20 20 20 20 7d 0a ITE_PERM;. }.
ed160 20 20 20 20 2f 2a 20 63 6f 75 6c 64 6e 27 74 20 /* couldn't
ed170 63 72 65 61 74 65 20 74 68 65 20 66 69 6c 65 2c create the file,
ed180 20 72 65 61 64 20 69 74 20 69 6e 73 74 65 61 64 read it instead
ed190 20 2a 2f 0a 20 20 20 20 66 64 20 3d 20 6f 70 65 */. fd = ope
ed1a0 6e 28 70 61 74 68 2c 20 4f 5f 52 44 4f 4e 4c 59 n(path, O_RDONLY
ed1b0 7c 4f 5f 45 58 43 4c 29 3b 0a 20 20 20 20 69 66 |O_EXCL);. if
ed1c0 28 20 66 64 3c 30 20 29 7b 0a 23 69 66 64 65 66 ( fd<0 ){.#ifdef
ed1d0 20 53 51 4c 49 54 45 5f 50 52 4f 58 59 5f 44 45 SQLITE_PROXY_DE
ed1e0 42 55 47 20 2f 2a 20 73 65 74 20 74 68 65 20 73 BUG /* set the s
ed1f0 71 6c 69 74 65 20 65 72 72 6f 72 20 6d 65 73 73 qlite error mess
ed200 61 67 65 20 69 6e 73 74 65 61 64 20 2a 2f 0a 20 age instead */.
ed210 20 20 20 20 20 69 6e 74 20 65 72 72 20 3d 20 65 int err = e
ed220 72 72 6e 6f 3b 0a 20 20 20 20 20 20 66 70 72 69 rrno;. fpri
ed230 6e 74 66 28 73 74 64 65 72 72 2c 20 22 73 71 6c ntf(stderr, "sql
ed240 69 74 65 20 65 72 72 6f 72 20 6f 70 65 6e 69 6e ite error openin
ed250 67 20 68 6f 73 74 20 49 44 20 66 69 6c 65 20 25 g host ID file %
ed260 73 3a 20 25 73 5c 6e 22 2c 0a 20 20 20 20 20 20 s: %s\n",.
ed270 20 20 20 20 20 20 20 20 70 61 74 68 2c 20 73 74 path, st
ed280 72 65 72 72 6f 72 28 65 72 72 29 29 3b 0a 23 65 rerror(err));.#e
ed290 6e 64 69 66 0a 20 20 20 20 20 20 72 65 74 75 72 ndif. retur
ed2a0 6e 20 53 51 4c 49 54 45 5f 50 45 52 4d 3b 0a 20 n SQLITE_PERM;.
ed2b0 20 20 20 7d 0a 20 20 20 20 6c 65 6e 20 3d 20 70 }. len = p
ed2c0 72 65 61 64 28 66 64 2c 20 70 48 6f 73 74 49 44 read(fd, pHostID
ed2d0 2c 20 48 4f 53 54 49 44 4c 45 4e 2c 20 30 29 3b , HOSTIDLEN, 0);
ed2e0 0a 20 20 20 20 69 66 28 20 6c 65 6e 3c 30 20 29 . if( len<0 )
ed2f0 7b 0a 20 20 20 20 20 20 2a 70 45 72 72 6f 72 20 {. *pError
ed300 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 = errno;. r
ed310 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 c = SQLITE_IOERR
ed320 5f 52 45 41 44 3b 0a 20 20 20 20 7d 65 6c 73 65 _READ;. }else
ed330 20 69 66 28 20 6c 65 6e 3c 48 4f 53 54 49 44 4c if( len<HOSTIDL
ed340 45 4e 20 29 7b 0a 20 20 20 20 20 20 2a 70 45 72 EN ){. *pEr
ed350 72 6f 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 ror = 0;. r
ed360 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 c = SQLITE_IOERR
ed370 5f 53 48 4f 52 54 5f 52 45 41 44 3b 0a 20 20 20 _SHORT_READ;.
ed380 20 7d 0a 20 20 20 20 63 6c 6f 73 65 28 66 64 29 }. close(fd)
ed390 3b 20 2f 2a 20 73 69 6c 65 6e 74 6c 79 20 6c 65 ; /* silently le
ed3a0 61 6b 20 74 68 65 20 66 64 20 69 66 20 69 74 20 ak the fd if it
ed3b0 66 61 69 6c 73 20 2a 2f 0a 20 20 20 20 4f 53 54 fails */. OST
ed3c0 52 41 43 45 33 28 22 47 45 54 48 4f 53 54 49 44 RACE3("GETHOSTID
ed3d0 20 20 72 65 61 64 20 25 73 20 70 69 64 3d 25 64 read %s pid=%d
ed3e0 5c 6e 22 2c 20 70 48 6f 73 74 49 44 2c 20 67 65 \n", pHostID, ge
ed3f0 74 70 69 64 28 29 29 3b 0a 20 20 20 20 72 65 74 tpid());. ret
ed400 75 72 6e 20 72 63 3b 0a 20 20 7d 65 6c 73 65 7b urn rc;. }else{
ed410 0a 20 20 20 20 2f 2a 20 77 65 27 72 65 20 63 72 . /* we're cr
ed420 65 61 74 69 6e 67 20 74 68 65 20 68 6f 73 74 20 eating the host
ed430 49 44 20 66 69 6c 65 20 28 75 73 65 20 61 20 72 ID file (use a r
ed440 61 6e 64 6f 6d 20 73 74 72 69 6e 67 20 6f 66 20 andom string of
ed450 62 79 74 65 73 29 20 2a 2f 0a 20 20 20 20 70 72 bytes) */. pr
ed460 6f 78 79 47 65 6e 65 72 61 74 65 48 6f 73 74 49 oxyGenerateHostI
ed470 44 28 70 48 6f 73 74 49 44 29 3b 0a 20 20 20 20 D(pHostID);.
ed480 6c 65 6e 20 3d 20 70 77 72 69 74 65 28 66 64 2c len = pwrite(fd,
ed490 20 70 48 6f 73 74 49 44 2c 20 48 4f 53 54 49 44 pHostID, HOSTID
ed4a0 4c 45 4e 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 LEN, 0);. if(
ed4b0 20 6c 65 6e 3c 30 20 29 7b 0a 20 20 20 20 20 20 len<0 ){.
ed4c0 2a 70 45 72 72 6f 72 20 3d 20 65 72 72 6e 6f 3b *pError = errno;
ed4d0 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 . rc = SQLI
ed4e0 54 45 5f 49 4f 45 52 52 5f 57 52 49 54 45 3b 0a TE_IOERR_WRITE;.
ed4f0 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 65 }else if( le
ed500 6e 3c 48 4f 53 54 49 44 4c 45 4e 20 29 7b 0a 20 n<HOSTIDLEN ){.
ed510 20 20 20 20 20 2a 70 45 72 72 6f 72 20 3d 20 30 *pError = 0
ed520 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c ;. rc = SQL
ed530 49 54 45 5f 49 4f 45 52 52 5f 57 52 49 54 45 3b ITE_IOERR_WRITE;
ed540 0a 20 20 20 20 7d 0a 20 20 20 20 63 6c 6f 73 65 . }. close
ed550 28 66 64 29 3b 20 2f 2a 20 73 69 6c 65 6e 74 6c (fd); /* silentl
ed560 79 20 6c 65 61 6b 20 74 68 65 20 66 64 20 69 66 y leak the fd if
ed570 20 69 74 20 66 61 69 6c 73 20 2a 2f 0a 20 20 20 it fails */.
ed580 20 4f 53 54 52 41 43 45 33 28 22 47 45 54 48 4f OSTRACE3("GETHO
ed590 53 54 49 44 20 20 77 72 6f 74 65 20 25 73 20 70 STID wrote %s p
ed5a0 69 64 3d 25 64 5c 6e 22 2c 20 70 48 6f 73 74 49 id=%d\n", pHostI
ed5b0 44 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 20 20 D, getpid());.
ed5c0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d return rc;. }
ed5d0 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 70 .}..static int p
ed5e0 72 6f 78 79 47 65 74 4c 6f 63 6b 50 61 74 68 28 roxyGetLockPath(
ed5f0 63 6f 6e 73 74 20 63 68 61 72 20 2a 64 62 50 61 const char *dbPa
ed600 74 68 2c 20 63 68 61 72 20 2a 6c 50 61 74 68 2c th, char *lPath,
ed610 20 73 69 7a 65 5f 74 20 6d 61 78 4c 65 6e 29 7b size_t maxLen){
ed620 0a 20 20 69 6e 74 20 6c 65 6e 3b 0a 20 20 69 6e . int len;. in
ed630 74 20 64 62 4c 65 6e 3b 0a 20 20 69 6e 74 20 69 t dbLen;. int i
ed640 3b 0a 0a 23 69 66 64 65 66 20 4c 4f 43 4b 50 52 ;..#ifdef LOCKPR
ed650 4f 58 59 44 49 52 0a 20 20 6c 65 6e 20 3d 20 73 OXYDIR. len = s
ed660 74 72 6c 63 70 79 28 6c 50 61 74 68 2c 20 4c 4f trlcpy(lPath, LO
ed670 43 4b 50 52 4f 58 59 44 49 52 2c 20 6d 61 78 4c CKPROXYDIR, maxL
ed680 65 6e 29 3b 0a 23 65 6c 73 65 0a 23 20 69 66 64 en);.#else.# ifd
ed690 65 66 20 5f 43 53 5f 44 41 52 57 49 4e 5f 55 53 ef _CS_DARWIN_US
ed6a0 45 52 5f 54 45 4d 50 5f 44 49 52 0a 20 20 7b 0a ER_TEMP_DIR. {.
ed6b0 20 20 20 20 63 6f 6e 66 73 74 72 28 5f 43 53 5f confstr(_CS_
ed6c0 44 41 52 57 49 4e 5f 55 53 45 52 5f 54 45 4d 50 DARWIN_USER_TEMP
ed6d0 5f 44 49 52 2c 20 6c 50 61 74 68 2c 20 6d 61 78 _DIR, lPath, max
ed6e0 4c 65 6e 29 3b 0a 20 20 20 20 6c 65 6e 20 3d 20 Len);. len =
ed6f0 73 74 72 6c 63 61 74 28 6c 50 61 74 68 2c 20 22 strlcat(lPath, "
ed700 73 71 6c 69 74 65 70 6c 6f 63 6b 73 22 2c 20 6d sqliteplocks", m
ed710 61 78 4c 65 6e 29 3b 0a 20 20 20 20 69 66 28 20 axLen);. if(
ed720 6d 6b 64 69 72 28 6c 50 61 74 68 2c 20 53 51 4c mkdir(lPath, SQL
ed730 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 52 4f 58 ITE_DEFAULT_PROX
ed740 59 44 49 52 5f 50 45 52 4d 49 53 53 49 4f 4e 53 YDIR_PERMISSIONS
ed750 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 69 66 ) ){. /* if
ed760 20 6d 6b 64 69 72 20 66 61 69 6c 73 2c 20 68 61 mkdir fails, ha
ed770 6e 64 6c 65 20 61 73 20 6c 6f 63 6b 20 66 69 6c ndle as lock fil
ed780 65 20 63 72 65 61 74 69 6f 6e 20 66 61 69 6c 75 e creation failu
ed790 72 65 20 2a 2f 0a 23 20 20 69 66 64 65 66 20 53 re */.# ifdef S
ed7a0 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 QLITE_DEBUG.
ed7b0 20 20 69 6e 74 20 65 72 72 20 3d 20 65 72 72 6e int err = errn
ed7c0 6f 3b 0a 20 20 20 20 20 20 69 66 28 20 65 72 72 o;. if( err
ed7d0 21 3d 45 45 58 49 53 54 20 29 7b 0a 20 20 20 20 !=EEXIST ){.
ed7e0 20 20 20 20 66 70 72 69 6e 74 66 28 73 74 64 65 fprintf(stde
ed7f0 72 72 2c 20 22 70 72 6f 78 79 47 65 74 4c 6f 63 rr, "proxyGetLoc
ed800 6b 50 61 74 68 3a 20 6d 6b 64 69 72 28 25 73 2c kPath: mkdir(%s,
ed810 30 25 6f 29 20 65 72 72 6f 72 20 25 64 20 25 73 0%o) error %d %s
ed820 5c 6e 22 2c 20 6c 50 61 74 68 2c 0a 20 20 20 20 \n", lPath,.
ed830 20 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 SQLI
ed840 54 45 5f 44 45 46 41 55 4c 54 5f 50 52 4f 58 59 TE_DEFAULT_PROXY
ed850 44 49 52 5f 50 45 52 4d 49 53 53 49 4f 4e 53 2c DIR_PERMISSIONS,
ed860 20 65 72 72 2c 20 73 74 72 65 72 72 6f 72 28 65 err, strerror(e
ed870 72 72 29 29 3b 0a 20 20 20 20 20 20 7d 0a 23 20 rr));. }.#
ed880 20 65 6e 64 69 66 0a 20 20 20 20 7d 65 6c 73 65 endif. }else
ed890 7b 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 33 {. OSTRACE3
ed8a0 28 22 47 45 54 4c 4f 43 4b 50 41 54 48 20 20 6d ("GETLOCKPATH m
ed8b0 6b 64 69 72 20 25 73 20 70 69 64 3d 25 64 5c 6e kdir %s pid=%d\n
ed8c0 22 2c 20 6c 50 61 74 68 2c 20 67 65 74 70 69 64 ", lPath, getpid
ed8d0 28 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 0a ());. }. .
ed8e0 20 20 7d 0a 23 20 65 6c 73 65 0a 20 20 6c 65 6e }.# else. len
ed8f0 20 3d 20 73 74 72 6c 63 70 79 28 6c 50 61 74 68 = strlcpy(lPath
ed900 2c 20 22 2f 74 6d 70 2f 22 2c 20 6d 61 78 4c 65 , "/tmp/", maxLe
ed910 6e 29 3b 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 n);.# endif.#end
ed920 69 66 0a 0a 20 20 69 66 28 20 6c 50 61 74 68 5b if.. if( lPath[
ed930 6c 65 6e 2d 31 5d 21 3d 27 2f 27 20 29 7b 0a 20 len-1]!='/' ){.
ed940 20 20 20 6c 65 6e 20 3d 20 73 74 72 6c 63 61 74 len = strlcat
ed950 28 6c 50 61 74 68 2c 20 22 2f 22 2c 20 6d 61 78 (lPath, "/", max
ed960 4c 65 6e 29 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f Len);. }. . /
ed970 2a 20 74 72 61 6e 73 66 6f 72 6d 20 74 68 65 20 * transform the
ed980 64 62 20 70 61 74 68 20 74 6f 20 61 20 75 6e 69 db path to a uni
ed990 71 75 65 20 63 61 63 68 65 20 6e 61 6d 65 20 2a que cache name *
ed9a0 2f 0a 20 20 64 62 4c 65 6e 20 3d 20 28 69 6e 74 /. dbLen = (int
ed9b0 29 73 74 72 6c 65 6e 28 64 62 50 61 74 68 29 3b )strlen(dbPath);
ed9c0 0a 20 20 66 6f 72 28 20 69 3d 30 3b 20 69 3c 64 . for( i=0; i<d
ed9d0 62 4c 65 6e 20 26 26 20 28 69 2b 6c 65 6e 2b 37 bLen && (i+len+7
ed9e0 29 3c 6d 61 78 4c 65 6e 3b 20 69 2b 2b 29 7b 0a )<maxLen; i++){.
ed9f0 20 20 20 20 63 68 61 72 20 63 20 3d 20 64 62 50 char c = dbP
eda00 61 74 68 5b 69 5d 3b 0a 20 20 20 20 6c 50 61 74 ath[i];. lPat
eda10 68 5b 69 2b 6c 65 6e 5d 20 3d 20 28 63 3d 3d 27 h[i+len] = (c=='
eda20 2f 27 29 3f 27 5f 27 3a 63 3b 0a 20 20 7d 0a 20 /')?'_':c;. }.
eda30 20 6c 50 61 74 68 5b 69 2b 6c 65 6e 5d 3d 27 5c lPath[i+len]='\
eda40 30 27 3b 0a 20 20 73 74 72 6c 63 61 74 28 6c 50 0';. strlcat(lP
eda50 61 74 68 2c 20 22 3a 61 75 74 6f 3a 22 2c 20 6d ath, ":auto:", m
eda60 61 78 4c 65 6e 29 3b 0a 20 20 72 65 74 75 72 6e axLen);. return
eda70 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f SQLITE_OK;.}../
eda80 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 *.** Create a ne
eda90 77 20 56 46 53 20 66 69 6c 65 20 64 65 73 63 72 w VFS file descr
edaa0 69 70 74 6f 72 20 28 73 74 6f 72 65 64 20 69 6e iptor (stored in
edab0 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 memory obtained
edac0 20 66 72 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65 33 from.** sqlite3
edad0 5f 6d 61 6c 6c 6f 63 29 20 61 6e 64 20 6f 70 65 _malloc) and ope
edae0 6e 20 74 68 65 20 66 69 6c 65 20 6e 61 6d 65 64 n the file named
edaf0 20 22 70 61 74 68 22 20 69 6e 20 74 68 65 20 66 "path" in the f
edb00 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 2e 0a ile descriptor..
edb10 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 **.** The caller
edb20 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 is responsible
edb30 6e 6f 74 20 6f 6e 6c 79 20 66 6f 72 20 63 6c 6f not only for clo
edb40 73 69 6e 67 20 74 68 65 20 66 69 6c 65 20 64 65 sing the file de
edb50 73 63 72 69 70 74 6f 72 0a 2a 2a 20 62 75 74 20 scriptor.** but
edb60 61 6c 73 6f 20 66 6f 72 20 66 72 65 65 69 6e 67 also for freeing
edb70 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 73 73 6f the memory asso
edb80 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 ciated with the
edb90 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 2e file descriptor.
edba0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 .*/.static int p
edbb0 72 6f 78 79 43 72 65 61 74 65 55 6e 69 78 46 69 roxyCreateUnixFi
edbc0 6c 65 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 le(const char *p
edbd0 61 74 68 2c 20 75 6e 69 78 46 69 6c 65 20 2a 2a ath, unixFile **
edbe0 70 70 46 69 6c 65 29 20 7b 0a 20 20 75 6e 69 78 ppFile) {. unix
edbf0 46 69 6c 65 20 2a 70 4e 65 77 3b 0a 20 20 69 6e File *pNew;. in
edc00 74 20 66 6c 61 67 73 20 3d 20 53 51 4c 49 54 45 t flags = SQLITE
edc10 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 7c 53 51 _OPEN_MAIN_DB|SQ
edc20 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 LITE_OPEN_CREATE
edc30 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 |SQLITE_OPEN_REA
edc40 44 57 52 49 54 45 3b 0a 20 20 69 6e 74 20 72 63 DWRITE;. int rc
edc50 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
edc60 73 71 6c 69 74 65 33 5f 76 66 73 20 64 75 6d 6d sqlite3_vfs dumm
edc70 79 56 66 73 3b 0a 0a 20 20 70 4e 65 77 20 3d 20 yVfs;.. pNew =
edc80 28 75 6e 69 78 46 69 6c 65 20 2a 29 73 71 6c 69 (unixFile *)sqli
edc90 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f te3_malloc(sizeo
edca0 66 28 75 6e 69 78 46 69 6c 65 29 29 3b 0a 20 20 f(unixFile));.
edcb0 69 66 28 20 21 70 4e 65 77 20 29 7b 0a 20 20 20 if( !pNew ){.
edcc0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e return SQLITE_N
edcd0 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 OMEM;. }. mems
edce0 65 74 28 70 4e 65 77 2c 20 30 2c 20 73 69 7a 65 et(pNew, 0, size
edcf0 6f 66 28 75 6e 69 78 46 69 6c 65 29 29 3b 0a 0a of(unixFile));..
edd00 20 20 2f 2a 20 43 61 6c 6c 20 75 6e 69 78 4f 70 /* Call unixOp
edd10 65 6e 28 29 20 74 6f 20 6f 70 65 6e 20 74 68 65 en() to open the
edd20 20 70 72 6f 78 79 20 66 69 6c 65 2e 20 54 68 65 proxy file. The
edd30 20 66 6c 61 67 73 20 70 61 73 73 65 64 20 74 6f flags passed to
edd40 20 75 6e 69 78 4f 70 65 6e 28 29 0a 20 20 2a 2a unixOpen(). **
edd50 20 73 75 67 67 65 73 74 20 74 68 61 74 20 74 68 suggest that th
edd60 65 20 66 69 6c 65 20 62 65 69 6e 67 20 6f 70 65 e file being ope
edd70 6e 65 64 20 69 73 20 61 20 22 6d 61 69 6e 20 64 ned is a "main d
edd80 61 74 61 62 61 73 65 22 2e 20 54 68 69 73 20 69 atabase". This i
edd90 73 0a 20 20 2a 2a 20 6e 65 63 65 73 73 61 72 79 s. ** necessary
edda0 20 61 73 20 6f 74 68 65 72 20 66 69 6c 65 20 74 as other file t
eddb0 79 70 65 73 20 64 6f 20 6e 6f 74 20 6e 65 63 65 ypes do not nece
eddc0 73 73 61 72 69 6c 79 20 73 75 70 70 6f 72 74 20 ssarily support
eddd0 6c 6f 63 6b 69 6e 67 2e 20 49 74 0a 20 20 2a 2a locking. It. **
edde0 20 69 73 20 62 65 74 74 65 72 20 74 6f 20 75 73 is better to us
eddf0 65 20 75 6e 69 78 4f 70 65 6e 28 29 20 69 6e 73 e unixOpen() ins
ede00 74 65 61 64 20 6f 66 20 6f 70 65 6e 69 6e 67 20 tead of opening
ede10 74 68 65 20 66 69 6c 65 20 64 69 72 65 63 74 6c the file directl
ede20 79 20 77 69 74 68 0a 20 20 2a 2a 20 6f 70 65 6e y with. ** open
ede30 28 29 2c 20 61 73 20 75 6e 69 78 4f 70 65 6e 28 (), as unixOpen(
ede40 29 20 73 65 74 73 20 75 70 20 74 68 65 20 76 61 ) sets up the va
ede50 72 69 6f 75 73 20 6d 65 63 68 61 6e 69 73 6d 73 rious mechanisms
ede60 20 72 65 71 75 69 72 65 64 20 74 6f 0a 20 20 2a required to. *
ede70 2a 20 6d 61 6b 65 20 73 75 72 65 20 61 20 63 61 * make sure a ca
ede80 6c 6c 20 74 6f 20 63 6c 6f 73 65 28 29 20 64 6f ll to close() do
ede90 65 73 20 6e 6f 74 20 63 61 75 73 65 20 74 68 65 es not cause the
edea0 20 73 79 73 74 65 6d 20 74 6f 20 64 69 73 63 61 system to disca
edeb0 72 64 0a 20 20 2a 2a 20 50 4f 53 49 58 20 6c 6f rd. ** POSIX lo
edec0 63 6b 73 20 70 72 65 6d 61 74 75 72 65 6c 79 2e cks prematurely.
eded0 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 74 20 69 73 . **. ** It is
edee0 20 69 6d 70 6f 72 74 61 6e 74 20 74 68 61 74 20 important that
edef0 74 68 65 20 78 4f 70 65 6e 20 6d 65 6d 62 65 72 the xOpen member
edf00 20 6f 66 20 74 68 65 20 56 46 53 20 6f 62 6a 65 of the VFS obje
edf10 63 74 20 70 61 73 73 65 64 20 74 6f 20 0a 20 20 ct passed to .
edf20 2a 2a 20 75 6e 69 78 4f 70 65 6e 28 29 20 69 73 ** unixOpen() is
edf30 20 4e 55 4c 4c 2e 20 54 68 69 73 20 74 65 6c 6c NULL. This tell
edf40 73 20 75 6e 69 78 4f 70 65 6e 28 29 20 6d 61 79 s unixOpen() may
edf50 20 74 72 79 20 74 6f 20 6f 70 65 6e 20 61 20 70 try to open a p
edf60 72 6f 78 79 2d 66 69 6c 65 20 0a 20 20 2a 2a 20 roxy-file . **
edf70 66 6f 72 20 74 68 65 20 70 72 6f 78 79 2d 66 69 for the proxy-fi
edf80 6c 65 20 28 63 72 65 61 74 69 6e 67 20 61 20 70 le (creating a p
edf90 6f 74 65 6e 74 69 61 6c 20 69 6e 66 69 6e 69 74 otential infinit
edfa0 65 20 6c 6f 6f 70 29 2e 0a 20 20 2a 2f 0a 20 20 e loop).. */.
edfb0 64 75 6d 6d 79 56 66 73 2e 70 41 70 70 44 61 74 dummyVfs.pAppDat
edfc0 61 20 3d 20 28 76 6f 69 64 2a 29 26 61 75 74 6f a = (void*)&auto
edfd0 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 3b 0a 20 20 lockIoFinder;.
edfe0 64 75 6d 6d 79 56 66 73 2e 78 4f 70 65 6e 20 3d dummyVfs.xOpen =
edff0 20 30 3b 0a 20 20 72 63 20 3d 20 75 6e 69 78 4f 0;. rc = unixO
ee000 70 65 6e 28 26 64 75 6d 6d 79 56 66 73 2c 20 70 pen(&dummyVfs, p
ee010 61 74 68 2c 20 28 73 71 6c 69 74 65 33 5f 66 69 ath, (sqlite3_fi
ee020 6c 65 20 2a 29 70 4e 65 77 2c 20 66 6c 61 67 73 le *)pNew, flags
ee030 2c 20 26 66 6c 61 67 73 29 3b 0a 20 20 69 66 28 , &flags);. if(
ee040 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 rc==SQLITE_OK &
ee050 26 20 28 66 6c 61 67 73 26 53 51 4c 49 54 45 5f & (flags&SQLITE_
ee060 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 29 20 29 OPEN_READONLY) )
ee070 7b 0a 20 20 20 20 70 4e 65 77 2d 3e 70 4d 65 74 {. pNew->pMet
ee080 68 6f 64 2d 3e 78 43 6c 6f 73 65 28 28 73 71 6c hod->xClose((sql
ee090 69 74 65 33 5f 66 69 6c 65 20 2a 29 70 4e 65 77 ite3_file *)pNew
ee0a0 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 );. rc = SQLI
ee0b0 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20 20 7d TE_CANTOPEN;. }
ee0c0 0a 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 .. if( rc!=SQLI
ee0d0 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c TE_OK ){. sql
ee0e0 69 74 65 33 5f 66 72 65 65 28 70 4e 65 77 29 3b ite3_free(pNew);
ee0f0 0a 20 20 20 20 70 4e 65 77 20 3d 20 30 3b 0a 20 . pNew = 0;.
ee100 20 7d 0a 0a 20 20 2a 70 70 46 69 6c 65 20 3d 20 }.. *ppFile =
ee110 70 4e 65 77 3b 0a 20 20 72 65 74 75 72 6e 20 72 pNew;. return r
ee120 63 3b 0a 7d 0a 0a 2f 2a 20 74 61 6b 65 73 20 74 c;.}../* takes t
ee130 68 65 20 63 6f 6e 63 68 20 62 79 20 74 61 6b 69 he conch by taki
ee140 6e 67 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b ng a shared lock
ee150 20 61 6e 64 20 72 65 61 64 20 74 68 65 20 63 6f and read the co
ee160 6e 74 65 6e 74 73 20 63 6f 6e 63 68 2c 20 69 66 ntents conch, if
ee170 20 0a 2a 2a 20 6c 6f 63 6b 50 61 74 68 20 69 73 .** lockPath is
ee180 20 6e 6f 6e 2d 4e 55 4c 4c 2c 20 74 68 65 20 68 non-NULL, the h
ee190 6f 73 74 20 49 44 20 61 6e 64 20 6c 6f 63 6b 20 ost ID and lock
ee1a0 66 69 6c 65 20 70 61 74 68 20 6d 75 73 74 20 6d file path must m
ee1b0 61 74 63 68 2e 20 20 41 20 4e 55 4c 4c 20 0a 2a atch. A NULL .*
ee1c0 2a 20 6c 6f 63 6b 50 61 74 68 20 6d 65 61 6e 73 * lockPath means
ee1d0 20 74 68 61 74 20 74 68 65 20 6c 6f 63 6b 50 61 that the lockPa
ee1e0 74 68 20 69 6e 20 74 68 65 20 63 6f 6e 63 68 20 th in the conch
ee1f0 66 69 6c 65 20 77 69 6c 6c 20 62 65 20 75 73 65 file will be use
ee200 64 20 69 66 20 74 68 65 20 0a 2a 2a 20 68 6f 73 d if the .** hos
ee210 74 20 49 44 73 20 6d 61 74 63 68 2c 20 6f 72 20 t IDs match, or
ee220 61 20 6e 65 77 20 6c 6f 63 6b 20 70 61 74 68 20 a new lock path
ee230 77 69 6c 6c 20 62 65 20 67 65 6e 65 72 61 74 65 will be generate
ee240 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 d automatically
ee250 0a 2a 2a 20 61 6e 64 20 77 72 69 74 74 65 6e 20 .** and written
ee260 74 6f 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c to the conch fil
ee270 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
ee280 20 70 72 6f 78 79 54 61 6b 65 43 6f 6e 63 68 28 proxyTakeConch(
ee290 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 29 unixFile *pFile)
ee2a0 7b 0a 20 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 {. proxyLocking
ee2b0 43 6f 6e 74 65 78 74 20 2a 70 43 74 78 20 3d 20 Context *pCtx =
ee2c0 28 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e (proxyLockingCon
ee2d0 74 65 78 74 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f text *)pFile->lo
ee2e0 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 20 0a 20 ckingContext; .
ee2f0 20 0a 20 20 69 66 28 20 70 43 74 78 2d 3e 63 6f . if( pCtx->co
ee300 6e 63 68 48 65 6c 64 3e 30 20 29 7b 0a 20 20 20 nchHeld>0 ){.
ee310 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
ee320 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 K;. }else{.
ee330 75 6e 69 78 46 69 6c 65 20 2a 63 6f 6e 63 68 46 unixFile *conchF
ee340 69 6c 65 20 3d 20 70 43 74 78 2d 3e 63 6f 6e 63 ile = pCtx->conc
ee350 68 46 69 6c 65 3b 0a 20 20 20 20 63 68 61 72 20 hFile;. char
ee360 74 65 73 74 56 61 6c 75 65 5b 43 4f 4e 43 48 4c testValue[CONCHL
ee370 45 4e 5d 3b 0a 20 20 20 20 63 68 61 72 20 63 6f EN];. char co
ee380 6e 63 68 56 61 6c 75 65 5b 43 4f 4e 43 48 4c 45 nchValue[CONCHLE
ee390 4e 5d 3b 0a 20 20 20 20 63 68 61 72 20 6c 6f 63 N];. char loc
ee3a0 6b 50 61 74 68 5b 4d 41 58 50 41 54 48 4c 45 4e kPath[MAXPATHLEN
ee3b0 5d 3b 0a 20 20 20 20 63 68 61 72 20 2a 74 4c 6f ];. char *tLo
ee3c0 63 6b 50 61 74 68 20 3d 20 4e 55 4c 4c 3b 0a 20 ckPath = NULL;.
ee3d0 20 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 int rc = SQLI
ee3e0 54 45 5f 4f 4b 3b 0a 20 20 20 20 69 6e 74 20 72 TE_OK;. int r
ee3f0 65 61 64 52 63 20 3d 20 53 51 4c 49 54 45 5f 4f eadRc = SQLITE_O
ee400 4b 3b 0a 20 20 20 20 69 6e 74 20 73 79 6e 63 50 K;. int syncP
ee410 65 72 6d 73 20 3d 20 30 3b 0a 0a 20 20 20 20 4f erms = 0;.. O
ee420 53 54 52 41 43 45 34 28 22 54 41 4b 45 43 4f 4e STRACE4("TAKECON
ee430 43 48 20 20 25 64 20 66 6f 72 20 25 73 20 70 69 CH %d for %s pi
ee440 64 3d 25 64 5c 6e 22 2c 20 63 6f 6e 63 68 46 69 d=%d\n", conchFi
ee450 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 20 le->h,.
ee460 20 20 20 20 28 70 43 74 78 2d 3e 6c 6f 63 6b 50 (pCtx->lockP
ee470 72 6f 78 79 50 61 74 68 20 3f 20 70 43 74 78 2d roxyPath ? pCtx-
ee480 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 3a >lockProxyPath :
ee490 20 22 3a 61 75 74 6f 3a 22 29 2c 20 67 65 74 70 ":auto:"), getp
ee4a0 69 64 28 29 29 3b 0a 0a 20 20 20 20 72 63 20 3d id());.. rc =
ee4b0 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 70 4d 65 74 conchFile->pMet
ee4c0 68 6f 64 2d 3e 78 4c 6f 63 6b 28 28 73 71 6c 69 hod->xLock((sqli
ee4d0 74 65 33 5f 66 69 6c 65 2a 29 63 6f 6e 63 68 46 te3_file*)conchF
ee4e0 69 6c 65 2c 20 53 48 41 52 45 44 5f 4c 4f 43 4b ile, SHARED_LOCK
ee4f0 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 );. if( rc==S
ee500 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
ee510 20 20 69 6e 74 20 70 45 72 72 6f 72 20 3d 20 30 int pError = 0
ee520 3b 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28 74 ;. memset(t
ee530 65 73 74 56 61 6c 75 65 2c 20 30 2c 20 43 4f 4e estValue, 0, CON
ee540 43 48 4c 45 4e 29 3b 20 2f 2a 20 63 6f 6e 63 68 CHLEN); /* conch
ee550 20 69 73 20 66 69 78 65 64 20 73 69 7a 65 20 2a is fixed size *
ee560 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 70 72 6f /. rc = pro
ee570 78 79 47 65 74 48 6f 73 74 49 44 28 74 65 73 74 xyGetHostID(test
ee580 56 61 6c 75 65 2c 20 26 70 45 72 72 6f 72 29 3b Value, &pError);
ee590 0a 20 20 20 20 20 20 69 66 28 20 28 72 63 26 30 . if( (rc&0
ee5a0 78 66 66 29 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 xff)==SQLITE_IOE
ee5b0 52 52 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 RR ){. pF
ee5c0 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d ile->lastErrno =
ee5d0 20 70 45 72 72 6f 72 3b 0a 20 20 20 20 20 20 7d pError;. }
ee5e0 0a 20 20 20 20 20 20 69 66 28 20 70 43 74 78 2d . if( pCtx-
ee5f0 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 29 >lockProxyPath )
ee600 7b 0a 20 20 20 20 20 20 20 20 73 74 72 6c 63 70 {. strlcp
ee610 79 28 26 74 65 73 74 56 61 6c 75 65 5b 48 4f 53 y(&testValue[HOS
ee620 54 49 44 4c 45 4e 5d 2c 20 70 43 74 78 2d 3e 6c TIDLEN], pCtx->l
ee630 6f 63 6b 50 72 6f 78 79 50 61 74 68 2c 20 4d 41 ockProxyPath, MA
ee640 58 50 41 54 48 4c 45 4e 29 3b 0a 20 20 20 20 20 XPATHLEN);.
ee650 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 }. }. if(
ee660 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
ee670 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 {. goto end
ee680 5f 74 61 6b 65 63 6f 6e 63 68 3b 0a 20 20 20 20 _takeconch;.
ee690 7d 0a 20 20 20 20 0a 20 20 20 20 72 65 61 64 52 }. . readR
ee6a0 63 20 3d 20 75 6e 69 78 52 65 61 64 28 28 73 71 c = unixRead((sq
ee6b0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 63 6f 6e lite3_file *)con
ee6c0 63 68 46 69 6c 65 2c 20 63 6f 6e 63 68 56 61 6c chFile, conchVal
ee6d0 75 65 2c 20 43 4f 4e 43 48 4c 45 4e 2c 20 30 29 ue, CONCHLEN, 0)
ee6e0 3b 0a 20 20 20 20 69 66 28 20 72 65 61 64 52 63 ;. if( readRc
ee6f0 21 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 !=SQLITE_IOERR_S
ee700 48 4f 52 54 5f 52 45 41 44 20 29 7b 0a 20 20 20 HORT_READ ){.
ee710 20 20 20 69 66 28 20 72 65 61 64 52 63 21 3d 53 if( readRc!=S
ee720 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
ee730 20 20 20 20 69 66 28 20 28 72 63 26 30 78 66 66 if( (rc&0xff
ee740 29 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 )==SQLITE_IOERR
ee750 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 46 69 ){. pFi
ee760 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 le->lastErrno =
ee770 63 6f 6e 63 68 46 69 6c 65 2d 3e 6c 61 73 74 45 conchFile->lastE
ee780 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 7d 0a rrno;. }.
ee790 20 20 20 20 20 20 20 20 72 63 20 3d 20 72 65 61 rc = rea
ee7a0 64 52 63 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 dRc;. got
ee7b0 6f 20 65 6e 64 5f 74 61 6b 65 63 6f 6e 63 68 3b o end_takeconch;
ee7c0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 2f . }. /
ee7d0 2a 20 69 66 20 74 68 65 20 63 6f 6e 63 68 20 68 * if the conch h
ee7e0 61 73 20 64 61 74 61 20 63 6f 6d 70 61 72 65 20 as data compare
ee7f0 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 2a 2f 0a the contents */.
ee800 20 20 20 20 20 20 69 66 28 20 21 70 43 74 78 2d if( !pCtx-
ee810 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 29 >lockProxyPath )
ee820 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 66 6f 72 {. /* for
ee830 20 61 75 74 6f 2d 6e 61 6d 65 64 20 6c 6f 63 61 auto-named loca
ee840 6c 20 6c 6f 63 6b 20 66 69 6c 65 2c 20 6a 75 73 l lock file, jus
ee850 74 20 63 68 65 63 6b 20 74 68 65 20 68 6f 73 74 t check the host
ee860 20 49 44 20 61 6e 64 20 77 65 27 6c 6c 0a 20 20 ID and we'll.
ee870 20 20 20 20 20 20 20 2a 2a 20 75 73 65 20 74 68 ** use th
ee880 65 20 6c 6f 63 61 6c 20 6c 6f 63 6b 20 66 69 6c e local lock fil
ee890 65 20 70 61 74 68 20 74 68 61 74 27 73 20 61 6c e path that's al
ee8a0 72 65 61 64 79 20 69 6e 20 74 68 65 72 65 20 2a ready in there *
ee8b0 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 6d /. if( !m
ee8c0 65 6d 63 6d 70 28 74 65 73 74 56 61 6c 75 65 2c emcmp(testValue,
ee8d0 20 63 6f 6e 63 68 56 61 6c 75 65 2c 20 48 4f 53 conchValue, HOS
ee8e0 54 49 44 4c 45 4e 29 20 29 7b 0a 20 20 20 20 20 TIDLEN) ){.
ee8f0 20 20 20 20 20 74 4c 6f 63 6b 50 61 74 68 20 3d tLockPath =
ee900 20 28 63 68 61 72 20 2a 29 26 63 6f 6e 63 68 56 (char *)&conchV
ee910 61 6c 75 65 5b 48 4f 53 54 49 44 4c 45 4e 5d 3b alue[HOSTIDLEN];
ee920 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 . goto
ee930 65 6e 64 5f 74 61 6b 65 63 6f 6e 63 68 3b 0a 20 end_takeconch;.
ee940 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
ee950 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a else{. /*
ee960 20 77 65 27 76 65 20 67 6f 74 20 74 68 65 20 63 we've got the c
ee970 6f 6e 63 68 20 69 66 20 63 6f 6e 63 68 56 61 6c onch if conchVal
ee980 75 65 20 6d 61 74 63 68 65 73 20 6f 75 72 20 70 ue matches our p
ee990 61 74 68 20 61 6e 64 20 68 6f 73 74 20 49 44 20 ath and host ID
ee9a0 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 */. if( !
ee9b0 6d 65 6d 63 6d 70 28 74 65 73 74 56 61 6c 75 65 memcmp(testValue
ee9c0 2c 20 63 6f 6e 63 68 56 61 6c 75 65 2c 20 43 4f , conchValue, CO
ee9d0 4e 43 48 4c 45 4e 29 20 29 7b 0a 20 20 20 20 20 NCHLEN) ){.
ee9e0 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 74 61 goto end_ta
ee9f0 6b 65 63 6f 6e 63 68 3b 0a 20 20 20 20 20 20 20 keconch;.
eea00 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d }. }. }
eea10 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 61 else{. /* a
eea20 20 73 68 6f 72 74 20 72 65 61 64 20 6d 65 61 6e short read mean
eea30 73 20 77 65 27 72 65 20 22 63 72 65 61 74 69 6e s we're "creatin
eea40 67 22 20 74 68 65 20 63 6f 6e 63 68 20 28 65 76 g" the conch (ev
eea50 65 6e 20 74 68 6f 75 67 68 20 69 74 20 63 6f 75 en though it cou
eea60 6c 64 20 0a 20 20 20 20 20 20 2a 2a 20 68 61 76 ld . ** hav
eea70 65 20 62 65 65 6e 20 75 73 65 72 2d 69 6e 74 65 e been user-inte
eea80 72 76 65 6e 74 69 6f 6e 29 2c 20 69 66 20 77 65 rvention), if we
eea90 20 61 63 71 75 69 72 65 20 74 68 65 20 65 78 63 acquire the exc
eeaa0 6c 75 73 69 76 65 20 6c 6f 63 6b 2c 0a 20 20 20 lusive lock,.
eeab0 20 20 20 2a 2a 20 77 65 27 6c 6c 20 74 72 79 20 ** we'll try
eeac0 74 6f 20 6d 61 74 63 68 20 74 68 65 20 63 75 72 to match the cur
eead0 72 65 6e 74 20 6f 6e 2d 64 69 73 6b 20 70 65 72 rent on-disk per
eeae0 6d 69 73 73 69 6f 6e 73 20 6f 66 20 74 68 65 20 missions of the
eeaf0 64 61 74 61 62 61 73 65 0a 20 20 20 20 20 20 2a database. *
eeb00 2f 0a 20 20 20 20 20 20 73 79 6e 63 50 65 72 6d /. syncPerm
eeb10 73 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 s = 1;. }.
eeb20 20 0a 20 20 20 20 2f 2a 20 65 69 74 68 65 72 20 . /* either
eeb30 63 6f 6e 63 68 20 77 61 73 20 65 6d 74 70 79 20 conch was emtpy
eeb40 6f 72 20 64 69 64 6e 27 74 20 6d 61 74 63 68 20 or didn't match
eeb50 2a 2f 0a 20 20 20 20 69 66 28 20 21 70 43 74 78 */. if( !pCtx
eeb60 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 ->lockProxyPath
eeb70 29 7b 0a 20 20 20 20 20 20 70 72 6f 78 79 47 65 ){. proxyGe
eeb80 74 4c 6f 63 6b 50 61 74 68 28 70 43 74 78 2d 3e tLockPath(pCtx->
eeb90 64 62 50 61 74 68 2c 20 6c 6f 63 6b 50 61 74 68 dbPath, lockPath
eeba0 2c 20 4d 41 58 50 41 54 48 4c 45 4e 29 3b 0a 20 , MAXPATHLEN);.
eebb0 20 20 20 20 20 74 4c 6f 63 6b 50 61 74 68 20 3d tLockPath =
eebc0 20 6c 6f 63 6b 50 61 74 68 3b 0a 20 20 20 20 20 lockPath;.
eebd0 20 73 74 72 6c 63 70 79 28 26 74 65 73 74 56 61 strlcpy(&testVa
eebe0 6c 75 65 5b 48 4f 53 54 49 44 4c 45 4e 5d 2c 20 lue[HOSTIDLEN],
eebf0 6c 6f 63 6b 50 61 74 68 2c 20 4d 41 58 50 41 54 lockPath, MAXPAT
eec00 48 4c 45 4e 29 3b 0a 20 20 20 20 7d 0a 20 20 20 HLEN);. }.
eec10 20 0a 20 20 20 20 2f 2a 20 75 70 64 61 74 65 20 . /* update
eec20 63 6f 6e 63 68 20 77 69 74 68 20 68 6f 73 74 20 conch with host
eec30 61 6e 64 20 70 61 74 68 20 28 74 68 69 73 20 77 and path (this w
eec40 69 6c 6c 20 66 61 69 6c 20 69 66 20 6f 74 68 65 ill fail if othe
eec50 72 20 70 72 6f 63 65 73 73 0a 20 20 20 20 20 2a r process. *
eec60 2a 20 68 61 73 20 61 20 73 68 61 72 65 64 20 6c * has a shared l
eec70 6f 63 6b 20 61 6c 72 65 61 64 79 29 20 2a 2f 0a ock already) */.
eec80 20 20 20 20 72 63 20 3d 20 63 6f 6e 63 68 46 69 rc = conchFi
eec90 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 4c 6f le->pMethod->xLo
eeca0 63 6b 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ck((sqlite3_file
eecb0 2a 29 63 6f 6e 63 68 46 69 6c 65 2c 20 45 58 43 *)conchFile, EXC
eecc0 4c 55 53 49 56 45 5f 4c 4f 43 4b 29 3b 0a 20 20 LUSIVE_LOCK);.
eecd0 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
eece0 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 _OK ){. rc
eecf0 3d 20 75 6e 69 78 57 72 69 74 65 28 28 73 71 6c = unixWrite((sql
eed00 69 74 65 33 5f 66 69 6c 65 20 2a 29 63 6f 6e 63 ite3_file *)conc
eed10 68 46 69 6c 65 2c 20 74 65 73 74 56 61 6c 75 65 hFile, testValue
eed20 2c 20 43 4f 4e 43 48 4c 45 4e 2c 20 30 29 3b 0a , CONCHLEN, 0);.
eed30 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 if( rc==SQ
eed40 4c 49 54 45 5f 4f 4b 20 26 26 20 73 79 6e 63 50 LITE_OK && syncP
eed50 65 72 6d 73 20 29 7b 0a 20 20 20 20 20 20 20 20 erms ){.
eed60 73 74 72 75 63 74 20 73 74 61 74 20 62 75 66 3b struct stat buf;
eed70 0a 20 20 20 20 20 20 20 20 69 6e 74 20 65 72 72 . int err
eed80 20 3d 20 66 73 74 61 74 28 70 46 69 6c 65 2d 3e = fstat(pFile->
eed90 68 2c 20 26 62 75 66 29 3b 0a 20 20 20 20 20 20 h, &buf);.
eeda0 20 20 69 66 28 20 65 72 72 3d 3d 30 20 29 7b 0a if( err==0 ){.
eedb0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 74 72 79 /* try
eedc0 20 74 6f 20 6d 61 74 63 68 20 74 68 65 20 64 61 to match the da
eedd0 74 61 62 61 73 65 20 66 69 6c 65 20 70 65 72 6d tabase file perm
eede0 69 73 73 69 6f 6e 73 2c 20 69 67 6e 6f 72 65 20 issions, ignore
eedf0 66 61 69 6c 75 72 65 20 2a 2f 0a 23 69 66 6e 64 failure */.#ifnd
eee00 65 66 20 53 51 4c 49 54 45 5f 50 52 4f 58 59 5f ef SQLITE_PROXY_
eee10 44 45 42 55 47 0a 20 20 20 20 20 20 20 20 20 20 DEBUG.
eee20 66 63 68 6d 6f 64 28 63 6f 6e 63 68 46 69 6c 65 fchmod(conchFile
eee30 2d 3e 68 2c 20 62 75 66 2e 73 74 5f 6d 6f 64 65 ->h, buf.st_mode
eee40 29 3b 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 20 );.#else.
eee50 20 20 20 69 66 28 20 66 63 68 6d 6f 64 28 63 6f if( fchmod(co
eee60 6e 63 68 46 69 6c 65 2d 3e 68 2c 20 62 75 66 2e nchFile->h, buf.
eee70 73 74 5f 6d 6f 64 65 29 21 3d 30 20 29 7b 0a 20 st_mode)!=0 ){.
eee80 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 63 int c
eee90 6f 64 65 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 ode = errno;.
eeea0 20 20 20 20 20 20 20 20 20 66 70 72 69 6e 74 66 fprintf
eeeb0 28 73 74 64 65 72 72 2c 20 22 66 63 68 6d 6f 64 (stderr, "fchmod
eeec0 20 25 6f 20 46 41 49 4c 45 44 20 77 69 74 68 20 %o FAILED with
eeed0 25 64 20 25 73 5c 6e 22 2c 0a 20 20 20 20 20 20 %d %s\n",.
eeee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
eeef0 20 20 20 20 20 20 20 62 75 66 2e 73 74 5f 6d 6f buf.st_mo
eef00 64 65 2c 20 63 6f 64 65 2c 20 73 74 72 65 72 72 de, code, strerr
eef10 6f 72 28 63 6f 64 65 29 29 3b 0a 20 20 20 20 20 or(code));.
eef20 20 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 } else {.
eef30 20 20 20 20 20 20 20 20 20 20 66 70 72 69 6e 74 fprint
eef40 66 28 73 74 64 65 72 72 2c 20 22 66 63 68 6d 6f f(stderr, "fchmo
eef50 64 20 25 6f 20 53 55 43 43 45 44 45 44 5c 6e 22 d %o SUCCEDED\n"
eef60 2c 62 75 66 2e 73 74 5f 6d 6f 64 65 29 3b 0a 20 ,buf.st_mode);.
eef70 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
eef80 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
eef90 20 20 20 20 69 6e 74 20 63 6f 64 65 20 3d 20 65 int code = e
eefa0 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 20 20 rrno;.
eefb0 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20 fprintf(stderr,
eefc0 22 53 54 41 54 20 46 41 49 4c 45 44 5b 25 64 5d "STAT FAILED[%d]
eefd0 20 77 69 74 68 20 25 64 20 25 73 5c 6e 22 2c 20 with %d %s\n",
eefe0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
eeff0 20 20 20 20 20 20 20 20 20 20 20 65 72 72 2c 20 err,
ef000 63 6f 64 65 2c 20 73 74 72 65 72 72 6f 72 28 63 code, strerror(c
ef010 6f 64 65 29 29 3b 0a 23 65 6e 64 69 66 0a 20 20 ode));.#endif.
ef020 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
ef030 20 20 20 20 7d 0a 20 20 20 20 63 6f 6e 63 68 46 }. conchF
ef040 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 55 ile->pMethod->xU
ef050 6e 6c 6f 63 6b 28 28 73 71 6c 69 74 65 33 5f 66 nlock((sqlite3_f
ef060 69 6c 65 2a 29 63 6f 6e 63 68 46 69 6c 65 2c 20 ile*)conchFile,
ef070 53 48 41 52 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20 SHARED_LOCK);.
ef080 0a 65 6e 64 5f 74 61 6b 65 63 6f 6e 63 68 3a 0a .end_takeconch:.
ef090 20 20 20 20 4f 53 54 52 41 43 45 32 28 22 54 52 OSTRACE2("TR
ef0a0 41 4e 53 50 52 4f 58 59 3a 20 43 4c 4f 53 45 20 ANSPROXY: CLOSE
ef0b0 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 %d\n", pFile->h
ef0c0 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 );. if( rc==S
ef0d0 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 46 69 6c QLITE_OK && pFil
ef0e0 65 2d 3e 6f 70 65 6e 46 6c 61 67 73 20 29 7b 0a e->openFlags ){.
ef0f0 20 20 20 20 20 20 69 66 28 20 70 46 69 6c 65 2d if( pFile-
ef100 3e 68 3e 3d 30 20 29 7b 0a 23 69 66 64 65 66 20 >h>=0 ){.#ifdef
ef110 53 54 52 49 43 54 5f 43 4c 4f 53 45 5f 45 52 52 STRICT_CLOSE_ERR
ef120 4f 52 0a 20 20 20 20 20 20 20 20 69 66 28 20 63 OR. if( c
ef130 6c 6f 73 65 28 70 46 69 6c 65 2d 3e 68 29 20 29 lose(pFile->h) )
ef140 7b 0a 20 20 20 20 20 20 20 20 20 20 70 46 69 6c {. pFil
ef150 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 e->lastErrno = e
ef160 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 20 20 rrno;.
ef170 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f return SQLITE_IO
ef180 45 52 52 5f 43 4c 4f 53 45 3b 0a 20 20 20 20 20 ERR_CLOSE;.
ef190 20 20 20 7d 0a 23 65 6c 73 65 0a 20 20 20 20 20 }.#else.
ef1a0 20 20 20 63 6c 6f 73 65 28 70 46 69 6c 65 2d 3e close(pFile->
ef1b0 68 29 3b 20 2f 2a 20 73 69 6c 65 6e 74 6c 79 20 h); /* silently
ef1c0 6c 65 61 6b 20 66 64 20 69 66 20 66 61 69 6c 20 leak fd if fail
ef1d0 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 */.#endif.
ef1e0 7d 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 68 }. pFile->h
ef1f0 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 69 6e 74 = -1;. int
ef200 20 66 64 20 3d 20 6f 70 65 6e 28 70 43 74 78 2d fd = open(pCtx-
ef210 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2d 3e >dbPath, pFile->
ef220 6f 70 65 6e 46 6c 61 67 73 2c 0a 20 20 20 20 20 openFlags,.
ef230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53 S
ef240 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 46 49 QLITE_DEFAULT_FI
ef250 4c 45 5f 50 45 52 4d 49 53 53 49 4f 4e 53 29 3b LE_PERMISSIONS);
ef260 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 32 28 . OSTRACE2(
ef270 22 54 52 41 4e 53 50 52 4f 58 59 3a 20 4f 50 45 "TRANSPROXY: OPE
ef280 4e 20 20 25 64 5c 6e 22 2c 20 66 64 29 3b 0a 20 N %d\n", fd);.
ef290 20 20 20 20 20 69 66 28 20 66 64 3e 3d 30 20 29 if( fd>=0 )
ef2a0 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d {. pFile-
ef2b0 3e 68 20 3d 20 66 64 3b 0a 20 20 20 20 20 20 7d >h = fd;. }
ef2c0 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72 63 else{. rc
ef2d0 3d 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e =SQLITE_CANTOPEN
ef2e0 3b 20 2f 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 ; /* SQLITE_BUSY
ef2f0 3f 20 70 72 6f 78 79 54 61 6b 65 43 6f 6e 63 68 ? proxyTakeConch
ef300 20 63 61 6c 6c 65 64 0a 20 20 20 20 20 20 20 20 called.
ef310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ef320 20 20 20 20 20 20 20 64 75 72 69 6e 67 20 6c 6f during lo
ef330 63 6b 69 6e 67 20 2a 2f 0a 20 20 20 20 20 20 7d cking */. }
ef340 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 . }. if( r
ef350 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c==SQLITE_OK &&
ef360 21 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 !pCtx->lockProxy
ef370 20 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a ){. char *
ef380 70 61 74 68 20 3d 20 74 4c 6f 63 6b 50 61 74 68 path = tLockPath
ef390 20 3f 20 74 4c 6f 63 6b 50 61 74 68 20 3a 20 70 ? tLockPath : p
ef3a0 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 Ctx->lockProxyPa
ef3b0 74 68 3b 0a 20 20 20 20 20 20 2f 2a 20 41 43 53 th;. /* ACS
ef3c0 3a 20 4e 65 65 64 20 74 6f 20 6d 61 6b 65 20 61 : Need to make a
ef3d0 20 63 6f 70 79 20 6f 66 20 70 61 74 68 20 73 6f copy of path so
ef3e0 6d 65 74 69 6d 65 73 20 2a 2f 0a 20 20 20 20 20 metimes */.
ef3f0 20 72 63 20 3d 20 70 72 6f 78 79 43 72 65 61 74 rc = proxyCreat
ef400 65 55 6e 69 78 46 69 6c 65 28 70 61 74 68 2c 20 eUnixFile(path,
ef410 26 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 &pCtx->lockProxy
ef420 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 );. }. if(
ef430 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
ef440 7b 0a 20 20 20 20 20 20 70 43 74 78 2d 3e 63 6f {. pCtx->co
ef450 6e 63 68 48 65 6c 64 20 3d 20 31 3b 0a 0a 20 20 nchHeld = 1;..
ef460 20 20 20 20 69 66 28 20 74 4c 6f 63 6b 50 61 74 if( tLockPat
ef470 68 20 29 7b 0a 20 20 20 20 20 20 20 20 70 43 74 h ){. pCt
ef480 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 x->lockProxyPath
ef490 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 = sqlite3DbStrD
ef4a0 75 70 28 30 2c 20 74 4c 6f 63 6b 50 61 74 68 29 up(0, tLockPath)
ef4b0 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 43 ;. if( pC
ef4c0 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 2d 3e 70 tx->lockProxy->p
ef4d0 4d 65 74 68 6f 64 20 3d 3d 20 26 61 66 70 49 6f Method == &afpIo
ef4e0 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 20 Methods ){.
ef4f0 20 20 20 20 20 28 28 61 66 70 4c 6f 63 6b 69 6e ((afpLockin
ef500 67 43 6f 6e 74 65 78 74 20 2a 29 70 43 74 78 2d gContext *)pCtx-
ef510 3e 6c 6f 63 6b 50 72 6f 78 79 2d 3e 6c 6f 63 6b >lockProxy->lock
ef520 69 6e 67 43 6f 6e 74 65 78 74 29 2d 3e 64 62 50 ingContext)->dbP
ef530 61 74 68 20 3d 0a 20 20 20 20 20 20 20 20 20 20 ath =.
ef540 20 20 20 20 20 20 20 20 20 20 20 70 43 74 78 2d pCtx-
ef550 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 3b 0a >lockProxyPath;.
ef560 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
ef570 7d 0a 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 }. } else {.
ef580 20 20 20 20 20 63 6f 6e 63 68 46 69 6c 65 2d 3e conchFile->
ef590 70 4d 65 74 68 6f 64 2d 3e 78 55 6e 6c 6f 63 6b pMethod->xUnlock
ef5a0 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 ((sqlite3_file*)
ef5b0 63 6f 6e 63 68 46 69 6c 65 2c 20 4e 4f 5f 4c 4f conchFile, NO_LO
ef5c0 43 4b 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 4f CK);. }. O
ef5d0 53 54 52 41 43 45 33 28 22 54 41 4b 45 43 4f 4e STRACE3("TAKECON
ef5e0 43 48 20 20 25 64 20 25 73 5c 6e 22 2c 20 63 6f CH %d %s\n", co
ef5f0 6e 63 68 46 69 6c 65 2d 3e 68 2c 20 72 63 3d 3d nchFile->h, rc==
ef600 53 51 4c 49 54 45 5f 4f 4b 3f 22 6f 6b 22 3a 22 SQLITE_OK?"ok":"
ef610 66 61 69 6c 65 64 22 29 3b 0a 20 20 20 20 72 65 failed");. re
ef620 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 7d 0a 0a turn rc;. }.}..
ef630 2f 2a 0a 2a 2a 20 49 66 20 70 46 69 6c 65 20 68 /*.** If pFile h
ef640 6f 6c 64 73 20 61 20 6c 6f 63 6b 20 6f 6e 20 61 olds a lock on a
ef650 20 63 6f 6e 63 68 20 66 69 6c 65 2c 20 74 68 65 conch file, the
ef660 6e 20 72 65 6c 65 61 73 65 20 74 68 61 74 20 6c n release that l
ef670 6f 63 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ock..*/.static i
ef680 6e 74 20 70 72 6f 78 79 52 65 6c 65 61 73 65 43 nt proxyReleaseC
ef690 6f 6e 63 68 28 75 6e 69 78 46 69 6c 65 20 2a 70 onch(unixFile *p
ef6a0 46 69 6c 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b File){. int rc;
ef6b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ef6c0 20 20 20 20 20 2f 2a 20 53 75 62 72 6f 75 74 69 /* Subrouti
ef6d0 6e 65 20 72 65 74 75 72 6e 20 63 6f 64 65 20 2a ne return code *
ef6e0 2f 0a 20 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 /. proxyLocking
ef6f0 43 6f 6e 74 65 78 74 20 2a 70 43 74 78 3b 20 20 Context *pCtx;
ef700 2f 2a 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20 63 /* The locking c
ef710 6f 6e 74 65 78 74 20 66 6f 72 20 74 68 65 20 70 ontext for the p
ef720 72 6f 78 79 20 6c 6f 63 6b 20 2a 2f 0a 20 20 75 roxy lock */. u
ef730 6e 69 78 46 69 6c 65 20 2a 63 6f 6e 63 68 46 69 nixFile *conchFi
ef740 6c 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 le; /* Na
ef750 6d 65 20 6f 66 20 74 68 65 20 63 6f 6e 63 68 20 me of the conch
ef760 66 69 6c 65 20 2a 2f 0a 0a 20 20 70 43 74 78 20 file */.. pCtx
ef770 3d 20 28 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 = (proxyLockingC
ef780 6f 6e 74 65 78 74 20 2a 29 70 46 69 6c 65 2d 3e ontext *)pFile->
ef790 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a lockingContext;.
ef7a0 20 20 63 6f 6e 63 68 46 69 6c 65 20 3d 20 70 43 conchFile = pC
ef7b0 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 3b 0a 20 tx->conchFile;.
ef7c0 20 4f 53 54 52 41 43 45 34 28 22 52 45 4c 45 41 OSTRACE4("RELEA
ef7d0 53 45 43 4f 4e 43 48 20 20 25 64 20 66 6f 72 20 SECONCH %d for
ef7e0 25 73 20 70 69 64 3d 25 64 5c 6e 22 2c 20 63 6f %s pid=%d\n", co
ef7f0 6e 63 68 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 nchFile->h,.
ef800 20 20 20 20 20 20 20 28 70 43 74 78 2d 3e 6c 6f (pCtx->lo
ef810 63 6b 50 72 6f 78 79 50 61 74 68 20 3f 20 70 43 ckProxyPath ? pC
ef820 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 tx->lockProxyPat
ef830 68 20 3a 20 22 3a 61 75 74 6f 3a 22 29 2c 20 0a h : ":auto:"), .
ef840 20 20 20 20 20 20 20 20 20 20 20 67 65 74 70 69 getpi
ef850 64 28 29 29 3b 0a 20 20 70 43 74 78 2d 3e 63 6f d());. pCtx->co
ef860 6e 63 68 48 65 6c 64 20 3d 20 30 3b 0a 20 20 72 nchHeld = 0;. r
ef870 63 20 3d 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 70 c = conchFile->p
ef880 4d 65 74 68 6f 64 2d 3e 78 55 6e 6c 6f 63 6b 28 Method->xUnlock(
ef890 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 63 (sqlite3_file*)c
ef8a0 6f 6e 63 68 46 69 6c 65 2c 20 4e 4f 5f 4c 4f 43 onchFile, NO_LOC
ef8b0 4b 29 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 22 K);. OSTRACE3("
ef8c0 52 45 4c 45 41 53 45 43 4f 4e 43 48 20 20 25 64 RELEASECONCH %d
ef8d0 20 25 73 5c 6e 22 2c 20 63 6f 6e 63 68 46 69 6c %s\n", conchFil
ef8e0 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 20 20 e->h,.
ef8f0 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 (rc==SQLITE_OK
ef900 3f 20 22 6f 6b 22 20 3a 20 22 66 61 69 6c 65 64 ? "ok" : "failed
ef910 22 29 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 "));. return rc
ef920 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e ;.}../*.** Given
ef930 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 64 the name of a d
ef940 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 63 6f atabase file, co
ef950 6d 70 75 74 65 20 74 68 65 20 6e 61 6d 65 20 6f mpute the name o
ef960 66 20 69 74 73 20 63 6f 6e 63 68 20 66 69 6c 65 f its conch file
ef970 2e 0a 2a 2a 20 53 74 6f 72 65 20 74 68 65 20 63 ..** Store the c
ef980 6f 6e 63 68 20 66 69 6c 65 6e 61 6d 65 20 69 6e onch filename in
ef990 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 memory obtained
ef9a0 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 from sqlite3_ma
ef9b0 6c 6c 6f 63 28 29 2e 0a 2a 2a 20 4d 61 6b 65 20 lloc()..** Make
ef9c0 2a 70 43 6f 6e 63 68 50 61 74 68 20 70 6f 69 6e *pConchPath poin
ef9d0 74 20 74 6f 20 74 68 65 20 6e 65 77 20 6e 61 6d t to the new nam
ef9e0 65 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54 e. Return SQLIT
ef9f0 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 0a E_OK on success.
efa00 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f 4e 4f 4d ** or SQLITE_NOM
efa10 45 4d 20 69 66 20 75 6e 61 62 6c 65 20 74 6f 20 EM if unable to
efa20 6f 62 74 61 69 6e 20 6d 65 6d 6f 72 79 2e 0a 2a obtain memory..*
efa30 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 *.** The caller
efa40 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 is responsible f
efa50 6f 72 20 65 6e 73 75 72 69 6e 67 20 74 68 61 74 or ensuring that
efa60 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 6d the allocated m
efa70 65 6d 6f 72 79 0a 2a 2a 20 73 70 61 63 65 20 69 emory.** space i
efa80 73 20 65 76 65 6e 74 75 61 6c 6c 79 20 66 72 65 s eventually fre
efa90 65 64 2e 0a 2a 2a 0a 2a 2a 20 2a 70 43 6f 6e 63 ed..**.** *pConc
efaa0 68 50 61 74 68 20 69 73 20 73 65 74 20 74 6f 20 hPath is set to
efab0 4e 55 4c 4c 20 69 66 20 61 20 6d 65 6d 6f 72 79 NULL if a memory
efac0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f allocation erro
efad0 72 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a 73 74 61 r occurs..*/.sta
efae0 74 69 63 20 69 6e 74 20 70 72 6f 78 79 43 72 65 tic int proxyCre
efaf0 61 74 65 43 6f 6e 63 68 50 61 74 68 6e 61 6d 65 ateConchPathname
efb00 28 63 68 61 72 20 2a 64 62 50 61 74 68 2c 20 63 (char *dbPath, c
efb10 68 61 72 20 2a 2a 70 43 6f 6e 63 68 50 61 74 68 har **pConchPath
efb20 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 ){. int i;
efb30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
efb40 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 /* Loop count
efb50 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6c 65 6e 20 er */. int len
efb60 3d 20 28 69 6e 74 29 73 74 72 6c 65 6e 28 64 62 = (int)strlen(db
efb70 50 61 74 68 29 3b 20 2f 2a 20 4c 65 6e 67 74 68 Path); /* Length
efb80 20 6f 66 20 64 61 74 61 62 61 73 65 20 66 69 6c of database fil
efb90 65 6e 61 6d 65 20 2d 20 64 62 50 61 74 68 20 2a ename - dbPath *
efba0 2f 0a 20 20 63 68 61 72 20 2a 63 6f 6e 63 68 50 /. char *conchP
efbb0 61 74 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 ath;
efbc0 20 20 2f 2a 20 62 75 66 66 65 72 20 69 6e 20 77 /* buffer in w
efbd0 68 69 63 68 20 74 6f 20 63 6f 6e 73 74 72 75 63 hich to construc
efbe0 74 20 63 6f 6e 63 68 20 6e 61 6d 65 20 2a 2f 0a t conch name */.
efbf0 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 73 . /* Allocate s
efc00 70 61 63 65 20 66 6f 72 20 74 68 65 20 63 6f 6e pace for the con
efc10 63 68 20 66 69 6c 65 6e 61 6d 65 20 61 6e 64 20 ch filename and
efc20 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6e initialize the n
efc30 61 6d 65 20 74 6f 0a 20 20 2a 2a 20 74 68 65 20 ame to. ** the
efc40 6e 61 6d 65 20 6f 66 20 74 68 65 20 6f 72 69 67 name of the orig
efc50 69 6e 61 6c 20 64 61 74 61 62 61 73 65 20 66 69 inal database fi
efc60 6c 65 2e 20 2a 2f 20 20 0a 20 20 2a 70 43 6f 6e le. */ . *pCon
efc70 63 68 50 61 74 68 20 3d 20 63 6f 6e 63 68 50 61 chPath = conchPa
efc80 74 68 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c th = (char *)sql
efc90 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6c 65 6e 20 ite3_malloc(len
efca0 2b 20 38 29 3b 0a 20 20 69 66 28 20 63 6f 6e 63 + 8);. if( conc
efcb0 68 50 61 74 68 3d 3d 30 20 29 7b 0a 20 20 20 20 hPath==0 ){.
efcc0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f return SQLITE_NO
efcd0 4d 45 4d 3b 0a 20 20 7d 0a 20 20 6d 65 6d 63 70 MEM;. }. memcp
efce0 79 28 63 6f 6e 63 68 50 61 74 68 2c 20 64 62 50 y(conchPath, dbP
efcf0 61 74 68 2c 20 6c 65 6e 2b 31 29 3b 0a 20 20 0a ath, len+1);. .
efd00 20 20 2f 2a 20 6e 6f 77 20 69 6e 73 65 72 74 20 /* now insert
efd10 61 20 22 2e 22 20 62 65 66 6f 72 65 20 74 68 65 a "." before the
efd20 20 6c 61 73 74 20 2f 20 63 68 61 72 61 63 74 65 last / characte
efd30 72 20 2a 2f 0a 20 20 66 6f 72 28 20 69 3d 28 6c r */. for( i=(l
efd40 65 6e 2d 31 29 3b 20 69 3e 3d 30 3b 20 69 2d 2d en-1); i>=0; i--
efd50 20 29 7b 0a 20 20 20 20 69 66 28 20 63 6f 6e 63 ){. if( conc
efd60 68 50 61 74 68 5b 69 5d 3d 3d 27 2f 27 20 29 7b hPath[i]=='/' ){
efd70 0a 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 . i++;.
efd80 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
efd90 20 7d 0a 20 20 63 6f 6e 63 68 50 61 74 68 5b 69 }. conchPath[i
efda0 5d 3d 27 2e 27 3b 0a 20 20 77 68 69 6c 65 20 28 ]='.';. while (
efdb0 20 69 3c 6c 65 6e 20 29 7b 0a 20 20 20 20 63 6f i<len ){. co
efdc0 6e 63 68 50 61 74 68 5b 69 2b 31 5d 3d 64 62 50 nchPath[i+1]=dbP
efdd0 61 74 68 5b 69 5d 3b 0a 20 20 20 20 69 2b 2b 3b ath[i];. i++;
efde0 0a 20 20 7d 0a 0a 20 20 2f 2a 20 61 70 70 65 6e . }.. /* appen
efdf0 64 20 74 68 65 20 22 2d 63 6f 6e 63 68 22 20 73 d the "-conch" s
efe00 75 66 66 69 78 20 74 6f 20 74 68 65 20 66 69 6c uffix to the fil
efe10 65 20 2a 2f 0a 20 20 6d 65 6d 63 70 79 28 26 63 e */. memcpy(&c
efe20 6f 6e 63 68 50 61 74 68 5b 69 2b 31 5d 2c 20 22 onchPath[i+1], "
efe30 2d 63 6f 6e 63 68 22 2c 20 37 29 3b 0a 20 20 61 -conch", 7);. a
efe40 73 73 65 72 74 28 20 28 69 6e 74 29 73 74 72 6c ssert( (int)strl
efe50 65 6e 28 63 6f 6e 63 68 50 61 74 68 29 20 3d 3d en(conchPath) ==
efe60 20 6c 65 6e 2b 37 20 29 3b 0a 0a 20 20 72 65 74 len+7 );.. ret
efe70 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
efe80 0a 0a 0a 2f 2a 20 54 61 6b 65 73 20 61 20 66 75 .../* Takes a fu
efe90 6c 6c 79 20 63 6f 6e 66 69 67 75 72 65 64 20 70 lly configured p
efea0 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 2d 73 74 79 roxy locking-sty
efeb0 6c 65 20 75 6e 69 78 20 66 69 6c 65 20 61 6e 64 le unix file and
efec0 20 73 77 69 74 63 68 65 73 0a 2a 2a 20 74 68 65 switches.** the
efed0 20 6c 6f 63 61 6c 20 6c 6f 63 6b 20 66 69 6c 65 local lock file
efee0 20 70 61 74 68 20 0a 2a 2f 0a 73 74 61 74 69 63 path .*/.static
efef0 20 69 6e 74 20 73 77 69 74 63 68 4c 6f 63 6b 50 int switchLockP
eff00 72 6f 78 79 50 61 74 68 28 75 6e 69 78 46 69 6c roxyPath(unixFil
eff10 65 20 2a 70 46 69 6c 65 2c 20 63 6f 6e 73 74 20 e *pFile, const
eff20 63 68 61 72 20 2a 70 61 74 68 29 20 7b 0a 20 20 char *path) {.
eff30 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 proxyLockingCont
eff40 65 78 74 20 2a 70 43 74 78 20 3d 20 28 70 72 6f ext *pCtx = (pro
eff50 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 xyLockingContext
eff60 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 *)pFile->locking
eff70 43 6f 6e 74 65 78 74 3b 0a 20 20 63 68 61 72 20 Context;. char
eff80 2a 6f 6c 64 50 61 74 68 20 3d 20 70 43 74 78 2d *oldPath = pCtx-
eff90 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 3b 0a >lockProxyPath;.
effa0 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
effb0 45 5f 4f 4b 3b 0a 0a 20 20 69 66 28 20 70 46 69 E_OK;.. if( pFi
effc0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 21 3d 4e 4f le->locktype!=NO
effd0 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 74 _LOCK ){. ret
effe0 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 3b urn SQLITE_BUSY;
efff0 0a 20 20 7d 20 20 0a 0a 20 20 2f 2a 20 6e 6f 74 . } .. /* not
f0000 68 69 6e 67 20 74 6f 20 64 6f 20 69 66 20 74 68 hing to do if th
f0010 65 20 70 61 74 68 20 69 73 20 4e 55 4c 4c 2c 20 e path is NULL,
f0020 3a 61 75 74 6f 3a 20 6f 72 20 6d 61 74 63 68 65 :auto: or matche
f0030 73 20 74 68 65 20 65 78 69 73 74 69 6e 67 20 70 s the existing p
f0040 61 74 68 20 2a 2f 0a 20 20 69 66 28 20 21 70 61 ath */. if( !pa
f0050 74 68 20 7c 7c 20 70 61 74 68 5b 30 5d 3d 3d 27 th || path[0]=='
f0060 5c 30 27 20 7c 7c 20 21 73 74 72 63 6d 70 28 70 \0' || !strcmp(p
f0070 61 74 68 2c 20 22 3a 61 75 74 6f 3a 22 29 20 7c ath, ":auto:") |
f0080 7c 0a 20 20 20 20 28 6f 6c 64 50 61 74 68 20 26 |. (oldPath &
f0090 26 20 21 73 74 72 6e 63 6d 70 28 6f 6c 64 50 61 & !strncmp(oldPa
f00a0 74 68 2c 20 70 61 74 68 2c 20 4d 41 58 50 41 54 th, path, MAXPAT
f00b0 48 4c 45 4e 29 29 20 29 7b 0a 20 20 20 20 72 65 HLEN)) ){. re
f00c0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
f00d0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 6e 69 }else{. uni
f00e0 78 46 69 6c 65 20 2a 6c 6f 63 6b 50 72 6f 78 79 xFile *lockProxy
f00f0 20 3d 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f = pCtx->lockPro
f0100 78 79 3b 0a 20 20 20 20 70 43 74 78 2d 3e 6c 6f xy;. pCtx->lo
f0110 63 6b 50 72 6f 78 79 3d 4e 55 4c 4c 3b 0a 20 20 ckProxy=NULL;.
f0120 20 20 70 43 74 78 2d 3e 63 6f 6e 63 68 48 65 6c pCtx->conchHel
f0130 64 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 6c d = 0;. if( l
f0140 6f 63 6b 50 72 6f 78 79 21 3d 4e 55 4c 4c 20 29 ockProxy!=NULL )
f0150 7b 0a 20 20 20 20 20 20 72 63 3d 6c 6f 63 6b 50 {. rc=lockP
f0160 72 6f 78 79 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 roxy->pMethod->x
f0170 43 6c 6f 73 65 28 28 73 71 6c 69 74 65 33 5f 66 Close((sqlite3_f
f0180 69 6c 65 20 2a 29 6c 6f 63 6b 50 72 6f 78 79 29 ile *)lockProxy)
f0190 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 ;. if( rc )
f01a0 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
f01b0 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 6c sqlite3_free(l
f01c0 6f 63 6b 50 72 6f 78 79 29 3b 0a 20 20 20 20 7d ockProxy);. }
f01d0 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 . sqlite3_fre
f01e0 65 28 6f 6c 64 50 61 74 68 29 3b 0a 20 20 20 20 e(oldPath);.
f01f0 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 pCtx->lockProxyP
f0200 61 74 68 20 3d 20 73 71 6c 69 74 65 33 44 62 53 ath = sqlite3DbS
f0210 74 72 44 75 70 28 30 2c 20 70 61 74 68 29 3b 0a trDup(0, path);.
f0220 20 20 7d 0a 20 20 0a 20 20 72 65 74 75 72 6e 20 }. . return
f0230 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 46 69 rc;.}../*.** pFi
f0240 6c 65 20 69 73 20 61 20 66 69 6c 65 20 74 68 61 le is a file tha
f0250 74 20 68 61 73 20 62 65 65 6e 20 6f 70 65 6e 65 t has been opene
f0260 64 20 62 79 20 61 20 70 72 69 6f 72 20 78 4f 70 d by a prior xOp
f0270 65 6e 20 63 61 6c 6c 2e 20 20 64 62 50 61 74 68 en call. dbPath
f0280 0a 2a 2a 20 69 73 20 61 20 73 74 72 69 6e 67 20 .** is a string
f0290 62 75 66 66 65 72 20 61 74 20 6c 65 61 73 74 20 buffer at least
f02a0 4d 41 58 50 41 54 48 4c 45 4e 2b 31 20 63 68 61 MAXPATHLEN+1 cha
f02b0 72 61 63 74 65 72 73 20 69 6e 20 73 69 7a 65 2e racters in size.
f02c0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
f02d0 69 6e 65 20 66 69 6e 64 20 74 68 65 20 66 69 6c ine find the fil
f02e0 65 6e 61 6d 65 20 61 73 73 6f 63 69 61 74 65 64 ename associated
f02f0 20 77 69 74 68 20 70 46 69 6c 65 20 61 6e 64 20 with pFile and
f0300 77 72 69 74 65 73 20 69 74 0a 2a 2a 20 69 6e 74 writes it.** int
f0310 20 64 62 50 61 74 68 2e 0a 2a 2f 0a 73 74 61 74 dbPath..*/.stat
f0320 69 63 20 69 6e 74 20 70 72 6f 78 79 47 65 74 44 ic int proxyGetD
f0330 62 50 61 74 68 46 6f 72 55 6e 69 78 46 69 6c 65 bPathForUnixFile
f0340 28 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 (unixFile *pFile
f0350 2c 20 63 68 61 72 20 2a 64 62 50 61 74 68 29 7b , char *dbPath){
f0360 0a 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 41 .#if defined(__A
f0370 50 50 4c 45 5f 5f 29 0a 20 20 69 66 28 20 70 46 PPLE__). if( pF
f0380 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 20 3d 3d 20 ile->pMethod ==
f0390 26 61 66 70 49 6f 4d 65 74 68 6f 64 73 20 29 7b &afpIoMethods ){
f03a0 0a 20 20 20 20 2f 2a 20 61 66 70 20 73 74 79 6c . /* afp styl
f03b0 65 20 6b 65 65 70 73 20 61 20 72 65 66 65 72 65 e keeps a refere
f03c0 6e 63 65 20 74 6f 20 74 68 65 20 64 62 20 70 61 nce to the db pa
f03d0 74 68 20 69 6e 20 74 68 65 20 66 69 6c 65 50 61 th in the filePa
f03e0 74 68 20 66 69 65 6c 64 20 0a 20 20 20 20 2a 2a th field . **
f03f0 20 6f 66 20 74 68 65 20 73 74 72 75 63 74 20 2a of the struct *
f0400 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 69 /. assert( (i
f0410 6e 74 29 73 74 72 6c 65 6e 28 28 63 68 61 72 2a nt)strlen((char*
f0420 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 )pFile->lockingC
f0430 6f 6e 74 65 78 74 29 3c 3d 4d 41 58 50 41 54 48 ontext)<=MAXPATH
f0440 4c 45 4e 20 29 3b 0a 20 20 20 20 73 74 72 63 70 LEN );. strcp
f0450 79 28 64 62 50 61 74 68 2c 20 28 28 61 66 70 4c y(dbPath, ((afpL
f0460 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29 ockingContext *)
f0470 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f pFile->lockingCo
f0480 6e 74 65 78 74 29 2d 3e 64 62 50 61 74 68 29 3b ntext)->dbPath);
f0490 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a . }else.#endif.
f04a0 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 70 4d 65 if( pFile->pMe
f04b0 74 68 6f 64 20 3d 3d 20 26 64 6f 74 6c 6f 63 6b thod == &dotlock
f04c0 49 6f 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 IoMethods ){.
f04d0 20 2f 2a 20 64 6f 74 20 6c 6f 63 6b 20 73 74 79 /* dot lock sty
f04e0 6c 65 20 75 73 65 73 20 74 68 65 20 6c 6f 63 6b le uses the lock
f04f0 69 6e 67 20 63 6f 6e 74 65 78 74 20 74 6f 20 73 ing context to s
f0500 74 6f 72 65 20 74 68 65 20 64 6f 74 20 6c 6f 63 tore the dot loc
f0510 6b 0a 20 20 20 20 2a 2a 20 66 69 6c 65 20 70 61 k. ** file pa
f0520 74 68 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6c 65 th */. int le
f0530 6e 20 3d 20 73 74 72 6c 65 6e 28 28 63 68 61 72 n = strlen((char
f0540 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e *)pFile->lockin
f0550 67 43 6f 6e 74 65 78 74 29 20 2d 20 73 74 72 6c gContext) - strl
f0560 65 6e 28 44 4f 54 4c 4f 43 4b 5f 53 55 46 46 49 en(DOTLOCK_SUFFI
f0570 58 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 64 X);. memcpy(d
f0580 62 50 61 74 68 2c 20 28 63 68 61 72 20 2a 29 70 bPath, (char *)p
f0590 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e File->lockingCon
f05a0 74 65 78 74 2c 20 6c 65 6e 20 2b 20 31 29 3b 0a text, len + 1);.
f05b0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 }else{. /*
f05c0 61 6c 6c 20 6f 74 68 65 72 20 73 74 79 6c 65 73 all other styles
f05d0 20 75 73 65 20 74 68 65 20 6c 6f 63 6b 69 6e 67 use the locking
f05e0 20 63 6f 6e 74 65 78 74 20 74 6f 20 73 74 6f 72 context to stor
f05f0 65 20 74 68 65 20 64 62 20 66 69 6c 65 20 70 61 e the db file pa
f0600 74 68 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 th */. assert
f0610 28 20 73 74 72 6c 65 6e 28 28 63 68 61 72 2a 29 ( strlen((char*)
f0620 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f pFile->lockingCo
f0630 6e 74 65 78 74 29 3c 3d 4d 41 58 50 41 54 48 4c ntext)<=MAXPATHL
f0640 45 4e 20 29 3b 0a 20 20 20 20 73 74 72 63 70 79 EN );. strcpy
f0650 28 64 62 50 61 74 68 2c 20 28 63 68 61 72 20 2a (dbPath, (char *
f0660 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 )pFile->lockingC
f0670 6f 6e 74 65 78 74 29 3b 0a 20 20 7d 0a 20 20 72 ontext);. }. r
f0680 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
f0690 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 61 6b 65 73 20 .}../*.** Takes
f06a0 61 6e 20 61 6c 72 65 61 64 79 20 66 69 6c 6c 65 an already fille
f06b0 64 20 69 6e 20 75 6e 69 78 20 66 69 6c 65 20 61 d in unix file a
f06c0 6e 64 20 61 6c 74 65 72 73 20 69 74 20 73 6f 20 nd alters it so
f06d0 61 6c 6c 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 all file locking
f06e0 20 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 70 65 72 .** will be per
f06f0 66 6f 72 6d 65 64 20 6f 6e 20 74 68 65 20 6c 6f formed on the lo
f0700 63 61 6c 20 70 72 6f 78 79 20 6c 6f 63 6b 20 66 cal proxy lock f
f0710 69 6c 65 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 ile. The follow
f0720 69 6e 67 20 66 69 65 6c 64 73 0a 2a 2a 20 61 72 ing fields.** ar
f0730 65 20 70 72 65 73 65 72 76 65 64 20 69 6e 20 74 e preserved in t
f0740 68 65 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 he locking conte
f0750 78 74 20 73 6f 20 74 68 61 74 20 74 68 65 79 20 xt so that they
f0760 63 61 6e 20 62 65 20 72 65 73 74 6f 72 65 64 20 can be restored
f0770 61 6e 64 20 0a 2a 2a 20 74 68 65 20 75 6e 69 78 and .** the unix
f0780 20 73 74 72 75 63 74 75 72 65 20 70 72 6f 70 65 structure prope
f0790 72 6c 79 20 63 6c 65 61 6e 65 64 20 75 70 20 61 rly cleaned up a
f07a0 74 20 63 6c 6f 73 65 20 74 69 6d 65 3a 0a 2a 2a t close time:.**
f07b0 20 20 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 ->lockingConte
f07c0 78 74 0a 2a 2a 20 20 2d 3e 70 4d 65 74 68 6f 64 xt.** ->pMethod
f07d0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 .*/.static int p
f07e0 72 6f 78 79 54 72 61 6e 73 66 6f 72 6d 55 6e 69 roxyTransformUni
f07f0 78 46 69 6c 65 28 75 6e 69 78 46 69 6c 65 20 2a xFile(unixFile *
f0800 70 46 69 6c 65 2c 20 63 6f 6e 73 74 20 63 68 61 pFile, const cha
f0810 72 20 2a 70 61 74 68 29 20 7b 0a 20 20 70 72 6f r *path) {. pro
f0820 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 xyLockingContext
f0830 20 2a 70 43 74 78 3b 0a 20 20 63 68 61 72 20 64 *pCtx;. char d
f0840 62 50 61 74 68 5b 4d 41 58 50 41 54 48 4c 45 4e bPath[MAXPATHLEN
f0850 2b 31 5d 3b 20 20 20 20 20 20 20 2f 2a 20 4e 61 +1]; /* Na
f0860 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 me of the databa
f0870 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 63 68 61 se file */. cha
f0880 72 20 2a 6c 6f 63 6b 50 61 74 68 3d 4e 55 4c 4c r *lockPath=NULL
f0890 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c ;. int rc = SQL
f08a0 49 54 45 5f 4f 4b 3b 0a 20 20 0a 20 20 69 66 28 ITE_OK;. . if(
f08b0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
f08c0 21 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 !=NO_LOCK ){.
f08d0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 return SQLITE_B
f08e0 55 53 59 3b 0a 20 20 7d 0a 20 20 70 72 6f 78 79 USY;. }. proxy
f08f0 47 65 74 44 62 50 61 74 68 46 6f 72 55 6e 69 78 GetDbPathForUnix
f0900 46 69 6c 65 28 70 46 69 6c 65 2c 20 64 62 50 61 File(pFile, dbPa
f0910 74 68 29 3b 0a 20 20 69 66 28 20 21 70 61 74 68 th);. if( !path
f0920 20 7c 7c 20 70 61 74 68 5b 30 5d 3d 3d 27 5c 30 || path[0]=='\0
f0930 27 20 7c 7c 20 21 73 74 72 63 6d 70 28 70 61 74 ' || !strcmp(pat
f0940 68 2c 20 22 3a 61 75 74 6f 3a 22 29 20 29 7b 0a h, ":auto:") ){.
f0950 20 20 20 20 6c 6f 63 6b 50 61 74 68 3d 4e 55 4c lockPath=NUL
f0960 4c 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 L;. }else{.
f0970 6c 6f 63 6b 50 61 74 68 3d 28 63 68 61 72 20 2a lockPath=(char *
f0980 29 70 61 74 68 3b 0a 20 20 7d 0a 20 20 0a 20 20 )path;. }. .
f0990 4f 53 54 52 41 43 45 34 28 22 54 52 41 4e 53 50 OSTRACE4("TRANSP
f09a0 52 4f 58 59 20 20 25 64 20 66 6f 72 20 25 73 20 ROXY %d for %s
f09b0 70 69 64 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 pid=%d\n", pFile
f09c0 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20 ->h,.
f09d0 28 6c 6f 63 6b 50 61 74 68 20 3f 20 6c 6f 63 6b (lockPath ? lock
f09e0 50 61 74 68 20 3a 20 22 3a 61 75 74 6f 3a 22 29 Path : ":auto:")
f09f0 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 0a 20 20 , getpid());..
f0a00 70 43 74 78 20 3d 20 73 71 6c 69 74 65 33 5f 6d pCtx = sqlite3_m
f0a10 61 6c 6c 6f 63 28 20 73 69 7a 65 6f 66 28 2a 70 alloc( sizeof(*p
f0a20 43 74 78 29 20 29 3b 0a 20 20 69 66 28 20 70 43 Ctx) );. if( pC
f0a30 74 78 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 tx==0 ){. ret
f0a40 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d urn SQLITE_NOMEM
f0a50 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28 70 ;. }. memset(p
f0a60 43 74 78 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a Ctx, 0, sizeof(*
f0a70 70 43 74 78 29 29 3b 0a 0a 20 20 72 63 20 3d 20 pCtx));.. rc =
f0a80 70 72 6f 78 79 43 72 65 61 74 65 43 6f 6e 63 68 proxyCreateConch
f0a90 50 61 74 68 6e 61 6d 65 28 64 62 50 61 74 68 2c Pathname(dbPath,
f0aa0 20 26 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c &pCtx->conchFil
f0ab0 65 50 61 74 68 29 3b 0a 20 20 69 66 28 20 72 63 ePath);. if( rc
f0ac0 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
f0ad0 20 20 20 72 63 20 3d 20 70 72 6f 78 79 43 72 65 rc = proxyCre
f0ae0 61 74 65 55 6e 69 78 46 69 6c 65 28 70 43 74 78 ateUnixFile(pCtx
f0af0 2d 3e 63 6f 6e 63 68 46 69 6c 65 50 61 74 68 2c ->conchFilePath,
f0b00 20 26 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c &pCtx->conchFil
f0b10 65 29 3b 0a 20 20 7d 20 20 0a 20 20 69 66 28 20 e);. } . if(
f0b20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
f0b30 20 6c 6f 63 6b 50 61 74 68 20 29 7b 0a 20 20 20 lockPath ){.
f0b40 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 pCtx->lockProxy
f0b50 50 61 74 68 20 3d 20 73 71 6c 69 74 65 33 44 62 Path = sqlite3Db
f0b60 53 74 72 44 75 70 28 30 2c 20 6c 6f 63 6b 50 61 StrDup(0, lockPa
f0b70 74 68 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 th);. }.. if(
f0b80 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
f0b90 0a 20 20 20 20 2f 2a 20 61 6c 6c 20 6d 65 6d 6f . /* all memo
f0ba0 72 79 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 2c ry is allocated,
f0bb0 20 70 72 6f 78 79 73 20 61 72 65 20 63 72 65 61 proxys are crea
f0bc0 74 65 64 20 61 6e 64 20 61 73 73 69 67 6e 65 64 ted and assigned
f0bd0 2c 20 0a 20 20 20 20 2a 2a 20 73 77 69 74 63 68 , . ** switch
f0be0 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e the locking con
f0bf0 74 65 78 74 20 61 6e 64 20 70 4d 65 74 68 6f 64 text and pMethod
f0c00 20 74 68 65 6e 20 72 65 74 75 72 6e 2e 0a 20 20 then return..
f0c10 20 20 2a 2f 0a 20 20 20 20 70 43 74 78 2d 3e 64 */. pCtx->d
f0c20 62 50 61 74 68 20 3d 20 73 71 6c 69 74 65 33 44 bPath = sqlite3D
f0c30 62 53 74 72 44 75 70 28 30 2c 20 64 62 50 61 74 bStrDup(0, dbPat
f0c40 68 29 3b 0a 20 20 20 20 70 43 74 78 2d 3e 6f 6c h);. pCtx->ol
f0c50 64 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 dLockingContext
f0c60 3d 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 = pFile->locking
f0c70 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 70 46 69 Context;. pFi
f0c80 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 le->lockingConte
f0c90 78 74 20 3d 20 70 43 74 78 3b 0a 20 20 20 20 70 xt = pCtx;. p
f0ca0 43 74 78 2d 3e 70 4f 6c 64 4d 65 74 68 6f 64 20 Ctx->pOldMethod
f0cb0 3d 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 = pFile->pMethod
f0cc0 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 70 4d 65 ;. pFile->pMe
f0cd0 74 68 6f 64 20 3d 20 26 70 72 6f 78 79 49 6f 4d thod = &proxyIoM
f0ce0 65 74 68 6f 64 73 3b 0a 20 20 7d 65 6c 73 65 7b ethods;. }else{
f0cf0 0a 20 20 20 20 69 66 28 20 70 43 74 78 2d 3e 63 . if( pCtx->c
f0d00 6f 6e 63 68 46 69 6c 65 20 29 7b 20 0a 20 20 20 onchFile ){ .
f0d10 20 20 20 72 63 20 3d 20 70 43 74 78 2d 3e 63 6f rc = pCtx->co
f0d20 6e 63 68 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 nchFile->pMethod
f0d30 2d 3e 78 43 6c 6f 73 65 28 28 73 71 6c 69 74 65 ->xClose((sqlite
f0d40 33 5f 66 69 6c 65 20 2a 29 70 43 74 78 2d 3e 63 3_file *)pCtx->c
f0d50 6f 6e 63 68 46 69 6c 65 29 3b 0a 20 20 20 20 20 onchFile);.
f0d60 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e if( rc ) return
f0d70 20 72 63 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 rc;. sqlit
f0d80 65 33 5f 66 72 65 65 28 70 43 74 78 2d 3e 63 6f e3_free(pCtx->co
f0d90 6e 63 68 46 69 6c 65 29 3b 0a 20 20 20 20 7d 0a nchFile);. }.
f0da0 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 sqlite3_free
f0db0 28 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 (pCtx->conchFile
f0dc0 50 61 74 68 29 3b 20 0a 20 20 20 20 73 71 6c 69 Path); . sqli
f0dd0 74 65 33 5f 66 72 65 65 28 70 43 74 78 29 3b 0a te3_free(pCtx);.
f0de0 20 20 7d 0a 20 20 4f 53 54 52 41 43 45 33 28 22 }. OSTRACE3("
f0df0 54 52 41 4e 53 50 52 4f 58 59 20 20 25 64 20 25 TRANSPROXY %d %
f0e00 73 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a s\n", pFile->h,.
f0e10 20 20 20 20 20 20 20 20 20 20 20 28 72 63 3d 3d (rc==
f0e20 53 51 4c 49 54 45 5f 4f 4b 20 3f 20 22 6f 6b 22 SQLITE_OK ? "ok"
f0e30 20 3a 20 22 66 61 69 6c 65 64 22 29 29 3b 0a 20 : "failed"));.
f0e40 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a return rc;.}...
f0e50 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 /*.** This routi
f0e60 6e 65 20 68 61 6e 64 6c 65 73 20 73 71 6c 69 74 ne handles sqlit
f0e70 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 e3_file_control(
f0e80 29 20 63 61 6c 6c 73 20 74 68 61 74 20 61 72 65 ) calls that are
f0e90 20 73 70 65 63 69 66 69 63 0a 2a 2a 20 74 6f 20 specific.** to
f0ea0 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 2e 0a 2a proxy locking..*
f0eb0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f /.static int pro
f0ec0 78 79 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 73 71 xyFileControl(sq
f0ed0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 lite3_file *id,
f0ee0 69 6e 74 20 6f 70 2c 20 76 6f 69 64 20 2a 70 41 int op, void *pA
f0ef0 72 67 29 7b 0a 20 20 73 77 69 74 63 68 28 20 6f rg){. switch( o
f0f00 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 p ){. case SQ
f0f10 4c 49 54 45 5f 47 45 54 5f 4c 4f 43 4b 50 52 4f LITE_GET_LOCKPRO
f0f20 58 59 46 49 4c 45 3a 20 7b 0a 20 20 20 20 20 20 XYFILE: {.
f0f30 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 unixFile *pFile
f0f40 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b = (unixFile*)id;
f0f50 0a 20 20 20 20 20 20 69 66 28 20 70 46 69 6c 65 . if( pFile
f0f60 2d 3e 70 4d 65 74 68 6f 64 20 3d 3d 20 26 70 72 ->pMethod == &pr
f0f70 6f 78 79 49 6f 4d 65 74 68 6f 64 73 20 29 7b 0a oxyIoMethods ){.
f0f80 20 20 20 20 20 20 20 20 70 72 6f 78 79 4c 6f 63 proxyLoc
f0f90 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43 74 kingContext *pCt
f0fa0 78 20 3d 20 28 70 72 6f 78 79 4c 6f 63 6b 69 6e x = (proxyLockin
f0fb0 67 43 6f 6e 74 65 78 74 2a 29 70 46 69 6c 65 2d gContext*)pFile-
f0fc0 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b >lockingContext;
f0fd0 0a 20 20 20 20 20 20 20 20 70 72 6f 78 79 54 61 . proxyTa
f0fe0 6b 65 43 6f 6e 63 68 28 70 46 69 6c 65 29 3b 0a keConch(pFile);.
f0ff0 20 20 20 20 20 20 20 20 69 66 28 20 70 43 74 78 if( pCtx
f1000 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 ->lockProxyPath
f1010 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 28 63 ){. *(c
f1020 6f 6e 73 74 20 63 68 61 72 20 2a 2a 29 70 41 72 onst char **)pAr
f1030 67 20 3d 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 g = pCtx->lockPr
f1040 6f 78 79 50 61 74 68 3b 0a 20 20 20 20 20 20 20 oxyPath;.
f1050 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
f1060 20 20 2a 28 63 6f 6e 73 74 20 63 68 61 72 20 2a *(const char *
f1070 2a 29 70 41 72 67 20 3d 20 22 3a 61 75 74 6f 3a *)pArg = ":auto:
f1080 20 28 6e 6f 74 20 68 65 6c 64 29 22 3b 0a 20 20 (not held)";.
f1090 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 20 }. }
f10a0 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 20 20 2a else {. *
f10b0 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 29 70 (const char **)p
f10c0 41 72 67 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 Arg = NULL;.
f10d0 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e }. return
f10e0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 SQLITE_OK;.
f10f0 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 }. case SQLIT
f1100 45 5f 53 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 E_SET_LOCKPROXYF
f1110 49 4c 45 3a 20 7b 0a 20 20 20 20 20 20 75 6e 69 ILE: {. uni
f1120 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 xFile *pFile = (
f1130 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 unixFile*)id;.
f1140 20 20 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c int rc = SQL
f1150 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 69 6e ITE_OK;. in
f1160 74 20 69 73 50 72 6f 78 79 53 74 79 6c 65 20 3d t isProxyStyle =
f1170 20 28 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 (pFile->pMethod
f1180 20 3d 3d 20 26 70 72 6f 78 79 49 6f 4d 65 74 68 == &proxyIoMeth
f1190 6f 64 73 29 3b 0a 20 20 20 20 20 20 69 66 28 20 ods);. if(
f11a0 70 41 72 67 3d 3d 4e 55 4c 4c 20 7c 7c 20 28 63 pArg==NULL || (c
f11b0 6f 6e 73 74 20 63 68 61 72 20 2a 29 70 41 72 67 onst char *)pArg
f11c0 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69 ==0 ){. i
f11d0 66 28 20 69 73 50 72 6f 78 79 53 74 79 6c 65 20 f( isProxyStyle
f11e0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ){. /*
f11f0 74 75 72 6e 20 6f 66 66 20 70 72 6f 78 79 20 6c turn off proxy l
f1200 6f 63 6b 69 6e 67 20 2d 20 6e 6f 74 20 73 75 70 ocking - not sup
f1210 70 6f 72 74 65 64 20 2a 2f 0a 20 20 20 20 20 20 ported */.
f1220 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
f1230 45 52 52 4f 52 20 2f 2a 53 51 4c 49 54 45 5f 50 ERROR /*SQLITE_P
f1240 52 4f 54 4f 43 4f 4c 3f 20 53 51 4c 49 54 45 5f ROTOCOL? SQLITE_
f1250 4d 49 53 55 53 45 3f 2a 2f 3b 0a 20 20 20 20 20 MISUSE?*/;.
f1260 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
f1270 20 20 20 20 2f 2a 20 74 75 72 6e 20 6f 66 66 20 /* turn off
f1280 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 2d 20 proxy locking -
f1290 61 6c 72 65 61 64 79 20 6f 66 66 20 2d 20 4e 4f already off - NO
f12a0 4f 50 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 OP */.
f12b0 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
f12c0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
f12d0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 63 }else{. c
f12e0 6f 6e 73 74 20 63 68 61 72 20 2a 70 72 6f 78 79 onst char *proxy
f12f0 50 61 74 68 20 3d 20 28 63 6f 6e 73 74 20 63 68 Path = (const ch
f1300 61 72 20 2a 29 70 41 72 67 3b 0a 20 20 20 20 20 ar *)pArg;.
f1310 20 20 20 69 66 28 20 69 73 50 72 6f 78 79 53 74 if( isProxySt
f1320 79 6c 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 yle ){.
f1330 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e proxyLockingCon
f1340 74 65 78 74 20 2a 70 43 74 78 20 3d 20 0a 20 20 text *pCtx = .
f1350 20 20 20 20 20 20 20 20 20 20 28 70 72 6f 78 79 (proxy
f1360 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 2a 29 LockingContext*)
f1370 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f pFile->lockingCo
f1380 6e 74 65 78 74 3b 0a 20 20 20 20 20 20 20 20 20 ntext;.
f1390 20 69 66 28 20 21 73 74 72 63 6d 70 28 70 41 72 if( !strcmp(pAr
f13a0 67 2c 20 22 3a 61 75 74 6f 3a 22 29 20 0a 20 20 g, ":auto:") .
f13b0 20 20 20 20 20 20 20 20 20 7c 7c 20 28 70 43 74 || (pCt
f13c0 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 x->lockProxyPath
f13d0 20 26 26 0a 20 20 20 20 20 20 20 20 20 20 20 20 &&.
f13e0 20 20 20 21 73 74 72 6e 63 6d 70 28 70 43 74 78 !strncmp(pCtx
f13f0 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 2c ->lockProxyPath,
f1400 20 70 72 6f 78 79 50 61 74 68 2c 20 4d 41 58 50 proxyPath, MAXP
f1410 41 54 48 4c 45 4e 29 29 0a 20 20 20 20 20 20 20 ATHLEN)).
f1420 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 ){.
f1430 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b rc = SQLITE_OK
f1440 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 ;. }els
f1450 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 e{. r
f1460 63 20 3d 20 73 77 69 74 63 68 4c 6f 63 6b 50 72 c = switchLockPr
f1470 6f 78 79 50 61 74 68 28 70 46 69 6c 65 2c 20 70 oxyPath(pFile, p
f1480 72 6f 78 79 50 61 74 68 29 3b 0a 20 20 20 20 20 roxyPath);.
f1490 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d }. }
f14a0 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 else{.
f14b0 2f 2a 20 74 75 72 6e 20 6f 6e 20 70 72 6f 78 79 /* turn on proxy
f14c0 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 2a 2f file locking */
f14d0 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 . rc =
f14e0 70 72 6f 78 79 54 72 61 6e 73 66 6f 72 6d 55 6e proxyTransformUn
f14f0 69 78 46 69 6c 65 28 70 46 69 6c 65 2c 20 70 72 ixFile(pFile, pr
f1500 6f 78 79 50 61 74 68 29 3b 0a 20 20 20 20 20 20 oxyPath);.
f1510 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
f1520 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 return rc;.
f1530 20 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 }. default:
f1540 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 {. assert(
f1550 30 20 29 3b 20 20 2f 2a 20 54 68 65 20 63 61 6c 0 ); /* The cal
f1560 6c 20 61 73 73 75 72 65 73 20 74 68 61 74 20 6f l assures that o
f1570 6e 6c 79 20 76 61 6c 69 64 20 6f 70 63 6f 64 65 nly valid opcode
f1580 73 20 61 72 65 20 73 65 6e 74 20 2a 2f 0a 20 20 s are sent */.
f1590 20 20 7d 0a 20 20 7d 0a 20 20 2f 2a 4e 4f 54 52 }. }. /*NOTR
f15a0 45 41 43 48 45 44 2a 2f 0a 20 20 72 65 74 75 72 EACHED*/. retur
f15b0 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a n SQLITE_ERROR;.
f15c0 7d 0a 0a 2f 2a 0a 2a 2a 20 57 69 74 68 69 6e 20 }../*.** Within
f15d0 74 68 69 73 20 64 69 76 69 73 69 6f 6e 20 28 74 this division (t
f15e0 68 65 20 70 72 6f 78 79 69 6e 67 20 6c 6f 63 6b he proxying lock
f15f0 69 6e 67 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 ing implementati
f1600 6f 6e 29 20 74 68 65 20 70 72 6f 63 65 64 75 72 on) the procedur
f1610 65 73 0a 2a 2a 20 61 62 6f 76 65 20 74 68 69 73 es.** above this
f1620 20 70 6f 69 6e 74 20 61 72 65 20 61 6c 6c 20 75 point are all u
f1630 74 69 6c 69 74 69 65 73 2e 20 20 54 68 65 20 6c tilities. The l
f1640 6f 63 6b 2d 72 65 6c 61 74 65 64 20 6d 65 74 68 ock-related meth
f1650 6f 64 73 20 6f 66 20 74 68 65 0a 2a 2a 20 70 72 ods of the.** pr
f1660 6f 78 79 2d 6c 6f 63 6b 69 6e 67 20 73 71 6c 69 oxy-locking sqli
f1670 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 20 6f 62 te3_io_method ob
f1680 6a 65 63 74 20 66 6f 6c 6c 6f 77 2e 0a 2a 2f 0a ject follow..*/.
f1690 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ../*.** This rou
f16a0 74 69 6e 65 20 63 68 65 63 6b 73 20 69 66 20 74 tine checks if t
f16b0 68 65 72 65 20 69 73 20 61 20 52 45 53 45 52 56 here is a RESERV
f16c0 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 ED lock held on
f16d0 74 68 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a the specified.**
f16e0 20 66 69 6c 65 20 62 79 20 74 68 69 73 20 6f 72 file by this or
f16f0 20 61 6e 79 20 6f 74 68 65 72 20 70 72 6f 63 65 any other proce
f1700 73 73 2e 20 49 66 20 73 75 63 68 20 61 20 6c 6f ss. If such a lo
f1710 63 6b 20 69 73 20 68 65 6c 64 2c 20 73 65 74 20 ck is held, set
f1720 2a 70 52 65 73 4f 75 74 0a 2a 2a 20 74 6f 20 61 *pResOut.** to a
f1730 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 20 non-zero value
f1740 6f 74 68 65 72 77 69 73 65 20 2a 70 52 65 73 4f otherwise *pResO
f1750 75 74 20 69 73 20 73 65 74 20 74 6f 20 7a 65 72 ut is set to zer
f1760 6f 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20 76 o. The return v
f1770 61 6c 75 65 0a 2a 2a 20 69 73 20 73 65 74 20 74 alue.** is set t
f1780 6f 20 53 51 4c 49 54 45 5f 4f 4b 20 75 6e 6c 65 o SQLITE_OK unle
f1790 73 73 20 61 6e 20 49 2f 4f 20 65 72 72 6f 72 20 ss an I/O error
f17a0 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 6c 6f occurs during lo
f17b0 63 6b 20 63 68 65 63 6b 69 6e 67 2e 0a 2a 2f 0a ck checking..*/.
f17c0 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 static int proxy
f17d0 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 CheckReservedLoc
f17e0 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a k(sqlite3_file *
f17f0 69 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 id, int *pResOut
f1800 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a ) {. unixFile *
f1810 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c pFile = (unixFil
f1820 65 2a 29 69 64 3b 0a 20 20 69 6e 74 20 72 63 20 e*)id;. int rc
f1830 3d 20 70 72 6f 78 79 54 61 6b 65 43 6f 6e 63 68 = proxyTakeConch
f1840 28 70 46 69 6c 65 29 3b 0a 20 20 69 66 28 20 72 (pFile);. if( r
f1850 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
f1860 20 20 20 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 proxyLocking
f1870 43 6f 6e 74 65 78 74 20 2a 70 43 74 78 20 3d 20 Context *pCtx =
f1880 28 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e (proxyLockingCon
f1890 74 65 78 74 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f text *)pFile->lo
f18a0 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 ckingContext;.
f18b0 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 72 6f 78 unixFile *prox
f18c0 79 20 3d 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 y = pCtx->lockPr
f18d0 6f 78 79 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 oxy;. return
f18e0 70 72 6f 78 79 2d 3e 70 4d 65 74 68 6f 64 2d 3e proxy->pMethod->
f18f0 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f xCheckReservedLo
f1900 63 6b 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ck((sqlite3_file
f1910 2a 29 70 72 6f 78 79 2c 20 70 52 65 73 4f 75 74 *)proxy, pResOut
f1920 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
f1930 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 rc;.}../*.** Loc
f1940 6b 20 74 68 65 20 66 69 6c 65 20 77 69 74 68 20 k the file with
f1950 74 68 65 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 the lock specifi
f1960 65 64 20 62 79 20 70 61 72 61 6d 65 74 65 72 20 ed by parameter
f1970 6c 6f 63 6b 74 79 70 65 20 2d 20 6f 6e 65 0a 2a locktype - one.*
f1980 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 * of the followi
f1990 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 ng:.**.** (1
f19a0 29 20 53 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a ) SHARED_LOCK.**
f19b0 20 20 20 20 20 28 32 29 20 52 45 53 45 52 56 45 (2) RESERVE
f19c0 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33 D_LOCK.** (3
f19d0 29 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a ) PENDING_LOCK.*
f19e0 2a 20 20 20 20 20 28 34 29 20 45 58 43 4c 55 53 * (4) EXCLUS
f19f0 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 IVE_LOCK.**.** S
f1a00 6f 6d 65 74 69 6d 65 73 20 77 68 65 6e 20 72 65 ometimes when re
f1a10 71 75 65 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 questing one loc
f1a20 6b 20 73 74 61 74 65 2c 20 61 64 64 69 74 69 6f k state, additio
f1a30 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a nal lock states.
f1a40 2a 2a 20 61 72 65 20 69 6e 73 65 72 74 65 64 20 ** are inserted
f1a50 69 6e 20 62 65 74 77 65 65 6e 2e 20 20 54 68 65 in between. The
f1a60 20 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 74 20 66 locking might f
f1a70 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 ail on one of th
f1a80 65 20 6c 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73 e later.** trans
f1a90 69 74 69 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74 itions leaving t
f1aa0 68 65 20 6c 6f 63 6b 20 73 74 61 74 65 20 64 69 he lock state di
f1ab0 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 77 68 61 fferent from wha
f1ac0 74 20 69 74 20 73 74 61 72 74 65 64 20 62 75 74 t it started but
f1ad0 0a 2a 2a 20 73 74 69 6c 6c 20 73 68 6f 72 74 20 .** still short
f1ae0 6f 66 20 69 74 73 20 67 6f 61 6c 2e 20 20 54 68 of its goal. Th
f1af0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72 e following char
f1b00 74 20 73 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f t shows the allo
f1b10 77 65 64 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f wed.** transitio
f1b20 6e 73 20 61 6e 64 20 74 68 65 20 69 6e 73 65 72 ns and the inser
f1b30 74 65 64 20 69 6e 74 65 72 6d 65 64 69 61 74 65 ted intermediate
f1b40 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 states:.**.**
f1b50 20 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 UNLOCKED -> SH
f1b60 41 52 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 ARED.** SHARE
f1b70 44 20 2d 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a D -> RESERVED.**
f1b80 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20 28 50 SHARED -> (P
f1b90 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 ENDING) -> EXCLU
f1ba0 53 49 56 45 0a 2a 2a 20 20 20 20 52 45 53 45 52 SIVE.** RESER
f1bb0 56 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 VED -> (PENDING)
f1bc0 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a -> EXCLUSIVE.**
f1bd0 20 20 20 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45 PENDING -> E
f1be0 58 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 XCLUSIVE.**.** T
f1bf0 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c his routine will
f1c00 20 6f 6e 6c 79 20 69 6e 63 72 65 61 73 65 20 61 only increase a
f1c10 20 6c 6f 63 6b 2e 20 20 55 73 65 20 74 68 65 20 lock. Use the
f1c20 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 sqlite3OsUnlock(
f1c30 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 74 6f 20 ).** routine to
f1c40 6c 6f 77 65 72 20 61 20 6c 6f 63 6b 69 6e 67 20 lower a locking
f1c50 6c 65 76 65 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 level..*/.static
f1c60 20 69 6e 74 20 70 72 6f 78 79 4c 6f 63 6b 28 73 int proxyLock(s
f1c70 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c qlite3_file *id,
f1c80 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b int locktype) {
f1c90 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 . unixFile *pFi
f1ca0 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 le = (unixFile*)
f1cb0 69 64 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 70 id;. int rc = p
f1cc0 72 6f 78 79 54 61 6b 65 43 6f 6e 63 68 28 70 46 roxyTakeConch(pF
f1cd0 69 6c 65 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d ile);. if( rc==
f1ce0 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
f1cf0 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e proxyLockingCon
f1d00 74 65 78 74 20 2a 70 43 74 78 20 3d 20 28 70 72 text *pCtx = (pr
f1d10 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 oxyLockingContex
f1d20 74 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 t *)pFile->locki
f1d30 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 75 ngContext;. u
f1d40 6e 69 78 46 69 6c 65 20 2a 70 72 6f 78 79 20 3d nixFile *proxy =
f1d50 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 pCtx->lockProxy
f1d60 3b 0a 20 20 20 20 72 63 20 3d 20 70 72 6f 78 79 ;. rc = proxy
f1d70 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 4c 6f 63 6b ->pMethod->xLock
f1d80 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 ((sqlite3_file*)
f1d90 70 72 6f 78 79 2c 20 6c 6f 63 6b 74 79 70 65 29 proxy, locktype)
f1da0 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 ;. pFile->loc
f1db0 6b 74 79 70 65 20 3d 20 70 72 6f 78 79 2d 3e 6c ktype = proxy->l
f1dc0 6f 63 6b 74 79 70 65 3b 0a 20 20 7d 0a 20 20 72 ocktype;. }. r
f1dd0 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a eturn rc;.}.../*
f1de0 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f .** Lower the lo
f1df0 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66 cking level on f
f1e00 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 70 ile descriptor p
f1e10 46 69 6c 65 20 74 6f 20 6c 6f 63 6b 74 79 70 65 File to locktype
f1e20 2e 20 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d . locktype.** m
f1e30 75 73 74 20 62 65 20 65 69 74 68 65 72 20 4e 4f ust be either NO
f1e40 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45 44 5f _LOCK or SHARED_
f1e50 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 LOCK..**.** If t
f1e60 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c he locking level
f1e70 20 6f 66 20 74 68 65 20 66 69 6c 65 20 64 65 73 of the file des
f1e80 63 72 69 70 74 6f 72 20 69 73 20 61 6c 72 65 61 criptor is alrea
f1e90 64 79 20 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a dy at or below.*
f1ea0 2a 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 * the requested
f1eb0 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74 locking level, t
f1ec0 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 his routine is a
f1ed0 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 no-op..*/.stati
f1ee0 63 20 69 6e 74 20 70 72 6f 78 79 55 6e 6c 6f 63 c int proxyUnloc
f1ef0 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a k(sqlite3_file *
f1f00 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 id, int locktype
f1f10 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a ) {. unixFile *
f1f20 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c pFile = (unixFil
f1f30 65 2a 29 69 64 3b 0a 20 20 69 6e 74 20 72 63 20 e*)id;. int rc
f1f40 3d 20 70 72 6f 78 79 54 61 6b 65 43 6f 6e 63 68 = proxyTakeConch
f1f50 28 70 46 69 6c 65 29 3b 0a 20 20 69 66 28 20 72 (pFile);. if( r
f1f60 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
f1f70 20 20 20 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 proxyLocking
f1f80 43 6f 6e 74 65 78 74 20 2a 70 43 74 78 20 3d 20 Context *pCtx =
f1f90 28 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e (proxyLockingCon
f1fa0 74 65 78 74 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f text *)pFile->lo
f1fb0 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 ckingContext;.
f1fc0 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 72 6f 78 unixFile *prox
f1fd0 79 20 3d 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 y = pCtx->lockPr
f1fe0 6f 78 79 3b 0a 20 20 20 20 72 63 20 3d 20 70 72 oxy;. rc = pr
f1ff0 6f 78 79 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 55 oxy->pMethod->xU
f2000 6e 6c 6f 63 6b 28 28 73 71 6c 69 74 65 33 5f 66 nlock((sqlite3_f
f2010 69 6c 65 2a 29 70 72 6f 78 79 2c 20 6c 6f 63 6b ile*)proxy, lock
f2020 74 79 70 65 29 3b 0a 20 20 20 20 70 46 69 6c 65 type);. pFile
f2030 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 70 72 6f ->locktype = pro
f2040 78 79 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 xy->locktype;.
f2050 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
f2060 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 ../*.** Close a
f2070 66 69 6c 65 20 74 68 61 74 20 75 73 65 73 20 70 file that uses p
f2080 72 6f 78 79 20 6c 6f 63 6b 73 2e 0a 2a 2f 0a 73 roxy locks..*/.s
f2090 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 43 tatic int proxyC
f20a0 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c lose(sqlite3_fil
f20b0 65 20 2a 69 64 29 20 7b 0a 20 20 69 66 28 20 69 e *id) {. if( i
f20c0 64 20 29 7b 0a 20 20 20 20 75 6e 69 78 46 69 6c d ){. unixFil
f20d0 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 e *pFile = (unix
f20e0 46 69 6c 65 2a 29 69 64 3b 0a 20 20 20 20 70 72 File*)id;. pr
f20f0 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 oxyLockingContex
f2100 74 20 2a 70 43 74 78 20 3d 20 28 70 72 6f 78 79 t *pCtx = (proxy
f2110 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a LockingContext *
f2120 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 )pFile->lockingC
f2130 6f 6e 74 65 78 74 3b 0a 20 20 20 20 75 6e 69 78 ontext;. unix
f2140 46 69 6c 65 20 2a 6c 6f 63 6b 50 72 6f 78 79 20 File *lockProxy
f2150 3d 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 = pCtx->lockProx
f2160 79 3b 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 20 y;. unixFile
f2170 2a 63 6f 6e 63 68 46 69 6c 65 20 3d 20 70 43 74 *conchFile = pCt
f2180 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 3b 0a 20 20 x->conchFile;.
f2190 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
f21a0 45 5f 4f 4b 3b 0a 20 20 20 20 0a 20 20 20 20 69 E_OK;. . i
f21b0 66 28 20 6c 6f 63 6b 50 72 6f 78 79 20 29 7b 0a f( lockProxy ){.
f21c0 20 20 20 20 20 20 72 63 20 3d 20 6c 6f 63 6b 50 rc = lockP
f21d0 72 6f 78 79 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 roxy->pMethod->x
f21e0 55 6e 6c 6f 63 6b 28 28 73 71 6c 69 74 65 33 5f Unlock((sqlite3_
f21f0 66 69 6c 65 2a 29 6c 6f 63 6b 50 72 6f 78 79 2c file*)lockProxy,
f2200 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 NO_LOCK);.
f2210 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e if( rc ) return
f2220 20 72 63 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 rc;. rc =
f2230 6c 6f 63 6b 50 72 6f 78 79 2d 3e 70 4d 65 74 68 lockProxy->pMeth
f2240 6f 64 2d 3e 78 43 6c 6f 73 65 28 28 73 71 6c 69 od->xClose((sqli
f2250 74 65 33 5f 66 69 6c 65 2a 29 6c 6f 63 6b 50 72 te3_file*)lockPr
f2260 6f 78 79 29 3b 0a 20 20 20 20 20 20 69 66 28 20 oxy);. if(
f2270 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a rc ) return rc;.
f2280 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 sqlite3_fr
f2290 65 65 28 6c 6f 63 6b 50 72 6f 78 79 29 3b 0a 20 ee(lockProxy);.
f22a0 20 20 20 20 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 pCtx->lockP
f22b0 72 6f 78 79 20 3d 20 30 3b 0a 20 20 20 20 7d 0a roxy = 0;. }.
f22c0 20 20 20 20 69 66 28 20 63 6f 6e 63 68 46 69 6c if( conchFil
f22d0 65 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 e ){. if( p
f22e0 43 74 78 2d 3e 63 6f 6e 63 68 48 65 6c 64 20 29 Ctx->conchHeld )
f22f0 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 {. rc = p
f2300 72 6f 78 79 52 65 6c 65 61 73 65 43 6f 6e 63 68 roxyReleaseConch
f2310 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 20 20 20 (pFile);.
f2320 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e if( rc ) return
f2330 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 rc;. }.
f2340 20 20 20 72 63 20 3d 20 63 6f 6e 63 68 46 69 6c rc = conchFil
f2350 65 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 43 6c 6f e->pMethod->xClo
f2360 73 65 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 se((sqlite3_file
f2370 2a 29 63 6f 6e 63 68 46 69 6c 65 29 3b 0a 20 20 *)conchFile);.
f2380 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 if( rc ) ret
f2390 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 73 71 urn rc;. sq
f23a0 6c 69 74 65 33 5f 66 72 65 65 28 63 6f 6e 63 68 lite3_free(conch
f23b0 46 69 6c 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 File);. }.
f23c0 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 sqlite3_free(pC
f23d0 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 tx->lockProxyPat
f23e0 68 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f h);. sqlite3_
f23f0 66 72 65 65 28 70 43 74 78 2d 3e 63 6f 6e 63 68 free(pCtx->conch
f2400 46 69 6c 65 50 61 74 68 29 3b 0a 20 20 20 20 73 FilePath);. s
f2410 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 74 78 qlite3_free(pCtx
f2420 2d 3e 64 62 50 61 74 68 29 3b 0a 20 20 20 20 2f ->dbPath);. /
f2430 2a 20 72 65 73 74 6f 72 65 20 74 68 65 20 6f 72 * restore the or
f2440 69 67 69 6e 61 6c 20 6c 6f 63 6b 69 6e 67 20 63 iginal locking c
f2450 6f 6e 74 65 78 74 20 61 6e 64 20 70 4d 65 74 68 ontext and pMeth
f2460 6f 64 20 74 68 65 6e 20 63 6c 6f 73 65 20 69 74 od then close it
f2470 20 2a 2f 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c */. pFile->l
f2480 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 3d 20 ockingContext =
f2490 70 43 74 78 2d 3e 6f 6c 64 4c 6f 63 6b 69 6e 67 pCtx->oldLocking
f24a0 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 70 46 69 Context;. pFi
f24b0 6c 65 2d 3e 70 4d 65 74 68 6f 64 20 3d 20 70 43 le->pMethod = pC
f24c0 74 78 2d 3e 70 4f 6c 64 4d 65 74 68 6f 64 3b 0a tx->pOldMethod;.
f24d0 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 sqlite3_free
f24e0 28 70 43 74 78 29 3b 0a 20 20 20 20 72 65 74 75 (pCtx);. retu
f24f0 72 6e 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f rn pFile->pMetho
f2500 64 2d 3e 78 43 6c 6f 73 65 28 69 64 29 3b 0a 20 d->xClose(id);.
f2510 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 }. return SQLI
f2520 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 0a 23 65 6e 64 TE_OK;.}....#end
f2530 69 66 20 2f 2a 20 64 65 66 69 6e 65 64 28 5f 5f if /* defined(__
f2540 41 50 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c 49 APPLE__) && SQLI
f2550 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e TE_ENABLE_LOCKIN
f2560 47 5f 53 54 59 4c 45 20 2a 2f 0a 2f 2a 0a 2a 2a G_STYLE */./*.**
f2570 20 54 68 65 20 70 72 6f 78 79 20 6c 6f 63 6b 69 The proxy locki
f2580 6e 67 20 73 74 79 6c 65 20 69 73 20 69 6e 74 65 ng style is inte
f2590 6e 64 65 64 20 66 6f 72 20 75 73 65 20 77 69 74 nded for use wit
f25a0 68 20 41 46 50 20 66 69 6c 65 73 79 73 74 65 6d h AFP filesystem
f25b0 73 2e 0a 2a 2a 20 41 6e 64 20 73 69 6e 63 65 20 s..** And since
f25c0 41 46 50 20 69 73 20 6f 6e 6c 79 20 73 75 70 70 AFP is only supp
f25d0 6f 72 74 65 64 20 6f 6e 20 4d 61 63 4f 53 58 2c orted on MacOSX,
f25e0 20 74 68 65 20 70 72 6f 78 79 20 6c 6f 63 6b 69 the proxy locki
f25f0 6e 67 20 69 73 20 61 6c 73 6f 0a 2a 2a 20 72 65 ng is also.** re
f2600 73 74 72 69 63 74 65 64 20 74 6f 20 4d 61 63 4f stricted to MacO
f2610 53 58 2e 0a 2a 2a 20 0a 2a 2a 0a 2a 2a 2a 2a 2a SX..** .**.*****
f2620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
f2630 6e 64 20 6f 66 20 74 68 65 20 70 72 6f 78 79 20 nd of the proxy
f2640 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 lock implementat
f2650 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ion ************
f2660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a **********.*****
f2670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f2680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f2690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f26a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f26b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a *********/../*.*
f26c0 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 * Initialize the
f26d0 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 operating syste
f26e0 6d 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a m interface..**.
f26f0 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
f2700 72 65 67 69 73 74 65 72 73 20 61 6c 6c 20 56 46 registers all VF
f2710 53 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e S implementation
f2720 73 20 66 6f 72 20 75 6e 69 78 2d 6c 69 6b 65 20 s for unix-like
f2730 6f 70 65 72 61 74 69 6e 67 0a 2a 2a 20 73 79 73 operating.** sys
f2740 74 65 6d 73 2e 20 20 54 68 69 73 20 72 6f 75 74 tems. This rout
f2750 69 6e 65 2c 20 61 6e 64 20 74 68 65 20 73 71 6c ine, and the sql
f2760 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29 20 72 6f ite3_os_end() ro
f2770 75 74 69 6e 65 20 74 68 61 74 20 66 6f 6c 6c 6f utine that follo
f2780 77 73 2c 0a 2a 2a 20 73 68 6f 75 6c 64 20 62 65 ws,.** should be
f2790 20 74 68 65 20 6f 6e 6c 79 20 72 6f 75 74 69 6e the only routin
f27a0 65 73 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 es in this file
f27b0 74 68 61 74 20 61 72 65 20 76 69 73 69 62 6c 65 that are visible
f27c0 20 66 72 6f 6d 20 6f 74 68 65 72 0a 2a 2a 20 66 from other.** f
f27d0 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 iles..**.** This
f27e0 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c routine is call
f27f0 65 64 20 6f 6e 63 65 20 64 75 72 69 6e 67 20 53 ed once during S
f2800 51 4c 69 74 65 20 69 6e 69 74 69 61 6c 69 7a 61 QLite initializa
f2810 74 69 6f 6e 20 61 6e 64 20 62 79 20 61 0a 2a 2a tion and by a.**
f2820 20 73 69 6e 67 6c 65 20 74 68 72 65 61 64 2e 20 single thread.
f2830 20 54 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f The memory allo
f2840 63 61 74 69 6f 6e 20 61 6e 64 20 6d 75 74 65 78 cation and mutex
f2850 20 73 75 62 73 79 73 74 65 6d 73 20 68 61 76 65 subsystems have
f2860 20 6e 6f 74 0a 2a 2a 20 6e 65 63 65 73 73 61 72 not.** necessar
f2870 69 6c 79 20 62 65 65 6e 20 69 6e 69 74 69 61 6c ily been initial
f2880 69 7a 65 64 20 77 68 65 6e 20 74 68 69 73 20 72 ized when this r
f2890 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 outine is called
f28a0 2c 20 61 6e 64 20 73 6f 20 74 68 65 79 0a 2a 2a , and so they.**
f28b0 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 75 should not be u
f28c0 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 sed..*/.SQLITE_A
f28d0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f PI int sqlite3_o
f28e0 73 5f 69 6e 69 74 28 76 6f 69 64 29 7b 20 0a 20 s_init(void){ .
f28f0 20 2f 2a 20 0a 20 20 2a 2a 20 54 68 65 20 66 6f /* . ** The fo
f2900 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 20 64 65 llowing macro de
f2910 66 69 6e 65 73 20 61 6e 20 69 6e 69 74 69 61 6c fines an initial
f2920 69 7a 65 72 20 66 6f 72 20 61 6e 20 73 71 6c 69 izer for an sqli
f2930 74 65 33 5f 76 66 73 20 6f 62 6a 65 63 74 2e 0a te3_vfs object..
f2940 20 20 2a 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 ** The name of
f2950 20 74 68 65 20 56 46 53 20 69 73 20 4e 41 4d 45 the VFS is NAME
f2960 2e 20 20 54 68 65 20 70 41 70 70 44 61 74 61 20 . The pAppData
f2970 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 is a pointer to
f2980 61 20 70 6f 69 6e 74 65 72 0a 20 20 2a 2a 20 74 a pointer. ** t
f2990 6f 20 74 68 65 20 22 66 69 6e 64 65 72 22 20 66 o the "finder" f
f29a0 75 6e 63 74 69 6f 6e 2e 20 20 28 70 41 70 70 44 unction. (pAppD
f29b0 61 74 61 20 69 73 20 61 20 70 6f 69 6e 74 65 72 ata is a pointer
f29c0 20 74 6f 20 61 20 70 6f 69 6e 74 65 72 20 62 65 to a pointer be
f29d0 63 61 75 73 65 0a 20 20 2a 2a 20 73 69 6c 6c 79 cause. ** silly
f29e0 20 43 39 30 20 72 75 6c 65 73 20 70 72 6f 68 69 C90 rules prohi
f29f0 62 69 74 20 61 20 76 6f 69 64 2a 20 66 72 6f 6d bit a void* from
f2a00 20 62 65 69 6e 67 20 63 61 73 74 20 74 6f 20 61 being cast to a
f2a10 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e 74 65 function pointe
f2a20 72 0a 20 20 2a 2a 20 61 6e 64 20 73 6f 20 77 65 r. ** and so we
f2a30 20 68 61 76 65 20 74 6f 20 67 6f 20 74 68 72 6f have to go thro
f2a40 75 67 68 20 74 68 65 20 69 6e 74 65 72 6d 65 64 ugh the intermed
f2a50 69 61 74 65 20 70 6f 69 6e 74 65 72 20 74 6f 20 iate pointer to
f2a60 61 76 6f 69 64 20 70 72 6f 62 6c 65 6d 73 0a 20 avoid problems.
f2a70 20 2a 2a 20 77 68 65 6e 20 63 6f 6d 70 69 6c 69 ** when compili
f2a80 6e 67 20 77 69 74 68 20 2d 70 65 64 61 6e 74 69 ng with -pedanti
f2a90 63 2d 65 72 72 6f 72 73 20 6f 6e 20 47 43 43 2e c-errors on GCC.
f2aa0 29 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 ). **. ** The
f2ab0 46 49 4e 44 45 52 20 70 61 72 61 6d 65 74 65 72 FINDER parameter
f2ac0 20 74 6f 20 74 68 69 73 20 6d 61 63 72 6f 20 69 to this macro i
f2ad0 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 s the name of th
f2ae0 65 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 e pointer to the
f2af0 0a 20 20 2a 2a 20 66 69 6e 64 65 72 2d 66 75 6e . ** finder-fun
f2b00 63 74 69 6f 6e 2e 20 20 54 68 65 20 66 69 6e 64 ction. The find
f2b10 65 72 2d 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 er-function retu
f2b20 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f rns a pointer to
f2b30 20 74 68 65 0a 20 20 2a 2a 20 73 71 6c 69 74 65 the. ** sqlite
f2b40 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 _io_methods obje
f2b50 63 74 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e ct that implemen
f2b60 74 73 20 74 68 65 20 64 65 73 69 72 65 64 20 6c ts the desired l
f2b70 6f 63 6b 69 6e 67 0a 20 20 2a 2a 20 62 65 68 61 ocking. ** beha
f2b80 76 69 6f 72 73 2e 20 20 53 65 65 20 74 68 65 20 viors. See the
f2b90 64 69 76 69 73 69 6f 6e 20 61 62 6f 76 65 20 74 division above t
f2ba0 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 hat contains the
f2bb0 20 49 4f 4d 45 54 48 4f 44 53 0a 20 20 2a 2a 20 IOMETHODS. **
f2bc0 6d 61 63 72 6f 20 66 6f 72 20 61 64 64 69 74 69 macro for additi
f2bd0 6f 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f on information o
f2be0 6e 20 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f n finder-functio
f2bf0 6e 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4d 6f ns.. **. ** Mo
f2c00 73 74 20 66 69 6e 64 65 72 73 20 73 69 6d 70 6c st finders simpl
f2c10 79 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 y return a point
f2c20 65 72 20 74 6f 20 61 20 66 69 78 65 64 20 73 71 er to a fixed sq
f2c30 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 lite3_io_methods
f2c40 0a 20 20 2a 2a 20 6f 62 6a 65 63 74 2e 20 20 42 . ** object. B
f2c50 75 74 20 74 68 65 20 22 61 75 74 6f 6c 6f 63 6b ut the "autolock
f2c60 49 6f 46 69 6e 64 65 72 22 20 61 76 61 69 6c 61 IoFinder" availa
f2c70 62 6c 65 20 6f 6e 20 4d 61 63 4f 53 58 20 64 6f ble on MacOSX do
f2c80 65 73 20 61 20 6c 69 74 74 6c 65 0a 20 20 2a 2a es a little. **
f2c90 20 6d 6f 72 65 20 74 68 61 6e 20 74 68 61 74 3b more than that;
f2ca0 20 69 74 20 6c 6f 6f 6b 73 20 61 74 20 74 68 65 it looks at the
f2cb0 20 66 69 6c 65 73 79 73 74 65 6d 20 74 79 70 65 filesystem type
f2cc0 20 74 68 61 74 20 68 6f 73 74 73 20 74 68 65 20 that hosts the
f2cd0 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 66 . ** database f
f2ce0 69 6c 65 20 61 6e 64 20 74 72 69 65 73 20 74 6f ile and tries to
f2cf0 20 63 68 6f 6f 73 65 20 61 6e 20 6c 6f 63 6b 69 choose an locki
f2d00 6e 67 20 6d 65 74 68 6f 64 20 61 70 70 72 6f 70 ng method approp
f2d10 72 69 61 74 65 20 66 6f 72 0a 20 20 2a 2a 20 74 riate for. ** t
f2d20 68 61 74 20 66 69 6c 65 73 79 73 74 65 6d 20 74 hat filesystem t
f2d30 69 6d 65 2e 0a 20 20 2a 2f 0a 20 20 23 64 65 66 ime.. */. #def
f2d40 69 6e 65 20 55 4e 49 58 56 46 53 28 56 46 53 4e ine UNIXVFS(VFSN
f2d50 41 4d 45 2c 20 46 49 4e 44 45 52 29 20 7b 20 20 AME, FINDER) {
f2d60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f2d70 20 20 20 20 20 20 5c 0a 20 20 20 20 31 2c 20 20 \. 1,
f2d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f2d90 20 20 2f 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f /* iVersion */
f2da0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f2db0 20 20 20 20 5c 0a 20 20 20 20 73 69 7a 65 6f 66 \. sizeof
f2dc0 28 75 6e 69 78 46 69 6c 65 29 2c 20 20 20 20 20 (unixFile),
f2dd0 2f 2a 20 73 7a 4f 73 46 69 6c 65 20 2a 2f 20 20 /* szOsFile */
f2de0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f2df0 20 20 5c 0a 20 20 20 20 4d 41 58 5f 50 41 54 48 \. MAX_PATH
f2e00 4e 41 4d 45 2c 20 20 20 20 20 20 20 20 20 2f 2a NAME, /*
f2e10 20 6d 78 50 61 74 68 6e 61 6d 65 20 2a 2f 20 20 mxPathname */
f2e20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f2e30 5c 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 \. 0,
f2e40 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 /* p
f2e50 4e 65 78 74 20 2a 2f 20 20 20 20 20 20 20 20 20 Next */
f2e60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
f2e70 20 20 20 20 56 46 53 4e 41 4d 45 2c 20 20 20 20 VFSNAME,
f2e80 20 20 20 20 20 20 20 20 20 20 2f 2a 20 7a 4e 61 /* zNa
f2e90 6d 65 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 me */
f2ea0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
f2eb0 20 20 28 76 6f 69 64 2a 29 26 46 49 4e 44 45 52 (void*)&FINDER
f2ec0 2c 20 20 20 20 20 20 20 2f 2a 20 70 41 70 70 44 , /* pAppD
f2ed0 61 74 61 20 2a 2f 20 20 20 20 20 20 20 20 20 20 ata */
f2ee0 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 \.
f2ef0 75 6e 69 78 4f 70 65 6e 2c 20 20 20 20 20 20 20 unixOpen,
f2f00 20 20 20 20 20 20 2f 2a 20 78 4f 70 65 6e 20 2a /* xOpen *
f2f10 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 /
f2f20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e \. un
f2f30 69 78 44 65 6c 65 74 65 2c 20 20 20 20 20 20 20 ixDelete,
f2f40 20 20 20 20 2f 2a 20 78 44 65 6c 65 74 65 20 2a /* xDelete *
f2f50 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 /
f2f60 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 \. unix
f2f70 41 63 63 65 73 73 2c 20 20 20 20 20 20 20 20 20 Access,
f2f80 20 20 2f 2a 20 78 41 63 63 65 73 73 20 2a 2f 20 /* xAccess */
f2f90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f2fa0 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 46 75 \. unixFu
f2fb0 6c 6c 50 61 74 68 6e 61 6d 65 2c 20 20 20 20 20 llPathname,
f2fc0 2f 2a 20 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65 /* xFullPathname
f2fd0 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 */
f2fe0 20 20 5c 0a 20 20 20 20 75 6e 69 78 44 6c 4f 70 \. unixDlOp
f2ff0 65 6e 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a en, /*
f3000 20 78 44 6c 4f 70 65 6e 20 2a 2f 20 20 20 20 20 xDlOpen */
f3010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f3020 5c 0a 20 20 20 20 75 6e 69 78 44 6c 45 72 72 6f \. unixDlErro
f3030 72 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 r, /* x
f3040 44 6c 45 72 72 6f 72 20 2a 2f 20 20 20 20 20 20 DlError */
f3050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
f3060 20 20 20 20 75 6e 69 78 44 6c 53 79 6d 2c 20 20 unixDlSym,
f3070 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c /* xDl
f3080 53 79 6d 20 2a 2f 20 20 20 20 20 20 20 20 20 20 Sym */
f3090 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
f30a0 20 20 75 6e 69 78 44 6c 43 6c 6f 73 65 2c 20 20 unixDlClose,
f30b0 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 43 6c /* xDlCl
f30c0 6f 73 65 20 2a 2f 20 20 20 20 20 20 20 20 20 20 ose */
f30d0 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 \.
f30e0 75 6e 69 78 52 61 6e 64 6f 6d 6e 65 73 73 2c 20 unixRandomness,
f30f0 20 20 20 20 20 20 2f 2a 20 78 52 61 6e 64 6f 6d /* xRandom
f3100 6e 65 73 73 20 2a 2f 20 20 20 20 20 20 20 20 20 ness */
f3110 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e \. un
f3120 69 78 53 6c 65 65 70 2c 20 20 20 20 20 20 20 20 ixSleep,
f3130 20 20 20 20 2f 2a 20 78 53 6c 65 65 70 20 2a 2f /* xSleep */
f3140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f3150 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 \. unix
f3160 43 75 72 72 65 6e 74 54 69 6d 65 2c 20 20 20 20 CurrentTime,
f3170 20 20 2f 2a 20 78 43 75 72 72 65 6e 74 54 69 6d /* xCurrentTim
f3180 65 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 e */
f3190 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 47 65 \. unixGe
f31a0 74 4c 61 73 74 45 72 72 6f 72 20 20 20 20 20 20 tLastError
f31b0 2f 2a 20 78 47 65 74 4c 61 73 74 45 72 72 6f 72 /* xGetLastError
f31c0 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 */
f31d0 20 20 5c 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 \. }.. /*.
f31e0 2a 2a 20 41 6c 6c 20 64 65 66 61 75 6c 74 20 56 ** All default V
f31f0 46 53 65 73 20 66 6f 72 20 75 6e 69 78 20 61 72 FSes for unix ar
f3200 65 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 74 e contained in t
f3210 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 72 he following arr
f3220 61 79 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4e 6f ay.. **. ** No
f3230 74 65 20 74 68 61 74 20 74 68 65 20 73 71 6c 69 te that the sqli
f3240 74 65 33 5f 76 66 73 2e 70 4e 65 78 74 20 66 69 te3_vfs.pNext fi
f3250 65 6c 64 20 6f 66 20 74 68 65 20 56 46 53 20 6f eld of the VFS o
f3260 62 6a 65 63 74 20 69 73 20 6d 6f 64 69 66 69 65 bject is modifie
f3270 64 0a 20 20 2a 2a 20 62 79 20 74 68 65 20 53 51 d. ** by the SQ
f3280 4c 69 74 65 20 63 6f 72 65 20 77 68 65 6e 20 74 Lite core when t
f3290 68 65 20 56 46 53 20 69 73 20 72 65 67 69 73 74 he VFS is regist
f32a0 65 72 65 64 2e 20 20 53 6f 20 74 68 65 20 66 6f ered. So the fo
f32b0 6c 6c 6f 77 69 6e 67 0a 20 20 2a 2a 20 61 72 72 llowing. ** arr
f32c0 61 79 20 63 61 6e 6e 6f 74 20 62 65 20 63 6f 6e ay cannot be con
f32d0 73 74 2e 0a 20 20 2a 2f 0a 20 20 73 74 61 74 69 st.. */. stati
f32e0 63 20 73 71 6c 69 74 65 33 5f 76 66 73 20 61 56 c sqlite3_vfs aV
f32f0 66 73 5b 5d 20 3d 20 7b 0a 23 69 66 20 53 51 4c fs[] = {.#if SQL
f3300 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 ITE_ENABLE_LOCKI
f3310 4e 47 5f 53 54 59 4c 45 20 26 26 20 28 4f 53 5f NG_STYLE && (OS_
f3320 56 58 57 4f 52 4b 53 20 7c 7c 20 64 65 66 69 6e VXWORKS || defin
f3330 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 29 0a 20 ed(__APPLE__)).
f3340 20 20 20 55 4e 49 58 56 46 53 28 22 75 6e 69 78 UNIXVFS("unix
f3350 22 2c 20 20 20 20 20 20 20 20 20 20 61 75 74 6f ", auto
f3360 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 20 29 2c 0a lockIoFinder ),.
f3370 23 65 6c 73 65 0a 20 20 20 20 55 4e 49 58 56 46 #else. UNIXVF
f3380 53 28 22 75 6e 69 78 22 2c 20 20 20 20 20 20 20 S("unix",
f3390 20 20 20 70 6f 73 69 78 49 6f 46 69 6e 64 65 72 posixIoFinder
f33a0 20 29 2c 0a 23 65 6e 64 69 66 0a 20 20 20 20 55 ),.#endif. U
f33b0 4e 49 58 56 46 53 28 22 75 6e 69 78 2d 6e 6f 6e NIXVFS("unix-non
f33c0 65 22 2c 20 20 20 20 20 6e 6f 6c 6f 63 6b 49 6f e", nolockIo
f33d0 46 69 6e 64 65 72 20 29 2c 0a 20 20 20 20 55 4e Finder ),. UN
f33e0 49 58 56 46 53 28 22 75 6e 69 78 2d 64 6f 74 66 IXVFS("unix-dotf
f33f0 69 6c 65 22 2c 20 20 64 6f 74 6c 6f 63 6b 49 6f ile", dotlockIo
f3400 46 69 6e 64 65 72 20 29 2c 0a 20 20 20 20 55 4e Finder ),. UN
f3410 49 58 56 46 53 28 22 75 6e 69 78 2d 77 66 6c 22 IXVFS("unix-wfl"
f3420 2c 20 20 20 20 20 20 70 6f 73 69 78 57 66 6c 49 , posixWflI
f3430 6f 46 69 6e 64 65 72 20 29 2c 0a 23 69 66 20 4f oFinder ),.#if O
f3440 53 5f 56 58 57 4f 52 4b 53 0a 20 20 20 20 55 4e S_VXWORKS. UN
f3450 49 58 56 46 53 28 22 75 6e 69 78 2d 6e 61 6d 65 IXVFS("unix-name
f3460 64 73 65 6d 22 2c 20 73 65 6d 49 6f 46 69 6e 64 dsem", semIoFind
f3470 65 72 20 29 2c 0a 23 65 6e 64 69 66 0a 23 69 66 er ),.#endif.#if
f3480 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c SQLITE_ENABLE_L
f3490 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 20 20 20 OCKING_STYLE.
f34a0 20 55 4e 49 58 56 46 53 28 22 75 6e 69 78 2d 70 UNIXVFS("unix-p
f34b0 6f 73 69 78 22 2c 20 20 20 20 70 6f 73 69 78 49 osix", posixI
f34c0 6f 46 69 6e 64 65 72 20 29 2c 0a 23 69 66 20 21 oFinder ),.#if !
f34d0 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 20 20 55 OS_VXWORKS. U
f34e0 4e 49 58 56 46 53 28 22 75 6e 69 78 2d 66 6c 6f NIXVFS("unix-flo
f34f0 63 6b 22 2c 20 20 20 20 66 6c 6f 63 6b 49 6f 46 ck", flockIoF
f3500 69 6e 64 65 72 20 29 2c 0a 23 65 6e 64 69 66 0a inder ),.#endif.
f3510 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54 #endif.#if SQLIT
f3520 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 E_ENABLE_LOCKING
f3530 5f 53 54 59 4c 45 20 26 26 20 64 65 66 69 6e 65 _STYLE && define
f3540 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 0a 20 20 20 d(__APPLE__).
f3550 20 55 4e 49 58 56 46 53 28 22 75 6e 69 78 2d 61 UNIXVFS("unix-a
f3560 66 70 22 2c 20 20 20 20 20 20 61 66 70 49 6f 46 fp", afpIoF
f3570 69 6e 64 65 72 20 29 2c 0a 20 20 20 20 55 4e 49 inder ),. UNI
f3580 58 56 46 53 28 22 75 6e 69 78 2d 70 72 6f 78 79 XVFS("unix-proxy
f3590 22 2c 20 20 20 20 70 72 6f 78 79 49 6f 46 69 6e ", proxyIoFin
f35a0 64 65 72 20 29 2c 0a 23 65 6e 64 69 66 0a 20 20 der ),.#endif.
f35b0 7d 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e };. unsigned in
f35c0 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 2f 2a t i; /*
f35d0 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f Loop counter */
f35e0 0a 0a 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 .. /* Register
f35f0 61 6c 6c 20 56 46 53 65 73 20 64 65 66 69 6e 65 all VFSes define
f3600 64 20 69 6e 20 74 68 65 20 61 56 66 73 5b 5d 20 d in the aVfs[]
f3610 61 72 72 61 79 20 2a 2f 0a 20 20 66 6f 72 28 69 array */. for(i
f3620 3d 30 3b 20 69 3c 28 73 69 7a 65 6f 66 28 61 56 =0; i<(sizeof(aV
f3630 66 73 29 2f 73 69 7a 65 6f 66 28 73 71 6c 69 74 fs)/sizeof(sqlit
f3640 65 33 5f 76 66 73 29 29 3b 20 69 2b 2b 29 7b 0a e3_vfs)); i++){.
f3650 20 20 20 20 73 71 6c 69 74 65 33 5f 76 66 73 5f sqlite3_vfs_
f3660 72 65 67 69 73 74 65 72 28 26 61 56 66 73 5b 69 register(&aVfs[i
f3670 5d 2c 20 69 3d 3d 30 29 3b 0a 20 20 7d 0a 20 20 ], i==0);. }.
f3680 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
f3690 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 68 75 74 ; .}../*.** Shut
f36a0 64 6f 77 6e 20 74 68 65 20 6f 70 65 72 61 74 69 down the operati
f36b0 6e 67 20 73 79 73 74 65 6d 20 69 6e 74 65 72 66 ng system interf
f36c0 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 20 ace..**.** Some
f36d0 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d operating system
f36e0 73 20 6d 69 67 68 74 20 6e 65 65 64 20 74 6f 20 s might need to
f36f0 64 6f 20 73 6f 6d 65 20 63 6c 65 61 6e 75 70 20 do some cleanup
f3700 69 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2c in this routine,
f3710 0a 2a 2a 20 74 6f 20 72 65 6c 65 61 73 65 20 64 .** to release d
f3720 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 ynamically alloc
f3730 61 74 65 64 20 6f 62 6a 65 63 74 73 2e 20 20 42 ated objects. B
f3740 75 74 20 6e 6f 74 20 6f 6e 20 75 6e 69 78 2e 0a ut not on unix..
f3750 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
f3760 69 73 20 61 20 6e 6f 2d 6f 70 20 66 6f 72 20 75 is a no-op for u
f3770 6e 69 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 nix..*/.SQLITE_A
f3780 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f PI int sqlite3_o
f3790 73 5f 65 6e 64 28 76 6f 69 64 29 7b 20 0a 20 20 s_end(void){ .
f37a0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
f37b0 3b 20 0a 7d 0a 20 0a 23 65 6e 64 69 66 20 2f 2a ; .}. .#endif /*
f37c0 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 20 SQLITE_OS_UNIX
f37d0 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../***********
f37e0 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73 5f 75 6e *** End of os_un
f37f0 69 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ix.c ***********
f3800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f3810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f3820 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
f3830 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f *** Begin file o
f3840 73 5f 77 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a s_win.c ********
f3850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f3860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f3870 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d **/./*.** 2004 M
f3880 61 79 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ay 22.**.** The
f3890 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 author disclaims
f38a0 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 copyright to th
f38b0 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 is source code.
f38c0 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 In place of.**
f38d0 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 a legal notice,
f38e0 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 here is a blessi
f38f0 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 ng:.**.** May
f3900 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 you do good and
f3910 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 not evil..**
f3920 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f May you find fo
f3930 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f rgiveness for yo
f3940 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 urself and forgi
f3950 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 ve others..**
f3960 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 May you share f
f3970 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b reely, never tak
f3980 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f ing more than yo
f3990 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a u give..**.*****
f39a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f39b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f39c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f39d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f39e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 *********.**.**
f39f0 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 This file contai
f3a00 6e 73 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 ns code that is
f3a10 73 70 65 63 69 66 69 63 20 74 6f 20 77 69 6e 64 specific to wind
f3a20 6f 77 73 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 ows..*/.#if SQLI
f3a30 54 45 5f 4f 53 5f 57 49 4e 20 20 20 20 20 20 20 TE_OS_WIN
f3a40 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 /* This
f3a50 66 69 6c 65 20 69 73 20 75 73 65 64 20 66 6f 72 file is used for
f3a60 20 77 69 6e 64 6f 77 73 20 6f 6e 6c 79 20 2a 2f windows only */
f3a70 0a 0a 0a 2f 2a 0a 2a 2a 20 41 20 4e 6f 74 65 20 .../*.** A Note
f3a80 41 62 6f 75 74 20 4d 65 6d 6f 72 79 20 41 6c 6c About Memory All
f3a90 6f 63 61 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 54 ocation:.**.** T
f3aa0 68 69 73 20 64 72 69 76 65 72 20 75 73 65 73 20 his driver uses
f3ab0 6d 61 6c 6c 6f 63 28 29 2f 66 72 65 65 28 29 20 malloc()/free()
f3ac0 64 69 72 65 63 74 6c 79 20 72 61 74 68 65 72 20 directly rather
f3ad0 74 68 61 6e 20 67 6f 69 6e 67 20 74 68 72 6f 75 than going throu
f3ae0 67 68 0a 2a 2a 20 74 68 65 20 53 51 4c 69 74 65 gh.** the SQLite
f3af0 2d 77 72 61 70 70 65 72 73 20 73 71 6c 69 74 65 -wrappers sqlite
f3b00 33 5f 6d 61 6c 6c 6f 63 28 29 2f 73 71 6c 69 74 3_malloc()/sqlit
f3b10 65 33 5f 66 72 65 65 28 29 2e 20 20 54 68 6f 73 e3_free(). Thos
f3b20 65 20 77 72 61 70 70 65 72 73 0a 2a 2a 20 61 72 e wrappers.** ar
f3b30 65 20 64 65 73 69 67 6e 65 64 20 66 6f 72 20 75 e designed for u
f3b40 73 65 20 6f 6e 20 65 6d 62 65 64 64 65 64 20 73 se on embedded s
f3b50 79 73 74 65 6d 73 20 77 68 65 72 65 20 6d 65 6d ystems where mem
f3b60 6f 72 79 20 69 73 20 73 63 61 72 63 65 20 61 6e ory is scarce an
f3b70 64 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 6c d.** malloc fail
f3b80 75 72 65 73 20 68 61 70 70 65 6e 20 66 72 65 71 ures happen freq
f3b90 75 65 6e 74 6c 79 2e 20 20 57 69 6e 33 32 20 64 uently. Win32 d
f3ba0 6f 65 73 20 6e 6f 74 20 74 79 70 69 63 61 6c 6c oes not typicall
f3bb0 79 20 72 75 6e 20 6f 6e 0a 2a 2a 20 65 6d 62 65 y run on.** embe
f3bc0 64 64 65 64 20 73 79 73 74 65 6d 73 2c 20 61 6e dded systems, an
f3bd0 64 20 77 68 65 6e 20 69 74 20 64 6f 65 73 20 74 d when it does t
f3be0 68 65 20 64 65 76 65 6c 6f 70 65 72 73 20 6e 6f he developers no
f3bf0 72 6d 61 6c 6c 79 20 68 61 76 65 20 62 69 67 67 rmally have bigg
f3c00 65 72 0a 2a 2a 20 70 72 6f 62 6c 65 6d 73 20 74 er.** problems t
f3c10 6f 20 77 6f 72 72 79 20 61 62 6f 75 74 20 74 68 o worry about th
f3c20 61 6e 20 72 75 6e 6e 69 6e 67 20 6f 75 74 20 6f an running out o
f3c30 66 20 6d 65 6d 6f 72 79 2e 20 20 53 6f 20 74 68 f memory. So th
f3c40 65 72 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 61 20 ere is not.** a
f3c50 63 6f 6d 70 65 6c 6c 69 6e 67 20 6e 65 65 64 20 compelling need
f3c60 74 6f 20 75 73 65 20 74 68 65 20 77 72 61 70 70 to use the wrapp
f3c70 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 42 75 74 20 74 ers..**.** But t
f3c80 68 65 72 65 20 69 73 20 61 20 67 6f 6f 64 20 72 here is a good r
f3c90 65 61 73 6f 6e 20 74 6f 20 6e 6f 74 20 75 73 65 eason to not use
f3ca0 20 74 68 65 20 77 72 61 70 70 65 72 73 2e 20 20 the wrappers.
f3cb0 49 66 20 77 65 20 75 73 65 20 74 68 65 0a 2a 2a If we use the.**
f3cc0 20 77 72 61 70 70 65 72 73 20 74 68 65 6e 20 77 wrappers then w
f3cd0 65 20 77 69 6c 6c 20 67 65 74 20 73 69 6d 75 6c e will get simul
f3ce0 61 74 65 64 20 6d 61 6c 6c 6f 63 28 29 20 66 61 ated malloc() fa
f3cf0 69 6c 75 72 65 73 20 77 69 74 68 69 6e 20 74 68 ilures within th
f3d00 69 73 0a 2a 2a 20 64 72 69 76 65 72 2e 20 20 41 is.** driver. A
f3d10 6e 64 20 74 68 61 74 20 63 61 75 73 65 73 20 61 nd that causes a
f3d20 6c 6c 20 6b 69 6e 64 73 20 6f 66 20 70 72 6f 62 ll kinds of prob
f3d30 6c 65 6d 73 20 66 6f 72 20 6f 75 72 20 74 65 73 lems for our tes
f3d40 74 73 2e 20 20 57 65 0a 2a 2a 20 63 6f 75 6c 64 ts. We.** could
f3d50 20 65 6e 68 61 6e 63 65 20 53 51 4c 69 74 65 20 enhance SQLite
f3d60 74 6f 20 64 65 61 6c 20 77 69 74 68 20 73 69 6d to deal with sim
f3d70 75 6c 61 74 65 64 20 6d 61 6c 6c 6f 63 20 66 61 ulated malloc fa
f3d80 69 6c 75 72 65 73 20 77 69 74 68 69 6e 0a 2a 2a ilures within.**
f3d90 20 74 68 65 20 4f 53 20 64 72 69 76 65 72 2c 20 the OS driver,
f3da0 62 75 74 20 74 68 65 20 63 6f 64 65 20 74 6f 20 but the code to
f3db0 64 65 61 6c 20 77 69 74 68 20 74 68 6f 73 65 20 deal with those
f3dc0 66 61 69 6c 75 72 65 20 77 6f 75 6c 64 20 6e 6f failure would no
f3dd0 74 0a 2a 2a 20 62 65 20 65 78 65 72 63 69 73 65 t.** be exercise
f3de0 64 20 6f 6e 20 4c 69 6e 75 78 20 28 77 68 69 63 d on Linux (whic
f3df0 68 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 h does not need
f3e00 74 6f 20 6d 61 6c 6c 6f 63 28 29 20 69 6e 20 74 to malloc() in t
f3e10 68 65 20 64 72 69 76 65 72 29 0a 2a 2a 20 61 6e he driver).** an
f3e20 64 20 73 6f 20 77 65 20 77 6f 75 6c 64 20 68 61 d so we would ha
f3e30 76 65 20 64 69 66 66 69 63 75 6c 74 79 20 77 72 ve difficulty wr
f3e40 69 74 69 6e 67 20 63 6f 76 65 72 61 67 65 20 74 iting coverage t
f3e50 65 73 74 73 20 66 6f 72 20 74 68 61 74 0a 2a 2a ests for that.**
f3e60 20 63 6f 64 65 2e 20 20 42 65 74 74 65 72 20 74 code. Better t
f3e70 6f 20 6c 65 61 76 65 20 74 68 65 20 63 6f 64 65 o leave the code
f3e80 20 6f 75 74 2c 20 77 65 20 74 68 69 6e 6b 2e 0a out, we think..
f3e90 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 6e 74 20 **.** The point
f3ea0 6f 66 20 74 68 69 73 20 64 69 73 63 75 73 73 69 of this discussi
f3eb0 6f 6e 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 on is as follows
f3ec0 3a 20 20 57 68 65 6e 20 63 72 65 61 74 69 6e 67 : When creating
f3ed0 20 61 20 6e 65 77 0a 2a 2a 20 4f 53 20 6c 61 79 a new.** OS lay
f3ee0 65 72 20 66 6f 72 20 61 6e 20 65 6d 62 65 64 64 er for an embedd
f3ef0 65 64 20 73 79 73 74 65 6d 2c 20 69 66 20 79 6f ed system, if yo
f3f00 75 20 75 73 65 20 74 68 69 73 20 66 69 6c 65 20 u use this file
f3f10 61 73 20 61 6e 20 65 78 61 6d 70 6c 65 2c 0a 2a as an example,.*
f3f20 2a 20 61 76 6f 69 64 20 74 68 65 20 75 73 65 20 * avoid the use
f3f30 6f 66 20 6d 61 6c 6c 6f 63 28 29 2f 66 72 65 65 of malloc()/free
f3f40 28 29 2e 20 20 54 68 6f 73 65 20 72 6f 75 74 69 (). Those routi
f3f50 6e 65 73 20 77 6f 72 6b 20 6f 6b 20 6f 6e 20 77 nes work ok on w
f3f60 69 6e 64 6f 77 73 0a 2a 2a 20 64 65 73 6b 74 6f indows.** deskto
f3f70 70 73 20 62 75 74 20 6e 6f 74 20 73 6f 20 77 65 ps but not so we
f3f80 6c 6c 20 69 6e 20 65 6d 62 65 64 64 65 64 20 73 ll in embedded s
f3f90 79 73 74 65 6d 73 2e 0a 2a 2f 0a 0a 23 69 6e 63 ystems..*/..#inc
f3fa0 6c 75 64 65 20 3c 77 69 6e 62 61 73 65 2e 68 3e lude <winbase.h>
f3fb0 0a 0a 23 69 66 64 65 66 20 5f 5f 43 59 47 57 49 ..#ifdef __CYGWI
f3fc0 4e 5f 5f 0a 23 20 69 6e 63 6c 75 64 65 20 3c 73 N__.# include <s
f3fd0 79 73 2f 63 79 67 77 69 6e 2e 68 3e 0a 23 65 6e ys/cygwin.h>.#en
f3fe0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f dif../*.** Macro
f3ff0 73 20 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d s used to determ
f4000 69 6e 65 20 77 68 65 74 68 65 72 20 6f 72 20 6e ine whether or n
f4010 6f 74 20 74 6f 20 75 73 65 20 74 68 72 65 61 64 ot to use thread
f4020 73 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 s..*/.#if define
f4030 64 28 54 48 52 45 41 44 53 41 46 45 29 20 26 26 d(THREADSAFE) &&
f4040 20 54 48 52 45 41 44 53 41 46 45 0a 23 20 64 65 THREADSAFE.# de
f4050 66 69 6e 65 20 53 51 4c 49 54 45 5f 57 33 32 5f fine SQLITE_W32_
f4060 54 48 52 45 41 44 53 20 31 0a 23 65 6e 64 69 66 THREADS 1.#endif
f4070 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 6c 75 64 65 20 ../*.** Include
f4080 63 6f 64 65 20 74 68 61 74 20 69 73 20 63 6f 6d code that is com
f4090 6d 6f 6e 20 74 6f 20 61 6c 6c 20 6f 73 5f 2a 2e mon to all os_*.
f40a0 63 20 66 69 6c 65 73 0a 2a 2f 0a 2f 2a 2a 2a 2a c files.*/./****
f40b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 ********** Inclu
f40c0 64 65 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 69 de os_common.h i
f40d0 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 n the middle of
f40e0 6f 73 5f 77 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a os_win.c *******
f40f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
f4100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
f4110 20 66 69 6c 65 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e file os_common.
f4120 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a h **************
f4130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f4140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a *********/./*.**
f4150 20 32 30 30 34 20 4d 61 79 20 32 32 0a 2a 2a 0a 2004 May 22.**.
f4160 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 ** The author di
f4170 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 sclaims copyrigh
f4180 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 t to this source
f4190 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 code. In place
f41a0 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e of.** a legal n
f41b0 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 otice, here is a
f41c0 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a blessing:.**.**
f41d0 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 May you do g
f41e0 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c ood and not evil
f41f0 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
f4200 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 find forgiveness
f4210 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e for yourself an
f4220 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 d forgive others
f4230 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
f4240 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 share freely, ne
f4250 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 ver taking more
f4260 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a than you give..*
f4270 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *.**************
f4280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f4290 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f42a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f42b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f42c0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 .**.** This file
f42d0 20 63 6f 6e 74 61 69 6e 73 20 6d 61 63 72 6f 73 contains macros
f42e0 20 61 6e 64 20 61 20 6c 69 74 74 6c 65 20 62 69 and a little bi
f42f0 74 20 6f 66 20 63 6f 64 65 20 74 68 61 74 20 69 t of code that i
f4300 73 20 63 6f 6d 6d 6f 6e 20 74 6f 0a 2a 2a 20 61 s common to.** a
f4310 6c 6c 20 6f 66 20 74 68 65 20 70 6c 61 74 66 6f ll of the platfo
f4320 72 6d 2d 73 70 65 63 69 66 69 63 20 66 69 6c 65 rm-specific file
f4330 73 20 28 6f 73 5f 2a 2e 63 29 20 61 6e 64 20 69 s (os_*.c) and i
f4340 73 20 23 69 6e 63 6c 75 64 65 64 20 69 6e 74 6f s #included into
f4350 20 74 68 6f 73 65 0a 2a 2a 20 66 69 6c 65 73 2e those.** files.
f4360 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 .**.** This file
f4370 20 73 68 6f 75 6c 64 20 62 65 20 23 69 6e 63 6c should be #incl
f4380 75 64 65 64 20 62 79 20 74 68 65 20 6f 73 5f 2a uded by the os_*
f4390 2e 63 20 66 69 6c 65 73 20 6f 6e 6c 79 2e 20 20 .c files only.
f43a0 49 74 20 69 73 20 6e 6f 74 20 61 0a 2a 2a 20 67 It is not a.** g
f43b0 65 6e 65 72 61 6c 20 70 75 72 70 6f 73 65 20 68 eneral purpose h
f43c0 65 61 64 65 72 20 66 69 6c 65 2e 0a 2a 2a 0a 2a eader file..**.*
f43d0 2a 20 24 49 64 3a 20 6f 73 5f 63 6f 6d 6d 6f 6e * $Id: os_common
f43e0 2e 68 2c 76 20 31 2e 33 38 20 32 30 30 39 2f 30 .h,v 1.38 2009/0
f43f0 32 2f 32 34 20 31 38 3a 34 30 3a 35 30 20 64 61 2/24 18:40:50 da
f4400 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a nielk1977 Exp $.
f4410 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 4f 53 5f 43 */.#ifndef _OS_C
f4420 4f 4d 4d 4f 4e 5f 48 5f 0a 23 64 65 66 69 6e 65 OMMON_H_.#define
f4430 20 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a 0a _OS_COMMON_H_..
f4440 2f 2a 0a 2a 2a 20 41 74 20 6c 65 61 73 74 20 74 /*.** At least t
f4450 77 6f 20 62 75 67 73 20 68 61 76 65 20 73 6c 69 wo bugs have sli
f4460 70 70 65 64 20 69 6e 20 62 65 63 61 75 73 65 20 pped in because
f4470 77 65 20 63 68 61 6e 67 65 64 20 74 68 65 20 4d we changed the M
f4480 45 4d 4f 52 59 5f 44 45 42 55 47 0a 2a 2a 20 6d EMORY_DEBUG.** m
f4490 61 63 72 6f 20 74 6f 20 53 51 4c 49 54 45 5f 44 acro to SQLITE_D
f44a0 45 42 55 47 20 61 6e 64 20 73 6f 6d 65 20 6f 6c EBUG and some ol
f44b0 64 65 72 20 6d 61 6b 65 66 69 6c 65 73 20 68 61 der makefiles ha
f44c0 76 65 20 6e 6f 74 20 79 65 74 20 6d 61 64 65 20 ve not yet made
f44d0 74 68 65 0a 2a 2a 20 73 77 69 74 63 68 2e 20 20 the.** switch.
f44e0 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f The following co
f44f0 64 65 20 73 68 6f 75 6c 64 20 63 61 74 63 68 20 de should catch
f4500 74 68 69 73 20 70 72 6f 62 6c 65 6d 20 61 74 20 this problem at
f4510 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 2e 0a 2a 2f compile-time..*/
f4520 0a 23 69 66 64 65 66 20 4d 45 4d 4f 52 59 5f 44 .#ifdef MEMORY_D
f4530 45 42 55 47 0a 23 20 65 72 72 6f 72 20 22 54 68 EBUG.# error "Th
f4540 65 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47 20 6d e MEMORY_DEBUG m
f4550 61 63 72 6f 20 69 73 20 6f 62 73 6f 6c 65 74 65 acro is obsolete
f4560 2e 20 20 55 73 65 20 53 51 4c 49 54 45 5f 44 45 . Use SQLITE_DE
f4570 42 55 47 20 69 6e 73 74 65 61 64 2e 22 0a 23 65 BUG instead.".#e
f4580 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c ndif..#ifdef SQL
f4590 49 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 ITE_DEBUG.SQLITE
f45a0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
f45b0 69 74 65 33 4f 53 54 72 61 63 65 20 3d 20 30 3b ite3OSTrace = 0;
f45c0 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 .#define OSTRACE
f45d0 31 28 58 29 20 20 20 20 20 20 20 20 20 69 66 28 1(X) if(
f45e0 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 sqlite3OSTrace
f45f0 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 ) sqlite3DebugPr
f4600 69 6e 74 66 28 58 29 0a 23 64 65 66 69 6e 65 20 intf(X).#define
f4610 4f 53 54 52 41 43 45 32 28 58 2c 59 29 20 20 20 OSTRACE2(X,Y)
f4620 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f if( sqlite3O
f4630 53 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 STrace ) sqlite3
f4640 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 29 DebugPrintf(X,Y)
f4650 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 .#define OSTRACE
f4660 33 28 58 2c 59 2c 5a 29 20 20 20 20 20 69 66 28 3(X,Y,Z) if(
f4670 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 sqlite3OSTrace
f4680 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 ) sqlite3DebugPr
f4690 69 6e 74 66 28 58 2c 59 2c 5a 29 0a 23 64 65 66 intf(X,Y,Z).#def
f46a0 69 6e 65 20 4f 53 54 52 41 43 45 34 28 58 2c 59 ine OSTRACE4(X,Y
f46b0 2c 5a 2c 41 29 20 20 20 69 66 28 20 73 71 6c 69 ,Z,A) if( sqli
f46c0 74 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c te3OSTrace ) sql
f46d0 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 ite3DebugPrintf(
f46e0 58 2c 59 2c 5a 2c 41 29 0a 23 64 65 66 69 6e 65 X,Y,Z,A).#define
f46f0 20 4f 53 54 52 41 43 45 35 28 58 2c 59 2c 5a 2c OSTRACE5(X,Y,Z,
f4700 41 2c 42 29 20 69 66 28 20 73 71 6c 69 74 65 33 A,B) if( sqlite3
f4710 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 OSTrace ) sqlite
f4720 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 3DebugPrintf(X,Y
f4730 2c 5a 2c 41 2c 42 29 0a 23 64 65 66 69 6e 65 20 ,Z,A,B).#define
f4740 4f 53 54 52 41 43 45 36 28 58 2c 59 2c 5a 2c 41 OSTRACE6(X,Y,Z,A
f4750 2c 42 2c 43 29 20 5c 0a 20 20 20 20 69 66 28 73 ,B,C) \. if(s
f4760 71 6c 69 74 65 33 4f 53 54 72 61 63 65 29 20 73 qlite3OSTrace) s
f4770 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 qlite3DebugPrint
f4780 66 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 0a 23 f(X,Y,Z,A,B,C).#
f4790 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 37 28 define OSTRACE7(
f47a0 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 20 5c X,Y,Z,A,B,C,D) \
f47b0 0a 20 20 20 20 69 66 28 73 71 6c 69 74 65 33 4f . if(sqlite3O
f47c0 53 54 72 61 63 65 29 20 73 71 6c 69 74 65 33 44 STrace) sqlite3D
f47d0 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a ebugPrintf(X,Y,Z
f47e0 2c 41 2c 42 2c 43 2c 44 29 0a 23 65 6c 73 65 0a ,A,B,C,D).#else.
f47f0 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 31 #define OSTRACE1
f4800 28 58 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 (X).#define OSTR
f4810 41 43 45 32 28 58 2c 59 29 0a 23 64 65 66 69 6e ACE2(X,Y).#defin
f4820 65 20 4f 53 54 52 41 43 45 33 28 58 2c 59 2c 5a e OSTRACE3(X,Y,Z
f4830 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 ).#define OSTRAC
f4840 45 34 28 58 2c 59 2c 5a 2c 41 29 0a 23 64 65 66 E4(X,Y,Z,A).#def
f4850 69 6e 65 20 4f 53 54 52 41 43 45 35 28 58 2c 59 ine OSTRACE5(X,Y
f4860 2c 5a 2c 41 2c 42 29 0a 23 64 65 66 69 6e 65 20 ,Z,A,B).#define
f4870 4f 53 54 52 41 43 45 36 28 58 2c 59 2c 5a 2c 41 OSTRACE6(X,Y,Z,A
f4880 2c 42 2c 43 29 0a 23 64 65 66 69 6e 65 20 4f 53 ,B,C).#define OS
f4890 54 52 41 43 45 37 28 58 2c 59 2c 5a 2c 41 2c 42 TRACE7(X,Y,Z,A,B
f48a0 2c 43 2c 44 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a ,C,D).#endif../*
f48b0 0a 2a 2a 20 4d 61 63 72 6f 73 20 66 6f 72 20 70 .** Macros for p
f48c0 65 72 66 6f 72 6d 61 6e 63 65 20 74 72 61 63 69 erformance traci
f48d0 6e 67 2e 20 20 4e 6f 72 6d 61 6c 6c 79 20 74 75 ng. Normally tu
f48e0 72 6e 65 64 20 6f 66 66 2e 20 20 4f 6e 6c 79 20 rned off. Only
f48f0 77 6f 72 6b 73 0a 2a 2a 20 6f 6e 20 69 34 38 36 works.** on i486
f4900 20 68 61 72 64 77 61 72 65 2e 0a 2a 2f 0a 23 69 hardware..*/.#i
f4910 66 64 65 66 20 53 51 4c 49 54 45 5f 50 45 52 46 fdef SQLITE_PERF
f4920 4f 52 4d 41 4e 43 45 5f 54 52 41 43 45 0a 0a 2f ORMANCE_TRACE../
f4930 2a 20 0a 2a 2a 20 68 77 74 69 6d 65 2e 68 20 63 * .** hwtime.h c
f4940 6f 6e 74 61 69 6e 73 20 69 6e 6c 69 6e 65 20 61 ontains inline a
f4950 73 73 65 6d 62 6c 65 72 20 63 6f 64 65 20 66 6f ssembler code fo
f4960 72 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 0a r implementing .
f4970 2a 2a 20 68 69 67 68 2d 70 65 72 66 6f 72 6d 61 ** high-performa
f4980 6e 63 65 20 74 69 6d 69 6e 67 20 72 6f 75 74 69 nce timing routi
f4990 6e 65 73 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a nes..*/./*******
f49a0 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 ******* Include
f49b0 68 77 74 69 6d 65 2e 68 20 69 6e 20 74 68 65 20 hwtime.h in the
f49c0 6d 69 64 64 6c 65 20 6f 66 20 6f 73 5f 63 6f 6d middle of os_com
f49d0 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a mon.h **********
f49e0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
f49f0 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 ******* Begin fi
f4a00 6c 65 20 68 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a le hwtime.h ****
f4a10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f4a20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f4a30 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 ******/./*.** 20
f4a40 30 38 20 4d 61 79 20 32 37 0a 2a 2a 0a 2a 2a 20 08 May 27.**.**
f4a50 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c The author discl
f4a60 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 aims copyright t
f4a70 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f o this source co
f4a80 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 de. In place of
f4a90 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 .** a legal noti
f4aa0 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c ce, here is a bl
f4ab0 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 essing:.**.**
f4ac0 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 May you do good
f4ad0 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a and not evil..*
f4ae0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e * May you fin
f4af0 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f d forgiveness fo
f4b00 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 r yourself and f
f4b10 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a orgive others..*
f4b20 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 * May you sha
f4b30 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 re freely, never
f4b40 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 taking more tha
f4b50 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a n you give..**.*
f4b60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f4b70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f4b80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f4b90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f4ba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
f4bb0 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f .** This file co
f4bc0 6e 74 61 69 6e 73 20 69 6e 6c 69 6e 65 20 61 73 ntains inline as
f4bd0 6d 20 63 6f 64 65 20 66 6f 72 20 72 65 74 72 69 m code for retri
f4be0 65 76 69 6e 67 20 22 68 69 67 68 2d 70 65 72 66 eving "high-perf
f4bf0 6f 72 6d 61 6e 63 65 22 0a 2a 2a 20 63 6f 75 6e ormance".** coun
f4c00 74 65 72 73 20 66 6f 72 20 78 38 36 20 63 6c 61 ters for x86 cla
f4c10 73 73 20 43 50 55 73 2e 0a 2a 2a 0a 2a 2a 20 24 ss CPUs..**.** $
f4c20 49 64 3a 20 68 77 74 69 6d 65 2e 68 2c 76 20 31 Id: hwtime.h,v 1
f4c30 2e 33 20 32 30 30 38 2f 30 38 2f 30 31 20 31 34 .3 2008/08/01 14
f4c40 3a 33 33 3a 31 35 20 73 68 61 6e 65 20 45 78 70 :33:15 shane Exp
f4c50 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 48 $.*/.#ifndef _H
f4c60 57 54 49 4d 45 5f 48 5f 0a 23 64 65 66 69 6e 65 WTIME_H_.#define
f4c70 20 5f 48 57 54 49 4d 45 5f 48 5f 0a 0a 2f 2a 0a _HWTIME_H_../*.
f4c80 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 ** The following
f4c90 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 77 6f routine only wo
f4ca0 72 6b 73 20 6f 6e 20 70 65 6e 74 69 75 6d 2d 63 rks on pentium-c
f4cb0 6c 61 73 73 20 28 6f 72 20 6e 65 77 65 72 29 20 lass (or newer)
f4cc0 70 72 6f 63 65 73 73 6f 72 73 2e 0a 2a 2a 20 49 processors..** I
f4cd0 74 20 75 73 65 73 20 74 68 65 20 52 44 54 53 43 t uses the RDTSC
f4ce0 20 6f 70 63 6f 64 65 20 74 6f 20 72 65 61 64 20 opcode to read
f4cf0 74 68 65 20 63 79 63 6c 65 20 63 6f 75 6e 74 20 the cycle count
f4d00 76 61 6c 75 65 20 6f 75 74 20 6f 66 20 74 68 65 value out of the
f4d10 0a 2a 2a 20 70 72 6f 63 65 73 73 6f 72 20 61 6e .** processor an
f4d20 64 20 72 65 74 75 72 6e 73 20 74 68 61 74 20 76 d returns that v
f4d30 61 6c 75 65 2e 20 20 54 68 69 73 20 63 61 6e 20 alue. This can
f4d40 62 65 20 75 73 65 64 20 66 6f 72 20 68 69 67 68 be used for high
f4d50 2d 72 65 73 0a 2a 2a 20 70 72 6f 66 69 6c 69 6e -res.** profilin
f4d60 67 2e 0a 2a 2f 0a 23 69 66 20 28 64 65 66 69 6e g..*/.#if (defin
f4d70 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 7c 7c 20 ed(__GNUC__) ||
f4d80 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52 defined(_MSC_VER
f4d90 29 29 20 26 26 20 5c 0a 20 20 20 20 20 20 28 64 )) && \. (d
f4da0 65 66 69 6e 65 64 28 69 33 38 36 29 20 7c 7c 20 efined(i386) ||
f4db0 64 65 66 69 6e 65 64 28 5f 5f 69 33 38 36 5f 5f defined(__i386__
f4dc0 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4d 5f ) || defined(_M_
f4dd0 49 58 38 36 29 29 0a 0a 20 20 23 69 66 20 64 65 IX86)).. #if de
f4de0 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 0a fined(__GNUC__).
f4df0 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 . __inline__ sq
f4e00 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 lite_uint64 sqli
f4e10 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b te3Hwtime(void){
f4e20 0a 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 69 . unsigned i
f4e30 6e 74 20 6c 6f 2c 20 68 69 3b 0a 20 20 20 20 20 nt lo, hi;.
f4e40 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 __asm__ __volati
f4e50 6c 65 5f 5f 20 28 22 72 64 74 73 63 22 20 3a 20 le__ ("rdtsc" :
f4e60 22 3d 61 22 20 28 6c 6f 29 2c 20 22 3d 64 22 20 "=a" (lo), "=d"
f4e70 28 68 69 29 29 3b 0a 20 20 20 20 20 72 65 74 75 (hi));. retu
f4e80 72 6e 20 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 rn (sqlite_uint6
f4e90 34 29 68 69 20 3c 3c 20 33 32 20 7c 20 6c 6f 3b 4)hi << 32 | lo;
f4ea0 0a 20 20 7d 0a 0a 20 20 23 65 6c 69 66 20 64 65 . }.. #elif de
f4eb0 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 0a fined(_MSC_VER).
f4ec0 0a 20 20 5f 5f 64 65 63 6c 73 70 65 63 28 6e 61 . __declspec(na
f4ed0 6b 65 64 29 20 5f 5f 69 6e 6c 69 6e 65 20 73 71 ked) __inline sq
f4ee0 6c 69 74 65 5f 75 69 6e 74 36 34 20 5f 5f 63 64 lite_uint64 __cd
f4ef0 65 63 6c 20 73 71 6c 69 74 65 33 48 77 74 69 6d ecl sqlite3Hwtim
f4f00 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 5f 5f e(void){. __
f4f10 61 73 6d 20 7b 0a 20 20 20 20 20 20 20 20 72 64 asm {. rd
f4f20 74 73 63 0a 20 20 20 20 20 20 20 20 72 65 74 20 tsc. ret
f4f30 20 20 20 20 20 20 3b 20 72 65 74 75 72 6e 20 76 ; return v
f4f40 61 6c 75 65 20 61 74 20 45 44 58 3a 45 41 58 0a alue at EDX:EAX.
f4f50 20 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 23 65 }. }.. #e
f4f60 6e 64 69 66 0a 0a 23 65 6c 69 66 20 28 64 65 66 ndif..#elif (def
f4f70 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 ined(__GNUC__) &
f4f80 26 20 64 65 66 69 6e 65 64 28 5f 5f 78 38 36 5f & defined(__x86_
f4f90 36 34 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 64__)).. __inli
f4fa0 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 ne__ sqlite_uint
f4fb0 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 64 sqlite3Hwtime
f4fc0 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 20 75 6e (void){. un
f4fd0 73 69 67 6e 65 64 20 6c 6f 6e 67 20 76 61 6c 3b signed long val;
f4fe0 0a 20 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f . __asm__ _
f4ff0 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 72 64 _volatile__ ("rd
f5000 74 73 63 22 20 3a 20 22 3d 41 22 20 28 76 61 6c tsc" : "=A" (val
f5010 29 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ));. return
f5020 20 76 61 6c 3b 0a 20 20 7d 0a 20 0a 23 65 6c 69 val;. }. .#eli
f5030 66 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 f (defined(__GNU
f5040 43 5f 5f 29 20 26 26 20 64 65 66 69 6e 65 64 28 C__) && defined(
f5050 5f 5f 70 70 63 5f 5f 29 29 0a 0a 20 20 5f 5f 69 __ppc__)).. __i
f5060 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 nline__ sqlite_u
f5070 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 int64 sqlite3Hwt
f5080 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 ime(void){.
f5090 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c unsigned long l
f50a0 6f 6e 67 20 72 65 74 76 61 6c 3b 0a 20 20 20 20 ong retval;.
f50b0 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 unsigned long
f50c0 6a 75 6e 6b 3b 0a 20 20 20 20 20 20 5f 5f 61 73 junk;. __as
f50d0 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f m__ __volatile__
f50e0 20 28 22 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 ("\n\.
f50f0 20 31 3a 20 20 20 20 20 20 6d 66 74 62 75 20 20 1: mftbu
f5100 20 25 31 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 %1\n\.
f5110 20 20 20 20 20 20 20 20 20 6d 66 74 62 20 20 20 mftb
f5120 20 25 4c 30 5c 6e 5c 0a 20 20 20 20 20 20 20 20 %L0\n\.
f5130 20 20 20 20 20 20 20 20 20 20 6d 66 74 62 75 20 mftbu
f5140 20 20 25 30 5c 6e 5c 0a 20 20 20 20 20 20 20 20 %0\n\.
f5150 20 20 20 20 20 20 20 20 20 20 63 6d 70 77 20 20 cmpw
f5160 20 20 25 30 2c 25 31 5c 6e 5c 0a 20 20 20 20 20 %0,%1\n\.
f5170 20 20 20 20 20 20 20 20 20 20 20 20 20 62 6e 65 bne
f5180 20 20 20 20 20 31 62 22 0a 20 20 20 20 20 20 20 1b".
f5190 20 20 20 20 20 20 20 20 20 20 20 3a 20 22 3d 72 : "=r
f51a0 22 20 28 72 65 74 76 61 6c 29 2c 20 22 3d 72 22 " (retval), "=r"
f51b0 20 28 6a 75 6e 6b 29 29 3b 0a 20 20 20 20 20 20 (junk));.
f51c0 72 65 74 75 72 6e 20 72 65 74 76 61 6c 3b 0a 20 return retval;.
f51d0 20 7d 0a 0a 23 65 6c 73 65 0a 0a 20 20 23 65 72 }..#else.. #er
f51e0 72 6f 72 20 4e 65 65 64 20 69 6d 70 6c 65 6d 65 ror Need impleme
f51f0 6e 74 61 74 69 6f 6e 20 6f 66 20 73 71 6c 69 74 ntation of sqlit
f5200 65 33 48 77 74 69 6d 65 28 29 20 66 6f 72 20 79 e3Hwtime() for y
f5210 6f 75 72 20 70 6c 61 74 66 6f 72 6d 2e 0a 0a 20 our platform...
f5220 20 2f 2a 0a 20 20 2a 2a 20 54 6f 20 63 6f 6d 70 /*. ** To comp
f5230 69 6c 65 20 77 69 74 68 6f 75 74 20 69 6d 70 6c ile without impl
f5240 65 6d 65 6e 74 69 6e 67 20 73 71 6c 69 74 65 33 ementing sqlite3
f5250 48 77 74 69 6d 65 28 29 20 66 6f 72 20 79 6f 75 Hwtime() for you
f5260 72 20 70 6c 61 74 66 6f 72 6d 2c 0a 20 20 2a 2a r platform,. **
f5270 20 79 6f 75 20 63 61 6e 20 72 65 6d 6f 76 65 20 you can remove
f5280 74 68 65 20 61 62 6f 76 65 20 23 65 72 72 6f 72 the above #error
f5290 20 61 6e 64 20 75 73 65 20 74 68 65 20 66 6f 6c and use the fol
f52a0 6c 6f 77 69 6e 67 0a 20 20 2a 2a 20 73 74 75 62 lowing. ** stub
f52b0 20 66 75 6e 63 74 69 6f 6e 2e 20 20 59 6f 75 20 function. You
f52c0 77 69 6c 6c 20 6c 6f 73 65 20 74 69 6d 69 6e 67 will lose timing
f52d0 20 73 75 70 70 6f 72 74 20 66 6f 72 20 6d 61 6e support for man
f52e0 79 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 64 65 y. ** of the de
f52f0 62 75 67 67 69 6e 67 20 61 6e 64 20 74 65 73 74 bugging and test
f5300 69 6e 67 20 75 74 69 6c 69 74 69 65 73 2c 20 62 ing utilities, b
f5310 75 74 20 69 74 20 73 68 6f 75 6c 64 20 61 74 0a ut it should at.
f5320 20 20 2a 2a 20 6c 65 61 73 74 20 63 6f 6d 70 69 ** least compi
f5330 6c 65 20 61 6e 64 20 72 75 6e 2e 0a 20 20 2a 2f le and run.. */
f5340 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
f5350 20 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 sqlite_uint64
f5360 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f sqlite3Hwtime(vo
f5370 69 64 29 7b 20 72 65 74 75 72 6e 20 28 28 73 71 id){ return ((sq
f5380 6c 69 74 65 5f 75 69 6e 74 36 34 29 30 29 3b 20 lite_uint64)0);
f5390 7d 0a 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 }..#endif..#endi
f53a0 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 5f 48 f /* !defined(_H
f53b0 57 54 49 4d 45 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a WTIME_H_) */../*
f53c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e ************* En
f53d0 64 20 6f 66 20 68 77 74 69 6d 65 2e 68 20 2a 2a d of hwtime.h **
f53e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f53f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f5400 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
f5410 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f ************* Co
f5420 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 ntinuing where w
f5430 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 6f 73 e left off in os
f5440 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a _common.h ******
f5450 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 73 ************/..s
f5460 74 61 74 69 63 20 73 71 6c 69 74 65 5f 75 69 6e tatic sqlite_uin
f5470 74 36 34 20 67 5f 73 74 61 72 74 3b 0a 73 74 61 t64 g_start;.sta
f5480 74 69 63 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 tic sqlite_uint6
f5490 34 20 67 5f 65 6c 61 70 73 65 64 3b 0a 23 64 65 4 g_elapsed;.#de
f54a0 66 69 6e 65 20 54 49 4d 45 52 5f 53 54 41 52 54 fine TIMER_START
f54b0 20 20 20 20 20 20 20 67 5f 73 74 61 72 74 3d 73 g_start=s
f54c0 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29 0a 23 qlite3Hwtime().#
f54d0 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4e 44 define TIMER_END
f54e0 20 20 20 20 20 20 20 20 20 67 5f 65 6c 61 70 73 g_elaps
f54f0 65 64 3d 73 71 6c 69 74 65 33 48 77 74 69 6d 65 ed=sqlite3Hwtime
f5500 28 29 2d 67 5f 73 74 61 72 74 0a 23 64 65 66 69 ()-g_start.#defi
f5510 6e 65 20 54 49 4d 45 52 5f 45 4c 41 50 53 45 44 ne TIMER_ELAPSED
f5520 20 20 20 20 20 67 5f 65 6c 61 70 73 65 64 0a 23 g_elapsed.#
f5530 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 54 49 4d else.#define TIM
f5540 45 52 5f 53 54 41 52 54 0a 23 64 65 66 69 6e 65 ER_START.#define
f5550 20 54 49 4d 45 52 5f 45 4e 44 0a 23 64 65 66 69 TIMER_END.#defi
f5560 6e 65 20 54 49 4d 45 52 5f 45 4c 41 50 53 45 44 ne TIMER_ELAPSED
f5570 20 20 20 20 20 28 28 73 71 6c 69 74 65 5f 75 69 ((sqlite_ui
f5580 6e 74 36 34 29 30 29 0a 23 65 6e 64 69 66 0a 0a nt64)0).#endif..
f5590 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 63 6f 6d 70 /*.** If we comp
f55a0 69 6c 65 20 77 69 74 68 20 74 68 65 20 53 51 4c ile with the SQL
f55b0 49 54 45 5f 54 45 53 54 20 6d 61 63 72 6f 20 73 ITE_TEST macro s
f55c0 65 74 2c 20 74 68 65 6e 20 74 68 65 20 66 6f 6c et, then the fol
f55d0 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 0a 2a 2a 20 lowing block.**
f55e0 6f 66 20 63 6f 64 65 20 77 69 6c 6c 20 67 69 76 of code will giv
f55f0 65 20 75 73 20 74 68 65 20 61 62 69 6c 69 74 79 e us the ability
f5600 20 74 6f 20 73 69 6d 75 6c 61 74 65 20 61 20 64 to simulate a d
f5610 69 73 6b 20 49 2f 4f 20 65 72 72 6f 72 2e 20 20 isk I/O error.
f5620 54 68 69 73 0a 2a 2a 20 69 73 20 75 73 65 64 20 This.** is used
f5630 66 6f 72 20 74 65 73 74 69 6e 67 20 74 68 65 20 for testing the
f5640 49 2f 4f 20 72 65 63 6f 76 65 72 79 20 6c 6f 67 I/O recovery log
f5650 69 63 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 ic..*/.#ifdef SQ
f5660 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 LITE_TEST.SQLITE
f5670 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
f5680 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 20 3d 20 _io_error_hit =
f5690 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 0; /*
f56a0 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 Total number of
f56b0 20 49 2f 4f 20 45 72 72 6f 72 73 20 2a 2f 0a 53 I/O Errors */.S
f56c0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
f56d0 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 lite3_io_error_h
f56e0 61 72 64 68 69 74 20 3d 20 30 3b 20 20 20 20 20 ardhit = 0;
f56f0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
f5700 6e 6f 6e 2d 62 65 6e 69 67 6e 20 65 72 72 6f 72 non-benign error
f5710 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 s */.SQLITE_API
f5720 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 int sqlite3_io_e
f5730 72 72 6f 72 5f 70 65 6e 64 69 6e 67 20 3d 20 30 rror_pending = 0
f5740 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 75 6e ; /* Coun
f5750 74 20 64 6f 77 6e 20 74 6f 20 66 69 72 73 74 20 t down to first
f5760 49 2f 4f 20 65 72 72 6f 72 20 2a 2f 0a 53 51 4c I/O error */.SQL
f5770 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
f5780 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 72 te3_io_error_per
f5790 73 69 73 74 20 3d 20 30 3b 20 20 20 20 20 20 20 sist = 0;
f57a0 20 2f 2a 20 54 72 75 65 20 69 66 20 49 2f 4f 20 /* True if I/O
f57b0 65 72 72 6f 72 73 20 70 65 72 73 69 73 74 20 2a errors persist *
f57c0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
f57d0 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f sqlite3_io_erro
f57e0 72 5f 62 65 6e 69 67 6e 20 3d 20 30 3b 20 20 20 r_benign = 0;
f57f0 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
f5800 20 65 72 72 6f 72 73 20 61 72 65 20 62 65 6e 69 errors are beni
f5810 67 6e 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 gn */.SQLITE_API
f5820 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 69 73 int sqlite3_dis
f5830 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 3d 20 kfull_pending =
f5840 30 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 0;.SQLITE_API in
f5850 74 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 t sqlite3_diskfu
f5860 6c 6c 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 ll = 0;.#define
f5870 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 SimulateIOErrorB
f5880 65 6e 69 67 6e 28 58 29 20 73 71 6c 69 74 65 33 enign(X) sqlite3
f5890 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67 6e _io_error_benign
f58a0 3d 28 58 29 0a 23 64 65 66 69 6e 65 20 53 69 6d =(X).#define Sim
f58b0 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 43 4f 44 ulateIOError(COD
f58c0 45 29 20 20 5c 0a 20 20 69 66 28 20 28 73 71 6c E) \. if( (sql
f58d0 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 ite3_io_error_pe
f58e0 72 73 69 73 74 20 26 26 20 73 71 6c 69 74 65 33 rsist && sqlite3
f58f0 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 29 20 5c _io_error_hit) \
f5900 0a 20 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 . || sqlit
f5910 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 e3_io_error_pend
f5920 69 6e 67 2d 2d 20 3d 3d 20 31 20 29 20 20 5c 0a ing-- == 1 ) \.
f5930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 {
f5940 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 29 3b 20 43 local_ioerr(); C
f5950 4f 44 45 3b 20 7d 0a 73 74 61 74 69 63 20 76 6f ODE; }.static vo
f5960 69 64 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 29 id local_ioerr()
f5970 7b 0a 20 20 49 4f 54 52 41 43 45 28 28 22 49 4f {. IOTRACE(("IO
f5980 45 52 52 5c 6e 22 29 29 3b 0a 20 20 73 71 6c 69 ERR\n"));. sqli
f5990 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 te3_io_error_hit
f59a0 2b 2b 3b 0a 20 20 69 66 28 20 21 73 71 6c 69 74 ++;. if( !sqlit
f59b0 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 e3_io_error_beni
f59c0 67 6e 20 29 20 73 71 6c 69 74 65 33 5f 69 6f 5f gn ) sqlite3_io_
f59d0 65 72 72 6f 72 5f 68 61 72 64 68 69 74 2b 2b 3b error_hardhit++;
f59e0 0a 7d 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c .}.#define Simul
f59f0 61 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72 ateDiskfullError
f5a00 28 43 4f 44 45 29 20 5c 0a 20 20 20 69 66 28 20 (CODE) \. if(
f5a10 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c sqlite3_diskfull
f5a20 5f 70 65 6e 64 69 6e 67 20 29 7b 20 5c 0a 20 20 _pending ){ \.
f5a30 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 64 if( sqlite3_d
f5a40 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 iskfull_pending
f5a50 3d 3d 20 31 20 29 7b 20 5c 0a 20 20 20 20 20 20 == 1 ){ \.
f5a60 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 29 3b 20 local_ioerr();
f5a70 5c 0a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 \. sqlite3
f5a80 5f 64 69 73 6b 66 75 6c 6c 20 3d 20 31 3b 20 5c _diskfull = 1; \
f5a90 0a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f . sqlite3_
f5aa0 69 6f 5f 65 72 72 6f 72 5f 68 69 74 20 3d 20 31 io_error_hit = 1
f5ab0 3b 20 5c 0a 20 20 20 20 20 20 20 43 4f 44 45 3b ; \. CODE;
f5ac0 20 5c 0a 20 20 20 20 20 7d 65 6c 73 65 7b 20 5c \. }else{ \
f5ad0 0a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f . sqlite3_
f5ae0 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 diskfull_pending
f5af0 2d 2d 3b 20 5c 0a 20 20 20 20 20 7d 20 5c 0a 20 --; \. } \.
f5b00 20 20 7d 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e }.#else.#defin
f5b10 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f e SimulateIOErro
f5b20 72 42 65 6e 69 67 6e 28 58 29 0a 23 64 65 66 69 rBenign(X).#defi
f5b30 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 ne SimulateIOErr
f5b40 6f 72 28 41 29 0a 23 64 65 66 69 6e 65 20 53 69 or(A).#define Si
f5b50 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 72 mulateDiskfullEr
f5b60 72 6f 72 28 41 29 0a 23 65 6e 64 69 66 0a 0a 2f ror(A).#endif../
f5b70 2a 0a 2a 2a 20 57 68 65 6e 20 74 65 73 74 69 6e *.** When testin
f5b80 67 2c 20 6b 65 65 70 20 61 20 63 6f 75 6e 74 20 g, keep a count
f5b90 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 of the number of
f5ba0 20 6f 70 65 6e 20 66 69 6c 65 73 2e 0a 2a 2f 0a open files..*/.
f5bb0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 #ifdef SQLITE_TE
f5bc0 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e ST.SQLITE_API in
f5bd0 74 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 66 t sqlite3_open_f
f5be0 69 6c 65 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 ile_count = 0;.#
f5bf0 64 65 66 69 6e 65 20 4f 70 65 6e 43 6f 75 6e 74 define OpenCount
f5c00 65 72 28 58 29 20 20 73 71 6c 69 74 65 33 5f 6f er(X) sqlite3_o
f5c10 70 65 6e 5f 66 69 6c 65 5f 63 6f 75 6e 74 2b 3d pen_file_count+=
f5c20 28 58 29 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e (X).#else.#defin
f5c30 65 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 58 29 e OpenCounter(X)
f5c40 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20 .#endif..#endif
f5c50 2f 2a 20 21 64 65 66 69 6e 65 64 28 5f 4f 53 5f /* !defined(_OS_
f5c60 43 4f 4d 4d 4f 4e 5f 48 5f 29 20 2a 2f 0a 0a 2f COMMON_H_) */../
f5c70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
f5c80 6e 64 20 6f 66 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e nd of os_common.
f5c90 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a h **************
f5ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f5cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
f5cc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 ************** C
f5cd0 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 ontinuing where
f5ce0 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 6f we left off in o
f5cf0 73 5f 77 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a s_win.c ********
f5d00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a *************/..
f5d10 2f 2a 0a 2a 2a 20 53 6f 6d 65 20 6d 69 63 72 6f /*.** Some micro
f5d20 73 6f 66 74 20 63 6f 6d 70 69 6c 65 72 73 20 6c soft compilers l
f5d30 61 63 6b 20 74 68 69 73 20 64 65 66 69 6e 69 74 ack this definit
f5d40 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 ion..*/.#ifndef
f5d50 49 4e 56 41 4c 49 44 5f 46 49 4c 45 5f 41 54 54 INVALID_FILE_ATT
f5d60 52 49 42 55 54 45 53 0a 23 20 64 65 66 69 6e 65 RIBUTES.# define
f5d70 20 49 4e 56 41 4c 49 44 5f 46 49 4c 45 5f 41 54 INVALID_FILE_AT
f5d80 54 52 49 42 55 54 45 53 20 28 28 44 57 4f 52 44 TRIBUTES ((DWORD
f5d90 29 2d 31 29 20 0a 23 65 6e 64 69 66 0a 0a 2f 2a )-1) .#endif../*
f5da0 0a 2a 2a 20 44 65 74 65 72 6d 69 6e 65 20 69 66 .** Determine if
f5db0 20 77 65 20 61 72 65 20 64 65 61 6c 69 6e 67 20 we are dealing
f5dc0 77 69 74 68 20 57 69 6e 64 6f 77 73 43 45 20 2d with WindowsCE -
f5dd0 20 77 68 69 63 68 20 68 61 73 20 61 20 6d 75 63 which has a muc
f5de0 68 0a 2a 2a 20 72 65 64 75 63 65 64 20 41 50 49 h.** reduced API
f5df0 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f ..*/.#if SQLITE_
f5e00 4f 53 5f 57 49 4e 43 45 0a 23 20 64 65 66 69 6e OS_WINCE.# defin
f5e10 65 20 41 72 65 46 69 6c 65 41 70 69 73 41 4e 53 e AreFileApisANS
f5e20 49 28 29 20 31 0a 23 20 64 65 66 69 6e 65 20 47 I() 1.# define G
f5e30 65 74 44 69 73 6b 46 72 65 65 53 70 61 63 65 57 etDiskFreeSpaceW
f5e40 28 29 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a () 0.#endif../*.
f5e50 2a 2a 20 57 69 6e 43 45 20 6c 61 63 6b 73 20 6e ** WinCE lacks n
f5e60 61 74 69 76 65 20 73 75 70 70 6f 72 74 20 66 6f ative support fo
f5e70 72 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 73 r file locking s
f5e80 6f 20 77 65 20 68 61 76 65 20 74 6f 20 66 61 6b o we have to fak
f5e90 65 20 69 74 0a 2a 2a 20 77 69 74 68 20 73 6f 6d e it.** with som
f5ea0 65 20 63 6f 64 65 20 6f 66 20 6f 75 72 20 6f 77 e code of our ow
f5eb0 6e 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 n..*/.#if SQLITE
f5ec0 5f 4f 53 5f 57 49 4e 43 45 0a 74 79 70 65 64 65 _OS_WINCE.typede
f5ed0 66 20 73 74 72 75 63 74 20 77 69 6e 63 65 4c 6f f struct winceLo
f5ee0 63 6b 20 7b 0a 20 20 69 6e 74 20 6e 52 65 61 64 ck {. int nRead
f5ef0 65 72 73 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 ers; /* Nu
f5f00 6d 62 65 72 20 6f 66 20 72 65 61 64 65 72 20 6c mber of reader l
f5f10 6f 63 6b 73 20 6f 62 74 61 69 6e 65 64 20 2a 2f ocks obtained */
f5f20 0a 20 20 42 4f 4f 4c 20 62 50 65 6e 64 69 6e 67 . BOOL bPending
f5f30 3b 20 20 20 20 20 20 2f 2a 20 49 6e 64 69 63 61 ; /* Indica
f5f40 74 65 73 20 61 20 70 65 6e 64 69 6e 67 20 6c 6f tes a pending lo
f5f50 63 6b 20 68 61 73 20 62 65 65 6e 20 6f 62 74 61 ck has been obta
f5f60 69 6e 65 64 20 2a 2f 0a 20 20 42 4f 4f 4c 20 62 ined */. BOOL b
f5f70 52 65 73 65 72 76 65 64 3b 20 20 20 20 20 2f 2a Reserved; /*
f5f80 20 49 6e 64 69 63 61 74 65 73 20 61 20 72 65 73 Indicates a res
f5f90 65 72 76 65 64 20 6c 6f 63 6b 20 68 61 73 20 62 erved lock has b
f5fa0 65 65 6e 20 6f 62 74 61 69 6e 65 64 20 2a 2f 0a een obtained */.
f5fb0 20 20 42 4f 4f 4c 20 62 45 78 63 6c 75 73 69 76 BOOL bExclusiv
f5fc0 65 3b 20 20 20 20 2f 2a 20 49 6e 64 69 63 61 74 e; /* Indicat
f5fd0 65 73 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 es an exclusive
f5fe0 6c 6f 63 6b 20 68 61 73 20 62 65 65 6e 20 6f 62 lock has been ob
f5ff0 74 61 69 6e 65 64 20 2a 2f 0a 7d 20 77 69 6e 63 tained */.} winc
f6000 65 4c 6f 63 6b 3b 0a 23 65 6e 64 69 66 0a 0a 2f eLock;.#endif../
f6010 2a 0a 2a 2a 20 54 68 65 20 77 69 6e 46 69 6c 65 *.** The winFile
f6020 20 73 74 72 75 63 74 75 72 65 20 69 73 20 61 20 structure is a
f6030 73 75 62 63 6c 61 73 73 20 6f 66 20 73 71 6c 69 subclass of sqli
f6040 74 65 33 5f 66 69 6c 65 2a 20 73 70 65 63 69 66 te3_file* specif
f6050 69 63 20 74 6f 20 74 68 65 20 77 69 6e 33 32 0a ic to the win32.
f6060 2a 2a 20 70 6f 72 74 61 62 69 6c 69 74 79 20 6c ** portability l
f6070 61 79 65 72 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 ayer..*/.typedef
f6080 20 73 74 72 75 63 74 20 77 69 6e 46 69 6c 65 20 struct winFile
f6090 77 69 6e 46 69 6c 65 3b 0a 73 74 72 75 63 74 20 winFile;.struct
f60a0 77 69 6e 46 69 6c 65 20 7b 0a 20 20 63 6f 6e 73 winFile {. cons
f60b0 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 t sqlite3_io_met
f60c0 68 6f 64 73 20 2a 70 4d 65 74 68 6f 64 3b 2f 2a hods *pMethod;/*
f60d0 20 4d 75 73 74 20 62 65 20 66 69 72 73 74 20 2a Must be first *
f60e0 2f 0a 20 20 48 41 4e 44 4c 45 20 68 3b 20 20 20 /. HANDLE h;
f60f0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 /* H
f6100 61 6e 64 6c 65 20 66 6f 72 20 61 63 63 65 73 73 andle for access
f6110 69 6e 67 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a ing the file */.
f6120 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 unsigned char
f6130 6c 6f 63 6b 74 79 70 65 3b 20 2f 2a 20 54 79 70 locktype; /* Typ
f6140 65 20 6f 66 20 6c 6f 63 6b 20 63 75 72 72 65 6e e of lock curren
f6150 74 6c 79 20 68 65 6c 64 20 6f 6e 20 74 68 69 73 tly held on this
f6160 20 66 69 6c 65 20 2a 2f 0a 20 20 73 68 6f 72 74 file */. short
f6170 20 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 65 3b sharedLockByte;
f6180 20 20 20 2f 2a 20 52 61 6e 64 6f 6d 6c 79 20 63 /* Randomly c
f6190 68 6f 73 65 6e 20 62 79 74 65 20 75 73 65 64 20 hosen byte used
f61a0 61 73 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b as a shared lock
f61b0 20 2a 2f 0a 20 20 44 57 4f 52 44 20 6c 61 73 74 */. DWORD last
f61c0 45 72 72 6e 6f 3b 20 20 20 20 20 20 20 20 2f 2a Errno; /*
f61d0 20 54 68 65 20 57 69 6e 64 6f 77 73 20 65 72 72 The Windows err
f61e0 6e 6f 20 66 72 6f 6d 20 74 68 65 20 6c 61 73 74 no from the last
f61f0 20 49 2f 4f 20 65 72 72 6f 72 20 2a 2f 0a 20 20 I/O error */.
f6200 44 57 4f 52 44 20 73 65 63 74 6f 72 53 69 7a 65 DWORD sectorSize
f6210 3b 20 20 20 20 20 20 20 2f 2a 20 53 65 63 74 6f ; /* Secto
f6220 72 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 65 r size of the de
f6230 76 69 63 65 20 66 69 6c 65 20 69 73 20 6f 6e 20 vice file is on
f6240 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 */.#if SQLITE_OS
f6250 5f 57 49 4e 43 45 0a 20 20 57 43 48 41 52 20 2a _WINCE. WCHAR *
f6260 7a 44 65 6c 65 74 65 4f 6e 43 6c 6f 73 65 3b 20 zDeleteOnClose;
f6270 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 66 69 6c 65 /* Name of file
f6280 20 74 6f 20 64 65 6c 65 74 65 20 77 68 65 6e 20 to delete when
f6290 63 6c 6f 73 69 6e 67 20 2a 2f 0a 20 20 48 41 4e closing */. HAN
f62a0 44 4c 45 20 68 4d 75 74 65 78 3b 20 20 20 20 20 DLE hMutex;
f62b0 20 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 75 73 /* Mutex us
f62c0 65 64 20 74 6f 20 63 6f 6e 74 72 6f 6c 20 61 63 ed to control ac
f62d0 63 65 73 73 20 74 6f 20 73 68 61 72 65 64 20 6c cess to shared l
f62e0 6f 63 6b 20 2a 2f 20 20 0a 20 20 48 41 4e 44 4c ock */ . HANDL
f62f0 45 20 68 53 68 61 72 65 64 3b 20 20 20 20 20 20 E hShared;
f6300 20 20 20 2f 2a 20 53 68 61 72 65 64 20 6d 65 6d /* Shared mem
f6310 6f 72 79 20 73 65 67 6d 65 6e 74 20 75 73 65 64 ory segment used
f6320 20 66 6f 72 20 6c 6f 63 6b 69 6e 67 20 2a 2f 0a for locking */.
f6330 20 20 77 69 6e 63 65 4c 6f 63 6b 20 6c 6f 63 61 winceLock loca
f6340 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 63 l; /* Loc
f6350 6b 73 20 6f 62 74 61 69 6e 65 64 20 62 79 20 74 ks obtained by t
f6360 68 69 73 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 his instance of
f6370 77 69 6e 46 69 6c 65 20 2a 2f 0a 20 20 77 69 6e winFile */. win
f6380 63 65 4c 6f 63 6b 20 2a 73 68 61 72 65 64 3b 20 ceLock *shared;
f6390 20 20 20 20 20 2f 2a 20 47 6c 6f 62 61 6c 20 73 /* Global s
f63a0 68 61 72 65 64 20 6c 6f 63 6b 20 6d 65 6d 6f 72 hared lock memor
f63b0 79 20 66 6f 72 20 74 68 65 20 66 69 6c 65 20 20 y for the file
f63c0 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a */.#endif.};../*
f63d0 0a 2a 2a 20 46 6f 72 77 61 72 64 20 70 72 6f 74 .** Forward prot
f63e0 6f 74 79 70 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 otypes..*/.stati
f63f0 63 20 69 6e 74 20 67 65 74 53 65 63 74 6f 72 53 c int getSectorS
f6400 69 7a 65 28 0a 20 20 20 20 73 71 6c 69 74 65 33 ize(. sqlite3
f6410 5f 76 66 73 20 2a 70 56 66 73 2c 0a 20 20 20 20 _vfs *pVfs,.
f6420 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 52 65 6c const char *zRel
f6430 61 74 69 76 65 20 20 20 20 20 2f 2a 20 55 54 46 ative /* UTF
f6440 2d 38 20 66 69 6c 65 20 6e 61 6d 65 20 2a 2f 0a -8 file name */.
f6450 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f );../*.** The fo
f6460 6c 6c 6f 77 69 6e 67 20 76 61 72 69 61 62 6c 65 llowing variable
f6470 20 69 73 20 28 6e 6f 72 6d 61 6c 6c 79 29 20 73 is (normally) s
f6480 65 74 20 6f 6e 63 65 20 61 6e 64 20 6e 65 76 65 et once and neve
f6490 72 20 63 68 61 6e 67 65 73 0a 2a 2a 20 74 68 65 r changes.** the
f64a0 72 65 61 66 74 65 72 2e 20 20 49 74 20 72 65 63 reafter. It rec
f64b0 6f 72 64 73 20 77 68 65 74 68 65 72 20 74 68 65 ords whether the
f64c0 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 operating syste
f64d0 6d 20 69 73 20 57 69 6e 39 35 0a 2a 2a 20 6f 72 m is Win95.** or
f64e0 20 57 69 6e 4e 54 2e 0a 2a 2a 0a 2a 2a 20 30 3a WinNT..**.** 0:
f64f0 20 20 20 4f 70 65 72 61 74 69 6e 67 20 73 79 73 Operating sys
f6500 74 65 6d 20 75 6e 6b 6e 6f 77 6e 2e 0a 2a 2a 20 tem unknown..**
f6510 31 3a 20 20 20 4f 70 65 72 61 74 69 6e 67 20 73 1: Operating s
f6520 79 73 74 65 6d 20 69 73 20 57 69 6e 39 35 2e 0a ystem is Win95..
f6530 2a 2a 20 32 3a 20 20 20 4f 70 65 72 61 74 69 6e ** 2: Operatin
f6540 67 20 73 79 73 74 65 6d 20 69 73 20 57 69 6e 4e g system is WinN
f6550 54 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 6f 72 64 65 T..**.** In orde
f6560 72 20 74 6f 20 66 61 63 69 6c 69 74 61 74 65 20 r to facilitate
f6570 74 65 73 74 69 6e 67 20 6f 6e 20 61 20 57 69 6e testing on a Win
f6580 4e 54 20 73 79 73 74 65 6d 2c 20 74 68 65 20 74 NT system, the t
f6590 65 73 74 20 66 69 78 74 75 72 65 0a 2a 2a 20 63 est fixture.** c
f65a0 61 6e 20 6d 61 6e 75 61 6c 6c 79 20 73 65 74 20 an manually set
f65b0 74 68 69 73 20 76 61 6c 75 65 20 74 6f 20 31 20 this value to 1
f65c0 74 6f 20 65 6d 75 6c 61 74 65 20 57 69 6e 39 38 to emulate Win98
f65d0 20 62 65 68 61 76 69 6f 72 2e 0a 2a 2f 0a 23 69 behavior..*/.#i
f65e0 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 fdef SQLITE_TEST
f65f0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
f6600 73 71 6c 69 74 65 33 5f 6f 73 5f 74 79 70 65 20 sqlite3_os_type
f6610 3d 20 30 3b 0a 23 65 6c 73 65 0a 73 74 61 74 69 = 0;.#else.stati
f6620 63 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73 c int sqlite3_os
f6630 5f 74 79 70 65 20 3d 20 30 3b 0a 23 65 6e 64 69 _type = 0;.#endi
f6640 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 f../*.** Return
f6650 74 72 75 65 20 28 6e 6f 6e 2d 7a 65 72 6f 29 20 true (non-zero)
f6660 69 66 20 77 65 20 61 72 65 20 72 75 6e 6e 69 6e if we are runnin
f6670 67 20 75 6e 64 65 72 20 57 69 6e 4e 54 2c 20 57 g under WinNT, W
f6680 69 6e 32 4b 2c 20 57 69 6e 58 50 2c 0a 2a 2a 20 in2K, WinXP,.**
f6690 6f 72 20 57 69 6e 43 45 2e 20 20 52 65 74 75 72 or WinCE. Retur
f66a0 6e 20 66 61 6c 73 65 20 28 7a 65 72 6f 29 20 66 n false (zero) f
f66b0 6f 72 20 57 69 6e 39 35 2c 20 57 69 6e 39 38 2c or Win95, Win98,
f66c0 20 6f 72 20 57 69 6e 4d 45 2e 0a 2a 2a 0a 2a 2a or WinME..**.**
f66d0 20 48 65 72 65 20 69 73 20 61 6e 20 69 6e 74 65 Here is an inte
f66e0 72 65 73 74 69 6e 67 20 6f 62 73 65 72 76 61 74 resting observat
f66f0 69 6f 6e 3a 20 20 57 69 6e 39 35 2c 20 57 69 6e ion: Win95, Win
f6700 39 38 2c 20 61 6e 64 20 57 69 6e 4d 45 20 6c 61 98, and WinME la
f6710 63 6b 0a 2a 2a 20 74 68 65 20 4c 6f 63 6b 46 69 ck.** the LockFi
f6720 6c 65 45 78 28 29 20 41 50 49 2e 20 20 42 75 74 leEx() API. But
f6730 20 77 65 20 63 61 6e 20 73 74 69 6c 6c 20 73 74 we can still st
f6740 61 74 69 63 61 6c 6c 79 20 6c 69 6e 6b 20 61 67 atically link ag
f6750 61 69 6e 73 74 20 74 68 61 74 0a 2a 2a 20 41 50 ainst that.** AP
f6760 49 20 61 73 20 6c 6f 6e 67 20 61 73 20 77 65 20 I as long as we
f6770 64 6f 6e 27 74 20 63 61 6c 6c 20 69 74 20 77 68 don't call it wh
f6780 65 6e 20 72 75 6e 6e 69 6e 67 20 57 69 6e 39 35 en running Win95
f6790 2f 39 38 2f 4d 45 2e 20 20 41 20 63 61 6c 6c 20 /98/ME. A call
f67a0 74 6f 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 to.** this routi
f67b0 6e 65 20 69 73 20 75 73 65 64 20 74 6f 20 64 65 ne is used to de
f67c0 74 65 72 6d 69 6e 65 20 69 66 20 74 68 65 20 68 termine if the h
f67d0 6f 73 74 20 69 73 20 57 69 6e 39 35 2f 39 38 2f ost is Win95/98/
f67e0 4d 45 20 6f 72 0a 2a 2a 20 57 69 6e 4e 54 2f 32 ME or.** WinNT/2
f67f0 4b 2f 58 50 20 73 6f 20 74 68 61 74 20 77 65 20 K/XP so that we
f6800 77 69 6c 6c 20 6b 6e 6f 77 20 77 68 65 74 68 65 will know whethe
f6810 72 20 6f 72 20 6e 6f 74 20 77 65 20 63 61 6e 20 r or not we can
f6820 73 61 66 65 6c 79 20 63 61 6c 6c 0a 2a 2a 20 74 safely call.** t
f6830 68 65 20 4c 6f 63 6b 46 69 6c 65 45 78 28 29 20 he LockFileEx()
f6840 41 50 49 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 API..*/.#if SQLI
f6850 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 23 20 64 65 TE_OS_WINCE.# de
f6860 66 69 6e 65 20 69 73 4e 54 28 29 20 20 28 31 29 fine isNT() (1)
f6870 0a 23 65 6c 73 65 0a 20 20 73 74 61 74 69 63 20 .#else. static
f6880 69 6e 74 20 69 73 4e 54 28 76 6f 69 64 29 7b 0a int isNT(void){.
f6890 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f if( sqlite3_
f68a0 6f 73 5f 74 79 70 65 3d 3d 30 20 29 7b 0a 20 20 os_type==0 ){.
f68b0 20 20 20 20 4f 53 56 45 52 53 49 4f 4e 49 4e 46 OSVERSIONINF
f68c0 4f 20 73 49 6e 66 6f 3b 0a 20 20 20 20 20 20 73 O sInfo;. s
f68d0 49 6e 66 6f 2e 64 77 4f 53 56 65 72 73 69 6f 6e Info.dwOSVersion
f68e0 49 6e 66 6f 53 69 7a 65 20 3d 20 73 69 7a 65 6f InfoSize = sizeo
f68f0 66 28 73 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 f(sInfo);.
f6900 47 65 74 56 65 72 73 69 6f 6e 45 78 28 26 73 49 GetVersionEx(&sI
f6910 6e 66 6f 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 nfo);. sqli
f6920 74 65 33 5f 6f 73 5f 74 79 70 65 20 3d 20 73 49 te3_os_type = sI
f6930 6e 66 6f 2e 64 77 50 6c 61 74 66 6f 72 6d 49 64 nfo.dwPlatformId
f6940 3d 3d 56 45 52 5f 50 4c 41 54 46 4f 52 4d 5f 57 ==VER_PLATFORM_W
f6950 49 4e 33 32 5f 4e 54 20 3f 20 32 20 3a 20 31 3b IN32_NT ? 2 : 1;
f6960 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 . }. retur
f6970 6e 20 73 71 6c 69 74 65 33 5f 6f 73 5f 74 79 70 n sqlite3_os_typ
f6980 65 3d 3d 32 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 e==2;. }.#endif
f6990 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 /* SQLITE_OS_WI
f69a0 4e 43 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f NCE */../*.** Co
f69b0 6e 76 65 72 74 20 61 20 55 54 46 2d 38 20 73 74 nvert a UTF-8 st
f69c0 72 69 6e 67 20 74 6f 20 6d 69 63 72 6f 73 6f 66 ring to microsof
f69d0 74 20 75 6e 69 63 6f 64 65 20 28 55 54 46 2d 31 t unicode (UTF-1
f69e0 36 3f 29 2e 20 0a 2a 2a 0a 2a 2a 20 53 70 61 63 6?). .**.** Spac
f69f0 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 65 e to hold the re
f6a00 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20 69 73 turned string is
f6a10 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d obtained from m
f6a20 61 6c 6c 6f 63 2e 0a 2a 2f 0a 73 74 61 74 69 63 alloc..*/.static
f6a30 20 57 43 48 41 52 20 2a 75 74 66 38 54 6f 55 6e WCHAR *utf8ToUn
f6a40 69 63 6f 64 65 28 63 6f 6e 73 74 20 63 68 61 72 icode(const char
f6a50 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 *zFilename){.
f6a60 69 6e 74 20 6e 43 68 61 72 3b 0a 20 20 57 43 48 int nChar;. WCH
f6a70 41 52 20 2a 7a 57 69 64 65 46 69 6c 65 6e 61 6d AR *zWideFilenam
f6a80 65 3b 0a 0a 20 20 6e 43 68 61 72 20 3d 20 4d 75 e;.. nChar = Mu
f6a90 6c 74 69 42 79 74 65 54 6f 57 69 64 65 43 68 61 ltiByteToWideCha
f6aa0 72 28 43 50 5f 55 54 46 38 2c 20 30 2c 20 7a 46 r(CP_UTF8, 0, zF
f6ab0 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 4e 55 4c ilename, -1, NUL
f6ac0 4c 2c 20 30 29 3b 0a 20 20 7a 57 69 64 65 46 69 L, 0);. zWideFi
f6ad0 6c 65 6e 61 6d 65 20 3d 20 6d 61 6c 6c 6f 63 28 lename = malloc(
f6ae0 20 6e 43 68 61 72 2a 73 69 7a 65 6f 66 28 7a 57 nChar*sizeof(zW
f6af0 69 64 65 46 69 6c 65 6e 61 6d 65 5b 30 5d 29 20 ideFilename[0])
f6b00 29 3b 0a 20 20 69 66 28 20 7a 57 69 64 65 46 69 );. if( zWideFi
f6b10 6c 65 6e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 lename==0 ){.
f6b20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 return 0;. }.
f6b30 20 6e 43 68 61 72 20 3d 20 4d 75 6c 74 69 42 79 nChar = MultiBy
f6b40 74 65 54 6f 57 69 64 65 43 68 61 72 28 43 50 5f teToWideChar(CP_
f6b50 55 54 46 38 2c 20 30 2c 20 7a 46 69 6c 65 6e 61 UTF8, 0, zFilena
f6b60 6d 65 2c 20 2d 31 2c 20 7a 57 69 64 65 46 69 6c me, -1, zWideFil
f6b70 65 6e 61 6d 65 2c 20 6e 43 68 61 72 29 3b 0a 20 ename, nChar);.
f6b80 20 69 66 28 20 6e 43 68 61 72 3d 3d 30 20 29 7b if( nChar==0 ){
f6b90 0a 20 20 20 20 66 72 65 65 28 7a 57 69 64 65 46 . free(zWideF
f6ba0 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 7a 57 ilename);. zW
f6bb0 69 64 65 46 69 6c 65 6e 61 6d 65 20 3d 20 30 3b ideFilename = 0;
f6bc0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 57 . }. return zW
f6bd0 69 64 65 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a ideFilename;.}..
f6be0 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 6d 69 /*.** Convert mi
f6bf0 63 72 6f 73 6f 66 74 20 75 6e 69 63 6f 64 65 20 crosoft unicode
f6c00 74 6f 20 55 54 46 2d 38 2e 20 20 53 70 61 63 65 to UTF-8. Space
f6c10 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 65 74 to hold the ret
f6c20 75 72 6e 65 64 20 73 74 72 69 6e 67 20 69 73 0a urned string is.
f6c30 2a 2a 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d ** obtained from
f6c40 20 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 malloc()..*/.st
f6c50 61 74 69 63 20 63 68 61 72 20 2a 75 6e 69 63 6f atic char *unico
f6c60 64 65 54 6f 55 74 66 38 28 63 6f 6e 73 74 20 57 deToUtf8(const W
f6c70 43 48 41 52 20 2a 7a 57 69 64 65 46 69 6c 65 6e CHAR *zWideFilen
f6c80 61 6d 65 29 7b 0a 20 20 69 6e 74 20 6e 42 79 74 ame){. int nByt
f6c90 65 3b 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c 65 e;. char *zFile
f6ca0 6e 61 6d 65 3b 0a 0a 20 20 6e 42 79 74 65 20 3d name;.. nByte =
f6cb0 20 57 69 64 65 43 68 61 72 54 6f 4d 75 6c 74 69 WideCharToMulti
f6cc0 42 79 74 65 28 43 50 5f 55 54 46 38 2c 20 30 2c Byte(CP_UTF8, 0,
f6cd0 20 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 2c 20 zWideFilename,
f6ce0 2d 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30 29 3b -1, 0, 0, 0, 0);
f6cf0 0a 20 20 7a 46 69 6c 65 6e 61 6d 65 20 3d 20 6d . zFilename = m
f6d00 61 6c 6c 6f 63 28 20 6e 42 79 74 65 20 29 3b 0a alloc( nByte );.
f6d10 20 20 69 66 28 20 7a 46 69 6c 65 6e 61 6d 65 3d if( zFilename=
f6d20 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e =0 ){. return
f6d30 20 30 3b 0a 20 20 7d 0a 20 20 6e 42 79 74 65 20 0;. }. nByte
f6d40 3d 20 57 69 64 65 43 68 61 72 54 6f 4d 75 6c 74 = WideCharToMult
f6d50 69 42 79 74 65 28 43 50 5f 55 54 46 38 2c 20 30 iByte(CP_UTF8, 0
f6d60 2c 20 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 2c , zWideFilename,
f6d70 20 2d 31 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 -1, zFilename,
f6d80 6e 42 79 74 65 2c 0a 20 20 20 20 20 20 20 20 20 nByte,.
f6d90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f6da0 20 20 20 20 20 30 2c 20 30 29 3b 0a 20 20 69 66 0, 0);. if
f6db0 28 20 6e 42 79 74 65 20 3d 3d 20 30 20 29 7b 0a ( nByte == 0 ){.
f6dc0 20 20 20 20 66 72 65 65 28 7a 46 69 6c 65 6e 61 free(zFilena
f6dd0 6d 65 29 3b 0a 20 20 20 20 7a 46 69 6c 65 6e 61 me);. zFilena
f6de0 6d 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 me = 0;. }. re
f6df0 74 75 72 6e 20 7a 46 69 6c 65 6e 61 6d 65 3b 0a turn zFilename;.
f6e00 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 }../*.** Convert
f6e10 20 61 6e 20 61 6e 73 69 20 73 74 72 69 6e 67 20 an ansi string
f6e20 74 6f 20 6d 69 63 72 6f 73 6f 66 74 20 75 6e 69 to microsoft uni
f6e30 63 6f 64 65 2c 20 62 61 73 65 64 20 6f 6e 20 74 code, based on t
f6e40 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 63 6f he.** current co
f6e50 64 65 70 61 67 65 20 73 65 74 74 69 6e 67 73 20 depage settings
f6e60 66 6f 72 20 66 69 6c 65 20 61 70 69 73 2e 0a 2a for file apis..*
f6e70 2a 20 0a 2a 2a 20 53 70 61 63 65 20 74 6f 20 68 * .** Space to h
f6e80 6f 6c 64 20 74 68 65 20 72 65 74 75 72 6e 65 64 old the returned
f6e90 20 73 74 72 69 6e 67 20 69 73 20 6f 62 74 61 69 string is obtai
f6ea0 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 6d 61 6c 6c ned.** from mall
f6eb0 6f 63 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 57 43 oc..*/.static WC
f6ec0 48 41 52 20 2a 6d 62 63 73 54 6f 55 6e 69 63 6f HAR *mbcsToUnico
f6ed0 64 65 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a de(const char *z
f6ee0 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 69 6e 74 Filename){. int
f6ef0 20 6e 42 79 74 65 3b 0a 20 20 57 43 48 41 52 20 nByte;. WCHAR
f6f00 2a 7a 4d 62 63 73 46 69 6c 65 6e 61 6d 65 3b 0a *zMbcsFilename;.
f6f10 20 20 69 6e 74 20 63 6f 64 65 70 61 67 65 20 3d int codepage =
f6f20 20 41 72 65 46 69 6c 65 41 70 69 73 41 4e 53 49 AreFileApisANSI
f6f30 28 29 20 3f 20 43 50 5f 41 43 50 20 3a 20 43 50 () ? CP_ACP : CP
f6f40 5f 4f 45 4d 43 50 3b 0a 0a 20 20 6e 42 79 74 65 _OEMCP;.. nByte
f6f50 20 3d 20 4d 75 6c 74 69 42 79 74 65 54 6f 57 69 = MultiByteToWi
f6f60 64 65 43 68 61 72 28 63 6f 64 65 70 61 67 65 2c deChar(codepage,
f6f70 20 30 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 2d 0, zFilename, -
f6f80 31 2c 20 4e 55 4c 4c 2c 30 29 2a 73 69 7a 65 6f 1, NULL,0)*sizeo
f6f90 66 28 57 43 48 41 52 29 3b 0a 20 20 7a 4d 62 63 f(WCHAR);. zMbc
f6fa0 73 46 69 6c 65 6e 61 6d 65 20 3d 20 6d 61 6c 6c sFilename = mall
f6fb0 6f 63 28 20 6e 42 79 74 65 2a 73 69 7a 65 6f 66 oc( nByte*sizeof
f6fc0 28 7a 4d 62 63 73 46 69 6c 65 6e 61 6d 65 5b 30 (zMbcsFilename[0
f6fd0 5d 29 20 29 3b 0a 20 20 69 66 28 20 7a 4d 62 63 ]) );. if( zMbc
f6fe0 73 46 69 6c 65 6e 61 6d 65 3d 3d 30 20 29 7b 0a sFilename==0 ){.
f6ff0 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 return 0;.
f7000 7d 0a 20 20 6e 42 79 74 65 20 3d 20 4d 75 6c 74 }. nByte = Mult
f7010 69 42 79 74 65 54 6f 57 69 64 65 43 68 61 72 28 iByteToWideChar(
f7020 63 6f 64 65 70 61 67 65 2c 20 30 2c 20 7a 46 69 codepage, 0, zFi
f7030 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 7a 4d 62 63 lename, -1, zMbc
f7040 73 46 69 6c 65 6e 61 6d 65 2c 20 6e 42 79 74 65 sFilename, nByte
f7050 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 3d 3d );. if( nByte==
f7060 30 20 29 7b 0a 20 20 20 20 66 72 65 65 28 7a 4d 0 ){. free(zM
f7070 62 63 73 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 bcsFilename);.
f7080 20 20 7a 4d 62 63 73 46 69 6c 65 6e 61 6d 65 20 zMbcsFilename
f7090 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 = 0;. }. retur
f70a0 6e 20 7a 4d 62 63 73 46 69 6c 65 6e 61 6d 65 3b n zMbcsFilename;
f70b0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 .}../*.** Conver
f70c0 74 20 6d 69 63 72 6f 73 6f 66 74 20 75 6e 69 63 t microsoft unic
f70d0 6f 64 65 20 74 6f 20 6d 75 6c 74 69 62 79 74 65 ode to multibyte
f70e0 20 63 68 61 72 61 63 74 65 72 20 73 74 72 69 6e character strin
f70f0 67 2c 20 62 61 73 65 64 20 6f 6e 20 74 68 65 0a g, based on the.
f7100 2a 2a 20 75 73 65 72 27 73 20 41 6e 73 69 20 63 ** user's Ansi c
f7110 6f 64 65 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 53 odepage..**.** S
f7120 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 pace to hold the
f7130 20 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e 67 returned string
f7140 20 69 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f is obtained fro
f7150 6d 0a 2a 2a 20 6d 61 6c 6c 6f 63 28 29 2e 0a 2a m.** malloc()..*
f7160 2f 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a 75 /.static char *u
f7170 6e 69 63 6f 64 65 54 6f 4d 62 63 73 28 63 6f 6e nicodeToMbcs(con
f7180 73 74 20 57 43 48 41 52 20 2a 7a 57 69 64 65 46 st WCHAR *zWideF
f7190 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 69 6e 74 20 ilename){. int
f71a0 6e 42 79 74 65 3b 0a 20 20 63 68 61 72 20 2a 7a nByte;. char *z
f71b0 46 69 6c 65 6e 61 6d 65 3b 0a 20 20 69 6e 74 20 Filename;. int
f71c0 63 6f 64 65 70 61 67 65 20 3d 20 41 72 65 46 69 codepage = AreFi
f71d0 6c 65 41 70 69 73 41 4e 53 49 28 29 20 3f 20 43 leApisANSI() ? C
f71e0 50 5f 41 43 50 20 3a 20 43 50 5f 4f 45 4d 43 50 P_ACP : CP_OEMCP
f71f0 3b 0a 0a 20 20 6e 42 79 74 65 20 3d 20 57 69 64 ;.. nByte = Wid
f7200 65 43 68 61 72 54 6f 4d 75 6c 74 69 42 79 74 65 eCharToMultiByte
f7210 28 63 6f 64 65 70 61 67 65 2c 20 30 2c 20 7a 57 (codepage, 0, zW
f7220 69 64 65 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c ideFilename, -1,
f7230 20 30 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 0, 0, 0, 0);.
f7240 7a 46 69 6c 65 6e 61 6d 65 20 3d 20 6d 61 6c 6c zFilename = mall
f7250 6f 63 28 20 6e 42 79 74 65 20 29 3b 0a 20 20 69 oc( nByte );. i
f7260 66 28 20 7a 46 69 6c 65 6e 61 6d 65 3d 3d 30 20 f( zFilename==0
f7270 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b ){. return 0;
f7280 0a 20 20 7d 0a 20 20 6e 42 79 74 65 20 3d 20 57 . }. nByte = W
f7290 69 64 65 43 68 61 72 54 6f 4d 75 6c 74 69 42 79 ideCharToMultiBy
f72a0 74 65 28 63 6f 64 65 70 61 67 65 2c 20 30 2c 20 te(codepage, 0,
f72b0 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 2c 20 2d zWideFilename, -
f72c0 31 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 6e 42 1, zFilename, nB
f72d0 79 74 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 yte,.
f72e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f72f0 20 20 20 30 2c 20 30 29 3b 0a 20 20 69 66 28 20 0, 0);. if(
f7300 6e 42 79 74 65 20 3d 3d 20 30 20 29 7b 0a 20 20 nByte == 0 ){.
f7310 20 20 66 72 65 65 28 7a 46 69 6c 65 6e 61 6d 65 free(zFilename
f7320 29 3b 0a 20 20 20 20 7a 46 69 6c 65 6e 61 6d 65 );. zFilename
f7330 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 = 0;. }. retu
f7340 72 6e 20 7a 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a rn zFilename;.}.
f7350 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 6d ./*.** Convert m
f7360 75 6c 74 69 62 79 74 65 20 63 68 61 72 61 63 74 ultibyte charact
f7370 65 72 20 73 74 72 69 6e 67 20 74 6f 20 55 54 46 er string to UTF
f7380 2d 38 2e 20 20 53 70 61 63 65 20 74 6f 20 68 6f -8. Space to ho
f7390 6c 64 20 74 68 65 0a 2a 2a 20 72 65 74 75 72 6e ld the.** return
f73a0 65 64 20 73 74 72 69 6e 67 20 69 73 20 6f 62 74 ed string is obt
f73b0 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f ained from mallo
f73c0 63 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 c()..*/.SQLITE_A
f73d0 50 49 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 PI char *sqlite3
f73e0 5f 77 69 6e 33 32 5f 6d 62 63 73 5f 74 6f 5f 75 _win32_mbcs_to_u
f73f0 74 66 38 28 63 6f 6e 73 74 20 63 68 61 72 20 2a tf8(const char *
f7400 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 63 68 zFilename){. ch
f7410 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 55 74 66 ar *zFilenameUtf
f7420 38 3b 0a 20 20 57 43 48 41 52 20 2a 7a 54 6d 70 8;. WCHAR *zTmp
f7430 57 69 64 65 3b 0a 0a 20 20 7a 54 6d 70 57 69 64 Wide;.. zTmpWid
f7440 65 20 3d 20 6d 62 63 73 54 6f 55 6e 69 63 6f 64 e = mbcsToUnicod
f7450 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 e(zFilename);.
f7460 69 66 28 20 7a 54 6d 70 57 69 64 65 3d 3d 30 20 if( zTmpWide==0
f7470 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b ){. return 0;
f7480 0a 20 20 7d 0a 20 20 7a 46 69 6c 65 6e 61 6d 65 . }. zFilename
f7490 55 74 66 38 20 3d 20 75 6e 69 63 6f 64 65 54 6f Utf8 = unicodeTo
f74a0 55 74 66 38 28 7a 54 6d 70 57 69 64 65 29 3b 0a Utf8(zTmpWide);.
f74b0 20 20 66 72 65 65 28 7a 54 6d 70 57 69 64 65 29 free(zTmpWide)
f74c0 3b 0a 20 20 72 65 74 75 72 6e 20 7a 46 69 6c 65 ;. return zFile
f74d0 6e 61 6d 65 55 74 66 38 3b 0a 7d 0a 0a 2f 2a 0a nameUtf8;.}../*.
f74e0 2a 2a 20 43 6f 6e 76 65 72 74 20 55 54 46 2d 38 ** Convert UTF-8
f74f0 20 74 6f 20 6d 75 6c 74 69 62 79 74 65 20 63 68 to multibyte ch
f7500 61 72 61 63 74 65 72 20 73 74 72 69 6e 67 2e 20 aracter string.
f7510 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 Space to hold t
f7520 68 65 20 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 he .** returned
f7530 73 74 72 69 6e 67 20 69 73 20 6f 62 74 61 69 6e string is obtain
f7540 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 28 29 ed from malloc()
f7550 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72 ..*/.static char
f7560 20 2a 75 74 66 38 54 6f 4d 62 63 73 28 63 6f 6e *utf8ToMbcs(con
f7570 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 st char *zFilena
f7580 6d 65 29 7b 0a 20 20 63 68 61 72 20 2a 7a 46 69 me){. char *zFi
f7590 6c 65 6e 61 6d 65 4d 62 63 73 3b 0a 20 20 57 43 lenameMbcs;. WC
f75a0 48 41 52 20 2a 7a 54 6d 70 57 69 64 65 3b 0a 0a HAR *zTmpWide;..
f75b0 20 20 7a 54 6d 70 57 69 64 65 20 3d 20 75 74 66 zTmpWide = utf
f75c0 38 54 6f 55 6e 69 63 6f 64 65 28 7a 46 69 6c 65 8ToUnicode(zFile
f75d0 6e 61 6d 65 29 3b 0a 20 20 69 66 28 20 7a 54 6d name);. if( zTm
f75e0 70 57 69 64 65 3d 3d 30 20 29 7b 0a 20 20 20 20 pWide==0 ){.
f75f0 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 return 0;. }.
f7600 7a 46 69 6c 65 6e 61 6d 65 4d 62 63 73 20 3d 20 zFilenameMbcs =
f7610 75 6e 69 63 6f 64 65 54 6f 4d 62 63 73 28 7a 54 unicodeToMbcs(zT
f7620 6d 70 57 69 64 65 29 3b 0a 20 20 66 72 65 65 28 mpWide);. free(
f7630 7a 54 6d 70 57 69 64 65 29 3b 0a 20 20 72 65 74 zTmpWide);. ret
f7640 75 72 6e 20 7a 46 69 6c 65 6e 61 6d 65 4d 62 63 urn zFilenameMbc
f7650 73 3b 0a 7d 0a 0a 23 69 66 20 53 51 4c 49 54 45 s;.}..#if SQLITE
f7660 5f 4f 53 5f 57 49 4e 43 45 0a 2f 2a 2a 2a 2a 2a _OS_WINCE./*****
f7670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f7680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f7690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f76a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f76b0 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 65 63 ****.** This sec
f76c0 74 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 63 6f tion contains co
f76d0 64 65 20 66 6f 72 20 57 69 6e 43 45 20 6f 6e 6c de for WinCE onl
f76e0 79 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a 20 57 69 6e 64 y..*/./*.** Wind
f76f0 6f 77 73 43 45 20 64 6f 65 73 20 6e 6f 74 20 68 owsCE does not h
f7700 61 76 65 20 61 20 6c 6f 63 61 6c 74 69 6d 65 28 ave a localtime(
f7710 29 20 66 75 6e 63 74 69 6f 6e 2e 20 20 53 6f 20 ) function. So
f7720 63 72 65 61 74 65 20 61 0a 2a 2a 20 73 75 62 73 create a.** subs
f7730 74 69 74 75 74 65 2e 0a 2a 2f 0a 73 74 72 75 63 titute..*/.struc
f7740 74 20 74 6d 20 2a 5f 5f 63 64 65 63 6c 20 6c 6f t tm *__cdecl lo
f7750 63 61 6c 74 69 6d 65 28 63 6f 6e 73 74 20 74 69 caltime(const ti
f7760 6d 65 5f 74 20 2a 74 29 0a 7b 0a 20 20 73 74 61 me_t *t).{. sta
f7770 74 69 63 20 73 74 72 75 63 74 20 74 6d 20 79 3b tic struct tm y;
f7780 0a 20 20 46 49 4c 45 54 49 4d 45 20 75 54 6d 2c . FILETIME uTm,
f7790 20 6c 54 6d 3b 0a 20 20 53 59 53 54 45 4d 54 49 lTm;. SYSTEMTI
f77a0 4d 45 20 70 54 6d 3b 0a 20 20 73 71 6c 69 74 65 ME pTm;. sqlite
f77b0 33 5f 69 6e 74 36 34 20 74 36 34 3b 0a 20 20 74 3_int64 t64;. t
f77c0 36 34 20 3d 20 2a 74 3b 0a 20 20 74 36 34 20 3d 64 = *t;. t64 =
f77d0 20 28 74 36 34 20 2b 20 31 31 36 34 34 34 37 33 (t64 + 11644473
f77e0 36 30 30 29 2a 31 30 30 30 30 30 30 30 3b 0a 20 600)*10000000;.
f77f0 20 75 54 6d 2e 64 77 4c 6f 77 44 61 74 65 54 69 uTm.dwLowDateTi
f7800 6d 65 20 3d 20 28 44 57 4f 52 44 29 28 74 36 34 me = (DWORD)(t64
f7810 20 26 20 30 78 46 46 46 46 46 46 46 46 29 3b 0a & 0xFFFFFFFF);.
f7820 20 20 75 54 6d 2e 64 77 48 69 67 68 44 61 74 65 uTm.dwHighDate
f7830 54 69 6d 65 3d 20 28 44 57 4f 52 44 29 28 74 36 Time= (DWORD)(t6
f7840 34 20 3e 3e 20 33 32 29 3b 0a 20 20 46 69 6c 65 4 >> 32);. File
f7850 54 69 6d 65 54 6f 4c 6f 63 61 6c 46 69 6c 65 54 TimeToLocalFileT
f7860 69 6d 65 28 26 75 54 6d 2c 26 6c 54 6d 29 3b 0a ime(&uTm,&lTm);.
f7870 20 20 46 69 6c 65 54 69 6d 65 54 6f 53 79 73 74 FileTimeToSyst
f7880 65 6d 54 69 6d 65 28 26 6c 54 6d 2c 26 70 54 6d emTime(&lTm,&pTm
f7890 29 3b 0a 20 20 79 2e 74 6d 5f 79 65 61 72 20 3d );. y.tm_year =
f78a0 20 70 54 6d 2e 77 59 65 61 72 20 2d 20 31 39 30 pTm.wYear - 190
f78b0 30 3b 0a 20 20 79 2e 74 6d 5f 6d 6f 6e 20 3d 20 0;. y.tm_mon =
f78c0 70 54 6d 2e 77 4d 6f 6e 74 68 20 2d 20 31 3b 0a pTm.wMonth - 1;.
f78d0 20 20 79 2e 74 6d 5f 77 64 61 79 20 3d 20 70 54 y.tm_wday = pT
f78e0 6d 2e 77 44 61 79 4f 66 57 65 65 6b 3b 0a 20 20 m.wDayOfWeek;.
f78f0 79 2e 74 6d 5f 6d 64 61 79 20 3d 20 70 54 6d 2e y.tm_mday = pTm.
f7900 77 44 61 79 3b 0a 20 20 79 2e 74 6d 5f 68 6f 75 wDay;. y.tm_hou
f7910 72 20 3d 20 70 54 6d 2e 77 48 6f 75 72 3b 0a 20 r = pTm.wHour;.
f7920 20 79 2e 74 6d 5f 6d 69 6e 20 3d 20 70 54 6d 2e y.tm_min = pTm.
f7930 77 4d 69 6e 75 74 65 3b 0a 20 20 79 2e 74 6d 5f wMinute;. y.tm_
f7940 73 65 63 20 3d 20 70 54 6d 2e 77 53 65 63 6f 6e sec = pTm.wSecon
f7950 64 3b 0a 20 20 72 65 74 75 72 6e 20 26 79 3b 0a d;. return &y;.
f7960 7d 0a 0a 2f 2a 20 54 68 69 73 20 77 69 6c 6c 20 }../* This will
f7970 6e 65 76 65 72 20 62 65 20 63 61 6c 6c 65 64 2c never be called,
f7980 20 62 75 74 20 64 65 66 69 6e 65 64 20 74 6f 20 but defined to
f7990 6d 61 6b 65 20 74 68 65 20 63 6f 64 65 20 63 6f make the code co
f79a0 6d 70 69 6c 65 20 2a 2f 0a 23 64 65 66 69 6e 65 mpile */.#define
f79b0 20 47 65 74 54 65 6d 70 50 61 74 68 41 28 61 2c GetTempPathA(a,
f79c0 62 29 0a 0a 23 64 65 66 69 6e 65 20 4c 6f 63 6b b)..#define Lock
f79d0 46 69 6c 65 28 61 2c 62 2c 63 2c 64 2c 65 29 20 File(a,b,c,d,e)
f79e0 20 20 20 20 20 20 77 69 6e 63 65 4c 6f 63 6b 46 winceLockF
f79f0 69 6c 65 28 26 61 2c 20 62 2c 20 63 2c 20 64 2c ile(&a, b, c, d,
f7a00 20 65 29 0a 23 64 65 66 69 6e 65 20 55 6e 6c 6f e).#define Unlo
f7a10 63 6b 46 69 6c 65 28 61 2c 62 2c 63 2c 64 2c 65 ckFile(a,b,c,d,e
f7a20 29 20 20 20 20 20 77 69 6e 63 65 55 6e 6c 6f 63 ) winceUnloc
f7a30 6b 46 69 6c 65 28 26 61 2c 20 62 2c 20 63 2c 20 kFile(&a, b, c,
f7a40 64 2c 20 65 29 0a 23 64 65 66 69 6e 65 20 4c 6f d, e).#define Lo
f7a50 63 6b 46 69 6c 65 45 78 28 61 2c 62 2c 63 2c 64 ckFileEx(a,b,c,d
f7a60 2c 65 2c 66 29 20 20 20 77 69 6e 63 65 4c 6f 63 ,e,f) winceLoc
f7a70 6b 46 69 6c 65 45 78 28 26 61 2c 20 62 2c 20 63 kFileEx(&a, b, c
f7a80 2c 20 64 2c 20 65 2c 20 66 29 0a 0a 23 64 65 66 , d, e, f)..#def
f7a90 69 6e 65 20 48 41 4e 44 4c 45 5f 54 4f 5f 57 49 ine HANDLE_TO_WI
f7aa0 4e 46 49 4c 45 28 61 29 20 28 77 69 6e 46 69 6c NFILE(a) (winFil
f7ab0 65 2a 29 26 28 28 63 68 61 72 2a 29 61 29 5b 2d e*)&((char*)a)[-
f7ac0 28 69 6e 74 29 6f 66 66 73 65 74 6f 66 28 77 69 (int)offsetof(wi
f7ad0 6e 46 69 6c 65 2c 68 29 5d 0a 0a 2f 2a 0a 2a 2a nFile,h)]../*.**
f7ae0 20 41 63 71 75 69 72 65 20 61 20 6c 6f 63 6b 20 Acquire a lock
f7af0 6f 6e 20 74 68 65 20 68 61 6e 64 6c 65 20 68 0a on the handle h.
f7b00 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77 */.static void w
f7b10 69 6e 63 65 4d 75 74 65 78 41 63 71 75 69 72 65 inceMutexAcquire
f7b20 28 48 41 4e 44 4c 45 20 68 29 7b 0a 20 20 20 44 (HANDLE h){. D
f7b30 57 4f 52 44 20 64 77 45 72 72 3b 0a 20 20 20 64 WORD dwErr;. d
f7b40 6f 20 7b 0a 20 20 20 20 20 64 77 45 72 72 20 3d o {. dwErr =
f7b50 20 57 61 69 74 46 6f 72 53 69 6e 67 6c 65 4f 62 WaitForSingleOb
f7b60 6a 65 63 74 28 68 2c 20 49 4e 46 49 4e 49 54 45 ject(h, INFINITE
f7b70 29 3b 0a 20 20 20 7d 20 77 68 69 6c 65 20 28 64 );. } while (d
f7b80 77 45 72 72 20 21 3d 20 57 41 49 54 5f 4f 42 4a wErr != WAIT_OBJ
f7b90 45 43 54 5f 30 20 26 26 20 64 77 45 72 72 20 21 ECT_0 && dwErr !
f7ba0 3d 20 57 41 49 54 5f 41 42 41 4e 44 4f 4e 45 44 = WAIT_ABANDONED
f7bb0 29 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 );.}./*.** Relea
f7bc0 73 65 20 61 20 6c 6f 63 6b 20 61 63 71 75 69 72 se a lock acquir
f7bd0 65 64 20 62 79 20 77 69 6e 63 65 4d 75 74 65 78 ed by winceMutex
f7be0 41 63 71 75 69 72 65 28 29 0a 2a 2f 0a 23 64 65 Acquire().*/.#de
f7bf0 66 69 6e 65 20 77 69 6e 63 65 4d 75 74 65 78 52 fine winceMutexR
f7c00 65 6c 65 61 73 65 28 68 29 20 52 65 6c 65 61 73 elease(h) Releas
f7c10 65 4d 75 74 65 78 28 68 29 0a 0a 2f 2a 0a 2a 2a eMutex(h)../*.**
f7c20 20 43 72 65 61 74 65 20 74 68 65 20 6d 75 74 65 Create the mute
f7c30 78 20 61 6e 64 20 73 68 61 72 65 64 20 6d 65 6d x and shared mem
f7c40 6f 72 79 20 75 73 65 64 20 66 6f 72 20 6c 6f 63 ory used for loc
f7c50 6b 69 6e 67 20 69 6e 20 74 68 65 20 66 69 6c 65 king in the file
f7c60 0a 2a 2a 20 64 65 73 63 72 69 70 74 6f 72 20 70 .** descriptor p
f7c70 46 69 6c 65 0a 2a 2f 0a 73 74 61 74 69 63 20 42 File.*/.static B
f7c80 4f 4f 4c 20 77 69 6e 63 65 43 72 65 61 74 65 4c OOL winceCreateL
f7c90 6f 63 6b 28 63 6f 6e 73 74 20 63 68 61 72 20 2a ock(const char *
f7ca0 7a 46 69 6c 65 6e 61 6d 65 2c 20 77 69 6e 46 69 zFilename, winFi
f7cb0 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20 57 43 le *pFile){. WC
f7cc0 48 41 52 20 2a 7a 54 6f 6b 3b 0a 20 20 57 43 48 HAR *zTok;. WCH
f7cd0 41 52 20 2a 7a 4e 61 6d 65 20 3d 20 75 74 66 38 AR *zName = utf8
f7ce0 54 6f 55 6e 69 63 6f 64 65 28 7a 46 69 6c 65 6e ToUnicode(zFilen
f7cf0 61 6d 65 29 3b 0a 20 20 42 4f 4f 4c 20 62 49 6e ame);. BOOL bIn
f7d00 69 74 20 3d 20 54 52 55 45 3b 0a 0a 20 20 2f 2a it = TRUE;.. /*
f7d10 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 Initialize the
f7d20 6c 6f 63 61 6c 20 6c 6f 63 6b 64 61 74 61 20 2a local lockdata *
f7d30 2f 0a 20 20 5a 65 72 6f 4d 65 6d 6f 72 79 28 26 /. ZeroMemory(&
f7d40 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2c 20 73 69 pFile->local, si
f7d50 7a 65 6f 66 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 zeof(pFile->loca
f7d60 6c 29 29 3b 0a 0a 20 20 2f 2a 20 52 65 70 6c 61 l));.. /* Repla
f7d70 63 65 20 74 68 65 20 62 61 63 6b 73 6c 61 73 68 ce the backslash
f7d80 65 73 20 66 72 6f 6d 20 74 68 65 20 66 69 6c 65 es from the file
f7d90 6e 61 6d 65 20 61 6e 64 20 6c 6f 77 65 72 63 61 name and lowerca
f7da0 73 65 20 69 74 0a 20 20 2a 2a 20 74 6f 20 64 65 se it. ** to de
f7db0 72 69 76 65 20 61 20 6d 75 74 65 78 20 6e 61 6d rive a mutex nam
f7dc0 65 2e 20 2a 2f 0a 20 20 7a 54 6f 6b 20 3d 20 43 e. */. zTok = C
f7dd0 68 61 72 4c 6f 77 65 72 57 28 7a 4e 61 6d 65 29 harLowerW(zName)
f7de0 3b 0a 20 20 66 6f 72 20 28 3b 2a 7a 54 6f 6b 3b ;. for (;*zTok;
f7df0 7a 54 6f 6b 2b 2b 29 7b 0a 20 20 20 20 69 66 20 zTok++){. if
f7e00 28 2a 7a 54 6f 6b 20 3d 3d 20 27 5c 5c 27 29 20 (*zTok == '\\')
f7e10 2a 7a 54 6f 6b 20 3d 20 27 5f 27 3b 0a 20 20 7d *zTok = '_';. }
f7e20 0a 0a 20 20 2f 2a 20 43 72 65 61 74 65 2f 6f 70 .. /* Create/op
f7e30 65 6e 20 74 68 65 20 6e 61 6d 65 64 20 6d 75 74 en the named mut
f7e40 65 78 20 2a 2f 0a 20 20 70 46 69 6c 65 2d 3e 68 ex */. pFile->h
f7e50 4d 75 74 65 78 20 3d 20 43 72 65 61 74 65 4d 75 Mutex = CreateMu
f7e60 74 65 78 57 28 4e 55 4c 4c 2c 20 46 41 4c 53 45 texW(NULL, FALSE
f7e70 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 69 66 20 28 , zName);. if (
f7e80 21 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 7b !pFile->hMutex){
f7e90 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 . pFile->last
f7ea0 45 72 72 6e 6f 20 3d 20 47 65 74 4c 61 73 74 45 Errno = GetLastE
f7eb0 72 72 6f 72 28 29 3b 0a 20 20 20 20 66 72 65 65 rror();. free
f7ec0 28 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 72 65 74 (zName);. ret
f7ed0 75 72 6e 20 46 41 4c 53 45 3b 0a 20 20 7d 0a 0a urn FALSE;. }..
f7ee0 20 20 2f 2a 20 41 63 71 75 69 72 65 20 74 68 65 /* Acquire the
f7ef0 20 6d 75 74 65 78 20 62 65 66 6f 72 65 20 63 6f mutex before co
f7f00 6e 74 69 6e 75 69 6e 67 20 2a 2f 0a 20 20 77 69 ntinuing */. wi
f7f10 6e 63 65 4d 75 74 65 78 41 63 71 75 69 72 65 28 nceMutexAcquire(
f7f20 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a pFile->hMutex);.
f7f30 20 20 0a 20 20 2f 2a 20 53 69 6e 63 65 20 74 68 . /* Since th
f7f40 65 20 6e 61 6d 65 73 20 6f 66 20 6e 61 6d 65 64 e names of named
f7f50 20 6d 75 74 65 78 65 73 2c 20 73 65 6d 61 70 68 mutexes, semaph
f7f60 6f 72 65 73 2c 20 66 69 6c 65 20 6d 61 70 70 69 ores, file mappi
f7f70 6e 67 73 20 65 74 63 20 61 72 65 20 0a 20 20 2a ngs etc are . *
f7f80 2a 20 63 61 73 65 2d 73 65 6e 73 69 74 69 76 65 * case-sensitive
f7f90 2c 20 74 61 6b 65 20 61 64 76 61 6e 74 61 67 65 , take advantage
f7fa0 20 6f 66 20 74 68 61 74 20 62 79 20 75 70 70 65 of that by uppe
f7fb0 72 63 61 73 69 6e 67 20 74 68 65 20 6d 75 74 65 rcasing the mute
f7fc0 78 20 6e 61 6d 65 0a 20 20 2a 2a 20 61 6e 64 20 x name. ** and
f7fd0 75 73 69 6e 67 20 74 68 61 74 20 61 73 20 74 68 using that as th
f7fe0 65 20 73 68 61 72 65 64 20 66 69 6c 65 6d 61 70 e shared filemap
f7ff0 70 69 6e 67 20 6e 61 6d 65 2e 0a 20 20 2a 2f 0a ping name.. */.
f8000 20 20 43 68 61 72 55 70 70 65 72 57 28 7a 4e 61 CharUpperW(zNa
f8010 6d 65 29 3b 0a 20 20 70 46 69 6c 65 2d 3e 68 53 me);. pFile->hS
f8020 68 61 72 65 64 20 3d 20 43 72 65 61 74 65 46 69 hared = CreateFi
f8030 6c 65 4d 61 70 70 69 6e 67 57 28 49 4e 56 41 4c leMappingW(INVAL
f8040 49 44 5f 48 41 4e 44 4c 45 5f 56 41 4c 55 45 2c ID_HANDLE_VALUE,
f8050 20 4e 55 4c 4c 2c 0a 20 20 20 20 20 20 20 20 20 NULL,.
f8060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f8070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 50 41 PA
f8080 47 45 5f 52 45 41 44 57 52 49 54 45 2c 20 30 2c GE_READWRITE, 0,
f8090 20 73 69 7a 65 6f 66 28 77 69 6e 63 65 4c 6f 63 sizeof(winceLoc
f80a0 6b 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 k),.
f80b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f80c0 20 20 20 20 20 20 20 20 20 20 20 7a 4e 61 6d 65 zName
f80d0 29 3b 20 20 0a 0a 20 20 2f 2a 20 53 65 74 20 61 ); .. /* Set a
f80e0 20 66 6c 61 67 20 74 68 61 74 20 69 6e 64 69 63 flag that indic
f80f0 61 74 65 73 20 77 65 27 72 65 20 74 68 65 20 66 ates we're the f
f8100 69 72 73 74 20 74 6f 20 63 72 65 61 74 65 20 74 irst to create t
f8110 68 65 20 6d 65 6d 6f 72 79 20 73 6f 20 69 74 20 he memory so it
f8120 0a 20 20 2a 2a 20 6d 75 73 74 20 62 65 20 7a 65 . ** must be ze
f8130 72 6f 2d 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a ro-initialized *
f8140 2f 0a 20 20 69 66 20 28 47 65 74 4c 61 73 74 45 /. if (GetLastE
f8150 72 72 6f 72 28 29 20 3d 3d 20 45 52 52 4f 52 5f rror() == ERROR_
f8160 41 4c 52 45 41 44 59 5f 45 58 49 53 54 53 29 7b ALREADY_EXISTS){
f8170 0a 20 20 20 20 62 49 6e 69 74 20 3d 20 46 41 4c . bInit = FAL
f8180 53 45 3b 0a 20 20 7d 0a 0a 20 20 66 72 65 65 28 SE;. }.. free(
f8190 7a 4e 61 6d 65 29 3b 0a 0a 20 20 2f 2a 20 49 66 zName);.. /* If
f81a0 20 77 65 20 73 75 63 63 65 65 64 65 64 20 69 6e we succeeded in
f81b0 20 6d 61 6b 69 6e 67 20 74 68 65 20 73 68 61 72 making the shar
f81c0 65 64 20 6d 65 6d 6f 72 79 20 68 61 6e 64 6c 65 ed memory handle
f81d0 2c 20 6d 61 70 20 69 74 2e 20 2a 2f 0a 20 20 69 , map it. */. i
f81e0 66 20 28 70 46 69 6c 65 2d 3e 68 53 68 61 72 65 f (pFile->hShare
f81f0 64 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 73 d){. pFile->s
f8200 68 61 72 65 64 20 3d 20 28 77 69 6e 63 65 4c 6f hared = (winceLo
f8210 63 6b 2a 29 4d 61 70 56 69 65 77 4f 66 46 69 6c ck*)MapViewOfFil
f8220 65 28 70 46 69 6c 65 2d 3e 68 53 68 61 72 65 64 e(pFile->hShared
f8230 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 , .
f8240 46 49 4c 45 5f 4d 41 50 5f 52 45 41 44 7c 46 49 FILE_MAP_READ|FI
f8250 4c 45 5f 4d 41 50 5f 57 52 49 54 45 2c 20 30 2c LE_MAP_WRITE, 0,
f8260 20 30 2c 20 73 69 7a 65 6f 66 28 77 69 6e 63 65 0, sizeof(wince
f8270 4c 6f 63 6b 29 29 3b 0a 20 20 20 20 2f 2a 20 49 Lock));. /* I
f8280 66 20 6d 61 70 70 69 6e 67 20 66 61 69 6c 65 64 f mapping failed
f8290 2c 20 63 6c 6f 73 65 20 74 68 65 20 73 68 61 72 , close the shar
f82a0 65 64 20 6d 65 6d 6f 72 79 20 68 61 6e 64 6c 65 ed memory handle
f82b0 20 61 6e 64 20 65 72 61 73 65 20 69 74 20 2a 2f and erase it */
f82c0 0a 20 20 20 20 69 66 20 28 21 70 46 69 6c 65 2d . if (!pFile-
f82d0 3e 73 68 61 72 65 64 29 7b 0a 20 20 20 20 20 20 >shared){.
f82e0 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f pFile->lastErrno
f82f0 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f 72 28 = GetLastError(
f8300 29 3b 0a 20 20 20 20 20 20 43 6c 6f 73 65 48 61 );. CloseHa
f8310 6e 64 6c 65 28 70 46 69 6c 65 2d 3e 68 53 68 61 ndle(pFile->hSha
f8320 72 65 64 29 3b 0a 20 20 20 20 20 20 70 46 69 6c red);. pFil
f8330 65 2d 3e 68 53 68 61 72 65 64 20 3d 20 4e 55 4c e->hShared = NUL
f8340 4c 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 L;. }. }..
f8350 2f 2a 20 49 66 20 73 68 61 72 65 64 20 6d 65 6d /* If shared mem
f8360 6f 72 79 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 ory could not be
f8370 20 63 72 65 61 74 65 64 2c 20 74 68 65 6e 20 63 created, then c
f8380 6c 6f 73 65 20 74 68 65 20 6d 75 74 65 78 20 61 lose the mutex a
f8390 6e 64 20 66 61 69 6c 20 2a 2f 0a 20 20 69 66 20 nd fail */. if
f83a0 28 70 46 69 6c 65 2d 3e 68 53 68 61 72 65 64 20 (pFile->hShared
f83b0 3d 3d 20 4e 55 4c 4c 29 7b 0a 20 20 20 20 77 69 == NULL){. wi
f83c0 6e 63 65 4d 75 74 65 78 52 65 6c 65 61 73 65 28 nceMutexRelease(
f83d0 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a pFile->hMutex);.
f83e0 20 20 20 20 43 6c 6f 73 65 48 61 6e 64 6c 65 28 CloseHandle(
f83f0 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a pFile->hMutex);.
f8400 20 20 20 20 70 46 69 6c 65 2d 3e 68 4d 75 74 65 pFile->hMute
f8410 78 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 72 65 x = NULL;. re
f8420 74 75 72 6e 20 46 41 4c 53 45 3b 0a 20 20 7d 0a turn FALSE;. }.
f8430 20 20 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 . /* Initiali
f8440 7a 65 20 74 68 65 20 73 68 61 72 65 64 20 6d 65 ze the shared me
f8450 6d 6f 72 79 20 69 66 20 77 65 27 72 65 20 73 75 mory if we're su
f8460 70 70 6f 73 65 64 20 74 6f 20 2a 2f 0a 20 20 69 pposed to */. i
f8470 66 20 28 62 49 6e 69 74 29 20 7b 0a 20 20 20 20 f (bInit) {.
f8480 5a 65 72 6f 4d 65 6d 6f 72 79 28 70 46 69 6c 65 ZeroMemory(pFile
f8490 2d 3e 73 68 61 72 65 64 2c 20 73 69 7a 65 6f 66 ->shared, sizeof
f84a0 28 77 69 6e 63 65 4c 6f 63 6b 29 29 3b 0a 20 20 (winceLock));.
f84b0 7d 0a 0a 20 20 77 69 6e 63 65 4d 75 74 65 78 52 }.. winceMutexR
f84c0 65 6c 65 61 73 65 28 70 46 69 6c 65 2d 3e 68 4d elease(pFile->hM
f84d0 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 utex);. return
f84e0 54 52 55 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 TRUE;.}../*.** D
f84f0 65 73 74 72 6f 79 20 74 68 65 20 70 61 72 74 20 estroy the part
f8500 6f 66 20 77 69 6e 46 69 6c 65 20 74 68 61 74 20 of winFile that
f8510 64 65 61 6c 73 20 77 69 74 68 20 77 69 6e 63 65 deals with wince
f8520 20 6c 6f 63 6b 73 0a 2a 2f 0a 73 74 61 74 69 63 locks.*/.static
f8530 20 76 6f 69 64 20 77 69 6e 63 65 44 65 73 74 72 void winceDestr
f8540 6f 79 4c 6f 63 6b 28 77 69 6e 46 69 6c 65 20 2a oyLock(winFile *
f8550 70 46 69 6c 65 29 7b 0a 20 20 69 66 20 28 70 46 pFile){. if (pF
f8560 69 6c 65 2d 3e 68 4d 75 74 65 78 29 7b 0a 20 20 ile->hMutex){.
f8570 20 20 2f 2a 20 41 63 71 75 69 72 65 20 74 68 65 /* Acquire the
f8580 20 6d 75 74 65 78 20 2a 2f 0a 20 20 20 20 77 69 mutex */. wi
f8590 6e 63 65 4d 75 74 65 78 41 63 71 75 69 72 65 28 nceMutexAcquire(
f85a0 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a pFile->hMutex);.
f85b0 0a 20 20 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c . /* The foll
f85c0 6f 77 69 6e 67 20 62 6c 6f 63 6b 73 20 73 68 6f owing blocks sho
f85d0 75 6c 64 20 70 72 6f 62 61 62 6c 79 20 61 73 73 uld probably ass
f85e0 65 72 74 20 69 6e 20 64 65 62 75 67 20 6d 6f 64 ert in debug mod
f85f0 65 2c 20 62 75 74 20 74 68 65 79 0a 20 20 20 20 e, but they.
f8600 20 20 20 61 72 65 20 74 6f 20 63 6c 65 61 6e 75 are to cleanu
f8610 70 20 69 6e 20 63 61 73 65 20 61 6e 79 20 6c 6f p in case any lo
f8620 63 6b 73 20 72 65 6d 61 69 6e 65 64 20 6f 70 65 cks remained ope
f8630 6e 20 2a 2f 0a 20 20 20 20 69 66 20 28 70 46 69 n */. if (pFi
f8640 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61 64 65 le->local.nReade
f8650 72 73 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 rs){. pFile
f8660 2d 3e 73 68 61 72 65 64 2d 3e 6e 52 65 61 64 65 ->shared->nReade
f8670 72 73 20 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 rs --;. }.
f8680 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 if (pFile->loca
f8690 6c 2e 62 52 65 73 65 72 76 65 64 29 7b 0a 20 20 l.bReserved){.
f86a0 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 pFile->share
f86b0 64 2d 3e 62 52 65 73 65 72 76 65 64 20 3d 20 46 d->bReserved = F
f86c0 41 4c 53 45 3b 0a 20 20 20 20 7d 0a 20 20 20 20 ALSE;. }.
f86d0 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c if (pFile->local
f86e0 2e 62 50 65 6e 64 69 6e 67 29 7b 0a 20 20 20 20 .bPending){.
f86f0 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d pFile->shared-
f8700 3e 62 50 65 6e 64 69 6e 67 20 3d 20 46 41 4c 53 >bPending = FALS
f8710 45 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 20 E;. }. if
f8720 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 45 (pFile->local.bE
f8730 78 63 6c 75 73 69 76 65 29 7b 0a 20 20 20 20 20 xclusive){.
f8740 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e pFile->shared->
f8750 62 45 78 63 6c 75 73 69 76 65 20 3d 20 46 41 4c bExclusive = FAL
f8760 53 45 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f SE;. }.. /
f8770 2a 20 44 65 2d 72 65 66 65 72 65 6e 63 65 20 61 * De-reference a
f8780 6e 64 20 63 6c 6f 73 65 20 6f 75 72 20 63 6f 70 nd close our cop
f8790 79 20 6f 66 20 74 68 65 20 73 68 61 72 65 64 20 y of the shared
f87a0 6d 65 6d 6f 72 79 20 68 61 6e 64 6c 65 20 2a 2f memory handle */
f87b0 0a 20 20 20 20 55 6e 6d 61 70 56 69 65 77 4f 66 . UnmapViewOf
f87c0 46 69 6c 65 28 70 46 69 6c 65 2d 3e 73 68 61 72 File(pFile->shar
f87d0 65 64 29 3b 0a 20 20 20 20 43 6c 6f 73 65 48 61 ed);. CloseHa
f87e0 6e 64 6c 65 28 70 46 69 6c 65 2d 3e 68 53 68 61 ndle(pFile->hSha
f87f0 72 65 64 29 3b 0a 0a 20 20 20 20 2f 2a 20 44 6f red);.. /* Do
f8800 6e 65 20 77 69 74 68 20 74 68 65 20 6d 75 74 65 ne with the mute
f8810 78 20 2a 2f 0a 20 20 20 20 77 69 6e 63 65 4d 75 x */. winceMu
f8820 74 65 78 52 65 6c 65 61 73 65 28 70 46 69 6c 65 texRelease(pFile
f8830 2d 3e 68 4d 75 74 65 78 29 3b 20 20 20 20 0a 20 ->hMutex); .
f8840 20 20 20 43 6c 6f 73 65 48 61 6e 64 6c 65 28 70 CloseHandle(p
f8850 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 File->hMutex);.
f8860 20 20 20 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 pFile->hMutex
f8870 20 3d 20 4e 55 4c 4c 3b 0a 20 20 7d 0a 7d 0a 0a = NULL;. }.}..
f8880 2f 2a 20 0a 2a 2a 20 41 6e 20 69 6d 70 6c 65 6d /* .** An implem
f8890 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 entation of the
f88a0 4c 6f 63 6b 46 69 6c 65 28 29 20 41 50 49 20 6f LockFile() API o
f88b0 66 20 77 69 6e 64 6f 77 73 20 66 6f 72 20 77 69 f windows for wi
f88c0 6e 63 65 0a 2a 2f 0a 73 74 61 74 69 63 20 42 4f nce.*/.static BO
f88d0 4f 4c 20 77 69 6e 63 65 4c 6f 63 6b 46 69 6c 65 OL winceLockFile
f88e0 28 0a 20 20 48 41 4e 44 4c 45 20 2a 70 68 46 69 (. HANDLE *phFi
f88f0 6c 65 2c 0a 20 20 44 57 4f 52 44 20 64 77 46 69 le,. DWORD dwFi
f8900 6c 65 4f 66 66 73 65 74 4c 6f 77 2c 0a 20 20 44 leOffsetLow,. D
f8910 57 4f 52 44 20 64 77 46 69 6c 65 4f 66 66 73 65 WORD dwFileOffse
f8920 74 48 69 67 68 2c 0a 20 20 44 57 4f 52 44 20 6e tHigh,. DWORD n
f8930 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 4c NumberOfBytesToL
f8940 6f 63 6b 4c 6f 77 2c 0a 20 20 44 57 4f 52 44 20 ockLow,. DWORD
f8950 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f nNumberOfBytesTo
f8960 4c 6f 63 6b 48 69 67 68 0a 29 7b 0a 20 20 77 69 LockHigh.){. wi
f8970 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 48 nFile *pFile = H
f8980 41 4e 44 4c 45 5f 54 4f 5f 57 49 4e 46 49 4c 45 ANDLE_TO_WINFILE
f8990 28 70 68 46 69 6c 65 29 3b 0a 20 20 42 4f 4f 4c (phFile);. BOOL
f89a0 20 62 52 65 74 75 72 6e 20 3d 20 46 41 4c 53 45 bReturn = FALSE
f89b0 3b 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 ;.. UNUSED_PARA
f89c0 4d 45 54 45 52 28 64 77 46 69 6c 65 4f 66 66 73 METER(dwFileOffs
f89d0 65 74 48 69 67 68 29 3b 0a 20 20 55 4e 55 53 45 etHigh);. UNUSE
f89e0 44 5f 50 41 52 41 4d 45 54 45 52 28 6e 4e 75 6d D_PARAMETER(nNum
f89f0 62 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b berOfBytesToLock
f8a00 48 69 67 68 29 3b 0a 0a 20 20 69 66 20 28 21 70 High);.. if (!p
f8a10 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 20 72 65 File->hMutex) re
f8a20 74 75 72 6e 20 54 52 55 45 3b 0a 20 20 77 69 6e turn TRUE;. win
f8a30 63 65 4d 75 74 65 78 41 63 71 75 69 72 65 28 70 ceMutexAcquire(p
f8a40 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 0a File->hMutex);..
f8a50 20 20 2f 2a 20 57 61 6e 74 69 6e 67 20 61 6e 20 /* Wanting an
f8a60 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 3f 20 exclusive lock?
f8a70 2a 2f 0a 20 20 69 66 20 28 64 77 46 69 6c 65 4f */. if (dwFileO
f8a80 66 66 73 65 74 4c 6f 77 20 3d 3d 20 28 44 57 4f ffsetLow == (DWO
f8a90 52 44 29 53 48 41 52 45 44 5f 46 49 52 53 54 0a RD)SHARED_FIRST.
f8aa0 20 20 20 20 20 20 20 26 26 20 6e 4e 75 6d 62 65 && nNumbe
f8ab0 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b 4c 6f rOfBytesToLockLo
f8ac0 77 20 3d 3d 20 28 44 57 4f 52 44 29 53 48 41 52 w == (DWORD)SHAR
f8ad0 45 44 5f 53 49 5a 45 29 7b 0a 20 20 20 20 69 66 ED_SIZE){. if
f8ae0 20 28 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d (pFile->shared-
f8af0 3e 6e 52 65 61 64 65 72 73 20 3d 3d 20 30 20 26 >nReaders == 0 &
f8b00 26 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d & pFile->shared-
f8b10 3e 62 45 78 63 6c 75 73 69 76 65 20 3d 3d 20 30 >bExclusive == 0
f8b20 29 7b 0a 20 20 20 20 20 20 20 70 46 69 6c 65 2d ){. pFile-
f8b30 3e 73 68 61 72 65 64 2d 3e 62 45 78 63 6c 75 73 >shared->bExclus
f8b40 69 76 65 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 ive = TRUE;.
f8b50 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e pFile->local.
f8b60 62 45 78 63 6c 75 73 69 76 65 20 3d 20 54 52 55 bExclusive = TRU
f8b70 45 3b 0a 20 20 20 20 20 20 20 62 52 65 74 75 72 E;. bRetur
f8b80 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a n = TRUE;. }.
f8b90 20 20 7d 0a 0a 20 20 2f 2a 20 57 61 6e 74 20 61 }.. /* Want a
f8ba0 20 72 65 61 64 2d 6f 6e 6c 79 20 6c 6f 63 6b 3f read-only lock?
f8bb0 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 20 28 64 */. else if (d
f8bc0 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3d wFileOffsetLow =
f8bd0 3d 20 28 44 57 4f 52 44 29 53 48 41 52 45 44 5f = (DWORD)SHARED_
f8be0 46 49 52 53 54 20 26 26 0a 20 20 20 20 20 20 20 FIRST &&.
f8bf0 20 20 20 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 nNumberOfByt
f8c00 65 73 54 6f 4c 6f 63 6b 4c 6f 77 20 3d 3d 20 31 esToLockLow == 1
f8c10 29 7b 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65 ){. if (pFile
f8c20 2d 3e 73 68 61 72 65 64 2d 3e 62 45 78 63 6c 75 ->shared->bExclu
f8c30 73 69 76 65 20 3d 3d 20 30 29 7b 0a 20 20 20 20 sive == 0){.
f8c40 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e pFile->local.n
f8c50 52 65 61 64 65 72 73 20 2b 2b 3b 0a 20 20 20 20 Readers ++;.
f8c60 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 if (pFile->loc
f8c70 61 6c 2e 6e 52 65 61 64 65 72 73 20 3d 3d 20 31 al.nReaders == 1
f8c80 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 ){. pFile
f8c90 2d 3e 73 68 61 72 65 64 2d 3e 6e 52 65 61 64 65 ->shared->nReade
f8ca0 72 73 20 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 rs ++;. }.
f8cb0 20 20 20 20 20 62 52 65 74 75 72 6e 20 3d 20 54 bReturn = T
f8cc0 52 55 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a RUE;. }. }..
f8cd0 20 20 2f 2a 20 57 61 6e 74 20 61 20 70 65 6e 64 /* Want a pend
f8ce0 69 6e 67 20 6c 6f 63 6b 3f 20 2a 2f 0a 20 20 65 ing lock? */. e
f8cf0 6c 73 65 20 69 66 20 28 64 77 46 69 6c 65 4f 66 lse if (dwFileOf
f8d00 66 73 65 74 4c 6f 77 20 3d 3d 20 28 44 57 4f 52 fsetLow == (DWOR
f8d10 44 29 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 26 D)PENDING_BYTE &
f8d20 26 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 & nNumberOfBytes
f8d30 54 6f 4c 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29 7b ToLockLow == 1){
f8d40 0a 20 20 20 20 2f 2a 20 49 66 20 6e 6f 20 70 65 . /* If no pe
f8d50 6e 64 69 6e 67 20 6c 6f 63 6b 20 68 61 73 20 62 nding lock has b
f8d60 65 65 6e 20 61 63 71 75 69 72 65 64 2c 20 74 68 een acquired, th
f8d70 65 6e 20 61 63 71 75 69 72 65 20 69 74 20 2a 2f en acquire it */
f8d80 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e . if (pFile->
f8d90 73 68 61 72 65 64 2d 3e 62 50 65 6e 64 69 6e 67 shared->bPending
f8da0 20 3d 3d 20 30 29 20 7b 0a 20 20 20 20 20 20 70 == 0) {. p
f8db0 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 50 File->shared->bP
f8dc0 65 6e 64 69 6e 67 20 3d 20 54 52 55 45 3b 0a 20 ending = TRUE;.
f8dd0 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 pFile->loca
f8de0 6c 2e 62 50 65 6e 64 69 6e 67 20 3d 20 54 52 55 l.bPending = TRU
f8df0 45 3b 0a 20 20 20 20 20 20 62 52 65 74 75 72 6e E;. bReturn
f8e00 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20 = TRUE;. }.
f8e10 20 7d 0a 0a 20 20 2f 2a 20 57 61 6e 74 20 61 20 }.. /* Want a
f8e20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 3f 20 2a reserved lock? *
f8e30 2f 0a 20 20 65 6c 73 65 20 69 66 20 28 64 77 46 /. else if (dwF
f8e40 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3d 3d 20 ileOffsetLow ==
f8e50 28 44 57 4f 52 44 29 52 45 53 45 52 56 45 44 5f (DWORD)RESERVED_
f8e60 42 59 54 45 20 26 26 20 6e 4e 75 6d 62 65 72 4f BYTE && nNumberO
f8e70 66 42 79 74 65 73 54 6f 4c 6f 63 6b 4c 6f 77 20 fBytesToLockLow
f8e80 3d 3d 20 31 29 7b 0a 20 20 20 20 69 66 20 28 70 == 1){. if (p
f8e90 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 52 File->shared->bR
f8ea0 65 73 65 72 76 65 64 20 3d 3d 20 30 29 20 7b 0a eserved == 0) {.
f8eb0 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 pFile->sha
f8ec0 72 65 64 2d 3e 62 52 65 73 65 72 76 65 64 20 3d red->bReserved =
f8ed0 20 54 52 55 45 3b 0a 20 20 20 20 20 20 70 46 69 TRUE;. pFi
f8ee0 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 52 65 73 65 72 le->local.bReser
f8ef0 76 65 64 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 ved = TRUE;.
f8f00 20 20 62 52 65 74 75 72 6e 20 3d 20 54 52 55 45 bReturn = TRUE
f8f10 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 77 ;. }. }.. w
f8f20 69 6e 63 65 4d 75 74 65 78 52 65 6c 65 61 73 65 inceMutexRelease
f8f30 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b (pFile->hMutex);
f8f40 0a 20 20 72 65 74 75 72 6e 20 62 52 65 74 75 72 . return bRetur
f8f50 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 n;.}../*.** An i
f8f60 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 mplementation of
f8f70 20 74 68 65 20 55 6e 6c 6f 63 6b 46 69 6c 65 20 the UnlockFile
f8f80 41 50 49 20 6f 66 20 77 69 6e 64 6f 77 73 20 66 API of windows f
f8f90 6f 72 20 77 69 6e 63 65 0a 2a 2f 0a 73 74 61 74 or wince.*/.stat
f8fa0 69 63 20 42 4f 4f 4c 20 77 69 6e 63 65 55 6e 6c ic BOOL winceUnl
f8fb0 6f 63 6b 46 69 6c 65 28 0a 20 20 48 41 4e 44 4c ockFile(. HANDL
f8fc0 45 20 2a 70 68 46 69 6c 65 2c 0a 20 20 44 57 4f E *phFile,. DWO
f8fd0 52 44 20 64 77 46 69 6c 65 4f 66 66 73 65 74 4c RD dwFileOffsetL
f8fe0 6f 77 2c 0a 20 20 44 57 4f 52 44 20 64 77 46 69 ow,. DWORD dwFi
f8ff0 6c 65 4f 66 66 73 65 74 48 69 67 68 2c 0a 20 20 leOffsetHigh,.
f9000 44 57 4f 52 44 20 6e 4e 75 6d 62 65 72 4f 66 42 DWORD nNumberOfB
f9010 79 74 65 73 54 6f 55 6e 6c 6f 63 6b 4c 6f 77 2c ytesToUnlockLow,
f9020 0a 20 20 44 57 4f 52 44 20 6e 4e 75 6d 62 65 72 . DWORD nNumber
f9030 4f 66 42 79 74 65 73 54 6f 55 6e 6c 6f 63 6b 48 OfBytesToUnlockH
f9040 69 67 68 0a 29 7b 0a 20 20 77 69 6e 46 69 6c 65 igh.){. winFile
f9050 20 2a 70 46 69 6c 65 20 3d 20 48 41 4e 44 4c 45 *pFile = HANDLE
f9060 5f 54 4f 5f 57 49 4e 46 49 4c 45 28 70 68 46 69 _TO_WINFILE(phFi
f9070 6c 65 29 3b 0a 20 20 42 4f 4f 4c 20 62 52 65 74 le);. BOOL bRet
f9080 75 72 6e 20 3d 20 46 41 4c 53 45 3b 0a 0a 20 20 urn = FALSE;..
f9090 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
f90a0 28 64 77 46 69 6c 65 4f 66 66 73 65 74 48 69 67 (dwFileOffsetHig
f90b0 68 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 h);. UNUSED_PAR
f90c0 41 4d 45 54 45 52 28 6e 4e 75 6d 62 65 72 4f 66 AMETER(nNumberOf
f90d0 42 79 74 65 73 54 6f 55 6e 6c 6f 63 6b 48 69 67 BytesToUnlockHig
f90e0 68 29 3b 0a 0a 20 20 69 66 20 28 21 70 46 69 6c h);.. if (!pFil
f90f0 65 2d 3e 68 4d 75 74 65 78 29 20 72 65 74 75 72 e->hMutex) retur
f9100 6e 20 54 52 55 45 3b 0a 20 20 77 69 6e 63 65 4d n TRUE;. winceM
f9110 75 74 65 78 41 63 71 75 69 72 65 28 70 46 69 6c utexAcquire(pFil
f9120 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 0a 20 20 2f e->hMutex);.. /
f9130 2a 20 52 65 6c 65 61 73 69 6e 67 20 61 20 72 65 * Releasing a re
f9140 61 64 65 72 20 6c 6f 63 6b 20 6f 72 20 61 6e 20 ader lock or an
f9150 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 2a exclusive lock *
f9160 2f 0a 20 20 69 66 20 28 64 77 46 69 6c 65 4f 66 /. if (dwFileOf
f9170 66 73 65 74 4c 6f 77 20 3d 3d 20 28 44 57 4f 52 fsetLow == (DWOR
f9180 44 29 53 48 41 52 45 44 5f 46 49 52 53 54 29 7b D)SHARED_FIRST){
f9190 0a 20 20 20 20 2f 2a 20 44 69 64 20 77 65 20 68 . /* Did we h
f91a0 61 76 65 20 61 6e 20 65 78 63 6c 75 73 69 76 65 ave an exclusive
f91b0 20 6c 6f 63 6b 3f 20 2a 2f 0a 20 20 20 20 69 66 lock? */. if
f91c0 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 (pFile->local.b
f91d0 45 78 63 6c 75 73 69 76 65 29 7b 0a 20 20 20 20 Exclusive){.
f91e0 20 20 61 73 73 65 72 74 28 6e 4e 75 6d 62 65 72 assert(nNumber
f91f0 4f 66 42 79 74 65 73 54 6f 55 6e 6c 6f 63 6b 4c OfBytesToUnlockL
f9200 6f 77 20 3d 3d 20 28 44 57 4f 52 44 29 53 48 41 ow == (DWORD)SHA
f9210 52 45 44 5f 53 49 5a 45 29 3b 0a 20 20 20 20 20 RED_SIZE);.
f9220 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 45 pFile->local.bE
f9230 78 63 6c 75 73 69 76 65 20 3d 20 46 41 4c 53 45 xclusive = FALSE
f9240 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 ;. pFile->s
f9250 68 61 72 65 64 2d 3e 62 45 78 63 6c 75 73 69 76 hared->bExclusiv
f9260 65 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20 20 e = FALSE;.
f9270 20 62 52 65 74 75 72 6e 20 3d 20 54 52 55 45 3b bReturn = TRUE;
f9280 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 . }.. /* D
f9290 69 64 20 77 65 20 6a 75 73 74 20 68 61 76 65 20 id we just have
f92a0 61 20 72 65 61 64 65 72 20 6c 6f 63 6b 3f 20 2a a reader lock? *
f92b0 2f 0a 20 20 20 20 65 6c 73 65 20 69 66 20 28 70 /. else if (p
f92c0 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61 File->local.nRea
f92d0 64 65 72 73 29 7b 0a 20 20 20 20 20 20 61 73 73 ders){. ass
f92e0 65 72 74 28 6e 4e 75 6d 62 65 72 4f 66 42 79 74 ert(nNumberOfByt
f92f0 65 73 54 6f 55 6e 6c 6f 63 6b 4c 6f 77 20 3d 3d esToUnlockLow ==
f9300 20 28 44 57 4f 52 44 29 53 48 41 52 45 44 5f 53 (DWORD)SHARED_S
f9310 49 5a 45 20 7c 7c 20 6e 4e 75 6d 62 65 72 4f 66 IZE || nNumberOf
f9320 42 79 74 65 73 54 6f 55 6e 6c 6f 63 6b 4c 6f 77 BytesToUnlockLow
f9330 20 3d 3d 20 31 29 3b 0a 20 20 20 20 20 20 70 46 == 1);. pF
f9340 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61 64 ile->local.nRead
f9350 65 72 73 20 2d 2d 3b 0a 20 20 20 20 20 20 69 66 ers --;. if
f9360 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e (pFile->local.n
f9370 52 65 61 64 65 72 73 20 3d 3d 20 30 29 0a 20 20 Readers == 0).
f9380 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20 70 46 {. pF
f9390 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 6e 52 65 ile->shared->nRe
f93a0 61 64 65 72 73 20 2d 2d 3b 0a 20 20 20 20 20 20 aders --;.
f93b0 7d 0a 20 20 20 20 20 20 62 52 65 74 75 72 6e 20 }. bReturn
f93c0 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20 20 = TRUE;. }.
f93d0 7d 0a 0a 20 20 2f 2a 20 52 65 6c 65 61 73 69 6e }.. /* Releasin
f93e0 67 20 61 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b g a pending lock
f93f0 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 20 28 64 */. else if (d
f9400 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3d wFileOffsetLow =
f9410 3d 20 28 44 57 4f 52 44 29 50 45 4e 44 49 4e 47 = (DWORD)PENDING
f9420 5f 42 59 54 45 20 26 26 20 6e 4e 75 6d 62 65 72 _BYTE && nNumber
f9430 4f 66 42 79 74 65 73 54 6f 55 6e 6c 6f 63 6b 4c OfBytesToUnlockL
f9440 6f 77 20 3d 3d 20 31 29 7b 0a 20 20 20 20 69 66 ow == 1){. if
f9450 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 (pFile->local.b
f9460 50 65 6e 64 69 6e 67 29 7b 0a 20 20 20 20 20 20 Pending){.
f9470 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 50 65 pFile->local.bPe
f9480 6e 64 69 6e 67 20 3d 20 46 41 4c 53 45 3b 0a 20 nding = FALSE;.
f9490 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 pFile->shar
f94a0 65 64 2d 3e 62 50 65 6e 64 69 6e 67 20 3d 20 46 ed->bPending = F
f94b0 41 4c 53 45 3b 0a 20 20 20 20 20 20 62 52 65 74 ALSE;. bRet
f94c0 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 urn = TRUE;.
f94d0 7d 0a 20 20 7d 0a 20 20 2f 2a 20 52 65 6c 65 61 }. }. /* Relea
f94e0 73 69 6e 67 20 61 20 72 65 73 65 72 76 65 64 20 sing a reserved
f94f0 6c 6f 63 6b 20 2a 2f 0a 20 20 65 6c 73 65 20 69 lock */. else i
f9500 66 20 28 64 77 46 69 6c 65 4f 66 66 73 65 74 4c f (dwFileOffsetL
f9510 6f 77 20 3d 3d 20 28 44 57 4f 52 44 29 52 45 53 ow == (DWORD)RES
f9520 45 52 56 45 44 5f 42 59 54 45 20 26 26 20 6e 4e ERVED_BYTE && nN
f9530 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 55 6e umberOfBytesToUn
f9540 6c 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29 7b 0a 20 lockLow == 1){.
f9550 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f if (pFile->lo
f9560 63 61 6c 2e 62 52 65 73 65 72 76 65 64 29 20 7b cal.bReserved) {
f9570 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f . pFile->lo
f9580 63 61 6c 2e 62 52 65 73 65 72 76 65 64 20 3d 20 cal.bReserved =
f9590 46 41 4c 53 45 3b 0a 20 20 20 20 20 20 70 46 69 FALSE;. pFi
f95a0 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 52 65 73 le->shared->bRes
f95b0 65 72 76 65 64 20 3d 20 46 41 4c 53 45 3b 0a 20 erved = FALSE;.
f95c0 20 20 20 20 20 62 52 65 74 75 72 6e 20 3d 20 54 bReturn = T
f95d0 52 55 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a RUE;. }. }..
f95e0 20 20 77 69 6e 63 65 4d 75 74 65 78 52 65 6c 65 winceMutexRele
f95f0 61 73 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 ase(pFile->hMute
f9600 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 62 52 65 x);. return bRe
f9610 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 turn;.}../*.** A
f9620 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e n implementation
f9630 20 6f 66 20 74 68 65 20 4c 6f 63 6b 46 69 6c 65 of the LockFile
f9640 45 78 28 29 20 41 50 49 20 6f 66 20 77 69 6e 64 Ex() API of wind
f9650 6f 77 73 20 66 6f 72 20 77 69 6e 63 65 0a 2a 2f ows for wince.*/
f9660 0a 73 74 61 74 69 63 20 42 4f 4f 4c 20 77 69 6e .static BOOL win
f9670 63 65 4c 6f 63 6b 46 69 6c 65 45 78 28 0a 20 20 ceLockFileEx(.
f9680 48 41 4e 44 4c 45 20 2a 70 68 46 69 6c 65 2c 0a HANDLE *phFile,.
f9690 20 20 44 57 4f 52 44 20 64 77 46 6c 61 67 73 2c DWORD dwFlags,
f96a0 0a 20 20 44 57 4f 52 44 20 64 77 52 65 73 65 72 . DWORD dwReser
f96b0 76 65 64 2c 0a 20 20 44 57 4f 52 44 20 6e 4e 75 ved,. DWORD nNu
f96c0 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 mberOfBytesToLoc
f96d0 6b 4c 6f 77 2c 0a 20 20 44 57 4f 52 44 20 6e 4e kLow,. DWORD nN
f96e0 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f umberOfBytesToLo
f96f0 63 6b 48 69 67 68 2c 0a 20 20 4c 50 4f 56 45 52 ckHigh,. LPOVER
f9700 4c 41 50 50 45 44 20 6c 70 4f 76 65 72 6c 61 70 LAPPED lpOverlap
f9710 70 65 64 0a 29 7b 0a 20 20 55 4e 55 53 45 44 5f ped.){. UNUSED_
f9720 50 41 52 41 4d 45 54 45 52 28 64 77 52 65 73 65 PARAMETER(dwRese
f9730 72 76 65 64 29 3b 0a 20 20 55 4e 55 53 45 44 5f rved);. UNUSED_
f9740 50 41 52 41 4d 45 54 45 52 28 6e 4e 75 6d 62 65 PARAMETER(nNumbe
f9750 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b 48 69 rOfBytesToLockHi
f9760 67 68 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 gh);.. /* If th
f9770 65 20 63 61 6c 6c 65 72 20 77 61 6e 74 73 20 61 e caller wants a
f9780 20 73 68 61 72 65 64 20 72 65 61 64 20 6c 6f 63 shared read loc
f9790 6b 2c 20 66 6f 72 77 61 72 64 20 74 68 69 73 20 k, forward this
f97a0 63 61 6c 6c 0a 20 20 2a 2a 20 74 6f 20 77 69 6e call. ** to win
f97b0 63 65 4c 6f 63 6b 46 69 6c 65 20 2a 2f 0a 20 20 ceLockFile */.
f97c0 69 66 20 28 6c 70 4f 76 65 72 6c 61 70 70 65 64 if (lpOverlapped
f97d0 2d 3e 4f 66 66 73 65 74 20 3d 3d 20 28 44 57 4f ->Offset == (DWO
f97e0 52 44 29 53 48 41 52 45 44 5f 46 49 52 53 54 20 RD)SHARED_FIRST
f97f0 26 26 0a 20 20 20 20 20 20 64 77 46 6c 61 67 73 &&. dwFlags
f9800 20 3d 3d 20 31 20 26 26 0a 20 20 20 20 20 20 6e == 1 &&. n
f9810 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 4c NumberOfBytesToL
f9820 6f 63 6b 4c 6f 77 20 3d 3d 20 28 44 57 4f 52 44 ockLow == (DWORD
f9830 29 53 48 41 52 45 44 5f 53 49 5a 45 29 7b 0a 20 )SHARED_SIZE){.
f9840 20 20 20 72 65 74 75 72 6e 20 77 69 6e 63 65 4c return winceL
f9850 6f 63 6b 46 69 6c 65 28 70 68 46 69 6c 65 2c 20 ockFile(phFile,
f9860 53 48 41 52 45 44 5f 46 49 52 53 54 2c 20 30 2c SHARED_FIRST, 0,
f9870 20 31 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65 1, 0);. }. re
f9880 74 75 72 6e 20 46 41 4c 53 45 3b 0a 7d 0a 2f 2a turn FALSE;.}./*
f9890 0a 2a 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 73 .** End of the s
f98a0 70 65 63 69 61 6c 20 63 6f 64 65 20 66 6f 72 20 pecial code for
f98b0 77 69 6e 63 65 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a wince.**********
f98c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f98d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f98e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f98f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9900 2a 2a 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 53 ***/.#endif /* S
f9910 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 20 2a QLITE_OS_WINCE *
f9920 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /../************
f9930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9970 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 67 72 *.** The next gr
f9980 6f 75 70 20 6f 66 20 72 6f 75 74 69 6e 65 73 20 oup of routines
f9990 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 49 2f implement the I/
f99a0 4f 20 6d 65 74 68 6f 64 73 20 73 70 65 63 69 66 O methods specif
f99b0 69 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 73 71 ied.** by the sq
f99c0 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 lite3_io_methods
f99d0 20 6f 62 6a 65 63 74 2e 0a 2a 2a 2a 2a 2a 2a 2a object..*******
f99e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f99f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9a00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9a10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9a20 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 *******/../*.**
f9a30 43 6c 6f 73 65 20 61 20 66 69 6c 65 2e 0a 2a 2a Close a file..**
f9a40 0a 2a 2a 20 49 74 20 69 73 20 72 65 70 6f 72 74 .** It is report
f9a50 65 64 20 74 68 61 74 20 61 6e 20 61 74 74 65 6d ed that an attem
f9a60 70 74 20 74 6f 20 63 6c 6f 73 65 20 61 20 68 61 pt to close a ha
f9a70 6e 64 6c 65 20 6d 69 67 68 74 20 73 6f 6d 65 74 ndle might somet
f9a80 69 6d 65 73 0a 2a 2a 20 66 61 69 6c 2e 20 20 54 imes.** fail. T
f9a90 68 69 73 20 69 73 20 61 20 76 65 72 79 20 75 6e his is a very un
f9aa0 72 65 61 73 6f 6e 61 62 6c 65 20 72 65 73 75 6c reasonable resul
f9ab0 74 2c 20 62 75 74 20 77 69 6e 64 6f 77 73 20 69 t, but windows i
f9ac0 73 20 6e 6f 74 6f 72 69 6f 75 73 0a 2a 2a 20 66 s notorious.** f
f9ad0 6f 72 20 62 65 69 6e 67 20 75 6e 72 65 61 73 6f or being unreaso
f9ae0 6e 61 62 6c 65 20 73 6f 20 49 20 64 6f 20 6e 6f nable so I do no
f9af0 74 20 64 6f 75 62 74 20 74 68 61 74 20 69 74 20 t doubt that it
f9b00 6d 69 67 68 74 20 68 61 70 70 65 6e 2e 20 20 49 might happen. I
f9b10 66 0a 2a 2a 20 74 68 65 20 63 6c 6f 73 65 20 66 f.** the close f
f9b20 61 69 6c 73 2c 20 77 65 20 70 61 75 73 65 20 66 ails, we pause f
f9b30 6f 72 20 31 30 30 20 6d 69 6c 6c 69 73 65 63 6f or 100 milliseco
f9b40 6e 64 73 20 61 6e 64 20 74 72 79 20 61 67 61 69 nds and try agai
f9b50 6e 2e 20 20 41 73 0a 2a 2a 20 6d 61 6e 79 20 61 n. As.** many a
f9b60 73 20 4d 58 5f 43 4c 4f 53 45 5f 41 54 54 45 4d s MX_CLOSE_ATTEM
f9b70 50 54 20 61 74 74 65 6d 70 74 73 20 74 6f 20 63 PT attempts to c
f9b80 6c 6f 73 65 20 74 68 65 20 68 61 6e 64 6c 65 20 lose the handle
f9b90 61 72 65 20 6d 61 64 65 20 62 65 66 6f 72 65 0a are made before.
f9ba0 2a 2a 20 67 69 76 69 6e 67 20 75 70 20 61 6e 64 ** giving up and
f9bb0 20 72 65 74 75 72 6e 69 6e 67 20 61 6e 20 65 72 returning an er
f9bc0 72 6f 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 ror..*/.#define
f9bd0 4d 58 5f 43 4c 4f 53 45 5f 41 54 54 45 4d 50 54 MX_CLOSE_ATTEMPT
f9be0 20 33 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 3.static int wi
f9bf0 6e 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 nClose(sqlite3_f
f9c00 69 6c 65 20 2a 69 64 29 7b 0a 20 20 69 6e 74 20 ile *id){. int
f9c10 72 63 2c 20 63 6e 74 20 3d 20 30 3b 0a 20 20 77 rc, cnt = 0;. w
f9c20 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 inFile *pFile =
f9c30 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a 0a 20 (winFile*)id;..
f9c40 20 61 73 73 65 72 74 28 20 69 64 21 3d 30 20 29 assert( id!=0 )
f9c50 3b 0a 20 20 4f 53 54 52 41 43 45 32 28 22 43 4c ;. OSTRACE2("CL
f9c60 4f 53 45 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 OSE %d\n", pFile
f9c70 2d 3e 68 29 3b 0a 20 20 64 6f 7b 0a 20 20 20 20 ->h);. do{.
f9c80 72 63 20 3d 20 43 6c 6f 73 65 48 61 6e 64 6c 65 rc = CloseHandle
f9c90 28 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 20 7d 77 (pFile->h);. }w
f9ca0 68 69 6c 65 28 20 72 63 3d 3d 30 20 26 26 20 2b hile( rc==0 && +
f9cb0 2b 63 6e 74 20 3c 20 4d 58 5f 43 4c 4f 53 45 5f +cnt < MX_CLOSE_
f9cc0 41 54 54 45 4d 50 54 20 26 26 20 28 53 6c 65 65 ATTEMPT && (Slee
f9cd0 70 28 31 30 30 29 2c 20 31 29 20 29 3b 0a 23 69 p(100), 1) );.#i
f9ce0 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 f SQLITE_OS_WINC
f9cf0 45 0a 23 64 65 66 69 6e 65 20 57 49 4e 43 45 5f E.#define WINCE_
f9d00 44 45 4c 45 54 49 4f 4e 5f 41 54 54 45 4d 50 54 DELETION_ATTEMPT
f9d10 53 20 33 0a 20 20 77 69 6e 63 65 44 65 73 74 72 S 3. winceDestr
f9d20 6f 79 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 oyLock(pFile);.
f9d30 20 69 66 28 20 70 46 69 6c 65 2d 3e 7a 44 65 6c if( pFile->zDel
f9d40 65 74 65 4f 6e 43 6c 6f 73 65 20 29 7b 0a 20 20 eteOnClose ){.
f9d50 20 20 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20 int cnt = 0;.
f9d60 20 20 20 77 68 69 6c 65 28 0a 20 20 20 20 20 20 while(.
f9d70 20 20 20 20 20 44 65 6c 65 74 65 46 69 6c 65 57 DeleteFileW
f9d80 28 70 46 69 6c 65 2d 3e 7a 44 65 6c 65 74 65 4f (pFile->zDeleteO
f9d90 6e 43 6c 6f 73 65 29 3d 3d 30 0a 20 20 20 20 20 nClose)==0.
f9da0 20 20 20 26 26 20 47 65 74 46 69 6c 65 41 74 74 && GetFileAtt
f9db0 72 69 62 75 74 65 73 57 28 70 46 69 6c 65 2d 3e ributesW(pFile->
f9dc0 7a 44 65 6c 65 74 65 4f 6e 43 6c 6f 73 65 29 21 zDeleteOnClose)!
f9dd0 3d 30 78 66 66 66 66 66 66 66 66 20 0a 20 20 20 =0xffffffff .
f9de0 20 20 20 20 20 26 26 20 63 6e 74 2b 2b 20 3c 20 && cnt++ <
f9df0 57 49 4e 43 45 5f 44 45 4c 45 54 49 4f 4e 5f 41 WINCE_DELETION_A
f9e00 54 54 45 4d 50 54 53 0a 20 20 20 20 29 7b 0a 20 TTEMPTS. ){.
f9e10 20 20 20 20 20 20 53 6c 65 65 70 28 31 30 30 29 Sleep(100)
f9e20 3b 20 20 2f 2a 20 57 61 69 74 20 61 20 6c 69 74 ; /* Wait a lit
f9e30 74 6c 65 20 62 65 66 6f 72 65 20 74 72 79 69 6e tle before tryin
f9e40 67 20 61 67 61 69 6e 20 2a 2f 0a 20 20 20 20 7d g again */. }
f9e50 0a 20 20 20 20 66 72 65 65 28 70 46 69 6c 65 2d . free(pFile-
f9e60 3e 7a 44 65 6c 65 74 65 4f 6e 43 6c 6f 73 65 29 >zDeleteOnClose)
f9e70 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 4f ;. }.#endif. O
f9e80 70 65 6e 43 6f 75 6e 74 65 72 28 2d 31 29 3b 0a penCounter(-1);.
f9e90 20 20 72 65 74 75 72 6e 20 72 63 20 3f 20 53 51 return rc ? SQ
f9ea0 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 LITE_OK : SQLITE
f9eb0 5f 49 4f 45 52 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a _IOERR;.}../*.**
f9ec0 20 53 6f 6d 65 20 6d 69 63 72 6f 73 6f 66 74 20 Some microsoft
f9ed0 63 6f 6d 70 69 6c 65 72 73 20 6c 61 63 6b 20 74 compilers lack t
f9ee0 68 69 73 20 64 65 66 69 6e 69 74 69 6f 6e 2e 0a his definition..
f9ef0 2a 2f 0a 23 69 66 6e 64 65 66 20 49 4e 56 41 4c */.#ifndef INVAL
f9f00 49 44 5f 53 45 54 5f 46 49 4c 45 5f 50 4f 49 4e ID_SET_FILE_POIN
f9f10 54 45 52 0a 23 20 64 65 66 69 6e 65 20 49 4e 56 TER.# define INV
f9f20 41 4c 49 44 5f 53 45 54 5f 46 49 4c 45 5f 50 4f ALID_SET_FILE_PO
f9f30 49 4e 54 45 52 20 28 28 44 57 4f 52 44 29 2d 31 INTER ((DWORD)-1
f9f40 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 ).#endif../*.**
f9f50 52 65 61 64 20 64 61 74 61 20 66 72 6f 6d 20 61 Read data from a
f9f60 20 66 69 6c 65 20 69 6e 74 6f 20 61 20 62 75 66 file into a buf
f9f70 66 65 72 2e 20 20 52 65 74 75 72 6e 20 53 51 4c fer. Return SQL
f9f80 49 54 45 5f 4f 4b 20 69 66 20 61 6c 6c 0a 2a 2a ITE_OK if all.**
f9f90 20 62 79 74 65 73 20 77 65 72 65 20 72 65 61 64 bytes were read
f9fa0 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 61 6e successfully an
f9fb0 64 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 69 d SQLITE_IOERR i
f9fc0 66 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 0a f anything goes.
f9fd0 2a 2a 20 77 72 6f 6e 67 2e 0a 2a 2f 0a 73 74 61 ** wrong..*/.sta
f9fe0 74 69 63 20 69 6e 74 20 77 69 6e 52 65 61 64 28 tic int winRead(
f9ff0 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 . sqlite3_file
fa000 2a 69 64 2c 20 20 20 20 20 20 20 20 20 20 2f 2a *id, /*
fa010 20 46 69 6c 65 20 74 6f 20 72 65 61 64 20 66 72 File to read fr
fa020 6f 6d 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 42 om */. void *pB
fa030 75 66 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 uf,
fa040 20 20 20 2f 2a 20 57 72 69 74 65 20 63 6f 6e 74 /* Write cont
fa050 65 6e 74 20 69 6e 74 6f 20 74 68 69 73 20 62 75 ent into this bu
fa060 66 66 65 72 20 2a 2f 0a 20 20 69 6e 74 20 61 6d ffer */. int am
fa070 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t,
fa080 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
fa090 66 20 62 79 74 65 73 20 74 6f 20 72 65 61 64 20 f bytes to read
fa0a0 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 */. sqlite3_int
fa0b0 36 34 20 6f 66 66 73 65 74 20 20 20 20 20 20 20 64 offset
fa0c0 2f 2a 20 42 65 67 69 6e 20 72 65 61 64 69 6e 67 /* Begin reading
fa0d0 20 61 74 20 74 68 69 73 20 6f 66 66 73 65 74 20 at this offset
fa0e0 2a 2f 0a 29 7b 0a 20 20 4c 4f 4e 47 20 75 70 70 */.){. LONG upp
fa0f0 65 72 42 69 74 73 20 3d 20 28 4c 4f 4e 47 29 28 erBits = (LONG)(
fa100 28 6f 66 66 73 65 74 3e 3e 33 32 29 20 26 20 30 (offset>>32) & 0
fa110 78 37 66 66 66 66 66 66 66 29 3b 0a 20 20 4c 4f x7fffffff);. LO
fa120 4e 47 20 6c 6f 77 65 72 42 69 74 73 20 3d 20 28 NG lowerBits = (
fa130 4c 4f 4e 47 29 28 6f 66 66 73 65 74 20 26 20 30 LONG)(offset & 0
fa140 78 66 66 66 66 66 66 66 66 29 3b 0a 20 20 44 57 xffffffff);. DW
fa150 4f 52 44 20 72 63 3b 0a 20 20 77 69 6e 46 69 6c ORD rc;. winFil
fa160 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 e *pFile = (winF
fa170 69 6c 65 2a 29 69 64 3b 0a 20 20 44 57 4f 52 44 ile*)id;. DWORD
fa180 20 65 72 72 6f 72 3b 0a 20 20 44 57 4f 52 44 20 error;. DWORD
fa190 67 6f 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 got;.. assert(
fa1a0 69 64 21 3d 30 20 29 3b 0a 20 20 53 69 6d 75 6c id!=0 );. Simul
fa1b0 61 74 65 49 4f 45 72 72 6f 72 28 72 65 74 75 72 ateIOError(retur
fa1c0 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 n SQLITE_IOERR_R
fa1d0 45 41 44 29 3b 0a 20 20 4f 53 54 52 41 43 45 33 EAD);. OSTRACE3
fa1e0 28 22 52 45 41 44 20 25 64 20 6c 6f 63 6b 3d 25 ("READ %d lock=%
fa1f0 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 d\n", pFile->h,
fa200 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 29 pFile->locktype)
fa210 3b 0a 20 20 72 63 20 3d 20 53 65 74 46 69 6c 65 ;. rc = SetFile
fa220 50 6f 69 6e 74 65 72 28 70 46 69 6c 65 2d 3e 68 Pointer(pFile->h
fa230 2c 20 6c 6f 77 65 72 42 69 74 73 2c 20 26 75 70 , lowerBits, &up
fa240 70 65 72 42 69 74 73 2c 20 46 49 4c 45 5f 42 45 perBits, FILE_BE
fa250 47 49 4e 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d GIN);. if( rc==
fa260 49 4e 56 41 4c 49 44 5f 53 45 54 5f 46 49 4c 45 INVALID_SET_FILE
fa270 5f 50 4f 49 4e 54 45 52 20 26 26 20 28 65 72 72 _POINTER && (err
fa280 6f 72 3d 47 65 74 4c 61 73 74 45 72 72 6f 72 28 or=GetLastError(
fa290 29 29 21 3d 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a ))!=NO_ERROR ){.
fa2a0 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 pFile->lastE
fa2b0 72 72 6e 6f 20 3d 20 65 72 72 6f 72 3b 0a 20 20 rrno = error;.
fa2c0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
fa2d0 46 55 4c 4c 3b 0a 20 20 7d 0a 20 20 69 66 28 20 FULL;. }. if(
fa2e0 21 52 65 61 64 46 69 6c 65 28 70 46 69 6c 65 2d !ReadFile(pFile-
fa2f0 3e 68 2c 20 70 42 75 66 2c 20 61 6d 74 2c 20 26 >h, pBuf, amt, &
fa300 67 6f 74 2c 20 30 29 20 29 7b 0a 20 20 20 20 70 got, 0) ){. p
fa310 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 File->lastErrno
fa320 3d 20 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 = GetLastError()
fa330 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c ;. return SQL
fa340 49 54 45 5f 49 4f 45 52 52 5f 52 45 41 44 3b 0a ITE_IOERR_READ;.
fa350 20 20 7d 0a 20 20 69 66 28 20 67 6f 74 3d 3d 28 }. if( got==(
fa360 44 57 4f 52 44 29 61 6d 74 20 29 7b 0a 20 20 20 DWORD)amt ){.
fa370 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
fa380 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 K;. }else{.
fa390 2f 2a 20 55 6e 72 65 61 64 20 70 61 72 74 73 20 /* Unread parts
fa3a0 6f 66 20 74 68 65 20 62 75 66 66 65 72 20 6d 75 of the buffer mu
fa3b0 73 74 20 62 65 20 7a 65 72 6f 2d 66 69 6c 6c 65 st be zero-fille
fa3c0 64 20 2a 2f 0a 20 20 20 20 6d 65 6d 73 65 74 28 d */. memset(
fa3d0 26 28 28 63 68 61 72 2a 29 70 42 75 66 29 5b 67 &((char*)pBuf)[g
fa3e0 6f 74 5d 2c 20 30 2c 20 61 6d 74 2d 67 6f 74 29 ot], 0, amt-got)
fa3f0 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c ;. return SQL
fa400 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f ITE_IOERR_SHORT_
fa410 52 45 41 44 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a READ;. }.}../*.
fa420 2a 2a 20 57 72 69 74 65 20 64 61 74 61 20 66 72 ** Write data fr
fa430 6f 6d 20 61 20 62 75 66 66 65 72 20 69 6e 74 6f om a buffer into
fa440 20 61 20 66 69 6c 65 2e 20 20 52 65 74 75 72 6e a file. Return
fa450 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 SQLITE_OK on su
fa460 63 63 65 73 73 0a 2a 2a 20 6f 72 20 73 6f 6d 65 ccess.** or some
fa470 20 6f 74 68 65 72 20 65 72 72 6f 72 20 63 6f 64 other error cod
fa480 65 20 6f 6e 20 66 61 69 6c 75 72 65 2e 0a 2a 2f e on failure..*/
fa490 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 57 .static int winW
fa4a0 72 69 74 65 28 0a 20 20 73 71 6c 69 74 65 33 5f rite(. sqlite3_
fa4b0 66 69 6c 65 20 2a 69 64 2c 20 20 20 20 20 20 20 file *id,
fa4c0 20 20 2f 2a 20 46 69 6c 65 20 74 6f 20 77 72 69 /* File to wri
fa4d0 74 65 20 69 6e 74 6f 20 2a 2f 0a 20 20 63 6f 6e te into */. con
fa4e0 73 74 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 20 st void *pBuf,
fa4f0 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 62 79 /* The by
fa500 74 65 73 20 74 6f 20 62 65 20 77 72 69 74 74 65 tes to be writte
fa510 6e 20 2a 2f 0a 20 20 69 6e 74 20 61 6d 74 2c 20 n */. int amt,
fa520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fa530 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 /* Number of by
fa540 74 65 73 20 74 6f 20 77 72 69 74 65 20 2a 2f 0a tes to write */.
fa550 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 sqlite3_int64
fa560 6f 66 66 73 65 74 20 20 20 20 20 20 2f 2a 20 4f offset /* O
fa570 66 66 73 65 74 20 69 6e 74 6f 20 74 68 65 20 66 ffset into the f
fa580 69 6c 65 20 74 6f 20 62 65 67 69 6e 20 77 72 69 ile to begin wri
fa590 74 69 6e 67 20 61 74 20 2a 2f 0a 29 7b 0a 20 20 ting at */.){.
fa5a0 4c 4f 4e 47 20 75 70 70 65 72 42 69 74 73 20 3d LONG upperBits =
fa5b0 20 28 4c 4f 4e 47 29 28 28 6f 66 66 73 65 74 3e (LONG)((offset>
fa5c0 3e 33 32 29 20 26 20 30 78 37 66 66 66 66 66 66 >32) & 0x7ffffff
fa5d0 66 29 3b 0a 20 20 4c 4f 4e 47 20 6c 6f 77 65 72 f);. LONG lower
fa5e0 42 69 74 73 20 3d 20 28 4c 4f 4e 47 29 28 6f 66 Bits = (LONG)(of
fa5f0 66 73 65 74 20 26 20 30 78 66 66 66 66 66 66 66 fset & 0xfffffff
fa600 66 29 3b 0a 20 20 44 57 4f 52 44 20 72 63 3b 0a f);. DWORD rc;.
fa610 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 winFile *pFile
fa620 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b = (winFile*)id;
fa630 0a 20 20 44 57 4f 52 44 20 65 72 72 6f 72 3b 0a . DWORD error;.
fa640 20 20 44 57 4f 52 44 20 77 72 6f 74 65 20 3d 20 DWORD wrote =
fa650 30 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 64 0;.. assert( id
fa660 21 3d 30 20 29 3b 0a 20 20 53 69 6d 75 6c 61 74 !=0 );. Simulat
fa670 65 49 4f 45 72 72 6f 72 28 72 65 74 75 72 6e 20 eIOError(return
fa680 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 57 52 49 SQLITE_IOERR_WRI
fa690 54 45 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 44 TE);. SimulateD
fa6a0 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 72 65 74 iskfullError(ret
fa6b0 75 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 29 urn SQLITE_FULL)
fa6c0 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 22 57 52 ;. OSTRACE3("WR
fa6d0 49 54 45 20 25 64 20 6c 6f 63 6b 3d 25 64 5c 6e ITE %d lock=%d\n
fa6e0 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 70 46 69 ", pFile->h, pFi
fa6f0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 29 3b 0a 20 le->locktype);.
fa700 20 72 63 20 3d 20 53 65 74 46 69 6c 65 50 6f 69 rc = SetFilePoi
fa710 6e 74 65 72 28 70 46 69 6c 65 2d 3e 68 2c 20 6c nter(pFile->h, l
fa720 6f 77 65 72 42 69 74 73 2c 20 26 75 70 70 65 72 owerBits, &upper
fa730 42 69 74 73 2c 20 46 49 4c 45 5f 42 45 47 49 4e Bits, FILE_BEGIN
fa740 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 49 4e 56 );. if( rc==INV
fa750 41 4c 49 44 5f 53 45 54 5f 46 49 4c 45 5f 50 4f ALID_SET_FILE_PO
fa760 49 4e 54 45 52 20 26 26 20 28 65 72 72 6f 72 3d INTER && (error=
fa770 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 29 21 GetLastError())!
fa780 3d 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 =NO_ERROR ){.
fa790 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e pFile->lastErrn
fa7a0 6f 20 3d 20 65 72 72 6f 72 3b 0a 20 20 20 20 72 o = error;. r
fa7b0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c eturn SQLITE_FUL
fa7c0 4c 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 L;. }. assert(
fa7d0 20 61 6d 74 3e 30 20 29 3b 0a 20 20 77 68 69 6c amt>0 );. whil
fa7e0 65 28 0a 20 20 20 20 20 61 6d 74 3e 30 0a 20 20 e(. amt>0.
fa7f0 20 20 20 26 26 20 28 72 63 20 3d 20 57 72 69 74 && (rc = Writ
fa800 65 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 eFile(pFile->h,
fa810 70 42 75 66 2c 20 61 6d 74 2c 20 26 77 72 6f 74 pBuf, amt, &wrot
fa820 65 2c 20 30 29 29 21 3d 30 0a 20 20 20 20 20 26 e, 0))!=0. &
fa830 26 20 77 72 6f 74 65 3e 30 0a 20 20 29 7b 0a 20 & wrote>0. ){.
fa840 20 20 20 61 6d 74 20 2d 3d 20 77 72 6f 74 65 3b amt -= wrote;
fa850 0a 20 20 20 20 70 42 75 66 20 3d 20 26 28 28 63 . pBuf = &((c
fa860 68 61 72 2a 29 70 42 75 66 29 5b 77 72 6f 74 65 har*)pBuf)[wrote
fa870 5d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 72 63 ];. }. if( !rc
fa880 20 7c 7c 20 61 6d 74 3e 28 69 6e 74 29 77 72 6f || amt>(int)wro
fa890 74 65 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d te ){. pFile-
fa8a0 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 47 65 74 >lastErrno = Get
fa8b0 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20 20 20 LastError();.
fa8c0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 46 return SQLITE_F
fa8d0 55 4c 4c 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 ULL;. }. retur
fa8e0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
fa8f0 2f 2a 0a 2a 2a 20 54 72 75 6e 63 61 74 65 20 61 /*.** Truncate a
fa900 6e 20 6f 70 65 6e 20 66 69 6c 65 20 74 6f 20 61 n open file to a
fa910 20 73 70 65 63 69 66 69 65 64 20 73 69 7a 65 0a specified size.
fa920 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 */.static int wi
fa930 6e 54 72 75 6e 63 61 74 65 28 73 71 6c 69 74 65 nTruncate(sqlite
fa940 33 5f 66 69 6c 65 20 2a 69 64 2c 20 73 71 6c 69 3_file *id, sqli
fa950 74 65 33 5f 69 6e 74 36 34 20 6e 42 79 74 65 29 te3_int64 nByte)
fa960 7b 0a 20 20 4c 4f 4e 47 20 75 70 70 65 72 42 69 {. LONG upperBi
fa970 74 73 20 3d 20 28 4c 4f 4e 47 29 28 28 6e 42 79 ts = (LONG)((nBy
fa980 74 65 3e 3e 33 32 29 20 26 20 30 78 37 66 66 66 te>>32) & 0x7fff
fa990 66 66 66 66 29 3b 0a 20 20 4c 4f 4e 47 20 6c 6f ffff);. LONG lo
fa9a0 77 65 72 42 69 74 73 20 3d 20 28 4c 4f 4e 47 29 werBits = (LONG)
fa9b0 28 6e 42 79 74 65 20 26 20 30 78 66 66 66 66 66 (nByte & 0xfffff
fa9c0 66 66 66 29 3b 0a 20 20 44 57 4f 52 44 20 72 63 fff);. DWORD rc
fa9d0 3b 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 ;. winFile *pFi
fa9e0 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 le = (winFile*)i
fa9f0 64 3b 0a 20 20 44 57 4f 52 44 20 65 72 72 6f 72 d;. DWORD error
faa00 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 64 21 ;.. assert( id!
faa10 3d 30 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 33 =0 );. OSTRACE3
faa20 28 22 54 52 55 4e 43 41 54 45 20 25 64 20 25 6c ("TRUNCATE %d %l
faa30 6c 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c ld\n", pFile->h,
faa40 20 6e 42 79 74 65 29 3b 0a 20 20 53 69 6d 75 6c nByte);. Simul
faa50 61 74 65 49 4f 45 72 72 6f 72 28 72 65 74 75 72 ateIOError(retur
faa60 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 54 n SQLITE_IOERR_T
faa70 52 55 4e 43 41 54 45 29 3b 0a 20 20 72 63 20 3d RUNCATE);. rc =
faa80 20 53 65 74 46 69 6c 65 50 6f 69 6e 74 65 72 28 SetFilePointer(
faa90 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 77 65 72 42 pFile->h, lowerB
faaa0 69 74 73 2c 20 26 75 70 70 65 72 42 69 74 73 2c its, &upperBits,
faab0 20 46 49 4c 45 5f 42 45 47 49 4e 29 3b 0a 20 20 FILE_BEGIN);.
faac0 69 66 28 20 72 63 3d 3d 49 4e 56 41 4c 49 44 5f if( rc==INVALID_
faad0 53 45 54 5f 46 49 4c 45 5f 50 4f 49 4e 54 45 52 SET_FILE_POINTER
faae0 20 26 26 20 28 65 72 72 6f 72 3d 47 65 74 4c 61 && (error=GetLa
faaf0 73 74 45 72 72 6f 72 28 29 29 21 3d 4e 4f 5f 45 stError())!=NO_E
fab00 52 52 4f 52 20 29 7b 0a 20 20 20 20 70 46 69 6c RROR ){. pFil
fab10 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 e->lastErrno = e
fab20 72 72 6f 72 3b 0a 20 20 20 20 72 65 74 75 72 6e rror;. return
fab30 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 54 52 SQLITE_IOERR_TR
fab40 55 4e 43 41 54 45 3b 0a 20 20 7d 0a 20 20 2f 2a UNCATE;. }. /*
fab50 20 53 65 74 45 6e 64 4f 66 46 69 6c 65 20 77 69 SetEndOfFile wi
fab60 6c 6c 20 66 61 69 6c 20 69 66 20 6e 42 79 74 65 ll fail if nByte
fab70 20 69 73 20 6e 65 67 61 74 69 76 65 20 2a 2f 0a is negative */.
fab80 20 20 69 66 28 20 21 53 65 74 45 6e 64 4f 66 46 if( !SetEndOfF
fab90 69 6c 65 28 70 46 69 6c 65 2d 3e 68 29 20 29 7b ile(pFile->h) ){
faba0 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 . pFile->last
fabb0 45 72 72 6e 6f 20 3d 20 47 65 74 4c 61 73 74 45 Errno = GetLastE
fabc0 72 72 6f 72 28 29 3b 0a 20 20 20 20 72 65 74 75 rror();. retu
fabd0 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f rn SQLITE_IOERR_
fabe0 54 52 55 4e 43 41 54 45 3b 0a 20 20 7d 0a 20 20 TRUNCATE;. }.
fabf0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
fac00 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 ;.}..#ifdef SQLI
fac10 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 43 6f TE_TEST./*.** Co
fac20 75 6e 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f unt the number o
fac30 66 20 66 75 6c 6c 73 79 6e 63 73 20 61 6e 64 20 f fullsyncs and
fac40 6e 6f 72 6d 61 6c 20 73 79 6e 63 73 2e 20 20 54 normal syncs. T
fac50 68 69 73 20 69 73 20 75 73 65 64 20 74 6f 20 74 his is used to t
fac60 65 73 74 0a 2a 2a 20 74 68 61 74 20 73 79 6e 63 est.** that sync
fac70 73 20 61 6e 64 20 66 75 6c 6c 73 79 6e 63 73 20 s and fullsyncs
fac80 61 72 65 20 6f 63 63 75 72 69 6e 67 20 61 74 20 are occuring at
fac90 74 68 65 20 72 69 67 68 74 20 74 69 6d 65 73 2e the right times.
faca0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
facb0 6e 74 20 73 71 6c 69 74 65 33 5f 73 79 6e 63 5f nt sqlite3_sync_
facc0 63 6f 75 6e 74 20 3d 20 30 3b 0a 53 51 4c 49 54 count = 0;.SQLIT
facd0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
face0 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74 3_fullsync_count
facf0 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a = 0;.#endif../*
fad00 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 61 6c .** Make sure al
fad10 6c 20 77 72 69 74 65 73 20 74 6f 20 61 20 70 61 l writes to a pa
fad20 72 74 69 63 75 6c 61 72 20 66 69 6c 65 20 61 72 rticular file ar
fad30 65 20 63 6f 6d 6d 69 74 74 65 64 20 74 6f 20 64 e committed to d
fad40 69 73 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 isk..*/.static i
fad50 6e 74 20 77 69 6e 53 79 6e 63 28 73 71 6c 69 74 nt winSync(sqlit
fad60 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 e3_file *id, int
fad70 20 66 6c 61 67 73 29 7b 0a 23 69 66 6e 64 65 66 flags){.#ifndef
fad80 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 0a SQLITE_NO_SYNC.
fad90 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 winFile *pFile
fada0 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b = (winFile*)id;
fadb0 0a 0a 20 20 61 73 73 65 72 74 28 20 69 64 21 3d .. assert( id!=
fadc0 30 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 0 );. OSTRACE3(
fadd0 22 53 59 4e 43 20 25 64 20 6c 6f 63 6b 3d 25 64 "SYNC %d lock=%d
fade0 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 70 \n", pFile->h, p
fadf0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 29 3b File->locktype);
fae00 0a 23 65 6c 73 65 0a 20 20 55 4e 55 53 45 44 5f .#else. UNUSED_
fae10 50 41 52 41 4d 45 54 45 52 28 69 64 29 3b 0a 23 PARAMETER(id);.#
fae20 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51 endif.#ifndef SQ
fae30 4c 49 54 45 5f 54 45 53 54 0a 20 20 55 4e 55 53 LITE_TEST. UNUS
fae40 45 44 5f 50 41 52 41 4d 45 54 45 52 28 66 6c 61 ED_PARAMETER(fla
fae50 67 73 29 3b 0a 23 65 6c 73 65 0a 20 20 69 66 28 gs);.#else. if(
fae60 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f flags & SQLITE_
fae70 53 59 4e 43 5f 46 55 4c 4c 20 29 7b 0a 20 20 20 SYNC_FULL ){.
fae80 20 73 71 6c 69 74 65 33 5f 66 75 6c 6c 73 79 6e sqlite3_fullsyn
fae90 63 5f 63 6f 75 6e 74 2b 2b 3b 0a 20 20 7d 0a 20 c_count++;. }.
faea0 20 73 71 6c 69 74 65 33 5f 73 79 6e 63 5f 63 6f sqlite3_sync_co
faeb0 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 unt++;.#endif.
faec0 2f 2a 20 49 66 20 77 65 20 63 6f 6d 70 69 6c 65 /* If we compile
faed0 64 20 77 69 74 68 20 74 68 65 20 53 51 4c 49 54 d with the SQLIT
faee0 45 5f 4e 4f 5f 53 59 4e 43 20 66 6c 61 67 2c 20 E_NO_SYNC flag,
faef0 74 68 65 6e 20 73 79 6e 63 69 6e 67 20 69 73 20 then syncing is
faf00 61 0a 20 20 2a 2a 20 6e 6f 2d 6f 70 0a 20 20 2a a. ** no-op. *
faf10 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
faf20 4e 4f 5f 53 59 4e 43 0a 20 20 20 20 72 65 74 75 NO_SYNC. retu
faf30 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 23 65 rn SQLITE_OK;.#e
faf40 6c 73 65 0a 20 20 69 66 28 20 46 6c 75 73 68 46 lse. if( FlushF
faf50 69 6c 65 42 75 66 66 65 72 73 28 70 46 69 6c 65 ileBuffers(pFile
faf60 2d 3e 68 29 20 29 7b 0a 20 20 20 20 72 65 74 75 ->h) ){. retu
faf70 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
faf80 7d 65 6c 73 65 7b 0a 20 20 20 20 70 46 69 6c 65 }else{. pFile
faf90 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 47 65 ->lastErrno = Ge
fafa0 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20 20 tLastError();.
fafb0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
fafc0 49 4f 45 52 52 3b 0a 20 20 7d 0a 23 65 6e 64 69 IOERR;. }.#endi
fafd0 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 74 65 72 f.}../*.** Deter
fafe0 6d 69 6e 65 20 74 68 65 20 63 75 72 72 65 6e 74 mine the current
faff0 20 73 69 7a 65 20 6f 66 20 61 20 66 69 6c 65 20 size of a file
fb000 69 6e 20 62 79 74 65 73 0a 2a 2f 0a 73 74 61 74 in bytes.*/.stat
fb010 69 63 20 69 6e 74 20 77 69 6e 46 69 6c 65 53 69 ic int winFileSi
fb020 7a 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 ze(sqlite3_file
fb030 2a 69 64 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 *id, sqlite3_int
fb040 36 34 20 2a 70 53 69 7a 65 29 7b 0a 20 20 44 57 64 *pSize){. DW
fb050 4f 52 44 20 75 70 70 65 72 42 69 74 73 3b 0a 20 ORD upperBits;.
fb060 20 44 57 4f 52 44 20 6c 6f 77 65 72 42 69 74 73 DWORD lowerBits
fb070 3b 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 ;. winFile *pFi
fb080 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 le = (winFile*)i
fb090 64 3b 0a 20 20 44 57 4f 52 44 20 65 72 72 6f 72 d;. DWORD error
fb0a0 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 64 21 ;.. assert( id!
fb0b0 3d 30 20 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 =0 );. Simulate
fb0c0 49 4f 45 72 72 6f 72 28 72 65 74 75 72 6e 20 53 IOError(return S
fb0d0 51 4c 49 54 45 5f 49 4f 45 52 52 5f 46 53 54 41 QLITE_IOERR_FSTA
fb0e0 54 29 3b 0a 20 20 6c 6f 77 65 72 42 69 74 73 20 T);. lowerBits
fb0f0 3d 20 47 65 74 46 69 6c 65 53 69 7a 65 28 70 46 = GetFileSize(pF
fb100 69 6c 65 2d 3e 68 2c 20 26 75 70 70 65 72 42 69 ile->h, &upperBi
fb110 74 73 29 3b 0a 20 20 69 66 28 20 20 20 28 6c 6f ts);. if( (lo
fb120 77 65 72 42 69 74 73 20 3d 3d 20 49 4e 56 41 4c werBits == INVAL
fb130 49 44 5f 46 49 4c 45 5f 53 49 5a 45 29 0a 20 20 ID_FILE_SIZE).
fb140 20 20 20 26 26 20 28 28 65 72 72 6f 72 20 3d 20 && ((error =
fb150 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 29 20 GetLastError())
fb160 21 3d 20 4e 4f 5f 45 52 52 4f 52 29 20 29 0a 20 != NO_ERROR) ).
fb170 20 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 {. pFile->la
fb180 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6f 72 3b stErrno = error;
fb190 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
fb1a0 54 45 5f 49 4f 45 52 52 5f 46 53 54 41 54 3b 0a TE_IOERR_FSTAT;.
fb1b0 20 20 7d 0a 20 20 2a 70 53 69 7a 65 20 3d 20 28 }. *pSize = (
fb1c0 28 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 ((sqlite3_int64)
fb1d0 75 70 70 65 72 42 69 74 73 29 3c 3c 33 32 29 20 upperBits)<<32)
fb1e0 2b 20 6c 6f 77 65 72 42 69 74 73 3b 0a 20 20 72 + lowerBits;. r
fb1f0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
fb200 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 4f 43 4b 46 49 .}../*.** LOCKFI
fb210 4c 45 5f 46 41 49 4c 5f 49 4d 4d 45 44 49 41 54 LE_FAIL_IMMEDIAT
fb220 45 4c 59 20 69 73 20 75 6e 64 65 66 69 6e 65 64 ELY is undefined
fb230 20 6f 6e 20 73 6f 6d 65 20 57 69 6e 64 6f 77 73 on some Windows
fb240 20 73 79 73 74 65 6d 73 2e 0a 2a 2f 0a 23 69 66 systems..*/.#if
fb250 6e 64 65 66 20 4c 4f 43 4b 46 49 4c 45 5f 46 41 ndef LOCKFILE_FA
fb260 49 4c 5f 49 4d 4d 45 44 49 41 54 45 4c 59 0a 23 IL_IMMEDIATELY.#
fb270 20 64 65 66 69 6e 65 20 4c 4f 43 4b 46 49 4c 45 define LOCKFILE
fb280 5f 46 41 49 4c 5f 49 4d 4d 45 44 49 41 54 45 4c _FAIL_IMMEDIATEL
fb290 59 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a Y 1.#endif../*.*
fb2a0 2a 20 41 63 71 75 69 72 65 20 61 20 72 65 61 64 * Acquire a read
fb2b0 65 72 20 6c 6f 63 6b 2e 0a 2a 2a 20 44 69 66 66 er lock..** Diff
fb2c0 65 72 65 6e 74 20 41 50 49 20 72 6f 75 74 69 6e erent API routin
fb2d0 65 73 20 61 72 65 20 63 61 6c 6c 65 64 20 64 65 es are called de
fb2e0 70 65 6e 64 69 6e 67 20 6f 6e 20 77 68 65 74 68 pending on wheth
fb2f0 65 72 20 6f 72 20 6e 6f 74 20 74 68 69 73 0a 2a er or not this.*
fb300 2a 20 69 73 20 57 69 6e 39 35 20 6f 72 20 57 69 * is Win95 or Wi
fb310 6e 4e 54 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 nNT..*/.static i
fb320 6e 74 20 67 65 74 52 65 61 64 4c 6f 63 6b 28 77 nt getReadLock(w
fb330 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a inFile *pFile){.
fb340 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 69 66 28 int res;. if(
fb350 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 4f isNT() ){. O
fb360 56 45 52 4c 41 50 50 45 44 20 6f 76 6c 70 3b 0a VERLAPPED ovlp;.
fb370 20 20 20 20 6f 76 6c 70 2e 4f 66 66 73 65 74 20 ovlp.Offset
fb380 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54 3b 0a = SHARED_FIRST;.
fb390 20 20 20 20 6f 76 6c 70 2e 4f 66 66 73 65 74 48 ovlp.OffsetH
fb3a0 69 67 68 20 3d 20 30 3b 0a 20 20 20 20 6f 76 6c igh = 0;. ovl
fb3b0 70 2e 68 45 76 65 6e 74 20 3d 20 30 3b 0a 20 20 p.hEvent = 0;.
fb3c0 20 20 72 65 73 20 3d 20 4c 6f 63 6b 46 69 6c 65 res = LockFile
fb3d0 45 78 28 70 46 69 6c 65 2d 3e 68 2c 20 4c 4f 43 Ex(pFile->h, LOC
fb3e0 4b 46 49 4c 45 5f 46 41 49 4c 5f 49 4d 4d 45 44 KFILE_FAIL_IMMED
fb3f0 49 41 54 45 4c 59 2c 0a 20 20 20 20 20 20 20 20 IATELY,.
fb400 20 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 20 0,
fb410 53 48 41 52 45 44 5f 53 49 5a 45 2c 20 30 2c 20 SHARED_SIZE, 0,
fb420 26 6f 76 6c 70 29 3b 0a 2f 2a 20 69 73 4e 54 28 &ovlp);./* isNT(
fb430 29 20 69 73 20 31 20 69 66 20 53 51 4c 49 54 45 ) is 1 if SQLITE
fb440 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 6f _OS_WINCE==1, so
fb450 20 74 68 69 73 20 65 6c 73 65 20 69 73 20 6e 65 this else is ne
fb460 76 65 72 20 65 78 65 63 75 74 65 64 2e 20 0a 2a ver executed. .*
fb470 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f /.#if SQLITE_OS_
fb480 57 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 WINCE==0. }else
fb490 7b 0a 20 20 20 20 69 6e 74 20 6c 6b 3b 0a 20 20 {. int lk;.
fb4a0 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d sqlite3_random
fb4b0 6e 65 73 73 28 73 69 7a 65 6f 66 28 6c 6b 29 2c ness(sizeof(lk),
fb4c0 20 26 6c 6b 29 3b 0a 20 20 20 20 70 46 69 6c 65 &lk);. pFile
fb4d0 2d 3e 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 65 ->sharedLockByte
fb4e0 20 3d 20 28 73 68 6f 72 74 29 28 28 6c 6b 20 26 = (short)((lk &
fb4f0 20 30 78 37 66 66 66 66 66 66 66 29 25 28 53 48 0x7fffffff)%(SH
fb500 41 52 45 44 5f 53 49 5a 45 20 2d 20 31 29 29 3b ARED_SIZE - 1));
fb510 0a 20 20 20 20 72 65 73 20 3d 20 4c 6f 63 6b 46 . res = LockF
fb520 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 53 48 ile(pFile->h, SH
fb530 41 52 45 44 5f 46 49 52 53 54 2b 70 46 69 6c 65 ARED_FIRST+pFile
fb540 2d 3e 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 65 ->sharedLockByte
fb550 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 23 65 6e 64 , 0, 1, 0);.#end
fb560 69 66 0a 20 20 7d 0a 20 20 69 66 28 20 72 65 73 if. }. if( res
fb570 20 3d 3d 20 30 20 29 7b 0a 20 20 20 20 70 46 69 == 0 ){. pFi
fb580 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 le->lastErrno =
fb590 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a GetLastError();.
fb5a0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 65 73 }. return res
fb5b0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 64 6f 20 ;.}../*.** Undo
fb5c0 61 20 72 65 61 64 6c 6f 63 6b 0a 2a 2f 0a 73 74 a readlock.*/.st
fb5d0 61 74 69 63 20 69 6e 74 20 75 6e 6c 6f 63 6b 52 atic int unlockR
fb5e0 65 61 64 4c 6f 63 6b 28 77 69 6e 46 69 6c 65 20 eadLock(winFile
fb5f0 2a 70 46 69 6c 65 29 7b 0a 20 20 69 6e 74 20 72 *pFile){. int r
fb600 65 73 3b 0a 20 20 69 66 28 20 69 73 4e 54 28 29 es;. if( isNT()
fb610 20 29 7b 0a 20 20 20 20 72 65 73 20 3d 20 55 6e ){. res = Un
fb620 6c 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e lockFile(pFile->
fb630 68 2c 20 53 48 41 52 45 44 5f 46 49 52 53 54 2c h, SHARED_FIRST,
fb640 20 30 2c 20 53 48 41 52 45 44 5f 53 49 5a 45 2c 0, SHARED_SIZE,
fb650 20 30 29 3b 0a 2f 2a 20 69 73 4e 54 28 29 20 69 0);./* isNT() i
fb660 73 20 31 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 s 1 if SQLITE_OS
fb670 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 68 _WINCE==1, so th
fb680 69 73 20 65 6c 73 65 20 69 73 20 6e 65 76 65 72 is else is never
fb690 20 65 78 65 63 75 74 65 64 2e 20 0a 2a 2f 0a 23 executed. .*/.#
fb6a0 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e if SQLITE_OS_WIN
fb6b0 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 CE==0. }else{.
fb6c0 20 20 20 72 65 73 20 3d 20 55 6e 6c 6f 63 6b 46 res = UnlockF
fb6d0 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 53 48 ile(pFile->h, SH
fb6e0 41 52 45 44 5f 46 49 52 53 54 20 2b 20 70 46 69 ARED_FIRST + pFi
fb6f0 6c 65 2d 3e 73 68 61 72 65 64 4c 6f 63 6b 42 79 le->sharedLockBy
fb700 74 65 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 23 65 te, 0, 1, 0);.#e
fb710 6e 64 69 66 0a 20 20 7d 0a 20 20 69 66 28 20 72 ndif. }. if( r
fb720 65 73 20 3d 3d 20 30 20 29 7b 0a 20 20 20 20 70 es == 0 ){. p
fb730 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 File->lastErrno
fb740 3d 20 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 = GetLastError()
fb750 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
fb760 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 es;.}../*.** Loc
fb770 6b 20 74 68 65 20 66 69 6c 65 20 77 69 74 68 20 k the file with
fb780 74 68 65 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 the lock specifi
fb790 65 64 20 62 79 20 70 61 72 61 6d 65 74 65 72 20 ed by parameter
fb7a0 6c 6f 63 6b 74 79 70 65 20 2d 20 6f 6e 65 0a 2a locktype - one.*
fb7b0 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 * of the followi
fb7c0 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 ng:.**.** (1
fb7d0 29 20 53 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a ) SHARED_LOCK.**
fb7e0 20 20 20 20 20 28 32 29 20 52 45 53 45 52 56 45 (2) RESERVE
fb7f0 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33 D_LOCK.** (3
fb800 29 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a ) PENDING_LOCK.*
fb810 2a 20 20 20 20 20 28 34 29 20 45 58 43 4c 55 53 * (4) EXCLUS
fb820 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 IVE_LOCK.**.** S
fb830 6f 6d 65 74 69 6d 65 73 20 77 68 65 6e 20 72 65 ometimes when re
fb840 71 75 65 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 questing one loc
fb850 6b 20 73 74 61 74 65 2c 20 61 64 64 69 74 69 6f k state, additio
fb860 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a nal lock states.
fb870 2a 2a 20 61 72 65 20 69 6e 73 65 72 74 65 64 20 ** are inserted
fb880 69 6e 20 62 65 74 77 65 65 6e 2e 20 20 54 68 65 in between. The
fb890 20 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 74 20 66 locking might f
fb8a0 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 ail on one of th
fb8b0 65 20 6c 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73 e later.** trans
fb8c0 69 74 69 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74 itions leaving t
fb8d0 68 65 20 6c 6f 63 6b 20 73 74 61 74 65 20 64 69 he lock state di
fb8e0 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 77 68 61 fferent from wha
fb8f0 74 20 69 74 20 73 74 61 72 74 65 64 20 62 75 74 t it started but
fb900 0a 2a 2a 20 73 74 69 6c 6c 20 73 68 6f 72 74 20 .** still short
fb910 6f 66 20 69 74 73 20 67 6f 61 6c 2e 20 20 54 68 of its goal. Th
fb920 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72 e following char
fb930 74 20 73 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f t shows the allo
fb940 77 65 64 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f wed.** transitio
fb950 6e 73 20 61 6e 64 20 74 68 65 20 69 6e 73 65 72 ns and the inser
fb960 74 65 64 20 69 6e 74 65 72 6d 65 64 69 61 74 65 ted intermediate
fb970 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 states:.**.**
fb980 20 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 UNLOCKED -> SH
fb990 41 52 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 ARED.** SHARE
fb9a0 44 20 2d 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a D -> RESERVED.**
fb9b0 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20 28 50 SHARED -> (P
fb9c0 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 ENDING) -> EXCLU
fb9d0 53 49 56 45 0a 2a 2a 20 20 20 20 52 45 53 45 52 SIVE.** RESER
fb9e0 56 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 VED -> (PENDING)
fb9f0 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a -> EXCLUSIVE.**
fba00 20 20 20 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45 PENDING -> E
fba10 58 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 XCLUSIVE.**.** T
fba20 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c his routine will
fba30 20 6f 6e 6c 79 20 69 6e 63 72 65 61 73 65 20 61 only increase a
fba40 20 6c 6f 63 6b 2e 20 20 54 68 65 20 77 69 6e 55 lock. The winU
fba50 6e 6c 6f 63 6b 28 29 20 72 6f 75 74 69 6e 65 0a nlock() routine.
fba60 2a 2a 20 65 72 61 73 65 73 20 61 6c 6c 20 6c 6f ** erases all lo
fba70 63 6b 73 20 61 74 20 6f 6e 63 65 20 61 6e 64 20 cks at once and
fba80 72 65 74 75 72 6e 73 20 75 73 20 69 6d 6d 65 64 returns us immed
fba90 69 61 74 65 6c 79 20 74 6f 20 6c 6f 63 6b 69 6e iately to lockin
fbaa0 67 20 6c 65 76 65 6c 20 30 2e 0a 2a 2a 20 49 74 g level 0..** It
fbab0 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 is not possible
fbac0 20 74 6f 20 6c 6f 77 65 72 20 74 68 65 20 6c 6f to lower the lo
fbad0 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 65 20 cking level one
fbae0 73 74 65 70 20 61 74 20 61 20 74 69 6d 65 2e 20 step at a time.
fbaf0 20 59 6f 75 0a 2a 2a 20 6d 75 73 74 20 67 6f 20 You.** must go
fbb00 73 74 72 61 69 67 68 74 20 74 6f 20 6c 6f 63 6b straight to lock
fbb10 69 6e 67 20 6c 65 76 65 6c 20 30 2e 0a 2a 2f 0a ing level 0..*/.
fbb20 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 4c 6f static int winLo
fbb30 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 ck(sqlite3_file
fbb40 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 *id, int locktyp
fbb50 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 e){. int rc = S
fbb60 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 2f 2a 20 QLITE_OK; /*
fbb70 52 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d Return code from
fbb80 20 73 75 62 72 6f 75 74 69 6e 65 73 20 2a 2f 0a subroutines */.
fbb90 20 20 69 6e 74 20 72 65 73 20 3d 20 31 3b 20 20 int res = 1;
fbba0 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 /* Resu
fbbb0 6c 74 20 6f 66 20 61 20 77 69 6e 64 6f 77 73 20 lt of a windows
fbbc0 6c 6f 63 6b 20 63 61 6c 6c 20 2a 2f 0a 20 20 69 lock call */. i
fbbd0 6e 74 20 6e 65 77 4c 6f 63 6b 74 79 70 65 3b 20 nt newLocktype;
fbbe0 20 20 20 20 20 20 2f 2a 20 53 65 74 20 70 46 69 /* Set pFi
fbbf0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 74 6f 20 le->locktype to
fbc00 74 68 69 73 20 76 61 6c 75 65 20 62 65 66 6f 72 this value befor
fbc10 65 20 65 78 69 74 69 6e 67 20 2a 2f 0a 20 20 69 e exiting */. i
fbc20 6e 74 20 67 6f 74 50 65 6e 64 69 6e 67 4c 6f 63 nt gotPendingLoc
fbc30 6b 20 3d 20 30 3b 2f 2a 20 54 72 75 65 20 69 66 k = 0;/* True if
fbc40 20 77 65 20 61 63 71 75 69 72 65 64 20 61 20 50 we acquired a P
fbc50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 74 68 69 73 ENDING lock this
fbc60 20 74 69 6d 65 20 2a 2f 0a 20 20 77 69 6e 46 69 time */. winFi
fbc70 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e le *pFile = (win
fbc80 46 69 6c 65 2a 29 69 64 3b 0a 20 20 44 57 4f 52 File*)id;. DWOR
fbc90 44 20 65 72 72 6f 72 20 3d 20 4e 4f 5f 45 52 52 D error = NO_ERR
fbca0 4f 52 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 OR;.. assert( i
fbcb0 64 21 3d 30 20 29 3b 0a 20 20 4f 53 54 52 41 43 d!=0 );. OSTRAC
fbcc0 45 35 28 22 4c 4f 43 4b 20 25 64 20 25 64 20 77 E5("LOCK %d %d w
fbcd0 61 73 20 25 64 28 25 64 29 5c 6e 22 2c 0a 20 20 as %d(%d)\n",.
fbce0 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 68 pFile->h
fbcf0 2c 20 6c 6f 63 6b 74 79 70 65 2c 20 70 46 69 6c , locktype, pFil
fbd00 65 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 70 46 69 e->locktype, pFi
fbd10 6c 65 2d 3e 73 68 61 72 65 64 4c 6f 63 6b 42 79 le->sharedLockBy
fbd20 74 65 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 te);.. /* If th
fbd30 65 72 65 20 69 73 20 61 6c 72 65 61 64 79 20 61 ere is already a
fbd40 20 6c 6f 63 6b 20 6f 66 20 74 68 69 73 20 74 79 lock of this ty
fbd50 70 65 20 6f 72 20 6d 6f 72 65 20 72 65 73 74 72 pe or more restr
fbd60 69 63 74 69 76 65 20 6f 6e 20 74 68 65 0a 20 20 ictive on the.
fbd70 2a 2a 20 4f 73 46 69 6c 65 2c 20 64 6f 20 6e 6f ** OsFile, do no
fbd80 74 68 69 6e 67 2e 20 44 6f 6e 27 74 20 75 73 65 thing. Don't use
fbd90 20 74 68 65 20 65 6e 64 5f 6c 6f 63 6b 3a 20 65 the end_lock: e
fbda0 78 69 74 20 70 61 74 68 2c 20 61 73 0a 20 20 2a xit path, as. *
fbdb0 2a 20 73 71 6c 69 74 65 33 4f 73 45 6e 74 65 72 * sqlite3OsEnter
fbdc0 4d 75 74 65 78 28 29 20 68 61 73 6e 27 74 20 62 Mutex() hasn't b
fbdd0 65 65 6e 20 63 61 6c 6c 65 64 20 79 65 74 2e 0a een called yet..
fbde0 20 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 */. if( pFile
fbdf0 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 6c 6f 63 6b ->locktype>=lock
fbe00 74 79 70 65 20 29 7b 0a 20 20 20 20 72 65 74 75 type ){. retu
fbe10 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
fbe20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 }.. /* Make sur
fbe30 65 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 73 65 e the locking se
fbe40 71 75 65 6e 63 65 20 69 73 20 63 6f 72 72 65 63 quence is correc
fbe50 74 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 t. */. assert(
fbe60 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
fbe70 21 3d 4e 4f 5f 4c 4f 43 4b 20 7c 7c 20 6c 6f 63 !=NO_LOCK || loc
fbe80 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f ktype==SHARED_LO
fbe90 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 CK );. assert(
fbea0 6c 6f 63 6b 74 79 70 65 21 3d 50 45 4e 44 49 4e locktype!=PENDIN
fbeb0 47 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 G_LOCK );. asse
fbec0 72 74 28 20 6c 6f 63 6b 74 79 70 65 21 3d 52 45 rt( locktype!=RE
fbed0 53 45 52 56 45 44 5f 4c 4f 43 4b 20 7c 7c 20 70 SERVED_LOCK || p
fbee0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d File->locktype==
fbef0 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 0a SHARED_LOCK );..
fbf00 20 20 2f 2a 20 4c 6f 63 6b 20 74 68 65 20 50 45 /* Lock the PE
fbf10 4e 44 49 4e 47 5f 4c 4f 43 4b 20 62 79 74 65 20 NDING_LOCK byte
fbf20 69 66 20 77 65 20 6e 65 65 64 20 74 6f 20 61 63 if we need to ac
fbf30 71 75 69 72 65 20 61 20 50 45 4e 44 49 4e 47 20 quire a PENDING
fbf40 6c 6f 63 6b 20 6f 72 0a 20 20 2a 2a 20 61 20 53 lock or. ** a S
fbf50 48 41 52 45 44 20 6c 6f 63 6b 2e 20 20 49 66 20 HARED lock. If
fbf60 77 65 20 61 72 65 20 61 63 71 75 69 72 69 6e 67 we are acquiring
fbf70 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 2c 20 a SHARED lock,
fbf80 74 68 65 20 61 63 71 75 69 73 69 74 69 6f 6e 20 the acquisition
fbf90 6f 66 0a 20 20 2a 2a 20 74 68 65 20 50 45 4e 44 of. ** the PEND
fbfa0 49 4e 47 5f 4c 4f 43 4b 20 62 79 74 65 20 69 73 ING_LOCK byte is
fbfb0 20 74 65 6d 70 6f 72 61 72 79 2e 0a 20 20 2a 2f temporary.. */
fbfc0 0a 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20 3d . newLocktype =
fbfd0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
fbfe0 3b 0a 20 20 69 66 28 20 20 20 28 70 46 69 6c 65 ;. if( (pFile
fbff0 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c ->locktype==NO_L
fc000 4f 43 4b 29 0a 20 20 20 20 20 7c 7c 20 28 20 20 OCK). || (
fc010 20 28 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c (locktype==EXCL
fc020 55 53 49 56 45 5f 4c 4f 43 4b 29 0a 20 20 20 20 USIVE_LOCK).
fc030 20 20 20 20 20 26 26 20 28 70 46 69 6c 65 2d 3e && (pFile->
fc040 6c 6f 63 6b 74 79 70 65 3d 3d 52 45 53 45 52 56 locktype==RESERV
fc050 45 44 5f 4c 4f 43 4b 29 29 0a 20 20 29 7b 0a 20 ED_LOCK)). ){.
fc060 20 20 20 69 6e 74 20 63 6e 74 20 3d 20 33 3b 0a int cnt = 3;.
fc070 20 20 20 20 77 68 69 6c 65 28 20 63 6e 74 2d 2d while( cnt--
fc080 3e 30 20 26 26 20 28 72 65 73 20 3d 20 4c 6f 63 >0 && (res = Loc
fc090 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 kFile(pFile->h,
fc0a0 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c 20 30 2c PENDING_BYTE, 0,
fc0b0 20 31 2c 20 30 29 29 3d 3d 30 20 29 7b 0a 20 20 1, 0))==0 ){.
fc0c0 20 20 20 20 2f 2a 20 54 72 79 20 33 20 74 69 6d /* Try 3 tim
fc0d0 65 73 20 74 6f 20 67 65 74 20 74 68 65 20 70 65 es to get the pe
fc0e0 6e 64 69 6e 67 20 6c 6f 63 6b 2e 20 20 54 68 65 nding lock. The
fc0f0 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 20 6d 69 pending lock mi
fc100 67 68 74 20 62 65 0a 20 20 20 20 20 20 2a 2a 20 ght be. **
fc110 68 65 6c 64 20 62 79 20 61 6e 6f 74 68 65 72 20 held by another
fc120 72 65 61 64 65 72 20 70 72 6f 63 65 73 73 20 77 reader process w
fc130 68 6f 20 77 69 6c 6c 20 72 65 6c 65 61 73 65 20 ho will release
fc140 69 74 20 6d 6f 6d 65 6e 74 61 72 69 6c 79 2e 0a it momentarily..
fc150 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 4f */. O
fc160 53 54 52 41 43 45 32 28 22 63 6f 75 6c 64 20 6e STRACE2("could n
fc170 6f 74 20 67 65 74 20 61 20 50 45 4e 44 49 4e 47 ot get a PENDING
fc180 20 6c 6f 63 6b 2e 20 63 6e 74 3d 25 64 5c 6e 22 lock. cnt=%d\n"
fc190 2c 20 63 6e 74 29 3b 0a 20 20 20 20 20 20 53 6c , cnt);. Sl
fc1a0 65 65 70 28 31 29 3b 0a 20 20 20 20 7d 0a 20 20 eep(1);. }.
fc1b0 20 20 67 6f 74 50 65 6e 64 69 6e 67 4c 6f 63 6b gotPendingLock
fc1c0 20 3d 20 72 65 73 3b 0a 20 20 20 20 69 66 28 20 = res;. if(
fc1d0 21 72 65 73 20 29 7b 0a 20 20 20 20 20 20 65 72 !res ){. er
fc1e0 72 6f 72 20 3d 20 47 65 74 4c 61 73 74 45 72 72 ror = GetLastErr
fc1f0 6f 72 28 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a or();. }. }.
fc200 0a 20 20 2f 2a 20 41 63 71 75 69 72 65 20 61 20 . /* Acquire a
fc210 73 68 61 72 65 64 20 6c 6f 63 6b 0a 20 20 2a 2f shared lock. */
fc220 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d . if( locktype=
fc230 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 26 26 20 =SHARED_LOCK &&
fc240 72 65 73 20 29 7b 0a 20 20 20 20 61 73 73 65 72 res ){. asser
fc250 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 t( pFile->lockty
fc260 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 3b 0a 20 pe==NO_LOCK );.
fc270 20 20 20 72 65 73 20 3d 20 67 65 74 52 65 61 64 res = getRead
fc280 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 Lock(pFile);.
fc290 20 69 66 28 20 72 65 73 20 29 7b 0a 20 20 20 20 if( res ){.
fc2a0 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20 3d 20 newLocktype =
fc2b0 53 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 SHARED_LOCK;.
fc2c0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 65 72 }else{. er
fc2d0 72 6f 72 20 3d 20 47 65 74 4c 61 73 74 45 72 72 ror = GetLastErr
fc2e0 6f 72 28 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a or();. }. }.
fc2f0 0a 20 20 2f 2a 20 41 63 71 75 69 72 65 20 61 20 . /* Acquire a
fc300 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 0a 20 20 RESERVED lock.
fc310 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 */. if( locktyp
fc320 65 3d 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b e==RESERVED_LOCK
fc330 20 26 26 20 72 65 73 20 29 7b 0a 20 20 20 20 61 && res ){. a
fc340 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c 6f ssert( pFile->lo
fc350 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c cktype==SHARED_L
fc360 4f 43 4b 20 29 3b 0a 20 20 20 20 72 65 73 20 3d OCK );. res =
fc370 20 4c 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d LockFile(pFile-
fc380 3e 68 2c 20 52 45 53 45 52 56 45 44 5f 42 59 54 >h, RESERVED_BYT
fc390 45 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20 20 20 E, 0, 1, 0);.
fc3a0 20 69 66 28 20 72 65 73 20 29 7b 0a 20 20 20 20 if( res ){.
fc3b0 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20 3d 20 newLocktype =
fc3c0 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 3b 0a 20 RESERVED_LOCK;.
fc3d0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
fc3e0 65 72 72 6f 72 20 3d 20 47 65 74 4c 61 73 74 45 error = GetLastE
fc3f0 72 72 6f 72 28 29 3b 0a 20 20 20 20 7d 0a 20 20 rror();. }.
fc400 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72 65 20 }.. /* Acquire
fc410 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 0a 20 a PENDING lock.
fc420 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 */. if( lockty
fc430 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f pe==EXCLUSIVE_LO
fc440 43 4b 20 26 26 20 72 65 73 20 29 7b 0a 20 20 20 CK && res ){.
fc450 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20 3d 20 50 newLocktype = P
fc460 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3b 0a 20 20 20 ENDING_LOCK;.
fc470 20 67 6f 74 50 65 6e 64 69 6e 67 4c 6f 63 6b 20 gotPendingLock
fc480 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 = 0;. }.. /* A
fc490 63 71 75 69 72 65 20 61 6e 20 45 58 43 4c 55 53 cquire an EXCLUS
fc4a0 49 56 45 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 IVE lock. */.
fc4b0 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 if( locktype==EX
fc4c0 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 CLUSIVE_LOCK &&
fc4d0 72 65 73 20 29 7b 0a 20 20 20 20 61 73 73 65 72 res ){. asser
fc4e0 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 t( pFile->lockty
fc4f0 70 65 3e 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 pe>=SHARED_LOCK
fc500 29 3b 0a 20 20 20 20 72 65 73 20 3d 20 75 6e 6c );. res = unl
fc510 6f 63 6b 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c ockReadLock(pFil
fc520 65 29 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 32 e);. OSTRACE2
fc530 28 22 75 6e 72 65 61 64 6c 6f 63 6b 20 3d 20 25 ("unreadlock = %
fc540 64 5c 6e 22 2c 20 72 65 73 29 3b 0a 20 20 20 20 d\n", res);.
fc550 72 65 73 20 3d 20 4c 6f 63 6b 46 69 6c 65 28 70 res = LockFile(p
fc560 46 69 6c 65 2d 3e 68 2c 20 53 48 41 52 45 44 5f File->h, SHARED_
fc570 46 49 52 53 54 2c 20 30 2c 20 53 48 41 52 45 44 FIRST, 0, SHARED
fc580 5f 53 49 5a 45 2c 20 30 29 3b 0a 20 20 20 20 69 _SIZE, 0);. i
fc590 66 28 20 72 65 73 20 29 7b 0a 20 20 20 20 20 20 f( res ){.
fc5a0 6e 65 77 4c 6f 63 6b 74 79 70 65 20 3d 20 45 58 newLocktype = EX
fc5b0 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 3b 0a 20 20 CLUSIVE_LOCK;.
fc5c0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 65 }else{. e
fc5d0 72 72 6f 72 20 3d 20 47 65 74 4c 61 73 74 45 72 rror = GetLastEr
fc5e0 72 6f 72 28 29 3b 0a 20 20 20 20 20 20 4f 53 54 ror();. OST
fc5f0 52 41 43 45 32 28 22 65 72 72 6f 72 2d 63 6f 64 RACE2("error-cod
fc600 65 20 3d 20 25 64 5c 6e 22 2c 20 65 72 72 6f 72 e = %d\n", error
fc610 29 3b 0a 20 20 20 20 20 20 67 65 74 52 65 61 64 );. getRead
fc620 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 Lock(pFile);.
fc630 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 }. }.. /* If
fc640 77 65 20 61 72 65 20 68 6f 6c 64 69 6e 67 20 61 we are holding a
fc650 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 74 68 PENDING lock th
fc660 61 74 20 6f 75 67 68 74 20 74 6f 20 62 65 20 72 at ought to be r
fc670 65 6c 65 61 73 65 64 2c 20 74 68 65 6e 0a 20 20 eleased, then.
fc680 2a 2a 20 72 65 6c 65 61 73 65 20 69 74 20 6e 6f ** release it no
fc690 77 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 67 6f w.. */. if( go
fc6a0 74 50 65 6e 64 69 6e 67 4c 6f 63 6b 20 26 26 20 tPendingLock &&
fc6b0 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 locktype==SHARED
fc6c0 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 55 6e 6c _LOCK ){. Unl
fc6d0 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 ockFile(pFile->h
fc6e0 2c 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c 20 , PENDING_BYTE,
fc6f0 30 2c 20 31 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20 0, 1, 0);. }..
fc700 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20 73 /* Update the s
fc710 74 61 74 65 20 6f 66 20 74 68 65 20 6c 6f 63 6b tate of the lock
fc720 20 68 61 73 20 68 65 6c 64 20 69 6e 20 74 68 65 has held in the
fc730 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 file descriptor
fc740 20 74 68 65 6e 0a 20 20 2a 2a 20 72 65 74 75 72 then. ** retur
fc750 6e 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 n the appropriat
fc760 65 20 72 65 73 75 6c 74 20 63 6f 64 65 2e 0a 20 e result code..
fc770 20 2a 2f 0a 20 20 69 66 28 20 72 65 73 20 29 7b */. if( res ){
fc780 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 . rc = SQLITE
fc790 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 _OK;. }else{.
fc7a0 20 20 4f 53 54 52 41 43 45 34 28 22 4c 4f 43 4b OSTRACE4("LOCK
fc7b0 20 46 41 49 4c 45 44 20 25 64 20 74 72 79 69 6e FAILED %d tryin
fc7c0 67 20 66 6f 72 20 25 64 20 62 75 74 20 67 6f 74 g for %d but got
fc7d0 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 %d\n", pFile->h
fc7e0 2c 0a 20 20 20 20 20 20 20 20 20 20 20 6c 6f 63 ,. loc
fc7f0 6b 74 79 70 65 2c 20 6e 65 77 4c 6f 63 6b 74 79 ktype, newLockty
fc800 70 65 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e pe);. pFile->
fc810 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6f lastErrno = erro
fc820 72 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 r;. rc = SQLI
fc830 54 45 5f 42 55 53 59 3b 0a 20 20 7d 0a 20 20 70 TE_BUSY;. }. p
fc840 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d File->locktype =
fc850 20 28 75 38 29 6e 65 77 4c 6f 63 6b 74 79 70 65 (u8)newLocktype
fc860 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
fc870 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ../*.** This rou
fc880 74 69 6e 65 20 63 68 65 63 6b 73 20 69 66 20 74 tine checks if t
fc890 68 65 72 65 20 69 73 20 61 20 52 45 53 45 52 56 here is a RESERV
fc8a0 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 ED lock held on
fc8b0 74 68 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a the specified.**
fc8c0 20 66 69 6c 65 20 62 79 20 74 68 69 73 20 6f 72 file by this or
fc8d0 20 61 6e 79 20 6f 74 68 65 72 20 70 72 6f 63 65 any other proce
fc8e0 73 73 2e 20 49 66 20 73 75 63 68 20 61 20 6c 6f ss. If such a lo
fc8f0 63 6b 20 69 73 20 68 65 6c 64 2c 20 72 65 74 75 ck is held, retu
fc900 72 6e 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 rn.** non-zero,
fc910 6f 74 68 65 72 77 69 73 65 20 7a 65 72 6f 2e 0a otherwise zero..
fc920 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 */.static int wi
fc930 6e 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f nCheckReservedLo
fc940 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 ck(sqlite3_file
fc950 2a 69 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 *id, int *pResOu
fc960 74 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 t){. int rc;.
fc970 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d winFile *pFile =
fc980 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a 0a (winFile*)id;..
fc990 20 20 61 73 73 65 72 74 28 20 69 64 21 3d 30 20 assert( id!=0
fc9a0 29 3b 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e );. if( pFile->
fc9b0 6c 6f 63 6b 74 79 70 65 3e 3d 52 45 53 45 52 56 locktype>=RESERV
fc9c0 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 ED_LOCK ){. r
fc9d0 63 20 3d 20 31 3b 0a 20 20 20 20 4f 53 54 52 41 c = 1;. OSTRA
fc9e0 43 45 33 28 22 54 45 53 54 20 57 52 2d 4c 4f 43 CE3("TEST WR-LOC
fc9f0 4b 20 25 64 20 25 64 20 28 6c 6f 63 61 6c 29 5c K %d %d (local)\
fca00 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 n", pFile->h, rc
fca10 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
fca20 72 63 20 3d 20 4c 6f 63 6b 46 69 6c 65 28 70 46 rc = LockFile(pF
fca30 69 6c 65 2d 3e 68 2c 20 52 45 53 45 52 56 45 44 ile->h, RESERVED
fca40 5f 42 59 54 45 2c 20 30 2c 20 31 2c 20 30 29 3b _BYTE, 0, 1, 0);
fca50 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 . if( rc ){.
fca60 20 20 20 20 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 UnlockFile(
fca70 70 46 69 6c 65 2d 3e 68 2c 20 52 45 53 45 52 56 pFile->h, RESERV
fca80 45 44 5f 42 59 54 45 2c 20 30 2c 20 31 2c 20 30 ED_BYTE, 0, 1, 0
fca90 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 );. }. rc
fcaa0 3d 20 21 72 63 3b 0a 20 20 20 20 4f 53 54 52 41 = !rc;. OSTRA
fcab0 43 45 33 28 22 54 45 53 54 20 57 52 2d 4c 4f 43 CE3("TEST WR-LOC
fcac0 4b 20 25 64 20 25 64 20 28 72 65 6d 6f 74 65 29 K %d %d (remote)
fcad0 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 \n", pFile->h, r
fcae0 63 29 3b 0a 20 20 7d 0a 20 20 2a 70 52 65 73 4f c);. }. *pResO
fcaf0 75 74 20 3d 20 72 63 3b 0a 20 20 72 65 74 75 72 ut = rc;. retur
fcb00 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
fcb10 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65 20 /*.** Lower the
fcb20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e locking level on
fcb30 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 file descriptor
fcb40 20 69 64 20 74 6f 20 6c 6f 63 6b 74 79 70 65 2e id to locktype.
fcb50 20 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 locktype.** mu
fcb60 73 74 20 62 65 20 65 69 74 68 65 72 20 4e 4f 5f st be either NO_
fcb70 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45 44 5f 4c LOCK or SHARED_L
fcb80 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 OCK..**.** If th
fcb90 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 e locking level
fcba0 6f 66 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 of the file desc
fcbb0 72 69 70 74 6f 72 20 69 73 20 61 6c 72 65 61 64 riptor is alread
fcbc0 79 20 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a y at or below.**
fcbd0 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 6c the requested l
fcbe0 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74 68 ocking level, th
fcbf0 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 is routine is a
fcc00 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 no-op..**.** It
fcc10 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 is not possible
fcc20 66 6f 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 for this routine
fcc30 20 74 6f 20 66 61 69 6c 20 69 66 20 74 68 65 20 to fail if the
fcc40 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 0a second argument.
fcc50 2a 2a 20 69 73 20 4e 4f 5f 4c 4f 43 4b 2e 20 20 ** is NO_LOCK.
fcc60 49 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 If the second ar
fcc70 67 75 6d 65 6e 74 20 69 73 20 53 48 41 52 45 44 gument is SHARED
fcc80 5f 4c 4f 43 4b 20 74 68 65 6e 20 74 68 69 73 20 _LOCK then this
fcc90 72 6f 75 74 69 6e 65 0a 2a 2a 20 6d 69 67 68 74 routine.** might
fcca0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 return SQLITE_I
fccb0 4f 45 52 52 3b 0a 2a 2f 0a 73 74 61 74 69 63 20 OERR;.*/.static
fccc0 69 6e 74 20 77 69 6e 55 6e 6c 6f 63 6b 28 73 71 int winUnlock(sq
fccd0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 lite3_file *id,
fcce0 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 int locktype){.
fccf0 20 69 6e 74 20 74 79 70 65 3b 0a 20 20 77 69 6e int type;. win
fcd00 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 File *pFile = (w
fcd10 69 6e 46 69 6c 65 2a 29 69 64 3b 0a 20 20 69 6e inFile*)id;. in
fcd20 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
fcd30 3b 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c ;. assert( pFil
fcd40 65 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 e!=0 );. assert
fcd50 28 20 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 41 52 ( locktype<=SHAR
fcd60 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 4f 53 54 ED_LOCK );. OST
fcd70 52 41 43 45 35 28 22 55 4e 4c 4f 43 4b 20 25 64 RACE5("UNLOCK %d
fcd80 20 74 6f 20 25 64 20 77 61 73 20 25 64 28 25 64 to %d was %d(%d
fcd90 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 )\n", pFile->h,
fcda0 6c 6f 63 6b 74 79 70 65 2c 0a 20 20 20 20 20 20 locktype,.
fcdb0 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 pFile->lockt
fcdc0 79 70 65 2c 20 70 46 69 6c 65 2d 3e 73 68 61 72 ype, pFile->shar
fcdd0 65 64 4c 6f 63 6b 42 79 74 65 29 3b 0a 20 20 74 edLockByte);. t
fcde0 79 70 65 20 3d 20 70 46 69 6c 65 2d 3e 6c 6f 63 ype = pFile->loc
fcdf0 6b 74 79 70 65 3b 0a 20 20 69 66 28 20 74 79 70 ktype;. if( typ
fce00 65 3e 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 e>=EXCLUSIVE_LOC
fce10 4b 20 29 7b 0a 20 20 20 20 55 6e 6c 6f 63 6b 46 K ){. UnlockF
fce20 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 53 48 ile(pFile->h, SH
fce30 41 52 45 44 5f 46 49 52 53 54 2c 20 30 2c 20 53 ARED_FIRST, 0, S
fce40 48 41 52 45 44 5f 53 49 5a 45 2c 20 30 29 3b 0a HARED_SIZE, 0);.
fce50 20 20 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 if( locktype
fce60 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 26 26 ==SHARED_LOCK &&
fce70 20 21 67 65 74 52 65 61 64 4c 6f 63 6b 28 70 46 !getReadLock(pF
fce80 69 6c 65 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a ile) ){. /*
fce90 20 54 68 69 73 20 73 68 6f 75 6c 64 20 6e 65 76 This should nev
fcea0 65 72 20 68 61 70 70 65 6e 2e 20 20 57 65 20 73 er happen. We s
fceb0 68 6f 75 6c 64 20 61 6c 77 61 79 73 20 62 65 20 hould always be
fcec0 61 62 6c 65 20 74 6f 0a 20 20 20 20 20 20 2a 2a able to. **
fced0 20 72 65 61 63 71 75 69 72 65 20 74 68 65 20 72 reacquire the r
fcee0 65 61 64 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 ead lock */.
fcef0 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f rc = SQLITE_IO
fcf00 45 52 52 5f 55 4e 4c 4f 43 4b 3b 0a 20 20 20 20 ERR_UNLOCK;.
fcf10 7d 0a 20 20 7d 0a 20 20 69 66 28 20 74 79 70 65 }. }. if( type
fcf20 3e 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 >=RESERVED_LOCK
fcf30 29 7b 0a 20 20 20 20 55 6e 6c 6f 63 6b 46 69 6c ){. UnlockFil
fcf40 65 28 70 46 69 6c 65 2d 3e 68 2c 20 52 45 53 45 e(pFile->h, RESE
fcf50 52 56 45 44 5f 42 59 54 45 2c 20 30 2c 20 31 2c RVED_BYTE, 0, 1,
fcf60 20 30 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6c 0);. }. if( l
fcf70 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b ocktype==NO_LOCK
fcf80 20 26 26 20 74 79 70 65 3e 3d 53 48 41 52 45 44 && type>=SHARED
fcf90 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 75 6e 6c _LOCK ){. unl
fcfa0 6f 63 6b 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c ockReadLock(pFil
fcfb0 65 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 74 79 e);. }. if( ty
fcfc0 70 65 3e 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b pe>=PENDING_LOCK
fcfd0 20 29 7b 0a 20 20 20 20 55 6e 6c 6f 63 6b 46 69 ){. UnlockFi
fcfe0 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 50 45 4e le(pFile->h, PEN
fcff0 44 49 4e 47 5f 42 59 54 45 2c 20 30 2c 20 31 2c DING_BYTE, 0, 1,
fd000 20 30 29 3b 0a 20 20 7d 0a 20 20 70 46 69 6c 65 0);. }. pFile
fd010 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 28 75 38 ->locktype = (u8
fd020 29 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 72 65 74 )locktype;. ret
fd030 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
fd040 20 43 6f 6e 74 72 6f 6c 20 61 6e 64 20 71 75 65 Control and que
fd050 72 79 20 6f 66 20 74 68 65 20 6f 70 65 6e 20 66 ry of the open f
fd060 69 6c 65 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 73 ile handle..*/.s
fd070 74 61 74 69 63 20 69 6e 74 20 77 69 6e 46 69 6c tatic int winFil
fd080 65 43 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65 33 eControl(sqlite3
fd090 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6f _file *id, int o
fd0a0 70 2c 20 76 6f 69 64 20 2a 70 41 72 67 29 7b 0a p, void *pArg){.
fd0b0 20 20 73 77 69 74 63 68 28 20 6f 70 20 29 7b 0a switch( op ){.
fd0c0 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f case SQLITE_
fd0d0 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 3a FCNTL_LOCKSTATE:
fd0e0 20 7b 0a 20 20 20 20 20 20 2a 28 69 6e 74 2a 29 {. *(int*)
fd0f0 70 41 72 67 20 3d 20 28 28 77 69 6e 46 69 6c 65 pArg = ((winFile
fd100 2a 29 69 64 29 2d 3e 6c 6f 63 6b 74 79 70 65 3b *)id)->locktype;
fd110 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 . return SQ
fd120 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 LITE_OK;. }.
fd130 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4c case SQLITE_L
fd140 41 53 54 5f 45 52 52 4e 4f 3a 20 7b 0a 20 20 20 AST_ERRNO: {.
fd150 20 20 20 2a 28 69 6e 74 2a 29 70 41 72 67 20 3d *(int*)pArg =
fd160 20 28 69 6e 74 29 28 28 77 69 6e 46 69 6c 65 2a (int)((winFile*
fd170 29 69 64 29 2d 3e 6c 61 73 74 45 72 72 6e 6f 3b )id)->lastErrno;
fd180 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 . return SQ
fd190 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 LITE_OK;. }.
fd1a0 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 }. return SQLI
fd1b0 54 45 5f 45 52 52 4f 52 3b 0a 7d 0a 0a 2f 2a 0a TE_ERROR;.}../*.
fd1c0 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 65 ** Return the se
fd1d0 63 74 6f 72 20 73 69 7a 65 20 69 6e 20 62 79 74 ctor size in byt
fd1e0 65 73 20 6f 66 20 74 68 65 20 75 6e 64 65 72 6c es of the underl
fd1f0 79 69 6e 67 20 62 6c 6f 63 6b 20 64 65 76 69 63 ying block devic
fd200 65 20 66 6f 72 0a 2a 2a 20 74 68 65 20 73 70 65 e for.** the spe
fd210 63 69 66 69 65 64 20 66 69 6c 65 2e 20 54 68 69 cified file. Thi
fd220 73 20 69 73 20 61 6c 6d 6f 73 74 20 61 6c 77 61 s is almost alwa
fd230 79 73 20 35 31 32 20 62 79 74 65 73 2c 20 62 75 ys 512 bytes, bu
fd240 74 20 6d 61 79 20 62 65 0a 2a 2a 20 6c 61 72 67 t may be.** larg
fd250 65 72 20 66 6f 72 20 73 6f 6d 65 20 64 65 76 69 er for some devi
fd260 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 ces..**.** SQLit
fd270 65 20 63 6f 64 65 20 61 73 73 75 6d 65 73 20 74 e code assumes t
fd280 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 61 6e his function can
fd290 6e 6f 74 20 66 61 69 6c 2e 20 49 74 20 61 6c 73 not fail. It als
fd2a0 6f 20 61 73 73 75 6d 65 73 20 74 68 61 74 0a 2a o assumes that.*
fd2b0 2a 20 69 66 20 74 77 6f 20 66 69 6c 65 73 20 61 * if two files a
fd2c0 72 65 20 63 72 65 61 74 65 64 20 69 6e 20 74 68 re created in th
fd2d0 65 20 73 61 6d 65 20 66 69 6c 65 2d 73 79 73 74 e same file-syst
fd2e0 65 6d 20 64 69 72 65 63 74 6f 72 79 20 28 69 2e em directory (i.
fd2f0 65 2e 0a 2a 2a 20 61 20 64 61 74 61 62 61 73 65 e..** a database
fd300 20 61 6e 64 20 69 74 73 20 6a 6f 75 72 6e 61 6c and its journal
fd310 20 66 69 6c 65 29 20 74 68 61 74 20 74 68 65 20 file) that the
fd320 73 65 63 74 6f 72 20 73 69 7a 65 20 77 69 6c 6c sector size will
fd330 20 62 65 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 be the.** same
fd340 66 6f 72 20 62 6f 74 68 2e 0a 2a 2f 0a 73 74 61 for both..*/.sta
fd350 74 69 63 20 69 6e 74 20 77 69 6e 53 65 63 74 6f tic int winSecto
fd360 72 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 69 rSize(sqlite3_fi
fd370 6c 65 20 2a 69 64 29 7b 0a 20 20 61 73 73 65 72 le *id){. asser
fd380 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 72 65 t( id!=0 );. re
fd390 74 75 72 6e 20 28 69 6e 74 29 28 28 28 77 69 6e turn (int)(((win
fd3a0 46 69 6c 65 2a 29 69 64 29 2d 3e 73 65 63 74 6f File*)id)->secto
fd3b0 72 53 69 7a 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a rSize);.}../*.**
fd3c0 20 52 65 74 75 72 6e 20 61 20 76 65 63 74 6f 72 Return a vector
fd3d0 20 6f 66 20 64 65 76 69 63 65 20 63 68 61 72 61 of device chara
fd3e0 63 74 65 72 69 73 74 69 63 73 2e 0a 2a 2f 0a 73 cteristics..*/.s
fd3f0 74 61 74 69 63 20 69 6e 74 20 77 69 6e 44 65 76 tatic int winDev
fd400 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 iceCharacteristi
fd410 63 73 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 cs(sqlite3_file
fd420 2a 69 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 *id){. UNUSED_P
fd430 41 52 41 4d 45 54 45 52 28 69 64 29 3b 0a 20 20 ARAMETER(id);.
fd440 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a return 0;.}../*.
fd450 2a 2a 20 54 68 69 73 20 76 65 63 74 6f 72 20 64 ** This vector d
fd460 65 66 69 6e 65 73 20 61 6c 6c 20 74 68 65 20 6d efines all the m
fd470 65 74 68 6f 64 73 20 74 68 61 74 20 63 61 6e 20 ethods that can
fd480 6f 70 65 72 61 74 65 20 6f 6e 20 61 6e 0a 2a 2a operate on an.**
fd490 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 66 6f sqlite3_file fo
fd4a0 72 20 77 69 6e 33 32 2e 0a 2a 2f 0a 73 74 61 74 r win32..*/.stat
fd4b0 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 ic const sqlite3
fd4c0 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 77 69 6e 49 _io_methods winI
fd4d0 6f 4d 65 74 68 6f 64 20 3d 20 7b 0a 20 20 31 2c oMethod = {. 1,
fd4e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fd4f0 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73 /* iVers
fd500 69 6f 6e 20 2a 2f 0a 20 20 77 69 6e 43 6c 6f 73 ion */. winClos
fd510 65 2c 0a 20 20 77 69 6e 52 65 61 64 2c 0a 20 20 e,. winRead,.
fd520 77 69 6e 57 72 69 74 65 2c 0a 20 20 77 69 6e 54 winWrite,. winT
fd530 72 75 6e 63 61 74 65 2c 0a 20 20 77 69 6e 53 79 runcate,. winSy
fd540 6e 63 2c 0a 20 20 77 69 6e 46 69 6c 65 53 69 7a nc,. winFileSiz
fd550 65 2c 0a 20 20 77 69 6e 4c 6f 63 6b 2c 0a 20 20 e,. winLock,.
fd560 77 69 6e 55 6e 6c 6f 63 6b 2c 0a 20 20 77 69 6e winUnlock,. win
fd570 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 CheckReservedLoc
fd580 6b 2c 0a 20 20 77 69 6e 46 69 6c 65 43 6f 6e 74 k,. winFileCont
fd590 72 6f 6c 2c 0a 20 20 77 69 6e 53 65 63 74 6f 72 rol,. winSector
fd5a0 53 69 7a 65 2c 0a 20 20 77 69 6e 44 65 76 69 63 Size,. winDevic
fd5b0 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73 eCharacteristics
fd5c0 0a 7d 3b 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .};../**********
fd5d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fd5e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fd5f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fd600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fd610 2a 0a 2a 2a 20 48 65 72 65 20 65 6e 64 73 20 74 *.** Here ends t
fd620 68 65 20 49 2f 4f 20 6d 65 74 68 6f 64 73 20 74 he I/O methods t
fd630 68 61 74 20 66 6f 72 6d 20 74 68 65 20 73 71 6c hat form the sql
fd640 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 ite3_io_methods
fd650 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 object..**.** Th
fd660 65 20 6e 65 78 74 20 62 6c 6f 63 6b 20 6f 66 20 e next block of
fd670 63 6f 64 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 code implements
fd680 74 68 65 20 56 46 53 20 6d 65 74 68 6f 64 73 2e the VFS methods.
fd690 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
fd6a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fd6b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fd6c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fd6d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a *************/..
fd6e0 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20 /*.** Convert a
fd6f0 55 54 46 2d 38 20 66 69 6c 65 6e 61 6d 65 20 69 UTF-8 filename i
fd700 6e 74 6f 20 77 68 61 74 65 76 65 72 20 66 6f 72 nto whatever for
fd710 6d 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 m the underlying
fd720 0a 2a 2a 20 6f 70 65 72 61 74 69 6e 67 20 73 79 .** operating sy
fd730 73 74 65 6d 20 77 61 6e 74 73 20 66 69 6c 65 6e stem wants filen
fd740 61 6d 65 73 20 69 6e 2e 20 20 53 70 61 63 65 20 ames in. Space
fd750 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 65 73 75 to hold the resu
fd760 6c 74 0a 2a 2a 20 69 73 20 6f 62 74 61 69 6e 65 lt.** is obtaine
fd770 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 20 61 6e d from malloc an
fd780 64 20 6d 75 73 74 20 62 65 20 66 72 65 65 64 20 d must be freed
fd790 62 79 20 74 68 65 20 63 61 6c 6c 69 6e 67 0a 2a by the calling.*
fd7a0 2a 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 * function..*/.s
fd7b0 74 61 74 69 63 20 76 6f 69 64 20 2a 63 6f 6e 76 tatic void *conv
fd7c0 65 72 74 55 74 66 38 46 69 6c 65 6e 61 6d 65 28 ertUtf8Filename(
fd7d0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c const char *zFil
fd7e0 65 6e 61 6d 65 29 7b 0a 20 20 76 6f 69 64 20 2a ename){. void *
fd7f0 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 30 3b 0a zConverted = 0;.
fd800 20 20 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0a if( isNT() ){.
fd810 20 20 20 20 7a 43 6f 6e 76 65 72 74 65 64 20 3d zConverted =
fd820 20 75 74 66 38 54 6f 55 6e 69 63 6f 64 65 28 7a utf8ToUnicode(z
fd830 46 69 6c 65 6e 61 6d 65 29 3b 0a 2f 2a 20 69 73 Filename);./* is
fd840 4e 54 28 29 20 69 73 20 31 20 69 66 20 53 51 4c NT() is 1 if SQL
fd850 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c ITE_OS_WINCE==1,
fd860 20 73 6f 20 74 68 69 73 20 65 6c 73 65 20 69 73 so this else is
fd870 20 6e 65 76 65 72 20 65 78 65 63 75 74 65 64 2e never executed.
fd880 20 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f .*/.#if SQLITE_
fd890 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 OS_WINCE==0. }e
fd8a0 6c 73 65 7b 0a 20 20 20 20 7a 43 6f 6e 76 65 72 lse{. zConver
fd8b0 74 65 64 20 3d 20 75 74 66 38 54 6f 4d 62 63 73 ted = utf8ToMbcs
fd8c0 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 23 65 6e (zFilename);.#en
fd8d0 64 69 66 0a 20 20 7d 0a 20 20 2f 2a 20 63 61 6c dif. }. /* cal
fd8e0 6c 65 72 20 77 69 6c 6c 20 68 61 6e 64 6c 65 20 ler will handle
fd8f0 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 20 2a 2f out of memory */
fd900 0a 20 20 72 65 74 75 72 6e 20 7a 43 6f 6e 76 65 . return zConve
fd910 72 74 65 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 rted;.}../*.** C
fd920 72 65 61 74 65 20 61 20 74 65 6d 70 6f 72 61 72 reate a temporar
fd930 79 20 66 69 6c 65 20 6e 61 6d 65 20 69 6e 20 7a y file name in z
fd940 42 75 66 2e 20 20 7a 42 75 66 20 6d 75 73 74 20 Buf. zBuf must
fd950 62 65 20 62 69 67 20 65 6e 6f 75 67 68 20 74 6f be big enough to
fd960 0a 2a 2a 20 68 6f 6c 64 20 61 74 20 70 56 66 73 .** hold at pVfs
fd970 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 20 63 68 61 ->mxPathname cha
fd980 72 61 63 74 65 72 73 2e 0a 2a 2f 0a 73 74 61 74 racters..*/.stat
fd990 69 63 20 69 6e 74 20 67 65 74 54 65 6d 70 6e 61 ic int getTempna
fd9a0 6d 65 28 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 me(int nBuf, cha
fd9b0 72 20 2a 7a 42 75 66 29 7b 0a 20 20 73 74 61 74 r *zBuf){. stat
fd9c0 69 63 20 63 68 61 72 20 7a 43 68 61 72 73 5b 5d ic char zChars[]
fd9d0 20 3d 0a 20 20 20 20 22 61 62 63 64 65 66 67 68 =. "abcdefgh
fd9e0 69 6a 6b 6c 6d 6e 6f 70 71 72 73 74 75 76 77 78 ijklmnopqrstuvwx
fd9f0 79 7a 22 0a 20 20 20 20 22 41 42 43 44 45 46 47 yz". "ABCDEFG
fda00 48 49 4a 4b 4c 4d 4e 4f 50 51 52 53 54 55 56 57 HIJKLMNOPQRSTUVW
fda10 58 59 5a 22 0a 20 20 20 20 22 30 31 32 33 34 35 XYZ". "012345
fda20 36 37 38 39 22 3b 0a 20 20 73 69 7a 65 5f 74 20 6789";. size_t
fda30 69 2c 20 6a 3b 0a 20 20 63 68 61 72 20 7a 54 65 i, j;. char zTe
fda40 6d 70 50 61 74 68 5b 4d 41 58 5f 50 41 54 48 2b mpPath[MAX_PATH+
fda50 31 5d 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 1];. if( sqlite
fda60 33 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 3_temp_directory
fda70 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f ){. sqlite3_
fda80 73 6e 70 72 69 6e 74 66 28 4d 41 58 5f 50 41 54 snprintf(MAX_PAT
fda90 48 2d 33 30 2c 20 7a 54 65 6d 70 50 61 74 68 2c H-30, zTempPath,
fdaa0 20 22 25 73 22 2c 20 73 71 6c 69 74 65 33 5f 74 "%s", sqlite3_t
fdab0 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 29 3b 0a emp_directory);.
fdac0 20 20 7d 65 6c 73 65 20 69 66 28 20 69 73 4e 54 }else if( isNT
fdad0 28 29 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a () ){. char *
fdae0 7a 4d 75 6c 74 69 3b 0a 20 20 20 20 57 43 48 41 zMulti;. WCHA
fdaf0 52 20 7a 57 69 64 65 50 61 74 68 5b 4d 41 58 5f R zWidePath[MAX_
fdb00 50 41 54 48 5d 3b 0a 20 20 20 20 47 65 74 54 65 PATH];. GetTe
fdb10 6d 70 50 61 74 68 57 28 4d 41 58 5f 50 41 54 48 mpPathW(MAX_PATH
fdb20 2d 33 30 2c 20 7a 57 69 64 65 50 61 74 68 29 3b -30, zWidePath);
fdb30 0a 20 20 20 20 7a 4d 75 6c 74 69 20 3d 20 75 6e . zMulti = un
fdb40 69 63 6f 64 65 54 6f 55 74 66 38 28 7a 57 69 64 icodeToUtf8(zWid
fdb50 65 50 61 74 68 29 3b 0a 20 20 20 20 69 66 28 20 ePath);. if(
fdb60 7a 4d 75 6c 74 69 20 29 7b 0a 20 20 20 20 20 20 zMulti ){.
fdb70 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
fdb80 28 4d 41 58 5f 50 41 54 48 2d 33 30 2c 20 7a 54 (MAX_PATH-30, zT
fdb90 65 6d 70 50 61 74 68 2c 20 22 25 73 22 2c 20 7a empPath, "%s", z
fdba0 4d 75 6c 74 69 29 3b 0a 20 20 20 20 20 20 66 72 Multi);. fr
fdbb0 65 65 28 7a 4d 75 6c 74 69 29 3b 0a 20 20 20 20 ee(zMulti);.
fdbc0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 65 74 }else{. ret
fdbd0 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d urn SQLITE_NOMEM
fdbe0 3b 0a 20 20 20 20 7d 0a 2f 2a 20 69 73 4e 54 28 ;. }./* isNT(
fdbf0 29 20 69 73 20 31 20 69 66 20 53 51 4c 49 54 45 ) is 1 if SQLITE
fdc00 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 6f _OS_WINCE==1, so
fdc10 20 74 68 69 73 20 65 6c 73 65 20 69 73 20 6e 65 this else is ne
fdc20 76 65 72 20 65 78 65 63 75 74 65 64 2e 20 0a 2a ver executed. .*
fdc30 2a 20 53 69 6e 63 65 20 74 68 65 20 41 53 43 49 * Since the ASCI
fdc40 49 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 I version of the
fdc50 73 65 20 57 69 6e 64 6f 77 73 20 41 50 49 20 64 se Windows API d
fdc60 6f 20 6e 6f 74 20 65 78 69 73 74 20 66 6f 72 20 o not exist for
fdc70 57 49 4e 43 45 2c 0a 2a 2a 20 69 74 27 73 20 69 WINCE,.** it's i
fdc80 6d 70 6f 72 74 61 6e 74 20 74 6f 20 6e 6f 74 20 mportant to not
fdc90 72 65 66 65 72 65 6e 63 65 20 74 68 65 6d 20 66 reference them f
fdca0 6f 72 20 57 49 4e 43 45 20 62 75 69 6c 64 73 2e or WINCE builds.
fdcb0 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f .*/.#if SQLITE_O
fdcc0 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c S_WINCE==0. }el
fdcd0 73 65 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 55 se{. char *zU
fdce0 74 66 38 3b 0a 20 20 20 20 63 68 61 72 20 7a 4d tf8;. char zM
fdcf0 62 63 73 50 61 74 68 5b 4d 41 58 5f 50 41 54 48 bcsPath[MAX_PATH
fdd00 5d 3b 0a 20 20 20 20 47 65 74 54 65 6d 70 50 61 ];. GetTempPa
fdd10 74 68 41 28 4d 41 58 5f 50 41 54 48 2d 33 30 2c thA(MAX_PATH-30,
fdd20 20 7a 4d 62 63 73 50 61 74 68 29 3b 0a 20 20 20 zMbcsPath);.
fdd30 20 7a 55 74 66 38 20 3d 20 73 71 6c 69 74 65 33 zUtf8 = sqlite3
fdd40 5f 77 69 6e 33 32 5f 6d 62 63 73 5f 74 6f 5f 75 _win32_mbcs_to_u
fdd50 74 66 38 28 7a 4d 62 63 73 50 61 74 68 29 3b 0a tf8(zMbcsPath);.
fdd60 20 20 20 20 69 66 28 20 7a 55 74 66 38 20 29 7b if( zUtf8 ){
fdd70 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 . sqlite3_s
fdd80 6e 70 72 69 6e 74 66 28 4d 41 58 5f 50 41 54 48 nprintf(MAX_PATH
fdd90 2d 33 30 2c 20 7a 54 65 6d 70 50 61 74 68 2c 20 -30, zTempPath,
fdda0 22 25 73 22 2c 20 7a 55 74 66 38 29 3b 0a 20 20 "%s", zUtf8);.
fddb0 20 20 20 20 66 72 65 65 28 7a 55 74 66 38 29 3b free(zUtf8);
fddc0 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
fddd0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
fdde0 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 23 65 6e NOMEM;. }.#en
fddf0 64 69 66 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d dif. }. for(i=
fde00 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 sqlite3Strlen30(
fde10 7a 54 65 6d 70 50 61 74 68 29 3b 20 69 3e 30 20 zTempPath); i>0
fde20 26 26 20 7a 54 65 6d 70 50 61 74 68 5b 69 2d 31 && zTempPath[i-1
fde30 5d 3d 3d 27 5c 5c 27 3b 20 69 2d 2d 29 7b 7d 0a ]=='\\'; i--){}.
fde40 20 20 7a 54 65 6d 70 50 61 74 68 5b 69 5d 20 3d zTempPath[i] =
fde50 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 0;. sqlite3_sn
fde60 70 72 69 6e 74 66 28 6e 42 75 66 2d 33 30 2c 20 printf(nBuf-30,
fde70 7a 42 75 66 2c 0a 20 20 20 20 20 20 20 20 20 20 zBuf,.
fde80 20 20 20 20 20 20 20 20 20 22 25 73 5c 5c 22 53 "%s\\"S
fde90 51 4c 49 54 45 5f 54 45 4d 50 5f 46 49 4c 45 5f QLITE_TEMP_FILE_
fdea0 50 52 45 46 49 58 2c 20 7a 54 65 6d 70 50 61 74 PREFIX, zTempPat
fdeb0 68 29 3b 0a 20 20 6a 20 3d 20 73 71 6c 69 74 65 h);. j = sqlite
fdec0 33 53 74 72 6c 65 6e 33 30 28 7a 42 75 66 29 3b 3Strlen30(zBuf);
fded0 0a 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f . sqlite3_rando
fdee0 6d 6e 65 73 73 28 32 30 2c 20 26 7a 42 75 66 5b mness(20, &zBuf[
fdef0 6a 5d 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 j]);. for(i=0;
fdf00 69 3c 32 30 3b 20 69 2b 2b 2c 20 6a 2b 2b 29 7b i<20; i++, j++){
fdf10 0a 20 20 20 20 7a 42 75 66 5b 6a 5d 20 3d 20 28 . zBuf[j] = (
fdf20 63 68 61 72 29 7a 43 68 61 72 73 5b 20 28 28 75 char)zChars[ ((u
fdf30 6e 73 69 67 6e 65 64 20 63 68 61 72 29 7a 42 75 nsigned char)zBu
fdf40 66 5b 6a 5d 29 25 28 73 69 7a 65 6f 66 28 7a 43 f[j])%(sizeof(zC
fdf50 68 61 72 73 29 2d 31 29 20 5d 3b 0a 20 20 7d 0a hars)-1) ];. }.
fdf60 20 20 7a 42 75 66 5b 6a 5d 20 3d 20 30 3b 0a 20 zBuf[j] = 0;.
fdf70 20 4f 53 54 52 41 43 45 32 28 22 54 45 4d 50 20 OSTRACE2("TEMP
fdf80 46 49 4c 45 4e 41 4d 45 3a 20 25 73 5c 6e 22 2c FILENAME: %s\n",
fdf90 20 7a 42 75 66 29 3b 0a 20 20 72 65 74 75 72 6e zBuf);. return
fdfa0 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 0a 7d 0a 0a SQLITE_OK; .}..
fdfb0 2f 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e /*.** The return
fdfc0 20 76 61 6c 75 65 20 6f 66 20 67 65 74 4c 61 73 value of getLas
fdfd0 74 45 72 72 6f 72 4d 73 67 0a 2a 2a 20 69 73 20 tErrorMsg.** is
fdfe0 7a 65 72 6f 20 69 66 20 74 68 65 20 65 72 72 6f zero if the erro
fdff0 72 20 6d 65 73 73 61 67 65 20 66 69 74 73 20 69 r message fits i
fe000 6e 20 74 68 65 20 62 75 66 66 65 72 2c 20 6f 72 n the buffer, or
fe010 20 6e 6f 6e 2d 7a 65 72 6f 0a 2a 2a 20 6f 74 68 non-zero.** oth
fe020 65 72 77 69 73 65 20 28 69 66 20 74 68 65 20 6d erwise (if the m
fe030 65 73 73 61 67 65 20 77 61 73 20 74 72 75 6e 63 essage was trunc
fe040 61 74 65 64 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 ated)..*/.static
fe050 20 69 6e 74 20 67 65 74 4c 61 73 74 45 72 72 6f int getLastErro
fe060 72 4d 73 67 28 69 6e 74 20 6e 42 75 66 2c 20 63 rMsg(int nBuf, c
fe070 68 61 72 20 2a 7a 42 75 66 29 7b 0a 20 20 44 57 har *zBuf){. DW
fe080 4f 52 44 20 65 72 72 6f 72 20 3d 20 47 65 74 4c ORD error = GetL
fe090 61 73 74 45 72 72 6f 72 28 29 3b 0a 0a 23 69 66 astError();..#if
fe0a0 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 SQLITE_OS_WINCE
fe0b0 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 . sqlite3_snpri
fe0c0 6e 74 66 28 6e 42 75 66 2c 20 7a 42 75 66 2c 20 ntf(nBuf, zBuf,
fe0d0 22 4f 73 45 72 72 6f 72 20 30 78 25 78 20 28 25 "OsError 0x%x (%
fe0e0 75 29 22 2c 20 65 72 72 6f 72 2c 20 65 72 72 6f u)", error, erro
fe0f0 72 29 3b 0a 23 65 6c 73 65 0a 20 20 2f 2a 20 46 r);.#else. /* F
fe100 6f 72 6d 61 74 4d 65 73 73 61 67 65 20 72 65 74 ormatMessage ret
fe110 75 72 6e 73 20 30 20 6f 6e 20 66 61 69 6c 75 72 urns 0 on failur
fe120 65 2e 20 20 4f 74 68 65 72 77 69 73 65 20 69 74 e. Otherwise it
fe130 0a 20 20 2a 2a 20 72 65 74 75 72 6e 73 20 74 68 . ** returns th
fe140 65 20 6e 75 6d 62 65 72 20 6f 66 20 54 43 48 41 e number of TCHA
fe150 52 73 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 Rs written to th
fe160 65 20 6f 75 74 70 75 74 0a 20 20 2a 2a 20 62 75 e output. ** bu
fe170 66 66 65 72 2c 20 65 78 63 6c 75 64 69 6e 67 20 ffer, excluding
fe180 74 68 65 20 74 65 72 6d 69 6e 61 74 69 6e 67 20 the terminating
fe190 6e 75 6c 6c 20 63 68 61 72 2e 0a 20 20 2a 2f 0a null char.. */.
fe1a0 20 20 69 66 20 28 21 46 6f 72 6d 61 74 4d 65 73 if (!FormatMes
fe1b0 73 61 67 65 41 28 46 4f 52 4d 41 54 5f 4d 45 53 sageA(FORMAT_MES
fe1c0 53 41 47 45 5f 46 52 4f 4d 5f 53 59 53 54 45 4d SAGE_FROM_SYSTEM
fe1d0 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
fe1e0 20 20 20 20 20 20 20 20 4e 55 4c 4c 2c 0a 20 20 NULL,.
fe1f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fe200 20 20 20 20 65 72 72 6f 72 2c 0a 20 20 20 20 20 error,.
fe210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fe220 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 0,.
fe230 20 20 20 20 20 20 20 20 20 20 7a 42 75 66 2c 0a zBuf,.
fe240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fe250 20 20 20 20 20 20 6e 42 75 66 2d 31 2c 0a 20 20 nBuf-1,.
fe260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fe270 20 20 20 20 30 29 29 0a 20 20 7b 0a 20 20 20 20 0)). {.
fe280 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
fe290 28 6e 42 75 66 2c 20 7a 42 75 66 2c 20 22 4f 73 (nBuf, zBuf, "Os
fe2a0 45 72 72 6f 72 20 30 78 25 78 20 28 25 75 29 22 Error 0x%x (%u)"
fe2b0 2c 20 65 72 72 6f 72 2c 20 65 72 72 6f 72 29 3b , error, error);
fe2c0 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 72 . }.#endif.. r
fe2d0 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a eturn 0;.}../*.*
fe2e0 2a 20 4f 70 65 6e 20 61 20 66 69 6c 65 2e 0a 2a * Open a file..*
fe2f0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e /.static int win
fe300 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f Open(. sqlite3_
fe310 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20 vfs *pVfs,
fe320 20 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20 2a 2f /* Not used */
fe330 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
fe340 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 Name, /*
fe350 4e 61 6d 65 20 6f 66 20 74 68 65 20 66 69 6c 65 Name of the file
fe360 20 28 55 54 46 2d 38 29 20 2a 2f 0a 20 20 73 71 (UTF-8) */. sq
fe370 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 lite3_file *id,
fe380 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 /* Write
fe390 20 74 68 65 20 53 51 4c 69 74 65 20 66 69 6c 65 the SQLite file
fe3a0 20 68 61 6e 64 6c 65 20 68 65 72 65 20 2a 2f 0a handle here */.
fe3b0 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 int flags,
fe3c0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f /* O
fe3d0 70 65 6e 20 6d 6f 64 65 20 66 6c 61 67 73 20 2a pen mode flags *
fe3e0 2f 0a 20 20 69 6e 74 20 2a 70 4f 75 74 46 6c 61 /. int *pOutFla
fe3f0 67 73 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a gs /*
fe400 20 53 74 61 74 75 73 20 72 65 74 75 72 6e 20 66 Status return f
fe410 6c 61 67 73 20 2a 2f 0a 29 7b 0a 20 20 48 41 4e lags */.){. HAN
fe420 44 4c 45 20 68 3b 0a 20 20 44 57 4f 52 44 20 64 DLE h;. DWORD d
fe430 77 44 65 73 69 72 65 64 41 63 63 65 73 73 3b 0a wDesiredAccess;.
fe440 20 20 44 57 4f 52 44 20 64 77 53 68 61 72 65 4d DWORD dwShareM
fe450 6f 64 65 3b 0a 20 20 44 57 4f 52 44 20 64 77 43 ode;. DWORD dwC
fe460 72 65 61 74 69 6f 6e 44 69 73 70 6f 73 69 74 69 reationDispositi
fe470 6f 6e 3b 0a 20 20 44 57 4f 52 44 20 64 77 46 6c on;. DWORD dwFl
fe480 61 67 73 41 6e 64 41 74 74 72 69 62 75 74 65 73 agsAndAttributes
fe490 20 3d 20 30 3b 0a 23 69 66 20 53 51 4c 49 54 45 = 0;.#if SQLITE
fe4a0 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 69 6e 74 20 _OS_WINCE. int
fe4b0 69 73 54 65 6d 70 20 3d 20 30 3b 0a 23 65 6e 64 isTemp = 0;.#end
fe4c0 69 66 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 if. winFile *pF
fe4d0 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29 ile = (winFile*)
fe4e0 69 64 3b 0a 20 20 76 6f 69 64 20 2a 7a 43 6f 6e id;. void *zCon
fe4f0 76 65 72 74 65 64 3b 20 20 20 20 20 20 20 20 20 verted;
fe500 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 6e /* Filen
fe510 61 6d 65 20 69 6e 20 4f 53 20 65 6e 63 6f 64 69 ame in OS encodi
fe520 6e 67 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 ng */. const ch
fe530 61 72 20 2a 7a 55 74 66 38 4e 61 6d 65 20 3d 20 ar *zUtf8Name =
fe540 7a 4e 61 6d 65 3b 20 20 20 20 2f 2a 20 46 69 6c zName; /* Fil
fe550 65 6e 61 6d 65 20 69 6e 20 55 54 46 2d 38 20 65 ename in UTF-8 e
fe560 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20 20 63 68 61 ncoding */. cha
fe570 72 20 7a 54 6d 70 6e 61 6d 65 5b 4d 41 58 5f 50 r zTmpname[MAX_P
fe580 41 54 48 2b 31 5d 3b 20 20 20 20 20 20 20 20 2f ATH+1]; /
fe590 2a 20 42 75 66 66 65 72 20 75 73 65 64 20 74 6f * Buffer used to
fe5a0 20 63 72 65 61 74 65 20 74 65 6d 70 20 66 69 6c create temp fil
fe5b0 65 6e 61 6d 65 20 2a 2f 0a 0a 20 20 61 73 73 65 ename */.. asse
fe5c0 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 55 rt( id!=0 );. U
fe5d0 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
fe5e0 70 56 66 73 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 pVfs);.. /* If
fe5f0 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d the second argum
fe600 65 6e 74 20 74 6f 20 74 68 69 73 20 66 75 6e 63 ent to this func
fe610 74 69 6f 6e 20 69 73 20 4e 55 4c 4c 2c 20 67 65 tion is NULL, ge
fe620 6e 65 72 61 74 65 20 61 20 0a 20 20 2a 2a 20 74 nerate a . ** t
fe630 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 6e 61 emporary file na
fe640 6d 65 20 74 6f 20 75 73 65 20 0a 20 20 2a 2f 0a me to use . */.
fe650 20 20 69 66 28 20 21 7a 55 74 66 38 4e 61 6d 65 if( !zUtf8Name
fe660 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d ){. int rc =
fe670 20 67 65 74 54 65 6d 70 6e 61 6d 65 28 4d 41 58 getTempname(MAX
fe680 5f 50 41 54 48 2b 31 2c 20 7a 54 6d 70 6e 61 6d _PATH+1, zTmpnam
fe690 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d e);. if( rc!=
fe6a0 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
fe6b0 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
fe6c0 20 20 7d 0a 20 20 20 20 7a 55 74 66 38 4e 61 6d }. zUtf8Nam
fe6d0 65 20 3d 20 7a 54 6d 70 6e 61 6d 65 3b 0a 20 20 e = zTmpname;.
fe6e0 7d 0a 0a 20 20 2f 2a 20 43 6f 6e 76 65 72 74 20 }.. /* Convert
fe6f0 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 74 6f 20 the filename to
fe700 74 68 65 20 73 79 73 74 65 6d 20 65 6e 63 6f 64 the system encod
fe710 69 6e 67 2e 20 2a 2f 0a 20 20 7a 43 6f 6e 76 65 ing. */. zConve
fe720 72 74 65 64 20 3d 20 63 6f 6e 76 65 72 74 55 74 rted = convertUt
fe730 66 38 46 69 6c 65 6e 61 6d 65 28 7a 55 74 66 38 f8Filename(zUtf8
fe740 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 7a 43 6f Name);. if( zCo
fe750 6e 76 65 72 74 65 64 3d 3d 30 20 29 7b 0a 20 20 nverted==0 ){.
fe760 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
fe770 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20 69 66 NOMEM;. }.. if
fe780 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 ( flags & SQLITE
fe790 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20 _OPEN_READWRITE
fe7a0 29 7b 0a 20 20 20 20 64 77 44 65 73 69 72 65 64 ){. dwDesired
fe7b0 41 63 63 65 73 73 20 3d 20 47 45 4e 45 52 49 43 Access = GENERIC
fe7c0 5f 52 45 41 44 20 7c 20 47 45 4e 45 52 49 43 5f _READ | GENERIC_
fe7d0 57 52 49 54 45 3b 0a 20 20 7d 65 6c 73 65 7b 0a WRITE;. }else{.
fe7e0 20 20 20 20 64 77 44 65 73 69 72 65 64 41 63 63 dwDesiredAcc
fe7f0 65 73 73 20 3d 20 47 45 4e 45 52 49 43 5f 52 45 ess = GENERIC_RE
fe800 41 44 3b 0a 20 20 7d 0a 20 20 2f 2a 20 53 51 4c AD;. }. /* SQL
fe810 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 ITE_OPEN_EXCLUSI
fe820 56 45 20 69 73 20 75 73 65 64 20 74 6f 20 6d 61 VE is used to ma
fe830 6b 65 20 73 75 72 65 20 74 68 61 74 20 61 20 6e ke sure that a n
fe840 65 77 20 66 69 6c 65 20 69 73 20 0a 20 20 2a 2a ew file is . **
fe850 20 63 72 65 61 74 65 64 2e 20 53 51 4c 69 74 65 created. SQLite
fe860 20 64 6f 65 73 6e 27 74 20 75 73 65 20 69 74 20 doesn't use it
fe870 74 6f 20 69 6e 64 69 63 61 74 65 20 22 65 78 63 to indicate "exc
fe880 6c 75 73 69 76 65 20 61 63 63 65 73 73 22 20 0a lusive access" .
fe890 20 20 2a 2a 20 61 73 20 69 74 20 69 73 20 75 73 ** as it is us
fe8a0 75 61 6c 6c 79 20 75 6e 64 65 72 73 74 6f 6f 64 ually understood
fe8b0 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 .. */. assert(
fe8c0 21 28 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 !(flags & SQLITE
fe8d0 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 29 _OPEN_EXCLUSIVE)
fe8e0 20 7c 7c 20 28 66 6c 61 67 73 20 26 20 53 51 4c || (flags & SQL
fe8f0 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 29 ITE_OPEN_CREATE)
fe900 29 3b 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26 );. if( flags &
fe910 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 SQLITE_OPEN_EXC
fe920 4c 55 53 49 56 45 20 29 7b 0a 20 20 20 20 2f 2a LUSIVE ){. /*
fe930 20 43 72 65 61 74 65 73 20 61 20 6e 65 77 20 66 Creates a new f
fe940 69 6c 65 2c 20 6f 6e 6c 79 20 69 66 20 69 74 20 ile, only if it
fe950 64 6f 65 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 does not already
fe960 20 65 78 69 73 74 2e 20 2a 2f 0a 20 20 20 20 2f exist. */. /
fe970 2a 20 49 66 20 74 68 65 20 66 69 6c 65 20 65 78 * If the file ex
fe980 69 73 74 73 2c 20 69 74 20 66 61 69 6c 73 2e 20 ists, it fails.
fe990 2a 2f 0a 20 20 20 20 64 77 43 72 65 61 74 69 6f */. dwCreatio
fe9a0 6e 44 69 73 70 6f 73 69 74 69 6f 6e 20 3d 20 43 nDisposition = C
fe9b0 52 45 41 54 45 5f 4e 45 57 3b 0a 20 20 7d 65 6c REATE_NEW;. }el
fe9c0 73 65 20 69 66 28 20 66 6c 61 67 73 20 26 20 53 se if( flags & S
fe9d0 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 QLITE_OPEN_CREAT
fe9e0 45 20 29 7b 0a 20 20 20 20 2f 2a 20 4f 70 65 6e E ){. /* Open
fe9f0 20 65 78 69 73 74 69 6e 67 20 66 69 6c 65 2c 20 existing file,
fea00 6f 72 20 63 72 65 61 74 65 20 69 66 20 69 74 20 or create if it
fea10 64 6f 65 73 6e 27 74 20 65 78 69 73 74 20 2a 2f doesn't exist */
fea20 0a 20 20 20 20 64 77 43 72 65 61 74 69 6f 6e 44 . dwCreationD
fea30 69 73 70 6f 73 69 74 69 6f 6e 20 3d 20 4f 50 45 isposition = OPE
fea40 4e 5f 41 4c 57 41 59 53 3b 0a 20 20 7d 65 6c 73 N_ALWAYS;. }els
fea50 65 7b 0a 20 20 20 20 2f 2a 20 4f 70 65 6e 73 20 e{. /* Opens
fea60 61 20 66 69 6c 65 2c 20 6f 6e 6c 79 20 69 66 20 a file, only if
fea70 69 74 20 65 78 69 73 74 73 2e 20 2a 2f 0a 20 20 it exists. */.
fea80 20 20 64 77 43 72 65 61 74 69 6f 6e 44 69 73 70 dwCreationDisp
fea90 6f 73 69 74 69 6f 6e 20 3d 20 4f 50 45 4e 5f 45 osition = OPEN_E
feaa0 58 49 53 54 49 4e 47 3b 0a 20 20 7d 0a 20 20 64 XISTING;. }. d
feab0 77 53 68 61 72 65 4d 6f 64 65 20 3d 20 46 49 4c wShareMode = FIL
feac0 45 5f 53 48 41 52 45 5f 52 45 41 44 20 7c 20 46 E_SHARE_READ | F
fead0 49 4c 45 5f 53 48 41 52 45 5f 57 52 49 54 45 3b ILE_SHARE_WRITE;
feae0 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 53 . if( flags & S
feaf0 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 QLITE_OPEN_DELET
feb00 45 4f 4e 43 4c 4f 53 45 20 29 7b 0a 23 69 66 20 EONCLOSE ){.#if
feb10 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a SQLITE_OS_WINCE.
feb20 20 20 20 20 64 77 46 6c 61 67 73 41 6e 64 41 74 dwFlagsAndAt
feb30 74 72 69 62 75 74 65 73 20 3d 20 46 49 4c 45 5f tributes = FILE_
feb40 41 54 54 52 49 42 55 54 45 5f 48 49 44 44 45 4e ATTRIBUTE_HIDDEN
feb50 3b 0a 20 20 20 20 69 73 54 65 6d 70 20 3d 20 31 ;. isTemp = 1
feb60 3b 0a 23 65 6c 73 65 0a 20 20 20 20 64 77 46 6c ;.#else. dwFl
feb70 61 67 73 41 6e 64 41 74 74 72 69 62 75 74 65 73 agsAndAttributes
feb80 20 3d 20 46 49 4c 45 5f 41 54 54 52 49 42 55 54 = FILE_ATTRIBUT
feb90 45 5f 54 45 4d 50 4f 52 41 52 59 0a 20 20 20 20 E_TEMPORARY.
feba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
febb0 20 20 20 20 20 20 20 20 20 20 20 7c 20 46 49 4c | FIL
febc0 45 5f 41 54 54 52 49 42 55 54 45 5f 48 49 44 44 E_ATTRIBUTE_HIDD
febd0 45 4e 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 EN.
febe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
febf0 20 20 7c 20 46 49 4c 45 5f 46 4c 41 47 5f 44 45 | FILE_FLAG_DE
fec00 4c 45 54 45 5f 4f 4e 5f 43 4c 4f 53 45 3b 0a 23 LETE_ON_CLOSE;.#
fec10 65 6e 64 69 66 0a 20 20 7d 65 6c 73 65 7b 0a 20 endif. }else{.
fec20 20 20 20 64 77 46 6c 61 67 73 41 6e 64 41 74 74 dwFlagsAndAtt
fec30 72 69 62 75 74 65 73 20 3d 20 46 49 4c 45 5f 41 ributes = FILE_A
fec40 54 54 52 49 42 55 54 45 5f 4e 4f 52 4d 41 4c 3b TTRIBUTE_NORMAL;
fec50 0a 20 20 7d 0a 20 20 2f 2a 20 52 65 70 6f 72 74 . }. /* Report
fec60 73 20 66 72 6f 6d 20 74 68 65 20 69 6e 74 65 72 s from the inter
fec70 6e 65 74 20 61 72 65 20 74 68 61 74 20 70 65 72 net are that per
fec80 66 6f 72 6d 61 6e 63 65 20 69 73 20 61 6c 77 61 formance is alwa
fec90 79 73 0a 20 20 2a 2a 20 62 65 74 74 65 72 20 69 ys. ** better i
feca0 66 20 46 49 4c 45 5f 46 4c 41 47 5f 52 41 4e 44 f FILE_FLAG_RAND
fecb0 4f 4d 5f 41 43 43 45 53 53 20 69 73 20 75 73 65 OM_ACCESS is use
fecc0 64 2e 20 20 54 69 63 6b 65 74 20 23 32 36 39 39 d. Ticket #2699
fecd0 2e 20 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f . */.#if SQLITE_
fece0 4f 53 5f 57 49 4e 43 45 0a 20 20 64 77 46 6c 61 OS_WINCE. dwFla
fecf0 67 73 41 6e 64 41 74 74 72 69 62 75 74 65 73 20 gsAndAttributes
fed00 7c 3d 20 46 49 4c 45 5f 46 4c 41 47 5f 52 41 4e |= FILE_FLAG_RAN
fed10 44 4f 4d 5f 41 43 43 45 53 53 3b 0a 23 65 6e 64 DOM_ACCESS;.#end
fed20 69 66 0a 20 20 69 66 28 20 69 73 4e 54 28 29 20 if. if( isNT()
fed30 29 7b 0a 20 20 20 20 68 20 3d 20 43 72 65 61 74 ){. h = Creat
fed40 65 46 69 6c 65 57 28 28 57 43 48 41 52 2a 29 7a eFileW((WCHAR*)z
fed50 43 6f 6e 76 65 72 74 65 64 2c 0a 20 20 20 20 20 Converted,.
fed60 20 20 64 77 44 65 73 69 72 65 64 41 63 63 65 73 dwDesiredAcces
fed70 73 2c 0a 20 20 20 20 20 20 20 64 77 53 68 61 72 s,. dwShar
fed80 65 4d 6f 64 65 2c 0a 20 20 20 20 20 20 20 4e 55 eMode,. NU
fed90 4c 4c 2c 0a 20 20 20 20 20 20 20 64 77 43 72 65 LL,. dwCre
feda0 61 74 69 6f 6e 44 69 73 70 6f 73 69 74 69 6f 6e ationDisposition
fedb0 2c 0a 20 20 20 20 20 20 20 64 77 46 6c 61 67 73 ,. dwFlags
fedc0 41 6e 64 41 74 74 72 69 62 75 74 65 73 2c 0a 20 AndAttributes,.
fedd0 20 20 20 20 20 20 4e 55 4c 4c 0a 20 20 20 20 29 NULL. )
fede0 3b 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 20 31 ;./* isNT() is 1
fedf0 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 if SQLITE_OS_WI
fee00 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73 20 NCE==1, so this
fee10 65 6c 73 65 20 69 73 20 6e 65 76 65 72 20 65 78 else is never ex
fee20 65 63 75 74 65 64 2e 20 0a 2a 2a 20 53 69 6e 63 ecuted. .** Sinc
fee30 65 20 74 68 65 20 41 53 43 49 49 20 76 65 72 73 e the ASCII vers
fee40 69 6f 6e 20 6f 66 20 74 68 65 73 65 20 57 69 6e ion of these Win
fee50 64 6f 77 73 20 41 50 49 20 64 6f 20 6e 6f 74 20 dows API do not
fee60 65 78 69 73 74 20 66 6f 72 20 57 49 4e 43 45 2c exist for WINCE,
fee70 0a 2a 2a 20 69 74 27 73 20 69 6d 70 6f 72 74 61 .** it's importa
fee80 6e 74 20 74 6f 20 6e 6f 74 20 72 65 66 65 72 65 nt to not refere
fee90 6e 63 65 20 74 68 65 6d 20 66 6f 72 20 57 49 4e nce them for WIN
feea0 43 45 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a 23 69 CE builds..*/.#i
feeb0 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 f SQLITE_OS_WINC
feec0 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 E==0. }else{.
feed0 20 20 68 20 3d 20 43 72 65 61 74 65 46 69 6c 65 h = CreateFile
feee0 41 28 28 63 68 61 72 2a 29 7a 43 6f 6e 76 65 72 A((char*)zConver
feef0 74 65 64 2c 0a 20 20 20 20 20 20 20 64 77 44 65 ted,. dwDe
fef00 73 69 72 65 64 41 63 63 65 73 73 2c 0a 20 20 20 siredAccess,.
fef10 20 20 20 20 64 77 53 68 61 72 65 4d 6f 64 65 2c dwShareMode,
fef20 0a 20 20 20 20 20 20 20 4e 55 4c 4c 2c 0a 20 20 . NULL,.
fef30 20 20 20 20 20 64 77 43 72 65 61 74 69 6f 6e 44 dwCreationD
fef40 69 73 70 6f 73 69 74 69 6f 6e 2c 0a 20 20 20 20 isposition,.
fef50 20 20 20 64 77 46 6c 61 67 73 41 6e 64 41 74 74 dwFlagsAndAtt
fef60 72 69 62 75 74 65 73 2c 0a 20 20 20 20 20 20 20 ributes,.
fef70 4e 55 4c 4c 0a 20 20 20 20 29 3b 0a 23 65 6e 64 NULL. );.#end
fef80 69 66 0a 20 20 7d 0a 20 20 69 66 28 20 68 3d 3d if. }. if( h==
fef90 49 4e 56 41 4c 49 44 5f 48 41 4e 44 4c 45 5f 56 INVALID_HANDLE_V
fefa0 41 4c 55 45 20 29 7b 0a 20 20 20 20 66 72 65 65 ALUE ){. free
fefb0 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 (zConverted);.
fefc0 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 if( flags & SQ
fefd0 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 LITE_OPEN_READWR
fefe0 49 54 45 20 29 7b 0a 20 20 20 20 20 20 72 65 74 ITE ){. ret
feff0 75 72 6e 20 77 69 6e 4f 70 65 6e 28 70 56 66 73 urn winOpen(pVfs
ff000 2c 20 7a 4e 61 6d 65 2c 20 69 64 2c 20 0a 20 20 , zName, id, .
ff010 20 20 20 20 20 20 20 20 20 20 20 28 28 66 6c 61 ((fla
ff020 67 73 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 gs|SQLITE_OPEN_R
ff030 45 41 44 4f 4e 4c 59 29 26 7e 53 51 4c 49 54 45 EADONLY)&~SQLITE
ff040 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 29 _OPEN_READWRITE)
ff050 2c 20 70 4f 75 74 46 6c 61 67 73 29 3b 0a 20 20 , pOutFlags);.
ff060 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 }else{. r
ff070 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 41 4e eturn SQLITE_CAN
ff080 54 4f 50 45 4e 3b 0a 20 20 20 20 7d 0a 20 20 7d TOPEN;. }. }
ff090 0a 20 20 69 66 28 20 70 4f 75 74 46 6c 61 67 73 . if( pOutFlags
ff0a0 20 29 7b 0a 20 20 20 20 69 66 28 20 66 6c 61 67 ){. if( flag
ff0b0 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f s & SQLITE_OPEN_
ff0c0 52 45 41 44 57 52 49 54 45 20 29 7b 0a 20 20 20 READWRITE ){.
ff0d0 20 20 20 2a 70 4f 75 74 46 6c 61 67 73 20 3d 20 *pOutFlags =
ff0e0 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 SQLITE_OPEN_READ
ff0f0 57 52 49 54 45 3b 0a 20 20 20 20 7d 65 6c 73 65 WRITE;. }else
ff100 7b 0a 20 20 20 20 20 20 2a 70 4f 75 74 46 6c 61 {. *pOutFla
ff110 67 73 20 3d 20 53 51 4c 49 54 45 5f 4f 50 45 4e gs = SQLITE_OPEN
ff120 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 20 20 7d _READONLY;. }
ff130 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28 70 46 . }. memset(pF
ff140 69 6c 65 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a ile, 0, sizeof(*
ff150 70 46 69 6c 65 29 29 3b 0a 20 20 70 46 69 6c 65 pFile));. pFile
ff160 2d 3e 70 4d 65 74 68 6f 64 20 3d 20 26 77 69 6e ->pMethod = &win
ff170 49 6f 4d 65 74 68 6f 64 3b 0a 20 20 70 46 69 6c IoMethod;. pFil
ff180 65 2d 3e 68 20 3d 20 68 3b 0a 20 20 70 46 69 6c e->h = h;. pFil
ff190 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 4e e->lastErrno = N
ff1a0 4f 5f 45 52 52 4f 52 3b 0a 20 20 70 46 69 6c 65 O_ERROR;. pFile
ff1b0 2d 3e 73 65 63 74 6f 72 53 69 7a 65 20 3d 20 67 ->sectorSize = g
ff1c0 65 74 53 65 63 74 6f 72 53 69 7a 65 28 70 56 66 etSectorSize(pVf
ff1d0 73 2c 20 7a 55 74 66 38 4e 61 6d 65 29 3b 0a 23 s, zUtf8Name);.#
ff1e0 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e if SQLITE_OS_WIN
ff1f0 43 45 0a 20 20 69 66 28 20 28 66 6c 61 67 73 20 CE. if( (flags
ff200 26 20 28 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 & (SQLITE_OPEN_R
ff210 45 41 44 57 52 49 54 45 7c 53 51 4c 49 54 45 5f EADWRITE|SQLITE_
ff220 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 29 29 20 3d OPEN_MAIN_DB)) =
ff230 3d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 =.
ff240 20 28 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 (SQLITE_OPEN_RE
ff250 41 44 57 52 49 54 45 7c 53 51 4c 49 54 45 5f 4f ADWRITE|SQLITE_O
ff260 50 45 4e 5f 4d 41 49 4e 5f 44 42 29 0a 20 20 20 PEN_MAIN_DB).
ff270 20 20 20 20 26 26 20 21 77 69 6e 63 65 43 72 65 && !winceCre
ff280 61 74 65 4c 6f 63 6b 28 7a 4e 61 6d 65 2c 20 70 ateLock(zName, p
ff290 46 69 6c 65 29 0a 20 20 29 7b 0a 20 20 20 20 43 File). ){. C
ff2a0 6c 6f 73 65 48 61 6e 64 6c 65 28 68 29 3b 0a 20 loseHandle(h);.
ff2b0 20 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 72 74 free(zConvert
ff2c0 65 64 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 ed);. return
ff2d0 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b SQLITE_CANTOPEN;
ff2e0 0a 20 20 7d 0a 20 20 69 66 28 20 69 73 54 65 6d . }. if( isTem
ff2f0 70 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e p ){. pFile->
ff300 7a 44 65 6c 65 74 65 4f 6e 43 6c 6f 73 65 20 3d zDeleteOnClose =
ff310 20 7a 43 6f 6e 76 65 72 74 65 64 3b 0a 20 20 7d zConverted;. }
ff320 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 7b 0a else.#endif. {.
ff330 20 20 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 72 free(zConver
ff340 74 65 64 29 3b 0a 20 20 7d 0a 20 20 4f 70 65 6e ted);. }. Open
ff350 43 6f 75 6e 74 65 72 28 2b 31 29 3b 0a 20 20 72 Counter(+1);. r
ff360 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
ff370 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 .}../*.** Delete
ff380 20 74 68 65 20 6e 61 6d 65 64 20 66 69 6c 65 2e the named file.
ff390 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 .**.** Note that
ff3a0 20 77 69 6e 64 6f 77 73 20 64 6f 65 73 20 6e 6f windows does no
ff3b0 74 20 61 6c 6c 6f 77 20 61 20 66 69 6c 65 20 74 t allow a file t
ff3c0 6f 20 62 65 20 64 65 6c 65 74 65 64 20 69 66 20 o be deleted if
ff3d0 73 6f 6d 65 20 6f 74 68 65 72 0a 2a 2a 20 70 72 some other.** pr
ff3e0 6f 63 65 73 73 20 68 61 73 20 69 74 20 6f 70 65 ocess has it ope
ff3f0 6e 2e 20 20 53 6f 6d 65 74 69 6d 65 73 20 61 20 n. Sometimes a
ff400 76 69 72 75 73 20 73 63 61 6e 6e 65 72 20 6f 72 virus scanner or
ff410 20 69 6e 64 65 78 69 6e 67 20 70 72 6f 67 72 61 indexing progra
ff420 6d 0a 2a 2a 20 77 69 6c 6c 20 6f 70 65 6e 20 61 m.** will open a
ff430 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 73 68 journal file sh
ff440 6f 72 74 6c 79 20 61 66 74 65 72 20 69 74 20 69 ortly after it i
ff450 73 20 63 72 65 61 74 65 64 20 69 6e 20 6f 72 64 s created in ord
ff460 65 72 20 74 6f 20 64 6f 0a 2a 2a 20 77 68 61 74 er to do.** what
ff470 65 76 65 72 20 69 74 20 64 6f 65 73 2e 20 20 57 ever it does. W
ff480 68 69 6c 65 20 74 68 69 73 20 6f 74 68 65 72 20 hile this other
ff490 70 72 6f 63 65 73 73 20 69 73 20 68 6f 6c 64 69 process is holdi
ff4a0 6e 67 20 74 68 65 0a 2a 2a 20 66 69 6c 65 20 6f ng the.** file o
ff4b0 70 65 6e 2c 20 77 65 20 77 69 6c 6c 20 62 65 20 pen, we will be
ff4c0 75 6e 61 62 6c 65 20 74 6f 20 64 65 6c 65 74 65 unable to delete
ff4d0 20 69 74 2e 20 20 54 6f 20 77 6f 72 6b 20 61 72 it. To work ar
ff4e0 6f 75 6e 64 20 74 68 69 73 0a 2a 2a 20 70 72 6f ound this.** pro
ff4f0 62 6c 65 6d 2c 20 77 65 20 64 65 6c 61 79 20 31 blem, we delay 1
ff500 30 30 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 00 milliseconds
ff510 61 6e 64 20 74 72 79 20 74 6f 20 64 65 6c 65 74 and try to delet
ff520 65 20 61 67 61 69 6e 2e 20 20 55 70 0a 2a 2a 20 e again. Up.**
ff530 74 6f 20 4d 58 5f 44 45 4c 45 54 49 4f 4e 5f 41 to MX_DELETION_A
ff540 54 54 45 4d 50 54 73 20 64 65 6c 65 74 69 6f 6e TTEMPTs deletion
ff550 20 61 74 74 65 6d 70 74 73 20 61 72 65 20 72 75 attempts are ru
ff560 6e 20 62 65 66 6f 72 65 20 67 69 76 69 6e 67 0a n before giving.
ff570 2a 2a 20 75 70 20 61 6e 64 20 72 65 74 75 72 6e ** up and return
ff580 69 6e 67 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f ing an error..*/
ff590 0a 23 64 65 66 69 6e 65 20 4d 58 5f 44 45 4c 45 .#define MX_DELE
ff5a0 54 49 4f 4e 5f 41 54 54 45 4d 50 54 53 20 35 0a TION_ATTEMPTS 5.
ff5b0 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 44 65 static int winDe
ff5c0 6c 65 74 65 28 0a 20 20 73 71 6c 69 74 65 33 5f lete(. sqlite3_
ff5d0 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20 vfs *pVfs,
ff5e0 20 20 20 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20 /* Not used
ff5f0 6f 6e 20 77 69 6e 33 32 20 2a 2f 0a 20 20 63 6f on win32 */. co
ff600 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e nst char *zFilen
ff610 61 6d 65 2c 20 20 20 20 20 20 2f 2a 20 4e 61 6d ame, /* Nam
ff620 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 64 65 6c e of file to del
ff630 65 74 65 20 2a 2f 0a 20 20 69 6e 74 20 73 79 6e ete */. int syn
ff640 63 44 69 72 20 20 20 20 20 20 20 20 20 20 20 20 cDir
ff650 20 20 20 20 20 2f 2a 20 4e 6f 74 20 75 73 65 64 /* Not used
ff660 20 6f 6e 20 77 69 6e 33 32 20 2a 2f 0a 29 7b 0a on win32 */.){.
ff670 20 20 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20 int cnt = 0;.
ff680 20 44 57 4f 52 44 20 72 63 3b 0a 20 20 44 57 4f DWORD rc;. DWO
ff690 52 44 20 65 72 72 6f 72 20 3d 20 30 3b 0a 20 20 RD error = 0;.
ff6a0 76 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 74 65 64 void *zConverted
ff6b0 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 46 69 = convertUtf8Fi
ff6c0 6c 65 6e 61 6d 65 28 7a 46 69 6c 65 6e 61 6d 65 lename(zFilename
ff6d0 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 );. UNUSED_PARA
ff6e0 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 20 20 55 METER(pVfs);. U
ff6f0 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
ff700 73 79 6e 63 44 69 72 29 3b 0a 20 20 69 66 28 20 syncDir);. if(
ff710 7a 43 6f 6e 76 65 72 74 65 64 3d 3d 30 20 29 7b zConverted==0 ){
ff720 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
ff730 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 TE_NOMEM;. }.
ff740 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 SimulateIOError(
ff750 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f return SQLITE_IO
ff760 45 52 52 5f 44 45 4c 45 54 45 29 3b 0a 20 20 69 ERR_DELETE);. i
ff770 66 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 f( isNT() ){.
ff780 20 64 6f 7b 0a 20 20 20 20 20 20 44 65 6c 65 74 do{. Delet
ff790 65 46 69 6c 65 57 28 7a 43 6f 6e 76 65 72 74 65 eFileW(zConverte
ff7a0 64 29 3b 0a 20 20 20 20 7d 77 68 69 6c 65 28 20 d);. }while(
ff7b0 20 20 28 20 20 20 28 28 72 63 20 3d 20 47 65 74 ( ((rc = Get
ff7c0 46 69 6c 65 41 74 74 72 69 62 75 74 65 73 57 28 FileAttributesW(
ff7d0 7a 43 6f 6e 76 65 72 74 65 64 29 29 20 21 3d 20 zConverted)) !=
ff7e0 49 4e 56 41 4c 49 44 5f 46 49 4c 45 5f 41 54 54 INVALID_FILE_ATT
ff7f0 52 49 42 55 54 45 53 29 0a 20 20 20 20 20 20 20 RIBUTES).
ff800 20 20 20 20 20 20 20 20 7c 7c 20 28 28 65 72 72 || ((err
ff810 6f 72 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f or = GetLastErro
ff820 72 28 29 29 20 3d 3d 20 45 52 52 4f 52 5f 41 43 r()) == ERROR_AC
ff830 43 45 53 53 5f 44 45 4e 49 45 44 29 29 0a 20 20 CESS_DENIED)).
ff840 20 20 20 20 20 20 20 20 20 26 26 20 28 2b 2b 63 && (++c
ff850 6e 74 20 3c 20 4d 58 5f 44 45 4c 45 54 49 4f 4e nt < MX_DELETION
ff860 5f 41 54 54 45 4d 50 54 53 29 0a 20 20 20 20 20 _ATTEMPTS).
ff870 20 20 20 20 20 20 26 26 20 28 53 6c 65 65 70 28 && (Sleep(
ff880 31 30 30 29 2c 20 31 29 20 29 3b 0a 2f 2a 20 69 100), 1) );./* i
ff890 73 4e 54 28 29 20 69 73 20 31 20 69 66 20 53 51 sNT() is 1 if SQ
ff8a0 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 LITE_OS_WINCE==1
ff8b0 2c 20 73 6f 20 74 68 69 73 20 65 6c 73 65 20 69 , so this else i
ff8c0 73 20 6e 65 76 65 72 20 65 78 65 63 75 74 65 64 s never executed
ff8d0 2e 20 0a 2a 2a 20 53 69 6e 63 65 20 74 68 65 20 . .** Since the
ff8e0 41 53 43 49 49 20 76 65 72 73 69 6f 6e 20 6f 66 ASCII version of
ff8f0 20 74 68 65 73 65 20 57 69 6e 64 6f 77 73 20 41 these Windows A
ff900 50 49 20 64 6f 20 6e 6f 74 20 65 78 69 73 74 20 PI do not exist
ff910 66 6f 72 20 57 49 4e 43 45 2c 0a 2a 2a 20 69 74 for WINCE,.** it
ff920 27 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 's important to
ff930 6e 6f 74 20 72 65 66 65 72 65 6e 63 65 20 74 68 not reference th
ff940 65 6d 20 66 6f 72 20 57 49 4e 43 45 20 62 75 69 em for WINCE bui
ff950 6c 64 73 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 lds..*/.#if SQLI
ff960 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 TE_OS_WINCE==0.
ff970 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 6f 7b 0a }else{. do{.
ff980 20 20 20 20 20 20 44 65 6c 65 74 65 46 69 6c 65 DeleteFile
ff990 41 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 A(zConverted);.
ff9a0 20 20 20 7d 77 68 69 6c 65 28 20 20 20 28 20 20 }while( (
ff9b0 20 28 28 72 63 20 3d 20 47 65 74 46 69 6c 65 41 ((rc = GetFileA
ff9c0 74 74 72 69 62 75 74 65 73 41 28 7a 43 6f 6e 76 ttributesA(zConv
ff9d0 65 72 74 65 64 29 29 20 21 3d 20 49 4e 56 41 4c erted)) != INVAL
ff9e0 49 44 5f 46 49 4c 45 5f 41 54 54 52 49 42 55 54 ID_FILE_ATTRIBUT
ff9f0 45 53 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 ES).
ffa00 20 20 20 7c 7c 20 28 28 65 72 72 6f 72 20 3d 20 || ((error =
ffa10 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 29 20 GetLastError())
ffa20 3d 3d 20 45 52 52 4f 52 5f 41 43 43 45 53 53 5f == ERROR_ACCESS_
ffa30 44 45 4e 49 45 44 29 29 0a 20 20 20 20 20 20 20 DENIED)).
ffa40 20 20 20 20 26 26 20 28 2b 2b 63 6e 74 20 3c 20 && (++cnt <
ffa50 4d 58 5f 44 45 4c 45 54 49 4f 4e 5f 41 54 54 45 MX_DELETION_ATTE
ffa60 4d 50 54 53 29 0a 20 20 20 20 20 20 20 20 20 20 MPTS).
ffa70 20 26 26 20 28 53 6c 65 65 70 28 31 30 30 29 2c && (Sleep(100),
ffa80 20 31 29 20 29 3b 0a 23 65 6e 64 69 66 0a 20 20 1) );.#endif.
ffa90 7d 0a 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 72 }. free(zConver
ffaa0 74 65 64 29 3b 0a 20 20 4f 53 54 52 41 43 45 32 ted);. OSTRACE2
ffab0 28 22 44 45 4c 45 54 45 20 5c 22 25 73 5c 22 5c ("DELETE \"%s\"\
ffac0 6e 22 2c 20 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a n", zFilename);.
ffad0 20 20 72 65 74 75 72 6e 20 28 20 20 20 28 72 63 return ( (rc
ffae0 20 3d 3d 20 49 4e 56 41 4c 49 44 5f 46 49 4c 45 == INVALID_FILE
ffaf0 5f 41 54 54 52 49 42 55 54 45 53 29 20 0a 20 20 _ATTRIBUTES) .
ffb00 20 20 20 20 20 20 20 20 26 26 20 28 65 72 72 6f && (erro
ffb10 72 20 3d 3d 20 45 52 52 4f 52 5f 46 49 4c 45 5f r == ERROR_FILE_
ffb20 4e 4f 54 5f 46 4f 55 4e 44 29 29 20 3f 20 53 51 NOT_FOUND)) ? SQ
ffb30 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 LITE_OK : SQLITE
ffb40 5f 49 4f 45 52 52 5f 44 45 4c 45 54 45 3b 0a 7d _IOERR_DELETE;.}
ffb50 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68 ../*.** Check th
ffb60 65 20 65 78 69 73 74 61 6e 63 65 20 61 6e 64 20 e existance and
ffb70 73 74 61 74 75 73 20 6f 66 20 61 20 66 69 6c 65 status of a file
ffb80 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
ffb90 77 69 6e 41 63 63 65 73 73 28 0a 20 20 73 71 6c winAccess(. sql
ffba0 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 ite3_vfs *pVfs,
ffbb0 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 75 /* Not u
ffbc0 73 65 64 20 6f 6e 20 77 69 6e 33 32 20 2a 2f 0a sed on win32 */.
ffbd0 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 const char *zF
ffbe0 69 6c 65 6e 61 6d 65 2c 20 20 20 20 20 2f 2a 20 ilename, /*
ffbf0 4e 61 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 Name of file to
ffc00 63 68 65 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 66 check */. int f
ffc10 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 lags,
ffc20 20 20 20 20 20 20 2f 2a 20 54 79 70 65 20 6f 66 /* Type of
ffc30 20 74 65 73 74 20 74 6f 20 6d 61 6b 65 20 6f 6e test to make on
ffc40 20 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a 20 20 this file */.
ffc50 69 6e 74 20 2a 70 52 65 73 4f 75 74 20 20 20 20 int *pResOut
ffc60 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 /* OU
ffc70 54 3a 20 52 65 73 75 6c 74 20 2a 2f 0a 29 7b 0a T: Result */.){.
ffc80 20 20 44 57 4f 52 44 20 61 74 74 72 3b 0a 20 20 DWORD attr;.
ffc90 69 6e 74 20 72 63 20 3d 20 30 3b 0a 20 20 76 6f int rc = 0;. vo
ffca0 69 64 20 2a 7a 43 6f 6e 76 65 72 74 65 64 20 3d id *zConverted =
ffcb0 20 63 6f 6e 76 65 72 74 55 74 66 38 46 69 6c 65 convertUtf8File
ffcc0 6e 61 6d 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b name(zFilename);
ffcd0 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
ffce0 54 45 52 28 70 56 66 73 29 3b 0a 20 20 69 66 28 TER(pVfs);. if(
ffcf0 20 7a 43 6f 6e 76 65 72 74 65 64 3d 3d 30 20 29 zConverted==0 )
ffd00 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
ffd10 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 ITE_NOMEM;. }.
ffd20 20 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 if( isNT() ){.
ffd30 20 20 20 61 74 74 72 20 3d 20 47 65 74 46 69 6c attr = GetFil
ffd40 65 41 74 74 72 69 62 75 74 65 73 57 28 28 57 43 eAttributesW((WC
ffd50 48 41 52 2a 29 7a 43 6f 6e 76 65 72 74 65 64 29 HAR*)zConverted)
ffd60 3b 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 20 31 ;./* isNT() is 1
ffd70 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 if SQLITE_OS_WI
ffd80 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73 20 NCE==1, so this
ffd90 65 6c 73 65 20 69 73 20 6e 65 76 65 72 20 65 78 else is never ex
ffda0 65 63 75 74 65 64 2e 20 0a 2a 2a 20 53 69 6e 63 ecuted. .** Sinc
ffdb0 65 20 74 68 65 20 41 53 43 49 49 20 76 65 72 73 e the ASCII vers
ffdc0 69 6f 6e 20 6f 66 20 74 68 65 73 65 20 57 69 6e ion of these Win
ffdd0 64 6f 77 73 20 41 50 49 20 64 6f 20 6e 6f 74 20 dows API do not
ffde0 65 78 69 73 74 20 66 6f 72 20 57 49 4e 43 45 2c exist for WINCE,
ffdf0 0a 2a 2a 20 69 74 27 73 20 69 6d 70 6f 72 74 61 .** it's importa
ffe00 6e 74 20 74 6f 20 6e 6f 74 20 72 65 66 65 72 65 nt to not refere
ffe10 6e 63 65 20 74 68 65 6d 20 66 6f 72 20 57 49 4e nce them for WIN
ffe20 43 45 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a 23 69 CE builds..*/.#i
ffe30 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 f SQLITE_OS_WINC
ffe40 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 E==0. }else{.
ffe50 20 20 61 74 74 72 20 3d 20 47 65 74 46 69 6c 65 attr = GetFile
ffe60 41 74 74 72 69 62 75 74 65 73 41 28 28 63 68 61 AttributesA((cha
ffe70 72 2a 29 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a r*)zConverted);.
ffe80 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 66 72 65 #endif. }. fre
ffe90 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 e(zConverted);.
ffea0 20 73 77 69 74 63 68 28 20 66 6c 61 67 73 20 29 switch( flags )
ffeb0 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 {. case SQLIT
ffec0 45 5f 41 43 43 45 53 53 5f 52 45 41 44 3a 0a 20 E_ACCESS_READ:.
ffed0 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 41 case SQLITE_A
ffee0 43 43 45 53 53 5f 45 58 49 53 54 53 3a 0a 20 20 CCESS_EXISTS:.
ffef0 20 20 20 20 72 63 20 3d 20 61 74 74 72 21 3d 49 rc = attr!=I
fff00 4e 56 41 4c 49 44 5f 46 49 4c 45 5f 41 54 54 52 NVALID_FILE_ATTR
fff10 49 42 55 54 45 53 3b 0a 20 20 20 20 20 20 62 72 IBUTES;. br
fff20 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53 51 eak;. case SQ
fff30 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 LITE_ACCESS_READ
fff40 57 52 49 54 45 3a 0a 20 20 20 20 20 20 72 63 20 WRITE:. rc
fff50 3d 20 28 61 74 74 72 20 26 20 46 49 4c 45 5f 41 = (attr & FILE_A
fff60 54 54 52 49 42 55 54 45 5f 52 45 41 44 4f 4e 4c TTRIBUTE_READONL
fff70 59 29 3d 3d 30 3b 0a 20 20 20 20 20 20 62 72 65 Y)==0;. bre
fff80 61 6b 3b 0a 20 20 20 20 64 65 66 61 75 6c 74 3a ak;. default:
fff90 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 21 22 . assert(!"
fffa0 49 6e 76 61 6c 69 64 20 66 6c 61 67 73 20 61 72 Invalid flags ar
fffb0 67 75 6d 65 6e 74 22 29 3b 0a 20 20 7d 0a 20 20 gument");. }.
fffc0 2a 70 52 65 73 4f 75 74 20 3d 20 72 63 3b 0a 20 *pResOut = rc;.
fffd0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
fffe0 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 75 72 K;.}.../*.** Tur
ffff0 6e 20 61 20 72 65 6c 61 74 69 76 65 20 70 61 74 n a relative pat
10000 68 6e 61 6d 65 20 69 6e 74 6f 20 61 20 66 75 6c hname into a ful
10001 6c 20 70 61 74 68 6e 61 6d 65 2e 20 20 57 72 69 l pathname. Wri
10002 74 65 20 74 68 65 20 66 75 6c 6c 0a 2a 2a 20 70 te the full.** p
10003 61 74 68 6e 61 6d 65 20 69 6e 74 6f 20 7a 4f 75 athname into zOu
10004 74 5b 5d 2e 20 20 7a 4f 75 74 5b 5d 20 77 69 6c t[]. zOut[] wil
10005 6c 20 62 65 20 61 74 20 6c 65 61 73 74 20 70 56 l be at least pV
10006 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 0a 2a fs->mxPathname.*
10007 2a 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2e * bytes in size.
10008 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 .*/.static int w
10009 69 6e 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 0a inFullPathname(.
1000a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 sqlite3_vfs *p
1000b 56 66 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 Vfs,
1000c 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 76 66 /* Pointer to vf
1000d 73 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 63 6f s object */. co
1000e 6e 73 74 20 63 68 61 72 20 2a 7a 52 65 6c 61 74 nst char *zRelat
1000f 69 76 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 ive, /* P
10010 6f 73 73 69 62 6c 79 20 72 65 6c 61 74 69 76 65 ossibly relative
10011 20 69 6e 70 75 74 20 70 61 74 68 20 2a 2f 0a 20 input path */.
10012 20 69 6e 74 20 6e 46 75 6c 6c 2c 20 20 20 20 20 int nFull,
10013 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
10014 2a 20 53 69 7a 65 20 6f 66 20 6f 75 74 70 75 74 * Size of output
10015 20 62 75 66 66 65 72 20 69 6e 20 62 79 74 65 73 buffer in bytes
10016 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 46 75 6c */. char *zFul
10017 6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 l
10018 20 20 20 20 2f 2a 20 4f 75 74 70 75 74 20 62 75 /* Output bu
10019 66 66 65 72 20 2a 2f 0a 29 7b 0a 20 20 0a 23 69 ffer */.){. .#i
1001a 66 20 64 65 66 69 6e 65 64 28 5f 5f 43 59 47 57 f defined(__CYGW
1001b 49 4e 5f 5f 29 0a 20 20 55 4e 55 53 45 44 5f 50 IN__). UNUSED_P
1001c 41 52 41 4d 45 54 45 52 28 6e 46 75 6c 6c 29 3b ARAMETER(nFull);
1001d 0a 20 20 63 79 67 77 69 6e 5f 63 6f 6e 76 5f 74 . cygwin_conv_t
1001e 6f 5f 66 75 6c 6c 5f 77 69 6e 33 32 5f 70 61 74 o_full_win32_pat
1001f 68 28 7a 52 65 6c 61 74 69 76 65 2c 20 7a 46 75 h(zRelative, zFu
10020 6c 6c 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 ll);. return SQ
10021 4c 49 54 45 5f 4f 4b 3b 0a 23 65 6e 64 69 66 0a LITE_OK;.#endif.
10022 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 .#if SQLITE_OS_W
10023 49 4e 43 45 0a 20 20 55 4e 55 53 45 44 5f 50 41 INCE. UNUSED_PA
10024 52 41 4d 45 54 45 52 28 6e 46 75 6c 6c 29 3b 0a RAMETER(nFull);.
10025 20 20 2f 2a 20 57 69 6e 43 45 20 68 61 73 20 6e /* WinCE has n
10026 6f 20 63 6f 6e 63 65 70 74 20 6f 66 20 61 20 72 o concept of a r
10027 65 6c 61 74 69 76 65 20 70 61 74 68 6e 61 6d 65 elative pathname
10028 2c 20 6f 72 20 73 6f 20 49 20 61 6d 20 74 6f 6c , or so I am tol
10029 64 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f d. */. sqlite3_
1002a 73 6e 70 72 69 6e 74 66 28 70 56 66 73 2d 3e 6d snprintf(pVfs->m
1002b 78 50 61 74 68 6e 61 6d 65 2c 20 7a 46 75 6c 6c xPathname, zFull
1002c 2c 20 22 25 73 22 2c 20 7a 52 65 6c 61 74 69 76 , "%s", zRelativ
1002d 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c e);. return SQL
1002e 49 54 45 5f 4f 4b 3b 0a 23 65 6e 64 69 66 0a 0a ITE_OK;.#endif..
1002f 23 69 66 20 21 53 51 4c 49 54 45 5f 4f 53 5f 57 #if !SQLITE_OS_W
10030 49 4e 43 45 20 26 26 20 21 64 65 66 69 6e 65 64 INCE && !defined
10031 28 5f 5f 43 59 47 57 49 4e 5f 5f 29 0a 20 20 69 (__CYGWIN__). i
10032 6e 74 20 6e 42 79 74 65 3b 0a 20 20 76 6f 69 64 nt nByte;. void
10033 20 2a 7a 43 6f 6e 76 65 72 74 65 64 3b 0a 20 20 *zConverted;.
10034 63 68 61 72 20 2a 7a 4f 75 74 3b 0a 20 20 55 4e char *zOut;. UN
10035 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 6e USED_PARAMETER(n
10036 46 75 6c 6c 29 3b 0a 20 20 7a 43 6f 6e 76 65 72 Full);. zConver
10037 74 65 64 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 ted = convertUtf
10038 38 46 69 6c 65 6e 61 6d 65 28 7a 52 65 6c 61 74 8Filename(zRelat
10039 69 76 65 29 3b 0a 20 20 69 66 28 20 69 73 4e 54 ive);. if( isNT
1003a 28 29 20 29 7b 0a 20 20 20 20 57 43 48 41 52 20 () ){. WCHAR
1003b 2a 7a 54 65 6d 70 3b 0a 20 20 20 20 6e 42 79 74 *zTemp;. nByt
1003c 65 20 3d 20 47 65 74 46 75 6c 6c 50 61 74 68 4e e = GetFullPathN
1003d 61 6d 65 57 28 28 57 43 48 41 52 2a 29 7a 43 6f ameW((WCHAR*)zCo
1003e 6e 76 65 72 74 65 64 2c 20 30 2c 20 30 2c 20 30 nverted, 0, 0, 0
1003f 29 20 2b 20 33 3b 0a 20 20 20 20 7a 54 65 6d 70 ) + 3;. zTemp
10040 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e 42 79 74 65 = malloc( nByte
10041 2a 73 69 7a 65 6f 66 28 7a 54 65 6d 70 5b 30 5d *sizeof(zTemp[0]
10042 29 20 29 3b 0a 20 20 20 20 69 66 28 20 7a 54 65 ) );. if( zTe
10043 6d 70 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 66 mp==0 ){. f
10044 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b ree(zConverted);
10045 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 . return SQ
10046 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 LITE_NOMEM;.
10047 7d 0a 20 20 20 20 47 65 74 46 75 6c 6c 50 61 74 }. GetFullPat
10048 68 4e 61 6d 65 57 28 28 57 43 48 41 52 2a 29 7a hNameW((WCHAR*)z
10049 43 6f 6e 76 65 72 74 65 64 2c 20 6e 42 79 74 65 Converted, nByte
1004a 2c 20 7a 54 65 6d 70 2c 20 30 29 3b 0a 20 20 20 , zTemp, 0);.
1004b 20 66 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 free(zConverted
1004c 29 3b 0a 20 20 20 20 7a 4f 75 74 20 3d 20 75 6e );. zOut = un
1004d 69 63 6f 64 65 54 6f 55 74 66 38 28 7a 54 65 6d icodeToUtf8(zTem
1004e 70 29 3b 0a 20 20 20 20 66 72 65 65 28 7a 54 65 p);. free(zTe
1004f 6d 70 29 3b 0a 2f 2a 20 69 73 4e 54 28 29 20 69 mp);./* isNT() i
10050 73 20 31 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 s 1 if SQLITE_OS
10051 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 68 _WINCE==1, so th
10052 69 73 20 65 6c 73 65 20 69 73 20 6e 65 76 65 72 is else is never
10053 20 65 78 65 63 75 74 65 64 2e 20 0a 2a 2a 20 53 executed. .** S
10054 69 6e 63 65 20 74 68 65 20 41 53 43 49 49 20 76 ince the ASCII v
10055 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 73 65 20 ersion of these
10056 57 69 6e 64 6f 77 73 20 41 50 49 20 64 6f 20 6e Windows API do n
10057 6f 74 20 65 78 69 73 74 20 66 6f 72 20 57 49 4e ot exist for WIN
10058 43 45 2c 0a 2a 2a 20 69 74 27 73 20 69 6d 70 6f CE,.** it's impo
10059 72 74 61 6e 74 20 74 6f 20 6e 6f 74 20 72 65 66 rtant to not ref
1005a 65 72 65 6e 63 65 20 74 68 65 6d 20 66 6f 72 20 erence them for
1005b 57 49 4e 43 45 20 62 75 69 6c 64 73 2e 0a 2a 2f WINCE builds..*/
1005c 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 .#if SQLITE_OS_W
1005d 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b INCE==0. }else{
1005e 0a 20 20 20 20 63 68 61 72 20 2a 7a 54 65 6d 70 . char *zTemp
1005f 3b 0a 20 20 20 20 6e 42 79 74 65 20 3d 20 47 65 ;. nByte = Ge
10060 74 46 75 6c 6c 50 61 74 68 4e 61 6d 65 41 28 28 tFullPathNameA((
10061 63 68 61 72 2a 29 7a 43 6f 6e 76 65 72 74 65 64 char*)zConverted
10062 2c 20 30 2c 20 30 2c 20 30 29 20 2b 20 33 3b 0a , 0, 0, 0) + 3;.
10063 20 20 20 20 7a 54 65 6d 70 20 3d 20 6d 61 6c 6c zTemp = mall
10064 6f 63 28 20 6e 42 79 74 65 2a 73 69 7a 65 6f 66 oc( nByte*sizeof
10065 28 7a 54 65 6d 70 5b 30 5d 29 20 29 3b 0a 20 20 (zTemp[0]) );.
10066 20 20 69 66 28 20 7a 54 65 6d 70 3d 3d 30 20 29 if( zTemp==0 )
10067 7b 0a 20 20 20 20 20 20 66 72 65 65 28 7a 43 6f {. free(zCo
10068 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 20 20 20 nverted);.
10069 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f return SQLITE_NO
1006a 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 47 MEM;. }. G
1006b 65 74 46 75 6c 6c 50 61 74 68 4e 61 6d 65 41 28 etFullPathNameA(
1006c 28 63 68 61 72 2a 29 7a 43 6f 6e 76 65 72 74 65 (char*)zConverte
1006d 64 2c 20 6e 42 79 74 65 2c 20 7a 54 65 6d 70 2c d, nByte, zTemp,
1006e 20 30 29 3b 0a 20 20 20 20 66 72 65 65 28 7a 43 0);. free(zC
1006f 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 20 7a onverted);. z
10070 4f 75 74 20 3d 20 73 71 6c 69 74 65 33 5f 77 69 Out = sqlite3_wi
10071 6e 33 32 5f 6d 62 63 73 5f 74 6f 5f 75 74 66 38 n32_mbcs_to_utf8
10072 28 7a 54 65 6d 70 29 3b 0a 20 20 20 20 66 72 65 (zTemp);. fre
10073 65 28 7a 54 65 6d 70 29 3b 0a 23 65 6e 64 69 66 e(zTemp);.#endif
10074 0a 20 20 7d 0a 20 20 69 66 28 20 7a 4f 75 74 20 . }. if( zOut
10075 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 ){. sqlite3_s
10076 6e 70 72 69 6e 74 66 28 70 56 66 73 2d 3e 6d 78 nprintf(pVfs->mx
10077 50 61 74 68 6e 61 6d 65 2c 20 7a 46 75 6c 6c 2c Pathname, zFull,
10078 20 22 25 73 22 2c 20 7a 4f 75 74 29 3b 0a 20 20 "%s", zOut);.
10079 20 20 66 72 65 65 28 7a 4f 75 74 29 3b 0a 20 20 free(zOut);.
1007a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
1007b 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 OK;. }else{.
1007c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e return SQLITE_N
1007d 4f 4d 45 4d 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 OMEM;. }.#endif
1007e 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20 74 68 .}../*.** Get th
1007f 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20 6f 66 e sector size of
10080 20 74 68 65 20 64 65 76 69 63 65 20 75 73 65 64 the device used
10081 20 74 6f 20 73 74 6f 72 65 0a 2a 2a 20 66 69 6c to store.** fil
10082 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
10083 20 67 65 74 53 65 63 74 6f 72 53 69 7a 65 28 0a getSectorSize(.
10084 20 20 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 sqlite3_vfs
10085 2a 70 56 66 73 2c 0a 20 20 20 20 63 6f 6e 73 74 *pVfs,. const
10086 20 63 68 61 72 20 2a 7a 52 65 6c 61 74 69 76 65 char *zRelative
10087 20 20 20 20 20 2f 2a 20 55 54 46 2d 38 20 66 69 /* UTF-8 fi
10088 6c 65 20 6e 61 6d 65 20 2a 2f 0a 29 7b 0a 20 20 le name */.){.
10089 44 57 4f 52 44 20 62 79 74 65 73 50 65 72 53 65 DWORD bytesPerSe
1008a 63 74 6f 72 20 3d 20 53 51 4c 49 54 45 5f 44 45 ctor = SQLITE_DE
1008b 46 41 55 4c 54 5f 53 45 43 54 4f 52 5f 53 49 5a FAULT_SECTOR_SIZ
1008c 45 3b 0a 20 20 2f 2a 20 47 65 74 44 69 73 6b 46 E;. /* GetDiskF
1008d 72 65 65 53 70 61 63 65 20 69 73 20 6e 6f 74 20 reeSpace is not
1008e 73 75 70 70 6f 72 74 65 64 20 75 6e 64 65 72 20 supported under
1008f 57 49 4e 43 45 20 2a 2f 0a 23 69 66 20 53 51 4c WINCE */.#if SQL
10090 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 55 ITE_OS_WINCE. U
10091 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
10092 70 56 66 73 29 3b 0a 20 20 55 4e 55 53 45 44 5f pVfs);. UNUSED_
10093 50 41 52 41 4d 45 54 45 52 28 7a 52 65 6c 61 74 PARAMETER(zRelat
10094 69 76 65 29 3b 0a 23 65 6c 73 65 0a 20 20 63 68 ive);.#else. ch
10095 61 72 20 7a 46 75 6c 6c 70 61 74 68 5b 4d 41 58 ar zFullpath[MAX
10096 5f 50 41 54 48 2b 31 5d 3b 0a 20 20 69 6e 74 20 _PATH+1];. int
10097 72 63 3b 0a 20 20 44 57 4f 52 44 20 64 77 52 65 rc;. DWORD dwRe
10098 74 20 3d 20 30 3b 0a 20 20 44 57 4f 52 44 20 64 t = 0;. DWORD d
10099 77 44 75 6d 6d 79 3b 0a 0a 20 20 2f 2a 0a 20 20 wDummy;.. /*.
1009a 2a 2a 20 57 65 20 6e 65 65 64 20 74 6f 20 67 65 ** We need to ge
1009b 74 20 74 68 65 20 66 75 6c 6c 20 70 61 74 68 20 t the full path
1009c 6e 61 6d 65 20 6f 66 20 74 68 65 20 66 69 6c 65 name of the file
1009d 0a 20 20 2a 2a 20 74 6f 20 67 65 74 20 74 68 65 . ** to get the
1009e 20 64 72 69 76 65 20 6c 65 74 74 65 72 20 74 6f drive letter to
1009f 20 6c 6f 6f 6b 20 75 70 20 74 68 65 20 73 65 63 look up the sec
100a0 74 6f 72 0a 20 20 2a 2a 20 73 69 7a 65 2e 0a 20 tor. ** size..
100a1 20 2a 2f 0a 20 20 72 63 20 3d 20 77 69 6e 46 75 */. rc = winFu
100a2 6c 6c 50 61 74 68 6e 61 6d 65 28 70 56 66 73 2c llPathname(pVfs,
100a3 20 7a 52 65 6c 61 74 69 76 65 2c 20 4d 41 58 5f zRelative, MAX_
100a4 50 41 54 48 2c 20 7a 46 75 6c 6c 70 61 74 68 29 PATH, zFullpath)
100a5 3b 0a 20 20 69 66 28 20 72 63 20 3d 3d 20 53 51 ;. if( rc == SQ
100a6 4c 49 54 45 5f 4f 4b 20 29 0a 20 20 7b 0a 20 20 LITE_OK ). {.
100a7 20 20 76 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 74 void *zConvert
100a8 65 64 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 ed = convertUtf8
100a9 46 69 6c 65 6e 61 6d 65 28 7a 46 75 6c 6c 70 61 Filename(zFullpa
100aa 74 68 29 3b 0a 20 20 20 20 69 66 28 20 7a 43 6f th);. if( zCo
100ab 6e 76 65 72 74 65 64 20 29 7b 0a 20 20 20 20 20 nverted ){.
100ac 20 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 if( isNT() ){.
100ad 20 20 20 20 20 20 20 2f 2a 20 74 72 69 6d 20 70 /* trim p
100ae 61 74 68 20 74 6f 20 6a 75 73 74 20 64 72 69 76 ath to just driv
100af 65 20 72 65 66 65 72 65 6e 63 65 20 2a 2f 0a 20 e reference */.
100b0 20 20 20 20 20 20 20 57 43 48 41 52 20 2a 70 20 WCHAR *p
100b1 3d 20 7a 43 6f 6e 76 65 72 74 65 64 3b 0a 20 20 = zConverted;.
100b2 20 20 20 20 20 20 66 6f 72 28 3b 2a 70 3b 70 2b for(;*p;p+
100b3 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 +){. if
100b4 28 20 2a 70 20 3d 3d 20 27 5c 5c 27 20 29 7b 0a ( *p == '\\' ){.
100b5 20 20 20 20 20 20 20 20 20 20 20 20 2a 70 20 3d *p =
100b6 20 27 5c 30 27 3b 0a 20 20 20 20 20 20 20 20 20 '\0';.
100b7 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
100b8 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a }. }.
100b9 20 20 20 20 20 20 20 20 64 77 52 65 74 20 3d 20 dwRet =
100ba 47 65 74 44 69 73 6b 46 72 65 65 53 70 61 63 65 GetDiskFreeSpace
100bb 57 28 28 57 43 48 41 52 2a 29 7a 43 6f 6e 76 65 W((WCHAR*)zConve
100bc 72 74 65 64 2c 0a 20 20 20 20 20 20 20 20 20 20 rted,.
100bd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
100be 20 20 20 20 20 20 20 20 26 64 77 44 75 6d 6d 79 &dwDummy
100bf 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
100c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
100c1 20 20 20 20 26 62 79 74 65 73 50 65 72 53 65 63 &bytesPerSec
100c2 74 6f 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 tor,.
100c3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
100c4 20 20 20 20 20 20 20 26 64 77 44 75 6d 6d 79 2c &dwDummy,
100c5 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
100c6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
100c7 20 20 20 26 64 77 44 75 6d 6d 79 29 3b 0a 20 20 &dwDummy);.
100c8 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
100c9 20 20 20 2f 2a 20 74 72 69 6d 20 70 61 74 68 20 /* trim path
100ca 74 6f 20 6a 75 73 74 20 64 72 69 76 65 20 72 65 to just drive re
100cb 66 65 72 65 6e 63 65 20 2a 2f 0a 20 20 20 20 20 ference */.
100cc 20 20 20 43 48 41 52 20 2a 70 20 3d 20 28 43 48 CHAR *p = (CH
100cd 41 52 20 2a 29 7a 43 6f 6e 76 65 72 74 65 64 3b AR *)zConverted;
100ce 0a 20 20 20 20 20 20 20 20 66 6f 72 28 3b 2a 70 . for(;*p
100cf 3b 70 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 ;p++){.
100d0 20 69 66 28 20 2a 70 20 3d 3d 20 27 5c 5c 27 20 if( *p == '\\'
100d1 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a ){. *
100d2 70 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20 20 20 p = '\0';.
100d3 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
100d4 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
100d5 20 7d 0a 20 20 20 20 20 20 20 20 64 77 52 65 74 }. dwRet
100d6 20 3d 20 47 65 74 44 69 73 6b 46 72 65 65 53 70 = GetDiskFreeSp
100d7 61 63 65 41 28 28 43 48 41 52 2a 29 7a 43 6f 6e aceA((CHAR*)zCon
100d8 76 65 72 74 65 64 2c 0a 20 20 20 20 20 20 20 20 verted,.
100d9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
100da 20 20 20 20 20 20 20 20 20 20 26 64 77 44 75 6d &dwDum
100db 6d 79 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 my,.
100dc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
100dd 20 20 20 20 20 20 26 62 79 74 65 73 50 65 72 53 &bytesPerS
100de 65 63 74 6f 72 2c 0a 20 20 20 20 20 20 20 20 20 ector,.
100df 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
100e0 20 20 20 20 20 20 20 20 20 26 64 77 44 75 6d 6d &dwDumm
100e1 79 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 y,.
100e2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
100e3 20 20 20 20 20 26 64 77 44 75 6d 6d 79 29 3b 0a &dwDummy);.
100e4 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 66 72 }. fr
100e5 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a ee(zConverted);.
100e6 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21 64 }. if( !d
100e7 77 52 65 74 20 29 7b 0a 20 20 20 20 20 20 62 79 wRet ){. by
100e8 74 65 73 50 65 72 53 65 63 74 6f 72 20 3d 20 53 tesPerSector = S
100e9 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 53 45 QLITE_DEFAULT_SE
100ea 43 54 4f 52 5f 53 49 5a 45 3b 0a 20 20 20 20 7d CTOR_SIZE;. }
100eb 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65 . }.#endif. re
100ec 74 75 72 6e 20 28 69 6e 74 29 20 62 79 74 65 73 turn (int) bytes
100ed 50 65 72 53 65 63 74 6f 72 3b 20 0a 7d 0a 0a 23 PerSector; .}..#
100ee 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
100ef 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f IT_LOAD_EXTENSIO
100f0 4e 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 66 61 63 N./*.** Interfac
100f1 65 73 20 66 6f 72 20 6f 70 65 6e 69 6e 67 20 61 es for opening a
100f2 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 2c shared library,
100f3 20 66 69 6e 64 69 6e 67 20 65 6e 74 72 79 20 70 finding entry p
100f4 6f 69 6e 74 73 0a 2a 2a 20 77 69 74 68 69 6e 20 oints.** within
100f5 74 68 65 20 73 68 61 72 65 64 20 6c 69 62 72 61 the shared libra
100f6 72 79 2c 20 61 6e 64 20 63 6c 6f 73 69 6e 67 20 ry, and closing
100f7 74 68 65 20 73 68 61 72 65 64 20 6c 69 62 72 61 the shared libra
100f8 72 79 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a 20 49 6e 74 ry..*/./*.** Int
100f9 65 72 66 61 63 65 73 20 66 6f 72 20 6f 70 65 6e erfaces for open
100fa 69 6e 67 20 61 20 73 68 61 72 65 64 20 6c 69 62 ing a shared lib
100fb 72 61 72 79 2c 20 66 69 6e 64 69 6e 67 20 65 6e rary, finding en
100fc 74 72 79 20 70 6f 69 6e 74 73 0a 2a 2a 20 77 69 try points.** wi
100fd 74 68 69 6e 20 74 68 65 20 73 68 61 72 65 64 20 thin the shared
100fe 6c 69 62 72 61 72 79 2c 20 61 6e 64 20 63 6c 6f library, and clo
100ff 73 69 6e 67 20 74 68 65 20 73 68 61 72 65 64 20 sing the shared
10100 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 73 74 61 74 library..*/.stat
10101 69 63 20 76 6f 69 64 20 2a 77 69 6e 44 6c 4f 70 ic void *winDlOp
10102 65 6e 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a en(sqlite3_vfs *
10103 70 56 66 73 2c 20 63 6f 6e 73 74 20 63 68 61 72 pVfs, const char
10104 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 *zFilename){.
10105 48 41 4e 44 4c 45 20 68 3b 0a 20 20 76 6f 69 64 HANDLE h;. void
10106 20 2a 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 63 *zConverted = c
10107 6f 6e 76 65 72 74 55 74 66 38 46 69 6c 65 6e 61 onvertUtf8Filena
10108 6d 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 me(zFilename);.
10109 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
1010a 52 28 70 56 66 73 29 3b 0a 20 20 69 66 28 20 7a R(pVfs);. if( z
1010b 43 6f 6e 76 65 72 74 65 64 3d 3d 30 20 29 7b 0a Converted==0 ){.
1010c 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 return 0;.
1010d 7d 0a 20 20 69 66 28 20 69 73 4e 54 28 29 20 29 }. if( isNT() )
1010e 7b 0a 20 20 20 20 68 20 3d 20 4c 6f 61 64 4c 69 {. h = LoadLi
1010f 62 72 61 72 79 57 28 28 57 43 48 41 52 2a 29 7a braryW((WCHAR*)z
10110 43 6f 6e 76 65 72 74 65 64 29 3b 0a 2f 2a 20 69 Converted);./* i
10111 73 4e 54 28 29 20 69 73 20 31 20 69 66 20 53 51 sNT() is 1 if SQ
10112 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 LITE_OS_WINCE==1
10113 2c 20 73 6f 20 74 68 69 73 20 65 6c 73 65 20 69 , so this else i
10114 73 20 6e 65 76 65 72 20 65 78 65 63 75 74 65 64 s never executed
10115 2e 20 0a 2a 2a 20 53 69 6e 63 65 20 74 68 65 20 . .** Since the
10116 41 53 43 49 49 20 76 65 72 73 69 6f 6e 20 6f 66 ASCII version of
10117 20 74 68 65 73 65 20 57 69 6e 64 6f 77 73 20 41 these Windows A
10118 50 49 20 64 6f 20 6e 6f 74 20 65 78 69 73 74 20 PI do not exist
10119 66 6f 72 20 57 49 4e 43 45 2c 0a 2a 2a 20 69 74 for WINCE,.** it
1011a 27 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 's important to
1011b 6e 6f 74 20 72 65 66 65 72 65 6e 63 65 20 74 68 not reference th
1011c 65 6d 20 66 6f 72 20 57 49 4e 43 45 20 62 75 69 em for WINCE bui
1011d 6c 64 73 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 lds..*/.#if SQLI
1011e 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 TE_OS_WINCE==0.
1011f 20 7d 65 6c 73 65 7b 0a 20 20 20 20 68 20 3d 20 }else{. h =
10120 4c 6f 61 64 4c 69 62 72 61 72 79 41 28 28 63 68 LoadLibraryA((ch
10121 61 72 2a 29 7a 43 6f 6e 76 65 72 74 65 64 29 3b ar*)zConverted);
10122 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 66 72 .#endif. }. fr
10123 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a ee(zConverted);.
10124 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 2a 29 return (void*)
10125 68 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 h;.}.static void
10126 20 77 69 6e 44 6c 45 72 72 6f 72 28 73 71 6c 69 winDlError(sqli
10127 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 te3_vfs *pVfs, i
10128 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a nt nBuf, char *z
10129 42 75 66 4f 75 74 29 7b 0a 20 20 55 4e 55 53 45 BufOut){. UNUSE
1012a 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66 73 D_PARAMETER(pVfs
1012b 29 3b 0a 20 20 67 65 74 4c 61 73 74 45 72 72 6f );. getLastErro
1012c 72 4d 73 67 28 6e 42 75 66 2c 20 7a 42 75 66 4f rMsg(nBuf, zBufO
1012d 75 74 29 3b 0a 7d 0a 76 6f 69 64 20 28 2a 77 69 ut);.}.void (*wi
1012e 6e 44 6c 53 79 6d 28 73 71 6c 69 74 65 33 5f 76 nDlSym(sqlite3_v
1012f 66 73 20 2a 70 56 66 73 2c 20 76 6f 69 64 20 2a fs *pVfs, void *
10130 70 48 61 6e 64 6c 65 2c 20 63 6f 6e 73 74 20 63 pHandle, const c
10131 68 61 72 20 2a 7a 53 79 6d 62 6f 6c 29 29 28 76 har *zSymbol))(v
10132 6f 69 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 oid){. UNUSED_P
10133 41 52 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0a ARAMETER(pVfs);.
10134 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 #if SQLITE_OS_WI
10135 4e 43 45 0a 20 20 2f 2a 20 54 68 65 20 47 65 74 NCE. /* The Get
10136 50 72 6f 63 41 64 64 72 65 73 73 41 28 29 20 72 ProcAddressA() r
10137 6f 75 74 69 6e 65 20 69 73 20 6f 6e 6c 79 20 61 outine is only a
10138 76 61 69 6c 61 62 6c 65 20 6f 6e 20 77 69 6e 63 vailable on winc
10139 65 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 28 e. */. return (
1013a 76 6f 69 64 28 2a 29 28 76 6f 69 64 29 29 47 65 void(*)(void))Ge
1013b 74 50 72 6f 63 41 64 64 72 65 73 73 41 28 28 48 tProcAddressA((H
1013c 41 4e 44 4c 45 29 70 48 61 6e 64 6c 65 2c 20 7a ANDLE)pHandle, z
1013d 53 79 6d 62 6f 6c 29 3b 0a 23 65 6c 73 65 0a 20 Symbol);.#else.
1013e 20 2f 2a 20 41 6c 6c 20 6f 74 68 65 72 20 77 69 /* All other wi
1013f 6e 64 6f 77 73 20 70 6c 61 74 66 6f 72 6d 73 20 ndows platforms
10140 65 78 70 65 63 74 20 47 65 74 50 72 6f 63 41 64 expect GetProcAd
10141 64 72 65 73 73 28 29 20 74 6f 20 74 61 6b 65 0a dress() to take.
10142 20 20 2a 2a 20 61 6e 20 41 6e 73 69 20 73 74 72 ** an Ansi str
10143 69 6e 67 20 72 65 67 61 72 64 6c 65 73 73 20 6f ing regardless o
10144 66 20 74 68 65 20 5f 55 4e 49 43 4f 44 45 20 73 f the _UNICODE s
10145 65 74 74 69 6e 67 20 2a 2f 0a 20 20 72 65 74 75 etting */. retu
10146 72 6e 20 28 76 6f 69 64 28 2a 29 28 76 6f 69 64 rn (void(*)(void
10147 29 29 47 65 74 50 72 6f 63 41 64 64 72 65 73 73 ))GetProcAddress
10148 28 28 48 41 4e 44 4c 45 29 70 48 61 6e 64 6c 65 ((HANDLE)pHandle
10149 2c 20 7a 53 79 6d 62 6f 6c 29 3b 0a 23 65 6e 64 , zSymbol);.#end
1014a 69 66 0a 7d 0a 76 6f 69 64 20 77 69 6e 44 6c 43 if.}.void winDlC
1014b 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 76 66 73 lose(sqlite3_vfs
1014c 20 2a 70 56 66 73 2c 20 76 6f 69 64 20 2a 70 48 *pVfs, void *pH
1014d 61 6e 64 6c 65 29 7b 0a 20 20 55 4e 55 53 45 44 andle){. UNUSED
1014e 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66 73 29 _PARAMETER(pVfs)
1014f 3b 0a 20 20 46 72 65 65 4c 69 62 72 61 72 79 28 ;. FreeLibrary(
10150 28 48 41 4e 44 4c 45 29 70 48 61 6e 64 6c 65 29 (HANDLE)pHandle)
10151 3b 0a 7d 0a 23 65 6c 73 65 20 2f 2a 20 69 66 20 ;.}.#else /* if
10152 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 SQLITE_OMIT_LOAD
10153 5f 45 58 54 45 4e 53 49 4f 4e 20 69 73 20 64 65 _EXTENSION is de
10154 66 69 6e 65 64 3a 20 2a 2f 0a 20 20 23 64 65 66 fined: */. #def
10155 69 6e 65 20 77 69 6e 44 6c 4f 70 65 6e 20 20 30 ine winDlOpen 0
10156 0a 20 20 23 64 65 66 69 6e 65 20 77 69 6e 44 6c . #define winDl
10157 45 72 72 6f 72 20 30 0a 20 20 23 64 65 66 69 6e Error 0. #defin
10158 65 20 77 69 6e 44 6c 53 79 6d 20 20 20 30 0a 20 e winDlSym 0.
10159 20 23 64 65 66 69 6e 65 20 77 69 6e 44 6c 43 6c #define winDlCl
1015a 6f 73 65 20 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f ose 0.#endif.../
1015b 2a 0a 2a 2a 20 57 72 69 74 65 20 75 70 20 74 6f *.** Write up to
1015c 20 6e 42 75 66 20 62 79 74 65 73 20 6f 66 20 72 nBuf bytes of r
1015d 61 6e 64 6f 6d 6e 65 73 73 20 69 6e 74 6f 20 7a andomness into z
1015e 42 75 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 Buf..*/.static i
1015f 6e 74 20 77 69 6e 52 61 6e 64 6f 6d 6e 65 73 73 nt winRandomness
10160 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 (sqlite3_vfs *pV
10161 66 73 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63 68 fs, int nBuf, ch
10162 61 72 20 2a 7a 42 75 66 29 7b 0a 20 20 69 6e 74 ar *zBuf){. int
10163 20 6e 20 3d 20 30 3b 0a 20 20 55 4e 55 53 45 44 n = 0;. UNUSED
10164 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66 73 29 _PARAMETER(pVfs)
10165 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 ;.#if defined(SQ
10166 4c 49 54 45 5f 54 45 53 54 29 0a 20 20 6e 20 3d LITE_TEST). n =
10167 20 6e 42 75 66 3b 0a 20 20 6d 65 6d 73 65 74 28 nBuf;. memset(
10168 7a 42 75 66 2c 20 30 2c 20 6e 42 75 66 29 3b 0a zBuf, 0, nBuf);.
10169 23 65 6c 73 65 0a 20 20 69 66 28 20 73 69 7a 65 #else. if( size
1016a 6f 66 28 53 59 53 54 45 4d 54 49 4d 45 29 3c 3d of(SYSTEMTIME)<=
1016b 6e 42 75 66 2d 6e 20 29 7b 0a 20 20 20 20 53 59 nBuf-n ){. SY
1016c 53 54 45 4d 54 49 4d 45 20 78 3b 0a 20 20 20 20 STEMTIME x;.
1016d 47 65 74 53 79 73 74 65 6d 54 69 6d 65 28 26 78 GetSystemTime(&x
1016e 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 7a );. memcpy(&z
1016f 42 75 66 5b 6e 5d 2c 20 26 78 2c 20 73 69 7a 65 Buf[n], &x, size
10170 6f 66 28 78 29 29 3b 0a 20 20 20 20 6e 20 2b 3d of(x));. n +=
10171 20 73 69 7a 65 6f 66 28 78 29 3b 0a 20 20 7d 0a sizeof(x);. }.
10172 20 20 69 66 28 20 73 69 7a 65 6f 66 28 44 57 4f if( sizeof(DWO
10173 52 44 29 3c 3d 6e 42 75 66 2d 6e 20 29 7b 0a 20 RD)<=nBuf-n ){.
10174 20 20 20 44 57 4f 52 44 20 70 69 64 20 3d 20 47 DWORD pid = G
10175 65 74 43 75 72 72 65 6e 74 50 72 6f 63 65 73 73 etCurrentProcess
10176 49 64 28 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 Id();. memcpy
10177 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 70 69 64 2c (&zBuf[n], &pid,
10178 20 73 69 7a 65 6f 66 28 70 69 64 29 29 3b 0a 20 sizeof(pid));.
10179 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 28 70 n += sizeof(p
1017a 69 64 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 id);. }. if( s
1017b 69 7a 65 6f 66 28 44 57 4f 52 44 29 3c 3d 6e 42 izeof(DWORD)<=nB
1017c 75 66 2d 6e 20 29 7b 0a 20 20 20 20 44 57 4f 52 uf-n ){. DWOR
1017d 44 20 63 6e 74 20 3d 20 47 65 74 54 69 63 6b 43 D cnt = GetTickC
1017e 6f 75 6e 74 28 29 3b 0a 20 20 20 20 6d 65 6d 63 ount();. memc
1017f 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 63 6e py(&zBuf[n], &cn
10180 74 2c 20 73 69 7a 65 6f 66 28 63 6e 74 29 29 3b t, sizeof(cnt));
10181 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 . n += sizeof
10182 28 63 6e 74 29 3b 0a 20 20 7d 0a 20 20 69 66 28 (cnt);. }. if(
10183 20 73 69 7a 65 6f 66 28 4c 41 52 47 45 5f 49 4e sizeof(LARGE_IN
10184 54 45 47 45 52 29 3c 3d 6e 42 75 66 2d 6e 20 29 TEGER)<=nBuf-n )
10185 7b 0a 20 20 20 20 4c 41 52 47 45 5f 49 4e 54 45 {. LARGE_INTE
10186 47 45 52 20 69 3b 0a 20 20 20 20 51 75 65 72 79 GER i;. Query
10187 50 65 72 66 6f 72 6d 61 6e 63 65 43 6f 75 6e 74 PerformanceCount
10188 65 72 28 26 69 29 3b 0a 20 20 20 20 6d 65 6d 63 er(&i);. memc
10189 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 69 2c py(&zBuf[n], &i,
1018a 20 73 69 7a 65 6f 66 28 69 29 29 3b 0a 20 20 20 sizeof(i));.
1018b 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 28 69 29 3b n += sizeof(i);
1018c 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65 . }.#endif. re
1018d 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a turn n;.}.../*.*
1018e 2a 20 53 6c 65 65 70 20 66 6f 72 20 61 20 6c 69 * Sleep for a li
1018f 74 74 6c 65 20 77 68 69 6c 65 2e 20 20 52 65 74 ttle while. Ret
10190 75 72 6e 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f urn the amount o
10191 66 20 74 69 6d 65 20 73 6c 65 70 74 2e 0a 2a 2f f time slept..*/
10192 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 53 .static int winS
10193 6c 65 65 70 28 73 71 6c 69 74 65 33 5f 76 66 73 leep(sqlite3_vfs
10194 20 2a 70 56 66 73 2c 20 69 6e 74 20 6d 69 63 72 *pVfs, int micr
10195 6f 73 65 63 29 7b 0a 20 20 53 6c 65 65 70 28 28 osec){. Sleep((
10196 6d 69 63 72 6f 73 65 63 2b 39 39 39 29 2f 31 30 microsec+999)/10
10197 30 30 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 00);. UNUSED_PA
10198 52 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 20 RAMETER(pVfs);.
10199 20 72 65 74 75 72 6e 20 28 28 6d 69 63 72 6f 73 return ((micros
1019a 65 63 2b 39 39 39 29 2f 31 30 30 30 29 2a 31 30 ec+999)/1000)*10
1019b 30 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 00;.}../*.** The
1019c 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 72 69 61 following varia
1019d 62 6c 65 2c 20 69 66 20 73 65 74 20 74 6f 20 61 ble, if set to a
1019e 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 2c non-zero value,
1019f 20 62 65 63 6f 6d 65 73 20 74 68 65 20 72 65 73 becomes the res
101a0 75 6c 74 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 ult.** returned
101a1 66 72 6f 6d 20 73 71 6c 69 74 65 33 4f 73 43 75 from sqlite3OsCu
101a2 72 72 65 6e 74 54 69 6d 65 28 29 2e 20 20 54 68 rrentTime(). Th
101a3 69 73 20 69 73 20 75 73 65 64 20 66 6f 72 20 74 is is used for t
101a4 65 73 74 69 6e 67 2e 0a 2a 2f 0a 23 69 66 64 65 esting..*/.#ifde
101a5 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 f SQLITE_TEST.SQ
101a6 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
101a7 69 74 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d ite3_current_tim
101a8 65 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f e = 0;.#endif../
101a9 2a 0a 2a 2a 20 46 69 6e 64 20 74 68 65 20 63 75 *.** Find the cu
101aa 72 72 65 6e 74 20 74 69 6d 65 20 28 69 6e 20 55 rrent time (in U
101ab 6e 69 76 65 72 73 61 6c 20 43 6f 6f 72 64 69 6e niversal Coordin
101ac 61 74 65 64 20 54 69 6d 65 29 2e 20 20 57 72 69 ated Time). Wri
101ad 74 65 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e te the.** curren
101ae 74 20 74 69 6d 65 20 61 6e 64 20 64 61 74 65 20 t time and date
101af 61 73 20 61 20 4a 75 6c 69 61 6e 20 44 61 79 20 as a Julian Day
101b0 6e 75 6d 62 65 72 20 69 6e 74 6f 20 2a 70 72 4e number into *prN
101b1 6f 77 20 61 6e 64 0a 2a 2a 20 72 65 74 75 72 6e ow and.** return
101b2 20 30 2e 20 20 52 65 74 75 72 6e 20 31 20 69 66 0. Return 1 if
101b3 20 74 68 65 20 74 69 6d 65 20 61 6e 64 20 64 61 the time and da
101b4 74 65 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f 75 te cannot be fou
101b5 6e 64 2e 0a 2a 2f 0a 69 6e 74 20 77 69 6e 43 75 nd..*/.int winCu
101b6 72 72 65 6e 74 54 69 6d 65 28 73 71 6c 69 74 65 rrentTime(sqlite
101b7 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 64 6f 75 3_vfs *pVfs, dou
101b8 62 6c 65 20 2a 70 72 4e 6f 77 29 7b 0a 20 20 46 ble *prNow){. F
101b9 49 4c 45 54 49 4d 45 20 66 74 3b 0a 20 20 2f 2a ILETIME ft;. /*
101ba 20 46 49 4c 45 54 49 4d 45 20 73 74 72 75 63 74 FILETIME struct
101bb 75 72 65 20 69 73 20 61 20 36 34 2d 62 69 74 20 ure is a 64-bit
101bc 76 61 6c 75 65 20 72 65 70 72 65 73 65 6e 74 69 value representi
101bd 6e 67 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 ng the number of
101be 20 0a 20 20 20 20 20 31 30 30 2d 6e 61 6e 6f 73 . 100-nanos
101bf 65 63 6f 6e 64 20 69 6e 74 65 72 76 61 6c 73 20 econd intervals
101c0 73 69 6e 63 65 20 4a 61 6e 75 61 72 79 20 31 2c since January 1,
101c1 20 31 36 30 31 20 28 3d 20 4a 44 20 32 33 30 35 1601 (= JD 2305
101c2 38 31 33 2e 35 29 2e 20 0a 20 20 2a 2f 0a 20 20 813.5). . */.
101c3 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 74 69 sqlite3_int64 ti
101c4 6d 65 57 3b 20 20 20 2f 2a 20 57 68 6f 6c 65 20 meW; /* Whole
101c5 64 61 79 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 days */. sqlite
101c6 33 5f 69 6e 74 36 34 20 74 69 6d 65 46 3b 20 20 3_int64 timeF;
101c7 20 2f 2a 20 46 72 61 63 74 69 6f 6e 61 6c 20 44 /* Fractional D
101c8 61 79 73 20 2a 2f 0a 0a 20 20 2f 2a 20 4e 75 6d ays */.. /* Num
101c9 62 65 72 20 6f 66 20 31 30 30 2d 6e 61 6e 6f 73 ber of 100-nanos
101ca 65 63 6f 6e 64 20 69 6e 74 65 72 76 61 6c 73 20 econd intervals
101cb 69 6e 20 61 20 73 69 6e 67 6c 65 20 64 61 79 20 in a single day
101cc 2a 2f 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 */. static cons
101cd 74 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 t sqlite3_int64
101ce 6e 74 75 50 65 72 44 61 79 20 3d 20 0a 20 20 20 ntuPerDay = .
101cf 20 20 20 31 30 30 30 30 30 30 30 2a 28 73 71 6c 10000000*(sql
101d0 69 74 65 33 5f 69 6e 74 36 34 29 38 36 34 30 30 ite3_int64)86400
101d1 3b 0a 0a 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f ;.. /* Number o
101d2 66 20 31 30 30 2d 6e 61 6e 6f 73 65 63 6f 6e 64 f 100-nanosecond
101d3 20 69 6e 74 65 72 76 61 6c 73 20 69 6e 20 68 61 intervals in ha
101d4 6c 66 20 6f 66 20 61 20 64 61 79 20 2a 2f 0a 20 lf of a day */.
101d5 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 static const sq
101d6 6c 69 74 65 33 5f 69 6e 74 36 34 20 6e 74 75 50 lite3_int64 ntuP
101d7 65 72 48 61 6c 66 44 61 79 20 3d 20 0a 20 20 20 erHalfDay = .
101d8 20 20 20 31 30 30 30 30 30 30 30 2a 28 73 71 6c 10000000*(sql
101d9 69 74 65 33 5f 69 6e 74 36 34 29 34 33 32 30 30 ite3_int64)43200
101da 3b 0a 0a 20 20 2f 2a 20 32 5e 33 32 20 2d 20 74 ;.. /* 2^32 - t
101db 6f 20 61 76 6f 69 64 20 75 73 65 20 6f 66 20 4c o avoid use of L
101dc 4c 20 61 6e 64 20 77 61 72 6e 69 6e 67 73 20 69 L and warnings i
101dd 6e 20 67 63 63 20 2a 2f 0a 20 20 73 74 61 74 69 n gcc */. stati
101de 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f c const sqlite3_
101df 69 6e 74 36 34 20 6d 61 78 33 32 42 69 74 56 61 int64 max32BitVa
101e0 6c 75 65 20 3d 20 0a 20 20 20 20 20 20 28 73 71 lue = . (sq
101e1 6c 69 74 65 33 5f 69 6e 74 36 34 29 32 30 30 30 lite3_int64)2000
101e2 30 30 30 30 30 30 20 2b 20 28 73 71 6c 69 74 65 000000 + (sqlite
101e3 33 5f 69 6e 74 36 34 29 32 30 30 30 30 30 30 30 3_int64)20000000
101e4 30 30 20 2b 20 28 73 71 6c 69 74 65 33 5f 69 6e 00 + (sqlite3_in
101e5 74 36 34 29 32 39 34 39 36 37 32 39 36 3b 0a 0a t64)294967296;..
101e6 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 #if SQLITE_OS_WI
101e7 4e 43 45 0a 20 20 53 59 53 54 45 4d 54 49 4d 45 NCE. SYSTEMTIME
101e8 20 74 69 6d 65 3b 0a 20 20 47 65 74 53 79 73 74 time;. GetSyst
101e9 65 6d 54 69 6d 65 28 26 74 69 6d 65 29 3b 0a 20 emTime(&time);.
101ea 20 2f 2a 20 69 66 20 53 79 73 74 65 6d 54 69 6d /* if SystemTim
101eb 65 54 6f 46 69 6c 65 54 69 6d 65 28 29 20 66 61 eToFileTime() fa
101ec 69 6c 73 2c 20 69 74 20 72 65 74 75 72 6e 73 20 ils, it returns
101ed 7a 65 72 6f 2e 20 2a 2f 0a 20 20 69 66 20 28 21 zero. */. if (!
101ee 53 79 73 74 65 6d 54 69 6d 65 54 6f 46 69 6c 65 SystemTimeToFile
101ef 54 69 6d 65 28 26 74 69 6d 65 2c 26 66 74 29 29 Time(&time,&ft))
101f0 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a {. return 1;.
101f1 20 20 7d 0a 23 65 6c 73 65 0a 20 20 47 65 74 53 }.#else. GetS
101f2 79 73 74 65 6d 54 69 6d 65 41 73 46 69 6c 65 54 ystemTimeAsFileT
101f3 69 6d 65 28 20 26 66 74 20 29 3b 0a 23 65 6e 64 ime( &ft );.#end
101f4 69 66 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 if. UNUSED_PARA
101f5 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 20 20 74 METER(pVfs);. t
101f6 69 6d 65 57 20 3d 20 28 28 28 73 71 6c 69 74 65 imeW = (((sqlite
101f7 33 5f 69 6e 74 36 34 29 66 74 2e 64 77 48 69 67 3_int64)ft.dwHig
101f8 68 44 61 74 65 54 69 6d 65 29 2a 6d 61 78 33 32 hDateTime)*max32
101f9 42 69 74 56 61 6c 75 65 29 20 2b 20 28 73 71 6c BitValue) + (sql
101fa 69 74 65 33 5f 69 6e 74 36 34 29 66 74 2e 64 77 ite3_int64)ft.dw
101fb 4c 6f 77 44 61 74 65 54 69 6d 65 3b 0a 20 20 74 LowDateTime;. t
101fc 69 6d 65 46 20 3d 20 74 69 6d 65 57 20 25 20 6e imeF = timeW % n
101fd 74 75 50 65 72 44 61 79 3b 20 20 20 20 20 20 20 tuPerDay;
101fe 20 20 20 2f 2a 20 66 72 61 63 74 69 6f 6e 61 6c /* fractional
101ff 20 64 61 79 73 20 28 31 30 30 2d 6e 61 6e 6f 73 days (100-nanos
10200 65 63 6f 6e 64 73 29 20 2a 2f 0a 20 20 74 69 6d econds) */. tim
10201 65 57 20 3d 20 74 69 6d 65 57 20 2f 20 6e 74 75 eW = timeW / ntu
10202 50 65 72 44 61 79 3b 20 20 20 20 20 20 20 20 20 PerDay;
10203 20 2f 2a 20 77 68 6f 6c 65 20 64 61 79 73 20 2a /* whole days *
10204 2f 0a 20 20 74 69 6d 65 57 20 3d 20 74 69 6d 65 /. timeW = time
10205 57 20 2b 20 32 33 30 35 38 31 33 3b 20 20 20 20 W + 2305813;
10206 20 20 20 20 20 20 20 20 2f 2a 20 61 64 64 20 77 /* add w
10207 68 6f 6c 65 20 64 61 79 73 20 28 66 72 6f 6d 20 hole days (from
10208 32 33 30 35 38 31 33 2e 35 29 20 2a 2f 0a 20 20 2305813.5) */.
10209 74 69 6d 65 46 20 3d 20 74 69 6d 65 46 20 2b 20 timeF = timeF +
1020a 6e 74 75 50 65 72 48 61 6c 66 44 61 79 3b 20 20 ntuPerHalfDay;
1020b 20 20 20 20 2f 2a 20 61 64 64 20 68 61 6c 66 20 /* add half
1020c 61 20 64 61 79 20 28 66 72 6f 6d 20 32 33 30 35 a day (from 2305
1020d 38 31 33 2e 35 29 20 2a 2f 0a 20 20 74 69 6d 65 813.5) */. time
1020e 57 20 3d 20 74 69 6d 65 57 20 2b 20 28 74 69 6d W = timeW + (tim
1020f 65 46 2f 6e 74 75 50 65 72 44 61 79 29 3b 20 20 eF/ntuPerDay);
10210 2f 2a 20 61 64 64 20 77 68 6f 6c 65 20 64 61 79 /* add whole day
10211 20 69 66 20 68 61 6c 66 20 64 61 79 20 6d 61 64 if half day mad
10212 65 20 6f 6e 65 20 2a 2f 0a 20 20 74 69 6d 65 46 e one */. timeF
10213 20 3d 20 74 69 6d 65 46 20 25 20 6e 74 75 50 65 = timeF % ntuPe
10214 72 44 61 79 3b 20 20 20 20 20 20 20 20 20 20 2f rDay; /
10215 2a 20 63 6f 6d 70 75 74 65 20 6e 65 77 20 66 72 * compute new fr
10216 61 63 74 69 6f 6e 61 6c 20 64 61 79 73 20 2a 2f actional days */
10217 0a 20 20 2a 70 72 4e 6f 77 20 3d 20 28 64 6f 75 . *prNow = (dou
10218 62 6c 65 29 74 69 6d 65 57 20 2b 20 28 28 64 6f ble)timeW + ((do
10219 75 62 6c 65 29 74 69 6d 65 46 20 2f 20 28 64 6f uble)timeF / (do
1021a 75 62 6c 65 29 6e 74 75 50 65 72 44 61 79 29 3b uble)ntuPerDay);
1021b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 .#ifdef SQLITE_T
1021c 45 53 54 0a 20 20 69 66 28 20 73 71 6c 69 74 65 EST. if( sqlite
1021d 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 20 29 3_current_time )
1021e 7b 0a 20 20 20 20 2a 70 72 4e 6f 77 20 3d 20 28 {. *prNow = (
1021f 28 64 6f 75 62 6c 65 29 73 71 6c 69 74 65 33 5f (double)sqlite3_
10220 63 75 72 72 65 6e 74 5f 74 69 6d 65 20 2b 20 28 current_time + (
10221 64 6f 75 62 6c 65 29 34 33 32 30 30 29 20 2f 20 double)43200) /
10222 28 64 6f 75 62 6c 65 29 38 36 34 30 30 20 2b 20 (double)86400 +
10223 28 64 6f 75 62 6c 65 29 32 34 34 30 35 38 37 3b (double)2440587;
10224 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65 . }.#endif. re
10225 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a turn 0;.}../*.**
10226 20 54 68 65 20 69 64 65 61 20 69 73 20 74 68 61 The idea is tha
10227 74 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 t this function
10228 77 6f 72 6b 73 20 6c 69 6b 65 20 61 20 63 6f 6d works like a com
10229 62 69 6e 61 74 69 6f 6e 20 6f 66 0a 2a 2a 20 47 bination of.** G
1022a 65 74 4c 61 73 74 45 72 72 6f 72 28 29 20 61 6e etLastError() an
1022b 64 20 46 6f 72 6d 61 74 4d 65 73 73 61 67 65 28 d FormatMessage(
1022c 29 20 6f 6e 20 77 69 6e 64 6f 77 73 20 28 6f 72 ) on windows (or
1022d 20 65 72 72 6e 6f 20 61 6e 64 0a 2a 2a 20 73 74 errno and.** st
1022e 72 65 72 72 6f 72 5f 72 28 29 20 6f 6e 20 75 6e rerror_r() on un
1022f 69 78 29 2e 20 41 66 74 65 72 20 61 6e 20 65 72 ix). After an er
10230 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 20 ror is returned
10231 62 79 20 61 6e 20 4f 53 0a 2a 2a 20 66 75 6e 63 by an OS.** func
10232 74 69 6f 6e 2c 20 53 51 4c 69 74 65 20 63 61 6c tion, SQLite cal
10233 6c 73 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e ls this function
10234 20 77 69 74 68 20 7a 42 75 66 20 70 6f 69 6e 74 with zBuf point
10235 69 6e 67 20 74 6f 0a 2a 2a 20 61 20 62 75 66 66 ing to.** a buff
10236 65 72 20 6f 66 20 6e 42 75 66 20 62 79 74 65 73 er of nBuf bytes
10237 2e 20 54 68 65 20 4f 53 20 6c 61 79 65 72 20 73 . The OS layer s
10238 68 6f 75 6c 64 20 70 6f 70 75 6c 61 74 65 20 74 hould populate t
10239 68 65 0a 2a 2a 20 62 75 66 66 65 72 20 77 69 74 he.** buffer wit
1023a 68 20 61 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 h a nul-terminat
1023b 65 64 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 ed UTF-8 encoded
1023c 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 0a 2a error message.*
1023d 2a 20 64 65 73 63 72 69 62 69 6e 67 20 74 68 65 * describing the
1023e 20 6c 61 73 74 20 49 4f 20 65 72 72 6f 72 20 74 last IO error t
1023f 6f 20 68 61 76 65 20 6f 63 63 75 72 72 65 64 20 o have occurred
10240 77 69 74 68 69 6e 20 74 68 65 20 63 61 6c 6c 69 within the calli
10241 6e 67 0a 2a 2a 20 74 68 72 65 61 64 2e 0a 2a 2a ng.** thread..**
10242 0a 2a 2a 20 49 66 20 74 68 65 20 65 72 72 6f 72 .** If the error
10243 20 6d 65 73 73 61 67 65 20 69 73 20 74 6f 6f 20 message is too
10244 6c 61 72 67 65 20 66 6f 72 20 74 68 65 20 73 75 large for the su
10245 70 70 6c 69 65 64 20 62 75 66 66 65 72 2c 0a 2a pplied buffer,.*
10246 2a 20 69 74 20 73 68 6f 75 6c 64 20 62 65 20 74 * it should be t
10247 72 75 6e 63 61 74 65 64 2e 20 54 68 65 20 72 65 runcated. The re
10248 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 78 47 turn value of xG
10249 65 74 4c 61 73 74 45 72 72 6f 72 0a 2a 2a 20 69 etLastError.** i
1024a 73 20 7a 65 72 6f 20 69 66 20 74 68 65 20 65 72 s zero if the er
1024b 72 6f 72 20 6d 65 73 73 61 67 65 20 66 69 74 73 ror message fits
1024c 20 69 6e 20 74 68 65 20 62 75 66 66 65 72 2c 20 in the buffer,
1024d 6f 72 20 6e 6f 6e 2d 7a 65 72 6f 0a 2a 2a 20 6f or non-zero.** o
1024e 74 68 65 72 77 69 73 65 20 28 69 66 20 74 68 65 therwise (if the
1024f 20 6d 65 73 73 61 67 65 20 77 61 73 20 74 72 75 message was tru
10250 6e 63 61 74 65 64 29 2e 20 49 66 20 6e 6f 6e 2d ncated). If non-
10251 7a 65 72 6f 20 69 73 20 72 65 74 75 72 6e 65 64 zero is returned
10252 2c 0a 2a 2a 20 74 68 65 6e 20 69 74 20 69 73 20 ,.** then it is
10253 6e 6f 74 20 6e 65 63 65 73 73 61 72 79 20 74 6f not necessary to
10254 20 69 6e 63 6c 75 64 65 20 74 68 65 20 6e 75 6c include the nul
10255 2d 74 65 72 6d 69 6e 61 74 6f 72 20 63 68 61 72 -terminator char
10256 61 63 74 65 72 0a 2a 2a 20 69 6e 20 74 68 65 20 acter.** in the
10257 6f 75 74 70 75 74 20 62 75 66 66 65 72 2e 0a 2a output buffer..*
10258 2a 0a 2a 2a 20 4e 6f 74 20 73 75 70 70 6c 79 69 *.** Not supplyi
10259 6e 67 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 ng an error mess
1025a 61 67 65 20 77 69 6c 6c 20 68 61 76 65 20 6e 6f age will have no
1025b 20 61 64 76 65 72 73 65 20 65 66 66 65 63 74 0a adverse effect.
1025c 2a 2a 20 6f 6e 20 53 51 4c 69 74 65 2e 20 49 74 ** on SQLite. It
1025d 20 69 73 20 66 69 6e 65 20 74 6f 20 68 61 76 65 is fine to have
1025e 20 61 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 an implementati
1025f 6f 6e 20 74 68 61 74 20 6e 65 76 65 72 0a 2a 2a on that never.**
10260 20 72 65 74 75 72 6e 73 20 61 6e 20 65 72 72 6f returns an erro
10261 72 20 6d 65 73 73 61 67 65 3a 0a 2a 2a 0a 2a 2a r message:.**.**
10262 20 20 20 69 6e 74 20 78 47 65 74 4c 61 73 74 45 int xGetLastE
10263 72 72 6f 72 28 73 71 6c 69 74 65 33 5f 76 66 73 rror(sqlite3_vfs
10264 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42 75 66 *pVfs, int nBuf
10265 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a 2a , char *zBuf){.*
10266 2a 20 20 20 20 20 61 73 73 65 72 74 28 7a 42 75 * assert(zBu
10267 66 5b 30 5d 3d 3d 27 5c 30 27 29 3b 0a 2a 2a 20 f[0]=='\0');.**
10268 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 2a 2a return 0;.**
10269 20 20 20 7d 0a 2a 2a 0a 2a 2a 20 48 6f 77 65 76 }.**.** Howev
1026a 65 72 20 69 66 20 61 6e 20 65 72 72 6f 72 20 6d er if an error m
1026b 65 73 73 61 67 65 20 69 73 20 73 75 70 70 6c 69 essage is suppli
1026c 65 64 2c 20 69 74 20 77 69 6c 6c 20 62 65 20 69 ed, it will be i
1026d 6e 63 6f 72 70 6f 72 61 74 65 64 0a 2a 2a 20 62 ncorporated.** b
1026e 79 20 73 71 6c 69 74 65 20 69 6e 74 6f 20 74 68 y sqlite into th
1026f 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 e error message
10270 61 76 61 69 6c 61 62 6c 65 20 74 6f 20 74 68 65 available to the
10271 20 75 73 65 72 20 75 73 69 6e 67 0a 2a 2a 20 73 user using.** s
10272 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 2c qlite3_errmsg(),
10273 20 70 6f 73 73 69 62 6c 79 20 6d 61 6b 69 6e 67 possibly making
10274 20 49 4f 20 65 72 72 6f 72 73 20 65 61 73 69 65 IO errors easie
10275 72 20 74 6f 20 64 65 62 75 67 2e 0a 2a 2f 0a 73 r to debug..*/.s
10276 74 61 74 69 63 20 69 6e 74 20 77 69 6e 47 65 74 tatic int winGet
10277 4c 61 73 74 45 72 72 6f 72 28 73 71 6c 69 74 65 LastError(sqlite
10278 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 3_vfs *pVfs, int
10279 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 nBuf, char *zBu
1027a 66 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 f){. UNUSED_PAR
1027b 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 20 20 AMETER(pVfs);.
1027c 72 65 74 75 72 6e 20 67 65 74 4c 61 73 74 45 72 return getLastEr
1027d 72 6f 72 4d 73 67 28 6e 42 75 66 2c 20 7a 42 75 rorMsg(nBuf, zBu
1027e 66 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 f);.}../*.** Ini
1027f 74 69 61 6c 69 7a 65 20 61 6e 64 20 64 65 69 6e tialize and dein
10280 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6f 70 65 itialize the ope
10281 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 6e rating system in
10282 74 65 72 66 61 63 65 2e 0a 2a 2f 0a 53 51 4c 49 terface..*/.SQLI
10283 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
10284 65 33 5f 6f 73 5f 69 6e 69 74 28 76 6f 69 64 29 e3_os_init(void)
10285 7b 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69 74 {. static sqlit
10286 65 33 5f 76 66 73 20 77 69 6e 56 66 73 20 3d 20 e3_vfs winVfs =
10287 7b 0a 20 20 20 20 31 2c 20 20 20 20 20 20 20 20 {. 1,
10288 20 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65 72 /* iVer
10289 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 73 69 7a 65 sion */. size
1028a 6f 66 28 77 69 6e 46 69 6c 65 29 2c 20 20 20 2f of(winFile), /
1028b 2a 20 73 7a 4f 73 46 69 6c 65 20 2a 2f 0a 20 20 * szOsFile */.
1028c 20 20 4d 41 58 5f 50 41 54 48 2c 20 20 20 20 20 MAX_PATH,
1028d 20 20 20 20 20 2f 2a 20 6d 78 50 61 74 68 6e 61 /* mxPathna
1028e 6d 65 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20 me */. 0,
1028f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
10290 70 4e 65 78 74 20 2a 2f 0a 20 20 20 20 22 77 69 pNext */. "wi
10291 6e 33 32 22 2c 20 20 20 20 20 20 20 20 20 20 20 n32",
10292 2f 2a 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 20 20 /* zName */.
10293 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
10294 20 20 20 2f 2a 20 70 41 70 70 44 61 74 61 20 2a /* pAppData *
10295 2f 0a 20 0a 20 20 20 20 77 69 6e 4f 70 65 6e 2c /. . winOpen,
10296 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4f /* xO
10297 70 65 6e 20 2a 2f 0a 20 20 20 20 77 69 6e 44 65 pen */. winDe
10298 6c 65 74 65 2c 20 20 20 20 20 20 20 20 20 2f 2a lete, /*
10299 20 78 44 65 6c 65 74 65 20 2a 2f 0a 20 20 20 20 xDelete */.
1029a 77 69 6e 41 63 63 65 73 73 2c 20 20 20 20 20 20 winAccess,
1029b 20 20 20 2f 2a 20 78 41 63 63 65 73 73 20 2a 2f /* xAccess */
1029c 0a 20 20 20 20 77 69 6e 46 75 6c 6c 50 61 74 68 . winFullPath
1029d 6e 61 6d 65 2c 20 20 20 2f 2a 20 78 46 75 6c 6c name, /* xFull
1029e 50 61 74 68 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 Pathname */.
1029f 77 69 6e 44 6c 4f 70 65 6e 2c 20 20 20 20 20 20 winDlOpen,
102a0 20 20 20 2f 2a 20 78 44 6c 4f 70 65 6e 20 2a 2f /* xDlOpen */
102a1 0a 20 20 20 20 77 69 6e 44 6c 45 72 72 6f 72 2c . winDlError,
102a2 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 45 72 /* xDlEr
102a3 72 6f 72 20 2a 2f 0a 20 20 20 20 77 69 6e 44 6c ror */. winDl
102a4 53 79 6d 2c 20 20 20 20 20 20 20 20 20 20 2f 2a Sym, /*
102a5 20 78 44 6c 53 79 6d 20 2a 2f 0a 20 20 20 20 77 xDlSym */. w
102a6 69 6e 44 6c 43 6c 6f 73 65 2c 20 20 20 20 20 20 inDlClose,
102a7 20 20 2f 2a 20 78 44 6c 43 6c 6f 73 65 20 2a 2f /* xDlClose */
102a8 0a 20 20 20 20 77 69 6e 52 61 6e 64 6f 6d 6e 65 . winRandomne
102a9 73 73 2c 20 20 20 20 20 2f 2a 20 78 52 61 6e 64 ss, /* xRand
102aa 6f 6d 6e 65 73 73 20 2a 2f 0a 20 20 20 20 77 69 omness */. wi
102ab 6e 53 6c 65 65 70 2c 20 20 20 20 20 20 20 20 20 nSleep,
102ac 20 2f 2a 20 78 53 6c 65 65 70 20 2a 2f 0a 20 20 /* xSleep */.
102ad 20 20 77 69 6e 43 75 72 72 65 6e 74 54 69 6d 65 winCurrentTime
102ae 2c 20 20 20 20 2f 2a 20 78 43 75 72 72 65 6e 74 , /* xCurrent
102af 54 69 6d 65 20 2a 2f 0a 20 20 20 20 77 69 6e 47 Time */. winG
102b0 65 74 4c 61 73 74 45 72 72 6f 72 20 20 20 20 2f etLastError /
102b1 2a 20 78 47 65 74 4c 61 73 74 45 72 72 6f 72 20 * xGetLastError
102b2 2a 2f 0a 20 20 7d 3b 0a 0a 20 20 73 71 6c 69 74 */. };.. sqlit
102b3 65 33 5f 76 66 73 5f 72 65 67 69 73 74 65 72 28 e3_vfs_register(
102b4 26 77 69 6e 56 66 73 2c 20 31 29 3b 0a 20 20 72 &winVfs, 1);. r
102b5 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
102b6 20 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .}.SQLITE_API i
102b7 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e nt sqlite3_os_en
102b8 64 28 76 6f 69 64 29 7b 20 0a 20 20 72 65 74 75 d(void){ . retu
102b9 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
102ba 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
102bb 45 5f 4f 53 5f 57 49 4e 20 2a 2f 0a 0a 2f 2a 2a E_OS_WIN */../**
102bc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
102bd 20 6f 66 20 6f 73 5f 77 69 6e 2e 63 20 2a 2a 2a of os_win.c ***
102be 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
102bf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
102c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
102c1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 ************ Beg
102c2 69 6e 20 66 69 6c 65 20 62 69 74 76 65 63 2e 63 in file bitvec.c
102c3 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
102c4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
102c5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a ***********/./*.
102c6 2a 2a 20 32 30 30 38 20 46 65 62 72 75 61 72 79 ** 2008 February
102c7 20 31 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 16.**.** The au
102c8 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 thor disclaims c
102c9 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 opyright to this
102ca 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 source code. I
102cb 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 n place of.** a
102cc 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 legal notice, he
102cd 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 re is a blessing
102ce 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 :.**.** May y
102cf 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e ou do good and n
102d0 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d ot evil..** M
102d1 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 ay you find forg
102d2 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 iveness for your
102d3 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 self and forgive
102d4 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d others..** M
102d5 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 ay you share fre
102d6 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e ely, never takin
102d7 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 g more than you
102d8 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a give..**.*******
102d9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
102da 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
102db 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
102dc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
102dd 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 **.** This file
102de 69 6d 70 6c 65 6d 65 6e 74 73 20 61 6e 20 6f 62 implements an ob
102df 6a 65 63 74 20 74 68 61 74 20 72 65 70 72 65 73 ject that repres
102e0 65 6e 74 73 20 61 20 66 69 78 65 64 2d 6c 65 6e ents a fixed-len
102e1 67 74 68 0a 2a 2a 20 62 69 74 6d 61 70 2e 20 20 gth.** bitmap.
102e2 42 69 74 73 20 61 72 65 20 6e 75 6d 62 65 72 65 Bits are numbere
102e3 64 20 73 74 61 72 74 69 6e 67 20 77 69 74 68 20 d starting with
102e4 31 2e 0a 2a 2a 0a 2a 2a 20 41 20 62 69 74 6d 61 1..**.** A bitma
102e5 70 20 69 73 20 75 73 65 64 20 74 6f 20 72 65 63 p is used to rec
102e6 6f 72 64 20 77 68 69 63 68 20 70 61 67 65 73 20 ord which pages
102e7 6f 66 20 61 20 64 61 74 61 62 61 73 65 20 66 69 of a database fi
102e8 6c 65 20 68 61 76 65 20 62 65 65 6e 0a 2a 2a 20 le have been.**
102e9 6a 6f 75 72 6e 61 6c 6c 65 64 20 64 75 72 69 6e journalled durin
102ea 67 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c g a transaction,
102eb 20 6f 72 20 77 68 69 63 68 20 70 61 67 65 73 20 or which pages
102ec 68 61 76 65 20 74 68 65 20 22 64 6f 6e 74 2d 77 have the "dont-w
102ed 72 69 74 65 22 0a 2a 2a 20 70 72 6f 70 65 72 74 rite".** propert
102ee 79 2e 20 20 55 73 75 61 6c 6c 79 20 6f 6e 6c 79 y. Usually only
102ef 20 61 20 66 65 77 20 70 61 67 65 73 20 61 72 65 a few pages are
102f0 20 6d 65 65 74 20 65 69 74 68 65 72 20 63 6f 6e meet either con
102f1 64 69 74 69 6f 6e 2e 0a 2a 2a 20 53 6f 20 74 68 dition..** So th
102f2 65 20 62 69 74 6d 61 70 20 69 73 20 75 73 75 61 e bitmap is usua
102f3 6c 6c 79 20 73 70 61 72 73 65 20 61 6e 64 20 68 lly sparse and h
102f4 61 73 20 6c 6f 77 20 63 61 72 64 69 6e 61 6c 69 as low cardinali
102f5 74 79 2e 0a 2a 2a 20 42 75 74 20 73 6f 6d 65 74 ty..** But somet
102f6 69 6d 65 73 20 28 66 6f 72 20 65 78 61 6d 70 6c imes (for exampl
102f7 65 20 77 68 65 6e 20 64 75 72 69 6e 67 20 61 20 e when during a
102f8 44 52 4f 50 20 6f 66 20 61 20 6c 61 72 67 65 20 DROP of a large
102f9 74 61 62 6c 65 29 20 6d 6f 73 74 0a 2a 2a 20 6f table) most.** o
102fa 72 20 61 6c 6c 20 6f 66 20 74 68 65 20 70 61 67 r all of the pag
102fb 65 73 20 69 6e 20 61 20 64 61 74 61 62 61 73 65 es in a database
102fc 20 63 61 6e 20 67 65 74 20 6a 6f 75 72 6e 61 6c can get journal
102fd 6c 65 64 2e 20 20 49 6e 20 74 68 6f 73 65 20 63 led. In those c
102fe 61 73 65 73 2c 20 0a 2a 2a 20 74 68 65 20 62 69 ases, .** the bi
102ff 74 6d 61 70 20 62 65 63 6f 6d 65 73 20 64 65 6e tmap becomes den
10300 73 65 20 77 69 74 68 20 68 69 67 68 20 63 61 72 se with high car
10301 64 69 6e 61 6c 69 74 79 2e 20 20 54 68 65 20 61 dinality. The a
10302 6c 67 6f 72 69 74 68 6d 20 6e 65 65 64 73 20 0a lgorithm needs .
10303 2a 2a 20 74 6f 20 68 61 6e 64 6c 65 20 62 6f 74 ** to handle bot
10304 68 20 63 61 73 65 73 20 77 65 6c 6c 2e 0a 2a 2a h cases well..**
10305 0a 2a 2a 20 54 68 65 20 73 69 7a 65 20 6f 66 20 .** The size of
10306 74 68 65 20 62 69 74 6d 61 70 20 69 73 20 66 69 the bitmap is fi
10307 78 65 64 20 77 68 65 6e 20 74 68 65 20 6f 62 6a xed when the obj
10308 65 63 74 20 69 73 20 63 72 65 61 74 65 64 2e 0a ect is created..
10309 2a 2a 0a 2a 2a 20 41 6c 6c 20 62 69 74 73 20 61 **.** All bits a
1030a 72 65 20 63 6c 65 61 72 20 77 68 65 6e 20 74 68 re clear when th
1030b 65 20 62 69 74 6d 61 70 20 69 73 20 63 72 65 61 e bitmap is crea
1030c 74 65 64 2e 20 20 49 6e 64 69 76 69 64 75 61 6c ted. Individual
1030d 20 62 69 74 73 0a 2a 2a 20 6d 61 79 20 62 65 20 bits.** may be
1030e 73 65 74 20 6f 72 20 63 6c 65 61 72 65 64 20 6f set or cleared o
1030f 6e 65 20 61 74 20 61 20 74 69 6d 65 2e 0a 2a 2a ne at a time..**
10310 0a 2a 2a 20 54 65 73 74 20 6f 70 65 72 61 74 69 .** Test operati
10311 6f 6e 73 20 61 72 65 20 61 62 6f 75 74 20 31 30 ons are about 10
10312 30 20 74 69 6d 65 73 20 6d 6f 72 65 20 63 6f 6d 0 times more com
10313 6d 6f 6e 20 74 68 61 74 20 73 65 74 20 6f 70 65 mon that set ope
10314 72 61 74 69 6f 6e 73 2e 0a 2a 2a 20 43 6c 65 61 rations..** Clea
10315 72 20 6f 70 65 72 61 74 69 6f 6e 73 20 61 72 65 r operations are
10316 20 65 78 63 65 65 64 69 6e 67 6c 79 20 72 61 72 exceedingly rar
10317 65 2e 20 20 54 68 65 72 65 20 61 72 65 20 75 73 e. There are us
10318 75 61 6c 6c 79 20 62 65 74 77 65 65 6e 0a 2a 2a ually between.**
10319 20 35 20 61 6e 64 20 35 30 30 20 73 65 74 20 6f 5 and 500 set o
1031a 70 65 72 61 74 69 6f 6e 73 20 70 65 72 20 42 69 perations per Bi
1031b 74 76 65 63 20 6f 62 6a 65 63 74 2c 20 74 68 6f tvec object, tho
1031c 75 67 68 20 74 68 65 20 6e 75 6d 62 65 72 20 6f ugh the number o
1031d 66 20 73 65 74 73 20 63 61 6e 0a 2a 2a 20 73 6f f sets can.** so
1031e 6d 65 74 69 6d 65 73 20 67 72 6f 77 20 69 6e 74 metimes grow int
1031f 6f 20 74 65 6e 73 20 6f 66 20 74 68 6f 75 73 61 o tens of thousa
10320 6e 64 73 20 6f 72 20 6c 61 72 67 65 72 2e 20 20 nds or larger.
10321 54 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 0a The size of the.
10322 2a 2a 20 42 69 74 76 65 63 20 6f 62 6a 65 63 74 ** Bitvec object
10323 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f is the number o
10324 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 64 f pages in the d
10325 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 74 20 atabase file at
10326 74 68 65 0a 2a 2a 20 73 74 61 72 74 20 6f 66 20 the.** start of
10327 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 61 a transaction, a
10328 6e 64 20 69 73 20 74 68 75 73 20 75 73 75 61 6c nd is thus usual
10329 6c 79 20 6c 65 73 73 20 74 68 61 6e 20 61 20 66 ly less than a f
1032a 65 77 20 74 68 6f 75 73 61 6e 64 2c 0a 2a 2a 20 ew thousand,.**
1032b 62 75 74 20 63 61 6e 20 62 65 20 61 73 20 6c 61 but can be as la
1032c 72 67 65 20 61 73 20 32 20 62 69 6c 6c 69 6f 6e rge as 2 billion
1032d 20 66 6f 72 20 61 20 72 65 61 6c 6c 79 20 62 69 for a really bi
1032e 67 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a g database..**.*
1032f 2a 20 40 28 23 29 20 24 49 64 3a 20 62 69 74 76 * @(#) $Id: bitv
10330 65 63 2e 63 2c 76 20 31 2e 31 37 20 32 30 30 39 ec.c,v 1.17 2009
10331 2f 30 37 2f 32 35 20 31 37 3a 33 33 3a 32 36 20 /07/25 17:33:26
10332 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a drh Exp $.*/../*
10333 20 53 69 7a 65 20 6f 66 20 74 68 65 20 42 69 74 Size of the Bit
10334 76 65 63 20 73 74 72 75 63 74 75 72 65 20 69 6e vec structure in
10335 20 62 79 74 65 73 2e 20 2a 2f 0a 23 64 65 66 69 bytes. */.#defi
10336 6e 65 20 42 49 54 56 45 43 5f 53 5a 20 20 20 20 ne BITVEC_SZ
10337 20 20 20 20 28 73 69 7a 65 6f 66 28 76 6f 69 64 (sizeof(void
10338 2a 29 2a 31 32 38 29 20 20 2f 2a 20 35 31 32 20 *)*128) /* 512
10339 6f 6e 20 33 32 62 69 74 2e 20 20 31 30 32 34 20 on 32bit. 1024
1033a 6f 6e 20 36 34 62 69 74 20 2a 2f 0a 0a 2f 2a 20 on 64bit */../*
1033b 52 6f 75 6e 64 20 74 68 65 20 75 6e 69 6f 6e 20 Round the union
1033c 73 69 7a 65 20 64 6f 77 6e 20 74 6f 20 74 68 65 size down to the
1033d 20 6e 65 61 72 65 73 74 20 70 6f 69 6e 74 65 72 nearest pointer
1033e 20 62 6f 75 6e 64 61 72 79 2c 20 73 69 6e 63 65 boundary, since
1033f 20 74 68 61 74 27 73 20 68 6f 77 20 0a 2a 2a 20 that's how .**
10340 69 74 20 77 69 6c 6c 20 62 65 20 61 6c 69 67 6e it will be align
10341 65 64 20 77 69 74 68 69 6e 20 74 68 65 20 42 69 ed within the Bi
10342 74 76 65 63 20 73 74 72 75 63 74 2e 20 2a 2f 0a tvec struct. */.
10343 23 64 65 66 69 6e 65 20 42 49 54 56 45 43 5f 55 #define BITVEC_U
10344 53 49 5a 45 20 20 20 20 20 28 28 28 42 49 54 56 SIZE (((BITV
10345 45 43 5f 53 5a 2d 28 33 2a 73 69 7a 65 6f 66 28 EC_SZ-(3*sizeof(
10346 75 33 32 29 29 29 2f 73 69 7a 65 6f 66 28 42 69 u32)))/sizeof(Bi
10347 74 76 65 63 2a 29 29 2a 73 69 7a 65 6f 66 28 42 tvec*))*sizeof(B
10348 69 74 76 65 63 2a 29 29 0a 0a 2f 2a 20 54 79 70 itvec*))../* Typ
10349 65 20 6f 66 20 74 68 65 20 61 72 72 61 79 20 22 e of the array "
1034a 65 6c 65 6d 65 6e 74 22 20 66 6f 72 20 74 68 65 element" for the
1034b 20 62 69 74 6d 61 70 20 72 65 70 72 65 73 65 6e bitmap represen
1034c 74 61 74 69 6f 6e 2e 20 0a 2a 2a 20 53 68 6f 75 tation. .** Shou
1034d 6c 64 20 62 65 20 61 20 70 6f 77 65 72 20 6f 66 ld be a power of
1034e 20 32 2c 20 61 6e 64 20 69 64 65 61 6c 6c 79 2c 2, and ideally,
1034f 20 65 76 65 6e 6c 79 20 64 69 76 69 64 65 20 69 evenly divide i
10350 6e 74 6f 20 42 49 54 56 45 43 5f 55 53 49 5a 45 nto BITVEC_USIZE
10351 2e 20 0a 2a 2a 20 53 65 74 74 69 6e 67 20 74 68 . .** Setting th
10352 69 73 20 74 6f 20 74 68 65 20 22 6e 61 74 75 72 is to the "natur
10353 61 6c 20 77 6f 72 64 22 20 73 69 7a 65 20 6f 66 al word" size of
10354 20 79 6f 75 72 20 43 50 55 20 6d 61 79 20 69 6d your CPU may im
10355 70 72 6f 76 65 0a 2a 2a 20 70 65 72 66 6f 72 6d prove.** perform
10356 61 6e 63 65 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 ance. */.#define
10357 20 42 49 54 56 45 43 5f 54 45 4c 45 4d 20 20 20 BITVEC_TELEM
10358 20 20 75 38 0a 2f 2a 20 53 69 7a 65 2c 20 69 6e u8./* Size, in
10359 20 62 69 74 73 2c 20 6f 66 20 74 68 65 20 62 69 bits, of the bi
1035a 74 6d 61 70 20 65 6c 65 6d 65 6e 74 2e 20 2a 2f tmap element. */
1035b 0a 23 64 65 66 69 6e 65 20 42 49 54 56 45 43 5f .#define BITVEC_
1035c 53 5a 45 4c 45 4d 20 20 20 20 38 0a 2f 2a 20 4e SZELEM 8./* N
1035d 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 umber of element
1035e 73 20 69 6e 20 61 20 62 69 74 6d 61 70 20 61 72 s in a bitmap ar
1035f 72 61 79 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 ray. */.#define
10360 42 49 54 56 45 43 5f 4e 45 4c 45 4d 20 20 20 20 BITVEC_NELEM
10361 20 28 42 49 54 56 45 43 5f 55 53 49 5a 45 2f 73 (BITVEC_USIZE/s
10362 69 7a 65 6f 66 28 42 49 54 56 45 43 5f 54 45 4c izeof(BITVEC_TEL
10363 45 4d 29 29 0a 2f 2a 20 4e 75 6d 62 65 72 20 6f EM))./* Number o
10364 66 20 62 69 74 73 20 69 6e 20 74 68 65 20 62 69 f bits in the bi
10365 74 6d 61 70 20 61 72 72 61 79 2e 20 2a 2f 0a 23 tmap array. */.#
10366 64 65 66 69 6e 65 20 42 49 54 56 45 43 5f 4e 42 define BITVEC_NB
10367 49 54 20 20 20 20 20 20 28 42 49 54 56 45 43 5f IT (BITVEC_
10368 4e 45 4c 45 4d 2a 42 49 54 56 45 43 5f 53 5a 45 NELEM*BITVEC_SZE
10369 4c 45 4d 29 0a 0a 2f 2a 20 4e 75 6d 62 65 72 20 LEM)../* Number
1036a 6f 66 20 75 33 32 20 76 61 6c 75 65 73 20 69 6e of u32 values in
1036b 20 68 61 73 68 20 74 61 62 6c 65 2e 20 2a 2f 0a hash table. */.
1036c 23 64 65 66 69 6e 65 20 42 49 54 56 45 43 5f 4e #define BITVEC_N
1036d 49 4e 54 20 20 20 20 20 20 28 42 49 54 56 45 43 INT (BITVEC
1036e 5f 55 53 49 5a 45 2f 73 69 7a 65 6f 66 28 75 33 _USIZE/sizeof(u3
1036f 32 29 29 0a 2f 2a 20 4d 61 78 69 6d 75 6d 20 6e 2))./* Maximum n
10370 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 umber of entries
10371 20 69 6e 20 68 61 73 68 20 74 61 62 6c 65 20 62 in hash table b
10372 65 66 6f 72 65 20 0a 2a 2a 20 73 75 62 2d 64 69 efore .** sub-di
10373 76 69 64 69 6e 67 20 61 6e 64 20 72 65 2d 68 61 viding and re-ha
10374 73 68 69 6e 67 2e 20 2a 2f 0a 23 64 65 66 69 6e shing. */.#defin
10375 65 20 42 49 54 56 45 43 5f 4d 58 48 41 53 48 20 e BITVEC_MXHASH
10376 20 20 20 28 42 49 54 56 45 43 5f 4e 49 4e 54 2f (BITVEC_NINT/
10377 32 29 0a 2f 2a 20 48 61 73 68 69 6e 67 20 66 75 2)./* Hashing fu
10378 6e 63 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 61 nction for the a
10379 48 61 73 68 20 72 65 70 72 65 73 65 6e 74 61 74 Hash representat
1037a 69 6f 6e 2e 0a 2a 2a 20 45 6d 70 69 72 69 63 61 ion..** Empirica
1037b 6c 20 74 65 73 74 69 6e 67 20 73 68 6f 77 65 64 l testing showed
1037c 20 74 68 61 74 20 74 68 65 20 2a 33 37 20 6d 75 that the *37 mu
1037d 6c 74 69 70 6c 69 65 72 20 0a 2a 2a 20 28 61 6e ltiplier .** (an
1037e 20 61 72 62 69 74 72 61 72 79 20 70 72 69 6d 65 arbitrary prime
1037f 29 69 6e 20 74 68 65 20 68 61 73 68 20 66 75 6e )in the hash fun
10380 63 74 69 6f 6e 20 70 72 6f 76 69 64 65 64 20 0a ction provided .
10381 2a 2a 20 6e 6f 20 66 65 77 65 72 20 63 6f 6c 6c ** no fewer coll
10382 69 73 69 6f 6e 73 20 74 68 61 6e 20 74 68 65 20 isions than the
10383 6e 6f 2d 6f 70 20 2a 31 2e 20 2a 2f 0a 23 64 65 no-op *1. */.#de
10384 66 69 6e 65 20 42 49 54 56 45 43 5f 48 41 53 48 fine BITVEC_HASH
10385 28 58 29 20 20 20 28 28 28 58 29 2a 31 29 25 42 (X) (((X)*1)%B
10386 49 54 56 45 43 5f 4e 49 4e 54 29 0a 0a 23 64 65 ITVEC_NINT)..#de
10387 66 69 6e 65 20 42 49 54 56 45 43 5f 4e 50 54 52 fine BITVEC_NPTR
10388 20 20 20 20 20 20 28 42 49 54 56 45 43 5f 55 53 (BITVEC_US
10389 49 5a 45 2f 73 69 7a 65 6f 66 28 42 69 74 76 65 IZE/sizeof(Bitve
1038a 63 20 2a 29 29 0a 0a 0a 2f 2a 0a 2a 2a 20 41 20 c *)).../*.** A
1038b 62 69 74 6d 61 70 20 69 73 20 61 6e 20 69 6e 73 bitmap is an ins
1038c 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c tance of the fol
1038d 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 lowing structure
1038e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 62 69 74 ..**.** This bit
1038f 6d 61 70 20 72 65 63 6f 72 64 73 20 74 68 65 20 map records the
10390 65 78 69 73 74 61 6e 63 65 20 6f 66 20 7a 65 72 existance of zer
10391 6f 20 6f 72 20 6d 6f 72 65 20 62 69 74 73 0a 2a o or more bits.*
10392 2a 20 77 69 74 68 20 76 61 6c 75 65 73 20 62 65 * with values be
10393 74 77 65 65 6e 20 31 20 61 6e 64 20 69 53 69 7a tween 1 and iSiz
10394 65 2c 20 69 6e 63 6c 75 73 69 76 65 2e 0a 2a 2a e, inclusive..**
10395 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 74 68 .** There are th
10396 72 65 65 20 70 6f 73 73 69 62 6c 65 20 72 65 70 ree possible rep
10397 72 65 73 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 resentations of
10398 74 68 65 20 62 69 74 6d 61 70 2e 0a 2a 2a 20 49 the bitmap..** I
10399 66 20 69 53 69 7a 65 3c 3d 42 49 54 56 45 43 5f f iSize<=BITVEC_
1039a 4e 42 49 54 2c 20 74 68 65 6e 20 42 69 74 76 65 NBIT, then Bitve
1039b 63 2e 75 2e 61 42 69 74 6d 61 70 5b 5d 20 69 73 c.u.aBitmap[] is
1039c 20 61 20 73 74 72 61 69 67 68 74 0a 2a 2a 20 62 a straight.** b
1039d 69 74 6d 61 70 2e 20 20 54 68 65 20 6c 65 61 73 itmap. The leas
1039e 74 20 73 69 67 6e 69 66 69 63 61 6e 74 20 62 69 t significant bi
1039f 74 20 69 73 20 62 69 74 20 31 2e 0a 2a 2a 0a 2a t is bit 1..**.*
103a0 2a 20 49 66 20 69 53 69 7a 65 3e 42 49 54 56 45 * If iSize>BITVE
103a1 43 5f 4e 42 49 54 20 61 6e 64 20 69 44 69 76 69 C_NBIT and iDivi
103a2 73 6f 72 3d 3d 30 20 74 68 65 6e 20 42 69 74 76 sor==0 then Bitv
103a3 65 63 2e 75 2e 61 48 61 73 68 5b 5d 20 69 73 0a ec.u.aHash[] is.
103a4 2a 2a 20 61 20 68 61 73 68 20 74 61 62 6c 65 20 ** a hash table
103a5 74 68 61 74 20 77 69 6c 6c 20 68 6f 6c 64 20 75 that will hold u
103a6 70 20 74 6f 20 42 49 54 56 45 43 5f 4d 58 48 41 p to BITVEC_MXHA
103a7 53 48 20 64 69 73 74 69 6e 63 74 20 76 61 6c 75 SH distinct valu
103a8 65 73 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 es..**.** Otherw
103a9 69 73 65 2c 20 74 68 65 20 76 61 6c 75 65 20 69 ise, the value i
103aa 20 69 73 20 72 65 64 69 72 65 63 74 65 64 20 69 is redirected i
103ab 6e 74 6f 20 6f 6e 65 20 6f 66 20 42 49 54 56 45 nto one of BITVE
103ac 43 5f 4e 50 54 52 0a 2a 2a 20 73 75 62 2d 62 69 C_NPTR.** sub-bi
103ad 74 6d 61 70 73 20 70 6f 69 6e 74 65 64 20 74 6f tmaps pointed to
103ae 20 62 79 20 42 69 74 76 65 63 2e 75 2e 61 70 53 by Bitvec.u.apS
103af 75 62 5b 5d 2e 20 20 45 61 63 68 20 73 75 62 62 ub[]. Each subb
103b0 69 74 6d 61 70 0a 2a 2a 20 68 61 6e 64 6c 65 73 itmap.** handles
103b1 20 75 70 20 74 6f 20 69 44 69 76 69 73 6f 72 20 up to iDivisor
103b2 73 65 70 61 72 61 74 65 20 76 61 6c 75 65 73 20 separate values
103b3 6f 66 20 69 2e 20 20 61 70 53 75 62 5b 30 5d 20 of i. apSub[0]
103b4 68 6f 6c 64 73 0a 2a 2a 20 76 61 6c 75 65 73 20 holds.** values
103b5 62 65 74 77 65 65 6e 20 31 20 61 6e 64 20 69 44 between 1 and iD
103b6 69 76 69 73 6f 72 2e 20 20 61 70 53 75 62 5b 31 ivisor. apSub[1
103b7 5d 20 68 6f 6c 64 73 20 76 61 6c 75 65 73 20 62 ] holds values b
103b8 65 74 77 65 65 6e 0a 2a 2a 20 69 44 69 76 69 73 etween.** iDivis
103b9 6f 72 2b 31 20 61 6e 64 20 32 2a 69 44 69 76 69 or+1 and 2*iDivi
103ba 73 6f 72 2e 20 20 61 70 53 75 62 5b 4e 5d 20 68 sor. apSub[N] h
103bb 6f 6c 64 73 20 76 61 6c 75 65 73 20 62 65 74 77 olds values betw
103bc 65 65 6e 0a 2a 2a 20 4e 2a 69 44 69 76 69 73 6f een.** N*iDiviso
103bd 72 2b 31 20 61 6e 64 20 28 4e 2b 31 29 2a 69 44 r+1 and (N+1)*iD
103be 69 76 69 73 6f 72 2e 20 20 45 61 63 68 20 73 75 ivisor. Each su
103bf 62 62 69 74 6d 61 70 20 69 73 20 6e 6f 72 6d 61 bbitmap is norma
103c0 6c 69 7a 65 64 0a 2a 2a 20 74 6f 20 68 6f 6c 64 lized.** to hold
103c1 20 64 65 61 6c 20 77 69 74 68 20 76 61 6c 75 65 deal with value
103c2 73 20 62 65 74 77 65 65 6e 20 31 20 61 6e 64 20 s between 1 and
103c3 69 44 69 76 69 73 6f 72 2e 0a 2a 2f 0a 73 74 72 iDivisor..*/.str
103c4 75 63 74 20 42 69 74 76 65 63 20 7b 0a 20 20 75 uct Bitvec {. u
103c5 33 32 20 69 53 69 7a 65 3b 20 20 20 20 20 20 2f 32 iSize; /
103c6 2a 20 4d 61 78 69 6d 75 6d 20 62 69 74 20 69 6e * Maximum bit in
103c7 64 65 78 2e 20 20 4d 61 78 20 69 53 69 7a 65 20 dex. Max iSize
103c8 69 73 20 34 2c 32 39 34 2c 39 36 37 2c 32 39 36 is 4,294,967,296
103c9 2e 20 2a 2f 0a 20 20 75 33 32 20 6e 53 65 74 3b . */. u32 nSet;
103ca 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
103cb 20 6f 66 20 62 69 74 73 20 74 68 61 74 20 61 72 of bits that ar
103cc 65 20 73 65 74 20 2d 20 6f 6e 6c 79 20 76 61 6c e set - only val
103cd 69 64 20 66 6f 72 20 61 48 61 73 68 0a 20 20 20 id for aHash.
103ce 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a *
103cf 2a 20 65 6c 65 6d 65 6e 74 2e 20 20 4d 61 78 20 * element. Max
103d0 69 73 20 42 49 54 56 45 43 5f 4e 49 4e 54 2e 20 is BITVEC_NINT.
103d1 20 46 6f 72 20 42 49 54 56 45 43 5f 53 5a 20 6f For BITVEC_SZ o
103d2 66 20 35 31 32 2c 0a 20 20 20 20 20 20 20 20 20 f 512,.
103d3 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68 69 73 ** this
103d4 20 77 6f 75 6c 64 20 62 65 20 31 32 35 2e 20 2a would be 125. *
103d5 2f 0a 20 20 75 33 32 20 69 44 69 76 69 73 6f 72 /. u32 iDivisor
103d6 3b 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 ; /* Number of
103d7 20 62 69 74 73 20 68 61 6e 64 6c 65 64 20 62 79 bits handled by
103d8 20 65 61 63 68 20 61 70 53 75 62 5b 5d 20 65 6e each apSub[] en
103d9 74 72 79 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 try. */.
103da 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 68 6f /* Sho
103db 75 6c 64 20 3e 3d 30 20 66 6f 72 20 61 70 53 75 uld >=0 for apSu
103dc 62 20 65 6c 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20 b element. */.
103dd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
103de 2f 2a 20 4d 61 78 20 69 44 69 76 69 73 6f 72 20 /* Max iDivisor
103df 69 73 20 6d 61 78 28 75 33 32 29 20 2f 20 42 49 is max(u32) / BI
103e0 54 56 45 43 5f 4e 50 54 52 20 2b 20 31 2e 20 20 TVEC_NPTR + 1.
103e1 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 */.
103e2 20 20 20 20 20 2f 2a 20 46 6f 72 20 61 20 42 49 /* For a BI
103e3 54 56 45 43 5f 53 5a 20 6f 66 20 35 31 32 2c 20 TVEC_SZ of 512,
103e4 74 68 69 73 20 77 6f 75 6c 64 20 62 65 20 33 34 this would be 34
103e5 2c 33 35 39 2c 37 33 39 2e 20 2a 2f 0a 20 20 75 ,359,739. */. u
103e6 6e 69 6f 6e 20 7b 0a 20 20 20 20 42 49 54 56 45 nion {. BITVE
103e7 43 5f 54 45 4c 45 4d 20 61 42 69 74 6d 61 70 5b C_TELEM aBitmap[
103e8 42 49 54 56 45 43 5f 4e 45 4c 45 4d 5d 3b 20 20 BITVEC_NELEM];
103e9 20 20 2f 2a 20 42 69 74 6d 61 70 20 72 65 70 72 /* Bitmap repr
103ea 65 73 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 20 esentation */.
103eb 20 20 75 33 32 20 61 48 61 73 68 5b 42 49 54 56 u32 aHash[BITV
103ec 45 43 5f 4e 49 4e 54 5d 3b 20 20 20 20 20 20 2f EC_NINT]; /
103ed 2a 20 48 61 73 68 20 74 61 62 6c 65 20 72 65 70 * Hash table rep
103ee 72 65 73 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 resentation */.
103ef 20 20 20 42 69 74 76 65 63 20 2a 61 70 53 75 62 Bitvec *apSub
103f0 5b 42 49 54 56 45 43 5f 4e 50 54 52 5d 3b 20 20 [BITVEC_NPTR];
103f1 2f 2a 20 52 65 63 75 72 73 69 76 65 20 72 65 70 /* Recursive rep
103f2 72 65 73 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 resentation */.
103f3 20 7d 20 75 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 } u;.};../*.**
103f4 43 72 65 61 74 65 20 61 20 6e 65 77 20 62 69 74 Create a new bit
103f5 6d 61 70 20 6f 62 6a 65 63 74 20 61 62 6c 65 20 map object able
103f6 74 6f 20 68 61 6e 64 6c 65 20 62 69 74 73 20 62 to handle bits b
103f7 65 74 77 65 65 6e 20 30 20 61 6e 64 20 69 53 69 etween 0 and iSi
103f8 7a 65 2c 0a 2a 2a 20 69 6e 63 6c 75 73 69 76 65 ze,.** inclusive
103f9 2e 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e . Return a poin
103fa 74 65 72 20 74 6f 20 74 68 65 20 6e 65 77 20 6f ter to the new o
103fb 62 6a 65 63 74 2e 20 20 52 65 74 75 72 6e 20 4e bject. Return N
103fc 55 4c 4c 20 69 66 20 0a 2a 2a 20 6d 61 6c 6c 6f ULL if .** mallo
103fd 63 20 66 61 69 6c 73 2e 0a 2a 2f 0a 53 51 4c 49 c fails..*/.SQLI
103fe 54 45 5f 50 52 49 56 41 54 45 20 42 69 74 76 65 TE_PRIVATE Bitve
103ff 63 20 2a 73 71 6c 69 74 65 33 42 69 74 76 65 63 c *sqlite3Bitvec
10400 43 72 65 61 74 65 28 75 33 32 20 69 53 69 7a 65 Create(u32 iSize
10401 29 7b 0a 20 20 42 69 74 76 65 63 20 2a 70 3b 0a ){. Bitvec *p;.
10402 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 assert( sizeof
10403 28 2a 70 29 3d 3d 42 49 54 56 45 43 5f 53 5a 20 (*p)==BITVEC_SZ
10404 29 3b 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33 );. p = sqlite3
10405 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65 MallocZero( size
10406 6f 66 28 2a 70 29 20 29 3b 0a 20 20 69 66 28 20 of(*p) );. if(
10407 70 20 29 7b 0a 20 20 20 20 70 2d 3e 69 53 69 7a p ){. p->iSiz
10408 65 20 3d 20 69 53 69 7a 65 3b 0a 20 20 7d 0a 20 e = iSize;. }.
10409 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a return p;.}../*
1040a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 .** Check to see
1040b 20 69 66 20 74 68 65 20 69 2d 74 68 20 62 69 74 if the i-th bit
1040c 20 69 73 20 73 65 74 2e 20 20 52 65 74 75 72 6e is set. Return
1040d 20 74 72 75 65 20 6f 72 20 66 61 6c 73 65 2e 0a true or false..
1040e 2a 2a 20 49 66 20 70 20 69 73 20 4e 55 4c 4c 20 ** If p is NULL
1040f 28 69 66 20 74 68 65 20 62 69 74 6d 61 70 20 68 (if the bitmap h
10410 61 73 20 6e 6f 74 20 62 65 65 6e 20 63 72 65 61 as not been crea
10411 74 65 64 29 20 6f 72 20 69 66 0a 2a 2a 20 69 20 ted) or if.** i
10412 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 2c is out of range,
10413 20 74 68 65 6e 20 72 65 74 75 72 6e 20 66 61 6c then return fal
10414 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 se..*/.SQLITE_PR
10415 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
10416 33 42 69 74 76 65 63 54 65 73 74 28 42 69 74 76 3BitvecTest(Bitv
10417 65 63 20 2a 70 2c 20 75 33 32 20 69 29 7b 0a 20 ec *p, u32 i){.
10418 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 if( p==0 ) retu
10419 72 6e 20 30 3b 0a 20 20 69 66 28 20 69 3e 70 2d rn 0;. if( i>p-
1041a 3e 69 53 69 7a 65 20 7c 7c 20 69 3d 3d 30 20 29 >iSize || i==0 )
1041b 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 2d 2d return 0;. i--
1041c 3b 0a 20 20 77 68 69 6c 65 28 20 70 2d 3e 69 44 ;. while( p->iD
1041d 69 76 69 73 6f 72 20 29 7b 0a 20 20 20 20 75 33 ivisor ){. u3
1041e 32 20 62 69 6e 20 3d 20 69 2f 70 2d 3e 69 44 69 2 bin = i/p->iDi
1041f 76 69 73 6f 72 3b 0a 20 20 20 20 69 20 3d 20 69 visor;. i = i
10420 25 70 2d 3e 69 44 69 76 69 73 6f 72 3b 0a 20 20 %p->iDivisor;.
10421 20 20 70 20 3d 20 70 2d 3e 75 2e 61 70 53 75 62 p = p->u.apSub
10422 5b 62 69 6e 5d 3b 0a 20 20 20 20 69 66 20 28 21 [bin];. if (!
10423 70 29 20 7b 0a 20 20 20 20 20 20 72 65 74 75 72 p) {. retur
10424 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 n 0;. }. }.
10425 20 69 66 28 20 70 2d 3e 69 53 69 7a 65 3c 3d 42 if( p->iSize<=B
10426 49 54 56 45 43 5f 4e 42 49 54 20 29 7b 0a 20 20 ITVEC_NBIT ){.
10427 20 20 72 65 74 75 72 6e 20 28 70 2d 3e 75 2e 61 return (p->u.a
10428 42 69 74 6d 61 70 5b 69 2f 42 49 54 56 45 43 5f Bitmap[i/BITVEC_
10429 53 5a 45 4c 45 4d 5d 20 26 20 28 31 3c 3c 28 69 SZELEM] & (1<<(i
1042a 26 28 42 49 54 56 45 43 5f 53 5a 45 4c 45 4d 2d &(BITVEC_SZELEM-
1042b 31 29 29 29 29 21 3d 30 3b 0a 20 20 7d 20 65 6c 1))))!=0;. } el
1042c 73 65 7b 0a 20 20 20 20 75 33 32 20 68 20 3d 20 se{. u32 h =
1042d 42 49 54 56 45 43 5f 48 41 53 48 28 69 2b 2b 29 BITVEC_HASH(i++)
1042e 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 70 2d 3e ;. while( p->
1042f 75 2e 61 48 61 73 68 5b 68 5d 20 29 7b 0a 20 20 u.aHash[h] ){.
10430 20 20 20 20 69 66 28 20 70 2d 3e 75 2e 61 48 61 if( p->u.aHa
10431 73 68 5b 68 5d 3d 3d 69 20 29 20 72 65 74 75 72 sh[h]==i ) retur
10432 6e 20 31 3b 0a 20 20 20 20 20 20 68 20 3d 20 28 n 1;. h = (
10433 68 2b 31 29 20 25 20 42 49 54 56 45 43 5f 4e 49 h+1) % BITVEC_NI
10434 4e 54 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 NT;. }. re
10435 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f turn 0;. }.}../
10436 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 69 2d 74 *.** Set the i-t
10437 68 20 62 69 74 2e 20 20 52 65 74 75 72 6e 20 30 h bit. Return 0
10438 20 6f 6e 20 73 75 63 63 65 73 73 20 61 6e 64 20 on success and
10439 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66 an error code if
1043a 0a 2a 2a 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 .** anything goe
1043b 73 20 77 72 6f 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 s wrong..**.** T
1043c 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 69 67 68 his routine migh
1043d 74 20 63 61 75 73 65 20 73 75 62 2d 62 69 74 6d t cause sub-bitm
1043e 61 70 73 20 74 6f 20 62 65 20 61 6c 6c 6f 63 61 aps to be alloca
1043f 74 65 64 2e 20 20 46 61 69 6c 69 6e 67 0a 2a 2a ted. Failing.**
10440 20 74 6f 20 67 65 74 20 74 68 65 20 6d 65 6d 6f to get the memo
10441 72 79 20 6e 65 65 64 65 64 20 74 6f 20 68 6f 6c ry needed to hol
10442 64 20 74 68 65 20 73 75 62 2d 62 69 74 6d 61 70 d the sub-bitmap
10443 20 69 73 20 74 68 65 20 6f 6e 6c 79 0a 2a 2a 20 is the only.**
10444 74 68 61 74 20 63 61 6e 20 67 6f 20 77 72 6f 6e that can go wron
10445 67 20 77 69 74 68 20 61 6e 20 69 6e 73 65 72 74 g with an insert
10446 2c 20 61 73 73 75 6d 69 6e 67 20 70 20 61 6e 64 , assuming p and
10447 20 69 20 61 72 65 20 76 61 6c 69 64 2e 0a 2a 2a i are valid..**
10448 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 .** The calling
10449 66 75 6e 63 74 69 6f 6e 20 6d 75 73 74 20 65 6e function must en
1044a 73 75 72 65 20 74 68 61 74 20 70 20 69 73 20 61 sure that p is a
1044b 20 76 61 6c 69 64 20 42 69 74 76 65 63 20 6f 62 valid Bitvec ob
1044c 6a 65 63 74 0a 2a 2a 20 61 6e 64 20 74 68 61 74 ject.** and that
1044d 20 74 68 65 20 76 61 6c 75 65 20 66 6f 72 20 22 the value for "
1044e 69 22 20 69 73 20 77 69 74 68 69 6e 20 72 61 6e i" is within ran
1044f 67 65 20 6f 66 20 74 68 65 20 42 69 74 76 65 63 ge of the Bitvec
10450 20 6f 62 6a 65 63 74 2e 0a 2a 2a 20 4f 74 68 65 object..** Othe
10451 72 77 69 73 65 20 74 68 65 20 62 65 68 61 76 69 rwise the behavi
10452 6f 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e or is undefined.
10453 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
10454 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 69 TE int sqlite3Bi
10455 74 76 65 63 53 65 74 28 42 69 74 76 65 63 20 2a tvecSet(Bitvec *
10456 70 2c 20 75 33 32 20 69 29 7b 0a 20 20 75 33 32 p, u32 i){. u32
10457 20 68 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 h;. if( p==0 )
10458 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
10459 4b 3b 0a 20 20 61 73 73 65 72 74 28 20 69 3e 30 K;. assert( i>0
1045a 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 3c );. assert( i<
1045b 3d 70 2d 3e 69 53 69 7a 65 20 29 3b 0a 20 20 69 =p->iSize );. i
1045c 2d 2d 3b 0a 20 20 77 68 69 6c 65 28 28 70 2d 3e --;. while((p->
1045d 69 53 69 7a 65 20 3e 20 42 49 54 56 45 43 5f 4e iSize > BITVEC_N
1045e 42 49 54 29 20 26 26 20 70 2d 3e 69 44 69 76 69 BIT) && p->iDivi
1045f 73 6f 72 29 20 7b 0a 20 20 20 20 75 33 32 20 62 sor) {. u32 b
10460 69 6e 20 3d 20 69 2f 70 2d 3e 69 44 69 76 69 73 in = i/p->iDivis
10461 6f 72 3b 0a 20 20 20 20 69 20 3d 20 69 25 70 2d or;. i = i%p-
10462 3e 69 44 69 76 69 73 6f 72 3b 0a 20 20 20 20 69 >iDivisor;. i
10463 66 28 20 70 2d 3e 75 2e 61 70 53 75 62 5b 62 69 f( p->u.apSub[bi
10464 6e 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 n]==0 ){. p
10465 2d 3e 75 2e 61 70 53 75 62 5b 62 69 6e 5d 20 3d ->u.apSub[bin] =
10466 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 43 72 sqlite3BitvecCr
10467 65 61 74 65 28 20 70 2d 3e 69 44 69 76 69 73 6f eate( p->iDiviso
10468 72 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 r );. if( p
10469 2d 3e 75 2e 61 70 53 75 62 5b 62 69 6e 5d 3d 3d ->u.apSub[bin]==
1046a 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 0 ) return SQLIT
1046b 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 E_NOMEM;. }.
1046c 20 20 20 70 20 3d 20 70 2d 3e 75 2e 61 70 53 75 p = p->u.apSu
1046d 62 5b 62 69 6e 5d 3b 0a 20 20 7d 0a 20 20 69 66 b[bin];. }. if
1046e 28 20 70 2d 3e 69 53 69 7a 65 3c 3d 42 49 54 56 ( p->iSize<=BITV
1046f 45 43 5f 4e 42 49 54 20 29 7b 0a 20 20 20 20 70 EC_NBIT ){. p
10470 2d 3e 75 2e 61 42 69 74 6d 61 70 5b 69 2f 42 49 ->u.aBitmap[i/BI
10471 54 56 45 43 5f 53 5a 45 4c 45 4d 5d 20 7c 3d 20 TVEC_SZELEM] |=
10472 31 20 3c 3c 20 28 69 26 28 42 49 54 56 45 43 5f 1 << (i&(BITVEC_
10473 53 5a 45 4c 45 4d 2d 31 29 29 3b 0a 20 20 20 20 SZELEM-1));.
10474 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
10475 3b 0a 20 20 7d 0a 20 20 68 20 3d 20 42 49 54 56 ;. }. h = BITV
10476 45 43 5f 48 41 53 48 28 69 2b 2b 29 3b 0a 20 20 EC_HASH(i++);.
10477 2f 2a 20 69 66 20 74 68 65 72 65 20 77 61 73 6e /* if there wasn
10478 27 74 20 61 20 68 61 73 68 20 63 6f 6c 6c 69 73 't a hash collis
10479 69 6f 6e 2c 20 61 6e 64 20 74 68 69 73 20 64 6f ion, and this do
1047a 65 73 6e 27 74 20 2a 2f 0a 20 20 2f 2a 20 63 6f esn't */. /* co
1047b 6d 70 6c 65 74 65 6c 79 20 66 69 6c 6c 20 74 68 mpletely fill th
1047c 65 20 68 61 73 68 2c 20 74 68 65 6e 20 6a 75 73 e hash, then jus
1047d 74 20 61 64 64 20 69 74 20 77 69 74 68 6f 75 74 t add it without
1047e 20 2a 2f 0a 20 20 2f 2a 20 77 6f 72 72 69 6e 67 */. /* worring
1047f 20 61 62 6f 75 74 20 73 75 62 2d 64 69 76 69 64 about sub-divid
10480 69 6e 67 20 61 6e 64 20 72 65 2d 68 61 73 68 69 ing and re-hashi
10481 6e 67 2e 20 2a 2f 0a 20 20 69 66 28 20 21 70 2d ng. */. if( !p-
10482 3e 75 2e 61 48 61 73 68 5b 68 5d 20 29 7b 0a 20 >u.aHash[h] ){.
10483 20 20 20 69 66 20 28 70 2d 3e 6e 53 65 74 3c 28 if (p->nSet<(
10484 42 49 54 56 45 43 5f 4e 49 4e 54 2d 31 29 29 20 BITVEC_NINT-1))
10485 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 69 74 {. goto bit
10486 76 65 63 5f 73 65 74 5f 65 6e 64 3b 0a 20 20 20 vec_set_end;.
10487 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 } else {.
10488 67 6f 74 6f 20 62 69 74 76 65 63 5f 73 65 74 5f goto bitvec_set_
10489 72 65 68 61 73 68 3b 0a 20 20 20 20 7d 0a 20 20 rehash;. }.
1048a 7d 0a 20 20 2f 2a 20 74 68 65 72 65 20 77 61 73 }. /* there was
1048b 20 61 20 63 6f 6c 6c 69 73 69 6f 6e 2c 20 63 68 a collision, ch
1048c 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 69 74 eck to see if it
1048d 27 73 20 61 6c 72 65 61 64 79 20 2a 2f 0a 20 20 's already */.
1048e 2f 2a 20 69 6e 20 68 61 73 68 2c 20 69 66 20 6e /* in hash, if n
1048f 6f 74 2c 20 74 72 79 20 74 6f 20 66 69 6e 64 20 ot, try to find
10490 61 20 73 70 6f 74 20 66 6f 72 20 69 74 20 2a 2f a spot for it */
10491 0a 20 20 64 6f 20 7b 0a 20 20 20 20 69 66 28 20 . do {. if(
10492 70 2d 3e 75 2e 61 48 61 73 68 5b 68 5d 3d 3d 69 p->u.aHash[h]==i
10493 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 ) return SQLITE
10494 5f 4f 4b 3b 0a 20 20 20 20 68 2b 2b 3b 0a 20 20 _OK;. h++;.
10495 20 20 69 66 28 20 68 3e 3d 42 49 54 56 45 43 5f if( h>=BITVEC_
10496 4e 49 4e 54 20 29 20 68 20 3d 20 30 3b 0a 20 20 NINT ) h = 0;.
10497 7d 20 77 68 69 6c 65 28 20 70 2d 3e 75 2e 61 48 } while( p->u.aH
10498 61 73 68 5b 68 5d 20 29 3b 0a 20 20 2f 2a 20 77 ash[h] );. /* w
10499 65 20 64 69 64 6e 27 74 20 66 69 6e 64 20 69 74 e didn't find it
1049a 20 69 6e 20 74 68 65 20 68 61 73 68 2e 20 20 68 in the hash. h
1049b 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 66 points to the f
1049c 69 72 73 74 20 2a 2f 0a 20 20 2f 2a 20 61 76 61 irst */. /* ava
1049d 69 6c 61 62 6c 65 20 66 72 65 65 20 73 70 6f 74 ilable free spot
1049e 2e 20 63 68 65 63 6b 20 74 6f 20 73 65 65 20 69 . check to see i
1049f 66 20 74 68 69 73 20 69 73 20 67 6f 69 6e 67 20 f this is going
104a0 74 6f 20 2a 2f 0a 20 20 2f 2a 20 6d 61 6b 65 20 to */. /* make
104a1 6f 75 72 20 68 61 73 68 20 74 6f 6f 20 22 66 75 our hash too "fu
104a2 6c 6c 22 2e 20 20 2a 2f 0a 62 69 74 76 65 63 5f ll". */.bitvec_
104a3 73 65 74 5f 72 65 68 61 73 68 3a 0a 20 20 69 66 set_rehash:. if
104a4 28 20 70 2d 3e 6e 53 65 74 3e 3d 42 49 54 56 45 ( p->nSet>=BITVE
104a5 43 5f 4d 58 48 41 53 48 20 29 7b 0a 20 20 20 20 C_MXHASH ){.
104a6 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6a 3b 0a unsigned int j;.
104a7 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 int rc;.
104a8 75 33 32 20 2a 61 69 56 61 6c 75 65 73 20 3d 20 u32 *aiValues =
104a9 73 71 6c 69 74 65 33 53 74 61 63 6b 41 6c 6c 6f sqlite3StackAllo
104aa 63 52 61 77 28 30 2c 20 73 69 7a 65 6f 66 28 70 cRaw(0, sizeof(p
104ab 2d 3e 75 2e 61 48 61 73 68 29 29 3b 0a 20 20 20 ->u.aHash));.
104ac 20 69 66 28 20 61 69 56 61 6c 75 65 73 3d 3d 30 if( aiValues==0
104ad 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
104ae 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
104af 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
104b0 6d 65 6d 63 70 79 28 61 69 56 61 6c 75 65 73 2c memcpy(aiValues,
104b1 20 70 2d 3e 75 2e 61 48 61 73 68 2c 20 73 69 7a p->u.aHash, siz
104b2 65 6f 66 28 70 2d 3e 75 2e 61 48 61 73 68 29 29 eof(p->u.aHash))
104b3 3b 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28 70 ;. memset(p
104b4 2d 3e 75 2e 61 70 53 75 62 2c 20 30 2c 20 73 69 ->u.apSub, 0, si
104b5 7a 65 6f 66 28 70 2d 3e 75 2e 61 70 53 75 62 29 zeof(p->u.apSub)
104b6 29 3b 0a 20 20 20 20 20 20 70 2d 3e 69 44 69 76 );. p->iDiv
104b7 69 73 6f 72 20 3d 20 28 70 2d 3e 69 53 69 7a 65 isor = (p->iSize
104b8 20 2b 20 42 49 54 56 45 43 5f 4e 50 54 52 20 2d + BITVEC_NPTR -
104b9 20 31 29 2f 42 49 54 56 45 43 5f 4e 50 54 52 3b 1)/BITVEC_NPTR;
104ba 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
104bb 74 65 33 42 69 74 76 65 63 53 65 74 28 70 2c 20 te3BitvecSet(p,
104bc 69 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d i);. for(j=
104bd 30 3b 20 6a 3c 42 49 54 56 45 43 5f 4e 49 4e 54 0; j<BITVEC_NINT
104be 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 ; j++){.
104bf 69 66 28 20 61 69 56 61 6c 75 65 73 5b 6a 5d 20 if( aiValues[j]
104c0 29 20 72 63 20 7c 3d 20 73 71 6c 69 74 65 33 42 ) rc |= sqlite3B
104c1 69 74 76 65 63 53 65 74 28 70 2c 20 61 69 56 61 itvecSet(p, aiVa
104c2 6c 75 65 73 5b 6a 5d 29 3b 0a 20 20 20 20 20 20 lues[j]);.
104c3 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 }. sqlite3S
104c4 74 61 63 6b 46 72 65 65 28 30 2c 20 61 69 56 61 tackFree(0, aiVa
104c5 6c 75 65 73 29 3b 0a 20 20 20 20 20 20 72 65 74 lues);. ret
104c6 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 urn rc;. }.
104c7 7d 0a 62 69 74 76 65 63 5f 73 65 74 5f 65 6e 64 }.bitvec_set_end
104c8 3a 0a 20 20 70 2d 3e 6e 53 65 74 2b 2b 3b 0a 20 :. p->nSet++;.
104c9 20 70 2d 3e 75 2e 61 48 61 73 68 5b 68 5d 20 3d p->u.aHash[h] =
104ca 20 69 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c i;. return SQL
104cb 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ITE_OK;.}../*.**
104cc 20 43 6c 65 61 72 20 74 68 65 20 69 2d 74 68 20 Clear the i-th
104cd 62 69 74 2e 0a 2a 2a 0a 2a 2a 20 70 42 75 66 20 bit..**.** pBuf
104ce 6d 75 73 74 20 62 65 20 61 20 70 6f 69 6e 74 65 must be a pointe
104cf 72 20 74 6f 20 61 74 20 6c 65 61 73 74 20 42 49 r to at least BI
104d0 54 56 45 43 5f 53 5a 20 62 79 74 65 73 20 6f 66 TVEC_SZ bytes of
104d1 20 74 65 6d 70 6f 72 61 72 79 20 73 74 6f 72 61 temporary stora
104d2 67 65 0a 2a 2a 20 74 68 61 74 20 42 69 74 76 65 ge.** that Bitve
104d3 63 43 6c 65 61 72 20 63 61 6e 20 75 73 65 20 74 cClear can use t
104d4 6f 20 72 65 62 75 69 6c 74 20 69 74 73 20 68 61 o rebuilt its ha
104d5 73 68 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c sh table..*/.SQL
104d6 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
104d7 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 43 6c sqlite3BitvecCl
104d8 65 61 72 28 42 69 74 76 65 63 20 2a 70 2c 20 75 ear(Bitvec *p, u
104d9 33 32 20 69 2c 20 76 6f 69 64 20 2a 70 42 75 66 32 i, void *pBuf
104da 29 7b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 ){. if( p==0 )
104db 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65 72 74 return;. assert
104dc 28 20 69 3e 30 20 29 3b 0a 20 20 69 2d 2d 3b 0a ( i>0 );. i--;.
104dd 20 20 77 68 69 6c 65 28 20 70 2d 3e 69 44 69 76 while( p->iDiv
104de 69 73 6f 72 20 29 7b 0a 20 20 20 20 75 33 32 20 isor ){. u32
104df 62 69 6e 20 3d 20 69 2f 70 2d 3e 69 44 69 76 69 bin = i/p->iDivi
104e0 73 6f 72 3b 0a 20 20 20 20 69 20 3d 20 69 25 70 sor;. i = i%p
104e1 2d 3e 69 44 69 76 69 73 6f 72 3b 0a 20 20 20 20 ->iDivisor;.
104e2 70 20 3d 20 70 2d 3e 75 2e 61 70 53 75 62 5b 62 p = p->u.apSub[b
104e3 69 6e 5d 3b 0a 20 20 20 20 69 66 20 28 21 70 29 in];. if (!p)
104e4 20 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b {. return;
104e5 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 . }. }. if(
104e6 20 70 2d 3e 69 53 69 7a 65 3c 3d 42 49 54 56 45 p->iSize<=BITVE
104e7 43 5f 4e 42 49 54 20 29 7b 0a 20 20 20 20 70 2d C_NBIT ){. p-
104e8 3e 75 2e 61 42 69 74 6d 61 70 5b 69 2f 42 49 54 >u.aBitmap[i/BIT
104e9 56 45 43 5f 53 5a 45 4c 45 4d 5d 20 26 3d 20 7e VEC_SZELEM] &= ~
104ea 28 31 20 3c 3c 20 28 69 26 28 42 49 54 56 45 43 (1 << (i&(BITVEC
104eb 5f 53 5a 45 4c 45 4d 2d 31 29 29 29 3b 0a 20 20 _SZELEM-1)));.
104ec 7d 65 6c 73 65 7b 0a 20 20 20 20 75 6e 73 69 67 }else{. unsig
104ed 6e 65 64 20 69 6e 74 20 6a 3b 0a 20 20 20 20 75 ned int j;. u
104ee 33 32 20 2a 61 69 56 61 6c 75 65 73 20 3d 20 70 32 *aiValues = p
104ef 42 75 66 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 Buf;. memcpy(
104f0 61 69 56 61 6c 75 65 73 2c 20 70 2d 3e 75 2e 61 aiValues, p->u.a
104f1 48 61 73 68 2c 20 73 69 7a 65 6f 66 28 70 2d 3e Hash, sizeof(p->
104f2 75 2e 61 48 61 73 68 29 29 3b 0a 20 20 20 20 6d u.aHash));. m
104f3 65 6d 73 65 74 28 70 2d 3e 75 2e 61 48 61 73 68 emset(p->u.aHash
104f4 2c 20 30 2c 20 73 69 7a 65 6f 66 28 70 2d 3e 75 , 0, sizeof(p->u
104f5 2e 61 48 61 73 68 29 29 3b 0a 20 20 20 20 70 2d .aHash));. p-
104f6 3e 6e 53 65 74 20 3d 20 30 3b 0a 20 20 20 20 66 >nSet = 0;. f
104f7 6f 72 28 6a 3d 30 3b 20 6a 3c 42 49 54 56 45 43 or(j=0; j<BITVEC
104f8 5f 4e 49 4e 54 3b 20 6a 2b 2b 29 7b 0a 20 20 20 _NINT; j++){.
104f9 20 20 20 69 66 28 20 61 69 56 61 6c 75 65 73 5b if( aiValues[
104fa 6a 5d 20 26 26 20 61 69 56 61 6c 75 65 73 5b 6a j] && aiValues[j
104fb 5d 21 3d 28 69 2b 31 29 20 29 7b 0a 20 20 20 20 ]!=(i+1) ){.
104fc 20 20 20 20 75 33 32 20 68 20 3d 20 42 49 54 56 u32 h = BITV
104fd 45 43 5f 48 41 53 48 28 61 69 56 61 6c 75 65 73 EC_HASH(aiValues
104fe 5b 6a 5d 2d 31 29 3b 0a 20 20 20 20 20 20 20 20 [j]-1);.
104ff 70 2d 3e 6e 53 65 74 2b 2b 3b 0a 20 20 20 20 20 p->nSet++;.
10500 20 20 20 77 68 69 6c 65 28 20 70 2d 3e 75 2e 61 while( p->u.a
10501 48 61 73 68 5b 68 5d 20 29 7b 0a 20 20 20 20 20 Hash[h] ){.
10502 20 20 20 20 20 68 2b 2b 3b 0a 20 20 20 20 20 20 h++;.
10503 20 20 20 20 69 66 28 20 68 3e 3d 42 49 54 56 45 if( h>=BITVE
10504 43 5f 4e 49 4e 54 20 29 20 68 20 3d 20 30 3b 0a C_NINT ) h = 0;.
10505 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
10506 20 20 70 2d 3e 75 2e 61 48 61 73 68 5b 68 5d 20 p->u.aHash[h]
10507 3d 20 61 69 56 61 6c 75 65 73 5b 6a 5d 3b 0a 20 = aiValues[j];.
10508 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d }. }. }
10509 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 73 74 72 6f .}../*.** Destro
1050a 79 20 61 20 62 69 74 6d 61 70 20 6f 62 6a 65 63 y a bitmap objec
1050b 74 2e 20 20 52 65 63 6c 61 69 6d 20 61 6c 6c 20 t. Reclaim all
1050c 6d 65 6d 6f 72 79 20 75 73 65 64 2e 0a 2a 2f 0a memory used..*/.
1050d 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
1050e 6f 69 64 20 73 71 6c 69 74 65 33 42 69 74 76 65 oid sqlite3Bitve
1050f 63 44 65 73 74 72 6f 79 28 42 69 74 76 65 63 20 cDestroy(Bitvec
10510 2a 70 29 7b 0a 20 20 69 66 28 20 70 3d 3d 30 20 *p){. if( p==0
10511 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 ) return;. if(
10512 70 2d 3e 69 44 69 76 69 73 6f 72 20 29 7b 0a 20 p->iDivisor ){.
10513 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 unsigned int
10514 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 i;. for(i=0;
10515 69 3c 42 49 54 56 45 43 5f 4e 50 54 52 3b 20 69 i<BITVEC_NPTR; i
10516 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 ++){. sqlit
10517 65 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 28 e3BitvecDestroy(
10518 70 2d 3e 75 2e 61 70 53 75 62 5b 69 5d 29 3b 0a p->u.apSub[i]);.
10519 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 }. }. sqli
1051a 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 7d 0a 0a te3_free(p);.}..
1051b 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 /*.** Return the
1051c 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 69 53 value of the iS
1051d 69 7a 65 20 70 61 72 61 6d 65 74 65 72 20 73 70 ize parameter sp
1051e 65 63 69 66 69 65 64 20 77 68 65 6e 20 42 69 74 ecified when Bit
1051f 76 65 63 20 2a 70 0a 2a 2a 20 77 61 73 20 63 72 vec *p.** was cr
10520 65 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 eated..*/.SQLITE
10521 5f 50 52 49 56 41 54 45 20 75 33 32 20 73 71 6c _PRIVATE u32 sql
10522 69 74 65 33 42 69 74 76 65 63 53 69 7a 65 28 42 ite3BitvecSize(B
10523 69 74 76 65 63 20 2a 70 29 7b 0a 20 20 72 65 74 itvec *p){. ret
10524 75 72 6e 20 70 2d 3e 69 53 69 7a 65 3b 0a 7d 0a urn p->iSize;.}.
10525 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
10526 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 OMIT_BUILTIN_TES
10527 54 0a 2f 2a 0a 2a 2a 20 4c 65 74 20 56 5b 5d 20 T./*.** Let V[]
10528 62 65 20 61 6e 20 61 72 72 61 79 20 6f 66 20 75 be an array of u
10529 6e 73 69 67 6e 65 64 20 63 68 61 72 61 63 74 65 nsigned characte
1052a 72 73 20 73 75 66 66 69 63 69 65 6e 74 20 74 6f rs sufficient to
1052b 20 68 6f 6c 64 0a 2a 2a 20 75 70 20 74 6f 20 4e hold.** up to N
1052c 20 62 69 74 73 2e 20 20 4c 65 74 20 49 20 62 65 bits. Let I be
1052d 20 61 6e 20 69 6e 74 65 67 65 72 20 62 65 74 77 an integer betw
1052e 65 65 6e 20 30 20 61 6e 64 20 4e 2e 20 20 30 3c een 0 and N. 0<
1052f 3d 49 3c 4e 2e 0a 2a 2a 20 54 68 65 6e 20 74 68 =I<N..** Then th
10530 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 e following macr
10531 6f 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 os can be used t
10532 6f 20 73 65 74 2c 20 63 6c 65 61 72 2c 20 6f 72 o set, clear, or
10533 20 74 65 73 74 0a 2a 2a 20 69 6e 64 69 76 69 64 test.** individ
10534 75 61 6c 20 62 69 74 73 20 77 69 74 68 69 6e 20 ual bits within
10535 56 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 45 V..*/.#define SE
10536 54 42 49 54 28 56 2c 49 29 20 20 20 20 20 20 56 TBIT(V,I) V
10537 5b 49 3e 3e 33 5d 20 7c 3d 20 28 31 3c 3c 28 49 [I>>3] |= (1<<(I
10538 26 37 29 29 0a 23 64 65 66 69 6e 65 20 43 4c 45 &7)).#define CLE
10539 41 52 42 49 54 28 56 2c 49 29 20 20 20 20 56 5b ARBIT(V,I) V[
1053a 49 3e 3e 33 5d 20 26 3d 20 7e 28 31 3c 3c 28 49 I>>3] &= ~(1<<(I
1053b 26 37 29 29 0a 23 64 65 66 69 6e 65 20 54 45 53 &7)).#define TES
1053c 54 42 49 54 28 56 2c 49 29 20 20 20 20 20 28 56 TBIT(V,I) (V
1053d 5b 49 3e 3e 33 5d 26 28 31 3c 3c 28 49 26 37 29 [I>>3]&(1<<(I&7)
1053e 29 29 21 3d 30 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 ))!=0../*.** Thi
1053f 73 20 72 6f 75 74 69 6e 65 20 72 75 6e 73 20 61 s routine runs a
10540 6e 20 65 78 74 65 6e 73 69 76 65 20 74 65 73 74 n extensive test
10541 20 6f 66 20 74 68 65 20 42 69 74 76 65 63 20 63 of the Bitvec c
10542 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 ode..**.** The i
10543 6e 70 75 74 20 69 73 20 61 6e 20 61 72 72 61 79 nput is an array
10544 20 6f 66 20 69 6e 74 65 67 65 72 73 20 74 68 61 of integers tha
10545 74 20 61 63 74 73 20 61 73 20 61 20 70 72 6f 67 t acts as a prog
10546 72 61 6d 0a 2a 2a 20 74 6f 20 74 65 73 74 20 74 ram.** to test t
10547 68 65 20 42 69 74 76 65 63 2e 20 20 54 68 65 20 he Bitvec. The
10548 69 6e 74 65 67 65 72 73 20 61 72 65 20 6f 70 63 integers are opc
10549 6f 64 65 73 20 66 6f 6c 6c 6f 77 65 64 0a 2a 2a odes followed.**
1054a 20 62 79 20 30 2c 20 31 2c 20 6f 72 20 33 20 6f by 0, 1, or 3 o
1054b 70 65 72 61 6e 64 73 2c 20 64 65 70 65 6e 64 69 perands, dependi
1054c 6e 67 20 6f 6e 20 74 68 65 20 6f 70 63 6f 64 65 ng on the opcode
1054d 2e 20 20 41 6e 6f 74 68 65 72 0a 2a 2a 20 6f 70 . Another.** op
1054e 63 6f 64 65 20 66 6f 6c 6c 6f 77 73 20 69 6d 6d code follows imm
1054f 65 64 69 61 74 65 6c 79 20 61 66 74 65 72 20 74 ediately after t
10550 68 65 20 6c 61 73 74 20 6f 70 65 72 61 6e 64 2e he last operand.
10551 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 .**.** There are
10552 20 36 20 6f 70 63 6f 64 65 73 20 6e 75 6d 62 65 6 opcodes numbe
10553 72 65 64 20 66 72 6f 6d 20 30 20 74 68 72 6f 75 red from 0 throu
10554 67 68 20 35 2e 20 20 30 20 69 73 20 74 68 65 0a gh 5. 0 is the.
10555 2a 2a 20 22 68 61 6c 74 22 20 6f 70 63 6f 64 65 ** "halt" opcode
10556 20 61 6e 64 20 63 61 75 73 65 73 20 74 68 65 20 and causes the
10557 74 65 73 74 20 74 6f 20 65 6e 64 2e 0a 2a 2a 0a test to end..**.
10558 2a 2a 20 20 20 20 30 20 20 20 20 20 20 20 20 20 ** 0
10559 20 48 61 6c 74 20 61 6e 64 20 72 65 74 75 72 6e Halt and return
1055a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 the number of e
1055b 72 72 6f 72 73 0a 2a 2a 20 20 20 20 31 20 4e 20 rrors.** 1 N
1055c 53 20 58 20 20 20 20 53 65 74 20 4e 20 62 69 74 S X Set N bit
1055d 73 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 s beginning with
1055e 20 53 20 61 6e 64 20 69 6e 63 72 65 6d 65 6e 74 S and increment
1055f 69 6e 67 20 62 79 20 58 0a 2a 2a 20 20 20 20 32 ing by X.** 2
10560 20 4e 20 53 20 58 20 20 20 20 43 6c 65 61 72 20 N S X Clear
10561 4e 20 62 69 74 73 20 62 65 67 69 6e 6e 69 6e 67 N bits beginning
10562 20 77 69 74 68 20 53 20 61 6e 64 20 69 6e 63 72 with S and incr
10563 65 6d 65 6e 74 69 6e 67 20 62 79 20 58 0a 2a 2a ementing by X.**
10564 20 20 20 20 33 20 4e 20 20 20 20 20 20 20 20 53 3 N S
10565 65 74 20 4e 20 72 61 6e 64 6f 6d 6c 79 20 63 68 et N randomly ch
10566 6f 73 65 6e 20 62 69 74 73 0a 2a 2a 20 20 20 20 osen bits.**
10567 34 20 4e 20 20 20 20 20 20 20 20 43 6c 65 61 72 4 N Clear
10568 20 4e 20 72 61 6e 64 6f 6d 6c 79 20 63 68 6f 73 N randomly chos
10569 65 6e 20 62 69 74 73 0a 2a 2a 20 20 20 20 35 20 en bits.** 5
1056a 4e 20 53 20 58 20 20 20 20 53 65 74 20 4e 20 62 N S X Set N b
1056b 69 74 73 20 66 72 6f 6d 20 53 20 69 6e 63 72 65 its from S incre
1056c 6d 65 6e 74 20 58 20 69 6e 20 61 72 72 61 79 20 ment X in array
1056d 6f 6e 6c 79 2c 20 6e 6f 74 20 69 6e 20 62 69 74 only, not in bit
1056e 76 65 63 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 70 vec.**.** The op
1056f 63 6f 64 65 73 20 31 20 74 68 72 6f 75 67 68 20 codes 1 through
10570 34 20 70 65 72 66 6f 72 6d 20 73 65 74 20 61 6e 4 perform set an
10571 64 20 63 6c 65 61 72 20 6f 70 65 72 61 74 69 6f d clear operatio
10572 6e 73 20 61 72 65 20 70 65 72 66 6f 72 6d 65 64 ns are performed
10573 0a 2a 2a 20 6f 6e 20 62 6f 74 68 20 61 20 42 69 .** on both a Bi
10574 74 76 65 63 20 6f 62 6a 65 63 74 20 61 6e 64 20 tvec object and
10575 6f 6e 20 61 20 6c 69 6e 65 61 72 20 61 72 72 61 on a linear arra
10576 79 20 6f 66 20 62 69 74 73 20 6f 62 74 61 69 6e y of bits obtain
10577 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 2e 0a ed from malloc..
10578 2a 2a 20 4f 70 63 6f 64 65 20 35 20 77 6f 72 6b ** Opcode 5 work
10579 73 20 6f 6e 20 74 68 65 20 6c 69 6e 65 61 72 20 s on the linear
1057a 61 72 72 61 79 20 6f 6e 6c 79 2c 20 6e 6f 74 20 array only, not
1057b 6f 6e 20 74 68 65 20 42 69 74 76 65 63 2e 0a 2a on the Bitvec..*
1057c 2a 20 4f 70 63 6f 64 65 20 35 20 69 73 20 75 73 * Opcode 5 is us
1057d 65 64 20 74 6f 20 64 65 6c 69 62 65 72 61 74 65 ed to deliberate
1057e 6c 79 20 69 6e 64 75 63 65 20 61 20 66 61 75 6c ly induce a faul
1057f 74 20 69 6e 20 6f 72 64 65 72 20 74 6f 0a 2a 2a t in order to.**
10580 20 63 6f 6e 66 69 72 6d 20 74 68 61 74 20 65 72 confirm that er
10581 72 6f 72 20 64 65 74 65 63 74 69 6f 6e 20 77 6f ror detection wo
10582 72 6b 73 2e 0a 2a 2a 0a 2a 2a 20 41 74 20 74 68 rks..**.** At th
10583 65 20 63 6f 6e 63 6c 75 73 69 6f 6e 20 6f 66 20 e conclusion of
10584 74 68 65 20 74 65 73 74 20 74 68 65 20 6c 69 6e the test the lin
10585 65 61 72 20 61 72 72 61 79 20 69 73 20 63 6f 6d ear array is com
10586 70 61 72 65 64 0a 2a 2a 20 61 67 61 69 6e 73 74 pared.** against
10587 20 74 68 65 20 42 69 74 76 65 63 20 6f 62 6a 65 the Bitvec obje
10588 63 74 2e 20 20 49 66 20 74 68 65 72 65 20 61 72 ct. If there ar
10589 65 20 61 6e 79 20 64 69 66 66 65 72 65 6e 63 65 e any difference
1058a 73 2c 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20 69 s,.** an error i
1058b 73 20 72 65 74 75 72 6e 65 64 2e 20 20 49 66 20 s returned. If
1058c 74 68 65 79 20 61 72 65 20 74 68 65 20 73 61 6d they are the sam
1058d 65 2c 20 7a 65 72 6f 20 69 73 20 72 65 74 75 72 e, zero is retur
1058e 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 ned..**.** If a
1058f 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
10590 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 n error occurs,
10591 72 65 74 75 72 6e 20 2d 31 2e 0a 2a 2f 0a 53 51 return -1..*/.SQ
10592 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
10593 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 42 75 sqlite3BitvecBu
10594 69 6c 74 69 6e 54 65 73 74 28 69 6e 74 20 73 7a iltinTest(int sz
10595 2c 20 69 6e 74 20 2a 61 4f 70 29 7b 0a 20 20 42 , int *aOp){. B
10596 69 74 76 65 63 20 2a 70 42 69 74 76 65 63 20 3d itvec *pBitvec =
10597 20 30 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 0;. unsigned c
10598 68 61 72 20 2a 70 56 20 3d 20 30 3b 0a 20 20 69 har *pV = 0;. i
10599 6e 74 20 72 63 20 3d 20 2d 31 3b 0a 20 20 69 6e nt rc = -1;. in
1059a 74 20 69 2c 20 6e 78 2c 20 70 63 2c 20 6f 70 3b t i, nx, pc, op;
1059b 0a 20 20 76 6f 69 64 20 2a 70 54 6d 70 53 70 61 . void *pTmpSpa
1059c 63 65 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 ce;.. /* Alloca
1059d 74 65 20 74 68 65 20 42 69 74 76 65 63 20 74 6f te the Bitvec to
1059e 20 62 65 20 74 65 73 74 65 64 20 61 6e 64 20 61 be tested and a
1059f 20 6c 69 6e 65 61 72 20 61 72 72 61 79 20 6f 66 linear array of
105a0 0a 20 20 2a 2a 20 62 69 74 73 20 74 6f 20 61 63 . ** bits to ac
105a1 74 20 61 73 20 74 68 65 20 72 65 66 65 72 65 6e t as the referen
105a2 63 65 20 2a 2f 0a 20 20 70 42 69 74 76 65 63 20 ce */. pBitvec
105a3 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 43 = sqlite3BitvecC
105a4 72 65 61 74 65 28 20 73 7a 20 29 3b 0a 20 20 70 reate( sz );. p
105a5 56 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c V = sqlite3_mall
105a6 6f 63 28 20 28 73 7a 2b 37 29 2f 38 20 2b 20 31 oc( (sz+7)/8 + 1
105a7 20 29 3b 0a 20 20 70 54 6d 70 53 70 61 63 65 20 );. pTmpSpace
105a8 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 = sqlite3_malloc
105a9 28 42 49 54 56 45 43 5f 53 5a 29 3b 0a 20 20 69 (BITVEC_SZ);. i
105aa 66 28 20 70 42 69 74 76 65 63 3d 3d 30 20 7c 7c f( pBitvec==0 ||
105ab 20 70 56 3d 3d 30 20 7c 7c 20 70 54 6d 70 53 70 pV==0 || pTmpSp
105ac 61 63 65 3d 3d 30 20 20 29 20 67 6f 74 6f 20 62 ace==0 ) goto b
105ad 69 74 76 65 63 5f 65 6e 64 3b 0a 20 20 6d 65 6d itvec_end;. mem
105ae 73 65 74 28 70 56 2c 20 30 2c 20 28 73 7a 2b 37 set(pV, 0, (sz+7
105af 29 2f 38 20 2b 20 31 29 3b 0a 0a 20 20 2f 2a 20 )/8 + 1);.. /*
105b0 4e 55 4c 4c 20 70 42 69 74 76 65 63 20 74 65 73 NULL pBitvec tes
105b1 74 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 42 ts */. sqlite3B
105b2 69 74 76 65 63 53 65 74 28 30 2c 20 31 29 3b 0a itvecSet(0, 1);.
105b3 20 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 43 sqlite3BitvecC
105b4 6c 65 61 72 28 30 2c 20 31 2c 20 70 54 6d 70 53 lear(0, 1, pTmpS
105b5 70 61 63 65 29 3b 0a 0a 20 20 2f 2a 20 52 75 6e pace);.. /* Run
105b6 20 74 68 65 20 70 72 6f 67 72 61 6d 20 2a 2f 0a the program */.
105b7 20 20 70 63 20 3d 20 30 3b 0a 20 20 77 68 69 6c pc = 0;. whil
105b8 65 28 20 28 6f 70 20 3d 20 61 4f 70 5b 70 63 5d e( (op = aOp[pc]
105b9 29 21 3d 30 20 29 7b 0a 20 20 20 20 73 77 69 74 )!=0 ){. swit
105ba 63 68 28 20 6f 70 20 29 7b 0a 20 20 20 20 20 20 ch( op ){.
105bb 63 61 73 65 20 31 3a 0a 20 20 20 20 20 20 63 61 case 1:. ca
105bc 73 65 20 32 3a 0a 20 20 20 20 20 20 63 61 73 65 se 2:. case
105bd 20 35 3a 20 7b 0a 20 20 20 20 20 20 20 20 6e 78 5: {. nx
105be 20 3d 20 34 3b 0a 20 20 20 20 20 20 20 20 69 20 = 4;. i
105bf 3d 20 61 4f 70 5b 70 63 2b 32 5d 20 2d 20 31 3b = aOp[pc+2] - 1;
105c0 0a 20 20 20 20 20 20 20 20 61 4f 70 5b 70 63 2b . aOp[pc+
105c1 32 5d 20 2b 3d 20 61 4f 70 5b 70 63 2b 33 5d 3b 2] += aOp[pc+3];
105c2 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a . break;.
105c3 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 }. ca
105c4 73 65 20 33 3a 0a 20 20 20 20 20 20 63 61 73 65 se 3:. case
105c5 20 34 3a 20 0a 20 20 20 20 20 20 64 65 66 61 75 4: . defau
105c6 6c 74 3a 20 7b 0a 20 20 20 20 20 20 20 20 6e 78 lt: {. nx
105c7 20 3d 20 32 3b 0a 20 20 20 20 20 20 20 20 73 71 = 2;. sq
105c8 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 lite3_randomness
105c9 28 73 69 7a 65 6f 66 28 69 29 2c 20 26 69 29 3b (sizeof(i), &i);
105ca 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a . break;.
105cb 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
105cc 20 20 69 66 28 20 28 2d 2d 61 4f 70 5b 70 63 2b if( (--aOp[pc+
105cd 31 5d 29 20 3e 20 30 20 29 20 6e 78 20 3d 20 30 1]) > 0 ) nx = 0
105ce 3b 0a 20 20 20 20 70 63 20 2b 3d 20 6e 78 3b 0a ;. pc += nx;.
105cf 20 20 20 20 69 20 3d 20 28 69 20 26 20 30 78 37 i = (i & 0x7
105d0 66 66 66 66 66 66 66 29 25 73 7a 3b 0a 20 20 20 fffffff)%sz;.
105d1 20 69 66 28 20 28 6f 70 20 26 20 31 29 21 3d 30 if( (op & 1)!=0
105d2 20 29 7b 0a 20 20 20 20 20 20 53 45 54 42 49 54 ){. SETBIT
105d3 28 70 56 2c 20 28 69 2b 31 29 29 3b 0a 20 20 20 (pV, (i+1));.
105d4 20 20 20 69 66 28 20 6f 70 21 3d 35 20 29 7b 0a if( op!=5 ){.
105d5 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 if( sqli
105d6 74 65 33 42 69 74 76 65 63 53 65 74 28 70 42 69 te3BitvecSet(pBi
105d7 74 76 65 63 2c 20 69 2b 31 29 20 29 20 67 6f 74 tvec, i+1) ) got
105d8 6f 20 62 69 74 76 65 63 5f 65 6e 64 3b 0a 20 20 o bitvec_end;.
105d9 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b }. }else{
105da 0a 20 20 20 20 20 20 43 4c 45 41 52 42 49 54 28 . CLEARBIT(
105db 70 56 2c 20 28 69 2b 31 29 29 3b 0a 20 20 20 20 pV, (i+1));.
105dc 20 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 43 sqlite3BitvecC
105dd 6c 65 61 72 28 70 42 69 74 76 65 63 2c 20 69 2b lear(pBitvec, i+
105de 31 2c 20 70 54 6d 70 53 70 61 63 65 29 3b 0a 20 1, pTmpSpace);.
105df 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 }. }.. /* T
105e0 65 73 74 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 est to make sure
105e1 20 74 68 65 20 6c 69 6e 65 61 72 20 61 72 72 61 the linear arra
105e2 79 20 65 78 61 63 74 6c 79 20 6d 61 74 63 68 65 y exactly matche
105e3 73 20 74 68 65 0a 20 20 2a 2a 20 42 69 74 76 65 s the. ** Bitve
105e4 63 20 6f 62 6a 65 63 74 2e 20 20 53 74 61 72 74 c object. Start
105e5 20 77 69 74 68 20 74 68 65 20 61 73 73 75 6d 70 with the assump
105e6 74 69 6f 6e 20 74 68 61 74 20 74 68 65 79 20 64 tion that they d
105e7 6f 0a 20 20 2a 2a 20 6d 61 74 63 68 20 28 72 63 o. ** match (rc
105e8 3d 3d 30 29 2e 20 20 43 68 61 6e 67 65 20 72 63 ==0). Change rc
105e9 20 74 6f 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20 to non-zero if
105ea 61 20 64 69 73 63 72 65 70 61 6e 63 79 0a 20 20 a discrepancy.
105eb 2a 2a 20 69 73 20 66 6f 75 6e 64 2e 0a 20 20 2a ** is found.. *
105ec 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 /. rc = sqlite3
105ed 42 69 74 76 65 63 54 65 73 74 28 30 2c 30 29 20 BitvecTest(0,0)
105ee 2b 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 54 + sqlite3BitvecT
105ef 65 73 74 28 70 42 69 74 76 65 63 2c 20 73 7a 2b est(pBitvec, sz+
105f0 31 29 0a 20 20 20 20 20 20 20 20 20 20 2b 20 73 1). + s
105f1 71 6c 69 74 65 33 42 69 74 76 65 63 54 65 73 74 qlite3BitvecTest
105f2 28 70 42 69 74 76 65 63 2c 20 30 29 0a 20 20 20 (pBitvec, 0).
105f3 20 20 20 20 20 20 20 2b 20 28 73 71 6c 69 74 65 + (sqlite
105f4 33 42 69 74 76 65 63 53 69 7a 65 28 70 42 69 74 3BitvecSize(pBit
105f5 76 65 63 29 20 2d 20 73 7a 29 3b 0a 20 20 66 6f vec) - sz);. fo
105f6 72 28 69 3d 31 3b 20 69 3c 3d 73 7a 3b 20 69 2b r(i=1; i<=sz; i+
105f7 2b 29 7b 0a 20 20 20 20 69 66 28 20 20 28 54 45 +){. if( (TE
105f8 53 54 42 49 54 28 70 56 2c 69 29 29 21 3d 73 71 STBIT(pV,i))!=sq
105f9 6c 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28 lite3BitvecTest(
105fa 70 42 69 74 76 65 63 2c 69 29 20 29 7b 0a 20 20 pBitvec,i) ){.
105fb 20 20 20 20 72 63 20 3d 20 69 3b 0a 20 20 20 20 rc = i;.
105fc 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
105fd 20 7d 0a 0a 20 20 2f 2a 20 46 72 65 65 20 61 6c }.. /* Free al
105fe 6c 6f 63 61 74 65 64 20 73 74 72 75 63 74 75 72 located structur
105ff 65 20 2a 2f 0a 62 69 74 76 65 63 5f 65 6e 64 3a e */.bitvec_end:
10600 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 . sqlite3_free(
10601 70 54 6d 70 53 70 61 63 65 29 3b 0a 20 20 73 71 pTmpSpace);. sq
10602 6c 69 74 65 33 5f 66 72 65 65 28 70 56 29 3b 0a lite3_free(pV);.
10603 20 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 44 sqlite3BitvecD
10604 65 73 74 72 6f 79 28 70 42 69 74 76 65 63 29 3b estroy(pBitvec);
10605 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
10606 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
10607 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 _OMIT_BUILTIN_TE
10608 53 54 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a ST */../********
10609 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 62 69 ****** End of bi
1060a 74 76 65 63 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a tvec.c *********
1060b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1060c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1060d 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
1060e 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c ****** Begin fil
1060f 65 20 70 63 61 63 68 65 2e 63 20 2a 2a 2a 2a 2a e pcache.c *****
10610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10611 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10612 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 *****/./*.** 200
10613 38 20 41 75 67 75 73 74 20 30 35 0a 2a 2a 0a 2a 8 August 05.**.*
10614 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 * The author dis
10615 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 claims copyright
10616 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 to this source
10617 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 code. In place
10618 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f of.** a legal no
10619 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 tice, here is a
1061a 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 blessing:.**.**
1061b 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f May you do go
1061c 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e od and not evil.
1061d 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 .** May you f
1061e 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 ind forgiveness
1061f 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 for yourself and
10620 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e forgive others.
10621 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 .** May you s
10622 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 hare freely, nev
10623 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 er taking more t
10624 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a han you give..**
10625 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
10626 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10627 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10628 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10629 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 **********.** Th
1062a 69 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e is file implemen
1062b 74 73 20 74 68 61 74 20 70 61 67 65 20 63 61 63 ts that page cac
1062c 68 65 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 he..**.** @(#) $
1062d 49 64 3a 20 70 63 61 63 68 65 2e 63 2c 76 20 31 Id: pcache.c,v 1
1062e 2e 34 37 20 32 30 30 39 2f 30 37 2f 32 35 20 31 .47 2009/07/25 1
1062f 31 3a 34 36 3a 34 39 20 64 61 6e 69 65 6c 6b 31 1:46:49 danielk1
10630 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 977 Exp $.*/../*
10631 0a 2a 2a 20 41 20 63 6f 6d 70 6c 65 74 65 20 70 .** A complete p
10632 61 67 65 20 63 61 63 68 65 20 69 73 20 61 6e 20 age cache is an
10633 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 instance of this
10634 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 structure..*/.s
10635 74 72 75 63 74 20 50 43 61 63 68 65 20 7b 0a 20 truct PCache {.
10636 20 50 67 48 64 72 20 2a 70 44 69 72 74 79 2c 20 PgHdr *pDirty,
10637 2a 70 44 69 72 74 79 54 61 69 6c 3b 20 20 20 20 *pDirtyTail;
10638 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 /* List of
10639 64 69 72 74 79 20 70 61 67 65 73 20 69 6e 20 4c dirty pages in L
1063a 52 55 20 6f 72 64 65 72 20 2a 2f 0a 20 20 50 67 RU order */. Pg
1063b 48 64 72 20 2a 70 53 79 6e 63 65 64 3b 20 20 20 Hdr *pSynced;
1063c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1063d 20 20 2f 2a 20 4c 61 73 74 20 73 79 6e 63 65 64 /* Last synced
1063e 20 70 61 67 65 20 69 6e 20 64 69 72 74 79 20 70 page in dirty p
1063f 61 67 65 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e age list */. in
10640 74 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20 t nRef;
10641 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10642 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 /* Number of r
10643 65 66 65 72 65 6e 63 65 64 20 70 61 67 65 73 20 eferenced pages
10644 2a 2f 0a 20 20 69 6e 74 20 6e 4d 61 78 3b 20 20 */. int nMax;
10645 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10646 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 66 /* Conf
10647 69 67 75 72 65 64 20 63 61 63 68 65 20 73 69 7a igured cache siz
10648 65 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 50 61 67 e */. int szPag
10649 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e;
1064a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 /* Si
1064b 7a 65 20 6f 66 20 65 76 65 72 79 20 70 61 67 65 ze of every page
1064c 20 69 6e 20 74 68 69 73 20 63 61 63 68 65 20 2a in this cache *
1064d 2f 0a 20 20 69 6e 74 20 73 7a 45 78 74 72 61 3b /. int szExtra;
1064e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1064f 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 /* Size
10650 6f 66 20 65 78 74 72 61 20 73 70 61 63 65 20 66 of extra space f
10651 6f 72 20 65 61 63 68 20 70 61 67 65 20 2a 2f 0a or each page */.
10652 20 20 69 6e 74 20 62 50 75 72 67 65 61 62 6c 65 int bPurgeable
10653 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
10654 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
10655 20 70 61 67 65 73 20 61 72 65 20 6f 6e 20 62 61 pages are on ba
10656 63 6b 69 6e 67 20 73 74 6f 72 65 20 2a 2f 0a 20 cking store */.
10657 20 69 6e 74 20 28 2a 78 53 74 72 65 73 73 29 28 int (*xStress)(
10658 76 6f 69 64 2a 2c 50 67 48 64 72 2a 29 3b 20 20 void*,PgHdr*);
10659 20 20 20 20 20 2f 2a 20 43 61 6c 6c 20 74 6f 20 /* Call to
1065a 74 72 79 20 6d 61 6b 65 20 61 20 70 61 67 65 20 try make a page
1065b 63 6c 65 61 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 clean */. void
1065c 2a 70 53 74 72 65 73 73 3b 20 20 20 20 20 20 20 *pStress;
1065d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1065e 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 78 53 * Argument to xS
1065f 74 72 65 73 73 20 2a 2f 0a 20 20 73 71 6c 69 74 tress */. sqlit
10660 65 33 5f 70 63 61 63 68 65 20 2a 70 43 61 63 68 e3_pcache *pCach
10661 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f e; /
10662 2a 20 50 6c 75 67 67 61 62 6c 65 20 63 61 63 68 * Pluggable cach
10663 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 50 67 e module */. Pg
10664 48 64 72 20 2a 70 50 61 67 65 31 3b 20 20 20 20 Hdr *pPage1;
10665 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10666 20 20 2f 2a 20 52 65 66 65 72 65 6e 63 65 20 74 /* Reference t
10667 6f 20 70 61 67 65 20 31 20 2a 2f 0a 7d 3b 0a 0a o page 1 */.};..
10668 2f 2a 0a 2a 2a 20 53 6f 6d 65 20 6f 66 20 74 68 /*.** Some of th
10669 65 20 61 73 73 65 72 74 28 29 20 6d 61 63 72 6f e assert() macro
1066a 73 20 69 6e 20 74 68 69 73 20 63 6f 64 65 20 61 s in this code a
1066b 72 65 20 74 6f 6f 20 65 78 70 65 6e 73 69 76 65 re too expensive
1066c 20 74 6f 20 72 75 6e 0a 2a 2a 20 65 76 65 6e 20 to run.** even
1066d 64 75 72 69 6e 67 20 6e 6f 72 6d 61 6c 20 64 65 during normal de
1066e 62 75 67 67 69 6e 67 2e 20 20 55 73 65 20 74 68 bugging. Use th
1066f 65 6d 20 6f 6e 6c 79 20 72 61 72 65 6c 79 20 6f em only rarely o
10670 6e 20 6c 6f 6e 67 2d 72 75 6e 6e 69 6e 67 0a 2a n long-running.*
10671 2a 20 74 65 73 74 73 2e 20 20 45 6e 61 62 6c 65 * tests. Enable
10672 20 74 68 65 20 65 78 70 65 6e 73 69 76 65 20 61 the expensive a
10673 73 73 65 72 74 73 20 75 73 69 6e 67 20 74 68 65 sserts using the
10674 0a 2a 2a 20 2d 44 53 51 4c 49 54 45 5f 45 4e 41 .** -DSQLITE_ENA
10675 42 4c 45 5f 45 58 50 45 4e 53 49 56 45 5f 41 53 BLE_EXPENSIVE_AS
10676 53 45 52 54 3d 31 20 63 6f 6d 70 69 6c 65 2d 74 SERT=1 compile-t
10677 69 6d 65 20 6f 70 74 69 6f 6e 2e 0a 2a 2f 0a 23 ime option..*/.#
10678 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 ifdef SQLITE_ENA
10679 42 4c 45 5f 45 58 50 45 4e 53 49 56 45 5f 41 53 BLE_EXPENSIVE_AS
1067a 53 45 52 54 0a 23 20 64 65 66 69 6e 65 20 65 78 SERT.# define ex
1067b 70 65 6e 73 69 76 65 5f 61 73 73 65 72 74 28 58 pensive_assert(X
1067c 29 20 20 61 73 73 65 72 74 28 58 29 0a 23 65 6c ) assert(X).#el
1067d 73 65 0a 23 20 64 65 66 69 6e 65 20 65 78 70 65 se.# define expe
1067e 6e 73 69 76 65 5f 61 73 73 65 72 74 28 58 29 0a nsive_assert(X).
1067f 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a #endif../*******
10680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10681 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 4c 69 6e 6b *********** Link
10682 65 64 20 4c 69 73 74 20 4d 61 6e 61 67 65 6d 65 ed List Manageme
10683 6e 74 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a nt *************
10684 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 23 69 66 20 21 64 *******/..#if !d
10685 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 20 26 efined(NDEBUG) &
10686 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 & defined(SQLITE
10687 5f 45 4e 41 42 4c 45 5f 45 58 50 45 4e 53 49 56 _ENABLE_EXPENSIV
10688 45 5f 41 53 53 45 52 54 29 0a 2f 2a 0a 2a 2a 20 E_ASSERT)./*.**
10689 43 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 70 Check that the p
1068a 43 61 63 68 65 2d 3e 70 53 79 6e 63 65 64 20 76 Cache->pSynced v
1068b 61 72 69 61 62 6c 65 20 69 73 20 73 65 74 20 63 ariable is set c
1068c 6f 72 72 65 63 74 6c 79 2e 20 49 66 20 69 74 0a orrectly. If it.
1068d 2a 2a 20 69 73 20 6e 6f 74 2c 20 65 69 74 68 65 ** is not, eithe
1068e 72 20 66 61 69 6c 20 61 6e 20 61 73 73 65 72 74 r fail an assert
1068f 20 6f 72 20 72 65 74 75 72 6e 20 7a 65 72 6f 2e or return zero.
10690 20 4f 74 68 65 72 77 69 73 65 2c 20 72 65 74 75 Otherwise, retu
10691 72 6e 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 rn.** non-zero.
10692 54 68 69 73 20 69 73 20 6f 6e 6c 79 20 75 73 65 This is only use
10693 64 20 69 6e 20 64 65 62 75 67 67 69 6e 67 20 62 d in debugging b
10694 75 69 6c 64 73 2c 20 61 73 20 66 6f 6c 6c 6f 77 uilds, as follow
10695 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 65 78 70 65 6e s:.**.** expen
10696 73 69 76 65 5f 61 73 73 65 72 74 28 20 70 63 61 sive_assert( pca
10697 63 68 65 43 68 65 63 6b 53 79 6e 63 65 64 28 70 cheCheckSynced(p
10698 43 61 63 68 65 29 20 29 3b 0a 2a 2f 0a 73 74 61 Cache) );.*/.sta
10699 74 69 63 20 69 6e 74 20 70 63 61 63 68 65 43 68 tic int pcacheCh
1069a 65 63 6b 53 79 6e 63 65 64 28 50 43 61 63 68 65 eckSynced(PCache
1069b 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 50 67 48 *pCache){. PgH
1069c 64 72 20 2a 70 3b 0a 20 20 66 6f 72 28 70 3d 70 dr *p;. for(p=p
1069d 43 61 63 68 65 2d 3e 70 44 69 72 74 79 54 61 69 Cache->pDirtyTai
1069e 6c 3b 20 70 21 3d 70 43 61 63 68 65 2d 3e 70 53 l; p!=pCache->pS
1069f 79 6e 63 65 64 3b 20 70 3d 70 2d 3e 70 44 69 72 ynced; p=p->pDir
106a0 74 79 50 72 65 76 29 7b 0a 20 20 20 20 61 73 73 tyPrev){. ass
106a1 65 72 74 28 20 70 2d 3e 6e 52 65 66 20 7c 7c 20 ert( p->nRef ||
106a2 28 70 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f (p->flags&PGHDR_
106a3 4e 45 45 44 5f 53 59 4e 43 29 20 29 3b 0a 20 20 NEED_SYNC) );.
106a4 7d 0a 20 20 72 65 74 75 72 6e 20 28 70 3d 3d 30 }. return (p==0
106a5 20 7c 7c 20 70 2d 3e 6e 52 65 66 20 7c 7c 20 28 || p->nRef || (
106a6 70 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e p->flags&PGHDR_N
106a7 45 45 44 5f 53 59 4e 43 29 3d 3d 30 29 3b 0a 7d EED_SYNC)==0);.}
106a8 0a 23 65 6e 64 69 66 20 2f 2a 20 21 4e 44 45 42 .#endif /* !NDEB
106a9 55 47 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 UG && SQLITE_ENA
106aa 42 4c 45 5f 45 58 50 45 4e 53 49 56 45 5f 41 53 BLE_EXPENSIVE_AS
106ab 53 45 52 54 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 SERT */../*.** R
106ac 65 6d 6f 76 65 20 70 61 67 65 20 70 50 61 67 65 emove page pPage
106ad 20 66 72 6f 6d 20 74 68 65 20 6c 69 73 74 20 6f from the list o
106ae 66 20 64 69 72 74 79 20 70 61 67 65 73 2e 0a 2a f dirty pages..*
106af 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 /.static void pc
106b0 61 63 68 65 52 65 6d 6f 76 65 46 72 6f 6d 44 69 acheRemoveFromDi
106b1 72 74 79 4c 69 73 74 28 50 67 48 64 72 20 2a 70 rtyList(PgHdr *p
106b2 50 61 67 65 29 7b 0a 20 20 50 43 61 63 68 65 20 Page){. PCache
106b3 2a 70 20 3d 20 70 50 61 67 65 2d 3e 70 43 61 63 *p = pPage->pCac
106b4 68 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 he;.. assert( p
106b5 50 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 Page->pDirtyNext
106b6 20 7c 7c 20 70 50 61 67 65 3d 3d 70 2d 3e 70 44 || pPage==p->pD
106b7 69 72 74 79 54 61 69 6c 20 29 3b 0a 20 20 61 73 irtyTail );. as
106b8 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 44 69 sert( pPage->pDi
106b9 72 74 79 50 72 65 76 20 7c 7c 20 70 50 61 67 65 rtyPrev || pPage
106ba 3d 3d 70 2d 3e 70 44 69 72 74 79 20 29 3b 0a 0a ==p->pDirty );..
106bb 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20 /* Update the
106bc 50 43 61 63 68 65 31 2e 70 53 79 6e 63 65 64 20 PCache1.pSynced
106bd 76 61 72 69 61 62 6c 65 20 69 66 20 6e 65 63 65 variable if nece
106be 73 73 61 72 79 2e 20 2a 2f 0a 20 20 69 66 28 20 ssary. */. if(
106bf 70 2d 3e 70 53 79 6e 63 65 64 3d 3d 70 50 61 67 p->pSynced==pPag
106c0 65 20 29 7b 0a 20 20 20 20 50 67 48 64 72 20 2a e ){. PgHdr *
106c1 70 53 79 6e 63 65 64 20 3d 20 70 50 61 67 65 2d pSynced = pPage-
106c2 3e 70 44 69 72 74 79 50 72 65 76 3b 0a 20 20 20 >pDirtyPrev;.
106c3 20 77 68 69 6c 65 28 20 70 53 79 6e 63 65 64 20 while( pSynced
106c4 26 26 20 28 70 53 79 6e 63 65 64 2d 3e 66 6c 61 && (pSynced->fla
106c5 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 gs&PGHDR_NEED_SY
106c6 4e 43 29 20 29 7b 0a 20 20 20 20 20 20 70 53 79 NC) ){. pSy
106c7 6e 63 65 64 20 3d 20 70 53 79 6e 63 65 64 2d 3e nced = pSynced->
106c8 70 44 69 72 74 79 50 72 65 76 3b 0a 20 20 20 20 pDirtyPrev;.
106c9 7d 0a 20 20 20 20 70 2d 3e 70 53 79 6e 63 65 64 }. p->pSynced
106ca 20 3d 20 70 53 79 6e 63 65 64 3b 0a 20 20 7d 0a = pSynced;. }.
106cb 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e 70 44 . if( pPage->pD
106cc 69 72 74 79 4e 65 78 74 20 29 7b 0a 20 20 20 20 irtyNext ){.
106cd 70 50 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 pPage->pDirtyNex
106ce 74 2d 3e 70 44 69 72 74 79 50 72 65 76 20 3d 20 t->pDirtyPrev =
106cf 70 50 61 67 65 2d 3e 70 44 69 72 74 79 50 72 65 pPage->pDirtyPre
106d0 76 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 v;. }else{.
106d1 61 73 73 65 72 74 28 20 70 50 61 67 65 3d 3d 70 assert( pPage==p
106d2 2d 3e 70 44 69 72 74 79 54 61 69 6c 20 29 3b 0a ->pDirtyTail );.
106d3 20 20 20 20 70 2d 3e 70 44 69 72 74 79 54 61 69 p->pDirtyTai
106d4 6c 20 3d 20 70 50 61 67 65 2d 3e 70 44 69 72 74 l = pPage->pDirt
106d5 79 50 72 65 76 3b 0a 20 20 7d 0a 20 20 69 66 28 yPrev;. }. if(
106d6 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 50 72 pPage->pDirtyPr
106d7 65 76 20 29 7b 0a 20 20 20 20 70 50 61 67 65 2d ev ){. pPage-
106d8 3e 70 44 69 72 74 79 50 72 65 76 2d 3e 70 44 69 >pDirtyPrev->pDi
106d9 72 74 79 4e 65 78 74 20 3d 20 70 50 61 67 65 2d rtyNext = pPage-
106da 3e 70 44 69 72 74 79 4e 65 78 74 3b 0a 20 20 7d >pDirtyNext;. }
106db 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 else{. assert
106dc 28 20 70 50 61 67 65 3d 3d 70 2d 3e 70 44 69 72 ( pPage==p->pDir
106dd 74 79 20 29 3b 0a 20 20 20 20 70 2d 3e 70 44 69 ty );. p->pDi
106de 72 74 79 20 3d 20 70 50 61 67 65 2d 3e 70 44 69 rty = pPage->pDi
106df 72 74 79 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 70 rtyNext;. }. p
106e0 50 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 Page->pDirtyNext
106e1 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 2d 3e 70 = 0;. pPage->p
106e2 44 69 72 74 79 50 72 65 76 20 3d 20 30 3b 0a 0a DirtyPrev = 0;..
106e3 20 20 65 78 70 65 6e 73 69 76 65 5f 61 73 73 65 expensive_asse
106e4 72 74 28 20 70 63 61 63 68 65 43 68 65 63 6b 53 rt( pcacheCheckS
106e5 79 6e 63 65 64 28 70 29 20 29 3b 0a 7d 0a 0a 2f ynced(p) );.}../
106e6 2a 0a 2a 2a 20 41 64 64 20 70 61 67 65 20 70 50 *.** Add page pP
106e7 61 67 65 20 74 6f 20 74 68 65 20 68 65 61 64 20 age to the head
106e8 6f 66 20 74 68 65 20 64 69 72 74 79 20 6c 69 73 of the dirty lis
106e9 74 20 28 50 43 61 63 68 65 31 2e 70 44 69 72 74 t (PCache1.pDirt
106ea 79 20 69 73 20 73 65 74 20 74 6f 0a 2a 2a 20 70 y is set to.** p
106eb 50 61 67 65 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 Page)..*/.static
106ec 20 76 6f 69 64 20 70 63 61 63 68 65 41 64 64 54 void pcacheAddT
106ed 6f 44 69 72 74 79 4c 69 73 74 28 50 67 48 64 72 oDirtyList(PgHdr
106ee 20 2a 70 50 61 67 65 29 7b 0a 20 20 50 43 61 63 *pPage){. PCac
106ef 68 65 20 2a 70 20 3d 20 70 50 61 67 65 2d 3e 70 he *p = pPage->p
106f0 43 61 63 68 65 3b 0a 0a 20 20 61 73 73 65 72 74 Cache;.. assert
106f1 28 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 4e ( pPage->pDirtyN
106f2 65 78 74 3d 3d 30 20 26 26 20 70 50 61 67 65 2d ext==0 && pPage-
106f3 3e 70 44 69 72 74 79 50 72 65 76 3d 3d 30 20 26 >pDirtyPrev==0 &
106f4 26 20 70 2d 3e 70 44 69 72 74 79 21 3d 70 50 61 & p->pDirty!=pPa
106f5 67 65 20 29 3b 0a 0a 20 20 70 50 61 67 65 2d 3e ge );.. pPage->
106f6 70 44 69 72 74 79 4e 65 78 74 20 3d 20 70 2d 3e pDirtyNext = p->
106f7 70 44 69 72 74 79 3b 0a 20 20 69 66 28 20 70 50 pDirty;. if( pP
106f8 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 20 age->pDirtyNext
106f9 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 ){. assert( p
106fa 50 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 Page->pDirtyNext
106fb 2d 3e 70 44 69 72 74 79 50 72 65 76 3d 3d 30 20 ->pDirtyPrev==0
106fc 29 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 44 );. pPage->pD
106fd 69 72 74 79 4e 65 78 74 2d 3e 70 44 69 72 74 79 irtyNext->pDirty
106fe 50 72 65 76 20 3d 20 70 50 61 67 65 3b 0a 20 20 Prev = pPage;.
106ff 7d 0a 20 20 70 2d 3e 70 44 69 72 74 79 20 3d 20 }. p->pDirty =
10700 70 50 61 67 65 3b 0a 20 20 69 66 28 20 21 70 2d pPage;. if( !p-
10701 3e 70 44 69 72 74 79 54 61 69 6c 20 29 7b 0a 20 >pDirtyTail ){.
10702 20 20 20 70 2d 3e 70 44 69 72 74 79 54 61 69 6c p->pDirtyTail
10703 20 3d 20 70 50 61 67 65 3b 0a 20 20 7d 0a 20 20 = pPage;. }.
10704 69 66 28 20 21 70 2d 3e 70 53 79 6e 63 65 64 20 if( !p->pSynced
10705 26 26 20 30 3d 3d 28 70 50 61 67 65 2d 3e 66 6c && 0==(pPage->fl
10706 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 ags&PGHDR_NEED_S
10707 59 4e 43 29 20 29 7b 0a 20 20 20 20 70 2d 3e 70 YNC) ){. p->p
10708 53 79 6e 63 65 64 20 3d 20 70 50 61 67 65 3b 0a Synced = pPage;.
10709 20 20 7d 0a 20 20 65 78 70 65 6e 73 69 76 65 5f }. expensive_
1070a 61 73 73 65 72 74 28 20 70 63 61 63 68 65 43 68 assert( pcacheCh
1070b 65 63 6b 53 79 6e 63 65 64 28 70 29 20 29 3b 0a eckSynced(p) );.
1070c 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 61 70 70 65 72 }../*.** Wrapper
1070d 20 61 72 6f 75 6e 64 20 74 68 65 20 70 6c 75 67 around the plug
1070e 67 61 62 6c 65 20 63 61 63 68 65 73 20 78 55 6e gable caches xUn
1070f 70 69 6e 20 6d 65 74 68 6f 64 2e 20 49 66 20 74 pin method. If t
10710 68 65 20 63 61 63 68 65 20 69 73 0a 2a 2a 20 62 he cache is.** b
10711 65 69 6e 67 20 75 73 65 64 20 66 6f 72 20 61 6e eing used for an
10712 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 in-memory datab
10713 61 73 65 2c 20 74 68 69 73 20 66 75 6e 63 74 69 ase, this functi
10714 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a on is a no-op..*
10715 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 /.static void pc
10716 61 63 68 65 55 6e 70 69 6e 28 50 67 48 64 72 20 acheUnpin(PgHdr
10717 2a 70 29 7b 0a 20 20 50 43 61 63 68 65 20 2a 70 *p){. PCache *p
10718 43 61 63 68 65 20 3d 20 70 2d 3e 70 43 61 63 68 Cache = p->pCach
10719 65 3b 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d e;. if( pCache-
1071a 3e 62 50 75 72 67 65 61 62 6c 65 20 29 7b 0a 20 >bPurgeable ){.
1071b 20 20 20 69 66 28 20 70 2d 3e 70 67 6e 6f 3d 3d if( p->pgno==
1071c 31 20 29 7b 0a 20 20 20 20 20 20 70 43 61 63 68 1 ){. pCach
1071d 65 2d 3e 70 50 61 67 65 31 20 3d 20 30 3b 0a 20 e->pPage1 = 0;.
1071e 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 }. sqlite3
1071f 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 GlobalConfig.pca
10720 63 68 65 2e 78 55 6e 70 69 6e 28 70 43 61 63 68 che.xUnpin(pCach
10721 65 2d 3e 70 43 61 63 68 65 2c 20 70 2c 20 30 29 e->pCache, p, 0)
10722 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a ;. }.}../******
10723 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10724 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10725 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 47 65 ************* Ge
10726 6e 65 72 61 6c 20 49 6e 74 65 72 66 61 63 65 73 neral Interfaces
10727 20 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 49 6e ******.**.** In
10728 69 74 69 61 6c 69 7a 65 20 61 6e 64 20 73 68 75 itialize and shu
10729 74 64 6f 77 6e 20 74 68 65 20 70 61 67 65 20 63 tdown the page c
1072a 61 63 68 65 20 73 75 62 73 79 73 74 65 6d 2e 20 ache subsystem.
1072b 4e 65 69 74 68 65 72 20 6f 66 20 74 68 65 73 65 Neither of these
1072c 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 61 .** functions a
1072d 72 65 20 74 68 72 65 61 64 73 61 66 65 2e 0a 2a re threadsafe..*
1072e 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1072f 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 int sqlite3Pcac
10730 68 65 49 6e 69 74 69 61 6c 69 7a 65 28 76 6f 69 heInitialize(voi
10731 64 29 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 d){. if( sqlite
10732 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 3GlobalConfig.pc
10733 61 63 68 65 2e 78 49 6e 69 74 3d 3d 30 20 29 7b ache.xInit==0 ){
10734 0a 20 20 20 20 73 71 6c 69 74 65 33 50 43 61 63 . sqlite3PCac
10735 68 65 53 65 74 44 65 66 61 75 6c 74 28 29 3b 0a heSetDefault();.
10736 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 73 71 6c }. return sql
10737 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
10738 2e 70 63 61 63 68 65 2e 78 49 6e 69 74 28 73 71 .pcache.xInit(sq
10739 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
1073a 67 2e 70 63 61 63 68 65 2e 70 41 72 67 29 3b 0a g.pcache.pArg);.
1073b 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 }.SQLITE_PRIVATE
1073c 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 void sqlite3Pca
1073d 63 68 65 53 68 75 74 64 6f 77 6e 28 76 6f 69 64 cheShutdown(void
1073e 29 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 ){. if( sqlite3
1073f 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 GlobalConfig.pca
10740 63 68 65 2e 78 53 68 75 74 64 6f 77 6e 20 29 7b che.xShutdown ){
10741 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 . sqlite3Glob
10742 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e alConfig.pcache.
10743 78 53 68 75 74 64 6f 77 6e 28 73 71 6c 69 74 65 xShutdown(sqlite
10744 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 3GlobalConfig.pc
10745 61 63 68 65 2e 70 41 72 67 29 3b 0a 20 20 7d 0a ache.pArg);. }.
10746 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
10747 74 68 65 20 73 69 7a 65 20 69 6e 20 62 79 74 65 the size in byte
10748 73 20 6f 66 20 61 20 50 43 61 63 68 65 20 6f 62 s of a PCache ob
10749 6a 65 63 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ject..*/.SQLITE_
1074a 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
1074b 74 65 33 50 63 61 63 68 65 53 69 7a 65 28 76 6f te3PcacheSize(vo
1074c 69 64 29 7b 20 72 65 74 75 72 6e 20 73 69 7a 65 id){ return size
1074d 6f 66 28 50 43 61 63 68 65 29 3b 20 7d 0a 0a 2f of(PCache); }../
1074e 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 *.** Create a ne
1074f 77 20 50 43 61 63 68 65 20 6f 62 6a 65 63 74 2e w PCache object.
10750 20 53 74 6f 72 61 67 65 20 73 70 61 63 65 20 74 Storage space t
10751 6f 20 68 6f 6c 64 20 74 68 65 20 6f 62 6a 65 63 o hold the objec
10752 74 0a 2a 2a 20 68 61 73 20 61 6c 72 65 61 64 79 t.** has already
10753 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 been allocated
10754 61 6e 64 20 69 73 20 70 61 73 73 65 64 20 69 6e and is passed in
10755 20 61 73 20 74 68 65 20 70 20 70 6f 69 6e 74 65 as the p pointe
10756 72 2e 20 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 65 r. .** The calle
10757 72 20 64 69 73 63 6f 76 65 72 73 20 68 6f 77 20 r discovers how
10758 6d 75 63 68 20 73 70 61 63 65 20 6e 65 65 64 73 much space needs
10759 20 74 6f 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 to be allocated
1075a 20 62 79 20 0a 2a 2a 20 63 61 6c 6c 69 6e 67 20 by .** calling
1075b 73 71 6c 69 74 65 33 50 63 61 63 68 65 53 69 7a sqlite3PcacheSiz
1075c 65 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 e()..*/.SQLITE_P
1075d 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
1075e 74 65 33 50 63 61 63 68 65 4f 70 65 6e 28 0a 20 te3PcacheOpen(.
1075f 20 69 6e 74 20 73 7a 50 61 67 65 2c 20 20 20 20 int szPage,
10760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
10761 20 53 69 7a 65 20 6f 66 20 65 76 65 72 79 20 70 Size of every p
10762 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 45 age */. int szE
10763 78 74 72 61 2c 20 20 20 20 20 20 20 20 20 20 20 xtra,
10764 20 20 20 20 20 20 2f 2a 20 45 78 74 72 61 20 73 /* Extra s
10765 70 61 63 65 20 61 73 73 6f 63 69 61 74 65 64 20 pace associated
10766 77 69 74 68 20 65 61 63 68 20 70 61 67 65 20 2a with each page *
10767 2f 0a 20 20 69 6e 74 20 62 50 75 72 67 65 61 62 /. int bPurgeab
10768 6c 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 le,
10769 20 2f 2a 20 54 72 75 65 20 69 66 20 70 61 67 65 /* True if page
1076a 73 20 61 72 65 20 6f 6e 20 62 61 63 6b 69 6e 67 s are on backing
1076b 20 73 74 6f 72 65 20 2a 2f 0a 20 20 69 6e 74 20 store */. int
1076c 28 2a 78 53 74 72 65 73 73 29 28 76 6f 69 64 2a (*xStress)(void*
1076d 2c 50 67 48 64 72 2a 29 2c 2f 2a 20 43 61 6c 6c ,PgHdr*),/* Call
1076e 20 74 6f 20 74 72 79 20 74 6f 20 6d 61 6b 65 20 to try to make
1076f 70 61 67 65 73 20 63 6c 65 61 6e 20 2a 2f 0a 20 pages clean */.
10770 20 76 6f 69 64 20 2a 70 53 74 72 65 73 73 2c 20 void *pStress,
10771 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
10772 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 78 53 74 Argument to xSt
10773 72 65 73 73 20 2a 2f 0a 20 20 50 43 61 63 68 65 ress */. PCache
10774 20 2a 70 20 20 20 20 20 20 20 20 20 20 20 20 20 *p
10775 20 20 20 20 20 20 20 2f 2a 20 50 72 65 61 6c 6c /* Preall
10776 6f 63 61 74 65 64 20 73 70 61 63 65 20 66 6f 72 ocated space for
10777 20 74 68 65 20 50 43 61 63 68 65 20 2a 2f 0a 29 the PCache */.)
10778 7b 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c {. memset(p, 0,
10779 20 73 69 7a 65 6f 66 28 50 43 61 63 68 65 29 29 sizeof(PCache))
1077a 3b 0a 20 20 70 2d 3e 73 7a 50 61 67 65 20 3d 20 ;. p->szPage =
1077b 73 7a 50 61 67 65 3b 0a 20 20 70 2d 3e 73 7a 45 szPage;. p->szE
1077c 78 74 72 61 20 3d 20 73 7a 45 78 74 72 61 3b 0a xtra = szExtra;.
1077d 20 20 70 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 p->bPurgeable
1077e 3d 20 62 50 75 72 67 65 61 62 6c 65 3b 0a 20 20 = bPurgeable;.
1077f 70 2d 3e 78 53 74 72 65 73 73 20 3d 20 78 53 74 p->xStress = xSt
10780 72 65 73 73 3b 0a 20 20 70 2d 3e 70 53 74 72 65 ress;. p->pStre
10781 73 73 20 3d 20 70 53 74 72 65 73 73 3b 0a 20 20 ss = pStress;.
10782 70 2d 3e 6e 4d 61 78 20 3d 20 31 30 30 3b 0a 7d p->nMax = 100;.}
10783 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 ../*.** Change t
10784 68 65 20 70 61 67 65 20 73 69 7a 65 20 66 6f 72 he page size for
10785 20 50 43 61 63 68 65 20 6f 62 6a 65 63 74 2e 20 PCache object.
10786 54 68 65 20 63 61 6c 6c 65 72 20 6d 75 73 74 20 The caller must
10787 65 6e 73 75 72 65 20 74 68 61 74 20 74 68 65 72 ensure that ther
10788 65 0a 2a 2a 20 61 72 65 20 6e 6f 20 6f 75 74 73 e.** are no outs
10789 74 61 6e 64 69 6e 67 20 70 61 67 65 20 72 65 66 tanding page ref
1078a 65 72 65 6e 63 65 73 20 77 68 65 6e 20 74 68 69 erences when thi
1078b 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 s function is ca
1078c 6c 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f lled..*/.SQLITE_
1078d 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
1078e 69 74 65 33 50 63 61 63 68 65 53 65 74 50 61 67 ite3PcacheSetPag
1078f 65 53 69 7a 65 28 50 43 61 63 68 65 20 2a 70 43 eSize(PCache *pC
10790 61 63 68 65 2c 20 69 6e 74 20 73 7a 50 61 67 65 ache, int szPage
10791 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 43 61 ){. assert( pCa
10792 63 68 65 2d 3e 6e 52 65 66 3d 3d 30 20 26 26 20 che->nRef==0 &&
10793 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79 3d 3d pCache->pDirty==
10794 30 20 29 3b 0a 20 20 69 66 28 20 70 43 61 63 68 0 );. if( pCach
10795 65 2d 3e 70 43 61 63 68 65 20 29 7b 0a 20 20 20 e->pCache ){.
10796 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
10797 6e 66 69 67 2e 70 63 61 63 68 65 2e 78 44 65 73 nfig.pcache.xDes
10798 74 72 6f 79 28 70 43 61 63 68 65 2d 3e 70 43 61 troy(pCache->pCa
10799 63 68 65 29 3b 0a 20 20 20 20 70 43 61 63 68 65 che);. pCache
1079a 2d 3e 70 43 61 63 68 65 20 3d 20 30 3b 0a 20 20 ->pCache = 0;.
1079b 7d 0a 20 20 70 43 61 63 68 65 2d 3e 73 7a 50 61 }. pCache->szPa
1079c 67 65 20 3d 20 73 7a 50 61 67 65 3b 0a 7d 0a 0a ge = szPage;.}..
1079d 2f 2a 0a 2a 2a 20 54 72 79 20 74 6f 20 6f 62 74 /*.** Try to obt
1079e 61 69 6e 20 61 20 70 61 67 65 20 66 72 6f 6d 20 ain a page from
1079f 74 68 65 20 63 61 63 68 65 2e 0a 2a 2f 0a 53 51 the cache..*/.SQ
107a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
107a1 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 46 65 sqlite3PcacheFe
107a2 74 63 68 28 0a 20 20 50 43 61 63 68 65 20 2a 70 tch(. PCache *p
107a3 43 61 63 68 65 2c 20 20 20 20 20 20 20 2f 2a 20 Cache, /*
107a4 4f 62 74 61 69 6e 20 74 68 65 20 70 61 67 65 20 Obtain the page
107a5 66 72 6f 6d 20 74 68 69 73 20 63 61 63 68 65 20 from this cache
107a6 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 */. Pgno pgno,
107a7 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 /* Pa
107a8 67 65 20 6e 75 6d 62 65 72 20 74 6f 20 6f 62 74 ge number to obt
107a9 61 69 6e 20 2a 2f 0a 20 20 69 6e 74 20 63 72 65 ain */. int cre
107aa 61 74 65 46 6c 61 67 2c 20 20 20 20 20 20 20 2f ateFlag, /
107ab 2a 20 49 66 20 74 72 75 65 2c 20 63 72 65 61 74 * If true, creat
107ac 65 20 70 61 67 65 20 69 66 20 69 74 20 64 6f 65 e page if it doe
107ad 73 20 6e 6f 74 20 65 78 69 73 74 20 61 6c 72 65 s not exist alre
107ae 61 64 79 20 2a 2f 0a 20 20 50 67 48 64 72 20 2a ady */. PgHdr *
107af 2a 70 70 50 61 67 65 20 20 20 20 20 20 20 20 2f *ppPage /
107b0 2a 20 57 72 69 74 65 20 74 68 65 20 70 61 67 65 * Write the page
107b1 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 50 67 here */.){. Pg
107b2 48 64 72 20 2a 70 50 61 67 65 20 3d 20 30 3b 0a Hdr *pPage = 0;.
107b3 20 20 69 6e 74 20 65 43 72 65 61 74 65 3b 0a 0a int eCreate;..
107b4 20 20 61 73 73 65 72 74 28 20 70 43 61 63 68 65 assert( pCache
107b5 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 !=0 );. assert(
107b6 20 63 72 65 61 74 65 46 6c 61 67 3d 3d 31 20 7c createFlag==1 |
107b7 7c 20 63 72 65 61 74 65 46 6c 61 67 3d 3d 30 20 | createFlag==0
107b8 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 67 6e );. assert( pgn
107b9 6f 3e 30 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 o>0 );.. /* If
107ba 74 68 65 20 70 6c 75 67 67 61 62 6c 65 20 63 61 the pluggable ca
107bb 63 68 65 20 28 73 71 6c 69 74 65 33 5f 70 63 61 che (sqlite3_pca
107bc 63 68 65 2a 29 20 68 61 73 20 6e 6f 74 20 62 65 che*) has not be
107bd 65 6e 20 61 6c 6c 6f 63 61 74 65 64 2c 0a 20 20 en allocated,.
107be 2a 2a 20 61 6c 6c 6f 63 61 74 65 20 69 74 20 6e ** allocate it n
107bf 6f 77 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 ow.. */. if( !
107c0 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 20 26 pCache->pCache &
107c1 26 20 63 72 65 61 74 65 46 6c 61 67 20 29 7b 0a & createFlag ){.
107c2 20 20 20 20 73 71 6c 69 74 65 33 5f 70 63 61 63 sqlite3_pcac
107c3 68 65 20 2a 70 3b 0a 20 20 20 20 69 6e 74 20 6e he *p;. int n
107c4 42 79 74 65 3b 0a 20 20 20 20 6e 42 79 74 65 20 Byte;. nByte
107c5 3d 20 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 = pCache->szPage
107c6 20 2b 20 70 43 61 63 68 65 2d 3e 73 7a 45 78 74 + pCache->szExt
107c7 72 61 20 2b 20 73 69 7a 65 6f 66 28 50 67 48 64 ra + sizeof(PgHd
107c8 72 29 3b 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 r);. p = sqli
107c9 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
107ca 70 63 61 63 68 65 2e 78 43 72 65 61 74 65 28 6e pcache.xCreate(n
107cb 42 79 74 65 2c 20 70 43 61 63 68 65 2d 3e 62 50 Byte, pCache->bP
107cc 75 72 67 65 61 62 6c 65 29 3b 0a 20 20 20 20 69 urgeable);. i
107cd 66 28 20 21 70 20 29 7b 0a 20 20 20 20 20 20 72 f( !p ){. r
107ce 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d eturn SQLITE_NOM
107cf 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 EM;. }. sq
107d0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
107d1 67 2e 70 63 61 63 68 65 2e 78 43 61 63 68 65 73 g.pcache.xCaches
107d2 69 7a 65 28 70 2c 20 70 43 61 63 68 65 2d 3e 6e ize(p, pCache->n
107d3 4d 61 78 29 3b 0a 20 20 20 20 70 43 61 63 68 65 Max);. pCache
107d4 2d 3e 70 43 61 63 68 65 20 3d 20 70 3b 0a 20 20 ->pCache = p;.
107d5 7d 0a 0a 20 20 65 43 72 65 61 74 65 20 3d 20 63 }.. eCreate = c
107d6 72 65 61 74 65 46 6c 61 67 20 2a 20 28 31 20 2b reateFlag * (1 +
107d7 20 28 21 70 43 61 63 68 65 2d 3e 62 50 75 72 67 (!pCache->bPurg
107d8 65 61 62 6c 65 20 7c 7c 20 21 70 43 61 63 68 65 eable || !pCache
107d9 2d 3e 70 44 69 72 74 79 29 29 3b 0a 20 20 69 66 ->pDirty));. if
107da 28 20 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 ( pCache->pCache
107db 20 29 7b 0a 20 20 20 20 70 50 61 67 65 20 3d 20 ){. pPage =
107dc 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
107dd 66 69 67 2e 70 63 61 63 68 65 2e 78 46 65 74 63 fig.pcache.xFetc
107de 68 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 h(pCache->pCache
107df 2c 20 70 67 6e 6f 2c 20 65 43 72 65 61 74 65 29 , pgno, eCreate)
107e0 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21 70 50 ;. }.. if( !pP
107e1 61 67 65 20 26 26 20 65 43 72 65 61 74 65 3d 3d age && eCreate==
107e2 31 20 29 7b 0a 20 20 20 20 50 67 48 64 72 20 2a 1 ){. PgHdr *
107e3 70 50 67 3b 0a 0a 20 20 20 20 2f 2a 20 46 69 6e pPg;.. /* Fin
107e4 64 20 61 20 64 69 72 74 79 20 70 61 67 65 20 74 d a dirty page t
107e5 6f 20 77 72 69 74 65 2d 6f 75 74 20 61 6e 64 20 o write-out and
107e6 72 65 63 79 63 6c 65 2e 20 46 69 72 73 74 20 74 recycle. First t
107e7 72 79 20 74 6f 20 66 69 6e 64 20 61 20 0a 20 20 ry to find a .
107e8 20 20 2a 2a 20 70 61 67 65 20 74 68 61 74 20 64 ** page that d
107e9 6f 65 73 20 6e 6f 74 20 72 65 71 75 69 72 65 20 oes not require
107ea 61 20 6a 6f 75 72 6e 61 6c 2d 73 79 6e 63 20 28 a journal-sync (
107eb 6f 6e 65 20 77 69 74 68 20 50 47 48 44 52 5f 4e one with PGHDR_N
107ec 45 45 44 5f 53 59 4e 43 0a 20 20 20 20 2a 2a 20 EED_SYNC. **
107ed 63 6c 65 61 72 65 64 29 2c 20 62 75 74 20 69 66 cleared), but if
107ee 20 74 68 61 74 20 69 73 20 6e 6f 74 20 70 6f 73 that is not pos
107ef 73 69 62 6c 65 20 73 65 74 74 6c 65 20 66 6f 72 sible settle for
107f0 20 61 6e 79 20 6f 74 68 65 72 20 0a 20 20 20 20 any other .
107f1 2a 2a 20 75 6e 72 65 66 65 72 65 6e 63 65 64 20 ** unreferenced
107f2 64 69 72 74 79 20 70 61 67 65 2e 0a 20 20 20 20 dirty page..
107f3 2a 2f 0a 20 20 20 20 65 78 70 65 6e 73 69 76 65 */. expensive
107f4 5f 61 73 73 65 72 74 28 20 70 63 61 63 68 65 43 _assert( pcacheC
107f5 68 65 63 6b 53 79 6e 63 65 64 28 70 43 61 63 68 heckSynced(pCach
107f6 65 29 20 29 3b 0a 20 20 20 20 66 6f 72 28 70 50 e) );. for(pP
107f7 67 3d 70 43 61 63 68 65 2d 3e 70 53 79 6e 63 65 g=pCache->pSynce
107f8 64 3b 20 0a 20 20 20 20 20 20 20 20 70 50 67 20 d; . pPg
107f9 26 26 20 28 70 50 67 2d 3e 6e 52 65 66 20 7c 7c && (pPg->nRef ||
107fa 20 28 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 (pPg->flags&PGH
107fb 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 29 3b 20 DR_NEED_SYNC));
107fc 0a 20 20 20 20 20 20 20 20 70 50 67 3d 70 50 67 . pPg=pPg
107fd 2d 3e 70 44 69 72 74 79 50 72 65 76 0a 20 20 20 ->pDirtyPrev.
107fe 20 29 3b 0a 20 20 20 20 69 66 28 20 21 70 50 67 );. if( !pPg
107ff 20 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 70 50 ){. for(pP
10800 67 3d 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79 g=pCache->pDirty
10801 54 61 69 6c 3b 20 70 50 67 20 26 26 20 70 50 67 Tail; pPg && pPg
10802 2d 3e 6e 52 65 66 3b 20 70 50 67 3d 70 50 67 2d ->nRef; pPg=pPg-
10803 3e 70 44 69 72 74 79 50 72 65 76 29 3b 0a 20 20 >pDirtyPrev);.
10804 20 20 7d 0a 20 20 20 20 69 66 28 20 70 50 67 20 }. if( pPg
10805 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 63 3b ){. int rc;
10806 0a 20 20 20 20 20 20 72 63 20 3d 20 70 43 61 63 . rc = pCac
10807 68 65 2d 3e 78 53 74 72 65 73 73 28 70 43 61 63 he->xStress(pCac
10808 68 65 2d 3e 70 53 74 72 65 73 73 2c 20 70 50 67 he->pStress, pPg
10809 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 );. if( rc!
1080a 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 63 =SQLITE_OK && rc
1080b 21 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 29 7b !=SQLITE_BUSY ){
1080c 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 . return
1080d 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 rc;. }.
1080e 7d 0a 0a 20 20 20 20 70 50 61 67 65 20 3d 20 73 }.. pPage = s
1080f 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
10810 69 67 2e 70 63 61 63 68 65 2e 78 46 65 74 63 68 ig.pcache.xFetch
10811 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 2c (pCache->pCache,
10812 20 70 67 6e 6f 2c 20 32 29 3b 0a 20 20 7d 0a 0a pgno, 2);. }..
10813 20 20 69 66 28 20 70 50 61 67 65 20 29 7b 0a 20 if( pPage ){.
10814 20 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 70 if( !pPage->p
10815 44 61 74 61 20 29 7b 0a 20 20 20 20 20 20 6d 65 Data ){. me
10816 6d 73 65 74 28 70 50 61 67 65 2c 20 30 2c 20 73 mset(pPage, 0, s
10817 69 7a 65 6f 66 28 50 67 48 64 72 29 20 2b 20 70 izeof(PgHdr) + p
10818 43 61 63 68 65 2d 3e 73 7a 45 78 74 72 61 29 3b Cache->szExtra);
10819 0a 20 20 20 20 20 20 70 50 61 67 65 2d 3e 70 45 . pPage->pE
1081a 78 74 72 61 20 3d 20 28 76 6f 69 64 2a 29 26 70 xtra = (void*)&p
1081b 50 61 67 65 5b 31 5d 3b 0a 20 20 20 20 20 20 70 Page[1];. p
1081c 50 61 67 65 2d 3e 70 44 61 74 61 20 3d 20 28 76 Page->pData = (v
1081d 6f 69 64 20 2a 29 26 28 28 63 68 61 72 20 2a 29 oid *)&((char *)
1081e 70 50 61 67 65 29 5b 73 69 7a 65 6f 66 28 50 67 pPage)[sizeof(Pg
1081f 48 64 72 29 20 2b 20 70 43 61 63 68 65 2d 3e 73 Hdr) + pCache->s
10820 7a 45 78 74 72 61 5d 3b 0a 20 20 20 20 20 20 70 zExtra];. p
10821 50 61 67 65 2d 3e 70 43 61 63 68 65 20 3d 20 70 Page->pCache = p
10822 43 61 63 68 65 3b 0a 20 20 20 20 20 20 70 50 61 Cache;. pPa
10823 67 65 2d 3e 70 67 6e 6f 20 3d 20 70 67 6e 6f 3b ge->pgno = pgno;
10824 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 . }. asser
10825 74 28 20 70 50 61 67 65 2d 3e 70 43 61 63 68 65 t( pPage->pCache
10826 3d 3d 70 43 61 63 68 65 20 29 3b 0a 20 20 20 20 ==pCache );.
10827 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 assert( pPage->p
10828 67 6e 6f 3d 3d 70 67 6e 6f 20 29 3b 0a 20 20 20 gno==pgno );.
10829 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e assert( pPage->
1082a 70 45 78 74 72 61 3d 3d 28 76 6f 69 64 20 2a 29 pExtra==(void *)
1082b 26 70 50 61 67 65 5b 31 5d 20 29 3b 0a 0a 20 20 &pPage[1] );..
1082c 20 20 69 66 28 20 30 3d 3d 70 50 61 67 65 2d 3e if( 0==pPage->
1082d 6e 52 65 66 20 29 7b 0a 20 20 20 20 20 20 70 43 nRef ){. pC
1082e 61 63 68 65 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 ache->nRef++;.
1082f 20 20 7d 0a 20 20 20 20 70 50 61 67 65 2d 3e 6e }. pPage->n
10830 52 65 66 2b 2b 3b 0a 20 20 20 20 69 66 28 20 70 Ref++;. if( p
10831 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 gno==1 ){.
10832 70 43 61 63 68 65 2d 3e 70 50 61 67 65 31 20 3d pCache->pPage1 =
10833 20 70 50 61 67 65 3b 0a 20 20 20 20 7d 0a 20 20 pPage;. }.
10834 7d 0a 20 20 2a 70 70 50 61 67 65 20 3d 20 70 50 }. *ppPage = pP
10835 61 67 65 3b 0a 20 20 72 65 74 75 72 6e 20 28 70 age;. return (p
10836 50 61 67 65 3d 3d 30 20 26 26 20 65 43 72 65 61 Page==0 && eCrea
10837 74 65 29 20 3f 20 53 51 4c 49 54 45 5f 4e 4f 4d te) ? SQLITE_NOM
10838 45 4d 20 3a 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a EM : SQLITE_OK;.
10839 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 72 65 6d 65 }../*.** Decreme
1083a 6e 74 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 nt the reference
1083b 20 63 6f 75 6e 74 20 6f 6e 20 61 20 70 61 67 65 count on a page
1083c 2e 20 49 66 20 74 68 65 20 70 61 67 65 20 69 73 . If the page is
1083d 20 63 6c 65 61 6e 20 61 6e 64 20 74 68 65 0a 2a clean and the.*
1083e 2a 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e * reference coun
1083f 74 20 64 72 6f 70 73 20 74 6f 20 30 2c 20 74 68 t drops to 0, th
10840 65 6e 20 69 74 20 69 73 20 6d 61 64 65 20 65 6c en it is made el
10841 69 62 6c 65 20 66 6f 72 20 72 65 63 79 63 6c 69 ible for recycli
10842 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ng..*/.SQLITE_PR
10843 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
10844 65 33 50 63 61 63 68 65 52 65 6c 65 61 73 65 28 e3PcacheRelease(
10845 50 67 48 64 72 20 2a 70 29 7b 0a 20 20 61 73 73 PgHdr *p){. ass
10846 65 72 74 28 20 70 2d 3e 6e 52 65 66 3e 30 20 29 ert( p->nRef>0 )
10847 3b 0a 20 20 70 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 ;. p->nRef--;.
10848 20 69 66 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 if( p->nRef==0
10849 29 7b 0a 20 20 20 20 50 43 61 63 68 65 20 2a 70 ){. PCache *p
1084a 43 61 63 68 65 20 3d 20 70 2d 3e 70 43 61 63 68 Cache = p->pCach
1084b 65 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 6e e;. pCache->n
1084c 52 65 66 2d 2d 3b 0a 20 20 20 20 69 66 28 20 28 Ref--;. if( (
1084d 70 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 p->flags&PGHDR_D
1084e 49 52 54 59 29 3d 3d 30 20 29 7b 0a 20 20 20 20 IRTY)==0 ){.
1084f 20 20 70 63 61 63 68 65 55 6e 70 69 6e 28 70 29 pcacheUnpin(p)
10850 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
10851 20 20 20 2f 2a 20 4d 6f 76 65 20 74 68 65 20 70 /* Move the p
10852 61 67 65 20 74 6f 20 74 68 65 20 68 65 61 64 20 age to the head
10853 6f 66 20 74 68 65 20 64 69 72 74 79 20 6c 69 73 of the dirty lis
10854 74 2e 20 2a 2f 0a 20 20 20 20 20 20 70 63 61 63 t. */. pcac
10855 68 65 52 65 6d 6f 76 65 46 72 6f 6d 44 69 72 74 heRemoveFromDirt
10856 79 4c 69 73 74 28 70 29 3b 0a 20 20 20 20 20 20 yList(p);.
10857 70 63 61 63 68 65 41 64 64 54 6f 44 69 72 74 79 pcacheAddToDirty
10858 4c 69 73 74 28 70 29 3b 0a 20 20 20 20 7d 0a 20 List(p);. }.
10859 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 72 }.}../*.** Incr
1085a 65 61 73 65 20 74 68 65 20 72 65 66 65 72 65 6e ease the referen
1085b 63 65 20 63 6f 75 6e 74 20 6f 66 20 61 20 73 75 ce count of a su
1085c 70 70 6c 69 65 64 20 70 61 67 65 20 62 79 20 31 pplied page by 1
1085d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1085e 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
1085f 50 63 61 63 68 65 52 65 66 28 50 67 48 64 72 20 PcacheRef(PgHdr
10860 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 70 2d *p){. assert(p-
10861 3e 6e 52 65 66 3e 30 29 3b 0a 20 20 70 2d 3e 6e >nRef>0);. p->n
10862 52 65 66 2b 2b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 Ref++;.}../*.**
10863 44 72 6f 70 20 61 20 70 61 67 65 20 66 72 6f 6d Drop a page from
10864 20 74 68 65 20 63 61 63 68 65 2e 20 54 68 65 72 the cache. Ther
10865 65 20 6d 75 73 74 20 62 65 20 65 78 61 63 74 6c e must be exactl
10866 79 20 6f 6e 65 20 72 65 66 65 72 65 6e 63 65 20 y one reference
10867 74 6f 20 74 68 65 0a 2a 2a 20 70 61 67 65 2e 20 to the.** page.
10868 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 65 This function de
10869 6c 65 74 65 73 20 74 68 61 74 20 72 65 66 65 72 letes that refer
1086a 65 6e 63 65 2c 20 73 6f 20 61 66 74 65 72 20 69 ence, so after i
1086b 74 20 72 65 74 75 72 6e 73 20 74 68 65 0a 2a 2a t returns the.**
1086c 20 70 61 67 65 20 70 6f 69 6e 74 65 64 20 74 6f page pointed to
1086d 20 62 79 20 70 20 69 73 20 69 6e 76 61 6c 69 64 by p is invalid
1086e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1086f 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
10870 50 63 61 63 68 65 44 72 6f 70 28 50 67 48 64 72 PcacheDrop(PgHdr
10871 20 2a 70 29 7b 0a 20 20 50 43 61 63 68 65 20 2a *p){. PCache *
10872 70 43 61 63 68 65 3b 0a 20 20 61 73 73 65 72 74 pCache;. assert
10873 28 20 70 2d 3e 6e 52 65 66 3d 3d 31 20 29 3b 0a ( p->nRef==1 );.
10874 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 26 50 if( p->flags&P
10875 47 48 44 52 5f 44 49 52 54 59 20 29 7b 0a 20 20 GHDR_DIRTY ){.
10876 20 20 70 63 61 63 68 65 52 65 6d 6f 76 65 46 72 pcacheRemoveFr
10877 6f 6d 44 69 72 74 79 4c 69 73 74 28 70 29 3b 0a omDirtyList(p);.
10878 20 20 7d 0a 20 20 70 43 61 63 68 65 20 3d 20 70 }. pCache = p
10879 2d 3e 70 43 61 63 68 65 3b 0a 20 20 70 43 61 63 ->pCache;. pCac
1087a 68 65 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 69 66 he->nRef--;. if
1087b 28 20 70 2d 3e 70 67 6e 6f 3d 3d 31 20 29 7b 0a ( p->pgno==1 ){.
1087c 20 20 20 20 70 43 61 63 68 65 2d 3e 70 50 61 67 pCache->pPag
1087d 65 31 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 73 71 e1 = 0;. }. sq
1087e 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
1087f 67 2e 70 63 61 63 68 65 2e 78 55 6e 70 69 6e 28 g.pcache.xUnpin(
10880 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 pCache->pCache,
10881 70 2c 20 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 p, 1);.}../*.**
10882 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 70 61 Make sure the pa
10883 67 65 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20 ge is marked as
10884 64 69 72 74 79 2e 20 49 66 20 69 74 20 69 73 6e dirty. If it isn
10885 27 74 20 64 69 72 74 79 20 61 6c 72 65 61 64 79 't dirty already
10886 2c 0a 2a 2a 20 6d 61 6b 65 20 69 74 20 73 6f 2e ,.** make it so.
10887 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
10888 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 TE void sqlite3P
10889 63 61 63 68 65 4d 61 6b 65 44 69 72 74 79 28 50 cacheMakeDirty(P
1088a 67 48 64 72 20 2a 70 29 7b 0a 20 20 70 2d 3e 66 gHdr *p){. p->f
1088b 6c 61 67 73 20 26 3d 20 7e 50 47 48 44 52 5f 44 lags &= ~PGHDR_D
1088c 4f 4e 54 5f 57 52 49 54 45 3b 0a 20 20 61 73 73 ONT_WRITE;. ass
1088d 65 72 74 28 20 70 2d 3e 6e 52 65 66 3e 30 20 29 ert( p->nRef>0 )
1088e 3b 0a 20 20 69 66 28 20 30 3d 3d 28 70 2d 3e 66 ;. if( 0==(p->f
1088f 6c 61 67 73 20 26 20 50 47 48 44 52 5f 44 49 52 lags & PGHDR_DIR
10890 54 59 29 20 29 7b 0a 20 20 20 20 70 2d 3e 66 6c TY) ){. p->fl
10891 61 67 73 20 7c 3d 20 50 47 48 44 52 5f 44 49 52 ags |= PGHDR_DIR
10892 54 59 3b 0a 20 20 20 20 70 63 61 63 68 65 41 64 TY;. pcacheAd
10893 64 54 6f 44 69 72 74 79 4c 69 73 74 28 20 70 29 dToDirtyList( p)
10894 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d ;. }.}../*.** M
10895 61 6b 65 20 73 75 72 65 20 74 68 65 20 70 61 67 ake sure the pag
10896 65 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20 63 e is marked as c
10897 6c 65 61 6e 2e 20 49 66 20 69 74 20 69 73 6e 27 lean. If it isn'
10898 74 20 63 6c 65 61 6e 20 61 6c 72 65 61 64 79 2c t clean already,
10899 0a 2a 2a 20 6d 61 6b 65 20 69 74 20 73 6f 2e 0a .** make it so..
1089a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1089b 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 E void sqlite3Pc
1089c 61 63 68 65 4d 61 6b 65 43 6c 65 61 6e 28 50 67 acheMakeClean(Pg
1089d 48 64 72 20 2a 70 29 7b 0a 20 20 69 66 28 20 28 Hdr *p){. if( (
1089e 70 2d 3e 66 6c 61 67 73 20 26 20 50 47 48 44 52 p->flags & PGHDR
1089f 5f 44 49 52 54 59 29 20 29 7b 0a 20 20 20 20 70 _DIRTY) ){. p
108a0 63 61 63 68 65 52 65 6d 6f 76 65 46 72 6f 6d 44 cacheRemoveFromD
108a1 69 72 74 79 4c 69 73 74 28 70 29 3b 0a 20 20 20 irtyList(p);.
108a2 20 70 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 50 p->flags &= ~(P
108a3 47 48 44 52 5f 44 49 52 54 59 7c 50 47 48 44 52 GHDR_DIRTY|PGHDR
108a4 5f 4e 45 45 44 5f 53 59 4e 43 29 3b 0a 20 20 20 _NEED_SYNC);.
108a5 20 69 66 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 if( p->nRef==0
108a6 29 7b 0a 20 20 20 20 20 20 70 63 61 63 68 65 55 ){. pcacheU
108a7 6e 70 69 6e 28 70 29 3b 0a 20 20 20 20 7d 0a 20 npin(p);. }.
108a8 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 }.}../*.** Make
108a9 20 65 76 65 72 79 20 70 61 67 65 20 69 6e 20 74 every page in t
108aa 68 65 20 63 61 63 68 65 20 63 6c 65 61 6e 2e 0a he cache clean..
108ab 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
108ac 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 E void sqlite3Pc
108ad 61 63 68 65 43 6c 65 61 6e 41 6c 6c 28 50 43 61 acheCleanAll(PCa
108ae 63 68 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 che *pCache){.
108af 50 67 48 64 72 20 2a 70 3b 0a 20 20 77 68 69 6c PgHdr *p;. whil
108b0 65 28 20 28 70 20 3d 20 70 43 61 63 68 65 2d 3e e( (p = pCache->
108b1 70 44 69 72 74 79 29 21 3d 30 20 29 7b 0a 20 20 pDirty)!=0 ){.
108b2 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d sqlite3PcacheM
108b3 61 6b 65 43 6c 65 61 6e 28 70 29 3b 0a 20 20 7d akeClean(p);. }
108b4 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 .}../*.** Clear
108b5 74 68 65 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 the PGHDR_NEED_S
108b6 59 4e 43 20 66 6c 61 67 20 66 72 6f 6d 20 61 6c YNC flag from al
108b7 6c 20 64 69 72 74 79 20 70 61 67 65 73 2e 0a 2a l dirty pages..*
108b8 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
108b9 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 void sqlite3Pca
108ba 63 68 65 43 6c 65 61 72 53 79 6e 63 46 6c 61 67 cheClearSyncFlag
108bb 73 28 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 s(PCache *pCache
108bc 29 7b 0a 20 20 50 67 48 64 72 20 2a 70 3b 0a 20 ){. PgHdr *p;.
108bd 20 66 6f 72 28 70 3d 70 43 61 63 68 65 2d 3e 70 for(p=pCache->p
108be 44 69 72 74 79 3b 20 70 3b 20 70 3d 70 2d 3e 70 Dirty; p; p=p->p
108bf 44 69 72 74 79 4e 65 78 74 29 7b 0a 20 20 20 20 DirtyNext){.
108c0 70 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 50 47 48 p->flags &= ~PGH
108c1 44 52 5f 4e 45 45 44 5f 53 59 4e 43 3b 0a 20 20 DR_NEED_SYNC;.
108c2 7d 0a 20 20 70 43 61 63 68 65 2d 3e 70 53 79 6e }. pCache->pSyn
108c3 63 65 64 20 3d 20 70 43 61 63 68 65 2d 3e 70 44 ced = pCache->pD
108c4 69 72 74 79 54 61 69 6c 3b 0a 7d 0a 0a 2f 2a 0a irtyTail;.}../*.
108c5 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 70 61 ** Change the pa
108c6 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 ge number of pag
108c7 65 20 70 20 74 6f 20 6e 65 77 50 67 6e 6f 2e 20 e p to newPgno.
108c8 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
108c9 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 TE void sqlite3P
108ca 63 61 63 68 65 4d 6f 76 65 28 50 67 48 64 72 20 cacheMove(PgHdr
108cb 2a 70 2c 20 50 67 6e 6f 20 6e 65 77 50 67 6e 6f *p, Pgno newPgno
108cc 29 7b 0a 20 20 50 43 61 63 68 65 20 2a 70 43 61 ){. PCache *pCa
108cd 63 68 65 20 3d 20 70 2d 3e 70 43 61 63 68 65 3b che = p->pCache;
108ce 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 . assert( p->nR
108cf 65 66 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 ef>0 );. assert
108d0 28 20 6e 65 77 50 67 6e 6f 3e 30 20 29 3b 0a 20 ( newPgno>0 );.
108d1 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
108d2 6e 66 69 67 2e 70 63 61 63 68 65 2e 78 52 65 6b nfig.pcache.xRek
108d3 65 79 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68 ey(pCache->pCach
108d4 65 2c 20 70 2c 20 70 2d 3e 70 67 6e 6f 2c 20 6e e, p, p->pgno, n
108d5 65 77 50 67 6e 6f 29 3b 0a 20 20 70 2d 3e 70 67 ewPgno);. p->pg
108d6 6e 6f 20 3d 20 6e 65 77 50 67 6e 6f 3b 0a 20 20 no = newPgno;.
108d7 69 66 28 20 28 70 2d 3e 66 6c 61 67 73 26 50 47 if( (p->flags&PG
108d8 48 44 52 5f 44 49 52 54 59 29 20 26 26 20 28 70 HDR_DIRTY) && (p
108d9 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 ->flags&PGHDR_NE
108da 45 44 5f 53 59 4e 43 29 20 29 7b 0a 20 20 20 20 ED_SYNC) ){.
108db 70 63 61 63 68 65 52 65 6d 6f 76 65 46 72 6f 6d pcacheRemoveFrom
108dc 44 69 72 74 79 4c 69 73 74 28 70 29 3b 0a 20 20 DirtyList(p);.
108dd 20 20 70 63 61 63 68 65 41 64 64 54 6f 44 69 72 pcacheAddToDir
108de 74 79 4c 69 73 74 28 70 29 3b 0a 20 20 7d 0a 7d tyList(p);. }.}
108df 0a 0a 2f 2a 0a 2a 2a 20 44 72 6f 70 20 65 76 65 ../*.** Drop eve
108e0 72 79 20 63 61 63 68 65 20 65 6e 74 72 79 20 77 ry cache entry w
108e1 68 6f 73 65 20 70 61 67 65 20 6e 75 6d 62 65 72 hose page number
108e2 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e is greater than
108e3 20 22 70 67 6e 6f 22 2e 20 54 68 65 0a 2a 2a 20 "pgno". The.**
108e4 63 61 6c 6c 65 72 20 6d 75 73 74 20 65 6e 73 75 caller must ensu
108e5 72 65 20 74 68 61 74 20 74 68 65 72 65 20 61 72 re that there ar
108e6 65 20 6e 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67 e no outstanding
108e7 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 61 references to a
108e8 6e 79 20 70 61 67 65 73 0a 2a 2a 20 6f 74 68 65 ny pages.** othe
108e9 72 20 74 68 61 6e 20 70 61 67 65 20 31 20 77 69 r than page 1 wi
108ea 74 68 20 61 20 70 61 67 65 20 6e 75 6d 62 65 72 th a page number
108eb 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 70 67 greater than pg
108ec 6e 6f 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 no..**.** If the
108ed 72 65 20 69 73 20 61 20 72 65 66 65 72 65 6e 63 re is a referenc
108ee 65 20 74 6f 20 70 61 67 65 20 31 20 61 6e 64 20 e to page 1 and
108ef 74 68 65 20 70 67 6e 6f 20 70 61 72 61 6d 65 74 the pgno paramet
108f0 65 72 20 70 61 73 73 65 64 20 74 6f 20 74 68 69 er passed to thi
108f1 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 69 73 s.** function is
108f2 20 30 2c 20 74 68 65 6e 20 74 68 65 20 64 61 74 0, then the dat
108f3 61 20 61 72 65 61 20 61 73 73 6f 63 69 61 74 65 a area associate
108f4 64 20 77 69 74 68 20 70 61 67 65 20 31 20 69 73 d with page 1 is
108f5 20 7a 65 72 6f 65 64 2c 20 62 75 74 0a 2a 2a 20 zeroed, but.**
108f6 74 68 65 20 70 61 67 65 20 6f 62 6a 65 63 74 20 the page object
108f7 69 73 20 6e 6f 74 20 64 72 6f 70 70 65 64 2e 0a is not dropped..
108f8 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
108f9 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 E void sqlite3Pc
108fa 61 63 68 65 54 72 75 6e 63 61 74 65 28 50 43 61 acheTruncate(PCa
108fb 63 68 65 20 2a 70 43 61 63 68 65 2c 20 50 67 6e che *pCache, Pgn
108fc 6f 20 70 67 6e 6f 29 7b 0a 20 20 69 66 28 20 70 o pgno){. if( p
108fd 43 61 63 68 65 2d 3e 70 43 61 63 68 65 20 29 7b Cache->pCache ){
108fe 0a 20 20 20 20 50 67 48 64 72 20 2a 70 3b 0a 20 . PgHdr *p;.
108ff 20 20 20 50 67 48 64 72 20 2a 70 4e 65 78 74 3b PgHdr *pNext;
10900 0a 20 20 20 20 66 6f 72 28 70 3d 70 43 61 63 68 . for(p=pCach
10901 65 2d 3e 70 44 69 72 74 79 3b 20 70 3b 20 70 3d e->pDirty; p; p=
10902 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 70 4e pNext){. pN
10903 65 78 74 20 3d 20 70 2d 3e 70 44 69 72 74 79 4e ext = p->pDirtyN
10904 65 78 74 3b 0a 20 20 20 20 20 20 69 66 28 20 70 ext;. if( p
10905 2d 3e 70 67 6e 6f 3e 70 67 6e 6f 20 29 7b 0a 20 ->pgno>pgno ){.
10906 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 assert( p
10907 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 ->flags&PGHDR_DI
10908 52 54 59 20 29 3b 0a 20 20 20 20 20 20 20 20 73 RTY );. s
10909 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 qlite3PcacheMake
1090a 43 6c 65 61 6e 28 70 29 3b 0a 20 20 20 20 20 20 Clean(p);.
1090b 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 }. }. if(
1090c 70 67 6e 6f 3d 3d 30 20 26 26 20 70 43 61 63 68 pgno==0 && pCach
1090d 65 2d 3e 70 50 61 67 65 31 20 29 7b 0a 20 20 20 e->pPage1 ){.
1090e 20 20 20 6d 65 6d 73 65 74 28 70 43 61 63 68 65 memset(pCache
1090f 2d 3e 70 50 61 67 65 31 2d 3e 70 44 61 74 61 2c ->pPage1->pData,
10910 20 30 2c 20 70 43 61 63 68 65 2d 3e 73 7a 50 61 0, pCache->szPa
10911 67 65 29 3b 0a 20 20 20 20 20 20 70 67 6e 6f 20 ge);. pgno
10912 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 = 1;. }. s
10913 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
10914 69 67 2e 70 63 61 63 68 65 2e 78 54 72 75 6e 63 ig.pcache.xTrunc
10915 61 74 65 28 70 43 61 63 68 65 2d 3e 70 43 61 63 ate(pCache->pCac
10916 68 65 2c 20 70 67 6e 6f 2b 31 29 3b 0a 20 20 7d he, pgno+1);. }
10917 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 .}../*.** Close
10918 61 20 63 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49 a cache..*/.SQLI
10919 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
1091a 73 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c 6f sqlite3PcacheClo
1091b 73 65 28 50 43 61 63 68 65 20 2a 70 43 61 63 68 se(PCache *pCach
1091c 65 29 7b 0a 20 20 69 66 28 20 70 43 61 63 68 65 e){. if( pCache
1091d 2d 3e 70 43 61 63 68 65 20 29 7b 0a 20 20 20 20 ->pCache ){.
1091e 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
1091f 66 69 67 2e 70 63 61 63 68 65 2e 78 44 65 73 74 fig.pcache.xDest
10920 72 6f 79 28 70 43 61 63 68 65 2d 3e 70 43 61 63 roy(pCache->pCac
10921 68 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 0a he);. }.}../* .
10922 2a 2a 20 44 69 73 63 61 72 64 20 74 68 65 20 63 ** Discard the c
10923 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 63 ontents of the c
10924 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ache..*/.SQLITE_
10925 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
10926 69 74 65 33 50 63 61 63 68 65 43 6c 65 61 72 28 ite3PcacheClear(
10927 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b PCache *pCache){
10928 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 . sqlite3Pcache
10929 54 72 75 6e 63 61 74 65 28 70 43 61 63 68 65 2c Truncate(pCache,
1092a 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65 0);.}../*.** Me
1092b 72 67 65 20 74 77 6f 20 6c 69 73 74 73 20 6f 66 rge two lists of
1092c 20 70 61 67 65 73 20 63 6f 6e 6e 65 63 74 65 64 pages connected
1092d 20 62 79 20 70 44 69 72 74 79 20 61 6e 64 20 69 by pDirty and i
1092e 6e 20 70 67 6e 6f 20 6f 72 64 65 72 2e 0a 2a 2a n pgno order..**
1092f 20 44 6f 20 6e 6f 74 20 62 6f 74 68 20 66 69 78 Do not both fix
10930 69 6e 67 20 74 68 65 20 70 44 69 72 74 79 50 72 ing the pDirtyPr
10931 65 76 20 70 6f 69 6e 74 65 72 73 2e 0a 2a 2f 0a ev pointers..*/.
10932 73 74 61 74 69 63 20 50 67 48 64 72 20 2a 70 63 static PgHdr *pc
10933 61 63 68 65 4d 65 72 67 65 44 69 72 74 79 4c 69 acheMergeDirtyLi
10934 73 74 28 50 67 48 64 72 20 2a 70 41 2c 20 50 67 st(PgHdr *pA, Pg
10935 48 64 72 20 2a 70 42 29 7b 0a 20 20 50 67 48 64 Hdr *pB){. PgHd
10936 72 20 72 65 73 75 6c 74 2c 20 2a 70 54 61 69 6c r result, *pTail
10937 3b 0a 20 20 70 54 61 69 6c 20 3d 20 26 72 65 73 ;. pTail = &res
10938 75 6c 74 3b 0a 20 20 77 68 69 6c 65 28 20 70 41 ult;. while( pA
10939 20 26 26 20 70 42 20 29 7b 0a 20 20 20 20 69 66 && pB ){. if
1093a 28 20 70 41 2d 3e 70 67 6e 6f 3c 70 42 2d 3e 70 ( pA->pgno<pB->p
1093b 67 6e 6f 20 29 7b 0a 20 20 20 20 20 20 70 54 61 gno ){. pTa
1093c 69 6c 2d 3e 70 44 69 72 74 79 20 3d 20 70 41 3b il->pDirty = pA;
1093d 0a 20 20 20 20 20 20 70 54 61 69 6c 20 3d 20 70 . pTail = p
1093e 41 3b 0a 20 20 20 20 20 20 70 41 20 3d 20 70 41 A;. pA = pA
1093f 2d 3e 70 44 69 72 74 79 3b 0a 20 20 20 20 7d 65 ->pDirty;. }e
10940 6c 73 65 7b 0a 20 20 20 20 20 20 70 54 61 69 6c lse{. pTail
10941 2d 3e 70 44 69 72 74 79 20 3d 20 70 42 3b 0a 20 ->pDirty = pB;.
10942 20 20 20 20 20 70 54 61 69 6c 20 3d 20 70 42 3b pTail = pB;
10943 0a 20 20 20 20 20 20 70 42 20 3d 20 70 42 2d 3e . pB = pB->
10944 70 44 69 72 74 79 3b 0a 20 20 20 20 7d 0a 20 20 pDirty;. }.
10945 7d 0a 20 20 69 66 28 20 70 41 20 29 7b 0a 20 20 }. if( pA ){.
10946 20 20 70 54 61 69 6c 2d 3e 70 44 69 72 74 79 20 pTail->pDirty
10947 3d 20 70 41 3b 0a 20 20 7d 65 6c 73 65 20 69 66 = pA;. }else if
10948 28 20 70 42 20 29 7b 0a 20 20 20 20 70 54 61 69 ( pB ){. pTai
10949 6c 2d 3e 70 44 69 72 74 79 20 3d 20 70 42 3b 0a l->pDirty = pB;.
1094a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 54 61 }else{. pTa
1094b 69 6c 2d 3e 70 44 69 72 74 79 20 3d 20 30 3b 0a il->pDirty = 0;.
1094c 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 65 73 }. return res
1094d 75 6c 74 2e 70 44 69 72 74 79 3b 0a 7d 0a 0a 2f ult.pDirty;.}../
1094e 2a 0a 2a 2a 20 53 6f 72 74 20 74 68 65 20 6c 69 *.** Sort the li
1094f 73 74 20 6f 66 20 70 61 67 65 73 20 69 6e 20 61 st of pages in a
10950 63 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 20 62 ccending order b
10951 79 20 70 67 6e 6f 2e 20 20 50 61 67 65 73 20 61 y pgno. Pages a
10952 72 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 65 64 20 re.** connected
10953 62 79 20 70 44 69 72 74 79 20 70 6f 69 6e 74 65 by pDirty pointe
10954 72 73 2e 20 20 54 68 65 20 70 44 69 72 74 79 50 rs. The pDirtyP
10955 72 65 76 20 70 6f 69 6e 74 65 72 73 20 61 72 65 rev pointers are
10956 0a 2a 2a 20 63 6f 72 72 75 70 74 65 64 20 62 79 .** corrupted by
10957 20 74 68 69 73 20 73 6f 72 74 2e 0a 2a 2a 0a 2a this sort..**.*
10958 2a 20 53 69 6e 63 65 20 74 68 65 72 65 20 63 61 * Since there ca
10959 6e 6e 6f 74 20 62 65 20 6d 6f 72 65 20 74 68 61 nnot be more tha
1095a 6e 20 32 5e 33 31 20 64 69 73 74 69 6e 63 74 20 n 2^31 distinct
1095b 70 61 67 65 73 20 69 6e 20 61 20 64 61 74 61 62 pages in a datab
1095c 61 73 65 2c 0a 2a 2a 20 74 68 65 72 65 20 63 61 ase,.** there ca
1095d 6e 6e 6f 74 20 62 65 20 6d 6f 72 65 20 74 68 61 nnot be more tha
1095e 6e 20 33 31 20 62 75 63 6b 65 74 73 20 72 65 71 n 31 buckets req
1095f 75 69 72 65 64 20 62 79 20 74 68 65 20 6d 65 72 uired by the mer
10960 67 65 20 73 6f 72 74 65 72 2e 0a 2a 2a 20 4f 6e ge sorter..** On
10961 65 20 65 78 74 72 61 20 62 75 63 6b 65 74 20 69 e extra bucket i
10962 73 20 61 64 64 65 64 20 74 6f 20 63 61 74 63 68 s added to catch
10963 20 6f 76 65 72 66 6c 6f 77 20 69 6e 20 63 61 73 overflow in cas
10964 65 20 73 6f 6d 65 74 68 69 6e 67 0a 2a 2a 20 65 e something.** e
10965 76 65 72 20 63 68 61 6e 67 65 73 20 74 6f 20 6d ver changes to m
10966 61 6b 65 20 74 68 65 20 70 72 65 76 69 6f 75 73 ake the previous
10967 20 73 65 6e 74 65 6e 63 65 20 69 6e 63 6f 72 72 sentence incorr
10968 65 63 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 ect..*/.#define
10969 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54 20 20 33 N_SORT_BUCKET 3
1096a 32 0a 73 74 61 74 69 63 20 50 67 48 64 72 20 2a 2.static PgHdr *
1096b 70 63 61 63 68 65 53 6f 72 74 44 69 72 74 79 4c pcacheSortDirtyL
1096c 69 73 74 28 50 67 48 64 72 20 2a 70 49 6e 29 7b ist(PgHdr *pIn){
1096d 0a 20 20 50 67 48 64 72 20 2a 61 5b 4e 5f 53 4f . PgHdr *a[N_SO
1096e 52 54 5f 42 55 43 4b 45 54 5d 2c 20 2a 70 3b 0a RT_BUCKET], *p;.
1096f 20 20 69 6e 74 20 69 3b 0a 20 20 6d 65 6d 73 65 int i;. memse
10970 74 28 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 61 t(a, 0, sizeof(a
10971 29 29 3b 0a 20 20 77 68 69 6c 65 28 20 70 49 6e ));. while( pIn
10972 20 29 7b 0a 20 20 20 20 70 20 3d 20 70 49 6e 3b ){. p = pIn;
10973 0a 20 20 20 20 70 49 6e 20 3d 20 70 2d 3e 70 44 . pIn = p->pD
10974 69 72 74 79 3b 0a 20 20 20 20 70 2d 3e 70 44 69 irty;. p->pDi
10975 72 74 79 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72 rty = 0;. for
10976 28 69 3d 30 3b 20 41 4c 57 41 59 53 28 69 3c 4e (i=0; ALWAYS(i<N
10977 5f 53 4f 52 54 5f 42 55 43 4b 45 54 2d 31 29 3b _SORT_BUCKET-1);
10978 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 i++){. if(
10979 20 61 5b 69 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 a[i]==0 ){.
1097a 20 20 20 20 61 5b 69 5d 20 3d 20 70 3b 0a 20 20 a[i] = p;.
1097b 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
1097c 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1097d 20 20 70 20 3d 20 70 63 61 63 68 65 4d 65 72 67 p = pcacheMerg
1097e 65 44 69 72 74 79 4c 69 73 74 28 61 5b 69 5d 2c eDirtyList(a[i],
1097f 20 70 29 3b 0a 20 20 20 20 20 20 20 20 61 5b 69 p);. a[i
10980 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 ] = 0;. }.
10981 20 20 20 7d 0a 20 20 20 20 69 66 28 20 4e 45 56 }. if( NEV
10982 45 52 28 69 3d 3d 4e 5f 53 4f 52 54 5f 42 55 43 ER(i==N_SORT_BUC
10983 4b 45 54 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 KET-1) ){.
10984 2f 2a 20 54 6f 20 67 65 74 20 68 65 72 65 2c 20 /* To get here,
10985 74 68 65 72 65 20 6e 65 65 64 20 74 6f 20 62 65 there need to be
10986 20 32 5e 28 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 2^(N_SORT_BUCKE
10987 54 29 20 65 6c 65 6d 65 6e 74 73 20 69 6e 0a 20 T) elements in.
10988 20 20 20 20 20 2a 2a 20 74 68 65 20 69 6e 70 75 ** the inpu
10989 74 20 6c 69 73 74 2e 20 20 42 75 74 20 74 68 61 t list. But tha
1098a 74 20 69 73 20 69 6d 70 6f 73 73 69 62 6c 65 2e t is impossible.
1098b 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
1098c 61 5b 69 5d 20 3d 20 70 63 61 63 68 65 4d 65 72 a[i] = pcacheMer
1098d 67 65 44 69 72 74 79 4c 69 73 74 28 61 5b 69 5d geDirtyList(a[i]
1098e 2c 20 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a , p);. }. }.
1098f 20 20 70 20 3d 20 61 5b 30 5d 3b 0a 20 20 66 6f p = a[0];. fo
10990 72 28 69 3d 31 3b 20 69 3c 4e 5f 53 4f 52 54 5f r(i=1; i<N_SORT_
10991 42 55 43 4b 45 54 3b 20 69 2b 2b 29 7b 0a 20 20 BUCKET; i++){.
10992 20 20 70 20 3d 20 70 63 61 63 68 65 4d 65 72 67 p = pcacheMerg
10993 65 44 69 72 74 79 4c 69 73 74 28 70 2c 20 61 5b eDirtyList(p, a[
10994 69 5d 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 i]);. }. retur
10995 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 n p;.}../*.** Re
10996 74 75 72 6e 20 61 20 6c 69 73 74 20 6f 66 20 61 turn a list of a
10997 6c 6c 20 64 69 72 74 79 20 70 61 67 65 73 20 69 ll dirty pages i
10998 6e 20 74 68 65 20 63 61 63 68 65 2c 20 73 6f 72 n the cache, sor
10999 74 65 64 20 62 79 20 70 61 67 65 20 6e 75 6d 62 ted by page numb
1099a 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 er..*/.SQLITE_PR
1099b 49 56 41 54 45 20 50 67 48 64 72 20 2a 73 71 6c IVATE PgHdr *sql
1099c 69 74 65 33 50 63 61 63 68 65 44 69 72 74 79 4c ite3PcacheDirtyL
1099d 69 73 74 28 50 43 61 63 68 65 20 2a 70 43 61 63 ist(PCache *pCac
1099e 68 65 29 7b 0a 20 20 50 67 48 64 72 20 2a 70 3b he){. PgHdr *p;
1099f 0a 20 20 66 6f 72 28 70 3d 70 43 61 63 68 65 2d . for(p=pCache-
109a0 3e 70 44 69 72 74 79 3b 20 70 3b 20 70 3d 70 2d >pDirty; p; p=p-
109a1 3e 70 44 69 72 74 79 4e 65 78 74 29 7b 0a 20 20 >pDirtyNext){.
109a2 20 20 70 2d 3e 70 44 69 72 74 79 20 3d 20 70 2d p->pDirty = p-
109a3 3e 70 44 69 72 74 79 4e 65 78 74 3b 0a 20 20 7d >pDirtyNext;. }
109a4 0a 20 20 72 65 74 75 72 6e 20 70 63 61 63 68 65 . return pcache
109a5 53 6f 72 74 44 69 72 74 79 4c 69 73 74 28 70 43 SortDirtyList(pC
109a6 61 63 68 65 2d 3e 70 44 69 72 74 79 29 3b 0a 7d ache->pDirty);.}
109a7 0a 0a 2f 2a 20 0a 2a 2a 20 52 65 74 75 72 6e 20 ../* .** Return
109a8 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 the total number
109a9 20 6f 66 20 72 65 66 65 72 65 6e 63 65 64 20 70 of referenced p
109aa 61 67 65 73 20 68 65 6c 64 20 62 79 20 74 68 65 ages held by the
109ab 20 63 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54 cache..*/.SQLIT
109ac 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
109ad 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 43 6f lite3PcacheRefCo
109ae 75 6e 74 28 50 43 61 63 68 65 20 2a 70 43 61 63 unt(PCache *pCac
109af 68 65 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 43 he){. return pC
109b0 61 63 68 65 2d 3e 6e 52 65 66 3b 0a 7d 0a 0a 2f ache->nRef;.}../
109b1 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
109b2 6e 75 6d 62 65 72 20 6f 66 20 72 65 66 65 72 65 number of refere
109b3 6e 63 65 73 20 74 6f 20 74 68 65 20 70 61 67 65 nces to the page
109b4 20 73 75 70 70 6c 69 65 64 20 61 73 20 61 6e 20 supplied as an
109b5 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c argument..*/.SQL
109b6 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
109b7 73 71 6c 69 74 65 33 50 63 61 63 68 65 50 61 67 sqlite3PcachePag
109b8 65 52 65 66 63 6f 75 6e 74 28 50 67 48 64 72 20 eRefcount(PgHdr
109b9 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 2d *p){. return p-
109ba 3e 6e 52 65 66 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a >nRef;.}../* .**
109bb 20 52 65 74 75 72 6e 20 74 68 65 20 74 6f 74 61 Return the tota
109bc 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 l number of page
109bd 73 20 69 6e 20 74 68 65 20 63 61 63 68 65 2e 0a s in the cache..
109be 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
109bf 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 E int sqlite3Pca
109c0 63 68 65 50 61 67 65 63 6f 75 6e 74 28 50 43 61 chePagecount(PCa
109c1 63 68 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 che *pCache){.
109c2 69 6e 74 20 6e 50 61 67 65 20 3d 20 30 3b 0a 20 int nPage = 0;.
109c3 20 69 66 28 20 70 43 61 63 68 65 2d 3e 70 43 61 if( pCache->pCa
109c4 63 68 65 20 29 7b 0a 20 20 20 20 6e 50 61 67 65 che ){. nPage
109c5 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c = sqlite3Global
109c6 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e 78 50 Config.pcache.xP
109c7 61 67 65 63 6f 75 6e 74 28 70 43 61 63 68 65 2d agecount(pCache-
109c8 3e 70 43 61 63 68 65 29 3b 0a 20 20 7d 0a 20 20 >pCache);. }.
109c9 72 65 74 75 72 6e 20 6e 50 61 67 65 3b 0a 7d 0a return nPage;.}.
109ca 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 .#ifdef SQLITE_T
109cb 45 53 54 0a 2f 2a 0a 2a 2a 20 47 65 74 20 74 68 EST./*.** Get th
109cc 65 20 73 75 67 67 65 73 74 65 64 20 63 61 63 68 e suggested cach
109cd 65 2d 73 69 7a 65 20 76 61 6c 75 65 2e 0a 2a 2f e-size value..*/
109ce 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
109cf 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 int sqlite3Pcach
109d0 65 47 65 74 43 61 63 68 65 73 69 7a 65 28 50 43 eGetCachesize(PC
109d1 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 ache *pCache){.
109d2 20 72 65 74 75 72 6e 20 70 43 61 63 68 65 2d 3e return pCache->
109d3 6e 4d 61 78 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a nMax;.}.#endif..
109d4 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 73 75 /*.** Set the su
109d5 67 67 65 73 74 65 64 20 63 61 63 68 65 2d 73 69 ggested cache-si
109d6 7a 65 20 76 61 6c 75 65 2e 0a 2a 2f 0a 53 51 4c ze value..*/.SQL
109d7 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
109d8 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 53 65 sqlite3PcacheSe
109d9 74 43 61 63 68 65 73 69 7a 65 28 50 43 61 63 68 tCachesize(PCach
109da 65 20 2a 70 43 61 63 68 65 2c 20 69 6e 74 20 6d e *pCache, int m
109db 78 50 61 67 65 29 7b 0a 20 20 70 43 61 63 68 65 xPage){. pCache
109dc 2d 3e 6e 4d 61 78 20 3d 20 6d 78 50 61 67 65 3b ->nMax = mxPage;
109dd 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e 70 . if( pCache->p
109de 43 61 63 68 65 20 29 7b 0a 20 20 20 20 73 71 6c Cache ){. sql
109df 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
109e0 2e 70 63 61 63 68 65 2e 78 43 61 63 68 65 73 69 .pcache.xCachesi
109e1 7a 65 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68 ze(pCache->pCach
109e2 65 2c 20 6d 78 50 61 67 65 29 3b 0a 20 20 7d 0a e, mxPage);. }.
109e3 7d 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 }..#if defined(S
109e4 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 QLITE_CHECK_PAGE
109e5 53 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 S) || defined(SQ
109e6 4c 49 54 45 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a LITE_DEBUG)./*.*
109e7 2a 20 46 6f 72 20 61 6c 6c 20 64 69 72 74 79 20 * For all dirty
109e8 70 61 67 65 73 20 63 75 72 72 65 6e 74 6c 79 20 pages currently
109e9 69 6e 20 74 68 65 20 63 61 63 68 65 2c 20 69 6e in the cache, in
109ea 76 6f 6b 65 20 74 68 65 20 73 70 65 63 69 66 69 voke the specifi
109eb 65 64 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 2e 20 ed.** callback.
109ec 54 68 69 73 20 69 73 20 6f 6e 6c 79 20 75 73 65 This is only use
109ed 64 20 69 66 20 74 68 65 20 53 51 4c 49 54 45 5f d if the SQLITE_
109ee 43 48 45 43 4b 5f 50 41 47 45 53 20 6d 61 63 72 CHECK_PAGES macr
109ef 6f 20 69 73 0a 2a 2a 20 64 65 66 69 6e 65 64 2e o is.** defined.
109f0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
109f1 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 TE void sqlite3P
109f2 63 61 63 68 65 49 74 65 72 61 74 65 44 69 72 74 cacheIterateDirt
109f3 79 28 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 y(PCache *pCache
109f4 2c 20 76 6f 69 64 20 28 2a 78 49 74 65 72 29 28 , void (*xIter)(
109f5 50 67 48 64 72 20 2a 29 29 7b 0a 20 20 50 67 48 PgHdr *)){. PgH
109f6 64 72 20 2a 70 44 69 72 74 79 3b 0a 20 20 66 6f dr *pDirty;. fo
109f7 72 28 70 44 69 72 74 79 3d 70 43 61 63 68 65 2d r(pDirty=pCache-
109f8 3e 70 44 69 72 74 79 3b 20 70 44 69 72 74 79 3b >pDirty; pDirty;
109f9 20 70 44 69 72 74 79 3d 70 44 69 72 74 79 2d 3e pDirty=pDirty->
109fa 70 44 69 72 74 79 4e 65 78 74 29 7b 0a 20 20 20 pDirtyNext){.
109fb 20 78 49 74 65 72 28 70 44 69 72 74 79 29 3b 0a xIter(pDirty);.
109fc 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a }.}.#endif../*
109fd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e ************* En
109fe 64 20 6f 66 20 70 63 61 63 68 65 2e 63 20 2a 2a d of pcache.c **
109ff 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10a00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10a01 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
10a02 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 ************* Be
10a03 67 69 6e 20 66 69 6c 65 20 70 63 61 63 68 65 31 gin file pcache1
10a04 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
10a05 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10a06 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
10a07 0a 2a 2a 20 32 30 30 38 20 4e 6f 76 65 6d 62 65 .** 2008 Novembe
10a08 72 20 30 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 r 05.**.** The a
10a09 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 uthor disclaims
10a0a 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 copyright to thi
10a0b 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 s source code.
10a0c 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 In place of.** a
10a0d 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 legal notice, h
10a0e 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e ere is a blessin
10a0f 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 g:.**.** May
10a10 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 you do good and
10a11 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 not evil..**
10a12 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 May you find for
10a13 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 giveness for you
10a14 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 rself and forgiv
10a15 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 e others..**
10a16 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 May you share fr
10a17 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 eely, never taki
10a18 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 ng more than you
10a19 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a give..**.******
10a1a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10a1b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10a1c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10a1d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10a1e 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 ***.**.** This f
10a1f 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 ile implements t
10a20 68 65 20 64 65 66 61 75 6c 74 20 70 61 67 65 20 he default page
10a21 63 61 63 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 cache implementa
10a22 74 69 6f 6e 20 28 74 68 65 0a 2a 2a 20 73 71 6c tion (the.** sql
10a23 69 74 65 33 5f 70 63 61 63 68 65 20 69 6e 74 65 ite3_pcache inte
10a24 72 66 61 63 65 29 2e 20 49 74 20 61 6c 73 6f 20 rface). It also
10a25 63 6f 6e 74 61 69 6e 73 20 70 61 72 74 20 6f 66 contains part of
10a26 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 the implementat
10a27 69 6f 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 53 51 ion.** of the SQ
10a28 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 LITE_CONFIG_PAGE
10a29 43 41 43 48 45 20 61 6e 64 20 73 71 6c 69 74 65 CACHE and sqlite
10a2a 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 3_release_memory
10a2b 28 29 20 66 65 61 74 75 72 65 73 2e 0a 2a 2a 20 () features..**
10a2c 49 66 20 74 68 65 20 64 65 66 61 75 6c 74 20 70 If the default p
10a2d 61 67 65 20 63 61 63 68 65 20 69 6d 70 6c 65 6d age cache implem
10a2e 65 6e 74 61 74 69 6f 6e 20 69 73 20 6f 76 65 72 entation is over
10a2f 72 69 64 65 6e 2c 20 74 68 65 6e 20 6e 65 69 74 riden, then neit
10a30 68 65 72 20 6f 66 0a 2a 2a 20 74 68 65 73 65 20 her of.** these
10a31 74 77 6f 20 66 65 61 74 75 72 65 73 20 61 72 65 two features are
10a32 20 61 76 61 69 6c 61 62 6c 65 2e 0a 2a 2a 0a 2a available..**.*
10a33 2a 20 40 28 23 29 20 24 49 64 3a 20 70 63 61 63 * @(#) $Id: pcac
10a34 68 65 31 2e 63 2c 76 20 31 2e 31 39 20 32 30 30 he1.c,v 1.19 200
10a35 39 2f 30 37 2f 31 37 20 31 31 3a 34 34 3a 30 37 9/07/17 11:44:07
10a36 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a drh Exp $.*/...
10a37 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 50 typedef struct P
10a38 43 61 63 68 65 31 20 50 43 61 63 68 65 31 3b 0a Cache1 PCache1;.
10a39 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 50 typedef struct P
10a3a 67 48 64 72 31 20 50 67 48 64 72 31 3b 0a 74 79 gHdr1 PgHdr1;.ty
10a3b 70 65 64 65 66 20 73 74 72 75 63 74 20 50 67 46 pedef struct PgF
10a3c 72 65 65 73 6c 6f 74 20 50 67 46 72 65 65 73 6c reeslot PgFreesl
10a3d 6f 74 3b 0a 0a 2f 2a 20 50 6f 69 6e 74 65 72 73 ot;../* Pointers
10a3e 20 74 6f 20 73 74 72 75 63 74 75 72 65 73 20 6f to structures o
10a3f 66 20 74 68 69 73 20 74 79 70 65 20 61 72 65 20 f this type are
10a40 63 61 73 74 20 61 6e 64 20 72 65 74 75 72 6e 65 cast and returne
10a41 64 20 61 73 20 0a 2a 2a 20 6f 70 61 71 75 65 20 d as .** opaque
10a42 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2a 20 sqlite3_pcache*
10a43 68 61 6e 64 6c 65 73 0a 2a 2f 0a 73 74 72 75 63 handles.*/.struc
10a44 74 20 50 43 61 63 68 65 31 20 7b 0a 20 20 2f 2a t PCache1 {. /*
10a45 20 43 61 63 68 65 20 63 6f 6e 66 69 67 75 72 61 Cache configura
10a46 74 69 6f 6e 20 70 61 72 61 6d 65 74 65 72 73 2e tion parameters.
10a47 20 50 61 67 65 20 73 69 7a 65 20 28 73 7a 50 61 Page size (szPa
10a48 67 65 29 20 61 6e 64 20 74 68 65 20 70 75 72 67 ge) and the purg
10a49 65 61 62 6c 65 0a 20 20 2a 2a 20 66 6c 61 67 20 eable. ** flag
10a4a 28 62 50 75 72 67 65 61 62 6c 65 29 20 61 72 65 (bPurgeable) are
10a4b 20 73 65 74 20 77 68 65 6e 20 74 68 65 20 63 61 set when the ca
10a4c 63 68 65 20 69 73 20 63 72 65 61 74 65 64 2e 20 che is created.
10a4d 6e 4d 61 78 20 6d 61 79 20 62 65 20 0a 20 20 2a nMax may be . *
10a4e 2a 20 6d 6f 64 69 66 69 65 64 20 61 74 20 61 6e * modified at an
10a4f 79 20 74 69 6d 65 20 62 79 20 61 20 63 61 6c 6c y time by a call
10a50 20 74 6f 20 74 68 65 20 70 63 61 63 68 65 31 43 to the pcache1C
10a51 61 63 68 65 53 69 7a 65 28 29 20 6d 65 74 68 6f acheSize() metho
10a52 64 2e 0a 20 20 2a 2a 20 54 68 65 20 67 6c 6f 62 d.. ** The glob
10a53 61 6c 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 al mutex must be
10a54 20 68 65 6c 64 20 77 68 65 6e 20 61 63 63 65 73 held when acces
10a55 73 69 6e 67 20 6e 4d 61 78 2e 0a 20 20 2a 2f 0a sing nMax.. */.
10a56 20 20 69 6e 74 20 73 7a 50 61 67 65 3b 20 20 20 int szPage;
10a57 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10a58 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 /* Size of
10a59 20 61 6c 6c 6f 63 61 74 65 64 20 70 61 67 65 73 allocated pages
10a5a 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 69 in bytes */. i
10a5b 6e 74 20 62 50 75 72 67 65 61 62 6c 65 3b 20 20 nt bPurgeable;
10a5c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10a5d 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 63 61 /* True if ca
10a5e 63 68 65 20 69 73 20 70 75 72 67 65 61 62 6c 65 che is purgeable
10a5f 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 */. unsigned i
10a60 6e 74 20 6e 4d 69 6e 3b 20 20 20 20 20 20 20 20 nt nMin;
10a61 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 69 6e /* Min
10a62 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 70 imum number of p
10a63 61 67 65 73 20 72 65 73 65 72 76 65 64 20 2a 2f ages reserved */
10a64 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 . unsigned int
10a65 6e 4d 61 78 3b 20 20 20 20 20 20 20 20 20 20 20 nMax;
10a66 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 66 69 67 /* Config
10a67 75 72 65 64 20 22 63 61 63 68 65 5f 73 69 7a 65 ured "cache_size
10a68 22 20 76 61 6c 75 65 20 2a 2f 0a 0a 20 20 2f 2a " value */.. /*
10a69 20 48 61 73 68 20 74 61 62 6c 65 20 6f 66 20 61 Hash table of a
10a6a 6c 6c 20 70 61 67 65 73 2e 20 54 68 65 20 66 6f ll pages. The fo
10a6b 6c 6c 6f 77 69 6e 67 20 76 61 72 69 61 62 6c 65 llowing variable
10a6c 73 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 61 63 s may only be ac
10a6d 63 65 73 73 65 64 0a 20 20 2a 2a 20 77 68 65 6e cessed. ** when
10a6e 20 74 68 65 20 61 63 63 65 73 73 6f 72 20 69 73 the accessor is
10a6f 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 67 6c 6f holding the glo
10a70 62 61 6c 20 6d 75 74 65 78 20 28 73 65 65 20 70 bal mutex (see p
10a71 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 cache1EnterMutex
10a72 28 29 20 0a 20 20 2a 2a 20 61 6e 64 20 70 63 61 () . ** and pca
10a73 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78 28 29 che1LeaveMutex()
10a74 29 2e 0a 20 20 2a 2f 0a 20 20 75 6e 73 69 67 6e ).. */. unsign
10a75 65 64 20 69 6e 74 20 6e 52 65 63 79 63 6c 61 62 ed int nRecyclab
10a76 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a le; /*
10a77 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 Number of pages
10a78 20 69 6e 20 74 68 65 20 4c 52 55 20 6c 69 73 74 in the LRU list
10a79 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 */. unsigned i
10a7a 6e 74 20 6e 50 61 67 65 3b 20 20 20 20 20 20 20 nt nPage;
10a7b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 /* Tot
10a7c 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 al number of pag
10a7d 65 73 20 69 6e 20 61 70 48 61 73 68 20 2a 2f 0a es in apHash */.
10a7e 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e unsigned int n
10a7f 48 61 73 68 3b 20 20 20 20 20 20 20 20 20 20 20 Hash;
10a80 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
10a81 6f 66 20 73 6c 6f 74 73 20 69 6e 20 61 70 48 61 of slots in apHa
10a82 73 68 5b 5d 20 2a 2f 0a 20 20 50 67 48 64 72 31 sh[] */. PgHdr1
10a83 20 2a 2a 61 70 48 61 73 68 3b 20 20 20 20 20 20 **apHash;
10a84 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
10a85 20 48 61 73 68 20 74 61 62 6c 65 20 66 6f 72 20 Hash table for
10a86 66 61 73 74 20 6c 6f 6f 6b 75 70 20 62 79 20 6b fast lookup by k
10a87 65 79 20 2a 2f 0a 0a 20 20 75 6e 73 69 67 6e 65 ey */.. unsigne
10a88 64 20 69 6e 74 20 69 4d 61 78 4b 65 79 3b 20 20 d int iMaxKey;
10a89 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
10a8a 4c 61 72 67 65 73 74 20 6b 65 79 20 73 65 65 6e Largest key seen
10a8b 20 73 69 6e 63 65 20 78 54 72 75 6e 63 61 74 65 since xTruncate
10a8c 28 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 () */.};../*.**
10a8d 45 61 63 68 20 63 61 63 68 65 20 65 6e 74 72 79 Each cache entry
10a8e 20 69 73 20 72 65 70 72 65 73 65 6e 74 65 64 20 is represented
10a8f 62 79 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f by an instance o
10a90 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
10a91 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 20 41 .** structure. A
10a92 20 62 75 66 66 65 72 20 6f 66 20 50 67 48 64 72 buffer of PgHdr
10a93 31 2e 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 1.pCache->szPage
10a94 20 62 79 74 65 73 20 69 73 20 61 6c 6c 6f 63 61 bytes is alloca
10a95 74 65 64 20 0a 2a 2a 20 64 69 72 65 63 74 6c 79 ted .** directly
10a96 20 62 65 66 6f 72 65 20 74 68 69 73 20 73 74 72 before this str
10a97 75 63 74 75 72 65 20 69 6e 20 6d 65 6d 6f 72 79 ucture in memory
10a98 20 28 73 65 65 20 74 68 65 20 50 47 48 44 52 31 (see the PGHDR1
10a99 5f 54 4f 5f 50 41 47 45 28 29 20 0a 2a 2a 20 6d _TO_PAGE() .** m
10a9a 61 63 72 6f 20 62 65 6c 6f 77 29 2e 0a 2a 2f 0a acro below)..*/.
10a9b 73 74 72 75 63 74 20 50 67 48 64 72 31 20 7b 0a struct PgHdr1 {.
10a9c 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 unsigned int i
10a9d 4b 65 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 Key;
10a9e 20 2f 2a 20 4b 65 79 20 76 61 6c 75 65 20 28 70 /* Key value (p
10a9f 61 67 65 20 6e 75 6d 62 65 72 29 20 2a 2f 0a 20 age number) */.
10aa0 20 50 67 48 64 72 31 20 2a 70 4e 65 78 74 3b 20 PgHdr1 *pNext;
10aa1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10aa2 2f 2a 20 4e 65 78 74 20 69 6e 20 68 61 73 68 20 /* Next in hash
10aa3 74 61 62 6c 65 20 63 68 61 69 6e 20 2a 2f 0a 20 table chain */.
10aa4 20 50 43 61 63 68 65 31 20 2a 70 43 61 63 68 65 PCache1 *pCache
10aa5 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
10aa6 2f 2a 20 43 61 63 68 65 20 74 68 61 74 20 63 75 /* Cache that cu
10aa7 72 72 65 6e 74 6c 79 20 6f 77 6e 73 20 74 68 69 rrently owns thi
10aa8 73 20 70 61 67 65 20 2a 2f 0a 20 20 50 67 48 64 s page */. PgHd
10aa9 72 31 20 2a 70 4c 72 75 4e 65 78 74 3b 20 20 20 r1 *pLruNext;
10aaa 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 /* Ne
10aab 78 74 20 69 6e 20 4c 52 55 20 6c 69 73 74 20 6f xt in LRU list o
10aac 66 20 75 6e 70 69 6e 6e 65 64 20 70 61 67 65 73 f unpinned pages
10aad 20 2a 2f 0a 20 20 50 67 48 64 72 31 20 2a 70 4c */. PgHdr1 *pL
10aae 72 75 50 72 65 76 3b 20 20 20 20 20 20 20 20 20 ruPrev;
10aaf 20 20 20 20 20 2f 2a 20 50 72 65 76 69 6f 75 73 /* Previous
10ab0 20 69 6e 20 4c 52 55 20 6c 69 73 74 20 6f 66 20 in LRU list of
10ab1 75 6e 70 69 6e 6e 65 64 20 70 61 67 65 73 20 2a unpinned pages *
10ab2 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 /.};../*.** Free
10ab3 20 73 6c 6f 74 73 20 69 6e 20 74 68 65 20 61 6c slots in the al
10ab4 6c 6f 63 61 74 6f 72 20 75 73 65 64 20 74 6f 20 locator used to
10ab5 64 69 76 69 64 65 20 75 70 20 74 68 65 20 62 75 divide up the bu
10ab6 66 66 65 72 20 70 72 6f 76 69 64 65 64 20 75 73 ffer provided us
10ab7 69 6e 67 0a 2a 2a 20 74 68 65 20 53 51 4c 49 54 ing.** the SQLIT
10ab8 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 E_CONFIG_PAGECAC
10ab9 48 45 20 6d 65 63 68 61 6e 69 73 6d 2e 0a 2a 2f HE mechanism..*/
10aba 0a 73 74 72 75 63 74 20 50 67 46 72 65 65 73 6c .struct PgFreesl
10abb 6f 74 20 7b 0a 20 20 50 67 46 72 65 65 73 6c 6f ot {. PgFreeslo
10abc 74 20 2a 70 4e 65 78 74 3b 20 20 2f 2a 20 4e 65 t *pNext; /* Ne
10abd 78 74 20 66 72 65 65 20 73 6c 6f 74 20 2a 2f 0a xt free slot */.
10abe 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 47 6c 6f 62 61 6c };../*.** Global
10abf 20 64 61 74 61 20 75 73 65 64 20 62 79 20 74 68 data used by th
10ac0 69 73 20 63 61 63 68 65 2e 0a 2a 2f 0a 73 74 61 is cache..*/.sta
10ac1 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44 20 73 tic SQLITE_WSD s
10ac2 74 72 75 63 74 20 50 43 61 63 68 65 47 6c 6f 62 truct PCacheGlob
10ac3 61 6c 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d al {. sqlite3_m
10ac4 75 74 65 78 20 2a 6d 75 74 65 78 3b 20 20 20 20 utex *mutex;
10ac5 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 74 /* st
10ac6 61 74 69 63 20 6d 75 74 65 78 20 4d 55 54 45 58 atic mutex MUTEX
10ac7 5f 53 54 41 54 49 43 5f 4c 52 55 20 2a 2f 0a 0a _STATIC_LRU */..
10ac8 20 20 69 6e 74 20 6e 4d 61 78 50 61 67 65 3b 20 int nMaxPage;
10ac9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10aca 20 20 20 20 20 20 2f 2a 20 53 75 6d 20 6f 66 20 /* Sum of
10acb 6e 4d 61 78 50 61 67 65 20 66 6f 72 20 70 75 72 nMaxPage for pur
10acc 67 65 61 62 6c 65 20 63 61 63 68 65 73 20 2a 2f geable caches */
10acd 0a 20 20 69 6e 74 20 6e 4d 69 6e 50 61 67 65 3b . int nMinPage;
10ace 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10acf 20 20 20 20 20 20 20 2f 2a 20 53 75 6d 20 6f 66 /* Sum of
10ad0 20 6e 4d 69 6e 50 61 67 65 20 66 6f 72 20 70 75 nMinPage for pu
10ad1 72 67 65 61 62 6c 65 20 63 61 63 68 65 73 20 2a rgeable caches *
10ad2 2f 0a 20 20 69 6e 74 20 6e 43 75 72 72 65 6e 74 /. int nCurrent
10ad3 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 Page;
10ad4 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
10ad5 72 20 6f 66 20 70 75 72 67 65 61 62 6c 65 20 70 r of purgeable p
10ad6 61 67 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 2a ages allocated *
10ad7 2f 0a 20 20 50 67 48 64 72 31 20 2a 70 4c 72 75 /. PgHdr1 *pLru
10ad8 48 65 61 64 2c 20 2a 70 4c 72 75 54 61 69 6c 3b Head, *pLruTail;
10ad9 20 20 20 20 20 20 20 20 2f 2a 20 4c 52 55 20 6c /* LRU l
10ada 69 73 74 20 6f 66 20 75 6e 70 69 6e 6e 65 64 20 ist of unpinned
10adb 70 61 67 65 73 20 2a 2f 0a 0a 20 20 2f 2a 20 56 pages */.. /* V
10adc 61 72 69 61 62 6c 65 73 20 72 65 6c 61 74 65 64 ariables related
10add 20 74 6f 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 to SQLITE_CONFI
10ade 47 5f 50 41 47 45 43 41 43 48 45 20 73 65 74 74 G_PAGECACHE sett
10adf 69 6e 67 73 2e 20 2a 2f 0a 20 20 69 6e 74 20 73 ings. */. int s
10ae0 7a 53 6c 6f 74 3b 20 20 20 20 20 20 20 20 20 20 zSlot;
10ae1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
10ae2 2a 20 53 69 7a 65 20 6f 66 20 65 61 63 68 20 66 * Size of each f
10ae3 72 65 65 20 73 6c 6f 74 20 2a 2f 0a 20 20 76 6f ree slot */. vo
10ae4 69 64 20 2a 70 53 74 61 72 74 2c 20 2a 70 45 6e id *pStart, *pEn
10ae5 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 d;
10ae6 20 20 2f 2a 20 42 6f 75 6e 64 73 20 6f 66 20 70 /* Bounds of p
10ae7 61 67 65 63 61 63 68 65 20 6d 61 6c 6c 6f 63 20 agecache malloc
10ae8 72 61 6e 67 65 20 2a 2f 0a 20 20 50 67 46 72 65 range */. PgFre
10ae9 65 73 6c 6f 74 20 2a 70 46 72 65 65 3b 20 20 20 eslot *pFree;
10aea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
10aeb 2a 20 46 72 65 65 20 70 61 67 65 20 62 6c 6f 63 * Free page bloc
10aec 6b 73 20 2a 2f 0a 20 20 69 6e 74 20 69 73 49 6e ks */. int isIn
10aed 69 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 it;
10aee 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
10aef 72 75 65 20 69 66 20 69 6e 69 74 69 61 6c 69 7a rue if initializ
10af0 65 64 20 2a 2f 0a 7d 20 70 63 61 63 68 65 31 5f ed */.} pcache1_
10af1 67 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 63 6f g;../*.** All co
10af2 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 de in this file
10af3 73 68 6f 75 6c 64 20 61 63 63 65 73 73 20 74 68 should access th
10af4 65 20 67 6c 6f 62 61 6c 20 73 74 72 75 63 74 75 e global structu
10af5 72 65 20 61 62 6f 76 65 20 76 69 61 20 74 68 65 re above via the
10af6 0a 2a 2a 20 61 6c 69 61 73 20 22 70 63 61 63 68 .** alias "pcach
10af7 65 31 22 2e 20 54 68 69 73 20 65 6e 73 75 72 65 e1". This ensure
10af8 73 20 74 68 61 74 20 74 68 65 20 57 53 44 20 65 s that the WSD e
10af9 6d 75 6c 61 74 69 6f 6e 20 69 73 20 75 73 65 64 mulation is used
10afa 20 77 68 65 6e 0a 2a 2a 20 63 6f 6d 70 69 6c 69 when.** compili
10afb 6e 67 20 66 6f 72 20 73 79 73 74 65 6d 73 20 74 ng for systems t
10afc 68 61 74 20 64 6f 20 6e 6f 74 20 73 75 70 70 6f hat do not suppo
10afd 72 74 20 72 65 61 6c 20 57 53 44 2e 0a 2a 2f 0a rt real WSD..*/.
10afe 23 64 65 66 69 6e 65 20 70 63 61 63 68 65 31 20 #define pcache1
10aff 28 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 50 (GLOBAL(struct P
10b00 43 61 63 68 65 47 6c 6f 62 61 6c 2c 20 70 63 61 CacheGlobal, pca
10b01 63 68 65 31 5f 67 29 29 0a 0a 2f 2a 0a 2a 2a 20 che1_g))../*.**
10b02 57 68 65 6e 20 61 20 50 67 48 64 72 31 20 73 74 When a PgHdr1 st
10b03 72 75 63 74 75 72 65 20 69 73 20 61 6c 6c 6f 63 ructure is alloc
10b04 61 74 65 64 2c 20 74 68 65 20 61 73 73 6f 63 69 ated, the associ
10b05 61 74 65 64 20 50 43 61 63 68 65 31 2e 73 7a 50 ated PCache1.szP
10b06 61 67 65 0a 2a 2a 20 62 79 74 65 73 20 6f 66 20 age.** bytes of
10b07 64 61 74 61 20 61 72 65 20 6c 6f 63 61 74 65 64 data are located
10b08 20 64 69 72 65 63 74 6c 79 20 62 65 66 6f 72 65 directly before
10b09 20 69 74 20 69 6e 20 6d 65 6d 6f 72 79 20 28 69 it in memory (i
10b0a 2e 65 2e 20 74 68 65 20 74 6f 74 61 6c 0a 2a 2a .e. the total.**
10b0b 20 73 69 7a 65 20 6f 66 20 74 68 65 20 61 6c 6c size of the all
10b0c 6f 63 61 74 69 6f 6e 20 69 73 20 73 69 7a 65 6f ocation is sizeo
10b0d 66 28 50 67 48 64 72 31 29 2b 50 43 61 63 68 65 f(PgHdr1)+PCache
10b0e 31 2e 73 7a 50 61 67 65 20 62 79 74 65 29 2e 20 1.szPage byte).
10b0f 54 68 65 0a 2a 2a 20 50 47 48 44 52 31 5f 54 4f The.** PGHDR1_TO
10b10 5f 50 41 47 45 28 29 20 6d 61 63 72 6f 20 74 61 _PAGE() macro ta
10b11 6b 65 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f kes a pointer to
10b12 20 61 20 50 67 48 64 72 31 20 73 74 72 75 63 74 a PgHdr1 struct
10b13 75 72 65 20 61 73 0a 2a 2a 20 61 6e 20 61 72 67 ure as.** an arg
10b14 75 6d 65 6e 74 20 61 6e 64 20 72 65 74 75 72 6e ument and return
10b15 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 s a pointer to t
10b16 68 65 20 61 73 73 6f 63 69 61 74 65 64 20 62 6c he associated bl
10b17 6f 63 6b 20 6f 66 20 73 7a 50 61 67 65 0a 2a 2a ock of szPage.**
10b18 20 62 79 74 65 73 2e 20 54 68 65 20 50 41 47 45 bytes. The PAGE
10b19 5f 54 4f 5f 50 47 48 44 52 31 28 29 20 6d 61 63 _TO_PGHDR1() mac
10b1a 72 6f 20 64 6f 65 73 20 74 68 65 20 6f 70 70 6f ro does the oppo
10b1b 73 69 74 65 3a 20 69 74 73 20 61 72 67 75 6d 65 site: its argume
10b1c 6e 74 20 69 73 0a 2a 2a 20 61 20 70 6f 69 6e 74 nt is.** a point
10b1d 65 72 20 74 6f 20 61 20 62 6c 6f 63 6b 20 6f 66 er to a block of
10b1e 20 73 7a 50 61 67 65 20 62 79 74 65 73 20 6f 66 szPage bytes of
10b1f 20 64 61 74 61 20 61 6e 64 20 74 68 65 20 72 65 data and the re
10b20 74 75 72 6e 20 76 61 6c 75 65 20 69 73 0a 2a 2a turn value is.**
10b21 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 a pointer to th
10b22 65 20 61 73 73 6f 63 69 61 74 65 64 20 50 67 48 e associated PgH
10b23 64 72 31 20 73 74 72 75 63 74 75 72 65 2e 0a 2a dr1 structure..*
10b24 2a 0a 2a 2a 20 20 20 61 73 73 65 72 74 28 20 50 *.** assert( P
10b25 47 48 44 52 31 5f 54 4f 5f 50 41 47 45 28 50 41 GHDR1_TO_PAGE(PA
10b26 47 45 5f 54 4f 5f 50 47 48 44 52 31 28 70 43 61 GE_TO_PGHDR1(pCa
10b27 63 68 65 2c 20 58 29 29 3d 3d 58 20 29 3b 0a 2a che, X))==X );.*
10b28 2f 0a 23 64 65 66 69 6e 65 20 50 47 48 44 52 31 /.#define PGHDR1
10b29 5f 54 4f 5f 50 41 47 45 28 70 29 20 20 20 20 28 _TO_PAGE(p) (
10b2a 76 6f 69 64 2a 29 28 28 28 63 68 61 72 2a 29 70 void*)(((char*)p
10b2b 29 20 2d 20 70 2d 3e 70 43 61 63 68 65 2d 3e 73 ) - p->pCache->s
10b2c 7a 50 61 67 65 29 0a 23 64 65 66 69 6e 65 20 50 zPage).#define P
10b2d 41 47 45 5f 54 4f 5f 50 47 48 44 52 31 28 63 2c AGE_TO_PGHDR1(c,
10b2e 20 70 29 20 28 50 67 48 64 72 31 2a 29 28 28 28 p) (PgHdr1*)(((
10b2f 63 68 61 72 2a 29 70 29 20 2b 20 63 2d 3e 73 7a char*)p) + c->sz
10b30 50 61 67 65 29 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 Page)../*.** Mac
10b31 72 6f 73 20 74 6f 20 65 6e 74 65 72 20 61 6e 64 ros to enter and
10b32 20 6c 65 61 76 65 20 74 68 65 20 67 6c 6f 62 61 leave the globa
10b33 6c 20 4c 52 55 20 6d 75 74 65 78 2e 0a 2a 2f 0a l LRU mutex..*/.
10b34 23 64 65 66 69 6e 65 20 70 63 61 63 68 65 31 45 #define pcache1E
10b35 6e 74 65 72 4d 75 74 65 78 28 29 20 73 71 6c 69 nterMutex() sqli
10b36 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 te3_mutex_enter(
10b37 70 63 61 63 68 65 31 2e 6d 75 74 65 78 29 0a 23 pcache1.mutex).#
10b38 64 65 66 69 6e 65 20 70 63 61 63 68 65 31 4c 65 define pcache1Le
10b39 61 76 65 4d 75 74 65 78 28 29 20 73 71 6c 69 74 aveMutex() sqlit
10b3a 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 e3_mutex_leave(p
10b3b 63 61 63 68 65 31 2e 6d 75 74 65 78 29 0a 0a 2f cache1.mutex)../
10b3c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10b3d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10b3e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10b3f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10b40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
10b41 2f 2a 2a 2a 2a 2a 2a 2a 2a 20 50 61 67 65 20 41 /******** Page A
10b42 6c 6c 6f 63 61 74 69 6f 6e 2f 53 51 4c 49 54 45 llocation/SQLITE
10b43 5f 43 4f 4e 46 49 47 5f 50 43 41 43 48 45 20 52 _CONFIG_PCACHE R
10b44 65 6c 61 74 65 64 20 46 75 6e 63 74 69 6f 6e 73 elated Functions
10b45 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f **************/
10b46 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e ../*.** This fun
10b47 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 ction is called
10b48 64 75 72 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a during initializ
10b49 61 74 69 6f 6e 20 69 66 20 61 20 73 74 61 74 69 ation if a stati
10b4a 63 20 62 75 66 66 65 72 20 69 73 20 0a 2a 2a 20 c buffer is .**
10b4b 73 75 70 70 6c 69 65 64 20 74 6f 20 75 73 65 20 supplied to use
10b4c 66 6f 72 20 74 68 65 20 70 61 67 65 2d 63 61 63 for the page-cac
10b4d 68 65 20 62 79 20 70 61 73 73 69 6e 67 20 74 68 he by passing th
10b4e 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f e SQLITE_CONFIG_
10b4f 50 41 47 45 43 41 43 48 45 0a 2a 2a 20 76 65 72 PAGECACHE.** ver
10b50 62 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6e b to sqlite3_con
10b51 66 69 67 28 29 2e 20 50 61 72 61 6d 65 74 65 72 fig(). Parameter
10b52 20 70 42 75 66 20 70 6f 69 6e 74 73 20 74 6f 20 pBuf points to
10b53 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6c 61 an allocation la
10b54 72 67 65 0a 2a 2a 20 65 6e 6f 75 67 68 20 74 6f rge.** enough to
10b55 20 63 6f 6e 74 61 69 6e 20 27 6e 27 20 62 75 66 contain 'n' buf
10b56 66 65 72 73 20 6f 66 20 27 73 7a 27 20 62 79 74 fers of 'sz' byt
10b57 65 73 20 65 61 63 68 2e 0a 2a 2f 0a 53 51 4c 49 es each..*/.SQLI
10b58 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
10b59 73 71 6c 69 74 65 33 50 43 61 63 68 65 42 75 66 sqlite3PCacheBuf
10b5a 66 65 72 53 65 74 75 70 28 76 6f 69 64 20 2a 70 ferSetup(void *p
10b5b 42 75 66 2c 20 69 6e 74 20 73 7a 2c 20 69 6e 74 Buf, int sz, int
10b5c 20 6e 29 7b 0a 20 20 69 66 28 20 70 63 61 63 68 n){. if( pcach
10b5d 65 31 2e 69 73 49 6e 69 74 20 29 7b 0a 20 20 20 e1.isInit ){.
10b5e 20 50 67 46 72 65 65 73 6c 6f 74 20 2a 70 3b 0a PgFreeslot *p;.
10b5f 20 20 20 20 73 7a 20 3d 20 52 4f 55 4e 44 44 4f sz = ROUNDDO
10b60 57 4e 38 28 73 7a 29 3b 0a 20 20 20 20 70 63 61 WN8(sz);. pca
10b61 63 68 65 31 2e 73 7a 53 6c 6f 74 20 3d 20 73 7a che1.szSlot = sz
10b62 3b 0a 20 20 20 20 70 63 61 63 68 65 31 2e 70 53 ;. pcache1.pS
10b63 74 61 72 74 20 3d 20 70 42 75 66 3b 0a 20 20 20 tart = pBuf;.
10b64 20 70 63 61 63 68 65 31 2e 70 46 72 65 65 20 3d pcache1.pFree =
10b65 20 30 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 6e 0;. while( n
10b66 2d 2d 20 29 7b 0a 20 20 20 20 20 20 70 20 3d 20 -- ){. p =
10b67 28 50 67 46 72 65 65 73 6c 6f 74 2a 29 70 42 75 (PgFreeslot*)pBu
10b68 66 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4e 65 78 f;. p->pNex
10b69 74 20 3d 20 70 63 61 63 68 65 31 2e 70 46 72 65 t = pcache1.pFre
10b6a 65 3b 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 e;. pcache1
10b6b 2e 70 46 72 65 65 20 3d 20 70 3b 0a 20 20 20 20 .pFree = p;.
10b6c 20 20 70 42 75 66 20 3d 20 28 76 6f 69 64 2a 29 pBuf = (void*)
10b6d 26 28 28 63 68 61 72 2a 29 70 42 75 66 29 5b 73 &((char*)pBuf)[s
10b6e 7a 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 63 z];. }. pc
10b6f 61 63 68 65 31 2e 70 45 6e 64 20 3d 20 70 42 75 ache1.pEnd = pBu
10b70 66 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 f;. }.}../*.**
10b71 4d 61 6c 6c 6f 63 20 66 75 6e 63 74 69 6f 6e 20 Malloc function
10b72 75 73 65 64 20 77 69 74 68 69 6e 20 74 68 69 73 used within this
10b73 20 66 69 6c 65 20 74 6f 20 61 6c 6c 6f 63 61 74 file to allocat
10b74 65 20 73 70 61 63 65 20 66 72 6f 6d 20 74 68 65 e space from the
10b75 20 62 75 66 66 65 72 0a 2a 2a 20 63 6f 6e 66 69 buffer.** confi
10b76 67 75 72 65 64 20 75 73 69 6e 67 20 73 71 6c 69 gured using sqli
10b77 74 65 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54 te3_config(SQLIT
10b78 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 E_CONFIG_PAGECAC
10b79 48 45 29 20 6f 70 74 69 6f 6e 2e 20 49 66 20 6e HE) option. If n
10b7a 6f 20 0a 2a 2a 20 73 75 63 68 20 62 75 66 66 65 o .** such buffe
10b7b 72 20 65 78 69 73 74 73 20 6f 72 20 74 68 65 72 r exists or ther
10b7c 65 20 69 73 20 6e 6f 20 73 70 61 63 65 20 6c 65 e is no space le
10b7d 66 74 20 69 6e 20 69 74 2c 20 74 68 69 73 20 66 ft in it, this f
10b7e 75 6e 63 74 69 6f 6e 20 66 61 6c 6c 73 20 0a 2a unction falls .*
10b7f 2a 20 62 61 63 6b 20 74 6f 20 73 71 6c 69 74 65 * back to sqlite
10b80 33 4d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 3Malloc()..*/.st
10b81 61 74 69 63 20 76 6f 69 64 20 2a 70 63 61 63 68 atic void *pcach
10b82 65 31 41 6c 6c 6f 63 28 69 6e 74 20 6e 42 79 74 e1Alloc(int nByt
10b83 65 29 7b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a 20 e){. void *p;.
10b84 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
10b85 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 63 61 63 _mutex_held(pcac
10b86 68 65 31 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 he1.mutex) );.
10b87 69 66 28 20 6e 42 79 74 65 3c 3d 70 63 61 63 68 if( nByte<=pcach
10b88 65 31 2e 73 7a 53 6c 6f 74 20 26 26 20 70 63 61 e1.szSlot && pca
10b89 63 68 65 31 2e 70 46 72 65 65 20 29 7b 0a 20 20 che1.pFree ){.
10b8a 20 20 61 73 73 65 72 74 28 20 70 63 61 63 68 65 assert( pcache
10b8b 31 2e 69 73 49 6e 69 74 20 29 3b 0a 20 20 20 20 1.isInit );.
10b8c 70 20 3d 20 28 50 67 48 64 72 31 20 2a 29 70 63 p = (PgHdr1 *)pc
10b8d 61 63 68 65 31 2e 70 46 72 65 65 3b 0a 20 20 20 ache1.pFree;.
10b8e 20 70 63 61 63 68 65 31 2e 70 46 72 65 65 20 3d pcache1.pFree =
10b8f 20 70 63 61 63 68 65 31 2e 70 46 72 65 65 2d 3e pcache1.pFree->
10b90 70 4e 65 78 74 3b 0a 20 20 20 20 73 71 6c 69 74 pNext;. sqlit
10b91 65 33 53 74 61 74 75 73 53 65 74 28 53 51 4c 49 e3StatusSet(SQLI
10b92 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45 43 41 TE_STATUS_PAGECA
10b93 43 48 45 5f 53 49 5a 45 2c 20 6e 42 79 74 65 29 CHE_SIZE, nByte)
10b94 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 ;. sqlite3Sta
10b95 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 tusAdd(SQLITE_ST
10b96 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 55 ATUS_PAGECACHE_U
10b97 53 45 44 2c 20 31 29 3b 0a 20 20 7d 65 6c 73 65 SED, 1);. }else
10b98 7b 0a 0a 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 {.. /* Alloca
10b99 74 65 20 61 20 6e 65 77 20 62 75 66 66 65 72 20 te a new buffer
10b9a 75 73 69 6e 67 20 73 71 6c 69 74 65 33 4d 61 6c using sqlite3Mal
10b9b 6c 6f 63 2e 20 42 65 66 6f 72 65 20 64 6f 69 6e loc. Before doin
10b9c 67 20 73 6f 2c 20 65 78 69 74 20 74 68 65 0a 20 g so, exit the.
10b9d 20 20 20 2a 2a 20 67 6c 6f 62 61 6c 20 70 63 61 ** global pca
10b9e 63 68 65 20 6d 75 74 65 78 20 61 6e 64 20 75 6e che mutex and un
10b9f 6c 6f 63 6b 20 74 68 65 20 70 61 67 65 72 2d 63 lock the pager-c
10ba0 61 63 68 65 20 6f 62 6a 65 63 74 20 70 43 61 63 ache object pCac
10ba1 68 65 2e 20 54 68 69 73 20 69 73 20 0a 20 20 20 he. This is .
10ba2 20 2a 2a 20 73 6f 20 74 68 61 74 20 69 66 20 74 ** so that if t
10ba3 68 65 20 61 74 74 65 6d 70 74 20 74 6f 20 61 6c he attempt to al
10ba4 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 62 75 66 locate a new buf
10ba5 66 65 72 20 63 61 75 73 65 73 20 74 68 65 20 74 fer causes the t
10ba6 68 65 20 0a 20 20 20 20 2a 2a 20 63 6f 6e 66 69 he . ** confi
10ba7 67 75 72 65 64 20 73 6f 66 74 2d 68 65 61 70 2d gured soft-heap-
10ba8 6c 69 6d 69 74 20 74 6f 20 62 65 20 62 72 65 61 limit to be brea
10ba9 63 68 65 64 2c 20 69 74 20 77 69 6c 6c 20 62 65 ched, it will be
10baa 20 70 6f 73 73 69 62 6c 65 20 74 6f 0a 20 20 20 possible to.
10bab 20 2a 2a 20 72 65 63 6c 61 69 6d 20 6d 65 6d 6f ** reclaim memo
10bac 72 79 20 66 72 6f 6d 20 74 68 69 73 20 70 61 67 ry from this pag
10bad 65 72 2d 63 61 63 68 65 2e 0a 20 20 20 20 2a 2f er-cache.. */
10bae 0a 20 20 20 20 70 63 61 63 68 65 31 4c 65 61 76 . pcache1Leav
10baf 65 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 70 20 eMutex();. p
10bb0 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 = sqlite3Malloc(
10bb1 6e 42 79 74 65 29 3b 0a 20 20 20 20 70 63 61 63 nByte);. pcac
10bb2 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 29 3b he1EnterMutex();
10bb3 0a 20 20 20 20 69 66 28 20 70 20 29 7b 0a 20 20 . if( p ){.
10bb4 20 20 20 20 69 6e 74 20 73 7a 20 3d 20 73 71 6c int sz = sql
10bb5 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 ite3MallocSize(p
10bb6 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
10bb7 53 74 61 74 75 73 41 64 64 28 53 51 4c 49 54 45 StatusAdd(SQLITE
10bb8 5f 53 54 41 54 55 53 5f 50 41 47 45 43 41 43 48 _STATUS_PAGECACH
10bb9 45 5f 4f 56 45 52 46 4c 4f 57 2c 20 73 7a 29 3b E_OVERFLOW, sz);
10bba 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 . }. }. ret
10bbb 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 urn p;.}../*.**
10bbc 46 72 65 65 20 61 6e 20 61 6c 6c 6f 63 61 74 65 Free an allocate
10bbd 64 20 62 75 66 66 65 72 20 6f 62 74 61 69 6e 65 d buffer obtaine
10bbe 64 20 66 72 6f 6d 20 70 63 61 63 68 65 31 41 6c d from pcache1Al
10bbf 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 loc()..*/.static
10bc0 20 76 6f 69 64 20 70 63 61 63 68 65 31 46 72 65 void pcache1Fre
10bc1 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 61 73 e(void *p){. as
10bc2 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
10bc3 74 65 78 5f 68 65 6c 64 28 70 63 61 63 68 65 31 tex_held(pcache1
10bc4 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 .mutex) );. if(
10bc5 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a p==0 ) return;.
10bc6 20 20 69 66 28 20 70 3e 3d 70 63 61 63 68 65 31 if( p>=pcache1
10bc7 2e 70 53 74 61 72 74 20 26 26 20 70 3c 70 63 61 .pStart && p<pca
10bc8 63 68 65 31 2e 70 45 6e 64 20 29 7b 0a 20 20 20 che1.pEnd ){.
10bc9 20 50 67 46 72 65 65 73 6c 6f 74 20 2a 70 53 6c PgFreeslot *pSl
10bca 6f 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 ot;. sqlite3S
10bcb 74 61 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f tatusAdd(SQLITE_
10bcc 53 54 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 STATUS_PAGECACHE
10bcd 5f 55 53 45 44 2c 20 2d 31 29 3b 0a 20 20 20 20 _USED, -1);.
10bce 70 53 6c 6f 74 20 3d 20 28 50 67 46 72 65 65 73 pSlot = (PgFrees
10bcf 6c 6f 74 2a 29 70 3b 0a 20 20 20 20 70 53 6c 6f lot*)p;. pSlo
10bd0 74 2d 3e 70 4e 65 78 74 20 3d 20 70 63 61 63 68 t->pNext = pcach
10bd1 65 31 2e 70 46 72 65 65 3b 0a 20 20 20 20 70 63 e1.pFree;. pc
10bd2 61 63 68 65 31 2e 70 46 72 65 65 20 3d 20 70 53 ache1.pFree = pS
10bd3 6c 6f 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 lot;. }else{.
10bd4 20 20 69 6e 74 20 69 53 69 7a 65 20 3d 20 73 71 int iSize = sq
10bd5 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 lite3MallocSize(
10bd6 70 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 p);. sqlite3S
10bd7 74 61 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f tatusAdd(SQLITE_
10bd8 53 54 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 STATUS_PAGECACHE
10bd9 5f 4f 56 45 52 46 4c 4f 57 2c 20 2d 69 53 69 7a _OVERFLOW, -iSiz
10bda 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f e);. sqlite3_
10bdb 66 72 65 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a free(p);. }.}..
10bdc 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 /*.** Allocate a
10bdd 20 6e 65 77 20 70 61 67 65 20 6f 62 6a 65 63 74 new page object
10bde 20 69 6e 69 74 69 61 6c 6c 79 20 61 73 73 6f 63 initially assoc
10bdf 69 61 74 65 64 20 77 69 74 68 20 63 61 63 68 65 iated with cache
10be0 20 70 43 61 63 68 65 2e 0a 2a 2f 0a 73 74 61 74 pCache..*/.stat
10be1 69 63 20 50 67 48 64 72 31 20 2a 70 63 61 63 68 ic PgHdr1 *pcach
10be2 65 31 41 6c 6c 6f 63 50 61 67 65 28 50 43 61 63 e1AllocPage(PCac
10be3 68 65 31 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 he1 *pCache){.
10be4 69 6e 74 20 6e 42 79 74 65 20 3d 20 73 69 7a 65 int nByte = size
10be5 6f 66 28 50 67 48 64 72 31 29 20 2b 20 70 43 61 of(PgHdr1) + pCa
10be6 63 68 65 2d 3e 73 7a 50 61 67 65 3b 0a 20 20 76 che->szPage;. v
10be7 6f 69 64 20 2a 70 50 67 20 3d 20 70 63 61 63 68 oid *pPg = pcach
10be8 65 31 41 6c 6c 6f 63 28 6e 42 79 74 65 29 3b 0a e1Alloc(nByte);.
10be9 20 20 50 67 48 64 72 31 20 2a 70 3b 0a 20 20 69 PgHdr1 *p;. i
10bea 66 28 20 70 50 67 20 29 7b 0a 20 20 20 20 70 20 f( pPg ){. p
10beb 3d 20 50 41 47 45 5f 54 4f 5f 50 47 48 44 52 31 = PAGE_TO_PGHDR1
10bec 28 70 43 61 63 68 65 2c 20 70 50 67 29 3b 0a 20 (pCache, pPg);.
10bed 20 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e 62 if( pCache->b
10bee 50 75 72 67 65 61 62 6c 65 20 29 7b 0a 20 20 20 Purgeable ){.
10bef 20 20 20 70 63 61 63 68 65 31 2e 6e 43 75 72 72 pcache1.nCurr
10bf0 65 6e 74 50 61 67 65 2b 2b 3b 0a 20 20 20 20 7d entPage++;. }
10bf1 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 20 . }else{. p
10bf2 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 = 0;. }. retur
10bf3 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 n p;.}../*.** Fr
10bf4 65 65 20 61 20 70 61 67 65 20 6f 62 6a 65 63 74 ee a page object
10bf5 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 70 63 allocated by pc
10bf6 61 63 68 65 31 41 6c 6c 6f 63 50 61 67 65 28 29 ache1AllocPage()
10bf7 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 6e ..**.** The poin
10bf8 74 65 72 20 69 73 20 61 6c 6c 6f 77 65 64 20 74 ter is allowed t
10bf9 6f 20 62 65 20 4e 55 4c 4c 2c 20 77 68 69 63 68 o be NULL, which
10bfa 20 69 73 20 70 72 75 64 65 6e 74 2e 20 20 42 75 is prudent. Bu
10bfb 74 20 69 74 20 74 75 72 6e 73 20 6f 75 74 0a 2a t it turns out.*
10bfc 2a 20 74 68 61 74 20 74 68 65 20 63 75 72 72 65 * that the curre
10bfd 6e 74 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f nt implementatio
10bfe 6e 20 68 61 70 70 65 6e 73 20 74 6f 20 6e 65 76 n happens to nev
10bff 65 72 20 63 61 6c 6c 20 74 68 69 73 20 72 6f 75 er call this rou
10c00 74 69 6e 65 0a 2a 2a 20 77 69 74 68 20 61 20 4e tine.** with a N
10c01 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 73 6f 20 ULL pointer, so
10c02 77 65 20 6d 61 72 6b 20 74 68 65 20 4e 55 4c 4c we mark the NULL
10c03 20 74 65 73 74 20 77 69 74 68 20 41 4c 57 41 59 test with ALWAY
10c04 53 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 S()..*/.static v
10c05 6f 69 64 20 70 63 61 63 68 65 31 46 72 65 65 50 oid pcache1FreeP
10c06 61 67 65 28 50 67 48 64 72 31 20 2a 70 29 7b 0a age(PgHdr1 *p){.
10c07 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 29 20 if( ALWAYS(p)
10c08 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 43 ){. if( p->pC
10c09 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 ache->bPurgeable
10c0a 20 29 7b 0a 20 20 20 20 20 20 70 63 61 63 68 65 ){. pcache
10c0b 31 2e 6e 43 75 72 72 65 6e 74 50 61 67 65 2d 2d 1.nCurrentPage--
10c0c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 63 61 63 ;. }. pcac
10c0d 68 65 31 46 72 65 65 28 50 47 48 44 52 31 5f 54 he1Free(PGHDR1_T
10c0e 4f 5f 50 41 47 45 28 70 29 29 3b 0a 20 20 7d 0a O_PAGE(p));. }.
10c0f 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6c 6c 6f 63 20 }../*.** Malloc
10c10 66 75 6e 63 74 69 6f 6e 20 75 73 65 64 20 62 79 function used by
10c11 20 53 51 4c 69 74 65 20 74 6f 20 6f 62 74 61 69 SQLite to obtai
10c12 6e 20 73 70 61 63 65 20 66 72 6f 6d 20 74 68 65 n space from the
10c13 20 62 75 66 66 65 72 20 63 6f 6e 66 69 67 75 72 buffer configur
10c14 65 64 0a 2a 2a 20 75 73 69 6e 67 20 73 71 6c 69 ed.** using sqli
10c15 74 65 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54 te3_config(SQLIT
10c16 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 E_CONFIG_PAGECAC
10c17 48 45 29 20 6f 70 74 69 6f 6e 2e 20 49 66 20 6e HE) option. If n
10c18 6f 20 73 75 63 68 20 62 75 66 66 65 72 0a 2a 2a o such buffer.**
10c19 20 65 78 69 73 74 73 2c 20 74 68 69 73 20 66 75 exists, this fu
10c1a 6e 63 74 69 6f 6e 20 66 61 6c 6c 73 20 62 61 63 nction falls bac
10c1b 6b 20 74 6f 20 73 71 6c 69 74 65 33 4d 61 6c 6c k to sqlite3Mall
10c1c 6f 63 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f oc()..*/.SQLITE_
10c1d 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 PRIVATE void *sq
10c1e 6c 69 74 65 33 50 61 67 65 4d 61 6c 6c 6f 63 28 lite3PageMalloc(
10c1f 69 6e 74 20 73 7a 29 7b 0a 20 20 76 6f 69 64 20 int sz){. void
10c20 2a 70 3b 0a 20 20 70 63 61 63 68 65 31 45 6e 74 *p;. pcache1Ent
10c21 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 70 20 3d erMutex();. p =
10c22 20 70 63 61 63 68 65 31 41 6c 6c 6f 63 28 73 7a pcache1Alloc(sz
10c23 29 3b 0a 20 20 70 63 61 63 68 65 31 4c 65 61 76 );. pcache1Leav
10c24 65 4d 75 74 65 78 28 29 3b 0a 20 20 72 65 74 75 eMutex();. retu
10c25 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 rn p;.}../*.** F
10c26 72 65 65 20 61 6e 20 61 6c 6c 6f 63 61 74 65 64 ree an allocated
10c27 20 62 75 66 66 65 72 20 6f 62 74 61 69 6e 65 64 buffer obtained
10c28 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 50 61 67 from sqlite3Pag
10c29 65 4d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 53 51 eMalloc()..*/.SQ
10c2a 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
10c2b 64 20 73 71 6c 69 74 65 33 50 61 67 65 46 72 65 d sqlite3PageFre
10c2c 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 70 63 e(void *p){. pc
10c2d 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 ache1EnterMutex(
10c2e 29 3b 0a 20 20 70 63 61 63 68 65 31 46 72 65 65 );. pcache1Free
10c2f 28 70 29 3b 0a 20 20 70 63 61 63 68 65 31 4c 65 (p);. pcache1Le
10c30 61 76 65 4d 75 74 65 78 28 29 3b 0a 7d 0a 0a 2f aveMutex();.}../
10c31 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10c32 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10c33 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10c34 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10c35 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
10c36 2f 2a 2a 2a 2a 2a 2a 2a 2a 20 47 65 6e 65 72 61 /******** Genera
10c37 6c 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e l Implementation
10c38 20 46 75 6e 63 74 69 6f 6e 73 20 2a 2a 2a 2a 2a Functions *****
10c39 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10c3a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
10c3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e ../*.** This fun
10c3c 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f ction is used to
10c3d 20 72 65 73 69 7a 65 20 74 68 65 20 68 61 73 68 resize the hash
10c3e 20 74 61 62 6c 65 20 75 73 65 64 20 62 79 20 74 table used by t
10c3f 68 65 20 63 61 63 68 65 20 70 61 73 73 65 64 0a he cache passed.
10c40 2a 2a 20 61 73 20 74 68 65 20 66 69 72 73 74 20 ** as the first
10c41 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 argument..**.**
10c42 54 68 65 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78 The global mutex
10c43 20 6d 75 73 74 20 62 65 20 68 65 6c 64 20 77 68 must be held wh
10c44 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e en this function
10c45 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 is called..*/.s
10c46 74 61 74 69 63 20 69 6e 74 20 70 63 61 63 68 65 tatic int pcache
10c47 31 52 65 73 69 7a 65 48 61 73 68 28 50 43 61 63 1ResizeHash(PCac
10c48 68 65 31 20 2a 70 29 7b 0a 20 20 50 67 48 64 72 he1 *p){. PgHdr
10c49 31 20 2a 2a 61 70 4e 65 77 3b 0a 20 20 75 6e 73 1 **apNew;. uns
10c4a 69 67 6e 65 64 20 69 6e 74 20 6e 4e 65 77 3b 0a igned int nNew;.
10c4b 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 unsigned int i
10c4c 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c ;.. assert( sql
10c4d 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
10c4e 70 63 61 63 68 65 31 2e 6d 75 74 65 78 29 20 29 pcache1.mutex) )
10c4f 3b 0a 0a 20 20 6e 4e 65 77 20 3d 20 70 2d 3e 6e ;.. nNew = p->n
10c50 48 61 73 68 2a 32 3b 0a 20 20 69 66 28 20 6e 4e Hash*2;. if( nN
10c51 65 77 3c 32 35 36 20 29 7b 0a 20 20 20 20 6e 4e ew<256 ){. nN
10c52 65 77 20 3d 20 32 35 36 3b 0a 20 20 7d 0a 0a 20 ew = 256;. }..
10c53 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d 75 74 pcache1LeaveMut
10c54 65 78 28 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e ex();. if( p->n
10c55 48 61 73 68 20 29 7b 20 73 71 6c 69 74 65 33 42 Hash ){ sqlite3B
10c56 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 eginBenignMalloc
10c57 28 29 3b 20 7d 0a 20 20 61 70 4e 65 77 20 3d 20 (); }. apNew =
10c58 28 50 67 48 64 72 31 20 2a 2a 29 73 71 6c 69 74 (PgHdr1 **)sqlit
10c59 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 e3_malloc(sizeof
10c5a 28 50 67 48 64 72 31 20 2a 29 2a 6e 4e 65 77 29 (PgHdr1 *)*nNew)
10c5b 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 48 61 73 68 ;. if( p->nHash
10c5c 20 29 7b 20 73 71 6c 69 74 65 33 45 6e 64 42 65 ){ sqlite3EndBe
10c5d 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 20 7d 0a nignMalloc(); }.
10c5e 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75 pcache1EnterMu
10c5f 74 65 78 28 29 3b 0a 20 20 69 66 28 20 61 70 4e tex();. if( apN
10c60 65 77 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65 74 ew ){. memset
10c61 28 61 70 4e 65 77 2c 20 30 2c 20 73 69 7a 65 6f (apNew, 0, sizeo
10c62 66 28 50 67 48 64 72 31 20 2a 29 2a 6e 4e 65 77 f(PgHdr1 *)*nNew
10c63 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 );. for(i=0;
10c64 69 3c 70 2d 3e 6e 48 61 73 68 3b 20 69 2b 2b 29 i<p->nHash; i++)
10c65 7b 0a 20 20 20 20 20 20 50 67 48 64 72 31 20 2a {. PgHdr1 *
10c66 70 50 61 67 65 3b 0a 20 20 20 20 20 20 50 67 48 pPage;. PgH
10c67 64 72 31 20 2a 70 4e 65 78 74 20 3d 20 70 2d 3e dr1 *pNext = p->
10c68 61 70 48 61 73 68 5b 69 5d 3b 0a 20 20 20 20 20 apHash[i];.
10c69 20 77 68 69 6c 65 28 20 28 70 50 61 67 65 20 3d while( (pPage =
10c6a 20 70 4e 65 78 74 29 21 3d 30 20 29 7b 0a 20 20 pNext)!=0 ){.
10c6b 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 69 unsigned i
10c6c 6e 74 20 68 20 3d 20 70 50 61 67 65 2d 3e 69 4b nt h = pPage->iK
10c6d 65 79 20 25 20 6e 4e 65 77 3b 0a 20 20 20 20 20 ey % nNew;.
10c6e 20 20 20 70 4e 65 78 74 20 3d 20 70 50 61 67 65 pNext = pPage
10c6f 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 ->pNext;.
10c70 20 70 50 61 67 65 2d 3e 70 4e 65 78 74 20 3d 20 pPage->pNext =
10c71 61 70 4e 65 77 5b 68 5d 3b 0a 20 20 20 20 20 20 apNew[h];.
10c72 20 20 61 70 4e 65 77 5b 68 5d 20 3d 20 70 50 61 apNew[h] = pPa
10c73 67 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ge;. }.
10c74 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 }. sqlite3_fr
10c75 65 65 28 70 2d 3e 61 70 48 61 73 68 29 3b 0a 20 ee(p->apHash);.
10c76 20 20 20 70 2d 3e 61 70 48 61 73 68 20 3d 20 61 p->apHash = a
10c77 70 4e 65 77 3b 0a 20 20 20 20 70 2d 3e 6e 48 61 pNew;. p->nHa
10c78 73 68 20 3d 20 6e 4e 65 77 3b 0a 20 20 7d 0a 0a sh = nNew;. }..
10c79 20 20 72 65 74 75 72 6e 20 28 70 2d 3e 61 70 48 return (p->apH
10c7a 61 73 68 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 ash ? SQLITE_OK
10c7b 3a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b : SQLITE_NOMEM);
10c7c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 .}../*.** This f
10c7d 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 unction is used
10c7e 69 6e 74 65 72 6e 61 6c 6c 79 20 74 6f 20 72 65 internally to re
10c7f 6d 6f 76 65 20 74 68 65 20 70 61 67 65 20 70 50 move the page pP
10c80 61 67 65 20 66 72 6f 6d 20 74 68 65 20 0a 2a 2a age from the .**
10c81 20 67 6c 6f 62 61 6c 20 4c 52 55 20 6c 69 73 74 global LRU list
10c82 2c 20 69 66 20 69 73 20 70 61 72 74 20 6f 66 20 , if is part of
10c83 69 74 2e 20 49 66 20 70 50 61 67 65 20 69 73 20 it. If pPage is
10c84 6e 6f 74 20 70 61 72 74 20 6f 66 20 74 68 65 20 not part of the
10c85 67 6c 6f 62 61 6c 0a 2a 2a 20 4c 52 55 20 6c 69 global.** LRU li
10c86 73 74 2c 20 74 68 65 6e 20 74 68 69 73 20 66 75 st, then this fu
10c87 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f nction is a no-o
10c88 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 67 6c 6f p..**.** The glo
10c89 62 61 6c 20 6d 75 74 65 78 20 6d 75 73 74 20 62 bal mutex must b
10c8a 65 20 68 65 6c 64 20 77 68 65 6e 20 74 68 69 73 e held when this
10c8b 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c function is cal
10c8c 6c 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 led..*/.static v
10c8d 6f 69 64 20 70 63 61 63 68 65 31 50 69 6e 50 61 oid pcache1PinPa
10c8e 67 65 28 50 67 48 64 72 31 20 2a 70 50 61 67 65 ge(PgHdr1 *pPage
10c8f 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c ){. assert( sql
10c90 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
10c91 70 63 61 63 68 65 31 2e 6d 75 74 65 78 29 20 29 pcache1.mutex) )
10c92 3b 0a 20 20 69 66 28 20 70 50 61 67 65 20 26 26 ;. if( pPage &&
10c93 20 28 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 (pPage->pLruNex
10c94 74 20 7c 7c 20 70 50 61 67 65 3d 3d 70 63 61 63 t || pPage==pcac
10c95 68 65 31 2e 70 4c 72 75 54 61 69 6c 29 20 29 7b he1.pLruTail) ){
10c96 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e . if( pPage->
10c97 70 4c 72 75 50 72 65 76 20 29 7b 0a 20 20 20 20 pLruPrev ){.
10c98 20 20 70 50 61 67 65 2d 3e 70 4c 72 75 50 72 65 pPage->pLruPre
10c99 76 2d 3e 70 4c 72 75 4e 65 78 74 20 3d 20 70 50 v->pLruNext = pP
10c9a 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74 3b 0a 20 age->pLruNext;.
10c9b 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 50 61 }. if( pPa
10c9c 67 65 2d 3e 70 4c 72 75 4e 65 78 74 20 29 7b 0a ge->pLruNext ){.
10c9d 20 20 20 20 20 20 70 50 61 67 65 2d 3e 70 4c 72 pPage->pLr
10c9e 75 4e 65 78 74 2d 3e 70 4c 72 75 50 72 65 76 20 uNext->pLruPrev
10c9f 3d 20 70 50 61 67 65 2d 3e 70 4c 72 75 50 72 65 = pPage->pLruPre
10ca0 76 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 v;. }. if(
10ca1 20 70 63 61 63 68 65 31 2e 70 4c 72 75 48 65 61 pcache1.pLruHea
10ca2 64 3d 3d 70 50 61 67 65 20 29 7b 0a 20 20 20 20 d==pPage ){.
10ca3 20 20 70 63 61 63 68 65 31 2e 70 4c 72 75 48 65 pcache1.pLruHe
10ca4 61 64 20 3d 20 70 50 61 67 65 2d 3e 70 4c 72 75 ad = pPage->pLru
10ca5 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 Next;. }.
10ca6 69 66 28 20 70 63 61 63 68 65 31 2e 70 4c 72 75 if( pcache1.pLru
10ca7 54 61 69 6c 3d 3d 70 50 61 67 65 20 29 7b 0a 20 Tail==pPage ){.
10ca8 20 20 20 20 20 70 63 61 63 68 65 31 2e 70 4c 72 pcache1.pLr
10ca9 75 54 61 69 6c 20 3d 20 70 50 61 67 65 2d 3e 70 uTail = pPage->p
10caa 4c 72 75 50 72 65 76 3b 0a 20 20 20 20 7d 0a 20 LruPrev;. }.
10cab 20 20 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 pPage->pLruNe
10cac 78 74 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 xt = 0;. pPag
10cad 65 2d 3e 70 4c 72 75 50 72 65 76 20 3d 20 30 3b e->pLruPrev = 0;
10cae 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 43 61 63 . pPage->pCac
10caf 68 65 2d 3e 6e 52 65 63 79 63 6c 61 62 6c 65 2d he->nRecyclable-
10cb0 2d 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a -;. }.}.../*.**
10cb1 20 52 65 6d 6f 76 65 20 74 68 65 20 70 61 67 65 Remove the page
10cb2 20 73 75 70 70 6c 69 65 64 20 61 73 20 61 6e 20 supplied as an
10cb3 61 72 67 75 6d 65 6e 74 20 66 72 6f 6d 20 74 68 argument from th
10cb4 65 20 68 61 73 68 20 74 61 62 6c 65 20 0a 2a 2a e hash table .**
10cb5 20 28 50 43 61 63 68 65 31 2e 61 70 48 61 73 68 (PCache1.apHash
10cb6 20 73 74 72 75 63 74 75 72 65 29 20 74 68 61 74 structure) that
10cb7 20 69 74 20 69 73 20 63 75 72 72 65 6e 74 6c 79 it is currently
10cb8 20 73 74 6f 72 65 64 20 69 6e 2e 0a 2a 2a 0a 2a stored in..**.*
10cb9 2a 20 54 68 65 20 67 6c 6f 62 61 6c 20 6d 75 74 * The global mut
10cba 65 78 20 6d 75 73 74 20 62 65 20 68 65 6c 64 20 ex must be held
10cbb 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 when this functi
10cbc 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f on is called..*/
10cbd 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 .static void pca
10cbe 63 68 65 31 52 65 6d 6f 76 65 46 72 6f 6d 48 61 che1RemoveFromHa
10cbf 73 68 28 50 67 48 64 72 31 20 2a 70 50 61 67 65 sh(PgHdr1 *pPage
10cc0 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e ){. unsigned in
10cc1 74 20 68 3b 0a 20 20 50 43 61 63 68 65 31 20 2a t h;. PCache1 *
10cc2 70 43 61 63 68 65 20 3d 20 70 50 61 67 65 2d 3e pCache = pPage->
10cc3 70 43 61 63 68 65 3b 0a 20 20 50 67 48 64 72 31 pCache;. PgHdr1
10cc4 20 2a 2a 70 70 3b 0a 0a 20 20 68 20 3d 20 70 50 **pp;.. h = pP
10cc5 61 67 65 2d 3e 69 4b 65 79 20 25 20 70 43 61 63 age->iKey % pCac
10cc6 68 65 2d 3e 6e 48 61 73 68 3b 0a 20 20 66 6f 72 he->nHash;. for
10cc7 28 70 70 3d 26 70 43 61 63 68 65 2d 3e 61 70 48 (pp=&pCache->apH
10cc8 61 73 68 5b 68 5d 3b 20 28 2a 70 70 29 21 3d 70 ash[h]; (*pp)!=p
10cc9 50 61 67 65 3b 20 70 70 3d 26 28 2a 70 70 29 2d Page; pp=&(*pp)-
10cca 3e 70 4e 65 78 74 29 3b 0a 20 20 2a 70 70 20 3d >pNext);. *pp =
10ccb 20 28 2a 70 70 29 2d 3e 70 4e 65 78 74 3b 0a 0a (*pp)->pNext;..
10ccc 20 20 70 43 61 63 68 65 2d 3e 6e 50 61 67 65 2d pCache->nPage-
10ccd 2d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 -;.}../*.** If t
10cce 68 65 72 65 20 61 72 65 20 63 75 72 72 65 6e 74 here are current
10ccf 6c 79 20 6d 6f 72 65 20 74 68 61 6e 20 70 63 61 ly more than pca
10cd0 63 68 65 2e 6e 4d 61 78 50 61 67 65 20 70 61 67 che.nMaxPage pag
10cd1 65 73 20 61 6c 6c 6f 63 61 74 65 64 2c 20 74 72 es allocated, tr
10cd2 79 0a 2a 2a 20 74 6f 20 72 65 63 79 63 6c 65 20 y.** to recycle
10cd3 70 61 67 65 73 20 74 6f 20 72 65 64 75 63 65 20 pages to reduce
10cd4 74 68 65 20 6e 75 6d 62 65 72 20 61 6c 6c 6f 63 the number alloc
10cd5 61 74 65 64 20 74 6f 20 70 63 61 63 68 65 2e 6e ated to pcache.n
10cd6 4d 61 78 50 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 MaxPage..*/.stat
10cd7 69 63 20 76 6f 69 64 20 70 63 61 63 68 65 31 45 ic void pcache1E
10cd8 6e 66 6f 72 63 65 4d 61 78 50 61 67 65 28 76 6f nforceMaxPage(vo
10cd9 69 64 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 id){. assert( s
10cda 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
10cdb 64 28 70 63 61 63 68 65 31 2e 6d 75 74 65 78 29 d(pcache1.mutex)
10cdc 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 70 63 61 );. while( pca
10cdd 63 68 65 31 2e 6e 43 75 72 72 65 6e 74 50 61 67 che1.nCurrentPag
10cde 65 3e 70 63 61 63 68 65 31 2e 6e 4d 61 78 50 61 e>pcache1.nMaxPa
10cdf 67 65 20 26 26 20 70 63 61 63 68 65 31 2e 70 4c ge && pcache1.pL
10ce0 72 75 54 61 69 6c 20 29 7b 0a 20 20 20 20 50 67 ruTail ){. Pg
10ce1 48 64 72 31 20 2a 70 20 3d 20 70 63 61 63 68 65 Hdr1 *p = pcache
10ce2 31 2e 70 4c 72 75 54 61 69 6c 3b 0a 20 20 20 20 1.pLruTail;.
10ce3 70 63 61 63 68 65 31 50 69 6e 50 61 67 65 28 70 pcache1PinPage(p
10ce4 29 3b 0a 20 20 20 20 70 63 61 63 68 65 31 52 65 );. pcache1Re
10ce5 6d 6f 76 65 46 72 6f 6d 48 61 73 68 28 70 29 3b moveFromHash(p);
10ce6 0a 20 20 20 20 70 63 61 63 68 65 31 46 72 65 65 . pcache1Free
10ce7 50 61 67 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a Page(p);. }.}..
10ce8 2f 2a 0a 2a 2a 20 44 69 73 63 61 72 64 20 61 6c /*.** Discard al
10ce9 6c 20 70 61 67 65 73 20 66 72 6f 6d 20 63 61 63 l pages from cac
10cea 68 65 20 70 43 61 63 68 65 20 77 69 74 68 20 61 he pCache with a
10ceb 20 70 61 67 65 20 6e 75 6d 62 65 72 20 28 6b 65 page number (ke
10cec 79 20 76 61 6c 75 65 29 20 0a 2a 2a 20 67 72 65 y value) .** gre
10ced 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75 ater than or equ
10cee 61 6c 20 74 6f 20 69 4c 69 6d 69 74 2e 20 41 6e al to iLimit. An
10cef 79 20 70 69 6e 6e 65 64 20 70 61 67 65 73 20 74 y pinned pages t
10cf0 68 61 74 20 6d 65 65 74 20 74 68 69 73 20 0a 2a hat meet this .*
10cf1 2a 20 63 72 69 74 65 72 69 61 20 61 72 65 20 75 * criteria are u
10cf2 6e 70 69 6e 6e 65 64 20 62 65 66 6f 72 65 20 74 npinned before t
10cf3 68 65 79 20 61 72 65 20 64 69 73 63 61 72 64 65 hey are discarde
10cf4 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 67 6c 6f d..**.** The glo
10cf5 62 61 6c 20 6d 75 74 65 78 20 6d 75 73 74 20 62 bal mutex must b
10cf6 65 20 68 65 6c 64 20 77 68 65 6e 20 74 68 69 73 e held when this
10cf7 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c function is cal
10cf8 6c 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 led..*/.static v
10cf9 6f 69 64 20 70 63 61 63 68 65 31 54 72 75 6e 63 oid pcache1Trunc
10cfa 61 74 65 55 6e 73 61 66 65 28 0a 20 20 50 43 61 ateUnsafe(. PCa
10cfb 63 68 65 31 20 2a 70 43 61 63 68 65 2c 20 0a 20 che1 *pCache, .
10cfc 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 4c unsigned int iL
10cfd 69 6d 69 74 20 0a 29 7b 0a 20 20 54 45 53 54 4f imit .){. TESTO
10cfe 4e 4c 59 28 20 75 6e 73 69 67 6e 65 64 20 69 6e NLY( unsigned in
10cff 74 20 6e 50 61 67 65 20 3d 20 30 3b 20 29 20 20 t nPage = 0; )
10d00 20 20 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 61 /* Used to a
10d01 73 73 65 72 74 20 70 43 61 63 68 65 2d 3e 6e 50 ssert pCache->nP
10d02 61 67 65 20 69 73 20 63 6f 72 72 65 63 74 20 2a age is correct *
10d03 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 /. unsigned int
10d04 20 68 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 h;. assert( sq
10d05 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
10d06 28 70 63 61 63 68 65 31 2e 6d 75 74 65 78 29 20 (pcache1.mutex)
10d07 29 3b 0a 20 20 66 6f 72 28 68 3d 30 3b 20 68 3c );. for(h=0; h<
10d08 70 43 61 63 68 65 2d 3e 6e 48 61 73 68 3b 20 68 pCache->nHash; h
10d09 2b 2b 29 7b 0a 20 20 20 20 50 67 48 64 72 31 20 ++){. PgHdr1
10d0a 2a 2a 70 70 20 3d 20 26 70 43 61 63 68 65 2d 3e **pp = &pCache->
10d0b 61 70 48 61 73 68 5b 68 5d 3b 20 0a 20 20 20 20 apHash[h]; .
10d0c 50 67 48 64 72 31 20 2a 70 50 61 67 65 3b 0a 20 PgHdr1 *pPage;.
10d0d 20 20 20 77 68 69 6c 65 28 20 28 70 50 61 67 65 while( (pPage
10d0e 20 3d 20 2a 70 70 29 21 3d 30 20 29 7b 0a 20 20 = *pp)!=0 ){.
10d0f 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 69 if( pPage->i
10d10 4b 65 79 3e 3d 69 4c 69 6d 69 74 20 29 7b 0a 20 Key>=iLimit ){.
10d11 20 20 20 20 20 20 20 70 43 61 63 68 65 2d 3e 6e pCache->n
10d12 50 61 67 65 2d 2d 3b 0a 20 20 20 20 20 20 20 20 Page--;.
10d13 2a 70 70 20 3d 20 70 50 61 67 65 2d 3e 70 4e 65 *pp = pPage->pNe
10d14 78 74 3b 0a 20 20 20 20 20 20 20 20 70 63 61 63 xt;. pcac
10d15 68 65 31 50 69 6e 50 61 67 65 28 70 50 61 67 65 he1PinPage(pPage
10d16 29 3b 0a 20 20 20 20 20 20 20 20 70 63 61 63 68 );. pcach
10d17 65 31 46 72 65 65 50 61 67 65 28 70 50 61 67 65 e1FreePage(pPage
10d18 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a );. }else{.
10d19 20 20 20 20 20 20 20 20 70 70 20 3d 20 26 70 50 pp = &pP
10d1a 61 67 65 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 age->pNext;.
10d1b 20 20 20 20 54 45 53 54 4f 4e 4c 59 28 20 6e 50 TESTONLY( nP
10d1c 61 67 65 2b 2b 3b 20 29 0a 20 20 20 20 20 20 7d age++; ). }
10d1d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73 . }. }. ass
10d1e 65 72 74 28 20 70 43 61 63 68 65 2d 3e 6e 50 61 ert( pCache->nPa
10d1f 67 65 3d 3d 6e 50 61 67 65 20 29 3b 0a 7d 0a 0a ge==nPage );.}..
10d20 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /***************
10d21 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10d22 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10d23 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10d24 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
10d25 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 20 73 71 6c 69 74 ./******** sqlit
10d26 65 33 5f 70 63 61 63 68 65 20 4d 65 74 68 6f 64 e3_pcache Method
10d27 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a s **************
10d28 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10d29 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10d2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 /../*.** Impleme
10d2b 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 ntation of the s
10d2c 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2e 78 49 qlite3_pcache.xI
10d2d 6e 69 74 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73 nit method..*/.s
10d2e 74 61 74 69 63 20 69 6e 74 20 70 63 61 63 68 65 tatic int pcache
10d2f 31 49 6e 69 74 28 76 6f 69 64 20 2a 4e 6f 74 55 1Init(void *NotU
10d30 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 sed){. UNUSED_P
10d31 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 ARAMETER(NotUsed
10d32 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 63 61 );. assert( pca
10d33 63 68 65 31 2e 69 73 49 6e 69 74 3d 3d 30 20 29 che1.isInit==0 )
10d34 3b 0a 20 20 6d 65 6d 73 65 74 28 26 70 63 61 63 ;. memset(&pcac
10d35 68 65 31 2c 20 30 2c 20 73 69 7a 65 6f 66 28 70 he1, 0, sizeof(p
10d36 63 61 63 68 65 31 29 29 3b 0a 20 20 69 66 28 20 cache1));. if(
10d37 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
10d38 66 69 67 2e 62 43 6f 72 65 4d 75 74 65 78 20 29 fig.bCoreMutex )
10d39 7b 0a 20 20 20 20 70 63 61 63 68 65 31 2e 6d 75 {. pcache1.mu
10d3a 74 65 78 20 3d 20 73 71 6c 69 74 65 33 5f 6d 75 tex = sqlite3_mu
10d3b 74 65 78 5f 61 6c 6c 6f 63 28 53 51 4c 49 54 45 tex_alloc(SQLITE
10d3c 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 _MUTEX_STATIC_LR
10d3d 55 29 3b 0a 20 20 7d 0a 20 20 70 63 61 63 68 65 U);. }. pcache
10d3e 31 2e 69 73 49 6e 69 74 20 3d 20 31 3b 0a 20 20 1.isInit = 1;.
10d3f 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
10d40 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 ;.}../*.** Imple
10d41 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 mentation of the
10d42 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2e sqlite3_pcache.
10d43 78 53 68 75 74 64 6f 77 6e 20 6d 65 74 68 6f 64 xShutdown method
10d44 2e 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 ..** Note that t
10d45 68 65 20 73 74 61 74 69 63 20 6d 75 74 65 78 20 he static mutex
10d46 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20 78 49 6e allocated in xIn
10d47 69 74 20 64 6f 65 73 20 0a 2a 2a 20 6e 6f 74 20 it does .** not
10d48 6e 65 65 64 20 74 6f 20 62 65 20 66 72 65 65 64 need to be freed
10d49 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
10d4a 20 70 63 61 63 68 65 31 53 68 75 74 64 6f 77 6e pcache1Shutdown
10d4b 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b (void *NotUsed){
10d4c 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
10d4d 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 TER(NotUsed);.
10d4e 61 73 73 65 72 74 28 20 70 63 61 63 68 65 31 2e assert( pcache1.
10d4f 69 73 49 6e 69 74 21 3d 30 20 29 3b 0a 20 20 6d isInit!=0 );. m
10d50 65 6d 73 65 74 28 26 70 63 61 63 68 65 31 2c 20 emset(&pcache1,
10d51 30 2c 20 73 69 7a 65 6f 66 28 70 63 61 63 68 65 0, sizeof(pcache
10d52 31 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 1));.}../*.** Im
10d53 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 plementation of
10d54 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63 the sqlite3_pcac
10d55 68 65 2e 78 43 72 65 61 74 65 20 6d 65 74 68 6f he.xCreate metho
10d56 64 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 d..**.** Allocat
10d57 65 20 61 20 6e 65 77 20 63 61 63 68 65 2e 0a 2a e a new cache..*
10d58 2f 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 /.static sqlite3
10d59 5f 70 63 61 63 68 65 20 2a 70 63 61 63 68 65 31 _pcache *pcache1
10d5a 43 72 65 61 74 65 28 69 6e 74 20 73 7a 50 61 67 Create(int szPag
10d5b 65 2c 20 69 6e 74 20 62 50 75 72 67 65 61 62 6c e, int bPurgeabl
10d5c 65 29 7b 0a 20 20 50 43 61 63 68 65 31 20 2a 70 e){. PCache1 *p
10d5d 43 61 63 68 65 3b 0a 0a 20 20 70 43 61 63 68 65 Cache;.. pCache
10d5e 20 3d 20 28 50 43 61 63 68 65 31 20 2a 29 73 71 = (PCache1 *)sq
10d5f 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a lite3_malloc(siz
10d60 65 6f 66 28 50 43 61 63 68 65 31 29 29 3b 0a 20 eof(PCache1));.
10d61 20 69 66 28 20 70 43 61 63 68 65 20 29 7b 0a 20 if( pCache ){.
10d62 20 20 20 6d 65 6d 73 65 74 28 70 43 61 63 68 65 memset(pCache
10d63 2c 20 30 2c 20 73 69 7a 65 6f 66 28 50 43 61 63 , 0, sizeof(PCac
10d64 68 65 31 29 29 3b 0a 20 20 20 20 70 43 61 63 68 he1));. pCach
10d65 65 2d 3e 73 7a 50 61 67 65 20 3d 20 73 7a 50 61 e->szPage = szPa
10d66 67 65 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e ge;. pCache->
10d67 62 50 75 72 67 65 61 62 6c 65 20 3d 20 28 62 50 bPurgeable = (bP
10d68 75 72 67 65 61 62 6c 65 20 3f 20 31 20 3a 20 30 urgeable ? 1 : 0
10d69 29 3b 0a 20 20 20 20 69 66 28 20 62 50 75 72 67 );. if( bPurg
10d6a 65 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 70 eable ){. p
10d6b 43 61 63 68 65 2d 3e 6e 4d 69 6e 20 3d 20 31 30 Cache->nMin = 10
10d6c 3b 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 45 ;. pcache1E
10d6d 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 nterMutex();.
10d6e 20 20 20 70 63 61 63 68 65 31 2e 6e 4d 69 6e 50 pcache1.nMinP
10d6f 61 67 65 20 2b 3d 20 70 43 61 63 68 65 2d 3e 6e age += pCache->n
10d70 4d 69 6e 3b 0a 20 20 20 20 20 20 70 63 61 63 68 Min;. pcach
10d71 65 31 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a e1LeaveMutex();.
10d72 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
10d73 72 6e 20 28 73 71 6c 69 74 65 33 5f 70 63 61 63 rn (sqlite3_pcac
10d74 68 65 20 2a 29 70 43 61 63 68 65 3b 0a 7d 0a 0a he *)pCache;.}..
10d75 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 /*.** Implementa
10d76 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 tion of the sqli
10d77 74 65 33 5f 70 63 61 63 68 65 2e 78 43 61 63 68 te3_pcache.xCach
10d78 65 73 69 7a 65 20 6d 65 74 68 6f 64 2e 20 0a 2a esize method. .*
10d79 2a 0a 2a 2a 20 43 6f 6e 66 69 67 75 72 65 20 74 *.** Configure t
10d7a 68 65 20 63 61 63 68 65 5f 73 69 7a 65 20 6c 69 he cache_size li
10d7b 6d 69 74 20 66 6f 72 20 61 20 63 61 63 68 65 2e mit for a cache.
10d7c 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
10d7d 70 63 61 63 68 65 31 43 61 63 68 65 73 69 7a 65 pcache1Cachesize
10d7e 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 (sqlite3_pcache
10d7f 2a 70 2c 20 69 6e 74 20 6e 4d 61 78 29 7b 0a 20 *p, int nMax){.
10d80 20 50 43 61 63 68 65 31 20 2a 70 43 61 63 68 65 PCache1 *pCache
10d81 20 3d 20 28 50 43 61 63 68 65 31 20 2a 29 70 3b = (PCache1 *)p;
10d82 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e 62 . if( pCache->b
10d83 50 75 72 67 65 61 62 6c 65 20 29 7b 0a 20 20 20 Purgeable ){.
10d84 20 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74 pcache1EnterMut
10d85 65 78 28 29 3b 0a 20 20 20 20 70 63 61 63 68 65 ex();. pcache
10d86 31 2e 6e 4d 61 78 50 61 67 65 20 2b 3d 20 28 6e 1.nMaxPage += (n
10d87 4d 61 78 20 2d 20 70 43 61 63 68 65 2d 3e 6e 4d Max - pCache->nM
10d88 61 78 29 3b 0a 20 20 20 20 70 43 61 63 68 65 2d ax);. pCache-
10d89 3e 6e 4d 61 78 20 3d 20 6e 4d 61 78 3b 0a 20 20 >nMax = nMax;.
10d8a 20 20 70 63 61 63 68 65 31 45 6e 66 6f 72 63 65 pcache1Enforce
10d8b 4d 61 78 50 61 67 65 28 29 3b 0a 20 20 20 20 70 MaxPage();. p
10d8c 63 61 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78 cache1LeaveMutex
10d8d 28 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a ();. }.}../*.**
10d8e 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 Implementation
10d8f 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 of the sqlite3_p
10d90 63 61 63 68 65 2e 78 50 61 67 65 63 6f 75 6e 74 cache.xPagecount
10d91 20 6d 65 74 68 6f 64 2e 20 0a 2a 2f 0a 73 74 61 method. .*/.sta
10d92 74 69 63 20 69 6e 74 20 70 63 61 63 68 65 31 50 tic int pcache1P
10d93 61 67 65 63 6f 75 6e 74 28 73 71 6c 69 74 65 33 agecount(sqlite3
10d94 5f 70 63 61 63 68 65 20 2a 70 29 7b 0a 20 20 69 _pcache *p){. i
10d95 6e 74 20 6e 3b 0a 20 20 70 63 61 63 68 65 31 45 nt n;. pcache1E
10d96 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 6e nterMutex();. n
10d97 20 3d 20 28 28 50 43 61 63 68 65 31 20 2a 29 70 = ((PCache1 *)p
10d98 29 2d 3e 6e 50 61 67 65 3b 0a 20 20 70 63 61 63 )->nPage;. pcac
10d99 68 65 31 4c 65 61 76 65 4d 75 74 65 78 28 29 3b he1LeaveMutex();
10d9a 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a . return n;.}..
10d9b 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 /*.** Implementa
10d9c 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 tion of the sqli
10d9d 74 65 33 5f 70 63 61 63 68 65 2e 78 46 65 74 63 te3_pcache.xFetc
10d9e 68 20 6d 65 74 68 6f 64 2e 20 0a 2a 2a 0a 2a 2a h method. .**.**
10d9f 20 46 65 74 63 68 20 61 20 70 61 67 65 20 62 79 Fetch a page by
10da0 20 6b 65 79 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a key value..**.*
10da1 2a 20 57 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 * Whether or not
10da2 20 61 20 6e 65 77 20 70 61 67 65 20 6d 61 79 20 a new page may
10da3 62 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 be allocated by
10da4 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 65 this function de
10da5 70 65 6e 64 73 20 6f 6e 0a 2a 2a 20 74 68 65 20 pends on.** the
10da6 76 61 6c 75 65 20 6f 66 20 74 68 65 20 63 72 65 value of the cre
10da7 61 74 65 46 6c 61 67 20 61 72 67 75 6d 65 6e 74 ateFlag argument
10da8 2e 20 20 30 20 6d 65 61 6e 73 20 64 6f 20 6e 6f . 0 means do no
10da9 74 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 t allocate a new
10daa 0a 2a 2a 20 70 61 67 65 2e 20 20 31 20 6d 65 61 .** page. 1 mea
10dab 6e 73 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 ns allocate a ne
10dac 77 20 70 61 67 65 20 69 66 20 73 70 61 63 65 20 w page if space
10dad 69 73 20 65 61 73 69 6c 79 20 61 76 61 69 6c 61 is easily availa
10dae 62 6c 65 2e 20 20 32 20 0a 2a 2a 20 6d 65 61 6e ble. 2 .** mean
10daf 73 20 74 6f 20 74 72 79 20 72 65 61 6c 6c 79 20 s to try really
10db0 68 61 72 64 20 74 6f 20 61 6c 6c 6f 63 61 74 65 hard to allocate
10db1 20 61 20 6e 65 77 20 70 61 67 65 2e 0a 2a 2a 0a a new page..**.
10db2 2a 2a 20 46 6f 72 20 61 20 6e 6f 6e 2d 70 75 72 ** For a non-pur
10db3 67 65 61 62 6c 65 20 63 61 63 68 65 20 28 61 20 geable cache (a
10db4 63 61 63 68 65 20 75 73 65 64 20 61 73 20 74 68 cache used as th
10db5 65 20 73 74 6f 72 61 67 65 20 66 6f 72 20 61 6e e storage for an
10db6 20 69 6e 2d 6d 65 6d 6f 72 79 0a 2a 2a 20 64 61 in-memory.** da
10db7 74 61 62 61 73 65 29 20 74 68 65 72 65 20 69 73 tabase) there is
10db8 20 72 65 61 6c 6c 79 20 6e 6f 20 64 69 66 66 65 really no diffe
10db9 72 65 6e 63 65 20 62 65 74 77 65 65 6e 20 63 72 rence between cr
10dba 65 61 74 65 46 6c 61 67 20 31 20 61 6e 64 20 32 eateFlag 1 and 2
10dbb 2e 20 20 53 6f 0a 2a 2a 20 74 68 65 20 63 61 6c . So.** the cal
10dbc 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 28 70 ling function (p
10dbd 63 61 63 68 65 2e 63 29 20 77 69 6c 6c 20 6e 65 cache.c) will ne
10dbe 76 65 72 20 68 61 76 65 20 61 20 63 72 65 61 74 ver have a creat
10dbf 65 46 6c 61 67 20 6f 66 20 31 20 6f 6e 0a 2a 2a eFlag of 1 on.**
10dc0 20 61 20 6e 6f 6e 2d 70 75 72 67 61 62 6c 65 20 a non-purgable
10dc1 63 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 cache..**.** The
10dc2 72 65 20 61 72 65 20 74 68 72 65 65 20 64 69 66 re are three dif
10dc3 66 65 72 65 6e 74 20 61 70 70 72 6f 61 63 68 65 ferent approache
10dc4 73 20 74 6f 20 6f 62 74 61 69 6e 69 6e 67 20 73 s to obtaining s
10dc5 70 61 63 65 20 66 6f 72 20 61 20 70 61 67 65 2c pace for a page,
10dc6 0a 2a 2a 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e .** depending on
10dc7 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 70 61 the value of pa
10dc8 72 61 6d 65 74 65 72 20 63 72 65 61 74 65 46 6c rameter createFl
10dc9 61 67 20 28 77 68 69 63 68 20 6d 61 79 20 62 65 ag (which may be
10dca 20 30 2c 20 31 20 6f 72 20 32 29 2e 0a 2a 2a 0a 0, 1 or 2)..**.
10dcb 2a 2a 20 20 20 31 2e 20 52 65 67 61 72 64 6c 65 ** 1. Regardle
10dcc 73 73 20 6f 66 20 74 68 65 20 76 61 6c 75 65 20 ss of the value
10dcd 6f 66 20 63 72 65 61 74 65 46 6c 61 67 2c 20 74 of createFlag, t
10dce 68 65 20 63 61 63 68 65 20 69 73 20 73 65 61 72 he cache is sear
10dcf 63 68 65 64 20 66 6f 72 20 61 20 0a 2a 2a 20 20 ched for a .**
10dd0 20 20 20 20 63 6f 70 79 20 6f 66 20 74 68 65 20 copy of the
10dd1 72 65 71 75 65 73 74 65 64 20 70 61 67 65 2e 20 requested page.
10dd2 49 66 20 6f 6e 65 20 69 73 20 66 6f 75 6e 64 2c If one is found,
10dd3 20 69 74 20 69 73 20 72 65 74 75 72 6e 65 64 2e it is returned.
10dd4 0a 2a 2a 0a 2a 2a 20 20 20 32 2e 20 49 66 20 63 .**.** 2. If c
10dd5 72 65 61 74 65 46 6c 61 67 3d 3d 30 20 61 6e 64 reateFlag==0 and
10dd6 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 the page is not
10dd7 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 already in the
10dd8 63 61 63 68 65 2c 20 4e 55 4c 4c 20 69 73 0a 2a cache, NULL is.*
10dd9 2a 20 20 20 20 20 20 72 65 74 75 72 6e 65 64 2e * returned.
10dda 0a 2a 2a 0a 2a 2a 20 20 20 33 2e 20 49 66 20 63 .**.** 3. If c
10ddb 72 65 61 74 65 46 6c 61 67 20 69 73 20 31 2c 20 reateFlag is 1,
10ddc 61 6e 64 20 74 68 65 20 70 61 67 65 20 69 73 20 and the page is
10ddd 6e 6f 74 20 61 6c 72 65 61 64 79 20 69 6e 20 74 not already in t
10dde 68 65 20 63 61 63 68 65 2c 0a 2a 2a 20 20 20 20 he cache,.**
10ddf 20 20 61 6e 64 20 69 66 20 65 69 74 68 65 72 20 and if either
10de0 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 of the following
10de1 20 61 72 65 20 74 72 75 65 2c 20 72 65 74 75 72 are true, retur
10de2 6e 20 4e 55 4c 4c 3a 0a 2a 2a 0a 2a 2a 20 20 20 n NULL:.**.**
10de3 20 20 20 20 28 61 29 20 74 68 65 20 6e 75 6d 62 (a) the numb
10de4 65 72 20 6f 66 20 70 61 67 65 73 20 70 69 6e 6e er of pages pinn
10de5 65 64 20 62 79 20 74 68 65 20 63 61 63 68 65 20 ed by the cache
10de6 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 0a is greater than.
10de7 2a 2a 20 20 20 20 20 20 20 20 20 20 20 50 43 61 ** PCa
10de8 63 68 65 31 2e 6e 4d 61 78 2c 20 6f 72 0a 2a 2a che1.nMax, or.**
10de9 20 20 20 20 20 20 20 28 62 29 20 74 68 65 20 6e (b) the n
10dea 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 70 umber of pages p
10deb 69 6e 6e 65 64 20 62 79 20 74 68 65 20 63 61 63 inned by the cac
10dec 68 65 20 69 73 20 67 72 65 61 74 65 72 20 74 68 he is greater th
10ded 61 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 an.**
10dee 74 68 65 20 73 75 6d 20 6f 66 20 6e 4d 61 78 20 the sum of nMax
10def 66 6f 72 20 61 6c 6c 20 70 75 72 67 65 61 62 6c for all purgeabl
10df0 65 20 63 61 63 68 65 73 2c 20 6c 65 73 73 20 74 e caches, less t
10df1 68 65 20 73 75 6d 20 6f 66 20 0a 2a 2a 20 20 20 he sum of .**
10df2 20 20 20 20 20 20 20 20 6e 4d 69 6e 20 66 6f 72 nMin for
10df3 20 61 6c 6c 20 6f 74 68 65 72 20 70 75 72 67 65 all other purge
10df4 61 62 6c 65 20 63 61 63 68 65 73 2e 20 0a 2a 2a able caches. .**
10df5 0a 2a 2a 20 20 20 34 2e 20 49 66 20 6e 6f 6e 65 .** 4. If none
10df6 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 74 68 of the first th
10df7 72 65 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20 61 ree conditions a
10df8 70 70 6c 79 20 61 6e 64 20 74 68 65 20 63 61 63 pply and the cac
10df9 68 65 20 69 73 20 6d 61 72 6b 65 64 0a 2a 2a 20 he is marked.**
10dfa 20 20 20 20 20 61 73 20 70 75 72 67 65 61 62 6c as purgeabl
10dfb 65 2c 20 61 6e 64 20 69 66 20 6f 6e 65 20 6f 66 e, and if one of
10dfc 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 the following i
10dfd 73 20 74 72 75 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 s true:.**.**
10dfe 20 20 20 20 28 61 29 20 54 68 65 20 6e 75 6d 62 (a) The numb
10dff 65 72 20 6f 66 20 70 61 67 65 73 20 61 6c 6c 6f er of pages allo
10e00 63 61 74 65 64 20 66 6f 72 20 74 68 65 20 63 61 cated for the ca
10e01 63 68 65 20 69 73 20 61 6c 72 65 61 64 79 20 0a che is already .
10e02 2a 2a 20 20 20 20 20 20 20 20 20 20 20 50 43 61 ** PCa
10e03 63 68 65 31 2e 6e 4d 61 78 2c 20 6f 72 0a 2a 2a che1.nMax, or.**
10e04 0a 2a 2a 20 20 20 20 20 20 20 28 62 29 20 54 68 .** (b) Th
10e05 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 e number of page
10e06 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 s allocated for
10e07 61 6c 6c 20 70 75 72 67 65 61 62 6c 65 20 63 61 all purgeable ca
10e08 63 68 65 73 20 69 73 0a 2a 2a 20 20 20 20 20 20 ches is.**
10e09 20 20 20 20 20 61 6c 72 65 61 64 79 20 65 71 75 already equ
10e0a 61 6c 20 74 6f 20 6f 72 20 67 72 65 61 74 65 72 al to or greater
10e0b 20 74 68 61 6e 20 74 68 65 20 73 75 6d 20 6f 66 than the sum of
10e0c 20 6e 4d 61 78 20 66 6f 72 20 61 6c 6c 0a 2a 2a nMax for all.**
10e0d 20 20 20 20 20 20 20 20 20 20 20 70 75 72 67 65 purge
10e0e 61 62 6c 65 20 63 61 63 68 65 73 2c 0a 2a 2a 0a able caches,.**.
10e0f 2a 2a 20 20 20 20 20 20 74 68 65 6e 20 61 74 74 ** then att
10e10 65 6d 70 74 20 74 6f 20 72 65 63 79 63 6c 65 20 empt to recycle
10e11 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20 a page from the
10e12 4c 52 55 20 6c 69 73 74 2e 20 49 66 20 69 74 20 LRU list. If it
10e13 69 73 20 74 68 65 20 72 69 67 68 74 0a 2a 2a 20 is the right.**
10e14 20 20 20 20 20 73 69 7a 65 2c 20 72 65 74 75 72 size, retur
10e15 6e 20 74 68 65 20 72 65 63 79 63 6c 65 64 20 62 n the recycled b
10e16 75 66 66 65 72 2e 20 4f 74 68 65 72 77 69 73 65 uffer. Otherwise
10e17 2c 20 66 72 65 65 20 74 68 65 20 62 75 66 66 65 , free the buffe
10e18 72 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 70 72 r and.** pr
10e19 6f 63 65 65 64 20 74 6f 20 73 74 65 70 20 35 2e oceed to step 5.
10e1a 20 0a 2a 2a 0a 2a 2a 20 20 20 35 2e 20 4f 74 68 .**.** 5. Oth
10e1b 65 72 77 69 73 65 2c 20 61 6c 6c 6f 63 61 74 65 erwise, allocate
10e1c 20 61 6e 64 20 72 65 74 75 72 6e 20 61 20 6e 65 and return a ne
10e1d 77 20 70 61 67 65 20 62 75 66 66 65 72 2e 0a 2a w page buffer..*
10e1e 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 70 /.static void *p
10e1f 63 61 63 68 65 31 46 65 74 63 68 28 73 71 6c 69 cache1Fetch(sqli
10e20 74 65 33 5f 70 63 61 63 68 65 20 2a 70 2c 20 75 te3_pcache *p, u
10e21 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 4b 65 79 nsigned int iKey
10e22 2c 20 69 6e 74 20 63 72 65 61 74 65 46 6c 61 67 , int createFlag
10e23 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e ){. unsigned in
10e24 74 20 6e 50 69 6e 6e 65 64 3b 0a 20 20 50 43 61 t nPinned;. PCa
10e25 63 68 65 31 20 2a 70 43 61 63 68 65 20 3d 20 28 che1 *pCache = (
10e26 50 43 61 63 68 65 31 20 2a 29 70 3b 0a 20 20 50 PCache1 *)p;. P
10e27 67 48 64 72 31 20 2a 70 50 61 67 65 20 3d 20 30 gHdr1 *pPage = 0
10e28 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 43 61 ;.. assert( pCa
10e29 63 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 che->bPurgeable
10e2a 7c 7c 20 63 72 65 61 74 65 46 6c 61 67 21 3d 31 || createFlag!=1
10e2b 20 29 3b 0a 20 20 70 63 61 63 68 65 31 45 6e 74 );. pcache1Ent
10e2c 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 69 66 28 erMutex();. if(
10e2d 20 63 72 65 61 74 65 46 6c 61 67 3d 3d 31 20 29 createFlag==1 )
10e2e 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e sqlite3BeginBen
10e2f 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 0a 20 20 ignMalloc();..
10e30 2f 2a 20 53 65 61 72 63 68 20 74 68 65 20 68 61 /* Search the ha
10e31 73 68 20 74 61 62 6c 65 20 66 6f 72 20 61 6e 20 sh table for an
10e32 65 78 69 73 74 69 6e 67 20 65 6e 74 72 79 2e 20 existing entry.
10e33 2a 2f 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d */. if( pCache-
10e34 3e 6e 48 61 73 68 3e 30 20 29 7b 0a 20 20 20 20 >nHash>0 ){.
10e35 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68 20 3d unsigned int h =
10e36 20 69 4b 65 79 20 25 20 70 43 61 63 68 65 2d 3e iKey % pCache->
10e37 6e 48 61 73 68 3b 0a 20 20 20 20 66 6f 72 28 70 nHash;. for(p
10e38 50 61 67 65 3d 70 43 61 63 68 65 2d 3e 61 70 48 Page=pCache->apH
10e39 61 73 68 5b 68 5d 3b 20 70 50 61 67 65 26 26 70 ash[h]; pPage&&p
10e3a 50 61 67 65 2d 3e 69 4b 65 79 21 3d 69 4b 65 79 Page->iKey!=iKey
10e3b 3b 20 70 50 61 67 65 3d 70 50 61 67 65 2d 3e 70 ; pPage=pPage->p
10e3c 4e 65 78 74 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 Next);. }.. if
10e3d 28 20 70 50 61 67 65 20 7c 7c 20 63 72 65 61 74 ( pPage || creat
10e3e 65 46 6c 61 67 3d 3d 30 20 29 7b 0a 20 20 20 20 eFlag==0 ){.
10e3f 70 63 61 63 68 65 31 50 69 6e 50 61 67 65 28 70 pcache1PinPage(p
10e40 50 61 67 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20 Page);. goto
10e41 66 65 74 63 68 5f 6f 75 74 3b 0a 20 20 7d 0a 0a fetch_out;. }..
10e42 20 20 2f 2a 20 53 74 65 70 20 33 20 6f 66 20 68 /* Step 3 of h
10e43 65 61 64 65 72 20 63 6f 6d 6d 65 6e 74 2e 20 2a eader comment. *
10e44 2f 0a 20 20 6e 50 69 6e 6e 65 64 20 3d 20 70 43 /. nPinned = pC
10e45 61 63 68 65 2d 3e 6e 50 61 67 65 20 2d 20 70 43 ache->nPage - pC
10e46 61 63 68 65 2d 3e 6e 52 65 63 79 63 6c 61 62 6c ache->nRecyclabl
10e47 65 3b 0a 20 20 69 66 28 20 63 72 65 61 74 65 46 e;. if( createF
10e48 6c 61 67 3d 3d 31 20 26 26 20 28 0a 20 20 20 20 lag==1 && (.
10e49 20 20 20 20 6e 50 69 6e 6e 65 64 3e 3d 28 70 63 nPinned>=(pc
10e4a 61 63 68 65 31 2e 6e 4d 61 78 50 61 67 65 2b 70 ache1.nMaxPage+p
10e4b 43 61 63 68 65 2d 3e 6e 4d 69 6e 2d 70 63 61 63 Cache->nMin-pcac
10e4c 68 65 31 2e 6e 4d 69 6e 50 61 67 65 29 0a 20 20 he1.nMinPage).
10e4d 20 20 20 7c 7c 20 6e 50 69 6e 6e 65 64 3e 3d 28 || nPinned>=(
10e4e 70 43 61 63 68 65 2d 3e 6e 4d 61 78 20 2a 20 39 pCache->nMax * 9
10e4f 20 2f 20 31 30 29 0a 20 20 29 29 7b 0a 20 20 20 / 10). )){.
10e50 20 67 6f 74 6f 20 66 65 74 63 68 5f 6f 75 74 3b goto fetch_out;
10e51 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 43 61 63 . }.. if( pCac
10e52 68 65 2d 3e 6e 50 61 67 65 3e 3d 70 43 61 63 68 he->nPage>=pCach
10e53 65 2d 3e 6e 48 61 73 68 20 26 26 20 70 63 61 63 e->nHash && pcac
10e54 68 65 31 52 65 73 69 7a 65 48 61 73 68 28 70 43 he1ResizeHash(pC
10e55 61 63 68 65 29 20 29 7b 0a 20 20 20 20 67 6f 74 ache) ){. got
10e56 6f 20 66 65 74 63 68 5f 6f 75 74 3b 0a 20 20 7d o fetch_out;. }
10e57 0a 0a 20 20 2f 2a 20 53 74 65 70 20 34 2e 20 54 .. /* Step 4. T
10e58 72 79 20 74 6f 20 72 65 63 79 63 6c 65 20 61 20 ry to recycle a
10e59 70 61 67 65 20 62 75 66 66 65 72 20 69 66 20 61 page buffer if a
10e5a 70 70 72 6f 70 72 69 61 74 65 2e 20 2a 2f 0a 20 ppropriate. */.
10e5b 20 69 66 28 20 70 43 61 63 68 65 2d 3e 62 50 75 if( pCache->bPu
10e5c 72 67 65 61 62 6c 65 20 26 26 20 70 63 61 63 68 rgeable && pcach
10e5d 65 31 2e 70 4c 72 75 54 61 69 6c 20 26 26 20 28 e1.pLruTail && (
10e5e 0a 20 20 20 20 20 28 70 43 61 63 68 65 2d 3e 6e . (pCache->n
10e5f 50 61 67 65 2b 31 3e 3d 70 43 61 63 68 65 2d 3e Page+1>=pCache->
10e60 6e 4d 61 78 29 20 7c 7c 20 70 63 61 63 68 65 31 nMax) || pcache1
10e61 2e 6e 43 75 72 72 65 6e 74 50 61 67 65 3e 3d 70 .nCurrentPage>=p
10e62 63 61 63 68 65 31 2e 6e 4d 61 78 50 61 67 65 0a cache1.nMaxPage.
10e63 20 20 29 29 7b 0a 20 20 20 20 70 50 61 67 65 20 )){. pPage
10e64 3d 20 70 63 61 63 68 65 31 2e 70 4c 72 75 54 61 = pcache1.pLruTa
10e65 69 6c 3b 0a 20 20 20 20 70 63 61 63 68 65 31 52 il;. pcache1R
10e66 65 6d 6f 76 65 46 72 6f 6d 48 61 73 68 28 70 50 emoveFromHash(pP
10e67 61 67 65 29 3b 0a 20 20 20 20 70 63 61 63 68 65 age);. pcache
10e68 31 50 69 6e 50 61 67 65 28 70 50 61 67 65 29 3b 1PinPage(pPage);
10e69 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e . if( pPage->
10e6a 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 21 3d pCache->szPage!=
10e6b 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 20 29 pCache->szPage )
10e6c 7b 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 46 {. pcache1F
10e6d 72 65 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a reePage(pPage);.
10e6e 20 20 20 20 20 20 70 50 61 67 65 20 3d 20 30 3b pPage = 0;
10e6f 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
10e70 20 20 70 63 61 63 68 65 31 2e 6e 43 75 72 72 65 pcache1.nCurre
10e71 6e 74 50 61 67 65 20 2d 3d 20 28 70 50 61 67 65 ntPage -= (pPage
10e72 2d 3e 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65 ->pCache->bPurge
10e73 61 62 6c 65 20 2d 20 70 43 61 63 68 65 2d 3e 62 able - pCache->b
10e74 50 75 72 67 65 61 62 6c 65 29 3b 0a 20 20 20 20 Purgeable);.
10e75 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 74 65 70 }. }.. /* Step
10e76 20 35 2e 20 49 66 20 61 20 75 73 61 62 6c 65 20 5. If a usable
10e77 70 61 67 65 20 62 75 66 66 65 72 20 68 61 73 20 page buffer has
10e78 73 74 69 6c 6c 20 6e 6f 74 20 62 65 65 6e 20 66 still not been f
10e79 6f 75 6e 64 2c 20 0a 20 20 2a 2a 20 61 74 74 65 ound, . ** atte
10e7a 6d 70 74 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 mpt to allocate
10e7b 61 20 6e 65 77 20 6f 6e 65 2e 20 0a 20 20 2a 2f a new one. . */
10e7c 0a 20 20 69 66 28 20 21 70 50 61 67 65 20 29 7b . if( !pPage ){
10e7d 0a 20 20 20 20 70 50 61 67 65 20 3d 20 70 63 61 . pPage = pca
10e7e 63 68 65 31 41 6c 6c 6f 63 50 61 67 65 28 70 43 che1AllocPage(pC
10e7f 61 63 68 65 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 ache);. }.. if
10e80 28 20 70 50 61 67 65 20 29 7b 0a 20 20 20 20 75 ( pPage ){. u
10e81 6e 73 69 67 6e 65 64 20 69 6e 74 20 68 20 3d 20 nsigned int h =
10e82 69 4b 65 79 20 25 20 70 43 61 63 68 65 2d 3e 6e iKey % pCache->n
10e83 48 61 73 68 3b 0a 20 20 20 20 70 43 61 63 68 65 Hash;. pCache
10e84 2d 3e 6e 50 61 67 65 2b 2b 3b 0a 20 20 20 20 70 ->nPage++;. p
10e85 50 61 67 65 2d 3e 69 4b 65 79 20 3d 20 69 4b 65 Page->iKey = iKe
10e86 79 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 4e y;. pPage->pN
10e87 65 78 74 20 3d 20 70 43 61 63 68 65 2d 3e 61 70 ext = pCache->ap
10e88 48 61 73 68 5b 68 5d 3b 0a 20 20 20 20 70 50 61 Hash[h];. pPa
10e89 67 65 2d 3e 70 43 61 63 68 65 20 3d 20 70 43 61 ge->pCache = pCa
10e8a 63 68 65 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e che;. pPage->
10e8b 70 4c 72 75 50 72 65 76 20 3d 20 30 3b 0a 20 20 pLruPrev = 0;.
10e8c 20 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 pPage->pLruNex
10e8d 74 20 3d 20 30 3b 0a 20 20 20 20 2a 28 76 6f 69 t = 0;. *(voi
10e8e 64 20 2a 2a 29 28 50 47 48 44 52 31 5f 54 4f 5f d **)(PGHDR1_TO_
10e8f 50 41 47 45 28 70 50 61 67 65 29 29 20 3d 20 30 PAGE(pPage)) = 0
10e90 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 61 70 ;. pCache->ap
10e91 48 61 73 68 5b 68 5d 20 3d 20 70 50 61 67 65 3b Hash[h] = pPage;
10e92 0a 20 20 7d 0a 0a 66 65 74 63 68 5f 6f 75 74 3a . }..fetch_out:
10e93 0a 20 20 69 66 28 20 70 50 61 67 65 20 26 26 20 . if( pPage &&
10e94 69 4b 65 79 3e 70 43 61 63 68 65 2d 3e 69 4d 61 iKey>pCache->iMa
10e95 78 4b 65 79 20 29 7b 0a 20 20 20 20 70 43 61 63 xKey ){. pCac
10e96 68 65 2d 3e 69 4d 61 78 4b 65 79 20 3d 20 69 4b he->iMaxKey = iK
10e97 65 79 3b 0a 20 20 7d 0a 20 20 69 66 28 20 63 72 ey;. }. if( cr
10e98 65 61 74 65 46 6c 61 67 3d 3d 31 20 29 20 73 71 eateFlag==1 ) sq
10e99 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 lite3EndBenignMa
10e9a 6c 6c 6f 63 28 29 3b 0a 20 20 70 63 61 63 68 65 lloc();. pcache
10e9b 31 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 1LeaveMutex();.
10e9c 20 72 65 74 75 72 6e 20 28 70 50 61 67 65 20 3f return (pPage ?
10e9d 20 50 47 48 44 52 31 5f 54 4f 5f 50 41 47 45 28 PGHDR1_TO_PAGE(
10e9e 70 50 61 67 65 29 20 3a 20 30 29 3b 0a 7d 0a 0a pPage) : 0);.}..
10e9f 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 ./*.** Implement
10ea0 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c ation of the sql
10ea1 69 74 65 33 5f 70 63 61 63 68 65 2e 78 55 6e 70 ite3_pcache.xUnp
10ea2 69 6e 20 6d 65 74 68 6f 64 2e 0a 2a 2a 0a 2a 2a in method..**.**
10ea3 20 4d 61 72 6b 20 61 20 70 61 67 65 20 61 73 20 Mark a page as
10ea4 75 6e 70 69 6e 6e 65 64 20 28 65 6c 69 67 69 62 unpinned (eligib
10ea5 6c 65 20 66 6f 72 20 61 73 79 6e 63 68 72 6f 6e le for asynchron
10ea6 6f 75 73 20 72 65 63 79 63 6c 69 6e 67 29 2e 0a ous recycling)..
10ea7 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 */.static void p
10ea8 63 61 63 68 65 31 55 6e 70 69 6e 28 73 71 6c 69 cache1Unpin(sqli
10ea9 74 65 33 5f 70 63 61 63 68 65 20 2a 70 2c 20 76 te3_pcache *p, v
10eaa 6f 69 64 20 2a 70 50 67 2c 20 69 6e 74 20 72 65 oid *pPg, int re
10eab 75 73 65 55 6e 6c 69 6b 65 6c 79 29 7b 0a 20 20 useUnlikely){.
10eac 50 43 61 63 68 65 31 20 2a 70 43 61 63 68 65 20 PCache1 *pCache
10ead 3d 20 28 50 43 61 63 68 65 31 20 2a 29 70 3b 0a = (PCache1 *)p;.
10eae 20 20 50 67 48 64 72 31 20 2a 70 50 61 67 65 20 PgHdr1 *pPage
10eaf 3d 20 50 41 47 45 5f 54 4f 5f 50 47 48 44 52 31 = PAGE_TO_PGHDR1
10eb0 28 70 43 61 63 68 65 2c 20 70 50 67 29 3b 0a 20 (pCache, pPg);.
10eb1 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
10eb2 2d 3e 70 43 61 63 68 65 3d 3d 70 43 61 63 68 65 ->pCache==pCache
10eb3 20 29 3b 0a 20 20 70 63 61 63 68 65 31 45 6e 74 );. pcache1Ent
10eb4 65 72 4d 75 74 65 78 28 29 3b 0a 0a 20 20 2f 2a erMutex();.. /*
10eb5 20 49 74 20 69 73 20 61 6e 20 65 72 72 6f 72 20 It is an error
10eb6 74 6f 20 63 61 6c 6c 20 74 68 69 73 20 66 75 6e to call this fun
10eb7 63 74 69 6f 6e 20 69 66 20 74 68 65 20 70 61 67 ction if the pag
10eb8 65 20 69 73 20 61 6c 72 65 61 64 79 20 0a 20 20 e is already .
10eb9 2a 2a 20 70 61 72 74 20 6f 66 20 74 68 65 20 67 ** part of the g
10eba 6c 6f 62 61 6c 20 4c 52 55 20 6c 69 73 74 2e 0a lobal LRU list..
10ebb 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 */. assert( p
10ebc 50 61 67 65 2d 3e 70 4c 72 75 50 72 65 76 3d 3d Page->pLruPrev==
10ebd 30 20 26 26 20 70 50 61 67 65 2d 3e 70 4c 72 75 0 && pPage->pLru
10ebe 4e 65 78 74 3d 3d 30 20 29 3b 0a 20 20 61 73 73 Next==0 );. ass
10ebf 65 72 74 28 20 70 63 61 63 68 65 31 2e 70 4c 72 ert( pcache1.pLr
10ec0 75 48 65 61 64 21 3d 70 50 61 67 65 20 26 26 20 uHead!=pPage &&
10ec1 70 63 61 63 68 65 31 2e 70 4c 72 75 54 61 69 6c pcache1.pLruTail
10ec2 21 3d 70 50 61 67 65 20 29 3b 0a 0a 20 20 69 66 !=pPage );.. if
10ec3 28 20 72 65 75 73 65 55 6e 6c 69 6b 65 6c 79 20 ( reuseUnlikely
10ec4 7c 7c 20 70 63 61 63 68 65 31 2e 6e 43 75 72 72 || pcache1.nCurr
10ec5 65 6e 74 50 61 67 65 3e 70 63 61 63 68 65 31 2e entPage>pcache1.
10ec6 6e 4d 61 78 50 61 67 65 20 29 7b 0a 20 20 20 20 nMaxPage ){.
10ec7 70 63 61 63 68 65 31 52 65 6d 6f 76 65 46 72 6f pcache1RemoveFro
10ec8 6d 48 61 73 68 28 70 50 61 67 65 29 3b 0a 20 20 mHash(pPage);.
10ec9 20 20 70 63 61 63 68 65 31 46 72 65 65 50 61 67 pcache1FreePag
10eca 65 28 70 50 61 67 65 29 3b 0a 20 20 7d 65 6c 73 e(pPage);. }els
10ecb 65 7b 0a 20 20 20 20 2f 2a 20 41 64 64 20 74 68 e{. /* Add th
10ecc 65 20 70 61 67 65 20 74 6f 20 74 68 65 20 67 6c e page to the gl
10ecd 6f 62 61 6c 20 4c 52 55 20 6c 69 73 74 2e 20 4e obal LRU list. N
10ece 6f 72 6d 61 6c 6c 79 2c 20 74 68 65 20 70 61 67 ormally, the pag
10ecf 65 20 69 73 20 61 64 64 65 64 20 74 6f 0a 20 20 e is added to.
10ed0 20 20 2a 2a 20 74 68 65 20 68 65 61 64 20 6f 66 ** the head of
10ed1 20 74 68 65 20 6c 69 73 74 20 28 6c 61 73 74 20 the list (last
10ed2 70 61 67 65 20 74 6f 20 62 65 20 72 65 63 79 63 page to be recyc
10ed3 6c 65 64 29 2e 20 48 6f 77 65 76 65 72 2c 20 69 led). However, i
10ed4 66 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 72 65 f the . ** re
10ed5 75 73 65 55 6e 6c 69 6b 65 6c 79 20 66 6c 61 67 useUnlikely flag
10ed6 20 70 61 73 73 65 64 20 74 6f 20 74 68 69 73 20 passed to this
10ed7 66 75 6e 63 74 69 6f 6e 20 69 73 20 74 72 75 65 function is true
10ed8 2c 20 74 68 65 20 70 61 67 65 20 69 73 20 61 64 , the page is ad
10ed9 64 65 64 0a 20 20 20 20 2a 2a 20 74 6f 20 74 68 ded. ** to th
10eda 65 20 74 61 69 6c 20 6f 66 20 74 68 65 20 6c 69 e tail of the li
10edb 73 74 20 28 66 69 72 73 74 20 70 61 67 65 20 74 st (first page t
10edc 6f 20 62 65 20 72 65 63 79 63 6c 65 64 29 2e 0a o be recycled)..
10edd 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 */. if( p
10ede 63 61 63 68 65 31 2e 70 4c 72 75 48 65 61 64 20 cache1.pLruHead
10edf 29 7b 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 ){. pcache1
10ee0 2e 70 4c 72 75 48 65 61 64 2d 3e 70 4c 72 75 50 .pLruHead->pLruP
10ee1 72 65 76 20 3d 20 70 50 61 67 65 3b 0a 20 20 20 rev = pPage;.
10ee2 20 20 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 pPage->pLruNe
10ee3 78 74 20 3d 20 70 63 61 63 68 65 31 2e 70 4c 72 xt = pcache1.pLr
10ee4 75 48 65 61 64 3b 0a 20 20 20 20 20 20 70 63 61 uHead;. pca
10ee5 63 68 65 31 2e 70 4c 72 75 48 65 61 64 20 3d 20 che1.pLruHead =
10ee6 70 50 61 67 65 3b 0a 20 20 20 20 7d 65 6c 73 65 pPage;. }else
10ee7 7b 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 2e {. pcache1.
10ee8 70 4c 72 75 54 61 69 6c 20 3d 20 70 50 61 67 65 pLruTail = pPage
10ee9 3b 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 2e ;. pcache1.
10eea 70 4c 72 75 48 65 61 64 20 3d 20 70 50 61 67 65 pLruHead = pPage
10eeb 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 43 61 63 ;. }. pCac
10eec 68 65 2d 3e 6e 52 65 63 79 63 6c 61 62 6c 65 2b he->nRecyclable+
10eed 2b 3b 0a 20 20 7d 0a 0a 20 20 70 63 61 63 68 65 +;. }.. pcache
10eee 31 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 7d 1LeaveMutex();.}
10eef 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e ../*.** Implemen
10ef0 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 tation of the sq
10ef1 6c 69 74 65 33 5f 70 63 61 63 68 65 2e 78 52 65 lite3_pcache.xRe
10ef2 6b 65 79 20 6d 65 74 68 6f 64 2e 20 0a 2a 2f 0a key method. .*/.
10ef3 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 static void pcac
10ef4 68 65 31 52 65 6b 65 79 28 0a 20 20 73 71 6c 69 he1Rekey(. sqli
10ef5 74 65 33 5f 70 63 61 63 68 65 20 2a 70 2c 0a 20 te3_pcache *p,.
10ef6 20 76 6f 69 64 20 2a 70 50 67 2c 0a 20 20 75 6e void *pPg,. un
10ef7 73 69 67 6e 65 64 20 69 6e 74 20 69 4f 6c 64 2c signed int iOld,
10ef8 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 . unsigned int
10ef9 69 4e 65 77 0a 29 7b 0a 20 20 50 43 61 63 68 65 iNew.){. PCache
10efa 31 20 2a 70 43 61 63 68 65 20 3d 20 28 50 43 61 1 *pCache = (PCa
10efb 63 68 65 31 20 2a 29 70 3b 0a 20 20 50 67 48 64 che1 *)p;. PgHd
10efc 72 31 20 2a 70 50 61 67 65 20 3d 20 50 41 47 45 r1 *pPage = PAGE
10efd 5f 54 4f 5f 50 47 48 44 52 31 28 70 43 61 63 68 _TO_PGHDR1(pCach
10efe 65 2c 20 70 50 67 29 3b 0a 20 20 50 67 48 64 72 e, pPg);. PgHdr
10eff 31 20 2a 2a 70 70 3b 0a 20 20 75 6e 73 69 67 6e 1 **pp;. unsign
10f00 65 64 20 69 6e 74 20 68 3b 20 0a 20 20 61 73 73 ed int h; . ass
10f01 65 72 74 28 20 70 50 61 67 65 2d 3e 69 4b 65 79 ert( pPage->iKey
10f02 3d 3d 69 4f 6c 64 20 29 3b 0a 20 20 61 73 73 65 ==iOld );. asse
10f03 72 74 28 20 70 50 61 67 65 2d 3e 70 43 61 63 68 rt( pPage->pCach
10f04 65 3d 3d 70 43 61 63 68 65 20 29 3b 0a 0a 20 20 e==pCache );..
10f05 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 pcache1EnterMute
10f06 78 28 29 3b 0a 0a 20 20 68 20 3d 20 69 4f 6c 64 x();.. h = iOld
10f07 25 70 43 61 63 68 65 2d 3e 6e 48 61 73 68 3b 0a %pCache->nHash;.
10f08 20 20 70 70 20 3d 20 26 70 43 61 63 68 65 2d 3e pp = &pCache->
10f09 61 70 48 61 73 68 5b 68 5d 3b 0a 20 20 77 68 69 apHash[h];. whi
10f0a 6c 65 28 20 28 2a 70 70 29 21 3d 70 50 61 67 65 le( (*pp)!=pPage
10f0b 20 29 7b 0a 20 20 20 20 70 70 20 3d 20 26 28 2a ){. pp = &(*
10f0c 70 70 29 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a pp)->pNext;. }.
10f0d 20 20 2a 70 70 20 3d 20 70 50 61 67 65 2d 3e 70 *pp = pPage->p
10f0e 4e 65 78 74 3b 0a 0a 20 20 68 20 3d 20 69 4e 65 Next;.. h = iNe
10f0f 77 25 70 43 61 63 68 65 2d 3e 6e 48 61 73 68 3b w%pCache->nHash;
10f10 0a 20 20 70 50 61 67 65 2d 3e 69 4b 65 79 20 3d . pPage->iKey =
10f11 20 69 4e 65 77 3b 0a 20 20 70 50 61 67 65 2d 3e iNew;. pPage->
10f12 70 4e 65 78 74 20 3d 20 70 43 61 63 68 65 2d 3e pNext = pCache->
10f13 61 70 48 61 73 68 5b 68 5d 3b 0a 20 20 70 43 61 apHash[h];. 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 0a 20 20 2f 2a 20 54 68 pPage;.. /* Th
10f16 65 20 78 52 65 6b 65 79 28 29 20 69 6e 74 65 72 e xRekey() inter
10f17 66 61 63 65 20 69 73 20 6f 6e 6c 79 20 75 73 65 face is only use
10f18 64 20 74 6f 20 6d 6f 76 65 20 70 61 67 65 73 20 d to move pages
10f19 65 61 72 6c 69 65 72 20 69 6e 20 74 68 65 0a 20 earlier in the.
10f1a 20 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c ** database fil
10f1b 65 20 28 69 6e 20 6f 72 64 65 72 20 74 6f 20 6d e (in order to m
10f1c 6f 76 65 20 61 6c 6c 20 66 72 65 65 20 70 61 67 ove all free pag
10f1d 65 73 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 es to the end of
10f1e 20 74 68 65 0a 20 20 2a 2a 20 66 69 6c 65 20 77 the. ** file w
10f1f 68 65 72 65 20 74 68 65 79 20 63 61 6e 20 62 65 here they can be
10f20 20 74 72 75 6e 63 61 74 65 64 20 6f 66 66 2e 29 truncated off.)
10f21 20 20 48 65 6e 63 65 2c 20 69 74 20 69 73 20 6e Hence, it is n
10f22 6f 74 20 70 6f 73 73 69 62 6c 65 0a 20 20 2a 2a ot possible. **
10f23 20 66 6f 72 20 74 68 65 20 6e 65 77 20 70 61 67 for the new pag
10f24 65 20 6e 75 6d 62 65 72 20 74 6f 20 62 65 20 67 e number to be g
10f25 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 65 20 reater than the
10f26 6c 61 72 67 65 73 74 20 70 72 65 76 69 6f 75 73 largest previous
10f27 6c 79 0a 20 20 2a 2a 20 66 65 74 63 68 65 64 20 ly. ** fetched
10f28 70 61 67 65 2e 20 20 42 75 74 20 77 65 20 72 65 page. But we re
10f29 74 61 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 tain the followi
10f2a 6e 67 20 74 65 73 74 20 69 6e 20 63 61 73 65 20 ng test in case
10f2b 78 52 65 6b 65 79 28 29 0a 20 20 2a 2a 20 62 65 xRekey(). ** be
10f2c 67 69 6e 73 20 74 6f 20 62 65 20 75 73 65 64 20 gins to be used
10f2d 69 6e 20 64 69 66 66 65 72 65 6e 74 20 77 61 79 in different way
10f2e 73 20 69 6e 20 74 68 65 20 66 75 74 75 72 65 2e s in the future.
10f2f 0a 20 20 2a 2f 0a 20 20 69 66 28 20 4e 45 56 45 . */. if( NEVE
10f30 52 28 69 4e 65 77 3e 70 43 61 63 68 65 2d 3e 69 R(iNew>pCache->i
10f31 4d 61 78 4b 65 79 29 20 29 7b 0a 20 20 20 20 70 MaxKey) ){. p
10f32 43 61 63 68 65 2d 3e 69 4d 61 78 4b 65 79 20 3d Cache->iMaxKey =
10f33 20 69 4e 65 77 3b 0a 20 20 7d 0a 0a 20 20 70 63 iNew;. }.. pc
10f34 61 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78 28 ache1LeaveMutex(
10f35 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c );.}../*.** Impl
10f36 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 ementation of th
10f37 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 e sqlite3_pcache
10f38 2e 78 54 72 75 6e 63 61 74 65 20 6d 65 74 68 6f .xTruncate metho
10f39 64 2e 20 0a 2a 2a 0a 2a 2a 20 44 69 73 63 61 72 d. .**.** Discar
10f3a 64 20 61 6c 6c 20 75 6e 70 69 6e 6e 65 64 20 70 d all unpinned p
10f3b 61 67 65 73 20 69 6e 20 74 68 65 20 63 61 63 68 ages in the cach
10f3c 65 20 77 69 74 68 20 61 20 70 61 67 65 20 6e 75 e with a page nu
10f3d 6d 62 65 72 20 65 71 75 61 6c 20 74 6f 0a 2a 2a mber equal to.**
10f3e 20 6f 72 20 67 72 65 61 74 65 72 20 74 68 61 6e or greater than
10f3f 20 70 61 72 61 6d 65 74 65 72 20 69 4c 69 6d 69 parameter iLimi
10f40 74 2e 20 41 6e 79 20 70 69 6e 6e 65 64 20 70 61 t. Any pinned pa
10f41 67 65 73 20 77 69 74 68 20 61 20 70 61 67 65 20 ges with a page
10f42 6e 75 6d 62 65 72 0a 2a 2a 20 65 71 75 61 6c 20 number.** equal
10f43 74 6f 20 6f 72 20 67 72 65 61 74 65 72 20 74 68 to or greater th
10f44 61 6e 20 69 4c 69 6d 69 74 20 61 72 65 20 69 6d an iLimit are im
10f45 70 6c 69 63 69 74 6c 79 20 75 6e 70 69 6e 6e 65 plicitly unpinne
10f46 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 d..*/.static voi
10f47 64 20 70 63 61 63 68 65 31 54 72 75 6e 63 61 74 d pcache1Truncat
10f48 65 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 e(sqlite3_pcache
10f49 20 2a 70 2c 20 75 6e 73 69 67 6e 65 64 20 69 6e *p, unsigned in
10f4a 74 20 69 4c 69 6d 69 74 29 7b 0a 20 20 50 43 61 t iLimit){. PCa
10f4b 63 68 65 31 20 2a 70 43 61 63 68 65 20 3d 20 28 che1 *pCache = (
10f4c 50 43 61 63 68 65 31 20 2a 29 70 3b 0a 20 20 70 PCache1 *)p;. p
10f4d 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 cache1EnterMutex
10f4e 28 29 3b 0a 20 20 69 66 28 20 69 4c 69 6d 69 74 ();. if( iLimit
10f4f 3c 3d 70 43 61 63 68 65 2d 3e 69 4d 61 78 4b 65 <=pCache->iMaxKe
10f50 79 20 29 7b 0a 20 20 20 20 70 63 61 63 68 65 31 y ){. pcache1
10f51 54 72 75 6e 63 61 74 65 55 6e 73 61 66 65 28 70 TruncateUnsafe(p
10f52 43 61 63 68 65 2c 20 69 4c 69 6d 69 74 29 3b 0a Cache, iLimit);.
10f53 20 20 20 20 70 43 61 63 68 65 2d 3e 69 4d 61 78 pCache->iMax
10f54 4b 65 79 20 3d 20 69 4c 69 6d 69 74 2d 31 3b 0a Key = iLimit-1;.
10f55 20 20 7d 0a 20 20 70 63 61 63 68 65 31 4c 65 61 }. pcache1Lea
10f56 76 65 4d 75 74 65 78 28 29 3b 0a 7d 0a 0a 2f 2a veMutex();.}../*
10f57 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 .** Implementati
10f58 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 on of the sqlite
10f59 33 5f 70 63 61 63 68 65 2e 78 44 65 73 74 72 6f 3_pcache.xDestro
10f5a 79 20 6d 65 74 68 6f 64 2e 20 0a 2a 2a 0a 2a 2a y method. .**.**
10f5b 20 44 65 73 74 72 6f 79 20 61 20 63 61 63 68 65 Destroy a cache
10f5c 20 61 6c 6c 6f 63 61 74 65 64 20 75 73 69 6e 67 allocated using
10f5d 20 70 63 61 63 68 65 31 43 72 65 61 74 65 28 29 pcache1Create()
10f5e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
10f5f 20 70 63 61 63 68 65 31 44 65 73 74 72 6f 79 28 pcache1Destroy(
10f60 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 2a sqlite3_pcache *
10f61 70 29 7b 0a 20 20 50 43 61 63 68 65 31 20 2a 70 p){. PCache1 *p
10f62 43 61 63 68 65 20 3d 20 28 50 43 61 63 68 65 31 Cache = (PCache1
10f63 20 2a 29 70 3b 0a 20 20 70 63 61 63 68 65 31 45 *)p;. pcache1E
10f64 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 70 nterMutex();. p
10f65 63 61 63 68 65 31 54 72 75 6e 63 61 74 65 55 6e cache1TruncateUn
10f66 73 61 66 65 28 70 43 61 63 68 65 2c 20 30 29 3b safe(pCache, 0);
10f67 0a 20 20 70 63 61 63 68 65 31 2e 6e 4d 61 78 50 . pcache1.nMaxP
10f68 61 67 65 20 2d 3d 20 70 43 61 63 68 65 2d 3e 6e age -= pCache->n
10f69 4d 61 78 3b 0a 20 20 70 63 61 63 68 65 31 2e 6e Max;. pcache1.n
10f6a 4d 69 6e 50 61 67 65 20 2d 3d 20 70 43 61 63 68 MinPage -= pCach
10f6b 65 2d 3e 6e 4d 69 6e 3b 0a 20 20 70 63 61 63 68 e->nMin;. pcach
10f6c 65 31 45 6e 66 6f 72 63 65 4d 61 78 50 61 67 65 e1EnforceMaxPage
10f6d 28 29 3b 0a 20 20 70 63 61 63 68 65 31 4c 65 61 ();. pcache1Lea
10f6e 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 73 71 6c veMutex();. sql
10f6f 69 74 65 33 5f 66 72 65 65 28 70 43 61 63 68 65 ite3_free(pCache
10f70 2d 3e 61 70 48 61 73 68 29 3b 0a 20 20 73 71 6c ->apHash);. sql
10f71 69 74 65 33 5f 66 72 65 65 28 70 43 61 63 68 65 ite3_free(pCache
10f72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 );.}../*.** This
10f73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c function is cal
10f74 6c 65 64 20 64 75 72 69 6e 67 20 69 6e 69 74 69 led during initi
10f75 61 6c 69 7a 61 74 69 6f 6e 20 28 73 71 6c 69 74 alization (sqlit
10f76 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 29 e3_initialize())
10f77 20 74 6f 0a 2a 2a 20 69 6e 73 74 61 6c 6c 20 74 to.** install t
10f78 68 65 20 64 65 66 61 75 6c 74 20 70 6c 75 67 67 he default plugg
10f79 61 62 6c 65 20 63 61 63 68 65 20 6d 6f 64 75 6c able cache modul
10f7a 65 2c 20 61 73 73 75 6d 69 6e 67 20 74 68 65 20 e, assuming the
10f7b 75 73 65 72 20 68 61 73 20 6e 6f 74 0a 2a 2a 20 user has not.**
10f7c 61 6c 72 65 61 64 79 20 70 72 6f 76 69 64 65 64 already provided
10f7d 20 61 6e 20 61 6c 74 65 72 6e 61 74 69 76 65 2e an alternative.
10f7e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
10f7f 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 TE void sqlite3P
10f80 43 61 63 68 65 53 65 74 44 65 66 61 75 6c 74 28 CacheSetDefault(
10f81 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 void){. static
10f82 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d sqlite3_pcache_m
10f83 65 74 68 6f 64 73 20 64 65 66 61 75 6c 74 4d 65 ethods defaultMe
10f84 74 68 6f 64 73 20 3d 20 7b 0a 20 20 20 20 30 2c thods = {. 0,
10f85 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10f86 20 20 20 20 20 20 20 2f 2a 20 70 41 72 67 20 2a /* pArg *
10f87 2f 0a 20 20 20 20 70 63 61 63 68 65 31 49 6e 69 /. pcache1Ini
10f88 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f t, /
10f89 2a 20 78 49 6e 69 74 20 2a 2f 0a 20 20 20 20 70 * xInit */. p
10f8a 63 61 63 68 65 31 53 68 75 74 64 6f 77 6e 2c 20 cache1Shutdown,
10f8b 20 20 20 20 20 20 20 20 2f 2a 20 78 53 68 75 74 /* xShut
10f8c 64 6f 77 6e 20 2a 2f 0a 20 20 20 20 70 63 61 63 down */. pcac
10f8d 68 65 31 43 72 65 61 74 65 2c 20 20 20 20 20 20 he1Create,
10f8e 20 20 20 20 20 2f 2a 20 78 43 72 65 61 74 65 20 /* xCreate
10f8f 2a 2f 0a 20 20 20 20 70 63 61 63 68 65 31 43 61 */. pcache1Ca
10f90 63 68 65 73 69 7a 65 2c 20 20 20 20 20 20 20 20 chesize,
10f91 2f 2a 20 78 43 61 63 68 65 73 69 7a 65 20 2a 2f /* xCachesize */
10f92 0a 20 20 20 20 70 63 61 63 68 65 31 50 61 67 65 . pcache1Page
10f93 63 6f 75 6e 74 2c 20 20 20 20 20 20 20 20 2f 2a count, /*
10f94 20 78 50 61 67 65 63 6f 75 6e 74 20 2a 2f 0a 20 xPagecount */.
10f95 20 20 20 70 63 61 63 68 65 31 46 65 74 63 68 2c pcache1Fetch,
10f96 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 /* x
10f97 46 65 74 63 68 20 2a 2f 0a 20 20 20 20 70 63 61 Fetch */. pca
10f98 63 68 65 31 55 6e 70 69 6e 2c 20 20 20 20 20 20 che1Unpin,
10f99 20 20 20 20 20 20 2f 2a 20 78 55 6e 70 69 6e 20 /* xUnpin
10f9a 2a 2f 0a 20 20 20 20 70 63 61 63 68 65 31 52 65 */. pcache1Re
10f9b 6b 65 79 2c 20 20 20 20 20 20 20 20 20 20 20 20 key,
10f9c 2f 2a 20 78 52 65 6b 65 79 20 2a 2f 0a 20 20 20 /* xRekey */.
10f9d 20 70 63 61 63 68 65 31 54 72 75 6e 63 61 74 65 pcache1Truncate
10f9e 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 78 54 72 , /* xTr
10f9f 75 6e 63 61 74 65 20 2a 2f 0a 20 20 20 20 70 63 uncate */. pc
10fa0 61 63 68 65 31 44 65 73 74 72 6f 79 20 20 20 20 ache1Destroy
10fa1 20 20 20 20 20 20 20 2f 2a 20 78 44 65 73 74 72 /* xDestr
10fa2 6f 79 20 2a 2f 0a 20 20 7d 3b 0a 20 20 73 71 6c oy */. };. sql
10fa3 69 74 65 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49 ite3_config(SQLI
10fa4 54 45 5f 43 4f 4e 46 49 47 5f 50 43 41 43 48 45 TE_CONFIG_PCACHE
10fa5 2c 20 26 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 , &defaultMethod
10fa6 73 29 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 s);.}..#ifdef SQ
10fa7 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f LITE_ENABLE_MEMO
10fa8 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 0a 2f 2a RY_MANAGEMENT./*
10fa9 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f .** This functio
10faa 6e 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 66 n is called to f
10fab 72 65 65 20 73 75 70 65 72 66 6c 75 6f 75 73 20 ree superfluous
10fac 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f dynamically allo
10fad 63 61 74 65 64 20 6d 65 6d 6f 72 79 0a 2a 2a 20 cated memory.**
10fae 68 65 6c 64 20 62 79 20 74 68 65 20 70 61 67 65 held by the page
10faf 72 20 73 79 73 74 65 6d 2e 20 4d 65 6d 6f 72 79 r system. Memory
10fb0 20 69 6e 20 75 73 65 20 62 79 20 61 6e 79 20 53 in use by any S
10fb1 51 4c 69 74 65 20 70 61 67 65 72 20 61 6c 6c 6f QLite pager allo
10fb2 63 61 74 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 cated.** by the
10fb3 63 75 72 72 65 6e 74 20 74 68 72 65 61 64 20 6d current thread m
10fb4 61 79 20 62 65 20 73 71 6c 69 74 65 33 5f 66 72 ay be sqlite3_fr
10fb5 65 65 28 29 65 64 2e 0a 2a 2a 0a 2a 2a 20 6e 52 ee()ed..**.** nR
10fb6 65 71 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 eq is the number
10fb7 20 6f 66 20 62 79 74 65 73 20 6f 66 20 6d 65 6d of bytes of mem
10fb8 6f 72 79 20 72 65 71 75 69 72 65 64 2e 20 4f 6e ory required. On
10fb9 63 65 20 74 68 69 73 20 6d 75 63 68 20 68 61 73 ce this much has
10fba 0a 2a 2a 20 62 65 65 6e 20 72 65 6c 65 61 73 65 .** been release
10fbb 64 2c 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 d, the function
10fbc 72 65 74 75 72 6e 73 2e 20 54 68 65 20 72 65 74 returns. The ret
10fbd 75 72 6e 20 76 61 6c 75 65 20 69 73 20 74 68 65 urn value is the
10fbe 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 0a 2a total number .*
10fbf 2a 20 6f 66 20 62 79 74 65 73 20 6f 66 20 6d 65 * of bytes of me
10fc0 6d 6f 72 79 20 72 65 6c 65 61 73 65 64 2e 0a 2a mory released..*
10fc1 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
10fc2 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 int sqlite3Pcac
10fc3 68 65 52 65 6c 65 61 73 65 4d 65 6d 6f 72 79 28 heReleaseMemory(
10fc4 69 6e 74 20 6e 52 65 71 29 7b 0a 20 20 69 6e 74 int nReq){. int
10fc5 20 6e 46 72 65 65 20 3d 20 30 3b 0a 20 20 69 66 nFree = 0;. if
10fc6 28 20 70 63 61 63 68 65 31 2e 70 53 74 61 72 74 ( pcache1.pStart
10fc7 3d 3d 30 20 29 7b 0a 20 20 20 20 50 67 48 64 72 ==0 ){. PgHdr
10fc8 31 20 2a 70 3b 0a 20 20 20 20 70 63 61 63 68 65 1 *p;. pcache
10fc9 31 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 1EnterMutex();.
10fca 20 20 20 77 68 69 6c 65 28 20 28 6e 52 65 71 3c while( (nReq<
10fcb 30 20 7c 7c 20 6e 46 72 65 65 3c 6e 52 65 71 29 0 || nFree<nReq)
10fcc 20 26 26 20 28 70 3d 70 63 61 63 68 65 31 2e 70 && (p=pcache1.p
10fcd 4c 72 75 54 61 69 6c 29 20 29 7b 0a 20 20 20 20 LruTail) ){.
10fce 20 20 6e 46 72 65 65 20 2b 3d 20 73 71 6c 69 74 nFree += sqlit
10fcf 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 50 47 48 e3MallocSize(PGH
10fd0 44 52 31 5f 54 4f 5f 50 41 47 45 28 70 29 29 3b DR1_TO_PAGE(p));
10fd1 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 50 69 . pcache1Pi
10fd2 6e 50 61 67 65 28 70 29 3b 0a 20 20 20 20 20 20 nPage(p);.
10fd3 70 63 61 63 68 65 31 52 65 6d 6f 76 65 46 72 6f pcache1RemoveFro
10fd4 6d 48 61 73 68 28 70 29 3b 0a 20 20 20 20 20 20 mHash(p);.
10fd5 70 63 61 63 68 65 31 46 72 65 65 50 61 67 65 28 pcache1FreePage(
10fd6 70 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 63 p);. }. pc
10fd7 61 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78 28 ache1LeaveMutex(
10fd8 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
10fd9 6e 46 72 65 65 3b 0a 7d 0a 23 65 6e 64 69 66 20 nFree;.}.#endif
10fda 2f 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 /* SQLITE_ENABLE
10fdb 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 _MEMORY_MANAGEME
10fdc 4e 54 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 NT */..#ifdef SQ
10fdd 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 LITE_TEST./*.**
10fde 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 This function is
10fdf 20 75 73 65 64 20 62 79 20 74 65 73 74 20 70 72 used by test pr
10fe0 6f 63 65 64 75 72 65 73 20 74 6f 20 69 6e 73 70 ocedures to insp
10fe1 65 63 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c ect the internal
10fe2 20 73 74 61 74 65 0a 2a 2a 20 6f 66 20 74 68 65 state.** of the
10fe3 20 67 6c 6f 62 61 6c 20 63 61 63 68 65 2e 0a 2a global cache..*
10fe4 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
10fe5 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 void sqlite3Pca
10fe6 63 68 65 53 74 61 74 73 28 0a 20 20 69 6e 74 20 cheStats(. int
10fe7 2a 70 6e 43 75 72 72 65 6e 74 2c 20 20 20 20 20 *pnCurrent,
10fe8 20 2f 2a 20 4f 55 54 3a 20 54 6f 74 61 6c 20 6e /* OUT: Total n
10fe9 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 63 umber of pages c
10fea 61 63 68 65 64 20 2a 2f 0a 20 20 69 6e 74 20 2a ached */. int *
10feb 70 6e 4d 61 78 2c 20 20 20 20 20 20 20 20 20 20 pnMax,
10fec 2f 2a 20 4f 55 54 3a 20 47 6c 6f 62 61 6c 20 6d /* OUT: Global m
10fed 61 78 69 6d 75 6d 20 63 61 63 68 65 20 73 69 7a aximum cache siz
10fee 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 4d 69 e */. int *pnMi
10fef 6e 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f n, /* O
10ff0 55 54 3a 20 53 75 6d 20 6f 66 20 50 43 61 63 68 UT: Sum of PCach
10ff1 65 31 2e 6e 4d 69 6e 20 66 6f 72 20 70 75 72 67 e1.nMin for purg
10ff2 65 61 62 6c 65 20 63 61 63 68 65 73 20 2a 2f 0a eable caches */.
10ff3 20 20 69 6e 74 20 2a 70 6e 52 65 63 79 63 6c 61 int *pnRecycla
10ff4 62 6c 65 20 20 20 20 2f 2a 20 4f 55 54 3a 20 54 ble /* OUT: T
10ff5 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 otal number of p
10ff6 61 67 65 73 20 61 76 61 69 6c 61 62 6c 65 20 66 ages available f
10ff7 6f 72 20 72 65 63 79 63 6c 69 6e 67 20 2a 2f 0a or recycling */.
10ff8 29 7b 0a 20 20 50 67 48 64 72 31 20 2a 70 3b 0a ){. PgHdr1 *p;.
10ff9 20 20 69 6e 74 20 6e 52 65 63 79 63 6c 61 62 6c int nRecyclabl
10ffa 65 20 3d 20 30 3b 0a 20 20 66 6f 72 28 70 3d 70 e = 0;. for(p=p
10ffb 63 61 63 68 65 31 2e 70 4c 72 75 48 65 61 64 3b cache1.pLruHead;
10ffc 20 70 3b 20 70 3d 70 2d 3e 70 4c 72 75 4e 65 78 p; p=p->pLruNex
10ffd 74 29 7b 0a 20 20 20 20 6e 52 65 63 79 63 6c 61 t){. nRecycla
10ffe 62 6c 65 2b 2b 3b 0a 20 20 7d 0a 20 20 2a 70 6e ble++;. }. *pn
10fff 43 75 72 72 65 6e 74 20 3d 20 70 63 61 63 68 65 Current = pcache
11000 31 2e 6e 43 75 72 72 65 6e 74 50 61 67 65 3b 0a 1.nCurrentPage;.
11001 20 20 2a 70 6e 4d 61 78 20 3d 20 70 63 61 63 68 *pnMax = pcach
11002 65 31 2e 6e 4d 61 78 50 61 67 65 3b 0a 20 20 2a e1.nMaxPage;. *
11003 70 6e 4d 69 6e 20 3d 20 70 63 61 63 68 65 31 2e pnMin = pcache1.
11004 6e 4d 69 6e 50 61 67 65 3b 0a 20 20 2a 70 6e 52 nMinPage;. *pnR
11005 65 63 79 63 6c 61 62 6c 65 20 3d 20 6e 52 65 63 ecyclable = nRec
11006 79 63 6c 61 62 6c 65 3b 0a 7d 0a 23 65 6e 64 69 yclable;.}.#endi
11007 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a f../************
11008 2a 2a 20 45 6e 64 20 6f 66 20 70 63 61 63 68 65 ** End of pcache
11009 31 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 1.c ************
1100a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1100b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1100c 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
1100d 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 72 6f ** Begin file ro
1100e 77 73 65 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a wset.c *********
1100f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
11010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
11011 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 44 65 */./*.** 2008 De
11012 63 65 6d 62 65 72 20 33 0a 2a 2a 0a 2a 2a 20 54 cember 3.**.** T
11013 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 he author discla
11014 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f ims copyright to
11015 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 this source cod
11016 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a e. In place of.
11017 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 ** a legal notic
11018 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 e, here is a ble
11019 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 ssing:.**.**
1101a 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 May you do good
1101b 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a and not evil..**
1101c 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 May you find
1101d 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 forgiveness for
1101e 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f yourself and fo
1101f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a rgive others..**
11020 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 May you shar
11021 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 e freely, never
11022 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e taking more than
11023 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a you give..**.**
11024 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
11025 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
11026 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
11027 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
11028 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 *******.**.** Th
11029 69 73 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d is module implem
1102a 65 6e 74 73 20 61 6e 20 6f 62 6a 65 63 74 20 77 ents an object w
1102b 65 20 63 61 6c 6c 20 61 20 22 52 6f 77 53 65 74 e call a "RowSet
1102c 22 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 52 6f 77 "..**.** The Row
1102d 53 65 74 20 6f 62 6a 65 63 74 20 69 73 20 61 20 Set object is a
1102e 63 6f 6c 6c 65 63 74 69 6f 6e 20 6f 66 20 72 6f collection of ro
1102f 77 69 64 73 2e 20 20 52 6f 77 69 64 73 0a 2a 2a wids. Rowids.**
11030 20 61 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e are inserted in
11031 74 6f 20 74 68 65 20 52 6f 77 53 65 74 20 69 6e to the RowSet in
11032 20 61 6e 20 61 72 62 69 74 72 61 72 79 20 6f 72 an arbitrary or
11033 64 65 72 2e 20 20 49 6e 73 65 72 74 73 0a 2a 2a der. Inserts.**
11034 20 63 61 6e 20 62 65 20 69 6e 74 65 72 6d 69 78 can be intermix
11035 65 64 20 77 69 74 68 20 74 65 73 74 73 20 74 6f ed with tests to
11036 20 73 65 65 20 69 66 20 61 20 67 69 76 65 6e 20 see if a given
11037 72 6f 77 69 64 20 68 61 73 20 62 65 65 6e 0a 2a rowid has been.*
11038 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 69 6e 73 * previously ins
11039 65 72 74 65 64 20 69 6e 74 6f 20 74 68 65 20 52 erted into the R
1103a 6f 77 53 65 74 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 owSet..**.** Aft
1103b 65 72 20 61 6c 6c 20 69 6e 73 65 72 74 73 20 61 er all inserts a
1103c 72 65 20 66 69 6e 69 73 68 65 64 2c 20 69 74 20 re finished, it
1103d 69 73 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 65 is possible to e
1103e 78 74 72 61 63 74 20 74 68 65 0a 2a 2a 20 65 6c xtract the.** el
1103f 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 52 6f ements of the Ro
11040 77 53 65 74 20 69 6e 20 73 6f 72 74 65 64 20 6f wSet in sorted o
11041 72 64 65 72 2e 20 20 4f 6e 63 65 20 74 68 69 73 rder. Once this
11042 20 65 78 74 72 61 63 74 69 6f 6e 0a 2a 2a 20 70 extraction.** p
11043 72 6f 63 65 73 73 20 68 61 73 20 73 74 61 72 74 rocess has start
11044 65 64 2c 20 6e 6f 20 6e 65 77 20 65 6c 65 6d 65 ed, no new eleme
11045 6e 74 73 20 6d 61 79 20 62 65 20 69 6e 73 65 72 nts may be inser
11046 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 48 65 6e 63 65 ted..**.** Hence
11047 2c 20 74 68 65 20 70 72 69 6d 69 74 69 76 65 20 , the primitive
11048 6f 70 65 72 61 74 69 6f 6e 73 20 66 6f 72 20 61 operations for a
11049 20 52 6f 77 53 65 74 20 61 72 65 3a 0a 2a 2a 0a RowSet are:.**.
1104a 2a 2a 20 20 20 20 43 52 45 41 54 45 0a 2a 2a 20 ** CREATE.**
1104b 20 20 20 49 4e 53 45 52 54 0a 2a 2a 20 20 20 20 INSERT.**
1104c 54 45 53 54 0a 2a 2a 20 20 20 20 53 4d 41 4c 4c TEST.** SMALL
1104d 45 53 54 0a 2a 2a 20 20 20 20 44 45 53 54 52 4f EST.** DESTRO
1104e 59 0a 2a 2a 0a 2a 2a 20 54 68 65 20 43 52 45 41 Y.**.** The CREA
1104f 54 45 20 61 6e 64 20 44 45 53 54 52 4f 59 20 70 TE and DESTROY p
11050 72 69 6d 69 74 69 76 65 73 20 61 72 65 20 74 68 rimitives are th
11051 65 20 63 6f 6e 73 74 72 75 63 74 6f 72 20 61 6e e constructor an
11052 64 20 64 65 73 74 72 75 63 74 6f 72 2c 0a 2a 2a d destructor,.**
11053 20 6f 62 76 69 6f 75 73 6c 79 2e 20 20 54 68 65 obviously. The
11054 20 49 4e 53 45 52 54 20 70 72 69 6d 69 74 69 76 INSERT primitiv
11055 65 20 61 64 64 73 20 61 20 6e 65 77 20 65 6c 65 e adds a new ele
11056 6d 65 6e 74 20 74 6f 20 74 68 65 20 52 6f 77 53 ment to the RowS
11057 65 74 2e 0a 2a 2a 20 54 45 53 54 20 63 68 65 63 et..** TEST chec
11058 6b 73 20 74 6f 20 73 65 65 20 69 66 20 61 6e 20 ks to see if an
11059 65 6c 65 6d 65 6e 74 20 69 73 20 61 6c 72 65 61 element is alrea
1105a 64 79 20 69 6e 20 74 68 65 20 52 6f 77 53 65 74 dy in the RowSet
1105b 2e 20 20 53 4d 41 4c 4c 45 53 54 0a 2a 2a 20 65 . SMALLEST.** e
1105c 78 74 72 61 63 74 73 20 74 68 65 20 6c 65 61 73 xtracts the leas
1105d 74 20 76 61 6c 75 65 20 66 72 6f 6d 20 74 68 65 t value from the
1105e 20 52 6f 77 53 65 74 2e 0a 2a 2a 0a 2a 2a 20 54 RowSet..**.** T
1105f 68 65 20 49 4e 53 45 52 54 20 70 72 69 6d 69 74 he INSERT primit
11060 69 76 65 20 6d 69 67 68 74 20 61 6c 6c 6f 63 61 ive might alloca
11061 74 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 6d 65 te additional me
11062 6d 6f 72 79 2e 20 20 4d 65 6d 6f 72 79 20 69 73 mory. Memory is
11063 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e .** allocated in
11064 20 63 68 75 6e 6b 73 20 73 6f 20 6d 6f 73 74 20 chunks so most
11065 49 4e 53 45 52 54 73 20 64 6f 20 6e 6f 20 61 6c INSERTs do no al
11066 6c 6f 63 61 74 69 6f 6e 2e 20 20 54 68 65 72 65 location. There
11067 20 69 73 20 61 6e 20 0a 2a 2a 20 75 70 70 65 72 is an .** upper
11068 20 62 6f 75 6e 64 20 6f 6e 20 74 68 65 20 73 69 bound on the si
11069 7a 65 20 6f 66 20 61 6c 6c 6f 63 61 74 65 64 20 ze of allocated
1106a 6d 65 6d 6f 72 79 2e 20 20 4e 6f 20 6d 65 6d 6f memory. No memo
1106b 72 79 20 69 73 20 66 72 65 65 64 0a 2a 2a 20 75 ry is freed.** u
1106c 6e 74 69 6c 20 44 45 53 54 52 4f 59 2e 0a 2a 2a ntil DESTROY..**
1106d 0a 2a 2a 20 54 68 65 20 54 45 53 54 20 70 72 69 .** The TEST pri
1106e 6d 69 74 69 76 65 20 69 6e 63 6c 75 64 65 73 20 mitive includes
1106f 61 20 22 62 61 74 63 68 22 20 6e 75 6d 62 65 72 a "batch" number
11070 2e 20 20 54 68 65 20 54 45 53 54 20 70 72 69 6d . The TEST prim
11071 69 74 69 76 65 0a 2a 2a 20 77 69 6c 6c 20 6f 6e itive.** will on
11072 6c 79 20 73 65 65 20 65 6c 65 6d 65 6e 74 73 20 ly see elements
11073 74 68 61 74 20 77 65 72 65 20 69 6e 73 65 72 74 that were insert
11074 65 64 20 62 65 66 6f 72 65 20 74 68 65 20 6c 61 ed before the la
11075 73 74 20 63 68 61 6e 67 65 0a 2a 2a 20 69 6e 20 st change.** in
11076 74 68 65 20 62 61 74 63 68 20 6e 75 6d 62 65 72 the batch number
11077 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 . In other word
11078 73 2c 20 69 66 20 61 6e 20 49 4e 53 45 52 54 20 s, if an INSERT
11079 6f 63 63 75 72 73 20 62 65 74 77 65 65 6e 0a 2a occurs between.*
1107a 2a 20 74 77 6f 20 54 45 53 54 73 20 77 68 65 72 * two TESTs wher
1107b 65 20 74 68 65 20 54 45 53 54 73 20 68 61 76 65 e the TESTs have
1107c 20 74 68 65 20 73 61 6d 65 20 62 61 74 63 68 20 the same batch
1107d 6e 75 62 6d 65 72 2c 20 74 68 65 6e 20 74 68 65 nubmer, then the
1107e 0a 2a 2a 20 76 61 6c 75 65 20 61 64 64 65 64 20 .** value added
1107f 62 79 20 74 68 65 20 49 4e 53 45 52 54 20 77 69 by the INSERT wi
11080 6c 6c 20 6e 6f 74 20 62 65 20 76 69 73 69 62 6c ll not be visibl
11081 65 20 74 6f 20 74 68 65 20 73 65 63 6f 6e 64 20 e to the second
11082 54 45 53 54 2e 0a 2a 2a 20 54 68 65 20 69 6e 69 TEST..** The ini
11083 74 69 61 6c 20 62 61 74 63 68 20 6e 75 6d 62 65 tial batch numbe
11084 72 20 69 73 20 7a 65 72 6f 2c 20 73 6f 20 69 66 r is zero, so if
11085 20 74 68 65 20 76 65 72 79 20 66 69 72 73 74 20 the very first
11086 54 45 53 54 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a TEST contains.**
11087 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 62 61 74 63 a non-zero batc
11088 68 20 6e 75 6d 62 65 72 2c 20 69 74 20 77 69 6c h number, it wil
11089 6c 20 73 65 65 20 61 6c 6c 20 70 72 69 6f 72 20 l see all prior
1108a 49 4e 53 45 52 54 73 2e 0a 2a 2a 0a 2a 2a 20 4e INSERTs..**.** N
1108b 6f 20 49 4e 53 45 52 54 73 20 6d 61 79 20 6f 63 o INSERTs may oc
1108c 63 75 72 73 20 61 66 74 65 72 20 61 20 53 4d 41 curs after a SMA
1108d 4c 4c 45 53 54 2e 20 20 41 6e 20 61 73 73 65 72 LLEST. An asser
1108e 74 69 6f 6e 20 77 69 6c 6c 20 66 61 69 6c 20 69 tion will fail i
1108f 66 0a 2a 2a 20 74 68 61 74 20 69 73 20 61 74 74 f.** that is att
11090 65 6d 70 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 empted..**.** Th
11091 65 20 63 6f 73 74 20 6f 66 20 61 6e 20 49 4e 53 e cost of an INS
11092 45 52 54 20 69 73 20 72 6f 75 67 68 6c 79 20 63 ERT is roughly c
11093 6f 6e 73 74 61 6e 74 2e 20 20 28 53 6f 6d 65 74 onstant. (Somet
11094 69 6d 65 20 6e 65 77 20 6d 65 6d 6f 72 79 0a 2a ime new memory.*
11095 2a 20 68 61 73 20 74 6f 20 62 65 20 61 6c 6c 6f * has to be allo
11096 63 61 74 65 64 20 6f 6e 20 61 6e 20 49 4e 53 45 cated on an INSE
11097 52 54 2e 29 20 20 54 68 65 20 63 6f 73 74 20 6f RT.) The cost o
11098 66 20 61 20 54 45 53 54 20 77 69 74 68 20 61 20 f a TEST with a
11099 6e 65 77 0a 2a 2a 20 62 61 74 63 68 20 6e 75 6d new.** batch num
1109a 62 65 72 20 69 73 20 4f 28 4e 6c 6f 67 4e 29 20 ber is O(NlogN)
1109b 77 68 65 72 65 20 4e 20 69 73 20 74 68 65 20 6e where N is the n
1109c 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 umber of element
1109d 73 20 69 6e 20 74 68 65 20 52 6f 77 53 65 74 2e s in the RowSet.
1109e 0a 2a 2a 20 54 68 65 20 63 6f 73 74 20 6f 66 20 .** The cost of
1109f 61 20 54 45 53 54 20 75 73 69 6e 67 20 74 68 65 a TEST using the
110a0 20 73 61 6d 65 20 62 61 74 63 68 20 6e 75 6d 62 same batch numb
110a1 65 72 20 69 73 20 4f 28 6c 6f 67 4e 29 2e 20 20 er is O(logN).
110a2 54 68 65 20 63 6f 73 74 0a 2a 2a 20 6f 66 20 74 The cost.** of t
110a3 68 65 20 66 69 72 73 74 20 53 4d 41 4c 4c 45 53 he first SMALLES
110a4 54 20 69 73 20 4f 28 4e 6c 6f 67 4e 29 2e 20 20 T is O(NlogN).
110a5 53 65 63 6f 6e 64 20 61 6e 64 20 73 75 62 73 65 Second and subse
110a6 71 75 65 6e 74 20 53 4d 41 4c 4c 45 53 54 0a 2a quent SMALLEST.*
110a7 2a 20 70 72 69 6d 69 74 69 76 65 73 20 61 72 65 * primitives are
110a8 20 63 6f 6e 73 74 61 6e 74 20 74 69 6d 65 2e 20 constant time.
110a9 20 54 68 65 20 63 6f 73 74 20 6f 66 20 44 45 53 The cost of DES
110aa 54 52 4f 59 20 69 73 20 4f 28 4e 29 2e 0a 2a 2a TROY is O(N)..**
110ab 0a 2a 2a 20 54 68 65 72 65 20 69 73 20 61 6e 20 .** There is an
110ac 61 64 64 65 64 20 63 6f 73 74 20 6f 66 20 4f 28 added cost of O(
110ad 4e 29 20 77 68 65 6e 20 73 77 69 74 63 68 69 6e N) when switchin
110ae 67 20 62 65 74 77 65 65 6e 20 54 45 53 54 20 61 g between TEST a
110af 6e 64 0a 2a 2a 20 53 4d 41 4c 4c 45 53 54 20 70 nd.** SMALLEST p
110b0 72 69 6d 69 74 69 76 65 73 2e 0a 2a 2a 0a 2a 2a rimitives..**.**
110b1 20 24 49 64 3a 20 72 6f 77 73 65 74 2e 63 2c 76 $Id: rowset.c,v
110b2 20 31 2e 37 20 32 30 30 39 2f 30 35 2f 32 32 20 1.7 2009/05/22
110b3 30 31 3a 30 30 3a 31 33 20 64 72 68 20 45 78 70 01:00:13 drh Exp
110b4 20 24 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 54 61 $.*/.../*.** Ta
110b5 72 67 65 74 20 73 69 7a 65 20 66 6f 72 20 61 6c rget size for al
110b6 6c 6f 63 61 74 69 6f 6e 20 63 68 75 6e 6b 73 2e location chunks.
110b7 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 52 4f 57 53 .*/.#define ROWS
110b8 45 54 5f 41 4c 4c 4f 43 41 54 49 4f 4e 5f 53 49 ET_ALLOCATION_SI
110b9 5a 45 20 31 30 32 34 0a 0a 2f 2a 0a 2a 2a 20 54 ZE 1024../*.** T
110ba 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 he number of row
110bb 73 65 74 20 65 6e 74 72 69 65 73 20 70 65 72 20 set entries per
110bc 61 6c 6c 6f 63 61 74 69 6f 6e 20 63 68 75 6e 6b allocation chunk
110bd 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 52 4f 57 ..*/.#define ROW
110be 53 45 54 5f 45 4e 54 52 59 5f 50 45 52 5f 43 48 SET_ENTRY_PER_CH
110bf 55 4e 4b 20 20 5c 0a 20 20 20 20 20 20 20 20 20 UNK \.
110c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 28 ((
110c1 52 4f 57 53 45 54 5f 41 4c 4c 4f 43 41 54 49 4f ROWSET_ALLOCATIO
110c2 4e 5f 53 49 5a 45 2d 38 29 2f 73 69 7a 65 6f 66 N_SIZE-8)/sizeof
110c3 28 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e (struct RowSetEn
110c4 74 72 79 29 29 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 try))../*.** Eac
110c5 68 20 65 6e 74 72 79 20 69 6e 20 61 20 52 6f 77 h entry in a Row
110c6 53 65 74 20 69 73 20 61 6e 20 69 6e 73 74 61 6e Set is an instan
110c7 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 ce of the follow
110c8 69 6e 67 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 73 ing object..*/.s
110c9 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 truct RowSetEntr
110ca 79 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 0a y { .
110cb 20 20 69 36 34 20 76 3b 20 20 20 20 20 20 20 20 i64 v;
110cc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
110cd 2f 2a 20 52 4f 57 49 44 20 76 61 6c 75 65 20 66 /* ROWID value f
110ce 6f 72 20 74 68 69 73 20 65 6e 74 72 79 20 2a 2f or this entry */
110cf 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 . struct RowSet
110d0 45 6e 74 72 79 20 2a 70 52 69 67 68 74 3b 20 20 Entry *pRight;
110d1 20 2f 2a 20 52 69 67 68 74 20 73 75 62 74 72 65 /* Right subtre
110d2 65 20 28 6c 61 72 67 65 72 20 65 6e 74 72 69 65 e (larger entrie
110d3 73 29 20 6f 72 20 6c 69 73 74 20 2a 2f 0a 20 20 s) or list */.
110d4 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 struct RowSetEnt
110d5 72 79 20 2a 70 4c 65 66 74 3b 20 20 20 20 2f 2a ry *pLeft; /*
110d6 20 4c 65 66 74 20 73 75 62 74 72 65 65 20 28 73 Left subtree (s
110d7 6d 61 6c 6c 65 72 20 65 6e 74 72 69 65 73 29 20 maller entries)
110d8 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52 6f 77 */.};../*.** Row
110d9 53 65 74 45 6e 74 72 79 20 6f 62 6a 65 63 74 73 SetEntry objects
110da 20 61 72 65 20 61 6c 6c 6f 63 61 74 65 64 20 69 are allocated i
110db 6e 20 6c 61 72 67 65 20 63 68 75 6e 6b 73 20 28 n large chunks (
110dc 69 6e 73 74 61 6e 63 65 73 20 6f 66 20 74 68 65 instances of the
110dd 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 .** following st
110de 72 75 63 74 75 72 65 29 20 74 6f 20 72 65 64 75 ructure) to redu
110df 63 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 ce memory alloca
110e0 74 69 6f 6e 20 6f 76 65 72 68 65 61 64 2e 20 20 tion overhead.
110e1 54 68 65 0a 2a 2a 20 63 68 75 6e 6b 73 20 61 72 The.** chunks ar
110e2 65 20 6b 65 70 74 20 6f 6e 20 61 20 6c 69 6e 6b e kept on a link
110e3 65 64 20 6c 69 73 74 20 73 6f 20 74 68 61 74 20 ed list so that
110e4 74 68 65 79 20 63 61 6e 20 62 65 20 64 65 61 6c they can be deal
110e5 6c 6f 63 61 74 65 64 0a 2a 2a 20 77 68 65 6e 20 located.** when
110e6 74 68 65 20 52 6f 77 53 65 74 20 69 73 20 64 65 the RowSet is de
110e7 73 74 72 6f 79 65 64 2e 0a 2a 2f 0a 73 74 72 75 stroyed..*/.stru
110e8 63 74 20 52 6f 77 53 65 74 43 68 75 6e 6b 20 7b ct RowSetChunk {
110e9 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 . struct RowSet
110ea 43 68 75 6e 6b 20 2a 70 4e 65 78 74 43 68 75 6e Chunk *pNextChun
110eb 6b 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 k; /* Nex
110ec 74 20 63 68 75 6e 6b 20 6f 6e 20 6c 69 73 74 20 t chunk on list
110ed 6f 66 20 74 68 65 6d 20 61 6c 6c 20 2a 2f 0a 20 of them all */.
110ee 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e struct RowSetEn
110ef 74 72 79 20 61 45 6e 74 72 79 5b 52 4f 57 53 45 try aEntry[ROWSE
110f0 54 5f 45 4e 54 52 59 5f 50 45 52 5f 43 48 55 4e T_ENTRY_PER_CHUN
110f1 4b 5d 3b 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 64 K]; /* Allocated
110f2 20 65 6e 74 72 69 65 73 20 2a 2f 0a 7d 3b 0a 0a entries */.};..
110f3 2f 2a 0a 2a 2a 20 41 20 52 6f 77 53 65 74 20 69 /*.** A RowSet i
110f4 6e 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 n an instance of
110f5 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 the following s
110f6 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 tructure..**.**
110f7 41 20 74 79 70 65 64 65 66 20 6f 66 20 74 68 69 A typedef of thi
110f8 73 20 73 74 72 75 63 74 75 72 65 20 69 66 20 66 s structure if f
110f9 6f 75 6e 64 20 69 6e 20 73 71 6c 69 74 65 49 6e ound in sqliteIn
110fa 74 2e 68 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 52 t.h..*/.struct R
110fb 6f 77 53 65 74 20 7b 0a 20 20 73 74 72 75 63 74 owSet {. struct
110fc 20 52 6f 77 53 65 74 43 68 75 6e 6b 20 2a 70 43 RowSetChunk *pC
110fd 68 75 6e 6b 3b 20 20 20 20 2f 2a 20 4c 69 73 74 hunk; /* List
110fe 20 6f 66 20 61 6c 6c 20 63 68 75 6e 6b 20 61 6c of all chunk al
110ff 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 73 locations */. s
11100 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 qlite3 *db;
11101 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
11102 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f The database co
11103 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 73 74 nnection */. st
11104 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 ruct RowSetEntry
11105 20 2a 70 45 6e 74 72 79 3b 20 20 20 20 2f 2a 20 *pEntry; /*
11106 4c 69 73 74 20 6f 66 20 65 6e 74 72 69 65 73 20 List of entries
11107 75 73 69 6e 67 20 70 52 69 67 68 74 20 2a 2f 0a using pRight */.
11108 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 struct RowSetE
11109 6e 74 72 79 20 2a 70 4c 61 73 74 3b 20 20 20 20 ntry *pLast;
1110a 20 2f 2a 20 4c 61 73 74 20 65 6e 74 72 79 20 6f /* Last entry o
1110b 6e 20 74 68 65 20 70 45 6e 74 72 79 20 6c 69 73 n the pEntry lis
1110c 74 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 52 6f t */. struct Ro
1110d 77 53 65 74 45 6e 74 72 79 20 2a 70 46 72 65 73 wSetEntry *pFres
1110e 68 3b 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 h; /* Source
1110f 6f 66 20 6e 65 77 20 65 6e 74 72 79 20 6f 62 6a of new entry obj
11110 65 63 74 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 ects */. struct
11111 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 54 RowSetEntry *pT
11112 72 65 65 3b 20 20 20 20 20 2f 2a 20 42 69 6e 61 ree; /* Bina
11113 72 79 20 74 72 65 65 20 6f 66 20 65 6e 74 72 69 ry tree of entri
11114 65 73 20 2a 2f 0a 20 20 75 31 36 20 6e 46 72 65 es */. u16 nFre
11115 73 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 sh;
11116 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
11117 20 6f 66 20 6f 62 6a 65 63 74 73 20 6f 6e 20 70 of objects on p
11118 46 72 65 73 68 20 2a 2f 0a 20 20 75 38 20 69 73 Fresh */. u8 is
11119 53 6f 72 74 65 64 3b 20 20 20 20 20 20 20 20 20 Sorted;
1111a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 /* Tru
1111b 65 20 69 66 20 70 45 6e 74 72 79 20 69 73 20 73 e if pEntry is s
1111c 6f 72 74 65 64 20 2a 2f 0a 20 20 75 38 20 69 42 orted */. u8 iB
1111d 61 74 63 68 3b 20 20 20 20 20 20 20 20 20 20 20 atch;
1111e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 /* Cur
1111f 72 65 6e 74 20 69 6e 73 65 72 74 20 62 61 74 63 rent insert batc
11120 68 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 h */.};../*.** T
11121 75 72 6e 20 62 75 6c 6b 20 6d 65 6d 6f 72 79 20 urn bulk memory
11122 69 6e 74 6f 20 61 20 52 6f 77 53 65 74 20 6f 62 into a RowSet ob
11123 6a 65 63 74 2e 20 20 4e 20 62 79 74 65 73 20 6f ject. N bytes o
11124 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 72 65 20 f memory.** are
11125 61 76 61 69 6c 61 62 6c 65 20 61 74 20 70 53 70 available at pSp
11126 61 63 65 2e 20 20 54 68 65 20 64 62 20 70 6f 69 ace. The db poi
11127 6e 74 65 72 20 69 73 20 75 73 65 64 20 61 73 20 nter is used as
11128 61 20 6d 65 6d 6f 72 79 20 63 6f 6e 74 65 78 74 a memory context
11129 0a 2a 2a 20 66 6f 72 20 61 6e 79 20 73 75 62 73 .** for any subs
1112a 65 71 75 65 6e 74 20 61 6c 6c 6f 63 61 74 69 6f equent allocatio
1112b 6e 73 20 74 68 61 74 20 6e 65 65 64 20 74 6f 20 ns that need to
1112c 6f 63 63 75 72 2e 0a 2a 2a 20 52 65 74 75 72 6e occur..** Return
1112d 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 a pointer to th
1112e 65 20 6e 65 77 20 52 6f 77 53 65 74 20 6f 62 6a e new RowSet obj
1112f 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 6d 75 ect..**.** It mu
11130 73 74 20 62 65 20 74 68 65 20 63 61 73 65 20 74 st be the case t
11131 68 61 74 20 4e 20 69 73 20 73 75 66 66 69 63 69 hat N is suffici
11132 65 6e 74 20 74 6f 20 6d 61 6b 65 20 61 20 52 6f ent to make a Ro
11133 77 73 65 74 2e 20 20 49 66 20 6e 6f 74 0a 2a 2a wset. If not.**
11134 20 61 6e 20 61 73 73 65 72 74 69 6f 6e 20 66 61 an assertion fa
11135 75 6c 74 20 6f 63 63 75 72 73 2e 0a 2a 2a 20 0a ult occurs..** .
11136 2a 2a 20 49 66 20 4e 20 69 73 20 6c 61 72 67 65 ** If N is large
11137 72 20 74 68 61 6e 20 74 68 65 20 6d 69 6e 69 6d r than the minim
11138 75 6d 2c 20 75 73 65 20 74 68 65 20 73 75 72 70 um, use the surp
11139 6c 75 73 20 61 73 20 61 6e 20 69 6e 69 74 69 61 lus as an initia
1113a 6c 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 l.** allocation
1113b 6f 66 20 65 6e 74 72 69 65 73 20 61 76 61 69 6c of entries avail
1113c 61 62 6c 65 20 74 6f 20 62 65 20 66 69 6c 6c 65 able to be fille
1113d 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 d..*/.SQLITE_PRI
1113e 56 41 54 45 20 52 6f 77 53 65 74 20 2a 73 71 6c VATE RowSet *sql
1113f 69 74 65 33 52 6f 77 53 65 74 49 6e 69 74 28 73 ite3RowSetInit(s
11140 71 6c 69 74 65 33 20 2a 64 62 2c 20 76 6f 69 64 qlite3 *db, void
11141 20 2a 70 53 70 61 63 65 2c 20 75 6e 73 69 67 6e *pSpace, unsign
11142 65 64 20 69 6e 74 20 4e 29 7b 0a 20 20 52 6f 77 ed int N){. Row
11143 53 65 74 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 Set *p;. assert
11144 28 20 4e 20 3e 3d 20 52 4f 55 4e 44 38 28 73 69 ( N >= ROUND8(si
11145 7a 65 6f 66 28 2a 70 29 29 20 29 3b 0a 20 20 70 zeof(*p)) );. p
11146 20 3d 20 70 53 70 61 63 65 3b 0a 20 20 70 2d 3e = pSpace;. p->
11147 70 43 68 75 6e 6b 20 3d 20 30 3b 0a 20 20 70 2d pChunk = 0;. p-
11148 3e 64 62 20 3d 20 64 62 3b 0a 20 20 70 2d 3e 70 >db = db;. p->p
11149 45 6e 74 72 79 20 3d 20 30 3b 0a 20 20 70 2d 3e Entry = 0;. p->
1114a 70 4c 61 73 74 20 3d 20 30 3b 0a 20 20 70 2d 3e pLast = 0;. p->
1114b 70 54 72 65 65 20 3d 20 30 3b 0a 20 20 70 2d 3e pTree = 0;. p->
1114c 70 46 72 65 73 68 20 3d 20 28 73 74 72 75 63 74 pFresh = (struct
1114d 20 52 6f 77 53 65 74 45 6e 74 72 79 2a 29 28 52 RowSetEntry*)(R
1114e 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 2a 70 29 OUND8(sizeof(*p)
1114f 29 20 2b 20 28 63 68 61 72 2a 29 70 29 3b 0a 20 ) + (char*)p);.
11150 20 70 2d 3e 6e 46 72 65 73 68 20 3d 20 28 75 31 p->nFresh = (u1
11151 36 29 28 28 4e 20 2d 20 52 4f 55 4e 44 38 28 73 6)((N - ROUND8(s
11152 69 7a 65 6f 66 28 2a 70 29 29 29 2f 73 69 7a 65 izeof(*p)))/size
11153 6f 66 28 73 74 72 75 63 74 20 52 6f 77 53 65 74 of(struct RowSet
11154 45 6e 74 72 79 29 29 3b 0a 20 20 70 2d 3e 69 73 Entry));. p->is
11155 53 6f 72 74 65 64 20 3d 20 31 3b 0a 20 20 70 2d Sorted = 1;. p-
11156 3e 69 42 61 74 63 68 20 3d 20 30 3b 0a 20 20 72 >iBatch = 0;. r
11157 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a eturn p;.}../*.*
11158 2a 20 44 65 61 6c 6c 6f 63 61 74 65 20 61 6c 6c * Deallocate all
11159 20 63 68 75 6e 6b 73 20 66 72 6f 6d 20 61 20 52 chunks from a R
1115a 6f 77 53 65 74 2e 20 20 54 68 69 73 20 66 72 65 owSet. This fre
1115b 65 73 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 74 68 es all memory th
1115c 61 74 0a 2a 2a 20 74 68 65 20 52 6f 77 53 65 74 at.** the RowSet
1115d 20 68 61 73 20 61 6c 6c 6f 63 61 74 65 64 20 6f has allocated o
1115e 76 65 72 20 69 74 73 20 6c 69 66 65 74 69 6d 65 ver its lifetime
1115f 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 . This routine
11160 69 73 0a 2a 2a 20 74 68 65 20 64 65 73 74 72 75 is.** the destru
11161 63 74 6f 72 20 66 6f 72 20 74 68 65 20 52 6f 77 ctor for the Row
11162 53 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 Set..*/.SQLITE_P
11163 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
11164 74 65 33 52 6f 77 53 65 74 43 6c 65 61 72 28 52 te3RowSetClear(R
11165 6f 77 53 65 74 20 2a 70 29 7b 0a 20 20 73 74 72 owSet *p){. str
11166 75 63 74 20 52 6f 77 53 65 74 43 68 75 6e 6b 20 uct RowSetChunk
11167 2a 70 43 68 75 6e 6b 2c 20 2a 70 4e 65 78 74 43 *pChunk, *pNextC
11168 68 75 6e 6b 3b 0a 20 20 66 6f 72 28 70 43 68 75 hunk;. for(pChu
11169 6e 6b 3d 70 2d 3e 70 43 68 75 6e 6b 3b 20 70 43 nk=p->pChunk; pC
1116a 68 75 6e 6b 3b 20 70 43 68 75 6e 6b 20 3d 20 70 hunk; pChunk = p
1116b 4e 65 78 74 43 68 75 6e 6b 29 7b 0a 20 20 20 20 NextChunk){.
1116c 70 4e 65 78 74 43 68 75 6e 6b 20 3d 20 70 43 68 pNextChunk = pCh
1116d 75 6e 6b 2d 3e 70 4e 65 78 74 43 68 75 6e 6b 3b unk->pNextChunk;
1116e 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 . sqlite3DbFr
1116f 65 65 28 70 2d 3e 64 62 2c 20 70 43 68 75 6e 6b ee(p->db, pChunk
11170 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 43 68 75 );. }. p->pChu
11171 6e 6b 20 3d 20 30 3b 0a 20 20 70 2d 3e 6e 46 72 nk = 0;. p->nFr
11172 65 73 68 20 3d 20 30 3b 0a 20 20 70 2d 3e 70 45 esh = 0;. p->pE
11173 6e 74 72 79 20 3d 20 30 3b 0a 20 20 70 2d 3e 70 ntry = 0;. p->p
11174 4c 61 73 74 20 3d 20 30 3b 0a 20 20 70 2d 3e 70 Last = 0;. p->p
11175 54 72 65 65 20 3d 20 30 3b 0a 20 20 70 2d 3e 69 Tree = 0;. p->i
11176 73 53 6f 72 74 65 64 20 3d 20 31 3b 0a 7d 0a 0a sSorted = 1;.}..
11177 2f 2a 0a 2a 2a 20 49 6e 73 65 72 74 20 61 20 6e /*.** Insert a n
11178 65 77 20 76 61 6c 75 65 20 69 6e 74 6f 20 61 20 ew value into a
11179 52 6f 77 53 65 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 RowSet..**.** Th
1117a 65 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66 e mallocFailed f
1117b 6c 61 67 20 6f 66 20 74 68 65 20 64 61 74 61 62 lag of the datab
1117c 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 ase connection i
1117d 73 20 73 65 74 20 69 66 20 61 0a 2a 2a 20 6d 65 s set if a.** me
1117e 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
1117f 66 61 69 6c 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 fails..*/.SQLITE
11180 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
11181 6c 69 74 65 33 52 6f 77 53 65 74 49 6e 73 65 72 lite3RowSetInser
11182 74 28 52 6f 77 53 65 74 20 2a 70 2c 20 69 36 34 t(RowSet *p, i64
11183 20 72 6f 77 69 64 29 7b 0a 20 20 73 74 72 75 63 rowid){. struc
11184 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 t RowSetEntry *p
11185 45 6e 74 72 79 3b 20 20 2f 2a 20 54 68 65 20 6e Entry; /* The n
11186 65 77 20 65 6e 74 72 79 20 2a 2f 0a 20 20 73 74 ew entry */. st
11187 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 ruct RowSetEntry
11188 20 2a 70 4c 61 73 74 3b 20 20 20 2f 2a 20 54 68 *pLast; /* Th
11189 65 20 6c 61 73 74 20 70 72 69 6f 72 20 65 6e 74 e last prior ent
1118a 72 79 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 ry */. assert(
1118b 70 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 2d p!=0 );. if( p-
1118c 3e 6e 46 72 65 73 68 3d 3d 30 20 29 7b 0a 20 20 >nFresh==0 ){.
1118d 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 43 struct RowSetC
1118e 68 75 6e 6b 20 2a 70 4e 65 77 3b 0a 20 20 20 20 hunk *pNew;.
1118f 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 pNew = sqlite3Db
11190 4d 61 6c 6c 6f 63 52 61 77 28 70 2d 3e 64 62 2c MallocRaw(p->db,
11191 20 73 69 7a 65 6f 66 28 2a 70 4e 65 77 29 29 3b sizeof(*pNew));
11192 0a 20 20 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 . if( pNew==0
11193 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
11194 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4e 65 77 ;. }. pNew
11195 2d 3e 70 4e 65 78 74 43 68 75 6e 6b 20 3d 20 70 ->pNextChunk = p
11196 2d 3e 70 43 68 75 6e 6b 3b 0a 20 20 20 20 70 2d ->pChunk;. p-
11197 3e 70 43 68 75 6e 6b 20 3d 20 70 4e 65 77 3b 0a >pChunk = pNew;.
11198 20 20 20 20 70 2d 3e 70 46 72 65 73 68 20 3d 20 p->pFresh =
11199 70 4e 65 77 2d 3e 61 45 6e 74 72 79 3b 0a 20 20 pNew->aEntry;.
1119a 20 20 70 2d 3e 6e 46 72 65 73 68 20 3d 20 52 4f p->nFresh = RO
1119b 57 53 45 54 5f 45 4e 54 52 59 5f 50 45 52 5f 43 WSET_ENTRY_PER_C
1119c 48 55 4e 4b 3b 0a 20 20 7d 0a 20 20 70 45 6e 74 HUNK;. }. pEnt
1119d 72 79 20 3d 20 70 2d 3e 70 46 72 65 73 68 2b 2b ry = p->pFresh++
1119e 3b 0a 20 20 70 2d 3e 6e 46 72 65 73 68 2d 2d 3b ;. p->nFresh--;
1119f 0a 20 20 70 45 6e 74 72 79 2d 3e 76 20 3d 20 72 . pEntry->v = r
111a0 6f 77 69 64 3b 0a 20 20 70 45 6e 74 72 79 2d 3e owid;. pEntry->
111a1 70 52 69 67 68 74 20 3d 20 30 3b 0a 20 20 70 4c pRight = 0;. pL
111a2 61 73 74 20 3d 20 70 2d 3e 70 4c 61 73 74 3b 0a ast = p->pLast;.
111a3 20 20 69 66 28 20 70 4c 61 73 74 20 29 7b 0a 20 if( pLast ){.
111a4 20 20 20 69 66 28 20 70 2d 3e 69 73 53 6f 72 74 if( p->isSort
111a5 65 64 20 26 26 20 72 6f 77 69 64 3c 3d 70 4c 61 ed && rowid<=pLa
111a6 73 74 2d 3e 76 20 29 7b 0a 20 20 20 20 20 20 70 st->v ){. p
111a7 2d 3e 69 73 53 6f 72 74 65 64 20 3d 20 30 3b 0a ->isSorted = 0;.
111a8 20 20 20 20 7d 0a 20 20 20 20 70 4c 61 73 74 2d }. pLast-
111a9 3e 70 52 69 67 68 74 20 3d 20 70 45 6e 74 72 79 >pRight = pEntry
111aa 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 ;. }else{. a
111ab 73 73 65 72 74 28 20 70 2d 3e 70 45 6e 74 72 79 ssert( p->pEntry
111ac 3d 3d 30 20 29 3b 20 2f 2a 20 46 69 72 65 73 20 ==0 ); /* Fires
111ad 69 66 20 49 4e 53 45 52 54 20 61 66 74 65 72 20 if INSERT after
111ae 53 4d 41 4c 4c 45 53 54 20 2a 2f 0a 20 20 20 20 SMALLEST */.
111af 70 2d 3e 70 45 6e 74 72 79 20 3d 20 70 45 6e 74 p->pEntry = pEnt
111b0 72 79 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 4c 61 ry;. }. p->pLa
111b1 73 74 20 3d 20 70 45 6e 74 72 79 3b 0a 7d 0a 0a st = pEntry;.}..
111b2 2f 2a 0a 2a 2a 20 4d 65 72 67 65 20 74 77 6f 20 /*.** Merge two
111b3 6c 69 73 74 73 20 6f 66 20 52 6f 77 53 65 74 45 lists of RowSetE
111b4 6e 74 72 79 20 6f 62 6a 65 63 74 73 2e 20 20 52 ntry objects. R
111b5 65 6d 6f 76 65 20 64 75 70 6c 69 63 61 74 65 73 emove duplicates
111b6 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 70 75 ..**.** The inpu
111b7 74 20 6c 69 73 74 73 20 61 72 65 20 63 6f 6e 6e t lists are conn
111b8 65 63 74 65 64 20 76 69 61 20 70 52 69 67 68 74 ected via pRight
111b9 20 70 6f 69 6e 74 65 72 73 20 61 6e 64 20 61 72 pointers and ar
111ba 65 20 0a 2a 2a 20 61 73 73 75 6d 65 64 20 74 6f e .** assumed to
111bb 20 65 61 63 68 20 61 6c 72 65 61 64 79 20 62 65 each already be
111bc 20 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 65 72 in sorted order
111bd 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75 ..*/.static stru
111be 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a ct RowSetEntry *
111bf 72 6f 77 53 65 74 4d 65 72 67 65 28 0a 20 20 73 rowSetMerge(. s
111c0 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 truct RowSetEntr
111c1 79 20 2a 70 41 2c 20 20 20 20 2f 2a 20 46 69 72 y *pA, /* Fir
111c2 73 74 20 73 6f 72 74 65 64 20 6c 69 73 74 20 74 st sorted list t
111c3 6f 20 62 65 20 6d 65 72 67 65 64 20 2a 2f 0a 20 o be merged */.
111c4 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e struct RowSetEn
111c5 74 72 79 20 2a 70 42 20 20 20 20 20 2f 2a 20 53 try *pB /* S
111c6 65 63 6f 6e 64 20 73 6f 72 74 65 64 20 6c 69 73 econd sorted lis
111c7 74 20 74 6f 20 62 65 20 6d 65 72 67 65 64 20 2a t to be merged *
111c8 2f 0a 29 7b 0a 20 20 73 74 72 75 63 74 20 52 6f /.){. struct Ro
111c9 77 53 65 74 45 6e 74 72 79 20 68 65 61 64 3b 0a wSetEntry head;.
111ca 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 struct RowSetE
111cb 6e 74 72 79 20 2a 70 54 61 69 6c 3b 0a 0a 20 20 ntry *pTail;..
111cc 70 54 61 69 6c 20 3d 20 26 68 65 61 64 3b 0a 20 pTail = &head;.
111cd 20 77 68 69 6c 65 28 20 70 41 20 26 26 20 70 42 while( pA && pB
111ce 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
111cf 70 41 2d 3e 70 52 69 67 68 74 3d 3d 30 20 7c 7c pA->pRight==0 ||
111d0 20 70 41 2d 3e 76 3c 3d 70 41 2d 3e 70 52 69 67 pA->v<=pA->pRig
111d1 68 74 2d 3e 76 20 29 3b 0a 20 20 20 20 61 73 73 ht->v );. ass
111d2 65 72 74 28 20 70 42 2d 3e 70 52 69 67 68 74 3d ert( pB->pRight=
111d3 3d 30 20 7c 7c 20 70 42 2d 3e 76 3c 3d 70 42 2d =0 || pB->v<=pB-
111d4 3e 70 52 69 67 68 74 2d 3e 76 20 29 3b 0a 20 20 >pRight->v );.
111d5 20 20 69 66 28 20 70 41 2d 3e 76 3c 70 42 2d 3e if( pA->v<pB->
111d6 76 20 29 7b 0a 20 20 20 20 20 20 70 54 61 69 6c v ){. pTail
111d7 2d 3e 70 52 69 67 68 74 20 3d 20 70 41 3b 0a 20 ->pRight = pA;.
111d8 20 20 20 20 20 70 41 20 3d 20 70 41 2d 3e 70 52 pA = pA->pR
111d9 69 67 68 74 3b 0a 20 20 20 20 20 20 70 54 61 69 ight;. pTai
111da 6c 20 3d 20 70 54 61 69 6c 2d 3e 70 52 69 67 68 l = pTail->pRigh
111db 74 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 t;. }else if(
111dc 20 70 42 2d 3e 76 3c 70 41 2d 3e 76 20 29 7b 0a pB->v<pA->v ){.
111dd 20 20 20 20 20 20 70 54 61 69 6c 2d 3e 70 52 69 pTail->pRi
111de 67 68 74 20 3d 20 70 42 3b 0a 20 20 20 20 20 20 ght = pB;.
111df 70 42 20 3d 20 70 42 2d 3e 70 52 69 67 68 74 3b pB = pB->pRight;
111e0 0a 20 20 20 20 20 20 70 54 61 69 6c 20 3d 20 70 . pTail = p
111e1 54 61 69 6c 2d 3e 70 52 69 67 68 74 3b 0a 20 20 Tail->pRight;.
111e2 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 }else{. p
111e3 41 20 3d 20 70 41 2d 3e 70 52 69 67 68 74 3b 0a A = pA->pRight;.
111e4 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 }. }. if(
111e5 70 41 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 pA ){. assert
111e6 28 20 70 41 2d 3e 70 52 69 67 68 74 3d 3d 30 20 ( pA->pRight==0
111e7 7c 7c 20 70 41 2d 3e 76 3c 3d 70 41 2d 3e 70 52 || pA->v<=pA->pR
111e8 69 67 68 74 2d 3e 76 20 29 3b 0a 20 20 20 20 70 ight->v );. p
111e9 54 61 69 6c 2d 3e 70 52 69 67 68 74 20 3d 20 70 Tail->pRight = p
111ea 41 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 A;. }else{.
111eb 61 73 73 65 72 74 28 20 70 42 3d 3d 30 20 7c 7c assert( pB==0 ||
111ec 20 70 42 2d 3e 70 52 69 67 68 74 3d 3d 30 20 7c pB->pRight==0 |
111ed 7c 20 70 42 2d 3e 76 3c 3d 70 42 2d 3e 70 52 69 | pB->v<=pB->pRi
111ee 67 68 74 2d 3e 76 20 29 3b 0a 20 20 20 20 70 54 ght->v );. pT
111ef 61 69 6c 2d 3e 70 52 69 67 68 74 20 3d 20 70 42 ail->pRight = pB
111f0 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 68 ;. }. return h
111f1 65 61 64 2e 70 52 69 67 68 74 3b 0a 7d 0a 0a 2f ead.pRight;.}../
111f2 2a 0a 2a 2a 20 53 6f 72 74 20 61 6c 6c 20 65 6c *.** Sort all el
111f3 65 6d 65 6e 74 73 20 6f 6e 20 74 68 65 20 70 45 ements on the pE
111f4 6e 74 72 79 20 6c 69 73 74 20 6f 66 20 74 68 65 ntry list of the
111f5 20 52 6f 77 53 65 74 20 69 6e 74 6f 20 61 73 63 RowSet into asc
111f6 65 6e 64 69 6e 67 20 6f 72 64 65 72 2e 0a 2a 2f ending order..*/
111f7 20 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 6f .static void ro
111f8 77 53 65 74 53 6f 72 74 28 52 6f 77 53 65 74 20 wSetSort(RowSet
111f9 2a 70 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 *p){. unsigned
111fa 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20 int i;. struct
111fb 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 45 6e RowSetEntry *pEn
111fc 74 72 79 3b 0a 20 20 73 74 72 75 63 74 20 52 6f try;. struct Ro
111fd 77 53 65 74 45 6e 74 72 79 20 2a 61 42 75 63 6b wSetEntry *aBuck
111fe 65 74 5b 34 30 5d 3b 0a 0a 20 20 61 73 73 65 72 et[40];.. asser
111ff 74 28 20 70 2d 3e 69 73 53 6f 72 74 65 64 3d 3d t( p->isSorted==
11200 30 20 29 3b 0a 20 20 6d 65 6d 73 65 74 28 61 42 0 );. memset(aB
11201 75 63 6b 65 74 2c 20 30 2c 20 73 69 7a 65 6f 66 ucket, 0, sizeof
11202 28 61 42 75 63 6b 65 74 29 29 3b 0a 20 20 77 68 (aBucket));. wh
11203 69 6c 65 28 20 70 2d 3e 70 45 6e 74 72 79 20 29 ile( p->pEntry )
11204 7b 0a 20 20 20 20 70 45 6e 74 72 79 20 3d 20 70 {. pEntry = p
11205 2d 3e 70 45 6e 74 72 79 3b 0a 20 20 20 20 70 2d ->pEntry;. p-
11206 3e 70 45 6e 74 72 79 20 3d 20 70 45 6e 74 72 79 >pEntry = pEntry
11207 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 70 45 ->pRight;. pE
11208 6e 74 72 79 2d 3e 70 52 69 67 68 74 20 3d 20 30 ntry->pRight = 0
11209 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 61 ;. for(i=0; a
1120a 42 75 63 6b 65 74 5b 69 5d 3b 20 69 2b 2b 29 7b Bucket[i]; i++){
1120b 0a 20 20 20 20 20 20 70 45 6e 74 72 79 20 3d 20 . pEntry =
1120c 72 6f 77 53 65 74 4d 65 72 67 65 28 61 42 75 63 rowSetMerge(aBuc
1120d 6b 65 74 5b 69 5d 2c 20 70 45 6e 74 72 79 29 3b ket[i], pEntry);
1120e 0a 20 20 20 20 20 20 61 42 75 63 6b 65 74 5b 69 . aBucket[i
1120f 5d 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 ] = 0;. }.
11210 20 61 42 75 63 6b 65 74 5b 69 5d 20 3d 20 70 45 aBucket[i] = pE
11211 6e 74 72 79 3b 0a 20 20 7d 0a 20 20 70 45 6e 74 ntry;. }. pEnt
11212 72 79 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69 3d ry = 0;. for(i=
11213 30 3b 20 69 3c 73 69 7a 65 6f 66 28 61 42 75 63 0; i<sizeof(aBuc
11214 6b 65 74 29 2f 73 69 7a 65 6f 66 28 61 42 75 63 ket)/sizeof(aBuc
11215 6b 65 74 5b 30 5d 29 3b 20 69 2b 2b 29 7b 0a 20 ket[0]); i++){.
11216 20 20 20 70 45 6e 74 72 79 20 3d 20 72 6f 77 53 pEntry = rowS
11217 65 74 4d 65 72 67 65 28 70 45 6e 74 72 79 2c 20 etMerge(pEntry,
11218 61 42 75 63 6b 65 74 5b 69 5d 29 3b 0a 20 20 7d aBucket[i]);. }
11219 0a 20 20 70 2d 3e 70 45 6e 74 72 79 20 3d 20 70 . p->pEntry = p
1121a 45 6e 74 72 79 3b 0a 20 20 70 2d 3e 70 4c 61 73 Entry;. p->pLas
1121b 74 20 3d 20 30 3b 0a 20 20 70 2d 3e 69 73 53 6f t = 0;. p->isSo
1121c 72 74 65 64 20 3d 20 31 3b 0a 7d 0a 0a 0a 2f 2a rted = 1;.}.../*
1121d 0a 2a 2a 20 54 68 65 20 69 6e 70 75 74 2c 20 70 .** The input, p
1121e 49 6e 2c 20 69 73 20 61 20 62 69 6e 61 72 79 20 In, is a binary
1121f 74 72 65 65 20 28 6f 72 20 73 75 62 74 72 65 65 tree (or subtree
11220 29 20 6f 66 20 52 6f 77 53 65 74 45 6e 74 72 79 ) of RowSetEntry
11221 20 6f 62 6a 65 63 74 73 2e 0a 2a 2a 20 43 6f 6e objects..** Con
11222 76 65 72 74 20 74 68 69 73 20 74 72 65 65 20 69 vert this tree i
11223 6e 74 6f 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 nto a linked lis
11224 74 20 63 6f 6e 6e 65 63 74 65 64 20 62 79 20 74 t connected by t
11225 68 65 20 70 52 69 67 68 74 20 70 6f 69 6e 74 65 he pRight pointe
11226 72 73 0a 2a 2a 20 61 6e 64 20 72 65 74 75 72 6e rs.** and return
11227 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65 pointers to the
11228 20 66 69 72 73 74 20 61 6e 64 20 6c 61 73 74 20 first and last
11229 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 elements of the
1122a 6e 65 77 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 new list..*/.sta
1122b 74 69 63 20 76 6f 69 64 20 72 6f 77 53 65 74 54 tic void rowSetT
1122c 72 65 65 54 6f 4c 69 73 74 28 0a 20 20 73 74 72 reeToList(. str
1122d 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 uct RowSetEntry
1122e 2a 70 49 6e 2c 20 20 20 20 20 20 20 20 20 2f 2a *pIn, /*
1122f 20 52 6f 6f 74 20 6f 66 20 74 68 65 20 69 6e 70 Root of the inp
11230 75 74 20 74 72 65 65 20 2a 2f 0a 20 20 73 74 72 ut tree */. str
11231 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 uct RowSetEntry
11232 2a 2a 70 70 46 69 72 73 74 2c 20 20 20 20 2f 2a **ppFirst, /*
11233 20 57 72 69 74 65 20 68 65 61 64 20 6f 66 20 74 Write head of t
11234 68 65 20 6f 75 74 70 75 74 20 6c 69 73 74 20 68 he output list h
11235 65 72 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 ere */. struct
11236 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 2a 70 70 RowSetEntry **pp
11237 4c 61 73 74 20 20 20 20 20 20 2f 2a 20 57 72 69 Last /* Wri
11238 74 65 20 74 61 69 6c 20 6f 66 20 74 68 65 20 6f te tail of the o
11239 75 74 70 75 74 20 6c 69 73 74 20 68 65 72 65 20 utput list here
1123a 2a 2f 0a 29 7b 0a 20 20 61 73 73 65 72 74 28 20 */.){. assert(
1123b 70 49 6e 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 pIn!=0 );. if(
1123c 70 49 6e 2d 3e 70 4c 65 66 74 20 29 7b 0a 20 20 pIn->pLeft ){.
1123d 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 struct RowSetE
1123e 6e 74 72 79 20 2a 70 3b 0a 20 20 20 20 72 6f 77 ntry *p;. row
1123f 53 65 74 54 72 65 65 54 6f 4c 69 73 74 28 70 49 SetTreeToList(pI
11240 6e 2d 3e 70 4c 65 66 74 2c 20 70 70 46 69 72 73 n->pLeft, ppFirs
11241 74 2c 20 26 70 29 3b 0a 20 20 20 20 70 2d 3e 70 t, &p);. p->p
11242 52 69 67 68 74 20 3d 20 70 49 6e 3b 0a 20 20 7d Right = pIn;. }
11243 65 6c 73 65 7b 0a 20 20 20 20 2a 70 70 46 69 72 else{. *ppFir
11244 73 74 20 3d 20 70 49 6e 3b 0a 20 20 7d 0a 20 20 st = pIn;. }.
11245 69 66 28 20 70 49 6e 2d 3e 70 52 69 67 68 74 20 if( pIn->pRight
11246 29 7b 0a 20 20 20 20 72 6f 77 53 65 74 54 72 65 ){. rowSetTre
11247 65 54 6f 4c 69 73 74 28 70 49 6e 2d 3e 70 52 69 eToList(pIn->pRi
11248 67 68 74 2c 20 26 70 49 6e 2d 3e 70 52 69 67 68 ght, &pIn->pRigh
11249 74 2c 20 70 70 4c 61 73 74 29 3b 0a 20 20 7d 65 t, ppLast);. }e
1124a 6c 73 65 7b 0a 20 20 20 20 2a 70 70 4c 61 73 74 lse{. *ppLast
1124b 20 3d 20 70 49 6e 3b 0a 20 20 7d 0a 20 20 61 73 = pIn;. }. as
1124c 73 65 72 74 28 20 28 2a 70 70 4c 61 73 74 29 2d sert( (*ppLast)-
1124d 3e 70 52 69 67 68 74 3d 3d 30 20 29 3b 0a 7d 0a >pRight==0 );.}.
1124e 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 ../*.** Convert
1124f 61 20 73 6f 72 74 65 64 20 6c 69 73 74 20 6f 66 a sorted list of
11250 20 65 6c 65 6d 65 6e 74 73 20 28 63 6f 6e 6e 65 elements (conne
11251 63 74 65 64 20 62 79 20 70 52 69 67 68 74 29 20 cted by pRight)
11252 69 6e 74 6f 20 61 20 62 69 6e 61 72 79 0a 2a 2a into a binary.**
11253 20 74 72 65 65 20 77 69 74 68 20 64 65 70 74 68 tree with depth
11254 20 6f 66 20 69 44 65 70 74 68 2e 20 20 41 20 64 of iDepth. A d
11255 65 70 74 68 20 6f 66 20 31 20 6d 65 61 6e 73 20 epth of 1 means
11256 74 68 65 20 74 72 65 65 20 63 6f 6e 74 61 69 6e the tree contain
11257 73 20 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 6e 6f s a single.** no
11258 64 65 20 74 61 6b 65 6e 20 66 72 6f 6d 20 74 68 de taken from th
11259 65 20 68 65 61 64 20 6f 66 20 2a 70 70 4c 69 73 e head of *ppLis
1125a 74 2e 20 20 41 20 64 65 70 74 68 20 6f 66 20 32 t. A depth of 2
1125b 20 6d 65 61 6e 73 20 61 20 74 72 65 65 20 77 69 means a tree wi
1125c 74 68 0a 2a 2a 20 74 68 72 65 65 20 6e 6f 64 65 th.** three node
1125d 73 2e 20 20 41 6e 64 20 73 6f 20 66 6f 72 74 68 s. And so forth
1125e 2e 0a 2a 2a 0a 2a 2a 20 55 73 65 20 61 73 20 6d ..**.** Use as m
1125f 61 6e 79 20 65 6e 74 72 69 65 73 20 66 72 6f 6d any entries from
11260 20 74 68 65 20 69 6e 70 75 74 20 6c 69 73 74 20 the input list
11261 61 73 20 72 65 71 75 69 72 65 64 20 61 6e 64 20 as required and
11262 75 70 64 61 74 65 20 74 68 65 0a 2a 2a 20 2a 70 update the.** *p
11263 70 4c 69 73 74 20 74 6f 20 70 6f 69 6e 74 20 74 pList to point t
11264 6f 20 74 68 65 20 75 6e 75 73 65 64 20 65 6c 65 o the unused ele
11265 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 6c 69 73 ments of the lis
11266 74 2e 20 20 49 66 20 74 68 65 20 69 6e 70 75 74 t. If the input
11267 0a 2a 2a 20 6c 69 73 74 20 63 6f 6e 74 61 69 6e .** list contain
11268 73 20 74 6f 6f 20 66 65 77 20 65 6c 65 6d 65 6e s too few elemen
11269 74 73 2c 20 74 68 65 6e 20 63 6f 6e 73 74 72 75 ts, then constru
1126a 63 74 20 61 6e 20 69 6e 63 6f 6d 70 6c 65 74 65 ct an incomplete
1126b 20 74 72 65 65 0a 2a 2a 20 61 6e 64 20 6c 65 61 tree.** and lea
1126c 76 65 20 2a 70 70 4c 69 73 74 20 73 65 74 20 74 ve *ppList set t
1126d 6f 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 52 65 o NULL..**.** Re
1126e 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 turn a pointer t
1126f 6f 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 74 68 o the root of th
11270 65 20 63 6f 6e 73 74 72 75 63 74 65 64 20 62 69 e constructed bi
11271 6e 61 72 79 20 74 72 65 65 2e 0a 2a 2f 0a 73 74 nary tree..*/.st
11272 61 74 69 63 20 73 74 72 75 63 74 20 52 6f 77 53 atic struct RowS
11273 65 74 45 6e 74 72 79 20 2a 72 6f 77 53 65 74 4e etEntry *rowSetN
11274 44 65 65 70 54 72 65 65 28 0a 20 20 73 74 72 75 DeepTree(. stru
11275 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a ct RowSetEntry *
11276 2a 70 70 4c 69 73 74 2c 0a 20 20 69 6e 74 20 69 *ppList,. int i
11277 44 65 70 74 68 0a 29 7b 0a 20 20 73 74 72 75 63 Depth.){. struc
11278 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 t RowSetEntry *p
11279 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f ; /* Roo
1127a 74 20 6f 66 20 74 68 65 20 6e 65 77 20 74 72 65 t of the new tre
1127b 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 52 6f e */. struct Ro
1127c 77 53 65 74 45 6e 74 72 79 20 2a 70 4c 65 66 74 wSetEntry *pLeft
1127d 3b 20 20 20 20 20 2f 2a 20 4c 65 66 74 20 73 75 ; /* Left su
1127e 62 74 72 65 65 20 2a 2f 0a 20 20 69 66 28 20 2a btree */. if( *
1127f 70 70 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20 ppList==0 ){.
11280 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 return 0;. }.
11281 20 69 66 28 20 69 44 65 70 74 68 3d 3d 31 20 29 if( iDepth==1 )
11282 7b 0a 20 20 20 20 70 20 3d 20 2a 70 70 4c 69 73 {. p = *ppLis
11283 74 3b 0a 20 20 20 20 2a 70 70 4c 69 73 74 20 3d t;. *ppList =
11284 20 70 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 p->pRight;.
11285 70 2d 3e 70 4c 65 66 74 20 3d 20 70 2d 3e 70 52 p->pLeft = p->pR
11286 69 67 68 74 20 3d 20 30 3b 0a 20 20 20 20 72 65 ight = 0;. re
11287 74 75 72 6e 20 70 3b 0a 20 20 7d 0a 20 20 70 4c turn p;. }. pL
11288 65 66 74 20 3d 20 72 6f 77 53 65 74 4e 44 65 65 eft = rowSetNDee
11289 70 54 72 65 65 28 70 70 4c 69 73 74 2c 20 69 44 pTree(ppList, iD
1128a 65 70 74 68 2d 31 29 3b 0a 20 20 70 20 3d 20 2a epth-1);. p = *
1128b 70 70 4c 69 73 74 3b 0a 20 20 69 66 28 20 70 3d ppList;. if( p=
1128c 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e =0 ){. return
1128d 20 70 4c 65 66 74 3b 0a 20 20 7d 0a 20 20 70 2d pLeft;. }. p-
1128e 3e 70 4c 65 66 74 20 3d 20 70 4c 65 66 74 3b 0a >pLeft = pLeft;.
1128f 20 20 2a 70 70 4c 69 73 74 20 3d 20 70 2d 3e 70 *ppList = p->p
11290 52 69 67 68 74 3b 0a 20 20 70 2d 3e 70 52 69 67 Right;. p->pRig
11291 68 74 20 3d 20 72 6f 77 53 65 74 4e 44 65 65 70 ht = rowSetNDeep
11292 54 72 65 65 28 70 70 4c 69 73 74 2c 20 69 44 65 Tree(ppList, iDe
11293 70 74 68 2d 31 29 3b 0a 20 20 72 65 74 75 72 6e pth-1);. return
11294 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e p;.}../*.** Con
11295 76 65 72 74 20 61 20 73 6f 72 74 65 64 20 6c 69 vert a sorted li
11296 73 74 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 69 st of elements i
11297 6e 74 6f 20 61 20 62 69 6e 61 72 79 20 74 72 65 nto a binary tre
11298 65 2e 20 4d 61 6b 65 20 74 68 65 20 74 72 65 65 e. Make the tree
11299 0a 2a 2a 20 61 73 20 64 65 65 70 20 61 73 20 69 .** as deep as i
1129a 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 69 6e t needs to be in
1129b 20 6f 72 64 65 72 20 74 6f 20 63 6f 6e 74 61 69 order to contai
1129c 6e 20 74 68 65 20 65 6e 74 69 72 65 20 6c 69 73 n the entire lis
1129d 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 t..*/.static str
1129e 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 uct RowSetEntry
1129f 2a 72 6f 77 53 65 74 4c 69 73 74 54 6f 54 72 65 *rowSetListToTre
112a0 65 28 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 e(struct RowSetE
112a1 6e 74 72 79 20 2a 70 4c 69 73 74 29 7b 0a 20 20 ntry *pList){.
112a2 69 6e 74 20 69 44 65 70 74 68 3b 20 20 20 20 20 int iDepth;
112a3 20 20 20 20 20 20 2f 2a 20 44 65 70 74 68 20 6f /* Depth o
112a4 66 20 74 68 65 20 74 72 65 65 20 73 6f 20 66 61 f the tree so fa
112a5 72 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 52 6f r */. struct Ro
112a6 77 53 65 74 45 6e 74 72 79 20 2a 70 3b 20 20 20 wSetEntry *p;
112a7 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 74 /* Current t
112a8 72 65 65 20 72 6f 6f 74 20 2a 2f 0a 20 20 73 74 ree root */. st
112a9 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 ruct RowSetEntry
112aa 20 2a 70 4c 65 66 74 3b 20 20 20 2f 2a 20 4c 65 *pLeft; /* Le
112ab 66 74 20 73 75 62 74 72 65 65 20 2a 2f 0a 0a 20 ft subtree */..
112ac 20 61 73 73 65 72 74 28 20 70 4c 69 73 74 21 3d assert( pList!=
112ad 30 20 29 3b 0a 20 20 70 20 3d 20 70 4c 69 73 74 0 );. p = pList
112ae 3b 0a 20 20 70 4c 69 73 74 20 3d 20 70 2d 3e 70 ;. pList = p->p
112af 52 69 67 68 74 3b 0a 20 20 70 2d 3e 70 4c 65 66 Right;. p->pLef
112b0 74 20 3d 20 70 2d 3e 70 52 69 67 68 74 20 3d 20 t = p->pRight =
112b1 30 3b 0a 20 20 66 6f 72 28 69 44 65 70 74 68 3d 0;. for(iDepth=
112b2 31 3b 20 70 4c 69 73 74 3b 20 69 44 65 70 74 68 1; pList; iDepth
112b3 2b 2b 29 7b 0a 20 20 20 20 70 4c 65 66 74 20 3d ++){. pLeft =
112b4 20 70 3b 0a 20 20 20 20 70 20 3d 20 70 4c 69 73 p;. p = pLis
112b5 74 3b 0a 20 20 20 20 70 4c 69 73 74 20 3d 20 70 t;. pList = p
112b6 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 70 2d ->pRight;. p-
112b7 3e 70 4c 65 66 74 20 3d 20 70 4c 65 66 74 3b 0a >pLeft = pLeft;.
112b8 20 20 20 20 70 2d 3e 70 52 69 67 68 74 20 3d 20 p->pRight =
112b9 72 6f 77 53 65 74 4e 44 65 65 70 54 72 65 65 28 rowSetNDeepTree(
112ba 26 70 4c 69 73 74 2c 20 69 44 65 70 74 68 29 3b &pList, iDepth);
112bb 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b . }. return p;
112bc 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 .}../*.** Conver
112bd 74 20 74 68 65 20 6c 69 73 74 20 69 6e 20 70 2d t the list in p-
112be 3e 70 45 6e 74 72 79 20 69 6e 74 6f 20 61 20 73 >pEntry into a s
112bf 6f 72 74 65 64 20 6c 69 73 74 20 69 66 20 69 74 orted list if it
112c0 20 69 73 20 6e 6f 74 0a 2a 2a 20 73 6f 72 74 65 is not.** sorte
112c1 64 20 61 6c 72 65 61 64 79 2e 20 20 49 66 20 74 d already. If t
112c2 68 65 72 65 20 69 73 20 61 20 62 69 6e 61 72 79 here is a binary
112c3 20 74 72 65 65 20 6f 6e 20 70 2d 3e 70 54 72 65 tree on p->pTre
112c4 65 2c 20 74 68 65 6e 0a 2a 2a 20 63 6f 6e 76 65 e, then.** conve
112c5 72 74 20 69 74 20 69 6e 74 6f 20 61 20 6c 69 73 rt it into a lis
112c6 74 20 74 6f 6f 20 61 6e 64 20 6d 65 72 67 65 20 t too and merge
112c7 69 74 20 69 6e 74 6f 20 74 68 65 20 70 2d 3e 70 it into the p->p
112c8 45 6e 74 72 79 20 6c 69 73 74 2e 0a 2a 2f 0a 73 Entry list..*/.s
112c9 74 61 74 69 63 20 76 6f 69 64 20 72 6f 77 53 65 tatic void rowSe
112ca 74 54 6f 4c 69 73 74 28 52 6f 77 53 65 74 20 2a tToList(RowSet *
112cb 70 29 7b 0a 20 20 69 66 28 20 21 70 2d 3e 69 73 p){. if( !p->is
112cc 53 6f 72 74 65 64 20 29 7b 0a 20 20 20 20 72 6f Sorted ){. ro
112cd 77 53 65 74 53 6f 72 74 28 70 29 3b 0a 20 20 7d wSetSort(p);. }
112ce 0a 20 20 69 66 28 20 70 2d 3e 70 54 72 65 65 20 . if( p->pTree
112cf 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 52 6f ){. struct Ro
112d0 77 53 65 74 45 6e 74 72 79 20 2a 70 48 65 61 64 wSetEntry *pHead
112d1 2c 20 2a 70 54 61 69 6c 3b 0a 20 20 20 20 72 6f , *pTail;. ro
112d2 77 53 65 74 54 72 65 65 54 6f 4c 69 73 74 28 70 wSetTreeToList(p
112d3 2d 3e 70 54 72 65 65 2c 20 26 70 48 65 61 64 2c ->pTree, &pHead,
112d4 20 26 70 54 61 69 6c 29 3b 0a 20 20 20 20 70 2d &pTail);. p-
112d5 3e 70 54 72 65 65 20 3d 20 30 3b 0a 20 20 20 20 >pTree = 0;.
112d6 70 2d 3e 70 45 6e 74 72 79 20 3d 20 72 6f 77 53 p->pEntry = rowS
112d7 65 74 4d 65 72 67 65 28 70 2d 3e 70 45 6e 74 72 etMerge(p->pEntr
112d8 79 2c 20 70 48 65 61 64 29 3b 0a 20 20 7d 0a 7d y, pHead);. }.}
112d9 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 72 61 63 74 20 ../*.** Extract
112da 74 68 65 20 73 6d 61 6c 6c 65 73 74 20 65 6c 65 the smallest ele
112db 6d 65 6e 74 20 66 72 6f 6d 20 74 68 65 20 52 6f ment from the Ro
112dc 77 53 65 74 2e 0a 2a 2a 20 57 72 69 74 65 20 74 wSet..** Write t
112dd 68 65 20 65 6c 65 6d 65 6e 74 20 69 6e 74 6f 20 he element into
112de 2a 70 52 6f 77 69 64 2e 20 20 52 65 74 75 72 6e *pRowid. Return
112df 20 31 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 1 on success.
112e0 52 65 74 75 72 6e 0a 2a 2a 20 30 20 69 66 20 74 Return.** 0 if t
112e1 68 65 20 52 6f 77 53 65 74 20 69 73 20 61 6c 72 he RowSet is alr
112e2 65 61 64 79 20 65 6d 70 74 79 2e 0a 2a 2a 0a 2a eady empty..**.*
112e3 2a 20 41 66 74 65 72 20 74 68 69 73 20 72 6f 75 * After this rou
112e4 74 69 6e 65 20 68 61 73 20 62 65 65 6e 20 63 61 tine has been ca
112e5 6c 6c 65 64 2c 20 74 68 65 20 73 71 6c 69 74 65 lled, the sqlite
112e6 33 52 6f 77 53 65 74 49 6e 73 65 72 74 28 29 0a 3RowSetInsert().
112e7 2a 2a 20 72 6f 75 74 69 6e 65 20 6d 61 79 20 6e ** routine may n
112e8 6f 74 20 62 65 20 63 61 6c 6c 65 64 20 61 67 61 ot be called aga
112e9 69 6e 2e 20 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f in. .*/.SQLITE_
112ea 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
112eb 74 65 33 52 6f 77 53 65 74 4e 65 78 74 28 52 6f te3RowSetNext(Ro
112ec 77 53 65 74 20 2a 70 2c 20 69 36 34 20 2a 70 52 wSet *p, i64 *pR
112ed 6f 77 69 64 29 7b 0a 20 20 72 6f 77 53 65 74 54 owid){. rowSetT
112ee 6f 4c 69 73 74 28 70 29 3b 0a 20 20 69 66 28 20 oList(p);. if(
112ef 70 2d 3e 70 45 6e 74 72 79 20 29 7b 0a 20 20 20 p->pEntry ){.
112f0 20 2a 70 52 6f 77 69 64 20 3d 20 70 2d 3e 70 45 *pRowid = p->pE
112f1 6e 74 72 79 2d 3e 76 3b 0a 20 20 20 20 70 2d 3e ntry->v;. p->
112f2 70 45 6e 74 72 79 20 3d 20 70 2d 3e 70 45 6e 74 pEntry = p->pEnt
112f3 72 79 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 ry->pRight;.
112f4 69 66 28 20 70 2d 3e 70 45 6e 74 72 79 3d 3d 30 if( p->pEntry==0
112f5 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
112f6 33 52 6f 77 53 65 74 43 6c 65 61 72 28 70 29 3b 3RowSetClear(p);
112f7 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 . }. retur
112f8 6e 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 n 1;. }else{.
112f9 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a return 0;. }.
112fa 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 }../*.** Check t
112fb 6f 20 73 65 65 20 69 66 20 65 6c 65 6d 65 6e 74 o see if element
112fc 20 69 52 6f 77 69 64 20 77 61 73 20 69 6e 73 65 iRowid was inse
112fd 72 74 65 64 20 69 6e 74 6f 20 74 68 65 20 74 68 rted into the th
112fe 65 20 72 6f 77 73 65 74 20 61 73 0a 2a 2a 20 70 e rowset as.** p
112ff 61 72 74 20 6f 66 20 61 6e 79 20 69 6e 73 65 72 art of any inser
11300 74 20 62 61 74 63 68 20 70 72 69 6f 72 20 74 6f t batch prior to
11301 20 69 42 61 74 63 68 2e 20 20 52 65 74 75 72 6e iBatch. Return
11302 20 31 20 6f 72 20 30 2e 0a 2a 2f 0a 53 51 4c 49 1 or 0..*/.SQLI
11303 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
11304 71 6c 69 74 65 33 52 6f 77 53 65 74 54 65 73 74 qlite3RowSetTest
11305 28 52 6f 77 53 65 74 20 2a 70 52 6f 77 53 65 74 (RowSet *pRowSet
11306 2c 20 75 38 20 69 42 61 74 63 68 2c 20 73 71 6c , u8 iBatch, sql
11307 69 74 65 33 5f 69 6e 74 36 34 20 69 52 6f 77 69 ite3_int64 iRowi
11308 64 29 7b 0a 20 20 73 74 72 75 63 74 20 52 6f 77 d){. struct Row
11309 53 65 74 45 6e 74 72 79 20 2a 70 3b 0a 20 20 69 SetEntry *p;. i
1130a 66 28 20 69 42 61 74 63 68 21 3d 70 52 6f 77 53 f( iBatch!=pRowS
1130b 65 74 2d 3e 69 42 61 74 63 68 20 29 7b 0a 20 20 et->iBatch ){.
1130c 20 20 69 66 28 20 70 52 6f 77 53 65 74 2d 3e 70 if( pRowSet->p
1130d 45 6e 74 72 79 20 29 7b 0a 20 20 20 20 20 20 72 Entry ){. r
1130e 6f 77 53 65 74 54 6f 4c 69 73 74 28 70 52 6f 77 owSetToList(pRow
1130f 53 65 74 29 3b 0a 20 20 20 20 20 20 70 52 6f 77 Set);. pRow
11310 53 65 74 2d 3e 70 54 72 65 65 20 3d 20 72 6f 77 Set->pTree = row
11311 53 65 74 4c 69 73 74 54 6f 54 72 65 65 28 70 52 SetListToTree(pR
11312 6f 77 53 65 74 2d 3e 70 45 6e 74 72 79 29 3b 0a owSet->pEntry);.
11313 20 20 20 20 20 20 70 52 6f 77 53 65 74 2d 3e 70 pRowSet->p
11314 45 6e 74 72 79 20 3d 20 30 3b 0a 20 20 20 20 20 Entry = 0;.
11315 20 70 52 6f 77 53 65 74 2d 3e 70 4c 61 73 74 20 pRowSet->pLast
11316 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 = 0;. }. p
11317 52 6f 77 53 65 74 2d 3e 69 42 61 74 63 68 20 3d RowSet->iBatch =
11318 20 69 42 61 74 63 68 3b 0a 20 20 7d 0a 20 20 70 iBatch;. }. p
11319 20 3d 20 70 52 6f 77 53 65 74 2d 3e 70 54 72 65 = pRowSet->pTre
1131a 65 3b 0a 20 20 77 68 69 6c 65 28 20 70 20 29 7b e;. while( p ){
1131b 0a 20 20 20 20 69 66 28 20 70 2d 3e 76 3c 69 52 . if( p->v<iR
1131c 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 70 20 owid ){. p
1131d 3d 20 70 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 = p->pRight;.
1131e 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 76 3e }else if( p->v>
1131f 69 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 iRowid ){.
11320 70 20 3d 20 70 2d 3e 70 4c 65 66 74 3b 0a 20 20 p = p->pLeft;.
11321 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 }else{. r
11322 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 eturn 1;. }.
11323 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d }. return 0;.}
11324 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
11325 2a 20 45 6e 64 20 6f 66 20 72 6f 77 73 65 74 2e * End of rowset.
11326 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
11327 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
11328 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
11329 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
1132a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 70 61 67 * Begin file pag
1132b 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a er.c ***********
1132c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1132d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1132e 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 /./*.** 2001 Sep
1132f 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 tember 15.**.**
11330 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c The author discl
11331 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 aims copyright t
11332 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f o this source co
11333 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 de. In place of
11334 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 .** a legal noti
11335 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c ce, here is a bl
11336 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 essing:.**.**
11337 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 May you do good
11338 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a and not evil..*
11339 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e * May you fin
1133a 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f d forgiveness fo
1133b 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 r yourself and f
1133c 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a orgive others..*
1133d 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 * May you sha
1133e 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 re freely, never
1133f 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 taking more tha
11340 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a n you give..**.*
11341 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
11342 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
11343 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
11344 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
11345 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 ********.** This
11346 20 69 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e is the implemen
11347 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 70 61 tation of the pa
11348 67 65 20 63 61 63 68 65 20 73 75 62 73 79 73 74 ge cache subsyst
11349 65 6d 20 6f 72 20 22 70 61 67 65 72 22 2e 0a 2a em or "pager"..*
1134a 2a 20 0a 2a 2a 20 54 68 65 20 70 61 67 65 72 20 * .** The pager
1134b 69 73 20 75 73 65 64 20 74 6f 20 61 63 63 65 73 is used to acces
1134c 73 20 61 20 64 61 74 61 62 61 73 65 20 64 69 73 s a database dis
1134d 6b 20 66 69 6c 65 2e 20 20 49 74 20 69 6d 70 6c k file. It impl
1134e 65 6d 65 6e 74 73 0a 2a 2a 20 61 74 6f 6d 69 63 ements.** atomic
1134f 20 63 6f 6d 6d 69 74 20 61 6e 64 20 72 6f 6c 6c commit and roll
11350 62 61 63 6b 20 74 68 72 6f 75 67 68 20 74 68 65 back through the
11351 20 75 73 65 20 6f 66 20 61 20 6a 6f 75 72 6e 61 use of a journa
11352 6c 20 66 69 6c 65 20 74 68 61 74 0a 2a 2a 20 69 l file that.** i
11353 73 20 73 65 70 61 72 61 74 65 20 66 72 6f 6d 20 s separate from
11354 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
11355 65 2e 20 20 54 68 65 20 70 61 67 65 72 20 61 6c e. The pager al
11356 73 6f 20 69 6d 70 6c 65 6d 65 6e 74 73 20 66 69 so implements fi
11357 6c 65 0a 2a 2a 20 6c 6f 63 6b 69 6e 67 20 74 6f le.** locking to
11358 20 70 72 65 76 65 6e 74 20 74 77 6f 20 70 72 6f prevent two pro
11359 63 65 73 73 65 73 20 66 72 6f 6d 20 77 72 69 74 cesses from writ
1135a 69 6e 67 20 74 68 65 20 73 61 6d 65 20 64 61 74 ing the same dat
1135b 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 20 73 69 abase.** file si
1135c 6d 75 6c 74 61 6e 65 6f 75 73 6c 79 2c 20 6f 72 multaneously, or
1135d 20 6f 6e 65 20 70 72 6f 63 65 73 73 20 66 72 6f one process fro
1135e 6d 20 72 65 61 64 69 6e 67 20 74 68 65 20 64 61 m reading the da
1135f 74 61 62 61 73 65 20 77 68 69 6c 65 0a 2a 2a 20 tabase while.**
11360 61 6e 6f 74 68 65 72 20 69 73 20 77 72 69 74 69 another is writi
11361 6e 67 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 ng..**.** @(#) $
11362 49 64 3a 20 70 61 67 65 72 2e 63 2c 76 20 31 2e Id: pager.c,v 1.
11363 36 32 39 20 32 30 30 39 2f 30 38 2f 31 30 20 31 629 2009/08/10 1
11364 37 3a 34 38 3a 35 37 20 64 72 68 20 45 78 70 20 7:48:57 drh Exp
11365 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c $.*/.#ifndef SQL
11366 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f 0a ITE_OMIT_DISKIO.
11367 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 66 6f ./*.** Macros fo
11368 72 20 74 72 6f 75 62 6c 65 73 68 6f 6f 74 69 6e r troubleshootin
11369 67 2e 20 20 4e 6f 72 6d 61 6c 6c 79 20 74 75 72 g. Normally tur
1136a 6e 65 64 20 6f 66 66 0a 2a 2f 0a 23 69 66 20 30 ned off.*/.#if 0
1136b 0a 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 .int sqlite3Page
1136c 72 54 72 61 63 65 3d 31 3b 20 20 2f 2a 20 54 72 rTrace=1; /* Tr
1136d 75 65 20 74 6f 20 65 6e 61 62 6c 65 20 74 72 61 ue to enable tra
1136e 63 69 6e 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20 cing */.#define
1136f 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e sqlite3DebugPrin
11370 74 66 20 70 72 69 6e 74 66 0a 23 64 65 66 69 6e tf printf.#defin
11371 65 20 50 41 47 45 52 54 52 41 43 45 28 58 29 20 e PAGERTRACE(X)
11372 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 50 if( sqlite3P
11373 61 67 65 72 54 72 61 63 65 20 29 7b 20 73 71 6c agerTrace ){ sql
11374 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 20 ite3DebugPrintf
11375 58 3b 20 7d 0a 23 65 6c 73 65 0a 23 64 65 66 69 X; }.#else.#defi
11376 6e 65 20 50 41 47 45 52 54 52 41 43 45 28 58 29 ne PAGERTRACE(X)
11377 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 .#endif../*.** T
11378 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 77 6f he following two
11379 20 6d 61 63 72 6f 73 20 61 72 65 20 75 73 65 64 macros are used
1137a 20 77 69 74 68 69 6e 20 74 68 65 20 50 41 47 45 within the PAGE
1137b 52 54 52 41 43 45 28 29 20 6d 61 63 72 6f 73 20 RTRACE() macros
1137c 61 62 6f 76 65 0a 2a 2a 20 74 6f 20 70 72 69 6e above.** to prin
1137d 74 20 6f 75 74 20 66 69 6c 65 2d 64 65 73 63 72 t out file-descr
1137e 69 70 74 6f 72 73 2e 20 0a 2a 2a 0a 2a 2a 20 50 iptors. .**.** P
1137f 41 47 45 52 49 44 28 29 20 74 61 6b 65 73 20 61 AGERID() takes a
11380 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 50 61 pointer to a Pa
11381 67 65 72 20 73 74 72 75 63 74 20 61 73 20 69 74 ger struct as it
11382 73 20 61 72 67 75 6d 65 6e 74 2e 20 54 68 65 0a s argument. The.
11383 2a 2a 20 61 73 73 6f 63 69 61 74 65 64 20 66 69 ** associated fi
11384 6c 65 2d 64 65 73 63 72 69 70 74 6f 72 20 69 73 le-descriptor is
11385 20 72 65 74 75 72 6e 65 64 2e 20 46 49 4c 45 48 returned. FILEH
11386 41 4e 44 4c 45 49 44 28 29 20 74 61 6b 65 73 20 ANDLEID() takes
11387 61 6e 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 0a an sqlite3_file.
11388 2a 2a 20 73 74 72 75 63 74 20 61 73 20 69 74 73 ** struct as its
11389 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 23 64 argument..*/.#d
1138a 65 66 69 6e 65 20 50 41 47 45 52 49 44 28 70 29 efine PAGERID(p)
1138b 20 28 28 69 6e 74 29 28 70 2d 3e 66 64 29 29 0a ((int)(p->fd)).
1138c 23 64 65 66 69 6e 65 20 46 49 4c 45 48 41 4e 44 #define FILEHAND
1138d 4c 45 49 44 28 66 64 29 20 28 28 69 6e 74 29 66 LEID(fd) ((int)f
1138e 64 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 61 d)../*.** The pa
1138f 67 65 20 63 61 63 68 65 20 61 73 20 61 20 77 68 ge cache as a wh
11390 6f 6c 65 20 69 73 20 61 6c 77 61 79 73 20 69 6e ole is always in
11391 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c one of the foll
11392 6f 77 69 6e 67 0a 2a 2a 20 73 74 61 74 65 73 3a owing.** states:
11393 0a 2a 2a 0a 2a 2a 20 20 20 50 41 47 45 52 5f 55 .**.** PAGER_U
11394 4e 4c 4f 43 4b 20 20 20 20 20 20 20 20 54 68 65 NLOCK The
11395 20 70 61 67 65 20 63 61 63 68 65 20 69 73 20 6e page cache is n
11396 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 72 65 61 ot currently rea
11397 64 69 6e 67 20 6f 72 20 0a 2a 2a 20 20 20 20 20 ding or .**
11398 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11399 20 20 77 72 69 74 69 6e 67 20 74 68 65 20 64 61 writing the da
1139a 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 54 68 tabase file. Th
1139b 65 72 65 20 69 73 20 6e 6f 0a 2a 2a 20 20 20 20 ere is no.**
1139c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1139d 20 20 20 64 61 74 61 20 68 65 6c 64 20 69 6e 20 data held in
1139e 6d 65 6d 6f 72 79 2e 20 20 54 68 69 73 20 69 73 memory. This is
1139f 20 74 68 65 20 69 6e 69 74 69 61 6c 0a 2a 2a 20 the initial.**
113a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
113a1 20 20 20 20 20 20 73 74 61 74 65 2e 0a 2a 2a 0a state..**.
113a2 2a 2a 20 20 20 50 41 47 45 52 5f 53 48 41 52 45 ** PAGER_SHARE
113a3 44 20 20 20 20 20 20 20 20 54 68 65 20 70 61 67 D The pag
113a4 65 20 63 61 63 68 65 20 69 73 20 72 65 61 64 69 e cache is readi
113a5 6e 67 20 74 68 65 20 64 61 74 61 62 61 73 65 2e ng the database.
113a6 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
113a7 20 20 20 20 20 20 20 20 20 20 57 72 69 74 69 6e Writin
113a8 67 20 69 73 20 6e 6f 74 20 70 65 72 6d 69 74 74 g is not permitt
113a9 65 64 2e 20 20 54 68 65 72 65 20 63 61 6e 20 62 ed. There can b
113aa 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 e.**
113ab 20 20 20 20 20 20 20 20 20 20 20 6d 75 6c 74 69 multi
113ac 70 6c 65 20 72 65 61 64 65 72 73 20 61 63 63 65 ple readers acce
113ad 73 73 69 6e 67 20 74 68 65 20 73 61 6d 65 20 64 ssing the same d
113ae 61 74 61 62 61 73 65 0a 2a 2a 20 20 20 20 20 20 atabase.**
113af 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
113b0 20 66 69 6c 65 20 61 74 20 74 68 65 20 73 61 6d file at the sam
113b1 65 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 e time..**.**
113b2 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 20 PAGER_RESERVED
113b3 20 20 20 20 54 68 69 73 20 70 72 6f 63 65 73 73 This process
113b4 20 68 61 73 20 72 65 73 65 72 76 65 64 20 74 68 has reserved th
113b5 65 20 64 61 74 61 62 61 73 65 20 66 6f 72 20 77 e database for w
113b6 72 69 74 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 riting.**
113b7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
113b8 62 75 74 20 68 61 73 20 6e 6f 74 20 79 65 74 20 but has not yet
113b9 6d 61 64 65 20 61 6e 79 20 63 68 61 6e 67 65 73 made any changes
113ba 2e 20 20 4f 6e 6c 79 20 6f 6e 65 20 70 72 6f 63 . Only one proc
113bb 65 73 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ess.**
113bc 20 20 20 20 20 20 20 20 20 20 20 20 20 61 74 20 at
113bd 61 20 74 69 6d 65 20 63 61 6e 20 72 65 73 65 72 a time can reser
113be 76 65 20 74 68 65 20 64 61 74 61 62 61 73 65 2e ve the database.
113bf 20 20 54 68 65 20 6f 72 69 67 69 6e 61 6c 0a 2a The original.*
113c0 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
113c1 20 20 20 20 20 20 20 20 64 61 74 61 62 61 73 65 database
113c2 20 66 69 6c 65 20 68 61 73 20 6e 6f 74 20 62 65 file has not be
113c3 65 6e 20 6d 6f 64 69 66 69 65 64 20 73 6f 20 6f en modified so o
113c4 74 68 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 ther.**
113c5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 72 pr
113c6 6f 63 65 73 73 65 73 20 6d 61 79 20 73 74 69 6c ocesses may stil
113c7 6c 20 62 65 20 72 65 61 64 69 6e 67 20 74 68 65 l be reading the
113c8 20 6f 6e 2d 64 69 73 6b 0a 2a 2a 20 20 20 20 20 on-disk.**
113c9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
113ca 20 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e database file.
113cb 0a 2a 2a 0a 2a 2a 20 20 20 50 41 47 45 52 5f 45 .**.** PAGER_E
113cc 58 43 4c 55 53 49 56 45 20 20 20 20 20 54 68 65 XCLUSIVE The
113cd 20 70 61 67 65 20 63 61 63 68 65 20 69 73 20 77 page cache is w
113ce 72 69 74 69 6e 67 20 74 68 65 20 64 61 74 61 62 riting the datab
113cf 61 73 65 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20 ase..**
113d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 41 63 Ac
113d1 63 65 73 73 20 69 73 20 65 78 63 6c 75 73 69 76 cess is exclusiv
113d2 65 2e 20 20 4e 6f 20 6f 74 68 65 72 20 70 72 6f e. No other pro
113d3 63 65 73 73 65 73 20 6f 72 0a 2a 2a 20 20 20 20 cesses or.**
113d4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
113d5 20 20 20 74 68 72 65 61 64 73 20 63 61 6e 20 62 threads can b
113d6 65 20 72 65 61 64 69 6e 67 20 6f 72 20 77 72 69 e reading or wri
113d7 74 69 6e 67 20 77 68 69 6c 65 20 6f 6e 65 0a 2a ting while one.*
113d8 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
113d9 20 20 20 20 20 20 20 20 70 72 6f 63 65 73 73 20 process
113da 69 73 20 77 72 69 74 69 6e 67 2e 0a 2a 2a 0a 2a is writing..**.*
113db 2a 20 20 20 50 41 47 45 52 5f 53 59 4e 43 45 44 * PAGER_SYNCED
113dc 20 20 20 20 20 20 20 20 54 68 65 20 70 61 67 65 The page
113dd 72 20 6d 6f 76 65 73 20 74 6f 20 74 68 69 73 20 r moves to this
113de 73 74 61 74 65 20 66 72 6f 6d 20 50 41 47 45 52 state from PAGER
113df 5f 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 _EXCLUSIVE.**
113e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
113e1 20 20 20 20 61 66 74 65 72 20 61 6c 6c 20 64 69 after all di
113e2 72 74 79 20 70 61 67 65 73 20 68 61 76 65 20 62 rty pages have b
113e3 65 65 6e 20 77 72 69 74 74 65 6e 20 74 6f 20 74 een written to t
113e4 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 he.**
113e5 20 20 20 20 20 20 20 20 20 20 20 20 64 61 74 61 data
113e6 62 61 73 65 20 66 69 6c 65 20 61 6e 64 20 74 68 base file and th
113e7 65 20 66 69 6c 65 20 68 61 73 20 62 65 65 6e 20 e file has been
113e8 73 79 6e 63 65 64 20 74 6f 0a 2a 2a 20 20 20 20 synced to.**
113e9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
113ea 20 20 20 64 69 73 6b 2e 20 41 6c 6c 20 74 68 61 disk. All tha
113eb 74 20 72 65 6d 61 69 6e 73 20 74 6f 20 64 6f 20 t remains to do
113ec 69 73 20 74 6f 20 72 65 6d 6f 76 65 20 6f 72 0a is to remove or.
113ed 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
113ee 20 20 20 20 20 20 20 20 20 74 72 75 6e 63 61 74 truncat
113ef 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 e the journal fi
113f0 6c 65 20 61 6e 64 20 74 68 65 20 74 72 61 6e 73 le and the trans
113f1 61 63 74 69 6f 6e 20 0a 2a 2a 20 20 20 20 20 20 action .**
113f2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
113f3 20 77 69 6c 6c 20 62 65 20 63 6f 6d 6d 69 74 74 will be committ
113f4 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 ed..**.** The pa
113f5 67 65 20 63 61 63 68 65 20 63 6f 6d 65 73 20 75 ge cache comes u
113f6 70 20 69 6e 20 50 41 47 45 52 5f 55 4e 4c 4f 43 p in PAGER_UNLOC
113f7 4b 2e 20 20 54 68 65 20 66 69 72 73 74 20 74 69 K. The first ti
113f8 6d 65 20 61 0a 2a 2a 20 73 71 6c 69 74 65 33 50 me a.** sqlite3P
113f9 61 67 65 72 47 65 74 28 29 20 6f 63 63 75 72 73 agerGet() occurs
113fa 2c 20 74 68 65 20 73 74 61 74 65 20 74 72 61 6e , the state tran
113fb 73 69 74 69 6f 6e 73 20 74 6f 20 50 41 47 45 52 sitions to PAGER
113fc 5f 53 48 41 52 45 44 2e 0a 2a 2a 20 41 66 74 65 _SHARED..** Afte
113fd 72 20 61 6c 6c 20 70 61 67 65 73 20 68 61 76 65 r all pages have
113fe 20 62 65 65 6e 20 72 65 6c 65 61 73 65 64 20 75 been released u
113ff 73 69 6e 67 20 73 71 6c 69 74 65 5f 70 61 67 65 sing sqlite_page
11400 5f 75 6e 72 65 66 28 29 2c 0a 2a 2a 20 74 68 65 _unref(),.** the
11401 20 73 74 61 74 65 20 74 72 61 6e 73 69 74 69 6f state transitio
11402 6e 73 20 62 61 63 6b 20 74 6f 20 50 41 47 45 52 ns back to PAGER
11403 5f 55 4e 4c 4f 43 4b 2e 20 20 54 68 65 20 66 69 _UNLOCK. The fi
11404 72 73 74 20 74 69 6d 65 0a 2a 2a 20 74 68 61 74 rst time.** that
11405 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 sqlite3PagerWri
11406 74 65 28 29 20 69 73 20 63 61 6c 6c 65 64 2c 20 te() is called,
11407 74 68 65 20 73 74 61 74 65 20 74 72 61 6e 73 69 the state transi
11408 74 69 6f 6e 73 20 74 6f 0a 2a 2a 20 50 41 47 45 tions to.** PAGE
11409 52 5f 52 45 53 45 52 56 45 44 2e 20 20 28 4e 6f R_RESERVED. (No
1140a 74 65 20 74 68 61 74 20 73 71 6c 69 74 65 33 50 te that sqlite3P
1140b 61 67 65 72 57 72 69 74 65 28 29 20 63 61 6e 20 agerWrite() can
1140c 6f 6e 6c 79 20 62 65 0a 2a 2a 20 63 61 6c 6c 65 only be.** calle
1140d 64 20 6f 6e 20 61 6e 20 6f 75 74 73 74 61 6e 64 d on an outstand
1140e 69 6e 67 20 70 61 67 65 20 77 68 69 63 68 20 6d ing page which m
1140f 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 70 61 eans that the pa
11410 67 65 72 20 6d 75 73 74 0a 2a 2a 20 62 65 20 69 ger must.** be i
11411 6e 20 50 41 47 45 52 5f 53 48 41 52 45 44 20 62 n PAGER_SHARED b
11412 65 66 6f 72 65 20 69 74 20 74 72 61 6e 73 69 74 efore it transit
11413 69 6f 6e 73 20 74 6f 20 50 41 47 45 52 5f 52 45 ions to PAGER_RE
11414 53 45 52 56 45 44 2e 29 0a 2a 2a 20 50 41 47 45 SERVED.).** PAGE
11415 52 5f 52 45 53 45 52 56 45 44 20 6d 65 61 6e 73 R_RESERVED means
11416 20 74 68 61 74 20 74 68 65 72 65 20 69 73 20 61 that there is a
11417 6e 20 6f 70 65 6e 20 72 6f 6c 6c 62 61 63 6b 20 n open rollback
11418 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 20 54 68 65 20 journal..** The
11419 74 72 61 6e 73 69 74 69 6f 6e 20 74 6f 20 50 41 transition to PA
1141a 47 45 52 5f 45 58 43 4c 55 53 49 56 45 20 6f 63 GER_EXCLUSIVE oc
1141b 63 75 72 73 20 62 65 66 6f 72 65 20 61 6e 79 20 curs before any
1141c 63 68 61 6e 67 65 73 0a 2a 2a 20 61 72 65 20 6d changes.** are m
1141d 61 64 65 20 74 6f 20 74 68 65 20 64 61 74 61 62 ade to the datab
1141e 61 73 65 20 66 69 6c 65 2c 20 74 68 6f 75 67 68 ase file, though
1141f 20 77 72 69 74 65 73 20 74 6f 20 74 68 65 20 72 writes to the r
11420 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 6a 6f 75 72 6e ollback.** journ
11421 61 6c 20 6f 63 63 75 72 73 20 77 69 74 68 20 6a al occurs with j
11422 75 73 74 20 50 41 47 45 52 5f 52 45 53 45 52 56 ust PAGER_RESERV
11423 45 44 2e 20 20 41 66 74 65 72 20 61 6e 20 73 71 ED. After an sq
11424 6c 69 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 lite3PagerRollba
11425 63 6b 28 29 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 ck().** or sqlit
11426 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 e3PagerCommitPha
11427 73 65 54 77 6f 28 29 2c 20 74 68 65 20 73 74 61 seTwo(), the sta
11428 74 65 20 63 61 6e 20 67 6f 20 62 61 63 6b 20 74 te can go back t
11429 6f 20 50 41 47 45 52 5f 53 48 41 52 45 44 2c 0a o PAGER_SHARED,.
1142a 2a 2a 20 6f 72 20 69 74 20 63 61 6e 20 73 74 61 ** or it can sta
1142b 79 20 61 74 20 50 41 47 45 52 5f 45 58 43 4c 55 y at PAGER_EXCLU
1142c 53 49 56 45 20 69 66 20 77 65 20 61 72 65 20 69 SIVE if we are i
1142d 6e 20 65 78 63 6c 75 73 69 76 65 20 61 63 63 65 n exclusive acce
1142e 73 73 20 6d 6f 64 65 2e 0a 2a 2f 0a 23 64 65 66 ss mode..*/.#def
1142f 69 6e 65 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b ine PAGER_UNLOCK
11430 20 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 0.#define
11431 50 41 47 45 52 5f 53 48 41 52 45 44 20 20 20 20 PAGER_SHARED
11432 20 20 31 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 1 /* same as
11433 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20 2a 2f 0a SHARED_LOCK */.
11434 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 52 45 #define PAGER_RE
11435 53 45 52 56 45 44 20 20 20 20 32 20 20 20 2f 2a SERVED 2 /*
11436 20 73 61 6d 65 20 61 73 20 52 45 53 45 52 56 45 same as RESERVE
11437 44 5f 4c 4f 43 4b 20 2a 2f 0a 23 64 65 66 69 6e D_LOCK */.#defin
11438 65 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 e PAGER_EXCLUSIV
11439 45 20 20 20 34 20 20 20 2f 2a 20 73 61 6d 65 20 E 4 /* same
1143a 61 73 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 as EXCLUSIVE_LOC
1143b 4b 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 K */.#define PAG
1143c 45 52 5f 53 59 4e 43 45 44 20 20 20 20 20 20 35 ER_SYNCED 5
1143d 0a 0a 2f 2a 0a 2a 2a 20 41 20 6d 61 63 72 6f 20 ../*.** A macro
1143e 75 73 65 64 20 66 6f 72 20 69 6e 76 6f 6b 69 6e used for invokin
1143f 67 20 74 68 65 20 63 6f 64 65 63 20 69 66 20 74 g the codec if t
11440 68 65 72 65 20 69 73 20 6f 6e 65 0a 2a 2f 0a 23 here is one.*/.#
11441 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48 41 53 ifdef SQLITE_HAS
11442 5f 43 4f 44 45 43 0a 23 20 64 65 66 69 6e 65 20 _CODEC.# define
11443 43 4f 44 45 43 31 28 50 2c 44 2c 4e 2c 58 2c 45 CODEC1(P,D,N,X,E
11444 29 20 5c 0a 20 20 20 20 69 66 28 20 50 2d 3e 78 ) \. if( P->x
11445 43 6f 64 65 63 20 26 26 20 50 2d 3e 78 43 6f 64 Codec && P->xCod
11446 65 63 28 50 2d 3e 70 43 6f 64 65 63 2c 44 2c 4e ec(P->pCodec,D,N
11447 2c 58 29 3d 3d 30 20 29 7b 20 45 3b 20 7d 0a 23 ,X)==0 ){ E; }.#
11448 20 64 65 66 69 6e 65 20 43 4f 44 45 43 32 28 50 define CODEC2(P
11449 2c 44 2c 4e 2c 58 2c 45 2c 4f 29 20 5c 0a 20 20 ,D,N,X,E,O) \.
1144a 20 20 69 66 28 20 50 2d 3e 78 43 6f 64 65 63 3d if( P->xCodec=
1144b 3d 30 20 29 7b 20 4f 3d 28 63 68 61 72 2a 29 44 =0 ){ O=(char*)D
1144c 3b 20 7d 65 6c 73 65 20 5c 0a 20 20 20 20 69 66 ; }else \. if
1144d 28 20 28 4f 3d 28 63 68 61 72 2a 29 28 50 2d 3e ( (O=(char*)(P->
1144e 78 43 6f 64 65 63 28 50 2d 3e 70 43 6f 64 65 63 xCodec(P->pCodec
1144f 2c 44 2c 4e 2c 58 29 29 29 3d 3d 30 20 29 7b 20 ,D,N,X)))==0 ){
11450 45 3b 20 7d 0a 23 65 6c 73 65 0a 23 20 64 65 66 E; }.#else.# def
11451 69 6e 65 20 43 4f 44 45 43 31 28 50 2c 44 2c 4e ine CODEC1(P,D,N
11452 2c 58 2c 45 29 20 20 20 2f 2a 20 4e 4f 2d 4f 50 ,X,E) /* NO-OP
11453 20 2a 2f 0a 23 20 64 65 66 69 6e 65 20 43 4f 44 */.# define COD
11454 45 43 32 28 50 2c 44 2c 4e 2c 58 2c 45 2c 4f 29 EC2(P,D,N,X,E,O)
11455 20 4f 3d 28 63 68 61 72 2a 29 44 0a 23 65 6e 64 O=(char*)D.#end
11456 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 if../*.** The ma
11457 78 69 6d 75 6d 20 61 6c 6c 6f 77 65 64 20 73 65 ximum allowed se
11458 63 74 6f 72 20 73 69 7a 65 2e 20 36 34 4b 69 42 ctor size. 64KiB
11459 2e 20 49 66 20 74 68 65 20 78 53 65 63 74 6f 72 . If the xSector
1145a 73 69 7a 65 28 29 20 6d 65 74 68 6f 64 20 0a 2a size() method .*
1145b 2a 20 72 65 74 75 72 6e 73 20 61 20 76 61 6c 75 * returns a valu
1145c 65 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 e larger than th
1145d 69 73 2c 20 74 68 65 6e 20 4d 41 58 5f 53 45 43 is, then MAX_SEC
1145e 54 4f 52 5f 53 49 5a 45 20 69 73 20 75 73 65 64 TOR_SIZE is used
1145f 20 69 6e 73 74 65 61 64 2e 0a 2a 2a 20 54 68 69 instead..** Thi
11460 73 20 63 6f 75 6c 64 20 63 6f 6e 63 65 69 76 61 s could conceiva
11461 62 6c 79 20 63 61 75 73 65 20 63 6f 72 72 75 70 bly cause corrup
11462 74 69 6f 6e 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 tion following a
11463 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 20 6f power failure o
11464 6e 0a 2a 2a 20 73 75 63 68 20 61 20 73 79 73 74 n.** such a syst
11465 65 6d 2e 20 54 68 69 73 20 69 73 20 63 75 72 72 em. This is curr
11466 65 6e 74 6c 79 20 61 6e 20 75 6e 64 6f 63 75 6d ently an undocum
11467 65 6e 74 65 64 20 6c 69 6d 69 74 2e 0a 2a 2f 0a ented limit..*/.
11468 23 64 65 66 69 6e 65 20 4d 41 58 5f 53 45 43 54 #define MAX_SECT
11469 4f 52 5f 53 49 5a 45 20 30 78 31 30 30 30 30 0a OR_SIZE 0x10000.
1146a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e ./*.** An instan
1146b 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 ce of the follow
1146c 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 69 73 ing structure is
1146d 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 65 allocated for e
1146e 61 63 68 20 61 63 74 69 76 65 0a 2a 2a 20 73 61 ach active.** sa
1146f 76 65 70 6f 69 6e 74 20 61 6e 64 20 73 74 61 74 vepoint and stat
11470 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f ement transactio
11471 6e 20 69 6e 20 74 68 65 20 73 79 73 74 65 6d 2e n in the system.
11472 20 41 6c 6c 20 73 75 63 68 20 73 74 72 75 63 74 All such struct
11473 75 72 65 73 0a 2a 2a 20 61 72 65 20 73 74 6f 72 ures.** are stor
11474 65 64 20 69 6e 20 74 68 65 20 50 61 67 65 72 2e ed in the Pager.
11475 61 53 61 76 65 70 6f 69 6e 74 5b 5d 20 61 72 72 aSavepoint[] arr
11476 61 79 2c 20 77 68 69 63 68 20 69 73 20 61 6c 6c ay, which is all
11477 6f 63 61 74 65 64 20 61 6e 64 0a 2a 2a 20 72 65 ocated and.** re
11478 73 69 7a 65 64 20 75 73 69 6e 67 20 73 71 6c 69 sized using sqli
11479 74 65 33 52 65 61 6c 6c 6f 63 28 29 2e 0a 2a 2a te3Realloc()..**
1147a 0a 2a 2a 20 57 68 65 6e 20 61 20 73 61 76 65 70 .** When a savep
1147b 6f 69 6e 74 20 69 73 20 63 72 65 61 74 65 64 2c oint is created,
1147c 20 74 68 65 20 50 61 67 65 72 53 61 76 65 70 6f the PagerSavepo
1147d 69 6e 74 2e 69 48 64 72 4f 66 66 73 65 74 20 66 int.iHdrOffset f
1147e 69 65 6c 64 20 69 73 0a 2a 2a 20 73 65 74 20 74 ield is.** set t
1147f 6f 20 30 2e 20 49 66 20 61 20 6a 6f 75 72 6e 61 o 0. If a journa
11480 6c 2d 68 65 61 64 65 72 20 69 73 20 77 72 69 74 l-header is writ
11481 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 6d 61 69 ten into the mai
11482 6e 20 6a 6f 75 72 6e 61 6c 20 77 68 69 6c 65 0a n journal while.
11483 2a 2a 20 74 68 65 20 73 61 76 65 70 6f 69 6e 74 ** the savepoint
11484 20 69 73 20 61 63 74 69 76 65 2c 20 74 68 65 6e is active, then
11485 20 69 48 64 72 4f 66 66 73 65 74 20 69 73 20 73 iHdrOffset is s
11486 65 74 20 74 6f 20 74 68 65 20 62 79 74 65 20 6f et to the byte o
11487 66 66 73 65 74 20 0a 2a 2a 20 69 6d 6d 65 64 69 ffset .** immedi
11488 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 69 6e 67 20 ately following
11489 74 68 65 20 6c 61 73 74 20 6a 6f 75 72 6e 61 6c the last journal
1148a 20 72 65 63 6f 72 64 20 77 72 69 74 74 65 6e 20 record written
1148b 69 6e 74 6f 20 74 68 65 20 6d 61 69 6e 0a 2a 2a into the main.**
1148c 20 6a 6f 75 72 6e 61 6c 20 62 65 66 6f 72 65 20 journal before
1148d 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 68 65 61 64 the journal-head
1148e 65 72 2e 20 54 68 69 73 20 69 73 20 72 65 71 75 er. This is requ
1148f 69 72 65 64 20 64 75 72 69 6e 67 20 73 61 76 65 ired during save
11490 70 6f 69 6e 74 0a 2a 2a 20 72 6f 6c 6c 62 61 63 point.** rollbac
11491 6b 20 28 73 65 65 20 70 61 67 65 72 50 6c 61 79 k (see pagerPlay
11492 62 61 63 6b 53 61 76 65 70 6f 69 6e 74 28 29 29 backSavepoint())
11493 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 ..*/.typedef str
11494 75 63 74 20 50 61 67 65 72 53 61 76 65 70 6f 69 uct PagerSavepoi
11495 6e 74 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e nt PagerSavepoin
11496 74 3b 0a 73 74 72 75 63 74 20 50 61 67 65 72 53 t;.struct PagerS
11497 61 76 65 70 6f 69 6e 74 20 7b 0a 20 20 69 36 34 avepoint {. i64
11498 20 69 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 iOffset;
11499 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61 /* Sta
1149a 72 74 69 6e 67 20 6f 66 66 73 65 74 20 69 6e 20 rting offset in
1149b 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a main journal */.
1149c 20 20 69 36 34 20 69 48 64 72 4f 66 66 73 65 74 i64 iHdrOffset
1149d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
1149e 2a 20 53 65 65 20 61 62 6f 76 65 20 2a 2f 0a 20 * See above */.
1149f 20 42 69 74 76 65 63 20 2a 70 49 6e 53 61 76 65 Bitvec *pInSave
114a0 70 6f 69 6e 74 3b 20 20 20 20 20 20 20 20 2f 2a point; /*
114a1 20 53 65 74 20 6f 66 20 70 61 67 65 73 20 69 6e Set of pages in
114a2 20 74 68 69 73 20 73 61 76 65 70 6f 69 6e 74 20 this savepoint
114a3 2a 2f 0a 20 20 50 67 6e 6f 20 6e 4f 72 69 67 3b */. Pgno nOrig;
114a4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
114a5 20 20 2f 2a 20 4f 72 69 67 69 6e 61 6c 20 6e 75 /* Original nu
114a6 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e mber of pages in
114a7 20 66 69 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 file */. Pgno
114a8 69 53 75 62 52 65 63 3b 20 20 20 20 20 20 20 20 iSubRec;
114a9 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 /* Index
114aa 20 6f 66 20 66 69 72 73 74 20 72 65 63 6f 72 64 of first record
114ab 20 69 6e 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 in sub-journal
114ac 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 6f */.};../*.** A o
114ad 70 65 6e 20 70 61 67 65 20 63 61 63 68 65 20 69 pen page cache i
114ae 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 s an instance of
114af 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 the following s
114b0 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 tructure..**.**
114b1 65 72 72 43 6f 64 65 0a 2a 2a 0a 2a 2a 20 20 20 errCode.**.**
114b2 50 61 67 65 72 2e 65 72 72 43 6f 64 65 20 6d 61 Pager.errCode ma
114b3 79 20 62 65 20 73 65 74 20 74 6f 20 53 51 4c 49 y be set to SQLI
114b4 54 45 5f 49 4f 45 52 52 2c 20 53 51 4c 49 54 45 TE_IOERR, SQLITE
114b5 5f 43 4f 52 52 55 50 54 2c 20 6f 72 0a 2a 2a 20 _CORRUPT, or.**
114b6 20 20 6f 72 20 53 51 4c 49 54 45 5f 46 55 4c 4c or SQLITE_FULL
114b7 2e 20 4f 6e 63 65 20 6f 6e 65 20 6f 66 20 74 68 . Once one of th
114b8 65 20 66 69 72 73 74 20 74 68 72 65 65 20 65 72 e first three er
114b9 72 6f 72 73 20 6f 63 63 75 72 73 2c 20 69 74 20 rors occurs, it
114ba 70 65 72 73 69 73 74 73 0a 2a 2a 20 20 20 61 6e persists.** an
114bb 64 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 73 d is returned as
114bc 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 65 the result of e
114bd 76 65 72 79 20 6d 61 6a 6f 72 20 70 61 67 65 72 very major pager
114be 20 41 50 49 20 63 61 6c 6c 2e 20 20 54 68 65 0a API call. The.
114bf 2a 2a 20 20 20 53 51 4c 49 54 45 5f 46 55 4c 4c ** SQLITE_FULL
114c0 20 72 65 74 75 72 6e 20 63 6f 64 65 20 69 73 20 return code is
114c1 73 6c 69 67 68 74 6c 79 20 64 69 66 66 65 72 65 slightly differe
114c2 6e 74 2e 20 49 74 20 70 65 72 73 69 73 74 73 20 nt. It persists
114c3 6f 6e 6c 79 20 75 6e 74 69 6c 20 74 68 65 0a 2a only until the.*
114c4 2a 20 20 20 6e 65 78 74 20 73 75 63 63 65 73 73 * next success
114c5 66 75 6c 20 72 6f 6c 6c 62 61 63 6b 20 69 73 20 ful rollback is
114c6 70 65 72 66 6f 72 6d 65 64 20 6f 6e 20 74 68 65 performed on the
114c7 20 70 61 67 65 72 20 63 61 63 68 65 2e 20 41 6c pager cache. Al
114c8 73 6f 2c 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f so,.** SQLITE_
114c9 46 55 4c 4c 20 64 6f 65 73 20 6e 6f 74 20 61 66 FULL does not af
114ca 66 65 63 74 20 74 68 65 20 73 71 6c 69 74 65 33 fect the sqlite3
114cb 50 61 67 65 72 47 65 74 28 29 20 61 6e 64 20 73 PagerGet() and s
114cc 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 6f 6b 75 qlite3PagerLooku
114cd 70 28 29 0a 2a 2a 20 20 20 41 50 49 73 2c 20 74 p().** APIs, t
114ce 68 65 79 20 6d 61 79 20 73 74 69 6c 6c 20 62 65 hey may still be
114cf 20 75 73 65 64 20 73 75 63 63 65 73 73 66 75 6c used successful
114d0 6c 79 2e 0a 2a 2a 0a 2a 2a 20 64 62 53 69 7a 65 ly..**.** dbSize
114d1 56 61 6c 69 64 2c 20 64 62 53 69 7a 65 2c 20 64 Valid, dbSize, d
114d2 62 4f 72 69 67 53 69 7a 65 2c 20 64 62 46 69 6c bOrigSize, dbFil
114d3 65 53 69 7a 65 0a 2a 2a 0a 2a 2a 20 20 20 4d 61 eSize.**.** Ma
114d4 6e 61 67 69 6e 67 20 74 68 65 20 73 69 7a 65 20 naging the size
114d5 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
114d6 66 69 6c 65 20 69 6e 20 70 61 67 65 73 20 69 73 file in pages is
114d7 20 61 20 6c 69 74 74 6c 65 20 63 6f 6d 70 6c 69 a little compli
114d8 63 61 74 65 64 2e 0a 2a 2a 20 20 20 54 68 65 20 cated..** The
114d9 76 61 72 69 61 62 6c 65 20 50 61 67 65 72 2e 64 variable Pager.d
114da 62 53 69 7a 65 20 63 6f 6e 74 61 69 6e 73 20 74 bSize contains t
114db 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 he number of pag
114dc 65 73 20 74 68 61 74 20 74 68 65 20 64 61 74 61 es that the data
114dd 62 61 73 65 0a 2a 2a 20 20 20 69 6d 61 67 65 20 base.** image
114de 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e 74 61 69 currently contai
114df 6e 73 2e 20 41 73 20 74 68 65 20 64 61 74 61 62 ns. As the datab
114e0 61 73 65 20 69 6d 61 67 65 20 67 72 6f 77 73 20 ase image grows
114e1 6f 72 20 73 68 72 69 6e 6b 73 20 74 68 69 73 0a or shrinks this.
114e2 2a 2a 20 20 20 76 61 72 69 61 62 6c 65 20 69 73 ** variable is
114e3 20 75 70 64 61 74 65 64 2e 20 54 68 65 20 76 61 updated. The va
114e4 72 69 61 62 6c 65 20 50 61 67 65 72 2e 64 62 46 riable Pager.dbF
114e5 69 6c 65 53 69 7a 65 20 63 6f 6e 74 61 69 6e 73 ileSize contains
114e6 20 74 68 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 20 the number.**
114e7 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 of pages in the
114e8 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 database file.
114e9 54 68 69 73 20 6d 61 79 20 62 65 20 64 69 66 66 This may be diff
114ea 65 72 65 6e 74 20 66 72 6f 6d 20 50 61 67 65 72 erent from Pager
114eb 2e 64 62 53 69 7a 65 0a 2a 2a 20 20 20 69 66 20 .dbSize.** if
114ec 73 6f 6d 65 20 70 61 67 65 73 20 68 61 76 65 20 some pages have
114ed 62 65 65 6e 20 61 70 70 65 6e 64 65 64 20 74 6f been appended to
114ee 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 6d the database im
114ef 61 67 65 20 62 75 74 20 6e 6f 74 20 79 65 74 20 age but not yet
114f0 77 72 69 74 74 65 6e 0a 2a 2a 20 20 20 6f 75 74 written.** out
114f1 20 66 72 6f 6d 20 74 68 65 20 63 61 63 68 65 20 from the cache
114f2 74 6f 20 74 68 65 20 61 63 74 75 61 6c 20 66 69 to the actual fi
114f3 6c 65 20 6f 6e 20 64 69 73 6b 2e 20 4f 72 20 69 le on disk. Or i
114f4 66 20 74 68 65 20 69 6d 61 67 65 20 68 61 73 20 f the image has
114f5 62 65 65 6e 0a 2a 2a 20 20 20 74 72 75 6e 63 61 been.** trunca
114f6 74 65 64 20 62 79 20 61 6e 20 69 6e 63 72 65 6d ted by an increm
114f7 65 6e 74 61 6c 2d 76 61 63 75 75 6d 20 6f 70 65 ental-vacuum ope
114f8 72 61 74 69 6f 6e 2e 20 54 68 65 20 50 61 67 65 ration. The Page
114f9 72 2e 64 62 4f 72 69 67 53 69 7a 65 20 76 61 72 r.dbOrigSize var
114fa 69 61 62 6c 65 0a 2a 2a 20 20 20 63 6f 6e 74 61 iable.** conta
114fb 69 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f ins the number o
114fc 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 64 f pages in the d
114fd 61 74 61 62 61 73 65 20 69 6d 61 67 65 20 77 68 atabase image wh
114fe 65 6e 20 74 68 65 20 63 75 72 72 65 6e 74 0a 2a en the current.*
114ff 2a 20 20 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 * transaction
11500 77 61 73 20 6f 70 65 6e 65 64 2e 20 54 68 65 20 was opened. The
11501 63 6f 6e 74 65 6e 74 73 20 6f 66 20 61 6c 6c 20 contents of all
11502 74 68 72 65 65 20 6f 66 20 74 68 65 73 65 20 76 three of these v
11503 61 72 69 61 62 6c 65 73 20 69 73 0a 2a 2a 20 20 ariables is.**
11504 20 6f 6e 6c 79 20 67 75 61 72 61 6e 74 65 65 64 only guaranteed
11505 20 74 6f 20 62 65 20 63 6f 72 72 65 63 74 20 69 to be correct i
11506 66 20 74 68 65 20 62 6f 6f 6c 65 61 6e 20 50 61 f the boolean Pa
11507 67 65 72 2e 64 62 53 69 7a 65 56 61 6c 69 64 20 ger.dbSizeValid
11508 69 73 20 74 72 75 65 2e 0a 2a 2a 0a 2a 2a 20 20 is true..**.**
11509 20 54 4f 44 4f 3a 20 55 6e 64 65 72 20 77 68 61 TODO: Under wha
1150a 74 20 63 6f 6e 64 69 74 69 6f 6e 73 20 69 73 20 t conditions is
1150b 64 62 53 69 7a 65 56 61 6c 69 64 20 73 65 74 3f dbSizeValid set?
1150c 20 43 6c 65 61 72 65 64 3f 0a 2a 2a 0a 2a 2a 20 Cleared?.**.**
1150d 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 0a changeCountDone.
1150e 2a 2a 0a 2a 2a 20 20 20 54 68 69 73 20 62 6f 6f **.** This boo
1150f 6c 65 61 6e 20 76 61 72 69 61 62 6c 65 20 69 73 lean variable is
11510 20 75 73 65 64 20 74 6f 20 6d 61 6b 65 20 73 75 used to make su
11511 72 65 20 74 68 61 74 20 74 68 65 20 63 68 61 6e re that the chan
11512 67 65 2d 63 6f 75 6e 74 65 72 20 0a 2a 2a 20 20 ge-counter .**
11513 20 28 74 68 65 20 34 2d 62 79 74 65 20 68 65 61 (the 4-byte hea
11514 64 65 72 20 66 69 65 6c 64 20 61 74 20 62 79 74 der field at byt
11515 65 20 6f 66 66 73 65 74 20 32 34 20 6f 66 20 74 e offset 24 of t
11516 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
11517 29 20 69 73 20 0a 2a 2a 20 20 20 6e 6f 74 20 75 ) is .** not u
11518 70 64 61 74 65 64 20 6d 6f 72 65 20 6f 66 74 65 pdated more ofte
11519 6e 20 74 68 61 6e 20 6e 65 63 65 73 73 61 72 79 n than necessary
1151a 2e 20 0a 2a 2a 0a 2a 2a 20 20 20 49 74 20 69 73 . .**.** It is
1151b 20 73 65 74 20 74 6f 20 74 72 75 65 20 77 68 65 set to true whe
1151c 6e 20 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 n the change-cou
1151d 6e 74 65 72 20 66 69 65 6c 64 20 69 73 20 75 70 nter field is up
1151e 64 61 74 65 64 2c 20 77 68 69 63 68 20 0a 2a 2a dated, which .**
1151f 20 20 20 63 61 6e 20 6f 6e 6c 79 20 68 61 70 70 can only happ
11520 65 6e 20 69 66 20 61 6e 20 65 78 63 6c 75 73 69 en if an exclusi
11521 76 65 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 20 ve lock is held
11522 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 on the database
11523 66 69 6c 65 2e 0a 2a 2a 20 20 20 49 74 20 69 73 file..** It is
11524 20 63 6c 65 61 72 65 64 20 28 73 65 74 20 74 6f cleared (set to
11525 20 66 61 6c 73 65 29 20 77 68 65 6e 65 76 65 72 false) whenever
11526 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f an exclusive lo
11527 63 6b 20 69 73 20 0a 2a 2a 20 20 20 72 65 6c 69 ck is .** reli
11528 6e 71 75 69 73 68 65 64 20 6f 6e 20 74 68 65 20 nquished on the
11529 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 45 database file. E
1152a 61 63 68 20 74 69 6d 65 20 61 20 74 72 61 6e 73 ach time a trans
1152b 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 action is commit
1152c 74 65 64 2c 0a 2a 2a 20 20 20 54 68 65 20 63 68 ted,.** The ch
1152d 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 66 6c angeCountDone fl
1152e 61 67 20 69 73 20 69 6e 73 70 65 63 74 65 64 2e ag is inspected.
1152f 20 49 66 20 69 74 20 69 73 20 74 72 75 65 2c 20 If it is true,
11530 74 68 65 20 77 6f 72 6b 20 6f 66 0a 2a 2a 20 20 the work of.**
11531 20 75 70 64 61 74 69 6e 67 20 74 68 65 20 63 68 updating the ch
11532 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20 69 73 20 ange-counter is
11533 6f 6d 69 74 74 65 64 20 66 6f 72 20 74 68 65 20 omitted for the
11534 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 current transact
11535 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 54 68 69 ion..**.** Thi
11536 73 20 6d 65 63 68 61 6e 69 73 6d 20 6d 65 61 6e s mechanism mean
11537 73 20 74 68 61 74 20 77 68 65 6e 20 72 75 6e 6e s that when runn
11538 69 6e 67 20 69 6e 20 65 78 63 6c 75 73 69 76 65 ing in exclusive
11539 20 6d 6f 64 65 2c 20 61 20 63 6f 6e 6e 65 63 74 mode, a connect
1153a 69 6f 6e 20 0a 2a 2a 20 20 20 6e 65 65 64 20 6f ion .** need o
1153b 6e 6c 79 20 75 70 64 61 74 65 20 74 68 65 20 63 nly update the c
1153c 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20 6f 6e hange-counter on
1153d 63 65 2c 20 66 6f 72 20 74 68 65 20 66 69 72 73 ce, for the firs
1153e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a t transaction.**
1153f 20 20 20 63 6f 6d 6d 69 74 74 65 64 2e 0a 2a 2a committed..**
11540 0a 2a 2a 20 64 62 4d 6f 64 69 66 69 65 64 0a 2a .** dbModified.*
11541 2a 0a 2a 2a 20 20 20 54 68 65 20 64 62 4d 6f 64 *.** The dbMod
11542 69 66 69 65 64 20 66 6c 61 67 20 69 73 20 73 65 ified flag is se
11543 74 20 77 68 65 6e 65 76 65 72 20 61 20 64 61 74 t whenever a dat
11544 61 62 61 73 65 20 70 61 67 65 20 69 73 20 64 69 abase page is di
11545 72 74 69 65 64 2e 0a 2a 2a 20 20 20 49 74 20 69 rtied..** It i
11546 73 20 63 6c 65 61 72 65 64 20 61 74 20 74 68 65 s cleared at the
11547 20 65 6e 64 20 6f 66 20 65 61 63 68 20 74 72 61 end of each tra
11548 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 nsaction..**.**
11549 20 20 49 74 20 69 73 20 75 73 65 64 20 77 68 65 It is used whe
1154a 6e 20 63 6f 6d 6d 69 74 74 69 6e 67 20 6f 72 20 n committing or
1154b 6f 74 68 65 72 77 69 73 65 20 65 6e 64 69 6e 67 otherwise ending
1154c 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 a transaction.
1154d 49 66 0a 2a 2a 20 20 20 74 68 65 20 64 62 4d 6f If.** the dbMo
1154e 64 69 66 69 65 64 20 66 6c 61 67 20 69 73 20 63 dified flag is c
1154f 6c 65 61 72 20 74 68 65 6e 20 6c 65 73 73 20 77 lear then less w
11550 6f 72 6b 20 68 61 73 20 74 6f 20 62 65 20 64 6f ork has to be do
11551 6e 65 2e 0a 2a 2a 0a 2a 2a 20 6a 6f 75 72 6e 61 ne..**.** journa
11552 6c 53 74 61 72 74 65 64 0a 2a 2a 0a 2a 2a 20 20 lStarted.**.**
11553 20 54 68 69 73 20 66 6c 61 67 20 69 73 20 73 65 This flag is se
11554 74 20 77 68 65 6e 65 76 65 72 20 74 68 65 20 74 t whenever the t
11555 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 he main journal
11556 69 73 20 73 79 6e 63 65 64 2e 20 0a 2a 2a 0a 2a is synced. .**.*
11557 2a 20 20 20 54 68 65 20 70 6f 69 6e 74 20 6f 66 * The point of
11558 20 74 68 69 73 20 66 6c 61 67 20 69 73 20 74 68 this flag is th
11559 61 74 20 69 74 20 6d 75 73 74 20 62 65 20 73 65 at it must be se
1155a 74 20 61 66 74 65 72 20 74 68 65 20 0a 2a 2a 20 t after the .**
1155b 20 20 66 69 72 73 74 20 6a 6f 75 72 6e 61 6c 20 first journal
1155c 68 65 61 64 65 72 20 69 6e 20 61 20 6a 6f 75 72 header in a jour
1155d 6e 61 6c 20 66 69 6c 65 20 68 61 73 20 62 65 65 nal file has bee
1155e 6e 20 73 79 6e 63 65 64 20 74 6f 20 64 69 73 6b n synced to disk
1155f 2e 0a 2a 2a 20 20 20 41 66 74 65 72 20 74 68 69 ..** After thi
11560 73 20 68 61 73 20 68 61 70 70 65 6e 65 64 2c 20 s has happened,
11561 6e 65 77 20 70 61 67 65 73 20 61 70 70 65 6e 64 new pages append
11562 65 64 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 ed to the databa
11563 73 65 20 0a 2a 2a 20 20 20 64 6f 20 6e 6f 74 20 se .** do not
11564 6e 65 65 64 20 74 68 65 20 50 47 48 44 52 5f 4e need the PGHDR_N
11565 45 45 44 5f 53 59 4e 43 20 66 6c 61 67 20 73 65 EED_SYNC flag se
11566 74 2c 20 61 73 20 74 68 65 79 20 64 6f 20 6e 6f t, as they do no
11567 74 20 6e 65 65 64 0a 2a 2a 20 20 20 74 6f 20 77 t need.** to w
11568 61 69 74 20 66 6f 72 20 61 20 6a 6f 75 72 6e 61 ait for a journa
11569 6c 20 73 79 6e 63 20 62 65 66 6f 72 65 20 74 68 l sync before th
1156a 65 79 20 63 61 6e 20 62 65 20 77 72 69 74 74 65 ey can be writte
1156b 6e 20 6f 75 74 20 74 6f 0a 2a 2a 20 20 20 74 68 n out to.** th
1156c 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
1156d 28 73 65 65 20 66 75 6e 63 74 69 6f 6e 20 70 61 (see function pa
1156e 67 65 72 5f 77 72 69 74 65 28 29 29 2e 0a 2a 2a ger_write())..**
1156f 20 20 20 0a 2a 2a 20 73 65 74 4d 61 73 74 65 72 .** setMaster
11570 0a 2a 2a 0a 2a 2a 20 20 20 54 68 69 73 20 76 61 .**.** This va
11571 72 69 61 62 6c 65 20 69 73 20 75 73 65 64 20 74 riable is used t
11572 6f 20 65 6e 73 75 72 65 20 74 68 61 74 20 74 68 o ensure that th
11573 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c e master journal
11574 20 66 69 6c 65 20 6e 61 6d 65 0a 2a 2a 20 20 20 file name.**
11575 28 69 66 20 61 6e 79 29 20 69 73 20 6f 6e 6c 79 (if any) is only
11576 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 written into th
11577 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6f e journal file o
11578 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 57 68 65 nce..**.** Whe
11579 6e 20 63 6f 6d 6d 69 74 74 69 6e 67 20 61 20 74 n committing a t
1157a 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65 20 ransaction, the
1157b 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 master journal f
1157c 69 6c 65 20 6e 61 6d 65 20 28 69 66 20 61 6e 79 ile name (if any
1157d 29 0a 2a 2a 20 20 20 6d 61 79 20 62 65 20 77 72 ).** may be wr
1157e 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 6a itten into the j
1157f 6f 75 72 6e 61 6c 20 66 69 6c 65 20 77 68 69 6c ournal file whil
11580 65 20 74 68 65 20 70 61 67 65 72 20 69 73 20 73 e the pager is s
11581 74 69 6c 6c 20 69 6e 0a 2a 2a 20 20 20 50 41 47 till in.** PAG
11582 45 52 5f 52 45 53 45 52 56 45 44 20 73 74 61 74 ER_RESERVED stat
11583 65 20 28 73 65 65 20 43 6f 6d 6d 69 74 50 68 61 e (see CommitPha
11584 73 65 4f 6e 65 28 29 20 66 6f 72 20 74 68 65 20 seOne() for the
11585 61 63 74 69 6f 6e 29 2e 20 49 74 0a 2a 2a 20 20 action). It.**
11586 20 74 68 65 6e 20 61 74 74 65 6d 70 74 73 20 74 then attempts t
11587 6f 20 75 70 67 72 61 64 65 20 74 6f 20 61 6e 20 o upgrade to an
11588 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 2e 20 exclusive lock.
11589 49 66 20 74 68 69 73 20 61 74 74 65 6d 70 74 0a If this attempt.
1158a 2a 2a 20 20 20 66 61 69 6c 73 2c 20 74 68 65 6e ** fails, then
1158b 20 53 51 4c 49 54 45 5f 42 55 53 59 20 6d 61 79 SQLITE_BUSY may
1158c 20 62 65 20 72 65 74 75 72 6e 65 64 20 74 6f 20 be returned to
1158d 74 68 65 20 75 73 65 72 20 61 6e 64 20 74 68 65 the user and the
1158e 20 75 73 65 72 0a 2a 2a 20 20 20 6d 61 79 20 61 user.** may a
1158f 74 74 65 6d 70 74 20 74 6f 20 63 6f 6d 6d 69 74 ttempt to commit
11590 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e the transaction
11591 20 61 67 61 69 6e 20 6c 61 74 65 72 20 28 63 61 again later (ca
11592 6c 6c 69 6e 67 0a 2a 2a 20 20 20 43 6f 6d 6d 69 lling.** Commi
11593 74 50 68 61 73 65 4f 6e 65 28 29 20 61 67 61 69 tPhaseOne() agai
11594 6e 29 2e 20 54 68 69 73 20 66 6c 61 67 20 69 73 n). This flag is
11595 20 75 73 65 64 20 74 6f 20 65 6e 73 75 72 65 20 used to ensure
11596 74 68 61 74 20 74 68 65 20 0a 2a 2a 20 20 20 6d that the .** m
11597 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 aster journal na
11598 6d 65 20 69 73 20 6f 6e 6c 79 20 77 72 69 74 74 me is only writt
11599 65 6e 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 en to the journa
1159a 6c 20 66 69 6c 65 20 74 68 65 20 66 69 72 73 74 l file the first
1159b 0a 2a 2a 20 20 20 74 69 6d 65 20 43 6f 6d 6d 69 .** time Commi
1159c 74 50 68 61 73 65 4f 6e 65 28 29 20 69 73 20 63 tPhaseOne() is c
1159d 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 64 6f 4e alled..**.** doN
1159e 6f 74 53 79 6e 63 0a 2a 2a 0a 2a 2a 20 20 20 54 otSync.**.** T
1159f 68 69 73 20 76 61 72 69 61 62 6c 65 20 69 73 20 his variable is
115a0 73 65 74 20 61 6e 64 20 63 6c 65 61 72 65 64 20 set and cleared
115a1 62 79 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 by sqlite3PagerW
115a2 72 69 74 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 6e 65 rite()..**.** ne
115a3 65 64 53 79 6e 63 0a 2a 2a 0a 2a 2a 20 20 20 54 edSync.**.** T
115a4 4f 44 4f 3a 20 49 74 20 6d 69 67 68 74 20 62 65 ODO: It might be
115a5 20 65 61 73 69 65 72 20 74 6f 20 73 65 74 20 74 easier to set t
115a6 68 69 73 20 76 61 72 69 61 62 6c 65 20 69 6e 20 his variable in
115a7 77 72 69 74 65 4a 6f 75 72 6e 61 6c 48 64 72 28 writeJournalHdr(
115a8 29 0a 2a 2a 20 20 20 61 6e 64 20 77 72 69 74 65 ).** and write
115a9 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28 29 20 MasterJournal()
115aa 6f 6e 6c 79 2e 20 43 68 61 6e 67 65 20 69 74 73 only. Change its
115ab 20 6d 65 61 6e 69 6e 67 20 74 6f 20 22 75 6e 73 meaning to "uns
115ac 79 6e 63 65 64 20 64 61 74 61 0a 2a 2a 20 20 20 ynced data.**
115ad 68 61 73 20 62 65 65 6e 20 77 72 69 74 74 65 6e has been written
115ae 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 22 to the journal"
115af 2e 0a 2a 2a 0a 2a 2a 20 73 75 62 6a 49 6e 4d 65 ..**.** subjInMe
115b0 6d 6f 72 79 0a 2a 2a 0a 2a 2a 20 20 20 54 68 69 mory.**.** Thi
115b1 73 20 69 73 20 61 20 62 6f 6f 6c 65 61 6e 20 76 s is a boolean v
115b2 61 72 69 61 62 6c 65 2e 20 49 66 20 74 72 75 65 ariable. If true
115b3 2c 20 74 68 65 6e 20 61 6e 79 20 72 65 71 75 69 , then any requi
115b4 72 65 64 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 0a red sub-journal.
115b5 2a 2a 20 20 20 69 73 20 6f 70 65 6e 65 64 20 61 ** is opened a
115b6 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a s an in-memory j
115b7 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 66 20 ournal file. If
115b8 66 61 6c 73 65 2c 20 74 68 65 6e 20 69 6e 2d 6d false, then in-m
115b9 65 6d 6f 72 79 0a 2a 2a 20 20 20 73 75 62 2d 6a emory.** sub-j
115ba 6f 75 72 6e 61 6c 73 20 61 72 65 20 6f 6e 6c 79 ournals are only
115bb 20 75 73 65 64 20 66 6f 72 20 69 6e 2d 6d 65 6d used for in-mem
115bc 6f 72 79 20 70 61 67 65 72 20 66 69 6c 65 73 2e ory pager files.
115bd 0a 2a 2f 0a 73 74 72 75 63 74 20 50 61 67 65 72 .*/.struct Pager
115be 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 {. sqlite3_vfs
115bf 20 2a 70 56 66 73 3b 20 20 20 20 20 20 20 20 20 *pVfs;
115c0 20 2f 2a 20 4f 53 20 66 75 6e 63 74 69 6f 6e 73 /* OS functions
115c1 20 74 6f 20 75 73 65 20 66 6f 72 20 49 4f 20 2a to use for IO *
115c2 2f 0a 20 20 75 38 20 65 78 63 6c 75 73 69 76 65 /. u8 exclusive
115c3 4d 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20 20 Mode;
115c4 2f 2a 20 42 6f 6f 6c 65 61 6e 2e 20 54 72 75 65 /* Boolean. True
115c5 20 69 66 20 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65 if locking_mode
115c6 3d 3d 45 58 43 4c 55 53 49 56 45 20 2a 2f 0a 20 ==EXCLUSIVE */.
115c7 20 75 38 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3b u8 journalMode;
115c8 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
115c9 4f 6e 20 6f 66 20 74 68 65 20 50 41 47 45 52 5f On of the PAGER_
115ca 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 2a 20 76 61 JOURNALMODE_* va
115cb 6c 75 65 73 20 2a 2f 0a 20 20 75 38 20 75 73 65 lues */. u8 use
115cc 4a 6f 75 72 6e 61 6c 3b 20 20 20 20 20 20 20 20 Journal;
115cd 20 20 20 20 20 20 2f 2a 20 55 73 65 20 61 20 72 /* Use a r
115ce 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 ollback journal
115cf 6f 6e 20 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a on this file */.
115d0 20 20 75 38 20 6e 6f 52 65 61 64 6c 6f 63 6b 3b u8 noReadlock;
115d1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
115d2 20 44 6f 20 6e 6f 74 20 62 6f 74 68 65 72 20 74 Do not bother t
115d3 6f 20 6f 62 74 61 69 6e 20 72 65 61 64 6c 6f 63 o obtain readloc
115d4 6b 73 20 2a 2f 0a 20 20 75 38 20 6e 6f 53 79 6e ks */. u8 noSyn
115d5 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c;
115d6 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 73 79 /* Do not sy
115d7 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 nc the journal i
115d8 66 20 74 72 75 65 20 2a 2f 0a 20 20 75 38 20 66 f true */. u8 f
115d9 75 6c 6c 53 79 6e 63 3b 20 20 20 20 20 20 20 20 ullSync;
115da 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 65 78 /* Do ex
115db 74 72 61 20 73 79 6e 63 73 20 6f 66 20 74 68 65 tra syncs of the
115dc 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 20 72 6f 62 journal for rob
115dd 75 73 74 6e 65 73 73 20 2a 2f 0a 20 20 75 38 20 ustness */. u8
115de 73 79 6e 63 5f 66 6c 61 67 73 3b 20 20 20 20 20 sync_flags;
115df 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 /* One
115e0 6f 66 20 53 59 4e 43 5f 4e 4f 52 4d 41 4c 20 6f of SYNC_NORMAL o
115e1 72 20 53 59 4e 43 5f 46 55 4c 4c 20 2a 2f 0a 20 r SYNC_FULL */.
115e2 20 75 38 20 74 65 6d 70 46 69 6c 65 3b 20 20 20 u8 tempFile;
115e3 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
115e4 7a 46 69 6c 65 6e 61 6d 65 20 69 73 20 61 20 74 zFilename is a t
115e5 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 2a 2f emporary file */
115e6 0a 20 20 75 38 20 72 65 61 64 4f 6e 6c 79 3b 20 . u8 readOnly;
115e7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
115e8 2a 20 54 72 75 65 20 66 6f 72 20 61 20 72 65 61 * True for a rea
115e9 64 2d 6f 6e 6c 79 20 64 61 74 61 62 61 73 65 20 d-only database
115ea 2a 2f 0a 20 20 75 38 20 6d 65 6d 44 62 3b 20 20 */. u8 memDb;
115eb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
115ec 20 2f 2a 20 54 72 75 65 20 74 6f 20 69 6e 68 69 /* True to inhi
115ed 62 69 74 20 61 6c 6c 20 66 69 6c 65 20 49 2f 4f bit all file I/O
115ee 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65 20 66 6f */.. /* The fo
115ef 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 63 6f llowing block co
115f0 6e 74 61 69 6e 73 20 74 68 6f 73 65 20 63 6c 61 ntains those cla
115f1 73 73 20 6d 65 6d 62 65 72 73 20 74 68 61 74 20 ss members that
115f2 61 72 65 20 64 79 6e 61 6d 69 63 61 6c 6c 79 0a are dynamically.
115f3 20 20 2a 2a 20 6d 6f 64 69 66 69 65 64 20 64 75 ** modified du
115f4 72 69 6e 67 20 6e 6f 72 6d 61 6c 20 6f 70 65 72 ring normal oper
115f5 61 74 69 6f 6e 73 2e 20 54 68 65 20 6f 74 68 65 ations. The othe
115f6 72 20 76 61 72 69 61 62 6c 65 73 20 69 6e 20 74 r variables in t
115f7 68 69 73 20 73 74 72 75 63 74 75 72 65 0a 20 20 his structure.
115f8 2a 2a 20 61 72 65 20 65 69 74 68 65 72 20 63 6f ** are either co
115f9 6e 73 74 61 6e 74 20 74 68 72 6f 75 67 68 6f 75 nstant throughou
115fa 74 20 74 68 65 20 6c 69 66 65 74 69 6d 65 20 6f t the lifetime o
115fb 66 20 74 68 65 20 70 61 67 65 72 2c 20 6f 72 20 f the pager, or
115fc 65 6c 73 65 0a 20 20 2a 2a 20 75 73 65 64 20 74 else. ** used t
115fd 6f 20 73 74 6f 72 65 20 63 6f 6e 66 69 67 75 72 o store configur
115fe 61 74 69 6f 6e 20 70 61 72 61 6d 65 74 65 72 73 ation parameters
115ff 20 74 68 61 74 20 61 66 66 65 63 74 20 74 68 65 that affect the
11600 20 77 61 79 20 74 68 65 20 70 61 67 65 72 20 0a way the pager .
11601 20 20 2a 2a 20 6f 70 65 72 61 74 65 73 2e 0a 20 ** operates..
11602 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 27 73 74 **. ** The 'st
11603 61 74 65 27 20 76 61 72 69 61 62 6c 65 20 69 73 ate' variable is
11604 20 64 65 73 63 72 69 62 65 64 20 69 6e 20 6d 6f described in mo
11605 72 65 20 64 65 74 61 69 6c 20 61 6c 6f 6e 67 20 re detail along
11606 77 69 74 68 20 74 68 65 0a 20 20 2a 2a 20 64 65 with the. ** de
11607 73 63 72 69 70 74 69 6f 6e 73 20 6f 66 20 74 68 scriptions of th
11608 65 20 76 61 6c 75 65 73 20 69 74 20 6d 61 79 20 e values it may
11609 74 61 6b 65 20 2d 20 50 41 47 45 52 5f 55 4e 4c take - PAGER_UNL
1160a 4f 43 4b 20 65 74 63 2e 20 4d 61 6e 79 20 6f 66 OCK etc. Many of
1160b 20 74 68 65 0a 20 20 2a 2a 20 6f 74 68 65 72 20 the. ** other
1160c 76 61 72 69 61 62 6c 65 73 20 69 6e 20 74 68 69 variables in thi
1160d 73 20 62 6c 6f 63 6b 20 61 72 65 20 64 65 73 63 s block are desc
1160e 72 69 62 65 64 20 69 6e 20 74 68 65 20 63 6f 6d ribed in the com
1160f 6d 65 6e 74 20 64 69 72 65 63 74 6c 79 20 0a 20 ment directly .
11610 20 2a 2a 20 61 62 6f 76 65 20 74 68 69 73 20 63 ** above this c
11611 6c 61 73 73 20 64 65 66 69 6e 69 74 69 6f 6e 2e lass definition.
11612 0a 20 20 2a 2f 0a 20 20 75 38 20 73 74 61 74 65 . */. u8 state
11613 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
11614 20 20 20 20 2f 2a 20 50 41 47 45 52 5f 55 4e 4c /* PAGER_UNL
11615 4f 43 4b 2c 20 5f 53 48 41 52 45 44 2c 20 5f 52 OCK, _SHARED, _R
11616 45 53 45 52 56 45 44 2c 20 65 74 63 2e 20 2a 2f ESERVED, etc. */
11617 0a 20 20 75 38 20 64 62 4d 6f 64 69 66 69 65 64 . u8 dbModified
11618 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
11619 2a 20 54 72 75 65 20 69 66 20 74 68 65 72 65 20 * True if there
1161a 61 72 65 20 61 6e 79 20 63 68 61 6e 67 65 73 20 are any changes
1161b 74 6f 20 74 68 65 20 44 62 20 2a 2f 0a 20 20 75 to the Db */. u
1161c 38 20 6e 65 65 64 53 79 6e 63 3b 20 20 20 20 20 8 needSync;
1161d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 /* Tr
1161e 75 65 20 69 66 20 61 6e 20 66 73 79 6e 63 28 29 ue if an fsync()
1161f 20 69 73 20 6e 65 65 64 65 64 20 6f 6e 20 74 68 is needed on th
11620 65 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 75 e journal */. u
11621 38 20 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 8 journalStarted
11622 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 ; /* Tr
11623 75 65 20 69 66 20 68 65 61 64 65 72 20 6f 66 20 ue if header of
11624 6a 6f 75 72 6e 61 6c 20 69 73 20 73 79 6e 63 65 journal is synce
11625 64 20 2a 2f 0a 20 20 75 38 20 63 68 61 6e 67 65 d */. u8 change
11626 43 6f 75 6e 74 44 6f 6e 65 3b 20 20 20 20 20 20 CountDone;
11627 20 20 20 2f 2a 20 53 65 74 20 61 66 74 65 72 20 /* Set after
11628 69 6e 63 72 65 6d 65 6e 74 69 6e 67 20 74 68 65 incrementing the
11629 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20 change-counter
1162a 2a 2f 0a 20 20 75 38 20 73 65 74 4d 61 73 74 65 */. u8 setMaste
1162b 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 r;
1162c 20 2f 2a 20 54 72 75 65 20 69 66 20 61 20 6d 2d /* True if a m-
1162d 6a 20 6e 61 6d 65 20 68 61 73 20 62 65 65 6e 20 j name has been
1162e 77 72 69 74 74 65 6e 20 74 6f 20 6a 72 6e 6c 20 written to jrnl
1162f 2a 2f 0a 20 20 75 38 20 64 6f 4e 6f 74 53 79 6e */. u8 doNotSyn
11630 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c;
11631 20 2f 2a 20 42 6f 6f 6c 65 61 6e 2e 20 57 68 69 /* Boolean. Whi
11632 6c 65 20 74 72 75 65 2c 20 64 6f 20 6e 6f 74 20 le true, do not
11633 73 70 69 6c 6c 20 74 68 65 20 63 61 63 68 65 20 spill the cache
11634 2a 2f 0a 20 20 75 38 20 64 62 53 69 7a 65 56 61 */. u8 dbSizeVa
11635 6c 69 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 lid;
11636 20 2f 2a 20 53 65 74 20 77 68 65 6e 20 64 62 53 /* Set when dbS
11637 69 7a 65 20 69 73 20 63 6f 72 72 65 63 74 20 2a ize is correct *
11638 2f 0a 20 20 75 38 20 73 75 62 6a 49 6e 4d 65 6d /. u8 subjInMem
11639 6f 72 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 ory;
1163a 2f 2a 20 54 72 75 65 20 74 6f 20 75 73 65 20 69 /* True to use i
1163b 6e 2d 6d 65 6d 6f 72 79 20 73 75 62 2d 6a 6f 75 n-memory sub-jou
1163c 72 6e 61 6c 73 20 2a 2f 0a 20 20 50 67 6e 6f 20 rnals */. Pgno
1163d 64 62 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 dbSize;
1163e 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
1163f 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 of pages in the
11640 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 50 database */. P
11641 67 6e 6f 20 64 62 4f 72 69 67 53 69 7a 65 3b 20 gno dbOrigSize;
11642 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 64 62 /* db
11643 53 69 7a 65 20 62 65 66 6f 72 65 20 74 68 65 20 Size before the
11644 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 current transact
11645 69 6f 6e 20 2a 2f 0a 20 20 50 67 6e 6f 20 64 62 ion */. Pgno db
11646 46 69 6c 65 53 69 7a 65 3b 20 20 20 20 20 20 20 FileSize;
11647 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
11648 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 64 f pages in the d
11649 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a atabase file */.
1164a 20 20 69 6e 74 20 65 72 72 43 6f 64 65 3b 20 20 int errCode;
1164b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1164c 20 4f 6e 65 20 6f 66 20 73 65 76 65 72 61 6c 20 One of several
1164d 6b 69 6e 64 73 20 6f 66 20 65 72 72 6f 72 73 20 kinds of errors
1164e 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 63 3b 20 20 */. int nRec;
1164f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11650 20 2f 2a 20 50 61 67 65 73 20 6a 6f 75 72 6e 61 /* Pages journa
11651 6c 6c 65 64 20 73 69 6e 63 65 20 6c 61 73 74 20 lled since last
11652 6a 2d 68 65 61 64 65 72 20 77 72 69 74 74 65 6e j-header written
11653 20 2a 2f 0a 20 20 75 33 32 20 63 6b 73 75 6d 49 */. u32 cksumI
11654 6e 69 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 nit;
11655 20 20 2f 2a 20 51 75 61 73 69 2d 72 61 6e 64 6f /* Quasi-rando
11656 6d 20 76 61 6c 75 65 20 61 64 64 65 64 20 74 6f m value added to
11657 20 65 76 65 72 79 20 63 68 65 63 6b 73 75 6d 20 every checksum
11658 2a 2f 0a 20 20 75 33 32 20 6e 53 75 62 52 65 63 */. u32 nSubRec
11659 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1165a 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 /* Number of re
1165b 63 6f 72 64 73 20 77 72 69 74 74 65 6e 20 74 6f cords written to
1165c 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 2a 2f 0a sub-journal */.
1165d 20 20 42 69 74 76 65 63 20 2a 70 49 6e 4a 6f 75 Bitvec *pInJou
1165e 72 6e 61 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a rnal; /*
1165f 20 4f 6e 65 20 62 69 74 20 66 6f 72 20 65 61 63 One bit for eac
11660 68 20 70 61 67 65 20 69 6e 20 74 68 65 20 64 61 h page in the da
11661 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 tabase file */.
11662 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 66 sqlite3_file *f
11663 64 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 d; /*
11664 46 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 File descriptor
11665 66 6f 72 20 64 61 74 61 62 61 73 65 20 2a 2f 0a for database */.
11666 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a sqlite3_file *
11667 6a 66 64 3b 20 20 20 20 20 20 20 20 20 20 2f 2a jfd; /*
11668 20 46 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 File descriptor
11669 20 66 6f 72 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 for main journa
1166a 6c 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 l */. sqlite3_f
1166b 69 6c 65 20 2a 73 6a 66 64 3b 20 20 20 20 20 20 ile *sjfd;
1166c 20 20 20 2f 2a 20 46 69 6c 65 20 64 65 73 63 72 /* File descr
1166d 69 70 74 6f 72 20 66 6f 72 20 73 75 62 2d 6a 6f iptor for sub-jo
1166e 75 72 6e 61 6c 20 2a 2f 0a 20 20 69 36 34 20 6a urnal */. i64 j
1166f 6f 75 72 6e 61 6c 4f 66 66 3b 20 20 20 20 20 20 ournalOff;
11670 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e /* Curren
11671 74 20 77 72 69 74 65 20 6f 66 66 73 65 74 20 69 t write offset i
11672 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 n the journal fi
11673 6c 65 20 2a 2f 0a 20 20 69 36 34 20 6a 6f 75 72 le */. i64 jour
11674 6e 61 6c 48 64 72 3b 20 20 20 20 20 20 20 20 20 nalHdr;
11675 20 20 20 20 2f 2a 20 42 79 74 65 20 6f 66 66 73 /* Byte offs
11676 65 74 20 74 6f 20 70 72 65 76 69 6f 75 73 20 6a et to previous j
11677 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 2a 2f ournal header */
11678 0a 20 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e . PagerSavepoin
11679 74 20 2a 61 53 61 76 65 70 6f 69 6e 74 3b 20 2f t *aSavepoint; /
1167a 2a 20 41 72 72 61 79 20 6f 66 20 61 63 74 69 76 * Array of activ
1167b 65 20 73 61 76 65 70 6f 69 6e 74 73 20 2a 2f 0a e savepoints */.
1167c 20 20 69 6e 74 20 6e 53 61 76 65 70 6f 69 6e 74 int nSavepoint
1167d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
1167e 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 Number of eleme
1167f 6e 74 73 20 69 6e 20 61 53 61 76 65 70 6f 69 6e nts in aSavepoin
11680 74 5b 5d 20 2a 2f 0a 20 20 63 68 61 72 20 64 62 t[] */. char db
11681 46 69 6c 65 56 65 72 73 5b 31 36 5d 3b 20 20 20 FileVers[16];
11682 20 20 20 20 20 2f 2a 20 43 68 61 6e 67 65 73 20 /* Changes
11683 77 68 65 6e 65 76 65 72 20 64 61 74 61 62 61 73 whenever databas
11684 65 20 66 69 6c 65 20 63 68 61 6e 67 65 73 20 2a e file changes *
11685 2f 0a 20 20 75 33 32 20 73 65 63 74 6f 72 53 69 /. u32 sectorSi
11686 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ze;
11687 2f 2a 20 41 73 73 75 6d 65 64 20 73 65 63 74 6f /* Assumed secto
11688 72 20 73 69 7a 65 20 64 75 72 69 6e 67 20 72 6f r size during ro
11689 6c 6c 62 61 63 6b 20 2a 2f 0a 0a 20 20 75 31 36 llback */.. u16
1168a 20 6e 45 78 74 72 61 3b 20 20 20 20 20 20 20 20 nExtra;
1168b 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 20 /* Add
1168c 74 68 69 73 20 6d 61 6e 79 20 62 79 74 65 73 20 this many bytes
1168d 74 6f 20 65 61 63 68 20 69 6e 2d 6d 65 6d 6f 72 to each in-memor
1168e 79 20 70 61 67 65 20 2a 2f 0a 20 20 69 31 36 20 y page */. i16
1168f 6e 52 65 73 65 72 76 65 3b 20 20 20 20 20 20 20 nReserve;
11690 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
11691 72 20 6f 66 20 75 6e 75 73 65 64 20 62 79 74 65 r of unused byte
11692 73 20 61 74 20 65 6e 64 20 6f 66 20 65 61 63 68 s at end of each
11693 20 70 61 67 65 20 2a 2f 0a 20 20 75 33 32 20 76 page */. u32 v
11694 66 73 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20 fsFlags;
11695 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 /* Flags
11696 66 6f 72 20 73 71 6c 69 74 65 33 5f 76 66 73 2e for sqlite3_vfs.
11697 78 4f 70 65 6e 28 29 20 2a 2f 0a 20 20 69 6e 74 xOpen() */. int
11698 20 70 61 67 65 53 69 7a 65 3b 20 20 20 20 20 20 pageSize;
11699 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
1169a 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 61 er of bytes in a
1169b 20 70 61 67 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 page */. Pgno
1169c 6d 78 50 67 6e 6f 3b 20 20 20 20 20 20 20 20 20 mxPgno;
1169d 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 /* Maximu
1169e 6d 20 61 6c 6c 6f 77 65 64 20 73 69 7a 65 20 6f m allowed size o
1169f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 2a f the database *
116a0 2f 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e /. char *zFilen
116a1 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 ame;
116a2 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 64 /* Name of the d
116a3 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a atabase file */.
116a4 20 20 63 68 61 72 20 2a 7a 4a 6f 75 72 6e 61 6c char *zJournal
116a5 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
116a6 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 6a 6f 75 Name of the jou
116a7 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 20 69 rnal file */. i
116a8 6e 74 20 28 2a 78 42 75 73 79 48 61 6e 64 6c 65 nt (*xBusyHandle
116a9 72 29 28 76 6f 69 64 2a 29 3b 20 2f 2a 20 46 75 r)(void*); /* Fu
116aa 6e 63 74 69 6f 6e 20 74 6f 20 63 61 6c 6c 20 77 nction to call w
116ab 68 65 6e 20 62 75 73 79 20 2a 2f 0a 20 20 76 6f hen busy */. vo
116ac 69 64 20 2a 70 42 75 73 79 48 61 6e 64 6c 65 72 id *pBusyHandler
116ad 41 72 67 3b 20 20 20 20 20 20 2f 2a 20 43 6f 6e Arg; /* Con
116ae 74 65 78 74 20 61 72 67 75 6d 65 6e 74 20 66 6f text argument fo
116af 72 20 78 42 75 73 79 48 61 6e 64 6c 65 72 20 2a r xBusyHandler *
116b0 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
116b1 54 45 53 54 0a 20 20 69 6e 74 20 6e 48 69 74 2c TEST. int nHit,
116b2 20 6e 4d 69 73 73 3b 20 20 20 20 20 20 20 20 20 nMiss;
116b3 20 20 20 2f 2a 20 43 61 63 68 65 20 68 69 74 73 /* Cache hits
116b4 20 61 6e 64 20 6d 69 73 73 69 6e 67 20 2a 2f 0a and missing */.
116b5 20 20 69 6e 74 20 6e 52 65 61 64 2c 20 6e 57 72 int nRead, nWr
116b6 69 74 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a ite; /*
116b7 20 44 61 74 61 62 61 73 65 20 70 61 67 65 73 20 Database pages
116b8 72 65 61 64 2f 77 72 69 74 74 65 6e 20 2a 2f 0a read/written */.
116b9 23 65 6e 64 69 66 0a 20 20 76 6f 69 64 20 28 2a #endif. void (*
116ba 78 52 65 69 6e 69 74 65 72 29 28 44 62 50 61 67 xReiniter)(DbPag
116bb 65 2a 29 3b 20 2f 2a 20 43 61 6c 6c 20 74 68 69 e*); /* Call thi
116bc 73 20 72 6f 75 74 69 6e 65 20 77 68 65 6e 20 72 s routine when r
116bd 65 6c 6f 61 64 69 6e 67 20 70 61 67 65 73 20 2a eloading pages *
116be 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
116bf 48 41 53 5f 43 4f 44 45 43 0a 20 20 76 6f 69 64 HAS_CODEC. void
116c0 20 2a 28 2a 78 43 6f 64 65 63 29 28 76 6f 69 64 *(*xCodec)(void
116c1 2a 2c 76 6f 69 64 2a 2c 50 67 6e 6f 2c 69 6e 74 *,void*,Pgno,int
116c2 29 3b 20 2f 2a 20 52 6f 75 74 69 6e 65 20 66 6f ); /* Routine fo
116c3 72 20 65 6e 2f 64 65 63 6f 64 69 6e 67 20 64 61 r en/decoding da
116c4 74 61 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 ta */. void (*x
116c5 43 6f 64 65 63 53 69 7a 65 43 68 6e 67 29 28 76 CodecSizeChng)(v
116c6 6f 69 64 2a 2c 69 6e 74 2c 69 6e 74 29 3b 20 2f oid*,int,int); /
116c7 2a 20 4e 6f 74 69 66 79 20 6f 66 20 70 61 67 65 * Notify of page
116c8 20 73 69 7a 65 20 63 68 61 6e 67 65 73 20 2a 2f size changes */
116c9 0a 20 20 76 6f 69 64 20 28 2a 78 43 6f 64 65 63 . void (*xCodec
116ca 46 72 65 65 29 28 76 6f 69 64 2a 29 3b 20 20 20 Free)(void*);
116cb 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 73 /* Des
116cc 74 72 75 63 74 6f 72 20 66 6f 72 20 74 68 65 20 tructor for the
116cd 63 6f 64 65 63 20 2a 2f 0a 20 20 76 6f 69 64 20 codec */. void
116ce 2a 70 43 6f 64 65 63 3b 20 20 20 20 20 20 20 20 *pCodec;
116cf 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 /* First
116d0 61 72 67 75 6d 65 6e 74 20 74 6f 20 78 43 6f 64 argument to xCod
116d1 65 63 2e 2e 2e 20 6d 65 74 68 6f 64 73 20 2a 2f ec... methods */
116d2 0a 23 65 6e 64 69 66 0a 20 20 63 68 61 72 20 2a .#endif. char *
116d3 70 54 6d 70 53 70 61 63 65 3b 20 20 20 20 20 20 pTmpSpace;
116d4 20 20 20 20 20 20 2f 2a 20 50 61 67 65 72 2e 70 /* Pager.p
116d5 61 67 65 53 69 7a 65 20 62 79 74 65 73 20 6f 66 ageSize bytes of
116d6 20 73 70 61 63 65 20 66 6f 72 20 74 6d 70 20 75 space for tmp u
116d7 73 65 20 2a 2f 0a 20 20 69 36 34 20 6a 6f 75 72 se */. i64 jour
116d8 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 3b 20 20 20 nalSizeLimit;
116d9 20 20 20 20 2f 2a 20 53 69 7a 65 20 6c 69 6d 69 /* Size limi
116da 74 20 66 6f 72 20 70 65 72 73 69 73 74 65 6e 74 t for persistent
116db 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 20 2a journal files *
116dc 2f 0a 20 20 50 43 61 63 68 65 20 2a 70 50 43 61 /. PCache *pPCa
116dd 63 68 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 che;
116de 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 70 61 /* Pointer to pa
116df 67 65 20 63 61 63 68 65 20 6f 62 6a 65 63 74 20 ge cache object
116e0 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 62 61 63 */. sqlite3_bac
116e1 6b 75 70 20 2a 70 42 61 63 6b 75 70 3b 20 20 20 kup *pBackup;
116e2 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 6c /* Pointer to l
116e3 69 73 74 20 6f 66 20 6f 6e 67 6f 69 6e 67 20 62 ist of ongoing b
116e4 61 63 6b 75 70 20 70 72 6f 63 65 73 73 65 73 20 ackup processes
116e5 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 */.};../*.** The
116e6 20 66 6f 6c 6c 6f 77 69 6e 67 20 67 6c 6f 62 61 following globa
116e7 6c 20 76 61 72 69 61 62 6c 65 73 20 68 6f 6c 64 l variables hold
116e8 20 63 6f 75 6e 74 65 72 73 20 75 73 65 64 20 66 counters used f
116e9 6f 72 0a 2a 2a 20 74 65 73 74 69 6e 67 20 70 75 or.** testing pu
116ea 72 70 6f 73 65 73 20 6f 6e 6c 79 2e 20 20 54 68 rposes only. Th
116eb 65 73 65 20 76 61 72 69 61 62 6c 65 73 20 64 6f ese variables do
116ec 20 6e 6f 74 20 65 78 69 73 74 20 69 6e 0a 2a 2a not exist in.**
116ed 20 61 20 6e 6f 6e 2d 74 65 73 74 69 6e 67 20 62 a non-testing b
116ee 75 69 6c 64 2e 20 20 54 68 65 73 65 20 76 61 72 uild. These var
116ef 69 61 62 6c 65 73 20 61 72 65 20 6e 6f 74 20 74 iables are not t
116f0 68 72 65 61 64 2d 73 61 66 65 2e 0a 2a 2f 0a 23 hread-safe..*/.#
116f1 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 ifdef SQLITE_TES
116f2 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 T.SQLITE_API int
116f3 20 73 71 6c 69 74 65 33 5f 70 61 67 65 72 5f 72 sqlite3_pager_r
116f4 65 61 64 64 62 5f 63 6f 75 6e 74 20 3d 20 30 3b eaddb_count = 0;
116f5 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
116f6 20 66 75 6c 6c 20 70 61 67 65 73 20 72 65 61 64 full pages read
116f7 20 66 72 6f 6d 20 44 42 20 2a 2f 0a 53 51 4c 49 from DB */.SQLI
116f8 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
116f9 65 33 5f 70 61 67 65 72 5f 77 72 69 74 65 64 62 e3_pager_writedb
116fa 5f 63 6f 75 6e 74 20 3d 20 30 3b 20 20 20 2f 2a _count = 0; /*
116fb 20 4e 75 6d 62 65 72 20 6f 66 20 66 75 6c 6c 20 Number of full
116fc 70 61 67 65 73 20 77 72 69 74 74 65 6e 20 74 6f pages written to
116fd 20 44 42 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 DB */.SQLITE_AP
116fe 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 61 I int sqlite3_pa
116ff 67 65 72 5f 77 72 69 74 65 6a 5f 63 6f 75 6e 74 ger_writej_count
11700 20 3d 20 30 3b 20 20 20 20 2f 2a 20 4e 75 6d 62 = 0; /* Numb
11701 65 72 20 6f 66 20 70 61 67 65 73 20 77 72 69 74 er of pages writ
11702 74 65 6e 20 74 6f 20 6a 6f 75 72 6e 61 6c 20 2a ten to journal *
11703 2f 0a 23 20 64 65 66 69 6e 65 20 50 41 47 45 52 /.# define PAGER
11704 5f 49 4e 43 52 28 76 29 20 20 76 2b 2b 0a 23 65 _INCR(v) v++.#e
11705 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 50 41 47 lse.# define PAG
11706 45 52 5f 49 4e 43 52 28 76 29 0a 23 65 6e 64 69 ER_INCR(v).#endi
11707 66 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 4a 6f 75 72 6e f..../*.** Journ
11708 61 6c 20 66 69 6c 65 73 20 62 65 67 69 6e 20 77 al files begin w
11709 69 74 68 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e ith the followin
1170a 67 20 6d 61 67 69 63 20 73 74 72 69 6e 67 2e 20 g magic string.
1170b 20 54 68 65 20 64 61 74 61 0a 2a 2a 20 77 61 73 The data.** was
1170c 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 2f obtained from /
1170d 64 65 76 2f 72 61 6e 64 6f 6d 2e 20 20 49 74 20 dev/random. It
1170e 69 73 20 75 73 65 64 20 6f 6e 6c 79 20 61 73 20 is used only as
1170f 61 20 73 61 6e 69 74 79 20 63 68 65 63 6b 2e 0a a sanity check..
11710 2a 2a 0a 2a 2a 20 53 69 6e 63 65 20 76 65 72 73 **.** Since vers
11711 69 6f 6e 20 32 2e 38 2e 30 2c 20 74 68 65 20 6a ion 2.8.0, the j
11712 6f 75 72 6e 61 6c 20 66 6f 72 6d 61 74 20 63 6f ournal format co
11713 6e 74 61 69 6e 73 20 61 64 64 69 74 69 6f 6e 61 ntains additiona
11714 6c 20 73 61 6e 69 74 79 0a 2a 2a 20 63 68 65 63 l sanity.** chec
11715 6b 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e king information
11716 2e 20 20 49 66 20 74 68 65 20 70 6f 77 65 72 20 . If the power
11717 66 61 69 6c 73 20 77 68 69 6c 65 20 74 68 65 20 fails while the
11718 6a 6f 75 72 6e 61 6c 20 69 73 20 62 65 69 6e 67 journal is being
11719 0a 2a 2a 20 77 72 69 74 74 65 6e 2c 20 73 65 6d .** written, sem
1171a 69 2d 72 61 6e 64 6f 6d 20 67 61 72 62 61 67 65 i-random garbage
1171b 20 64 61 74 61 20 6d 69 67 68 74 20 61 70 70 65 data might appe
1171c 61 72 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 ar in the journa
1171d 6c 0a 2a 2a 20 66 69 6c 65 20 61 66 74 65 72 20 l.** file after
1171e 70 6f 77 65 72 20 69 73 20 72 65 73 74 6f 72 65 power is restore
1171f 64 2e 20 20 49 66 20 61 6e 20 61 74 74 65 6d 70 d. If an attemp
11720 74 20 69 73 20 74 68 65 6e 20 6d 61 64 65 0a 2a t is then made.*
11721 2a 20 74 6f 20 72 6f 6c 6c 20 74 68 65 20 6a 6f * to roll the jo
11722 75 72 6e 61 6c 20 62 61 63 6b 2c 20 74 68 65 20 urnal back, the
11723 64 61 74 61 62 61 73 65 20 63 6f 75 6c 64 20 62 database could b
11724 65 20 63 6f 72 72 75 70 74 65 64 2e 20 20 54 68 e corrupted. Th
11725 65 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 e additional.**
11726 73 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 20 sanity checking
11727 64 61 74 61 20 69 73 20 61 6e 20 61 74 74 65 6d data is an attem
11728 70 74 20 74 6f 20 64 69 73 63 6f 76 65 72 20 74 pt to discover t
11729 68 65 20 67 61 72 62 61 67 65 20 69 6e 20 74 68 he garbage in th
1172a 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 e.** journal and
1172b 20 69 67 6e 6f 72 65 20 69 74 2e 0a 2a 2a 0a 2a ignore it..**.*
1172c 2a 20 54 68 65 20 73 61 6e 69 74 79 20 63 68 65 * The sanity che
1172d 63 6b 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69 6f cking informatio
1172e 6e 20 66 6f 72 20 74 68 65 20 6e 65 77 20 6a 6f n for the new jo
1172f 75 72 6e 61 6c 20 66 6f 72 6d 61 74 20 63 6f 6e urnal format con
11730 73 69 73 74 73 0a 2a 2a 20 6f 66 20 61 20 33 32 sists.** of a 32
11731 2d 62 69 74 20 63 68 65 63 6b 73 75 6d 20 6f 6e -bit checksum on
11732 20 65 61 63 68 20 70 61 67 65 20 6f 66 20 64 61 each page of da
11733 74 61 2e 20 20 54 68 65 20 63 68 65 63 6b 73 75 ta. The checksu
11734 6d 20 63 6f 76 65 72 73 20 62 6f 74 68 0a 2a 2a m covers both.**
11735 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 the page number
11736 20 61 6e 64 20 74 68 65 20 70 50 61 67 65 72 2d and the pPager-
11737 3e 70 61 67 65 53 69 7a 65 20 62 79 74 65 73 20 >pageSize bytes
11738 6f 66 20 64 61 74 61 20 66 6f 72 20 74 68 65 20 of data for the
11739 70 61 67 65 2e 0a 2a 2a 20 54 68 69 73 20 63 6b page..** This ck
1173a 73 75 6d 20 69 73 20 69 6e 69 74 69 61 6c 69 7a sum is initializ
1173b 65 64 20 74 6f 20 61 20 33 32 2d 62 69 74 20 72 ed to a 32-bit r
1173c 61 6e 64 6f 6d 20 76 61 6c 75 65 20 74 68 61 74 andom value that
1173d 20 61 70 70 65 61 72 73 20 69 6e 20 74 68 65 0a appears in the.
1173e 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 ** journal file
1173f 72 69 67 68 74 20 61 66 74 65 72 20 74 68 65 20 right after the
11740 68 65 61 64 65 72 2e 20 20 54 68 65 20 72 61 6e header. The ran
11741 64 6f 6d 20 69 6e 69 74 69 61 6c 69 7a 65 72 20 dom initializer
11742 69 73 20 69 6d 70 6f 72 74 61 6e 74 2c 0a 2a 2a is important,.**
11743 20 62 65 63 61 75 73 65 20 67 61 72 62 61 67 65 because garbage
11744 20 64 61 74 61 20 74 68 61 74 20 61 70 70 65 61 data that appea
11745 72 73 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 rs at the end of
11746 20 61 20 6a 6f 75 72 6e 61 6c 20 69 73 20 6c 69 a journal is li
11747 6b 65 6c 79 0a 2a 2a 20 64 61 74 61 20 74 68 61 kely.** data tha
11748 74 20 77 61 73 20 6f 6e 63 65 20 69 6e 20 6f 74 t was once in ot
11749 68 65 72 20 66 69 6c 65 73 20 74 68 61 74 20 68 her files that h
1174a 61 76 65 20 6e 6f 77 20 62 65 65 6e 20 64 65 6c ave now been del
1174b 65 74 65 64 2e 20 20 49 66 20 74 68 65 0a 2a 2a eted. If the.**
1174c 20 67 61 72 62 61 67 65 20 64 61 74 61 20 63 61 garbage data ca
1174d 6d 65 20 66 72 6f 6d 20 61 6e 20 6f 62 73 6f 6c me from an obsol
1174e 65 74 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ete journal file
1174f 2c 20 74 68 65 20 63 68 65 63 6b 73 75 6d 73 20 , the checksums
11750 6d 69 67 68 74 0a 2a 2a 20 62 65 20 63 6f 72 72 might.** be corr
11751 65 63 74 2e 20 20 42 75 74 20 62 79 20 69 6e 69 ect. But by ini
11752 74 69 61 6c 69 7a 69 6e 67 20 74 68 65 20 63 68 tializing the ch
11753 65 63 6b 73 75 6d 20 74 6f 20 72 61 6e 64 6f 6d ecksum to random
11754 20 76 61 6c 75 65 20 77 68 69 63 68 0a 2a 2a 20 value which.**
11755 69 73 20 64 69 66 66 65 72 65 6e 74 20 66 6f 72 is different for
11756 20 65 76 65 72 79 20 6a 6f 75 72 6e 61 6c 2c 20 every journal,
11757 77 65 20 6d 69 6e 69 6d 69 7a 65 20 74 68 61 74 we minimize that
11758 20 72 69 73 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 risk..*/.static
11759 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 const unsigned
1175a 63 68 61 72 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 char aJournalMag
1175b 69 63 5b 5d 20 3d 20 7b 0a 20 20 30 78 64 39 2c ic[] = {. 0xd9,
1175c 20 30 78 64 35 2c 20 30 78 30 35 2c 20 30 78 66 0xd5, 0x05, 0xf
1175d 39 2c 20 30 78 32 30 2c 20 30 78 61 31 2c 20 30 9, 0x20, 0xa1, 0
1175e 78 36 33 2c 20 30 78 64 37 2c 0a 7d 3b 0a 0a 2f x63, 0xd7,.};../
1175f 2a 0a 2a 2a 20 54 68 65 20 73 69 7a 65 20 6f 66 *.** The size of
11760 20 74 68 65 20 6f 66 20 65 61 63 68 20 70 61 67 the of each pag
11761 65 20 72 65 63 6f 72 64 20 69 6e 20 74 68 65 20 e record in the
11762 6a 6f 75 72 6e 61 6c 20 69 73 20 67 69 76 65 6e journal is given
11763 20 62 79 0a 2a 2a 20 74 68 65 20 66 6f 6c 6c 6f by.** the follo
11764 77 69 6e 67 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 23 wing macro..*/.#
11765 64 65 66 69 6e 65 20 4a 4f 55 52 4e 41 4c 5f 50 define JOURNAL_P
11766 47 5f 53 5a 28 70 50 61 67 65 72 29 20 20 28 28 G_SZ(pPager) ((
11767 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 pPager->pageSize
11768 29 20 2b 20 38 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 ) + 8)../*.** Th
11769 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 e journal header
1176a 20 73 69 7a 65 20 66 6f 72 20 74 68 69 73 20 70 size for this p
1176b 61 67 65 72 2e 20 54 68 69 73 20 69 73 20 75 73 ager. This is us
1176c 75 61 6c 6c 79 20 74 68 65 20 73 61 6d 65 20 0a ually the same .
1176d 2a 2a 20 73 69 7a 65 20 61 73 20 61 20 73 69 6e ** size as a sin
1176e 67 6c 65 20 64 69 73 6b 20 73 65 63 74 6f 72 2e gle disk sector.
1176f 20 53 65 65 20 61 6c 73 6f 20 73 65 74 53 65 63 See also setSec
11770 74 6f 72 53 69 7a 65 28 29 2e 0a 2a 2f 0a 23 64 torSize()..*/.#d
11771 65 66 69 6e 65 20 4a 4f 55 52 4e 41 4c 5f 48 44 efine JOURNAL_HD
11772 52 5f 53 5a 28 70 50 61 67 65 72 29 20 28 70 50 R_SZ(pPager) (pP
11773 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 ager->sectorSize
11774 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 63 )../*.** The mac
11775 72 6f 20 4d 45 4d 44 42 20 69 73 20 74 72 75 65 ro MEMDB is true
11776 20 69 66 20 77 65 20 61 72 65 20 64 65 61 6c 69 if we are deali
11777 6e 67 20 77 69 74 68 20 61 6e 20 69 6e 2d 6d 65 ng with an in-me
11778 6d 6f 72 79 20 64 61 74 61 62 61 73 65 2e 0a 2a mory database..*
11779 2a 20 57 65 20 64 6f 20 74 68 69 73 20 61 73 20 * We do this as
1177a 61 20 6d 61 63 72 6f 20 73 6f 20 74 68 61 74 20 a macro so that
1177b 69 66 20 74 68 65 20 53 51 4c 49 54 45 5f 4f 4d if the SQLITE_OM
1177c 49 54 5f 4d 45 4d 4f 52 59 44 42 20 6d 61 63 72 IT_MEMORYDB macr
1177d 6f 20 69 73 20 73 65 74 2c 0a 2a 2a 20 74 68 65 o is set,.** the
1177e 20 76 61 6c 75 65 20 6f 66 20 4d 45 4d 44 42 20 value of MEMDB
1177f 77 69 6c 6c 20 62 65 20 61 20 63 6f 6e 73 74 61 will be a consta
11780 6e 74 20 61 6e 64 20 74 68 65 20 63 6f 6d 70 69 nt and the compi
11781 6c 65 72 20 77 69 6c 6c 20 6f 70 74 69 6d 69 7a ler will optimiz
11782 65 0a 2a 2a 20 6f 75 74 20 63 6f 64 65 20 74 68 e.** out code th
11783 61 74 20 77 6f 75 6c 64 20 6e 65 76 65 72 20 65 at would never e
11784 78 65 63 75 74 65 2e 0a 2a 2f 0a 23 69 66 64 65 xecute..*/.#ifde
11785 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45 f SQLITE_OMIT_ME
11786 4d 4f 52 59 44 42 0a 23 20 64 65 66 69 6e 65 20 MORYDB.# define
11787 4d 45 4d 44 42 20 30 0a 23 65 6c 73 65 0a 23 20 MEMDB 0.#else.#
11788 64 65 66 69 6e 65 20 4d 45 4d 44 42 20 70 50 61 define MEMDB pPa
11789 67 65 72 2d 3e 6d 65 6d 44 62 0a 23 65 6e 64 69 ger->memDb.#endi
1178a 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 f../*.** The max
1178b 69 6d 75 6d 20 6c 65 67 61 6c 20 70 61 67 65 20 imum legal page
1178c 6e 75 6d 62 65 72 20 69 73 20 28 32 5e 33 31 20 number is (2^31
1178d 2d 20 31 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 - 1)..*/.#define
1178e 20 50 41 47 45 52 5f 4d 41 58 5f 50 47 4e 4f 20 PAGER_MAX_PGNO
1178f 32 31 34 37 34 38 33 36 34 37 0a 0a 23 69 66 6e 2147483647..#ifn
11790 64 65 66 20 4e 44 45 42 55 47 20 0a 2f 2a 0a 2a def NDEBUG ./*.*
11791 2a 20 55 73 61 67 65 3a 0a 2a 2a 0a 2a 2a 20 20 * Usage:.**.**
11792 20 61 73 73 65 72 74 28 20 61 73 73 65 72 74 5f assert( assert_
11793 70 61 67 65 72 5f 73 74 61 74 65 28 70 50 61 67 pager_state(pPag
11794 65 72 29 20 29 3b 0a 2a 2f 0a 73 74 61 74 69 63 er) );.*/.static
11795 20 69 6e 74 20 61 73 73 65 72 74 5f 70 61 67 65 int assert_page
11796 72 5f 73 74 61 74 65 28 50 61 67 65 72 20 2a 70 r_state(Pager *p
11797 50 61 67 65 72 29 7b 0a 0a 20 20 2f 2a 20 41 20 Pager){.. /* A
11798 74 65 6d 70 2d 66 69 6c 65 20 69 73 20 61 6c 77 temp-file is alw
11799 61 79 73 20 69 6e 20 50 41 47 45 52 5f 45 58 43 ays in PAGER_EXC
1179a 4c 55 53 49 56 45 20 6f 72 20 50 41 47 45 52 5f LUSIVE or PAGER_
1179b 53 59 4e 43 45 44 20 73 74 61 74 65 2e 20 2a 2f SYNCED state. */
1179c 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
1179d 72 2d 3e 74 65 6d 70 46 69 6c 65 3d 3d 30 20 7c r->tempFile==0 |
1179e 7c 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e | pPager->state>
1179f 3d 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 =PAGER_EXCLUSIVE
117a0 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 63 68 );.. /* The ch
117a1 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 66 6c angeCountDone fl
117a2 61 67 20 69 73 20 61 6c 77 61 79 73 20 73 65 74 ag is always set
117a3 20 66 6f 72 20 74 65 6d 70 2d 66 69 6c 65 73 20 for temp-files
117a4 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 */. assert( pPa
117a5 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 3d 3d 30 ger->tempFile==0
117a6 20 7c 7c 20 70 50 61 67 65 72 2d 3e 63 68 61 6e || pPager->chan
117a7 67 65 43 6f 75 6e 74 44 6f 6e 65 20 29 3b 0a 0a geCountDone );..
117a8 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 return 1;.}.#e
117a9 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 ndif../*.** Retu
117aa 72 6e 20 74 72 75 65 20 69 66 20 69 74 20 69 73 rn true if it is
117ab 20 6e 65 63 65 73 73 61 72 79 20 74 6f 20 77 72 necessary to wr
117ac 69 74 65 20 70 61 67 65 20 2a 70 50 67 20 69 6e ite page *pPg in
117ad 74 6f 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e to the sub-journ
117ae 61 6c 2e 0a 2a 2a 20 41 20 70 61 67 65 20 6e 65 al..** A page ne
117af 65 64 73 20 74 6f 20 62 65 20 77 72 69 74 74 65 eds to be writte
117b0 6e 20 69 6e 74 6f 20 74 68 65 20 73 75 62 2d 6a n into the sub-j
117b1 6f 75 72 6e 61 6c 20 69 66 20 74 68 65 72 65 20 ournal if there
117b2 65 78 69 73 74 73 20 6f 6e 65 0a 2a 2a 20 6f 72 exists one.** or
117b3 20 6d 6f 72 65 20 6f 70 65 6e 20 73 61 76 65 70 more open savep
117b4 6f 69 6e 74 73 20 66 6f 72 20 77 68 69 63 68 3a oints for which:
117b5 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 54 68 65 20 70 .**.** * The p
117b6 61 67 65 2d 6e 75 6d 62 65 72 20 69 73 20 6c 65 age-number is le
117b7 73 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c ss than or equal
117b8 20 74 6f 20 50 61 67 65 72 53 61 76 65 70 6f 69 to PagerSavepoi
117b9 6e 74 2e 6e 4f 72 69 67 2c 20 61 6e 64 0a 2a 2a nt.nOrig, and.**
117ba 20 20 20 2a 20 54 68 65 20 62 69 74 20 63 6f 72 * The bit cor
117bb 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 74 68 responding to th
117bc 65 20 70 61 67 65 2d 6e 75 6d 62 65 72 20 69 73 e page-number is
117bd 20 6e 6f 74 20 73 65 74 20 69 6e 0a 2a 2a 20 20 not set in.**
117be 20 20 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e PagerSavepoin
117bf 74 2e 70 49 6e 53 61 76 65 70 6f 69 6e 74 2e 0a t.pInSavepoint..
117c0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 75 */.static int su
117c1 62 6a 52 65 71 75 69 72 65 73 50 61 67 65 28 50 bjRequiresPage(P
117c2 67 48 64 72 20 2a 70 50 67 29 7b 0a 20 20 50 67 gHdr *pPg){. Pg
117c3 6e 6f 20 70 67 6e 6f 20 3d 20 70 50 67 2d 3e 70 no pgno = pPg->p
117c4 67 6e 6f 3b 0a 20 20 50 61 67 65 72 20 2a 70 50 gno;. Pager *pP
117c5 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 67 ager = pPg->pPag
117c6 65 72 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66 er;. int i;. f
117c7 6f 72 28 69 3d 30 3b 20 69 3c 70 50 61 67 65 72 or(i=0; i<pPager
117c8 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3b 20 69 2b ->nSavepoint; i+
117c9 2b 29 7b 0a 20 20 20 20 50 61 67 65 72 53 61 76 +){. PagerSav
117ca 65 70 6f 69 6e 74 20 2a 70 20 3d 20 26 70 50 61 epoint *p = &pPa
117cb 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 5b ger->aSavepoint[
117cc 69 5d 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 6e i];. if( p->n
117cd 4f 72 69 67 3e 3d 70 67 6e 6f 20 26 26 20 30 3d Orig>=pgno && 0=
117ce 3d 73 71 6c 69 74 65 33 42 69 74 76 65 63 54 65 =sqlite3BitvecTe
117cf 73 74 28 70 2d 3e 70 49 6e 53 61 76 65 70 6f 69 st(p->pInSavepoi
117d0 6e 74 2c 20 70 67 6e 6f 29 20 29 7b 0a 20 20 20 nt, pgno) ){.
117d1 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 return 1;.
117d2 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
117d3 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 0;.}../*.** Retu
117d4 72 6e 20 74 72 75 65 20 69 66 20 74 68 65 20 70 rn true if the p
117d5 61 67 65 20 69 73 20 61 6c 72 65 61 64 79 20 69 age is already i
117d6 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 n the journal fi
117d7 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e le..*/.static in
117d8 74 20 70 61 67 65 49 6e 4a 6f 75 72 6e 61 6c 28 t pageInJournal(
117d9 50 67 48 64 72 20 2a 70 50 67 29 7b 0a 20 20 72 PgHdr *pPg){. r
117da 65 74 75 72 6e 20 73 71 6c 69 74 65 33 42 69 74 eturn sqlite3Bit
117db 76 65 63 54 65 73 74 28 70 50 67 2d 3e 70 50 61 vecTest(pPg->pPa
117dc 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 2c ger->pInJournal,
117dd 20 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 7d 0a 0a pPg->pgno);.}..
117de 2f 2a 0a 2a 2a 20 52 65 61 64 20 61 20 33 32 2d /*.** Read a 32-
117df 62 69 74 20 69 6e 74 65 67 65 72 20 66 72 6f 6d bit integer from
117e0 20 74 68 65 20 67 69 76 65 6e 20 66 69 6c 65 20 the given file
117e1 64 65 73 63 72 69 70 74 6f 72 2e 20 20 53 74 6f descriptor. Sto
117e2 72 65 20 74 68 65 20 69 6e 74 65 67 65 72 0a 2a re the integer.*
117e3 2a 20 74 68 61 74 20 69 73 20 72 65 61 64 20 69 * that is read i
117e4 6e 20 2a 70 52 65 73 2e 20 20 52 65 74 75 72 6e n *pRes. Return
117e5 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 65 76 SQLITE_OK if ev
117e6 65 72 79 74 68 69 6e 67 20 77 6f 72 6b 65 64 2c erything worked,
117e7 20 6f 72 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 20 or an.** error
117e8 63 6f 64 65 20 69 73 20 73 6f 6d 65 74 68 69 6e code is somethin
117e9 67 20 67 6f 65 73 20 77 72 6f 6e 67 2e 0a 2a 2a g goes wrong..**
117ea 0a 2a 2a 20 41 6c 6c 20 76 61 6c 75 65 73 20 61 .** All values a
117eb 72 65 20 73 74 6f 72 65 64 20 6f 6e 20 64 69 73 re stored on dis
117ec 6b 20 61 73 20 62 69 67 2d 65 6e 64 69 61 6e 2e k as big-endian.
117ed 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 .*/.static int r
117ee 65 61 64 33 32 62 69 74 73 28 73 71 6c 69 74 65 ead32bits(sqlite
117ef 33 5f 66 69 6c 65 20 2a 66 64 2c 20 69 36 34 20 3_file *fd, i64
117f0 6f 66 66 73 65 74 2c 20 75 33 32 20 2a 70 52 65 offset, u32 *pRe
117f1 73 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 s){. unsigned c
117f2 68 61 72 20 61 63 5b 34 5d 3b 0a 20 20 69 6e 74 har ac[4];. int
117f3 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 rc = sqlite3OsR
117f4 65 61 64 28 66 64 2c 20 61 63 2c 20 73 69 7a 65 ead(fd, ac, size
117f5 6f 66 28 61 63 29 2c 20 6f 66 66 73 65 74 29 3b of(ac), offset);
117f6 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
117f7 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2a 70 52 65 E_OK ){. *pRe
117f8 73 20 3d 20 73 71 6c 69 74 65 33 47 65 74 34 62 s = sqlite3Get4b
117f9 79 74 65 28 61 63 29 3b 0a 20 20 7d 0a 20 20 72 yte(ac);. }. r
117fa 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
117fb 2a 2a 20 57 72 69 74 65 20 61 20 33 32 2d 62 69 ** Write a 32-bi
117fc 74 20 69 6e 74 65 67 65 72 20 69 6e 74 6f 20 61 t integer into a
117fd 20 73 74 72 69 6e 67 20 62 75 66 66 65 72 20 69 string buffer i
117fe 6e 20 62 69 67 2d 65 6e 64 69 61 6e 20 62 79 74 n big-endian byt
117ff 65 20 6f 72 64 65 72 2e 0a 2a 2f 0a 23 64 65 66 e order..*/.#def
11800 69 6e 65 20 70 75 74 33 32 62 69 74 73 28 41 2c ine put32bits(A,
11801 42 29 20 20 73 71 6c 69 74 65 33 50 75 74 34 62 B) sqlite3Put4b
11802 79 74 65 28 28 75 38 2a 29 41 2c 42 29 0a 0a 2f yte((u8*)A,B)../
11803 2a 0a 2a 2a 20 57 72 69 74 65 20 61 20 33 32 2d *.** Write a 32-
11804 62 69 74 20 69 6e 74 65 67 65 72 20 69 6e 74 6f bit integer into
11805 20 74 68 65 20 67 69 76 65 6e 20 66 69 6c 65 20 the given file
11806 64 65 73 63 72 69 70 74 6f 72 2e 20 20 52 65 74 descriptor. Ret
11807 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a urn SQLITE_OK.**
11808 20 6f 6e 20 73 75 63 63 65 73 73 20 6f 72 20 61 on success or a
11809 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 n error code is
1180a 73 6f 6d 65 74 68 69 6e 67 20 67 6f 65 73 20 77 something goes w
1180b 72 6f 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 rong..*/.static
1180c 69 6e 74 20 77 72 69 74 65 33 32 62 69 74 73 28 int write32bits(
1180d 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 66 64 sqlite3_file *fd
1180e 2c 20 69 36 34 20 6f 66 66 73 65 74 2c 20 75 33 , i64 offset, u3
1180f 32 20 76 61 6c 29 7b 0a 20 20 63 68 61 72 20 61 2 val){. char a
11810 63 5b 34 5d 3b 0a 20 20 70 75 74 33 32 62 69 74 c[4];. put32bit
11811 73 28 61 63 2c 20 76 61 6c 29 3b 0a 20 20 72 65 s(ac, val);. re
11812 74 75 72 6e 20 73 71 6c 69 74 65 33 4f 73 57 72 turn sqlite3OsWr
11813 69 74 65 28 66 64 2c 20 61 63 2c 20 34 2c 20 6f ite(fd, ac, 4, o
11814 66 66 73 65 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ffset);.}../*.**
11815 20 54 68 65 20 61 72 67 75 6d 65 6e 74 20 74 6f The argument to
11816 20 74 68 69 73 20 6d 61 63 72 6f 20 69 73 20 61 this macro is a
11817 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 file descriptor
11818 20 28 74 79 70 65 20 73 71 6c 69 74 65 33 5f 66 (type sqlite3_f
11819 69 6c 65 2a 29 2e 0a 2a 2a 20 52 65 74 75 72 6e ile*)..** Return
1181a 20 30 20 69 66 20 69 74 20 69 73 20 6e 6f 74 20 0 if it is not
1181b 6f 70 65 6e 2c 20 6f 72 20 6e 6f 6e 2d 7a 65 72 open, or non-zer
1181c 6f 20 28 62 75 74 20 6e 6f 74 20 31 29 20 69 66 o (but not 1) if
1181d 20 69 74 20 69 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 it is..**.** Th
1181e 69 73 20 69 73 20 73 6f 20 74 68 61 74 20 65 78 is is so that ex
1181f 70 72 65 73 73 69 6f 6e 73 20 63 61 6e 20 62 65 pressions can be
11820 20 77 72 69 74 74 65 6e 20 61 73 3a 0a 2a 2a 0a written as:.**.
11821 2a 2a 20 20 20 69 66 28 20 69 73 4f 70 65 6e 28 ** if( isOpen(
11822 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 7b 20 pPager->jfd) ){
11823 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 69 6e 73 74 65 61 ....**.** instea
11824 64 20 6f 66 0a 2a 2a 0a 2a 2a 20 20 20 69 66 28 d of.**.** if(
11825 20 70 50 61 67 65 72 2d 3e 6a 66 64 2d 3e 70 4d pPager->jfd->pM
11826 65 74 68 6f 64 73 20 29 7b 20 2e 2e 2e 0a 2a 2f ethods ){ ....*/
11827 0a 23 64 65 66 69 6e 65 20 69 73 4f 70 65 6e 28 .#define isOpen(
11828 70 46 64 29 20 28 28 70 46 64 29 2d 3e 70 4d 65 pFd) ((pFd)->pMe
11829 74 68 6f 64 73 29 0a 0a 2f 2a 0a 2a 2a 20 49 66 thods)../*.** If
1182a 20 66 69 6c 65 20 70 46 64 20 69 73 20 6f 70 65 file pFd is ope
1182b 6e 2c 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 4f n, call sqlite3O
1182c 73 55 6e 6c 6f 63 6b 28 29 20 6f 6e 20 69 74 2e sUnlock() on it.
1182d 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f .*/.static int o
1182e 73 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f sUnlock(sqlite3_
1182f 66 69 6c 65 20 2a 70 46 64 2c 20 69 6e 74 20 65 file *pFd, int e
11830 4c 6f 63 6b 29 7b 0a 20 20 69 66 28 20 21 69 73 Lock){. if( !is
11831 4f 70 65 6e 28 70 46 64 29 20 29 7b 0a 20 20 20 Open(pFd) ){.
11832 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
11833 4b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 K;. }. return
11834 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 sqlite3OsUnlock(
11835 70 46 64 2c 20 65 4c 6f 63 6b 29 3b 0a 7d 0a 0a pFd, eLock);.}..
11836 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 /*.** This funct
11837 69 6f 6e 20 64 65 74 65 72 6d 69 6e 65 73 20 77 ion determines w
11838 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 hether or not th
11839 65 20 61 74 6f 6d 69 63 2d 77 72 69 74 65 20 6f e atomic-write o
1183a 70 74 69 6d 69 7a 61 74 69 6f 6e 0a 2a 2a 20 63 ptimization.** c
1183b 61 6e 20 62 65 20 75 73 65 64 20 77 69 74 68 20 an be used with
1183c 74 68 69 73 20 70 61 67 65 72 2e 20 54 68 65 20 this pager. The
1183d 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 63 61 6e optimization can
1183e 20 62 65 20 75 73 65 64 20 69 66 3a 0a 2a 2a 0a be used if:.**.
1183f 2a 2a 20 20 28 61 29 20 74 68 65 20 76 61 6c 75 ** (a) the valu
11840 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 4f 73 e returned by Os
11841 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 DeviceCharacteri
11842 73 74 69 63 73 28 29 20 69 6e 64 69 63 61 74 65 stics() indicate
11843 73 20 74 68 61 74 0a 2a 2a 20 20 20 20 20 20 61 s that.** a
11844 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 6d database page m
11845 61 79 20 62 65 20 77 72 69 74 74 65 6e 20 61 74 ay be written at
11846 6f 6d 69 63 61 6c 6c 79 2c 20 61 6e 64 0a 2a 2a omically, and.**
11847 20 20 28 62 29 20 74 68 65 20 76 61 6c 75 65 20 (b) the value
11848 72 65 74 75 72 6e 65 64 20 62 79 20 4f 73 53 65 returned by OsSe
11849 63 74 6f 72 53 69 7a 65 28 29 20 69 73 20 6c 65 ctorSize() is le
1184a 73 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c ss than or equal
1184b 0a 2a 2a 20 20 20 20 20 20 74 6f 20 74 68 65 20 .** to the
1184c 70 61 67 65 20 73 69 7a 65 2e 0a 2a 2a 0a 2a 2a page size..**.**
1184d 20 54 68 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f The optimizatio
1184e 6e 20 69 73 20 61 6c 73 6f 20 61 6c 77 61 79 73 n is also always
1184f 20 65 6e 61 62 6c 65 64 20 66 6f 72 20 74 65 6d enabled for tem
11850 70 6f 72 61 72 79 20 66 69 6c 65 73 2e 20 49 74 porary files. It
11851 20 69 73 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20 is.** an error
11852 74 6f 20 63 61 6c 6c 20 74 68 69 73 20 66 75 6e to call this fun
11853 63 74 69 6f 6e 20 69 66 20 70 50 61 67 65 72 20 ction if pPager
11854 69 73 20 6f 70 65 6e 65 64 20 6f 6e 20 61 6e 20 is opened on an
11855 69 6e 2d 6d 65 6d 6f 72 79 0a 2a 2a 20 64 61 74 in-memory.** dat
11856 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 abase..**.** If
11857 74 68 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e the optimization
11858 20 63 61 6e 6e 6f 74 20 62 65 20 75 73 65 64 2c cannot be used,
11859 20 30 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 0 is returned.
1185a 49 66 20 69 74 20 63 61 6e 20 62 65 20 75 73 65 If it can be use
1185b 64 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 76 d,.** then the v
1185c 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 73 alue returned is
1185d 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 the size of the
1185e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 77 68 journal file wh
1185f 65 6e 20 69 74 0a 2a 2a 20 63 6f 6e 74 61 69 6e en it.** contain
11860 73 20 72 6f 6c 6c 62 61 63 6b 20 64 61 74 61 20 s rollback data
11861 66 6f 72 20 65 78 61 63 74 6c 79 20 6f 6e 65 20 for exactly one
11862 70 61 67 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 page..*/.#ifdef
11863 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54 SQLITE_ENABLE_AT
11864 4f 4d 49 43 5f 57 52 49 54 45 0a 73 74 61 74 69 OMIC_WRITE.stati
11865 63 20 69 6e 74 20 6a 72 6e 6c 42 75 66 66 65 72 c int jrnlBuffer
11866 53 69 7a 65 28 50 61 67 65 72 20 2a 70 50 61 67 Size(Pager *pPag
11867 65 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 21 er){. assert( !
11868 4d 45 4d 44 42 20 29 3b 0a 20 20 69 66 28 20 21 MEMDB );. if( !
11869 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 pPager->tempFile
1186a 20 29 7b 0a 20 20 20 20 69 6e 74 20 64 63 3b 20 ){. int dc;
1186b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1186c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 76 /* Dev
1186d 69 63 65 20 63 68 61 72 61 63 74 65 72 69 73 74 ice characterist
1186e 69 63 73 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e ics */. int n
1186f 53 65 63 74 6f 72 3b 20 20 20 20 20 20 20 20 20 Sector;
11870 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
11871 53 65 63 74 6f 72 20 73 69 7a 65 20 2a 2f 0a 20 Sector size */.
11872 20 20 20 69 6e 74 20 73 7a 50 61 67 65 3b 20 20 int szPage;
11873 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11874 20 20 20 20 20 2f 2a 20 50 61 67 65 20 73 69 7a /* Page siz
11875 65 20 2a 2f 0a 0a 20 20 20 20 61 73 73 65 72 74 e */.. assert
11876 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d ( isOpen(pPager-
11877 3e 66 64 29 20 29 3b 0a 20 20 20 20 64 63 20 3d >fd) );. dc =
11878 20 73 71 6c 69 74 65 33 4f 73 44 65 76 69 63 65 sqlite3OsDevice
11879 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73 28 Characteristics(
1187a 70 50 61 67 65 72 2d 3e 66 64 29 3b 0a 20 20 20 pPager->fd);.
1187b 20 6e 53 65 63 74 6f 72 20 3d 20 70 50 61 67 65 nSector = pPage
1187c 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 3b 0a 20 r->sectorSize;.
1187d 20 20 20 73 7a 50 61 67 65 20 3d 20 70 50 61 67 szPage = pPag
1187e 65 72 2d 3e 70 61 67 65 53 69 7a 65 3b 0a 0a 20 er->pageSize;..
1187f 20 20 20 61 73 73 65 72 74 28 53 51 4c 49 54 45 assert(SQLITE
11880 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 35 31 32 _IOCAP_ATOMIC512
11881 3d 3d 28 35 31 32 3e 3e 38 29 29 3b 0a 20 20 20 ==(512>>8));.
11882 20 61 73 73 65 72 74 28 53 51 4c 49 54 45 5f 49 assert(SQLITE_I
11883 4f 43 41 50 5f 41 54 4f 4d 49 43 36 34 4b 3d 3d OCAP_ATOMIC64K==
11884 28 36 35 35 33 36 3e 3e 38 29 29 3b 0a 20 20 20 (65536>>8));.
11885 20 69 66 28 20 30 3d 3d 28 64 63 26 28 53 51 4c if( 0==(dc&(SQL
11886 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 ITE_IOCAP_ATOMIC
11887 7c 28 73 7a 50 61 67 65 3e 3e 38 29 29 20 7c 7c |(szPage>>8)) ||
11888 20 6e 53 65 63 74 6f 72 3e 73 7a 50 61 67 65 29 nSector>szPage)
11889 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
1188a 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 0;. }. }..
1188b 20 72 65 74 75 72 6e 20 4a 4f 55 52 4e 41 4c 5f return JOURNAL_
1188c 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 20 2b HDR_SZ(pPager) +
1188d 20 4a 4f 55 52 4e 41 4c 5f 50 47 5f 53 5a 28 70 JOURNAL_PG_SZ(p
1188e 50 61 67 65 72 29 3b 0a 7d 0a 23 65 6e 64 69 66 Pager);.}.#endif
1188f 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54 ../*.** If SQLIT
11890 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 20 69 73 E_CHECK_PAGES is
11891 20 64 65 66 69 6e 65 64 20 74 68 65 6e 20 77 65 defined then we
11892 20 64 6f 20 73 6f 6d 65 20 73 61 6e 69 74 79 20 do some sanity
11893 63 68 65 63 6b 69 6e 67 0a 2a 2a 20 6f 6e 20 74 checking.** on t
11894 68 65 20 63 61 63 68 65 20 75 73 69 6e 67 20 61 he cache using a
11895 20 68 61 73 68 20 66 75 6e 63 74 69 6f 6e 2e 20 hash function.
11896 20 54 68 69 73 20 69 73 20 75 73 65 64 20 66 6f This is used fo
11897 72 20 74 65 73 74 69 6e 67 0a 2a 2a 20 61 6e 64 r testing.** and
11898 20 64 65 62 75 67 67 69 6e 67 20 6f 6e 6c 79 2e debugging only.
11899 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 .*/.#ifdef SQLIT
1189a 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a 2f 2a E_CHECK_PAGES./*
1189b 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 33 32 2d .** Return a 32-
1189c 62 69 74 20 68 61 73 68 20 6f 66 20 74 68 65 20 bit hash of the
1189d 70 61 67 65 20 64 61 74 61 20 66 6f 72 20 70 50 page data for pP
1189e 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 75 age..*/.static u
1189f 33 32 20 70 61 67 65 72 5f 64 61 74 61 68 61 73 32 pager_datahas
118a0 68 28 69 6e 74 20 6e 42 79 74 65 2c 20 75 6e 73 h(int nByte, uns
118a1 69 67 6e 65 64 20 63 68 61 72 20 2a 70 44 61 74 igned char *pDat
118a2 61 29 7b 0a 20 20 75 33 32 20 68 61 73 68 20 3d a){. u32 hash =
118a3 20 30 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66 0;. int i;. f
118a4 6f 72 28 69 3d 30 3b 20 69 3c 6e 42 79 74 65 3b or(i=0; i<nByte;
118a5 20 69 2b 2b 29 7b 0a 20 20 20 20 68 61 73 68 20 i++){. hash
118a6 3d 20 28 68 61 73 68 2a 31 30 33 39 29 20 2b 20 = (hash*1039) +
118a7 70 44 61 74 61 5b 69 5d 3b 0a 20 20 7d 0a 20 20 pData[i];. }.
118a8 72 65 74 75 72 6e 20 68 61 73 68 3b 0a 7d 0a 73 return hash;.}.s
118a9 74 61 74 69 63 20 75 33 32 20 70 61 67 65 72 5f tatic u32 pager_
118aa 70 61 67 65 68 61 73 68 28 50 67 48 64 72 20 2a pagehash(PgHdr *
118ab 70 50 61 67 65 29 7b 0a 20 20 72 65 74 75 72 6e pPage){. return
118ac 20 70 61 67 65 72 5f 64 61 74 61 68 61 73 68 28 pager_datahash(
118ad 70 50 61 67 65 2d 3e 70 50 61 67 65 72 2d 3e 70 pPage->pPager->p
118ae 61 67 65 53 69 7a 65 2c 20 28 75 6e 73 69 67 6e ageSize, (unsign
118af 65 64 20 63 68 61 72 20 2a 29 70 50 61 67 65 2d ed char *)pPage-
118b0 3e 70 44 61 74 61 29 3b 0a 7d 0a 73 74 61 74 69 >pData);.}.stati
118b1 63 20 76 6f 69 64 20 70 61 67 65 72 5f 73 65 74 c void pager_set
118b2 5f 70 61 67 65 68 61 73 68 28 50 67 48 64 72 20 _pagehash(PgHdr
118b3 2a 70 50 61 67 65 29 7b 0a 20 20 70 50 61 67 65 *pPage){. pPage
118b4 2d 3e 70 61 67 65 48 61 73 68 20 3d 20 70 61 67 ->pageHash = pag
118b5 65 72 5f 70 61 67 65 68 61 73 68 28 70 50 61 67 er_pagehash(pPag
118b6 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 e);.}../*.** The
118b7 20 43 48 45 43 4b 5f 50 41 47 45 20 6d 61 63 72 CHECK_PAGE macr
118b8 6f 20 74 61 6b 65 73 20 61 20 50 67 48 64 72 2a o takes a PgHdr*
118b9 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 2e as an argument.
118ba 20 49 66 20 53 51 4c 49 54 45 5f 43 48 45 43 4b If SQLITE_CHECK
118bb 5f 50 41 47 45 53 0a 2a 2a 20 69 73 20 64 65 66 _PAGES.** is def
118bc 69 6e 65 64 2c 20 61 6e 64 20 4e 44 45 42 55 47 ined, and NDEBUG
118bd 20 69 73 20 6e 6f 74 20 64 65 66 69 6e 65 64 2c is not defined,
118be 20 61 6e 20 61 73 73 65 72 74 28 29 20 73 74 61 an assert() sta
118bf 74 65 6d 65 6e 74 20 63 68 65 63 6b 73 0a 2a 2a tement checks.**
118c0 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20 69 that the page i
118c1 73 20 65 69 74 68 65 72 20 64 69 72 74 79 20 6f s either dirty o
118c2 72 20 73 74 69 6c 6c 20 6d 61 74 63 68 65 73 20 r still matches
118c3 74 68 65 20 63 61 6c 63 75 6c 61 74 65 64 20 70 the calculated p
118c4 61 67 65 2d 68 61 73 68 2e 0a 2a 2f 0a 23 64 65 age-hash..*/.#de
118c5 66 69 6e 65 20 43 48 45 43 4b 5f 50 41 47 45 28 fine CHECK_PAGE(
118c6 78 29 20 63 68 65 63 6b 50 61 67 65 28 78 29 0a x) checkPage(x).
118c7 73 74 61 74 69 63 20 76 6f 69 64 20 63 68 65 63 static void chec
118c8 6b 50 61 67 65 28 50 67 48 64 72 20 2a 70 50 67 kPage(PgHdr *pPg
118c9 29 7b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 ){. Pager *pPag
118ca 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 72 er = pPg->pPager
118cb 3b 0a 20 20 61 73 73 65 72 74 28 20 21 70 50 67 ;. assert( !pPg
118cc 2d 3e 70 61 67 65 48 61 73 68 20 7c 7c 20 70 50 ->pageHash || pP
118cd 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 0a 20 20 ager->errCode.
118ce 20 20 20 20 7c 7c 20 28 70 50 67 2d 3e 66 6c 61 || (pPg->fla
118cf 67 73 26 50 47 48 44 52 5f 44 49 52 54 59 29 20 gs&PGHDR_DIRTY)
118d0 7c 7c 20 70 50 67 2d 3e 70 61 67 65 48 61 73 68 || pPg->pageHash
118d1 3d 3d 70 61 67 65 72 5f 70 61 67 65 68 61 73 68 ==pager_pagehash
118d2 28 70 50 67 29 20 29 3b 0a 7d 0a 0a 23 65 6c 73 (pPg) );.}..#els
118d3 65 0a 23 64 65 66 69 6e 65 20 70 61 67 65 72 5f e.#define pager_
118d4 64 61 74 61 68 61 73 68 28 58 2c 59 29 20 20 30 datahash(X,Y) 0
118d5 0a 23 64 65 66 69 6e 65 20 70 61 67 65 72 5f 70 .#define pager_p
118d6 61 67 65 68 61 73 68 28 58 29 20 20 30 0a 23 64 agehash(X) 0.#d
118d7 65 66 69 6e 65 20 43 48 45 43 4b 5f 50 41 47 45 efine CHECK_PAGE
118d8 28 78 29 0a 23 65 6e 64 69 66 20 20 2f 2a 20 53 (x).#endif /* S
118d9 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 QLITE_CHECK_PAGE
118da 53 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e S */../*.** When
118db 20 74 68 69 73 20 69 73 20 63 61 6c 6c 65 64 20 this is called
118dc 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
118dd 20 66 6f 72 20 70 61 67 65 72 20 70 50 61 67 65 for pager pPage
118de 72 20 6d 75 73 74 20 62 65 20 6f 70 65 6e 2e 0a r must be open..
118df 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
118e0 20 61 74 74 65 6d 70 74 73 20 74 6f 20 72 65 61 attempts to rea
118e1 64 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e d a master journ
118e2 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 66 72 6f al file name fro
118e3 6d 20 74 68 65 20 0a 2a 2a 20 65 6e 64 20 6f 66 m the .** end of
118e4 20 74 68 65 20 66 69 6c 65 20 61 6e 64 2c 20 69 the file and, i
118e5 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 63 6f f successful, co
118e6 70 69 65 73 20 69 74 20 69 6e 74 6f 20 6d 65 6d pies it into mem
118e7 6f 72 79 20 73 75 70 70 6c 69 65 64 20 0a 2a 2a ory supplied .**
118e8 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72 2e 20 by the caller.
118e9 53 65 65 20 63 6f 6d 6d 65 6e 74 73 20 61 62 6f See comments abo
118ea 76 65 20 77 72 69 74 65 4d 61 73 74 65 72 4a 6f ve writeMasterJo
118eb 75 72 6e 61 6c 28 29 20 66 6f 72 20 74 68 65 20 urnal() for the
118ec 66 6f 72 6d 61 74 0a 2a 2a 20 75 73 65 64 20 74 format.** used t
118ed 6f 20 73 74 6f 72 65 20 61 20 6d 61 73 74 65 72 o store a master
118ee 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 journal file na
118ef 6d 65 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 me at the end of
118f0 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e a journal file.
118f1 0a 2a 2a 0a 2a 2a 20 7a 4d 61 73 74 65 72 20 6d .**.** zMaster m
118f2 75 73 74 20 70 6f 69 6e 74 20 74 6f 20 61 20 62 ust point to a b
118f3 75 66 66 65 72 20 6f 66 20 61 74 20 6c 65 61 73 uffer of at leas
118f4 74 20 6e 4d 61 73 74 65 72 20 62 79 74 65 73 20 t nMaster bytes
118f5 61 6c 6c 6f 63 61 74 65 64 20 62 79 0a 2a 2a 20 allocated by.**
118f6 74 68 65 20 63 61 6c 6c 65 72 2e 20 54 68 69 73 the caller. This
118f7 20 73 68 6f 75 6c 64 20 62 65 20 73 71 6c 69 74 should be sqlit
118f8 65 33 5f 76 66 73 2e 6d 78 50 61 74 68 6e 61 6d e3_vfs.mxPathnam
118f9 65 2b 31 20 28 74 6f 20 65 6e 73 75 72 65 20 74 e+1 (to ensure t
118fa 68 65 72 65 20 69 73 0a 2a 2a 20 65 6e 6f 75 67 here is.** enoug
118fb 68 20 73 70 61 63 65 20 74 6f 20 77 72 69 74 65 h space to write
118fc 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 the master jour
118fd 6e 61 6c 20 6e 61 6d 65 29 2e 20 49 66 20 74 68 nal name). If th
118fe 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c e master journal
118ff 0a 2a 2a 20 6e 61 6d 65 20 69 6e 20 74 68 65 20 .** name in the
11900 6a 6f 75 72 6e 61 6c 20 69 73 20 6c 6f 6e 67 65 journal is longe
11901 72 20 74 68 61 6e 20 6e 4d 61 73 74 65 72 20 62 r than nMaster b
11902 79 74 65 73 20 28 69 6e 63 6c 75 64 69 6e 67 20 ytes (including
11903 61 0a 2a 2a 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 a.** nul-termina
11904 74 6f 72 29 2c 20 74 68 65 6e 20 74 68 69 73 20 tor), then this
11905 69 73 20 68 61 6e 64 6c 65 64 20 61 73 20 69 66 is handled as if
11906 20 6e 6f 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e no master journ
11907 61 6c 20 6e 61 6d 65 0a 2a 2a 20 77 65 72 65 20 al name.** were
11908 70 72 65 73 65 6e 74 20 69 6e 20 74 68 65 20 6a present in the j
11909 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 ournal..**.** If
1190a 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 a master journa
1190b 6c 20 66 69 6c 65 20 6e 61 6d 65 20 69 73 20 70 l file name is p
1190c 72 65 73 65 6e 74 20 61 74 20 74 68 65 20 65 6e resent at the en
1190d 64 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c d of the journal
1190e 0a 2a 2a 20 66 69 6c 65 2c 20 74 68 65 6e 20 69 .** file, then i
1190f 74 20 69 73 20 63 6f 70 69 65 64 20 69 6e 74 6f t is copied into
11910 20 74 68 65 20 62 75 66 66 65 72 20 70 6f 69 6e the buffer poin
11911 74 65 64 20 74 6f 20 62 79 20 7a 4d 61 73 74 65 ted to by zMaste
11912 72 2e 20 41 0a 2a 2a 20 6e 75 6c 2d 74 65 72 6d r. A.** nul-term
11913 69 6e 61 74 6f 72 20 62 79 74 65 20 69 73 20 61 inator byte is a
11914 70 70 65 6e 64 65 64 20 74 6f 20 74 68 65 20 62 ppended to the b
11915 75 66 66 65 72 20 66 6f 6c 6c 6f 77 69 6e 67 20 uffer following
11916 74 68 65 20 6d 61 73 74 65 72 0a 2a 2a 20 6a 6f the master.** jo
11917 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 2e urnal file name.
11918 0a 2a 2a 0a 2a 2a 20 49 66 20 69 74 20 69 73 20 .**.** If it is
11919 64 65 74 65 72 6d 69 6e 65 64 20 74 68 61 74 20 determined that
1191a 6e 6f 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 no master journa
1191b 6c 20 66 69 6c 65 20 6e 61 6d 65 20 69 73 20 70 l file name is p
1191c 72 65 73 65 6e 74 20 0a 2a 2a 20 7a 4d 61 73 74 resent .** zMast
1191d 65 72 5b 30 5d 20 69 73 20 73 65 74 20 74 6f 20 er[0] is set to
1191e 30 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20 0 and SQLITE_OK
1191f 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 returned..**.**
11920 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 If an error occu
11921 72 73 20 77 68 69 6c 65 20 72 65 61 64 69 6e 67 rs while reading
11922 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 from the journa
11923 6c 20 66 69 6c 65 2c 20 61 6e 20 53 51 4c 69 74 l file, an SQLit
11924 65 0a 2a 2a 20 65 72 72 6f 72 20 63 6f 64 65 20 e.** error code
11925 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a is returned..*/.
11926 73 74 61 74 69 63 20 69 6e 74 20 72 65 61 64 4d static int readM
11927 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28 73 71 6c asterJournal(sql
11928 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 72 6e 6c ite3_file *pJrnl
11929 2c 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 2c , char *zMaster,
1192a 20 75 33 32 20 6e 4d 61 73 74 65 72 29 7b 0a 20 u32 nMaster){.
1192b 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 int rc;
1192c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 /* R
1192d 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 eturn code */.
1192e 75 33 32 20 6c 65 6e 3b 20 20 20 20 20 20 20 20 u32 len;
1192f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 /* Le
11930 6e 67 74 68 20 69 6e 20 62 79 74 65 73 20 6f 66 ngth in bytes of
11931 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
11932 6e 61 6d 65 20 2a 2f 0a 20 20 69 36 34 20 73 7a name */. i64 sz
11933 4a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 J;
11934 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 73 69 /* Total si
11935 7a 65 20 69 6e 20 62 79 74 65 73 20 6f 66 20 6a ze in bytes of j
11936 6f 75 72 6e 61 6c 20 66 69 6c 65 20 70 4a 72 6e ournal file pJrn
11937 6c 20 2a 2f 0a 20 20 75 33 32 20 63 6b 73 75 6d l */. u32 cksum
11938 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
11939 20 20 2f 2a 20 4d 4a 20 63 68 65 63 6b 73 75 6d /* MJ checksum
1193a 20 76 61 6c 75 65 20 72 65 61 64 20 66 72 6f 6d value read from
1193b 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 75 33 journal */. u3
1193c 32 20 75 3b 20 20 20 20 20 20 20 20 20 20 20 20 2 u;
1193d 20 20 20 20 20 20 20 20 20 2f 2a 20 55 6e 73 69 /* Unsi
1193e 67 6e 65 64 20 6c 6f 6f 70 20 63 6f 75 6e 74 65 gned loop counte
1193f 72 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 r */. unsigned
11940 63 68 61 72 20 61 4d 61 67 69 63 5b 38 5d 3b 20 char aMagic[8];
11941 20 20 2f 2a 20 41 20 62 75 66 66 65 72 20 74 6f /* A buffer to
11942 20 68 6f 6c 64 20 74 68 65 20 6d 61 67 69 63 20 hold the magic
11943 68 65 61 64 65 72 20 2a 2f 0a 20 20 7a 4d 61 73 header */. zMas
11944 74 65 72 5b 30 5d 20 3d 20 27 5c 30 27 3b 0a 0a ter[0] = '\0';..
11945 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 if( SQLITE_OK!
11946 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 =(rc = sqlite3Os
11947 46 69 6c 65 53 69 7a 65 28 70 4a 72 6e 6c 2c 20 FileSize(pJrnl,
11948 26 73 7a 4a 29 29 0a 20 20 20 7c 7c 20 73 7a 4a &szJ)). || szJ
11949 3c 31 36 0a 20 20 20 7c 7c 20 53 51 4c 49 54 45 <16. || SQLITE
1194a 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65 61 64 33 _OK!=(rc = read3
1194b 32 62 69 74 73 28 70 4a 72 6e 6c 2c 20 73 7a 4a 2bits(pJrnl, szJ
1194c 2d 31 36 2c 20 26 6c 65 6e 29 29 0a 20 20 20 7c -16, &len)). |
1194d 7c 20 6c 65 6e 3e 3d 6e 4d 61 73 74 65 72 20 0a | len>=nMaster .
1194e 20 20 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 || SQLITE_OK!
1194f 3d 28 72 63 20 3d 20 72 65 61 64 33 32 62 69 74 =(rc = read32bit
11950 73 28 70 4a 72 6e 6c 2c 20 73 7a 4a 2d 31 32 2c s(pJrnl, szJ-12,
11951 20 26 63 6b 73 75 6d 29 29 0a 20 20 20 7c 7c 20 &cksum)). ||
11952 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d SQLITE_OK!=(rc =
11953 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 70 sqlite3OsRead(p
11954 4a 72 6e 6c 2c 20 61 4d 61 67 69 63 2c 20 38 2c Jrnl, aMagic, 8,
11955 20 73 7a 4a 2d 38 29 29 0a 20 20 20 7c 7c 20 6d szJ-8)). || m
11956 65 6d 63 6d 70 28 61 4d 61 67 69 63 2c 20 61 4a emcmp(aMagic, aJ
11957 6f 75 72 6e 61 6c 4d 61 67 69 63 2c 20 38 29 0a ournalMagic, 8).
11958 20 20 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 || SQLITE_OK!
11959 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 =(rc = sqlite3Os
1195a 52 65 61 64 28 70 4a 72 6e 6c 2c 20 7a 4d 61 73 Read(pJrnl, zMas
1195b 74 65 72 2c 20 6c 65 6e 2c 20 73 7a 4a 2d 31 36 ter, len, szJ-16
1195c 2d 6c 65 6e 29 29 0a 20 20 29 7b 0a 20 20 20 20 -len)). ){.
1195d 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a return rc;. }..
1195e 20 20 2f 2a 20 53 65 65 20 69 66 20 74 68 65 20 /* See if the
1195f 63 68 65 63 6b 73 75 6d 20 6d 61 74 63 68 65 73 checksum matches
11960 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 the master jour
11961 6e 61 6c 20 6e 61 6d 65 20 2a 2f 0a 20 20 66 6f nal name */. fo
11962 72 28 75 3d 30 3b 20 75 3c 6c 65 6e 3b 20 75 2b r(u=0; u<len; u+
11963 2b 29 7b 0a 20 20 20 20 63 6b 73 75 6d 20 2d 3d +){. cksum -=
11964 20 7a 4d 61 73 74 65 72 5b 75 5d 3b 0a 20 20 7d zMaster[u];. }
11965 0a 20 20 69 66 28 20 63 6b 73 75 6d 20 29 7b 0a . if( cksum ){.
11966 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 63 68 /* If the ch
11967 65 63 6b 73 75 6d 20 64 6f 65 73 6e 27 74 20 61 ecksum doesn't a
11968 64 64 20 75 70 2c 20 74 68 65 6e 20 6f 6e 65 20 dd up, then one
11969 6f 72 20 6d 6f 72 65 20 6f 66 20 74 68 65 20 64 or more of the d
1196a 69 73 6b 20 73 65 63 74 6f 72 73 0a 20 20 20 20 isk sectors.
1196b 2a 2a 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 ** containing th
1196c 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c e master journal
1196d 20 66 69 6c 65 6e 61 6d 65 20 69 73 20 63 6f 72 filename is cor
1196e 72 75 70 74 65 64 2e 20 54 68 69 73 20 6d 65 61 rupted. This mea
1196f 6e 73 0a 20 20 20 20 2a 2a 20 64 65 66 69 6e 69 ns. ** defini
11970 74 65 6c 79 20 72 6f 6c 6c 20 62 61 63 6b 2c 20 tely roll back,
11971 73 6f 20 6a 75 73 74 20 72 65 74 75 72 6e 20 53 so just return S
11972 51 4c 49 54 45 5f 4f 4b 20 61 6e 64 20 72 65 70 QLITE_OK and rep
11973 6f 72 74 20 61 20 28 6e 75 6c 29 0a 20 20 20 20 ort a (nul).
11974 2a 2a 20 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61 ** master-journa
11975 6c 20 66 69 6c 65 6e 61 6d 65 2e 0a 20 20 20 20 l filename..
11976 2a 2f 0a 20 20 20 20 6c 65 6e 20 3d 20 30 3b 0a */. len = 0;.
11977 20 20 7d 0a 20 20 7a 4d 61 73 74 65 72 5b 6c 65 }. zMaster[le
11978 6e 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 20 0a 20 n] = '\0';. .
11979 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
1197a 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 K;.}../*.** Retu
1197b 72 6e 20 74 68 65 20 6f 66 66 73 65 74 20 6f 66 rn the offset of
1197c 20 74 68 65 20 73 65 63 74 6f 72 20 62 6f 75 6e the sector boun
1197d 64 61 72 79 20 61 74 20 6f 72 20 69 6d 6d 65 64 dary at or immed
1197e 69 61 74 65 6c 79 20 0a 2a 2a 20 66 6f 6c 6c 6f iately .** follo
1197f 77 69 6e 67 20 74 68 65 20 76 61 6c 75 65 20 69 wing the value i
11980 6e 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 n pPager->journa
11981 6c 4f 66 66 2c 20 61 73 73 75 6d 69 6e 67 20 61 lOff, assuming a
11982 20 73 65 63 74 6f 72 20 0a 2a 2a 20 73 69 7a 65 sector .** size
11983 20 6f 66 20 70 50 61 67 65 72 2d 3e 73 65 63 74 of pPager->sect
11984 6f 72 53 69 7a 65 20 62 79 74 65 73 2e 0a 2a 2a orSize bytes..**
11985 0a 2a 2a 20 69 2e 65 20 66 6f 72 20 61 20 73 65 .** i.e for a se
11986 63 74 6f 72 20 73 69 7a 65 20 6f 66 20 35 31 32 ctor size of 512
11987 3a 0a 2a 2a 0a 2a 2a 20 20 20 50 61 67 65 72 2e :.**.** Pager.
11988 6a 6f 75 72 6e 61 6c 4f 66 66 20 20 20 20 20 20 journalOff
11989 20 20 20 20 52 65 74 75 72 6e 20 76 61 6c 75 65 Return value
1198a 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d .** ----------
1198b 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1198c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a -------------.**
1198d 20 20 20 30 20 20 20 20 20 20 20 20 20 20 20 20 0
1198e 20 20 20 20 20 20 20 20 20 20 20 20 20 30 0a 2a 0.*
1198f 2a 20 20 20 35 31 32 20 20 20 20 20 20 20 20 20 * 512
11990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 31 51
11991 32 0a 2a 2a 20 20 20 31 30 30 20 20 20 20 20 20 2.** 100
11992 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11993 20 35 31 32 0a 2a 2a 20 20 20 32 30 30 30 20 20 512.** 2000
11994 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11995 20 20 20 20 32 30 34 38 0a 2a 2a 20 0a 2a 2f 0a 2048.** .*/.
11996 73 74 61 74 69 63 20 69 36 34 20 6a 6f 75 72 6e static i64 journ
11997 61 6c 48 64 72 4f 66 66 73 65 74 28 50 61 67 65 alHdrOffset(Page
11998 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 36 r *pPager){. i6
11999 34 20 6f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 4 offset = 0;.
1199a 69 36 34 20 63 20 3d 20 70 50 61 67 65 72 2d 3e i64 c = pPager->
1199b 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a 20 20 69 66 journalOff;. if
1199c 28 20 63 20 29 7b 0a 20 20 20 20 6f 66 66 73 65 ( c ){. offse
1199d 74 20 3d 20 28 28 63 2d 31 29 2f 4a 4f 55 52 4e t = ((c-1)/JOURN
1199e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 AL_HDR_SZ(pPager
1199f 29 20 2b 20 31 29 20 2a 20 4a 4f 55 52 4e 41 4c ) + 1) * JOURNAL
119a0 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 3b _HDR_SZ(pPager);
119a1 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 6f . }. assert( o
119a2 66 66 73 65 74 25 4a 4f 55 52 4e 41 4c 5f 48 44 ffset%JOURNAL_HD
119a3 52 5f 53 5a 28 70 50 61 67 65 72 29 3d 3d 30 20 R_SZ(pPager)==0
119a4 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6f 66 66 );. assert( off
119a5 73 65 74 3e 3d 63 20 29 3b 0a 20 20 61 73 73 65 set>=c );. asse
119a6 72 74 28 20 28 6f 66 66 73 65 74 2d 63 29 3c 4a rt( (offset-c)<J
119a7 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 OURNAL_HDR_SZ(pP
119a8 61 67 65 72 29 20 29 3b 0a 20 20 72 65 74 75 72 ager) );. retur
119a9 6e 20 6f 66 66 73 65 74 3b 0a 7d 0a 0a 2f 2a 0a n offset;.}../*.
119aa 2a 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 ** The journal f
119ab 69 6c 65 20 6d 75 73 74 20 62 65 20 6f 70 65 6e ile must be open
119ac 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 when this funct
119ad 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a ion is called..*
119ae 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 *.** This functi
119af 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66 on is a no-op if
119b0 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
119b1 65 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 77 e has not been w
119b2 72 69 74 74 65 6e 20 74 6f 0a 2a 2a 20 77 69 74 ritten to.** wit
119b3 68 69 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 hin the current
119b4 74 72 61 6e 73 61 63 74 69 6f 6e 20 28 69 2e 65 transaction (i.e
119b5 2e 20 69 66 20 50 61 67 65 72 2e 6a 6f 75 72 6e . if Pager.journ
119b6 61 6c 4f 66 66 3d 3d 30 29 2e 0a 2a 2a 0a 2a 2a alOff==0)..**.**
119b7 20 49 66 20 64 6f 54 72 75 6e 63 61 74 65 20 69 If doTruncate i
119b8 73 20 6e 6f 6e 2d 7a 65 72 6f 20 6f 72 20 74 68 s non-zero or th
119b9 65 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 53 e Pager.journalS
119ba 69 7a 65 4c 69 6d 69 74 20 76 61 72 69 61 62 6c izeLimit variabl
119bb 65 20 69 73 0a 2a 2a 20 73 65 74 20 74 6f 20 30 e is.** set to 0
119bc 2c 20 74 68 65 6e 20 74 72 75 6e 63 61 74 65 20 , then truncate
119bd 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
119be 20 74 6f 20 7a 65 72 6f 20 62 79 74 65 73 20 69 to zero bytes i
119bf 6e 20 73 69 7a 65 2e 20 4f 74 68 65 72 77 69 73 n size. Otherwis
119c0 65 2c 0a 2a 2a 20 7a 65 72 6f 20 74 68 65 20 32 e,.** zero the 2
119c1 38 2d 62 79 74 65 20 68 65 61 64 65 72 20 61 74 8-byte header at
119c2 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 the start of th
119c3 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 e journal file.
119c4 49 6e 20 65 69 74 68 65 72 20 63 61 73 65 2c 20 In either case,
119c5 0a 2a 2a 20 69 66 20 74 68 65 20 70 61 67 65 72 .** if the pager
119c6 20 69 73 20 6e 6f 74 20 69 6e 20 6e 6f 2d 73 79 is not in no-sy
119c7 6e 63 20 6d 6f 64 65 2c 20 73 79 6e 63 20 74 68 nc mode, sync th
119c8 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 e journal file i
119c9 6d 6d 65 64 69 61 74 65 6c 79 20 0a 2a 2a 20 61 mmediately .** a
119ca 66 74 65 72 20 77 72 69 74 69 6e 67 20 6f 72 20 fter writing or
119cb 74 72 75 6e 63 61 74 69 6e 67 20 69 74 2e 0a 2a truncating it..*
119cc 2a 0a 2a 2a 20 49 66 20 50 61 67 65 72 2e 6a 6f *.** If Pager.jo
119cd 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 20 69 urnalSizeLimit i
119ce 73 20 73 65 74 20 74 6f 20 61 20 70 6f 73 69 74 s set to a posit
119cf 69 76 65 2c 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 ive, non-zero va
119d0 6c 75 65 2c 20 61 6e 64 0a 2a 2a 20 66 6f 6c 6c lue, and.** foll
119d1 6f 77 69 6e 67 20 74 68 65 20 74 72 75 6e 63 61 owing the trunca
119d2 74 69 6f 6e 20 6f 72 20 7a 65 72 6f 69 6e 67 20 tion or zeroing
119d3 64 65 73 63 72 69 62 65 64 20 61 62 6f 76 65 20 described above
119d4 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 the size of the
119d5 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 .** journal file
119d6 20 69 6e 20 62 79 74 65 73 20 69 73 20 6c 61 72 in bytes is lar
119d7 67 65 72 20 74 68 61 6e 20 74 68 69 73 20 76 61 ger than this va
119d8 6c 75 65 2c 20 74 68 65 6e 20 74 72 75 6e 63 61 lue, then trunca
119d9 74 65 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 te the.** journa
119da 6c 20 66 69 6c 65 20 74 6f 20 50 61 67 65 72 2e l file to Pager.
119db 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 journalSizeLimit
119dc 20 62 79 74 65 73 2e 20 54 68 65 20 6a 6f 75 72 bytes. The jour
119dd 6e 61 6c 20 66 69 6c 65 20 64 6f 65 73 0a 2a 2a nal file does.**
119de 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20 not need to be
119df 73 79 6e 63 65 64 20 66 6f 6c 6c 6f 77 69 6e 67 synced following
119e0 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 2e this operation.
119e1 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 49 4f 20 .**.** If an IO
119e2 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 61 62 error occurs, ab
119e3 61 6e 64 6f 6e 20 70 72 6f 63 65 73 73 69 6e 67 andon processing
119e4 20 61 6e 64 20 72 65 74 75 72 6e 20 74 68 65 20 and return the
119e5 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a IO error code..*
119e6 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 72 65 74 * Otherwise, ret
119e7 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a urn SQLITE_OK..*
119e8 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 7a 65 72 /.static int zer
119e9 6f 4a 6f 75 72 6e 61 6c 48 64 72 28 50 61 67 65 oJournalHdr(Page
119ea 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 64 r *pPager, int d
119eb 6f 54 72 75 6e 63 61 74 65 29 7b 0a 20 20 69 6e oTruncate){. in
119ec 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
119ed 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
119ee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
119ef 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a /* Return code *
119f0 2f 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f 70 /. assert( isOp
119f1 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 en(pPager->jfd)
119f2 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d );. if( pPager-
119f3 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 29 7b 0a 20 >journalOff ){.
119f4 20 20 20 63 6f 6e 73 74 20 69 36 34 20 69 4c 69 const i64 iLi
119f5 6d 69 74 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f mit = pPager->jo
119f6 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 3b 20 urnalSizeLimit;
119f7 20 20 20 2f 2a 20 4c 6f 63 61 6c 20 63 61 63 68 /* Local cach
119f8 65 20 6f 66 20 6a 73 6c 20 2a 2f 0a 0a 20 20 20 e of jsl */..
119f9 20 49 4f 54 52 41 43 45 28 28 22 4a 5a 45 52 4f IOTRACE(("JZERO
119fa 48 44 52 20 25 70 5c 6e 22 2c 20 70 50 61 67 65 HDR %p\n", pPage
119fb 72 29 29 0a 20 20 20 20 69 66 28 20 64 6f 54 72 r)). if( doTr
119fc 75 6e 63 61 74 65 20 7c 7c 20 69 4c 69 6d 69 74 uncate || iLimit
119fd 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 ==0 ){. rc
119fe 3d 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 = sqlite3OsTrunc
119ff 61 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c ate(pPager->jfd,
11a00 20 30 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 0);. }else{.
11a01 20 20 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e static con
11a02 73 74 20 63 68 61 72 20 7a 65 72 6f 48 64 72 5b st char zeroHdr[
11a03 32 38 5d 20 3d 20 7b 30 7d 3b 0a 20 20 20 20 20 28] = {0};.
11a04 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 rc = sqlite3OsW
11a05 72 69 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 rite(pPager->jfd
11a06 2c 20 7a 65 72 6f 48 64 72 2c 20 73 69 7a 65 6f , zeroHdr, sizeo
11a07 66 28 7a 65 72 6f 48 64 72 29 2c 20 30 29 3b 0a f(zeroHdr), 0);.
11a08 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 }. if( rc
11a09 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 ==SQLITE_OK && !
11a0a 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 29 pPager->noSync )
11a0b 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c {. rc = sql
11a0c 69 74 65 33 4f 73 53 79 6e 63 28 70 50 61 67 65 ite3OsSync(pPage
11a0d 72 2d 3e 6a 66 64 2c 20 53 51 4c 49 54 45 5f 53 r->jfd, SQLITE_S
11a0e 59 4e 43 5f 44 41 54 41 4f 4e 4c 59 7c 70 50 61 YNC_DATAONLY|pPa
11a0f 67 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 29 ger->sync_flags)
11a10 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 ;. }.. /*
11a11 41 74 20 74 68 69 73 20 70 6f 69 6e 74 20 74 68 At this point th
11a12 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 e transaction is
11a13 20 63 6f 6d 6d 69 74 74 65 64 20 62 75 74 20 74 committed but t
11a14 68 65 20 77 72 69 74 65 20 6c 6f 63 6b 20 0a 20 he write lock .
11a15 20 20 20 2a 2a 20 69 73 20 73 74 69 6c 6c 20 68 ** is still h
11a16 65 6c 64 20 6f 6e 20 74 68 65 20 66 69 6c 65 2e eld on the file.
11a17 20 49 66 20 74 68 65 72 65 20 69 73 20 61 20 73 If there is a s
11a18 69 7a 65 20 6c 69 6d 69 74 20 63 6f 6e 66 69 67 ize limit config
11a19 75 72 65 64 20 66 6f 72 20 0a 20 20 20 20 2a 2a ured for . **
11a1a 20 74 68 65 20 70 65 72 73 69 73 74 65 6e 74 20 the persistent
11a1b 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 74 68 65 20 journal and the
11a1c 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 63 75 72 journal file cur
11a1d 72 65 6e 74 6c 79 20 63 6f 6e 73 75 6d 65 73 20 rently consumes
11a1e 6d 6f 72 65 0a 20 20 20 20 2a 2a 20 73 70 61 63 more. ** spac
11a1f 65 20 74 68 61 6e 20 74 68 61 74 20 6c 69 6d 69 e than that limi
11a20 74 20 61 6c 6c 6f 77 73 20 66 6f 72 2c 20 74 72 t allows for, tr
11a21 75 6e 63 61 74 65 20 69 74 20 6e 6f 77 2e 20 54 uncate it now. T
11a22 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 0a here is no need.
11a23 20 20 20 20 2a 2a 20 74 6f 20 73 79 6e 63 20 74 ** to sync t
11a24 68 65 20 66 69 6c 65 20 66 6f 6c 6c 6f 77 69 6e he file followin
11a25 67 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e g this operation
11a26 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 .. */. if(
11a27 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 rc==SQLITE_OK &
11a28 26 20 69 4c 69 6d 69 74 3e 30 20 29 7b 0a 20 20 & iLimit>0 ){.
11a29 20 20 20 20 69 36 34 20 73 7a 3b 0a 20 20 20 20 i64 sz;.
11a2a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
11a2b 46 69 6c 65 53 69 7a 65 28 70 50 61 67 65 72 2d FileSize(pPager-
11a2c 3e 6a 66 64 2c 20 26 73 7a 29 3b 0a 20 20 20 20 >jfd, &sz);.
11a2d 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
11a2e 5f 4f 4b 20 26 26 20 73 7a 3e 69 4c 69 6d 69 74 _OK && sz>iLimit
11a2f 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d ){. rc =
11a30 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 sqlite3OsTrunca
11a31 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 te(pPager->jfd,
11a32 69 4c 69 6d 69 74 29 3b 0a 20 20 20 20 20 20 7d iLimit);. }
11a33 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 . }. }. ret
11a34 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
11a35 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c The journal fil
11a36 65 20 6d 75 73 74 20 62 65 20 6f 70 65 6e 20 77 e must be open w
11a37 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 hen this routine
11a38 20 69 73 20 63 61 6c 6c 65 64 2e 20 41 20 6a 6f is called. A jo
11a39 75 72 6e 61 6c 0a 2a 2a 20 68 65 61 64 65 72 20 urnal.** header
11a3a 28 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 20 (JOURNAL_HDR_SZ
11a3b 62 79 74 65 73 29 20 69 73 20 77 72 69 74 74 65 bytes) is writte
11a3c 6e 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e n into the journ
11a3d 61 6c 20 66 69 6c 65 20 61 74 20 74 68 65 0a 2a al file at the.*
11a3e 2a 20 63 75 72 72 65 6e 74 20 6c 6f 63 61 74 69 * current locati
11a3f 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f on..**.** The fo
11a40 72 6d 61 74 20 66 6f 72 20 74 68 65 20 6a 6f 75 rmat for the jou
11a41 72 6e 61 6c 20 68 65 61 64 65 72 20 69 73 20 61 rnal header is a
11a42 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 20 2d 20 s follows:.** -
11a43 38 20 62 79 74 65 73 3a 20 4d 61 67 69 63 20 69 8 bytes: Magic i
11a44 64 65 6e 74 69 66 79 69 6e 67 20 6a 6f 75 72 6e dentifying journ
11a45 61 6c 20 66 6f 72 6d 61 74 2e 0a 2a 2a 20 2d 20 al format..** -
11a46 34 20 62 79 74 65 73 3a 20 4e 75 6d 62 65 72 20 4 bytes: Number
11a47 6f 66 20 72 65 63 6f 72 64 73 20 69 6e 20 6a 6f of records in jo
11a48 75 72 6e 61 6c 2c 20 6f 72 20 2d 31 20 6e 6f 2d urnal, or -1 no-
11a49 73 79 6e 63 20 6d 6f 64 65 20 69 73 20 6f 6e 2e sync mode is on.
11a4a 0a 2a 2a 20 2d 20 34 20 62 79 74 65 73 3a 20 52 .** - 4 bytes: R
11a4b 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 75 73 65 andom number use
11a4c 64 20 66 6f 72 20 70 61 67 65 20 68 61 73 68 2e d for page hash.
11a4d 0a 2a 2a 20 2d 20 34 20 62 79 74 65 73 3a 20 49 .** - 4 bytes: I
11a4e 6e 69 74 69 61 6c 20 64 61 74 61 62 61 73 65 20 nitial database
11a4f 70 61 67 65 20 63 6f 75 6e 74 2e 0a 2a 2a 20 2d page count..** -
11a50 20 34 20 62 79 74 65 73 3a 20 53 65 63 74 6f 72 4 bytes: Sector
11a51 20 73 69 7a 65 20 75 73 65 64 20 62 79 20 74 68 size used by th
11a52 65 20 70 72 6f 63 65 73 73 20 74 68 61 74 20 77 e process that w
11a53 72 6f 74 65 20 74 68 69 73 20 6a 6f 75 72 6e 61 rote this journa
11a54 6c 2e 0a 2a 2a 20 2d 20 34 20 62 79 74 65 73 3a l..** - 4 bytes:
11a55 20 44 61 74 61 62 61 73 65 20 70 61 67 65 20 73 Database page s
11a56 69 7a 65 2e 0a 2a 2a 20 0a 2a 2a 20 46 6f 6c 6c ize..** .** Foll
11a57 6f 77 65 64 20 62 79 20 28 4a 4f 55 52 4e 41 4c owed by (JOURNAL
11a58 5f 48 44 52 5f 53 5a 20 2d 20 32 38 29 20 62 79 _HDR_SZ - 28) by
11a59 74 65 73 20 6f 66 20 75 6e 75 73 65 64 20 73 70 tes of unused sp
11a5a 61 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ace..*/.static i
11a5b 6e 74 20 77 72 69 74 65 4a 6f 75 72 6e 61 6c 48 nt writeJournalH
11a5c 64 72 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 dr(Pager *pPager
11a5d 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 ){. int rc = SQ
11a5e 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 LITE_OK;
11a5f 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 /* Retu
11a60 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 63 68 61 rn code */. cha
11a61 72 20 2a 7a 48 65 61 64 65 72 20 3d 20 70 50 61 r *zHeader = pPa
11a62 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 3b 20 ger->pTmpSpace;
11a63 20 2f 2a 20 54 65 6d 70 6f 72 61 72 79 20 73 70 /* Temporary sp
11a64 61 63 65 20 75 73 65 64 20 74 6f 20 62 75 69 6c ace used to buil
11a65 64 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 75 33 d header */. u3
11a66 32 20 6e 48 65 61 64 65 72 20 3d 20 70 50 61 67 2 nHeader = pPag
11a67 65 72 2d 3e 70 61 67 65 53 69 7a 65 3b 20 20 20 er->pageSize;
11a68 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 62 75 66 /* Size of buf
11a69 66 65 72 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 fer pointed to b
11a6a 79 20 7a 48 65 61 64 65 72 20 2a 2f 0a 20 20 75 y zHeader */. u
11a6b 33 32 20 6e 57 72 69 74 65 3b 20 20 20 20 20 20 32 nWrite;
11a6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11a6d 20 20 20 2f 2a 20 42 79 74 65 73 20 6f 66 20 68 /* Bytes of h
11a6e 65 61 64 65 72 20 73 65 63 74 6f 72 20 77 72 69 eader sector wri
11a6f 74 74 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 69 tten */. int ii
11a70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
11a71 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
11a72 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f Loop counter */
11a73 0a 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f 70 .. assert( isOp
11a74 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 en(pPager->jfd)
11a75 29 3b 20 20 20 20 20 20 2f 2a 20 4a 6f 75 72 6e ); /* Journ
11a76 61 6c 20 66 69 6c 65 20 6d 75 73 74 20 62 65 20 al file must be
11a77 6f 70 65 6e 2e 20 2a 2f 0a 0a 20 20 69 66 28 20 open. */.. if(
11a78 6e 48 65 61 64 65 72 3e 4a 4f 55 52 4e 41 4c 5f nHeader>JOURNAL_
11a79 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 20 29 HDR_SZ(pPager) )
11a7a 7b 0a 20 20 20 20 6e 48 65 61 64 65 72 20 3d 20 {. nHeader =
11a7b 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 JOURNAL_HDR_SZ(p
11a7c 50 61 67 65 72 29 3b 0a 20 20 7d 0a 0a 20 20 2f Pager);. }.. /
11a7d 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20 61 * If there are a
11a7e 63 74 69 76 65 20 73 61 76 65 70 6f 69 6e 74 73 ctive savepoints
11a7f 20 61 6e 64 20 61 6e 79 20 6f 66 20 74 68 65 6d and any of them
11a80 20 77 65 72 65 20 63 72 65 61 74 65 64 20 0a 20 were created .
11a81 20 2a 2a 20 73 69 6e 63 65 20 74 68 65 20 6d 6f ** since the mo
11a82 73 74 20 72 65 63 65 6e 74 20 6a 6f 75 72 6e 61 st recent journa
11a83 6c 20 68 65 61 64 65 72 20 77 61 73 20 77 72 69 l header was wri
11a84 74 74 65 6e 2c 20 75 70 64 61 74 65 20 74 68 65 tten, update the
11a85 20 0a 20 20 2a 2a 20 50 61 67 65 72 53 61 76 65 . ** PagerSave
11a86 70 6f 69 6e 74 2e 69 48 64 72 4f 66 66 73 65 74 point.iHdrOffset
11a87 20 66 69 65 6c 64 73 20 6e 6f 77 2e 0a 20 20 2a fields now.. *
11a88 2f 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 /. for(ii=0; ii
11a89 3c 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f <pPager->nSavepo
11a8a 69 6e 74 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 int; ii++){.
11a8b 69 66 28 20 70 50 61 67 65 72 2d 3e 61 53 61 76 if( pPager->aSav
11a8c 65 70 6f 69 6e 74 5b 69 69 5d 2e 69 48 64 72 4f epoint[ii].iHdrO
11a8d 66 66 73 65 74 3d 3d 30 20 29 7b 0a 20 20 20 20 ffset==0 ){.
11a8e 20 20 70 50 61 67 65 72 2d 3e 61 53 61 76 65 70 pPager->aSavep
11a8f 6f 69 6e 74 5b 69 69 5d 2e 69 48 64 72 4f 66 66 oint[ii].iHdrOff
11a90 73 65 74 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f set = pPager->jo
11a91 75 72 6e 61 6c 4f 66 66 3b 0a 20 20 20 20 7d 0a urnalOff;. }.
11a92 20 20 7d 0a 0a 20 20 70 50 61 67 65 72 2d 3e 6a }.. pPager->j
11a93 6f 75 72 6e 61 6c 48 64 72 20 3d 20 70 50 61 67 ournalHdr = pPag
11a94 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d er->journalOff =
11a95 20 6a 6f 75 72 6e 61 6c 48 64 72 4f 66 66 73 65 journalHdrOffse
11a96 74 28 70 50 61 67 65 72 29 3b 0a 0a 20 20 2f 2a t(pPager);.. /*
11a97 20 0a 20 20 2a 2a 20 57 72 69 74 65 20 74 68 65 . ** Write the
11a98 20 6e 52 65 63 20 46 69 65 6c 64 20 2d 20 74 68 nRec Field - th
11a99 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 e number of page
11a9a 20 72 65 63 6f 72 64 73 20 74 68 61 74 20 66 6f records that fo
11a9b 6c 6c 6f 77 20 74 68 69 73 0a 20 20 2a 2a 20 6a llow this. ** j
11a9c 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 2e 20 4e ournal header. N
11a9d 6f 72 6d 61 6c 6c 79 2c 20 7a 65 72 6f 20 69 73 ormally, zero is
11a9e 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 69 73 written to this
11a9f 20 76 61 6c 75 65 20 61 74 20 74 68 69 73 20 74 value at this t
11aa0 69 6d 65 2e 0a 20 20 2a 2a 20 41 66 74 65 72 20 ime.. ** After
11aa1 74 68 65 20 72 65 63 6f 72 64 73 20 61 72 65 20 the records are
11aa2 61 64 64 65 64 20 74 6f 20 74 68 65 20 6a 6f 75 added to the jou
11aa3 72 6e 61 6c 20 28 61 6e 64 20 74 68 65 20 6a 6f rnal (and the jo
11aa4 75 72 6e 61 6c 20 73 79 6e 63 65 64 2c 20 0a 20 urnal synced, .
11aa5 20 2a 2a 20 69 66 20 69 6e 20 66 75 6c 6c 2d 73 ** if in full-s
11aa6 79 6e 63 20 6d 6f 64 65 29 2c 20 74 68 65 20 7a ync mode), the z
11aa7 65 72 6f 20 69 73 20 6f 76 65 72 77 72 69 74 74 ero is overwritt
11aa8 65 6e 20 77 69 74 68 20 74 68 65 20 74 72 75 65 en with the true
11aa9 20 6e 75 6d 62 65 72 0a 20 20 2a 2a 20 6f 66 20 number. ** of
11aaa 72 65 63 6f 72 64 73 20 28 73 65 65 20 73 79 6e records (see syn
11aab 63 4a 6f 75 72 6e 61 6c 28 29 29 2e 0a 20 20 2a cJournal()).. *
11aac 2a 0a 20 20 2a 2a 20 41 20 66 61 73 74 65 72 20 *. ** A faster
11aad 61 6c 74 65 72 6e 61 74 69 76 65 20 69 73 20 74 alternative is t
11aae 6f 20 77 72 69 74 65 20 30 78 46 46 46 46 46 46 o write 0xFFFFFF
11aaf 46 46 20 74 6f 20 74 68 65 20 6e 52 65 63 20 66 FF to the nRec f
11ab0 69 65 6c 64 2e 20 57 68 65 6e 0a 20 20 2a 2a 20 ield. When. **
11ab1 72 65 61 64 69 6e 67 20 74 68 65 20 6a 6f 75 72 reading the jour
11ab2 6e 61 6c 20 74 68 69 73 20 76 61 6c 75 65 20 74 nal this value t
11ab3 65 6c 6c 73 20 53 51 4c 69 74 65 20 74 6f 20 61 ells SQLite to a
11ab4 73 73 75 6d 65 20 74 68 61 74 20 74 68 65 0a 20 ssume that the.
11ab5 20 2a 2a 20 72 65 73 74 20 6f 66 20 74 68 65 20 ** rest of the
11ab6 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 63 6f 6e journal file con
11ab7 74 61 69 6e 73 20 76 61 6c 69 64 20 70 61 67 65 tains valid page
11ab8 20 72 65 63 6f 72 64 73 2e 20 54 68 69 73 20 61 records. This a
11ab9 73 73 75 6d 70 74 69 6f 6e 0a 20 20 2a 2a 20 69 ssumption. ** i
11aba 73 20 64 61 6e 67 65 72 6f 75 73 2c 20 61 73 20 s dangerous, as
11abb 69 66 20 61 20 66 61 69 6c 75 72 65 20 6f 63 63 if a failure occ
11abc 75 72 72 65 64 20 77 68 69 6c 73 74 20 77 72 69 urred whilst wri
11abd 74 69 6e 67 20 74 6f 20 74 68 65 20 6a 6f 75 72 ting to the jour
11abe 6e 61 6c 0a 20 20 2a 2a 20 66 69 6c 65 20 69 74 nal. ** file it
11abf 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20 73 6f 6d may contain som
11ac0 65 20 67 61 72 62 61 67 65 20 64 61 74 61 2e 20 e garbage data.
11ac1 54 68 65 72 65 20 61 72 65 20 74 77 6f 20 73 63 There are two sc
11ac2 65 6e 61 72 69 6f 73 0a 20 20 2a 2a 20 77 68 65 enarios. ** whe
11ac3 72 65 20 74 68 69 73 20 72 69 73 6b 20 63 61 6e re this risk can
11ac4 20 62 65 20 69 67 6e 6f 72 65 64 3a 0a 20 20 2a be ignored:. *
11ac5 2a 0a 20 20 2a 2a 20 20 20 2a 20 57 68 65 6e 20 *. ** * When
11ac6 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 the pager is in
11ac7 6e 6f 2d 73 79 6e 63 20 6d 6f 64 65 2e 20 43 6f no-sync mode. Co
11ac8 72 72 75 70 74 69 6f 6e 20 63 61 6e 20 66 6f 6c rruption can fol
11ac9 6c 6f 77 20 61 0a 20 20 2a 2a 20 20 20 20 20 70 low a. ** p
11aca 6f 77 65 72 20 66 61 69 6c 75 72 65 20 69 6e 20 ower failure in
11acb 74 68 69 73 20 63 61 73 65 20 61 6e 79 77 61 79 this case anyway
11acc 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 2a 20 .. **. ** *
11acd 57 68 65 6e 20 74 68 65 20 53 51 4c 49 54 45 5f When the SQLITE_
11ace 49 4f 43 41 50 5f 53 41 46 45 5f 41 50 50 45 4e IOCAP_SAFE_APPEN
11acf 44 20 66 6c 61 67 20 69 73 20 73 65 74 2e 20 54 D flag is set. T
11ad0 68 69 73 20 67 75 61 72 61 6e 74 65 65 73 0a 20 his guarantees.
11ad1 20 2a 2a 20 20 20 20 20 74 68 61 74 20 67 61 72 ** that gar
11ad2 62 61 67 65 20 64 61 74 61 20 69 73 20 6e 65 76 bage data is nev
11ad3 65 72 20 61 70 70 65 6e 64 65 64 20 74 6f 20 74 er appended to t
11ad4 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e he journal file.
11ad5 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 . */. assert(
11ad6 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 isOpen(pPager->f
11ad7 64 29 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6e 6f d) || pPager->no
11ad8 53 79 6e 63 20 29 3b 0a 20 20 69 66 28 20 28 70 Sync );. if( (p
11ad9 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 29 20 7c Pager->noSync) |
11ada 7c 20 28 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e | (pPager->journ
11adb 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f alMode==PAGER_JO
11adc 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 URNALMODE_MEMORY
11add 29 0a 20 20 20 7c 7c 20 28 73 71 6c 69 74 65 33 ). || (sqlite3
11ade 4f 73 44 65 76 69 63 65 43 68 61 72 61 63 74 65 OsDeviceCharacte
11adf 72 69 73 74 69 63 73 28 70 50 61 67 65 72 2d 3e ristics(pPager->
11ae0 66 64 29 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 fd)&SQLITE_IOCAP
11ae1 5f 53 41 46 45 5f 41 50 50 45 4e 44 29 20 0a 20 _SAFE_APPEND) .
11ae2 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 7a ){. memcpy(z
11ae3 48 65 61 64 65 72 2c 20 61 4a 6f 75 72 6e 61 6c Header, aJournal
11ae4 4d 61 67 69 63 2c 20 73 69 7a 65 6f 66 28 61 4a Magic, sizeof(aJ
11ae5 6f 75 72 6e 61 6c 4d 61 67 69 63 29 29 3b 0a 20 ournalMagic));.
11ae6 20 20 20 70 75 74 33 32 62 69 74 73 28 26 7a 48 put32bits(&zH
11ae7 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 61 4a 6f eader[sizeof(aJo
11ae8 75 72 6e 61 6c 4d 61 67 69 63 29 5d 2c 20 30 78 urnalMagic)], 0x
11ae9 66 66 66 66 66 66 66 66 29 3b 0a 20 20 7d 65 6c ffffffff);. }el
11aea 73 65 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28 7a se{. memset(z
11aeb 48 65 61 64 65 72 2c 20 30 2c 20 73 69 7a 65 6f Header, 0, sizeo
11aec 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 f(aJournalMagic)
11aed 2b 34 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 +4);. }.. /* T
11aee 68 65 20 72 61 6e 64 6f 6d 20 63 68 65 63 6b 2d he random check-
11aef 68 61 73 68 20 69 6e 69 74 69 61 6c 69 73 65 72 hash initialiser
11af0 20 2a 2f 20 0a 20 20 73 71 6c 69 74 65 33 5f 72 */ . sqlite3_r
11af1 61 6e 64 6f 6d 6e 65 73 73 28 73 69 7a 65 6f 66 andomness(sizeof
11af2 28 70 50 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e (pPager->cksumIn
11af3 69 74 29 2c 20 26 70 50 61 67 65 72 2d 3e 63 6b it), &pPager->ck
11af4 73 75 6d 49 6e 69 74 29 3b 0a 20 20 70 75 74 33 sumInit);. put3
11af5 32 62 69 74 73 28 26 7a 48 65 61 64 65 72 5b 73 2bits(&zHeader[s
11af6 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 izeof(aJournalMa
11af7 67 69 63 29 2b 34 5d 2c 20 70 50 61 67 65 72 2d gic)+4], pPager-
11af8 3e 63 6b 73 75 6d 49 6e 69 74 29 3b 0a 20 20 2f >cksumInit);. /
11af9 2a 20 54 68 65 20 69 6e 69 74 69 61 6c 20 64 61 * The initial da
11afa 74 61 62 61 73 65 20 73 69 7a 65 20 2a 2f 0a 20 tabase size */.
11afb 20 70 75 74 33 32 62 69 74 73 28 26 7a 48 65 61 put32bits(&zHea
11afc 64 65 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 der[sizeof(aJour
11afd 6e 61 6c 4d 61 67 69 63 29 2b 38 5d 2c 20 70 50 nalMagic)+8], pP
11afe 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 ager->dbOrigSize
11aff 29 3b 0a 20 20 2f 2a 20 54 68 65 20 61 73 73 75 );. /* The assu
11b00 6d 65 64 20 73 65 63 74 6f 72 20 73 69 7a 65 20 med sector size
11b01 66 6f 72 20 74 68 69 73 20 70 72 6f 63 65 73 73 for this process
11b02 20 2a 2f 0a 20 20 70 75 74 33 32 62 69 74 73 28 */. put32bits(
11b03 26 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 &zHeader[sizeof(
11b04 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b 31 aJournalMagic)+1
11b05 32 5d 2c 20 70 50 61 67 65 72 2d 3e 73 65 63 74 2], pPager->sect
11b06 6f 72 53 69 7a 65 29 3b 0a 0a 20 20 2f 2a 20 54 orSize);.. /* T
11b07 68 65 20 70 61 67 65 20 73 69 7a 65 20 2a 2f 0a he page size */.
11b08 20 20 70 75 74 33 32 62 69 74 73 28 26 7a 48 65 put32bits(&zHe
11b09 61 64 65 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 ader[sizeof(aJou
11b0a 72 6e 61 6c 4d 61 67 69 63 29 2b 31 36 5d 2c 20 rnalMagic)+16],
11b0b 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 pPager->pageSize
11b0c 29 3b 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c );.. /* Initial
11b0d 69 7a 69 6e 67 20 74 68 65 20 74 61 69 6c 20 6f izing the tail o
11b0e 66 20 74 68 65 20 62 75 66 66 65 72 20 69 73 20 f the buffer is
11b0f 6e 6f 74 20 6e 65 63 65 73 73 61 72 79 2e 20 20 not necessary.
11b10 45 76 65 72 79 74 68 69 6e 67 0a 20 20 2a 2a 20 Everything. **
11b11 77 6f 72 6b 73 20 66 69 6e 64 20 69 66 20 74 68 works find if th
11b12 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 65 6d 73 e following mems
11b13 65 74 28 29 20 69 73 20 6f 6d 69 74 74 65 64 2e et() is omitted.
11b14 20 20 42 75 74 20 69 6e 69 74 69 61 6c 69 7a 69 But initializi
11b15 6e 67 0a 20 20 2a 2a 20 74 68 65 20 6d 65 6d 6f ng. ** the memo
11b16 72 79 20 70 72 65 76 65 6e 74 73 20 76 61 6c 67 ry prevents valg
11b17 72 69 6e 64 20 66 72 6f 6d 20 63 6f 6d 70 6c 61 rind from compla
11b18 69 6e 69 6e 67 2c 20 73 6f 20 77 65 20 61 72 65 ining, so we are
11b19 20 77 69 6c 6c 69 6e 67 20 74 6f 0a 20 20 2a 2a willing to. **
11b1a 20 74 61 6b 65 20 74 68 65 20 70 65 72 66 6f 72 take the perfor
11b1b 6d 61 6e 63 65 20 68 69 74 2e 0a 20 20 2a 2f 0a mance hit.. */.
11b1c 20 20 6d 65 6d 73 65 74 28 26 7a 48 65 61 64 65 memset(&zHeade
11b1d 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 r[sizeof(aJourna
11b1e 6c 4d 61 67 69 63 29 2b 32 30 5d 2c 20 30 2c 0a lMagic)+20], 0,.
11b1f 20 20 20 20 20 20 20 20 20 6e 48 65 61 64 65 72 nHeader
11b20 2d 28 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 -(sizeof(aJourna
11b21 6c 4d 61 67 69 63 29 2b 32 30 29 29 3b 0a 0a 20 lMagic)+20));..
11b22 20 2f 2a 20 49 6e 20 74 68 65 6f 72 79 2c 20 69 /* In theory, i
11b23 74 20 69 73 20 6f 6e 6c 79 20 6e 65 63 65 73 73 t is only necess
11b24 61 72 79 20 74 6f 20 77 72 69 74 65 20 74 68 65 ary to write the
11b25 20 32 38 20 62 79 74 65 73 20 74 68 61 74 20 74 28 bytes that t
11b26 68 65 20 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c he . ** journal
11b27 20 68 65 61 64 65 72 20 63 6f 6e 73 75 6d 65 73 header consumes
11b28 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 to the journal
11b29 66 69 6c 65 20 68 65 72 65 2e 20 54 68 65 6e 20 file here. Then
11b2a 69 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 0a 20 increment the .
11b2b 20 2a 2a 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 ** Pager.journa
11b2c 6c 4f 66 66 20 76 61 72 69 61 62 6c 65 20 62 79 lOff variable by
11b2d 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 20 JOURNAL_HDR_SZ
11b2e 73 6f 20 74 68 61 74 20 74 68 65 20 6e 65 78 74 so that the next
11b2f 20 0a 20 20 2a 2a 20 72 65 63 6f 72 64 20 69 73 . ** record is
11b30 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 written to the
11b31 66 6f 6c 6c 6f 77 69 6e 67 20 73 65 63 74 6f 72 following sector
11b32 20 28 6c 65 61 76 69 6e 67 20 61 20 67 61 70 20 (leaving a gap
11b33 69 6e 20 74 68 65 20 66 69 6c 65 0a 20 20 2a 2a in the file. **
11b34 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 69 6d that will be im
11b35 70 6c 69 63 69 74 6c 79 20 66 69 6c 6c 65 64 20 plicitly filled
11b36 69 6e 20 62 79 20 74 68 65 20 4f 53 29 2e 0a 20 in by the OS)..
11b37 20 2a 2a 0a 20 20 2a 2a 20 48 6f 77 65 76 65 72 **. ** However
11b38 20 69 74 20 68 61 73 20 62 65 65 6e 20 64 69 73 it has been dis
11b39 63 6f 76 65 72 65 64 20 74 68 61 74 20 6f 6e 20 covered that on
11b3a 73 6f 6d 65 20 73 79 73 74 65 6d 73 20 74 68 69 some systems thi
11b3b 73 20 70 61 74 74 65 72 6e 20 63 61 6e 20 0a 20 s pattern can .
11b3c 20 2a 2a 20 62 65 20 73 69 67 6e 69 66 69 63 61 ** be significa
11b3d 6e 74 6c 79 20 73 6c 6f 77 65 72 20 74 68 61 6e ntly slower than
11b3e 20 63 6f 6e 74 69 67 75 6f 75 73 6c 79 20 77 72 contiguously wr
11b3f 69 74 69 6e 67 20 64 61 74 61 20 74 6f 20 74 68 iting data to th
11b40 65 20 66 69 6c 65 2c 0a 20 20 2a 2a 20 65 76 65 e file,. ** eve
11b41 6e 20 69 66 20 74 68 61 74 20 6d 65 61 6e 73 20 n if that means
11b42 65 78 70 6c 69 63 69 74 6c 79 20 77 72 69 74 69 explicitly writi
11b43 6e 67 20 64 61 74 61 20 74 6f 20 74 68 65 20 62 ng data to the b
11b44 6c 6f 63 6b 20 6f 66 20 0a 20 20 2a 2a 20 28 4a lock of . ** (J
11b45 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 20 2d 20 OURNAL_HDR_SZ -
11b46 32 38 29 20 62 79 74 65 73 20 74 68 61 74 20 77 28) bytes that w
11b47 69 6c 6c 20 6e 6f 74 20 62 65 20 75 73 65 64 2e ill not be used.
11b48 20 53 6f 20 74 68 61 74 20 69 73 20 77 68 61 74 So that is what
11b49 0a 20 20 2a 2a 20 69 73 20 64 6f 6e 65 2e 20 0a . ** is done. .
11b4a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 6c 6f **. ** The lo
11b4b 6f 70 20 69 73 20 72 65 71 75 69 72 65 64 20 68 op is required h
11b4c 65 72 65 20 69 6e 20 63 61 73 65 20 74 68 65 20 ere in case the
11b4d 73 65 63 74 6f 72 2d 73 69 7a 65 20 69 73 20 6c sector-size is l
11b4e 61 72 67 65 72 20 74 68 61 6e 20 74 68 65 20 0a arger than the .
11b4f 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 70 61 ** database pa
11b50 67 65 20 73 69 7a 65 2e 20 53 69 6e 63 65 20 74 ge size. Since t
11b51 68 65 20 7a 48 65 61 64 65 72 20 62 75 66 66 65 he zHeader buffe
11b52 72 20 69 73 20 6f 6e 6c 79 20 50 61 67 65 72 2e r is only Pager.
11b53 70 61 67 65 53 69 7a 65 0a 20 20 2a 2a 20 62 79 pageSize. ** by
11b54 74 65 73 20 69 6e 20 73 69 7a 65 2c 20 6d 6f 72 tes in size, mor
11b55 65 20 74 68 61 6e 20 6f 6e 65 20 63 61 6c 6c 20 e than one call
11b56 74 6f 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 to sqlite3OsWrit
11b57 65 28 29 20 6d 61 79 20 62 65 20 72 65 71 75 69 e() may be requi
11b58 72 65 64 0a 20 20 2a 2a 20 74 6f 20 70 6f 70 75 red. ** to popu
11b59 6c 61 74 65 20 74 68 65 20 65 6e 74 69 72 65 20 late the entire
11b5a 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 73 journal header s
11b5b 65 63 74 6f 72 2e 0a 20 20 2a 2f 20 0a 20 20 66 ector.. */ . f
11b5c 6f 72 28 6e 57 72 69 74 65 3d 30 3b 20 72 63 3d or(nWrite=0; rc=
11b5d 3d 53 51 4c 49 54 45 5f 4f 4b 26 26 6e 57 72 69 =SQLITE_OK&&nWri
11b5e 74 65 3c 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 te<JOURNAL_HDR_S
11b5f 5a 28 70 50 61 67 65 72 29 3b 20 6e 57 72 69 74 Z(pPager); nWrit
11b60 65 2b 3d 6e 48 65 61 64 65 72 29 7b 0a 20 20 20 e+=nHeader){.
11b61 20 49 4f 54 52 41 43 45 28 28 22 4a 48 44 52 20 IOTRACE(("JHDR
11b62 25 70 20 25 6c 6c 64 20 25 64 5c 6e 22 2c 20 70 %p %lld %d\n", p
11b63 50 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 6a Pager, pPager->j
11b64 6f 75 72 6e 61 6c 48 64 72 2c 20 6e 48 65 61 64 ournalHdr, nHead
11b65 65 72 29 29 0a 20 20 20 20 72 63 20 3d 20 73 71 er)). rc = sq
11b66 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 50 61 lite3OsWrite(pPa
11b67 67 65 72 2d 3e 6a 66 64 2c 20 7a 48 65 61 64 65 ger->jfd, zHeade
11b68 72 2c 20 6e 48 65 61 64 65 72 2c 20 70 50 61 67 r, nHeader, pPag
11b69 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 29 3b er->journalOff);
11b6a 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 . pPager->jou
11b6b 72 6e 61 6c 4f 66 66 20 2b 3d 20 6e 48 65 61 64 rnalOff += nHead
11b6c 65 72 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 er;. }.. retur
11b6d 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 n rc;.}../*.** T
11b6e 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
11b6f 6d 75 73 74 20 62 65 20 6f 70 65 6e 20 77 68 65 must be open whe
11b70 6e 20 74 68 69 73 20 69 73 20 63 61 6c 6c 65 64 n this is called
11b71 2e 20 41 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 . A journal head
11b72 65 72 20 66 69 6c 65 0a 2a 2a 20 28 4a 4f 55 52 er file.** (JOUR
11b73 4e 41 4c 5f 48 44 52 5f 53 5a 20 62 79 74 65 73 NAL_HDR_SZ bytes
11b74 29 20 69 73 20 72 65 61 64 20 66 72 6f 6d 20 74 ) is read from t
11b75 68 65 20 63 75 72 72 65 6e 74 20 6c 6f 63 61 74 he current locat
11b76 69 6f 6e 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e ion in the journ
11b77 61 6c 0a 2a 2a 20 66 69 6c 65 2e 20 54 68 65 20 al.** file. The
11b78 63 75 72 72 65 6e 74 20 6c 6f 63 61 74 69 6f 6e current location
11b79 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 in the journal
11b7a 66 69 6c 65 20 69 73 20 67 69 76 65 6e 20 62 79 file is given by
11b7b 0a 2a 2a 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 .** pPager->jour
11b7c 6e 61 6c 4f 66 66 2e 20 53 65 65 20 63 6f 6d 6d nalOff. See comm
11b7d 65 6e 74 73 20 61 62 6f 76 65 20 66 75 6e 63 74 ents above funct
11b7e 69 6f 6e 20 77 72 69 74 65 4a 6f 75 72 6e 61 6c ion writeJournal
11b7f 48 64 72 28 29 20 66 6f 72 0a 2a 2a 20 61 20 64 Hdr() for.** a d
11b80 65 73 63 72 69 70 74 69 6f 6e 20 6f 66 20 74 68 escription of th
11b81 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 e journal header
11b82 20 66 6f 72 6d 61 74 2e 0a 2a 2a 0a 2a 2a 20 49 format..**.** I
11b83 66 20 74 68 65 20 68 65 61 64 65 72 20 69 73 20 f the header is
11b84 72 65 61 64 20 73 75 63 63 65 73 73 66 75 6c 6c read successfull
11b85 79 2c 20 2a 70 4e 52 65 63 20 69 73 20 73 65 74 y, *pNRec is set
11b86 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f to the number o
11b87 66 0a 2a 2a 20 70 61 67 65 20 72 65 63 6f 72 64 f.** page record
11b88 73 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 69 73 s following this
11b89 20 68 65 61 64 65 72 20 61 6e 64 20 2a 70 44 62 header and *pDb
11b8a 53 69 7a 65 20 69 73 20 73 65 74 20 74 6f 20 74 Size is set to t
11b8b 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 0a 2a he size of the.*
11b8c 2a 20 64 61 74 61 62 61 73 65 20 62 65 66 6f 72 * database befor
11b8d 65 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f e the transactio
11b8e 6e 20 62 65 67 61 6e 2c 20 69 6e 20 70 61 67 65 n began, in page
11b8f 73 2e 20 41 6c 73 6f 2c 20 70 50 61 67 65 72 2d s. Also, pPager-
11b90 3e 63 6b 73 75 6d 49 6e 69 74 0a 2a 2a 20 69 73 >cksumInit.** is
11b91 20 73 65 74 20 74 6f 20 74 68 65 20 76 61 6c 75 set to the valu
11b92 65 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 e read from the
11b93 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 2e 20 journal header.
11b94 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 SQLITE_OK is ret
11b95 75 72 6e 65 64 0a 2a 2a 20 69 6e 20 74 68 69 73 urned.** in this
11b96 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 case..**.** If
11b97 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 the journal head
11b98 65 72 20 66 69 6c 65 20 61 70 70 65 61 72 73 20 er file appears
11b99 74 6f 20 62 65 20 63 6f 72 72 75 70 74 65 64 2c to be corrupted,
11b9a 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 69 73 0a SQLITE_DONE is.
11b9b 2a 2a 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 ** returned and
11b9c 2a 70 4e 52 65 63 20 61 6e 64 20 2a 50 44 62 53 *pNRec and *PDbS
11b9d 69 7a 65 20 61 72 65 20 75 6e 64 65 66 69 6e 65 ize are undefine
11b9e 64 2e 20 20 49 66 20 4a 4f 55 52 4e 41 4c 5f 48 d. If JOURNAL_H
11b9f 44 52 5f 53 5a 20 62 79 74 65 73 0a 2a 2a 20 63 DR_SZ bytes.** c
11ba0 61 6e 6e 6f 74 20 62 65 20 72 65 61 64 20 66 72 annot be read fr
11ba1 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 om the journal f
11ba2 69 6c 65 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 ile an error cod
11ba3 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a e is returned..*
11ba4 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 61 /.static int rea
11ba5 64 4a 6f 75 72 6e 61 6c 48 64 72 28 0a 20 20 50 dJournalHdr(. P
11ba6 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 20 20 ager *pPager,
11ba7 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 /* P
11ba8 61 67 65 72 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 ager object */.
11ba9 20 69 6e 74 20 69 73 48 6f 74 2c 0a 20 20 69 36 int isHot,. i6
11baa 34 20 6a 6f 75 72 6e 61 6c 53 69 7a 65 2c 20 20 4 journalSize,
11bab 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 /* Si
11bac 7a 65 20 6f 66 20 74 68 65 20 6f 70 65 6e 20 6a ze of the open j
11bad 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 6e 20 62 ournal file in b
11bae 79 74 65 73 20 2a 2f 0a 20 20 75 33 32 20 2a 70 ytes */. u32 *p
11baf 4e 52 65 63 2c 20 20 20 20 20 20 20 20 20 20 20 NRec,
11bb0 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 56 /* OUT: V
11bb1 61 6c 75 65 20 72 65 61 64 20 66 72 6f 6d 20 74 alue read from t
11bb2 68 65 20 6e 52 65 63 20 66 69 65 6c 64 20 2a 2f he nRec field */
11bb3 0a 20 20 75 33 32 20 2a 70 44 62 53 69 7a 65 20 . u32 *pDbSize
11bb4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11bb5 2f 2a 20 4f 55 54 3a 20 56 61 6c 75 65 20 6f 66 /* OUT: Value of
11bb6 20 6f 72 69 67 69 6e 61 6c 20 64 61 74 61 62 61 original databa
11bb7 73 65 20 73 69 7a 65 20 66 69 65 6c 64 20 2a 2f se size field */
11bb8 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 .){. int rc;
11bb9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11bba 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 /* Return cod
11bbb 65 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 e */. unsigned
11bbc 63 68 61 72 20 61 4d 61 67 69 63 5b 38 5d 3b 20 char aMagic[8];
11bbd 20 20 20 20 2f 2a 20 41 20 62 75 66 66 65 72 20 /* A buffer
11bbe 74 6f 20 68 6f 6c 64 20 74 68 65 20 6d 61 67 69 to hold the magi
11bbf 63 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 69 36 c header */. i6
11bc0 34 20 69 48 64 72 4f 66 66 3b 20 20 20 20 20 20 4 iHdrOff;
11bc1 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 /* Of
11bc2 66 73 65 74 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 fset of journal
11bc3 68 65 61 64 65 72 20 62 65 69 6e 67 20 72 65 61 header being rea
11bc4 64 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 d */.. assert(
11bc5 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a isOpen(pPager->j
11bc6 66 64 29 20 29 3b 20 20 20 20 20 20 2f 2a 20 4a fd) ); /* J
11bc7 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6d 75 73 74 ournal file must
11bc8 20 62 65 20 6f 70 65 6e 2e 20 2a 2f 0a 0a 20 20 be open. */..
11bc9 2f 2a 20 41 64 76 61 6e 63 65 20 50 61 67 65 72 /* Advance Pager
11bca 2e 6a 6f 75 72 6e 61 6c 4f 66 66 20 74 6f 20 74 .journalOff to t
11bcb 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 he start of the
11bcc 6e 65 78 74 20 73 65 63 74 6f 72 2e 20 49 66 20 next sector. If
11bcd 74 68 65 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c the. ** journal
11bce 20 66 69 6c 65 20 69 73 20 74 6f 6f 20 73 6d 61 file is too sma
11bcf 6c 6c 20 66 6f 72 20 74 68 65 72 65 20 74 6f 20 ll for there to
11bd0 62 65 20 61 20 68 65 61 64 65 72 20 73 74 6f 72 be a header stor
11bd1 65 64 20 61 74 20 74 68 69 73 0a 20 20 2a 2a 20 ed at this. **
11bd2 70 6f 69 6e 74 2c 20 72 65 74 75 72 6e 20 53 51 point, return SQ
11bd3 4c 49 54 45 5f 44 4f 4e 45 2e 0a 20 20 2a 2f 0a LITE_DONE.. */.
11bd4 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 pPager->journa
11bd5 6c 4f 66 66 20 3d 20 6a 6f 75 72 6e 61 6c 48 64 lOff = journalHd
11bd6 72 4f 66 66 73 65 74 28 70 50 61 67 65 72 29 3b rOffset(pPager);
11bd7 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6a . if( pPager->j
11bd8 6f 75 72 6e 61 6c 4f 66 66 2b 4a 4f 55 52 4e 41 ournalOff+JOURNA
11bd9 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 L_HDR_SZ(pPager)
11bda 20 3e 20 6a 6f 75 72 6e 61 6c 53 69 7a 65 20 29 > journalSize )
11bdb 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
11bdc 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d 0a 20 20 ITE_DONE;. }.
11bdd 69 48 64 72 4f 66 66 20 3d 20 70 50 61 67 65 72 iHdrOff = pPager
11bde 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a 0a 20 ->journalOff;..
11bdf 20 2f 2a 20 52 65 61 64 20 69 6e 20 74 68 65 20 /* Read in the
11be0 66 69 72 73 74 20 38 20 62 79 74 65 73 20 6f 66 first 8 bytes of
11be1 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 the journal hea
11be2 64 65 72 2e 20 49 66 20 74 68 65 79 20 64 6f 20 der. If they do
11be3 6e 6f 74 20 6d 61 74 63 68 0a 20 20 2a 2a 20 74 not match. ** t
11be4 68 65 20 20 6d 61 67 69 63 20 73 74 72 69 6e 67 he magic string
11be5 20 66 6f 75 6e 64 20 61 74 20 74 68 65 20 73 74 found at the st
11be6 61 72 74 20 6f 66 20 65 61 63 68 20 6a 6f 75 72 art of each jour
11be7 6e 61 6c 20 68 65 61 64 65 72 2c 20 72 65 74 75 nal header, retu
11be8 72 6e 0a 20 20 2a 2a 20 53 51 4c 49 54 45 5f 44 rn. ** SQLITE_D
11be9 4f 4e 45 2e 20 49 66 20 61 6e 20 49 4f 20 65 72 ONE. If an IO er
11bea 72 6f 72 20 6f 63 63 75 72 73 2c 20 72 65 74 75 ror occurs, retu
11beb 72 6e 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 rn an error code
11bec 2e 20 4f 74 68 65 72 77 69 73 65 2c 0a 20 20 2a . Otherwise,. *
11bed 2a 20 70 72 6f 63 65 65 64 2e 0a 20 20 2a 2f 0a * proceed.. */.
11bee 20 20 69 66 28 20 69 73 48 6f 74 20 7c 7c 20 69 if( isHot || i
11bef 48 64 72 4f 66 66 21 3d 70 50 61 67 65 72 2d 3e HdrOff!=pPager->
11bf0 6a 6f 75 72 6e 61 6c 48 64 72 20 29 7b 0a 20 20 journalHdr ){.
11bf1 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
11bf2 52 65 61 64 28 70 50 61 67 65 72 2d 3e 6a 66 64 Read(pPager->jfd
11bf3 2c 20 61 4d 61 67 69 63 2c 20 73 69 7a 65 6f 66 , aMagic, sizeof
11bf4 28 61 4d 61 67 69 63 29 2c 20 69 48 64 72 4f 66 (aMagic), iHdrOf
11bf5 66 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 f);. if( rc )
11bf6 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 {. return r
11bf7 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 c;. }. if(
11bf8 20 6d 65 6d 63 6d 70 28 61 4d 61 67 69 63 2c 20 memcmp(aMagic,
11bf9 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 2c 20 73 aJournalMagic, s
11bfa 69 7a 65 6f 66 28 61 4d 61 67 69 63 29 29 21 3d izeof(aMagic))!=
11bfb 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 0 ){. retur
11bfc 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 n SQLITE_DONE;.
11bfd 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 }. }.. /* R
11bfe 65 61 64 20 74 68 65 20 66 69 72 73 74 20 74 68 ead the first th
11bff 72 65 65 20 33 32 2d 62 69 74 20 66 69 65 6c 64 ree 32-bit field
11c00 73 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c s of the journal
11c01 20 68 65 61 64 65 72 3a 20 54 68 65 20 6e 52 65 header: The nRe
11c02 63 0a 20 20 2a 2a 20 66 69 65 6c 64 2c 20 74 68 c. ** field, th
11c03 65 20 63 68 65 63 6b 73 75 6d 2d 69 6e 69 74 69 e checksum-initi
11c04 61 6c 69 7a 65 72 20 61 6e 64 20 74 68 65 20 64 alizer and the d
11c05 61 74 61 62 61 73 65 20 73 69 7a 65 20 61 74 20 atabase size at
11c06 74 68 65 20 73 74 61 72 74 0a 20 20 2a 2a 20 6f the start. ** o
11c07 66 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f f the transactio
11c08 6e 2e 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72 n. Return an err
11c09 6f 72 20 63 6f 64 65 20 69 66 20 61 6e 79 74 68 or code if anyth
11c0a 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 2e 0a ing goes wrong..
11c0b 20 20 2a 2f 0a 20 20 69 66 28 20 53 51 4c 49 54 */. if( SQLIT
11c0c 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65 61 64 E_OK!=(rc = read
11c0d 33 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 32bits(pPager->j
11c0e 66 64 2c 20 69 48 64 72 4f 66 66 2b 38 2c 20 70 fd, iHdrOff+8, p
11c0f 4e 52 65 63 29 29 0a 20 20 20 7c 7c 20 53 51 4c NRec)). || SQL
11c10 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65 ITE_OK!=(rc = re
11c11 61 64 33 32 62 69 74 73 28 70 50 61 67 65 72 2d ad32bits(pPager-
11c12 3e 6a 66 64 2c 20 69 48 64 72 4f 66 66 2b 31 32 >jfd, iHdrOff+12
11c13 2c 20 26 70 50 61 67 65 72 2d 3e 63 6b 73 75 6d , &pPager->cksum
11c14 49 6e 69 74 29 29 0a 20 20 20 7c 7c 20 53 51 4c Init)). || SQL
11c15 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65 ITE_OK!=(rc = re
11c16 61 64 33 32 62 69 74 73 28 70 50 61 67 65 72 2d ad32bits(pPager-
11c17 3e 6a 66 64 2c 20 69 48 64 72 4f 66 66 2b 31 36 >jfd, iHdrOff+16
11c18 2c 20 70 44 62 53 69 7a 65 29 29 0a 20 20 29 7b , pDbSize)). ){
11c19 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a . return rc;.
11c1a 20 20 7d 0a 0a 20 20 69 66 28 20 70 50 61 67 65 }.. if( pPage
11c1b 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d 30 r->journalOff==0
11c1c 20 29 7b 0a 20 20 20 20 75 33 32 20 69 50 61 67 ){. u32 iPag
11c1d 65 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 eSize;
11c1e 20 20 20 20 20 2f 2a 20 50 61 67 65 2d 73 69 7a /* Page-siz
11c1f 65 20 66 69 65 6c 64 20 6f 66 20 6a 6f 75 72 6e e field of journ
11c20 61 6c 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 20 al header */.
11c21 20 75 33 32 20 69 53 65 63 74 6f 72 53 69 7a 65 u32 iSectorSize
11c22 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
11c23 20 53 65 63 74 6f 72 2d 73 69 7a 65 20 66 69 65 Sector-size fie
11c24 6c 64 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 68 65 ld of journal he
11c25 61 64 65 72 20 2a 2f 0a 20 20 20 20 75 31 36 20 ader */. u16
11c26 69 50 61 67 65 53 69 7a 65 31 36 3b 20 20 20 20 iPageSize16;
11c27 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 /* Copy
11c28 20 6f 66 20 69 50 61 67 65 53 69 7a 65 20 69 6e of iPageSize in
11c29 20 31 36 2d 62 69 74 20 76 61 72 69 61 62 6c 65 16-bit variable
11c2a 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 52 65 61 64 */.. /* Read
11c2b 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65 20 61 the page-size a
11c2c 6e 64 20 73 65 63 74 6f 72 2d 73 69 7a 65 20 6a nd sector-size j
11c2d 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 66 69 ournal header fi
11c2e 65 6c 64 73 2e 20 2a 2f 0a 20 20 20 20 69 66 28 elds. */. if(
11c2f 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 SQLITE_OK!=(rc
11c30 3d 20 72 65 61 64 33 32 62 69 74 73 28 70 50 61 = read32bits(pPa
11c31 67 65 72 2d 3e 6a 66 64 2c 20 69 48 64 72 4f 66 ger->jfd, iHdrOf
11c32 66 2b 32 30 2c 20 26 69 53 65 63 74 6f 72 53 69 f+20, &iSectorSi
11c33 7a 65 29 29 0a 20 20 20 20 20 7c 7c 20 53 51 4c ze)). || SQL
11c34 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65 ITE_OK!=(rc = re
11c35 61 64 33 32 62 69 74 73 28 70 50 61 67 65 72 2d ad32bits(pPager-
11c36 3e 6a 66 64 2c 20 69 48 64 72 4f 66 66 2b 32 34 >jfd, iHdrOff+24
11c37 2c 20 26 69 50 61 67 65 53 69 7a 65 29 29 0a 20 , &iPageSize)).
11c38 20 20 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 ){. retu
11c39 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 rn rc;. }..
11c3a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61 74 20 /* Check that
11c3b 74 68 65 20 76 61 6c 75 65 73 20 72 65 61 64 20 the values read
11c3c 66 72 6f 6d 20 74 68 65 20 70 61 67 65 2d 73 69 from the page-si
11c3d 7a 65 20 61 6e 64 20 73 65 63 74 6f 72 2d 73 69 ze and sector-si
11c3e 7a 65 20 66 69 65 6c 64 73 0a 20 20 20 20 2a 2a ze fields. **
11c3f 20 61 72 65 20 77 69 74 68 69 6e 20 72 61 6e 67 are within rang
11c40 65 2e 20 54 6f 20 62 65 20 27 69 6e 20 72 61 6e e. To be 'in ran
11c41 67 65 27 2c 20 62 6f 74 68 20 76 61 6c 75 65 73 ge', both values
11c42 20 6e 65 65 64 20 74 6f 20 62 65 20 61 20 70 6f need to be a po
11c43 77 65 72 0a 20 20 20 20 2a 2a 20 6f 66 20 74 77 wer. ** of tw
11c44 6f 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f o greater than o
11c45 72 20 65 71 75 61 6c 20 74 6f 20 35 31 32 2c 20 r equal to 512,
11c46 61 6e 64 20 6e 6f 74 20 67 72 65 61 74 65 72 20 and not greater
11c47 74 68 61 6e 20 74 68 65 69 72 20 0a 20 20 20 20 than their .
11c48 2a 2a 20 72 65 73 70 65 63 74 69 76 65 20 63 6f ** respective co
11c49 6d 70 69 6c 65 20 74 69 6d 65 20 6d 61 78 69 6d mpile time maxim
11c4a 75 6d 20 6c 69 6d 69 74 73 2e 0a 20 20 20 20 2a um limits.. *
11c4b 2f 0a 20 20 20 20 69 66 28 20 69 50 61 67 65 53 /. if( iPageS
11c4c 69 7a 65 3c 35 31 32 20 20 20 20 20 20 20 20 20 ize<512
11c4d 20 20 20 20 20 20 20 20 20 7c 7c 20 69 53 65 63 || iSec
11c4e 74 6f 72 53 69 7a 65 3c 35 31 32 0a 20 20 20 20 torSize<512.
11c4f 20 7c 7c 20 69 50 61 67 65 53 69 7a 65 3e 53 51 || iPageSize>SQ
11c50 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 LITE_MAX_PAGE_SI
11c51 5a 45 20 7c 7c 20 69 53 65 63 74 6f 72 53 69 7a ZE || iSectorSiz
11c52 65 3e 4d 41 58 5f 53 45 43 54 4f 52 5f 53 49 5a e>MAX_SECTOR_SIZ
11c53 45 0a 20 20 20 20 20 7c 7c 20 28 28 69 50 61 67 E. || ((iPag
11c54 65 53 69 7a 65 2d 31 29 26 69 50 61 67 65 53 69 eSize-1)&iPageSi
11c55 7a 65 29 21 3d 30 20 20 20 7c 7c 20 28 28 69 53 ze)!=0 || ((iS
11c56 65 63 74 6f 72 53 69 7a 65 2d 31 29 26 69 53 65 ectorSize-1)&iSe
11c57 63 74 6f 72 53 69 7a 65 29 21 3d 30 20 0a 20 20 ctorSize)!=0 .
11c58 20 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 ){. /* If
11c59 20 74 68 65 20 65 69 74 68 65 72 20 74 68 65 20 the either the
11c5a 70 61 67 65 2d 73 69 7a 65 20 6f 72 20 73 65 63 page-size or sec
11c5b 74 6f 72 2d 73 69 7a 65 20 69 6e 20 74 68 65 20 tor-size in the
11c5c 6a 6f 75 72 6e 61 6c 2d 68 65 61 64 65 72 20 69 journal-header i
11c5d 73 20 0a 20 20 20 20 20 20 2a 2a 20 69 6e 76 61 s . ** inva
11c5e 6c 69 64 2c 20 74 68 65 6e 20 74 68 65 20 70 72 lid, then the pr
11c5f 6f 63 65 73 73 20 74 68 61 74 20 77 72 6f 74 65 ocess that wrote
11c60 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 68 65 61 the journal-hea
11c61 64 65 72 20 6d 75 73 74 20 68 61 76 65 20 0a 20 der must have .
11c62 20 20 20 20 20 2a 2a 20 63 72 61 73 68 65 64 20 ** crashed
11c63 62 65 66 6f 72 65 20 74 68 65 20 68 65 61 64 65 before the heade
11c64 72 20 77 61 73 20 73 79 6e 63 65 64 2e 20 49 6e r was synced. In
11c65 20 74 68 69 73 20 63 61 73 65 20 73 74 6f 70 20 this case stop
11c66 72 65 61 64 69 6e 67 20 0a 20 20 20 20 20 20 2a reading . *
11c67 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 * the journal fi
11c68 6c 65 20 68 65 72 65 2e 0a 20 20 20 20 20 20 2a le here.. *
11c69 2f 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 /. return S
11c6a 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 QLITE_DONE;.
11c6b 7d 0a 0a 20 20 20 20 2f 2a 20 55 70 64 61 74 65 }.. /* Update
11c6c 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65 20 74 the page-size t
11c6d 6f 20 6d 61 74 63 68 20 74 68 65 20 76 61 6c 75 o match the valu
11c6e 65 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 e read from the
11c6f 6a 6f 75 72 6e 61 6c 2e 20 0a 20 20 20 20 2a 2a journal. . **
11c70 20 55 73 65 20 61 20 74 65 73 74 63 61 73 65 28 Use a testcase(
11c71 29 20 6d 61 63 72 6f 20 74 6f 20 6d 61 6b 65 20 ) macro to make
11c72 73 75 72 65 20 74 68 61 74 20 6d 61 6c 6c 6f 63 sure that malloc
11c73 20 66 61 69 6c 75 72 65 20 77 69 74 68 69 6e 20 failure within
11c74 0a 20 20 20 20 2a 2a 20 50 61 67 65 72 53 65 74 . ** PagerSet
11c75 50 61 67 65 73 69 7a 65 28 29 20 69 73 20 74 65 Pagesize() is te
11c76 73 74 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 sted.. */.
11c77 20 69 50 61 67 65 53 69 7a 65 31 36 20 3d 20 28 iPageSize16 = (
11c78 75 31 36 29 69 50 61 67 65 53 69 7a 65 3b 0a 20 u16)iPageSize;.
11c79 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 rc = sqlite3P
11c7a 61 67 65 72 53 65 74 50 61 67 65 73 69 7a 65 28 agerSetPagesize(
11c7b 70 50 61 67 65 72 2c 20 26 69 50 61 67 65 53 69 pPager, &iPageSi
11c7c 7a 65 31 36 2c 20 2d 31 29 3b 0a 20 20 20 20 74 ze16, -1);. t
11c7d 65 73 74 63 61 73 65 28 20 72 63 21 3d 53 51 4c estcase( rc!=SQL
11c7e 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 61 73 ITE_OK );. as
11c7f 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 sert( rc!=SQLITE
11c80 5f 4f 4b 20 7c 7c 20 69 50 61 67 65 53 69 7a 65 _OK || iPageSize
11c81 31 36 3d 3d 28 75 31 36 29 69 50 61 67 65 53 69 16==(u16)iPageSi
11c82 7a 65 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 55 70 ze );.. /* Up
11c83 64 61 74 65 20 74 68 65 20 61 73 73 75 6d 65 64 date the assumed
11c84 20 73 65 63 74 6f 72 2d 73 69 7a 65 20 74 6f 20 sector-size to
11c85 6d 61 74 63 68 20 74 68 65 20 76 61 6c 75 65 20 match the value
11c86 75 73 65 64 20 62 79 20 0a 20 20 20 20 2a 2a 20 used by . **
11c87 74 68 65 20 70 72 6f 63 65 73 73 20 74 68 61 74 the process that
11c88 20 63 72 65 61 74 65 64 20 74 68 69 73 20 6a 6f created this jo
11c89 75 72 6e 61 6c 2e 20 49 66 20 74 68 69 73 20 6a urnal. If this j
11c8a 6f 75 72 6e 61 6c 20 77 61 73 0a 20 20 20 20 2a ournal was. *
11c8b 2a 20 63 72 65 61 74 65 64 20 62 79 20 61 20 70 * created by a p
11c8c 72 6f 63 65 73 73 20 6f 74 68 65 72 20 74 68 61 rocess other tha
11c8d 6e 20 74 68 69 73 20 6f 6e 65 2c 20 74 68 65 6e n this one, then
11c8e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 20 20 this routine.
11c8f 20 20 2a 2a 20 69 73 20 62 65 69 6e 67 20 63 61 ** is being ca
11c90 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e lled from within
11c91 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 pager_playback(
11c92 29 2e 20 54 68 65 20 6c 6f 63 61 6c 20 76 61 6c ). The local val
11c93 75 65 0a 20 20 20 20 2a 2a 20 6f 66 20 50 61 67 ue. ** of Pag
11c94 65 72 2e 73 65 63 74 6f 72 53 69 7a 65 20 69 73 er.sectorSize is
11c95 20 72 65 73 74 6f 72 65 64 20 61 74 20 74 68 65 restored at the
11c96 20 65 6e 64 20 6f 66 20 74 68 61 74 20 72 6f 75 end of that rou
11c97 74 69 6e 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 tine.. */.
11c98 20 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 pPager->sectorS
11c99 69 7a 65 20 3d 20 69 53 65 63 74 6f 72 53 69 7a ize = iSectorSiz
11c9a 65 3b 0a 20 20 7d 0a 0a 20 20 70 50 61 67 65 72 e;. }.. pPager
11c9b 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b 3d 20 ->journalOff +=
11c9c 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 JOURNAL_HDR_SZ(p
11c9d 50 61 67 65 72 29 3b 0a 20 20 72 65 74 75 72 6e Pager);. return
11c9e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 57 rc;.}.../*.** W
11c9f 72 69 74 65 20 74 68 65 20 73 75 70 70 6c 69 65 rite the supplie
11ca0 64 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c d master journal
11ca1 20 6e 61 6d 65 20 69 6e 74 6f 20 74 68 65 20 6a name into the j
11ca2 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 20 ournal file for
11ca3 70 61 67 65 72 0a 2a 2a 20 70 50 61 67 65 72 20 pager.** pPager
11ca4 61 74 20 74 68 65 20 63 75 72 72 65 6e 74 20 6c at the current l
11ca5 6f 63 61 74 69 6f 6e 2e 20 54 68 65 20 6d 61 73 ocation. The mas
11ca6 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 ter journal name
11ca7 20 6d 75 73 74 20 62 65 20 74 68 65 20 6c 61 73 must be the las
11ca8 74 0a 2a 2a 20 74 68 69 6e 67 20 77 72 69 74 74 t.** thing writt
11ca9 65 6e 20 74 6f 20 61 20 6a 6f 75 72 6e 61 6c 20 en to a journal
11caa 66 69 6c 65 2e 20 49 66 20 74 68 65 20 70 61 67 file. If the pag
11cab 65 72 20 69 73 20 69 6e 20 66 75 6c 6c 2d 73 79 er is in full-sy
11cac 6e 63 20 6d 6f 64 65 2c 20 74 68 65 0a 2a 2a 20 nc mode, the.**
11cad 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 64 65 73 journal file des
11cae 63 72 69 70 74 6f 72 20 69 73 20 61 64 76 61 6e criptor is advan
11caf 63 65 64 20 74 6f 20 74 68 65 20 6e 65 78 74 20 ced to the next
11cb0 73 65 63 74 6f 72 20 62 6f 75 6e 64 61 72 79 20 sector boundary
11cb1 62 65 66 6f 72 65 0a 2a 2a 20 61 6e 79 74 68 69 before.** anythi
11cb2 6e 67 20 69 73 20 77 72 69 74 74 65 6e 2e 20 54 ng is written. T
11cb3 68 65 20 66 6f 72 6d 61 74 20 69 73 3a 0a 2a 2a he format is:.**
11cb4 0a 2a 2a 20 20 20 2b 20 34 20 62 79 74 65 73 3a .** + 4 bytes:
11cb5 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 2e 0a PAGER_MJ_PGNO..
11cb6 2a 2a 20 20 20 2b 20 4e 20 62 79 74 65 73 3a 20 ** + N bytes:
11cb7 4d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 Master journal f
11cb8 69 6c 65 6e 61 6d 65 20 69 6e 20 75 74 66 2d 38 ilename in utf-8
11cb9 2e 0a 2a 2a 20 20 20 2b 20 34 20 62 79 74 65 73 ..** + 4 bytes
11cba 3a 20 4e 20 28 6c 65 6e 67 74 68 20 6f 66 20 6d : N (length of m
11cbb 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 aster journal na
11cbc 6d 65 20 69 6e 20 62 79 74 65 73 2c 20 6e 6f 20 me in bytes, no
11cbd 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 6f 72 29 2e nul-terminator).
11cbe 0a 2a 2a 20 20 20 2b 20 34 20 62 79 74 65 73 3a .** + 4 bytes:
11cbf 20 4d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 Master journal
11cc0 6e 61 6d 65 20 63 68 65 63 6b 73 75 6d 2e 0a 2a name checksum..*
11cc1 2a 20 20 20 2b 20 38 20 62 79 74 65 73 3a 20 61 * + 8 bytes: a
11cc2 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 5b 5d 2e 0a JournalMagic[]..
11cc3 2a 2a 0a 2a 2a 20 54 68 65 20 6d 61 73 74 65 72 **.** The master
11cc4 20 6a 6f 75 72 6e 61 6c 20 70 61 67 65 20 63 68 journal page ch
11cc5 65 63 6b 73 75 6d 20 69 73 20 74 68 65 20 73 75 ecksum is the su
11cc6 6d 20 6f 66 20 74 68 65 20 62 79 74 65 73 20 69 m of the bytes i
11cc7 6e 20 74 68 65 20 6d 61 73 74 65 72 0a 2a 2a 20 n the master.**
11cc8 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 2c 20 77 68 journal name, wh
11cc9 65 72 65 20 65 61 63 68 20 62 79 74 65 20 69 73 ere each byte is
11cca 20 69 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 interpreted as
11ccb 61 20 73 69 67 6e 65 64 20 38 2d 62 69 74 20 69 a signed 8-bit i
11ccc 6e 74 65 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 nteger..**.** If
11ccd 20 7a 4d 61 73 74 65 72 20 69 73 20 61 20 4e 55 zMaster is a NU
11cce 4c 4c 20 70 6f 69 6e 74 65 72 20 28 6f 63 63 75 LL pointer (occu
11ccf 72 73 20 66 6f 72 20 61 20 73 69 6e 67 6c 65 20 rs for a single
11cd0 64 61 74 61 62 61 73 65 20 74 72 61 6e 73 61 63 database transac
11cd1 74 69 6f 6e 29 2c 20 0a 2a 2a 20 74 68 69 73 20 tion), .** this
11cd2 63 61 6c 6c 20 69 73 20 61 20 6e 6f 2d 6f 70 2e call is a no-op.
11cd3 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 .*/.static int w
11cd4 72 69 74 65 4d 61 73 74 65 72 4a 6f 75 72 6e 61 riteMasterJourna
11cd5 6c 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c l(Pager *pPager,
11cd6 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 61 const char *zMa
11cd7 73 74 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b ster){. int rc;
11cd8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11cd9 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 /* Ret
11cda 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 69 6e urn code */. in
11cdb 74 20 6e 4d 61 73 74 65 72 3b 20 20 20 20 20 20 t nMaster;
11cdc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
11cdd 2a 20 4c 65 6e 67 74 68 20 6f 66 20 73 74 72 69 * Length of stri
11cde 6e 67 20 7a 4d 61 73 74 65 72 20 2a 2f 0a 20 20 ng zMaster */.
11cdf 69 36 34 20 69 48 64 72 4f 66 66 3b 20 20 20 20 i64 iHdrOff;
11ce0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11ce1 20 2f 2a 20 4f 66 66 73 65 74 20 6f 66 20 68 65 /* Offset of he
11ce2 61 64 65 72 20 69 6e 20 6a 6f 75 72 6e 61 6c 20 ader in journal
11ce3 66 69 6c 65 20 2a 2f 0a 20 20 69 36 34 20 6a 72 file */. i64 jr
11ce4 6e 6c 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 nlSize;
11ce5 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 /* Si
11ce6 7a 65 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 66 69 ze of journal fi
11ce7 6c 65 20 6f 6e 20 64 69 73 6b 20 2a 2f 0a 20 20 le on disk */.
11ce8 75 33 32 20 63 6b 73 75 6d 20 3d 20 30 3b 20 20 u32 cksum = 0;
11ce9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11cea 20 2f 2a 20 43 68 65 63 6b 73 75 6d 20 6f 66 20 /* Checksum of
11ceb 73 74 72 69 6e 67 20 7a 4d 61 73 74 65 72 20 2a string zMaster *
11cec 2f 0a 0a 20 20 69 66 28 20 21 7a 4d 61 73 74 65 /.. if( !zMaste
11ced 72 20 7c 7c 20 70 50 61 67 65 72 2d 3e 73 65 74 r || pPager->set
11cee 4d 61 73 74 65 72 0a 20 20 20 7c 7c 20 70 50 61 Master. || pPa
11cef 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 ger->journalMode
11cf0 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d ==PAGER_JOURNALM
11cf1 4f 44 45 5f 4d 45 4d 4f 52 59 20 0a 20 20 20 7c ODE_MEMORY . |
11cf2 7c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 | pPager->journa
11cf3 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 lMode==PAGER_JOU
11cf4 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 0a 20 20 RNALMODE_OFF .
11cf5 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 ){. return SQ
11cf6 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 70 LITE_OK;. }. p
11cf7 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 Pager->setMaster
11cf8 20 3d 20 31 3b 0a 20 20 61 73 73 65 72 74 28 20 = 1;. assert(
11cf9 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a isOpen(pPager->j
11cfa 66 64 29 20 29 3b 0a 0a 20 20 2f 2a 20 43 61 6c fd) );.. /* Cal
11cfb 63 75 6c 61 74 65 20 74 68 65 20 6c 65 6e 67 74 culate the lengt
11cfc 68 20 69 6e 20 62 79 74 65 73 20 61 6e 64 20 74 h in bytes and t
11cfd 68 65 20 63 68 65 63 6b 73 75 6d 20 6f 66 20 7a he checksum of z
11cfe 4d 61 73 74 65 72 20 2a 2f 0a 20 20 66 6f 72 28 Master */. for(
11cff 6e 4d 61 73 74 65 72 3d 30 3b 20 7a 4d 61 73 74 nMaster=0; zMast
11d00 65 72 5b 6e 4d 61 73 74 65 72 5d 3b 20 6e 4d 61 er[nMaster]; nMa
11d01 73 74 65 72 2b 2b 29 7b 0a 20 20 20 20 63 6b 73 ster++){. cks
11d02 75 6d 20 2b 3d 20 7a 4d 61 73 74 65 72 5b 6e 4d um += zMaster[nM
11d03 61 73 74 65 72 5d 3b 0a 20 20 7d 0a 0a 20 20 2f aster];. }.. /
11d04 2a 20 49 66 20 69 6e 20 66 75 6c 6c 2d 73 79 6e * If in full-syn
11d05 63 20 6d 6f 64 65 2c 20 61 64 76 61 6e 63 65 20 c mode, advance
11d06 74 6f 20 74 68 65 20 6e 65 78 74 20 64 69 73 6b to the next disk
11d07 20 73 65 63 74 6f 72 20 62 65 66 6f 72 65 20 77 sector before w
11d08 72 69 74 69 6e 67 0a 20 20 2a 2a 20 74 68 65 20 riting. ** the
11d09 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e master journal n
11d0a 61 6d 65 2e 20 54 68 69 73 20 69 73 20 69 6e 20 ame. This is in
11d0b 63 61 73 65 20 74 68 65 20 70 72 65 76 69 6f 75 case the previou
11d0c 73 20 70 61 67 65 20 77 72 69 74 74 65 6e 20 74 s page written t
11d0d 6f 0a 20 20 2a 2a 20 74 68 65 20 6a 6f 75 72 6e o. ** the journ
11d0e 61 6c 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 al has already b
11d0f 65 65 6e 20 73 79 6e 63 65 64 2e 0a 20 20 2a 2f een synced.. */
11d10 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 66 . if( pPager->f
11d11 75 6c 6c 53 79 6e 63 20 29 7b 0a 20 20 20 20 70 ullSync ){. p
11d12 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 Pager->journalOf
11d13 66 20 3d 20 6a 6f 75 72 6e 61 6c 48 64 72 4f 66 f = journalHdrOf
11d14 66 73 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20 fset(pPager);.
11d15 7d 0a 20 20 69 48 64 72 4f 66 66 20 3d 20 70 50 }. iHdrOff = pP
11d16 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 ager->journalOff
11d17 3b 0a 0a 20 20 2f 2a 20 57 72 69 74 65 20 74 68 ;.. /* Write th
11d18 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c e master journal
11d19 20 64 61 74 61 20 74 6f 20 74 68 65 20 65 6e 64 data to the end
11d1a 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 of the journal
11d1b 66 69 6c 65 2e 20 49 66 0a 20 20 2a 2a 20 61 6e file. If. ** an
11d1c 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 72 error occurs, r
11d1d 65 74 75 72 6e 20 74 68 65 20 65 72 72 6f 72 20 eturn the error
11d1e 63 6f 64 65 20 74 6f 20 74 68 65 20 63 61 6c 6c code to the call
11d1f 65 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 er.. */. if( (
11d20 30 20 21 3d 20 28 72 63 20 3d 20 77 72 69 74 65 0 != (rc = write
11d21 33 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 32bits(pPager->j
11d22 66 64 2c 20 69 48 64 72 4f 66 66 2c 20 50 41 47 fd, iHdrOff, PAG
11d23 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 65 ER_MJ_PGNO(pPage
11d24 72 29 29 29 29 0a 20 20 20 7c 7c 20 28 30 20 21 r)))). || (0 !
11d25 3d 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 4f = (rc = sqlite3O
11d26 73 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e 6a sWrite(pPager->j
11d27 66 64 2c 20 7a 4d 61 73 74 65 72 2c 20 6e 4d 61 fd, zMaster, nMa
11d28 73 74 65 72 2c 20 69 48 64 72 4f 66 66 2b 34 29 ster, iHdrOff+4)
11d29 29 29 0a 20 20 20 7c 7c 20 28 30 20 21 3d 20 28 )). || (0 != (
11d2a 72 63 20 3d 20 77 72 69 74 65 33 32 62 69 74 73 rc = write32bits
11d2b 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69 48 (pPager->jfd, iH
11d2c 64 72 4f 66 66 2b 34 2b 6e 4d 61 73 74 65 72 2c drOff+4+nMaster,
11d2d 20 6e 4d 61 73 74 65 72 29 29 29 0a 20 20 20 7c nMaster))). |
11d2e 7c 20 28 30 20 21 3d 20 28 72 63 20 3d 20 77 72 | (0 != (rc = wr
11d2f 69 74 65 33 32 62 69 74 73 28 70 50 61 67 65 72 ite32bits(pPager
11d30 2d 3e 6a 66 64 2c 20 69 48 64 72 4f 66 66 2b 34 ->jfd, iHdrOff+4
11d31 2b 6e 4d 61 73 74 65 72 2b 34 2c 20 63 6b 73 75 +nMaster+4, cksu
11d32 6d 29 29 29 0a 20 20 20 7c 7c 20 28 30 20 21 3d m))). || (0 !=
11d33 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 (rc = sqlite3Os
11d34 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 Write(pPager->jf
11d35 64 2c 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 d, aJournalMagic
11d36 2c 20 38 2c 20 69 48 64 72 4f 66 66 2b 34 2b 6e , 8, iHdrOff+4+n
11d37 4d 61 73 74 65 72 2b 38 29 29 29 0a 20 20 29 7b Master+8))). ){
11d38 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a . return rc;.
11d39 20 20 7d 0a 20 20 70 50 61 67 65 72 2d 3e 6a 6f }. pPager->jo
11d3a 75 72 6e 61 6c 4f 66 66 20 2b 3d 20 28 6e 4d 61 urnalOff += (nMa
11d3b 73 74 65 72 2b 32 30 29 3b 0a 20 20 70 50 61 67 ster+20);. pPag
11d3c 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 21 er->needSync = !
11d3d 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 3b 0a pPager->noSync;.
11d3e 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 70 61 67 . /* If the pag
11d3f 65 72 20 69 73 20 69 6e 20 70 65 72 69 73 74 65 er is in periste
11d40 6e 74 2d 6a 6f 75 72 6e 61 6c 20 6d 6f 64 65 2c nt-journal mode,
11d41 20 74 68 65 6e 20 74 68 65 20 70 68 79 73 69 63 then the physic
11d42 61 6c 20 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c al . ** journal
11d43 2d 66 69 6c 65 20 6d 61 79 20 65 78 74 65 6e 64 -file may extend
11d44 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 past the end of
11d45 20 74 68 65 20 6d 61 73 74 65 72 2d 6a 6f 75 72 the master-jour
11d46 6e 61 6c 20 6e 61 6d 65 0a 20 20 2a 2a 20 61 6e nal name. ** an
11d47 64 20 38 20 62 79 74 65 73 20 6f 66 20 6d 61 67 d 8 bytes of mag
11d48 69 63 20 64 61 74 61 20 6a 75 73 74 20 77 72 69 ic data just wri
11d49 74 74 65 6e 20 74 6f 20 74 68 65 20 66 69 6c 65 tten to the file
11d4a 2e 20 54 68 69 73 20 69 73 20 0a 20 20 2a 2a 20 . This is . **
11d4b 64 61 6e 67 65 72 6f 75 73 20 62 65 63 61 75 73 dangerous becaus
11d4c 65 20 74 68 65 20 63 6f 64 65 20 74 6f 20 72 6f e the code to ro
11d4d 6c 6c 62 61 63 6b 20 61 20 68 6f 74 2d 6a 6f 75 llback a hot-jou
11d4e 72 6e 61 6c 20 66 69 6c 65 0a 20 20 2a 2a 20 77 rnal file. ** w
11d4f 69 6c 6c 20 6e 6f 74 20 62 65 20 61 62 6c 65 20 ill not be able
11d50 74 6f 20 66 69 6e 64 20 74 68 65 20 6d 61 73 74 to find the mast
11d51 65 72 2d 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 er-journal name
11d52 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 0a 20 20 to determine .
11d53 2a 2a 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f ** whether or no
11d54 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 t the journal is
11d55 20 68 6f 74 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a hot. . **. **
11d56 20 45 61 73 69 65 73 74 20 74 68 69 6e 67 20 74 Easiest thing t
11d57 6f 20 64 6f 20 69 6e 20 74 68 69 73 20 73 63 65 o do in this sce
11d58 6e 61 72 69 6f 20 69 73 20 74 6f 20 74 72 75 6e nario is to trun
11d59 63 61 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c cate the journal
11d5a 20 0a 20 20 2a 2a 20 66 69 6c 65 20 74 6f 20 74 . ** file to t
11d5b 68 65 20 72 65 71 75 69 72 65 64 20 73 69 7a 65 he required size
11d5c 2e 0a 20 20 2a 2f 20 0a 20 20 69 66 28 20 53 51 .. */ . if( SQ
11d5d 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 LITE_OK==(rc = s
11d5e 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 qlite3OsFileSize
11d5f 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 26 6a (pPager->jfd, &j
11d60 72 6e 6c 53 69 7a 65 29 29 0a 20 20 20 26 26 20 rnlSize)). &&
11d61 6a 72 6e 6c 53 69 7a 65 3e 70 50 61 67 65 72 2d jrnlSize>pPager-
11d62 3e 6a 6f 75 72 6e 61 6c 4f 66 66 0a 20 20 29 7b >journalOff. ){
11d63 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
11d64 33 4f 73 54 72 75 6e 63 61 74 65 28 70 50 61 67 3OsTruncate(pPag
11d65 65 72 2d 3e 6a 66 64 2c 20 70 50 61 67 65 72 2d er->jfd, pPager-
11d66 3e 6a 6f 75 72 6e 61 6c 4f 66 66 29 3b 0a 20 20 >journalOff);.
11d67 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
11d68 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 61 20 70 ../*.** Find a p
11d69 61 67 65 20 69 6e 20 74 68 65 20 68 61 73 68 20 age in the hash
11d6a 74 61 62 6c 65 20 67 69 76 65 6e 20 69 74 73 20 table given its
11d6b 70 61 67 65 20 6e 75 6d 62 65 72 2e 20 52 65 74 page number. Ret
11d6c 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 urn.** a pointer
11d6d 20 74 6f 20 74 68 65 20 70 61 67 65 20 6f 72 20 to the page or
11d6e 4e 55 4c 4c 20 69 66 20 74 68 65 20 72 65 71 75 NULL if the requ
11d6f 65 73 74 65 64 20 70 61 67 65 20 69 73 20 6e 6f ested page is no
11d70 74 20 0a 2a 2a 20 61 6c 72 65 61 64 79 20 69 6e t .** already in
11d71 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 73 74 61 74 memory..*/.stat
11d72 69 63 20 50 67 48 64 72 20 2a 70 61 67 65 72 5f ic PgHdr *pager_
11d73 6c 6f 6f 6b 75 70 28 50 61 67 65 72 20 2a 70 50 lookup(Pager *pP
11d74 61 67 65 72 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 ager, Pgno pgno)
11d75 7b 0a 20 20 50 67 48 64 72 20 2a 70 3b 20 20 20 {. PgHdr *p;
11d76 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11d77 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 /* Return
11d78 76 61 6c 75 65 20 2a 2f 0a 0a 20 20 2f 2a 20 49 value */.. /* I
11d79 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c t is not possibl
11d7a 65 20 66 6f 72 20 61 20 63 61 6c 6c 20 74 6f 20 e for a call to
11d7b 50 63 61 63 68 65 46 65 74 63 68 28 29 20 77 69 PcacheFetch() wi
11d7c 74 68 20 63 72 65 61 74 65 46 6c 61 67 3d 3d 30 th createFlag==0
11d7d 20 74 6f 0a 20 20 2a 2a 20 66 61 69 6c 2c 20 73 to. ** fail, s
11d7e 69 6e 63 65 20 6e 6f 20 61 74 74 65 6d 70 74 20 ince no attempt
11d7f 74 6f 20 61 6c 6c 6f 63 61 74 65 20 64 79 6e 61 to allocate dyna
11d80 6d 69 63 20 6d 65 6d 6f 72 79 20 77 69 6c 6c 20 mic memory will
11d81 62 65 20 6d 61 64 65 2e 0a 20 20 2a 2f 0a 20 20 be made.. */.
11d82 28 76 6f 69 64 29 73 71 6c 69 74 65 33 50 63 61 (void)sqlite3Pca
11d83 63 68 65 46 65 74 63 68 28 70 50 61 67 65 72 2d cheFetch(pPager-
11d84 3e 70 50 43 61 63 68 65 2c 20 70 67 6e 6f 2c 20 >pPCache, pgno,
11d85 30 2c 20 26 70 29 3b 0a 20 20 72 65 74 75 72 6e 0, &p);. return
11d86 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c p;.}../*.** Unl
11d87 65 73 73 20 74 68 65 20 70 61 67 65 72 20 69 73 ess the pager is
11d88 20 69 6e 20 65 72 72 6f 72 2d 73 74 61 74 65 2c in error-state,
11d89 20 64 69 73 63 61 72 64 20 61 6c 6c 20 69 6e 2d discard all in-
11d8a 6d 65 6d 6f 72 79 20 70 61 67 65 73 2e 20 49 66 memory pages. If
11d8b 0a 2a 2a 20 74 68 65 20 70 61 67 65 72 20 69 73 .** the pager is
11d8c 20 69 6e 20 65 72 72 6f 72 2d 73 74 61 74 65 2c in error-state,
11d8d 20 74 68 65 6e 20 74 68 69 73 20 63 61 6c 6c 20 then this call
11d8e 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a is a no-op..**.*
11d8f 2a 20 54 4f 44 4f 3a 20 57 68 79 20 63 61 6e 20 * TODO: Why can
11d90 77 65 20 6e 6f 74 20 72 65 73 65 74 20 74 68 65 we not reset the
11d91 20 70 61 67 65 72 20 77 68 69 6c 65 20 69 6e 20 pager while in
11d92 65 72 72 6f 72 20 73 74 61 74 65 3f 0a 2a 2f 0a error state?.*/.
11d93 73 74 61 74 69 63 20 76 6f 69 64 20 70 61 67 65 static void page
11d94 72 5f 72 65 73 65 74 28 50 61 67 65 72 20 2a 70 r_reset(Pager *p
11d95 50 61 67 65 72 29 7b 0a 20 20 69 66 28 20 53 51 Pager){. if( SQ
11d96 4c 49 54 45 5f 4f 4b 3d 3d 70 50 61 67 65 72 2d LITE_OK==pPager-
11d97 3e 65 72 72 43 6f 64 65 20 29 7b 0a 20 20 20 20 >errCode ){.
11d98 73 71 6c 69 74 65 33 42 61 63 6b 75 70 52 65 73 sqlite3BackupRes
11d99 74 61 72 74 28 70 50 61 67 65 72 2d 3e 70 42 61 tart(pPager->pBa
11d9a 63 6b 75 70 29 3b 0a 20 20 20 20 73 71 6c 69 74 ckup);. sqlit
11d9b 65 33 50 63 61 63 68 65 43 6c 65 61 72 28 70 50 e3PcacheClear(pP
11d9c 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a ager->pPCache);.
11d9d 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 pPager->dbSi
11d9e 7a 65 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 7d zeValid = 0;. }
11d9f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 .}../*.** Free a
11da0 6c 6c 20 73 74 72 75 63 74 75 72 65 73 20 69 6e ll structures in
11da1 20 74 68 65 20 50 61 67 65 72 2e 61 53 61 76 65 the Pager.aSave
11da2 70 6f 69 6e 74 5b 5d 20 61 72 72 61 79 20 61 6e point[] array an
11da3 64 20 73 65 74 20 62 6f 74 68 0a 2a 2a 20 50 61 d set both.** Pa
11da4 67 65 72 2e 61 53 61 76 65 70 6f 69 6e 74 20 61 ger.aSavepoint a
11da5 6e 64 20 50 61 67 65 72 2e 6e 53 61 76 65 70 6f nd Pager.nSavepo
11da6 69 6e 74 20 74 6f 20 7a 65 72 6f 2e 20 43 6c 6f int to zero. Clo
11da7 73 65 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e se the sub-journ
11da8 61 6c 0a 2a 2a 20 69 66 20 69 74 20 69 73 20 6f al.** if it is o
11da9 70 65 6e 20 61 6e 64 20 74 68 65 20 70 61 67 65 pen and the page
11daa 72 20 69 73 20 6e 6f 74 20 69 6e 20 65 78 63 6c r is not in excl
11dab 75 73 69 76 65 20 6d 6f 64 65 2e 0a 2a 2f 0a 73 usive mode..*/.s
11dac 74 61 74 69 63 20 76 6f 69 64 20 72 65 6c 65 61 tatic void relea
11dad 73 65 41 6c 6c 53 61 76 65 70 6f 69 6e 74 73 28 seAllSavepoints(
11dae 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a Pager *pPager){.
11daf 20 20 69 6e 74 20 69 69 3b 20 20 20 20 20 20 20 int ii;
11db0 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61 /* Itera
11db1 74 6f 72 20 66 6f 72 20 6c 6f 6f 70 69 6e 67 20 tor for looping
11db2 74 68 72 6f 75 67 68 20 50 61 67 65 72 2e 61 53 through Pager.aS
11db3 61 76 65 70 6f 69 6e 74 20 2a 2f 0a 20 20 66 6f avepoint */. fo
11db4 72 28 69 69 3d 30 3b 20 69 69 3c 70 50 61 67 65 r(ii=0; ii<pPage
11db5 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3b 20 69 r->nSavepoint; i
11db6 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 i++){. sqlite
11db7 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 28 70 3BitvecDestroy(p
11db8 50 61 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e Pager->aSavepoin
11db9 74 5b 69 69 5d 2e 70 49 6e 53 61 76 65 70 6f 69 t[ii].pInSavepoi
11dba 6e 74 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 nt);. }. if( !
11dbb 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 pPager->exclusiv
11dbc 65 4d 6f 64 65 20 7c 7c 20 73 71 6c 69 74 65 33 eMode || sqlite3
11dbd 49 73 4d 65 6d 4a 6f 75 72 6e 61 6c 28 70 50 61 IsMemJournal(pPa
11dbe 67 65 72 2d 3e 73 6a 66 64 29 20 29 7b 0a 20 20 ger->sjfd) ){.
11dbf 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 sqlite3OsClose
11dc0 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 29 3b 0a (pPager->sjfd);.
11dc1 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 }. sqlite3_fr
11dc2 65 65 28 70 50 61 67 65 72 2d 3e 61 53 61 76 65 ee(pPager->aSave
11dc3 70 6f 69 6e 74 29 3b 0a 20 20 70 50 61 67 65 72 point);. pPager
11dc4 2d 3e 61 53 61 76 65 70 6f 69 6e 74 20 3d 20 30 ->aSavepoint = 0
11dc5 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6e 53 61 76 ;. pPager->nSav
11dc6 65 70 6f 69 6e 74 20 3d 20 30 3b 0a 20 20 70 50 epoint = 0;. pP
11dc7 61 67 65 72 2d 3e 6e 53 75 62 52 65 63 20 3d 20 ager->nSubRec =
11dc8 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 0;.}../*.** Set
11dc9 74 68 65 20 62 69 74 20 6e 75 6d 62 65 72 20 70 the bit number p
11dca 67 6e 6f 20 69 6e 20 74 68 65 20 50 61 67 65 72 gno in the Pager
11dcb 53 61 76 65 70 6f 69 6e 74 2e 70 49 6e 53 61 76 Savepoint.pInSav
11dcc 65 70 6f 69 6e 74 20 0a 2a 2a 20 62 69 74 76 65 epoint .** bitve
11dcd 63 73 20 6f 66 20 61 6c 6c 20 6f 70 65 6e 20 73 cs of all open s
11dce 61 76 65 70 6f 69 6e 74 73 2e 20 52 65 74 75 72 avepoints. Retur
11dcf 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 73 n SQLITE_OK if s
11dd0 75 63 63 65 73 73 66 75 6c 0a 2a 2a 20 6f 72 20 uccessful.** or
11dd1 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 66 20 SQLITE_NOMEM if
11dd2 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 a malloc failure
11dd3 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a 73 74 61 74 occurs..*/.stat
11dd4 69 63 20 69 6e 74 20 61 64 64 54 6f 53 61 76 65 ic int addToSave
11dd5 70 6f 69 6e 74 42 69 74 76 65 63 73 28 50 61 67 pointBitvecs(Pag
11dd6 65 72 20 2a 70 50 61 67 65 72 2c 20 50 67 6e 6f er *pPager, Pgno
11dd7 20 70 67 6e 6f 29 7b 0a 20 20 69 6e 74 20 69 69 pgno){. int ii
11dd8 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
11dd9 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e /* Loop coun
11dda 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 ter */. int rc
11ddb 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 = SQLITE_OK;
11ddc 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 63 6f 64 /* Result cod
11ddd 65 20 2a 2f 0a 0a 20 20 66 6f 72 28 69 69 3d 30 e */.. for(ii=0
11dde 3b 20 69 69 3c 70 50 61 67 65 72 2d 3e 6e 53 61 ; ii<pPager->nSa
11ddf 76 65 70 6f 69 6e 74 3b 20 69 69 2b 2b 29 7b 0a vepoint; ii++){.
11de0 20 20 20 20 50 61 67 65 72 53 61 76 65 70 6f 69 PagerSavepoi
11de1 6e 74 20 2a 70 20 3d 20 26 70 50 61 67 65 72 2d nt *p = &pPager-
11de2 3e 61 53 61 76 65 70 6f 69 6e 74 5b 69 69 5d 3b >aSavepoint[ii];
11de3 0a 20 20 20 20 69 66 28 20 70 67 6e 6f 3c 3d 70 . if( pgno<=p
11de4 2d 3e 6e 4f 72 69 67 20 29 7b 0a 20 20 20 20 20 ->nOrig ){.
11de5 20 72 63 20 7c 3d 20 73 71 6c 69 74 65 33 42 69 rc |= sqlite3Bi
11de6 74 76 65 63 53 65 74 28 70 2d 3e 70 49 6e 53 61 tvecSet(p->pInSa
11de7 76 65 70 6f 69 6e 74 2c 20 70 67 6e 6f 29 3b 0a vepoint, pgno);.
11de8 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
11de9 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d rc==SQLITE_NOMEM
11dea 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 );. assert
11deb 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
11dec 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f || rc==SQLITE_NO
11ded 4d 45 4d 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d MEM );. }. }
11dee 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
11def 0a 2f 2a 0a 2a 2a 20 55 6e 6c 6f 63 6b 20 74 68 ./*.** Unlock th
11df0 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e e database file.
11df1 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 This function i
11df2 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20 74 68 65 s a no-op if the
11df3 20 70 61 67 65 72 0a 2a 2a 20 69 73 20 69 6e 20 pager.** is in
11df4 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 2e 0a exclusive mode..
11df5 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67 **.** If the pag
11df6 65 72 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 er is currently
11df7 69 6e 20 65 72 72 6f 72 20 73 74 61 74 65 2c 20 in error state,
11df8 64 69 73 63 61 72 64 20 74 68 65 20 63 6f 6e 74 discard the cont
11df9 65 6e 74 73 20 6f 66 20 0a 2a 2a 20 74 68 65 20 ents of .** the
11dfa 63 61 63 68 65 20 61 6e 64 20 72 65 73 65 74 20 cache and reset
11dfb 74 68 65 20 50 61 67 65 72 20 73 74 72 75 63 74 the Pager struct
11dfc 75 72 65 20 69 6e 74 65 72 6e 61 6c 20 73 74 61 ure internal sta
11dfd 74 65 2e 20 49 66 20 74 68 65 72 65 20 69 73 0a te. If there is.
11dfe 2a 2a 20 61 6e 20 6f 70 65 6e 20 6a 6f 75 72 6e ** an open journ
11dff 61 6c 2d 66 69 6c 65 2c 20 74 68 65 6e 20 74 68 al-file, then th
11e00 65 20 6e 65 78 74 20 74 69 6d 65 20 61 20 73 68 e next time a sh
11e01 61 72 65 64 2d 6c 6f 63 6b 20 69 73 20 6f 62 74 ared-lock is obt
11e02 61 69 6e 65 64 0a 2a 2a 20 6f 6e 20 74 68 65 20 ained.** on the
11e03 70 61 67 65 72 20 66 69 6c 65 20 28 62 79 20 74 pager file (by t
11e04 68 69 73 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 his or any other
11e05 20 70 72 6f 63 65 73 73 29 2c 20 69 74 20 77 69 process), it wi
11e06 6c 6c 20 62 65 0a 2a 2a 20 74 72 65 61 74 65 64 ll be.** treated
11e07 20 61 73 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 as a hot-journa
11e08 6c 20 61 6e 64 20 72 6f 6c 6c 65 64 20 62 61 63 l and rolled bac
11e09 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 k..*/.static voi
11e0a 64 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 50 d pager_unlock(P
11e0b 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 ager *pPager){.
11e0c 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e 65 78 if( !pPager->ex
11e0d 63 6c 75 73 69 76 65 4d 6f 64 65 20 29 7b 0a 20 clusiveMode ){.
11e0e 20 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 int rc;
11e0f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11e10 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a /* Return code *
11e11 2f 0a 0a 20 20 20 20 2f 2a 20 41 6c 77 61 79 73 /.. /* Always
11e12 20 63 6c 6f 73 65 20 74 68 65 20 6a 6f 75 72 6e close the journ
11e13 61 6c 20 66 69 6c 65 20 77 68 65 6e 20 64 72 6f al file when dro
11e14 70 70 69 6e 67 20 74 68 65 20 64 61 74 61 62 61 pping the databa
11e15 73 65 20 6c 6f 63 6b 2e 0a 20 20 20 20 2a 2a 20 se lock.. **
11e16 4f 74 68 65 72 77 69 73 65 2c 20 61 6e 6f 74 68 Otherwise, anoth
11e17 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 69 er connection wi
11e18 74 68 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d th journal_mode=
11e19 64 65 6c 65 74 65 20 6d 69 67 68 74 0a 20 20 20 delete might.
11e1a 20 2a 2a 20 64 65 6c 65 74 65 20 74 68 65 20 66 ** delete the f
11e1b 69 6c 65 20 6f 75 74 20 66 72 6f 6d 20 75 6e 64 ile out from und
11e1c 65 72 20 75 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 er us.. */.
11e1d 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 sqlite3OsClose
11e1e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 (pPager->jfd);.
11e1f 20 20 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 sqlite3Bitvec
11e20 44 65 73 74 72 6f 79 28 70 50 61 67 65 72 2d 3e Destroy(pPager->
11e21 70 49 6e 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 20 pInJournal);.
11e22 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 pPager->pInJour
11e23 6e 61 6c 20 3d 20 30 3b 0a 20 20 20 20 72 65 6c nal = 0;. rel
11e24 65 61 73 65 41 6c 6c 53 61 76 65 70 6f 69 6e 74 easeAllSavepoint
11e25 73 28 70 50 61 67 65 72 29 3b 0a 0a 20 20 20 20 s(pPager);..
11e26 2f 2a 20 49 66 20 74 68 65 20 66 69 6c 65 20 69 /* If the file i
11e27 73 20 75 6e 6c 6f 63 6b 65 64 2c 20 73 6f 6d 65 s unlocked, some
11e28 62 6f 64 79 20 65 6c 73 65 20 6d 69 67 68 74 20 body else might
11e29 63 68 61 6e 67 65 20 69 74 2e 20 54 68 65 0a 20 change it. The.
11e2a 20 20 20 2a 2a 20 76 61 6c 75 65 73 20 73 74 6f ** values sto
11e2b 72 65 64 20 69 6e 20 50 61 67 65 72 2e 64 62 53 red in Pager.dbS
11e2c 69 7a 65 20 65 74 63 2e 20 6d 69 67 68 74 20 62 ize etc. might b
11e2d 65 63 6f 6d 65 20 69 6e 76 61 6c 69 64 20 69 66 ecome invalid if
11e2e 0a 20 20 20 20 2a 2a 20 74 68 69 73 20 68 61 70 . ** this hap
11e2f 70 65 6e 73 2e 20 54 4f 44 4f 3a 20 52 65 61 6c pens. TODO: Real
11e30 6c 79 2c 20 74 68 69 73 20 64 6f 65 73 6e 27 74 ly, this doesn't
11e31 20 6e 65 65 64 20 74 6f 20 62 65 20 63 6c 65 61 need to be clea
11e32 72 65 64 0a 20 20 20 20 2a 2a 20 75 6e 74 69 6c red. ** until
11e33 20 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e the change-coun
11e34 74 65 72 20 63 68 65 63 6b 20 66 61 69 6c 73 20 ter check fails
11e35 69 6e 20 50 61 67 65 72 53 68 61 72 65 64 4c 6f in PagerSharedLo
11e36 63 6b 28 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 ck().. */.
11e37 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 pPager->dbSizeV
11e38 61 6c 69 64 20 3d 20 30 3b 0a 0a 20 20 20 20 72 alid = 0;.. r
11e39 63 20 3d 20 6f 73 55 6e 6c 6f 63 6b 28 70 50 61 c = osUnlock(pPa
11e3a 67 65 72 2d 3e 66 64 2c 20 4e 4f 5f 4c 4f 43 4b ger->fd, NO_LOCK
11e3b 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b );. if( rc ){
11e3c 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 65 . pPager->e
11e3d 72 72 43 6f 64 65 20 3d 20 72 63 3b 0a 20 20 20 rrCode = rc;.
11e3e 20 7d 0a 20 20 20 20 49 4f 54 52 41 43 45 28 28 }. IOTRACE((
11e3f 22 55 4e 4c 4f 43 4b 20 25 70 5c 6e 22 2c 20 70 "UNLOCK %p\n", p
11e40 50 61 67 65 72 29 29 0a 0a 20 20 20 20 2f 2a 20 Pager)).. /*
11e41 49 66 20 50 61 67 65 72 2e 65 72 72 43 6f 64 65 If Pager.errCode
11e42 20 69 73 20 73 65 74 2c 20 74 68 65 20 63 6f 6e is set, the con
11e43 74 65 6e 74 73 20 6f 66 20 74 68 65 20 70 61 67 tents of the pag
11e44 65 72 20 63 61 63 68 65 20 63 61 6e 6e 6f 74 20 er cache cannot
11e45 62 65 0a 20 20 20 20 2a 2a 20 74 72 75 73 74 65 be. ** truste
11e46 64 2e 20 4e 6f 77 20 74 68 61 74 20 74 68 65 20 d. Now that the
11e47 70 61 67 65 72 20 66 69 6c 65 20 69 73 20 75 6e pager file is un
11e48 6c 6f 63 6b 65 64 2c 20 74 68 65 20 63 6f 6e 74 locked, the cont
11e49 65 6e 74 73 20 6f 66 20 74 68 65 0a 20 20 20 20 ents of the.
11e4a 2a 2a 20 63 61 63 68 65 20 63 61 6e 20 62 65 20 ** cache can be
11e4b 64 69 73 63 61 72 64 65 64 20 61 6e 64 20 74 68 discarded and th
11e4c 65 20 65 72 72 6f 72 20 63 6f 64 65 20 73 61 66 e error code saf
11e4d 65 6c 79 20 63 6c 65 61 72 65 64 2e 0a 20 20 20 ely cleared..
11e4e 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 50 61 67 */. if( pPag
11e4f 65 72 2d 3e 65 72 72 43 6f 64 65 20 29 7b 0a 20 er->errCode ){.
11e50 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c if( rc==SQL
11e51 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
11e52 20 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 pPager->errCod
11e53 65 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 e = SQLITE_OK;.
11e54 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 61 67 }. pag
11e55 65 72 5f 72 65 73 65 74 28 70 50 61 67 65 72 29 er_reset(pPager)
11e56 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 70 50 61 ;. }.. pPa
11e57 67 65 72 2d 3e 63 68 61 6e 67 65 43 6f 75 6e 74 ger->changeCount
11e58 44 6f 6e 65 20 3d 20 30 3b 0a 20 20 20 20 70 50 Done = 0;. pP
11e59 61 67 65 72 2d 3e 73 74 61 74 65 20 3d 20 50 41 ager->state = PA
11e5a 47 45 52 5f 55 4e 4c 4f 43 4b 3b 0a 20 20 7d 0a GER_UNLOCK;. }.
11e5b 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 }../*.** This fu
11e5c 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 62 65 nction should be
11e5d 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 61 6e 20 called when an
11e5e 49 4f 45 52 52 2c 20 43 4f 52 52 55 50 54 20 6f IOERR, CORRUPT o
11e5f 72 20 46 55 4c 4c 20 65 72 72 6f 72 0a 2a 2a 20 r FULL error.**
11e60 6d 61 79 20 68 61 76 65 20 6f 63 63 75 72 72 65 may have occurre
11e61 64 2e 20 54 68 65 20 66 69 72 73 74 20 61 72 67 d. The first arg
11e62 75 6d 65 6e 74 20 69 73 20 61 20 70 6f 69 6e 74 ument is a point
11e63 65 72 20 74 6f 20 74 68 65 20 70 61 67 65 72 20 er to the pager
11e64 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2c 20 74 .** structure, t
11e65 68 65 20 73 65 63 6f 6e 64 20 74 68 65 20 65 72 he second the er
11e66 72 6f 72 2d 63 6f 64 65 20 61 62 6f 75 74 20 74 ror-code about t
11e67 6f 20 62 65 20 72 65 74 75 72 6e 65 64 20 62 79 o be returned by
11e68 20 61 20 70 61 67 65 72 20 0a 2a 2a 20 41 50 49 a pager .** API
11e69 20 66 75 6e 63 74 69 6f 6e 2e 20 54 68 65 20 76 function. The v
11e6a 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 73 alue returned is
11e6b 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 73 a copy of the s
11e6c 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 0a econd argument .
11e6d 2a 2a 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 ** to this funct
11e6e 69 6f 6e 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 ion. .**.** If t
11e6f 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 he second argume
11e70 6e 74 20 69 73 20 53 51 4c 49 54 45 5f 49 4f 45 nt is SQLITE_IOE
11e71 52 52 2c 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 RR, SQLITE_CORRU
11e72 50 54 2c 20 6f 72 20 53 51 4c 49 54 45 5f 46 55 PT, or SQLITE_FU
11e73 4c 4c 0a 2a 2a 20 74 68 65 20 65 72 72 6f 72 20 LL.** the error
11e74 62 65 63 6f 6d 65 73 20 70 65 72 73 69 73 74 65 becomes persiste
11e75 6e 74 2e 20 55 6e 74 69 6c 20 74 68 65 20 70 65 nt. Until the pe
11e76 72 73 69 73 74 65 6e 20 65 72 72 6f 72 20 69 73 rsisten error is
11e77 20 63 6c 65 61 72 65 64 2c 0a 2a 2a 20 73 75 62 cleared,.** sub
11e78 73 65 71 75 65 6e 74 20 41 50 49 20 63 61 6c 6c sequent API call
11e79 73 20 6f 6e 20 74 68 69 73 20 50 61 67 65 72 20 s on this Pager
11e7a 77 69 6c 6c 20 69 6d 6d 65 64 69 61 74 65 6c 79 will immediately
11e7b 20 72 65 74 75 72 6e 20 74 68 65 20 73 61 6d 65 return the same
11e7c 20 0a 2a 2a 20 65 72 72 6f 72 20 63 6f 64 65 2e .** error code.
11e7d 0a 2a 2a 0a 2a 2a 20 41 20 70 65 72 73 69 73 74 .**.** A persist
11e7e 65 6e 74 20 65 72 72 6f 72 20 69 6e 64 69 63 61 ent error indica
11e7f 74 65 73 20 74 68 61 74 20 74 68 65 20 63 6f 6e tes that the con
11e80 74 65 6e 74 73 20 6f 66 20 74 68 65 20 70 61 67 tents of the pag
11e81 65 72 2d 63 61 63 68 65 20 0a 2a 2a 20 63 61 6e er-cache .** can
11e82 6e 6f 74 20 62 65 20 74 72 75 73 74 65 64 2e 20 not be trusted.
11e83 54 68 69 73 20 73 74 61 74 65 20 63 61 6e 20 62 This state can b
11e84 65 20 63 6c 65 61 72 65 64 20 62 79 20 63 6f 6d e cleared by com
11e85 70 6c 65 74 65 6c 79 20 64 69 73 63 61 72 64 69 pletely discardi
11e86 6e 67 20 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 ng .** the conte
11e87 6e 74 73 20 6f 66 20 74 68 65 20 70 61 67 65 72 nts of the pager
11e88 2d 63 61 63 68 65 2e 20 49 66 20 61 20 74 72 61 -cache. If a tra
11e89 6e 73 61 63 74 69 6f 6e 20 77 61 73 20 61 63 74 nsaction was act
11e8a 69 76 65 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20 ive when.** the
11e8b 70 65 72 73 69 73 74 65 6e 74 20 65 72 72 6f 72 persistent error
11e8c 20 6f 63 63 75 72 72 65 64 2c 20 74 68 65 6e 20 occurred, then
11e8d 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 the rollback jou
11e8e 72 6e 61 6c 20 6d 61 79 20 6e 65 65 64 0a 2a 2a rnal may need.**
11e8f 20 74 6f 20 62 65 20 72 65 70 6c 61 79 65 64 20 to be replayed
11e90 74 6f 20 72 65 73 74 6f 72 65 20 74 68 65 20 63 to restore the c
11e91 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 64 ontents of the d
11e92 61 74 61 62 61 73 65 20 66 69 6c 65 20 28 61 73 atabase file (as
11e93 20 69 66 0a 2a 2a 20 69 74 20 77 65 72 65 20 61 if.** it were a
11e94 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 29 2e 0a 2a hot-journal)..*
11e95 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 /.static int pag
11e96 65 72 5f 65 72 72 6f 72 28 50 61 67 65 72 20 2a er_error(Pager *
11e97 70 50 61 67 65 72 2c 20 69 6e 74 20 72 63 29 7b pPager, int rc){
11e98 0a 20 20 69 6e 74 20 72 63 32 20 3d 20 72 63 20 . int rc2 = rc
11e99 26 20 30 78 66 66 3b 0a 20 20 61 73 73 65 72 74 & 0xff;. assert
11e9a 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
11e9b 7c 7c 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20 61 || !MEMDB );. a
11e9c 73 73 65 72 74 28 0a 20 20 20 20 20 20 20 70 50 ssert(. pP
11e9d 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3d 3d 53 ager->errCode==S
11e9e 51 4c 49 54 45 5f 46 55 4c 4c 20 7c 7c 0a 20 20 QLITE_FULL ||.
11e9f 20 20 20 20 20 70 50 61 67 65 72 2d 3e 65 72 72 pPager->err
11ea0 43 6f 64 65 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 Code==SQLITE_OK
11ea1 7c 7c 0a 20 20 20 20 20 20 20 28 70 50 61 67 65 ||. (pPage
11ea2 72 2d 3e 65 72 72 43 6f 64 65 20 26 20 30 78 66 r->errCode & 0xf
11ea3 66 29 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 f)==SQLITE_IOERR
11ea4 0a 20 20 29 3b 0a 20 20 69 66 28 20 72 63 32 3d . );. if( rc2=
11ea5 3d 53 51 4c 49 54 45 5f 46 55 4c 4c 20 7c 7c 20 =SQLITE_FULL ||
11ea6 72 63 32 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 rc2==SQLITE_IOER
11ea7 52 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d R ){. pPager-
11ea8 3e 65 72 72 43 6f 64 65 20 3d 20 72 63 3b 0a 20 >errCode = rc;.
11ea9 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
11eaa 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 65 63 75 74 65 }../*.** Execute
11eab 20 61 20 72 6f 6c 6c 62 61 63 6b 20 69 66 20 61 a rollback if a
11eac 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 transaction is
11ead 61 63 74 69 76 65 20 61 6e 64 20 75 6e 6c 6f 63 active and unloc
11eae 6b 20 74 68 65 20 0a 2a 2a 20 64 61 74 61 62 61 k the .** databa
11eaf 73 65 20 66 69 6c 65 2e 20 0a 2a 2a 0a 2a 2a 20 se file. .**.**
11eb0 49 66 20 74 68 65 20 70 61 67 65 72 20 68 61 73 If the pager has
11eb1 20 61 6c 72 65 61 64 79 20 65 6e 74 65 72 65 64 already entered
11eb2 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 the error state
11eb3 2c 20 64 6f 20 6e 6f 74 20 61 74 74 65 6d 70 74 , do not attempt
11eb4 20 0a 2a 2a 20 74 68 65 20 72 6f 6c 6c 62 61 63 .** the rollbac
11eb5 6b 20 61 74 20 74 68 69 73 20 74 69 6d 65 2e 20 k at this time.
11eb6 49 6e 73 74 65 61 64 2c 20 70 61 67 65 72 5f 75 Instead, pager_u
11eb7 6e 6c 6f 63 6b 28 29 20 69 73 20 63 61 6c 6c 65 nlock() is calle
11eb8 64 2e 20 54 68 65 0a 2a 2a 20 63 61 6c 6c 20 74 d. The.** call t
11eb9 6f 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 29 o pager_unlock()
11eba 20 77 69 6c 6c 20 64 69 73 63 61 72 64 20 61 6c will discard al
11ebb 6c 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 l in-memory page
11ebc 73 2c 20 75 6e 6c 6f 63 6b 0a 2a 2a 20 74 68 65 s, unlock.** the
11ebd 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 database file a
11ebe 6e 64 20 63 6c 65 61 72 20 74 68 65 20 65 72 72 nd clear the err
11ebf 6f 72 20 73 74 61 74 65 2e 20 49 66 20 74 68 69 or state. If thi
11ec0 73 20 6d 65 61 6e 73 20 74 68 61 74 0a 2a 2a 20 s means that.**
11ec1 74 68 65 72 65 20 69 73 20 61 20 68 6f 74 2d 6a there is a hot-j
11ec2 6f 75 72 6e 61 6c 20 6c 65 66 74 20 69 6e 20 74 ournal left in t
11ec3 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 2c 20 he file-system,
11ec4 74 68 65 20 6e 65 78 74 20 63 6f 6e 6e 65 63 74 the next connect
11ec5 69 6f 6e 0a 2a 2a 20 74 6f 20 6f 62 74 61 69 6e ion.** to obtain
11ec6 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 6f a shared lock o
11ec7 6e 20 74 68 65 20 70 61 67 65 72 20 28 77 68 69 n the pager (whi
11ec8 63 68 20 6d 61 79 20 62 65 20 74 68 69 73 20 6f ch may be this o
11ec9 6e 65 29 20 77 69 6c 6c 0a 2a 2a 20 72 6f 6c 6c ne) will.** roll
11eca 20 69 74 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 it back..**.**
11ecb 49 66 20 74 68 65 20 70 61 67 65 72 20 68 61 73 If the pager has
11ecc 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 65 6e 74 not already ent
11ecd 65 72 65 64 20 74 68 65 20 65 72 72 6f 72 20 73 ered the error s
11ece 74 61 74 65 2c 20 62 75 74 20 61 6e 20 49 4f 20 tate, but an IO
11ecf 6f 72 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 65 72 72 or.** malloc err
11ed0 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 or occurs during
11ed1 20 61 20 72 6f 6c 6c 62 61 63 6b 2c 20 74 68 65 a rollback, the
11ed2 6e 20 74 68 69 73 20 77 69 6c 6c 20 69 74 73 65 n this will itse
11ed3 6c 66 20 63 61 75 73 65 20 0a 2a 2a 20 74 68 65 lf cause .** the
11ed4 20 70 61 67 65 72 20 74 6f 20 65 6e 74 65 72 20 pager to enter
11ed5 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 2e the error state.
11ed6 20 57 68 69 63 68 20 77 69 6c 6c 20 62 65 20 63 Which will be c
11ed7 6c 65 61 72 65 64 20 62 79 20 74 68 65 0a 2a 2a leared by the.**
11ed8 20 63 61 6c 6c 20 74 6f 20 70 61 67 65 72 5f 75 call to pager_u
11ed9 6e 6c 6f 63 6b 28 29 2c 20 61 73 20 64 65 73 63 nlock(), as desc
11eda 72 69 62 65 64 20 61 62 6f 76 65 2e 0a 2a 2f 0a ribed above..*/.
11edb 73 74 61 74 69 63 20 76 6f 69 64 20 70 61 67 65 static void page
11edc 72 55 6e 6c 6f 63 6b 41 6e 64 52 6f 6c 6c 62 61 rUnlockAndRollba
11edd 63 6b 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 ck(Pager *pPager
11ede 29 7b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d ){. if( pPager-
11edf 3e 65 72 72 43 6f 64 65 3d 3d 53 51 4c 49 54 45 >errCode==SQLITE
11ee0 5f 4f 4b 20 26 26 20 70 50 61 67 65 72 2d 3e 73 _OK && pPager->s
11ee1 74 61 74 65 3e 3d 50 41 47 45 52 5f 52 45 53 45 tate>=PAGER_RESE
11ee2 52 56 45 44 20 29 7b 0a 20 20 20 20 73 71 6c 69 RVED ){. sqli
11ee3 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 te3BeginBenignMa
11ee4 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 73 71 6c 69 lloc();. sqli
11ee5 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b te3PagerRollback
11ee6 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 73 71 (pPager);. sq
11ee7 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 lite3EndBenignMa
11ee8 6c 6c 6f 63 28 29 3b 0a 20 20 7d 0a 20 20 70 61 lloc();. }. pa
11ee9 67 65 72 5f 75 6e 6c 6f 63 6b 28 70 50 61 67 65 ger_unlock(pPage
11eea 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 r);.}../*.** Thi
11eeb 73 20 72 6f 75 74 69 6e 65 20 65 6e 64 73 20 61 s routine ends a
11eec 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 41 20 transaction. A
11eed 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 75 transaction is u
11eee 73 75 61 6c 6c 79 20 65 6e 64 65 64 20 62 79 20 sually ended by
11eef 0a 2a 2a 20 65 69 74 68 65 72 20 61 20 43 4f 4d .** either a COM
11ef0 4d 49 54 20 6f 72 20 61 20 52 4f 4c 4c 42 41 43 MIT or a ROLLBAC
11ef1 4b 20 6f 70 65 72 61 74 69 6f 6e 2e 20 54 68 69 K operation. Thi
11ef2 73 20 72 6f 75 74 69 6e 65 20 6d 61 79 20 62 65 s routine may be
11ef3 20 63 61 6c 6c 65 64 20 0a 2a 2a 20 61 66 74 65 called .** afte
11ef4 72 20 72 6f 6c 6c 62 61 63 6b 20 6f 66 20 61 20 r rollback of a
11ef5 68 6f 74 2d 6a 6f 75 72 6e 61 6c 2c 20 6f 72 20 hot-journal, or
11ef6 69 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 if an error occu
11ef7 72 73 20 77 68 69 6c 65 20 6f 70 65 6e 69 6e 67 rs while opening
11ef8 0a 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 .** the journal
11ef9 66 69 6c 65 20 6f 72 20 77 72 69 74 69 6e 67 20 file or writing
11efa 74 68 65 20 76 65 72 79 20 66 69 72 73 74 20 6a the very first j
11efb 6f 75 72 6e 61 6c 2d 68 65 61 64 65 72 20 6f 66 ournal-header of
11efc 20 61 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 74 a.** database t
11efd 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 20 0a ransaction..** .
11efe 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65 72 20 ** If the pager
11eff 69 73 20 69 6e 20 50 41 47 45 52 5f 53 48 41 52 is in PAGER_SHAR
11f00 45 44 20 6f 72 20 50 41 47 45 52 5f 55 4e 4c 4f ED or PAGER_UNLO
11f01 43 4b 20 73 74 61 74 65 20 77 68 65 6e 20 74 68 CK state when th
11f02 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 69 73 is.** routine is
11f03 20 63 61 6c 6c 65 64 2c 20 69 74 20 69 73 20 61 called, it is a
11f04 20 6e 6f 2d 6f 70 20 28 72 65 74 75 72 6e 73 20 no-op (returns
11f05 53 51 4c 49 54 45 5f 4f 4b 29 2e 0a 2a 2a 0a 2a SQLITE_OK)..**.*
11f06 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 61 6e 79 * Otherwise, any
11f07 20 61 63 74 69 76 65 20 73 61 76 65 70 6f 69 6e active savepoin
11f08 74 73 20 61 72 65 20 72 65 6c 65 61 73 65 64 2e ts are released.
11f09 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6a 6f .**.** If the jo
11f0a 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6f 70 urnal file is op
11f0b 65 6e 2c 20 74 68 65 6e 20 69 74 20 69 73 20 22 en, then it is "
11f0c 66 69 6e 61 6c 69 7a 65 64 22 2e 20 4f 6e 63 65 finalized". Once
11f0d 20 61 20 6a 6f 75 72 6e 61 6c 20 0a 2a 2a 20 66 a journal .** f
11f0e 69 6c 65 20 68 61 73 20 62 65 65 6e 20 66 69 6e ile has been fin
11f0f 61 6c 69 7a 65 64 20 69 74 20 69 73 20 6e 6f 74 alized it is not
11f10 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 75 73 65 possible to use
11f11 20 69 74 20 74 6f 20 72 6f 6c 6c 20 62 61 63 6b it to roll back
11f12 20 61 20 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 a .** transacti
11f13 6f 6e 2e 20 4e 6f 72 20 77 69 6c 6c 20 69 74 20 on. Nor will it
11f14 62 65 20 63 6f 6e 73 69 64 65 72 65 64 20 74 6f be considered to
11f15 20 62 65 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 be a hot-journa
11f16 6c 20 62 79 20 74 68 69 73 0a 2a 2a 20 6f 72 20 l by this.** or
11f17 61 6e 79 20 6f 74 68 65 72 20 64 61 74 61 62 61 any other databa
11f18 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 45 se connection. E
11f19 78 61 63 74 6c 79 20 68 6f 77 20 61 20 6a 6f 75 xactly how a jou
11f1a 72 6e 61 6c 20 69 73 20 66 69 6e 61 6c 69 7a 65 rnal is finalize
11f1b 64 0a 2a 2a 20 64 65 70 65 6e 64 73 20 6f 6e 20 d.** depends on
11f1c 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 whether or not t
11f1d 68 65 20 70 61 67 65 72 20 69 73 20 72 75 6e 6e he pager is runn
11f1e 69 6e 67 20 69 6e 20 65 78 63 6c 75 73 69 76 65 ing in exclusive
11f1f 20 6d 6f 64 65 20 61 6e 64 0a 2a 2a 20 74 68 65 mode and.** the
11f20 20 63 75 72 72 65 6e 74 20 6a 6f 75 72 6e 61 6c current journal
11f21 2d 6d 6f 64 65 20 28 50 61 67 65 72 2e 6a 6f 75 -mode (Pager.jou
11f22 72 6e 61 6c 4d 6f 64 65 20 76 61 6c 75 65 29 2c rnalMode value),
11f23 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a as follows:.**.
11f24 2a 2a 20 20 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 ** journalMode
11f25 3d 3d 4d 45 4d 4f 52 59 0a 2a 2a 20 20 20 20 20 ==MEMORY.**
11f26 4a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 64 65 73 Journal file des
11f27 63 72 69 70 74 6f 72 20 69 73 20 73 69 6d 70 6c criptor is simpl
11f28 79 20 63 6c 6f 73 65 64 2e 20 54 68 69 73 20 64 y closed. This d
11f29 65 73 74 72 6f 79 73 20 61 6e 20 0a 2a 2a 20 20 estroys an .**
11f2a 20 20 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 in-memory jou
11f2b 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a 20 20 20 6a 6f rnal..**.** jo
11f2c 75 72 6e 61 6c 4d 6f 64 65 3d 3d 54 52 55 4e 43 urnalMode==TRUNC
11f2d 41 54 45 0a 2a 2a 20 20 20 20 20 4a 6f 75 72 6e ATE.** Journ
11f2e 61 6c 20 66 69 6c 65 20 69 73 20 74 72 75 6e 63 al file is trunc
11f2f 61 74 65 64 20 74 6f 20 7a 65 72 6f 20 62 79 74 ated to zero byt
11f30 65 73 20 69 6e 20 73 69 7a 65 2e 0a 2a 2a 0a 2a es in size..**.*
11f31 2a 20 20 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d * journalMode=
11f32 3d 50 45 52 53 49 53 54 0a 2a 2a 20 20 20 20 20 =PERSIST.**
11f33 54 68 65 20 66 69 72 73 74 20 32 38 20 62 79 74 The first 28 byt
11f34 65 73 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 es of the journa
11f35 6c 20 66 69 6c 65 20 61 72 65 20 7a 65 72 6f 65 l file are zeroe
11f36 64 2e 20 54 68 69 73 20 69 6e 76 61 6c 69 64 61 d. This invalida
11f37 74 65 73 0a 2a 2a 20 20 20 20 20 74 68 65 20 66 tes.** the f
11f38 69 72 73 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61 irst journal hea
11f39 64 65 72 20 69 6e 20 74 68 65 20 66 69 6c 65 2c der in the file,
11f3a 20 61 6e 64 20 68 65 6e 63 65 20 74 68 65 20 65 and hence the e
11f3b 6e 74 69 72 65 20 6a 6f 75 72 6e 61 6c 0a 2a 2a ntire journal.**
11f3c 20 20 20 20 20 66 69 6c 65 2e 20 41 6e 20 69 6e file. An in
11f3d 76 61 6c 69 64 20 6a 6f 75 72 6e 61 6c 20 66 69 valid journal fi
11f3e 6c 65 20 63 61 6e 6e 6f 74 20 62 65 20 72 6f 6c le cannot be rol
11f3f 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 led back..**.**
11f40 20 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 44 journalMode==D
11f41 45 4c 45 54 45 0a 2a 2a 20 20 20 20 20 54 68 65 ELETE.** The
11f42 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 journal file is
11f43 20 63 6c 6f 73 65 64 20 61 6e 64 20 64 65 6c 65 closed and dele
11f44 74 65 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65 ted using sqlite
11f45 33 4f 73 44 65 6c 65 74 65 28 29 2e 0a 2a 2a 0a 3OsDelete()..**.
11f46 2a 2a 20 20 20 20 20 49 66 20 74 68 65 20 70 61 ** If the pa
11f47 67 65 72 20 69 73 20 72 75 6e 6e 69 6e 67 20 69 ger is running i
11f48 6e 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 n exclusive mode
11f49 2c 20 74 68 69 73 20 6d 65 74 68 6f 64 20 6f 66 , this method of
11f4a 20 66 69 6e 61 6c 69 7a 69 6e 67 0a 2a 2a 20 20 finalizing.**
11f4b 20 20 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 the journal f
11f4c 69 6c 65 20 69 73 20 6e 65 76 65 72 20 75 73 65 ile is never use
11f4d 64 2e 20 49 6e 73 74 65 61 64 2c 20 69 66 20 74 d. Instead, if t
11f4e 68 65 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 20 69 he journalMode i
11f4f 73 0a 2a 2a 20 20 20 20 20 44 45 4c 45 54 45 20 s.** DELETE
11f50 61 6e 64 20 74 68 65 20 70 61 67 65 72 20 69 73 and the pager is
11f51 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20 6d 6f in exclusive mo
11f52 64 65 2c 20 74 68 65 20 6d 65 74 68 6f 64 20 64 de, the method d
11f53 65 73 63 72 69 62 65 64 20 75 6e 64 65 72 0a 2a escribed under.*
11f54 2a 20 20 20 20 20 6a 6f 75 72 6e 61 6c 4d 6f 64 * journalMod
11f55 65 3d 3d 50 45 52 53 49 53 54 20 69 73 20 75 73 e==PERSIST is us
11f56 65 64 20 69 6e 73 74 65 61 64 2e 0a 2a 2a 0a 2a ed instead..**.*
11f57 2a 20 41 66 74 65 72 20 74 68 65 20 6a 6f 75 72 * After the jour
11f58 6e 61 6c 20 69 73 20 66 69 6e 61 6c 69 7a 65 64 nal is finalized
11f59 2c 20 69 66 20 72 75 6e 6e 69 6e 67 20 69 6e 20 , if running in
11f5a 6e 6f 6e 2d 65 78 63 6c 75 73 69 76 65 20 6d 6f non-exclusive mo
11f5b 64 65 2c 20 74 68 65 0a 2a 2a 20 70 61 67 65 72 de, the.** pager
11f5c 20 6d 6f 76 65 73 20 74 6f 20 50 41 47 45 52 5f moves to PAGER_
11f5d 53 48 41 52 45 44 20 73 74 61 74 65 20 28 61 6e SHARED state (an
11f5e 64 20 64 6f 77 6e 67 72 61 64 65 73 20 74 68 65 d downgrades the
11f5f 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 0a 2a 2a 20 lock on the.**
11f60 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 63 database file ac
11f61 63 6f 72 64 69 6e 67 6c 79 29 2e 0a 2a 2a 0a 2a cordingly)..**.*
11f62 2a 20 49 66 20 74 68 65 20 70 61 67 65 72 20 69 * If the pager i
11f63 73 20 72 75 6e 6e 69 6e 67 20 69 6e 20 65 78 63 s running in exc
11f64 6c 75 73 69 76 65 20 6d 6f 64 65 20 61 6e 64 20 lusive mode and
11f65 69 73 20 69 6e 20 50 41 47 45 52 5f 53 59 4e 43 is in PAGER_SYNC
11f66 45 44 20 73 74 61 74 65 2c 0a 2a 2a 20 69 74 20 ED state,.** it
11f67 6d 6f 76 65 73 20 74 6f 20 50 41 47 45 52 5f 45 moves to PAGER_E
11f68 58 43 4c 55 53 49 56 45 2e 20 4e 6f 20 6c 6f 63 XCLUSIVE. No loc
11f69 6b 73 20 61 72 65 20 64 6f 77 6e 67 72 61 64 65 ks are downgrade
11f6a 64 20 77 68 65 6e 20 72 75 6e 6e 69 6e 67 20 69 d when running i
11f6b 6e 0a 2a 2a 20 65 78 63 6c 75 73 69 76 65 20 6d n.** exclusive m
11f6c 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 ode..**.** SQLIT
11f6d 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 E_OK is returned
11f6e 20 69 66 20 6e 6f 20 65 72 72 6f 72 20 6f 63 63 if no error occ
11f6f 75 72 73 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 urs. If an error
11f70 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 0a 2a occurs during.*
11f71 2a 20 61 6e 79 20 6f 66 20 74 68 65 20 49 4f 20 * any of the IO
11f72 6f 70 65 72 61 74 69 6f 6e 73 20 74 6f 20 66 69 operations to fi
11f73 6e 61 6c 69 7a 65 20 74 68 65 20 6a 6f 75 72 6e nalize the journ
11f74 61 6c 20 66 69 6c 65 20 6f 72 20 75 6e 6c 6f 63 al file or unloc
11f75 6b 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 k the.** databas
11f76 65 20 74 68 65 6e 20 74 68 65 20 49 4f 20 65 72 e then the IO er
11f77 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 ror code is retu
11f78 72 6e 65 64 20 74 6f 20 74 68 65 20 75 73 65 72 rned to the user
11f79 2e 20 49 66 20 74 68 65 20 0a 2a 2a 20 6f 70 65 . If the .** ope
11f7a 72 61 74 69 6f 6e 20 74 6f 20 66 69 6e 61 6c 69 ration to finali
11f7b 7a 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 ze the journal f
11f7c 69 6c 65 20 66 61 69 6c 73 2c 20 74 68 65 6e 20 ile fails, then
11f7d 74 68 65 20 63 6f 64 65 20 73 74 69 6c 6c 0a 2a the code still.*
11f7e 2a 20 74 72 69 65 73 20 74 6f 20 75 6e 6c 6f 63 * tries to unloc
11f7f 6b 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 k the database f
11f80 69 6c 65 20 69 66 20 6e 6f 74 20 69 6e 20 65 78 ile if not in ex
11f81 63 6c 75 73 69 76 65 20 6d 6f 64 65 2e 20 49 66 clusive mode. If
11f82 20 74 68 65 0a 2a 2a 20 75 6e 6c 6f 63 6b 20 6f the.** unlock o
11f83 70 65 72 61 74 69 6f 6e 20 66 61 69 6c 73 20 61 peration fails a
11f84 73 20 77 65 6c 6c 2c 20 74 68 65 6e 20 74 68 65 s well, then the
11f85 20 66 69 72 73 74 20 65 72 72 6f 72 20 63 6f 64 first error cod
11f86 65 20 72 65 6c 61 74 65 64 0a 2a 2a 20 74 6f 20 e related.** to
11f87 74 68 65 20 66 69 72 73 74 20 65 72 72 6f 72 20 the first error
11f88 65 6e 63 6f 75 6e 74 65 72 65 64 20 28 74 68 65 encountered (the
11f89 20 6a 6f 75 72 6e 61 6c 20 66 69 6e 61 6c 69 7a journal finaliz
11f8a 61 74 69 6f 6e 20 6f 6e 65 29 20 69 73 0a 2a 2a ation one) is.**
11f8b 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 returned..*/.st
11f8c 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 5f 65 atic int pager_e
11f8d 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 50 nd_transaction(P
11f8e 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e ager *pPager, in
11f8f 74 20 68 61 73 4d 61 73 74 65 72 29 7b 0a 20 20 t hasMaster){.
11f90 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
11f91 4f 4b 3b 20 20 20 20 20 20 2f 2a 20 45 72 72 6f OK; /* Erro
11f92 72 20 63 6f 64 65 20 66 72 6f 6d 20 6a 6f 75 72 r code from jour
11f93 6e 61 6c 20 66 69 6e 61 6c 69 7a 61 74 69 6f 6e nal finalization
11f94 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a 20 20 operation */.
11f95 69 6e 74 20 72 63 32 20 3d 20 53 51 4c 49 54 45 int rc2 = SQLITE
11f96 5f 4f 4b 3b 20 20 20 20 20 2f 2a 20 45 72 72 6f _OK; /* Erro
11f97 72 20 63 6f 64 65 20 66 72 6f 6d 20 64 62 20 66 r code from db f
11f98 69 6c 65 20 75 6e 6c 6f 63 6b 20 6f 70 65 72 61 ile unlock opera
11f99 74 69 6f 6e 20 2a 2f 0a 0a 20 20 69 66 28 20 70 tion */.. if( p
11f9a 50 61 67 65 72 2d 3e 73 74 61 74 65 3c 50 41 47 Pager->state<PAG
11f9b 45 52 5f 52 45 53 45 52 56 45 44 20 29 7b 0a 20 ER_RESERVED ){.
11f9c 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
11f9d 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 72 65 6c 65 61 _OK;. }. relea
11f9e 73 65 41 6c 6c 53 61 76 65 70 6f 69 6e 74 73 28 seAllSavepoints(
11f9f 70 50 61 67 65 72 29 3b 0a 0a 20 20 61 73 73 65 pPager);.. asse
11fa0 72 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 rt( isOpen(pPage
11fa1 72 2d 3e 6a 66 64 29 20 7c 7c 20 70 50 61 67 65 r->jfd) || pPage
11fa2 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 r->pInJournal==0
11fa3 20 29 3b 0a 20 20 69 66 28 20 69 73 4f 70 65 6e );. if( isOpen
11fa4 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 7b (pPager->jfd) ){
11fa5 0a 0a 20 20 20 20 2f 2a 20 46 69 6e 61 6c 69 7a .. /* Finaliz
11fa6 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 e the journal fi
11fa7 6c 65 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 73 le. */. if( s
11fa8 71 6c 69 74 65 33 49 73 4d 65 6d 4a 6f 75 72 6e qlite3IsMemJourn
11fa9 61 6c 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 al(pPager->jfd)
11faa 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 ){. assert(
11fab 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
11fac 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 Mode==PAGER_JOUR
11fad 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 NALMODE_MEMORY )
11fae 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f ;. sqlite3O
11faf 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e 6a sClose(pPager->j
11fb0 66 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 fd);. }else i
11fb1 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e f( pPager->journ
11fb2 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f alMode==PAGER_JO
11fb3 55 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 URNALMODE_TRUNCA
11fb4 54 45 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 TE ){. if(
11fb5 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
11fb6 66 66 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 ff==0 ){.
11fb7 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
11fb8 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
11fb9 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
11fba 65 33 4f 73 54 72 75 6e 63 61 74 65 28 70 50 61 e3OsTruncate(pPa
11fbb 67 65 72 2d 3e 6a 66 64 2c 20 30 29 3b 0a 20 20 ger->jfd, 0);.
11fbc 20 20 20 20 7d 0a 20 20 20 20 20 20 70 50 61 67 }. pPag
11fbd 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d er->journalOff =
11fbe 20 30 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72 0;. pPager
11fbf 2d 3e 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 ->journalStarted
11fc0 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20 = 0;. }else
11fc1 69 66 28 20 70 50 61 67 65 72 2d 3e 65 78 63 6c if( pPager->excl
11fc2 75 73 69 76 65 4d 6f 64 65 20 0a 20 20 20 20 20 usiveMode .
11fc3 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e || pPager->journ
11fc4 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f alMode==PAGER_JO
11fc5 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53 URNALMODE_PERSIS
11fc6 54 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 72 T. ){. r
11fc7 63 20 3d 20 7a 65 72 6f 4a 6f 75 72 6e 61 6c 48 c = zeroJournalH
11fc8 64 72 28 70 50 61 67 65 72 2c 20 68 61 73 4d 61 dr(pPager, hasMa
11fc9 73 74 65 72 29 3b 0a 20 20 20 20 20 20 70 61 67 ster);. pag
11fca 65 72 5f 65 72 72 6f 72 28 70 50 61 67 65 72 2c er_error(pPager,
11fcb 20 72 63 29 3b 0a 20 20 20 20 20 20 70 50 61 67 rc);. pPag
11fcc 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d er->journalOff =
11fcd 20 30 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72 0;. pPager
11fce 2d 3e 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 ->journalStarted
11fcf 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b = 0;. }else{
11fd0 0a 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 62 . /* This b
11fd1 72 61 6e 63 68 20 6d 61 79 20 62 65 20 65 78 65 ranch may be exe
11fd2 63 75 74 65 64 20 77 69 74 68 20 50 61 67 65 72 cuted with Pager
11fd3 2e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 4d 45 .journalMode==ME
11fd4 4d 4f 52 59 20 69 66 0a 20 20 20 20 20 20 2a 2a MORY if. **
11fd5 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 77 a hot-journal w
11fd6 61 73 20 6a 75 73 74 20 72 6f 6c 6c 65 64 20 62 as just rolled b
11fd7 61 63 6b 2e 20 49 6e 20 74 68 69 73 20 63 61 73 ack. In this cas
11fd8 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 20 20 e the journal.
11fd9 20 20 20 20 2a 2a 20 66 69 6c 65 20 73 68 6f 75 ** file shou
11fda 6c 64 20 62 65 20 63 6c 6f 73 65 64 20 61 6e 64 ld be closed and
11fdb 20 64 65 6c 65 74 65 64 2e 20 49 66 20 74 68 69 deleted. If thi
11fdc 73 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 72 69 s connection wri
11fdd 74 65 73 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 tes to. **
11fde 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
11fdf 65 2c 20 69 74 20 77 69 6c 6c 20 64 6f 20 73 6f e, it will do so
11fe0 20 75 73 69 6e 67 20 61 6e 20 69 6e 2d 6d 65 6d using an in-mem
11fe1 6f 72 79 20 6a 6f 75 72 6e 61 6c 2e 20 20 2a 2f ory journal. */
11fe2 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
11fe3 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f Pager->journalMo
11fe4 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 de==PAGER_JOURNA
11fe5 4c 4d 4f 44 45 5f 44 45 4c 45 54 45 20 0a 20 20 LMODE_DELETE .
11fe6 20 20 20 20 20 20 20 20 20 7c 7c 20 70 50 61 67 || pPag
11fe7 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d er->journalMode=
11fe8 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f =PAGER_JOURNALMO
11fe9 44 45 5f 4d 45 4d 4f 52 59 20 0a 20 20 20 20 20 DE_MEMORY .
11fea 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 );. sqlite
11feb 33 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3OsClose(pPager-
11fec 3e 6a 66 64 29 3b 0a 20 20 20 20 20 20 69 66 28 >jfd);. if(
11fed 20 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 !pPager->tempFi
11fee 6c 65 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 le ){. rc
11fef 20 3d 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 = sqlite3OsDele
11ff0 74 65 28 70 50 61 67 65 72 2d 3e 70 56 66 73 2c te(pPager->pVfs,
11ff1 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 pPager->zJourna
11ff2 6c 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 l, 0);. }.
11ff3 20 20 20 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c }..#ifdef SQL
11ff4 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a ITE_CHECK_PAGES.
11ff5 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 sqlite3Pcach
11ff6 65 49 74 65 72 61 74 65 44 69 72 74 79 28 70 50 eIterateDirty(pP
11ff7 61 67 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 70 ager->pPCache, p
11ff8 61 67 65 72 5f 73 65 74 5f 70 61 67 65 68 61 73 ager_set_pagehas
11ff9 68 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 h);.#endif..
11ffa 73 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c 65 sqlite3PcacheCle
11ffb 61 6e 41 6c 6c 28 70 50 61 67 65 72 2d 3e 70 50 anAll(pPager->pP
11ffc 43 61 63 68 65 29 3b 0a 20 20 20 20 73 71 6c 69 Cache);. sqli
11ffd 74 65 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 te3BitvecDestroy
11ffe 28 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 (pPager->pInJour
11fff 6e 61 6c 29 3b 0a 20 20 20 20 70 50 61 67 65 72 nal);. pPager
12000 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 20 3d 20 30 ->pInJournal = 0
12001 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 52 ;. pPager->nR
12002 65 63 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 69 ec = 0;. }.. i
12003 66 28 20 21 70 50 61 67 65 72 2d 3e 65 78 63 6c f( !pPager->excl
12004 75 73 69 76 65 4d 6f 64 65 20 29 7b 0a 20 20 20 usiveMode ){.
12005 20 72 63 32 20 3d 20 6f 73 55 6e 6c 6f 63 6b 28 rc2 = osUnlock(
12006 70 50 61 67 65 72 2d 3e 66 64 2c 20 53 48 41 52 pPager->fd, SHAR
12007 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 70 50 ED_LOCK);. pP
12008 61 67 65 72 2d 3e 73 74 61 74 65 20 3d 20 50 41 ager->state = PA
12009 47 45 52 5f 53 48 41 52 45 44 3b 0a 20 20 20 20 GER_SHARED;.
1200a 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 43 6f pPager->changeCo
1200b 75 6e 74 44 6f 6e 65 20 3d 20 30 3b 0a 20 20 7d untDone = 0;. }
1200c 65 6c 73 65 20 69 66 28 20 70 50 61 67 65 72 2d else if( pPager-
1200d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 5f 53 59 >state==PAGER_SY
1200e 4e 43 45 44 20 29 7b 0a 20 20 20 20 70 50 61 67 NCED ){. pPag
1200f 65 72 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45 er->state = PAGE
12010 52 5f 45 58 43 4c 55 53 49 56 45 3b 0a 20 20 7d R_EXCLUSIVE;. }
12011 0a 20 20 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 . pPager->setMa
12012 73 74 65 72 20 3d 20 30 3b 0a 20 20 70 50 61 67 ster = 0;. pPag
12013 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 30 er->needSync = 0
12014 3b 0a 20 20 70 50 61 67 65 72 2d 3e 64 62 4d 6f ;. pPager->dbMo
12015 64 69 66 69 65 64 20 3d 20 30 3b 0a 0a 20 20 2f dified = 0;.. /
12016 2a 20 54 4f 44 4f 3a 20 49 73 20 74 68 69 73 20 * TODO: Is this
12017 6f 70 74 69 6d 61 6c 3f 20 57 68 79 20 69 73 20 optimal? Why is
12018 74 68 65 20 64 62 20 73 69 7a 65 20 69 6e 76 61 the db size inva
12019 6c 69 64 61 74 65 64 20 68 65 72 65 20 0a 20 20 lidated here .
1201a 2a 2a 20 77 68 65 6e 20 74 68 65 20 64 61 74 61 ** when the data
1201b 62 61 73 65 20 66 69 6c 65 20 69 73 20 6e 6f 74 base file is not
1201c 20 75 6e 6c 6f 63 6b 65 64 3f 20 2a 2f 0a 20 20 unlocked? */.
1201d 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 pPager->dbOrigSi
1201e 7a 65 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 ze = 0;. sqlite
1201f 33 50 63 61 63 68 65 54 72 75 6e 63 61 74 65 28 3PcacheTruncate(
12020 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 2c pPager->pPCache,
12021 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 29 pPager->dbSize)
12022 3b 0a 20 20 69 66 28 20 21 4d 45 4d 44 42 20 29 ;. if( !MEMDB )
12023 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 {. pPager->db
12024 53 69 7a 65 56 61 6c 69 64 20 3d 20 30 3b 0a 20 SizeValid = 0;.
12025 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 28 72 63 }.. return (rc
12026 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3f 72 63 32 3a ==SQLITE_OK?rc2:
12027 72 63 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 rc);.}../*.** Pa
12028 72 61 6d 65 74 65 72 20 61 44 61 74 61 20 6d 75 rameter aData mu
12029 73 74 20 70 6f 69 6e 74 20 74 6f 20 61 20 62 75 st point to a bu
1202a 66 66 65 72 20 6f 66 20 70 50 61 67 65 72 2d 3e ffer of pPager->
1202b 70 61 67 65 53 69 7a 65 20 62 79 74 65 73 0a 2a pageSize bytes.*
1202c 2a 20 6f 66 20 64 61 74 61 2e 20 43 6f 6d 70 75 * of data. Compu
1202d 74 65 20 61 6e 64 20 72 65 74 75 72 6e 20 61 20 te and return a
1202e 63 68 65 63 6b 73 75 6d 20 62 61 73 65 64 20 6f checksum based o
1202f 6e 74 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 nt the contents
12030 6f 66 20 74 68 65 20 0a 2a 2a 20 70 61 67 65 20 of the .** page
12031 6f 66 20 64 61 74 61 20 61 6e 64 20 74 68 65 20 of data and the
12032 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 current value of
12033 20 70 50 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e pPager->cksumIn
12034 69 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 it..**.** This i
12035 73 20 6e 6f 74 20 61 20 72 65 61 6c 20 63 68 65 s not a real che
12036 63 6b 73 75 6d 2e 20 49 74 20 69 73 20 72 65 61 cksum. It is rea
12037 6c 6c 79 20 6a 75 73 74 20 74 68 65 20 73 75 6d lly just the sum
12038 20 6f 66 20 74 68 65 20 0a 2a 2a 20 72 61 6e 64 of the .** rand
12039 6f 6d 20 69 6e 69 74 69 61 6c 20 76 61 6c 75 65 om initial value
1203a 20 28 70 50 61 67 65 72 2d 3e 63 6b 73 75 6d 49 (pPager->cksumI
1203b 6e 69 74 29 20 61 6e 64 20 65 76 65 72 79 20 32 nit) and every 2
1203c 30 30 74 68 20 62 79 74 65 0a 2a 2a 20 6f 66 20 00th byte.** of
1203d 74 68 65 20 70 61 67 65 20 64 61 74 61 2c 20 73 the page data, s
1203e 74 61 72 74 69 6e 67 20 77 69 74 68 20 62 79 74 tarting with byt
1203f 65 20 6f 66 66 73 65 74 20 28 70 50 61 67 65 72 e offset (pPager
12040 2d 3e 70 61 67 65 53 69 7a 65 25 32 30 30 29 2e ->pageSize%200).
12041 0a 2a 2a 20 45 61 63 68 20 62 79 74 65 20 69 73 .** Each byte is
12042 20 69 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 interpreted as
12043 61 6e 20 38 2d 62 69 74 20 75 6e 73 69 67 6e 65 an 8-bit unsigne
12044 64 20 69 6e 74 65 67 65 72 2e 0a 2a 2a 0a 2a 2a d integer..**.**
12045 20 43 68 61 6e 67 69 6e 67 20 74 68 65 20 66 6f Changing the fo
12046 72 6d 75 6c 61 20 75 73 65 64 20 74 6f 20 63 6f rmula used to co
12047 6d 70 75 74 65 20 74 68 69 73 20 63 68 65 63 6b mpute this check
12048 73 75 6d 20 72 65 73 75 6c 74 73 20 69 6e 20 61 sum results in a
12049 6e 0a 2a 2a 20 69 6e 63 6f 6d 70 61 74 69 62 6c n.** incompatibl
1204a 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 e journal file f
1204b 6f 72 6d 61 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 ormat..**.** If
1204c 6a 6f 75 72 6e 61 6c 20 63 6f 72 72 75 70 74 69 journal corrupti
1204d 6f 6e 20 6f 63 63 75 72 73 20 64 75 65 20 74 6f on occurs due to
1204e 20 61 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 a power failure
1204f 2c 20 74 68 65 20 6d 6f 73 74 20 6c 69 6b 65 6c , the most likel
12050 79 20 0a 2a 2a 20 73 63 65 6e 61 72 69 6f 20 69 y .** scenario i
12051 73 20 74 68 61 74 20 6f 6e 65 20 65 6e 64 20 6f s that one end o
12052 72 20 74 68 65 20 6f 74 68 65 72 20 6f 66 20 74 r the other of t
12053 68 65 20 72 65 63 6f 72 64 20 77 69 6c 6c 20 62 he record will b
12054 65 20 63 68 61 6e 67 65 64 2e 20 0a 2a 2a 20 49 e changed. .** I
12055 74 20 69 73 20 6d 75 63 68 20 6c 65 73 73 20 6c t is much less l
12056 69 6b 65 6c 79 20 74 68 61 74 20 74 68 65 20 74 ikely that the t
12057 77 6f 20 65 6e 64 73 20 6f 66 20 74 68 65 20 6a wo ends of the j
12058 6f 75 72 6e 61 6c 20 72 65 63 6f 72 64 20 77 69 ournal record wi
12059 6c 6c 20 62 65 0a 2a 2a 20 63 6f 72 72 65 63 74 ll be.** correct
1205a 20 61 6e 64 20 74 68 65 20 6d 69 64 64 6c 65 20 and the middle
1205b 62 65 20 63 6f 72 72 75 70 74 2e 20 20 54 68 75 be corrupt. Thu
1205c 73 2c 20 74 68 69 73 20 22 63 68 65 63 6b 73 75 s, this "checksu
1205d 6d 22 20 73 63 68 65 6d 65 2c 0a 2a 2a 20 74 68 m" scheme,.** th
1205e 6f 75 67 68 20 66 61 73 74 20 61 6e 64 20 73 69 ough fast and si
1205f 6d 70 6c 65 2c 20 63 61 74 63 68 65 73 20 74 68 mple, catches th
12060 65 20 6d 6f 73 74 6c 79 20 6c 69 6b 65 6c 79 20 e mostly likely
12061 6b 69 6e 64 20 6f 66 20 63 6f 72 72 75 70 74 69 kind of corrupti
12062 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 75 33 on..*/.static u3
12063 32 20 70 61 67 65 72 5f 63 6b 73 75 6d 28 50 61 2 pager_cksum(Pa
12064 67 65 72 20 2a 70 50 61 67 65 72 2c 20 63 6f 6e ger *pPager, con
12065 73 74 20 75 38 20 2a 61 44 61 74 61 29 7b 0a 20 st u8 *aData){.
12066 20 75 33 32 20 63 6b 73 75 6d 20 3d 20 70 50 61 u32 cksum = pPa
12067 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 3b 20 ger->cksumInit;
12068 20 20 20 20 20 20 20 20 2f 2a 20 43 68 65 63 6b /* Check
12069 73 75 6d 20 76 61 6c 75 65 20 74 6f 20 72 65 74 sum value to ret
1206a 75 72 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 20 3d urn */. int i =
1206b 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a pPager->pageSiz
1206c 65 2d 32 30 30 3b 20 20 20 20 20 20 20 20 20 20 e-200;
1206d 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 /* Loop counter
1206e 2a 2f 0a 20 20 77 68 69 6c 65 28 20 69 3e 30 20 */. while( i>0
1206f 29 7b 0a 20 20 20 20 63 6b 73 75 6d 20 2b 3d 20 ){. cksum +=
12070 61 44 61 74 61 5b 69 5d 3b 0a 20 20 20 20 69 20 aData[i];. i
12071 2d 3d 20 32 30 30 3b 0a 20 20 7d 0a 20 20 72 65 -= 200;. }. re
12072 74 75 72 6e 20 63 6b 73 75 6d 3b 0a 7d 0a 0a 2f turn cksum;.}../
12073 2a 0a 2a 2a 20 52 65 61 64 20 61 20 73 69 6e 67 *.** Read a sing
12074 6c 65 20 70 61 67 65 20 66 72 6f 6d 20 65 69 74 le page from eit
12075 68 65 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 her the journal
12076 66 69 6c 65 20 28 69 66 20 69 73 4d 61 69 6e 4a file (if isMainJ
12077 72 6e 6c 3d 3d 31 29 20 6f 72 0a 2a 2a 20 66 72 rnl==1) or.** fr
12078 6f 6d 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e om the sub-journ
12079 61 6c 20 28 69 66 20 69 73 4d 61 69 6e 4a 72 6e al (if isMainJrn
1207a 6c 3d 3d 30 29 20 61 6e 64 20 70 6c 61 79 62 61 l==0) and playba
1207b 63 6b 20 74 68 61 74 20 70 61 67 65 2e 0a 2a 2a ck that page..**
1207c 20 54 68 65 20 70 61 67 65 20 62 65 67 69 6e 73 The page begins
1207d 20 61 74 20 6f 66 66 73 65 74 20 2a 70 4f 66 66 at offset *pOff
1207e 73 65 74 20 69 6e 74 6f 20 74 68 65 20 66 69 6c set into the fil
1207f 65 2e 20 54 68 65 20 2a 70 4f 66 66 73 65 74 0a e. The *pOffset.
12080 2a 2a 20 76 61 6c 75 65 20 69 73 20 69 6e 63 72 ** value is incr
12081 65 61 73 65 64 20 74 6f 20 74 68 65 20 73 74 61 eased to the sta
12082 72 74 20 6f 66 20 74 68 65 20 6e 65 78 74 20 70 rt of the next p
12083 61 67 65 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e age in the journ
12084 61 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 73 al..**.** The is
12085 4d 61 69 6e 4a 72 6e 6c 20 66 6c 61 67 20 69 73 MainJrnl flag is
12086 20 74 72 75 65 20 69 66 20 74 68 69 73 20 69 73 true if this is
12087 20 74 68 65 20 6d 61 69 6e 20 72 6f 6c 6c 62 61 the main rollba
12088 63 6b 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 0a 2a ck journal and.*
12089 2a 20 66 61 6c 73 65 20 66 6f 72 20 74 68 65 20 * false for the
1208a 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 statement journa
1208b 6c 2e 20 20 54 68 65 20 6d 61 69 6e 20 72 6f 6c l. The main rol
1208c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 75 73 lback journal us
1208d 65 73 0a 2a 2a 20 63 68 65 63 6b 73 75 6d 73 20 es.** checksums
1208e 2d 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 - the statement
1208f 6a 6f 75 72 6e 61 6c 20 64 6f 65 73 20 6e 6f 74 journal does not
12090 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 ..**.** If the p
12091 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 age number of th
12092 65 20 70 61 67 65 20 72 65 63 6f 72 64 20 72 65 e page record re
12093 61 64 20 66 72 6f 6d 20 74 68 65 20 28 73 75 62 ad from the (sub
12094 2d 29 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 2a -)journal file.*
12095 2a 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 * is greater tha
12096 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 76 61 n the current va
12097 6c 75 65 20 6f 66 20 50 61 67 65 72 2e 64 62 53 lue of Pager.dbS
12098 69 7a 65 2c 20 74 68 65 6e 20 70 6c 61 79 62 61 ize, then playba
12099 63 6b 20 69 73 0a 2a 2a 20 73 6b 69 70 70 65 64 ck is.** skipped
1209a 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20 69 and SQLITE_OK i
1209b 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a s returned..**.*
1209c 2a 20 49 66 20 70 44 6f 6e 65 20 69 73 20 6e 6f * If pDone is no
1209d 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 69 74 20 t NULL, then it
1209e 69 73 20 61 20 72 65 63 6f 72 64 20 6f 66 20 70 is a record of p
1209f 61 67 65 73 20 74 68 61 74 20 68 61 76 65 20 61 ages that have a
120a0 6c 72 65 61 64 79 0a 2a 2a 20 62 65 65 6e 20 70 lready.** been p
120a1 6c 61 79 65 64 20 62 61 63 6b 2e 20 20 49 66 20 layed back. If
120a2 74 68 65 20 70 61 67 65 20 61 74 20 2a 70 4f 66 the page at *pOf
120a3 66 73 65 74 20 68 61 73 20 61 6c 72 65 61 64 79 fset has already
120a4 20 62 65 65 6e 20 70 6c 61 79 65 64 20 62 61 63 been played bac
120a5 6b 0a 2a 2a 20 28 69 66 20 74 68 65 20 63 6f 72 k.** (if the cor
120a6 72 65 73 70 6f 6e 64 69 6e 67 20 70 44 6f 6e 65 responding pDone
120a7 20 62 69 74 20 69 73 20 73 65 74 29 20 74 68 65 bit is set) the
120a8 6e 20 73 6b 69 70 20 74 68 65 20 70 6c 61 79 62 n skip the playb
120a9 61 63 6b 2e 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 ack..** Make sur
120aa 65 20 74 68 65 20 70 44 6f 6e 65 20 62 69 74 20 e the pDone bit
120ab 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f corresponding to
120ac 20 74 68 65 20 2a 70 4f 66 66 73 65 74 20 70 61 the *pOffset pa
120ad 67 65 20 69 73 20 73 65 74 0a 2a 2a 20 70 72 69 ge is set.** pri
120ae 6f 72 20 74 6f 20 72 65 74 75 72 6e 69 6e 67 2e or to returning.
120af 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 .**.** If the pa
120b0 67 65 20 72 65 63 6f 72 64 20 69 73 20 73 75 63 ge record is suc
120b1 63 65 73 73 66 75 6c 6c 79 20 72 65 61 64 20 66 cessfully read f
120b2 72 6f 6d 20 74 68 65 20 28 73 75 62 2d 29 6a 6f rom the (sub-)jo
120b3 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 61 6e urnal file.** an
120b4 64 20 70 6c 61 79 65 64 20 62 61 63 6b 2c 20 74 d played back, t
120b5 68 65 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 hen SQLITE_OK is
120b6 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20 61 6e returned. If an
120b7 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 IO error occurs
120b8 0a 2a 2a 20 77 68 69 6c 65 20 72 65 61 64 69 6e .** while readin
120b9 67 20 74 68 65 20 72 65 63 6f 72 64 20 66 72 6f g the record fro
120ba 6d 20 74 68 65 20 28 73 75 62 2d 29 6a 6f 75 72 m the (sub-)jour
120bb 6e 61 6c 20 66 69 6c 65 20 6f 72 20 77 68 69 6c nal file or whil
120bc 65 20 77 72 69 74 69 6e 67 0a 2a 2a 20 74 6f 20 e writing.** to
120bd 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
120be 65 2c 20 74 68 65 6e 20 74 68 65 20 49 4f 20 65 e, then the IO e
120bf 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 rror code is ret
120c0 75 72 6e 65 64 2e 20 49 66 20 64 61 74 61 0a 2a urned. If data.*
120c1 2a 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 6c * is successfull
120c2 79 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 y read from the
120c3 28 73 75 62 2d 29 6a 6f 75 72 6e 61 6c 20 66 69 (sub-)journal fi
120c4 6c 65 20 62 75 74 20 61 70 70 65 61 72 73 20 74 le but appears t
120c5 6f 20 62 65 0a 2a 2a 20 63 6f 72 72 75 70 74 65 o be.** corrupte
120c6 64 2c 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 69 d, SQLITE_DONE i
120c7 73 20 72 65 74 75 72 6e 65 64 2e 20 44 61 74 61 s returned. Data
120c8 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20 63 is considered c
120c9 6f 72 72 75 70 74 65 64 20 69 6e 0a 2a 2a 20 74 orrupted in.** t
120ca 77 6f 20 63 69 72 63 75 6d 73 74 61 6e 63 65 73 wo circumstances
120cb 3a 0a 2a 2a 20 0a 2a 2a 20 20 20 2a 20 49 66 20 :.** .** * If
120cc 74 68 65 20 72 65 63 6f 72 64 20 70 61 67 65 2d the record page-
120cd 6e 75 6d 62 65 72 20 69 73 20 69 6c 6c 65 67 61 number is illega
120ce 6c 20 28 30 20 6f 72 20 50 41 47 45 52 5f 4d 4a l (0 or PAGER_MJ
120cf 5f 50 47 4e 4f 29 2c 20 6f 72 0a 2a 2a 20 20 20 _PGNO), or.**
120d0 2a 20 49 66 20 74 68 65 20 72 65 63 6f 72 64 20 * If the record
120d1 69 73 20 62 65 69 6e 67 20 72 6f 6c 6c 65 64 20 is being rolled
120d2 62 61 63 6b 20 66 72 6f 6d 20 74 68 65 20 6d 61 back from the ma
120d3 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a in journal file.
120d4 2a 2a 20 20 20 20 20 61 6e 64 20 74 68 65 20 63 ** and the c
120d5 68 65 63 6b 73 75 6d 20 66 69 65 6c 64 20 64 6f hecksum field do
120d6 65 73 20 6e 6f 74 20 6d 61 74 63 68 20 74 68 65 es not match the
120d7 20 72 65 63 6f 72 64 20 63 6f 6e 74 65 6e 74 2e record content.
120d8 0a 2a 2a 0a 2a 2a 20 4e 65 69 74 68 65 72 20 6f .**.** Neither o
120d9 66 20 74 68 65 73 65 20 74 77 6f 20 73 63 65 6e f these two scen
120da 61 72 69 6f 73 20 61 72 65 20 70 6f 73 73 69 62 arios are possib
120db 6c 65 20 64 75 72 69 6e 67 20 61 20 73 61 76 65 le during a save
120dc 70 6f 69 6e 74 20 72 6f 6c 6c 62 61 63 6b 2e 0a point rollback..
120dd 2a 2a 0a 2a 2a 20 49 66 20 74 68 69 73 20 69 73 **.** If this is
120de 20 61 20 73 61 76 65 70 6f 69 6e 74 20 72 6f 6c a savepoint rol
120df 6c 62 61 63 6b 2c 20 74 68 65 6e 20 6d 65 6d 6f lback, then memo
120e0 72 79 20 6d 61 79 20 68 61 76 65 20 74 6f 20 62 ry may have to b
120e1 65 20 64 79 6e 61 6d 69 63 61 6c 6c 79 0a 2a 2a e dynamically.**
120e2 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 74 68 allocated by th
120e3 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20 49 66 20 is function. If
120e4 74 68 69 73 20 69 73 20 74 68 65 20 63 61 73 65 this is the case
120e5 20 61 6e 64 20 61 6e 20 61 6c 6c 6f 63 61 74 69 and an allocati
120e6 6f 6e 20 66 61 69 6c 73 2c 0a 2a 2a 20 53 51 4c on fails,.** SQL
120e7 49 54 45 5f 4e 4f 4d 45 4d 20 69 73 20 72 65 74 ITE_NOMEM is ret
120e8 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 urned..*/.static
120e9 20 69 6e 74 20 70 61 67 65 72 5f 70 6c 61 79 62 int pager_playb
120ea 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 0a 20 20 ack_one_page(.
120eb 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 20 Pager *pPager,
120ec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
120ed 20 54 68 65 20 70 61 67 65 72 20 62 65 69 6e 67 The pager being
120ee 20 70 6c 61 79 65 64 20 62 61 63 6b 20 2a 2f 0a played back */.
120ef 20 20 69 6e 74 20 69 73 4d 61 69 6e 4a 72 6e 6c int isMainJrnl
120f0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
120f1 2f 2a 20 31 20 2d 3e 20 6d 61 69 6e 20 6a 6f 75 /* 1 -> main jou
120f2 72 6e 61 6c 2e 20 30 20 2d 3e 20 73 75 62 2d 6a rnal. 0 -> sub-j
120f3 6f 75 72 6e 61 6c 2e 20 2a 2f 0a 20 20 69 6e 74 ournal. */. int
120f4 20 69 73 55 6e 73 79 6e 63 2c 20 20 20 20 20 20 isUnsync,
120f5 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 /* Tr
120f6 75 65 20 69 66 20 72 65 61 64 69 6e 67 20 66 72 ue if reading fr
120f7 6f 6d 20 75 6e 73 79 6e 63 65 64 20 6d 61 69 6e om unsynced main
120f8 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 69 36 journal */. i6
120f9 34 20 2a 70 4f 66 66 73 65 74 2c 20 20 20 20 20 4 *pOffset,
120fa 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f /* O
120fb 66 66 73 65 74 20 6f 66 20 72 65 63 6f 72 64 20 ffset of record
120fc 74 6f 20 70 6c 61 79 62 61 63 6b 20 2a 2f 0a 20 to playback */.
120fd 20 69 6e 74 20 69 73 53 61 76 65 70 6e 74 2c 20 int isSavepnt,
120fe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
120ff 2a 20 54 72 75 65 20 66 6f 72 20 61 20 73 61 76 * True for a sav
12100 65 70 6f 69 6e 74 20 72 6f 6c 6c 62 61 63 6b 20 epoint rollback
12101 2a 2f 0a 20 20 42 69 74 76 65 63 20 2a 70 44 6f */. Bitvec *pDo
12102 6e 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ne
12103 20 20 20 2f 2a 20 42 69 74 76 65 63 20 6f 66 20 /* Bitvec of
12104 70 61 67 65 73 20 61 6c 72 65 61 64 79 20 70 6c pages already pl
12105 61 79 65 64 20 62 61 63 6b 20 2a 2f 0a 29 7b 0a ayed back */.){.
12106 20 20 69 6e 74 20 72 63 3b 0a 20 20 50 67 48 64 int rc;. PgHd
12107 72 20 2a 70 50 67 3b 20 20 20 20 20 20 20 20 20 r *pPg;
12108 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 /* An
12109 65 78 69 73 74 69 6e 67 20 70 61 67 65 20 69 6e existing page in
1210a 20 74 68 65 20 63 61 63 68 65 20 2a 2f 0a 20 20 the cache */.
1210b 50 67 6e 6f 20 70 67 6e 6f 3b 20 20 20 20 20 20 Pgno pgno;
1210c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1210d 20 54 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 The page number
1210e 20 6f 66 20 61 20 70 61 67 65 20 69 6e 20 6a 6f of a page in jo
1210f 75 72 6e 61 6c 20 2a 2f 0a 20 20 75 33 32 20 63 urnal */. u32 c
12110 6b 73 75 6d 3b 20 20 20 20 20 20 20 20 20 20 20 ksum;
12111 20 20 20 20 20 20 20 20 20 2f 2a 20 43 68 65 63 /* Chec
12112 6b 73 75 6d 20 75 73 65 64 20 66 6f 72 20 73 61 ksum used for sa
12113 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 20 2a 2f nity checking */
12114 0a 20 20 75 38 20 2a 61 44 61 74 61 3b 20 20 20 . u8 *aData;
12115 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12116 20 2f 2a 20 54 65 6d 70 6f 72 61 72 79 20 73 74 /* Temporary st
12117 6f 72 61 67 65 20 66 6f 72 20 74 68 65 20 70 61 orage for the pa
12118 67 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f ge */. sqlite3_
12119 66 69 6c 65 20 2a 6a 66 64 3b 20 20 20 20 20 20 file *jfd;
1211a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 69 6c /* The fil
1211b 65 20 64 65 73 63 72 69 70 74 6f 72 20 66 6f 72 e descriptor for
1211c 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
1211d 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 e */.. assert(
1211e 28 69 73 4d 61 69 6e 4a 72 6e 6c 26 7e 31 29 3d (isMainJrnl&~1)=
1211f 3d 30 20 29 3b 20 20 20 20 20 20 2f 2a 20 69 73 =0 ); /* is
12120 4d 61 69 6e 4a 72 6e 6c 20 69 73 20 30 20 6f 72 MainJrnl is 0 or
12121 20 31 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 1 */. assert(
12122 28 69 73 53 61 76 65 70 6e 74 26 7e 31 29 3d 3d (isSavepnt&~1)==
12123 30 20 29 3b 20 20 20 20 20 20 20 2f 2a 20 69 73 0 ); /* is
12124 53 61 76 65 70 6e 74 20 69 73 20 30 20 6f 72 20 Savepnt is 0 or
12125 31 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 69 1 */. assert( i
12126 73 4d 61 69 6e 4a 72 6e 6c 20 7c 7c 20 70 44 6f sMainJrnl || pDo
12127 6e 65 20 29 3b 20 20 20 20 20 2f 2a 20 70 44 6f ne ); /* pDo
12128 6e 65 20 61 6c 77 61 79 73 20 75 73 65 64 20 6f ne always used o
12129 6e 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 73 20 2a n sub-journals *
1212a 2f 0a 20 20 61 73 73 65 72 74 28 20 69 73 53 61 /. assert( isSa
1212b 76 65 70 6e 74 20 7c 7c 20 70 44 6f 6e 65 3d 3d vepnt || pDone==
1212c 30 20 29 3b 20 20 20 2f 2a 20 70 44 6f 6e 65 20 0 ); /* pDone
1212d 6e 65 76 65 72 20 75 73 65 64 20 6f 6e 20 6e 6f never used on no
1212e 6e 2d 73 61 76 65 70 6f 69 6e 74 20 2a 2f 0a 0a n-savepoint */..
1212f 20 20 61 44 61 74 61 20 3d 20 28 75 38 2a 29 70 aData = (u8*)p
12130 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 Pager->pTmpSpace
12131 3b 0a 20 20 61 73 73 65 72 74 28 20 61 44 61 74 ;. assert( aDat
12132 61 20 29 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 a ); /*
12133 54 65 6d 70 20 73 74 6f 72 61 67 65 20 6d 75 73 Temp storage mus
12134 74 20 68 61 76 65 20 61 6c 72 65 61 64 79 20 62 t have already b
12135 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 2a 2f een allocated */
12136 0a 0a 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20 .. /* Read the
12137 70 61 67 65 20 6e 75 6d 62 65 72 20 61 6e 64 20 page number and
12138 70 61 67 65 20 64 61 74 61 20 66 72 6f 6d 20 74 page data from t
12139 68 65 20 6a 6f 75 72 6e 61 6c 20 6f 72 20 73 75 he journal or su
1213a 62 2d 6a 6f 75 72 6e 61 6c 0a 20 20 2a 2a 20 66 b-journal. ** f
1213b 69 6c 65 2e 20 52 65 74 75 72 6e 20 61 6e 20 65 ile. Return an e
1213c 72 72 6f 72 20 63 6f 64 65 20 74 6f 20 74 68 65 rror code to the
1213d 20 63 61 6c 6c 65 72 20 69 66 20 61 6e 20 49 4f caller if an IO
1213e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 0a 20 error occurs..
1213f 20 2a 2f 0a 20 20 6a 66 64 20 3d 20 69 73 4d 61 */. jfd = isMa
12140 69 6e 4a 72 6e 6c 20 3f 20 70 50 61 67 65 72 2d inJrnl ? pPager-
12141 3e 6a 66 64 20 3a 20 70 50 61 67 65 72 2d 3e 73 >jfd : pPager->s
12142 6a 66 64 3b 0a 20 20 72 63 20 3d 20 72 65 61 64 jfd;. rc = read
12143 33 32 62 69 74 73 28 6a 66 64 2c 20 2a 70 4f 66 32bits(jfd, *pOf
12144 66 73 65 74 2c 20 26 70 67 6e 6f 29 3b 0a 20 20 fset, &pgno);.
12145 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
12146 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 K ) return rc;.
12147 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 rc = sqlite3OsR
12148 65 61 64 28 6a 66 64 2c 20 61 44 61 74 61 2c 20 ead(jfd, aData,
12149 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 pPager->pageSize
1214a 2c 20 28 2a 70 4f 66 66 73 65 74 29 2b 34 29 3b , (*pOffset)+4);
1214b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 . if( rc!=SQLIT
1214c 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 E_OK ) return rc
1214d 3b 0a 20 20 2a 70 4f 66 66 73 65 74 20 2b 3d 20 ;. *pOffset +=
1214e 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 pPager->pageSize
1214f 20 2b 20 34 20 2b 20 69 73 4d 61 69 6e 4a 72 6e + 4 + isMainJrn
12150 6c 2a 34 3b 0a 0a 20 20 2f 2a 20 53 61 6e 69 74 l*4;.. /* Sanit
12151 79 20 63 68 65 63 6b 69 6e 67 20 6f 6e 20 74 68 y checking on th
12152 65 20 70 61 67 65 2e 20 20 54 68 69 73 20 69 73 e page. This is
12153 20 6d 6f 72 65 20 69 6d 70 6f 72 74 61 6e 74 20 more important
12154 74 68 61 74 20 49 20 6f 72 69 67 69 6e 61 6c 6c that I originall
12155 79 0a 20 20 2a 2a 20 74 68 6f 75 67 68 74 2e 20 y. ** thought.
12156 20 49 66 20 61 20 70 6f 77 65 72 20 66 61 69 6c If a power fail
12157 75 72 65 20 6f 63 63 75 72 73 20 77 68 69 6c 65 ure occurs while
12158 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 the journal is
12159 62 65 69 6e 67 20 77 72 69 74 74 65 6e 2c 0a 20 being written,.
1215a 20 2a 2a 20 69 74 20 63 6f 75 6c 64 20 63 61 75 ** it could cau
1215b 73 65 20 69 6e 76 61 6c 69 64 20 64 61 74 61 20 se invalid data
1215c 74 6f 20 62 65 20 77 72 69 74 74 65 6e 20 69 6e to be written in
1215d 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 to the journal.
1215e 20 57 65 20 6e 65 65 64 20 74 6f 0a 20 20 2a 2a We need to. **
1215f 20 64 65 74 65 63 74 20 74 68 69 73 20 69 6e 76 detect this inv
12160 61 6c 69 64 20 64 61 74 61 20 28 77 69 74 68 20 alid data (with
12161 68 69 67 68 20 70 72 6f 62 61 62 69 6c 69 74 79 high probability
12162 29 20 61 6e 64 20 69 67 6e 6f 72 65 20 69 74 2e ) and ignore it.
12163 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 67 6e 6f . */. if( pgno
12164 3d 3d 30 20 7c 7c 20 70 67 6e 6f 3d 3d 50 41 47 ==0 || pgno==PAG
12165 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 65 ER_MJ_PGNO(pPage
12166 72 29 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 r) ){. assert
12167 28 20 21 69 73 53 61 76 65 70 6e 74 20 29 3b 0a ( !isSavepnt );.
12168 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
12169 45 5f 44 4f 4e 45 3b 0a 20 20 7d 0a 20 20 69 66 E_DONE;. }. if
1216a 28 20 70 67 6e 6f 3e 28 50 67 6e 6f 29 70 50 61 ( pgno>(Pgno)pPa
1216b 67 65 72 2d 3e 64 62 53 69 7a 65 20 7c 7c 20 73 ger->dbSize || s
1216c 71 6c 69 74 65 33 42 69 74 76 65 63 54 65 73 74 qlite3BitvecTest
1216d 28 70 44 6f 6e 65 2c 20 70 67 6e 6f 29 20 29 7b (pDone, pgno) ){
1216e 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
1216f 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 TE_OK;. }. if(
12170 20 69 73 4d 61 69 6e 4a 72 6e 6c 20 29 7b 0a 20 isMainJrnl ){.
12171 20 20 20 72 63 20 3d 20 72 65 61 64 33 32 62 69 rc = read32bi
12172 74 73 28 6a 66 64 2c 20 28 2a 70 4f 66 66 73 65 ts(jfd, (*pOffse
12173 74 29 2d 34 2c 20 26 63 6b 73 75 6d 29 3b 0a 20 t)-4, &cksum);.
12174 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 if( rc ) retu
12175 72 6e 20 72 63 3b 0a 20 20 20 20 69 66 28 20 21 rn rc;. if( !
12176 69 73 53 61 76 65 70 6e 74 20 26 26 20 70 61 67 isSavepnt && pag
12177 65 72 5f 63 6b 73 75 6d 28 70 50 61 67 65 72 2c er_cksum(pPager,
12178 20 61 44 61 74 61 29 21 3d 63 6b 73 75 6d 20 29 aData)!=cksum )
12179 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 {. return S
1217a 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 QLITE_DONE;.
1217b 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 44 6f }. }.. if( pDo
1217c 6e 65 20 26 26 20 28 72 63 20 3d 20 73 71 6c 69 ne && (rc = sqli
1217d 74 65 33 42 69 74 76 65 63 53 65 74 28 70 44 6f te3BitvecSet(pDo
1217e 6e 65 2c 20 70 67 6e 6f 29 29 21 3d 53 51 4c 49 ne, pgno))!=SQLI
1217f 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 TE_OK ){. ret
12180 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 61 urn rc;. }.. a
12181 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 ssert( pPager->s
12182 74 61 74 65 3d 3d 50 41 47 45 52 5f 52 45 53 45 tate==PAGER_RESE
12183 52 56 45 44 20 7c 7c 20 70 50 61 67 65 72 2d 3e RVED || pPager->
12184 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 45 58 43 state>=PAGER_EXC
12185 4c 55 53 49 56 45 20 29 3b 0a 0a 20 20 2f 2a 20 LUSIVE );.. /*
12186 49 66 20 74 68 65 20 70 61 67 65 72 20 69 73 20 If the pager is
12187 69 6e 20 52 45 53 45 52 56 45 44 20 73 74 61 74 in RESERVED stat
12188 65 2c 20 74 68 65 6e 20 74 68 65 72 65 20 6d 75 e, then there mu
12189 73 74 20 62 65 20 61 20 63 6f 70 79 20 6f 66 20 st be a copy of
1218a 74 68 69 73 0a 20 20 2a 2a 20 70 61 67 65 20 69 this. ** page i
1218b 6e 20 74 68 65 20 70 61 67 65 72 20 63 61 63 68 n the pager cach
1218c 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 e. In this case
1218d 6a 75 73 74 20 75 70 64 61 74 65 20 74 68 65 20 just update the
1218e 70 61 67 65 72 20 63 61 63 68 65 2c 0a 20 20 2a pager cache,. *
1218f 2a 20 6e 6f 74 20 74 68 65 20 64 61 74 61 62 61 * not the databa
12190 73 65 20 66 69 6c 65 2e 20 54 68 65 20 70 61 67 se file. The pag
12191 65 20 69 73 20 6c 65 66 74 20 6d 61 72 6b 65 64 e is left marked
12192 20 64 69 72 74 79 20 69 6e 20 74 68 69 73 20 63 dirty in this c
12193 61 73 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 ase.. **. ** A
12194 6e 20 65 78 63 65 70 74 69 6f 6e 20 74 6f 20 74 n exception to t
12195 68 65 20 61 62 6f 76 65 20 72 75 6c 65 3a 20 49 he above rule: I
12196 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 f the database i
12197 73 20 69 6e 20 6e 6f 2d 73 79 6e 63 20 6d 6f 64 s in no-sync mod
12198 65 0a 20 20 2a 2a 20 61 6e 64 20 61 20 70 61 67 e. ** and a pag
12199 65 20 69 73 20 6d 6f 76 65 64 20 64 75 72 69 6e e is moved durin
1219a 67 20 61 6e 20 69 6e 63 72 65 6d 65 6e 74 61 6c g an incremental
1219b 20 76 61 63 75 75 6d 20 74 68 65 6e 20 74 68 65 vacuum then the
1219c 20 70 61 67 65 20 6d 61 79 0a 20 20 2a 2a 20 6e page may. ** n
1219d 6f 74 20 62 65 20 69 6e 20 74 68 65 20 70 61 67 ot be in the pag
1219e 65 72 20 63 61 63 68 65 2e 20 4c 61 74 65 72 3a er cache. Later:
1219f 20 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 6f if a malloc() o
121a0 72 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 r IO error occur
121a1 73 0a 20 20 2a 2a 20 64 75 72 69 6e 67 20 61 20 s. ** during a
121a2 4d 6f 76 65 70 61 67 65 28 29 20 63 61 6c 6c 2c Movepage() call,
121a3 20 74 68 65 6e 20 74 68 65 20 70 61 67 65 20 6d then the page m
121a4 61 79 20 6e 6f 74 20 62 65 20 69 6e 20 74 68 65 ay not be in the
121a5 20 63 61 63 68 65 0a 20 20 2a 2a 20 65 69 74 68 cache. ** eith
121a6 65 72 2e 20 53 6f 20 74 68 65 20 63 6f 6e 64 69 er. So the condi
121a7 74 69 6f 6e 20 64 65 73 63 72 69 62 65 64 20 69 tion described i
121a8 6e 20 74 68 65 20 61 62 6f 76 65 20 70 61 72 61 n the above para
121a9 67 72 61 70 68 20 69 73 20 6e 6f 74 0a 20 20 2a graph is not. *
121aa 2a 20 61 73 73 65 72 74 28 29 61 62 6c 65 2e 0a * assert()able..
121ab 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 69 6e 20 **. ** If in
121ac 45 58 43 4c 55 53 49 56 45 20 73 74 61 74 65 2c EXCLUSIVE state,
121ad 20 74 68 65 6e 20 77 65 20 75 70 64 61 74 65 20 then we update
121ae 74 68 65 20 70 61 67 65 72 20 63 61 63 68 65 20 the pager cache
121af 69 66 20 69 74 20 65 78 69 73 74 73 0a 20 20 2a if it exists. *
121b0 2a 20 61 6e 64 20 74 68 65 20 6d 61 69 6e 20 66 * and the main f
121b1 69 6c 65 2e 20 54 68 65 20 70 61 67 65 20 69 73 ile. The page is
121b2 20 74 68 65 6e 20 6d 61 72 6b 65 64 20 6e 6f 74 then marked not
121b3 20 64 69 72 74 79 2e 0a 20 20 2a 2a 0a 20 20 2a dirty.. **. *
121b4 2a 20 54 69 63 6b 65 74 20 23 31 31 37 31 3a 20 * Ticket #1171:
121b5 20 54 68 65 20 73 74 61 74 65 6d 65 6e 74 20 6a The statement j
121b6 6f 75 72 6e 61 6c 20 6d 69 67 68 74 20 63 6f 6e ournal might con
121b7 74 61 69 6e 20 70 61 67 65 20 63 6f 6e 74 65 6e tain page conten
121b8 74 20 74 68 61 74 20 69 73 0a 20 20 2a 2a 20 64 t that is. ** d
121b9 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 74 68 ifferent from th
121ba 65 20 70 61 67 65 20 63 6f 6e 74 65 6e 74 20 61 e page content a
121bb 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 t the start of t
121bc 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a he transaction..
121bd 20 20 2a 2a 20 54 68 69 73 20 6f 63 63 75 72 73 ** This occurs
121be 20 77 68 65 6e 20 61 20 70 61 67 65 20 69 73 20 when a page is
121bf 63 68 61 6e 67 65 64 20 70 72 69 6f 72 20 74 6f changed prior to
121c0 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 61 20 the start of a
121c1 73 74 61 74 65 6d 65 6e 74 0a 20 20 2a 2a 20 74 statement. ** t
121c2 68 65 6e 20 63 68 61 6e 67 65 64 20 61 67 61 69 hen changed agai
121c3 6e 20 77 69 74 68 69 6e 20 74 68 65 20 73 74 61 n within the sta
121c4 74 65 6d 65 6e 74 2e 20 20 57 68 65 6e 20 72 6f tement. When ro
121c5 6c 6c 69 6e 67 20 62 61 63 6b 20 73 75 63 68 20 lling back such
121c6 61 0a 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 a. ** statement
121c7 20 77 65 20 6d 75 73 74 20 6e 6f 74 20 77 72 69 we must not wri
121c8 74 65 20 74 6f 20 74 68 65 20 6f 72 69 67 69 6e te to the origin
121c9 61 6c 20 64 61 74 61 62 61 73 65 20 75 6e 6c 65 al database unle
121ca 73 73 20 77 65 20 6b 6e 6f 77 0a 20 20 2a 2a 20 ss we know. **
121cb 66 6f 72 20 63 65 72 74 61 69 6e 20 74 68 61 74 for certain that
121cc 20 6f 72 69 67 69 6e 61 6c 20 70 61 67 65 20 63 original page c
121cd 6f 6e 74 65 6e 74 73 20 61 72 65 20 73 79 6e 63 ontents are sync
121ce 65 64 20 69 6e 74 6f 20 74 68 65 20 6d 61 69 6e ed into the main
121cf 20 72 6f 6c 6c 62 61 63 6b 0a 20 20 2a 2a 20 6a rollback. ** j
121d0 6f 75 72 6e 61 6c 2e 20 20 4f 74 68 65 72 77 69 ournal. Otherwi
121d1 73 65 2c 20 61 20 70 6f 77 65 72 20 6c 6f 73 73 se, a power loss
121d2 20 6d 69 67 68 74 20 6c 65 61 76 65 20 6d 6f 64 might leave mod
121d3 69 66 69 65 64 20 64 61 74 61 20 69 6e 20 74 68 ified data in th
121d4 65 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 e. ** database
121d5 66 69 6c 65 20 77 69 74 68 6f 75 74 20 61 6e 20 file without an
121d6 65 6e 74 72 79 20 69 6e 20 74 68 65 20 72 6f 6c entry in the rol
121d7 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 74 68 lback journal th
121d8 61 74 20 63 61 6e 0a 20 20 2a 2a 20 72 65 73 74 at can. ** rest
121d9 6f 72 65 20 74 68 65 20 64 61 74 61 62 61 73 65 ore the database
121da 20 74 6f 20 69 74 73 20 6f 72 69 67 69 6e 61 6c to its original
121db 20 66 6f 72 6d 2e 20 20 54 77 6f 20 63 6f 6e 64 form. Two cond
121dc 69 74 69 6f 6e 73 20 6d 75 73 74 20 62 65 0a 20 itions must be.
121dd 20 2a 2a 20 6d 65 74 20 62 65 66 6f 72 65 20 77 ** met before w
121de 72 69 74 69 6e 67 20 74 6f 20 74 68 65 20 64 61 riting to the da
121df 74 61 62 61 73 65 20 66 69 6c 65 73 2e 20 28 31 tabase files. (1
121e0 29 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6d ) the database m
121e1 75 73 74 20 62 65 0a 20 20 2a 2a 20 6c 6f 63 6b ust be. ** lock
121e2 65 64 2e 20 20 28 32 29 20 77 65 20 6b 6e 6f 77 ed. (2) we know
121e3 20 74 68 61 74 20 74 68 65 20 6f 72 69 67 69 6e that the origin
121e4 61 6c 20 70 61 67 65 20 63 6f 6e 74 65 6e 74 20 al page content
121e5 69 73 20 66 75 6c 6c 79 20 73 79 6e 63 65 64 0a is fully synced.
121e6 20 20 2a 2a 20 69 6e 20 74 68 65 20 6d 61 69 6e ** in the main
121e7 20 6a 6f 75 72 6e 61 6c 20 65 69 74 68 65 72 20 journal either
121e8 62 65 63 61 75 73 65 20 74 68 65 20 70 61 67 65 because the page
121e9 20 69 73 20 6e 6f 74 20 69 6e 20 63 61 63 68 65 is not in cache
121ea 20 6f 72 20 65 6c 73 65 0a 20 20 2a 2a 20 74 68 or else. ** th
121eb 65 20 70 61 67 65 20 69 73 20 6d 61 72 6b 65 64 e page is marked
121ec 20 61 73 20 6e 65 65 64 53 79 6e 63 3d 3d 30 2e as needSync==0.
121ed 0a 20 20 2a 2a 0a 20 20 2a 2a 20 32 30 30 38 2d . **. ** 2008-
121ee 30 34 2d 31 34 3a 20 20 57 68 65 6e 20 61 74 74 04-14: When att
121ef 65 6d 70 74 69 6e 67 20 74 6f 20 76 61 63 75 75 empting to vacuu
121f0 6d 20 61 20 63 6f 72 72 75 70 74 20 64 61 74 61 m a corrupt data
121f1 62 61 73 65 20 66 69 6c 65 2c 20 69 74 0a 20 20 base file, it.
121f2 2a 2a 20 69 73 20 70 6f 73 73 69 62 6c 65 20 74 ** is possible t
121f3 6f 20 66 61 69 6c 20 61 20 73 74 61 74 65 6d 65 o fail a stateme
121f4 6e 74 20 6f 6e 20 61 20 64 61 74 61 62 61 73 65 nt on a database
121f5 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 79 that does not y
121f6 65 74 20 65 78 69 73 74 2e 0a 20 20 2a 2a 20 44 et exist.. ** D
121f7 6f 20 6e 6f 74 20 61 74 74 65 6d 70 74 20 74 6f o not attempt to
121f8 20 77 72 69 74 65 20 69 66 20 64 61 74 61 62 61 write if databa
121f9 73 65 20 66 69 6c 65 20 68 61 73 20 6e 65 76 65 se file has neve
121fa 72 20 62 65 65 6e 20 6f 70 65 6e 65 64 2e 0a 20 r been opened..
121fb 20 2a 2f 0a 20 20 70 50 67 20 3d 20 70 61 67 65 */. pPg = page
121fc 72 5f 6c 6f 6f 6b 75 70 28 70 50 61 67 65 72 2c r_lookup(pPager,
121fd 20 70 67 6e 6f 29 3b 0a 20 20 61 73 73 65 72 74 pgno);. assert
121fe 28 20 70 50 67 20 7c 7c 20 21 4d 45 4d 44 42 20 ( pPg || !MEMDB
121ff 29 3b 0a 20 20 50 41 47 45 52 54 52 41 43 45 28 );. PAGERTRACE(
12200 28 22 50 4c 41 59 42 41 43 4b 20 25 64 20 70 61 ("PLAYBACK %d pa
12201 67 65 20 25 64 20 68 61 73 68 28 25 30 38 78 29 ge %d hash(%08x)
12202 20 25 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 %s\n",.
12203 20 20 20 20 20 20 20 50 41 47 45 52 49 44 28 70 PAGERID(p
12204 50 61 67 65 72 29 2c 20 70 67 6e 6f 2c 20 70 61 Pager), pgno, pa
12205 67 65 72 5f 64 61 74 61 68 61 73 68 28 70 50 61 ger_datahash(pPa
12206 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 61 ger->pageSize, a
12207 44 61 74 61 29 2c 0a 20 20 20 20 20 20 20 20 20 Data),.
12208 20 20 20 20 20 20 28 69 73 4d 61 69 6e 4a 72 6e (isMainJrn
12209 6c 3f 22 6d 61 69 6e 2d 6a 6f 75 72 6e 61 6c 22 l?"main-journal"
1220a 3a 22 73 75 62 2d 6a 6f 75 72 6e 61 6c 22 29 0a :"sub-journal").
1220b 20 20 29 29 3b 0a 20 20 69 66 28 20 28 70 50 61 ));. if( (pPa
1220c 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 ger->state>=PAGE
1220d 52 5f 45 58 43 4c 55 53 49 56 45 29 0a 20 20 20 R_EXCLUSIVE).
1220e 26 26 20 28 70 50 67 3d 3d 30 20 7c 7c 20 30 3d && (pPg==0 || 0=
1220f 3d 28 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 =(pPg->flags&PGH
12210 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 29 0a 20 DR_NEED_SYNC)).
12211 20 20 26 26 20 69 73 4f 70 65 6e 28 70 50 61 67 && isOpen(pPag
12212 65 72 2d 3e 66 64 29 0a 20 20 20 26 26 20 21 69 er->fd). && !i
12213 73 55 6e 73 79 6e 63 0a 20 20 29 7b 0a 20 20 20 sUnsync. ){.
12214 20 69 36 34 20 6f 66 73 74 20 3d 20 28 70 67 6e i64 ofst = (pgn
12215 6f 2d 31 29 2a 28 69 36 34 29 70 50 61 67 65 72 o-1)*(i64)pPager
12216 2d 3e 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 ->pageSize;.
12217 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 rc = sqlite3OsWr
12218 69 74 65 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 ite(pPager->fd,
12219 61 44 61 74 61 2c 20 70 50 61 67 65 72 2d 3e 70 aData, pPager->p
1221a 61 67 65 53 69 7a 65 2c 20 6f 66 73 74 29 3b 0a ageSize, ofst);.
1221b 20 20 20 20 69 66 28 20 70 67 6e 6f 3e 70 50 61 if( pgno>pPa
1221c 67 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a 65 20 ger->dbFileSize
1221d 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d ){. pPager-
1221e 3e 64 62 46 69 6c 65 53 69 7a 65 20 3d 20 70 67 >dbFileSize = pg
1221f 6e 6f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 no;. }. if
12220 28 20 70 50 61 67 65 72 2d 3e 70 42 61 63 6b 75 ( pPager->pBacku
12221 70 20 29 7b 0a 20 20 20 20 20 20 43 4f 44 45 43 p ){. CODEC
12222 31 28 70 50 61 67 65 72 2c 20 61 44 61 74 61 2c 1(pPager, aData,
12223 20 70 67 6e 6f 2c 20 33 2c 20 72 63 3d 53 51 4c pgno, 3, rc=SQL
12224 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 20 20 20 20 ITE_NOMEM);.
12225 20 20 73 71 6c 69 74 65 33 42 61 63 6b 75 70 55 sqlite3BackupU
12226 70 64 61 74 65 28 70 50 61 67 65 72 2d 3e 70 42 pdate(pPager->pB
12227 61 63 6b 75 70 2c 20 70 67 6e 6f 2c 20 61 44 61 ackup, pgno, aDa
12228 74 61 29 3b 0a 20 20 20 20 20 20 43 4f 44 45 43 ta);. CODEC
12229 31 28 70 50 61 67 65 72 2c 20 61 44 61 74 61 2c 1(pPager, aData,
1222a 20 70 67 6e 6f 2c 20 30 2c 20 72 63 3d 53 51 4c pgno, 0, rc=SQL
1222b 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 20 20 20 20 ITE_NOMEM);.
1222c 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 21 69 }. }else if( !i
1222d 73 4d 61 69 6e 4a 72 6e 6c 20 26 26 20 70 50 67 sMainJrnl && pPg
1222e 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 ==0 ){. /* If
1222f 20 74 68 69 73 20 69 73 20 61 20 72 6f 6c 6c 62 this is a rollb
12230 61 63 6b 20 6f 66 20 61 20 73 61 76 65 70 6f 69 ack of a savepoi
12231 6e 74 20 61 6e 64 20 64 61 74 61 20 77 61 73 20 nt and data was
12232 6e 6f 74 20 77 72 69 74 74 65 6e 20 74 6f 0a 20 not written to.
12233 20 20 20 2a 2a 20 74 68 65 20 64 61 74 61 62 61 ** the databa
12234 73 65 20 61 6e 64 20 74 68 65 20 70 61 67 65 20 se and the page
12235 69 73 20 6e 6f 74 20 69 6e 2d 6d 65 6d 6f 72 79 is not in-memory
12236 2c 20 74 68 65 72 65 20 69 73 20 61 20 70 6f 74 , there is a pot
12237 65 6e 74 69 61 6c 0a 20 20 20 20 2a 2a 20 70 72 ential. ** pr
12238 6f 62 6c 65 6d 2e 20 57 68 65 6e 20 74 68 65 20 oblem. When the
12239 70 61 67 65 20 69 73 20 6e 65 78 74 20 66 65 74 page is next fet
1223a 63 68 65 64 20 62 79 20 74 68 65 20 62 2d 74 72 ched by the b-tr
1223b 65 65 20 6c 61 79 65 72 2c 20 69 74 20 0a 20 20 ee layer, it .
1223c 20 20 2a 2a 20 77 69 6c 6c 20 62 65 20 72 65 61 ** will be rea
1223d 64 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 d from the datab
1223e 61 73 65 20 66 69 6c 65 2c 20 77 68 69 63 68 20 ase file, which
1223f 6d 61 79 20 6f 72 20 6d 61 79 20 6e 6f 74 20 62 may or may not b
12240 65 20 0a 20 20 20 20 2a 2a 20 63 75 72 72 65 6e e . ** curren
12241 74 2e 20 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a t. . **. *
12242 2a 20 54 68 65 72 65 20 61 72 65 20 61 20 63 6f * There are a co
12243 75 70 6c 65 20 6f 66 20 64 69 66 66 65 72 65 6e uple of differen
12244 74 20 77 61 79 73 20 74 68 69 73 20 63 61 6e 20 t ways this can
12245 68 61 70 70 65 6e 2e 20 41 6c 6c 20 61 72 65 20 happen. All are
12246 71 75 69 74 65 0a 20 20 20 20 2a 2a 20 6f 62 73 quite. ** obs
12247 63 75 72 65 2e 20 57 68 65 6e 20 72 75 6e 6e 69 cure. When runni
12248 6e 67 20 69 6e 20 73 79 6e 63 68 72 6f 6e 6f 75 ng in synchronou
12249 73 20 6d 6f 64 65 2c 20 74 68 69 73 20 63 61 6e s mode, this can
1224a 20 6f 6e 6c 79 20 68 61 70 70 65 6e 20 0a 20 20 only happen .
1224b 20 20 2a 2a 20 69 66 20 74 68 65 20 70 61 67 65 ** if the page
1224c 20 69 73 20 6f 6e 20 74 68 65 20 66 72 65 65 2d is on the free-
1224d 6c 69 73 74 20 61 74 20 74 68 65 20 73 74 61 72 list at the star
1224e 74 20 6f 66 20 74 68 65 20 74 72 61 6e 73 61 63 t of the transac
1224f 74 69 6f 6e 2c 20 74 68 65 6e 0a 20 20 20 20 2a tion, then. *
12250 2a 20 70 6f 70 75 6c 61 74 65 64 2c 20 74 68 65 * populated, the
12251 6e 20 6d 6f 76 65 64 20 75 73 69 6e 67 20 73 71 n moved using sq
12252 6c 69 74 65 33 50 61 67 65 72 4d 6f 76 65 70 61 lite3PagerMovepa
12253 67 65 28 29 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 ge().. **.
12254 20 2a 2a 20 54 68 65 20 73 6f 6c 75 74 69 6f 6e ** The solution
12255 20 69 73 20 74 6f 20 61 64 64 20 61 6e 20 69 6e is to add an in
12256 2d 6d 65 6d 6f 72 79 20 70 61 67 65 20 74 6f 20 -memory page to
12257 74 68 65 20 63 61 63 68 65 20 63 6f 6e 74 61 69 the cache contai
12258 6e 69 6e 67 0a 20 20 20 20 2a 2a 20 74 68 65 20 ning. ** the
12259 64 61 74 61 20 6a 75 73 74 20 72 65 61 64 20 66 data just read f
1225a 72 6f 6d 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 rom the sub-jour
1225b 6e 61 6c 2e 20 4d 61 72 6b 20 74 68 65 20 70 61 nal. Mark the pa
1225c 67 65 20 61 73 20 64 69 72 74 79 20 0a 20 20 20 ge as dirty .
1225d 20 2a 2a 20 61 6e 64 20 69 66 20 74 68 65 20 70 ** and if the p
1225e 61 67 65 72 20 72 65 71 75 69 72 65 73 20 61 20 ager requires a
1225f 6a 6f 75 72 6e 61 6c 2d 73 79 6e 63 2c 20 74 68 journal-sync, th
12260 65 6e 20 6d 61 72 6b 20 74 68 65 20 70 61 67 65 en mark the page
12261 20 61 73 20 0a 20 20 20 20 2a 2a 20 72 65 71 75 as . ** requ
12262 69 72 69 6e 67 20 61 20 6a 6f 75 72 6e 61 6c 2d iring a journal-
12263 73 79 6e 63 20 62 65 66 6f 72 65 20 69 74 20 69 sync before it i
12264 73 20 77 72 69 74 74 65 6e 2e 0a 20 20 20 20 2a s written.. *
12265 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 73 /. assert( is
12266 53 61 76 65 70 6e 74 20 29 3b 0a 20 20 20 20 69 Savepnt );. i
12267 66 28 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 f( (rc = sqlite3
12268 50 61 67 65 72 41 63 71 75 69 72 65 28 70 50 61 PagerAcquire(pPa
12269 67 65 72 2c 20 70 67 6e 6f 2c 20 26 70 50 67 2c ger, pgno, &pPg,
1226a 20 31 29 29 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 1))!=SQLITE_OK
1226b 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
1226c 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 rc;. }. pP
1226d 67 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 50 47 48 g->flags &= ~PGH
1226e 44 52 5f 4e 45 45 44 5f 52 45 41 44 3b 0a 20 20 DR_NEED_READ;.
1226f 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d sqlite3PcacheM
12270 61 6b 65 44 69 72 74 79 28 70 50 67 29 3b 0a 20 akeDirty(pPg);.
12271 20 7d 0a 20 20 69 66 28 20 70 50 67 20 29 7b 0a }. if( pPg ){.
12272 20 20 20 20 2f 2a 20 4e 6f 20 70 61 67 65 20 73 /* No page s
12273 68 6f 75 6c 64 20 65 76 65 72 20 62 65 20 65 78 hould ever be ex
12274 70 6c 69 63 69 74 6c 79 20 72 6f 6c 6c 65 64 20 plicitly rolled
12275 62 61 63 6b 20 74 68 61 74 20 69 73 20 69 6e 20 back that is in
12276 75 73 65 2c 20 65 78 63 65 70 74 0a 20 20 20 20 use, except.
12277 2a 2a 20 66 6f 72 20 70 61 67 65 20 31 20 77 68 ** for page 1 wh
12278 69 63 68 20 69 73 20 68 65 6c 64 20 69 6e 20 75 ich is held in u
12279 73 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6b se in order to k
1227a 65 65 70 20 74 68 65 20 6c 6f 63 6b 20 6f 6e 20 eep the lock on
1227b 74 68 65 0a 20 20 20 20 2a 2a 20 64 61 74 61 62 the. ** datab
1227c 61 73 65 20 61 63 74 69 76 65 2e 20 48 6f 77 65 ase active. Howe
1227d 76 65 72 20 73 75 63 68 20 61 20 70 61 67 65 20 ver such a page
1227e 6d 61 79 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 may be rolled ba
1227f 63 6b 20 61 73 20 61 20 72 65 73 75 6c 74 0a 20 ck as a result.
12280 20 20 20 2a 2a 20 6f 66 20 61 6e 20 69 6e 74 65 ** of an inte
12281 72 6e 61 6c 20 65 72 72 6f 72 20 72 65 73 75 6c rnal error resul
12282 74 69 6e 67 20 69 6e 20 61 6e 20 61 75 74 6f 6d ting in an autom
12283 61 74 69 63 20 63 61 6c 6c 20 74 6f 0a 20 20 20 atic call to.
12284 20 2a 2a 20 73 71 6c 69 74 65 33 50 61 67 65 72 ** sqlite3Pager
12285 52 6f 6c 6c 62 61 63 6b 28 29 2e 0a 20 20 20 20 Rollback()..
12286 2a 2f 0a 20 20 20 20 76 6f 69 64 20 2a 70 44 61 */. void *pDa
12287 74 61 3b 0a 20 20 20 20 70 44 61 74 61 20 3d 20 ta;. pData =
12288 70 50 67 2d 3e 70 44 61 74 61 3b 0a 20 20 20 20 pPg->pData;.
12289 6d 65 6d 63 70 79 28 70 44 61 74 61 2c 20 61 44 memcpy(pData, aD
1228a 61 74 61 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 ata, pPager->pag
1228b 65 53 69 7a 65 29 3b 0a 20 20 20 20 70 50 61 67 eSize);. pPag
1228c 65 72 2d 3e 78 52 65 69 6e 69 74 65 72 28 70 50 er->xReiniter(pP
1228d 67 29 3b 0a 20 20 20 20 69 66 28 20 69 73 4d 61 g);. if( isMa
1228e 69 6e 4a 72 6e 6c 20 26 26 20 28 21 69 73 53 61 inJrnl && (!isSa
1228f 76 65 70 6e 74 20 7c 7c 20 2a 70 4f 66 66 73 65 vepnt || *pOffse
12290 74 3c 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e t<=pPager->journ
12291 61 6c 48 64 72 29 20 29 7b 0a 20 20 20 20 20 20 alHdr) ){.
12292 2f 2a 20 49 66 20 74 68 65 20 63 6f 6e 74 65 6e /* If the conten
12293 74 73 20 6f 66 20 74 68 69 73 20 70 61 67 65 20 ts of this page
12294 77 65 72 65 20 6a 75 73 74 20 72 65 73 74 6f 72 were just restor
12295 65 64 20 66 72 6f 6d 20 74 68 65 20 6d 61 69 6e ed from the main
12296 20 0a 20 20 20 20 20 20 2a 2a 20 6a 6f 75 72 6e . ** journ
12297 61 6c 20 66 69 6c 65 2c 20 74 68 65 6e 20 69 74 al file, then it
12298 73 20 63 6f 6e 74 65 6e 74 20 6d 75 73 74 20 62 s content must b
12299 65 20 61 73 20 74 68 65 79 20 77 65 72 65 20 77 e as they were w
1229a 68 65 6e 20 74 68 65 20 0a 20 20 20 20 20 20 2a hen the . *
1229b 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 * transaction wa
1229c 73 20 66 69 72 73 74 20 6f 70 65 6e 65 64 2e 20 s first opened.
1229d 49 6e 20 74 68 69 73 20 63 61 73 65 20 77 65 20 In this case we
1229e 63 61 6e 20 6d 61 72 6b 20 74 68 65 20 70 61 67 can mark the pag
1229f 65 0a 20 20 20 20 20 20 2a 2a 20 61 73 20 63 6c e. ** as cl
122a0 65 61 6e 2c 20 73 69 6e 63 65 20 74 68 65 72 65 ean, since there
122a1 20 77 69 6c 6c 20 62 65 20 6e 6f 20 6e 65 65 64 will be no need
122a2 20 74 6f 20 77 72 69 74 65 20 69 74 20 6f 75 74 to write it out
122a3 20 74 6f 20 74 68 65 2e 0a 20 20 20 20 20 20 2a to the.. *
122a4 2a 0a 20 20 20 20 20 20 2a 2a 20 54 68 65 72 65 *. ** There
122a5 20 69 73 20 6f 6e 65 20 65 78 63 65 70 74 69 6f is one exceptio
122a6 6e 20 74 6f 20 74 68 69 73 20 72 75 6c 65 2e 20 n to this rule.
122a7 49 66 20 74 68 65 20 70 61 67 65 20 69 73 20 62 If the page is b
122a8 65 69 6e 67 20 72 6f 6c 6c 65 64 0a 20 20 20 20 eing rolled.
122a9 20 20 2a 2a 20 62 61 63 6b 20 61 73 20 70 61 72 ** back as par
122aa 74 20 6f 66 20 61 20 73 61 76 65 70 6f 69 6e 74 t of a savepoint
122ab 20 28 6f 72 20 73 74 61 74 65 6d 65 6e 74 29 20 (or statement)
122ac 72 6f 6c 6c 62 61 63 6b 20 66 72 6f 6d 20 61 6e rollback from an
122ad 20 0a 20 20 20 20 20 20 2a 2a 20 75 6e 73 79 6e . ** unsyn
122ae 63 65 64 20 70 6f 72 74 69 6f 6e 20 6f 66 20 74 ced portion of t
122af 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 he main journal
122b0 66 69 6c 65 2c 20 74 68 65 6e 20 69 74 20 69 73 file, then it is
122b1 20 6e 6f 74 20 73 61 66 65 0a 20 20 20 20 20 20 not safe.
122b2 2a 2a 20 74 6f 20 6d 61 72 6b 20 74 68 65 20 70 ** to mark the p
122b3 61 67 65 20 61 73 20 63 6c 65 61 6e 2e 20 54 68 age as clean. Th
122b4 69 73 20 69 73 20 62 65 63 61 75 73 65 20 6d 61 is is because ma
122b5 72 6b 69 6e 67 20 74 68 65 20 70 61 67 65 20 61 rking the page a
122b6 73 0a 20 20 20 20 20 20 2a 2a 20 63 6c 65 61 6e s. ** clean
122b7 20 77 69 6c 6c 20 63 6c 65 61 72 20 74 68 65 20 will clear the
122b8 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 PGHDR_NEED_SYNC
122b9 66 6c 61 67 2e 20 53 69 6e 63 65 20 74 68 65 20 flag. Since the
122ba 70 61 67 65 20 69 73 0a 20 20 20 20 20 20 2a 2a page is. **
122bb 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 already in the
122bc 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 28 72 65 journal file (re
122bd 63 6f 72 64 65 64 20 69 6e 20 50 61 67 65 72 2e corded in Pager.
122be 70 49 6e 4a 6f 75 72 6e 61 6c 29 20 61 6e 64 0a pInJournal) and.
122bf 20 20 20 20 20 20 2a 2a 20 74 68 65 20 50 47 48 ** the PGH
122c0 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 66 6c 61 DR_NEED_SYNC fla
122c1 67 20 69 73 20 63 6c 65 61 72 65 64 2c 20 69 66 g is cleared, if
122c2 20 74 68 65 20 70 61 67 65 20 69 73 20 77 72 69 the page is wri
122c3 74 74 65 6e 20 74 6f 0a 20 20 20 20 20 20 2a 2a tten to. **
122c4 20 61 67 61 69 6e 20 77 69 74 68 69 6e 20 74 68 again within th
122c5 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 is transaction,
122c6 69 74 20 77 69 6c 6c 20 62 65 20 6d 61 72 6b 65 it will be marke
122c7 64 20 61 73 20 64 69 72 74 79 20 62 75 74 0a 20 d as dirty but.
122c8 20 20 20 20 20 2a 2a 20 74 68 65 20 50 47 48 44 ** the PGHD
122c9 52 5f 4e 45 45 44 5f 53 59 4e 43 20 66 6c 61 67 R_NEED_SYNC flag
122ca 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 73 65 74 will not be set
122cb 2e 20 49 74 20 63 6f 75 6c 64 20 74 68 65 6e 20 . It could then
122cc 70 6f 74 65 6e 74 69 61 6c 6c 79 0a 20 20 20 20 potentially.
122cd 20 20 2a 2a 20 62 65 20 77 72 69 74 74 65 6e 20 ** be written
122ce 6f 75 74 20 69 6e 74 6f 20 74 68 65 20 64 61 74 out into the dat
122cf 61 62 61 73 65 20 66 69 6c 65 20 62 65 66 6f 72 abase file befor
122d0 65 20 69 74 73 20 6a 6f 75 72 6e 61 6c 20 66 69 e its journal fi
122d1 6c 65 0a 20 20 20 20 20 20 2a 2a 20 73 65 67 6d le. ** segm
122d2 65 6e 74 20 69 73 20 73 79 6e 63 65 64 2e 20 49 ent is synced. I
122d3 66 20 61 20 63 72 61 73 68 20 6f 63 63 75 72 73 f a crash occurs
122d4 20 64 75 72 69 6e 67 20 6f 72 20 66 6f 6c 6c 6f during or follo
122d5 77 69 6e 67 20 74 68 69 73 2c 0a 20 20 20 20 20 wing this,.
122d6 20 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f 72 ** database cor
122d7 72 75 70 74 69 6f 6e 20 6d 61 79 20 65 6e 73 75 ruption may ensu
122d8 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 e.. */.
122d9 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d sqlite3PcacheM
122da 61 6b 65 43 6c 65 61 6e 28 70 50 67 29 3b 0a 20 akeClean(pPg);.
122db 20 20 20 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 }.#ifdef SQLI
122dc 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a 20 TE_CHECK_PAGES.
122dd 20 20 20 70 50 67 2d 3e 70 61 67 65 48 61 73 68 pPg->pageHash
122de 20 3d 20 70 61 67 65 72 5f 70 61 67 65 68 61 73 = pager_pagehas
122df 68 28 70 50 67 29 3b 0a 23 65 6e 64 69 66 0a 20 h(pPg);.#endif.
122e0 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 77 61 /* If this wa
122e1 73 20 70 61 67 65 20 31 2c 20 74 68 65 6e 20 72 s page 1, then r
122e2 65 73 74 6f 72 65 20 74 68 65 20 76 61 6c 75 65 estore the value
122e3 20 6f 66 20 50 61 67 65 72 2e 64 62 46 69 6c 65 of Pager.dbFile
122e4 56 65 72 73 2e 0a 20 20 20 20 2a 2a 20 44 6f 20 Vers.. ** Do
122e5 74 68 69 73 20 62 65 66 6f 72 65 20 61 6e 79 20 this before any
122e6 64 65 63 6f 64 69 6e 67 2e 20 2a 2f 0a 20 20 20 decoding. */.
122e7 20 69 66 28 20 70 67 6e 6f 3d 3d 31 20 29 7b 0a if( pgno==1 ){.
122e8 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 70 50 memcpy(&pP
122e9 61 67 65 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 ager->dbFileVers
122ea 2c 20 26 28 28 75 38 2a 29 70 44 61 74 61 29 5b , &((u8*)pData)[
122eb 32 34 5d 2c 73 69 7a 65 6f 66 28 70 50 61 67 65 24],sizeof(pPage
122ec 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 29 29 3b r->dbFileVers));
122ed 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 . }.. /* D
122ee 65 63 6f 64 65 20 74 68 65 20 70 61 67 65 20 6a ecode the page j
122ef 75 73 74 20 72 65 61 64 20 66 72 6f 6d 20 64 69 ust read from di
122f0 73 6b 20 2a 2f 0a 20 20 20 20 43 4f 44 45 43 31 sk */. CODEC1
122f1 28 70 50 61 67 65 72 2c 20 70 44 61 74 61 2c 20 (pPager, pData,
122f2 70 50 67 2d 3e 70 67 6e 6f 2c 20 33 2c 20 72 63 pPg->pgno, 3, rc
122f3 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a =SQLITE_NOMEM);.
122f4 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 sqlite3Pcach
122f5 65 52 65 6c 65 61 73 65 28 70 50 67 29 3b 0a 20 eRelease(pPg);.
122f6 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
122f7 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 }../*.** Paramet
122f8 65 72 20 7a 4d 61 73 74 65 72 20 69 73 20 74 68 er zMaster is th
122f9 65 20 6e 61 6d 65 20 6f 66 20 61 20 6d 61 73 74 e name of a mast
122fa 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e er journal file.
122fb 20 41 20 73 69 6e 67 6c 65 20 6a 6f 75 72 6e 61 A single journa
122fc 6c 0a 2a 2a 20 66 69 6c 65 20 74 68 61 74 20 72 l.** file that r
122fd 65 66 65 72 72 65 64 20 74 6f 20 74 68 65 20 6d eferred to the m
122fe 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 aster journal fi
122ff 6c 65 20 68 61 73 20 6a 75 73 74 20 62 65 65 6e le has just been
12300 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a rolled back..**
12301 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 This routine ch
12302 65 63 6b 73 20 69 66 20 69 74 20 69 73 20 70 6f ecks if it is po
12303 73 73 69 62 6c 65 20 74 6f 20 64 65 6c 65 74 65 ssible to delete
12304 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 the master jour
12305 6e 61 6c 20 66 69 6c 65 2c 0a 2a 2a 20 61 6e 64 nal file,.** and
12306 20 64 6f 65 73 20 73 6f 20 69 66 20 69 74 20 69 does so if it i
12307 73 2e 0a 2a 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e s..**.** Argumen
12308 74 20 7a 4d 61 73 74 65 72 20 6d 61 79 20 70 6f t zMaster may po
12309 69 6e 74 20 74 6f 20 50 61 67 65 72 2e 70 54 6d int to Pager.pTm
1230a 70 53 70 61 63 65 2e 20 53 6f 20 74 68 61 74 20 pSpace. So that
1230b 62 75 66 66 65 72 20 69 73 20 6e 6f 74 20 0a 2a buffer is not .*
1230c 2a 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 * available for
1230d 75 73 65 20 77 69 74 68 69 6e 20 74 68 69 73 20 use within this
1230e 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 function..**.**
1230f 57 68 65 6e 20 61 20 6d 61 73 74 65 72 20 6a 6f When a master jo
12310 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 63 72 urnal file is cr
12311 65 61 74 65 64 2c 20 69 74 20 69 73 20 70 6f 70 eated, it is pop
12312 75 6c 61 74 65 64 20 77 69 74 68 20 74 68 65 20 ulated with the
12313 6e 61 6d 65 73 20 0a 2a 2a 20 6f 66 20 61 6c 6c names .** of all
12314 20 6f 66 20 69 74 73 20 63 68 69 6c 64 20 6a 6f of its child jo
12315 75 72 6e 61 6c 73 2c 20 6f 6e 65 20 61 66 74 65 urnals, one afte
12316 72 20 61 6e 6f 74 68 65 72 2c 20 66 6f 72 6d 61 r another, forma
12317 74 74 65 64 20 61 73 20 75 74 66 2d 38 20 0a 2a tted as utf-8 .*
12318 2a 20 65 6e 63 6f 64 65 64 20 74 65 78 74 2e 20 * encoded text.
12319 54 68 65 20 65 6e 64 20 6f 66 20 65 61 63 68 20 The end of each
1231a 63 68 69 6c 64 20 6a 6f 75 72 6e 61 6c 20 66 69 child journal fi
1231b 6c 65 20 69 73 20 6d 61 72 6b 65 64 20 77 69 74 le is marked wit
1231c 68 20 61 20 0a 2a 2a 20 6e 75 6c 2d 74 65 72 6d h a .** nul-term
1231d 69 6e 61 74 6f 72 20 62 79 74 65 20 28 30 78 30 inator byte (0x0
1231e 30 29 2e 20 69 2e 65 2e 20 74 68 65 20 65 6e 74 0). i.e. the ent
1231f 69 72 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 ire contents of
12320 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c a master journal
12321 0a 2a 2a 20 66 69 6c 65 20 66 6f 72 20 61 20 74 .** file for a t
12322 72 61 6e 73 61 63 74 69 6f 6e 20 69 6e 76 6f 6c ransaction invol
12323 76 69 6e 67 20 74 77 6f 20 64 61 74 61 62 61 73 ving two databas
12324 65 73 20 6d 69 67 68 74 20 62 65 3a 0a 2a 2a 0a es might be:.**.
12325 2a 2a 20 20 20 22 2f 68 6f 6d 65 2f 62 69 6c 6c ** "/home/bill
12326 2f 61 2e 64 62 2d 6a 6f 75 72 6e 61 6c 5c 78 30 /a.db-journal\x0
12327 30 2f 68 6f 6d 65 2f 62 69 6c 6c 2f 62 2e 64 62 0/home/bill/b.db
12328 2d 6a 6f 75 72 6e 61 6c 5c 78 30 30 22 0a 2a 2a -journal\x00".**
12329 0a 2a 2a 20 41 20 6d 61 73 74 65 72 20 6a 6f 75 .** A master jou
1232a 72 6e 61 6c 20 66 69 6c 65 20 6d 61 79 20 6f 6e rnal file may on
1232b 6c 79 20 62 65 20 64 65 6c 65 74 65 64 20 6f 6e ly be deleted on
1232c 63 65 20 61 6c 6c 20 6f 66 20 69 74 73 20 63 68 ce all of its ch
1232d 69 6c 64 20 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 73 ild .** journals
1232e 20 68 61 76 65 20 62 65 65 6e 20 72 6f 6c 6c 65 have been rolle
1232f 64 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 d back..**.** Th
12330 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 61 64 is function read
12331 73 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f s the contents o
12332 66 20 74 68 65 20 6d 61 73 74 65 72 2d 6a 6f 75 f the master-jou
12333 72 6e 61 6c 20 66 69 6c 65 20 69 6e 74 6f 20 0a rnal file into .
12334 2a 2a 20 6d 65 6d 6f 72 79 20 61 6e 64 20 6c 6f ** memory and lo
12335 6f 70 73 20 74 68 72 6f 75 67 68 20 65 61 63 68 ops through each
12336 20 6f 66 20 74 68 65 20 63 68 69 6c 64 20 6a 6f of the child jo
12337 75 72 6e 61 6c 20 6e 61 6d 65 73 2e 20 46 6f 72 urnal names. For
12338 0a 2a 2a 20 65 61 63 68 20 63 68 69 6c 64 20 6a .** each child j
12339 6f 75 72 6e 61 6c 2c 20 69 74 20 63 68 65 63 6b ournal, it check
1233a 73 20 69 66 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 s if:.**.** *
1233b 69 66 20 74 68 65 20 63 68 69 6c 64 20 6a 6f 75 if the child jou
1233c 72 6e 61 6c 20 65 78 69 73 74 73 2c 20 61 6e 64 rnal exists, and
1233d 20 69 66 20 73 6f 0a 2a 2a 20 20 20 2a 20 69 66 if so.** * if
1233e 20 74 68 65 20 63 68 69 6c 64 20 6a 6f 75 72 6e the child journ
1233f 61 6c 20 63 6f 6e 74 61 69 6e 73 20 61 20 72 65 al contains a re
12340 66 65 72 65 6e 63 65 20 74 6f 20 6d 61 73 74 65 ference to maste
12341 72 20 6a 6f 75 72 6e 61 6c 20 0a 2a 2a 20 20 20 r journal .**
12342 20 20 66 69 6c 65 20 7a 4d 61 73 74 65 72 0a 2a file zMaster.*
12343 2a 0a 2a 2a 20 49 66 20 61 20 63 68 69 6c 64 20 *.** If a child
12344 6a 6f 75 72 6e 61 6c 20 63 61 6e 20 62 65 20 66 journal can be f
12345 6f 75 6e 64 20 74 68 61 74 20 6d 61 74 63 68 65 ound that matche
12346 73 20 62 6f 74 68 20 6f 66 20 74 68 65 20 63 72 s both of the cr
12347 69 74 65 72 69 61 0a 2a 2a 20 61 62 6f 76 65 2c iteria.** above,
12348 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 this function r
12349 65 74 75 72 6e 73 20 77 69 74 68 6f 75 74 20 64 eturns without d
1234a 6f 69 6e 67 20 61 6e 79 74 68 69 6e 67 2e 20 4f oing anything. O
1234b 74 68 65 72 77 69 73 65 2c 20 69 66 0a 2a 2a 20 therwise, if.**
1234c 6e 6f 20 73 75 63 68 20 63 68 69 6c 64 20 6a 6f no such child jo
1234d 75 72 6e 61 6c 20 63 61 6e 20 62 65 20 66 6f 75 urnal can be fou
1234e 6e 64 2c 20 66 69 6c 65 20 7a 4d 61 73 74 65 72 nd, file zMaster
1234f 20 69 73 20 64 65 6c 65 74 65 64 20 66 72 6f 6d is deleted from
12350 0a 2a 2a 20 74 68 65 20 66 69 6c 65 2d 73 79 73 .** the file-sys
12351 74 65 6d 20 75 73 69 6e 67 20 73 71 6c 69 74 65 tem using sqlite
12352 33 4f 73 44 65 6c 65 74 65 28 29 2e 0a 2a 2a 0a 3OsDelete()..**.
12353 2a 2a 20 49 66 20 61 6e 20 49 4f 20 65 72 72 6f ** If an IO erro
12354 72 20 77 69 74 68 69 6e 20 74 68 69 73 20 66 75 r within this fu
12355 6e 63 74 69 6f 6e 2c 20 61 6e 20 65 72 72 6f 72 nction, an error
12356 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 code is returne
12357 64 2e 20 54 68 69 73 0a 2a 2a 20 66 75 6e 63 74 d. This.** funct
12358 69 6f 6e 20 61 6c 6c 6f 63 61 74 65 73 20 6d 65 ion allocates me
12359 6d 6f 72 79 20 62 79 20 63 61 6c 6c 69 6e 67 20 mory by calling
1235a 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 29 2e sqlite3Malloc().
1235b 20 49 66 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f If an allocatio
1235c 6e 0a 2a 2a 20 66 61 69 6c 73 2c 20 53 51 4c 49 n.** fails, SQLI
1235d 54 45 5f 4e 4f 4d 45 4d 20 69 73 20 72 65 74 75 TE_NOMEM is retu
1235e 72 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c rned. Otherwise,
1235f 20 69 66 20 6e 6f 20 49 4f 20 6f 72 20 6d 61 6c if no IO or mal
12360 6c 6f 63 20 65 72 72 6f 72 73 20 0a 2a 2a 20 6f loc errors .** o
12361 63 63 75 72 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 ccur, SQLITE_OK
12362 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a is returned..**.
12363 2a 2a 20 54 4f 44 4f 3a 20 54 68 69 73 20 66 75 ** TODO: This fu
12364 6e 63 74 69 6f 6e 20 61 6c 6c 6f 63 61 74 65 73 nction allocates
12365 20 61 20 73 69 6e 67 6c 65 20 62 6c 6f 63 6b 20 a single block
12366 6f 66 20 6d 65 6d 6f 72 79 20 74 6f 20 6c 6f 61 of memory to loa
12367 64 0a 2a 2a 20 74 68 65 20 65 6e 74 69 72 65 20 d.** the entire
12368 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 contents of the
12369 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 master journal f
1236a 69 6c 65 2e 20 54 68 69 73 20 63 6f 75 6c 64 20 ile. This could
1236b 62 65 0a 2a 2a 20 61 20 63 6f 75 70 6c 65 20 6f be.** a couple o
1236c 66 20 6b 69 6c 6f 62 79 74 65 73 20 6f 72 20 73 f kilobytes or s
1236d 6f 20 2d 20 70 6f 74 65 6e 74 69 61 6c 6c 79 20 o - potentially
1236e 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 65 20 larger than the
1236f 70 61 67 65 20 0a 2a 2a 20 73 69 7a 65 2e 0a 2a page .** size..*
12370 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 /.static int pag
12371 65 72 5f 64 65 6c 6d 61 73 74 65 72 28 50 61 67 er_delmaster(Pag
12372 65 72 20 2a 70 50 61 67 65 72 2c 20 63 6f 6e 73 er *pPager, cons
12373 74 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 29 t char *zMaster)
12374 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 {. sqlite3_vfs
12375 2a 70 56 66 73 20 3d 20 70 50 61 67 65 72 2d 3e *pVfs = pPager->
12376 70 56 66 73 3b 0a 20 20 69 6e 74 20 72 63 3b 20 pVfs;. int rc;
12377 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12378 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 /* Return code
12379 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 */. sqlite3_fi
1237a 6c 65 20 2a 70 4d 61 73 74 65 72 3b 20 20 20 20 le *pMaster;
1237b 2f 2a 20 4d 61 6c 6c 6f 63 27 64 20 6d 61 73 74 /* Malloc'd mast
1237c 65 72 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 er-journal file
1237d 64 65 73 63 72 69 70 74 6f 72 20 2a 2f 0a 20 20 descriptor */.
1237e 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a sqlite3_file *pJ
1237f 6f 75 72 6e 61 6c 3b 20 20 20 2f 2a 20 4d 61 6c ournal; /* Mal
12380 6c 6f 63 27 64 20 63 68 69 6c 64 2d 6a 6f 75 72 loc'd child-jour
12381 6e 61 6c 20 66 69 6c 65 20 64 65 73 63 72 69 70 nal file descrip
12382 74 6f 72 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a tor */. char *z
12383 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 20 3d 20 MasterJournal =
12384 30 3b 20 2f 2a 20 43 6f 6e 74 65 6e 74 73 20 6f 0; /* Contents o
12385 66 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c f master journal
12386 20 66 69 6c 65 20 2a 2f 0a 20 20 69 36 34 20 6e file */. i64 n
12387 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 3b 20 20 MasterJournal;
12388 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 /* Size of
12389 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 master journal f
1238a 69 6c 65 20 2a 2f 0a 0a 20 20 2f 2a 20 41 6c 6c ile */.. /* All
1238b 6f 63 61 74 65 20 73 70 61 63 65 20 66 6f 72 20 ocate space for
1238c 62 6f 74 68 20 74 68 65 20 70 4a 6f 75 72 6e 61 both the pJourna
1238d 6c 20 61 6e 64 20 70 4d 61 73 74 65 72 20 66 69 l and pMaster fi
1238e 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73 2e 0a le descriptors..
1238f 20 20 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66 ** If successf
12390 75 6c 2c 20 6f 70 65 6e 20 74 68 65 20 6d 61 73 ul, open the mas
12391 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ter journal file
12392 20 66 6f 72 20 72 65 61 64 69 6e 67 2e 0a 20 20 for reading..
12393 2a 2f 0a 20 20 70 4d 61 73 74 65 72 20 3d 20 28 */. pMaster = (
12394 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 73 sqlite3_file *)s
12395 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f qlite3MallocZero
12396 28 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c 65 20 (pVfs->szOsFile
12397 2a 20 32 29 3b 0a 20 20 70 4a 6f 75 72 6e 61 6c * 2);. pJournal
12398 20 3d 20 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 = (sqlite3_file
12399 20 2a 29 28 28 28 75 38 20 2a 29 70 4d 61 73 74 *)(((u8 *)pMast
1239a 65 72 29 20 2b 20 70 56 66 73 2d 3e 73 7a 4f 73 er) + pVfs->szOs
1239b 46 69 6c 65 29 3b 0a 20 20 69 66 28 20 21 70 4d File);. if( !pM
1239c 61 73 74 65 72 20 29 7b 0a 20 20 20 20 72 63 20 aster ){. rc
1239d 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a = SQLITE_NOMEM;.
1239e 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 6f 6e }else{. con
1239f 73 74 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 28 st int flags = (
123a0 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 SQLITE_OPEN_READ
123a1 4f 4e 4c 59 7c 53 51 4c 49 54 45 5f 4f 50 45 4e ONLY|SQLITE_OPEN
123a2 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 29 _MASTER_JOURNAL)
123a3 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 ;. rc = sqlit
123a4 65 33 4f 73 4f 70 65 6e 28 70 56 66 73 2c 20 7a e3OsOpen(pVfs, z
123a5 4d 61 73 74 65 72 2c 20 70 4d 61 73 74 65 72 2c Master, pMaster,
123a6 20 66 6c 61 67 73 2c 20 30 29 3b 0a 20 20 7d 0a flags, 0);. }.
123a7 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
123a8 5f 4f 4b 20 29 20 67 6f 74 6f 20 64 65 6c 6d 61 _OK ) goto delma
123a9 73 74 65 72 5f 6f 75 74 3b 0a 0a 20 20 72 63 20 ster_out;.. rc
123aa 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 = sqlite3OsFileS
123ab 69 7a 65 28 70 4d 61 73 74 65 72 2c 20 26 6e 4d ize(pMaster, &nM
123ac 61 73 74 65 72 4a 6f 75 72 6e 61 6c 29 3b 0a 20 asterJournal);.
123ad 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
123ae 4f 4b 20 29 20 67 6f 74 6f 20 64 65 6c 6d 61 73 OK ) goto delmas
123af 74 65 72 5f 6f 75 74 3b 0a 0a 20 20 69 66 28 20 ter_out;.. if(
123b0 6e 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 3e 30 nMasterJournal>0
123b1 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 4a ){. char *zJ
123b2 6f 75 72 6e 61 6c 3b 0a 20 20 20 20 63 68 61 72 ournal;. char
123b3 20 2a 7a 4d 61 73 74 65 72 50 74 72 20 3d 20 30 *zMasterPtr = 0
123b4 3b 0a 20 20 20 20 69 6e 74 20 6e 4d 61 73 74 65 ;. int nMaste
123b5 72 50 74 72 20 3d 20 70 56 66 73 2d 3e 6d 78 50 rPtr = pVfs->mxP
123b6 61 74 68 6e 61 6d 65 2b 31 3b 0a 0a 20 20 20 20 athname+1;..
123b7 2f 2a 20 4c 6f 61 64 20 74 68 65 20 65 6e 74 69 /* Load the enti
123b8 72 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 re master journa
123b9 6c 20 66 69 6c 65 20 69 6e 74 6f 20 73 70 61 63 l file into spac
123ba 65 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a e obtained from.
123bb 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 6d ** sqlite3_m
123bc 61 6c 6c 6f 63 28 29 20 61 6e 64 20 70 6f 69 6e alloc() and poin
123bd 74 65 64 20 74 6f 20 62 79 20 7a 4d 61 73 74 65 ted to by zMaste
123be 72 4a 6f 75 72 6e 61 6c 2e 20 0a 20 20 20 20 2a rJournal. . *
123bf 2f 0a 20 20 20 20 7a 4d 61 73 74 65 72 4a 6f 75 /. zMasterJou
123c0 72 6e 61 6c 20 3d 20 73 71 6c 69 74 65 33 4d 61 rnal = sqlite3Ma
123c1 6c 6c 6f 63 28 28 69 6e 74 29 6e 4d 61 73 74 65 lloc((int)nMaste
123c2 72 4a 6f 75 72 6e 61 6c 20 2b 20 6e 4d 61 73 74 rJournal + nMast
123c3 65 72 50 74 72 20 2b 20 31 29 3b 0a 20 20 20 20 erPtr + 1);.
123c4 69 66 28 20 21 7a 4d 61 73 74 65 72 4a 6f 75 72 if( !zMasterJour
123c5 6e 61 6c 20 29 7b 0a 20 20 20 20 20 20 72 63 20 nal ){. rc
123c6 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a = SQLITE_NOMEM;.
123c7 20 20 20 20 20 20 67 6f 74 6f 20 64 65 6c 6d 61 goto delma
123c8 73 74 65 72 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a ster_out;. }.
123c9 20 20 20 20 7a 4d 61 73 74 65 72 50 74 72 20 3d zMasterPtr =
123ca 20 26 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c &zMasterJournal
123cb 5b 6e 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 2b [nMasterJournal+
123cc 31 5d 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 1];. rc = sql
123cd 69 74 65 33 4f 73 52 65 61 64 28 70 4d 61 73 74 ite3OsRead(pMast
123ce 65 72 2c 20 7a 4d 61 73 74 65 72 4a 6f 75 72 6e er, zMasterJourn
123cf 61 6c 2c 20 28 69 6e 74 29 6e 4d 61 73 74 65 72 al, (int)nMaster
123d0 4a 6f 75 72 6e 61 6c 2c 20 30 29 3b 0a 20 20 20 Journal, 0);.
123d1 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
123d2 4f 4b 20 29 20 67 6f 74 6f 20 64 65 6c 6d 61 73 OK ) goto delmas
123d3 74 65 72 5f 6f 75 74 3b 0a 20 20 20 20 7a 4d 61 ter_out;. zMa
123d4 73 74 65 72 4a 6f 75 72 6e 61 6c 5b 6e 4d 61 73 sterJournal[nMas
123d5 74 65 72 4a 6f 75 72 6e 61 6c 5d 20 3d 20 30 3b terJournal] = 0;
123d6 0a 0a 20 20 20 20 7a 4a 6f 75 72 6e 61 6c 20 3d .. zJournal =
123d7 20 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 3b zMasterJournal;
123d8 0a 20 20 20 20 77 68 69 6c 65 28 20 28 7a 4a 6f . while( (zJo
123d9 75 72 6e 61 6c 2d 7a 4d 61 73 74 65 72 4a 6f 75 urnal-zMasterJou
123da 72 6e 61 6c 29 3c 6e 4d 61 73 74 65 72 4a 6f 75 rnal)<nMasterJou
123db 72 6e 61 6c 20 29 7b 0a 20 20 20 20 20 20 69 6e rnal ){. in
123dc 74 20 65 78 69 73 74 73 3b 0a 20 20 20 20 20 20 t exists;.
123dd 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 41 63 rc = sqlite3OsAc
123de 63 65 73 73 28 70 56 66 73 2c 20 7a 4a 6f 75 72 cess(pVfs, zJour
123df 6e 61 6c 2c 20 53 51 4c 49 54 45 5f 41 43 43 45 nal, SQLITE_ACCE
123e0 53 53 5f 45 58 49 53 54 53 2c 20 26 65 78 69 73 SS_EXISTS, &exis
123e1 74 73 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 ts);. if( r
123e2 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
123e3 20 20 20 20 20 20 20 20 67 6f 74 6f 20 64 65 6c goto del
123e4 6d 61 73 74 65 72 5f 6f 75 74 3b 0a 20 20 20 20 master_out;.
123e5 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 65 78 }. if( ex
123e6 69 73 74 73 20 29 7b 0a 20 20 20 20 20 20 20 20 ists ){.
123e7 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 6a 6f /* One of the jo
123e8 75 72 6e 61 6c 73 20 70 6f 69 6e 74 65 64 20 74 urnals pointed t
123e9 6f 20 62 79 20 74 68 65 20 6d 61 73 74 65 72 20 o by the master
123ea 6a 6f 75 72 6e 61 6c 20 65 78 69 73 74 73 2e 0a journal exists..
123eb 20 20 20 20 20 20 20 20 2a 2a 20 4f 70 65 6e 20 ** Open
123ec 69 74 20 61 6e 64 20 63 68 65 63 6b 20 69 66 20 it and check if
123ed 69 74 20 70 6f 69 6e 74 73 20 61 74 20 74 68 65 it points at the
123ee 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 2e master journal.
123ef 20 49 66 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 If. ** s
123f0 6f 2c 20 72 65 74 75 72 6e 20 77 69 74 68 6f 75 o, return withou
123f1 74 20 64 65 6c 65 74 69 6e 67 20 74 68 65 20 6d t deleting the m
123f2 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 aster journal fi
123f3 6c 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 le.. */.
123f4 20 20 20 20 20 20 20 69 6e 74 20 63 3b 0a 20 20 int c;.
123f5 20 20 20 20 20 20 69 6e 74 20 66 6c 61 67 73 20 int flags
123f6 3d 20 28 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 = (SQLITE_OPEN_R
123f7 45 41 44 4f 4e 4c 59 7c 53 51 4c 49 54 45 5f 4f EADONLY|SQLITE_O
123f8 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c PEN_MAIN_JOURNAL
123f9 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 );. rc =
123fa 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 56 sqlite3OsOpen(pV
123fb 66 73 2c 20 7a 4a 6f 75 72 6e 61 6c 2c 20 70 4a fs, zJournal, pJ
123fc 6f 75 72 6e 61 6c 2c 20 66 6c 61 67 73 2c 20 30 ournal, flags, 0
123fd 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 );. if( r
123fe 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
123ff 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 64 goto d
12400 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0a 20 20 elmaster_out;.
12401 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 }..
12402 20 72 63 20 3d 20 72 65 61 64 4d 61 73 74 65 72 rc = readMaster
12403 4a 6f 75 72 6e 61 6c 28 70 4a 6f 75 72 6e 61 6c Journal(pJournal
12404 2c 20 7a 4d 61 73 74 65 72 50 74 72 2c 20 6e 4d , zMasterPtr, nM
12405 61 73 74 65 72 50 74 72 29 3b 0a 20 20 20 20 20 asterPtr);.
12406 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 sqlite3OsClos
12407 65 28 70 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 20 e(pJournal);.
12408 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c if( rc!=SQL
12409 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
1240a 20 20 20 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 goto delmast
1240b 65 72 5f 6f 75 74 3b 0a 20 20 20 20 20 20 20 20 er_out;.
1240c 7d 0a 0a 20 20 20 20 20 20 20 20 63 20 3d 20 7a }.. c = z
1240d 4d 61 73 74 65 72 50 74 72 5b 30 5d 21 3d 30 20 MasterPtr[0]!=0
1240e 26 26 20 73 74 72 63 6d 70 28 7a 4d 61 73 74 65 && strcmp(zMaste
1240f 72 50 74 72 2c 20 7a 4d 61 73 74 65 72 29 3d 3d rPtr, zMaster)==
12410 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 63 0;. if( c
12411 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a ){. /*
12412 20 57 65 20 68 61 76 65 20 61 20 6d 61 74 63 68 We have a match
12413 2e 20 44 6f 20 6e 6f 74 20 64 65 6c 65 74 65 20 . Do not delete
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 2e 20 2a 2f 0a 20 20 20 20 al file. */.
12416 20 20 20 20 20 20 67 6f 74 6f 20 64 65 6c 6d 61 goto delma
12417 73 74 65 72 5f 6f 75 74 3b 0a 20 20 20 20 20 20 ster_out;.
12418 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
12419 20 20 7a 4a 6f 75 72 6e 61 6c 20 2b 3d 20 28 73 zJournal += (s
1241a 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a qlite3Strlen30(z
1241b 4a 6f 75 72 6e 61 6c 29 2b 31 29 3b 0a 20 20 20 Journal)+1);.
1241c 20 7d 0a 20 20 7d 0a 20 20 0a 20 20 72 63 20 3d }. }. . rc =
1241d 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 sqlite3OsDelete
1241e 28 70 56 66 73 2c 20 7a 4d 61 73 74 65 72 2c 20 (pVfs, zMaster,
1241f 30 29 3b 0a 0a 64 65 6c 6d 61 73 74 65 72 5f 6f 0);..delmaster_o
12420 75 74 3a 0a 20 20 69 66 28 20 7a 4d 61 73 74 65 ut:. if( zMaste
12421 72 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20 20 rJournal ){.
12422 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 4d 61 sqlite3_free(zMa
12423 73 74 65 72 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 sterJournal);.
12424 7d 20 20 0a 20 20 69 66 28 20 70 4d 61 73 74 65 } . if( pMaste
12425 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 r ){. sqlite3
12426 4f 73 43 6c 6f 73 65 28 70 4d 61 73 74 65 72 29 OsClose(pMaster)
12427 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 69 ;. assert( !i
12428 73 4f 70 65 6e 28 70 4a 6f 75 72 6e 61 6c 29 20 sOpen(pJournal)
12429 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 );. }. sqlite3
1242a 5f 66 72 65 65 28 70 4d 61 73 74 65 72 29 3b 0a _free(pMaster);.
1242b 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
1242c 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 ./*.** This func
1242d 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 tion is used to
1242e 63 68 61 6e 67 65 20 74 68 65 20 61 63 74 75 61 change the actua
1242f 6c 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 l size of the da
12430 74 61 62 61 73 65 20 0a 2a 2a 20 66 69 6c 65 20 tabase .** file
12431 69 6e 20 74 68 65 20 66 69 6c 65 2d 73 79 73 74 in the file-syst
12432 65 6d 2e 20 54 68 69 73 20 6f 6e 6c 79 20 68 61 em. This only ha
12433 70 70 65 6e 73 20 77 68 65 6e 20 63 6f 6d 6d 69 ppens when commi
12434 74 74 69 6e 67 20 61 20 74 72 61 6e 73 61 63 74 tting a transact
12435 69 6f 6e 2c 0a 2a 2a 20 6f 72 20 72 6f 6c 6c 69 ion,.** or rolli
12436 6e 67 20 62 61 63 6b 20 61 20 74 72 61 6e 73 61 ng back a transa
12437 63 74 69 6f 6e 20 28 69 6e 63 6c 75 64 69 6e 67 ction (including
12438 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 61 20 rolling back a
12439 68 6f 74 2d 6a 6f 75 72 6e 61 6c 29 2e 0a 2a 2a hot-journal)..**
1243a 0a 2a 2a 20 49 66 20 74 68 65 20 6d 61 69 6e 20 .** If the main
1243b 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 73 database file is
1243c 20 6e 6f 74 20 6f 70 65 6e 2c 20 6f 72 20 61 6e not open, or an
1243d 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 exclusive lock
1243e 69 73 20 6e 6f 74 0a 2a 2a 20 68 65 6c 64 2c 20 is not.** held,
1243f 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 this function is
12440 20 61 20 6e 6f 2d 6f 70 2e 20 4f 74 68 65 72 77 a no-op. Otherw
12441 69 73 65 2c 20 74 68 65 20 73 69 7a 65 20 6f 66 ise, the size of
12442 20 74 68 65 20 66 69 6c 65 20 69 73 0a 2a 2a 20 the file is.**
12443 63 68 61 6e 67 65 64 20 74 6f 20 6e 50 61 67 65 changed to nPage
12444 20 70 61 67 65 73 20 28 6e 50 61 67 65 2a 70 50 pages (nPage*pP
12445 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 62 ager->pageSize b
12446 79 74 65 73 29 2e 20 49 66 20 74 68 65 20 66 69 ytes). If the fi
12447 6c 65 0a 2a 2a 20 6f 6e 20 64 69 73 6b 20 69 73 le.** on disk is
12448 20 63 75 72 72 65 6e 74 6c 79 20 6c 61 72 67 65 currently large
12449 72 20 74 68 61 6e 20 6e 50 61 67 65 20 70 61 67 r than nPage pag
1244a 65 73 2c 20 74 68 65 6e 20 75 73 65 20 74 68 65 es, then use the
1244b 20 56 46 53 0a 2a 2a 20 78 54 72 75 6e 63 61 74 VFS.** xTruncat
1244c 65 28 29 20 6d 65 74 68 6f 64 20 74 6f 20 74 72 e() method to tr
1244d 75 6e 63 61 74 65 20 69 74 2e 0a 2a 2a 0a 2a 2a uncate it..**.**
1244e 20 4f 72 2c 20 69 74 20 6d 69 67 68 74 20 6d 69 Or, it might mi
1244f 67 68 74 20 62 65 20 74 68 65 20 63 61 73 65 20 ght be the case
12450 74 68 61 74 20 74 68 65 20 66 69 6c 65 20 6f 6e that the file on
12451 20 64 69 73 6b 20 69 73 20 73 6d 61 6c 6c 65 72 disk is smaller
12452 20 74 68 61 6e 20 0a 2a 2a 20 6e 50 61 67 65 20 than .** nPage
12453 70 61 67 65 73 2e 20 53 6f 6d 65 20 6f 70 65 72 pages. Some oper
12454 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 6d 70 ating system imp
12455 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 63 61 6e lementations can
12456 20 67 65 74 20 63 6f 6e 66 75 73 65 64 20 69 66 get confused if
12457 20 0a 2a 2a 20 79 6f 75 20 74 72 79 20 74 6f 20 .** you try to
12458 74 72 75 6e 63 61 74 65 20 61 20 66 69 6c 65 20 truncate a file
12459 74 6f 20 73 6f 6d 65 20 73 69 7a 65 20 74 68 61 to some size tha
1245a 74 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e t is larger than
1245b 20 69 74 20 0a 2a 2a 20 63 75 72 72 65 6e 74 6c it .** currentl
1245c 79 20 69 73 2c 20 73 6f 20 64 65 74 65 63 74 20 y is, so detect
1245d 74 68 69 73 20 63 61 73 65 20 61 6e 64 20 77 72 this case and wr
1245e 69 74 65 20 61 20 73 69 6e 67 6c 65 20 7a 65 72 ite a single zer
1245f 6f 20 62 79 74 65 20 74 6f 20 0a 2a 2a 20 74 68 o byte to .** th
12460 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6e 65 77 e end of the new
12461 20 66 69 6c 65 20 69 6e 73 74 65 61 64 2e 0a 2a file instead..*
12462 2a 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66 *.** If successf
12463 75 6c 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ul, return SQLIT
12464 45 5f 4f 4b 2e 20 49 66 20 61 6e 20 49 4f 20 65 E_OK. If an IO e
12465 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c rror occurs whil
12466 65 20 6d 6f 64 69 66 79 69 6e 67 0a 2a 2a 20 74 e modifying.** t
12467 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
12468 2c 20 72 65 74 75 72 6e 20 74 68 65 20 65 72 72 , return the err
12469 6f 72 20 63 6f 64 65 20 74 6f 20 74 68 65 20 63 or code to the c
1246a 61 6c 6c 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 aller..*/.static
1246b 20 69 6e 74 20 70 61 67 65 72 5f 74 72 75 6e 63 int pager_trunc
1246c 61 74 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 ate(Pager *pPage
1246d 72 2c 20 50 67 6e 6f 20 6e 50 61 67 65 29 7b 0a r, Pgno nPage){.
1246e 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
1246f 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 50 61 67 E_OK;. if( pPag
12470 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 er->state>=PAGER
12471 5f 45 58 43 4c 55 53 49 56 45 20 26 26 20 69 73 _EXCLUSIVE && is
12472 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 Open(pPager->fd)
12473 20 29 7b 0a 20 20 20 20 69 36 34 20 63 75 72 72 ){. i64 curr
12474 65 6e 74 53 69 7a 65 2c 20 6e 65 77 53 69 7a 65 entSize, newSize
12475 3b 0a 20 20 20 20 2f 2a 20 54 4f 44 4f 3a 20 49 ;. /* TODO: I
12476 73 20 69 74 20 73 61 66 65 20 74 6f 20 75 73 65 s it safe to use
12477 20 50 61 67 65 72 2e 64 62 46 69 6c 65 53 69 7a Pager.dbFileSiz
12478 65 20 68 65 72 65 3f 20 2a 2f 0a 20 20 20 20 72 e here? */. r
12479 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c c = sqlite3OsFil
1247a 65 53 69 7a 65 28 70 50 61 67 65 72 2d 3e 66 64 eSize(pPager->fd
1247b 2c 20 26 63 75 72 72 65 6e 74 53 69 7a 65 29 3b , ¤tSize);
1247c 0a 20 20 20 20 6e 65 77 53 69 7a 65 20 3d 20 70 . newSize = p
1247d 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2a Pager->pageSize*
1247e 28 69 36 34 29 6e 50 61 67 65 3b 0a 20 20 20 20 (i64)nPage;.
1247f 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
12480 4b 20 26 26 20 63 75 72 72 65 6e 74 53 69 7a 65 K && currentSize
12481 21 3d 6e 65 77 53 69 7a 65 20 29 7b 0a 20 20 20 !=newSize ){.
12482 20 20 20 69 66 28 20 63 75 72 72 65 6e 74 53 69 if( currentSi
12483 7a 65 3e 6e 65 77 53 69 7a 65 20 29 7b 0a 20 20 ze>newSize ){.
12484 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
12485 65 33 4f 73 54 72 75 6e 63 61 74 65 28 70 50 61 e3OsTruncate(pPa
12486 67 65 72 2d 3e 66 64 2c 20 6e 65 77 53 69 7a 65 ger->fd, newSize
12487 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a );. }else{.
12488 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c rc = sql
12489 69 74 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 ite3OsWrite(pPag
1248a 65 72 2d 3e 66 64 2c 20 22 22 2c 20 31 2c 20 6e er->fd, "", 1, n
1248b 65 77 53 69 7a 65 2d 31 29 3b 0a 20 20 20 20 20 ewSize-1);.
1248c 20 7d 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d }. if( rc=
1248d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
1248e 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 pPager->db
1248f 46 69 6c 65 53 69 7a 65 20 3d 20 6e 50 61 67 65 FileSize = nPage
12490 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
12491 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
12492 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 .}../*.** Set th
12493 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 50 e value of the P
12494 61 67 65 72 2e 73 65 63 74 6f 72 53 69 7a 65 20 ager.sectorSize
12495 76 61 72 69 61 62 6c 65 20 66 6f 72 20 74 68 65 variable for the
12496 20 67 69 76 65 6e 0a 2a 2a 20 70 61 67 65 72 20 given.** pager
12497 62 61 73 65 64 20 6f 6e 20 74 68 65 20 76 61 6c based on the val
12498 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 ue returned by t
12499 68 65 20 78 53 65 63 74 6f 72 53 69 7a 65 20 6d he xSectorSize m
1249a 65 74 68 6f 64 0a 2a 2a 20 6f 66 20 74 68 65 20 ethod.** of the
1249b 6f 70 65 6e 20 64 61 74 61 62 61 73 65 20 66 69 open database fi
1249c 6c 65 2e 20 54 68 65 20 73 65 63 74 6f 72 20 73 le. The sector s
1249d 69 7a 65 20 77 69 6c 6c 20 62 65 20 75 73 65 64 ize will be used
1249e 20 75 73 65 64 20 0a 2a 2a 20 74 6f 20 64 65 74 used .** to det
1249f 65 72 6d 69 6e 65 20 74 68 65 20 73 69 7a 65 20 ermine the size
124a0 61 6e 64 20 61 6c 69 67 6e 6d 65 6e 74 20 6f 66 and alignment of
124a1 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 journal header
124a2 61 6e 64 20 0a 2a 2a 20 6d 61 73 74 65 72 20 6a and .** master j
124a3 6f 75 72 6e 61 6c 20 70 6f 69 6e 74 65 72 73 20 ournal pointers
124a4 77 69 74 68 69 6e 20 63 72 65 61 74 65 64 20 6a within created j
124a5 6f 75 72 6e 61 6c 20 66 69 6c 65 73 2e 0a 2a 2a ournal files..**
124a6 0a 2a 2a 20 46 6f 72 20 74 65 6d 70 6f 72 61 72 .** For temporar
124a7 79 20 66 69 6c 65 73 20 74 68 65 20 65 66 66 65 y files the effe
124a8 63 74 69 76 65 20 73 65 63 74 6f 72 20 73 69 7a ctive sector siz
124a9 65 20 69 73 20 61 6c 77 61 79 73 20 35 31 32 20 e is always 512
124aa 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 bytes..**.** Oth
124ab 65 72 77 69 73 65 2c 20 66 6f 72 20 6e 6f 6e 2d erwise, for non-
124ac 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 2c temporary files,
124ad 20 74 68 65 20 65 66 66 65 63 74 69 76 65 20 73 the effective s
124ae 65 63 74 6f 72 20 73 69 7a 65 20 69 73 0a 2a 2a ector size is.**
124af 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 the value retur
124b0 6e 65 64 20 62 79 20 74 68 65 20 78 53 65 63 74 ned by the xSect
124b1 6f 72 53 69 7a 65 28 29 20 6d 65 74 68 6f 64 20 orSize() method
124b2 72 6f 75 6e 64 65 64 20 75 70 20 74 6f 20 35 31 rounded up to 51
124b3 32 20 69 66 0a 2a 2a 20 69 74 20 69 73 20 6c 65 2 if.** it is le
124b4 73 73 20 74 68 61 6e 20 35 31 32 2c 20 6f 72 20 ss than 512, or
124b5 72 6f 75 6e 64 65 64 20 64 6f 77 6e 20 74 6f 20 rounded down to
124b6 4d 41 58 5f 53 45 43 54 4f 52 5f 53 49 5a 45 20 MAX_SECTOR_SIZE
124b7 69 66 20 69 74 0a 2a 2a 20 69 73 20 67 72 65 61 if it.** is grea
124b8 74 65 72 20 74 68 61 6e 20 4d 41 58 5f 53 45 43 ter than MAX_SEC
124b9 54 4f 52 5f 53 49 5a 45 2e 0a 2a 2f 0a 73 74 61 TOR_SIZE..*/.sta
124ba 74 69 63 20 76 6f 69 64 20 73 65 74 53 65 63 74 tic void setSect
124bb 6f 72 53 69 7a 65 28 50 61 67 65 72 20 2a 70 50 orSize(Pager *pP
124bc 61 67 65 72 29 7b 0a 20 20 61 73 73 65 72 74 28 ager){. assert(
124bd 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e isOpen(pPager->
124be 66 64 29 20 7c 7c 20 70 50 61 67 65 72 2d 3e 74 fd) || pPager->t
124bf 65 6d 70 46 69 6c 65 20 29 3b 0a 0a 20 20 69 66 empFile );.. if
124c0 28 20 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 ( !pPager->tempF
124c1 69 6c 65 20 29 7b 0a 20 20 20 20 2f 2a 20 53 65 ile ){. /* Se
124c2 63 74 6f 72 20 73 69 7a 65 20 64 6f 65 73 6e 27 ctor size doesn'
124c3 74 20 6d 61 74 74 65 72 20 66 6f 72 20 74 65 6d t matter for tem
124c4 70 6f 72 61 72 79 20 66 69 6c 65 73 2e 20 41 6c porary files. Al
124c5 73 6f 2c 20 74 68 65 20 66 69 6c 65 0a 20 20 20 so, the file.
124c6 20 2a 2a 20 6d 61 79 20 6e 6f 74 20 68 61 76 65 ** may not have
124c7 20 62 65 65 6e 20 6f 70 65 6e 65 64 20 79 65 74 been opened yet
124c8 2c 20 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 , in which case
124c9 74 68 65 20 4f 73 53 65 63 74 6f 72 53 69 7a 65 the OsSectorSize
124ca 28 29 0a 20 20 20 20 2a 2a 20 63 61 6c 6c 20 77 (). ** call w
124cb 69 6c 6c 20 73 65 67 66 61 75 6c 74 2e 0a 20 20 ill segfault..
124cc 20 20 2a 2f 0a 20 20 20 20 70 50 61 67 65 72 2d */. pPager-
124cd 3e 73 65 63 74 6f 72 53 69 7a 65 20 3d 20 73 71 >sectorSize = sq
124ce 6c 69 74 65 33 4f 73 53 65 63 74 6f 72 53 69 7a lite3OsSectorSiz
124cf 65 28 70 50 61 67 65 72 2d 3e 66 64 29 3b 0a 20 e(pPager->fd);.
124d0 20 7d 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d }. if( pPager-
124d1 3e 73 65 63 74 6f 72 53 69 7a 65 3c 35 31 32 20 >sectorSize<512
124d2 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 ){. pPager->s
124d3 65 63 74 6f 72 53 69 7a 65 20 3d 20 35 31 32 3b ectorSize = 512;
124d4 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 61 67 65 . }. if( pPage
124d5 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 3e 4d 41 r->sectorSize>MA
124d6 58 5f 53 45 43 54 4f 52 5f 53 49 5a 45 20 29 7b X_SECTOR_SIZE ){
124d7 0a 20 20 20 20 61 73 73 65 72 74 28 20 4d 41 58 . assert( MAX
124d8 5f 53 45 43 54 4f 52 5f 53 49 5a 45 3e 3d 35 31 _SECTOR_SIZE>=51
124d9 32 20 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 2 );. pPager-
124da 3e 73 65 63 74 6f 72 53 69 7a 65 20 3d 20 4d 41 >sectorSize = MA
124db 58 5f 53 45 43 54 4f 52 5f 53 49 5a 45 3b 0a 20 X_SECTOR_SIZE;.
124dc 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 6c 61 79 }.}../*.** Play
124dd 62 61 63 6b 20 74 68 65 20 6a 6f 75 72 6e 61 6c back the journal
124de 20 61 6e 64 20 74 68 75 73 20 72 65 73 74 6f 72 and thus restor
124df 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 e the database f
124e0 69 6c 65 20 74 6f 0a 2a 2a 20 74 68 65 20 73 74 ile to.** the st
124e1 61 74 65 20 69 74 20 77 61 73 20 69 6e 20 62 65 ate it was in be
124e2 66 6f 72 65 20 77 65 20 73 74 61 72 74 65 64 20 fore we started
124e3 6d 61 6b 69 6e 67 20 63 68 61 6e 67 65 73 2e 20 making changes.
124e4 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6a 6f 75 72 .**.** The jour
124e5 6e 61 6c 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 nal file format
124e6 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 20 0a is as follows: .
124e7 2a 2a 0a 2a 2a 20 20 28 31 29 20 20 38 20 62 79 **.** (1) 8 by
124e8 74 65 20 70 72 65 66 69 78 2e 20 20 41 20 63 6f te prefix. A co
124e9 70 79 20 6f 66 20 61 4a 6f 75 72 6e 61 6c 4d 61 py of aJournalMa
124ea 67 69 63 5b 5d 2e 0a 2a 2a 20 20 28 32 29 20 20 gic[]..** (2)
124eb 34 20 62 79 74 65 20 62 69 67 2d 65 6e 64 69 61 4 byte big-endia
124ec 6e 20 69 6e 74 65 67 65 72 20 77 68 69 63 68 20 n integer which
124ed 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 is the number of
124ee 20 76 61 6c 69 64 20 70 61 67 65 20 72 65 63 6f valid page reco
124ef 72 64 73 0a 2a 2a 20 20 20 20 20 20 20 69 6e 20 rds.** in
124f0 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 49 66 the journal. If
124f1 20 74 68 69 73 20 76 61 6c 75 65 20 69 73 20 30 this value is 0
124f2 78 66 66 66 66 66 66 66 66 2c 20 74 68 65 6e 20 xffffffff, then
124f3 63 6f 6d 70 75 74 65 20 74 68 65 0a 2a 2a 20 20 compute the.**
124f4 20 20 20 20 20 6e 75 6d 62 65 72 20 6f 66 20 70 number of p
124f5 61 67 65 20 72 65 63 6f 72 64 73 20 66 72 6f 6d age records from
124f6 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 73 69 7a the journal siz
124f7 65 2e 0a 2a 2a 20 20 28 33 29 20 20 34 20 62 79 e..** (3) 4 by
124f8 74 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e te big-endian in
124f9 74 65 67 65 72 20 77 68 69 63 68 20 69 73 20 74 teger which is t
124fa 68 65 20 69 6e 69 74 69 61 6c 20 76 61 6c 75 65 he initial value
124fb 20 66 6f 72 20 74 68 65 20 0a 2a 2a 20 20 20 20 for the .**
124fc 20 20 20 73 61 6e 69 74 79 20 63 68 65 63 6b 73 sanity checks
124fd 75 6d 2e 0a 2a 2a 20 20 28 34 29 20 20 34 20 62 um..** (4) 4 b
124fe 79 74 65 20 69 6e 74 65 67 65 72 20 77 68 69 63 yte integer whic
124ff 68 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 h is the number
12500 6f 66 20 70 61 67 65 73 20 74 6f 20 74 72 75 6e of pages to trun
12501 63 61 74 65 20 74 68 65 0a 2a 2a 20 20 20 20 20 cate the.**
12502 20 20 64 61 74 61 62 61 73 65 20 74 6f 20 64 75 database to du
12503 72 69 6e 67 20 61 20 72 6f 6c 6c 62 61 63 6b 2e ring a rollback.
12504 0a 2a 2a 20 20 28 35 29 20 20 34 20 62 79 74 65 .** (5) 4 byte
12505 20 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65 big-endian inte
12506 67 65 72 20 77 68 69 63 68 20 69 73 20 74 68 65 ger which is the
12507 20 73 65 63 74 6f 72 20 73 69 7a 65 2e 20 20 54 sector size. T
12508 68 65 20 68 65 61 64 65 72 0a 2a 2a 20 20 20 20 he header.**
12509 20 20 20 69 73 20 74 68 69 73 20 6d 61 6e 79 20 is this many
1250a 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2e 0a 2a bytes in size..*
1250b 2a 20 20 28 36 29 20 20 34 20 62 79 74 65 20 62 * (6) 4 byte b
1250c 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 65 ig-endian intege
1250d 72 20 77 68 69 63 68 20 69 73 20 74 68 65 20 70 r which is the p
1250e 61 67 65 20 63 61 73 65 2e 0a 2a 2a 20 20 28 37 age case..** (7
1250f 29 20 20 34 20 62 79 74 65 20 69 6e 74 65 67 65 ) 4 byte intege
12510 72 20 77 68 69 63 68 20 69 73 20 74 68 65 20 6e r which is the n
12511 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 umber of bytes i
12512 6e 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 n the master jou
12513 72 6e 61 6c 0a 2a 2a 20 20 20 20 20 20 20 6e 61 rnal.** na
12514 6d 65 2e 20 20 54 68 65 20 76 61 6c 75 65 20 6d me. The value m
12515 61 79 20 62 65 20 7a 65 72 6f 20 28 69 6e 64 69 ay be zero (indi
12516 63 61 74 65 20 74 68 61 74 20 74 68 65 72 65 20 cate that there
12517 69 73 20 6e 6f 20 6d 61 73 74 65 72 0a 2a 2a 20 is no master.**
12518 20 20 20 20 20 20 6a 6f 75 72 6e 61 6c 2e 29 0a journal.).
12519 2a 2a 20 20 28 38 29 20 20 4e 20 62 79 74 65 73 ** (8) N bytes
1251a 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72 20 6a of the master j
1251b 6f 75 72 6e 61 6c 20 6e 61 6d 65 2e 20 20 54 68 ournal name. Th
1251c 65 20 6e 61 6d 65 20 77 69 6c 6c 20 62 65 20 6e e name will be n
1251d 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64 0a 2a 2a ul-terminated.**
1251e 20 20 20 20 20 20 20 61 6e 64 20 6d 69 67 68 74 and might
1251f 20 62 65 20 73 68 6f 72 74 65 72 20 74 68 61 6e be shorter than
12520 20 74 68 65 20 76 61 6c 75 65 20 72 65 61 64 20 the value read
12521 66 72 6f 6d 20 28 35 29 2e 20 20 49 66 20 74 68 from (5). If th
12522 65 20 66 69 72 73 74 20 62 79 74 65 0a 2a 2a 20 e first byte.**
12523 20 20 20 20 20 20 6f 66 20 74 68 65 20 6e 61 6d of the nam
12524 65 20 69 73 20 5c 30 30 30 20 74 68 65 6e 20 74 e is \000 then t
12525 68 65 72 65 20 69 73 20 6e 6f 20 6d 61 73 74 65 here is no maste
12526 72 20 6a 6f 75 72 6e 61 6c 2e 20 20 54 68 65 20 r journal. The
12527 6d 61 73 74 65 72 0a 2a 2a 20 20 20 20 20 20 20 master.**
12528 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 69 73 20 journal name is
12529 73 74 6f 72 65 64 20 69 6e 20 55 54 46 2d 38 2e stored in UTF-8.
1252a 0a 2a 2a 20 20 28 39 29 20 20 5a 65 72 6f 20 6f .** (9) Zero o
1252b 72 20 6d 6f 72 65 20 70 61 67 65 73 20 69 6e 73 r more pages ins
1252c 74 61 6e 63 65 73 2c 20 65 61 63 68 20 61 73 20 tances, each as
1252d 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 20 20 20 20 20 follows:.**
1252e 20 20 20 2b 20 20 34 20 62 79 74 65 20 70 61 67 + 4 byte pag
1252f 65 20 6e 75 6d 62 65 72 2e 0a 2a 2a 20 20 20 20 e number..**
12530 20 20 20 20 2b 20 20 70 50 61 67 65 72 2d 3e 70 + pPager->p
12531 61 67 65 53 69 7a 65 20 62 79 74 65 73 20 6f 66 ageSize bytes of
12532 20 64 61 74 61 2e 0a 2a 2a 20 20 20 20 20 20 20 data..**
12533 20 2b 20 20 34 20 62 79 74 65 20 63 68 65 63 6b + 4 byte check
12534 73 75 6d 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 77 sum.**.** When w
12535 65 20 73 70 65 61 6b 20 6f 66 20 74 68 65 20 6a e speak of the j
12536 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 2c 20 77 ournal header, w
12537 65 20 6d 65 61 6e 20 74 68 65 20 66 69 72 73 74 e mean the first
12538 20 38 20 69 74 65 6d 73 20 61 62 6f 76 65 2e 0a 8 items above..
12539 2a 2a 20 45 61 63 68 20 65 6e 74 72 79 20 69 6e ** Each entry in
1253a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 the journal is
1253b 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 an instance of t
1253c 68 65 20 39 74 68 20 69 74 65 6d 2e 0a 2a 2a 0a he 9th item..**.
1253d 2a 2a 20 43 61 6c 6c 20 74 68 65 20 76 61 6c 75 ** Call the valu
1253e 65 20 66 72 6f 6d 20 74 68 65 20 73 65 63 6f 6e e from the secon
1253f 64 20 62 75 6c 6c 65 74 20 22 6e 52 65 63 22 2e d bullet "nRec".
12540 20 20 6e 52 65 63 20 69 73 20 74 68 65 20 6e 75 nRec is the nu
12541 6d 62 65 72 20 6f 66 0a 2a 2a 20 76 61 6c 69 64 mber of.** valid
12542 20 70 61 67 65 20 65 6e 74 72 69 65 73 20 69 6e page entries in
12543 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 49 the journal. I
12544 6e 20 6d 6f 73 74 20 63 61 73 65 73 2c 20 79 6f n most cases, yo
12545 75 20 63 61 6e 20 63 6f 6d 70 75 74 65 20 74 68 u can compute th
12546 65 0a 2a 2a 20 76 61 6c 75 65 20 6f 66 20 6e 52 e.** value of nR
12547 65 63 20 66 72 6f 6d 20 74 68 65 20 73 69 7a 65 ec from the size
12548 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 of the journal
12549 66 69 6c 65 2e 20 20 42 75 74 20 69 66 20 61 20 file. But if a
1254a 70 6f 77 65 72 0a 2a 2a 20 66 61 69 6c 75 72 65 power.** failure
1254b 20 6f 63 63 75 72 72 65 64 20 77 68 69 6c 65 20 occurred while
1254c 74 68 65 20 6a 6f 75 72 6e 61 6c 20 77 61 73 20 the journal was
1254d 62 65 69 6e 67 20 77 72 69 74 74 65 6e 2c 20 69 being written, i
1254e 74 20 63 6f 75 6c 64 20 62 65 20 74 68 65 0a 2a t could be the.*
1254f 2a 20 63 61 73 65 20 74 68 61 74 20 74 68 65 20 * case that the
12550 73 69 7a 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 size of the jour
12551 6e 61 6c 20 66 69 6c 65 20 68 61 64 20 61 6c 72 nal file had alr
12552 65 61 64 79 20 62 65 65 6e 20 69 6e 63 72 65 61 eady been increa
12553 73 65 64 20 62 75 74 0a 2a 2a 20 74 68 65 20 65 sed but.** the e
12554 78 74 72 61 20 65 6e 74 72 69 65 73 20 68 61 64 xtra entries had
12555 20 6e 6f 74 20 79 65 74 20 6d 61 64 65 20 69 74 not yet made it
12556 20 73 61 66 65 6c 79 20 74 6f 20 64 69 73 6b 2e safely to disk.
12557 20 20 49 6e 20 73 75 63 68 20 61 20 63 61 73 65 In such a case
12558 2c 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f ,.** the value o
12559 66 20 6e 52 65 63 20 63 6f 6d 70 75 74 65 64 20 f nRec computed
1255a 66 72 6f 6d 20 74 68 65 20 66 69 6c 65 20 73 69 from the file si
1255b 7a 65 20 77 6f 75 6c 64 20 62 65 20 74 6f 6f 20 ze would be too
1255c 6c 61 72 67 65 2e 20 20 46 6f 72 0a 2a 2a 20 74 large. For.** t
1255d 68 61 74 20 72 65 61 73 6f 6e 2c 20 77 65 20 61 hat reason, we a
1255e 6c 77 61 79 73 20 75 73 65 20 74 68 65 20 6e 52 lways use the nR
1255f 65 63 20 76 61 6c 75 65 20 69 6e 20 74 68 65 20 ec value in the
12560 68 65 61 64 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 header..**.** If
12561 20 74 68 65 20 6e 52 65 63 20 76 61 6c 75 65 20 the nRec value
12562 69 73 20 30 78 66 66 66 66 66 66 66 66 20 69 74 is 0xffffffff it
12563 20 6d 65 61 6e 73 20 74 68 61 74 20 6e 52 65 63 means that nRec
12564 20 73 68 6f 75 6c 64 20 62 65 20 63 6f 6d 70 75 should be compu
12565 74 65 64 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 ted.** from the
12566 66 69 6c 65 20 73 69 7a 65 2e 20 20 54 68 69 73 file size. This
12567 20 76 61 6c 75 65 20 69 73 20 75 73 65 64 20 77 value is used w
12568 68 65 6e 20 74 68 65 20 75 73 65 72 20 73 65 6c hen the user sel
12569 65 63 74 73 20 74 68 65 0a 2a 2a 20 6e 6f 2d 73 ects the.** no-s
1256a 79 6e 63 20 6f 70 74 69 6f 6e 20 66 6f 72 20 74 ync option for t
1256b 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 41 20 70 he journal. A p
1256c 6f 77 65 72 20 66 61 69 6c 75 72 65 20 63 6f 75 ower failure cou
1256d 6c 64 20 6c 65 61 64 20 74 6f 20 63 6f 72 72 75 ld lead to corru
1256e 70 74 69 6f 6e 0a 2a 2a 20 69 6e 20 74 68 69 73 ption.** in this
1256f 20 63 61 73 65 2e 20 20 42 75 74 20 66 6f 72 20 case. But for
12570 74 68 69 6e 67 73 20 6c 69 6b 65 20 74 65 6d 70 things like temp
12571 6f 72 61 72 79 20 74 61 62 6c 65 20 28 77 68 69 orary table (whi
12572 63 68 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 64 65 ch will be.** de
12573 6c 65 74 65 64 20 77 68 65 6e 20 74 68 65 20 70 leted when the p
12574 6f 77 65 72 20 69 73 20 72 65 73 74 6f 72 65 64 ower is restored
12575 29 20 77 65 20 64 6f 6e 27 74 20 63 61 72 65 2e ) we don't care.
12576 20 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 .**.** If the
12577 66 69 6c 65 20 6f 70 65 6e 65 64 20 61 73 20 74 file opened as t
12578 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
12579 69 73 20 6e 6f 74 20 61 20 77 65 6c 6c 2d 66 6f is not a well-fo
1257a 72 6d 65 64 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 rmed.** journal
1257b 66 69 6c 65 20 74 68 65 6e 20 61 6c 6c 20 70 61 file then all pa
1257c 67 65 73 20 75 70 20 74 6f 20 74 68 65 20 66 69 ges up to the fi
1257d 72 73 74 20 63 6f 72 72 75 70 74 65 64 20 70 61 rst corrupted pa
1257e 67 65 20 61 72 65 20 72 6f 6c 6c 65 64 0a 2a 2a ge are rolled.**
1257f 20 62 61 63 6b 20 28 6f 72 20 6e 6f 20 70 61 67 back (or no pag
12580 65 73 20 69 66 20 74 68 65 20 6a 6f 75 72 6e 61 es if the journa
12581 6c 20 68 65 61 64 65 72 20 69 73 20 63 6f 72 72 l header is corr
12582 75 70 74 65 64 29 2e 20 54 68 65 20 6a 6f 75 72 upted). The jour
12583 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 69 73 20 74 nal file.** is t
12584 68 65 6e 20 64 65 6c 65 74 65 64 20 61 6e 64 20 hen deleted and
12585 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e SQLITE_OK return
12586 65 64 2c 20 6a 75 73 74 20 61 73 20 69 66 20 6e ed, just as if n
12587 6f 20 63 6f 72 72 75 70 74 69 6f 6e 20 68 61 64 o corruption had
12588 0a 2a 2a 20 62 65 65 6e 20 65 6e 63 6f 75 6e 74 .** been encount
12589 65 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 ered..**.** If a
1258a 6e 20 49 2f 4f 20 6f 72 20 6d 61 6c 6c 6f 63 28 n I/O or malloc(
1258b 29 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 ) error occurs,
1258c 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 66 69 6c 65 the journal-file
1258d 20 69 73 20 6e 6f 74 20 64 65 6c 65 74 65 64 0a is not deleted.
1258e 2a 2a 20 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 ** and an error
1258f 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 code is returned
12590 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 73 48 6f ..**.** The isHo
12591 74 20 70 61 72 61 6d 65 74 65 72 20 69 6e 64 69 t parameter indi
12592 63 61 74 65 73 20 74 68 61 74 20 77 65 20 61 72 cates that we ar
12593 65 20 74 72 79 69 6e 67 20 74 6f 20 72 6f 6c 6c e trying to roll
12594 62 61 63 6b 20 61 20 6a 6f 75 72 6e 61 6c 0a 2a back a journal.*
12595 2a 20 74 68 61 74 20 6d 69 67 68 74 20 62 65 20 * that might be
12596 61 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 2e 20 20 a hot journal.
12597 4f 72 2c 20 69 74 20 63 6f 75 6c 64 20 62 65 20 Or, it could be
12598 74 68 61 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c that the journal
12599 20 69 73 20 0a 2a 2a 20 70 72 65 73 65 72 76 65 is .** preserve
1259a 64 20 62 65 63 61 75 73 65 20 6f 66 20 4a 4f 55 d because of JOU
1259b 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54 RNALMODE_PERSIST
1259c 20 6f 72 20 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f or JOURNALMODE_
1259d 54 52 55 4e 43 41 54 45 2e 0a 2a 2a 20 49 66 20 TRUNCATE..** If
1259e 74 68 65 20 6a 6f 75 72 6e 61 6c 20 72 65 61 6c the journal real
1259f 6c 79 20 69 73 20 68 6f 74 2c 20 72 65 73 65 74 ly is hot, reset
125a0 20 74 68 65 20 70 61 67 65 72 20 63 61 63 68 65 the pager cache
125a1 20 70 72 69 6f 72 20 72 6f 6c 6c 69 6e 67 0a 2a prior rolling.*
125a2 2a 20 62 61 63 6b 20 61 6e 79 20 63 6f 6e 74 65 * back any conte
125a3 6e 74 2e 20 20 49 66 20 74 68 65 20 6a 6f 75 72 nt. If the jour
125a4 6e 61 6c 20 69 73 20 6d 65 72 65 6c 79 20 70 65 nal is merely pe
125a5 72 73 69 73 74 65 6e 74 2c 20 6e 6f 20 72 65 73 rsistent, no res
125a6 65 74 20 69 73 0a 2a 2a 20 6e 65 65 64 65 64 2e et is.** needed.
125a7 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 .*/.static int p
125a8 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 50 61 ager_playback(Pa
125a9 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 ger *pPager, int
125aa 20 69 73 48 6f 74 29 7b 0a 20 20 73 71 6c 69 74 isHot){. sqlit
125ab 65 33 5f 76 66 73 20 2a 70 56 66 73 20 3d 20 70 e3_vfs *pVfs = p
125ac 50 61 67 65 72 2d 3e 70 56 66 73 3b 0a 20 20 69 Pager->pVfs;. i
125ad 36 34 20 73 7a 4a 3b 20 20 20 20 20 20 20 20 20 64 szJ;
125ae 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 /* Size
125af 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 of the journal f
125b0 69 6c 65 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a ile in bytes */.
125b1 20 20 75 33 32 20 6e 52 65 63 3b 20 20 20 20 20 u32 nRec;
125b2 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
125b3 6d 62 65 72 20 6f 66 20 52 65 63 6f 72 64 73 20 mber of Records
125b4 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 2a in the journal *
125b5 2f 0a 20 20 75 33 32 20 75 3b 20 20 20 20 20 20 /. u32 u;
125b6 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
125b7 55 6e 73 69 67 6e 65 64 20 6c 6f 6f 70 20 63 6f Unsigned loop co
125b8 75 6e 74 65 72 20 2a 2f 0a 20 20 50 67 6e 6f 20 unter */. Pgno
125b9 6d 78 50 67 20 3d 20 30 3b 20 20 20 20 20 20 20 mxPg = 0;
125ba 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 /* Size of t
125bb 68 65 20 6f 72 69 67 69 6e 61 6c 20 66 69 6c 65 he original file
125bc 20 69 6e 20 70 61 67 65 73 20 2a 2f 0a 20 20 69 in pages */. i
125bd 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 nt rc;
125be 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c /* Resul
125bf 74 20 63 6f 64 65 20 6f 66 20 61 20 73 75 62 72 t code of a subr
125c0 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 outine */. int
125c1 72 65 73 20 3d 20 31 3b 20 20 20 20 20 20 20 20 res = 1;
125c2 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 72 65 /* Value re
125c3 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 turned by sqlite
125c4 33 4f 73 41 63 63 65 73 73 28 29 20 2a 2f 0a 20 3OsAccess() */.
125c5 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 20 3d char *zMaster =
125c6 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 0; /* Nam
125c7 65 20 6f 66 20 6d 61 73 74 65 72 20 6a 6f 75 72 e of master jour
125c8 6e 61 6c 20 66 69 6c 65 20 69 66 20 61 6e 79 20 nal file if any
125c9 2a 2f 0a 20 20 69 6e 74 20 6e 65 65 64 50 61 67 */. int needPag
125ca 65 72 52 65 73 65 74 3b 20 20 20 20 20 20 2f 2a erReset; /*
125cb 20 54 72 75 65 20 74 6f 20 72 65 73 65 74 20 70 True to reset p
125cc 61 67 65 20 70 72 69 6f 72 20 74 6f 20 66 69 72 age prior to fir
125cd 73 74 20 70 61 67 65 20 72 6f 6c 6c 62 61 63 6b st page rollback
125ce 20 2a 2f 0a 0a 20 20 2f 2a 20 46 69 67 75 72 65 */.. /* Figure
125cf 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20 72 65 out how many re
125d0 63 6f 72 64 73 20 61 72 65 20 69 6e 20 74 68 65 cords are in the
125d1 20 6a 6f 75 72 6e 61 6c 2e 20 20 41 62 6f 72 74 journal. Abort
125d2 20 65 61 72 6c 79 20 69 66 0a 20 20 2a 2a 20 74 early if. ** t
125d3 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 65 6d he journal is em
125d4 70 74 79 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 pty.. */. asse
125d5 72 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 rt( isOpen(pPage
125d6 72 2d 3e 6a 66 64 29 20 29 3b 0a 20 20 72 63 20 r->jfd) );. rc
125d7 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 = sqlite3OsFileS
125d8 69 7a 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c ize(pPager->jfd,
125d9 20 26 73 7a 4a 29 3b 0a 20 20 69 66 28 20 72 63 &szJ);. if( rc
125da 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 73 !=SQLITE_OK || s
125db 7a 4a 3d 3d 30 20 29 7b 0a 20 20 20 20 67 6f 74 zJ==0 ){. got
125dc 6f 20 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3b 0a o end_playback;.
125dd 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 61 64 20 74 }.. /* Read t
125de 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 he master journa
125df 6c 20 6e 61 6d 65 20 66 72 6f 6d 20 74 68 65 20 l name from the
125e0 6a 6f 75 72 6e 61 6c 2c 20 69 66 20 69 74 20 69 journal, if it i
125e1 73 20 70 72 65 73 65 6e 74 2e 0a 20 20 2a 2a 20 s present.. **
125e2 49 66 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 If a master jour
125e3 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 69 73 nal file name is
125e4 20 73 70 65 63 69 66 69 65 64 2c 20 62 75 74 20 specified, but
125e5 74 68 65 20 66 69 6c 65 20 69 73 20 6e 6f 74 0a the file is not.
125e6 20 20 2a 2a 20 70 72 65 73 65 6e 74 20 6f 6e 20 ** present on
125e7 64 69 73 6b 2c 20 74 68 65 6e 20 74 68 65 20 6a disk, then the j
125e8 6f 75 72 6e 61 6c 20 69 73 20 6e 6f 74 20 68 6f ournal is not ho
125e9 74 20 61 6e 64 20 64 6f 65 73 20 6e 6f 74 20 6e t and does not n
125ea 65 65 64 20 74 6f 20 62 65 0a 20 20 2a 2a 20 70 eed to be. ** p
125eb 6c 61 79 65 64 20 62 61 63 6b 2e 0a 20 20 2a 2a layed back.. **
125ec 0a 20 20 2a 2a 20 54 4f 44 4f 3a 20 54 65 63 68 . ** TODO: Tech
125ed 6e 69 63 61 6c 6c 79 20 74 68 65 20 66 6f 6c 6c nically the foll
125ee 6f 77 69 6e 67 20 69 73 20 61 6e 20 65 72 72 6f owing is an erro
125ef 72 20 62 65 63 61 75 73 65 20 69 74 20 61 73 73 r because it ass
125f0 75 6d 65 73 20 74 68 61 74 0a 20 20 2a 2a 20 62 umes that. ** b
125f1 75 66 66 65 72 20 50 61 67 65 72 2e 70 54 6d 70 uffer Pager.pTmp
125f2 53 70 61 63 65 20 69 73 20 28 6d 78 50 61 74 68 Space is (mxPath
125f3 6e 61 6d 65 2b 31 29 20 62 79 74 65 73 20 6f 72 name+1) bytes or
125f4 20 6c 61 72 67 65 72 2e 20 69 2e 65 2e 20 74 68 larger. i.e. th
125f5 61 74 0a 20 20 2a 2a 20 28 70 50 61 67 65 72 2d at. ** (pPager-
125f6 3e 70 61 67 65 53 69 7a 65 20 3e 3d 20 70 50 61 >pageSize >= pPa
125f7 67 65 72 2d 3e 70 56 66 73 2d 3e 6d 78 50 61 74 ger->pVfs->mxPat
125f8 68 6e 61 6d 65 2b 31 29 2e 20 55 73 69 6e 67 20 hname+1). Using
125f9 6f 73 5f 75 6e 69 78 2e 63 2c 0a 20 20 2a 2a 20 os_unix.c,. **
125fa 20 6d 78 50 61 74 68 6e 61 6d 65 20 69 73 20 35 mxPathname is 5
125fb 31 32 2c 20 77 68 69 63 68 20 69 73 20 74 68 65 12, which is the
125fc 20 73 61 6d 65 20 61 73 20 74 68 65 20 6d 69 6e same as the min
125fd 69 6d 75 6d 20 61 6c 6c 6f 77 61 62 6c 65 20 76 imum allowable v
125fe 61 6c 75 65 0a 20 20 2a 2a 20 66 6f 72 20 70 61 alue. ** for pa
125ff 67 65 53 69 7a 65 2e 0a 20 20 2a 2f 0a 20 20 7a geSize.. */. z
12600 4d 61 73 74 65 72 20 3d 20 70 50 61 67 65 72 2d Master = pPager-
12601 3e 70 54 6d 70 53 70 61 63 65 3b 0a 20 20 72 63 >pTmpSpace;. rc
12602 20 3d 20 72 65 61 64 4d 61 73 74 65 72 4a 6f 75 = readMasterJou
12603 72 6e 61 6c 28 70 50 61 67 65 72 2d 3e 6a 66 64 rnal(pPager->jfd
12604 2c 20 7a 4d 61 73 74 65 72 2c 20 70 50 61 67 65 , zMaster, pPage
12605 72 2d 3e 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e r->pVfs->mxPathn
12606 61 6d 65 2b 31 29 3b 0a 20 20 69 66 28 20 72 63 ame+1);. if( rc
12607 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 7a ==SQLITE_OK && z
12608 4d 61 73 74 65 72 5b 30 5d 20 29 7b 0a 20 20 20 Master[0] ){.
12609 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 41 rc = sqlite3OsA
1260a 63 63 65 73 73 28 70 56 66 73 2c 20 7a 4d 61 73 ccess(pVfs, zMas
1260b 74 65 72 2c 20 53 51 4c 49 54 45 5f 41 43 43 45 ter, SQLITE_ACCE
1260c 53 53 5f 45 58 49 53 54 53 2c 20 26 72 65 73 29 SS_EXISTS, &res)
1260d 3b 0a 20 20 7d 0a 20 20 7a 4d 61 73 74 65 72 20 ;. }. zMaster
1260e 3d 20 30 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 = 0;. if( rc!=S
1260f 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 21 72 65 73 QLITE_OK || !res
12610 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65 6e 64 ){. goto end
12611 5f 70 6c 61 79 62 61 63 6b 3b 0a 20 20 7d 0a 20 _playback;. }.
12612 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
12613 4f 66 66 20 3d 20 30 3b 0a 20 20 6e 65 65 64 50 Off = 0;. needP
12614 61 67 65 72 52 65 73 65 74 20 3d 20 69 73 48 6f agerReset = isHo
12615 74 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 6c 6f t;.. /* This lo
12616 6f 70 20 74 65 72 6d 69 6e 61 74 65 73 20 65 69 op terminates ei
12617 74 68 65 72 20 77 68 65 6e 20 61 20 72 65 61 64 ther when a read
12618 4a 6f 75 72 6e 61 6c 48 64 72 28 29 20 6f 72 20 JournalHdr() or
12619 0a 20 20 2a 2a 20 70 61 67 65 72 5f 70 6c 61 79 . ** pager_play
1261a 62 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 29 20 back_one_page()
1261b 63 61 6c 6c 20 72 65 74 75 72 6e 73 20 53 51 4c call returns SQL
1261c 49 54 45 5f 44 4f 4e 45 20 6f 72 20 61 6e 20 49 ITE_DONE or an I
1261d 4f 20 65 72 72 6f 72 20 0a 20 20 2a 2a 20 6f 63 O error . ** oc
1261e 63 75 72 73 2e 20 0a 20 20 2a 2f 0a 20 20 77 68 curs. . */. wh
1261f 69 6c 65 28 20 31 20 29 7b 0a 20 20 20 20 69 6e ile( 1 ){. in
12620 74 20 69 73 55 6e 73 79 6e 63 20 3d 20 30 3b 0a t isUnsync = 0;.
12621 0a 20 20 20 20 2f 2a 20 52 65 61 64 20 74 68 65 . /* Read the
12622 20 6e 65 78 74 20 6a 6f 75 72 6e 61 6c 20 68 65 next journal he
12623 61 64 65 72 20 66 72 6f 6d 20 74 68 65 20 6a 6f ader from the jo
12624 75 72 6e 61 6c 20 66 69 6c 65 2e 20 20 49 66 20 urnal file. If
12625 74 68 65 72 65 20 61 72 65 0a 20 20 20 20 2a 2a there are. **
12626 20 6e 6f 74 20 65 6e 6f 75 67 68 20 62 79 74 65 not enough byte
12627 73 20 6c 65 66 74 20 69 6e 20 74 68 65 20 6a 6f s left in the jo
12628 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 20 61 urnal file for a
12629 20 63 6f 6d 70 6c 65 74 65 20 68 65 61 64 65 72 complete header
1262a 2c 20 6f 72 0a 20 20 20 20 2a 2a 20 69 74 20 69 , or. ** it i
1262b 73 20 63 6f 72 72 75 70 74 65 64 2c 20 74 68 65 s corrupted, the
1262c 6e 20 61 20 70 72 6f 63 65 73 73 20 6d 75 73 74 n a process must
1262d 20 6f 66 20 66 61 69 6c 65 64 20 77 68 69 6c 65 of failed while
1262e 20 77 72 69 74 69 6e 67 20 69 74 2e 0a 20 20 20 writing it..
1262f 20 2a 2a 20 54 68 69 73 20 69 6e 64 69 63 61 74 ** This indicat
12630 65 73 20 6e 6f 74 68 69 6e 67 20 6d 6f 72 65 20 es nothing more
12631 6e 65 65 64 73 20 74 6f 20 62 65 20 72 6f 6c 6c needs to be roll
12632 65 64 20 62 61 63 6b 2e 0a 20 20 20 20 2a 2f 0a ed back.. */.
12633 20 20 20 20 72 63 20 3d 20 72 65 61 64 4a 6f 75 rc = readJou
12634 72 6e 61 6c 48 64 72 28 70 50 61 67 65 72 2c 20 rnalHdr(pPager,
12635 69 73 48 6f 74 2c 20 73 7a 4a 2c 20 26 6e 52 65 isHot, szJ, &nRe
12636 63 2c 20 26 6d 78 50 67 29 3b 0a 20 20 20 20 69 c, &mxPg);. i
12637 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
12638 20 29 7b 20 0a 20 20 20 20 20 20 69 66 28 20 72 ){ . if( r
12639 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 c==SQLITE_DONE )
1263a 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 {. rc = S
1263b 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 QLITE_OK;.
1263c 7d 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 }. goto end
1263d 5f 70 6c 61 79 62 61 63 6b 3b 0a 20 20 20 20 7d _playback;. }
1263e 0a 0a 20 20 20 20 2f 2a 20 49 66 20 6e 52 65 63 .. /* If nRec
1263f 20 69 73 20 30 78 66 66 66 66 66 66 66 66 2c 20 is 0xffffffff,
12640 74 68 65 6e 20 74 68 69 73 20 6a 6f 75 72 6e 61 then this journa
12641 6c 20 77 61 73 20 63 72 65 61 74 65 64 20 62 79 l was created by
12642 20 61 20 70 72 6f 63 65 73 73 0a 20 20 20 20 2a a process. *
12643 2a 20 77 6f 72 6b 69 6e 67 20 69 6e 20 6e 6f 2d * working in no-
12644 73 79 6e 63 20 6d 6f 64 65 2e 20 54 68 69 73 20 sync mode. This
12645 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 72 means that the r
12646 65 73 74 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e est of the journ
12647 61 6c 0a 20 20 20 20 2a 2a 20 66 69 6c 65 20 63 al. ** file c
12648 6f 6e 73 69 73 74 73 20 6f 66 20 70 61 67 65 73 onsists of pages
12649 2c 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 6d , there are no m
1264a 6f 72 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 ore journal head
1264b 65 72 73 2e 20 43 6f 6d 70 75 74 65 0a 20 20 20 ers. Compute.
1264c 20 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66 ** the value of
1264d 20 6e 52 65 63 20 62 61 73 65 64 20 6f 6e 20 74 nRec based on t
1264e 68 69 73 20 61 73 73 75 6d 70 74 69 6f 6e 2e 0a his assumption..
1264f 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 6e */. if( n
12650 52 65 63 3d 3d 30 78 66 66 66 66 66 66 66 66 20 Rec==0xffffffff
12651 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 ){. assert(
12652 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
12653 4f 66 66 3d 3d 4a 4f 55 52 4e 41 4c 5f 48 44 52 Off==JOURNAL_HDR
12654 5f 53 5a 28 70 50 61 67 65 72 29 20 29 3b 0a 20 _SZ(pPager) );.
12655 20 20 20 20 20 6e 52 65 63 20 3d 20 28 69 6e 74 nRec = (int
12656 29 28 28 73 7a 4a 20 2d 20 4a 4f 55 52 4e 41 4c )((szJ - JOURNAL
12657 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 29 _HDR_SZ(pPager))
12658 2f 4a 4f 55 52 4e 41 4c 5f 50 47 5f 53 5a 28 70 /JOURNAL_PG_SZ(p
12659 50 61 67 65 72 29 29 3b 0a 20 20 20 20 7d 0a 0a Pager));. }..
1265a 20 20 20 20 2f 2a 20 49 66 20 6e 52 65 63 20 69 /* If nRec i
1265b 73 20 30 20 61 6e 64 20 74 68 69 73 20 72 6f 6c s 0 and this rol
1265c 6c 62 61 63 6b 20 69 73 20 6f 66 20 61 20 74 72 lback is of a tr
1265d 61 6e 73 61 63 74 69 6f 6e 20 63 72 65 61 74 65 ansaction create
1265e 64 20 62 79 20 74 68 69 73 0a 20 20 20 20 2a 2a d by this. **
1265f 20 70 72 6f 63 65 73 73 20 61 6e 64 20 69 66 20 process and if
12660 74 68 69 73 20 69 73 20 74 68 65 20 66 69 6e 61 this is the fina
12661 6c 20 68 65 61 64 65 72 20 69 6e 20 74 68 65 20 l header in the
12662 6a 6f 75 72 6e 61 6c 2c 20 74 68 65 6e 20 69 74 journal, then it
12663 20 6d 65 61 6e 73 0a 20 20 20 20 2a 2a 20 74 68 means. ** th
12664 61 74 20 74 68 69 73 20 70 61 72 74 20 6f 66 20 at this part of
12665 74 68 65 20 6a 6f 75 72 6e 61 6c 20 77 61 73 20 the journal was
12666 62 65 69 6e 67 20 66 69 6c 6c 65 64 20 62 75 74 being filled but
12667 20 68 61 73 20 6e 6f 74 20 79 65 74 20 62 65 65 has not yet bee
12668 6e 0a 20 20 20 20 2a 2a 20 73 79 6e 63 65 64 20 n. ** synced
12669 74 6f 20 64 69 73 6b 2e 20 20 43 6f 6d 70 75 74 to disk. Comput
1266a 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 e the number of
1266b 70 61 67 65 73 20 62 61 73 65 64 20 6f 6e 20 74 pages based on t
1266c 68 65 20 72 65 6d 61 69 6e 69 6e 67 0a 20 20 20 he remaining.
1266d 20 2a 2a 20 73 69 7a 65 20 6f 66 20 74 68 65 20 ** size of the
1266e 66 69 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 file.. **.
1266f 20 2a 2a 20 54 68 65 20 74 68 69 72 64 20 74 65 ** The third te
12670 72 6d 20 6f 66 20 74 68 65 20 74 65 73 74 20 77 rm of the test w
12671 61 73 20 61 64 64 65 64 20 74 6f 20 66 69 78 20 as added to fix
12672 74 69 63 6b 65 74 20 23 32 35 36 35 2e 0a 20 20 ticket #2565..
12673 20 20 2a 2a 20 57 68 65 6e 20 72 6f 6c 6c 69 6e ** When rollin
12674 67 20 62 61 63 6b 20 61 20 68 6f 74 20 6a 6f 75 g back a hot jou
12675 72 6e 61 6c 2c 20 6e 52 65 63 3d 3d 30 20 61 6c rnal, nRec==0 al
12676 77 61 79 73 20 6d 65 61 6e 73 20 74 68 61 74 20 ways means that
12677 74 68 65 20 6e 65 78 74 0a 20 20 20 20 2a 2a 20 the next. **
12678 63 68 75 6e 6b 20 6f 66 20 74 68 65 20 6a 6f 75 chunk of the jou
12679 72 6e 61 6c 20 63 6f 6e 74 61 69 6e 73 20 7a 65 rnal contains ze
1267a 72 6f 20 70 61 67 65 73 20 74 6f 20 62 65 20 72 ro pages to be r
1267b 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 20 42 75 74 olled back. But
1267c 0a 20 20 20 20 2a 2a 20 77 68 65 6e 20 64 6f 69 . ** when doi
1267d 6e 67 20 61 20 52 4f 4c 4c 42 41 43 4b 20 61 6e ng a ROLLBACK an
1267e 64 20 74 68 65 20 6e 52 65 63 3d 3d 30 20 63 68 d the nRec==0 ch
1267f 75 6e 6b 20 69 73 20 74 68 65 20 6c 61 73 74 20 unk is the last
12680 63 68 75 6e 6b 20 69 6e 0a 20 20 20 20 2a 2a 20 chunk in. **
12681 74 68 65 20 6a 6f 75 72 6e 61 6c 2c 20 69 74 20 the journal, it
12682 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 6a means that the j
12683 6f 75 72 6e 61 6c 20 6d 69 67 68 74 20 63 6f 6e ournal might con
12684 74 61 69 6e 20 61 64 64 69 74 69 6f 6e 61 6c 0a tain additional.
12685 20 20 20 20 2a 2a 20 70 61 67 65 73 20 74 68 61 ** pages tha
12686 74 20 6e 65 65 64 20 74 6f 20 62 65 20 72 6f 6c t need to be rol
12687 6c 65 64 20 62 61 63 6b 20 61 6e 64 20 74 68 61 led back and tha
12688 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 t the number of
12689 70 61 67 65 73 20 0a 20 20 20 20 2a 2a 20 73 68 pages . ** sh
1268a 6f 75 6c 64 20 62 65 20 63 6f 6d 70 75 74 65 64 ould be computed
1268b 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20 6a 6f based on the jo
1268c 75 72 6e 61 6c 20 66 69 6c 65 20 73 69 7a 65 2e urnal file size.
1268d 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 . */. if(
1268e 6e 52 65 63 3d 3d 30 20 26 26 20 21 69 73 48 6f nRec==0 && !isHo
1268f 74 20 26 26 0a 20 20 20 20 20 20 20 20 70 50 61 t &&. pPa
12690 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 2b ger->journalHdr+
12691 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 JOURNAL_HDR_SZ(p
12692 50 61 67 65 72 29 3d 3d 70 50 61 67 65 72 2d 3e Pager)==pPager->
12693 6a 6f 75 72 6e 61 6c 4f 66 66 20 29 7b 0a 20 20 journalOff ){.
12694 20 20 20 20 6e 52 65 63 20 3d 20 28 69 6e 74 29 nRec = (int)
12695 28 28 73 7a 4a 20 2d 20 70 50 61 67 65 72 2d 3e ((szJ - pPager->
12696 6a 6f 75 72 6e 61 6c 4f 66 66 29 20 2f 20 4a 4f journalOff) / JO
12697 55 52 4e 41 4c 5f 50 47 5f 53 5a 28 70 50 61 67 URNAL_PG_SZ(pPag
12698 65 72 29 29 3b 0a 20 20 20 20 20 20 69 73 55 6e er));. isUn
12699 73 79 6e 63 20 3d 20 31 3b 0a 20 20 20 20 7d 0a sync = 1;. }.
1269a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 . /* If this
1269b 69 73 20 74 68 65 20 66 69 72 73 74 20 68 65 61 is the first hea
1269c 64 65 72 20 72 65 61 64 20 66 72 6f 6d 20 74 68 der read from th
1269d 65 20 6a 6f 75 72 6e 61 6c 2c 20 74 72 75 6e 63 e journal, trunc
1269e 61 74 65 20 74 68 65 0a 20 20 20 20 2a 2a 20 64 ate the. ** d
1269f 61 74 61 62 61 73 65 20 66 69 6c 65 20 62 61 63 atabase file bac
126a0 6b 20 74 6f 20 69 74 73 20 6f 72 69 67 69 6e 61 k to its origina
126a1 6c 20 73 69 7a 65 2e 0a 20 20 20 20 2a 2f 0a 20 l size.. */.
126a2 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6a if( pPager->j
126a3 6f 75 72 6e 61 6c 4f 66 66 3d 3d 4a 4f 55 52 4e ournalOff==JOURN
126a4 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 AL_HDR_SZ(pPager
126a5 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 ) ){. rc =
126a6 70 61 67 65 72 5f 74 72 75 6e 63 61 74 65 28 70 pager_truncate(p
126a7 50 61 67 65 72 2c 20 6d 78 50 67 29 3b 0a 20 20 Pager, mxPg);.
126a8 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
126a9 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
126aa 20 67 6f 74 6f 20 65 6e 64 5f 70 6c 61 79 62 61 goto end_playba
126ab 63 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ck;. }.
126ac 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 pPager->dbSize
126ad 20 3d 20 6d 78 50 67 3b 0a 20 20 20 20 7d 0a 0a = mxPg;. }..
126ae 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f 72 69 67 /* Copy orig
126af 69 6e 61 6c 20 70 61 67 65 73 20 6f 75 74 20 6f inal pages out o
126b0 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 61 6e f the journal an
126b1 64 20 62 61 63 6b 20 69 6e 74 6f 20 74 68 65 20 d back into the
126b2 0a 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 . ** database
126b3 20 66 69 6c 65 20 61 6e 64 2f 6f 72 20 70 61 67 file and/or pag
126b4 65 20 63 61 63 68 65 2e 0a 20 20 20 20 2a 2f 0a e cache.. */.
126b5 20 20 20 20 66 6f 72 28 75 3d 30 3b 20 75 3c 6e for(u=0; u<n
126b6 52 65 63 3b 20 75 2b 2b 29 7b 0a 20 20 20 20 20 Rec; u++){.
126b7 20 69 66 28 20 6e 65 65 64 50 61 67 65 72 52 65 if( needPagerRe
126b8 73 65 74 20 29 7b 0a 20 20 20 20 20 20 20 20 70 set ){. p
126b9 61 67 65 72 5f 72 65 73 65 74 28 70 50 61 67 65 ager_reset(pPage
126ba 72 29 3b 0a 20 20 20 20 20 20 20 20 6e 65 65 64 r);. need
126bb 50 61 67 65 72 52 65 73 65 74 20 3d 20 30 3b 0a PagerReset = 0;.
126bc 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 63 }. rc
126bd 20 3d 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 = pager_playbac
126be 6b 5f 6f 6e 65 5f 70 61 67 65 28 70 50 61 67 65 k_one_page(pPage
126bf 72 2c 31 2c 69 73 55 6e 73 79 6e 63 2c 26 70 50 r,1,isUnsync,&pP
126c0 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 ager->journalOff
126c1 2c 30 2c 30 29 3b 0a 20 20 20 20 20 20 69 66 28 ,0,0);. if(
126c2 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
126c3 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 {. if( rc
126c4 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b ==SQLITE_DONE ){
126c5 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 . rc =
126c6 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 SQLITE_OK;.
126c7 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 pPager->jou
126c8 72 6e 61 6c 4f 66 66 20 3d 20 73 7a 4a 3b 0a 20 rnalOff = szJ;.
126c9 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a break;.
126ca 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }else{.
126cb 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 77 /* If w
126cc 65 20 61 72 65 20 75 6e 61 62 6c 65 20 74 6f 20 e are unable to
126cd 72 6f 6c 6c 62 61 63 6b 2c 20 71 75 69 74 20 61 rollback, quit a
126ce 6e 64 20 72 65 74 75 72 6e 20 74 68 65 20 65 72 nd return the er
126cf 72 6f 72 0a 20 20 20 20 20 20 20 20 20 20 2a 2a ror. **
126d0 20 63 6f 64 65 2e 20 20 54 68 69 73 20 77 69 6c code. This wil
126d1 6c 20 63 61 75 73 65 20 74 68 65 20 70 61 67 65 l cause the page
126d2 72 20 74 6f 20 65 6e 74 65 72 20 74 68 65 20 65 r to enter the e
126d3 72 72 6f 72 20 73 74 61 74 65 0a 20 20 20 20 20 rror state.
126d4 20 20 20 20 20 2a 2a 20 73 6f 20 74 68 61 74 20 ** so that
126d5 6e 6f 20 66 75 72 74 68 65 72 20 68 61 72 6d 20 no further harm
126d6 77 69 6c 6c 20 62 65 20 64 6f 6e 65 2e 20 20 50 will be done. P
126d7 65 72 68 61 70 73 20 74 68 65 20 6e 65 78 74 0a erhaps the next.
126d8 20 20 20 20 20 20 20 20 20 20 2a 2a 20 70 72 6f ** pro
126d9 63 65 73 73 20 74 6f 20 63 6f 6d 65 20 61 6c 6f cess to come alo
126da 6e 67 20 77 69 6c 6c 20 62 65 20 61 62 6c 65 20 ng will be able
126db 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20 to rollback the
126dc 64 61 74 61 62 61 73 65 2e 0a 20 20 20 20 20 20 database..
126dd 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 */.
126de 20 67 6f 74 6f 20 65 6e 64 5f 70 6c 61 79 62 61 goto end_playba
126df 63 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 ck;. }.
126e0 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a }. }. }.
126e1 20 20 2f 2a 4e 4f 54 52 45 41 43 48 45 44 2a 2f /*NOTREACHED*/
126e2 0a 20 20 61 73 73 65 72 74 28 20 30 20 29 3b 0a . assert( 0 );.
126e3 0a 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3a 0a 20 .end_playback:.
126e4 20 2f 2a 20 46 6f 6c 6c 6f 77 69 6e 67 20 61 20 /* Following a
126e5 72 6f 6c 6c 62 61 63 6b 2c 20 74 68 65 20 64 61 rollback, the da
126e6 74 61 62 61 73 65 20 66 69 6c 65 20 73 68 6f 75 tabase file shou
126e7 6c 64 20 62 65 20 62 61 63 6b 20 69 6e 20 69 74 ld be back in it
126e8 73 20 6f 72 69 67 69 6e 61 6c 0a 20 20 2a 2a 20 s original. **
126e9 73 74 61 74 65 20 70 72 69 6f 72 20 74 6f 20 74 state prior to t
126ea 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 he start of the
126eb 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 73 6f 20 transaction, so
126ec 69 6e 76 6f 6b 65 20 74 68 65 0a 20 20 2a 2a 20 invoke the. **
126ed 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 44 42 5f SQLITE_FCNTL_DB_
126ee 55 4e 43 48 41 4e 47 45 44 20 66 69 6c 65 2d 63 UNCHANGED file-c
126ef 6f 6e 74 72 6f 6c 20 6d 65 74 68 6f 64 20 74 6f ontrol method to
126f0 20 64 69 73 61 62 6c 65 20 74 68 65 0a 20 20 2a disable the. *
126f1 2a 20 61 73 73 65 72 74 69 6f 6e 20 74 68 61 74 * assertion that
126f2 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e the transaction
126f3 20 63 6f 75 6e 74 65 72 20 77 61 73 20 6d 6f 64 counter was mod
126f4 69 66 69 65 64 2e 0a 20 20 2a 2f 0a 20 20 61 73 ified.. */. as
126f5 73 65 72 74 28 0a 20 20 20 20 70 50 61 67 65 72 sert(. pPager
126f6 2d 3e 66 64 2d 3e 70 4d 65 74 68 6f 64 73 3d 3d ->fd->pMethods==
126f7 30 20 7c 7c 0a 20 20 20 20 73 71 6c 69 74 65 33 0 ||. sqlite3
126f8 4f 73 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 70 50 OsFileControl(pP
126f9 61 67 65 72 2d 3e 66 64 2c 53 51 4c 49 54 45 5f ager->fd,SQLITE_
126fa 46 43 4e 54 4c 5f 44 42 5f 55 4e 43 48 41 4e 47 FCNTL_DB_UNCHANG
126fb 45 44 2c 30 29 3e 3d 53 51 4c 49 54 45 5f 4f 4b ED,0)>=SQLITE_OK
126fc 0a 20 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 . );.. /* If t
126fd 68 69 73 20 70 6c 61 79 62 61 63 6b 20 69 73 20 his playback is
126fe 68 61 70 70 65 6e 69 6e 67 20 61 75 74 6f 6d 61 happening automa
126ff 74 69 63 61 6c 6c 79 20 61 73 20 61 20 72 65 73 tically as a res
12700 75 6c 74 20 6f 66 20 61 6e 20 49 4f 20 6f 72 20 ult of an IO or
12701 0a 20 20 2a 2a 20 6d 61 6c 6c 6f 63 20 65 72 72 . ** malloc err
12702 6f 72 20 74 68 61 74 20 6f 63 63 75 72 72 65 64 or that occurred
12703 20 61 66 74 65 72 20 74 68 65 20 63 68 61 6e 67 after the chang
12704 65 2d 63 6f 75 6e 74 65 72 20 77 61 73 20 75 70 e-counter was up
12705 64 61 74 65 64 20 62 75 74 20 0a 20 20 2a 2a 20 dated but . **
12706 62 65 66 6f 72 65 20 74 68 65 20 74 72 61 6e 73 before the trans
12707 61 63 74 69 6f 6e 20 77 61 73 20 63 6f 6d 6d 69 action was commi
12708 74 74 65 64 2c 20 74 68 65 6e 20 74 68 65 20 63 tted, then the c
12709 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20 0a 20 hange-counter .
1270a 20 2a 2a 20 6d 6f 64 69 66 69 63 61 74 69 6f 6e ** modification
1270b 20 6d 61 79 20 6a 75 73 74 20 68 61 76 65 20 62 may just have b
1270c 65 65 6e 20 72 65 76 65 72 74 65 64 2e 20 49 66 een reverted. If
1270d 20 74 68 69 73 20 68 61 70 70 65 6e 73 20 69 6e this happens in
1270e 20 65 78 63 6c 75 73 69 76 65 20 0a 20 20 2a 2a exclusive . **
1270f 20 6d 6f 64 65 2c 20 74 68 65 6e 20 73 75 62 73 mode, then subs
12710 65 71 75 65 6e 74 20 74 72 61 6e 73 61 63 74 69 equent transacti
12711 6f 6e 73 20 70 65 72 66 6f 72 6d 65 64 20 62 79 ons performed by
12712 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 the connection
12713 77 69 6c 6c 20 6e 6f 74 0a 20 20 2a 2a 20 75 70 will not. ** up
12714 64 61 74 65 20 74 68 65 20 63 68 61 6e 67 65 2d date the change-
12715 63 6f 75 6e 74 65 72 20 61 74 20 61 6c 6c 2e 20 counter at all.
12716 54 68 69 73 20 6d 61 79 20 6c 65 61 64 20 74 6f This may lead to
12717 20 63 61 63 68 65 20 69 6e 63 6f 6e 73 69 73 74 cache inconsist
12718 65 6e 63 79 0a 20 20 2a 2a 20 70 72 6f 62 6c 65 ency. ** proble
12719 6d 73 20 66 6f 72 20 6f 74 68 65 72 20 70 72 6f ms for other pro
1271a 63 65 73 73 65 73 20 61 74 20 73 6f 6d 65 20 70 cesses at some p
1271b 6f 69 6e 74 20 69 6e 20 74 68 65 20 66 75 74 75 oint in the futu
1271c 72 65 2e 20 53 6f 2c 20 6a 75 73 74 0a 20 20 2a re. So, just. *
1271d 2a 20 69 6e 20 63 61 73 65 20 74 68 69 73 20 68 * in case this h
1271e 61 73 20 68 61 70 70 65 6e 65 64 2c 20 63 6c 65 as happened, cle
1271f 61 72 20 74 68 65 20 63 68 61 6e 67 65 43 6f 75 ar the changeCou
12720 6e 74 44 6f 6e 65 20 66 6c 61 67 20 6e 6f 77 2e ntDone flag now.
12721 0a 20 20 2a 2f 0a 20 20 70 50 61 67 65 72 2d 3e . */. pPager->
12722 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 changeCountDone
12723 3d 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 = pPager->tempFi
12724 6c 65 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 le;.. if( rc==S
12725 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
12726 7a 4d 61 73 74 65 72 20 3d 20 70 50 61 67 65 72 zMaster = pPager
12727 2d 3e 70 54 6d 70 53 70 61 63 65 3b 0a 20 20 20 ->pTmpSpace;.
12728 20 72 63 20 3d 20 72 65 61 64 4d 61 73 74 65 72 rc = readMaster
12729 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 2d 3e Journal(pPager->
1272a 6a 66 64 2c 20 7a 4d 61 73 74 65 72 2c 20 70 50 jfd, zMaster, pP
1272b 61 67 65 72 2d 3e 70 56 66 73 2d 3e 6d 78 50 61 ager->pVfs->mxPa
1272c 74 68 6e 61 6d 65 2b 31 29 3b 0a 20 20 20 20 74 thname+1);. t
1272d 65 73 74 63 61 73 65 28 20 72 63 21 3d 53 51 4c estcase( rc!=SQL
1272e 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 7d 0a 20 20 ITE_OK );. }.
1272f 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
12730 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 61 K ){. rc = pa
12731 67 65 72 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 ger_end_transact
12732 69 6f 6e 28 70 50 61 67 65 72 2c 20 7a 4d 61 73 ion(pPager, zMas
12733 74 65 72 5b 30 5d 21 3d 27 5c 30 27 29 3b 0a 20 ter[0]!='\0');.
12734 20 20 20 74 65 73 74 63 61 73 65 28 20 72 63 21 testcase( rc!
12735 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 =SQLITE_OK );.
12736 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 }. if( rc==SQLI
12737 54 45 5f 4f 4b 20 26 26 20 7a 4d 61 73 74 65 72 TE_OK && zMaster
12738 5b 30 5d 20 26 26 20 72 65 73 20 29 7b 0a 20 20 [0] && res ){.
12739 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 77 61 /* If there wa
1273a 73 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e s a master journ
1273b 61 6c 20 61 6e 64 20 74 68 69 73 20 72 6f 75 74 al and this rout
1273c 69 6e 65 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 ine will return
1273d 73 75 63 63 65 73 73 2c 0a 20 20 20 20 2a 2a 20 success,. **
1273e 73 65 65 20 69 66 20 69 74 20 69 73 20 70 6f 73 see if it is pos
1273f 73 69 62 6c 65 20 74 6f 20 64 65 6c 65 74 65 20 sible to delete
12740 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e the master journ
12741 61 6c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 al.. */. r
12742 63 20 3d 20 70 61 67 65 72 5f 64 65 6c 6d 61 73 c = pager_delmas
12743 74 65 72 28 70 50 61 67 65 72 2c 20 7a 4d 61 73 ter(pPager, zMas
12744 74 65 72 29 3b 0a 20 20 20 20 74 65 73 74 63 61 ter);. testca
12745 73 65 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f se( rc!=SQLITE_O
12746 4b 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 K );. }.. /* T
12747 68 65 20 50 61 67 65 72 2e 73 65 63 74 6f 72 53 he Pager.sectorS
12748 69 7a 65 20 76 61 72 69 61 62 6c 65 20 6d 61 79 ize variable may
12749 20 68 61 76 65 20 62 65 65 6e 20 75 70 64 61 74 have been updat
1274a 65 64 20 77 68 69 6c 65 20 72 6f 6c 6c 69 6e 67 ed while rolling
1274b 0a 20 20 2a 2a 20 62 61 63 6b 20 61 20 6a 6f 75 . ** back a jou
1274c 72 6e 61 6c 20 63 72 65 61 74 65 64 20 62 79 20 rnal created by
1274d 61 20 70 72 6f 63 65 73 73 20 77 69 74 68 20 61 a process with a
1274e 20 64 69 66 66 65 72 65 6e 74 20 73 65 63 74 6f different secto
1274f 72 20 73 69 7a 65 0a 20 20 2a 2a 20 76 61 6c 75 r size. ** valu
12750 65 2e 20 52 65 73 65 74 20 69 74 20 74 6f 20 74 e. Reset it to t
12751 68 65 20 63 6f 72 72 65 63 74 20 76 61 6c 75 65 he correct value
12752 20 66 6f 72 20 74 68 69 73 20 70 72 6f 63 65 73 for this proces
12753 73 2e 0a 20 20 2a 2f 0a 20 20 73 65 74 53 65 63 s.. */. setSec
12754 74 6f 72 53 69 7a 65 28 70 50 61 67 65 72 29 3b torSize(pPager);
12755 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
12756 0a 2f 2a 0a 2a 2a 20 50 6c 61 79 62 61 63 6b 20 ./*.** Playback
12757 73 61 76 65 70 6f 69 6e 74 20 70 53 61 76 65 70 savepoint pSavep
12758 6f 69 6e 74 2e 20 4f 72 2c 20 69 66 20 70 53 61 oint. Or, if pSa
12759 76 65 70 6f 69 6e 74 3d 3d 4e 55 4c 4c 2c 20 74 vepoint==NULL, t
1275a 68 65 6e 20 70 6c 61 79 62 61 63 6b 0a 2a 2a 20 hen playback.**
1275b 74 68 65 20 65 6e 74 69 72 65 20 6d 61 73 74 65 the entire maste
1275c 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 r journal file.
1275d 54 68 65 20 63 61 73 65 20 70 53 61 76 65 70 6f The case pSavepo
1275e 69 6e 74 3d 3d 4e 55 4c 4c 20 6f 63 63 75 72 73 int==NULL occurs
1275f 20 77 68 65 6e 20 0a 2a 2a 20 61 20 52 4f 4c 4c when .** a ROLL
12760 42 41 43 4b 20 54 4f 20 63 6f 6d 6d 61 6e 64 20 BACK TO command
12761 69 73 20 69 6e 76 6f 6b 65 64 20 6f 6e 20 61 20 is invoked on a
12762 53 41 56 45 50 4f 49 4e 54 20 74 68 61 74 20 69 SAVEPOINT that i
12763 73 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 s a transaction
12764 0a 2a 2a 20 73 61 76 65 70 6f 69 6e 74 2e 0a 2a .** savepoint..*
12765 2a 0a 2a 2a 20 57 68 65 6e 20 70 53 61 76 65 70 *.** When pSavep
12766 6f 69 6e 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c oint is not NULL
12767 20 28 6d 65 61 6e 69 6e 67 20 61 20 6e 6f 6e 2d (meaning a non-
12768 74 72 61 6e 73 61 63 74 69 6f 6e 20 73 61 76 65 transaction save
12769 70 6f 69 6e 74 20 69 73 20 0a 2a 2a 20 62 65 69 point is .** bei
1276a 6e 67 20 72 6f 6c 6c 65 64 20 62 61 63 6b 29 2c ng rolled back),
1276b 20 74 68 65 6e 20 74 68 65 20 72 6f 6c 6c 62 61 then the rollba
1276c 63 6b 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 75 ck consists of u
1276d 70 20 74 6f 20 74 68 72 65 65 20 73 74 61 67 65 p to three stage
1276e 73 2c 0a 2a 2a 20 70 65 72 66 6f 72 6d 65 64 20 s,.** performed
1276f 69 6e 20 74 68 65 20 6f 72 64 65 72 20 73 70 65 in the order spe
12770 63 69 66 69 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 cified:.**.**
12771 2a 20 50 61 67 65 73 20 61 72 65 20 70 6c 61 79 * Pages are play
12772 65 64 20 62 61 63 6b 20 66 72 6f 6d 20 74 68 65 ed back from the
12773 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 73 74 main journal st
12774 61 72 74 69 6e 67 20 61 74 20 62 79 74 65 0a 2a arting at byte.*
12775 2a 20 20 20 20 20 6f 66 66 73 65 74 20 50 61 67 * offset Pag
12776 65 72 53 61 76 65 70 6f 69 6e 74 2e 69 4f 66 66 erSavepoint.iOff
12777 73 65 74 20 61 6e 64 20 63 6f 6e 74 69 6e 75 69 set and continui
12778 6e 67 20 74 6f 20 0a 2a 2a 20 20 20 20 20 50 61 ng to .** Pa
12779 67 65 72 53 61 76 65 70 6f 69 6e 74 2e 69 48 64 gerSavepoint.iHd
1277a 72 4f 66 66 73 65 74 2c 20 6f 72 20 74 6f 20 74 rOffset, or to t
1277b 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6d 61 he end of the ma
1277c 69 6e 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 20 20 in journal.**
1277d 20 20 66 69 6c 65 20 69 66 20 50 61 67 65 72 53 file if PagerS
1277e 61 76 65 70 6f 69 6e 74 2e 69 48 64 72 4f 66 66 avepoint.iHdrOff
1277f 73 65 74 20 69 73 20 7a 65 72 6f 2e 0a 2a 2a 0a set is zero..**.
12780 2a 2a 20 20 20 2a 20 49 66 20 50 61 67 65 72 53 ** * If PagerS
12781 61 76 65 70 6f 69 6e 74 2e 69 48 64 72 4f 66 66 avepoint.iHdrOff
12782 73 65 74 20 69 73 20 6e 6f 74 20 7a 65 72 6f 2c set is not zero,
12783 20 74 68 65 6e 20 70 61 67 65 73 20 61 72 65 20 then pages are
12784 70 6c 61 79 65 64 0a 2a 2a 20 20 20 20 20 62 61 played.** ba
12785 63 6b 20 73 74 61 72 74 69 6e 67 20 66 72 6f 6d ck starting from
12786 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 the journal hea
12787 64 65 72 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 der immediately
12788 66 6f 6c 6c 6f 77 69 6e 67 20 0a 2a 2a 20 20 20 following .**
12789 20 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 PagerSavepoint
1278a 2e 69 48 64 72 4f 66 66 73 65 74 20 74 6f 20 74 .iHdrOffset to t
1278b 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6d 61 he end of the ma
1278c 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e in journal file.
1278d 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 50 61 67 65 73 .**.** * Pages
1278e 20 61 72 65 20 74 68 65 6e 20 70 6c 61 79 65 64 are then played
1278f 20 62 61 63 6b 20 66 72 6f 6d 20 74 68 65 20 73 back from the s
12790 75 62 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c ub-journal file,
12791 20 73 74 61 72 74 69 6e 67 0a 2a 2a 20 20 20 20 starting.**
12792 20 77 69 74 68 20 74 68 65 20 50 61 67 65 72 53 with the PagerS
12793 61 76 65 70 6f 69 6e 74 2e 69 53 75 62 52 65 63 avepoint.iSubRec
12794 20 61 6e 64 20 63 6f 6e 74 69 6e 75 69 6e 67 20 and continuing
12795 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 0a 2a 2a to the end of.**
12796 20 20 20 20 20 74 68 65 20 6a 6f 75 72 6e 61 6c the journal
12797 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 72 file..**.** Thr
12798 6f 75 67 68 6f 75 74 20 74 68 65 20 72 6f 6c 6c oughout the roll
12799 62 61 63 6b 20 70 72 6f 63 65 73 73 2c 20 65 61 back process, ea
1279a 63 68 20 74 69 6d 65 20 61 20 70 61 67 65 20 69 ch time a page i
1279b 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2c 20 74 s rolled back, t
1279c 68 65 0a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 he.** correspond
1279d 69 6e 67 20 62 69 74 20 69 73 20 73 65 74 20 69 ing bit is set i
1279e 6e 20 61 20 62 69 74 76 65 63 20 73 74 72 75 63 n a bitvec struc
1279f 74 75 72 65 20 28 76 61 72 69 61 62 6c 65 20 70 ture (variable p
127a0 44 6f 6e 65 20 69 6e 20 74 68 65 0a 2a 2a 20 69 Done in the.** i
127a1 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 62 65 mplementation be
127a2 6c 6f 77 29 2e 20 54 68 69 73 20 69 73 20 75 73 low). This is us
127a3 65 64 20 74 6f 20 65 6e 73 75 72 65 20 74 68 61 ed to ensure tha
127a4 74 20 61 20 70 61 67 65 20 69 73 20 6f 6e 6c 79 t a page is only
127a5 0a 2a 2a 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 .** rolled back
127a6 74 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 69 the first time i
127a7 74 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 t is encountered
127a8 20 69 6e 20 65 69 74 68 65 72 20 6a 6f 75 72 6e in either journ
127a9 61 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 53 61 al..**.** If pSa
127aa 76 65 70 6f 69 6e 74 20 69 73 20 4e 55 4c 4c 2c vepoint is NULL,
127ab 20 74 68 65 6e 20 70 61 67 65 73 20 61 72 65 20 then pages are
127ac 6f 6e 6c 79 20 70 6c 61 79 65 64 20 62 61 63 6b only played back
127ad 20 66 72 6f 6d 20 74 68 65 20 6d 61 69 6e 0a 2a from the main.*
127ae 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 * journal file.
127af 54 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 There is no need
127b0 20 66 6f 72 20 61 20 62 69 74 76 65 63 20 69 6e for a bitvec in
127b1 20 74 68 69 73 20 63 61 73 65 2e 0a 2a 2a 0a 2a this case..**.*
127b2 2a 20 49 6e 20 65 69 74 68 65 72 20 63 61 73 65 * In either case
127b3 2c 20 62 65 66 6f 72 65 20 70 6c 61 79 62 61 63 , before playbac
127b4 6b 20 63 6f 6d 6d 65 6e 63 65 73 20 74 68 65 20 k commences the
127b5 50 61 67 65 72 2e 64 62 53 69 7a 65 20 76 61 72 Pager.dbSize var
127b6 69 61 62 6c 65 0a 2a 2a 20 69 73 20 72 65 73 65 iable.** is rese
127b7 74 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 74 t to the value t
127b8 68 61 74 20 69 74 20 68 65 6c 64 20 61 74 20 74 hat it held at t
127b9 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 he start of the
127ba 73 61 76 65 70 6f 69 6e 74 20 0a 2a 2a 20 28 6f savepoint .** (o
127bb 72 20 74 72 61 6e 73 61 63 74 69 6f 6e 29 2e 20 r transaction).
127bc 4e 6f 20 70 61 67 65 20 77 69 74 68 20 61 20 70 No page with a p
127bd 61 67 65 2d 6e 75 6d 62 65 72 20 67 72 65 61 74 age-number great
127be 65 72 20 74 68 61 6e 20 74 68 69 73 20 76 61 6c er than this val
127bf 75 65 0a 2a 2a 20 69 73 20 70 6c 61 79 65 64 20 ue.** is played
127c0 62 61 63 6b 2e 20 49 66 20 6f 6e 65 20 69 73 20 back. If one is
127c1 65 6e 63 6f 75 6e 74 65 72 65 64 20 69 74 20 69 encountered it i
127c2 73 20 73 69 6d 70 6c 79 20 73 6b 69 70 70 65 64 s simply skipped
127c3 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
127c4 70 61 67 65 72 50 6c 61 79 62 61 63 6b 53 61 76 pagerPlaybackSav
127c5 65 70 6f 69 6e 74 28 50 61 67 65 72 20 2a 70 50 epoint(Pager *pP
127c6 61 67 65 72 2c 20 50 61 67 65 72 53 61 76 65 70 ager, PagerSavep
127c7 6f 69 6e 74 20 2a 70 53 61 76 65 70 6f 69 6e 74 oint *pSavepoint
127c8 29 7b 0a 20 20 69 36 34 20 73 7a 4a 3b 20 20 20 ){. i64 szJ;
127c9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
127ca 20 45 66 66 65 63 74 69 76 65 20 73 69 7a 65 20 Effective size
127cb 6f 66 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 of the main jour
127cc 6e 61 6c 20 2a 2f 0a 20 20 69 36 34 20 69 48 64 nal */. i64 iHd
127cd 72 4f 66 66 3b 20 20 20 20 20 20 20 20 20 20 20 rOff;
127ce 20 20 2f 2a 20 45 6e 64 20 6f 66 20 66 69 72 73 /* End of firs
127cf 74 20 73 65 67 6d 65 6e 74 20 6f 66 20 6d 61 69 t segment of mai
127d0 6e 2d 6a 6f 75 72 6e 61 6c 20 72 65 63 6f 72 64 n-journal record
127d1 73 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 s */. int rc =
127d2 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 SQLITE_OK;
127d3 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a /* Return code *
127d4 2f 0a 20 20 42 69 74 76 65 63 20 2a 70 44 6f 6e /. Bitvec *pDon
127d5 65 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 e = 0; /*
127d6 42 69 74 76 65 63 20 74 6f 20 65 6e 73 75 72 65 Bitvec to ensure
127d7 20 70 61 67 65 73 20 70 6c 61 79 65 64 20 62 61 pages played ba
127d8 63 6b 20 6f 6e 6c 79 20 6f 6e 63 65 20 2a 2f 0a ck only once */.
127d9 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
127da 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f r->state>=PAGER_
127db 53 48 41 52 45 44 20 29 3b 0a 0a 20 20 2f 2a 20 SHARED );.. /*
127dc 41 6c 6c 6f 63 61 74 65 20 61 20 62 69 74 76 65 Allocate a bitve
127dd 63 20 74 6f 20 75 73 65 20 74 6f 20 73 74 6f 72 c to use to stor
127de 65 20 74 68 65 20 73 65 74 20 6f 66 20 70 61 67 e the set of pag
127df 65 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 2a es rolled back *
127e0 2f 0a 20 20 69 66 28 20 70 53 61 76 65 70 6f 69 /. if( pSavepoi
127e1 6e 74 20 29 7b 0a 20 20 20 20 70 44 6f 6e 65 20 nt ){. pDone
127e2 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 43 = sqlite3BitvecC
127e3 72 65 61 74 65 28 70 53 61 76 65 70 6f 69 6e 74 reate(pSavepoint
127e4 2d 3e 6e 4f 72 69 67 29 3b 0a 20 20 20 20 69 66 ->nOrig);. if
127e5 28 20 21 70 44 6f 6e 65 20 29 7b 0a 20 20 20 20 ( !pDone ){.
127e6 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
127e7 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 7d NOMEM;. }. }
127e8 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 20 64 .. /* Set the d
127e9 61 74 61 62 61 73 65 20 73 69 7a 65 20 62 61 63 atabase size bac
127ea 6b 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 69 k to the value i
127eb 74 20 77 61 73 20 62 65 66 6f 72 65 20 74 68 65 t was before the
127ec 20 73 61 76 65 70 6f 69 6e 74 20 0a 20 20 2a 2a savepoint . **
127ed 20 62 65 69 6e 67 20 72 65 76 65 72 74 65 64 20 being reverted
127ee 77 61 73 20 6f 70 65 6e 65 64 2e 0a 20 20 2a 2f was opened.. */
127ef 0a 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a . pPager->dbSiz
127f0 65 20 3d 20 70 53 61 76 65 70 6f 69 6e 74 20 3f e = pSavepoint ?
127f1 20 70 53 61 76 65 70 6f 69 6e 74 2d 3e 6e 4f 72 pSavepoint->nOr
127f2 69 67 20 3a 20 70 50 61 67 65 72 2d 3e 64 62 4f ig : pPager->dbO
127f3 72 69 67 53 69 7a 65 3b 0a 0a 20 20 2f 2a 20 55 rigSize;.. /* U
127f4 73 65 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e se pPager->journ
127f5 61 6c 4f 66 66 20 61 73 20 74 68 65 20 65 66 66 alOff as the eff
127f6 65 63 74 69 76 65 20 73 69 7a 65 20 6f 66 20 74 ective size of t
127f7 68 65 20 6d 61 69 6e 20 72 6f 6c 6c 62 61 63 6b he main rollback
127f8 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 2e 20 20 . ** journal.
127f9 54 68 65 20 61 63 74 75 61 6c 20 66 69 6c 65 20 The actual file
127fa 6d 69 67 68 74 20 62 65 20 6c 61 72 67 65 72 20 might be larger
127fb 74 68 61 6e 20 74 68 69 73 20 69 6e 0a 20 20 2a than this in. *
127fc 2a 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d * PAGER_JOURNALM
127fd 4f 44 45 5f 54 52 55 4e 43 41 54 45 20 6f 72 20 ODE_TRUNCATE or
127fe 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 PAGER_JOURNALMOD
127ff 45 5f 50 45 52 53 49 53 54 2e 20 20 42 75 74 20 E_PERSIST. But
12800 61 6e 79 74 68 69 6e 67 0a 20 20 2a 2a 20 70 61 anything. ** pa
12801 73 74 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e st pPager->journ
12802 61 6c 4f 66 66 20 69 73 20 6f 66 66 2d 6c 69 6d alOff is off-lim
12803 69 74 73 20 74 6f 20 75 73 2e 0a 20 20 2a 2f 0a its to us.. */.
12804 20 20 73 7a 4a 20 3d 20 70 50 61 67 65 72 2d 3e szJ = pPager->
12805 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a 0a 20 20 2f journalOff;.. /
12806 2a 20 42 65 67 69 6e 20 62 79 20 72 6f 6c 6c 69 * Begin by rolli
12807 6e 67 20 62 61 63 6b 20 72 65 63 6f 72 64 73 20 ng back records
12808 66 72 6f 6d 20 74 68 65 20 6d 61 69 6e 20 6a 6f from the main jo
12809 75 72 6e 61 6c 20 73 74 61 72 74 69 6e 67 20 61 urnal starting a
1280a 74 0a 20 20 2a 2a 20 50 61 67 65 72 53 61 76 65 t. ** PagerSave
1280b 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 20 61 6e point.iOffset an
1280c 64 20 63 6f 6e 74 69 6e 75 69 6e 67 20 74 6f 20 d continuing to
1280d 74 68 65 20 6e 65 78 74 20 6a 6f 75 72 6e 61 6c the next journal
1280e 20 68 65 61 64 65 72 2e 0a 20 20 2a 2a 20 54 68 header.. ** Th
1280f 65 72 65 20 6d 69 67 68 74 20 62 65 20 72 65 63 ere might be rec
12810 6f 72 64 73 20 69 6e 20 74 68 65 20 6d 61 69 6e ords in the main
12811 20 6a 6f 75 72 6e 61 6c 20 74 68 61 74 20 68 61 journal that ha
12812 76 65 20 61 20 70 61 67 65 20 6e 75 6d 62 65 72 ve a page number
12813 0a 20 20 2a 2a 20 67 72 65 61 74 65 72 20 74 68 . ** greater th
12814 61 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 64 an the current d
12815 61 74 61 62 61 73 65 20 73 69 7a 65 20 28 70 50 atabase size (pP
12816 61 67 65 72 2d 3e 64 62 53 69 7a 65 29 20 62 75 ager->dbSize) bu
12817 74 20 74 68 6f 73 65 0a 20 20 2a 2a 20 77 69 6c t those. ** wil
12818 6c 20 62 65 20 73 6b 69 70 70 65 64 20 61 75 74 l be skipped aut
12819 6f 6d 61 74 69 63 61 6c 6c 79 2e 20 20 50 61 67 omatically. Pag
1281a 65 73 20 61 72 65 20 61 64 64 65 64 20 74 6f 20 es are added to
1281b 70 44 6f 6e 65 20 61 73 20 74 68 65 79 0a 20 20 pDone as they.
1281c 2a 2a 20 61 72 65 20 70 6c 61 79 65 64 20 62 61 ** are played ba
1281d 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 ck.. */. if( p
1281e 53 61 76 65 70 6f 69 6e 74 20 29 7b 0a 20 20 20 Savepoint ){.
1281f 20 69 48 64 72 4f 66 66 20 3d 20 70 53 61 76 65 iHdrOff = pSave
12820 70 6f 69 6e 74 2d 3e 69 48 64 72 4f 66 66 73 65 point->iHdrOffse
12821 74 20 3f 20 70 53 61 76 65 70 6f 69 6e 74 2d 3e t ? pSavepoint->
12822 69 48 64 72 4f 66 66 73 65 74 20 3a 20 73 7a 4a iHdrOffset : szJ
12823 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f ;. pPager->jo
12824 75 72 6e 61 6c 4f 66 66 20 3d 20 70 53 61 76 65 urnalOff = pSave
12825 70 6f 69 6e 74 2d 3e 69 4f 66 66 73 65 74 3b 0a point->iOffset;.
12826 20 20 20 20 77 68 69 6c 65 28 20 72 63 3d 3d 53 while( rc==S
12827 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 50 61 67 QLITE_OK && pPag
12828 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3c 69 er->journalOff<i
12829 48 64 72 4f 66 66 20 29 7b 0a 20 20 20 20 20 20 HdrOff ){.
1282a 72 63 20 3d 20 70 61 67 65 72 5f 70 6c 61 79 62 rc = pager_playb
1282b 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 70 50 61 ack_one_page(pPa
1282c 67 65 72 2c 20 31 2c 20 30 2c 20 26 70 50 61 67 ger, 1, 0, &pPag
1282d 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 er->journalOff,
1282e 31 2c 20 70 44 6f 6e 65 29 3b 0a 20 20 20 20 7d 1, pDone);. }
1282f 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 63 21 . assert( rc!
12830 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 3b 0a =SQLITE_DONE );.
12831 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 50 61 }else{. pPa
12832 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 ger->journalOff
12833 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 = 0;. }.. /* C
12834 6f 6e 74 69 6e 75 65 20 72 6f 6c 6c 69 6e 67 20 ontinue rolling
12835 62 61 63 6b 20 72 65 63 6f 72 64 73 20 6f 75 74 back records out
12836 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 of the main jou
12837 72 6e 61 6c 20 73 74 61 72 74 69 6e 67 20 61 74 rnal starting at
12838 0a 20 20 2a 2a 20 74 68 65 20 66 69 72 73 74 20 . ** the first
12839 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 73 journal header s
1283a 65 65 6e 20 61 6e 64 20 63 6f 6e 74 69 6e 75 69 een and continui
1283b 6e 67 20 75 6e 74 69 6c 20 74 68 65 20 65 66 66 ng until the eff
1283c 65 63 74 69 76 65 20 65 6e 64 0a 20 20 2a 2a 20 ective end. **
1283d 6f 66 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 of the main jour
1283e 6e 61 6c 20 66 69 6c 65 2e 20 20 43 6f 6e 74 69 nal file. Conti
1283f 6e 75 65 20 74 6f 20 73 6b 69 70 20 6f 75 74 2d nue to skip out-
12840 6f 66 2d 72 61 6e 67 65 20 70 61 67 65 73 20 61 of-range pages a
12841 6e 64 0a 20 20 2a 2a 20 63 6f 6e 74 69 6e 75 65 nd. ** continue
12842 20 61 64 64 69 6e 67 20 70 61 67 65 73 20 72 6f adding pages ro
12843 6c 6c 65 64 20 62 61 63 6b 20 74 6f 20 70 44 6f lled back to pDo
12844 6e 65 2e 0a 20 20 2a 2f 0a 20 20 77 68 69 6c 65 ne.. */. while
12845 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
12846 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e && pPager->journ
12847 61 6c 4f 66 66 3c 73 7a 4a 20 29 7b 0a 20 20 20 alOff<szJ ){.
12848 20 75 33 32 20 69 69 3b 20 20 20 20 20 20 20 20 u32 ii;
12849 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e /* Loop coun
1284a 74 65 72 20 2a 2f 0a 20 20 20 20 75 33 32 20 6e ter */. u32 n
1284b 4a 52 65 63 20 3d 20 30 3b 20 20 20 20 20 2f 2a JRec = 0; /*
1284c 20 4e 75 6d 62 65 72 20 6f 66 20 4a 6f 75 72 6e Number of Journ
1284d 61 6c 20 52 65 63 6f 72 64 73 20 2a 2f 0a 20 20 al Records */.
1284e 20 20 75 33 32 20 64 75 6d 6d 79 3b 0a 20 20 20 u32 dummy;.
1284f 20 72 63 20 3d 20 72 65 61 64 4a 6f 75 72 6e 61 rc = readJourna
12850 6c 48 64 72 28 70 50 61 67 65 72 2c 20 30 2c 20 lHdr(pPager, 0,
12851 73 7a 4a 2c 20 26 6e 4a 52 65 63 2c 20 26 64 75 szJ, &nJRec, &du
12852 6d 6d 79 29 3b 0a 20 20 20 20 61 73 73 65 72 74 mmy);. assert
12853 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e ( rc!=SQLITE_DON
12854 45 20 29 3b 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 E );.. /*.
12855 20 2a 2a 20 54 68 65 20 22 70 50 61 67 65 72 2d ** The "pPager-
12856 3e 6a 6f 75 72 6e 61 6c 48 64 72 2b 4a 4f 55 52 >journalHdr+JOUR
12857 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 NAL_HDR_SZ(pPage
12858 72 29 3d 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 72 r)==pPager->jour
12859 6e 61 6c 4f 66 66 22 0a 20 20 20 20 2a 2a 20 74 nalOff". ** t
1285a 65 73 74 20 69 73 20 72 65 6c 61 74 65 64 20 74 est is related t
1285b 6f 20 74 69 63 6b 65 74 20 23 32 35 36 35 2e 20 o ticket #2565.
1285c 20 53 65 65 20 74 68 65 20 64 69 73 63 75 73 73 See the discuss
1285d 69 6f 6e 20 69 6e 20 74 68 65 0a 20 20 20 20 2a ion in the. *
1285e 2a 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b * pager_playback
1285f 28 29 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 () function for
12860 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 additional infor
12861 6d 61 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 mation.. */.
12862 20 20 20 69 66 28 20 6e 4a 52 65 63 3d 3d 30 20 if( nJRec==0
12863 0a 20 20 20 20 20 26 26 20 70 50 61 67 65 72 2d . && pPager-
12864 3e 6a 6f 75 72 6e 61 6c 48 64 72 2b 4a 4f 55 52 >journalHdr+JOUR
12865 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 NAL_HDR_SZ(pPage
12866 72 29 3d 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 72 r)==pPager->jour
12867 6e 61 6c 4f 66 66 0a 20 20 20 20 29 7b 0a 20 20 nalOff. ){.
12868 20 20 20 20 6e 4a 52 65 63 20 3d 20 28 75 33 32 nJRec = (u32
12869 29 28 28 73 7a 4a 20 2d 20 70 50 61 67 65 72 2d )((szJ - pPager-
1286a 3e 6a 6f 75 72 6e 61 6c 4f 66 66 29 2f 4a 4f 55 >journalOff)/JOU
1286b 52 4e 41 4c 5f 50 47 5f 53 5a 28 70 50 61 67 65 RNAL_PG_SZ(pPage
1286c 72 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 r));. }. f
1286d 6f 72 28 69 69 3d 30 3b 20 72 63 3d 3d 53 51 4c or(ii=0; rc==SQL
1286e 49 54 45 5f 4f 4b 20 26 26 20 69 69 3c 6e 4a 52 ITE_OK && ii<nJR
1286f 65 63 20 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f ec && pPager->jo
12870 75 72 6e 61 6c 4f 66 66 3c 73 7a 4a 3b 20 69 69 urnalOff<szJ; ii
12871 2b 2b 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 ++){. rc =
12872 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 5f 6f pager_playback_o
12873 6e 65 5f 70 61 67 65 28 70 50 61 67 65 72 2c 20 ne_page(pPager,
12874 31 2c 20 30 2c 20 26 70 50 61 67 65 72 2d 3e 6a 1, 0, &pPager->j
12875 6f 75 72 6e 61 6c 4f 66 66 2c 20 31 2c 20 70 44 ournalOff, 1, pD
12876 6f 6e 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 one);. }.
12877 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 assert( rc!=SQLI
12878 54 45 5f 44 4f 4e 45 20 29 3b 0a 20 20 7d 0a 20 TE_DONE );. }.
12879 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c assert( rc!=SQL
1287a 49 54 45 5f 4f 4b 20 7c 7c 20 70 50 61 67 65 72 ITE_OK || pPager
1287b 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d 73 7a ->journalOff==sz
1287c 4a 20 29 3b 0a 0a 20 20 2f 2a 20 46 69 6e 61 6c J );.. /* Final
1287d 6c 79 2c 20 20 72 6f 6c 6c 62 61 63 6b 20 70 61 ly, rollback pa
1287e 67 65 73 20 66 72 6f 6d 20 74 68 65 20 73 75 62 ges from the sub
1287f 2d 6a 6f 75 72 6e 61 6c 2e 20 20 50 61 67 65 20 -journal. Page
12880 74 68 61 74 20 77 65 72 65 0a 20 20 2a 2a 20 70 that were. ** p
12881 72 65 76 69 6f 75 73 6c 79 20 72 6f 6c 6c 65 64 reviously rolled
12882 20 62 61 63 6b 20 6f 75 74 20 6f 66 20 74 68 65 back out of the
12883 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 28 61 main journal (a
12884 6e 64 20 61 72 65 20 68 65 6e 63 65 20 69 6e 20 nd are hence in
12885 70 44 6f 6e 65 29 0a 20 20 2a 2a 20 77 69 6c 6c pDone). ** will
12886 20 62 65 20 73 6b 69 70 70 65 64 2e 20 20 4f 75 be skipped. Ou
12887 74 2d 6f 66 2d 72 61 6e 67 65 20 70 61 67 65 73 t-of-range pages
12888 20 61 72 65 20 61 6c 73 6f 20 73 6b 69 70 70 65 are also skippe
12889 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 53 d.. */. if( pS
1288a 61 76 65 70 6f 69 6e 74 20 29 7b 0a 20 20 20 20 avepoint ){.
1288b 75 33 32 20 69 69 3b 20 20 20 20 20 20 20 20 20 u32 ii;
1288c 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 /* Loop count
1288d 65 72 20 2a 2f 0a 20 20 20 20 69 36 34 20 6f 66 er */. i64 of
1288e 66 73 65 74 20 3d 20 70 53 61 76 65 70 6f 69 6e fset = pSavepoin
1288f 74 2d 3e 69 53 75 62 52 65 63 2a 28 34 2b 70 50 t->iSubRec*(4+pP
12890 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b ager->pageSize);
12891 0a 20 20 20 20 66 6f 72 28 69 69 3d 70 53 61 76 . for(ii=pSav
12892 65 70 6f 69 6e 74 2d 3e 69 53 75 62 52 65 63 3b epoint->iSubRec;
12893 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 rc==SQLITE_OK &
12894 26 20 69 69 3c 70 50 61 67 65 72 2d 3e 6e 53 75 & ii<pPager->nSu
12895 62 52 65 63 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 bRec; ii++){.
12896 20 20 20 61 73 73 65 72 74 28 20 6f 66 66 73 65 assert( offse
12897 74 3d 3d 69 69 2a 28 34 2b 70 50 61 67 65 72 2d t==ii*(4+pPager-
12898 3e 70 61 67 65 53 69 7a 65 29 20 29 3b 0a 20 20 >pageSize) );.
12899 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 70 rc = pager_p
1289a 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 layback_one_page
1289b 28 70 50 61 67 65 72 2c 20 30 2c 20 30 2c 20 26 (pPager, 0, 0, &
1289c 6f 66 66 73 65 74 2c 20 31 2c 20 70 44 6f 6e 65 offset, 1, pDone
1289d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 );. }. ass
1289e 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f ert( rc!=SQLITE_
1289f 44 4f 4e 45 20 29 3b 0a 20 20 7d 0a 0a 20 20 73 DONE );. }.. s
128a0 71 6c 69 74 65 33 42 69 74 76 65 63 44 65 73 74 qlite3BitvecDest
128a1 72 6f 79 28 70 44 6f 6e 65 29 3b 0a 20 20 69 66 roy(pDone);. if
128a2 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
128a3 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a ){. pPager->j
128a4 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 73 7a 4a 3b ournalOff = szJ;
128a5 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
128a6 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 ;.}../*.** Chang
128a7 65 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 e the maximum nu
128a8 6d 62 65 72 20 6f 66 20 69 6e 2d 6d 65 6d 6f 72 mber of in-memor
128a9 79 20 70 61 67 65 73 20 74 68 61 74 20 61 72 65 y pages that are
128aa 20 61 6c 6c 6f 77 65 64 2e 0a 2a 2f 0a 53 51 4c allowed..*/.SQL
128ab 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
128ac 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 sqlite3PagerSet
128ad 43 61 63 68 65 73 69 7a 65 28 50 61 67 65 72 20 Cachesize(Pager
128ae 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 6d 78 50 *pPager, int mxP
128af 61 67 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 50 age){. sqlite3P
128b0 63 61 63 68 65 53 65 74 43 61 63 68 65 73 69 7a cacheSetCachesiz
128b1 65 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 e(pPager->pPCach
128b2 65 2c 20 6d 78 50 61 67 65 29 3b 0a 7d 0a 0a 2f e, mxPage);.}../
128b3 2a 0a 2a 2a 20 41 64 6a 75 73 74 20 74 68 65 20 *.** Adjust the
128b4 72 6f 62 75 73 74 6e 65 73 73 20 6f 66 20 74 68 robustness of th
128b5 65 20 64 61 74 61 62 61 73 65 20 74 6f 20 64 61 e database to da
128b6 6d 61 67 65 20 64 75 65 20 74 6f 20 4f 53 20 63 mage due to OS c
128b7 72 61 73 68 65 73 0a 2a 2a 20 6f 72 20 70 6f 77 rashes.** or pow
128b8 65 72 20 66 61 69 6c 75 72 65 73 20 62 79 20 63 er failures by c
128b9 68 61 6e 67 69 6e 67 20 74 68 65 20 6e 75 6d 62 hanging the numb
128ba 65 72 20 6f 66 20 73 79 6e 63 73 28 29 73 20 77 er of syncs()s w
128bb 68 65 6e 20 77 72 69 74 69 6e 67 0a 2a 2a 20 74 hen writing.** t
128bc 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 he rollback jour
128bd 6e 61 6c 2e 20 20 54 68 65 72 65 20 61 72 65 20 nal. There are
128be 74 68 72 65 65 20 6c 65 76 65 6c 73 3a 0a 2a 2a three levels:.**
128bf 0a 2a 2a 20 20 20 20 4f 46 46 20 20 20 20 20 20 .** OFF
128c0 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 29 sqlite3OsSync()
128c1 20 69 73 20 6e 65 76 65 72 20 63 61 6c 6c 65 64 is never called
128c2 2e 20 20 54 68 69 73 20 69 73 20 74 68 65 20 64 . This is the d
128c3 65 66 61 75 6c 74 0a 2a 2a 20 20 20 20 20 20 20 efault.**
128c4 20 20 20 20 20 20 20 66 6f 72 20 74 65 6d 70 6f for tempo
128c5 72 61 72 79 20 61 6e 64 20 74 72 61 6e 73 69 65 rary and transie
128c6 6e 74 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 nt files..**.**
128c7 20 20 20 4e 4f 52 4d 41 4c 20 20 20 20 54 68 65 NORMAL The
128c8 20 6a 6f 75 72 6e 61 6c 20 69 73 20 73 79 6e 63 journal is sync
128c9 65 64 20 6f 6e 63 65 20 62 65 66 6f 72 65 20 77 ed once before w
128ca 72 69 74 65 73 20 62 65 67 69 6e 20 6f 6e 20 74 rites begin on t
128cb 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 he.**
128cc 20 20 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 database. Th
128cd 69 73 20 69 73 20 6e 6f 72 6d 61 6c 6c 79 20 61 is is normally a
128ce 64 65 71 75 61 74 65 20 70 72 6f 74 65 63 74 69 dequate protecti
128cf 6f 6e 2c 20 62 75 74 0a 2a 2a 20 20 20 20 20 20 on, but.**
128d0 20 20 20 20 20 20 20 20 69 74 20 69 73 20 74 68 it is th
128d1 65 6f 72 65 74 69 63 61 6c 6c 79 20 70 6f 73 73 eoretically poss
128d2 69 62 6c 65 2c 20 74 68 6f 75 67 68 20 76 65 72 ible, though ver
128d3 79 20 75 6e 6c 69 6b 65 6c 79 2c 0a 2a 2a 20 20 y unlikely,.**
128d4 20 20 20 20 20 20 20 20 20 20 20 20 74 68 61 74 that
128d5 20 61 6e 20 69 6e 6f 70 65 72 74 75 6e 65 20 70 an inopertune p
128d6 6f 77 65 72 20 66 61 69 6c 75 72 65 20 63 6f 75 ower failure cou
128d7 6c 64 20 6c 65 61 76 65 20 74 68 65 20 6a 6f 75 ld leave the jou
128d8 72 6e 61 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 rnal.**
128d9 20 20 20 20 20 69 6e 20 61 20 73 74 61 74 65 20 in a state
128da 77 68 69 63 68 20 77 6f 75 6c 64 20 63 61 75 73 which would caus
128db 65 20 64 61 6d 61 67 65 20 74 6f 20 74 68 65 20 e damage to the
128dc 64 61 74 61 62 61 73 65 0a 2a 2a 20 20 20 20 20 database.**
128dd 20 20 20 20 20 20 20 20 20 77 68 65 6e 20 69 74 when it
128de 20 69 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e is rolled back.
128df 0a 2a 2a 0a 2a 2a 20 20 20 20 46 55 4c 4c 20 20 .**.** FULL
128e0 20 20 20 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 The journal
128e1 69 73 20 73 79 6e 63 65 64 20 74 77 69 63 65 20 is synced twice
128e2 62 65 66 6f 72 65 20 77 72 69 74 65 73 20 62 65 before writes be
128e3 67 69 6e 20 6f 6e 20 74 68 65 0a 2a 2a 20 20 20 gin on the.**
128e4 20 20 20 20 20 20 20 20 20 20 20 64 61 74 61 62 datab
128e5 61 73 65 20 28 77 69 74 68 20 73 6f 6d 65 20 61 ase (with some a
128e6 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d dditional inform
128e7 61 74 69 6f 6e 20 2d 20 74 68 65 20 6e 52 65 63 ation - the nRec
128e8 20 66 69 65 6c 64 0a 2a 2a 20 20 20 20 20 20 20 field.**
128e9 20 20 20 20 20 20 20 6f 66 20 74 68 65 20 6a 6f of the jo
128ea 75 72 6e 61 6c 20 68 65 61 64 65 72 20 2d 20 62 urnal header - b
128eb 65 69 6e 67 20 77 72 69 74 74 65 6e 20 69 6e 20 eing written in
128ec 62 65 74 77 65 65 6e 20 74 68 65 20 74 77 6f 0a between the two.
128ed 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
128ee 73 79 6e 63 73 29 2e 20 20 49 66 20 77 65 20 61 syncs). If we a
128ef 73 73 75 6d 65 20 74 68 61 74 20 77 72 69 74 69 ssume that writi
128f0 6e 67 20 61 0a 2a 2a 20 20 20 20 20 20 20 20 20 ng a.**
128f1 20 20 20 20 20 73 69 6e 67 6c 65 20 64 69 73 6b single disk
128f2 20 73 65 63 74 6f 72 20 69 73 20 61 74 6f 6d 69 sector is atomi
128f3 63 2c 20 74 68 65 6e 20 74 68 69 73 20 6d 6f 64 c, then this mod
128f4 65 20 70 72 6f 76 69 64 65 73 0a 2a 2a 20 20 20 e provides.**
128f5 20 20 20 20 20 20 20 20 20 20 20 61 73 73 75 72 assur
128f6 61 6e 63 65 20 74 68 61 74 20 74 68 65 20 6a 6f ance that the jo
128f7 75 72 6e 61 6c 20 77 69 6c 6c 20 6e 6f 74 20 62 urnal will not b
128f8 65 20 63 6f 72 72 75 70 74 65 64 20 74 6f 20 74 e corrupted to t
128f9 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 he.**
128fa 20 20 20 70 6f 69 6e 74 20 6f 66 20 63 61 75 73 point of caus
128fb 69 6e 67 20 64 61 6d 61 67 65 20 74 6f 20 74 68 ing damage to th
128fc 65 20 64 61 74 61 62 61 73 65 20 64 75 72 69 6e e database durin
128fd 67 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a g rollback..**.*
128fe 2a 20 4e 75 6d 65 72 69 63 20 76 61 6c 75 65 73 * Numeric values
128ff 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
12900 20 74 68 65 73 65 20 73 74 61 74 65 73 20 61 72 these states ar
12901 65 20 4f 46 46 3d 3d 31 2c 20 4e 4f 52 4d 41 4c e OFF==1, NORMAL
12902 3d 32 2c 0a 2a 2a 20 61 6e 64 20 46 55 4c 4c 3d =2,.** and FULL=
12903 33 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 3..*/.#ifndef SQ
12904 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f LITE_OMIT_PAGER_
12905 50 52 41 47 4d 41 53 0a 53 51 4c 49 54 45 5f 50 PRAGMAS.SQLITE_P
12906 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
12907 74 65 33 50 61 67 65 72 53 65 74 53 61 66 65 74 te3PagerSetSafet
12908 79 4c 65 76 65 6c 28 50 61 67 65 72 20 2a 70 50 yLevel(Pager *pP
12909 61 67 65 72 2c 20 69 6e 74 20 6c 65 76 65 6c 2c ager, int level,
1290a 20 69 6e 74 20 62 46 75 6c 6c 46 73 79 6e 63 29 int bFullFsync)
1290b 7b 0a 20 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 {. pPager->noSy
1290c 6e 63 20 3d 20 20 28 6c 65 76 65 6c 3d 3d 31 20 nc = (level==1
1290d 7c 7c 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 || pPager->tempF
1290e 69 6c 65 29 20 3f 31 3a 30 3b 0a 20 20 70 50 61 ile) ?1:0;. pPa
1290f 67 65 72 2d 3e 66 75 6c 6c 53 79 6e 63 20 3d 20 ger->fullSync =
12910 28 6c 65 76 65 6c 3d 3d 33 20 26 26 20 21 70 50 (level==3 && !pP
12911 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 29 20 ager->tempFile)
12912 3f 31 3a 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e ?1:0;. pPager->
12913 73 79 6e 63 5f 66 6c 61 67 73 20 3d 20 28 62 46 sync_flags = (bF
12914 75 6c 6c 46 73 79 6e 63 3f 53 51 4c 49 54 45 5f ullFsync?SQLITE_
12915 53 59 4e 43 5f 46 55 4c 4c 3a 53 51 4c 49 54 45 SYNC_FULL:SQLITE
12916 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c 29 3b 0a 20 _SYNC_NORMAL);.
12917 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6e 6f 53 if( pPager->noS
12918 79 6e 63 20 29 20 70 50 61 67 65 72 2d 3e 6e 65 ync ) pPager->ne
12919 65 64 53 79 6e 63 20 3d 20 30 3b 0a 7d 0a 23 65 edSync = 0;.}.#e
1291a 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 ndif../*.** The
1291b 66 6f 6c 6c 6f 77 69 6e 67 20 67 6c 6f 62 61 6c following global
1291c 20 76 61 72 69 61 62 6c 65 20 69 73 20 69 6e 63 variable is inc
1291d 72 65 6d 65 6e 74 65 64 20 77 68 65 6e 65 76 65 remented wheneve
1291e 72 20 74 68 65 20 6c 69 62 72 61 72 79 0a 2a 2a r the library.**
1291f 20 61 74 74 65 6d 70 74 73 20 74 6f 20 6f 70 65 attempts to ope
12920 6e 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69 n a temporary fi
12921 6c 65 2e 20 20 54 68 69 73 20 69 6e 66 6f 72 6d le. This inform
12922 61 74 69 6f 6e 20 69 73 20 75 73 65 64 20 66 6f ation is used fo
12923 72 0a 2a 2a 20 74 65 73 74 69 6e 67 20 61 6e 64 r.** testing and
12924 20 61 6e 61 6c 79 73 69 73 20 6f 6e 6c 79 2e 20 analysis only.
12925 20 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 .*/.#ifdef SQLI
12926 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 TE_TEST.SQLITE_A
12927 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f PI int sqlite3_o
12928 70 65 6e 74 65 6d 70 5f 63 6f 75 6e 74 20 3d 20 pentemp_count =
12929 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 0;.#endif../*.**
1292a 20 4f 70 65 6e 20 61 20 74 65 6d 70 6f 72 61 72 Open a temporar
1292b 79 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 57 72 y file..**.** Wr
1292c 69 74 65 20 74 68 65 20 66 69 6c 65 20 64 65 73 ite the file des
1292d 63 72 69 70 74 6f 72 20 69 6e 74 6f 20 2a 70 46 criptor into *pF
1292e 69 6c 65 2e 20 52 65 74 75 72 6e 20 53 51 4c 49 ile. Return SQLI
1292f 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 TE_OK on success
12930 20 0a 2a 2a 20 6f 72 20 73 6f 6d 65 20 6f 74 68 .** or some oth
12931 65 72 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66 er error code if
12932 20 77 65 20 66 61 69 6c 2e 20 54 68 65 20 4f 53 we fail. The OS
12933 20 77 69 6c 6c 20 61 75 74 6f 6d 61 74 69 63 61 will automatica
12934 6c 6c 79 20 0a 2a 2a 20 64 65 6c 65 74 65 20 74 lly .** delete t
12935 68 65 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c he temporary fil
12936 65 20 77 68 65 6e 20 69 74 20 69 73 20 63 6c 6f e when it is clo
12937 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 sed..**.** The f
12938 6c 61 67 73 20 70 61 73 73 65 64 20 74 6f 20 74 lags passed to t
12939 68 65 20 56 46 53 20 6c 61 79 65 72 20 78 4f 70 he VFS layer xOp
1293a 65 6e 28 29 20 63 61 6c 6c 20 61 72 65 20 74 68 en() call are th
1293b 6f 73 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a ose specified.**
1293c 20 62 79 20 70 61 72 61 6d 65 74 65 72 20 76 66 by parameter vf
1293d 73 46 6c 61 67 73 20 4f 52 65 64 20 77 69 74 68 sFlags ORed with
1293e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a the following:.
1293f 2a 2a 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 **.** SQLITE
12940 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 0a _OPEN_READWRITE.
12941 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 ** SQLITE_OP
12942 45 4e 5f 43 52 45 41 54 45 0a 2a 2a 20 20 20 20 EN_CREATE.**
12943 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 SQLITE_OPEN_EXC
12944 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 20 53 51 LUSIVE.** SQ
12945 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 LITE_OPEN_DELETE
12946 4f 4e 43 4c 4f 53 45 0a 2a 2f 0a 73 74 61 74 69 ONCLOSE.*/.stati
12947 63 20 69 6e 74 20 70 61 67 65 72 4f 70 65 6e 74 c int pagerOpent
12948 65 6d 70 28 0a 20 20 50 61 67 65 72 20 2a 70 50 emp(. Pager *pP
12949 61 67 65 72 2c 20 20 20 20 20 20 20 20 2f 2a 20 ager, /*
1294a 54 68 65 20 70 61 67 65 72 20 6f 62 6a 65 63 74 The pager object
1294b 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 */. sqlite3_fi
1294c 6c 65 20 2a 70 46 69 6c 65 2c 20 20 2f 2a 20 57 le *pFile, /* W
1294d 72 69 74 65 20 74 68 65 20 66 69 6c 65 20 64 65 rite the file de
1294e 73 63 72 69 70 74 6f 72 20 68 65 72 65 20 2a 2f scriptor here */
1294f 0a 20 20 69 6e 74 20 76 66 73 46 6c 61 67 73 20 . int vfsFlags
12950 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 /* Flag
12951 73 20 70 61 73 73 65 64 20 74 68 72 6f 75 67 68 s passed through
12952 20 74 6f 20 74 68 65 20 56 46 53 20 2a 2f 0a 29 to the VFS */.)
12953 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 {. int rc;
12954 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 /* Ret
12955 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a 23 69 66 urn code */..#if
12956 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a def SQLITE_TEST.
12957 20 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 74 65 sqlite3_opente
12958 6d 70 5f 63 6f 75 6e 74 2b 2b 3b 20 20 2f 2a 20 mp_count++; /*
12959 55 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 Used for testing
1295a 20 61 6e 64 20 61 6e 61 6c 79 73 69 73 20 6f 6e and analysis on
1295b 6c 79 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 20 20 ly */.#endif..
1295c 76 66 73 46 6c 61 67 73 20 7c 3d 20 20 53 51 4c vfsFlags |= SQL
1295d 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 ITE_OPEN_READWRI
1295e 54 45 20 7c 20 53 51 4c 49 54 45 5f 4f 50 45 4e TE | SQLITE_OPEN
1295f 5f 43 52 45 41 54 45 20 7c 0a 20 20 20 20 20 20 _CREATE |.
12960 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 SQLITE_OPE
12961 4e 5f 45 58 43 4c 55 53 49 56 45 20 7c 20 53 51 N_EXCLUSIVE | SQ
12962 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 LITE_OPEN_DELETE
12963 4f 4e 43 4c 4f 53 45 3b 0a 20 20 72 63 20 3d 20 ONCLOSE;. rc =
12964 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 50 sqlite3OsOpen(pP
12965 61 67 65 72 2d 3e 70 56 66 73 2c 20 30 2c 20 70 ager->pVfs, 0, p
12966 46 69 6c 65 2c 20 76 66 73 46 6c 61 67 73 2c 20 File, vfsFlags,
12967 30 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 0);. assert( rc
12968 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 69 !=SQLITE_OK || i
12969 73 4f 70 65 6e 28 70 46 69 6c 65 29 20 29 3b 0a sOpen(pFile) );.
1296a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
1296b 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 62 75 /*.** Set the bu
1296c 73 79 20 68 61 6e 64 6c 65 72 20 66 75 6e 63 74 sy handler funct
1296d 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 ion..**.** The p
1296e 61 67 65 72 20 69 6e 76 6f 6b 65 73 20 74 68 65 ager invokes the
1296f 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 20 69 66 busy-handler if
12970 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 29 sqlite3OsLock()
12971 20 72 65 74 75 72 6e 73 20 0a 2a 2a 20 53 51 4c returns .** SQL
12972 49 54 45 5f 42 55 53 59 20 77 68 65 6e 20 74 72 ITE_BUSY when tr
12973 79 69 6e 67 20 74 6f 20 75 70 67 72 61 64 65 20 ying to upgrade
12974 66 72 6f 6d 20 6e 6f 2d 6c 6f 63 6b 20 74 6f 20 from no-lock to
12975 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 2c 0a 2a a SHARED lock,.*
12976 2a 20 6f 72 20 77 68 65 6e 20 74 72 79 69 6e 67 * or when trying
12977 20 74 6f 20 75 70 67 72 61 64 65 20 66 72 6f 6d to upgrade from
12978 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b a RESERVED lock
12979 20 74 6f 20 61 6e 20 45 58 43 4c 55 53 49 56 45 to an EXCLUSIVE
1297a 20 0a 2a 2a 20 6c 6f 63 6b 2e 20 49 74 20 64 6f .** lock. It do
1297b 65 73 20 2a 6e 6f 74 2a 20 69 6e 76 6f 6b 65 20 es *not* invoke
1297c 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 the busy handler
1297d 20 77 68 65 6e 20 75 70 67 72 61 64 69 6e 67 20 when upgrading
1297e 66 72 6f 6d 0a 2a 2a 20 53 48 41 52 45 44 20 74 from.** SHARED t
1297f 6f 20 52 45 53 45 52 56 45 44 2c 20 6f 72 20 77 o RESERVED, or w
12980 68 65 6e 20 75 70 67 72 61 64 69 6e 67 20 66 72 hen upgrading fr
12981 6f 6d 20 53 48 41 52 45 44 20 74 6f 20 45 58 43 om SHARED to EXC
12982 4c 55 53 49 56 45 0a 2a 2a 20 28 77 68 69 63 68 LUSIVE.** (which
12983 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 68 occurs during h
12984 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62 ot-journal rollb
12985 61 63 6b 29 2e 20 53 75 6d 6d 61 72 79 3a 0a 2a ack). Summary:.*
12986 2a 0a 2a 2a 20 20 20 54 72 61 6e 73 69 74 69 6f *.** Transitio
12987 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 n
12988 20 20 20 20 20 20 20 20 20 7c 20 49 6e 76 6f 6b | Invok
12989 65 73 20 78 42 75 73 79 48 61 6e 64 6c 65 72 0a es xBusyHandler.
1298a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ** -----------
1298b 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1298c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1298d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a -------------.**
1298e 20 20 20 4e 4f 5f 4c 4f 43 4b 20 20 20 20 20 20 NO_LOCK
1298f 20 2d 3e 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20 -> SHARED_LOCK
12990 20 20 20 20 20 7c 20 59 65 73 0a 2a 2a 20 20 20 | Yes.**
12991 53 48 41 52 45 44 5f 4c 4f 43 4b 20 20 20 2d 3e SHARED_LOCK ->
12992 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 20 RESERVED_LOCK
12993 20 20 7c 20 4e 6f 0a 2a 2a 20 20 20 53 48 41 52 | No.** SHAR
12994 45 44 5f 4c 4f 43 4b 20 20 20 2d 3e 20 45 58 43 ED_LOCK -> EXC
12995 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 20 20 7c 20 LUSIVE_LOCK |
12996 4e 6f 0a 2a 2a 20 20 20 52 45 53 45 52 56 45 44 No.** RESERVED
12997 5f 4c 4f 43 4b 20 2d 3e 20 45 58 43 4c 55 53 49 _LOCK -> EXCLUSI
12998 56 45 5f 4c 4f 43 4b 20 20 20 7c 20 59 65 73 0a VE_LOCK | Yes.
12999 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 62 75 73 **.** If the bus
1299a 79 2d 68 61 6e 64 6c 65 72 20 63 61 6c 6c 62 61 y-handler callba
1299b 63 6b 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a ck returns non-z
1299c 65 72 6f 2c 20 74 68 65 20 6c 6f 63 6b 20 69 73 ero, the lock is
1299d 20 0a 2a 2a 20 72 65 74 72 69 65 64 2e 20 49 66 .** retried. If
1299e 20 69 74 20 72 65 74 75 72 6e 73 20 7a 65 72 6f it returns zero
1299f 2c 20 74 68 65 6e 20 74 68 65 20 53 51 4c 49 54 , then the SQLIT
129a0 45 5f 42 55 53 59 20 65 72 72 6f 72 20 69 73 0a E_BUSY error is.
129a1 2a 2a 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 ** returned to t
129a2 68 65 20 63 61 6c 6c 65 72 20 6f 66 20 74 68 65 he caller of the
129a3 20 70 61 67 65 72 20 41 50 49 20 66 75 6e 63 74 pager API funct
129a4 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ion..*/.SQLITE_P
129a5 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
129a6 74 65 33 50 61 67 65 72 53 65 74 42 75 73 79 68 te3PagerSetBusyh
129a7 61 6e 64 6c 65 72 28 0a 20 20 50 61 67 65 72 20 andler(. Pager
129a8 2a 70 50 61 67 65 72 2c 20 20 20 20 20 20 20 20 *pPager,
129a9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
129aa 2a 20 50 61 67 65 72 20 6f 62 6a 65 63 74 20 2a * Pager object *
129ab 2f 0a 20 20 69 6e 74 20 28 2a 78 42 75 73 79 48 /. int (*xBusyH
129ac 61 6e 64 6c 65 72 29 28 76 6f 69 64 20 2a 29 2c andler)(void *),
129ad 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e /* Poin
129ae 74 65 72 20 74 6f 20 62 75 73 79 2d 68 61 6e 64 ter to busy-hand
129af 6c 65 72 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a ler function */.
129b0 20 20 76 6f 69 64 20 2a 70 42 75 73 79 48 61 6e void *pBusyHan
129b1 64 6c 65 72 41 72 67 20 20 20 20 20 20 20 20 20 dlerArg
129b2 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 /* Argume
129b3 6e 74 20 74 6f 20 70 61 73 73 20 74 6f 20 78 42 nt to pass to xB
129b4 75 73 79 48 61 6e 64 6c 65 72 20 2a 2f 0a 29 7b usyHandler */.){
129b5 20 20 0a 20 20 70 50 61 67 65 72 2d 3e 78 42 75 . pPager->xBu
129b6 73 79 48 61 6e 64 6c 65 72 20 3d 20 78 42 75 73 syHandler = xBus
129b7 79 48 61 6e 64 6c 65 72 3b 0a 20 20 70 50 61 67 yHandler;. pPag
129b8 65 72 2d 3e 70 42 75 73 79 48 61 6e 64 6c 65 72 er->pBusyHandler
129b9 41 72 67 20 3d 20 70 42 75 73 79 48 61 6e 64 6c Arg = pBusyHandl
129ba 65 72 41 72 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 erArg;.}../*.**
129bb 52 65 70 6f 72 74 20 74 68 65 20 63 75 72 72 65 Report the curre
129bc 6e 74 20 70 61 67 65 20 73 69 7a 65 20 61 6e 64 nt page size and
129bd 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 73 65 72 number of reser
129be 76 65 64 20 62 79 74 65 73 20 62 61 63 6b 0a 2a ved bytes back.*
129bf 2a 20 74 6f 20 74 68 65 20 63 6f 64 65 63 2e 0a * to the codec..
129c0 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
129c1 5f 48 41 53 5f 43 4f 44 45 43 0a 73 74 61 74 69 _HAS_CODEC.stati
129c2 63 20 76 6f 69 64 20 70 61 67 65 72 52 65 70 6f c void pagerRepo
129c3 72 74 53 69 7a 65 28 50 61 67 65 72 20 2a 70 50 rtSize(Pager *pP
129c4 61 67 65 72 29 7b 0a 20 20 69 66 28 20 70 50 61 ager){. if( pPa
129c5 67 65 72 2d 3e 78 43 6f 64 65 63 53 69 7a 65 43 ger->xCodecSizeC
129c6 68 6e 67 20 29 7b 0a 20 20 20 20 70 50 61 67 65 hng ){. pPage
129c7 72 2d 3e 78 43 6f 64 65 63 53 69 7a 65 43 68 6e r->xCodecSizeChn
129c8 67 28 70 50 61 67 65 72 2d 3e 70 43 6f 64 65 63 g(pPager->pCodec
129c9 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 , pPager->pageSi
129ca 7a 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 ze,.
129cb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 (
129cc 69 6e 74 29 70 50 61 67 65 72 2d 3e 6e 52 65 73 int)pPager->nRes
129cd 65 72 76 65 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6c erve);. }.}.#el
129ce 73 65 0a 23 20 64 65 66 69 6e 65 20 70 61 67 65 se.# define page
129cf 72 52 65 70 6f 72 74 53 69 7a 65 28 58 29 20 20 rReportSize(X)
129d0 20 20 20 2f 2a 20 4e 6f 2d 6f 70 20 69 66 20 77 /* No-op if w
129d1 65 20 64 6f 20 6e 6f 74 20 73 75 70 70 6f 72 74 e do not support
129d2 20 61 20 63 6f 64 65 63 20 2a 2f 0a 23 65 6e 64 a codec */.#end
129d3 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 if../*.** Change
129d4 20 74 68 65 20 70 61 67 65 20 73 69 7a 65 20 75 the page size u
129d5 73 65 64 20 62 79 20 74 68 65 20 50 61 67 65 72 sed by the Pager
129d6 20 6f 62 6a 65 63 74 2e 20 54 68 65 20 6e 65 77 object. The new
129d7 20 70 61 67 65 20 73 69 7a 65 20 0a 2a 2a 20 69 page size .** i
129d8 73 20 70 61 73 73 65 64 20 69 6e 20 2a 70 50 61 s passed in *pPa
129d9 67 65 53 69 7a 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 geSize..**.** If
129da 20 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e the pager is in
129db 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 the error state
129dc 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 when this funct
129dd 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2c 20 69 ion is called, i
129de 74 0a 2a 2a 20 69 73 20 61 20 6e 6f 2d 6f 70 2e t.** is a no-op.
129df 20 54 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 The value retur
129e0 6e 65 64 20 69 73 20 74 68 65 20 65 72 72 6f 72 ned is the error
129e1 20 73 74 61 74 65 20 65 72 72 6f 72 20 63 6f 64 state error cod
129e2 65 20 28 69 2e 65 2e 20 0a 2a 2a 20 6f 6e 65 20 e (i.e. .** one
129e3 6f 66 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 2c of SQLITE_IOERR,
129e4 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 20 SQLITE_CORRUPT
129e5 6f 72 20 53 51 4c 49 54 45 5f 46 55 4c 4c 29 2e or SQLITE_FULL).
129e6 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 .**.** Otherwise
129e7 2c 20 69 66 20 61 6c 6c 20 6f 66 20 74 68 65 20 , if all of the
129e8 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 74 72 following are tr
129e9 75 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 74 68 ue:.**.** * th
129ea 65 20 6e 65 77 20 70 61 67 65 20 73 69 7a 65 20 e new page size
129eb 28 76 61 6c 75 65 20 6f 66 20 2a 70 50 61 67 65 (value of *pPage
129ec 53 69 7a 65 29 20 69 73 20 76 61 6c 69 64 20 28 Size) is valid (
129ed 61 20 70 6f 77 65 72 20 0a 2a 2a 20 20 20 20 20 a power .**
129ee 6f 66 20 74 77 6f 20 62 65 74 77 65 65 6e 20 35 of two between 5
129ef 31 32 20 61 6e 64 20 53 51 4c 49 54 45 5f 4d 41 12 and SQLITE_MA
129f0 58 5f 50 41 47 45 5f 53 49 5a 45 2c 20 69 6e 63 X_PAGE_SIZE, inc
129f1 6c 75 73 69 76 65 29 2c 20 61 6e 64 0a 2a 2a 0a lusive), and.**.
129f2 2a 2a 20 20 20 2a 20 74 68 65 72 65 20 61 72 65 ** * there are
129f3 20 6e 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 no outstanding
129f4 70 61 67 65 20 72 65 66 65 72 65 6e 63 65 73 2c page references,
129f5 20 61 6e 64 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 74 and.**.** * t
129f6 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 65 he database is e
129f7 69 74 68 65 72 20 6e 6f 74 20 61 6e 20 69 6e 2d ither not an in-
129f8 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20 memory database
129f9 6f 72 20 69 74 20 69 73 0a 2a 2a 20 20 20 20 20 or it is.**
129fa 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 an in-memory dat
129fb 61 62 61 73 65 20 74 68 61 74 20 63 75 72 72 65 abase that curre
129fc 6e 74 6c 79 20 63 6f 6e 73 69 73 74 73 20 6f 66 ntly consists of
129fd 20 7a 65 72 6f 20 70 61 67 65 73 2e 0a 2a 2a 0a zero pages..**.
129fe 2a 2a 20 74 68 65 6e 20 74 68 65 20 70 61 67 65 ** then the page
129ff 72 20 6f 62 6a 65 63 74 20 70 61 67 65 20 73 69 r object page si
12a00 7a 65 20 69 73 20 73 65 74 20 74 6f 20 2a 70 50 ze is set to *pP
12a01 61 67 65 53 69 7a 65 2e 0a 2a 2a 0a 2a 2a 20 49 ageSize..**.** I
12a02 66 20 74 68 65 20 70 61 67 65 20 73 69 7a 65 20 f the page size
12a03 69 73 20 63 68 61 6e 67 65 64 2c 20 74 68 65 6e is changed, then
12a04 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 75 this function u
12a05 73 65 73 20 73 71 6c 69 74 65 33 50 61 67 65 72 ses sqlite3Pager
12a06 4d 61 6c 6c 6f 63 28 29 20 0a 2a 2a 20 74 6f 20 Malloc() .** to
12a07 6f 62 74 61 69 6e 20 61 20 6e 65 77 20 50 61 67 obtain a new Pag
12a08 65 72 2e 70 54 6d 70 53 70 61 63 65 20 62 75 66 er.pTmpSpace buf
12a09 66 65 72 2e 20 49 66 20 74 68 69 73 20 61 6c 6c fer. If this all
12a0a 6f 63 61 74 69 6f 6e 20 61 74 74 65 6d 70 74 20 ocation attempt
12a0b 0a 2a 2a 20 66 61 69 6c 73 2c 20 53 51 4c 49 54 .** fails, SQLIT
12a0c 45 5f 4e 4f 4d 45 4d 20 69 73 20 72 65 74 75 72 E_NOMEM is retur
12a0d 6e 65 64 20 61 6e 64 20 74 68 65 20 70 61 67 65 ned and the page
12a0e 20 73 69 7a 65 20 72 65 6d 61 69 6e 73 20 75 6e size remains un
12a0f 63 68 61 6e 67 65 64 2e 20 0a 2a 2a 20 49 6e 20 changed. .** In
12a10 61 6c 6c 20 6f 74 68 65 72 20 63 61 73 65 73 2c all other cases,
12a11 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 SQLITE_OK is re
12a12 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 turned..**.** If
12a13 20 74 68 65 20 70 61 67 65 20 73 69 7a 65 20 69 the page size i
12a14 73 20 6e 6f 74 20 63 68 61 6e 67 65 64 2c 20 65 s not changed, e
12a15 69 74 68 65 72 20 62 65 63 61 75 73 65 20 6f 6e ither because on
12a16 65 20 6f 66 20 74 68 65 20 65 6e 75 6d 65 72 61 e of the enumera
12a17 74 65 64 0a 2a 2a 20 63 6f 6e 64 69 74 69 6f 6e ted.** condition
12a18 73 20 61 62 6f 76 65 20 69 73 20 6e 6f 74 20 74 s above is not t
12a19 72 75 65 2c 20 74 68 65 20 70 61 67 65 72 20 77 rue, the pager w
12a1a 61 73 20 69 6e 20 65 72 72 6f 72 20 73 74 61 74 as in error stat
12a1b 65 20 77 68 65 6e 20 74 68 69 73 0a 2a 2a 20 66 e when this.** f
12a1c 75 6e 63 74 69 6f 6e 20 77 61 73 20 63 61 6c 6c unction was call
12a1d 65 64 2c 20 6f 72 20 62 65 63 61 75 73 65 20 74 ed, or because t
12a1e 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 he memory alloca
12a1f 74 69 6f 6e 20 61 74 74 65 6d 70 74 20 66 61 69 tion attempt fai
12a20 6c 65 64 2c 20 0a 2a 2a 20 74 68 65 6e 20 2a 70 led, .** then *p
12a21 50 61 67 65 53 69 7a 65 20 69 73 20 73 65 74 20 PageSize is set
12a22 74 6f 20 74 68 65 20 6f 6c 64 2c 20 72 65 74 61 to the old, reta
12a23 69 6e 65 64 20 70 61 67 65 20 73 69 7a 65 20 62 ined page size b
12a24 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e efore returning.
12a25 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
12a26 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 TE int sqlite3Pa
12a27 67 65 72 53 65 74 50 61 67 65 73 69 7a 65 28 50 gerSetPagesize(P
12a28 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 75 31 ager *pPager, u1
12a29 36 20 2a 70 50 61 67 65 53 69 7a 65 2c 20 69 6e 6 *pPageSize, in
12a2a 74 20 6e 52 65 73 65 72 76 65 29 7b 0a 20 20 69 t nReserve){. i
12a2b 6e 74 20 72 63 20 3d 20 70 50 61 67 65 72 2d 3e nt rc = pPager->
12a2c 65 72 72 43 6f 64 65 3b 0a 0a 20 20 69 66 28 20 errCode;.. if(
12a2d 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
12a2e 0a 20 20 20 20 75 31 36 20 70 61 67 65 53 69 7a . u16 pageSiz
12a2f 65 20 3d 20 2a 70 50 61 67 65 53 69 7a 65 3b 0a e = *pPageSize;.
12a30 20 20 20 20 61 73 73 65 72 74 28 20 70 61 67 65 assert( page
12a31 53 69 7a 65 3d 3d 30 20 7c 7c 20 28 70 61 67 65 Size==0 || (page
12a32 53 69 7a 65 3e 3d 35 31 32 20 26 26 20 70 61 67 Size>=512 && pag
12a33 65 53 69 7a 65 3c 3d 53 51 4c 49 54 45 5f 4d 41 eSize<=SQLITE_MA
12a34 58 5f 50 41 47 45 5f 53 49 5a 45 29 20 29 3b 0a X_PAGE_SIZE) );.
12a35 20 20 20 20 69 66 28 20 28 70 50 61 67 65 72 2d if( (pPager-
12a36 3e 6d 65 6d 44 62 3d 3d 30 20 7c 7c 20 70 50 61 >memDb==0 || pPa
12a37 67 65 72 2d 3e 64 62 53 69 7a 65 3d 3d 30 29 0a ger->dbSize==0).
12a38 20 20 20 20 20 26 26 20 73 71 6c 69 74 65 33 50 && sqlite3P
12a39 63 61 63 68 65 52 65 66 43 6f 75 6e 74 28 70 50 cacheRefCount(pP
12a3a 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3d 3d ager->pPCache)==
12a3b 30 20 0a 20 20 20 20 20 26 26 20 70 61 67 65 53 0 . && pageS
12a3c 69 7a 65 20 26 26 20 70 61 67 65 53 69 7a 65 21 ize && pageSize!
12a3d 3d 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a =pPager->pageSiz
12a3e 65 20 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 e . ){.
12a3f 63 68 61 72 20 2a 70 4e 65 77 20 3d 20 28 63 68 char *pNew = (ch
12a40 61 72 20 2a 29 73 71 6c 69 74 65 33 50 61 67 65 ar *)sqlite3Page
12a41 4d 61 6c 6c 6f 63 28 70 61 67 65 53 69 7a 65 29 Malloc(pageSize)
12a42 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70 4e 65 ;. if( !pNe
12a43 77 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 w ){. rc
12a44 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a = SQLITE_NOMEM;.
12a45 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
12a46 20 20 20 20 20 70 61 67 65 72 5f 72 65 73 65 74 pager_reset
12a47 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 20 20 (pPager);.
12a48 20 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 pPager->pageSi
12a49 7a 65 20 3d 20 70 61 67 65 53 69 7a 65 3b 0a 20 ze = pageSize;.
12a4a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 sqlite3Pa
12a4b 67 65 46 72 65 65 28 70 50 61 67 65 72 2d 3e 70 geFree(pPager->p
12a4c 54 6d 70 53 70 61 63 65 29 3b 0a 20 20 20 20 20 TmpSpace);.
12a4d 20 20 20 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 pPager->pTmpS
12a4e 70 61 63 65 20 3d 20 70 4e 65 77 3b 0a 20 20 20 pace = pNew;.
12a4f 20 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 sqlite3Pcac
12a50 68 65 53 65 74 50 61 67 65 53 69 7a 65 28 70 50 heSetPageSize(pP
12a51 61 67 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 70 ager->pPCache, p
12a52 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 ageSize);.
12a53 7d 0a 20 20 20 20 7d 0a 20 20 20 20 2a 70 50 61 }. }. *pPa
12a54 67 65 53 69 7a 65 20 3d 20 28 75 31 36 29 70 50 geSize = (u16)pP
12a55 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 3b 0a ager->pageSize;.
12a56 20 20 20 20 69 66 28 20 6e 52 65 73 65 72 76 65 if( nReserve
12a57 3c 30 20 29 20 6e 52 65 73 65 72 76 65 20 3d 20 <0 ) nReserve =
12a58 70 50 61 67 65 72 2d 3e 6e 52 65 73 65 72 76 65 pPager->nReserve
12a59 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 52 ;. assert( nR
12a5a 65 73 65 72 76 65 3e 3d 30 20 26 26 20 6e 52 65 eserve>=0 && nRe
12a5b 73 65 72 76 65 3c 31 30 30 30 20 29 3b 0a 20 20 serve<1000 );.
12a5c 20 20 70 50 61 67 65 72 2d 3e 6e 52 65 73 65 72 pPager->nReser
12a5d 76 65 20 3d 20 28 69 31 36 29 6e 52 65 73 65 72 ve = (i16)nReser
12a5e 76 65 3b 0a 20 20 20 20 70 61 67 65 72 52 65 70 ve;. pagerRep
12a5f 6f 72 74 53 69 7a 65 28 70 50 61 67 65 72 29 3b ortSize(pPager);
12a60 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
12a61 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 ;.}../*.** Retur
12a62 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 n a pointer to t
12a63 68 65 20 22 74 65 6d 70 6f 72 61 72 79 20 70 61 he "temporary pa
12a64 67 65 22 20 62 75 66 66 65 72 20 68 65 6c 64 20 ge" buffer held
12a65 69 6e 74 65 72 6e 61 6c 6c 79 0a 2a 2a 20 62 79 internally.** by
12a66 20 74 68 65 20 70 61 67 65 72 2e 20 20 54 68 69 the pager. Thi
12a67 73 20 69 73 20 61 20 62 75 66 66 65 72 20 74 68 s is a buffer th
12a68 61 74 20 69 73 20 62 69 67 20 65 6e 6f 75 67 68 at is big enough
12a69 20 74 6f 20 68 6f 6c 64 20 74 68 65 0a 2a 2a 20 to hold the.**
12a6a 65 6e 74 69 72 65 20 63 6f 6e 74 65 6e 74 20 6f entire content o
12a6b 66 20 61 20 64 61 74 61 62 61 73 65 20 70 61 67 f a database pag
12a6c 65 2e 20 20 54 68 69 73 20 62 75 66 66 65 72 20 e. This buffer
12a6d 69 73 20 75 73 65 64 20 69 6e 74 65 72 6e 61 6c is used internal
12a6e 6c 79 0a 2a 2a 20 64 75 72 69 6e 67 20 72 6f 6c ly.** during rol
12a6f 6c 62 61 63 6b 20 61 6e 64 20 77 69 6c 6c 20 62 lback and will b
12a70 65 20 6f 76 65 72 77 72 69 74 74 65 6e 20 77 68 e overwritten wh
12a71 65 6e 65 76 65 72 20 61 20 72 6f 6c 6c 62 61 63 enever a rollbac
12a72 6b 0a 2a 2a 20 6f 63 63 75 72 73 2e 20 20 42 75 k.** occurs. Bu
12a73 74 20 6f 74 68 65 72 20 6d 6f 64 75 6c 65 73 20 t other modules
12a74 61 72 65 20 66 72 65 65 20 74 6f 20 75 73 65 20 are free to use
12a75 69 74 20 74 6f 6f 2c 20 61 73 20 6c 6f 6e 67 20 it too, as long
12a76 61 73 0a 2a 2a 20 6e 6f 20 72 6f 6c 6c 62 61 63 as.** no rollbac
12a77 6b 73 20 61 72 65 20 68 61 70 70 65 6e 69 6e 67 ks are happening
12a78 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
12a79 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 ATE void *sqlite
12a7a 33 50 61 67 65 72 54 65 6d 70 53 70 61 63 65 28 3PagerTempSpace(
12a7b 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a Pager *pPager){.
12a7c 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d return pPager-
12a7d 3e 70 54 6d 70 53 70 61 63 65 3b 0a 7d 0a 0a 2f >pTmpSpace;.}../
12a7e 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 *.** Attempt to
12a7f 73 65 74 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 set the maximum
12a80 64 61 74 61 62 61 73 65 20 70 61 67 65 20 63 6f database page co
12a81 75 6e 74 20 69 66 20 6d 78 50 61 67 65 20 69 73 unt if mxPage is
12a82 20 70 6f 73 69 74 69 76 65 2e 20 0a 2a 2a 20 4d positive. .** M
12a83 61 6b 65 20 6e 6f 20 63 68 61 6e 67 65 73 20 69 ake no changes i
12a84 66 20 6d 78 50 61 67 65 20 69 73 20 7a 65 72 6f f mxPage is zero
12a85 20 6f 72 20 6e 65 67 61 74 69 76 65 2e 20 20 41 or negative. A
12a86 6e 64 20 6e 65 76 65 72 20 72 65 64 75 63 65 20 nd never reduce
12a87 74 68 65 0a 2a 2a 20 6d 61 78 69 6d 75 6d 20 70 the.** maximum p
12a88 61 67 65 20 63 6f 75 6e 74 20 62 65 6c 6f 77 20 age count below
12a89 74 68 65 20 63 75 72 72 65 6e 74 20 73 69 7a 65 the current size
12a8a 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
12a8b 2e 0a 2a 2a 0a 2a 2a 20 52 65 67 61 72 64 6c 65 ..**.** Regardle
12a8c 73 73 20 6f 66 20 6d 78 50 61 67 65 2c 20 72 65 ss of mxPage, re
12a8d 74 75 72 6e 20 74 68 65 20 63 75 72 72 65 6e 74 turn the current
12a8e 20 6d 61 78 69 6d 75 6d 20 70 61 67 65 20 63 6f maximum page co
12a8f 75 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 unt..*/.SQLITE_P
12a90 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
12a91 65 33 50 61 67 65 72 4d 61 78 50 61 67 65 43 6f e3PagerMaxPageCo
12a92 75 6e 74 28 50 61 67 65 72 20 2a 70 50 61 67 65 unt(Pager *pPage
12a93 72 2c 20 69 6e 74 20 6d 78 50 61 67 65 29 7b 0a r, int mxPage){.
12a94 20 20 69 66 28 20 6d 78 50 61 67 65 3e 30 20 29 if( mxPage>0 )
12a95 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6d 78 {. pPager->mx
12a96 50 67 6e 6f 20 3d 20 6d 78 50 61 67 65 3b 0a 20 Pgno = mxPage;.
12a97 20 7d 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 }. sqlite3Page
12a98 72 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65 rPagecount(pPage
12a99 72 2c 20 30 29 3b 0a 20 20 72 65 74 75 72 6e 20 r, 0);. return
12a9a 70 50 61 67 65 72 2d 3e 6d 78 50 67 6e 6f 3b 0a pPager->mxPgno;.
12a9b 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c }../*.** The fol
12a9c 6c 6f 77 69 6e 67 20 73 65 74 20 6f 66 20 72 6f lowing set of ro
12a9d 75 74 69 6e 65 73 20 61 72 65 20 75 73 65 64 20 utines are used
12a9e 74 6f 20 64 69 73 61 62 6c 65 20 74 68 65 20 73 to disable the s
12a9f 69 6d 75 6c 61 74 65 64 0a 2a 2a 20 49 2f 4f 20 imulated.** I/O
12aa0 65 72 72 6f 72 20 6d 65 63 68 61 6e 69 73 6d 2e error mechanism.
12aa1 20 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 These routines
12aa2 20 61 72 65 20 75 73 65 64 20 74 6f 20 61 76 6f are used to avo
12aa3 69 64 20 73 69 6d 75 6c 61 74 65 64 0a 2a 2a 20 id simulated.**
12aa4 65 72 72 6f 72 73 20 69 6e 20 70 6c 61 63 65 73 errors in places
12aa5 20 77 68 65 72 65 20 77 65 20 64 6f 20 6e 6f 74 where we do not
12aa6 20 63 61 72 65 20 61 62 6f 75 74 20 65 72 72 6f care about erro
12aa7 72 73 2e 0a 2a 2a 0a 2a 2a 20 55 6e 6c 65 73 73 rs..**.** Unless
12aa8 20 2d 44 53 51 4c 49 54 45 5f 54 45 53 54 3d 31 -DSQLITE_TEST=1
12aa9 20 69 73 20 75 73 65 64 2c 20 74 68 65 73 65 20 is used, these
12aaa 72 6f 75 74 69 6e 65 73 20 61 72 65 20 61 6c 6c routines are all
12aab 20 6e 6f 2d 6f 70 73 0a 2a 2a 20 61 6e 64 20 67 no-ops.** and g
12aac 65 6e 65 72 61 74 65 20 6e 6f 20 63 6f 64 65 2e enerate no code.
12aad 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 .*/.#ifdef SQLIT
12aae 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 E_TEST.SQLITE_AP
12aaf 49 20 65 78 74 65 72 6e 20 69 6e 74 20 73 71 6c I extern int sql
12ab0 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 ite3_io_error_pe
12ab1 6e 64 69 6e 67 3b 0a 53 51 4c 49 54 45 5f 41 50 nding;.SQLITE_AP
12ab2 49 20 65 78 74 65 72 6e 20 69 6e 74 20 73 71 6c I extern int sql
12ab3 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 ite3_io_error_hi
12ab4 74 3b 0a 73 74 61 74 69 63 20 69 6e 74 20 73 61 t;.static int sa
12ab5 76 65 64 5f 63 6e 74 3b 0a 76 6f 69 64 20 64 69 ved_cnt;.void di
12ab6 73 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f sable_simulated_
12ab7 69 6f 5f 65 72 72 6f 72 73 28 76 6f 69 64 29 7b io_errors(void){
12ab8 0a 20 20 73 61 76 65 64 5f 63 6e 74 20 3d 20 73 . saved_cnt = s
12ab9 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f qlite3_io_error_
12aba 70 65 6e 64 69 6e 67 3b 0a 20 20 73 71 6c 69 74 pending;. sqlit
12abb 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 e3_io_error_pend
12abc 69 6e 67 20 3d 20 2d 31 3b 0a 7d 0a 76 6f 69 64 ing = -1;.}.void
12abd 20 65 6e 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 enable_simulate
12abe 64 5f 69 6f 5f 65 72 72 6f 72 73 28 76 6f 69 64 d_io_errors(void
12abf 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f ){. sqlite3_io_
12ac0 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 20 3d 20 error_pending =
12ac1 73 61 76 65 64 5f 63 6e 74 3b 0a 7d 0a 23 65 6c saved_cnt;.}.#el
12ac2 73 65 0a 23 20 64 65 66 69 6e 65 20 64 69 73 61 se.# define disa
12ac3 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f ble_simulated_io
12ac4 5f 65 72 72 6f 72 73 28 29 0a 23 20 64 65 66 69 _errors().# defi
12ac5 6e 65 20 65 6e 61 62 6c 65 5f 73 69 6d 75 6c 61 ne enable_simula
12ac6 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29 0a ted_io_errors().
12ac7 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 #endif../*.** Re
12ac8 61 64 20 74 68 65 20 66 69 72 73 74 20 4e 20 62 ad the first N b
12ac9 79 74 65 73 20 66 72 6f 6d 20 74 68 65 20 62 65 ytes from the be
12aca 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 66 ginning of the f
12acb 69 6c 65 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 0a ile into memory.
12acc 2a 2a 20 74 68 61 74 20 70 44 65 73 74 20 70 6f ** that pDest po
12acd 69 6e 74 73 20 74 6f 2e 20 0a 2a 2a 0a 2a 2a 20 ints to. .**.**
12ace 49 66 20 74 68 65 20 70 61 67 65 72 20 77 61 73 If the pager was
12acf 20 6f 70 65 6e 65 64 20 6f 6e 20 61 20 74 72 61 opened on a tra
12ad0 6e 73 69 65 6e 74 20 66 69 6c 65 20 28 7a 46 69 nsient file (zFi
12ad1 6c 65 6e 61 6d 65 3d 3d 22 22 29 2c 20 6f 72 0a lename==""), or.
12ad2 2a 2a 20 6f 70 65 6e 65 64 20 6f 6e 20 61 20 66 ** opened on a f
12ad3 69 6c 65 20 6c 65 73 73 20 74 68 61 6e 20 4e 20 ile less than N
12ad4 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2c 20 74 bytes in size, t
12ad5 68 65 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 he output buffer
12ad6 20 69 73 0a 2a 2a 20 7a 65 72 6f 65 64 20 61 6e is.** zeroed an
12ad7 64 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 d SQLITE_OK retu
12ad8 72 6e 65 64 2e 20 54 68 65 20 72 61 74 69 6f 6e rned. The ration
12ad9 61 6c 65 20 66 6f 72 20 74 68 69 73 20 69 73 20 ale for this is
12ada 74 68 61 74 20 74 68 69 73 20 0a 2a 2a 20 66 75 that this .** fu
12adb 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74 nction is used t
12adc 6f 20 72 65 61 64 20 64 61 74 61 62 61 73 65 20 o read database
12add 68 65 61 64 65 72 73 2c 20 61 6e 64 20 61 20 6e headers, and a n
12ade 65 77 20 74 72 61 6e 73 69 65 6e 74 20 6f 72 0a ew transient or.
12adf 2a 2a 20 7a 65 72 6f 20 73 69 7a 65 64 20 64 61 ** zero sized da
12ae0 74 61 62 61 73 65 20 68 61 73 20 61 20 68 65 61 tabase has a hea
12ae1 64 65 72 20 74 68 61 6e 20 63 6f 6e 73 69 73 74 der than consist
12ae2 73 20 65 6e 74 69 72 65 6c 79 20 6f 66 20 7a 65 s entirely of ze
12ae3 72 6f 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 roes..**.** If a
12ae4 6e 79 20 49 4f 20 65 72 72 6f 72 20 61 70 61 72 ny IO error apar
12ae5 74 20 66 72 6f 6d 20 53 51 4c 49 54 45 5f 49 4f t from SQLITE_IO
12ae6 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 20 69 ERR_SHORT_READ i
12ae7 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2c 0a 2a s encountered,.*
12ae8 2a 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 * the error code
12ae9 20 69 73 20 72 65 74 75 72 6e 65 64 20 74 6f 20 is returned to
12aea 74 68 65 20 63 61 6c 6c 65 72 20 61 6e 64 20 74 the caller and t
12aeb 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 he contents of t
12aec 68 65 0a 2a 2a 20 6f 75 74 70 75 74 20 62 75 66 he.** output buf
12aed 66 65 72 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a fer undefined..*
12aee 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
12aef 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 int sqlite3Page
12af0 72 52 65 61 64 46 69 6c 65 68 65 61 64 65 72 28 rReadFileheader(
12af1 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 Pager *pPager, i
12af2 6e 74 20 4e 2c 20 75 6e 73 69 67 6e 65 64 20 63 nt N, unsigned c
12af3 68 61 72 20 2a 70 44 65 73 74 29 7b 0a 20 20 69 har *pDest){. i
12af4 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
12af5 4b 3b 0a 20 20 6d 65 6d 73 65 74 28 70 44 65 73 K;. memset(pDes
12af6 74 2c 20 30 2c 20 4e 29 3b 0a 20 20 61 73 73 65 t, 0, N);. asse
12af7 72 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 rt( isOpen(pPage
12af8 72 2d 3e 66 64 29 20 7c 7c 20 70 50 61 67 65 72 r->fd) || pPager
12af9 2d 3e 74 65 6d 70 46 69 6c 65 20 29 3b 0a 20 20 ->tempFile );.
12afa 69 66 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 if( isOpen(pPage
12afb 72 2d 3e 66 64 29 20 29 7b 0a 20 20 20 20 49 4f r->fd) ){. IO
12afc 54 52 41 43 45 28 28 22 44 42 48 44 52 20 25 70 TRACE(("DBHDR %p
12afd 20 30 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 0 %d\n", pPager
12afe 2c 20 4e 29 29 0a 20 20 20 20 72 63 20 3d 20 73 , N)). rc = s
12aff 71 6c 69 74 65 33 4f 73 52 65 61 64 28 70 50 61 qlite3OsRead(pPa
12b00 67 65 72 2d 3e 66 64 2c 20 70 44 65 73 74 2c 20 ger->fd, pDest,
12b01 4e 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 N, 0);. if( r
12b02 63 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f c==SQLITE_IOERR_
12b03 53 48 4f 52 54 5f 52 45 41 44 20 29 7b 0a 20 20 SHORT_READ ){.
12b04 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
12b05 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 OK;. }. }.
12b06 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
12b07 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 74 .** Return the t
12b08 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 otal number of p
12b09 61 67 65 73 20 69 6e 20 74 68 65 20 64 61 74 61 ages in the data
12b0a 62 61 73 65 20 66 69 6c 65 20 61 73 73 6f 63 69 base file associ
12b0b 61 74 65 64 20 0a 2a 2a 20 77 69 74 68 20 70 50 ated .** with pP
12b0c 61 67 65 72 2e 20 4e 6f 72 6d 61 6c 6c 79 2c 20 ager. Normally,
12b0d 74 68 69 73 20 69 73 20 63 61 6c 63 75 6c 61 74 this is calculat
12b0e 65 64 20 61 73 20 28 3c 64 62 20 66 69 6c 65 20 ed as (<db file
12b0f 73 69 7a 65 3e 2f 3c 70 61 67 65 2d 73 69 7a 65 size>/<page-size
12b10 3e 29 2e 0a 2a 2a 20 48 6f 77 65 76 65 72 2c 20 >)..** However,
12b11 69 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 62 if the file is b
12b12 65 74 77 65 65 6e 20 31 20 61 6e 64 20 3c 70 61 etween 1 and <pa
12b13 67 65 2d 73 69 7a 65 3e 20 62 79 74 65 73 20 69 ge-size> bytes i
12b14 6e 20 73 69 7a 65 2c 20 74 68 65 6e 20 0a 2a 2a n size, then .**
12b15 20 74 68 69 73 20 69 73 20 63 6f 6e 73 69 64 65 this is conside
12b16 72 65 64 20 61 20 31 20 70 61 67 65 20 66 69 6c red a 1 page fil
12b17 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 e..**.** If the
12b18 70 61 67 65 72 20 69 73 20 69 6e 20 65 72 72 6f pager is in erro
12b19 72 20 73 74 61 74 65 20 77 68 65 6e 20 74 68 69 r state when thi
12b1a 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 s function is ca
12b1b 6c 6c 65 64 2c 20 74 68 65 6e 20 74 68 65 0a 2a lled, then the.*
12b1c 2a 20 65 72 72 6f 72 20 73 74 61 74 65 20 65 72 * error state er
12b1d 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 ror code is retu
12b1e 72 6e 65 64 20 61 6e 64 20 2a 70 6e 50 61 67 65 rned and *pnPage
12b1f 20 6c 65 66 74 20 75 6e 63 68 61 6e 67 65 64 2e left unchanged.
12b20 20 4f 72 2c 0a 2a 2a 20 69 66 20 74 68 65 20 66 Or,.** if the f
12b21 69 6c 65 20 73 79 73 74 65 6d 20 68 61 73 20 74 ile system has t
12b22 6f 20 62 65 20 71 75 65 72 69 65 64 20 66 6f 72 o be queried for
12b23 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 the size of the
12b24 20 66 69 6c 65 20 61 6e 64 0a 2a 2a 20 74 68 65 file and.** the
12b25 20 71 75 65 72 79 20 61 74 74 65 6d 70 74 20 72 query attempt r
12b26 65 74 75 72 6e 73 20 61 6e 20 49 4f 20 65 72 72 eturns an IO err
12b27 6f 72 2c 20 74 68 65 20 49 4f 20 65 72 72 6f 72 or, the IO error
12b28 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 code is returne
12b29 64 0a 2a 2a 20 61 6e 64 20 2a 70 6e 50 61 67 65 d.** and *pnPage
12b2a 20 69 73 20 6c 65 66 74 20 75 6e 63 68 61 6e 67 is left unchang
12b2b 65 64 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 ed..**.** Otherw
12b2c 69 73 65 2c 20 69 66 20 65 76 65 72 79 74 68 69 ise, if everythi
12b2d 6e 67 20 69 73 20 73 75 63 63 65 73 73 66 75 6c ng is successful
12b2e 2c 20 74 68 65 6e 20 53 51 4c 49 54 45 5f 4f 4b , then SQLITE_OK
12b2f 20 69 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 is returned.**
12b30 61 6e 64 20 2a 70 6e 50 61 67 65 20 69 73 20 73 and *pnPage is s
12b31 65 74 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 et to the number
12b32 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 of pages in the
12b33 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a 53 51 database..*/.SQ
12b34 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
12b35 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 sqlite3PagerPag
12b36 65 63 6f 75 6e 74 28 50 61 67 65 72 20 2a 70 50 ecount(Pager *pP
12b37 61 67 65 72 2c 20 69 6e 74 20 2a 70 6e 50 61 67 ager, int *pnPag
12b38 65 29 7b 0a 20 20 50 67 6e 6f 20 6e 50 61 67 65 e){. Pgno nPage
12b39 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
12b3a 2f 2a 20 56 61 6c 75 65 20 74 6f 20 72 65 74 75 /* Value to retu
12b3b 72 6e 20 76 69 61 20 2a 70 6e 50 61 67 65 20 2a rn via *pnPage *
12b3c 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 70 /.. /* If the p
12b3d 61 67 65 72 20 69 73 20 61 6c 72 65 61 64 79 20 ager is already
12b3e 69 6e 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 in the error sta
12b3f 74 65 2c 20 72 65 74 75 72 6e 20 74 68 65 20 65 te, return the e
12b40 72 72 6f 72 20 63 6f 64 65 2e 20 2a 2f 0a 20 20 rror code. */.
12b41 69 66 28 20 70 50 61 67 65 72 2d 3e 65 72 72 43 if( pPager->errC
12b42 6f 64 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ode ){. retur
12b43 6e 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 n pPager->errCod
12b44 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 44 65 74 e;. }.. /* Det
12b45 65 72 6d 69 6e 65 20 74 68 65 20 6e 75 6d 62 65 ermine the numbe
12b46 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 r of pages in th
12b47 65 20 66 69 6c 65 2e 20 53 74 6f 72 65 20 74 68 e file. Store th
12b48 69 73 20 69 6e 20 6e 50 61 67 65 2e 20 2a 2f 0a is in nPage. */.
12b49 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 64 62 if( pPager->db
12b4a 53 69 7a 65 56 61 6c 69 64 20 29 7b 0a 20 20 20 SizeValid ){.
12b4b 20 6e 50 61 67 65 20 3d 20 70 50 61 67 65 72 2d nPage = pPager-
12b4c 3e 64 62 53 69 7a 65 3b 0a 20 20 7d 65 6c 73 65 >dbSize;. }else
12b4d 7b 0a 20 20 20 20 69 6e 74 20 72 63 3b 20 20 20 {. int rc;
12b4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
12b4f 20 45 72 72 6f 72 20 72 65 74 75 72 6e 65 64 20 Error returned
12b50 62 79 20 4f 73 46 69 6c 65 53 69 7a 65 28 29 20 by OsFileSize()
12b51 2a 2f 0a 20 20 20 20 69 36 34 20 6e 20 3d 20 30 */. i64 n = 0
12b52 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
12b53 2a 20 46 69 6c 65 20 73 69 7a 65 20 69 6e 20 62 * File size in b
12b54 79 74 65 73 20 72 65 74 75 72 6e 65 64 20 62 79 ytes returned by
12b55 20 4f 73 46 69 6c 65 53 69 7a 65 28 29 20 2a 2f OsFileSize() */
12b56 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 73 .. assert( is
12b57 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 Open(pPager->fd)
12b58 20 7c 7c 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 || pPager->temp
12b59 46 69 6c 65 20 29 3b 0a 20 20 20 20 69 66 28 20 File );. if(
12b5a 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 isOpen(pPager->f
12b5b 64 29 20 26 26 20 28 30 20 21 3d 20 28 72 63 20 d) && (0 != (rc
12b5c 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 = sqlite3OsFileS
12b5d 69 7a 65 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 ize(pPager->fd,
12b5e 26 6e 29 29 29 20 29 7b 0a 20 20 20 20 20 20 70 &n))) ){. p
12b5f 61 67 65 72 5f 65 72 72 6f 72 28 70 50 61 67 65 ager_error(pPage
12b60 72 2c 20 72 63 29 3b 0a 20 20 20 20 20 20 72 65 r, rc);. re
12b61 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 turn rc;. }.
12b62 20 20 20 69 66 28 20 6e 3e 30 20 26 26 20 6e 3c if( n>0 && n<
12b63 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 pPager->pageSize
12b64 20 29 7b 0a 20 20 20 20 20 20 6e 50 61 67 65 20 ){. nPage
12b65 3d 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a = 1;. }else{.
12b66 20 20 20 20 20 20 6e 50 61 67 65 20 3d 20 28 50 nPage = (P
12b67 67 6e 6f 29 28 6e 20 2f 20 70 50 61 67 65 72 2d gno)(n / pPager-
12b68 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 >pageSize);.
12b69 7d 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72 }. if( pPager
12b6a 2d 3e 73 74 61 74 65 21 3d 50 41 47 45 52 5f 55 ->state!=PAGER_U
12b6b 4e 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 20 20 70 NLOCK ){. p
12b6c 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20 Pager->dbSize =
12b6d 6e 50 61 67 65 3b 0a 20 20 20 20 20 20 70 50 61 nPage;. pPa
12b6e 67 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a 65 20 ger->dbFileSize
12b6f 3d 20 6e 50 61 67 65 3b 0a 20 20 20 20 20 20 70 = nPage;. p
12b70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c Pager->dbSizeVal
12b71 69 64 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 id = 1;. }.
12b72 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 63 }.. /* If the c
12b73 75 72 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 urrent number of
12b74 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 66 69 pages in the fi
12b75 6c 65 20 69 73 20 67 72 65 61 74 65 72 20 74 68 le is greater th
12b76 61 6e 20 74 68 65 20 0a 20 20 2a 2a 20 63 6f 6e an the . ** con
12b77 66 69 67 75 72 65 64 20 6d 61 78 69 6d 75 6d 20 figured maximum
12b78 70 61 67 65 72 20 6e 75 6d 62 65 72 2c 20 69 6e pager number, in
12b79 63 72 65 61 73 65 20 74 68 65 20 61 6c 6c 6f 77 crease the allow
12b7a 65 64 20 6c 69 6d 69 74 20 73 6f 0a 20 20 2a 2a ed limit so. **
12b7b 20 74 68 61 74 20 74 68 65 20 66 69 6c 65 20 63 that the file c
12b7c 61 6e 20 62 65 20 72 65 61 64 2e 0a 20 20 2a 2f an be read.. */
12b7d 0a 20 20 69 66 28 20 6e 50 61 67 65 3e 70 50 61 . if( nPage>pPa
12b7e 67 65 72 2d 3e 6d 78 50 67 6e 6f 20 29 7b 0a 20 ger->mxPgno ){.
12b7f 20 20 20 70 50 61 67 65 72 2d 3e 6d 78 50 67 6e pPager->mxPgn
12b80 6f 20 3d 20 28 50 67 6e 6f 29 6e 50 61 67 65 3b o = (Pgno)nPage;
12b81 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 20 74 . }.. /* Set t
12b82 68 65 20 6f 75 74 70 75 74 20 76 61 72 69 61 62 he output variab
12b83 6c 65 20 61 6e 64 20 72 65 74 75 72 6e 20 53 51 le and return SQ
12b84 4c 49 54 45 5f 4f 4b 20 2a 2f 0a 20 20 69 66 28 LITE_OK */. if(
12b85 20 70 6e 50 61 67 65 20 29 7b 0a 20 20 20 20 2a pnPage ){. *
12b86 70 6e 50 61 67 65 20 3d 20 6e 50 61 67 65 3b 0a pnPage = nPage;.
12b87 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c }. return SQL
12b88 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a ITE_OK;.}.../*.*
12b89 2a 20 54 72 79 20 74 6f 20 6f 62 74 61 69 6e 20 * Try to obtain
12b8a 61 20 6c 6f 63 6b 20 6f 66 20 74 79 70 65 20 6c a lock of type l
12b8b 6f 63 6b 74 79 70 65 20 6f 6e 20 74 68 65 20 64 ocktype on the d
12b8c 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 49 66 atabase file. If
12b8d 0a 2a 2a 20 61 20 73 69 6d 69 6c 61 72 20 6f 72 .** a similar or
12b8e 20 67 72 65 61 74 65 72 20 6c 6f 63 6b 20 69 73 greater lock is
12b8f 20 61 6c 72 65 61 64 79 20 68 65 6c 64 2c 20 74 already held, t
12b90 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
12b91 61 20 6e 6f 2d 6f 70 0a 2a 2a 20 28 72 65 74 75 a no-op.** (retu
12b92 72 6e 69 6e 67 20 53 51 4c 49 54 45 5f 4f 4b 20 rning SQLITE_OK
12b93 69 6d 6d 65 64 69 61 74 65 6c 79 29 2e 0a 2a 2a immediately)..**
12b94 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 61 .** Otherwise, a
12b95 74 74 65 6d 70 74 20 74 6f 20 6f 62 74 61 69 6e ttempt to obtain
12b96 20 74 68 65 20 6c 6f 63 6b 20 75 73 69 6e 67 20 the lock using
12b97 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 29 2e sqlite3OsLock().
12b98 20 49 6e 76 6f 6b 65 20 0a 2a 2a 20 74 68 65 20 Invoke .** the
12b99 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 20 69 66 busy callback if
12b9a 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 63 75 72 the lock is cur
12b9b 72 65 6e 74 6c 79 20 6e 6f 74 20 61 76 61 69 6c rently not avail
12b9c 61 62 6c 65 2e 20 52 65 70 65 61 74 20 0a 2a 2a able. Repeat .**
12b9d 20 75 6e 74 69 6c 20 74 68 65 20 62 75 73 79 20 until the busy
12b9e 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 callback returns
12b9f 20 66 61 6c 73 65 20 6f 72 20 75 6e 74 69 6c 20 false or until
12ba0 74 68 65 20 61 74 74 65 6d 70 74 20 74 6f 20 0a the attempt to .
12ba1 2a 2a 20 6f 62 74 61 69 6e 20 74 68 65 20 6c 6f ** obtain the lo
12ba2 63 6b 20 73 75 63 63 65 65 64 73 2e 0a 2a 2a 0a ck succeeds..**.
12ba3 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 ** Return SQLITE
12ba4 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 20 61 _OK on success a
12ba5 6e 64 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 nd an error code
12ba6 20 69 66 20 77 65 20 63 61 6e 6e 6f 74 20 6f 62 if we cannot ob
12ba7 74 61 69 6e 0a 2a 2a 20 74 68 65 20 6c 6f 63 6b tain.** the lock
12ba8 2e 20 49 66 20 74 68 65 20 6c 6f 63 6b 20 69 73 . If the lock is
12ba9 20 6f 62 74 61 69 6e 65 64 20 73 75 63 63 65 73 obtained succes
12baa 73 66 75 6c 6c 79 2c 20 73 65 74 20 74 68 65 20 sfully, set the
12bab 50 61 67 65 72 2e 73 74 61 74 65 20 0a 2a 2a 20 Pager.state .**
12bac 76 61 72 69 61 62 6c 65 20 74 6f 20 6c 6f 63 6b variable to lock
12bad 74 79 70 65 20 62 65 66 6f 72 65 20 72 65 74 75 type before retu
12bae 72 6e 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 rning..*/.static
12baf 20 69 6e 74 20 70 61 67 65 72 5f 77 61 69 74 5f int pager_wait_
12bb0 6f 6e 5f 6c 6f 63 6b 28 50 61 67 65 72 20 2a 70 on_lock(Pager *p
12bb1 50 61 67 65 72 2c 20 69 6e 74 20 6c 6f 63 6b 74 Pager, int lockt
12bb2 79 70 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 ype){. int rc;
12bb3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12bb4 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
12bb5 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a Return code */..
12bb6 20 20 2f 2a 20 54 68 65 20 4f 53 20 6c 6f 63 6b /* The OS lock
12bb7 20 76 61 6c 75 65 73 20 6d 75 73 74 20 62 65 20 values must be
12bb8 74 68 65 20 73 61 6d 65 20 61 73 20 74 68 65 20 the same as the
12bb9 50 61 67 65 72 20 6c 6f 63 6b 20 76 61 6c 75 65 Pager lock value
12bba 73 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 50 s */. assert( P
12bbb 41 47 45 52 5f 53 48 41 52 45 44 3d 3d 53 48 41 AGER_SHARED==SHA
12bbc 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 RED_LOCK );. as
12bbd 73 65 72 74 28 20 50 41 47 45 52 5f 52 45 53 45 sert( PAGER_RESE
12bbe 52 56 45 44 3d 3d 52 45 53 45 52 56 45 44 5f 4c RVED==RESERVED_L
12bbf 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 OCK );. assert(
12bc0 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 PAGER_EXCLUSIVE
12bc1 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b ==EXCLUSIVE_LOCK
12bc2 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 );.. /* If the
12bc3 20 66 69 6c 65 20 69 73 20 63 75 72 72 65 6e 74 file is current
12bc4 6c 79 20 75 6e 6c 6f 63 6b 65 64 20 74 68 65 6e ly unlocked then
12bc5 20 74 68 65 20 73 69 7a 65 20 6d 75 73 74 20 62 the size must b
12bc6 65 20 75 6e 6b 6e 6f 77 6e 20 2a 2f 0a 20 20 61 e unknown */. a
12bc7 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 ssert( pPager->s
12bc8 74 61 74 65 3e 3d 50 41 47 45 52 5f 53 48 41 52 tate>=PAGER_SHAR
12bc9 45 44 20 7c 7c 20 70 50 61 67 65 72 2d 3e 64 62 ED || pPager->db
12bca 53 69 7a 65 56 61 6c 69 64 3d 3d 30 20 29 3b 0a SizeValid==0 );.
12bcb 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61 74 . /* Check that
12bcc 20 74 68 69 73 20 69 73 20 65 69 74 68 65 72 20 this is either
12bcd 61 20 6e 6f 2d 6f 70 20 28 62 65 63 61 75 73 65 a no-op (because
12bce 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 6c the requested l
12bcf 6f 63 6b 20 69 73 20 0a 20 20 2a 2a 20 61 6c 72 ock is . ** alr
12bd0 65 61 64 79 20 68 65 6c 64 2c 20 6f 72 20 6f 6e eady held, or on
12bd1 65 20 6f 66 20 74 68 65 20 74 72 61 6e 73 69 73 e of the transis
12bd2 74 69 6f 6e 73 20 74 68 61 74 20 74 68 65 20 62 tions that the b
12bd3 75 73 79 2d 68 61 6e 64 6c 65 72 0a 20 20 2a 2a usy-handler. **
12bd4 20 6d 61 79 20 62 65 20 69 6e 76 6f 6b 65 64 20 may be invoked
12bd5 64 75 72 69 6e 67 2c 20 61 63 63 6f 72 64 69 6e during, accordin
12bd6 67 20 74 6f 20 74 68 65 20 63 6f 6d 6d 65 6e 74 g to the comment
12bd7 20 61 62 6f 76 65 0a 20 20 2a 2a 20 73 71 6c 69 above. ** sqli
12bd8 74 65 33 50 61 67 65 72 53 65 74 42 75 73 79 68 te3PagerSetBusyh
12bd9 61 6e 64 6c 65 72 28 29 2e 0a 20 20 2a 2f 0a 20 andler().. */.
12bda 20 61 73 73 65 72 74 28 20 28 70 50 61 67 65 72 assert( (pPager
12bdb 2d 3e 73 74 61 74 65 3e 3d 6c 6f 63 6b 74 79 70 ->state>=locktyp
12bdc 65 29 0a 20 20 20 20 20 20 20 7c 7c 20 28 70 50 e). || (pP
12bdd 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 ager->state==PAG
12bde 45 52 5f 55 4e 4c 4f 43 4b 20 26 26 20 6c 6f 63 ER_UNLOCK && loc
12bdf 6b 74 79 70 65 3d 3d 50 41 47 45 52 5f 53 48 41 ktype==PAGER_SHA
12be0 52 45 44 29 0a 20 20 20 20 20 20 20 7c 7c 20 28 RED). || (
12be1 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 pPager->state==P
12be2 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 26 26 AGER_RESERVED &&
12be3 20 6c 6f 63 6b 74 79 70 65 3d 3d 50 41 47 45 52 locktype==PAGER
12be4 5f 45 58 43 4c 55 53 49 56 45 29 0a 20 20 29 3b _EXCLUSIVE). );
12be5 0a 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e .. if( pPager->
12be6 73 74 61 74 65 3e 3d 6c 6f 63 6b 74 79 70 65 20 state>=locktype
12be7 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 ){. rc = SQLI
12be8 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a TE_OK;. }else{.
12be9 20 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 20 72 do {. r
12bea 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 c = sqlite3OsLoc
12beb 6b 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 6c 6f k(pPager->fd, lo
12bec 63 6b 74 79 70 65 29 3b 0a 20 20 20 20 7d 77 68 cktype);. }wh
12bed 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f ile( rc==SQLITE_
12bee 42 55 53 59 20 26 26 20 70 50 61 67 65 72 2d 3e BUSY && pPager->
12bef 78 42 75 73 79 48 61 6e 64 6c 65 72 28 70 50 61 xBusyHandler(pPa
12bf0 67 65 72 2d 3e 70 42 75 73 79 48 61 6e 64 6c 65 ger->pBusyHandle
12bf1 72 41 72 67 29 20 29 3b 0a 20 20 20 20 69 66 28 rArg) );. if(
12bf2 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
12bf3 7b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e {. pPager->
12bf4 73 74 61 74 65 20 3d 20 28 75 38 29 6c 6f 63 6b state = (u8)lock
12bf5 74 79 70 65 3b 0a 20 20 20 20 20 20 49 4f 54 52 type;. IOTR
12bf6 41 43 45 28 28 22 4c 4f 43 4b 20 25 70 20 25 64 ACE(("LOCK %p %d
12bf7 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 6c 6f 63 \n", pPager, loc
12bf8 6b 74 79 70 65 29 29 0a 20 20 20 20 7d 0a 20 20 ktype)). }.
12bf9 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
12bfa 0a 0a 2f 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e ../*.** Function
12bfb 20 61 73 73 65 72 74 54 72 75 6e 63 61 74 65 43 assertTruncateC
12bfc 6f 6e 73 74 72 61 69 6e 74 28 70 50 61 67 65 72 onstraint(pPager
12bfd 29 20 63 68 65 63 6b 73 20 74 68 61 74 20 6f 6e ) checks that on
12bfe 65 20 6f 66 20 74 68 65 20 0a 2a 2a 20 66 6f 6c e of the .** fol
12bff 6c 6f 77 69 6e 67 20 69 73 20 74 72 75 65 20 66 lowing is true f
12c00 6f 72 20 61 6c 6c 20 64 69 72 74 79 20 70 61 67 or all dirty pag
12c01 65 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 es currently in
12c02 74 68 65 20 70 61 67 65 2d 63 61 63 68 65 3a 0a the page-cache:.
12c03 2a 2a 0a 2a 2a 20 20 20 61 29 20 54 68 65 20 70 **.** a) The p
12c04 61 67 65 20 6e 75 6d 62 65 72 20 69 73 20 6c 65 age number is le
12c05 73 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c ss than or equal
12c06 20 74 6f 20 74 68 65 20 73 69 7a 65 20 6f 66 20 to the size of
12c07 74 68 65 20 0a 2a 2a 20 20 20 20 20 20 63 75 72 the .** cur
12c08 72 65 6e 74 20 64 61 74 61 62 61 73 65 20 69 6d rent database im
12c09 61 67 65 2c 20 69 6e 20 70 61 67 65 73 2c 20 4f age, in pages, O
12c0a 52 0a 2a 2a 0a 2a 2a 20 20 20 62 29 20 69 66 20 R.**.** b) if
12c0b 74 68 65 20 70 61 67 65 20 63 6f 6e 74 65 6e 74 the page content
12c0c 20 77 65 72 65 20 77 72 69 74 74 65 6e 20 61 74 were written at
12c0d 20 74 68 69 73 20 74 69 6d 65 2c 20 69 74 20 77 this time, it w
12c0e 6f 75 6c 64 20 6e 6f 74 0a 2a 2a 20 20 20 20 20 ould not.**
12c0f 20 62 65 20 6e 65 63 65 73 73 61 72 79 20 74 6f be necessary to
12c10 20 77 72 69 74 65 20 74 68 65 20 63 75 72 72 65 write the curre
12c11 6e 74 20 63 6f 6e 74 65 6e 74 20 6f 75 74 20 74 nt content out t
12c12 6f 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 o the sub-journa
12c13 6c 0a 2a 2a 20 20 20 20 20 20 28 61 73 20 64 65 l.** (as de
12c14 74 65 72 6d 69 6e 65 64 20 62 79 20 66 75 6e 63 termined by func
12c15 74 69 6f 6e 20 73 75 62 6a 52 65 71 75 69 72 65 tion subjRequire
12c16 73 50 61 67 65 28 29 29 2e 0a 2a 2a 0a 2a 2a 20 sPage())..**.**
12c17 49 66 20 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e If the condition
12c18 20 61 73 73 65 72 74 65 64 20 62 79 20 74 68 69 asserted by thi
12c19 73 20 66 75 6e 63 74 69 6f 6e 20 77 65 72 65 20 s function were
12c1a 6e 6f 74 20 74 72 75 65 2c 20 61 6e 64 20 74 68 not true, and th
12c1b 65 0a 2a 2a 20 64 69 72 74 79 20 70 61 67 65 20 e.** dirty page
12c1c 77 65 72 65 20 74 6f 20 62 65 20 64 69 73 63 61 were to be disca
12c1d 72 64 65 64 20 66 72 6f 6d 20 74 68 65 20 63 61 rded from the ca
12c1e 63 68 65 20 76 69 61 20 74 68 65 20 70 61 67 65 che via the page
12c1f 72 53 74 72 65 73 73 28 29 0a 2a 2a 20 72 6f 75 rStress().** rou
12c20 74 69 6e 65 2c 20 70 61 67 65 72 53 74 72 65 73 tine, pagerStres
12c21 73 28 29 20 77 6f 75 6c 64 20 6e 6f 74 20 77 72 s() would not wr
12c22 69 74 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 ite the current
12c23 70 61 67 65 20 63 6f 6e 74 65 6e 74 20 74 6f 0a page content to.
12c24 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 ** the database
12c25 66 69 6c 65 2e 20 49 66 20 61 20 73 61 76 65 70 file. If a savep
12c26 6f 69 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e oint transaction
12c27 20 77 65 72 65 20 72 6f 6c 6c 65 64 20 62 61 63 were rolled bac
12c28 6b 20 61 66 74 65 72 0a 2a 2a 20 74 68 69 73 20 k after.** this
12c29 68 61 70 70 65 6e 65 64 2c 20 74 68 65 20 63 6f happened, the co
12c2a 72 72 65 63 74 20 62 65 68 61 76 69 6f 75 72 20 rrect behaviour
12c2b 77 6f 75 6c 64 20 62 65 20 74 6f 20 72 65 73 74 would be to rest
12c2c 6f 72 65 20 74 68 65 20 63 75 72 72 65 6e 74 0a ore the current.
12c2d 2a 2a 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 ** content of th
12c2e 65 20 70 61 67 65 2e 20 48 6f 77 65 76 65 72 2c e page. However,
12c2f 20 73 69 6e 63 65 20 74 68 69 73 20 63 6f 6e 74 since this cont
12c30 65 6e 74 20 69 73 20 6e 6f 74 20 70 72 65 73 65 ent is not prese
12c31 6e 74 20 69 6e 20 65 69 74 68 65 72 0a 2a 2a 20 nt in either.**
12c32 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
12c33 65 20 6f 72 20 74 68 65 20 70 6f 72 74 69 6f 6e e or the portion
12c34 20 6f 66 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b of the rollback
12c35 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 0a 2a 2a journal and .**
12c36 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c sub-journal rol
12c37 6c 65 64 20 62 61 63 6b 20 74 68 65 20 63 6f 6e led back the con
12c38 74 65 6e 74 20 63 6f 75 6c 64 20 6e 6f 74 20 62 tent could not b
12c39 65 20 72 65 73 74 6f 72 65 64 20 61 6e 64 20 74 e restored and t
12c3a 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 69 he.** database i
12c3b 6d 61 67 65 20 77 6f 75 6c 64 20 62 65 63 6f 6d mage would becom
12c3c 65 20 63 6f 72 72 75 70 74 2e 20 49 74 20 69 73 e corrupt. It is
12c3d 20 74 68 65 72 65 66 6f 72 65 20 66 6f 72 74 75 therefore fortu
12c3e 6e 61 74 65 20 74 68 61 74 20 0a 2a 2a 20 74 68 nate that .** th
12c3f 69 73 20 63 69 72 63 75 6d 73 74 61 6e 63 65 20 is circumstance
12c40 63 61 6e 6e 6f 74 20 61 72 69 73 65 2e 0a 2a 2f cannot arise..*/
12c41 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c .#if defined(SQL
12c42 49 54 45 5f 44 45 42 55 47 29 0a 73 74 61 74 69 ITE_DEBUG).stati
12c43 63 20 76 6f 69 64 20 61 73 73 65 72 74 54 72 75 c void assertTru
12c44 6e 63 61 74 65 43 6f 6e 73 74 72 61 69 6e 74 43 ncateConstraintC
12c45 62 28 50 67 48 64 72 20 2a 70 50 67 29 7b 0a 20 b(PgHdr *pPg){.
12c46 20 61 73 73 65 72 74 28 20 70 50 67 2d 3e 66 6c assert( pPg->fl
12c47 61 67 73 26 50 47 48 44 52 5f 44 49 52 54 59 20 ags&PGHDR_DIRTY
12c48 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 73 75 );. assert( !su
12c49 62 6a 52 65 71 75 69 72 65 73 50 61 67 65 28 70 bjRequiresPage(p
12c4a 50 67 29 20 7c 7c 20 70 50 67 2d 3e 70 67 6e 6f Pg) || pPg->pgno
12c4b 3c 3d 70 50 67 2d 3e 70 50 61 67 65 72 2d 3e 64 <=pPg->pPager->d
12c4c 62 53 69 7a 65 20 29 3b 0a 7d 0a 73 74 61 74 69 bSize );.}.stati
12c4d 63 20 76 6f 69 64 20 61 73 73 65 72 74 54 72 75 c void assertTru
12c4e 6e 63 61 74 65 43 6f 6e 73 74 72 61 69 6e 74 28 ncateConstraint(
12c4f 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a Pager *pPager){.
12c50 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 49 sqlite3PcacheI
12c51 74 65 72 61 74 65 44 69 72 74 79 28 70 50 61 67 terateDirty(pPag
12c52 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 61 73 73 er->pPCache, ass
12c53 65 72 74 54 72 75 6e 63 61 74 65 43 6f 6e 73 74 ertTruncateConst
12c54 72 61 69 6e 74 43 62 29 3b 0a 7d 0a 23 65 6c 73 raintCb);.}.#els
12c55 65 0a 23 20 64 65 66 69 6e 65 20 61 73 73 65 72 e.# define asser
12c56 74 54 72 75 6e 63 61 74 65 43 6f 6e 73 74 72 61 tTruncateConstra
12c57 69 6e 74 28 70 50 61 67 65 72 29 0a 23 65 6e 64 int(pPager).#end
12c58 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 72 75 6e 63 61 if../*.** Trunca
12c59 74 65 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 te the in-memory
12c5a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 database file i
12c5b 6d 61 67 65 20 74 6f 20 6e 50 61 67 65 20 70 61 mage to nPage pa
12c5c 67 65 73 2e 20 54 68 69 73 20 0a 2a 2a 20 66 75 ges. This .** fu
12c5d 6e 63 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 nction does not
12c5e 61 63 74 75 61 6c 6c 79 20 6d 6f 64 69 66 79 20 actually modify
12c5f 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
12c60 65 20 6f 6e 20 64 69 73 6b 2e 20 49 74 20 0a 2a e on disk. It .*
12c61 2a 20 6a 75 73 74 20 73 65 74 73 20 74 68 65 20 * just sets the
12c62 69 6e 74 65 72 6e 61 6c 20 73 74 61 74 65 20 6f internal state o
12c63 66 20 74 68 65 20 70 61 67 65 72 20 6f 62 6a 65 f the pager obje
12c64 63 74 20 73 6f 20 74 68 61 74 20 74 68 65 20 0a ct so that the .
12c65 2a 2a 20 74 72 75 6e 63 61 74 69 6f 6e 20 77 69 ** truncation wi
12c66 6c 6c 20 62 65 20 64 6f 6e 65 20 77 68 65 6e 20 ll be done when
12c67 74 68 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e the current tran
12c68 73 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 saction is commi
12c69 74 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f tted..*/.SQLITE_
12c6a 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
12c6b 69 74 65 33 50 61 67 65 72 54 72 75 6e 63 61 74 ite3PagerTruncat
12c6c 65 49 6d 61 67 65 28 50 61 67 65 72 20 2a 70 50 eImage(Pager *pP
12c6d 61 67 65 72 2c 20 50 67 6e 6f 20 6e 50 61 67 65 ager, Pgno nPage
12c6e 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 ){. assert( pPa
12c6f 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 ger->dbSizeValid
12c70 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 );. assert( pP
12c71 61 67 65 72 2d 3e 64 62 53 69 7a 65 3e 3d 6e 50 ager->dbSize>=nP
12c72 61 67 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 age );. assert(
12c73 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d pPager->state>=
12c74 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 29 PAGER_RESERVED )
12c75 3b 0a 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 ;. pPager->dbSi
12c76 7a 65 20 3d 20 6e 50 61 67 65 3b 0a 20 20 61 73 ze = nPage;. as
12c77 73 65 72 74 54 72 75 6e 63 61 74 65 43 6f 6e 73 sertTruncateCons
12c78 74 72 61 69 6e 74 28 70 50 61 67 65 72 29 3b 0a traint(pPager);.
12c79 7d 0a 0a 2f 2a 0a 2a 2a 20 53 68 75 74 64 6f 77 }../*.** Shutdow
12c7a 6e 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65 n the page cache
12c7b 2e 20 20 46 72 65 65 20 61 6c 6c 20 6d 65 6d 6f . Free all memo
12c7c 72 79 20 61 6e 64 20 63 6c 6f 73 65 20 61 6c 6c ry and close all
12c7d 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 files..**.** If
12c7e 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 a transaction w
12c7f 61 73 20 69 6e 20 70 72 6f 67 72 65 73 73 20 77 as in progress w
12c80 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 hen this routine
12c81 20 69 73 20 63 61 6c 6c 65 64 2c 20 74 68 61 74 is called, that
12c82 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 .** transaction
12c83 69 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 is rolled back.
12c84 20 41 6c 6c 20 6f 75 74 73 74 61 6e 64 69 6e 67 All outstanding
12c85 20 70 61 67 65 73 20 61 72 65 20 69 6e 76 61 6c pages are inval
12c86 69 64 61 74 65 64 0a 2a 2a 20 61 6e 64 20 74 68 idated.** and th
12c87 65 69 72 20 6d 65 6d 6f 72 79 20 69 73 20 66 72 eir memory is fr
12c88 65 65 64 2e 20 20 41 6e 79 20 61 74 74 65 6d 70 eed. Any attemp
12c89 74 20 74 6f 20 75 73 65 20 61 20 70 61 67 65 20 t to use a page
12c8a 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20 77 69 associated.** wi
12c8b 74 68 20 74 68 69 73 20 70 61 67 65 20 63 61 63 th this page cac
12c8c 68 65 20 61 66 74 65 72 20 74 68 69 73 20 66 75 he after this fu
12c8d 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 77 nction returns w
12c8e 69 6c 6c 20 6c 69 6b 65 6c 79 0a 2a 2a 20 72 65 ill likely.** re
12c8f 73 75 6c 74 20 69 6e 20 61 20 63 6f 72 65 64 75 sult in a coredu
12c90 6d 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 mp..**.** This f
12c91 75 6e 63 74 69 6f 6e 20 61 6c 77 61 79 73 20 73 unction always s
12c92 75 63 63 65 65 64 73 2e 20 49 66 20 61 20 74 72 ucceeds. If a tr
12c93 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74 ansaction is act
12c94 69 76 65 20 61 6e 20 61 74 74 65 6d 70 74 0a 2a ive an attempt.*
12c95 2a 20 69 73 20 6d 61 64 65 20 74 6f 20 72 6f 6c * is made to rol
12c96 6c 20 69 74 20 62 61 63 6b 2e 20 49 66 20 61 6e l it back. If an
12c97 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 error occurs du
12c98 72 69 6e 67 20 74 68 65 20 72 6f 6c 6c 62 61 63 ring the rollbac
12c99 6b 20 0a 2a 2a 20 61 20 68 6f 74 20 6a 6f 75 72 k .** a hot jour
12c9a 6e 61 6c 20 6d 61 79 20 62 65 20 6c 65 66 74 20 nal may be left
12c9b 69 6e 20 74 68 65 20 66 69 6c 65 73 79 73 74 65 in the filesyste
12c9c 6d 20 62 75 74 20 6e 6f 20 65 72 72 6f 72 20 69 m but no error i
12c9d 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 74 6f s returned.** to
12c9e 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a 2a 2f 0a the caller..*/.
12c9f 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
12ca0 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 nt sqlite3PagerC
12ca1 6c 6f 73 65 28 50 61 67 65 72 20 2a 70 50 61 67 lose(Pager *pPag
12ca2 65 72 29 7b 0a 20 20 64 69 73 61 62 6c 65 5f 73 er){. disable_s
12ca3 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f imulated_io_erro
12ca4 72 73 28 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 rs();. sqlite3B
12ca5 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 eginBenignMalloc
12ca6 28 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 65 72 ();. pPager->er
12ca7 72 43 6f 64 65 20 3d 20 30 3b 0a 20 20 70 50 61 rCode = 0;. pPa
12ca8 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f ger->exclusiveMo
12ca9 64 65 20 3d 20 30 3b 0a 20 20 70 61 67 65 72 5f de = 0;. pager_
12caa 72 65 73 65 74 28 70 50 61 67 65 72 29 3b 0a 20 reset(pPager);.
12cab 20 69 66 28 20 4d 45 4d 44 42 20 29 7b 0a 20 20 if( MEMDB ){.
12cac 20 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 70 pager_unlock(p
12cad 50 61 67 65 72 29 3b 0a 20 20 7d 65 6c 73 65 7b Pager);. }else{
12cae 0a 20 20 20 20 2f 2a 20 53 65 74 20 50 61 67 65 . /* Set Page
12caf 72 2e 6a 6f 75 72 6e 61 6c 48 64 72 20 74 6f 20 r.journalHdr to
12cb0 2d 31 20 66 6f 72 20 74 68 65 20 62 65 6e 65 66 -1 for the benef
12cb1 69 74 20 6f 66 20 74 68 65 20 70 61 67 65 72 5f it of the pager_
12cb2 70 6c 61 79 62 61 63 6b 28 29 20 0a 20 20 20 20 playback() .
12cb3 2a 2a 20 63 61 6c 6c 20 77 68 69 63 68 20 6d 61 ** call which ma
12cb4 79 20 62 65 20 6d 61 64 65 20 66 72 6f 6d 20 77 y be made from w
12cb5 69 74 68 69 6e 20 70 61 67 65 72 55 6e 6c 6f 63 ithin pagerUnloc
12cb6 6b 41 6e 64 52 6f 6c 6c 62 61 63 6b 28 29 2e 20 kAndRollback().
12cb7 49 66 20 69 74 0a 20 20 20 20 2a 2a 20 69 73 20 If it. ** is
12cb8 6e 6f 74 20 2d 31 2c 20 74 68 65 6e 20 74 68 65 not -1, then the
12cb9 20 75 6e 73 79 6e 63 65 64 20 70 6f 72 74 69 6f unsynced portio
12cba 6e 20 6f 66 20 61 6e 20 6f 70 65 6e 20 6a 6f 75 n of an open jou
12cbb 72 6e 61 6c 20 66 69 6c 65 20 6d 61 79 0a 20 20 rnal file may.
12cbc 20 20 2a 2a 20 62 65 20 70 6c 61 79 65 64 20 62 ** be played b
12cbd 61 63 6b 20 69 6e 74 6f 20 74 68 65 20 64 61 74 ack into the dat
12cbe 61 62 61 73 65 2e 20 49 66 20 61 20 70 6f 77 65 abase. If a powe
12cbf 72 20 66 61 69 6c 75 72 65 20 6f 63 63 75 72 73 r failure occurs
12cc0 20 77 68 69 6c 65 0a 20 20 20 20 2a 2a 20 74 68 while. ** th
12cc1 69 73 20 69 73 20 68 61 70 70 65 6e 69 6e 67 2c is is happening,
12cc2 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6d 61 the database ma
12cc3 79 20 62 65 63 6f 6d 65 20 63 6f 72 72 75 70 74 y become corrupt
12cc4 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 50 61 .. */. pPa
12cc5 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 ger->journalHdr
12cc6 3d 20 2d 31 3b 0a 20 20 20 20 70 61 67 65 72 55 = -1;. pagerU
12cc7 6e 6c 6f 63 6b 41 6e 64 52 6f 6c 6c 62 61 63 6b nlockAndRollback
12cc8 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a 20 20 (pPager);. }.
12cc9 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e sqlite3EndBenign
12cca 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 65 6e 61 62 Malloc();. enab
12ccb 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f le_simulated_io_
12ccc 65 72 72 6f 72 73 28 29 3b 0a 20 20 50 41 47 45 errors();. PAGE
12ccd 52 54 52 41 43 45 28 28 22 43 4c 4f 53 45 20 25 RTRACE(("CLOSE %
12cce 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 70 50 d\n", PAGERID(pP
12ccf 61 67 65 72 29 29 29 3b 0a 20 20 49 4f 54 52 41 ager)));. IOTRA
12cd0 43 45 28 28 22 43 4c 4f 53 45 20 25 70 5c 6e 22 CE(("CLOSE %p\n"
12cd1 2c 20 70 50 61 67 65 72 29 29 0a 20 20 73 71 6c , pPager)). sql
12cd2 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 50 61 67 ite3OsClose(pPag
12cd3 65 72 2d 3e 66 64 29 3b 0a 20 20 73 71 6c 69 74 er->fd);. sqlit
12cd4 65 33 50 61 67 65 46 72 65 65 28 70 50 61 67 65 e3PageFree(pPage
12cd5 72 2d 3e 70 54 6d 70 53 70 61 63 65 29 3b 0a 20 r->pTmpSpace);.
12cd6 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c sqlite3PcacheCl
12cd7 6f 73 65 28 70 50 61 67 65 72 2d 3e 70 50 43 61 ose(pPager->pPCa
12cd8 63 68 65 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 che);..#ifdef SQ
12cd9 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 0a 20 LITE_HAS_CODEC.
12cda 20 69 66 28 20 70 50 61 67 65 72 2d 3e 78 43 6f if( pPager->xCo
12cdb 64 65 63 46 72 65 65 20 29 20 70 50 61 67 65 72 decFree ) pPager
12cdc 2d 3e 78 43 6f 64 65 63 46 72 65 65 28 70 50 61 ->xCodecFree(pPa
12cdd 67 65 72 2d 3e 70 43 6f 64 65 63 29 3b 0a 23 65 ger->pCodec);.#e
12cde 6e 64 69 66 0a 0a 20 20 61 73 73 65 72 74 28 20 ndif.. assert(
12cdf 21 70 50 61 67 65 72 2d 3e 61 53 61 76 65 70 6f !pPager->aSavepo
12ce0 69 6e 74 20 26 26 20 21 70 50 61 67 65 72 2d 3e int && !pPager->
12ce1 70 49 6e 4a 6f 75 72 6e 61 6c 20 29 3b 0a 20 20 pInJournal );.
12ce2 61 73 73 65 72 74 28 20 21 69 73 4f 70 65 6e 28 assert( !isOpen(
12ce3 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 26 26 20 pPager->jfd) &&
12ce4 21 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e !isOpen(pPager->
12ce5 73 6a 66 64 29 20 29 3b 0a 0a 20 20 73 71 6c 69 sjfd) );.. sqli
12ce6 74 65 33 5f 66 72 65 65 28 70 50 61 67 65 72 29 te3_free(pPager)
12ce7 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
12ce8 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 20 21 64 65 E_OK;.}..#if !de
12ce9 66 69 6e 65 64 28 4e 44 45 42 55 47 29 20 7c 7c fined(NDEBUG) ||
12cea 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f defined(SQLITE_
12ceb 54 45 53 54 29 0a 2f 2a 0a 2a 2a 20 52 65 74 75 TEST)./*.** Retu
12cec 72 6e 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 rn the page numb
12ced 65 72 20 66 6f 72 20 70 61 67 65 20 70 50 67 2e er for page pPg.
12cee 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
12cef 54 45 20 50 67 6e 6f 20 73 71 6c 69 74 65 33 50 TE Pgno sqlite3P
12cf0 61 67 65 72 50 61 67 65 6e 75 6d 62 65 72 28 44 agerPagenumber(D
12cf1 62 50 61 67 65 20 2a 70 50 67 29 7b 0a 20 20 72 bPage *pPg){. r
12cf2 65 74 75 72 6e 20 70 50 67 2d 3e 70 67 6e 6f 3b eturn pPg->pgno;
12cf3 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a .}.#endif../*.**
12cf4 20 49 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 72 Increment the r
12cf5 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 66 eference count f
12cf6 6f 72 20 70 61 67 65 20 70 50 67 2e 0a 2a 2f 0a or page pPg..*/.
12cf7 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
12cf8 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 oid sqlite3Pager
12cf9 52 65 66 28 44 62 50 61 67 65 20 2a 70 50 67 29 Ref(DbPage *pPg)
12cfa 7b 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 {. sqlite3Pcach
12cfb 65 52 65 66 28 70 50 67 29 3b 0a 7d 0a 0a 2f 2a eRef(pPg);.}../*
12cfc 0a 2a 2a 20 53 79 6e 63 20 74 68 65 20 6a 6f 75 .** Sync the jou
12cfd 72 6e 61 6c 2e 20 49 6e 20 6f 74 68 65 72 20 77 rnal. In other w
12cfe 6f 72 64 73 2c 20 6d 61 6b 65 20 73 75 72 65 20 ords, make sure
12cff 61 6c 6c 20 74 68 65 20 70 61 67 65 73 20 74 68 all the pages th
12d00 61 74 20 68 61 76 65 0a 2a 2a 20 62 65 65 6e 20 at have.** been
12d01 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 6a written to the j
12d02 6f 75 72 6e 61 6c 20 68 61 76 65 20 61 63 74 75 ournal have actu
12d03 61 6c 6c 79 20 72 65 61 63 68 65 64 20 74 68 65 ally reached the
12d04 20 73 75 72 66 61 63 65 20 6f 66 20 74 68 65 0a surface of the.
12d05 2a 2a 20 64 69 73 6b 20 61 6e 64 20 63 61 6e 20 ** disk and can
12d06 62 65 20 72 65 73 74 6f 72 65 64 20 69 6e 20 74 be restored in t
12d07 68 65 20 65 76 65 6e 74 20 6f 66 20 61 20 68 6f he event of a ho
12d08 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62 61 t-journal rollba
12d09 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 ck..**.** If the
12d0a 20 50 61 67 65 72 2e 6e 65 65 64 53 79 6e 63 20 Pager.needSync
12d0b 66 6c 61 67 20 69 73 20 6e 6f 74 20 73 65 74 2c flag is not set,
12d0c 20 74 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 then this funct
12d0d 69 6f 6e 20 69 73 20 61 0a 2a 2a 20 6e 6f 2d 6f ion is a.** no-o
12d0e 70 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68 p. Otherwise, th
12d0f 65 20 61 63 74 69 6f 6e 73 20 72 65 71 75 69 72 e actions requir
12d10 65 64 20 64 65 70 65 6e 64 20 6f 6e 20 74 68 65 ed depend on the
12d11 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 65 0a 2a 2a journal-mode.**
12d12 20 61 6e 64 20 74 68 65 20 64 65 76 69 63 65 20 and the device
12d13 63 68 61 72 61 63 74 65 72 69 73 74 69 63 73 20 characteristics
12d14 6f 66 20 74 68 65 20 74 68 65 20 66 69 6c 65 2d of the the file-
12d15 73 79 73 74 65 6d 2c 20 61 73 20 66 6f 6c 6c 6f system, as follo
12d16 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 49 66 ws:.**.** * If
12d17 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
12d18 65 20 69 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 e is an in-memor
12d19 79 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 y journal file,
12d1a 6e 6f 20 61 63 74 69 6f 6e 20 6e 65 65 64 0a 2a no action need.*
12d1b 2a 20 20 20 20 20 62 65 20 74 61 6b 65 6e 2e 0a * be taken..
12d1c 2a 2a 0a 2a 2a 20 20 20 2a 20 4f 74 68 65 72 77 **.** * Otherw
12d1d 69 73 65 2c 20 69 66 20 74 68 65 20 64 65 76 69 ise, if the devi
12d1e 63 65 20 64 6f 65 73 20 6e 6f 74 20 73 75 70 70 ce does not supp
12d1f 6f 72 74 20 74 68 65 20 53 41 46 45 5f 41 50 50 ort the SAFE_APP
12d20 45 4e 44 20 70 72 6f 70 65 72 74 79 2c 0a 2a 2a END property,.**
12d21 20 20 20 20 20 74 68 65 6e 20 74 68 65 20 6e 52 then the nR
12d22 65 63 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20 ec field of the
12d23 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 77 72 most recently wr
12d24 69 74 74 65 6e 20 6a 6f 75 72 6e 61 6c 20 68 65 itten journal he
12d25 61 64 65 72 0a 2a 2a 20 20 20 20 20 69 73 20 75 ader.** is u
12d26 70 64 61 74 65 64 20 74 6f 20 63 6f 6e 74 61 69 pdated to contai
12d27 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 n the number of
12d28 6a 6f 75 72 6e 61 6c 20 72 65 63 6f 72 64 73 20 journal records
12d29 74 68 61 74 20 68 61 76 65 0a 2a 2a 20 20 20 20 that have.**
12d2a 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20 66 6f been written fo
12d2b 6c 6c 6f 77 69 6e 67 20 69 74 2e 20 49 66 20 74 llowing it. If t
12d2c 68 65 20 70 61 67 65 72 20 69 73 20 6f 70 65 72 he pager is oper
12d2d 61 74 69 6e 67 20 69 6e 20 66 75 6c 6c 2d 73 79 ating in full-sy
12d2e 6e 63 0a 2a 2a 20 20 20 20 20 6d 6f 64 65 2c 20 nc.** mode,
12d2f 74 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c then the journal
12d30 20 66 69 6c 65 20 69 73 20 73 79 6e 63 65 64 20 file is synced
12d31 62 65 66 6f 72 65 20 74 68 69 73 20 66 69 65 6c before this fiel
12d32 64 20 69 73 20 75 70 64 61 74 65 64 2e 0a 2a 2a d is updated..**
12d33 0a 2a 2a 20 20 20 2a 20 49 66 20 74 68 65 20 64 .** * If the d
12d34 65 76 69 63 65 20 64 6f 65 73 20 6e 6f 74 20 73 evice does not s
12d35 75 70 70 6f 72 74 20 74 68 65 20 53 45 51 55 45 upport the SEQUE
12d36 4e 54 49 41 4c 20 70 72 6f 70 65 72 74 79 2c 20 NTIAL property,
12d37 74 68 65 6e 20 0a 2a 2a 20 20 20 20 20 6a 6f 75 then .** jou
12d38 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 73 79 6e rnal file is syn
12d39 63 65 64 2e 0a 2a 2a 0a 2a 2a 20 4f 72 2c 20 69 ced..**.** Or, i
12d3a 6e 20 70 73 65 75 64 6f 2d 63 6f 64 65 3a 0a 2a n pseudo-code:.*
12d3b 2a 0a 2a 2a 20 20 20 69 66 28 20 4e 4f 54 20 3c *.** if( NOT <
12d3c 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 in-memory journa
12d3d 6c 3e 20 29 7b 0a 2a 2a 20 20 20 20 20 69 66 28 l> ){.** if(
12d3e 20 4e 4f 54 20 53 41 46 45 5f 41 50 50 45 4e 44 NOT SAFE_APPEND
12d3f 20 29 7b 0a 2a 2a 20 20 20 20 20 20 20 69 66 28 ){.** if(
12d40 20 3c 66 75 6c 6c 2d 73 79 6e 63 20 6d 6f 64 65 <full-sync mode
12d41 3e 20 29 20 78 53 79 6e 63 28 3c 6a 6f 75 72 6e > ) xSync(<journ
12d42 61 6c 20 66 69 6c 65 3e 29 3b 0a 2a 2a 20 20 20 al file>);.**
12d43 20 20 20 20 3c 75 70 64 61 74 65 20 6e 52 65 63 <update nRec
12d44 20 66 69 65 6c 64 3e 0a 2a 2a 20 20 20 20 20 7d field>.** }
12d45 20 0a 2a 2a 20 20 20 20 20 69 66 28 20 4e 4f 54 .** if( NOT
12d46 20 53 45 51 55 45 4e 54 49 41 4c 20 29 20 78 53 SEQUENTIAL ) xS
12d47 79 6e 63 28 3c 6a 6f 75 72 6e 61 6c 20 66 69 6c ync(<journal fil
12d48 65 3e 29 3b 0a 2a 2a 20 20 20 7d 0a 2a 2a 0a 2a e>);.** }.**.*
12d49 2a 20 54 68 65 20 50 61 67 65 72 2e 6e 65 65 64 * The Pager.need
12d4a 53 79 6e 63 20 66 6c 61 67 20 69 73 20 6e 65 76 Sync flag is nev
12d4b 65 72 20 62 65 20 73 65 74 20 66 6f 72 20 74 65 er be set for te
12d4c 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 2c 20 6f mporary files, o
12d4d 72 20 61 6e 79 0a 2a 2a 20 66 69 6c 65 20 6f 70 r any.** file op
12d4e 65 72 61 74 69 6e 67 20 69 6e 20 6e 6f 2d 73 79 erating in no-sy
12d4f 6e 63 20 6d 6f 64 65 20 28 50 61 67 65 72 2e 6e nc mode (Pager.n
12d50 6f 53 79 6e 63 20 73 65 74 20 74 6f 20 6e 6f 6e oSync set to non
12d51 2d 7a 65 72 6f 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 -zero)..**.** If
12d52 20 73 75 63 63 65 73 73 66 75 6c 2c 20 74 68 69 successful, thi
12d53 73 20 72 6f 75 74 69 6e 65 20 63 6c 65 61 72 73 s routine clears
12d54 20 74 68 65 20 50 47 48 44 52 5f 4e 45 45 44 5f the PGHDR_NEED_
12d55 53 59 4e 43 20 66 6c 61 67 20 6f 66 20 65 76 65 SYNC flag of eve
12d56 72 79 20 0a 2a 2a 20 70 61 67 65 20 63 75 72 72 ry .** page curr
12d57 65 6e 74 6c 79 20 68 65 6c 64 20 69 6e 20 6d 65 ently held in me
12d58 6d 6f 72 79 20 62 65 66 6f 72 65 20 72 65 74 75 mory before retu
12d59 72 6e 69 6e 67 20 53 51 4c 49 54 45 5f 4f 4b 2e rning SQLITE_OK.
12d5a 20 49 66 20 61 6e 20 49 4f 0a 2a 2a 20 65 72 72 If an IO.** err
12d5b 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 or is encountere
12d5c 64 2c 20 74 68 65 6e 20 74 68 65 20 49 4f 20 65 d, then the IO e
12d5d 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 rror code is ret
12d5e 75 72 6e 65 64 20 74 6f 20 74 68 65 20 63 61 6c urned to the cal
12d5f 6c 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ler..*/.static i
12d60 6e 74 20 73 79 6e 63 4a 6f 75 72 6e 61 6c 28 50 nt syncJournal(P
12d61 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 ager *pPager){.
12d62 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6e 65 65 if( pPager->nee
12d63 64 53 79 6e 63 20 29 7b 0a 20 20 20 20 61 73 73 dSync ){. ass
12d64 65 72 74 28 20 21 70 50 61 67 65 72 2d 3e 74 65 ert( !pPager->te
12d65 6d 70 46 69 6c 65 20 29 3b 0a 20 20 20 20 69 66 mpFile );. if
12d66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 ( pPager->journa
12d67 6c 4d 6f 64 65 21 3d 50 41 47 45 52 5f 4a 4f 55 lMode!=PAGER_JOU
12d68 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 RNALMODE_MEMORY
12d69 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 63 3b ){. int rc;
12d6a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12d6b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
12d6c 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a Return code */.
12d6d 20 20 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 const int
12d6e 69 44 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 44 iDc = sqlite3OsD
12d6f 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 eviceCharacteris
12d70 74 69 63 73 28 70 50 61 67 65 72 2d 3e 66 64 29 tics(pPager->fd)
12d71 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
12d72 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a isOpen(pPager->j
12d73 66 64 29 20 29 3b 0a 0a 20 20 20 20 20 20 69 66 fd) );.. if
12d74 28 20 30 3d 3d 28 69 44 63 26 53 51 4c 49 54 45 ( 0==(iDc&SQLITE
12d75 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41 50 50 45 _IOCAP_SAFE_APPE
12d76 4e 44 29 20 29 7b 0a 20 20 20 20 20 20 20 20 2f ND) ){. /
12d77 2a 20 54 68 69 73 20 62 6c 6f 63 6b 20 64 65 61 * This block dea
12d78 6c 73 20 77 69 74 68 20 61 6e 20 6f 62 73 63 75 ls with an obscu
12d79 72 65 20 70 72 6f 62 6c 65 6d 2e 20 49 66 20 74 re problem. If t
12d7a 68 65 20 6c 61 73 74 20 63 6f 6e 6e 65 63 74 69 he last connecti
12d7b 6f 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 on. ** th
12d7c 61 74 20 77 72 6f 74 65 20 74 6f 20 74 68 69 73 at wrote to this
12d7d 20 64 61 74 61 62 61 73 65 20 77 61 73 20 6f 70 database was op
12d7e 65 72 61 74 69 6e 67 20 69 6e 20 70 65 72 73 69 erating in persi
12d7f 73 74 65 6e 74 2d 6a 6f 75 72 6e 61 6c 0a 20 20 stent-journal.
12d80 20 20 20 20 20 20 2a 2a 20 6d 6f 64 65 2c 20 74 ** mode, t
12d81 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 hen the journal
12d82 66 69 6c 65 20 6d 61 79 20 61 74 20 74 68 69 73 file may at this
12d83 20 70 6f 69 6e 74 20 61 63 74 75 61 6c 6c 79 20 point actually
12d84 62 65 20 6c 61 72 67 65 72 0a 20 20 20 20 20 20 be larger.
12d85 20 20 2a 2a 20 74 68 61 6e 20 50 61 67 65 72 2e ** than Pager.
12d86 6a 6f 75 72 6e 61 6c 4f 66 66 20 62 79 74 65 73 journalOff bytes
12d87 2e 20 49 66 20 74 68 65 20 6e 65 78 74 20 74 68 . If the next th
12d88 69 6e 67 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e ing in the journ
12d89 61 6c 0a 20 20 20 20 20 20 20 20 2a 2a 20 66 69 al. ** fi
12d8a 6c 65 20 68 61 70 70 65 6e 73 20 74 6f 20 62 65 le happens to be
12d8b 20 61 20 6a 6f 75 72 6e 61 6c 2d 68 65 61 64 65 a journal-heade
12d8c 72 20 28 77 72 69 74 74 65 6e 20 61 73 20 70 61 r (written as pa
12d8d 72 74 20 6f 66 20 74 68 65 0a 20 20 20 20 20 20 rt of the.
12d8e 20 20 2a 2a 20 70 72 65 76 69 6f 75 73 20 63 6f ** previous co
12d8f 6e 6e 65 63 74 69 6f 6e 73 20 74 72 61 6e 73 61 nnections transa
12d90 63 74 69 6f 6e 29 2c 20 61 6e 64 20 61 20 63 72 ction), and a cr
12d91 61 73 68 20 6f 72 20 70 6f 77 65 72 2d 66 61 69 ash or power-fai
12d92 6c 75 72 65 20 0a 20 20 20 20 20 20 20 20 2a 2a lure . **
12d93 20 6f 63 63 75 72 73 20 61 66 74 65 72 20 6e 52 occurs after nR
12d94 65 63 20 69 73 20 75 70 64 61 74 65 64 20 62 75 ec is updated bu
12d95 74 20 62 65 66 6f 72 65 20 74 68 69 73 20 63 6f t before this co
12d96 6e 6e 65 63 74 69 6f 6e 20 77 72 69 74 65 73 20 nnection writes
12d97 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 6e 79 74 . ** anyt
12d98 68 69 6e 67 20 65 6c 73 65 20 74 6f 20 74 68 65 hing else to the
12d99 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 28 6f journal file (o
12d9a 72 20 63 6f 6d 6d 69 74 73 2f 72 6f 6c 6c 73 20 r commits/rolls
12d9b 62 61 63 6b 20 69 74 73 20 0a 20 20 20 20 20 20 back its .
12d9c 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e ** transaction
12d9d 29 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 20 6d ), then SQLite m
12d9e 61 79 20 62 65 63 6f 6d 65 20 63 6f 6e 66 75 73 ay become confus
12d9f 65 64 20 77 68 65 6e 20 64 6f 69 6e 67 20 74 68 ed when doing th
12da0 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 68 6f e . ** ho
12da1 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62 61 t-journal rollba
12da2 63 6b 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 65 63 ck following rec
12da3 6f 76 65 72 79 2e 20 49 74 20 6d 61 79 20 72 6f overy. It may ro
12da4 6c 6c 20 62 61 63 6b 20 61 6c 6c 0a 20 20 20 20 ll back all.
12da5 20 20 20 20 2a 2a 20 6f 66 20 74 68 69 73 20 63 ** of this c
12da6 6f 6e 6e 65 63 74 69 6f 6e 73 20 64 61 74 61 2c onnections data,
12da7 20 74 68 65 6e 20 70 72 6f 63 65 65 64 20 74 6f then proceed to
12da8 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 74 68 rolling back th
12da9 65 20 6f 6c 64 2c 0a 20 20 20 20 20 20 20 20 2a e old,. *
12daa 2a 20 6f 75 74 2d 6f 66 2d 64 61 74 65 20 64 61 * out-of-date da
12dab 74 61 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73 20 ta that follows
12dac 69 74 2e 20 44 61 74 61 62 61 73 65 20 63 6f 72 it. Database cor
12dad 72 75 70 74 69 6f 6e 2e 0a 20 20 20 20 20 20 20 ruption..
12dae 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 54 **. ** T
12daf 6f 20 77 6f 72 6b 20 61 72 6f 75 6e 64 20 74 68 o work around th
12db0 69 73 2c 20 69 66 20 74 68 65 20 6a 6f 75 72 6e is, if the journ
12db1 61 6c 20 66 69 6c 65 20 64 6f 65 73 20 61 70 70 al file does app
12db2 65 61 72 20 74 6f 20 63 6f 6e 74 61 69 6e 0a 20 ear to contain.
12db3 20 20 20 20 20 20 20 2a 2a 20 61 20 76 61 6c 69 ** a vali
12db4 64 20 68 65 61 64 65 72 20 66 6f 6c 6c 6f 77 69 d header followi
12db5 6e 67 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c ng Pager.journal
12db6 4f 66 66 2c 20 74 68 65 6e 20 77 72 69 74 65 20 Off, then write
12db7 61 20 30 78 30 30 0a 20 20 20 20 20 20 20 20 2a a 0x00. *
12db8 2a 20 62 79 74 65 20 74 6f 20 74 68 65 20 73 74 * byte to the st
12db9 61 72 74 20 6f 66 20 69 74 20 74 6f 20 70 72 65 art of it to pre
12dba 76 65 6e 74 20 69 74 20 66 72 6f 6d 20 62 65 69 vent it from bei
12dbb 6e 67 20 72 65 63 6f 67 6e 69 7a 65 64 2e 0a 20 ng recognized..
12dbc 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 **.
12dbd 20 20 2a 2a 20 56 61 72 69 61 62 6c 65 20 69 4e ** Variable iN
12dbe 65 78 74 48 64 72 4f 66 66 73 65 74 20 69 73 20 extHdrOffset is
12dbf 73 65 74 20 74 6f 20 74 68 65 20 6f 66 66 73 65 set to the offse
12dc0 74 20 61 74 20 77 68 69 63 68 20 74 68 69 73 0a t at which this.
12dc1 20 20 20 20 20 20 20 20 2a 2a 20 70 72 6f 62 6c ** probl
12dc2 65 6d 61 74 69 63 20 68 65 61 64 65 72 20 77 69 ematic header wi
12dc3 6c 6c 20 6f 63 63 75 72 2c 20 69 66 20 69 74 20 ll occur, if it
12dc4 65 78 69 73 74 73 2e 20 61 4d 61 67 69 63 20 69 exists. aMagic i
12dc5 73 20 75 73 65 64 20 0a 20 20 20 20 20 20 20 20 s used .
12dc6 2a 2a 20 61 73 20 61 20 74 65 6d 70 6f 72 61 72 ** as a temporar
12dc7 79 20 62 75 66 66 65 72 20 74 6f 20 69 6e 73 70 y buffer to insp
12dc8 65 63 74 20 74 68 65 20 66 69 72 73 74 20 63 6f ect the first co
12dc9 75 70 6c 65 20 6f 66 20 62 79 74 65 73 20 6f 66 uple of bytes of
12dca 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20 . ** the
12dcb 70 6f 74 65 6e 74 69 61 6c 20 6a 6f 75 72 6e 61 potential journa
12dcc 6c 20 68 65 61 64 65 72 2e 0a 20 20 20 20 20 20 l header..
12dcd 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 36 34 */. i64
12dce 20 69 4e 65 78 74 48 64 72 4f 66 66 73 65 74 3b iNextHdrOffset;
12dcf 0a 20 20 20 20 20 20 20 20 75 38 20 61 4d 61 67 . u8 aMag
12dd0 69 63 5b 38 5d 3b 0a 09 75 38 20 7a 48 65 61 64 ic[8];..u8 zHead
12dd1 65 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e er[sizeof(aJourn
12dd2 61 6c 4d 61 67 69 63 29 2b 34 5d 3b 0a 0a 09 6d alMagic)+4];...m
12dd3 65 6d 63 70 79 28 7a 48 65 61 64 65 72 2c 20 61 emcpy(zHeader, a
12dd4 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 2c 20 73 69 JournalMagic, si
12dd5 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 zeof(aJournalMag
12dd6 69 63 29 29 3b 0a 09 70 75 74 33 32 62 69 74 73 ic));..put32bits
12dd7 28 26 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 (&zHeader[sizeof
12dd8 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 5d (aJournalMagic)]
12dd9 2c 20 70 50 61 67 65 72 2d 3e 6e 52 65 63 29 3b , pPager->nRec);
12dda 0a 0a 20 20 20 20 20 20 20 20 69 4e 65 78 74 48 .. iNextH
12ddb 64 72 4f 66 66 73 65 74 20 3d 20 6a 6f 75 72 6e drOffset = journ
12ddc 61 6c 48 64 72 4f 66 66 73 65 74 28 70 50 61 67 alHdrOffset(pPag
12ddd 65 72 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 er);. rc
12dde 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 = sqlite3OsRead(
12ddf 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 61 4d 61 pPager->jfd, aMa
12de0 67 69 63 2c 20 38 2c 20 69 4e 65 78 74 48 64 72 gic, 8, iNextHdr
12de1 4f 66 66 73 65 74 29 3b 0a 20 20 20 20 20 20 20 Offset);.
12de2 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
12de3 4f 4b 20 26 26 20 30 3d 3d 6d 65 6d 63 6d 70 28 OK && 0==memcmp(
12de4 61 4d 61 67 69 63 2c 20 61 4a 6f 75 72 6e 61 6c aMagic, aJournal
12de5 4d 61 67 69 63 2c 20 38 29 20 29 7b 0a 20 20 20 Magic, 8) ){.
12de6 20 20 20 20 20 20 20 73 74 61 74 69 63 20 63 6f static co
12de7 6e 73 74 20 75 38 20 7a 65 72 6f 62 79 74 65 20 nst u8 zerobyte
12de8 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 72 = 0;. r
12de9 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 c = sqlite3OsWri
12dea 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 te(pPager->jfd,
12deb 26 7a 65 72 6f 62 79 74 65 2c 20 31 2c 20 69 4e &zerobyte, 1, iN
12dec 65 78 74 48 64 72 4f 66 66 73 65 74 29 3b 0a 20 extHdrOffset);.
12ded 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
12dee 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
12def 4f 4b 20 26 26 20 72 63 21 3d 53 51 4c 49 54 45 OK && rc!=SQLITE
12df0 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 _IOERR_SHORT_REA
12df1 44 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 D ){. r
12df2 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 eturn rc;.
12df3 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 }.. /*
12df4 57 72 69 74 65 20 74 68 65 20 6e 52 65 63 20 76 Write the nRec v
12df5 61 6c 75 65 20 69 6e 74 6f 20 74 68 65 20 6a 6f alue into the jo
12df6 75 72 6e 61 6c 20 66 69 6c 65 20 68 65 61 64 65 urnal file heade
12df7 72 2e 20 49 66 20 69 6e 0a 20 20 20 20 20 20 20 r. If in.
12df8 20 2a 2a 20 66 75 6c 6c 2d 73 79 6e 63 68 72 6f ** full-synchro
12df9 6e 6f 75 73 20 6d 6f 64 65 2c 20 73 79 6e 63 20 nous mode, sync
12dfa 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 72 73 the journal firs
12dfb 74 2e 20 54 68 69 73 20 65 6e 73 75 72 65 73 20 t. This ensures
12dfc 74 68 61 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 that. **
12dfd 61 6c 6c 20 64 61 74 61 20 68 61 73 20 72 65 61 all data has rea
12dfe 6c 6c 79 20 68 69 74 20 74 68 65 20 64 69 73 6b lly hit the disk
12dff 20 62 65 66 6f 72 65 20 6e 52 65 63 20 69 73 20 before nRec is
12e00 75 70 64 61 74 65 64 20 74 6f 20 6d 61 72 6b 0a updated to mark.
12e01 20 20 20 20 20 20 20 20 2a 2a 20 69 74 20 61 73 ** it as
12e02 20 61 20 63 61 6e 64 69 64 61 74 65 20 66 6f 72 a candidate for
12e03 20 72 6f 6c 6c 62 61 63 6b 2e 0a 20 20 20 20 20 rollback..
12e04 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a **. **
12e05 20 54 68 69 73 20 69 73 20 6e 6f 74 20 72 65 71 This is not req
12e06 75 69 72 65 64 20 69 66 20 74 68 65 20 70 65 72 uired if the per
12e07 73 69 73 74 65 6e 74 20 6d 65 64 69 61 20 73 75 sistent media su
12e08 70 70 6f 72 74 73 20 74 68 65 0a 20 20 20 20 20 pports the.
12e09 20 20 20 2a 2a 20 53 41 46 45 5f 41 50 50 45 4e ** SAFE_APPEN
12e0a 44 20 70 72 6f 70 65 72 74 79 2e 20 42 65 63 61 D property. Beca
12e0b 75 73 65 20 69 6e 20 74 68 69 73 20 63 61 73 65 use in this case
12e0c 20 69 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 it is not possi
12e0d 62 6c 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 ble . **
12e0e 66 6f 72 20 67 61 72 62 61 67 65 20 64 61 74 61 for garbage data
12e0f 20 74 6f 20 62 65 20 61 70 70 65 6e 64 65 64 20 to be appended
12e10 74 6f 20 74 68 65 20 66 69 6c 65 2c 20 74 68 65 to the file, the
12e11 20 6e 52 65 63 20 66 69 65 6c 64 0a 20 20 20 20 nRec field.
12e12 20 20 20 20 2a 2a 20 69 73 20 70 6f 70 75 6c 61 ** is popula
12e13 74 65 64 20 77 69 74 68 20 30 78 46 46 46 46 46 ted with 0xFFFFF
12e14 46 46 46 20 77 68 65 6e 20 74 68 65 20 6a 6f 75 FFF when the jou
12e15 72 6e 61 6c 20 68 65 61 64 65 72 20 69 73 20 77 rnal header is w
12e16 72 69 74 74 65 6e 0a 20 20 20 20 20 20 20 20 2a ritten. *
12e17 2a 20 61 6e 64 20 6e 65 76 65 72 20 6e 65 65 64 * and never need
12e18 73 20 74 6f 20 62 65 20 75 70 64 61 74 65 64 2e s to be updated.
12e19 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 . */.
12e1a 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e if( pPager->
12e1b 66 75 6c 6c 53 79 6e 63 20 26 26 20 30 3d 3d 28 fullSync && 0==(
12e1c 69 44 63 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 iDc&SQLITE_IOCAP
12e1d 5f 53 45 51 55 45 4e 54 49 41 4c 29 20 29 7b 0a _SEQUENTIAL) ){.
12e1e 20 20 20 20 20 20 20 20 20 20 50 41 47 45 52 54 PAGERT
12e1f 52 41 43 45 28 28 22 53 59 4e 43 20 6a 6f 75 72 RACE(("SYNC jour
12e20 6e 61 6c 20 6f 66 20 25 64 5c 6e 22 2c 20 50 41 nal of %d\n", PA
12e21 47 45 52 49 44 28 70 50 61 67 65 72 29 29 29 3b GERID(pPager)));
12e22 0a 20 20 20 20 20 20 20 20 20 20 49 4f 54 52 41 . IOTRA
12e23 43 45 28 28 22 4a 53 59 4e 43 20 25 70 5c 6e 22 CE(("JSYNC %p\n"
12e24 2c 20 70 50 61 67 65 72 29 29 0a 20 20 20 20 20 , pPager)).
12e25 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
12e26 33 4f 73 53 79 6e 63 28 70 50 61 67 65 72 2d 3e 3OsSync(pPager->
12e27 6a 66 64 2c 20 70 50 61 67 65 72 2d 3e 73 79 6e jfd, pPager->syn
12e28 63 5f 66 6c 61 67 73 29 3b 0a 20 20 20 20 20 20 c_flags);.
12e29 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
12e2a 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 TE_OK ) return r
12e2b 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 c;. }.
12e2c 20 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 4a IOTRACE(("J
12e2d 48 44 52 20 25 70 20 25 6c 6c 64 5c 6e 22 2c 20 HDR %p %lld\n",
12e2e 70 50 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e pPager, pPager->
12e2f 6a 6f 75 72 6e 61 6c 48 64 72 29 29 3b 0a 20 20 journalHdr));.
12e30 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
12e31 65 33 4f 73 57 72 69 74 65 28 0a 20 20 20 20 20 e3OsWrite(.
12e32 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a pPager->j
12e33 66 64 2c 20 7a 48 65 61 64 65 72 2c 20 73 69 7a fd, zHeader, siz
12e34 65 6f 66 28 7a 48 65 61 64 65 72 29 2c 20 70 50 eof(zHeader), pP
12e35 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 ager->journalHdr
12e36 0a 09 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 ..);. if(
12e37 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
12e38 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
12e39 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 30 3d }. if( 0=
12e3a 3d 28 69 44 63 26 53 51 4c 49 54 45 5f 49 4f 43 =(iDc&SQLITE_IOC
12e3b 41 50 5f 53 45 51 55 45 4e 54 49 41 4c 29 20 29 AP_SEQUENTIAL) )
12e3c 7b 0a 20 20 20 20 20 20 20 20 50 41 47 45 52 54 {. PAGERT
12e3d 52 41 43 45 28 28 22 53 59 4e 43 20 6a 6f 75 72 RACE(("SYNC jour
12e3e 6e 61 6c 20 6f 66 20 25 64 5c 6e 22 2c 20 50 41 nal of %d\n", PA
12e3f 47 45 52 49 44 28 70 50 61 67 65 72 29 29 29 3b GERID(pPager)));
12e40 0a 20 20 20 20 20 20 20 20 49 4f 54 52 41 43 45 . IOTRACE
12e41 28 28 22 4a 53 59 4e 43 20 25 70 5c 6e 22 2c 20 (("JSYNC %p\n",
12e42 70 50 61 67 65 72 29 29 0a 20 20 20 20 20 20 20 pPager)).
12e43 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 rc = sqlite3OsS
12e44 79 6e 63 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c ync(pPager->jfd,
12e45 20 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f 66 6c pPager->sync_fl
12e46 61 67 73 7c 20 0a 20 20 20 20 20 20 20 20 20 20 ags| .
12e47 28 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f 66 6c (pPager->sync_fl
12e48 61 67 73 3d 3d 53 51 4c 49 54 45 5f 53 59 4e 43 ags==SQLITE_SYNC
12e49 5f 46 55 4c 4c 3f 53 51 4c 49 54 45 5f 53 59 4e _FULL?SQLITE_SYN
12e4a 43 5f 44 41 54 41 4f 4e 4c 59 3a 30 29 0a 20 20 C_DATAONLY:0).
12e4b 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 20 );.
12e4c 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
12e4d 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a OK ) return rc;.
12e4e 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 }. }..
12e4f 20 20 20 2f 2a 20 54 68 65 20 6a 6f 75 72 6e 61 /* The journa
12e50 6c 20 66 69 6c 65 20 77 61 73 20 6a 75 73 74 20 l file was just
12e51 73 75 63 63 65 73 73 66 75 6c 6c 79 20 73 79 6e successfully syn
12e52 63 65 64 2e 20 53 65 74 20 50 61 67 65 72 2e 6e ced. Set Pager.n
12e53 65 65 64 53 79 6e 63 20 0a 20 20 20 20 2a 2a 20 eedSync . **
12e54 74 6f 20 7a 65 72 6f 20 61 6e 64 20 63 6c 65 61 to zero and clea
12e55 72 20 74 68 65 20 50 47 48 44 52 5f 4e 45 45 44 r the PGHDR_NEED
12e56 5f 53 59 4e 43 20 66 6c 61 67 20 6f 6e 20 61 6c _SYNC flag on al
12e57 6c 20 70 61 67 65 73 73 2e 0a 20 20 20 20 2a 2f l pagess.. */
12e58 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 65 65 . pPager->nee
12e59 64 53 79 6e 63 20 3d 20 30 3b 0a 20 20 20 20 70 dSync = 0;. p
12e5a 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 74 Pager->journalSt
12e5b 61 72 74 65 64 20 3d 20 31 3b 0a 20 20 20 20 73 arted = 1;. s
12e5c 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c 65 61 qlite3PcacheClea
12e5d 72 53 79 6e 63 46 6c 61 67 73 28 70 50 61 67 65 rSyncFlags(pPage
12e5e 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a 20 20 7d r->pPCache);. }
12e5f 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 .. return SQLIT
12e60 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 E_OK;.}../*.** T
12e61 68 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 he argument is t
12e62 68 65 20 66 69 72 73 74 20 69 6e 20 61 20 6c 69 he first in a li
12e63 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 64 69 72 nked list of dir
12e64 74 79 20 70 61 67 65 73 20 63 6f 6e 6e 65 63 74 ty pages connect
12e65 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 50 67 48 ed.** by the PgH
12e66 64 72 2e 70 44 69 72 74 79 20 70 6f 69 6e 74 65 dr.pDirty pointe
12e67 72 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e r. This function
12e68 20 77 72 69 74 65 73 20 65 61 63 68 20 6f 6e 65 writes each one
12e69 20 6f 66 20 74 68 65 0a 2a 2a 20 69 6e 2d 6d 65 of the.** in-me
12e6a 6d 6f 72 79 20 70 61 67 65 73 20 69 6e 20 74 68 mory pages in th
12e6b 65 20 6c 69 73 74 20 74 6f 20 74 68 65 20 64 61 e list to the da
12e6c 74 61 62 61 73 65 20 66 69 6c 65 2e 20 54 68 65 tabase file. The
12e6d 20 61 72 67 75 6d 65 6e 74 20 6d 61 79 0a 2a 2a argument may.**
12e6e 20 62 65 20 4e 55 4c 4c 2c 20 72 65 70 72 65 73 be NULL, repres
12e6f 65 6e 74 69 6e 67 20 61 6e 20 65 6d 70 74 79 20 enting an empty
12e70 6c 69 73 74 2e 20 49 6e 20 74 68 69 73 20 63 61 list. In this ca
12e71 73 65 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e se this function
12e72 20 69 73 0a 2a 2a 20 61 20 6e 6f 2d 6f 70 2e 0a is.** a no-op..
12e73 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 72 20 **.** The pager
12e74 6d 75 73 74 20 68 6f 6c 64 20 61 74 20 6c 65 61 must hold at lea
12e75 73 74 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f st a RESERVED lo
12e76 63 6b 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e ck when this fun
12e77 63 74 69 6f 6e 0a 2a 2a 20 69 73 20 63 61 6c 6c ction.** is call
12e78 65 64 2e 20 42 65 66 6f 72 65 20 77 72 69 74 69 ed. Before writi
12e79 6e 67 20 61 6e 79 74 68 69 6e 67 20 74 6f 20 74 ng anything to t
12e7a 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
12e7b 2c 20 74 68 69 73 20 6c 6f 63 6b 0a 2a 2a 20 69 , this lock.** i
12e7c 73 20 75 70 67 72 61 64 65 64 20 74 6f 20 61 6e s upgraded to an
12e7d 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e EXCLUSIVE lock.
12e7e 20 49 66 20 74 68 65 20 6c 6f 63 6b 20 63 61 6e If the lock can
12e7f 6e 6f 74 20 62 65 20 6f 62 74 61 69 6e 65 64 2c not be obtained,
12e80 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 20 .** SQLITE_BUSY
12e81 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 is returned and
12e82 6e 6f 20 64 61 74 61 20 69 73 20 77 72 69 74 74 no data is writt
12e83 65 6e 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 en to the databa
12e84 73 65 20 66 69 6c 65 2e 0a 2a 2a 20 0a 2a 2a 20 se file..** .**
12e85 49 66 20 74 68 65 20 70 61 67 65 72 20 69 73 20 If the pager is
12e86 61 20 74 65 6d 70 2d 66 69 6c 65 20 70 61 67 65 a temp-file page
12e87 72 20 61 6e 64 20 74 68 65 20 61 63 74 75 61 6c r and the actual
12e88 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 66 69 6c file-system fil
12e89 65 0a 2a 2a 20 69 73 20 6e 6f 74 20 79 65 74 20 e.** is not yet
12e8a 6f 70 65 6e 2c 20 69 74 20 69 73 20 63 72 65 61 open, it is crea
12e8b 74 65 64 20 61 6e 64 20 6f 70 65 6e 65 64 20 62 ted and opened b
12e8c 65 66 6f 72 65 20 61 6e 79 20 64 61 74 61 20 69 efore any data i
12e8d 73 20 0a 2a 2a 20 77 72 69 74 74 65 6e 20 6f 75 s .** written ou
12e8e 74 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 63 65 20 74 68 t..**.** Once th
12e8f 65 20 6c 6f 63 6b 20 68 61 73 20 62 65 65 6e 20 e lock has been
12e90 75 70 67 72 61 64 65 64 20 61 6e 64 2c 20 69 66 upgraded and, if
12e91 20 6e 65 63 65 73 73 61 72 79 2c 20 74 68 65 20 necessary, the
12e92 66 69 6c 65 20 6f 70 65 6e 65 64 2c 0a 2a 2a 20 file opened,.**
12e93 74 68 65 20 70 61 67 65 73 20 61 72 65 20 77 72 the pages are wr
12e94 69 74 74 65 6e 20 6f 75 74 20 74 6f 20 74 68 65 itten out to the
12e95 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 database file i
12e96 6e 20 6c 69 73 74 20 6f 72 64 65 72 2e 20 57 72 n list order. Wr
12e97 69 74 69 6e 67 0a 2a 2a 20 61 20 70 61 67 65 20 iting.** a page
12e98 69 73 20 73 6b 69 70 70 65 64 20 69 66 20 69 74 is skipped if it
12e99 20 6d 65 65 74 73 20 65 69 74 68 65 72 20 6f 66 meets either of
12e9a 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 the following c
12e9b 72 69 74 65 72 69 61 3a 0a 2a 2a 0a 2a 2a 20 20 riteria:.**.**
12e9c 20 2a 20 54 68 65 20 70 61 67 65 20 6e 75 6d 62 * The page numb
12e9d 65 72 20 69 73 20 67 72 65 61 74 65 72 20 74 68 er is greater th
12e9e 61 6e 20 50 61 67 65 72 2e 64 62 53 69 7a 65 2c an Pager.dbSize,
12e9f 20 6f 72 0a 2a 2a 20 20 20 2a 20 54 68 65 20 50 or.** * The P
12ea0 47 48 44 52 5f 44 4f 4e 54 5f 57 52 49 54 45 20 GHDR_DONT_WRITE
12ea1 66 6c 61 67 20 69 73 20 73 65 74 20 6f 6e 20 74 flag is set on t
12ea2 68 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 49 he page..**.** I
12ea3 66 20 77 72 69 74 69 6e 67 20 6f 75 74 20 61 20 f writing out a
12ea4 70 61 67 65 20 63 61 75 73 65 73 20 74 68 65 20 page causes the
12ea5 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 74 6f database file to
12ea6 20 67 72 6f 77 2c 20 50 61 67 65 72 2e 64 62 46 grow, Pager.dbF
12ea7 69 6c 65 53 69 7a 65 0a 2a 2a 20 69 73 20 75 70 ileSize.** is up
12ea8 64 61 74 65 64 20 61 63 63 6f 72 64 69 6e 67 6c dated accordingl
12ea9 79 2e 20 49 66 20 70 61 67 65 20 31 20 69 73 20 y. If page 1 is
12eaa 77 72 69 74 74 65 6e 20 6f 75 74 2c 20 74 68 65 written out, the
12eab 6e 20 74 68 65 20 76 61 6c 75 65 20 63 61 63 68 n the value cach
12eac 65 64 0a 2a 2a 20 69 6e 20 50 61 67 65 72 2e 64 ed.** in Pager.d
12ead 62 46 69 6c 65 56 65 72 73 5b 5d 20 69 73 20 75 bFileVers[] is u
12eae 70 64 61 74 65 64 20 74 6f 20 6d 61 74 63 68 20 pdated to match
12eaf 74 68 65 20 6e 65 77 20 76 61 6c 75 65 20 73 74 the new value st
12eb0 6f 72 65 64 20 69 6e 0a 2a 2a 20 74 68 65 20 64 ored in.** the d
12eb1 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a atabase file..**
12eb2 0a 2a 2a 20 49 66 20 65 76 65 72 79 74 68 69 6e .** If everythin
12eb3 67 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 2c g is successful,
12eb4 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 SQLITE_OK is re
12eb5 74 75 72 6e 65 64 2e 20 49 66 20 61 6e 20 49 4f turned. If an IO
12eb6 20 65 72 72 6f 72 20 0a 2a 2a 20 6f 63 63 75 72 error .** occur
12eb7 73 2c 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 63 s, an IO error c
12eb8 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e ode is returned.
12eb9 20 4f 72 2c 20 69 66 20 74 68 65 20 45 58 43 4c Or, if the EXCL
12eba 55 53 49 56 45 20 6c 6f 63 6b 20 63 61 6e 6e 6f USIVE lock canno
12ebb 74 0a 2a 2a 20 62 65 20 6f 62 74 61 69 6e 65 64 t.** be obtained
12ebc 2c 20 53 51 4c 49 54 45 5f 42 55 53 59 20 69 73 , SQLITE_BUSY is
12ebd 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 returned..*/.st
12ebe 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 5f 77 atic int pager_w
12ebf 72 69 74 65 5f 70 61 67 65 6c 69 73 74 28 50 67 rite_pagelist(Pg
12ec0 48 64 72 20 2a 70 4c 69 73 74 29 7b 0a 20 20 50 Hdr *pList){. P
12ec1 61 67 65 72 20 2a 70 50 61 67 65 72 3b 20 20 20 ager *pPager;
12ec2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12ec3 20 20 20 20 2f 2a 20 50 61 67 65 72 20 6f 62 6a /* Pager obj
12ec4 65 63 74 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b ect */. int rc;
12ec5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12ec6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
12ec7 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a Return code */.
12ec8 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70 4c 69 . if( NEVER(pLi
12ec9 73 74 3d 3d 30 29 20 29 20 72 65 74 75 72 6e 20 st==0) ) return
12eca 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 70 50 61 SQLITE_OK;. pPa
12ecb 67 65 72 20 3d 20 70 4c 69 73 74 2d 3e 70 50 61 ger = pList->pPa
12ecc 67 65 72 3b 0a 0a 20 20 2f 2a 20 41 74 20 74 68 ger;.. /* At th
12ecd 69 73 20 70 6f 69 6e 74 20 74 68 65 72 65 20 6d is point there m
12ece 61 79 20 62 65 20 65 69 74 68 65 72 20 61 20 52 ay be either a R
12ecf 45 53 45 52 56 45 44 20 6f 72 20 45 58 43 4c 55 ESERVED or EXCLU
12ed0 53 49 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 SIVE lock on the
12ed1 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 66 . ** database f
12ed2 69 6c 65 2e 20 49 66 20 74 68 65 72 65 20 69 73 ile. If there is
12ed3 20 61 6c 72 65 61 64 79 20 61 6e 20 45 58 43 4c already an EXCL
12ed4 55 53 49 56 45 20 6c 6f 63 6b 2c 20 74 68 65 20 USIVE lock, the
12ed5 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 2a 2a 20 63 following. ** c
12ed6 61 6c 6c 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a all is a no-op..
12ed7 20 20 2a 2a 0a 20 20 2a 2a 20 4d 6f 76 69 6e 67 **. ** Moving
12ed8 20 74 68 65 20 6c 6f 63 6b 20 66 72 6f 6d 20 52 the lock from R
12ed9 45 53 45 52 56 45 44 20 74 6f 20 45 58 43 4c 55 ESERVED to EXCLU
12eda 53 49 56 45 20 61 63 74 75 61 6c 6c 79 20 69 6e SIVE actually in
12edb 76 6f 6c 76 65 73 20 67 6f 69 6e 67 0a 20 20 2a volves going. *
12edc 2a 20 74 68 72 6f 75 67 68 20 61 6e 20 69 6e 74 * through an int
12edd 65 72 6d 65 64 69 61 74 65 20 73 74 61 74 65 20 ermediate state
12ede 50 45 4e 44 49 4e 47 2e 20 20 20 41 20 50 45 4e PENDING. A PEN
12edf 44 49 4e 47 20 6c 6f 63 6b 20 70 72 65 76 65 6e DING lock preven
12ee0 74 73 20 6e 65 77 0a 20 20 2a 2a 20 72 65 61 64 ts new. ** read
12ee1 65 72 73 20 66 72 6f 6d 20 61 74 74 61 63 68 69 ers from attachi
12ee2 6e 67 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 ng to the databa
12ee3 73 65 20 62 75 74 20 69 73 20 75 6e 73 75 66 66 se but is unsuff
12ee4 69 63 69 65 6e 74 20 66 6f 72 20 75 73 20 74 6f icient for us to
12ee5 0a 20 20 2a 2a 20 77 72 69 74 65 2e 20 20 54 68 . ** write. Th
12ee6 65 20 69 64 65 61 20 6f 66 20 61 20 50 45 4e 44 e idea of a PEND
12ee7 49 4e 47 20 6c 6f 63 6b 20 69 73 20 74 6f 20 70 ING lock is to p
12ee8 72 65 76 65 6e 74 20 6e 65 77 20 72 65 61 64 65 revent new reade
12ee9 72 73 20 66 72 6f 6d 0a 20 20 2a 2a 20 63 6f 6d rs from. ** com
12eea 69 6e 67 20 69 6e 20 77 68 69 6c 65 20 77 65 20 ing in while we
12eeb 77 61 69 74 20 66 6f 72 20 65 78 69 73 74 69 6e wait for existin
12eec 67 20 72 65 61 64 65 72 73 20 74 6f 20 63 6c 65 g readers to cle
12eed 61 72 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57 68 ar.. **. ** Wh
12eee 69 6c 65 20 74 68 65 20 70 61 67 65 72 20 69 73 ile the pager is
12eef 20 69 6e 20 74 68 65 20 52 45 53 45 52 56 45 44 in the RESERVED
12ef0 20 73 74 61 74 65 2c 20 74 68 65 20 6f 72 69 67 state, the orig
12ef1 69 6e 61 6c 20 64 61 74 61 62 61 73 65 20 66 69 inal database fi
12ef2 6c 65 0a 20 20 2a 2a 20 69 73 20 75 6e 63 68 61 le. ** is uncha
12ef3 6e 67 65 64 20 61 6e 64 20 77 65 20 63 61 6e 20 nged and we can
12ef4 72 6f 6c 6c 62 61 63 6b 20 77 69 74 68 6f 75 74 rollback without
12ef5 20 68 61 76 69 6e 67 20 74 6f 20 70 6c 61 79 62 having to playb
12ef6 61 63 6b 20 74 68 65 0a 20 20 2a 2a 20 6a 6f 75 ack the. ** jou
12ef7 72 6e 61 6c 20 69 6e 74 6f 20 74 68 65 20 6f 72 rnal into the or
12ef8 69 67 69 6e 61 6c 20 64 61 74 61 62 61 73 65 20 iginal database
12ef9 66 69 6c 65 2e 20 20 4f 6e 63 65 20 77 65 20 74 file. Once we t
12efa 72 61 6e 73 69 74 69 6f 6e 20 74 6f 0a 20 20 2a ransition to. *
12efb 2a 20 45 58 43 4c 55 53 49 56 45 2c 20 69 74 20 * EXCLUSIVE, it
12efc 6d 65 61 6e 73 20 74 68 65 20 64 61 74 61 62 61 means the databa
12efd 73 65 20 66 69 6c 65 20 68 61 73 20 62 65 65 6e se file has been
12efe 20 63 68 61 6e 67 65 64 20 61 6e 64 20 61 6e 79 changed and any
12eff 20 72 6f 6c 6c 62 61 63 6b 0a 20 20 2a 2a 20 77 rollback. ** w
12f00 69 6c 6c 20 72 65 71 75 69 72 65 20 61 20 6a 6f ill require a jo
12f01 75 72 6e 61 6c 20 70 6c 61 79 62 61 63 6b 2e 0a urnal playback..
12f02 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 */. assert( p
12f03 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 Pager->state>=PA
12f04 47 45 52 5f 52 45 53 45 52 56 45 44 20 29 3b 0a GER_RESERVED );.
12f05 20 20 72 63 20 3d 20 70 61 67 65 72 5f 77 61 69 rc = pager_wai
12f06 74 5f 6f 6e 5f 6c 6f 63 6b 28 70 50 61 67 65 72 t_on_lock(pPager
12f07 2c 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b , EXCLUSIVE_LOCK
12f08 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 );.. /* If the
12f09 66 69 6c 65 20 69 73 20 61 20 74 65 6d 70 2d 66 file is a temp-f
12f0a 69 6c 65 20 68 61 73 20 6e 6f 74 20 79 65 74 20 ile has not yet
12f0b 62 65 65 6e 20 6f 70 65 6e 65 64 2c 20 6f 70 65 been opened, ope
12f0c 6e 20 69 74 20 6e 6f 77 2e 20 49 74 0a 20 20 2a n it now. It. *
12f0d 2a 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c * is not possibl
12f0e 65 20 66 6f 72 20 72 63 20 74 6f 20 62 65 20 6f e for rc to be o
12f0f 74 68 65 72 20 74 68 61 6e 20 53 51 4c 49 54 45 ther than SQLITE
12f10 5f 4f 4b 20 69 66 20 74 68 69 73 20 62 72 61 6e _OK if this bran
12f11 63 68 0a 20 20 2a 2a 20 69 73 20 74 61 6b 65 6e ch. ** is taken
12f12 2c 20 61 73 20 70 61 67 65 72 5f 77 61 69 74 5f , as pager_wait_
12f13 6f 6e 5f 6c 6f 63 6b 28 29 20 69 73 20 61 20 6e on_lock() is a n
12f14 6f 2d 6f 70 20 66 6f 72 20 74 65 6d 70 2d 66 69 o-op for temp-fi
12f15 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 les.. */. if(
12f16 21 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e !isOpen(pPager->
12f17 66 64 29 20 29 7b 0a 20 20 20 20 61 73 73 65 72 fd) ){. asser
12f18 74 28 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 t( pPager->tempF
12f19 69 6c 65 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 ile && rc==SQLIT
12f1a 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 72 63 20 3d E_OK );. rc =
12f1b 20 70 61 67 65 72 4f 70 65 6e 74 65 6d 70 28 70 pagerOpentemp(p
12f1c 50 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 66 Pager, pPager->f
12f1d 64 2c 20 70 50 61 67 65 72 2d 3e 76 66 73 46 6c d, pPager->vfsFl
12f1e 61 67 73 29 3b 0a 20 20 7d 0a 0a 20 20 77 68 69 ags);. }.. whi
12f1f 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f le( rc==SQLITE_O
12f20 4b 20 26 26 20 70 4c 69 73 74 20 29 7b 0a 20 20 K && pList ){.
12f21 20 20 50 67 6e 6f 20 70 67 6e 6f 20 3d 20 70 4c Pgno pgno = pL
12f22 69 73 74 2d 3e 70 67 6e 6f 3b 0a 0a 20 20 20 20 ist->pgno;..
12f23 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20 /* If there are
12f24 64 69 72 74 79 20 70 61 67 65 73 20 69 6e 20 74 dirty pages in t
12f25 68 65 20 70 61 67 65 20 63 61 63 68 65 20 77 69 he page cache wi
12f26 74 68 20 70 61 67 65 20 6e 75 6d 62 65 72 73 20 th page numbers
12f27 67 72 65 61 74 65 72 0a 20 20 20 20 2a 2a 20 74 greater. ** t
12f28 68 61 6e 20 50 61 67 65 72 2e 64 62 53 69 7a 65 han Pager.dbSize
12f29 2c 20 74 68 69 73 20 6d 65 61 6e 73 20 73 71 6c , this means sql
12f2a 69 74 65 33 50 61 67 65 72 54 72 75 6e 63 61 74 ite3PagerTruncat
12f2b 65 49 6d 61 67 65 28 29 20 77 61 73 20 63 61 6c eImage() was cal
12f2c 6c 65 64 20 74 6f 0a 20 20 20 20 2a 2a 20 6d 61 led to. ** ma
12f2d 6b 65 20 74 68 65 20 66 69 6c 65 20 73 6d 61 6c ke the file smal
12f2e 6c 65 72 20 28 70 72 65 73 75 6d 61 62 6c 79 20 ler (presumably
12f2f 62 79 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 63 by auto-vacuum c
12f30 6f 64 65 29 2e 20 44 6f 20 6e 6f 74 20 77 72 69 ode). Do not wri
12f31 74 65 0a 20 20 20 20 2a 2a 20 61 6e 79 20 73 75 te. ** any su
12f32 63 68 20 70 61 67 65 73 20 74 6f 20 74 68 65 20 ch pages to the
12f33 66 69 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 file.. **.
12f34 20 2a 2a 20 41 6c 73 6f 2c 20 64 6f 20 6e 6f 74 ** Also, do not
12f35 20 77 72 69 74 65 20 6f 75 74 20 61 6e 79 20 70 write out any p
12f36 61 67 65 20 74 68 61 74 20 68 61 73 20 74 68 65 age that has the
12f37 20 50 47 48 44 52 5f 44 4f 4e 54 5f 57 52 49 54 PGHDR_DONT_WRIT
12f38 45 20 66 6c 61 67 0a 20 20 20 20 2a 2a 20 73 65 E flag. ** se
12f39 74 20 28 73 65 74 20 62 79 20 73 71 6c 69 74 65 t (set by sqlite
12f3a 33 50 61 67 65 72 44 6f 6e 74 57 72 69 74 65 28 3PagerDontWrite(
12f3b 29 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 )).. */. i
12f3c 66 28 20 70 67 6e 6f 3c 3d 70 50 61 67 65 72 2d f( pgno<=pPager-
12f3d 3e 64 62 53 69 7a 65 20 26 26 20 30 3d 3d 28 70 >dbSize && 0==(p
12f3e 4c 69 73 74 2d 3e 66 6c 61 67 73 26 50 47 48 44 List->flags&PGHD
12f3f 52 5f 44 4f 4e 54 5f 57 52 49 54 45 29 20 29 7b R_DONT_WRITE) ){
12f40 0a 20 20 20 20 20 20 69 36 34 20 6f 66 66 73 65 . i64 offse
12f41 74 20 3d 20 28 70 67 6e 6f 2d 31 29 2a 28 69 36 t = (pgno-1)*(i6
12f42 34 29 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 4)pPager->pageSi
12f43 7a 65 3b 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 ze; /* Offset
12f44 74 6f 20 77 72 69 74 65 20 2a 2f 0a 20 20 20 20 to write */.
12f45 20 20 63 68 61 72 20 2a 70 44 61 74 61 3b 20 20 char *pData;
12f46 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12f47 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12f48 20 2f 2a 20 44 61 74 61 20 74 6f 20 77 72 69 74 /* Data to writ
12f49 65 20 2a 2f 20 20 20 20 0a 0a 20 20 20 20 20 20 e */ ..
12f4a 2f 2a 20 45 6e 63 6f 64 65 20 74 68 65 20 64 61 /* Encode the da
12f4b 74 61 62 61 73 65 20 2a 2f 0a 20 20 20 20 20 20 tabase */.
12f4c 43 4f 44 45 43 32 28 70 50 61 67 65 72 2c 20 70 CODEC2(pPager, p
12f4d 4c 69 73 74 2d 3e 70 44 61 74 61 2c 20 70 67 6e List->pData, pgn
12f4e 6f 2c 20 36 2c 20 72 65 74 75 72 6e 20 53 51 4c o, 6, return SQL
12f4f 49 54 45 5f 4e 4f 4d 45 4d 2c 20 70 44 61 74 61 ITE_NOMEM, pData
12f50 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 57 72 69 );.. /* Wri
12f51 74 65 20 6f 75 74 20 74 68 65 20 70 61 67 65 20 te out the page
12f52 64 61 74 61 2e 20 2a 2f 0a 20 20 20 20 20 20 72 data. */. r
12f53 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 c = sqlite3OsWri
12f54 74 65 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 70 te(pPager->fd, p
12f55 44 61 74 61 2c 20 70 50 61 67 65 72 2d 3e 70 61 Data, pPager->pa
12f56 67 65 53 69 7a 65 2c 20 6f 66 66 73 65 74 29 3b geSize, offset);
12f57 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 70 61 .. /* If pa
12f58 67 65 20 31 20 77 61 73 20 6a 75 73 74 20 77 72 ge 1 was just wr
12f59 69 74 74 65 6e 2c 20 75 70 64 61 74 65 20 50 61 itten, update Pa
12f5a 67 65 72 2e 64 62 46 69 6c 65 56 65 72 73 20 74 ger.dbFileVers t
12f5b 6f 20 6d 61 74 63 68 0a 20 20 20 20 20 20 2a 2a o match. **
12f5c 20 74 68 65 20 76 61 6c 75 65 20 6e 6f 77 20 73 the value now s
12f5d 74 6f 72 65 64 20 69 6e 20 74 68 65 20 64 61 74 tored in the dat
12f5e 61 62 61 73 65 20 66 69 6c 65 2e 20 49 66 20 77 abase file. If w
12f5f 72 69 74 69 6e 67 20 74 68 69 73 20 0a 20 20 20 riting this .
12f60 20 20 20 2a 2a 20 70 61 67 65 20 63 61 75 73 65 ** page cause
12f61 64 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 d the database f
12f62 69 6c 65 20 74 6f 20 67 72 6f 77 2c 20 75 70 64 ile to grow, upd
12f63 61 74 65 20 64 62 46 69 6c 65 53 69 7a 65 2e 20 ate dbFileSize.
12f64 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
12f65 69 66 28 20 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 if( pgno==1 ){.
12f66 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 70 memcpy(&p
12f67 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 56 65 72 Pager->dbFileVer
12f68 73 2c 20 26 70 44 61 74 61 5b 32 34 5d 2c 20 73 s, &pData[24], s
12f69 69 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e 64 62 izeof(pPager->db
12f6a 46 69 6c 65 56 65 72 73 29 29 3b 0a 20 20 20 20 FileVers));.
12f6b 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70 67 }. if( pg
12f6c 6e 6f 3e 70 50 61 67 65 72 2d 3e 64 62 46 69 6c no>pPager->dbFil
12f6d 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 eSize ){.
12f6e 20 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 53 pPager->dbFileS
12f6f 69 7a 65 20 3d 20 70 67 6e 6f 3b 0a 20 20 20 20 ize = pgno;.
12f70 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 55 70 }.. /* Up
12f71 64 61 74 65 20 61 6e 79 20 62 61 63 6b 75 70 20 date any backup
12f72 6f 62 6a 65 63 74 73 20 63 6f 70 79 69 6e 67 20 objects copying
12f73 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 the contents of
12f74 74 68 69 73 20 70 61 67 65 72 2e 20 2a 2f 0a 20 this pager. */.
12f75 20 20 20 20 20 73 71 6c 69 74 65 33 42 61 63 6b sqlite3Back
12f76 75 70 55 70 64 61 74 65 28 70 50 61 67 65 72 2d upUpdate(pPager-
12f77 3e 70 42 61 63 6b 75 70 2c 20 70 67 6e 6f 2c 20 >pBackup, pgno,
12f78 28 75 38 2a 29 70 4c 69 73 74 2d 3e 70 44 61 74 (u8*)pList->pDat
12f79 61 29 3b 0a 0a 20 20 20 20 20 20 50 41 47 45 52 a);.. PAGER
12f7a 54 52 41 43 45 28 28 22 53 54 4f 52 45 20 25 64 TRACE(("STORE %d
12f7b 20 70 61 67 65 20 25 64 20 68 61 73 68 28 25 30 page %d hash(%0
12f7c 38 78 29 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 8x)\n",.
12f7d 20 20 20 20 20 20 20 20 20 20 20 50 41 47 45 52 PAGER
12f7e 49 44 28 70 50 61 67 65 72 29 2c 20 70 67 6e 6f ID(pPager), pgno
12f7f 2c 20 70 61 67 65 72 5f 70 61 67 65 68 61 73 68 , pager_pagehash
12f80 28 70 4c 69 73 74 29 29 29 3b 0a 20 20 20 20 20 (pList)));.
12f81 20 49 4f 54 52 41 43 45 28 28 22 50 47 4f 55 54 IOTRACE(("PGOUT
12f82 20 25 70 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 %p %d\n", pPage
12f83 72 2c 20 70 67 6e 6f 29 29 3b 0a 20 20 20 20 20 r, pgno));.
12f84 20 50 41 47 45 52 5f 49 4e 43 52 28 73 71 6c 69 PAGER_INCR(sqli
12f85 74 65 33 5f 70 61 67 65 72 5f 77 72 69 74 65 64 te3_pager_writed
12f86 62 5f 63 6f 75 6e 74 29 3b 0a 20 20 20 20 20 20 b_count);.
12f87 50 41 47 45 52 5f 49 4e 43 52 28 70 50 61 67 65 PAGER_INCR(pPage
12f88 72 2d 3e 6e 57 72 69 74 65 29 3b 0a 20 20 20 20 r->nWrite);.
12f89 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 50 41 47 }else{. PAG
12f8a 45 52 54 52 41 43 45 28 28 22 4e 4f 53 54 4f 52 ERTRACE(("NOSTOR
12f8b 45 20 25 64 20 70 61 67 65 20 25 64 5c 6e 22 2c E %d page %d\n",
12f8c 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72 29 PAGERID(pPager)
12f8d 2c 20 70 67 6e 6f 29 29 3b 0a 20 20 20 20 7d 0a , pgno));. }.
12f8e 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43 48 #ifdef SQLITE_CH
12f8f 45 43 4b 5f 50 41 47 45 53 0a 20 20 20 20 70 4c ECK_PAGES. pL
12f90 69 73 74 2d 3e 70 61 67 65 48 61 73 68 20 3d 20 ist->pageHash =
12f91 70 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 70 pager_pagehash(p
12f92 4c 69 73 74 29 3b 0a 23 65 6e 64 69 66 0a 20 20 List);.#endif.
12f93 20 20 70 4c 69 73 74 20 3d 20 70 4c 69 73 74 2d pList = pList-
12f94 3e 70 44 69 72 74 79 3b 0a 20 20 7d 0a 0a 20 20 >pDirty;. }..
12f95 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
12f96 0a 2a 2a 20 41 70 70 65 6e 64 20 61 20 72 65 63 .** Append a rec
12f97 6f 72 64 20 6f 66 20 74 68 65 20 63 75 72 72 65 ord of the curre
12f98 6e 74 20 73 74 61 74 65 20 6f 66 20 70 61 67 65 nt state of page
12f99 20 70 50 67 20 74 6f 20 74 68 65 20 73 75 62 2d pPg to the sub-
12f9a 6a 6f 75 72 6e 61 6c 2e 20 0a 2a 2a 20 49 74 20 journal. .** It
12f9b 69 73 20 74 68 65 20 63 61 6c 6c 65 72 73 20 72 is the callers r
12f9c 65 73 70 6f 6e 73 69 62 69 6c 69 74 79 20 74 6f esponsibility to
12f9d 20 75 73 65 20 73 75 62 6a 52 65 71 75 69 72 65 use subjRequire
12f9e 73 50 61 67 65 28 29 20 74 6f 20 63 68 65 63 6b sPage() to check
12f9f 20 0a 2a 2a 20 74 68 61 74 20 69 74 20 69 73 20 .** that it is
12fa0 72 65 61 6c 6c 79 20 72 65 71 75 69 72 65 64 20 really required
12fa1 62 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 74 before calling t
12fa2 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a his function..**
12fa3 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66 75 .** If successfu
12fa4 6c 2c 20 73 65 74 20 74 68 65 20 62 69 74 20 63 l, set the bit c
12fa5 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 orresponding to
12fa6 70 50 67 2d 3e 70 67 6e 6f 20 69 6e 20 74 68 65 pPg->pgno in the
12fa7 20 62 69 74 76 65 63 73 0a 2a 2a 20 66 6f 72 20 bitvecs.** for
12fa8 61 6c 6c 20 6f 70 65 6e 20 73 61 76 65 70 6f 69 all open savepoi
12fa9 6e 74 73 20 62 65 66 6f 72 65 20 72 65 74 75 72 nts before retur
12faa 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 ning..**.** This
12fab 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e function return
12fac 73 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 65 s SQLITE_OK if e
12fad 76 65 72 79 74 68 69 6e 67 20 69 73 20 73 75 63 verything is suc
12fae 63 65 73 73 66 75 6c 2c 20 61 6e 20 49 4f 0a 2a cessful, an IO.*
12faf 2a 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66 20 * error code if
12fb0 74 68 65 20 61 74 74 65 6d 70 74 20 74 6f 20 77 the attempt to w
12fb1 72 69 74 65 20 74 6f 20 74 68 65 20 73 75 62 2d rite to the sub-
12fb2 6a 6f 75 72 6e 61 6c 20 66 61 69 6c 73 2c 20 6f journal fails, o
12fb3 72 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d r .** SQLITE_NOM
12fb4 45 4d 20 69 66 20 61 20 6d 61 6c 6c 6f 63 20 66 EM if a malloc f
12fb5 61 69 6c 73 20 77 68 69 6c 65 20 73 65 74 74 69 ails while setti
12fb6 6e 67 20 61 20 62 69 74 20 69 6e 20 61 20 73 61 ng a bit in a sa
12fb7 76 65 70 6f 69 6e 74 0a 2a 2a 20 62 69 74 76 65 vepoint.** bitve
12fb8 63 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 c..*/.static int
12fb9 20 73 75 62 6a 6f 75 72 6e 61 6c 50 61 67 65 28 subjournalPage(
12fba 50 67 48 64 72 20 2a 70 50 67 29 7b 0a 20 20 69 PgHdr *pPg){. i
12fbb 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
12fbc 4b 3b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 K;. Pager *pPag
12fbd 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 72 er = pPg->pPager
12fbe 3b 0a 20 20 69 66 28 20 69 73 4f 70 65 6e 28 70 ;. if( isOpen(p
12fbf 50 61 67 65 72 2d 3e 73 6a 66 64 29 20 29 7b 0a Pager->sjfd) ){.
12fc0 20 20 20 20 76 6f 69 64 20 2a 70 44 61 74 61 20 void *pData
12fc1 3d 20 70 50 67 2d 3e 70 44 61 74 61 3b 0a 20 20 = pPg->pData;.
12fc2 20 20 69 36 34 20 6f 66 66 73 65 74 20 3d 20 70 i64 offset = p
12fc3 50 61 67 65 72 2d 3e 6e 53 75 62 52 65 63 2a 28 Pager->nSubRec*(
12fc4 34 2b 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 4+pPager->pageSi
12fc5 7a 65 29 3b 0a 20 20 20 20 63 68 61 72 20 2a 70 ze);. char *p
12fc6 44 61 74 61 32 3b 0a 0a 20 20 20 20 43 4f 44 45 Data2;.. CODE
12fc7 43 32 28 70 50 61 67 65 72 2c 20 70 44 61 74 61 C2(pPager, pData
12fc8 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 37 2c 20 , pPg->pgno, 7,
12fc9 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f return SQLITE_NO
12fca 4d 45 4d 2c 20 70 44 61 74 61 32 29 3b 0a 20 20 MEM, pData2);.
12fcb 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22 53 PAGERTRACE(("S
12fcc 54 4d 54 2d 4a 4f 55 52 4e 41 4c 20 25 64 20 70 TMT-JOURNAL %d p
12fcd 61 67 65 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 age %d\n", PAGER
12fce 49 44 28 70 50 61 67 65 72 29 2c 20 70 50 67 2d ID(pPager), pPg-
12fcf 3e 70 67 6e 6f 29 29 3b 0a 20 20 0a 20 20 20 20 >pgno));. .
12fd0 61 73 73 65 72 74 28 20 70 61 67 65 49 6e 4a 6f assert( pageInJo
12fd1 75 72 6e 61 6c 28 70 50 67 29 20 7c 7c 20 70 50 urnal(pPg) || pP
12fd2 67 2d 3e 70 67 6e 6f 3e 70 50 61 67 65 72 2d 3e g->pgno>pPager->
12fd3 64 62 4f 72 69 67 53 69 7a 65 20 29 3b 0a 20 20 dbOrigSize );.
12fd4 20 20 72 63 20 3d 20 77 72 69 74 65 33 32 62 69 rc = write32bi
12fd5 74 73 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 2c ts(pPager->sjfd,
12fd6 20 6f 66 66 73 65 74 2c 20 70 50 67 2d 3e 70 67 offset, pPg->pg
12fd7 6e 6f 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d no);. if( rc=
12fd8 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
12fd9 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
12fda 4f 73 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e OsWrite(pPager->
12fdb 73 6a 66 64 2c 20 70 44 61 74 61 32 2c 20 70 50 sjfd, pData2, pP
12fdc 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 ager->pageSize,
12fdd 6f 66 66 73 65 74 2b 34 29 3b 0a 20 20 20 20 7d offset+4);. }
12fde 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 . }. if( rc==S
12fdf 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
12fe0 70 50 61 67 65 72 2d 3e 6e 53 75 62 52 65 63 2b pPager->nSubRec+
12fe1 2b 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 +;. assert( p
12fe2 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e Pager->nSavepoin
12fe3 74 3e 30 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 t>0 );. rc =
12fe4 61 64 64 54 6f 53 61 76 65 70 6f 69 6e 74 42 69 addToSavepointBi
12fe5 74 76 65 63 73 28 70 50 61 67 65 72 2c 20 70 50 tvecs(pPager, pP
12fe6 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20 7d 0a 20 20 g->pgno);. }.
12fe7 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f return rc;.}.../
12fe8 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 *.** This functi
12fe9 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 on is called by
12fea 74 68 65 20 70 63 61 63 68 65 20 6c 61 79 65 72 the pcache layer
12feb 20 77 68 65 6e 20 69 74 20 68 61 73 20 72 65 61 when it has rea
12fec 63 68 65 64 20 73 6f 6d 65 0a 2a 2a 20 73 6f 66 ched some.** sof
12fed 74 20 6d 65 6d 6f 72 79 20 6c 69 6d 69 74 2e 20 t memory limit.
12fee 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 The first argume
12fef 6e 74 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 nt is a pointer
12ff0 74 6f 20 61 20 50 61 67 65 72 20 6f 62 6a 65 63 to a Pager objec
12ff1 74 0a 2a 2a 20 28 63 61 73 74 20 61 73 20 61 20 t.** (cast as a
12ff2 76 6f 69 64 2a 29 2e 20 54 68 65 20 70 61 67 65 void*). The page
12ff3 72 20 69 73 20 61 6c 77 61 79 73 20 27 70 75 72 r is always 'pur
12ff4 67 65 61 62 6c 65 27 20 28 6e 6f 74 20 61 6e 20 geable' (not an
12ff5 69 6e 2d 6d 65 6d 6f 72 79 0a 2a 2a 20 64 61 74 in-memory.** dat
12ff6 61 62 61 73 65 29 2e 20 54 68 65 20 73 65 63 6f abase). The seco
12ff7 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 nd argument is a
12ff8 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 61 20 reference to a
12ff9 70 61 67 65 20 74 68 61 74 20 69 73 20 0a 2a 2a page that is .**
12ffa 20 63 75 72 72 65 6e 74 6c 79 20 64 69 72 74 79 currently dirty
12ffb 20 62 75 74 20 68 61 73 20 6e 6f 20 6f 75 74 73 but has no outs
12ffc 74 61 6e 64 69 6e 67 20 72 65 66 65 72 65 6e 63 tanding referenc
12ffd 65 73 2e 20 54 68 65 20 70 61 67 65 0a 2a 2a 20 es. The page.**
12ffe 69 73 20 61 6c 77 61 79 73 20 61 73 73 6f 63 69 is always associ
12fff 61 74 65 64 20 77 69 74 68 20 74 68 65 20 50 61 ated with the Pa
13000 67 65 72 20 6f 62 6a 65 63 74 20 70 61 73 73 65 ger object passe
13001 64 20 61 73 20 74 68 65 20 66 69 72 73 74 20 0a d as the first .
13002 2a 2a 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a ** argument..**.
13003 2a 2a 20 54 68 65 20 6a 6f 62 20 6f 66 20 74 68 ** The job of th
13004 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 74 is function is t
13005 6f 20 6d 61 6b 65 20 70 50 67 20 63 6c 65 61 6e o make pPg clean
13006 20 62 79 20 77 72 69 74 69 6e 67 20 69 74 73 20 by writing its
13007 63 6f 6e 74 65 6e 74 73 0a 2a 2a 20 6f 75 74 20 contents.** out
13008 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 to the database
13009 66 69 6c 65 2c 20 69 66 20 70 6f 73 73 69 62 6c file, if possibl
1300a 65 2e 20 54 68 69 73 20 6d 61 79 20 69 6e 76 6f e. This may invo
1300b 6c 76 65 20 73 79 6e 63 69 6e 67 20 74 68 65 0a lve syncing the.
1300c 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e ** journal file.
1300d 20 0a 2a 2a 0a 2a 2a 20 49 66 20 73 75 63 63 65 .**.** If succe
1300e 73 73 66 75 6c 2c 20 73 71 6c 69 74 65 33 50 63 ssful, sqlite3Pc
1300f 61 63 68 65 4d 61 6b 65 43 6c 65 61 6e 28 29 20 acheMakeClean()
13010 69 73 20 63 61 6c 6c 65 64 20 6f 6e 20 74 68 65 is called on the
13011 20 70 61 67 65 20 61 6e 64 0a 2a 2a 20 53 51 4c page and.** SQL
13012 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64 2e ITE_OK returned.
13013 20 49 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 If an IO error
13014 6f 63 63 75 72 73 20 77 68 69 6c 65 20 74 72 79 occurs while try
13015 69 6e 67 20 74 6f 20 6d 61 6b 65 20 74 68 65 0a ing to make the.
13016 2a 2a 20 70 61 67 65 20 63 6c 65 61 6e 2c 20 74 ** page clean, t
13017 68 65 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 he IO error code
13018 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49 66 is returned. If
13019 20 74 68 65 20 70 61 67 65 20 63 61 6e 6e 6f 74 the page cannot
1301a 20 62 65 0a 2a 2a 20 6d 61 64 65 20 63 6c 65 61 be.** made clea
1301b 6e 20 66 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 n for some other
1301c 20 72 65 61 73 6f 6e 2c 20 62 75 74 20 6e 6f 20 reason, but no
1301d 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 74 68 error occurs, th
1301e 65 6e 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 en SQLITE_OK.**
1301f 69 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 is returned by s
13020 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 qlite3PcacheMake
13021 43 6c 65 61 6e 28 29 20 69 73 20 6e 6f 74 20 63 Clean() is not c
13022 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 alled..*/.static
13023 20 69 6e 74 20 70 61 67 65 72 53 74 72 65 73 73 int pagerStress
13024 28 76 6f 69 64 20 2a 70 2c 20 50 67 48 64 72 20 (void *p, PgHdr
13025 2a 70 50 67 29 7b 0a 20 20 50 61 67 65 72 20 2a *pPg){. Pager *
13026 70 50 61 67 65 72 20 3d 20 28 50 61 67 65 72 20 pPager = (Pager
13027 2a 29 70 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 *)p;. int rc =
13028 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 61 73 SQLITE_OK;.. as
13029 73 65 72 74 28 20 70 50 67 2d 3e 70 50 61 67 65 sert( pPg->pPage
1302a 72 3d 3d 70 50 61 67 65 72 20 29 3b 0a 20 20 61 r==pPager );. a
1302b 73 73 65 72 74 28 20 70 50 67 2d 3e 66 6c 61 67 ssert( pPg->flag
1302c 73 26 50 47 48 44 52 5f 44 49 52 54 59 20 29 3b s&PGHDR_DIRTY );
1302d 0a 0a 20 20 2f 2a 20 54 68 65 20 64 6f 4e 6f 74 .. /* The doNot
1302e 53 79 6e 63 20 66 6c 61 67 20 69 73 20 73 65 74 Sync flag is set
1302f 20 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 50 by the sqlite3P
13030 61 67 65 72 57 72 69 74 65 28 29 20 66 75 6e 63 agerWrite() func
13031 74 69 6f 6e 20 77 68 69 6c 65 20 69 74 0a 20 20 tion while it.
13032 2a 2a 20 69 73 20 6a 6f 75 72 6e 61 6c 6c 69 6e ** is journallin
13033 67 20 61 20 73 65 74 20 6f 66 20 74 77 6f 20 6f g a set of two o
13034 72 20 6d 6f 72 65 20 64 61 74 61 62 61 73 65 20 r more database
13035 70 61 67 65 73 20 74 68 61 74 20 61 72 65 20 73 pages that are s
13036 74 6f 72 65 64 0a 20 20 2a 2a 20 6f 6e 20 74 68 tored. ** on th
13037 65 20 73 61 6d 65 20 64 69 73 6b 20 73 65 63 74 e same disk sect
13038 6f 72 2e 20 53 79 6e 63 69 6e 67 20 74 68 65 20 or. Syncing the
13039 6a 6f 75 72 6e 61 6c 20 69 73 20 6e 6f 74 20 61 journal is not a
1303a 6c 6c 6f 77 65 64 20 77 68 69 6c 65 0a 20 20 2a llowed while. *
1303b 2a 20 74 68 69 73 20 69 73 20 68 61 70 70 65 6e * this is happen
1303c 69 6e 67 20 61 73 20 69 74 20 69 73 20 69 6d 70 ing as it is imp
1303d 6f 72 74 61 6e 74 20 74 68 61 74 20 61 6c 6c 20 ortant that all
1303e 6d 65 6d 62 65 72 73 20 6f 66 20 73 75 63 68 20 members of such
1303f 61 0a 20 20 2a 2a 20 73 65 74 20 6f 66 20 70 61 a. ** set of pa
13040 67 65 73 20 61 72 65 20 73 79 6e 63 65 64 20 74 ges are synced t
13041 6f 20 64 69 73 6b 20 74 6f 67 65 74 68 65 72 2e o disk together.
13042 20 53 6f 2c 20 69 66 20 74 68 65 20 70 61 67 65 So, if the page
13043 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 20 this function.
13044 20 2a 2a 20 69 73 20 74 72 79 69 6e 67 20 74 6f ** is trying to
13045 20 6d 61 6b 65 20 63 6c 65 61 6e 20 77 69 6c 6c make clean will
13046 20 72 65 71 75 69 72 65 20 61 20 6a 6f 75 72 6e require a journ
13047 61 6c 20 73 79 6e 63 20 61 6e 64 20 74 68 65 20 al sync and the
13048 64 6f 4e 6f 74 53 79 6e 63 0a 20 20 2a 2a 20 66 doNotSync. ** f
13049 6c 61 67 20 69 73 20 73 65 74 2c 20 72 65 74 75 lag is set, retu
1304a 72 6e 20 77 69 74 68 6f 75 74 20 64 6f 69 6e 67 rn without doing
1304b 20 61 6e 79 74 68 69 6e 67 2e 20 54 68 65 20 70 anything. The p
1304c 63 61 63 68 65 20 6c 61 79 65 72 20 77 69 6c 6c cache layer will
1304d 0a 20 20 2a 2a 20 6a 75 73 74 20 68 61 76 65 20 . ** just have
1304e 74 6f 20 67 6f 20 61 68 65 61 64 20 61 6e 64 20 to go ahead and
1304f 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 70 allocate a new p
13050 61 67 65 20 62 75 66 66 65 72 20 69 6e 73 74 65 age buffer inste
13051 61 64 20 6f 66 0a 20 20 2a 2a 20 72 65 75 73 69 ad of. ** reusi
13052 6e 67 20 70 50 67 2e 0a 20 20 2a 2a 0a 20 20 2a ng pPg.. **. *
13053 2a 20 53 69 6d 69 6c 61 72 6c 79 2c 20 69 66 20 * Similarly, if
13054 74 68 65 20 70 61 67 65 72 20 68 61 73 20 61 6c the pager has al
13055 72 65 61 64 79 20 65 6e 74 65 72 65 64 20 74 68 ready entered th
13056 65 20 65 72 72 6f 72 20 73 74 61 74 65 2c 20 64 e error state, d
13057 6f 20 6e 6f 74 0a 20 20 2a 2a 20 74 72 79 20 74 o not. ** try t
13058 6f 20 77 72 69 74 65 20 74 68 65 20 63 6f 6e 74 o write the cont
13059 65 6e 74 73 20 6f 66 20 70 50 67 20 74 6f 20 64 ents of pPg to d
1305a 69 73 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 isk.. */. if(
1305b 4e 45 56 45 52 28 70 50 61 67 65 72 2d 3e 65 72 NEVER(pPager->er
1305c 72 43 6f 64 65 29 0a 20 20 20 7c 7c 20 28 70 50 rCode). || (pP
1305d 61 67 65 72 2d 3e 64 6f 4e 6f 74 53 79 6e 63 20 ager->doNotSync
1305e 26 26 20 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 && pPg->flags&PG
1305f 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 0a 20 HDR_NEED_SYNC).
13060 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
13061 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 QLITE_OK;. }..
13062 20 2f 2a 20 53 79 6e 63 20 74 68 65 20 6a 6f 75 /* Sync the jou
13063 72 6e 61 6c 20 66 69 6c 65 20 69 66 20 72 65 71 rnal file if req
13064 75 69 72 65 64 2e 20 2a 2f 0a 20 20 69 66 28 20 uired. */. if(
13065 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 pPg->flags&PGHDR
13066 5f 4e 45 45 44 5f 53 59 4e 43 20 29 7b 0a 20 20 _NEED_SYNC ){.
13067 20 20 72 63 20 3d 20 73 79 6e 63 4a 6f 75 72 6e rc = syncJourn
13068 61 6c 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 al(pPager);.
13069 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
1306a 4b 20 26 26 20 70 50 61 67 65 72 2d 3e 66 75 6c K && pPager->ful
1306b 6c 53 79 6e 63 20 26 26 20 0a 20 20 20 20 20 20 lSync && .
1306c 21 28 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 !(pPager->journa
1306d 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 lMode==PAGER_JOU
1306e 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 29 RNALMODE_MEMORY)
1306f 20 26 26 0a 20 20 20 20 20 20 21 28 73 71 6c 69 &&. !(sqli
13070 74 65 33 4f 73 44 65 76 69 63 65 43 68 61 72 61 te3OsDeviceChara
13071 63 74 65 72 69 73 74 69 63 73 28 70 50 61 67 65 cteristics(pPage
13072 72 2d 3e 66 64 29 26 53 51 4c 49 54 45 5f 49 4f r->fd)&SQLITE_IO
13073 43 41 50 5f 53 41 46 45 5f 41 50 50 45 4e 44 29 CAP_SAFE_APPEND)
13074 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 70 50 . ){. pP
13075 61 67 65 72 2d 3e 6e 52 65 63 20 3d 20 30 3b 0a ager->nRec = 0;.
13076 20 20 20 20 20 20 72 63 20 3d 20 77 72 69 74 65 rc = write
13077 4a 6f 75 72 6e 61 6c 48 64 72 28 70 50 61 67 65 JournalHdr(pPage
13078 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 r);. }. }..
13079 20 2f 2a 20 49 66 20 74 68 65 20 70 61 67 65 20 /* If the page
1307a 6e 75 6d 62 65 72 20 6f 66 20 74 68 69 73 20 70 number of this p
1307b 61 67 65 20 69 73 20 6c 61 72 67 65 72 20 74 68 age is larger th
1307c 61 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 an the current s
1307d 69 7a 65 20 6f 66 0a 20 20 2a 2a 20 74 68 65 20 ize of. ** the
1307e 64 61 74 61 62 61 73 65 20 69 6d 61 67 65 2c 20 database image,
1307f 69 74 20 6d 61 79 20 6e 65 65 64 20 74 6f 20 62 it may need to b
13080 65 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 e written to the
13081 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 2e 0a 20 20 sub-journal..
13082 2a 2a 20 54 68 69 73 20 69 73 20 62 65 63 61 75 ** This is becau
13083 73 65 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 70 se the call to p
13084 61 67 65 72 5f 77 72 69 74 65 5f 70 61 67 65 6c ager_write_pagel
13085 69 73 74 28 29 20 62 65 6c 6f 77 20 77 69 6c 6c ist() below will
13086 20 6e 6f 74 0a 20 20 2a 2a 20 61 63 74 75 61 6c not. ** actual
13087 6c 79 20 77 72 69 74 65 20 64 61 74 61 20 74 6f ly write data to
13088 20 74 68 65 20 66 69 6c 65 20 69 6e 20 74 68 69 the file in thi
13089 73 20 63 61 73 65 2e 0a 20 20 2a 2a 0a 20 20 2a s case.. **. *
1308a 2a 20 43 6f 6e 73 69 64 65 72 20 74 68 65 20 66 * Consider the f
1308b 6f 6c 6c 6f 77 69 6e 67 20 73 65 71 75 65 6e 63 ollowing sequenc
1308c 65 20 6f 66 20 65 76 65 6e 74 73 3a 0a 20 20 2a e of events:. *
1308d 2a 0a 20 20 2a 2a 20 20 20 42 45 47 49 4e 3b 0a *. ** BEGIN;.
1308e 20 20 2a 2a 20 20 20 20 20 3c 6a 6f 75 72 6e 61 ** <journa
1308f 6c 20 70 61 67 65 20 58 3e 0a 20 20 2a 2a 20 20 l page X>. **
13090 20 20 20 3c 6d 6f 64 69 66 79 20 70 61 67 65 20 <modify page
13091 58 3e 0a 20 20 2a 2a 20 20 20 20 20 53 41 56 45 X>. ** SAVE
13092 50 4f 49 4e 54 20 73 70 3b 0a 20 20 2a 2a 20 20 POINT sp;. **
13093 20 20 20 20 20 3c 73 68 72 69 6e 6b 20 64 61 74 <shrink dat
13094 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20 59 20 abase file to Y
13095 70 61 67 65 73 3e 0a 20 20 2a 2a 20 20 20 20 20 pages>. **
13096 20 20 70 61 67 65 72 53 74 72 65 73 73 28 70 61 pagerStress(pa
13097 67 65 20 58 29 0a 20 20 2a 2a 20 20 20 20 20 52 ge X). ** R
13098 4f 4c 4c 42 41 43 4b 20 54 4f 20 73 70 3b 0a 20 OLLBACK TO sp;.
13099 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 28 58 3e 59 **. ** If (X>Y
1309a 29 2c 20 74 68 65 6e 20 77 68 65 6e 20 70 61 67 ), then when pag
1309b 65 72 53 74 72 65 73 73 20 69 73 20 63 61 6c 6c erStress is call
1309c 65 64 20 70 61 67 65 20 58 20 77 69 6c 6c 20 6e ed page X will n
1309d 6f 74 20 62 65 20 77 72 69 74 74 65 6e 0a 20 20 ot be written.
1309e 2a 2a 20 6f 75 74 20 74 6f 20 74 68 65 20 64 61 ** out to the da
1309f 74 61 62 61 73 65 20 66 69 6c 65 2c 20 62 75 74 tabase file, but
130a0 20 77 69 6c 6c 20 62 65 20 64 72 6f 70 70 65 64 will be dropped
130a1 20 66 72 6f 6d 20 74 68 65 20 63 61 63 68 65 2e from the cache.
130a2 20 54 68 65 6e 2c 0a 20 20 2a 2a 20 66 6f 6c 6c Then,. ** foll
130a3 6f 77 69 6e 67 20 74 68 65 20 22 52 4f 4c 4c 42 owing the "ROLLB
130a4 41 43 4b 20 54 4f 20 73 70 22 20 73 74 61 74 65 ACK TO sp" state
130a5 6d 65 6e 74 2c 20 72 65 61 64 69 6e 67 20 70 61 ment, reading pa
130a6 67 65 20 58 20 77 69 6c 6c 20 72 65 61 64 0a 20 ge X will read.
130a7 20 2a 2a 20 64 61 74 61 20 66 72 6f 6d 20 74 68 ** data from th
130a8 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e e database file.
130a9 20 54 68 69 73 20 77 69 6c 6c 20 62 65 20 74 68 This will be th
130aa 65 20 63 6f 70 79 20 6f 66 20 70 61 67 65 20 58 e copy of page X
130ab 20 61 73 20 69 74 0a 20 20 2a 2a 20 77 61 73 20 as it. ** was
130ac 77 68 65 6e 20 74 68 65 20 74 72 61 6e 73 61 63 when the transac
130ad 74 69 6f 6e 20 73 74 61 72 74 65 64 2c 20 6e 6f tion started, no
130ae 74 20 61 73 20 69 74 20 77 61 73 20 77 68 65 6e t as it was when
130af 20 22 53 41 56 45 50 4f 49 4e 54 20 73 70 22 0a "SAVEPOINT sp".
130b0 20 20 2a 2a 20 77 61 73 20 65 78 65 63 75 74 65 ** was execute
130b1 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 d.. **. ** The
130b2 20 73 6f 6c 75 74 69 6f 6e 20 69 73 20 74 6f 20 solution is to
130b3 77 72 69 74 65 20 74 68 65 20 63 75 72 72 65 6e write the curren
130b4 74 20 64 61 74 61 20 66 6f 72 20 70 61 67 65 20 t data for page
130b5 58 20 69 6e 74 6f 20 74 68 65 20 0a 20 20 2a 2a X into the . **
130b6 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c sub-journal fil
130b7 65 20 6e 6f 77 20 28 69 66 20 69 74 20 69 73 20 e now (if it is
130b8 6e 6f 74 20 61 6c 72 65 61 64 79 20 74 68 65 72 not already ther
130b9 65 29 2c 20 73 6f 20 74 68 61 74 20 69 74 20 77 e), so that it w
130ba 69 6c 6c 0a 20 20 2a 2a 20 62 65 20 72 65 73 74 ill. ** be rest
130bb 6f 72 65 64 20 74 6f 20 69 74 73 20 63 75 72 72 ored to its curr
130bc 65 6e 74 20 76 61 6c 75 65 20 77 68 65 6e 20 74 ent value when t
130bd 68 65 20 22 52 4f 4c 4c 42 41 43 4b 20 54 4f 20 he "ROLLBACK TO
130be 73 70 22 20 69 73 20 0a 20 20 2a 2a 20 65 78 65 sp" is . ** exe
130bf 63 75 74 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 cuted.. */. if
130c0 28 20 4e 45 56 45 52 28 0a 20 20 20 20 20 20 72 ( NEVER(. r
130c1 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c==SQLITE_OK &&
130c2 70 50 67 2d 3e 70 67 6e 6f 3e 70 50 61 67 65 72 pPg->pgno>pPager
130c3 2d 3e 64 62 53 69 7a 65 20 26 26 20 73 75 62 6a ->dbSize && subj
130c4 52 65 71 75 69 72 65 73 50 61 67 65 28 70 50 67 RequiresPage(pPg
130c5 29 0a 20 20 29 20 29 7b 0a 20 20 20 20 72 63 20 ). ) ){. rc
130c6 3d 20 73 75 62 6a 6f 75 72 6e 61 6c 50 61 67 65 = subjournalPage
130c7 28 70 50 67 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a (pPg);. }.. /*
130c8 20 57 72 69 74 65 20 74 68 65 20 63 6f 6e 74 65 Write the conte
130c9 6e 74 73 20 6f 66 20 74 68 65 20 70 61 67 65 20 nts of the page
130ca 6f 75 74 20 74 6f 20 74 68 65 20 64 61 74 61 62 out to the datab
130cb 61 73 65 20 66 69 6c 65 2e 20 2a 2f 0a 20 20 69 ase file. */. i
130cc 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
130cd 20 29 7b 0a 20 20 20 20 70 50 67 2d 3e 70 44 69 ){. pPg->pDi
130ce 72 74 79 20 3d 20 30 3b 0a 20 20 20 20 72 63 20 rty = 0;. rc
130cf 3d 20 70 61 67 65 72 5f 77 72 69 74 65 5f 70 61 = pager_write_pa
130d0 67 65 6c 69 73 74 28 70 50 67 29 3b 0a 20 20 7d gelist(pPg);. }
130d1 0a 0a 20 20 2f 2a 20 4d 61 72 6b 20 74 68 65 20 .. /* Mark the
130d2 70 61 67 65 20 61 73 20 63 6c 65 61 6e 2e 20 2a page as clean. *
130d3 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 /. if( rc==SQLI
130d4 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 50 41 47 TE_OK ){. PAG
130d5 45 52 54 52 41 43 45 28 28 22 53 54 52 45 53 53 ERTRACE(("STRESS
130d6 20 25 64 20 70 61 67 65 20 25 64 5c 6e 22 2c 20 %d page %d\n",
130d7 50 41 47 45 52 49 44 28 70 50 61 67 65 72 29 2c PAGERID(pPager),
130d8 20 70 50 67 2d 3e 70 67 6e 6f 29 29 3b 0a 20 20 pPg->pgno));.
130d9 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d sqlite3PcacheM
130da 61 6b 65 43 6c 65 61 6e 28 70 50 67 29 3b 0a 20 akeClean(pPg);.
130db 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 70 61 67 }.. return pag
130dc 65 72 5f 65 72 72 6f 72 28 70 50 61 67 65 72 2c er_error(pPager,
130dd 20 72 63 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 rc);.}.../*.**
130de 41 6c 6c 6f 63 61 74 65 20 61 6e 64 20 69 6e 69 Allocate and ini
130df 74 69 61 6c 69 7a 65 20 61 20 6e 65 77 20 50 61 tialize a new Pa
130e0 67 65 72 20 6f 62 6a 65 63 74 20 61 6e 64 20 70 ger object and p
130e1 75 74 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 ut a pointer to
130e2 69 74 0a 2a 2a 20 69 6e 20 2a 70 70 50 61 67 65 it.** in *ppPage
130e3 72 2e 20 54 68 65 20 70 61 67 65 72 20 73 68 6f r. The pager sho
130e4 75 6c 64 20 65 76 65 6e 74 75 61 6c 6c 79 20 62 uld eventually b
130e5 65 20 66 72 65 65 64 20 62 79 20 70 61 73 73 69 e freed by passi
130e6 6e 67 20 69 74 0a 2a 2a 20 74 6f 20 73 71 6c 69 ng it.** to sqli
130e7 74 65 33 50 61 67 65 72 43 6c 6f 73 65 28 29 2e te3PagerClose().
130e8 0a 2a 2a 0a 2a 2a 20 54 68 65 20 7a 46 69 6c 65 .**.** The zFile
130e9 6e 61 6d 65 20 61 72 67 75 6d 65 6e 74 20 69 73 name argument is
130ea 20 74 68 65 20 70 61 74 68 20 74 6f 20 74 68 65 the path to the
130eb 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 74 database file t
130ec 6f 20 6f 70 65 6e 2e 0a 2a 2a 20 49 66 20 7a 46 o open..** If zF
130ed 69 6c 65 6e 61 6d 65 20 69 73 20 4e 55 4c 4c 20 ilename is NULL
130ee 74 68 65 6e 20 61 20 72 61 6e 64 6f 6d 6c 79 2d then a randomly-
130ef 6e 61 6d 65 64 20 74 65 6d 70 6f 72 61 72 79 20 named temporary
130f0 66 69 6c 65 20 69 73 20 63 72 65 61 74 65 64 0a file is created.
130f1 2a 2a 20 61 6e 64 20 75 73 65 64 20 61 73 20 74 ** and used as t
130f2 68 65 20 66 69 6c 65 20 74 6f 20 62 65 20 63 61 he file to be ca
130f3 63 68 65 64 2e 20 54 65 6d 70 6f 72 61 72 79 20 ched. Temporary
130f4 66 69 6c 65 73 20 61 72 65 20 62 65 20 64 65 6c files are be del
130f5 65 74 65 64 0a 2a 2a 20 61 75 74 6f 6d 61 74 69 eted.** automati
130f6 63 61 6c 6c 79 20 77 68 65 6e 20 74 68 65 79 20 cally when they
130f7 61 72 65 20 63 6c 6f 73 65 64 2e 20 49 66 20 7a are closed. If z
130f8 46 69 6c 65 6e 61 6d 65 20 69 73 20 22 3a 6d 65 Filename is ":me
130f9 6d 6f 72 79 3a 22 20 74 68 65 6e 20 0a 2a 2a 20 mory:" then .**
130fa 61 6c 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 all information
130fb 69 73 20 68 65 6c 64 20 69 6e 20 63 61 63 68 65 is held in cache
130fc 2e 20 49 74 20 69 73 20 6e 65 76 65 72 20 77 72 . It is never wr
130fd 69 74 74 65 6e 20 74 6f 20 64 69 73 6b 2e 20 0a itten to disk. .
130fe 2a 2a 20 54 68 69 73 20 63 61 6e 20 62 65 20 75 ** This can be u
130ff 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 sed to implement
13100 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 an in-memory da
13101 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 tabase..**.** Th
13102 65 20 6e 45 78 74 72 61 20 70 61 72 61 6d 65 74 e nExtra paramet
13103 65 72 20 73 70 65 63 69 66 69 65 73 20 74 68 65 er specifies the
13104 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 number of bytes
13105 20 6f 66 20 73 70 61 63 65 20 61 6c 6c 6f 63 61 of space alloca
13106 74 65 64 0a 2a 2a 20 61 6c 6f 6e 67 20 77 69 74 ted.** along wit
13107 68 20 65 61 63 68 20 70 61 67 65 20 72 65 66 65 h each page refe
13108 72 65 6e 63 65 2e 20 54 68 69 73 20 73 70 61 63 rence. This spac
13109 65 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20 74 e is available t
1310a 6f 20 74 68 65 20 75 73 65 72 0a 2a 2a 20 76 69 o the user.** vi
1310b 61 20 74 68 65 20 73 71 6c 69 74 65 33 50 61 67 a the sqlite3Pag
1310c 65 72 47 65 74 45 78 74 72 61 28 29 20 41 50 49 erGetExtra() API
1310d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6c 61 67 ..**.** The flag
1310e 73 20 61 72 67 75 6d 65 6e 74 20 69 73 20 75 73 s argument is us
1310f 65 64 20 74 6f 20 73 70 65 63 69 66 79 20 70 72 ed to specify pr
13110 6f 70 65 72 74 69 65 73 20 74 68 61 74 20 61 66 operties that af
13111 66 65 63 74 20 74 68 65 0a 2a 2a 20 6f 70 65 72 fect the.** oper
13112 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 70 61 67 ation of the pag
13113 65 72 2e 20 49 74 20 73 68 6f 75 6c 64 20 62 65 er. It should be
13114 20 70 61 73 73 65 64 20 73 6f 6d 65 20 62 69 74 passed some bit
13115 77 69 73 65 20 63 6f 6d 62 69 6e 61 74 69 6f 6e wise combination
13116 0a 2a 2a 20 6f 66 20 74 68 65 20 50 41 47 45 52 .** of the PAGER
13117 5f 4f 4d 49 54 5f 4a 4f 55 52 4e 41 4c 20 61 6e _OMIT_JOURNAL an
13118 64 20 50 41 47 45 52 5f 4e 4f 5f 52 45 41 44 4c d PAGER_NO_READL
13119 4f 43 4b 20 66 6c 61 67 73 2e 0a 2a 2a 0a 2a 2a OCK flags..**.**
1311a 20 54 68 65 20 76 66 73 46 6c 61 67 73 20 70 61 The vfsFlags pa
1311b 72 61 6d 65 74 65 72 20 69 73 20 61 20 62 69 74 rameter is a bit
1311c 6d 61 73 6b 20 74 6f 20 70 61 73 73 20 74 6f 20 mask to pass to
1311d 74 68 65 20 66 6c 61 67 73 20 70 61 72 61 6d 65 the flags parame
1311e 74 65 72 0a 2a 2a 20 6f 66 20 74 68 65 20 78 4f ter.** of the xO
1311f 70 65 6e 28 29 20 6d 65 74 68 6f 64 20 6f 66 20 pen() method of
13120 74 68 65 20 73 75 70 70 6c 69 65 64 20 56 46 53 the supplied VFS
13121 20 77 68 65 6e 20 6f 70 65 6e 69 6e 67 20 66 69 when opening fi
13122 6c 65 73 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 les. .**.** If t
13123 68 65 20 70 61 67 65 72 20 6f 62 6a 65 63 74 20 he pager object
13124 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 is allocated and
13125 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 66 the specified f
13126 69 6c 65 20 6f 70 65 6e 65 64 20 0a 2a 2a 20 73 ile opened .** s
13127 75 63 63 65 73 73 66 75 6c 6c 79 2c 20 53 51 4c uccessfully, SQL
13128 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e ITE_OK is return
13129 65 64 20 61 6e 64 20 2a 70 70 50 61 67 65 72 20 ed and *ppPager
1312a 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f 0a set to point to.
1312b 2a 2a 20 74 68 65 20 6e 65 77 20 70 61 67 65 72 ** the new pager
1312c 20 6f 62 6a 65 63 74 2e 20 49 66 20 61 6e 20 65 object. If an e
1312d 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 2a 70 70 rror occurs, *pp
1312e 50 61 67 65 72 20 69 73 20 73 65 74 20 74 6f 20 Pager is set to
1312f 4e 55 4c 4c 0a 2a 2a 20 61 6e 64 20 65 72 72 6f NULL.** and erro
13130 72 20 63 6f 64 65 20 72 65 74 75 72 6e 65 64 2e r code returned.
13131 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d This function m
13132 61 79 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 ay return SQLITE
13133 5f 4e 4f 4d 45 4d 0a 2a 2a 20 28 73 71 6c 69 74 _NOMEM.** (sqlit
13134 65 33 4d 61 6c 6c 6f 63 28 29 20 69 73 20 75 73 e3Malloc() is us
13135 65 64 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 6d ed to allocate m
13136 65 6d 6f 72 79 29 2c 20 53 51 4c 49 54 45 5f 43 emory), SQLITE_C
13137 41 4e 54 4f 50 45 4e 20 6f 72 20 0a 2a 2a 20 76 ANTOPEN or .** v
13138 61 72 69 6f 75 73 20 53 51 4c 49 54 45 5f 49 4f arious SQLITE_IO
13139 5f 58 58 58 20 65 72 72 6f 72 73 2e 0a 2a 2f 0a _XXX errors..*/.
1313a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
1313b 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 4f nt sqlite3PagerO
1313c 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 pen(. sqlite3_v
1313d 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20 20 fs *pVfs,
1313e 2f 2a 20 54 68 65 20 76 69 72 74 75 61 6c 20 66 /* The virtual f
1313f 69 6c 65 20 73 79 73 74 65 6d 20 74 6f 20 75 73 ile system to us
13140 65 20 2a 2f 0a 20 20 50 61 67 65 72 20 2a 2a 70 e */. Pager **p
13141 70 50 61 67 65 72 2c 20 20 20 20 20 20 20 20 20 pPager,
13142 2f 2a 20 4f 55 54 3a 20 52 65 74 75 72 6e 20 74 /* OUT: Return t
13143 68 65 20 50 61 67 65 72 20 73 74 72 75 63 74 75 he Pager structu
13144 72 65 20 68 65 72 65 20 2a 2f 0a 20 20 63 6f 6e re here */. con
13145 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 st char *zFilena
13146 6d 65 2c 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 me, /* Name of
13147 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
13148 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a 20 20 le to open */.
13149 69 6e 74 20 6e 45 78 74 72 61 2c 20 20 20 20 20 int nExtra,
1314a 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 74 72 /* Extr
1314b 61 20 62 79 74 65 73 20 61 70 70 65 6e 64 20 74 a bytes append t
1314c 6f 20 65 61 63 68 20 69 6e 2d 6d 65 6d 6f 72 79 o each in-memory
1314d 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 66 page */. int f
1314e 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 lags,
1314f 20 20 20 20 2f 2a 20 66 6c 61 67 73 20 63 6f 6e /* flags con
13150 74 72 6f 6c 6c 69 6e 67 20 74 68 69 73 20 66 69 trolling this fi
13151 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 76 66 73 46 le */. int vfsF
13152 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 lags,
13153 20 2f 2a 20 66 6c 61 67 73 20 70 61 73 73 65 64 /* flags passed
13154 20 74 68 72 6f 75 67 68 20 74 6f 20 73 71 6c 69 through to sqli
13155 74 65 33 5f 76 66 73 2e 78 4f 70 65 6e 28 29 20 te3_vfs.xOpen()
13156 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 52 65 69 */. void (*xRei
13157 6e 69 74 29 28 44 62 50 61 67 65 2a 29 20 2f 2a nit)(DbPage*) /*
13158 20 46 75 6e 63 74 69 6f 6e 20 74 6f 20 72 65 69 Function to rei
13159 6e 69 74 69 61 6c 69 7a 65 20 70 61 67 65 73 20 nitialize pages
1315a 2a 2f 0a 29 7b 0a 20 20 75 38 20 2a 70 50 74 72 */.){. u8 *pPtr
1315b 3b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 ;. Pager *pPage
1315c 72 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 r = 0; /*
1315d 50 61 67 65 72 20 6f 62 6a 65 63 74 20 74 6f 20 Pager object to
1315e 61 6c 6c 6f 63 61 74 65 20 61 6e 64 20 72 65 74 allocate and ret
1315f 75 72 6e 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 urn */. int rc
13160 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 = SQLITE_OK;
13161 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 /* Return code
13162 20 2a 2f 0a 20 20 69 6e 74 20 74 65 6d 70 46 69 */. int tempFi
13163 6c 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f le = 0; /
13164 2a 20 54 72 75 65 20 66 6f 72 20 74 65 6d 70 20 * True for temp
13165 66 69 6c 65 73 20 28 69 6e 63 6c 2e 20 69 6e 2d files (incl. in-
13166 6d 65 6d 6f 72 79 20 66 69 6c 65 73 29 20 2a 2f memory files) */
13167 0a 20 20 69 6e 74 20 6d 65 6d 44 62 20 3d 20 30 . int memDb = 0
13168 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 ; /* T
13169 72 75 65 20 69 66 20 74 68 69 73 20 69 73 20 61 rue if this is a
1316a 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 66 69 6c 65 n in-memory file
1316b 20 2a 2f 0a 20 20 69 6e 74 20 72 65 61 64 4f 6e */. int readOn
1316c 6c 79 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f ly = 0; /
1316d 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20 69 * True if this i
1316e 73 20 61 20 72 65 61 64 2d 6f 6e 6c 79 20 66 69 s a read-only fi
1316f 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6a 6f 75 72 le */. int jour
13170 6e 61 6c 46 69 6c 65 53 69 7a 65 3b 20 20 20 20 nalFileSize;
13171 20 2f 2a 20 42 79 74 65 73 20 74 6f 20 61 6c 6c /* Bytes to all
13172 6f 63 61 74 65 20 66 6f 72 20 65 61 63 68 20 6a ocate for each j
13173 6f 75 72 6e 61 6c 20 66 64 20 2a 2f 0a 20 20 63 ournal fd */. c
13174 68 61 72 20 2a 7a 50 61 74 68 6e 61 6d 65 20 3d har *zPathname =
13175 20 30 3b 20 20 20 20 20 2f 2a 20 46 75 6c 6c 20 0; /* Full
13176 70 61 74 68 20 74 6f 20 64 61 74 61 62 61 73 65 path to database
13177 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e file */. int n
13178 50 61 74 68 6e 61 6d 65 20 3d 20 30 3b 20 20 20 Pathname = 0;
13179 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
1317a 20 62 79 74 65 73 20 69 6e 20 7a 50 61 74 68 6e bytes in zPathn
1317b 61 6d 65 20 2a 2f 0a 20 20 69 6e 74 20 75 73 65 ame */. int use
1317c 4a 6f 75 72 6e 61 6c 20 3d 20 28 66 6c 61 67 73 Journal = (flags
1317d 20 26 20 50 41 47 45 52 5f 4f 4d 49 54 5f 4a 4f & PAGER_OMIT_JO
1317e 55 52 4e 41 4c 29 3d 3d 30 3b 20 2f 2a 20 46 61 URNAL)==0; /* Fa
1317f 6c 73 65 20 74 6f 20 6f 6d 69 74 20 6a 6f 75 72 lse to omit jour
13180 6e 61 6c 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f 52 nal */. int noR
13181 65 61 64 6c 6f 63 6b 20 3d 20 28 66 6c 61 67 73 eadlock = (flags
13182 20 26 20 50 41 47 45 52 5f 4e 4f 5f 52 45 41 44 & PAGER_NO_READ
13183 4c 4f 43 4b 29 21 3d 30 3b 20 20 2f 2a 20 54 72 LOCK)!=0; /* Tr
13184 75 65 20 74 6f 20 6f 6d 69 74 20 72 65 61 64 2d ue to omit read-
13185 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 70 63 lock */. int pc
13186 61 63 68 65 53 69 7a 65 20 3d 20 73 71 6c 69 74 acheSize = sqlit
13187 65 33 50 63 61 63 68 65 53 69 7a 65 28 29 3b 20 e3PcacheSize();
13188 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 74 /* Bytes t
13189 6f 20 61 6c 6c 6f 63 61 74 65 20 66 6f 72 20 50 o allocate for P
1318a 43 61 63 68 65 20 2a 2f 0a 20 20 75 31 36 20 73 Cache */. u16 s
1318b 7a 50 61 67 65 44 66 6c 74 20 3d 20 53 51 4c 49 zPageDflt = SQLI
1318c 54 45 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f TE_DEFAULT_PAGE_
1318d 53 49 5a 45 3b 20 20 2f 2a 20 44 65 66 61 75 6c SIZE; /* Defaul
1318e 74 20 70 61 67 65 20 73 69 7a 65 20 2a 2f 0a 0a t page size */..
1318f 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20 /* Figure out
13190 68 6f 77 20 6d 75 63 68 20 73 70 61 63 65 20 69 how much space i
13191 73 20 72 65 71 75 69 72 65 64 20 66 6f 72 20 65 s required for e
13192 61 63 68 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ach journal file
13193 2d 68 61 6e 64 6c 65 0a 20 20 2a 2a 20 28 74 68 -handle. ** (th
13194 65 72 65 20 61 72 65 20 74 77 6f 20 6f 66 20 74 ere are two of t
13195 68 65 6d 2c 20 74 68 65 20 6d 61 69 6e 20 6a 6f hem, the main jo
13196 75 72 6e 61 6c 20 61 6e 64 20 74 68 65 20 73 75 urnal and the su
13197 62 2d 6a 6f 75 72 6e 61 6c 29 2e 20 54 68 69 73 b-journal). This
13198 0a 20 20 2a 2a 20 69 73 20 74 68 65 20 6d 61 78 . ** is the max
13199 69 6d 75 6d 20 73 70 61 63 65 20 72 65 71 75 69 imum space requi
1319a 72 65 64 20 66 6f 72 20 61 6e 20 69 6e 2d 6d 65 red for an in-me
1319b 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 20 66 69 6c mory journal fil
1319c 65 20 68 61 6e 64 6c 65 20 0a 20 20 2a 2a 20 61 e handle . ** a
1319d 6e 64 20 61 20 72 65 67 75 6c 61 72 20 6a 6f 75 nd a regular jou
1319e 72 6e 61 6c 20 66 69 6c 65 2d 68 61 6e 64 6c 65 rnal file-handle
1319f 2e 20 4e 6f 74 65 20 74 68 61 74 20 61 20 22 72 . Note that a "r
131a0 65 67 75 6c 61 72 20 6a 6f 75 72 6e 61 6c 2d 68 egular journal-h
131a1 61 6e 64 6c 65 22 0a 20 20 2a 2a 20 6d 61 79 20 andle". ** may
131a2 62 65 20 61 20 77 72 61 70 70 65 72 20 63 61 70 be a wrapper cap
131a3 61 62 6c 65 20 6f 66 20 63 61 63 68 69 6e 67 20 able of caching
131a4 74 68 65 20 66 69 72 73 74 20 70 6f 72 74 69 6f the first portio
131a5 6e 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c n of the journal
131a6 0a 20 20 2a 2a 20 66 69 6c 65 20 69 6e 20 6d 65 . ** file in me
131a7 6d 6f 72 79 20 74 6f 20 69 6d 70 6c 65 6d 65 6e mory to implemen
131a8 74 20 74 68 65 20 61 74 6f 6d 69 63 2d 77 72 69 t the atomic-wri
131a9 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 te optimization
131aa 28 73 65 65 20 0a 20 20 2a 2a 20 73 6f 75 72 63 (see . ** sourc
131ab 65 20 66 69 6c 65 20 6a 6f 75 72 6e 61 6c 2e 63 e file journal.c
131ac 29 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 ).. */. if( sq
131ad 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 53 69 7a 65 lite3JournalSize
131ae 28 70 56 66 73 29 3e 73 71 6c 69 74 65 33 4d 65 (pVfs)>sqlite3Me
131af 6d 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 29 20 29 mJournalSize() )
131b0 7b 0a 20 20 20 20 6a 6f 75 72 6e 61 6c 46 69 6c {. journalFil
131b1 65 53 69 7a 65 20 3d 20 52 4f 55 4e 44 38 28 73 eSize = ROUND8(s
131b2 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 53 69 7a qlite3JournalSiz
131b3 65 28 70 56 66 73 29 29 3b 0a 20 20 7d 65 6c 73 e(pVfs));. }els
131b4 65 7b 0a 20 20 20 20 6a 6f 75 72 6e 61 6c 46 69 e{. journalFi
131b5 6c 65 53 69 7a 65 20 3d 20 52 4f 55 4e 44 38 28 leSize = ROUND8(
131b6 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 sqlite3MemJourna
131b7 6c 53 69 7a 65 28 29 29 3b 0a 20 20 7d 0a 0a 20 lSize());. }..
131b8 20 2f 2a 20 53 65 74 20 74 68 65 20 6f 75 74 70 /* Set the outp
131b9 75 74 20 76 61 72 69 61 62 6c 65 20 74 6f 20 4e ut variable to N
131ba 55 4c 4c 20 69 6e 20 63 61 73 65 20 61 6e 20 65 ULL in case an e
131bb 72 72 6f 72 20 6f 63 63 75 72 73 2e 20 2a 2f 0a rror occurs. */.
131bc 20 20 2a 70 70 50 61 67 65 72 20 3d 20 30 3b 0a *ppPager = 0;.
131bd 0a 20 20 2f 2a 20 43 6f 6d 70 75 74 65 20 61 6e . /* Compute an
131be 64 20 73 74 6f 72 65 20 74 68 65 20 66 75 6c 6c d store the full
131bf 20 70 61 74 68 6e 61 6d 65 20 69 6e 20 61 6e 20 pathname in an
131c0 61 6c 6c 6f 63 61 74 65 64 20 62 75 66 66 65 72 allocated buffer
131c1 20 70 6f 69 6e 74 65 64 0a 20 20 2a 2a 20 74 6f pointed. ** to
131c2 20 62 79 20 7a 50 61 74 68 6e 61 6d 65 2c 20 6c by zPathname, l
131c3 65 6e 67 74 68 20 6e 50 61 74 68 6e 61 6d 65 2e ength nPathname.
131c4 20 4f 72 2c 20 69 66 20 74 68 69 73 20 69 73 20 Or, if this is
131c5 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 a temporary file
131c6 2c 0a 20 20 2a 2a 20 6c 65 61 76 65 20 62 6f 74 ,. ** leave bot
131c7 68 20 6e 50 61 74 68 6e 61 6d 65 20 61 6e 64 20 h nPathname and
131c8 7a 50 61 74 68 6e 61 6d 65 20 73 65 74 20 74 6f zPathname set to
131c9 20 30 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 7a 0.. */. if( z
131ca 46 69 6c 65 6e 61 6d 65 20 26 26 20 7a 46 69 6c Filename && zFil
131cb 65 6e 61 6d 65 5b 30 5d 20 29 7b 0a 20 20 20 20 ename[0] ){.
131cc 6e 50 61 74 68 6e 61 6d 65 20 3d 20 70 56 66 73 nPathname = pVfs
131cd 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 2b 31 3b 0a ->mxPathname+1;.
131ce 20 20 20 20 7a 50 61 74 68 6e 61 6d 65 20 3d 20 zPathname =
131cf 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 50 sqlite3Malloc(nP
131d0 61 74 68 6e 61 6d 65 2a 32 29 3b 0a 20 20 20 20 athname*2);.
131d1 69 66 28 20 7a 50 61 74 68 6e 61 6d 65 3d 3d 30 if( zPathname==0
131d2 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
131d3 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
131d4 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c }.#ifndef SQL
131d5 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59 44 ITE_OMIT_MEMORYD
131d6 42 0a 20 20 20 20 69 66 28 20 73 74 72 63 6d 70 B. if( strcmp
131d7 28 7a 46 69 6c 65 6e 61 6d 65 2c 22 3a 6d 65 6d (zFilename,":mem
131d8 6f 72 79 3a 22 29 3d 3d 30 20 29 7b 0a 20 20 20 ory:")==0 ){.
131d9 20 20 20 6d 65 6d 44 62 20 3d 20 31 3b 0a 20 20 memDb = 1;.
131da 20 20 20 20 7a 50 61 74 68 6e 61 6d 65 5b 30 5d zPathname[0]
131db 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 0a = 0;. }else.
131dc 23 65 6e 64 69 66 0a 20 20 20 20 7b 0a 20 20 20 #endif. {.
131dd 20 20 20 7a 50 61 74 68 6e 61 6d 65 5b 30 5d 20 zPathname[0]
131de 3d 20 30 3b 20 2f 2a 20 4d 61 6b 65 20 73 75 72 = 0; /* Make sur
131df 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 65 76 e initialized ev
131e0 65 6e 20 69 66 20 46 75 6c 6c 50 61 74 68 6e 61 en if FullPathna
131e1 6d 65 28 29 20 66 61 69 6c 73 20 2a 2f 0a 20 20 me() fails */.
131e2 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
131e3 4f 73 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 70 OsFullPathname(p
131e4 56 66 73 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 Vfs, zFilename,
131e5 6e 50 61 74 68 6e 61 6d 65 2c 20 7a 50 61 74 68 nPathname, zPath
131e6 6e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 name);. }..
131e7 20 20 6e 50 61 74 68 6e 61 6d 65 20 3d 20 73 71 nPathname = sq
131e8 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 50 lite3Strlen30(zP
131e9 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 69 66 athname);. if
131ea 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
131eb 26 26 20 6e 50 61 74 68 6e 61 6d 65 2b 38 3e 70 && nPathname+8>p
131ec 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 20 Vfs->mxPathname
131ed 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 69 73 ){. /* This
131ee 20 62 72 61 6e 63 68 20 69 73 20 74 61 6b 65 6e branch is taken
131ef 20 77 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 when the journa
131f0 6c 20 70 61 74 68 20 72 65 71 75 69 72 65 64 20 l path required
131f1 62 79 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 by. ** the
131f2 64 61 74 61 62 61 73 65 20 62 65 69 6e 67 20 6f database being o
131f3 70 65 6e 65 64 20 77 69 6c 6c 20 62 65 20 6d 6f pened will be mo
131f4 72 65 20 74 68 61 6e 20 70 56 66 73 2d 3e 6d 78 re than pVfs->mx
131f5 50 61 74 68 6e 61 6d 65 0a 20 20 20 20 20 20 2a Pathname. *
131f6 2a 20 62 79 74 65 73 20 69 6e 20 6c 65 6e 67 74 * bytes in lengt
131f7 68 2e 20 54 68 69 73 20 6d 65 61 6e 73 20 74 68 h. This means th
131f8 65 20 64 61 74 61 62 61 73 65 20 63 61 6e 6e 6f e database canno
131f9 74 20 62 65 20 6f 70 65 6e 65 64 2c 0a 20 20 20 t be opened,.
131fa 20 20 20 2a 2a 20 61 73 20 69 74 20 77 69 6c 6c ** as it will
131fb 20 6e 6f 74 20 62 65 20 70 6f 73 73 69 62 6c 65 not be possible
131fc 20 74 6f 20 6f 70 65 6e 20 74 68 65 20 6a 6f 75 to open the jou
131fd 72 6e 61 6c 20 66 69 6c 65 20 6f 72 20 65 76 65 rnal file or eve
131fe 6e 0a 20 20 20 20 20 20 2a 2a 20 63 68 65 63 6b n. ** check
131ff 20 66 6f 72 20 61 20 68 6f 74 2d 6a 6f 75 72 6e for a hot-journ
13200 61 6c 20 62 65 66 6f 72 65 20 72 65 61 64 69 6e al before readin
13201 67 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 g.. */.
13202 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 41 rc = SQLITE_CA
13203 4e 54 4f 50 45 4e 3b 0a 20 20 20 20 7d 0a 20 20 NTOPEN;. }.
13204 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
13205 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c _OK ){. sql
13206 69 74 65 33 5f 66 72 65 65 28 7a 50 61 74 68 6e ite3_free(zPathn
13207 61 6d 65 29 3b 0a 20 20 20 20 20 20 72 65 74 75 ame);. retu
13208 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d rn rc;. }. }
13209 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 .. /* Allocate
1320a 6d 65 6d 6f 72 79 20 66 6f 72 20 74 68 65 20 50 memory for the P
1320b 61 67 65 72 20 73 74 72 75 63 74 75 72 65 2c 20 ager structure,
1320c 50 43 61 63 68 65 20 6f 62 6a 65 63 74 2c 20 74 PCache object, t
1320d 68 65 0a 20 20 2a 2a 20 74 68 72 65 65 20 66 69 he. ** three fi
1320e 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73 2c 20 le descriptors,
1320f 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
13210 65 20 6e 61 6d 65 20 61 6e 64 20 74 68 65 20 6a e name and the j
13211 6f 75 72 6e 61 6c 20 0a 20 20 2a 2a 20 66 69 6c ournal . ** fil
13212 65 20 6e 61 6d 65 2e 20 54 68 65 20 6c 61 79 6f e name. The layo
13213 75 74 20 69 6e 20 6d 65 6d 6f 72 79 20 69 73 20 ut in memory is
13214 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 20 20 2a 2a as follows:. **
13215 0a 20 20 2a 2a 20 20 20 20 20 50 61 67 65 72 20 . ** Pager
13216 6f 62 6a 65 63 74 20 20 20 20 20 20 20 20 20 20 object
13217 20 20 20 20 20 20 20 20 20 20 28 73 69 7a 65 6f (sizeo
13218 66 28 50 61 67 65 72 29 20 62 79 74 65 73 29 0a f(Pager) bytes).
13219 20 20 2a 2a 20 20 20 20 20 50 43 61 63 68 65 20 ** PCache
1321a 6f 62 6a 65 63 74 20 20 20 20 20 20 20 20 20 20 object
1321b 20 20 20 20 20 20 20 20 20 28 73 71 6c 69 74 65 (sqlite
1321c 33 50 63 61 63 68 65 53 69 7a 65 28 29 20 62 79 3PcacheSize() by
1321d 74 65 73 29 0a 20 20 2a 2a 20 20 20 20 20 44 61 tes). ** Da
1321e 74 61 62 61 73 65 20 66 69 6c 65 20 68 61 6e 64 tabase file hand
1321f 6c 65 20 20 20 20 20 20 20 20 20 20 20 20 28 70 le (p
13220 56 66 73 2d 3e 73 7a 4f 73 46 69 6c 65 20 62 79 Vfs->szOsFile by
13221 74 65 73 29 0a 20 20 2a 2a 20 20 20 20 20 53 75 tes). ** Su
13222 62 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 b-journal file h
13223 61 6e 64 6c 65 20 20 20 20 20 20 20 20 20 28 6a andle (j
13224 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65 20 62 ournalFileSize b
13225 79 74 65 73 29 0a 20 20 2a 2a 20 20 20 20 20 4d ytes). ** M
13226 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ain journal file
13227 20 68 61 6e 64 6c 65 20 20 20 20 20 20 20 20 28 handle (
13228 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65 20 journalFileSize
13229 62 79 74 65 73 29 0a 20 20 2a 2a 20 20 20 20 20 bytes). **
1322a 44 61 74 61 62 61 73 65 20 66 69 6c 65 20 6e 61 Database file na
1322b 6d 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 me
1322c 28 6e 50 61 74 68 6e 61 6d 65 2b 31 20 62 79 74 (nPathname+1 byt
1322d 65 73 29 0a 20 20 2a 2a 20 20 20 20 20 4a 6f 75 es). ** Jou
1322e 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 20 rnal file name
1322f 20 20 20 20 20 20 20 20 20 20 20 20 20 28 6e 50 (nP
13230 61 74 68 6e 61 6d 65 2b 38 2b 31 20 62 79 74 65 athname+8+1 byte
13231 73 29 0a 20 20 2a 2f 0a 20 20 70 50 74 72 20 3d s). */. pPtr =
13232 20 28 75 38 20 2a 29 73 71 6c 69 74 65 33 4d 61 (u8 *)sqlite3Ma
13233 6c 6c 6f 63 5a 65 72 6f 28 0a 20 20 20 20 52 4f llocZero(. RO
13234 55 4e 44 38 28 73 69 7a 65 6f 66 28 2a 70 50 61 UND8(sizeof(*pPa
13235 67 65 72 29 29 20 2b 20 20 20 20 20 20 2f 2a 20 ger)) + /*
13236 50 61 67 65 72 20 73 74 72 75 63 74 75 72 65 20 Pager structure
13237 2a 2f 0a 20 20 20 20 52 4f 55 4e 44 38 28 70 63 */. ROUND8(pc
13238 61 63 68 65 53 69 7a 65 29 20 2b 20 20 20 20 20 acheSize) +
13239 20 20 20 20 20 20 2f 2a 20 50 43 61 63 68 65 20 /* PCache
1323a 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 20 20 52 4f object */. RO
1323b 55 4e 44 38 28 70 56 66 73 2d 3e 73 7a 4f 73 46 UND8(pVfs->szOsF
1323c 69 6c 65 29 20 2b 20 20 20 20 20 20 20 2f 2a 20 ile) + /*
1323d 54 68 65 20 6d 61 69 6e 20 64 62 20 66 69 6c 65 The main db file
1323e 20 2a 2f 0a 20 20 20 20 6a 6f 75 72 6e 61 6c 46 */. journalF
1323f 69 6c 65 53 69 7a 65 20 2a 20 32 20 2b 20 20 20 ileSize * 2 +
13240 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 77 /* The tw
13241 6f 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 20 o journal files
13242 2a 2f 20 0a 20 20 20 20 6e 50 61 74 68 6e 61 6d */ . nPathnam
13243 65 20 2b 20 31 20 2b 20 20 20 20 20 20 20 20 20 e + 1 +
13244 20 20 20 20 20 20 20 2f 2a 20 7a 46 69 6c 65 6e /* zFilen
13245 61 6d 65 20 2a 2f 0a 20 20 20 20 6e 50 61 74 68 ame */. nPath
13246 6e 61 6d 65 20 2b 20 38 20 2b 20 31 20 20 20 20 name + 8 + 1
13247 20 20 20 20 20 20 20 20 20 20 2f 2a 20 7a 4a 6f /* zJo
13248 75 72 6e 61 6c 20 2a 2f 0a 20 20 29 3b 0a 20 20 urnal */. );.
13249 61 73 73 65 72 74 28 20 45 49 47 48 54 5f 42 59 assert( EIGHT_BY
1324a 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 53 51 4c TE_ALIGNMENT(SQL
1324b 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 52 28 6a ITE_INT_TO_PTR(j
1324c 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65 29 29 ournalFileSize))
1324d 20 29 3b 0a 20 20 69 66 28 20 21 70 50 74 72 20 );. if( !pPtr
1324e 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 ){. sqlite3_f
1324f 72 65 65 28 7a 50 61 74 68 6e 61 6d 65 29 3b 0a ree(zPathname);.
13250 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
13251 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 70 E_NOMEM;. }. p
13252 50 61 67 65 72 20 3d 20 20 20 20 20 20 20 20 20 Pager =
13253 20 20 20 20 20 28 50 61 67 65 72 2a 29 28 70 50 (Pager*)(pP
13254 74 72 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 70 tr);. pPager->p
13255 50 43 61 63 68 65 20 3d 20 20 20 20 28 50 43 61 PCache = (PCa
13256 63 68 65 2a 29 28 70 50 74 72 20 2b 3d 20 52 4f che*)(pPtr += RO
13257 55 4e 44 38 28 73 69 7a 65 6f 66 28 2a 70 50 61 UND8(sizeof(*pPa
13258 67 65 72 29 29 29 3b 0a 20 20 70 50 61 67 65 72 ger)));. pPager
13259 2d 3e 66 64 20 3d 20 20 20 28 73 71 6c 69 74 65 ->fd = (sqlite
1325a 33 5f 66 69 6c 65 2a 29 28 70 50 74 72 20 2b 3d 3_file*)(pPtr +=
1325b 20 52 4f 55 4e 44 38 28 70 63 61 63 68 65 53 69 ROUND8(pcacheSi
1325c 7a 65 29 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e ze));. pPager->
1325d 73 6a 66 64 20 3d 20 28 73 71 6c 69 74 65 33 5f sjfd = (sqlite3_
1325e 66 69 6c 65 2a 29 28 70 50 74 72 20 2b 3d 20 52 file*)(pPtr += R
1325f 4f 55 4e 44 38 28 70 56 66 73 2d 3e 73 7a 4f 73 OUND8(pVfs->szOs
13260 46 69 6c 65 29 29 3b 0a 20 20 70 50 61 67 65 72 File));. pPager
13261 2d 3e 6a 66 64 20 3d 20 20 28 73 71 6c 69 74 65 ->jfd = (sqlite
13262 33 5f 66 69 6c 65 2a 29 28 70 50 74 72 20 2b 3d 3_file*)(pPtr +=
13263 20 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65 journalFileSize
13264 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 7a 46 69 );. pPager->zFi
13265 6c 65 6e 61 6d 65 20 3d 20 20 20 20 28 63 68 61 lename = (cha
13266 72 2a 29 28 70 50 74 72 20 2b 3d 20 6a 6f 75 72 r*)(pPtr += jour
13267 6e 61 6c 46 69 6c 65 53 69 7a 65 29 3b 0a 20 20 nalFileSize);.
13268 61 73 73 65 72 74 28 20 45 49 47 48 54 5f 42 59 assert( EIGHT_BY
13269 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 70 50 61 TE_ALIGNMENT(pPa
1326a 67 65 72 2d 3e 6a 66 64 29 20 29 3b 0a 0a 20 20 ger->jfd) );..
1326b 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 68 65 20 50 /* Fill in the P
1326c 61 67 65 72 2e 7a 46 69 6c 65 6e 61 6d 65 20 61 ager.zFilename a
1326d 6e 64 20 50 61 67 65 72 2e 7a 4a 6f 75 72 6e 61 nd Pager.zJourna
1326e 6c 20 62 75 66 66 65 72 73 2c 20 69 66 20 72 65 l buffers, if re
1326f 71 75 69 72 65 64 2e 20 2a 2f 0a 20 20 69 66 28 quired. */. if(
13270 20 7a 50 61 74 68 6e 61 6d 65 20 29 7b 0a 20 20 zPathname ){.
13271 20 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e pPager->zJourn
13272 61 6c 20 3d 20 20 20 28 63 68 61 72 2a 29 28 70 al = (char*)(p
13273 50 74 72 20 2b 3d 20 6e 50 61 74 68 6e 61 6d 65 Ptr += nPathname
13274 20 2b 20 31 29 3b 0a 20 20 20 20 6d 65 6d 63 70 + 1);. memcp
13275 79 28 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e y(pPager->zFilen
13276 61 6d 65 2c 20 7a 50 61 74 68 6e 61 6d 65 2c 20 ame, zPathname,
13277 6e 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 nPathname);.
13278 6d 65 6d 63 70 79 28 70 50 61 67 65 72 2d 3e 7a memcpy(pPager->z
13279 4a 6f 75 72 6e 61 6c 2c 20 7a 50 61 74 68 6e 61 Journal, zPathna
1327a 6d 65 2c 20 6e 50 61 74 68 6e 61 6d 65 29 3b 0a me, nPathname);.
1327b 20 20 20 20 6d 65 6d 63 70 79 28 26 70 50 61 67 memcpy(&pPag
1327c 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 5b 6e 50 61 er->zJournal[nPa
1327d 74 68 6e 61 6d 65 5d 2c 20 22 2d 6a 6f 75 72 6e thname], "-journ
1327e 61 6c 22 2c 20 38 29 3b 0a 20 20 20 20 69 66 28 al", 8);. if(
1327f 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 pPager->zFilena
13280 6d 65 5b 30 5d 3d 3d 30 20 29 20 70 50 61 67 65 me[0]==0 ) pPage
13281 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 5b 30 5d 20 3d r->zJournal[0] =
13282 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 0;. sqlite3_
13283 66 72 65 65 28 7a 50 61 74 68 6e 61 6d 65 29 3b free(zPathname);
13284 0a 20 20 7d 0a 20 20 70 50 61 67 65 72 2d 3e 70 . }. pPager->p
13285 56 66 73 20 3d 20 70 56 66 73 3b 0a 20 20 70 50 Vfs = pVfs;. pP
13286 61 67 65 72 2d 3e 76 66 73 46 6c 61 67 73 20 3d ager->vfsFlags =
13287 20 76 66 73 46 6c 61 67 73 3b 0a 0a 20 20 2f 2a vfsFlags;.. /*
13288 20 4f 70 65 6e 20 74 68 65 20 70 61 67 65 72 20 Open the pager
13289 66 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 file.. */. if(
1328a 20 7a 46 69 6c 65 6e 61 6d 65 20 26 26 20 7a 46 zFilename && zF
1328b 69 6c 65 6e 61 6d 65 5b 30 5d 20 26 26 20 21 6d ilename[0] && !m
1328c 65 6d 44 62 20 29 7b 0a 20 20 20 20 69 6e 74 20 emDb ){. int
1328d 66 6f 75 74 20 3d 20 30 3b 20 20 20 20 20 20 20 fout = 0;
1328e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1328f 56 46 53 20 66 6c 61 67 73 20 72 65 74 75 72 6e VFS flags return
13290 65 64 20 62 79 20 78 4f 70 65 6e 28 29 20 2a 2f ed by xOpen() */
13291 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
13292 33 4f 73 4f 70 65 6e 28 70 56 66 73 2c 20 70 50 3OsOpen(pVfs, pP
13293 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 2c ager->zFilename,
13294 20 70 50 61 67 65 72 2d 3e 66 64 2c 20 76 66 73 pPager->fd, vfs
13295 46 6c 61 67 73 2c 20 26 66 6f 75 74 29 3b 0a 20 Flags, &fout);.
13296 20 20 20 72 65 61 64 4f 6e 6c 79 20 3d 20 28 66 readOnly = (f
13297 6f 75 74 26 53 51 4c 49 54 45 5f 4f 50 45 4e 5f out&SQLITE_OPEN_
13298 52 45 41 44 4f 4e 4c 59 29 3b 0a 0a 20 20 20 20 READONLY);..
13299 2f 2a 20 49 66 20 74 68 65 20 66 69 6c 65 20 77 /* If the file w
1329a 61 73 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 as successfully
1329b 6f 70 65 6e 65 64 20 66 6f 72 20 72 65 61 64 2f opened for read/
1329c 77 72 69 74 65 20 61 63 63 65 73 73 2c 0a 20 20 write access,.
1329d 20 20 2a 2a 20 63 68 6f 6f 73 65 20 61 20 64 65 ** choose a de
1329e 66 61 75 6c 74 20 70 61 67 65 20 73 69 7a 65 20 fault page size
1329f 69 6e 20 63 61 73 65 20 77 65 20 68 61 76 65 20 in case we have
132a0 74 6f 20 63 72 65 61 74 65 20 74 68 65 0a 20 20 to create the.
132a1 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 ** database fi
132a2 6c 65 2e 20 54 68 65 20 64 65 66 61 75 6c 74 20 le. The default
132a3 70 61 67 65 20 73 69 7a 65 20 69 73 20 74 68 65 page size is the
132a4 20 6d 61 78 69 6d 75 6d 20 6f 66 3a 0a 20 20 20 maximum of:.
132a5 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 2b 20 **. ** +
132a6 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 SQLITE_DEFAULT_P
132a7 41 47 45 5f 53 49 5a 45 2c 0a 20 20 20 20 2a 2a AGE_SIZE,. **
132a8 20 20 20 20 2b 20 54 68 65 20 76 61 6c 75 65 20 + The value
132a9 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 returned by sqli
132aa 74 65 33 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 te3OsSectorSize(
132ab 29 0a 20 20 20 20 2a 2a 20 20 20 20 2b 20 54 68 ). ** + Th
132ac 65 20 6c 61 72 67 65 73 74 20 70 61 67 65 20 73 e largest page s
132ad 69 7a 65 20 74 68 61 74 20 63 61 6e 20 62 65 20 ize that can be
132ae 77 72 69 74 74 65 6e 20 61 74 6f 6d 69 63 61 6c written atomical
132af 6c 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 ly.. */. i
132b0 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
132b1 20 26 26 20 21 72 65 61 64 4f 6e 6c 79 20 29 7b && !readOnly ){
132b2 0a 20 20 20 20 20 20 73 65 74 53 65 63 74 6f 72 . setSector
132b3 53 69 7a 65 28 70 50 61 67 65 72 29 3b 0a 20 20 Size(pPager);.
132b4 20 20 20 20 61 73 73 65 72 74 28 53 51 4c 49 54 assert(SQLIT
132b5 45 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 E_DEFAULT_PAGE_S
132b6 49 5a 45 3c 3d 53 51 4c 49 54 45 5f 4d 41 58 5f IZE<=SQLITE_MAX_
132b7 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a DEFAULT_PAGE_SIZ
132b8 45 29 3b 0a 20 20 20 20 20 20 69 66 28 20 73 7a E);. if( sz
132b9 50 61 67 65 44 66 6c 74 3c 70 50 61 67 65 72 2d PageDflt<pPager-
132ba 3e 73 65 63 74 6f 72 53 69 7a 65 20 29 7b 0a 20 >sectorSize ){.
132bb 20 20 20 20 20 20 20 69 66 28 20 70 50 61 67 65 if( pPage
132bc 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 3e 53 51 r->sectorSize>SQ
132bd 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54 LITE_MAX_DEFAULT
132be 5f 50 41 47 45 5f 53 49 5a 45 20 29 7b 0a 20 20 _PAGE_SIZE ){.
132bf 20 20 20 20 20 20 20 20 73 7a 50 61 67 65 44 66 szPageDf
132c0 6c 74 20 3d 20 53 51 4c 49 54 45 5f 4d 41 58 5f lt = SQLITE_MAX_
132c1 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a DEFAULT_PAGE_SIZ
132c2 45 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 E;. }else
132c3 7b 0a 20 20 20 20 20 20 20 20 20 20 73 7a 50 61 {. szPa
132c4 67 65 44 66 6c 74 20 3d 20 28 75 31 36 29 70 50 geDflt = (u16)pP
132c5 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 ager->sectorSize
132c6 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
132c7 20 20 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 }.#ifdef SQLIT
132c8 45 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 5f E_ENABLE_ATOMIC_
132c9 57 52 49 54 45 0a 20 20 20 20 20 20 7b 0a 20 20 WRITE. {.
132ca 20 20 20 20 20 20 69 6e 74 20 69 44 63 20 3d 20 int iDc =
132cb 73 71 6c 69 74 65 33 4f 73 44 65 76 69 63 65 43 sqlite3OsDeviceC
132cc 68 61 72 61 63 74 65 72 69 73 74 69 63 73 28 70 haracteristics(p
132cd 50 61 67 65 72 2d 3e 66 64 29 3b 0a 20 20 20 20 Pager->fd);.
132ce 20 20 20 20 69 6e 74 20 69 69 3b 0a 20 20 20 20 int ii;.
132cf 20 20 20 20 61 73 73 65 72 74 28 53 51 4c 49 54 assert(SQLIT
132d0 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 35 31 E_IOCAP_ATOMIC51
132d1 32 3d 3d 28 35 31 32 3e 3e 38 29 29 3b 0a 20 20 2==(512>>8));.
132d2 20 20 20 20 20 20 61 73 73 65 72 74 28 53 51 4c assert(SQL
132d3 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 ITE_IOCAP_ATOMIC
132d4 36 34 4b 3d 3d 28 36 35 35 33 36 3e 3e 38 29 29 64K==(65536>>8))
132d5 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 ;. assert
132d6 28 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41 (SQLITE_MAX_DEFA
132d7 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 3c 3d 36 ULT_PAGE_SIZE<=6
132d8 35 35 33 36 29 3b 0a 20 20 20 20 20 20 20 20 66 5536);. f
132d9 6f 72 28 69 69 3d 73 7a 50 61 67 65 44 66 6c 74 or(ii=szPageDflt
132da 3b 20 69 69 3c 3d 53 51 4c 49 54 45 5f 4d 41 58 ; ii<=SQLITE_MAX
132db 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 _DEFAULT_PAGE_SI
132dc 5a 45 3b 20 69 69 3d 69 69 2a 32 29 7b 0a 20 20 ZE; ii=ii*2){.
132dd 20 20 20 20 20 20 20 20 69 66 28 20 69 44 63 26 if( iDc&
132de 28 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 (SQLITE_IOCAP_AT
132df 4f 4d 49 43 7c 28 69 69 3e 3e 38 29 29 20 29 7b OMIC|(ii>>8)) ){
132e0 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 7a 50 . szP
132e1 61 67 65 44 66 6c 74 20 3d 20 69 69 3b 0a 20 20 ageDflt = ii;.
132e2 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
132e3 20 20 7d 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 }. }.#end
132e4 69 66 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 if. }. }else
132e5 7b 0a 20 20 20 20 2f 2a 20 49 66 20 61 20 74 65 {. /* If a te
132e6 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 69 73 20 mporary file is
132e7 72 65 71 75 65 73 74 65 64 2c 20 69 74 20 69 73 requested, it is
132e8 20 6e 6f 74 20 6f 70 65 6e 65 64 20 69 6d 6d 65 not opened imme
132e9 64 69 61 74 65 6c 79 2e 0a 20 20 20 20 2a 2a 20 diately.. **
132ea 49 6e 20 74 68 69 73 20 63 61 73 65 20 77 65 20 In this case we
132eb 61 63 63 65 70 74 20 74 68 65 20 64 65 66 61 75 accept the defau
132ec 6c 74 20 70 61 67 65 20 73 69 7a 65 20 61 6e 64 lt page size and
132ed 20 64 65 6c 61 79 20 61 63 74 75 61 6c 6c 79 0a delay actually.
132ee 20 20 20 20 2a 2a 20 6f 70 65 6e 69 6e 67 20 74 ** opening t
132ef 68 65 20 66 69 6c 65 20 75 6e 74 69 6c 20 74 68 he file until th
132f0 65 20 66 69 72 73 74 20 63 61 6c 6c 20 74 6f 20 e first call to
132f1 4f 73 57 72 69 74 65 28 29 2e 0a 20 20 20 20 2a OsWrite().. *
132f2 2a 0a 20 20 20 20 2a 2a 20 54 68 69 73 20 62 72 *. ** This br
132f3 61 6e 63 68 20 69 73 20 61 6c 73 6f 20 72 75 6e anch is also run
132f4 20 66 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 for an in-memor
132f5 79 20 64 61 74 61 62 61 73 65 2e 20 41 6e 20 69 y database. An i
132f6 6e 2d 6d 65 6d 6f 72 79 0a 20 20 20 20 2a 2a 20 n-memory. **
132f7 64 61 74 61 62 61 73 65 20 69 73 20 74 68 65 20 database is the
132f8 73 61 6d 65 20 61 73 20 61 20 74 65 6d 70 2d 66 same as a temp-f
132f9 69 6c 65 20 74 68 61 74 20 69 73 20 6e 65 76 65 ile that is neve
132fa 72 20 77 72 69 74 74 65 6e 20 6f 75 74 20 74 6f r written out to
132fb 0a 20 20 20 20 2a 2a 20 64 69 73 6b 20 61 6e 64 . ** disk and
132fc 20 75 73 65 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f uses an in-memo
132fd 72 79 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 ry rollback jour
132fe 6e 61 6c 2e 0a 20 20 20 20 2a 2f 20 0a 20 20 20 nal.. */ .
132ff 20 74 65 6d 70 46 69 6c 65 20 3d 20 31 3b 0a 20 tempFile = 1;.
13300 20 20 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 pPager->state
13301 20 3d 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 = PAGER_EXCLUSI
13302 56 45 3b 0a 20 20 20 20 72 65 61 64 4f 6e 6c 79 VE;. readOnly
13303 20 3d 20 28 76 66 73 46 6c 61 67 73 26 53 51 4c = (vfsFlags&SQL
13304 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c ITE_OPEN_READONL
13305 59 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 Y);. }.. /* Th
13306 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 61 6c 6c e following call
13307 20 74 6f 20 50 61 67 65 72 53 65 74 50 61 67 65 to PagerSetPage
13308 73 69 7a 65 28 29 20 73 65 72 76 65 73 20 74 6f size() serves to
13309 20 73 65 74 20 74 68 65 20 76 61 6c 75 65 20 6f set the value o
1330a 66 20 0a 20 20 2a 2a 20 50 61 67 65 72 2e 70 61 f . ** Pager.pa
1330b 67 65 53 69 7a 65 20 61 6e 64 20 74 6f 20 61 6c geSize and to al
1330c 6c 6f 63 61 74 65 20 74 68 65 20 50 61 67 65 72 locate the Pager
1330d 2e 70 54 6d 70 53 70 61 63 65 20 62 75 66 66 65 .pTmpSpace buffe
1330e 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 63 r.. */. if( rc
1330f 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
13310 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 assert( pPage
13311 72 2d 3e 6d 65 6d 44 62 3d 3d 30 20 29 3b 0a 20 r->memDb==0 );.
13312 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 rc = sqlite3P
13313 61 67 65 72 53 65 74 50 61 67 65 73 69 7a 65 28 agerSetPagesize(
13314 70 50 61 67 65 72 2c 20 26 73 7a 50 61 67 65 44 pPager, &szPageD
13315 66 6c 74 2c 20 2d 31 29 3b 0a 20 20 20 20 74 65 flt, -1);. te
13316 73 74 63 61 73 65 28 20 72 63 21 3d 53 51 4c 49 stcase( rc!=SQLI
13317 54 45 5f 4f 4b 20 29 3b 0a 20 20 7d 0a 0a 20 20 TE_OK );. }..
13318 2f 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f /* If an error o
13319 63 63 75 72 72 65 64 20 69 6e 20 65 69 74 68 65 ccurred in eithe
1331a 72 20 6f 66 20 74 68 65 20 62 6c 6f 63 6b 73 20 r of the blocks
1331b 61 62 6f 76 65 2c 20 66 72 65 65 20 74 68 65 20 above, free the
1331c 0a 20 20 2a 2a 20 50 61 67 65 72 20 73 74 72 75 . ** Pager stru
1331d 63 74 75 72 65 20 61 6e 64 20 63 6c 6f 73 65 20 cture and close
1331e 74 68 65 20 66 69 6c 65 2e 0a 20 20 2a 2f 0a 20 the file.. */.
1331f 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
13320 4f 4b 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 OK ){. assert
13321 28 20 21 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 ( !pPager->pTmpS
13322 70 61 63 65 20 29 3b 0a 20 20 20 20 73 71 6c 69 pace );. sqli
13323 74 65 33 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 te3OsClose(pPage
13324 72 2d 3e 66 64 29 3b 0a 20 20 20 20 73 71 6c 69 r->fd);. sqli
13325 74 65 33 5f 66 72 65 65 28 70 50 61 67 65 72 29 te3_free(pPager)
13326 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b ;. return rc;
13327 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 . }.. /* Initi
13328 61 6c 69 7a 65 20 74 68 65 20 50 43 61 63 68 65 alize the PCache
13329 20 6f 62 6a 65 63 74 2e 20 2a 2f 0a 20 20 61 73 object. */. as
1332a 73 65 72 74 28 20 6e 45 78 74 72 61 3c 31 30 30 sert( nExtra<100
1332b 30 20 29 3b 0a 20 20 6e 45 78 74 72 61 20 3d 20 0 );. nExtra =
1332c 52 4f 55 4e 44 38 28 6e 45 78 74 72 61 29 3b 0a ROUND8(nExtra);.
1332d 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4f sqlite3PcacheO
1332e 70 65 6e 28 73 7a 50 61 67 65 44 66 6c 74 2c 20 pen(szPageDflt,
1332f 6e 45 78 74 72 61 2c 20 21 6d 65 6d 44 62 2c 0a nExtra, !memDb,.
13330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13331 20 20 20 20 21 6d 65 6d 44 62 3f 70 61 67 65 72 !memDb?pager
13332 53 74 72 65 73 73 3a 30 2c 20 28 76 6f 69 64 20 Stress:0, (void
13333 2a 29 70 50 61 67 65 72 2c 20 70 50 61 67 65 72 *)pPager, pPager
13334 2d 3e 70 50 43 61 63 68 65 29 3b 0a 0a 20 20 50 ->pPCache);.. P
13335 41 47 45 52 54 52 41 43 45 28 28 22 4f 50 45 4e AGERTRACE(("OPEN
13336 20 25 64 20 25 73 5c 6e 22 2c 20 46 49 4c 45 48 %d %s\n", FILEH
13337 41 4e 44 4c 45 49 44 28 70 50 61 67 65 72 2d 3e ANDLEID(pPager->
13338 66 64 29 2c 20 70 50 61 67 65 72 2d 3e 7a 46 69 fd), pPager->zFi
13339 6c 65 6e 61 6d 65 29 29 3b 0a 20 20 49 4f 54 52 lename));. IOTR
1333a 41 43 45 28 28 22 4f 50 45 4e 20 25 70 20 25 73 ACE(("OPEN %p %s
1333b 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 50 61 \n", pPager, pPa
1333c 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 29 29 ger->zFilename))
1333d 0a 0a 20 20 70 50 61 67 65 72 2d 3e 75 73 65 4a .. pPager->useJ
1333e 6f 75 72 6e 61 6c 20 3d 20 28 75 38 29 75 73 65 ournal = (u8)use
1333f 4a 6f 75 72 6e 61 6c 3b 0a 20 20 70 50 61 67 65 Journal;. pPage
13340 72 2d 3e 6e 6f 52 65 61 64 6c 6f 63 6b 20 3d 20 r->noReadlock =
13341 28 6e 6f 52 65 61 64 6c 6f 63 6b 20 26 26 20 72 (noReadlock && r
13342 65 61 64 4f 6e 6c 79 29 20 3f 31 3a 30 3b 0a 20 eadOnly) ?1:0;.
13343 20 2f 2a 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 /* pPager->stmt
13344 4f 70 65 6e 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f Open = 0; */. /
13345 2a 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 49 6e * pPager->stmtIn
13346 55 73 65 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a Use = 0; */. /*
13347 20 70 50 61 67 65 72 2d 3e 6e 52 65 66 20 3d 20 pPager->nRef =
13348 30 3b 20 2a 2f 0a 20 20 70 50 61 67 65 72 2d 3e 0; */. pPager->
13349 64 62 53 69 7a 65 56 61 6c 69 64 20 3d 20 28 75 dbSizeValid = (u
1334a 38 29 6d 65 6d 44 62 3b 0a 20 20 2f 2a 20 70 50 8)memDb;. /* pP
1334b 61 67 65 72 2d 3e 73 74 6d 74 53 69 7a 65 20 3d ager->stmtSize =
1334c 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 0; */. /* pPag
1334d 65 72 2d 3e 73 74 6d 74 4a 53 69 7a 65 20 3d 20 er->stmtJSize =
1334e 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 65 0; */. /* pPage
1334f 72 2d 3e 6e 50 61 67 65 20 3d 20 30 3b 20 2a 2f r->nPage = 0; */
13350 0a 20 20 70 50 61 67 65 72 2d 3e 6d 78 50 67 6e . pPager->mxPgn
13351 6f 20 3d 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50 o = SQLITE_MAX_P
13352 41 47 45 5f 43 4f 55 4e 54 3b 0a 20 20 2f 2a 20 AGE_COUNT;. /*
13353 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20 3d 20 pPager->state =
13354 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 3b 20 2a 2f PAGER_UNLOCK; */
13355 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
13356 72 2d 3e 73 74 61 74 65 20 3d 3d 20 28 74 65 6d r->state == (tem
13357 70 46 69 6c 65 20 3f 20 50 41 47 45 52 5f 45 58 pFile ? PAGER_EX
13358 43 4c 55 53 49 56 45 20 3a 20 50 41 47 45 52 5f CLUSIVE : PAGER_
13359 55 4e 4c 4f 43 4b 29 20 29 3b 0a 20 20 2f 2a 20 UNLOCK) );. /*
1335a 70 50 61 67 65 72 2d 3e 65 72 72 4d 61 73 6b 20 pPager->errMask
1335b 3d 20 30 3b 20 2a 2f 0a 20 20 70 50 61 67 65 72 = 0; */. pPager
1335c 2d 3e 74 65 6d 70 46 69 6c 65 20 3d 20 28 75 38 ->tempFile = (u8
1335d 29 74 65 6d 70 46 69 6c 65 3b 0a 20 20 61 73 73 )tempFile;. ass
1335e 65 72 74 28 20 74 65 6d 70 46 69 6c 65 3d 3d 50 ert( tempFile==P
1335f 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 AGER_LOCKINGMODE
13360 5f 4e 4f 52 4d 41 4c 20 0a 20 20 20 20 20 20 20 _NORMAL .
13361 20 20 20 7c 7c 20 74 65 6d 70 46 69 6c 65 3d 3d || tempFile==
13362 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 PAGER_LOCKINGMOD
13363 45 5f 45 58 43 4c 55 53 49 56 45 20 29 3b 0a 20 E_EXCLUSIVE );.
13364 20 61 73 73 65 72 74 28 20 50 41 47 45 52 5f 4c assert( PAGER_L
13365 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 OCKINGMODE_EXCLU
13366 53 49 56 45 3d 3d 31 20 29 3b 0a 20 20 70 50 61 SIVE==1 );. pPa
13367 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f ger->exclusiveMo
13368 64 65 20 3d 20 28 75 38 29 74 65 6d 70 46 69 6c de = (u8)tempFil
13369 65 3b 20 0a 20 20 70 50 61 67 65 72 2d 3e 63 68 e; . pPager->ch
1336a 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 3d 20 angeCountDone =
1336b 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 pPager->tempFile
1336c 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6d 65 6d 44 ;. pPager->memD
1336d 62 20 3d 20 28 75 38 29 6d 65 6d 44 62 3b 0a 20 b = (u8)memDb;.
1336e 20 70 50 61 67 65 72 2d 3e 72 65 61 64 4f 6e 6c pPager->readOnl
1336f 79 20 3d 20 28 75 38 29 72 65 61 64 4f 6e 6c 79 y = (u8)readOnly
13370 3b 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 6e ;. /* pPager->n
13371 65 65 64 53 79 6e 63 20 3d 20 30 3b 20 2a 2f 0a eedSync = 0; */.
13372 20 20 61 73 73 65 72 74 28 20 75 73 65 4a 6f 75 assert( useJou
13373 72 6e 61 6c 20 7c 7c 20 70 50 61 67 65 72 2d 3e rnal || pPager->
13374 74 65 6d 70 46 69 6c 65 20 29 3b 0a 20 20 70 50 tempFile );. pP
13375 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 3d 20 70 ager->noSync = p
13376 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 3b Pager->tempFile;
13377 0a 20 20 70 50 61 67 65 72 2d 3e 66 75 6c 6c 53 . pPager->fullS
13378 79 6e 63 20 3d 20 70 50 61 67 65 72 2d 3e 6e 6f ync = pPager->no
13379 53 79 6e 63 20 3f 30 3a 31 3b 0a 20 20 70 50 61 Sync ?0:1;. pPa
1337a 67 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 20 ger->sync_flags
1337b 3d 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f = SQLITE_SYNC_NO
1337c 52 4d 41 4c 3b 0a 20 20 2f 2a 20 70 50 61 67 65 RMAL;. /* pPage
1337d 72 2d 3e 70 46 69 72 73 74 20 3d 20 30 3b 20 2a r->pFirst = 0; *
1337e 2f 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 70 /. /* pPager->p
1337f 46 69 72 73 74 53 79 6e 63 65 64 20 3d 20 30 3b FirstSynced = 0;
13380 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d */. /* pPager-
13381 3e 70 4c 61 73 74 20 3d 20 30 3b 20 2a 2f 0a 20 >pLast = 0; */.
13382 20 70 50 61 67 65 72 2d 3e 6e 45 78 74 72 61 20 pPager->nExtra
13383 3d 20 28 75 31 36 29 6e 45 78 74 72 61 3b 0a 20 = (u16)nExtra;.
13384 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
13385 53 69 7a 65 4c 69 6d 69 74 20 3d 20 53 51 4c 49 SizeLimit = SQLI
13386 54 45 5f 44 45 46 41 55 4c 54 5f 4a 4f 55 52 4e TE_DEFAULT_JOURN
13387 41 4c 5f 53 49 5a 45 5f 4c 49 4d 49 54 3b 0a 20 AL_SIZE_LIMIT;.
13388 20 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 assert( isOpen(
13389 70 50 61 67 65 72 2d 3e 66 64 29 20 7c 7c 20 74 pPager->fd) || t
1338a 65 6d 70 46 69 6c 65 20 29 3b 0a 20 20 73 65 74 empFile );. set
1338b 53 65 63 74 6f 72 53 69 7a 65 28 70 50 61 67 65 SectorSize(pPage
1338c 72 29 3b 0a 20 20 69 66 28 20 21 75 73 65 4a 6f r);. if( !useJo
1338d 75 72 6e 61 6c 20 29 7b 0a 20 20 20 20 70 50 61 urnal ){. pPa
1338e 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 ger->journalMode
1338f 20 3d 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c = PAGER_JOURNAL
13390 4d 4f 44 45 5f 4f 46 46 3b 0a 20 20 7d 65 6c 73 MODE_OFF;. }els
13391 65 20 69 66 28 20 6d 65 6d 44 62 20 29 7b 0a 20 e if( memDb ){.
13392 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e pPager->journ
13393 61 6c 4d 6f 64 65 20 3d 20 50 41 47 45 52 5f 4a alMode = PAGER_J
13394 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 OURNALMODE_MEMOR
13395 59 3b 0a 20 20 7d 0a 20 20 2f 2a 20 70 50 61 67 Y;. }. /* pPag
13396 65 72 2d 3e 78 42 75 73 79 48 61 6e 64 6c 65 72 er->xBusyHandler
13397 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 = 0; */. /* pP
13398 61 67 65 72 2d 3e 70 42 75 73 79 48 61 6e 64 6c ager->pBusyHandl
13399 65 72 41 72 67 20 3d 20 30 3b 20 2a 2f 0a 20 20 erArg = 0; */.
1339a 70 50 61 67 65 72 2d 3e 78 52 65 69 6e 69 74 65 pPager->xReinite
1339b 72 20 3d 20 78 52 65 69 6e 69 74 3b 0a 20 20 2f r = xReinit;. /
1339c 2a 20 6d 65 6d 73 65 74 28 70 50 61 67 65 72 2d * memset(pPager-
1339d 3e 61 48 61 73 68 2c 20 30 2c 20 73 69 7a 65 6f >aHash, 0, sizeo
1339e 66 28 70 50 61 67 65 72 2d 3e 61 48 61 73 68 29 f(pPager->aHash)
1339f 29 3b 20 2a 2f 0a 20 20 2a 70 70 50 61 67 65 72 ); */. *ppPager
133a0 20 3d 20 70 50 61 67 65 72 3b 0a 20 20 72 65 74 = pPager;. ret
133a1 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
133a2 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 ..../*.** This f
133a3 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 unction is calle
133a4 64 20 61 66 74 65 72 20 74 72 61 6e 73 69 74 69 d after transiti
133a5 6f 6e 69 6e 67 20 66 72 6f 6d 20 50 41 47 45 52 oning from PAGER
133a6 5f 55 4e 4c 4f 43 4b 20 74 6f 0a 2a 2a 20 50 41 _UNLOCK to.** PA
133a7 47 45 52 5f 53 48 41 52 45 44 20 73 74 61 74 65 GER_SHARED state
133a8 2e 20 49 74 20 74 65 73 74 73 20 69 66 20 74 68 . It tests if th
133a9 65 72 65 20 69 73 20 61 20 68 6f 74 20 6a 6f 75 ere is a hot jou
133aa 72 6e 61 6c 20 70 72 65 73 65 6e 74 20 69 6e 0a rnal present in.
133ab 2a 2a 20 74 68 65 20 66 69 6c 65 2d 73 79 73 74 ** the file-syst
133ac 65 6d 20 66 6f 72 20 74 68 65 20 67 69 76 65 6e em for the given
133ad 20 70 61 67 65 72 2e 20 41 20 68 6f 74 20 6a 6f pager. A hot jo
133ae 75 72 6e 61 6c 20 69 73 20 6f 6e 65 20 74 68 61 urnal is one tha
133af 74 20 0a 2a 2a 20 6e 65 65 64 73 20 74 6f 20 62 t .** needs to b
133b0 65 20 70 6c 61 79 65 64 20 62 61 63 6b 2e 20 41 e played back. A
133b1 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 69 73 ccording to this
133b2 20 66 75 6e 63 74 69 6f 6e 2c 20 61 20 68 6f 74 function, a hot
133b3 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 -journal.** file
133b4 20 65 78 69 73 74 73 20 69 66 20 74 68 65 20 66 exists if the f
133b5 6f 6c 6c 6f 77 69 6e 67 20 63 72 69 74 65 72 69 ollowing criteri
133b6 61 20 61 72 65 20 6d 65 74 3a 0a 2a 2a 0a 2a 2a a are met:.**.**
133b7 20 20 20 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c * The journal
133b8 20 66 69 6c 65 20 65 78 69 73 74 73 20 69 6e 20 file exists in
133b9 74 68 65 20 66 69 6c 65 20 73 79 73 74 65 6d 2c the file system,
133ba 20 61 6e 64 0a 2a 2a 20 20 20 2a 20 4e 6f 20 70 and.** * No p
133bb 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 61 20 52 rocess holds a R
133bc 45 53 45 52 56 45 44 20 6f 72 20 67 72 65 61 74 ESERVED or great
133bd 65 72 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 er lock on the d
133be 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 61 6e atabase file, an
133bf 64 0a 2a 2a 20 20 20 2a 20 54 68 65 20 64 61 74 d.** * The dat
133c0 61 62 61 73 65 20 66 69 6c 65 20 69 74 73 65 6c abase file itsel
133c1 66 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 f is greater tha
133c2 6e 20 30 20 62 79 74 65 73 20 69 6e 20 73 69 7a n 0 bytes in siz
133c3 65 2c 20 61 6e 64 0a 2a 2a 20 20 20 2a 20 54 68 e, and.** * Th
133c4 65 20 66 69 72 73 74 20 62 79 74 65 20 6f 66 20 e first byte of
133c5 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
133c6 20 65 78 69 73 74 73 20 61 6e 64 20 69 73 20 6e exists and is n
133c7 6f 74 20 30 78 30 30 2e 0a 2a 2a 0a 2a 2a 20 49 ot 0x00..**.** I
133c8 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 69 f the current si
133c9 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 ze of the databa
133ca 73 65 20 66 69 6c 65 20 69 73 20 30 20 62 75 74 se file is 0 but
133cb 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a a journal file.
133cc 2a 2a 20 65 78 69 73 74 73 2c 20 74 68 61 74 20 ** exists, that
133cd 69 73 20 70 72 6f 62 61 62 6c 79 20 61 6e 20 6f is probably an o
133ce 6c 64 20 6a 6f 75 72 6e 61 6c 20 6c 65 66 74 20 ld journal left
133cf 6f 76 65 72 20 66 72 6f 6d 20 61 20 70 72 69 6f over from a prio
133d0 72 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 77 69 r.** database wi
133d1 74 68 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 th the same name
133d2 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 . In this case t
133d3 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
133d4 69 73 0a 2a 2a 20 6a 75 73 74 20 64 65 6c 65 74 is.** just delet
133d5 65 64 20 75 73 69 6e 67 20 4f 73 44 65 6c 65 74 ed using OsDelet
133d6 65 2c 20 2a 70 45 78 69 73 74 73 20 69 73 20 73 e, *pExists is s
133d7 65 74 20 74 6f 20 30 20 61 6e 64 20 53 51 4c 49 et to 0 and SQLI
133d8 54 45 5f 4f 4b 0a 2a 2a 20 69 73 20 72 65 74 75 TE_OK.** is retu
133d9 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 rned..**.** This
133da 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 6e 6f routine does no
133db 74 20 63 68 65 63 6b 20 69 66 20 74 68 65 72 65 t check if there
133dc 20 69 73 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 is a master jou
133dd 72 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 0a 2a 2a rnal filename.**
133de 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 at the end of t
133df 68 65 20 66 69 6c 65 2e 20 49 66 20 74 68 65 72 he file. If ther
133e0 65 20 69 73 2c 20 61 6e 64 20 74 68 61 74 20 6d e is, and that m
133e1 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 aster journal fi
133e2 6c 65 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 65 le.** does not e
133e3 78 69 73 74 2c 20 74 68 65 6e 20 74 68 65 20 6a xist, then the j
133e4 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6e ournal file is n
133e5 6f 74 20 72 65 61 6c 6c 79 20 68 6f 74 2e 20 49 ot really hot. I
133e6 6e 20 74 68 69 73 0a 2a 2a 20 63 61 73 65 20 74 n this.** case t
133e7 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c his routine will
133e8 20 72 65 74 75 72 6e 20 61 20 66 61 6c 73 65 2d return a false-
133e9 70 6f 73 69 74 69 76 65 2e 20 54 68 65 20 70 61 positive. The pa
133ea 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 29 0a 2a ger_playback().*
133eb 2a 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 64 * routine will d
133ec 69 73 63 6f 76 65 72 20 74 68 61 74 20 74 68 65 iscover that the
133ed 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 journal file is
133ee 20 6e 6f 74 20 72 65 61 6c 6c 79 20 68 6f 74 20 not really hot
133ef 61 6e 64 20 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 and .** will not
133f0 20 72 6f 6c 6c 20 69 74 20 62 61 63 6b 2e 20 0a roll it back. .
133f1 2a 2a 0a 2a 2a 20 49 66 20 61 20 68 6f 74 2d 6a **.** If a hot-j
133f2 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 66 ournal file is f
133f3 6f 75 6e 64 20 74 6f 20 65 78 69 73 74 2c 20 2a ound to exist, *
133f4 70 45 78 69 73 74 73 20 69 73 20 73 65 74 20 74 pExists is set t
133f5 6f 20 31 20 61 6e 64 20 0a 2a 2a 20 53 51 4c 49 o 1 and .** SQLI
133f6 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64 2e 20 TE_OK returned.
133f7 49 66 20 6e 6f 20 68 6f 74 2d 6a 6f 75 72 6e 61 If no hot-journa
133f8 6c 20 66 69 6c 65 20 69 73 20 70 72 65 73 65 6e l file is presen
133f9 74 2c 20 2a 70 45 78 69 73 74 73 20 69 73 0a 2a t, *pExists is.*
133fa 2a 20 73 65 74 20 74 6f 20 30 20 61 6e 64 20 53 * set to 0 and S
133fb 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65 QLITE_OK returne
133fc 64 2e 20 49 66 20 61 6e 20 49 4f 20 65 72 72 6f d. If an IO erro
133fd 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 74 r occurs while t
133fe 72 79 69 6e 67 0a 2a 2a 20 74 6f 20 64 65 74 65 rying.** to dete
133ff 72 6d 69 6e 65 20 77 68 65 74 68 65 72 20 6f 72 rmine whether or
13400 20 6e 6f 74 20 61 20 68 6f 74 2d 6a 6f 75 72 6e not a hot-journ
13401 61 6c 20 66 69 6c 65 20 65 78 69 73 74 73 2c 20 al file exists,
13402 74 68 65 20 49 4f 20 65 72 72 6f 72 0a 2a 2a 20 the IO error.**
13403 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 code is returned
13404 20 61 6e 64 20 74 68 65 20 76 61 6c 75 65 20 6f and the value o
13405 66 20 2a 70 45 78 69 73 74 73 20 69 73 20 75 6e f *pExists is un
13406 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 defined..*/.stat
13407 69 63 20 69 6e 74 20 68 61 73 48 6f 74 4a 6f 75 ic int hasHotJou
13408 72 6e 61 6c 28 50 61 67 65 72 20 2a 70 50 61 67 rnal(Pager *pPag
13409 65 72 2c 20 69 6e 74 20 2a 70 45 78 69 73 74 73 er, int *pExists
1340a 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 ){. sqlite3_vfs
1340b 20 2a 20 63 6f 6e 73 74 20 70 56 66 73 20 3d 20 * const pVfs =
1340c 70 50 61 67 65 72 2d 3e 70 56 66 73 3b 0a 20 20 pPager->pVfs;.
1340d 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 int rc;
1340e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1340f 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a Return code */.
13410 20 20 69 6e 74 20 65 78 69 73 74 73 3b 20 20 20 int exists;
13411 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13412 2f 2a 20 54 72 75 65 20 69 66 20 61 20 6a 6f 75 /* True if a jou
13413 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 70 72 65 rnal file is pre
13414 73 65 6e 74 20 2a 2f 0a 0a 20 20 61 73 73 65 72 sent */.. asser
13415 74 28 20 70 50 61 67 65 72 21 3d 30 20 29 3b 0a t( pPager!=0 );.
13416 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 assert( pPager
13417 2d 3e 75 73 65 4a 6f 75 72 6e 61 6c 20 29 3b 0a ->useJournal );.
13418 20 20 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e assert( isOpen
13419 28 70 50 61 67 65 72 2d 3e 66 64 29 20 29 3b 0a (pPager->fd) );.
1341a 20 20 61 73 73 65 72 74 28 20 21 69 73 4f 70 65 assert( !isOpe
1341b 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 n(pPager->jfd) )
1341c 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 ;. assert( pPag
1341d 65 72 2d 3e 73 74 61 74 65 20 3c 3d 20 50 41 47 er->state <= PAG
1341e 45 52 5f 53 48 41 52 45 44 20 29 3b 0a 0a 20 20 ER_SHARED );..
1341f 2a 70 45 78 69 73 74 73 20 3d 20 30 3b 0a 20 20 *pExists = 0;.
13420 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 41 63 rc = sqlite3OsAc
13421 63 65 73 73 28 70 56 66 73 2c 20 70 50 61 67 65 cess(pVfs, pPage
13422 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 53 51 4c r->zJournal, SQL
13423 49 54 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54 ITE_ACCESS_EXIST
13424 53 2c 20 26 65 78 69 73 74 73 29 3b 0a 20 20 69 S, &exists);. i
13425 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
13426 20 26 26 20 65 78 69 73 74 73 20 29 7b 0a 20 20 && exists ){.
13427 20 20 69 6e 74 20 6c 6f 63 6b 65 64 3b 20 20 20 int locked;
13428 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
13429 20 54 72 75 65 20 69 66 20 73 6f 6d 65 20 70 72 True if some pr
1342a 6f 63 65 73 73 20 68 6f 6c 64 73 20 61 20 52 45 ocess holds a RE
1342b 53 45 52 56 45 44 20 6c 6f 63 6b 20 2a 2f 0a 0a SERVED lock */..
1342c 20 20 20 20 2f 2a 20 52 61 63 65 20 63 6f 6e 64 /* Race cond
1342d 69 74 69 6f 6e 20 68 65 72 65 3a 20 20 41 6e 6f ition here: Ano
1342e 74 68 65 72 20 70 72 6f 63 65 73 73 20 6d 69 67 ther process mig
1342f 68 74 20 68 61 76 65 20 62 65 65 6e 20 68 6f 6c ht have been hol
13430 64 69 6e 67 20 74 68 65 0a 20 20 20 20 2a 2a 20 ding the. **
13431 74 68 65 20 52 45 53 45 52 56 45 44 20 6c 6f 63 the RESERVED loc
13432 6b 20 61 6e 64 20 68 61 76 65 20 61 20 6a 6f 75 k and have a jou
13433 72 6e 61 6c 20 6f 70 65 6e 20 61 74 20 74 68 65 rnal open at the
13434 20 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73 73 sqlite3OsAccess
13435 28 29 20 0a 20 20 20 20 2a 2a 20 63 61 6c 6c 20 () . ** call
13436 61 62 6f 76 65 2c 20 62 75 74 20 74 68 65 6e 20 above, but then
13437 64 65 6c 65 74 65 20 74 68 65 20 6a 6f 75 72 6e delete the journ
13438 61 6c 20 61 6e 64 20 64 72 6f 70 20 74 68 65 20 al and drop the
13439 6c 6f 63 6b 20 62 65 66 6f 72 65 0a 20 20 20 20 lock before.
1343a 2a 2a 20 77 65 20 67 65 74 20 74 6f 20 74 68 65 ** we get to the
1343b 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 71 6c 69 74 following sqlit
1343c 65 33 4f 73 43 68 65 63 6b 52 65 73 65 72 76 65 e3OsCheckReserve
1343d 64 4c 6f 63 6b 28 29 20 63 61 6c 6c 2e 20 20 49 dLock() call. I
1343e 66 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 69 73 f that. ** is
1343f 20 74 68 65 20 63 61 73 65 2c 20 74 68 69 73 20 the case, this
13440 72 6f 75 74 69 6e 65 20 6d 69 67 68 74 20 74 68 routine might th
13441 69 6e 6b 20 74 68 65 72 65 20 69 73 20 61 20 68 ink there is a h
13442 6f 74 20 6a 6f 75 72 6e 61 6c 20 77 68 65 6e 0a ot journal when.
13443 20 20 20 20 2a 2a 20 69 6e 20 66 61 63 74 20 74 ** in fact t
13444 68 65 72 65 20 69 73 20 6e 6f 6e 65 2e 20 20 54 here is none. T
13445 68 69 73 20 72 65 73 75 6c 74 73 20 69 6e 20 61 his results in a
13446 20 66 61 6c 73 65 2d 70 6f 73 69 74 69 76 65 20 false-positive
13447 77 68 69 63 68 20 77 69 6c 6c 0a 20 20 20 20 2a which will. *
13448 2a 20 62 65 20 64 65 61 6c 74 20 77 69 74 68 20 * be dealt with
13449 62 79 20 74 68 65 20 70 6c 61 79 62 61 63 6b 20 by the playback
1344a 72 6f 75 74 69 6e 65 2e 20 20 54 69 63 6b 65 74 routine. Ticket
1344b 20 23 33 38 38 33 2e 0a 20 20 20 20 2a 2f 0a 20 #3883.. */.
1344c 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f rc = sqlite3O
1344d 73 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f sCheckReservedLo
1344e 63 6b 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 26 ck(pPager->fd, &
1344f 6c 6f 63 6b 65 64 29 3b 0a 20 20 20 20 69 66 28 locked);. if(
13450 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 rc==SQLITE_OK &
13451 26 20 21 6c 6f 63 6b 65 64 20 29 7b 0a 20 20 20 & !locked ){.
13452 20 20 20 69 6e 74 20 6e 50 61 67 65 3b 0a 0a 20 int nPage;..
13453 20 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 /* Check th
13454 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 e size of the da
13455 74 61 62 61 73 65 20 66 69 6c 65 2e 20 49 66 20 tabase file. If
13456 69 74 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 30 it consists of 0
13457 20 70 61 67 65 73 2c 0a 20 20 20 20 20 20 2a 2a pages,. **
13458 20 74 68 65 6e 20 64 65 6c 65 74 65 20 74 68 65 then delete the
13459 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 53 journal file. S
1345a 65 65 20 74 68 65 20 68 65 61 64 65 72 20 63 6f ee the header co
1345b 6d 6d 65 6e 74 20 61 62 6f 76 65 20 66 6f 72 20 mment above for
1345c 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 72 65 . ** the re
1345d 61 73 6f 6e 69 6e 67 20 68 65 72 65 2e 20 20 44 asoning here. D
1345e 65 6c 65 74 65 20 74 68 65 20 6f 62 73 6f 6c 65 elete the obsole
1345f 74 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 te journal file
13460 75 6e 64 65 72 0a 20 20 20 20 20 20 2a 2a 20 61 under. ** a
13461 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 74 RESERVED lock t
13462 6f 20 61 76 6f 69 64 20 72 61 63 65 20 63 6f 6e o avoid race con
13463 64 69 74 69 6f 6e 73 20 61 6e 64 20 74 6f 20 61 ditions and to a
13464 76 6f 69 64 20 76 69 6f 6c 61 74 69 6e 67 0a 20 void violating.
13465 20 20 20 20 20 2a 2a 20 5b 48 33 33 30 32 30 5d ** [H33020]
13466 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 .. */.
13467 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
13468 65 72 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67 erPagecount(pPag
13469 65 72 2c 20 26 6e 50 61 67 65 29 3b 0a 20 20 20 er, &nPage);.
1346a 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
1346b 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
1346c 69 66 28 20 6e 50 61 67 65 3d 3d 30 20 29 7b 0a if( nPage==0 ){.
1346d 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
1346e 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 3BeginBenignMall
1346f 6f 63 28 29 3b 0a 20 20 20 20 20 20 20 20 20 20 oc();.
13470 69 66 28 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 if( sqlite3OsLoc
13471 6b 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 52 45 k(pPager->fd, RE
13472 53 45 52 56 45 44 5f 4c 4f 43 4b 29 3d 3d 53 51 SERVED_LOCK)==SQ
13473 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
13474 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 sqlite3Os
13475 44 65 6c 65 74 65 28 70 56 66 73 2c 20 70 50 61 Delete(pVfs, pPa
13476 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 30 ger->zJournal, 0
13477 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 );. s
13478 71 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 70 qlite3OsUnlock(p
13479 50 61 67 65 72 2d 3e 66 64 2c 20 53 48 41 52 45 Pager->fd, SHARE
1347a 44 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 20 D_LOCK);.
1347b 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 73 }. s
1347c 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d qlite3EndBenignM
1347d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20 20 alloc();.
1347e 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
1347f 20 20 2f 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c /* The journal
13480 20 66 69 6c 65 20 65 78 69 73 74 73 20 61 6e 64 file exists and
13481 20 6e 6f 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63 no other connec
13482 74 69 6f 6e 20 68 61 73 20 61 20 72 65 73 65 72 tion has a reser
13483 76 65 64 0a 20 20 20 20 20 20 20 20 20 20 2a 2a ved. **
13484 20 6f 72 20 67 72 65 61 74 65 72 20 6c 6f 63 6b or greater lock
13485 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 on the database
13486 20 66 69 6c 65 2e 20 4e 6f 77 20 63 68 65 63 6b file. Now check
13487 20 74 68 61 74 20 74 68 65 72 65 20 69 73 0a 20 that there is.
13488 20 20 20 20 20 20 20 20 20 2a 2a 20 61 74 20 6c ** at l
13489 65 61 73 74 20 6f 6e 65 20 6e 6f 6e 2d 7a 65 72 east one non-zer
1348a 6f 20 62 79 74 65 73 20 61 74 20 74 68 65 20 73 o bytes at the s
1348b 74 61 72 74 20 6f 66 20 74 68 65 20 6a 6f 75 72 tart of the jour
1348c 6e 61 6c 20 66 69 6c 65 2e 0a 20 20 20 20 20 20 nal file..
1348d 20 20 20 20 2a 2a 20 49 66 20 74 68 65 72 65 20 ** If there
1348e 69 73 2c 20 74 68 65 6e 20 77 65 20 63 6f 6e 73 is, then we cons
1348f 69 64 65 72 20 74 68 69 73 20 6a 6f 75 72 6e 61 ider this journa
13490 6c 20 74 6f 20 62 65 20 68 6f 74 2e 20 49 66 20 l to be hot. If
13491 6e 6f 74 2c 20 0a 20 20 20 20 20 20 20 20 20 20 not, .
13492 2a 2a 20 69 74 20 63 61 6e 20 62 65 20 69 67 6e ** it can be ign
13493 6f 72 65 64 2e 0a 20 20 20 20 20 20 20 20 20 20 ored..
13494 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 */. int
13495 20 66 20 3d 20 53 51 4c 49 54 45 5f 4f 50 45 4e f = SQLITE_OPEN
13496 5f 52 45 41 44 4f 4e 4c 59 7c 53 51 4c 49 54 45 _READONLY|SQLITE
13497 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e _OPEN_MAIN_JOURN
13498 41 4c 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63 AL;. rc
13499 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e = sqlite3OsOpen
1349a 28 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a (pVfs, pPager->z
1349b 4a 6f 75 72 6e 61 6c 2c 20 70 50 61 67 65 72 2d Journal, pPager-
1349c 3e 6a 66 64 2c 20 66 2c 20 26 66 29 3b 0a 20 20 >jfd, f, &f);.
1349d 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d if( rc==
1349e 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
1349f 20 20 20 20 20 20 20 20 20 75 38 20 66 69 72 73 u8 firs
134a0 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 t = 0;.
134a1 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f rc = sqlite3O
134a2 73 52 65 61 64 28 70 50 61 67 65 72 2d 3e 6a 66 sRead(pPager->jf
134a3 64 2c 20 28 76 6f 69 64 20 2a 29 26 66 69 72 73 d, (void *)&firs
134a4 74 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 20 20 t, 1, 0);.
134a5 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 if( rc==SQ
134a6 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 LITE_IOERR_SHORT
134a7 5f 52 45 41 44 20 29 7b 0a 20 20 20 20 20 20 20 _READ ){.
134a8 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 rc = SQLI
134a9 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20 20 TE_OK;.
134aa 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 }.
134ab 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 sqlite3OsClose(
134ac 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 pPager->jfd);.
134ad 20 20 20 20 20 20 20 20 20 20 2a 70 45 78 69 73 *pExis
134ae 74 73 20 3d 20 28 66 69 72 73 74 21 3d 30 29 3b ts = (first!=0);
134af 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 . }else
134b0 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
134b1 43 41 4e 54 4f 50 45 4e 20 29 7b 0a 20 20 20 20 CANTOPEN ){.
134b2 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 77 65 /* If we
134b3 20 63 61 6e 6e 6f 74 20 6f 70 65 6e 20 74 68 65 cannot open the
134b4 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 rollback journa
134b5 6c 20 66 69 6c 65 20 69 6e 20 6f 72 64 65 72 20 l file in order
134b6 74 6f 20 73 65 65 20 69 66 0a 20 20 20 20 20 20 to see if.
134b7 20 20 20 20 20 20 2a 2a 20 69 74 73 20 68 61 73 ** its has
134b8 20 61 20 7a 65 72 6f 20 68 65 61 64 65 72 2c 20 a zero header,
134b9 74 68 61 74 20 6d 69 67 68 74 20 62 65 20 64 75 that might be du
134ba 65 20 74 6f 20 61 6e 20 49 2f 4f 20 65 72 72 6f e to an I/O erro
134bb 72 2c 20 6f 72 0a 20 20 20 20 20 20 20 20 20 20 r, or.
134bc 20 20 2a 2a 20 69 74 20 6d 69 67 68 74 20 62 65 ** it might be
134bd 20 64 75 65 20 74 6f 20 74 68 65 20 72 61 63 65 due to the race
134be 20 63 6f 6e 64 69 74 69 6f 6e 20 64 65 73 63 72 condition descr
134bf 69 62 65 64 20 61 62 6f 76 65 20 61 6e 64 20 69 ibed above and i
134c0 6e 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a n. **
134c1 20 74 69 63 6b 65 74 20 23 33 38 38 33 2e 20 20 ticket #3883.
134c2 45 69 74 68 65 72 20 77 61 79 2c 20 61 73 73 75 Either way, assu
134c3 6d 65 20 74 68 61 74 20 74 68 65 20 6a 6f 75 72 me that the jour
134c4 6e 61 6c 20 69 73 20 68 6f 74 2e 0a 20 20 20 20 nal is hot..
134c5 20 20 20 20 20 20 20 20 2a 2a 20 54 68 69 73 20 ** This
134c6 6d 69 67 68 74 20 62 65 20 61 20 66 61 6c 73 65 might be a false
134c7 20 70 6f 73 69 74 69 76 65 2e 20 20 42 75 74 20 positive. But
134c8 69 66 20 69 74 20 69 73 2c 20 74 68 65 6e 20 74 if it is, then t
134c9 68 65 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a he. *
134ca 2a 20 61 75 74 6f 6d 61 74 69 63 20 6a 6f 75 72 * automatic jour
134cb 6e 61 6c 20 70 6c 61 79 62 61 63 6b 20 61 6e 64 nal playback and
134cc 20 72 65 63 6f 76 65 72 79 20 6d 65 63 68 61 6e recovery mechan
134cd 69 73 6d 20 77 69 6c 6c 20 64 65 61 6c 0a 20 20 ism will deal.
134ce 20 20 20 20 20 20 20 20 20 20 2a 2a 20 77 69 74 ** wit
134cf 68 20 69 74 20 75 6e 64 65 72 20 61 6e 20 45 58 h it under an EX
134d0 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 77 68 65 CLUSIVE lock whe
134d1 72 65 20 77 65 20 64 6f 20 6e 6f 74 20 6e 65 65 re we do not nee
134d2 64 20 74 6f 0a 20 20 20 20 20 20 20 20 20 20 20 d to.
134d3 20 2a 2a 20 77 6f 72 72 79 20 73 6f 20 6d 75 63 ** worry so muc
134d4 68 20 77 69 74 68 20 72 61 63 65 20 63 6f 6e 64 h with race cond
134d5 69 74 69 6f 6e 73 2e 0a 20 20 20 20 20 20 20 20 itions..
134d6 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 */.
134d7 20 20 20 2a 70 45 78 69 73 74 73 20 3d 20 31 3b *pExists = 1;
134d8 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20 . rc
134d9 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 = SQLITE_OK;.
134da 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
134db 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d }. }. }
134dc 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 . }.. return r
134dd 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 c;.}../*.** Read
134de 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 66 6f 72 the content for
134df 20 70 61 67 65 20 70 50 67 20 6f 75 74 20 6f 66 page pPg out of
134e0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
134e1 6c 65 20 61 6e 64 20 69 6e 74 6f 20 0a 2a 2a 20 le and into .**
134e2 70 50 67 2d 3e 70 44 61 74 61 2e 20 41 20 73 68 pPg->pData. A sh
134e3 61 72 65 64 20 6c 6f 63 6b 20 6f 72 20 67 72 65 ared lock or gre
134e4 61 74 65 72 20 6d 75 73 74 20 62 65 20 68 65 6c ater must be hel
134e5 64 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 d on the databas
134e6 65 0a 2a 2a 20 66 69 6c 65 20 62 65 66 6f 72 65 e.** file before
134e7 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 this function i
134e8 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 s called..**.**
134e9 49 66 20 70 61 67 65 20 31 20 69 73 20 72 65 61 If page 1 is rea
134ea 64 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 d, then the valu
134eb 65 20 6f 66 20 50 61 67 65 72 2e 64 62 46 69 6c e of Pager.dbFil
134ec 65 56 65 72 73 5b 5d 20 69 73 20 73 65 74 20 74 eVers[] is set t
134ed 6f 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 72 o.** the value r
134ee 65 61 64 20 66 72 6f 6d 20 74 68 65 20 64 61 74 ead from the dat
134ef 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a abase file..**.*
134f0 2a 20 49 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 * If an IO error
134f1 20 6f 63 63 75 72 73 2c 20 74 68 65 6e 20 74 68 occurs, then th
134f2 65 20 49 4f 20 65 72 72 6f 72 20 69 73 20 72 65 e IO error is re
134f3 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20 63 61 turned to the ca
134f4 6c 6c 65 72 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 ller..** Otherwi
134f5 73 65 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 se, SQLITE_OK is
134f6 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 returned..*/.st
134f7 61 74 69 63 20 69 6e 74 20 72 65 61 64 44 62 50 atic int readDbP
134f8 61 67 65 28 50 67 48 64 72 20 2a 70 50 67 29 7b age(PgHdr *pPg){
134f9 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 . Pager *pPager
134fa 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 72 3b 20 = pPg->pPager;
134fb 2f 2a 20 50 61 67 65 72 20 6f 62 6a 65 63 74 20 /* Pager object
134fc 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
134fd 70 61 67 65 20 70 50 67 20 2a 2f 0a 20 20 50 67 page pPg */. Pg
134fe 6e 6f 20 70 67 6e 6f 20 3d 20 70 50 67 2d 3e 70 no pgno = pPg->p
134ff 67 6e 6f 3b 20 20 20 20 20 20 20 2f 2a 20 50 61 gno; /* Pa
13500 67 65 20 6e 75 6d 62 65 72 20 74 6f 20 72 65 61 ge number to rea
13501 64 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 d */. int rc;
13502 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13503 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f /* Return co
13504 64 65 20 2a 2f 0a 20 20 69 36 34 20 69 4f 66 66 de */. i64 iOff
13505 73 65 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 set;
13506 20 20 20 20 20 2f 2a 20 42 79 74 65 20 6f 66 66 /* Byte off
13507 73 65 74 20 6f 66 20 66 69 6c 65 20 74 6f 20 72 set of file to r
13508 65 61 64 20 66 72 6f 6d 20 2a 2f 0a 0a 20 20 61 ead from */.. a
13509 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 ssert( pPager->s
1350a 74 61 74 65 3e 3d 50 41 47 45 52 5f 53 48 41 52 tate>=PAGER_SHAR
1350b 45 44 20 26 26 20 21 4d 45 4d 44 42 20 29 3b 0a ED && !MEMDB );.
1350c 20 20 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e assert( isOpen
1350d 28 70 50 61 67 65 72 2d 3e 66 64 29 20 29 3b 0a (pPager->fd) );.
1350e 0a 20 20 69 66 28 20 4e 45 56 45 52 28 21 69 73 . if( NEVER(!is
1350f 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 Open(pPager->fd)
13510 29 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 ) ){. assert(
13511 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c pPager->tempFil
13512 65 20 29 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 e );. memset(
13513 70 50 67 2d 3e 70 44 61 74 61 2c 20 30 2c 20 70 pPg->pData, 0, p
13514 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 Pager->pageSize)
13515 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c ;. return SQL
13516 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 4f ITE_OK;. }. iO
13517 66 66 73 65 74 20 3d 20 28 70 67 6e 6f 2d 31 29 ffset = (pgno-1)
13518 2a 28 69 36 34 29 70 50 61 67 65 72 2d 3e 70 61 *(i64)pPager->pa
13519 67 65 53 69 7a 65 3b 0a 20 20 72 63 20 3d 20 73 geSize;. rc = s
1351a 71 6c 69 74 65 33 4f 73 52 65 61 64 28 70 50 61 qlite3OsRead(pPa
1351b 67 65 72 2d 3e 66 64 2c 20 70 50 67 2d 3e 70 44 ger->fd, pPg->pD
1351c 61 74 61 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 ata, pPager->pag
1351d 65 53 69 7a 65 2c 20 69 4f 66 66 73 65 74 29 3b eSize, iOffset);
1351e 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
1351f 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 E_IOERR_SHORT_RE
13520 41 44 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 AD ){. rc = S
13521 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 QLITE_OK;. }.
13522 69 66 28 20 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 if( pgno==1 ){.
13523 20 20 20 75 38 20 2a 64 62 46 69 6c 65 56 65 72 u8 *dbFileVer
13524 73 20 3d 20 26 28 28 75 38 2a 29 70 50 67 2d 3e s = &((u8*)pPg->
13525 70 44 61 74 61 29 5b 32 34 5d 3b 0a 20 20 20 20 pData)[24];.
13526 6d 65 6d 63 70 79 28 26 70 50 61 67 65 72 2d 3e memcpy(&pPager->
13527 64 62 46 69 6c 65 56 65 72 73 2c 20 64 62 46 69 dbFileVers, dbFi
13528 6c 65 56 65 72 73 2c 20 73 69 7a 65 6f 66 28 70 leVers, sizeof(p
13529 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 56 65 72 Pager->dbFileVer
1352a 73 29 29 3b 0a 20 20 7d 0a 20 20 43 4f 44 45 43 s));. }. CODEC
1352b 31 28 70 50 61 67 65 72 2c 20 70 50 67 2d 3e 70 1(pPager, pPg->p
1352c 44 61 74 61 2c 20 70 67 6e 6f 2c 20 33 2c 20 72 Data, pgno, 3, r
1352d 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d c = SQLITE_NOMEM
1352e 29 3b 0a 0a 20 20 50 41 47 45 52 5f 49 4e 43 52 );.. PAGER_INCR
1352f 28 73 71 6c 69 74 65 33 5f 70 61 67 65 72 5f 72 (sqlite3_pager_r
13530 65 61 64 64 62 5f 63 6f 75 6e 74 29 3b 0a 20 20 eaddb_count);.
13531 50 41 47 45 52 5f 49 4e 43 52 28 70 50 61 67 65 PAGER_INCR(pPage
13532 72 2d 3e 6e 52 65 61 64 29 3b 0a 20 20 49 4f 54 r->nRead);. IOT
13533 52 41 43 45 28 28 22 50 47 49 4e 20 25 70 20 25 RACE(("PGIN %p %
13534 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 67 d\n", pPager, pg
13535 6e 6f 29 29 3b 0a 20 20 50 41 47 45 52 54 52 41 no));. PAGERTRA
13536 43 45 28 28 22 46 45 54 43 48 20 25 64 20 70 61 CE(("FETCH %d pa
13537 67 65 20 25 64 20 68 61 73 68 28 25 30 38 78 29 ge %d hash(%08x)
13538 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 \n",.
13539 20 20 20 20 50 41 47 45 52 49 44 28 70 50 61 67 PAGERID(pPag
1353a 65 72 29 2c 20 70 67 6e 6f 2c 20 70 61 67 65 72 er), pgno, pager
1353b 5f 70 61 67 65 68 61 73 68 28 70 50 67 29 29 29 _pagehash(pPg)))
1353c 3b 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a ;.. return rc;.
1353d 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 }../*.** This fu
1353e 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 nction is called
1353f 20 74 6f 20 6f 62 74 61 69 6e 20 61 20 73 68 61 to obtain a sha
13540 72 65 64 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 red lock on the
13541 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a database file..*
13542 2a 20 49 74 20 69 73 20 69 6c 6c 65 67 61 6c 20 * It is illegal
13543 74 6f 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 50 to call sqlite3P
13544 61 67 65 72 41 63 71 75 69 72 65 28 29 20 75 6e agerAcquire() un
13545 74 69 6c 20 61 66 74 65 72 20 74 68 69 73 20 66 til after this f
13546 75 6e 63 74 69 6f 6e 0a 2a 2a 20 68 61 73 20 62 unction.** has b
13547 65 65 6e 20 73 75 63 63 65 73 73 66 75 6c 6c 79 een successfully
13548 20 63 61 6c 6c 65 64 2e 20 49 66 20 61 20 73 68 called. If a sh
13549 61 72 65 64 2d 6c 6f 63 6b 20 69 73 20 61 6c 72 ared-lock is alr
1354a 65 61 64 79 20 68 65 6c 64 20 77 68 65 6e 0a 2a eady held when.*
1354b 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * this function
1354c 69 73 20 63 61 6c 6c 65 64 2c 20 69 74 20 69 73 is called, it is
1354d 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 a no-op..**.**
1354e 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6f 70 The following op
1354f 65 72 61 74 69 6f 6e 73 20 61 72 65 20 61 6c 73 erations are als
13550 6f 20 70 65 72 66 6f 72 6d 65 64 20 62 79 20 74 o performed by t
13551 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a his function..**
13552 0a 2a 2a 20 20 20 31 29 20 49 66 20 74 68 65 20 .** 1) If the
13553 70 61 67 65 72 20 69 73 20 63 75 72 72 65 6e 74 pager is current
13554 6c 79 20 69 6e 20 50 41 47 45 52 5f 55 4e 4c 4f ly in PAGER_UNLO
13555 43 4b 20 73 74 61 74 65 20 28 6e 6f 20 6c 6f 63 CK state (no loc
13556 6b 20 68 65 6c 64 0a 2a 2a 20 20 20 20 20 20 6f k held.** o
13557 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 n the database f
13558 69 6c 65 29 2c 20 74 68 65 6e 20 61 6e 20 61 74 ile), then an at
13559 74 65 6d 70 74 20 69 73 20 6d 61 64 65 20 74 6f tempt is made to
1355a 20 6f 62 74 61 69 6e 20 61 0a 2a 2a 20 20 20 20 obtain a.**
1355b 20 20 53 48 41 52 45 44 20 6c 6f 63 6b 20 6f 6e SHARED lock on
1355c 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
1355d 6c 65 2e 20 49 6d 6d 65 64 69 61 74 65 6c 79 20 le. Immediately
1355e 61 66 74 65 72 20 6f 62 74 61 69 6e 69 6e 67 0a after obtaining.
1355f 2a 2a 20 20 20 20 20 20 74 68 65 20 53 48 41 52 ** the SHAR
13560 45 44 20 6c 6f 63 6b 2c 20 74 68 65 20 66 69 6c ED lock, the fil
13561 65 2d 73 79 73 74 65 6d 20 69 73 20 63 68 65 63 e-system is chec
13562 6b 65 64 20 66 6f 72 20 61 20 68 6f 74 2d 6a 6f ked for a hot-jo
13563 75 72 6e 61 6c 2c 0a 2a 2a 20 20 20 20 20 20 77 urnal,.** w
13564 68 69 63 68 20 69 73 20 70 6c 61 79 65 64 20 62 hich is played b
13565 61 63 6b 20 69 66 20 70 72 65 73 65 6e 74 2e 20 ack if present.
13566 46 6f 6c 6c 6f 77 69 6e 67 20 61 6e 79 20 68 6f Following any ho
13567 74 2d 6a 6f 75 72 6e 61 6c 20 0a 2a 2a 20 20 20 t-journal .**
13568 20 20 20 72 6f 6c 6c 62 61 63 6b 2c 20 74 68 65 rollback, the
13569 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 contents of the
1356a 20 63 61 63 68 65 20 61 72 65 20 76 61 6c 69 64 cache are valid
1356b 61 74 65 64 20 62 79 20 63 68 65 63 6b 69 6e 67 ated by checking
1356c 0a 2a 2a 20 20 20 20 20 20 74 68 65 20 27 63 68 .** the 'ch
1356d 61 6e 67 65 2d 63 6f 75 6e 74 65 72 27 20 66 69 ange-counter' fi
1356e 65 6c 64 20 6f 66 20 74 68 65 20 64 61 74 61 62 eld of the datab
1356f 61 73 65 20 66 69 6c 65 20 68 65 61 64 65 72 20 ase file header
13570 61 6e 64 0a 2a 2a 20 20 20 20 20 20 64 69 73 63 and.** disc
13571 61 72 64 65 64 20 69 66 20 74 68 65 79 20 61 72 arded if they ar
13572 65 20 66 6f 75 6e 64 20 74 6f 20 62 65 20 69 6e e found to be in
13573 76 61 6c 69 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 32 valid..**.** 2
13574 29 20 49 66 20 74 68 65 20 70 61 67 65 72 20 69 ) If the pager i
13575 73 20 72 75 6e 6e 69 6e 67 20 69 6e 20 65 78 63 s running in exc
13576 6c 75 73 69 76 65 2d 6d 6f 64 65 2c 20 61 6e 64 lusive-mode, and
13577 20 74 68 65 72 65 20 61 72 65 20 63 75 72 72 65 there are curre
13578 6e 74 6c 79 0a 2a 2a 20 20 20 20 20 20 6e 6f 20 ntly.** no
13579 6f 75 74 73 74 61 6e 64 69 6e 67 20 72 65 66 65 outstanding refe
1357a 72 65 6e 63 65 73 20 74 6f 20 61 6e 79 20 70 61 rences to any pa
1357b 67 65 73 2c 20 61 6e 64 20 69 73 20 69 6e 20 74 ges, and is in t
1357c 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 2c 0a he error state,.
1357d 2a 2a 20 20 20 20 20 20 74 68 65 6e 20 61 6e 20 ** then an
1357e 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65 20 attempt is made
1357f 74 6f 20 63 6c 65 61 72 20 74 68 65 20 65 72 72 to clear the err
13580 6f 72 20 73 74 61 74 65 20 62 79 20 64 69 73 63 or state by disc
13581 61 72 64 69 6e 67 0a 2a 2a 20 20 20 20 20 20 74 arding.** t
13582 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 he contents of t
13583 68 65 20 70 61 67 65 20 63 61 63 68 65 20 61 6e he page cache an
13584 64 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 61 d rolling back a
13585 6e 79 20 6f 70 65 6e 20 6a 6f 75 72 6e 61 6c 0a ny open journal.
13586 2a 2a 20 20 20 20 20 20 66 69 6c 65 2e 0a 2a 2a ** file..**
13587 0a 2a 2a 20 49 66 20 74 68 65 20 6f 70 65 72 61 .** If the opera
13588 74 69 6f 6e 20 64 65 73 63 72 69 62 65 64 20 62 tion described b
13589 79 20 28 32 29 20 61 62 6f 76 65 20 69 73 20 6e y (2) above is n
1358a 6f 74 20 61 74 74 65 6d 70 74 65 64 2c 20 61 6e ot attempted, an
1358b 64 20 69 66 20 74 68 65 0a 2a 2a 20 70 61 67 65 d if the.** page
1358c 72 20 69 73 20 69 6e 20 61 6e 20 65 72 72 6f 72 r is in an error
1358d 20 73 74 61 74 65 20 6f 74 68 65 72 20 74 68 61 state other tha
1358e 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 77 68 n SQLITE_FULL wh
1358f 65 6e 20 74 68 69 73 20 69 73 20 63 61 6c 6c 65 en this is calle
13590 64 2c 0a 2a 2a 20 74 68 65 20 65 72 72 6f 72 20 d,.** the error
13591 73 74 61 74 65 20 65 72 72 6f 72 20 63 6f 64 65 state error code
13592 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49 74 is returned. It
13593 20 69 73 20 70 65 72 6d 69 74 74 65 64 20 74 6f is permitted to
13594 20 72 65 61 64 20 74 68 65 0a 2a 2a 20 64 61 74 read the.** dat
13595 61 62 61 73 65 20 77 68 65 6e 20 69 6e 20 53 51 abase when in SQ
13596 4c 49 54 45 5f 46 55 4c 4c 20 65 72 72 6f 72 20 LITE_FULL error
13597 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 state..**.** Oth
13598 65 72 77 69 73 65 2c 20 69 66 20 65 76 65 72 79 erwise, if every
13599 74 68 69 6e 67 20 69 73 20 73 75 63 63 65 73 73 thing is success
1359a 66 75 6c 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 ful, SQLITE_OK i
1359b 73 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20 61 s returned. If a
1359c 6e 0a 2a 2a 20 49 4f 20 65 72 72 6f 72 20 6f 63 n.** IO error oc
1359d 63 75 72 73 20 77 68 69 6c 65 20 6c 6f 63 6b 69 curs while locki
1359e 6e 67 20 74 68 65 20 64 61 74 61 62 61 73 65 2c ng the database,
1359f 20 63 68 65 63 6b 69 6e 67 20 66 6f 72 20 61 20 checking for a
135a0 68 6f 74 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 66 hot-journal.** f
135a1 69 6c 65 20 6f 72 20 72 6f 6c 6c 69 6e 67 20 62 ile or rolling b
135a2 61 63 6b 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 ack a journal fi
135a3 6c 65 2c 20 74 68 65 20 49 4f 20 65 72 72 6f 72 le, the IO error
135a4 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 code is returne
135a5 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 d..*/.SQLITE_PRI
135a6 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
135a7 50 61 67 65 72 53 68 61 72 65 64 4c 6f 63 6b 28 PagerSharedLock(
135a8 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a Pager *pPager){.
135a9 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
135aa 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 E_OK;
135ab 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 /* Return c
135ac 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 69 73 45 ode */. int isE
135ad 72 72 6f 72 52 65 73 65 74 20 3d 20 30 3b 20 20 rrorReset = 0;
135ae 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
135af 72 75 65 20 69 66 20 72 65 63 6f 76 65 72 69 6e rue if recoverin
135b0 67 20 66 72 6f 6d 20 65 72 72 6f 72 20 73 74 61 g from error sta
135b1 74 65 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 69 73 te */.. /* This
135b2 20 72 6f 75 74 69 6e 65 20 69 73 20 6f 6e 6c 79 routine is only
135b3 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 62 2d 74 called from b-t
135b4 72 65 65 20 61 6e 64 20 6f 6e 6c 79 20 77 68 65 ree and only whe
135b5 6e 20 74 68 65 72 65 20 61 72 65 20 6e 6f 0a 20 n there are no.
135b6 20 2a 2a 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 ** outstanding
135b7 70 61 67 65 73 20 2a 2f 0a 20 20 61 73 73 65 72 pages */. asser
135b8 74 28 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 t( sqlite3Pcache
135b9 52 65 66 43 6f 75 6e 74 28 70 50 61 67 65 72 2d RefCount(pPager-
135ba 3e 70 50 43 61 63 68 65 29 3d 3d 30 20 29 3b 0a >pPCache)==0 );.
135bb 20 20 69 66 28 20 4e 45 56 45 52 28 4d 45 4d 44 if( NEVER(MEMD
135bc 42 20 26 26 20 70 50 61 67 65 72 2d 3e 65 72 72 B && pPager->err
135bd 43 6f 64 65 29 20 29 7b 20 72 65 74 75 72 6e 20 Code) ){ return
135be 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b pPager->errCode;
135bf 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 }.. /* If this
135c0 20 64 61 74 61 62 61 73 65 20 69 73 20 69 6e 20 database is in
135c1 61 6e 20 65 72 72 6f 72 2d 73 74 61 74 65 2c 20 an error-state,
135c2 6e 6f 77 20 69 73 20 61 20 63 68 61 6e 63 65 20 now is a chance
135c3 74 6f 20 63 6c 65 61 72 0a 20 20 2a 2a 20 74 68 to clear. ** th
135c4 65 20 65 72 72 6f 72 2e 20 44 69 73 63 61 72 64 e error. Discard
135c5 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 the contents of
135c6 20 74 68 65 20 70 61 67 65 72 2d 63 61 63 68 65 the pager-cache
135c7 20 61 6e 64 20 72 6f 6c 6c 62 61 63 6b 0a 20 20 and rollback.
135c8 2a 2a 20 61 6e 79 20 68 6f 74 20 6a 6f 75 72 6e ** any hot journ
135c9 61 6c 20 69 6e 20 74 68 65 20 66 69 6c 65 2d 73 al in the file-s
135ca 79 73 74 65 6d 2e 0a 20 20 2a 2f 0a 20 20 69 66 ystem.. */. if
135cb 28 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 ( pPager->errCod
135cc 65 20 29 7b 0a 20 20 20 20 69 66 28 20 69 73 4f e ){. if( isO
135cd 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 pen(pPager->jfd)
135ce 20 7c 7c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 || pPager->zJou
135cf 72 6e 61 6c 20 29 7b 0a 20 20 20 20 20 20 69 73 rnal ){. is
135d0 45 72 72 6f 72 52 65 73 65 74 20 3d 20 31 3b 0a ErrorReset = 1;.
135d1 20 20 20 20 7d 0a 20 20 20 20 70 50 61 67 65 72 }. pPager
135d2 2d 3e 65 72 72 43 6f 64 65 20 3d 20 53 51 4c 49 ->errCode = SQLI
135d3 54 45 5f 4f 4b 3b 0a 20 20 20 20 70 61 67 65 72 TE_OK;. pager
135d4 5f 72 65 73 65 74 28 70 50 61 67 65 72 29 3b 0a _reset(pPager);.
135d5 20 20 7d 0a 0a 20 20 69 66 28 20 70 50 61 67 65 }.. if( pPage
135d6 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 5f r->state==PAGER_
135d7 55 4e 4c 4f 43 4b 20 7c 7c 20 69 73 45 72 72 6f UNLOCK || isErro
135d8 72 52 65 73 65 74 20 29 7b 0a 20 20 20 20 73 71 rReset ){. sq
135d9 6c 69 74 65 33 5f 76 66 73 20 2a 20 63 6f 6e 73 lite3_vfs * cons
135da 74 20 70 56 66 73 20 3d 20 70 50 61 67 65 72 2d t pVfs = pPager-
135db 3e 70 56 66 73 3b 0a 20 20 20 20 69 6e 74 20 69 >pVfs;. int i
135dc 73 48 6f 74 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b sHotJournal = 0;
135dd 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 4d 45 . assert( !ME
135de 4d 44 42 20 29 3b 0a 20 20 20 20 61 73 73 65 72 MDB );. asser
135df 74 28 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 t( sqlite3Pcache
135e0 52 65 66 43 6f 75 6e 74 28 70 50 61 67 65 72 2d RefCount(pPager-
135e1 3e 70 50 43 61 63 68 65 29 3d 3d 30 20 29 3b 0a >pPCache)==0 );.
135e2 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e if( pPager->
135e3 6e 6f 52 65 61 64 6c 6f 63 6b 20 29 7b 0a 20 20 noReadlock ){.
135e4 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 assert( pPag
135e5 65 72 2d 3e 72 65 61 64 4f 6e 6c 79 20 29 3b 0a er->readOnly );.
135e6 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 pPager->st
135e7 61 74 65 20 3d 20 50 41 47 45 52 5f 53 48 41 52 ate = PAGER_SHAR
135e8 45 44 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ED;. }else{.
135e9 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f rc = pager_
135ea 77 61 69 74 5f 6f 6e 5f 6c 6f 63 6b 28 70 50 61 wait_on_lock(pPa
135eb 67 65 72 2c 20 53 48 41 52 45 44 5f 4c 4f 43 4b ger, SHARED_LOCK
135ec 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 );. if( rc!
135ed 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
135ee 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 assert( pP
135ef 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 ager->state==PAG
135f0 45 52 5f 55 4e 4c 4f 43 4b 20 29 3b 0a 20 20 20 ER_UNLOCK );.
135f1 20 20 20 20 20 72 65 74 75 72 6e 20 70 61 67 65 return page
135f2 72 5f 65 72 72 6f 72 28 70 50 61 67 65 72 2c 20 r_error(pPager,
135f3 72 63 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 rc);. }.
135f4 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 }. assert( p
135f5 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 53 48 Pager->state>=SH
135f6 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 0a 20 20 ARED_LOCK );..
135f7 20 20 2f 2a 20 49 66 20 61 20 6a 6f 75 72 6e 61 /* If a journa
135f8 6c 20 66 69 6c 65 20 65 78 69 73 74 73 2c 20 61 l file exists, a
135f9 6e 64 20 74 68 65 72 65 20 69 73 20 6e 6f 20 52 nd there is no R
135fa 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 6f 6e 20 ESERVED lock on
135fb 74 68 65 0a 20 20 20 20 2a 2a 20 64 61 74 61 62 the. ** datab
135fc 61 73 65 20 66 69 6c 65 2c 20 74 68 65 6e 20 69 ase file, then i
135fd 74 20 65 69 74 68 65 72 20 6e 65 65 64 73 20 74 t either needs t
135fe 6f 20 62 65 20 70 6c 61 79 65 64 20 62 61 63 6b o be played back
135ff 20 6f 72 20 64 65 6c 65 74 65 64 2e 0a 20 20 20 or deleted..
13600 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 69 73 45 */. if( !isE
13601 72 72 6f 72 52 65 73 65 74 20 29 7b 0a 20 20 20 rrorReset ){.
13602 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 assert( pPage
13603 72 2d 3e 73 74 61 74 65 20 3c 3d 20 50 41 47 45 r->state <= PAGE
13604 52 5f 53 48 41 52 45 44 20 29 3b 0a 20 20 20 20 R_SHARED );.
13605 20 20 72 63 20 3d 20 68 61 73 48 6f 74 4a 6f 75 rc = hasHotJou
13606 72 6e 61 6c 28 70 50 61 67 65 72 2c 20 26 69 73 rnal(pPager, &is
13607 48 6f 74 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 20 HotJournal);.
13608 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
13609 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
1360a 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20 20 goto failed;.
1360b 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 }. }. i
1360c 66 28 20 69 73 45 72 72 6f 72 52 65 73 65 74 20 f( isErrorReset
1360d 7c 7c 20 69 73 48 6f 74 4a 6f 75 72 6e 61 6c 20 || isHotJournal
1360e 29 7b 0a 20 20 20 20 20 20 2f 2a 20 47 65 74 20 ){. /* Get
1360f 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 an EXCLUSIVE loc
13610 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 k on the databas
13611 65 20 66 69 6c 65 2e 20 41 74 20 74 68 69 73 20 e file. At this
13612 70 6f 69 6e 74 20 69 74 20 69 73 0a 20 20 20 20 point it is.
13613 20 20 2a 2a 20 69 6d 70 6f 72 74 61 6e 74 20 74 ** important t
13614 68 61 74 20 61 20 52 45 53 45 52 56 45 44 20 6c hat a RESERVED l
13615 6f 63 6b 20 69 73 20 6e 6f 74 20 6f 62 74 61 69 ock is not obtai
13616 6e 65 64 20 6f 6e 20 74 68 65 20 77 61 79 20 74 ned on the way t
13617 6f 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 45 o the. ** E
13618 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 49 XCLUSIVE lock. I
13619 66 20 69 74 20 77 65 72 65 2c 20 61 6e 6f 74 68 f it were, anoth
1361a 65 72 20 70 72 6f 63 65 73 73 20 6d 69 67 68 74 er process might
1361b 20 6f 70 65 6e 20 74 68 65 0a 20 20 20 20 20 20 open the.
1361c 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 ** database file
1361d 2c 20 64 65 74 65 63 74 20 74 68 65 20 52 45 53 , detect the RES
1361e 45 52 56 45 44 20 6c 6f 63 6b 2c 20 61 6e 64 20 ERVED lock, and
1361f 63 6f 6e 63 6c 75 64 65 20 74 68 61 74 20 74 68 conclude that th
13620 65 0a 20 20 20 20 20 20 2a 2a 20 64 61 74 61 62 e. ** datab
13621 61 73 65 20 69 73 20 73 61 66 65 20 74 6f 20 72 ase is safe to r
13622 65 61 64 20 77 68 69 6c 65 20 74 68 69 73 20 70 ead while this p
13623 72 6f 63 65 73 73 20 69 73 20 73 74 69 6c 6c 20 rocess is still
13624 72 6f 6c 6c 69 6e 67 20 74 68 65 20 0a 20 20 20 rolling the .
13625 20 20 20 2a 2a 20 68 6f 74 2d 6a 6f 75 72 6e 61 ** hot-journa
13626 6c 20 62 61 63 6b 2e 0a 20 20 20 20 20 20 2a 2a l back.. **
13627 20 0a 20 20 20 20 20 20 2a 2a 20 42 65 63 61 75 . ** Becau
13628 73 65 20 74 68 65 20 69 6e 74 65 72 6d 65 64 69 se the intermedi
13629 61 74 65 20 52 45 53 45 52 56 45 44 20 6c 6f 63 ate RESERVED loc
1362a 6b 20 69 73 20 6e 6f 74 20 72 65 71 75 65 73 74 k is not request
1362b 65 64 2c 20 61 6e 79 0a 20 20 20 20 20 20 2a 2a ed, any. **
1362c 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20 61 other process a
1362d 74 74 65 6d 70 74 69 6e 67 20 74 6f 20 61 63 63 ttempting to acc
1362e 65 73 73 20 74 68 65 20 64 61 74 61 62 61 73 65 ess the database
1362f 20 66 69 6c 65 20 77 69 6c 6c 20 67 65 74 20 74 file will get t
13630 6f 20 0a 20 20 20 20 20 20 2a 2a 20 74 68 69 73 o . ** this
13631 20 70 6f 69 6e 74 20 69 6e 20 74 68 65 20 63 6f point in the co
13632 64 65 20 61 6e 64 20 66 61 69 6c 20 74 6f 20 6f de and fail to o
13633 62 74 61 69 6e 20 69 74 73 20 6f 77 6e 20 45 58 btain its own EX
13634 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 0a 20 20 CLUSIVE lock .
13635 20 20 20 20 2a 2a 20 6f 6e 20 74 68 65 20 64 61 ** on the da
13636 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 20 20 20 tabase file..
13637 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 */. if(
13638 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3c 45 58 pPager->state<EX
13639 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 29 7b 0a CLUSIVE_LOCK ){.
1363a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c rc = sql
1363b 69 74 65 33 4f 73 4c 6f 63 6b 28 70 50 61 67 65 ite3OsLock(pPage
1363c 72 2d 3e 66 64 2c 20 45 58 43 4c 55 53 49 56 45 r->fd, EXCLUSIVE
1363d 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 20 20 _LOCK);.
1363e 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
1363f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 K ){. r
13640 63 20 3d 20 70 61 67 65 72 5f 65 72 72 6f 72 28 c = pager_error(
13641 70 50 61 67 65 72 2c 20 72 63 29 3b 0a 20 20 20 pPager, rc);.
13642 20 20 20 20 20 20 20 67 6f 74 6f 20 66 61 69 6c goto fail
13643 65 64 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 ed;. }.
13644 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 pPager->st
13645 61 74 65 20 3d 20 50 41 47 45 52 5f 45 58 43 4c ate = PAGER_EXCL
13646 55 53 49 56 45 3b 0a 20 20 20 20 20 20 7d 0a 20 USIVE;. }.
13647 0a 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 74 . /* Open t
13648 68 65 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 20 72 he journal for r
13649 65 61 64 2f 77 72 69 74 65 20 61 63 63 65 73 73 ead/write access
1364a 2e 20 54 68 69 73 20 69 73 20 62 65 63 61 75 73 . This is becaus
1364b 65 20 69 6e 20 0a 20 20 20 20 20 20 2a 2a 20 65 e in . ** e
1364c 78 63 6c 75 73 69 76 65 2d 61 63 63 65 73 73 20 xclusive-access
1364d 6d 6f 64 65 20 74 68 65 20 66 69 6c 65 20 64 65 mode the file de
1364e 73 63 72 69 70 74 6f 72 20 77 69 6c 6c 20 62 65 scriptor will be
1364f 20 6b 65 70 74 20 6f 70 65 6e 20 61 6e 64 0a 20 kept open and.
13650 20 20 20 20 20 2a 2a 20 70 6f 73 73 69 62 6c 79 ** possibly
13651 20 75 73 65 64 20 66 6f 72 20 61 20 74 72 61 6e used for a tran
13652 73 61 63 74 69 6f 6e 20 6c 61 74 65 72 20 6f 6e saction later on
13653 2e 20 4f 6e 20 73 6f 6d 65 20 73 79 73 74 65 6d . On some system
13654 73 2c 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 s, the. **
13655 4f 73 54 72 75 6e 63 61 74 65 28 29 20 63 61 6c OsTruncate() cal
13656 6c 20 75 73 65 64 20 69 6e 20 65 78 63 6c 75 73 l used in exclus
13657 69 76 65 2d 61 63 63 65 73 73 20 6d 6f 64 65 20 ive-access mode
13658 61 6c 73 6f 20 72 65 71 75 69 72 65 73 0a 20 20 also requires.
13659 20 20 20 20 2a 2a 20 61 20 72 65 61 64 2f 77 72 ** a read/wr
1365a 69 74 65 20 66 69 6c 65 20 68 61 6e 64 6c 65 2e ite file handle.
1365b 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
1365c 69 66 28 20 21 69 73 4f 70 65 6e 28 70 50 61 67 if( !isOpen(pPag
1365d 65 72 2d 3e 6a 66 64 29 20 29 7b 0a 20 20 20 20 er->jfd) ){.
1365e 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20 int res;.
1365f 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
13660 33 4f 73 41 63 63 65 73 73 28 70 56 66 73 2c 70 3OsAccess(pVfs,p
13661 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c Pager->zJournal,
13662 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58 SQLITE_ACCESS_EX
13663 49 53 54 53 2c 26 72 65 73 29 3b 0a 20 20 20 20 ISTS,&res);.
13664 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
13665 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
13666 20 20 20 69 66 28 20 72 65 73 20 29 7b 0a 20 20 if( res ){.
13667 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 66 6f int fo
13668 75 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 ut = 0;.
13669 20 20 20 20 69 6e 74 20 66 20 3d 20 53 51 4c 49 int f = SQLI
1366a 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 TE_OPEN_READWRIT
1366b 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 E|SQLITE_OPEN_MA
1366c 49 4e 5f 4a 4f 55 52 4e 41 4c 3b 0a 20 20 20 20 IN_JOURNAL;.
1366d 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
1366e 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c !pPager->tempFil
1366f 65 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 e );.
13670 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f rc = sqlite3OsO
13671 70 65 6e 28 70 56 66 73 2c 20 70 50 61 67 65 72 pen(pVfs, pPager
13672 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 70 50 61 67 ->zJournal, pPag
13673 65 72 2d 3e 6a 66 64 2c 20 66 2c 20 26 66 6f 75 er->jfd, f, &fou
13674 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 t);.
13675 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 assert( rc!=SQLI
13676 54 45 5f 4f 4b 20 7c 7c 20 69 73 4f 70 65 6e 28 TE_OK || isOpen(
13677 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 0a pPager->jfd) );.
13678 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 if(
13679 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
1367a 20 66 6f 75 74 26 53 51 4c 49 54 45 5f 4f 50 45 fout&SQLITE_OPE
1367b 4e 5f 52 45 41 44 4f 4e 4c 59 20 29 7b 0a 20 20 N_READONLY ){.
1367c 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d rc =
1367d 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e SQLITE_CANTOPEN
1367e 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;.
1367f 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 sqlite3OsClose(p
13680 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 Pager->jfd);.
13681 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
13682 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
13683 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 /* If th
13684 65 20 6a 6f 75 72 6e 61 6c 20 64 6f 65 73 20 6e e journal does n
13685 6f 74 20 65 78 69 73 74 2c 20 69 74 20 75 73 75 ot exist, it usu
13686 61 6c 6c 79 20 6d 65 61 6e 73 20 74 68 61 74 20 ally means that
13687 73 6f 6d 65 20 0a 20 20 20 20 20 20 20 20 20 20 some .
13688 20 20 2a 2a 20 6f 74 68 65 72 20 63 6f 6e 6e 65 ** other conne
13689 63 74 69 6f 6e 20 6d 61 6e 61 67 65 64 20 74 6f ction managed to
1368a 20 67 65 74 20 69 6e 20 61 6e 64 20 72 6f 6c 6c get in and roll
1368b 20 69 74 20 62 61 63 6b 20 62 65 66 6f 72 65 20 it back before
1368c 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 . **
1368d 74 68 69 73 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 this connection
1368e 6f 62 74 61 69 6e 65 64 20 74 68 65 20 65 78 63 obtained the exc
1368f 6c 75 73 69 76 65 20 6c 6f 63 6b 20 61 62 6f 76 lusive lock abov
13690 65 2e 20 4f 72 2c 20 69 74 20 0a 20 20 20 20 20 e. Or, it .
13691 20 20 20 20 20 20 20 2a 2a 20 6d 61 79 20 6d 65 ** may me
13692 61 6e 20 74 68 61 74 20 74 68 65 20 70 61 67 65 an that the page
13693 72 20 77 61 73 20 69 6e 20 74 68 65 20 65 72 72 r was in the err
13694 6f 72 2d 73 74 61 74 65 20 77 68 65 6e 20 74 68 or-state when th
13695 69 73 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a is. *
13696 2a 20 66 75 6e 63 74 69 6f 6e 20 77 61 73 20 63 * function was c
13697 61 6c 6c 65 64 20 61 6e 64 20 74 68 65 20 6a 6f alled and the jo
13698 75 72 6e 61 6c 20 66 69 6c 65 20 64 6f 65 73 20 urnal file does
13699 6e 6f 74 20 65 78 69 73 74 2e 20 20 2a 2f 0a 20 not exist. */.
1369a 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 rc =
1369b 70 61 67 65 72 5f 65 6e 64 5f 74 72 61 6e 73 61 pager_end_transa
1369c 63 74 69 6f 6e 28 70 50 61 67 65 72 2c 20 30 29 ction(pPager, 0)
1369d 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
1369e 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
1369f 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 if( rc!=SQ
136a0 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
136a1 20 20 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a goto failed;.
136a2 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f }.. /
136a3 2a 20 54 4f 44 4f 3a 20 57 68 79 20 61 72 65 20 * TODO: Why are
136a4 74 68 65 73 65 20 63 6c 65 61 72 65 64 20 68 65 these cleared he
136a5 72 65 3f 20 49 73 20 69 74 20 6e 65 63 65 73 73 re? Is it necess
136a6 61 72 79 3f 20 2a 2f 0a 20 20 20 20 20 20 70 50 ary? */. pP
136a7 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 74 61 ager->journalSta
136a8 72 74 65 64 20 3d 20 30 3b 0a 20 20 20 20 20 20 rted = 0;.
136a9 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
136aa 66 66 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 50 ff = 0;. pP
136ab 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 20 ager->setMaster
136ac 3d 20 30 3b 0a 20 20 20 20 20 20 70 50 61 67 65 = 0;. pPage
136ad 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 3d 20 r->journalHdr =
136ae 30 3b 0a 20 0a 20 20 20 20 20 20 2f 2a 20 50 6c 0;. . /* Pl
136af 61 79 62 61 63 6b 20 61 6e 64 20 64 65 6c 65 74 ayback and delet
136b0 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 e the journal.
136b1 44 72 6f 70 20 74 68 65 20 64 61 74 61 62 61 73 Drop the databas
136b2 65 20 77 72 69 74 65 0a 20 20 20 20 20 20 2a 2a e write. **
136b3 20 6c 6f 63 6b 20 61 6e 64 20 72 65 61 63 71 75 lock and reacqu
136b4 69 72 65 20 74 68 65 20 72 65 61 64 20 6c 6f 63 ire the read loc
136b5 6b 2e 20 50 75 72 67 65 20 74 68 65 20 63 61 63 k. Purge the cac
136b6 68 65 20 62 65 66 6f 72 65 0a 20 20 20 20 20 20 he before.
136b7 2a 2a 20 70 6c 61 79 69 6e 67 20 62 61 63 6b 20 ** playing back
136b8 74 68 65 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 the hot-journal
136b9 73 6f 20 74 68 61 74 20 77 65 20 64 6f 6e 27 74 so that we don't
136ba 20 65 6e 64 20 75 70 20 77 69 74 68 0a 20 20 20 end up with.
136bb 20 20 20 2a 2a 20 61 6e 20 69 6e 63 6f 6e 73 69 ** an inconsi
136bc 73 74 65 6e 74 20 63 61 63 68 65 2e 0a 20 20 20 stent cache..
136bd 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 */. if(
136be 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a isOpen(pPager->j
136bf 66 64 29 20 29 7b 0a 20 20 20 20 20 20 20 20 72 fd) ){. r
136c0 63 20 3d 20 70 61 67 65 72 5f 70 6c 61 79 62 61 c = pager_playba
136c1 63 6b 28 70 50 61 67 65 72 2c 20 31 29 3b 0a 20 ck(pPager, 1);.
136c2 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 if( rc!=S
136c3 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
136c4 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 rc = pager
136c5 5f 65 72 72 6f 72 28 70 50 61 67 65 72 2c 20 72 _error(pPager, r
136c6 63 29 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f c);. go
136c7 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 20 to failed;.
136c8 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
136c9 20 20 20 61 73 73 65 72 74 28 20 28 70 50 61 67 assert( (pPag
136ca 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 er->state==PAGER
136cb 5f 53 48 41 52 45 44 29 0a 20 20 20 20 20 20 20 _SHARED).
136cc 20 20 20 20 7c 7c 20 28 70 50 61 67 65 72 2d 3e || (pPager->
136cd 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 26 26 exclusiveMode &&
136ce 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 50 pPager->state>P
136cf 41 47 45 52 5f 53 48 41 52 45 44 29 0a 20 20 20 AGER_SHARED).
136d0 20 20 20 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 );. }..
136d1 20 69 66 28 20 70 50 61 67 65 72 2d 3e 70 42 61 if( pPager->pBa
136d2 63 6b 75 70 20 7c 7c 20 73 71 6c 69 74 65 33 50 ckup || sqlite3P
136d3 63 61 63 68 65 50 61 67 65 63 6f 75 6e 74 28 70 cachePagecount(p
136d4 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3e Pager->pPCache)>
136d5 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 0 ){. /* Th
136d6 65 20 73 68 61 72 65 64 2d 6c 6f 63 6b 20 68 61 e shared-lock ha
136d7 73 20 6a 75 73 74 20 62 65 65 6e 20 61 63 71 75 s just been acqu
136d8 69 72 65 64 20 6f 6e 20 74 68 65 20 64 61 74 61 ired on the data
136d9 62 61 73 65 20 66 69 6c 65 0a 20 20 20 20 20 20 base file.
136da 2a 2a 20 61 6e 64 20 74 68 65 72 65 20 61 72 65 ** and there are
136db 20 61 6c 72 65 61 64 79 20 70 61 67 65 73 20 69 already pages i
136dc 6e 20 74 68 65 20 63 61 63 68 65 20 28 66 72 6f n the cache (fro
136dd 6d 20 61 20 70 72 65 76 69 6f 75 73 0a 20 20 20 m a previous.
136de 20 20 20 2a 2a 20 72 65 61 64 20 6f 72 20 77 72 ** read or wr
136df 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 29 ite transaction)
136e0 2e 20 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 . Check to see
136e1 69 66 20 74 68 65 20 64 61 74 61 62 61 73 65 0a if the database.
136e2 20 20 20 20 20 20 2a 2a 20 68 61 73 20 62 65 65 ** has bee
136e3 6e 20 6d 6f 64 69 66 69 65 64 2e 20 20 49 66 20 n modified. If
136e4 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61 73 the database has
136e5 20 63 68 61 6e 67 65 64 2c 20 66 6c 75 73 68 20 changed, flush
136e6 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 63 61 63 the. ** cac
136e7 68 65 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 he.. **.
136e8 20 20 20 2a 2a 20 44 61 74 61 62 61 73 65 20 63 ** Database c
136e9 68 61 6e 67 65 73 20 69 73 20 64 65 74 65 63 74 hanges is detect
136ea 65 64 20 62 79 20 6c 6f 6f 6b 69 6e 67 20 61 74 ed by looking at
136eb 20 31 35 20 62 79 74 65 73 20 62 65 67 69 6e 6e 15 bytes beginn
136ec 69 6e 67 0a 20 20 20 20 20 20 2a 2a 20 61 74 20 ing. ** at
136ed 6f 66 66 73 65 74 20 32 34 20 69 6e 74 6f 20 74 offset 24 into t
136ee 68 65 20 66 69 6c 65 2e 20 20 54 68 65 20 66 69 he file. The fi
136ef 72 73 74 20 34 20 6f 66 20 74 68 65 73 65 20 31 rst 4 of these 1
136f0 36 20 62 79 74 65 73 20 61 72 65 0a 20 20 20 20 6 bytes are.
136f1 20 20 2a 2a 20 61 20 33 32 2d 62 69 74 20 63 6f ** a 32-bit co
136f2 75 6e 74 65 72 20 74 68 61 74 20 69 73 20 69 6e unter that is in
136f3 63 72 65 6d 65 6e 74 65 64 20 77 69 74 68 20 65 cremented with e
136f4 61 63 68 20 63 68 61 6e 67 65 2e 20 20 54 68 65 ach change. The
136f5 0a 20 20 20 20 20 20 2a 2a 20 6f 74 68 65 72 20 . ** other
136f6 62 79 74 65 73 20 63 68 61 6e 67 65 20 72 61 6e bytes change ran
136f7 64 6f 6d 6c 79 20 77 69 74 68 20 65 61 63 68 20 domly with each
136f8 66 69 6c 65 20 63 68 61 6e 67 65 20 77 68 65 6e file change when
136f9 0a 20 20 20 20 20 20 2a 2a 20 61 20 63 6f 64 65 . ** a code
136fa 63 20 69 73 20 69 6e 20 75 73 65 2e 0a 20 20 20 c is in use..
136fb 20 20 20 2a 2a 20 0a 20 20 20 20 20 20 2a 2a 20 ** . **
136fc 54 68 65 72 65 20 69 73 20 61 20 76 61 6e 69 73 There is a vanis
136fd 68 69 6e 67 6c 79 20 73 6d 61 6c 6c 20 63 68 61 hingly small cha
136fe 6e 63 65 20 74 68 61 74 20 61 20 63 68 61 6e 67 nce that a chang
136ff 65 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 0a 20 e will not be .
13700 20 20 20 20 20 2a 2a 20 64 65 74 65 63 74 65 64 ** detected
13701 2e 20 20 54 68 65 20 63 68 61 6e 63 65 20 6f 66 . The chance of
13702 20 61 6e 20 75 6e 64 65 74 65 63 74 65 64 20 63 an undetected c
13703 68 61 6e 67 65 20 69 73 20 73 6f 20 73 6d 61 6c hange is so smal
13704 6c 20 74 68 61 74 0a 20 20 20 20 20 20 2a 2a 20 l that. **
13705 69 74 20 63 61 6e 20 62 65 20 6e 65 67 6c 65 63 it can be neglec
13706 74 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 ted.. */.
13707 20 20 20 20 63 68 61 72 20 64 62 46 69 6c 65 56 char dbFileV
13708 65 72 73 5b 73 69 7a 65 6f 66 28 70 50 61 67 65 ers[sizeof(pPage
13709 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 29 5d 3b r->dbFileVers)];
1370a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 . sqlite3Pa
1370b 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 50 61 gerPagecount(pPa
1370c 67 65 72 2c 20 30 29 3b 0a 0a 20 20 20 20 20 20 ger, 0);..
1370d 69 66 28 20 70 50 61 67 65 72 2d 3e 65 72 72 43 if( pPager->errC
1370e 6f 64 65 20 29 7b 0a 20 20 20 20 20 20 20 20 72 ode ){. r
1370f 63 20 3d 20 70 50 61 67 65 72 2d 3e 65 72 72 43 c = pPager->errC
13710 6f 64 65 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 ode;. got
13711 6f 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 20 20 o failed;.
13712 7d 0a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 }.. assert(
13713 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 pPager->dbSizeV
13714 61 6c 69 64 20 29 3b 0a 20 20 20 20 20 20 69 66 alid );. if
13715 28 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 ( pPager->dbSize
13716 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 49 4f >0 ){. IO
13717 54 52 41 43 45 28 28 22 43 4b 56 45 52 53 20 25 TRACE(("CKVERS %
13718 70 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c p %d\n", pPager,
13719 20 73 69 7a 65 6f 66 28 64 62 46 69 6c 65 56 65 sizeof(dbFileVe
1371a 72 73 29 29 29 3b 0a 20 20 20 20 20 20 20 20 72 rs)));. r
1371b 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 c = sqlite3OsRea
1371c 64 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 26 64 d(pPager->fd, &d
1371d 62 46 69 6c 65 56 65 72 73 2c 20 73 69 7a 65 6f bFileVers, sizeo
1371e 66 28 64 62 46 69 6c 65 56 65 72 73 29 2c 20 32 f(dbFileVers), 2
1371f 34 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 4);. if(
13720 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
13721 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 . goto
13722 66 61 69 6c 65 64 3b 0a 20 20 20 20 20 20 20 20 failed;.
13723 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }. }else{.
13724 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28 64 62 memset(db
13725 46 69 6c 65 56 65 72 73 2c 20 30 2c 20 73 69 7a FileVers, 0, siz
13726 65 6f 66 28 64 62 46 69 6c 65 56 65 72 73 29 29 eof(dbFileVers))
13727 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 ;. }..
13728 20 69 66 28 20 6d 65 6d 63 6d 70 28 70 50 61 67 if( memcmp(pPag
13729 65 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 2c 20 er->dbFileVers,
1372a 64 62 46 69 6c 65 56 65 72 73 2c 20 73 69 7a 65 dbFileVers, size
1372b 6f 66 28 64 62 46 69 6c 65 56 65 72 73 29 29 21 of(dbFileVers))!
1372c 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 61 =0 ){. pa
1372d 67 65 72 5f 72 65 73 65 74 28 70 50 61 67 65 72 ger_reset(pPager
1372e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d );. }. }
1372f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 . assert( pPa
13730 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f ger->exclusiveMo
13731 64 65 20 7c 7c 20 70 50 61 67 65 72 2d 3e 73 74 de || pPager->st
13732 61 74 65 3d 3d 50 41 47 45 52 5f 53 48 41 52 45 ate==PAGER_SHARE
13733 44 20 29 3b 0a 20 20 7d 0a 0a 20 66 61 69 6c 65 D );. }.. faile
13734 64 3a 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c d:. if( rc!=SQL
13735 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2f 2a ITE_OK ){. /*
13736 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 29 20 pager_unlock()
13737 69 73 20 61 20 6e 6f 2d 6f 70 20 66 6f 72 20 65 is a no-op for e
13738 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 20 61 6e xclusive mode an
13739 64 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 d in-memory data
1373a 62 61 73 65 73 2e 20 2a 2f 0a 20 20 20 20 70 61 bases. */. pa
1373b 67 65 72 5f 75 6e 6c 6f 63 6b 28 70 50 61 67 65 ger_unlock(pPage
1373c 72 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e r);. }. return
1373d 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 rc;.}../*.** If
1373e 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 63 the reference c
1373f 6f 75 6e 74 20 68 61 73 20 72 65 61 63 68 65 64 ount has reached
13740 20 7a 65 72 6f 2c 20 72 6f 6c 6c 62 61 63 6b 20 zero, rollback
13741 61 6e 79 20 61 63 74 69 76 65 0a 2a 2a 20 74 72 any active.** tr
13742 61 6e 73 61 63 74 69 6f 6e 20 61 6e 64 20 75 6e ansaction and un
13743 6c 6f 63 6b 20 74 68 65 20 70 61 67 65 72 2e 0a lock the pager..
13744 2a 2a 0a 2a 2a 20 45 78 63 65 70 74 2c 20 69 6e **.** Except, in
13745 20 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65 3d 45 58 locking_mode=EX
13746 43 4c 55 53 49 56 45 20 77 68 65 6e 20 74 68 65 CLUSIVE when the
13747 72 65 20 69 73 20 6e 6f 74 68 69 6e 67 20 74 6f re is nothing to
13748 20 69 6e 0a 2a 2a 20 74 68 65 20 72 6f 6c 6c 62 in.** the rollb
13749 61 63 6b 20 6a 6f 75 72 6e 61 6c 2c 20 74 68 65 ack journal, the
1374a 20 75 6e 6c 6f 63 6b 20 69 73 20 6e 6f 74 20 70 unlock is not p
1374b 65 72 66 6f 72 6d 65 64 20 61 6e 64 20 74 68 65 erformed and the
1374c 72 65 20 69 73 0a 2a 2a 20 6e 6f 74 68 69 6e 67 re is.** nothing
1374d 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 2c 20 73 6f to rollback, so
1374e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 this routine is
1374f 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 20 0a 73 74 a no-op..*/ .st
13750 61 74 69 63 20 76 6f 69 64 20 70 61 67 65 72 55 atic void pagerU
13751 6e 6c 6f 63 6b 49 66 55 6e 75 73 65 64 28 50 61 nlockIfUnused(Pa
13752 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 ger *pPager){.
13753 69 66 28 20 28 73 71 6c 69 74 65 33 50 63 61 63 if( (sqlite3Pcac
13754 68 65 52 65 66 43 6f 75 6e 74 28 70 50 61 67 65 heRefCount(pPage
13755 72 2d 3e 70 50 43 61 63 68 65 29 3d 3d 30 29 0a r->pPCache)==0).
13756 20 20 20 26 26 20 28 21 70 50 61 67 65 72 2d 3e && (!pPager->
13757 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 7c 7c exclusiveMode ||
13758 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
13759 4f 66 66 3e 30 29 20 0a 20 20 29 7b 0a 20 20 20 Off>0) . ){.
1375a 20 70 61 67 65 72 55 6e 6c 6f 63 6b 41 6e 64 52 pagerUnlockAndR
1375b 6f 6c 6c 62 61 63 6b 28 70 50 61 67 65 72 29 3b ollback(pPager);
1375c 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 63 . }.}../*.** Ac
1375d 71 75 69 72 65 20 61 20 72 65 66 65 72 65 6e 63 quire a referenc
1375e 65 20 74 6f 20 70 61 67 65 20 6e 75 6d 62 65 72 e to page number
1375f 20 70 67 6e 6f 20 69 6e 20 70 61 67 65 72 20 70 pgno in pager p
13760 50 61 67 65 72 20 28 61 20 70 61 67 65 0a 2a 2a Pager (a page.**
13761 20 72 65 66 65 72 65 6e 63 65 20 68 61 73 20 74 reference has t
13762 79 70 65 20 44 62 50 61 67 65 2a 29 2e 20 49 66 ype DbPage*). If
13763 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 72 the requested r
13764 65 66 65 72 65 6e 63 65 20 69 73 20 0a 2a 2a 20 eference is .**
13765 73 75 63 63 65 73 73 66 75 6c 6c 79 20 6f 62 74 successfully obt
13766 61 69 6e 65 64 2c 20 69 74 20 69 73 20 63 6f 70 ained, it is cop
13767 69 65 64 20 74 6f 20 2a 70 70 50 61 67 65 20 61 ied to *ppPage a
13768 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 nd SQLITE_OK ret
13769 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 urned..**.** If
1376a 74 68 65 20 72 65 71 75 65 73 74 65 64 20 70 61 the requested pa
1376b 67 65 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e ge is already in
1376c 20 74 68 65 20 63 61 63 68 65 2c 20 69 74 20 69 the cache, it i
1376d 73 20 72 65 74 75 72 6e 65 64 2e 20 0a 2a 2a 20 s returned. .**
1376e 4f 74 68 65 72 77 69 73 65 2c 20 61 20 6e 65 77 Otherwise, a new
1376f 20 70 61 67 65 20 6f 62 6a 65 63 74 20 69 73 20 page object is
13770 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 70 6f allocated and po
13771 70 75 6c 61 74 65 64 20 77 69 74 68 20 64 61 74 pulated with dat
13772 61 0a 2a 2a 20 72 65 61 64 20 66 72 6f 6d 20 74 a.** read from t
13773 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
13774 2e 20 49 6e 20 73 6f 6d 65 20 63 61 73 65 73 2c . In some cases,
13775 20 74 68 65 20 70 63 61 63 68 65 20 6d 6f 64 75 the pcache modu
13776 6c 65 20 6d 61 79 0a 2a 2a 20 63 68 6f 6f 73 65 le may.** choose
13777 20 6e 6f 74 20 74 6f 20 61 6c 6c 6f 63 61 74 65 not to allocate
13778 20 61 20 6e 65 77 20 70 61 67 65 20 6f 62 6a 65 a new page obje
13779 63 74 20 61 6e 64 20 6d 61 79 20 72 65 75 73 65 ct and may reuse
1377a 20 61 6e 20 65 78 69 73 74 69 6e 67 0a 2a 2a 20 an existing.**
1377b 6f 62 6a 65 63 74 20 77 69 74 68 20 6e 6f 20 6f object with no o
1377c 75 74 73 74 61 6e 64 69 6e 67 20 72 65 66 65 72 utstanding refer
1377d 65 6e 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ences..**.** The
1377e 20 65 78 74 72 61 20 64 61 74 61 20 61 70 70 65 extra data appe
1377f 6e 64 65 64 20 74 6f 20 61 20 70 61 67 65 20 69 nded to a page i
13780 73 20 61 6c 77 61 79 73 20 69 6e 69 74 69 61 6c s always initial
13781 69 7a 65 64 20 74 6f 20 7a 65 72 6f 73 20 74 68 ized to zeros th
13782 65 20 0a 2a 2a 20 66 69 72 73 74 20 74 69 6d 65 e .** first time
13783 20 61 20 70 61 67 65 20 69 73 20 6c 6f 61 64 65 a page is loade
13784 64 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 2e 20 49 d into memory. I
13785 66 20 74 68 65 20 70 61 67 65 20 72 65 71 75 65 f the page reque
13786 73 74 65 64 20 69 73 20 0a 2a 2a 20 61 6c 72 65 sted is .** alre
13787 61 64 79 20 69 6e 20 74 68 65 20 63 61 63 68 65 ady in the cache
13788 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 when this funct
13789 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2c 20 74 ion is called, t
1378a 68 65 6e 20 74 68 65 20 65 78 74 72 61 0a 2a 2a hen the extra.**
1378b 20 64 61 74 61 20 69 73 20 6c 65 66 74 20 61 73 data is left as
1378c 20 69 74 20 77 61 73 20 77 68 65 6e 20 74 68 65 it was when the
1378d 20 70 61 67 65 20 6f 62 6a 65 63 74 20 77 61 73 page object was
1378e 20 6c 61 73 74 20 75 73 65 64 2e 0a 2a 2a 0a 2a last used..**.*
1378f 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73 * If the databas
13790 65 20 69 6d 61 67 65 20 69 73 20 73 6d 61 6c 6c e image is small
13791 65 72 20 74 68 61 6e 20 74 68 65 20 72 65 71 75 er than the requ
13792 65 73 74 65 64 20 70 61 67 65 20 6f 72 20 69 66 ested page or if
13793 20 61 20 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 20 a .** non-zero
13794 76 61 6c 75 65 20 69 73 20 70 61 73 73 65 64 20 value is passed
13795 61 73 20 74 68 65 20 6e 6f 43 6f 6e 74 65 6e 74 as the noContent
13796 20 70 61 72 61 6d 65 74 65 72 20 61 6e 64 20 74 parameter and t
13797 68 65 20 0a 2a 2a 20 72 65 71 75 65 73 74 65 64 he .** requested
13798 20 70 61 67 65 20 69 73 20 6e 6f 74 20 61 6c 72 page is not alr
13799 65 61 64 79 20 73 74 6f 72 65 64 20 69 6e 20 74 eady stored in t
1379a 68 65 20 63 61 63 68 65 2c 20 74 68 65 6e 20 6e he cache, then n
1379b 6f 20 0a 2a 2a 20 61 63 74 75 61 6c 20 64 69 73 o .** actual dis
1379c 6b 20 72 65 61 64 20 6f 63 63 75 72 73 2e 20 49 k read occurs. I
1379d 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65 20 n this case the
1379e 6d 65 6d 6f 72 79 20 69 6d 61 67 65 20 6f 66 20 memory image of
1379f 74 68 65 20 0a 2a 2a 20 70 61 67 65 20 69 73 20 the .** page is
137a0 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f 20 61 initialized to a
137a1 6c 6c 20 7a 65 72 6f 73 2e 20 0a 2a 2a 0a 2a 2a ll zeros. .**.**
137a2 20 49 66 20 6e 6f 43 6f 6e 74 65 6e 74 20 69 73 If noContent is
137a3 20 74 72 75 65 2c 20 69 74 20 6d 65 61 6e 73 20 true, it means
137a4 74 68 61 74 20 77 65 20 64 6f 20 6e 6f 74 20 63 that we do not c
137a5 61 72 65 20 61 62 6f 75 74 20 74 68 65 20 63 6f are about the co
137a6 6e 74 65 6e 74 73 0a 2a 2a 20 6f 66 20 74 68 65 ntents.** of the
137a7 20 70 61 67 65 2e 20 54 68 69 73 20 6f 63 63 75 page. This occu
137a8 72 73 20 69 6e 20 74 77 6f 20 73 65 70 65 72 61 rs in two sepera
137a9 74 65 20 73 63 65 6e 61 72 69 6f 73 3a 0a 2a 2a te scenarios:.**
137aa 0a 2a 2a 20 20 20 61 29 20 57 68 65 6e 20 72 65 .** a) When re
137ab 61 64 69 6e 67 20 61 20 66 72 65 65 2d 6c 69 73 ading a free-lis
137ac 74 20 6c 65 61 66 20 70 61 67 65 20 66 72 6f 6d t leaf page from
137ad 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20 61 the database, a
137ae 6e 64 0a 2a 2a 0a 2a 2a 20 20 20 62 29 20 57 68 nd.**.** b) Wh
137af 65 6e 20 61 20 73 61 76 65 70 6f 69 6e 74 20 69 en a savepoint i
137b0 73 20 62 65 69 6e 67 20 72 6f 6c 6c 65 64 20 62 s being rolled b
137b1 61 63 6b 20 61 6e 64 20 77 65 20 6e 65 65 64 20 ack and we need
137b2 74 6f 20 6c 6f 61 64 0a 2a 2a 20 20 20 20 20 20 to load.**
137b3 61 20 6e 65 77 20 70 61 67 65 20 69 6e 74 6f 20 a new page into
137b4 74 68 65 20 63 61 63 68 65 20 74 6f 20 70 6f 70 the cache to pop
137b5 75 6c 61 74 65 20 77 69 74 68 20 74 68 65 20 64 ulate with the d
137b6 61 74 61 20 72 65 61 64 0a 2a 2a 20 20 20 20 20 ata read.**
137b7 20 66 72 6f 6d 20 74 68 65 20 73 61 76 65 70 6f from the savepo
137b8 69 6e 74 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a int journal..**.
137b9 2a 2a 20 49 66 20 6e 6f 43 6f 6e 74 65 6e 74 20 ** If noContent
137ba 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74 68 is true, then th
137bb 65 20 64 61 74 61 20 72 65 74 75 72 6e 65 64 20 e data returned
137bc 69 73 20 7a 65 72 6f 65 64 20 69 6e 73 74 65 61 is zeroed instea
137bd 64 20 6f 66 0a 2a 2a 20 62 65 69 6e 67 20 72 65 d of.** being re
137be 61 64 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 ad from the data
137bf 62 61 73 65 2e 20 41 64 64 69 74 69 6f 6e 61 6c base. Additional
137c0 6c 79 2c 20 74 68 65 20 62 69 74 73 20 63 6f 72 ly, the bits cor
137c1 72 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20 74 6f responding.** to
137c2 20 70 67 6e 6f 20 69 6e 20 50 61 67 65 72 2e 70 pgno in Pager.p
137c3 49 6e 4a 6f 75 72 6e 61 6c 20 28 62 69 74 76 65 InJournal (bitve
137c4 63 20 6f 66 20 70 61 67 65 73 20 61 6c 72 65 61 c of pages alrea
137c5 64 79 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 dy written to th
137c6 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c e.** journal fil
137c7 65 29 20 61 6e 64 20 74 68 65 20 50 61 67 65 72 e) and the Pager
137c8 53 61 76 65 70 6f 69 6e 74 2e 70 49 6e 53 61 76 Savepoint.pInSav
137c9 65 70 6f 69 6e 74 20 62 69 74 76 65 63 73 20 6f epoint bitvecs o
137ca 66 20 61 6e 79 20 6f 70 65 6e 0a 2a 2a 20 73 61 f any open.** sa
137cb 76 65 70 6f 69 6e 74 73 20 61 72 65 20 73 65 74 vepoints are set
137cc 2e 20 54 68 69 73 20 6d 65 61 6e 73 20 69 66 20 . This means if
137cd 74 68 65 20 70 61 67 65 20 69 73 20 6d 61 64 65 the page is made
137ce 20 77 72 69 74 61 62 6c 65 20 61 74 20 61 6e 79 writable at any
137cf 0a 2a 2a 20 70 6f 69 6e 74 20 69 6e 20 74 68 65 .** point in the
137d0 20 66 75 74 75 72 65 2c 20 75 73 69 6e 67 20 61 future, using a
137d1 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 call to sqlite3
137d2 50 61 67 65 72 57 72 69 74 65 28 29 2c 20 69 74 PagerWrite(), it
137d3 73 20 63 6f 6e 74 65 6e 74 73 0a 2a 2a 20 77 69 s contents.** wi
137d4 6c 6c 20 6e 6f 74 20 62 65 20 6a 6f 75 72 6e 61 ll not be journa
137d5 6c 65 64 2e 20 54 68 69 73 20 73 61 76 65 73 20 led. This saves
137d6 49 4f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 63 IO..**.** The ac
137d7 71 75 69 73 69 74 69 6f 6e 20 6d 69 67 68 74 20 quisition might
137d8 66 61 69 6c 20 66 6f 72 20 73 65 76 65 72 61 6c fail for several
137d9 20 72 65 61 73 6f 6e 73 2e 20 20 49 6e 20 61 6c reasons. In al
137da 6c 20 63 61 73 65 73 2c 0a 2a 2a 20 61 6e 20 61 l cases,.** an a
137db 70 70 72 6f 70 72 69 61 74 65 20 65 72 72 6f 72 ppropriate error
137dc 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 code is returne
137dd 64 20 61 6e 64 20 2a 70 70 50 61 67 65 20 69 73 d and *ppPage is
137de 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a set to NULL..**
137df 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 73 71 6c .** See also sql
137e0 69 74 65 33 50 61 67 65 72 4c 6f 6f 6b 75 70 28 ite3PagerLookup(
137e1 29 2e 20 20 42 6f 74 68 20 74 68 69 73 20 72 6f ). Both this ro
137e2 75 74 69 6e 65 20 61 6e 64 20 4c 6f 6f 6b 75 70 utine and Lookup
137e3 28 29 20 61 74 74 65 6d 70 74 0a 2a 2a 20 74 6f () attempt.** to
137e4 20 66 69 6e 64 20 61 20 70 61 67 65 20 69 6e 20 find a page in
137e5 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 63 61 the in-memory ca
137e6 63 68 65 20 66 69 72 73 74 2e 20 20 49 66 20 74 che first. If t
137e7 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20 61 he page is not a
137e8 6c 72 65 61 64 79 0a 2a 2a 20 69 6e 20 6d 65 6d lready.** in mem
137e9 6f 72 79 2c 20 74 68 69 73 20 72 6f 75 74 69 6e ory, this routin
137ea 65 20 67 6f 65 73 20 74 6f 20 64 69 73 6b 20 74 e goes to disk t
137eb 6f 20 72 65 61 64 20 69 74 20 69 6e 20 77 68 65 o read it in whe
137ec 72 65 61 73 20 4c 6f 6f 6b 75 70 28 29 0a 2a 2a reas Lookup().**
137ed 20 6a 75 73 74 20 72 65 74 75 72 6e 73 20 30 2e just returns 0.
137ee 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 This routine a
137ef 63 71 75 69 72 65 73 20 61 20 72 65 61 64 2d 6c cquires a read-l
137f0 6f 63 6b 20 74 68 65 20 66 69 72 73 74 20 74 69 ock the first ti
137f1 6d 65 20 69 74 0a 2a 2a 20 68 61 73 20 74 6f 20 me it.** has to
137f2 67 6f 20 74 6f 20 64 69 73 6b 2c 20 61 6e 64 20 go to disk, and
137f3 63 6f 75 6c 64 20 61 6c 73 6f 20 70 6c 61 79 62 could also playb
137f4 61 63 6b 20 61 6e 20 6f 6c 64 20 6a 6f 75 72 6e ack an old journ
137f5 61 6c 20 69 66 20 6e 65 63 65 73 73 61 72 79 2e al if necessary.
137f6 0a 2a 2a 20 53 69 6e 63 65 20 4c 6f 6f 6b 75 70 .** Since Lookup
137f7 28 29 20 6e 65 76 65 72 20 67 6f 65 73 20 74 6f () never goes to
137f8 20 64 69 73 6b 2c 20 69 74 20 6e 65 76 65 72 20 disk, it never
137f9 68 61 73 20 74 6f 20 64 65 61 6c 20 77 69 74 68 has to deal with
137fa 20 6c 6f 63 6b 73 0a 2a 2a 20 6f 72 20 6a 6f 75 locks.** or jou
137fb 72 6e 61 6c 20 66 69 6c 65 73 2e 0a 2a 2f 0a 53 rnal files..*/.S
137fc 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
137fd 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 41 63 t sqlite3PagerAc
137fe 71 75 69 72 65 28 0a 20 20 50 61 67 65 72 20 2a quire(. Pager *
137ff 70 50 61 67 65 72 2c 20 20 20 20 20 20 2f 2a 20 pPager, /*
13800 54 68 65 20 70 61 67 65 72 20 6f 70 65 6e 20 6f The pager open o
13801 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 n the database f
13802 69 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 ile */. Pgno pg
13803 6e 6f 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 no, /*
13804 50 61 67 65 20 6e 75 6d 62 65 72 20 74 6f 20 66 Page number to f
13805 65 74 63 68 20 2a 2f 0a 20 20 44 62 50 61 67 65 etch */. DbPage
13806 20 2a 2a 70 70 50 61 67 65 2c 20 20 20 20 2f 2a **ppPage, /*
13807 20 57 72 69 74 65 20 61 20 70 6f 69 6e 74 65 72 Write a pointer
13808 20 74 6f 20 74 68 65 20 70 61 67 65 20 68 65 72 to the page her
13809 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f 43 6f 6e e */. int noCon
1380a 74 65 6e 74 20 20 20 20 20 20 20 2f 2a 20 44 6f tent /* Do
1380b 20 6e 6f 74 20 62 6f 74 68 65 72 20 72 65 61 64 not bother read
1380c 69 6e 67 20 63 6f 6e 74 65 6e 74 20 66 72 6f 6d ing content from
1380d 20 64 69 73 6b 20 69 66 20 74 72 75 65 20 2a 2f disk if true */
1380e 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 .){. int rc;.
1380f 50 67 48 64 72 20 2a 70 50 67 3b 0a 0a 20 20 61 PgHdr *pPg;.. a
13810 73 73 65 72 74 28 20 61 73 73 65 72 74 5f 70 61 ssert( assert_pa
13811 67 65 72 5f 73 74 61 74 65 28 70 50 61 67 65 72 ger_state(pPager
13812 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 ) );. assert( p
13813 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 50 41 47 Pager->state>PAG
13814 45 52 5f 55 4e 4c 4f 43 4b 20 29 3b 0a 0a 20 20 ER_UNLOCK );..
13815 69 66 28 20 70 67 6e 6f 3d 3d 30 20 29 7b 0a 20 if( pgno==0 ){.
13816 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
13817 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 _CORRUPT_BKPT;.
13818 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 }.. /* If the
13819 70 61 67 65 72 20 69 73 20 69 6e 20 74 68 65 20 pager is in the
1381a 65 72 72 6f 72 20 73 74 61 74 65 2c 20 72 65 74 error state, ret
1381b 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 69 6d 6d urn an error imm
1381c 65 64 69 61 74 65 6c 79 2e 20 0a 20 20 2a 2a 20 ediately. . **
1381d 4f 74 68 65 72 77 69 73 65 2c 20 72 65 71 75 65 Otherwise, reque
1381e 73 74 20 74 68 65 20 70 61 67 65 20 66 72 6f 6d st the page from
1381f 20 74 68 65 20 50 43 61 63 68 65 20 6c 61 79 65 the PCache laye
13820 72 2e 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 67 r. */. if( pPag
13821 65 72 2d 3e 65 72 72 43 6f 64 65 21 3d 53 51 4c er->errCode!=SQL
13822 49 54 45 5f 4f 4b 20 26 26 20 70 50 61 67 65 72 ITE_OK && pPager
13823 2d 3e 65 72 72 43 6f 64 65 21 3d 53 51 4c 49 54 ->errCode!=SQLIT
13824 45 5f 46 55 4c 4c 20 29 7b 0a 20 20 20 20 72 63 E_FULL ){. rc
13825 20 3d 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f = pPager->errCo
13826 64 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 de;. }else{.
13827 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 63 61 rc = sqlite3Pca
13828 63 68 65 46 65 74 63 68 28 70 50 61 67 65 72 2d cheFetch(pPager-
13829 3e 70 50 43 61 63 68 65 2c 20 70 67 6e 6f 2c 20 >pPCache, pgno,
1382a 31 2c 20 70 70 50 61 67 65 29 3b 0a 20 20 7d 0a 1, ppPage);. }.
1382b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 . if( rc!=SQLIT
1382c 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2f 2a 20 45 E_OK ){. /* E
1382d 69 74 68 65 72 20 74 68 65 20 63 61 6c 6c 20 74 ither the call t
1382e 6f 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 46 o sqlite3PcacheF
1382f 65 74 63 68 28 29 20 72 65 74 75 72 6e 65 64 20 etch() returned
13830 61 6e 20 65 72 72 6f 72 20 6f 72 20 74 68 65 0a an error or the.
13831 20 20 20 20 2a 2a 20 70 61 67 65 72 20 77 61 73 ** pager was
13832 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 already in the
13833 65 72 72 6f 72 2d 73 74 61 74 65 20 77 68 65 6e error-state when
13834 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 this function w
13835 61 73 20 63 61 6c 6c 65 64 2e 0a 20 20 20 20 2a as called.. *
13836 2a 20 53 65 74 20 70 50 67 20 74 6f 20 30 20 61 * Set pPg to 0 a
13837 6e 64 20 6a 75 6d 70 20 74 6f 20 74 68 65 20 65 nd jump to the e
13838 78 63 65 70 74 69 6f 6e 20 68 61 6e 64 6c 65 72 xception handler
13839 2e 20 20 2a 2f 0a 20 20 20 20 70 50 67 20 3d 20 . */. pPg =
1383a 30 3b 0a 20 20 20 20 67 6f 74 6f 20 70 61 67 65 0;. goto page
1383b 72 5f 61 63 71 75 69 72 65 5f 65 72 72 3b 0a 20 r_acquire_err;.
1383c 20 7d 0a 20 20 61 73 73 65 72 74 28 20 28 2a 70 }. assert( (*p
1383d 70 50 61 67 65 29 2d 3e 70 67 6e 6f 3d 3d 70 67 pPage)->pgno==pg
1383e 6e 6f 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 no );. assert(
1383f 28 2a 70 70 50 61 67 65 29 2d 3e 70 50 61 67 65 (*ppPage)->pPage
13840 72 3d 3d 70 50 61 67 65 72 20 7c 7c 20 28 2a 70 r==pPager || (*p
13841 70 50 61 67 65 29 2d 3e 70 50 61 67 65 72 3d 3d pPage)->pPager==
13842 30 20 29 3b 0a 0a 20 20 69 66 28 20 28 2a 70 70 0 );.. if( (*pp
13843 50 61 67 65 29 2d 3e 70 50 61 67 65 72 20 29 7b Page)->pPager ){
13844 0a 20 20 20 20 2f 2a 20 49 6e 20 74 68 69 73 20 . /* In this
13845 63 61 73 65 20 74 68 65 20 70 63 61 63 68 65 20 case the pcache
13846 61 6c 72 65 61 64 79 20 63 6f 6e 74 61 69 6e 73 already contains
13847 20 61 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 an initialized
13848 63 6f 70 79 20 6f 66 0a 20 20 20 20 2a 2a 20 74 copy of. ** t
13849 68 65 20 70 61 67 65 2e 20 52 65 74 75 72 6e 20 he page. Return
1384a 77 69 74 68 6f 75 74 20 66 75 72 74 68 65 72 20 without further
1384b 61 64 6f 2e 20 20 2a 2f 0a 20 20 20 20 61 73 73 ado. */. ass
1384c 65 72 74 28 20 70 67 6e 6f 3c 3d 50 41 47 45 52 ert( pgno<=PAGER
1384d 5f 4d 41 58 5f 50 47 4e 4f 20 26 26 20 70 67 6e _MAX_PGNO && pgn
1384e 6f 21 3d 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f o!=PAGER_MJ_PGNO
1384f 28 70 50 61 67 65 72 29 20 29 3b 0a 20 20 20 20 (pPager) );.
13850 50 41 47 45 52 5f 49 4e 43 52 28 70 50 61 67 65 PAGER_INCR(pPage
13851 72 2d 3e 6e 48 69 74 29 3b 0a 20 20 20 20 72 65 r->nHit);. re
13852 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
13853 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a . }else{. /*
13854 20 54 68 65 20 70 61 67 65 72 20 63 61 63 68 65 The pager cache
13855 20 68 61 73 20 63 72 65 61 74 65 64 20 61 20 6e has created a n
13856 65 77 20 70 61 67 65 2e 20 49 74 73 20 63 6f 6e ew page. Its con
13857 74 65 6e 74 20 6e 65 65 64 73 20 74 6f 20 0a 20 tent needs to .
13858 20 20 20 2a 2a 20 62 65 20 69 6e 69 74 69 61 6c ** be initial
13859 69 7a 65 64 2e 20 20 2a 2f 0a 20 20 20 20 69 6e ized. */. in
1385a 74 20 6e 4d 61 78 3b 0a 0a 20 20 20 20 50 41 47 t nMax;.. PAG
1385b 45 52 5f 49 4e 43 52 28 70 50 61 67 65 72 2d 3e ER_INCR(pPager->
1385c 6e 4d 69 73 73 29 3b 0a 20 20 20 20 70 50 67 20 nMiss);. pPg
1385d 3d 20 2a 70 70 50 61 67 65 3b 0a 20 20 20 20 70 = *ppPage;. p
1385e 50 67 2d 3e 70 50 61 67 65 72 20 3d 20 70 50 61 Pg->pPager = pPa
1385f 67 65 72 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 65 ger;.. /* The
13860 20 6d 61 78 69 6d 75 6d 20 70 61 67 65 20 6e 75 maximum page nu
13861 6d 62 65 72 20 69 73 20 32 5e 33 31 2e 20 52 65 mber is 2^31. Re
13862 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 turn SQLITE_CORR
13863 55 50 54 20 69 66 20 61 20 70 61 67 65 0a 20 20 UPT if a page.
13864 20 20 2a 2a 20 6e 75 6d 62 65 72 20 67 72 65 61 ** number grea
13865 74 65 72 20 74 68 61 6e 20 74 68 69 73 2c 20 6f ter than this, o
13866 72 20 74 68 65 20 75 6e 75 73 65 64 20 6c 6f 63 r the unused loc
13867 6b 69 6e 67 2d 70 61 67 65 2c 20 69 73 20 72 65 king-page, is re
13868 71 75 65 73 74 65 64 2e 20 2a 2f 0a 20 20 20 20 quested. */.
13869 69 66 28 20 70 67 6e 6f 3e 50 41 47 45 52 5f 4d if( pgno>PAGER_M
1386a 41 58 5f 50 47 4e 4f 20 7c 7c 20 70 67 6e 6f 3d AX_PGNO || pgno=
1386b 3d 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 =PAGER_MJ_PGNO(p
1386c 50 61 67 65 72 29 20 29 7b 0a 20 20 20 20 20 20 Pager) ){.
1386d 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 rc = SQLITE_CORR
1386e 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 UPT_BKPT;.
1386f 67 6f 74 6f 20 70 61 67 65 72 5f 61 63 71 75 69 goto pager_acqui
13870 72 65 5f 65 72 72 3b 0a 20 20 20 20 7d 0a 0a 20 re_err;. }..
13871 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 rc = sqlite3P
13872 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 50 agerPagecount(pP
13873 61 67 65 72 2c 20 26 6e 4d 61 78 29 3b 0a 20 20 ager, &nMax);.
13874 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
13875 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 _OK ){. got
13876 6f 20 70 61 67 65 72 5f 61 63 71 75 69 72 65 5f o pager_acquire_
13877 65 72 72 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 err;. }..
13878 69 66 28 20 6e 4d 61 78 3c 28 69 6e 74 29 70 67 if( nMax<(int)pg
13879 6e 6f 20 7c 7c 20 4d 45 4d 44 42 20 7c 7c 20 6e no || MEMDB || n
1387a 6f 43 6f 6e 74 65 6e 74 20 29 7b 0a 20 20 20 20 oContent ){.
1387b 20 20 69 66 28 20 70 67 6e 6f 3e 70 50 61 67 65 if( pgno>pPage
1387c 72 2d 3e 6d 78 50 67 6e 6f 20 29 7b 0a 09 72 63 r->mxPgno ){..rc
1387d 20 3d 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a = SQLITE_FULL;.
1387e 09 67 6f 74 6f 20 70 61 67 65 72 5f 61 63 71 75 .goto pager_acqu
1387f 69 72 65 5f 65 72 72 3b 0a 20 20 20 20 20 20 7d ire_err;. }
13880 0a 20 20 20 20 20 20 69 66 28 20 6e 6f 43 6f 6e . if( noCon
13881 74 65 6e 74 20 29 7b 0a 20 20 20 20 20 20 20 20 tent ){.
13882 2f 2a 20 46 61 69 6c 75 72 65 20 74 6f 20 73 65 /* Failure to se
13883 74 20 74 68 65 20 62 69 74 73 20 69 6e 20 74 68 t the bits in th
13884 65 20 49 6e 4a 6f 75 72 6e 61 6c 20 62 69 74 2d e InJournal bit-
13885 76 65 63 74 6f 72 73 20 69 73 20 62 65 6e 69 67 vectors is benig
13886 6e 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20 49 74 n.. ** It
13887 20 6d 65 72 65 6c 79 20 6d 65 61 6e 73 20 74 68 merely means th
13888 61 74 20 77 65 20 6d 69 67 68 74 20 64 6f 20 73 at we might do s
13889 6f 6d 65 20 65 78 74 72 61 20 77 6f 72 6b 20 74 ome extra work t
1388a 6f 20 6a 6f 75 72 6e 61 6c 20 61 20 0a 20 20 20 o journal a .
1388b 20 20 20 20 20 2a 2a 20 70 61 67 65 20 74 68 61 ** page tha
1388c 74 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 t does not need
1388d 74 6f 20 62 65 20 6a 6f 75 72 6e 61 6c 65 64 2e to be journaled.
1388e 20 20 4e 65 76 65 72 74 68 65 6c 65 73 73 2c 20 Nevertheless,
1388f 62 65 20 73 75 72 65 20 0a 20 20 20 20 20 20 20 be sure .
13890 20 2a 2a 20 74 6f 20 74 65 73 74 20 74 68 65 20 ** to test the
13891 63 61 73 65 20 77 68 65 72 65 20 61 20 6d 61 6c case where a mal
13892 6c 6f 63 20 65 72 72 6f 72 20 6f 63 63 75 72 73 loc error occurs
13893 20 77 68 69 6c 65 20 74 72 79 69 6e 67 20 74 6f while trying to
13894 20 73 65 74 20 0a 20 20 20 20 20 20 20 20 2a 2a set . **
13895 20 61 20 62 69 74 20 69 6e 20 61 20 62 69 74 20 a bit in a bit
13896 76 65 63 74 6f 72 2e 0a 20 20 20 20 20 20 20 20 vector..
13897 2a 2f 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 */. sqlit
13898 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c e3BeginBenignMal
13899 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20 20 20 69 loc();. i
1389a 66 28 20 70 67 6e 6f 3c 3d 70 50 61 67 65 72 2d f( pgno<=pPager-
1389b 3e 64 62 4f 72 69 67 53 69 7a 65 20 29 7b 0a 20 >dbOrigSize ){.
1389c 20 20 20 20 20 20 20 20 20 54 45 53 54 4f 4e 4c TESTONL
1389d 59 28 20 72 63 20 3d 20 29 20 73 71 6c 69 74 65 Y( rc = ) sqlite
1389e 33 42 69 74 76 65 63 53 65 74 28 70 50 61 67 65 3BitvecSet(pPage
1389f 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20 70 r->pInJournal, p
138a0 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20 20 20 gno);.
138a1 74 65 73 74 63 61 73 65 28 20 72 63 3d 3d 53 51 testcase( rc==SQ
138a2 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 LITE_NOMEM );.
138a3 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
138a4 54 45 53 54 4f 4e 4c 59 28 20 72 63 20 3d 20 29 TESTONLY( rc = )
138a5 20 61 64 64 54 6f 53 61 76 65 70 6f 69 6e 74 42 addToSavepointB
138a6 69 74 76 65 63 73 28 70 50 61 67 65 72 2c 20 70 itvecs(pPager, p
138a7 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20 74 65 gno);. te
138a8 73 74 63 61 73 65 28 20 72 63 3d 3d 53 51 4c 49 stcase( rc==SQLI
138a9 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 TE_NOMEM );.
138aa 20 20 20 20 73 71 6c 69 74 65 33 45 6e 64 42 65 sqlite3EndBe
138ab 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 nignMalloc();.
138ac 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
138ad 20 20 20 6d 65 6d 73 65 74 28 70 50 67 2d 3e 70 memset(pPg->p
138ae 44 61 74 61 2c 20 30 2c 20 70 50 61 67 65 72 2d Data, 0, pPager-
138af 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 >pageSize);.
138b0 20 20 7d 0a 20 20 20 20 20 20 49 4f 54 52 41 43 }. IOTRAC
138b1 45 28 28 22 5a 45 52 4f 20 25 70 20 25 64 5c 6e E(("ZERO %p %d\n
138b2 22 2c 20 70 50 61 67 65 72 2c 20 70 67 6e 6f 29 ", pPager, pgno)
138b3 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 );. }else{.
138b4 20 20 20 20 61 73 73 65 72 74 28 20 70 50 67 2d assert( pPg-
138b5 3e 70 50 61 67 65 72 3d 3d 70 50 61 67 65 72 20 >pPager==pPager
138b6 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 72 65 );. rc = re
138b7 61 64 44 62 50 61 67 65 28 70 50 67 29 3b 0a 20 adDbPage(pPg);.
138b8 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c if( rc!=SQL
138b9 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
138ba 20 20 67 6f 74 6f 20 70 61 67 65 72 5f 61 63 71 goto pager_acq
138bb 75 69 72 65 5f 65 72 72 3b 0a 20 20 20 20 20 20 uire_err;.
138bc 7d 0a 20 20 20 20 7d 0a 23 69 66 64 65 66 20 53 }. }.#ifdef S
138bd 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 QLITE_CHECK_PAGE
138be 53 0a 20 20 20 20 70 50 67 2d 3e 70 61 67 65 48 S. pPg->pageH
138bf 61 73 68 20 3d 20 70 61 67 65 72 5f 70 61 67 65 ash = pager_page
138c0 68 61 73 68 28 70 50 67 29 3b 0a 23 65 6e 64 69 hash(pPg);.#endi
138c1 66 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 f. }.. return
138c2 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 70 61 67 65 SQLITE_OK;..page
138c3 72 5f 61 63 71 75 69 72 65 5f 65 72 72 3a 0a 20 r_acquire_err:.
138c4 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c assert( rc!=SQL
138c5 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 69 66 28 20 ITE_OK );. if(
138c6 70 50 67 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 pPg ){. sqlit
138c7 65 33 50 63 61 63 68 65 44 72 6f 70 28 70 50 67 e3PcacheDrop(pPg
138c8 29 3b 0a 20 20 7d 0a 20 20 70 61 67 65 72 55 6e );. }. pagerUn
138c9 6c 6f 63 6b 49 66 55 6e 75 73 65 64 28 70 50 61 lockIfUnused(pPa
138ca 67 65 72 29 3b 0a 0a 20 20 2a 70 70 50 61 67 65 ger);.. *ppPage
138cb 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 72 = 0;. return r
138cc 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 63 71 75 c;.}../*.** Acqu
138cd 69 72 65 20 61 20 70 61 67 65 20 69 66 20 69 74 ire a page if it
138ce 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20 74 is already in t
138cf 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 63 61 63 he in-memory cac
138d0 68 65 2e 20 20 44 6f 0a 2a 2a 20 6e 6f 74 20 72 he. Do.** not r
138d1 65 61 64 20 74 68 65 20 70 61 67 65 20 66 72 6f ead the page fro
138d2 6d 20 64 69 73 6b 2e 20 20 52 65 74 75 72 6e 20 m disk. Return
138d3 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
138d4 20 70 61 67 65 2c 0a 2a 2a 20 6f 72 20 30 20 69 page,.** or 0 i
138d5 66 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f f the page is no
138d6 74 20 69 6e 20 63 61 63 68 65 2e 20 41 6c 73 6f t in cache. Also
138d7 2c 20 72 65 74 75 72 6e 20 30 20 69 66 20 74 68 , return 0 if th
138d8 65 20 0a 2a 2a 20 70 61 67 65 72 20 69 73 20 69 e .** pager is i
138d9 6e 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 73 n PAGER_UNLOCK s
138da 74 61 74 65 20 77 68 65 6e 20 74 68 69 73 20 66 tate when this f
138db 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 unction is calle
138dc 64 2c 0a 2a 2a 20 6f 72 20 69 66 20 74 68 65 20 d,.** or if the
138dd 70 61 67 65 72 20 69 73 20 69 6e 20 61 6e 20 65 pager is in an e
138de 72 72 6f 72 20 73 74 61 74 65 20 6f 74 68 65 72 rror state other
138df 20 74 68 61 6e 20 53 51 4c 49 54 45 5f 46 55 4c than SQLITE_FUL
138e0 4c 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 L..**.** See als
138e1 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 o sqlite3PagerGe
138e2 74 28 29 2e 20 20 54 68 65 20 64 69 66 66 65 72 t(). The differ
138e3 65 6e 63 65 20 62 65 74 77 65 65 6e 20 74 68 69 ence between thi
138e4 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 61 6e 64 s routine.** and
138e5 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 sqlite3PagerGet
138e6 28 29 20 69 73 20 74 68 61 74 20 5f 67 65 74 28 () is that _get(
138e7 29 20 77 69 6c 6c 20 67 6f 20 74 6f 20 74 68 65 ) will go to the
138e8 20 64 69 73 6b 20 61 6e 64 20 72 65 61 64 0a 2a disk and read.*
138e9 2a 20 69 6e 20 74 68 65 20 70 61 67 65 20 69 66 * in the page if
138ea 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 the page is not
138eb 20 61 6c 72 65 61 64 79 20 69 6e 20 63 61 63 68 already in cach
138ec 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 e. This routine
138ed 0a 2a 2a 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c .** returns NULL
138ee 20 69 66 20 74 68 65 20 70 61 67 65 20 69 73 20 if the page is
138ef 6e 6f 74 20 69 6e 20 63 61 63 68 65 20 6f 72 20 not in cache or
138f0 69 66 20 61 20 64 69 73 6b 20 49 2f 4f 20 65 72 if a disk I/O er
138f1 72 6f 72 20 0a 2a 2a 20 68 61 73 20 65 76 65 72 ror .** has ever
138f2 20 68 61 70 70 65 6e 65 64 2e 0a 2a 2f 0a 53 51 happened..*/.SQ
138f3 4c 49 54 45 5f 50 52 49 56 41 54 45 20 44 62 50 LITE_PRIVATE DbP
138f4 61 67 65 20 2a 73 71 6c 69 74 65 33 50 61 67 65 age *sqlite3Page
138f5 72 4c 6f 6f 6b 75 70 28 50 61 67 65 72 20 2a 70 rLookup(Pager *p
138f6 50 61 67 65 72 2c 20 50 67 6e 6f 20 70 67 6e 6f Pager, Pgno pgno
138f7 29 7b 0a 20 20 50 67 48 64 72 20 2a 70 50 67 20 ){. PgHdr *pPg
138f8 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 70 = 0;. assert( p
138f9 50 61 67 65 72 21 3d 30 20 29 3b 0a 20 20 61 73 Pager!=0 );. as
138fa 73 65 72 74 28 20 70 67 6e 6f 21 3d 30 20 29 3b sert( pgno!=0 );
138fb 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
138fc 72 2d 3e 70 50 43 61 63 68 65 21 3d 30 20 29 3b r->pPCache!=0 );
138fd 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
138fe 72 2d 3e 73 74 61 74 65 20 3e 20 50 41 47 45 52 r->state > PAGER
138ff 5f 55 4e 4c 4f 43 4b 20 29 3b 0a 20 20 73 71 6c _UNLOCK );. sql
13900 69 74 65 33 50 63 61 63 68 65 46 65 74 63 68 28 ite3PcacheFetch(
13901 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 2c pPager->pPCache,
13902 20 70 67 6e 6f 2c 20 30 2c 20 26 70 50 67 29 3b pgno, 0, &pPg);
13903 0a 20 20 72 65 74 75 72 6e 20 70 50 67 3b 0a 7d . return pPg;.}
13904 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20 ../*.** Release
13905 61 20 70 61 67 65 20 72 65 66 65 72 65 6e 63 65 a page reference
13906 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e ..**.** If the n
13907 75 6d 62 65 72 20 6f 66 20 72 65 66 65 72 65 6e umber of referen
13908 63 65 73 20 74 6f 20 74 68 65 20 70 61 67 65 20 ces to the page
13909 64 72 6f 70 20 74 6f 20 7a 65 72 6f 2c 20 74 68 drop to zero, th
1390a 65 6e 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 69 en the.** page i
1390b 73 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 4c s added to the L
1390c 52 55 20 6c 69 73 74 2e 20 20 57 68 65 6e 20 61 RU list. When a
1390d 6c 6c 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f ll references to
1390e 20 61 6c 6c 20 70 61 67 65 73 0a 2a 2a 20 61 72 all pages.** ar
1390f 65 20 72 65 6c 65 61 73 65 64 2c 20 61 20 72 6f e released, a ro
13910 6c 6c 62 61 63 6b 20 6f 63 63 75 72 73 20 61 6e llback occurs an
13911 64 20 74 68 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 d the lock on th
13912 65 20 64 61 74 61 62 61 73 65 20 69 73 0a 2a 2a e database is.**
13913 20 72 65 6d 6f 76 65 64 2e 0a 2a 2f 0a 53 51 4c removed..*/.SQL
13914 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
13915 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 sqlite3PagerUnr
13916 65 66 28 44 62 50 61 67 65 20 2a 70 50 67 29 7b ef(DbPage *pPg){
13917 0a 20 20 69 66 28 20 70 50 67 20 29 7b 0a 20 20 . if( pPg ){.
13918 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 Pager *pPager
13919 3d 20 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a 20 = pPg->pPager;.
1391a 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 sqlite3Pcache
1391b 52 65 6c 65 61 73 65 28 70 50 67 29 3b 0a 20 20 Release(pPg);.
1391c 20 20 70 61 67 65 72 55 6e 6c 6f 63 6b 49 66 55 pagerUnlockIfU
1391d 6e 75 73 65 64 28 70 50 61 67 65 72 29 3b 0a 20 nused(pPager);.
1391e 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 }.}../*.** If t
1391f 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 he main journal
13920 66 69 6c 65 20 68 61 73 20 61 6c 72 65 61 64 79 file has already
13921 20 62 65 65 6e 20 6f 70 65 6e 65 64 2c 20 65 6e been opened, en
13922 73 75 72 65 20 74 68 61 74 20 74 68 65 0a 2a 2a sure that the.**
13923 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c sub-journal fil
13924 65 20 69 73 20 6f 70 65 6e 20 74 6f 6f 2e 20 49 e is open too. I
13925 66 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e f the main journ
13926 61 6c 20 69 73 20 6e 6f 74 20 6f 70 65 6e 2c 0a al is not open,.
13927 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e ** this function
13928 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a is a no-op..**.
13929 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 ** SQLITE_OK is
1392a 72 65 74 75 72 6e 65 64 20 69 66 20 65 76 65 72 returned if ever
1392b 79 74 68 69 6e 67 20 67 6f 65 73 20 61 63 63 6f ything goes acco
1392c 72 64 69 6e 67 20 74 6f 20 70 6c 61 6e 2e 20 0a rding to plan. .
1392d 2a 2a 20 41 6e 20 53 51 4c 49 54 45 5f 49 4f 45 ** An SQLITE_IOE
1392e 52 52 5f 58 58 58 20 65 72 72 6f 72 20 63 6f 64 RR_XXX error cod
1392f 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 e is returned if
13930 20 61 20 63 61 6c 6c 20 74 6f 20 0a 2a 2a 20 73 a call to .** s
13931 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 29 20 66 qlite3OsOpen() f
13932 61 69 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ails..*/.static
13933 69 6e 74 20 6f 70 65 6e 53 75 62 4a 6f 75 72 6e int openSubJourn
13934 61 6c 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 al(Pager *pPager
13935 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 ){. int rc = SQ
13936 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 69 LITE_OK;. if( i
13937 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 sOpen(pPager->jf
13938 64 29 20 26 26 20 21 69 73 4f 70 65 6e 28 70 50 d) && !isOpen(pP
13939 61 67 65 72 2d 3e 73 6a 66 64 29 20 29 7b 0a 20 ager->sjfd) ){.
1393a 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6a if( pPager->j
1393b 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 ournalMode==PAGE
1393c 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 R_JOURNALMODE_ME
1393d 4d 4f 52 59 20 7c 7c 20 70 50 61 67 65 72 2d 3e MORY || pPager->
1393e 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 20 29 7b 0a subjInMemory ){.
1393f 20 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d sqlite3Mem
13940 4a 6f 75 72 6e 61 6c 4f 70 65 6e 28 70 50 61 67 JournalOpen(pPag
13941 65 72 2d 3e 73 6a 66 64 29 3b 0a 20 20 20 20 7d er->sjfd);. }
13942 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d else{. rc =
13943 20 70 61 67 65 72 4f 70 65 6e 74 65 6d 70 28 70 pagerOpentemp(p
13944 50 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 73 Pager, pPager->s
13945 6a 66 64 2c 20 53 51 4c 49 54 45 5f 4f 50 45 4e jfd, SQLITE_OPEN
13946 5f 53 55 42 4a 4f 55 52 4e 41 4c 29 3b 0a 20 20 _SUBJOURNAL);.
13947 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e }. }. return
13948 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 rc;.}../*.** Th
13949 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 is function is c
1394a 61 6c 6c 65 64 20 61 74 20 74 68 65 20 73 74 61 alled at the sta
1394b 72 74 20 6f 66 20 65 76 65 72 79 20 77 72 69 74 rt of every writ
1394c 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a e transaction..*
1394d 2a 20 54 68 65 72 65 20 6d 75 73 74 20 61 6c 72 * There must alr
1394e 65 61 64 79 20 62 65 20 61 20 52 45 53 45 52 56 eady be a RESERV
1394f 45 44 20 6f 72 20 45 58 43 4c 55 53 49 56 45 20 ED or EXCLUSIVE
13950 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 lock on the data
13951 62 61 73 65 20 0a 2a 2a 20 66 69 6c 65 20 77 68 base .** file wh
13952 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 en this routine
13953 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a is called..**.**
13954 20 4f 70 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 Open the journa
13955 6c 20 66 69 6c 65 20 66 6f 72 20 70 61 67 65 72 l file for pager
13956 20 70 50 61 67 65 72 20 61 6e 64 20 77 72 69 74 pPager and writ
13957 65 20 61 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 e a journal head
13958 65 72 0a 2a 2a 20 74 6f 20 74 68 65 20 73 74 61 er.** to the sta
13959 72 74 20 6f 66 20 69 74 2e 20 49 66 20 74 68 65 rt of it. If the
1395a 72 65 20 61 72 65 20 61 63 74 69 76 65 20 73 61 re are active sa
1395b 76 65 70 6f 69 6e 74 73 2c 20 6f 70 65 6e 20 74 vepoints, open t
1395c 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 0a 2a he sub-journal.*
1395d 2a 20 61 73 20 77 65 6c 6c 2e 20 54 68 69 73 20 * as well. This
1395e 66 75 6e 63 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 function is only
1395f 20 75 73 65 64 20 77 68 65 6e 20 74 68 65 20 6a used when the j
13960 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 62 ournal file is b
13961 65 69 6e 67 20 0a 2a 2a 20 6f 70 65 6e 65 64 20 eing .** opened
13962 74 6f 20 77 72 69 74 65 20 61 20 72 6f 6c 6c 62 to write a rollb
13963 61 63 6b 20 6c 6f 67 20 66 6f 72 20 61 20 74 72 ack log for a tr
13964 61 6e 73 61 63 74 69 6f 6e 2e 20 49 74 20 69 73 ansaction. It is
13965 20 6e 6f 74 20 75 73 65 64 20 0a 2a 2a 20 77 68 not used .** wh
13966 65 6e 20 6f 70 65 6e 69 6e 67 20 61 20 68 6f 74 en opening a hot
13967 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 6f journal file to
13968 20 72 6f 6c 6c 20 69 74 20 62 61 63 6b 2e 0a 2a roll it back..*
13969 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6a 6f 75 72 *.** If the jour
1396a 6e 61 6c 20 66 69 6c 65 20 69 73 20 61 6c 72 65 nal file is alre
1396b 61 64 79 20 6f 70 65 6e 20 28 61 73 20 69 74 20 ady open (as it
1396c 6d 61 79 20 62 65 20 69 6e 20 65 78 63 6c 75 73 may be in exclus
1396d 69 76 65 20 6d 6f 64 65 29 2c 0a 2a 2a 20 74 68 ive mode),.** th
1396e 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e en this function
1396f 20 6a 75 73 74 20 77 72 69 74 65 73 20 61 20 6a just writes a j
13970 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 74 6f ournal header to
13971 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 the start of th
13972 65 0a 2a 2a 20 61 6c 72 65 61 64 79 20 6f 70 65 e.** already ope
13973 6e 20 66 69 6c 65 2e 20 0a 2a 2a 0a 2a 2a 20 57 n file. .**.** W
13974 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 hether or not th
13975 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 e journal file i
13976 73 20 6f 70 65 6e 65 64 20 62 79 20 74 68 69 73 s opened by this
13977 20 66 75 6e 63 74 69 6f 6e 2c 20 74 68 65 0a 2a function, the.*
13978 2a 20 50 61 67 65 72 2e 70 49 6e 4a 6f 75 72 6e * Pager.pInJourn
13979 61 6c 20 62 69 74 76 65 63 20 73 74 72 75 63 74 al bitvec struct
1397a 75 72 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 ure is allocated
1397b 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 ..**.** Return S
1397c 51 4c 49 54 45 5f 4f 4b 20 69 66 20 65 76 65 72 QLITE_OK if ever
1397d 79 74 68 69 6e 67 20 69 73 20 73 75 63 63 65 73 ything is succes
1397e 73 66 75 6c 2e 20 4f 74 68 65 72 77 69 73 65 2c sful. Otherwise,
1397f 20 72 65 74 75 72 6e 20 0a 2a 2a 20 53 51 4c 49 return .** SQLI
13980 54 45 5f 4e 4f 4d 45 4d 20 69 66 20 74 68 65 20 TE_NOMEM if the
13981 61 74 74 65 6d 70 74 20 74 6f 20 61 6c 6c 6f 63 attempt to alloc
13982 61 74 65 20 50 61 67 65 72 2e 70 49 6e 4a 6f 75 ate Pager.pInJou
13983 72 6e 61 6c 20 66 61 69 6c 73 2c 20 6f 72 20 0a rnal fails, or .
13984 2a 2a 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 63 ** an IO error c
13985 6f 64 65 20 69 66 20 6f 70 65 6e 69 6e 67 20 6f ode if opening o
13986 72 20 77 72 69 74 69 6e 67 20 74 68 65 20 6a 6f r writing the jo
13987 75 72 6e 61 6c 20 66 69 6c 65 20 66 61 69 6c 73 urnal file fails
13988 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
13989 70 61 67 65 72 5f 6f 70 65 6e 5f 6a 6f 75 72 6e pager_open_journ
1398a 61 6c 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 al(Pager *pPager
1398b 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 ){. int rc = SQ
1398c 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 LITE_OK;
1398d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1398e 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a /* Return code *
1398f 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 /. sqlite3_vfs
13990 2a 20 63 6f 6e 73 74 20 70 56 66 73 20 3d 20 70 * const pVfs = p
13991 50 61 67 65 72 2d 3e 70 56 66 73 3b 20 20 20 2f Pager->pVfs; /
13992 2a 20 4c 6f 63 61 6c 20 63 61 63 68 65 20 6f 66 * Local cache of
13993 20 76 66 73 20 70 6f 69 6e 74 65 72 20 2a 2f 0a vfs pointer */.
13994 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
13995 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f r->state>=PAGER_
13996 52 45 53 45 52 56 45 44 20 29 3b 0a 20 20 61 73 RESERVED );. as
13997 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 75 73 sert( pPager->us
13998 65 4a 6f 75 72 6e 61 6c 20 29 3b 0a 20 20 61 73 eJournal );. as
13999 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6a 6f sert( pPager->jo
1399a 75 72 6e 61 6c 4d 6f 64 65 21 3d 50 41 47 45 52 urnalMode!=PAGER
1399b 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 _JOURNALMODE_OFF
1399c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 );. assert( pP
1399d 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c ager->pInJournal
1399e 3d 3d 30 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 49 ==0 );. . /* I
1399f 66 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 f already in the
139a0 20 65 72 72 6f 72 20 73 74 61 74 65 2c 20 74 68 error state, th
139a1 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 is function is a
139a2 20 6e 6f 2d 6f 70 2e 20 20 42 75 74 20 6f 6e 0a no-op. But on.
139a3 20 20 2a 2a 20 74 68 65 20 6f 74 68 65 72 20 68 ** the other h
139a4 61 6e 64 2c 20 74 68 69 73 20 72 6f 75 74 69 6e and, this routin
139a5 65 20 69 73 20 6e 65 76 65 72 20 63 61 6c 6c 65 e is never calle
139a6 64 20 69 66 20 77 65 20 61 72 65 20 61 6c 72 65 d if we are alre
139a7 61 64 79 20 69 6e 0a 20 20 2a 2a 20 61 6e 20 65 ady in. ** an e
139a8 72 72 6f 72 20 73 74 61 74 65 2e 20 2a 2f 0a 20 rror state. */.
139a9 20 69 66 28 20 4e 45 56 45 52 28 70 50 61 67 65 if( NEVER(pPage
139aa 72 2d 3e 65 72 72 43 6f 64 65 29 20 29 20 72 65 r->errCode) ) re
139ab 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 65 72 72 turn pPager->err
139ac 43 6f 64 65 3b 0a 0a 20 20 2f 2a 20 54 4f 44 4f Code;.. /* TODO
139ad 3a 20 49 73 20 69 74 20 72 65 61 6c 6c 79 20 70 : Is it really p
139ae 6f 73 73 69 62 6c 65 20 74 6f 20 67 65 74 20 68 ossible to get h
139af 65 72 65 20 77 69 74 68 20 64 62 53 69 7a 65 56 ere with dbSizeV
139b0 61 6c 69 64 3d 3d 30 3f 20 49 66 20 6e 6f 74 2c alid==0? If not,
139b1 0a 20 20 2a 2a 20 74 68 65 20 63 61 6c 6c 20 74 . ** the call t
139b2 6f 20 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 o PagerPagecount
139b3 28 29 20 63 61 6e 20 62 65 20 72 65 6d 6f 76 65 () can be remove
139b4 64 2e 0a 20 20 2a 2f 0a 20 20 74 65 73 74 63 61 d.. */. testca
139b5 73 65 28 20 70 50 61 67 65 72 2d 3e 64 62 53 69 se( pPager->dbSi
139b6 7a 65 56 61 6c 69 64 3d 3d 30 20 29 3b 0a 20 20 zeValid==0 );.
139b7 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 sqlite3PagerPage
139b8 63 6f 75 6e 74 28 70 50 61 67 65 72 2c 20 30 29 count(pPager, 0)
139b9 3b 0a 0a 20 20 70 50 61 67 65 72 2d 3e 70 49 6e ;.. pPager->pIn
139ba 4a 6f 75 72 6e 61 6c 20 3d 20 73 71 6c 69 74 65 Journal = sqlite
139bb 33 42 69 74 76 65 63 43 72 65 61 74 65 28 70 50 3BitvecCreate(pP
139bc 61 67 65 72 2d 3e 64 62 53 69 7a 65 29 3b 0a 20 ager->dbSize);.
139bd 20 69 66 28 20 70 50 61 67 65 72 2d 3e 70 49 6e if( pPager->pIn
139be 4a 6f 75 72 6e 61 6c 3d 3d 30 20 29 7b 0a 20 20 Journal==0 ){.
139bf 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
139c0 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a NOMEM;. }.. /*
139c1 20 4f 70 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 Open the journa
139c2 6c 20 66 69 6c 65 20 69 66 20 69 74 20 69 73 20 l file if it is
139c3 6e 6f 74 20 61 6c 72 65 61 64 79 20 6f 70 65 6e not already open
139c4 2e 20 2a 2f 0a 20 20 69 66 28 20 21 69 73 4f 70 . */. if( !isOp
139c5 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 en(pPager->jfd)
139c6 29 7b 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 ){. if( pPage
139c7 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d r->journalMode==
139c8 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 PAGER_JOURNALMOD
139c9 45 5f 4d 45 4d 4f 52 59 20 29 7b 0a 20 20 20 20 E_MEMORY ){.
139ca 20 20 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 sqlite3MemJour
139cb 6e 61 6c 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e nalOpen(pPager->
139cc 6a 66 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b jfd);. }else{
139cd 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 . const int
139ce 20 66 6c 61 67 73 20 3d 20 20 20 20 20 20 20 20 flags =
139cf 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 46 /* VF
139d0 53 20 66 6c 61 67 73 20 74 6f 20 6f 70 65 6e 20 S flags to open
139d1 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a journal file */.
139d2 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f SQLITE_O
139d3 50 45 4e 5f 52 45 41 44 57 52 49 54 45 7c 53 51 PEN_READWRITE|SQ
139d4 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 LITE_OPEN_CREATE
139d5 7c 0a 20 20 20 20 20 20 20 20 28 70 50 61 67 65 |. (pPage
139d6 72 2d 3e 74 65 6d 70 46 69 6c 65 20 3f 20 0a 20 r->tempFile ? .
139d7 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 (SQLITE
139d8 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c _OPEN_DELETEONCL
139d9 4f 53 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f OSE|SQLITE_OPEN_
139da 54 45 4d 50 5f 4a 4f 55 52 4e 41 4c 29 3a 0a 20 TEMP_JOURNAL):.
139db 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 (SQLITE
139dc 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e _OPEN_MAIN_JOURN
139dd 41 4c 29 0a 20 20 20 20 20 20 20 20 29 3b 0a 23 AL). );.#
139de 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 ifdef SQLITE_ENA
139df 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45 BLE_ATOMIC_WRITE
139e0 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
139e1 74 65 33 4a 6f 75 72 6e 61 6c 4f 70 65 6e 28 0a te3JournalOpen(.
139e2 20 20 20 20 20 20 20 20 20 20 70 56 66 73 2c 20 pVfs,
139e3 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c pPager->zJournal
139e4 2c 20 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 66 , pPager->jfd, f
139e5 6c 61 67 73 2c 20 6a 72 6e 6c 42 75 66 66 65 72 lags, jrnlBuffer
139e6 53 69 7a 65 28 70 50 61 67 65 72 29 0a 20 20 20 Size(pPager).
139e7 20 20 20 29 3b 0a 23 65 6c 73 65 0a 20 20 20 20 );.#else.
139e8 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
139e9 4f 70 65 6e 28 70 56 66 73 2c 20 70 50 61 67 65 Open(pVfs, pPage
139ea 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 70 50 61 r->zJournal, pPa
139eb 67 65 72 2d 3e 6a 66 64 2c 20 66 6c 61 67 73 2c ger->jfd, flags,
139ec 20 30 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 0);.#endif.
139ed 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 63 }. assert( rc
139ee 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 69 !=SQLITE_OK || i
139ef 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 sOpen(pPager->jf
139f0 64 29 20 29 3b 0a 20 20 7d 0a 0a 0a 20 20 2f 2a d) );. }... /*
139f1 20 57 72 69 74 65 20 74 68 65 20 66 69 72 73 74 Write the first
139f2 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 journal header
139f3 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 to the journal f
139f4 69 6c 65 20 61 6e 64 20 6f 70 65 6e 20 0a 20 20 ile and open .
139f5 2a 2a 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e ** the sub-journ
139f6 61 6c 20 69 66 20 6e 65 63 65 73 73 61 72 79 2e al if necessary.
139f7 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d . */. if( rc==
139f8 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
139f9 20 2f 2a 20 54 4f 44 4f 3a 20 43 68 65 63 6b 20 /* TODO: Check
139fa 69 66 20 61 6c 6c 20 6f 66 20 74 68 65 73 65 20 if all of these
139fb 61 72 65 20 72 65 61 6c 6c 79 20 72 65 71 75 69 are really requi
139fc 72 65 64 2e 20 2a 2f 0a 20 20 20 20 70 50 61 67 red. */. pPag
139fd 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 3d er->dbOrigSize =
139fe 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3b pPager->dbSize;
139ff 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 . pPager->jou
13a00 72 6e 61 6c 53 74 61 72 74 65 64 20 3d 20 30 3b rnalStarted = 0;
13a01 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 65 65 . pPager->nee
13a02 64 53 79 6e 63 20 3d 20 30 3b 0a 20 20 20 20 70 dSync = 0;. p
13a03 50 61 67 65 72 2d 3e 6e 52 65 63 20 3d 20 30 3b Pager->nRec = 0;
13a04 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 . pPager->jou
13a05 72 6e 61 6c 4f 66 66 20 3d 20 30 3b 0a 20 20 20 rnalOff = 0;.
13a06 20 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 pPager->setMast
13a07 65 72 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 er = 0;. pPag
13a08 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 3d er->journalHdr =
13a09 20 30 3b 0a 20 20 20 20 72 63 20 3d 20 77 72 69 0;. rc = wri
13a0a 74 65 4a 6f 75 72 6e 61 6c 48 64 72 28 70 50 61 teJournalHdr(pPa
13a0b 67 65 72 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 ger);. }. if(
13a0c 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
13a0d 20 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f pPager->nSavepo
13a0e 69 6e 74 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 int ){. rc =
13a0f 6f 70 65 6e 53 75 62 4a 6f 75 72 6e 61 6c 28 70 openSubJournal(p
13a10 50 61 67 65 72 29 3b 0a 20 20 7d 0a 0a 20 20 69 Pager);. }.. i
13a11 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
13a12 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 ){. sqlite3B
13a13 69 74 76 65 63 44 65 73 74 72 6f 79 28 70 50 61 itvecDestroy(pPa
13a14 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 29 ger->pInJournal)
13a15 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 70 49 ;. pPager->pI
13a16 6e 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b 0a 20 20 nJournal = 0;.
13a17 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
13a18 0a 0a 2f 2a 0a 2a 2a 20 42 65 67 69 6e 20 61 20 ../*.** Begin a
13a19 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f write-transactio
13a1a 6e 20 6f 6e 20 74 68 65 20 73 70 65 63 69 66 69 n on the specifi
13a1b 65 64 20 70 61 67 65 72 20 6f 62 6a 65 63 74 2e ed pager object.
13a1c 20 49 66 20 61 20 0a 2a 2a 20 77 72 69 74 65 2d If a .** write-
13a1d 74 72 61 6e 73 61 63 74 69 6f 6e 20 68 61 73 20 transaction has
13a1e 61 6c 72 65 61 64 79 20 62 65 65 6e 20 6f 70 65 already been ope
13a1f 6e 65 64 2c 20 74 68 69 73 20 66 75 6e 63 74 69 ned, this functi
13a20 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a on is a no-op..*
13a21 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65 78 46 6c *.** If the exFl
13a22 61 67 20 61 72 67 75 6d 65 6e 74 20 69 73 20 66 ag argument is f
13a23 61 6c 73 65 2c 20 74 68 65 6e 20 61 63 71 75 69 alse, then acqui
13a24 72 65 20 61 74 20 6c 65 61 73 74 20 61 20 52 45 re at least a RE
13a25 53 45 52 56 45 44 0a 2a 2a 20 6c 6f 63 6b 20 6f SERVED.** lock o
13a26 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 n the database f
13a27 69 6c 65 2e 20 49 66 20 65 78 46 6c 61 67 20 69 ile. If exFlag i
13a28 73 20 74 72 75 65 2c 20 74 68 65 6e 20 61 63 71 s true, then acq
13a29 75 69 72 65 20 61 74 20 6c 65 61 73 74 0a 2a 2a uire at least.**
13a2a 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f an EXCLUSIVE lo
13a2b 63 6b 2e 20 49 66 20 73 75 63 68 20 61 20 6c 6f ck. If such a lo
13a2c 63 6b 20 69 73 20 61 6c 72 65 61 64 79 20 68 65 ck is already he
13a2d 6c 64 2c 20 6e 6f 20 6c 6f 63 6b 69 6e 67 20 0a ld, no locking .
13a2e 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 6e 65 65 ** functions nee
13a2f 64 20 62 65 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a d be called..**.
13a30 2a 2a 20 49 66 20 74 68 69 73 20 69 73 20 6e 6f ** If this is no
13a31 74 20 61 20 74 65 6d 70 6f 72 61 72 79 20 6f 72 t a temporary or
13a32 20 69 6e 2d 6d 65 6d 6f 72 79 20 66 69 6c 65 20 in-memory file
13a33 61 6e 64 2c 20 74 68 65 20 6a 6f 75 72 6e 61 6c and, the journal
13a34 20 66 69 6c 65 20 69 73 20 0a 2a 2a 20 6f 70 65 file is .** ope
13a35 6e 65 64 20 69 66 20 69 74 20 68 61 73 20 6e 6f ned if it has no
13a36 74 20 62 65 65 6e 20 61 6c 72 65 61 64 79 2e 20 t been already.
13a37 46 6f 72 20 61 20 74 65 6d 70 6f 72 61 72 79 20 For a temporary
13a38 66 69 6c 65 2c 20 74 68 65 20 6f 70 65 6e 69 6e file, the openin
13a39 67 20 0a 2a 2a 20 6f 66 20 74 68 65 20 6a 6f 75 g .** of the jou
13a3a 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 64 65 66 rnal file is def
13a3b 65 72 72 65 64 20 75 6e 74 69 6c 20 74 68 65 72 erred until ther
13a3c 65 20 69 73 20 61 6e 20 61 63 74 75 61 6c 20 6e e is an actual n
13a3d 65 65 64 20 74 6f 20 0a 2a 2a 20 77 72 69 74 65 eed to .** write
13a3e 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e to the journal.
13a3f 20 54 4f 44 4f 3a 20 57 68 79 20 68 61 6e 64 6c TODO: Why handl
13a40 65 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 e temporary file
13a41 73 20 64 69 66 66 65 72 65 6e 74 6c 79 3f 0a 2a s differently?.*
13a42 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6a 6f 75 72 *.** If the jour
13a43 6e 61 6c 20 66 69 6c 65 20 69 73 20 6f 70 65 6e nal file is open
13a44 65 64 20 28 6f 72 20 69 66 20 69 74 20 69 73 20 ed (or if it is
13a45 61 6c 72 65 61 64 79 20 6f 70 65 6e 29 2c 20 74 already open), t
13a46 68 65 6e 20 61 0a 2a 2a 20 6a 6f 75 72 6e 61 6c hen a.** journal
13a47 2d 68 65 61 64 65 72 20 69 73 20 77 72 69 74 74 -header is writt
13a48 65 6e 20 74 6f 20 74 68 65 20 73 74 61 72 74 20 en to the start
13a49 6f 66 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 of it..**.** If
13a4a 74 68 65 20 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 the subjInMemory
13a4b 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6e 6f 6e argument is non
13a4c 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 61 6e 79 20 -zero, then any
13a4d 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 6f 70 65 6e sub-journal open
13a4e 65 64 0a 2a 2a 20 77 69 74 68 69 6e 20 74 68 69 ed.** within thi
13a4f 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 69 s transaction wi
13a50 6c 6c 20 62 65 20 6f 70 65 6e 65 64 20 61 73 20 ll be opened as
13a51 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 66 69 6c an in-memory fil
13a52 65 2e 20 54 68 69 73 0a 2a 2a 20 68 61 73 20 6e e. This.** has n
13a53 6f 20 65 66 66 65 63 74 20 69 66 20 74 68 65 20 o effect if the
13a54 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 69 73 20 61 sub-journal is a
13a55 6c 72 65 61 64 79 20 6f 70 65 6e 65 64 20 28 61 lready opened (a
13a56 73 20 69 74 20 6d 61 79 20 62 65 20 77 68 65 6e s it may be when
13a57 0a 2a 2a 20 72 75 6e 6e 69 6e 67 20 69 6e 20 65 .** running in e
13a58 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 29 20 6f xclusive mode) o
13a59 72 20 69 66 20 74 68 65 20 74 72 61 6e 73 61 63 r if the transac
13a5a 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 72 65 tion does not re
13a5b 71 75 69 72 65 20 61 0a 2a 2a 20 73 75 62 2d 6a quire a.** sub-j
13a5c 6f 75 72 6e 61 6c 2e 20 49 66 20 74 68 65 20 73 ournal. If the s
13a5d 75 62 6a 49 6e 4d 65 6d 6f 72 79 20 61 72 67 75 ubjInMemory argu
13a5e 6d 65 6e 74 20 69 73 20 7a 65 72 6f 2c 20 74 68 ment is zero, th
13a5f 65 6e 20 61 6e 79 20 72 65 71 75 69 72 65 64 0a en any required.
13a60 2a 2a 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 69 ** sub-journal i
13a61 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 69 6e s implemented in
13a62 2d 6d 65 6d 6f 72 79 20 69 66 20 70 50 61 67 65 -memory if pPage
13a63 72 20 69 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 r is an in-memor
13a64 79 20 64 61 74 61 62 61 73 65 2c 20 0a 2a 2a 20 y database, .**
13a65 6f 72 20 75 73 69 6e 67 20 61 20 74 65 6d 70 6f or using a tempo
13a66 72 61 72 79 20 66 69 6c 65 20 6f 74 68 65 72 77 rary file otherw
13a67 69 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ise..*/.SQLITE_P
13a68 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
13a69 65 33 50 61 67 65 72 42 65 67 69 6e 28 50 61 67 e3PagerBegin(Pag
13a6a 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 er *pPager, int
13a6b 65 78 46 6c 61 67 2c 20 69 6e 74 20 73 75 62 6a exFlag, int subj
13a6c 49 6e 4d 65 6d 6f 72 79 29 7b 0a 20 20 69 6e 74 InMemory){. int
13a6d 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
13a6e 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
13a6f 72 2d 3e 73 74 61 74 65 21 3d 50 41 47 45 52 5f r->state!=PAGER_
13a70 55 4e 4c 4f 43 4b 20 29 3b 0a 20 20 70 50 61 67 UNLOCK );. pPag
13a71 65 72 2d 3e 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 er->subjInMemory
13a72 20 3d 20 28 75 38 29 73 75 62 6a 49 6e 4d 65 6d = (u8)subjInMem
13a73 6f 72 79 3b 0a 20 20 69 66 28 20 70 50 61 67 65 ory;. if( pPage
13a74 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 5f r->state==PAGER_
13a75 53 48 41 52 45 44 20 29 7b 0a 20 20 20 20 61 73 SHARED ){. as
13a76 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 70 49 sert( pPager->pI
13a77 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20 29 3b 0a 20 nJournal==0 );.
13a78 20 20 20 61 73 73 65 72 74 28 20 21 4d 45 4d 44 assert( !MEMD
13a79 42 20 26 26 20 21 70 50 61 67 65 72 2d 3e 74 65 B && !pPager->te
13a7a 6d 70 46 69 6c 65 20 29 3b 0a 0a 20 20 20 20 2f mpFile );.. /
13a7b 2a 20 4f 62 74 61 69 6e 20 61 20 52 45 53 45 52 * Obtain a RESER
13a7c 56 45 44 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 VED lock on the
13a7d 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 49 database file. I
13a7e 66 20 74 68 65 20 65 78 46 6c 61 67 20 70 61 72 f the exFlag par
13a7f 61 6d 65 74 65 72 0a 20 20 20 20 2a 2a 20 69 73 ameter. ** is
13a80 20 74 72 75 65 2c 20 74 68 65 6e 20 69 6d 6d 65 true, then imme
13a81 64 69 61 74 65 6c 79 20 75 70 67 72 61 64 65 20 diately upgrade
13a82 74 68 69 73 20 74 6f 20 61 6e 20 45 58 43 4c 55 this to an EXCLU
13a83 53 49 56 45 20 6c 6f 63 6b 2e 20 54 68 65 0a 20 SIVE lock. The.
13a84 20 20 20 2a 2a 20 62 75 73 79 2d 68 61 6e 64 6c ** busy-handl
13a85 65 72 20 63 61 6c 6c 62 61 63 6b 20 63 61 6e 20 er callback can
13a86 62 65 20 75 73 65 64 20 77 68 65 6e 20 75 70 67 be used when upg
13a87 72 61 64 69 6e 67 20 74 6f 20 74 68 65 20 45 58 rading to the EX
13a88 43 4c 55 53 49 56 45 0a 20 20 20 20 2a 2a 20 6c CLUSIVE. ** l
13a89 6f 63 6b 2c 20 62 75 74 20 6e 6f 74 20 77 68 65 ock, but not whe
13a8a 6e 20 6f 62 74 61 69 6e 69 6e 67 20 74 68 65 20 n obtaining the
13a8b 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 2e 0a 20 RESERVED lock..
13a8c 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 */. rc = s
13a8d 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 70 50 61 qlite3OsLock(pPa
13a8e 67 65 72 2d 3e 66 64 2c 20 52 45 53 45 52 56 45 ger->fd, RESERVE
13a8f 44 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 69 66 28 D_LOCK);. if(
13a90 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
13a91 7b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e {. pPager->
13a92 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f 52 45 state = PAGER_RE
13a93 53 45 52 56 45 44 3b 0a 20 20 20 20 20 20 69 66 SERVED;. if
13a94 28 20 65 78 46 6c 61 67 20 29 7b 0a 20 20 20 20 ( exFlag ){.
13a95 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 77 rc = pager_w
13a96 61 69 74 5f 6f 6e 5f 6c 6f 63 6b 28 70 50 61 67 ait_on_lock(pPag
13a97 65 72 2c 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f er, EXCLUSIVE_LO
13a98 43 4b 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 CK);. }.
13a99 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 }.. /* If th
13a9a 65 20 72 65 71 75 69 72 65 64 20 6c 6f 63 6b 73 e required locks
13a9b 20 77 65 72 65 20 73 75 63 63 65 73 73 66 75 6c were successful
13a9c 6c 79 20 6f 62 74 61 69 6e 65 64 2c 20 6f 70 65 ly obtained, ope
13a9d 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 20 20 n the journal.
13a9e 20 20 2a 2a 20 66 69 6c 65 20 61 6e 64 20 77 72 ** file and wr
13a9f 69 74 65 20 74 68 65 20 66 69 72 73 74 20 6a 6f ite the first jo
13aa0 75 72 6e 61 6c 2d 68 65 61 64 65 72 20 74 6f 20 urnal-header to
13aa1 69 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 it.. */. i
13aa2 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
13aa3 20 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 && pPager->jour
13aa4 6e 61 6c 4d 6f 64 65 21 3d 50 41 47 45 52 5f 4a nalMode!=PAGER_J
13aa5 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 29 OURNALMODE_OFF )
13aa6 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 {. rc = pag
13aa7 65 72 5f 6f 70 65 6e 5f 6a 6f 75 72 6e 61 6c 28 er_open_journal(
13aa8 70 50 61 67 65 72 29 3b 0a 20 20 20 20 7d 0a 20 pPager);. }.
13aa9 20 7d 65 6c 73 65 20 69 66 28 20 69 73 4f 70 65 }else if( isOpe
13aaa 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 26 n(pPager->jfd) &
13aab 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 & pPager->journa
13aac 6c 4f 66 66 3d 3d 30 20 29 7b 0a 20 20 20 20 2f lOff==0 ){. /
13aad 2a 20 54 68 69 73 20 68 61 70 70 65 6e 73 20 77 * This happens w
13aae 68 65 6e 20 74 68 65 20 70 61 67 65 72 20 77 61 hen the pager wa
13aaf 73 20 69 6e 20 65 78 63 6c 75 73 69 76 65 2d 61 s in exclusive-a
13ab0 63 63 65 73 73 20 6d 6f 64 65 20 74 68 65 20 6c ccess mode the l
13ab1 61 73 74 0a 20 20 20 20 2a 2a 20 74 69 6d 65 20 ast. ** time
13ab2 61 20 28 72 65 61 64 20 6f 72 20 77 72 69 74 65 a (read or write
13ab3 29 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 ) transaction wa
13ab4 73 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 63 s successfully c
13ab5 6f 6e 63 6c 75 64 65 64 0a 20 20 20 20 2a 2a 20 oncluded. **
13ab6 62 79 20 74 68 69 73 20 63 6f 6e 6e 65 63 74 69 by this connecti
13ab7 6f 6e 2e 20 49 6e 73 74 65 61 64 20 6f 66 20 64 on. Instead of d
13ab8 65 6c 65 74 69 6e 67 20 74 68 65 20 6a 6f 75 72 eleting the jour
13ab9 6e 61 6c 20 66 69 6c 65 20 69 74 20 77 61 73 20 nal file it was
13aba 0a 20 20 20 20 2a 2a 20 6b 65 70 74 20 6f 70 65 . ** kept ope
13abb 6e 20 61 6e 64 20 65 69 74 68 65 72 20 77 61 73 n and either was
13abc 20 74 72 75 6e 63 61 74 65 64 20 74 6f 20 30 20 truncated to 0
13abd 62 79 74 65 73 20 6f 72 20 69 74 73 20 68 65 61 bytes or its hea
13abe 64 65 72 20 77 61 73 0a 20 20 20 20 2a 2a 20 6f der was. ** o
13abf 76 65 72 77 72 69 74 74 65 6e 20 77 69 74 68 20 verwritten with
13ac0 7a 65 72 6f 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 zeros.. */.
13ac1 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 assert( pPager
13ac2 2d 3e 6e 52 65 63 3d 3d 30 20 29 3b 0a 20 20 20 ->nRec==0 );.
13ac3 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
13ac4 3e 64 62 4f 72 69 67 53 69 7a 65 3d 3d 30 20 29 >dbOrigSize==0 )
13ac5 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 ;. assert( pP
13ac6 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c ager->pInJournal
13ac7 3d 3d 30 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 ==0 );. rc =
13ac8 70 61 67 65 72 5f 6f 70 65 6e 5f 6a 6f 75 72 6e pager_open_journ
13ac9 61 6c 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a al(pPager);. }.
13aca 0a 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22 . PAGERTRACE(("
13acb 54 52 41 4e 53 41 43 54 49 4f 4e 20 25 64 5c 6e TRANSACTION %d\n
13acc 22 2c 20 50 41 47 45 52 49 44 28 70 50 61 67 65 ", PAGERID(pPage
13acd 72 29 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20 r)));. assert(
13ace 21 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e !isOpen(pPager->
13acf 6a 66 64 29 20 7c 7c 20 70 50 61 67 65 72 2d 3e jfd) || pPager->
13ad0 6a 6f 75 72 6e 61 6c 4f 66 66 3e 30 20 7c 7c 20 journalOff>0 ||
13ad1 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b rc!=SQLITE_OK );
13ad2 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 . if( rc!=SQLIT
13ad3 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 61 73 73 65 E_OK ){. asse
13ad4 72 74 28 20 21 70 50 61 67 65 72 2d 3e 64 62 4d rt( !pPager->dbM
13ad5 6f 64 69 66 69 65 64 20 29 3b 0a 20 20 20 20 2f odified );. /
13ad6 2a 20 49 67 6e 6f 72 65 20 61 6e 79 20 49 4f 20 * Ignore any IO
13ad7 65 72 72 6f 72 20 74 68 61 74 20 6f 63 63 75 72 error that occur
13ad8 73 20 77 69 74 68 69 6e 20 70 61 67 65 72 5f 65 s within pager_e
13ad9 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 29 nd_transaction()
13ada 2e 20 54 68 65 0a 20 20 20 20 2a 2a 20 70 75 72 . The. ** pur
13adb 70 6f 73 65 20 6f 66 20 74 68 69 73 20 63 61 6c pose of this cal
13adc 6c 20 69 73 20 74 6f 20 72 65 73 65 74 20 74 68 l is to reset th
13add 65 20 69 6e 74 65 72 6e 61 6c 20 73 74 61 74 65 e internal state
13ade 20 6f 66 20 74 68 65 20 70 61 67 65 72 0a 20 20 of the pager.
13adf 20 20 2a 2a 20 73 75 62 2d 73 79 73 74 65 6d 2e ** sub-system.
13ae0 20 49 74 20 64 6f 65 73 6e 27 74 20 6d 61 74 74 It doesn't matt
13ae1 65 72 20 69 66 20 74 68 65 20 6a 6f 75 72 6e 61 er if the journa
13ae2 6c 2d 66 69 6c 65 20 69 73 20 6e 6f 74 20 70 72 l-file is not pr
13ae3 6f 70 65 72 6c 79 0a 20 20 20 20 2a 2a 20 66 69 operly. ** fi
13ae4 6e 61 6c 69 7a 65 64 20 61 74 20 74 68 69 73 20 nalized at this
13ae5 70 6f 69 6e 74 20 28 73 69 6e 63 65 20 69 74 20 point (since it
13ae6 69 73 20 6e 6f 74 20 61 20 76 61 6c 69 64 20 6a is not a valid j
13ae7 6f 75 72 6e 61 6c 20 66 69 6c 65 20 61 6e 79 77 ournal file anyw
13ae8 61 79 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ay).. */.
13ae9 70 61 67 65 72 5f 65 6e 64 5f 74 72 61 6e 73 61 pager_end_transa
13aea 63 74 69 6f 6e 28 70 50 61 67 65 72 2c 20 30 29 ction(pPager, 0)
13aeb 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
13aec 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 72 6b c;.}../*.** Mark
13aed 20 61 20 73 69 6e 67 6c 65 20 64 61 74 61 20 70 a single data p
13aee 61 67 65 20 61 73 20 77 72 69 74 65 61 62 6c 65 age as writeable
13aef 2e 20 54 68 65 20 70 61 67 65 20 69 73 20 77 72 . The page is wr
13af0 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 0a itten into the .
13af1 2a 2a 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 ** main journal
13af2 6f 72 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 61 or sub-journal a
13af3 73 20 72 65 71 75 69 72 65 64 2e 20 49 66 20 74 s required. If t
13af4 68 65 20 70 61 67 65 20 69 73 20 77 72 69 74 74 he page is writt
13af5 65 6e 20 69 6e 74 6f 0a 2a 2a 20 6f 6e 65 20 6f en into.** one o
13af6 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 73 2c 20 f the journals,
13af7 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e the correspondin
13af8 67 20 62 69 74 20 69 73 20 73 65 74 20 69 6e 20 g bit is set in
13af9 74 68 65 20 0a 2a 2a 20 50 61 67 65 72 2e 70 49 the .** Pager.pI
13afa 6e 4a 6f 75 72 6e 61 6c 20 62 69 74 76 65 63 20 nJournal bitvec
13afb 61 6e 64 20 74 68 65 20 50 61 67 65 72 53 61 76 and the PagerSav
13afc 65 70 6f 69 6e 74 2e 70 49 6e 53 61 76 65 70 6f epoint.pInSavepo
13afd 69 6e 74 20 62 69 74 76 65 63 73 0a 2a 2a 20 6f int bitvecs.** o
13afe 66 20 61 6e 79 20 6f 70 65 6e 20 73 61 76 65 70 f any open savep
13aff 6f 69 6e 74 73 20 61 73 20 61 70 70 72 6f 70 72 oints as appropr
13b00 69 61 74 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 iate..*/.static
13b01 69 6e 74 20 70 61 67 65 72 5f 77 72 69 74 65 28 int pager_write(
13b02 50 67 48 64 72 20 2a 70 50 67 29 7b 0a 20 20 76 PgHdr *pPg){. v
13b03 6f 69 64 20 2a 70 44 61 74 61 20 3d 20 70 50 67 oid *pData = pPg
13b04 2d 3e 70 44 61 74 61 3b 0a 20 20 50 61 67 65 72 ->pData;. Pager
13b05 20 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e *pPager = pPg->
13b06 70 50 61 67 65 72 3b 0a 20 20 69 6e 74 20 72 63 pPager;. int rc
13b07 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 = SQLITE_OK;..
13b08 20 2f 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 /* This routine
13b09 20 69 73 20 6e 6f 74 20 63 61 6c 6c 65 64 20 75 is not called u
13b0a 6e 6c 65 73 73 20 61 20 74 72 61 6e 73 61 63 74 nless a transact
13b0b 69 6f 6e 20 68 61 73 20 61 6c 72 65 61 64 79 20 ion has already
13b0c 62 65 65 6e 0a 20 20 2a 2a 20 73 74 61 72 74 65 been. ** starte
13b0d 64 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 d.. */. assert
13b0e 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e ( pPager->state>
13b0f 3d 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 =PAGER_RESERVED
13b10 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 61 6e 20 65 );.. /* If an e
13b11 72 72 6f 72 20 68 61 73 20 62 65 65 6e 20 70 72 rror has been pr
13b12 65 76 69 6f 75 73 6c 79 20 64 65 74 65 63 74 65 eviously detecte
13b13 64 2c 20 77 65 20 73 68 6f 75 6c 64 20 6e 6f 74 d, we should not
13b14 20 62 65 0a 20 20 2a 2a 20 63 61 6c 6c 69 6e 67 be. ** calling
13b15 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 this routine.
13b16 52 65 70 65 61 74 20 74 68 65 20 65 72 72 6f 72 Repeat the error
13b17 20 66 6f 72 20 72 6f 62 75 73 74 6e 65 73 73 2e for robustness.
13b18 0a 20 20 2a 2f 0a 20 20 69 66 28 20 4e 45 56 45 . */. if( NEVE
13b19 52 28 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 R(pPager->errCod
13b1a 65 29 20 29 20 20 72 65 74 75 72 6e 20 70 50 61 e) ) return pPa
13b1b 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 0a 20 ger->errCode;..
13b1c 20 2f 2a 20 48 69 67 68 65 72 2d 6c 65 76 65 6c /* Higher-level
13b1d 20 72 6f 75 74 69 6e 65 73 20 6e 65 76 65 72 20 routines never
13b1e 63 61 6c 6c 20 74 68 69 73 20 66 75 6e 63 74 69 call this functi
13b1f 6f 6e 20 69 66 20 64 61 74 61 62 61 73 65 20 69 on if database i
13b20 73 20 6e 6f 74 0a 20 20 2a 2a 20 77 72 69 74 61 s not. ** writa
13b21 62 6c 65 2e 20 20 42 75 74 20 63 68 65 63 6b 20 ble. But check
13b22 61 6e 79 77 61 79 2c 20 6a 75 73 74 20 66 6f 72 anyway, just for
13b23 20 72 6f 62 75 73 74 6e 65 73 73 2e 20 2a 2f 0a robustness. */.
13b24 20 20 69 66 28 20 4e 45 56 45 52 28 70 50 61 67 if( NEVER(pPag
13b25 65 72 2d 3e 72 65 61 64 4f 6e 6c 79 29 20 29 20 er->readOnly) )
13b26 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 50 45 return SQLITE_PE
13b27 52 4d 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 21 RM;.. assert( !
13b28 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 65 pPager->setMaste
13b29 72 20 29 3b 0a 0a 20 20 43 48 45 43 4b 5f 50 41 r );.. CHECK_PA
13b2a 47 45 28 70 50 67 29 3b 0a 0a 20 20 2f 2a 20 4d GE(pPg);.. /* M
13b2b 61 72 6b 20 74 68 65 20 70 61 67 65 20 61 73 20 ark the page as
13b2c 64 69 72 74 79 2e 20 20 49 66 20 74 68 65 20 70 dirty. If the p
13b2d 61 67 65 20 68 61 73 20 61 6c 72 65 61 64 79 20 age has already
13b2e 62 65 65 6e 20 77 72 69 74 74 65 6e 0a 20 20 2a been written. *
13b2f 2a 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c * to the journal
13b30 20 74 68 65 6e 20 77 65 20 63 61 6e 20 72 65 74 then we can ret
13b31 75 72 6e 20 72 69 67 68 74 20 61 77 61 79 2e 0a urn right away..
13b32 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 50 63 */. sqlite3Pc
13b33 61 63 68 65 4d 61 6b 65 44 69 72 74 79 28 70 50 acheMakeDirty(pP
13b34 67 29 3b 0a 20 20 69 66 28 20 70 61 67 65 49 6e g);. if( pageIn
13b35 4a 6f 75 72 6e 61 6c 28 70 50 67 29 20 26 26 20 Journal(pPg) &&
13b36 21 73 75 62 6a 52 65 71 75 69 72 65 73 50 61 67 !subjRequiresPag
13b37 65 28 70 50 67 29 20 29 7b 0a 20 20 20 20 70 50 e(pPg) ){. pP
13b38 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64 ager->dbModified
13b39 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 0a = 1;. }else{..
13b3a 20 20 20 20 2f 2a 20 49 66 20 77 65 20 67 65 74 /* If we get
13b3b 20 74 68 69 73 20 66 61 72 2c 20 69 74 20 6d 65 this far, it me
13b3c 61 6e 73 20 74 68 61 74 20 74 68 65 20 70 61 67 ans that the pag
13b3d 65 20 6e 65 65 64 73 20 74 6f 20 62 65 0a 20 20 e needs to be.
13b3e 20 20 2a 2a 20 77 72 69 74 74 65 6e 20 74 6f 20 ** written to
13b3f 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 the transaction
13b40 6a 6f 75 72 6e 61 6c 20 6f 72 20 74 68 65 20 63 journal or the c
13b41 6b 65 63 6b 70 6f 69 6e 74 20 6a 6f 75 72 6e 61 keckpoint journa
13b42 6c 0a 20 20 20 20 2a 2a 20 6f 72 20 62 6f 74 68 l. ** or both
13b43 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 .. **. **
13b44 48 69 67 68 65 72 20 6c 65 76 65 6c 20 72 6f 75 Higher level rou
13b45 74 69 6e 65 73 20 73 68 6f 75 6c 64 20 68 61 76 tines should hav
13b46 65 20 61 6c 72 65 61 64 79 20 73 74 61 72 74 65 e already starte
13b47 64 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c d a transaction,
13b48 0a 20 20 20 20 2a 2a 20 77 68 69 63 68 20 6d 65 . ** which me
13b49 61 6e 73 20 74 68 65 79 20 68 61 76 65 20 61 63 ans they have ac
13b4a 71 75 69 72 65 64 20 74 68 65 20 6e 65 63 65 73 quired the neces
13b4b 73 61 72 79 20 6c 6f 63 6b 73 20 61 6e 64 20 6f sary locks and o
13b4c 70 65 6e 65 64 0a 20 20 20 20 2a 2a 20 61 20 72 pened. ** a r
13b4d 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e ollback journal.
13b4e 20 20 44 6f 75 62 6c 65 2d 63 68 65 63 6b 20 74 Double-check t
13b4f 6f 20 6d 61 6b 65 73 20 73 75 72 65 20 74 68 69 o makes sure thi
13b50 73 20 69 73 20 74 68 65 20 63 61 73 65 2e 0a 20 s is the case..
13b51 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 */. rc = s
13b52 71 6c 69 74 65 33 50 61 67 65 72 42 65 67 69 6e qlite3PagerBegin
13b53 28 70 50 61 67 65 72 2c 20 30 2c 20 70 50 61 67 (pPager, 0, pPag
13b54 65 72 2d 3e 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 er->subjInMemory
13b55 29 3b 0a 20 20 20 20 69 66 28 20 4e 45 56 45 52 );. if( NEVER
13b56 28 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 (rc!=SQLITE_OK)
13b57 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
13b58 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 rc;. }. if
13b59 28 20 21 69 73 4f 70 65 6e 28 70 50 61 67 65 72 ( !isOpen(pPager
13b5a 2d 3e 6a 66 64 29 20 26 26 20 70 50 61 67 65 72 ->jfd) && pPager
13b5b 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 21 3d 50 ->journalMode!=P
13b5c 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 AGER_JOURNALMODE
13b5d 5f 4f 46 46 20 29 7b 0a 20 20 20 20 20 20 61 73 _OFF ){. as
13b5e 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 75 73 sert( pPager->us
13b5f 65 4a 6f 75 72 6e 61 6c 20 29 3b 0a 20 20 20 20 eJournal );.
13b60 20 20 72 63 20 3d 20 70 61 67 65 72 5f 6f 70 65 rc = pager_ope
13b61 6e 5f 6a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 n_journal(pPager
13b62 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 );. if( rc!
13b63 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 =SQLITE_OK ) ret
13b64 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 urn rc;. }.
13b65 20 20 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 pPager->dbModi
13b66 66 69 65 64 20 3d 20 31 3b 0a 20 20 0a 20 20 20 fied = 1;. .
13b67 20 2f 2a 20 54 68 65 20 74 72 61 6e 73 61 63 74 /* The transact
13b68 69 6f 6e 20 6a 6f 75 72 6e 61 6c 20 6e 6f 77 20 ion journal now
13b69 65 78 69 73 74 73 20 61 6e 64 20 77 65 20 68 61 exists and we ha
13b6a 76 65 20 61 20 52 45 53 45 52 56 45 44 20 6f 72 ve a RESERVED or
13b6b 20 61 6e 0a 20 20 20 20 2a 2a 20 45 58 43 4c 55 an. ** EXCLU
13b6c 53 49 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 SIVE lock on the
13b6d 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 main database f
13b6e 69 6c 65 2e 20 20 57 72 69 74 65 20 74 68 65 20 ile. Write the
13b6f 63 75 72 72 65 6e 74 20 70 61 67 65 20 74 6f 0a current page to.
13b70 20 20 20 20 2a 2a 20 74 68 65 20 74 72 61 6e 73 ** the trans
13b71 61 63 74 69 6f 6e 20 6a 6f 75 72 6e 61 6c 20 69 action journal i
13b72 66 20 69 74 20 69 73 20 6e 6f 74 20 74 68 65 72 f it is not ther
13b73 65 20 61 6c 72 65 61 64 79 2e 0a 20 20 20 20 2a e already.. *
13b74 2f 0a 20 20 20 20 69 66 28 20 21 70 61 67 65 49 /. if( !pageI
13b75 6e 4a 6f 75 72 6e 61 6c 28 70 50 67 29 20 26 26 nJournal(pPg) &&
13b76 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e isOpen(pPager->
13b77 6a 66 64 29 20 29 7b 0a 20 20 20 20 20 20 69 66 jfd) ){. if
13b78 28 20 70 50 67 2d 3e 70 67 6e 6f 3c 3d 70 50 61 ( pPg->pgno<=pPa
13b79 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 ger->dbOrigSize
13b7a 29 7b 0a 20 20 20 20 20 20 20 20 75 33 32 20 63 ){. u32 c
13b7b 6b 73 75 6d 3b 0a 20 20 20 20 20 20 20 20 63 68 ksum;. ch
13b7c 61 72 20 2a 70 44 61 74 61 32 3b 0a 0a 20 20 20 ar *pData2;..
13b7d 20 20 20 20 20 2f 2a 20 57 65 20 73 68 6f 75 6c /* We shoul
13b7e 64 20 6e 65 76 65 72 20 77 72 69 74 65 20 74 6f d never write to
13b7f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
13b80 65 20 74 68 65 20 70 61 67 65 20 74 68 61 74 0a e the page that.
13b81 20 20 20 20 20 20 20 20 2a 2a 20 63 6f 6e 74 61 ** conta
13b82 69 6e 73 20 74 68 65 20 64 61 74 61 62 61 73 65 ins the database
13b83 20 6c 6f 63 6b 73 2e 20 20 54 68 65 20 66 6f 6c locks. The fol
13b84 6c 6f 77 69 6e 67 20 61 73 73 65 72 74 20 76 65 lowing assert ve
13b85 72 69 66 69 65 73 0a 20 20 20 20 20 20 20 20 2a rifies. *
13b86 2a 20 74 68 61 74 20 77 65 20 64 6f 20 6e 6f 74 * that we do not
13b87 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 73 73 . */. ass
13b88 65 72 74 28 20 70 50 67 2d 3e 70 67 6e 6f 21 3d ert( pPg->pgno!=
13b89 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 PAGER_MJ_PGNO(pP
13b8a 61 67 65 72 29 20 29 3b 0a 20 20 20 20 20 20 20 ager) );.
13b8b 20 43 4f 44 45 43 32 28 70 50 61 67 65 72 2c 20 CODEC2(pPager,
13b8c 70 44 61 74 61 2c 20 70 50 67 2d 3e 70 67 6e 6f pData, pPg->pgno
13b8d 2c 20 37 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 , 7, return SQLI
13b8e 54 45 5f 4e 4f 4d 45 4d 2c 20 70 44 61 74 61 32 TE_NOMEM, pData2
13b8f 29 3b 0a 20 20 20 20 20 20 20 20 63 6b 73 75 6d );. cksum
13b90 20 3d 20 70 61 67 65 72 5f 63 6b 73 75 6d 28 70 = pager_cksum(p
13b91 50 61 67 65 72 2c 20 28 75 38 2a 29 70 44 61 74 Pager, (u8*)pDat
13b92 61 32 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 a2);. rc
13b93 3d 20 77 72 69 74 65 33 32 62 69 74 73 28 70 50 = write32bits(pP
13b94 61 67 65 72 2d 3e 6a 66 64 2c 20 70 50 61 67 65 ager->jfd, pPage
13b95 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 70 r->journalOff, p
13b96 50 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 Pg->pgno);.
13b97 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
13b98 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
13b99 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
13b9a 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 Write(pPager->jf
13b9b 64 2c 20 70 44 61 74 61 32 2c 20 70 50 61 67 65 d, pData2, pPage
13b9c 72 2d 3e 70 61 67 65 53 69 7a 65 2c 0a 20 20 20 r->pageSize,.
13b9d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13b9e 20 20 20 20 20 20 20 20 20 20 20 70 50 61 67 65 pPage
13b9f 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b 20 r->journalOff +
13ba0 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70 50 4);. pP
13ba1 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 ager->journalOff
13ba2 20 2b 3d 20 70 50 61 67 65 72 2d 3e 70 61 67 65 += pPager->page
13ba3 53 69 7a 65 2b 34 3b 0a 20 20 20 20 20 20 20 20 Size+4;.
13ba4 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 }. if( rc
13ba5 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
13ba6 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 77 72 rc = wr
13ba7 69 74 65 33 32 62 69 74 73 28 70 50 61 67 65 72 ite32bits(pPager
13ba8 2d 3e 6a 66 64 2c 20 70 50 61 67 65 72 2d 3e 6a ->jfd, pPager->j
13ba9 6f 75 72 6e 61 6c 4f 66 66 2c 20 63 6b 73 75 6d ournalOff, cksum
13baa 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70 50 61 );. pPa
13bab 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 ger->journalOff
13bac 2b 3d 20 34 3b 0a 20 20 20 20 20 20 20 20 7d 0a += 4;. }.
13bad 20 20 20 20 20 20 20 20 49 4f 54 52 41 43 45 28 IOTRACE(
13bae 28 22 4a 4f 55 54 20 25 70 20 25 64 20 25 6c 6c ("JOUT %p %d %ll
13baf 64 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c d %d\n", pPager,
13bb0 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 0a 20 20 20 pPg->pgno, .
13bb1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 50 pP
13bb2 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 ager->journalOff
13bb3 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 , pPager->pageSi
13bb4 7a 65 29 29 3b 0a 20 20 20 20 20 20 20 20 50 41 ze));. PA
13bb5 47 45 52 5f 49 4e 43 52 28 73 71 6c 69 74 65 33 GER_INCR(sqlite3
13bb6 5f 70 61 67 65 72 5f 77 72 69 74 65 6a 5f 63 6f _pager_writej_co
13bb7 75 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 50 41 unt);. PA
13bb8 47 45 52 54 52 41 43 45 28 28 22 4a 4f 55 52 4e GERTRACE(("JOURN
13bb9 41 4c 20 25 64 20 70 61 67 65 20 25 64 20 6e 65 AL %d page %d ne
13bba 65 64 53 79 6e 63 3d 25 64 20 68 61 73 68 28 25 edSync=%d hash(%
13bbb 30 38 78 29 5c 6e 22 2c 0a 20 20 20 20 20 20 20 08x)\n",.
13bbc 20 20 20 20 20 20 50 41 47 45 52 49 44 28 70 50 PAGERID(pP
13bbd 61 67 65 72 29 2c 20 70 50 67 2d 3e 70 67 6e 6f ager), pPg->pgno
13bbe 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 , .
13bbf 28 28 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 ((pPg->flags&PGH
13bc0 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 3f 31 3a DR_NEED_SYNC)?1:
13bc1 30 29 2c 20 70 61 67 65 72 5f 70 61 67 65 68 61 0), pager_pageha
13bc2 73 68 28 70 50 67 29 29 29 3b 0a 0a 20 20 20 20 sh(pPg)));..
13bc3 20 20 20 20 2f 2a 20 45 76 65 6e 20 69 66 20 61 /* Even if a
13bc4 6e 20 49 4f 20 6f 72 20 64 69 73 6b 66 75 6c 6c n IO or diskfull
13bc5 20 65 72 72 6f 72 20 6f 63 63 75 72 72 65 64 20 error occurred
13bc6 77 68 69 6c 65 20 6a 6f 75 72 6e 61 6c 6c 69 6e while journallin
13bc7 67 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a g the. **
13bc8 20 70 61 67 65 20 69 6e 20 74 68 65 20 62 6c 6f page in the blo
13bc9 63 6b 20 61 62 6f 76 65 2c 20 73 65 74 20 74 68 ck above, set th
13bca 65 20 6e 65 65 64 2d 73 79 6e 63 20 66 6c 61 67 e need-sync flag
13bcb 20 66 6f 72 20 74 68 65 20 70 61 67 65 2e 0a 20 for the page..
13bcc 20 20 20 20 20 20 20 2a 2a 20 4f 74 68 65 72 77 ** Otherw
13bcd 69 73 65 2c 20 77 68 65 6e 20 74 68 65 20 74 72 ise, when the tr
13bce 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 72 6f 6c ansaction is rol
13bcf 6c 65 64 20 62 61 63 6b 2c 20 74 68 65 20 6c 6f led back, the lo
13bd0 67 69 63 20 69 6e 0a 20 20 20 20 20 20 20 20 2a gic in. *
13bd1 2a 20 70 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f 70 * playback_one_p
13bd2 61 67 65 28 29 20 77 69 6c 6c 20 74 68 69 6e 6b age() will think
13bd3 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20 6e that the page n
13bd4 65 65 64 73 20 74 6f 20 62 65 20 72 65 73 74 6f eeds to be resto
13bd5 72 65 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 red. ** i
13bd6 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 n the database f
13bd7 69 6c 65 2e 20 41 6e 64 20 69 66 20 61 6e 20 49 ile. And if an I
13bd8 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77 O error occurs w
13bd9 68 69 6c 65 20 64 6f 69 6e 67 20 73 6f 2c 0a 20 hile doing so,.
13bda 20 20 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 63 ** then c
13bdb 6f 72 72 75 70 74 69 6f 6e 20 6d 61 79 20 66 6f orruption may fo
13bdc 6c 6c 6f 77 2e 0a 20 20 20 20 20 20 20 20 2a 2f llow.. */
13bdd 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 70 50 . if( !pP
13bde 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 29 7b 0a ager->noSync ){.
13bdf 20 20 20 20 20 20 20 20 20 20 70 50 67 2d 3e 66 pPg->f
13be0 6c 61 67 73 20 7c 3d 20 50 47 48 44 52 5f 4e 45 lags |= PGHDR_NE
13be1 45 44 5f 53 59 4e 43 3b 0a 20 20 20 20 20 20 20 ED_SYNC;.
13be2 20 20 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 pPager->needS
13be3 79 6e 63 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 ync = 1;.
13be4 20 7d 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 41 }.. /* A
13be5 6e 20 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75 n error has occu
13be6 72 72 65 64 20 77 72 69 74 69 6e 67 20 74 6f 20 rred writing to
13be7 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
13be8 2e 20 54 68 65 20 0a 20 20 20 20 20 20 20 20 2a . The . *
13be9 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 69 * transaction wi
13bea 6c 6c 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 ll be rolled bac
13beb 6b 20 62 79 20 74 68 65 20 6c 61 79 65 72 20 61 k by the layer a
13bec 62 6f 76 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f bove.. */
13bed 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 . if( rc!
13bee 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
13bef 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 return r
13bf0 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 c;. }..
13bf1 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 52 pPager->nR
13bf2 65 63 2b 2b 3b 0a 20 20 20 20 20 20 20 20 61 73 ec++;. as
13bf3 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 70 49 sert( pPager->pI
13bf4 6e 4a 6f 75 72 6e 61 6c 21 3d 30 20 29 3b 0a 20 nJournal!=0 );.
13bf5 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
13bf6 74 65 33 42 69 74 76 65 63 53 65 74 28 70 50 61 te3BitvecSet(pPa
13bf7 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 2c ger->pInJournal,
13bf8 20 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 pPg->pgno);.
13bf9 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72 testcase( r
13bfa 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 c==SQLITE_NOMEM
13bfb 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 );. asser
13bfc 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b t( rc==SQLITE_OK
13bfd 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e || rc==SQLITE_N
13bfe 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 20 20 20 20 OMEM );.
13bff 72 63 20 7c 3d 20 61 64 64 54 6f 53 61 76 65 70 rc |= addToSavep
13c00 6f 69 6e 74 42 69 74 76 65 63 73 28 70 50 61 67 ointBitvecs(pPag
13c01 65 72 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a er, pPg->pgno);.
13c02 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d if( rc!=
13c03 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
13c04 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72 assert( r
13c05 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 c==SQLITE_NOMEM
13c06 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 );. ret
13c07 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20 urn rc;.
13c08 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }. }else{.
13c09 20 20 20 20 20 20 20 69 66 28 20 21 70 50 61 67 if( !pPag
13c0a 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 74 61 72 74 er->journalStart
13c0b 65 64 20 26 26 20 21 70 50 61 67 65 72 2d 3e 6e ed && !pPager->n
13c0c 6f 53 79 6e 63 20 29 7b 0a 20 20 20 20 20 20 20 oSync ){.
13c0d 20 20 20 70 50 67 2d 3e 66 6c 61 67 73 20 7c 3d pPg->flags |=
13c0e 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 PGHDR_NEED_SYNC
13c0f 3b 0a 20 20 20 20 20 20 20 20 20 20 70 50 61 67 ;. pPag
13c10 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 31 er->needSync = 1
13c11 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
13c12 20 20 20 20 50 41 47 45 52 54 52 41 43 45 28 28 PAGERTRACE((
13c13 22 41 50 50 45 4e 44 20 25 64 20 70 61 67 65 20 "APPEND %d page
13c14 25 64 20 6e 65 65 64 53 79 6e 63 3d 25 64 5c 6e %d needSync=%d\n
13c15 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ",.
13c16 20 20 20 50 41 47 45 52 49 44 28 70 50 61 67 65 PAGERID(pPage
13c17 72 29 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 0a 20 r), pPg->pgno,.
13c18 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 28 ((
13c19 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 pPg->flags&PGHDR
13c1a 5f 4e 45 45 44 5f 53 59 4e 43 29 3f 31 3a 30 29 _NEED_SYNC)?1:0)
13c1b 29 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ));. }.
13c1c 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 49 66 20 74 }. . /* If t
13c1d 68 65 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 he statement jou
13c1e 72 6e 61 6c 20 69 73 20 6f 70 65 6e 20 61 6e 64 rnal is open and
13c1f 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 the page is not
13c20 20 69 6e 20 69 74 2c 0a 20 20 20 20 2a 2a 20 74 in it,. ** t
13c21 68 65 6e 20 77 72 69 74 65 20 74 68 65 20 63 75 hen write the cu
13c22 72 72 65 6e 74 20 70 61 67 65 20 74 6f 20 74 68 rrent page to th
13c23 65 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 e statement jour
13c24 6e 61 6c 2e 20 20 4e 6f 74 65 20 74 68 61 74 0a nal. Note that.
13c25 20 20 20 20 2a 2a 20 74 68 65 20 73 74 61 74 65 ** the state
13c26 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 ment journal for
13c27 6d 61 74 20 64 69 66 66 65 72 73 20 66 72 6f 6d mat differs from
13c28 20 74 68 65 20 73 74 61 6e 64 61 72 64 20 6a 6f the standard jo
13c29 75 72 6e 61 6c 20 66 6f 72 6d 61 74 0a 20 20 20 urnal format.
13c2a 20 2a 2a 20 69 6e 20 74 68 61 74 20 69 74 20 6f ** in that it o
13c2b 6d 69 74 73 20 74 68 65 20 63 68 65 63 6b 73 75 mits the checksu
13c2c 6d 73 20 61 6e 64 20 74 68 65 20 68 65 61 64 65 ms and the heade
13c2d 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 r.. */. if
13c2e 28 20 73 75 62 6a 52 65 71 75 69 72 65 73 50 61 ( subjRequiresPa
13c2f 67 65 28 70 50 67 29 20 29 7b 0a 20 20 20 20 20 ge(pPg) ){.
13c30 20 72 63 20 3d 20 73 75 62 6a 6f 75 72 6e 61 6c rc = subjournal
13c31 50 61 67 65 28 70 50 67 29 3b 0a 20 20 20 20 7d Page(pPg);. }
13c32 0a 20 20 7d 0a 0a 20 20 2f 2a 20 55 70 64 61 74 . }.. /* Updat
13c33 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 e the database s
13c34 69 7a 65 20 61 6e 64 20 72 65 74 75 72 6e 2e 0a ize and return..
13c35 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 */. assert( p
13c36 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 Pager->state>=PA
13c37 47 45 52 5f 53 48 41 52 45 44 20 29 3b 0a 20 20 GER_SHARED );.
13c38 69 66 28 20 70 50 61 67 65 72 2d 3e 64 62 53 69 if( pPager->dbSi
13c39 7a 65 3c 70 50 67 2d 3e 70 67 6e 6f 20 29 7b 0a ze<pPg->pgno ){.
13c3a 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 pPager->dbSi
13c3b 7a 65 20 3d 20 70 50 67 2d 3e 70 67 6e 6f 3b 0a ze = pPg->pgno;.
13c3c 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
13c3d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 72 6b 20 61 .}../*.** Mark a
13c3e 20 64 61 74 61 20 70 61 67 65 20 61 73 20 77 72 data page as wr
13c3f 69 74 65 61 62 6c 65 2e 20 54 68 69 73 20 72 6f iteable. This ro
13c40 75 74 69 6e 65 20 6d 75 73 74 20 62 65 20 63 61 utine must be ca
13c41 6c 6c 65 64 20 62 65 66 6f 72 65 20 0a 2a 2a 20 lled before .**
13c42 6d 61 6b 69 6e 67 20 63 68 61 6e 67 65 73 20 74 making changes t
13c43 6f 20 61 20 70 61 67 65 2e 20 54 68 65 20 63 61 o a page. The ca
13c44 6c 6c 65 72 20 6d 75 73 74 20 63 68 65 63 6b 20 ller must check
13c45 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 the return value
13c46 20 0a 2a 2a 20 6f 66 20 74 68 69 73 20 66 75 6e .** of this fun
13c47 63 74 69 6f 6e 20 61 6e 64 20 62 65 20 63 61 72 ction and be car
13c48 65 66 75 6c 20 6e 6f 74 20 74 6f 20 63 68 61 6e eful not to chan
13c49 67 65 20 61 6e 79 20 70 61 67 65 20 64 61 74 61 ge any page data
13c4a 20 75 6e 6c 65 73 73 20 0a 2a 2a 20 74 68 69 73 unless .** this
13c4b 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 routine returns
13c4c 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a SQLITE_OK..**.*
13c4d 2a 20 54 68 65 20 64 69 66 66 65 72 65 6e 63 65 * The difference
13c4e 20 62 65 74 77 65 65 6e 20 74 68 69 73 20 66 75 between this fu
13c4f 6e 63 74 69 6f 6e 20 61 6e 64 20 70 61 67 65 72 nction and pager
13c50 5f 77 72 69 74 65 28 29 20 69 73 20 74 68 61 74 _write() is that
13c51 20 74 68 69 73 0a 2a 2a 20 66 75 6e 63 74 69 6f this.** functio
13c52 6e 20 61 6c 73 6f 20 64 65 61 6c 73 20 77 69 74 n also deals wit
13c53 68 20 74 68 65 20 73 70 65 63 69 61 6c 20 63 61 h the special ca
13c54 73 65 20 77 68 65 72 65 20 32 20 6f 72 20 6d 6f se where 2 or mo
13c55 72 65 20 70 61 67 65 73 0a 2a 2a 20 66 69 74 20 re pages.** fit
13c56 6f 6e 20 61 20 73 69 6e 67 6c 65 20 64 69 73 6b on a single disk
13c57 20 73 65 63 74 6f 72 2e 20 49 6e 20 74 68 69 73 sector. In this
13c58 20 63 61 73 65 20 61 6c 6c 20 63 6f 2d 72 65 73 case all co-res
13c59 69 64 65 6e 74 20 70 61 67 65 73 0a 2a 2a 20 6d ident pages.** m
13c5a 75 73 74 20 68 61 76 65 20 62 65 65 6e 20 77 72 ust have been wr
13c5b 69 74 74 65 6e 20 74 6f 20 74 68 65 20 6a 6f 75 itten to the jou
13c5c 72 6e 61 6c 20 66 69 6c 65 20 62 65 66 6f 72 65 rnal file before
13c5d 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2a 0a 2a returning..**.*
13c5e 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 * If an error oc
13c5f 63 75 72 73 2c 20 53 51 4c 49 54 45 5f 4e 4f 4d curs, SQLITE_NOM
13c60 45 4d 20 6f 72 20 61 6e 20 49 4f 20 65 72 72 6f EM or an IO erro
13c61 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e r code is return
13c62 65 64 0a 2a 2a 20 61 73 20 61 70 70 72 6f 70 72 ed.** as appropr
13c63 69 61 74 65 2e 20 4f 74 68 65 72 77 69 73 65 2c iate. Otherwise,
13c64 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53 SQLITE_OK..*/.S
13c65 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
13c66 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 t sqlite3PagerWr
13c67 69 74 65 28 44 62 50 61 67 65 20 2a 70 44 62 50 ite(DbPage *pDbP
13c68 61 67 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d age){. int rc =
13c69 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 50 SQLITE_OK;.. P
13c6a 67 48 64 72 20 2a 70 50 67 20 3d 20 70 44 62 50 gHdr *pPg = pDbP
13c6b 61 67 65 3b 0a 20 20 50 61 67 65 72 20 2a 70 50 age;. Pager *pP
13c6c 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 67 ager = pPg->pPag
13c6d 65 72 3b 0a 20 20 50 67 6e 6f 20 6e 50 61 67 65 er;. Pgno nPage
13c6e 50 65 72 53 65 63 74 6f 72 20 3d 20 28 70 50 61 PerSector = (pPa
13c6f 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 2f ger->sectorSize/
13c70 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 pPager->pageSize
13c71 29 3b 0a 0a 20 20 69 66 28 20 6e 50 61 67 65 50 );.. if( nPageP
13c72 65 72 53 65 63 74 6f 72 3e 31 20 29 7b 0a 20 20 erSector>1 ){.
13c73 20 20 50 67 6e 6f 20 6e 50 61 67 65 43 6f 75 6e Pgno nPageCoun
13c74 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 t; /* T
13c75 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 otal number of p
13c76 61 67 65 73 20 69 6e 20 64 61 74 61 62 61 73 65 ages in database
13c77 20 66 69 6c 65 20 2a 2f 0a 20 20 20 20 50 67 6e file */. Pgn
13c78 6f 20 70 67 31 3b 20 20 20 20 20 20 20 20 20 20 o pg1;
13c79 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 /* First
13c7a 70 61 67 65 20 6f 66 20 74 68 65 20 73 65 63 74 page of the sect
13c7b 6f 72 20 70 50 67 20 69 73 20 6c 6f 63 61 74 65 or pPg is locate
13c7c 64 20 6f 6e 2e 20 2a 2f 0a 20 20 20 20 69 6e 74 d on. */. int
13c7d 20 6e 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 nPage;
13c7e 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
13c7f 20 6f 66 20 70 61 67 65 73 20 73 74 61 72 74 69 of pages starti
13c80 6e 67 20 61 74 20 70 67 31 20 74 6f 20 6a 6f 75 ng at pg1 to jou
13c81 72 6e 61 6c 20 2a 2f 0a 20 20 20 20 69 6e 74 20 rnal */. int
13c82 69 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ii;
13c83 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f /* Loop co
13c84 75 6e 74 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 unter */. int
13c85 20 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b 20 20 needSync = 0;
13c86 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
13c87 66 20 61 6e 79 20 70 61 67 65 20 68 61 73 20 50 f any page has P
13c88 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 2a GHDR_NEED_SYNC *
13c89 2f 0a 0a 20 20 20 20 2f 2a 20 53 65 74 20 74 68 /.. /* Set th
13c8a 65 20 64 6f 4e 6f 74 53 79 6e 63 20 66 6c 61 67 e doNotSync flag
13c8b 20 74 6f 20 31 2e 20 54 68 69 73 20 69 73 20 62 to 1. This is b
13c8c 65 63 61 75 73 65 20 77 65 20 63 61 6e 6e 6f 74 ecause we cannot
13c8d 20 61 6c 6c 6f 77 20 61 20 6a 6f 75 72 6e 61 6c allow a journal
13c8e 0a 20 20 20 20 2a 2a 20 68 65 61 64 65 72 20 74 . ** header t
13c8f 6f 20 62 65 20 77 72 69 74 74 65 6e 20 62 65 74 o be written bet
13c90 77 65 65 6e 20 74 68 65 20 70 61 67 65 73 20 6a ween the pages j
13c91 6f 75 72 6e 61 6c 65 64 20 62 79 20 74 68 69 73 ournaled by this
13c92 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 20 20 2a function.. *
13c93 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 4d /. assert( !M
13c94 45 4d 44 42 20 29 3b 0a 20 20 20 20 61 73 73 65 EMDB );. asse
13c95 72 74 28 20 70 50 61 67 65 72 2d 3e 64 6f 4e 6f rt( pPager->doNo
13c96 74 53 79 6e 63 3d 3d 30 20 29 3b 0a 20 20 20 20 tSync==0 );.
13c97 70 50 61 67 65 72 2d 3e 64 6f 4e 6f 74 53 79 6e pPager->doNotSyn
13c98 63 20 3d 20 31 3b 0a 0a 20 20 20 20 2f 2a 20 54 c = 1;.. /* T
13c99 68 69 73 20 74 72 69 63 6b 20 61 73 73 75 6d 65 his trick assume
13c9a 73 20 74 68 61 74 20 62 6f 74 68 20 74 68 65 20 s that both the
13c9b 70 61 67 65 2d 73 69 7a 65 20 61 6e 64 20 73 65 page-size and se
13c9c 63 74 6f 72 2d 73 69 7a 65 20 61 72 65 0a 20 20 ctor-size are.
13c9d 20 20 2a 2a 20 61 6e 20 69 6e 74 65 67 65 72 20 ** an integer
13c9e 70 6f 77 65 72 20 6f 66 20 32 2e 20 49 74 20 73 power of 2. It s
13c9f 65 74 73 20 76 61 72 69 61 62 6c 65 20 70 67 31 ets variable pg1
13ca0 20 74 6f 20 74 68 65 20 69 64 65 6e 74 69 66 69 to the identifi
13ca1 65 72 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 er. ** of the
13ca2 20 66 69 72 73 74 20 70 61 67 65 20 6f 66 20 74 first page of t
13ca3 68 65 20 73 65 63 74 6f 72 20 70 50 67 20 69 73 he sector pPg is
13ca4 20 6c 6f 63 61 74 65 64 20 6f 6e 2e 0a 20 20 20 located on..
13ca5 20 2a 2f 0a 20 20 20 20 70 67 31 20 3d 20 28 28 */. pg1 = ((
13ca6 70 50 67 2d 3e 70 67 6e 6f 2d 31 29 20 26 20 7e pPg->pgno-1) & ~
13ca7 28 6e 50 61 67 65 50 65 72 53 65 63 74 6f 72 2d (nPagePerSector-
13ca8 31 29 29 20 2b 20 31 3b 0a 0a 20 20 20 20 73 71 1)) + 1;.. sq
13ca9 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 63 6f lite3PagerPageco
13caa 75 6e 74 28 70 50 61 67 65 72 2c 20 28 69 6e 74 unt(pPager, (int
13cab 20 2a 29 26 6e 50 61 67 65 43 6f 75 6e 74 29 3b *)&nPageCount);
13cac 0a 20 20 20 20 69 66 28 20 70 50 67 2d 3e 70 67 . if( pPg->pg
13cad 6e 6f 3e 6e 50 61 67 65 43 6f 75 6e 74 20 29 7b no>nPageCount ){
13cae 0a 20 20 20 20 20 20 6e 50 61 67 65 20 3d 20 28 . nPage = (
13caf 70 50 67 2d 3e 70 67 6e 6f 20 2d 20 70 67 31 29 pPg->pgno - pg1)
13cb0 2b 31 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 +1;. }else if
13cb1 28 20 28 70 67 31 2b 6e 50 61 67 65 50 65 72 53 ( (pg1+nPagePerS
13cb2 65 63 74 6f 72 2d 31 29 3e 6e 50 61 67 65 43 6f ector-1)>nPageCo
13cb3 75 6e 74 20 29 7b 0a 20 20 20 20 20 20 6e 50 61 unt ){. nPa
13cb4 67 65 20 3d 20 6e 50 61 67 65 43 6f 75 6e 74 2b ge = nPageCount+
13cb5 31 2d 70 67 31 3b 0a 20 20 20 20 7d 65 6c 73 65 1-pg1;. }else
13cb6 7b 0a 20 20 20 20 20 20 6e 50 61 67 65 20 3d 20 {. nPage =
13cb7 6e 50 61 67 65 50 65 72 53 65 63 74 6f 72 3b 0a nPagePerSector;.
13cb8 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 }. assert
13cb9 28 6e 50 61 67 65 3e 30 29 3b 0a 20 20 20 20 61 (nPage>0);. a
13cba 73 73 65 72 74 28 70 67 31 3c 3d 70 50 67 2d 3e ssert(pg1<=pPg->
13cbb 70 67 6e 6f 29 3b 0a 20 20 20 20 61 73 73 65 72 pgno);. asser
13cbc 74 28 28 70 67 31 2b 6e 50 61 67 65 29 3e 70 50 t((pg1+nPage)>pP
13cbd 67 2d 3e 70 67 6e 6f 29 3b 0a 0a 20 20 20 20 66 g->pgno);.. f
13cbe 6f 72 28 69 69 3d 30 3b 20 69 69 3c 6e 50 61 67 or(ii=0; ii<nPag
13cbf 65 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f e && rc==SQLITE_
13cc0 4f 4b 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20 OK; ii++){.
13cc1 20 50 67 6e 6f 20 70 67 20 3d 20 70 67 31 2b 69 Pgno pg = pg1+i
13cc2 69 3b 0a 20 20 20 20 20 20 50 67 48 64 72 20 2a i;. PgHdr *
13cc3 70 50 61 67 65 3b 0a 20 20 20 20 20 20 69 66 28 pPage;. if(
13cc4 20 70 67 3d 3d 70 50 67 2d 3e 70 67 6e 6f 20 7c pg==pPg->pgno |
13cc5 7c 20 21 73 71 6c 69 74 65 33 42 69 74 76 65 63 | !sqlite3Bitvec
13cc6 54 65 73 74 28 70 50 61 67 65 72 2d 3e 70 49 6e Test(pPager->pIn
13cc7 4a 6f 75 72 6e 61 6c 2c 20 70 67 29 20 29 7b 0a Journal, pg) ){.
13cc8 20 20 20 20 20 20 20 20 69 66 28 20 70 67 21 3d if( pg!=
13cc9 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 PAGER_MJ_PGNO(pP
13cca 61 67 65 72 29 20 29 7b 0a 20 20 20 20 20 20 20 ager) ){.
13ccb 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 rc = sqlite3P
13ccc 61 67 65 72 47 65 74 28 70 50 61 67 65 72 2c 20 agerGet(pPager,
13ccd 70 67 2c 20 26 70 50 61 67 65 29 3b 0a 20 20 20 pg, &pPage);.
13cce 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 if( rc==S
13ccf 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
13cd0 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 rc = pag
13cd1 65 72 5f 77 72 69 74 65 28 70 50 61 67 65 29 3b er_write(pPage);
13cd2 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 . if(
13cd3 20 70 50 61 67 65 2d 3e 66 6c 61 67 73 26 50 47 pPage->flags&PG
13cd4 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 29 7b HDR_NEED_SYNC ){
13cd5 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6e . n
13cd6 65 65 64 53 79 6e 63 20 3d 20 31 3b 0a 20 20 20 eedSync = 1;.
13cd7 20 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 asser
13cd8 74 28 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 t(pPager->needSy
13cd9 6e 63 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 nc);.
13cda 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 }. s
13cdb 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 qlite3PagerUnref
13cdc 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 (pPage);.
13cdd 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 }. }.
13cde 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 28 }else if( (
13cdf 70 50 61 67 65 20 3d 20 70 61 67 65 72 5f 6c 6f pPage = pager_lo
13ce0 6f 6b 75 70 28 70 50 61 67 65 72 2c 20 70 67 29 okup(pPager, pg)
13ce1 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 )!=0 ){.
13ce2 69 66 28 20 70 50 61 67 65 2d 3e 66 6c 61 67 73 if( pPage->flags
13ce3 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 &PGHDR_NEED_SYNC
13ce4 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e 65 ){. ne
13ce5 65 64 53 79 6e 63 20 3d 20 31 3b 0a 20 20 20 20 edSync = 1;.
13ce6 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 73 71 }. sq
13ce7 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 lite3PagerUnref(
13ce8 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 7d 0a pPage);. }.
13ce9 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 }.. /* If
13cea 20 74 68 65 20 50 47 48 44 52 5f 4e 45 45 44 5f the PGHDR_NEED_
13ceb 53 59 4e 43 20 66 6c 61 67 20 69 73 20 73 65 74 SYNC flag is set
13cec 20 66 6f 72 20 61 6e 79 20 6f 66 20 74 68 65 20 for any of the
13ced 6e 50 61 67 65 20 70 61 67 65 73 20 0a 20 20 20 nPage pages .
13cee 20 2a 2a 20 73 74 61 72 74 69 6e 67 20 61 74 20 ** starting at
13cef 70 67 31 2c 20 74 68 65 6e 20 69 74 20 6e 65 65 pg1, then it nee
13cf0 64 73 20 74 6f 20 62 65 20 73 65 74 20 66 6f 72 ds to be set for
13cf1 20 61 6c 6c 20 6f 66 20 74 68 65 6d 2e 20 42 65 all of them. Be
13cf2 63 61 75 73 65 0a 20 20 20 20 2a 2a 20 77 72 69 cause. ** wri
13cf3 74 69 6e 67 20 74 6f 20 61 6e 79 20 6f 66 20 74 ting to any of t
13cf4 68 65 73 65 20 6e 50 61 67 65 20 70 61 67 65 73 hese nPage pages
13cf5 20 6d 61 79 20 64 61 6d 61 67 65 20 74 68 65 20 may damage the
13cf6 6f 74 68 65 72 73 2c 20 74 68 65 0a 20 20 20 20 others, the.
13cf7 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 ** journal file
13cf8 6d 75 73 74 20 63 6f 6e 74 61 69 6e 20 73 79 6e must contain syn
13cf9 63 28 29 65 64 20 63 6f 70 69 65 73 20 6f 66 20 c()ed copies of
13cfa 61 6c 6c 20 6f 66 20 74 68 65 6d 0a 20 20 20 20 all of them.
13cfb 2a 2a 20 62 65 66 6f 72 65 20 61 6e 79 20 6f 66 ** before any of
13cfc 20 74 68 65 6d 20 63 61 6e 20 62 65 20 77 72 69 them can be wri
13cfd 74 74 65 6e 20 6f 75 74 20 74 6f 20 74 68 65 20 tten out to the
13cfe 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 20 database file..
13cff 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 */. if( rc
13d00 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 6e ==SQLITE_OK && n
13d01 65 65 64 53 79 6e 63 20 29 7b 0a 20 20 20 20 20 eedSync ){.
13d02 20 61 73 73 65 72 74 28 20 21 4d 45 4d 44 42 20 assert( !MEMDB
13d03 26 26 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e && pPager->noSyn
13d04 63 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 66 6f c==0 );. fo
13d05 72 28 69 69 3d 30 3b 20 69 69 3c 6e 50 61 67 65 r(ii=0; ii<nPage
13d06 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 ; ii++){.
13d07 20 50 67 48 64 72 20 2a 70 50 61 67 65 20 3d 20 PgHdr *pPage =
13d08 70 61 67 65 72 5f 6c 6f 6f 6b 75 70 28 70 50 61 pager_lookup(pPa
13d09 67 65 72 2c 20 70 67 31 2b 69 69 29 3b 0a 20 20 ger, pg1+ii);.
13d0a 20 20 20 20 20 20 69 66 28 20 70 50 61 67 65 20 if( pPage
13d0b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 50 61 ){. pPa
13d0c 67 65 2d 3e 66 6c 61 67 73 20 7c 3d 20 50 47 48 ge->flags |= PGH
13d0d 44 52 5f 4e 45 45 44 5f 53 59 4e 43 3b 0a 20 20 DR_NEED_SYNC;.
13d0e 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 sqlite3P
13d0f 61 67 65 72 55 6e 72 65 66 28 70 50 61 67 65 29 agerUnref(pPage)
13d10 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
13d11 20 20 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74 }. assert
13d12 28 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e (pPager->needSyn
13d13 63 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 61 c);. }.. a
13d14 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 64 ssert( pPager->d
13d15 6f 4e 6f 74 53 79 6e 63 3d 3d 31 20 29 3b 0a 20 oNotSync==1 );.
13d16 20 20 20 70 50 61 67 65 72 2d 3e 64 6f 4e 6f 74 pPager->doNot
13d17 53 79 6e 63 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 Sync = 0;. }els
13d18 65 7b 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65 e{. rc = page
13d19 72 5f 77 72 69 74 65 28 70 44 62 50 61 67 65 29 r_write(pDbPage)
13d1a 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
13d1b 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 c;.}../*.** Retu
13d1c 72 6e 20 54 52 55 45 20 69 66 20 74 68 65 20 70 rn TRUE if the p
13d1d 61 67 65 20 67 69 76 65 6e 20 69 6e 20 74 68 65 age given in the
13d1e 20 61 72 67 75 6d 65 6e 74 20 77 61 73 20 70 72 argument was pr
13d1f 65 76 69 6f 75 73 6c 79 20 70 61 73 73 65 64 0a eviously passed.
13d20 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 50 61 67 ** to sqlite3Pag
13d21 65 72 57 72 69 74 65 28 29 2e 20 20 49 6e 20 6f erWrite(). In o
13d22 74 68 65 72 20 77 6f 72 64 73 2c 20 72 65 74 75 ther words, retu
13d23 72 6e 20 54 52 55 45 20 69 66 20 69 74 20 69 73 rn TRUE if it is
13d24 20 6f 6b 0a 2a 2a 20 74 6f 20 63 68 61 6e 67 65 ok.** to change
13d25 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 the content of
13d26 74 68 65 20 70 61 67 65 2e 0a 2a 2f 0a 23 69 66 the page..*/.#if
13d27 6e 64 65 66 20 4e 44 45 42 55 47 0a 53 51 4c 49 ndef NDEBUG.SQLI
13d28 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
13d29 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 qlite3PagerIswri
13d2a 74 65 61 62 6c 65 28 44 62 50 61 67 65 20 2a 70 teable(DbPage *p
13d2b 50 67 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 Pg){. return pP
13d2c 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 g->flags&PGHDR_D
13d2d 49 52 54 59 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a IRTY;.}.#endif..
13d2e 2f 2a 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 6f 20 /*.** A call to
13d2f 74 68 69 73 20 72 6f 75 74 69 6e 65 20 74 65 6c this routine tel
13d30 6c 73 20 74 68 65 20 70 61 67 65 72 20 74 68 61 ls the pager tha
13d31 74 20 69 74 20 69 73 20 6e 6f 74 20 6e 65 63 65 t it is not nece
13d32 73 73 61 72 79 20 74 6f 0a 2a 2a 20 77 72 69 74 ssary to.** writ
13d33 65 20 74 68 65 20 69 6e 66 6f 72 6d 61 74 69 6f e the informatio
13d34 6e 20 6f 6e 20 70 61 67 65 20 70 50 67 20 62 61 n on page pPg ba
13d35 63 6b 20 74 6f 20 74 68 65 20 64 69 73 6b 2c 20 ck to the disk,
13d36 65 76 65 6e 20 74 68 6f 75 67 68 0a 2a 2a 20 74 even though.** t
13d37 68 61 74 20 70 61 67 65 20 6d 69 67 68 74 20 62 hat page might b
13d38 65 20 6d 61 72 6b 65 64 20 61 73 20 64 69 72 74 e marked as dirt
13d39 79 2e 20 20 54 68 69 73 20 68 61 70 70 65 6e 73 y. This happens
13d3a 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 77 , for example, w
13d3b 68 65 6e 0a 2a 2a 20 74 68 65 20 70 61 67 65 20 hen.** the page
13d3c 68 61 73 20 62 65 65 6e 20 61 64 64 65 64 20 61 has been added a
13d3d 73 20 61 20 6c 65 61 66 20 6f 66 20 74 68 65 20 s a leaf of the
13d3e 66 72 65 65 6c 69 73 74 20 61 6e 64 20 73 6f 20 freelist and so
13d3f 69 74 73 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 6e its.** content n
13d40 6f 20 6c 6f 6e 67 65 72 20 6d 61 74 74 65 72 73 o longer matters
13d41 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 76 65 72 ..**.** The over
13d42 6c 79 69 6e 67 20 73 6f 66 74 77 61 72 65 20 6c lying software l
13d43 61 79 65 72 20 63 61 6c 6c 73 20 74 68 69 73 20 ayer calls this
13d44 72 6f 75 74 69 6e 65 20 77 68 65 6e 20 61 6c 6c routine when all
13d45 20 6f 66 20 74 68 65 20 64 61 74 61 0a 2a 2a 20 of the data.**
13d46 6f 6e 20 74 68 65 20 67 69 76 65 6e 20 70 61 67 on the given pag
13d47 65 20 69 73 20 75 6e 75 73 65 64 2e 20 54 68 65 e is unused. The
13d48 20 70 61 67 65 72 20 6d 61 72 6b 73 20 74 68 65 pager marks the
13d49 20 70 61 67 65 20 61 73 20 63 6c 65 61 6e 20 73 page as clean s
13d4a 6f 0a 2a 2a 20 74 68 61 74 20 69 74 20 64 6f 65 o.** that it doe
13d4b 73 20 6e 6f 74 20 67 65 74 20 77 72 69 74 74 65 s not get writte
13d4c 6e 20 74 6f 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a n to disk..**.**
13d4d 20 54 65 73 74 73 20 73 68 6f 77 20 74 68 61 74 Tests show that
13d4e 20 74 68 69 73 20 6f 70 74 69 6d 69 7a 61 74 69 this optimizati
13d4f 6f 6e 20 63 61 6e 20 71 75 61 64 72 75 70 6c 65 on can quadruple
13d50 20 74 68 65 20 73 70 65 65 64 20 6f 66 20 6c 61 the speed of la
13d51 72 67 65 20 0a 2a 2a 20 44 45 4c 45 54 45 20 6f rge .** DELETE o
13d52 70 65 72 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 perations..*/.SQ
13d53 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
13d54 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 44 6f d sqlite3PagerDo
13d55 6e 74 57 72 69 74 65 28 50 67 48 64 72 20 2a 70 ntWrite(PgHdr *p
13d56 50 67 29 7b 0a 20 20 50 61 67 65 72 20 2a 70 50 Pg){. Pager *pP
13d57 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 67 ager = pPg->pPag
13d58 65 72 3b 0a 20 20 69 66 28 20 28 70 50 67 2d 3e er;. if( (pPg->
13d59 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52 54 flags&PGHDR_DIRT
13d5a 59 29 20 26 26 20 70 50 61 67 65 72 2d 3e 6e 53 Y) && pPager->nS
13d5b 61 76 65 70 6f 69 6e 74 3d 3d 30 20 29 7b 0a 20 avepoint==0 ){.
13d5c 20 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22 PAGERTRACE(("
13d5d 44 4f 4e 54 5f 57 52 49 54 45 20 70 61 67 65 20 DONT_WRITE page
13d5e 25 64 20 6f 66 20 25 64 5c 6e 22 2c 20 70 50 67 %d of %d\n", pPg
13d5f 2d 3e 70 67 6e 6f 2c 20 50 41 47 45 52 49 44 28 ->pgno, PAGERID(
13d60 70 50 61 67 65 72 29 29 29 3b 0a 20 20 20 20 49 pPager)));. I
13d61 4f 54 52 41 43 45 28 28 22 43 4c 45 41 4e 20 25 OTRACE(("CLEAN %
13d62 70 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c p %d\n", pPager,
13d63 20 70 50 67 2d 3e 70 67 6e 6f 29 29 0a 20 20 20 pPg->pgno)).
13d64 20 70 50 67 2d 3e 66 6c 61 67 73 20 7c 3d 20 50 pPg->flags |= P
13d65 47 48 44 52 5f 44 4f 4e 54 5f 57 52 49 54 45 3b GHDR_DONT_WRITE;
13d66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43 .#ifdef SQLITE_C
13d67 48 45 43 4b 5f 50 41 47 45 53 0a 20 20 20 20 70 HECK_PAGES. p
13d68 50 67 2d 3e 70 61 67 65 48 61 73 68 20 3d 20 70 Pg->pageHash = p
13d69 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 70 50 ager_pagehash(pP
13d6a 67 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 7d g);.#endif. }.}
13d6b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ../*.** This rou
13d6c 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 74 tine is called t
13d6d 6f 20 69 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 o increment the
13d6e 76 61 6c 75 65 20 6f 66 20 74 68 65 20 64 61 74 value of the dat
13d6f 61 62 61 73 65 20 66 69 6c 65 20 0a 2a 2a 20 63 abase file .** c
13d70 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 2c 20 73 hange-counter, s
13d71 74 6f 72 65 64 20 61 73 20 61 20 34 2d 62 79 74 tored as a 4-byt
13d72 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74 e big-endian int
13d73 65 67 65 72 20 73 74 61 72 74 69 6e 67 20 61 74 eger starting at
13d74 20 0a 2a 2a 20 62 79 74 65 20 6f 66 66 73 65 74 .** byte offset
13d75 20 32 34 20 6f 66 20 74 68 65 20 70 61 67 65 72 24 of the pager
13d76 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 file..**.** If
13d77 74 68 65 20 69 73 44 69 72 65 63 74 4d 6f 64 65 the isDirectMode
13d78 20 66 6c 61 67 20 69 73 20 7a 65 72 6f 2c 20 74 flag is zero, t
13d79 68 65 6e 20 74 68 69 73 20 69 73 20 64 6f 6e 65 hen this is done
13d7a 20 62 79 20 63 61 6c 6c 69 6e 67 20 0a 2a 2a 20 by calling .**
13d7b 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 sqlite3PagerWrit
13d7c 65 28 29 20 6f 6e 20 70 61 67 65 20 31 2c 20 74 e() on page 1, t
13d7d 68 65 6e 20 6d 6f 64 69 66 79 69 6e 67 20 74 68 hen modifying th
13d7e 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 e contents of th
13d7f 65 0a 2a 2a 20 70 61 67 65 20 64 61 74 61 2e 20 e.** page data.
13d80 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65 In this case the
13d81 20 66 69 6c 65 20 77 69 6c 6c 20 62 65 20 75 70 file will be up
13d82 64 61 74 65 64 20 77 68 65 6e 20 74 68 65 20 63 dated when the c
13d83 75 72 72 65 6e 74 0a 2a 2a 20 74 72 61 6e 73 61 urrent.** transa
13d84 63 74 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 74 ction is committ
13d85 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 73 ed..**.** The is
13d86 44 69 72 65 63 74 4d 6f 64 65 20 66 6c 61 67 20 DirectMode flag
13d87 6d 61 79 20 6f 6e 6c 79 20 62 65 20 6e 6f 6e 2d may only be non-
13d88 7a 65 72 6f 20 69 66 20 74 68 65 20 6c 69 62 72 zero if the libr
13d89 61 72 79 20 77 61 73 20 63 6f 6d 70 69 6c 65 64 ary was compiled
13d8a 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 53 51 4c .** with the SQL
13d8b 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 ITE_ENABLE_ATOMI
13d8c 43 5f 57 52 49 54 45 20 6d 61 63 72 6f 20 64 65 C_WRITE macro de
13d8d 66 69 6e 65 64 2e 20 49 6e 20 74 68 69 73 20 63 fined. In this c
13d8e 61 73 65 2c 0a 2a 2a 20 69 66 20 69 73 44 69 72 ase,.** if isDir
13d8f 65 63 74 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c ect is non-zero,
13d90 20 74 68 65 6e 20 74 68 65 20 64 61 74 61 62 61 then the databa
13d91 73 65 20 66 69 6c 65 20 69 73 20 75 70 64 61 74 se file is updat
13d92 65 64 20 64 69 72 65 63 74 6c 79 0a 2a 2a 20 62 ed directly.** b
13d93 79 20 77 72 69 74 69 6e 67 20 61 6e 20 75 70 64 y writing an upd
13d94 61 74 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 ated version of
13d95 70 61 67 65 20 31 20 75 73 69 6e 67 20 61 20 63 page 1 using a c
13d96 61 6c 6c 20 74 6f 20 74 68 65 20 0a 2a 2a 20 73 all to the .** s
13d97 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 29 20 qlite3OsWrite()
13d98 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 function..*/.sta
13d99 74 69 63 20 69 6e 74 20 70 61 67 65 72 5f 69 6e tic int pager_in
13d9a 63 72 5f 63 68 61 6e 67 65 63 6f 75 6e 74 65 72 cr_changecounter
13d9b 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 (Pager *pPager,
13d9c 69 6e 74 20 69 73 44 69 72 65 63 74 4d 6f 64 65 int isDirectMode
13d9d 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 ){. int rc = SQ
13d9e 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20 44 LITE_OK;.. /* D
13d9f 65 63 6c 61 72 65 20 61 6e 64 20 69 6e 69 74 69 eclare and initi
13da0 61 6c 69 7a 65 20 63 6f 6e 73 74 61 6e 74 20 69 alize constant i
13da1 6e 74 65 67 65 72 20 27 69 73 44 69 72 65 63 74 nteger 'isDirect
13da2 27 2e 20 49 66 20 74 68 65 0a 20 20 2a 2a 20 61 '. If the. ** a
13da3 74 6f 6d 69 63 2d 77 72 69 74 65 20 6f 70 74 69 tomic-write opti
13da4 6d 69 7a 61 74 69 6f 6e 20 69 73 20 65 6e 61 62 mization is enab
13da5 6c 65 64 20 69 6e 20 74 68 69 73 20 62 75 69 6c led in this buil
13da6 64 2c 20 74 68 65 6e 20 69 73 44 69 72 65 63 74 d, then isDirect
13da7 0a 20 20 2a 2a 20 69 73 20 69 6e 69 74 69 61 6c . ** is initial
13da8 69 7a 65 64 20 74 6f 20 74 68 65 20 76 61 6c 75 ized to the valu
13da9 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 e passed as the
13daa 69 73 44 69 72 65 63 74 4d 6f 64 65 20 70 61 72 isDirectMode par
13dab 61 6d 65 74 65 72 0a 20 20 2a 2a 20 74 6f 20 74 ameter. ** to t
13dac 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20 4f 74 his function. Ot
13dad 68 65 72 77 69 73 65 2c 20 69 74 20 69 73 20 61 herwise, it is a
13dae 6c 77 61 79 73 20 73 65 74 20 74 6f 20 7a 65 72 lways set to zer
13daf 6f 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 o.. **. ** The
13db0 20 69 64 65 61 20 69 73 20 74 68 61 74 20 69 66 idea is that if
13db1 20 74 68 65 20 61 74 6f 6d 69 63 2d 77 72 69 74 the atomic-writ
13db2 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69 e optimization i
13db3 73 20 6e 6f 74 0a 20 20 2a 2a 20 65 6e 61 62 6c s not. ** enabl
13db4 65 64 20 61 74 20 63 6f 6d 70 69 6c 65 20 74 69 ed at compile ti
13db5 6d 65 2c 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 me, the compiler
13db6 20 63 61 6e 20 6f 6d 69 74 20 74 68 65 20 74 65 can omit the te
13db7 73 74 73 20 6f 66 0a 20 20 2a 2a 20 27 69 73 44 sts of. ** 'isD
13db8 69 72 65 63 74 27 20 62 65 6c 6f 77 2c 20 61 73 irect' below, as
13db9 20 77 65 6c 6c 20 61 73 20 74 68 65 20 62 6c 6f well as the blo
13dba 63 6b 20 65 6e 63 6c 6f 73 65 64 20 69 6e 20 74 ck enclosed in t
13dbb 68 65 0a 20 20 2a 2a 20 22 69 66 28 20 69 73 44 he. ** "if( isD
13dbc 69 72 65 63 74 20 29 22 20 63 6f 6e 64 69 74 69 irect )" conditi
13dbd 6f 6e 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 on.. */.#ifndef
13dbe 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 SQLITE_ENABLE_A
13dbf 54 4f 4d 49 43 5f 57 52 49 54 45 0a 23 20 64 65 TOMIC_WRITE.# de
13dc0 66 69 6e 65 20 44 49 52 45 43 54 5f 4d 4f 44 45 fine DIRECT_MODE
13dc1 20 30 0a 20 20 61 73 73 65 72 74 28 20 69 73 44 0. assert( isD
13dc2 69 72 65 63 74 4d 6f 64 65 3d 3d 30 20 29 3b 0a irectMode==0 );.
13dc3 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
13dc4 45 52 28 69 73 44 69 72 65 63 74 4d 6f 64 65 29 ER(isDirectMode)
13dc5 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 ;.#else.# define
13dc6 20 44 49 52 45 43 54 5f 4d 4f 44 45 20 69 73 44 DIRECT_MODE isD
13dc7 69 72 65 63 74 4d 6f 64 65 0a 23 65 6e 64 69 66 irectMode.#endif
13dc8 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 .. assert( pPag
13dc9 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 er->state>=PAGER
13dca 5f 52 45 53 45 52 56 45 44 20 29 3b 0a 20 20 69 _RESERVED );. i
13dcb 66 28 20 21 70 50 61 67 65 72 2d 3e 63 68 61 6e f( !pPager->chan
13dcc 67 65 43 6f 75 6e 74 44 6f 6e 65 20 26 26 20 41 geCountDone && A
13dcd 4c 57 41 59 53 28 70 50 61 67 65 72 2d 3e 64 62 LWAYS(pPager->db
13dce 53 69 7a 65 3e 30 29 20 29 7b 0a 20 20 20 20 50 Size>0) ){. P
13dcf 67 48 64 72 20 2a 70 50 67 48 64 72 3b 20 20 20 gHdr *pPgHdr;
13dd0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
13dd1 52 65 66 65 72 65 6e 63 65 20 74 6f 20 70 61 67 Reference to pag
13dd2 65 20 31 20 2a 2f 0a 20 20 20 20 75 33 32 20 63 e 1 */. u32 c
13dd3 68 61 6e 67 65 5f 63 6f 75 6e 74 65 72 3b 20 20 hange_counter;
13dd4 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 69 74 /* Init
13dd5 69 61 6c 20 76 61 6c 75 65 20 6f 66 20 63 68 61 ial value of cha
13dd6 6e 67 65 2d 63 6f 75 6e 74 65 72 20 66 69 65 6c nge-counter fiel
13dd7 64 20 2a 2f 0a 0a 20 20 20 20 61 73 73 65 72 74 d */.. assert
13dd8 28 20 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 ( !pPager->tempF
13dd9 69 6c 65 20 26 26 20 69 73 4f 70 65 6e 28 70 50 ile && isOpen(pP
13dda 61 67 65 72 2d 3e 66 64 29 20 29 3b 0a 0a 20 20 ager->fd) );..
13ddb 20 20 2f 2a 20 4f 70 65 6e 20 70 61 67 65 20 31 /* Open page 1
13ddc 20 6f 66 20 74 68 65 20 66 69 6c 65 20 66 6f 72 of the file for
13ddd 20 77 72 69 74 69 6e 67 2e 20 2a 2f 0a 20 20 20 writing. */.
13dde 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
13ddf 65 72 47 65 74 28 70 50 61 67 65 72 2c 20 31 2c erGet(pPager, 1,
13de0 20 26 70 50 67 48 64 72 29 3b 0a 20 20 20 20 61 &pPgHdr);. a
13de1 73 73 65 72 74 28 20 70 50 67 48 64 72 3d 3d 30 ssert( pPgHdr==0
13de2 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f || rc==SQLITE_O
13de3 4b 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 K );.. /* If
13de4 70 61 67 65 20 6f 6e 65 20 77 61 73 20 66 65 74 page one was fet
13de5 63 68 65 64 20 73 75 63 63 65 73 73 66 75 6c 6c ched successfull
13de6 79 2c 20 61 6e 64 20 74 68 69 73 20 66 75 6e 63 y, and this func
13de7 74 69 6f 6e 20 69 73 20 6e 6f 74 0a 20 20 20 20 tion is not.
13de8 2a 2a 20 6f 70 65 72 61 74 69 6e 67 20 69 6e 20 ** operating in
13de9 64 69 72 65 63 74 2d 6d 6f 64 65 2c 20 6d 61 6b direct-mode, mak
13dea 65 20 70 61 67 65 20 31 20 77 72 69 74 61 62 6c e page 1 writabl
13deb 65 2e 20 20 57 68 65 6e 20 6e 6f 74 20 69 6e 20 e. When not in
13dec 0a 20 20 20 20 2a 2a 20 64 69 72 65 63 74 20 6d . ** direct m
13ded 6f 64 65 2c 20 70 61 67 65 20 31 20 69 73 20 61 ode, page 1 is a
13dee 6c 77 61 79 73 20 68 65 6c 64 20 69 6e 20 63 61 lways held in ca
13def 63 68 65 20 61 6e 64 20 68 65 6e 63 65 20 74 68 che and hence th
13df0 65 20 50 61 67 65 72 47 65 74 28 29 0a 20 20 20 e PagerGet().
13df1 20 2a 2a 20 61 62 6f 76 65 20 69 73 20 61 6c 77 ** above is alw
13df2 61 79 73 20 73 75 63 63 65 73 73 66 75 6c 20 2d ays successful -
13df3 20 68 65 6e 63 65 20 74 68 65 20 41 4c 57 41 59 hence the ALWAY
13df4 53 20 6f 6e 20 72 63 3d 3d 53 51 4c 49 54 45 5f S on rc==SQLITE_
13df5 4f 4b 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 OK.. */. i
13df6 66 28 20 21 44 49 52 45 43 54 5f 4d 4f 44 45 20 f( !DIRECT_MODE
13df7 26 26 20 41 4c 57 41 59 53 28 72 63 3d 3d 53 51 && ALWAYS(rc==SQ
13df8 4c 49 54 45 5f 4f 4b 29 20 29 7b 0a 20 20 20 20 LITE_OK) ){.
13df9 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
13dfa 67 65 72 57 72 69 74 65 28 70 50 67 48 64 72 29 gerWrite(pPgHdr)
13dfb 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 ;. }.. if(
13dfc 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
13dfd 7b 0a 20 20 20 20 20 20 2f 2a 20 49 6e 63 72 65 {. /* Incre
13dfe 6d 65 6e 74 20 74 68 65 20 76 61 6c 75 65 20 6a ment the value j
13dff 75 73 74 20 72 65 61 64 20 61 6e 64 20 77 72 69 ust read and wri
13e00 74 65 20 69 74 20 62 61 63 6b 20 74 6f 20 62 79 te it back to by
13e01 74 65 20 32 34 2e 20 2a 2f 0a 20 20 20 20 20 20 te 24. */.
13e02 63 68 61 6e 67 65 5f 63 6f 75 6e 74 65 72 20 3d change_counter =
13e03 20 73 71 6c 69 74 65 33 47 65 74 34 62 79 74 65 sqlite3Get4byte
13e04 28 28 75 38 2a 29 70 50 61 67 65 72 2d 3e 64 62 ((u8*)pPager->db
13e05 46 69 6c 65 56 65 72 73 29 3b 0a 20 20 20 20 20 FileVers);.
13e06 20 63 68 61 6e 67 65 5f 63 6f 75 6e 74 65 72 2b change_counter+
13e07 2b 3b 0a 20 20 20 20 20 20 70 75 74 33 32 62 69 +;. put32bi
13e08 74 73 28 28 28 63 68 61 72 2a 29 70 50 67 48 64 ts(((char*)pPgHd
13e09 72 2d 3e 70 44 61 74 61 29 2b 32 34 2c 20 63 68 r->pData)+24, ch
13e0a 61 6e 67 65 5f 63 6f 75 6e 74 65 72 29 3b 0a 0a ange_counter);..
13e0b 20 20 20 20 20 20 2f 2a 20 49 66 20 72 75 6e 6e /* If runn
13e0c 69 6e 67 20 69 6e 20 64 69 72 65 63 74 20 6d 6f ing in direct mo
13e0d 64 65 2c 20 77 72 69 74 65 20 74 68 65 20 63 6f de, write the co
13e0e 6e 74 65 6e 74 73 20 6f 66 20 70 61 67 65 20 31 ntents of page 1
13e0f 20 74 6f 20 74 68 65 20 66 69 6c 65 2e 20 2a 2f to the file. */
13e10 0a 20 20 20 20 20 20 69 66 28 20 44 49 52 45 43 . if( DIREC
13e11 54 5f 4d 4f 44 45 20 29 7b 0a 20 20 20 20 20 20 T_MODE ){.
13e12 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 42 const void *zB
13e13 75 66 20 3d 20 70 50 67 48 64 72 2d 3e 70 44 61 uf = pPgHdr->pDa
13e14 74 61 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 ta;. asse
13e15 72 74 28 20 70 50 61 67 65 72 2d 3e 64 62 46 69 rt( pPager->dbFi
13e16 6c 65 53 69 7a 65 3e 30 20 29 3b 0a 20 20 20 20 leSize>0 );.
13e17 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
13e18 4f 73 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e OsWrite(pPager->
13e19 66 64 2c 20 7a 42 75 66 2c 20 70 50 61 67 65 72 fd, zBuf, pPager
13e1a 2d 3e 70 61 67 65 53 69 7a 65 2c 20 30 29 3b 0a ->pageSize, 0);.
13e1b 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d if( rc==
13e1c 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
13e1d 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 63 pPager->c
13e1e 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 3d hangeCountDone =
13e1f 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 1;. }.
13e20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
13e21 20 20 20 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 pPager->chang
13e22 65 43 6f 75 6e 74 44 6f 6e 65 20 3d 20 31 3b 0a eCountDone = 1;.
13e23 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 }. }..
13e24 20 20 20 2f 2a 20 52 65 6c 65 61 73 65 20 74 68 /* Release th
13e25 65 20 70 61 67 65 20 72 65 66 65 72 65 6e 63 65 e page reference
13e26 2e 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 . */. sqlite3
13e27 50 61 67 65 72 55 6e 72 65 66 28 70 50 67 48 64 PagerUnref(pPgHd
13e28 72 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e r);. }. return
13e29 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 79 rc;.}../*.** Sy
13e2a 6e 63 20 74 68 65 20 70 61 67 65 72 20 66 69 6c nc the pager fil
13e2b 65 20 74 6f 20 64 69 73 6b 2e 20 54 68 69 73 20 e to disk. This
13e2c 69 73 20 61 20 6e 6f 2d 6f 70 20 66 6f 72 20 69 is a no-op for i
13e2d 6e 2d 6d 65 6d 6f 72 79 20 66 69 6c 65 73 0a 2a n-memory files.*
13e2e 2a 20 6f 72 20 70 61 67 65 73 20 77 69 74 68 20 * or pages with
13e2f 74 68 65 20 50 61 67 65 72 2e 6e 6f 53 79 6e 63 the Pager.noSync
13e30 20 66 6c 61 67 20 73 65 74 2e 0a 2a 2a 0a 2a 2a flag set..**.**
13e31 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 If successful,
13e32 6f 72 20 63 61 6c 6c 65 64 20 6f 6e 20 61 20 70 or called on a p
13e33 61 67 65 72 20 66 6f 72 20 77 68 69 63 68 20 69 ager for which i
13e34 74 20 69 73 20 61 20 6e 6f 2d 6f 70 2c 20 74 68 t is a no-op, th
13e35 69 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 72 is.** function r
13e36 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b eturns SQLITE_OK
13e37 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 61 6e 20 . Otherwise, an
13e38 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 IO error code is
13e39 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 returned..*/.SQ
13e3a 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
13e3b 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 79 6e sqlite3PagerSyn
13e3c 63 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 c(Pager *pPager)
13e3d 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 {. int rc;
13e3e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13e3f 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 /* Retu
13e40 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 61 73 73 rn code */. ass
13e41 65 72 74 28 20 21 4d 45 4d 44 42 20 29 3b 0a 20 ert( !MEMDB );.
13e42 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6e 6f 53 if( pPager->noS
13e43 79 6e 63 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 ync ){. rc =
13e44 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c SQLITE_OK;. }el
13e45 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c se{. rc = sql
13e46 69 74 65 33 4f 73 53 79 6e 63 28 70 50 61 67 65 ite3OsSync(pPage
13e47 72 2d 3e 66 64 2c 20 70 50 61 67 65 72 2d 3e 73 r->fd, pPager->s
13e48 79 6e 63 5f 66 6c 61 67 73 29 3b 0a 20 20 7d 0a ync_flags);. }.
13e49 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
13e4a 2f 2a 0a 2a 2a 20 53 79 6e 63 20 74 68 65 20 64 /*.** Sync the d
13e4b 61 74 61 62 61 73 65 20 66 69 6c 65 20 66 6f 72 atabase file for
13e4c 20 74 68 65 20 70 61 67 65 72 20 70 50 61 67 65 the pager pPage
13e4d 72 2e 20 7a 4d 61 73 74 65 72 20 70 6f 69 6e 74 r. zMaster point
13e4e 73 20 74 6f 20 74 68 65 20 6e 61 6d 65 0a 2a 2a s to the name.**
13e4f 20 6f 66 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 of a master jou
13e50 72 6e 61 6c 20 66 69 6c 65 20 74 68 61 74 20 73 rnal file that s
13e51 68 6f 75 6c 64 20 62 65 20 77 72 69 74 74 65 6e hould be written
13e52 20 69 6e 74 6f 20 74 68 65 20 69 6e 64 69 76 69 into the indivi
13e53 64 75 61 6c 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 dual.** journal
13e54 66 69 6c 65 2e 20 7a 4d 61 73 74 65 72 20 6d 61 file. zMaster ma
13e55 79 20 62 65 20 4e 55 4c 4c 2c 20 77 68 69 63 68 y be NULL, which
13e56 20 69 73 20 69 6e 74 65 72 70 72 65 74 65 64 20 is interpreted
13e57 61 73 20 6e 6f 20 6d 61 73 74 65 72 0a 2a 2a 20 as no master.**
13e58 6a 6f 75 72 6e 61 6c 20 28 61 20 73 69 6e 67 6c journal (a singl
13e59 65 20 64 61 74 61 62 61 73 65 20 74 72 61 6e 73 e database trans
13e5a 61 63 74 69 6f 6e 29 2e 0a 2a 2a 0a 2a 2a 20 54 action)..**.** T
13e5b 68 69 73 20 72 6f 75 74 69 6e 65 20 65 6e 73 75 his routine ensu
13e5c 72 65 73 20 74 68 61 74 3a 0a 2a 2a 0a 2a 2a 20 res that:.**.**
13e5d 20 20 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 * The database
13e5e 20 66 69 6c 65 20 63 68 61 6e 67 65 2d 63 6f 75 file change-cou
13e5f 6e 74 65 72 20 69 73 20 75 70 64 61 74 65 64 2c nter is updated,
13e60 0a 2a 2a 20 20 20 2a 20 74 68 65 20 6a 6f 75 72 .** * the jour
13e61 6e 61 6c 20 69 73 20 73 79 6e 63 65 64 20 28 75 nal is synced (u
13e62 6e 6c 65 73 73 20 74 68 65 20 61 74 6f 6d 69 63 nless the atomic
13e63 2d 77 72 69 74 65 20 6f 70 74 69 6d 69 7a 61 74 -write optimizat
13e64 69 6f 6e 20 69 73 20 75 73 65 64 29 2c 0a 2a 2a ion is used),.**
13e65 20 20 20 2a 20 61 6c 6c 20 64 69 72 74 79 20 70 * all dirty p
13e66 61 67 65 73 20 61 72 65 20 77 72 69 74 74 65 6e ages are written
13e67 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 to the database
13e68 20 66 69 6c 65 2c 20 0a 2a 2a 20 20 20 2a 20 74 file, .** * t
13e69 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
13e6a 20 69 73 20 74 72 75 6e 63 61 74 65 64 20 28 69 is truncated (i
13e6b 66 20 72 65 71 75 69 72 65 64 29 2c 20 61 6e 64 f required), and
13e6c 0a 2a 2a 20 20 20 2a 20 74 68 65 20 64 61 74 61 .** * the data
13e6d 62 61 73 65 20 66 69 6c 65 20 73 79 6e 63 65 64 base file synced
13e6e 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 6e 6c . .**.** The onl
13e6f 79 20 74 68 69 6e 67 20 74 68 61 74 20 72 65 6d y thing that rem
13e70 61 69 6e 73 20 74 6f 20 63 6f 6d 6d 69 74 20 74 ains to commit t
13e71 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 he transaction i
13e72 73 20 74 6f 20 66 69 6e 61 6c 69 7a 65 20 0a 2a s to finalize .*
13e73 2a 20 28 64 65 6c 65 74 65 2c 20 74 72 75 6e 63 * (delete, trunc
13e74 61 74 65 20 6f 72 20 7a 65 72 6f 20 74 68 65 20 ate or zero the
13e75 66 69 72 73 74 20 70 61 72 74 20 6f 66 29 20 74 first part of) t
13e76 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
13e77 28 6f 72 20 0a 2a 2a 20 64 65 6c 65 74 65 20 74 (or .** delete t
13e78 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 he master journa
13e79 6c 20 66 69 6c 65 20 69 66 20 73 70 65 63 69 66 l file if specif
13e7a 69 65 64 29 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 ied)..**.** Note
13e7b 20 74 68 61 74 20 69 66 20 7a 4d 61 73 74 65 72 that if zMaster
13e7c 3d 3d 4e 55 4c 4c 2c 20 74 68 69 73 20 64 6f 65 ==NULL, this doe
13e7d 73 20 6e 6f 74 20 6f 76 65 72 77 72 69 74 65 20 s not overwrite
13e7e 61 20 70 72 65 76 69 6f 75 73 20 76 61 6c 75 65 a previous value
13e7f 0a 2a 2a 20 70 61 73 73 65 64 20 74 6f 20 61 6e .** passed to an
13e80 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f 6d sqlite3PagerCom
13e81 6d 69 74 50 68 61 73 65 4f 6e 65 28 29 20 63 61 mitPhaseOne() ca
13e82 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 ll..**.** If the
13e83 20 66 69 6e 61 6c 20 70 61 72 61 6d 65 74 65 72 final parameter
13e84 20 2d 20 6e 6f 53 79 6e 63 20 2d 20 69 73 20 74 - noSync - is t
13e85 72 75 65 2c 20 74 68 65 6e 20 74 68 65 20 64 61 rue, then the da
13e86 74 61 62 61 73 65 20 66 69 6c 65 20 69 74 73 65 tabase file itse
13e87 6c 66 0a 2a 2a 20 69 73 20 6e 6f 74 20 73 79 6e lf.** is not syn
13e88 63 65 64 2e 20 54 68 65 20 63 61 6c 6c 65 72 20 ced. The caller
13e89 6d 75 73 74 20 63 61 6c 6c 20 73 71 6c 69 74 65 must call sqlite
13e8a 33 50 61 67 65 72 53 79 6e 63 28 29 20 64 69 72 3PagerSync() dir
13e8b 65 63 74 6c 79 20 74 6f 0a 2a 2a 20 73 79 6e 63 ectly to.** sync
13e8c 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
13e8d 6c 65 20 62 65 66 6f 72 65 20 63 61 6c 6c 69 6e le before callin
13e8e 67 20 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f g CommitPhaseTwo
13e8f 28 29 20 74 6f 20 64 65 6c 65 74 65 20 74 68 65 () to delete the
13e90 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 .** journal file
13e91 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a 2a in this case..*
13e92 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
13e93 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 int sqlite3Page
13e94 72 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 rCommitPhaseOne(
13e95 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 . Pager *pPager
13e96 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
13e97 20 20 20 2f 2a 20 50 61 67 65 72 20 6f 62 6a 65 /* Pager obje
13e98 63 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 ct */. const ch
13e99 61 72 20 2a 7a 4d 61 73 74 65 72 2c 20 20 20 20 ar *zMaster,
13e9a 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 6e 6f /* If no
13e9b 74 20 4e 55 4c 4c 2c 20 74 68 65 20 6d 61 73 74 t NULL, the mast
13e9c 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 er journal name
13e9d 2a 2f 0a 20 20 69 6e 74 20 6e 6f 53 79 6e 63 20 */. int noSync
13e9e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13e9f 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 /* True to
13ea0 6f 6d 69 74 20 74 68 65 20 78 53 79 6e 63 20 6f omit the xSync o
13ea1 6e 20 74 68 65 20 64 62 20 66 69 6c 65 20 2a 2f n the db file */
13ea2 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 .){. int rc = S
13ea3 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 QLITE_OK;
13ea4 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 /* Return
13ea5 63 6f 64 65 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 code */.. /* Th
13ea6 65 20 64 62 4f 72 69 67 53 69 7a 65 20 69 73 20 e dbOrigSize is
13ea7 6e 65 76 65 72 20 73 65 74 20 69 66 20 6a 6f 75 never set if jou
13ea8 72 6e 61 6c 5f 6d 6f 64 65 3d 4f 46 46 20 2a 2f rnal_mode=OFF */
13ea9 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
13eaa 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 21 3d r->journalMode!=
13eab 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 PAGER_JOURNALMOD
13eac 45 5f 4f 46 46 20 7c 7c 20 70 50 61 67 65 72 2d E_OFF || pPager-
13ead 3e 64 62 4f 72 69 67 53 69 7a 65 3d 3d 30 20 29 >dbOrigSize==0 )
13eae 3b 0a 0a 20 20 2f 2a 20 49 66 20 61 20 70 72 69 ;.. /* If a pri
13eaf 6f 72 20 65 72 72 6f 72 20 6f 63 63 75 72 72 65 or error occurre
13eb0 64 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 d, this routine
13eb1 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63 61 should not be ca
13eb2 6c 6c 65 64 2e 20 20 52 4f 4c 4c 42 41 43 4b 0a lled. ROLLBACK.
13eb3 20 20 2a 2a 20 69 73 20 74 68 65 20 61 70 70 72 ** is the appr
13eb4 6f 70 72 69 61 74 65 20 72 65 73 70 6f 6e 73 65 opriate response
13eb5 20 74 6f 20 61 6e 20 65 72 72 6f 72 2c 20 6e 6f to an error, no
13eb6 74 20 43 4f 4d 4d 49 54 2e 20 20 47 75 61 72 64 t COMMIT. Guard
13eb7 20 61 67 61 69 6e 73 74 0a 20 20 2a 2a 20 63 6f against. ** co
13eb8 64 69 6e 67 20 65 72 72 6f 72 73 20 62 79 20 72 ding errors by r
13eb9 65 70 65 61 74 69 6e 67 20 74 68 65 20 70 72 69 epeating the pri
13eba 6f 72 20 65 72 72 6f 72 2e 20 2a 2f 0a 20 20 69 or error. */. i
13ebb 66 28 20 4e 45 56 45 52 28 70 50 61 67 65 72 2d f( NEVER(pPager-
13ebc 3e 65 72 72 43 6f 64 65 29 20 29 20 72 65 74 75 >errCode) ) retu
13ebd 72 6e 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f rn pPager->errCo
13ebe 64 65 3b 0a 0a 20 20 50 41 47 45 52 54 52 41 43 de;.. PAGERTRAC
13ebf 45 28 28 22 44 41 54 41 42 41 53 45 20 53 59 4e E(("DATABASE SYN
13ec0 43 3a 20 46 69 6c 65 3d 25 73 20 7a 4d 61 73 74 C: File=%s zMast
13ec1 65 72 3d 25 73 20 6e 53 69 7a 65 3d 25 64 5c 6e er=%s nSize=%d\n
13ec2 22 2c 20 0a 20 20 20 20 20 20 70 50 61 67 65 72 ", . pPager
13ec3 2d 3e 7a 46 69 6c 65 6e 61 6d 65 2c 20 7a 4d 61 ->zFilename, zMa
13ec4 73 74 65 72 2c 20 70 50 61 67 65 72 2d 3e 64 62 ster, pPager->db
13ec5 53 69 7a 65 29 29 3b 0a 0a 20 20 69 66 28 20 4d Size));.. if( M
13ec6 45 4d 44 42 20 26 26 20 70 50 61 67 65 72 2d 3e EMDB && pPager->
13ec7 64 62 4d 6f 64 69 66 69 65 64 20 29 7b 0a 20 20 dbModified ){.
13ec8 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 /* If this is
13ec9 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 62 2c an in-memory db,
13eca 20 6f 72 20 6e 6f 20 70 61 67 65 73 20 68 61 76 or no pages hav
13ecb 65 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20 74 e been written t
13ecc 6f 2c 20 6f 72 20 74 68 69 73 0a 20 20 20 20 2a o, or this. *
13ecd 2a 20 66 75 6e 63 74 69 6f 6e 20 68 61 73 20 61 * function has a
13ece 6c 72 65 61 64 79 20 62 65 65 6e 20 63 61 6c 6c lready been call
13ecf 65 64 2c 20 69 74 20 69 73 20 6d 6f 73 74 6c 79 ed, it is mostly
13ed0 20 61 20 6e 6f 2d 6f 70 2e 20 20 48 6f 77 65 76 a no-op. Howev
13ed1 65 72 2c 20 61 6e 79 0a 20 20 20 20 2a 2a 20 62 er, any. ** b
13ed2 61 63 6b 75 70 20 69 6e 20 70 72 6f 67 72 65 73 ackup in progres
13ed3 73 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 65 s needs to be re
13ed4 73 74 61 72 74 65 64 2e 0a 20 20 20 20 2a 2f 0a started.. */.
13ed5 20 20 20 20 73 71 6c 69 74 65 33 42 61 63 6b 75 sqlite3Backu
13ed6 70 52 65 73 74 61 72 74 28 70 50 61 67 65 72 2d pRestart(pPager-
13ed7 3e 70 42 61 63 6b 75 70 29 3b 0a 20 20 7d 65 6c >pBackup);. }el
13ed8 73 65 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 se if( pPager->s
13ed9 74 61 74 65 21 3d 50 41 47 45 52 5f 53 59 4e 43 tate!=PAGER_SYNC
13eda 45 44 20 26 26 20 70 50 61 67 65 72 2d 3e 64 62 ED && pPager->db
13edb 4d 6f 64 69 66 69 65 64 20 29 7b 0a 0a 20 20 20 Modified ){..
13edc 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e /* The followin
13edd 67 20 62 6c 6f 63 6b 20 75 70 64 61 74 65 73 20 g block updates
13ede 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 the change-count
13edf 65 72 2e 20 45 78 61 63 74 6c 79 20 68 6f 77 20 er. Exactly how
13ee0 69 74 0a 20 20 20 20 2a 2a 20 64 6f 65 73 20 74 it. ** does t
13ee1 68 69 73 20 64 65 70 65 6e 64 73 20 6f 6e 20 77 his depends on w
13ee2 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 hether or not th
13ee3 65 20 61 74 6f 6d 69 63 2d 75 70 64 61 74 65 20 e atomic-update
13ee4 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 0a 20 20 20 optimization.
13ee5 20 2a 2a 20 77 61 73 20 65 6e 61 62 6c 65 64 20 ** was enabled
13ee6 61 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 2c at compile time,
13ee7 20 61 6e 64 20 69 66 20 74 68 69 73 20 74 72 61 and if this tra
13ee8 6e 73 61 63 74 69 6f 6e 20 6d 65 65 74 73 20 74 nsaction meets t
13ee9 68 65 20 0a 20 20 20 20 2a 2a 20 72 75 6e 74 69 he . ** runti
13eea 6d 65 20 63 72 69 74 65 72 69 61 20 74 6f 20 75 me criteria to u
13eeb 73 65 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e se the operation
13eec 3a 20 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a : . **. **
13eed 20 20 20 20 2a 20 54 68 65 20 66 69 6c 65 2d 73 * The file-s
13eee 79 73 74 65 6d 20 73 75 70 70 6f 72 74 73 20 74 ystem supports t
13eef 68 65 20 61 74 6f 6d 69 63 2d 77 72 69 74 65 20 he atomic-write
13ef0 70 72 6f 70 65 72 74 79 20 66 6f 72 0a 20 20 20 property for.
13ef1 20 2a 2a 20 20 20 20 20 20 62 6c 6f 63 6b 73 20 ** blocks
13ef2 6f 66 20 73 69 7a 65 20 70 61 67 65 2d 73 69 7a of size page-siz
13ef3 65 2c 20 61 6e 64 20 0a 20 20 20 20 2a 2a 20 20 e, and . **
13ef4 20 20 2a 20 54 68 69 73 20 63 6f 6d 6d 69 74 20 * This commit
13ef5 69 73 20 6e 6f 74 20 70 61 72 74 20 6f 66 20 61 is not part of a
13ef6 20 6d 75 6c 74 69 2d 66 69 6c 65 20 74 72 61 6e multi-file tran
13ef7 73 61 63 74 69 6f 6e 2c 20 61 6e 64 0a 20 20 20 saction, and.
13ef8 20 2a 2a 20 20 20 20 2a 20 45 78 61 63 74 6c 79 ** * Exactly
13ef9 20 6f 6e 65 20 70 61 67 65 20 68 61 73 20 62 65 one page has be
13efa 65 6e 20 6d 6f 64 69 66 69 65 64 20 61 6e 64 20 en modified and
13efb 73 74 6f 72 65 20 69 6e 20 74 68 65 20 6a 6f 75 store in the jou
13efc 72 6e 61 6c 20 66 69 6c 65 2e 0a 20 20 20 20 2a rnal file.. *
13efd 2a 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20 *. ** If the
13efe 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 77 61 73 optimization was
13eff 20 6e 6f 74 20 65 6e 61 62 6c 65 64 20 61 74 20 not enabled at
13f00 63 6f 6d 70 69 6c 65 20 74 69 6d 65 2c 20 74 68 compile time, th
13f01 65 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 70 61 en the. ** pa
13f02 67 65 72 5f 69 6e 63 72 5f 63 68 61 6e 67 65 63 ger_incr_changec
13f03 6f 75 6e 74 65 72 28 29 20 66 75 6e 63 74 69 6f ounter() functio
13f04 6e 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 75 n is called to u
13f05 70 64 61 74 65 20 74 68 65 20 63 68 61 6e 67 65 pdate the change
13f06 0a 20 20 20 20 2a 2a 20 63 6f 75 6e 74 65 72 20 . ** counter
13f07 69 6e 20 27 69 6e 64 69 72 65 63 74 2d 6d 6f 64 in 'indirect-mod
13f08 65 27 2e 20 49 66 20 74 68 65 20 6f 70 74 69 6d e'. If the optim
13f09 69 7a 61 74 69 6f 6e 20 69 73 20 63 6f 6d 70 69 ization is compi
13f0a 6c 65 64 20 69 6e 20 62 75 74 0a 20 20 20 20 2a led in but. *
13f0b 2a 20 69 73 20 6e 6f 74 20 61 70 70 6c 69 63 61 * is not applica
13f0c 62 6c 65 20 74 6f 20 74 68 69 73 20 74 72 61 6e ble to this tran
13f0d 73 61 63 74 69 6f 6e 2c 20 63 61 6c 6c 20 73 71 saction, call sq
13f0e 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 43 72 65 61 lite3JournalCrea
13f0f 74 65 28 29 0a 20 20 20 20 2a 2a 20 74 6f 20 6d te(). ** to m
13f10 61 6b 65 20 73 75 72 65 20 74 68 65 20 6a 6f 75 ake sure the jou
13f11 72 6e 61 6c 20 66 69 6c 65 20 68 61 73 20 61 63 rnal file has ac
13f12 74 75 61 6c 6c 79 20 62 65 65 6e 20 63 72 65 61 tually been crea
13f13 74 65 64 2c 20 74 68 65 6e 20 63 61 6c 6c 0a 20 ted, then call.
13f14 20 20 20 2a 2a 20 70 61 67 65 72 5f 69 6e 63 72 ** pager_incr
13f15 5f 63 68 61 6e 67 65 63 6f 75 6e 74 65 72 28 29 _changecounter()
13f16 20 74 6f 20 75 70 64 61 74 65 20 74 68 65 20 63 to update the c
13f17 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20 69 6e hange-counter in
13f18 20 69 6e 64 69 72 65 63 74 0a 20 20 20 20 2a 2a indirect. **
13f19 20 6d 6f 64 65 2e 20 0a 20 20 20 20 2a 2a 0a 20 mode. . **.
13f1a 20 20 20 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c ** Otherwise,
13f1b 20 69 66 20 74 68 65 20 6f 70 74 69 6d 69 7a 61 if the optimiza
13f1c 74 69 6f 6e 20 69 73 20 62 6f 74 68 20 65 6e 61 tion is both ena
13f1d 62 6c 65 64 20 61 6e 64 20 61 70 70 6c 69 63 61 bled and applica
13f1e 62 6c 65 2c 0a 20 20 20 20 2a 2a 20 74 68 65 6e ble,. ** then
13f1f 20 63 61 6c 6c 20 70 61 67 65 72 5f 69 6e 63 72 call pager_incr
13f20 5f 63 68 61 6e 67 65 63 6f 75 6e 74 65 72 28 29 _changecounter()
13f21 20 74 6f 20 75 70 64 61 74 65 20 74 68 65 20 63 to update the c
13f22 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 0a 20 20 hange-counter.
13f23 20 20 2a 2a 20 69 6e 20 27 64 69 72 65 63 74 27 ** in 'direct'
13f24 20 6d 6f 64 65 2e 20 49 6e 20 74 68 69 73 20 63 mode. In this c
13f25 61 73 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 ase the journal
13f26 66 69 6c 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 file will never
13f27 62 65 0a 20 20 20 20 2a 2a 20 63 72 65 61 74 65 be. ** create
13f28 64 20 66 6f 72 20 74 68 69 73 20 74 72 61 6e 73 d for this trans
13f29 61 63 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 23 action.. */.#
13f2a 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 ifdef SQLITE_ENA
13f2b 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45 BLE_ATOMIC_WRITE
13f2c 0a 20 20 20 20 50 67 48 64 72 20 2a 70 50 67 3b . PgHdr *pPg;
13f2d 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 73 4f . assert( isO
13f2e 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 pen(pPager->jfd)
13f2f 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 || pPager->jour
13f30 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a nalMode==PAGER_J
13f31 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 29 OURNALMODE_OFF )
13f32 3b 0a 20 20 20 20 69 66 28 20 21 7a 4d 61 73 74 ;. if( !zMast
13f33 65 72 20 26 26 20 69 73 4f 70 65 6e 28 70 50 61 er && isOpen(pPa
13f34 67 65 72 2d 3e 6a 66 64 29 20 0a 20 20 20 20 20 ger->jfd) .
13f35 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e && pPager->journ
13f36 61 6c 4f 66 66 3d 3d 6a 72 6e 6c 42 75 66 66 65 alOff==jrnlBuffe
13f37 72 53 69 7a 65 28 70 50 61 67 65 72 29 20 0a 20 rSize(pPager) .
13f38 20 20 20 20 26 26 20 70 50 61 67 65 72 2d 3e 64 && pPager->d
13f39 62 53 69 7a 65 3e 3d 70 50 61 67 65 72 2d 3e 64 bSize>=pPager->d
13f3a 62 46 69 6c 65 53 69 7a 65 0a 20 20 20 20 20 26 bFileSize. &
13f3b 26 20 28 30 3d 3d 28 70 50 67 20 3d 20 73 71 6c & (0==(pPg = sql
13f3c 69 74 65 33 50 63 61 63 68 65 44 69 72 74 79 4c ite3PcacheDirtyL
13f3d 69 73 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61 ist(pPager->pPCa
13f3e 63 68 65 29 29 20 7c 7c 20 30 3d 3d 70 50 67 2d che)) || 0==pPg-
13f3f 3e 70 44 69 72 74 79 29 0a 20 20 20 20 29 7b 0a >pDirty). ){.
13f40 20 20 20 20 20 20 2f 2a 20 55 70 64 61 74 65 20 /* Update
13f41 74 68 65 20 64 62 20 66 69 6c 65 20 63 68 61 6e the db file chan
13f42 67 65 20 63 6f 75 6e 74 65 72 20 76 69 61 20 74 ge counter via t
13f43 68 65 20 64 69 72 65 63 74 2d 77 72 69 74 65 20 he direct-write
13f44 6d 65 74 68 6f 64 2e 20 54 68 65 20 0a 20 20 20 method. The .
13f45 20 20 20 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 ** following
13f46 63 61 6c 6c 20 77 69 6c 6c 20 6d 6f 64 69 66 79 call will modify
13f47 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 the in-memory r
13f48 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 epresentation of
13f49 20 70 61 67 65 20 31 20 0a 20 20 20 20 20 20 2a page 1 . *
13f4a 2a 20 74 6f 20 69 6e 63 6c 75 64 65 20 74 68 65 * to include the
13f4b 20 75 70 64 61 74 65 64 20 63 68 61 6e 67 65 20 updated change
13f4c 63 6f 75 6e 74 65 72 20 61 6e 64 20 74 68 65 6e counter and then
13f4d 20 77 72 69 74 65 20 70 61 67 65 20 31 20 0a 20 write page 1 .
13f4e 20 20 20 20 20 2a 2a 20 64 69 72 65 63 74 6c 79 ** directly
13f4f 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 to the database
13f50 20 66 69 6c 65 2e 20 42 65 63 61 75 73 65 20 6f file. Because o
13f51 66 20 74 68 65 20 61 74 6f 6d 69 63 2d 77 72 69 f the atomic-wri
13f52 74 65 20 0a 20 20 20 20 20 20 2a 2a 20 70 72 6f te . ** pro
13f53 70 65 72 74 79 20 6f 66 20 74 68 65 20 68 6f 73 perty of the hos
13f54 74 20 66 69 6c 65 2d 73 79 73 74 65 6d 2c 20 74 t file-system, t
13f55 68 69 73 20 69 73 20 73 61 66 65 2e 0a 20 20 20 his is safe..
13f56 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d */. rc =
13f57 20 70 61 67 65 72 5f 69 6e 63 72 5f 63 68 61 6e pager_incr_chan
13f58 67 65 63 6f 75 6e 74 65 72 28 70 50 61 67 65 72 gecounter(pPager
13f59 2c 20 31 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b , 1);. }else{
13f5a 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
13f5b 74 65 33 4a 6f 75 72 6e 61 6c 43 72 65 61 74 65 te3JournalCreate
13f5c 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 (pPager->jfd);.
13f5d 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c if( rc==SQL
13f5e 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
13f5f 20 20 72 63 20 3d 20 70 61 67 65 72 5f 69 6e 63 rc = pager_inc
13f60 72 5f 63 68 61 6e 67 65 63 6f 75 6e 74 65 72 28 r_changecounter(
13f61 70 50 61 67 65 72 2c 20 30 29 3b 0a 20 20 20 20 pPager, 0);.
13f62 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6c 73 65 0a }. }.#else.
13f63 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 69 rc = pager_i
13f64 6e 63 72 5f 63 68 61 6e 67 65 63 6f 75 6e 74 65 ncr_changecounte
13f65 72 28 70 50 61 67 65 72 2c 20 30 29 3b 0a 23 65 r(pPager, 0);.#e
13f66 6e 64 69 66 0a 20 20 20 20 69 66 28 20 72 63 21 ndif. if( rc!
13f67 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 =SQLITE_OK ) got
13f68 6f 20 63 6f 6d 6d 69 74 5f 70 68 61 73 65 5f 6f o commit_phase_o
13f69 6e 65 5f 65 78 69 74 3b 0a 0a 20 20 20 20 2f 2a ne_exit;.. /*
13f6a 20 49 66 20 74 68 69 73 20 74 72 61 6e 73 61 63 If this transac
13f6b 74 69 6f 6e 20 68 61 73 20 6d 61 64 65 20 74 68 tion has made th
13f6c 65 20 64 61 74 61 62 61 73 65 20 73 6d 61 6c 6c e database small
13f6d 65 72 2c 20 74 68 65 6e 20 61 6c 6c 20 70 61 67 er, then all pag
13f6e 65 73 0a 20 20 20 20 2a 2a 20 62 65 69 6e 67 20 es. ** being
13f6f 64 69 73 63 61 72 64 65 64 20 62 79 20 74 68 65 discarded by the
13f70 20 74 72 75 6e 63 61 74 69 6f 6e 20 6d 75 73 74 truncation must
13f71 20 62 65 20 77 72 69 74 74 65 6e 20 74 6f 20 74 be written to t
13f72 68 65 20 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 2a he journal. *
13f73 2a 20 66 69 6c 65 2e 20 54 68 69 73 20 63 61 6e * file. This can
13f74 20 6f 6e 6c 79 20 68 61 70 70 65 6e 20 69 6e 20 only happen in
13f75 61 75 74 6f 2d 76 61 63 75 75 6d 20 6d 6f 64 65 auto-vacuum mode
13f76 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 .. **. **
13f77 42 65 66 6f 72 65 20 72 65 61 64 69 6e 67 20 74 Before reading t
13f78 68 65 20 70 61 67 65 73 20 77 69 74 68 20 70 61 he pages with pa
13f79 67 65 20 6e 75 6d 62 65 72 73 20 6c 61 72 67 65 ge numbers large
13f7a 72 20 74 68 61 6e 20 74 68 65 20 0a 20 20 20 20 r than the .
13f7b 2a 2a 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 ** current value
13f7c 20 6f 66 20 50 61 67 65 72 2e 64 62 53 69 7a 65 of Pager.dbSize
13f7d 2c 20 73 65 74 20 64 62 53 69 7a 65 20 62 61 63 , set dbSize bac
13f7e 6b 20 74 6f 20 74 68 65 20 76 61 6c 75 65 0a 20 k to the value.
13f7f 20 20 20 2a 2a 20 74 68 61 74 20 69 74 20 74 6f ** that it to
13f80 6f 6b 20 61 74 20 74 68 65 20 73 74 61 72 74 20 ok at the start
13f81 6f 66 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 of the transacti
13f82 6f 6e 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 74 on. Otherwise, t
13f83 68 65 0a 20 20 20 20 2a 2a 20 63 61 6c 6c 73 20 he. ** calls
13f84 74 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 to sqlite3PagerG
13f85 65 74 28 29 20 72 65 74 75 72 6e 20 7a 65 72 6f et() return zero
13f86 65 64 20 70 61 67 65 73 20 69 6e 73 74 65 61 64 ed pages instead
13f87 20 6f 66 20 0a 20 20 20 20 2a 2a 20 72 65 61 64 of . ** read
13f88 69 6e 67 20 64 61 74 61 20 66 72 6f 6d 20 74 68 ing data from th
13f89 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e e database file.
13f8a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 57 . **. ** W
13f8b 68 65 6e 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 hen journal_mode
13f8c 3d 3d 4f 46 46 20 74 68 65 20 64 62 4f 72 69 67 ==OFF the dbOrig
13f8d 53 69 7a 65 20 69 73 20 61 6c 77 61 79 73 20 7a Size is always z
13f8e 65 72 6f 2c 20 73 6f 20 74 68 69 73 0a 20 20 20 ero, so this.
13f8f 20 2a 2a 20 62 6c 6f 63 6b 20 6e 65 76 65 72 20 ** block never
13f90 72 75 6e 73 20 69 66 20 6a 6f 75 72 6e 61 6c 5f runs if journal_
13f91 6d 6f 64 65 3d 4f 46 46 2e 0a 20 20 20 20 2a 2f mode=OFF.. */
13f92 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
13f93 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a OMIT_AUTOVACUUM.
13f94 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e if( pPager->
13f95 64 62 53 69 7a 65 3c 70 50 61 67 65 72 2d 3e 64 dbSize<pPager->d
13f96 62 4f 72 69 67 53 69 7a 65 20 0a 20 20 20 20 20 bOrigSize .
13f97 26 26 20 41 4c 57 41 59 53 28 70 50 61 67 65 72 && ALWAYS(pPager
13f98 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 21 3d 50 ->journalMode!=P
13f99 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 AGER_JOURNALMODE
13f9a 5f 4f 46 46 29 0a 20 20 20 20 29 7b 0a 20 20 20 _OFF). ){.
13f9b 20 20 20 50 67 6e 6f 20 69 3b 20 20 20 20 20 20 Pgno i;
13f9c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13f9d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
13f9e 49 74 65 72 61 74 6f 72 20 76 61 72 69 61 62 6c Iterator variabl
13f9f 65 20 2a 2f 0a 20 20 20 20 20 20 63 6f 6e 73 74 e */. const
13fa0 20 50 67 6e 6f 20 69 53 6b 69 70 20 3d 20 50 41 Pgno iSkip = PA
13fa1 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 GER_MJ_PGNO(pPag
13fa2 65 72 29 3b 20 2f 2a 20 50 65 6e 64 69 6e 67 20 er); /* Pending
13fa3 6c 6f 63 6b 20 70 61 67 65 20 2a 2f 0a 20 20 20 lock page */.
13fa4 20 20 20 63 6f 6e 73 74 20 50 67 6e 6f 20 64 62 const Pgno db
13fa5 53 69 7a 65 20 3d 20 70 50 61 67 65 72 2d 3e 64 Size = pPager->d
13fa6 62 53 69 7a 65 3b 20 20 20 20 20 20 20 2f 2a 20 bSize; /*
13fa7 44 61 74 61 62 61 73 65 20 69 6d 61 67 65 20 73 Database image s
13fa8 69 7a 65 20 2a 2f 20 0a 20 20 20 20 20 20 70 50 ize */ . pP
13fa9 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20 70 ager->dbSize = p
13faa 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a Pager->dbOrigSiz
13fab 65 3b 0a 20 20 20 20 20 20 66 6f 72 28 20 69 3d e;. for( i=
13fac 64 62 53 69 7a 65 2b 31 3b 20 69 3c 3d 70 50 61 dbSize+1; i<=pPa
13fad 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 3b ger->dbOrigSize;
13fae 20 69 2b 2b 20 29 7b 0a 20 20 20 20 20 20 20 20 i++ ){.
13faf 69 66 28 20 21 73 71 6c 69 74 65 33 42 69 74 76 if( !sqlite3Bitv
13fb0 65 63 54 65 73 74 28 70 50 61 67 65 72 2d 3e 70 ecTest(pPager->p
13fb1 49 6e 4a 6f 75 72 6e 61 6c 2c 20 69 29 20 26 26 InJournal, i) &&
13fb2 20 69 21 3d 69 53 6b 69 70 20 29 7b 0a 20 20 20 i!=iSkip ){.
13fb3 20 20 20 20 20 20 20 50 67 48 64 72 20 2a 70 50 PgHdr *pP
13fb4 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 age;
13fb5 20 2f 2a 20 50 61 67 65 20 74 6f 20 6a 6f 75 72 /* Page to jour
13fb6 6e 61 6c 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 nal */.
13fb7 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
13fb8 65 72 47 65 74 28 70 50 61 67 65 72 2c 20 69 2c erGet(pPager, i,
13fb9 20 26 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 &pPage);.
13fba 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
13fbb 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 63 6f 6d TE_OK ) goto com
13fbc 6d 69 74 5f 70 68 61 73 65 5f 6f 6e 65 5f 65 78 mit_phase_one_ex
13fbd 69 74 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63 it;. rc
13fbe 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 = sqlite3PagerW
13fbf 72 69 74 65 28 70 50 61 67 65 29 3b 0a 20 20 20 rite(pPage);.
13fc0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 sqlite3Pa
13fc1 67 65 72 55 6e 72 65 66 28 70 50 61 67 65 29 3b gerUnref(pPage);
13fc2 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 . if( r
13fc3 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 c!=SQLITE_OK ) g
13fc4 6f 74 6f 20 63 6f 6d 6d 69 74 5f 70 68 61 73 65 oto commit_phase
13fc5 5f 6f 6e 65 5f 65 78 69 74 3b 0a 20 20 20 20 20 _one_exit;.
13fc6 20 20 20 7d 0a 20 20 20 20 20 20 7d 20 0a 20 20 }. } .
13fc7 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 pPager->dbSi
13fc8 7a 65 20 3d 20 64 62 53 69 7a 65 3b 0a 20 20 20 ze = dbSize;.
13fc9 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f }.#endif.. /
13fca 2a 20 57 72 69 74 65 20 74 68 65 20 6d 61 73 74 * Write the mast
13fcb 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 er journal name
13fcc 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c into the journal
13fcd 20 66 69 6c 65 2e 20 49 66 20 61 20 6d 61 73 74 file. If a mast
13fce 65 72 20 0a 20 20 20 20 2a 2a 20 6a 6f 75 72 6e er . ** journ
13fcf 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 68 61 73 al file name has
13fd0 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 77 72 already been wr
13fd1 69 74 74 65 6e 20 74 6f 20 74 68 65 20 6a 6f 75 itten to the jou
13fd2 72 6e 61 6c 20 66 69 6c 65 2c 20 0a 20 20 20 20 rnal file, .
13fd3 2a 2a 20 6f 72 20 69 66 20 7a 4d 61 73 74 65 72 ** or if zMaster
13fd4 20 69 73 20 4e 55 4c 4c 20 28 6e 6f 20 6d 61 73 is NULL (no mas
13fd5 74 65 72 20 6a 6f 75 72 6e 61 6c 29 2c 20 74 68 ter journal), th
13fd6 65 6e 20 74 68 69 73 20 63 61 6c 6c 20 69 73 20 en this call is
13fd7 61 20 6e 6f 2d 6f 70 2e 0a 20 20 20 20 2a 2f 0a a no-op.. */.
13fd8 20 20 20 20 72 63 20 3d 20 77 72 69 74 65 4d 61 rc = writeMa
13fd9 73 74 65 72 4a 6f 75 72 6e 61 6c 28 70 50 61 67 sterJournal(pPag
13fda 65 72 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20 20 er, zMaster);.
13fdb 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
13fdc 5f 4f 4b 20 29 20 67 6f 74 6f 20 63 6f 6d 6d 69 _OK ) goto commi
13fdd 74 5f 70 68 61 73 65 5f 6f 6e 65 5f 65 78 69 74 t_phase_one_exit
13fde 3b 0a 0a 20 20 20 20 2f 2a 20 53 79 6e 63 20 74 ;.. /* Sync t
13fdf 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e he journal file.
13fe0 20 49 66 20 74 68 65 20 61 74 6f 6d 69 63 2d 75 If the atomic-u
13fe1 70 64 61 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 pdate optimizati
13fe2 6f 6e 20 69 73 20 62 65 69 6e 67 0a 20 20 20 20 on is being.
13fe3 2a 2a 20 75 73 65 64 2c 20 74 68 69 73 20 63 61 ** used, this ca
13fe4 6c 6c 20 77 69 6c 6c 20 6e 6f 74 20 63 72 65 61 ll will not crea
13fe5 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 te the journal f
13fe6 69 6c 65 20 6f 72 20 70 65 72 66 6f 72 6d 20 61 ile or perform a
13fe7 6e 79 0a 20 20 20 20 2a 2a 20 72 65 61 6c 20 49 ny. ** real I
13fe8 4f 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 O.. */. rc
13fe9 20 3d 20 73 79 6e 63 4a 6f 75 72 6e 61 6c 28 70 = syncJournal(p
13fea 50 61 67 65 72 29 3b 0a 20 20 20 20 69 66 28 20 Pager);. if(
13feb 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 rc!=SQLITE_OK )
13fec 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f 70 68 61 73 goto commit_phas
13fed 65 5f 6f 6e 65 5f 65 78 69 74 3b 0a 0a 20 20 20 e_one_exit;..
13fee 20 2f 2a 20 57 72 69 74 65 20 61 6c 6c 20 64 69 /* Write all di
13fef 72 74 79 20 70 61 67 65 73 20 74 6f 20 74 68 65 rty pages to the
13ff0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 database file.
13ff1 2a 2f 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65 */. rc = page
13ff2 72 5f 77 72 69 74 65 5f 70 61 67 65 6c 69 73 74 r_write_pagelist
13ff3 28 73 71 6c 69 74 65 33 50 63 61 63 68 65 44 69 (sqlite3PcacheDi
13ff4 72 74 79 4c 69 73 74 28 70 50 61 67 65 72 2d 3e rtyList(pPager->
13ff5 70 50 43 61 63 68 65 29 29 3b 0a 20 20 20 20 69 pPCache));. i
13ff6 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
13ff7 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 ){. assert
13ff8 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 49 4f 45 ( rc!=SQLITE_IOE
13ff9 52 52 5f 42 4c 4f 43 4b 45 44 20 29 3b 0a 20 20 RR_BLOCKED );.
13ffa 20 20 20 20 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f goto commit_
13ffb 70 68 61 73 65 5f 6f 6e 65 5f 65 78 69 74 3b 0a phase_one_exit;.
13ffc 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 }. sqlite
13ffd 33 50 63 61 63 68 65 43 6c 65 61 6e 41 6c 6c 28 3PcacheCleanAll(
13ffe 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 pPager->pPCache)
13fff 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 ;.. /* If the
14000 20 66 69 6c 65 20 6f 6e 20 64 69 73 6b 20 69 73 file on disk is
14001 20 6e 6f 74 20 74 68 65 20 73 61 6d 65 20 73 69 not the same si
14002 7a 65 20 61 73 20 74 68 65 20 64 61 74 61 62 61 ze as the databa
14003 73 65 20 69 6d 61 67 65 2c 0a 20 20 20 20 2a 2a se image,. **
14004 20 74 68 65 6e 20 75 73 65 20 70 61 67 65 72 5f then use pager_
14005 74 72 75 6e 63 61 74 65 20 74 6f 20 67 72 6f 77 truncate to grow
14006 20 6f 72 20 73 68 72 69 6e 6b 20 74 68 65 20 66 or shrink the f
14007 69 6c 65 20 68 65 72 65 2e 0a 20 20 20 20 2a 2f ile here.. */
14008 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d . if( pPager-
14009 3e 64 62 53 69 7a 65 21 3d 70 50 61 67 65 72 2d >dbSize!=pPager-
1400a 3e 64 62 46 69 6c 65 53 69 7a 65 20 29 7b 0a 20 >dbFileSize ){.
1400b 20 20 20 20 20 50 67 6e 6f 20 6e 4e 65 77 20 3d Pgno nNew =
1400c 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 pPager->dbSize
1400d 2d 20 28 70 50 61 67 65 72 2d 3e 64 62 53 69 7a - (pPager->dbSiz
1400e 65 3d 3d 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f e==PAGER_MJ_PGNO
1400f 28 70 50 61 67 65 72 29 29 3b 0a 20 20 20 20 20 (pPager));.
14010 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
14011 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 45 58 >state>=PAGER_EX
14012 43 4c 55 53 49 56 45 20 29 3b 0a 20 20 20 20 20 CLUSIVE );.
14013 20 72 63 20 3d 20 70 61 67 65 72 5f 74 72 75 6e rc = pager_trun
14014 63 61 74 65 28 70 50 61 67 65 72 2c 20 6e 4e 65 cate(pPager, nNe
14015 77 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 w);. if( rc
14016 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f !=SQLITE_OK ) go
14017 74 6f 20 63 6f 6d 6d 69 74 5f 70 68 61 73 65 5f to commit_phase_
14018 6f 6e 65 5f 65 78 69 74 3b 0a 20 20 20 20 7d 0a one_exit;. }.
14019 0a 20 20 20 20 2f 2a 20 46 69 6e 61 6c 6c 79 2c . /* Finally,
1401a 20 73 79 6e 63 20 74 68 65 20 64 61 74 61 62 61 sync the databa
1401b 73 65 20 66 69 6c 65 2e 20 2a 2f 0a 20 20 20 20 se file. */.
1401c 69 66 28 20 21 70 50 61 67 65 72 2d 3e 6e 6f 53 if( !pPager->noS
1401d 79 6e 63 20 26 26 20 21 6e 6f 53 79 6e 63 20 29 ync && !noSync )
1401e 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c {. rc = sql
1401f 69 74 65 33 4f 73 53 79 6e 63 28 70 50 61 67 65 ite3OsSync(pPage
14020 72 2d 3e 66 64 2c 20 70 50 61 67 65 72 2d 3e 73 r->fd, pPager->s
14021 79 6e 63 5f 66 6c 61 67 73 29 3b 0a 20 20 20 20 ync_flags);.
14022 7d 0a 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 }. IOTRACE(("
14023 44 42 53 59 4e 43 20 25 70 5c 6e 22 2c 20 70 50 DBSYNC %p\n", pP
14024 61 67 65 72 29 29 0a 0a 20 20 20 20 70 50 61 67 ager)).. pPag
14025 65 72 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45 er->state = PAGE
14026 52 5f 53 59 4e 43 45 44 3b 0a 20 20 7d 0a 0a 63 R_SYNCED;. }..c
14027 6f 6d 6d 69 74 5f 70 68 61 73 65 5f 6f 6e 65 5f ommit_phase_one_
14028 65 78 69 74 3a 0a 20 20 72 65 74 75 72 6e 20 72 exit:. return r
14029 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 c;.}.../*.** Whe
1402a 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 n this function
1402b 69 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 20 64 is called, the d
1402c 61 74 61 62 61 73 65 20 66 69 6c 65 20 68 61 73 atabase file has
1402d 20 62 65 65 6e 20 63 6f 6d 70 6c 65 74 65 6c 79 been completely
1402e 0a 2a 2a 20 75 70 64 61 74 65 64 20 74 6f 20 72 .** updated to r
1402f 65 66 6c 65 63 74 20 74 68 65 20 63 68 61 6e 67 eflect the chang
14030 65 73 20 6d 61 64 65 20 62 79 20 74 68 65 20 63 es made by the c
14031 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69 urrent transacti
14032 6f 6e 20 61 6e 64 0a 2a 2a 20 73 79 6e 63 65 64 on and.** synced
14033 20 74 6f 20 64 69 73 6b 2e 20 54 68 65 20 6a 6f to disk. The jo
14034 75 72 6e 61 6c 20 66 69 6c 65 20 73 74 69 6c 6c urnal file still
14035 20 65 78 69 73 74 73 20 69 6e 20 74 68 65 20 66 exists in the f
14036 69 6c 65 2d 73 79 73 74 65 6d 20 0a 2a 2a 20 74 ile-system .** t
14037 68 6f 75 67 68 2c 20 61 6e 64 20 69 66 20 61 20 hough, and if a
14038 66 61 69 6c 75 72 65 20 6f 63 63 75 72 73 20 61 failure occurs a
14039 74 20 74 68 69 73 20 70 6f 69 6e 74 20 69 74 20 t this point it
1403a 77 69 6c 6c 20 65 76 65 6e 74 75 61 6c 6c 79 0a will eventually.
1403b 2a 2a 20 62 65 20 75 73 65 64 20 61 73 20 61 20 ** be used as a
1403c 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 hot-journal and
1403d 74 68 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e the current tran
1403e 73 61 63 74 69 6f 6e 20 72 6f 6c 6c 65 64 20 62 saction rolled b
1403f 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ack..**.** This
14040 66 75 6e 63 74 69 6f 6e 20 66 69 6e 61 6c 69 7a function finaliz
14041 65 73 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 es the journal f
14042 69 6c 65 2c 20 65 69 74 68 65 72 20 62 79 20 64 ile, either by d
14043 65 6c 65 74 69 6e 67 2c 20 0a 2a 2a 20 74 72 75 eleting, .** tru
14044 6e 63 61 74 69 6e 67 20 6f 72 20 70 61 72 74 69 ncating or parti
14045 61 6c 6c 79 20 7a 65 72 6f 69 6e 67 20 69 74 2c ally zeroing it,
14046 20 73 6f 20 74 68 61 74 20 69 74 20 63 61 6e 6e so that it cann
14047 6f 74 20 62 65 20 75 73 65 64 20 0a 2a 2a 20 66 ot be used .** f
14048 6f 72 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 or hot-journal r
14049 6f 6c 6c 62 61 63 6b 2e 20 4f 6e 63 65 20 74 68 ollback. Once th
1404a 69 73 20 69 73 20 64 6f 6e 65 20 74 68 65 20 74 is is done the t
1404b 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 0a 2a 2a ransaction is.**
1404c 20 69 72 72 65 76 6f 63 61 62 6c 79 20 63 6f 6d irrevocably com
1404d 6d 69 74 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 mitted..**.** If
1404e 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 an error occurs
1404f 2c 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f , an IO error co
14050 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 de is returned a
14051 6e 64 20 74 68 65 20 70 61 67 65 72 0a 2a 2a 20 nd the pager.**
14052 6d 6f 76 65 73 20 69 6e 74 6f 20 74 68 65 20 65 moves into the e
14053 72 72 6f 72 20 73 74 61 74 65 2e 20 4f 74 68 65 rror state. Othe
14054 72 77 69 73 65 2c 20 53 51 4c 49 54 45 5f 4f 4b rwise, SQLITE_OK
14055 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f is returned..*/
14056 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
14057 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 int sqlite3Pager
14058 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 50 CommitPhaseTwo(P
14059 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 ager *pPager){.
1405a 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
1405b 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20 _OK;
1405c 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 /* Return
1405d 63 6f 64 65 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 code */.. /* Th
1405e 69 73 20 72 6f 75 74 69 6e 65 20 73 68 6f 75 6c is routine shoul
1405f 64 20 6e 6f 74 20 62 65 20 63 61 6c 6c 65 64 20 d not be called
14060 69 66 20 61 20 70 72 69 6f 72 20 65 72 72 6f 72 if a prior error
14061 20 68 61 73 20 6f 63 63 75 72 72 65 64 2e 0a 20 has occurred..
14062 20 2a 2a 20 42 75 74 20 69 66 20 28 64 75 65 20 ** But if (due
14063 74 6f 20 61 20 63 6f 64 69 6e 67 20 65 72 72 6f to a coding erro
14064 72 20 65 6c 73 65 77 68 65 72 65 20 69 6e 20 74 r elsewhere in t
14065 68 65 20 73 79 73 74 65 6d 29 20 69 74 20 64 6f he system) it do
14066 65 73 20 67 65 74 0a 20 20 2a 2a 20 63 61 6c 6c es get. ** call
14067 65 64 2c 20 6a 75 73 74 20 72 65 74 75 72 6e 20 ed, just return
14068 74 68 65 20 73 61 6d 65 20 65 72 72 6f 72 20 63 the same error c
14069 6f 64 65 20 77 69 74 68 6f 75 74 20 64 6f 69 6e ode without doin
1406a 67 20 61 6e 79 74 68 69 6e 67 2e 20 2a 2f 0a 20 g anything. */.
1406b 20 69 66 28 20 4e 45 56 45 52 28 70 50 61 67 65 if( NEVER(pPage
1406c 72 2d 3e 65 72 72 43 6f 64 65 29 20 29 20 72 65 r->errCode) ) re
1406d 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 65 72 72 turn pPager->err
1406e 43 6f 64 65 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 Code;.. /* This
1406f 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 function should
14070 20 6e 6f 74 20 62 65 20 63 61 6c 6c 65 64 20 69 not be called i
14071 66 20 74 68 65 20 70 61 67 65 72 20 69 73 20 6e f the pager is n
14072 6f 74 20 69 6e 20 61 74 20 6c 65 61 73 74 0a 20 ot in at least.
14073 20 2a 2a 20 50 41 47 45 52 5f 52 45 53 45 52 56 ** PAGER_RESERV
14074 45 44 20 73 74 61 74 65 2e 20 41 6e 64 20 69 6e ED state. And in
14075 64 65 65 64 20 53 51 4c 69 74 65 20 6e 65 76 65 deed SQLite neve
14076 72 20 64 6f 65 73 20 74 68 69 73 2e 20 42 75 74 r does this. But
14077 20 69 74 20 69 73 0a 20 20 2a 2a 20 6e 69 63 65 it is. ** nice
14078 20 74 6f 20 68 61 76 65 20 74 68 69 73 20 64 65 to have this de
14079 66 65 6e 73 69 76 65 20 74 65 73 74 20 68 65 72 fensive test her
1407a 65 20 61 6e 79 77 61 79 2e 0a 20 20 2a 2f 0a 20 e anyway.. */.
1407b 20 69 66 28 20 4e 45 56 45 52 28 70 50 61 67 65 if( NEVER(pPage
1407c 72 2d 3e 73 74 61 74 65 3c 50 41 47 45 52 5f 52 r->state<PAGER_R
1407d 45 53 45 52 56 45 44 29 20 29 20 72 65 74 75 72 ESERVED) ) retur
1407e 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a n SQLITE_ERROR;.
1407f 0a 20 20 2f 2a 20 41 6e 20 6f 70 74 69 6d 69 7a . /* An optimiz
14080 61 74 69 6f 6e 2e 20 49 66 20 74 68 65 20 64 61 ation. If the da
14081 74 61 62 61 73 65 20 77 61 73 20 6e 6f 74 20 61 tabase was not a
14082 63 74 75 61 6c 6c 79 20 6d 6f 64 69 66 69 65 64 ctually modified
14083 20 64 75 72 69 6e 67 0a 20 20 2a 2a 20 74 68 69 during. ** thi
14084 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 s transaction, t
14085 68 65 20 70 61 67 65 72 20 69 73 20 72 75 6e 6e he pager is runn
14086 69 6e 67 20 69 6e 20 65 78 63 6c 75 73 69 76 65 ing in exclusive
14087 2d 6d 6f 64 65 20 61 6e 64 20 69 73 0a 20 20 2a -mode and is. *
14088 2a 20 75 73 69 6e 67 20 70 65 72 73 69 73 74 65 * using persiste
14089 6e 74 20 6a 6f 75 72 6e 61 6c 73 2c 20 74 68 65 nt journals, the
1408a 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 n this function
1408b 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 20 20 2a 2a is a no-op.. **
1408c 0a 20 20 2a 2a 20 54 68 65 20 73 74 61 72 74 20 . ** The start
1408d 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 of the journal f
1408e 69 6c 65 20 63 75 72 72 65 6e 74 6c 79 20 63 6f ile currently co
1408f 6e 74 61 69 6e 73 20 61 20 73 69 6e 67 6c 65 20 ntains a single
14090 6a 6f 75 72 6e 61 6c 20 0a 20 20 2a 2a 20 68 65 journal . ** he
14091 61 64 65 72 20 77 69 74 68 20 74 68 65 20 6e 52 ader with the nR
14092 65 63 20 66 69 65 6c 64 20 73 65 74 20 74 6f 20 ec field set to
14093 30 2e 20 49 66 20 73 75 63 68 20 61 20 6a 6f 75 0. If such a jou
14094 72 6e 61 6c 20 69 73 20 75 73 65 64 20 61 73 0a rnal is used as.
14095 20 20 2a 2a 20 61 20 68 6f 74 2d 6a 6f 75 72 6e ** a hot-journ
14096 61 6c 20 64 75 72 69 6e 67 20 68 6f 74 2d 6a 6f al during hot-jo
14097 75 72 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b 2c 20 urnal rollback,
14098 30 20 63 68 61 6e 67 65 73 20 77 69 6c 6c 20 62 0 changes will b
14099 65 20 6d 61 64 65 0a 20 20 2a 2a 20 74 6f 20 74 e made. ** to t
1409a 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
1409b 2e 20 53 6f 20 74 68 65 72 65 20 69 73 20 6e 6f . So there is no
1409c 20 6e 65 65 64 20 74 6f 20 7a 65 72 6f 20 74 68 need to zero th
1409d 65 20 6a 6f 75 72 6e 61 6c 20 0a 20 20 2a 2a 20 e journal . **
1409e 68 65 61 64 65 72 2e 20 53 69 6e 63 65 20 74 68 header. Since th
1409f 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 65 78 e pager is in ex
140a0 63 6c 75 73 69 76 65 20 6d 6f 64 65 2c 20 74 68 clusive mode, th
140a1 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 0a 20 ere is no need.
140a2 20 2a 2a 20 74 6f 20 64 72 6f 70 20 61 6e 79 20 ** to drop any
140a3 6c 6f 63 6b 73 20 65 69 74 68 65 72 2e 0a 20 20 locks either..
140a4 2a 2f 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d */. if( pPager-
140a5 3e 64 62 4d 6f 64 69 66 69 65 64 3d 3d 30 20 26 >dbModified==0 &
140a6 26 20 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 & pPager->exclus
140a7 69 76 65 4d 6f 64 65 20 0a 20 20 20 26 26 20 70 iveMode . && p
140a8 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f Pager->journalMo
140a9 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 de==PAGER_JOURNA
140aa 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54 0a 20 20 LMODE_PERSIST.
140ab 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 ){. assert( p
140ac 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 Pager->journalOf
140ad 66 3d 3d 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 f==JOURNAL_HDR_S
140ae 5a 28 70 50 61 67 65 72 29 20 29 3b 0a 20 20 20 Z(pPager) );.
140af 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
140b0 4b 3b 0a 20 20 7d 0a 0a 20 20 50 41 47 45 52 54 K;. }.. PAGERT
140b1 52 41 43 45 28 28 22 43 4f 4d 4d 49 54 20 25 64 RACE(("COMMIT %d
140b2 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 70 50 61 \n", PAGERID(pPa
140b3 67 65 72 29 29 29 3b 0a 20 20 61 73 73 65 72 74 ger)));. assert
140b4 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d ( pPager->state=
140b5 3d 50 41 47 45 52 5f 53 59 4e 43 45 44 20 7c 7c =PAGER_SYNCED ||
140b6 20 4d 45 4d 44 42 20 7c 7c 20 21 70 50 61 67 65 MEMDB || !pPage
140b7 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64 20 29 3b r->dbModified );
140b8 0a 20 20 72 63 20 3d 20 70 61 67 65 72 5f 65 6e . rc = pager_en
140b9 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 70 50 d_transaction(pP
140ba 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 73 65 ager, pPager->se
140bb 74 4d 61 73 74 65 72 29 3b 0a 20 20 72 65 74 75 tMaster);. retu
140bc 72 6e 20 70 61 67 65 72 5f 65 72 72 6f 72 28 70 rn pager_error(p
140bd 50 61 67 65 72 2c 20 72 63 29 3b 0a 7d 0a 0a 2f Pager, rc);.}../
140be 2a 0a 2a 2a 20 52 6f 6c 6c 62 61 63 6b 20 61 6c *.** Rollback al
140bf 6c 20 63 68 61 6e 67 65 73 2e 20 54 68 65 20 64 l changes. The d
140c0 61 74 61 62 61 73 65 20 66 61 6c 6c 73 20 62 61 atabase falls ba
140c1 63 6b 20 74 6f 20 50 41 47 45 52 5f 53 48 41 52 ck to PAGER_SHAR
140c2 45 44 20 6d 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 ED mode..**.** T
140c3 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 70 65 72 his function per
140c4 66 6f 72 6d 73 20 74 77 6f 20 74 61 73 6b 73 3a forms two tasks:
140c5 0a 2a 2a 0a 2a 2a 20 20 20 31 29 20 49 74 20 72 .**.** 1) It r
140c6 6f 6c 6c 73 20 62 61 63 6b 20 74 68 65 20 6a 6f olls back the jo
140c7 75 72 6e 61 6c 20 66 69 6c 65 2c 20 72 65 73 74 urnal file, rest
140c8 6f 72 69 6e 67 20 61 6c 6c 20 64 61 74 61 62 61 oring all databa
140c9 73 65 20 66 69 6c 65 20 61 6e 64 20 0a 2a 2a 20 se file and .**
140ca 20 20 20 20 20 69 6e 2d 6d 65 6d 6f 72 79 20 63 in-memory c
140cb 61 63 68 65 20 70 61 67 65 73 20 74 6f 20 74 68 ache pages to th
140cc 65 20 73 74 61 74 65 20 74 68 65 79 20 77 65 72 e state they wer
140cd 65 20 69 6e 20 77 68 65 6e 20 74 68 65 20 74 72 e in when the tr
140ce 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 ansaction.**
140cf 20 20 77 61 73 20 6f 70 65 6e 65 64 2c 20 61 6e was opened, an
140d0 64 0a 2a 2a 20 20 20 32 29 20 49 74 20 66 69 6e d.** 2) It fin
140d1 61 6c 69 7a 65 73 20 74 68 65 20 6a 6f 75 72 6e alizes the journ
140d2 61 6c 20 66 69 6c 65 2c 20 73 6f 20 74 68 61 74 al file, so that
140d3 20 69 74 20 69 73 20 6e 6f 74 20 75 73 65 64 20 it is not used
140d4 66 6f 72 20 68 6f 74 0a 2a 2a 20 20 20 20 20 20 for hot.**
140d5 72 6f 6c 6c 62 61 63 6b 20 61 74 20 61 6e 79 20 rollback at any
140d6 70 6f 69 6e 74 20 69 6e 20 74 68 65 20 66 75 74 point in the fut
140d7 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 73 75 62 6a 65 ure..**.** subje
140d8 63 74 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 ct to the follow
140d9 69 6e 67 20 71 75 61 6c 69 66 69 63 61 74 69 6f ing qualificatio
140da 6e 73 3a 0a 2a 2a 0a 2a 2a 20 2a 20 49 66 20 74 ns:.**.** * If t
140db 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
140dc 69 73 20 6e 6f 74 20 79 65 74 20 6f 70 65 6e 20 is not yet open
140dd 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 when this functi
140de 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2c 0a 2a 2a on is called,.**
140df 20 20 20 74 68 65 6e 20 6f 6e 6c 79 20 28 32 29 then only (2)
140e0 20 69 73 20 70 65 72 66 6f 72 6d 65 64 2e 20 49 is performed. I
140e1 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65 72 n this case ther
140e2 65 20 69 73 20 6e 6f 20 6a 6f 75 72 6e 61 6c 20 e is no journal
140e3 66 69 6c 65 0a 2a 2a 20 20 20 74 6f 20 72 6f 6c file.** to rol
140e4 6c 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 2a 20 l back..**.** *
140e5 49 66 20 69 6e 20 61 6e 20 65 72 72 6f 72 20 73 If in an error s
140e6 74 61 74 65 20 6f 74 68 65 72 20 74 68 61 6e 20 tate other than
140e7 53 51 4c 49 54 45 5f 46 55 4c 4c 2c 20 74 68 65 SQLITE_FULL, the
140e8 6e 20 74 61 73 6b 20 28 31 29 20 69 73 20 0a 2a n task (1) is .*
140e9 2a 20 20 20 70 65 72 66 6f 72 6d 65 64 2e 20 49 * performed. I
140ea 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 74 61 f successful, ta
140eb 73 6b 20 28 32 29 2e 20 52 65 67 61 72 64 6c 65 sk (2). Regardle
140ec 73 73 20 6f 66 20 74 68 65 20 6f 75 74 63 6f 6d ss of the outcom
140ed 65 0a 2a 2a 20 20 20 6f 66 20 65 69 74 68 65 72 e.** of either
140ee 2c 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 , the error stat
140ef 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 e error code is
140f0 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20 returned to the
140f1 63 61 6c 6c 65 72 0a 2a 2a 20 20 20 28 69 2e 65 caller.** (i.e
140f2 2e 20 65 69 74 68 65 72 20 53 51 4c 49 54 45 5f . either SQLITE_
140f3 49 4f 45 52 52 20 6f 72 20 53 51 4c 49 54 45 5f IOERR or SQLITE_
140f4 43 4f 52 52 55 50 54 29 2e 0a 2a 2a 0a 2a 2a 20 CORRUPT)..**.**
140f5 2a 20 49 66 20 74 68 65 20 70 61 67 65 72 20 69 * If the pager i
140f6 73 20 69 6e 20 50 41 47 45 52 5f 52 45 53 45 52 s in PAGER_RESER
140f7 56 45 44 20 73 74 61 74 65 2c 20 74 68 65 6e 20 VED state, then
140f8 61 74 74 65 6d 70 74 20 28 31 29 2e 20 57 68 65 attempt (1). Whe
140f9 74 68 65 72 0a 2a 2a 20 20 20 6f 72 20 6e 6f 74 ther.** or not
140fa 20 28 31 29 20 69 73 20 73 75 63 63 75 73 73 66 (1) is succussf
140fb 75 6c 2c 20 61 6c 73 6f 20 61 74 74 65 6d 70 74 ul, also attempt
140fc 20 28 32 29 2e 20 49 66 20 73 75 63 63 65 73 73 (2). If success
140fd 66 75 6c 2c 20 72 65 74 75 72 6e 0a 2a 2a 20 20 ful, return.**
140fe 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 4f 74 68 65 SQLITE_OK. Othe
140ff 72 77 69 73 65 2c 20 65 6e 74 65 72 20 74 68 65 rwise, enter the
14100 20 65 72 72 6f 72 20 73 74 61 74 65 20 61 6e 64 error state and
14101 20 72 65 74 75 72 6e 20 74 68 65 20 66 69 72 73 return the firs
14102 74 20 0a 2a 2a 20 20 20 65 72 72 6f 72 20 63 6f t .** error co
14103 64 65 20 65 6e 63 6f 75 6e 74 65 72 65 64 2e 20 de encountered.
14104 0a 2a 2a 0a 2a 2a 20 20 20 49 6e 20 74 68 69 73 .**.** In this
14105 20 63 61 73 65 20 74 68 65 72 65 20 69 73 20 6e case there is n
14106 6f 20 63 68 61 6e 63 65 20 74 68 61 74 20 74 68 o chance that th
14107 65 20 64 61 74 61 62 61 73 65 20 77 61 73 20 77 e database was w
14108 72 69 74 74 65 6e 20 74 6f 2e 20 0a 2a 2a 20 20 ritten to. .**
14109 20 53 6f 20 69 73 20 73 61 66 65 20 74 6f 20 66 So is safe to f
1410a 69 6e 61 6c 69 7a 65 20 74 68 65 20 6a 6f 75 72 inalize the jour
1410b 6e 61 6c 20 66 69 6c 65 20 65 76 65 6e 20 69 66 nal file even if
1410c 20 74 68 65 20 70 6c 61 79 62 61 63 6b 20 0a 2a the playback .*
1410d 2a 20 20 20 28 6f 70 65 72 61 74 69 6f 6e 20 31 * (operation 1
1410e 29 20 66 61 69 6c 65 64 2e 20 48 6f 77 65 76 65 ) failed. Howeve
1410f 72 20 74 68 65 20 70 61 67 65 72 20 6d 75 73 74 r the pager must
14110 20 65 6e 74 65 72 20 74 68 65 20 65 72 72 6f 72 enter the error
14111 20 73 74 61 74 65 0a 2a 2a 20 20 20 61 73 20 74 state.** as t
14112 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 he contents of t
14113 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 63 61 63 he in-memory cac
14114 68 65 20 61 72 65 20 6e 6f 77 20 73 75 73 70 65 he are now suspe
14115 63 74 2e 0a 2a 2a 0a 2a 2a 20 2a 20 46 69 6e 61 ct..**.** * Fina
14116 6c 6c 79 2c 20 69 66 20 69 6e 20 50 41 47 45 52 lly, if in PAGER
14117 5f 45 58 43 4c 55 53 49 56 45 20 73 74 61 74 65 _EXCLUSIVE state
14118 2c 20 74 68 65 6e 20 61 74 74 65 6d 70 74 20 28 , then attempt (
14119 31 29 2e 20 4f 6e 6c 79 0a 2a 2a 20 20 20 61 74 1). Only.** at
1411a 74 65 6d 70 74 20 28 32 29 20 69 66 20 28 31 29 tempt (2) if (1)
1411b 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 2e 20 is successful.
1411c 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b Return SQLITE_OK
1411d 20 69 66 20 73 75 63 63 65 73 73 66 75 6c 2c 0a if successful,.
1411e 2a 2a 20 20 20 6f 74 68 65 72 77 69 73 65 20 65 ** otherwise e
1411f 6e 74 65 72 20 74 68 65 20 65 72 72 6f 72 20 73 nter the error s
14120 74 61 74 65 20 61 6e 64 20 72 65 74 75 72 6e 20 tate and return
14121 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 66 the error code f
14122 72 6f 6d 20 74 68 65 20 0a 2a 2a 20 20 20 66 61 rom the .** fa
14123 69 6c 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e 2e iling operation.
14124 0a 2a 2a 0a 2a 2a 20 20 20 49 6e 20 74 68 69 73 .**.** In this
14125 20 63 61 73 65 20 74 68 65 20 64 61 74 61 62 61 case the databa
14126 73 65 20 66 69 6c 65 20 6d 61 79 20 68 61 76 65 se file may have
14127 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20 74 6f been written to
14128 2e 20 53 6f 20 69 66 20 74 68 65 0a 2a 2a 20 20 . So if the.**
14129 20 70 6c 61 79 62 61 63 6b 20 6f 70 65 72 61 74 playback operat
1412a 69 6f 6e 20 64 69 64 20 6e 6f 74 20 73 75 63 63 ion did not succ
1412b 65 65 64 20 69 74 20 77 6f 75 6c 64 20 6e 6f 74 eed it would not
1412c 20 62 65 20 73 61 66 65 20 74 6f 20 66 69 6e 61 be safe to fina
1412d 6c 69 7a 65 0a 2a 2a 20 20 20 74 68 65 20 6a 6f lize.** the jo
1412e 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 74 20 6e urnal file. It n
1412f 65 65 64 73 20 74 6f 20 62 65 20 6c 65 66 74 20 eeds to be left
14130 69 6e 20 74 68 65 20 66 69 6c 65 2d 73 79 73 74 in the file-syst
14131 65 6d 20 73 6f 20 74 68 61 74 0a 2a 2a 20 20 20 em so that.**
14132 73 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f 63 65 some other proce
14133 73 73 20 63 61 6e 20 75 73 65 20 69 74 20 74 6f ss can use it to
14134 20 72 65 73 74 6f 72 65 20 74 68 65 20 64 61 74 restore the dat
14135 61 62 61 73 65 20 73 74 61 74 65 20 28 62 79 0a abase state (by.
14136 2a 2a 20 20 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c ** hot-journal
14137 20 72 6f 6c 6c 62 61 63 6b 29 2e 0a 2a 2f 0a 53 rollback)..*/.S
14138 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
14139 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 6f t sqlite3PagerRo
1413a 6c 6c 62 61 63 6b 28 50 61 67 65 72 20 2a 70 50 llback(Pager *pP
1413b 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 ager){. int rc
1413c 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 = SQLITE_OK;
1413d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1413e 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a Return code */.
1413f 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22 52 PAGERTRACE(("R
14140 4f 4c 4c 42 41 43 4b 20 25 64 5c 6e 22 2c 20 50 OLLBACK %d\n", P
14141 41 47 45 52 49 44 28 70 50 61 67 65 72 29 29 29 AGERID(pPager)))
14142 3b 0a 20 20 69 66 28 20 21 70 50 61 67 65 72 2d ;. if( !pPager-
14143 3e 64 62 4d 6f 64 69 66 69 65 64 20 7c 7c 20 21 >dbModified || !
14144 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a isOpen(pPager->j
14145 66 64 29 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 fd) ){. rc =
14146 70 61 67 65 72 5f 65 6e 64 5f 74 72 61 6e 73 61 pager_end_transa
14147 63 74 69 6f 6e 28 70 50 61 67 65 72 2c 20 70 50 ction(pPager, pP
14148 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 29 ager->setMaster)
14149 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 50 ;. }else if( pP
1414a 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 26 26 ager->errCode &&
1414b 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 pPager->errCode
1414c 21 3d 53 51 4c 49 54 45 5f 46 55 4c 4c 20 29 7b !=SQLITE_FULL ){
1414d 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d . if( pPager-
1414e 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 45 58 >state>=PAGER_EX
1414f 43 4c 55 53 49 56 45 20 29 7b 0a 20 20 20 20 20 CLUSIVE ){.
14150 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 pager_playback(
14151 70 50 61 67 65 72 2c 20 30 29 3b 0a 20 20 20 20 pPager, 0);.
14152 7d 0a 20 20 20 20 72 63 20 3d 20 70 50 61 67 65 }. rc = pPage
14153 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 20 20 7d 65 r->errCode;. }e
14154 6c 73 65 7b 0a 20 20 20 20 69 66 28 20 70 50 61 lse{. if( pPa
14155 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 ger->state==PAGE
14156 52 5f 52 45 53 45 52 56 45 44 20 29 7b 0a 20 20 R_RESERVED ){.
14157 20 20 20 20 69 6e 74 20 72 63 32 3b 0a 20 20 20 int rc2;.
14158 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 70 6c rc = pager_pl
14159 61 79 62 61 63 6b 28 70 50 61 67 65 72 2c 20 30 ayback(pPager, 0
1415a 29 3b 0a 20 20 20 20 20 20 72 63 32 20 3d 20 70 );. rc2 = p
1415b 61 67 65 72 5f 65 6e 64 5f 74 72 61 6e 73 61 63 ager_end_transac
1415c 74 69 6f 6e 28 70 50 61 67 65 72 2c 20 70 50 61 tion(pPager, pPa
1415d 67 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 29 3b ger->setMaster);
1415e 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 . if( rc==S
1415f 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
14160 20 20 20 20 72 63 20 3d 20 72 63 32 3b 0a 20 20 rc = rc2;.
14161 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b }. }else{
14162 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 . rc = page
14163 72 5f 70 6c 61 79 62 61 63 6b 28 70 50 61 67 65 r_playback(pPage
14164 72 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 r, 0);. }..
14165 20 20 69 66 28 20 21 4d 45 4d 44 42 20 29 7b 0a if( !MEMDB ){.
14166 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 pPager->db
14167 53 69 7a 65 56 61 6c 69 64 20 3d 20 30 3b 0a 20 SizeValid = 0;.
14168 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 }.. /* If
14169 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 an error occurs
1416a 64 75 72 69 6e 67 20 61 20 52 4f 4c 4c 42 41 43 during a ROLLBAC
1416b 4b 2c 20 77 65 20 63 61 6e 20 6e 6f 20 6c 6f 6e K, we can no lon
1416c 67 65 72 20 74 72 75 73 74 20 74 68 65 20 70 61 ger trust the pa
1416d 67 65 72 0a 20 20 20 20 2a 2a 20 63 61 63 68 65 ger. ** cache
1416e 2e 20 53 6f 20 63 61 6c 6c 20 70 61 67 65 72 5f . So call pager_
1416f 65 72 72 6f 72 28 29 20 6f 6e 20 74 68 65 20 77 error() on the w
14170 61 79 20 6f 75 74 20 74 6f 20 6d 61 6b 65 20 61 ay out to make a
14171 6e 79 20 65 72 72 6f 72 20 0a 20 20 20 20 2a 2a ny error . **
14172 20 70 65 72 73 69 73 74 65 6e 74 2e 0a 20 20 20 persistent..
14173 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 70 61 67 */. rc = pag
14174 65 72 5f 65 72 72 6f 72 28 70 50 61 67 65 72 2c er_error(pPager,
14175 20 72 63 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 rc);. }. retu
14176 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
14177 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 74 Return TRUE if t
14178 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
14179 20 69 73 20 6f 70 65 6e 65 64 20 72 65 61 64 2d is opened read-
1417a 6f 6e 6c 79 2e 20 20 52 65 74 75 72 6e 20 46 41 only. Return FA
1417b 4c 53 45 0a 2a 2a 20 69 66 20 74 68 65 20 64 61 LSE.** if the da
1417c 74 61 62 61 73 65 20 69 73 20 28 69 6e 20 74 68 tabase is (in th
1417d 65 6f 72 79 29 20 77 72 69 74 61 62 6c 65 2e 0a eory) writable..
1417e 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1417f 45 20 75 38 20 73 71 6c 69 74 65 33 50 61 67 65 E u8 sqlite3Page
14180 72 49 73 72 65 61 64 6f 6e 6c 79 28 50 61 67 65 rIsreadonly(Page
14181 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 65 r *pPager){. re
14182 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 72 65 61 turn pPager->rea
14183 64 4f 6e 6c 79 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 dOnly;.}../*.**
14184 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 Return the numbe
14185 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65 73 20 r of references
14186 74 6f 20 74 68 65 20 70 61 67 65 72 2e 0a 2a 2f to the pager..*/
14187 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
14188 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 int sqlite3Pager
14189 52 65 66 63 6f 75 6e 74 28 50 61 67 65 72 20 2a Refcount(Pager *
1418a 70 50 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 pPager){. retur
1418b 6e 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 n sqlite3PcacheR
1418c 65 66 43 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e efCount(pPager->
1418d 70 50 43 61 63 68 65 29 3b 0a 7d 0a 0a 2f 2a 0a pPCache);.}../*.
1418e 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 ** Return the nu
1418f 6d 62 65 72 20 6f 66 20 72 65 66 65 72 65 6e 63 mber of referenc
14190 65 73 20 74 6f 20 74 68 65 20 73 70 65 63 69 66 es to the specif
14191 69 65 64 20 70 61 67 65 2e 0a 2a 2f 0a 53 51 4c ied page..*/.SQL
14192 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
14193 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 sqlite3PagerPage
14194 52 65 66 63 6f 75 6e 74 28 44 62 50 61 67 65 20 Refcount(DbPage
14195 2a 70 50 61 67 65 29 7b 0a 20 20 72 65 74 75 72 *pPage){. retur
14196 6e 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 50 n sqlite3PcacheP
14197 61 67 65 52 65 66 63 6f 75 6e 74 28 70 50 61 67 ageRefcount(pPag
14198 65 29 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 e);.}..#ifdef SQ
14199 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 LITE_TEST./*.**
1419a 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 This routine is
1419b 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 used for testing
1419c 20 61 6e 64 20 61 6e 61 6c 79 73 69 73 20 6f 6e and analysis on
1419d 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ly..*/.SQLITE_PR
1419e 49 56 41 54 45 20 69 6e 74 20 2a 73 71 6c 69 74 IVATE int *sqlit
1419f 65 33 50 61 67 65 72 53 74 61 74 73 28 50 61 67 e3PagerStats(Pag
141a0 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 73 er *pPager){. s
141a1 74 61 74 69 63 20 69 6e 74 20 61 5b 31 31 5d 3b tatic int a[11];
141a2 0a 20 20 61 5b 30 5d 20 3d 20 73 71 6c 69 74 65 . a[0] = sqlite
141a3 33 50 63 61 63 68 65 52 65 66 43 6f 75 6e 74 28 3PcacheRefCount(
141a4 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 pPager->pPCache)
141a5 3b 0a 20 20 61 5b 31 5d 20 3d 20 73 71 6c 69 74 ;. a[1] = sqlit
141a6 65 33 50 63 61 63 68 65 50 61 67 65 63 6f 75 6e e3PcachePagecoun
141a7 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 t(pPager->pPCach
141a8 65 29 3b 0a 20 20 61 5b 32 5d 20 3d 20 73 71 6c e);. a[2] = sql
141a9 69 74 65 33 50 63 61 63 68 65 47 65 74 43 61 63 ite3PcacheGetCac
141aa 68 65 73 69 7a 65 28 70 50 61 67 65 72 2d 3e 70 hesize(pPager->p
141ab 50 43 61 63 68 65 29 3b 0a 20 20 61 5b 33 5d 20 PCache);. a[3]
141ac 3d 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 = pPager->dbSize
141ad 56 61 6c 69 64 20 3f 20 28 69 6e 74 29 20 70 50 Valid ? (int) pP
141ae 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 3a 20 2d ager->dbSize : -
141af 31 3b 0a 20 20 61 5b 34 5d 20 3d 20 70 50 61 67 1;. a[4] = pPag
141b0 65 72 2d 3e 73 74 61 74 65 3b 0a 20 20 61 5b 35 er->state;. a[5
141b1 5d 20 3d 20 70 50 61 67 65 72 2d 3e 65 72 72 43 ] = pPager->errC
141b2 6f 64 65 3b 0a 20 20 61 5b 36 5d 20 3d 20 70 50 ode;. a[6] = pP
141b3 61 67 65 72 2d 3e 6e 48 69 74 3b 0a 20 20 61 5b ager->nHit;. a[
141b4 37 5d 20 3d 20 70 50 61 67 65 72 2d 3e 6e 4d 69 7] = pPager->nMi
141b5 73 73 3b 0a 20 20 61 5b 38 5d 20 3d 20 30 3b 20 ss;. a[8] = 0;
141b6 20 2f 2a 20 55 73 65 64 20 74 6f 20 62 65 20 70 /* Used to be p
141b7 50 61 67 65 72 2d 3e 6e 4f 76 66 6c 20 2a 2f 0a Pager->nOvfl */.
141b8 20 20 61 5b 39 5d 20 3d 20 70 50 61 67 65 72 2d a[9] = pPager-
141b9 3e 6e 52 65 61 64 3b 0a 20 20 61 5b 31 30 5d 20 >nRead;. a[10]
141ba 3d 20 70 50 61 67 65 72 2d 3e 6e 57 72 69 74 65 = pPager->nWrite
141bb 3b 0a 20 20 72 65 74 75 72 6e 20 61 3b 0a 7d 0a ;. return a;.}.
141bc 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 #endif../*.** Re
141bd 74 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 69 turn true if thi
141be 73 20 69 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 s is an in-memor
141bf 79 20 70 61 67 65 72 2e 0a 2a 2f 0a 53 51 4c 49 y pager..*/.SQLI
141c0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
141c1 71 6c 69 74 65 33 50 61 67 65 72 49 73 4d 65 6d qlite3PagerIsMem
141c2 64 62 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 db(Pager *pPager
141c3 29 7b 0a 20 20 72 65 74 75 72 6e 20 4d 45 4d 44 ){. return MEMD
141c4 42 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 B;.}../*.** Chec
141c5 6b 20 74 68 61 74 20 74 68 65 72 65 20 61 72 65 k that there are
141c6 20 61 74 20 6c 65 61 73 74 20 6e 53 61 76 65 70 at least nSavep
141c7 6f 69 6e 74 20 73 61 76 65 70 6f 69 6e 74 73 20 oint savepoints
141c8 6f 70 65 6e 2e 20 49 66 20 74 68 65 72 65 20 61 open. If there a
141c9 72 65 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 re.** currently
141ca 6c 65 73 73 20 74 68 61 6e 20 6e 53 61 76 65 70 less than nSavep
141cb 6f 69 6e 74 73 20 6f 70 65 6e 2c 20 74 68 65 6e oints open, then
141cc 20 6f 70 65 6e 20 6f 6e 65 20 6f 72 20 6d 6f 72 open one or mor
141cd 65 20 73 61 76 65 70 6f 69 6e 74 73 0a 2a 2a 20 e savepoints.**
141ce 74 6f 20 6d 61 6b 65 20 75 70 20 74 68 65 20 64 to make up the d
141cf 69 66 66 65 72 65 6e 63 65 2e 20 49 66 20 74 68 ifference. If th
141d0 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 61 76 65 e number of save
141d1 70 6f 69 6e 74 73 20 69 73 20 61 6c 72 65 61 64 points is alread
141d2 79 0a 2a 2a 20 65 71 75 61 6c 20 74 6f 20 6e 53 y.** equal to nS
141d3 61 76 65 70 6f 69 6e 74 2c 20 74 68 65 6e 20 74 avepoint, then t
141d4 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
141d5 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 a no-op..**.** I
141d6 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 f a memory alloc
141d7 61 74 69 6f 6e 20 66 61 69 6c 73 2c 20 53 51 4c ation fails, SQL
141d8 49 54 45 5f 4e 4f 4d 45 4d 20 69 73 20 72 65 74 ITE_NOMEM is ret
141d9 75 72 6e 65 64 2e 20 49 66 20 61 6e 20 65 72 72 urned. If an err
141da 6f 72 20 0a 2a 2a 20 6f 63 63 75 72 73 20 77 68 or .** occurs wh
141db 69 6c 65 20 6f 70 65 6e 69 6e 67 20 74 68 65 20 ile opening the
141dc 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 sub-journal file
141dd 2c 20 74 68 65 6e 20 61 6e 20 49 4f 20 65 72 72 , then an IO err
141de 6f 72 20 63 6f 64 65 20 69 73 0a 2a 2a 20 72 65 or code is.** re
141df 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 turned. Otherwis
141e0 65 2c 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f e, SQLITE_OK..*/
141e1 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
141e2 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 int sqlite3Pager
141e3 4f 70 65 6e 53 61 76 65 70 6f 69 6e 74 28 50 61 OpenSavepoint(Pa
141e4 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 ger *pPager, int
141e5 20 6e 53 61 76 65 70 6f 69 6e 74 29 7b 0a 20 20 nSavepoint){.
141e6 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
141e7 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 OK;
141e8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 /* Ret
141e9 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 69 6e urn code */. in
141ea 74 20 6e 43 75 72 72 65 6e 74 20 3d 20 70 50 61 t nCurrent = pPa
141eb 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3b ger->nSavepoint;
141ec 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 /* Curre
141ed 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20 73 61 76 nt number of sav
141ee 65 70 6f 69 6e 74 73 20 2a 2f 0a 0a 20 20 69 66 epoints */.. if
141ef 28 20 6e 53 61 76 65 70 6f 69 6e 74 3e 6e 43 75 ( nSavepoint>nCu
141f0 72 72 65 6e 74 20 26 26 20 70 50 61 67 65 72 2d rrent && pPager-
141f1 3e 75 73 65 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 >useJournal ){.
141f2 20 20 20 69 6e 74 20 69 69 3b 20 20 20 20 20 20 int ii;
141f3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
141f4 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74 /* It
141f5 65 72 61 74 6f 72 20 76 61 72 69 61 62 6c 65 20 erator variable
141f6 2a 2f 0a 20 20 20 20 50 61 67 65 72 53 61 76 65 */. PagerSave
141f7 70 6f 69 6e 74 20 2a 61 4e 65 77 3b 20 20 20 20 point *aNew;
141f8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
141f9 2a 20 4e 65 77 20 50 61 67 65 72 2e 61 53 61 76 * New Pager.aSav
141fa 65 70 6f 69 6e 74 20 61 72 72 61 79 20 2a 2f 0a epoint array */.
141fb 0a 20 20 20 20 2f 2a 20 45 69 74 68 65 72 20 74 . /* Either t
141fc 68 65 72 65 20 69 73 20 6e 6f 20 61 63 74 69 76 here is no activ
141fd 65 20 6a 6f 75 72 6e 61 6c 20 6f 72 20 74 68 65 e journal or the
141fe 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 69 73 20 sub-journal is
141ff 6f 70 65 6e 20 6f 72 20 0a 20 20 20 20 2a 2a 20 open or . **
14200 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 61 the journal is a
14201 6c 77 61 79 73 20 73 74 6f 72 65 64 20 69 6e 20 lways stored in
14202 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 20 20 61 73 memory */. as
14203 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6e 53 sert( pPager->nS
14204 61 76 65 70 6f 69 6e 74 3d 3d 30 20 7c 7c 20 69 avepoint==0 || i
14205 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 73 6a sOpen(pPager->sj
14206 66 64 29 20 7c 7c 0a 20 20 20 20 20 20 20 20 20 fd) ||.
14207 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e pPager->journ
14208 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f alMode==PAGER_JO
14209 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 URNALMODE_MEMORY
1420a 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 47 72 6f 77 );.. /* Grow
1420b 20 74 68 65 20 50 61 67 65 72 2e 61 53 61 76 65 the Pager.aSave
1420c 70 6f 69 6e 74 20 61 72 72 61 79 20 75 73 69 6e point array usin
1420d 67 20 72 65 61 6c 6c 6f 63 28 29 2e 20 52 65 74 g realloc(). Ret
1420e 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d urn SQLITE_NOMEM
1420f 0a 20 20 20 20 2a 2a 20 69 66 20 74 68 65 20 61 . ** if the a
14210 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2e llocation fails.
14211 20 4f 74 68 65 72 77 69 73 65 2c 20 7a 65 72 6f Otherwise, zero
14212 20 74 68 65 20 6e 65 77 20 70 6f 72 74 69 6f 6e the new portion
14213 20 69 6e 20 63 61 73 65 20 61 20 0a 20 20 20 20 in case a .
14214 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 ** malloc failur
14215 65 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 70 e occurs while p
14216 6f 70 75 6c 61 74 69 6e 67 20 69 74 20 69 6e 20 opulating it in
14217 74 68 65 20 66 6f 72 28 2e 2e 2e 29 20 6c 6f 6f the for(...) loo
14218 70 20 62 65 6c 6f 77 2e 0a 20 20 20 20 2a 2f 0a p below.. */.
14219 20 20 20 20 61 4e 65 77 20 3d 20 28 50 61 67 65 aNew = (Page
1421a 72 53 61 76 65 70 6f 69 6e 74 20 2a 29 73 71 6c rSavepoint *)sql
1421b 69 74 65 33 52 65 61 6c 6c 6f 63 28 0a 20 20 20 ite3Realloc(.
1421c 20 20 20 20 20 70 50 61 67 65 72 2d 3e 61 53 61 pPager->aSa
1421d 76 65 70 6f 69 6e 74 2c 20 73 69 7a 65 6f 66 28 vepoint, sizeof(
1421e 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 29 2a PagerSavepoint)*
1421f 6e 53 61 76 65 70 6f 69 6e 74 0a 20 20 20 20 29 nSavepoint. )
14220 3b 0a 20 20 20 20 69 66 28 20 21 61 4e 65 77 20 ;. if( !aNew
14221 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
14222 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
14223 20 20 7d 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 }. memset(&
14224 61 4e 65 77 5b 6e 43 75 72 72 65 6e 74 5d 2c 20 aNew[nCurrent],
14225 30 2c 20 28 6e 53 61 76 65 70 6f 69 6e 74 2d 6e 0, (nSavepoint-n
14226 43 75 72 72 65 6e 74 29 20 2a 20 73 69 7a 65 6f Current) * sizeo
14227 66 28 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 f(PagerSavepoint
14228 29 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e ));. pPager->
14229 61 53 61 76 65 70 6f 69 6e 74 20 3d 20 61 4e 65 aSavepoint = aNe
1422a 77 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6e w;. pPager->n
1422b 53 61 76 65 70 6f 69 6e 74 20 3d 20 6e 53 61 76 Savepoint = nSav
1422c 65 70 6f 69 6e 74 3b 0a 0a 20 20 20 20 2f 2a 20 epoint;.. /*
1422d 50 6f 70 75 6c 61 74 65 20 74 68 65 20 50 61 67 Populate the Pag
1422e 65 72 53 61 76 65 70 6f 69 6e 74 20 73 74 72 75 erSavepoint stru
1422f 63 74 75 72 65 73 20 6a 75 73 74 20 61 6c 6c 6f ctures just allo
14230 63 61 74 65 64 2e 20 2a 2f 0a 20 20 20 20 66 6f cated. */. fo
14231 72 28 69 69 3d 6e 43 75 72 72 65 6e 74 3b 20 69 r(ii=nCurrent; i
14232 69 3c 6e 53 61 76 65 70 6f 69 6e 74 3b 20 69 69 i<nSavepoint; ii
14233 2b 2b 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 ++){. asser
14234 74 28 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a t( pPager->dbSiz
14235 65 56 61 6c 69 64 20 29 3b 0a 20 20 20 20 20 20 eValid );.
14236 61 4e 65 77 5b 69 69 5d 2e 6e 4f 72 69 67 20 3d aNew[ii].nOrig =
14237 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3b pPager->dbSize;
14238 0a 20 20 20 20 20 20 69 66 28 20 69 73 4f 70 65 . if( isOpe
14239 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 26 n(pPager->jfd) &
1423a 26 20 41 4c 57 41 59 53 28 70 50 61 67 65 72 2d & ALWAYS(pPager-
1423b 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3e 30 29 20 29 >journalOff>0) )
1423c 7b 0a 20 20 20 20 20 20 20 20 61 4e 65 77 5b 69 {. aNew[i
1423d 69 5d 2e 69 4f 66 66 73 65 74 20 3d 20 70 50 61 i].iOffset = pPa
1423e 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b ger->journalOff;
1423f 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
14240 20 20 20 20 20 20 61 4e 65 77 5b 69 69 5d 2e 69 aNew[ii].i
14241 4f 66 66 73 65 74 20 3d 20 4a 4f 55 52 4e 41 4c Offset = JOURNAL
14242 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 3b _HDR_SZ(pPager);
14243 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 . }. a
14244 4e 65 77 5b 69 69 5d 2e 69 53 75 62 52 65 63 20 New[ii].iSubRec
14245 3d 20 70 50 61 67 65 72 2d 3e 6e 53 75 62 52 65 = pPager->nSubRe
14246 63 3b 0a 20 20 20 20 20 20 61 4e 65 77 5b 69 69 c;. aNew[ii
14247 5d 2e 70 49 6e 53 61 76 65 70 6f 69 6e 74 20 3d ].pInSavepoint =
14248 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 43 72 sqlite3BitvecCr
14249 65 61 74 65 28 70 50 61 67 65 72 2d 3e 64 62 53 eate(pPager->dbS
1424a 69 7a 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 ize);. if(
1424b 21 61 4e 65 77 5b 69 69 5d 2e 70 49 6e 53 61 76 !aNew[ii].pInSav
1424c 65 70 6f 69 6e 74 20 29 7b 0a 20 20 20 20 20 20 epoint ){.
1424d 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
1424e 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 NOMEM;. }.
1424f 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 4f 70 65 }.. /* Ope
14250 6e 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 n the sub-journa
14251 6c 2c 20 69 66 20 69 74 20 69 73 20 6e 6f 74 20 l, if it is not
14252 61 6c 72 65 61 64 79 20 6f 70 65 6e 65 64 2e 20 already opened.
14253 2a 2f 0a 20 20 20 20 72 63 20 3d 20 6f 70 65 6e */. rc = open
14254 53 75 62 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 SubJournal(pPage
14255 72 29 3b 0a 20 20 20 20 61 73 73 65 72 74 54 72 r);. assertTr
14256 75 6e 63 61 74 65 43 6f 6e 73 74 72 61 69 6e 74 uncateConstraint
14257 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a 0a 20 (pPager);. }..
14258 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
14259 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 *.** This functi
1425a 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 on is called to
1425b 72 6f 6c 6c 62 61 63 6b 20 6f 72 20 72 65 6c 65 rollback or rele
1425c 61 73 65 20 28 63 6f 6d 6d 69 74 29 20 61 20 73 ase (commit) a s
1425d 61 76 65 70 6f 69 6e 74 2e 0a 2a 2a 20 54 68 65 avepoint..** The
1425e 20 73 61 76 65 70 6f 69 6e 74 20 74 6f 20 72 65 savepoint to re
1425f 6c 65 61 73 65 20 6f 72 20 72 6f 6c 6c 62 61 63 lease or rollbac
14260 6b 20 6e 65 65 64 20 6e 6f 74 20 62 65 20 74 68 k need not be th
14261 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 e most recently
14262 0a 2a 2a 20 63 72 65 61 74 65 64 20 73 61 76 65 .** created save
14263 70 6f 69 6e 74 2e 0a 2a 2a 0a 2a 2a 20 50 61 72 point..**.** Par
14264 61 6d 65 74 65 72 20 6f 70 20 69 73 20 61 6c 77 ameter op is alw
14265 61 79 73 20 65 69 74 68 65 72 20 53 41 56 45 50 ays either SAVEP
14266 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 6f 72 OINT_ROLLBACK or
14267 20 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 SAVEPOINT_RELEA
14268 53 45 2e 0a 2a 2a 20 49 66 20 69 74 20 69 73 20 SE..** If it is
14269 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 SAVEPOINT_RELEAS
1426a 45 2c 20 74 68 65 6e 20 72 65 6c 65 61 73 65 20 E, then release
1426b 61 6e 64 20 64 65 73 74 72 6f 79 20 74 68 65 20 and destroy the
1426c 73 61 76 65 70 6f 69 6e 74 20 77 69 74 68 0a 2a savepoint with.*
1426d 2a 20 69 6e 64 65 78 20 69 53 61 76 65 70 6f 69 * index iSavepoi
1426e 6e 74 2e 20 49 66 20 69 74 20 69 73 20 53 41 56 nt. If it is SAV
1426f 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 2c EPOINT_ROLLBACK,
14270 20 74 68 65 6e 20 72 6f 6c 6c 62 61 63 6b 20 61 then rollback a
14271 6c 6c 20 63 68 61 6e 67 65 73 0a 2a 2a 20 74 68 ll changes.** th
14272 61 74 20 68 61 76 65 20 6f 63 63 75 72 72 65 64 at have occurred
14273 20 73 69 6e 63 65 20 74 68 65 20 73 70 65 63 69 since the speci
14274 66 69 65 64 20 73 61 76 65 70 6f 69 6e 74 20 77 fied savepoint w
14275 61 73 20 63 72 65 61 74 65 64 2e 0a 2a 2a 0a 2a as created..**.*
14276 2a 20 54 68 65 20 73 61 76 65 70 6f 69 6e 74 20 * The savepoint
14277 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 6f 72 20 72 to rollback or r
14278 65 6c 65 61 73 65 20 69 73 20 69 64 65 6e 74 69 elease is identi
14279 66 69 65 64 20 62 79 20 70 61 72 61 6d 65 74 65 fied by paramete
1427a 72 20 0a 2a 2a 20 69 53 61 76 65 70 6f 69 6e 74 r .** iSavepoint
1427b 2e 20 41 20 76 61 6c 75 65 20 6f 66 20 30 20 6d . A value of 0 m
1427c 65 61 6e 73 20 74 6f 20 6f 70 65 72 61 74 65 20 eans to operate
1427d 6f 6e 20 74 68 65 20 6f 75 74 65 72 6d 6f 73 74 on the outermost
1427e 20 73 61 76 65 70 6f 69 6e 74 0a 2a 2a 20 28 74 savepoint.** (t
1427f 68 65 20 66 69 72 73 74 20 63 72 65 61 74 65 64 he first created
14280 29 2e 20 41 20 76 61 6c 75 65 20 6f 66 20 28 50 ). A value of (P
14281 61 67 65 72 2e 6e 53 61 76 65 70 6f 69 6e 74 2d ager.nSavepoint-
14282 31 29 20 6d 65 61 6e 73 20 6f 70 65 72 61 74 65 1) means operate
14283 0a 2a 2a 20 6f 6e 20 74 68 65 20 6d 6f 73 74 20 .** on the most
14284 72 65 63 65 6e 74 6c 79 20 63 72 65 61 74 65 64 recently created
14285 20 73 61 76 65 70 6f 69 6e 74 2e 20 49 66 20 69 savepoint. If i
14286 53 61 76 65 70 6f 69 6e 74 20 69 73 20 67 72 65 Savepoint is gre
14287 61 74 65 72 20 74 68 61 6e 0a 2a 2a 20 28 50 61 ater than.** (Pa
14288 67 65 72 2e 6e 53 61 76 65 70 6f 69 6e 74 2d 31 ger.nSavepoint-1
14289 29 2c 20 74 68 65 6e 20 74 68 69 73 20 66 75 6e ), then this fun
1428a 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 ction is a no-op
1428b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 6e 65 67 ..**.** If a neg
1428c 61 74 69 76 65 20 76 61 6c 75 65 20 69 73 20 70 ative value is p
1428d 61 73 73 65 64 20 74 6f 20 74 68 69 73 20 66 75 assed to this fu
1428e 6e 63 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 nction, then the
1428f 20 63 75 72 72 65 6e 74 0a 2a 2a 20 74 72 61 6e current.** tran
14290 73 61 63 74 69 6f 6e 20 69 73 20 72 6f 6c 6c 65 saction is rolle
14291 64 20 62 61 63 6b 2e 20 54 68 69 73 20 69 73 20 d back. This is
14292 64 69 66 66 65 72 65 6e 74 20 74 6f 20 63 61 6c different to cal
14293 6c 69 6e 67 20 0a 2a 2a 20 73 71 6c 69 74 65 33 ling .** sqlite3
14294 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 29 20 PagerRollback()
14295 62 65 63 61 75 73 65 20 74 68 69 73 20 66 75 6e because this fun
14296 63 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 74 ction does not t
14297 65 72 6d 69 6e 61 74 65 0a 2a 2a 20 74 68 65 20 erminate.** the
14298 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 72 20 75 transaction or u
14299 6e 6c 6f 63 6b 20 74 68 65 20 64 61 74 61 62 61 nlock the databa
1429a 73 65 2c 20 69 74 20 6a 75 73 74 20 72 65 73 74 se, it just rest
1429b 6f 72 65 73 20 74 68 65 20 0a 2a 2a 20 63 6f 6e ores the .** con
1429c 74 65 6e 74 73 20 6f 66 20 74 68 65 20 64 61 74 tents of the dat
1429d 61 62 61 73 65 20 74 6f 20 69 74 73 20 6f 72 69 abase to its ori
1429e 67 69 6e 61 6c 20 73 74 61 74 65 2e 20 0a 2a 2a ginal state. .**
1429f 0a 2a 2a 20 49 6e 20 61 6e 79 20 63 61 73 65 2c .** In any case,
142a0 20 61 6c 6c 20 73 61 76 65 70 6f 69 6e 74 73 20 all savepoints
142a1 77 69 74 68 20 61 6e 20 69 6e 64 65 78 20 67 72 with an index gr
142a2 65 61 74 65 72 20 74 68 61 6e 20 69 53 61 76 65 eater than iSave
142a3 70 6f 69 6e 74 20 0a 2a 2a 20 61 72 65 20 64 65 point .** are de
142a4 73 74 72 6f 79 65 64 2e 20 49 66 20 74 68 69 73 stroyed. If this
142a5 20 69 73 20 61 20 72 65 6c 65 61 73 65 20 6f 70 is a release op
142a6 65 72 61 74 69 6f 6e 20 28 6f 70 3d 3d 53 41 56 eration (op==SAV
142a7 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 29 2c EPOINT_RELEASE),
142a8 0a 2a 2a 20 74 68 65 6e 20 73 61 76 65 70 6f 69 .** then savepoi
142a9 6e 74 20 69 53 61 76 65 70 6f 69 6e 74 20 69 73 nt iSavepoint is
142aa 20 61 6c 73 6f 20 64 65 73 74 72 6f 79 65 64 2e also destroyed.
142ab 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 .**.** This func
142ac 74 69 6f 6e 20 6d 61 79 20 72 65 74 75 72 6e 20 tion may return
142ad 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 66 20 SQLITE_NOMEM if
142ae 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 a memory allocat
142af 69 6f 6e 20 66 61 69 6c 73 2c 0a 2a 2a 20 6f 72 ion fails,.** or
142b0 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 an IO error cod
142b1 65 20 69 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 e if an IO error
142b2 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 72 6f occurs while ro
142b3 6c 6c 69 6e 67 20 62 61 63 6b 20 61 20 0a 2a 2a lling back a .**
142b4 20 73 61 76 65 70 6f 69 6e 74 2e 20 49 66 20 6e savepoint. If n
142b5 6f 20 65 72 72 6f 72 73 20 6f 63 63 75 72 2c 20 o errors occur,
142b6 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 SQLITE_OK is ret
142b7 75 72 6e 65 64 2e 0a 2a 2f 20 0a 53 51 4c 49 54 urned..*/ .SQLIT
142b8 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
142b9 6c 69 74 65 33 50 61 67 65 72 53 61 76 65 70 6f lite3PagerSavepo
142ba 69 6e 74 28 50 61 67 65 72 20 2a 70 50 61 67 65 int(Pager *pPage
142bb 72 2c 20 69 6e 74 20 6f 70 2c 20 69 6e 74 20 69 r, int op, int i
142bc 53 61 76 65 70 6f 69 6e 74 29 7b 0a 20 20 69 6e Savepoint){. in
142bd 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
142be 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 6f 70 3d ;.. assert( op=
142bf 3d 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 =SAVEPOINT_RELEA
142c0 53 45 20 7c 7c 20 6f 70 3d 3d 53 41 56 45 50 4f SE || op==SAVEPO
142c1 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 29 3b 0a INT_ROLLBACK );.
142c2 20 20 61 73 73 65 72 74 28 20 69 53 61 76 65 70 assert( iSavep
142c3 6f 69 6e 74 3e 3d 30 20 7c 7c 20 6f 70 3d 3d 53 oint>=0 || op==S
142c4 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 AVEPOINT_ROLLBAC
142c5 4b 20 29 3b 0a 0a 20 20 69 66 28 20 69 53 61 76 K );.. if( iSav
142c6 65 70 6f 69 6e 74 3c 70 50 61 67 65 72 2d 3e 6e epoint<pPager->n
142c7 53 61 76 65 70 6f 69 6e 74 20 29 7b 0a 20 20 20 Savepoint ){.
142c8 20 69 6e 74 20 69 69 3b 20 20 20 20 20 20 20 20 int ii;
142c9 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 /* Iterator
142ca 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20 20 20 variable */.
142cb 69 6e 74 20 6e 4e 65 77 3b 20 20 20 20 20 20 20 int nNew;
142cc 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
142cd 72 65 6d 61 69 6e 69 6e 67 20 73 61 76 65 70 6f remaining savepo
142ce 69 6e 74 73 20 61 66 74 65 72 20 74 68 69 73 20 ints after this
142cf 6f 70 2e 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 46 op. */.. /* F
142d0 69 67 75 72 65 20 6f 75 74 20 68 6f 77 20 6d 61 igure out how ma
142d1 6e 79 20 73 61 76 65 70 6f 69 6e 74 73 20 77 69 ny savepoints wi
142d2 6c 6c 20 73 74 69 6c 6c 20 62 65 20 61 63 74 69 ll still be acti
142d3 76 65 20 61 66 74 65 72 20 74 68 69 73 0a 20 20 ve after this.
142d4 20 20 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 2e 20 ** operation.
142d5 53 74 6f 72 65 20 74 68 69 73 20 76 61 6c 75 65 Store this value
142d6 20 69 6e 20 6e 4e 65 77 2e 20 54 68 65 6e 20 66 in nNew. Then f
142d7 72 65 65 20 72 65 73 6f 75 72 63 65 73 20 61 73 ree resources as
142d8 73 6f 63 69 61 74 65 64 20 0a 20 20 20 20 2a 2a sociated . **
142d9 20 77 69 74 68 20 61 6e 79 20 73 61 76 65 70 6f with any savepo
142da 69 6e 74 73 20 74 68 61 74 20 61 72 65 20 64 65 ints that are de
142db 73 74 72 6f 79 65 64 20 62 79 20 74 68 69 73 20 stroyed by this
142dc 6f 70 65 72 61 74 69 6f 6e 2e 0a 20 20 20 20 2a operation.. *
142dd 2f 0a 20 20 20 20 6e 4e 65 77 20 3d 20 69 53 61 /. nNew = iSa
142de 76 65 70 6f 69 6e 74 20 2b 20 28 6f 70 3d 3d 53 vepoint + (op==S
142df 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 AVEPOINT_ROLLBAC
142e0 4b 29 3b 0a 20 20 20 20 66 6f 72 28 69 69 3d 6e K);. for(ii=n
142e1 4e 65 77 3b 20 69 69 3c 70 50 61 67 65 72 2d 3e New; ii<pPager->
142e2 6e 53 61 76 65 70 6f 69 6e 74 3b 20 69 69 2b 2b nSavepoint; ii++
142e3 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
142e4 42 69 74 76 65 63 44 65 73 74 72 6f 79 28 70 50 BitvecDestroy(pP
142e5 61 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 ager->aSavepoint
142e6 5b 69 69 5d 2e 70 49 6e 53 61 76 65 70 6f 69 6e [ii].pInSavepoin
142e7 74 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 t);. }. pP
142e8 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 ager->nSavepoint
142e9 20 3d 20 6e 4e 65 77 3b 0a 0a 20 20 20 20 2f 2a = nNew;.. /*
142ea 20 49 66 20 74 68 69 73 20 69 73 20 61 20 72 6f If this is a ro
142eb 6c 6c 62 61 63 6b 20 6f 70 65 72 61 74 69 6f 6e llback operation
142ec 2c 20 70 6c 61 79 62 61 63 6b 20 74 68 65 20 73 , playback the s
142ed 70 65 63 69 66 69 65 64 20 73 61 76 65 70 6f 69 pecified savepoi
142ee 6e 74 2e 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 nt.. ** If th
142ef 69 73 20 69 73 20 61 20 74 65 6d 70 2d 66 69 6c is is a temp-fil
142f0 65 2c 20 69 74 20 69 73 20 70 6f 73 73 69 62 6c e, it is possibl
142f1 65 20 74 68 61 74 20 74 68 65 20 6a 6f 75 72 6e e that the journ
142f2 61 6c 20 66 69 6c 65 20 68 61 73 0a 20 20 20 20 al file has.
142f3 2a 2a 20 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 ** not yet been
142f4 6f 70 65 6e 65 64 2e 20 49 6e 20 74 68 69 73 20 opened. In this
142f5 63 61 73 65 20 74 68 65 72 65 20 68 61 76 65 20 case there have
142f6 62 65 65 6e 20 6e 6f 20 63 68 61 6e 67 65 73 20 been no changes
142f7 74 6f 0a 20 20 20 20 2a 2a 20 74 68 65 20 64 61 to. ** the da
142f8 74 61 62 61 73 65 20 66 69 6c 65 2c 20 73 6f 20 tabase file, so
142f9 74 68 65 20 70 6c 61 79 62 61 63 6b 20 6f 70 65 the playback ope
142fa 72 61 74 69 6f 6e 20 63 61 6e 20 62 65 20 73 6b ration can be sk
142fb 69 70 70 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 ipped.. */.
142fc 20 20 69 66 28 20 6f 70 3d 3d 53 41 56 45 50 4f if( op==SAVEPO
142fd 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 26 26 20 INT_ROLLBACK &&
142fe 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a isOpen(pPager->j
142ff 66 64 29 20 29 7b 0a 20 20 20 20 20 20 50 61 67 fd) ){. Pag
14300 65 72 53 61 76 65 70 6f 69 6e 74 20 2a 70 53 61 erSavepoint *pSa
14301 76 65 70 6f 69 6e 74 20 3d 20 28 6e 4e 65 77 3d vepoint = (nNew=
14302 3d 30 29 3f 30 3a 26 70 50 61 67 65 72 2d 3e 61 =0)?0:&pPager->a
14303 53 61 76 65 70 6f 69 6e 74 5b 6e 4e 65 77 2d 31 Savepoint[nNew-1
14304 5d 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 ];. rc = pa
14305 67 65 72 50 6c 61 79 62 61 63 6b 53 61 76 65 70 gerPlaybackSavep
14306 6f 69 6e 74 28 70 50 61 67 65 72 2c 20 70 53 61 oint(pPager, pSa
14307 76 65 70 6f 69 6e 74 29 3b 0a 20 20 20 20 20 20 vepoint);.
14308 61 73 73 65 72 74 28 72 63 21 3d 53 51 4c 49 54 assert(rc!=SQLIT
14309 45 5f 44 4f 4e 45 29 3b 0a 20 20 20 20 7d 0a 20 E_DONE);. }.
1430a 20 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 . /* If this
1430b 20 69 73 20 61 20 72 65 6c 65 61 73 65 20 6f 66 is a release of
1430c 20 74 68 65 20 6f 75 74 65 72 6d 6f 73 74 20 73 the outermost s
1430d 61 76 65 70 6f 69 6e 74 2c 20 74 72 75 6e 63 61 avepoint, trunca
1430e 74 65 20 0a 20 20 20 20 2a 2a 20 74 68 65 20 73 te . ** the s
1430f 75 62 2d 6a 6f 75 72 6e 61 6c 20 74 6f 20 7a 65 ub-journal to ze
14310 72 6f 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 ro bytes in size
14311 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 6e 4e 65 . */. if( nNe
14312 77 3d 3d 30 20 26 26 20 6f 70 3d 3d 53 41 56 45 w==0 && op==SAVE
14313 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 20 26 26 POINT_RELEASE &&
14314 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e isOpen(pPager->
14315 73 6a 66 64 29 20 29 7b 0a 20 20 20 20 20 20 61 sjfd) ){. a
14316 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 ssert( rc==SQLIT
14317 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 20 20 72 63 E_OK );. rc
14318 20 3d 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e = sqlite3OsTrun
14319 63 61 74 65 28 70 50 61 67 65 72 2d 3e 73 6a 66 cate(pPager->sjf
1431a 64 2c 20 30 29 3b 0a 20 20 20 20 20 20 70 50 61 d, 0);. pPa
1431b 67 65 72 2d 3e 6e 53 75 62 52 65 63 20 3d 20 30 ger->nSubRec = 0
1431c 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 ;. }. }. re
1431d 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
1431e 2a 20 52 65 74 75 72 6e 20 74 68 65 20 66 75 6c * Return the ful
1431f 6c 20 70 61 74 68 6e 61 6d 65 20 6f 66 20 74 68 l pathname of th
14320 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e e database file.
14321 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
14322 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 TE const char *s
14323 71 6c 69 74 65 33 50 61 67 65 72 46 69 6c 65 6e qlite3PagerFilen
14324 61 6d 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 ame(Pager *pPage
14325 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 r){. return pPa
14326 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 3b 0a ger->zFilename;.
14327 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
14328 74 68 65 20 56 46 53 20 73 74 72 75 63 74 75 72 the VFS structur
14329 65 20 66 6f 72 20 74 68 65 20 70 61 67 65 72 2e e for the pager.
1432a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1432b 54 45 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 TE const sqlite3
1432c 5f 76 66 73 20 2a 73 71 6c 69 74 65 33 50 61 67 _vfs *sqlite3Pag
1432d 65 72 56 66 73 28 50 61 67 65 72 20 2a 70 50 61 erVfs(Pager *pPa
1432e 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 ger){. return p
1432f 50 61 67 65 72 2d 3e 70 56 66 73 3b 0a 7d 0a 0a Pager->pVfs;.}..
14330 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 /*.** Return the
14331 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 66 6f 72 file handle for
14332 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
14333 6c 65 20 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a le associated.**
14334 20 77 69 74 68 20 74 68 65 20 70 61 67 65 72 2e with the pager.
14335 20 20 54 68 69 73 20 6d 69 67 68 74 20 72 65 74 This might ret
14336 75 72 6e 20 4e 55 4c 4c 20 69 66 20 74 68 65 20 urn NULL if the
14337 66 69 6c 65 20 68 61 73 0a 2a 2a 20 6e 6f 74 20 file has.** not
14338 79 65 74 20 62 65 65 6e 20 6f 70 65 6e 65 64 2e yet been opened.
14339 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1433a 54 45 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 TE sqlite3_file
1433b 2a 73 71 6c 69 74 65 33 50 61 67 65 72 46 69 6c *sqlite3PagerFil
1433c 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 e(Pager *pPager)
1433d 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 {. return pPage
1433e 72 2d 3e 66 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 r->fd;.}../*.**
1433f 52 65 74 75 72 6e 20 74 68 65 20 66 75 6c 6c 20 Return the full
14340 70 61 74 68 6e 61 6d 65 20 6f 66 20 74 68 65 20 pathname of the
14341 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 2a 2f journal file..*/
14342 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
14343 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 const char *sqli
14344 74 65 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c 6e te3PagerJournaln
14345 61 6d 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 ame(Pager *pPage
14346 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 r){. return pPa
14347 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 3b 0a 7d ger->zJournal;.}
14348 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ../*.** Return t
14349 72 75 65 20 69 66 20 66 73 79 6e 63 28 29 20 63 rue if fsync() c
1434a 61 6c 6c 73 20 61 72 65 20 64 69 73 61 62 6c 65 alls are disable
1434b 64 20 66 6f 72 20 74 68 69 73 20 70 61 67 65 72 d for this pager
1434c 2e 20 20 52 65 74 75 72 6e 20 46 41 4c 53 45 0a . Return FALSE.
1434d 2a 2a 20 69 66 20 66 73 79 6e 63 28 29 73 20 61 ** if fsync()s a
1434e 72 65 20 65 78 65 63 75 74 65 64 20 6e 6f 72 6d re executed norm
1434f 61 6c 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ally..*/.SQLITE_
14350 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
14351 74 65 33 50 61 67 65 72 4e 6f 73 79 6e 63 28 50 te3PagerNosync(P
14352 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 ager *pPager){.
14353 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e return pPager->
14354 6e 6f 53 79 6e 63 3b 0a 7d 0a 0a 23 69 66 64 65 noSync;.}..#ifde
14355 66 20 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 f SQLITE_HAS_COD
14356 45 43 0a 2f 2a 0a 2a 2a 20 53 65 74 20 6f 72 20 EC./*.** Set or
14357 72 65 74 72 69 65 76 65 20 74 68 65 20 63 6f 64 retrieve the cod
14358 65 63 20 66 6f 72 20 74 68 69 73 20 70 61 67 65 ec for this page
14359 72 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 r.*/.static void
1435a 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 sqlite3PagerSet
1435b 43 6f 64 65 63 28 0a 20 20 50 61 67 65 72 20 2a Codec(. Pager *
1435c 70 50 61 67 65 72 2c 0a 20 20 76 6f 69 64 20 2a pPager,. void *
1435d 28 2a 78 43 6f 64 65 63 29 28 76 6f 69 64 2a 2c (*xCodec)(void*,
1435e 76 6f 69 64 2a 2c 50 67 6e 6f 2c 69 6e 74 29 2c void*,Pgno,int),
1435f 0a 20 20 76 6f 69 64 20 28 2a 78 43 6f 64 65 63 . void (*xCodec
14360 53 69 7a 65 43 68 6e 67 29 28 76 6f 69 64 2a 2c SizeChng)(void*,
14361 69 6e 74 2c 69 6e 74 29 2c 0a 20 20 76 6f 69 64 int,int),. void
14362 20 28 2a 78 43 6f 64 65 63 46 72 65 65 29 28 76 (*xCodecFree)(v
14363 6f 69 64 2a 29 2c 0a 20 20 76 6f 69 64 20 2a 70 oid*),. void *p
14364 43 6f 64 65 63 0a 29 7b 0a 20 20 69 66 28 20 70 Codec.){. if( p
14365 50 61 67 65 72 2d 3e 78 43 6f 64 65 63 46 72 65 Pager->xCodecFre
14366 65 20 29 20 70 50 61 67 65 72 2d 3e 78 43 6f 64 e ) pPager->xCod
14367 65 63 46 72 65 65 28 70 50 61 67 65 72 2d 3e 70 ecFree(pPager->p
14368 43 6f 64 65 63 29 3b 0a 20 20 70 50 61 67 65 72 Codec);. pPager
14369 2d 3e 78 43 6f 64 65 63 20 3d 20 78 43 6f 64 65 ->xCodec = xCode
1436a 63 3b 0a 20 20 70 50 61 67 65 72 2d 3e 78 43 6f c;. pPager->xCo
1436b 64 65 63 53 69 7a 65 43 68 6e 67 20 3d 20 78 43 decSizeChng = xC
1436c 6f 64 65 63 53 69 7a 65 43 68 6e 67 3b 0a 20 20 odecSizeChng;.
1436d 70 50 61 67 65 72 2d 3e 78 43 6f 64 65 63 46 72 pPager->xCodecFr
1436e 65 65 20 3d 20 78 43 6f 64 65 63 46 72 65 65 3b ee = xCodecFree;
1436f 0a 20 20 70 50 61 67 65 72 2d 3e 70 43 6f 64 65 . pPager->pCode
14370 63 20 3d 20 70 43 6f 64 65 63 3b 0a 20 20 70 61 c = pCodec;. pa
14371 67 65 72 52 65 70 6f 72 74 53 69 7a 65 28 70 50 gerReportSize(pP
14372 61 67 65 72 29 3b 0a 7d 0a 73 74 61 74 69 63 20 ager);.}.static
14373 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61 67 void *sqlite3Pag
14374 65 72 47 65 74 43 6f 64 65 63 28 50 61 67 65 72 erGetCodec(Pager
14375 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 65 74 *pPager){. ret
14376 75 72 6e 20 70 50 61 67 65 72 2d 3e 70 43 6f 64 urn pPager->pCod
14377 65 63 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 ec;.}.#endif..#i
14378 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
14379 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 2f 2a 0a T_AUTOVACUUM./*.
1437a 2a 2a 20 4d 6f 76 65 20 74 68 65 20 70 61 67 65 ** Move the page
1437b 20 70 50 67 20 74 6f 20 6c 6f 63 61 74 69 6f 6e pPg to location
1437c 20 70 67 6e 6f 20 69 6e 20 74 68 65 20 66 69 6c pgno in the fil
1437d 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 6d e..**.** There m
1437e 75 73 74 20 62 65 20 6e 6f 20 72 65 66 65 72 65 ust be no refere
1437f 6e 63 65 73 20 74 6f 20 74 68 65 20 70 61 67 65 nces to the page
14380 20 70 72 65 76 69 6f 75 73 6c 79 20 6c 6f 63 61 previously loca
14381 74 65 64 20 61 74 0a 2a 2a 20 70 67 6e 6f 20 28 ted at.** pgno (
14382 77 68 69 63 68 20 77 65 20 63 61 6c 6c 20 70 50 which we call pP
14383 67 4f 6c 64 29 20 74 68 6f 75 67 68 20 74 68 61 gOld) though tha
14384 74 20 70 61 67 65 20 69 73 20 61 6c 6c 6f 77 65 t page is allowe
14385 64 20 74 6f 20 62 65 0a 2a 2a 20 69 6e 20 63 61 d to be.** in ca
14386 63 68 65 2e 20 20 49 66 20 74 68 65 20 70 61 67 che. If the pag
14387 65 20 70 72 65 76 69 6f 75 73 6c 79 20 6c 6f 63 e previously loc
14388 61 74 65 64 20 61 74 20 70 67 6e 6f 20 69 73 20 ated at pgno is
14389 6e 6f 74 20 61 6c 72 65 61 64 79 0a 2a 2a 20 69 not already.** i
1438a 6e 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a n the rollback j
1438b 6f 75 72 6e 61 6c 2c 20 69 74 20 69 73 20 6e 6f ournal, it is no
1438c 74 20 70 75 74 20 74 68 65 72 65 20 62 79 20 62 t put there by b
1438d 79 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a y this routine..
1438e 2a 2a 0a 2a 2a 20 52 65 66 65 72 65 6e 63 65 73 **.** References
1438f 20 74 6f 20 74 68 65 20 70 61 67 65 20 70 50 67 to the page pPg
14390 20 72 65 6d 61 69 6e 20 76 61 6c 69 64 2e 20 55 remain valid. U
14391 70 64 61 74 69 6e 67 20 61 6e 79 0a 2a 2a 20 6d pdating any.** m
14392 65 74 61 2d 64 61 74 61 20 61 73 73 6f 63 69 61 eta-data associa
14393 74 65 64 20 77 69 74 68 20 70 50 67 20 28 69 2e ted with pPg (i.
14394 65 2e 20 64 61 74 61 20 73 74 6f 72 65 64 20 69 e. data stored i
14395 6e 20 74 68 65 20 6e 45 78 74 72 61 20 62 79 74 n the nExtra byt
14396 65 73 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 es.** allocated
14397 61 6c 6f 6e 67 20 77 69 74 68 20 74 68 65 20 70 along with the p
14398 61 67 65 29 20 69 73 20 74 68 65 20 72 65 73 70 age) is the resp
14399 6f 6e 73 69 62 69 6c 69 74 79 20 6f 66 20 74 68 onsibility of th
1439a 65 20 63 61 6c 6c 65 72 2e 0a 2a 2a 0a 2a 2a 20 e caller..**.**
1439b 41 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 75 A transaction mu
1439c 73 74 20 62 65 20 61 63 74 69 76 65 20 77 68 65 st be active whe
1439d 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 n this routine i
1439e 73 20 63 61 6c 6c 65 64 2e 20 49 74 20 75 73 65 s called. It use
1439f 64 20 74 6f 20 62 65 0a 2a 2a 20 72 65 71 75 69 d to be.** requi
143a0 72 65 64 20 74 68 61 74 20 61 20 73 74 61 74 65 red that a state
143a1 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e ment transaction
143a2 20 77 61 73 20 6e 6f 74 20 61 63 74 69 76 65 2c was not active,
143a3 20 62 75 74 20 74 68 69 73 20 72 65 73 74 72 69 but this restri
143a4 63 74 69 6f 6e 0a 2a 2a 20 68 61 73 20 62 65 65 ction.** has bee
143a5 6e 20 72 65 6d 6f 76 65 64 20 28 43 52 45 41 54 n removed (CREAT
143a6 45 20 49 4e 44 45 58 20 6e 65 65 64 73 20 74 6f E INDEX needs to
143a7 20 6d 6f 76 65 20 61 20 70 61 67 65 20 77 68 65 move a page whe
143a8 6e 20 61 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a n a statement.**
143a9 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 transaction is
143aa 61 63 74 69 76 65 29 2e 0a 2a 2a 0a 2a 2a 20 49 active)..**.** I
143ab 66 20 74 68 65 20 66 6f 75 72 74 68 20 61 72 67 f the fourth arg
143ac 75 6d 65 6e 74 2c 20 69 73 43 6f 6d 6d 69 74 2c ument, isCommit,
143ad 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 is non-zero, th
143ae 65 6e 20 74 68 69 73 20 70 61 67 65 20 69 73 20 en this page is
143af 62 65 69 6e 67 0a 2a 2a 20 6d 6f 76 65 64 20 61 being.** moved a
143b0 73 20 70 61 72 74 20 6f 66 20 61 20 64 61 74 61 s part of a data
143b1 62 61 73 65 20 72 65 6f 72 67 61 6e 69 7a 61 74 base reorganizat
143b2 69 6f 6e 20 6a 75 73 74 20 62 65 66 6f 72 65 20 ion just before
143b3 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 the transaction
143b4 0a 2a 2a 20 69 73 20 62 65 69 6e 67 20 63 6f 6d .** is being com
143b5 6d 69 74 74 65 64 2e 20 49 6e 20 74 68 69 73 20 mitted. In this
143b6 63 61 73 65 2c 20 69 74 20 69 73 20 67 75 61 72 case, it is guar
143b7 61 6e 74 65 65 64 20 74 68 61 74 20 74 68 65 20 anteed that the
143b8 64 61 74 61 62 61 73 65 20 70 61 67 65 20 0a 2a database page .*
143b9 2a 20 70 50 67 20 72 65 66 65 72 73 20 74 6f 20 * pPg refers to
143ba 77 69 6c 6c 20 6e 6f 74 20 62 65 20 77 72 69 74 will not be writ
143bb 74 65 6e 20 74 6f 20 61 67 61 69 6e 20 77 69 74 ten to again wit
143bc 68 69 6e 20 74 68 69 73 20 74 72 61 6e 73 61 63 hin this transac
143bd 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 tion..**.** This
143be 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 72 65 function may re
143bf 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 turn SQLITE_NOME
143c0 4d 20 6f 72 20 61 6e 20 49 4f 20 65 72 72 6f 72 M or an IO error
143c1 20 63 6f 64 65 20 69 66 20 61 6e 20 65 72 72 6f code if an erro
143c2 72 0a 2a 2a 20 6f 63 63 75 72 73 2e 20 4f 74 68 r.** occurs. Oth
143c3 65 72 77 69 73 65 2c 20 69 74 20 72 65 74 75 72 erwise, it retur
143c4 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f ns SQLITE_OK..*/
143c5 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
143c6 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 int sqlite3Pager
143c7 4d 6f 76 65 70 61 67 65 28 50 61 67 65 72 20 2a Movepage(Pager *
143c8 70 50 61 67 65 72 2c 20 44 62 50 61 67 65 20 2a pPager, DbPage *
143c9 70 50 67 2c 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 pPg, Pgno pgno,
143ca 69 6e 74 20 69 73 43 6f 6d 6d 69 74 29 7b 0a 20 int isCommit){.
143cb 20 50 67 48 64 72 20 2a 70 50 67 4f 6c 64 3b 20 PgHdr *pPgOld;
143cc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
143cd 20 54 68 65 20 70 61 67 65 20 62 65 69 6e 67 20 The page being
143ce 6f 76 65 72 77 72 69 74 74 65 6e 2e 20 2a 2f 0a overwritten. */.
143cf 20 20 50 67 6e 6f 20 6e 65 65 64 53 79 6e 63 50 Pgno needSyncP
143d0 67 6e 6f 20 3d 20 30 3b 20 20 20 20 20 20 20 2f gno = 0; /
143d1 2a 20 4f 6c 64 20 76 61 6c 75 65 20 6f 66 20 70 * Old value of p
143d2 50 67 2d 3e 70 67 6e 6f 2c 20 69 66 20 73 79 6e Pg->pgno, if syn
143d3 63 20 69 73 20 72 65 71 75 69 72 65 64 20 2a 2f c is required */
143d4 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 . int rc;
143d5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
143d6 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a /* Return code *
143d7 2f 0a 20 20 50 67 6e 6f 20 6f 72 69 67 50 67 6e /. Pgno origPgn
143d8 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 o;
143d9 20 2f 2a 20 54 68 65 20 6f 72 69 67 69 6e 61 6c /* The original
143da 20 70 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a page number */.
143db 0a 20 20 61 73 73 65 72 74 28 20 70 50 67 2d 3e . assert( pPg->
143dc 6e 52 65 66 3e 30 20 29 3b 0a 0a 20 20 2f 2a 20 nRef>0 );.. /*
143dd 49 66 20 74 68 65 20 70 61 67 65 20 62 65 69 6e If the page bein
143de 67 20 6d 6f 76 65 64 20 69 73 20 64 69 72 74 79 g moved is dirty
143df 20 61 6e 64 20 68 61 73 20 6e 6f 74 20 62 65 65 and has not bee
143e0 6e 20 73 61 76 65 64 20 62 79 20 74 68 65 20 6c n saved by the l
143e1 61 74 65 73 74 0a 20 20 2a 2a 20 73 61 76 65 70 atest. ** savep
143e2 6f 69 6e 74 2c 20 74 68 65 6e 20 73 61 76 65 20 oint, then save
143e3 74 68 65 20 63 75 72 72 65 6e 74 20 63 6f 6e 74 the current cont
143e4 65 6e 74 73 20 6f 66 20 74 68 65 20 70 61 67 65 ents of the page
143e5 20 69 6e 74 6f 20 74 68 65 20 0a 20 20 2a 2a 20 into the . **
143e6 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 6e 6f 77 2e sub-journal now.
143e7 20 54 68 69 73 20 69 73 20 72 65 71 75 69 72 65 This is require
143e8 64 20 74 6f 20 68 61 6e 64 6c 65 20 74 68 65 20 d to handle the
143e9 66 6f 6c 6c 6f 77 69 6e 67 20 73 63 65 6e 61 72 following scenar
143ea 69 6f 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 io:. **. **
143eb 42 45 47 49 4e 3b 0a 20 20 2a 2a 20 20 20 20 20 BEGIN;. **
143ec 3c 6a 6f 75 72 6e 61 6c 20 70 61 67 65 20 58 2c <journal page X,
143ed 20 74 68 65 6e 20 6d 6f 64 69 66 79 20 69 74 20 then modify it
143ee 69 6e 20 6d 65 6d 6f 72 79 3e 0a 20 20 2a 2a 20 in memory>. **
143ef 20 20 20 20 53 41 56 45 50 4f 49 4e 54 20 6f 6e SAVEPOINT on
143f0 65 3b 0a 20 20 2a 2a 20 20 20 20 20 20 20 3c 4d e;. ** <M
143f1 6f 76 65 20 70 61 67 65 20 58 20 74 6f 20 6c 6f ove page X to lo
143f2 63 61 74 69 6f 6e 20 59 3e 0a 20 20 2a 2a 20 20 cation Y>. **
143f3 20 20 20 52 4f 4c 4c 42 41 43 4b 20 54 4f 20 6f ROLLBACK TO o
143f4 6e 65 3b 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 ne;. **. ** If
143f5 20 70 61 67 65 20 58 20 77 65 72 65 20 6e 6f 74 page X were not
143f6 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 written to the
143f7 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 68 65 72 65 sub-journal here
143f8 2c 20 69 74 20 77 6f 75 6c 64 20 6e 6f 74 0a 20 , it would not.
143f9 20 2a 2a 20 62 65 20 70 6f 73 73 69 62 6c 65 20 ** be possible
143fa 74 6f 20 72 65 73 74 6f 72 65 20 69 74 73 20 63 to restore its c
143fb 6f 6e 74 65 6e 74 73 20 77 68 65 6e 20 74 68 65 ontents when the
143fc 20 22 52 4f 4c 4c 42 41 43 4b 20 54 4f 20 6f 6e "ROLLBACK TO on
143fd 65 22 0a 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e e". ** statemen
143fe 74 20 77 65 72 65 20 69 73 20 70 72 6f 63 65 73 t were is proces
143ff 73 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 73 sed.. **. ** s
14400 75 62 6a 6f 75 72 6e 61 6c 50 61 67 65 28 29 20 ubjournalPage()
14401 6d 61 79 20 6e 65 65 64 20 74 6f 20 61 6c 6c 6f may need to allo
14402 63 61 74 65 20 73 70 61 63 65 20 74 6f 20 73 74 cate space to st
14403 6f 72 65 20 70 50 67 2d 3e 70 67 6e 6f 20 69 6e ore pPg->pgno in
14404 74 6f 0a 20 20 2a 2a 20 6f 6e 65 20 6f 72 20 6d to. ** one or m
14405 6f 72 65 20 73 61 76 65 70 6f 69 6e 74 20 62 69 ore savepoint bi
14406 74 76 65 63 73 2e 20 54 68 69 73 20 69 73 20 74 tvecs. This is t
14407 68 65 20 72 65 61 73 6f 6e 20 74 68 69 73 20 66 he reason this f
14408 75 6e 63 74 69 6f 6e 0a 20 20 2a 2a 20 6d 61 79 unction. ** may
14409 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e return SQLITE_N
1440a 4f 4d 45 4d 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 OMEM.. */. if(
1440b 20 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 pPg->flags&PGHD
1440c 52 5f 44 49 52 54 59 20 0a 20 20 20 26 26 20 73 R_DIRTY . && s
1440d 75 62 6a 52 65 71 75 69 72 65 73 50 61 67 65 28 ubjRequiresPage(
1440e 70 50 67 29 0a 20 20 20 26 26 20 53 51 4c 49 54 pPg). && SQLIT
1440f 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 75 62 6a E_OK!=(rc = subj
14410 6f 75 72 6e 61 6c 50 61 67 65 28 70 50 67 29 29 ournalPage(pPg))
14411 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e . ){. return
14412 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 50 41 47 45 rc;. }.. PAGE
14413 52 54 52 41 43 45 28 28 22 4d 4f 56 45 20 25 64 RTRACE(("MOVE %d
14414 20 70 61 67 65 20 25 64 20 28 6e 65 65 64 53 79 page %d (needSy
14415 6e 63 3d 25 64 29 20 6d 6f 76 65 73 20 74 6f 20 nc=%d) moves to
14416 25 64 5c 6e 22 2c 20 0a 20 20 20 20 20 20 50 41 %d\n", . PA
14417 47 45 52 49 44 28 70 50 61 67 65 72 29 2c 20 70 GERID(pPager), p
14418 50 67 2d 3e 70 67 6e 6f 2c 20 28 70 50 67 2d 3e Pg->pgno, (pPg->
14419 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 flags&PGHDR_NEED
1441a 5f 53 59 4e 43 29 3f 31 3a 30 2c 20 70 67 6e 6f _SYNC)?1:0, pgno
1441b 29 29 3b 0a 20 20 49 4f 54 52 41 43 45 28 28 22 ));. IOTRACE(("
1441c 4d 4f 56 45 20 25 70 20 25 64 20 25 64 5c 6e 22 MOVE %p %d %d\n"
1441d 2c 20 70 50 61 67 65 72 2c 20 70 50 67 2d 3e 70 , pPager, pPg->p
1441e 67 6e 6f 2c 20 70 67 6e 6f 29 29 0a 0a 20 20 2f gno, pgno)).. /
1441f 2a 20 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c * If the journal
14420 20 6e 65 65 64 73 20 74 6f 20 62 65 20 73 79 6e needs to be syn
14421 63 28 29 65 64 20 62 65 66 6f 72 65 20 70 61 67 c()ed before pag
14422 65 20 70 50 67 2d 3e 70 67 6e 6f 20 63 61 6e 0a e pPg->pgno can.
14423 20 20 2a 2a 20 62 65 20 77 72 69 74 74 65 6e 20 ** be written
14424 74 6f 2c 20 73 74 6f 72 65 20 70 50 67 2d 3e 70 to, store pPg->p
14425 67 6e 6f 20 69 6e 20 6c 6f 63 61 6c 20 76 61 72 gno in local var
14426 69 61 62 6c 65 20 6e 65 65 64 53 79 6e 63 50 67 iable needSyncPg
14427 6e 6f 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 no.. **. ** If
14428 20 74 68 65 20 69 73 43 6f 6d 6d 69 74 20 66 6c the isCommit fl
14429 61 67 20 69 73 20 73 65 74 2c 20 74 68 65 72 65 ag is set, there
1442a 20 69 73 20 6e 6f 20 6e 65 65 64 20 74 6f 20 72 is no need to r
1442b 65 6d 65 6d 62 65 72 20 74 68 61 74 0a 20 20 2a emember that. *
1442c 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 6e 65 * the journal ne
1442d 65 64 73 20 74 6f 20 62 65 20 73 79 6e 63 28 29 eds to be sync()
1442e 65 64 20 62 65 66 6f 72 65 20 64 61 74 61 62 61 ed before databa
1442f 73 65 20 70 61 67 65 20 70 50 67 2d 3e 70 67 6e se page pPg->pgn
14430 6f 20 0a 20 20 2a 2a 20 63 61 6e 20 62 65 20 77 o . ** can be w
14431 72 69 74 74 65 6e 20 74 6f 2e 20 54 68 65 20 63 ritten to. The c
14432 61 6c 6c 65 72 20 68 61 73 20 61 6c 72 65 61 64 aller has alread
14433 79 20 70 72 6f 6d 69 73 65 64 20 6e 6f 74 20 74 y promised not t
14434 6f 20 77 72 69 74 65 20 74 6f 20 69 74 2e 0a 20 o write to it..
14435 20 2a 2f 0a 20 20 69 66 28 20 28 70 50 67 2d 3e */. if( (pPg->
14436 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 flags&PGHDR_NEED
14437 5f 53 59 4e 43 29 20 26 26 20 21 69 73 43 6f 6d _SYNC) && !isCom
14438 6d 69 74 20 29 7b 0a 20 20 20 20 6e 65 65 64 53 mit ){. needS
14439 79 6e 63 50 67 6e 6f 20 3d 20 70 50 67 2d 3e 70 yncPgno = pPg->p
1443a 67 6e 6f 3b 0a 20 20 20 20 61 73 73 65 72 74 28 gno;. assert(
1443b 20 70 61 67 65 49 6e 4a 6f 75 72 6e 61 6c 28 70 pageInJournal(p
1443c 50 67 29 20 7c 7c 20 70 50 67 2d 3e 70 67 6e 6f Pg) || pPg->pgno
1443d 3e 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 >pPager->dbOrigS
1443e 69 7a 65 20 29 3b 0a 20 20 20 20 61 73 73 65 72 ize );. asser
1443f 74 28 20 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 t( pPg->flags&PG
14440 48 44 52 5f 44 49 52 54 59 20 29 3b 0a 20 20 20 HDR_DIRTY );.
14441 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
14442 3e 6e 65 65 64 53 79 6e 63 20 29 3b 0a 20 20 7d >needSync );. }
14443 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 63 61 .. /* If the ca
14444 63 68 65 20 63 6f 6e 74 61 69 6e 73 20 61 20 70 che contains a p
14445 61 67 65 20 77 69 74 68 20 70 61 67 65 2d 6e 75 age with page-nu
14446 6d 62 65 72 20 70 67 6e 6f 2c 20 72 65 6d 6f 76 mber pgno, remov
14447 65 20 69 74 0a 20 20 2a 2a 20 66 72 6f 6d 20 69 e it. ** from i
14448 74 73 20 68 61 73 68 20 63 68 61 69 6e 2e 20 41 ts hash chain. A
14449 6c 73 6f 2c 20 69 66 20 74 68 65 20 50 67 48 64 lso, if the PgHd
1444a 72 2e 6e 65 65 64 53 79 6e 63 20 77 61 73 20 73 r.needSync was s
1444b 65 74 20 66 6f 72 20 0a 20 20 2a 2a 20 70 61 67 et for . ** pag
1444c 65 20 70 67 6e 6f 20 62 65 66 6f 72 65 20 74 68 e pgno before th
1444d 65 20 27 6d 6f 76 65 27 20 6f 70 65 72 61 74 69 e 'move' operati
1444e 6f 6e 2c 20 69 74 20 6e 65 65 64 73 20 74 6f 20 on, it needs to
1444f 62 65 20 72 65 74 61 69 6e 65 64 20 0a 20 20 2a be retained . *
14450 2a 20 66 6f 72 20 74 68 65 20 70 61 67 65 20 6d * for the page m
14451 6f 76 65 64 20 74 68 65 72 65 2e 0a 20 20 2a 2f oved there.. */
14452 0a 20 20 70 50 67 2d 3e 66 6c 61 67 73 20 26 3d . pPg->flags &=
14453 20 7e 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e ~PGHDR_NEED_SYN
14454 43 3b 0a 20 20 70 50 67 4f 6c 64 20 3d 20 70 61 C;. pPgOld = pa
14455 67 65 72 5f 6c 6f 6f 6b 75 70 28 70 50 61 67 65 ger_lookup(pPage
14456 72 2c 20 70 67 6e 6f 29 3b 0a 20 20 61 73 73 65 r, pgno);. asse
14457 72 74 28 20 21 70 50 67 4f 6c 64 20 7c 7c 20 70 rt( !pPgOld || p
14458 50 67 4f 6c 64 2d 3e 6e 52 65 66 3d 3d 31 20 29 PgOld->nRef==1 )
14459 3b 0a 20 20 69 66 28 20 70 50 67 4f 6c 64 20 29 ;. if( pPgOld )
1445a 7b 0a 20 20 20 20 70 50 67 2d 3e 66 6c 61 67 73 {. pPg->flags
1445b 20 7c 3d 20 28 70 50 67 4f 6c 64 2d 3e 66 6c 61 |= (pPgOld->fla
1445c 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 gs&PGHDR_NEED_SY
1445d 4e 43 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 NC);. sqlite3
1445e 50 63 61 63 68 65 44 72 6f 70 28 70 50 67 4f 6c PcacheDrop(pPgOl
1445f 64 29 3b 0a 20 20 7d 0a 0a 20 20 6f 72 69 67 50 d);. }.. origP
14460 67 6e 6f 20 3d 20 70 50 67 2d 3e 70 67 6e 6f 3b gno = pPg->pgno;
14461 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 . sqlite3Pcache
14462 4d 6f 76 65 28 70 50 67 2c 20 70 67 6e 6f 29 3b Move(pPg, pgno);
14463 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 . sqlite3Pcache
14464 4d 61 6b 65 44 69 72 74 79 28 70 50 67 29 3b 0a MakeDirty(pPg);.
14465 20 20 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 pPager->dbModi
14466 66 69 65 64 20 3d 20 31 3b 0a 0a 20 20 69 66 28 fied = 1;.. if(
14467 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f 20 29 7b needSyncPgno ){
14468 0a 20 20 20 20 2f 2a 20 49 66 20 6e 65 65 64 53 . /* If needS
14469 79 6e 63 50 67 6e 6f 20 69 73 20 6e 6f 6e 2d 7a yncPgno is non-z
1446a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 6a 6f ero, then the jo
1446b 75 72 6e 61 6c 20 66 69 6c 65 20 6e 65 65 64 73 urnal file needs
1446c 20 74 6f 20 62 65 20 0a 20 20 20 20 2a 2a 20 73 to be . ** s
1446d 79 6e 63 28 29 65 64 20 62 65 66 6f 72 65 20 61 ync()ed before a
1446e 6e 79 20 64 61 74 61 20 69 73 20 77 72 69 74 74 ny data is writt
1446f 65 6e 20 74 6f 20 64 61 74 61 62 61 73 65 20 66 en to database f
14470 69 6c 65 20 70 61 67 65 20 6e 65 65 64 53 79 6e ile page needSyn
14471 63 50 67 6e 6f 2e 0a 20 20 20 20 2a 2a 20 43 75 cPgno.. ** Cu
14472 72 72 65 6e 74 6c 79 2c 20 6e 6f 20 73 75 63 68 rrently, no such
14473 20 70 61 67 65 20 65 78 69 73 74 73 20 69 6e 20 page exists in
14474 74 68 65 20 70 61 67 65 2d 63 61 63 68 65 20 61 the page-cache a
14475 6e 64 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 22 nd the . ** "
14476 69 73 20 6a 6f 75 72 6e 61 6c 65 64 22 20 62 69 is journaled" bi
14477 74 76 65 63 20 66 6c 61 67 20 68 61 73 20 62 65 tvec flag has be
14478 65 6e 20 73 65 74 2e 20 54 68 69 73 20 6e 65 65 en set. This nee
14479 64 73 20 74 6f 20 62 65 20 72 65 6d 65 64 69 65 ds to be remedie
1447a 64 20 62 79 0a 20 20 20 20 2a 2a 20 6c 6f 61 64 d by. ** load
1447b 69 6e 67 20 74 68 65 20 70 61 67 65 20 69 6e 74 ing the page int
1447c 6f 20 74 68 65 20 70 61 67 65 72 2d 63 61 63 68 o the pager-cach
1447d 65 20 61 6e 64 20 73 65 74 74 69 6e 67 20 74 68 e and setting th
1447e 65 20 50 67 48 64 72 2e 6e 65 65 64 53 79 6e 63 e PgHdr.needSync
1447f 20 0a 20 20 20 20 2a 2a 20 66 6c 61 67 2e 0a 20 . ** flag..
14480 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 20 **. ** If
14481 74 68 65 20 61 74 74 65 6d 70 74 20 74 6f 20 6c the attempt to l
14482 6f 61 64 20 74 68 65 20 70 61 67 65 20 69 6e 74 oad the page int
14483 6f 20 74 68 65 20 70 61 67 65 2d 63 61 63 68 65 o the page-cache
14484 20 66 61 69 6c 73 2c 20 28 64 75 65 0a 20 20 20 fails, (due.
14485 20 2a 2a 20 74 6f 20 61 20 6d 61 6c 6c 6f 63 28 ** to a malloc(
14486 29 20 6f 72 20 49 4f 20 66 61 69 6c 75 72 65 29 ) or IO failure)
14487 2c 20 63 6c 65 61 72 20 74 68 65 20 62 69 74 20 , clear the bit
14488 69 6e 20 74 68 65 20 70 49 6e 4a 6f 75 72 6e 61 in the pInJourna
14489 6c 5b 5d 0a 20 20 20 20 2a 2a 20 61 72 72 61 79 l[]. ** array
1448a 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 . Otherwise, if
1448b 74 68 65 20 70 61 67 65 20 69 73 20 6c 6f 61 64 the page is load
1448c 65 64 20 61 6e 64 20 77 72 69 74 74 65 6e 20 61 ed and written a
1448d 67 61 69 6e 20 69 6e 0a 20 20 20 20 2a 2a 20 74 gain in. ** t
1448e 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c his transaction,
1448f 20 69 74 20 6d 61 79 20 62 65 20 77 72 69 74 74 it may be writt
14490 65 6e 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 en to the databa
14491 73 65 20 66 69 6c 65 20 62 65 66 6f 72 65 0a 20 se file before.
14492 20 20 20 2a 2a 20 69 74 20 69 73 20 73 79 6e 63 ** it is sync
14493 65 64 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72 ed into the jour
14494 6e 61 6c 20 66 69 6c 65 2e 20 54 68 69 73 20 77 nal file. This w
14495 61 79 2c 20 69 74 20 6d 61 79 20 65 6e 64 20 75 ay, it may end u
14496 70 20 69 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20 p in. ** the
14497 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 77 69 journal file twi
14498 63 65 2c 20 62 75 74 20 74 68 61 74 20 69 73 20 ce, but that is
14499 6e 6f 74 20 61 20 70 72 6f 62 6c 65 6d 2e 0a 20 not a problem..
1449a 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 **. ** The
1449b 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 sqlite3PagerGet
1449c 28 29 20 63 61 6c 6c 20 6d 61 79 20 63 61 75 73 () call may caus
1449d 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 74 6f e the journal to
1449e 20 73 79 6e 63 2e 20 53 6f 20 6d 61 6b 65 0a 20 sync. So make.
1449f 20 20 20 2a 2a 20 73 75 72 65 20 74 68 65 20 50 ** sure the P
144a0 61 67 65 72 2e 6e 65 65 64 53 79 6e 63 20 66 6c ager.needSync fl
144a1 61 67 20 69 73 20 73 65 74 20 74 6f 6f 2e 0a 20 ag is set too..
144a2 20 20 20 2a 2f 0a 20 20 20 20 50 67 48 64 72 20 */. PgHdr
144a3 2a 70 50 67 48 64 72 3b 0a 20 20 20 20 61 73 73 *pPgHdr;. ass
144a4 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6e 65 65 ert( pPager->nee
144a5 64 53 79 6e 63 20 29 3b 0a 20 20 20 20 72 63 20 dSync );. rc
144a6 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 = sqlite3PagerGe
144a7 74 28 70 50 61 67 65 72 2c 20 6e 65 65 64 53 79 t(pPager, needSy
144a8 6e 63 50 67 6e 6f 2c 20 26 70 50 67 48 64 72 29 ncPgno, &pPgHdr)
144a9 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 ;. if( rc!=SQ
144aa 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
144ab 20 69 66 28 20 6e 65 65 64 53 79 6e 63 50 67 6e if( needSyncPgn
144ac 6f 3c 3d 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 o<=pPager->dbOri
144ad 67 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 gSize ){.
144ae 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
144af 3e 70 54 6d 70 53 70 61 63 65 21 3d 30 20 29 3b >pTmpSpace!=0 );
144b0 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
144b1 42 69 74 76 65 63 43 6c 65 61 72 28 70 50 61 67 BitvecClear(pPag
144b2 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20 er->pInJournal,
144b3 6e 65 65 64 53 79 6e 63 50 67 6e 6f 2c 20 70 50 needSyncPgno, pP
144b4 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 29 ager->pTmpSpace)
144b5 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
144b6 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d return rc;. }
144b7 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 65 65 . pPager->nee
144b8 64 53 79 6e 63 20 3d 20 31 3b 0a 20 20 20 20 61 dSync = 1;. a
144b9 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6e ssert( pPager->n
144ba 6f 53 79 6e 63 3d 3d 30 20 26 26 20 21 4d 45 4d oSync==0 && !MEM
144bb 44 42 20 29 3b 0a 20 20 20 20 70 50 67 48 64 72 DB );. pPgHdr
144bc 2d 3e 66 6c 61 67 73 20 7c 3d 20 50 47 48 44 52 ->flags |= PGHDR
144bd 5f 4e 45 45 44 5f 53 59 4e 43 3b 0a 20 20 20 20 _NEED_SYNC;.
144be 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b sqlite3PcacheMak
144bf 65 44 69 72 74 79 28 70 50 67 48 64 72 29 3b 0a eDirty(pPgHdr);.
144c0 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 sqlite3Pager
144c1 55 6e 72 65 66 28 70 50 67 48 64 72 29 3b 0a 20 Unref(pPgHdr);.
144c2 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 46 6f }.. /*. ** Fo
144c3 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 r an in-memory d
144c4 61 74 61 62 61 73 65 2c 20 6d 61 6b 65 20 73 75 atabase, make su
144c5 72 65 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 re the original
144c6 70 61 67 65 20 63 6f 6e 74 69 6e 75 65 73 0a 20 page continues.
144c7 20 2a 2a 20 74 6f 20 65 78 69 73 74 2c 20 69 6e ** to exist, in
144c8 20 63 61 73 65 20 74 68 65 20 74 72 61 6e 73 61 case the transa
144c9 63 74 69 6f 6e 20 6e 65 65 64 73 20 74 6f 20 72 ction needs to r
144ca 6f 6c 6c 20 62 61 63 6b 2e 20 20 57 65 20 61 6c oll back. We al
144cb 6c 6f 63 61 74 65 0a 20 20 2a 2a 20 74 68 65 20 locate. ** the
144cc 70 61 67 65 20 6e 6f 77 2c 20 69 6e 73 74 65 61 page now, instea
144cd 64 20 6f 66 20 61 74 20 72 6f 6c 6c 62 61 63 6b d of at rollback
144ce 2c 20 62 65 63 61 75 73 65 20 77 65 20 63 61 6e , because we can
144cf 20 62 65 74 74 65 72 20 64 65 61 6c 0a 20 20 2a better deal. *
144d0 2a 20 77 69 74 68 20 61 6e 20 6f 75 74 2d 6f 66 * with an out-of
144d1 2d 6d 65 6d 6f 72 79 20 65 72 72 6f 72 20 6e 6f -memory error no
144d2 77 2e 20 20 54 69 63 6b 65 74 20 23 33 37 36 31 w. Ticket #3761
144d3 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 4d 45 4d .. */. if( MEM
144d4 44 42 20 29 7b 0a 20 20 20 20 44 62 50 61 67 65 DB ){. DbPage
144d5 20 2a 70 4e 65 77 3b 0a 20 20 20 20 72 63 20 3d *pNew;. rc =
144d6 20 73 71 6c 69 74 65 33 50 61 67 65 72 41 63 71 sqlite3PagerAcq
144d7 75 69 72 65 28 70 50 61 67 65 72 2c 20 6f 72 69 uire(pPager, ori
144d8 67 50 67 6e 6f 2c 20 26 70 4e 65 77 2c 20 31 29 gPgno, &pNew, 1)
144d9 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 ;. if( rc!=SQ
144da 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
144db 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d 6f sqlite3PcacheMo
144dc 76 65 28 70 50 67 2c 20 6f 72 69 67 50 67 6e 6f ve(pPg, origPgno
144dd 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 );. return
144de 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 rc;. }. sq
144df 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 lite3PagerUnref(
144e0 70 4e 65 77 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 pNew);. }.. re
144e1 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
144e2 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 }.#endif../*.**
144e3 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 Return a pointer
144e4 20 74 6f 20 74 68 65 20 64 61 74 61 20 66 6f 72 to the data for
144e5 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 70 the specified p
144e6 61 67 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 age..*/.SQLITE_P
144e7 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c RIVATE void *sql
144e8 69 74 65 33 50 61 67 65 72 47 65 74 44 61 74 61 ite3PagerGetData
144e9 28 44 62 50 61 67 65 20 2a 70 50 67 29 7b 0a 20 (DbPage *pPg){.
144ea 20 61 73 73 65 72 74 28 20 70 50 67 2d 3e 6e 52 assert( pPg->nR
144eb 65 66 3e 30 20 7c 7c 20 70 50 67 2d 3e 70 50 61 ef>0 || pPg->pPa
144ec 67 65 72 2d 3e 6d 65 6d 44 62 20 29 3b 0a 20 20 ger->memDb );.
144ed 72 65 74 75 72 6e 20 70 50 67 2d 3e 70 44 61 74 return pPg->pDat
144ee 61 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 a;.}../*.** Retu
144ef 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 rn a pointer to
144f0 74 68 65 20 50 61 67 65 72 2e 6e 45 78 74 72 61 the Pager.nExtra
144f1 20 62 79 74 65 73 20 6f 66 20 22 65 78 74 72 61 bytes of "extra
144f2 22 20 73 70 61 63 65 20 0a 2a 2a 20 61 6c 6c 6f " space .** allo
144f3 63 61 74 65 64 20 61 6c 6f 6e 67 20 77 69 74 68 cated along with
144f4 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 70 the specified p
144f5 61 67 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 age..*/.SQLITE_P
144f6 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c RIVATE void *sql
144f7 69 74 65 33 50 61 67 65 72 47 65 74 45 78 74 72 ite3PagerGetExtr
144f8 61 28 44 62 50 61 67 65 20 2a 70 50 67 29 7b 0a a(DbPage *pPg){.
144f9 20 20 72 65 74 75 72 6e 20 70 50 67 2d 3e 70 45 return pPg->pE
144fa 78 74 72 61 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 xtra;.}../*.** G
144fb 65 74 2f 73 65 74 20 74 68 65 20 6c 6f 63 6b 69 et/set the locki
144fc 6e 67 2d 6d 6f 64 65 20 66 6f 72 20 74 68 69 73 ng-mode for this
144fd 20 70 61 67 65 72 2e 20 50 61 72 61 6d 65 74 65 pager. Paramete
144fe 72 20 65 4d 6f 64 65 20 6d 75 73 74 20 62 65 20 r eMode must be
144ff 6f 6e 65 0a 2a 2a 20 6f 66 20 50 41 47 45 52 5f one.** of PAGER_
14500 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 51 55 45 52 LOCKINGMODE_QUER
14501 59 2c 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 Y, PAGER_LOCKING
14502 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 20 6f 72 20 0a MODE_NORMAL or .
14503 2a 2a 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 ** PAGER_LOCKING
14504 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 2e 20 MODE_EXCLUSIVE.
14505 49 66 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 If the parameter
14506 20 69 73 20 6e 6f 74 20 5f 51 55 45 52 59 2c 20 is not _QUERY,
14507 74 68 65 6e 0a 2a 2a 20 74 68 65 20 6c 6f 63 6b then.** the lock
14508 69 6e 67 2d 6d 6f 64 65 20 69 73 20 73 65 74 20 ing-mode is set
14509 74 6f 20 74 68 65 20 76 61 6c 75 65 20 73 70 65 to the value spe
1450a 63 69 66 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 cified..**.** Th
1450b 65 20 72 65 74 75 72 6e 65 64 20 76 61 6c 75 65 e returned value
1450c 20 69 73 20 65 69 74 68 65 72 20 50 41 47 45 52 is either PAGER
1450d 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 _LOCKINGMODE_NOR
1450e 4d 41 4c 20 6f 72 0a 2a 2a 20 50 41 47 45 52 5f MAL or.** PAGER_
1450f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c LOCKINGMODE_EXCL
14510 55 53 49 56 45 2c 20 69 6e 64 69 63 61 74 69 6e USIVE, indicatin
14511 67 20 74 68 65 20 63 75 72 72 65 6e 74 20 28 70 g the current (p
14512 6f 73 73 69 62 6c 79 20 75 70 64 61 74 65 64 29 ossibly updated)
14513 0a 2a 2a 20 6c 6f 63 6b 69 6e 67 2d 6d 6f 64 65 .** locking-mode
14514 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
14515 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 ATE int sqlite3P
14516 61 67 65 72 4c 6f 63 6b 69 6e 67 4d 6f 64 65 28 agerLockingMode(
14517 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 Pager *pPager, i
14518 6e 74 20 65 4d 6f 64 65 29 7b 0a 20 20 61 73 73 nt eMode){. ass
14519 65 72 74 28 20 65 4d 6f 64 65 3d 3d 50 41 47 45 ert( eMode==PAGE
1451a 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 51 55 R_LOCKINGMODE_QU
1451b 45 52 59 0a 20 20 20 20 20 20 20 20 20 20 20 20 ERY.
1451c 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f || eMode==PAGER_
1451d 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d LOCKINGMODE_NORM
1451e 41 4c 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c AL. |
1451f 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4c | eMode==PAGER_L
14520 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 OCKINGMODE_EXCLU
14521 53 49 56 45 20 29 3b 0a 20 20 61 73 73 65 72 74 SIVE );. assert
14522 28 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d ( PAGER_LOCKINGM
14523 4f 44 45 5f 51 55 45 52 59 3c 30 20 29 3b 0a 20 ODE_QUERY<0 );.
14524 20 61 73 73 65 72 74 28 20 50 41 47 45 52 5f 4c assert( PAGER_L
14525 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 OCKINGMODE_NORMA
14526 4c 3e 3d 30 20 26 26 20 50 41 47 45 52 5f 4c 4f L>=0 && PAGER_LO
14527 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 CKINGMODE_EXCLUS
14528 49 56 45 3e 3d 30 20 29 3b 0a 20 20 69 66 28 20 IVE>=0 );. if(
14529 65 4d 6f 64 65 3e 3d 30 20 26 26 20 21 70 50 61 eMode>=0 && !pPa
1452a 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 7b ger->tempFile ){
1452b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 65 78 63 . pPager->exc
1452c 6c 75 73 69 76 65 4d 6f 64 65 20 3d 20 28 75 38 lusiveMode = (u8
1452d 29 65 4d 6f 64 65 3b 0a 20 20 7d 0a 20 20 72 65 )eMode;. }. re
1452e 74 75 72 6e 20 28 69 6e 74 29 70 50 61 67 65 72 turn (int)pPager
1452f 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 3b ->exclusiveMode;
14530 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 2f 73 65 .}../*.** Get/se
14531 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 6d 6f t the journal-mo
14532 64 65 20 66 6f 72 20 74 68 69 73 20 70 61 67 65 de for this page
14533 72 2e 20 50 61 72 61 6d 65 74 65 72 20 65 4d 6f r. Parameter eMo
14534 64 65 20 6d 75 73 74 20 62 65 20 6f 6e 65 20 6f de must be one o
14535 66 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 50 41 47 45 f:.**.** PAGE
14536 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51 55 R_JOURNALMODE_QU
14537 45 52 59 0a 2a 2a 20 20 20 20 50 41 47 45 52 5f ERY.** PAGER_
14538 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 44 45 4c 45 JOURNALMODE_DELE
14539 54 45 0a 2a 2a 20 20 20 20 50 41 47 45 52 5f 4a TE.** PAGER_J
1453a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 OURNALMODE_TRUNC
1453b 41 54 45 0a 2a 2a 20 20 20 20 50 41 47 45 52 5f ATE.** PAGER_
1453c 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 JOURNALMODE_PERS
1453d 49 53 54 0a 2a 2a 20 20 20 20 50 41 47 45 52 5f IST.** PAGER_
1453e 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 0a JOURNALMODE_OFF.
1453f 2a 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 52 ** PAGER_JOUR
14540 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 0a 2a NALMODE_MEMORY.*
14541 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 72 61 *.** If the para
14542 6d 65 74 65 72 20 69 73 20 6e 6f 74 20 5f 51 55 meter is not _QU
14543 45 52 59 2c 20 74 68 65 6e 20 74 68 65 20 6a 6f ERY, then the jo
14544 75 72 6e 61 6c 5f 6d 6f 64 65 20 69 73 20 73 65 urnal_mode is se
14545 74 20 74 6f 20 74 68 65 0a 2a 2a 20 76 61 6c 75 t to the.** valu
14546 65 20 73 70 65 63 69 66 69 65 64 20 69 66 20 74 e specified if t
14547 68 65 20 63 68 61 6e 67 65 20 69 73 20 61 6c 6c he change is all
14548 6f 77 65 64 2e 20 20 54 68 65 20 63 68 61 6e 67 owed. The chang
14549 65 20 69 73 20 64 69 73 61 6c 6c 6f 77 65 64 0a e is disallowed.
1454a 2a 2a 20 66 6f 72 20 74 68 65 20 66 6f 6c 6c 6f ** for the follo
1454b 77 69 6e 67 20 72 65 61 73 6f 6e 73 3a 0a 2a 2a wing reasons:.**
1454c 0a 2a 2a 20 20 20 2a 20 20 41 6e 20 69 6e 2d 6d .** * An in-m
1454d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20 63 emory database c
1454e 61 6e 20 6f 6e 6c 79 20 68 61 76 65 20 69 74 73 an only have its
1454f 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 20 73 65 journal_mode se
14550 74 20 74 6f 20 5f 4f 46 46 0a 2a 2a 20 20 20 20 t to _OFF.**
14551 20 20 6f 72 20 5f 4d 45 4d 4f 52 59 2e 0a 2a 2a or _MEMORY..**
14552 0a 2a 2a 20 20 20 2a 20 20 54 68 65 20 6a 6f 75 .** * The jou
14553 72 6e 61 6c 20 6d 6f 64 65 20 6d 61 79 20 6e 6f rnal mode may no
14554 74 20 62 65 20 63 68 61 6e 67 65 64 20 77 68 69 t be changed whi
14555 6c 65 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e le a transaction
14556 20 69 73 20 61 63 74 69 76 65 2e 0a 2a 2a 0a 2a is active..**.*
14557 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 69 * The returned i
14558 6e 64 69 63 61 74 65 20 74 68 65 20 63 75 72 72 ndicate the curr
14559 65 6e 74 20 28 70 6f 73 73 69 62 6c 79 20 75 70 ent (possibly up
1455a 64 61 74 65 64 29 20 6a 6f 75 72 6e 61 6c 2d 6d dated) journal-m
1455b 6f 64 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ode..*/.SQLITE_P
1455c 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
1455d 65 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f e3PagerJournalMo
1455e 64 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 de(Pager *pPager
1455f 2c 20 69 6e 74 20 65 4d 6f 64 65 29 7b 0a 20 20 , int eMode){.
14560 61 73 73 65 72 74 28 20 65 4d 6f 64 65 3d 3d 50 assert( eMode==P
14561 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 AGER_JOURNALMODE
14562 5f 51 55 45 52 59 0a 20 20 20 20 20 20 20 20 20 _QUERY.
14563 20 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 || eMode==PAG
14564 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 44 ER_JOURNALMODE_D
14565 45 4c 45 54 45 0a 20 20 20 20 20 20 20 20 20 20 ELETE.
14566 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 || eMode==PAGE
14567 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 54 52 R_JOURNALMODE_TR
14568 55 4e 43 41 54 45 0a 20 20 20 20 20 20 20 20 20 UNCATE.
14569 20 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 || eMode==PAG
1456a 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50 ER_JOURNALMODE_P
1456b 45 52 53 49 53 54 0a 20 20 20 20 20 20 20 20 20 ERSIST.
1456c 20 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 || eMode==PAG
1456d 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f ER_JOURNALMODE_O
1456e 46 46 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 FF .
1456f 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f || eMode==PAGER_
14570 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f JOURNALMODE_MEMO
14571 52 59 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 RY );. assert(
14572 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 PAGER_JOURNALMOD
14573 45 5f 51 55 45 52 59 3c 30 20 29 3b 0a 20 20 69 E_QUERY<0 );. i
14574 66 28 20 65 4d 6f 64 65 3e 3d 30 0a 20 20 20 26 f( eMode>=0. &
14575 26 20 28 21 4d 45 4d 44 42 20 7c 7c 20 65 4d 6f & (!MEMDB || eMo
14576 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 de==PAGER_JOURNA
14577 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 0a 20 20 LMODE_MEMORY .
14578 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65 || e
14579 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 Mode==PAGER_JOUR
1457a 4e 41 4c 4d 4f 44 45 5f 4f 46 46 29 0a 20 20 20 NALMODE_OFF).
1457b 26 26 20 21 70 50 61 67 65 72 2d 3e 64 62 4d 6f && !pPager->dbMo
1457c 64 69 66 69 65 64 0a 20 20 20 26 26 20 28 21 69 dified. && (!i
1457d 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 sOpen(pPager->jf
1457e 64 29 20 7c 7c 20 30 3d 3d 70 50 61 67 65 72 2d d) || 0==pPager-
1457f 3e 6a 6f 75 72 6e 61 6c 4f 66 66 29 0a 20 20 29 >journalOff). )
14580 7b 0a 20 20 20 20 69 66 28 20 69 73 4f 70 65 6e {. if( isOpen
14581 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 7b (pPager->jfd) ){
14582 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 . sqlite3Os
14583 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e 6a 66 Close(pPager->jf
14584 64 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 d);. }. pP
14585 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 ager->journalMod
14586 65 20 3d 20 28 75 38 29 65 4d 6f 64 65 3b 0a 20 e = (u8)eMode;.
14587 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 69 6e 74 }. return (int
14588 29 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c )pPager->journal
14589 4d 6f 64 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 Mode;.}../*.** G
1458a 65 74 2f 73 65 74 20 74 68 65 20 73 69 7a 65 2d et/set the size-
1458b 6c 69 6d 69 74 20 75 73 65 64 20 66 6f 72 20 70 limit used for p
1458c 65 72 73 69 73 74 65 6e 74 20 6a 6f 75 72 6e 61 ersistent journa
1458d 6c 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 l files..**.** S
1458e 65 74 74 69 6e 67 20 74 68 65 20 73 69 7a 65 20 etting the size
1458f 6c 69 6d 69 74 20 74 6f 20 2d 31 20 6d 65 61 6e limit to -1 mean
14590 73 20 6e 6f 20 6c 69 6d 69 74 20 69 73 20 65 6e s no limit is en
14591 66 6f 72 63 65 64 2e 0a 2a 2a 20 41 6e 20 61 74 forced..** An at
14592 74 65 6d 70 74 20 74 6f 20 73 65 74 20 61 20 6c tempt to set a l
14593 69 6d 69 74 20 73 6d 61 6c 6c 65 72 20 74 68 61 imit smaller tha
14594 6e 20 2d 31 20 69 73 20 61 20 6e 6f 2d 6f 70 2e n -1 is a no-op.
14595 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
14596 54 45 20 69 36 34 20 73 71 6c 69 74 65 33 50 61 TE i64 sqlite3Pa
14597 67 65 72 4a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 gerJournalSizeLi
14598 6d 69 74 28 50 61 67 65 72 20 2a 70 50 61 67 65 mit(Pager *pPage
14599 72 2c 20 69 36 34 20 69 4c 69 6d 69 74 29 7b 0a r, i64 iLimit){.
1459a 20 20 69 66 28 20 69 4c 69 6d 69 74 3e 3d 2d 31 if( iLimit>=-1
1459b 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e ){. pPager->
1459c 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 journalSizeLimit
1459d 20 3d 20 69 4c 69 6d 69 74 3b 0a 20 20 7d 0a 20 = iLimit;. }.
1459e 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e return pPager->
1459f 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 journalSizeLimit
145a0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 ;.}../*.** Retur
145a1 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 n a pointer to t
145a2 68 65 20 70 50 61 67 65 72 2d 3e 70 42 61 63 6b he pPager->pBack
145a3 75 70 20 76 61 72 69 61 62 6c 65 2e 20 54 68 65 up variable. The
145a4 20 62 61 63 6b 75 70 20 6d 6f 64 75 6c 65 0a 2a backup module.*
145a5 2a 20 69 6e 20 62 61 63 6b 75 70 2e 63 20 6d 61 * in backup.c ma
145a6 69 6e 74 61 69 6e 73 20 74 68 65 20 63 6f 6e 74 intains the cont
145a7 65 6e 74 20 6f 66 20 74 68 69 73 20 76 61 72 69 ent of this vari
145a8 61 62 6c 65 2e 20 54 68 69 73 20 6d 6f 64 75 6c able. This modul
145a9 65 0a 2a 2a 20 75 73 65 73 20 69 74 20 6f 70 61 e.** uses it opa
145aa 71 75 65 6c 79 20 61 73 20 61 6e 20 61 72 67 75 quely as an argu
145ab 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 42 ment to sqlite3B
145ac 61 63 6b 75 70 52 65 73 74 61 72 74 28 29 20 61 ackupRestart() a
145ad 6e 64 0a 2a 2a 20 73 71 6c 69 74 65 33 42 61 63 nd.** sqlite3Bac
145ae 6b 75 70 55 70 64 61 74 65 28 29 20 6f 6e 6c 79 kupUpdate() only
145af 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
145b0 41 54 45 20 73 71 6c 69 74 65 33 5f 62 61 63 6b ATE sqlite3_back
145b1 75 70 20 2a 2a 73 71 6c 69 74 65 33 50 61 67 65 up **sqlite3Page
145b2 72 42 61 63 6b 75 70 50 74 72 28 50 61 67 65 72 rBackupPtr(Pager
145b3 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 65 74 *pPager){. ret
145b4 75 72 6e 20 26 70 50 61 67 65 72 2d 3e 70 42 61 urn &pPager->pBa
145b5 63 6b 75 70 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 ckup;.}..#endif
145b6 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 /* SQLITE_OMIT_D
145b7 49 53 4b 49 4f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a ISKIO */../*****
145b8 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 ********* End of
145b9 20 70 61 67 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a pager.c *******
145ba 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
145bb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
145bc 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
145bd 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 ********* Begin
145be 66 69 6c 65 20 62 74 6d 75 74 65 78 2e 63 20 2a file btmutex.c *
145bf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
145c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
145c1 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 ********/./*.**
145c2 32 30 30 37 20 41 75 67 75 73 74 20 32 37 0a 2a 2007 August 27.*
145c3 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 *.** The author
145c4 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 disclaims copyri
145c5 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 ght to this sour
145c6 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 ce code. In pla
145c7 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c ce of.** a legal
145c8 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 notice, here is
145c9 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a a blessing:.**.
145ca 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f ** May you do
145cb 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 good and not ev
145cc 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f il..** May yo
145cd 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 u find forgivene
145ce 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 ss for yourself
145cf 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 and forgive othe
145d0 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f rs..** May yo
145d1 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 u share freely,
145d2 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 never taking mor
145d3 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e e than you give.
145d4 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .**.************
145d5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
145d6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
145d7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
145d8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
145d9 0a 2a 2a 20 24 49 64 3a 20 62 74 6d 75 74 65 78 .** $Id: btmutex
145da 2e 63 2c 76 20 31 2e 31 37 20 32 30 30 39 2f 30 .c,v 1.17 2009/0
145db 37 2f 32 30 20 31 32 3a 33 33 3a 33 33 20 64 72 7/20 12:33:33 dr
145dc 68 20 45 78 70 20 24 0a 2a 2a 0a 2a 2a 20 54 68 h Exp $.**.** Th
145dd 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 is file contains
145de 20 63 6f 64 65 20 75 73 65 64 20 74 6f 20 69 6d code used to im
145df 70 6c 65 6d 65 6e 74 20 6d 75 74 65 78 65 73 20 plement mutexes
145e0 6f 6e 20 42 74 72 65 65 20 6f 62 6a 65 63 74 73 on Btree objects
145e1 2e 0a 2a 2a 20 54 68 69 73 20 63 6f 64 65 20 72 ..** This code r
145e2 65 61 6c 6c 79 20 62 65 6c 6f 6e 67 73 20 69 6e eally belongs in
145e3 20 62 74 72 65 65 2e 63 2e 20 20 42 75 74 20 62 btree.c. But b
145e4 74 72 65 65 2e 63 20 69 73 20 67 65 74 74 69 6e tree.c is gettin
145e5 67 20 74 6f 6f 0a 2a 2a 20 62 69 67 20 61 6e 64 g too.** big and
145e6 20 77 65 20 77 61 6e 74 20 74 6f 20 62 72 65 61 we want to brea
145e7 6b 20 69 74 20 64 6f 77 6e 20 73 6f 6d 65 2e 20 k it down some.
145e8 20 54 68 69 73 20 70 61 63 6b 61 67 65 64 20 73 This packaged s
145e9 65 65 6d 65 64 20 6c 69 6b 65 0a 2a 2a 20 61 20 eemed like.** a
145ea 67 6f 6f 64 20 62 72 65 61 6b 6f 75 74 2e 0a 2a good breakout..*
145eb 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
145ec 2a 20 49 6e 63 6c 75 64 65 20 62 74 72 65 65 49 * Include btreeI
145ed 6e 74 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 nt.h in the midd
145ee 6c 65 20 6f 66 20 62 74 6d 75 74 65 78 2e 63 20 le of btmutex.c
145ef 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
145f0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
145f1 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 62 74 72 * Begin file btr
145f2 65 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a eeInt.h ********
145f3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
145f4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
145f5 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 41 70 72 /./*.** 2004 Apr
145f6 69 6c 20 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 il 6.**.** The a
145f7 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 uthor disclaims
145f8 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 copyright to thi
145f9 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 s source code.
145fa 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 In place of.** a
145fb 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 legal notice, h
145fc 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e ere is a blessin
145fd 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 g:.**.** May
145fe 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 you do good and
145ff 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 not evil..**
14600 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 May you find for
14601 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 giveness for you
14602 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 rself and forgiv
14603 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 e others..**
14604 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 May you share fr
14605 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 eely, never taki
14606 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 ng more than you
14607 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a give..**.******
14608 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14609 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1460a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1460b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1460c 2a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 62 74 72 65 ***.** $Id: btre
1460d 65 49 6e 74 2e 68 2c 76 20 31 2e 35 32 20 32 30 eInt.h,v 1.52 20
1460e 30 39 2f 30 37 2f 31 35 20 31 37 3a 32 35 3a 34 09/07/15 17:25:4
1460f 36 20 64 72 68 20 45 78 70 20 24 0a 2a 2a 0a 2a 6 drh Exp $.**.*
14610 2a 20 54 68 69 73 20 66 69 6c 65 20 69 6d 70 6c * This file impl
14611 65 6d 65 6e 74 73 20 61 20 65 78 74 65 72 6e 61 ements a externa
14612 6c 20 28 64 69 73 6b 2d 62 61 73 65 64 29 20 64 l (disk-based) d
14613 61 74 61 62 61 73 65 20 75 73 69 6e 67 20 42 54 atabase using BT
14614 72 65 65 73 2e 0a 2a 2a 20 46 6f 72 20 61 20 64 rees..** For a d
14615 65 74 61 69 6c 65 64 20 64 69 73 63 75 73 73 69 etailed discussi
14616 6f 6e 20 6f 66 20 42 54 72 65 65 73 2c 20 72 65 on of BTrees, re
14617 66 65 72 20 74 6f 0a 2a 2a 0a 2a 2a 20 20 20 20 fer to.**.**
14618 20 44 6f 6e 61 6c 64 20 45 2e 20 4b 6e 75 74 68 Donald E. Knuth
14619 2c 20 54 48 45 20 41 52 54 20 4f 46 20 43 4f 4d , THE ART OF COM
1461a 50 55 54 45 52 20 50 52 4f 47 52 41 4d 4d 49 4e PUTER PROGRAMMIN
1461b 47 2c 20 56 6f 6c 75 6d 65 20 33 3a 0a 2a 2a 20 G, Volume 3:.**
1461c 20 20 20 20 22 53 6f 72 74 69 6e 67 20 41 6e 64 "Sorting And
1461d 20 53 65 61 72 63 68 69 6e 67 22 2c 20 70 61 67 Searching", pag
1461e 65 73 20 34 37 33 2d 34 38 30 2e 20 41 64 64 69 es 473-480. Addi
1461f 73 6f 6e 2d 57 65 73 6c 65 79 0a 2a 2a 20 20 20 son-Wesley.**
14620 20 20 50 75 62 6c 69 73 68 69 6e 67 20 43 6f 6d Publishing Com
14621 70 61 6e 79 2c 20 52 65 61 64 69 6e 67 2c 20 4d pany, Reading, M
14622 61 73 73 61 63 68 75 73 65 74 74 73 2e 0a 2a 2a assachusetts..**
14623 0a 2a 2a 20 54 68 65 20 62 61 73 69 63 20 69 64 .** The basic id
14624 65 61 20 69 73 20 74 68 61 74 20 65 61 63 68 20 ea is that each
14625 70 61 67 65 20 6f 66 20 74 68 65 20 66 69 6c 65 page of the file
14626 20 63 6f 6e 74 61 69 6e 73 20 4e 20 64 61 74 61 contains N data
14627 62 61 73 65 0a 2a 2a 20 65 6e 74 72 69 65 73 20 base.** entries
14628 61 6e 64 20 4e 2b 31 20 70 6f 69 6e 74 65 72 73 and N+1 pointers
14629 20 74 6f 20 73 75 62 70 61 67 65 73 2e 0a 2a 2a to subpages..**
1462a 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d .** ----------
1462b 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1462c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1462d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1462e 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 7c 20 20 50 ------.** | P
1462f 74 72 28 30 29 20 7c 20 4b 65 79 28 30 29 20 7c tr(0) | Key(0) |
14630 20 50 74 72 28 31 29 20 7c 20 4b 65 79 28 31 29 Ptr(1) | Key(1)
14631 20 7c 20 2e 2e 2e 20 7c 20 4b 65 79 28 4e 2d 31 | ... | Key(N-1
14632 29 20 7c 20 50 74 72 28 4e 29 20 7c 0a 2a 2a 20 ) | Ptr(N) |.**
14633 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d --------------
14634 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
14635 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
14636 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
14637 2d 2d 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 20 --.**.** All of
14638 74 68 65 20 6b 65 79 73 20 6f 6e 20 74 68 65 20 the keys on the
14639 70 61 67 65 20 74 68 61 74 20 50 74 72 28 30 29 page that Ptr(0)
1463a 20 70 6f 69 6e 74 73 20 74 6f 20 68 61 76 65 20 points to have
1463b 76 61 6c 75 65 73 20 6c 65 73 73 0a 2a 2a 20 74 values less.** t
1463c 68 61 6e 20 4b 65 79 28 30 29 2e 20 20 41 6c 6c han Key(0). All
1463d 20 6f 66 20 74 68 65 20 6b 65 79 73 20 6f 6e 20 of the keys on
1463e 70 61 67 65 20 50 74 72 28 31 29 20 61 6e 64 20 page Ptr(1) and
1463f 69 74 73 20 73 75 62 70 61 67 65 73 20 68 61 76 its subpages hav
14640 65 0a 2a 2a 20 76 61 6c 75 65 73 20 67 72 65 61 e.** values grea
14641 74 65 72 20 74 68 61 6e 20 4b 65 79 28 30 29 20 ter than Key(0)
14642 61 6e 64 20 6c 65 73 73 20 74 68 61 6e 20 4b 65 and less than Ke
14643 79 28 31 29 2e 20 20 41 6c 6c 20 6f 66 20 74 68 y(1). All of th
14644 65 20 6b 65 79 73 0a 2a 2a 20 6f 6e 20 50 74 72 e keys.** on Ptr
14645 28 4e 29 20 61 6e 64 20 69 74 73 20 73 75 62 70 (N) and its subp
14646 61 67 65 73 20 68 61 76 65 20 76 61 6c 75 65 73 ages have values
14647 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 4b 65 greater than Ke
14648 79 28 4e 2d 31 29 2e 20 20 41 6e 64 0a 2a 2a 20 y(N-1). And.**
14649 73 6f 20 66 6f 72 74 68 2e 0a 2a 2a 0a 2a 2a 20 so forth..**.**
1464a 46 69 6e 64 69 6e 67 20 61 20 70 61 72 74 69 63 Finding a partic
1464b 75 6c 61 72 20 6b 65 79 20 72 65 71 75 69 72 65 ular key require
1464c 73 20 72 65 61 64 69 6e 67 20 4f 28 6c 6f 67 28 s reading O(log(
1464d 4d 29 29 20 70 61 67 65 73 20 66 72 6f 6d 20 74 M)) pages from t
1464e 68 65 20 0a 2a 2a 20 64 69 73 6b 20 77 68 65 72 he .** disk wher
1464f 65 20 4d 20 69 73 20 74 68 65 20 6e 75 6d 62 65 e M is the numbe
14650 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 r of entries in
14651 74 68 65 20 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 the tree..**.**
14652 49 6e 20 74 68 69 73 20 69 6d 70 6c 65 6d 65 6e In this implemen
14653 74 61 74 69 6f 6e 2c 20 61 20 73 69 6e 67 6c 65 tation, a single
14654 20 66 69 6c 65 20 63 61 6e 20 68 6f 6c 64 20 6f file can hold o
14655 6e 65 20 6f 72 20 6d 6f 72 65 20 73 65 70 61 72 ne or more separ
14656 61 74 65 20 0a 2a 2a 20 42 54 72 65 65 73 2e 20 ate .** BTrees.
14657 20 45 61 63 68 20 42 54 72 65 65 20 69 73 20 69 Each BTree is i
14658 64 65 6e 74 69 66 69 65 64 20 62 79 20 74 68 65 dentified by the
14659 20 69 6e 64 65 78 20 6f 66 20 69 74 73 20 72 6f index of its ro
1465a 6f 74 20 70 61 67 65 2e 20 20 54 68 65 0a 2a 2a ot page. The.**
1465b 20 6b 65 79 20 61 6e 64 20 64 61 74 61 20 66 6f key and data fo
1465c 72 20 61 6e 79 20 65 6e 74 72 79 20 61 72 65 20 r any entry are
1465d 63 6f 6d 62 69 6e 65 64 20 74 6f 20 66 6f 72 6d combined to form
1465e 20 74 68 65 20 22 70 61 79 6c 6f 61 64 22 2e 20 the "payload".
1465f 20 41 0a 2a 2a 20 66 69 78 65 64 20 61 6d 6f 75 A.** fixed amou
14660 6e 74 20 6f 66 20 70 61 79 6c 6f 61 64 20 63 61 nt of payload ca
14661 6e 20 62 65 20 63 61 72 72 69 65 64 20 64 69 72 n be carried dir
14662 65 63 74 6c 79 20 6f 6e 20 74 68 65 20 64 61 74 ectly on the dat
14663 61 62 61 73 65 0a 2a 2a 20 70 61 67 65 2e 20 20 abase.** page.
14664 49 66 20 74 68 65 20 70 61 79 6c 6f 61 64 20 69 If the payload i
14665 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 s larger than th
14666 65 20 70 72 65 73 65 74 20 61 6d 6f 75 6e 74 20 e preset amount
14667 74 68 65 6e 20 73 75 72 70 6c 75 73 0a 2a 2a 20 then surplus.**
14668 62 79 74 65 73 20 61 72 65 20 73 74 6f 72 65 64 bytes are stored
14669 20 6f 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 on overflow pag
1466a 65 73 2e 20 20 54 68 65 20 70 61 79 6c 6f 61 64 es. The payload
1466b 20 66 6f 72 20 61 6e 20 65 6e 74 72 79 0a 2a 2a for an entry.**
1466c 20 61 6e 64 20 74 68 65 20 70 72 65 63 65 64 69 and the precedi
1466d 6e 67 20 70 6f 69 6e 74 65 72 20 61 72 65 20 63 ng pointer are c
1466e 6f 6d 62 69 6e 65 64 20 74 6f 20 66 6f 72 6d 20 ombined to form
1466f 61 20 22 43 65 6c 6c 22 2e 20 20 45 61 63 68 20 a "Cell". Each
14670 0a 2a 2a 20 70 61 67 65 20 68 61 73 20 61 20 73 .** page has a s
14671 6d 61 6c 6c 20 68 65 61 64 65 72 20 77 68 69 63 mall header whic
14672 68 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 50 h contains the P
14673 74 72 28 4e 29 20 70 6f 69 6e 74 65 72 20 61 6e tr(N) pointer an
14674 64 20 6f 74 68 65 72 0a 2a 2a 20 69 6e 66 6f 72 d other.** infor
14675 6d 61 74 69 6f 6e 20 73 75 63 68 20 61 73 20 74 mation such as t
14676 68 65 20 73 69 7a 65 20 6f 66 20 6b 65 79 20 61 he size of key a
14677 6e 64 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 46 nd data..**.** F
14678 4f 52 4d 41 54 20 44 45 54 41 49 4c 53 0a 2a 2a ORMAT DETAILS.**
14679 0a 2a 2a 20 54 68 65 20 66 69 6c 65 20 69 73 20 .** The file is
1467a 64 69 76 69 64 65 64 20 69 6e 74 6f 20 70 61 67 divided into pag
1467b 65 73 2e 20 20 54 68 65 20 66 69 72 73 74 20 70 es. The first p
1467c 61 67 65 20 69 73 20 63 61 6c 6c 65 64 20 70 61 age is called pa
1467d 67 65 20 31 2c 0a 2a 2a 20 74 68 65 20 73 65 63 ge 1,.** the sec
1467e 6f 6e 64 20 69 73 20 70 61 67 65 20 32 2c 20 61 ond is page 2, a
1467f 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20 20 41 20 nd so forth. A
14680 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 7a page number of z
14681 65 72 6f 20 69 6e 64 69 63 61 74 65 73 0a 2a 2a ero indicates.**
14682 20 22 6e 6f 20 73 75 63 68 20 70 61 67 65 22 2e "no such page".
14683 20 20 54 68 65 20 70 61 67 65 20 73 69 7a 65 20 The page size
14684 63 61 6e 20 62 65 20 61 6e 79 74 68 69 6e 67 20 can be anything
14685 62 65 74 77 65 65 6e 20 35 31 32 20 61 6e 64 20 between 512 and
14686 36 35 35 33 36 2e 0a 2a 2a 20 45 61 63 68 20 70 65536..** Each p
14687 61 67 65 20 63 61 6e 20 62 65 20 65 69 74 68 65 age can be eithe
14688 72 20 61 20 62 74 72 65 65 20 70 61 67 65 2c 20 r a btree page,
14689 61 20 66 72 65 65 6c 69 73 74 20 70 61 67 65 20 a freelist page
1468a 6f 72 20 61 6e 20 6f 76 65 72 66 6c 6f 77 0a 2a or an overflow.*
1468b 2a 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 * page..**.** Th
1468c 65 20 66 69 72 73 74 20 70 61 67 65 20 69 73 20 e first page is
1468d 61 6c 77 61 79 73 20 61 20 62 74 72 65 65 20 70 always a btree p
1468e 61 67 65 2e 20 20 54 68 65 20 66 69 72 73 74 20 age. The first
1468f 31 30 30 20 62 79 74 65 73 20 6f 66 20 74 68 65 100 bytes of the
14690 20 66 69 72 73 74 0a 2a 2a 20 70 61 67 65 20 63 first.** page c
14691 6f 6e 74 61 69 6e 20 61 20 73 70 65 63 69 61 6c ontain a special
14692 20 68 65 61 64 65 72 20 28 74 68 65 20 22 66 69 header (the "fi
14693 6c 65 20 68 65 61 64 65 72 22 29 20 74 68 61 74 le header") that
14694 20 64 65 73 63 72 69 62 65 73 20 74 68 65 20 66 describes the f
14695 69 6c 65 2e 0a 2a 2a 20 54 68 65 20 66 6f 72 6d ile..** The form
14696 61 74 20 6f 66 20 74 68 65 20 66 69 6c 65 20 68 at of the file h
14697 65 61 64 65 72 20 69 73 20 61 73 20 66 6f 6c 6c eader is as foll
14698 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 4f 46 46 ows:.**.** OFF
14699 53 45 54 20 20 20 53 49 5a 45 20 20 20 20 44 45 SET SIZE DE
1469a 53 43 52 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 SCRIPTION.**
1469b 20 20 30 20 20 20 20 20 20 31 36 20 20 20 20 20 0 16
1469c 48 65 61 64 65 72 20 73 74 72 69 6e 67 3a 20 22 Header string: "
1469d 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 5c SQLite format 3\
1469e 30 30 30 22 0a 2a 2a 20 20 20 20 20 31 36 20 20 000".** 16
1469f 20 20 20 20 20 32 20 20 20 20 20 50 61 67 65 20 2 Page
146a0 73 69 7a 65 20 69 6e 20 62 79 74 65 73 2e 20 20 size in bytes.
146a1 0a 2a 2a 20 20 20 20 20 31 38 20 20 20 20 20 20 .** 18
146a2 20 31 20 20 20 20 20 46 69 6c 65 20 66 6f 72 6d 1 File form
146a3 61 74 20 77 72 69 74 65 20 76 65 72 73 69 6f 6e at write version
146a4 0a 2a 2a 20 20 20 20 20 31 39 20 20 20 20 20 20 .** 19
146a5 20 31 20 20 20 20 20 46 69 6c 65 20 66 6f 72 6d 1 File form
146a6 61 74 20 72 65 61 64 20 76 65 72 73 69 6f 6e 0a at read version.
146a7 2a 2a 20 20 20 20 20 32 30 20 20 20 20 20 20 20 ** 20
146a8 31 20 20 20 20 20 42 79 74 65 73 20 6f 66 20 75 1 Bytes of u
146a9 6e 75 73 65 64 20 73 70 61 63 65 20 61 74 20 74 nused space at t
146aa 68 65 20 65 6e 64 20 6f 66 20 65 61 63 68 20 70 he end of each p
146ab 61 67 65 0a 2a 2a 20 20 20 20 20 32 31 20 20 20 age.** 21
146ac 20 20 20 20 31 20 20 20 20 20 4d 61 78 20 65 6d 1 Max em
146ad 62 65 64 64 65 64 20 70 61 79 6c 6f 61 64 20 66 bedded payload f
146ae 72 61 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 32 raction.** 2
146af 32 20 20 20 20 20 20 20 31 20 20 20 20 20 4d 69 2 1 Mi
146b0 6e 20 65 6d 62 65 64 64 65 64 20 70 61 79 6c 6f n embedded paylo
146b1 61 64 20 66 72 61 63 74 69 6f 6e 0a 2a 2a 20 20 ad fraction.**
146b2 20 20 20 32 33 20 20 20 20 20 20 20 31 20 20 20 23 1
146b3 20 20 4d 69 6e 20 6c 65 61 66 20 70 61 79 6c 6f Min leaf paylo
146b4 61 64 20 66 72 61 63 74 69 6f 6e 0a 2a 2a 20 20 ad fraction.**
146b5 20 20 20 32 34 20 20 20 20 20 20 20 34 20 20 20 24 4
146b6 20 20 46 69 6c 65 20 63 68 61 6e 67 65 20 63 6f File change co
146b7 75 6e 74 65 72 0a 2a 2a 20 20 20 20 20 32 38 20 unter.** 28
146b8 20 20 20 20 20 20 34 20 20 20 20 20 52 65 73 65 4 Rese
146b9 72 76 65 64 20 66 6f 72 20 66 75 74 75 72 65 20 rved for future
146ba 75 73 65 0a 2a 2a 20 20 20 20 20 33 32 20 20 20 use.** 32
146bb 20 20 20 20 34 20 20 20 20 20 46 69 72 73 74 20 4 First
146bc 66 72 65 65 6c 69 73 74 20 70 61 67 65 0a 2a 2a freelist page.**
146bd 20 20 20 20 20 33 36 20 20 20 20 20 20 20 34 20 36 4
146be 20 20 20 20 4e 75 6d 62 65 72 20 6f 66 20 66 72 Number of fr
146bf 65 65 6c 69 73 74 20 70 61 67 65 73 20 69 6e 20 eelist pages in
146c0 74 68 65 20 66 69 6c 65 0a 2a 2a 20 20 20 20 20 the file.**
146c1 34 30 20 20 20 20 20 20 36 30 20 20 20 20 20 31 40 60 1
146c2 35 20 34 2d 62 79 74 65 20 6d 65 74 61 20 76 61 5 4-byte meta va
146c3 6c 75 65 73 20 70 61 73 73 65 64 20 74 6f 20 68 lues passed to h
146c4 69 67 68 65 72 20 6c 61 79 65 72 73 0a 2a 2a 0a igher layers.**.
146c5 2a 2a 20 20 20 20 20 34 30 20 20 20 20 20 20 20 ** 40
146c6 34 20 20 20 20 20 53 63 68 65 6d 61 20 63 6f 6f 4 Schema coo
146c7 6b 69 65 0a 2a 2a 20 20 20 20 20 34 34 20 20 20 kie.** 44
146c8 20 20 20 20 34 20 20 20 20 20 46 69 6c 65 20 66 4 File f
146c9 6f 72 6d 61 74 20 6f 66 20 73 63 68 65 6d 61 20 ormat of schema
146ca 6c 61 79 65 72 0a 2a 2a 20 20 20 20 20 34 38 20 layer.** 48
146cb 20 20 20 20 20 20 34 20 20 20 20 20 53 69 7a 65 4 Size
146cc 20 6f 66 20 70 61 67 65 20 63 61 63 68 65 0a 2a of page cache.*
146cd 2a 20 20 20 20 20 35 32 20 20 20 20 20 20 20 34 * 52 4
146ce 20 20 20 20 20 4c 61 72 67 65 73 74 20 72 6f 6f Largest roo
146cf 74 2d 70 61 67 65 20 28 61 75 74 6f 2f 69 6e 63 t-page (auto/inc
146d0 72 5f 76 61 63 75 75 6d 29 0a 2a 2a 20 20 20 20 r_vacuum).**
146d1 20 35 36 20 20 20 20 20 20 20 34 20 20 20 20 20 56 4
146d2 31 3d 55 54 46 2d 38 20 32 3d 55 54 46 31 36 6c 1=UTF-8 2=UTF16l
146d3 65 20 33 3d 55 54 46 31 36 62 65 0a 2a 2a 20 20 e 3=UTF16be.**
146d4 20 20 20 36 30 20 20 20 20 20 20 20 34 20 20 20 60 4
146d5 20 20 55 73 65 72 20 76 65 72 73 69 6f 6e 0a 2a User version.*
146d6 2a 20 20 20 20 20 36 34 20 20 20 20 20 20 20 34 * 64 4
146d7 20 20 20 20 20 49 6e 63 72 65 6d 65 6e 74 61 6c Incremental
146d8 20 76 61 63 75 75 6d 20 6d 6f 64 65 0a 2a 2a 20 vacuum mode.**
146d9 20 20 20 20 36 38 20 20 20 20 20 20 20 34 20 20 68 4
146da 20 20 20 75 6e 75 73 65 64 0a 2a 2a 20 20 20 20 unused.**
146db 20 37 32 20 20 20 20 20 20 20 34 20 20 20 20 20 72 4
146dc 75 6e 75 73 65 64 0a 2a 2a 20 20 20 20 20 37 36 unused.** 76
146dd 20 20 20 20 20 20 20 34 20 20 20 20 20 75 6e 75 4 unu
146de 73 65 64 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 sed.**.** All of
146df 20 74 68 65 20 69 6e 74 65 67 65 72 20 76 61 6c the integer val
146e0 75 65 73 20 61 72 65 20 62 69 67 2d 65 6e 64 69 ues are big-endi
146e1 61 6e 20 28 6d 6f 73 74 20 73 69 67 6e 69 66 69 an (most signifi
146e2 63 61 6e 74 20 62 79 74 65 20 66 69 72 73 74 29 cant byte first)
146e3 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 6c 65 ..**.** The file
146e4 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 20 change counter
146e5 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 77 is incremented w
146e6 68 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 hen the database
146e7 20 69 73 20 63 68 61 6e 67 65 64 0a 2a 2a 20 54 is changed.** T
146e8 68 69 73 20 63 6f 75 6e 74 65 72 20 61 6c 6c 6f his counter allo
146e9 77 73 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 ws other process
146ea 65 73 20 74 6f 20 6b 6e 6f 77 20 77 68 65 6e 20 es to know when
146eb 74 68 65 20 66 69 6c 65 20 68 61 73 20 63 68 61 the file has cha
146ec 6e 67 65 64 0a 2a 2a 20 61 6e 64 20 74 68 75 73 nged.** and thus
146ed 20 77 68 65 6e 20 74 68 65 79 20 6e 65 65 64 20 when they need
146ee 74 6f 20 66 6c 75 73 68 20 74 68 65 69 72 20 63 to flush their c
146ef 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ache..**.** The
146f0 6d 61 78 20 65 6d 62 65 64 64 65 64 20 70 61 79 max embedded pay
146f1 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e 20 69 73 load fraction is
146f2 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 74 the amount of t
146f3 68 65 20 74 6f 74 61 6c 20 75 73 61 62 6c 65 0a he total usable.
146f4 2a 2a 20 73 70 61 63 65 20 69 6e 20 61 20 70 61 ** space in a pa
146f5 67 65 20 74 68 61 74 20 63 61 6e 20 62 65 20 63 ge that can be c
146f6 6f 6e 73 75 6d 65 64 20 62 79 20 61 20 73 69 6e onsumed by a sin
146f7 67 6c 65 20 63 65 6c 6c 20 66 6f 72 20 73 74 61 gle cell for sta
146f8 6e 64 61 72 64 0a 2a 2a 20 42 2d 74 72 65 65 20 ndard.** B-tree
146f9 28 6e 6f 6e 2d 4c 45 41 46 44 41 54 41 29 20 74 (non-LEAFDATA) t
146fa 61 62 6c 65 73 2e 20 20 41 20 76 61 6c 75 65 20 ables. A value
146fb 6f 66 20 32 35 35 20 6d 65 61 6e 73 20 31 30 30 of 255 means 100
146fc 25 2e 20 20 54 68 65 20 64 65 66 61 75 6c 74 0a %. The default.
146fd 2a 2a 20 69 73 20 74 6f 20 6c 69 6d 69 74 20 74 ** is to limit t
146fe 68 65 20 6d 61 78 69 6d 75 6d 20 63 65 6c 6c 20 he maximum cell
146ff 73 69 7a 65 20 73 6f 20 74 68 61 74 20 61 74 20 size so that at
14700 6c 65 61 73 74 20 34 20 63 65 6c 6c 73 20 77 69 least 4 cells wi
14701 6c 6c 20 66 69 74 0a 2a 2a 20 6f 6e 20 6f 6e 65 ll fit.** on one
14702 20 70 61 67 65 2e 20 20 54 68 75 73 20 74 68 65 page. Thus the
14703 20 64 65 66 61 75 6c 74 20 6d 61 78 20 65 6d 62 default max emb
14704 65 64 64 65 64 20 70 61 79 6c 6f 61 64 20 66 72 edded payload fr
14705 61 63 74 69 6f 6e 20 69 73 20 36 34 2e 0a 2a 2a action is 64..**
14706 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 79 6c 6f .** If the paylo
14707 61 64 20 66 6f 72 20 61 20 63 65 6c 6c 20 69 73 ad for a cell is
14708 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 65 larger than the
14709 20 6d 61 78 20 70 61 79 6c 6f 61 64 2c 20 74 68 max payload, th
1470a 65 6e 20 65 78 74 72 61 0a 2a 2a 20 70 61 79 6c en extra.** payl
1470b 6f 61 64 20 69 73 20 73 70 69 6c 6c 65 64 20 74 oad is spilled t
1470c 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 o overflow pages
1470d 2e 20 20 4f 6e 63 65 20 61 6e 20 6f 76 65 72 66 . Once an overf
1470e 6c 6f 77 20 70 61 67 65 20 69 73 20 61 6c 6c 6f low page is allo
1470f 63 61 74 65 64 2c 0a 2a 2a 20 61 73 20 6d 61 6e cated,.** as man
14710 79 20 62 79 74 65 73 20 61 73 20 70 6f 73 73 69 y bytes as possi
14711 62 6c 65 20 61 72 65 20 6d 6f 76 65 64 20 69 6e ble are moved in
14712 74 6f 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 to the overflow
14713 70 61 67 65 73 20 77 69 74 68 6f 75 74 20 6c 65 pages without le
14714 74 74 69 6e 67 0a 2a 2a 20 74 68 65 20 63 65 6c tting.** the cel
14715 6c 20 73 69 7a 65 20 64 72 6f 70 20 62 65 6c 6f l size drop belo
14716 77 20 74 68 65 20 6d 69 6e 20 65 6d 62 65 64 64 w the min embedd
14717 65 64 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74 ed payload fract
14718 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d ion..**.** The m
14719 69 6e 20 6c 65 61 66 20 70 61 79 6c 6f 61 64 20 in leaf payload
1471a 66 72 61 63 74 69 6f 6e 20 69 73 20 6c 69 6b 65 fraction is like
1471b 20 74 68 65 20 6d 69 6e 20 65 6d 62 65 64 64 65 the min embedde
1471c 64 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74 69 d payload fracti
1471d 6f 6e 0a 2a 2a 20 65 78 63 65 70 74 20 74 68 61 on.** except tha
1471e 74 20 69 74 20 61 70 70 6c 69 65 73 20 74 6f 20 t it applies to
1471f 6c 65 61 66 20 6e 6f 64 65 73 20 69 6e 20 61 20 leaf nodes in a
14720 4c 45 41 46 44 41 54 41 20 74 72 65 65 2e 20 20 LEAFDATA tree.
14721 54 68 65 20 6d 61 78 69 6d 75 6d 0a 2a 2a 20 70 The maximum.** p
14722 61 79 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e 20 ayload fraction
14723 66 6f 72 20 61 20 4c 45 41 46 44 41 54 41 20 74 for a LEAFDATA t
14724 72 65 65 20 69 73 20 61 6c 77 61 79 73 20 31 30 ree is always 10
14725 30 25 20 28 6f 72 20 32 35 35 29 20 61 6e 64 20 0% (or 255) and
14726 69 74 0a 2a 2a 20 6e 6f 74 20 73 70 65 63 69 66 it.** not specif
14727 69 65 64 20 69 6e 20 74 68 65 20 68 65 61 64 65 ied in the heade
14728 72 2e 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 62 74 r..**.** Each bt
14729 72 65 65 20 70 61 67 65 73 20 69 73 20 64 69 76 ree pages is div
1472a 69 64 65 64 20 69 6e 74 6f 20 74 68 72 65 65 20 ided into three
1472b 73 65 63 74 69 6f 6e 73 3a 20 20 54 68 65 20 68 sections: The h
1472c 65 61 64 65 72 2c 20 74 68 65 0a 2a 2a 20 63 65 eader, the.** ce
1472d 6c 6c 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79 ll pointer array
1472e 2c 20 61 6e 64 20 74 68 65 20 63 65 6c 6c 20 63 , and the cell c
1472f 6f 6e 74 65 6e 74 20 61 72 65 61 2e 20 20 50 61 ontent area. Pa
14730 67 65 20 31 20 61 6c 73 6f 20 68 61 73 20 61 20 ge 1 also has a
14731 31 30 30 2d 62 79 74 65 0a 2a 2a 20 66 69 6c 65 100-byte.** file
14732 20 68 65 61 64 65 72 20 74 68 61 74 20 6f 63 63 header that occ
14733 75 72 73 20 62 65 66 6f 72 65 20 74 68 65 20 70 urs before the p
14734 61 67 65 20 68 65 61 64 65 72 2e 0a 2a 2a 0a 2a age header..**.*
14735 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d * |--------
14736 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a 20 20 20 20 --------|.**
14737 20 20 7c 20 66 69 6c 65 20 68 65 61 64 65 72 20 | file header
14738 20 20 20 7c 20 20 20 31 30 30 20 62 79 74 65 73 | 100 bytes
14739 2e 20 20 50 61 67 65 20 31 20 6f 6e 6c 79 2e 0a . Page 1 only..
1473a 2a 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d ** |-------
1473b 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a 20 20 20 ---------|.**
1473c 20 20 20 7c 20 70 61 67 65 20 68 65 61 64 65 72 | page header
1473d 20 20 20 20 7c 20 20 20 38 20 62 79 74 65 73 20 | 8 bytes
1473e 66 6f 72 20 6c 65 61 76 65 73 2e 20 20 31 32 20 for leaves. 12
1473f 62 79 74 65 73 20 66 6f 72 20 69 6e 74 65 72 69 bytes for interi
14740 6f 72 20 6e 6f 64 65 73 0a 2a 2a 20 20 20 20 20 or nodes.**
14741 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d |--------------
14742 2d 2d 7c 0a 2a 2a 20 20 20 20 20 20 7c 20 63 65 --|.** | ce
14743 6c 6c 20 70 6f 69 6e 74 65 72 20 20 20 7c 20 20 ll pointer |
14744 20 7c 20 20 32 20 62 79 74 65 73 20 70 65 72 20 | 2 bytes per
14745 63 65 6c 6c 2e 20 20 53 6f 72 74 65 64 20 6f 72 cell. Sorted or
14746 64 65 72 2e 0a 2a 2a 20 20 20 20 20 20 7c 20 61 der..** | a
14747 72 72 61 79 20 20 20 20 20 20 20 20 20 20 7c 20 rray |
14748 20 20 7c 20 20 47 72 6f 77 73 20 64 6f 77 6e 77 | Grows downw
14749 61 72 64 0a 2a 2a 20 20 20 20 20 20 7c 20 20 20 ard.** |
1474a 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 20 20 |
1474b 20 76 0a 2a 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d v.** |----
1474c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a ------------|.**
1474d 20 20 20 20 20 20 7c 20 75 6e 61 6c 6c 6f 63 61 | unalloca
1474e 74 65 64 20 20 20 20 7c 0a 2a 2a 20 20 20 20 20 ted |.**
1474f 20 7c 20 73 70 61 63 65 20 20 20 20 20 20 20 20 | space
14750 20 20 7c 0a 2a 2a 20 20 20 20 20 20 7c 2d 2d 2d |.** |---
14751 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 20 20 -------------|
14752 20 5e 20 20 47 72 6f 77 73 20 75 70 77 61 72 64 ^ Grows upward
14753 73 0a 2a 2a 20 20 20 20 20 20 7c 20 63 65 6c 6c s.** | cell
14754 20 63 6f 6e 74 65 6e 74 20 20 20 7c 20 20 20 7c content | |
14755 20 20 41 72 62 69 74 72 61 72 79 20 6f 72 64 65 Arbitrary orde
14756 72 20 69 6e 74 65 72 73 70 65 72 73 65 64 20 77 r interspersed w
14757 69 74 68 20 66 72 65 65 62 6c 6f 63 6b 73 2e 0a ith freeblocks..
14758 2a 2a 20 20 20 20 20 20 7c 20 61 72 65 61 20 20 ** | area
14759 20 20 20 20 20 20 20 20 20 7c 20 20 20 7c 20 20 | |
1475a 61 6e 64 20 66 72 65 65 20 73 70 61 63 65 20 66 and free space f
1475b 72 61 67 6d 65 6e 74 73 2e 0a 2a 2a 20 20 20 20 ragments..**
1475c 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d |-------------
1475d 2d 2d 2d 7c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 ---|.**.** The p
1475e 61 67 65 20 68 65 61 64 65 72 73 20 6c 6f 6f 6b age headers look
1475f 73 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a s like this:.**.
14760 2a 2a 20 20 20 4f 46 46 53 45 54 20 20 20 53 49 ** OFFSET SI
14761 5a 45 20 20 20 20 20 44 45 53 43 52 49 50 54 49 ZE DESCRIPTI
14762 4f 4e 0a 2a 2a 20 20 20 20 20 20 30 20 20 20 20 ON.** 0
14763 20 20 20 31 20 20 20 20 20 20 46 6c 61 67 73 2e 1 Flags.
14764 20 31 3a 20 69 6e 74 6b 65 79 2c 20 32 3a 20 7a 1: intkey, 2: z
14765 65 72 6f 64 61 74 61 2c 20 34 3a 20 6c 65 61 66 erodata, 4: leaf
14766 64 61 74 61 2c 20 38 3a 20 6c 65 61 66 0a 2a 2a data, 8: leaf.**
14767 20 20 20 20 20 20 31 20 20 20 20 20 20 20 32 20 1 2
14768 20 20 20 20 20 62 79 74 65 20 6f 66 66 73 65 74 byte offset
14769 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 66 72 to the first fr
1476a 65 65 62 6c 6f 63 6b 0a 2a 2a 20 20 20 20 20 20 eeblock.**
1476b 33 20 20 20 20 20 20 20 32 20 20 20 20 20 20 6e 3 2 n
1476c 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 6f umber of cells o
1476d 6e 20 74 68 69 73 20 70 61 67 65 0a 2a 2a 20 20 n this page.**
1476e 20 20 20 20 35 20 20 20 20 20 20 20 32 20 20 20 5 2
1476f 20 20 20 66 69 72 73 74 20 62 79 74 65 20 6f 66 first byte of
14770 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e the cell conten
14771 74 20 61 72 65 61 0a 2a 2a 20 20 20 20 20 20 37 t area.** 7
14772 20 20 20 20 20 20 20 31 20 20 20 20 20 20 6e 75 1 nu
14773 6d 62 65 72 20 6f 66 20 66 72 61 67 6d 65 6e 74 mber of fragment
14774 65 64 20 66 72 65 65 20 62 79 74 65 73 0a 2a 2a ed free bytes.**
14775 20 20 20 20 20 20 38 20 20 20 20 20 20 20 34 20 8 4
14776 20 20 20 20 20 52 69 67 68 74 20 63 68 69 6c 64 Right child
14777 20 28 74 68 65 20 50 74 72 28 4e 29 20 76 61 6c (the Ptr(N) val
14778 75 65 29 2e 20 20 4f 6d 69 74 74 65 64 20 6f 6e ue). Omitted on
14779 20 6c 65 61 76 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 leaves..**.** T
1477a 68 65 20 66 6c 61 67 73 20 64 65 66 69 6e 65 20 he flags define
1477b 74 68 65 20 66 6f 72 6d 61 74 20 6f 66 20 74 68 the format of th
1477c 69 73 20 62 74 72 65 65 20 70 61 67 65 2e 20 20 is btree page.
1477d 54 68 65 20 6c 65 61 66 20 66 6c 61 67 20 6d 65 The leaf flag me
1477e 61 6e 73 20 74 68 61 74 0a 2a 2a 20 74 68 69 73 ans that.** this
1477f 20 70 61 67 65 20 68 61 73 20 6e 6f 20 63 68 69 page has no chi
14780 6c 64 72 65 6e 2e 20 20 54 68 65 20 7a 65 72 6f ldren. The zero
14781 64 61 74 61 20 66 6c 61 67 20 6d 65 61 6e 73 20 data flag means
14782 74 68 61 74 20 74 68 69 73 20 70 61 67 65 20 63 that this page c
14783 61 72 72 69 65 73 0a 2a 2a 20 6f 6e 6c 79 20 6b arries.** only k
14784 65 79 73 20 61 6e 64 20 6e 6f 20 64 61 74 61 2e eys and no data.
14785 20 20 54 68 65 20 69 6e 74 6b 65 79 20 66 6c 61 The intkey fla
14786 67 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 g means that the
14787 20 6b 65 79 20 69 73 20 61 20 69 6e 74 65 67 65 key is a intege
14788 72 0a 2a 2a 20 77 68 69 63 68 20 69 73 20 73 74 r.** which is st
14789 6f 72 65 64 20 69 6e 20 74 68 65 20 6b 65 79 20 ored in the key
1478a 73 69 7a 65 20 65 6e 74 72 79 20 6f 66 20 74 68 size entry of th
1478b 65 20 63 65 6c 6c 20 68 65 61 64 65 72 20 72 61 e cell header ra
1478c 74 68 65 72 20 74 68 61 6e 20 69 6e 0a 2a 2a 20 ther than in.**
1478d 74 68 65 20 70 61 79 6c 6f 61 64 20 61 72 65 61 the payload area
1478e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 65 6c 6c ..**.** The cell
1478f 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79 20 62 pointer array b
14790 65 67 69 6e 73 20 6f 6e 20 74 68 65 20 66 69 72 egins on the fir
14791 73 74 20 62 79 74 65 20 61 66 74 65 72 20 74 68 st byte after th
14792 65 20 70 61 67 65 20 68 65 61 64 65 72 2e 0a 2a e page header..*
14793 2a 20 54 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74 * The cell point
14794 65 72 20 61 72 72 61 79 20 63 6f 6e 74 61 69 6e er array contain
14795 73 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 32 s zero or more 2
14796 2d 62 79 74 65 20 6e 75 6d 62 65 72 73 20 77 68 -byte numbers wh
14797 69 63 68 20 61 72 65 0a 2a 2a 20 6f 66 66 73 65 ich are.** offse
14798 74 73 20 66 72 6f 6d 20 74 68 65 20 62 65 67 69 ts from the begi
14799 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 70 61 67 nning of the pag
1479a 65 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 63 6f e to the cell co
1479b 6e 74 65 6e 74 20 69 6e 20 74 68 65 20 63 65 6c ntent in the cel
1479c 6c 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 61 72 65 l.** content are
1479d 61 2e 20 20 54 68 65 20 63 65 6c 6c 20 70 6f 69 a. The cell poi
1479e 6e 74 65 72 73 20 6f 63 63 75 72 20 69 6e 20 73 nters occur in s
1479f 6f 72 74 65 64 20 6f 72 64 65 72 2e 20 20 54 68 orted order. Th
147a0 65 20 73 79 73 74 65 6d 20 73 74 72 69 76 65 73 e system strives
147a1 0a 2a 2a 20 74 6f 20 6b 65 65 70 20 66 72 65 65 .** to keep free
147a2 20 73 70 61 63 65 20 61 66 74 65 72 20 74 68 65 space after the
147a3 20 6c 61 73 74 20 63 65 6c 6c 20 70 6f 69 6e 74 last cell point
147a4 65 72 20 73 6f 20 74 68 61 74 20 6e 65 77 20 63 er so that new c
147a5 65 6c 6c 73 20 63 61 6e 0a 2a 2a 20 62 65 20 65 ells can.** be e
147a6 61 73 69 6c 79 20 61 64 64 65 64 20 77 69 74 68 asily added with
147a7 6f 75 74 20 68 61 76 69 6e 67 20 74 6f 20 64 65 out having to de
147a8 66 72 61 67 6d 65 6e 74 20 74 68 65 20 70 61 67 fragment the pag
147a9 65 2e 0a 2a 2a 0a 2a 2a 20 43 65 6c 6c 20 63 6f e..**.** Cell co
147aa 6e 74 65 6e 74 20 69 73 20 73 74 6f 72 65 64 20 ntent is stored
147ab 61 74 20 74 68 65 20 76 65 72 79 20 65 6e 64 20 at the very end
147ac 6f 66 20 74 68 65 20 70 61 67 65 20 61 6e 64 20 of the page and
147ad 67 72 6f 77 73 20 74 6f 77 61 72 64 20 74 68 65 grows toward the
147ae 0a 2a 2a 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 .** beginning of
147af 20 74 68 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a the page..**.**
147b0 20 55 6e 75 73 65 64 20 73 70 61 63 65 20 77 69 Unused space wi
147b1 74 68 69 6e 20 74 68 65 20 63 65 6c 6c 20 63 6f thin the cell co
147b2 6e 74 65 6e 74 20 61 72 65 61 20 69 73 20 63 6f ntent area is co
147b3 6c 6c 65 63 74 65 64 20 69 6e 74 6f 20 61 20 6c llected into a l
147b4 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 0a 2a 2a inked list of.**
147b5 20 66 72 65 65 62 6c 6f 63 6b 73 2e 20 20 45 61 freeblocks. Ea
147b6 63 68 20 66 72 65 65 62 6c 6f 63 6b 20 69 73 20 ch freeblock is
147b7 61 74 20 6c 65 61 73 74 20 34 20 62 79 74 65 73 at least 4 bytes
147b8 20 69 6e 20 73 69 7a 65 2e 20 20 54 68 65 20 62 in size. The b
147b9 79 74 65 20 6f 66 66 73 65 74 0a 2a 2a 20 74 6f yte offset.** to
147ba 20 74 68 65 20 66 69 72 73 74 20 66 72 65 65 62 the first freeb
147bb 6c 6f 63 6b 20 69 73 20 67 69 76 65 6e 20 69 6e lock is given in
147bc 20 74 68 65 20 68 65 61 64 65 72 2e 20 20 46 72 the header. Fr
147bd 65 65 62 6c 6f 63 6b 73 20 6f 63 63 75 72 20 69 eeblocks occur i
147be 6e 0a 2a 2a 20 69 6e 63 72 65 61 73 69 6e 67 20 n.** increasing
147bf 6f 72 64 65 72 2e 20 20 42 65 63 61 75 73 65 20 order. Because
147c0 61 20 66 72 65 65 62 6c 6f 63 6b 20 6d 75 73 74 a freeblock must
147c1 20 62 65 20 61 74 20 6c 65 61 73 74 20 34 20 62 be at least 4 b
147c2 79 74 65 73 20 69 6e 20 73 69 7a 65 2c 0a 2a 2a ytes in size,.**
147c3 20 61 6e 79 20 67 72 6f 75 70 20 6f 66 20 33 20 any group of 3
147c4 6f 72 20 66 65 77 65 72 20 75 6e 75 73 65 64 20 or fewer unused
147c5 62 79 74 65 73 20 69 6e 20 74 68 65 20 63 65 6c bytes in the cel
147c6 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 63 l content area c
147c7 61 6e 6e 6f 74 0a 2a 2a 20 65 78 69 73 74 20 6f annot.** exist o
147c8 6e 20 74 68 65 20 66 72 65 65 62 6c 6f 63 6b 20 n the freeblock
147c9 63 68 61 69 6e 2e 20 20 41 20 67 72 6f 75 70 20 chain. A group
147ca 6f 66 20 33 20 6f 72 20 66 65 77 65 72 20 66 72 of 3 or fewer fr
147cb 65 65 20 62 79 74 65 73 20 69 73 20 63 61 6c 6c ee bytes is call
147cc 65 64 0a 2a 2a 20 61 20 66 72 61 67 6d 65 6e 74 ed.** a fragment
147cd 2e 20 20 54 68 65 20 74 6f 74 61 6c 20 6e 75 6d . The total num
147ce 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 ber of bytes in
147cf 61 6c 6c 20 66 72 61 67 6d 65 6e 74 73 20 69 73 all fragments is
147d0 20 72 65 63 6f 72 64 65 64 2e 0a 2a 2a 20 69 6e recorded..** in
147d1 20 74 68 65 20 70 61 67 65 20 68 65 61 64 65 72 the page header
147d2 20 61 74 20 6f 66 66 73 65 74 20 37 2e 0a 2a 2a at offset 7..**
147d3 0a 2a 2a 20 20 20 20 53 49 5a 45 20 20 20 20 44 .** SIZE D
147d4 45 53 43 52 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 ESCRIPTION.**
147d5 20 20 20 32 20 20 20 20 20 42 79 74 65 20 6f 66 2 Byte of
147d6 66 73 65 74 20 6f 66 20 74 68 65 20 6e 65 78 74 fset of the next
147d7 20 66 72 65 65 62 6c 6f 63 6b 0a 2a 2a 20 20 20 freeblock.**
147d8 20 20 20 32 20 20 20 20 20 42 79 74 65 73 20 69 2 Bytes i
147d9 6e 20 74 68 69 73 20 66 72 65 65 62 6c 6f 63 6b n this freeblock
147da 0a 2a 2a 0a 2a 2a 20 43 65 6c 6c 73 20 61 72 65 .**.** Cells are
147db 20 6f 66 20 76 61 72 69 61 62 6c 65 20 6c 65 6e of variable len
147dc 67 74 68 2e 20 20 43 65 6c 6c 73 20 61 72 65 20 gth. Cells are
147dd 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 63 65 stored in the ce
147de 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 ll content area
147df 61 74 0a 2a 2a 20 74 68 65 20 65 6e 64 20 6f 66 at.** the end of
147e0 20 74 68 65 20 70 61 67 65 2e 20 20 50 6f 69 6e the page. Poin
147e1 74 65 72 73 20 74 6f 20 74 68 65 20 63 65 6c 6c ters to the cell
147e2 73 20 61 72 65 20 69 6e 20 74 68 65 20 63 65 6c s are in the cel
147e3 6c 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79 0a l pointer array.
147e4 2a 2a 20 74 68 61 74 20 69 6d 6d 65 64 69 61 74 ** that immediat
147e5 65 6c 79 20 66 6f 6c 6c 6f 77 73 20 74 68 65 20 ely follows the
147e6 70 61 67 65 20 68 65 61 64 65 72 2e 20 20 43 65 page header. Ce
147e7 6c 6c 73 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 lls is not neces
147e8 73 61 72 69 6c 79 0a 2a 2a 20 63 6f 6e 74 69 67 sarily.** contig
147e9 75 6f 75 73 20 6f 72 20 69 6e 20 6f 72 64 65 72 uous or in order
147ea 2c 20 62 75 74 20 63 65 6c 6c 20 70 6f 69 6e 74 , but cell point
147eb 65 72 73 20 61 72 65 20 63 6f 6e 74 69 67 75 6f ers are contiguo
147ec 75 73 20 61 6e 64 20 69 6e 20 6f 72 64 65 72 2e us and in order.
147ed 0a 2a 2a 0a 2a 2a 20 43 65 6c 6c 20 63 6f 6e 74 .**.** Cell cont
147ee 65 6e 74 20 6d 61 6b 65 73 20 75 73 65 20 6f 66 ent makes use of
147ef 20 76 61 72 69 61 62 6c 65 20 6c 65 6e 67 74 68 variable length
147f0 20 69 6e 74 65 67 65 72 73 2e 20 20 41 20 76 61 integers. A va
147f1 72 69 61 62 6c 65 0a 2a 2a 20 6c 65 6e 67 74 68 riable.** length
147f2 20 69 6e 74 65 67 65 72 20 69 73 20 31 20 74 6f integer is 1 to
147f3 20 39 20 62 79 74 65 73 20 77 68 65 72 65 20 74 9 bytes where t
147f4 68 65 20 6c 6f 77 65 72 20 37 20 62 69 74 73 20 he lower 7 bits
147f5 6f 66 20 65 61 63 68 20 0a 2a 2a 20 62 79 74 65 of each .** byte
147f6 20 61 72 65 20 75 73 65 64 2e 20 20 54 68 65 20 are used. The
147f7 69 6e 74 65 67 65 72 20 63 6f 6e 73 69 73 74 73 integer consists
147f8 20 6f 66 20 61 6c 6c 20 62 79 74 65 73 20 74 68 of all bytes th
147f9 61 74 20 68 61 76 65 20 62 69 74 20 38 20 73 65 at have bit 8 se
147fa 74 20 61 6e 64 0a 2a 2a 20 74 68 65 20 66 69 72 t and.** the fir
147fb 73 74 20 62 79 74 65 20 77 69 74 68 20 62 69 74 st byte with bit
147fc 20 38 20 63 6c 65 61 72 2e 20 20 54 68 65 20 6d 8 clear. The m
147fd 6f 73 74 20 73 69 67 6e 69 66 69 63 61 6e 74 20 ost significant
147fe 62 79 74 65 20 6f 66 20 74 68 65 20 69 6e 74 65 byte of the inte
147ff 67 65 72 0a 2a 2a 20 61 70 70 65 61 72 73 20 66 ger.** appears f
14800 69 72 73 74 2e 20 20 41 20 76 61 72 69 61 62 6c irst. A variabl
14801 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 e-length integer
14802 20 6d 61 79 20 6e 6f 74 20 62 65 20 6d 6f 72 65 may not be more
14803 20 74 68 61 6e 20 39 20 62 79 74 65 73 20 6c 6f than 9 bytes lo
14804 6e 67 2e 0a 2a 2a 20 41 73 20 61 20 73 70 65 63 ng..** As a spec
14805 69 61 6c 20 63 61 73 65 2c 20 61 6c 6c 20 38 20 ial case, all 8
14806 62 79 74 65 73 20 6f 66 20 74 68 65 20 39 74 68 bytes of the 9th
14807 20 62 79 74 65 20 61 72 65 20 75 73 65 64 20 61 byte are used a
14808 73 20 64 61 74 61 2e 20 20 54 68 69 73 0a 2a 2a s data. This.**
14809 20 61 6c 6c 6f 77 73 20 61 20 36 34 2d 62 69 74 allows a 64-bit
1480a 20 69 6e 74 65 67 65 72 20 74 6f 20 62 65 20 65 integer to be e
1480b 6e 63 6f 64 65 64 20 69 6e 20 39 20 62 79 74 65 ncoded in 9 byte
1480c 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 30 78 30 30 s..**.** 0x00
1480d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1480e 20 20 20 20 20 20 62 65 63 6f 6d 65 73 20 20 30 becomes 0
1480f 78 30 30 30 30 30 30 30 30 0a 2a 2a 20 20 20 20 x00000000.**
14810 30 78 37 66 20 20 20 20 20 20 20 20 20 20 20 20 0x7f
14811 20 20 20 20 20 20 20 20 20 20 62 65 63 6f 6d 65 become
14812 73 20 20 30 78 30 30 30 30 30 30 37 66 0a 2a 2a s 0x0000007f.**
14813 20 20 20 20 30 78 38 31 20 30 78 30 30 20 20 20 0x81 0x00
14814 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 65 be
14815 63 6f 6d 65 73 20 20 30 78 30 30 30 30 30 30 38 comes 0x0000008
14816 30 0a 2a 2a 20 20 20 20 30 78 38 32 20 30 78 30 0.** 0x82 0x0
14817 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0
14818 20 20 62 65 63 6f 6d 65 73 20 20 30 78 30 30 30 becomes 0x000
14819 30 30 31 30 30 0a 2a 2a 20 20 20 20 30 78 38 30 00100.** 0x80
1481a 20 30 78 37 66 20 20 20 20 20 20 20 20 20 20 20 0x7f
1481b 20 20 20 20 20 20 62 65 63 6f 6d 65 73 20 20 30 becomes 0
1481c 78 30 30 30 30 30 30 37 66 0a 2a 2a 20 20 20 20 x0000007f.**
1481d 30 78 38 61 20 30 78 39 31 20 30 78 64 31 20 30 0x8a 0x91 0xd1 0
1481e 78 61 63 20 30 78 37 38 20 20 62 65 63 6f 6d 65 xac 0x78 become
1481f 73 20 20 30 78 31 32 33 34 35 36 37 38 0a 2a 2a s 0x12345678.**
14820 20 20 20 20 30 78 38 31 20 30 78 38 31 20 30 78 0x81 0x81 0x
14821 38 31 20 30 78 38 31 20 30 78 30 31 20 20 62 65 81 0x81 0x01 be
14822 63 6f 6d 65 73 20 20 30 78 31 30 32 30 34 30 38 comes 0x1020408
14823 31 0a 2a 2a 0a 2a 2a 20 56 61 72 69 61 62 6c 65 1.**.** Variable
14824 20 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 73 length integers
14825 20 61 72 65 20 75 73 65 64 20 66 6f 72 20 72 6f are used for ro
14826 77 69 64 73 20 61 6e 64 20 74 6f 20 68 6f 6c 64 wids and to hold
14827 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a the number of.*
14828 2a 20 62 79 74 65 73 20 6f 66 20 6b 65 79 20 61 * bytes of key a
14829 6e 64 20 64 61 74 61 20 69 6e 20 61 20 62 74 72 nd data in a btr
1482a 65 65 20 63 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 54 ee cell..**.** T
1482b 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 20 he content of a
1482c 63 65 6c 6c 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 cell looks like
1482d 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 53 this:.**.** S
1482e 49 5a 45 20 20 20 20 44 45 53 43 52 49 50 54 49 IZE DESCRIPTI
1482f 4f 4e 0a 2a 2a 20 20 20 20 20 20 34 20 20 20 20 ON.** 4
14830 20 50 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 Page number of
14831 74 68 65 20 6c 65 66 74 20 63 68 69 6c 64 2e 20 the left child.
14832 4f 6d 69 74 74 65 64 20 69 66 20 6c 65 61 66 20 Omitted if leaf
14833 66 6c 61 67 20 69 73 20 73 65 74 2e 0a 2a 2a 20 flag is set..**
14834 20 20 20 20 76 61 72 20 20 20 20 4e 75 6d 62 65 var Numbe
14835 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 64 61 r of bytes of da
14836 74 61 2e 20 4f 6d 69 74 74 65 64 20 69 66 20 74 ta. Omitted if t
14837 68 65 20 7a 65 72 6f 64 61 74 61 20 66 6c 61 67 he zerodata flag
14838 20 69 73 20 73 65 74 2e 0a 2a 2a 20 20 20 20 20 is set..**
14839 76 61 72 20 20 20 20 4e 75 6d 62 65 72 20 6f 66 var Number of
1483a 20 62 79 74 65 73 20 6f 66 20 6b 65 79 2e 20 4f bytes of key. O
1483b 72 20 74 68 65 20 6b 65 79 20 69 74 73 65 6c 66 r the key itself
1483c 20 69 66 20 69 6e 74 6b 65 79 20 66 6c 61 67 20 if intkey flag
1483d 69 73 20 73 65 74 2e 0a 2a 2a 20 20 20 20 20 20 is set..**
1483e 2a 20 20 20 20 20 50 61 79 6c 6f 61 64 0a 2a 2a * Payload.**
1483f 20 20 20 20 20 20 34 20 20 20 20 20 46 69 72 73 4 Firs
14840 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 6f 76 t page of the ov
14841 65 72 66 6c 6f 77 20 63 68 61 69 6e 2e 20 20 4f erflow chain. O
14842 6d 69 74 74 65 64 20 69 66 20 6e 6f 20 6f 76 65 mitted if no ove
14843 72 66 6c 6f 77 0a 2a 2a 0a 2a 2a 20 4f 76 65 72 rflow.**.** Over
14844 66 6c 6f 77 20 70 61 67 65 73 20 66 6f 72 6d 20 flow pages form
14845 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 2e 20 20 a linked list.
14846 45 61 63 68 20 70 61 67 65 20 65 78 63 65 70 74 Each page except
14847 20 74 68 65 20 6c 61 73 74 20 69 73 20 63 6f 6d the last is com
14848 70 6c 65 74 65 6c 79 0a 2a 2a 20 66 69 6c 6c 65 pletely.** fille
14849 64 20 77 69 74 68 20 64 61 74 61 20 28 70 61 67 d with data (pag
1484a 65 73 69 7a 65 20 2d 20 34 20 62 79 74 65 73 29 esize - 4 bytes)
1484b 2e 20 20 54 68 65 20 6c 61 73 74 20 70 61 67 65 . The last page
1484c 20 63 61 6e 20 68 61 76 65 20 61 73 20 6c 69 74 can have as lit
1484d 74 6c 65 0a 2a 2a 20 61 73 20 31 20 62 79 74 65 tle.** as 1 byte
1484e 20 6f 66 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 of data..**.**
1484f 20 20 20 53 49 5a 45 20 20 20 20 44 45 53 43 52 SIZE DESCR
14850 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 20 34 IPTION.** 4
14851 20 20 20 20 20 50 61 67 65 20 6e 75 6d 62 65 72 Page number
14852 20 6f 66 20 6e 65 78 74 20 6f 76 65 72 66 6c 6f of next overflo
14853 77 20 70 61 67 65 0a 2a 2a 20 20 20 20 20 20 2a w page.** *
14854 20 20 20 20 20 44 61 74 61 0a 2a 2a 0a 2a 2a 20 Data.**.**
14855 46 72 65 65 6c 69 73 74 20 70 61 67 65 73 20 63 Freelist pages c
14856 6f 6d 65 20 69 6e 20 74 77 6f 20 73 75 62 74 79 ome in two subty
14857 70 65 73 3a 20 74 72 75 6e 6b 20 70 61 67 65 73 pes: trunk pages
14858 20 61 6e 64 20 6c 65 61 66 20 70 61 67 65 73 2e and leaf pages.
14859 20 20 54 68 65 0a 2a 2a 20 66 69 6c 65 20 68 65 The.** file he
1485a 61 64 65 72 20 70 6f 69 6e 74 73 20 74 6f 20 74 ader points to t
1485b 68 65 20 66 69 72 73 74 20 69 6e 20 61 20 6c 69 he first in a li
1485c 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 74 72 75 nked list of tru
1485d 6e 6b 20 70 61 67 65 2e 20 20 45 61 63 68 20 74 nk page. Each t
1485e 72 75 6e 6b 0a 2a 2a 20 70 61 67 65 20 70 6f 69 runk.** page poi
1485f 6e 74 73 20 74 6f 20 6d 75 6c 74 69 70 6c 65 20 nts to multiple
14860 6c 65 61 66 20 70 61 67 65 73 2e 20 20 54 68 65 leaf pages. The
14861 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 20 6c 65 content of a le
14862 61 66 20 70 61 67 65 20 69 73 0a 2a 2a 20 75 6e af page is.** un
14863 73 70 65 63 69 66 69 65 64 2e 20 20 41 20 74 72 specified. A tr
14864 75 6e 6b 20 70 61 67 65 20 6c 6f 6f 6b 73 20 6c unk page looks l
14865 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 ike this:.**.**
14866 20 20 20 53 49 5a 45 20 20 20 20 44 45 53 43 52 SIZE DESCR
14867 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 20 34 IPTION.** 4
14868 20 20 20 20 20 50 61 67 65 20 6e 75 6d 62 65 72 Page number
14869 20 6f 66 20 6e 65 78 74 20 74 72 75 6e 6b 20 70 of next trunk p
1486a 61 67 65 0a 2a 2a 20 20 20 20 20 20 34 20 20 20 age.** 4
1486b 20 20 4e 75 6d 62 65 72 20 6f 66 20 6c 65 61 66 Number of leaf
1486c 20 70 6f 69 6e 74 65 72 73 20 6f 6e 20 74 68 69 pointers on thi
1486d 73 20 70 61 67 65 0a 2a 2a 20 20 20 20 20 20 2a s page.** *
1486e 20 20 20 20 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 zero or mor
1486f 65 20 70 61 67 65 73 20 6e 75 6d 62 65 72 73 20 e pages numbers
14870 6f 66 20 6c 65 61 76 65 73 0a 2a 2f 0a 0a 0a 2f of leaves.*/.../
14871 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * The following
14872 76 61 6c 75 65 20 69 73 20 74 68 65 20 6d 61 78 value is the max
14873 69 6d 75 6d 20 63 65 6c 6c 20 73 69 7a 65 20 61 imum cell size a
14874 73 73 75 6d 69 6e 67 20 61 20 6d 61 78 69 6d 75 ssuming a maximu
14875 6d 20 70 61 67 65 0a 2a 2a 20 73 69 7a 65 20 67 m page.** size g
14876 69 76 65 20 61 62 6f 76 65 2e 0a 2a 2f 0a 23 64 ive above..*/.#d
14877 65 66 69 6e 65 20 4d 58 5f 43 45 4c 4c 5f 53 49 efine MX_CELL_SI
14878 5a 45 28 70 42 74 29 20 20 28 70 42 74 2d 3e 70 ZE(pBt) (pBt->p
14879 61 67 65 53 69 7a 65 2d 38 29 0a 0a 2f 2a 20 54 ageSize-8)../* T
1487a 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 he maximum numbe
1487b 72 20 6f 66 20 63 65 6c 6c 73 20 6f 6e 20 61 20 r of cells on a
1487c 73 69 6e 67 6c 65 20 70 61 67 65 20 6f 66 20 74 single page of t
1487d 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 he database. Th
1487e 69 73 0a 2a 2a 20 61 73 73 75 6d 65 73 20 61 20 is.** assumes a
1487f 6d 69 6e 69 6d 75 6d 20 63 65 6c 6c 20 73 69 7a minimum cell siz
14880 65 20 6f 66 20 36 20 62 79 74 65 73 20 20 28 34 e of 6 bytes (4
14881 20 62 79 74 65 73 20 66 6f 72 20 74 68 65 20 63 bytes for the c
14882 65 6c 6c 20 69 74 73 65 6c 66 0a 2a 2a 20 70 6c ell itself.** pl
14883 75 73 20 32 20 62 79 74 65 73 20 66 6f 72 20 74 us 2 bytes for t
14884 68 65 20 69 6e 64 65 78 20 74 6f 20 74 68 65 20 he index to the
14885 63 65 6c 6c 20 69 6e 20 74 68 65 20 70 61 67 65 cell in the page
14886 20 68 65 61 64 65 72 29 2e 20 20 53 75 63 68 0a header). Such.
14887 2a 2a 20 73 6d 61 6c 6c 20 63 65 6c 6c 73 20 77 ** small cells w
14888 69 6c 6c 20 62 65 20 72 61 72 65 2c 20 62 75 74 ill be rare, but
14889 20 74 68 65 79 20 61 72 65 20 70 6f 73 73 69 62 they are possib
1488a 6c 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d le..*/.#define M
1488b 58 5f 43 45 4c 4c 28 70 42 74 29 20 28 28 70 42 X_CELL(pBt) ((pB
1488c 74 2d 3e 70 61 67 65 53 69 7a 65 2d 38 29 2f 36 t->pageSize-8)/6
1488d 29 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 64 65 )../* Forward de
1488e 63 6c 61 72 61 74 69 6f 6e 73 20 2a 2f 0a 74 79 clarations */.ty
1488f 70 65 64 65 66 20 73 74 72 75 63 74 20 4d 65 6d pedef struct Mem
14890 50 61 67 65 20 4d 65 6d 50 61 67 65 3b 0a 74 79 Page MemPage;.ty
14891 70 65 64 65 66 20 73 74 72 75 63 74 20 42 74 4c pedef struct BtL
14892 6f 63 6b 20 42 74 4c 6f 63 6b 3b 0a 0a 2f 2a 0a ock BtLock;../*.
14893 2a 2a 20 54 68 69 73 20 69 73 20 61 20 6d 61 67 ** This is a mag
14894 69 63 20 73 74 72 69 6e 67 20 74 68 61 74 20 61 ic string that a
14895 70 70 65 61 72 73 20 61 74 20 74 68 65 20 62 65 ppears at the be
14896 67 69 6e 6e 69 6e 67 20 6f 66 20 65 76 65 72 79 ginning of every
14897 0a 2a 2a 20 53 51 4c 69 74 65 20 64 61 74 61 62 .** SQLite datab
14898 61 73 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 ase in order to
14899 69 64 65 6e 74 69 66 79 20 74 68 65 20 66 69 6c identify the fil
1489a 65 20 61 73 20 61 20 72 65 61 6c 20 64 61 74 61 e as a real data
1489b 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 59 6f 75 20 base..**.** You
1489c 63 61 6e 20 63 68 61 6e 67 65 20 74 68 69 73 20 can change this
1489d 76 61 6c 75 65 20 61 74 20 63 6f 6d 70 69 6c 65 value at compile
1489e 2d 74 69 6d 65 20 62 79 20 73 70 65 63 69 66 79 -time by specify
1489f 69 6e 67 20 61 0a 2a 2a 20 2d 44 53 51 4c 49 54 ing a.** -DSQLIT
148a0 45 5f 46 49 4c 45 5f 48 45 41 44 45 52 3d 22 2e E_FILE_HEADER=".
148a1 2e 2e 22 20 6f 6e 20 74 68 65 20 63 6f 6d 70 69 .." on the compi
148a2 6c 65 72 20 63 6f 6d 6d 61 6e 64 2d 6c 69 6e 65 ler command-line
148a3 2e 20 20 54 68 65 0a 2a 2a 20 68 65 61 64 65 72 . The.** header
148a4 20 6d 75 73 74 20 62 65 20 65 78 61 63 74 6c 79 must be exactly
148a5 20 31 36 20 62 79 74 65 73 20 69 6e 63 6c 75 64 16 bytes includ
148a6 69 6e 67 20 74 68 65 20 7a 65 72 6f 2d 74 65 72 ing the zero-ter
148a7 6d 69 6e 61 74 6f 72 20 73 6f 0a 2a 2a 20 74 68 minator so.** th
148a8 65 20 73 74 72 69 6e 67 20 69 74 73 65 6c 66 20 e string itself
148a9 73 68 6f 75 6c 64 20 62 65 20 31 35 20 63 68 61 should be 15 cha
148aa 72 61 63 74 65 72 73 20 6c 6f 6e 67 2e 20 20 49 racters long. I
148ab 66 20 79 6f 75 20 63 68 61 6e 67 65 0a 2a 2a 20 f you change.**
148ac 74 68 65 20 68 65 61 64 65 72 2c 20 74 68 65 6e the header, then
148ad 20 79 6f 75 72 20 63 75 73 74 6f 6d 20 6c 69 62 your custom lib
148ae 72 61 72 79 20 77 69 6c 6c 20 6e 6f 74 20 62 65 rary will not be
148af 20 61 62 6c 65 20 74 6f 20 72 65 61 64 20 0a 2a able to read .*
148b0 2a 20 64 61 74 61 62 61 73 65 73 20 67 65 6e 65 * databases gene
148b1 72 61 74 65 64 20 62 79 20 74 68 65 20 73 74 61 rated by the sta
148b2 6e 64 61 72 64 20 74 6f 6f 6c 73 20 61 6e 64 20 ndard tools and
148b3 74 68 65 20 73 74 61 6e 64 61 72 64 20 74 6f 6f the standard too
148b4 6c 73 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 62 ls.** will not b
148b5 65 20 61 62 6c 65 20 74 6f 20 72 65 61 64 20 64 e able to read d
148b6 61 74 61 62 61 73 65 73 20 63 72 65 61 74 65 64 atabases created
148b7 20 62 79 20 79 6f 75 72 20 63 75 73 74 6f 6d 20 by your custom
148b8 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 23 69 66 6e library..*/.#ifn
148b9 64 65 66 20 53 51 4c 49 54 45 5f 46 49 4c 45 5f def SQLITE_FILE_
148ba 48 45 41 44 45 52 20 2f 2a 20 31 32 33 34 35 36 HEADER /* 123456
148bb 37 38 39 20 31 32 33 34 35 36 20 2a 2f 0a 23 20 789 123456 */.#
148bc 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 define SQLITE_F
148bd 49 4c 45 5f 48 45 41 44 45 52 20 22 53 51 4c 69 ILE_HEADER "SQLi
148be 74 65 20 66 6f 72 6d 61 74 20 33 22 0a 23 65 6e te format 3".#en
148bf 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 50 61 67 65 20 dif../*.** Page
148c0 74 79 70 65 20 66 6c 61 67 73 2e 20 20 41 6e 20 type flags. An
148c1 4f 52 65 64 20 63 6f 6d 62 69 6e 61 74 69 6f 6e ORed combination
148c2 20 6f 66 20 74 68 65 73 65 20 66 6c 61 67 73 20 of these flags
148c3 61 70 70 65 61 72 20 61 73 20 74 68 65 0a 2a 2a appear as the.**
148c4 20 66 69 72 73 74 20 62 79 74 65 20 6f 66 20 6f first byte of o
148c5 6e 2d 64 69 73 6b 20 69 6d 61 67 65 20 6f 66 20 n-disk image of
148c6 65 76 65 72 79 20 42 54 72 65 65 20 70 61 67 65 every BTree page
148c7 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 54 46 ..*/.#define PTF
148c8 5f 49 4e 54 4b 45 59 20 20 20 20 30 78 30 31 0a _INTKEY 0x01.
148c9 23 64 65 66 69 6e 65 20 50 54 46 5f 5a 45 52 4f #define PTF_ZERO
148ca 44 41 54 41 20 20 30 78 30 32 0a 23 64 65 66 69 DATA 0x02.#defi
148cb 6e 65 20 50 54 46 5f 4c 45 41 46 44 41 54 41 20 ne PTF_LEAFDATA
148cc 20 30 78 30 34 0a 23 64 65 66 69 6e 65 20 50 54 0x04.#define PT
148cd 46 5f 4c 45 41 46 20 20 20 20 20 20 30 78 30 38 F_LEAF 0x08
148ce 0a 0a 2f 2a 0a 2a 2a 20 41 73 20 65 61 63 68 20 ../*.** As each
148cf 70 61 67 65 20 6f 66 20 74 68 65 20 66 69 6c 65 page of the file
148d0 20 69 73 20 6c 6f 61 64 65 64 20 69 6e 74 6f 20 is loaded into
148d1 6d 65 6d 6f 72 79 2c 20 61 6e 20 69 6e 73 74 61 memory, an insta
148d2 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f nce of the follo
148d3 77 69 6e 67 0a 2a 2a 20 73 74 72 75 63 74 75 72 wing.** structur
148d4 65 20 69 73 20 61 70 70 65 6e 64 65 64 20 61 6e e is appended an
148d5 64 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f d initialized to
148d6 20 7a 65 72 6f 2e 20 20 54 68 69 73 20 73 74 72 zero. This str
148d7 75 63 74 75 72 65 20 73 74 6f 72 65 73 0a 2a 2a ucture stores.**
148d8 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f information abo
148d9 75 74 20 74 68 65 20 70 61 67 65 20 74 68 61 74 ut the page that
148da 20 69 73 20 64 65 63 6f 64 65 64 20 66 72 6f 6d is decoded from
148db 20 74 68 65 20 72 61 77 20 66 69 6c 65 20 70 61 the raw file pa
148dc 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 50 ge..**.** The pP
148dd 61 72 65 6e 74 20 66 69 65 6c 64 20 70 6f 69 6e arent field poin
148de 74 73 20 62 61 63 6b 20 74 6f 20 74 68 65 20 70 ts back to the p
148df 61 72 65 6e 74 20 70 61 67 65 2e 20 20 54 68 69 arent page. Thi
148e0 73 20 61 6c 6c 6f 77 73 20 75 73 20 74 6f 0a 2a s allows us to.*
148e1 2a 20 77 61 6c 6b 20 75 70 20 74 68 65 20 42 54 * walk up the BT
148e2 72 65 65 20 66 72 6f 6d 20 61 6e 79 20 6c 65 61 ree from any lea
148e3 66 20 74 6f 20 74 68 65 20 72 6f 6f 74 2e 20 20 f to the root.
148e4 43 61 72 65 20 6d 75 73 74 20 62 65 20 74 61 6b Care must be tak
148e5 65 6e 20 74 6f 0a 2a 2a 20 75 6e 72 65 66 28 29 en to.** unref()
148e6 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 the parent page
148e7 20 70 6f 69 6e 74 65 72 20 77 68 65 6e 20 74 68 pointer when th
148e8 69 73 20 70 61 67 65 20 69 73 20 6e 6f 20 6c 6f is page is no lo
148e9 6e 67 65 72 20 72 65 66 65 72 65 6e 63 65 64 2e nger referenced.
148ea 0a 2a 2a 20 54 68 65 20 70 61 67 65 44 65 73 74 .** The pageDest
148eb 72 75 63 74 6f 72 28 29 20 72 6f 75 74 69 6e 65 ructor() routine
148ec 20 68 61 6e 64 6c 65 73 20 74 68 61 74 20 63 68 handles that ch
148ed 6f 72 65 2e 0a 2a 2a 0a 2a 2a 20 41 63 63 65 73 ore..**.** Acces
148ee 73 20 74 6f 20 61 6c 6c 20 66 69 65 6c 64 73 20 s to all fields
148ef 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 of this structur
148f0 65 20 69 73 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 e is controlled
148f1 62 79 20 74 68 65 20 6d 75 74 65 78 0a 2a 2a 20 by the mutex.**
148f2 73 74 6f 72 65 64 20 69 6e 20 4d 65 6d 50 61 67 stored in MemPag
148f3 65 2e 70 42 74 2d 3e 6d 75 74 65 78 2e 0a 2a 2f e.pBt->mutex..*/
148f4 0a 73 74 72 75 63 74 20 4d 65 6d 50 61 67 65 20 .struct MemPage
148f5 7b 0a 20 20 75 38 20 69 73 49 6e 69 74 3b 20 20 {. u8 isInit;
148f6 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
148f7 20 69 66 20 70 72 65 76 69 6f 75 73 6c 79 20 69 if previously i
148f8 6e 69 74 69 61 6c 69 7a 65 64 2e 20 4d 55 53 54 nitialized. MUST
148f9 20 42 45 20 46 49 52 53 54 21 20 2a 2f 0a 20 20 BE FIRST! */.
148fa 75 38 20 6e 4f 76 65 72 66 6c 6f 77 3b 20 20 20 u8 nOverflow;
148fb 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
148fc 66 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 20 f overflow cell
148fd 62 6f 64 69 65 73 20 69 6e 20 61 43 65 6c 6c 5b bodies in aCell[
148fe 5d 20 2a 2f 0a 20 20 75 38 20 69 6e 74 4b 65 79 ] */. u8 intKey
148ff 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 ; /* T
14900 72 75 65 20 69 66 20 69 6e 74 6b 65 79 20 66 6c rue if intkey fl
14901 61 67 20 69 73 20 73 65 74 20 2a 2f 0a 20 20 75 ag is set */. u
14902 38 20 6c 65 61 66 3b 20 20 20 20 20 20 20 20 20 8 leaf;
14903 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 6c /* True if l
14904 65 61 66 20 66 6c 61 67 20 69 73 20 73 65 74 20 eaf flag is set
14905 2a 2f 0a 20 20 75 38 20 68 61 73 44 61 74 61 3b */. u8 hasData;
14906 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 /* Tru
14907 65 20 69 66 20 74 68 69 73 20 70 61 67 65 20 73 e if this page s
14908 74 6f 72 65 73 20 64 61 74 61 20 2a 2f 0a 20 20 tores data */.
14909 75 38 20 68 64 72 4f 66 66 73 65 74 3b 20 20 20 u8 hdrOffset;
1490a 20 20 20 20 20 2f 2a 20 31 30 30 20 66 6f 72 20 /* 100 for
1490b 70 61 67 65 20 31 2e 20 20 30 20 6f 74 68 65 72 page 1. 0 other
1490c 77 69 73 65 20 2a 2f 0a 20 20 75 38 20 63 68 69 wise */. u8 chi
1490d 6c 64 50 74 72 53 69 7a 65 3b 20 20 20 20 20 2f ldPtrSize; /
1490e 2a 20 30 20 69 66 20 6c 65 61 66 3d 3d 31 2e 20 * 0 if leaf==1.
1490f 20 34 20 69 66 20 6c 65 61 66 3d 3d 30 20 2a 2f 4 if leaf==0 */
14910 0a 20 20 75 31 36 20 6d 61 78 4c 6f 63 61 6c 3b . u16 maxLocal;
14911 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 /* Copy
14912 6f 66 20 42 74 53 68 61 72 65 64 2e 6d 61 78 4c of BtShared.maxL
14913 6f 63 61 6c 20 6f 72 20 42 74 53 68 61 72 65 64 ocal or BtShared
14914 2e 6d 61 78 4c 65 61 66 20 2a 2f 0a 20 20 75 31 .maxLeaf */. u1
14915 36 20 6d 69 6e 4c 6f 63 61 6c 3b 20 20 20 20 20 6 minLocal;
14916 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 42 74 /* Copy of Bt
14917 53 68 61 72 65 64 2e 6d 69 6e 4c 6f 63 61 6c 20 Shared.minLocal
14918 6f 72 20 42 74 53 68 61 72 65 64 2e 6d 69 6e 4c or BtShared.minL
14919 65 61 66 20 2a 2f 0a 20 20 75 31 36 20 63 65 6c eaf */. u16 cel
1491a 6c 4f 66 66 73 65 74 3b 20 20 20 20 20 20 2f 2a lOffset; /*
1491b 20 49 6e 64 65 78 20 69 6e 20 61 44 61 74 61 20 Index in aData
1491c 6f 66 20 66 69 72 73 74 20 63 65 6c 6c 20 70 6f of first cell po
1491d 69 6e 74 65 72 20 2a 2f 0a 20 20 75 31 36 20 6e inter */. u16 n
1491e 46 72 65 65 3b 20 20 20 20 20 20 20 20 20 20 20 Free;
1491f 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 72 65 /* Number of fre
14920 65 20 62 79 74 65 73 20 6f 6e 20 74 68 65 20 70 e bytes on the p
14921 61 67 65 20 2a 2f 0a 20 20 75 31 36 20 6e 43 65 age */. u16 nCe
14922 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a ll; /*
14923 20 4e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 Number of cells
14924 20 6f 6e 20 74 68 69 73 20 70 61 67 65 2c 20 6c on this page, l
14925 6f 63 61 6c 20 61 6e 64 20 6f 76 66 6c 20 2a 2f ocal and ovfl */
14926 0a 20 20 75 31 36 20 6d 61 73 6b 50 61 67 65 3b . u16 maskPage;
14927 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 /* Mask
14928 66 6f 72 20 70 61 67 65 20 6f 66 66 73 65 74 20 for page offset
14929 2a 2f 0a 20 20 73 74 72 75 63 74 20 5f 4f 76 66 */. struct _Ovf
1492a 6c 43 65 6c 6c 20 7b 20 20 20 2f 2a 20 43 65 6c lCell { /* Cel
1492b 6c 73 20 74 68 61 74 20 77 69 6c 6c 20 6e 6f 74 ls that will not
1492c 20 66 69 74 20 6f 6e 20 61 44 61 74 61 5b 5d 20 fit on aData[]
1492d 2a 2f 0a 20 20 20 20 75 38 20 2a 70 43 65 6c 6c */. u8 *pCell
1492e 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f ; /* Po
1492f 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20 62 6f inters to the bo
14930 64 79 20 6f 66 20 74 68 65 20 6f 76 65 72 66 6c dy of the overfl
14931 6f 77 20 63 65 6c 6c 20 2a 2f 0a 20 20 20 20 75 ow cell */. u
14932 31 36 20 69 64 78 3b 20 20 20 20 20 20 20 20 20 16 idx;
14933 20 20 20 2f 2a 20 49 6e 73 65 72 74 20 74 68 69 /* Insert thi
14934 73 20 63 65 6c 6c 20 62 65 66 6f 72 65 20 69 64 s cell before id
14935 78 2d 74 68 20 6e 6f 6e 2d 6f 76 65 72 66 6c 6f x-th non-overflo
14936 77 20 63 65 6c 6c 20 2a 2f 0a 20 20 7d 20 61 4f w cell */. } aO
14937 76 66 6c 5b 35 5d 3b 0a 20 20 42 74 53 68 61 72 vfl[5];. BtShar
14938 65 64 20 2a 70 42 74 3b 20 20 20 20 20 20 20 2f ed *pBt; /
14939 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 42 74 53 * Pointer to BtS
1493a 68 61 72 65 64 20 74 68 61 74 20 74 68 69 73 20 hared that this
1493b 70 61 67 65 20 69 73 20 70 61 72 74 20 6f 66 20 page is part of
1493c 2a 2f 0a 20 20 75 38 20 2a 61 44 61 74 61 3b 20 */. u8 *aData;
1493d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 /* Poi
1493e 6e 74 65 72 20 74 6f 20 64 69 73 6b 20 69 6d 61 nter to disk ima
1493f 67 65 20 6f 66 20 74 68 65 20 70 61 67 65 20 64 ge of the page d
14940 61 74 61 20 2a 2f 0a 20 20 44 62 50 61 67 65 20 ata */. DbPage
14941 2a 70 44 62 50 61 67 65 3b 20 20 20 20 20 2f 2a *pDbPage; /*
14942 20 50 61 67 65 72 20 70 61 67 65 20 68 61 6e 64 Pager page hand
14943 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e le */. Pgno pgn
14944 6f 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 o; /*
14945 50 61 67 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 Page number for
14946 74 68 69 73 20 70 61 67 65 20 2a 2f 0a 7d 3b 0a this page */.};.
14947 0a 2f 2a 0a 2a 2a 20 54 68 65 20 69 6e 2d 6d 65 ./*.** The in-me
14948 6d 6f 72 79 20 69 6d 61 67 65 20 6f 66 20 61 20 mory image of a
14949 64 69 73 6b 20 70 61 67 65 20 68 61 73 20 74 68 disk page has th
1494a 65 20 61 75 78 69 6c 69 61 72 79 20 69 6e 66 6f e auxiliary info
1494b 72 6d 61 74 69 6f 6e 20 61 70 70 65 6e 64 65 64 rmation appended
1494c 0a 2a 2a 20 74 6f 20 74 68 65 20 65 6e 64 2e 20 .** to the end.
1494d 20 45 58 54 52 41 5f 53 49 5a 45 20 69 73 20 74 EXTRA_SIZE is t
1494e 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 he number of byt
1494f 65 73 20 6f 66 20 73 70 61 63 65 20 6e 65 65 64 es of space need
14950 65 64 20 74 6f 20 68 6f 6c 64 0a 2a 2a 20 74 68 ed to hold.** th
14951 61 74 20 65 78 74 72 61 20 69 6e 66 6f 72 6d 61 at extra informa
14952 74 69 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 tion..*/.#define
14953 20 45 58 54 52 41 5f 53 49 5a 45 20 73 69 7a 65 EXTRA_SIZE size
14954 6f 66 28 4d 65 6d 50 61 67 65 29 0a 0a 2f 2a 0a of(MemPage)../*.
14955 2a 2a 20 41 20 6c 69 6e 6b 65 64 20 6c 69 73 74 ** A linked list
14956 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e of the followin
14957 67 20 73 74 72 75 63 74 75 72 65 73 20 69 73 20 g structures is
14958 73 74 6f 72 65 64 20 61 74 20 42 74 53 68 61 72 stored at BtShar
14959 65 64 2e 70 4c 6f 63 6b 2e 0a 2a 2a 20 4c 6f 63 ed.pLock..** Loc
1495a 6b 73 20 61 72 65 20 61 64 64 65 64 20 28 6f 72 ks are added (or
1495b 20 75 70 67 72 61 64 65 64 20 66 72 6f 6d 20 52 upgraded from R
1495c 45 41 44 5f 4c 4f 43 4b 20 74 6f 20 57 52 49 54 EAD_LOCK to WRIT
1495d 45 5f 4c 4f 43 4b 29 20 77 68 65 6e 20 61 20 63 E_LOCK) when a c
1495e 75 72 73 6f 72 20 0a 2a 2a 20 69 73 20 6f 70 65 ursor .** is ope
1495f 6e 65 64 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 ned on the table
14960 20 77 69 74 68 20 72 6f 6f 74 20 70 61 67 65 20 with root page
14961 42 74 53 68 61 72 65 64 2e 69 54 61 62 6c 65 2e BtShared.iTable.
14962 20 4c 6f 63 6b 73 20 61 72 65 20 72 65 6d 6f 76 Locks are remov
14963 65 64 0a 2a 2a 20 66 72 6f 6d 20 74 68 69 73 20 ed.** from this
14964 6c 69 73 74 20 77 68 65 6e 20 61 20 74 72 61 6e list when a tran
14965 73 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 saction is commi
14966 74 74 65 64 20 6f 72 20 72 6f 6c 6c 65 64 20 62 tted or rolled b
14967 61 63 6b 2c 20 6f 72 20 77 68 65 6e 0a 2a 2a 20 ack, or when.**
14968 61 20 62 74 72 65 65 20 68 61 6e 64 6c 65 20 69 a btree handle i
14969 73 20 63 6c 6f 73 65 64 2e 0a 2a 2f 0a 73 74 72 s closed..*/.str
1496a 75 63 74 20 42 74 4c 6f 63 6b 20 7b 0a 20 20 42 uct BtLock {. B
1496b 74 72 65 65 20 2a 70 42 74 72 65 65 3b 20 20 20 tree *pBtree;
1496c 20 20 20 20 20 2f 2a 20 42 74 72 65 65 20 68 61 /* Btree ha
1496d 6e 64 6c 65 20 68 6f 6c 64 69 6e 67 20 74 68 69 ndle holding thi
1496e 73 20 6c 6f 63 6b 20 2a 2f 0a 20 20 50 67 6e 6f s lock */. Pgno
1496f 20 69 54 61 62 6c 65 3b 20 20 20 20 20 20 20 20 iTable;
14970 20 20 2f 2a 20 52 6f 6f 74 20 70 61 67 65 20 6f /* Root page o
14971 66 20 74 61 62 6c 65 20 2a 2f 0a 20 20 75 38 20 f table */. u8
14972 65 4c 6f 63 6b 3b 20 20 20 20 20 20 20 20 20 20 eLock;
14973 20 20 20 2f 2a 20 52 45 41 44 5f 4c 4f 43 4b 20 /* READ_LOCK
14974 6f 72 20 57 52 49 54 45 5f 4c 4f 43 4b 20 2a 2f or WRITE_LOCK */
14975 0a 20 20 42 74 4c 6f 63 6b 20 2a 70 4e 65 78 74 . BtLock *pNext
14976 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 ; /* Next
14977 20 69 6e 20 42 74 53 68 61 72 65 64 2e 70 4c 6f in BtShared.pLo
14978 63 6b 20 6c 69 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f ck list */.};../
14979 2a 20 43 61 6e 64 69 64 61 74 65 20 76 61 6c 75 * Candidate valu
1497a 65 73 20 66 6f 72 20 42 74 4c 6f 63 6b 2e 65 4c es for BtLock.eL
1497b 6f 63 6b 20 2a 2f 0a 23 64 65 66 69 6e 65 20 52 ock */.#define R
1497c 45 41 44 5f 4c 4f 43 4b 20 20 20 20 20 31 0a 23 EAD_LOCK 1.#
1497d 64 65 66 69 6e 65 20 57 52 49 54 45 5f 4c 4f 43 define WRITE_LOC
1497e 4b 20 20 20 20 32 0a 0a 2f 2a 20 41 20 42 74 72 K 2../* A Btr
1497f 65 65 20 68 61 6e 64 6c 65 0a 2a 2a 0a 2a 2a 20 ee handle.**.**
14980 41 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 A database conne
14981 63 74 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 61 ction contains a
14982 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 pointer to an i
14983 6e 73 74 61 6e 63 65 20 6f 66 0a 2a 2a 20 74 68 nstance of.** th
14984 69 73 20 6f 62 6a 65 63 74 20 66 6f 72 20 65 76 is object for ev
14985 65 72 79 20 64 61 74 61 62 61 73 65 20 66 69 6c ery database fil
14986 65 20 74 68 61 74 20 69 74 20 68 61 73 20 6f 70 e that it has op
14987 65 6e 2e 20 20 54 68 69 73 20 73 74 72 75 63 74 en. This struct
14988 75 72 65 0a 2a 2a 20 69 73 20 6f 70 61 71 75 65 ure.** is opaque
14989 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 to the database
1498a 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 54 68 connection. Th
1498b 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 e database conne
1498c 63 74 69 6f 6e 20 63 61 6e 6e 6f 74 0a 2a 2a 20 ction cannot.**
1498d 73 65 65 20 74 68 65 20 69 6e 74 65 72 6e 61 6c see the internal
1498e 73 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 s of this struct
1498f 75 72 65 20 61 6e 64 20 6f 6e 6c 79 20 64 65 61 ure and only dea
14990 6c 73 20 77 69 74 68 20 70 6f 69 6e 74 65 72 73 ls with pointers
14991 20 74 6f 0a 2a 2a 20 74 68 69 73 20 73 74 72 75 to.** this stru
14992 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 cture..**.** For
14993 20 73 6f 6d 65 20 64 61 74 61 62 61 73 65 20 66 some database f
14994 69 6c 65 73 2c 20 74 68 65 20 73 61 6d 65 20 75 iles, the same u
14995 6e 64 65 72 6c 79 69 6e 67 20 64 61 74 61 62 61 nderlying databa
14996 73 65 20 63 61 63 68 65 20 6d 69 67 68 74 20 62 se cache might b
14997 65 20 0a 2a 2a 20 73 68 61 72 65 64 20 62 65 74 e .** shared bet
14998 77 65 65 6e 20 6d 75 6c 74 69 70 6c 65 20 63 6f ween multiple co
14999 6e 6e 65 63 74 69 6f 6e 73 2e 20 20 49 6e 20 74 nnections. In t
1499a 68 61 74 20 63 61 73 65 2c 20 65 61 63 68 20 63 hat case, each c
1499b 6f 6e 74 65 63 74 69 6f 6e 0a 2a 2a 20 68 61 73 ontection.** has
1499c 20 69 74 20 6f 77 6e 20 70 6f 69 6e 74 65 72 20 it own pointer
1499d 74 6f 20 74 68 69 73 20 6f 62 6a 65 63 74 2e 20 to this object.
1499e 20 42 75 74 20 65 61 63 68 20 69 6e 73 74 61 6e But each instan
1499f 63 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 ce of this objec
149a0 74 0a 2a 2a 20 70 6f 69 6e 74 73 20 74 6f 20 74 t.** points to t
149a1 68 65 20 73 61 6d 65 20 42 74 53 68 61 72 65 64 he same BtShared
149a2 20 6f 62 6a 65 63 74 2e 20 20 54 68 65 20 64 61 object. The da
149a3 74 61 62 61 73 65 20 63 61 63 68 65 20 61 6e 64 tabase cache and
149a4 20 74 68 65 0a 2a 2a 20 73 63 68 65 6d 61 20 61 the.** schema a
149a5 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 ssociated with t
149a6 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
149a7 20 61 72 65 20 61 6c 6c 20 63 6f 6e 74 61 69 6e are all contain
149a8 65 64 20 77 69 74 68 69 6e 0a 2a 2a 20 74 68 65 ed within.** the
149a9 20 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 BtShared object
149aa 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 66 69 65 6c ..**.** All fiel
149ab 64 73 20 69 6e 20 74 68 69 73 20 73 74 72 75 63 ds in this struc
149ac 74 75 72 65 20 61 72 65 20 61 63 63 65 73 73 65 ture are accesse
149ad 64 20 75 6e 64 65 72 20 73 71 6c 69 74 65 33 2e d under sqlite3.
149ae 6d 75 74 65 78 2e 0a 2a 2a 20 54 68 65 20 70 42 mutex..** The pB
149af 74 20 70 6f 69 6e 74 65 72 20 69 74 73 65 6c 66 t pointer itself
149b0 20 6d 61 79 20 6e 6f 74 20 62 65 20 63 68 61 6e may not be chan
149b1 67 65 64 20 77 68 69 6c 65 20 74 68 65 72 65 20 ged while there
149b2 65 78 69 73 74 73 20 63 75 72 73 6f 72 73 20 0a exists cursors .
149b3 2a 2a 20 69 6e 20 74 68 65 20 72 65 66 65 72 65 ** in the refere
149b4 6e 63 65 64 20 42 74 53 68 61 72 65 64 20 74 68 nced BtShared th
149b5 61 74 20 70 6f 69 6e 74 20 62 61 63 6b 20 74 6f at point back to
149b6 20 74 68 69 73 20 42 74 72 65 65 20 73 69 6e 63 this Btree sinc
149b7 65 20 74 68 6f 73 65 0a 2a 2a 20 63 75 72 73 6f e those.** curso
149b8 72 73 20 68 61 76 65 20 74 6f 20 64 6f 20 67 6f rs have to do go
149b9 20 74 68 72 6f 75 67 68 20 74 68 69 73 20 42 74 through this Bt
149ba 72 65 65 20 74 6f 20 66 69 6e 64 20 74 68 65 69 ree to find thei
149bb 72 20 42 74 53 68 61 72 65 64 20 61 6e 64 0a 2a r BtShared and.*
149bc 2a 20 74 68 65 79 20 6f 66 74 65 6e 20 64 6f 20 * they often do
149bd 73 6f 20 77 69 74 68 6f 75 74 20 68 6f 6c 64 69 so without holdi
149be 6e 67 20 73 71 6c 69 74 65 33 2e 6d 75 74 65 78 ng sqlite3.mutex
149bf 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 42 74 72 65 ..*/.struct Btre
149c0 65 20 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 e {. sqlite3 *d
149c1 62 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 b; /* The
149c2 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
149c3 69 6f 6e 20 68 6f 6c 64 69 6e 67 20 74 68 69 73 ion holding this
149c4 20 62 74 72 65 65 20 2a 2f 0a 20 20 42 74 53 68 btree */. BtSh
149c5 61 72 65 64 20 2a 70 42 74 3b 20 20 20 20 20 2f ared *pBt; /
149c6 2a 20 53 68 61 72 61 62 6c 65 20 63 6f 6e 74 65 * Sharable conte
149c7 6e 74 20 6f 66 20 74 68 69 73 20 62 74 72 65 65 nt of this btree
149c8 20 2a 2f 0a 20 20 75 38 20 69 6e 54 72 61 6e 73 */. u8 inTrans
149c9 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 52 41 4e ; /* TRAN
149ca 53 5f 4e 4f 4e 45 2c 20 54 52 41 4e 53 5f 52 45 S_NONE, TRANS_RE
149cb 41 44 20 6f 72 20 54 52 41 4e 53 5f 57 52 49 54 AD or TRANS_WRIT
149cc 45 20 2a 2f 0a 20 20 75 38 20 73 68 61 72 61 62 E */. u8 sharab
149cd 6c 65 3b 20 20 20 20 20 20 20 2f 2a 20 54 72 75 le; /* Tru
149ce 65 20 69 66 20 77 65 20 63 61 6e 20 73 68 61 72 e if we can shar
149cf 65 20 70 42 74 20 77 69 74 68 20 61 6e 6f 74 68 e pBt with anoth
149d0 65 72 20 64 62 20 2a 2f 0a 20 20 75 38 20 6c 6f er db */. u8 lo
149d1 63 6b 65 64 3b 20 20 20 20 20 20 20 20 20 2f 2a cked; /*
149d2 20 54 72 75 65 20 69 66 20 64 62 20 63 75 72 72 True if db curr
149d3 65 6e 74 6c 79 20 68 61 73 20 70 42 74 20 6c 6f ently has pBt lo
149d4 63 6b 65 64 20 2a 2f 0a 20 20 69 6e 74 20 77 61 cked */. int wa
149d5 6e 74 54 6f 4c 6f 63 6b 3b 20 20 20 20 2f 2a 20 ntToLock; /*
149d6 4e 75 6d 62 65 72 20 6f 66 20 6e 65 73 74 65 64 Number of nested
149d7 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 calls to sqlite
149d8 33 42 74 72 65 65 45 6e 74 65 72 28 29 20 2a 2f 3BtreeEnter() */
149d9 0a 20 20 69 6e 74 20 6e 42 61 63 6b 75 70 3b 20 . int nBackup;
149da 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
149db 6f 66 20 62 61 63 6b 75 70 20 6f 70 65 72 61 74 of backup operat
149dc 69 6f 6e 73 20 72 65 61 64 69 6e 67 20 74 68 69 ions reading thi
149dd 73 20 62 74 72 65 65 20 2a 2f 0a 20 20 42 74 72 s btree */. Btr
149de 65 65 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 ee *pNext;
149df 2f 2a 20 4c 69 73 74 20 6f 66 20 6f 74 68 65 72 /* List of other
149e0 20 73 68 61 72 61 62 6c 65 20 42 74 72 65 65 73 sharable Btrees
149e1 20 66 72 6f 6d 20 74 68 65 20 73 61 6d 65 20 64 from the same d
149e2 62 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 50 b */. Btree *pP
149e3 72 65 76 3b 20 20 20 20 20 20 2f 2a 20 42 61 63 rev; /* Bac
149e4 6b 20 70 6f 69 6e 74 65 72 20 6f 66 20 74 68 65 k pointer of the
149e5 20 73 61 6d 65 20 6c 69 73 74 20 2a 2f 0a 23 69 same list */.#i
149e6 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
149e7 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20 T_SHARED_CACHE.
149e8 20 42 74 4c 6f 63 6b 20 6c 6f 63 6b 3b 20 20 20 BtLock lock;
149e9 20 20 20 20 2f 2a 20 4f 62 6a 65 63 74 20 75 73 /* Object us
149ea 65 64 20 74 6f 20 6c 6f 63 6b 20 70 61 67 65 20 ed to lock page
149eb 31 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 1 */.#endif.};..
149ec 2f 2a 0a 2a 2a 20 42 74 72 65 65 2e 69 6e 54 72 /*.** Btree.inTr
149ed 61 6e 73 20 6d 61 79 20 74 61 6b 65 20 6f 6e 65 ans may take one
149ee 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e of the followin
149ef 67 20 76 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 g values..**.**
149f0 49 66 20 74 68 65 20 73 68 61 72 65 64 2d 64 61 If the shared-da
149f1 74 61 20 65 78 74 65 6e 73 69 6f 6e 20 69 73 20 ta extension is
149f2 65 6e 61 62 6c 65 64 2c 20 74 68 65 72 65 20 6d enabled, there m
149f3 61 79 20 62 65 20 6d 75 6c 74 69 70 6c 65 20 75 ay be multiple u
149f4 73 65 72 73 0a 2a 2a 20 6f 66 20 74 68 65 20 42 sers.** of the B
149f5 74 72 65 65 20 73 74 72 75 63 74 75 72 65 2e 20 tree structure.
149f6 41 74 20 6d 6f 73 74 20 6f 6e 65 20 6f 66 20 74 At most one of t
149f7 68 65 73 65 20 6d 61 79 20 6f 70 65 6e 20 61 20 hese may open a
149f8 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f write transactio
149f9 6e 2c 0a 2a 2a 20 62 75 74 20 61 6e 79 20 6e 75 n,.** but any nu
149fa 6d 62 65 72 20 6d 61 79 20 68 61 76 65 20 61 63 mber may have ac
149fb 74 69 76 65 20 72 65 61 64 20 74 72 61 6e 73 61 tive read transa
149fc 63 74 69 6f 6e 73 2e 0a 2a 2f 0a 23 64 65 66 69 ctions..*/.#defi
149fd 6e 65 20 54 52 41 4e 53 5f 4e 4f 4e 45 20 20 30 ne TRANS_NONE 0
149fe 0a 23 64 65 66 69 6e 65 20 54 52 41 4e 53 5f 52 .#define TRANS_R
149ff 45 41 44 20 20 31 0a 23 64 65 66 69 6e 65 20 54 EAD 1.#define T
14a00 52 41 4e 53 5f 57 52 49 54 45 20 32 0a 0a 2f 2a RANS_WRITE 2../*
14a01 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 .** An instance
14a02 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74 20 72 of this object r
14a03 65 70 72 65 73 65 6e 74 73 20 61 20 73 69 6e 67 epresents a sing
14a04 6c 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 le database file
14a05 2e 0a 2a 2a 20 0a 2a 2a 20 41 20 73 69 6e 67 6c ..** .** A singl
14a06 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
14a07 63 61 6e 20 62 65 20 69 6e 20 75 73 65 20 61 73 can be in use as
14a08 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 20 62 the same time b
14a09 79 20 74 77 6f 0a 2a 2a 20 6f 72 20 6d 6f 72 65 y two.** or more
14a0a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
14a0b 74 69 6f 6e 73 2e 20 20 57 68 65 6e 20 74 77 6f tions. When two
14a0c 20 6f 72 20 6d 6f 72 65 20 63 6f 6e 6e 65 63 74 or more connect
14a0d 69 6f 6e 73 20 61 72 65 0a 2a 2a 20 73 68 61 72 ions are.** shar
14a0e 69 6e 67 20 74 68 65 20 73 61 6d 65 20 64 61 74 ing the same dat
14a0f 61 62 61 73 65 20 66 69 6c 65 2c 20 65 61 63 68 abase file, each
14a10 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 73 20 connection has
14a11 69 74 20 6f 77 6e 0a 2a 2a 20 70 72 69 76 61 74 it own.** privat
14a12 65 20 42 74 72 65 65 20 6f 62 6a 65 63 74 20 66 e Btree object f
14a13 6f 72 20 74 68 65 20 66 69 6c 65 20 61 6e 64 20 or the file and
14a14 65 61 63 68 20 6f 66 20 74 68 6f 73 65 20 42 74 each of those Bt
14a15 72 65 65 73 20 70 6f 69 6e 74 73 0a 2a 2a 20 74 rees points.** t
14a16 6f 20 74 68 69 73 20 6f 6e 65 20 42 74 53 68 61 o this one BtSha
14a17 72 65 64 20 6f 62 6a 65 63 74 2e 20 20 42 74 53 red object. BtS
14a18 68 61 72 65 64 2e 6e 52 65 66 20 69 73 20 74 68 hared.nRef is th
14a19 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 63 e number of.** c
14a1a 6f 6e 6e 65 63 74 69 6f 6e 73 20 63 75 72 72 65 onnections curre
14a1b 6e 74 6c 79 20 73 68 61 72 69 6e 67 20 74 68 69 ntly sharing thi
14a1c 73 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e s database file.
14a1d 0a 2a 2a 0a 2a 2a 20 46 69 65 6c 64 73 20 69 6e .**.** Fields in
14a1e 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 this structure
14a1f 61 72 65 20 61 63 63 65 73 73 65 64 20 75 6e 64 are accessed und
14a20 65 72 20 74 68 65 20 42 74 53 68 61 72 65 64 2e er the BtShared.
14a21 6d 75 74 65 78 0a 2a 2a 20 6d 75 74 65 78 2c 20 mutex.** mutex,
14a22 65 78 63 65 70 74 20 66 6f 72 20 6e 52 65 66 20 except for nRef
14a23 61 6e 64 20 70 4e 65 78 74 20 77 68 69 63 68 20 and pNext which
14a24 61 72 65 20 61 63 63 65 73 73 65 64 20 75 6e 64 are accessed und
14a25 65 72 20 74 68 65 0a 2a 2a 20 67 6c 6f 62 61 6c er the.** global
14a26 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 SQLITE_MUTEX_ST
14a27 41 54 49 43 5f 4d 41 53 54 45 52 20 6d 75 74 65 ATIC_MASTER mute
14a28 78 2e 20 20 54 68 65 20 70 50 61 67 65 72 20 66 x. The pPager f
14a29 69 65 6c 64 0a 2a 2a 20 6d 61 79 20 6e 6f 74 20 ield.** may not
14a2a 62 65 20 6d 6f 64 69 66 69 65 64 20 6f 6e 63 65 be modified once
14a2b 20 69 74 20 69 73 20 69 6e 69 74 69 61 6c 6c 79 it is initially
14a2c 20 73 65 74 20 61 73 20 6c 6f 6e 67 20 61 73 20 set as long as
14a2d 6e 52 65 66 3e 30 2e 0a 2a 2a 20 54 68 65 20 70 nRef>0..** The p
14a2e 53 63 68 65 6d 61 20 66 69 65 6c 64 20 6d 61 79 Schema field may
14a2f 20 62 65 20 73 65 74 20 6f 6e 63 65 20 75 6e 64 be set once und
14a30 65 72 20 42 74 53 68 61 72 65 64 2e 6d 75 74 65 er BtShared.mute
14a31 78 20 61 6e 64 0a 2a 2a 20 74 68 65 72 65 61 66 x and.** thereaf
14a32 74 65 72 20 69 73 20 75 6e 63 68 61 6e 67 65 64 ter is unchanged
14a33 20 61 73 20 6c 6f 6e 67 20 61 73 20 6e 52 65 66 as long as nRef
14a34 3e 30 2e 0a 2a 2a 0a 2a 2a 20 69 73 50 65 6e 64 >0..**.** isPend
14a35 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 49 66 20 ing:.**.** If
14a36 61 20 42 74 53 68 61 72 65 64 20 63 6c 69 65 6e a BtShared clien
14a37 74 20 66 61 69 6c 73 20 74 6f 20 6f 62 74 61 69 t fails to obtai
14a38 6e 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f n a write-lock o
14a39 6e 20 61 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 n a database.**
14a3a 20 20 74 61 62 6c 65 20 28 62 65 63 61 75 73 65 table (because
14a3b 20 74 68 65 72 65 20 65 78 69 73 74 73 20 6f 6e there exists on
14a3c 65 20 6f 72 20 6d 6f 72 65 20 72 65 61 64 2d 6c e or more read-l
14a3d 6f 63 6b 73 20 6f 6e 20 74 68 65 20 74 61 62 6c ocks on the tabl
14a3e 65 29 2c 0a 2a 2a 20 20 20 74 68 65 20 73 68 61 e),.** the sha
14a3f 72 65 64 2d 63 61 63 68 65 20 65 6e 74 65 72 73 red-cache enters
14a40 20 27 70 65 6e 64 69 6e 67 2d 6c 6f 63 6b 27 20 'pending-lock'
14a41 73 74 61 74 65 20 61 6e 64 20 69 73 50 65 6e 64 state and isPend
14a42 69 6e 67 20 69 73 0a 2a 2a 20 20 20 73 65 74 20 ing is.** set
14a43 74 6f 20 74 72 75 65 2e 0a 2a 2a 0a 2a 2a 20 20 to true..**.**
14a44 20 54 68 65 20 73 68 61 72 65 64 2d 63 61 63 68 The shared-cach
14a45 65 20 6c 65 61 76 65 73 20 74 68 65 20 27 70 65 e leaves the 'pe
14a46 6e 64 69 6e 67 20 6c 6f 63 6b 27 20 73 74 61 74 nding lock' stat
14a47 65 20 77 68 65 6e 20 65 69 74 68 65 72 20 6f 66 e when either of
14a48 0a 2a 2a 20 20 20 74 68 65 20 66 6f 6c 6c 6f 77 .** the follow
14a49 69 6e 67 20 6f 63 63 75 72 3a 0a 2a 2a 0a 2a 2a ing occur:.**.**
14a4a 20 20 20 20 20 31 29 20 54 68 65 20 63 75 72 72 1) The curr
14a4b 65 6e 74 20 77 72 69 74 65 72 20 28 42 74 53 68 ent writer (BtSh
14a4c 61 72 65 64 2e 70 57 72 69 74 65 72 29 20 63 6f ared.pWriter) co
14a4d 6e 63 6c 75 64 65 73 20 69 74 73 20 74 72 61 6e ncludes its tran
14a4e 73 61 63 74 69 6f 6e 2c 20 4f 52 0a 2a 2a 20 20 saction, OR.**
14a4f 20 20 20 32 29 20 54 68 65 20 6e 75 6d 62 65 72 2) The number
14a50 20 6f 66 20 6c 6f 63 6b 73 20 68 65 6c 64 20 62 of locks held b
14a51 79 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 y other connecti
14a52 6f 6e 73 20 64 72 6f 70 73 20 74 6f 20 7a 65 72 ons drops to zer
14a53 6f 2e 0a 2a 2a 0a 2a 2a 20 20 20 77 68 69 6c 65 o..**.** while
14a54 20 69 6e 20 74 68 65 20 27 70 65 6e 64 69 6e 67 in the 'pending
14a55 2d 6c 6f 63 6b 27 20 73 74 61 74 65 2c 20 6e 6f -lock' state, no
14a56 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6d 61 79 20 connection may
14a57 73 74 61 72 74 20 61 20 6e 65 77 0a 2a 2a 20 20 start a new.**
14a58 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a transaction..**
14a59 0a 2a 2a 20 20 20 54 68 69 73 20 66 65 61 74 75 .** This featu
14a5a 72 65 20 69 73 20 69 6e 63 6c 75 64 65 64 20 74 re is included t
14a5b 6f 20 68 65 6c 70 20 70 72 65 76 65 6e 74 20 77 o help prevent w
14a5c 72 69 74 65 72 2d 73 74 61 72 76 61 74 69 6f 6e riter-starvation
14a5d 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 42 74 53 68 ..*/.struct BtSh
14a5e 61 72 65 64 20 7b 0a 20 20 50 61 67 65 72 20 2a ared {. Pager *
14a5f 70 50 61 67 65 72 3b 20 20 20 20 20 20 20 20 2f pPager; /
14a60 2a 20 54 68 65 20 70 61 67 65 20 63 61 63 68 65 * The page cache
14a61 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 */. sqlite3 *d
14a62 62 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 b; /* D
14a63 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
14a64 6f 6e 20 63 75 72 72 65 6e 74 6c 79 20 75 73 69 on currently usi
14a65 6e 67 20 74 68 69 73 20 42 74 72 65 65 20 2a 2f ng this Btree */
14a66 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 . BtCursor *pCu
14a67 72 73 6f 72 3b 20 20 20 20 2f 2a 20 41 20 6c 69 rsor; /* A li
14a68 73 74 20 6f 66 20 61 6c 6c 20 6f 70 65 6e 20 63 st of all open c
14a69 75 72 73 6f 72 73 20 2a 2f 0a 20 20 4d 65 6d 50 ursors */. MemP
14a6a 61 67 65 20 2a 70 50 61 67 65 31 3b 20 20 20 20 age *pPage1;
14a6b 20 20 2f 2a 20 46 69 72 73 74 20 70 61 67 65 20 /* First page
14a6c 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
14a6d 2a 2f 0a 20 20 75 38 20 72 65 61 64 4f 6e 6c 79 */. u8 readOnly
14a6e 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 ; /* Tr
14a6f 75 65 20 69 66 20 74 68 65 20 75 6e 64 65 72 6c ue if the underl
14a70 79 69 6e 67 20 66 69 6c 65 20 69 73 20 72 65 61 ying file is rea
14a71 64 6f 6e 6c 79 20 2a 2f 0a 20 20 75 38 20 70 61 donly */. u8 pa
14a72 67 65 53 69 7a 65 46 69 78 65 64 3b 20 20 20 20 geSizeFixed;
14a73 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 /* True if the
14a74 70 61 67 65 20 73 69 7a 65 20 63 61 6e 20 6e 6f page size can no
14a75 20 6c 6f 6e 67 65 72 20 62 65 20 63 68 61 6e 67 longer be chang
14a76 65 64 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 ed */.#ifndef SQ
14a77 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 LITE_OMIT_AUTOVA
14a78 43 55 55 4d 0a 20 20 75 38 20 61 75 74 6f 56 61 CUUM. u8 autoVa
14a79 63 75 75 6d 3b 20 20 20 20 20 20 20 20 2f 2a 20 cuum; /*
14a7a 54 72 75 65 20 69 66 20 61 75 74 6f 2d 76 61 63 True if auto-vac
14a7b 75 75 6d 20 69 73 20 65 6e 61 62 6c 65 64 20 2a uum is enabled *
14a7c 2f 0a 20 20 75 38 20 69 6e 63 72 56 61 63 75 75 /. u8 incrVacuu
14a7d 6d 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 m; /* Tru
14a7e 65 20 69 66 20 69 6e 63 72 2d 76 61 63 75 75 6d e if incr-vacuum
14a7f 20 69 73 20 65 6e 61 62 6c 65 64 20 2a 2f 0a 23 is enabled */.#
14a80 65 6e 64 69 66 0a 20 20 75 31 36 20 70 61 67 65 endif. u16 page
14a81 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 2f 2a Size; /*
14a82 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 Total number of
14a83 20 62 79 74 65 73 20 6f 6e 20 61 20 70 61 67 65 bytes on a page
14a84 20 2a 2f 0a 20 20 75 31 36 20 75 73 61 62 6c 65 */. u16 usable
14a85 53 69 7a 65 3b 20 20 20 20 20 20 20 2f 2a 20 4e Size; /* N
14a86 75 6d 62 65 72 20 6f 66 20 75 73 61 62 6c 65 20 umber of usable
14a87 62 79 74 65 73 20 6f 6e 20 65 61 63 68 20 70 61 bytes on each pa
14a88 67 65 20 2a 2f 0a 20 20 75 31 36 20 6d 61 78 4c ge */. u16 maxL
14a89 6f 63 61 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a ocal; /*
14a8a 20 4d 61 78 69 6d 75 6d 20 6c 6f 63 61 6c 20 70 Maximum local p
14a8b 61 79 6c 6f 61 64 20 69 6e 20 6e 6f 6e 2d 4c 45 ayload in non-LE
14a8c 41 46 44 41 54 41 20 74 61 62 6c 65 73 20 2a 2f AFDATA tables */
14a8d 0a 20 20 75 31 36 20 6d 69 6e 4c 6f 63 61 6c 3b . u16 minLocal;
14a8e 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 69 6e 69 /* Mini
14a8f 6d 75 6d 20 6c 6f 63 61 6c 20 70 61 79 6c 6f 61 mum local payloa
14a90 64 20 69 6e 20 6e 6f 6e 2d 4c 45 41 46 44 41 54 d in non-LEAFDAT
14a91 41 20 74 61 62 6c 65 73 20 2a 2f 0a 20 20 75 31 A tables */. u1
14a92 36 20 6d 61 78 4c 65 61 66 3b 20 20 20 20 20 20 6 maxLeaf;
14a93 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c /* Maximum l
14a94 6f 63 61 6c 20 70 61 79 6c 6f 61 64 20 69 6e 20 ocal payload in
14a95 61 20 4c 45 41 46 44 41 54 41 20 74 61 62 6c 65 a LEAFDATA table
14a96 20 2a 2f 0a 20 20 75 31 36 20 6d 69 6e 4c 65 61 */. u16 minLea
14a97 66 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d f; /* M
14a98 69 6e 69 6d 75 6d 20 6c 6f 63 61 6c 20 70 61 79 inimum local pay
14a99 6c 6f 61 64 20 69 6e 20 61 20 4c 45 41 46 44 41 load in a LEAFDA
14a9a 54 41 20 74 61 62 6c 65 20 2a 2f 0a 20 20 75 38 TA table */. u8
14a9b 20 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3b 20 inTransaction;
14a9c 20 20 20 20 2f 2a 20 54 72 61 6e 73 61 63 74 69 /* Transacti
14a9d 6f 6e 20 73 74 61 74 65 20 2a 2f 0a 20 20 69 6e on state */. in
14a9e 74 20 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3b 20 t nTransaction;
14a9f 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
14aa0 20 6f 70 65 6e 20 74 72 61 6e 73 61 63 74 69 6f open transactio
14aa1 6e 73 20 28 72 65 61 64 20 2b 20 77 72 69 74 65 ns (read + write
14aa2 29 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 53 63 ) */. void *pSc
14aa3 68 65 6d 61 3b 20 20 20 20 20 20 20 20 2f 2a 20 hema; /*
14aa4 50 6f 69 6e 74 65 72 20 74 6f 20 73 70 61 63 65 Pointer to space
14aa5 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 73 71 allocated by sq
14aa6 6c 69 74 65 33 42 74 72 65 65 53 63 68 65 6d 61 lite3BtreeSchema
14aa7 28 29 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 () */. void (*x
14aa8 46 72 65 65 53 63 68 65 6d 61 29 28 76 6f 69 64 FreeSchema)(void
14aa9 2a 29 3b 20 20 2f 2a 20 44 65 73 74 72 75 63 74 *); /* Destruct
14aaa 6f 72 20 66 6f 72 20 42 74 53 68 61 72 65 64 2e or for BtShared.
14aab 70 53 63 68 65 6d 61 20 2a 2f 0a 20 20 73 71 6c pSchema */. sql
14aac 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 ite3_mutex *mute
14aad 78 3b 20 2f 2a 20 4e 6f 6e 2d 72 65 63 75 72 73 x; /* Non-recurs
14aae 69 76 65 20 6d 75 74 65 78 20 72 65 71 75 69 72 ive mutex requir
14aaf 65 64 20 74 6f 20 61 63 63 65 73 73 20 74 68 69 ed to access thi
14ab0 73 20 73 74 72 75 63 74 20 2a 2f 0a 20 20 42 69 s struct */. Bi
14ab1 74 76 65 63 20 2a 70 48 61 73 43 6f 6e 74 65 6e tvec *pHasConten
14ab2 74 3b 20 20 2f 2a 20 53 65 74 20 6f 66 20 70 61 t; /* Set of pa
14ab3 67 65 73 20 6d 6f 76 65 64 20 74 6f 20 66 72 65 ges moved to fre
14ab4 65 2d 6c 69 73 74 20 74 68 69 73 20 74 72 61 6e e-list this tran
14ab5 73 61 63 74 69 6f 6e 20 2a 2f 0a 23 69 66 6e 64 saction */.#ifnd
14ab6 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 ef SQLITE_OMIT_S
14ab7 48 41 52 45 44 5f 43 41 43 48 45 0a 20 20 69 6e HARED_CACHE. in
14ab8 74 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20 t nRef;
14ab9 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
14aba 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74 references to t
14abb 68 69 73 20 73 74 72 75 63 74 75 72 65 20 2a 2f his structure */
14abc 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 4e 65 . BtShared *pNe
14abd 78 74 3b 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 xt; /* Next
14abe 20 6f 6e 20 61 20 6c 69 73 74 20 6f 66 20 73 68 on a list of sh
14abf 61 72 61 62 6c 65 20 42 74 53 68 61 72 65 64 20 arable BtShared
14ac0 73 74 72 75 63 74 73 20 2a 2f 0a 20 20 42 74 4c structs */. BtL
14ac1 6f 63 6b 20 2a 70 4c 6f 63 6b 3b 20 20 20 20 20 ock *pLock;
14ac2 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 6c 6f /* List of lo
14ac3 63 6b 73 20 68 65 6c 64 20 6f 6e 20 74 68 69 73 cks held on this
14ac4 20 73 68 61 72 65 64 2d 62 74 72 65 65 20 73 74 shared-btree st
14ac5 72 75 63 74 20 2a 2f 0a 20 20 42 74 72 65 65 20 ruct */. Btree
14ac6 2a 70 57 72 69 74 65 72 3b 20 20 20 20 20 20 20 *pWriter;
14ac7 2f 2a 20 42 74 72 65 65 20 77 69 74 68 20 63 75 /* Btree with cu
14ac8 72 72 65 6e 74 6c 79 20 6f 70 65 6e 20 77 72 69 rrently open wri
14ac9 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a te transaction *
14aca 2f 0a 20 20 75 38 20 69 73 45 78 63 6c 75 73 69 /. u8 isExclusi
14acb 76 65 3b 20 20 20 20 20 20 20 2f 2a 20 54 72 75 ve; /* Tru
14acc 65 20 69 66 20 70 57 72 69 74 65 72 20 68 61 73 e if pWriter has
14acd 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f an EXCLUSIVE lo
14ace 63 6b 20 6f 6e 20 74 68 65 20 64 62 20 2a 2f 0a ck on the db */.
14acf 20 20 75 38 20 69 73 50 65 6e 64 69 6e 67 3b 20 u8 isPending;
14ad0 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 77 61 /* If wa
14ad1 69 74 69 6e 67 20 66 6f 72 20 72 65 61 64 2d 6c iting for read-l
14ad2 6f 63 6b 73 20 74 6f 20 63 6c 65 61 72 20 2a 2f ocks to clear */
14ad3 0a 23 65 6e 64 69 66 0a 20 20 75 38 20 2a 70 54 .#endif. u8 *pT
14ad4 6d 70 53 70 61 63 65 3b 20 20 20 20 20 20 20 20 mpSpace;
14ad5 2f 2a 20 42 74 53 68 61 72 65 64 2e 70 61 67 65 /* BtShared.page
14ad6 53 69 7a 65 20 62 79 74 65 73 20 6f 66 20 73 70 Size bytes of sp
14ad7 61 63 65 20 66 6f 72 20 74 6d 70 20 75 73 65 20 ace for tmp use
14ad8 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 */.};../*.** An
14ad9 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 instance of the
14ada 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 following struct
14adb 75 72 65 20 69 73 20 75 73 65 64 20 74 6f 20 68 ure is used to h
14adc 6f 6c 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a old information.
14add 2a 2a 20 61 62 6f 75 74 20 61 20 63 65 6c 6c 2e ** about a cell.
14ade 20 20 54 68 65 20 70 61 72 73 65 43 65 6c 6c 50 The parseCellP
14adf 74 72 28 29 20 66 75 6e 63 74 69 6f 6e 20 66 69 tr() function fi
14ae0 6c 6c 73 20 69 6e 20 74 68 69 73 20 73 74 72 75 lls in this stru
14ae1 63 74 75 72 65 0a 2a 2a 20 62 61 73 65 64 20 6f cture.** based o
14ae2 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 65 78 n information ex
14ae3 74 72 61 63 74 20 66 72 6f 6d 20 74 68 65 20 72 tract from the r
14ae4 61 77 20 64 69 73 6b 20 70 61 67 65 2e 0a 2a 2f aw disk page..*/
14ae5 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
14ae6 43 65 6c 6c 49 6e 66 6f 20 43 65 6c 6c 49 6e 66 CellInfo CellInf
14ae7 6f 3b 0a 73 74 72 75 63 74 20 43 65 6c 6c 49 6e o;.struct CellIn
14ae8 66 6f 20 7b 0a 20 20 75 38 20 2a 70 43 65 6c 6c fo {. u8 *pCell
14ae9 3b 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 ; /* Pointer
14aea 20 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66 to the start of
14aeb 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 2a 2f cell content */
14aec 0a 20 20 69 36 34 20 6e 4b 65 79 3b 20 20 20 20 . i64 nKey;
14aed 20 20 2f 2a 20 54 68 65 20 6b 65 79 20 66 6f 72 /* The key for
14aee 20 49 4e 54 4b 45 59 20 74 61 62 6c 65 73 2c 20 INTKEY tables,
14aef 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 or number of byt
14af0 65 73 20 69 6e 20 6b 65 79 20 2a 2f 0a 20 20 75 es in key */. u
14af1 33 32 20 6e 44 61 74 61 3b 20 20 20 20 20 2f 2a 32 nData; /*
14af2 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 Number of bytes
14af3 20 6f 66 20 64 61 74 61 20 2a 2f 0a 20 20 75 33 of data */. u3
14af4 32 20 6e 50 61 79 6c 6f 61 64 3b 20 20 2f 2a 20 2 nPayload; /*
14af5 54 6f 74 61 6c 20 61 6d 6f 75 6e 74 20 6f 66 20 Total amount of
14af6 70 61 79 6c 6f 61 64 20 2a 2f 0a 20 20 75 31 36 payload */. u16
14af7 20 6e 48 65 61 64 65 72 3b 20 20 20 2f 2a 20 53 nHeader; /* S
14af8 69 7a 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 ize of the cell
14af9 63 6f 6e 74 65 6e 74 20 68 65 61 64 65 72 20 69 content header i
14afa 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 75 31 36 n bytes */. u16
14afb 20 6e 4c 6f 63 61 6c 3b 20 20 20 20 2f 2a 20 41 nLocal; /* A
14afc 6d 6f 75 6e 74 20 6f 66 20 70 61 79 6c 6f 61 64 mount of payload
14afd 20 68 65 6c 64 20 6c 6f 63 61 6c 6c 79 20 2a 2f held locally */
14afe 0a 20 20 75 31 36 20 69 4f 76 65 72 66 6c 6f 77 . u16 iOverflow
14aff 3b 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20 6f ; /* Offset to o
14b00 76 65 72 66 6c 6f 77 20 70 61 67 65 20 6e 75 6d verflow page num
14b01 62 65 72 2e 20 20 5a 65 72 6f 20 69 66 20 6e 6f ber. Zero if no
14b02 20 6f 76 65 72 66 6c 6f 77 20 2a 2f 0a 20 20 75 overflow */. u
14b03 31 36 20 6e 53 69 7a 65 3b 20 20 20 20 20 2f 2a 16 nSize; /*
14b04 20 53 69 7a 65 20 6f 66 20 74 68 65 20 63 65 6c Size of the cel
14b05 6c 20 63 6f 6e 74 65 6e 74 20 6f 6e 20 74 68 65 l content on the
14b06 20 6d 61 69 6e 20 62 2d 74 72 65 65 20 70 61 67 main b-tree pag
14b07 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 4d e */.};../*.** M
14b08 61 78 69 6d 75 6d 20 64 65 70 74 68 20 6f 66 20 aximum depth of
14b09 61 6e 20 53 51 4c 69 74 65 20 42 2d 54 72 65 65 an SQLite B-Tree
14b0a 20 73 74 72 75 63 74 75 72 65 2e 20 41 6e 79 20 structure. Any
14b0b 42 2d 54 72 65 65 20 64 65 65 70 65 72 20 74 68 B-Tree deeper th
14b0c 61 6e 0a 2a 2a 20 74 68 69 73 20 77 69 6c 6c 20 an.** this will
14b0d 62 65 20 64 65 63 6c 61 72 65 64 20 63 6f 72 72 be declared corr
14b0e 75 70 74 2e 20 54 68 69 73 20 76 61 6c 75 65 20 upt. This value
14b0f 69 73 20 63 61 6c 63 75 6c 61 74 65 64 20 62 61 is calculated ba
14b10 73 65 64 20 6f 6e 20 61 0a 2a 2a 20 6d 61 78 69 sed on a.** maxi
14b11 6d 75 6d 20 64 61 74 61 62 61 73 65 20 73 69 7a mum database siz
14b12 65 20 6f 66 20 32 5e 33 31 20 70 61 67 65 73 20 e of 2^31 pages
14b13 61 20 6d 69 6e 69 6d 75 6d 20 66 61 6e 6f 75 74 a minimum fanout
14b14 20 6f 66 20 32 20 66 6f 72 20 61 0a 2a 2a 20 72 of 2 for a.** r
14b15 6f 6f 74 2d 6e 6f 64 65 20 61 6e 64 20 33 20 66 oot-node and 3 f
14b16 6f 72 20 61 6c 6c 20 6f 74 68 65 72 20 69 6e 74 or all other int
14b17 65 72 6e 61 6c 20 6e 6f 64 65 73 2e 0a 2a 2a 0a ernal nodes..**.
14b18 2a 2a 20 49 66 20 61 20 74 72 65 65 20 74 68 61 ** If a tree tha
14b19 74 20 61 70 70 65 61 72 73 20 74 6f 20 62 65 20 t appears to be
14b1a 74 61 6c 6c 65 72 20 74 68 61 6e 20 74 68 69 73 taller than this
14b1b 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2c is encountered,
14b1c 20 69 74 20 69 73 0a 2a 2a 20 61 73 73 75 6d 65 it is.** assume
14b1d 64 20 74 68 61 74 20 74 68 65 20 64 61 74 61 62 d that the datab
14b1e 61 73 65 20 69 73 20 63 6f 72 72 75 70 74 2e 0a ase is corrupt..
14b1f 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 43 55 52 */.#define BTCUR
14b20 53 4f 52 5f 4d 41 58 5f 44 45 50 54 48 20 32 30 SOR_MAX_DEPTH 20
14b21 0a 0a 2f 2a 0a 2a 2a 20 41 20 63 75 72 73 6f 72 ../*.** A cursor
14b22 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f is a pointer to
14b23 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 65 6e a particular en
14b24 74 72 79 20 77 69 74 68 69 6e 20 61 20 70 61 72 try within a par
14b25 74 69 63 75 6c 61 72 0a 2a 2a 20 62 2d 74 72 65 ticular.** b-tre
14b26 65 20 77 69 74 68 69 6e 20 61 20 64 61 74 61 62 e within a datab
14b27 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 ase file..**.**
14b28 54 68 65 20 65 6e 74 72 79 20 69 73 20 69 64 65 The entry is ide
14b29 6e 74 69 66 69 65 64 20 62 79 20 69 74 73 20 4d ntified by its M
14b2a 65 6d 50 61 67 65 20 61 6e 64 20 74 68 65 20 69 emPage and the i
14b2b 6e 64 65 78 20 69 6e 0a 2a 2a 20 4d 65 6d 50 61 ndex in.** MemPa
14b2c 67 65 2e 61 43 65 6c 6c 5b 5d 20 6f 66 20 74 68 ge.aCell[] of th
14b2d 65 20 65 6e 74 72 79 2e 0a 2a 2a 0a 2a 2a 20 57 e entry..**.** W
14b2e 68 65 6e 20 61 20 73 69 6e 67 6c 65 20 64 61 74 hen a single dat
14b2f 61 62 61 73 65 20 66 69 6c 65 20 63 61 6e 20 73 abase file can s
14b30 68 61 72 65 64 20 62 79 20 74 77 6f 20 6d 6f 72 hared by two mor
14b31 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 e database conne
14b32 63 74 69 6f 6e 73 2c 0a 2a 2a 20 62 75 74 20 63 ctions,.** but c
14b33 75 72 73 6f 72 73 20 63 61 6e 6e 6f 74 20 62 65 ursors cannot be
14b34 20 73 68 61 72 65 64 2e 20 20 45 61 63 68 20 63 shared. Each c
14b35 75 72 73 6f 72 20 69 73 20 61 73 73 6f 63 69 61 ursor is associa
14b36 74 65 64 20 77 69 74 68 20 61 0a 2a 2a 20 70 61 ted with a.** pa
14b37 72 74 69 63 75 6c 61 72 20 64 61 74 61 62 61 73 rticular databas
14b38 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 64 65 e connection ide
14b39 6e 74 69 66 69 65 64 20 42 74 43 75 72 73 6f 72 ntified BtCursor
14b3a 2e 70 42 74 72 65 65 2e 64 62 2e 0a 2a 2a 0a 2a .pBtree.db..**.*
14b3b 2a 20 46 69 65 6c 64 73 20 69 6e 20 74 68 69 73 * Fields in this
14b3c 20 73 74 72 75 63 74 75 72 65 20 61 72 65 20 61 structure are a
14b3d 63 63 65 73 73 65 64 20 75 6e 64 65 72 20 74 68 ccessed under th
14b3e 65 20 42 74 53 68 61 72 65 64 2e 6d 75 74 65 78 e BtShared.mutex
14b3f 0a 2a 2a 20 66 6f 75 6e 64 20 61 74 20 73 65 6c .** found at sel
14b40 66 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 2e 20 0a f->pBt->mutex. .
14b41 2a 2f 0a 73 74 72 75 63 74 20 42 74 43 75 72 73 */.struct BtCurs
14b42 6f 72 20 7b 0a 20 20 42 74 72 65 65 20 2a 70 42 or {. Btree *pB
14b43 74 72 65 65 3b 20 20 20 20 20 20 20 20 20 20 20 tree;
14b44 20 2f 2a 20 54 68 65 20 42 74 72 65 65 20 74 6f /* The Btree to
14b45 20 77 68 69 63 68 20 74 68 69 73 20 63 75 72 73 which this curs
14b46 6f 72 20 62 65 6c 6f 6e 67 73 20 2a 2f 0a 20 20 or belongs */.
14b47 42 74 53 68 61 72 65 64 20 2a 70 42 74 3b 20 20 BtShared *pBt;
14b48 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
14b49 20 42 74 53 68 61 72 65 64 20 74 68 69 73 20 63 BtShared this c
14b4a 75 72 73 6f 72 20 70 6f 69 6e 74 73 20 74 6f 20 ursor points to
14b4b 2a 2f 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 */. BtCursor *p
14b4c 4e 65 78 74 2c 20 2a 70 50 72 65 76 3b 20 20 2f Next, *pPrev; /
14b4d 2a 20 46 6f 72 6d 73 20 61 20 6c 69 6e 6b 65 64 * Forms a linked
14b4e 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 63 75 72 list of all cur
14b4f 73 6f 72 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 sors */. struct
14b50 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e KeyInfo *pKeyIn
14b51 66 6f 3b 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 fo; /* Argument
14b52 70 61 73 73 65 64 20 74 6f 20 63 6f 6d 70 61 72 passed to compar
14b53 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 2a 2f ison function */
14b54 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 52 6f 6f 74 . Pgno pgnoRoot
14b55 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
14b56 54 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 The root page of
14b57 20 74 68 69 73 20 74 72 65 65 20 2a 2f 0a 20 20 this tree */.
14b58 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 63 61 sqlite3_int64 ca
14b59 63 68 65 64 52 6f 77 69 64 3b 20 2f 2a 20 4e 65 chedRowid; /* Ne
14b5a 78 74 20 72 6f 77 69 64 20 63 61 63 68 65 2e 20 xt rowid cache.
14b5b 20 30 20 6d 65 61 6e 73 20 6e 6f 74 20 76 61 6c 0 means not val
14b5c 69 64 20 2a 2f 0a 20 20 43 65 6c 6c 49 6e 66 6f id */. CellInfo
14b5d 20 69 6e 66 6f 3b 20 20 20 20 20 20 20 20 20 20 info;
14b5e 20 20 2f 2a 20 41 20 70 61 72 73 65 20 6f 66 20 /* A parse of
14b5f 74 68 65 20 63 65 6c 6c 20 77 65 20 61 72 65 20 the cell we are
14b60 70 6f 69 6e 74 69 6e 67 20 61 74 20 2a 2f 0a 20 pointing at */.
14b61 20 75 38 20 77 72 46 6c 61 67 3b 20 20 20 20 20 u8 wrFlag;
14b62 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 /* Tr
14b63 75 65 20 69 66 20 77 72 69 74 61 62 6c 65 20 2a ue if writable *
14b64 2f 0a 20 20 75 38 20 61 74 4c 61 73 74 3b 20 20 /. u8 atLast;
14b65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
14b66 20 43 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 Cursor pointing
14b67 20 74 6f 20 74 68 65 20 6c 61 73 74 20 65 6e 74 to the last ent
14b68 72 79 20 2a 2f 0a 20 20 75 38 20 76 61 6c 69 64 ry */. u8 valid
14b69 4e 4b 65 79 3b 20 20 20 20 20 20 20 20 20 20 20 NKey;
14b6a 20 20 2f 2a 20 54 72 75 65 20 69 66 20 69 6e 66 /* True if inf
14b6b 6f 2e 6e 4b 65 79 20 69 73 20 76 61 6c 69 64 20 o.nKey is valid
14b6c 2a 2f 0a 20 20 75 38 20 65 53 74 61 74 65 3b 20 */. u8 eState;
14b6d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
14b6e 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 43 55 52 * One of the CUR
14b6f 53 4f 52 5f 58 58 58 20 63 6f 6e 73 74 61 6e 74 SOR_XXX constant
14b70 73 20 28 73 65 65 20 62 65 6c 6f 77 29 20 2a 2f s (see below) */
14b71 0a 20 20 76 6f 69 64 20 2a 70 4b 65 79 3b 20 20 . void *pKey;
14b72 20 20 20 20 2f 2a 20 53 61 76 65 64 20 6b 65 79 /* Saved key
14b73 20 74 68 61 74 20 77 61 73 20 63 75 72 73 6f 72 that was cursor
14b74 27 73 20 6c 61 73 74 20 6b 6e 6f 77 6e 20 70 6f 's last known po
14b75 73 69 74 69 6f 6e 20 2a 2f 0a 20 20 69 36 34 20 sition */. i64
14b76 6e 4b 65 79 3b 20 20 20 20 20 20 20 20 2f 2a 20 nKey; /*
14b77 53 69 7a 65 20 6f 66 20 70 4b 65 79 2c 20 6f 72 Size of pKey, or
14b78 20 6c 61 73 74 20 69 6e 74 65 67 65 72 20 6b 65 last integer ke
14b79 79 20 2a 2f 0a 20 20 69 6e 74 20 73 6b 69 70 4e y */. int skipN
14b7a 65 78 74 3b 20 20 20 20 2f 2a 20 50 72 65 76 28 ext; /* Prev(
14b7b 29 20 69 73 20 6e 6f 6f 70 20 69 66 20 6e 65 67 ) is noop if neg
14b7c 61 74 69 76 65 2e 20 4e 65 78 74 28 29 20 69 73 ative. Next() is
14b7d 20 6e 6f 6f 70 20 69 66 20 70 6f 73 69 74 69 76 noop if positiv
14b7e 65 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c e */.#ifndef SQL
14b7f 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f ITE_OMIT_INCRBLO
14b80 42 0a 20 20 75 38 20 69 73 49 6e 63 72 62 6c 6f B. u8 isIncrblo
14b81 62 48 61 6e 64 6c 65 3b 20 20 20 20 20 20 2f 2a bHandle; /*
14b82 20 54 72 75 65 20 69 66 20 74 68 69 73 20 63 75 True if this cu
14b83 72 73 6f 72 20 69 73 20 61 6e 20 69 6e 63 72 2e rsor is an incr.
14b84 20 69 6f 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 io handle */.
14b85 50 67 6e 6f 20 2a 61 4f 76 65 72 66 6c 6f 77 3b Pgno *aOverflow;
14b86 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 61 63 /* Cac
14b87 68 65 20 6f 66 20 6f 76 65 72 66 6c 6f 77 20 70 he of overflow p
14b88 61 67 65 20 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f age locations */
14b89 0a 23 65 6e 64 69 66 0a 20 20 69 31 36 20 69 50 .#endif. i16 iP
14b8a 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 age;
14b8b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14b8c 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 75 72 72 /* Index of curr
14b8d 65 6e 74 20 70 61 67 65 20 69 6e 20 61 70 50 61 ent page in apPa
14b8e 67 65 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 ge */. MemPage
14b8f 2a 61 70 50 61 67 65 5b 42 54 43 55 52 53 4f 52 *apPage[BTCURSOR
14b90 5f 4d 41 58 5f 44 45 50 54 48 5d 3b 20 20 2f 2a _MAX_DEPTH]; /*
14b91 20 50 61 67 65 73 20 66 72 6f 6d 20 72 6f 6f 74 Pages from root
14b92 20 74 6f 20 63 75 72 72 65 6e 74 20 70 61 67 65 to current page
14b93 20 2a 2f 0a 20 20 75 31 36 20 61 69 49 64 78 5b */. u16 aiIdx[
14b94 42 54 43 55 52 53 4f 52 5f 4d 41 58 5f 44 45 50 BTCURSOR_MAX_DEP
14b95 54 48 5d 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 TH]; /* C
14b96 75 72 72 65 6e 74 20 69 6e 64 65 78 20 69 6e 20 urrent index in
14b97 61 70 50 61 67 65 5b 69 5d 20 2a 2f 0a 7d 3b 0a apPage[i] */.};.
14b98 0a 2f 2a 0a 2a 2a 20 50 6f 74 65 6e 74 69 61 6c ./*.** Potential
14b99 20 76 61 6c 75 65 73 20 66 6f 72 20 42 74 43 75 values for BtCu
14b9a 72 73 6f 72 2e 65 53 74 61 74 65 2e 0a 2a 2a 0a rsor.eState..**.
14b9b 2a 2a 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 3a ** CURSOR_VALID:
14b9c 0a 2a 2a 20 20 20 43 75 72 73 6f 72 20 70 6f 69 .** Cursor poi
14b9d 6e 74 73 20 74 6f 20 61 20 76 61 6c 69 64 20 65 nts to a valid e
14b9e 6e 74 72 79 2e 20 67 65 74 50 61 79 6c 6f 61 64 ntry. getPayload
14b9f 28 29 20 65 74 63 2e 20 6d 61 79 20 62 65 20 63 () etc. may be c
14ba0 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 43 55 52 alled..**.** CUR
14ba1 53 4f 52 5f 49 4e 56 41 4c 49 44 3a 0a 2a 2a 20 SOR_INVALID:.**
14ba2 20 20 43 75 72 73 6f 72 20 64 6f 65 73 20 6e 6f Cursor does no
14ba3 74 20 70 6f 69 6e 74 20 74 6f 20 61 20 76 61 6c t point to a val
14ba4 69 64 20 65 6e 74 72 79 2e 20 54 68 69 73 20 63 id entry. This c
14ba5 61 6e 20 68 61 70 70 65 6e 20 28 66 6f 72 20 65 an happen (for e
14ba6 78 61 6d 70 6c 65 29 20 0a 2a 2a 20 20 20 62 65 xample) .** be
14ba7 63 61 75 73 65 20 74 68 65 20 74 61 62 6c 65 20 cause the table
14ba8 69 73 20 65 6d 70 74 79 20 6f 72 20 62 65 63 61 is empty or beca
14ba9 75 73 65 20 42 74 72 65 65 43 75 72 73 6f 72 46 use BtreeCursorF
14baa 69 72 73 74 28 29 20 68 61 73 20 6e 6f 74 20 62 irst() has not b
14bab 65 65 6e 0a 2a 2a 20 20 20 63 61 6c 6c 65 64 2e een.** called.
14bac 0a 2a 2a 0a 2a 2a 20 43 55 52 53 4f 52 5f 52 45 .**.** CURSOR_RE
14bad 51 55 49 52 45 53 45 45 4b 3a 0a 2a 2a 20 20 20 QUIRESEEK:.**
14bae 54 68 65 20 74 61 62 6c 65 20 74 68 61 74 20 74 The table that t
14baf 68 69 73 20 63 75 72 73 6f 72 20 77 61 73 20 6f his cursor was o
14bb0 70 65 6e 65 64 20 6f 6e 20 73 74 69 6c 6c 20 65 pened on still e
14bb1 78 69 73 74 73 2c 20 62 75 74 20 68 61 73 20 62 xists, but has b
14bb2 65 65 6e 20 0a 2a 2a 20 20 20 6d 6f 64 69 66 69 een .** modifi
14bb3 65 64 20 73 69 6e 63 65 20 74 68 65 20 63 75 72 ed since the cur
14bb4 73 6f 72 20 77 61 73 20 6c 61 73 74 20 75 73 65 sor was last use
14bb5 64 2e 20 54 68 65 20 63 75 72 73 6f 72 20 70 6f d. The cursor po
14bb6 73 69 74 69 6f 6e 20 69 73 20 73 61 76 65 64 0a sition is saved.
14bb7 2a 2a 20 20 20 69 6e 20 76 61 72 69 61 62 6c 65 ** in variable
14bb8 73 20 42 74 43 75 72 73 6f 72 2e 70 4b 65 79 20 s BtCursor.pKey
14bb9 61 6e 64 20 42 74 43 75 72 73 6f 72 2e 6e 4b 65 and BtCursor.nKe
14bba 79 2e 20 57 68 65 6e 20 61 20 63 75 72 73 6f 72 y. When a cursor
14bbb 20 69 73 20 69 6e 20 0a 2a 2a 20 20 20 74 68 69 is in .** thi
14bbc 73 20 73 74 61 74 65 2c 20 72 65 73 74 6f 72 65 s state, restore
14bbd 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 CursorPosition()
14bbe 20 63 61 6e 20 62 65 20 63 61 6c 6c 65 64 20 74 can be called t
14bbf 6f 20 61 74 74 65 6d 70 74 20 74 6f 0a 2a 2a 20 o attempt to.**
14bc0 20 20 73 65 65 6b 20 74 68 65 20 63 75 72 73 6f seek the curso
14bc1 72 20 74 6f 20 74 68 65 20 73 61 76 65 64 20 70 r to the saved p
14bc2 6f 73 69 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 43 osition..**.** C
14bc3 55 52 53 4f 52 5f 46 41 55 4c 54 3a 0a 2a 2a 20 URSOR_FAULT:.**
14bc4 20 20 41 20 75 6e 72 65 63 6f 76 65 72 61 62 6c A unrecoverabl
14bc5 65 20 65 72 72 6f 72 20 28 61 6e 20 49 2f 4f 20 e error (an I/O
14bc6 65 72 72 6f 72 20 6f 72 20 61 20 6d 61 6c 6c 6f error or a mallo
14bc7 63 20 66 61 69 6c 75 72 65 29 20 68 61 73 20 6f c failure) has o
14bc8 63 63 75 72 72 65 64 0a 2a 2a 20 20 20 6f 6e 20 ccurred.** on
14bc9 61 20 64 69 66 66 65 72 65 6e 74 20 63 6f 6e 6e a different conn
14bca 65 63 74 69 6f 6e 20 74 68 61 74 20 73 68 61 72 ection that shar
14bcb 65 73 20 74 68 65 20 42 74 53 68 61 72 65 64 20 es the BtShared
14bcc 63 61 63 68 65 20 77 69 74 68 20 74 68 69 73 0a cache with this.
14bcd 2a 2a 20 20 20 63 75 72 73 6f 72 2e 20 20 54 68 ** cursor. Th
14bce 65 20 65 72 72 6f 72 20 68 61 73 20 6c 65 66 74 e error has left
14bcf 20 74 68 65 20 63 61 63 68 65 20 69 6e 20 61 6e the cache in an
14bd0 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20 73 74 inconsistent st
14bd1 61 74 65 2e 0a 2a 2a 20 20 20 44 6f 20 6e 6f 74 ate..** Do not
14bd2 68 69 6e 67 20 65 6c 73 65 20 77 69 74 68 20 74 hing else with t
14bd3 68 69 73 20 63 75 72 73 6f 72 2e 20 20 41 6e 79 his cursor. Any
14bd4 20 61 74 74 65 6d 70 74 20 74 6f 20 75 73 65 20 attempt to use
14bd5 74 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 20 20 the cursor.**
14bd6 73 68 6f 75 6c 64 20 72 65 74 75 72 6e 20 74 68 should return th
14bd7 65 20 65 72 72 6f 72 20 63 6f 64 65 20 73 74 6f e error code sto
14bd8 72 65 64 20 69 6e 20 42 74 43 75 72 73 6f 72 2e red in BtCursor.
14bd9 73 6b 69 70 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 skip.*/.#define
14bda 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 20 CURSOR_INVALID
14bdb 20 20 20 20 20 20 20 20 20 30 0a 23 64 65 66 69 0.#defi
14bdc 6e 65 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 ne CURSOR_VALID
14bdd 20 20 20 20 20 20 20 20 20 20 20 20 31 0a 23 64 1.#d
14bde 65 66 69 6e 65 20 43 55 52 53 4f 52 5f 52 45 51 efine CURSOR_REQ
14bdf 55 49 52 45 53 45 45 4b 20 20 20 20 20 20 20 32 UIRESEEK 2
14be0 0a 23 64 65 66 69 6e 65 20 43 55 52 53 4f 52 5f .#define CURSOR_
14be1 46 41 55 4c 54 20 20 20 20 20 20 20 20 20 20 20 FAULT
14be2 20 20 33 0a 0a 2f 2a 20 0a 2a 2a 20 54 68 65 20 3../* .** The
14be3 64 61 74 61 62 61 73 65 20 70 61 67 65 20 74 68 database page th
14be4 65 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 6f e PENDING_BYTE o
14be5 63 63 75 70 69 65 73 2e 20 54 68 69 73 20 70 61 ccupies. This pa
14be6 67 65 20 69 73 20 6e 65 76 65 72 20 75 73 65 64 ge is never used
14be7 2e 0a 2a 2f 0a 23 20 64 65 66 69 6e 65 20 50 45 ..*/.# define PE
14be8 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 NDING_BYTE_PAGE(
14be9 70 42 74 29 20 50 41 47 45 52 5f 4d 4a 5f 50 47 pBt) PAGER_MJ_PG
14bea 4e 4f 28 70 42 74 29 0a 0a 2f 2a 0a 2a 2a 20 54 NO(pBt)../*.** T
14beb 68 65 73 65 20 6d 61 63 72 6f 73 20 64 65 66 69 hese macros defi
14bec 6e 65 20 74 68 65 20 6c 6f 63 61 74 69 6f 6e 20 ne the location
14bed 6f 66 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d of the pointer-m
14bee 61 70 20 65 6e 74 72 79 20 66 6f 72 20 61 20 0a ap entry for a .
14bef 2a 2a 20 64 61 74 61 62 61 73 65 20 70 61 67 65 ** database page
14bf0 2e 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75 . The first argu
14bf1 6d 65 6e 74 20 74 6f 20 65 61 63 68 20 69 73 20 ment to each is
14bf2 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 75 73 the number of us
14bf3 61 62 6c 65 0a 2a 2a 20 62 79 74 65 73 20 6f 6e able.** bytes on
14bf4 20 65 61 63 68 20 70 61 67 65 20 6f 66 20 74 68 each page of th
14bf5 65 20 64 61 74 61 62 61 73 65 20 28 6f 66 74 65 e database (ofte
14bf6 6e 20 31 30 32 34 29 2e 20 54 68 65 20 73 65 63 n 1024). The sec
14bf7 6f 6e 64 20 69 73 20 74 68 65 0a 2a 2a 20 70 61 ond is the.** pa
14bf8 67 65 20 6e 75 6d 62 65 72 20 74 6f 20 6c 6f 6f ge number to loo
14bf9 6b 20 75 70 20 69 6e 20 74 68 65 20 70 6f 69 6e k up in the poin
14bfa 74 65 72 20 6d 61 70 2e 0a 2a 2a 0a 2a 2a 20 50 ter map..**.** P
14bfb 54 52 4d 41 50 5f 50 41 47 45 4e 4f 20 72 65 74 TRMAP_PAGENO ret
14bfc 75 72 6e 73 20 74 68 65 20 64 61 74 61 62 61 73 urns the databas
14bfd 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 e page number of
14bfe 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 the pointer-map
14bff 0a 2a 2a 20 70 61 67 65 20 74 68 61 74 20 73 74 .** page that st
14c00 6f 72 65 73 20 74 68 65 20 72 65 71 75 69 72 65 ores the require
14c01 64 20 70 6f 69 6e 74 65 72 2e 20 50 54 52 4d 41 d pointer. PTRMA
14c02 50 5f 50 54 52 4f 46 46 53 45 54 20 72 65 74 75 P_PTROFFSET retu
14c03 72 6e 73 0a 2a 2a 20 74 68 65 20 6f 66 66 73 65 rns.** the offse
14c04 74 20 6f 66 20 74 68 65 20 72 65 71 75 65 73 74 t of the request
14c05 65 64 20 6d 61 70 20 65 6e 74 72 79 2e 0a 2a 2a ed map entry..**
14c06 0a 2a 2a 20 49 66 20 74 68 65 20 70 67 6e 6f 20 .** If the pgno
14c07 61 72 67 75 6d 65 6e 74 20 70 61 73 73 65 64 20 argument passed
14c08 74 6f 20 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f to PTRMAP_PAGENO
14c09 20 69 73 20 61 20 70 6f 69 6e 74 65 72 2d 6d 61 is a pointer-ma
14c0a 70 20 70 61 67 65 2c 0a 2a 2a 20 74 68 65 6e 20 p page,.** then
14c0b 70 67 6e 6f 20 69 73 20 72 65 74 75 72 6e 65 64 pgno is returned
14c0c 2e 20 53 6f 20 28 70 67 6e 6f 3d 3d 50 54 52 4d . So (pgno==PTRM
14c0d 41 50 5f 50 41 47 45 4e 4f 28 70 67 73 7a 2c 20 AP_PAGENO(pgsz,
14c0e 70 67 6e 6f 29 29 20 63 61 6e 20 62 65 0a 2a 2a pgno)) can be.**
14c0f 20 75 73 65 64 20 74 6f 20 74 65 73 74 20 69 66 used to test if
14c10 20 70 67 6e 6f 20 69 73 20 61 20 70 6f 69 6e 74 pgno is a point
14c11 65 72 2d 6d 61 70 20 70 61 67 65 2e 20 50 54 52 er-map page. PTR
14c12 4d 41 50 5f 49 53 50 41 47 45 20 69 6d 70 6c 65 MAP_ISPAGE imple
14c13 6d 65 6e 74 73 0a 2a 2a 20 74 68 69 73 20 74 65 ments.** this te
14c14 73 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 st..*/.#define P
14c15 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70 42 74 TRMAP_PAGENO(pBt
14c16 2c 20 70 67 6e 6f 29 20 70 74 72 6d 61 70 50 61 , pgno) ptrmapPa
14c17 67 65 6e 6f 28 70 42 74 2c 20 70 67 6e 6f 29 0a geno(pBt, pgno).
14c18 23 64 65 66 69 6e 65 20 50 54 52 4d 41 50 5f 50 #define PTRMAP_P
14c19 54 52 4f 46 46 53 45 54 28 70 67 70 74 72 6d 61 TROFFSET(pgptrma
14c1a 70 2c 20 70 67 6e 6f 29 20 28 35 2a 28 70 67 6e p, pgno) (5*(pgn
14c1b 6f 2d 70 67 70 74 72 6d 61 70 2d 31 29 29 0a 23 o-pgptrmap-1)).#
14c1c 64 65 66 69 6e 65 20 50 54 52 4d 41 50 5f 49 53 define PTRMAP_IS
14c1d 50 41 47 45 28 70 42 74 2c 20 70 67 6e 6f 29 20 PAGE(pBt, pgno)
14c1e 28 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 28 (PTRMAP_PAGENO((
14c1f 70 42 74 29 2c 28 70 67 6e 6f 29 29 3d 3d 28 70 pBt),(pgno))==(p
14c20 67 6e 6f 29 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 gno))../*.** The
14c21 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 69 73 20 pointer map is
14c22 61 20 6c 6f 6f 6b 75 70 20 74 61 62 6c 65 20 74 a lookup table t
14c23 68 61 74 20 69 64 65 6e 74 69 66 69 65 73 20 74 hat identifies t
14c24 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 20 66 he parent page f
14c25 6f 72 0a 2a 2a 20 65 61 63 68 20 63 68 69 6c 64 or.** each child
14c26 20 70 61 67 65 20 69 6e 20 74 68 65 20 64 61 74 page in the dat
14c27 61 62 61 73 65 20 66 69 6c 65 2e 20 20 54 68 65 abase file. The
14c28 20 70 61 72 65 6e 74 20 70 61 67 65 20 69 73 20 parent page is
14c29 74 68 65 20 70 61 67 65 20 74 68 61 74 0a 2a 2a the page that.**
14c2a 20 63 6f 6e 74 61 69 6e 73 20 61 20 70 6f 69 6e contains a poin
14c2b 74 65 72 20 74 6f 20 74 68 65 20 63 68 69 6c 64 ter to the child
14c2c 2e 20 20 45 76 65 72 79 20 70 61 67 65 20 69 6e . Every page in
14c2d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f the database co
14c2e 6e 74 61 69 6e 73 0a 2a 2a 20 30 20 6f 72 20 31 ntains.** 0 or 1
14c2f 20 70 61 72 65 6e 74 20 70 61 67 65 73 2e 20 20 parent pages.
14c30 28 49 6e 20 74 68 69 73 20 63 6f 6e 74 65 78 74 (In this context
14c31 20 27 64 61 74 61 62 61 73 65 20 70 61 67 65 27 'database page'
14c32 20 72 65 66 65 72 73 0a 2a 2a 20 74 6f 20 61 6e refers.** to an
14c33 79 20 70 61 67 65 20 74 68 61 74 20 69 73 20 6e y page that is n
14c34 6f 74 20 70 61 72 74 20 6f 66 20 74 68 65 20 70 ot part of the p
14c35 6f 69 6e 74 65 72 20 6d 61 70 20 69 74 73 65 6c ointer map itsel
14c36 66 2e 29 20 20 45 61 63 68 20 70 6f 69 6e 74 65 f.) Each pointe
14c37 72 20 6d 61 70 0a 2a 2a 20 65 6e 74 72 79 20 63 r map.** entry c
14c38 6f 6e 73 69 73 74 73 20 6f 66 20 61 20 73 69 6e onsists of a sin
14c39 67 6c 65 20 62 79 74 65 20 27 74 79 70 65 27 20 gle byte 'type'
14c3a 61 6e 64 20 61 20 34 20 62 79 74 65 20 70 61 72 and a 4 byte par
14c3b 65 6e 74 20 70 61 67 65 20 6e 75 6d 62 65 72 2e ent page number.
14c3c 0a 2a 2a 20 54 68 65 20 50 54 52 4d 41 50 5f 58 .** The PTRMAP_X
14c3d 58 58 20 69 64 65 6e 74 69 66 69 65 72 73 20 62 XX identifiers b
14c3e 65 6c 6f 77 20 61 72 65 20 74 68 65 20 76 61 6c elow are the val
14c3f 69 64 20 74 79 70 65 73 2e 0a 2a 2a 0a 2a 2a 20 id types..**.**
14c40 54 68 65 20 70 75 72 70 6f 73 65 20 6f 66 20 74 The purpose of t
14c41 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 69 he pointer map i
14c42 73 20 74 6f 20 66 61 63 69 6c 69 74 79 20 6d 6f s to facility mo
14c43 76 69 6e 67 20 70 61 67 65 73 20 66 72 6f 6d 20 ving pages from
14c44 6f 6e 65 0a 2a 2a 20 70 6f 73 69 74 69 6f 6e 20 one.** position
14c45 69 6e 20 74 68 65 20 66 69 6c 65 20 74 6f 20 61 in the file to a
14c46 6e 6f 74 68 65 72 20 61 73 20 70 61 72 74 20 6f nother as part o
14c47 66 20 61 75 74 6f 76 61 63 75 75 6d 2e 20 20 57 f autovacuum. W
14c48 68 65 6e 20 61 20 70 61 67 65 0a 2a 2a 20 69 73 hen a page.** is
14c49 20 6d 6f 76 65 64 2c 20 74 68 65 20 70 6f 69 6e moved, the poin
14c4a 74 65 72 20 69 6e 20 69 74 73 20 70 61 72 65 6e ter in its paren
14c4b 74 20 6d 75 73 74 20 62 65 20 75 70 64 61 74 65 t must be update
14c4c 64 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 d to point to th
14c4d 65 0a 2a 2a 20 6e 65 77 20 6c 6f 63 61 74 69 6f e.** new locatio
14c4e 6e 2e 20 20 54 68 65 20 70 6f 69 6e 74 65 72 20 n. The pointer
14c4f 6d 61 70 20 69 73 20 75 73 65 64 20 74 6f 20 6c map is used to l
14c50 6f 63 61 74 65 20 74 68 65 20 70 61 72 65 6e 74 ocate the parent
14c51 20 70 61 67 65 20 71 75 69 63 6b 6c 79 2e 0a 2a page quickly..*
14c52 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f 52 4f 4f 54 *.** PTRMAP_ROOT
14c53 50 41 47 45 3a 20 54 68 65 20 64 61 74 61 62 61 PAGE: The databa
14c54 73 65 20 70 61 67 65 20 69 73 20 61 20 72 6f 6f se page is a roo
14c55 74 2d 70 61 67 65 2e 20 54 68 65 20 70 61 67 65 t-page. The page
14c56 2d 6e 75 6d 62 65 72 20 69 73 20 6e 6f 74 0a 2a -number is not.*
14c57 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
14c58 20 20 20 75 73 65 64 20 69 6e 20 74 68 69 73 20 used in this
14c59 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d case..**.** PTRM
14c5a 41 50 5f 46 52 45 45 50 41 47 45 3a 20 54 68 65 AP_FREEPAGE: The
14c5b 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 69 database page i
14c5c 73 20 61 6e 20 75 6e 75 73 65 64 20 28 66 72 65 s an unused (fre
14c5d 65 29 20 70 61 67 65 2e 20 54 68 65 20 70 61 67 e) page. The pag
14c5e 65 2d 6e 75 6d 62 65 72 20 0a 2a 2a 20 20 20 20 e-number .**
14c5f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 73 is
14c60 20 6e 6f 74 20 75 73 65 64 20 69 6e 20 74 68 69 not used in thi
14c61 73 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 50 54 s case..**.** PT
14c62 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 3a 20 RMAP_OVERFLOW1:
14c63 54 68 65 20 64 61 74 61 62 61 73 65 20 70 61 67 The database pag
14c64 65 20 69 73 20 74 68 65 20 66 69 72 73 74 20 70 e is the first p
14c65 61 67 65 20 69 6e 20 61 20 6c 69 73 74 20 6f 66 age in a list of
14c66 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 .**
14c67 20 20 20 20 20 20 20 6f 76 65 72 66 6c 6f 77 20 overflow
14c68 70 61 67 65 73 2e 20 54 68 65 20 70 61 67 65 20 pages. The page
14c69 6e 75 6d 62 65 72 20 69 64 65 6e 74 69 66 69 65 number identifie
14c6a 73 20 74 68 65 20 70 61 67 65 20 74 68 61 74 0a s the page that.
14c6b 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
14c6c 20 20 20 20 20 63 6f 6e 74 61 69 6e 73 20 74 68 contains th
14c6d 65 20 63 65 6c 6c 20 77 69 74 68 20 61 20 70 6f e cell with a po
14c6e 69 6e 74 65 72 20 74 6f 20 74 68 69 73 20 6f 76 inter to this ov
14c6f 65 72 66 6c 6f 77 20 70 61 67 65 2e 0a 2a 2a 0a erflow page..**.
14c70 2a 2a 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c ** PTRMAP_OVERFL
14c71 4f 57 32 3a 20 54 68 65 20 64 61 74 61 62 61 73 OW2: The databas
14c72 65 20 70 61 67 65 20 69 73 20 74 68 65 20 73 65 e page is the se
14c73 63 6f 6e 64 20 6f 72 20 6c 61 74 65 72 20 70 61 cond or later pa
14c74 67 65 20 69 6e 20 61 20 6c 69 73 74 20 6f 66 0a ge in a list of.
14c75 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
14c76 20 20 20 20 20 6f 76 65 72 66 6c 6f 77 20 70 61 overflow pa
14c77 67 65 73 2e 20 54 68 65 20 70 61 67 65 2d 6e 75 ges. The page-nu
14c78 6d 62 65 72 20 69 64 65 6e 74 69 66 69 65 73 20 mber identifies
14c79 74 68 65 20 70 72 65 76 69 6f 75 73 0a 2a 2a 20 the previous.**
14c7a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14c7b 20 20 70 61 67 65 20 69 6e 20 74 68 65 20 6f 76 page in the ov
14c7c 65 72 66 6c 6f 77 20 70 61 67 65 20 6c 69 73 74 erflow page list
14c7d 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f 42 ..**.** PTRMAP_B
14c7e 54 52 45 45 3a 20 54 68 65 20 64 61 74 61 62 61 TREE: The databa
14c7f 73 65 20 70 61 67 65 20 69 73 20 61 20 6e 6f 6e se page is a non
14c80 2d 72 6f 6f 74 20 62 74 72 65 65 20 70 61 67 65 -root btree page
14c81 2e 20 54 68 65 20 70 61 67 65 20 6e 75 6d 62 65 . The page numbe
14c82 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 r.**
14c83 20 20 20 69 64 65 6e 74 69 66 69 65 73 20 74 68 identifies th
14c84 65 20 70 61 72 65 6e 74 20 70 61 67 65 20 69 6e e parent page in
14c85 20 74 68 65 20 62 74 72 65 65 2e 0a 2a 2f 0a 23 the btree..*/.#
14c86 64 65 66 69 6e 65 20 50 54 52 4d 41 50 5f 52 4f define PTRMAP_RO
14c87 4f 54 50 41 47 45 20 31 0a 23 64 65 66 69 6e 65 OTPAGE 1.#define
14c88 20 50 54 52 4d 41 50 5f 46 52 45 45 50 41 47 45 PTRMAP_FREEPAGE
14c89 20 32 0a 23 64 65 66 69 6e 65 20 50 54 52 4d 41 2.#define PTRMA
14c8a 50 5f 4f 56 45 52 46 4c 4f 57 31 20 33 0a 23 64 P_OVERFLOW1 3.#d
14c8b 65 66 69 6e 65 20 50 54 52 4d 41 50 5f 4f 56 45 efine PTRMAP_OVE
14c8c 52 46 4c 4f 57 32 20 34 0a 23 64 65 66 69 6e 65 RFLOW2 4.#define
14c8d 20 50 54 52 4d 41 50 5f 42 54 52 45 45 20 35 0a PTRMAP_BTREE 5.
14c8e 0a 2f 2a 20 41 20 62 75 6e 63 68 20 6f 66 20 61 ./* A bunch of a
14c8f 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e ssert() statemen
14c90 74 73 20 74 6f 20 63 68 65 63 6b 20 74 68 65 20 ts to check the
14c91 74 72 61 6e 73 61 63 74 69 6f 6e 20 73 74 61 74 transaction stat
14c92 65 20 76 61 72 69 61 62 6c 65 73 0a 2a 2a 20 6f e variables.** o
14c93 66 20 68 61 6e 64 6c 65 20 70 20 28 74 79 70 65 f handle p (type
14c94 20 42 74 72 65 65 2a 29 20 61 72 65 20 69 6e 74 Btree*) are int
14c95 65 72 6e 61 6c 6c 79 20 63 6f 6e 73 69 73 74 65 ernally consiste
14c96 6e 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 62 nt..*/.#define b
14c97 74 72 65 65 49 6e 74 65 67 72 69 74 79 28 70 29 treeIntegrity(p)
14c98 20 5c 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e \. assert( p->
14c99 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 pBt->inTransacti
14c9a 6f 6e 21 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 7c on!=TRANS_NONE |
14c9b 7c 20 70 2d 3e 70 42 74 2d 3e 6e 54 72 61 6e 73 | p->pBt->nTrans
14c9c 61 63 74 69 6f 6e 3d 3d 30 20 29 3b 20 5c 0a 20 action==0 ); \.
14c9d 20 61 73 73 65 72 74 28 20 70 2d 3e 70 42 74 2d assert( p->pBt-
14c9e 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3e 3d >inTransaction>=
14c9f 70 2d 3e 69 6e 54 72 61 6e 73 20 29 3b 20 0a 0a p->inTrans ); ..
14ca0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 49 53 41 55 54 ./*.** The ISAUT
14ca1 4f 56 41 43 55 55 4d 20 6d 61 63 72 6f 20 69 73 OVACUUM macro is
14ca2 20 75 73 65 64 20 77 69 74 68 69 6e 20 62 61 6c used within bal
14ca3 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 29 20 74 ance_nonroot() t
14ca4 6f 20 64 65 74 65 72 6d 69 6e 65 0a 2a 2a 20 69 o determine.** i
14ca5 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 f the database s
14ca6 75 70 70 6f 72 74 73 20 61 75 74 6f 2d 76 61 63 upports auto-vac
14ca7 75 75 6d 20 6f 72 20 6e 6f 74 2e 20 42 65 63 61 uum or not. Beca
14ca8 75 73 65 20 69 74 20 69 73 20 75 73 65 64 0a 2a use it is used.*
14ca9 2a 20 77 69 74 68 69 6e 20 61 6e 20 65 78 70 72 * within an expr
14caa 65 73 73 69 6f 6e 20 74 68 61 74 20 69 73 20 61 ession that is a
14cab 6e 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 61 6e n argument to an
14cac 6f 74 68 65 72 20 6d 61 63 72 6f 20 0a 2a 2a 20 other macro .**
14cad 28 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 52 61 77 (sqliteMallocRaw
14cae 29 2c 20 69 74 20 69 73 20 6e 6f 74 20 70 6f 73 ), it is not pos
14caf 73 69 62 6c 65 20 74 6f 20 75 73 65 20 63 6f 6e sible to use con
14cb0 64 69 74 69 6f 6e 61 6c 20 63 6f 6d 70 69 6c 61 ditional compila
14cb1 74 69 6f 6e 2e 0a 2a 2a 20 53 6f 2c 20 74 68 69 tion..** So, thi
14cb2 73 20 6d 61 63 72 6f 20 69 73 20 64 65 66 69 6e s macro is defin
14cb3 65 64 20 69 6e 73 74 65 61 64 2e 0a 2a 2f 0a 23 ed instead..*/.#
14cb4 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
14cb5 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 23 64 IT_AUTOVACUUM.#d
14cb6 65 66 69 6e 65 20 49 53 41 55 54 4f 56 41 43 55 efine ISAUTOVACU
14cb7 55 4d 20 28 70 42 74 2d 3e 61 75 74 6f 56 61 63 UM (pBt->autoVac
14cb8 75 75 6d 29 0a 23 65 6c 73 65 0a 23 64 65 66 69 uum).#else.#defi
14cb9 6e 65 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20 ne ISAUTOVACUUM
14cba 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 0.#endif.../*.**
14cbb 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65 20 This structure
14cbc 69 73 20 70 61 73 73 65 64 20 61 72 6f 75 6e 64 is passed around
14cbd 20 74 68 72 6f 75 67 68 20 61 6c 6c 20 74 68 65 through all the
14cbe 20 73 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 sanity checking
14cbf 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 69 6e 20 routines.** in
14cc0 6f 72 64 65 72 20 74 6f 20 6b 65 65 70 20 74 72 order to keep tr
14cc1 61 63 6b 20 6f 66 20 73 6f 6d 65 20 67 6c 6f 62 ack of some glob
14cc2 61 6c 20 73 74 61 74 65 20 69 6e 66 6f 72 6d 61 al state informa
14cc3 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 tion..*/.typedef
14cc4 20 73 74 72 75 63 74 20 49 6e 74 65 67 72 69 74 struct Integrit
14cc5 79 43 6b 20 49 6e 74 65 67 72 69 74 79 43 6b 3b yCk IntegrityCk;
14cc6 0a 73 74 72 75 63 74 20 49 6e 74 65 67 72 69 74 .struct Integrit
14cc7 79 43 6b 20 7b 0a 20 20 42 74 53 68 61 72 65 64 yCk {. BtShared
14cc8 20 2a 70 42 74 3b 20 20 20 20 2f 2a 20 54 68 65 *pBt; /* The
14cc9 20 74 72 65 65 20 62 65 69 6e 67 20 63 68 65 63 tree being chec
14cca 6b 65 64 20 6f 75 74 20 2a 2f 0a 20 20 50 61 67 ked out */. Pag
14ccb 65 72 20 2a 70 50 61 67 65 72 3b 20 20 20 20 2f er *pPager; /
14ccc 2a 20 54 68 65 20 61 73 73 6f 63 69 61 74 65 64 * The associated
14ccd 20 70 61 67 65 72 2e 20 20 41 6c 73 6f 20 61 63 pager. Also ac
14cce 63 65 73 73 69 62 6c 65 20 62 79 20 70 42 74 2d cessible by pBt-
14ccf 3e 70 50 61 67 65 72 20 2a 2f 0a 20 20 50 67 6e >pPager */. Pgn
14cd0 6f 20 6e 50 61 67 65 3b 20 20 20 20 20 20 20 2f o nPage; /
14cd1 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 * Number of page
14cd2 73 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 s in the databas
14cd3 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 6e 52 65 e */. int *anRe
14cd4 66 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 f; /* Numb
14cd5 65 72 20 6f 66 20 74 69 6d 65 73 20 65 61 63 68 er of times each
14cd6 20 70 61 67 65 20 69 73 20 72 65 66 65 72 65 6e page is referen
14cd7 63 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 45 ced */. int mxE
14cd8 72 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 74 rr; /* St
14cd9 6f 70 20 61 63 63 75 6d 75 6c 61 74 69 6e 67 20 op accumulating
14cda 65 72 72 6f 72 73 20 77 68 65 6e 20 74 68 69 73 errors when this
14cdb 20 72 65 61 63 68 65 73 20 7a 65 72 6f 20 2a 2f reaches zero */
14cdc 0a 20 20 69 6e 74 20 6e 45 72 72 3b 20 20 20 20 . int nErr;
14cdd 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
14cde 66 20 6d 65 73 73 61 67 65 73 20 77 72 69 74 74 f messages writt
14cdf 65 6e 20 74 6f 20 7a 45 72 72 4d 73 67 20 73 6f en to zErrMsg so
14ce0 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20 6d 61 far */. int ma
14ce1 6c 6c 6f 63 46 61 69 6c 65 64 3b 20 2f 2a 20 41 llocFailed; /* A
14ce2 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
14ce3 6f 6e 20 65 72 72 6f 72 20 68 61 73 20 6f 63 63 on error has occ
14ce4 75 72 72 65 64 20 2a 2f 0a 20 20 53 74 72 41 63 urred */. StrAc
14ce5 63 75 6d 20 65 72 72 4d 73 67 3b 20 20 2f 2a 20 cum errMsg; /*
14ce6 41 63 63 75 6d 75 6c 61 74 65 20 74 68 65 20 65 Accumulate the e
14ce7 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74 65 78 rror message tex
14ce8 74 20 68 65 72 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a t here */.};../*
14ce9 0a 2a 2a 20 52 65 61 64 20 6f 72 20 77 72 69 74 .** Read or writ
14cea 65 20 61 20 74 77 6f 2d 20 61 6e 64 20 66 6f 75 e a two- and fou
14ceb 72 2d 62 79 74 65 20 62 69 67 2d 65 6e 64 69 61 r-byte big-endia
14cec 6e 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 73 n integer values
14ced 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 67 65 74 ..*/.#define get
14cee 32 62 79 74 65 28 78 29 20 20 20 28 28 78 29 5b 2byte(x) ((x)[
14cef 30 5d 3c 3c 38 20 7c 20 28 78 29 5b 31 5d 29 0a 0]<<8 | (x)[1]).
14cf0 23 64 65 66 69 6e 65 20 70 75 74 32 62 79 74 65 #define put2byte
14cf1 28 70 2c 76 29 20 28 28 70 29 5b 30 5d 20 3d 20 (p,v) ((p)[0] =
14cf2 28 75 38 29 28 28 76 29 3e 3e 38 29 2c 20 28 70 (u8)((v)>>8), (p
14cf3 29 5b 31 5d 20 3d 20 28 75 38 29 28 76 29 29 0a )[1] = (u8)(v)).
14cf4 23 64 65 66 69 6e 65 20 67 65 74 34 62 79 74 65 #define get4byte
14cf5 20 73 71 6c 69 74 65 33 47 65 74 34 62 79 74 65 sqlite3Get4byte
14cf6 0a 23 64 65 66 69 6e 65 20 70 75 74 34 62 79 74 .#define put4byt
14cf7 65 20 73 71 6c 69 74 65 33 50 75 74 34 62 79 74 e sqlite3Put4byt
14cf8 65 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e../************
14cf9 2a 2a 20 45 6e 64 20 6f 66 20 62 74 72 65 65 49 ** End of btreeI
14cfa 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a nt.h ***********
14cfb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14cfc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14cfd 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
14cfe 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 ** Continuing wh
14cff 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 ere we left off
14d00 69 6e 20 62 74 6d 75 74 65 78 2e 63 20 2a 2a 2a in btmutex.c ***
14d01 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14d02 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 */.#ifndef SQLIT
14d03 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 E_OMIT_SHARED_CA
14d04 43 48 45 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 CHE.#if SQLITE_T
14d05 48 52 45 41 44 53 41 46 45 0a 0a 2f 2a 0a 2a 2a HREADSAFE../*.**
14d06 20 4f 62 74 61 69 6e 20 74 68 65 20 42 74 53 68 Obtain the BtSh
14d07 61 72 65 64 20 6d 75 74 65 78 20 61 73 73 6f 63 ared mutex assoc
14d08 69 61 74 65 64 20 77 69 74 68 20 42 2d 54 72 65 iated with B-Tre
14d09 65 20 68 61 6e 64 6c 65 20 70 2e 20 41 6c 73 6f e handle p. Also
14d0a 2c 0a 2a 2a 20 73 65 74 20 42 74 53 68 61 72 65 ,.** set BtShare
14d0b 64 2e 64 62 20 74 6f 20 74 68 65 20 64 61 74 61 d.db to the data
14d0c 62 61 73 65 20 68 61 6e 64 6c 65 20 61 73 73 6f base handle asso
14d0d 63 69 61 74 65 64 20 77 69 74 68 20 70 20 61 6e ciated with p an
14d0e 64 20 74 68 65 0a 2a 2a 20 70 2d 3e 6c 6f 63 6b d the.** p->lock
14d0f 65 64 20 62 6f 6f 6c 65 61 6e 20 74 6f 20 74 72 ed boolean to tr
14d10 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ue..*/.static vo
14d11 69 64 20 6c 6f 63 6b 42 74 72 65 65 4d 75 74 65 id lockBtreeMute
14d12 78 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 61 x(Btree *p){. a
14d13 73 73 65 72 74 28 20 70 2d 3e 6c 6f 63 6b 65 64 ssert( p->locked
14d14 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ==0 );. assert(
14d15 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e sqlite3_mutex_n
14d16 6f 74 68 65 6c 64 28 70 2d 3e 70 42 74 2d 3e 6d otheld(p->pBt->m
14d17 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 utex) );. asser
14d18 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
14d19 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 _held(p->db->mut
14d1a 65 78 29 20 29 3b 0a 0a 20 20 73 71 6c 69 74 65 ex) );.. sqlite
14d1b 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 2d 3_mutex_enter(p-
14d1c 3e 70 42 74 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 >pBt->mutex);.
14d1d 70 2d 3e 70 42 74 2d 3e 64 62 20 3d 20 70 2d 3e p->pBt->db = p->
14d1e 64 62 3b 0a 20 20 70 2d 3e 6c 6f 63 6b 65 64 20 db;. p->locked
14d1f 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 = 1;.}../*.** Re
14d20 6c 65 61 73 65 20 74 68 65 20 42 74 53 68 61 72 lease the BtShar
14d21 65 64 20 6d 75 74 65 78 20 61 73 73 6f 63 69 61 ed mutex associa
14d22 74 65 64 20 77 69 74 68 20 42 2d 54 72 65 65 20 ted with B-Tree
14d23 68 61 6e 64 6c 65 20 70 20 61 6e 64 0a 2a 2a 20 handle p and.**
14d24 63 6c 65 61 72 20 74 68 65 20 70 2d 3e 6c 6f 63 clear the p->loc
14d25 6b 65 64 20 62 6f 6f 6c 65 61 6e 2e 0a 2a 2f 0a ked boolean..*/.
14d26 73 74 61 74 69 63 20 76 6f 69 64 20 75 6e 6c 6f static void unlo
14d27 63 6b 42 74 72 65 65 4d 75 74 65 78 28 42 74 72 ckBtreeMutex(Btr
14d28 65 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 ee *p){. assert
14d29 28 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 31 20 29 ( p->locked==1 )
14d2a 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
14d2b 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
14d2c 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b ->pBt->mutex) );
14d2d 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
14d2e 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d e3_mutex_held(p-
14d2f 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 >db->mutex) );.
14d30 20 61 73 73 65 72 74 28 20 70 2d 3e 64 62 3d 3d assert( p->db==
14d31 70 2d 3e 70 42 74 2d 3e 64 62 20 29 3b 0a 0a 20 p->pBt->db );..
14d32 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
14d33 65 61 76 65 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 eave(p->pBt->mut
14d34 65 78 29 3b 0a 20 20 70 2d 3e 6c 6f 63 6b 65 64 ex);. p->locked
14d35 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 = 0;.}../*.** E
14d36 6e 74 65 72 20 61 20 6d 75 74 65 78 20 6f 6e 20 nter a mutex on
14d37 74 68 65 20 67 69 76 65 6e 20 42 54 72 65 65 20 the given BTree
14d38 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 object..**.** If
14d39 20 74 68 65 20 6f 62 6a 65 63 74 20 69 73 20 6e the object is n
14d3a 6f 74 20 73 68 61 72 61 62 6c 65 2c 20 74 68 65 ot sharable, the
14d3b 6e 20 6e 6f 20 6d 75 74 65 78 20 69 73 20 65 76 n no mutex is ev
14d3c 65 72 20 72 65 71 75 69 72 65 64 0a 2a 2a 20 61 er required.** a
14d3d 6e 64 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 nd this routine
14d3e 69 73 20 61 20 6e 6f 2d 6f 70 2e 20 20 54 68 65 is a no-op. The
14d3f 20 75 6e 64 65 72 6c 79 69 6e 67 20 6d 75 74 65 underlying mute
14d40 78 20 69 73 20 6e 6f 6e 2d 72 65 63 75 72 73 69 x is non-recursi
14d41 76 65 2e 0a 2a 2a 20 42 75 74 20 77 65 20 6b 65 ve..** But we ke
14d42 65 70 20 61 20 72 65 66 65 72 65 6e 63 65 20 63 ep a reference c
14d43 6f 75 6e 74 20 69 6e 20 42 74 72 65 65 2e 77 61 ount in Btree.wa
14d44 6e 74 54 6f 4c 6f 63 6b 20 73 6f 20 74 68 65 20 ntToLock so the
14d45 62 65 68 61 76 69 6f 72 0a 2a 2a 20 6f 66 20 74 behavior.** of t
14d46 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 69 73 his interface is
14d47 20 72 65 63 75 72 73 69 76 65 2e 0a 2a 2a 0a 2a recursive..**.*
14d48 2a 20 54 6f 20 61 76 6f 69 64 20 64 65 61 64 6c * To avoid deadl
14d49 6f 63 6b 73 2c 20 6d 75 6c 74 69 70 6c 65 20 42 ocks, multiple B
14d4a 74 72 65 65 73 20 61 72 65 20 6c 6f 63 6b 65 64 trees are locked
14d4b 20 69 6e 20 74 68 65 20 73 61 6d 65 20 6f 72 64 in the same ord
14d4c 65 72 0a 2a 2a 20 62 79 20 61 6c 6c 20 64 61 74 er.** by all dat
14d4d 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
14d4e 73 2e 20 20 54 68 65 20 70 2d 3e 70 4e 65 78 74 s. The p->pNext
14d4f 20 69 73 20 61 20 6c 69 73 74 20 6f 66 20 6f 74 is a list of ot
14d50 68 65 72 0a 2a 2a 20 42 74 72 65 65 73 20 62 65 her.** Btrees be
14d51 6c 6f 6e 67 69 6e 67 20 74 6f 20 74 68 65 20 73 longing to the s
14d52 61 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e ame database con
14d53 6e 65 63 74 69 6f 6e 20 61 73 20 74 68 65 20 70 nection as the p
14d54 20 42 74 72 65 65 0a 2a 2a 20 77 68 69 63 68 20 Btree.** which
14d55 6e 65 65 64 20 74 6f 20 62 65 20 6c 6f 63 6b 65 need to be locke
14d56 64 20 61 66 74 65 72 20 70 2e 20 20 49 66 20 77 d after p. If w
14d57 65 20 63 61 6e 6e 6f 74 20 67 65 74 20 61 20 6c e cannot get a l
14d58 6f 63 6b 20 6f 6e 0a 2a 2a 20 70 2c 20 74 68 65 ock on.** p, the
14d59 6e 20 66 69 72 73 74 20 75 6e 6c 6f 63 6b 20 61 n first unlock a
14d5a 6c 6c 20 6f 66 20 74 68 65 20 6f 74 68 65 72 73 ll of the others
14d5b 20 6f 6e 20 70 2d 3e 70 4e 65 78 74 2c 20 74 68 on p->pNext, th
14d5c 65 6e 20 77 61 69 74 0a 2a 2a 20 66 6f 72 20 74 en wait.** for t
14d5d 68 65 20 6c 6f 63 6b 20 74 6f 20 62 65 63 6f 6d he lock to becom
14d5e 65 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 70 e available on p
14d5f 2c 20 74 68 65 6e 20 72 65 6c 6f 63 6b 20 61 6c , then relock al
14d60 6c 20 6f 66 20 74 68 65 0a 2a 2a 20 73 75 62 73 l of the.** subs
14d61 65 71 75 65 6e 74 20 42 74 72 65 65 73 20 74 68 equent Btrees th
14d62 61 74 20 64 65 73 69 72 65 20 61 20 6c 6f 63 6b at desire a lock
14d63 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
14d64 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
14d65 42 74 72 65 65 45 6e 74 65 72 28 42 74 72 65 65 BtreeEnter(Btree
14d66 20 2a 70 29 7b 0a 20 20 42 74 72 65 65 20 2a 70 *p){. Btree *p
14d67 4c 61 74 65 72 3b 0a 0a 20 20 2f 2a 20 53 6f 6d Later;.. /* Som
14d68 65 20 62 61 73 69 63 20 73 61 6e 69 74 79 20 63 e basic sanity c
14d69 68 65 63 6b 69 6e 67 20 6f 6e 20 74 68 65 20 42 hecking on the B
14d6a 74 72 65 65 2e 20 20 54 68 65 20 6c 69 73 74 20 tree. The list
14d6b 6f 66 20 42 74 72 65 65 73 0a 20 20 2a 2a 20 63 of Btrees. ** c
14d6c 6f 6e 6e 65 63 74 65 64 20 62 79 20 70 4e 65 78 onnected by pNex
14d6d 74 20 61 6e 64 20 70 50 72 65 76 20 73 68 6f 75 t and pPrev shou
14d6e 6c 64 20 62 65 20 69 6e 20 73 6f 72 74 65 64 20 ld be in sorted
14d6f 6f 72 64 65 72 20 62 79 0a 20 20 2a 2a 20 42 74 order by. ** Bt
14d70 72 65 65 2e 70 42 74 20 76 61 6c 75 65 2e 20 41 ree.pBt value. A
14d71 6c 6c 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 ll elements of t
14d72 68 65 20 6c 69 73 74 20 73 68 6f 75 6c 64 20 62 he list should b
14d73 65 6c 6f 6e 67 20 74 6f 0a 20 20 2a 2a 20 74 68 elong to. ** th
14d74 65 20 73 61 6d 65 20 63 6f 6e 6e 65 63 74 69 6f e same connectio
14d75 6e 2e 20 4f 6e 6c 79 20 73 68 61 72 65 64 20 42 n. Only shared B
14d76 74 72 65 65 73 20 61 72 65 20 6f 6e 20 74 68 65 trees are on the
14d77 20 6c 69 73 74 2e 20 2a 2f 0a 20 20 61 73 73 65 list. */. asse
14d78 72 74 28 20 70 2d 3e 70 4e 65 78 74 3d 3d 30 20 rt( p->pNext==0
14d79 7c 7c 20 70 2d 3e 70 4e 65 78 74 2d 3e 70 42 74 || p->pNext->pBt
14d7a 3e 70 2d 3e 70 42 74 20 29 3b 0a 20 20 61 73 73 >p->pBt );. ass
14d7b 65 72 74 28 20 70 2d 3e 70 50 72 65 76 3d 3d 30 ert( p->pPrev==0
14d7c 20 7c 7c 20 70 2d 3e 70 50 72 65 76 2d 3e 70 42 || p->pPrev->pB
14d7d 74 3c 70 2d 3e 70 42 74 20 29 3b 0a 20 20 61 73 t<p->pBt );. as
14d7e 73 65 72 74 28 20 70 2d 3e 70 4e 65 78 74 3d 3d sert( p->pNext==
14d7f 30 20 7c 7c 20 70 2d 3e 70 4e 65 78 74 2d 3e 64 0 || p->pNext->d
14d80 62 3d 3d 70 2d 3e 64 62 20 29 3b 0a 20 20 61 73 b==p->db );. as
14d81 73 65 72 74 28 20 70 2d 3e 70 50 72 65 76 3d 3d sert( p->pPrev==
14d82 30 20 7c 7c 20 70 2d 3e 70 50 72 65 76 2d 3e 64 0 || p->pPrev->d
14d83 62 3d 3d 70 2d 3e 64 62 20 29 3b 0a 20 20 61 73 b==p->db );. as
14d84 73 65 72 74 28 20 70 2d 3e 73 68 61 72 61 62 6c sert( p->sharabl
14d85 65 20 7c 7c 20 28 70 2d 3e 70 4e 65 78 74 3d 3d e || (p->pNext==
14d86 30 20 26 26 20 70 2d 3e 70 50 72 65 76 3d 3d 30 0 && p->pPrev==0
14d87 29 20 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b ) );.. /* Check
14d88 20 66 6f 72 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e for locking con
14d89 73 69 73 74 65 6e 63 79 20 2a 2f 0a 20 20 61 73 sistency */. as
14d8a 73 65 72 74 28 20 21 70 2d 3e 6c 6f 63 6b 65 64 sert( !p->locked
14d8b 20 7c 7c 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 || p->wantToLoc
14d8c 6b 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 k>0 );. assert(
14d8d 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 7c 7c 20 p->sharable ||
14d8e 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 p->wantToLock==0
14d8f 20 29 3b 0a 0a 20 20 2f 2a 20 57 65 20 73 68 6f );.. /* We sho
14d90 75 6c 64 20 61 6c 72 65 61 64 79 20 68 6f 6c 64 uld already hold
14d91 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 a lock on the d
14d92 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
14d93 6f 6e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 on */. assert(
14d94 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
14d95 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 ld(p->db->mutex)
14d96 20 29 3b 0a 0a 20 20 2f 2a 20 55 6e 6c 65 73 73 );.. /* Unless
14d97 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73 the database is
14d98 20 73 68 61 72 61 62 6c 65 20 61 6e 64 20 75 6e sharable and un
14d99 6c 6f 63 6b 65 64 2c 20 74 68 65 6e 20 42 74 53 locked, then BtS
14d9a 68 61 72 65 64 2e 64 62 0a 20 20 2a 2a 20 73 68 hared.db. ** sh
14d9b 6f 75 6c 64 20 61 6c 72 65 61 64 79 20 62 65 20 ould already be
14d9c 73 65 74 20 63 6f 72 72 65 63 74 6c 79 2e 20 2a set correctly. *
14d9d 2f 0a 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e /. assert( (p->
14d9e 6c 6f 63 6b 65 64 3d 3d 30 20 26 26 20 70 2d 3e locked==0 && p->
14d9f 73 68 61 72 61 62 6c 65 29 20 7c 7c 20 70 2d 3e sharable) || p->
14da0 70 42 74 2d 3e 64 62 3d 3d 70 2d 3e 64 62 20 29 pBt->db==p->db )
14da1 3b 0a 0a 20 20 69 66 28 20 21 70 2d 3e 73 68 61 ;.. if( !p->sha
14da2 72 61 62 6c 65 20 29 20 72 65 74 75 72 6e 3b 0a rable ) return;.
14da3 20 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 2b p->wantToLock+
14da4 2b 3b 0a 20 20 69 66 28 20 70 2d 3e 6c 6f 63 6b +;. if( p->lock
14da5 65 64 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 ed ) return;..
14da6 2f 2a 20 49 6e 20 6d 6f 73 74 20 63 61 73 65 73 /* In most cases
14da7 2c 20 77 65 20 73 68 6f 75 6c 64 20 62 65 20 61 , we should be a
14da8 62 6c 65 20 74 6f 20 61 63 71 75 69 72 65 20 74 ble to acquire t
14da9 68 65 20 6c 6f 63 6b 20 77 65 0a 20 20 2a 2a 20 he lock we. **
14daa 77 61 6e 74 20 77 69 74 68 6f 75 74 20 68 61 76 want without hav
14dab 69 6e 67 20 74 6f 20 67 6f 20 74 68 72 6f 75 67 ing to go throug
14dac 68 74 20 74 68 65 20 61 73 63 65 6e 64 69 6e 67 ht the ascending
14dad 20 6c 6f 63 6b 0a 20 20 2a 2a 20 70 72 6f 63 65 lock. ** proce
14dae 64 75 72 65 20 74 68 61 74 20 66 6f 6c 6c 6f 77 dure that follow
14daf 73 2e 20 20 4a 75 73 74 20 62 65 20 73 75 72 65 s. Just be sure
14db0 20 6e 6f 74 20 74 6f 20 62 6c 6f 63 6b 2e 0a 20 not to block..
14db1 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 */. if( sqlite
14db2 33 5f 6d 75 74 65 78 5f 74 72 79 28 70 2d 3e 70 3_mutex_try(p->p
14db3 42 74 2d 3e 6d 75 74 65 78 29 3d 3d 53 51 4c 49 Bt->mutex)==SQLI
14db4 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 2d 3e TE_OK ){. p->
14db5 70 42 74 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b pBt->db = p->db;
14db6 0a 20 20 20 20 70 2d 3e 6c 6f 63 6b 65 64 20 3d . p->locked =
14db7 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 1;. return;.
14db8 20 20 7d 0a 0a 20 20 2f 2a 20 54 6f 20 61 76 6f }.. /* To avo
14db9 69 64 20 64 65 61 64 6c 6f 63 6b 2c 20 66 69 72 id deadlock, fir
14dba 73 74 20 72 65 6c 65 61 73 65 20 61 6c 6c 20 6c st release all l
14dbb 6f 63 6b 73 20 77 69 74 68 20 61 20 6c 61 72 67 ocks with a larg
14dbc 65 72 0a 20 20 2a 2a 20 42 74 53 68 61 72 65 64 er. ** BtShared
14dbd 20 61 64 64 72 65 73 73 2e 20 20 54 68 65 6e 20 address. Then
14dbe 61 63 71 75 69 72 65 20 6f 75 72 20 6c 6f 63 6b acquire our lock
14dbf 2e 20 20 54 68 65 6e 20 72 65 61 63 71 75 69 72 . Then reacquir
14dc0 65 0a 20 20 2a 2a 20 74 68 65 20 6f 74 68 65 72 e. ** the other
14dc1 20 42 74 53 68 61 72 65 64 20 6c 6f 63 6b 73 20 BtShared locks
14dc2 74 68 61 74 20 77 65 20 75 73 65 64 20 74 6f 20 that we used to
14dc3 68 6f 6c 64 20 69 6e 20 61 73 63 65 6e 64 69 6e hold in ascendin
14dc4 67 0a 20 20 2a 2a 20 6f 72 64 65 72 2e 0a 20 20 g. ** order..
14dc5 2a 2f 0a 20 20 66 6f 72 28 70 4c 61 74 65 72 3d */. for(pLater=
14dc6 70 2d 3e 70 4e 65 78 74 3b 20 70 4c 61 74 65 72 p->pNext; pLater
14dc7 3b 20 70 4c 61 74 65 72 3d 70 4c 61 74 65 72 2d ; pLater=pLater-
14dc8 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 61 73 73 >pNext){. ass
14dc9 65 72 74 28 20 70 4c 61 74 65 72 2d 3e 73 68 61 ert( pLater->sha
14dca 72 61 62 6c 65 20 29 3b 0a 20 20 20 20 61 73 73 rable );. ass
14dcb 65 72 74 28 20 70 4c 61 74 65 72 2d 3e 70 4e 65 ert( pLater->pNe
14dcc 78 74 3d 3d 30 20 7c 7c 20 70 4c 61 74 65 72 2d xt==0 || pLater-
14dcd 3e 70 4e 65 78 74 2d 3e 70 42 74 3e 70 4c 61 74 >pNext->pBt>pLat
14dce 65 72 2d 3e 70 42 74 20 29 3b 0a 20 20 20 20 61 er->pBt );. a
14dcf 73 73 65 72 74 28 20 21 70 4c 61 74 65 72 2d 3e ssert( !pLater->
14dd0 6c 6f 63 6b 65 64 20 7c 7c 20 70 4c 61 74 65 72 locked || pLater
14dd1 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3e 30 20 29 ->wantToLock>0 )
14dd2 3b 0a 20 20 20 20 69 66 28 20 70 4c 61 74 65 72 ;. if( pLater
14dd3 2d 3e 6c 6f 63 6b 65 64 20 29 7b 0a 20 20 20 20 ->locked ){.
14dd4 20 20 75 6e 6c 6f 63 6b 42 74 72 65 65 4d 75 74 unlockBtreeMut
14dd5 65 78 28 70 4c 61 74 65 72 29 3b 0a 20 20 20 20 ex(pLater);.
14dd6 7d 0a 20 20 7d 0a 20 20 6c 6f 63 6b 42 74 72 65 }. }. lockBtre
14dd7 65 4d 75 74 65 78 28 70 29 3b 0a 20 20 66 6f 72 eMutex(p);. for
14dd8 28 70 4c 61 74 65 72 3d 70 2d 3e 70 4e 65 78 74 (pLater=p->pNext
14dd9 3b 20 70 4c 61 74 65 72 3b 20 70 4c 61 74 65 72 ; pLater; pLater
14dda 3d 70 4c 61 74 65 72 2d 3e 70 4e 65 78 74 29 7b =pLater->pNext){
14ddb 0a 20 20 20 20 69 66 28 20 70 4c 61 74 65 72 2d . if( pLater-
14ddc 3e 77 61 6e 74 54 6f 4c 6f 63 6b 20 29 7b 0a 20 >wantToLock ){.
14ddd 20 20 20 20 20 6c 6f 63 6b 42 74 72 65 65 4d 75 lockBtreeMu
14dde 74 65 78 28 70 4c 61 74 65 72 29 3b 0a 20 20 20 tex(pLater);.
14ddf 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 }. }.}../*.**
14de0 45 78 69 74 20 74 68 65 20 72 65 63 75 72 73 69 Exit the recursi
14de1 76 65 20 6d 75 74 65 78 20 6f 6e 20 61 20 42 74 ve mutex on a Bt
14de2 72 65 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ree..*/.SQLITE_P
14de3 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
14de4 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 42 74 te3BtreeLeave(Bt
14de5 72 65 65 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 ree *p){. if( p
14de6 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 20 ->sharable ){.
14de7 20 20 61 73 73 65 72 74 28 20 70 2d 3e 77 61 6e assert( p->wan
14de8 74 54 6f 4c 6f 63 6b 3e 30 20 29 3b 0a 20 20 20 tToLock>0 );.
14de9 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 2d 2d p->wantToLock--
14dea 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 77 61 6e ;. if( p->wan
14deb 74 54 6f 4c 6f 63 6b 3d 3d 30 20 29 7b 0a 20 20 tToLock==0 ){.
14dec 20 20 20 20 75 6e 6c 6f 63 6b 42 74 72 65 65 4d unlockBtreeM
14ded 75 74 65 78 28 70 29 3b 0a 20 20 20 20 7d 0a 20 utex(p);. }.
14dee 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44 }.}..#ifndef ND
14def 45 42 55 47 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 EBUG./*.** Retur
14df0 6e 20 74 72 75 65 20 69 66 20 74 68 65 20 42 74 n true if the Bt
14df1 53 68 61 72 65 64 20 6d 75 74 65 78 20 69 73 20 Shared mutex is
14df2 68 65 6c 64 20 6f 6e 20 74 68 65 20 62 74 72 65 held on the btre
14df3 65 2c 20 6f 72 20 69 66 20 74 68 65 0a 2a 2a 20 e, or if the.**
14df4 42 2d 54 72 65 65 20 69 73 20 6e 6f 74 20 6d 61 B-Tree is not ma
14df5 72 6b 65 64 20 61 73 20 73 68 61 72 61 62 6c 65 rked as sharable
14df6 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
14df7 74 69 6e 65 20 69 73 20 75 73 65 64 20 6f 6e 6c tine is used onl
14df8 79 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 61 73 y from within as
14df9 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 sert() statement
14dfa 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 s..*/.SQLITE_PRI
14dfb 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
14dfc 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 BtreeHoldsMutex(
14dfd 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 61 73 73 Btree *p){. ass
14dfe 65 72 74 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 ert( p->sharable
14dff 3d 3d 30 20 7c 7c 20 70 2d 3e 6c 6f 63 6b 65 64 ==0 || p->locked
14e00 3d 3d 30 20 7c 7c 20 70 2d 3e 77 61 6e 74 54 6f ==0 || p->wantTo
14e01 4c 6f 63 6b 3e 30 20 29 3b 0a 20 20 61 73 73 65 Lock>0 );. asse
14e02 72 74 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 3d rt( p->sharable=
14e03 3d 30 20 7c 7c 20 70 2d 3e 6c 6f 63 6b 65 64 3d =0 || p->locked=
14e04 3d 30 20 7c 7c 20 70 2d 3e 64 62 3d 3d 70 2d 3e =0 || p->db==p->
14e05 70 42 74 2d 3e 64 62 20 29 3b 0a 20 20 61 73 73 pBt->db );. ass
14e06 65 72 74 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 ert( p->sharable
14e07 3d 3d 30 20 7c 7c 20 70 2d 3e 6c 6f 63 6b 65 64 ==0 || p->locked
14e08 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d ==0 || sqlite3_m
14e09 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 70 42 74 utex_held(p->pBt
14e0a 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 ->mutex) );. as
14e0b 73 65 72 74 28 20 70 2d 3e 73 68 61 72 61 62 6c sert( p->sharabl
14e0c 65 3d 3d 30 20 7c 7c 20 70 2d 3e 6c 6f 63 6b 65 e==0 || p->locke
14e0d 64 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f d==0 || sqlite3_
14e0e 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 mutex_held(p->db
14e0f 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 72 ->mutex) );.. r
14e10 65 74 75 72 6e 20 28 70 2d 3e 73 68 61 72 61 62 eturn (p->sharab
14e11 6c 65 3d 3d 30 20 7c 7c 20 70 2d 3e 6c 6f 63 6b le==0 || p->lock
14e12 65 64 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a ed);.}.#endif...
14e13 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
14e14 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 2f 2a 0a MIT_INCRBLOB./*.
14e15 2a 2a 20 45 6e 74 65 72 20 61 6e 64 20 6c 65 61 ** Enter and lea
14e16 76 65 20 61 20 6d 75 74 65 78 20 6f 6e 20 61 20 ve a mutex on a
14e17 42 74 72 65 65 20 67 69 76 65 6e 20 61 20 63 75 Btree given a cu
14e18 72 73 6f 72 20 6f 77 6e 65 64 20 62 79 20 74 68 rsor owned by th
14e19 61 74 0a 2a 2a 20 42 74 72 65 65 2e 20 20 54 68 at.** Btree. Th
14e1a 65 73 65 20 65 6e 74 72 79 20 70 6f 69 6e 74 73 ese entry points
14e1b 20 61 72 65 20 75 73 65 64 20 62 79 20 69 6e 63 are used by inc
14e1c 72 65 6d 65 6e 74 61 6c 20 49 2f 4f 20 61 6e 64 remental I/O and
14e1d 20 63 61 6e 20 62 65 0a 2a 2a 20 6f 6d 69 74 74 can be.** omitt
14e1e 65 64 20 69 66 20 74 68 61 74 20 6d 6f 64 75 6c ed if that modul
14e1f 65 20 69 73 20 6e 6f 74 20 75 73 65 64 2e 0a 2a e is not used..*
14e20 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
14e21 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 void sqlite3Btr
14e22 65 65 45 6e 74 65 72 43 75 72 73 6f 72 28 42 74 eeEnterCursor(Bt
14e23 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 Cursor *pCur){.
14e24 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 sqlite3BtreeEnt
14e25 65 72 28 70 43 75 72 2d 3e 70 42 74 72 65 65 29 er(pCur->pBtree)
14e26 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ;.}.SQLITE_PRIVA
14e27 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 TE void sqlite3B
14e28 74 72 65 65 4c 65 61 76 65 43 75 72 73 6f 72 28 treeLeaveCursor(
14e29 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b BtCursor *pCur){
14e2a 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c . sqlite3BtreeL
14e2b 65 61 76 65 28 70 43 75 72 2d 3e 70 42 74 72 65 eave(pCur->pBtre
14e2c 65 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 e);.}.#endif /*
14e2d 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 SQLITE_OMIT_INCR
14e2e 42 4c 4f 42 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 BLOB */.../*.**
14e2f 45 6e 74 65 72 20 74 68 65 20 6d 75 74 65 78 20 Enter the mutex
14e30 6f 6e 20 65 76 65 72 79 20 42 74 72 65 65 20 61 on every Btree a
14e31 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 ssociated with a
14e32 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e database.** con
14e33 6e 65 63 74 69 6f 6e 2e 20 20 54 68 69 73 20 69 nection. This i
14e34 73 20 6e 65 65 64 65 64 20 28 66 6f 72 20 65 78 s needed (for ex
14e35 61 6d 70 6c 65 29 20 70 72 69 6f 72 20 74 6f 20 ample) prior to
14e36 70 61 72 73 69 6e 67 0a 2a 2a 20 61 20 73 74 61 parsing.** a sta
14e37 74 65 6d 65 6e 74 20 73 69 6e 63 65 20 77 65 20 tement since we
14e38 77 69 6c 6c 20 62 65 20 63 6f 6d 70 61 72 69 6e will be comparin
14e39 67 20 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75 g table and colu
14e3a 6d 6e 20 6e 61 6d 65 73 0a 2a 2a 20 61 67 61 69 mn names.** agai
14e3b 6e 73 74 20 61 6c 6c 20 73 63 68 65 6d 61 73 20 nst all schemas
14e3c 61 6e 64 20 77 65 20 64 6f 20 6e 6f 74 20 77 61 and we do not wa
14e3d 6e 74 20 74 68 6f 73 65 20 73 63 68 65 6d 61 73 nt those schemas
14e3e 20 62 65 69 6e 67 0a 2a 2a 20 72 65 73 65 74 20 being.** reset
14e3f 6f 75 74 20 66 72 6f 6d 20 75 6e 64 65 72 20 75 out from under u
14e40 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 69 s..**.** There i
14e41 73 20 61 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e s a correspondin
14e42 67 20 6c 65 61 76 65 2d 61 6c 6c 20 70 72 6f 63 g leave-all proc
14e43 65 64 75 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 45 6e edures..**.** En
14e44 74 65 72 20 74 68 65 20 6d 75 74 65 78 65 73 20 ter the mutexes
14e45 69 6e 20 61 63 63 65 6e 64 69 6e 67 20 6f 72 64 in accending ord
14e46 65 72 20 62 79 20 42 74 53 68 61 72 65 64 20 70 er by BtShared p
14e47 6f 69 6e 74 65 72 20 61 64 64 72 65 73 73 0a 2a ointer address.*
14e48 2a 20 74 6f 20 61 76 6f 69 64 20 74 68 65 20 70 * to avoid the p
14e49 6f 73 73 69 62 69 6c 69 74 79 20 6f 66 20 64 65 ossibility of de
14e4a 61 64 6c 6f 63 6b 20 77 68 65 6e 20 74 77 6f 20 adlock when two
14e4b 74 68 72 65 61 64 73 20 77 69 74 68 0a 2a 2a 20 threads with.**
14e4c 74 77 6f 20 6f 72 20 6d 6f 72 65 20 62 74 72 65 two or more btre
14e4d 65 73 20 69 6e 20 63 6f 6d 6d 6f 6e 20 62 6f 74 es in common bot
14e4e 68 20 74 72 79 20 74 6f 20 6c 6f 63 6b 20 61 6c h try to lock al
14e4f 6c 20 74 68 65 69 72 20 62 74 72 65 65 73 0a 2a l their btrees.*
14e50 2a 20 61 74 20 74 68 65 20 73 61 6d 65 20 69 6e * at the same in
14e51 73 74 61 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 stant..*/.SQLITE
14e52 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
14e53 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 41 lite3BtreeEnterA
14e54 6c 6c 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b ll(sqlite3 *db){
14e55 0a 20 20 69 6e 74 20 69 3b 0a 20 20 42 74 72 65 . int i;. Btre
14e56 65 20 2a 70 2c 20 2a 70 4c 61 74 65 72 3b 0a 20 e *p, *pLater;.
14e57 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
14e58 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e _mutex_held(db->
14e59 6d 75 74 65 78 29 20 29 3b 0a 20 20 66 6f 72 28 mutex) );. for(
14e5a 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 i=0; i<db->nDb;
14e5b 69 2b 2b 29 7b 0a 20 20 20 20 70 20 3d 20 64 62 i++){. p = db
14e5c 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 ->aDb[i].pBt;.
14e5d 20 20 61 73 73 65 72 74 28 20 21 70 20 7c 7c 20 assert( !p ||
14e5e 28 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 26 26 (p->locked==0 &&
14e5f 20 70 2d 3e 73 68 61 72 61 62 6c 65 29 20 7c 7c p->sharable) ||
14e60 20 70 2d 3e 70 42 74 2d 3e 64 62 3d 3d 70 2d 3e p->pBt->db==p->
14e61 64 62 20 29 3b 0a 20 20 20 20 69 66 28 20 70 20 db );. if( p
14e62 26 26 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 && p->sharable )
14e63 7b 0a 20 20 20 20 20 20 70 2d 3e 77 61 6e 74 54 {. p->wantT
14e64 6f 4c 6f 63 6b 2b 2b 3b 0a 20 20 20 20 20 20 69 oLock++;. i
14e65 66 28 20 21 70 2d 3e 6c 6f 63 6b 65 64 20 29 7b f( !p->locked ){
14e66 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
14e67 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d p->wantToLock==
14e68 31 20 29 3b 0a 20 20 20 20 20 20 20 20 77 68 69 1 );. whi
14e69 6c 65 28 20 70 2d 3e 70 50 72 65 76 20 29 20 70 le( p->pPrev ) p
14e6a 20 3d 20 70 2d 3e 70 50 72 65 76 3b 0a 20 20 20 = p->pPrev;.
14e6b 20 20 20 20 20 2f 2a 20 52 65 61 73 6f 6e 20 66 /* Reason f
14e6c 6f 72 20 41 4c 57 41 59 53 3a 20 20 54 68 65 72 or ALWAYS: Ther
14e6d 65 20 6d 75 73 74 20 62 65 20 61 74 20 6c 65 61 e must be at lea
14e6e 73 74 20 6f 6e 20 75 6e 6c 6f 63 6b 65 64 20 42 st on unlocked B
14e6f 74 72 65 65 20 69 6e 0a 20 20 20 20 20 20 20 20 tree in.
14e70 2a 2a 20 74 68 65 20 63 68 61 69 6e 2e 20 20 4f ** the chain. O
14e71 74 68 65 72 77 69 73 65 20 74 68 65 20 21 70 2d therwise the !p-
14e72 3e 6c 6f 63 6b 65 64 20 74 65 73 74 20 61 62 6f >locked test abo
14e73 76 65 20 77 6f 75 6c 64 20 68 61 76 65 20 66 61 ve would have fa
14e74 69 6c 65 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 iled */.
14e75 77 68 69 6c 65 28 20 70 2d 3e 6c 6f 63 6b 65 64 while( p->locked
14e76 20 26 26 20 41 4c 57 41 59 53 28 70 2d 3e 70 4e && ALWAYS(p->pN
14e77 65 78 74 29 20 29 20 70 20 3d 20 70 2d 3e 70 4e ext) ) p = p->pN
14e78 65 78 74 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 ext;. for
14e79 28 70 4c 61 74 65 72 20 3d 20 70 2d 3e 70 4e 65 (pLater = p->pNe
14e7a 78 74 3b 20 70 4c 61 74 65 72 3b 20 70 4c 61 74 xt; pLater; pLat
14e7b 65 72 3d 70 4c 61 74 65 72 2d 3e 70 4e 65 78 74 er=pLater->pNext
14e7c 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 ){. if(
14e7d 20 70 4c 61 74 65 72 2d 3e 6c 6f 63 6b 65 64 20 pLater->locked
14e7e 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 75 ){. u
14e7f 6e 6c 6f 63 6b 42 74 72 65 65 4d 75 74 65 78 28 nlockBtreeMutex(
14e80 70 4c 61 74 65 72 29 3b 0a 20 20 20 20 20 20 20 pLater);.
14e81 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 }. }.
14e82 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 20 while( p
14e83 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6c 6f 63 ){. loc
14e84 6b 42 74 72 65 65 4d 75 74 65 78 28 70 29 3b 0a kBtreeMutex(p);.
14e85 20 20 20 20 20 20 20 20 20 20 70 20 3d 20 70 2d p = p-
14e86 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 20 >pNext;.
14e87 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a }. }. }.
14e88 20 20 7d 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 }.}.SQLITE_PRI
14e89 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
14e8a 33 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 73 3BtreeLeaveAll(s
14e8b 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 qlite3 *db){. i
14e8c 6e 74 20 69 3b 0a 20 20 42 74 72 65 65 20 2a 70 nt i;. Btree *p
14e8d 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
14e8e 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 te3_mutex_held(d
14e8f 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 66 b->mutex) );. f
14e90 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 or(i=0; i<db->nD
14e91 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 20 3d b; i++){. p =
14e92 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b db->aDb[i].pBt;
14e93 0a 20 20 20 20 69 66 28 20 70 20 26 26 20 70 2d . if( p && p-
14e94 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 >sharable ){.
14e95 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 77 61 assert( p->wa
14e96 6e 74 54 6f 4c 6f 63 6b 3e 30 20 29 3b 0a 20 20 ntToLock>0 );.
14e97 20 20 20 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 p->wantToLoc
14e98 6b 2d 2d 3b 0a 20 20 20 20 20 20 69 66 28 20 70 k--;. if( p
14e99 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 ->wantToLock==0
14e9a 29 7b 0a 20 20 20 20 20 20 20 20 75 6e 6c 6f 63 ){. unloc
14e9b 6b 42 74 72 65 65 4d 75 74 65 78 28 70 29 3b 0a kBtreeMutex(p);.
14e9c 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
14e9d 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 }.}..#ifndef NDE
14e9e 42 55 47 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e BUG./*.** Return
14e9f 20 74 72 75 65 20 69 66 20 74 68 65 20 63 75 72 true if the cur
14ea0 72 65 6e 74 20 74 68 72 65 61 64 20 68 6f 6c 64 rent thread hold
14ea1 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 s the database c
14ea2 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 6d 75 74 onnection.** mut
14ea3 65 78 20 61 6e 64 20 61 6c 6c 20 72 65 71 75 69 ex and all requi
14ea4 72 65 64 20 42 74 53 68 61 72 65 64 20 6d 75 74 red BtShared mut
14ea5 65 78 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 exes..**.** This
14ea6 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 routine is used
14ea7 20 69 6e 73 69 64 65 20 61 73 73 65 72 74 28 29 inside assert()
14ea8 20 73 74 61 74 65 6d 65 6e 74 73 20 6f 6e 6c 79 statements only
14ea9 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
14eaa 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
14eab 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 treeHoldsAllMute
14eac 78 65 73 28 73 71 6c 69 74 65 33 20 2a 64 62 29 xes(sqlite3 *db)
14ead 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 {. int i;. if(
14eae 20 21 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f !sqlite3_mutex_
14eaf 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 held(db->mutex)
14eb0 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b ){. return 0;
14eb1 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 . }. for(i=0;
14eb2 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b i<db->nDb; i++){
14eb3 0a 20 20 20 20 42 74 72 65 65 20 2a 70 3b 0a 20 . Btree *p;.
14eb4 20 20 20 70 20 3d 20 64 62 2d 3e 61 44 62 5b 69 p = db->aDb[i
14eb5 5d 2e 70 42 74 3b 0a 20 20 20 20 69 66 28 20 70 ].pBt;. if( p
14eb6 20 26 26 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 && p->sharable
14eb7 26 26 0a 20 20 20 20 20 20 20 20 20 28 70 2d 3e &&. (p->
14eb8 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 7c 7c wantToLock==0 ||
14eb9 20 21 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f !sqlite3_mutex_
14eba 68 65 6c 64 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 held(p->pBt->mut
14ebb 65 78 29 29 20 29 7b 0a 20 20 20 20 20 20 72 65 ex)) ){. re
14ebc 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 turn 0;. }.
14ebd 7d 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a }. return 1;.}.
14ebe 23 65 6e 64 69 66 20 2f 2a 20 4e 44 45 42 55 47 #endif /* NDEBUG
14ebf 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 */../*.** Add a
14ec0 20 6e 65 77 20 42 74 72 65 65 20 70 6f 69 6e 74 new Btree point
14ec1 65 72 20 74 6f 20 61 20 42 74 72 65 65 4d 75 74 er to a BtreeMut
14ec2 65 78 41 72 72 61 79 2e 20 0a 2a 2a 20 69 66 20 exArray. .** if
14ec3 74 68 65 20 70 6f 69 6e 74 65 72 20 63 61 6e 20 the pointer can
14ec4 70 6f 73 73 69 62 6c 79 20 62 65 20 73 68 61 72 possibly be shar
14ec5 65 64 20 77 69 74 68 0a 2a 2a 20 61 6e 6f 74 68 ed with.** anoth
14ec6 65 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e er database conn
14ec7 65 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 ection..**.** Th
14ec8 65 20 70 6f 69 6e 74 65 72 73 20 61 72 65 20 6b e pointers are k
14ec9 65 70 74 20 69 6e 20 73 6f 72 74 65 64 20 6f 72 ept in sorted or
14eca 64 65 72 20 62 79 20 70 42 74 72 65 65 2d 3e 70 der by pBtree->p
14ecb 42 74 2e 20 20 54 68 61 74 0a 2a 2a 20 77 61 79 Bt. That.** way
14ecc 20 77 68 65 6e 20 77 65 20 67 6f 20 74 6f 20 65 when we go to e
14ecd 6e 74 65 72 20 61 6c 6c 20 74 68 65 20 6d 75 74 nter all the mut
14ece 65 78 65 73 2c 20 77 65 20 63 61 6e 20 65 6e 74 exes, we can ent
14ecf 65 72 20 74 68 65 6d 0a 2a 2a 20 69 6e 20 6f 72 er them.** in or
14ed0 64 65 72 20 77 69 74 68 6f 75 74 20 65 76 65 72 der without ever
14ed1 79 20 68 61 76 69 6e 67 20 74 6f 20 62 61 63 6b y having to back
14ed2 75 70 20 61 6e 64 20 72 65 74 72 79 20 61 6e 64 up and retry and
14ed3 20 77 69 74 68 6f 75 74 0a 2a 2a 20 77 6f 72 72 without.** worr
14ed4 79 69 6e 67 20 61 62 6f 75 74 20 64 65 61 64 6c ying about deadl
14ed5 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e ock..**.** The n
14ed6 75 6d 62 65 72 20 6f 66 20 73 68 61 72 65 64 20 umber of shared
14ed7 62 74 72 65 65 73 20 77 69 6c 6c 20 61 6c 77 61 btrees will alwa
14ed8 79 73 20 62 65 20 73 6d 61 6c 6c 20 28 75 73 75 ys be small (usu
14ed9 61 6c 6c 79 20 30 20 6f 72 20 31 29 0a 2a 2a 20 ally 0 or 1).**
14eda 73 6f 20 61 6e 20 69 6e 73 65 72 74 69 6f 6e 20 so an insertion
14edb 73 6f 72 74 20 69 73 20 61 6e 20 61 64 65 71 75 sort is an adequ
14edc 61 74 65 20 61 6c 67 6f 72 69 74 68 6d 20 68 65 ate algorithm he
14edd 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 re..*/.SQLITE_PR
14ede 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
14edf 65 33 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 e3BtreeMutexArra
14ee0 79 49 6e 73 65 72 74 28 42 74 72 65 65 4d 75 74 yInsert(BtreeMut
14ee1 65 78 41 72 72 61 79 20 2a 70 41 72 72 61 79 2c exArray *pArray,
14ee2 20 42 74 72 65 65 20 2a 70 42 74 72 65 65 29 7b Btree *pBtree){
14ee3 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 42 . int i, j;. B
14ee4 74 53 68 61 72 65 64 20 2a 70 42 74 3b 0a 20 20 tShared *pBt;.
14ee5 69 66 28 20 70 42 74 72 65 65 3d 3d 30 20 7c 7c if( pBtree==0 ||
14ee6 20 70 42 74 72 65 65 2d 3e 73 68 61 72 61 62 6c pBtree->sharabl
14ee7 65 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 23 e==0 ) return;.#
14ee8 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 ifndef NDEBUG.
14ee9 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 {. for(i=0; i
14eea 3c 70 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78 3b <pArray->nMutex;
14eeb 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 61 73 73 i++){. ass
14eec 65 72 74 28 20 70 41 72 72 61 79 2d 3e 61 42 74 ert( pArray->aBt
14eed 72 65 65 5b 69 5d 21 3d 70 42 74 72 65 65 20 29 ree[i]!=pBtree )
14eee 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 ;. }. }.#end
14eef 69 66 0a 20 20 61 73 73 65 72 74 28 20 70 41 72 if. assert( pAr
14ef0 72 61 79 2d 3e 6e 4d 75 74 65 78 3e 3d 30 20 29 ray->nMutex>=0 )
14ef1 3b 0a 20 20 61 73 73 65 72 74 28 20 70 41 72 72 ;. assert( pArr
14ef2 61 79 2d 3e 6e 4d 75 74 65 78 3c 41 72 72 61 79 ay->nMutex<Array
14ef3 53 69 7a 65 28 70 41 72 72 61 79 2d 3e 61 42 74 Size(pArray->aBt
14ef4 72 65 65 29 2d 31 20 29 3b 0a 20 20 70 42 74 20 ree)-1 );. pBt
14ef5 3d 20 70 42 74 72 65 65 2d 3e 70 42 74 3b 0a 20 = pBtree->pBt;.
14ef6 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 41 72 72 for(i=0; i<pArr
14ef7 61 79 2d 3e 6e 4d 75 74 65 78 3b 20 69 2b 2b 29 ay->nMutex; i++)
14ef8 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 41 {. assert( pA
14ef9 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 69 5d 21 rray->aBtree[i]!
14efa 3d 70 42 74 72 65 65 20 29 3b 0a 20 20 20 20 69 =pBtree );. i
14efb 66 28 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65 f( pArray->aBtre
14efc 65 5b 69 5d 2d 3e 70 42 74 3e 70 42 74 20 29 7b e[i]->pBt>pBt ){
14efd 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 70 41 72 . for(j=pAr
14efe 72 61 79 2d 3e 6e 4d 75 74 65 78 3b 20 6a 3e 69 ray->nMutex; j>i
14eff 3b 20 6a 2d 2d 29 7b 0a 20 20 20 20 20 20 20 20 ; j--){.
14f00 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 6a pArray->aBtree[j
14f01 5d 20 3d 20 70 41 72 72 61 79 2d 3e 61 42 74 72 ] = pArray->aBtr
14f02 65 65 5b 6a 2d 31 5d 3b 0a 20 20 20 20 20 20 7d ee[j-1];. }
14f03 0a 20 20 20 20 20 20 70 41 72 72 61 79 2d 3e 61 . pArray->a
14f04 42 74 72 65 65 5b 69 5d 20 3d 20 70 42 74 72 65 Btree[i] = pBtre
14f05 65 3b 0a 20 20 20 20 20 20 70 41 72 72 61 79 2d e;. pArray-
14f06 3e 6e 4d 75 74 65 78 2b 2b 3b 0a 20 20 20 20 20 >nMutex++;.
14f07 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 return;. }.
14f08 20 7d 0a 20 20 70 41 72 72 61 79 2d 3e 61 42 74 }. pArray->aBt
14f09 72 65 65 5b 70 41 72 72 61 79 2d 3e 6e 4d 75 74 ree[pArray->nMut
14f0a 65 78 2b 2b 5d 20 3d 20 70 42 74 72 65 65 3b 0a ex++] = pBtree;.
14f0b 7d 0a 0a 2f 2a 0a 2a 2a 20 45 6e 74 65 72 20 74 }../*.** Enter t
14f0c 68 65 20 6d 75 74 65 78 20 6f 66 20 65 76 65 72 he mutex of ever
14f0d 79 20 62 74 72 65 65 20 69 6e 20 74 68 65 20 61 y btree in the a
14f0e 72 72 61 79 2e 20 20 54 68 69 73 20 72 6f 75 74 rray. This rout
14f0f 69 6e 65 20 69 73 0a 2a 2a 20 63 61 6c 6c 65 64 ine is.** called
14f10 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e at the beginnin
14f11 67 20 6f 66 20 73 71 6c 69 74 65 33 56 64 62 65 g of sqlite3Vdbe
14f12 45 78 65 63 28 29 2e 20 20 54 68 65 20 6d 75 74 Exec(). The mut
14f13 65 78 65 73 20 61 72 65 0a 2a 2a 20 65 78 69 74 exes are.** exit
14f14 65 64 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 ed at the end of
14f15 20 74 68 65 20 73 61 6d 65 20 66 75 6e 63 74 69 the same functi
14f16 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 on..*/.SQLITE_PR
14f17 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
14f18 65 33 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 e3BtreeMutexArra
14f19 79 45 6e 74 65 72 28 42 74 72 65 65 4d 75 74 65 yEnter(BtreeMute
14f1a 78 41 72 72 61 79 20 2a 70 41 72 72 61 79 29 7b xArray *pArray){
14f1b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 . int i;. for(
14f1c 69 3d 30 3b 20 69 3c 70 41 72 72 61 79 2d 3e 6e i=0; i<pArray->n
14f1d 4d 75 74 65 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 Mutex; i++){.
14f1e 20 42 74 72 65 65 20 2a 70 20 3d 20 70 41 72 72 Btree *p = pArr
14f1f 61 79 2d 3e 61 42 74 72 65 65 5b 69 5d 3b 0a 20 ay->aBtree[i];.
14f20 20 20 20 2f 2a 20 53 6f 6d 65 20 62 61 73 69 63 /* Some basic
14f21 20 73 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 sanity checking
14f22 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 */. assert(
14f23 69 3d 3d 30 20 7c 7c 20 70 41 72 72 61 79 2d 3e i==0 || pArray->
14f24 61 42 74 72 65 65 5b 69 2d 31 5d 2d 3e 70 42 74 aBtree[i-1]->pBt
14f25 3c 70 2d 3e 70 42 74 20 29 3b 0a 20 20 20 20 61 <p->pBt );. a
14f26 73 73 65 72 74 28 20 21 70 2d 3e 6c 6f 63 6b 65 ssert( !p->locke
14f27 64 20 7c 7c 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f d || p->wantToLo
14f28 63 6b 3e 30 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 ck>0 );.. /*
14f29 57 65 20 73 68 6f 75 6c 64 20 61 6c 72 65 61 64 We should alread
14f2a 79 20 68 6f 6c 64 20 61 20 6c 6f 63 6b 20 6f 6e y hold a lock on
14f2b 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f the database co
14f2c 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 nnection */.
14f2d 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
14f2e 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 mutex_held(p->db
14f2f 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 20 ->mutex) );..
14f30 20 2f 2a 20 54 68 65 20 42 74 72 65 65 20 69 73 /* The Btree is
14f31 20 73 68 61 72 61 62 6c 65 20 62 65 63 61 75 73 sharable becaus
14f32 65 20 6f 6e 6c 79 20 73 68 61 72 61 62 6c 65 20 e only sharable
14f33 42 74 72 65 65 73 20 61 72 65 20 65 6e 74 65 72 Btrees are enter
14f34 65 64 0a 20 20 20 20 2a 2a 20 69 6e 74 6f 20 74 ed. ** into t
14f35 68 65 20 61 72 72 61 79 20 69 6e 20 74 68 65 20 he array in the
14f36 66 69 72 73 74 20 70 6c 61 63 65 2e 20 2a 2f 0a first place. */.
14f37 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 73 assert( p->s
14f38 68 61 72 61 62 6c 65 20 29 3b 0a 0a 20 20 20 20 harable );..
14f39 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 2b 2b 3b p->wantToLock++;
14f3a 0a 20 20 20 20 69 66 28 20 21 70 2d 3e 6c 6f 63 . if( !p->loc
14f3b 6b 65 64 20 29 7b 0a 20 20 20 20 20 20 6c 6f 63 ked ){. loc
14f3c 6b 42 74 72 65 65 4d 75 74 65 78 28 70 29 3b 0a kBtreeMutex(p);.
14f3d 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a }. }.}../*.
14f3e 2a 2a 20 4c 65 61 76 65 20 74 68 65 20 6d 75 74 ** Leave the mut
14f3f 65 78 20 6f 66 20 65 76 65 72 79 20 62 74 72 65 ex of every btre
14f40 65 20 69 6e 20 74 68 65 20 67 72 6f 75 70 2e 0a e in the group..
14f41 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
14f42 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 E void sqlite3Bt
14f43 72 65 65 4d 75 74 65 78 41 72 72 61 79 4c 65 61 reeMutexArrayLea
14f44 76 65 28 42 74 72 65 65 4d 75 74 65 78 41 72 72 ve(BtreeMutexArr
14f45 61 79 20 2a 70 41 72 72 61 79 29 7b 0a 20 20 69 ay *pArray){. i
14f46 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b nt i;. for(i=0;
14f47 20 69 3c 70 41 72 72 61 79 2d 3e 6e 4d 75 74 65 i<pArray->nMute
14f48 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 42 74 72 x; i++){. Btr
14f49 65 65 20 2a 70 20 3d 20 70 41 72 72 61 79 2d 3e ee *p = pArray->
14f4a 61 42 74 72 65 65 5b 69 5d 3b 0a 20 20 20 20 2f aBtree[i];. /
14f4b 2a 20 53 6f 6d 65 20 62 61 73 69 63 20 73 61 6e * Some basic san
14f4c 69 74 79 20 63 68 65 63 6b 69 6e 67 20 2a 2f 0a ity checking */.
14f4d 20 20 20 20 61 73 73 65 72 74 28 20 69 3d 3d 30 assert( i==0
14f4e 20 7c 7c 20 70 41 72 72 61 79 2d 3e 61 42 74 72 || pArray->aBtr
14f4f 65 65 5b 69 2d 31 5d 2d 3e 70 42 74 3c 70 2d 3e ee[i-1]->pBt<p->
14f50 70 42 74 20 29 3b 0a 20 20 20 20 61 73 73 65 72 pBt );. asser
14f51 74 28 20 70 2d 3e 6c 6f 63 6b 65 64 20 29 3b 0a t( p->locked );.
14f52 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 77 assert( p->w
14f53 61 6e 74 54 6f 4c 6f 63 6b 3e 30 20 29 3b 0a 0a antToLock>0 );..
14f54 20 20 20 20 2f 2a 20 57 65 20 73 68 6f 75 6c 64 /* We should
14f55 20 61 6c 72 65 61 64 79 20 68 6f 6c 64 20 61 20 already hold a
14f56 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 lock on the data
14f57 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
14f58 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 */. assert( s
14f59 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
14f5a 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 d(p->db->mutex)
14f5b 29 3b 0a 0a 20 20 20 20 70 2d 3e 77 61 6e 74 54 );.. p->wantT
14f5c 6f 4c 6f 63 6b 2d 2d 3b 0a 20 20 20 20 69 66 28 oLock--;. if(
14f5d 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d p->wantToLock==
14f5e 30 20 29 7b 0a 20 20 20 20 20 20 75 6e 6c 6f 63 0 ){. unloc
14f5f 6b 42 74 72 65 65 4d 75 74 65 78 28 70 29 3b 0a kBtreeMutex(p);.
14f60 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 23 65 6c }. }.}..#el
14f61 73 65 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 se.SQLITE_PRIVAT
14f62 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 E void sqlite3Bt
14f63 72 65 65 45 6e 74 65 72 28 42 74 72 65 65 20 2a reeEnter(Btree *
14f64 70 29 7b 0a 20 20 70 2d 3e 70 42 74 2d 3e 64 62 p){. p->pBt->db
14f65 20 3d 20 70 2d 3e 64 62 3b 0a 7d 0a 53 51 4c 49 = p->db;.}.SQLI
14f66 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
14f67 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 sqlite3BtreeEnte
14f68 72 41 6c 6c 28 73 71 6c 69 74 65 33 20 2a 64 62 rAll(sqlite3 *db
14f69 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f ){. int i;. fo
14f6a 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 r(i=0; i<db->nDb
14f6b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 42 74 72 65 ; i++){. Btre
14f6c 65 20 2a 70 20 3d 20 64 62 2d 3e 61 44 62 5b 69 e *p = db->aDb[i
14f6d 5d 2e 70 42 74 3b 0a 20 20 20 20 69 66 28 20 70 ].pBt;. if( p
14f6e 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 70 42 74 ){. p->pBt
14f6f 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 ->db = p->db;.
14f70 20 20 7d 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 }. }.}.#endif
14f71 20 2f 2a 20 69 66 20 53 51 4c 49 54 45 5f 54 48 /* if SQLITE_TH
14f72 52 45 41 44 53 41 46 45 20 2a 2f 0a 23 65 6e 64 READSAFE */.#end
14f73 69 66 20 2f 2a 20 69 66 6e 64 65 66 20 53 51 4c if /* ifndef SQL
14f74 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f ITE_OMIT_SHARED_
14f75 43 41 43 48 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a CACHE */../*****
14f76 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 ********* End of
14f77 20 62 74 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a btmutex.c *****
14f78 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14f79 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14f7a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
14f7b 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 ********* Begin
14f7c 66 69 6c 65 20 62 74 72 65 65 2e 63 20 2a 2a 2a file btree.c ***
14f7d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14f7e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14f7f 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 ********/./*.**
14f80 32 30 30 34 20 41 70 72 69 6c 20 36 0a 2a 2a 0a 2004 April 6.**.
14f81 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 ** The author di
14f82 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 sclaims copyrigh
14f83 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 t to this source
14f84 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 code. In place
14f85 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e of.** a legal n
14f86 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 otice, here is a
14f87 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a blessing:.**.**
14f88 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 May you do g
14f89 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c ood and not evil
14f8a 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
14f8b 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 find forgiveness
14f8c 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e for yourself an
14f8d 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 d forgive others
14f8e 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
14f8f 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 share freely, ne
14f90 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 ver taking more
14f91 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a than you give..*
14f92 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *.**************
14f93 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14f94 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14f95 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14f96 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 24 ***********.** $
14f97 49 64 3a 20 62 74 72 65 65 2e 63 2c 76 20 31 2e Id: btree.c,v 1.
14f98 37 30 35 20 32 30 30 39 2f 30 38 2f 31 30 20 30 705 2009/08/10 0
14f99 33 3a 35 37 3a 35 38 20 73 68 61 6e 65 20 45 78 3:57:58 shane Ex
14f9a 70 20 24 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 p $.**.** This f
14f9b 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 ile implements a
14f9c 20 65 78 74 65 72 6e 61 6c 20 28 64 69 73 6b 2d external (disk-
14f9d 62 61 73 65 64 29 20 64 61 74 61 62 61 73 65 20 based) database
14f9e 75 73 69 6e 67 20 42 54 72 65 65 73 2e 0a 2a 2a using BTrees..**
14f9f 20 53 65 65 20 74 68 65 20 68 65 61 64 65 72 20 See the header
14fa0 63 6f 6d 6d 65 6e 74 20 6f 6e 20 22 62 74 72 65 comment on "btre
14fa1 65 49 6e 74 2e 68 22 20 66 6f 72 20 61 64 64 69 eInt.h" for addi
14fa2 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 tional informati
14fa3 6f 6e 2e 0a 2a 2a 20 49 6e 63 6c 75 64 69 6e 67 on..** Including
14fa4 20 61 20 64 65 73 63 72 69 70 74 69 6f 6e 20 6f a description o
14fa5 66 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 61 6e f file format an
14fa6 64 20 61 6e 20 6f 76 65 72 76 69 65 77 20 6f 66 d an overview of
14fa7 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a 2f 0a 0a operation..*/..
14fa8 2f 2a 0a 2a 2a 20 54 68 65 20 68 65 61 64 65 72 /*.** The header
14fa9 20 73 74 72 69 6e 67 20 74 68 61 74 20 61 70 70 string that app
14faa 65 61 72 73 20 61 74 20 74 68 65 20 62 65 67 69 ears at the begi
14fab 6e 6e 69 6e 67 20 6f 66 20 65 76 65 72 79 0a 2a nning of every.*
14fac 2a 20 53 51 4c 69 74 65 20 64 61 74 61 62 61 73 * SQLite databas
14fad 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e e..*/.static con
14fae 73 74 20 63 68 61 72 20 7a 4d 61 67 69 63 48 65 st char zMagicHe
14faf 61 64 65 72 5b 5d 20 3d 20 53 51 4c 49 54 45 5f ader[] = SQLITE_
14fb0 46 49 4c 45 5f 48 45 41 44 45 52 3b 0a 0a 2f 2a FILE_HEADER;../*
14fb1 0a 2a 2a 20 53 65 74 20 74 68 69 73 20 67 6c 6f .** Set this glo
14fb2 62 61 6c 20 76 61 72 69 61 62 6c 65 20 74 6f 20 bal variable to
14fb3 31 20 74 6f 20 65 6e 61 62 6c 65 20 74 72 61 63 1 to enable trac
14fb4 69 6e 67 20 75 73 69 6e 67 20 74 68 65 20 54 52 ing using the TR
14fb5 41 43 45 0a 2a 2a 20 6d 61 63 72 6f 2e 0a 2a 2f ACE.** macro..*/
14fb6 0a 23 69 66 20 30 0a 69 6e 74 20 73 71 6c 69 74 .#if 0.int sqlit
14fb7 65 33 42 74 72 65 65 54 72 61 63 65 3d 31 3b 20 e3BtreeTrace=1;
14fb8 20 2f 2a 20 54 72 75 65 20 74 6f 20 65 6e 61 62 /* True to enab
14fb9 6c 65 20 74 72 61 63 69 6e 67 20 2a 2f 0a 23 20 le tracing */.#
14fba 64 65 66 69 6e 65 20 54 52 41 43 45 28 58 29 20 define TRACE(X)
14fbb 20 69 66 28 73 71 6c 69 74 65 33 42 74 72 65 65 if(sqlite3Btree
14fbc 54 72 61 63 65 29 7b 70 72 69 6e 74 66 20 58 3b Trace){printf X;
14fbd 66 66 6c 75 73 68 28 73 74 64 6f 75 74 29 3b 7d fflush(stdout);}
14fbe 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 .#else.# define
14fbf 54 52 41 43 45 28 58 29 0a 23 65 6e 64 69 66 0a TRACE(X).#endif.
14fc0 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 ...#ifndef SQLIT
14fc1 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 E_OMIT_SHARED_CA
14fc2 43 48 45 0a 2f 2a 0a 2a 2a 20 41 20 6c 69 73 74 CHE./*.** A list
14fc3 20 6f 66 20 42 74 53 68 61 72 65 64 20 6f 62 6a of BtShared obj
14fc4 65 63 74 73 20 74 68 61 74 20 61 72 65 20 65 6c ects that are el
14fc5 69 67 69 62 6c 65 20 66 6f 72 20 70 61 72 74 69 igible for parti
14fc6 63 69 70 61 74 69 6f 6e 0a 2a 2a 20 69 6e 20 73 cipation.** in s
14fc7 68 61 72 65 64 20 63 61 63 68 65 2e 20 20 54 68 hared cache. Th
14fc8 69 73 20 76 61 72 69 61 62 6c 65 20 68 61 73 20 is variable has
14fc9 66 69 6c 65 20 73 63 6f 70 65 20 64 75 72 69 6e file scope durin
14fca 67 20 6e 6f 72 6d 61 6c 20 62 75 69 6c 64 73 2c g normal builds,
14fcb 0a 2a 2a 20 62 75 74 20 74 68 65 20 74 65 73 74 .** but the test
14fcc 20 68 61 72 6e 65 73 73 20 6e 65 65 64 73 20 74 harness needs t
14fcd 6f 20 61 63 63 65 73 73 20 69 74 20 73 6f 20 77 o access it so w
14fce 65 20 6d 61 6b 65 20 69 74 20 67 6c 6f 62 61 6c e make it global
14fcf 20 66 6f 72 20 0a 2a 2a 20 74 65 73 74 20 62 75 for .** test bu
14fd0 69 6c 64 73 2e 0a 2a 2a 0a 2a 2a 20 41 63 63 65 ilds..**.** Acce
14fd1 73 73 20 74 6f 20 74 68 69 73 20 76 61 72 69 61 ss to this varia
14fd2 62 6c 65 20 69 73 20 70 72 6f 74 65 63 74 65 64 ble is protected
14fd3 20 62 79 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 by SQLITE_MUTEX
14fd4 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 2e 0a _STATIC_MASTER..
14fd5 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
14fd6 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 52 49 _TEST.SQLITE_PRI
14fd7 56 41 54 45 20 42 74 53 68 61 72 65 64 20 2a 53 VATE BtShared *S
14fd8 51 4c 49 54 45 5f 57 53 44 20 73 71 6c 69 74 65 QLITE_WSD sqlite
14fd9 33 53 68 61 72 65 64 43 61 63 68 65 4c 69 73 74 3SharedCacheList
14fda 20 3d 20 30 3b 0a 23 65 6c 73 65 0a 73 74 61 74 = 0;.#else.stat
14fdb 69 63 20 42 74 53 68 61 72 65 64 20 2a 53 51 4c ic BtShared *SQL
14fdc 49 54 45 5f 57 53 44 20 73 71 6c 69 74 65 33 53 ITE_WSD sqlite3S
14fdd 68 61 72 65 64 43 61 63 68 65 4c 69 73 74 20 3d haredCacheList =
14fde 20 30 3b 0a 23 65 6e 64 69 66 0a 23 65 6e 64 69 0;.#endif.#endi
14fdf 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 f /* SQLITE_OMIT
14fe0 5f 53 48 41 52 45 44 5f 43 41 43 48 45 20 2a 2f _SHARED_CACHE */
14fe1 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
14fe2 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 _OMIT_SHARED_CAC
14fe3 48 45 0a 2f 2a 0a 2a 2a 20 45 6e 61 62 6c 65 20 HE./*.** Enable
14fe4 6f 72 20 64 69 73 61 62 6c 65 20 74 68 65 20 73 or disable the s
14fe5 68 61 72 65 64 20 70 61 67 65 72 20 61 6e 64 20 hared pager and
14fe6 73 63 68 65 6d 61 20 66 65 61 74 75 72 65 73 2e schema features.
14fe7 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
14fe8 69 6e 65 20 68 61 73 20 6e 6f 20 65 66 66 65 63 ine has no effec
14fe9 74 20 6f 6e 20 65 78 69 73 74 69 6e 67 20 64 61 t on existing da
14fea 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
14feb 6e 73 2e 0a 2a 2a 20 54 68 65 20 73 68 61 72 65 ns..** The share
14fec 64 20 63 61 63 68 65 20 73 65 74 74 69 6e 67 20 d cache setting
14fed 65 66 66 65 63 74 73 20 6f 6e 6c 79 20 66 75 74 effects only fut
14fee 75 72 65 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20 ure calls to.**
14fef 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 2c 20 sqlite3_open(),
14ff0 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 sqlite3_open16()
14ff1 2c 20 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 65 , or sqlite3_ope
14ff2 6e 5f 76 32 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 n_v2()..*/.SQLIT
14ff3 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
14ff4 33 5f 65 6e 61 62 6c 65 5f 73 68 61 72 65 64 5f 3_enable_shared_
14ff5 63 61 63 68 65 28 69 6e 74 20 65 6e 61 62 6c 65 cache(int enable
14ff6 29 7b 0a 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 ){. sqlite3Glob
14ff7 61 6c 43 6f 6e 66 69 67 2e 73 68 61 72 65 64 43 alConfig.sharedC
14ff8 61 63 68 65 45 6e 61 62 6c 65 64 20 3d 20 65 6e acheEnabled = en
14ff9 61 62 6c 65 3b 0a 20 20 72 65 74 75 72 6e 20 53 able;. return S
14ffa 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 QLITE_OK;.}.#end
14ffb 69 66 0a 0a 0a 0a 23 69 66 64 65 66 20 53 51 4c if....#ifdef SQL
14ffc 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f ITE_OMIT_SHARED_
14ffd 43 41 43 48 45 0a 20 20 2f 2a 0a 20 20 2a 2a 20 CACHE. /*. **
14ffe 54 68 65 20 66 75 6e 63 74 69 6f 6e 73 20 71 75 The functions qu
14fff 65 72 79 53 68 61 72 65 64 43 61 63 68 65 54 61 erySharedCacheTa
15000 62 6c 65 4c 6f 63 6b 28 29 2c 20 73 65 74 53 68 bleLock(), setSh
15001 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f aredCacheTableLo
15002 63 6b 28 29 2c 0a 20 20 2a 2a 20 61 6e 64 20 63 ck(),. ** and c
15003 6c 65 61 72 41 6c 6c 53 68 61 72 65 64 43 61 63 learAllSharedCac
15004 68 65 54 61 62 6c 65 4c 6f 63 6b 73 28 29 0a 20 heTableLocks().
15005 20 2a 2a 20 6d 61 6e 69 70 75 6c 61 74 65 20 65 ** manipulate e
15006 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20 42 74 ntries in the Bt
15007 53 68 61 72 65 64 2e 70 4c 6f 63 6b 20 6c 69 6e Shared.pLock lin
15008 6b 65 64 20 6c 69 73 74 20 75 73 65 64 20 74 6f ked list used to
15009 20 73 74 6f 72 65 0a 20 20 2a 2a 20 73 68 61 72 store. ** shar
1500a 65 64 2d 63 61 63 68 65 20 74 61 62 6c 65 20 6c ed-cache table l
1500b 65 76 65 6c 20 6c 6f 63 6b 73 2e 20 49 66 20 74 evel locks. If t
1500c 68 65 20 6c 69 62 72 61 72 79 20 69 73 20 63 6f he library is co
1500d 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68 65 0a mpiled with the.
1500e 20 20 2a 2a 20 73 68 61 72 65 64 2d 63 61 63 68 ** shared-cach
1500f 65 20 66 65 61 74 75 72 65 20 64 69 73 61 62 6c e feature disabl
15010 65 64 2c 20 74 68 65 6e 20 74 68 65 72 65 20 69 ed, then there i
15011 73 20 6f 6e 6c 79 20 65 76 65 72 20 6f 6e 65 20 s only ever one
15012 75 73 65 72 0a 20 20 2a 2a 20 6f 66 20 65 61 63 user. ** of eac
15013 68 20 42 74 53 68 61 72 65 64 20 73 74 72 75 63 h BtShared struc
15014 74 75 72 65 20 61 6e 64 20 73 6f 20 74 68 69 73 ture and so this
15015 20 6c 6f 63 6b 69 6e 67 20 69 73 20 6e 6f 74 20 locking is not
15016 6e 65 63 65 73 73 61 72 79 2e 20 0a 20 20 2a 2a necessary. . **
15017 20 53 6f 20 64 65 66 69 6e 65 20 74 68 65 20 6c So define the l
15018 6f 63 6b 20 72 65 6c 61 74 65 64 20 66 75 6e 63 ock related func
15019 74 69 6f 6e 73 20 61 73 20 6e 6f 2d 6f 70 73 2e tions as no-ops.
1501a 0a 20 20 2a 2f 0a 20 20 23 64 65 66 69 6e 65 20 . */. #define
1501b 71 75 65 72 79 53 68 61 72 65 64 43 61 63 68 65 querySharedCache
1501c 54 61 62 6c 65 4c 6f 63 6b 28 61 2c 62 2c 63 29 TableLock(a,b,c)
1501d 20 53 51 4c 49 54 45 5f 4f 4b 0a 20 20 23 64 65 SQLITE_OK. #de
1501e 66 69 6e 65 20 73 65 74 53 68 61 72 65 64 43 61 fine setSharedCa
1501f 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 61 2c 62 cheTableLock(a,b
15020 2c 63 29 20 53 51 4c 49 54 45 5f 4f 4b 0a 20 20 ,c) SQLITE_OK.
15021 23 64 65 66 69 6e 65 20 63 6c 65 61 72 41 6c 6c #define clearAll
15022 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 SharedCacheTable
15023 4c 6f 63 6b 73 28 61 29 0a 20 20 23 64 65 66 69 Locks(a). #defi
15024 6e 65 20 64 6f 77 6e 67 72 61 64 65 41 6c 6c 53 ne downgradeAllS
15025 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c haredCacheTableL
15026 6f 63 6b 73 28 61 29 0a 20 20 23 64 65 66 69 6e ocks(a). #defin
15027 65 20 68 61 73 53 68 61 72 65 64 43 61 63 68 65 e hasSharedCache
15028 54 61 62 6c 65 4c 6f 63 6b 28 61 2c 62 2c 63 2c TableLock(a,b,c,
15029 64 29 20 31 0a 20 20 23 64 65 66 69 6e 65 20 68 d) 1. #define h
1502a 61 73 52 65 61 64 43 6f 6e 66 6c 69 63 74 73 28 asReadConflicts(
1502b 61 2c 20 62 29 20 30 0a 23 65 6e 64 69 66 0a 0a a, b) 0.#endif..
1502c 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1502d 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 MIT_SHARED_CACHE
1502e 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ..#ifdef SQLITE_
1502f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68 69 73 DEBUG./*.** This
15030 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6f 6e 6c function is onl
15031 79 20 75 73 65 64 20 61 73 20 70 61 72 74 20 6f y used as part o
15032 66 20 61 6e 20 61 73 73 65 72 74 28 29 20 73 74 f an assert() st
15033 61 74 65 6d 65 6e 74 2e 20 49 74 20 63 68 65 63 atement. It chec
15034 6b 73 0a 2a 2a 20 74 68 61 74 20 63 6f 6e 6e 65 ks.** that conne
15035 63 74 69 6f 6e 20 70 20 68 6f 6c 64 73 20 74 68 ction p holds th
15036 65 20 72 65 71 75 69 72 65 64 20 6c 6f 63 6b 73 e required locks
15037 20 74 6f 20 72 65 61 64 20 6f 72 20 77 72 69 74 to read or writ
15038 65 20 74 6f 20 74 68 65 20 0a 2a 2a 20 62 2d 74 e to the .** b-t
15039 72 65 65 20 77 69 74 68 20 72 6f 6f 74 20 70 61 ree with root pa
1503a 67 65 20 69 52 6f 6f 74 2e 20 49 66 20 73 6f 2c ge iRoot. If so,
1503b 20 74 72 75 65 20 69 73 20 72 65 74 75 72 6e 65 true is returne
1503c 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 66 61 d. Otherwise, fa
1503d 6c 73 65 2e 20 0a 2a 2a 20 46 6f 72 20 65 78 61 lse. .** For exa
1503e 6d 70 6c 65 2c 20 77 68 65 6e 20 77 72 69 74 69 mple, when writi
1503f 6e 67 20 74 6f 20 61 20 74 61 62 6c 65 20 62 2d ng to a table b-
15040 74 72 65 65 20 77 69 74 68 20 72 6f 6f 74 2d 70 tree with root-p
15041 61 67 65 20 69 52 6f 6f 74 20 76 69 61 20 0a 2a age iRoot via .*
15042 2a 20 42 74 72 65 65 20 63 6f 6e 6e 65 63 74 69 * Btree connecti
15043 6f 6e 20 70 42 74 72 65 65 3a 0a 2a 2a 0a 2a 2a on pBtree:.**.**
15044 20 20 20 20 61 73 73 65 72 74 28 20 68 61 73 53 assert( hasS
15045 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c haredCacheTableL
15046 6f 63 6b 28 70 42 74 72 65 65 2c 20 69 52 6f 6f ock(pBtree, iRoo
15047 74 2c 20 30 2c 20 57 52 49 54 45 5f 4c 4f 43 4b t, 0, WRITE_LOCK
15048 29 20 29 3b 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 ) );.**.** When
15049 77 72 69 74 69 6e 67 20 74 6f 20 61 6e 20 69 6e writing to an in
1504a 64 65 78 20 62 2d 74 72 65 65 20 74 68 61 74 20 dex b-tree that
1504b 72 65 73 69 64 65 73 20 69 6e 20 61 20 73 68 61 resides in a sha
1504c 72 61 62 6c 65 20 64 61 74 61 62 61 73 65 2c 20 rable database,
1504d 74 68 65 20 0a 2a 2a 20 63 61 6c 6c 65 72 20 73 the .** caller s
1504e 68 6f 75 6c 64 20 68 61 76 65 20 66 69 72 73 74 hould have first
1504f 20 6f 62 74 61 69 6e 65 64 20 61 20 6c 6f 63 6b obtained a lock
15050 20 73 70 65 63 69 66 79 69 6e 67 20 74 68 65 20 specifying the
15051 72 6f 6f 74 20 70 61 67 65 20 6f 66 0a 2a 2a 20 root page of.**
15052 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e the correspondin
15053 67 20 74 61 62 6c 65 20 62 2d 74 72 65 65 2e 20 g table b-tree.
15054 54 68 69 73 20 6d 61 6b 65 73 20 74 68 69 6e 67 This makes thing
15055 73 20 61 20 62 69 74 20 6d 6f 72 65 20 63 6f 6d s a bit more com
15056 70 6c 69 63 61 74 65 64 2c 0a 2a 2a 20 61 73 20 plicated,.** as
15057 74 68 69 73 20 6d 6f 64 75 6c 65 20 74 72 65 61 this module trea
15058 74 73 20 65 61 63 68 20 62 2d 74 72 65 65 20 61 ts each b-tree a
15059 73 20 61 20 73 65 70 61 72 61 74 65 20 73 74 72 s a separate str
1505a 75 63 74 75 72 65 2e 20 54 6f 20 64 65 74 65 72 ucture. To deter
1505b 6d 69 6e 65 0a 2a 2a 20 74 68 65 20 74 61 62 6c mine.** the tabl
1505c 65 20 62 2d 74 72 65 65 20 63 6f 72 72 65 73 70 e b-tree corresp
1505d 6f 6e 64 69 6e 67 20 74 6f 20 74 68 65 20 69 6e onding to the in
1505e 64 65 78 20 62 2d 74 72 65 65 20 62 65 69 6e 67 dex b-tree being
1505f 20 77 72 69 74 74 65 6e 2c 20 74 68 69 73 0a 2a written, this.*
15060 2a 20 66 75 6e 63 74 69 6f 6e 20 68 61 73 20 74 * function has t
15061 6f 20 73 65 61 72 63 68 20 74 68 72 6f 75 67 68 o search through
15062 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63 the database sc
15063 68 65 6d 61 2e 0a 2a 2a 0a 2a 2a 20 49 6e 73 74 hema..**.** Inst
15064 65 61 64 20 6f 66 20 61 20 6c 6f 63 6b 20 6f 6e ead of a lock on
15065 20 74 68 65 20 62 2d 74 72 65 65 20 72 6f 6f 74 the b-tree root
15066 65 64 20 61 74 20 70 61 67 65 20 69 52 6f 6f 74 ed at page iRoot
15067 2c 20 74 68 65 20 63 61 6c 6c 65 72 20 6d 61 79 , the caller may
15068 0a 2a 2a 20 68 6f 6c 64 20 61 20 77 72 69 74 65 .** hold a write
15069 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 73 63 68 -lock on the sch
1506a 65 6d 61 20 74 61 62 6c 65 20 28 72 6f 6f 74 20 ema table (root
1506b 70 61 67 65 20 31 29 2e 20 54 68 69 73 20 69 73 page 1). This is
1506c 20 61 6c 73 6f 0a 2a 2a 20 61 63 63 65 70 74 61 also.** accepta
1506d 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ble..*/.static i
1506e 6e 74 20 68 61 73 53 68 61 72 65 64 43 61 63 68 nt hasSharedCach
1506f 65 54 61 62 6c 65 4c 6f 63 6b 28 0a 20 20 42 74 eTableLock(. Bt
15070 72 65 65 20 2a 70 42 74 72 65 65 2c 20 20 20 20 ree *pBtree,
15071 20 20 20 20 20 2f 2a 20 48 61 6e 64 6c 65 20 74 /* Handle t
15072 68 61 74 20 6d 75 73 74 20 68 6f 6c 64 20 6c 6f hat must hold lo
15073 63 6b 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 52 6f ck */. Pgno iRo
15074 6f 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f ot, /
15075 2a 20 52 6f 6f 74 20 70 61 67 65 20 6f 66 20 62 * Root page of b
15076 2d 74 72 65 65 20 2a 2f 0a 20 20 69 6e 74 20 69 -tree */. int i
15077 73 49 6e 64 65 78 2c 20 20 20 20 20 20 20 20 20 sIndex,
15078 20 20 2f 2a 20 54 72 75 65 20 69 66 20 69 52 6f /* True if iRo
15079 6f 74 20 69 73 20 74 68 65 20 72 6f 6f 74 20 6f ot is the root o
1507a 66 20 61 6e 20 69 6e 64 65 78 20 62 2d 74 72 65 f an index b-tre
1507b 65 20 2a 2f 0a 20 20 69 6e 74 20 65 4c 6f 63 6b e */. int eLock
1507c 54 79 70 65 20 20 20 20 20 20 20 20 20 20 2f 2a Type /*
1507d 20 52 65 71 75 69 72 65 64 20 6c 6f 63 6b 20 74 Required lock t
1507e 79 70 65 20 28 52 45 41 44 5f 4c 4f 43 4b 20 6f ype (READ_LOCK o
1507f 72 20 57 52 49 54 45 5f 4c 4f 43 4b 29 20 2a 2f r WRITE_LOCK) */
15080 0a 29 7b 0a 20 20 53 63 68 65 6d 61 20 2a 70 53 .){. Schema *pS
15081 63 68 65 6d 61 20 3d 20 28 53 63 68 65 6d 61 20 chema = (Schema
15082 2a 29 70 42 74 72 65 65 2d 3e 70 42 74 2d 3e 70 *)pBtree->pBt->p
15083 53 63 68 65 6d 61 3b 0a 20 20 50 67 6e 6f 20 69 Schema;. Pgno i
15084 54 61 62 20 3d 20 30 3b 0a 20 20 42 74 4c 6f 63 Tab = 0;. BtLoc
15085 6b 20 2a 70 4c 6f 63 6b 3b 0a 0a 20 20 2f 2a 20 k *pLock;.. /*
15086 49 66 20 74 68 69 73 20 62 2d 74 72 65 65 20 64 If this b-tree d
15087 61 74 61 62 61 73 65 20 69 73 20 6e 6f 74 20 73 atabase is not s
15088 68 61 72 65 61 62 6c 65 2c 20 6f 72 20 69 66 20 hareable, or if
15089 74 68 65 20 63 6c 69 65 6e 74 20 69 73 20 72 65 the client is re
1508a 61 64 69 6e 67 0a 20 20 2a 2a 20 61 6e 64 20 68 ading. ** and h
1508b 61 73 20 74 68 65 20 72 65 61 64 2d 75 6e 63 6f as the read-unco
1508c 6d 6d 69 74 74 65 64 20 66 6c 61 67 20 73 65 74 mmitted flag set
1508d 2c 20 74 68 65 6e 20 6e 6f 20 6c 6f 63 6b 20 69 , then no lock i
1508e 73 20 72 65 71 75 69 72 65 64 2e 20 0a 20 20 2a s required. . *
1508f 2a 20 49 6e 20 74 68 65 73 65 20 63 61 73 65 73 * In these cases
15090 20 72 65 74 75 72 6e 20 74 72 75 65 20 69 6d 6d return true imm
15091 65 64 69 61 74 65 6c 79 2e 20 20 49 66 20 74 68 ediately. If th
15092 65 20 63 6c 69 65 6e 74 20 69 73 20 72 65 61 64 e client is read
15093 69 6e 67 20 0a 20 20 2a 2a 20 6f 72 20 77 72 69 ing . ** or wri
15094 74 69 6e 67 20 61 6e 20 69 6e 64 65 78 20 62 2d ting an index b-
15095 74 72 65 65 2c 20 62 75 74 20 74 68 65 20 73 63 tree, but the sc
15096 68 65 6d 61 20 69 73 20 6e 6f 74 20 6c 6f 61 64 hema is not load
15097 65 64 2c 20 74 68 65 6e 20 72 65 74 75 72 6e 0a ed, then return.
15098 20 20 2a 2a 20 74 72 75 65 20 61 6c 73 6f 2e 20 ** true also.
15099 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65 In this case the
1509a 20 6c 6f 63 6b 20 69 73 20 72 65 71 75 69 72 65 lock is require
1509b 64 2c 20 62 75 74 20 69 74 20 69 73 20 74 6f 6f d, but it is too
1509c 20 64 69 66 66 69 63 75 6c 74 0a 20 20 2a 2a 20 difficult. **
1509d 74 6f 20 63 68 65 63 6b 20 69 66 20 74 68 65 20 to check if the
1509e 63 6c 69 65 6e 74 20 61 63 74 75 61 6c 6c 79 20 client actually
1509f 68 6f 6c 64 73 20 69 74 2e 20 54 68 69 73 20 64 holds it. This d
150a0 6f 65 73 6e 27 74 20 68 61 70 70 65 6e 20 76 65 oesn't happen ve
150a1 72 79 0a 20 20 2a 2a 20 6f 66 74 65 6e 2e 20 20 ry. ** often.
150a2 2a 2f 0a 20 20 69 66 28 20 28 70 42 74 72 65 65 */. if( (pBtree
150a3 2d 3e 73 68 61 72 61 62 6c 65 3d 3d 30 29 0a 20 ->sharable==0).
150a4 20 20 7c 7c 20 28 65 4c 6f 63 6b 54 79 70 65 3d || (eLockType=
150a5 3d 52 45 41 44 5f 4c 4f 43 4b 20 26 26 20 28 70 =READ_LOCK && (p
150a6 42 74 72 65 65 2d 3e 64 62 2d 3e 66 6c 61 67 73 Btree->db->flags
150a7 20 26 20 53 51 4c 49 54 45 5f 52 65 61 64 55 6e & SQLITE_ReadUn
150a8 63 6f 6d 6d 69 74 74 65 64 29 29 0a 20 20 20 7c committed)). |
150a9 7c 20 28 69 73 49 6e 64 65 78 20 26 26 20 28 21 | (isIndex && (!
150aa 70 53 63 68 65 6d 61 20 7c 7c 20 28 70 53 63 68 pSchema || (pSch
150ab 65 6d 61 2d 3e 66 6c 61 67 73 26 44 42 5f 53 63 ema->flags&DB_Sc
150ac 68 65 6d 61 4c 6f 61 64 65 64 29 3d 3d 30 20 29 hemaLoaded)==0 )
150ad 29 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ). ){. retur
150ae 6e 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 n 1;. }.. /* F
150af 69 67 75 72 65 20 6f 75 74 20 74 68 65 20 72 6f igure out the ro
150b0 6f 74 2d 70 61 67 65 20 74 68 61 74 20 74 68 65 ot-page that the
150b1 20 6c 6f 63 6b 20 73 68 6f 75 6c 64 20 62 65 20 lock should be
150b2 68 65 6c 64 20 6f 6e 2e 20 46 6f 72 20 74 61 62 held on. For tab
150b3 6c 65 0a 20 20 2a 2a 20 62 2d 74 72 65 65 73 2c le. ** b-trees,
150b4 20 74 68 69 73 20 69 73 20 6a 75 73 74 20 74 68 this is just th
150b5 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 e root page of t
150b6 68 65 20 62 2d 74 72 65 65 20 62 65 69 6e 67 20 he b-tree being
150b7 72 65 61 64 20 6f 72 0a 20 20 2a 2a 20 77 72 69 read or. ** wri
150b8 74 74 65 6e 2e 20 46 6f 72 20 69 6e 64 65 78 20 tten. For index
150b9 62 2d 74 72 65 65 73 2c 20 69 74 20 69 73 20 74 b-trees, it is t
150ba 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 he root page of
150bb 74 68 65 20 61 73 73 6f 63 69 61 74 65 64 0a 20 the associated.
150bc 20 2a 2a 20 74 61 62 6c 65 2e 20 20 2a 2f 0a 20 ** table. */.
150bd 20 69 66 28 20 69 73 49 6e 64 65 78 20 29 7b 0a if( isIndex ){.
150be 20 20 20 20 48 61 73 68 45 6c 65 6d 20 2a 70 3b HashElem *p;
150bf 0a 20 20 20 20 66 6f 72 28 70 3d 73 71 6c 69 74 . for(p=sqlit
150c0 65 48 61 73 68 46 69 72 73 74 28 26 70 53 63 68 eHashFirst(&pSch
150c1 65 6d 61 2d 3e 69 64 78 48 61 73 68 29 3b 20 70 ema->idxHash); p
150c2 3b 20 70 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 ; p=sqliteHashNe
150c3 78 74 28 70 29 29 7b 0a 20 20 20 20 20 20 49 6e xt(p)){. In
150c4 64 65 78 20 2a 70 49 64 78 20 3d 20 28 49 6e 64 dex *pIdx = (Ind
150c5 65 78 20 2a 29 73 71 6c 69 74 65 48 61 73 68 44 ex *)sqliteHashD
150c6 61 74 61 28 70 29 3b 0a 20 20 20 20 20 20 69 66 ata(p);. if
150c7 28 20 70 49 64 78 2d 3e 74 6e 75 6d 3d 3d 28 69 ( pIdx->tnum==(i
150c8 6e 74 29 69 52 6f 6f 74 20 29 7b 0a 20 20 20 20 nt)iRoot ){.
150c9 20 20 20 20 69 54 61 62 20 3d 20 70 49 64 78 2d iTab = pIdx-
150ca 3e 70 54 61 62 6c 65 2d 3e 74 6e 75 6d 3b 0a 20 >pTable->tnum;.
150cb 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d }. }. }
150cc 65 6c 73 65 7b 0a 20 20 20 20 69 54 61 62 20 3d else{. iTab =
150cd 20 69 52 6f 6f 74 3b 0a 20 20 7d 0a 0a 20 20 2f iRoot;. }.. /
150ce 2a 20 53 65 61 72 63 68 20 66 6f 72 20 74 68 65 * Search for the
150cf 20 72 65 71 75 69 72 65 64 20 6c 6f 63 6b 2e 20 required lock.
150d0 45 69 74 68 65 72 20 61 20 77 72 69 74 65 2d 6c Either a write-l
150d1 6f 63 6b 20 6f 6e 20 72 6f 6f 74 2d 70 61 67 65 ock on root-page
150d2 20 69 54 61 62 2c 20 61 20 0a 20 20 2a 2a 20 77 iTab, a . ** w
150d3 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 rite-lock on the
150d4 20 73 63 68 65 6d 61 20 74 61 62 6c 65 2c 20 6f schema table, o
150d5 72 20 28 69 66 20 74 68 65 20 63 6c 69 65 6e 74 r (if the client
150d6 20 69 73 20 72 65 61 64 69 6e 67 29 20 61 0a 20 is reading) a.
150d7 20 2a 2a 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e ** read-lock on
150d8 20 69 54 61 62 20 77 69 6c 6c 20 73 75 66 66 69 iTab will suffi
150d9 63 65 2e 20 52 65 74 75 72 6e 20 31 20 69 66 20 ce. Return 1 if
150da 61 6e 79 20 6f 66 20 74 68 65 73 65 20 61 72 65 any of these are
150db 20 66 6f 75 6e 64 2e 20 20 2a 2f 0a 20 20 66 6f found. */. fo
150dc 72 28 70 4c 6f 63 6b 3d 70 42 74 72 65 65 2d 3e r(pLock=pBtree->
150dd 70 42 74 2d 3e 70 4c 6f 63 6b 3b 20 70 4c 6f 63 pBt->pLock; pLoc
150de 6b 3b 20 70 4c 6f 63 6b 3d 70 4c 6f 63 6b 2d 3e k; pLock=pLock->
150df 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20 pNext){. if(
150e0 70 4c 6f 63 6b 2d 3e 70 42 74 72 65 65 3d 3d 70 pLock->pBtree==p
150e1 42 74 72 65 65 20 0a 20 20 20 20 20 26 26 20 28 Btree . && (
150e2 70 4c 6f 63 6b 2d 3e 69 54 61 62 6c 65 3d 3d 69 pLock->iTable==i
150e3 54 61 62 20 7c 7c 20 28 70 4c 6f 63 6b 2d 3e 65 Tab || (pLock->e
150e4 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b Lock==WRITE_LOCK
150e5 20 26 26 20 70 4c 6f 63 6b 2d 3e 69 54 61 62 6c && pLock->iTabl
150e6 65 3d 3d 31 29 29 0a 20 20 20 20 20 26 26 20 70 e==1)). && p
150e7 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 3e 3d 65 4c 6f Lock->eLock>=eLo
150e8 63 6b 54 79 70 65 20 0a 20 20 20 20 29 7b 0a 20 ckType . ){.
150e9 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 return 1;.
150ea 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 }. }.. /* F
150eb 61 69 6c 65 64 20 74 6f 20 66 69 6e 64 20 74 68 ailed to find th
150ec 65 20 72 65 71 75 69 72 65 64 20 6c 6f 63 6b 2e e required lock.
150ed 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a */. return 0;.
150ee 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 }../*.** This fu
150ef 6e 63 74 69 6f 6e 20 69 73 20 61 6c 73 6f 20 75 nction is also u
150f0 73 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 61 sed as part of a
150f1 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e ssert() statemen
150f2 74 73 20 6f 6e 6c 79 2e 20 49 74 20 0a 2a 2a 20 ts only. It .**
150f3 72 65 74 75 72 6e 73 20 74 72 75 65 20 69 66 20 returns true if
150f4 74 68 65 72 65 20 65 78 69 73 74 20 6f 6e 65 20 there exist one
150f5 6f 72 20 6d 6f 72 65 20 63 75 72 73 6f 72 73 20 or more cursors
150f6 6f 70 65 6e 20 6f 6e 20 74 68 65 20 74 61 62 6c open on the tabl
150f7 65 20 0a 2a 2a 20 77 69 74 68 20 72 6f 6f 74 20 e .** with root
150f8 70 61 67 65 20 69 52 6f 6f 74 20 74 68 61 74 20 page iRoot that
150f9 64 6f 20 6e 6f 74 20 62 65 6c 6f 6e 67 20 74 6f do not belong to
150fa 20 65 69 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 either connecti
150fb 6f 6e 20 70 42 74 72 65 65 20 0a 2a 2a 20 6f 72 on pBtree .** or
150fc 20 73 6f 6d 65 20 6f 74 68 65 72 20 63 6f 6e 6e some other conn
150fd 65 63 74 69 6f 6e 20 74 68 61 74 20 68 61 73 20 ection that has
150fe 74 68 65 20 72 65 61 64 2d 75 6e 63 6f 6d 6d 69 the read-uncommi
150ff 74 74 65 64 20 66 6c 61 67 20 73 65 74 2e 0a 2a tted flag set..*
15100 2a 0a 2a 2a 20 46 6f 72 20 65 78 61 6d 70 6c 65 *.** For example
15101 2c 20 62 65 66 6f 72 65 20 77 72 69 74 69 6e 67 , before writing
15102 20 74 6f 20 70 61 67 65 20 69 52 6f 6f 74 3a 0a to page iRoot:.
15103 2a 2a 0a 2a 2a 20 20 20 20 61 73 73 65 72 74 28 **.** assert(
15104 20 21 68 61 73 52 65 61 64 43 6f 6e 66 6c 69 63 !hasReadConflic
15105 74 73 28 70 42 74 72 65 65 2c 20 69 52 6f 6f 74 ts(pBtree, iRoot
15106 29 20 29 3b 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ) );.*/.static i
15107 6e 74 20 68 61 73 52 65 61 64 43 6f 6e 66 6c 69 nt hasReadConfli
15108 63 74 73 28 42 74 72 65 65 20 2a 70 42 74 72 65 cts(Btree *pBtre
15109 65 2c 20 50 67 6e 6f 20 69 52 6f 6f 74 29 7b 0a e, Pgno iRoot){.
1510a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 3b 0a 20 BtCursor *p;.
1510b 20 66 6f 72 28 70 3d 70 42 74 72 65 65 2d 3e 70 for(p=pBtree->p
1510c 42 74 2d 3e 70 43 75 72 73 6f 72 3b 20 70 3b 20 Bt->pCursor; p;
1510d 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 p=p->pNext){.
1510e 20 69 66 28 20 70 2d 3e 70 67 6e 6f 52 6f 6f 74 if( p->pgnoRoot
1510f 3d 3d 69 52 6f 6f 74 20 0a 20 20 20 20 20 26 26 ==iRoot . &&
15110 20 70 2d 3e 70 42 74 72 65 65 21 3d 70 42 74 72 p->pBtree!=pBtr
15111 65 65 0a 20 20 20 20 20 26 26 20 30 3d 3d 28 70 ee. && 0==(p
15112 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e 66 6c ->pBtree->db->fl
15113 61 67 73 20 26 20 53 51 4c 49 54 45 5f 52 65 61 ags & SQLITE_Rea
15114 64 55 6e 63 6f 6d 6d 69 74 74 65 64 29 0a 20 20 dUncommitted).
15115 20 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 ){. retur
15116 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 n 1;. }. }.
15117 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e return 0;.}.#en
15118 64 69 66 20 20 20 20 2f 2a 20 23 69 66 64 65 66 dif /* #ifdef
15119 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 2a 2f SQLITE_DEBUG */
1511a 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72 79 20 74 6f ../*.** Query to
1511b 20 73 65 65 20 69 66 20 62 74 72 65 65 20 68 61 see if btree ha
1511c 6e 64 6c 65 20 70 20 6d 61 79 20 6f 62 74 61 69 ndle p may obtai
1511d 6e 20 61 20 6c 6f 63 6b 20 6f 66 20 74 79 70 65 n a lock of type
1511e 20 65 4c 6f 63 6b 20 0a 2a 2a 20 28 52 45 41 44 eLock .** (READ
1511f 5f 4c 4f 43 4b 20 6f 72 20 57 52 49 54 45 5f 4c _LOCK or WRITE_L
15120 4f 43 4b 29 20 6f 6e 20 74 68 65 20 74 61 62 6c OCK) on the tabl
15121 65 20 77 69 74 68 20 72 6f 6f 74 2d 70 61 67 65 e with root-page
15122 20 69 54 61 62 2e 20 52 65 74 75 72 6e 0a 2a 2a iTab. Return.**
15123 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 74 68 SQLITE_OK if th
15124 65 20 6c 6f 63 6b 20 6d 61 79 20 62 65 20 6f 62 e lock may be ob
15125 74 61 69 6e 65 64 20 28 62 79 20 63 61 6c 6c 69 tained (by calli
15126 6e 67 0a 2a 2a 20 73 65 74 53 68 61 72 65 64 43 ng.** setSharedC
15127 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 29 29 acheTableLock())
15128 2c 20 6f 72 20 53 51 4c 49 54 45 5f 4c 4f 43 4b , or SQLITE_LOCK
15129 45 44 20 69 66 20 6e 6f 74 2e 0a 2a 2f 0a 73 74 ED if not..*/.st
1512a 61 74 69 63 20 69 6e 74 20 71 75 65 72 79 53 68 atic int querySh
1512b 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f aredCacheTableLo
1512c 63 6b 28 42 74 72 65 65 20 2a 70 2c 20 50 67 6e ck(Btree *p, Pgn
1512d 6f 20 69 54 61 62 2c 20 75 38 20 65 4c 6f 63 6b o iTab, u8 eLock
1512e 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 ){. BtShared *p
1512f 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 42 Bt = p->pBt;. B
15130 74 4c 6f 63 6b 20 2a 70 49 74 65 72 3b 0a 0a 20 tLock *pIter;..
15131 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
15132 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 BtreeHoldsMutex(
15133 70 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 p) );. assert(
15134 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b eLock==READ_LOCK
15135 20 7c 7c 20 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 || eLock==WRITE
15136 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 _LOCK );. asser
15137 74 28 20 70 2d 3e 64 62 21 3d 30 20 29 3b 0a 20 t( p->db!=0 );.
15138 20 61 73 73 65 72 74 28 20 21 28 70 2d 3e 64 62 assert( !(p->db
15139 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 52 ->flags&SQLITE_R
1513a 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 29 7c eadUncommitted)|
1513b 7c 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f |eLock==WRITE_LO
1513c 43 4b 7c 7c 69 54 61 62 3d 3d 31 20 29 3b 0a 20 CK||iTab==1 );.
1513d 20 0a 20 20 2f 2a 20 49 66 20 72 65 71 75 65 73 . /* If reques
1513e 74 69 6e 67 20 61 20 77 72 69 74 65 2d 6c 6f 63 ting a write-loc
1513f 6b 2c 20 74 68 65 6e 20 74 68 65 20 42 74 72 65 k, then the Btre
15140 65 20 6d 75 73 74 20 68 61 76 65 20 61 6e 20 6f e must have an o
15141 70 65 6e 20 77 72 69 74 65 0a 20 20 2a 2a 20 74 pen write. ** t
15142 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 74 68 ransaction on th
15143 69 73 20 66 69 6c 65 2e 20 41 6e 64 2c 20 6f 62 is file. And, ob
15144 76 69 6f 75 73 6c 79 2c 20 66 6f 72 20 74 68 69 viously, for thi
15145 73 20 74 6f 20 62 65 20 73 6f 20 74 68 65 72 65 s to be so there
15146 20 0a 20 20 2a 2a 20 6d 75 73 74 20 62 65 20 61 . ** must be a
15147 6e 20 6f 70 65 6e 20 77 72 69 74 65 20 74 72 61 n open write tra
15148 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 74 68 65 20 nsaction on the
15149 66 69 6c 65 20 69 74 73 65 6c 66 2e 0a 20 20 2a file itself.. *
1514a 2f 0a 20 20 61 73 73 65 72 74 28 20 65 4c 6f 63 /. assert( eLoc
1514b 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 k==READ_LOCK ||
1514c 28 70 3d 3d 70 42 74 2d 3e 70 57 72 69 74 65 72 (p==pBt->pWriter
1514d 20 26 26 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d && p->inTrans==
1514e 54 52 41 4e 53 5f 57 52 49 54 45 29 20 29 3b 0a TRANS_WRITE) );.
1514f 20 20 61 73 73 65 72 74 28 20 65 4c 6f 63 6b 3d assert( eLock=
15150 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 70 42 =READ_LOCK || pB
15151 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e t->inTransaction
15152 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b ==TRANS_WRITE );
15153 0a 20 20 0a 20 20 2f 2a 20 54 68 69 73 20 69 73 . . /* This is
15154 20 61 20 6e 6f 2d 6f 70 20 69 66 20 74 68 65 20 a no-op if the
15155 73 68 61 72 65 64 2d 63 61 63 68 65 20 69 73 20 shared-cache is
15156 6e 6f 74 20 65 6e 61 62 6c 65 64 20 2a 2f 0a 20 not enabled */.
15157 20 69 66 28 20 21 70 2d 3e 73 68 61 72 61 62 6c if( !p->sharabl
15158 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 e ){. return
15159 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a SQLITE_OK;. }..
1515a 20 20 2f 2a 20 49 66 20 73 6f 6d 65 20 6f 74 68 /* If some oth
1515b 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 er connection is
1515c 20 68 6f 6c 64 69 6e 67 20 61 6e 20 65 78 63 6c holding an excl
1515d 75 73 69 76 65 20 6c 6f 63 6b 2c 20 74 68 65 0a usive lock, the.
1515e 20 20 2a 2a 20 72 65 71 75 65 73 74 65 64 20 6c ** requested l
1515f 6f 63 6b 20 6d 61 79 20 6e 6f 74 20 62 65 20 6f ock may not be o
15160 62 74 61 69 6e 65 64 2e 0a 20 20 2a 2f 0a 20 20 btained.. */.
15161 69 66 28 20 70 42 74 2d 3e 70 57 72 69 74 65 72 if( pBt->pWriter
15162 21 3d 70 20 26 26 20 70 42 74 2d 3e 69 73 45 78 !=p && pBt->isEx
15163 63 6c 75 73 69 76 65 20 29 7b 0a 20 20 20 20 73 clusive ){. s
15164 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e qlite3Connection
15165 42 6c 6f 63 6b 65 64 28 70 2d 3e 64 62 2c 20 70 Blocked(p->db, p
15166 42 74 2d 3e 70 57 72 69 74 65 72 2d 3e 64 62 29 Bt->pWriter->db)
15167 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c ;. return SQL
15168 49 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 ITE_LOCKED_SHARE
15169 44 43 41 43 48 45 3b 0a 20 20 7d 0a 0a 20 20 66 DCACHE;. }.. f
1516a 6f 72 28 70 49 74 65 72 3d 70 42 74 2d 3e 70 4c or(pIter=pBt->pL
1516b 6f 63 6b 3b 20 70 49 74 65 72 3b 20 70 49 74 65 ock; pIter; pIte
1516c 72 3d 70 49 74 65 72 2d 3e 70 4e 65 78 74 29 7b r=pIter->pNext){
1516d 0a 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6e 64 . /* The cond
1516e 69 74 69 6f 6e 20 28 70 49 74 65 72 2d 3e 65 4c ition (pIter->eL
1516f 6f 63 6b 21 3d 65 4c 6f 63 6b 29 20 69 6e 20 74 ock!=eLock) in t
15170 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 66 28 he following if(
15171 2e 2e 2e 29 20 0a 20 20 20 20 2a 2a 20 73 74 61 ...) . ** sta
15172 74 65 6d 65 6e 74 20 69 73 20 61 20 73 69 6d 70 tement is a simp
15173 6c 69 66 69 63 61 74 69 6f 6e 20 6f 66 3a 0a 20 lification of:.
15174 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 28 **. ** (
15175 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 eLock==WRITE_LOC
15176 4b 20 7c 7c 20 70 49 74 65 72 2d 3e 65 4c 6f 63 K || pIter->eLoc
15177 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 29 0a 20 k==WRITE_LOCK).
15178 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 73 69 6e **. ** sin
15179 63 65 20 77 65 20 6b 6e 6f 77 20 74 68 61 74 20 ce we know that
1517a 69 66 20 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f if eLock==WRITE_
1517b 4c 4f 43 4b 2c 20 74 68 65 6e 20 6e 6f 20 6f 74 LOCK, then no ot
1517c 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 20 her connection.
1517d 20 20 20 2a 2a 20 6d 61 79 20 68 6f 6c 64 20 61 ** may hold a
1517e 20 57 52 49 54 45 5f 4c 4f 43 4b 20 6f 6e 20 61 WRITE_LOCK on a
1517f 6e 79 20 74 61 62 6c 65 20 69 6e 20 74 68 69 73 ny table in this
15180 20 66 69 6c 65 20 28 73 69 6e 63 65 20 74 68 65 file (since the
15181 72 65 20 63 61 6e 0a 20 20 20 20 2a 2a 20 6f 6e re can. ** on
15182 6c 79 20 62 65 20 61 20 73 69 6e 67 6c 65 20 77 ly be a single w
15183 72 69 74 65 72 29 2e 0a 20 20 20 20 2a 2f 0a 20 riter).. */.
15184 20 20 20 61 73 73 65 72 74 28 20 70 49 74 65 72 assert( pIter
15185 2d 3e 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f ->eLock==READ_LO
15186 43 4b 20 7c 7c 20 70 49 74 65 72 2d 3e 65 4c 6f CK || pIter->eLo
15187 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 29 ck==WRITE_LOCK )
15188 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 65 4c ;. assert( eL
15189 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c ock==READ_LOCK |
1518a 7c 20 70 49 74 65 72 2d 3e 70 42 74 72 65 65 3d | pIter->pBtree=
1518b 3d 70 20 7c 7c 20 70 49 74 65 72 2d 3e 65 4c 6f =p || pIter->eLo
1518c 63 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b 29 3b 0a ck==READ_LOCK);.
1518d 20 20 20 20 69 66 28 20 70 49 74 65 72 2d 3e 70 if( pIter->p
1518e 42 74 72 65 65 21 3d 70 20 26 26 20 70 49 74 65 Btree!=p && pIte
1518f 72 2d 3e 69 54 61 62 6c 65 3d 3d 69 54 61 62 20 r->iTable==iTab
15190 26 26 20 70 49 74 65 72 2d 3e 65 4c 6f 63 6b 21 && pIter->eLock!
15191 3d 65 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 20 20 =eLock ){.
15192 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f sqlite3Connectio
15193 6e 42 6c 6f 63 6b 65 64 28 70 2d 3e 64 62 2c 20 nBlocked(p->db,
15194 70 49 74 65 72 2d 3e 70 42 74 72 65 65 2d 3e 64 pIter->pBtree->d
15195 62 29 3b 0a 20 20 20 20 20 20 69 66 28 20 65 4c b);. if( eL
15196 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 ock==WRITE_LOCK
15197 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 ){. asser
15198 74 28 20 70 3d 3d 70 42 74 2d 3e 70 57 72 69 74 t( p==pBt->pWrit
15199 65 72 20 29 3b 0a 20 20 20 20 20 20 20 20 70 42 er );. pB
1519a 74 2d 3e 69 73 50 65 6e 64 69 6e 67 20 3d 20 31 t->isPending = 1
1519b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
1519c 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f return SQLITE_LO
1519d 43 4b 45 44 5f 53 48 41 52 45 44 43 41 43 48 45 CKED_SHAREDCACHE
1519e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 ;. }. }. re
1519f 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
151a0 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 53 51 4c }.#endif /* !SQL
151a1 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f ITE_OMIT_SHARED_
151a2 43 41 43 48 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 CACHE */..#ifnde
151a3 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 f SQLITE_OMIT_SH
151a4 41 52 45 44 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a ARED_CACHE./*.**
151a5 20 41 64 64 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 Add a lock on t
151a6 68 65 20 74 61 62 6c 65 20 77 69 74 68 20 72 6f he table with ro
151a7 6f 74 2d 70 61 67 65 20 69 54 61 62 6c 65 20 74 ot-page iTable t
151a8 6f 20 74 68 65 20 73 68 61 72 65 64 2d 62 74 72 o the shared-btr
151a9 65 65 20 75 73 65 64 0a 2a 2a 20 62 79 20 42 74 ee used.** by Bt
151aa 72 65 65 20 68 61 6e 64 6c 65 20 70 2e 20 50 61 ree handle p. Pa
151ab 72 61 6d 65 74 65 72 20 65 4c 6f 63 6b 20 6d 75 rameter eLock mu
151ac 73 74 20 62 65 20 65 69 74 68 65 72 20 52 45 41 st be either REA
151ad 44 5f 4c 4f 43 4b 20 6f 72 20 0a 2a 2a 20 57 52 D_LOCK or .** WR
151ae 49 54 45 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 ITE_LOCK..**.**
151af 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 73 This function as
151b0 73 75 6d 65 73 20 74 68 65 20 66 6f 6c 6c 6f 77 sumes the follow
151b1 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 28 61 29 ing:.**.** (a)
151b2 20 54 68 65 20 73 70 65 63 69 66 69 65 64 20 62 The specified b
151b3 2d 74 72 65 65 20 63 6f 6e 6e 65 63 74 69 6f 6e -tree connection
151b4 20 68 61 6e 64 6c 65 20 69 73 20 63 6f 6e 6e 65 handle is conne
151b5 63 74 65 64 20 74 6f 20 61 20 73 68 61 72 61 62 cted to a sharab
151b6 6c 65 0a 2a 2a 20 20 20 20 20 20 20 62 2d 74 72 le.** b-tr
151b7 65 65 20 64 61 74 61 62 61 73 65 20 28 6f 6e 65 ee database (one
151b8 20 77 69 74 68 20 74 68 65 20 42 74 53 68 61 72 with the BtShar
151b9 65 64 2e 73 68 61 72 61 62 6c 65 29 20 66 6c 61 ed.sharable) fla
151ba 67 20 73 65 74 2c 20 61 6e 64 0a 2a 2a 0a 2a 2a g set, and.**.**
151bb 20 20 20 28 62 29 20 4e 6f 20 6f 74 68 65 72 20 (b) No other
151bc 62 2d 74 72 65 65 20 63 6f 6e 6e 65 63 74 69 6f b-tree connectio
151bd 6e 20 68 61 6e 64 6c 65 20 68 6f 6c 64 73 20 61 n handle holds a
151be 20 6c 6f 63 6b 20 74 68 61 74 20 63 6f 6e 66 6c lock that confl
151bf 69 63 74 73 0a 2a 2a 20 20 20 20 20 20 20 77 69 icts.** wi
151c0 74 68 20 74 68 65 20 72 65 71 75 65 73 74 65 64 th the requested
151c1 20 6c 6f 63 6b 20 28 69 2e 65 2e 20 71 75 65 72 lock (i.e. quer
151c2 79 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c ySharedCacheTabl
151c3 65 4c 6f 63 6b 28 29 20 68 61 73 0a 2a 2a 20 20 eLock() has.**
151c4 20 20 20 20 20 61 6c 72 65 61 64 79 20 62 65 65 already bee
151c5 6e 20 63 61 6c 6c 65 64 20 61 6e 64 20 72 65 74 n called and ret
151c6 75 72 6e 65 64 20 53 51 4c 49 54 45 5f 4f 4b 29 urned SQLITE_OK)
151c7 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f ..**.** SQLITE_O
151c8 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 K is returned if
151c9 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 61 64 64 the lock is add
151ca 65 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2e ed successfully.
151cb 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 0a 2a SQLITE_NOMEM .*
151cc 2a 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 * is returned if
151cd 20 61 20 6d 61 6c 6c 6f 63 20 61 74 74 65 6d 70 a malloc attemp
151ce 74 20 66 61 69 6c 73 2e 0a 2a 2f 0a 73 74 61 74 t fails..*/.stat
151cf 69 63 20 69 6e 74 20 73 65 74 53 68 61 72 65 64 ic int setShared
151d0 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 42 CacheTableLock(B
151d1 74 72 65 65 20 2a 70 2c 20 50 67 6e 6f 20 69 54 tree *p, Pgno iT
151d2 61 62 6c 65 2c 20 75 38 20 65 4c 6f 63 6b 29 7b able, u8 eLock){
151d3 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 . BtShared *pBt
151d4 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 42 74 4c = p->pBt;. BtL
151d5 6f 63 6b 20 2a 70 4c 6f 63 6b 20 3d 20 30 3b 0a ock *pLock = 0;.
151d6 20 20 42 74 4c 6f 63 6b 20 2a 70 49 74 65 72 3b BtLock *pIter;
151d7 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 .. assert( sqli
151d8 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 te3BtreeHoldsMut
151d9 65 78 28 70 29 20 29 3b 0a 20 20 61 73 73 65 72 ex(p) );. asser
151da 74 28 20 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f 4c t( eLock==READ_L
151db 4f 43 4b 20 7c 7c 20 65 4c 6f 63 6b 3d 3d 57 52 OCK || eLock==WR
151dc 49 54 45 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 ITE_LOCK );. as
151dd 73 65 72 74 28 20 70 2d 3e 64 62 21 3d 30 20 29 sert( p->db!=0 )
151de 3b 0a 0a 20 20 2f 2a 20 41 20 63 6f 6e 6e 65 63 ;.. /* A connec
151df 74 69 6f 6e 20 77 69 74 68 20 74 68 65 20 72 65 tion with the re
151e0 61 64 2d 75 6e 63 6f 6d 6d 69 74 74 65 64 20 66 ad-uncommitted f
151e1 6c 61 67 20 73 65 74 20 77 69 6c 6c 20 6e 65 76 lag set will nev
151e2 65 72 20 74 72 79 20 74 6f 0a 20 20 2a 2a 20 6f er try to. ** o
151e3 62 74 61 69 6e 20 61 20 72 65 61 64 2d 6c 6f 63 btain a read-loc
151e4 6b 20 75 73 69 6e 67 20 74 68 69 73 20 66 75 6e k using this fun
151e5 63 74 69 6f 6e 2e 20 54 68 65 20 6f 6e 6c 79 20 ction. The only
151e6 72 65 61 64 2d 6c 6f 63 6b 20 6f 62 74 61 69 6e read-lock obtain
151e7 65 64 0a 20 20 2a 2a 20 62 79 20 61 20 63 6f 6e ed. ** by a con
151e8 6e 65 63 74 69 6f 6e 20 69 6e 20 72 65 61 64 2d nection in read-
151e9 75 6e 63 6f 6d 6d 69 74 74 65 64 20 6d 6f 64 65 uncommitted mode
151ea 20 69 73 20 6f 6e 20 74 68 65 20 73 71 6c 69 74 is on the sqlit
151eb 65 5f 6d 61 73 74 65 72 20 0a 20 20 2a 2a 20 74 e_master . ** t
151ec 61 62 6c 65 2c 20 61 6e 64 20 74 68 61 74 20 6c able, and that l
151ed 6f 63 6b 20 69 73 20 6f 62 74 61 69 6e 65 64 20 ock is obtained
151ee 69 6e 20 42 74 72 65 65 42 65 67 69 6e 54 72 61 in BtreeBeginTra
151ef 6e 73 28 29 2e 20 20 2a 2f 0a 20 20 61 73 73 65 ns(). */. asse
151f0 72 74 28 20 30 3d 3d 28 70 2d 3e 64 62 2d 3e 66 rt( 0==(p->db->f
151f1 6c 61 67 73 26 53 51 4c 49 54 45 5f 52 65 61 64 lags&SQLITE_Read
151f2 55 6e 63 6f 6d 6d 69 74 74 65 64 29 20 7c 7c 20 Uncommitted) ||
151f3 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 eLock==WRITE_LOC
151f4 4b 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 K );.. /* This
151f5 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 function should
151f6 6f 6e 6c 79 20 62 65 20 63 61 6c 6c 65 64 20 6f only be called o
151f7 6e 20 61 20 73 68 61 72 61 62 6c 65 20 62 2d 74 n a sharable b-t
151f8 72 65 65 20 61 66 74 65 72 20 69 74 20 0a 20 20 ree after it .
151f9 2a 2a 20 68 61 73 20 62 65 65 6e 20 64 65 74 65 ** has been dete
151fa 72 6d 69 6e 65 64 20 74 68 61 74 20 6e 6f 20 6f rmined that no o
151fb 74 68 65 72 20 62 2d 74 72 65 65 20 68 6f 6c 64 ther b-tree hold
151fc 73 20 61 20 63 6f 6e 66 6c 69 63 74 69 6e 67 20 s a conflicting
151fd 6c 6f 63 6b 2e 20 20 2a 2f 0a 20 20 61 73 73 65 lock. */. asse
151fe 72 74 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 rt( p->sharable
151ff 29 3b 0a 20 20 61 73 73 65 72 74 28 20 53 51 4c );. assert( SQL
15200 49 54 45 5f 4f 4b 3d 3d 71 75 65 72 79 53 68 61 ITE_OK==querySha
15201 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 redCacheTableLoc
15202 6b 28 70 2c 20 69 54 61 62 6c 65 2c 20 65 4c 6f k(p, iTable, eLo
15203 63 6b 29 20 29 3b 0a 0a 20 20 2f 2a 20 46 69 72 ck) );.. /* Fir
15204 73 74 20 73 65 61 72 63 68 20 74 68 65 20 6c 69 st search the li
15205 73 74 20 66 6f 72 20 61 6e 20 65 78 69 73 74 69 st for an existi
15206 6e 67 20 6c 6f 63 6b 20 6f 6e 20 74 68 69 73 20 ng lock on this
15207 74 61 62 6c 65 2e 20 2a 2f 0a 20 20 66 6f 72 28 table. */. for(
15208 70 49 74 65 72 3d 70 42 74 2d 3e 70 4c 6f 63 6b pIter=pBt->pLock
15209 3b 20 70 49 74 65 72 3b 20 70 49 74 65 72 3d 70 ; pIter; pIter=p
1520a 49 74 65 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 Iter->pNext){.
1520b 20 20 69 66 28 20 70 49 74 65 72 2d 3e 69 54 61 if( pIter->iTa
1520c 62 6c 65 3d 3d 69 54 61 62 6c 65 20 26 26 20 70 ble==iTable && p
1520d 49 74 65 72 2d 3e 70 42 74 72 65 65 3d 3d 70 20 Iter->pBtree==p
1520e 29 7b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 20 3d ){. pLock =
1520f 20 70 49 74 65 72 3b 0a 20 20 20 20 20 20 62 72 pIter;. br
15210 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a eak;. }. }..
15211 20 20 2f 2a 20 49 66 20 74 68 65 20 61 62 6f 76 /* If the abov
15212 65 20 73 65 61 72 63 68 20 64 69 64 20 6e 6f 74 e search did not
15213 20 66 69 6e 64 20 61 20 42 74 4c 6f 63 6b 20 73 find a BtLock s
15214 74 72 75 63 74 20 61 73 73 6f 63 69 61 74 69 6e truct associatin
15215 67 20 42 74 72 65 65 20 70 0a 20 20 2a 2a 20 77 g Btree p. ** w
15216 69 74 68 20 74 61 62 6c 65 20 69 54 61 62 6c 65 ith table iTable
15217 2c 20 61 6c 6c 6f 63 61 74 65 20 6f 6e 65 20 61 , allocate one a
15218 6e 64 20 6c 69 6e 6b 20 69 74 20 69 6e 74 6f 20 nd link it into
15219 74 68 65 20 6c 69 73 74 2e 0a 20 20 2a 2f 0a 20 the list.. */.
1521a 20 69 66 28 20 21 70 4c 6f 63 6b 20 29 7b 0a 20 if( !pLock ){.
1521b 20 20 20 70 4c 6f 63 6b 20 3d 20 28 42 74 4c 6f pLock = (BtLo
1521c 63 6b 20 2a 29 73 71 6c 69 74 65 33 4d 61 6c 6c ck *)sqlite3Mall
1521d 6f 63 5a 65 72 6f 28 73 69 7a 65 6f 66 28 42 74 ocZero(sizeof(Bt
1521e 4c 6f 63 6b 29 29 3b 0a 20 20 20 20 69 66 28 20 Lock));. if(
1521f 21 70 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 20 20 !pLock ){.
15220 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f return SQLITE_NO
15221 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 MEM;. }. p
15222 4c 6f 63 6b 2d 3e 69 54 61 62 6c 65 20 3d 20 69 Lock->iTable = i
15223 54 61 62 6c 65 3b 0a 20 20 20 20 70 4c 6f 63 6b Table;. pLock
15224 2d 3e 70 42 74 72 65 65 20 3d 20 70 3b 0a 20 20 ->pBtree = p;.
15225 20 20 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 20 3d pLock->pNext =
15226 20 70 42 74 2d 3e 70 4c 6f 63 6b 3b 0a 20 20 20 pBt->pLock;.
15227 20 70 42 74 2d 3e 70 4c 6f 63 6b 20 3d 20 70 4c pBt->pLock = pL
15228 6f 63 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 ock;. }.. /* S
15229 65 74 20 74 68 65 20 42 74 4c 6f 63 6b 2e 65 4c et the BtLock.eL
1522a 6f 63 6b 20 76 61 72 69 61 62 6c 65 20 74 6f 20 ock variable to
1522b 74 68 65 20 6d 61 78 69 6d 75 6d 20 6f 66 20 74 the maximum of t
1522c 68 65 20 63 75 72 72 65 6e 74 20 6c 6f 63 6b 0a he current lock.
1522d 20 20 2a 2a 20 61 6e 64 20 74 68 65 20 72 65 71 ** and the req
1522e 75 65 73 74 65 64 20 6c 6f 63 6b 2e 20 54 68 69 uested lock. Thi
1522f 73 20 6d 65 61 6e 73 20 69 66 20 61 20 77 72 69 s means if a wri
15230 74 65 2d 6c 6f 63 6b 20 77 61 73 20 61 6c 72 65 te-lock was alre
15231 61 64 79 20 68 65 6c 64 0a 20 20 2a 2a 20 61 6e ady held. ** an
15232 64 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 72 65 d a read-lock re
15233 71 75 65 73 74 65 64 2c 20 77 65 20 64 6f 6e 27 quested, we don'
15234 74 20 69 6e 63 6f 72 72 65 63 74 6c 79 20 64 6f t incorrectly do
15235 77 6e 67 72 61 64 65 20 74 68 65 20 6c 6f 63 6b wngrade the lock
15236 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 .. */. assert(
15237 20 57 52 49 54 45 5f 4c 4f 43 4b 3e 52 45 41 44 WRITE_LOCK>READ
15238 5f 4c 4f 43 4b 20 29 3b 0a 20 20 69 66 28 20 65 _LOCK );. if( e
15239 4c 6f 63 6b 3e 70 4c 6f 63 6b 2d 3e 65 4c 6f 63 Lock>pLock->eLoc
1523a 6b 20 29 7b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e k ){. pLock->
1523b 65 4c 6f 63 6b 20 3d 20 65 4c 6f 63 6b 3b 0a 20 eLock = eLock;.
1523c 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c }.. return SQL
1523d 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 ITE_OK;.}.#endif
1523e 20 2f 2a 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54 /* !SQLITE_OMIT
1523f 5f 53 48 41 52 45 44 5f 43 41 43 48 45 20 2a 2f _SHARED_CACHE */
15240 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
15241 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 _OMIT_SHARED_CAC
15242 48 45 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 HE./*.** Release
15243 20 61 6c 6c 20 74 68 65 20 74 61 62 6c 65 20 6c all the table l
15244 6f 63 6b 73 20 28 6c 6f 63 6b 73 20 6f 62 74 61 ocks (locks obta
15245 69 6e 65 64 20 76 69 61 20 63 61 6c 6c 73 20 74 ined via calls t
15246 6f 0a 2a 2a 20 74 68 65 20 73 65 74 53 68 61 72 o.** the setShar
15247 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b edCacheTableLock
15248 28 29 20 70 72 6f 63 65 64 75 72 65 29 20 68 65 () procedure) he
15249 6c 64 20 62 79 20 42 74 72 65 65 20 68 61 6e 64 ld by Btree hand
1524a 6c 65 20 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 le p..**.** This
1524b 20 66 75 6e 63 74 69 6f 6e 20 61 73 73 75 6d 65 function assume
1524c 73 20 74 68 61 74 20 68 61 6e 64 6c 65 20 70 20 s that handle p
1524d 68 61 73 20 61 6e 20 6f 70 65 6e 20 72 65 61 64 has an open read
1524e 20 6f 72 20 77 72 69 74 65 20 0a 2a 2a 20 74 72 or write .** tr
1524f 61 6e 73 61 63 74 69 6f 6e 2e 20 49 66 20 69 74 ansaction. If it
15250 20 64 6f 65 73 20 6e 6f 74 2c 20 74 68 65 6e 20 does not, then
15251 74 68 65 20 42 74 53 68 61 72 65 64 2e 69 73 50 the BtShared.isP
15252 65 6e 64 69 6e 67 20 76 61 72 69 61 62 6c 65 0a ending variable.
15253 2a 2a 20 6d 61 79 20 62 65 20 69 6e 63 6f 72 72 ** may be incorr
15254 65 63 74 6c 79 20 63 6c 65 61 72 65 64 2e 0a 2a ectly cleared..*
15255 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6c /.static void cl
15256 65 61 72 41 6c 6c 53 68 61 72 65 64 43 61 63 68 earAllSharedCach
15257 65 54 61 62 6c 65 4c 6f 63 6b 73 28 42 74 72 65 eTableLocks(Btre
15258 65 20 2a 70 29 7b 0a 20 20 42 74 53 68 61 72 65 e *p){. BtShare
15259 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b d *pBt = p->pBt;
1525a 0a 20 20 42 74 4c 6f 63 6b 20 2a 2a 70 70 49 74 . BtLock **ppIt
1525b 65 72 20 3d 20 26 70 42 74 2d 3e 70 4c 6f 63 6b er = &pBt->pLock
1525c 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c ;.. assert( sql
1525d 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 ite3BtreeHoldsMu
1525e 74 65 78 28 70 29 20 29 3b 0a 20 20 61 73 73 65 tex(p) );. asse
1525f 72 74 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 rt( p->sharable
15260 7c 7c 20 30 3d 3d 2a 70 70 49 74 65 72 20 29 3b || 0==*ppIter );
15261 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 6e . assert( p->in
15262 54 72 61 6e 73 3e 30 20 29 3b 0a 0a 20 20 77 68 Trans>0 );.. wh
15263 69 6c 65 28 20 2a 70 70 49 74 65 72 20 29 7b 0a ile( *ppIter ){.
15264 20 20 20 20 42 74 4c 6f 63 6b 20 2a 70 4c 6f 63 BtLock *pLoc
15265 6b 20 3d 20 2a 70 70 49 74 65 72 3b 0a 20 20 20 k = *ppIter;.
15266 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 69 73 assert( pBt->is
15267 45 78 63 6c 75 73 69 76 65 3d 3d 30 20 7c 7c 20 Exclusive==0 ||
15268 70 42 74 2d 3e 70 57 72 69 74 65 72 3d 3d 70 4c pBt->pWriter==pL
15269 6f 63 6b 2d 3e 70 42 74 72 65 65 20 29 3b 0a 20 ock->pBtree );.
1526a 20 20 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b assert( pLock
1526b 2d 3e 70 42 74 72 65 65 2d 3e 69 6e 54 72 61 6e ->pBtree->inTran
1526c 73 3e 3d 70 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 20 s>=pLock->eLock
1526d 29 3b 0a 20 20 20 20 69 66 28 20 70 4c 6f 63 6b );. if( pLock
1526e 2d 3e 70 42 74 72 65 65 3d 3d 70 20 29 7b 0a 20 ->pBtree==p ){.
1526f 20 20 20 20 20 2a 70 70 49 74 65 72 20 3d 20 70 *ppIter = p
15270 4c 6f 63 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 Lock->pNext;.
15271 20 20 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b assert( pLock
15272 2d 3e 69 54 61 62 6c 65 21 3d 31 20 7c 7c 20 70 ->iTable!=1 || p
15273 4c 6f 63 6b 3d 3d 26 70 2d 3e 6c 6f 63 6b 20 29 Lock==&p->lock )
15274 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4c 6f 63 ;. if( pLoc
15275 6b 2d 3e 69 54 61 62 6c 65 21 3d 31 20 29 7b 0a k->iTable!=1 ){.
15276 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f sqlite3_
15277 66 72 65 65 28 70 4c 6f 63 6b 29 3b 0a 20 20 20 free(pLock);.
15278 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a }. }else{.
15279 20 20 20 20 20 20 70 70 49 74 65 72 20 3d 20 26 ppIter = &
1527a 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 pLock->pNext;.
1527b 20 20 7d 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 }. }.. asser
1527c 74 28 20 70 42 74 2d 3e 69 73 50 65 6e 64 69 6e t( pBt->isPendin
1527d 67 3d 3d 30 20 7c 7c 20 70 42 74 2d 3e 70 57 72 g==0 || pBt->pWr
1527e 69 74 65 72 20 29 3b 0a 20 20 69 66 28 20 70 42 iter );. if( pB
1527f 74 2d 3e 70 57 72 69 74 65 72 3d 3d 70 20 29 7b t->pWriter==p ){
15280 0a 20 20 20 20 70 42 74 2d 3e 70 57 72 69 74 65 . pBt->pWrite
15281 72 20 3d 20 30 3b 0a 20 20 20 20 70 42 74 2d 3e r = 0;. pBt->
15282 69 73 45 78 63 6c 75 73 69 76 65 20 3d 20 30 3b isExclusive = 0;
15283 0a 20 20 20 20 70 42 74 2d 3e 69 73 50 65 6e 64 . pBt->isPend
15284 69 6e 67 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 ing = 0;. }else
15285 20 69 66 28 20 70 42 74 2d 3e 6e 54 72 61 6e 73 if( pBt->nTrans
15286 61 63 74 69 6f 6e 3d 3d 32 20 29 7b 0a 20 20 20 action==2 ){.
15287 20 2f 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f /* This functio
15288 6e 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e n is called when
15289 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 20 69 73 connection p is
1528a 20 63 6f 6e 63 6c 75 64 69 6e 67 20 69 74 73 20 concluding its
1528b 0a 20 20 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 . ** transact
1528c 69 6f 6e 2e 20 49 66 20 74 68 65 72 65 20 63 75 ion. If there cu
1528d 72 72 65 6e 74 6c 79 20 65 78 69 73 74 73 20 61 rrently exists a
1528e 20 77 72 69 74 65 72 2c 20 61 6e 64 20 70 20 69 writer, and p i
1528f 73 20 6e 6f 74 0a 20 20 20 20 2a 2a 20 74 68 61 s not. ** tha
15290 74 20 77 72 69 74 65 72 2c 20 74 68 65 6e 20 74 t writer, then t
15291 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6c 6f 63 he number of loc
15292 6b 73 20 68 65 6c 64 20 62 79 20 63 6f 6e 6e 65 ks held by conne
15293 63 74 69 6f 6e 73 20 6f 74 68 65 72 0a 20 20 20 ctions other.
15294 20 2a 2a 20 74 68 61 6e 20 74 68 65 20 77 72 69 ** than the wri
15295 74 65 72 20 6d 75 73 74 20 62 65 20 61 62 6f 75 ter must be abou
15296 74 20 74 6f 20 64 72 6f 70 20 74 6f 20 7a 65 72 t to drop to zer
15297 6f 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 0a o. In this case.
15298 20 20 20 20 2a 2a 20 73 65 74 20 74 68 65 20 69 ** set the i
15299 73 50 65 6e 64 69 6e 67 20 66 6c 61 67 20 74 6f sPending flag to
1529a 20 30 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 0.. **. *
1529b 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f * If there is no
1529c 74 20 63 75 72 72 65 6e 74 6c 79 20 61 20 77 72 t currently a wr
1529d 69 74 65 72 2c 20 74 68 65 6e 20 42 74 53 68 61 iter, then BtSha
1529e 72 65 64 2e 69 73 50 65 6e 64 69 6e 67 20 6d 75 red.isPending mu
1529f 73 74 0a 20 20 20 20 2a 2a 20 62 65 20 7a 65 72 st. ** be zer
152a0 6f 20 61 6c 72 65 61 64 79 2e 20 53 6f 20 74 68 o already. So th
152a1 69 73 20 6e 65 78 74 20 6c 69 6e 65 20 69 73 20 is next line is
152a2 68 61 72 6d 6c 65 73 73 20 69 6e 20 74 68 61 74 harmless in that
152a3 20 63 61 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 case.. */.
152a4 20 20 70 42 74 2d 3e 69 73 50 65 6e 64 69 6e 67 pBt->isPending
152a5 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a = 0;. }.}../*.
152a6 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
152a7 20 63 68 61 6e 67 65 73 20 61 6c 6c 20 77 72 69 changes all wri
152a8 74 65 2d 6c 6f 63 6b 73 20 68 65 6c 64 20 62 79 te-locks held by
152a9 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 20 74 6f connection p to
152aa 20 72 65 61 64 2d 6c 6f 63 6b 73 2e 0a 2a 2f 0a read-locks..*/.
152ab 73 74 61 74 69 63 20 76 6f 69 64 20 64 6f 77 6e static void down
152ac 67 72 61 64 65 41 6c 6c 53 68 61 72 65 64 43 61 gradeAllSharedCa
152ad 63 68 65 54 61 62 6c 65 4c 6f 63 6b 73 28 42 74 cheTableLocks(Bt
152ae 72 65 65 20 2a 70 29 7b 0a 20 20 42 74 53 68 61 ree *p){. BtSha
152af 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 red *pBt = p->pB
152b0 74 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e 70 57 t;. if( pBt->pW
152b1 72 69 74 65 72 3d 3d 70 20 29 7b 0a 20 20 20 20 riter==p ){.
152b2 42 74 4c 6f 63 6b 20 2a 70 4c 6f 63 6b 3b 0a 20 BtLock *pLock;.
152b3 20 20 20 70 42 74 2d 3e 70 57 72 69 74 65 72 20 pBt->pWriter
152b4 3d 20 30 3b 0a 20 20 20 20 70 42 74 2d 3e 69 73 = 0;. pBt->is
152b5 45 78 63 6c 75 73 69 76 65 20 3d 20 30 3b 0a 20 Exclusive = 0;.
152b6 20 20 20 70 42 74 2d 3e 69 73 50 65 6e 64 69 6e pBt->isPendin
152b7 67 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28 70 g = 0;. for(p
152b8 4c 6f 63 6b 3d 70 42 74 2d 3e 70 4c 6f 63 6b 3b Lock=pBt->pLock;
152b9 20 70 4c 6f 63 6b 3b 20 70 4c 6f 63 6b 3d 70 4c pLock; pLock=pL
152ba 6f 63 6b 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 ock->pNext){.
152bb 20 20 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b assert( pLock
152bc 2d 3e 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f ->eLock==READ_LO
152bd 43 4b 20 7c 7c 20 70 4c 6f 63 6b 2d 3e 70 42 74 CK || pLock->pBt
152be 72 65 65 3d 3d 70 20 29 3b 0a 20 20 20 20 20 20 ree==p );.
152bf 70 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 20 3d 20 52 pLock->eLock = R
152c0 45 41 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a EAD_LOCK;. }.
152c1 20 20 7d 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a }.}..#endif /*
152c2 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 SQLITE_OMIT_SHA
152c3 52 45 44 5f 43 41 43 48 45 20 2a 2f 0a 0a 73 74 RED_CACHE */..st
152c4 61 74 69 63 20 76 6f 69 64 20 72 65 6c 65 61 73 atic void releas
152c5 65 50 61 67 65 28 4d 65 6d 50 61 67 65 20 2a 70 ePage(MemPage *p
152c6 50 61 67 65 29 3b 20 20 2f 2a 20 46 6f 72 77 61 Page); /* Forwa
152c7 72 64 20 72 65 66 65 72 65 6e 63 65 20 2a 2f 0a rd reference */.
152c8 0a 2f 2a 0a 2a 2a 20 56 65 72 69 66 79 20 74 68 ./*.** Verify th
152c9 61 74 20 74 68 65 20 63 75 72 73 6f 72 20 68 6f at the cursor ho
152ca 6c 64 73 20 61 20 6d 75 74 65 78 20 6f 6e 20 74 lds a mutex on t
152cb 68 65 20 42 74 53 68 61 72 65 64 0a 2a 2f 0a 23 he BtShared.*/.#
152cc 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 73 74 ifndef NDEBUG.st
152cd 61 74 69 63 20 69 6e 74 20 63 75 72 73 6f 72 48 atic int cursorH
152ce 6f 6c 64 73 4d 75 74 65 78 28 42 74 43 75 72 73 oldsMutex(BtCurs
152cf 6f 72 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e or *p){. return
152d0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
152d1 65 6c 64 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 eld(p->pBt->mute
152d2 78 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 23 x);.}.#endif...#
152d3 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
152d4 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 2f 2a 0a 2a IT_INCRBLOB./*.*
152d5 2a 20 49 6e 76 61 6c 69 64 61 74 65 20 74 68 65 * Invalidate the
152d6 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2d 6c overflow page-l
152d7 69 73 74 20 63 61 63 68 65 20 66 6f 72 20 63 75 ist cache for cu
152d8 72 73 6f 72 20 70 43 75 72 2c 20 69 66 20 61 6e rsor pCur, if an
152d9 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 y..*/.static voi
152da 64 20 69 6e 76 61 6c 69 64 61 74 65 4f 76 65 72 d invalidateOver
152db 66 6c 6f 77 43 61 63 68 65 28 42 74 43 75 72 73 flowCache(BtCurs
152dc 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 61 73 73 or *pCur){. ass
152dd 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 ert( cursorHolds
152de 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 Mutex(pCur) );.
152df 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 sqlite3_free(pC
152e0 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 29 3b 0a ur->aOverflow);.
152e1 20 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f pCur->aOverflo
152e2 77 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 w = 0;.}../*.**
152e3 49 6e 76 61 6c 69 64 61 74 65 20 74 68 65 20 6f Invalidate the o
152e4 76 65 72 66 6c 6f 77 20 70 61 67 65 2d 6c 69 73 verflow page-lis
152e5 74 20 63 61 63 68 65 20 66 6f 72 20 61 6c 6c 20 t cache for all
152e6 63 75 72 73 6f 72 73 20 6f 70 65 6e 65 64 0a 2a cursors opened.*
152e7 2a 20 6f 6e 20 74 68 65 20 73 68 61 72 65 64 20 * on the shared
152e8 62 74 72 65 65 20 73 74 72 75 63 74 75 72 65 20 btree structure
152e9 70 42 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 pBt..*/.static v
152ea 6f 69 64 20 69 6e 76 61 6c 69 64 61 74 65 41 6c oid invalidateAl
152eb 6c 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28 42 lOverflowCache(B
152ec 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 tShared *pBt){.
152ed 20 42 74 43 75 72 73 6f 72 20 2a 70 3b 0a 20 20 BtCursor *p;.
152ee 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
152ef 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e mutex_held(pBt->
152f0 6d 75 74 65 78 29 20 29 3b 0a 20 20 66 6f 72 28 mutex) );. for(
152f1 70 3d 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b 20 p=pBt->pCursor;
152f2 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a p; p=p->pNext){.
152f3 20 20 20 20 69 6e 76 61 6c 69 64 61 74 65 4f 76 invalidateOv
152f4 65 72 66 6c 6f 77 43 61 63 68 65 28 70 29 3b 0a erflowCache(p);.
152f5 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 }.}../*.** Thi
152f6 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 s function is ca
152f7 6c 6c 65 64 20 62 65 66 6f 72 65 20 6d 6f 64 69 lled before modi
152f8 66 79 69 6e 67 20 74 68 65 20 63 6f 6e 74 65 6e fying the conten
152f9 74 73 20 6f 66 20 61 20 74 61 62 6c 65 0a 2a 2a ts of a table.**
152fa 20 62 2d 74 72 65 65 20 74 6f 20 69 6e 76 61 6c b-tree to inval
152fb 69 64 61 74 65 20 61 6e 79 20 69 6e 63 72 62 6c idate any incrbl
152fc 6f 62 20 63 75 72 73 6f 72 73 20 74 68 61 74 20 ob cursors that
152fd 61 72 65 20 6f 70 65 6e 20 6f 6e 20 74 68 65 0a are open on the.
152fe 2a 2a 20 72 6f 77 20 6f 72 20 6f 6e 65 20 6f 66 ** row or one of
152ff 20 74 68 65 20 72 6f 77 73 20 62 65 69 6e 67 20 the rows being
15300 6d 6f 64 69 66 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 modified..**.**
15301 49 66 20 61 72 67 75 6d 65 6e 74 20 69 73 43 6c If argument isCl
15302 65 61 72 54 61 62 6c 65 20 69 73 20 74 72 75 65 earTable is true
15303 2c 20 74 68 65 6e 20 74 68 65 20 65 6e 74 69 72 , then the entir
15304 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 e contents of th
15305 65 0a 2a 2a 20 74 61 62 6c 65 20 69 73 20 61 62 e.** table is ab
15306 6f 75 74 20 74 6f 20 62 65 20 64 65 6c 65 74 65 out to be delete
15307 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 d. In this case
15308 69 6e 76 61 6c 69 64 61 74 65 20 61 6c 6c 20 69 invalidate all i
15309 6e 63 72 62 6c 6f 62 0a 2a 2a 20 63 75 72 73 6f ncrblob.** curso
1530a 72 73 20 6f 70 65 6e 20 6f 6e 20 61 6e 79 20 72 rs open on any r
1530b 6f 77 20 77 69 74 68 69 6e 20 74 68 65 20 74 61 ow within the ta
1530c 62 6c 65 20 77 69 74 68 20 72 6f 6f 74 2d 70 61 ble with root-pa
1530d 67 65 20 70 67 6e 6f 52 6f 6f 74 2e 0a 2a 2a 0a ge pgnoRoot..**.
1530e 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 ** Otherwise, if
1530f 20 61 72 67 75 6d 65 6e 74 20 69 73 43 6c 65 61 argument isClea
15310 72 54 61 62 6c 65 20 69 73 20 66 61 6c 73 65 2c rTable is false,
15311 20 74 68 65 6e 20 74 68 65 20 72 6f 77 20 77 69 then the row wi
15312 74 68 0a 2a 2a 20 72 6f 77 69 64 20 69 52 6f 77 th.** rowid iRow
15313 20 69 73 20 62 65 69 6e 67 20 72 65 70 6c 61 63 is being replac
15314 65 64 20 6f 72 20 64 65 6c 65 74 65 64 2e 20 49 ed or deleted. I
15315 6e 20 74 68 69 73 20 63 61 73 65 20 69 6e 76 61 n this case inva
15316 6c 69 64 61 74 65 0a 2a 2a 20 6f 6e 6c 79 20 74 lidate.** only t
15317 68 6f 73 65 20 69 6e 63 72 62 6c 6f 62 20 63 75 hose incrblob cu
15318 72 73 6f 72 73 20 6f 70 65 6e 20 6f 6e 20 74 68 rsors open on th
15319 69 73 20 73 70 65 63 69 66 69 63 20 72 6f 77 2e is specific row.
1531a 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
1531b 69 6e 76 61 6c 69 64 61 74 65 49 6e 63 72 62 6c invalidateIncrbl
1531c 6f 62 43 75 72 73 6f 72 73 28 0a 20 20 42 74 72 obCursors(. Btr
1531d 65 65 20 2a 70 42 74 72 65 65 2c 20 20 20 20 20 ee *pBtree,
1531e 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 /* The data
1531f 62 61 73 65 20 66 69 6c 65 20 74 6f 20 63 68 65 base file to che
15320 63 6b 20 2a 2f 0a 20 20 69 36 34 20 69 52 6f 77 ck */. i64 iRow
15321 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
15322 2f 2a 20 54 68 65 20 72 6f 77 69 64 20 74 68 61 /* The rowid tha
15323 74 20 6d 69 67 68 74 20 62 65 20 63 68 61 6e 67 t might be chang
15324 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 69 73 43 ing */. int isC
15325 6c 65 61 72 54 61 62 6c 65 20 20 20 20 20 20 20 learTable
15326 20 2f 2a 20 54 72 75 65 20 69 66 20 61 6c 6c 20 /* True if all
15327 72 6f 77 73 20 61 72 65 20 62 65 69 6e 67 20 64 rows are being d
15328 65 6c 65 74 65 64 20 2a 2f 0a 29 7b 0a 20 20 42 eleted */.){. B
15329 74 43 75 72 73 6f 72 20 2a 70 3b 0a 20 20 42 74 tCursor *p;. Bt
1532a 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 42 Shared *pBt = pB
1532b 74 72 65 65 2d 3e 70 42 74 3b 0a 20 20 61 73 73 tree->pBt;. ass
1532c 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 ert( sqlite3Btre
1532d 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 42 74 72 eHoldsMutex(pBtr
1532e 65 65 29 20 29 3b 0a 20 20 66 6f 72 28 70 3d 70 ee) );. for(p=p
1532f 42 74 2d 3e 70 43 75 72 73 6f 72 3b 20 70 3b 20 Bt->pCursor; p;
15330 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 p=p->pNext){.
15331 20 69 66 28 20 70 2d 3e 69 73 49 6e 63 72 62 6c if( p->isIncrbl
15332 6f 62 48 61 6e 64 6c 65 20 26 26 20 28 69 73 43 obHandle && (isC
15333 6c 65 61 72 54 61 62 6c 65 20 7c 7c 20 70 2d 3e learTable || p->
15334 69 6e 66 6f 2e 6e 4b 65 79 3d 3d 69 52 6f 77 29 info.nKey==iRow)
15335 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 65 53 74 ){. p->eSt
15336 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56 ate = CURSOR_INV
15337 41 4c 49 44 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a ALID;. }. }.
15338 7d 0a 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 }..#else. #defi
15339 6e 65 20 69 6e 76 61 6c 69 64 61 74 65 4f 76 65 ne invalidateOve
1533a 72 66 6c 6f 77 43 61 63 68 65 28 78 29 0a 20 20 rflowCache(x).
1533b 23 64 65 66 69 6e 65 20 69 6e 76 61 6c 69 64 61 #define invalida
1533c 74 65 41 6c 6c 4f 76 65 72 66 6c 6f 77 43 61 63 teAllOverflowCac
1533d 68 65 28 78 29 0a 20 20 23 64 65 66 69 6e 65 20 he(x). #define
1533e 69 6e 76 61 6c 69 64 61 74 65 49 6e 63 72 62 6c invalidateIncrbl
1533f 6f 62 43 75 72 73 6f 72 73 28 78 2c 79 2c 7a 29 obCursors(x,y,z)
15340 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 53 .#endif../*.** S
15341 65 74 20 62 69 74 20 70 67 6e 6f 20 6f 66 20 74 et bit pgno of t
15342 68 65 20 42 74 53 68 61 72 65 64 2e 70 48 61 73 he BtShared.pHas
15343 43 6f 6e 74 65 6e 74 20 62 69 74 76 65 63 2e 20 Content bitvec.
15344 54 68 69 73 20 69 73 20 63 61 6c 6c 65 64 20 0a This is called .
15345 2a 2a 20 77 68 65 6e 20 61 20 70 61 67 65 20 74 ** when a page t
15346 68 61 74 20 70 72 65 76 69 6f 75 73 6c 79 20 63 hat previously c
15347 6f 6e 74 61 69 6e 65 64 20 64 61 74 61 20 62 65 ontained data be
15348 63 6f 6d 65 73 20 61 20 66 72 65 65 2d 6c 69 73 comes a free-lis
15349 74 20 6c 65 61 66 20 0a 2a 2a 20 70 61 67 65 2e t leaf .** page.
1534a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 42 74 53 68 61 .**.** The BtSha
1534b 72 65 64 2e 70 48 61 73 43 6f 6e 74 65 6e 74 20 red.pHasContent
1534c 62 69 74 76 65 63 20 65 78 69 73 74 73 20 74 6f bitvec exists to
1534d 20 77 6f 72 6b 20 61 72 6f 75 6e 64 20 61 6e 20 work around an
1534e 6f 62 73 63 75 72 65 0a 2a 2a 20 62 75 67 20 63 obscure.** bug c
1534f 61 75 73 65 64 20 62 79 20 74 68 65 20 69 6e 74 aused by the int
15350 65 72 61 63 74 69 6f 6e 20 6f 66 20 74 77 6f 20 eraction of two
15351 75 73 65 66 75 6c 20 49 4f 20 6f 70 74 69 6d 69 useful IO optimi
15352 7a 61 74 69 6f 6e 73 20 73 75 72 72 6f 75 6e 64 zations surround
15353 69 6e 67 0a 2a 2a 20 66 72 65 65 2d 6c 69 73 74 ing.** free-list
15354 20 6c 65 61 66 20 70 61 67 65 73 3a 0a 2a 2a 0a leaf pages:.**.
15355 2a 2a 20 20 20 31 29 20 57 68 65 6e 20 61 6c 6c ** 1) When all
15356 20 64 61 74 61 20 69 73 20 64 65 6c 65 74 65 64 data is deleted
15357 20 66 72 6f 6d 20 61 20 70 61 67 65 20 61 6e 64 from a page and
15358 20 74 68 65 20 70 61 67 65 20 62 65 63 6f 6d 65 the page become
15359 73 0a 2a 2a 20 20 20 20 20 20 61 20 66 72 65 65 s.** a free
1535a 2d 6c 69 73 74 20 6c 65 61 66 20 70 61 67 65 2c -list leaf page,
1535b 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 the page is not
1535c 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 written to the
1535d 64 61 74 61 62 61 73 65 0a 2a 2a 20 20 20 20 20 database.**
1535e 20 28 61 73 20 66 72 65 65 2d 6c 69 73 74 20 6c (as free-list l
1535f 65 61 66 20 70 61 67 65 73 20 63 6f 6e 74 61 69 eaf pages contai
15360 6e 20 6e 6f 20 6d 65 61 6e 69 6e 67 66 75 6c 20 n no meaningful
15361 64 61 74 61 29 2e 20 53 6f 6d 65 74 69 6d 65 73 data). Sometimes
15362 0a 2a 2a 20 20 20 20 20 20 73 75 63 68 20 61 20 .** such a
15363 70 61 67 65 20 69 73 20 6e 6f 74 20 65 76 65 6e page is not even
15364 20 6a 6f 75 72 6e 61 6c 6c 65 64 20 28 61 73 20 journalled (as
15365 69 74 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 6d it will not be m
15366 6f 64 69 66 69 65 64 2c 0a 2a 2a 20 20 20 20 20 odified,.**
15367 20 77 68 79 20 62 6f 74 68 65 72 20 6a 6f 75 72 why bother jour
15368 6e 61 6c 6c 69 6e 67 20 69 74 3f 29 2e 0a 2a 2a nalling it?)..**
15369 0a 2a 2a 20 20 20 32 29 20 57 68 65 6e 20 61 20 .** 2) When a
1536a 66 72 65 65 2d 6c 69 73 74 20 6c 65 61 66 20 70 free-list leaf p
1536b 61 67 65 20 69 73 20 72 65 75 73 65 64 2c 20 69 age is reused, i
1536c 74 73 20 63 6f 6e 74 65 6e 74 20 69 73 20 6e 6f ts content is no
1536d 74 20 72 65 61 64 0a 2a 2a 20 20 20 20 20 20 66 t read.** f
1536e 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 rom the database
1536f 20 6f 72 20 77 72 69 74 74 65 6e 20 74 6f 20 74 or written to t
15370 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
15371 28 77 68 79 20 73 68 6f 75 6c 64 20 69 74 0a 2a (why should it.*
15372 2a 20 20 20 20 20 20 62 65 2c 20 69 66 20 69 74 * be, if it
15373 20 69 73 20 6e 6f 74 20 61 74 20 61 6c 6c 20 6d is not at all m
15374 65 61 6e 69 6e 67 66 75 6c 3f 29 2e 0a 2a 2a 0a eaningful?)..**.
15375 2a 2a 20 42 79 20 74 68 65 6d 73 65 6c 76 65 73 ** By themselves
15376 2c 20 74 68 65 73 65 20 6f 70 74 69 6d 69 7a 61 , these optimiza
15377 74 69 6f 6e 73 20 77 6f 72 6b 20 66 69 6e 65 20 tions work fine
15378 61 6e 64 20 70 72 6f 76 69 64 65 20 61 20 68 61 and provide a ha
15379 6e 64 79 0a 2a 2a 20 70 65 72 66 6f 72 6d 61 6e ndy.** performan
1537a 63 65 20 62 6f 6f 73 74 20 74 6f 20 62 75 6c 6b ce boost to bulk
1537b 20 64 65 6c 65 74 65 20 6f 72 20 69 6e 73 65 72 delete or inser
1537c 74 20 6f 70 65 72 61 74 69 6f 6e 73 2e 20 48 6f t operations. Ho
1537d 77 65 76 65 72 2c 20 69 66 0a 2a 2a 20 61 20 70 wever, if.** a p
1537e 61 67 65 20 69 73 20 6d 6f 76 65 64 20 74 6f 20 age is moved to
1537f 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20 61 6e the free-list an
15380 64 20 74 68 65 6e 20 72 65 75 73 65 64 20 77 69 d then reused wi
15381 74 68 69 6e 20 74 68 65 20 73 61 6d 65 0a 2a 2a thin the same.**
15382 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 61 20 transaction, a
15383 70 72 6f 62 6c 65 6d 20 63 6f 6d 65 73 20 75 70 problem comes up
15384 2e 20 49 66 20 74 68 65 20 70 61 67 65 20 69 73 . If the page is
15385 20 6e 6f 74 20 6a 6f 75 72 6e 61 6c 6c 65 64 20 not journalled
15386 77 68 65 6e 0a 2a 2a 20 69 74 20 69 73 20 6d 6f when.** it is mo
15387 76 65 64 20 74 6f 20 74 68 65 20 66 72 65 65 2d ved to the free-
15388 6c 69 73 74 20 61 6e 64 20 69 74 20 69 73 20 61 list and it is a
15389 6c 73 6f 20 6e 6f 74 20 6a 6f 75 72 6e 61 6c 6c lso not journall
1538a 65 64 20 77 68 65 6e 20 69 74 0a 2a 2a 20 69 73 ed when it.** is
1538b 20 65 78 74 72 61 63 74 65 64 20 66 72 6f 6d 20 extracted from
1538c 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20 61 6e the free-list an
1538d 64 20 72 65 75 73 65 64 2c 20 74 68 65 6e 20 74 d reused, then t
1538e 68 65 20 6f 72 69 67 69 6e 61 6c 20 64 61 74 61 he original data
1538f 0a 2a 2a 20 6d 61 79 20 62 65 20 6c 6f 73 74 2e .** may be lost.
15390 20 49 6e 20 74 68 65 20 65 76 65 6e 74 20 6f 66 In the event of
15391 20 61 20 72 6f 6c 6c 62 61 63 6b 2c 20 69 74 20 a rollback, it
15392 6d 61 79 20 6e 6f 74 20 62 65 20 70 6f 73 73 69 may not be possi
15393 62 6c 65 0a 2a 2a 20 74 6f 20 72 65 73 74 6f 72 ble.** to restor
15394 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74 e the database t
15395 6f 20 69 74 73 20 6f 72 69 67 69 6e 61 6c 20 63 o its original c
15396 6f 6e 66 69 67 75 72 61 74 69 6f 6e 2e 0a 2a 2a onfiguration..**
15397 0a 2a 2a 20 54 68 65 20 73 6f 6c 75 74 69 6f 6e .** The solution
15398 20 69 73 20 74 68 65 20 42 74 53 68 61 72 65 64 is the BtShared
15399 2e 70 48 61 73 43 6f 6e 74 65 6e 74 20 62 69 74 .pHasContent bit
1539a 76 65 63 2e 20 57 68 65 6e 65 76 65 72 20 61 20 vec. Whenever a
1539b 70 61 67 65 20 69 73 20 0a 2a 2a 20 6d 6f 76 65 page is .** move
1539c 64 20 74 6f 20 62 65 63 6f 6d 65 20 61 20 66 72 d to become a fr
1539d 65 65 2d 6c 69 73 74 20 6c 65 61 66 20 70 61 67 ee-list leaf pag
1539e 65 2c 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e e, the correspon
1539f 64 69 6e 67 20 62 69 74 20 69 73 0a 2a 2a 20 73 ding bit is.** s
153a0 65 74 20 69 6e 20 74 68 65 20 62 69 74 76 65 63 et in the bitvec
153a1 2e 20 57 68 65 6e 65 76 65 72 20 61 20 6c 65 61 . Whenever a lea
153a2 66 20 70 61 67 65 20 69 73 20 65 78 74 72 61 63 f page is extrac
153a3 74 65 64 20 66 72 6f 6d 20 74 68 65 20 66 72 65 ted from the fre
153a4 65 2d 6c 69 73 74 2c 0a 2a 2a 20 6f 70 74 69 6d e-list,.** optim
153a5 69 7a 61 74 69 6f 6e 20 32 20 61 62 6f 76 65 20 ization 2 above
153a6 69 73 20 6f 6d 6d 69 74 74 65 64 20 69 66 20 74 is ommitted if t
153a7 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 he corresponding
153a8 20 62 69 74 20 69 73 20 61 6c 72 65 61 64 79 0a bit is already.
153a9 2a 2a 20 73 65 74 20 69 6e 20 42 74 53 68 61 72 ** set in BtShar
153aa 65 64 2e 70 48 61 73 43 6f 6e 74 65 6e 74 2e 20 ed.pHasContent.
153ab 54 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 The contents of
153ac 74 68 65 20 62 69 74 76 65 63 20 61 72 65 20 63 the bitvec are c
153ad 6c 65 61 72 65 64 0a 2a 2a 20 61 74 20 74 68 65 leared.** at the
153ae 20 65 6e 64 20 6f 66 20 65 76 65 72 79 20 74 72 end of every tr
153af 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 ansaction..*/.st
153b0 61 74 69 63 20 69 6e 74 20 62 74 72 65 65 53 65 atic int btreeSe
153b1 74 48 61 73 43 6f 6e 74 65 6e 74 28 42 74 53 68 tHasContent(BtSh
153b2 61 72 65 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20 ared *pBt, Pgno
153b3 70 67 6e 6f 29 7b 0a 20 20 69 6e 74 20 72 63 20 pgno){. int rc
153b4 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 = SQLITE_OK;. i
153b5 66 28 20 21 70 42 74 2d 3e 70 48 61 73 43 6f 6e f( !pBt->pHasCon
153b6 74 65 6e 74 20 29 7b 0a 20 20 20 20 69 6e 74 20 tent ){. int
153b7 6e 50 61 67 65 20 3d 20 31 30 30 3b 0a 20 20 20 nPage = 100;.
153b8 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 sqlite3PagerPag
153b9 65 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 ecount(pBt->pPag
153ba 65 72 2c 20 26 6e 50 61 67 65 29 3b 0a 20 20 20 er, &nPage);.
153bb 20 2f 2a 20 49 66 20 73 71 6c 69 74 65 33 50 61 /* If sqlite3Pa
153bc 67 65 72 50 61 67 65 63 6f 75 6e 74 28 29 20 66 gerPagecount() f
153bd 61 69 6c 73 20 74 68 65 72 65 20 69 73 20 6e 6f ails there is no
153be 20 68 61 72 6d 20 62 65 63 61 75 73 65 20 74 68 harm because th
153bf 65 0a 20 20 20 20 2a 2a 20 6e 50 61 67 65 20 76 e. ** nPage v
153c0 61 72 69 61 62 6c 65 20 69 73 20 75 6e 63 68 61 ariable is uncha
153c1 6e 67 65 64 20 66 72 6f 6d 20 69 74 73 20 64 65 nged from its de
153c2 66 61 75 6c 74 20 76 61 6c 75 65 20 6f 66 20 31 fault value of 1
153c3 30 30 20 2a 2f 0a 20 20 20 20 70 42 74 2d 3e 70 00 */. pBt->p
153c4 48 61 73 43 6f 6e 74 65 6e 74 20 3d 20 73 71 6c HasContent = sql
153c5 69 74 65 33 42 69 74 76 65 63 43 72 65 61 74 65 ite3BitvecCreate
153c6 28 28 75 33 32 29 6e 50 61 67 65 29 3b 0a 20 20 ((u32)nPage);.
153c7 20 20 69 66 28 20 21 70 42 74 2d 3e 70 48 61 73 if( !pBt->pHas
153c8 43 6f 6e 74 65 6e 74 20 29 7b 0a 20 20 20 20 20 Content ){.
153c9 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d rc = SQLITE_NOM
153ca 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 EM;. }. }.
153cb 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
153cc 4b 20 26 26 20 70 67 6e 6f 3c 3d 73 71 6c 69 74 K && pgno<=sqlit
153cd 65 33 42 69 74 76 65 63 53 69 7a 65 28 70 42 74 e3BitvecSize(pBt
153ce 2d 3e 70 48 61 73 43 6f 6e 74 65 6e 74 29 20 29 ->pHasContent) )
153cf 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 {. rc = sqlit
153d0 65 33 42 69 74 76 65 63 53 65 74 28 70 42 74 2d e3BitvecSet(pBt-
153d1 3e 70 48 61 73 43 6f 6e 74 65 6e 74 2c 20 70 67 >pHasContent, pg
153d2 6e 6f 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 no);. }. retur
153d3 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51 n rc;.}../*.** Q
153d4 75 65 72 79 20 74 68 65 20 42 74 53 68 61 72 65 uery the BtShare
153d5 64 2e 70 48 61 73 43 6f 6e 74 65 6e 74 20 76 65 d.pHasContent ve
153d6 63 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 ctor..**.** This
153d7 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c function is cal
153d8 6c 65 64 20 77 68 65 6e 20 61 20 66 72 65 65 2d led when a free-
153d9 6c 69 73 74 20 6c 65 61 66 20 70 61 67 65 20 69 list leaf page i
153da 73 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20 74 s removed from t
153db 68 65 0a 2a 2a 20 66 72 65 65 2d 6c 69 73 74 20 he.** free-list
153dc 66 6f 72 20 72 65 75 73 65 2e 20 49 74 20 72 65 for reuse. It re
153dd 74 75 72 6e 73 20 66 61 6c 73 65 20 69 66 20 69 turns false if i
153de 74 20 69 73 20 73 61 66 65 20 74 6f 20 72 65 74 t is safe to ret
153df 72 69 65 76 65 20 74 68 65 0a 2a 2a 20 70 61 67 rieve the.** pag
153e0 65 20 66 72 6f 6d 20 74 68 65 20 70 61 67 65 72 e from the pager
153e1 20 6c 61 79 65 72 20 77 69 74 68 20 74 68 65 20 layer with the
153e2 27 6e 6f 2d 63 6f 6e 74 65 6e 74 27 20 66 6c 61 'no-content' fla
153e3 67 20 73 65 74 2e 20 54 72 75 65 20 6f 74 68 65 g set. True othe
153e4 72 77 69 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 rwise..*/.static
153e5 20 69 6e 74 20 62 74 72 65 65 47 65 74 48 61 73 int btreeGetHas
153e6 43 6f 6e 74 65 6e 74 28 42 74 53 68 61 72 65 64 Content(BtShared
153e7 20 2a 70 42 74 2c 20 50 67 6e 6f 20 70 67 6e 6f *pBt, Pgno pgno
153e8 29 7b 0a 20 20 42 69 74 76 65 63 20 2a 70 20 3d ){. Bitvec *p =
153e9 20 70 42 74 2d 3e 70 48 61 73 43 6f 6e 74 65 6e pBt->pHasConten
153ea 74 3b 0a 20 20 72 65 74 75 72 6e 20 28 70 20 26 t;. return (p &
153eb 26 20 28 70 67 6e 6f 3e 73 71 6c 69 74 65 33 42 & (pgno>sqlite3B
153ec 69 74 76 65 63 53 69 7a 65 28 70 29 20 7c 7c 20 itvecSize(p) ||
153ed 73 71 6c 69 74 65 33 42 69 74 76 65 63 54 65 73 sqlite3BitvecTes
153ee 74 28 70 2c 20 70 67 6e 6f 29 29 29 3b 0a 7d 0a t(p, pgno)));.}.
153ef 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 28 64 65 ./*.** Clear (de
153f0 73 74 72 6f 79 29 20 74 68 65 20 42 74 53 68 61 stroy) the BtSha
153f1 72 65 64 2e 70 48 61 73 43 6f 6e 74 65 6e 74 20 red.pHasContent
153f2 62 69 74 76 65 63 2e 20 54 68 69 73 20 73 68 6f bitvec. This sho
153f3 75 6c 64 20 62 65 0a 2a 2a 20 69 6e 76 6f 6b 65 uld be.** invoke
153f4 64 20 61 74 20 74 68 65 20 63 6f 6e 63 6c 75 73 d at the conclus
153f5 69 6f 6e 20 6f 66 20 65 61 63 68 20 77 72 69 74 ion of each writ
153f6 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a e-transaction..*
153f7 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 62 74 /.static void bt
153f8 72 65 65 43 6c 65 61 72 48 61 73 43 6f 6e 74 65 reeClearHasConte
153f9 6e 74 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 nt(BtShared *pBt
153fa 29 7b 0a 20 20 73 71 6c 69 74 65 33 42 69 74 76 ){. sqlite3Bitv
153fb 65 63 44 65 73 74 72 6f 79 28 70 42 74 2d 3e 70 ecDestroy(pBt->p
153fc 48 61 73 43 6f 6e 74 65 6e 74 29 3b 0a 20 20 70 HasContent);. p
153fd 42 74 2d 3e 70 48 61 73 43 6f 6e 74 65 6e 74 20 Bt->pHasContent
153fe 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 61 = 0;.}../*.** Sa
153ff 76 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 63 ve the current c
15400 75 72 73 6f 72 20 70 6f 73 69 74 69 6f 6e 20 69 ursor position i
15401 6e 20 74 68 65 20 76 61 72 69 61 62 6c 65 73 20 n the variables
15402 42 74 43 75 72 73 6f 72 2e 6e 4b 65 79 20 0a 2a BtCursor.nKey .*
15403 2a 20 61 6e 64 20 42 74 43 75 72 73 6f 72 2e 70 * and BtCursor.p
15404 4b 65 79 2e 20 54 68 65 20 63 75 72 73 6f 72 27 Key. The cursor'
15405 73 20 73 74 61 74 65 20 69 73 20 73 65 74 20 74 s state is set t
15406 6f 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 o CURSOR_REQUIRE
15407 53 45 45 4b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 SEEK..**.** The
15408 63 61 6c 6c 65 72 20 6d 75 73 74 20 65 6e 73 75 caller must ensu
15409 72 65 20 74 68 61 74 20 74 68 65 20 63 75 72 73 re that the curs
1540a 6f 72 20 69 73 20 76 61 6c 69 64 20 28 68 61 73 or is valid (has
1540b 20 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f eState==CURSOR_
1540c 56 41 4c 49 44 29 0a 2a 2a 20 70 72 69 6f 72 20 VALID).** prior
1540d 74 6f 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 to calling this
1540e 72 6f 75 74 69 6e 65 2e 20 20 0a 2a 2f 0a 73 74 routine. .*/.st
1540f 61 74 69 63 20 69 6e 74 20 73 61 76 65 43 75 72 atic int saveCur
15410 73 6f 72 50 6f 73 69 74 69 6f 6e 28 42 74 43 75 sorPosition(BtCu
15411 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 69 rsor *pCur){. i
15412 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 nt rc;.. assert
15413 28 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 3d 3d ( CURSOR_VALID==
15414 70 43 75 72 2d 3e 65 53 74 61 74 65 20 29 3b 0a pCur->eState );.
15415 20 20 61 73 73 65 72 74 28 20 30 3d 3d 70 43 75 assert( 0==pCu
15416 72 2d 3e 70 4b 65 79 20 29 3b 0a 20 20 61 73 73 r->pKey );. ass
15417 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 ert( cursorHolds
15418 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 0a Mutex(pCur) );..
15419 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 rc = sqlite3Bt
1541a 72 65 65 4b 65 79 53 69 7a 65 28 70 43 75 72 2c reeKeySize(pCur,
1541b 20 26 70 43 75 72 2d 3e 6e 4b 65 79 29 3b 0a 20 &pCur->nKey);.
1541c 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c assert( rc==SQL
1541d 49 54 45 5f 4f 4b 20 29 3b 20 20 2f 2a 20 4b 65 ITE_OK ); /* Ke
1541e 79 53 69 7a 65 28 29 20 63 61 6e 6e 6f 74 20 66 ySize() cannot f
1541f 61 69 6c 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 ail */.. /* If
15420 74 68 69 73 20 69 73 20 61 6e 20 69 6e 74 4b 65 this is an intKe
15421 79 20 74 61 62 6c 65 2c 20 74 68 65 6e 20 74 68 y table, then th
15422 65 20 61 62 6f 76 65 20 63 61 6c 6c 20 74 6f 20 e above call to
15423 42 74 72 65 65 4b 65 79 53 69 7a 65 28 29 0a 20 BtreeKeySize().
15424 20 2a 2a 20 73 74 6f 72 65 73 20 74 68 65 20 69 ** stores the i
15425 6e 74 65 67 65 72 20 6b 65 79 20 69 6e 20 70 43 nteger key in pC
15426 75 72 2d 3e 6e 4b 65 79 2e 20 49 6e 20 74 68 69 ur->nKey. In thi
15427 73 20 63 61 73 65 20 74 68 69 73 20 76 61 6c 75 s case this valu
15428 65 20 69 73 0a 20 20 2a 2a 20 61 6c 6c 20 74 68 e is. ** all th
15429 61 74 20 69 73 20 72 65 71 75 69 72 65 64 2e 20 at is required.
1542a 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 70 43 Otherwise, if pC
1542b 75 72 20 69 73 20 6e 6f 74 20 6f 70 65 6e 20 6f ur is not open o
1542c 6e 20 61 6e 20 69 6e 74 4b 65 79 0a 20 20 2a 2a n an intKey. **
1542d 20 74 61 62 6c 65 2c 20 74 68 65 6e 20 6d 61 6c table, then mal
1542e 6c 6f 63 20 73 70 61 63 65 20 66 6f 72 20 61 6e loc space for an
1542f 64 20 73 74 6f 72 65 20 74 68 65 20 70 43 75 72 d store the pCur
15430 2d 3e 6e 4b 65 79 20 62 79 74 65 73 20 6f 66 20 ->nKey bytes of
15431 6b 65 79 20 0a 20 20 2a 2a 20 64 61 74 61 2e 0a key . ** data..
15432 20 20 2a 2f 0a 20 20 69 66 28 20 30 3d 3d 70 43 */. if( 0==pC
15433 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e 69 ur->apPage[0]->i
15434 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 76 6f 69 ntKey ){. voi
15435 64 20 2a 70 4b 65 79 20 3d 20 73 71 6c 69 74 65 d *pKey = sqlite
15436 33 4d 61 6c 6c 6f 63 28 20 28 69 6e 74 29 70 43 3Malloc( (int)pC
15437 75 72 2d 3e 6e 4b 65 79 20 29 3b 0a 20 20 20 20 ur->nKey );.
15438 69 66 28 20 70 4b 65 79 20 29 7b 0a 20 20 20 20 if( pKey ){.
15439 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 rc = sqlite3Bt
1543a 72 65 65 4b 65 79 28 70 43 75 72 2c 20 30 2c 20 reeKey(pCur, 0,
1543b 28 69 6e 74 29 70 43 75 72 2d 3e 6e 4b 65 79 2c (int)pCur->nKey,
1543c 20 70 4b 65 79 29 3b 0a 20 20 20 20 20 20 69 66 pKey);. if
1543d 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
1543e 29 7b 0a 20 20 20 20 20 20 20 20 70 43 75 72 2d ){. pCur-
1543f 3e 70 4b 65 79 20 3d 20 70 4b 65 79 3b 0a 20 20 >pKey = pKey;.
15440 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
15441 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 sqlite3_free(
15442 70 4b 65 79 29 3b 0a 20 20 20 20 20 20 7d 0a 20 pKey);. }.
15443 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
15444 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 rc = SQLITE_NOME
15445 4d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61 M;. }. }. a
15446 73 73 65 72 74 28 20 21 70 43 75 72 2d 3e 61 70 ssert( !pCur->ap
15447 50 61 67 65 5b 30 5d 2d 3e 69 6e 74 4b 65 79 20 Page[0]->intKey
15448 7c 7c 20 21 70 43 75 72 2d 3e 70 4b 65 79 20 29 || !pCur->pKey )
15449 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c ;.. if( rc==SQL
1544a 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 6e ITE_OK ){. in
1544b 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 t i;. for(i=0
1544c 3b 20 69 3c 3d 70 43 75 72 2d 3e 69 50 61 67 65 ; i<=pCur->iPage
1544d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 72 65 ; i++){. re
1544e 6c 65 61 73 65 50 61 67 65 28 70 43 75 72 2d 3e leasePage(pCur->
1544f 61 70 50 61 67 65 5b 69 5d 29 3b 0a 20 20 20 20 apPage[i]);.
15450 20 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 pCur->apPage[i
15451 5d 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 ] = 0;. }.
15452 20 70 43 75 72 2d 3e 69 50 61 67 65 20 3d 20 2d pCur->iPage = -
15453 31 3b 0a 20 20 20 20 70 43 75 72 2d 3e 65 53 74 1;. pCur->eSt
15454 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 52 45 51 ate = CURSOR_REQ
15455 55 49 52 45 53 45 45 4b 3b 0a 20 20 7d 0a 0a 20 UIRESEEK;. }..
15456 20 69 6e 76 61 6c 69 64 61 74 65 4f 76 65 72 66 invalidateOverf
15457 6c 6f 77 43 61 63 68 65 28 70 43 75 72 29 3b 0a lowCache(pCur);.
15458 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
15459 2f 2a 0a 2a 2a 20 53 61 76 65 20 74 68 65 20 70 /*.** Save the p
1545a 6f 73 69 74 69 6f 6e 73 20 6f 66 20 61 6c 6c 20 ositions of all
1545b 63 75 72 73 6f 72 73 20 65 78 63 65 70 74 20 70 cursors except p
1545c 45 78 63 65 70 74 20 6f 70 65 6e 20 6f 6e 20 74 Except open on t
1545d 68 65 20 74 61 62 6c 65 20 0a 2a 2a 20 77 69 74 he table .** wit
1545e 68 20 72 6f 6f 74 2d 70 61 67 65 20 69 52 6f 6f h root-page iRoo
1545f 74 2e 20 55 73 75 61 6c 6c 79 2c 20 74 68 69 73 t. Usually, this
15460 20 69 73 20 63 61 6c 6c 65 64 20 6a 75 73 74 20 is called just
15461 62 65 66 6f 72 65 20 63 75 72 73 6f 72 0a 2a 2a before cursor.**
15462 20 70 45 78 63 65 70 74 20 69 73 20 75 73 65 64 pExcept is used
15463 20 74 6f 20 6d 6f 64 69 66 79 20 74 68 65 20 74 to modify the t
15464 61 62 6c 65 20 28 42 74 72 65 65 44 65 6c 65 74 able (BtreeDelet
15465 65 28 29 20 6f 72 20 42 74 72 65 65 49 6e 73 65 e() or BtreeInse
15466 72 74 28 29 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 rt())..*/.static
15467 20 69 6e 74 20 73 61 76 65 41 6c 6c 43 75 72 73 int saveAllCurs
15468 6f 72 73 28 42 74 53 68 61 72 65 64 20 2a 70 42 ors(BtShared *pB
15469 74 2c 20 50 67 6e 6f 20 69 52 6f 6f 74 2c 20 42 t, Pgno iRoot, B
1546a 74 43 75 72 73 6f 72 20 2a 70 45 78 63 65 70 74 tCursor *pExcept
1546b 29 7b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 ){. BtCursor *p
1546c 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
1546d 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
1546e 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 Bt->mutex) );.
1546f 61 73 73 65 72 74 28 20 70 45 78 63 65 70 74 3d assert( pExcept=
15470 3d 30 20 7c 7c 20 70 45 78 63 65 70 74 2d 3e 70 =0 || pExcept->p
15471 42 74 3d 3d 70 42 74 20 29 3b 0a 20 20 66 6f 72 Bt==pBt );. for
15472 28 70 3d 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b (p=pBt->pCursor;
15473 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b p; p=p->pNext){
15474 0a 20 20 20 20 69 66 28 20 70 21 3d 70 45 78 63 . if( p!=pExc
15475 65 70 74 20 26 26 20 28 30 3d 3d 69 52 6f 6f 74 ept && (0==iRoot
15476 20 7c 7c 20 70 2d 3e 70 67 6e 6f 52 6f 6f 74 3d || p->pgnoRoot=
15477 3d 69 52 6f 6f 74 29 20 26 26 20 0a 20 20 20 20 =iRoot) && .
15478 20 20 20 20 70 2d 3e 65 53 74 61 74 65 3d 3d 43 p->eState==C
15479 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 7b 0a 20 URSOR_VALID ){.
1547a 20 20 20 20 20 69 6e 74 20 72 63 20 3d 20 73 61 int rc = sa
1547b 76 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e veCursorPosition
1547c 28 70 29 3b 0a 20 20 20 20 20 20 69 66 28 20 53 (p);. if( S
1547d 51 4c 49 54 45 5f 4f 4b 21 3d 72 63 20 29 7b 0a QLITE_OK!=rc ){.
1547e 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 return r
1547f 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d c;. }. }
15480 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 . }. return SQ
15481 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a LITE_OK;.}../*.*
15482 2a 20 43 6c 65 61 72 20 74 68 65 20 63 75 72 72 * Clear the curr
15483 65 6e 74 20 63 75 72 73 6f 72 20 70 6f 73 69 74 ent cursor posit
15484 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ion..*/.SQLITE_P
15485 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
15486 74 65 33 42 74 72 65 65 43 6c 65 61 72 43 75 72 te3BtreeClearCur
15487 73 6f 72 28 42 74 43 75 72 73 6f 72 20 2a 70 43 sor(BtCursor *pC
15488 75 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 63 ur){. assert( c
15489 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 ursorHoldsMutex(
1548a 70 43 75 72 29 20 29 3b 0a 20 20 73 71 6c 69 74 pCur) );. sqlit
1548b 65 33 5f 66 72 65 65 28 70 43 75 72 2d 3e 70 4b e3_free(pCur->pK
1548c 65 79 29 3b 0a 20 20 70 43 75 72 2d 3e 70 4b 65 ey);. pCur->pKe
1548d 79 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e 65 y = 0;. pCur->e
1548e 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 49 State = CURSOR_I
1548f 4e 56 41 4c 49 44 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a NVALID;.}../*.**
15490 20 49 6e 20 74 68 69 73 20 76 65 72 73 69 6f 6e In this version
15491 20 6f 66 20 42 74 72 65 65 4d 6f 76 65 74 6f 2c of BtreeMoveto,
15492 20 70 4b 65 79 20 69 73 20 61 20 70 61 63 6b 65 pKey is a packe
15493 64 20 69 6e 64 65 78 20 72 65 63 6f 72 64 0a 2a d index record.*
15494 2a 20 73 75 63 68 20 61 73 20 69 73 20 67 65 6e * such as is gen
15495 65 72 61 74 65 64 20 62 79 20 74 68 65 20 4f 50 erated by the OP
15496 5f 4d 61 6b 65 52 65 63 6f 72 64 20 6f 70 63 6f _MakeRecord opco
15497 64 65 2e 20 20 55 6e 70 61 63 6b 20 74 68 65 0a de. Unpack the.
15498 2a 2a 20 72 65 63 6f 72 64 20 61 6e 64 20 74 68 ** record and th
15499 65 6e 20 63 61 6c 6c 20 42 74 72 65 65 4d 6f 76 en call BtreeMov
1549a 65 74 6f 55 6e 70 61 63 6b 65 64 28 29 20 74 6f etoUnpacked() to
1549b 20 64 6f 20 74 68 65 20 77 6f 72 6b 2e 0a 2a 2f do the work..*/
1549c 0a 73 74 61 74 69 63 20 69 6e 74 20 62 74 72 65 .static int btre
1549d 65 4d 6f 76 65 74 6f 28 0a 20 20 42 74 43 75 72 eMoveto(. BtCur
1549e 73 6f 72 20 2a 70 43 75 72 2c 20 20 20 20 20 2f sor *pCur, /
1549f 2a 20 43 75 72 73 6f 72 20 6f 70 65 6e 20 6f 6e * Cursor open on
154a0 20 74 68 65 20 62 74 72 65 65 20 74 6f 20 62 65 the btree to be
154a1 20 73 65 61 72 63 68 65 64 20 2a 2f 0a 20 20 63 searched */. c
154a2 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c onst void *pKey,
154a3 20 20 20 2f 2a 20 50 61 63 6b 65 64 20 6b 65 79 /* Packed key
154a4 20 69 66 20 74 68 65 20 62 74 72 65 65 20 69 73 if the btree is
154a5 20 61 6e 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 an index */. i
154a6 36 34 20 6e 4b 65 79 2c 20 20 20 20 20 20 20 20 64 nKey,
154a7 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 6b 65 /* Integer ke
154a8 79 20 66 6f 72 20 74 61 62 6c 65 73 2e 20 20 53 y for tables. S
154a9 69 7a 65 20 6f 66 20 70 4b 65 79 20 66 6f 72 20 ize of pKey for
154aa 69 6e 64 69 63 65 73 20 2a 2f 0a 20 20 69 6e 74 indices */. int
154ab 20 62 69 61 73 2c 20 20 20 20 20 20 20 20 20 20 bias,
154ac 20 2f 2a 20 42 69 61 73 20 73 65 61 72 63 68 20 /* Bias search
154ad 74 6f 20 74 68 65 20 68 69 67 68 20 65 6e 64 20 to the high end
154ae 2a 2f 0a 20 20 69 6e 74 20 2a 70 52 65 73 20 20 */. int *pRes
154af 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 /* Writ
154b0 65 20 73 65 61 72 63 68 20 72 65 73 75 6c 74 73 e search results
154b1 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e here */.){. in
154b2 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 t rc;
154b3 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61 74 /* Stat
154b4 75 73 20 63 6f 64 65 20 2a 2f 0a 20 20 55 6e 70 us code */. Unp
154b5 61 63 6b 65 64 52 65 63 6f 72 64 20 2a 70 49 64 ackedRecord *pId
154b6 78 4b 65 79 3b 20 20 20 2f 2a 20 55 6e 70 61 63 xKey; /* Unpac
154b7 6b 65 64 20 69 6e 64 65 78 20 6b 65 79 20 2a 2f ked index key */
154b8 0a 20 20 63 68 61 72 20 61 53 70 61 63 65 5b 31 . char aSpace[1
154b9 35 30 5d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 50]; /*
154ba 20 54 65 6d 70 20 73 70 61 63 65 20 66 6f 72 20 Temp space for
154bb 70 49 64 78 4b 65 79 20 2d 20 74 6f 20 61 76 6f pIdxKey - to avo
154bc 69 64 20 61 20 6d 61 6c 6c 6f 63 20 2a 2f 0a 0a id a malloc */..
154bd 20 20 69 66 28 20 70 4b 65 79 20 29 7b 0a 20 20 if( pKey ){.
154be 20 20 61 73 73 65 72 74 28 20 6e 4b 65 79 3d 3d assert( nKey==
154bf 28 69 36 34 29 28 69 6e 74 29 6e 4b 65 79 20 29 (i64)(int)nKey )
154c0 3b 0a 20 20 20 20 70 49 64 78 4b 65 79 20 3d 20 ;. pIdxKey =
154c1 73 71 6c 69 74 65 33 56 64 62 65 52 65 63 6f 72 sqlite3VdbeRecor
154c2 64 55 6e 70 61 63 6b 28 70 43 75 72 2d 3e 70 4b dUnpack(pCur->pK
154c3 65 79 49 6e 66 6f 2c 20 28 69 6e 74 29 6e 4b 65 eyInfo, (int)nKe
154c4 79 2c 20 70 4b 65 79 2c 0a 20 20 20 20 20 20 20 y, pKey,.
154c5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
154c6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61 a
154c7 53 70 61 63 65 2c 20 73 69 7a 65 6f 66 28 61 53 Space, sizeof(aS
154c8 70 61 63 65 29 29 3b 0a 20 20 20 20 69 66 28 20 pace));. if(
154c9 70 49 64 78 4b 65 79 3d 3d 30 20 29 20 72 65 74 pIdxKey==0 ) ret
154ca 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d urn SQLITE_NOMEM
154cb 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 ;. }else{. p
154cc 49 64 78 4b 65 79 20 3d 20 30 3b 0a 20 20 7d 0a IdxKey = 0;. }.
154cd 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 rc = sqlite3Bt
154ce 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 reeMovetoUnpacke
154cf 64 28 70 43 75 72 2c 20 70 49 64 78 4b 65 79 2c d(pCur, pIdxKey,
154d0 20 6e 4b 65 79 2c 20 62 69 61 73 2c 20 70 52 65 nKey, bias, pRe
154d1 73 29 3b 0a 20 20 69 66 28 20 70 4b 65 79 20 29 s);. if( pKey )
154d2 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 {. sqlite3Vdb
154d3 65 44 65 6c 65 74 65 55 6e 70 61 63 6b 65 64 52 eDeleteUnpackedR
154d4 65 63 6f 72 64 28 70 49 64 78 4b 65 79 29 3b 0a ecord(pIdxKey);.
154d5 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
154d6 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 74 6f 72 .}../*.** Restor
154d7 65 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20 e the cursor to
154d8 74 68 65 20 70 6f 73 69 74 69 6f 6e 20 69 74 20 the position it
154d9 77 61 73 20 69 6e 20 28 6f 72 20 61 73 20 63 6c was in (or as cl
154da 6f 73 65 20 74 6f 20 61 73 20 70 6f 73 73 69 62 ose to as possib
154db 6c 65 29 0a 2a 2a 20 77 68 65 6e 20 73 61 76 65 le).** when save
154dc 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 CursorPosition()
154dd 20 77 61 73 20 63 61 6c 6c 65 64 2e 20 4e 6f 74 was called. Not
154de 65 20 74 68 61 74 20 74 68 69 73 20 63 61 6c 6c e that this call
154df 20 64 65 6c 65 74 65 73 20 74 68 65 20 0a 2a 2a deletes the .**
154e0 20 73 61 76 65 64 20 70 6f 73 69 74 69 6f 6e 20 saved position
154e1 69 6e 66 6f 20 73 74 6f 72 65 64 20 62 79 20 73 info stored by s
154e2 61 76 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f aveCursorPositio
154e3 6e 28 29 2c 20 73 6f 20 74 68 65 72 65 20 63 61 n(), so there ca
154e4 6e 20 62 65 0a 2a 2a 20 61 74 20 6d 6f 73 74 20 n be.** at most
154e5 6f 6e 65 20 65 66 66 65 63 74 69 76 65 20 72 65 one effective re
154e6 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 storeCursorPosit
154e7 69 6f 6e 28 29 20 63 61 6c 6c 20 61 66 74 65 72 ion() call after
154e8 20 65 61 63 68 20 0a 2a 2a 20 73 61 76 65 43 75 each .** saveCu
154e9 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 2e 0a rsorPosition()..
154ea 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 74 */.static int bt
154eb 72 65 65 52 65 73 74 6f 72 65 43 75 72 73 6f 72 reeRestoreCursor
154ec 50 6f 73 69 74 69 6f 6e 28 42 74 43 75 72 73 6f Position(BtCurso
154ed 72 20 2a 70 43 75 72 29 7b 0a 20 20 69 6e 74 20 r *pCur){. int
154ee 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20 63 75 rc;. assert( cu
154ef 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 rsorHoldsMutex(p
154f0 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 Cur) );. assert
154f1 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3e 3d ( pCur->eState>=
154f2 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 CURSOR_REQUIRESE
154f3 45 4b 20 29 3b 0a 20 20 69 66 28 20 70 43 75 72 EK );. if( pCur
154f4 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 ->eState==CURSOR
154f5 5f 46 41 55 4c 54 20 29 7b 0a 20 20 20 20 72 65 _FAULT ){. re
154f6 74 75 72 6e 20 70 43 75 72 2d 3e 73 6b 69 70 4e turn pCur->skipN
154f7 65 78 74 3b 0a 20 20 7d 0a 20 20 70 43 75 72 2d ext;. }. pCur-
154f8 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52 >eState = CURSOR
154f9 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 72 63 20 3d _INVALID;. rc =
154fa 20 62 74 72 65 65 4d 6f 76 65 74 6f 28 70 43 75 btreeMoveto(pCu
154fb 72 2c 20 70 43 75 72 2d 3e 70 4b 65 79 2c 20 70 r, pCur->pKey, p
154fc 43 75 72 2d 3e 6e 4b 65 79 2c 20 30 2c 20 26 70 Cur->nKey, 0, &p
154fd 43 75 72 2d 3e 73 6b 69 70 4e 65 78 74 29 3b 0a Cur->skipNext);.
154fe 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
154ff 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 _OK ){. sqlit
15500 65 33 5f 66 72 65 65 28 70 43 75 72 2d 3e 70 4b e3_free(pCur->pK
15501 65 79 29 3b 0a 20 20 20 20 70 43 75 72 2d 3e 70 ey);. pCur->p
15502 4b 65 79 20 3d 20 30 3b 0a 20 20 20 20 61 73 73 Key = 0;. ass
15503 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 ert( pCur->eStat
15504 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 e==CURSOR_VALID
15505 7c 7c 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d || pCur->eState=
15506 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 =CURSOR_INVALID
15507 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
15508 72 63 3b 0a 7d 0a 0a 23 64 65 66 69 6e 65 20 72 rc;.}..#define r
15509 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 estoreCursorPosi
1550a 74 69 6f 6e 28 70 29 20 5c 0a 20 20 28 70 2d 3e tion(p) \. (p->
1550b 65 53 74 61 74 65 3e 3d 43 55 52 53 4f 52 5f 52 eState>=CURSOR_R
1550c 45 51 55 49 52 45 53 45 45 4b 20 3f 20 5c 0a 20 EQUIRESEEK ? \.
1550d 20 20 20 20 20 20 20 20 62 74 72 65 65 52 65 73 btreeRes
1550e 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 toreCursorPositi
1550f 6f 6e 28 70 29 20 3a 20 5c 0a 20 20 20 20 20 20 on(p) : \.
15510 20 20 20 53 51 4c 49 54 45 5f 4f 4b 29 0a 0a 2f SQLITE_OK)../
15511 2a 0a 2a 2a 20 44 65 74 65 72 6d 69 6e 65 20 77 *.** Determine w
15512 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 61 20 hether or not a
15513 63 75 72 73 6f 72 20 68 61 73 20 6d 6f 76 65 64 cursor has moved
15514 20 66 72 6f 6d 20 74 68 65 20 70 6f 73 69 74 69 from the positi
15515 6f 6e 20 69 74 0a 2a 2a 20 77 61 73 20 6c 61 73 on it.** was las
15516 74 20 70 6c 61 63 65 64 20 61 74 2e 20 20 43 75 t placed at. Cu
15517 72 73 6f 72 73 20 63 61 6e 20 6d 6f 76 65 20 77 rsors can move w
15518 68 65 6e 20 74 68 65 20 72 6f 77 20 74 68 65 79 hen the row they
15519 20 61 72 65 20 70 6f 69 6e 74 69 6e 67 0a 2a 2a are pointing.**
1551a 20 61 74 20 69 73 20 64 65 6c 65 74 65 64 20 6f at is deleted o
1551b 75 74 20 66 72 6f 6d 20 75 6e 64 65 72 20 74 68 ut from under th
1551c 65 6d 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 em..**.** This r
1551d 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 61 outine returns a
1551e 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66 20 n error code if
1551f 73 6f 6d 65 74 68 69 6e 67 20 67 6f 65 73 20 77 something goes w
15520 72 6f 6e 67 2e 20 20 54 68 65 0a 2a 2a 20 69 6e rong. The.** in
15521 74 65 67 65 72 20 2a 70 48 61 73 4d 6f 76 65 64 teger *pHasMoved
15522 20 69 73 20 73 65 74 20 74 6f 20 6f 6e 65 20 69 is set to one i
15523 66 20 74 68 65 20 63 75 72 73 6f 72 20 68 61 73 f the cursor has
15524 20 6d 6f 76 65 64 20 61 6e 64 20 30 20 69 66 20 moved and 0 if
15525 6e 6f 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 not..*/.SQLITE_P
15526 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
15527 65 33 42 74 72 65 65 43 75 72 73 6f 72 48 61 73 e3BtreeCursorHas
15528 4d 6f 76 65 64 28 42 74 43 75 72 73 6f 72 20 2a Moved(BtCursor *
15529 70 43 75 72 2c 20 69 6e 74 20 2a 70 48 61 73 4d pCur, int *pHasM
1552a 6f 76 65 64 29 7b 0a 20 20 69 6e 74 20 72 63 3b oved){. int rc;
1552b 0a 0a 20 20 72 63 20 3d 20 72 65 73 74 6f 72 65 .. rc = restore
1552c 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 CursorPosition(p
1552d 43 75 72 29 3b 0a 20 20 69 66 28 20 72 63 20 29 Cur);. if( rc )
1552e 7b 0a 20 20 20 20 2a 70 48 61 73 4d 6f 76 65 64 {. *pHasMoved
1552f 20 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e = 1;. return
15530 20 72 63 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 rc;. }. if( p
15531 43 75 72 2d 3e 65 53 74 61 74 65 21 3d 43 55 52 Cur->eState!=CUR
15532 53 4f 52 5f 56 41 4c 49 44 20 7c 7c 20 70 43 75 SOR_VALID || pCu
15533 72 2d 3e 73 6b 69 70 4e 65 78 74 21 3d 30 20 29 r->skipNext!=0 )
15534 7b 0a 20 20 20 20 2a 70 48 61 73 4d 6f 76 65 64 {. *pHasMoved
15535 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 = 1;. }else{.
15536 20 20 20 2a 70 48 61 73 4d 6f 76 65 64 20 3d 20 *pHasMoved =
15537 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 0;. }. return
15538 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 SQLITE_OK;.}..#i
15539 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
1553a 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 2f 2a 0a T_AUTOVACUUM./*.
1553b 2a 2a 20 47 69 76 65 6e 20 61 20 70 61 67 65 20 ** Given a page
1553c 6e 75 6d 62 65 72 20 6f 66 20 61 20 72 65 67 75 number of a regu
1553d 6c 61 72 20 64 61 74 61 62 61 73 65 20 70 61 67 lar database pag
1553e 65 2c 20 72 65 74 75 72 6e 20 74 68 65 20 70 61 e, return the pa
1553f 67 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 66 6f 72 ge.** number for
15540 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 the pointer-map
15541 20 70 61 67 65 20 74 68 61 74 20 63 6f 6e 74 61 page that conta
15542 69 6e 73 20 74 68 65 20 65 6e 74 72 79 20 66 6f ins the entry fo
15543 72 20 74 68 65 0a 2a 2a 20 69 6e 70 75 74 20 70 r the.** input p
15544 61 67 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 age number..*/.s
15545 74 61 74 69 63 20 50 67 6e 6f 20 70 74 72 6d 61 tatic Pgno ptrma
15546 70 50 61 67 65 6e 6f 28 42 74 53 68 61 72 65 64 pPageno(BtShared
15547 20 2a 70 42 74 2c 20 50 67 6e 6f 20 70 67 6e 6f *pBt, Pgno pgno
15548 29 7b 0a 20 20 69 6e 74 20 6e 50 61 67 65 73 50 ){. int nPagesP
15549 65 72 4d 61 70 50 61 67 65 3b 0a 20 20 50 67 6e erMapPage;. Pgn
1554a 6f 20 69 50 74 72 4d 61 70 2c 20 72 65 74 3b 0a o iPtrMap, ret;.
1554b 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
1554c 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 3_mutex_held(pBt
1554d 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 6e 50 ->mutex) );. nP
1554e 61 67 65 73 50 65 72 4d 61 70 50 61 67 65 20 3d agesPerMapPage =
1554f 20 28 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a (pBt->usableSiz
15550 65 2f 35 29 2b 31 3b 0a 20 20 69 50 74 72 4d 61 e/5)+1;. iPtrMa
15551 70 20 3d 20 28 70 67 6e 6f 2d 32 29 2f 6e 50 61 p = (pgno-2)/nPa
15552 67 65 73 50 65 72 4d 61 70 50 61 67 65 3b 0a 20 gesPerMapPage;.
15553 20 72 65 74 20 3d 20 28 69 50 74 72 4d 61 70 2a ret = (iPtrMap*
15554 6e 50 61 67 65 73 50 65 72 4d 61 70 50 61 67 65 nPagesPerMapPage
15555 29 20 2b 20 32 3b 20 0a 20 20 69 66 28 20 72 65 ) + 2; . if( re
15556 74 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f t==PENDING_BYTE_
15557 50 41 47 45 28 70 42 74 29 20 29 7b 0a 20 20 20 PAGE(pBt) ){.
15558 20 72 65 74 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 ret++;. }. re
15559 74 75 72 6e 20 72 65 74 3b 0a 7d 0a 0a 2f 2a 0a turn ret;.}../*.
1555a 2a 2a 20 57 72 69 74 65 20 61 6e 20 65 6e 74 72 ** Write an entr
1555b 79 20 69 6e 74 6f 20 74 68 65 20 70 6f 69 6e 74 y into the point
1555c 65 72 20 6d 61 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 er map..**.** Th
1555d 69 73 20 72 6f 75 74 69 6e 65 20 75 70 64 61 74 is routine updat
1555e 65 73 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d es the pointer m
1555f 61 70 20 65 6e 74 72 79 20 66 6f 72 20 70 61 67 ap entry for pag
15560 65 20 6e 75 6d 62 65 72 20 27 6b 65 79 27 0a 2a e number 'key'.*
15561 2a 20 73 6f 20 74 68 61 74 20 69 74 20 6d 61 70 * so that it map
15562 73 20 74 6f 20 74 79 70 65 20 27 65 54 79 70 65 s to type 'eType
15563 27 20 61 6e 64 20 70 61 72 65 6e 74 20 70 61 67 ' and parent pag
15564 65 20 6e 75 6d 62 65 72 20 27 70 67 6e 6f 27 2e e number 'pgno'.
15565 0a 2a 2a 0a 2a 2a 20 49 66 20 2a 70 52 43 20 69 .**.** If *pRC i
15566 73 20 69 6e 69 74 69 61 6c 6c 79 20 6e 6f 6e 2d s initially non-
15567 7a 65 72 6f 20 28 6e 6f 6e 2d 53 51 4c 49 54 45 zero (non-SQLITE
15568 5f 4f 4b 29 20 74 68 65 6e 20 74 68 69 73 20 72 _OK) then this r
15569 6f 75 74 69 6e 65 20 69 73 0a 2a 2a 20 61 20 6e outine is.** a n
1556a 6f 2d 6f 70 2e 20 20 49 66 20 61 6e 20 65 72 72 o-op. If an err
1556b 6f 72 20 6f 63 63 75 72 73 2c 20 74 68 65 20 61 or occurs, the a
1556c 70 70 72 6f 70 72 69 61 74 65 20 65 72 72 6f 72 ppropriate error
1556d 20 63 6f 64 65 20 69 73 20 77 72 69 74 74 65 6e code is written
1556e 0a 2a 2a 20 69 6e 74 6f 20 2a 70 52 43 2e 0a 2a .** into *pRC..*
1556f 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 74 /.static void pt
15570 72 6d 61 70 50 75 74 28 42 74 53 68 61 72 65 64 rmapPut(BtShared
15571 20 2a 70 42 74 2c 20 50 67 6e 6f 20 6b 65 79 2c *pBt, Pgno key,
15572 20 75 38 20 65 54 79 70 65 2c 20 50 67 6e 6f 20 u8 eType, Pgno
15573 70 61 72 65 6e 74 2c 20 69 6e 74 20 2a 70 52 43 parent, int *pRC
15574 29 7b 0a 20 20 44 62 50 61 67 65 20 2a 70 44 62 ){. DbPage *pDb
15575 50 61 67 65 3b 20 20 2f 2a 20 54 68 65 20 70 6f Page; /* The po
15576 69 6e 74 65 72 20 6d 61 70 20 70 61 67 65 20 2a inter map page *
15577 2f 0a 20 20 75 38 20 2a 70 50 74 72 6d 61 70 3b /. u8 *pPtrmap;
15578 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 6f 69 /* The poi
15579 6e 74 65 72 20 6d 61 70 20 64 61 74 61 20 2a 2f nter map data */
1557a 0a 20 20 50 67 6e 6f 20 69 50 74 72 6d 61 70 3b . Pgno iPtrmap;
1557b 20 20 20 20 20 2f 2a 20 54 68 65 20 70 6f 69 6e /* The poin
1557c 74 65 72 20 6d 61 70 20 70 61 67 65 20 6e 75 6d ter map page num
1557d 62 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6f 66 66 ber */. int off
1557e 73 65 74 3b 20 20 20 20 20 20 20 2f 2a 20 4f 66 set; /* Of
1557f 66 73 65 74 20 69 6e 20 70 6f 69 6e 74 65 72 20 fset in pointer
15580 6d 61 70 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e map page */. in
15581 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 t rc;
15582 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 66 /* Return code f
15583 72 6f 6d 20 73 75 62 66 75 6e 63 74 69 6f 6e 73 rom subfunctions
15584 20 2a 2f 0a 0a 20 20 69 66 28 20 2a 70 52 43 20 */.. if( *pRC
15585 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 61 73 73 ) return;.. ass
15586 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
15587 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 ex_held(pBt->mut
15588 65 78 29 20 29 3b 0a 20 20 2f 2a 20 54 68 65 20 ex) );. /* The
15589 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 70 master-journal p
1558a 61 67 65 20 6e 75 6d 62 65 72 20 6d 75 73 74 20 age number must
1558b 6e 65 76 65 72 20 62 65 20 75 73 65 64 20 61 73 never be used as
1558c 20 61 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 70 a pointer map p
1558d 61 67 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 age */. assert(
1558e 20 30 3d 3d 50 54 52 4d 41 50 5f 49 53 50 41 47 0==PTRMAP_ISPAG
1558f 45 28 70 42 74 2c 20 50 45 4e 44 49 4e 47 5f 42 E(pBt, PENDING_B
15590 59 54 45 5f 50 41 47 45 28 70 42 74 29 29 20 29 YTE_PAGE(pBt)) )
15591 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 ;.. assert( pBt
15592 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 3b 0a ->autoVacuum );.
15593 20 20 69 66 28 20 6b 65 79 3d 3d 30 20 29 7b 0a if( key==0 ){.
15594 20 20 20 20 2a 70 52 43 20 3d 20 53 51 4c 49 54 *pRC = SQLIT
15595 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a E_CORRUPT_BKPT;.
15596 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a return;. }.
15597 20 20 69 50 74 72 6d 61 70 20 3d 20 50 54 52 4d iPtrmap = PTRM
15598 41 50 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20 6b AP_PAGENO(pBt, k
15599 65 79 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 ey);. rc = sqli
1559a 74 65 33 50 61 67 65 72 47 65 74 28 70 42 74 2d te3PagerGet(pBt-
1559b 3e 70 50 61 67 65 72 2c 20 69 50 74 72 6d 61 70 >pPager, iPtrmap
1559c 2c 20 26 70 44 62 50 61 67 65 29 3b 0a 20 20 69 , &pDbPage);. i
1559d 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
1559e 20 29 7b 0a 20 20 20 20 2a 70 52 43 20 3d 20 72 ){. *pRC = r
1559f 63 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 c;. return;.
155a0 20 7d 0a 20 20 6f 66 66 73 65 74 20 3d 20 50 54 }. offset = PT
155a1 52 4d 41 50 5f 50 54 52 4f 46 46 53 45 54 28 69 RMAP_PTROFFSET(i
155a2 50 74 72 6d 61 70 2c 20 6b 65 79 29 3b 0a 20 20 Ptrmap, key);.
155a3 69 66 28 20 6f 66 66 73 65 74 3c 30 20 29 7b 0a if( offset<0 ){.
155a4 20 20 20 20 2a 70 52 43 20 3d 20 53 51 4c 49 54 *pRC = SQLIT
155a5 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a E_CORRUPT_BKPT;.
155a6 20 20 20 20 67 6f 74 6f 20 70 74 72 6d 61 70 5f goto ptrmap_
155a7 65 78 69 74 3b 0a 20 20 7d 0a 20 20 70 50 74 72 exit;. }. pPtr
155a8 6d 61 70 20 3d 20 28 75 38 20 2a 29 73 71 6c 69 map = (u8 *)sqli
155a9 74 65 33 50 61 67 65 72 47 65 74 44 61 74 61 28 te3PagerGetData(
155aa 70 44 62 50 61 67 65 29 3b 0a 0a 20 20 69 66 28 pDbPage);.. if(
155ab 20 65 54 79 70 65 21 3d 70 50 74 72 6d 61 70 5b eType!=pPtrmap[
155ac 6f 66 66 73 65 74 5d 20 7c 7c 20 67 65 74 34 62 offset] || get4b
155ad 79 74 65 28 26 70 50 74 72 6d 61 70 5b 6f 66 66 yte(&pPtrmap[off
155ae 73 65 74 2b 31 5d 29 21 3d 70 61 72 65 6e 74 20 set+1])!=parent
155af 29 7b 0a 20 20 20 20 54 52 41 43 45 28 28 22 50 ){. TRACE(("P
155b0 54 52 4d 41 50 5f 55 50 44 41 54 45 3a 20 25 64 TRMAP_UPDATE: %d
155b1 2d 3e 28 25 64 2c 25 64 29 5c 6e 22 2c 20 6b 65 ->(%d,%d)\n", ke
155b2 79 2c 20 65 54 79 70 65 2c 20 70 61 72 65 6e 74 y, eType, parent
155b3 29 29 3b 0a 20 20 20 20 2a 70 52 43 3d 20 72 63 ));. *pRC= rc
155b4 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 = sqlite3PagerW
155b5 72 69 74 65 28 70 44 62 50 61 67 65 29 3b 0a 20 rite(pDbPage);.
155b6 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
155b7 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 50 E_OK ){. pP
155b8 74 72 6d 61 70 5b 6f 66 66 73 65 74 5d 20 3d 20 trmap[offset] =
155b9 65 54 79 70 65 3b 0a 20 20 20 20 20 20 70 75 74 eType;. put
155ba 34 62 79 74 65 28 26 70 50 74 72 6d 61 70 5b 6f 4byte(&pPtrmap[o
155bb 66 66 73 65 74 2b 31 5d 2c 20 70 61 72 65 6e 74 ffset+1], parent
155bc 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 70 74 );. }. }..pt
155bd 72 6d 61 70 5f 65 78 69 74 3a 0a 20 20 73 71 6c rmap_exit:. sql
155be 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 ite3PagerUnref(p
155bf 44 62 50 61 67 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a DbPage);.}../*.*
155c0 2a 20 52 65 61 64 20 61 6e 20 65 6e 74 72 79 20 * Read an entry
155c1 66 72 6f 6d 20 74 68 65 20 70 6f 69 6e 74 65 72 from the pointer
155c2 20 6d 61 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 map..**.** This
155c3 20 72 6f 75 74 69 6e 65 20 72 65 74 72 69 65 76 routine retriev
155c4 65 73 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d es the pointer m
155c5 61 70 20 65 6e 74 72 79 20 66 6f 72 20 70 61 67 ap entry for pag
155c6 65 20 27 6b 65 79 27 2c 20 77 72 69 74 69 6e 67 e 'key', writing
155c7 0a 2a 2a 20 74 68 65 20 74 79 70 65 20 61 6e 64 .** the type and
155c8 20 70 61 72 65 6e 74 20 70 61 67 65 20 6e 75 6d parent page num
155c9 62 65 72 20 74 6f 20 2a 70 45 54 79 70 65 20 61 ber to *pEType a
155ca 6e 64 20 2a 70 50 67 6e 6f 20 72 65 73 70 65 63 nd *pPgno respec
155cb 74 69 76 65 6c 79 2e 0a 2a 2a 20 41 6e 20 65 72 tively..** An er
155cc 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 ror code is retu
155cd 72 6e 65 64 20 69 66 20 73 6f 6d 65 74 68 69 6e rned if somethin
155ce 67 20 67 6f 65 73 20 77 72 6f 6e 67 2c 20 6f 74 g goes wrong, ot
155cf 68 65 72 77 69 73 65 20 53 51 4c 49 54 45 5f 4f herwise SQLITE_O
155d0 4b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 K..*/.static int
155d1 20 70 74 72 6d 61 70 47 65 74 28 42 74 53 68 61 ptrmapGet(BtSha
155d2 72 65 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20 6b red *pBt, Pgno k
155d3 65 79 2c 20 75 38 20 2a 70 45 54 79 70 65 2c 20 ey, u8 *pEType,
155d4 50 67 6e 6f 20 2a 70 50 67 6e 6f 29 7b 0a 20 20 Pgno *pPgno){.
155d5 44 62 50 61 67 65 20 2a 70 44 62 50 61 67 65 3b DbPage *pDbPage;
155d6 20 20 20 2f 2a 20 54 68 65 20 70 6f 69 6e 74 65 /* The pointe
155d7 72 20 6d 61 70 20 70 61 67 65 20 2a 2f 0a 20 20 r map page */.
155d8 69 6e 74 20 69 50 74 72 6d 61 70 3b 20 20 20 20 int iPtrmap;
155d9 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 6d 61 /* Pointer ma
155da 70 20 70 61 67 65 20 69 6e 64 65 78 20 2a 2f 0a p page index */.
155db 20 20 75 38 20 2a 70 50 74 72 6d 61 70 3b 20 20 u8 *pPtrmap;
155dc 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 /* Pointer
155dd 6d 61 70 20 70 61 67 65 20 64 61 74 61 20 2a 2f map page data */
155de 0a 20 20 69 6e 74 20 6f 66 66 73 65 74 3b 20 20 . int offset;
155df 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 /* Offset
155e0 6f 66 20 65 6e 74 72 79 20 69 6e 20 70 6f 69 6e of entry in poin
155e1 74 65 72 20 6d 61 70 20 2a 2f 0a 20 20 69 6e 74 ter map */. int
155e2 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 rc;.. assert(
155e3 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
155e4 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 ld(pBt->mutex) )
155e5 3b 0a 0a 20 20 69 50 74 72 6d 61 70 20 3d 20 50 ;.. iPtrmap = P
155e6 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70 42 74 TRMAP_PAGENO(pBt
155e7 2c 20 6b 65 79 29 3b 0a 20 20 72 63 20 3d 20 73 , key);. rc = s
155e8 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 70 qlite3PagerGet(p
155e9 42 74 2d 3e 70 50 61 67 65 72 2c 20 69 50 74 72 Bt->pPager, iPtr
155ea 6d 61 70 2c 20 26 70 44 62 50 61 67 65 29 3b 0a map, &pDbPage);.
155eb 20 20 69 66 28 20 72 63 21 3d 30 20 29 7b 0a 20 if( rc!=0 ){.
155ec 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
155ed 7d 0a 20 20 70 50 74 72 6d 61 70 20 3d 20 28 75 }. pPtrmap = (u
155ee 38 20 2a 29 73 71 6c 69 74 65 33 50 61 67 65 72 8 *)sqlite3Pager
155ef 47 65 74 44 61 74 61 28 70 44 62 50 61 67 65 29 GetData(pDbPage)
155f0 3b 0a 0a 20 20 6f 66 66 73 65 74 20 3d 20 50 54 ;.. offset = PT
155f1 52 4d 41 50 5f 50 54 52 4f 46 46 53 45 54 28 69 RMAP_PTROFFSET(i
155f2 50 74 72 6d 61 70 2c 20 6b 65 79 29 3b 0a 20 20 Ptrmap, key);.
155f3 61 73 73 65 72 74 28 20 70 45 54 79 70 65 21 3d assert( pEType!=
155f4 30 20 29 3b 0a 20 20 2a 70 45 54 79 70 65 20 3d 0 );. *pEType =
155f5 20 70 50 74 72 6d 61 70 5b 6f 66 66 73 65 74 5d pPtrmap[offset]
155f6 3b 0a 20 20 69 66 28 20 70 50 67 6e 6f 20 29 20 ;. if( pPgno )
155f7 2a 70 50 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 *pPgno = get4byt
155f8 65 28 26 70 50 74 72 6d 61 70 5b 6f 66 66 73 65 e(&pPtrmap[offse
155f9 74 2b 31 5d 29 3b 0a 0a 20 20 73 71 6c 69 74 65 t+1]);.. sqlite
155fa 33 50 61 67 65 72 55 6e 72 65 66 28 70 44 62 50 3PagerUnref(pDbP
155fb 61 67 65 29 3b 0a 20 20 69 66 28 20 2a 70 45 54 age);. if( *pET
155fc 79 70 65 3c 31 20 7c 7c 20 2a 70 45 54 79 70 65 ype<1 || *pEType
155fd 3e 35 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 >5 ) return SQLI
155fe 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b TE_CORRUPT_BKPT;
155ff 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
15600 5f 4f 4b 3b 0a 7d 0a 0a 23 65 6c 73 65 20 2f 2a _OK;.}..#else /*
15601 20 69 66 20 64 65 66 69 6e 65 64 20 53 51 4c 49 if defined SQLI
15602 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 TE_OMIT_AUTOVACU
15603 55 4d 20 2a 2f 0a 20 20 23 64 65 66 69 6e 65 20 UM */. #define
15604 70 74 72 6d 61 70 50 75 74 28 77 2c 78 2c 79 2c ptrmapPut(w,x,y,
15605 7a 2c 72 63 29 0a 20 20 23 64 65 66 69 6e 65 20 z,rc). #define
15606 70 74 72 6d 61 70 47 65 74 28 77 2c 78 2c 79 2c ptrmapGet(w,x,y,
15607 7a 29 20 53 51 4c 49 54 45 5f 4f 4b 0a 20 20 23 z) SQLITE_OK. #
15608 64 65 66 69 6e 65 20 70 74 72 6d 61 70 50 75 74 define ptrmapPut
15609 4f 76 66 6c 50 74 72 28 78 2c 20 79 2c 20 72 63 OvflPtr(x, y, rc
1560a 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 ).#endif../*.**
1560b 47 69 76 65 6e 20 61 20 62 74 72 65 65 20 70 61 Given a btree pa
1560c 67 65 20 61 6e 64 20 61 20 63 65 6c 6c 20 69 6e ge and a cell in
1560d 64 65 78 20 28 30 20 6d 65 61 6e 73 20 74 68 65 dex (0 means the
1560e 20 66 69 72 73 74 20 63 65 6c 6c 20 6f 6e 0a 2a first cell on.*
1560f 2a 20 74 68 65 20 70 61 67 65 2c 20 31 20 6d 65 * the page, 1 me
15610 61 6e 73 20 74 68 65 20 73 65 63 6f 6e 64 20 63 ans the second c
15611 65 6c 6c 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 ell, and so fort
15612 68 29 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e h) return a poin
15613 74 65 72 0a 2a 2a 20 74 6f 20 74 68 65 20 63 65 ter.** to the ce
15614 6c 6c 20 63 6f 6e 74 65 6e 74 2e 0a 2a 2a 0a 2a ll content..**.*
15615 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 * This routine w
15616 6f 72 6b 73 20 6f 6e 6c 79 20 66 6f 72 20 70 61 orks only for pa
15617 67 65 73 20 74 68 61 74 20 64 6f 20 6e 6f 74 20 ges that do not
15618 63 6f 6e 74 61 69 6e 20 6f 76 65 72 66 6c 6f 77 contain overflow
15619 20 63 65 6c 6c 73 2e 0a 2a 2f 0a 23 64 65 66 69 cells..*/.#defi
1561a 6e 65 20 66 69 6e 64 43 65 6c 6c 28 50 2c 49 29 ne findCell(P,I)
1561b 20 5c 0a 20 20 28 28 50 29 2d 3e 61 44 61 74 61 \. ((P)->aData
1561c 20 2b 20 28 28 50 29 2d 3e 6d 61 73 6b 50 61 67 + ((P)->maskPag
1561d 65 20 26 20 67 65 74 32 62 79 74 65 28 26 28 50 e & get2byte(&(P
1561e 29 2d 3e 61 44 61 74 61 5b 28 50 29 2d 3e 63 65 )->aData[(P)->ce
1561f 6c 6c 4f 66 66 73 65 74 2b 32 2a 28 49 29 5d 29 llOffset+2*(I)])
15620 29 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 61 ))../*.** This a
15621 20 6d 6f 72 65 20 63 6f 6d 70 6c 65 78 20 76 65 more complex ve
15622 72 73 69 6f 6e 20 6f 66 20 66 69 6e 64 43 65 6c rsion of findCel
15623 6c 28 29 20 74 68 61 74 20 77 6f 72 6b 73 20 66 l() that works f
15624 6f 72 0a 2a 2a 20 70 61 67 65 73 20 74 68 61 74 or.** pages that
15625 20 64 6f 20 63 6f 6e 74 61 69 6e 20 6f 76 65 72 do contain over
15626 66 6c 6f 77 20 63 65 6c 6c 73 2e 0a 2a 2f 0a 73 flow cells..*/.s
15627 74 61 74 69 63 20 75 38 20 2a 66 69 6e 64 4f 76 tatic u8 *findOv
15628 65 72 66 6c 6f 77 43 65 6c 6c 28 4d 65 6d 50 61 erflowCell(MemPa
15629 67 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 20 69 ge *pPage, int i
1562a 43 65 6c 6c 29 7b 0a 20 20 69 6e 74 20 69 3b 0a Cell){. int i;.
1562b 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
1562c 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 3_mutex_held(pPa
1562d 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 ge->pBt->mutex)
1562e 29 3b 0a 20 20 66 6f 72 28 69 3d 70 50 61 67 65 );. for(i=pPage
1562f 2d 3e 6e 4f 76 65 72 66 6c 6f 77 2d 31 3b 20 69 ->nOverflow-1; i
15630 3e 3d 30 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 69 >=0; i--){. i
15631 6e 74 20 6b 3b 0a 20 20 20 20 73 74 72 75 63 74 nt k;. struct
15632 20 5f 4f 76 66 6c 43 65 6c 6c 20 2a 70 4f 76 66 _OvflCell *pOvf
15633 6c 3b 0a 20 20 20 20 70 4f 76 66 6c 20 3d 20 26 l;. pOvfl = &
15634 70 50 61 67 65 2d 3e 61 4f 76 66 6c 5b 69 5d 3b pPage->aOvfl[i];
15635 0a 20 20 20 20 6b 20 3d 20 70 4f 76 66 6c 2d 3e . k = pOvfl->
15636 69 64 78 3b 0a 20 20 20 20 69 66 28 20 6b 3c 3d idx;. if( k<=
15637 69 43 65 6c 6c 20 29 7b 0a 20 20 20 20 20 20 69 iCell ){. i
15638 66 28 20 6b 3d 3d 69 43 65 6c 6c 20 29 7b 0a 20 f( k==iCell ){.
15639 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 70 4f return pO
1563a 76 66 6c 2d 3e 70 43 65 6c 6c 3b 0a 20 20 20 20 vfl->pCell;.
1563b 20 20 7d 0a 20 20 20 20 20 20 69 43 65 6c 6c 2d }. iCell-
1563c 2d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 -;. }. }. r
1563d 65 74 75 72 6e 20 66 69 6e 64 43 65 6c 6c 28 70 eturn findCell(p
1563e 50 61 67 65 2c 20 69 43 65 6c 6c 29 3b 0a 7d 0a Page, iCell);.}.
1563f 0a 2f 2a 0a 2a 2a 20 50 61 72 73 65 20 61 20 63 ./*.** Parse a c
15640 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 62 6c 6f 63 ell content bloc
15641 6b 20 61 6e 64 20 66 69 6c 6c 20 69 6e 20 74 68 k and fill in th
15642 65 20 43 65 6c 6c 49 6e 66 6f 20 73 74 72 75 63 e CellInfo struc
15643 74 75 72 65 2e 20 20 54 68 65 72 65 0a 2a 2a 20 ture. There.**
15644 61 72 65 20 74 77 6f 20 76 65 72 73 69 6f 6e 73 are two versions
15645 20 6f 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f of this functio
15646 6e 2e 20 20 62 74 72 65 65 50 61 72 73 65 43 65 n. btreeParseCe
15647 6c 6c 28 29 20 74 61 6b 65 73 20 61 20 0a 2a 2a ll() takes a .**
15648 20 63 65 6c 6c 20 69 6e 64 65 78 20 61 73 20 74 cell index as t
15649 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 he second argume
1564a 6e 74 20 61 6e 64 20 62 74 72 65 65 50 61 72 73 nt and btreePars
1564b 65 43 65 6c 6c 50 74 72 28 29 20 0a 2a 2a 20 74 eCellPtr() .** t
1564c 61 6b 65 73 20 61 20 70 6f 69 6e 74 65 72 20 74 akes a pointer t
1564d 6f 20 74 68 65 20 62 6f 64 79 20 6f 66 20 74 68 o the body of th
1564e 65 20 63 65 6c 6c 20 61 73 20 69 74 73 20 73 65 e cell as its se
1564f 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2e 0a 2a cond argument..*
15650 2a 0a 2a 2a 20 57 69 74 68 69 6e 20 74 68 69 73 *.** Within this
15651 20 66 69 6c 65 2c 20 74 68 65 20 70 61 72 73 65 file, the parse
15652 43 65 6c 6c 28 29 20 6d 61 63 72 6f 20 63 61 6e Cell() macro can
15653 20 62 65 20 63 61 6c 6c 65 64 20 69 6e 73 74 65 be called inste
15654 61 64 20 6f 66 0a 2a 2a 20 62 74 72 65 65 50 61 ad of.** btreePa
15655 72 73 65 43 65 6c 6c 50 74 72 28 29 2e 20 55 73 rseCellPtr(). Us
15656 69 6e 67 20 73 6f 6d 65 20 63 6f 6d 70 69 6c 65 ing some compile
15657 72 73 2c 20 74 68 69 73 20 77 69 6c 6c 20 62 65 rs, this will be
15658 20 66 61 73 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 faster..*/.stat
15659 69 63 20 76 6f 69 64 20 62 74 72 65 65 50 61 72 ic void btreePar
1565a 73 65 43 65 6c 6c 50 74 72 28 0a 20 20 4d 65 6d seCellPtr(. Mem
1565b 50 61 67 65 20 2a 70 50 61 67 65 2c 20 20 20 20 Page *pPage,
1565c 20 20 20 20 20 2f 2a 20 50 61 67 65 20 63 6f 6e /* Page con
1565d 74 61 69 6e 69 6e 67 20 74 68 65 20 63 65 6c 6c taining the cell
1565e 20 2a 2f 0a 20 20 75 38 20 2a 70 43 65 6c 6c 2c */. u8 *pCell,
1565f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
15660 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 Pointer to the
15661 63 65 6c 6c 20 74 65 78 74 2e 20 2a 2f 0a 20 20 cell text. */.
15662 43 65 6c 6c 49 6e 66 6f 20 2a 70 49 6e 66 6f 20 CellInfo *pInfo
15663 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 6c 20 /* Fill
15664 69 6e 20 74 68 69 73 20 73 74 72 75 63 74 75 72 in this structur
15665 65 20 2a 2f 0a 29 7b 0a 20 20 75 31 36 20 6e 3b e */.){. u16 n;
15666 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15667 20 20 2f 2a 20 4e 75 6d 62 65 72 20 62 79 74 65 /* Number byte
15668 73 20 69 6e 20 63 65 6c 6c 20 63 6f 6e 74 65 6e s in cell conten
15669 74 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 75 33 t header */. u3
1566a 32 20 6e 50 61 79 6c 6f 61 64 3b 20 20 20 20 20 2 nPayload;
1566b 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
1566c 6f 66 20 62 79 74 65 73 20 6f 66 20 63 65 6c 6c of bytes of cell
1566d 20 70 61 79 6c 6f 61 64 20 2a 2f 0a 0a 20 20 61 payload */.. a
1566e 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
1566f 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d utex_held(pPage-
15670 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a >pBt->mutex) );.
15671 0a 20 20 70 49 6e 66 6f 2d 3e 70 43 65 6c 6c 20 . pInfo->pCell
15672 3d 20 70 43 65 6c 6c 3b 0a 20 20 61 73 73 65 72 = pCell;. asser
15673 74 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 3d 3d t( pPage->leaf==
15674 30 20 7c 7c 20 70 50 61 67 65 2d 3e 6c 65 61 66 0 || pPage->leaf
15675 3d 3d 31 20 29 3b 0a 20 20 6e 20 3d 20 70 50 61 ==1 );. n = pPa
15676 67 65 2d 3e 63 68 69 6c 64 50 74 72 53 69 7a 65 ge->childPtrSize
15677 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 3d 3d 34 ;. assert( n==4
15678 2d 34 2a 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 -4*pPage->leaf )
15679 3b 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e 69 ;. if( pPage->i
1567a 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 69 66 28 ntKey ){. if(
1567b 20 70 50 61 67 65 2d 3e 68 61 73 44 61 74 61 20 pPage->hasData
1567c 29 7b 0a 20 20 20 20 20 20 6e 20 2b 3d 20 67 65 ){. n += ge
1567d 74 56 61 72 69 6e 74 33 32 28 26 70 43 65 6c 6c tVarint32(&pCell
1567e 5b 6e 5d 2c 20 6e 50 61 79 6c 6f 61 64 29 3b 0a [n], nPayload);.
1567f 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
15680 20 6e 50 61 79 6c 6f 61 64 20 3d 20 30 3b 0a 20 nPayload = 0;.
15681 20 20 20 7d 0a 20 20 20 20 6e 20 2b 3d 20 67 65 }. n += ge
15682 74 56 61 72 69 6e 74 28 26 70 43 65 6c 6c 5b 6e tVarint(&pCell[n
15683 5d 2c 20 28 75 36 34 2a 29 26 70 49 6e 66 6f 2d ], (u64*)&pInfo-
15684 3e 6e 4b 65 79 29 3b 0a 20 20 20 20 70 49 6e 66 >nKey);. pInf
15685 6f 2d 3e 6e 44 61 74 61 20 3d 20 6e 50 61 79 6c o->nData = nPayl
15686 6f 61 64 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 oad;. }else{.
15687 20 20 70 49 6e 66 6f 2d 3e 6e 44 61 74 61 20 3d pInfo->nData =
15688 20 30 3b 0a 20 20 20 20 6e 20 2b 3d 20 67 65 74 0;. n += get
15689 56 61 72 69 6e 74 33 32 28 26 70 43 65 6c 6c 5b Varint32(&pCell[
1568a 6e 5d 2c 20 6e 50 61 79 6c 6f 61 64 29 3b 0a 20 n], nPayload);.
1568b 20 20 20 70 49 6e 66 6f 2d 3e 6e 4b 65 79 20 3d pInfo->nKey =
1568c 20 6e 50 61 79 6c 6f 61 64 3b 0a 20 20 7d 0a 20 nPayload;. }.
1568d 20 70 49 6e 66 6f 2d 3e 6e 50 61 79 6c 6f 61 64 pInfo->nPayload
1568e 20 3d 20 6e 50 61 79 6c 6f 61 64 3b 0a 20 20 70 = nPayload;. p
1568f 49 6e 66 6f 2d 3e 6e 48 65 61 64 65 72 20 3d 20 Info->nHeader =
15690 6e 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 6e n;. testcase( n
15691 50 61 79 6c 6f 61 64 3d 3d 70 50 61 67 65 2d 3e Payload==pPage->
15692 6d 61 78 4c 6f 63 61 6c 20 29 3b 0a 20 20 74 65 maxLocal );. te
15693 73 74 63 61 73 65 28 20 6e 50 61 79 6c 6f 61 64 stcase( nPayload
15694 3d 3d 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 ==pPage->maxLoca
15695 6c 2b 31 20 29 3b 0a 20 20 69 66 28 20 6c 69 6b l+1 );. if( lik
15696 65 6c 79 28 6e 50 61 79 6c 6f 61 64 3c 3d 70 50 ely(nPayload<=pP
15697 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 29 20 29 age->maxLocal) )
15698 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 69 73 {. /* This is
15699 20 74 68 65 20 28 65 61 73 79 29 20 63 6f 6d 6d the (easy) comm
1569a 6f 6e 20 63 61 73 65 20 77 68 65 72 65 20 74 68 on case where th
1569b 65 20 65 6e 74 69 72 65 20 70 61 79 6c 6f 61 64 e entire payload
1569c 20 66 69 74 73 0a 20 20 20 20 2a 2a 20 6f 6e 20 fits. ** on
1569d 74 68 65 20 6c 6f 63 61 6c 20 70 61 67 65 2e 20 the local page.
1569e 20 4e 6f 20 6f 76 65 72 66 6c 6f 77 20 69 73 20 No overflow is
1569f 72 65 71 75 69 72 65 64 2e 0a 20 20 20 20 2a 2f required.. */
156a0 0a 20 20 20 20 69 6e 74 20 6e 53 69 7a 65 3b 20 . int nSize;
156a1 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 /* Tota
156a2 6c 20 73 69 7a 65 20 6f 66 20 63 65 6c 6c 20 63 l size of cell c
156a3 6f 6e 74 65 6e 74 20 69 6e 20 62 79 74 65 73 20 ontent in bytes
156a4 2a 2f 0a 20 20 20 20 6e 53 69 7a 65 20 3d 20 6e */. nSize = n
156a5 50 61 79 6c 6f 61 64 20 2b 20 6e 3b 0a 20 20 20 Payload + n;.
156a6 20 70 49 6e 66 6f 2d 3e 6e 4c 6f 63 61 6c 20 3d pInfo->nLocal =
156a7 20 28 75 31 36 29 6e 50 61 79 6c 6f 61 64 3b 0a (u16)nPayload;.
156a8 20 20 20 20 70 49 6e 66 6f 2d 3e 69 4f 76 65 72 pInfo->iOver
156a9 66 6c 6f 77 20 3d 20 30 3b 0a 20 20 20 20 69 66 flow = 0;. if
156aa 28 20 28 6e 53 69 7a 65 20 26 20 7e 33 29 3d 3d ( (nSize & ~3)==
156ab 30 20 29 7b 0a 20 20 20 20 20 20 6e 53 69 7a 65 0 ){. nSize
156ac 20 3d 20 34 3b 20 20 20 20 20 20 20 20 2f 2a 20 = 4; /*
156ad 4d 69 6e 69 6d 75 6d 20 63 65 6c 6c 20 73 69 7a Minimum cell siz
156ae 65 20 69 73 20 34 20 2a 2f 0a 20 20 20 20 7d 0a e is 4 */. }.
156af 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 53 69 7a 65 pInfo->nSize
156b0 20 3d 20 28 75 31 36 29 6e 53 69 7a 65 3b 0a 20 = (u16)nSize;.
156b1 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 49 }else{. /* I
156b2 66 20 74 68 65 20 70 61 79 6c 6f 61 64 20 77 69 f the payload wi
156b3 6c 6c 20 6e 6f 74 20 66 69 74 20 63 6f 6d 70 6c ll not fit compl
156b4 65 74 65 6c 79 20 6f 6e 20 74 68 65 20 6c 6f 63 etely on the loc
156b5 61 6c 20 70 61 67 65 2c 20 77 65 20 68 61 76 65 al page, we have
156b6 0a 20 20 20 20 2a 2a 20 74 6f 20 64 65 63 69 64 . ** to decid
156b7 65 20 68 6f 77 20 6d 75 63 68 20 74 6f 20 73 74 e how much to st
156b8 6f 72 65 20 6c 6f 63 61 6c 6c 79 20 61 6e 64 20 ore locally and
156b9 68 6f 77 20 6d 75 63 68 20 74 6f 20 73 70 69 6c how much to spil
156ba 6c 20 6f 6e 74 6f 0a 20 20 20 20 2a 2a 20 6f 76 l onto. ** ov
156bb 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 20 20 54 erflow pages. T
156bc 68 65 20 73 74 72 61 74 65 67 79 20 69 73 20 74 he strategy is t
156bd 6f 20 6d 69 6e 69 6d 69 7a 65 20 74 68 65 20 61 o minimize the a
156be 6d 6f 75 6e 74 20 6f 66 20 75 6e 75 73 65 64 0a mount of unused.
156bf 20 20 20 20 2a 2a 20 73 70 61 63 65 20 6f 6e 20 ** space on
156c0 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20 77 overflow pages w
156c1 68 69 6c 65 20 6b 65 65 70 69 6e 67 20 74 68 65 hile keeping the
156c2 20 61 6d 6f 75 6e 74 20 6f 66 20 6c 6f 63 61 6c amount of local
156c3 20 73 74 6f 72 61 67 65 0a 20 20 20 20 2a 2a 20 storage. **
156c4 69 6e 20 62 65 74 77 65 65 6e 20 6d 69 6e 4c 6f in between minLo
156c5 63 61 6c 20 61 6e 64 20 6d 61 78 4c 6f 63 61 6c cal and maxLocal
156c6 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 .. **. **
156c7 57 61 72 6e 69 6e 67 3a 20 20 63 68 61 6e 67 69 Warning: changi
156c8 6e 67 20 74 68 65 20 77 61 79 20 6f 76 65 72 66 ng the way overf
156c9 6c 6f 77 20 70 61 79 6c 6f 61 64 20 69 73 20 64 low payload is d
156ca 69 73 74 72 69 62 75 74 65 64 20 69 6e 20 61 6e istributed in an
156cb 79 0a 20 20 20 20 2a 2a 20 77 61 79 20 77 69 6c y. ** way wil
156cc 6c 20 72 65 73 75 6c 74 20 69 6e 20 61 6e 20 69 l result in an i
156cd 6e 63 6f 6d 70 61 74 69 62 6c 65 20 66 69 6c 65 ncompatible file
156ce 20 66 6f 72 6d 61 74 2e 0a 20 20 20 20 2a 2f 0a format.. */.
156cf 20 20 20 20 69 6e 74 20 6d 69 6e 4c 6f 63 61 6c int minLocal
156d0 3b 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 61 6d ; /* Minimum am
156d1 6f 75 6e 74 20 6f 66 20 70 61 79 6c 6f 61 64 20 ount of payload
156d2 68 65 6c 64 20 6c 6f 63 61 6c 6c 79 20 2a 2f 0a held locally */.
156d3 20 20 20 20 69 6e 74 20 6d 61 78 4c 6f 63 61 6c int maxLocal
156d4 3b 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 61 6d ; /* Maximum am
156d5 6f 75 6e 74 20 6f 66 20 70 61 79 6c 6f 61 64 20 ount of payload
156d6 68 65 6c 64 20 6c 6f 63 61 6c 6c 79 20 2a 2f 0a held locally */.
156d7 20 20 20 20 69 6e 74 20 73 75 72 70 6c 75 73 3b int surplus;
156d8 20 20 20 2f 2a 20 4f 76 65 72 66 6c 6f 77 20 70 /* Overflow p
156d9 61 79 6c 6f 61 64 20 61 76 61 69 6c 61 62 6c 65 ayload available
156da 20 66 6f 72 20 6c 6f 63 61 6c 20 73 74 6f 72 61 for local stora
156db 67 65 20 2a 2f 0a 0a 20 20 20 20 6d 69 6e 4c 6f ge */.. minLo
156dc 63 61 6c 20 3d 20 70 50 61 67 65 2d 3e 6d 69 6e cal = pPage->min
156dd 4c 6f 63 61 6c 3b 0a 20 20 20 20 6d 61 78 4c 6f Local;. maxLo
156de 63 61 6c 20 3d 20 70 50 61 67 65 2d 3e 6d 61 78 cal = pPage->max
156df 4c 6f 63 61 6c 3b 0a 20 20 20 20 73 75 72 70 6c Local;. surpl
156e0 75 73 20 3d 20 6d 69 6e 4c 6f 63 61 6c 20 2b 20 us = minLocal +
156e1 28 6e 50 61 79 6c 6f 61 64 20 2d 20 6d 69 6e 4c (nPayload - minL
156e2 6f 63 61 6c 29 25 28 70 50 61 67 65 2d 3e 70 42 ocal)%(pPage->pB
156e3 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 t->usableSize -
156e4 34 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 4);. testcase
156e5 28 20 73 75 72 70 6c 75 73 3d 3d 6d 61 78 4c 6f ( surplus==maxLo
156e6 63 61 6c 20 29 3b 0a 20 20 20 20 74 65 73 74 63 cal );. testc
156e7 61 73 65 28 20 73 75 72 70 6c 75 73 3d 3d 6d 61 ase( surplus==ma
156e8 78 4c 6f 63 61 6c 2b 31 20 29 3b 0a 20 20 20 20 xLocal+1 );.
156e9 69 66 28 20 73 75 72 70 6c 75 73 20 3c 3d 20 6d if( surplus <= m
156ea 61 78 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 20 axLocal ){.
156eb 20 70 49 6e 66 6f 2d 3e 6e 4c 6f 63 61 6c 20 3d pInfo->nLocal =
156ec 20 28 75 31 36 29 73 75 72 70 6c 75 73 3b 0a 20 (u16)surplus;.
156ed 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
156ee 70 49 6e 66 6f 2d 3e 6e 4c 6f 63 61 6c 20 3d 20 pInfo->nLocal =
156ef 28 75 31 36 29 6d 69 6e 4c 6f 63 61 6c 3b 0a 20 (u16)minLocal;.
156f0 20 20 20 7d 0a 20 20 20 20 70 49 6e 66 6f 2d 3e }. pInfo->
156f1 69 4f 76 65 72 66 6c 6f 77 20 3d 20 28 75 31 36 iOverflow = (u16
156f2 29 28 70 49 6e 66 6f 2d 3e 6e 4c 6f 63 61 6c 20 )(pInfo->nLocal
156f3 2b 20 6e 29 3b 0a 20 20 20 20 70 49 6e 66 6f 2d + n);. pInfo-
156f4 3e 6e 53 69 7a 65 20 3d 20 70 49 6e 66 6f 2d 3e >nSize = pInfo->
156f5 69 4f 76 65 72 66 6c 6f 77 20 2b 20 34 3b 0a 20 iOverflow + 4;.
156f6 20 7d 0a 7d 0a 23 64 65 66 69 6e 65 20 70 61 72 }.}.#define par
156f7 73 65 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 43 seCell(pPage, iC
156f8 65 6c 6c 2c 20 70 49 6e 66 6f 29 20 5c 0a 20 20 ell, pInfo) \.
156f9 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 btreeParseCellPt
156fa 72 28 28 70 50 61 67 65 29 2c 20 66 69 6e 64 43 r((pPage), findC
156fb 65 6c 6c 28 28 70 50 61 67 65 29 2c 20 28 69 43 ell((pPage), (iC
156fc 65 6c 6c 29 29 2c 20 28 70 49 6e 66 6f 29 29 0a ell)), (pInfo)).
156fd 73 74 61 74 69 63 20 76 6f 69 64 20 62 74 72 65 static void btre
156fe 65 50 61 72 73 65 43 65 6c 6c 28 0a 20 20 4d 65 eParseCell(. Me
156ff 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 20 20 mPage *pPage,
15700 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 63 6f /* Page co
15701 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 63 65 6c ntaining the cel
15702 6c 20 2a 2f 0a 20 20 69 6e 74 20 69 43 65 6c 6c l */. int iCell
15703 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f , /
15704 2a 20 54 68 65 20 63 65 6c 6c 20 69 6e 64 65 78 * The cell index
15705 2e 20 20 46 69 72 73 74 20 63 65 6c 6c 20 69 73 . First cell is
15706 20 30 20 2a 2f 0a 20 20 43 65 6c 6c 49 6e 66 6f 0 */. CellInfo
15707 20 2a 70 49 6e 66 6f 20 20 20 20 20 20 20 20 20 *pInfo
15708 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 68 69 73 20 /* Fill in this
15709 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 29 7b 0a structure */.){.
1570a 20 20 70 61 72 73 65 43 65 6c 6c 28 70 50 61 67 parseCell(pPag
1570b 65 2c 20 69 43 65 6c 6c 2c 20 70 49 6e 66 6f 29 e, iCell, pInfo)
1570c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 ;.}../*.** Compu
1570d 74 65 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d te the total num
1570e 62 65 72 20 6f 66 20 62 79 74 65 73 20 74 68 61 ber of bytes tha
1570f 74 20 61 20 43 65 6c 6c 20 6e 65 65 64 73 20 69 t a Cell needs i
15710 6e 20 74 68 65 20 63 65 6c 6c 0a 2a 2a 20 64 61 n the cell.** da
15711 74 61 20 61 72 65 61 20 6f 66 20 74 68 65 20 62 ta area of the b
15712 74 72 65 65 2d 70 61 67 65 2e 20 20 54 68 65 20 tree-page. The
15713 72 65 74 75 72 6e 20 6e 75 6d 62 65 72 20 69 6e return number in
15714 63 6c 75 64 65 73 20 74 68 65 20 63 65 6c 6c 0a cludes the cell.
15715 2a 2a 20 64 61 74 61 20 68 65 61 64 65 72 20 61 ** data header a
15716 6e 64 20 74 68 65 20 6c 6f 63 61 6c 20 70 61 79 nd the local pay
15717 6c 6f 61 64 2c 20 62 75 74 20 6e 6f 74 20 61 6e load, but not an
15718 79 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 y overflow page
15719 6f 72 0a 2a 2a 20 74 68 65 20 73 70 61 63 65 20 or.** the space
1571a 75 73 65 64 20 62 79 20 74 68 65 20 63 65 6c 6c used by the cell
1571b 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 73 74 61 pointer..*/.sta
1571c 74 69 63 20 75 31 36 20 63 65 6c 6c 53 69 7a 65 tic u16 cellSize
1571d 50 74 72 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 Ptr(MemPage *pPa
1571e 67 65 2c 20 75 38 20 2a 70 43 65 6c 6c 29 7b 0a ge, u8 *pCell){.
1571f 20 20 75 38 20 2a 70 49 74 65 72 20 3d 20 26 70 u8 *pIter = &p
15720 43 65 6c 6c 5b 70 50 61 67 65 2d 3e 63 68 69 6c Cell[pPage->chil
15721 64 50 74 72 53 69 7a 65 5d 3b 0a 20 20 75 33 32 dPtrSize];. u32
15722 20 6e 53 69 7a 65 3b 0a 0a 23 69 66 64 65 66 20 nSize;..#ifdef
15723 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 2f SQLITE_DEBUG. /
15724 2a 20 54 68 65 20 76 61 6c 75 65 20 72 65 74 75 * The value retu
15725 72 6e 65 64 20 62 79 20 74 68 69 73 20 66 75 6e rned by this fun
15726 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 61 6c 77 ction should alw
15727 61 79 73 20 62 65 20 74 68 65 20 73 61 6d 65 20 ays be the same
15728 61 73 0a 20 20 2a 2a 20 74 68 65 20 28 43 65 6c as. ** the (Cel
15729 6c 49 6e 66 6f 2e 6e 53 69 7a 65 29 20 76 61 6c lInfo.nSize) val
1572a 75 65 20 66 6f 75 6e 64 20 62 79 20 64 6f 69 6e ue found by doin
1572b 67 20 61 20 66 75 6c 6c 20 70 61 72 73 65 20 6f g a full parse o
1572c 66 20 74 68 65 0a 20 20 2a 2a 20 63 65 6c 6c 2e f the. ** cell.
1572d 20 49 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 If SQLITE_DEBUG
1572e 20 69 73 20 64 65 66 69 6e 65 64 2c 20 61 6e 20 is defined, an
1572f 61 73 73 65 72 74 28 29 20 61 74 20 74 68 65 20 assert() at the
15730 62 6f 74 74 6f 6d 20 6f 66 0a 20 20 2a 2a 20 74 bottom of. ** t
15731 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 76 65 72 his function ver
15732 69 66 69 65 73 20 74 68 61 74 20 74 68 69 73 20 ifies that this
15733 69 6e 76 61 72 69 61 6e 74 20 69 73 20 6e 6f 74 invariant is not
15734 20 76 69 6f 6c 61 74 65 64 2e 20 2a 2f 0a 20 20 violated. */.
15735 43 65 6c 6c 49 6e 66 6f 20 64 65 62 75 67 69 6e CellInfo debugin
15736 66 6f 3b 0a 20 20 62 74 72 65 65 50 61 72 73 65 fo;. btreeParse
15737 43 65 6c 6c 50 74 72 28 70 50 61 67 65 2c 20 70 CellPtr(pPage, p
15738 43 65 6c 6c 2c 20 26 64 65 62 75 67 69 6e 66 6f Cell, &debuginfo
15739 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 );.#endif.. if(
1573a 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 pPage->intKey )
1573b 7b 0a 20 20 20 20 75 38 20 2a 70 45 6e 64 3b 0a {. u8 *pEnd;.
1573c 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 68 if( pPage->h
1573d 61 73 44 61 74 61 20 29 7b 0a 20 20 20 20 20 20 asData ){.
1573e 70 49 74 65 72 20 2b 3d 20 67 65 74 56 61 72 69 pIter += getVari
1573f 6e 74 33 32 28 70 49 74 65 72 2c 20 6e 53 69 7a nt32(pIter, nSiz
15740 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 e);. }else{.
15741 20 20 20 20 20 6e 53 69 7a 65 20 3d 20 30 3b 0a nSize = 0;.
15742 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 70 49 }.. /* pI
15743 74 65 72 20 6e 6f 77 20 70 6f 69 6e 74 73 20 61 ter now points a
15744 74 20 74 68 65 20 36 34 2d 62 69 74 20 69 6e 74 t the 64-bit int
15745 65 67 65 72 20 6b 65 79 20 76 61 6c 75 65 2c 20 eger key value,
15746 61 20 76 61 72 69 61 62 6c 65 20 6c 65 6e 67 74 a variable lengt
15747 68 20 0a 20 20 20 20 2a 2a 20 69 6e 74 65 67 65 h . ** intege
15748 72 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 r. The following
15749 20 62 6c 6f 63 6b 20 6d 6f 76 65 73 20 70 49 74 block moves pIt
1574a 65 72 20 74 6f 20 70 6f 69 6e 74 20 61 74 20 74 er to point at t
1574b 68 65 20 66 69 72 73 74 20 62 79 74 65 0a 20 20 he first byte.
1574c 20 20 2a 2a 20 70 61 73 74 20 74 68 65 20 65 6e ** past the en
1574d 64 20 6f 66 20 74 68 65 20 6b 65 79 20 76 61 6c d of the key val
1574e 75 65 2e 20 2a 2f 0a 20 20 20 20 70 45 6e 64 20 ue. */. pEnd
1574f 3d 20 26 70 49 74 65 72 5b 39 5d 3b 0a 20 20 20 = &pIter[9];.
15750 20 77 68 69 6c 65 28 20 28 2a 70 49 74 65 72 2b while( (*pIter+
15751 2b 29 26 30 78 38 30 20 26 26 20 70 49 74 65 72 +)&0x80 && pIter
15752 3c 70 45 6e 64 20 29 3b 0a 20 20 7d 65 6c 73 65 <pEnd );. }else
15753 7b 0a 20 20 20 20 70 49 74 65 72 20 2b 3d 20 67 {. pIter += g
15754 65 74 56 61 72 69 6e 74 33 32 28 70 49 74 65 72 etVarint32(pIter
15755 2c 20 6e 53 69 7a 65 29 3b 0a 20 20 7d 0a 0a 20 , nSize);. }..
15756 20 74 65 73 74 63 61 73 65 28 20 6e 53 69 7a 65 testcase( nSize
15757 3d 3d 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 ==pPage->maxLoca
15758 6c 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 l );. testcase(
15759 20 6e 53 69 7a 65 3d 3d 70 50 61 67 65 2d 3e 6d nSize==pPage->m
1575a 61 78 4c 6f 63 61 6c 2b 31 20 29 3b 0a 20 20 69 axLocal+1 );. i
1575b 66 28 20 6e 53 69 7a 65 3e 70 50 61 67 65 2d 3e f( nSize>pPage->
1575c 6d 61 78 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 maxLocal ){.
1575d 69 6e 74 20 6d 69 6e 4c 6f 63 61 6c 20 3d 20 70 int minLocal = p
1575e 50 61 67 65 2d 3e 6d 69 6e 4c 6f 63 61 6c 3b 0a Page->minLocal;.
1575f 20 20 20 20 6e 53 69 7a 65 20 3d 20 6d 69 6e 4c nSize = minL
15760 6f 63 61 6c 20 2b 20 28 6e 53 69 7a 65 20 2d 20 ocal + (nSize -
15761 6d 69 6e 4c 6f 63 61 6c 29 20 25 20 28 70 50 61 minLocal) % (pPa
15762 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 ge->pBt->usableS
15763 69 7a 65 20 2d 20 34 29 3b 0a 20 20 20 20 74 65 ize - 4);. te
15764 73 74 63 61 73 65 28 20 6e 53 69 7a 65 3d 3d 70 stcase( nSize==p
15765 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 20 29 Page->maxLocal )
15766 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 ;. testcase(
15767 6e 53 69 7a 65 3d 3d 70 50 61 67 65 2d 3e 6d 61 nSize==pPage->ma
15768 78 4c 6f 63 61 6c 2b 31 20 29 3b 0a 20 20 20 20 xLocal+1 );.
15769 69 66 28 20 6e 53 69 7a 65 3e 70 50 61 67 65 2d if( nSize>pPage-
1576a 3e 6d 61 78 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 >maxLocal ){.
1576b 20 20 20 6e 53 69 7a 65 20 3d 20 6d 69 6e 4c 6f nSize = minLo
1576c 63 61 6c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e cal;. }. n
1576d 53 69 7a 65 20 2b 3d 20 34 3b 0a 20 20 7d 0a 20 Size += 4;. }.
1576e 20 6e 53 69 7a 65 20 2b 3d 20 28 75 33 32 29 28 nSize += (u32)(
1576f 70 49 74 65 72 20 2d 20 70 43 65 6c 6c 29 3b 0a pIter - pCell);.
15770 0a 20 20 2f 2a 20 54 68 65 20 6d 69 6e 69 6d 75 . /* The minimu
15771 6d 20 73 69 7a 65 20 6f 66 20 61 6e 79 20 63 65 m size of any ce
15772 6c 6c 20 69 73 20 34 20 62 79 74 65 73 2e 20 2a ll is 4 bytes. *
15773 2f 0a 20 20 69 66 28 20 6e 53 69 7a 65 3c 34 20 /. if( nSize<4
15774 29 7b 0a 20 20 20 20 6e 53 69 7a 65 20 3d 20 34 ){. nSize = 4
15775 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 ;. }.. assert(
15776 20 6e 53 69 7a 65 3d 3d 64 65 62 75 67 69 6e 66 nSize==debuginf
15777 6f 2e 6e 53 69 7a 65 20 29 3b 0a 20 20 72 65 74 o.nSize );. ret
15778 75 72 6e 20 28 75 31 36 29 6e 53 69 7a 65 3b 0a urn (u16)nSize;.
15779 7d 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 }.#ifndef NDEBUG
1577a 0a 73 74 61 74 69 63 20 75 31 36 20 63 65 6c 6c .static u16 cell
1577b 53 69 7a 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 Size(MemPage *pP
1577c 61 67 65 2c 20 69 6e 74 20 69 43 65 6c 6c 29 7b age, int iCell){
1577d 0a 20 20 72 65 74 75 72 6e 20 63 65 6c 6c 53 69 . return cellSi
1577e 7a 65 50 74 72 28 70 50 61 67 65 2c 20 66 69 6e zePtr(pPage, fin
1577f 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 43 65 dCell(pPage, iCe
15780 6c 6c 29 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a ll));.}.#endif..
15781 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
15782 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 2f MIT_AUTOVACUUM./
15783 2a 0a 2a 2a 20 49 66 20 74 68 65 20 63 65 6c 6c *.** If the cell
15784 20 70 43 65 6c 6c 2c 20 70 61 72 74 20 6f 66 20 pCell, part of
15785 70 61 67 65 20 70 50 61 67 65 20 63 6f 6e 74 61 page pPage conta
15786 69 6e 73 20 61 20 70 6f 69 6e 74 65 72 0a 2a 2a ins a pointer.**
15787 20 74 6f 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 to an overflow
15788 70 61 67 65 2c 20 69 6e 73 65 72 74 20 61 6e 20 page, insert an
15789 65 6e 74 72 79 20 69 6e 74 6f 20 74 68 65 20 70 entry into the p
1578a 6f 69 6e 74 65 72 2d 6d 61 70 0a 2a 2a 20 66 6f ointer-map.** fo
1578b 72 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 r the overflow p
1578c 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 age..*/.static v
1578d 6f 69 64 20 70 74 72 6d 61 70 50 75 74 4f 76 66 oid ptrmapPutOvf
1578e 6c 50 74 72 28 4d 65 6d 50 61 67 65 20 2a 70 50 lPtr(MemPage *pP
1578f 61 67 65 2c 20 75 38 20 2a 70 43 65 6c 6c 2c 20 age, u8 *pCell,
15790 69 6e 74 20 2a 70 52 43 29 7b 0a 20 20 43 65 6c int *pRC){. Cel
15791 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20 69 66 lInfo info;. if
15792 28 20 2a 70 52 43 20 29 20 72 65 74 75 72 6e 3b ( *pRC ) return;
15793 0a 20 20 61 73 73 65 72 74 28 20 70 43 65 6c 6c . assert( pCell
15794 21 3d 30 20 29 3b 0a 20 20 62 74 72 65 65 50 61 !=0 );. btreePa
15795 72 73 65 43 65 6c 6c 50 74 72 28 70 50 61 67 65 rseCellPtr(pPage
15796 2c 20 70 43 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b , pCell, &info);
15797 0a 20 20 61 73 73 65 72 74 28 20 28 69 6e 66 6f . assert( (info
15798 2e 6e 44 61 74 61 2b 28 70 50 61 67 65 2d 3e 69 .nData+(pPage->i
15799 6e 74 4b 65 79 3f 30 3a 69 6e 66 6f 2e 6e 4b 65 ntKey?0:info.nKe
1579a 79 29 29 3d 3d 69 6e 66 6f 2e 6e 50 61 79 6c 6f y))==info.nPaylo
1579b 61 64 20 29 3b 0a 20 20 69 66 28 20 69 6e 66 6f ad );. if( info
1579c 2e 69 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 .iOverflow ){.
1579d 20 20 50 67 6e 6f 20 6f 76 66 6c 20 3d 20 67 65 Pgno ovfl = ge
1579e 74 34 62 79 74 65 28 26 70 43 65 6c 6c 5b 69 6e t4byte(&pCell[in
1579f 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d 29 3b 0a fo.iOverflow]);.
157a0 20 20 20 20 70 74 72 6d 61 70 50 75 74 28 70 50 ptrmapPut(pP
157a1 61 67 65 2d 3e 70 42 74 2c 20 6f 76 66 6c 2c 20 age->pBt, ovfl,
157a2 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 PTRMAP_OVERFLOW1
157a3 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 2c 20 70 , pPage->pgno, p
157a4 52 43 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 RC);. }.}.#endi
157a5 66 0a 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 72 61 67 f.../*.** Defrag
157a6 6d 65 6e 74 20 74 68 65 20 70 61 67 65 20 67 69 ment the page gi
157a7 76 65 6e 2e 20 20 41 6c 6c 20 43 65 6c 6c 73 20 ven. All Cells
157a8 61 72 65 20 6d 6f 76 65 64 20 74 6f 20 74 68 65 are moved to the
157a9 0a 2a 2a 20 65 6e 64 20 6f 66 20 74 68 65 20 70 .** end of the p
157aa 61 67 65 20 61 6e 64 20 61 6c 6c 20 66 72 65 65 age and all free
157ab 20 73 70 61 63 65 20 69 73 20 63 6f 6c 6c 65 63 space is collec
157ac 74 65 64 20 69 6e 74 6f 20 6f 6e 65 0a 2a 2a 20 ted into one.**
157ad 62 69 67 20 46 72 65 65 42 6c 6b 20 74 68 61 74 big FreeBlk that
157ae 20 6f 63 63 75 72 73 20 69 6e 20 62 65 74 77 65 occurs in betwe
157af 65 6e 20 74 68 65 20 68 65 61 64 65 72 20 61 6e en the header an
157b0 64 20 63 65 6c 6c 0a 2a 2a 20 70 6f 69 6e 74 65 d cell.** pointe
157b1 72 20 61 72 72 61 79 20 61 6e 64 20 74 68 65 20 r array and the
157b2 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 cell content are
157b3 61 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 a..*/.static int
157b4 20 64 65 66 72 61 67 6d 65 6e 74 50 61 67 65 28 defragmentPage(
157b5 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 29 7b MemPage *pPage){
157b6 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 . int i;
157b7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
157b8 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f Loop counter */
157b9 0a 20 20 69 6e 74 20 70 63 3b 20 20 20 20 20 20 . int pc;
157ba 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
157bb 20 41 64 64 72 65 73 73 20 6f 66 20 61 20 69 2d Address of a i-
157bc 74 68 20 63 65 6c 6c 20 2a 2f 0a 20 20 69 6e 74 th cell */. int
157bd 20 68 64 72 3b 20 20 20 20 20 20 20 20 20 20 20 hdr;
157be 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 /* Offse
157bf 74 20 74 6f 20 74 68 65 20 70 61 67 65 20 68 65 t to the page he
157c0 61 64 65 72 20 2a 2f 0a 20 20 69 6e 74 20 73 69 ader */. int si
157c1 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ze;
157c2 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 /* Size of
157c3 61 20 63 65 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 a cell */. int
157c4 75 73 61 62 6c 65 53 69 7a 65 3b 20 20 20 20 20 usableSize;
157c5 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
157c6 20 6f 66 20 75 73 61 62 6c 65 20 62 79 74 65 73 of usable bytes
157c7 20 6f 6e 20 61 20 70 61 67 65 20 2a 2f 0a 20 20 on a page */.
157c8 69 6e 74 20 63 65 6c 6c 4f 66 66 73 65 74 3b 20 int cellOffset;
157c9 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 /* Of
157ca 66 73 65 74 20 74 6f 20 74 68 65 20 63 65 6c 6c fset to the cell
157cb 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79 20 2a pointer array *
157cc 2f 0a 20 20 69 6e 74 20 63 62 72 6b 3b 20 20 20 /. int cbrk;
157cd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
157ce 2a 20 4f 66 66 73 65 74 20 74 6f 20 74 68 65 20 * Offset to the
157cf 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 cell content are
157d0 61 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 65 6c 6c a */. int nCell
157d1 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
157d2 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 /* Number of c
157d3 65 6c 6c 73 20 6f 6e 20 74 68 65 20 70 61 67 65 ells on the page
157d4 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 */. unsigned c
157d5 68 61 72 20 2a 64 61 74 61 3b 20 20 20 20 20 20 har *data;
157d6 20 2f 2a 20 54 68 65 20 70 61 67 65 20 64 61 74 /* The page dat
157d7 61 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 a */. unsigned
157d8 63 68 61 72 20 2a 74 65 6d 70 3b 20 20 20 20 20 char *temp;
157d9 20 20 2f 2a 20 54 65 6d 70 20 61 72 65 61 20 66 /* Temp area f
157da 6f 72 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 or cell content
157db 2a 2f 0a 20 20 69 6e 74 20 69 43 65 6c 6c 46 69 */. int iCellFi
157dc 72 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 rst;
157dd 2f 2a 20 46 69 72 73 74 20 61 6c 6c 6f 77 61 62 /* First allowab
157de 6c 65 20 63 65 6c 6c 20 69 6e 64 65 78 20 2a 2f le cell index */
157df 0a 20 20 69 6e 74 20 69 43 65 6c 6c 4c 61 73 74 . int iCellLast
157e0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
157e1 20 4c 61 73 74 20 70 6f 73 73 69 62 6c 65 20 63 Last possible c
157e2 65 6c 6c 20 69 6e 64 65 78 20 2a 2f 0a 0a 0a 20 ell index */...
157e3 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
157e4 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 PagerIswriteable
157e5 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 (pPage->pDbPage)
157e6 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 );. assert( pP
157e7 61 67 65 2d 3e 70 42 74 21 3d 30 20 29 3b 0a 20 age->pBt!=0 );.
157e8 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e assert( pPage->
157e9 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 pBt->usableSize
157ea 3c 3d 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 <= SQLITE_MAX_PA
157eb 47 45 5f 53 49 5a 45 20 29 3b 0a 20 20 61 73 73 GE_SIZE );. ass
157ec 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 ert( pPage->nOve
157ed 72 66 6c 6f 77 3d 3d 30 20 29 3b 0a 20 20 61 73 rflow==0 );. as
157ee 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
157ef 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e tex_held(pPage->
157f0 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 pBt->mutex) );.
157f1 20 74 65 6d 70 20 3d 20 73 71 6c 69 74 65 33 50 temp = sqlite3P
157f2 61 67 65 72 54 65 6d 70 53 70 61 63 65 28 70 50 agerTempSpace(pP
157f3 61 67 65 2d 3e 70 42 74 2d 3e 70 50 61 67 65 72 age->pBt->pPager
157f4 29 3b 0a 20 20 64 61 74 61 20 3d 20 70 50 61 67 );. data = pPag
157f5 65 2d 3e 61 44 61 74 61 3b 0a 20 20 68 64 72 20 e->aData;. hdr
157f6 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 = pPage->hdrOffs
157f7 65 74 3b 0a 20 20 63 65 6c 6c 4f 66 66 73 65 74 et;. cellOffset
157f8 20 3d 20 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 = pPage->cellOf
157f9 66 73 65 74 3b 0a 20 20 6e 43 65 6c 6c 20 3d 20 fset;. nCell =
157fa 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 pPage->nCell;.
157fb 61 73 73 65 72 74 28 20 6e 43 65 6c 6c 3d 3d 67 assert( nCell==g
157fc 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 et2byte(&data[hd
157fd 72 2b 33 5d 29 20 29 3b 0a 20 20 75 73 61 62 6c r+3]) );. usabl
157fe 65 53 69 7a 65 20 3d 20 70 50 61 67 65 2d 3e 70 eSize = pPage->p
157ff 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b 0a Bt->usableSize;.
15800 20 20 63 62 72 6b 20 3d 20 67 65 74 32 62 79 74 cbrk = get2byt
15801 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 3b e(&data[hdr+5]);
15802 0a 20 20 6d 65 6d 63 70 79 28 26 74 65 6d 70 5b . memcpy(&temp[
15803 63 62 72 6b 5d 2c 20 26 64 61 74 61 5b 63 62 72 cbrk], &data[cbr
15804 6b 5d 2c 20 75 73 61 62 6c 65 53 69 7a 65 20 2d k], usableSize -
15805 20 63 62 72 6b 29 3b 0a 20 20 63 62 72 6b 20 3d cbrk);. cbrk =
15806 20 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20 69 usableSize;. i
15807 43 65 6c 6c 46 69 72 73 74 20 3d 20 63 65 6c 6c CellFirst = cell
15808 4f 66 66 73 65 74 20 2b 20 32 2a 6e 43 65 6c 6c Offset + 2*nCell
15809 3b 0a 20 20 69 43 65 6c 6c 4c 61 73 74 20 3d 20 ;. iCellLast =
1580a 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 34 3b 0a usableSize - 4;.
1580b 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 65 for(i=0; i<nCe
1580c 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 75 38 ll; i++){. u8
1580d 20 2a 70 41 64 64 72 3b 20 20 20 20 20 2f 2a 20 *pAddr; /*
1580e 54 68 65 20 69 2d 74 68 20 63 65 6c 6c 20 70 6f The i-th cell po
1580f 69 6e 74 65 72 20 2a 2f 0a 20 20 20 20 70 41 64 inter */. pAd
15810 64 72 20 3d 20 26 64 61 74 61 5b 63 65 6c 6c 4f dr = &data[cellO
15811 66 66 73 65 74 20 2b 20 69 2a 32 5d 3b 0a 20 20 ffset + i*2];.
15812 20 20 70 63 20 3d 20 67 65 74 32 62 79 74 65 28 pc = get2byte(
15813 70 41 64 64 72 29 3b 0a 20 20 20 20 74 65 73 74 pAddr);. test
15814 63 61 73 65 28 20 70 63 3d 3d 69 43 65 6c 6c 46 case( pc==iCellF
15815 69 72 73 74 20 29 3b 0a 20 20 20 20 74 65 73 74 irst );. test
15816 63 61 73 65 28 20 70 63 3d 3d 69 43 65 6c 6c 4c case( pc==iCellL
15817 61 73 74 20 29 3b 0a 23 69 66 20 21 64 65 66 69 ast );.#if !defi
15818 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c ned(SQLITE_ENABL
15819 45 5f 4f 56 45 52 53 49 5a 45 5f 43 45 4c 4c 5f E_OVERSIZE_CELL_
1581a 43 48 45 43 4b 29 0a 20 20 20 20 2f 2a 20 54 68 CHECK). /* Th
1581b 65 73 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20 68 ese conditions h
1581c 61 76 65 20 61 6c 72 65 61 64 79 20 62 65 65 6e ave already been
1581d 20 76 65 72 69 66 69 65 64 20 69 6e 20 62 74 72 verified in btr
1581e 65 65 49 6e 69 74 50 61 67 65 28 29 0a 20 20 20 eeInitPage().
1581f 20 2a 2a 20 69 66 20 53 51 4c 49 54 45 5f 45 4e ** if SQLITE_EN
15820 41 42 4c 45 5f 4f 56 45 52 53 49 5a 45 5f 43 45 ABLE_OVERSIZE_CE
15821 4c 4c 5f 43 48 45 43 4b 20 69 73 20 64 65 66 69 LL_CHECK is defi
15822 6e 65 64 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ned . */.
15823 69 66 28 20 70 63 3c 69 43 65 6c 6c 46 69 72 73 if( pc<iCellFirs
15824 74 20 7c 7c 20 70 63 3e 69 43 65 6c 6c 4c 61 73 t || pc>iCellLas
15825 74 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 t ){. retur
15826 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 n SQLITE_CORRUPT
15827 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 23 65 6e _BKPT;. }.#en
15828 64 69 66 0a 20 20 20 20 61 73 73 65 72 74 28 20 dif. assert(
15829 70 63 3e 3d 69 43 65 6c 6c 46 69 72 73 74 20 26 pc>=iCellFirst &
1582a 26 20 70 63 3c 3d 69 43 65 6c 6c 4c 61 73 74 20 & pc<=iCellLast
1582b 29 3b 0a 20 20 20 20 73 69 7a 65 20 3d 20 63 65 );. size = ce
1582c 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 67 65 2c llSizePtr(pPage,
1582d 20 26 74 65 6d 70 5b 70 63 5d 29 3b 0a 20 20 20 &temp[pc]);.
1582e 20 63 62 72 6b 20 2d 3d 20 73 69 7a 65 3b 0a 23 cbrk -= size;.#
1582f 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 if defined(SQLIT
15830 45 5f 45 4e 41 42 4c 45 5f 4f 56 45 52 53 49 5a E_ENABLE_OVERSIZ
15831 45 5f 43 45 4c 4c 5f 43 48 45 43 4b 29 0a 20 20 E_CELL_CHECK).
15832 20 20 69 66 28 20 63 62 72 6b 3c 69 43 65 6c 6c if( cbrk<iCell
15833 46 69 72 73 74 20 29 7b 0a 20 20 20 20 20 20 72 First ){. r
15834 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 eturn SQLITE_COR
15835 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d RUPT_BKPT;. }
15836 0a 23 65 6c 73 65 0a 20 20 20 20 69 66 28 20 63 .#else. if( c
15837 62 72 6b 3c 69 43 65 6c 6c 46 69 72 73 74 20 7c brk<iCellFirst |
15838 7c 20 70 63 2b 73 69 7a 65 3e 75 73 61 62 6c 65 | pc+size>usable
15839 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 72 65 Size ){. re
1583a 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 turn SQLITE_CORR
1583b 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a UPT_BKPT;. }.
1583c 23 65 6e 64 69 66 0a 20 20 20 20 61 73 73 65 72 #endif. asser
1583d 74 28 20 63 62 72 6b 2b 73 69 7a 65 3c 3d 75 73 t( cbrk+size<=us
1583e 61 62 6c 65 53 69 7a 65 20 26 26 20 63 62 72 6b ableSize && cbrk
1583f 3e 3d 69 43 65 6c 6c 46 69 72 73 74 20 29 3b 0a >=iCellFirst );.
15840 20 20 20 20 74 65 73 74 63 61 73 65 28 20 63 62 testcase( cb
15841 72 6b 2b 73 69 7a 65 3d 3d 75 73 61 62 6c 65 53 rk+size==usableS
15842 69 7a 65 20 29 3b 0a 20 20 20 20 74 65 73 74 63 ize );. testc
15843 61 73 65 28 20 70 63 2b 73 69 7a 65 3d 3d 75 73 ase( pc+size==us
15844 61 62 6c 65 53 69 7a 65 20 29 3b 0a 20 20 20 20 ableSize );.
15845 6d 65 6d 63 70 79 28 26 64 61 74 61 5b 63 62 72 memcpy(&data[cbr
15846 6b 5d 2c 20 26 74 65 6d 70 5b 70 63 5d 2c 20 73 k], &temp[pc], s
15847 69 7a 65 29 3b 0a 20 20 20 20 70 75 74 32 62 79 ize);. put2by
15848 74 65 28 70 41 64 64 72 2c 20 63 62 72 6b 29 3b te(pAddr, cbrk);
15849 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 63 . }. assert( c
1584a 62 72 6b 3e 3d 69 43 65 6c 6c 46 69 72 73 74 20 brk>=iCellFirst
1584b 29 3b 0a 20 20 70 75 74 32 62 79 74 65 28 26 64 );. put2byte(&d
1584c 61 74 61 5b 68 64 72 2b 35 5d 2c 20 63 62 72 6b ata[hdr+5], cbrk
1584d 29 3b 0a 20 20 64 61 74 61 5b 68 64 72 2b 31 5d );. data[hdr+1]
1584e 20 3d 20 30 3b 0a 20 20 64 61 74 61 5b 68 64 72 = 0;. data[hdr
1584f 2b 32 5d 20 3d 20 30 3b 0a 20 20 64 61 74 61 5b +2] = 0;. data[
15850 68 64 72 2b 37 5d 20 3d 20 30 3b 0a 20 20 6d 65 hdr+7] = 0;. me
15851 6d 73 65 74 28 26 64 61 74 61 5b 69 43 65 6c 6c mset(&data[iCell
15852 46 69 72 73 74 5d 2c 20 30 2c 20 63 62 72 6b 2d First], 0, cbrk-
15853 69 43 65 6c 6c 46 69 72 73 74 29 3b 0a 20 20 61 iCellFirst);. a
15854 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 ssert( sqlite3Pa
15855 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 gerIswriteable(p
15856 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 Page->pDbPage) )
15857 3b 0a 20 20 69 66 28 20 63 62 72 6b 2d 69 43 65 ;. if( cbrk-iCe
15858 6c 6c 46 69 72 73 74 21 3d 70 50 61 67 65 2d 3e llFirst!=pPage->
15859 6e 46 72 65 65 20 29 7b 0a 20 20 20 20 72 65 74 nFree ){. ret
1585a 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 urn SQLITE_CORRU
1585b 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20 20 72 PT_BKPT;. }. r
1585c 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
1585d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 .}../*.** Alloca
1585e 74 65 20 6e 42 79 74 65 20 62 79 74 65 73 20 6f te nByte bytes o
1585f 66 20 73 70 61 63 65 20 66 72 6f 6d 20 77 69 74 f space from wit
15860 68 69 6e 20 74 68 65 20 42 2d 54 72 65 65 20 70 hin the B-Tree p
15861 61 67 65 20 70 61 73 73 65 64 0a 2a 2a 20 61 73 age passed.** as
15862 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d the first argum
15863 65 6e 74 2e 20 57 72 69 74 65 20 69 6e 74 6f 20 ent. Write into
15864 2a 70 49 64 78 20 74 68 65 20 69 6e 64 65 78 20 *pIdx the index
15865 69 6e 74 6f 20 70 50 61 67 65 2d 3e 61 44 61 74 into pPage->aDat
15866 61 5b 5d 0a 2a 2a 20 6f 66 20 74 68 65 20 66 69 a[].** of the fi
15867 72 73 74 20 62 79 74 65 20 6f 66 20 61 6c 6c 6f rst byte of allo
15868 63 61 74 65 64 20 73 70 61 63 65 2e 20 52 65 74 cated space. Ret
15869 75 72 6e 20 65 69 74 68 65 72 20 53 51 4c 49 54 urn either SQLIT
1586a 45 5f 4f 4b 20 6f 72 0a 2a 2a 20 61 6e 20 65 72 E_OK or.** an er
1586b 72 6f 72 20 63 6f 64 65 20 28 75 73 75 61 6c 6c ror code (usuall
1586c 79 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 y SQLITE_CORRUPT
1586d 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c )..**.** The cal
1586e 6c 65 72 20 67 75 61 72 61 6e 74 65 65 73 20 74 ler guarantees t
1586f 68 61 74 20 74 68 65 72 65 20 69 73 20 73 75 66 hat there is suf
15870 66 69 63 69 65 6e 74 20 73 70 61 63 65 20 74 6f ficient space to
15871 20 6d 61 6b 65 20 74 68 65 0a 2a 2a 20 61 6c 6c make the.** all
15872 6f 63 61 74 69 6f 6e 2e 20 20 54 68 69 73 20 72 ocation. This r
15873 6f 75 74 69 6e 65 20 6d 69 67 68 74 20 6e 65 65 outine might nee
15874 64 20 74 6f 20 64 65 66 72 61 67 6d 65 6e 74 20 d to defragment
15875 69 6e 20 6f 72 64 65 72 20 74 6f 20 62 72 69 6e in order to brin
15876 67 0a 2a 2a 20 61 6c 6c 20 74 68 65 20 73 70 61 g.** all the spa
15877 63 65 20 74 6f 67 65 74 68 65 72 2c 20 68 6f 77 ce together, how
15878 65 76 65 72 2e 20 20 54 68 69 73 20 72 6f 75 74 ever. This rout
15879 69 6e 65 20 77 69 6c 6c 20 61 76 6f 69 64 20 75 ine will avoid u
1587a 73 69 6e 67 0a 2a 2a 20 74 68 65 20 66 69 72 73 sing.** the firs
1587b 74 20 74 77 6f 20 62 79 74 65 73 20 70 61 73 74 t two bytes past
1587c 20 74 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65 the cell pointe
1587d 72 20 61 72 65 61 20 73 69 6e 63 65 20 70 72 65 r area since pre
1587e 73 75 6d 61 62 6c 79 20 74 68 69 73 0a 2a 2a 20 sumably this.**
1587f 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 62 65 allocation is be
15880 69 6e 67 20 6d 61 64 65 20 69 6e 20 6f 72 64 65 ing made in orde
15881 72 20 74 6f 20 69 6e 73 65 72 74 20 61 20 6e 65 r to insert a ne
15882 77 20 63 65 6c 6c 2c 20 73 6f 20 77 65 20 77 69 w cell, so we wi
15883 6c 6c 0a 2a 2a 20 61 6c 73 6f 20 65 6e 64 20 75 ll.** also end u
15884 70 20 6e 65 65 64 69 6e 67 20 61 20 6e 65 77 20 p needing a new
15885 63 65 6c 6c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f cell pointer..*/
15886 0a 73 74 61 74 69 63 20 69 6e 74 20 61 6c 6c 6f .static int allo
15887 63 61 74 65 53 70 61 63 65 28 4d 65 6d 50 61 67 cateSpace(MemPag
15888 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 20 6e 42 e *pPage, int nB
15889 79 74 65 2c 20 69 6e 74 20 2a 70 49 64 78 29 7b yte, int *pIdx){
1588a 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 68 64 72 . const int hdr
1588b 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 = pPage->hdrOff
1588c 73 65 74 3b 20 20 20 20 2f 2a 20 4c 6f 63 61 6c set; /* Local
1588d 20 63 61 63 68 65 20 6f 66 20 70 50 61 67 65 2d cache of pPage-
1588e 3e 68 64 72 4f 66 66 73 65 74 20 2a 2f 0a 20 20 >hdrOffset */.
1588f 75 38 20 2a 20 63 6f 6e 73 74 20 64 61 74 61 20 u8 * const data
15890 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61 3b 20 = pPage->aData;
15891 20 20 20 20 20 2f 2a 20 4c 6f 63 61 6c 20 63 61 /* Local ca
15892 63 68 65 20 6f 66 20 70 50 61 67 65 2d 3e 61 44 che of pPage->aD
15893 61 74 61 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 72 ata */. int nFr
15894 61 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ag;
15895 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
15896 20 4e 75 6d 62 65 72 20 6f 66 20 66 72 61 67 6d Number of fragm
15897 65 6e 74 65 64 20 62 79 74 65 73 20 6f 6e 20 70 ented bytes on p
15898 50 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 74 6f Page */. int to
15899 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 p;
1589a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1589b 2a 20 46 69 72 73 74 20 62 79 74 65 20 6f 66 20 * First byte of
1589c 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 cell content are
1589d 61 20 2a 2f 0a 20 20 69 6e 74 20 67 61 70 3b 20 a */. int gap;
1589e 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 /* First
1589f 62 79 74 65 20 6f 66 20 67 61 70 20 62 65 74 77 byte of gap betw
158a0 65 65 6e 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 een cell pointer
158a1 73 20 61 6e 64 20 63 65 6c 6c 20 63 6f 6e 74 65 s and cell conte
158a2 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 nt */. int rc;
158a3 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 74 65 67 /* Integ
158a4 65 72 20 72 65 74 75 72 6e 20 63 6f 64 65 20 2a er return code *
158a5 2f 0a 20 20 0a 20 20 61 73 73 65 72 74 28 20 73 /. . assert( s
158a6 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 qlite3PagerIswri
158a7 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 teable(pPage->pD
158a8 62 50 61 67 65 29 20 29 3b 0a 20 20 61 73 73 65 bPage) );. asse
158a9 72 74 28 20 70 50 61 67 65 2d 3e 70 42 74 20 29 rt( pPage->pBt )
158aa 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
158ab 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
158ac 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 Page->pBt->mutex
158ad 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e ) );. assert( n
158ae 42 79 74 65 3e 3d 30 20 29 3b 20 20 2f 2a 20 4d Byte>=0 ); /* M
158af 69 6e 69 6d 75 6d 20 63 65 6c 6c 20 73 69 7a 65 inimum cell size
158b0 20 69 73 20 34 20 2a 2f 0a 20 20 61 73 73 65 72 is 4 */. asser
158b1 74 28 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 3e t( pPage->nFree>
158b2 3d 6e 42 79 74 65 20 29 3b 0a 20 20 61 73 73 65 =nByte );. asse
158b3 72 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 rt( pPage->nOver
158b4 66 6c 6f 77 3d 3d 30 20 29 3b 0a 20 20 61 73 73 flow==0 );. ass
158b5 65 72 74 28 20 6e 42 79 74 65 3c 70 50 61 67 65 ert( nByte<pPage
158b6 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a ->pBt->usableSiz
158b7 65 2d 38 20 29 3b 0a 0a 20 20 6e 46 72 61 67 20 e-8 );.. nFrag
158b8 3d 20 64 61 74 61 5b 68 64 72 2b 37 5d 3b 0a 20 = data[hdr+7];.
158b9 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e assert( pPage->
158ba 63 65 6c 6c 4f 66 66 73 65 74 20 3d 3d 20 68 64 cellOffset == hd
158bb 72 20 2b 20 31 32 20 2d 20 34 2a 70 50 61 67 65 r + 12 - 4*pPage
158bc 2d 3e 6c 65 61 66 20 29 3b 0a 20 20 67 61 70 20 ->leaf );. gap
158bd 3d 20 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 = pPage->cellOff
158be 73 65 74 20 2b 20 32 2a 70 50 61 67 65 2d 3e 6e set + 2*pPage->n
158bf 43 65 6c 6c 3b 0a 20 20 74 6f 70 20 3d 20 67 65 Cell;. top = ge
158c0 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 t2byte(&data[hdr
158c1 2b 35 5d 29 3b 0a 20 20 69 66 28 20 67 61 70 3e +5]);. if( gap>
158c2 74 6f 70 20 29 20 72 65 74 75 72 6e 20 53 51 4c top ) return SQL
158c3 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
158c4 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 67 61 ;. testcase( ga
158c5 70 2b 32 3d 3d 74 6f 70 20 29 3b 0a 20 20 74 65 p+2==top );. te
158c6 73 74 63 61 73 65 28 20 67 61 70 2b 31 3d 3d 74 stcase( gap+1==t
158c7 6f 70 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 op );. testcase
158c8 28 20 67 61 70 3d 3d 74 6f 70 20 29 3b 0a 0a 20 ( gap==top );..
158c9 20 69 66 28 20 6e 46 72 61 67 3e 3d 36 30 20 29 if( nFrag>=60 )
158ca 7b 0a 20 20 20 20 2f 2a 20 41 6c 77 61 79 73 20 {. /* Always
158cb 64 65 66 72 61 67 6d 65 6e 74 20 68 69 67 68 6c defragment highl
158cc 79 20 66 72 61 67 6d 65 6e 74 65 64 20 70 61 67 y fragmented pag
158cd 65 73 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 64 es */. rc = d
158ce 65 66 72 61 67 6d 65 6e 74 50 61 67 65 28 70 50 efragmentPage(pP
158cf 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 age);. if( rc
158d0 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 ) return rc;.
158d1 20 20 74 6f 70 20 3d 20 67 65 74 32 62 79 74 65 top = get2byte
158d2 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 3b 0a (&data[hdr+5]);.
158d3 20 20 7d 65 6c 73 65 20 69 66 28 20 67 61 70 2b }else if( gap+
158d4 32 3c 3d 74 6f 70 20 29 7b 0a 20 20 20 20 2f 2a 2<=top ){. /*
158d5 20 53 65 61 72 63 68 20 74 68 65 20 66 72 65 65 Search the free
158d6 6c 69 73 74 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 list looking for
158d7 20 61 20 66 72 65 65 20 73 6c 6f 74 20 62 69 67 a free slot big
158d8 20 65 6e 6f 75 67 68 20 74 6f 20 73 61 74 69 73 enough to satis
158d9 66 79 20 0a 20 20 20 20 2a 2a 20 74 68 65 20 72 fy . ** the r
158da 65 71 75 65 73 74 2e 20 54 68 65 20 61 6c 6c 6f equest. The allo
158db 63 61 74 69 6f 6e 20 69 73 20 6d 61 64 65 20 66 cation is made f
158dc 72 6f 6d 20 74 68 65 20 66 69 72 73 74 20 66 72 rom the first fr
158dd 65 65 20 73 6c 6f 74 20 69 6e 20 0a 20 20 20 20 ee slot in .
158de 2a 2a 20 74 68 65 20 6c 69 73 74 20 74 68 61 74 ** the list that
158df 20 69 73 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 is large enough
158e0 20 74 6f 20 61 63 63 6f 6d 61 64 61 74 65 20 69 to accomadate i
158e1 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e t.. */. in
158e2 74 20 70 63 2c 20 61 64 64 72 3b 0a 20 20 20 20 t pc, addr;.
158e3 66 6f 72 28 61 64 64 72 3d 68 64 72 2b 31 3b 20 for(addr=hdr+1;
158e4 28 70 63 20 3d 20 67 65 74 32 62 79 74 65 28 26 (pc = get2byte(&
158e5 64 61 74 61 5b 61 64 64 72 5d 29 29 3e 30 3b 20 data[addr]))>0;
158e6 61 64 64 72 3d 70 63 29 7b 0a 20 20 20 20 20 20 addr=pc){.
158e7 69 6e 74 20 73 69 7a 65 20 3d 20 67 65 74 32 62 int size = get2b
158e8 79 74 65 28 26 64 61 74 61 5b 70 63 2b 32 5d 29 yte(&data[pc+2])
158e9 3b 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 ; /* Size of
158ea 20 66 72 65 65 20 73 6c 6f 74 20 2a 2f 0a 20 20 free slot */.
158eb 20 20 20 20 69 66 28 20 73 69 7a 65 3e 3d 6e 42 if( size>=nB
158ec 79 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20 69 yte ){. i
158ed 6e 74 20 78 20 3d 20 73 69 7a 65 20 2d 20 6e 42 nt x = size - nB
158ee 79 74 65 3b 0a 20 20 20 20 20 20 20 20 74 65 73 yte;. tes
158ef 74 63 61 73 65 28 20 78 3d 3d 34 20 29 3b 0a 20 tcase( x==4 );.
158f0 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 testcase(
158f1 20 78 3d 3d 33 20 29 3b 0a 20 20 20 20 20 20 20 x==3 );.
158f2 20 69 66 28 20 78 3c 34 20 29 7b 0a 20 20 20 20 if( x<4 ){.
158f3 20 20 20 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20 /* Remove
158f4 74 68 65 20 73 6c 6f 74 20 66 72 6f 6d 20 74 68 the slot from th
158f5 65 20 66 72 65 65 2d 6c 69 73 74 2e 20 55 70 64 e free-list. Upd
158f6 61 74 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f ate the number o
158f7 66 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 66 f. ** f
158f8 72 61 67 6d 65 6e 74 65 64 20 62 79 74 65 73 20 ragmented bytes
158f9 77 69 74 68 69 6e 20 74 68 65 20 70 61 67 65 2e within the page.
158fa 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 6d 65 */. me
158fb 6d 63 70 79 28 26 64 61 74 61 5b 61 64 64 72 5d mcpy(&data[addr]
158fc 2c 20 26 64 61 74 61 5b 70 63 5d 2c 20 32 29 3b , &data[pc], 2);
158fd 0a 20 20 20 20 20 20 20 20 20 20 64 61 74 61 5b . data[
158fe 68 64 72 2b 37 5d 20 3d 20 28 75 38 29 28 6e 46 hdr+7] = (u8)(nF
158ff 72 61 67 20 2b 20 78 29 3b 0a 20 20 20 20 20 20 rag + x);.
15900 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
15901 20 20 20 2f 2a 20 54 68 65 20 73 6c 6f 74 20 72 /* The slot r
15902 65 6d 61 69 6e 73 20 6f 6e 20 74 68 65 20 66 72 emains on the fr
15903 65 65 2d 6c 69 73 74 2e 20 52 65 64 75 63 65 20 ee-list. Reduce
15904 69 74 73 20 73 69 7a 65 20 74 6f 20 61 63 63 6f its size to acco
15905 75 6e 74 0a 20 20 20 20 20 20 20 20 20 20 2a 2a unt. **
15906 20 66 6f 72 20 74 68 65 20 70 6f 72 74 69 6f 6e for the portion
15907 20 75 73 65 64 20 62 79 20 74 68 65 20 6e 65 77 used by the new
15908 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 2a 2f 0a allocation. */.
15909 20 20 20 20 20 20 20 20 20 20 70 75 74 32 62 79 put2by
1590a 74 65 28 26 64 61 74 61 5b 70 63 2b 32 5d 2c 20 te(&data[pc+2],
1590b 78 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 x);. }.
1590c 20 20 20 20 20 20 2a 70 49 64 78 20 3d 20 70 63 *pIdx = pc
1590d 20 2b 20 78 3b 0a 20 20 20 20 20 20 20 20 72 65 + x;. re
1590e 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
1590f 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
15910 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f }.. /* Check to
15911 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65 72 65 make sure there
15912 20 69 73 20 65 6e 6f 75 67 68 20 73 70 61 63 65 is enough space
15913 20 69 6e 20 74 68 65 20 67 61 70 20 74 6f 20 73 in the gap to s
15914 61 74 69 73 66 79 0a 20 20 2a 2a 20 74 68 65 20 atisfy. ** the
15915 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20 49 66 20 allocation. If
15916 6e 6f 74 2c 20 64 65 66 72 61 67 6d 65 6e 74 2e not, defragment.
15917 0a 20 20 2a 2f 0a 20 20 74 65 73 74 63 61 73 65 . */. testcase
15918 28 20 67 61 70 2b 32 2b 6e 42 79 74 65 3d 3d 74 ( gap+2+nByte==t
15919 6f 70 20 29 3b 0a 20 20 69 66 28 20 67 61 70 2b op );. if( gap+
1591a 32 2b 6e 42 79 74 65 3e 74 6f 70 20 29 7b 0a 20 2+nByte>top ){.
1591b 20 20 20 72 63 20 3d 20 64 65 66 72 61 67 6d 65 rc = defragme
1591c 6e 74 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 ntPage(pPage);.
1591d 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 if( rc ) retu
1591e 72 6e 20 72 63 3b 0a 20 20 20 20 74 6f 70 20 3d rn rc;. top =
1591f 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b get2byte(&data[
15920 68 64 72 2b 35 5d 29 3b 0a 20 20 20 20 61 73 73 hdr+5]);. ass
15921 65 72 74 28 20 67 61 70 2b 6e 42 79 74 65 3c 3d ert( gap+nByte<=
15922 74 6f 70 20 29 3b 0a 20 20 7d 0a 0a 0a 20 20 2f top );. }... /
15923 2a 20 41 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 * Allocate memor
15924 79 20 66 72 6f 6d 20 74 68 65 20 67 61 70 20 69 y from the gap i
15925 6e 20 62 65 74 77 65 65 6e 20 74 68 65 20 63 65 n between the ce
15926 6c 6c 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79 ll pointer array
15927 0a 20 20 2a 2a 20 61 6e 64 20 74 68 65 20 63 65 . ** and the ce
15928 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 2e ll content area.
15929 20 20 54 68 65 20 62 74 72 65 65 49 6e 69 74 50 The btreeInitP
1592a 61 67 65 28 29 20 63 61 6c 6c 20 68 61 73 20 61 age() call has a
1592b 6c 72 65 61 64 79 0a 20 20 2a 2a 20 76 61 6c 69 lready. ** vali
1592c 64 61 74 65 64 20 74 68 65 20 66 72 65 65 6c 69 dated the freeli
1592d 73 74 2e 20 20 47 69 76 65 6e 20 74 68 61 74 20 st. Given that
1592e 74 68 65 20 66 72 65 65 6c 69 73 74 20 69 73 20 the freelist is
1592f 76 61 6c 69 64 2c 20 74 68 65 72 65 0a 20 20 2a valid, there. *
15930 2a 20 69 73 20 6e 6f 20 77 61 79 20 74 68 61 74 * is no way that
15931 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 the allocation
15932 63 61 6e 20 65 78 74 65 6e 64 20 6f 66 66 20 74 can extend off t
15933 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 70 61 he end of the pa
15934 67 65 2e 0a 20 20 2a 2a 20 54 68 65 20 61 73 73 ge.. ** The ass
15935 65 72 74 28 29 20 62 65 6c 6f 77 20 76 65 72 69 ert() below veri
15936 66 69 65 73 20 74 68 65 20 70 72 65 76 69 6f 75 fies the previou
15937 73 20 73 65 6e 74 65 6e 63 65 2e 0a 20 20 2a 2f s sentence.. */
15938 0a 20 20 74 6f 70 20 2d 3d 20 6e 42 79 74 65 3b . top -= nByte;
15939 0a 20 20 70 75 74 32 62 79 74 65 28 26 64 61 74 . put2byte(&dat
1593a 61 5b 68 64 72 2b 35 5d 2c 20 74 6f 70 29 3b 0a a[hdr+5], top);.
1593b 20 20 61 73 73 65 72 74 28 20 74 6f 70 2b 6e 42 assert( top+nB
1593c 79 74 65 20 3c 3d 20 70 50 61 67 65 2d 3e 70 42 yte <= pPage->pB
1593d 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 29 3b t->usableSize );
1593e 0a 20 20 2a 70 49 64 78 20 3d 20 74 6f 70 3b 0a . *pIdx = top;.
1593f 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
15940 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 OK;.}../*.** Ret
15941 75 72 6e 20 61 20 73 65 63 74 69 6f 6e 20 6f 66 urn a section of
15942 20 74 68 65 20 70 50 61 67 65 2d 3e 61 44 61 74 the pPage->aDat
15943 61 20 74 6f 20 74 68 65 20 66 72 65 65 6c 69 73 a to the freelis
15944 74 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 t..** The first
15945 62 79 74 65 20 6f 66 20 74 68 65 20 6e 65 77 20 byte of the new
15946 66 72 65 65 20 62 6c 6f 63 6b 20 69 73 20 70 50 free block is pP
15947 61 67 65 2d 3e 61 44 69 73 6b 5b 73 74 61 72 74 age->aDisk[start
15948 5d 0a 2a 2a 20 61 6e 64 20 74 68 65 20 73 69 7a ].** and the siz
15949 65 20 6f 66 20 74 68 65 20 62 6c 6f 63 6b 20 69 e of the block i
1594a 73 20 22 73 69 7a 65 22 20 62 79 74 65 73 2e 0a s "size" bytes..
1594b 2a 2a 0a 2a 2a 20 4d 6f 73 74 20 6f 66 20 74 68 **.** Most of th
1594c 65 20 65 66 66 6f 72 74 20 68 65 72 65 20 69 73 e effort here is
1594d 20 69 6e 76 6f 6c 76 65 64 20 69 6e 20 63 6f 61 involved in coa
1594e 6c 65 73 69 6e 67 20 61 64 6a 61 63 65 6e 74 0a lesing adjacent.
1594f 2a 2a 20 66 72 65 65 20 62 6c 6f 63 6b 73 20 69 ** free blocks i
15950 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 62 69 67 nto a single big
15951 20 66 72 65 65 20 62 6c 6f 63 6b 2e 0a 2a 2f 0a free block..*/.
15952 73 74 61 74 69 63 20 69 6e 74 20 66 72 65 65 53 static int freeS
15953 70 61 63 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 pace(MemPage *pP
15954 61 67 65 2c 20 69 6e 74 20 73 74 61 72 74 2c 20 age, int start,
15955 69 6e 74 20 73 69 7a 65 29 7b 0a 20 20 69 6e 74 int size){. int
15956 20 61 64 64 72 2c 20 70 62 65 67 69 6e 2c 20 68 addr, pbegin, h
15957 64 72 3b 0a 20 20 69 6e 74 20 69 4c 61 73 74 3b dr;. int iLast;
15958 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15959 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 72 67 65 /* Large
1595a 73 74 20 70 6f 73 73 69 62 6c 65 20 66 72 65 65 st possible free
1595b 62 6c 6f 63 6b 20 6f 66 66 73 65 74 20 2a 2f 0a block offset */.
1595c 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 unsigned char
1595d 2a 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 *data = pPage->a
1595e 44 61 74 61 3b 0a 0a 20 20 61 73 73 65 72 74 28 Data;.. assert(
1595f 20 70 50 61 67 65 2d 3e 70 42 74 21 3d 30 20 29 pPage->pBt!=0 )
15960 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
15961 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 te3PagerIswritea
15962 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 ble(pPage->pDbPa
15963 67 65 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ge) );. assert(
15964 20 73 74 61 72 74 3e 3d 70 50 61 67 65 2d 3e 68 start>=pPage->h
15965 64 72 4f 66 66 73 65 74 2b 36 2b 70 50 61 67 65 drOffset+6+pPage
15966 2d 3e 63 68 69 6c 64 50 74 72 53 69 7a 65 20 29 ->childPtrSize )
15967 3b 0a 20 20 61 73 73 65 72 74 28 20 28 73 74 61 ;. assert( (sta
15968 72 74 20 2b 20 73 69 7a 65 29 3c 3d 70 50 61 67 rt + size)<=pPag
15969 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 e->pBt->usableSi
1596a 7a 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 ze );. assert(
1596b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
1596c 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d ld(pPage->pBt->m
1596d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 utex) );. asser
1596e 74 28 20 73 69 7a 65 3e 3d 30 20 29 3b 20 20 20 t( size>=0 );
1596f 2f 2a 20 4d 69 6e 69 6d 75 6d 20 63 65 6c 6c 20 /* Minimum cell
15970 73 69 7a 65 20 69 73 20 34 20 2a 2f 0a 0a 23 69 size is 4 */..#i
15971 66 64 65 66 20 53 51 4c 49 54 45 5f 53 45 43 55 fdef SQLITE_SECU
15972 52 45 5f 44 45 4c 45 54 45 0a 20 20 2f 2a 20 4f RE_DELETE. /* O
15973 76 65 72 77 72 69 74 65 20 64 65 6c 65 74 65 64 verwrite deleted
15974 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 77 69 74 information wit
15975 68 20 7a 65 72 6f 73 20 77 68 65 6e 20 74 68 65 h zeros when the
15976 20 53 45 43 55 52 45 5f 44 45 4c 45 54 45 20 0a SECURE_DELETE .
15977 20 20 2a 2a 20 6f 70 74 69 6f 6e 20 69 73 20 65 ** option is e
15978 6e 61 62 6c 65 64 20 61 74 20 63 6f 6d 70 69 6c nabled at compil
15979 65 2d 74 69 6d 65 20 2a 2f 0a 20 20 6d 65 6d 73 e-time */. mems
1597a 65 74 28 26 64 61 74 61 5b 73 74 61 72 74 5d 2c et(&data[start],
1597b 20 30 2c 20 73 69 7a 65 29 3b 0a 23 65 6e 64 69 0, size);.#endi
1597c 66 0a 0a 20 20 2f 2a 20 41 64 64 20 74 68 65 20 f.. /* Add the
1597d 73 70 61 63 65 20 62 61 63 6b 20 69 6e 74 6f 20 space back into
1597e 74 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 the linked list
1597f 6f 66 20 66 72 65 65 62 6c 6f 63 6b 73 2e 20 20 of freeblocks.
15980 4e 6f 74 65 20 74 68 61 74 0a 20 20 2a 2a 20 65 Note that. ** e
15981 76 65 6e 20 74 68 6f 75 67 68 20 74 68 65 20 66 ven though the f
15982 72 65 65 62 6c 6f 63 6b 20 6c 69 73 74 20 77 61 reeblock list wa
15983 73 20 63 68 65 63 6b 65 64 20 62 79 20 62 74 72 s checked by btr
15984 65 65 49 6e 69 74 50 61 67 65 28 29 2c 0a 20 20 eeInitPage(),.
15985 2a 2a 20 62 74 72 65 65 49 6e 69 74 50 61 67 65 ** btreeInitPage
15986 28 29 20 64 69 64 20 6e 6f 74 20 64 65 74 65 63 () did not detec
15987 74 20 6f 76 65 72 6c 61 70 70 69 6e 67 20 63 65 t overlapping ce
15988 6c 6c 73 20 6f 72 0a 20 20 2a 2a 20 66 72 65 65 lls or. ** free
15989 62 6c 6f 63 6b 73 20 74 68 61 74 20 6f 76 65 72 blocks that over
1598a 6c 61 70 70 65 64 20 63 65 6c 6c 73 2e 20 20 20 lapped cells.
1598b 4e 6f 72 20 64 6f 65 73 20 69 74 20 64 65 74 65 Nor does it dete
1598c 63 74 20 77 68 65 6e 20 74 68 65 0a 20 20 2a 2a ct when the. **
1598d 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 cell content ar
1598e 65 61 20 65 78 63 65 65 64 73 20 74 68 65 20 76 ea exceeds the v
1598f 61 6c 75 65 20 69 6e 20 74 68 65 20 70 61 67 65 alue in the page
15990 20 68 65 61 64 65 72 2e 20 20 49 66 20 74 68 65 header. If the
15991 73 65 0a 20 20 2a 2a 20 73 69 74 75 61 74 69 6f se. ** situatio
15992 6e 73 20 61 72 69 73 65 2c 20 74 68 65 6e 20 73 ns arise, then s
15993 75 62 73 65 71 75 65 6e 74 20 69 6e 73 65 72 74 ubsequent insert
15994 20 6f 70 65 72 61 74 69 6f 6e 73 20 6d 69 67 68 operations migh
15995 74 20 63 6f 72 72 75 70 74 0a 20 20 2a 2a 20 74 t corrupt. ** t
15996 68 65 20 66 72 65 65 6c 69 73 74 2e 20 20 53 6f he freelist. So
15997 20 77 65 20 64 6f 20 6e 65 65 64 20 74 6f 20 63 we do need to c
15998 68 65 63 6b 20 66 6f 72 20 63 6f 72 72 75 70 74 heck for corrupt
15999 69 6f 6e 20 77 68 69 6c 65 20 73 63 61 6e 6e 69 ion while scanni
1599a 6e 67 0a 20 20 2a 2a 20 74 68 65 20 66 72 65 65 ng. ** the free
1599b 6c 69 73 74 2e 0a 20 20 2a 2f 0a 20 20 68 64 72 list.. */. hdr
1599c 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 = pPage->hdrOff
1599d 73 65 74 3b 0a 20 20 61 64 64 72 20 3d 20 68 64 set;. addr = hd
1599e 72 20 2b 20 31 3b 0a 20 20 69 4c 61 73 74 20 3d r + 1;. iLast =
1599f 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 pPage->pBt->usa
159a0 62 6c 65 53 69 7a 65 20 2d 20 34 3b 0a 20 20 61 bleSize - 4;. a
159a1 73 73 65 72 74 28 20 73 74 61 72 74 3c 3d 69 4c ssert( start<=iL
159a2 61 73 74 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 ast );. while(
159a3 28 70 62 65 67 69 6e 20 3d 20 67 65 74 32 62 79 (pbegin = get2by
159a4 74 65 28 26 64 61 74 61 5b 61 64 64 72 5d 29 29 te(&data[addr]))
159a5 3c 73 74 61 72 74 20 26 26 20 70 62 65 67 69 6e <start && pbegin
159a6 3e 30 20 29 7b 0a 20 20 20 20 69 66 28 20 70 62 >0 ){. if( pb
159a7 65 67 69 6e 3c 61 64 64 72 2b 34 20 29 7b 0a 20 egin<addr+4 ){.
159a8 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
159a9 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b TE_CORRUPT_BKPT;
159aa 0a 20 20 20 20 7d 0a 20 20 20 20 61 64 64 72 20 . }. addr
159ab 3d 20 70 62 65 67 69 6e 3b 0a 20 20 7d 0a 20 20 = pbegin;. }.
159ac 69 66 28 20 70 62 65 67 69 6e 3e 69 4c 61 73 74 if( pbegin>iLast
159ad 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
159ae 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b QLITE_CORRUPT_BK
159af 50 54 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 PT;. }. assert
159b0 28 20 70 62 65 67 69 6e 3e 61 64 64 72 20 7c 7c ( pbegin>addr ||
159b1 20 70 62 65 67 69 6e 3d 3d 30 20 29 3b 0a 20 20 pbegin==0 );.
159b2 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 61 put2byte(&data[a
159b3 64 64 72 5d 2c 20 73 74 61 72 74 29 3b 0a 20 20 ddr], start);.
159b4 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 73 put2byte(&data[s
159b5 74 61 72 74 5d 2c 20 70 62 65 67 69 6e 29 3b 0a tart], pbegin);.
159b6 20 20 70 75 74 32 62 79 74 65 28 26 64 61 74 61 put2byte(&data
159b7 5b 73 74 61 72 74 2b 32 5d 2c 20 73 69 7a 65 29 [start+2], size)
159b8 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 ;. pPage->nFree
159b9 20 3d 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 = pPage->nFree
159ba 2b 20 28 75 31 36 29 73 69 7a 65 3b 0a 0a 20 20 + (u16)size;..
159bb 2f 2a 20 43 6f 61 6c 65 73 63 65 20 61 64 6a 61 /* Coalesce adja
159bc 63 65 6e 74 20 66 72 65 65 20 62 6c 6f 63 6b 73 cent free blocks
159bd 20 2a 2f 0a 20 20 61 64 64 72 20 3d 20 68 64 72 */. addr = hdr
159be 20 2b 20 31 3b 0a 20 20 77 68 69 6c 65 28 20 28 + 1;. while( (
159bf 70 62 65 67 69 6e 20 3d 20 67 65 74 32 62 79 74 pbegin = get2byt
159c0 65 28 26 64 61 74 61 5b 61 64 64 72 5d 29 29 3e e(&data[addr]))>
159c1 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 70 6e 65 0 ){. int pne
159c2 78 74 2c 20 70 73 69 7a 65 2c 20 78 3b 0a 20 20 xt, psize, x;.
159c3 20 20 61 73 73 65 72 74 28 20 70 62 65 67 69 6e assert( pbegin
159c4 3e 61 64 64 72 20 29 3b 0a 20 20 20 20 61 73 73 >addr );. ass
159c5 65 72 74 28 20 70 62 65 67 69 6e 3c 3d 70 50 61 ert( pbegin<=pPa
159c6 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 ge->pBt->usableS
159c7 69 7a 65 2d 34 20 29 3b 0a 20 20 20 20 70 6e 65 ize-4 );. pne
159c8 78 74 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 xt = get2byte(&d
159c9 61 74 61 5b 70 62 65 67 69 6e 5d 29 3b 0a 20 20 ata[pbegin]);.
159ca 20 20 70 73 69 7a 65 20 3d 20 67 65 74 32 62 79 psize = get2by
159cb 74 65 28 26 64 61 74 61 5b 70 62 65 67 69 6e 2b te(&data[pbegin+
159cc 32 5d 29 3b 0a 20 20 20 20 69 66 28 20 70 62 65 2]);. if( pbe
159cd 67 69 6e 20 2b 20 70 73 69 7a 65 20 2b 20 33 20 gin + psize + 3
159ce 3e 3d 20 70 6e 65 78 74 20 26 26 20 70 6e 65 78 >= pnext && pnex
159cf 74 3e 30 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 t>0 ){. int
159d0 20 66 72 61 67 20 3d 20 70 6e 65 78 74 20 2d 20 frag = pnext -
159d1 28 70 62 65 67 69 6e 2b 70 73 69 7a 65 29 3b 0a (pbegin+psize);.
159d2 20 20 20 20 20 20 69 66 28 20 28 66 72 61 67 3c if( (frag<
159d3 30 29 20 7c 7c 20 28 66 72 61 67 3e 28 69 6e 74 0) || (frag>(int
159d4 29 64 61 74 61 5b 68 64 72 2b 37 5d 29 20 29 7b )data[hdr+7]) ){
159d5 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 . return
159d6 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 SQLITE_CORRUPT_B
159d7 4b 50 54 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 KPT;. }.
159d8 20 20 20 64 61 74 61 5b 68 64 72 2b 37 5d 20 2d data[hdr+7] -
159d9 3d 20 28 75 38 29 66 72 61 67 3b 0a 20 20 20 20 = (u8)frag;.
159da 20 20 78 20 3d 20 67 65 74 32 62 79 74 65 28 26 x = get2byte(&
159db 64 61 74 61 5b 70 6e 65 78 74 5d 29 3b 0a 20 20 data[pnext]);.
159dc 20 20 20 20 70 75 74 32 62 79 74 65 28 26 64 61 put2byte(&da
159dd 74 61 5b 70 62 65 67 69 6e 5d 2c 20 78 29 3b 0a ta[pbegin], x);.
159de 20 20 20 20 20 20 78 20 3d 20 70 6e 65 78 74 20 x = pnext
159df 2b 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 + get2byte(&data
159e0 5b 70 6e 65 78 74 2b 32 5d 29 20 2d 20 70 62 65 [pnext+2]) - pbe
159e1 67 69 6e 3b 0a 20 20 20 20 20 20 70 75 74 32 62 gin;. put2b
159e2 79 74 65 28 26 64 61 74 61 5b 70 62 65 67 69 6e yte(&data[pbegin
159e3 2b 32 5d 2c 20 78 29 3b 0a 20 20 20 20 7d 65 6c +2], x);. }el
159e4 73 65 7b 0a 20 20 20 20 20 20 61 64 64 72 20 3d se{. addr =
159e5 20 70 62 65 67 69 6e 3b 0a 20 20 20 20 7d 0a 20 pbegin;. }.
159e6 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 }.. /* If the
159e7 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 cell content are
159e8 61 20 62 65 67 69 6e 73 20 77 69 74 68 20 61 20 a begins with a
159e9 66 72 65 65 62 6c 6f 63 6b 2c 20 72 65 6d 6f 76 freeblock, remov
159ea 65 20 69 74 2e 20 2a 2f 0a 20 20 69 66 28 20 64 e it. */. if( d
159eb 61 74 61 5b 68 64 72 2b 31 5d 3d 3d 64 61 74 61 ata[hdr+1]==data
159ec 5b 68 64 72 2b 35 5d 20 26 26 20 64 61 74 61 5b [hdr+5] && data[
159ed 68 64 72 2b 32 5d 3d 3d 64 61 74 61 5b 68 64 72 hdr+2]==data[hdr
159ee 2b 36 5d 20 29 7b 0a 20 20 20 20 69 6e 74 20 74 +6] ){. int t
159ef 6f 70 3b 0a 20 20 20 20 70 62 65 67 69 6e 20 3d op;. pbegin =
159f0 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b get2byte(&data[
159f1 68 64 72 2b 31 5d 29 3b 0a 20 20 20 20 6d 65 6d hdr+1]);. mem
159f2 63 70 79 28 26 64 61 74 61 5b 68 64 72 2b 31 5d cpy(&data[hdr+1]
159f3 2c 20 26 64 61 74 61 5b 70 62 65 67 69 6e 5d 2c , &data[pbegin],
159f4 20 32 29 3b 0a 20 20 20 20 74 6f 70 20 3d 20 67 2);. top = g
159f5 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 et2byte(&data[hd
159f6 72 2b 35 5d 29 20 2b 20 67 65 74 32 62 79 74 65 r+5]) + get2byte
159f7 28 26 64 61 74 61 5b 70 62 65 67 69 6e 2b 32 5d (&data[pbegin+2]
159f8 29 3b 0a 20 20 20 20 70 75 74 32 62 79 74 65 28 );. put2byte(
159f9 26 64 61 74 61 5b 68 64 72 2b 35 5d 2c 20 74 6f &data[hdr+5], to
159fa 70 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 p);. }. assert
159fb 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 ( sqlite3PagerIs
159fc 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d writeable(pPage-
159fd 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 72 >pDbPage) );. r
159fe 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
159ff 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 6f 64 65 .}../*.** Decode
15a00 20 74 68 65 20 66 6c 61 67 73 20 62 79 74 65 20 the flags byte
15a01 28 74 68 65 20 66 69 72 73 74 20 62 79 74 65 20 (the first byte
15a02 6f 66 20 74 68 65 20 68 65 61 64 65 72 29 20 66 of the header) f
15a03 6f 72 20 61 20 70 61 67 65 0a 2a 2a 20 61 6e 64 or a page.** and
15a04 20 69 6e 69 74 69 61 6c 69 7a 65 20 66 69 65 6c initialize fiel
15a05 64 73 20 6f 66 20 74 68 65 20 4d 65 6d 50 61 67 ds of the MemPag
15a06 65 20 73 74 72 75 63 74 75 72 65 20 61 63 63 6f e structure acco
15a07 72 64 69 6e 67 6c 79 2e 0a 2a 2a 0a 2a 2a 20 4f rdingly..**.** O
15a08 6e 6c 79 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e nly the followin
15a09 67 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 73 20 61 g combinations a
15a0a 72 65 20 73 75 70 70 6f 72 74 65 64 2e 20 20 41 re supported. A
15a0b 6e 79 74 68 69 6e 67 20 64 69 66 66 65 72 65 6e nything differen
15a0c 74 0a 2a 2a 20 69 6e 64 69 63 61 74 65 73 20 61 t.** indicates a
15a0d 20 63 6f 72 72 75 70 74 20 64 61 74 61 62 61 73 corrupt databas
15a0e 65 20 66 69 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 e files:.**.**
15a0f 20 20 20 20 20 20 20 50 54 46 5f 5a 45 52 4f 44 PTF_ZEROD
15a10 41 54 41 0a 2a 2a 20 20 20 20 20 20 20 20 20 50 ATA.** P
15a11 54 46 5f 5a 45 52 4f 44 41 54 41 20 7c 20 50 54 TF_ZERODATA | PT
15a12 46 5f 4c 45 41 46 0a 2a 2a 20 20 20 20 20 20 20 F_LEAF.**
15a13 20 20 50 54 46 5f 4c 45 41 46 44 41 54 41 20 7c PTF_LEAFDATA |
15a14 20 50 54 46 5f 49 4e 54 4b 45 59 0a 2a 2a 20 20 PTF_INTKEY.**
15a15 20 20 20 20 20 20 20 50 54 46 5f 4c 45 41 46 44 PTF_LEAFD
15a16 41 54 41 20 7c 20 50 54 46 5f 49 4e 54 4b 45 59 ATA | PTF_INTKEY
15a17 20 7c 20 50 54 46 5f 4c 45 41 46 0a 2a 2f 0a 73 | PTF_LEAF.*/.s
15a18 74 61 74 69 63 20 69 6e 74 20 64 65 63 6f 64 65 tatic int decode
15a19 46 6c 61 67 73 28 4d 65 6d 50 61 67 65 20 2a 70 Flags(MemPage *p
15a1a 50 61 67 65 2c 20 69 6e 74 20 66 6c 61 67 42 79 Page, int flagBy
15a1b 74 65 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 te){. BtShared
15a1c 2a 70 42 74 3b 20 20 20 20 20 2f 2a 20 41 20 63 *pBt; /* A c
15a1d 6f 70 79 20 6f 66 20 70 50 61 67 65 2d 3e 70 42 opy of pPage->pB
15a1e 74 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 t */.. assert(
15a1f 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 pPage->hdrOffset
15a20 3d 3d 28 70 50 61 67 65 2d 3e 70 67 6e 6f 3d 3d ==(pPage->pgno==
15a21 31 20 3f 20 31 30 30 20 3a 20 30 29 20 29 3b 0a 1 ? 100 : 0) );.
15a22 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
15a23 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 3_mutex_held(pPa
15a24 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 ge->pBt->mutex)
15a25 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6c 65 61 66 );. pPage->leaf
15a26 20 3d 20 28 75 38 29 28 66 6c 61 67 42 79 74 65 = (u8)(flagByte
15a27 3e 3e 33 29 3b 20 20 61 73 73 65 72 74 28 20 50 >>3); assert( P
15a28 54 46 5f 4c 45 41 46 20 3d 3d 20 31 3c 3c 33 20 TF_LEAF == 1<<3
15a29 29 3b 0a 20 20 66 6c 61 67 42 79 74 65 20 26 3d );. flagByte &=
15a2a 20 7e 50 54 46 5f 4c 45 41 46 3b 0a 20 20 70 50 ~PTF_LEAF;. pP
15a2b 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 53 69 7a age->childPtrSiz
15a2c 65 20 3d 20 34 2d 34 2a 70 50 61 67 65 2d 3e 6c e = 4-4*pPage->l
15a2d 65 61 66 3b 0a 20 20 70 42 74 20 3d 20 70 50 61 eaf;. pBt = pPa
15a2e 67 65 2d 3e 70 42 74 3b 0a 20 20 69 66 28 20 66 ge->pBt;. if( f
15a2f 6c 61 67 42 79 74 65 3d 3d 28 50 54 46 5f 4c 45 lagByte==(PTF_LE
15a30 41 46 44 41 54 41 20 7c 20 50 54 46 5f 49 4e 54 AFDATA | PTF_INT
15a31 4b 45 59 29 20 29 7b 0a 20 20 20 20 70 50 61 67 KEY) ){. pPag
15a32 65 2d 3e 69 6e 74 4b 65 79 20 3d 20 31 3b 0a 20 e->intKey = 1;.
15a33 20 20 20 70 50 61 67 65 2d 3e 68 61 73 44 61 74 pPage->hasDat
15a34 61 20 3d 20 70 50 61 67 65 2d 3e 6c 65 61 66 3b a = pPage->leaf;
15a35 0a 20 20 20 20 70 50 61 67 65 2d 3e 6d 61 78 4c . pPage->maxL
15a36 6f 63 61 6c 20 3d 20 70 42 74 2d 3e 6d 61 78 4c ocal = pBt->maxL
15a37 65 61 66 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e eaf;. pPage->
15a38 6d 69 6e 4c 6f 63 61 6c 20 3d 20 70 42 74 2d 3e minLocal = pBt->
15a39 6d 69 6e 4c 65 61 66 3b 0a 20 20 7d 65 6c 73 65 minLeaf;. }else
15a3a 20 69 66 28 20 66 6c 61 67 42 79 74 65 3d 3d 50 if( flagByte==P
15a3b 54 46 5f 5a 45 52 4f 44 41 54 41 20 29 7b 0a 20 TF_ZERODATA ){.
15a3c 20 20 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 pPage->intKey
15a3d 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65 2d = 0;. pPage-
15a3e 3e 68 61 73 44 61 74 61 20 3d 20 30 3b 0a 20 20 >hasData = 0;.
15a3f 20 20 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 pPage->maxLoca
15a40 6c 20 3d 20 70 42 74 2d 3e 6d 61 78 4c 6f 63 61 l = pBt->maxLoca
15a41 6c 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6d 69 l;. pPage->mi
15a42 6e 4c 6f 63 61 6c 20 3d 20 70 42 74 2d 3e 6d 69 nLocal = pBt->mi
15a43 6e 4c 6f 63 61 6c 3b 0a 20 20 7d 65 6c 73 65 7b nLocal;. }else{
15a44 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
15a45 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b TE_CORRUPT_BKPT;
15a46 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 . }. return SQ
15a47 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a LITE_OK;.}../*.*
15a48 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 * Initialize the
15a49 20 61 75 78 69 6c 69 61 72 79 20 69 6e 66 6f 72 auxiliary infor
15a4a 6d 61 74 69 6f 6e 20 66 6f 72 20 61 20 64 69 73 mation for a dis
15a4b 6b 20 62 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 52 k block..**.** R
15a4c 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 eturn SQLITE_OK
15a4d 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 49 66 20 on success. If
15a4e 77 65 20 73 65 65 20 74 68 61 74 20 74 68 65 20 we see that the
15a4f 70 61 67 65 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 page does.** not
15a50 20 63 6f 6e 74 61 69 6e 20 61 20 77 65 6c 6c 2d contain a well-
15a51 66 6f 72 6d 65 64 20 64 61 74 61 62 61 73 65 20 formed database
15a52 70 61 67 65 2c 20 74 68 65 6e 20 72 65 74 75 72 page, then retur
15a53 6e 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 43 4f 52 n .** SQLITE_COR
15a54 52 55 50 54 2e 20 20 4e 6f 74 65 20 74 68 61 74 RUPT. Note that
15a55 20 61 20 72 65 74 75 72 6e 20 6f 66 20 53 51 4c a return of SQL
15a56 49 54 45 5f 4f 4b 20 64 6f 65 73 20 6e 6f 74 0a ITE_OK does not.
15a57 2a 2a 20 67 75 61 72 61 6e 74 65 65 20 74 68 61 ** guarantee tha
15a58 74 20 74 68 65 20 70 61 67 65 20 69 73 20 77 65 t the page is we
15a59 6c 6c 2d 66 6f 72 6d 65 64 2e 20 20 49 74 20 6f ll-formed. It o
15a5a 6e 6c 79 20 73 68 6f 77 73 20 74 68 61 74 0a 2a nly shows that.*
15a5b 2a 20 77 65 20 66 61 69 6c 65 64 20 74 6f 20 64 * we failed to d
15a5c 65 74 65 63 74 20 61 6e 79 20 63 6f 72 72 75 70 etect any corrup
15a5d 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 tion..*/.static
15a5e 69 6e 74 20 62 74 72 65 65 49 6e 69 74 50 61 67 int btreeInitPag
15a5f 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 e(MemPage *pPage
15a60 29 7b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 ){.. assert( pP
15a61 61 67 65 2d 3e 70 42 74 21 3d 30 20 29 3b 0a 20 age->pBt!=0 );.
15a62 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
15a63 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 _mutex_held(pPag
15a64 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 e->pBt->mutex) )
15a65 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 ;. assert( pPag
15a66 65 2d 3e 70 67 6e 6f 3d 3d 73 71 6c 69 74 65 33 e->pgno==sqlite3
15a67 50 61 67 65 72 50 61 67 65 6e 75 6d 62 65 72 28 PagerPagenumber(
15a68 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 pPage->pDbPage)
15a69 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 );. assert( pPa
15a6a 67 65 20 3d 3d 20 73 71 6c 69 74 65 33 50 61 67 ge == sqlite3Pag
15a6b 65 72 47 65 74 45 78 74 72 61 28 70 50 61 67 65 erGetExtra(pPage
15a6c 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 ->pDbPage) );.
15a6d 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 61 assert( pPage->a
15a6e 44 61 74 61 20 3d 3d 20 73 71 6c 69 74 65 33 50 Data == sqlite3P
15a6f 61 67 65 72 47 65 74 44 61 74 61 28 70 50 61 67 agerGetData(pPag
15a70 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 0a e->pDbPage) );..
15a71 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 69 73 if( !pPage->is
15a72 49 6e 69 74 20 29 7b 0a 20 20 20 20 75 31 36 20 Init ){. u16
15a73 70 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f pc; /
15a74 2a 20 41 64 64 72 65 73 73 20 6f 66 20 61 20 66 * Address of a f
15a75 72 65 65 62 6c 6f 63 6b 20 77 69 74 68 69 6e 20 reeblock within
15a76 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 5d 20 2a pPage->aData[] *
15a77 2f 0a 20 20 20 20 75 38 20 68 64 72 3b 20 20 20 /. u8 hdr;
15a78 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 /* Offs
15a79 65 74 20 74 6f 20 62 65 67 69 6e 6e 69 6e 67 20 et to beginning
15a7a 6f 66 20 70 61 67 65 20 68 65 61 64 65 72 20 2a of page header *
15a7b 2f 0a 20 20 20 20 75 38 20 2a 64 61 74 61 3b 20 /. u8 *data;
15a7c 20 20 20 20 20 20 20 20 20 2f 2a 20 45 71 75 61 /* Equa
15a7d 6c 20 74 6f 20 70 50 61 67 65 2d 3e 61 44 61 74 l to pPage->aDat
15a7e 61 20 2a 2f 0a 20 20 20 20 42 74 53 68 61 72 65 a */. BtShare
15a7f 64 20 2a 70 42 74 3b 20 20 20 20 20 20 20 20 2f d *pBt; /
15a80 2a 20 54 68 65 20 6d 61 69 6e 20 62 74 72 65 65 * The main btree
15a81 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 structure */.
15a82 20 20 75 31 36 20 75 73 61 62 6c 65 53 69 7a 65 u16 usableSize
15a83 3b 20 20 20 20 2f 2a 20 41 6d 6f 75 6e 74 20 6f ; /* Amount o
15a84 66 20 75 73 61 62 6c 65 20 73 70 61 63 65 20 6f f usable space o
15a85 6e 20 65 61 63 68 20 70 61 67 65 20 2a 2f 0a 20 n each page */.
15a86 20 20 20 75 31 36 20 63 65 6c 6c 4f 66 66 73 65 u16 cellOffse
15a87 74 3b 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 t; /* Offset
15a88 66 72 6f 6d 20 73 74 61 72 74 20 6f 66 20 70 61 from start of pa
15a89 67 65 20 74 6f 20 66 69 72 73 74 20 63 65 6c 6c ge to first cell
15a8a 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 20 20 pointer */.
15a8b 75 31 36 20 6e 46 72 65 65 3b 20 20 20 20 20 20 u16 nFree;
15a8c 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
15a8d 75 6e 75 73 65 64 20 62 79 74 65 73 20 6f 6e 20 unused bytes on
15a8e 74 68 65 20 70 61 67 65 20 2a 2f 0a 20 20 20 20 the page */.
15a8f 75 31 36 20 74 6f 70 3b 20 20 20 20 20 20 20 20 u16 top;
15a90 20 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74 65 /* First byte
15a91 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e of the cell con
15a92 74 65 6e 74 20 61 72 65 61 20 2a 2f 0a 20 20 20 tent area */.
15a93 20 69 6e 74 20 69 43 65 6c 6c 46 69 72 73 74 3b int iCellFirst;
15a94 20 20 20 20 2f 2a 20 46 69 72 73 74 20 61 6c 6c /* First all
15a95 6f 77 61 62 6c 65 20 63 65 6c 6c 20 6f 72 20 66 owable cell or f
15a96 72 65 65 62 6c 6f 63 6b 20 6f 66 66 73 65 74 20 reeblock offset
15a97 2a 2f 0a 20 20 20 20 69 6e 74 20 69 43 65 6c 6c */. int iCell
15a98 4c 61 73 74 3b 20 20 20 20 20 2f 2a 20 4c 61 73 Last; /* Las
15a99 74 20 70 6f 73 73 69 62 6c 65 20 63 65 6c 6c 20 t possible cell
15a9a 6f 72 20 66 72 65 65 62 6c 6f 63 6b 20 6f 66 66 or freeblock off
15a9b 73 65 74 20 2a 2f 0a 0a 20 20 20 20 70 42 74 20 set */.. pBt
15a9c 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a 0a 20 = pPage->pBt;..
15a9d 20 20 20 68 64 72 20 3d 20 70 50 61 67 65 2d 3e hdr = pPage->
15a9e 68 64 72 4f 66 66 73 65 74 3b 0a 20 20 20 20 64 hdrOffset;. d
15a9f 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 ata = pPage->aDa
15aa0 74 61 3b 0a 20 20 20 20 69 66 28 20 64 65 63 6f ta;. if( deco
15aa1 64 65 46 6c 61 67 73 28 70 50 61 67 65 2c 20 64 deFlags(pPage, d
15aa2 61 74 61 5b 68 64 72 5d 29 20 29 20 72 65 74 75 ata[hdr]) ) retu
15aa3 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 rn SQLITE_CORRUP
15aa4 54 5f 42 4b 50 54 3b 0a 20 20 20 20 61 73 73 65 T_BKPT;. asse
15aa5 72 74 28 20 70 42 74 2d 3e 70 61 67 65 53 69 7a rt( pBt->pageSiz
15aa6 65 3e 3d 35 31 32 20 26 26 20 70 42 74 2d 3e 70 e>=512 && pBt->p
15aa7 61 67 65 53 69 7a 65 3c 3d 33 32 37 36 38 20 29 ageSize<=32768 )
15aa8 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6d 61 73 ;. pPage->mas
15aa9 6b 50 61 67 65 20 3d 20 70 42 74 2d 3e 70 61 67 kPage = pBt->pag
15aaa 65 53 69 7a 65 20 2d 20 31 3b 0a 20 20 20 20 70 eSize - 1;. p
15aab 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 Page->nOverflow
15aac 3d 20 30 3b 0a 20 20 20 20 75 73 61 62 6c 65 53 = 0;. usableS
15aad 69 7a 65 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c ize = pBt->usabl
15aae 65 53 69 7a 65 3b 0a 20 20 20 20 70 50 61 67 65 eSize;. pPage
15aaf 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 20 3d 20 63 ->cellOffset = c
15ab0 65 6c 6c 4f 66 66 73 65 74 20 3d 20 68 64 72 20 ellOffset = hdr
15ab1 2b 20 31 32 20 2d 20 34 2a 70 50 61 67 65 2d 3e + 12 - 4*pPage->
15ab2 6c 65 61 66 3b 0a 20 20 20 20 74 6f 70 20 3d 20 leaf;. top =
15ab3 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 get2byte(&data[h
15ab4 64 72 2b 35 5d 29 3b 0a 20 20 20 20 70 50 61 67 dr+5]);. pPag
15ab5 65 2d 3e 6e 43 65 6c 6c 20 3d 20 67 65 74 32 62 e->nCell = get2b
15ab6 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 33 5d yte(&data[hdr+3]
15ab7 29 3b 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 );. if( pPage
15ab8 2d 3e 6e 43 65 6c 6c 3e 4d 58 5f 43 45 4c 4c 28 ->nCell>MX_CELL(
15ab9 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a pBt) ){. /*
15aba 20 54 6f 20 6d 61 6e 79 20 63 65 6c 6c 73 20 66 To many cells f
15abb 6f 72 20 61 20 73 69 6e 67 6c 65 20 70 61 67 65 or a single page
15abc 2e 20 20 54 68 65 20 70 61 67 65 20 6d 75 73 74 . The page must
15abd 20 62 65 20 63 6f 72 72 75 70 74 20 2a 2f 0a 20 be corrupt */.
15abe 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
15abf 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b TE_CORRUPT_BKPT;
15ac0 0a 20 20 20 20 7d 0a 20 20 20 20 74 65 73 74 63 . }. testc
15ac1 61 73 65 28 20 70 50 61 67 65 2d 3e 6e 43 65 6c ase( pPage->nCel
15ac2 6c 3d 3d 4d 58 5f 43 45 4c 4c 28 70 42 74 29 20 l==MX_CELL(pBt)
15ac3 29 3b 0a 0a 20 20 20 20 2f 2a 20 41 20 6d 61 6c );.. /* A mal
15ac4 66 6f 72 6d 65 64 20 64 61 74 61 62 61 73 65 20 formed database
15ac5 70 61 67 65 20 6d 69 67 68 74 20 63 61 75 73 65 page might cause
15ac6 20 75 73 20 74 6f 20 72 65 61 64 20 70 61 73 74 us to read past
15ac7 20 74 68 65 20 65 6e 64 0a 20 20 20 20 2a 2a 20 the end. **
15ac8 6f 66 20 70 61 67 65 20 77 68 65 6e 20 70 61 72 of page when par
15ac9 73 69 6e 67 20 61 20 63 65 6c 6c 2e 20 20 0a 20 sing a cell. .
15aca 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 **. ** The
15acb 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b following block
15acc 20 6f 66 20 63 6f 64 65 20 63 68 65 63 6b 73 20 of code checks
15acd 65 61 72 6c 79 20 74 6f 20 73 65 65 20 69 66 20 early to see if
15ace 61 20 63 65 6c 6c 20 65 78 74 65 6e 64 73 0a 20 a cell extends.
15acf 20 20 20 2a 2a 20 70 61 73 74 20 74 68 65 20 65 ** past the e
15ad0 6e 64 20 6f 66 20 61 20 70 61 67 65 20 62 6f 75 nd of a page bou
15ad1 6e 64 61 72 79 20 61 6e 64 20 63 61 75 73 65 73 ndary and causes
15ad2 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 20 SQLITE_CORRUPT
15ad3 74 6f 20 62 65 20 0a 20 20 20 20 2a 2a 20 72 65 to be . ** re
15ad4 74 75 72 6e 65 64 20 69 66 20 69 74 20 64 6f 65 turned if it doe
15ad5 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 43 s.. */. iC
15ad6 65 6c 6c 46 69 72 73 74 20 3d 20 63 65 6c 6c 4f ellFirst = cellO
15ad7 66 66 73 65 74 20 2b 20 32 2a 70 50 61 67 65 2d ffset + 2*pPage-
15ad8 3e 6e 43 65 6c 6c 3b 0a 20 20 20 20 69 43 65 6c >nCell;. iCel
15ad9 6c 4c 61 73 74 20 3d 20 75 73 61 62 6c 65 53 69 lLast = usableSi
15ada 7a 65 20 2d 20 34 3b 0a 23 69 66 20 64 65 66 69 ze - 4;.#if defi
15adb 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c ned(SQLITE_ENABL
15adc 45 5f 4f 56 45 52 53 49 5a 45 5f 43 45 4c 4c 5f E_OVERSIZE_CELL_
15add 43 48 45 43 4b 29 0a 20 20 20 20 7b 0a 20 20 20 CHECK). {.
15ade 20 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 int i;
15adf 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e /* Index in
15ae0 74 6f 20 74 68 65 20 63 65 6c 6c 20 70 6f 69 6e to the cell poin
15ae1 74 65 72 20 61 72 72 61 79 20 2a 2f 0a 20 20 20 ter array */.
15ae2 20 20 20 69 6e 74 20 73 7a 3b 20 20 20 20 20 20 int sz;
15ae3 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 /* Size of
15ae4 61 20 63 65 6c 6c 20 2a 2f 0a 0a 20 20 20 20 20 a cell */..
15ae5 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 if( !pPage->lea
15ae6 66 20 29 20 69 43 65 6c 6c 4c 61 73 74 2d 2d 3b f ) iCellLast--;
15ae7 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 . for(i=0;
15ae8 69 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 20 i<pPage->nCell;
15ae9 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 70 63 i++){. pc
15aea 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 = get2byte(&dat
15aeb 61 5b 63 65 6c 6c 4f 66 66 73 65 74 2b 69 2a 32 a[cellOffset+i*2
15aec 5d 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 ]);. test
15aed 63 61 73 65 28 20 70 63 3d 3d 69 43 65 6c 6c 46 case( pc==iCellF
15aee 69 72 73 74 20 29 3b 0a 20 20 20 20 20 20 20 20 irst );.
15aef 74 65 73 74 63 61 73 65 28 20 70 63 3d 3d 69 43 testcase( pc==iC
15af0 65 6c 6c 4c 61 73 74 20 29 3b 0a 20 20 20 20 20 ellLast );.
15af1 20 20 20 69 66 28 20 70 63 3c 69 43 65 6c 6c 46 if( pc<iCellF
15af2 69 72 73 74 20 7c 7c 20 70 63 3e 69 43 65 6c 6c irst || pc>iCell
15af3 4c 61 73 74 20 29 7b 0a 20 20 20 20 20 20 20 20 Last ){.
15af4 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
15af5 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 CORRUPT_BKPT;.
15af6 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
15af7 73 7a 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 sz = cellSizePtr
15af8 28 70 50 61 67 65 2c 20 26 64 61 74 61 5b 70 63 (pPage, &data[pc
15af9 5d 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 ]);. test
15afa 63 61 73 65 28 20 70 63 2b 73 7a 3d 3d 75 73 61 case( pc+sz==usa
15afb 62 6c 65 53 69 7a 65 20 29 3b 0a 20 20 20 20 20 bleSize );.
15afc 20 20 20 69 66 28 20 70 63 2b 73 7a 3e 75 73 61 if( pc+sz>usa
15afd 62 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 bleSize ){.
15afe 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
15aff 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b TE_CORRUPT_BKPT;
15b00 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
15b01 20 7d 0a 20 20 20 20 20 20 69 66 28 20 21 70 50 }. if( !pP
15b02 61 67 65 2d 3e 6c 65 61 66 20 29 20 69 43 65 6c age->leaf ) iCel
15b03 6c 4c 61 73 74 2b 2b 3b 0a 20 20 20 20 7d 20 20 lLast++;. }
15b04 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 .#endif.. /*
15b05 43 6f 6d 70 75 74 65 20 74 68 65 20 74 6f 74 61 Compute the tota
15b06 6c 20 66 72 65 65 20 73 70 61 63 65 20 6f 6e 20 l free space on
15b07 74 68 65 20 70 61 67 65 20 2a 2f 0a 20 20 20 20 the page */.
15b08 70 63 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 pc = get2byte(&d
15b09 61 74 61 5b 68 64 72 2b 31 5d 29 3b 0a 20 20 20 ata[hdr+1]);.
15b0a 20 6e 46 72 65 65 20 3d 20 64 61 74 61 5b 68 64 nFree = data[hd
15b0b 72 2b 37 5d 20 2b 20 74 6f 70 3b 0a 20 20 20 20 r+7] + top;.
15b0c 77 68 69 6c 65 28 20 70 63 3e 30 20 29 7b 0a 20 while( pc>0 ){.
15b0d 20 20 20 20 20 75 31 36 20 6e 65 78 74 2c 20 73 u16 next, s
15b0e 69 7a 65 3b 0a 20 20 20 20 20 20 69 66 28 20 70 ize;. if( p
15b0f 63 3c 69 43 65 6c 6c 46 69 72 73 74 20 7c 7c 20 c<iCellFirst ||
15b10 70 63 3e 69 43 65 6c 6c 4c 61 73 74 20 29 7b 0a pc>iCellLast ){.
15b11 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61 72 74 /* Start
15b12 20 6f 66 20 66 72 65 65 20 62 6c 6f 63 6b 20 69 of free block i
15b13 73 20 6f 66 66 20 74 68 65 20 70 61 67 65 20 2a s off the page *
15b14 2f 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e /. return
15b15 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
15b16 42 4b 50 54 3b 20 0a 20 20 20 20 20 20 7d 0a 20 BKPT; . }.
15b17 20 20 20 20 20 6e 65 78 74 20 3d 20 67 65 74 32 next = get2
15b18 62 79 74 65 28 26 64 61 74 61 5b 70 63 5d 29 3b byte(&data[pc]);
15b19 0a 20 20 20 20 20 20 73 69 7a 65 20 3d 20 67 65 . size = ge
15b1a 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 63 2b t2byte(&data[pc+
15b1b 32 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 28 2]);. if( (
15b1c 6e 65 78 74 3e 30 20 26 26 20 6e 65 78 74 3c 3d next>0 && next<=
15b1d 70 63 2b 73 69 7a 65 2b 33 29 20 7c 7c 20 70 63 pc+size+3) || pc
15b1e 2b 73 69 7a 65 3e 75 73 61 62 6c 65 53 69 7a 65 +size>usableSize
15b1f 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 46 ){. /* F
15b20 72 65 65 20 62 6c 6f 63 6b 73 20 6d 75 73 74 20 ree blocks must
15b21 62 65 20 69 6e 20 61 73 63 65 6e 64 69 6e 67 20 be in ascending
15b22 6f 72 64 65 72 2e 20 41 6e 64 20 74 68 65 20 6c order. And the l
15b23 61 73 74 20 62 79 74 65 20 6f 66 0a 09 2a 2a 20 ast byte of..**
15b24 74 68 65 20 66 72 65 65 2d 62 6c 6f 63 6b 20 6d the free-block m
15b25 75 73 74 20 6c 69 65 20 6f 6e 20 74 68 65 20 64 ust lie on the d
15b26 61 74 61 62 61 73 65 20 70 61 67 65 2e 20 20 2a atabase page. *
15b27 2f 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e /. return
15b28 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
15b29 42 4b 50 54 3b 20 0a 20 20 20 20 20 20 7d 0a 20 BKPT; . }.
15b2a 20 20 20 20 20 6e 46 72 65 65 20 3d 20 6e 46 72 nFree = nFr
15b2b 65 65 20 2b 20 73 69 7a 65 3b 0a 20 20 20 20 20 ee + size;.
15b2c 20 70 63 20 3d 20 6e 65 78 74 3b 0a 20 20 20 20 pc = next;.
15b2d 7d 0a 0a 20 20 20 20 2f 2a 20 41 74 20 74 68 69 }.. /* At thi
15b2e 73 20 70 6f 69 6e 74 2c 20 6e 46 72 65 65 20 63 s point, nFree c
15b2f 6f 6e 74 61 69 6e 73 20 74 68 65 20 73 75 6d 20 ontains the sum
15b30 6f 66 20 74 68 65 20 6f 66 66 73 65 74 20 74 6f of the offset to
15b31 20 74 68 65 20 73 74 61 72 74 0a 20 20 20 20 2a the start. *
15b32 2a 20 6f 66 20 74 68 65 20 63 65 6c 6c 2d 63 6f * of the cell-co
15b33 6e 74 65 6e 74 20 61 72 65 61 20 70 6c 75 73 20 ntent area plus
15b34 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 66 72 the number of fr
15b35 65 65 20 62 79 74 65 73 20 77 69 74 68 69 6e 0a ee bytes within.
15b36 20 20 20 20 2a 2a 20 74 68 65 20 63 65 6c 6c 2d ** the cell-
15b37 63 6f 6e 74 65 6e 74 20 61 72 65 61 2e 20 49 66 content area. If
15b38 20 74 68 69 73 20 69 73 20 67 72 65 61 74 65 72 this is greater
15b39 20 74 68 61 6e 20 74 68 65 20 75 73 61 62 6c 65 than the usable
15b3a 2d 73 69 7a 65 0a 20 20 20 20 2a 2a 20 6f 66 20 -size. ** of
15b3b 74 68 65 20 70 61 67 65 2c 20 74 68 65 6e 20 74 the page, then t
15b3c 68 65 20 70 61 67 65 20 6d 75 73 74 20 62 65 20 he page must be
15b3d 63 6f 72 72 75 70 74 65 64 2e 20 54 68 69 73 20 corrupted. This
15b3e 63 68 65 63 6b 20 61 6c 73 6f 0a 20 20 20 20 2a check also. *
15b3f 2a 20 73 65 72 76 65 73 20 74 6f 20 76 65 72 69 * serves to veri
15b40 66 79 20 74 68 61 74 20 74 68 65 20 6f 66 66 73 fy that the offs
15b41 65 74 20 74 6f 20 74 68 65 20 73 74 61 72 74 20 et to the start
15b42 6f 66 20 74 68 65 20 63 65 6c 6c 2d 63 6f 6e 74 of the cell-cont
15b43 65 6e 74 0a 20 20 20 20 2a 2a 20 61 72 65 61 2c ent. ** area,
15b44 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 according to th
15b45 65 20 70 61 67 65 20 68 65 61 64 65 72 2c 20 6c e page header, l
15b46 69 65 73 20 77 69 74 68 69 6e 20 74 68 65 20 70 ies within the p
15b47 61 67 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 age.. */.
15b48 69 66 28 20 6e 46 72 65 65 3e 75 73 61 62 6c 65 if( nFree>usable
15b49 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 72 65 Size ){. re
15b4a 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 turn SQLITE_CORR
15b4b 55 50 54 5f 42 4b 50 54 3b 20 0a 20 20 20 20 7d UPT_BKPT; . }
15b4c 0a 20 20 20 20 70 50 61 67 65 2d 3e 6e 46 72 65 . pPage->nFre
15b4d 65 20 3d 20 28 75 31 36 29 28 6e 46 72 65 65 20 e = (u16)(nFree
15b4e 2d 20 69 43 65 6c 6c 46 69 72 73 74 29 3b 0a 20 - iCellFirst);.
15b4f 20 20 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 pPage->isInit
15b50 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 = 1;. }. retu
15b51 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
15b52 0a 2f 2a 0a 2a 2a 20 53 65 74 20 75 70 20 61 20 ./*.** Set up a
15b53 72 61 77 20 70 61 67 65 20 73 6f 20 74 68 61 74 raw page so that
15b54 20 69 74 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 61 it looks like a
15b55 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 68 database page h
15b56 6f 6c 64 69 6e 67 0a 2a 2a 20 6e 6f 20 65 6e 74 olding.** no ent
15b57 72 69 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ries..*/.static
15b58 76 6f 69 64 20 7a 65 72 6f 50 61 67 65 28 4d 65 void zeroPage(Me
15b59 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 69 6e mPage *pPage, in
15b5a 74 20 66 6c 61 67 73 29 7b 0a 20 20 75 6e 73 69 t flags){. unsi
15b5b 67 6e 65 64 20 63 68 61 72 20 2a 64 61 74 61 20 gned char *data
15b5c 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61 3b 0a = pPage->aData;.
15b5d 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 BtShared *pBt
15b5e 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 = pPage->pBt;.
15b5f 75 38 20 68 64 72 20 3d 20 70 50 61 67 65 2d 3e u8 hdr = pPage->
15b60 68 64 72 4f 66 66 73 65 74 3b 0a 20 20 75 31 36 hdrOffset;. u16
15b61 20 66 69 72 73 74 3b 0a 0a 20 20 61 73 73 65 72 first;.. asser
15b62 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 t( sqlite3PagerP
15b63 61 67 65 6e 75 6d 62 65 72 28 70 50 61 67 65 2d agenumber(pPage-
15b64 3e 70 44 62 50 61 67 65 29 3d 3d 70 50 61 67 65 >pDbPage)==pPage
15b65 2d 3e 70 67 6e 6f 20 29 3b 0a 20 20 61 73 73 65 ->pgno );. asse
15b66 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 rt( sqlite3Pager
15b67 47 65 74 45 78 74 72 61 28 70 50 61 67 65 2d 3e GetExtra(pPage->
15b68 70 44 62 50 61 67 65 29 20 3d 3d 20 28 76 6f 69 pDbPage) == (voi
15b69 64 2a 29 70 50 61 67 65 20 29 3b 0a 20 20 61 73 d*)pPage );. as
15b6a 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 sert( sqlite3Pag
15b6b 65 72 47 65 74 44 61 74 61 28 70 50 61 67 65 2d erGetData(pPage-
15b6c 3e 70 44 62 50 61 67 65 29 20 3d 3d 20 64 61 74 >pDbPage) == dat
15b6d 61 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 a );. assert( s
15b6e 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 qlite3PagerIswri
15b6f 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 teable(pPage->pD
15b70 62 50 61 67 65 29 20 29 3b 0a 20 20 61 73 73 65 bPage) );. asse
15b71 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
15b72 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 x_held(pBt->mute
15b73 78 29 20 29 3b 0a 20 20 2f 2a 6d 65 6d 73 65 74 x) );. /*memset
15b74 28 26 64 61 74 61 5b 68 64 72 5d 2c 20 30 2c 20 (&data[hdr], 0,
15b75 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 pBt->usableSize
15b76 2d 20 68 64 72 29 3b 2a 2f 0a 20 20 64 61 74 61 - hdr);*/. data
15b77 5b 68 64 72 5d 20 3d 20 28 63 68 61 72 29 66 6c [hdr] = (char)fl
15b78 61 67 73 3b 0a 20 20 66 69 72 73 74 20 3d 20 68 ags;. first = h
15b79 64 72 20 2b 20 38 20 2b 20 34 2a 28 28 66 6c 61 dr + 8 + 4*((fla
15b7a 67 73 26 50 54 46 5f 4c 45 41 46 29 3d 3d 30 20 gs&PTF_LEAF)==0
15b7b 3f 31 3a 30 29 3b 0a 20 20 6d 65 6d 73 65 74 28 ?1:0);. memset(
15b7c 26 64 61 74 61 5b 68 64 72 2b 31 5d 2c 20 30 2c &data[hdr+1], 0,
15b7d 20 34 29 3b 0a 20 20 64 61 74 61 5b 68 64 72 2b 4);. data[hdr+
15b7e 37 5d 20 3d 20 30 3b 0a 20 20 70 75 74 32 62 79 7] = 0;. put2by
15b7f 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 2c te(&data[hdr+5],
15b80 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 pBt->usableSize
15b81 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 46 72 65 );. pPage->nFre
15b82 65 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 e = pBt->usableS
15b83 69 7a 65 20 2d 20 66 69 72 73 74 3b 0a 20 20 64 ize - first;. d
15b84 65 63 6f 64 65 46 6c 61 67 73 28 70 50 61 67 65 ecodeFlags(pPage
15b85 2c 20 66 6c 61 67 73 29 3b 0a 20 20 70 50 61 67 , flags);. pPag
15b86 65 2d 3e 68 64 72 4f 66 66 73 65 74 20 3d 20 68 e->hdrOffset = h
15b87 64 72 3b 0a 20 20 70 50 61 67 65 2d 3e 63 65 6c dr;. pPage->cel
15b88 6c 4f 66 66 73 65 74 20 3d 20 66 69 72 73 74 3b lOffset = first;
15b89 0a 20 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 . pPage->nOverf
15b8a 6c 6f 77 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 low = 0;. asser
15b8b 74 28 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 t( pBt->pageSize
15b8c 3e 3d 35 31 32 20 26 26 20 70 42 74 2d 3e 70 61 >=512 && pBt->pa
15b8d 67 65 53 69 7a 65 3c 3d 33 32 37 36 38 20 29 3b geSize<=32768 );
15b8e 0a 20 20 70 50 61 67 65 2d 3e 6d 61 73 6b 50 61 . pPage->maskPa
15b8f 67 65 20 3d 20 70 42 74 2d 3e 70 61 67 65 53 69 ge = pBt->pageSi
15b90 7a 65 20 2d 20 31 3b 0a 20 20 70 50 61 67 65 2d ze - 1;. pPage-
15b91 3e 6e 43 65 6c 6c 20 3d 20 30 3b 0a 20 20 70 50 >nCell = 0;. pP
15b92 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 31 3b age->isInit = 1;
15b93 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 .}.../*.** Conve
15b94 72 74 20 61 20 44 62 50 61 67 65 20 6f 62 74 61 rt a DbPage obta
15b95 69 6e 65 64 20 66 72 6f 6d 20 74 68 65 20 70 61 ined from the pa
15b96 67 65 72 20 69 6e 74 6f 20 61 20 4d 65 6d 50 61 ger into a MemPa
15b97 67 65 20 75 73 65 64 20 62 79 0a 2a 2a 20 74 68 ge used by.** th
15b98 65 20 62 74 72 65 65 20 6c 61 79 65 72 2e 0a 2a e btree layer..*
15b99 2f 0a 73 74 61 74 69 63 20 4d 65 6d 50 61 67 65 /.static MemPage
15b9a 20 2a 62 74 72 65 65 50 61 67 65 46 72 6f 6d 44 *btreePageFromD
15b9b 62 50 61 67 65 28 44 62 50 61 67 65 20 2a 70 44 bPage(DbPage *pD
15b9c 62 50 61 67 65 2c 20 50 67 6e 6f 20 70 67 6e 6f bPage, Pgno pgno
15b9d 2c 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 , BtShared *pBt)
15b9e 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 {. MemPage *pPa
15b9f 67 65 20 3d 20 28 4d 65 6d 50 61 67 65 2a 29 73 ge = (MemPage*)s
15ba0 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 45 78 qlite3PagerGetEx
15ba1 74 72 61 28 70 44 62 50 61 67 65 29 3b 0a 20 20 tra(pDbPage);.
15ba2 70 50 61 67 65 2d 3e 61 44 61 74 61 20 3d 20 73 pPage->aData = s
15ba3 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61 qlite3PagerGetDa
15ba4 74 61 28 70 44 62 50 61 67 65 29 3b 0a 20 20 70 ta(pDbPage);. p
15ba5 50 61 67 65 2d 3e 70 44 62 50 61 67 65 20 3d 20 Page->pDbPage =
15ba6 70 44 62 50 61 67 65 3b 0a 20 20 70 50 61 67 65 pDbPage;. pPage
15ba7 2d 3e 70 42 74 20 3d 20 70 42 74 3b 0a 20 20 70 ->pBt = pBt;. p
15ba8 50 61 67 65 2d 3e 70 67 6e 6f 20 3d 20 70 67 6e Page->pgno = pgn
15ba9 6f 3b 0a 20 20 70 50 61 67 65 2d 3e 68 64 72 4f o;. pPage->hdrO
15baa 66 66 73 65 74 20 3d 20 70 50 61 67 65 2d 3e 70 ffset = pPage->p
15bab 67 6e 6f 3d 3d 31 20 3f 20 31 30 30 20 3a 20 30 gno==1 ? 100 : 0
15bac 3b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 ;. return pPage
15bad 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20 ; .}../*.** Get
15bae 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20 a page from the
15baf 70 61 67 65 72 2e 20 20 49 6e 69 74 69 61 6c 69 pager. Initiali
15bb0 7a 65 20 74 68 65 20 4d 65 6d 50 61 67 65 2e 70 ze the MemPage.p
15bb1 42 74 20 61 6e 64 0a 2a 2a 20 4d 65 6d 50 61 67 Bt and.** MemPag
15bb2 65 2e 61 44 61 74 61 20 65 6c 65 6d 65 6e 74 73 e.aData elements
15bb3 20 69 66 20 6e 65 65 64 65 64 2e 0a 2a 2a 0a 2a if needed..**.*
15bb4 2a 20 49 66 20 74 68 65 20 6e 6f 43 6f 6e 74 65 * If the noConte
15bb5 6e 74 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 nt flag is set,
15bb6 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 77 65 it means that we
15bb7 20 64 6f 20 6e 6f 74 20 63 61 72 65 20 61 62 6f do not care abo
15bb8 75 74 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e ut.** the conten
15bb9 74 20 6f 66 20 74 68 65 20 70 61 67 65 20 61 74 t of the page at
15bba 20 74 68 69 73 20 74 69 6d 65 2e 20 20 53 6f 20 this time. So
15bbb 64 6f 20 6e 6f 74 20 67 6f 20 74 6f 20 74 68 65 do not go to the
15bbc 20 64 69 73 6b 0a 2a 2a 20 74 6f 20 66 65 74 63 disk.** to fetc
15bbd 68 20 74 68 65 20 63 6f 6e 74 65 6e 74 2e 20 20 h the content.
15bbe 4a 75 73 74 20 66 69 6c 6c 20 69 6e 20 74 68 65 Just fill in the
15bbf 20 63 6f 6e 74 65 6e 74 20 77 69 74 68 20 7a 65 content with ze
15bc0 72 6f 73 20 66 6f 72 20 6e 6f 77 2e 0a 2a 2a 20 ros for now..**
15bc1 49 66 20 69 6e 20 74 68 65 20 66 75 74 75 72 65 If in the future
15bc2 20 77 65 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 we call sqlite3
15bc3 50 61 67 65 72 57 72 69 74 65 28 29 20 6f 6e 20 PagerWrite() on
15bc4 74 68 69 73 20 70 61 67 65 2c 20 74 68 61 74 0a this page, that.
15bc5 2a 2a 20 6d 65 61 6e 73 20 77 65 20 68 61 76 65 ** means we have
15bc6 20 73 74 61 72 74 65 64 20 74 6f 20 62 65 20 63 started to be c
15bc7 6f 6e 63 65 72 6e 65 64 20 61 62 6f 75 74 20 63 oncerned about c
15bc8 6f 6e 74 65 6e 74 20 61 6e 64 20 74 68 65 20 64 ontent and the d
15bc9 69 73 6b 0a 2a 2a 20 72 65 61 64 20 73 68 6f 75 isk.** read shou
15bca 6c 64 20 6f 63 63 75 72 20 61 74 20 74 68 61 74 ld occur at that
15bcb 20 70 6f 69 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 point..*/.stati
15bcc 63 20 69 6e 74 20 62 74 72 65 65 47 65 74 50 61 c int btreeGetPa
15bcd 67 65 28 0a 20 20 42 74 53 68 61 72 65 64 20 2a ge(. BtShared *
15bce 70 42 74 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 pBt, /* Th
15bcf 65 20 62 74 72 65 65 20 2a 2f 0a 20 20 50 67 6e e btree */. Pgn
15bd0 6f 20 70 67 6e 6f 2c 20 20 20 20 20 20 20 20 20 o pgno,
15bd1 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 /* Number of t
15bd2 68 65 20 70 61 67 65 20 74 6f 20 66 65 74 63 68 he page to fetch
15bd3 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 2a */. MemPage **
15bd4 70 70 50 61 67 65 2c 20 20 20 20 2f 2a 20 52 65 ppPage, /* Re
15bd5 74 75 72 6e 20 74 68 65 20 70 61 67 65 20 69 6e turn the page in
15bd6 20 74 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 this parameter
15bd7 2a 2f 0a 20 20 69 6e 74 20 6e 6f 43 6f 6e 74 65 */. int noConte
15bd8 6e 74 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 nt /* Do
15bd9 6e 6f 74 20 6c 6f 61 64 20 70 61 67 65 20 63 6f not load page co
15bda 6e 74 65 6e 74 20 69 66 20 74 72 75 65 20 2a 2f ntent if true */
15bdb 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 .){. int rc;.
15bdc 44 62 50 61 67 65 20 2a 70 44 62 50 61 67 65 3b DbPage *pDbPage;
15bdd 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 .. assert( sqli
15bde 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
15bdf 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 Bt->mutex) );.
15be0 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
15be1 72 41 63 71 75 69 72 65 28 70 42 74 2d 3e 70 50 rAcquire(pBt->pP
15be2 61 67 65 72 2c 20 70 67 6e 6f 2c 20 28 44 62 50 ager, pgno, (DbP
15be3 61 67 65 2a 2a 29 26 70 44 62 50 61 67 65 2c 20 age**)&pDbPage,
15be4 6e 6f 43 6f 6e 74 65 6e 74 29 3b 0a 20 20 69 66 noContent);. if
15be5 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 ( rc ) return rc
15be6 3b 0a 20 20 2a 70 70 50 61 67 65 20 3d 20 62 74 ;. *ppPage = bt
15be7 72 65 65 50 61 67 65 46 72 6f 6d 44 62 50 61 67 reePageFromDbPag
15be8 65 28 70 44 62 50 61 67 65 2c 20 70 67 6e 6f 2c e(pDbPage, pgno,
15be9 20 70 42 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 pBt);. return
15bea 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a SQLITE_OK;.}../*
15beb 0a 2a 2a 20 52 65 74 72 69 65 76 65 20 61 20 70 .** Retrieve a p
15bec 61 67 65 20 66 72 6f 6d 20 74 68 65 20 70 61 67 age from the pag
15bed 65 72 20 63 61 63 68 65 2e 20 49 66 20 74 68 65 er cache. If the
15bee 20 72 65 71 75 65 73 74 65 64 20 70 61 67 65 20 requested page
15bef 69 73 20 6e 6f 74 0a 2a 2a 20 61 6c 72 65 61 64 is not.** alread
15bf0 79 20 69 6e 20 74 68 65 20 70 61 67 65 72 20 63 y in the pager c
15bf1 61 63 68 65 20 72 65 74 75 72 6e 20 4e 55 4c 4c ache return NULL
15bf2 2e 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 . Initialize the
15bf3 20 4d 65 6d 50 61 67 65 2e 70 42 74 20 61 6e 64 MemPage.pBt and
15bf4 0a 2a 2a 20 4d 65 6d 50 61 67 65 2e 61 44 61 74 .** MemPage.aDat
15bf5 61 20 65 6c 65 6d 65 6e 74 73 20 69 66 20 6e 65 a elements if ne
15bf6 65 64 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 eded..*/.static
15bf7 4d 65 6d 50 61 67 65 20 2a 62 74 72 65 65 50 61 MemPage *btreePa
15bf8 67 65 4c 6f 6f 6b 75 70 28 42 74 53 68 61 72 65 geLookup(BtShare
15bf9 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20 70 67 6e d *pBt, Pgno pgn
15bfa 6f 29 7b 0a 20 20 44 62 50 61 67 65 20 2a 70 44 o){. DbPage *pD
15bfb 62 50 61 67 65 3b 0a 20 20 61 73 73 65 72 74 28 bPage;. assert(
15bfc 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
15bfd 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 eld(pBt->mutex)
15bfe 29 3b 0a 20 20 70 44 62 50 61 67 65 20 3d 20 73 );. pDbPage = s
15bff 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 6f 6b 75 qlite3PagerLooku
15c00 70 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 70 p(pBt->pPager, p
15c01 67 6e 6f 29 3b 0a 20 20 69 66 28 20 70 44 62 50 gno);. if( pDbP
15c02 61 67 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 age ){. retur
15c03 6e 20 62 74 72 65 65 50 61 67 65 46 72 6f 6d 44 n btreePageFromD
15c04 62 50 61 67 65 28 70 44 62 50 61 67 65 2c 20 70 bPage(pDbPage, p
15c05 67 6e 6f 2c 20 70 42 74 29 3b 0a 20 20 7d 0a 20 gno, pBt);. }.
15c06 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a return 0;.}../*
15c07 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 .** Return the s
15c08 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 ize of the datab
15c09 61 73 65 20 66 69 6c 65 20 69 6e 20 70 61 67 65 ase file in page
15c0a 73 2e 20 49 66 20 74 68 65 72 65 20 69 73 20 61 s. If there is a
15c0b 6e 79 20 6b 69 6e 64 20 6f 66 0a 2a 2a 20 65 72 ny kind of.** er
15c0c 72 6f 72 2c 20 72 65 74 75 72 6e 20 28 28 75 6e ror, return ((un
15c0d 73 69 67 6e 65 64 20 69 6e 74 29 2d 31 29 2e 0a signed int)-1)..
15c0e 2a 2f 0a 73 74 61 74 69 63 20 50 67 6e 6f 20 70 */.static Pgno p
15c0f 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 42 74 agerPagecount(Bt
15c10 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 Shared *pBt){.
15c11 69 6e 74 20 6e 50 61 67 65 20 3d 20 2d 31 3b 0a int nPage = -1;.
15c12 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 int rc;. asse
15c13 72 74 28 20 70 42 74 2d 3e 70 50 61 67 65 31 20 rt( pBt->pPage1
15c14 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 );. rc = sqlite
15c15 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 3PagerPagecount(
15c16 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 26 6e 50 pBt->pPager, &nP
15c17 61 67 65 29 3b 0a 20 20 61 73 73 65 72 74 28 20 age);. assert(
15c18 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c rc==SQLITE_OK ||
15c19 20 6e 50 61 67 65 3d 3d 2d 31 20 29 3b 0a 20 20 nPage==-1 );.
15c1a 72 65 74 75 72 6e 20 28 50 67 6e 6f 29 6e 50 61 return (Pgno)nPa
15c1b 67 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 ge;.}../*.** Get
15c1c 20 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 a page from the
15c1d 20 70 61 67 65 72 20 61 6e 64 20 69 6e 69 74 69 pager and initi
15c1e 61 6c 69 7a 65 20 69 74 2e 20 20 54 68 69 73 20 alize it. This
15c1f 72 6f 75 74 69 6e 65 20 69 73 20 6a 75 73 74 20 routine is just
15c20 61 0a 2a 2a 20 63 6f 6e 76 65 6e 69 65 6e 63 65 a.** convenience
15c21 20 77 72 61 70 70 65 72 20 61 72 6f 75 6e 64 20 wrapper around
15c22 73 65 70 61 72 61 74 65 20 63 61 6c 6c 73 20 74 separate calls t
15c23 6f 20 62 74 72 65 65 47 65 74 50 61 67 65 28 29 o btreeGetPage()
15c24 20 61 6e 64 20 0a 2a 2a 20 62 74 72 65 65 49 6e and .** btreeIn
15c25 69 74 50 61 67 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 itPage()..**.**
15c26 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 If an error occu
15c27 72 73 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c rs, then the val
15c28 75 65 20 2a 70 70 50 61 67 65 20 69 73 20 73 65 ue *ppPage is se
15c29 74 20 74 6f 20 69 73 20 75 6e 64 65 66 69 6e 65 t to is undefine
15c2a 64 2e 20 49 74 0a 2a 2a 20 6d 61 79 20 72 65 6d d. It.** may rem
15c2b 61 69 6e 20 75 6e 63 68 61 6e 67 65 64 2c 20 6f ain unchanged, o
15c2c 72 20 69 74 20 6d 61 79 20 62 65 20 73 65 74 20 r it may be set
15c2d 74 6f 20 61 6e 20 69 6e 76 61 6c 69 64 20 76 61 to an invalid va
15c2e 6c 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 lue..*/.static i
15c2f 6e 74 20 67 65 74 41 6e 64 49 6e 69 74 50 61 67 nt getAndInitPag
15c30 65 28 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 e(. BtShared *p
15c31 42 74 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 Bt, /*
15c32 54 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c The database fil
15c33 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f e */. Pgno pgno
15c34 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e , /* N
15c35 75 6d 62 65 72 20 6f 66 20 74 68 65 20 70 61 67 umber of the pag
15c36 65 20 74 6f 20 67 65 74 20 2a 2f 0a 20 20 4d 65 e to get */. Me
15c37 6d 50 61 67 65 20 2a 2a 70 70 50 61 67 65 20 20 mPage **ppPage
15c38 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 /* Write the
15c39 70 61 67 65 20 70 6f 69 6e 74 65 72 20 68 65 72 page pointer her
15c3a 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 e */.){. int rc
15c3b 3b 0a 20 20 54 45 53 54 4f 4e 4c 59 28 20 50 67 ;. TESTONLY( Pg
15c3c 6e 6f 20 69 4c 61 73 74 50 67 20 3d 20 70 61 67 no iLastPg = pag
15c3d 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29 erPagecount(pBt)
15c3e 3b 20 29 0a 20 20 61 73 73 65 72 74 28 20 73 71 ; ). assert( sq
15c3f 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
15c40 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a (pBt->mutex) );.
15c41 0a 20 20 72 63 20 3d 20 62 74 72 65 65 47 65 74 . rc = btreeGet
15c42 50 61 67 65 28 70 42 74 2c 20 70 67 6e 6f 2c 20 Page(pBt, pgno,
15c43 70 70 50 61 67 65 2c 20 30 29 3b 0a 20 20 69 66 ppPage, 0);. if
15c44 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
15c45 29 7b 0a 20 20 20 20 72 63 20 3d 20 62 74 72 65 ){. rc = btre
15c46 65 49 6e 69 74 50 61 67 65 28 2a 70 70 50 61 67 eInitPage(*ppPag
15c47 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d e);. if( rc!=
15c48 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
15c49 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 2a releasePage(*
15c4a 70 70 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20 ppPage);. }.
15c4b 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 }.. /* If the
15c4c 72 65 71 75 65 73 74 65 64 20 70 61 67 65 20 6e requested page n
15c4d 75 6d 62 65 72 20 77 61 73 20 65 69 74 68 65 72 umber was either
15c4e 20 30 20 6f 72 20 67 72 65 61 74 65 72 20 74 68 0 or greater th
15c4f 61 6e 20 74 68 65 20 70 61 67 65 0a 20 20 2a 2a an the page. **
15c50 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 6c number of the l
15c51 61 73 74 20 70 61 67 65 20 69 6e 20 74 68 65 20 ast page in the
15c52 64 61 74 61 62 61 73 65 2c 20 74 68 69 73 20 66 database, this f
15c53 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 72 unction should r
15c54 65 74 75 72 6e 0a 20 20 2a 2a 20 53 51 4c 49 54 eturn. ** SQLIT
15c55 45 5f 43 4f 52 52 55 50 54 20 6f 72 20 73 6f 6d E_CORRUPT or som
15c56 65 20 6f 74 68 65 72 20 65 72 72 6f 72 20 28 69 e other error (i
15c57 2e 65 2e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 29 .e. SQLITE_FULL)
15c58 2e 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 69 . Check that thi
15c59 73 0a 20 20 2a 2a 20 69 73 20 74 68 65 20 63 61 s. ** is the ca
15c5a 73 65 2e 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 se. */. assert
15c5b 28 20 28 70 67 6e 6f 3e 30 20 26 26 20 70 67 6e ( (pgno>0 && pgn
15c5c 6f 3c 3d 69 4c 61 73 74 50 67 29 20 7c 7c 20 72 o<=iLastPg) || r
15c5d 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a c!=SQLITE_OK );.
15c5e 20 20 74 65 73 74 63 61 73 65 28 20 70 67 6e 6f testcase( pgno
15c5f 3d 3d 30 20 29 3b 0a 20 20 74 65 73 74 63 61 73 ==0 );. testcas
15c60 65 28 20 70 67 6e 6f 3d 3d 69 4c 61 73 74 50 67 e( pgno==iLastPg
15c61 20 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 );.. return rc
15c62 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 ;.}../*.** Relea
15c63 73 65 20 61 20 4d 65 6d 50 61 67 65 2e 20 20 54 se a MemPage. T
15c64 68 69 73 20 73 68 6f 75 6c 64 20 62 65 20 63 61 his should be ca
15c65 6c 6c 65 64 20 6f 6e 63 65 20 66 6f 72 20 65 61 lled once for ea
15c66 63 68 20 70 72 69 6f 72 0a 2a 2a 20 63 61 6c 6c ch prior.** call
15c67 20 74 6f 20 62 74 72 65 65 47 65 74 50 61 67 65 to btreeGetPage
15c68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
15c69 20 72 65 6c 65 61 73 65 50 61 67 65 28 4d 65 6d releasePage(Mem
15c6a 50 61 67 65 20 2a 70 50 61 67 65 29 7b 0a 20 20 Page *pPage){.
15c6b 69 66 28 20 70 50 61 67 65 20 29 7b 0a 20 20 20 if( pPage ){.
15c6c 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e assert( pPage->
15c6d 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 7c 7c 20 nOverflow==0 ||
15c6e 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 sqlite3PagerPage
15c6f 52 65 66 63 6f 75 6e 74 28 70 50 61 67 65 2d 3e Refcount(pPage->
15c70 70 44 62 50 61 67 65 29 3e 31 20 29 3b 0a 20 20 pDbPage)>1 );.
15c71 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d assert( pPage-
15c72 3e 61 44 61 74 61 20 29 3b 0a 20 20 20 20 61 73 >aData );. as
15c73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 42 74 sert( pPage->pBt
15c74 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
15c75 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 45 sqlite3PagerGetE
15c76 78 74 72 61 28 70 50 61 67 65 2d 3e 70 44 62 50 xtra(pPage->pDbP
15c77 61 67 65 29 20 3d 3d 20 28 76 6f 69 64 2a 29 70 age) == (void*)p
15c78 50 61 67 65 20 29 3b 0a 20 20 20 20 61 73 73 65 Page );. asse
15c79 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 rt( sqlite3Pager
15c7a 47 65 74 44 61 74 61 28 70 50 61 67 65 2d 3e 70 GetData(pPage->p
15c7b 44 62 50 61 67 65 29 3d 3d 70 50 61 67 65 2d 3e DbPage)==pPage->
15c7c 61 44 61 74 61 20 29 3b 0a 20 20 20 20 61 73 73 aData );. ass
15c7d 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
15c7e 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 ex_held(pPage->p
15c7f 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 Bt->mutex) );.
15c80 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e sqlite3PagerUn
15c81 72 65 66 28 70 50 61 67 65 2d 3e 70 44 62 50 61 ref(pPage->pDbPa
15c82 67 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a ge);. }.}../*.*
15c83 2a 20 44 75 72 69 6e 67 20 61 20 72 6f 6c 6c 62 * During a rollb
15c84 61 63 6b 2c 20 77 68 65 6e 20 74 68 65 20 70 61 ack, when the pa
15c85 67 65 72 20 72 65 6c 6f 61 64 73 20 69 6e 66 6f ger reloads info
15c86 72 6d 61 74 69 6f 6e 20 69 6e 74 6f 20 74 68 65 rmation into the
15c87 20 63 61 63 68 65 0a 2a 2a 20 73 6f 20 74 68 61 cache.** so tha
15c88 74 20 74 68 65 20 63 61 63 68 65 20 69 73 20 72 t the cache is r
15c89 65 73 74 6f 72 65 64 20 74 6f 20 69 74 73 20 6f estored to its o
15c8a 72 69 67 69 6e 61 6c 20 73 74 61 74 65 20 61 74 riginal state at
15c8b 20 74 68 65 20 73 74 61 72 74 20 6f 66 0a 2a 2a the start of.**
15c8c 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e the transaction
15c8d 2c 20 66 6f 72 20 65 61 63 68 20 70 61 67 65 20 , for each page
15c8e 72 65 73 74 6f 72 65 64 20 74 68 69 73 20 72 6f restored this ro
15c8f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e utine is called.
15c90 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
15c91 69 6e 65 20 6e 65 65 64 73 20 74 6f 20 72 65 73 ine needs to res
15c92 65 74 20 74 68 65 20 65 78 74 72 61 20 64 61 74 et the extra dat
15c93 61 20 73 65 63 74 69 6f 6e 20 61 74 20 74 68 65 a section at the
15c94 20 65 6e 64 20 6f 66 20 74 68 65 0a 2a 2a 20 70 end of the.** p
15c95 61 67 65 20 74 6f 20 61 67 72 65 65 20 77 69 74 age to agree wit
15c96 68 20 74 68 65 20 72 65 73 74 6f 72 65 64 20 64 h the restored d
15c97 61 74 61 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ata..*/.static v
15c98 6f 69 64 20 70 61 67 65 52 65 69 6e 69 74 28 44 oid pageReinit(D
15c99 62 50 61 67 65 20 2a 70 44 61 74 61 29 7b 0a 20 bPage *pData){.
15c9a 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b MemPage *pPage;
15c9b 0a 20 20 70 50 61 67 65 20 3d 20 28 4d 65 6d 50 . pPage = (MemP
15c9c 61 67 65 20 2a 29 73 71 6c 69 74 65 33 50 61 67 age *)sqlite3Pag
15c9d 65 72 47 65 74 45 78 74 72 61 28 70 44 61 74 61 erGetExtra(pData
15c9e 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c );. assert( sql
15c9f 69 74 65 33 50 61 67 65 72 50 61 67 65 52 65 66 ite3PagerPageRef
15ca0 63 6f 75 6e 74 28 70 44 61 74 61 29 3e 30 20 29 count(pData)>0 )
15ca1 3b 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e 69 ;. if( pPage->i
15ca2 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 61 73 73 sInit ){. ass
15ca3 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
15ca4 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 ex_held(pPage->p
15ca5 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 Bt->mutex) );.
15ca6 20 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 pPage->isInit
15ca7 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 73 71 6c = 0;. if( sql
15ca8 69 74 65 33 50 61 67 65 72 50 61 67 65 52 65 66 ite3PagerPageRef
15ca9 63 6f 75 6e 74 28 70 44 61 74 61 29 3e 31 20 29 count(pData)>1 )
15caa 7b 0a 20 20 20 20 20 20 2f 2a 20 70 50 61 67 65 {. /* pPage
15cab 20 6d 69 67 68 74 20 6e 6f 74 20 62 65 20 61 20 might not be a
15cac 62 74 72 65 65 20 70 61 67 65 3b 20 20 69 74 20 btree page; it
15cad 6d 69 67 68 74 20 62 65 20 61 6e 20 6f 76 65 72 might be an over
15cae 66 6c 6f 77 20 70 61 67 65 0a 20 20 20 20 20 20 flow page.
15caf 2a 2a 20 6f 72 20 70 74 72 6d 61 70 20 70 61 67 ** or ptrmap pag
15cb0 65 20 6f 72 20 61 20 66 72 65 65 20 70 61 67 65 e or a free page
15cb1 2e 20 20 49 6e 20 74 68 6f 73 65 20 63 61 73 65 . In those case
15cb2 73 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 s, the following
15cb3 0a 20 20 20 20 20 20 2a 2a 20 63 61 6c 6c 20 74 . ** call t
15cb4 6f 20 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 o btreeInitPage(
15cb5 29 20 77 69 6c 6c 20 6c 69 6b 65 6c 79 20 72 65 ) will likely re
15cb6 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 turn SQLITE_CORR
15cb7 55 50 54 2e 0a 20 20 20 20 20 20 2a 2a 20 42 75 UPT.. ** Bu
15cb8 74 20 6e 6f 20 68 61 72 6d 20 69 73 20 64 6f 6e t no harm is don
15cb9 65 20 62 79 20 74 68 69 73 2e 20 20 41 6e 64 20 e by this. And
15cba 69 74 20 69 73 20 76 65 72 79 20 69 6d 70 6f 72 it is very impor
15cbb 74 61 6e 74 20 74 68 61 74 0a 20 20 20 20 20 20 tant that.
15cbc 2a 2a 20 62 74 72 65 65 49 6e 69 74 50 61 67 65 ** btreeInitPage
15cbd 28 29 20 62 65 20 63 61 6c 6c 65 64 20 6f 6e 20 () be called on
15cbe 65 76 65 72 79 20 62 74 72 65 65 20 70 61 67 65 every btree page
15cbf 20 73 6f 20 77 65 20 6d 61 6b 65 0a 20 20 20 20 so we make.
15cc0 20 20 2a 2a 20 74 68 65 20 63 61 6c 6c 20 66 6f ** the call fo
15cc1 72 20 65 76 65 72 79 20 70 61 67 65 20 74 68 61 r every page tha
15cc2 74 20 63 6f 6d 65 73 20 69 6e 20 66 6f 72 20 72 t comes in for r
15cc3 65 2d 69 6e 69 74 69 6e 67 2e 20 2a 2f 0a 20 20 e-initing. */.
15cc4 20 20 20 20 62 74 72 65 65 49 6e 69 74 50 61 67 btreeInitPag
15cc5 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a e(pPage);. }.
15cc6 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 }.}../*.** Inv
15cc7 6f 6b 65 20 74 68 65 20 62 75 73 79 20 68 61 6e oke the busy han
15cc8 64 6c 65 72 20 66 6f 72 20 61 20 62 74 72 65 65 dler for a btree
15cc9 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
15cca 62 74 72 65 65 49 6e 76 6f 6b 65 42 75 73 79 48 btreeInvokeBusyH
15ccb 61 6e 64 6c 65 72 28 76 6f 69 64 20 2a 70 41 72 andler(void *pAr
15ccc 67 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a g){. BtShared *
15ccd 70 42 74 20 3d 20 28 42 74 53 68 61 72 65 64 2a pBt = (BtShared*
15cce 29 70 41 72 67 3b 0a 20 20 61 73 73 65 72 74 28 )pArg;. assert(
15ccf 20 70 42 74 2d 3e 64 62 20 29 3b 0a 20 20 61 73 pBt->db );. as
15cd0 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
15cd1 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 64 62 tex_held(pBt->db
15cd2 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 65 ->mutex) );. re
15cd3 74 75 72 6e 20 73 71 6c 69 74 65 33 49 6e 76 6f turn sqlite3Invo
15cd4 6b 65 42 75 73 79 48 61 6e 64 6c 65 72 28 26 70 keBusyHandler(&p
15cd5 42 74 2d 3e 64 62 2d 3e 62 75 73 79 48 61 6e 64 Bt->db->busyHand
15cd6 6c 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f ler);.}../*.** O
15cd7 70 65 6e 20 61 20 64 61 74 61 62 61 73 65 20 66 pen a database f
15cd8 69 6c 65 2e 0a 2a 2a 20 0a 2a 2a 20 7a 46 69 6c ile..** .** zFil
15cd9 65 6e 61 6d 65 20 69 73 20 74 68 65 20 6e 61 6d ename is the nam
15cda 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 e of the databas
15cdb 65 20 66 69 6c 65 2e 20 20 49 66 20 7a 46 69 6c e file. If zFil
15cdc 65 6e 61 6d 65 20 69 73 20 4e 55 4c 4c 0a 2a 2a ename is NULL.**
15cdd 20 61 20 6e 65 77 20 64 61 74 61 62 61 73 65 20 a new database
15cde 77 69 74 68 20 61 20 72 61 6e 64 6f 6d 20 6e 61 with a random na
15cdf 6d 65 20 69 73 20 63 72 65 61 74 65 64 2e 20 20 me is created.
15ce0 54 68 69 73 20 72 61 6e 64 6f 6d 6c 79 20 6e 61 This randomly na
15ce1 6d 65 64 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 med.** database
15ce2 66 69 6c 65 20 77 69 6c 6c 20 62 65 20 64 65 6c file will be del
15ce3 65 74 65 64 20 77 68 65 6e 20 73 71 6c 69 74 65 eted when sqlite
15ce4 33 42 74 72 65 65 43 6c 6f 73 65 28 29 20 69 73 3BtreeClose() is
15ce5 20 63 61 6c 6c 65 64 2e 0a 2a 2a 20 49 66 20 7a called..** If z
15ce6 46 69 6c 65 6e 61 6d 65 20 69 73 20 22 3a 6d 65 Filename is ":me
15ce7 6d 6f 72 79 3a 22 20 74 68 65 6e 20 61 6e 20 69 mory:" then an i
15ce8 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 n-memory databas
15ce9 65 20 69 73 20 63 72 65 61 74 65 64 0a 2a 2a 20 e is created.**
15cea 74 68 61 74 20 69 73 20 61 75 74 6f 6d 61 74 69 that is automati
15ceb 63 61 6c 6c 79 20 64 65 73 74 72 6f 79 65 64 20 cally destroyed
15cec 77 68 65 6e 20 69 74 20 69 73 20 63 6c 6f 73 65 when it is close
15ced 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 d..**.** If the
15cee 64 61 74 61 62 61 73 65 20 69 73 20 61 6c 72 65 database is alre
15cef 61 64 79 20 6f 70 65 6e 65 64 20 69 6e 20 74 68 ady opened in th
15cf0 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 e same database
15cf1 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 61 6e connection.** an
15cf2 64 20 77 65 20 61 72 65 20 69 6e 20 73 68 61 72 d we are in shar
15cf3 65 64 20 63 61 63 68 65 20 6d 6f 64 65 2c 20 74 ed cache mode, t
15cf4 68 65 6e 20 74 68 65 20 6f 70 65 6e 20 77 69 6c hen the open wil
15cf5 6c 20 66 61 69 6c 20 77 69 74 68 20 61 6e 0a 2a l fail with an.*
15cf6 2a 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 * SQLITE_CONSTRA
15cf7 49 4e 54 20 65 72 72 6f 72 2e 20 20 57 65 20 63 INT error. We c
15cf8 61 6e 6e 6f 74 20 61 6c 6c 6f 77 20 74 77 6f 20 annot allow two
15cf9 6f 72 20 6d 6f 72 65 20 42 74 53 68 61 72 65 64 or more BtShared
15cfa 0a 2a 2a 20 6f 62 6a 65 63 74 73 20 69 6e 20 74 .** objects in t
15cfb 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 he same database
15cfc 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 73 69 6e 63 connection sinc
15cfd 65 20 64 6f 69 6e 67 20 73 6f 20 77 69 6c 6c 20 e doing so will
15cfe 6c 65 61 64 0a 2a 2a 20 74 6f 20 70 72 6f 62 6c lead.** to probl
15cff 65 6d 73 20 77 69 74 68 20 6c 6f 63 6b 69 6e 67 ems with locking
15d00 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
15d01 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
15d02 74 72 65 65 4f 70 65 6e 28 0a 20 20 63 6f 6e 73 treeOpen(. cons
15d03 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d t char *zFilenam
15d04 65 2c 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 e, /* Name of t
15d05 68 65 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 69 he file containi
15d06 6e 67 20 74 68 65 20 42 54 72 65 65 20 64 61 74 ng the BTree dat
15d07 61 62 61 73 65 20 2a 2f 0a 20 20 73 71 6c 69 74 abase */. sqlit
15d08 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 e3 *db,
15d09 20 20 20 2f 2a 20 41 73 73 6f 63 69 61 74 65 64 /* Associated
15d0a 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 database handle
15d0b 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 2a 70 70 */. Btree **pp
15d0c 42 74 72 65 65 2c 20 20 20 20 20 20 20 20 2f 2a Btree, /*
15d0d 20 50 6f 69 6e 74 65 72 20 74 6f 20 6e 65 77 20 Pointer to new
15d0e 42 74 72 65 65 20 6f 62 6a 65 63 74 20 77 72 69 Btree object wri
15d0f 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a 20 20 69 tten here */. i
15d10 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 nt flags,
15d11 20 20 20 20 20 20 20 2f 2a 20 4f 70 74 69 6f 6e /* Option
15d12 73 20 2a 2f 0a 20 20 69 6e 74 20 76 66 73 46 6c s */. int vfsFl
15d13 61 67 73 20 20 20 20 20 20 20 20 20 20 20 20 2f ags /
15d14 2a 20 46 6c 61 67 73 20 70 61 73 73 65 64 20 74 * Flags passed t
15d15 68 72 6f 75 67 68 20 74 6f 20 73 71 6c 69 74 65 hrough to sqlite
15d16 33 5f 76 66 73 2e 78 4f 70 65 6e 28 29 20 2a 2f 3_vfs.xOpen() */
15d17 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 .){. sqlite3_vf
15d18 73 20 2a 70 56 66 73 3b 20 20 20 20 20 20 20 20 s *pVfs;
15d19 20 20 20 20 20 2f 2a 20 54 68 65 20 56 46 53 20 /* The VFS
15d1a 74 6f 20 75 73 65 20 66 6f 72 20 74 68 69 73 20 to use for this
15d1b 62 74 72 65 65 20 2a 2f 0a 20 20 42 74 53 68 61 btree */. BtSha
15d1c 72 65 64 20 2a 70 42 74 20 3d 20 30 3b 20 20 20 red *pBt = 0;
15d1d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 68 61 /* Sha
15d1e 72 65 64 20 70 61 72 74 20 6f 66 20 62 74 72 65 red part of btre
15d1f 65 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 e structure */.
15d20 20 42 74 72 65 65 20 2a 70 3b 20 20 20 20 20 20 Btree *p;
15d21 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15d22 2f 2a 20 48 61 6e 64 6c 65 20 74 6f 20 72 65 74 /* Handle to ret
15d23 75 72 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 urn */. sqlite3
15d24 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 4f 70 65 _mutex *mutexOpe
15d25 6e 20 3d 20 30 3b 20 20 2f 2a 20 50 72 65 76 65 n = 0; /* Preve
15d26 6e 74 73 20 61 20 72 61 63 65 20 63 6f 6e 64 69 nts a race condi
15d27 74 69 6f 6e 2e 20 54 69 63 6b 65 74 20 23 33 35 tion. Ticket #35
15d28 33 37 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 37 */. int rc =
15d29 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 SQLITE_OK;
15d2a 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 /* Result
15d2b 20 63 6f 64 65 20 66 72 6f 6d 20 74 68 69 73 20 code from this
15d2c 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 75 38 function */. u8
15d2d 20 6e 52 65 73 65 72 76 65 3b 20 20 20 20 20 20 nReserve;
15d2e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
15d2f 42 79 74 65 20 6f 66 20 75 6e 75 73 65 64 20 73 Byte of unused s
15d30 70 61 63 65 20 6f 6e 20 65 61 63 68 20 70 61 67 pace on each pag
15d31 65 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 e */. unsigned
15d32 63 68 61 72 20 7a 44 62 48 65 61 64 65 72 5b 31 char zDbHeader[1
15d33 30 30 5d 3b 20 20 2f 2a 20 44 61 74 61 62 61 73 00]; /* Databas
15d34 65 20 68 65 61 64 65 72 20 63 6f 6e 74 65 6e 74 e header content
15d35 20 2a 2f 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 */.. /* Set th
15d36 65 20 76 61 72 69 61 62 6c 65 20 69 73 4d 65 6d e variable isMem
15d37 64 62 20 74 6f 20 74 72 75 65 20 66 6f 72 20 61 db to true for a
15d38 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 n in-memory data
15d39 62 61 73 65 2c 20 6f 72 20 0a 20 20 2a 2a 20 66 base, or . ** f
15d3a 61 6c 73 65 20 66 6f 72 20 61 20 66 69 6c 65 2d alse for a file-
15d3b 62 61 73 65 64 20 64 61 74 61 62 61 73 65 2e 20 based database.
15d3c 54 68 69 73 20 73 79 6d 62 6f 6c 20 69 73 20 6f This symbol is o
15d3d 6e 6c 79 20 72 65 71 75 69 72 65 64 20 69 66 0a nly required if.
15d3e 20 20 2a 2a 20 65 69 74 68 65 72 20 6f 66 20 74 ** either of t
15d3f 68 65 20 73 68 61 72 65 64 2d 64 61 74 61 20 6f he shared-data o
15d40 72 20 61 75 74 6f 76 61 63 75 75 6d 20 66 65 61 r autovacuum fea
15d41 74 75 72 65 73 20 61 72 65 20 63 6f 6d 70 69 6c tures are compil
15d42 65 64 20 0a 20 20 2a 2a 20 69 6e 74 6f 20 74 68 ed . ** into th
15d43 65 20 6c 69 62 72 61 72 79 2e 0a 20 20 2a 2f 0a e library.. */.
15d44 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c #if !defined(SQL
15d45 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f ITE_OMIT_SHARED_
15d46 43 41 43 48 45 29 20 7c 7c 20 21 64 65 66 69 6e CACHE) || !defin
15d47 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ed(SQLITE_OMIT_A
15d48 55 54 4f 56 41 43 55 55 4d 29 0a 20 20 23 69 66 UTOVACUUM). #if
15d49 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
15d4a 4d 45 4d 4f 52 59 44 42 0a 20 20 20 20 63 6f 6e MEMORYDB. con
15d4b 73 74 20 69 6e 74 20 69 73 4d 65 6d 64 62 20 3d st int isMemdb =
15d4c 20 30 3b 0a 20 20 23 65 6c 73 65 0a 20 20 20 20 0;. #else.
15d4d 63 6f 6e 73 74 20 69 6e 74 20 69 73 4d 65 6d 64 const int isMemd
15d4e 62 20 3d 20 7a 46 69 6c 65 6e 61 6d 65 20 26 26 b = zFilename &&
15d4f 20 21 73 74 72 63 6d 70 28 7a 46 69 6c 65 6e 61 !strcmp(zFilena
15d50 6d 65 2c 20 22 3a 6d 65 6d 6f 72 79 3a 22 29 3b me, ":memory:");
15d51 0a 20 20 23 65 6e 64 69 66 0a 23 65 6e 64 69 66 . #endif.#endif
15d52 0a 0a 20 20 61 73 73 65 72 74 28 20 64 62 21 3d .. assert( db!=
15d53 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 0 );. assert( s
15d54 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
15d55 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a d(db->mutex) );.
15d56 0a 20 20 70 56 66 73 20 3d 20 64 62 2d 3e 70 56 . pVfs = db->pV
15d57 66 73 3b 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 fs;. p = sqlite
15d58 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 69 7a 65 3MallocZero(size
15d59 6f 66 28 42 74 72 65 65 29 29 3b 0a 20 20 69 66 of(Btree));. if
15d5a 28 20 21 70 20 29 7b 0a 20 20 20 20 72 65 74 75 ( !p ){. retu
15d5b 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b rn SQLITE_NOMEM;
15d5c 0a 20 20 7d 0a 20 20 70 2d 3e 69 6e 54 72 61 6e . }. p->inTran
15d5d 73 20 3d 20 54 52 41 4e 53 5f 4e 4f 4e 45 3b 0a s = TRANS_NONE;.
15d5e 20 20 70 2d 3e 64 62 20 3d 20 64 62 3b 0a 23 69 p->db = db;.#i
15d5f 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
15d60 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20 T_SHARED_CACHE.
15d61 20 70 2d 3e 6c 6f 63 6b 2e 70 42 74 72 65 65 20 p->lock.pBtree
15d62 3d 20 70 3b 0a 20 20 70 2d 3e 6c 6f 63 6b 2e 69 = p;. p->lock.i
15d63 54 61 62 6c 65 20 3d 20 31 3b 0a 23 65 6e 64 69 Table = 1;.#endi
15d64 66 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 f..#if !defined(
15d65 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 SQLITE_OMIT_SHAR
15d66 45 44 5f 43 41 43 48 45 29 20 26 26 20 21 64 65 ED_CACHE) && !de
15d67 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 fined(SQLITE_OMI
15d68 54 5f 44 49 53 4b 49 4f 29 0a 20 20 2f 2a 0a 20 T_DISKIO). /*.
15d69 20 2a 2a 20 49 66 20 74 68 69 73 20 42 74 72 65 ** If this Btre
15d6a 65 20 69 73 20 61 20 63 61 6e 64 69 64 61 74 65 e is a candidate
15d6b 20 66 6f 72 20 73 68 61 72 65 64 20 63 61 63 68 for shared cach
15d6c 65 2c 20 74 72 79 20 74 6f 20 66 69 6e 64 20 61 e, try to find a
15d6d 6e 0a 20 20 2a 2a 20 65 78 69 73 74 69 6e 67 20 n. ** existing
15d6e 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 20 BtShared object
15d6f 74 68 61 74 20 77 65 20 63 61 6e 20 73 68 61 72 that we can shar
15d70 65 20 77 69 74 68 0a 20 20 2a 2f 0a 20 20 69 66 e with. */. if
15d71 28 20 69 73 4d 65 6d 64 62 3d 3d 30 20 26 26 20 ( isMemdb==0 &&
15d72 7a 46 69 6c 65 6e 61 6d 65 20 26 26 20 7a 46 69 zFilename && zFi
15d73 6c 65 6e 61 6d 65 5b 30 5d 20 29 7b 0a 20 20 20 lename[0] ){.
15d74 20 69 66 28 20 76 66 73 46 6c 61 67 73 20 26 20 if( vfsFlags &
15d75 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 48 41 52 SQLITE_OPEN_SHAR
15d76 45 44 43 41 43 48 45 20 29 7b 0a 20 20 20 20 20 EDCACHE ){.
15d77 20 69 6e 74 20 6e 46 75 6c 6c 50 61 74 68 6e 61 int nFullPathna
15d78 6d 65 20 3d 20 70 56 66 73 2d 3e 6d 78 50 61 74 me = pVfs->mxPat
15d79 68 6e 61 6d 65 2b 31 3b 0a 20 20 20 20 20 20 63 hname+1;. c
15d7a 68 61 72 20 2a 7a 46 75 6c 6c 50 61 74 68 6e 61 har *zFullPathna
15d7b 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c me = sqlite3Mall
15d7c 6f 63 28 6e 46 75 6c 6c 50 61 74 68 6e 61 6d 65 oc(nFullPathname
15d7d 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
15d7e 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 53 68 61 _mutex *mutexSha
15d7f 72 65 64 3b 0a 20 20 20 20 20 20 70 2d 3e 73 68 red;. p->sh
15d80 61 72 61 62 6c 65 20 3d 20 31 3b 0a 20 20 20 20 arable = 1;.
15d81 20 20 69 66 28 20 21 7a 46 75 6c 6c 50 61 74 68 if( !zFullPath
15d82 6e 61 6d 65 20 29 7b 0a 20 20 20 20 20 20 20 20 name ){.
15d83 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b sqlite3_free(p);
15d84 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 . return
15d85 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
15d86 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 }. sqli
15d87 74 65 33 4f 73 46 75 6c 6c 50 61 74 68 6e 61 6d te3OsFullPathnam
15d88 65 28 70 56 66 73 2c 20 7a 46 69 6c 65 6e 61 6d e(pVfs, zFilenam
15d89 65 2c 20 6e 46 75 6c 6c 50 61 74 68 6e 61 6d 65 e, nFullPathname
15d8a 2c 20 7a 46 75 6c 6c 50 61 74 68 6e 61 6d 65 29 , zFullPathname)
15d8b 3b 0a 20 20 20 20 20 20 6d 75 74 65 78 4f 70 65 ;. mutexOpe
15d8c 6e 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 n = sqlite3Mutex
15d8d 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 Alloc(SQLITE_MUT
15d8e 45 58 5f 53 54 41 54 49 43 5f 4f 50 45 4e 29 3b EX_STATIC_OPEN);
15d8f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d . sqlite3_m
15d90 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 utex_enter(mutex
15d91 4f 70 65 6e 29 3b 0a 20 20 20 20 20 20 6d 75 74 Open);. mut
15d92 65 78 53 68 61 72 65 64 20 3d 20 73 71 6c 69 74 exShared = sqlit
15d93 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c e3MutexAlloc(SQL
15d94 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 ITE_MUTEX_STATIC
15d95 5f 4d 41 53 54 45 52 29 3b 0a 20 20 20 20 20 20 _MASTER);.
15d96 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e sqlite3_mutex_en
15d97 74 65 72 28 6d 75 74 65 78 53 68 61 72 65 64 29 ter(mutexShared)
15d98 3b 0a 20 20 20 20 20 20 66 6f 72 28 70 42 74 3d ;. for(pBt=
15d99 47 4c 4f 42 41 4c 28 42 74 53 68 61 72 65 64 2a GLOBAL(BtShared*
15d9a 2c 73 71 6c 69 74 65 33 53 68 61 72 65 64 43 61 ,sqlite3SharedCa
15d9b 63 68 65 4c 69 73 74 29 3b 20 70 42 74 3b 20 70 cheList); pBt; p
15d9c 42 74 3d 70 42 74 2d 3e 70 4e 65 78 74 29 7b 0a Bt=pBt->pNext){.
15d9d 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
15d9e 70 42 74 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 pBt->nRef>0 );.
15d9f 20 20 20 20 20 20 20 69 66 28 20 30 3d 3d 73 74 if( 0==st
15da0 72 63 6d 70 28 7a 46 75 6c 6c 50 61 74 68 6e 61 rcmp(zFullPathna
15da1 6d 65 2c 20 73 71 6c 69 74 65 33 50 61 67 65 72 me, sqlite3Pager
15da2 46 69 6c 65 6e 61 6d 65 28 70 42 74 2d 3e 70 50 Filename(pBt->pP
15da3 61 67 65 72 29 29 0a 20 20 20 20 20 20 20 20 20 ager)).
15da4 20 20 20 20 20 20 20 20 26 26 20 73 71 6c 69 74 && sqlit
15da5 65 33 50 61 67 65 72 56 66 73 28 70 42 74 2d 3e e3PagerVfs(pBt->
15da6 70 50 61 67 65 72 29 3d 3d 70 56 66 73 20 29 7b pPager)==pVfs ){
15da7 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 69 . int i
15da8 44 62 3b 0a 20 20 20 20 20 20 20 20 20 20 66 6f Db;. fo
15da9 72 28 69 44 62 3d 64 62 2d 3e 6e 44 62 2d 31 3b r(iDb=db->nDb-1;
15daa 20 69 44 62 3e 3d 30 3b 20 69 44 62 2d 2d 29 7b iDb>=0; iDb--){
15dab 0a 20 20 20 20 20 20 20 20 20 20 20 20 42 74 72 . Btr
15dac 65 65 20 2a 70 45 78 69 73 74 69 6e 67 20 3d 20 ee *pExisting =
15dad 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74 db->aDb[iDb].pBt
15dae 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 ;. if
15daf 28 20 70 45 78 69 73 74 69 6e 67 20 26 26 20 70 ( pExisting && p
15db0 45 78 69 73 74 69 6e 67 2d 3e 70 42 74 3d 3d 70 Existing->pBt==p
15db1 42 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 Bt ){.
15db2 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 sqlite3_mute
15db3 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 53 68 61 x_leave(mutexSha
15db4 72 65 64 29 3b 0a 20 20 20 20 20 20 20 20 20 20 red);.
15db5 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 sqlite3_mute
15db6 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 4f 70 65 x_leave(mutexOpe
15db7 6e 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 n);.
15db8 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a sqlite3_free(z
15db9 46 75 6c 6c 50 61 74 68 6e 61 6d 65 29 3b 0a 20 FullPathname);.
15dba 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c sql
15dbb 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 ite3_free(p);.
15dbc 20 20 20 20 20 20 20 20 20 20 20 20 72 65 74 75 retu
15dbd 72 6e 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 rn SQLITE_CONSTR
15dbe 41 49 4e 54 3b 0a 20 20 20 20 20 20 20 20 20 20 AINT;.
15dbf 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a }. }.
15dc0 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70 42 74 p->pBt
15dc1 20 3d 20 70 42 74 3b 0a 20 20 20 20 20 20 20 20 = pBt;.
15dc2 20 20 70 42 74 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 pBt->nRef++;.
15dc3 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a break;.
15dc4 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
15dc5 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f }. sqlite3_
15dc6 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 mutex_leave(mute
15dc7 78 53 68 61 72 65 64 29 3b 0a 20 20 20 20 20 20 xShared);.
15dc8 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 46 75 sqlite3_free(zFu
15dc9 6c 6c 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20 llPathname);.
15dca 20 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 }.#ifdef SQLITE
15dcb 5f 44 45 42 55 47 0a 20 20 20 20 65 6c 73 65 7b _DEBUG. else{
15dcc 0a 20 20 20 20 20 20 2f 2a 20 49 6e 20 64 65 62 . /* In deb
15dcd 75 67 20 6d 6f 64 65 2c 20 77 65 20 6d 61 72 6b ug mode, we mark
15dce 20 61 6c 6c 20 70 65 72 73 69 73 74 65 6e 74 20 all persistent
15dcf 64 61 74 61 62 61 73 65 73 20 61 73 20 73 68 61 databases as sha
15dd0 72 61 62 6c 65 0a 20 20 20 20 20 20 2a 2a 20 65 rable. ** e
15dd1 76 65 6e 20 77 68 65 6e 20 74 68 65 79 20 61 72 ven when they ar
15dd2 65 20 6e 6f 74 2e 20 20 54 68 69 73 20 65 78 65 e not. This exe
15dd3 72 63 69 73 65 73 20 74 68 65 20 6c 6f 63 6b 69 rcises the locki
15dd4 6e 67 20 63 6f 64 65 20 61 6e 64 0a 20 20 20 20 ng code and.
15dd5 20 20 2a 2a 20 67 69 76 65 73 20 6d 6f 72 65 20 ** gives more
15dd6 6f 70 70 6f 72 74 75 6e 69 74 79 20 66 6f 72 20 opportunity for
15dd7 61 73 73 65 72 74 73 28 73 71 6c 69 74 65 33 5f asserts(sqlite3_
15dd8 6d 75 74 65 78 5f 68 65 6c 64 28 29 29 0a 20 20 mutex_held()).
15dd9 20 20 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 ** statement
15dda 73 20 74 6f 20 66 69 6e 64 20 6c 6f 63 6b 69 6e s to find lockin
15ddb 67 20 70 72 6f 62 6c 65 6d 73 2e 0a 20 20 20 20 g problems..
15ddc 20 20 2a 2f 0a 20 20 20 20 20 20 70 2d 3e 73 68 */. p->sh
15ddd 61 72 61 62 6c 65 20 3d 20 31 3b 0a 20 20 20 20 arable = 1;.
15dde 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 23 65 6e }.#endif. }.#en
15ddf 64 69 66 0a 20 20 69 66 28 20 70 42 74 3d 3d 30 dif. if( pBt==0
15de0 20 29 7b 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a ){. /*. *
15de1 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * The following
15de2 61 73 73 65 72 74 73 20 6d 61 6b 65 20 73 75 72 asserts make sur
15de3 65 20 74 68 61 74 20 73 74 72 75 63 74 75 72 65 e that structure
15de4 73 20 75 73 65 64 20 62 79 20 74 68 65 20 62 74 s used by the bt
15de5 72 65 65 20 61 72 65 0a 20 20 20 20 2a 2a 20 74 ree are. ** t
15de6 68 65 20 72 69 67 68 74 20 73 69 7a 65 2e 20 20 he right size.
15de7 54 68 69 73 20 69 73 20 74 6f 20 67 75 61 72 64 This is to guard
15de8 20 61 67 61 69 6e 73 74 20 73 69 7a 65 20 63 68 against size ch
15de9 61 6e 67 65 73 20 74 68 61 74 20 72 65 73 75 6c anges that resul
15dea 74 0a 20 20 20 20 2a 2a 20 77 68 65 6e 20 63 6f t. ** when co
15deb 6d 70 69 6c 69 6e 67 20 6f 6e 20 61 20 64 69 66 mpiling on a dif
15dec 66 65 72 65 6e 74 20 61 72 63 68 69 74 65 63 74 ferent architect
15ded 75 72 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ure.. */.
15dee 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 69 assert( sizeof(i
15def 36 34 29 3d 3d 38 20 7c 7c 20 73 69 7a 65 6f 66 64)==8 || sizeof
15df0 28 69 36 34 29 3d 3d 34 20 29 3b 0a 20 20 20 20 (i64)==4 );.
15df1 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 75 assert( sizeof(u
15df2 36 34 29 3d 3d 38 20 7c 7c 20 73 69 7a 65 6f 66 64)==8 || sizeof
15df3 28 75 36 34 29 3d 3d 34 20 29 3b 0a 20 20 20 20 (u64)==4 );.
15df4 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 75 assert( sizeof(u
15df5 33 32 29 3d 3d 34 20 29 3b 0a 20 20 20 20 61 73 32)==4 );. as
15df6 73 65 72 74 28 20 73 69 7a 65 6f 66 28 75 31 36 sert( sizeof(u16
15df7 29 3d 3d 32 20 29 3b 0a 20 20 20 20 61 73 73 65 )==2 );. asse
15df8 72 74 28 20 73 69 7a 65 6f 66 28 50 67 6e 6f 29 rt( sizeof(Pgno)
15df9 3d 3d 34 20 29 3b 0a 20 20 0a 20 20 20 20 70 42 ==4 );. . pB
15dfa 74 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f t = sqlite3Mallo
15dfb 63 5a 65 72 6f 28 20 73 69 7a 65 6f 66 28 2a 70 cZero( sizeof(*p
15dfc 42 74 29 20 29 3b 0a 20 20 20 20 69 66 28 20 70 Bt) );. if( p
15dfd 42 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 Bt==0 ){. r
15dfe 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d c = SQLITE_NOMEM
15dff 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 74 72 ;. goto btr
15e00 65 65 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 ee_open_out;.
15e01 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 }. rc = sqli
15e02 74 65 33 50 61 67 65 72 4f 70 65 6e 28 70 56 66 te3PagerOpen(pVf
15e03 73 2c 20 26 70 42 74 2d 3e 70 50 61 67 65 72 2c s, &pBt->pPager,
15e04 20 7a 46 69 6c 65 6e 61 6d 65 2c 0a 20 20 20 20 zFilename,.
15e05 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15e06 20 20 20 20 20 20 45 58 54 52 41 5f 53 49 5a 45 EXTRA_SIZE
15e07 2c 20 66 6c 61 67 73 2c 20 76 66 73 46 6c 61 67 , flags, vfsFlag
15e08 73 2c 20 70 61 67 65 52 65 69 6e 69 74 29 3b 0a s, pageReinit);.
15e09 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
15e0a 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 TE_OK ){. r
15e0b 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
15e0c 52 65 61 64 46 69 6c 65 68 65 61 64 65 72 28 70 ReadFileheader(p
15e0d 42 74 2d 3e 70 50 61 67 65 72 2c 73 69 7a 65 6f Bt->pPager,sizeo
15e0e 66 28 7a 44 62 48 65 61 64 65 72 29 2c 7a 44 62 f(zDbHeader),zDb
15e0f 48 65 61 64 65 72 29 3b 0a 20 20 20 20 7d 0a 20 Header);. }.
15e10 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
15e11 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 67 6f E_OK ){. go
15e12 74 6f 20 62 74 72 65 65 5f 6f 70 65 6e 5f 6f 75 to btree_open_ou
15e13 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 42 74 t;. }. pBt
15e14 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 20 20 20 73 ->db = db;. s
15e15 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 42 75 qlite3PagerSetBu
15e16 73 79 68 61 6e 64 6c 65 72 28 70 42 74 2d 3e 70 syhandler(pBt->p
15e17 50 61 67 65 72 2c 20 62 74 72 65 65 49 6e 76 6f Pager, btreeInvo
15e18 6b 65 42 75 73 79 48 61 6e 64 6c 65 72 2c 20 70 keBusyHandler, p
15e19 42 74 29 3b 0a 20 20 20 20 70 2d 3e 70 42 74 20 Bt);. p->pBt
15e1a 3d 20 70 42 74 3b 0a 20 20 0a 20 20 20 20 70 42 = pBt;. . pB
15e1b 74 2d 3e 70 43 75 72 73 6f 72 20 3d 20 30 3b 0a t->pCursor = 0;.
15e1c 20 20 20 20 70 42 74 2d 3e 70 50 61 67 65 31 20 pBt->pPage1
15e1d 3d 20 30 3b 0a 20 20 20 20 70 42 74 2d 3e 72 65 = 0;. pBt->re
15e1e 61 64 4f 6e 6c 79 20 3d 20 73 71 6c 69 74 65 33 adOnly = sqlite3
15e1f 50 61 67 65 72 49 73 72 65 61 64 6f 6e 6c 79 28 PagerIsreadonly(
15e20 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 pBt->pPager);.
15e21 20 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 pBt->pageSize
15e22 3d 20 67 65 74 32 62 79 74 65 28 26 7a 44 62 48 = get2byte(&zDbH
15e23 65 61 64 65 72 5b 31 36 5d 29 3b 0a 20 20 20 20 eader[16]);.
15e24 69 66 28 20 70 42 74 2d 3e 70 61 67 65 53 69 7a if( pBt->pageSiz
15e25 65 3c 35 31 32 20 7c 7c 20 70 42 74 2d 3e 70 61 e<512 || pBt->pa
15e26 67 65 53 69 7a 65 3e 53 51 4c 49 54 45 5f 4d 41 geSize>SQLITE_MA
15e27 58 5f 50 41 47 45 5f 53 49 5a 45 0a 20 20 20 20 X_PAGE_SIZE.
15e28 20 20 20 20 20 7c 7c 20 28 28 70 42 74 2d 3e 70 || ((pBt->p
15e29 61 67 65 53 69 7a 65 2d 31 29 26 70 42 74 2d 3e ageSize-1)&pBt->
15e2a 70 61 67 65 53 69 7a 65 29 21 3d 30 20 29 7b 0a pageSize)!=0 ){.
15e2b 20 20 20 20 20 20 70 42 74 2d 3e 70 61 67 65 53 pBt->pageS
15e2c 69 7a 65 20 3d 20 30 3b 0a 23 69 66 6e 64 65 66 ize = 0;.#ifndef
15e2d 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 SQLITE_OMIT_AUT
15e2e 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 20 2f 2a OVACUUM. /*
15e2f 20 49 66 20 74 68 65 20 6d 61 67 69 63 20 6e 61 If the magic na
15e30 6d 65 20 22 3a 6d 65 6d 6f 72 79 3a 22 20 77 69 me ":memory:" wi
15e31 6c 6c 20 63 72 65 61 74 65 20 61 6e 20 69 6e 2d ll create an in-
15e32 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 2c memory database,
15e33 20 74 68 65 6e 0a 20 20 20 20 20 20 2a 2a 20 6c then. ** l
15e34 65 61 76 65 20 74 68 65 20 61 75 74 6f 56 61 63 eave the autoVac
15e35 75 75 6d 20 6d 6f 64 65 20 61 74 20 30 20 28 64 uum mode at 0 (d
15e36 6f 20 6e 6f 74 20 61 75 74 6f 2d 76 61 63 75 75 o not auto-vacuu
15e37 6d 29 2c 20 65 76 65 6e 20 69 66 0a 20 20 20 20 m), even if.
15e38 20 20 2a 2a 20 53 51 4c 49 54 45 5f 44 45 46 41 ** SQLITE_DEFA
15e39 55 4c 54 5f 41 55 54 4f 56 41 43 55 55 4d 20 69 ULT_AUTOVACUUM i
15e3a 73 20 74 72 75 65 2e 20 4f 6e 20 74 68 65 20 6f s true. On the o
15e3b 74 68 65 72 20 68 61 6e 64 2c 20 69 66 0a 20 20 ther hand, if.
15e3c 20 20 20 20 2a 2a 20 53 51 4c 49 54 45 5f 4f 4d ** SQLITE_OM
15e3d 49 54 5f 4d 45 4d 4f 52 59 44 42 20 68 61 73 20 IT_MEMORYDB has
15e3e 62 65 65 6e 20 64 65 66 69 6e 65 64 2c 20 74 68 been defined, th
15e3f 65 6e 20 22 3a 6d 65 6d 6f 72 79 3a 22 20 69 73 en ":memory:" is
15e40 20 6a 75 73 74 20 61 0a 20 20 20 20 20 20 2a 2a just a. **
15e41 20 72 65 67 75 6c 61 72 20 66 69 6c 65 2d 6e 61 regular file-na
15e42 6d 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 me. In this case
15e43 20 74 68 65 20 61 75 74 6f 2d 76 61 63 75 75 6d the auto-vacuum
15e44 20 61 70 70 6c 69 65 73 20 61 73 20 70 65 72 20 applies as per
15e45 6e 6f 72 6d 61 6c 2e 0a 20 20 20 20 20 20 2a 2f normal.. */
15e46 0a 20 20 20 20 20 20 69 66 28 20 7a 46 69 6c 65 . if( zFile
15e47 6e 61 6d 65 20 26 26 20 21 69 73 4d 65 6d 64 62 name && !isMemdb
15e48 20 29 7b 0a 20 20 20 20 20 20 20 20 70 42 74 2d ){. pBt-
15e49 3e 61 75 74 6f 56 61 63 75 75 6d 20 3d 20 28 53 >autoVacuum = (S
15e4a 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 41 55 QLITE_DEFAULT_AU
15e4b 54 4f 56 41 43 55 55 4d 20 3f 20 31 20 3a 20 30 TOVACUUM ? 1 : 0
15e4c 29 3b 0a 20 20 20 20 20 20 20 20 70 42 74 2d 3e );. pBt->
15e4d 69 6e 63 72 56 61 63 75 75 6d 20 3d 20 28 53 51 incrVacuum = (SQ
15e4e 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 41 55 54 LITE_DEFAULT_AUT
15e4f 4f 56 41 43 55 55 4d 3d 3d 32 20 3f 20 31 20 3a OVACUUM==2 ? 1 :
15e50 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 0);. }.#en
15e51 64 69 66 0a 20 20 20 20 20 20 6e 52 65 73 65 72 dif. nReser
15e52 76 65 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 ve = 0;. }els
15e53 65 7b 0a 20 20 20 20 20 20 6e 52 65 73 65 72 76 e{. nReserv
15e54 65 20 3d 20 7a 44 62 48 65 61 64 65 72 5b 32 30 e = zDbHeader[20
15e55 5d 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 70 61 ];. pBt->pa
15e56 67 65 53 69 7a 65 46 69 78 65 64 20 3d 20 31 3b geSizeFixed = 1;
15e57 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
15e58 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a OMIT_AUTOVACUUM.
15e59 20 20 20 20 20 20 70 42 74 2d 3e 61 75 74 6f 56 pBt->autoV
15e5a 61 63 75 75 6d 20 3d 20 28 67 65 74 34 62 79 74 acuum = (get4byt
15e5b 65 28 26 7a 44 62 48 65 61 64 65 72 5b 33 36 20 e(&zDbHeader[36
15e5c 2b 20 34 2a 34 5d 29 3f 31 3a 30 29 3b 0a 20 20 + 4*4])?1:0);.
15e5d 20 20 20 20 70 42 74 2d 3e 69 6e 63 72 56 61 63 pBt->incrVac
15e5e 75 75 6d 20 3d 20 28 67 65 74 34 62 79 74 65 28 uum = (get4byte(
15e5f 26 7a 44 62 48 65 61 64 65 72 5b 33 36 20 2b 20 &zDbHeader[36 +
15e60 37 2a 34 5d 29 3f 31 3a 30 29 3b 0a 23 65 6e 64 7*4])?1:0);.#end
15e61 69 66 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 if. }. rc
15e62 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 = sqlite3PagerSe
15e63 74 50 61 67 65 73 69 7a 65 28 70 42 74 2d 3e 70 tPagesize(pBt->p
15e64 50 61 67 65 72 2c 20 26 70 42 74 2d 3e 70 61 67 Pager, &pBt->pag
15e65 65 53 69 7a 65 2c 20 6e 52 65 73 65 72 76 65 29 eSize, nReserve)
15e66 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 67 ;. if( rc ) g
15e67 6f 74 6f 20 62 74 72 65 65 5f 6f 70 65 6e 5f 6f oto btree_open_o
15e68 75 74 3b 0a 20 20 20 20 70 42 74 2d 3e 75 73 61 ut;. pBt->usa
15e69 62 6c 65 53 69 7a 65 20 3d 20 70 42 74 2d 3e 70 bleSize = pBt->p
15e6a 61 67 65 53 69 7a 65 20 2d 20 6e 52 65 73 65 72 ageSize - nReser
15e6b 76 65 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ve;. assert(
15e6c 28 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 26 (pBt->pageSize &
15e6d 20 37 29 3d 3d 30 20 29 3b 20 20 2f 2a 20 38 2d 7)==0 ); /* 8-
15e6e 62 79 74 65 20 61 6c 69 67 6e 6d 65 6e 74 20 6f byte alignment o
15e6f 66 20 70 61 67 65 53 69 7a 65 20 2a 2f 0a 20 20 f pageSize */.
15e70 20 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 .#if !defined(S
15e71 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 QLITE_OMIT_SHARE
15e72 44 5f 43 41 43 48 45 29 20 26 26 20 21 64 65 66 D_CACHE) && !def
15e73 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 ined(SQLITE_OMIT
15e74 5f 44 49 53 4b 49 4f 29 0a 20 20 20 20 2f 2a 20 _DISKIO). /*
15e75 41 64 64 20 74 68 65 20 6e 65 77 20 42 74 53 68 Add the new BtSh
15e76 61 72 65 64 20 6f 62 6a 65 63 74 20 74 6f 20 74 ared object to t
15e77 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 73 he linked list s
15e78 68 61 72 61 62 6c 65 20 42 74 53 68 61 72 65 64 harable BtShared
15e79 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 s.. */. if
15e7a 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b ( p->sharable ){
15e7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d . sqlite3_m
15e7c 75 74 65 78 20 2a 6d 75 74 65 78 53 68 61 72 65 utex *mutexShare
15e7d 64 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 6e 52 d;. pBt->nR
15e7e 65 66 20 3d 20 31 3b 0a 20 20 20 20 20 20 6d 75 ef = 1;. mu
15e7f 74 65 78 53 68 61 72 65 64 20 3d 20 73 71 6c 69 texShared = sqli
15e80 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 te3MutexAlloc(SQ
15e81 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 LITE_MUTEX_STATI
15e82 43 5f 4d 41 53 54 45 52 29 3b 0a 20 20 20 20 20 C_MASTER);.
15e83 20 69 66 28 20 53 51 4c 49 54 45 5f 54 48 52 45 if( SQLITE_THRE
15e84 41 44 53 41 46 45 20 26 26 20 73 71 6c 69 74 65 ADSAFE && sqlite
15e85 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 43 3GlobalConfig.bC
15e86 6f 72 65 4d 75 74 65 78 20 29 7b 0a 20 20 20 20 oreMutex ){.
15e87 20 20 20 20 70 42 74 2d 3e 6d 75 74 65 78 20 3d pBt->mutex =
15e88 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c sqlite3MutexAll
15e89 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f oc(SQLITE_MUTEX_
15e8a 46 41 53 54 29 3b 0a 20 20 20 20 20 20 20 20 69 FAST);. i
15e8b 66 28 20 70 42 74 2d 3e 6d 75 74 65 78 3d 3d 30 f( pBt->mutex==0
15e8c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 ){. rc
15e8d 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b = SQLITE_NOMEM;
15e8e 0a 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e 6d . db->m
15e8f 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 30 3b allocFailed = 0;
15e90 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 . goto
15e91 62 74 72 65 65 5f 6f 70 65 6e 5f 6f 75 74 3b 0a btree_open_out;.
15e92 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
15e93 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f }. sqlite3_
15e94 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 mutex_enter(mute
15e95 78 53 68 61 72 65 64 29 3b 0a 20 20 20 20 20 20 xShared);.
15e96 70 42 74 2d 3e 70 4e 65 78 74 20 3d 20 47 4c 4f pBt->pNext = GLO
15e97 42 41 4c 28 42 74 53 68 61 72 65 64 2a 2c 73 71 BAL(BtShared*,sq
15e98 6c 69 74 65 33 53 68 61 72 65 64 43 61 63 68 65 lite3SharedCache
15e99 4c 69 73 74 29 3b 0a 20 20 20 20 20 20 47 4c 4f List);. GLO
15e9a 42 41 4c 28 42 74 53 68 61 72 65 64 2a 2c 73 71 BAL(BtShared*,sq
15e9b 6c 69 74 65 33 53 68 61 72 65 64 43 61 63 68 65 lite3SharedCache
15e9c 4c 69 73 74 29 20 3d 20 70 42 74 3b 0a 20 20 20 List) = pBt;.
15e9d 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 sqlite3_mutex
15e9e 5f 6c 65 61 76 65 28 6d 75 74 65 78 53 68 61 72 _leave(mutexShar
15e9f 65 64 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 ed);. }.#endi
15ea0 66 0a 20 20 7d 0a 0a 23 69 66 20 21 64 65 66 69 f. }..#if !defi
15ea1 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f ned(SQLITE_OMIT_
15ea2 53 48 41 52 45 44 5f 43 41 43 48 45 29 20 26 26 SHARED_CACHE) &&
15ea3 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 !defined(SQLITE
15ea4 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f 29 0a 20 20 _OMIT_DISKIO).
15ea5 2f 2a 20 49 66 20 74 68 65 20 6e 65 77 20 42 74 /* If the new Bt
15ea6 72 65 65 20 75 73 65 73 20 61 20 73 68 61 72 61 ree uses a shara
15ea7 62 6c 65 20 70 42 74 53 68 61 72 65 64 2c 20 74 ble pBtShared, t
15ea8 68 65 6e 20 6c 69 6e 6b 20 74 68 65 20 6e 65 77 hen link the new
15ea9 0a 20 20 2a 2a 20 42 74 72 65 65 20 69 6e 74 6f . ** Btree into
15eaa 20 74 68 65 20 6c 69 73 74 20 6f 66 20 61 6c 6c the list of all
15eab 20 73 68 61 72 61 62 6c 65 20 42 74 72 65 65 73 sharable Btrees
15eac 20 66 6f 72 20 74 68 65 20 73 61 6d 65 20 63 6f for the same co
15ead 6e 6e 65 63 74 69 6f 6e 2e 0a 20 20 2a 2a 20 54 nnection.. ** T
15eae 68 65 20 6c 69 73 74 20 69 73 20 6b 65 70 74 20 he list is kept
15eaf 69 6e 20 61 73 63 65 6e 64 69 6e 67 20 6f 72 64 in ascending ord
15eb0 65 72 20 62 79 20 70 42 74 20 61 64 64 72 65 73 er by pBt addres
15eb1 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d s.. */. if( p-
15eb2 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 >sharable ){.
15eb3 20 69 6e 74 20 69 3b 0a 20 20 20 20 42 74 72 65 int i;. Btre
15eb4 65 20 2a 70 53 69 62 3b 0a 20 20 20 20 66 6f 72 e *pSib;. for
15eb5 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b (i=0; i<db->nDb;
15eb6 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 i++){. if(
15eb7 20 28 70 53 69 62 20 3d 20 64 62 2d 3e 61 44 62 (pSib = db->aDb
15eb8 5b 69 5d 2e 70 42 74 29 21 3d 30 20 26 26 20 70 [i].pBt)!=0 && p
15eb9 53 69 62 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b Sib->sharable ){
15eba 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 . while(
15ebb 70 53 69 62 2d 3e 70 50 72 65 76 20 29 7b 20 70 pSib->pPrev ){ p
15ebc 53 69 62 20 3d 20 70 53 69 62 2d 3e 70 50 72 65 Sib = pSib->pPre
15ebd 76 3b 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 v; }. if(
15ebe 20 70 2d 3e 70 42 74 3c 70 53 69 62 2d 3e 70 42 p->pBt<pSib->pB
15ebf 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 t ){. p
15ec0 2d 3e 70 4e 65 78 74 20 3d 20 70 53 69 62 3b 0a ->pNext = pSib;.
15ec1 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70 50 72 p->pPr
15ec2 65 76 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 ev = 0;.
15ec3 20 20 70 53 69 62 2d 3e 70 50 72 65 76 20 3d 20 pSib->pPrev =
15ec4 70 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 p;. }else
15ec5 7b 0a 20 20 20 20 20 20 20 20 20 20 77 68 69 6c {. whil
15ec6 65 28 20 70 53 69 62 2d 3e 70 4e 65 78 74 20 26 e( pSib->pNext &
15ec7 26 20 70 53 69 62 2d 3e 70 4e 65 78 74 2d 3e 70 & pSib->pNext->p
15ec8 42 74 3c 70 2d 3e 70 42 74 20 29 7b 0a 20 20 20 Bt<p->pBt ){.
15ec9 20 20 20 20 20 20 20 20 20 70 53 69 62 20 3d 20 pSib =
15eca 70 53 69 62 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 pSib->pNext;.
15ecb 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
15ecc 20 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 70 53 p->pNext = pS
15ecd 69 62 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 ib->pNext;.
15ece 20 20 20 20 20 70 2d 3e 70 50 72 65 76 20 3d 20 p->pPrev =
15ecf 70 53 69 62 3b 0a 20 20 20 20 20 20 20 20 20 20 pSib;.
15ed0 69 66 28 20 70 2d 3e 70 4e 65 78 74 20 29 7b 0a if( p->pNext ){.
15ed1 20 20 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70 p->p
15ed2 4e 65 78 74 2d 3e 70 50 72 65 76 20 3d 20 70 3b Next->pPrev = p;
15ed3 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 . }.
15ed4 20 20 20 20 20 20 20 70 53 69 62 2d 3e 70 4e 65 pSib->pNe
15ed5 78 74 20 3d 20 70 3b 0a 20 20 20 20 20 20 20 20 xt = p;.
15ed6 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b }. break;
15ed7 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
15ed8 20 7d 0a 23 65 6e 64 69 66 0a 20 20 2a 70 70 42 }.#endif. *ppB
15ed9 74 72 65 65 20 3d 20 70 3b 0a 0a 62 74 72 65 65 tree = p;..btree
15eda 5f 6f 70 65 6e 5f 6f 75 74 3a 0a 20 20 69 66 28 _open_out:. if(
15edb 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
15edc 7b 0a 20 20 20 20 69 66 28 20 70 42 74 20 26 26 {. if( pBt &&
15edd 20 70 42 74 2d 3e 70 50 61 67 65 72 20 29 7b 0a pBt->pPager ){.
15ede 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 sqlite3Pag
15edf 65 72 43 6c 6f 73 65 28 70 42 74 2d 3e 70 50 61 erClose(pBt->pPa
15ee0 67 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 ger);. }.
15ee1 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 42 74 sqlite3_free(pBt
15ee2 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 );. sqlite3_f
15ee3 72 65 65 28 70 29 3b 0a 20 20 20 20 2a 70 70 42 ree(p);. *ppB
15ee4 74 72 65 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 tree = 0;. }.
15ee5 69 66 28 20 6d 75 74 65 78 4f 70 65 6e 20 29 7b if( mutexOpen ){
15ee6 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c . assert( sql
15ee7 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
15ee8 6d 75 74 65 78 4f 70 65 6e 29 20 29 3b 0a 20 20 mutexOpen) );.
15ee9 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
15eea 6c 65 61 76 65 28 6d 75 74 65 78 4f 70 65 6e 29 leave(mutexOpen)
15eeb 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
15eec 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 72 c;.}../*.** Decr
15eed 65 6d 65 6e 74 20 74 68 65 20 42 74 53 68 61 72 ement the BtShar
15eee 65 64 2e 6e 52 65 66 20 63 6f 75 6e 74 65 72 2e ed.nRef counter.
15eef 20 20 57 68 65 6e 20 69 74 20 72 65 61 63 68 65 When it reache
15ef0 73 20 7a 65 72 6f 2c 0a 2a 2a 20 72 65 6d 6f 76 s zero,.** remov
15ef1 65 20 74 68 65 20 42 74 53 68 61 72 65 64 20 73 e the BtShared s
15ef2 74 72 75 63 74 75 72 65 20 66 72 6f 6d 20 74 68 tructure from th
15ef3 65 20 73 68 61 72 69 6e 67 20 6c 69 73 74 2e 20 e sharing list.
15ef4 20 52 65 74 75 72 6e 0a 2a 2a 20 74 72 75 65 20 Return.** true
15ef5 69 66 20 74 68 65 20 42 74 53 68 61 72 65 64 2e if the BtShared.
15ef6 6e 52 65 66 20 63 6f 75 6e 74 65 72 20 72 65 61 nRef counter rea
15ef7 63 68 65 73 20 7a 65 72 6f 20 61 6e 64 20 72 65 ches zero and re
15ef8 74 75 72 6e 0a 2a 2a 20 66 61 6c 73 65 20 69 66 turn.** false if
15ef9 20 69 74 20 69 73 20 73 74 69 6c 6c 20 70 6f 73 it is still pos
15efa 69 74 69 76 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 itive..*/.static
15efb 20 69 6e 74 20 72 65 6d 6f 76 65 46 72 6f 6d 53 int removeFromS
15efc 68 61 72 69 6e 67 4c 69 73 74 28 42 74 53 68 61 haringList(BtSha
15efd 72 65 64 20 2a 70 42 74 29 7b 0a 23 69 66 6e 64 red *pBt){.#ifnd
15efe 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 ef SQLITE_OMIT_S
15eff 48 41 52 45 44 5f 43 41 43 48 45 0a 20 20 73 71 HARED_CACHE. sq
15f00 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 4d 61 lite3_mutex *pMa
15f01 73 74 65 72 3b 0a 20 20 42 74 53 68 61 72 65 64 ster;. BtShared
15f02 20 2a 70 4c 69 73 74 3b 0a 20 20 69 6e 74 20 72 *pList;. int r
15f03 65 6d 6f 76 65 64 20 3d 20 30 3b 0a 0a 20 20 61 emoved = 0;.. a
15f04 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
15f05 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 70 42 74 utex_notheld(pBt
15f06 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 4d ->mutex) );. pM
15f07 61 73 74 65 72 20 3d 20 73 71 6c 69 74 65 33 4d aster = sqlite3M
15f08 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 utexAlloc(SQLITE
15f09 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 _MUTEX_STATIC_MA
15f0a 53 54 45 52 29 3b 0a 20 20 73 71 6c 69 74 65 33 STER);. sqlite3
15f0b 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 4d 61 _mutex_enter(pMa
15f0c 73 74 65 72 29 3b 0a 20 20 70 42 74 2d 3e 6e 52 ster);. pBt->nR
15f0d 65 66 2d 2d 3b 0a 20 20 69 66 28 20 70 42 74 2d ef--;. if( pBt-
15f0e 3e 6e 52 65 66 3c 3d 30 20 29 7b 0a 20 20 20 20 >nRef<=0 ){.
15f0f 69 66 28 20 47 4c 4f 42 41 4c 28 42 74 53 68 61 if( GLOBAL(BtSha
15f10 72 65 64 2a 2c 73 71 6c 69 74 65 33 53 68 61 72 red*,sqlite3Shar
15f11 65 64 43 61 63 68 65 4c 69 73 74 29 3d 3d 70 42 edCacheList)==pB
15f12 74 20 29 7b 0a 20 20 20 20 20 20 47 4c 4f 42 41 t ){. GLOBA
15f13 4c 28 42 74 53 68 61 72 65 64 2a 2c 73 71 6c 69 L(BtShared*,sqli
15f14 74 65 33 53 68 61 72 65 64 43 61 63 68 65 4c 69 te3SharedCacheLi
15f15 73 74 29 20 3d 20 70 42 74 2d 3e 70 4e 65 78 74 st) = pBt->pNext
15f16 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
15f17 20 20 20 70 4c 69 73 74 20 3d 20 47 4c 4f 42 41 pList = GLOBA
15f18 4c 28 42 74 53 68 61 72 65 64 2a 2c 73 71 6c 69 L(BtShared*,sqli
15f19 74 65 33 53 68 61 72 65 64 43 61 63 68 65 4c 69 te3SharedCacheLi
15f1a 73 74 29 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 st);. while
15f1b 28 20 41 4c 57 41 59 53 28 70 4c 69 73 74 29 20 ( ALWAYS(pList)
15f1c 26 26 20 70 4c 69 73 74 2d 3e 70 4e 65 78 74 21 && pList->pNext!
15f1d 3d 70 42 74 20 29 7b 0a 20 20 20 20 20 20 20 20 =pBt ){.
15f1e 70 4c 69 73 74 3d 70 4c 69 73 74 2d 3e 70 4e 65 pList=pList->pNe
15f1f 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 xt;. }.
15f20 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 4c 69 if( ALWAYS(pLi
15f21 73 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 st) ){. p
15f22 4c 69 73 74 2d 3e 70 4e 65 78 74 20 3d 20 70 42 List->pNext = pB
15f23 74 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 t->pNext;.
15f24 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 }. }. if(
15f25 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 SQLITE_THREADSAF
15f26 45 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 E ){. sqlit
15f27 65 33 5f 6d 75 74 65 78 5f 66 72 65 65 28 70 42 e3_mutex_free(pB
15f28 74 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 7d t->mutex);. }
15f29 0a 20 20 20 20 72 65 6d 6f 76 65 64 20 3d 20 31 . removed = 1
15f2a 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f ;. }. sqlite3_
15f2b 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 4d 61 73 mutex_leave(pMas
15f2c 74 65 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 ter);. return r
15f2d 65 6d 6f 76 65 64 3b 0a 23 65 6c 73 65 0a 20 20 emoved;.#else.
15f2e 72 65 74 75 72 6e 20 31 3b 0a 23 65 6e 64 69 66 return 1;.#endif
15f2f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 .}../*.** Make s
15f30 75 72 65 20 70 42 74 2d 3e 70 54 6d 70 53 70 61 ure pBt->pTmpSpa
15f31 63 65 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e 20 ce points to an
15f32 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 0a 2a allocation of .*
15f33 2a 20 4d 58 5f 43 45 4c 4c 5f 53 49 5a 45 28 70 * MX_CELL_SIZE(p
15f34 42 74 29 20 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 Bt) bytes..*/.st
15f35 61 74 69 63 20 76 6f 69 64 20 61 6c 6c 6f 63 61 atic void alloca
15f36 74 65 54 65 6d 70 53 70 61 63 65 28 42 74 53 68 teTempSpace(BtSh
15f37 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 69 66 ared *pBt){. if
15f38 28 20 21 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 ( !pBt->pTmpSpac
15f39 65 20 29 7b 0a 20 20 20 20 70 42 74 2d 3e 70 54 e ){. pBt->pT
15f3a 6d 70 53 70 61 63 65 20 3d 20 73 71 6c 69 74 65 mpSpace = sqlite
15f3b 33 50 61 67 65 4d 61 6c 6c 6f 63 28 20 70 42 74 3PageMalloc( pBt
15f3c 2d 3e 70 61 67 65 53 69 7a 65 20 29 3b 0a 20 20 ->pageSize );.
15f3d 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 }.}../*.** Free
15f3e 74 68 65 20 70 42 74 2d 3e 70 54 6d 70 53 70 61 the pBt->pTmpSpa
15f3f 63 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2f ce allocation.*/
15f40 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 72 65 .static void fre
15f41 65 54 65 6d 70 53 70 61 63 65 28 42 74 53 68 61 eTempSpace(BtSha
15f42 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 73 71 6c red *pBt){. sql
15f43 69 74 65 33 50 61 67 65 46 72 65 65 28 20 70 42 ite3PageFree( pB
15f44 74 2d 3e 70 54 6d 70 53 70 61 63 65 29 3b 0a 20 t->pTmpSpace);.
15f45 20 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 20 pBt->pTmpSpace
15f46 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c = 0;.}../*.** Cl
15f47 6f 73 65 20 61 6e 20 6f 70 65 6e 20 64 61 74 61 ose an open data
15f48 62 61 73 65 20 61 6e 64 20 69 6e 76 61 6c 69 64 base and invalid
15f49 61 74 65 20 61 6c 6c 20 63 75 72 73 6f 72 73 2e ate all cursors.
15f4a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
15f4b 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
15f4c 72 65 65 43 6c 6f 73 65 28 42 74 72 65 65 20 2a reeClose(Btree *
15f4d 70 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a p){. BtShared *
15f4e 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 pBt = p->pBt;.
15f4f 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 3b 0a BtCursor *pCur;.
15f50 0a 20 20 2f 2a 20 43 6c 6f 73 65 20 61 6c 6c 20 . /* Close all
15f51 63 75 72 73 6f 72 73 20 6f 70 65 6e 65 64 20 76 cursors opened v
15f52 69 61 20 74 68 69 73 20 68 61 6e 64 6c 65 2e 20 ia this handle.
15f53 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 73 71 */. assert( sq
15f54 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
15f55 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 (p->db->mutex) )
15f56 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 ;. sqlite3Btree
15f57 45 6e 74 65 72 28 70 29 3b 0a 20 20 70 43 75 72 Enter(p);. pCur
15f58 20 3d 20 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b = pBt->pCursor;
15f59 0a 20 20 77 68 69 6c 65 28 20 70 43 75 72 20 29 . while( pCur )
15f5a 7b 0a 20 20 20 20 42 74 43 75 72 73 6f 72 20 2a {. BtCursor *
15f5b 70 54 6d 70 20 3d 20 70 43 75 72 3b 0a 20 20 20 pTmp = pCur;.
15f5c 20 70 43 75 72 20 3d 20 70 43 75 72 2d 3e 70 4e pCur = pCur->pN
15f5d 65 78 74 3b 0a 20 20 20 20 69 66 28 20 70 54 6d ext;. if( pTm
15f5e 70 2d 3e 70 42 74 72 65 65 3d 3d 70 20 29 7b 0a p->pBtree==p ){.
15f5f 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 sqlite3Btr
15f60 65 65 43 6c 6f 73 65 43 75 72 73 6f 72 28 70 54 eeCloseCursor(pT
15f61 6d 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a mp);. }. }..
15f62 20 20 2f 2a 20 52 6f 6c 6c 62 61 63 6b 20 61 6e /* Rollback an
15f63 79 20 61 63 74 69 76 65 20 74 72 61 6e 73 61 63 y active transac
15f64 74 69 6f 6e 20 61 6e 64 20 66 72 65 65 20 74 68 tion and free th
15f65 65 20 68 61 6e 64 6c 65 20 73 74 72 75 63 74 75 e handle structu
15f66 72 65 2e 0a 20 20 2a 2a 20 54 68 65 20 63 61 6c re.. ** The cal
15f67 6c 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65 l to sqlite3Btre
15f68 65 52 6f 6c 6c 62 61 63 6b 28 29 20 64 72 6f 70 eRollback() drop
15f69 73 20 61 6e 79 20 74 61 62 6c 65 2d 6c 6f 63 6b s any table-lock
15f6a 73 20 68 65 6c 64 20 62 79 0a 20 20 2a 2a 20 74 s held by. ** t
15f6b 68 69 73 20 68 61 6e 64 6c 65 2e 0a 20 20 2a 2f his handle.. */
15f6c 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 52 . sqlite3BtreeR
15f6d 6f 6c 6c 62 61 63 6b 28 70 29 3b 0a 20 20 73 71 ollback(p);. sq
15f6e 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 lite3BtreeLeave(
15f6f 70 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 p);.. /* If the
15f70 72 65 20 61 72 65 20 73 74 69 6c 6c 20 6f 74 68 re are still oth
15f71 65 72 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 72 er outstanding r
15f72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68 65 eferences to the
15f73 20 73 68 61 72 65 64 2d 62 74 72 65 65 0a 20 20 shared-btree.
15f74 2a 2a 20 73 74 72 75 63 74 75 72 65 2c 20 72 65 ** structure, re
15f75 74 75 72 6e 20 6e 6f 77 2e 20 54 68 65 20 72 65 turn now. The re
15f76 6d 61 69 6e 64 65 72 20 6f 66 20 74 68 69 73 20 mainder of this
15f77 70 72 6f 63 65 64 75 72 65 20 63 6c 65 61 6e 73 procedure cleans
15f78 20 0a 20 20 2a 2a 20 75 70 20 74 68 65 20 73 68 . ** up the sh
15f79 61 72 65 64 2d 62 74 72 65 65 2e 0a 20 20 2a 2f ared-btree.. */
15f7a 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 77 61 . assert( p->wa
15f7b 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 26 26 20 70 ntToLock==0 && p
15f7c 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 29 3b 0a 20 ->locked==0 );.
15f7d 20 69 66 28 20 21 70 2d 3e 73 68 61 72 61 62 6c if( !p->sharabl
15f7e 65 20 7c 7c 20 72 65 6d 6f 76 65 46 72 6f 6d 53 e || removeFromS
15f7f 68 61 72 69 6e 67 4c 69 73 74 28 70 42 74 29 20 haringList(pBt)
15f80 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 70 42 ){. /* The pB
15f81 74 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 6f t is no longer o
15f82 6e 20 74 68 65 20 73 68 61 72 69 6e 67 20 6c 69 n the sharing li
15f83 73 74 2c 20 73 6f 20 77 65 20 63 61 6e 20 61 63 st, so we can ac
15f84 63 65 73 73 0a 20 20 20 20 2a 2a 20 69 74 20 77 cess. ** it w
15f85 69 74 68 6f 75 74 20 68 61 76 69 6e 67 20 74 6f ithout having to
15f86 20 68 6f 6c 64 20 74 68 65 20 6d 75 74 65 78 2e hold the mutex.
15f87 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 43 . **. ** C
15f88 6c 65 61 6e 20 6f 75 74 20 61 6e 64 20 64 65 6c lean out and del
15f89 65 74 65 20 74 68 65 20 42 74 53 68 61 72 65 64 ete the BtShared
15f8a 20 6f 62 6a 65 63 74 2e 0a 20 20 20 20 2a 2f 0a object.. */.
15f8b 20 20 20 20 61 73 73 65 72 74 28 20 21 70 42 74 assert( !pBt
15f8c 2d 3e 70 43 75 72 73 6f 72 20 29 3b 0a 20 20 20 ->pCursor );.
15f8d 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6c 6f sqlite3PagerClo
15f8e 73 65 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3b se(pBt->pPager);
15f8f 0a 20 20 20 20 69 66 28 20 70 42 74 2d 3e 78 46 . if( pBt->xF
15f90 72 65 65 53 63 68 65 6d 61 20 26 26 20 70 42 74 reeSchema && pBt
15f91 2d 3e 70 53 63 68 65 6d 61 20 29 7b 0a 20 20 20 ->pSchema ){.
15f92 20 20 20 70 42 74 2d 3e 78 46 72 65 65 53 63 68 pBt->xFreeSch
15f93 65 6d 61 28 70 42 74 2d 3e 70 53 63 68 65 6d 61 ema(pBt->pSchema
15f94 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c );. }. sql
15f95 69 74 65 33 5f 66 72 65 65 28 70 42 74 2d 3e 70 ite3_free(pBt->p
15f96 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 66 72 65 Schema);. fre
15f97 65 54 65 6d 70 53 70 61 63 65 28 70 42 74 29 3b eTempSpace(pBt);
15f98 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 . sqlite3_fre
15f99 65 28 70 42 74 29 3b 0a 20 20 7d 0a 0a 23 69 66 e(pBt);. }..#if
15f9a 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
15f9b 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20 20 _SHARED_CACHE.
15f9c 61 73 73 65 72 74 28 20 70 2d 3e 77 61 6e 74 54 assert( p->wantT
15f9d 6f 4c 6f 63 6b 3d 3d 30 20 29 3b 0a 20 20 61 73 oLock==0 );. as
15f9e 73 65 72 74 28 20 70 2d 3e 6c 6f 63 6b 65 64 3d sert( p->locked=
15f9f 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 70 =0 );. if( p->p
15fa0 50 72 65 76 20 29 20 70 2d 3e 70 50 72 65 76 2d Prev ) p->pPrev-
15fa1 3e 70 4e 65 78 74 20 3d 20 70 2d 3e 70 4e 65 78 >pNext = p->pNex
15fa2 74 3b 0a 20 20 69 66 28 20 70 2d 3e 70 4e 65 78 t;. if( p->pNex
15fa3 74 20 29 20 70 2d 3e 70 4e 65 78 74 2d 3e 70 50 t ) p->pNext->pP
15fa4 72 65 76 20 3d 20 70 2d 3e 70 50 72 65 76 3b 0a rev = p->pPrev;.
15fa5 23 65 6e 64 69 66 0a 0a 20 20 73 71 6c 69 74 65 #endif.. sqlite
15fa6 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 72 65 74 3_free(p);. ret
15fa7 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
15fa8 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 ../*.** Change t
15fa9 68 65 20 6c 69 6d 69 74 20 6f 6e 20 74 68 65 20 he limit on the
15faa 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 number of pages
15fab 61 6c 6c 6f 77 65 64 20 69 6e 20 74 68 65 20 63 allowed in the c
15fac 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ache..**.** The
15fad 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f maximum number o
15fae 66 20 63 61 63 68 65 20 70 61 67 65 73 20 69 73 f cache pages is
15faf 20 73 65 74 20 74 6f 20 74 68 65 20 61 62 73 6f set to the abso
15fb0 6c 75 74 65 0a 2a 2a 20 76 61 6c 75 65 20 6f 66 lute.** value of
15fb1 20 6d 78 50 61 67 65 2e 20 20 49 66 20 6d 78 50 mxPage. If mxP
15fb2 61 67 65 20 69 73 20 6e 65 67 61 74 69 76 65 2c age is negative,
15fb3 20 74 68 65 20 70 61 67 65 72 20 77 69 6c 6c 0a the pager will.
15fb4 2a 2a 20 6f 70 65 72 61 74 65 20 61 73 79 6e 63 ** operate async
15fb5 68 72 6f 6e 6f 75 73 6c 79 20 2d 20 69 74 20 77 hronously - it w
15fb6 69 6c 6c 20 6e 6f 74 20 73 74 6f 70 20 74 6f 20 ill not stop to
15fb7 64 6f 20 66 73 79 6e 63 28 29 73 0a 2a 2a 20 74 do fsync()s.** t
15fb8 6f 20 69 6e 73 75 72 65 20 64 61 74 61 20 69 73 o insure data is
15fb9 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 written to the
15fba 64 69 73 6b 20 73 75 72 66 61 63 65 20 62 65 66 disk surface bef
15fbb 6f 72 65 0a 2a 2a 20 63 6f 6e 74 69 6e 75 69 6e ore.** continuin
15fbc 67 2e 20 20 54 72 61 6e 73 61 63 74 69 6f 6e 73 g. Transactions
15fbd 20 73 74 69 6c 6c 20 77 6f 72 6b 20 69 66 20 73 still work if s
15fbe 79 6e 63 68 72 6f 6e 6f 75 73 20 69 73 20 6f 66 ynchronous is of
15fbf 66 2c 0a 2a 2a 20 61 6e 64 20 74 68 65 20 64 61 f,.** and the da
15fc0 74 61 62 61 73 65 20 63 61 6e 6e 6f 74 20 62 65 tabase cannot be
15fc1 20 63 6f 72 72 75 70 74 65 64 20 69 66 20 74 68 corrupted if th
15fc2 69 73 20 70 72 6f 67 72 61 6d 0a 2a 2a 20 63 72 is program.** cr
15fc3 61 73 68 65 73 2e 20 20 42 75 74 20 69 66 20 74 ashes. But if t
15fc4 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 he operating sys
15fc5 74 65 6d 20 63 72 61 73 68 65 73 20 6f 72 20 74 tem crashes or t
15fc6 68 65 72 65 20 69 73 0a 2a 2a 20 61 6e 20 61 62 here is.** an ab
15fc7 72 75 70 74 20 70 6f 77 65 72 20 66 61 69 6c 75 rupt power failu
15fc8 72 65 20 77 68 65 6e 20 73 79 6e 63 68 72 6f 6e re when synchron
15fc9 6f 75 73 20 69 73 20 6f 66 66 2c 20 74 68 65 20 ous is off, the
15fca 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f 75 6c database.** coul
15fcb 64 20 62 65 20 6c 65 66 74 20 69 6e 20 61 6e 20 d be left in an
15fcc 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20 61 6e 64 inconsistent and
15fcd 20 75 6e 72 65 63 6f 76 65 72 61 62 6c 65 20 73 unrecoverable s
15fce 74 61 74 65 2e 0a 2a 2a 20 53 79 6e 63 68 72 6f tate..** Synchro
15fcf 6e 6f 75 73 20 69 73 20 6f 6e 20 62 79 20 64 65 nous is on by de
15fd0 66 61 75 6c 74 20 73 6f 20 64 61 74 61 62 61 73 fault so databas
15fd1 65 20 63 6f 72 72 75 70 74 69 6f 6e 20 69 73 20 e corruption is
15fd2 6e 6f 74 0a 2a 2a 20 6e 6f 72 6d 61 6c 6c 79 20 not.** normally
15fd3 61 20 77 6f 72 72 79 2e 0a 2a 2f 0a 53 51 4c 49 a worry..*/.SQLI
15fd4 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
15fd5 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 43 61 qlite3BtreeSetCa
15fd6 63 68 65 53 69 7a 65 28 42 74 72 65 65 20 2a 70 cheSize(Btree *p
15fd7 2c 20 69 6e 74 20 6d 78 50 61 67 65 29 7b 0a 20 , int mxPage){.
15fd8 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d BtShared *pBt =
15fd9 20 70 2d 3e 70 42 74 3b 0a 20 20 61 73 73 65 72 p->pBt;. asser
15fda 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
15fdb 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 _held(p->db->mut
15fdc 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 ex) );. sqlite3
15fdd 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 BtreeEnter(p);.
15fde 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 sqlite3PagerSet
15fdf 43 61 63 68 65 73 69 7a 65 28 70 42 74 2d 3e 70 Cachesize(pBt->p
15fe0 50 61 67 65 72 2c 20 6d 78 50 61 67 65 29 3b 0a Pager, mxPage);.
15fe1 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 sqlite3BtreeLe
15fe2 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e ave(p);. return
15fe3 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f SQLITE_OK;.}../
15fe4 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 *.** Change the
15fe5 77 61 79 20 64 61 74 61 20 69 73 20 73 79 6e 63 way data is sync
15fe6 65 64 20 74 6f 20 64 69 73 6b 20 69 6e 20 6f 72 ed to disk in or
15fe7 64 65 72 20 74 6f 20 69 6e 63 72 65 61 73 65 20 der to increase
15fe8 6f 72 20 64 65 63 72 65 61 73 65 0a 2a 2a 20 68 or decrease.** h
15fe9 6f 77 20 77 65 6c 6c 20 74 68 65 20 64 61 74 61 ow well the data
15fea 62 61 73 65 20 72 65 73 69 73 74 73 20 64 61 6d base resists dam
15feb 61 67 65 20 64 75 65 20 74 6f 20 4f 53 20 63 72 age due to OS cr
15fec 61 73 68 65 73 20 61 6e 64 20 70 6f 77 65 72 0a ashes and power.
15fed 2a 2a 20 66 61 69 6c 75 72 65 73 2e 20 20 4c 65 ** failures. Le
15fee 76 65 6c 20 31 20 69 73 20 74 68 65 20 73 61 6d vel 1 is the sam
15fef 65 20 61 73 20 61 73 79 6e 63 68 72 6f 6e 6f 75 e as asynchronou
15ff0 73 20 28 6e 6f 20 73 79 6e 63 73 28 29 20 6f 63 s (no syncs() oc
15ff1 63 75 72 20 61 6e 64 0a 2a 2a 20 74 68 65 72 65 cur and.** there
15ff2 20 69 73 20 61 20 68 69 67 68 20 70 72 6f 62 61 is a high proba
15ff3 62 69 6c 69 74 79 20 6f 66 20 64 61 6d 61 67 65 bility of damage
15ff4 29 20 20 4c 65 76 65 6c 20 32 20 69 73 20 74 68 ) Level 2 is th
15ff5 65 20 64 65 66 61 75 6c 74 2e 20 20 54 68 65 72 e default. Ther
15ff6 65 0a 2a 2a 20 69 73 20 61 20 76 65 72 79 20 6c e.** is a very l
15ff7 6f 77 20 62 75 74 20 6e 6f 6e 2d 7a 65 72 6f 20 ow but non-zero
15ff8 70 72 6f 62 61 62 69 6c 69 74 79 20 6f 66 20 64 probability of d
15ff9 61 6d 61 67 65 2e 20 20 4c 65 76 65 6c 20 33 20 amage. Level 3
15ffa 72 65 64 75 63 65 73 20 74 68 65 0a 2a 2a 20 70 reduces the.** p
15ffb 72 6f 62 61 62 69 6c 69 74 79 20 6f 66 20 64 61 robability of da
15ffc 6d 61 67 65 20 74 6f 20 6e 65 61 72 20 7a 65 72 mage to near zer
15ffd 6f 20 62 75 74 20 77 69 74 68 20 61 20 77 72 69 o but with a wri
15ffe 74 65 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 72 te performance r
15fff 65 64 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 eduction..*/.#if
16000 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
16001 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53 0a 53 _PAGER_PRAGMAS.S
16002 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
16003 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 t sqlite3BtreeSe
16004 74 53 61 66 65 74 79 4c 65 76 65 6c 28 42 74 72 tSafetyLevel(Btr
16005 65 65 20 2a 70 2c 20 69 6e 74 20 6c 65 76 65 6c ee *p, int level
16006 2c 20 69 6e 74 20 66 75 6c 6c 53 79 6e 63 29 7b , int fullSync){
16007 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 . BtShared *pBt
16008 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 61 73 73 = p->pBt;. ass
16009 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
1600a 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d ex_held(p->db->m
1600b 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 utex) );. sqlit
1600c 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b e3BtreeEnter(p);
1600d 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 . sqlite3PagerS
1600e 65 74 53 61 66 65 74 79 4c 65 76 65 6c 28 70 42 etSafetyLevel(pB
1600f 74 2d 3e 70 50 61 67 65 72 2c 20 6c 65 76 65 6c t->pPager, level
16010 2c 20 66 75 6c 6c 53 79 6e 63 29 3b 0a 20 20 73 , fullSync);. s
16011 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 qlite3BtreeLeave
16012 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 (p);. return SQ
16013 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 LITE_OK;.}.#endi
16014 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 f../*.** Return
16015 54 52 55 45 20 69 66 20 74 68 65 20 67 69 76 65 TRUE if the give
16016 6e 20 62 74 72 65 65 20 69 73 20 73 65 74 20 74 n btree is set t
16017 6f 20 73 61 66 65 74 79 20 6c 65 76 65 6c 20 31 o safety level 1
16018 2e 20 20 49 6e 20 6f 74 68 65 72 0a 2a 2a 20 77 . In other.** w
16019 6f 72 64 73 2c 20 72 65 74 75 72 6e 20 54 52 55 ords, return TRU
1601a 45 20 69 66 20 6e 6f 20 73 79 6e 63 28 29 20 6f E if no sync() o
1601b 63 63 75 72 73 20 6f 6e 20 74 68 65 20 64 69 73 ccurs on the dis
1601c 6b 20 66 69 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49 k files..*/.SQLI
1601d 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
1601e 71 6c 69 74 65 33 42 74 72 65 65 53 79 6e 63 44 qlite3BtreeSyncD
1601f 69 73 61 62 6c 65 64 28 42 74 72 65 65 20 2a 70 isabled(Btree *p
16020 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 ){. BtShared *p
16021 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 69 Bt = p->pBt;. i
16022 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74 28 nt rc;. assert(
16023 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
16024 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 eld(p->db->mutex
16025 29 20 29 3b 20 20 0a 20 20 73 71 6c 69 74 65 33 ) ); . sqlite3
16026 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 BtreeEnter(p);.
16027 20 61 73 73 65 72 74 28 20 70 42 74 20 26 26 20 assert( pBt &&
16028 70 42 74 2d 3e 70 50 61 67 65 72 20 29 3b 0a 20 pBt->pPager );.
16029 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
1602a 65 72 4e 6f 73 79 6e 63 28 70 42 74 2d 3e 70 50 erNosync(pBt->pP
1602b 61 67 65 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 ager);. sqlite3
1602c 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 BtreeLeave(p);.
1602d 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 return rc;.}..#
1602e 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 if !defined(SQLI
1602f 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 TE_OMIT_PAGER_PR
16030 41 47 4d 41 53 29 20 7c 7c 20 21 64 65 66 69 6e AGMAS) || !defin
16031 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 ed(SQLITE_OMIT_V
16032 41 43 55 55 4d 29 0a 2f 2a 0a 2a 2a 20 43 68 61 ACUUM)./*.** Cha
16033 6e 67 65 20 74 68 65 20 64 65 66 61 75 6c 74 20 nge the default
16034 70 61 67 65 73 20 73 69 7a 65 20 61 6e 64 20 74 pages size and t
16035 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 73 he number of res
16036 65 72 76 65 64 20 62 79 74 65 73 20 70 65 72 20 erved bytes per
16037 70 61 67 65 2e 0a 2a 2a 20 4f 72 2c 20 69 66 20 page..** Or, if
16038 74 68 65 20 70 61 67 65 20 73 69 7a 65 20 68 61 the page size ha
16039 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 66 s already been f
1603a 69 78 65 64 2c 20 72 65 74 75 72 6e 20 53 51 4c ixed, return SQL
1603b 49 54 45 5f 52 45 41 44 4f 4e 4c 59 20 0a 2a 2a ITE_READONLY .**
1603c 20 77 69 74 68 6f 75 74 20 63 68 61 6e 67 69 6e without changin
1603d 67 20 61 6e 79 74 68 69 6e 67 2e 0a 2a 2a 0a 2a g anything..**.*
1603e 2a 20 54 68 65 20 70 61 67 65 20 73 69 7a 65 20 * The page size
1603f 6d 75 73 74 20 62 65 20 61 20 70 6f 77 65 72 20 must be a power
16040 6f 66 20 32 20 62 65 74 77 65 65 6e 20 35 31 32 of 2 between 512
16041 20 61 6e 64 20 36 35 35 33 36 2e 20 20 49 66 20 and 65536. If
16042 74 68 65 20 70 61 67 65 0a 2a 2a 20 73 69 7a 65 the page.** size
16043 20 73 75 70 70 6c 69 65 64 20 64 6f 65 73 20 6e supplied does n
16044 6f 74 20 6d 65 65 74 20 74 68 69 73 20 63 6f 6e ot meet this con
16045 73 74 72 61 69 6e 74 20 74 68 65 6e 20 74 68 65 straint then the
16046 20 70 61 67 65 20 73 69 7a 65 20 69 73 20 6e 6f page size is no
16047 74 0a 2a 2a 20 63 68 61 6e 67 65 64 2e 0a 2a 2a t.** changed..**
16048 0a 2a 2a 20 50 61 67 65 20 73 69 7a 65 73 20 61 .** Page sizes a
16049 72 65 20 63 6f 6e 73 74 72 61 69 6e 65 64 20 74 re constrained t
1604a 6f 20 62 65 20 61 20 70 6f 77 65 72 20 6f 66 20 o be a power of
1604b 74 77 6f 20 73 6f 20 74 68 61 74 20 74 68 65 20 two so that the
1604c 72 65 67 69 6f 6e 0a 2a 2a 20 6f 66 20 74 68 65 region.** of the
1604d 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 75 database file u
1604e 73 65 64 20 66 6f 72 20 6c 6f 63 6b 69 6e 67 20 sed for locking
1604f 28 62 65 67 69 6e 6e 69 6e 67 20 61 74 20 50 45 (beginning at PE
16050 4e 44 49 4e 47 5f 42 59 54 45 2c 0a 2a 2a 20 74 NDING_BYTE,.** t
16051 68 65 20 66 69 72 73 74 20 62 79 74 65 20 70 61 he first byte pa
16052 73 74 20 74 68 65 20 31 47 42 20 62 6f 75 6e 64 st the 1GB bound
16053 61 72 79 2c 20 30 78 34 30 30 30 30 30 30 30 29 ary, 0x40000000)
16054 20 6e 65 65 64 73 20 74 6f 20 6f 63 63 75 72 0a needs to occur.
16055 2a 2a 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e ** at the beginn
16056 69 6e 67 20 6f 66 20 61 20 70 61 67 65 2e 0a 2a ing of a page..*
16057 2a 0a 2a 2a 20 49 66 20 70 61 72 61 6d 65 74 65 *.** If paramete
16058 72 20 6e 52 65 73 65 72 76 65 20 69 73 20 6c 65 r nReserve is le
16059 73 73 20 74 68 61 6e 20 7a 65 72 6f 2c 20 74 68 ss than zero, th
1605a 65 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 en the number of
1605b 20 72 65 73 65 72 76 65 64 0a 2a 2a 20 62 79 74 reserved.** byt
1605c 65 73 20 70 65 72 20 70 61 67 65 20 69 73 20 6c es per page is l
1605d 65 66 74 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a eft unchanged..*
1605e 2a 0a 2a 2a 20 49 66 20 74 68 65 20 69 46 69 78 *.** If the iFix
1605f 21 3d 30 20 74 68 65 6e 20 74 68 65 20 70 61 67 !=0 then the pag
16060 65 53 69 7a 65 46 69 78 65 64 20 66 6c 61 67 20 eSizeFixed flag
16061 69 73 20 73 65 74 20 73 6f 20 74 68 61 74 20 74 is set so that t
16062 68 65 20 70 61 67 65 20 73 69 7a 65 0a 2a 2a 20 he page size.**
16063 61 6e 64 20 61 75 74 6f 76 61 63 75 75 6d 20 6d and autovacuum m
16064 6f 64 65 20 63 61 6e 20 6e 6f 20 6c 6f 6e 67 65 ode can no longe
16065 72 20 62 65 20 63 68 61 6e 67 65 64 2e 0a 2a 2f r be changed..*/
16066 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
16067 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
16068 53 65 74 50 61 67 65 53 69 7a 65 28 42 74 72 65 SetPageSize(Btre
16069 65 20 2a 70 2c 20 69 6e 74 20 70 61 67 65 53 69 e *p, int pageSi
1606a 7a 65 2c 20 69 6e 74 20 6e 52 65 73 65 72 76 65 ze, int nReserve
1606b 2c 20 69 6e 74 20 69 46 69 78 29 7b 0a 20 20 69 , int iFix){. i
1606c 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
1606d 4b 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 K;. BtShared *p
1606e 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 61 Bt = p->pBt;. a
1606f 73 73 65 72 74 28 20 6e 52 65 73 65 72 76 65 3e ssert( nReserve>
16070 3d 2d 31 20 26 26 20 6e 52 65 73 65 72 76 65 3c =-1 && nReserve<
16071 3d 32 35 35 20 29 3b 0a 20 20 73 71 6c 69 74 65 =255 );. sqlite
16072 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 3BtreeEnter(p);.
16073 20 20 69 66 28 20 70 42 74 2d 3e 70 61 67 65 53 if( pBt->pageS
16074 69 7a 65 46 69 78 65 64 20 29 7b 0a 20 20 20 20 izeFixed ){.
16075 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 sqlite3BtreeLeav
16076 65 28 70 29 3b 0a 20 20 20 20 72 65 74 75 72 6e e(p);. return
16077 20 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 SQLITE_READONLY
16078 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 52 65 73 ;. }. if( nRes
16079 65 72 76 65 3c 30 20 29 7b 0a 20 20 20 20 6e 52 erve<0 ){. nR
1607a 65 73 65 72 76 65 20 3d 20 70 42 74 2d 3e 70 61 eserve = pBt->pa
1607b 67 65 53 69 7a 65 20 2d 20 70 42 74 2d 3e 75 73 geSize - pBt->us
1607c 61 62 6c 65 53 69 7a 65 3b 0a 20 20 7d 0a 20 20 ableSize;. }.
1607d 61 73 73 65 72 74 28 20 6e 52 65 73 65 72 76 65 assert( nReserve
1607e 3e 3d 30 20 26 26 20 6e 52 65 73 65 72 76 65 3c >=0 && nReserve<
1607f 3d 32 35 35 20 29 3b 0a 20 20 69 66 28 20 70 61 =255 );. if( pa
16080 67 65 53 69 7a 65 3e 3d 35 31 32 20 26 26 20 70 geSize>=512 && p
16081 61 67 65 53 69 7a 65 3c 3d 53 51 4c 49 54 45 5f ageSize<=SQLITE_
16082 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 20 26 26 MAX_PAGE_SIZE &&
16083 0a 20 20 20 20 20 20 20 20 28 28 70 61 67 65 53 . ((pageS
16084 69 7a 65 2d 31 29 26 70 61 67 65 53 69 7a 65 29 ize-1)&pageSize)
16085 3d 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 ==0 ){. asser
16086 74 28 20 28 70 61 67 65 53 69 7a 65 20 26 20 37 t( (pageSize & 7
16087 29 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 )==0 );. asse
16088 72 74 28 20 21 70 42 74 2d 3e 70 50 61 67 65 31 rt( !pBt->pPage1
16089 20 26 26 20 21 70 42 74 2d 3e 70 43 75 72 73 6f && !pBt->pCurso
1608a 72 20 29 3b 0a 20 20 20 20 70 42 74 2d 3e 70 61 r );. pBt->pa
1608b 67 65 53 69 7a 65 20 3d 20 28 75 31 36 29 70 61 geSize = (u16)pa
1608c 67 65 53 69 7a 65 3b 0a 20 20 20 20 66 72 65 65 geSize;. free
1608d 54 65 6d 70 53 70 61 63 65 28 70 42 74 29 3b 0a TempSpace(pBt);.
1608e 20 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 }. rc = sqlit
1608f 65 33 50 61 67 65 72 53 65 74 50 61 67 65 73 69 e3PagerSetPagesi
16090 7a 65 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 ze(pBt->pPager,
16091 26 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 2c 20 &pBt->pageSize,
16092 6e 52 65 73 65 72 76 65 29 3b 0a 20 20 70 42 74 nReserve);. pBt
16093 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 70 ->usableSize = p
16094 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d 20 28 Bt->pageSize - (
16095 75 31 36 29 6e 52 65 73 65 72 76 65 3b 0a 20 20 u16)nReserve;.
16096 69 66 28 20 69 46 69 78 20 29 20 70 42 74 2d 3e if( iFix ) pBt->
16097 70 61 67 65 53 69 7a 65 46 69 78 65 64 20 3d 20 pageSizeFixed =
16098 31 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 1;. sqlite3Btre
16099 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 eLeave(p);. ret
1609a 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
1609b 20 52 65 74 75 72 6e 20 74 68 65 20 63 75 72 72 Return the curr
1609c 65 6e 74 6c 79 20 64 65 66 69 6e 65 64 20 70 61 ently defined pa
1609d 67 65 20 73 69 7a 65 0a 2a 2f 0a 53 51 4c 49 54 ge size.*/.SQLIT
1609e 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
1609f 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67 lite3BtreeGetPag
160a0 65 53 69 7a 65 28 42 74 72 65 65 20 2a 70 29 7b eSize(Btree *p){
160a1 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 70 42 74 . return p->pBt
160a2 2d 3e 70 61 67 65 53 69 7a 65 3b 0a 7d 0a 0a 2f ->pageSize;.}../
160a3 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
160a4 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 number of bytes
160a5 6f 66 20 73 70 61 63 65 20 61 74 20 74 68 65 20 of space at the
160a6 65 6e 64 20 6f 66 20 65 76 65 72 79 20 70 61 67 end of every pag
160a7 65 20 74 68 61 74 0a 2a 2a 20 61 72 65 20 69 6e e that.** are in
160a8 74 65 6e 74 75 61 6c 6c 79 20 6c 65 66 74 20 75 tentually left u
160a9 6e 75 73 65 64 2e 20 20 54 68 69 73 20 69 73 20 nused. This is
160aa 74 68 65 20 22 72 65 73 65 72 76 65 64 22 20 73 the "reserved" s
160ab 70 61 63 65 20 74 68 61 74 20 69 73 0a 2a 2a 20 pace that is.**
160ac 73 6f 6d 65 74 69 6d 65 73 20 75 73 65 64 20 62 sometimes used b
160ad 79 20 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f y extensions..*/
160ae 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
160af 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
160b0 47 65 74 52 65 73 65 72 76 65 28 42 74 72 65 65 GetReserve(Btree
160b1 20 2a 70 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20 *p){. int n;.
160b2 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 sqlite3BtreeEnt
160b3 65 72 28 70 29 3b 0a 20 20 6e 20 3d 20 70 2d 3e er(p);. n = p->
160b4 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d 20 pBt->pageSize -
160b5 70 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 p->pBt->usableSi
160b6 7a 65 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 ze;. sqlite3Btr
160b7 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 eeLeave(p);. re
160b8 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a turn n;.}../*.**
160b9 20 53 65 74 20 74 68 65 20 6d 61 78 69 6d 75 6d Set the maximum
160ba 20 70 61 67 65 20 63 6f 75 6e 74 20 66 6f 72 20 page count for
160bb 61 20 64 61 74 61 62 61 73 65 20 69 66 20 6d 78 a database if mx
160bc 50 61 67 65 20 69 73 20 70 6f 73 69 74 69 76 65 Page is positive
160bd 2e 0a 2a 2a 20 4e 6f 20 63 68 61 6e 67 65 73 20 ..** No changes
160be 61 72 65 20 6d 61 64 65 20 69 66 20 6d 78 50 61 are made if mxPa
160bf 67 65 20 69 73 20 30 20 6f 72 20 6e 65 67 61 74 ge is 0 or negat
160c0 69 76 65 2e 0a 2a 2a 20 52 65 67 61 72 64 6c 65 ive..** Regardle
160c1 73 73 20 6f 66 20 74 68 65 20 76 61 6c 75 65 20 ss of the value
160c2 6f 66 20 6d 78 50 61 67 65 2c 20 72 65 74 75 72 of mxPage, retur
160c3 6e 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 70 61 n the maximum pa
160c4 67 65 20 63 6f 75 6e 74 2e 0a 2a 2f 0a 53 51 4c ge count..*/.SQL
160c5 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
160c6 73 71 6c 69 74 65 33 42 74 72 65 65 4d 61 78 50 sqlite3BtreeMaxP
160c7 61 67 65 43 6f 75 6e 74 28 42 74 72 65 65 20 2a ageCount(Btree *
160c8 70 2c 20 69 6e 74 20 6d 78 50 61 67 65 29 7b 0a p, int mxPage){.
160c9 20 20 69 6e 74 20 6e 3b 0a 20 20 73 71 6c 69 74 int n;. sqlit
160ca 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b e3BtreeEnter(p);
160cb 0a 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 50 61 . n = sqlite3Pa
160cc 67 65 72 4d 61 78 50 61 67 65 43 6f 75 6e 74 28 gerMaxPageCount(
160cd 70 2d 3e 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 p->pBt->pPager,
160ce 6d 78 50 61 67 65 29 3b 0a 20 20 73 71 6c 69 74 mxPage);. sqlit
160cf 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b e3BtreeLeave(p);
160d0 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 23 . return n;.}.#
160d1 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 endif /* !define
160d2 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 d(SQLITE_OMIT_PA
160d3 47 45 52 5f 50 52 41 47 4d 41 53 29 20 7c 7c 20 GER_PRAGMAS) ||
160d4 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f !defined(SQLITE_
160d5 4f 4d 49 54 5f 56 41 43 55 55 4d 29 20 2a 2f 0a OMIT_VACUUM) */.
160d6 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 ./*.** Change th
160d7 65 20 27 61 75 74 6f 2d 76 61 63 75 75 6d 27 20 e 'auto-vacuum'
160d8 70 72 6f 70 65 72 74 79 20 6f 66 20 74 68 65 20 property of the
160d9 64 61 74 61 62 61 73 65 2e 20 49 66 20 74 68 65 database. If the
160da 20 27 61 75 74 6f 56 61 63 75 75 6d 27 0a 2a 2a 'autoVacuum'.**
160db 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e 6f parameter is no
160dc 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 61 75 74 n-zero, then aut
160dd 6f 2d 76 61 63 75 75 6d 20 6d 6f 64 65 20 69 73 o-vacuum mode is
160de 20 65 6e 61 62 6c 65 64 2e 20 49 66 20 7a 65 72 enabled. If zer
160df 6f 2c 20 69 74 0a 2a 2a 20 69 73 20 64 69 73 61 o, it.** is disa
160e0 62 6c 65 64 2e 20 54 68 65 20 64 65 66 61 75 6c bled. The defaul
160e1 74 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20 t value for the
160e2 61 75 74 6f 2d 76 61 63 75 75 6d 20 70 72 6f 70 auto-vacuum prop
160e3 65 72 74 79 20 69 73 20 0a 2a 2a 20 64 65 74 65 erty is .** dete
160e4 72 6d 69 6e 65 64 20 62 79 20 74 68 65 20 53 51 rmined by the SQ
160e5 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 41 55 54 LITE_DEFAULT_AUT
160e6 4f 56 41 43 55 55 4d 20 6d 61 63 72 6f 2e 0a 2a OVACUUM macro..*
160e7 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
160e8 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
160e9 65 53 65 74 41 75 74 6f 56 61 63 75 75 6d 28 42 eSetAutoVacuum(B
160ea 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 61 75 74 tree *p, int aut
160eb 6f 56 61 63 75 75 6d 29 7b 0a 23 69 66 64 65 66 oVacuum){.#ifdef
160ec 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 SQLITE_OMIT_AUT
160ed 4f 56 41 43 55 55 4d 0a 20 20 72 65 74 75 72 6e OVACUUM. return
160ee 20 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 SQLITE_READONLY
160ef 3b 0a 23 65 6c 73 65 0a 20 20 42 74 53 68 61 72 ;.#else. BtShar
160f0 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 ed *pBt = p->pBt
160f1 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c ;. int rc = SQL
160f2 49 54 45 5f 4f 4b 3b 0a 20 20 75 38 20 61 76 20 ITE_OK;. u8 av
160f3 3d 20 28 75 38 29 61 75 74 6f 56 61 63 75 75 6d = (u8)autoVacuum
160f4 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 ;.. sqlite3Btre
160f5 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 69 66 28 eEnter(p);. if(
160f6 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 46 69 pBt->pageSizeFi
160f7 78 65 64 20 26 26 20 28 61 76 20 3f 31 3a 30 29 xed && (av ?1:0)
160f8 21 3d 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 !=pBt->autoVacuu
160f9 6d 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 m ){. rc = SQ
160fa 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 LITE_READONLY;.
160fb 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 42 74 2d }else{. pBt-
160fc 3e 61 75 74 6f 56 61 63 75 75 6d 20 3d 20 61 76 >autoVacuum = av
160fd 20 3f 31 3a 30 3b 0a 20 20 20 20 70 42 74 2d 3e ?1:0;. pBt->
160fe 69 6e 63 72 56 61 63 75 75 6d 20 3d 20 61 76 3d incrVacuum = av=
160ff 3d 32 20 3f 31 3a 30 3b 0a 20 20 7d 0a 20 20 73 =2 ?1:0;. }. s
16100 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 qlite3BtreeLeave
16101 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 (p);. return rc
16102 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a ;.#endif.}../*.*
16103 2a 20 52 65 74 75 72 6e 20 74 68 65 20 76 61 6c * Return the val
16104 75 65 20 6f 66 20 74 68 65 20 27 61 75 74 6f 2d ue of the 'auto-
16105 76 61 63 75 75 6d 27 20 70 72 6f 70 65 72 74 79 vacuum' property
16106 2e 20 49 66 20 61 75 74 6f 2d 76 61 63 75 75 6d . If auto-vacuum
16107 20 69 73 20 0a 2a 2a 20 65 6e 61 62 6c 65 64 20 is .** enabled
16108 31 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 4f 1 is returned. O
16109 74 68 65 72 77 69 73 65 20 30 2e 0a 2a 2f 0a 53 therwise 0..*/.S
1610a 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
1610b 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 t sqlite3BtreeGe
1610c 74 41 75 74 6f 56 61 63 75 75 6d 28 42 74 72 65 tAutoVacuum(Btre
1610d 65 20 2a 70 29 7b 0a 23 69 66 64 65 66 20 53 51 e *p){.#ifdef SQ
1610e 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 LITE_OMIT_AUTOVA
1610f 43 55 55 4d 0a 20 20 72 65 74 75 72 6e 20 42 54 CUUM. return BT
16110 52 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 4e REE_AUTOVACUUM_N
16111 4f 4e 45 3b 0a 23 65 6c 73 65 0a 20 20 69 6e 74 ONE;.#else. int
16112 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 rc;. sqlite3Bt
16113 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 72 reeEnter(p);. r
16114 63 20 3d 20 28 0a 20 20 20 20 28 21 70 2d 3e 70 c = (. (!p->p
16115 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 29 3f Bt->autoVacuum)?
16116 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55 4d BTREE_AUTOVACUUM
16117 5f 4e 4f 4e 45 3a 0a 20 20 20 20 28 21 70 2d 3e _NONE:. (!p->
16118 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 29 pBt->incrVacuum)
16119 3f 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55 ?BTREE_AUTOVACUU
1611a 4d 5f 46 55 4c 4c 3a 0a 20 20 20 20 42 54 52 45 M_FULL:. BTRE
1611b 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 49 4e 43 E_AUTOVACUUM_INC
1611c 52 0a 20 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 R. );. sqlite3
1611d 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 BtreeLeave(p);.
1611e 20 72 65 74 75 72 6e 20 72 63 3b 0a 23 65 6e 64 return rc;.#end
1611f 69 66 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 if.}.../*.** Get
16120 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 a reference to
16121 70 50 61 67 65 31 20 6f 66 20 74 68 65 20 64 61 pPage1 of the da
16122 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 54 68 tabase file. Th
16123 69 73 20 77 69 6c 6c 0a 2a 2a 20 61 6c 73 6f 20 is will.** also
16124 61 63 71 75 69 72 65 20 61 20 72 65 61 64 6c 6f acquire a readlo
16125 63 6b 20 6f 6e 20 74 68 61 74 20 66 69 6c 65 2e ck on that file.
16126 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b .**.** SQLITE_OK
16127 20 69 73 20 72 65 74 75 72 6e 65 64 20 6f 6e 20 is returned on
16128 73 75 63 63 65 73 73 2e 20 20 49 66 20 74 68 65 success. If the
16129 20 66 69 6c 65 20 69 73 20 6e 6f 74 20 61 0a 2a file is not a.*
1612a 2a 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 20 64 61 * well-formed da
1612b 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68 65 tabase file, the
1612c 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 n SQLITE_CORRUPT
1612d 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a is returned..**
1612e 20 53 51 4c 49 54 45 5f 42 55 53 59 20 69 73 20 SQLITE_BUSY is
1612f 72 65 74 75 72 6e 65 64 20 69 66 20 74 68 65 20 returned if the
16130 64 61 74 61 62 61 73 65 20 69 73 20 6c 6f 63 6b database is lock
16131 65 64 2e 20 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 ed. SQLITE_NOME
16132 4d 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 M.** is returned
16133 20 69 66 20 77 65 20 72 75 6e 20 6f 75 74 20 6f if we run out o
16134 66 20 6d 65 6d 6f 72 79 2e 20 0a 2a 2f 0a 73 74 f memory. .*/.st
16135 61 74 69 63 20 69 6e 74 20 6c 6f 63 6b 42 74 72 atic int lockBtr
16136 65 65 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 ee(BtShared *pBt
16137 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 4d ){. int rc;. M
16138 65 6d 50 61 67 65 20 2a 70 50 61 67 65 31 3b 0a emPage *pPage1;.
16139 20 20 69 6e 74 20 6e 50 61 67 65 3b 0a 0a 20 20 int nPage;..
1613a 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
1613b 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e mutex_held(pBt->
1613c 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 mutex) );. asse
1613d 72 74 28 20 70 42 74 2d 3e 70 50 61 67 65 31 3d rt( pBt->pPage1=
1613e 3d 30 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c =0 );. rc = sql
1613f 69 74 65 33 50 61 67 65 72 53 68 61 72 65 64 4c ite3PagerSharedL
16140 6f 63 6b 28 70 42 74 2d 3e 70 50 61 67 65 72 29 ock(pBt->pPager)
16141 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 ;. if( rc!=SQLI
16142 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 TE_OK ) return r
16143 63 3b 0a 20 20 72 63 20 3d 20 62 74 72 65 65 47 c;. rc = btreeG
16144 65 74 50 61 67 65 28 70 42 74 2c 20 31 2c 20 26 etPage(pBt, 1, &
16145 70 50 61 67 65 31 2c 20 30 29 3b 0a 20 20 69 66 pPage1, 0);. if
16146 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
16147 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 ) return rc;..
16148 2f 2a 20 44 6f 20 73 6f 6d 65 20 63 68 65 63 6b /* Do some check
16149 69 6e 67 20 74 6f 20 68 65 6c 70 20 69 6e 73 75 ing to help insu
1614a 72 65 20 74 68 65 20 66 69 6c 65 20 77 65 20 6f re the file we o
1614b 70 65 6e 65 64 20 72 65 61 6c 6c 79 20 69 73 0a pened really is.
1614c 20 20 2a 2a 20 61 20 76 61 6c 69 64 20 64 61 74 ** a valid dat
1614d 61 62 61 73 65 20 66 69 6c 65 2e 20 0a 20 20 2a abase file. . *
1614e 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 /. rc = sqlite3
1614f 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 PagerPagecount(p
16150 42 74 2d 3e 70 50 61 67 65 72 2c 20 26 6e 50 61 Bt->pPager, &nPa
16151 67 65 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 ge);. if( rc!=S
16152 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
16153 67 6f 74 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f goto page1_init_
16154 66 61 69 6c 65 64 3b 0a 20 20 7d 65 6c 73 65 20 failed;. }else
16155 69 66 28 20 6e 50 61 67 65 3e 30 20 29 7b 0a 20 if( nPage>0 ){.
16156 20 20 20 69 6e 74 20 70 61 67 65 53 69 7a 65 3b int pageSize;
16157 0a 20 20 20 20 69 6e 74 20 75 73 61 62 6c 65 53 . int usableS
16158 69 7a 65 3b 0a 20 20 20 20 75 38 20 2a 70 61 67 ize;. u8 *pag
16159 65 31 20 3d 20 70 50 61 67 65 31 2d 3e 61 44 61 e1 = pPage1->aDa
1615a 74 61 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c ta;. rc = SQL
1615b 49 54 45 5f 4e 4f 54 41 44 42 3b 0a 20 20 20 20 ITE_NOTADB;.
1615c 69 66 28 20 6d 65 6d 63 6d 70 28 70 61 67 65 31 if( memcmp(page1
1615d 2c 20 7a 4d 61 67 69 63 48 65 61 64 65 72 2c 20 , zMagicHeader,
1615e 31 36 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 16)!=0 ){.
1615f 67 6f 74 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f goto page1_init_
16160 66 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 20 20 failed;. }.
16161 20 20 69 66 28 20 70 61 67 65 31 5b 31 38 5d 3e if( page1[18]>
16162 31 20 29 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e 1 ){. pBt->
16163 72 65 61 64 4f 6e 6c 79 20 3d 20 31 3b 0a 20 20 readOnly = 1;.
16164 20 20 7d 0a 20 20 20 20 69 66 28 20 70 61 67 65 }. if( page
16165 31 5b 31 39 5d 3e 31 20 29 7b 0a 20 20 20 20 20 1[19]>1 ){.
16166 20 67 6f 74 6f 20 70 61 67 65 31 5f 69 6e 69 74 goto page1_init
16167 5f 66 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 0a _failed;. }..
16168 20 20 20 20 2f 2a 20 54 68 65 20 6d 61 78 69 6d /* The maxim
16169 75 6d 20 65 6d 62 65 64 64 65 64 20 66 72 61 63 um embedded frac
1616a 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 65 78 61 tion must be exa
1616b 63 74 6c 79 20 32 35 25 2e 20 20 41 6e 64 20 74 ctly 25%. And t
1616c 68 65 20 6d 69 6e 69 6d 75 6d 0a 20 20 20 20 2a he minimum. *
1616d 2a 20 65 6d 62 65 64 64 65 64 20 66 72 61 63 74 * embedded fract
1616e 69 6f 6e 20 6d 75 73 74 20 62 65 20 31 32 2e 35 ion must be 12.5
1616f 25 20 66 6f 72 20 62 6f 74 68 20 6c 65 61 66 2d % for both leaf-
16170 64 61 74 61 20 61 6e 64 20 6e 6f 6e 2d 6c 65 61 data and non-lea
16171 66 2d 64 61 74 61 2e 0a 20 20 20 20 2a 2a 20 54 f-data.. ** T
16172 68 65 20 6f 72 69 67 69 6e 61 6c 20 64 65 73 69 he original desi
16173 67 6e 20 61 6c 6c 6f 77 65 64 20 74 68 65 73 65 gn allowed these
16174 20 61 6d 6f 75 6e 74 73 20 74 6f 20 76 61 72 79 amounts to vary
16175 2c 20 62 75 74 20 61 73 20 6f 66 0a 20 20 20 20 , but as of.
16176 2a 2a 20 76 65 72 73 69 6f 6e 20 33 2e 36 2e 30 ** version 3.6.0
16177 2c 20 77 65 20 72 65 71 75 69 72 65 20 74 68 65 , we require the
16178 6d 20 74 6f 20 62 65 20 66 69 78 65 64 2e 0a 20 m to be fixed..
16179 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 6d 65 */. if( me
1617a 6d 63 6d 70 28 26 70 61 67 65 31 5b 32 31 5d 2c mcmp(&page1[21],
1617b 20 22 5c 31 30 30 5c 30 34 30 5c 30 34 30 22 2c "\100\040\040",
1617c 33 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 67 3)!=0 ){. g
1617d 6f 74 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f 66 oto page1_init_f
1617e 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 20 20 20 ailed;. }.
1617f 20 70 61 67 65 53 69 7a 65 20 3d 20 67 65 74 32 pageSize = get2
16180 62 79 74 65 28 26 70 61 67 65 31 5b 31 36 5d 29 byte(&page1[16])
16181 3b 0a 20 20 20 20 69 66 28 20 28 28 70 61 67 65 ;. if( ((page
16182 53 69 7a 65 2d 31 29 26 70 61 67 65 53 69 7a 65 Size-1)&pageSize
16183 29 21 3d 30 20 7c 7c 20 70 61 67 65 53 69 7a 65 )!=0 || pageSize
16184 3c 35 31 32 20 7c 7c 0a 20 20 20 20 20 20 20 20 <512 ||.
16185 28 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 (SQLITE_MAX_PAGE
16186 5f 53 49 5a 45 3c 33 32 37 36 38 20 26 26 20 70 _SIZE<32768 && p
16187 61 67 65 53 69 7a 65 3e 53 51 4c 49 54 45 5f 4d ageSize>SQLITE_M
16188 41 58 5f 50 41 47 45 5f 53 49 5a 45 29 0a 20 20 AX_PAGE_SIZE).
16189 20 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 ){. goto
1618a 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 page1_init_faile
1618b 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 d;. }. ass
1618c 65 72 74 28 20 28 70 61 67 65 53 69 7a 65 20 26 ert( (pageSize &
1618d 20 37 29 3d 3d 30 20 29 3b 0a 20 20 20 20 75 73 7)==0 );. us
1618e 61 62 6c 65 53 69 7a 65 20 3d 20 70 61 67 65 53 ableSize = pageS
1618f 69 7a 65 20 2d 20 70 61 67 65 31 5b 32 30 5d 3b ize - page1[20];
16190 0a 20 20 20 20 69 66 28 20 70 61 67 65 53 69 7a . if( pageSiz
16191 65 21 3d 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 e!=pBt->pageSize
16192 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 41 66 74 ){. /* Aft
16193 65 72 20 72 65 61 64 69 6e 67 20 74 68 65 20 66 er reading the f
16194 69 72 73 74 20 70 61 67 65 20 6f 66 20 74 68 65 irst page of the
16195 20 64 61 74 61 62 61 73 65 20 61 73 73 75 6d 69 database assumi
16196 6e 67 20 61 20 70 61 67 65 20 73 69 7a 65 0a 20 ng a page size.
16197 20 20 20 20 20 2a 2a 20 6f 66 20 42 74 53 68 61 ** of BtSha
16198 72 65 64 2e 70 61 67 65 53 69 7a 65 2c 20 77 65 red.pageSize, we
16199 20 68 61 76 65 20 64 69 73 63 6f 76 65 72 65 64 have discovered
1619a 20 74 68 61 74 20 74 68 65 20 70 61 67 65 2d 73 that the page-s
1619b 69 7a 65 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 ize is. **
1619c 61 63 74 75 61 6c 6c 79 20 70 61 67 65 53 69 7a actually pageSiz
1619d 65 2e 20 55 6e 6c 6f 63 6b 20 74 68 65 20 64 61 e. Unlock the da
1619e 74 61 62 61 73 65 2c 20 6c 65 61 76 65 20 70 42 tabase, leave pB
1619f 74 2d 3e 70 50 61 67 65 31 20 61 74 0a 20 20 20 t->pPage1 at.
161a0 20 20 20 2a 2a 20 7a 65 72 6f 20 61 6e 64 20 72 ** zero and r
161a1 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e eturn SQLITE_OK.
161a2 20 54 68 65 20 63 61 6c 6c 65 72 20 77 69 6c 6c The caller will
161a3 20 63 61 6c 6c 20 74 68 69 73 20 66 75 6e 63 74 call this funct
161a4 69 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 61 67 61 ion. ** aga
161a5 69 6e 20 77 69 74 68 20 74 68 65 20 63 6f 72 72 in with the corr
161a6 65 63 74 20 70 61 67 65 2d 73 69 7a 65 2e 0a 20 ect page-size..
161a7 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 65 */. re
161a8 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65 31 leasePage(pPage1
161a9 29 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 75 73 );. pBt->us
161aa 61 62 6c 65 53 69 7a 65 20 3d 20 28 75 31 36 29 ableSize = (u16)
161ab 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20 20 20 usableSize;.
161ac 20 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 pBt->pageSize
161ad 3d 20 28 75 31 36 29 70 61 67 65 53 69 7a 65 3b = (u16)pageSize;
161ae 0a 20 20 20 20 20 20 66 72 65 65 54 65 6d 70 53 . freeTempS
161af 70 61 63 65 28 70 42 74 29 3b 0a 20 20 20 20 20 pace(pBt);.
161b0 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
161b1 65 72 53 65 74 50 61 67 65 73 69 7a 65 28 70 42 erSetPagesize(pB
161b2 74 2d 3e 70 50 61 67 65 72 2c 20 26 70 42 74 2d t->pPager, &pBt-
161b3 3e 70 61 67 65 53 69 7a 65 2c 0a 20 20 20 20 20 >pageSize,.
161b4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
161b5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 61 pa
161b6 67 65 53 69 7a 65 2d 75 73 61 62 6c 65 53 69 7a geSize-usableSiz
161b7 65 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e e);. return
161b8 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 rc;. }. i
161b9 66 28 20 75 73 61 62 6c 65 53 69 7a 65 3c 34 38 f( usableSize<48
161ba 30 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 0 ){. goto
161bb 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 page1_init_faile
161bc 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 42 74 d;. }. pBt
161bd 2d 3e 70 61 67 65 53 69 7a 65 20 3d 20 28 75 31 ->pageSize = (u1
161be 36 29 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 6)pageSize;.
161bf 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 pBt->usableSize
161c0 3d 20 28 75 31 36 29 75 73 61 62 6c 65 53 69 7a = (u16)usableSiz
161c1 65 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 e;.#ifndef SQLIT
161c2 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 E_OMIT_AUTOVACUU
161c3 4d 0a 20 20 20 20 70 42 74 2d 3e 61 75 74 6f 56 M. pBt->autoV
161c4 61 63 75 75 6d 20 3d 20 28 67 65 74 34 62 79 74 acuum = (get4byt
161c5 65 28 26 70 61 67 65 31 5b 33 36 20 2b 20 34 2a e(&page1[36 + 4*
161c6 34 5d 29 3f 31 3a 30 29 3b 0a 20 20 20 20 70 42 4])?1:0);. pB
161c7 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 20 3d 20 t->incrVacuum =
161c8 28 67 65 74 34 62 79 74 65 28 26 70 61 67 65 31 (get4byte(&page1
161c9 5b 33 36 20 2b 20 37 2a 34 5d 29 3f 31 3a 30 29 [36 + 7*4])?1:0)
161ca 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 20 20 ;.#endif. }..
161cb 2f 2a 20 6d 61 78 4c 6f 63 61 6c 20 69 73 20 74 /* maxLocal is t
161cc 68 65 20 6d 61 78 69 6d 75 6d 20 61 6d 6f 75 6e he maximum amoun
161cd 74 20 6f 66 20 70 61 79 6c 6f 61 64 20 74 6f 20 t of payload to
161ce 73 74 6f 72 65 20 6c 6f 63 61 6c 6c 79 20 66 6f store locally fo
161cf 72 0a 20 20 2a 2a 20 61 20 63 65 6c 6c 2e 20 20 r. ** a cell.
161d0 4d 61 6b 65 20 73 75 72 65 20 69 74 20 69 73 20 Make sure it is
161d1 73 6d 61 6c 6c 20 65 6e 6f 75 67 68 20 73 6f 20 small enough so
161d2 74 68 61 74 20 61 74 20 6c 65 61 73 74 20 6d 69 that at least mi
161d3 6e 46 61 6e 6f 75 74 0a 20 20 2a 2a 20 63 65 6c nFanout. ** cel
161d4 6c 73 20 63 61 6e 20 77 69 6c 6c 20 66 69 74 20 ls can will fit
161d5 6f 6e 20 6f 6e 65 20 70 61 67 65 2e 20 20 57 65 on one page. We
161d6 20 61 73 73 75 6d 65 20 61 20 31 30 2d 62 79 74 assume a 10-byt
161d7 65 20 70 61 67 65 20 68 65 61 64 65 72 2e 0a 20 e page header..
161d8 20 2a 2a 20 42 65 73 69 64 65 73 20 74 68 65 20 ** Besides the
161d9 70 61 79 6c 6f 61 64 2c 20 74 68 65 20 63 65 6c payload, the cel
161da 6c 20 6d 75 73 74 20 73 74 6f 72 65 3a 0a 20 20 l must store:.
161db 2a 2a 20 20 20 20 20 32 2d 62 79 74 65 20 70 6f ** 2-byte po
161dc 69 6e 74 65 72 20 74 6f 20 74 68 65 20 63 65 6c inter to the cel
161dd 6c 0a 20 20 2a 2a 20 20 20 20 20 34 2d 62 79 74 l. ** 4-byt
161de 65 20 63 68 69 6c 64 20 70 6f 69 6e 74 65 72 0a e child pointer.
161df 20 20 2a 2a 20 20 20 20 20 39 2d 62 79 74 65 20 ** 9-byte
161e0 6e 4b 65 79 20 76 61 6c 75 65 0a 20 20 2a 2a 20 nKey value. **
161e1 20 20 20 20 34 2d 62 79 74 65 20 6e 44 61 74 61 4-byte nData
161e2 20 76 61 6c 75 65 0a 20 20 2a 2a 20 20 20 20 20 value. **
161e3 34 2d 62 79 74 65 20 6f 76 65 72 66 6c 6f 77 20 4-byte overflow
161e4 70 61 67 65 20 70 6f 69 6e 74 65 72 0a 20 20 2a page pointer. *
161e5 2a 20 53 6f 20 61 20 63 65 6c 6c 20 63 6f 6e 73 * So a cell cons
161e6 69 73 74 73 20 6f 66 20 61 20 32 2d 62 79 74 65 ists of a 2-byte
161e7 20 70 6f 69 6e 65 72 2c 20 61 20 68 65 61 64 65 poiner, a heade
161e8 72 20 77 68 69 63 68 20 69 73 20 61 73 20 6d 75 r which is as mu
161e9 63 68 20 61 73 0a 20 20 2a 2a 20 31 37 20 62 79 ch as. ** 17 by
161ea 74 65 73 20 6c 6f 6e 67 2c 20 30 20 74 6f 20 4e tes long, 0 to N
161eb 20 62 79 74 65 73 20 6f 66 20 70 61 79 6c 6f 61 bytes of payloa
161ec 64 2c 20 61 6e 64 20 61 6e 20 6f 70 74 69 6f 6e d, and an option
161ed 61 6c 20 34 20 62 79 74 65 20 6f 76 65 72 66 6c al 4 byte overfl
161ee 6f 77 0a 20 20 2a 2a 20 70 61 67 65 20 70 6f 69 ow. ** page poi
161ef 6e 74 65 72 2e 0a 20 20 2a 2f 0a 20 20 70 42 74 nter.. */. pBt
161f0 2d 3e 6d 61 78 4c 6f 63 61 6c 20 3d 20 28 70 42 ->maxLocal = (pB
161f1 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2d 31 32 t->usableSize-12
161f2 29 2a 36 34 2f 32 35 35 20 2d 20 32 33 3b 0a 20 )*64/255 - 23;.
161f3 20 70 42 74 2d 3e 6d 69 6e 4c 6f 63 61 6c 20 3d pBt->minLocal =
161f4 20 28 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a (pBt->usableSiz
161f5 65 2d 31 32 29 2a 33 32 2f 32 35 35 20 2d 20 32 e-12)*32/255 - 2
161f6 33 3b 0a 20 20 70 42 74 2d 3e 6d 61 78 4c 65 61 3;. pBt->maxLea
161f7 66 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 f = pBt->usableS
161f8 69 7a 65 20 2d 20 33 35 3b 0a 20 20 70 42 74 2d ize - 35;. pBt-
161f9 3e 6d 69 6e 4c 65 61 66 20 3d 20 28 70 42 74 2d >minLeaf = (pBt-
161fa 3e 75 73 61 62 6c 65 53 69 7a 65 2d 31 32 29 2a >usableSize-12)*
161fb 33 32 2f 32 35 35 20 2d 20 32 33 3b 0a 20 20 61 32/255 - 23;. a
161fc 73 73 65 72 74 28 20 70 42 74 2d 3e 6d 61 78 4c ssert( pBt->maxL
161fd 65 61 66 20 2b 20 32 33 20 3c 3d 20 4d 58 5f 43 eaf + 23 <= MX_C
161fe 45 4c 4c 5f 53 49 5a 45 28 70 42 74 29 20 29 3b ELL_SIZE(pBt) );
161ff 0a 20 20 70 42 74 2d 3e 70 50 61 67 65 31 20 3d . pBt->pPage1 =
16200 20 70 50 61 67 65 31 3b 0a 20 20 72 65 74 75 72 pPage1;. retur
16201 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 70 61 n SQLITE_OK;..pa
16202 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64 3a ge1_init_failed:
16203 0a 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 . releasePage(p
16204 50 61 67 65 31 29 3b 0a 20 20 70 42 74 2d 3e 70 Page1);. pBt->p
16205 50 61 67 65 31 20 3d 20 30 3b 0a 20 20 72 65 74 Page1 = 0;. ret
16206 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
16207 20 49 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f If there are no
16208 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 63 75 72 outstanding cur
16209 73 6f 72 73 20 61 6e 64 20 77 65 20 61 72 65 20 sors and we are
1620a 6e 6f 74 20 69 6e 20 74 68 65 20 6d 69 64 64 6c not in the middl
1620b 65 0a 2a 2a 20 6f 66 20 61 20 74 72 61 6e 73 61 e.** of a transa
1620c 63 74 69 6f 6e 20 62 75 74 20 74 68 65 72 65 20 ction but there
1620d 69 73 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 6f is a read lock o
1620e 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20 n the database,
1620f 74 68 65 6e 0a 2a 2a 20 74 68 69 73 20 72 6f 75 then.** this rou
16210 74 69 6e 65 20 75 6e 72 65 66 73 20 74 68 65 20 tine unrefs the
16211 66 69 72 73 74 20 70 61 67 65 20 6f 66 20 74 68 first page of th
16212 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
16213 77 68 69 63 68 20 0a 2a 2a 20 68 61 73 20 74 68 which .** has th
16214 65 20 65 66 66 65 63 74 20 6f 66 20 72 65 6c 65 e effect of rele
16215 61 73 69 6e 67 20 74 68 65 20 72 65 61 64 20 6c asing the read l
16216 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 ock..**.** If th
16217 65 72 65 20 69 73 20 61 20 74 72 61 6e 73 61 63 ere is a transac
16218 74 69 6f 6e 20 69 6e 20 70 72 6f 67 72 65 73 73 tion in progress
16219 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 , this routine i
1621a 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 s a no-op..*/.st
1621b 61 74 69 63 20 76 6f 69 64 20 75 6e 6c 6f 63 6b atic void unlock
1621c 42 74 72 65 65 49 66 55 6e 75 73 65 64 28 42 74 BtreeIfUnused(Bt
1621d 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 Shared *pBt){.
1621e 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
1621f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e mutex_held(pBt->
16220 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 mutex) );. asse
16221 72 74 28 20 70 42 74 2d 3e 70 43 75 72 73 6f 72 rt( pBt->pCursor
16222 3d 3d 30 20 7c 7c 20 70 42 74 2d 3e 69 6e 54 72 ==0 || pBt->inTr
16223 61 6e 73 61 63 74 69 6f 6e 3e 54 52 41 4e 53 5f ansaction>TRANS_
16224 4e 4f 4e 45 20 29 3b 0a 20 20 69 66 28 20 70 42 NONE );. if( pB
16225 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e t->inTransaction
16226 3d 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 26 26 20 ==TRANS_NONE &&
16227 70 42 74 2d 3e 70 50 61 67 65 31 21 3d 30 20 29 pBt->pPage1!=0 )
16228 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 42 {. assert( pB
16229 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 t->pPage1->aData
1622a 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
1622b 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 63 sqlite3PagerRefc
1622c 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 ount(pBt->pPager
1622d 29 3d 3d 31 20 29 3b 0a 20 20 20 20 61 73 73 65 )==1 );. asse
1622e 72 74 28 20 70 42 74 2d 3e 70 50 61 67 65 31 2d rt( pBt->pPage1-
1622f 3e 61 44 61 74 61 20 29 3b 0a 20 20 20 20 72 65 >aData );. re
16230 6c 65 61 73 65 50 61 67 65 28 70 42 74 2d 3e 70 leasePage(pBt->p
16231 50 61 67 65 31 29 3b 0a 20 20 20 20 70 42 74 2d Page1);. pBt-
16232 3e 70 50 61 67 65 31 20 3d 20 30 3b 0a 20 20 7d >pPage1 = 0;. }
16233 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 70 42 74 .}../*.** If pBt
16234 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e 20 65 6d points to an em
16235 70 74 79 20 66 69 6c 65 20 74 68 65 6e 20 63 6f pty file then co
16236 6e 76 65 72 74 20 74 68 61 74 20 65 6d 70 74 79 nvert that empty
16237 20 66 69 6c 65 0a 2a 2a 20 69 6e 74 6f 20 61 20 file.** into a
16238 6e 65 77 20 65 6d 70 74 79 20 64 61 74 61 62 61 new empty databa
16239 73 65 20 62 79 20 69 6e 69 74 69 61 6c 69 7a 69 se by initializi
1623a 6e 67 20 74 68 65 20 66 69 72 73 74 20 70 61 67 ng the first pag
1623b 65 20 6f 66 0a 2a 2a 20 74 68 65 20 64 61 74 61 e of.** the data
1623c 62 61 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 base..*/.static
1623d 69 6e 74 20 6e 65 77 44 61 74 61 62 61 73 65 28 int newDatabase(
1623e 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a BtShared *pBt){.
1623f 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 31 3b 0a MemPage *pP1;.
16240 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 unsigned char
16241 2a 64 61 74 61 3b 0a 20 20 69 6e 74 20 72 63 3b *data;. int rc;
16242 0a 20 20 69 6e 74 20 6e 50 61 67 65 3b 0a 0a 20 . int nPage;..
16243 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
16244 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d _mutex_held(pBt-
16245 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 2f 2a 20 >mutex) );. /*
16246 54 68 65 20 64 61 74 61 62 61 73 65 20 73 69 7a The database siz
16247 65 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 e has already be
16248 65 6e 20 6d 65 61 73 75 72 65 64 20 61 6e 64 20 en measured and
16249 63 61 63 68 65 64 2c 20 73 6f 20 66 61 69 6c 75 cached, so failu
1624a 72 65 0a 20 20 2a 2a 20 69 73 20 69 6d 70 6f 73 re. ** is impos
1624b 73 69 62 6c 65 20 68 65 72 65 2e 20 20 49 66 20 sible here. If
1624c 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 73 69 7a the original siz
1624d 65 20 6d 65 61 73 75 72 65 6d 65 6e 74 20 66 61 e measurement fa
1624e 69 6c 65 64 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 iled, then. **
1624f 70 72 6f 63 65 73 73 69 6e 67 20 61 62 6f 72 74 processing abort
16250 73 20 62 65 66 6f 72 65 20 65 6e 74 65 72 69 6e s before enterin
16251 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 g this routine.
16252 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 */. rc = sqlite
16253 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 3PagerPagecount(
16254 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 26 6e 50 pBt->pPager, &nP
16255 61 67 65 29 3b 0a 20 20 69 66 28 20 4e 45 56 45 age);. if( NEVE
16256 52 28 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 29 R(rc!=SQLITE_OK)
16257 20 7c 7c 20 6e 50 61 67 65 3e 30 20 29 7b 0a 20 || nPage>0 ){.
16258 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
16259 7d 0a 20 20 70 50 31 20 3d 20 70 42 74 2d 3e 70 }. pP1 = pBt->p
1625a 50 61 67 65 31 3b 0a 20 20 61 73 73 65 72 74 28 Page1;. assert(
1625b 20 70 50 31 21 3d 30 20 29 3b 0a 20 20 64 61 74 pP1!=0 );. dat
1625c 61 20 3d 20 70 50 31 2d 3e 61 44 61 74 61 3b 0a a = pP1->aData;.
1625d 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
1625e 67 65 72 57 72 69 74 65 28 70 50 31 2d 3e 70 44 gerWrite(pP1->pD
1625f 62 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 bPage);. if( rc
16260 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 ) return rc;.
16261 6d 65 6d 63 70 79 28 64 61 74 61 2c 20 7a 4d 61 memcpy(data, zMa
16262 67 69 63 48 65 61 64 65 72 2c 20 73 69 7a 65 6f gicHeader, sizeo
16263 66 28 7a 4d 61 67 69 63 48 65 61 64 65 72 29 29 f(zMagicHeader))
16264 3b 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 ;. assert( size
16265 6f 66 28 7a 4d 61 67 69 63 48 65 61 64 65 72 29 of(zMagicHeader)
16266 3d 3d 31 36 20 29 3b 0a 20 20 70 75 74 32 62 79 ==16 );. put2by
16267 74 65 28 26 64 61 74 61 5b 31 36 5d 2c 20 70 42 te(&data[16], pB
16268 74 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 t->pageSize);.
16269 64 61 74 61 5b 31 38 5d 20 3d 20 31 3b 0a 20 20 data[18] = 1;.
1626a 64 61 74 61 5b 31 39 5d 20 3d 20 31 3b 0a 20 20 data[19] = 1;.
1626b 61 73 73 65 72 74 28 20 70 42 74 2d 3e 75 73 61 assert( pBt->usa
1626c 62 6c 65 53 69 7a 65 3c 3d 70 42 74 2d 3e 70 61 bleSize<=pBt->pa
1626d 67 65 53 69 7a 65 20 26 26 20 70 42 74 2d 3e 75 geSize && pBt->u
1626e 73 61 62 6c 65 53 69 7a 65 2b 32 35 35 3e 3d 70 sableSize+255>=p
1626f 42 74 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 Bt->pageSize);.
16270 20 64 61 74 61 5b 32 30 5d 20 3d 20 28 75 38 29 data[20] = (u8)
16271 28 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d (pBt->pageSize -
16272 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 pBt->usableSize
16273 29 3b 0a 20 20 64 61 74 61 5b 32 31 5d 20 3d 20 );. data[21] =
16274 36 34 3b 0a 20 20 64 61 74 61 5b 32 32 5d 20 3d 64;. data[22] =
16275 20 33 32 3b 0a 20 20 64 61 74 61 5b 32 33 5d 20 32;. data[23]
16276 3d 20 33 32 3b 0a 20 20 6d 65 6d 73 65 74 28 26 = 32;. memset(&
16277 64 61 74 61 5b 32 34 5d 2c 20 30 2c 20 31 30 30 data[24], 0, 100
16278 2d 32 34 29 3b 0a 20 20 7a 65 72 6f 50 61 67 65 -24);. zeroPage
16279 28 70 50 31 2c 20 50 54 46 5f 49 4e 54 4b 45 59 (pP1, PTF_INTKEY
1627a 7c 50 54 46 5f 4c 45 41 46 7c 50 54 46 5f 4c 45 |PTF_LEAF|PTF_LE
1627b 41 46 44 41 54 41 20 29 3b 0a 20 20 70 42 74 2d AFDATA );. pBt-
1627c 3e 70 61 67 65 53 69 7a 65 46 69 78 65 64 20 3d >pageSizeFixed =
1627d 20 31 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 1;.#ifndef SQLI
1627e 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 TE_OMIT_AUTOVACU
1627f 55 4d 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 UM. assert( pBt
16280 2d 3e 61 75 74 6f 56 61 63 75 75 6d 3d 3d 31 20 ->autoVacuum==1
16281 7c 7c 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 || pBt->autoVacu
16282 75 6d 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 um==0 );. asser
16283 74 28 20 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 t( pBt->incrVacu
16284 75 6d 3d 3d 31 20 7c 7c 20 70 42 74 2d 3e 69 6e um==1 || pBt->in
16285 63 72 56 61 63 75 75 6d 3d 3d 30 20 29 3b 0a 20 crVacuum==0 );.
16286 20 70 75 74 34 62 79 74 65 28 26 64 61 74 61 5b put4byte(&data[
16287 33 36 20 2b 20 34 2a 34 5d 2c 20 70 42 74 2d 3e 36 + 4*4], pBt->
16288 61 75 74 6f 56 61 63 75 75 6d 29 3b 0a 20 20 70 autoVacuum);. p
16289 75 74 34 62 79 74 65 28 26 64 61 74 61 5b 33 36 ut4byte(&data[36
1628a 20 2b 20 37 2a 34 5d 2c 20 70 42 74 2d 3e 69 6e + 7*4], pBt->in
1628b 63 72 56 61 63 75 75 6d 29 3b 0a 23 65 6e 64 69 crVacuum);.#endi
1628c 66 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 f. return SQLIT
1628d 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 E_OK;.}../*.** A
1628e 74 74 65 6d 70 74 20 74 6f 20 73 74 61 72 74 20 ttempt to start
1628f 61 20 6e 65 77 20 74 72 61 6e 73 61 63 74 69 6f a new transactio
16290 6e 2e 20 41 20 77 72 69 74 65 2d 74 72 61 6e 73 n. A write-trans
16291 61 63 74 69 6f 6e 0a 2a 2a 20 69 73 20 73 74 61 action.** is sta
16292 72 74 65 64 20 69 66 20 74 68 65 20 73 65 63 6f rted if the seco
16293 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6e nd argument is n
16294 6f 6e 7a 65 72 6f 2c 20 6f 74 68 65 72 77 69 73 onzero, otherwis
16295 65 20 61 20 72 65 61 64 2d 0a 2a 2a 20 74 72 61 e a read-.** tra
16296 6e 73 61 63 74 69 6f 6e 2e 20 20 49 66 20 74 68 nsaction. If th
16297 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e e second argumen
16298 74 20 69 73 20 32 20 6f 72 20 6d 6f 72 65 20 61 t is 2 or more a
16299 6e 64 20 65 78 63 6c 75 73 69 76 65 0a 2a 2a 20 nd exclusive.**
1629a 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 transaction is s
1629b 74 61 72 74 65 64 2c 20 6d 65 61 6e 69 6e 67 20 tarted, meaning
1629c 74 68 61 74 20 6e 6f 20 6f 74 68 65 72 20 70 72 that no other pr
1629d 6f 63 65 73 73 20 69 73 20 61 6c 6c 6f 77 65 64 ocess is allowed
1629e 0a 2a 2a 20 74 6f 20 61 63 63 65 73 73 20 74 68 .** to access th
1629f 65 20 64 61 74 61 62 61 73 65 2e 20 20 41 20 70 e database. A p
162a0 72 65 65 78 69 73 74 69 6e 67 20 74 72 61 6e 73 reexisting trans
162a1 61 63 74 69 6f 6e 20 6d 61 79 20 6e 6f 74 20 62 action may not b
162a2 65 0a 2a 2a 20 75 70 67 72 61 64 65 64 20 74 6f e.** upgraded to
162a3 20 65 78 63 6c 75 73 69 76 65 20 62 79 20 63 61 exclusive by ca
162a4 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69 lling this routi
162a5 6e 65 20 61 20 73 65 63 6f 6e 64 20 74 69 6d 65 ne a second time
162a6 20 2d 20 74 68 65 0a 2a 2a 20 65 78 63 6c 75 73 - the.** exclus
162a7 69 76 69 74 79 20 66 6c 61 67 20 6f 6e 6c 79 20 ivity flag only
162a8 77 6f 72 6b 73 20 66 6f 72 20 61 20 6e 65 77 20 works for a new
162a9 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a transaction..**.
162aa 2a 2a 20 41 20 77 72 69 74 65 2d 74 72 61 6e 73 ** A write-trans
162ab 61 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 73 action must be s
162ac 74 61 72 74 65 64 20 62 65 66 6f 72 65 20 61 74 tarted before at
162ad 74 65 6d 70 74 69 6e 67 20 61 6e 79 20 0a 2a 2a tempting any .**
162ae 20 63 68 61 6e 67 65 73 20 74 6f 20 74 68 65 20 changes to the
162af 64 61 74 61 62 61 73 65 2e 20 20 4e 6f 6e 65 20 database. None
162b0 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 of the following
162b1 20 72 6f 75 74 69 6e 65 73 20 0a 2a 2a 20 77 69 routines .** wi
162b2 6c 6c 20 77 6f 72 6b 20 75 6e 6c 65 73 73 20 61 ll work unless a
162b3 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 transaction is
162b4 73 74 61 72 74 65 64 20 66 69 72 73 74 3a 0a 2a started first:.*
162b5 2a 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 *.** sqlite
162b6 33 42 74 72 65 65 43 72 65 61 74 65 54 61 62 6c 3BtreeCreateTabl
162b7 65 28 29 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 e().** sqli
162b8 74 65 33 42 74 72 65 65 43 72 65 61 74 65 49 6e te3BtreeCreateIn
162b9 64 65 78 28 29 0a 2a 2a 20 20 20 20 20 20 73 71 dex().** sq
162ba 6c 69 74 65 33 42 74 72 65 65 43 6c 65 61 72 54 lite3BtreeClearT
162bb 61 62 6c 65 28 29 0a 2a 2a 20 20 20 20 20 20 73 able().** s
162bc 71 6c 69 74 65 33 42 74 72 65 65 44 72 6f 70 54 qlite3BtreeDropT
162bd 61 62 6c 65 28 29 0a 2a 2a 20 20 20 20 20 20 73 able().** s
162be 71 6c 69 74 65 33 42 74 72 65 65 49 6e 73 65 72 qlite3BtreeInser
162bf 74 28 29 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 t().** sqli
162c0 74 65 33 42 74 72 65 65 44 65 6c 65 74 65 28 29 te3BtreeDelete()
162c1 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 33 .** sqlite3
162c2 42 74 72 65 65 55 70 64 61 74 65 4d 65 74 61 28 BtreeUpdateMeta(
162c3 29 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 69 6e ).**.** If an in
162c4 69 74 69 61 6c 20 61 74 74 65 6d 70 74 20 74 6f itial attempt to
162c5 20 61 63 71 75 69 72 65 20 74 68 65 20 6c 6f 63 acquire the loc
162c6 6b 20 66 61 69 6c 73 20 62 65 63 61 75 73 65 20 k fails because
162c7 6f 66 20 6c 6f 63 6b 20 63 6f 6e 74 65 6e 74 69 of lock contenti
162c8 6f 6e 0a 2a 2a 20 61 6e 64 20 74 68 65 20 64 61 on.** and the da
162c9 74 61 62 61 73 65 20 77 61 73 20 70 72 65 76 69 tabase was previ
162ca 6f 75 73 6c 79 20 75 6e 6c 6f 63 6b 65 64 2c 20 ously unlocked,
162cb 74 68 65 6e 20 69 6e 76 6f 6b 65 20 74 68 65 20 then invoke the
162cc 62 75 73 79 20 68 61 6e 64 6c 65 72 0a 2a 2a 20 busy handler.**
162cd 69 66 20 74 68 65 72 65 20 69 73 20 6f 6e 65 2e if there is one.
162ce 20 20 42 75 74 20 69 66 20 74 68 65 72 65 20 77 But if there w
162cf 61 73 20 70 72 65 76 69 6f 75 73 6c 79 20 61 20 as previously a
162d0 72 65 61 64 2d 6c 6f 63 6b 2c 20 64 6f 20 6e 6f read-lock, do no
162d1 74 0a 2a 2a 20 69 6e 76 6f 6b 65 20 74 68 65 20 t.** invoke the
162d2 62 75 73 79 20 68 61 6e 64 6c 65 72 20 2d 20 6a busy handler - j
162d3 75 73 74 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ust return SQLIT
162d4 45 5f 42 55 53 59 2e 20 20 53 51 4c 49 54 45 5f E_BUSY. SQLITE_
162d5 42 55 53 59 20 69 73 20 0a 2a 2a 20 72 65 74 75 BUSY is .** retu
162d6 72 6e 65 64 20 77 68 65 6e 20 74 68 65 72 65 20 rned when there
162d7 69 73 20 61 6c 72 65 61 64 79 20 61 20 72 65 61 is already a rea
162d8 64 2d 6c 6f 63 6b 20 69 6e 20 6f 72 64 65 72 20 d-lock in order
162d9 74 6f 20 61 76 6f 69 64 20 61 20 64 65 61 64 6c to avoid a deadl
162da 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 53 75 70 70 6f ock..**.** Suppo
162db 73 65 20 74 68 65 72 65 20 61 72 65 20 74 77 6f se there are two
162dc 20 70 72 6f 63 65 73 73 65 73 20 41 20 61 6e 64 processes A and
162dd 20 42 2e 20 20 41 20 68 61 73 20 61 20 72 65 61 B. A has a rea
162de 64 20 6c 6f 63 6b 20 61 6e 64 20 42 20 68 61 73 d lock and B has
162df 0a 2a 2a 20 61 20 72 65 73 65 72 76 65 64 20 6c .** a reserved l
162e0 6f 63 6b 2e 20 20 42 20 74 72 69 65 73 20 74 6f ock. B tries to
162e1 20 70 72 6f 6d 6f 74 65 20 74 6f 20 65 78 63 6c promote to excl
162e2 75 73 69 76 65 20 62 75 74 20 69 73 20 62 6c 6f usive but is blo
162e3 63 6b 65 64 20 62 65 63 61 75 73 65 0a 2a 2a 20 cked because.**
162e4 6f 66 20 41 27 73 20 72 65 61 64 20 6c 6f 63 6b of A's read lock
162e5 2e 20 20 41 20 74 72 69 65 73 20 74 6f 20 70 72 . A tries to pr
162e6 6f 6d 6f 74 65 20 74 6f 20 72 65 73 65 72 76 65 omote to reserve
162e7 64 20 62 75 74 20 69 73 20 62 6c 6f 63 6b 65 64 d but is blocked
162e8 20 62 79 20 42 2e 0a 2a 2a 20 4f 6e 65 20 6f 72 by B..** One or
162e9 20 74 68 65 20 6f 74 68 65 72 20 6f 66 20 74 68 the other of th
162ea 65 20 74 77 6f 20 70 72 6f 63 65 73 73 65 73 20 e two processes
162eb 6d 75 73 74 20 67 69 76 65 20 77 61 79 20 6f 72 must give way or
162ec 20 74 68 65 72 65 20 63 61 6e 20 62 65 0a 2a 2a there can be.**
162ed 20 6e 6f 20 70 72 6f 67 72 65 73 73 2e 20 20 42 no progress. B
162ee 79 20 72 65 74 75 72 6e 69 6e 67 20 53 51 4c 49 y returning SQLI
162ef 54 45 5f 42 55 53 59 20 61 6e 64 20 6e 6f 74 20 TE_BUSY and not
162f0 69 6e 76 6f 6b 69 6e 67 20 74 68 65 20 62 75 73 invoking the bus
162f1 79 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 77 68 y callback.** wh
162f2 65 6e 20 41 20 61 6c 72 65 61 64 79 20 68 61 73 en A already has
162f3 20 61 20 72 65 61 64 20 6c 6f 63 6b 2c 20 77 65 a read lock, we
162f4 20 65 6e 63 6f 75 72 61 67 65 20 41 20 74 6f 20 encourage A to
162f5 67 69 76 65 20 75 70 20 61 6e 64 20 6c 65 74 20 give up and let
162f6 42 0a 2a 2a 20 70 72 6f 63 65 65 64 2e 0a 2a 2f B.** proceed..*/
162f7 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
162f8 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
162f9 42 65 67 69 6e 54 72 61 6e 73 28 42 74 72 65 65 BeginTrans(Btree
162fa 20 2a 70 2c 20 69 6e 74 20 77 72 66 6c 61 67 29 *p, int wrflag)
162fb 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 70 42 6c {. sqlite3 *pBl
162fc 6f 63 6b 20 3d 20 30 3b 0a 20 20 42 74 53 68 61 ock = 0;. BtSha
162fd 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 red *pBt = p->pB
162fe 74 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 t;. int rc = SQ
162ff 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 73 71 6c 69 LITE_OK;.. sqli
16300 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 te3BtreeEnter(p)
16301 3b 0a 20 20 62 74 72 65 65 49 6e 74 65 67 72 69 ;. btreeIntegri
16302 74 79 28 70 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 ty(p);.. /* If
16303 74 68 65 20 62 74 72 65 65 20 69 73 20 61 6c 72 the btree is alr
16304 65 61 64 79 20 69 6e 20 61 20 77 72 69 74 65 2d eady in a write-
16305 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 6f 72 20 transaction, or
16306 69 74 0a 20 20 2a 2a 20 69 73 20 61 6c 72 65 61 it. ** is alrea
16307 64 79 20 69 6e 20 61 20 72 65 61 64 2d 74 72 61 dy in a read-tra
16308 6e 73 61 63 74 69 6f 6e 20 61 6e 64 20 61 20 72 nsaction and a r
16309 65 61 64 2d 74 72 61 6e 73 61 63 74 69 6f 6e 0a ead-transaction.
1630a 20 20 2a 2a 20 69 73 20 72 65 71 75 65 73 74 65 ** is requeste
1630b 64 2c 20 74 68 69 73 20 69 73 20 61 20 6e 6f 2d d, this is a no-
1630c 6f 70 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 op.. */. if( p
1630d 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 ->inTrans==TRANS
1630e 5f 57 52 49 54 45 20 7c 7c 20 28 70 2d 3e 69 6e _WRITE || (p->in
1630f 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 52 45 41 Trans==TRANS_REA
16310 44 20 26 26 20 21 77 72 66 6c 61 67 29 20 29 7b D && !wrflag) ){
16311 0a 20 20 20 20 67 6f 74 6f 20 74 72 61 6e 73 5f . goto trans_
16312 62 65 67 75 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a begun;. }.. /*
16313 20 57 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 Write transacti
16314 6f 6e 73 20 61 72 65 20 6e 6f 74 20 70 6f 73 73 ons are not poss
16315 69 62 6c 65 20 6f 6e 20 61 20 72 65 61 64 2d 6f ible on a read-o
16316 6e 6c 79 20 64 61 74 61 62 61 73 65 20 2a 2f 0a nly database */.
16317 20 20 69 66 28 20 70 42 74 2d 3e 72 65 61 64 4f if( pBt->readO
16318 6e 6c 79 20 26 26 20 77 72 66 6c 61 67 20 29 7b nly && wrflag ){
16319 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 . rc = SQLITE
1631a 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 20 20 67 _READONLY;. g
1631b 6f 74 6f 20 74 72 61 6e 73 5f 62 65 67 75 6e 3b oto trans_begun;
1631c 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 . }..#ifndef SQ
1631d 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 LITE_OMIT_SHARED
1631e 5f 43 41 43 48 45 0a 20 20 2f 2a 20 49 66 20 61 _CACHE. /* If a
1631f 6e 6f 74 68 65 72 20 64 61 74 61 62 61 73 65 20 nother database
16320 68 61 6e 64 6c 65 20 68 61 73 20 61 6c 72 65 61 handle has alrea
16321 64 79 20 6f 70 65 6e 65 64 20 61 20 77 72 69 74 dy opened a writ
16322 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 0a 20 e transaction .
16323 20 2a 2a 20 6f 6e 20 74 68 69 73 20 73 68 61 72 ** on this shar
16324 65 64 2d 62 74 72 65 65 20 73 74 72 75 63 74 75 ed-btree structu
16325 72 65 20 61 6e 64 20 61 20 73 65 63 6f 6e 64 20 re and a second
16326 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f write transactio
16327 6e 20 69 73 0a 20 20 2a 2a 20 72 65 71 75 65 73 n is. ** reques
16328 74 65 64 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 ted, return SQLI
16329 54 45 5f 4c 4f 43 4b 45 44 2e 0a 20 20 2a 2f 0a TE_LOCKED.. */.
1632a 20 20 69 66 28 20 28 77 72 66 6c 61 67 20 26 26 if( (wrflag &&
1632b 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 pBt->inTransact
1632c 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 ion==TRANS_WRITE
1632d 29 20 7c 7c 20 70 42 74 2d 3e 69 73 50 65 6e 64 ) || pBt->isPend
1632e 69 6e 67 20 29 7b 0a 20 20 20 20 70 42 6c 6f 63 ing ){. pBloc
1632f 6b 20 3d 20 70 42 74 2d 3e 70 57 72 69 74 65 72 k = pBt->pWriter
16330 2d 3e 64 62 3b 0a 20 20 7d 65 6c 73 65 20 69 66 ->db;. }else if
16331 28 20 77 72 66 6c 61 67 3e 31 20 29 7b 0a 20 20 ( wrflag>1 ){.
16332 20 20 42 74 4c 6f 63 6b 20 2a 70 49 74 65 72 3b BtLock *pIter;
16333 0a 20 20 20 20 66 6f 72 28 70 49 74 65 72 3d 70 . for(pIter=p
16334 42 74 2d 3e 70 4c 6f 63 6b 3b 20 70 49 74 65 72 Bt->pLock; pIter
16335 3b 20 70 49 74 65 72 3d 70 49 74 65 72 2d 3e 70 ; pIter=pIter->p
16336 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 69 66 28 Next){. if(
16337 20 70 49 74 65 72 2d 3e 70 42 74 72 65 65 21 3d pIter->pBtree!=
16338 70 20 29 7b 0a 20 20 20 20 20 20 20 20 70 42 6c p ){. pBl
16339 6f 63 6b 20 3d 20 70 49 74 65 72 2d 3e 70 42 74 ock = pIter->pBt
1633a 72 65 65 2d 3e 64 62 3b 0a 20 20 20 20 20 20 20 ree->db;.
1633b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a break;. }.
1633c 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 }. }. if(
1633d 70 42 6c 6f 63 6b 20 29 7b 0a 20 20 20 20 73 71 pBlock ){. sq
1633e 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 lite3ConnectionB
1633f 6c 6f 63 6b 65 64 28 70 2d 3e 64 62 2c 20 70 42 locked(p->db, pB
16340 6c 6f 63 6b 29 3b 0a 20 20 20 20 72 63 20 3d 20 lock);. rc =
16341 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 SQLITE_LOCKED_SH
16342 41 52 45 44 43 41 43 48 45 3b 0a 20 20 20 20 67 AREDCACHE;. g
16343 6f 74 6f 20 74 72 61 6e 73 5f 62 65 67 75 6e 3b oto trans_begun;
16344 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f . }.#endif.. /
16345 2a 20 41 6e 79 20 72 65 61 64 2d 6f 6e 6c 79 20 * Any read-only
16346 6f 72 20 72 65 61 64 2d 77 72 69 74 65 20 74 72 or read-write tr
16347 61 6e 73 61 63 74 69 6f 6e 20 69 6d 70 6c 69 65 ansaction implie
16348 73 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e s a read-lock on
16349 20 0a 20 20 2a 2a 20 70 61 67 65 20 31 2e 20 53 . ** page 1. S
1634a 6f 20 69 66 20 73 6f 6d 65 20 6f 74 68 65 72 20 o if some other
1634b 73 68 61 72 65 64 2d 63 61 63 68 65 20 63 6c 69 shared-cache cli
1634c 65 6e 74 20 61 6c 72 65 61 64 79 20 68 61 73 20 ent already has
1634d 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 0a 20 20 a write-lock .
1634e 2a 2a 20 6f 6e 20 70 61 67 65 20 31 2c 20 74 68 ** on page 1, th
1634f 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 61 e transaction ca
16350 6e 6e 6f 74 20 62 65 20 6f 70 65 6e 65 64 2e 20 nnot be opened.
16351 2a 2f 0a 20 20 72 63 20 3d 20 71 75 65 72 79 53 */. rc = queryS
16352 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c haredCacheTableL
16353 6f 63 6b 28 70 2c 20 4d 41 53 54 45 52 5f 52 4f ock(p, MASTER_RO
16354 4f 54 2c 20 52 45 41 44 5f 4c 4f 43 4b 29 3b 0a OT, READ_LOCK);.
16355 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 if( SQLITE_OK!
16356 3d 72 63 20 29 20 67 6f 74 6f 20 74 72 61 6e 73 =rc ) goto trans
16357 5f 62 65 67 75 6e 3b 0a 0a 20 20 64 6f 20 7b 0a _begun;.. do {.
16358 20 20 20 20 2f 2a 20 43 61 6c 6c 20 6c 6f 63 6b /* Call lock
16359 42 74 72 65 65 28 29 20 75 6e 74 69 6c 20 65 69 Btree() until ei
1635a 74 68 65 72 20 70 42 74 2d 3e 70 50 61 67 65 31 ther pBt->pPage1
1635b 20 69 73 20 70 6f 70 75 6c 61 74 65 64 20 6f 72 is populated or
1635c 0a 20 20 20 20 2a 2a 20 6c 6f 63 6b 42 74 72 65 . ** lockBtre
1635d 65 28 29 20 72 65 74 75 72 6e 73 20 73 6f 6d 65 e() returns some
1635e 74 68 69 6e 67 20 6f 74 68 65 72 20 74 68 61 6e thing other than
1635f 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 6c 6f 63 6b SQLITE_OK. lock
16360 42 74 72 65 65 28 29 0a 20 20 20 20 2a 2a 20 6d Btree(). ** m
16361 61 79 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 ay return SQLITE
16362 5f 4f 4b 20 62 75 74 20 6c 65 61 76 65 20 70 42 _OK but leave pB
16363 74 2d 3e 70 50 61 67 65 31 20 73 65 74 20 74 6f t->pPage1 set to
16364 20 30 20 69 66 20 61 66 74 65 72 0a 20 20 20 20 0 if after.
16365 2a 2a 20 72 65 61 64 69 6e 67 20 70 61 67 65 20 ** reading page
16366 31 20 69 74 20 64 69 73 63 6f 76 65 72 73 20 74 1 it discovers t
16367 68 61 74 20 74 68 65 20 70 61 67 65 2d 73 69 7a hat the page-siz
16368 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 e of the databas
16369 65 20 0a 20 20 20 20 2a 2a 20 66 69 6c 65 20 69 e . ** file i
1636a 73 20 6e 6f 74 20 70 42 74 2d 3e 70 61 67 65 53 s not pBt->pageS
1636b 69 7a 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73 ize. In this cas
1636c 65 20 6c 6f 63 6b 42 74 72 65 65 28 29 20 77 69 e lockBtree() wi
1636d 6c 6c 20 75 70 64 61 74 65 0a 20 20 20 20 2a 2a ll update. **
1636e 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 74 pBt->pageSize t
1636f 6f 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65 20 o the page-size
16370 6f 66 20 74 68 65 20 66 69 6c 65 20 6f 6e 20 64 of the file on d
16371 69 73 6b 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 isk.. */.
16372 77 68 69 6c 65 28 20 70 42 74 2d 3e 70 50 61 67 while( pBt->pPag
16373 65 31 3d 3d 30 20 26 26 20 53 51 4c 49 54 45 5f e1==0 && SQLITE_
16374 4f 4b 3d 3d 28 72 63 20 3d 20 6c 6f 63 6b 42 74 OK==(rc = lockBt
16375 72 65 65 28 70 42 74 29 29 20 29 3b 0a 0a 20 20 ree(pBt)) );..
16376 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
16377 5f 4f 4b 20 26 26 20 77 72 66 6c 61 67 20 29 7b _OK && wrflag ){
16378 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 2d 3e . if( pBt->
16379 72 65 61 64 4f 6e 6c 79 20 29 7b 0a 20 20 20 20 readOnly ){.
1637a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
1637b 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 20 20 20 20 READONLY;.
1637c 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72 }else{. r
1637d 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
1637e 42 65 67 69 6e 28 70 42 74 2d 3e 70 50 61 67 65 Begin(pBt->pPage
1637f 72 2c 77 72 66 6c 61 67 3e 31 2c 73 71 6c 69 74 r,wrflag>1,sqlit
16380 65 33 54 65 6d 70 49 6e 4d 65 6d 6f 72 79 28 70 e3TempInMemory(p
16381 2d 3e 64 62 29 29 3b 0a 20 20 20 20 20 20 20 20 ->db));.
16382 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
16383 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 K ){. r
16384 63 20 3d 20 6e 65 77 44 61 74 61 62 61 73 65 28 c = newDatabase(
16385 70 42 74 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a pBt);. }.
16386 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
16387 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c . if( rc!=SQL
16388 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
16389 75 6e 6c 6f 63 6b 42 74 72 65 65 49 66 55 6e 75 unlockBtreeIfUnu
1638a 73 65 64 28 70 42 74 29 3b 0a 20 20 20 20 7d 0a sed(pBt);. }.
1638b 20 20 7d 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 }while( rc==SQ
1638c 4c 49 54 45 5f 42 55 53 59 20 26 26 20 70 42 74 LITE_BUSY && pBt
1638d 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d ->inTransaction=
1638e 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 26 26 0a 20 =TRANS_NONE &&.
1638f 20 20 20 20 20 20 20 20 20 62 74 72 65 65 49 6e btreeIn
16390 76 6f 6b 65 42 75 73 79 48 61 6e 64 6c 65 72 28 vokeBusyHandler(
16391 70 42 74 29 20 29 3b 0a 0a 20 20 69 66 28 20 72 pBt) );.. if( r
16392 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
16393 20 20 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 61 if( p->inTra
16394 6e 73 3d 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 ns==TRANS_NONE )
16395 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e 6e 54 72 {. pBt->nTr
16396 61 6e 73 61 63 74 69 6f 6e 2b 2b 3b 0a 23 69 66 ansaction++;.#if
16397 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
16398 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20 20 _SHARED_CACHE.
16399 20 20 20 20 69 66 28 20 70 2d 3e 73 68 61 72 61 if( p->shara
1639a 62 6c 65 20 29 7b 0a 09 61 73 73 65 72 74 28 20 ble ){..assert(
1639b 70 2d 3e 6c 6f 63 6b 2e 70 42 74 72 65 65 3d 3d p->lock.pBtree==
1639c 70 20 26 26 20 70 2d 3e 6c 6f 63 6b 2e 69 54 61 p && p->lock.iTa
1639d 62 6c 65 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 ble==1 );.
1639e 20 20 70 2d 3e 6c 6f 63 6b 2e 65 4c 6f 63 6b 20 p->lock.eLock
1639f 3d 20 52 45 41 44 5f 4c 4f 43 4b 3b 0a 20 20 20 = READ_LOCK;.
163a0 20 20 20 20 20 70 2d 3e 6c 6f 63 6b 2e 70 4e 65 p->lock.pNe
163a1 78 74 20 3d 20 70 42 74 2d 3e 70 4c 6f 63 6b 3b xt = pBt->pLock;
163a2 0a 20 20 20 20 20 20 20 20 70 42 74 2d 3e 70 4c . pBt->pL
163a3 6f 63 6b 20 3d 20 26 70 2d 3e 6c 6f 63 6b 3b 0a ock = &p->lock;.
163a4 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 }.#endif.
163a5 20 20 20 7d 0a 20 20 20 20 70 2d 3e 69 6e 54 72 }. p->inTr
163a6 61 6e 73 20 3d 20 28 77 72 66 6c 61 67 3f 54 52 ans = (wrflag?TR
163a7 41 4e 53 5f 57 52 49 54 45 3a 54 52 41 4e 53 5f ANS_WRITE:TRANS_
163a8 52 45 41 44 29 3b 0a 20 20 20 20 69 66 28 20 70 READ);. if( p
163a9 2d 3e 69 6e 54 72 61 6e 73 3e 70 42 74 2d 3e 69 ->inTrans>pBt->i
163aa 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 29 7b 0a nTransaction ){.
163ab 20 20 20 20 20 20 70 42 74 2d 3e 69 6e 54 72 61 pBt->inTra
163ac 6e 73 61 63 74 69 6f 6e 20 3d 20 70 2d 3e 69 6e nsaction = p->in
163ad 54 72 61 6e 73 3b 0a 20 20 20 20 7d 0a 23 69 66 Trans;. }.#if
163ae 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
163af 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20 20 _SHARED_CACHE.
163b0 20 20 69 66 28 20 77 72 66 6c 61 67 20 29 7b 0a if( wrflag ){.
163b1 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 70 assert( !p
163b2 42 74 2d 3e 70 57 72 69 74 65 72 20 29 3b 0a 20 Bt->pWriter );.
163b3 20 20 20 20 20 70 42 74 2d 3e 70 57 72 69 74 65 pBt->pWrite
163b4 72 20 3d 20 70 3b 0a 20 20 20 20 20 20 70 42 74 r = p;. pBt
163b5 2d 3e 69 73 45 78 63 6c 75 73 69 76 65 20 3d 20 ->isExclusive =
163b6 28 75 38 29 28 77 72 66 6c 61 67 3e 31 29 3b 0a (u8)(wrflag>1);.
163b7 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d }.#endif. }
163b8 0a 0a 0a 74 72 61 6e 73 5f 62 65 67 75 6e 3a 0a ...trans_begun:.
163b9 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
163ba 5f 4f 4b 20 26 26 20 77 72 66 6c 61 67 20 29 7b _OK && wrflag ){
163bb 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 63 61 6c . /* This cal
163bc 6c 20 6d 61 6b 65 73 20 73 75 72 65 20 74 68 61 l makes sure tha
163bd 74 20 74 68 65 20 70 61 67 65 72 20 68 61 73 20 t the pager has
163be 74 68 65 20 63 6f 72 72 65 63 74 20 6e 75 6d 62 the correct numb
163bf 65 72 20 6f 66 0a 20 20 20 20 2a 2a 20 6f 70 65 er of. ** ope
163c0 6e 20 73 61 76 65 70 6f 69 6e 74 73 2e 20 49 66 n savepoints. If
163c1 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 the second para
163c2 6d 65 74 65 72 20 69 73 20 67 72 65 61 74 65 72 meter is greater
163c3 20 74 68 61 6e 20 30 20 61 6e 64 0a 20 20 20 20 than 0 and.
163c4 2a 2a 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e ** the sub-journ
163c5 61 6c 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 al is not alread
163c6 79 20 6f 70 65 6e 2c 20 74 68 65 6e 20 69 74 20 y open, then it
163c7 77 69 6c 6c 20 62 65 20 6f 70 65 6e 65 64 20 68 will be opened h
163c8 65 72 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ere.. */.
163c9 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
163ca 72 4f 70 65 6e 53 61 76 65 70 6f 69 6e 74 28 70 rOpenSavepoint(p
163cb 42 74 2d 3e 70 50 61 67 65 72 2c 20 70 2d 3e 64 Bt->pPager, p->d
163cc 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 29 3b 0a b->nSavepoint);.
163cd 20 20 7d 0a 0a 20 20 62 74 72 65 65 49 6e 74 65 }.. btreeInte
163ce 67 72 69 74 79 28 70 29 3b 0a 20 20 73 71 6c 69 grity(p);. sqli
163cf 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 te3BtreeLeave(p)
163d0 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
163d1 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
163d2 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d _OMIT_AUTOVACUUM
163d3 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 ../*.** Set the
163d4 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 pointer-map entr
163d5 69 65 73 20 66 6f 72 20 61 6c 6c 20 63 68 69 6c ies for all chil
163d6 64 72 65 6e 20 6f 66 20 70 61 67 65 20 70 50 61 dren of page pPa
163d7 67 65 2e 20 41 6c 73 6f 2c 20 69 66 0a 2a 2a 20 ge. Also, if.**
163d8 70 50 61 67 65 20 63 6f 6e 74 61 69 6e 73 20 63 pPage contains c
163d9 65 6c 6c 73 20 74 68 61 74 20 70 6f 69 6e 74 20 ells that point
163da 74 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 to overflow page
163db 73 2c 20 73 65 74 20 74 68 65 20 70 6f 69 6e 74 s, set the point
163dc 65 72 0a 2a 2a 20 6d 61 70 20 65 6e 74 72 69 65 er.** map entrie
163dd 73 20 66 6f 72 20 74 68 65 20 6f 76 65 72 66 6c s for the overfl
163de 6f 77 20 70 61 67 65 73 20 61 73 20 77 65 6c 6c ow pages as well
163df 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
163e0 73 65 74 43 68 69 6c 64 50 74 72 6d 61 70 73 28 setChildPtrmaps(
163e1 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 29 7b MemPage *pPage){
163e2 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 . int i;
163e3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
163e4 20 20 20 20 20 20 2f 2a 20 43 6f 75 6e 74 65 72 /* Counter
163e5 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20 69 variable */. i
163e6 6e 74 20 6e 43 65 6c 6c 3b 20 20 20 20 20 20 20 nt nCell;
163e7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
163e8 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 /* Number of c
163e9 65 6c 6c 73 20 69 6e 20 70 61 67 65 20 70 50 61 ells in page pPa
163ea 67 65 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 ge */. int rc;
163eb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
163ec 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 /* Re
163ed 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 42 turn code */. B
163ee 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 tShared *pBt = p
163ef 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 75 38 20 Page->pBt;. u8
163f0 69 73 49 6e 69 74 4f 72 69 67 20 3d 20 70 50 61 isInitOrig = pPa
163f1 67 65 2d 3e 69 73 49 6e 69 74 3b 0a 20 20 50 67 ge->isInit;. Pg
163f2 6e 6f 20 70 67 6e 6f 20 3d 20 70 50 61 67 65 2d no pgno = pPage-
163f3 3e 70 67 6e 6f 3b 0a 0a 20 20 61 73 73 65 72 74 >pgno;.. assert
163f4 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
163f5 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d held(pPage->pBt-
163f6 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 63 20 >mutex) );. rc
163f7 3d 20 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 = btreeInitPage(
163f8 70 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 pPage);. if( rc
163f9 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
163fa 20 20 20 67 6f 74 6f 20 73 65 74 5f 63 68 69 6c goto set_chil
163fb 64 5f 70 74 72 6d 61 70 73 5f 6f 75 74 3b 0a 20 d_ptrmaps_out;.
163fc 20 7d 0a 20 20 6e 43 65 6c 6c 20 3d 20 70 50 61 }. nCell = pPa
163fd 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 0a 20 20 66 6f ge->nCell;.. fo
163fe 72 28 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 r(i=0; i<nCell;
163ff 69 2b 2b 29 7b 0a 20 20 20 20 75 38 20 2a 70 43 i++){. u8 *pC
16400 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 ell = findCell(p
16401 50 61 67 65 2c 20 69 29 3b 0a 0a 20 20 20 20 70 Page, i);.. p
16402 74 72 6d 61 70 50 75 74 4f 76 66 6c 50 74 72 28 trmapPutOvflPtr(
16403 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 72 pPage, pCell, &r
16404 63 29 3b 0a 0a 20 20 20 20 69 66 28 20 21 70 50 c);.. if( !pP
16405 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 age->leaf ){.
16406 20 20 20 50 67 6e 6f 20 63 68 69 6c 64 50 67 6e Pgno childPgn
16407 6f 20 3d 20 67 65 74 34 62 79 74 65 28 70 43 65 o = get4byte(pCe
16408 6c 6c 29 3b 0a 20 20 20 20 20 20 70 74 72 6d 61 ll);. ptrma
16409 70 50 75 74 28 70 42 74 2c 20 63 68 69 6c 64 50 pPut(pBt, childP
1640a 67 6e 6f 2c 20 50 54 52 4d 41 50 5f 42 54 52 45 gno, PTRMAP_BTRE
1640b 45 2c 20 70 67 6e 6f 2c 20 26 72 63 29 3b 0a 20 E, pgno, &rc);.
1640c 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 }. }.. if(
1640d 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a !pPage->leaf ){.
1640e 20 20 20 20 50 67 6e 6f 20 63 68 69 6c 64 50 67 Pgno childPg
1640f 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 no = get4byte(&p
16410 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 Page->aData[pPag
16411 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 e->hdrOffset+8])
16412 3b 0a 20 20 20 20 70 74 72 6d 61 70 50 75 74 28 ;. ptrmapPut(
16413 70 42 74 2c 20 63 68 69 6c 64 50 67 6e 6f 2c 20 pBt, childPgno,
16414 50 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 70 67 PTRMAP_BTREE, pg
16415 6e 6f 2c 20 26 72 63 29 3b 0a 20 20 7d 0a 0a 73 no, &rc);. }..s
16416 65 74 5f 63 68 69 6c 64 5f 70 74 72 6d 61 70 73 et_child_ptrmaps
16417 5f 6f 75 74 3a 0a 20 20 70 50 61 67 65 2d 3e 69 _out:. pPage->i
16418 73 49 6e 69 74 20 3d 20 69 73 49 6e 69 74 4f 72 sInit = isInitOr
16419 69 67 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b ig;. return rc;
1641a 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 77 68 .}../*.** Somewh
1641b 65 72 65 20 6f 6e 20 70 50 61 67 65 20 69 73 20 ere on pPage is
1641c 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 70 61 67 a pointer to pag
1641d 65 20 69 46 72 6f 6d 2e 20 20 4d 6f 64 69 66 79 e iFrom. Modify
1641e 20 74 68 69 73 20 70 6f 69 6e 74 65 72 20 73 6f this pointer so
1641f 0a 2a 2a 20 74 68 61 74 20 69 74 20 70 6f 69 6e .** that it poin
16420 74 73 20 74 6f 20 69 54 6f 2e 20 50 61 72 61 6d ts to iTo. Param
16421 65 74 65 72 20 65 54 79 70 65 20 64 65 73 63 72 eter eType descr
16422 69 62 65 73 20 74 68 65 20 74 79 70 65 20 6f 66 ibes the type of
16423 20 70 6f 69 6e 74 65 72 20 74 6f 0a 2a 2a 20 62 pointer to.** b
16424 65 20 6d 6f 64 69 66 69 65 64 2c 20 61 73 20 20 e modified, as
16425 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 50 follows:.**.** P
16426 54 52 4d 41 50 5f 42 54 52 45 45 3a 20 20 20 20 TRMAP_BTREE:
16427 20 70 50 61 67 65 20 69 73 20 61 20 62 74 72 65 pPage is a btre
16428 65 2d 70 61 67 65 2e 20 54 68 65 20 70 6f 69 6e e-page. The poin
16429 74 65 72 20 70 6f 69 6e 74 73 20 61 74 20 61 20 ter points at a
1642a 63 68 69 6c 64 20 0a 2a 2a 20 20 20 20 20 20 20 child .**
1642b 20 20 20 20 20 20 20 20 20 20 20 20 70 61 67 65 page
1642c 20 6f 66 20 70 50 61 67 65 2e 0a 2a 2a 0a 2a 2a of pPage..**.**
1642d 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 PTRMAP_OVERFLOW
1642e 31 3a 20 70 50 61 67 65 20 69 73 20 61 20 62 74 1: pPage is a bt
1642f 72 65 65 2d 70 61 67 65 2e 20 54 68 65 20 70 6f ree-page. The po
16430 69 6e 74 65 72 20 70 6f 69 6e 74 73 20 61 74 20 inter points at
16431 61 6e 20 6f 76 65 72 66 6c 6f 77 0a 2a 2a 20 20 an overflow.**
16432 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16433 20 70 61 67 65 20 70 6f 69 6e 74 65 64 20 74 6f page pointed to
16434 20 62 79 20 6f 6e 65 20 6f 66 20 74 68 65 20 63 by one of the c
16435 65 6c 6c 73 20 6f 6e 20 70 50 61 67 65 2e 0a 2a ells on pPage..*
16436 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f 4f 56 45 52 *.** PTRMAP_OVER
16437 46 4c 4f 57 32 3a 20 70 50 61 67 65 20 69 73 20 FLOW2: pPage is
16438 61 6e 20 6f 76 65 72 66 6c 6f 77 2d 70 61 67 65 an overflow-page
16439 2e 20 54 68 65 20 70 6f 69 6e 74 65 72 20 70 6f . The pointer po
1643a 69 6e 74 73 20 61 74 20 74 68 65 20 6e 65 78 74 ints at the next
1643b 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
1643c 20 20 20 20 20 20 6f 76 65 72 66 6c 6f 77 20 70 overflow p
1643d 61 67 65 20 69 6e 20 74 68 65 20 6c 69 73 74 2e age in the list.
1643e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d .*/.static int m
1643f 6f 64 69 66 79 50 61 67 65 50 6f 69 6e 74 65 72 odifyPagePointer
16440 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c (MemPage *pPage,
16441 20 50 67 6e 6f 20 69 46 72 6f 6d 2c 20 50 67 6e Pgno iFrom, Pgn
16442 6f 20 69 54 6f 2c 20 75 38 20 65 54 79 70 65 29 o iTo, u8 eType)
16443 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 {. assert( sqli
16444 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
16445 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 Page->pBt->mutex
16446 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 ) );. assert( s
16447 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 qlite3PagerIswri
16448 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 teable(pPage->pD
16449 62 50 61 67 65 29 20 29 3b 0a 20 20 69 66 28 20 bPage) );. if(
1644a 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 4f 56 eType==PTRMAP_OV
1644b 45 52 46 4c 4f 57 32 20 29 7b 0a 20 20 20 20 2f ERFLOW2 ){. /
1644c 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 20 69 73 * The pointer is
1644d 20 61 6c 77 61 79 73 20 74 68 65 20 66 69 72 73 always the firs
1644e 74 20 34 20 62 79 74 65 73 20 6f 66 20 74 68 65 t 4 bytes of the
1644f 20 70 61 67 65 20 69 6e 20 74 68 69 73 20 63 61 page in this ca
16450 73 65 2e 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 se. */. if(
16451 67 65 74 34 62 79 74 65 28 70 50 61 67 65 2d 3e get4byte(pPage->
16452 61 44 61 74 61 29 21 3d 69 46 72 6f 6d 20 29 7b aData)!=iFrom ){
16453 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 . return SQ
16454 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 LITE_CORRUPT_BKP
16455 54 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 75 74 T;. }. put
16456 34 62 79 74 65 28 70 50 61 67 65 2d 3e 61 44 61 4byte(pPage->aDa
16457 74 61 2c 20 69 54 6f 29 3b 0a 20 20 7d 65 6c 73 ta, iTo);. }els
16458 65 7b 0a 20 20 20 20 75 38 20 69 73 49 6e 69 74 e{. u8 isInit
16459 4f 72 69 67 20 3d 20 70 50 61 67 65 2d 3e 69 73 Orig = pPage->is
1645a 49 6e 69 74 3b 0a 20 20 20 20 69 6e 74 20 69 3b Init;. int i;
1645b 0a 20 20 20 20 69 6e 74 20 6e 43 65 6c 6c 3b 0a . int nCell;.
1645c 0a 20 20 20 20 62 74 72 65 65 49 6e 69 74 50 61 . btreeInitPa
1645d 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 6e ge(pPage);. n
1645e 43 65 6c 6c 20 3d 20 70 50 61 67 65 2d 3e 6e 43 Cell = pPage->nC
1645f 65 6c 6c 3b 0a 0a 20 20 20 20 66 6f 72 28 69 3d ell;.. for(i=
16460 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29 0; i<nCell; i++)
16461 7b 0a 20 20 20 20 20 20 75 38 20 2a 70 43 65 6c {. u8 *pCel
16462 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 l = findCell(pPa
16463 67 65 2c 20 69 29 3b 0a 20 20 20 20 20 20 69 66 ge, i);. if
16464 28 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f ( eType==PTRMAP_
16465 4f 56 45 52 46 4c 4f 57 31 20 29 7b 0a 20 20 20 OVERFLOW1 ){.
16466 20 20 20 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e CellInfo in
16467 66 6f 3b 0a 20 20 20 20 20 20 20 20 62 74 72 65 fo;. btre
16468 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70 50 eParseCellPtr(pP
16469 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 69 6e 66 age, pCell, &inf
1646a 6f 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 o);. if(
1646b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 20 29 info.iOverflow )
1646c 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 {. if(
1646d 69 46 72 6f 6d 3d 3d 67 65 74 34 62 79 74 65 28 iFrom==get4byte(
1646e 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 &pCell[info.iOve
1646f 72 66 6c 6f 77 5d 29 20 29 7b 0a 20 20 20 20 20 rflow]) ){.
16470 20 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 put4byte(
16471 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 &pCell[info.iOve
16472 72 66 6c 6f 77 5d 2c 20 69 54 6f 29 3b 0a 20 20 rflow], iTo);.
16473 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b break;
16474 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 . }.
16475 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c }. }el
16476 73 65 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 se{. if(
16477 67 65 74 34 62 79 74 65 28 70 43 65 6c 6c 29 3d get4byte(pCell)=
16478 3d 69 46 72 6f 6d 20 29 7b 0a 20 20 20 20 20 20 =iFrom ){.
16479 20 20 20 20 70 75 74 34 62 79 74 65 28 70 43 65 put4byte(pCe
1647a 6c 6c 2c 20 69 54 6f 29 3b 0a 20 20 20 20 20 20 ll, iTo);.
1647b 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
1647c 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
1647d 20 7d 0a 20 20 0a 20 20 20 20 69 66 28 20 69 3d }. . if( i=
1647e 3d 6e 43 65 6c 6c 20 29 7b 0a 20 20 20 20 20 20 =nCell ){.
1647f 69 66 28 20 65 54 79 70 65 21 3d 50 54 52 4d 41 if( eType!=PTRMA
16480 50 5f 42 54 52 45 45 20 7c 7c 20 0a 20 20 20 20 P_BTREE || .
16481 20 20 20 20 20 20 67 65 74 34 62 79 74 65 28 26 get4byte(&
16482 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 pPage->aData[pPa
16483 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d ge->hdrOffset+8]
16484 29 21 3d 69 46 72 6f 6d 20 29 7b 0a 20 20 20 20 )!=iFrom ){.
16485 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
16486 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a E_CORRUPT_BKPT;.
16487 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 75 }. pu
16488 74 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61 t4byte(&pPage->a
16489 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f Data[pPage->hdrO
1648a 66 66 73 65 74 2b 38 5d 2c 20 69 54 6f 29 3b 0a ffset+8], iTo);.
1648b 20 20 20 20 7d 0a 0a 20 20 20 20 70 50 61 67 65 }.. pPage
1648c 2d 3e 69 73 49 6e 69 74 20 3d 20 69 73 49 6e 69 ->isInit = isIni
1648d 74 4f 72 69 67 3b 0a 20 20 7d 0a 20 20 72 65 74 tOrig;. }. ret
1648e 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
1648f 0a 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 .../*.** Move th
16490 65 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65 20 e open database
16491 70 61 67 65 20 70 44 62 50 61 67 65 20 74 6f 20 page pDbPage to
16492 6c 6f 63 61 74 69 6f 6e 20 69 46 72 65 65 50 61 location iFreePa
16493 67 65 20 69 6e 20 74 68 65 20 0a 2a 2a 20 64 61 ge in the .** da
16494 74 61 62 61 73 65 2e 20 54 68 65 20 70 44 62 50 tabase. The pDbP
16495 61 67 65 20 72 65 66 65 72 65 6e 63 65 20 72 65 age reference re
16496 6d 61 69 6e 73 20 76 61 6c 69 64 2e 0a 2a 2a 0a mains valid..**.
16497 2a 2a 20 54 68 65 20 69 73 43 6f 6d 6d 69 74 20 ** The isCommit
16498 66 6c 61 67 20 69 6e 64 69 63 61 74 65 73 20 74 flag indicates t
16499 68 61 74 20 74 68 65 72 65 20 69 73 20 6e 6f 20 hat there is no
1649a 6e 65 65 64 20 74 6f 20 72 65 6d 65 6d 62 65 72 need to remember
1649b 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 6a 6f 75 that.** the jou
1649c 72 6e 61 6c 20 6e 65 65 64 73 20 74 6f 20 62 65 rnal needs to be
1649d 20 73 79 6e 63 28 29 65 64 20 62 65 66 6f 72 65 sync()ed before
1649e 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 70 database page p
1649f 44 62 50 61 67 65 2d 3e 70 67 6e 6f 20 0a 2a 2a DbPage->pgno .**
164a0 20 63 61 6e 20 62 65 20 77 72 69 74 74 65 6e 20 can be written
164a1 74 6f 2e 20 54 68 65 20 63 61 6c 6c 65 72 20 68 to. The caller h
164a2 61 73 20 61 6c 72 65 61 64 79 20 70 72 6f 6d 69 as already promi
164a3 73 65 64 20 6e 6f 74 20 74 6f 20 77 72 69 74 65 sed not to write
164a4 20 74 6f 20 74 68 61 74 0a 2a 2a 20 70 61 67 65 to that.** page
164a5 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
164a6 72 65 6c 6f 63 61 74 65 50 61 67 65 28 0a 20 20 relocatePage(.
164a7 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 20 BtShared *pBt,
164a8 20 20 20 20 20 20 20 20 20 2f 2a 20 42 74 72 65 /* Btre
164a9 65 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a e */. MemPage *
164aa 70 44 62 50 61 67 65 2c 20 20 20 20 20 20 20 20 pDbPage,
164ab 2f 2a 20 4f 70 65 6e 20 70 61 67 65 20 74 6f 20 /* Open page to
164ac 6d 6f 76 65 20 2a 2f 0a 20 20 75 38 20 65 54 79 move */. u8 eTy
164ad 70 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 pe,
164ae 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 6d 61 /* Pointer ma
164af 70 20 27 74 79 70 65 27 20 65 6e 74 72 79 20 66 p 'type' entry f
164b0 6f 72 20 70 44 62 50 61 67 65 20 2a 2f 0a 20 20 or pDbPage */.
164b1 50 67 6e 6f 20 69 50 74 72 50 61 67 65 2c 20 20 Pgno iPtrPage,
164b2 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e /* Poin
164b3 74 65 72 20 6d 61 70 20 27 70 61 67 65 2d 6e 6f ter map 'page-no
164b4 27 20 65 6e 74 72 79 20 66 6f 72 20 70 44 62 50 ' entry for pDbP
164b5 61 67 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 46 age */. Pgno iF
164b6 72 65 65 50 61 67 65 2c 20 20 20 20 20 20 20 20 reePage,
164b7 20 20 2f 2a 20 54 68 65 20 6c 6f 63 61 74 69 6f /* The locatio
164b8 6e 20 74 6f 20 6d 6f 76 65 20 70 44 62 50 61 67 n to move pDbPag
164b9 65 20 74 6f 20 2a 2f 0a 20 20 69 6e 74 20 69 73 e to */. int is
164ba 43 6f 6d 6d 69 74 20 20 20 20 20 20 20 20 20 20 Commit
164bb 20 20 20 2f 2a 20 69 73 43 6f 6d 6d 69 74 20 66 /* isCommit f
164bc 6c 61 67 20 70 61 73 73 65 64 20 74 6f 20 73 71 lag passed to sq
164bd 6c 69 74 65 33 50 61 67 65 72 4d 6f 76 65 70 61 lite3PagerMovepa
164be 67 65 20 2a 2f 0a 29 7b 0a 20 20 4d 65 6d 50 61 ge */.){. MemPa
164bf 67 65 20 2a 70 50 74 72 50 61 67 65 3b 20 20 20 ge *pPtrPage;
164c0 2f 2a 20 54 68 65 20 70 61 67 65 20 74 68 61 74 /* The page that
164c1 20 63 6f 6e 74 61 69 6e 73 20 61 20 70 6f 69 6e contains a poin
164c2 74 65 72 20 74 6f 20 70 44 62 50 61 67 65 20 2a ter to pDbPage *
164c3 2f 0a 20 20 50 67 6e 6f 20 69 44 62 50 61 67 65 /. Pgno iDbPage
164c4 20 3d 20 70 44 62 50 61 67 65 2d 3e 70 67 6e 6f = pDbPage->pgno
164c5 3b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 ;. Pager *pPage
164c6 72 20 3d 20 70 42 74 2d 3e 70 50 61 67 65 72 3b r = pBt->pPager;
164c7 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73 . int rc;.. as
164c8 73 65 72 74 28 20 65 54 79 70 65 3d 3d 50 54 52 sert( eType==PTR
164c9 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32 20 7c 7c MAP_OVERFLOW2 ||
164ca 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 4f eType==PTRMAP_O
164cb 56 45 52 46 4c 4f 57 31 20 7c 7c 20 0a 20 20 20 VERFLOW1 || .
164cc 20 20 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 eType==PTRMAP
164cd 5f 42 54 52 45 45 20 7c 7c 20 65 54 79 70 65 3d _BTREE || eType=
164ce 3d 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 =PTRMAP_ROOTPAGE
164cf 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 );. assert( sq
164d0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
164d1 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a (pBt->mutex) );.
164d2 20 20 61 73 73 65 72 74 28 20 70 44 62 50 61 67 assert( pDbPag
164d3 65 2d 3e 70 42 74 3d 3d 70 42 74 20 29 3b 0a 0a e->pBt==pBt );..
164d4 20 20 2f 2a 20 4d 6f 76 65 20 70 61 67 65 20 69 /* Move page i
164d5 44 62 50 61 67 65 20 66 72 6f 6d 20 69 74 73 20 DbPage from its
164d6 63 75 72 72 65 6e 74 20 6c 6f 63 61 74 69 6f 6e current location
164d7 20 74 6f 20 70 61 67 65 20 6e 75 6d 62 65 72 20 to page number
164d8 69 46 72 65 65 50 61 67 65 20 2a 2f 0a 20 20 54 iFreePage */. T
164d9 52 41 43 45 28 28 22 41 55 54 4f 56 41 43 55 55 RACE(("AUTOVACUU
164da 4d 3a 20 4d 6f 76 69 6e 67 20 25 64 20 74 6f 20 M: Moving %d to
164db 66 72 65 65 20 70 61 67 65 20 25 64 20 28 70 74 free page %d (pt
164dc 72 20 70 61 67 65 20 25 64 20 74 79 70 65 20 25 r page %d type %
164dd 64 29 5c 6e 22 2c 20 0a 20 20 20 20 20 20 69 44 d)\n", . iD
164de 62 50 61 67 65 2c 20 69 46 72 65 65 50 61 67 65 bPage, iFreePage
164df 2c 20 69 50 74 72 50 61 67 65 2c 20 65 54 79 70 , iPtrPage, eTyp
164e0 65 29 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 e));. rc = sqli
164e1 74 65 33 50 61 67 65 72 4d 6f 76 65 70 61 67 65 te3PagerMovepage
164e2 28 70 50 61 67 65 72 2c 20 70 44 62 50 61 67 65 (pPager, pDbPage
164e3 2d 3e 70 44 62 50 61 67 65 2c 20 69 46 72 65 65 ->pDbPage, iFree
164e4 50 61 67 65 2c 20 69 73 43 6f 6d 6d 69 74 29 3b Page, isCommit);
164e5 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 . if( rc!=SQLIT
164e6 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 E_OK ){. retu
164e7 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 70 44 62 rn rc;. }. pDb
164e8 50 61 67 65 2d 3e 70 67 6e 6f 20 3d 20 69 46 72 Page->pgno = iFr
164e9 65 65 50 61 67 65 3b 0a 0a 20 20 2f 2a 20 49 66 eePage;.. /* If
164ea 20 70 44 62 50 61 67 65 20 77 61 73 20 61 20 62 pDbPage was a b
164eb 74 72 65 65 2d 70 61 67 65 2c 20 74 68 65 6e 20 tree-page, then
164ec 69 74 20 6d 61 79 20 68 61 76 65 20 63 68 69 6c it may have chil
164ed 64 20 70 61 67 65 73 20 61 6e 64 2f 6f 72 20 63 d pages and/or c
164ee 65 6c 6c 73 0a 20 20 2a 2a 20 74 68 61 74 20 70 ells. ** that p
164ef 6f 69 6e 74 20 74 6f 20 6f 76 65 72 66 6c 6f 77 oint to overflow
164f0 20 70 61 67 65 73 2e 20 54 68 65 20 70 6f 69 6e pages. The poin
164f1 74 65 72 20 6d 61 70 20 65 6e 74 72 69 65 73 20 ter map entries
164f2 66 6f 72 20 61 6c 6c 20 74 68 65 73 65 0a 20 20 for all these.
164f3 2a 2a 20 70 61 67 65 73 20 6e 65 65 64 20 74 6f ** pages need to
164f4 20 62 65 20 63 68 61 6e 67 65 64 2e 0a 20 20 2a be changed.. *
164f5 2a 0a 20 20 2a 2a 20 49 66 20 70 44 62 50 61 67 *. ** If pDbPag
164f6 65 20 69 73 20 61 6e 20 6f 76 65 72 66 6c 6f 77 e is an overflow
164f7 20 70 61 67 65 2c 20 74 68 65 6e 20 74 68 65 20 page, then the
164f8 66 69 72 73 74 20 34 20 62 79 74 65 73 20 6d 61 first 4 bytes ma
164f9 79 20 73 74 6f 72 65 20 61 0a 20 20 2a 2a 20 70 y store a. ** p
164fa 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 75 62 73 ointer to a subs
164fb 65 71 75 65 6e 74 20 6f 76 65 72 66 6c 6f 77 20 equent overflow
164fc 70 61 67 65 2e 20 49 66 20 74 68 69 73 20 69 73 page. If this is
164fd 20 74 68 65 20 63 61 73 65 2c 20 74 68 65 6e 0a the case, then.
164fe 20 20 2a 2a 20 74 68 65 20 70 6f 69 6e 74 65 72 ** the pointer
164ff 20 6d 61 70 20 6e 65 65 64 73 20 74 6f 20 62 65 map needs to be
16500 20 75 70 64 61 74 65 64 20 66 6f 72 20 74 68 65 updated for the
16501 20 73 75 62 73 65 71 75 65 6e 74 20 6f 76 65 72 subsequent over
16502 66 6c 6f 77 20 70 61 67 65 2e 0a 20 20 2a 2f 0a flow page.. */.
16503 20 20 69 66 28 20 65 54 79 70 65 3d 3d 50 54 52 if( eType==PTR
16504 4d 41 50 5f 42 54 52 45 45 20 7c 7c 20 65 54 79 MAP_BTREE || eTy
16505 70 65 3d 3d 50 54 52 4d 41 50 5f 52 4f 4f 54 50 pe==PTRMAP_ROOTP
16506 41 47 45 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 AGE ){. rc =
16507 73 65 74 43 68 69 6c 64 50 74 72 6d 61 70 73 28 setChildPtrmaps(
16508 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69 66 pDbPage);. if
16509 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
1650a 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
1650b 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 rc;. }. }els
1650c 65 7b 0a 20 20 20 20 50 67 6e 6f 20 6e 65 78 74 e{. Pgno next
1650d 4f 76 66 6c 20 3d 20 67 65 74 34 62 79 74 65 28 Ovfl = get4byte(
1650e 70 44 62 50 61 67 65 2d 3e 61 44 61 74 61 29 3b pDbPage->aData);
1650f 0a 20 20 20 20 69 66 28 20 6e 65 78 74 4f 76 66 . if( nextOvf
16510 6c 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 74 l!=0 ){. pt
16511 72 6d 61 70 50 75 74 28 70 42 74 2c 20 6e 65 78 rmapPut(pBt, nex
16512 74 4f 76 66 6c 2c 20 50 54 52 4d 41 50 5f 4f 56 tOvfl, PTRMAP_OV
16513 45 52 46 4c 4f 57 32 2c 20 69 46 72 65 65 50 61 ERFLOW2, iFreePa
16514 67 65 2c 20 26 72 63 29 3b 0a 20 20 20 20 20 20 ge, &rc);.
16515 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
16516 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 K ){. ret
16517 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a urn rc;. }.
16518 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 }. }.. /*
16519 46 69 78 20 74 68 65 20 64 61 74 61 62 61 73 65 Fix the database
1651a 20 70 6f 69 6e 74 65 72 20 6f 6e 20 70 61 67 65 pointer on page
1651b 20 69 50 74 72 50 61 67 65 20 74 68 61 74 20 70 iPtrPage that p
1651c 6f 69 6e 74 65 64 20 61 74 20 69 44 62 50 61 67 ointed at iDbPag
1651d 65 20 73 6f 0a 20 20 2a 2a 20 74 68 61 74 20 69 e so. ** that i
1651e 74 20 70 6f 69 6e 74 73 20 61 74 20 69 46 72 65 t points at iFre
1651f 65 50 61 67 65 2e 20 41 6c 73 6f 20 66 69 78 20 ePage. Also fix
16520 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 the pointer map
16521 65 6e 74 72 79 20 66 6f 72 0a 20 20 2a 2a 20 69 entry for. ** i
16522 50 74 72 50 61 67 65 2e 0a 20 20 2a 2f 0a 20 20 PtrPage.. */.
16523 69 66 28 20 65 54 79 70 65 21 3d 50 54 52 4d 41 if( eType!=PTRMA
16524 50 5f 52 4f 4f 54 50 41 47 45 20 29 7b 0a 20 20 P_ROOTPAGE ){.
16525 20 20 72 63 20 3d 20 62 74 72 65 65 47 65 74 50 rc = btreeGetP
16526 61 67 65 28 70 42 74 2c 20 69 50 74 72 50 61 67 age(pBt, iPtrPag
16527 65 2c 20 26 70 50 74 72 50 61 67 65 2c 20 30 29 e, &pPtrPage, 0)
16528 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 ;. if( rc!=SQ
16529 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
1652a 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
1652b 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 }. rc = sqlit
1652c 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 74 e3PagerWrite(pPt
1652d 72 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b rPage->pDbPage);
1652e 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c . if( rc!=SQL
1652f 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
16530 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 74 72 releasePage(pPtr
16531 50 61 67 65 29 3b 0a 20 20 20 20 20 20 72 65 74 Page);. ret
16532 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 urn rc;. }.
16533 20 20 72 63 20 3d 20 6d 6f 64 69 66 79 50 61 67 rc = modifyPag
16534 65 50 6f 69 6e 74 65 72 28 70 50 74 72 50 61 67 ePointer(pPtrPag
16535 65 2c 20 69 44 62 50 61 67 65 2c 20 69 46 72 65 e, iDbPage, iFre
16536 65 50 61 67 65 2c 20 65 54 79 70 65 29 3b 0a 20 ePage, eType);.
16537 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 releasePage(p
16538 50 74 72 50 61 67 65 29 3b 0a 20 20 20 20 69 66 PtrPage);. if
16539 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
1653a 29 7b 0a 20 20 20 20 20 20 70 74 72 6d 61 70 50 ){. ptrmapP
1653b 75 74 28 70 42 74 2c 20 69 46 72 65 65 50 61 67 ut(pBt, iFreePag
1653c 65 2c 20 65 54 79 70 65 2c 20 69 50 74 72 50 61 e, eType, iPtrPa
1653d 67 65 2c 20 26 72 63 29 3b 0a 20 20 20 20 7d 0a ge, &rc);. }.
1653e 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
1653f 0a 7d 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 64 .}../* Forward d
16540 65 63 6c 61 72 61 74 69 6f 6e 20 72 65 71 75 69 eclaration requi
16541 72 65 64 20 62 79 20 69 6e 63 72 56 61 63 75 75 red by incrVacuu
16542 6d 53 74 65 70 28 29 2e 20 2a 2f 0a 73 74 61 74 mStep(). */.stat
16543 69 63 20 69 6e 74 20 61 6c 6c 6f 63 61 74 65 42 ic int allocateB
16544 74 72 65 65 50 61 67 65 28 42 74 53 68 61 72 65 treePage(BtShare
16545 64 20 2a 2c 20 4d 65 6d 50 61 67 65 20 2a 2a 2c d *, MemPage **,
16546 20 50 67 6e 6f 20 2a 2c 20 50 67 6e 6f 2c 20 75 Pgno *, Pgno, u
16547 38 29 3b 0a 0a 2f 2a 0a 2a 2a 20 50 65 72 66 6f 8);../*.** Perfo
16548 72 6d 20 61 20 73 69 6e 67 6c 65 20 73 74 65 70 rm a single step
16549 20 6f 66 20 61 6e 20 69 6e 63 72 65 6d 65 6e 74 of an increment
1654a 61 6c 2d 76 61 63 75 75 6d 2e 20 49 66 20 73 75 al-vacuum. If su
1654b 63 63 65 73 73 66 75 6c 2c 0a 2a 2a 20 72 65 74 ccessful,.** ret
1654c 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 49 urn SQLITE_OK. I
1654d 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 77 6f f there is no wo
1654e 72 6b 20 74 6f 20 64 6f 20 28 61 6e 64 20 74 68 rk to do (and th
1654f 65 72 65 66 6f 72 65 20 6e 6f 0a 2a 2a 20 70 6f erefore no.** po
16550 69 6e 74 20 69 6e 20 63 61 6c 6c 69 6e 67 20 74 int in calling t
16551 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 67 61 his function aga
16552 69 6e 29 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 in), return SQLI
16553 54 45 5f 44 4f 4e 45 2e 0a 2a 2a 0a 2a 2a 20 4d TE_DONE..**.** M
16554 6f 72 65 20 73 70 65 63 69 66 69 63 6c 79 2c 20 ore specificly,
16555 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 74 this function at
16556 74 65 6d 70 74 73 20 74 6f 20 72 65 2d 6f 72 67 tempts to re-org
16557 61 6e 69 7a 65 20 74 68 65 20 0a 2a 2a 20 64 61 anize the .** da
16558 74 61 62 61 73 65 20 73 6f 20 74 68 61 74 20 74 tabase so that t
16559 68 65 20 6c 61 73 74 20 70 61 67 65 20 6f 66 20 he last page of
1655a 74 68 65 20 66 69 6c 65 20 63 75 72 72 65 6e 74 the file current
1655b 6c 79 20 69 6e 20 75 73 65 0a 2a 2a 20 69 73 20 ly in use.** is
1655c 6e 6f 20 6c 6f 6e 67 65 72 20 69 6e 20 75 73 65 no longer in use
1655d 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e ..**.** If the n
1655e 46 69 6e 20 70 61 72 61 6d 65 74 65 72 20 69 73 Fin parameter is
1655f 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 69 73 20 non-zero, this
16560 66 75 6e 63 74 69 6f 6e 20 61 73 73 75 6d 65 73 function assumes
16561 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 63 61 6c .** that the cal
16562 6c 65 72 20 77 69 6c 6c 20 6b 65 65 70 20 63 61 ler will keep ca
16563 6c 6c 69 6e 67 20 69 6e 63 72 56 61 63 75 75 6d lling incrVacuum
16564 53 74 65 70 28 29 20 75 6e 74 69 6c 0a 2a 2a 20 Step() until.**
16565 69 74 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 it returns SQLIT
16566 45 5f 44 4f 4e 45 20 6f 72 20 61 6e 20 65 72 72 E_DONE or an err
16567 6f 72 2c 20 61 6e 64 20 74 68 61 74 20 6e 46 69 or, and that nFi
16568 6e 20 69 73 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 n is the.** numb
16569 65 72 20 6f 66 20 70 61 67 65 73 20 74 68 65 20 er of pages the
1656a 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 77 69 database file wi
1656b 6c 6c 20 63 6f 6e 74 61 69 6e 20 61 66 74 65 72 ll contain after
1656c 20 74 68 69 73 20 0a 2a 2a 20 70 72 6f 63 65 73 this .** proces
1656d 73 20 69 73 20 63 6f 6d 70 6c 65 74 65 2e 20 20 s is complete.
1656e 49 66 20 6e 46 69 6e 20 69 73 20 7a 65 72 6f 2c If nFin is zero,
1656f 20 69 74 20 69 73 20 61 73 73 75 6d 65 64 20 74 it is assumed t
16570 68 61 74 0a 2a 2a 20 69 6e 63 72 56 61 63 75 75 hat.** incrVacuu
16571 6d 53 74 65 70 28 29 20 77 69 6c 6c 20 62 65 20 mStep() will be
16572 63 61 6c 6c 65 64 20 61 20 66 69 6e 69 74 65 20 called a finite
16573 61 6d 6f 75 6e 74 20 6f 66 20 74 69 6d 65 73 0a amount of times.
16574 2a 2a 20 77 68 69 63 68 20 6d 61 79 20 6f 72 20 ** which may or
16575 6d 61 79 20 6e 6f 74 20 65 6d 70 74 79 20 74 68 may not empty th
16576 65 20 66 72 65 65 6c 69 73 74 2e 20 20 41 20 66 e freelist. A f
16577 75 6c 6c 20 61 75 74 6f 76 61 63 75 75 6d 0a 2a ull autovacuum.*
16578 2a 20 68 61 73 20 6e 46 69 6e 3e 30 2e 20 20 41 * has nFin>0. A
16579 20 22 50 52 41 47 4d 41 20 69 6e 63 72 65 6d 65 "PRAGMA increme
1657a 6e 74 61 6c 5f 76 61 63 75 75 6d 22 20 68 61 73 ntal_vacuum" has
1657b 20 6e 46 69 6e 3d 3d 30 2e 0a 2a 2f 0a 73 74 61 nFin==0..*/.sta
1657c 74 69 63 20 69 6e 74 20 69 6e 63 72 56 61 63 75 tic int incrVacu
1657d 75 6d 53 74 65 70 28 42 74 53 68 61 72 65 64 20 umStep(BtShared
1657e 2a 70 42 74 2c 20 50 67 6e 6f 20 6e 46 69 6e 2c *pBt, Pgno nFin,
1657f 20 50 67 6e 6f 20 69 4c 61 73 74 50 67 29 7b 0a Pgno iLastPg){.
16580 20 20 50 67 6e 6f 20 6e 46 72 65 65 4c 69 73 74 Pgno nFreeList
16581 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e ; /* N
16582 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 73 umber of pages s
16583 74 69 6c 6c 20 6f 6e 20 74 68 65 20 66 72 65 65 till on the free
16584 2d 6c 69 73 74 20 2a 2f 0a 0a 20 20 61 73 73 65 -list */.. asse
16585 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
16586 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 x_held(pBt->mute
16587 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 x) );. assert(
16588 69 4c 61 73 74 50 67 3e 6e 46 69 6e 20 29 3b 0a iLastPg>nFin );.
16589 0a 20 20 69 66 28 20 21 50 54 52 4d 41 50 5f 49 . if( !PTRMAP_I
1658a 53 50 41 47 45 28 70 42 74 2c 20 69 4c 61 73 74 SPAGE(pBt, iLast
1658b 50 67 29 20 26 26 20 69 4c 61 73 74 50 67 21 3d Pg) && iLastPg!=
1658c 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 PENDING_BYTE_PAG
1658d 45 28 70 42 74 29 20 29 7b 0a 20 20 20 20 69 6e E(pBt) ){. in
1658e 74 20 72 63 3b 0a 20 20 20 20 75 38 20 65 54 79 t rc;. u8 eTy
1658f 70 65 3b 0a 20 20 20 20 50 67 6e 6f 20 69 50 74 pe;. Pgno iPt
16590 72 50 61 67 65 3b 0a 0a 20 20 20 20 6e 46 72 65 rPage;.. nFre
16591 65 4c 69 73 74 20 3d 20 67 65 74 34 62 79 74 65 eList = get4byte
16592 28 26 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 (&pBt->pPage1->a
16593 44 61 74 61 5b 33 36 5d 29 3b 0a 20 20 20 20 69 Data[36]);. i
16594 66 28 20 6e 46 72 65 65 4c 69 73 74 3d 3d 30 20 f( nFreeList==0
16595 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
16596 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 SQLITE_DONE;.
16597 20 7d 0a 0a 20 20 20 20 72 63 20 3d 20 70 74 72 }.. rc = ptr
16598 6d 61 70 47 65 74 28 70 42 74 2c 20 69 4c 61 73 mapGet(pBt, iLas
16599 74 50 67 2c 20 26 65 54 79 70 65 2c 20 26 69 50 tPg, &eType, &iP
1659a 74 72 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 trPage);. if(
1659b 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
1659c 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 {. return r
1659d 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 c;. }. if(
1659e 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 52 eType==PTRMAP_R
1659f 4f 4f 54 50 41 47 45 20 29 7b 0a 20 20 20 20 20 OOTPAGE ){.
165a0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
165a1 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 ORRUPT_BKPT;.
165a2 20 7d 0a 0a 20 20 20 20 69 66 28 20 65 54 79 70 }.. if( eTyp
165a3 65 3d 3d 50 54 52 4d 41 50 5f 46 52 45 45 50 41 e==PTRMAP_FREEPA
165a4 47 45 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 GE ){. if(
165a5 6e 46 69 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 nFin==0 ){.
165a6 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20 74 68 65 /* Remove the
165a7 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20 66 page from the f
165a8 69 6c 65 73 20 66 72 65 65 2d 6c 69 73 74 2e 20 iles free-list.
165a9 54 68 69 73 20 69 73 20 6e 6f 74 20 72 65 71 75 This is not requ
165aa 69 72 65 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 ired. **
165ab 69 66 20 6e 46 69 6e 20 69 73 20 6e 6f 6e 2d 7a if nFin is non-z
165ac 65 72 6f 2e 20 49 6e 20 74 68 61 74 20 63 61 73 ero. In that cas
165ad 65 2c 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 e, the free-list
165ae 20 77 69 6c 6c 20 62 65 0a 20 20 20 20 20 20 20 will be.
165af 20 2a 2a 20 74 72 75 6e 63 61 74 65 64 20 74 6f ** truncated to
165b0 20 7a 65 72 6f 20 61 66 74 65 72 20 74 68 69 73 zero after this
165b1 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e function return
165b2 73 2c 20 73 6f 20 69 74 20 64 6f 65 73 6e 27 74 s, so it doesn't
165b3 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 6d 61 74 . ** mat
165b4 74 65 72 20 69 66 20 69 74 20 73 74 69 6c 6c 20 ter if it still
165b5 63 6f 6e 74 61 69 6e 73 20 73 6f 6d 65 20 67 61 contains some ga
165b6 72 62 61 67 65 20 65 6e 74 72 69 65 73 2e 0a 20 rbage entries..
165b7 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 */.
165b8 20 20 50 67 6e 6f 20 69 46 72 65 65 50 67 3b 0a Pgno iFreePg;.
165b9 20 20 20 20 20 20 20 20 4d 65 6d 50 61 67 65 20 MemPage
165ba 2a 70 46 72 65 65 50 67 3b 0a 20 20 20 20 20 20 *pFreePg;.
165bb 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74 65 42 rc = allocateB
165bc 74 72 65 65 50 61 67 65 28 70 42 74 2c 20 26 70 treePage(pBt, &p
165bd 46 72 65 65 50 67 2c 20 26 69 46 72 65 65 50 67 FreePg, &iFreePg
165be 2c 20 69 4c 61 73 74 50 67 2c 20 31 29 3b 0a 20 , iLastPg, 1);.
165bf 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 if( rc!=S
165c0 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
165c1 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b return rc;
165c2 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
165c3 20 20 20 61 73 73 65 72 74 28 20 69 46 72 65 65 assert( iFree
165c4 50 67 3d 3d 69 4c 61 73 74 50 67 20 29 3b 0a 20 Pg==iLastPg );.
165c5 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 releasePa
165c6 67 65 28 70 46 72 65 65 50 67 29 3b 0a 20 20 20 ge(pFreePg);.
165c7 20 20 20 7d 0a 20 20 20 20 7d 20 65 6c 73 65 20 }. } else
165c8 7b 0a 20 20 20 20 20 20 50 67 6e 6f 20 69 46 72 {. Pgno iFr
165c9 65 65 50 67 3b 20 20 20 20 20 20 20 20 20 20 20 eePg;
165ca 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 66 72 /* Index of fr
165cb 65 65 20 70 61 67 65 20 74 6f 20 6d 6f 76 65 20 ee page to move
165cc 70 4c 61 73 74 50 67 20 74 6f 20 2a 2f 0a 20 20 pLastPg to */.
165cd 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 4c 61 MemPage *pLa
165ce 73 74 50 67 3b 0a 0a 20 20 20 20 20 20 72 63 20 stPg;.. rc
165cf 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 28 70 = btreeGetPage(p
165d0 42 74 2c 20 69 4c 61 73 74 50 67 2c 20 26 70 4c Bt, iLastPg, &pL
165d1 61 73 74 50 67 2c 20 30 29 3b 0a 20 20 20 20 20 astPg, 0);.
165d2 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
165d3 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 OK ){. re
165d4 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d turn rc;. }
165d5 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 6e 46 .. /* If nF
165d6 69 6e 20 69 73 20 7a 65 72 6f 2c 20 74 68 69 73 in is zero, this
165d7 20 6c 6f 6f 70 20 72 75 6e 73 20 65 78 61 63 74 loop runs exact
165d8 6c 79 20 6f 6e 63 65 20 61 6e 64 20 70 61 67 65 ly once and page
165d9 20 70 4c 61 73 74 50 67 0a 20 20 20 20 20 20 2a pLastPg. *
165da 2a 20 69 73 20 73 77 61 70 70 65 64 20 77 69 74 * is swapped wit
165db 68 20 74 68 65 20 66 69 72 73 74 20 66 72 65 65 h the first free
165dc 20 70 61 67 65 20 70 75 6c 6c 65 64 20 6f 66 66 page pulled off
165dd 20 74 68 65 20 66 72 65 65 20 6c 69 73 74 2e 0a the free list..
165de 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a **. *
165df 2a 20 4f 6e 20 74 68 65 20 6f 74 68 65 72 20 68 * On the other h
165e0 61 6e 64 2c 20 69 66 20 6e 46 69 6e 20 69 73 20 and, if nFin is
165e1 67 72 65 61 74 65 72 20 74 68 61 6e 20 7a 65 72 greater than zer
165e2 6f 2c 20 74 68 65 6e 20 6b 65 65 70 0a 20 20 20 o, then keep.
165e3 20 20 20 2a 2a 20 6c 6f 6f 70 69 6e 67 20 75 6e ** looping un
165e4 74 69 6c 20 61 20 66 72 65 65 2d 70 61 67 65 20 til a free-page
165e5 6c 6f 63 61 74 65 64 20 77 69 74 68 69 6e 20 74 located within t
165e6 68 65 20 66 69 72 73 74 20 6e 46 69 6e 20 70 61 he first nFin pa
165e7 67 65 73 0a 20 20 20 20 20 20 2a 2a 20 6f 66 20 ges. ** of
165e8 74 68 65 20 66 69 6c 65 20 69 73 20 66 6f 75 6e the file is foun
165e9 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 d.. */.
165ea 20 20 64 6f 20 7b 0a 20 20 20 20 20 20 20 20 4d do {. M
165eb 65 6d 50 61 67 65 20 2a 70 46 72 65 65 50 67 3b emPage *pFreePg;
165ec 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 61 6c . rc = al
165ed 6c 6f 63 61 74 65 42 74 72 65 65 50 61 67 65 28 locateBtreePage(
165ee 70 42 74 2c 20 26 70 46 72 65 65 50 67 2c 20 26 pBt, &pFreePg, &
165ef 69 46 72 65 65 50 67 2c 20 30 2c 20 30 29 3b 0a iFreePg, 0, 0);.
165f0 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d if( rc!=
165f1 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
165f2 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 releasePa
165f3 67 65 28 70 4c 61 73 74 50 67 29 3b 0a 20 20 20 ge(pLastPg);.
165f4 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 return rc
165f5 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
165f6 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 releasePage(
165f7 70 46 72 65 65 50 67 29 3b 0a 20 20 20 20 20 20 pFreePg);.
165f8 7d 77 68 69 6c 65 28 20 6e 46 69 6e 21 3d 30 20 }while( nFin!=0
165f9 26 26 20 69 46 72 65 65 50 67 3e 6e 46 69 6e 20 && iFreePg>nFin
165fa 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
165fb 20 69 46 72 65 65 50 67 3c 69 4c 61 73 74 50 67 iFreePg<iLastPg
165fc 20 29 3b 0a 20 20 20 20 20 20 0a 20 20 20 20 20 );. .
165fd 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
165fe 65 72 57 72 69 74 65 28 70 4c 61 73 74 50 67 2d erWrite(pLastPg-
165ff 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 >pDbPage);.
16600 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
16601 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 OK ){. rc
16602 20 3d 20 72 65 6c 6f 63 61 74 65 50 61 67 65 28 = relocatePage(
16603 70 42 74 2c 20 70 4c 61 73 74 50 67 2c 20 65 54 pBt, pLastPg, eT
16604 79 70 65 2c 20 69 50 74 72 50 61 67 65 2c 20 69 ype, iPtrPage, i
16605 46 72 65 65 50 67 2c 20 6e 46 69 6e 21 3d 30 29 FreePg, nFin!=0)
16606 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
16607 72 65 6c 65 61 73 65 50 61 67 65 28 70 4c 61 73 releasePage(pLas
16608 74 50 67 29 3b 0a 20 20 20 20 20 20 69 66 28 20 tPg);. if(
16609 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
1660a 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 . return
1660b 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 rc;. }.
1660c 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 6e 46 69 }. }.. if( nFi
1660d 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 69 4c 61 73 n==0 ){. iLas
1660e 74 50 67 2d 2d 3b 0a 20 20 20 20 77 68 69 6c 65 tPg--;. while
1660f 28 20 69 4c 61 73 74 50 67 3d 3d 50 45 4e 44 49 ( iLastPg==PENDI
16610 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 NG_BYTE_PAGE(pBt
16611 29 7c 7c 50 54 52 4d 41 50 5f 49 53 50 41 47 45 )||PTRMAP_ISPAGE
16612 28 70 42 74 2c 20 69 4c 61 73 74 50 67 29 20 29 (pBt, iLastPg) )
16613 7b 0a 20 20 20 20 20 20 69 66 28 20 50 54 52 4d {. if( PTRM
16614 41 50 5f 49 53 50 41 47 45 28 70 42 74 2c 20 69 AP_ISPAGE(pBt, i
16615 4c 61 73 74 50 67 29 20 29 7b 0a 20 20 20 20 20 LastPg) ){.
16616 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 67 3b MemPage *pPg;
16617 0a 20 20 20 20 20 20 20 20 69 6e 74 20 72 63 20 . int rc
16618 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 28 70 = btreeGetPage(p
16619 42 74 2c 20 69 4c 61 73 74 50 67 2c 20 26 70 50 Bt, iLastPg, &pP
1661a 67 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 69 g, 0);. i
1661b 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
1661c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 ){. re
1661d 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 turn rc;.
1661e 20 7d 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 }. rc =
1661f 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 sqlite3PagerWrit
16620 65 28 70 50 67 2d 3e 70 44 62 50 61 67 65 29 3b e(pPg->pDbPage);
16621 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 . release
16622 50 61 67 65 28 70 50 67 29 3b 0a 20 20 20 20 20 Page(pPg);.
16623 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
16624 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
16625 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 return rc;.
16626 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 }. }.
16627 20 20 20 20 20 69 4c 61 73 74 50 67 2d 2d 3b 0a iLastPg--;.
16628 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 }. sqlite
16629 33 50 61 67 65 72 54 72 75 6e 63 61 74 65 49 6d 3PagerTruncateIm
1662a 61 67 65 28 70 42 74 2d 3e 70 50 61 67 65 72 2c age(pBt->pPager,
1662b 20 69 4c 61 73 74 50 67 29 3b 0a 20 20 7d 0a 20 iLastPg);. }.
1662c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
1662d 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 20 77 72 K;.}../*.** A wr
1662e 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 ite-transaction
1662f 6d 75 73 74 20 62 65 20 6f 70 65 6e 65 64 20 62 must be opened b
16630 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 74 68 efore calling th
16631 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 is function..**
16632 49 74 20 70 65 72 66 6f 72 6d 73 20 61 20 73 69 It performs a si
16633 6e 67 6c 65 20 75 6e 69 74 20 6f 66 20 77 6f 72 ngle unit of wor
16634 6b 20 74 6f 77 61 72 64 73 20 61 6e 20 69 6e 63 k towards an inc
16635 72 65 6d 65 6e 74 61 6c 20 76 61 63 75 75 6d 2e remental vacuum.
16636 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 69 6e .**.** If the in
16637 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 75 75 6d cremental vacuum
16638 20 69 73 20 66 69 6e 69 73 68 65 64 20 61 66 74 is finished aft
16639 65 72 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e er this function
1663a 20 68 61 73 20 72 75 6e 2c 0a 2a 2a 20 53 51 4c has run,.** SQL
1663b 49 54 45 5f 44 4f 4e 45 20 69 73 20 72 65 74 75 ITE_DONE is retu
1663c 72 6e 65 64 2e 20 49 66 20 69 74 20 69 73 20 6e rned. If it is n
1663d 6f 74 20 66 69 6e 69 73 68 65 64 2c 20 62 75 74 ot finished, but
1663e 20 6e 6f 20 65 72 72 6f 72 20 6f 63 63 75 72 72 no error occurr
1663f 65 64 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b ed,.** SQLITE_OK
16640 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 4f 74 is returned. Ot
16641 68 65 72 77 69 73 65 20 61 6e 20 53 51 4c 69 74 herwise an SQLit
16642 65 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 0a 2a e error code. .*
16643 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
16644 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
16645 65 49 6e 63 72 56 61 63 75 75 6d 28 42 74 72 65 eIncrVacuum(Btre
16646 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b e *p){. int rc;
16647 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 . BtShared *pBt
16648 20 3d 20 70 2d 3e 70 42 74 3b 0a 0a 20 20 73 71 = p->pBt;.. sq
16649 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 lite3BtreeEnter(
1664a 70 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 p);. assert( pB
1664b 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e t->inTransaction
1664c 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 26 26 ==TRANS_WRITE &&
1664d 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 p->inTrans==TRA
1664e 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 69 66 NS_WRITE );. if
1664f 28 20 21 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 ( !pBt->autoVacu
16650 75 6d 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 um ){. rc = S
16651 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d 65 QLITE_DONE;. }e
16652 6c 73 65 7b 0a 20 20 20 20 69 6e 76 61 6c 69 64 lse{. invalid
16653 61 74 65 41 6c 6c 4f 76 65 72 66 6c 6f 77 43 61 ateAllOverflowCa
16654 63 68 65 28 70 42 74 29 3b 0a 20 20 20 20 72 63 che(pBt);. rc
16655 20 3d 20 69 6e 63 72 56 61 63 75 75 6d 53 74 65 = incrVacuumSte
16656 70 28 70 42 74 2c 20 30 2c 20 70 61 67 65 72 50 p(pBt, 0, pagerP
16657 61 67 65 63 6f 75 6e 74 28 70 42 74 29 29 3b 0a agecount(pBt));.
16658 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 72 }. sqlite3Btr
16659 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 eeLeave(p);. re
1665a 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
1665b 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 * This routine i
1665c 73 20 63 61 6c 6c 65 64 20 70 72 69 6f 72 20 74 s called prior t
1665d 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f o sqlite3PagerCo
1665e 6d 6d 69 74 20 77 68 65 6e 20 61 20 74 72 61 6e mmit when a tran
1665f 73 61 63 74 69 6f 6e 0a 2a 2a 20 69 73 20 63 6f saction.** is co
16660 6d 6d 69 74 65 64 20 66 6f 72 20 61 6e 20 61 75 mmited for an au
16661 74 6f 2d 76 61 63 75 75 6d 20 64 61 74 61 62 61 to-vacuum databa
16662 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 53 51 4c se..**.** If SQL
16663 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e ITE_OK is return
16664 65 64 2c 20 74 68 65 6e 20 2a 70 6e 54 72 75 6e ed, then *pnTrun
16665 63 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 c is set to the
16666 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 0a number of pages.
16667 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 ** the database
16668 66 69 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 74 file should be t
16669 72 75 6e 63 61 74 65 64 20 74 6f 20 64 75 72 69 runcated to duri
1666a 6e 67 20 74 68 65 20 63 6f 6d 6d 69 74 20 70 72 ng the commit pr
1666b 6f 63 65 73 73 2e 20 0a 2a 2a 20 69 2e 65 2e 20 ocess. .** i.e.
1666c 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61 73 the database has
1666d 20 62 65 65 6e 20 72 65 6f 72 67 61 6e 69 7a 65 been reorganize
1666e 64 20 73 6f 20 74 68 61 74 20 6f 6e 6c 79 20 74 d so that only t
1666f 68 65 20 66 69 72 73 74 20 2a 70 6e 54 72 75 6e he first *pnTrun
16670 63 0a 2a 2a 20 70 61 67 65 73 20 61 72 65 20 69 c.** pages are i
16671 6e 20 75 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 n use..*/.static
16672 20 69 6e 74 20 61 75 74 6f 56 61 63 75 75 6d 43 int autoVacuumC
16673 6f 6d 6d 69 74 28 42 74 53 68 61 72 65 64 20 2a ommit(BtShared *
16674 70 42 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d pBt){. int rc =
16675 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 50 61 SQLITE_OK;. Pa
16676 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70 42 ger *pPager = pB
16677 74 2d 3e 70 50 61 67 65 72 3b 0a 20 20 56 56 41 t->pPager;. VVA
16678 5f 4f 4e 4c 59 28 20 69 6e 74 20 6e 52 65 66 20 _ONLY( int nRef
16679 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 = sqlite3PagerRe
1667a 66 63 6f 75 6e 74 28 70 50 61 67 65 72 29 20 29 fcount(pPager) )
1667b 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c ;.. assert( sql
1667c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
1667d 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 pBt->mutex) );.
1667e 20 69 6e 76 61 6c 69 64 61 74 65 41 6c 6c 4f 76 invalidateAllOv
1667f 65 72 66 6c 6f 77 43 61 63 68 65 28 70 42 74 29 erflowCache(pBt)
16680 3b 0a 20 20 61 73 73 65 72 74 28 70 42 74 2d 3e ;. assert(pBt->
16681 61 75 74 6f 56 61 63 75 75 6d 29 3b 0a 20 20 69 autoVacuum);. i
16682 66 28 20 21 70 42 74 2d 3e 69 6e 63 72 56 61 63 f( !pBt->incrVac
16683 75 75 6d 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20 uum ){. Pgno
16684 6e 46 69 6e 3b 20 20 20 20 20 20 20 20 20 2f 2a nFin; /*
16685 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 Number of pages
16686 20 69 6e 20 64 61 74 61 62 61 73 65 20 61 66 74 in database aft
16687 65 72 20 61 75 74 6f 76 61 63 75 75 6d 69 6e 67 er autovacuuming
16688 20 2a 2f 0a 20 20 20 20 50 67 6e 6f 20 6e 46 72 */. Pgno nFr
16689 65 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 ee; /* Nu
1668a 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 6f 6e mber of pages on
1668b 20 74 68 65 20 66 72 65 65 6c 69 73 74 20 69 6e the freelist in
1668c 69 74 69 61 6c 6c 79 20 2a 2f 0a 20 20 20 20 50 itially */. P
1668d 67 6e 6f 20 6e 50 74 72 6d 61 70 3b 20 20 20 20 gno nPtrmap;
1668e 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 50 /* Number of P
1668f 74 72 4d 61 70 20 70 61 67 65 73 20 74 6f 20 62 trMap pages to b
16690 65 20 66 72 65 65 64 20 2a 2f 0a 20 20 20 20 50 e freed */. P
16691 67 6e 6f 20 69 46 72 65 65 3b 20 20 20 20 20 20 gno iFree;
16692 20 20 2f 2a 20 54 68 65 20 6e 65 78 74 20 70 61 /* The next pa
16693 67 65 20 74 6f 20 62 65 20 66 72 65 65 64 20 2a ge to be freed *
16694 2f 0a 20 20 20 20 69 6e 74 20 6e 45 6e 74 72 79 /. int nEntry
16695 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 ; /* Numb
16696 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 6f 6e er of entries on
16697 20 6f 6e 65 20 70 74 72 6d 61 70 20 70 61 67 65 one ptrmap page
16698 20 2a 2f 0a 20 20 20 20 50 67 6e 6f 20 6e 4f 72 */. Pgno nOr
16699 69 67 3b 20 20 20 20 20 20 20 20 2f 2a 20 44 61 ig; /* Da
1669a 74 61 62 61 73 65 20 73 69 7a 65 20 62 65 66 6f tabase size befo
1669b 72 65 20 66 72 65 65 69 6e 67 20 2a 2f 0a 0a 20 re freeing */..
1669c 20 20 20 6e 4f 72 69 67 20 3d 20 70 61 67 65 72 nOrig = pager
1669d 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29 3b 0a Pagecount(pBt);.
1669e 20 20 20 20 69 66 28 20 50 54 52 4d 41 50 5f 49 if( PTRMAP_I
1669f 53 50 41 47 45 28 70 42 74 2c 20 6e 4f 72 69 67 SPAGE(pBt, nOrig
166a0 29 20 7c 7c 20 6e 4f 72 69 67 3d 3d 50 45 4e 44 ) || nOrig==PEND
166a1 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 ING_BYTE_PAGE(pB
166a2 74 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 t) ){. /* I
166a3 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c t is not possibl
166a4 65 20 74 6f 20 63 72 65 61 74 65 20 61 20 64 61 e to create a da
166a5 74 61 62 61 73 65 20 66 6f 72 20 77 68 69 63 68 tabase for which
166a6 20 74 68 65 20 66 69 6e 61 6c 20 70 61 67 65 0a the final page.
166a7 20 20 20 20 20 20 2a 2a 20 69 73 20 65 69 74 68 ** is eith
166a8 65 72 20 61 20 70 6f 69 6e 74 65 72 2d 6d 61 70 er a pointer-map
166a9 20 70 61 67 65 20 6f 72 20 74 68 65 20 70 65 6e page or the pen
166aa 64 69 6e 67 2d 62 79 74 65 20 70 61 67 65 2e 20 ding-byte page.
166ab 49 66 20 6f 6e 65 0a 20 20 20 20 20 20 2a 2a 20 If one. **
166ac 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2c 20 is encountered,
166ad 74 68 69 73 20 69 6e 64 69 63 61 74 65 73 20 63 this indicates c
166ae 6f 72 72 75 70 74 69 6f 6e 2e 0a 20 20 20 20 20 orruption..
166af 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 72 6e */. return
166b0 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
166b1 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 BKPT;. }..
166b2 20 6e 46 72 65 65 20 3d 20 67 65 74 34 62 79 74 nFree = get4byt
166b3 65 28 26 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e e(&pBt->pPage1->
166b4 61 44 61 74 61 5b 33 36 5d 29 3b 0a 20 20 20 20 aData[36]);.
166b5 6e 45 6e 74 72 79 20 3d 20 70 42 74 2d 3e 75 73 nEntry = pBt->us
166b6 61 62 6c 65 53 69 7a 65 2f 35 3b 0a 20 20 20 20 ableSize/5;.
166b7 6e 50 74 72 6d 61 70 20 3d 20 28 6e 46 72 65 65 nPtrmap = (nFree
166b8 2d 6e 4f 72 69 67 2b 50 54 52 4d 41 50 5f 50 41 -nOrig+PTRMAP_PA
166b9 47 45 4e 4f 28 70 42 74 2c 20 6e 4f 72 69 67 29 GENO(pBt, nOrig)
166ba 2b 6e 45 6e 74 72 79 29 2f 6e 45 6e 74 72 79 3b +nEntry)/nEntry;
166bb 0a 20 20 20 20 6e 46 69 6e 20 3d 20 6e 4f 72 69 . nFin = nOri
166bc 67 20 2d 20 6e 46 72 65 65 20 2d 20 6e 50 74 72 g - nFree - nPtr
166bd 6d 61 70 3b 0a 20 20 20 20 69 66 28 20 6e 4f 72 map;. if( nOr
166be 69 67 3e 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f ig>PENDING_BYTE_
166bf 50 41 47 45 28 70 42 74 29 20 26 26 20 6e 46 69 PAGE(pBt) && nFi
166c0 6e 3c 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 n<PENDING_BYTE_P
166c1 41 47 45 28 70 42 74 29 20 29 7b 0a 20 20 20 20 AGE(pBt) ){.
166c2 20 20 6e 46 69 6e 2d 2d 3b 0a 20 20 20 20 7d 0a nFin--;. }.
166c3 20 20 20 20 77 68 69 6c 65 28 20 50 54 52 4d 41 while( PTRMA
166c4 50 5f 49 53 50 41 47 45 28 70 42 74 2c 20 6e 46 P_ISPAGE(pBt, nF
166c5 69 6e 29 20 7c 7c 20 6e 46 69 6e 3d 3d 50 45 4e in) || nFin==PEN
166c6 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 DING_BYTE_PAGE(p
166c7 42 74 29 20 29 7b 0a 20 20 20 20 20 20 6e 46 69 Bt) ){. nFi
166c8 6e 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 n--;. }. i
166c9 66 28 20 6e 46 69 6e 3e 6e 4f 72 69 67 20 29 20 f( nFin>nOrig )
166ca 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f return SQLITE_CO
166cb 52 52 55 50 54 5f 42 4b 50 54 3b 0a 0a 20 20 20 RRUPT_BKPT;..
166cc 20 66 6f 72 28 69 46 72 65 65 3d 6e 4f 72 69 67 for(iFree=nOrig
166cd 3b 20 69 46 72 65 65 3e 6e 46 69 6e 20 26 26 20 ; iFree>nFin &&
166ce 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3b 20 69 rc==SQLITE_OK; i
166cf 46 72 65 65 2d 2d 29 7b 0a 20 20 20 20 20 20 72 Free--){. r
166d0 63 20 3d 20 69 6e 63 72 56 61 63 75 75 6d 53 74 c = incrVacuumSt
166d1 65 70 28 70 42 74 2c 20 6e 46 69 6e 2c 20 69 46 ep(pBt, nFin, iF
166d2 72 65 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 ree);. }.
166d3 69 66 28 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f if( (rc==SQLITE_
166d4 44 4f 4e 45 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 DONE || rc==SQLI
166d5 54 45 5f 4f 4b 29 20 26 26 20 6e 46 72 65 65 3e TE_OK) && nFree>
166d6 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 0 ){. rc =
166d7 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 SQLITE_OK;.
166d8 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
166d9 65 72 57 72 69 74 65 28 70 42 74 2d 3e 70 50 61 erWrite(pBt->pPa
166da 67 65 31 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 ge1->pDbPage);.
166db 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26 70 put4byte(&p
166dc 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 Bt->pPage1->aDat
166dd 61 5b 33 32 5d 2c 20 30 29 3b 0a 20 20 20 20 20 a[32], 0);.
166de 20 70 75 74 34 62 79 74 65 28 26 70 42 74 2d 3e put4byte(&pBt->
166df 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 36 pPage1->aData[36
166e0 5d 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71 6c ], 0);. sql
166e1 69 74 65 33 50 61 67 65 72 54 72 75 6e 63 61 74 ite3PagerTruncat
166e2 65 49 6d 61 67 65 28 70 42 74 2d 3e 70 50 61 67 eImage(pBt->pPag
166e3 65 72 2c 20 6e 46 69 6e 29 3b 0a 20 20 20 20 7d er, nFin);. }
166e4 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c . if( rc!=SQL
166e5 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
166e6 73 71 6c 69 74 65 33 50 61 67 65 72 52 6f 6c 6c sqlite3PagerRoll
166e7 62 61 63 6b 28 70 50 61 67 65 72 29 3b 0a 20 20 back(pPager);.
166e8 20 20 7d 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 }. }.. asser
166e9 74 28 20 6e 52 65 66 3d 3d 73 71 6c 69 74 65 33 t( nRef==sqlite3
166ea 50 61 67 65 72 52 65 66 63 6f 75 6e 74 28 70 50 PagerRefcount(pP
166eb 61 67 65 72 29 20 29 3b 0a 20 20 72 65 74 75 72 ager) );. retur
166ec 6e 20 72 63 3b 0a 7d 0a 0a 23 65 6c 73 65 20 2f n rc;.}..#else /
166ed 2a 20 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f * ifndef SQLITE_
166ee 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 20 OMIT_AUTOVACUUM
166ef 2a 2f 0a 23 20 64 65 66 69 6e 65 20 73 65 74 43 */.# define setC
166f0 68 69 6c 64 50 74 72 6d 61 70 73 28 78 29 20 53 hildPtrmaps(x) S
166f1 51 4c 49 54 45 5f 4f 4b 0a 23 65 6e 64 69 66 0a QLITE_OK.#endif.
166f2 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 ./*.** This rout
166f3 69 6e 65 20 64 6f 65 73 20 74 68 65 20 66 69 72 ine does the fir
166f4 73 74 20 70 68 61 73 65 20 6f 66 20 61 20 74 77 st phase of a tw
166f5 6f 2d 70 68 61 73 65 20 63 6f 6d 6d 69 74 2e 20 o-phase commit.
166f6 20 54 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a This routine.**
166f7 20 63 61 75 73 65 73 20 61 20 72 6f 6c 6c 62 61 causes a rollba
166f8 63 6b 20 6a 6f 75 72 6e 61 6c 20 74 6f 20 62 65 ck journal to be
166f9 20 63 72 65 61 74 65 64 20 28 69 66 20 69 74 20 created (if it
166fa 64 6f 65 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 does not already
166fb 20 65 78 69 73 74 29 0a 2a 2a 20 61 6e 64 20 70 exist).** and p
166fc 6f 70 75 6c 61 74 65 64 20 77 69 74 68 20 65 6e opulated with en
166fd 6f 75 67 68 20 69 6e 66 6f 72 6d 61 74 69 6f 6e ough information
166fe 20 73 6f 20 74 68 61 74 20 69 66 20 61 20 70 6f so that if a po
166ff 77 65 72 20 6c 6f 73 73 20 6f 63 63 75 72 73 0a wer loss occurs.
16700 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 ** the database
16701 63 61 6e 20 62 65 20 72 65 73 74 6f 72 65 64 20 can be restored
16702 74 6f 20 69 74 73 20 6f 72 69 67 69 6e 61 6c 20 to its original
16703 73 74 61 74 65 20 62 79 20 70 6c 61 79 69 6e 67 state by playing
16704 20 62 61 63 6b 0a 2a 2a 20 74 68 65 20 6a 6f 75 back.** the jou
16705 72 6e 61 6c 2e 20 20 54 68 65 6e 20 74 68 65 20 rnal. Then the
16706 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 contents of the
16707 6a 6f 75 72 6e 61 6c 20 61 72 65 20 66 6c 75 73 journal are flus
16708 68 65 64 20 6f 75 74 20 74 6f 0a 2a 2a 20 74 68 hed out to.** th
16709 65 20 64 69 73 6b 2e 20 20 41 66 74 65 72 20 74 e disk. After t
1670a 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 73 61 he journal is sa
1670b 66 65 6c 79 20 6f 6e 20 6f 78 69 64 65 2c 20 74 fely on oxide, t
1670c 68 65 20 63 68 61 6e 67 65 73 20 74 6f 20 74 68 he changes to th
1670d 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 61 72 e.** database ar
1670e 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 e written into t
1670f 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
16710 20 61 6e 64 20 66 6c 75 73 68 65 64 20 74 6f 20 and flushed to
16711 6f 78 69 64 65 2e 0a 2a 2a 20 41 74 20 74 68 65 oxide..** At the
16712 20 65 6e 64 20 6f 66 20 74 68 69 73 20 63 61 6c end of this cal
16713 6c 2c 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 l, the rollback
16714 6a 6f 75 72 6e 61 6c 20 73 74 69 6c 6c 20 65 78 journal still ex
16715 69 73 74 73 20 6f 6e 20 74 68 65 0a 2a 2a 20 64 ists on the.** d
16716 69 73 6b 20 61 6e 64 20 77 65 20 61 72 65 20 73 isk and we are s
16717 74 69 6c 6c 20 68 6f 6c 64 69 6e 67 20 61 6c 6c till holding all
16718 20 6c 6f 63 6b 73 2c 20 73 6f 20 74 68 65 20 74 locks, so the t
16719 72 61 6e 73 61 63 74 69 6f 6e 20 68 61 73 20 6e ransaction has n
1671a 6f 74 0a 2a 2a 20 63 6f 6d 6d 69 74 74 65 64 2e ot.** committed.
1671b 20 20 53 65 65 20 73 71 6c 69 74 65 33 42 74 72 See sqlite3Btr
1671c 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f eeCommitPhaseTwo
1671d 28 29 20 66 6f 72 20 74 68 65 20 73 65 63 6f 6e () for the secon
1671e 64 20 70 68 61 73 65 20 6f 66 20 74 68 65 0a 2a d phase of the.*
1671f 2a 20 63 6f 6d 6d 69 74 20 70 72 6f 63 65 73 73 * commit process
16720 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 63 61 6c ..**.** This cal
16721 6c 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20 l is a no-op if
16722 6e 6f 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 no write-transac
16723 74 69 6f 6e 20 69 73 20 63 75 72 72 65 6e 74 6c tion is currentl
16724 79 20 61 63 74 69 76 65 20 6f 6e 20 70 42 74 2e y active on pBt.
16725 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 .**.** Otherwise
16726 2c 20 73 79 6e 63 20 74 68 65 20 64 61 74 61 62 , sync the datab
16727 61 73 65 20 66 69 6c 65 20 66 6f 72 20 74 68 65 ase file for the
16728 20 62 74 72 65 65 20 70 42 74 2e 20 7a 4d 61 73 btree pBt. zMas
16729 74 65 72 20 70 6f 69 6e 74 73 20 74 6f 0a 2a 2a ter points to.**
1672a 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 6d the name of a m
1672b 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 aster journal fi
1672c 6c 65 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 le that should b
1672d 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 e written into t
1672e 68 65 0a 2a 2a 20 69 6e 64 69 76 69 64 75 61 6c he.** individual
1672f 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 6f journal file, o
16730 72 20 69 73 20 4e 55 4c 4c 2c 20 69 6e 64 69 63 r is NULL, indic
16731 61 74 69 6e 67 20 6e 6f 20 6d 61 73 74 65 72 20 ating no master
16732 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 0a 2a 2a journal file .**
16733 20 28 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73 (single databas
16734 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 29 2e 0a e transaction)..
16735 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 69 73 20 **.** When this
16736 69 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 20 6d is called, the m
16737 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 73 68 aster journal sh
16738 6f 75 6c 64 20 61 6c 72 65 61 64 79 20 68 61 76 ould already hav
16739 65 20 62 65 65 6e 0a 2a 2a 20 63 72 65 61 74 65 e been.** create
1673a 64 2c 20 70 6f 70 75 6c 61 74 65 64 20 77 69 74 d, populated wit
1673b 68 20 74 68 69 73 20 6a 6f 75 72 6e 61 6c 20 70 h this journal p
1673c 6f 69 6e 74 65 72 20 61 6e 64 20 73 79 6e 63 65 ointer and synce
1673d 64 20 74 6f 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a d to disk..**.**
1673e 20 4f 6e 63 65 20 74 68 69 73 20 69 73 20 72 6f Once this is ro
1673f 75 74 69 6e 65 20 68 61 73 20 72 65 74 75 72 6e utine has return
16740 65 64 2c 20 74 68 65 20 6f 6e 6c 79 20 74 68 69 ed, the only thi
16741 6e 67 20 72 65 71 75 69 72 65 64 20 74 6f 20 63 ng required to c
16742 6f 6d 6d 69 74 0a 2a 2a 20 74 68 65 20 77 72 69 ommit.** the wri
16743 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 66 te-transaction f
16744 6f 72 20 74 68 69 73 20 64 61 74 61 62 61 73 65 or this database
16745 20 66 69 6c 65 20 69 73 20 74 6f 20 64 65 6c 65 file is to dele
16746 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 0a te the journal..
16747 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
16748 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
16749 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 eeCommitPhaseOne
1674a 28 42 74 72 65 65 20 2a 70 2c 20 63 6f 6e 73 74 (Btree *p, const
1674b 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 29 7b char *zMaster){
1674c 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
1674d 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 2d 3e TE_OK;. if( p->
1674e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 inTrans==TRANS_W
1674f 52 49 54 45 20 29 7b 0a 20 20 20 20 42 74 53 68 RITE ){. BtSh
16750 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 ared *pBt = p->p
16751 42 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 Bt;. sqlite3B
16752 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 23 69 treeEnter(p);.#i
16753 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
16754 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 T_AUTOVACUUM.
16755 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 if( pBt->autoVa
16756 63 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 72 63 cuum ){. rc
16757 20 3d 20 61 75 74 6f 56 61 63 75 75 6d 43 6f 6d = autoVacuumCom
16758 6d 69 74 28 70 42 74 29 3b 0a 20 20 20 20 20 20 mit(pBt);.
16759 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
1675a 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c K ){. sql
1675b 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 ite3BtreeLeave(p
1675c 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 );. retur
1675d 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 n rc;. }.
1675e 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 72 }.#endif. r
1675f 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
16760 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 70 CommitPhaseOne(p
16761 42 74 2d 3e 70 50 61 67 65 72 2c 20 7a 4d 61 73 Bt->pPager, zMas
16762 74 65 72 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c ter, 0);. sql
16763 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 ite3BtreeLeave(p
16764 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
16765 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 rc;.}../*.** Thi
16766 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 s function is ca
16767 6c 6c 65 64 20 66 72 6f 6d 20 62 6f 74 68 20 42 lled from both B
16768 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 54 treeCommitPhaseT
16769 77 6f 28 29 20 61 6e 64 20 42 74 72 65 65 52 6f wo() and BtreeRo
1676a 6c 6c 62 61 63 6b 28 29 0a 2a 2a 20 61 74 20 74 llback().** at t
1676b 68 65 20 63 6f 6e 63 6c 75 73 69 6f 6e 20 6f 66 he conclusion of
1676c 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a a transaction..
1676d 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 62 */.static void b
1676e 74 72 65 65 45 6e 64 54 72 61 6e 73 61 63 74 69 treeEndTransacti
1676f 6f 6e 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 on(Btree *p){.
16770 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 BtShared *pBt =
16771 70 2d 3e 70 42 74 3b 0a 20 20 42 74 43 75 72 73 p->pBt;. BtCurs
16772 6f 72 20 2a 70 43 73 72 3b 0a 20 20 61 73 73 65 or *pCsr;. asse
16773 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 rt( sqlite3Btree
16774 48 6f 6c 64 73 4d 75 74 65 78 28 70 29 20 29 3b HoldsMutex(p) );
16775 0a 0a 20 20 2f 2a 20 53 65 61 72 63 68 20 66 6f .. /* Search fo
16776 72 20 61 20 63 75 72 73 6f 72 20 68 65 6c 64 20 r a cursor held
16777 6f 70 65 6e 20 62 79 20 74 68 69 73 20 62 2d 74 open by this b-t
16778 72 65 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 ree connection.
16779 49 66 20 6f 6e 65 20 65 78 69 73 74 73 2c 0a 20 If one exists,.
1677a 20 2a 2a 20 74 68 65 6e 20 74 68 65 20 74 72 61 ** then the tra
1677b 6e 73 61 63 74 69 6f 6e 20 77 69 6c 6c 20 62 65 nsaction will be
1677c 20 64 6f 77 6e 67 72 61 64 65 64 20 74 6f 20 61 downgraded to a
1677d 20 72 65 61 64 2d 6f 6e 6c 79 20 74 72 61 6e 73 read-only trans
1677e 61 63 74 69 6f 6e 0a 20 20 2a 2a 20 69 6e 73 74 action. ** inst
1677f 65 61 64 20 6f 66 20 61 63 74 75 61 6c 6c 79 20 ead of actually
16780 63 6f 6e 63 6c 75 64 65 64 2e 20 41 20 73 75 62 concluded. A sub
16781 73 65 71 75 65 6e 74 20 63 61 6c 6c 20 74 6f 20 sequent call to
16782 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 29 CommitPhaseTwo()
16783 20 0a 20 20 2a 2a 20 6f 72 20 52 6f 6c 6c 62 61 . ** or Rollba
16784 63 6b 28 29 20 77 69 6c 6c 20 66 69 6e 69 73 68 ck() will finish
16785 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e the transaction
16786 20 61 6e 64 20 75 6e 6c 6f 63 6b 20 74 68 65 20 and unlock the
16787 64 61 74 61 62 61 73 65 2e 20 20 2a 2f 0a 20 20 database. */.
16788 66 6f 72 28 70 43 73 72 3d 70 42 74 2d 3e 70 43 for(pCsr=pBt->pC
16789 75 72 73 6f 72 3b 20 70 43 73 72 20 26 26 20 70 ursor; pCsr && p
1678a 43 73 72 2d 3e 70 42 74 72 65 65 21 3d 70 3b 20 Csr->pBtree!=p;
1678b 70 43 73 72 3d 70 43 73 72 2d 3e 70 4e 65 78 74 pCsr=pCsr->pNext
1678c 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 73 );. assert( pCs
1678d 72 3d 3d 30 20 7c 7c 20 70 2d 3e 69 6e 54 72 61 r==0 || p->inTra
1678e 6e 73 3e 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 3b ns>TRANS_NONE );
1678f 0a 0a 20 20 62 74 72 65 65 43 6c 65 61 72 48 61 .. btreeClearHa
16790 73 43 6f 6e 74 65 6e 74 28 70 42 74 29 3b 0a 20 sContent(pBt);.
16791 20 69 66 28 20 70 43 73 72 20 29 7b 0a 20 20 20 if( pCsr ){.
16792 20 64 6f 77 6e 67 72 61 64 65 41 6c 6c 53 68 61 downgradeAllSha
16793 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 redCacheTableLoc
16794 6b 73 28 70 29 3b 0a 20 20 20 20 70 2d 3e 69 6e ks(p);. p->in
16795 54 72 61 6e 73 20 3d 20 54 52 41 4e 53 5f 52 45 Trans = TRANS_RE
16796 41 44 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 AD;. }else{.
16797 20 2f 2a 20 49 66 20 74 68 65 20 68 61 6e 64 6c /* If the handl
16798 65 20 68 61 64 20 61 6e 79 20 6b 69 6e 64 20 6f e had any kind o
16799 66 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70 f transaction op
1679a 65 6e 2c 20 64 65 63 72 65 6d 65 6e 74 20 74 68 en, decrement th
1679b 65 20 0a 20 20 20 20 2a 2a 20 74 72 61 6e 73 61 e . ** transa
1679c 63 74 69 6f 6e 20 63 6f 75 6e 74 20 6f 66 20 74 ction count of t
1679d 68 65 20 73 68 61 72 65 64 20 62 74 72 65 65 2e he shared btree.
1679e 20 49 66 20 74 68 65 20 74 72 61 6e 73 61 63 74 If the transact
1679f 69 6f 6e 20 63 6f 75 6e 74 20 0a 20 20 20 20 2a ion count . *
167a0 2a 20 72 65 61 63 68 65 73 20 30 2c 20 73 65 74 * reaches 0, set
167a1 20 74 68 65 20 73 68 61 72 65 64 20 73 74 61 74 the shared stat
167a2 65 20 74 6f 20 54 52 41 4e 53 5f 4e 4f 4e 45 2e e to TRANS_NONE.
167a3 20 54 68 65 20 75 6e 6c 6f 63 6b 42 74 72 65 65 The unlockBtree
167a4 49 66 55 6e 75 73 65 64 28 29 0a 20 20 20 20 2a IfUnused(). *
167a5 2a 20 63 61 6c 6c 20 62 65 6c 6f 77 20 77 69 6c * call below wil
167a6 6c 20 75 6e 6c 6f 63 6b 20 74 68 65 20 70 61 67 l unlock the pag
167a7 65 72 2e 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 er. */. if(
167a8 70 2d 3e 69 6e 54 72 61 6e 73 21 3d 54 52 41 4e p->inTrans!=TRAN
167a9 53 5f 4e 4f 4e 45 20 29 7b 0a 20 20 20 20 20 20 S_NONE ){.
167aa 63 6c 65 61 72 41 6c 6c 53 68 61 72 65 64 43 61 clearAllSharedCa
167ab 63 68 65 54 61 62 6c 65 4c 6f 63 6b 73 28 70 29 cheTableLocks(p)
167ac 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 6e 54 72 ;. pBt->nTr
167ad 61 6e 73 61 63 74 69 6f 6e 2d 2d 3b 0a 20 20 20 ansaction--;.
167ae 20 20 20 69 66 28 20 30 3d 3d 70 42 74 2d 3e 6e if( 0==pBt->n
167af 54 72 61 6e 73 61 63 74 69 6f 6e 20 29 7b 0a 20 Transaction ){.
167b0 20 20 20 20 20 20 20 70 42 74 2d 3e 69 6e 54 72 pBt->inTr
167b1 61 6e 73 61 63 74 69 6f 6e 20 3d 20 54 52 41 4e ansaction = TRAN
167b2 53 5f 4e 4f 4e 45 3b 0a 20 20 20 20 20 20 7d 0a S_NONE;. }.
167b3 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 53 65 }.. /* Se
167b4 74 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 72 t the current tr
167b5 61 6e 73 61 63 74 69 6f 6e 20 73 74 61 74 65 20 ansaction state
167b6 74 6f 20 54 52 41 4e 53 5f 4e 4f 4e 45 20 61 6e to TRANS_NONE an
167b7 64 20 75 6e 6c 6f 63 6b 20 74 68 65 20 0a 20 20 d unlock the .
167b8 20 20 2a 2a 20 70 61 67 65 72 20 69 66 20 74 68 ** pager if th
167b9 69 73 20 63 61 6c 6c 20 63 6c 6f 73 65 64 20 74 is call closed t
167ba 68 65 20 6f 6e 6c 79 20 72 65 61 64 20 6f 72 20 he only read or
167bb 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f write transactio
167bc 6e 2e 20 20 2a 2f 0a 20 20 20 20 70 2d 3e 69 6e n. */. p->in
167bd 54 72 61 6e 73 20 3d 20 54 52 41 4e 53 5f 4e 4f Trans = TRANS_NO
167be 4e 45 3b 0a 20 20 20 20 75 6e 6c 6f 63 6b 42 74 NE;. unlockBt
167bf 72 65 65 49 66 55 6e 75 73 65 64 28 70 42 74 29 reeIfUnused(pBt)
167c0 3b 0a 20 20 7d 0a 0a 20 20 62 74 72 65 65 49 6e ;. }.. btreeIn
167c1 74 65 67 72 69 74 79 28 70 29 3b 0a 7d 0a 0a 2f tegrity(p);.}../
167c2 2a 0a 2a 2a 20 43 6f 6d 6d 69 74 20 74 68 65 20 *.** Commit the
167c3 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 75 72 72 transaction curr
167c4 65 6e 74 6c 79 20 69 6e 20 70 72 6f 67 72 65 73 ently in progres
167c5 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f s..**.** This ro
167c6 75 74 69 6e 65 20 69 6d 70 6c 65 6d 65 6e 74 73 utine implements
167c7 20 74 68 65 20 73 65 63 6f 6e 64 20 70 68 61 73 the second phas
167c8 65 20 6f 66 20 61 20 32 2d 70 68 61 73 65 20 63 e of a 2-phase c
167c9 6f 6d 6d 69 74 2e 20 20 54 68 65 0a 2a 2a 20 73 ommit. The.** s
167ca 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 qlite3BtreeCommi
167cb 74 50 68 61 73 65 4f 6e 65 28 29 20 72 6f 75 74 tPhaseOne() rout
167cc 69 6e 65 20 64 6f 65 73 20 74 68 65 20 66 69 72 ine does the fir
167cd 73 74 20 70 68 61 73 65 20 61 6e 64 20 73 68 6f st phase and sho
167ce 75 6c 64 0a 2a 2a 20 62 65 20 69 6e 76 6f 6b 65 uld.** be invoke
167cf 64 20 70 72 69 6f 72 20 74 6f 20 63 61 6c 6c 69 d prior to calli
167d0 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e ng this routine.
167d1 20 20 54 68 65 20 73 71 6c 69 74 65 33 42 74 72 The sqlite3Btr
167d2 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 eeCommitPhaseOne
167d3 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 64 69 ().** routine di
167d4 64 20 61 6c 6c 20 74 68 65 20 77 6f 72 6b 20 6f d all the work o
167d5 66 20 77 72 69 74 69 6e 67 20 69 6e 66 6f 72 6d f writing inform
167d6 61 74 69 6f 6e 20 6f 75 74 20 74 6f 20 64 69 73 ation out to dis
167d7 6b 20 61 6e 64 20 66 6c 75 73 68 69 6e 67 20 74 k and flushing t
167d8 68 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74 73 20 73 he.** contents s
167d9 6f 20 74 68 61 74 20 74 68 65 79 20 61 72 65 20 o that they are
167da 77 72 69 74 74 65 6e 20 6f 6e 74 6f 20 74 68 65 written onto the
167db 20 64 69 73 6b 20 70 6c 61 74 74 65 72 2e 20 20 disk platter.
167dc 41 6c 6c 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 All this.** rout
167dd 69 6e 65 20 68 61 73 20 74 6f 20 64 6f 20 69 73 ine has to do is
167de 20 64 65 6c 65 74 65 20 6f 72 20 74 72 75 6e 63 delete or trunc
167df 61 74 65 20 6f 72 20 7a 65 72 6f 20 74 68 65 20 ate or zero the
167e0 68 65 61 64 65 72 20 69 6e 20 74 68 65 0a 2a 2a header in the.**
167e1 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f the rollback jo
167e2 75 72 6e 61 6c 20 28 77 68 69 63 68 20 63 61 75 urnal (which cau
167e3 73 65 73 20 74 68 65 20 74 72 61 6e 73 61 63 74 ses the transact
167e4 69 6f 6e 20 74 6f 20 63 6f 6d 6d 69 74 29 20 61 ion to commit) a
167e5 6e 64 0a 2a 2a 20 64 72 6f 70 20 6c 6f 63 6b 73 nd.** drop locks
167e6 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 69 6c ..**.** This wil
167e7 6c 20 72 65 6c 65 61 73 65 20 74 68 65 20 77 72 l release the wr
167e8 69 74 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 ite lock on the
167e9 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 database file.
167ea 49 66 20 74 68 65 72 65 0a 2a 2a 20 61 72 65 20 If there.** are
167eb 6e 6f 20 61 63 74 69 76 65 20 63 75 72 73 6f 72 no active cursor
167ec 73 2c 20 69 74 20 61 6c 73 6f 20 72 65 6c 65 61 s, it also relea
167ed 73 65 73 20 74 68 65 20 72 65 61 64 20 6c 6f 63 ses the read loc
167ee 6b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 k..*/.SQLITE_PRI
167ef 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
167f0 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 BtreeCommitPhase
167f1 54 77 6f 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 Two(Btree *p){.
167f2 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d BtShared *pBt =
167f3 20 70 2d 3e 70 42 74 3b 0a 0a 20 20 73 71 6c 69 p->pBt;.. sqli
167f4 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 te3BtreeEnter(p)
167f5 3b 0a 20 20 62 74 72 65 65 49 6e 74 65 67 72 69 ;. btreeIntegri
167f6 74 79 28 70 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 ty(p);.. /* If
167f7 74 68 65 20 68 61 6e 64 6c 65 20 68 61 73 20 61 the handle has a
167f8 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 write-transacti
167f9 6f 6e 20 6f 70 65 6e 2c 20 63 6f 6d 6d 69 74 20 on open, commit
167fa 74 68 65 20 73 68 61 72 65 64 2d 62 74 72 65 65 the shared-btree
167fb 73 20 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 s . ** transact
167fc 69 6f 6e 20 61 6e 64 20 73 65 74 20 74 68 65 20 ion and set the
167fd 73 68 61 72 65 64 20 73 74 61 74 65 20 74 6f 20 shared state to
167fe 54 52 41 4e 53 5f 52 45 41 44 2e 0a 20 20 2a 2f TRANS_READ.. */
167ff 0a 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e . if( p->inTran
16800 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 s==TRANS_WRITE )
16801 7b 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 {. int rc;.
16802 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 69 assert( pBt->i
16803 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 nTransaction==TR
16804 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 20 ANS_WRITE );.
16805 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 6e 54 assert( pBt->nT
16806 72 61 6e 73 61 63 74 69 6f 6e 3e 30 20 29 3b 0a ransaction>0 );.
16807 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
16808 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 PagerCommitPhase
16809 54 77 6f 28 70 42 74 2d 3e 70 50 61 67 65 72 29 Two(pBt->pPager)
1680a 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 ;. if( rc!=SQ
1680b 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
1680c 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 sqlite3BtreeLea
1680d 76 65 28 70 29 3b 0a 20 20 20 20 20 20 72 65 74 ve(p);. ret
1680e 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 urn rc;. }.
1680f 20 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 pBt->inTransac
16810 74 69 6f 6e 20 3d 20 54 52 41 4e 53 5f 52 45 41 tion = TRANS_REA
16811 44 3b 0a 20 20 7d 0a 0a 20 20 62 74 72 65 65 45 D;. }.. btreeE
16812 6e 64 54 72 61 6e 73 61 63 74 69 6f 6e 28 70 29 ndTransaction(p)
16813 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 ;. sqlite3Btree
16814 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 Leave(p);. retu
16815 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
16816 0a 2f 2a 0a 2a 2a 20 44 6f 20 62 6f 74 68 20 70 ./*.** Do both p
16817 68 61 73 65 73 20 6f 66 20 61 20 63 6f 6d 6d 69 hases of a commi
16818 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 t..*/.SQLITE_PRI
16819 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
1681a 42 74 72 65 65 43 6f 6d 6d 69 74 28 42 74 72 65 BtreeCommit(Btre
1681b 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b e *p){. int rc;
1681c 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 . sqlite3BtreeE
1681d 6e 74 65 72 28 70 29 3b 0a 20 20 72 63 20 3d 20 nter(p);. rc =
1681e 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d sqlite3BtreeComm
1681f 69 74 50 68 61 73 65 4f 6e 65 28 70 2c 20 30 29 itPhaseOne(p, 0)
16820 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 ;. if( rc==SQLI
16821 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20 TE_OK ){. rc
16822 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f = sqlite3BtreeCo
16823 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 70 29 3b mmitPhaseTwo(p);
16824 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 . }. sqlite3Bt
16825 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 reeLeave(p);. r
16826 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 eturn rc;.}..#if
16827 6e 64 65 66 20 4e 44 45 42 55 47 0a 2f 2a 0a 2a ndef NDEBUG./*.*
16828 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d * Return the num
16829 62 65 72 20 6f 66 20 77 72 69 74 65 2d 63 75 72 ber of write-cur
1682a 73 6f 72 73 20 6f 70 65 6e 20 6f 6e 20 74 68 69 sors open on thi
1682b 73 20 68 61 6e 64 6c 65 2e 20 54 68 69 73 20 69 s handle. This i
1682c 73 20 66 6f 72 20 75 73 65 0a 2a 2a 20 69 6e 20 s for use.** in
1682d 61 73 73 65 72 74 28 29 20 65 78 70 72 65 73 73 assert() express
1682e 69 6f 6e 73 2c 20 73 6f 20 69 74 20 69 73 20 6f ions, so it is o
1682f 6e 6c 79 20 63 6f 6d 70 69 6c 65 64 20 69 66 20 nly compiled if
16830 4e 44 45 42 55 47 20 69 73 20 6e 6f 74 0a 2a 2a NDEBUG is not.**
16831 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 defined..**.**
16832 46 6f 72 20 74 68 65 20 70 75 72 70 6f 73 65 73 For the purposes
16833 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 of this routine
16834 2c 20 61 20 77 72 69 74 65 2d 63 75 72 73 6f 72 , a write-cursor
16835 20 69 73 20 61 6e 79 20 63 75 72 73 6f 72 20 74 is any cursor t
16836 68 61 74 0a 2a 2a 20 69 73 20 63 61 70 61 62 6c hat.** is capabl
16837 65 20 6f 66 20 77 72 69 74 69 6e 67 20 74 6f 20 e of writing to
16838 74 68 65 20 64 61 74 61 62 73 65 2e 20 20 54 68 the databse. Th
16839 61 74 20 6d 65 61 6e 73 20 74 68 65 20 63 75 72 at means the cur
1683a 73 6f 72 20 77 61 73 0a 2a 2a 20 6f 72 69 67 69 sor was.** origi
1683b 6e 61 6c 6c 79 20 6f 70 65 6e 65 64 20 66 6f 72 nally opened for
1683c 20 77 72 69 74 69 6e 67 20 61 6e 64 20 74 68 65 writing and the
1683d 20 63 75 72 73 6f 72 20 68 61 73 20 6e 6f 74 20 cursor has not
1683e 62 65 20 64 69 73 61 62 6c 65 64 0a 2a 2a 20 62 be disabled.** b
1683f 79 20 68 61 76 69 6e 67 20 69 74 73 20 73 74 61 y having its sta
16840 74 65 20 63 68 61 6e 67 65 64 20 74 6f 20 43 55 te changed to CU
16841 52 53 4f 52 5f 46 41 55 4c 54 2e 0a 2a 2f 0a 73 RSOR_FAULT..*/.s
16842 74 61 74 69 63 20 69 6e 74 20 63 6f 75 6e 74 57 tatic int countW
16843 72 69 74 65 43 75 72 73 6f 72 73 28 42 74 53 68 riteCursors(BtSh
16844 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 42 74 ared *pBt){. Bt
16845 43 75 72 73 6f 72 20 2a 70 43 75 72 3b 0a 20 20 Cursor *pCur;.
16846 69 6e 74 20 72 20 3d 20 30 3b 0a 20 20 66 6f 72 int r = 0;. for
16847 28 70 43 75 72 3d 70 42 74 2d 3e 70 43 75 72 73 (pCur=pBt->pCurs
16848 6f 72 3b 20 70 43 75 72 3b 20 70 43 75 72 3d 70 or; pCur; pCur=p
16849 43 75 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 Cur->pNext){.
1684a 20 69 66 28 20 70 43 75 72 2d 3e 77 72 46 6c 61 if( pCur->wrFla
1684b 67 20 26 26 20 70 43 75 72 2d 3e 65 53 74 61 74 g && pCur->eStat
1684c 65 21 3d 43 55 52 53 4f 52 5f 46 41 55 4c 54 20 e!=CURSOR_FAULT
1684d 29 20 72 2b 2b 3b 20 0a 20 20 7d 0a 20 20 72 65 ) r++; . }. re
1684e 74 75 72 6e 20 72 3b 0a 7d 0a 23 65 6e 64 69 66 turn r;.}.#endif
1684f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ../*.** This rou
16850 74 69 6e 65 20 73 65 74 73 20 74 68 65 20 73 74 tine sets the st
16851 61 74 65 20 74 6f 20 43 55 52 53 4f 52 5f 46 41 ate to CURSOR_FA
16852 55 4c 54 20 61 6e 64 20 74 68 65 20 65 72 72 6f ULT and the erro
16853 72 0a 2a 2a 20 63 6f 64 65 20 74 6f 20 65 72 72 r.** code to err
16854 43 6f 64 65 20 66 6f 72 20 65 76 65 72 79 20 63 Code for every c
16855 75 72 73 6f 72 20 6f 6e 20 42 74 53 68 61 72 65 ursor on BtShare
16856 64 20 74 68 61 74 20 70 42 74 72 65 65 0a 2a 2a d that pBtree.**
16857 20 72 65 66 65 72 65 6e 63 65 73 2e 0a 2a 2a 0a references..**.
16858 2a 2a 20 45 76 65 72 79 20 63 75 72 73 6f 72 20 ** Every cursor
16859 69 73 20 74 72 69 70 70 65 64 2c 20 69 6e 63 6c is tripped, incl
1685a 75 64 69 6e 67 20 63 75 72 73 6f 72 73 20 74 68 uding cursors th
1685b 61 74 20 62 65 6c 6f 6e 67 0a 2a 2a 20 74 6f 20 at belong.** to
1685c 6f 74 68 65 72 20 64 61 74 61 62 61 73 65 20 63 other database c
1685d 6f 6e 6e 65 63 74 69 6f 6e 73 20 74 68 61 74 20 onnections that
1685e 68 61 70 70 65 6e 20 74 6f 20 62 65 20 73 68 61 happen to be sha
1685f 72 69 6e 67 0a 2a 2a 20 74 68 65 20 63 61 63 68 ring.** the cach
16860 65 20 77 69 74 68 20 70 42 74 72 65 65 2e 0a 2a e with pBtree..*
16861 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
16862 65 20 67 65 74 73 20 63 61 6c 6c 65 64 20 77 68 e gets called wh
16863 65 6e 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6f 63 en a rollback oc
16864 63 75 72 73 2e 0a 2a 2a 20 41 6c 6c 20 63 75 72 curs..** All cur
16865 73 6f 72 73 20 75 73 69 6e 67 20 74 68 65 20 73 sors using the s
16866 61 6d 65 20 63 61 63 68 65 20 6d 75 73 74 20 62 ame cache must b
16867 65 20 74 72 69 70 70 65 64 0a 2a 2a 20 74 6f 20 e tripped.** to
16868 70 72 65 76 65 6e 74 20 74 68 65 6d 20 66 72 6f prevent them fro
16869 6d 20 74 72 79 69 6e 67 20 74 6f 20 75 73 65 20 m trying to use
1686a 74 68 65 20 62 74 72 65 65 20 61 66 74 65 72 0a the btree after.
1686b 2a 2a 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 2e ** the rollback.
1686c 20 20 54 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6d The rollback m
1686d 61 79 20 68 61 76 65 20 64 65 6c 65 74 65 64 20 ay have deleted
1686e 74 61 62 6c 65 73 0a 2a 2a 20 6f 72 20 6d 6f 76 tables.** or mov
1686f 65 64 20 72 6f 6f 74 20 70 61 67 65 73 2c 20 73 ed root pages, s
16870 6f 20 69 74 20 69 73 20 6e 6f 74 20 73 75 66 66 o it is not suff
16871 69 63 69 65 6e 74 20 74 6f 0a 2a 2a 20 73 61 76 icient to.** sav
16872 65 20 74 68 65 20 73 74 61 74 65 20 6f 66 20 74 e the state of t
16873 68 65 20 63 75 72 73 6f 72 2e 20 20 54 68 65 20 he cursor. The
16874 63 75 72 73 6f 72 20 6d 75 73 74 20 62 65 0a 2a cursor must be.*
16875 2a 20 69 6e 76 61 6c 69 64 61 74 65 64 2e 0a 2a * invalidated..*
16876 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
16877 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 void sqlite3Btr
16878 65 65 54 72 69 70 41 6c 6c 43 75 72 73 6f 72 73 eeTripAllCursors
16879 28 42 74 72 65 65 20 2a 70 42 74 72 65 65 2c 20 (Btree *pBtree,
1687a 69 6e 74 20 65 72 72 43 6f 64 65 29 7b 0a 20 20 int errCode){.
1687b 42 74 43 75 72 73 6f 72 20 2a 70 3b 0a 20 20 73 BtCursor *p;. s
1687c 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 qlite3BtreeEnter
1687d 28 70 42 74 72 65 65 29 3b 0a 20 20 66 6f 72 28 (pBtree);. for(
1687e 70 3d 70 42 74 72 65 65 2d 3e 70 42 74 2d 3e 70 p=pBtree->pBt->p
1687f 43 75 72 73 6f 72 3b 20 70 3b 20 70 3d 70 2d 3e Cursor; p; p=p->
16880 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 6e 74 20 pNext){. int
16881 69 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 i;. sqlite3Bt
16882 72 65 65 43 6c 65 61 72 43 75 72 73 6f 72 28 70 reeClearCursor(p
16883 29 3b 0a 20 20 20 20 70 2d 3e 65 53 74 61 74 65 );. p->eState
16884 20 3d 20 43 55 52 53 4f 52 5f 46 41 55 4c 54 3b = CURSOR_FAULT;
16885 0a 20 20 20 20 70 2d 3e 73 6b 69 70 4e 65 78 74 . p->skipNext
16886 20 3d 20 65 72 72 43 6f 64 65 3b 0a 20 20 20 20 = errCode;.
16887 66 6f 72 28 69 3d 30 3b 20 69 3c 3d 70 2d 3e 69 for(i=0; i<=p->i
16888 50 61 67 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 Page; i++){.
16889 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 2d releasePage(p-
1688a 3e 61 70 50 61 67 65 5b 69 5d 29 3b 0a 20 20 20 >apPage[i]);.
1688b 20 20 20 70 2d 3e 61 70 50 61 67 65 5b 69 5d 20 p->apPage[i]
1688c 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 = 0;. }. }.
1688d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 sqlite3BtreeLea
1688e 76 65 28 70 42 74 72 65 65 29 3b 0a 7d 0a 0a 2f ve(pBtree);.}../
1688f 2a 0a 2a 2a 20 52 6f 6c 6c 62 61 63 6b 20 74 68 *.** Rollback th
16890 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 6e e transaction in
16891 20 70 72 6f 67 72 65 73 73 2e 20 20 41 6c 6c 20 progress. All
16892 63 75 72 73 6f 72 73 20 77 69 6c 6c 20 62 65 0a cursors will be.
16893 2a 2a 20 69 6e 76 61 6c 69 64 65 64 20 62 79 20 ** invalided by
16894 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 2e 20 this operation.
16895 20 41 6e 79 20 61 74 74 65 6d 70 74 20 74 6f 20 Any attempt to
16896 75 73 65 20 61 20 63 75 72 73 6f 72 0a 2a 2a 20 use a cursor.**
16897 74 68 61 74 20 77 61 73 20 6f 70 65 6e 20 61 74 that was open at
16898 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f the beginning o
16899 66 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e f this operation
1689a 20 77 69 6c 6c 20 72 65 73 75 6c 74 0a 2a 2a 20 will result.**
1689b 69 6e 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2a 0a in an error..**.
1689c 2a 2a 20 54 68 69 73 20 77 69 6c 6c 20 72 65 6c ** This will rel
1689d 65 61 73 65 20 74 68 65 20 77 72 69 74 65 20 6c ease the write l
1689e 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 ock on the datab
1689f 61 73 65 20 66 69 6c 65 2e 20 20 49 66 20 74 68 ase file. If th
168a0 65 72 65 0a 2a 2a 20 61 72 65 20 6e 6f 20 61 63 ere.** are no ac
168a1 74 69 76 65 20 63 75 72 73 6f 72 73 2c 20 69 74 tive cursors, it
168a2 20 61 6c 73 6f 20 72 65 6c 65 61 73 65 73 20 74 also releases t
168a3 68 65 20 72 65 61 64 20 6c 6f 63 6b 2e 0a 2a 2f he read lock..*/
168a4 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
168a5 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
168a6 52 6f 6c 6c 62 61 63 6b 28 42 74 72 65 65 20 2a Rollback(Btree *
168a7 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 p){. int rc;.
168a8 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 BtShared *pBt =
168a9 70 2d 3e 70 42 74 3b 0a 20 20 4d 65 6d 50 61 67 p->pBt;. MemPag
168aa 65 20 2a 70 50 61 67 65 31 3b 0a 0a 20 20 73 71 e *pPage1;.. sq
168ab 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 lite3BtreeEnter(
168ac 70 29 3b 0a 20 20 72 63 20 3d 20 73 61 76 65 41 p);. rc = saveA
168ad 6c 6c 43 75 72 73 6f 72 73 28 70 42 74 2c 20 30 llCursors(pBt, 0
168ae 2c 20 30 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 , 0);.#ifndef SQ
168af 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 LITE_OMIT_SHARED
168b0 5f 43 41 43 48 45 0a 20 20 69 66 28 20 72 63 21 _CACHE. if( rc!
168b1 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
168b2 20 20 2f 2a 20 54 68 69 73 20 69 73 20 61 20 68 /* This is a h
168b3 6f 72 72 69 62 6c 65 20 73 69 74 75 61 74 69 6f orrible situatio
168b4 6e 2e 20 41 6e 20 49 4f 20 6f 72 20 6d 61 6c 6c n. An IO or mall
168b5 6f 63 28 29 20 65 72 72 6f 72 20 6f 63 63 75 72 oc() error occur
168b6 72 65 64 20 77 68 69 6c 73 74 0a 20 20 20 20 2a red whilst. *
168b7 2a 20 74 72 79 69 6e 67 20 74 6f 20 73 61 76 65 * trying to save
168b8 20 63 75 72 73 6f 72 20 70 6f 73 69 74 69 6f 6e cursor position
168b9 73 2e 20 49 66 20 74 68 69 73 20 69 73 20 61 6e s. If this is an
168ba 20 61 75 74 6f 6d 61 74 69 63 20 72 6f 6c 6c 62 automatic rollb
168bb 61 63 6b 20 28 61 73 0a 20 20 20 20 2a 2a 20 74 ack (as. ** t
168bc 68 65 20 72 65 73 75 6c 74 20 6f 66 20 61 20 63 he result of a c
168bd 6f 6e 73 74 72 61 69 6e 74 2c 20 6d 61 6c 6c 6f onstraint, mallo
168be 63 28 29 20 66 61 69 6c 75 72 65 20 6f 72 20 49 c() failure or I
168bf 4f 20 65 72 72 6f 72 29 20 74 68 65 6e 20 0a 20 O error) then .
168c0 20 20 20 2a 2a 20 74 68 65 20 63 61 63 68 65 20 ** the cache
168c1 6d 61 79 20 62 65 20 69 6e 74 65 72 6e 61 6c 6c may be internall
168c2 79 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20 28 y inconsistent (
168c3 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 76 61 6c 69 not contain vali
168c4 64 20 74 72 65 65 73 29 20 73 6f 0a 20 20 20 20 d trees) so.
168c5 2a 2a 20 77 65 20 63 61 6e 6e 6f 74 20 73 69 6d ** we cannot sim
168c6 70 6c 79 20 72 65 74 75 72 6e 20 74 68 65 20 65 ply return the e
168c7 72 72 6f 72 20 74 6f 20 74 68 65 20 63 61 6c 6c rror to the call
168c8 65 72 2e 20 49 6e 73 74 65 61 64 2c 20 61 62 6f er. Instead, abo
168c9 72 74 20 0a 20 20 20 20 2a 2a 20 61 6c 6c 20 71 rt . ** all q
168ca 75 65 72 69 65 73 20 74 68 61 74 20 6d 61 79 20 ueries that may
168cb 62 65 20 75 73 69 6e 67 20 61 6e 79 20 6f 66 20 be using any of
168cc 74 68 65 20 63 75 72 73 6f 72 73 20 74 68 61 74 the cursors that
168cd 20 66 61 69 6c 65 64 20 74 6f 20 73 61 76 65 2e failed to save.
168ce 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 . */. sqli
168cf 74 65 33 42 74 72 65 65 54 72 69 70 41 6c 6c 43 te3BtreeTripAllC
168d0 75 72 73 6f 72 73 28 70 2c 20 72 63 29 3b 0a 20 ursors(p, rc);.
168d1 20 7d 0a 23 65 6e 64 69 66 0a 20 20 62 74 72 65 }.#endif. btre
168d2 65 49 6e 74 65 67 72 69 74 79 28 70 29 3b 0a 0a eIntegrity(p);..
168d3 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 if( p->inTrans
168d4 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 7b ==TRANS_WRITE ){
168d5 0a 20 20 20 20 69 6e 74 20 72 63 32 3b 0a 0a 20 . int rc2;..
168d6 20 20 20 61 73 73 65 72 74 28 20 54 52 41 4e 53 assert( TRANS
168d7 5f 57 52 49 54 45 3d 3d 70 42 74 2d 3e 69 6e 54 _WRITE==pBt->inT
168d8 72 61 6e 73 61 63 74 69 6f 6e 20 29 3b 0a 20 20 ransaction );.
168d9 20 20 72 63 32 20 3d 20 73 71 6c 69 74 65 33 50 rc2 = sqlite3P
168da 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 70 42 74 agerRollback(pBt
168db 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 20 20 69 ->pPager);. i
168dc 66 28 20 72 63 32 21 3d 53 51 4c 49 54 45 5f 4f f( rc2!=SQLITE_O
168dd 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 K ){. rc =
168de 72 63 32 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 rc2;. }..
168df 2f 2a 20 54 68 65 20 72 6f 6c 6c 62 61 63 6b 20 /* The rollback
168e0 6d 61 79 20 68 61 76 65 20 64 65 73 74 72 6f 79 may have destroy
168e1 65 64 20 74 68 65 20 70 50 61 67 65 31 2d 3e 61 ed the pPage1->a
168e2 44 61 74 61 20 76 61 6c 75 65 2e 20 20 53 6f 0a Data value. So.
168e3 20 20 20 20 2a 2a 20 63 61 6c 6c 20 62 74 72 65 ** call btre
168e4 65 47 65 74 50 61 67 65 28 29 20 6f 6e 20 70 61 eGetPage() on pa
168e5 67 65 20 31 20 61 67 61 69 6e 20 74 6f 20 6d 61 ge 1 again to ma
168e6 6b 65 0a 20 20 20 20 2a 2a 20 73 75 72 65 20 70 ke. ** sure p
168e7 50 61 67 65 31 2d 3e 61 44 61 74 61 20 69 73 20 Page1->aData is
168e8 73 65 74 20 63 6f 72 72 65 63 74 6c 79 2e 20 2a set correctly. *
168e9 2f 0a 20 20 20 20 69 66 28 20 62 74 72 65 65 47 /. if( btreeG
168ea 65 74 50 61 67 65 28 70 42 74 2c 20 31 2c 20 26 etPage(pBt, 1, &
168eb 70 50 61 67 65 31 2c 20 30 29 3d 3d 53 51 4c 49 pPage1, 0)==SQLI
168ec 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 TE_OK ){. r
168ed 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65 eleasePage(pPage
168ee 31 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 1);. }. as
168ef 73 65 72 74 28 20 63 6f 75 6e 74 57 72 69 74 65 sert( countWrite
168f0 43 75 72 73 6f 72 73 28 70 42 74 29 3d 3d 30 20 Cursors(pBt)==0
168f1 29 3b 0a 20 20 20 20 70 42 74 2d 3e 69 6e 54 72 );. pBt->inTr
168f2 61 6e 73 61 63 74 69 6f 6e 20 3d 20 54 52 41 4e ansaction = TRAN
168f3 53 5f 52 45 41 44 3b 0a 20 20 7d 0a 0a 20 20 62 S_READ;. }.. b
168f4 74 72 65 65 45 6e 64 54 72 61 6e 73 61 63 74 69 treeEndTransacti
168f5 6f 6e 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 on(p);. sqlite3
168f6 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 BtreeLeave(p);.
168f7 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
168f8 2a 0a 2a 2a 20 53 74 61 72 74 20 61 20 73 74 61 *.** Start a sta
168f9 74 65 6d 65 6e 74 20 73 75 62 74 72 61 6e 73 61 tement subtransa
168fa 63 74 69 6f 6e 2e 20 54 68 65 20 73 75 62 74 72 ction. The subtr
168fb 61 6e 73 61 63 74 69 6f 6e 20 63 61 6e 20 63 61 ansaction can ca
168fc 6e 20 62 65 20 72 6f 6c 6c 65 64 0a 2a 2a 20 62 n be rolled.** b
168fd 61 63 6b 20 69 6e 64 65 70 65 6e 64 65 6e 74 6c ack independentl
168fe 79 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 74 72 y of the main tr
168ff 61 6e 73 61 63 74 69 6f 6e 2e 20 59 6f 75 20 6d ansaction. You m
16900 75 73 74 20 73 74 61 72 74 20 61 20 74 72 61 6e ust start a tran
16901 73 61 63 74 69 6f 6e 20 0a 2a 2a 20 62 65 66 6f saction .** befo
16902 72 65 20 73 74 61 72 74 69 6e 67 20 61 20 73 75 re starting a su
16903 62 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 54 68 btransaction. Th
16904 65 20 73 75 62 74 72 61 6e 73 61 63 74 69 6f 6e e subtransaction
16905 20 69 73 20 65 6e 64 65 64 20 61 75 74 6f 6d 61 is ended automa
16906 74 69 63 61 6c 6c 79 20 0a 2a 2a 20 69 66 20 74 tically .** if t
16907 68 65 20 6d 61 69 6e 20 74 72 61 6e 73 61 63 74 he main transact
16908 69 6f 6e 20 63 6f 6d 6d 69 74 73 20 6f 72 20 72 ion commits or r
16909 6f 6c 6c 73 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a olls back..**.**
1690a 20 53 74 61 74 65 6d 65 6e 74 20 73 75 62 74 72 Statement subtr
1690b 61 6e 73 61 63 74 69 6f 6e 73 20 61 72 65 20 75 ansactions are u
1690c 73 65 64 20 61 72 6f 75 6e 64 20 69 6e 64 69 76 sed around indiv
1690d 69 64 75 61 6c 20 53 51 4c 20 73 74 61 74 65 6d idual SQL statem
1690e 65 6e 74 73 0a 2a 2a 20 74 68 61 74 20 61 72 65 ents.** that are
1690f 20 63 6f 6e 74 61 69 6e 65 64 20 77 69 74 68 69 contained withi
16910 6e 20 61 20 42 45 47 49 4e 2e 2e 2e 43 4f 4d 4d n a BEGIN...COMM
16911 49 54 20 62 6c 6f 63 6b 2e 20 20 49 66 20 61 20 IT block. If a
16912 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 65 72 constraint.** er
16913 72 6f 72 20 6f 63 63 75 72 73 20 77 69 74 68 69 ror occurs withi
16914 6e 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 2c n the statement,
16915 20 74 68 65 20 65 66 66 65 63 74 20 6f 66 20 74 the effect of t
16916 68 61 74 20 6f 6e 65 20 73 74 61 74 65 6d 65 6e hat one statemen
16917 74 0a 2a 2a 20 63 61 6e 20 62 65 20 72 6f 6c 6c t.** can be roll
16918 65 64 20 62 61 63 6b 20 77 69 74 68 6f 75 74 20 ed back without
16919 68 61 76 69 6e 67 20 74 6f 20 72 6f 6c 6c 62 61 having to rollba
1691a 63 6b 20 74 68 65 20 65 6e 74 69 72 65 20 74 72 ck the entire tr
1691b 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a ansaction..**.**
1691c 20 41 20 73 74 61 74 65 6d 65 6e 74 20 73 75 62 A statement sub
1691d 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 -transaction is
1691e 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 20 61 implemented as a
1691f 6e 20 61 6e 6f 6e 79 6d 6f 75 73 20 73 61 76 65 n anonymous save
16920 70 6f 69 6e 74 2e 20 54 68 65 0a 2a 2a 20 76 61 point. The.** va
16921 6c 75 65 20 70 61 73 73 65 64 20 61 73 20 74 68 lue passed as th
16922 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 e second paramet
16923 65 72 20 69 73 20 74 68 65 20 74 6f 74 61 6c 20 er is the total
16924 6e 75 6d 62 65 72 20 6f 66 20 73 61 76 65 70 6f number of savepo
16925 69 6e 74 73 2c 0a 2a 2a 20 69 6e 63 6c 75 64 69 ints,.** includi
16926 6e 67 20 74 68 65 20 6e 65 77 20 61 6e 6f 6e 79 ng the new anony
16927 6d 6f 75 73 20 73 61 76 65 70 6f 69 6e 74 2c 20 mous savepoint,
16928 6f 70 65 6e 20 6f 6e 20 74 68 65 20 42 2d 54 72 open on the B-Tr
16929 65 65 2e 20 69 2e 65 2e 20 69 66 20 74 68 65 72 ee. i.e. if ther
1692a 65 0a 2a 2a 20 61 72 65 20 6e 6f 20 61 63 74 69 e.** are no acti
1692b 76 65 20 73 61 76 65 70 6f 69 6e 74 73 20 61 6e ve savepoints an
1692c 64 20 6e 6f 20 6f 74 68 65 72 20 73 74 61 74 65 d no other state
1692d 6d 65 6e 74 2d 74 72 61 6e 73 61 63 74 69 6f 6e ment-transaction
1692e 73 20 6f 70 65 6e 2c 0a 2a 2a 20 69 53 74 61 74 s open,.** iStat
1692f 65 6d 65 6e 74 20 69 73 20 31 2e 20 54 68 69 73 ement is 1. This
16930 20 61 6e 6f 6e 79 6d 6f 75 73 20 73 61 76 65 70 anonymous savep
16931 6f 69 6e 74 20 63 61 6e 20 62 65 20 72 65 6c 65 oint can be rele
16932 61 73 65 64 20 6f 72 20 72 6f 6c 6c 65 64 20 62 ased or rolled b
16933 61 63 6b 0a 2a 2a 20 75 73 69 6e 67 20 74 68 65 ack.** using the
16934 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 61 76 sqlite3BtreeSav
16935 65 70 6f 69 6e 74 28 29 20 66 75 6e 63 74 69 6f epoint() functio
16936 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 n..*/.SQLITE_PRI
16937 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
16938 42 74 72 65 65 42 65 67 69 6e 53 74 6d 74 28 42 BtreeBeginStmt(B
16939 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 69 53 74 tree *p, int iSt
1693a 61 74 65 6d 65 6e 74 29 7b 0a 20 20 69 6e 74 20 atement){. int
1693b 72 63 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a rc;. BtShared *
1693c 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 pBt = p->pBt;.
1693d 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 sqlite3BtreeEnte
1693e 72 28 70 29 3b 0a 20 20 61 73 73 65 72 74 28 20 r(p);. assert(
1693f 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e p->inTrans==TRAN
16940 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 61 73 73 S_WRITE );. ass
16941 65 72 74 28 20 70 42 74 2d 3e 72 65 61 64 4f 6e ert( pBt->readOn
16942 6c 79 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 ly==0 );. asser
16943 74 28 20 69 53 74 61 74 65 6d 65 6e 74 3e 30 20 t( iStatement>0
16944 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 53 74 );. assert( iSt
16945 61 74 65 6d 65 6e 74 3e 70 2d 3e 64 62 2d 3e 6e atement>p->db->n
16946 53 61 76 65 70 6f 69 6e 74 20 29 3b 0a 20 20 69 Savepoint );. i
16947 66 28 20 4e 45 56 45 52 28 70 2d 3e 69 6e 54 72 f( NEVER(p->inTr
16948 61 6e 73 21 3d 54 52 41 4e 53 5f 57 52 49 54 45 ans!=TRANS_WRITE
16949 20 7c 7c 20 70 42 74 2d 3e 72 65 61 64 4f 6e 6c || pBt->readOnl
1694a 79 29 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 y) ){. rc = S
1694b 51 4c 49 54 45 5f 49 4e 54 45 52 4e 41 4c 3b 0a QLITE_INTERNAL;.
1694c 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 }else{. ass
1694d 65 72 74 28 20 70 42 74 2d 3e 69 6e 54 72 61 6e ert( pBt->inTran
1694e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 saction==TRANS_W
1694f 52 49 54 45 20 29 3b 0a 20 20 20 20 2f 2a 20 41 RITE );. /* A
16950 74 20 74 68 65 20 70 61 67 65 72 20 6c 65 76 65 t the pager leve
16951 6c 2c 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74 l, a statement t
16952 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 20 ransaction is a
16953 73 61 76 65 70 6f 69 6e 74 20 77 69 74 68 0a 20 savepoint with.
16954 20 20 20 2a 2a 20 61 6e 20 69 6e 64 65 78 20 67 ** an index g
16955 72 65 61 74 65 72 20 74 68 61 6e 20 61 6c 6c 20 reater than all
16956 73 61 76 65 70 6f 69 6e 74 73 20 63 72 65 61 74 savepoints creat
16957 65 64 20 65 78 70 6c 69 63 69 74 6c 79 20 75 73 ed explicitly us
16958 69 6e 67 0a 20 20 20 20 2a 2a 20 53 51 4c 20 73 ing. ** SQL s
16959 74 61 74 65 6d 65 6e 74 73 2e 20 49 74 20 69 73 tatements. It is
1695a 20 69 6c 6c 65 67 61 6c 20 74 6f 20 6f 70 65 6e illegal to open
1695b 2c 20 72 65 6c 65 61 73 65 20 6f 72 20 72 6f 6c , release or rol
1695c 6c 62 61 63 6b 20 61 6e 79 0a 20 20 20 20 2a 2a lback any. **
1695d 20 73 75 63 68 20 73 61 76 65 70 6f 69 6e 74 73 such savepoints
1695e 20 77 68 69 6c 65 20 74 68 65 20 73 74 61 74 65 while the state
1695f 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e ment transaction
16960 20 73 61 76 65 70 6f 69 6e 74 20 69 73 20 61 63 savepoint is ac
16961 74 69 76 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 tive.. */.
16962 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
16963 65 72 4f 70 65 6e 53 61 76 65 70 6f 69 6e 74 28 erOpenSavepoint(
16964 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 69 53 74 pBt->pPager, iSt
16965 61 74 65 6d 65 6e 74 29 3b 0a 20 20 7d 0a 20 20 atement);. }.
16966 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 sqlite3BtreeLeav
16967 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 e(p);. return r
16968 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 c;.}../*.** The
16969 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 second argument
1696a 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e to this function
1696b 2c 20 6f 70 2c 20 69 73 20 61 6c 77 61 79 73 20 , op, is always
1696c 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 SAVEPOINT_ROLLBA
1696d 43 4b 0a 2a 2a 20 6f 72 20 53 41 56 45 50 4f 49 CK.** or SAVEPOI
1696e 4e 54 5f 52 45 4c 45 41 53 45 2e 20 54 68 69 73 NT_RELEASE. This
1696f 20 66 75 6e 63 74 69 6f 6e 20 65 69 74 68 65 72 function either
16970 20 72 65 6c 65 61 73 65 73 20 6f 72 20 72 6f 6c releases or rol
16971 6c 73 20 62 61 63 6b 20 74 68 65 0a 2a 2a 20 73 ls back the.** s
16972 61 76 65 70 6f 69 6e 74 20 69 64 65 6e 74 69 66 avepoint identif
16973 69 65 64 20 62 79 20 70 61 72 61 6d 65 74 65 72 ied by parameter
16974 20 69 53 61 76 65 70 6f 69 6e 74 2c 20 64 65 70 iSavepoint, dep
16975 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 76 61 ending on the va
16976 6c 75 65 20 0a 2a 2a 20 6f 66 20 6f 70 2e 0a 2a lue .** of op..*
16977 2a 0a 2a 2a 20 4e 6f 72 6d 61 6c 6c 79 2c 20 69 *.** Normally, i
16978 53 61 76 65 70 6f 69 6e 74 20 69 73 20 67 72 65 Savepoint is gre
16979 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75 ater than or equ
1697a 61 6c 20 74 6f 20 7a 65 72 6f 2e 20 48 6f 77 65 al to zero. Howe
1697b 76 65 72 2c 20 69 66 20 6f 70 20 69 73 0a 2a 2a ver, if op is.**
1697c 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 SAVEPOINT_ROLLB
1697d 41 43 4b 2c 20 74 68 65 6e 20 69 53 61 76 65 70 ACK, then iSavep
1697e 6f 69 6e 74 20 6d 61 79 20 61 6c 73 6f 20 62 65 oint may also be
1697f 20 2d 31 2e 20 49 6e 20 74 68 69 73 20 63 61 73 -1. In this cas
16980 65 20 74 68 65 20 0a 2a 2a 20 63 6f 6e 74 65 6e e the .** conten
16981 74 73 20 6f 66 20 74 68 65 20 65 6e 74 69 72 65 ts of the entire
16982 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 72 65 transaction are
16983 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 54 68 rolled back. Th
16984 69 73 20 69 73 20 64 69 66 66 65 72 65 6e 74 0a is is different.
16985 2a 2a 20 66 72 6f 6d 20 61 20 6e 6f 72 6d 61 6c ** from a normal
16986 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 72 6f 6c transaction rol
16987 6c 62 61 63 6b 2c 20 61 73 20 6e 6f 20 6c 6f 63 lback, as no loc
16988 6b 73 20 61 72 65 20 72 65 6c 65 61 73 65 64 20 ks are released
16989 61 6e 64 20 74 68 65 0a 2a 2a 20 74 72 61 6e 73 and the.** trans
1698a 61 63 74 69 6f 6e 20 72 65 6d 61 69 6e 73 20 6f action remains o
1698b 70 65 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 pen..*/.SQLITE_P
1698c 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
1698d 65 33 42 74 72 65 65 53 61 76 65 70 6f 69 6e 74 e3BtreeSavepoint
1698e 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 6f (Btree *p, int o
1698f 70 2c 20 69 6e 74 20 69 53 61 76 65 70 6f 69 6e p, int iSavepoin
16990 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 t){. int rc = S
16991 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 QLITE_OK;. if(
16992 70 20 26 26 20 70 2d 3e 69 6e 54 72 61 6e 73 3d p && p->inTrans=
16993 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 7b 0a =TRANS_WRITE ){.
16994 20 20 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 BtShared *pB
16995 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 20 20 t = p->pBt;.
16996 61 73 73 65 72 74 28 20 6f 70 3d 3d 53 41 56 45 assert( op==SAVE
16997 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 20 7c 7c POINT_RELEASE ||
16998 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 op==SAVEPOINT_R
16999 4f 4c 4c 42 41 43 4b 20 29 3b 0a 20 20 20 20 61 OLLBACK );. a
1699a 73 73 65 72 74 28 20 69 53 61 76 65 70 6f 69 6e ssert( iSavepoin
1699b 74 3e 3d 30 20 7c 7c 20 28 69 53 61 76 65 70 6f t>=0 || (iSavepo
1699c 69 6e 74 3d 3d 2d 31 20 26 26 20 6f 70 3d 3d 53 int==-1 && op==S
1699d 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 AVEPOINT_ROLLBAC
1699e 4b 29 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 K) );. sqlite
1699f 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 3BtreeEnter(p);.
169a0 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
169a1 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 28 70 PagerSavepoint(p
169a2 42 74 2d 3e 70 50 61 67 65 72 2c 20 6f 70 2c 20 Bt->pPager, op,
169a3 69 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 20 iSavepoint);.
169a4 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
169a5 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d OK ){. rc =
169a6 20 6e 65 77 44 61 74 61 62 61 73 65 28 70 42 74 newDatabase(pBt
169a7 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c );. }. sql
169a8 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 ite3BtreeLeave(p
169a9 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
169aa 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 rc;.}../*.** Cre
169ab 61 74 65 20 61 20 6e 65 77 20 63 75 72 73 6f 72 ate a new cursor
169ac 20 66 6f 72 20 74 68 65 20 42 54 72 65 65 20 77 for the BTree w
169ad 68 6f 73 65 20 72 6f 6f 74 20 69 73 20 6f 6e 20 hose root is on
169ae 74 68 65 20 70 61 67 65 0a 2a 2a 20 69 54 61 62 the page.** iTab
169af 6c 65 2e 20 49 66 20 61 20 72 65 61 64 2d 6f 6e le. If a read-on
169b0 6c 79 20 63 75 72 73 6f 72 20 69 73 20 72 65 71 ly cursor is req
169b1 75 65 73 74 65 64 2c 20 69 74 20 69 73 20 61 73 uested, it is as
169b2 73 75 6d 65 64 20 74 68 61 74 0a 2a 2a 20 74 68 sumed that.** th
169b3 65 20 63 61 6c 6c 65 72 20 61 6c 72 65 61 64 79 e caller already
169b4 20 68 61 73 20 61 74 20 6c 65 61 73 74 20 61 20 has at least a
169b5 72 65 61 64 2d 6f 6e 6c 79 20 74 72 61 6e 73 61 read-only transa
169b6 63 74 69 6f 6e 20 6f 70 65 6e 0a 2a 2a 20 6f 6e ction open.** on
169b7 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61 6c the database al
169b8 72 65 61 64 79 2e 20 49 66 20 61 20 77 72 69 74 ready. If a writ
169b9 65 2d 63 75 72 73 6f 72 20 69 73 20 72 65 71 75 e-cursor is requ
169ba 65 73 74 65 64 2c 20 74 68 65 6e 0a 2a 2a 20 74 ested, then.** t
169bb 68 65 20 63 61 6c 6c 65 72 20 69 73 20 61 73 73 he caller is ass
169bc 75 6d 65 64 20 74 6f 20 68 61 76 65 20 61 6e 20 umed to have an
169bd 6f 70 65 6e 20 77 72 69 74 65 20 74 72 61 6e 73 open write trans
169be 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 action..**.** If
169bf 20 77 72 46 6c 61 67 3d 3d 30 2c 20 74 68 65 6e wrFlag==0, then
169c0 20 74 68 65 20 63 75 72 73 6f 72 20 63 61 6e 20 the cursor can
169c1 6f 6e 6c 79 20 62 65 20 75 73 65 64 20 66 6f 72 only be used for
169c2 20 72 65 61 64 69 6e 67 2e 0a 2a 2a 20 49 66 20 reading..** If
169c3 77 72 46 6c 61 67 3d 3d 31 2c 20 74 68 65 6e 20 wrFlag==1, then
169c4 74 68 65 20 63 75 72 73 6f 72 20 63 61 6e 20 62 the cursor can b
169c5 65 20 75 73 65 64 20 66 6f 72 20 72 65 61 64 69 e used for readi
169c6 6e 67 20 6f 72 20 66 6f 72 0a 2a 2a 20 77 72 69 ng or for.** wri
169c7 74 69 6e 67 20 69 66 20 6f 74 68 65 72 20 63 6f ting if other co
169c8 6e 64 69 74 69 6f 6e 73 20 66 6f 72 20 77 72 69 nditions for wri
169c9 74 69 6e 67 20 61 72 65 20 61 6c 73 6f 20 6d 65 ting are also me
169ca 74 2e 20 20 54 68 65 73 65 0a 2a 2a 20 61 72 65 t. These.** are
169cb 20 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20 the conditions
169cc 74 68 61 74 20 6d 75 73 74 20 62 65 20 6d 65 74 that must be met
169cd 20 69 6e 20 6f 72 64 65 72 20 66 6f 72 20 77 72 in order for wr
169ce 69 74 69 6e 67 20 74 6f 0a 2a 2a 20 62 65 20 61 iting to.** be a
169cf 6c 6c 6f 77 65 64 3a 0a 2a 2a 0a 2a 2a 20 31 3a llowed:.**.** 1:
169d0 20 20 54 68 65 20 63 75 72 73 6f 72 20 6d 75 73 The cursor mus
169d1 74 20 68 61 76 65 20 62 65 65 6e 20 6f 70 65 6e t have been open
169d2 65 64 20 77 69 74 68 20 77 72 46 6c 61 67 3d 3d ed with wrFlag==
169d3 31 0a 2a 2a 0a 2a 2a 20 32 3a 20 20 4f 74 68 65 1.**.** 2: Othe
169d4 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 r database conne
169d5 63 74 69 6f 6e 73 20 74 68 61 74 20 73 68 61 72 ctions that shar
169d6 65 20 74 68 65 20 73 61 6d 65 20 70 61 67 65 72 e the same pager
169d7 20 63 61 63 68 65 0a 2a 2a 20 20 20 20 20 62 75 cache.** bu
169d8 74 20 77 68 69 63 68 20 61 72 65 20 6e 6f 74 20 t which are not
169d9 69 6e 20 74 68 65 20 52 45 41 44 5f 55 4e 43 4f in the READ_UNCO
169da 4d 4d 49 54 54 45 44 20 73 74 61 74 65 20 6d 61 MMITTED state ma
169db 79 20 6e 6f 74 20 68 61 76 65 0a 2a 2a 20 20 20 y not have.**
169dc 20 20 63 75 72 73 6f 72 73 20 6f 70 65 6e 20 77 cursors open w
169dd 69 74 68 20 77 72 46 6c 61 67 3d 3d 30 20 6f 6e ith wrFlag==0 on
169de 20 74 68 65 20 73 61 6d 65 20 74 61 62 6c 65 2e the same table.
169df 20 20 4f 74 68 65 72 77 69 73 65 0a 2a 2a 20 20 Otherwise.**
169e0 20 20 20 74 68 65 20 63 68 61 6e 67 65 73 20 6d the changes m
169e1 61 64 65 20 62 79 20 74 68 69 73 20 77 72 69 74 ade by this writ
169e2 65 20 63 75 72 73 6f 72 20 77 6f 75 6c 64 20 62 e cursor would b
169e3 65 20 76 69 73 69 62 6c 65 20 74 6f 0a 2a 2a 20 e visible to.**
169e4 20 20 20 20 74 68 65 20 72 65 61 64 20 63 75 72 the read cur
169e5 73 6f 72 73 20 69 6e 20 74 68 65 20 6f 74 68 65 sors in the othe
169e6 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 r database conne
169e7 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 33 3a 20 ction..**.** 3:
169e8 20 54 68 65 20 64 61 74 61 62 61 73 65 20 6d 75 The database mu
169e9 73 74 20 62 65 20 77 72 69 74 61 62 6c 65 20 28 st be writable (
169ea 6e 6f 74 20 6f 6e 20 72 65 61 64 2d 6f 6e 6c 79 not on read-only
169eb 20 6d 65 64 69 61 29 0a 2a 2a 0a 2a 2a 20 34 3a media).**.** 4:
169ec 20 20 54 68 65 72 65 20 6d 75 73 74 20 62 65 20 There must be
169ed 61 6e 20 61 63 74 69 76 65 20 74 72 61 6e 73 61 an active transa
169ee 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 20 ction..**.** No
169ef 63 68 65 63 6b 69 6e 67 20 69 73 20 64 6f 6e 65 checking is done
169f0 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 to make sure th
169f1 61 74 20 70 61 67 65 20 69 54 61 62 6c 65 20 72 at page iTable r
169f2 65 61 6c 6c 79 20 69 73 20 74 68 65 0a 2a 2a 20 eally is the.**
169f3 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 61 20 62 root page of a b
169f4 2d 74 72 65 65 2e 20 20 49 66 20 69 74 20 69 73 -tree. If it is
169f5 20 6e 6f 74 2c 20 74 68 65 6e 20 74 68 65 20 63 not, then the c
169f6 75 72 73 6f 72 20 61 63 71 75 69 72 65 64 0a 2a ursor acquired.*
169f7 2a 20 77 69 6c 6c 20 6e 6f 74 20 77 6f 72 6b 20 * will not work
169f8 63 6f 72 72 65 63 74 6c 79 2e 0a 2a 2a 0a 2a 2a correctly..**.**
169f9 20 49 74 20 69 73 20 61 73 73 75 6d 65 64 20 74 It is assumed t
169fa 68 61 74 20 74 68 65 20 73 71 6c 69 74 65 33 42 hat the sqlite3B
169fb 74 72 65 65 43 75 72 73 6f 72 53 69 7a 65 28 29 treeCursorSize()
169fc 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 bytes of memory
169fd 20 0a 2a 2a 20 70 6f 69 6e 74 65 64 20 74 6f 20 .** pointed to
169fe 62 79 20 70 43 75 72 20 68 61 76 65 20 62 65 65 by pCur have bee
169ff 6e 20 7a 65 72 6f 65 64 20 62 79 20 74 68 65 20 n zeroed by the
16a00 63 61 6c 6c 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 caller..*/.stati
16a01 63 20 69 6e 74 20 62 74 72 65 65 43 75 72 73 6f c int btreeCurso
16a02 72 28 0a 20 20 42 74 72 65 65 20 2a 70 2c 20 20 r(. Btree *p,
16a03 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16a04 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
16a05 68 65 20 62 74 72 65 65 20 2a 2f 0a 20 20 69 6e he btree */. in
16a06 74 20 69 54 61 62 6c 65 2c 20 20 20 20 20 20 20 t iTable,
16a07 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16a08 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 70 61 67 /* Root pag
16a09 65 20 6f 66 20 74 61 62 6c 65 20 74 6f 20 6f 70 e of table to op
16a0a 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 77 72 46 6c en */. int wrFl
16a0b 61 67 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 ag,
16a0c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
16a0d 2a 20 31 20 74 6f 20 77 72 69 74 65 2e 20 30 20 * 1 to write. 0
16a0e 72 65 61 64 2d 6f 6e 6c 79 20 2a 2f 0a 20 20 73 read-only */. s
16a0f 74 72 75 63 74 20 4b 65 79 49 6e 66 6f 20 2a 70 truct KeyInfo *p
16a10 4b 65 79 49 6e 66 6f 2c 20 20 20 20 20 20 20 20 KeyInfo,
16a11 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 61 /* First a
16a12 72 67 20 74 6f 20 63 6f 6d 70 61 72 69 73 6f 6e rg to comparison
16a13 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 42 function */. B
16a14 74 43 75 72 73 6f 72 20 2a 70 43 75 72 20 20 20 tCursor *pCur
16a15 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16a16 20 20 20 20 20 20 2f 2a 20 53 70 61 63 65 20 66 /* Space f
16a17 6f 72 20 6e 65 77 20 63 75 72 73 6f 72 20 2a 2f or new cursor */
16a18 0a 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a .){. BtShared *
16a19 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 20 20 20 pBt = p->pBt;
16a1a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
16a1b 53 68 61 72 65 64 20 62 2d 74 72 65 65 20 68 61 Shared b-tree ha
16a1c 6e 64 6c 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 ndle */.. asser
16a1d 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 t( sqlite3BtreeH
16a1e 6f 6c 64 73 4d 75 74 65 78 28 70 29 20 29 3b 0a oldsMutex(p) );.
16a1f 20 20 61 73 73 65 72 74 28 20 77 72 46 6c 61 67 assert( wrFlag
16a20 3d 3d 30 20 7c 7c 20 77 72 46 6c 61 67 3d 3d 31 ==0 || wrFlag==1
16a21 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 66 6f );.. /* The fo
16a22 6c 6c 6f 77 69 6e 67 20 61 73 73 65 72 74 20 73 llowing assert s
16a23 74 61 74 65 6d 65 6e 74 73 20 76 65 72 69 66 79 tatements verify
16a24 20 74 68 61 74 20 69 66 20 74 68 69 73 20 69 73 that if this is
16a25 20 61 20 73 68 61 72 61 62 6c 65 20 0a 20 20 2a a sharable . *
16a26 2a 20 62 2d 74 72 65 65 20 64 61 74 61 62 61 73 * b-tree databas
16a27 65 2c 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f e, the connectio
16a28 6e 20 69 73 20 68 6f 6c 64 69 6e 67 20 74 68 65 n is holding the
16a29 20 72 65 71 75 69 72 65 64 20 74 61 62 6c 65 20 required table
16a2a 6c 6f 63 6b 73 2c 20 0a 20 20 2a 2a 20 61 6e 64 locks, . ** and
16a2b 20 74 68 61 74 20 6e 6f 20 6f 74 68 65 72 20 63 that no other c
16a2c 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 73 20 61 6e onnection has an
16a2d 79 20 6f 70 65 6e 20 63 75 72 73 6f 72 20 74 68 y open cursor th
16a2e 61 74 20 63 6f 6e 66 6c 69 63 74 73 20 77 69 74 at conflicts wit
16a2f 68 20 0a 20 20 2a 2a 20 74 68 69 73 20 6c 6f 63 h . ** this loc
16a30 6b 2e 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 k. */. assert(
16a31 20 68 61 73 53 68 61 72 65 64 43 61 63 68 65 54 hasSharedCacheT
16a32 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 69 54 61 62 ableLock(p, iTab
16a33 6c 65 2c 20 70 4b 65 79 49 6e 66 6f 21 3d 30 2c le, pKeyInfo!=0,
16a34 20 77 72 46 6c 61 67 2b 31 29 20 29 3b 0a 20 20 wrFlag+1) );.
16a35 61 73 73 65 72 74 28 20 77 72 46 6c 61 67 3d 3d assert( wrFlag==
16a36 30 20 7c 7c 20 21 68 61 73 52 65 61 64 43 6f 6e 0 || !hasReadCon
16a37 66 6c 69 63 74 73 28 70 2c 20 69 54 61 62 6c 65 flicts(p, iTable
16a38 29 20 29 3b 0a 0a 20 20 2f 2a 20 41 73 73 65 72 ) );.. /* Asser
16a39 74 20 74 68 61 74 20 74 68 65 20 63 61 6c 6c 65 t that the calle
16a3a 72 20 68 61 73 20 6f 70 65 6e 65 64 20 74 68 65 r has opened the
16a3b 20 72 65 71 75 69 72 65 64 20 74 72 61 6e 73 61 required transa
16a3c 63 74 69 6f 6e 2e 20 2a 2f 0a 20 20 61 73 73 65 ction. */. asse
16a3d 72 74 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3e 54 rt( p->inTrans>T
16a3e 52 41 4e 53 5f 4e 4f 4e 45 20 29 3b 0a 20 20 61 RANS_NONE );. a
16a3f 73 73 65 72 74 28 20 77 72 46 6c 61 67 3d 3d 30 ssert( wrFlag==0
16a40 20 7c 7c 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d || p->inTrans==
16a41 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 TRANS_WRITE );.
16a42 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 70 50 assert( pBt->pP
16a43 61 67 65 31 20 26 26 20 70 42 74 2d 3e 70 50 61 age1 && pBt->pPa
16a44 67 65 31 2d 3e 61 44 61 74 61 20 29 3b 0a 0a 20 ge1->aData );..
16a45 20 69 66 28 20 4e 45 56 45 52 28 77 72 46 6c 61 if( NEVER(wrFla
16a46 67 20 26 26 20 70 42 74 2d 3e 72 65 61 64 4f 6e g && pBt->readOn
16a47 6c 79 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ly) ){. retur
16a48 6e 20 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c n SQLITE_READONL
16a49 59 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 54 61 Y;. }. if( iTa
16a4a 62 6c 65 3d 3d 31 20 26 26 20 70 61 67 65 72 50 ble==1 && pagerP
16a4b 61 67 65 63 6f 75 6e 74 28 70 42 74 29 3d 3d 30 agecount(pBt)==0
16a4c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
16a4d 51 4c 49 54 45 5f 45 4d 50 54 59 3b 0a 20 20 7d QLITE_EMPTY;. }
16a4e 0a 0a 20 20 2f 2a 20 4e 6f 77 20 74 68 61 74 20 .. /* Now that
16a4f 6e 6f 20 6f 74 68 65 72 20 65 72 72 6f 72 73 20 no other errors
16a50 63 61 6e 20 6f 63 63 75 72 2c 20 66 69 6e 69 73 can occur, finis
16a51 68 20 66 69 6c 6c 69 6e 67 20 69 6e 20 74 68 65 h filling in the
16a52 20 42 74 43 75 72 73 6f 72 0a 20 20 2a 2a 20 76 BtCursor. ** v
16a53 61 72 69 61 62 6c 65 73 20 61 6e 64 20 6c 69 6e ariables and lin
16a54 6b 20 74 68 65 20 63 75 72 73 6f 72 20 69 6e 74 k the cursor int
16a55 6f 20 74 68 65 20 42 74 53 68 61 72 65 64 20 6c o the BtShared l
16a56 69 73 74 2e 20 20 2a 2f 0a 20 20 70 43 75 72 2d ist. */. pCur-
16a57 3e 70 67 6e 6f 52 6f 6f 74 20 3d 20 28 50 67 6e >pgnoRoot = (Pgn
16a58 6f 29 69 54 61 62 6c 65 3b 0a 20 20 70 43 75 72 o)iTable;. pCur
16a59 2d 3e 69 50 61 67 65 20 3d 20 2d 31 3b 0a 20 20 ->iPage = -1;.
16a5a 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 20 3d pCur->pKeyInfo =
16a5b 20 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 70 43 75 pKeyInfo;. pCu
16a5c 72 2d 3e 70 42 74 72 65 65 20 3d 20 70 3b 0a 20 r->pBtree = p;.
16a5d 20 70 43 75 72 2d 3e 70 42 74 20 3d 20 70 42 74 pCur->pBt = pBt
16a5e 3b 0a 20 20 70 43 75 72 2d 3e 77 72 46 6c 61 67 ;. pCur->wrFlag
16a5f 20 3d 20 28 75 38 29 77 72 46 6c 61 67 3b 0a 20 = (u8)wrFlag;.
16a60 20 70 43 75 72 2d 3e 70 4e 65 78 74 20 3d 20 70 pCur->pNext = p
16a61 42 74 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 69 Bt->pCursor;. i
16a62 66 28 20 70 43 75 72 2d 3e 70 4e 65 78 74 20 29 f( pCur->pNext )
16a63 7b 0a 20 20 20 20 70 43 75 72 2d 3e 70 4e 65 78 {. pCur->pNex
16a64 74 2d 3e 70 50 72 65 76 20 3d 20 70 43 75 72 3b t->pPrev = pCur;
16a65 0a 20 20 7d 0a 20 20 70 42 74 2d 3e 70 43 75 72 . }. pBt->pCur
16a66 73 6f 72 20 3d 20 70 43 75 72 3b 0a 20 20 70 43 sor = pCur;. pC
16a67 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 ur->eState = CUR
16a68 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 70 SOR_INVALID;. p
16a69 43 75 72 2d 3e 63 61 63 68 65 64 52 6f 77 69 64 Cur->cachedRowid
16a6a 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 53 = 0;. return S
16a6b 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 53 51 4c 49 QLITE_OK;.}.SQLI
16a6c 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
16a6d 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f qlite3BtreeCurso
16a6e 72 28 0a 20 20 42 74 72 65 65 20 2a 70 2c 20 20 r(. Btree *p,
16a6f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16a70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16a71 20 2f 2a 20 54 68 65 20 62 74 72 65 65 20 2a 2f /* The btree */
16a72 0a 20 20 69 6e 74 20 69 54 61 62 6c 65 2c 20 20 . int iTable,
16a73 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16a74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
16a75 2a 20 52 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 * Root page of t
16a76 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a able to open */.
16a77 20 20 69 6e 74 20 77 72 46 6c 61 67 2c 20 20 20 int wrFlag,
16a78 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16a79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
16a7a 20 31 20 74 6f 20 77 72 69 74 65 2e 20 30 20 72 1 to write. 0 r
16a7b 65 61 64 2d 6f 6e 6c 79 20 2a 2f 0a 20 20 73 74 ead-only */. st
16a7c 72 75 63 74 20 4b 65 79 49 6e 66 6f 20 2a 70 4b ruct KeyInfo *pK
16a7d 65 79 49 6e 66 6f 2c 20 20 20 20 20 20 20 20 20 eyInfo,
16a7e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 /* Fir
16a7f 73 74 20 61 72 67 20 74 6f 20 78 43 6f 6d 70 61 st arg to xCompa
16a80 72 65 28 29 20 2a 2f 0a 20 20 42 74 43 75 72 73 re() */. BtCurs
16a81 6f 72 20 2a 70 43 75 72 20 20 20 20 20 20 20 20 or *pCur
16a82 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16a83 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 6e /* Write n
16a84 65 77 20 63 75 72 73 6f 72 20 68 65 72 65 20 2a ew cursor here *
16a85 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 /.){. int rc;.
16a86 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 sqlite3BtreeEnt
16a87 65 72 28 70 29 3b 0a 20 20 72 63 20 3d 20 62 74 er(p);. rc = bt
16a88 72 65 65 43 75 72 73 6f 72 28 70 2c 20 69 54 61 reeCursor(p, iTa
16a89 62 6c 65 2c 20 77 72 46 6c 61 67 2c 20 70 4b 65 ble, wrFlag, pKe
16a8a 79 49 6e 66 6f 2c 20 70 43 75 72 29 3b 0a 20 20 yInfo, pCur);.
16a8b 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 sqlite3BtreeLeav
16a8c 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 e(p);. return r
16a8d 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 c;.}../*.** Retu
16a8e 72 6e 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 rn the size of a
16a8f 20 42 74 43 75 72 73 6f 72 20 6f 62 6a 65 63 74 BtCursor object
16a90 20 69 6e 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a in bytes..**.**
16a91 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 73 This interfaces
16a92 20 69 73 20 6e 65 65 64 65 64 20 73 6f 20 74 68 is needed so th
16a93 61 74 20 75 73 65 72 73 20 6f 66 20 63 75 72 73 at users of curs
16a94 6f 72 73 20 63 61 6e 20 70 72 65 61 6c 6c 6f 63 ors can prealloc
16a95 61 74 65 0a 2a 2a 20 73 75 66 66 69 63 69 65 6e ate.** sufficien
16a96 74 20 73 74 6f 72 61 67 65 20 74 6f 20 68 6f 6c t storage to hol
16a97 64 20 61 20 63 75 72 73 6f 72 2e 20 20 54 68 65 d a cursor. The
16a98 20 42 74 43 75 72 73 6f 72 20 6f 62 6a 65 63 74 BtCursor object
16a99 20 69 73 20 6f 70 61 71 75 65 0a 2a 2a 20 74 6f is opaque.** to
16a9a 20 75 73 65 72 73 20 73 6f 20 74 68 65 79 20 63 users so they c
16a9b 61 6e 6e 6f 74 20 64 6f 20 74 68 65 20 73 69 7a annot do the siz
16a9c 65 6f 66 28 29 20 74 68 65 6d 73 65 6c 76 65 73 eof() themselves
16a9d 20 2d 20 74 68 65 79 20 6d 75 73 74 20 63 61 6c - they must cal
16a9e 6c 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e l.** this routin
16a9f 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e..*/.SQLITE_PRI
16aa0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
16aa1 42 74 72 65 65 43 75 72 73 6f 72 53 69 7a 65 28 BtreeCursorSize(
16aa2 76 6f 69 64 29 7b 0a 20 20 72 65 74 75 72 6e 20 void){. return
16aa3 73 69 7a 65 6f 66 28 42 74 43 75 72 73 6f 72 29 sizeof(BtCursor)
16aa4 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 ;.}../*.** Set t
16aa5 68 65 20 63 61 63 68 65 64 20 72 6f 77 69 64 20 he cached rowid
16aa6 76 61 6c 75 65 20 6f 66 20 65 76 65 72 79 20 63 value of every c
16aa7 75 72 73 6f 72 20 69 6e 20 74 68 65 20 73 61 6d ursor in the sam
16aa8 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 0a e database file.
16aa9 2a 2a 20 61 73 20 70 43 75 72 20 61 6e 64 20 68 ** as pCur and h
16aaa 61 76 69 6e 67 20 74 68 65 20 73 61 6d 65 20 72 aving the same r
16aab 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 oot page number
16aac 61 73 20 70 43 75 72 2e 20 20 54 68 65 20 76 61 as pCur. The va
16aad 6c 75 65 20 69 73 0a 2a 2a 20 73 65 74 20 74 6f lue is.** set to
16aae 20 69 52 6f 77 69 64 2e 0a 2a 2a 0a 2a 2a 20 4f iRowid..**.** O
16aaf 6e 6c 79 20 70 6f 73 69 74 69 76 65 20 72 6f 77 nly positive row
16ab0 69 64 20 76 61 6c 75 65 73 20 61 72 65 20 63 6f id values are co
16ab1 6e 73 69 64 65 72 65 64 20 76 61 6c 69 64 20 66 nsidered valid f
16ab2 6f 72 20 74 68 69 73 20 63 61 63 68 65 2e 0a 2a or this cache..*
16ab3 2a 20 54 68 65 20 63 61 63 68 65 20 69 73 20 69 * The cache is i
16ab4 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f 20 7a 65 nitialized to ze
16ab5 72 6f 2c 20 69 6e 64 69 63 61 74 69 6e 67 20 61 ro, indicating a
16ab6 6e 20 69 6e 76 61 6c 69 64 20 63 61 63 68 65 2e n invalid cache.
16ab7 0a 2a 2a 20 41 20 62 74 72 65 65 20 77 69 6c 6c .** A btree will
16ab8 20 77 6f 72 6b 20 66 69 6e 65 20 77 69 74 68 20 work fine with
16ab9 7a 65 72 6f 20 6f 72 20 6e 65 67 61 74 69 76 65 zero or negative
16aba 20 72 6f 77 69 64 73 2e 20 20 57 65 20 6a 75 73 rowids. We jus
16abb 74 20 63 61 6e 6e 6f 74 0a 2a 2a 20 63 61 63 68 t cannot.** cach
16abc 65 20 7a 65 72 6f 20 6f 72 20 6e 65 67 61 74 69 e zero or negati
16abd 76 65 20 72 6f 77 69 64 73 2c 20 77 68 69 63 68 ve rowids, which
16abe 20 6d 65 61 6e 73 20 74 61 62 6c 65 73 20 74 68 means tables th
16abf 61 74 20 75 73 65 20 7a 65 72 6f 20 6f 72 0a 2a at use zero or.*
16ac0 2a 20 6e 65 67 61 74 69 76 65 20 72 6f 77 69 64 * negative rowid
16ac1 73 20 6d 69 67 68 74 20 72 75 6e 20 61 20 6c 69 s might run a li
16ac2 74 74 6c 65 20 73 6c 6f 77 65 72 2e 20 20 42 75 ttle slower. Bu
16ac3 74 20 69 6e 20 70 72 61 63 74 69 63 65 2c 20 7a t in practice, z
16ac4 65 72 6f 0a 2a 2a 20 6f 72 20 6e 65 67 61 74 69 ero.** or negati
16ac5 76 65 20 72 6f 77 69 64 73 20 61 72 65 20 76 65 ve rowids are ve
16ac6 72 79 20 75 6e 63 6f 6d 6d 6f 6e 20 73 6f 20 74 ry uncommon so t
16ac7 68 69 73 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 his should not b
16ac8 65 20 61 20 70 72 6f 62 6c 65 6d 2e 0a 2a 2f 0a e a problem..*/.
16ac9 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
16aca 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 oid sqlite3Btree
16acb 53 65 74 43 61 63 68 65 64 52 6f 77 69 64 28 42 SetCachedRowid(B
16acc 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 73 tCursor *pCur, s
16acd 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 52 6f qlite3_int64 iRo
16ace 77 69 64 29 7b 0a 20 20 42 74 43 75 72 73 6f 72 wid){. BtCursor
16acf 20 2a 70 3b 0a 20 20 66 6f 72 28 70 3d 70 43 75 *p;. for(p=pCu
16ad0 72 2d 3e 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b r->pBt->pCursor;
16ad1 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b p; p=p->pNext){
16ad2 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 67 6e 6f . if( p->pgno
16ad3 52 6f 6f 74 3d 3d 70 43 75 72 2d 3e 70 67 6e 6f Root==pCur->pgno
16ad4 52 6f 6f 74 20 29 20 70 2d 3e 63 61 63 68 65 64 Root ) p->cached
16ad5 52 6f 77 69 64 20 3d 20 69 52 6f 77 69 64 3b 0a Rowid = iRowid;.
16ad6 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 43 }. assert( pC
16ad7 75 72 2d 3e 63 61 63 68 65 64 52 6f 77 69 64 3d ur->cachedRowid=
16ad8 3d 69 52 6f 77 69 64 20 29 3b 0a 7d 0a 0a 2f 2a =iRowid );.}../*
16ad9 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 63 .** Return the c
16ada 61 63 68 65 64 20 72 6f 77 69 64 20 66 6f 72 20 ached rowid for
16adb 74 68 65 20 67 69 76 65 6e 20 63 75 72 73 6f 72 the given cursor
16adc 2e 20 20 41 20 6e 65 67 61 74 69 76 65 20 6f 72 . A negative or
16add 20 7a 65 72 6f 0a 2a 2a 20 72 65 74 75 72 6e 20 zero.** return
16ade 76 61 6c 75 65 20 69 6e 64 69 63 61 74 65 73 20 value indicates
16adf 74 68 61 74 20 74 68 65 20 72 6f 77 69 64 20 63 that the rowid c
16ae0 61 63 68 65 20 69 73 20 69 6e 76 61 6c 69 64 20 ache is invalid
16ae1 61 6e 64 20 73 68 6f 75 6c 64 20 62 65 0a 2a 2a and should be.**
16ae2 20 69 67 6e 6f 72 65 64 2e 20 20 49 66 20 74 68 ignored. If th
16ae3 65 20 72 6f 77 69 64 20 63 61 63 68 65 20 68 61 e rowid cache ha
16ae4 73 20 6e 65 76 65 72 20 62 65 66 6f 72 65 20 62 s never before b
16ae5 65 65 6e 20 73 65 74 2c 20 74 68 65 6e 20 61 0a een set, then a.
16ae6 2a 2a 20 7a 65 72 6f 20 69 73 20 72 65 74 75 72 ** zero is retur
16ae7 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ned..*/.SQLITE_P
16ae8 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f 69 RIVATE sqlite3_i
16ae9 6e 74 36 34 20 73 71 6c 69 74 65 33 42 74 72 65 nt64 sqlite3Btre
16aea 65 47 65 74 43 61 63 68 65 64 52 6f 77 69 64 28 eGetCachedRowid(
16aeb 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b BtCursor *pCur){
16aec 0a 20 20 72 65 74 75 72 6e 20 70 43 75 72 2d 3e . return pCur->
16aed 63 61 63 68 65 64 52 6f 77 69 64 3b 0a 7d 0a 0a cachedRowid;.}..
16aee 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 63 75 /*.** Close a cu
16aef 72 73 6f 72 2e 20 20 54 68 65 20 72 65 61 64 20 rsor. The read
16af0 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 lock on the data
16af1 62 61 73 65 20 66 69 6c 65 20 69 73 20 72 65 6c base file is rel
16af2 65 61 73 65 64 0a 2a 2a 20 77 68 65 6e 20 74 68 eased.** when th
16af3 65 20 6c 61 73 74 20 63 75 72 73 6f 72 20 69 73 e last cursor is
16af4 20 63 6c 6f 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 closed..*/.SQLI
16af5 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
16af6 71 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65 qlite3BtreeClose
16af7 43 75 72 73 6f 72 28 42 74 43 75 72 73 6f 72 20 Cursor(BtCursor
16af8 2a 70 43 75 72 29 7b 0a 20 20 42 74 72 65 65 20 *pCur){. Btree
16af9 2a 70 42 74 72 65 65 20 3d 20 70 43 75 72 2d 3e *pBtree = pCur->
16afa 70 42 74 72 65 65 3b 0a 20 20 69 66 28 20 70 42 pBtree;. if( pB
16afb 74 72 65 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 tree ){. int
16afc 69 3b 0a 20 20 20 20 42 74 53 68 61 72 65 64 20 i;. BtShared
16afd 2a 70 42 74 20 3d 20 70 43 75 72 2d 3e 70 42 74 *pBt = pCur->pBt
16afe 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 ;. sqlite3Btr
16aff 65 65 45 6e 74 65 72 28 70 42 74 72 65 65 29 3b eeEnter(pBtree);
16b00 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 . sqlite3Btre
16b01 65 43 6c 65 61 72 43 75 72 73 6f 72 28 70 43 75 eClearCursor(pCu
16b02 72 29 3b 0a 20 20 20 20 69 66 28 20 70 43 75 72 r);. if( pCur
16b03 2d 3e 70 50 72 65 76 20 29 7b 0a 20 20 20 20 20 ->pPrev ){.
16b04 20 70 43 75 72 2d 3e 70 50 72 65 76 2d 3e 70 4e pCur->pPrev->pN
16b05 65 78 74 20 3d 20 70 43 75 72 2d 3e 70 4e 65 78 ext = pCur->pNex
16b06 74 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 t;. }else{.
16b07 20 20 20 20 70 42 74 2d 3e 70 43 75 72 73 6f 72 pBt->pCursor
16b08 20 3d 20 70 43 75 72 2d 3e 70 4e 65 78 74 3b 0a = pCur->pNext;.
16b09 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 43 }. if( pC
16b0a 75 72 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20 ur->pNext ){.
16b0b 20 20 20 70 43 75 72 2d 3e 70 4e 65 78 74 2d 3e pCur->pNext->
16b0c 70 50 72 65 76 20 3d 20 70 43 75 72 2d 3e 70 50 pPrev = pCur->pP
16b0d 72 65 76 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 rev;. }. f
16b0e 6f 72 28 69 3d 30 3b 20 69 3c 3d 70 43 75 72 2d or(i=0; i<=pCur-
16b0f 3e 69 50 61 67 65 3b 20 69 2b 2b 29 7b 0a 20 20 >iPage; i++){.
16b10 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 releasePage(
16b11 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 5d 29 pCur->apPage[i])
16b12 3b 0a 20 20 20 20 7d 0a 20 20 20 20 75 6e 6c 6f ;. }. unlo
16b13 63 6b 42 74 72 65 65 49 66 55 6e 75 73 65 64 28 ckBtreeIfUnused(
16b14 70 42 74 29 3b 0a 20 20 20 20 69 6e 76 61 6c 69 pBt);. invali
16b15 64 61 74 65 4f 76 65 72 66 6c 6f 77 43 61 63 68 dateOverflowCach
16b16 65 28 70 43 75 72 29 3b 0a 20 20 20 20 2f 2a 20 e(pCur);. /*
16b17 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 75 sqlite3_free(pCu
16b18 72 29 3b 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 r); */. sqlit
16b19 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 42 74 e3BtreeLeave(pBt
16b1a 72 65 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 ree);. }. retu
16b1b 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
16b1c 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 ./*.** Make sure
16b1d 20 74 68 65 20 42 74 43 75 72 73 6f 72 2a 20 67 the BtCursor* g
16b1e 69 76 65 6e 20 69 6e 20 74 68 65 20 61 72 67 75 iven in the argu
16b1f 6d 65 6e 74 20 68 61 73 20 61 20 76 61 6c 69 64 ment has a valid
16b20 0a 2a 2a 20 42 74 43 75 72 73 6f 72 2e 69 6e 66 .** BtCursor.inf
16b21 6f 20 73 74 72 75 63 74 75 72 65 2e 20 20 49 66 o structure. If
16b22 20 69 74 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 it is not alrea
16b23 64 79 20 76 61 6c 69 64 2c 20 63 61 6c 6c 0a 2a dy valid, call.*
16b24 2a 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c * btreeParseCell
16b25 28 29 20 74 6f 20 66 69 6c 6c 20 69 74 20 69 6e () to fill it in
16b26 2e 0a 2a 2a 0a 2a 2a 20 42 74 43 75 72 73 6f 72 ..**.** BtCursor
16b27 2e 69 6e 66 6f 20 69 73 20 61 20 63 61 63 68 65 .info is a cache
16b28 20 6f 66 20 74 68 65 20 69 6e 66 6f 72 6d 61 74 of the informat
16b29 69 6f 6e 20 69 6e 20 74 68 65 20 63 75 72 72 65 ion in the curre
16b2a 6e 74 20 63 65 6c 6c 2e 0a 2a 2a 20 55 73 69 6e nt cell..** Usin
16b2b 67 20 74 68 69 73 20 63 61 63 68 65 20 72 65 64 g this cache red
16b2c 75 63 65 73 20 74 68 65 20 6e 75 6d 62 65 72 20 uces the number
16b2d 6f 66 20 63 61 6c 6c 73 20 74 6f 20 62 74 72 65 of calls to btre
16b2e 65 50 61 72 73 65 43 65 6c 6c 28 29 2e 0a 2a 2a eParseCell()..**
16b2f 0a 2a 2a 20 32 30 30 37 2d 30 36 2d 32 35 3a 20 .** 2007-06-25:
16b30 20 54 68 65 72 65 20 69 73 20 61 20 62 75 67 20 There is a bug
16b31 69 6e 20 73 6f 6d 65 20 76 65 72 73 69 6f 6e 73 in some versions
16b32 20 6f 66 20 4d 53 56 43 20 74 68 61 74 20 63 61 of MSVC that ca
16b33 75 73 65 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 69 use the.** compi
16b34 6c 65 72 20 74 6f 20 63 72 61 73 68 20 77 68 65 ler to crash whe
16b35 6e 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28 29 20 n getCellInfo()
16b36 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 is implemented a
16b37 73 20 61 20 6d 61 63 72 6f 2e 0a 2a 2a 20 42 75 s a macro..** Bu
16b38 74 20 74 68 65 72 65 20 69 73 20 61 20 6d 65 61 t there is a mea
16b39 73 75 72 65 61 62 6c 65 20 73 70 65 65 64 20 61 sureable speed a
16b3a 64 76 61 6e 74 61 67 65 20 74 6f 20 75 73 69 6e dvantage to usin
16b3b 67 20 74 68 65 20 6d 61 63 72 6f 20 6f 6e 20 67 g the macro on g
16b3c 63 63 0a 2a 2a 20 28 77 68 65 6e 20 6c 65 73 73 cc.** (when less
16b3d 20 63 6f 6d 70 69 6c 65 72 20 6f 70 74 69 6d 69 compiler optimi
16b3e 7a 61 74 69 6f 6e 73 20 6c 69 6b 65 20 2d 4f 73 zations like -Os
16b3f 20 6f 72 20 2d 4f 30 20 61 72 65 20 75 73 65 64 or -O0 are used
16b40 20 61 6e 64 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 and the.** comp
16b41 69 6c 65 72 20 69 73 20 6e 6f 74 20 64 6f 69 6e iler is not doin
16b42 67 20 61 67 72 65 73 73 69 76 65 20 69 6e 6c 69 g agressive inli
16b43 6e 69 6e 67 2e 29 20 20 53 6f 20 77 65 20 75 73 ning.) So we us
16b44 65 20 61 20 72 65 61 6c 20 66 75 6e 63 74 69 6f e a real functio
16b45 6e 0a 2a 2a 20 66 6f 72 20 4d 53 56 43 20 61 6e n.** for MSVC an
16b46 64 20 61 20 6d 61 63 72 6f 20 66 6f 72 20 65 76 d a macro for ev
16b47 65 72 79 74 68 69 6e 67 20 65 6c 73 65 2e 20 20 erything else.
16b48 54 69 63 6b 65 74 20 23 32 34 35 37 2e 0a 2a 2f Ticket #2457..*/
16b49 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a .#ifndef NDEBUG.
16b4a 20 20 73 74 61 74 69 63 20 76 6f 69 64 20 61 73 static void as
16b4b 73 65 72 74 43 65 6c 6c 49 6e 66 6f 28 42 74 43 sertCellInfo(BtC
16b4c 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 ursor *pCur){.
16b4d 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b CellInfo info;
16b4e 0a 20 20 20 20 69 6e 74 20 69 50 61 67 65 20 3d . int iPage =
16b4f 20 70 43 75 72 2d 3e 69 50 61 67 65 3b 0a 20 20 pCur->iPage;.
16b50 20 20 6d 65 6d 73 65 74 28 26 69 6e 66 6f 2c 20 memset(&info,
16b51 30 2c 20 73 69 7a 65 6f 66 28 69 6e 66 6f 29 29 0, sizeof(info))
16b52 3b 0a 20 20 20 20 62 74 72 65 65 50 61 72 73 65 ;. btreeParse
16b53 43 65 6c 6c 28 70 43 75 72 2d 3e 61 70 50 61 67 Cell(pCur->apPag
16b54 65 5b 69 50 61 67 65 5d 2c 20 70 43 75 72 2d 3e e[iPage], pCur->
16b55 61 69 49 64 78 5b 69 50 61 67 65 5d 2c 20 26 69 aiIdx[iPage], &i
16b56 6e 66 6f 29 3b 0a 20 20 20 20 61 73 73 65 72 74 nfo);. assert
16b57 28 20 6d 65 6d 63 6d 70 28 26 69 6e 66 6f 2c 20 ( memcmp(&info,
16b58 26 70 43 75 72 2d 3e 69 6e 66 6f 2c 20 73 69 7a &pCur->info, siz
16b59 65 6f 66 28 69 6e 66 6f 29 29 3d 3d 30 20 29 3b eof(info))==0 );
16b5a 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 23 64 65 . }.#else. #de
16b5b 66 69 6e 65 20 61 73 73 65 72 74 43 65 6c 6c 49 fine assertCellI
16b5c 6e 66 6f 28 78 29 0a 23 65 6e 64 69 66 0a 23 69 nfo(x).#endif.#i
16b5d 66 64 65 66 20 5f 4d 53 43 5f 56 45 52 0a 20 20 fdef _MSC_VER.
16b5e 2f 2a 20 55 73 65 20 61 20 72 65 61 6c 20 66 75 /* Use a real fu
16b5f 6e 63 74 69 6f 6e 20 69 6e 20 4d 53 56 43 20 74 nction in MSVC t
16b60 6f 20 77 6f 72 6b 20 61 72 6f 75 6e 64 20 62 75 o work around bu
16b61 67 73 20 69 6e 20 74 68 61 74 20 63 6f 6d 70 69 gs in that compi
16b62 6c 65 72 2e 20 2a 2f 0a 20 20 73 74 61 74 69 63 ler. */. static
16b63 20 76 6f 69 64 20 67 65 74 43 65 6c 6c 49 6e 66 void getCellInf
16b64 6f 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 o(BtCursor *pCur
16b65 29 7b 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d ){. if( pCur-
16b66 3e 69 6e 66 6f 2e 6e 53 69 7a 65 3d 3d 30 20 29 >info.nSize==0 )
16b67 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 50 61 67 {. int iPag
16b68 65 20 3d 20 70 43 75 72 2d 3e 69 50 61 67 65 3b e = pCur->iPage;
16b69 0a 20 20 20 20 20 20 62 74 72 65 65 50 61 72 73 . btreePars
16b6a 65 43 65 6c 6c 28 70 43 75 72 2d 3e 61 70 50 61 eCell(pCur->apPa
16b6b 67 65 5b 69 50 61 67 65 5d 2c 70 43 75 72 2d 3e ge[iPage],pCur->
16b6c 61 69 49 64 78 5b 69 50 61 67 65 5d 2c 26 70 43 aiIdx[iPage],&pC
16b6d 75 72 2d 3e 69 6e 66 6f 29 3b 0a 20 20 20 20 20 ur->info);.
16b6e 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 pCur->validNKey
16b6f 20 3d 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b = 1;. }else{
16b70 0a 20 20 20 20 20 20 61 73 73 65 72 74 43 65 6c . assertCel
16b71 6c 49 6e 66 6f 28 70 43 75 72 29 3b 0a 20 20 20 lInfo(pCur);.
16b72 20 7d 0a 20 20 7d 0a 23 65 6c 73 65 20 2f 2a 20 }. }.#else /*
16b73 69 66 20 6e 6f 74 20 5f 4d 53 43 5f 56 45 52 20 if not _MSC_VER
16b74 2a 2f 0a 20 20 2f 2a 20 55 73 65 20 61 20 6d 61 */. /* Use a ma
16b75 63 72 6f 20 69 6e 20 61 6c 6c 20 6f 74 68 65 72 cro in all other
16b76 20 63 6f 6d 70 69 6c 65 72 73 20 73 6f 20 74 68 compilers so th
16b77 61 74 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 at the function
16b78 69 73 20 69 6e 6c 69 6e 65 64 20 2a 2f 0a 23 64 is inlined */.#d
16b79 65 66 69 6e 65 20 67 65 74 43 65 6c 6c 49 6e 66 efine getCellInf
16b7a 6f 28 70 43 75 72 29 20 20 20 20 20 20 20 20 20 o(pCur)
16b7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16b7c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16b7d 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
16b7e 20 69 66 28 20 70 43 75 72 2d 3e 69 6e 66 6f 2e if( pCur->info.
16b7f 6e 53 69 7a 65 3d 3d 30 20 29 7b 20 20 20 20 20 nSize==0 ){
16b80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16b81 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16b82 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
16b83 20 20 20 20 69 6e 74 20 69 50 61 67 65 20 3d 20 int iPage =
16b84 70 43 75 72 2d 3e 69 50 61 67 65 3b 20 20 20 20 pCur->iPage;
16b85 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16b86 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16b87 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
16b88 0a 20 20 20 20 62 74 72 65 65 50 61 72 73 65 43 . btreeParseC
16b89 65 6c 6c 28 70 43 75 72 2d 3e 61 70 50 61 67 65 ell(pCur->apPage
16b8a 5b 69 50 61 67 65 5d 2c 70 43 75 72 2d 3e 61 69 [iPage],pCur->ai
16b8b 49 64 78 5b 69 50 61 67 65 5d 2c 26 70 43 75 72 Idx[iPage],&pCur
16b8c 2d 3e 69 6e 66 6f 29 3b 20 5c 0a 20 20 20 20 70 ->info); \. p
16b8d 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d Cur->validNKey =
16b8e 20 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 1;
16b8f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16b90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16b91 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 65 \. }e
16b92 6c 73 65 7b 20 20 20 20 20 20 20 20 20 20 20 20 lse{
16b93 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16b94 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16b95 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16b96 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
16b97 20 61 73 73 65 72 74 43 65 6c 6c 49 6e 66 6f 28 assertCellInfo(
16b98 70 43 75 72 29 3b 20 20 20 20 20 20 20 20 20 20 pCur);
16b99 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16b9a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16b9b 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
16b9c 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 4d 53 43 }.#endif /* _MSC
16b9d 5f 56 45 52 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 _VER */..#ifndef
16b9e 20 4e 44 45 42 55 47 20 20 2f 2a 20 54 68 65 20 NDEBUG /* The
16b9f 6e 65 78 74 20 72 6f 75 74 69 6e 65 20 75 73 65 next routine use
16ba0 64 20 6f 6e 6c 79 20 77 69 74 68 69 6e 20 61 73 d only within as
16ba1 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 sert() statement
16ba2 73 20 2a 2f 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 s */./*.** Retur
16ba3 6e 20 74 72 75 65 20 69 66 20 74 68 65 20 67 69 n true if the gi
16ba4 76 65 6e 20 42 74 43 75 72 73 6f 72 20 69 73 20 ven BtCursor is
16ba5 76 61 6c 69 64 2e 20 20 41 20 76 61 6c 69 64 20 valid. A valid
16ba6 63 75 72 73 6f 72 20 69 73 20 6f 6e 65 0a 2a 2a cursor is one.**
16ba7 20 74 68 61 74 20 69 73 20 63 75 72 72 65 6e 74 that is current
16ba8 6c 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 61 ly pointing to a
16ba9 20 72 6f 77 20 69 6e 20 61 20 28 6e 6f 6e 2d 65 row in a (non-e
16baa 6d 70 74 79 29 20 74 61 62 6c 65 2e 0a 2a 2a 20 mpty) table..**
16bab 54 68 69 73 20 69 73 20 61 20 76 65 72 69 66 69 This is a verifi
16bac 63 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65 20 69 cation routine i
16bad 73 20 75 73 65 64 20 6f 6e 6c 79 20 77 69 74 68 s used only with
16bae 69 6e 20 61 73 73 65 72 74 28 29 20 73 74 61 74 in assert() stat
16baf 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 53 51 4c 49 54 ements..*/.SQLIT
16bb0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
16bb1 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 lite3BtreeCursor
16bb2 49 73 56 61 6c 69 64 28 42 74 43 75 72 73 6f 72 IsValid(BtCursor
16bb3 20 2a 70 43 75 72 29 7b 0a 20 20 72 65 74 75 72 *pCur){. retur
16bb4 6e 20 70 43 75 72 20 26 26 20 70 43 75 72 2d 3e n pCur && pCur->
16bb5 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 eState==CURSOR_V
16bb6 41 4c 49 44 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f ALID;.}.#endif /
16bb7 2a 20 4e 44 45 42 55 47 20 2a 2f 0a 0a 2f 2a 0a * NDEBUG */../*.
16bb8 2a 2a 20 53 65 74 20 2a 70 53 69 7a 65 20 74 6f ** Set *pSize to
16bb9 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 the size of the
16bba 20 62 75 66 66 65 72 20 6e 65 65 64 65 64 20 74 buffer needed t
16bbb 6f 20 68 6f 6c 64 20 74 68 65 20 76 61 6c 75 65 o hold the value
16bbc 20 6f 66 0a 2a 2a 20 74 68 65 20 6b 65 79 20 66 of.** the key f
16bbd 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 65 or the current e
16bbe 6e 74 72 79 2e 20 20 49 66 20 74 68 65 20 63 75 ntry. If the cu
16bbf 72 73 6f 72 20 69 73 20 6e 6f 74 20 70 6f 69 6e rsor is not poin
16bc0 74 69 6e 67 0a 2a 2a 20 74 6f 20 61 20 76 61 6c ting.** to a val
16bc1 69 64 20 65 6e 74 72 79 2c 20 2a 70 53 69 7a 65 id entry, *pSize
16bc2 20 69 73 20 73 65 74 20 74 6f 20 30 2e 20 0a 2a is set to 0. .*
16bc3 2a 0a 2a 2a 20 46 6f 72 20 61 20 74 61 62 6c 65 *.** For a table
16bc4 20 77 69 74 68 20 74 68 65 20 49 4e 54 4b 45 59 with the INTKEY
16bc5 20 66 6c 61 67 20 73 65 74 2c 20 74 68 69 73 20 flag set, this
16bc6 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 routine returns
16bc7 74 68 65 20 6b 65 79 0a 2a 2a 20 69 74 73 65 6c the key.** itsel
16bc8 66 2c 20 6e 6f 74 20 74 68 65 20 6e 75 6d 62 65 f, not the numbe
16bc9 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 r of bytes in th
16bca 65 20 6b 65 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 e key..**.** The
16bcb 20 63 61 6c 6c 65 72 20 6d 75 73 74 20 70 6f 73 caller must pos
16bcc 69 74 69 6f 6e 20 74 68 65 20 63 75 72 73 6f 72 ition the cursor
16bcd 20 70 72 69 6f 72 20 74 6f 20 69 6e 76 6f 6b 69 prior to invoki
16bce 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e ng this routine.
16bcf 0a 2a 2a 20 0a 2a 2a 20 54 68 69 73 20 72 6f 75 .** .** This rou
16bd0 74 69 6e 65 20 63 61 6e 6e 6f 74 20 66 61 69 6c tine cannot fail
16bd1 2e 20 20 49 74 20 61 6c 77 61 79 73 20 72 65 74 . It always ret
16bd2 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 urns SQLITE_OK.
16bd3 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 .*/.SQLITE_PRIV
16bd4 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
16bd5 74 72 65 65 4b 65 79 53 69 7a 65 28 42 74 43 75 treeKeySize(BtCu
16bd6 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 36 34 20 rsor *pCur, i64
16bd7 2a 70 53 69 7a 65 29 7b 0a 20 20 61 73 73 65 72 *pSize){. asser
16bd8 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 t( cursorHoldsMu
16bd9 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61 tex(pCur) );. a
16bda 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 ssert( pCur->eSt
16bdb 61 74 65 3d 3d 43 55 52 53 4f 52 5f 49 4e 56 41 ate==CURSOR_INVA
16bdc 4c 49 44 20 7c 7c 20 70 43 75 72 2d 3e 65 53 74 LID || pCur->eSt
16bdd 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 ate==CURSOR_VALI
16bde 44 20 29 3b 0a 20 20 69 66 28 20 70 43 75 72 2d D );. if( pCur-
16bdf 3e 65 53 74 61 74 65 21 3d 43 55 52 53 4f 52 5f >eState!=CURSOR_
16be0 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 2a 70 53 VALID ){. *pS
16be1 69 7a 65 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 ize = 0;. }else
16be2 7b 0a 20 20 20 20 67 65 74 43 65 6c 6c 49 6e 66 {. getCellInf
16be3 6f 28 70 43 75 72 29 3b 0a 20 20 20 20 2a 70 53 o(pCur);. *pS
16be4 69 7a 65 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f ize = pCur->info
16be5 2e 6e 4b 65 79 3b 0a 20 20 7d 0a 20 20 72 65 74 .nKey;. }. ret
16be6 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
16be7 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 2a 70 53 69 ../*.** Set *pSi
16be8 7a 65 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 ze to the number
16be9 20 6f 66 20 62 79 74 65 73 20 6f 66 20 64 61 74 of bytes of dat
16bea 61 20 69 6e 20 74 68 65 20 65 6e 74 72 79 20 74 a in the entry t
16beb 68 65 0a 2a 2a 20 63 75 72 73 6f 72 20 63 75 72 he.** cursor cur
16bec 72 65 6e 74 6c 79 20 70 6f 69 6e 74 73 20 74 6f rently points to
16bed 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c ..**.** The call
16bee 65 72 20 6d 75 73 74 20 67 75 61 72 61 6e 74 65 er must guarante
16bef 65 20 74 68 61 74 20 74 68 65 20 63 75 72 73 6f e that the curso
16bf0 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 74 6f r is pointing to
16bf1 20 61 20 6e 6f 6e 2d 4e 55 4c 4c 0a 2a 2a 20 76 a non-NULL.** v
16bf2 61 6c 69 64 20 65 6e 74 72 79 2e 20 20 49 6e 20 alid entry. In
16bf3 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 74 68 65 other words, the
16bf4 20 63 61 6c 6c 69 6e 67 20 70 72 6f 63 65 64 75 calling procedu
16bf5 72 65 20 6d 75 73 74 20 67 75 61 72 61 6e 74 65 re must guarante
16bf6 65 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 63 75 e.** that the cu
16bf7 72 73 6f 72 20 68 61 73 20 43 75 72 73 6f 72 2e rsor has Cursor.
16bf8 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 eState==CURSOR_V
16bf9 41 4c 49 44 2e 0a 2a 2a 0a 2a 2a 20 46 61 69 6c ALID..**.** Fail
16bfa 75 72 65 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 ure is not possi
16bfb 62 6c 65 2e 20 20 54 68 69 73 20 66 75 6e 63 74 ble. This funct
16bfc 69 6f 6e 20 61 6c 77 61 79 73 20 72 65 74 75 72 ion always retur
16bfd 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a ns SQLITE_OK..**
16bfe 20 49 74 20 6d 69 67 68 74 20 6a 75 73 74 20 61 It might just a
16bff 73 20 77 65 6c 6c 20 62 65 20 61 20 70 72 6f 63 s well be a proc
16c00 65 64 75 72 65 20 28 72 65 74 75 72 6e 69 6e 67 edure (returning
16c01 20 76 6f 69 64 29 20 62 75 74 20 77 65 20 63 6f void) but we co
16c02 6e 74 69 6e 75 65 0a 2a 2a 20 74 6f 20 72 65 74 ntinue.** to ret
16c03 75 72 6e 20 61 6e 20 69 6e 74 65 67 65 72 20 72 urn an integer r
16c04 65 73 75 6c 74 20 63 6f 64 65 20 66 6f 72 20 68 esult code for h
16c05 69 73 74 6f 72 69 63 61 6c 20 72 65 61 73 6f 6e istorical reason
16c06 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 s..*/.SQLITE_PRI
16c07 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
16c08 42 74 72 65 65 44 61 74 61 53 69 7a 65 28 42 74 BtreeDataSize(Bt
16c09 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 75 33 Cursor *pCur, u3
16c0a 32 20 2a 70 53 69 7a 65 29 7b 0a 20 20 61 73 73 2 *pSize){. ass
16c0b 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 ert( cursorHolds
16c0c 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 Mutex(pCur) );.
16c0d 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 assert( pCur->e
16c0e 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 State==CURSOR_VA
16c0f 4c 49 44 20 29 3b 0a 20 20 67 65 74 43 65 6c 6c LID );. getCell
16c10 49 6e 66 6f 28 70 43 75 72 29 3b 0a 20 20 2a 70 Info(pCur);. *p
16c11 53 69 7a 65 20 3d 20 70 43 75 72 2d 3e 69 6e 66 Size = pCur->inf
16c12 6f 2e 6e 44 61 74 61 3b 0a 20 20 72 65 74 75 72 o.nData;. retur
16c13 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
16c14 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 74 68 65 20 /*.** Given the
16c15 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 page number of a
16c16 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 n overflow page
16c17 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 in the database
16c18 28 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20 6f 76 (parameter.** ov
16c19 66 6c 29 2c 20 74 68 69 73 20 66 75 6e 63 74 69 fl), this functi
16c1a 6f 6e 20 66 69 6e 64 73 20 74 68 65 20 70 61 67 on finds the pag
16c1b 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 e number of the
16c1c 6e 65 78 74 20 70 61 67 65 20 69 6e 20 74 68 65 next page in the
16c1d 20 0a 2a 2a 20 6c 69 6e 6b 65 64 20 6c 69 73 74 .** linked list
16c1e 20 6f 66 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 of overflow pag
16c1f 65 73 2e 20 49 66 20 70 6f 73 73 69 62 6c 65 2c es. If possible,
16c20 20 69 74 20 75 73 65 73 20 74 68 65 20 61 75 74 it uses the aut
16c21 6f 2d 76 61 63 75 75 6d 0a 2a 2a 20 70 6f 69 6e o-vacuum.** poin
16c22 74 65 72 2d 6d 61 70 20 64 61 74 61 20 69 6e 73 ter-map data ins
16c23 74 65 61 64 20 6f 66 20 72 65 61 64 69 6e 67 20 tead of reading
16c24 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 70 the content of p
16c25 61 67 65 20 6f 76 66 6c 20 74 6f 20 64 6f 20 73 age ovfl to do s
16c26 6f 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 o. .**.** If an
16c27 65 72 72 6f 72 20 6f 63 63 75 72 73 20 61 6e 20 error occurs an
16c28 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64 SQLite error cod
16c29 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 4f e is returned. O
16c2a 74 68 65 72 77 69 73 65 3a 0a 2a 2a 0a 2a 2a 20 therwise:.**.**
16c2b 54 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 The page number
16c2c 6f 66 20 74 68 65 20 6e 65 78 74 20 6f 76 65 72 of the next over
16c2d 66 6c 6f 77 20 70 61 67 65 20 69 6e 20 74 68 65 flow page in the
16c2e 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 69 73 20 linked list is
16c2f 0a 2a 2a 20 77 72 69 74 74 65 6e 20 74 6f 20 2a .** written to *
16c30 70 50 67 6e 6f 4e 65 78 74 2e 20 49 66 20 70 61 pPgnoNext. If pa
16c31 67 65 20 6f 76 66 6c 20 69 73 20 74 68 65 20 6c ge ovfl is the l
16c32 61 73 74 20 70 61 67 65 20 69 6e 20 69 74 73 20 ast page in its
16c33 6c 69 6e 6b 65 64 20 0a 2a 2a 20 6c 69 73 74 2c linked .** list,
16c34 20 2a 70 50 67 6e 6f 4e 65 78 74 20 69 73 20 73 *pPgnoNext is s
16c35 65 74 20 74 6f 20 7a 65 72 6f 2e 20 0a 2a 2a 0a et to zero. .**.
16c36 2a 2a 20 49 66 20 70 70 50 61 67 65 20 69 73 20 ** If ppPage is
16c37 6e 6f 74 20 4e 55 4c 4c 2c 20 61 6e 64 20 61 20 not NULL, and a
16c38 72 65 66 65 72 65 6e 63 65 20 74 6f 20 74 68 65 reference to the
16c39 20 4d 65 6d 50 61 67 65 20 6f 62 6a 65 63 74 20 MemPage object
16c3a 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a corresponding.**
16c3b 20 74 6f 20 70 61 67 65 20 6e 75 6d 62 65 72 20 to page number
16c3c 70 4f 76 66 6c 20 77 61 73 20 6f 62 74 61 69 6e pOvfl was obtain
16c3d 65 64 2c 20 74 68 65 6e 20 2a 70 70 50 61 67 65 ed, then *ppPage
16c3e 20 69 73 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 is set to point
16c3f 20 74 6f 20 74 68 61 74 0a 2a 2a 20 72 65 66 65 to that.** refe
16c40 72 65 6e 63 65 2e 20 49 74 20 69 73 20 74 68 65 rence. It is the
16c41 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 74 79 20 responsibility
16c42 6f 66 20 74 68 65 20 63 61 6c 6c 65 72 20 74 6f of the caller to
16c43 20 63 61 6c 6c 20 72 65 6c 65 61 73 65 50 61 67 call releasePag
16c44 65 28 29 0a 2a 2a 20 6f 6e 20 2a 70 70 50 61 67 e().** on *ppPag
16c45 65 20 74 6f 20 66 72 65 65 20 74 68 65 20 72 65 e to free the re
16c46 66 65 72 65 6e 63 65 2e 20 49 6e 20 6e 6f 20 72 ference. In no r
16c47 65 66 65 72 65 6e 63 65 20 77 61 73 20 6f 62 74 eference was obt
16c48 61 69 6e 65 64 20 28 62 65 63 61 75 73 65 0a 2a ained (because.*
16c49 2a 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 * the pointer-ma
16c4a 70 20 77 61 73 20 75 73 65 64 20 74 6f 20 6f 62 p was used to ob
16c4b 74 61 69 6e 20 74 68 65 20 76 61 6c 75 65 20 66 tain the value f
16c4c 6f 72 20 2a 70 50 67 6e 6f 4e 65 78 74 29 2c 20 or *pPgnoNext),
16c4d 74 68 65 6e 0a 2a 2a 20 2a 70 70 50 61 67 65 20 then.** *ppPage
16c4e 69 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e 0a is set to zero..
16c4f 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 */.static int ge
16c50 74 4f 76 65 72 66 6c 6f 77 50 61 67 65 28 0a 20 tOverflowPage(.
16c51 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 BtShared *pBt,
16c52 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
16c53 20 54 68 65 20 64 61 74 61 62 61 73 65 20 66 69 The database fi
16c54 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 6f 76 66 le */. Pgno ovf
16c55 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 l,
16c56 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 /* Current
16c57 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 6e 75 overflow page nu
16c58 6d 62 65 72 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 mber */. MemPag
16c59 65 20 2a 2a 70 70 50 61 67 65 2c 20 20 20 20 20 e **ppPage,
16c5a 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 4d /* OUT: M
16c5b 65 6d 50 61 67 65 20 68 61 6e 64 6c 65 20 28 6d emPage handle (m
16c5c 61 79 20 62 65 20 4e 55 4c 4c 29 20 2a 2f 0a 20 ay be NULL) */.
16c5d 20 50 67 6e 6f 20 2a 70 50 67 6e 6f 4e 65 78 74 Pgno *pPgnoNext
16c5e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
16c5f 20 4f 55 54 3a 20 4e 65 78 74 20 6f 76 65 72 66 OUT: Next overf
16c60 6c 6f 77 20 70 61 67 65 20 6e 75 6d 62 65 72 20 low page number
16c61 2a 2f 0a 29 7b 0a 20 20 50 67 6e 6f 20 6e 65 78 */.){. Pgno nex
16c62 74 20 3d 20 30 3b 0a 20 20 4d 65 6d 50 61 67 65 t = 0;. MemPage
16c63 20 2a 70 50 61 67 65 20 3d 20 30 3b 0a 20 20 69 *pPage = 0;. i
16c64 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
16c65 4b 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 K;.. assert( sq
16c66 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
16c67 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a (pBt->mutex) );.
16c68 20 20 61 73 73 65 72 74 28 70 50 67 6e 6f 4e 65 assert(pPgnoNe
16c69 78 74 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 xt);..#ifndef SQ
16c6a 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 LITE_OMIT_AUTOVA
16c6b 43 55 55 4d 0a 20 20 2f 2a 20 54 72 79 20 74 6f CUUM. /* Try to
16c6c 20 66 69 6e 64 20 74 68 65 20 6e 65 78 74 20 70 find the next p
16c6d 61 67 65 20 69 6e 20 74 68 65 20 6f 76 65 72 66 age in the overf
16c6e 6c 6f 77 20 6c 69 73 74 20 75 73 69 6e 67 20 74 low list using t
16c6f 68 65 0a 20 20 2a 2a 20 61 75 74 6f 76 61 63 75 he. ** autovacu
16c70 75 6d 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 um pointer-map p
16c71 61 67 65 73 2e 20 47 75 65 73 73 20 74 68 61 74 ages. Guess that
16c72 20 74 68 65 20 6e 65 78 74 20 70 61 67 65 20 69 the next page i
16c73 6e 20 0a 20 20 2a 2a 20 74 68 65 20 6f 76 65 72 n . ** the over
16c74 66 6c 6f 77 20 6c 69 73 74 20 69 73 20 70 61 67 flow list is pag
16c75 65 20 6e 75 6d 62 65 72 20 28 6f 76 66 6c 2b 31 e number (ovfl+1
16c76 29 2e 20 49 66 20 74 68 61 74 20 67 75 65 73 73 ). If that guess
16c77 20 74 75 72 6e 73 20 0a 20 20 2a 2a 20 6f 75 74 turns . ** out
16c78 20 74 6f 20 62 65 20 77 72 6f 6e 67 2c 20 66 61 to be wrong, fa
16c79 6c 6c 20 62 61 63 6b 20 74 6f 20 6c 6f 61 64 69 ll back to loadi
16c7a 6e 67 20 74 68 65 20 64 61 74 61 20 6f 66 20 70 ng the data of p
16c7b 61 67 65 20 0a 20 20 2a 2a 20 6e 75 6d 62 65 72 age . ** number
16c7c 20 6f 76 66 6c 20 74 6f 20 64 65 74 65 72 6d 69 ovfl to determi
16c7d 6e 65 20 74 68 65 20 6e 65 78 74 20 70 61 67 65 ne the next page
16c7e 20 6e 75 6d 62 65 72 2e 0a 20 20 2a 2f 0a 20 20 number.. */.
16c7f 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 if( pBt->autoVac
16c80 75 75 6d 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20 uum ){. Pgno
16c81 70 67 6e 6f 3b 0a 20 20 20 20 50 67 6e 6f 20 69 pgno;. Pgno i
16c82 47 75 65 73 73 20 3d 20 6f 76 66 6c 2b 31 3b 0a Guess = ovfl+1;.
16c83 20 20 20 20 75 38 20 65 54 79 70 65 3b 0a 0a 20 u8 eType;..
16c84 20 20 20 77 68 69 6c 65 28 20 50 54 52 4d 41 50 while( PTRMAP
16c85 5f 49 53 50 41 47 45 28 70 42 74 2c 20 69 47 75 _ISPAGE(pBt, iGu
16c86 65 73 73 29 20 7c 7c 20 69 47 75 65 73 73 3d 3d ess) || iGuess==
16c87 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 PENDING_BYTE_PAG
16c88 45 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 E(pBt) ){.
16c89 69 47 75 65 73 73 2b 2b 3b 0a 20 20 20 20 7d 0a iGuess++;. }.
16c8a 0a 20 20 20 20 69 66 28 20 69 47 75 65 73 73 3c . if( iGuess<
16c8b 3d 70 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 =pagerPagecount(
16c8c 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 72 63 pBt) ){. rc
16c8d 20 3d 20 70 74 72 6d 61 70 47 65 74 28 70 42 74 = ptrmapGet(pBt
16c8e 2c 20 69 47 75 65 73 73 2c 20 26 65 54 79 70 65 , iGuess, &eType
16c8f 2c 20 26 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 , &pgno);.
16c90 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
16c91 4b 20 26 26 20 65 54 79 70 65 3d 3d 50 54 52 4d K && eType==PTRM
16c92 41 50 5f 4f 56 45 52 46 4c 4f 57 32 20 26 26 20 AP_OVERFLOW2 &&
16c93 70 67 6e 6f 3d 3d 6f 76 66 6c 20 29 7b 0a 20 20 pgno==ovfl ){.
16c94 20 20 20 20 20 20 6e 65 78 74 20 3d 20 69 47 75 next = iGu
16c95 65 73 73 3b 0a 20 20 20 20 20 20 20 20 72 63 20 ess;. rc
16c96 3d 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 = SQLITE_DONE;.
16c97 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d }. }. }
16c98 0a 23 65 6e 64 69 66 0a 0a 20 20 61 73 73 65 72 .#endif.. asser
16c99 74 28 20 6e 65 78 74 3d 3d 30 20 7c 7c 20 72 63 t( next==0 || rc
16c9a 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 3b ==SQLITE_DONE );
16c9b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
16c9c 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d E_OK ){. rc =
16c9d 20 62 74 72 65 65 47 65 74 50 61 67 65 28 70 42 btreeGetPage(pB
16c9e 74 2c 20 6f 76 66 6c 2c 20 26 70 50 61 67 65 2c t, ovfl, &pPage,
16c9f 20 30 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 0);. assert(
16ca0 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c rc==SQLITE_OK |
16ca1 7c 20 70 50 61 67 65 3d 3d 30 20 29 3b 0a 20 20 | pPage==0 );.
16ca2 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
16ca3 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 6e 65 78 _OK ){. nex
16ca4 74 20 3d 20 67 65 74 34 62 79 74 65 28 70 50 61 t = get4byte(pPa
16ca5 67 65 2d 3e 61 44 61 74 61 29 3b 0a 20 20 20 20 ge->aData);.
16ca6 7d 0a 20 20 7d 0a 0a 20 20 2a 70 50 67 6e 6f 4e }. }.. *pPgnoN
16ca7 65 78 74 20 3d 20 6e 65 78 74 3b 0a 20 20 69 66 ext = next;. if
16ca8 28 20 70 70 50 61 67 65 20 29 7b 0a 20 20 20 20 ( ppPage ){.
16ca9 2a 70 70 50 61 67 65 20 3d 20 70 50 61 67 65 3b *ppPage = pPage;
16caa 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 . }else{. re
16cab 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65 29 leasePage(pPage)
16cac 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 ;. }. return (
16cad 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 rc==SQLITE_DONE
16cae 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 72 63 ? SQLITE_OK : rc
16caf 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 70 79 );.}../*.** Copy
16cb0 20 64 61 74 61 20 66 72 6f 6d 20 61 20 62 75 66 data from a buf
16cb1 66 65 72 20 74 6f 20 61 20 70 61 67 65 2c 20 6f fer to a page, o
16cb2 72 20 66 72 6f 6d 20 61 20 70 61 67 65 20 74 6f r from a page to
16cb3 20 61 20 62 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a a buffer..**.**
16cb4 20 70 50 61 79 6c 6f 61 64 20 69 73 20 61 20 70 pPayload is a p
16cb5 6f 69 6e 74 65 72 20 74 6f 20 64 61 74 61 20 73 ointer to data s
16cb6 74 6f 72 65 64 20 6f 6e 20 64 61 74 61 62 61 73 tored on databas
16cb7 65 20 70 61 67 65 20 70 44 62 50 61 67 65 2e 0a e page pDbPage..
16cb8 2a 2a 20 49 66 20 61 72 67 75 6d 65 6e 74 20 65 ** If argument e
16cb9 4f 70 20 69 73 20 66 61 6c 73 65 2c 20 74 68 65 Op is false, the
16cba 6e 20 6e 42 79 74 65 20 62 79 74 65 73 20 6f 66 n nByte bytes of
16cbb 20 64 61 74 61 20 61 72 65 20 63 6f 70 69 65 64 data are copied
16cbc 0a 2a 2a 20 66 72 6f 6d 20 70 50 61 79 6c 6f 61 .** from pPayloa
16cbd 64 20 74 6f 20 74 68 65 20 62 75 66 66 65 72 20 d to the buffer
16cbe 70 6f 69 6e 74 65 64 20 61 74 20 62 79 20 70 42 pointed at by pB
16cbf 75 66 2e 20 49 66 20 65 4f 70 20 69 73 20 74 72 uf. If eOp is tr
16cc0 75 65 2c 0a 2a 2a 20 74 68 65 6e 20 73 71 6c 69 ue,.** then sqli
16cc1 74 65 33 50 61 67 65 72 57 72 69 74 65 28 29 20 te3PagerWrite()
16cc2 69 73 20 63 61 6c 6c 65 64 20 6f 6e 20 70 44 62 is called on pDb
16cc3 50 61 67 65 20 61 6e 64 20 6e 42 79 74 65 20 62 Page and nByte b
16cc4 79 74 65 73 0a 2a 2a 20 6f 66 20 64 61 74 61 20 ytes.** of data
16cc5 61 72 65 20 63 6f 70 69 65 64 20 66 72 6f 6d 20 are copied from
16cc6 74 68 65 20 62 75 66 66 65 72 20 70 42 75 66 20 the buffer pBuf
16cc7 74 6f 20 70 50 61 79 6c 6f 61 64 2e 0a 2a 2a 0a to pPayload..**.
16cc8 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 ** SQLITE_OK is
16cc9 72 65 74 75 72 6e 65 64 20 6f 6e 20 73 75 63 63 returned on succ
16cca 65 73 73 2c 20 6f 74 68 65 72 77 69 73 65 20 61 ess, otherwise a
16ccb 6e 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2f n error code..*/
16ccc 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f 70 79 .static int copy
16ccd 50 61 79 6c 6f 61 64 28 0a 20 20 76 6f 69 64 20 Payload(. void
16cce 2a 70 50 61 79 6c 6f 61 64 2c 20 20 20 20 20 20 *pPayload,
16ccf 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 /* Pointer
16cd0 74 6f 20 70 61 67 65 20 64 61 74 61 20 2a 2f 0a to page data */.
16cd1 20 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 20 20 void *pBuf,
16cd2 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 /* P
16cd3 6f 69 6e 74 65 72 20 74 6f 20 62 75 66 66 65 72 ointer to buffer
16cd4 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 2c */. int nByte,
16cd5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16cd6 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 /* Number of byt
16cd7 65 73 20 74 6f 20 63 6f 70 79 20 2a 2f 0a 20 20 es to copy */.
16cd8 69 6e 74 20 65 4f 70 2c 20 20 20 20 20 20 20 20 int eOp,
16cd9 20 20 20 20 20 20 20 20 20 20 2f 2a 20 30 20 2d /* 0 -
16cda 3e 20 63 6f 70 79 20 66 72 6f 6d 20 70 61 67 65 > copy from page
16cdb 2c 20 31 20 2d 3e 20 63 6f 70 79 20 74 6f 20 70 , 1 -> copy to p
16cdc 61 67 65 20 2a 2f 0a 20 20 44 62 50 61 67 65 20 age */. DbPage
16cdd 2a 70 44 62 50 61 67 65 20 20 20 20 20 20 20 20 *pDbPage
16cde 20 20 20 2f 2a 20 50 61 67 65 20 63 6f 6e 74 61 /* Page conta
16cdf 69 6e 69 6e 67 20 70 50 61 79 6c 6f 61 64 20 2a ining pPayload *
16ce0 2f 0a 29 7b 0a 20 20 69 66 28 20 65 4f 70 20 29 /.){. if( eOp )
16ce1 7b 0a 20 20 20 20 2f 2a 20 43 6f 70 79 20 64 61 {. /* Copy da
16ce2 74 61 20 66 72 6f 6d 20 62 75 66 66 65 72 20 74 ta from buffer t
16ce3 6f 20 70 61 67 65 20 28 61 20 77 72 69 74 65 20 o page (a write
16ce4 6f 70 65 72 61 74 69 6f 6e 29 20 2a 2f 0a 20 20 operation) */.
16ce5 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 int rc = sqlit
16ce6 65 33 50 61 67 65 72 57 72 69 74 65 28 70 44 62 e3PagerWrite(pDb
16ce7 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 Page);. if( r
16ce8 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
16ce9 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b return rc;
16cea 0a 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 63 70 . }. memcp
16ceb 79 28 70 50 61 79 6c 6f 61 64 2c 20 70 42 75 66 y(pPayload, pBuf
16cec 2c 20 6e 42 79 74 65 29 3b 0a 20 20 7d 65 6c 73 , nByte);. }els
16ced 65 7b 0a 20 20 20 20 2f 2a 20 43 6f 70 79 20 64 e{. /* Copy d
16cee 61 74 61 20 66 72 6f 6d 20 70 61 67 65 20 74 6f ata from page to
16cef 20 62 75 66 66 65 72 20 28 61 20 72 65 61 64 20 buffer (a read
16cf0 6f 70 65 72 61 74 69 6f 6e 29 20 2a 2f 0a 20 20 operation) */.
16cf1 20 20 6d 65 6d 63 70 79 28 70 42 75 66 2c 20 70 memcpy(pBuf, p
16cf2 50 61 79 6c 6f 61 64 2c 20 6e 42 79 74 65 29 3b Payload, nByte);
16cf3 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 . }. return SQ
16cf4 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a LITE_OK;.}../*.*
16cf5 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
16cf6 69 73 20 75 73 65 64 20 74 6f 20 72 65 61 64 20 is used to read
16cf7 6f 72 20 6f 76 65 72 77 72 69 74 65 20 70 61 79 or overwrite pay
16cf8 6c 6f 61 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e load information
16cf9 0a 2a 2a 20 66 6f 72 20 74 68 65 20 65 6e 74 72 .** for the entr
16cfa 79 20 74 68 61 74 20 74 68 65 20 70 43 75 72 20 y that the pCur
16cfb 63 75 72 73 6f 72 20 69 73 20 70 6f 69 6e 74 69 cursor is pointi
16cfc 6e 67 20 74 6f 2e 20 49 66 20 74 68 65 20 65 4f ng to. If the eO
16cfd 70 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 69 p.** parameter i
16cfe 73 20 30 2c 20 74 68 69 73 20 69 73 20 61 20 72 s 0, this is a r
16cff 65 61 64 20 6f 70 65 72 61 74 69 6f 6e 20 28 64 ead operation (d
16d00 61 74 61 20 63 6f 70 69 65 64 20 69 6e 74 6f 0a ata copied into.
16d01 2a 2a 20 62 75 66 66 65 72 20 70 42 75 66 29 2e ** buffer pBuf).
16d02 20 49 66 20 69 74 20 69 73 20 6e 6f 6e 2d 7a 65 If it is non-ze
16d03 72 6f 2c 20 61 20 77 72 69 74 65 20 28 64 61 74 ro, a write (dat
16d04 61 20 63 6f 70 69 65 64 20 66 72 6f 6d 0a 2a 2a a copied from.**
16d05 20 62 75 66 66 65 72 20 70 42 75 66 29 2e 0a 2a buffer pBuf)..*
16d06 2a 0a 2a 2a 20 41 20 74 6f 74 61 6c 20 6f 66 20 *.** A total of
16d07 22 61 6d 74 22 20 62 79 74 65 73 20 61 72 65 20 "amt" bytes are
16d08 72 65 61 64 20 6f 72 20 77 72 69 74 74 65 6e 20 read or written
16d09 62 65 67 69 6e 6e 69 6e 67 20 61 74 20 22 6f 66 beginning at "of
16d0a 66 73 65 74 22 2e 0a 2a 2a 20 44 61 74 61 20 69 fset"..** Data i
16d0b 73 20 72 65 61 64 20 74 6f 20 6f 72 20 66 72 6f s read to or fro
16d0c 6d 20 74 68 65 20 62 75 66 66 65 72 20 70 42 75 m the buffer pBu
16d0d 66 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e f..**.** The con
16d0e 74 65 6e 74 20 62 65 69 6e 67 20 72 65 61 64 20 tent being read
16d0f 6f 72 20 77 72 69 74 74 65 6e 20 6d 69 67 68 74 or written might
16d10 20 61 70 70 65 61 72 20 6f 6e 20 74 68 65 20 6d appear on the m
16d11 61 69 6e 20 70 61 67 65 0a 2a 2a 20 6f 72 20 62 ain page.** or b
16d12 65 20 73 63 61 74 74 65 72 65 64 20 6f 75 74 20 e scattered out
16d13 6f 6e 20 6d 75 6c 74 69 70 6c 65 20 6f 76 65 72 on multiple over
16d14 66 6c 6f 77 20 70 61 67 65 73 2e 0a 2a 2a 0a 2a flow pages..**.*
16d15 2a 20 49 66 20 74 68 65 20 42 74 43 75 72 73 6f * If the BtCurso
16d16 72 2e 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64 r.isIncrblobHand
16d17 6c 65 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 le flag is set,
16d18 61 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74 0a and the current.
16d19 2a 2a 20 63 75 72 73 6f 72 20 65 6e 74 72 79 20 ** cursor entry
16d1a 75 73 65 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 uses one or more
16d1b 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2c overflow pages,
16d1c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 2a this function.*
16d1d 2a 20 61 6c 6c 6f 63 61 74 65 73 20 73 70 61 63 * allocates spac
16d1e 65 20 66 6f 72 20 61 6e 64 20 6c 61 7a 69 6c 79 e for and lazily
16d1f 20 70 6f 70 6c 75 61 74 65 73 20 74 68 65 20 6f popluates the o
16d20 76 65 72 66 6c 6f 77 20 70 61 67 65 2d 6c 69 73 verflow page-lis
16d21 74 20 0a 2a 2a 20 63 61 63 68 65 20 61 72 72 61 t .** cache arra
16d22 79 20 28 42 74 43 75 72 73 6f 72 2e 61 4f 76 65 y (BtCursor.aOve
16d23 72 66 6c 6f 77 29 2e 20 53 75 62 73 65 71 75 65 rflow). Subseque
16d24 6e 74 20 63 61 6c 6c 73 20 75 73 65 20 74 68 69 nt calls use thi
16d25 73 0a 2a 2a 20 63 61 63 68 65 20 74 6f 20 6d 61 s.** cache to ma
16d26 6b 65 20 73 65 65 6b 69 6e 67 20 74 6f 20 74 68 ke seeking to th
16d27 65 20 73 75 70 70 6c 69 65 64 20 6f 66 66 73 65 e supplied offse
16d28 74 20 6d 6f 72 65 20 65 66 66 69 63 69 65 6e 74 t more efficient
16d29 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 63 65 20 61 6e 20 ..**.** Once an
16d2a 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2d 6c 69 overflow page-li
16d2b 73 74 20 63 61 63 68 65 20 68 61 73 20 62 65 65 st cache has bee
16d2c 6e 20 61 6c 6c 6f 63 61 74 65 64 2c 20 69 74 20 n allocated, it
16d2d 6d 61 79 20 62 65 0a 2a 2a 20 69 6e 76 61 6c 69 may be.** invali
16d2e 64 61 74 65 64 20 69 66 20 73 6f 6d 65 20 6f 74 dated if some ot
16d2f 68 65 72 20 63 75 72 73 6f 72 20 77 72 69 74 65 her cursor write
16d30 73 20 74 6f 20 74 68 65 20 73 61 6d 65 20 74 61 s to the same ta
16d31 62 6c 65 2c 20 6f 72 20 69 66 0a 2a 2a 20 74 68 ble, or if.** th
16d32 65 20 63 75 72 73 6f 72 20 69 73 20 6d 6f 76 65 e cursor is move
16d33 64 20 74 6f 20 61 20 64 69 66 66 65 72 65 6e 74 d to a different
16d34 20 72 6f 77 2e 20 41 64 64 69 74 69 6f 6e 61 6c row. Additional
16d35 6c 79 2c 20 69 6e 20 61 75 74 6f 2d 76 61 63 75 ly, in auto-vacu
16d36 75 6d 0a 2a 2a 20 6d 6f 64 65 2c 20 74 68 65 20 um.** mode, the
16d37 66 6f 6c 6c 6f 77 69 6e 67 20 65 76 65 6e 74 73 following events
16d38 20 6d 61 79 20 69 6e 76 61 6c 69 64 61 74 65 20 may invalidate
16d39 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 an overflow page
16d3a 2d 6c 69 73 74 20 63 61 63 68 65 2e 0a 2a 2a 0a -list cache..**.
16d3b 2a 2a 20 20 20 2a 20 41 6e 20 69 6e 63 72 65 6d ** * An increm
16d3c 65 6e 74 61 6c 20 76 61 63 75 75 6d 2c 0a 2a 2a ental vacuum,.**
16d3d 20 20 20 2a 20 41 20 63 6f 6d 6d 69 74 20 69 6e * A commit in
16d3e 20 61 75 74 6f 5f 76 61 63 75 75 6d 3d 22 66 75 auto_vacuum="fu
16d3f 6c 6c 22 20 6d 6f 64 65 2c 0a 2a 2a 20 20 20 2a ll" mode,.** *
16d40 20 43 72 65 61 74 69 6e 67 20 61 20 74 61 62 6c Creating a tabl
16d41 65 20 28 6d 61 79 20 72 65 71 75 69 72 65 20 6d e (may require m
16d42 6f 76 69 6e 67 20 61 6e 20 6f 76 65 72 66 6c 6f oving an overflo
16d43 77 20 70 61 67 65 29 2e 0a 2a 2f 0a 73 74 61 74 w page)..*/.stat
16d44 69 63 20 69 6e 74 20 61 63 63 65 73 73 50 61 79 ic int accessPay
16d45 6c 6f 61 64 28 0a 20 20 42 74 43 75 72 73 6f 72 load(. BtCursor
16d46 20 2a 70 43 75 72 2c 20 20 20 20 20 20 2f 2a 20 *pCur, /*
16d47 43 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 Cursor pointing
16d48 74 6f 20 65 6e 74 72 79 20 74 6f 20 72 65 61 64 to entry to read
16d49 20 66 72 6f 6d 20 2a 2f 0a 20 20 75 33 32 20 6f from */. u32 o
16d4a 66 66 73 65 74 2c 20 20 20 20 20 20 20 20 20 20 ffset,
16d4b 2f 2a 20 42 65 67 69 6e 20 72 65 61 64 69 6e 67 /* Begin reading
16d4c 20 74 68 69 73 20 66 61 72 20 69 6e 74 6f 20 70 this far into p
16d4d 61 79 6c 6f 61 64 20 2a 2f 0a 20 20 75 33 32 20 ayload */. u32
16d4e 61 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 amt,
16d4f 20 2f 2a 20 52 65 61 64 20 74 68 69 73 20 6d 61 /* Read this ma
16d50 6e 79 20 62 79 74 65 73 20 2a 2f 0a 20 20 75 6e ny bytes */. un
16d51 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 42 75 signed char *pBu
16d52 66 2c 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 f, /* Write the
16d53 62 79 74 65 73 20 69 6e 74 6f 20 74 68 69 73 20 bytes into this
16d54 62 75 66 66 65 72 20 2a 2f 20 0a 20 20 69 6e 74 buffer */ . int
16d55 20 65 4f 70 20 20 20 20 20 20 20 20 20 20 20 20 eOp
16d56 20 20 2f 2a 20 7a 65 72 6f 20 74 6f 20 72 65 61 /* zero to rea
16d57 64 2e 20 6e 6f 6e 2d 7a 65 72 6f 20 74 6f 20 77 d. non-zero to w
16d58 72 69 74 65 2e 20 2a 2f 0a 29 7b 0a 20 20 75 6e rite. */.){. un
16d59 73 69 67 6e 65 64 20 63 68 61 72 20 2a 61 50 61 signed char *aPa
16d5a 79 6c 6f 61 64 3b 0a 20 20 69 6e 74 20 72 63 20 yload;. int rc
16d5b 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 = SQLITE_OK;. u
16d5c 33 32 20 6e 4b 65 79 3b 0a 20 20 69 6e 74 20 69 32 nKey;. int i
16d5d 49 64 78 20 3d 20 30 3b 0a 20 20 4d 65 6d 50 61 Idx = 0;. MemPa
16d5e 67 65 20 2a 70 50 61 67 65 20 3d 20 70 43 75 72 ge *pPage = pCur
16d5f 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 ->apPage[pCur->i
16d60 50 61 67 65 5d 3b 20 2f 2a 20 42 74 72 65 65 20 Page]; /* Btree
16d61 70 61 67 65 20 6f 66 20 63 75 72 72 65 6e 74 20 page of current
16d62 65 6e 74 72 79 20 2a 2f 0a 20 20 42 74 53 68 61 entry */. BtSha
16d63 72 65 64 20 2a 70 42 74 20 3d 20 70 43 75 72 2d red *pBt = pCur-
16d64 3e 70 42 74 3b 20 20 20 20 20 20 20 20 20 20 20 >pBt;
16d65 20 20 20 20 20 20 20 2f 2a 20 42 74 72 65 65 20 /* Btree
16d66 74 68 69 73 20 63 75 72 73 6f 72 20 62 65 6c 6f this cursor belo
16d67 6e 67 73 20 74 6f 20 2a 2f 0a 0a 20 20 61 73 73 ngs to */.. ass
16d68 65 72 74 28 20 70 50 61 67 65 20 29 3b 0a 20 20 ert( pPage );.
16d69 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 assert( pCur->eS
16d6a 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c tate==CURSOR_VAL
16d6b 49 44 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 ID );. assert(
16d6c 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 pCur->aiIdx[pCur
16d6d 2d 3e 69 50 61 67 65 5d 3c 70 50 61 67 65 2d 3e ->iPage]<pPage->
16d6e 6e 43 65 6c 6c 20 29 3b 0a 20 20 61 73 73 65 72 nCell );. asser
16d6f 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 t( cursorHoldsMu
16d70 74 65 78 28 70 43 75 72 29 20 29 3b 0a 0a 20 20 tex(pCur) );..
16d71 67 65 74 43 65 6c 6c 49 6e 66 6f 28 70 43 75 72 getCellInfo(pCur
16d72 29 3b 0a 20 20 61 50 61 79 6c 6f 61 64 20 3d 20 );. aPayload =
16d73 70 43 75 72 2d 3e 69 6e 66 6f 2e 70 43 65 6c 6c pCur->info.pCell
16d74 20 2b 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 48 + pCur->info.nH
16d75 65 61 64 65 72 3b 0a 20 20 6e 4b 65 79 20 3d 20 eader;. nKey =
16d76 28 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 3f (pPage->intKey ?
16d77 20 30 20 3a 20 28 69 6e 74 29 70 43 75 72 2d 3e 0 : (int)pCur->
16d78 69 6e 66 6f 2e 6e 4b 65 79 29 3b 0a 0a 20 20 69 info.nKey);.. i
16d79 66 28 20 4e 45 56 45 52 28 6f 66 66 73 65 74 2b f( NEVER(offset+
16d7a 61 6d 74 20 3e 20 6e 4b 65 79 2b 70 43 75 72 2d amt > nKey+pCur-
16d7b 3e 69 6e 66 6f 2e 6e 44 61 74 61 29 20 0a 20 20 >info.nData) .
16d7c 20 7c 7c 20 26 61 50 61 79 6c 6f 61 64 5b 70 43 || &aPayload[pC
16d7d 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 5d ur->info.nLocal]
16d7e 20 3e 20 26 70 50 61 67 65 2d 3e 61 44 61 74 61 > &pPage->aData
16d7f 5b 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 [pBt->usableSize
16d80 5d 0a 20 20 29 7b 0a 20 20 20 20 2f 2a 20 54 72 ]. ){. /* Tr
16d81 79 69 6e 67 20 74 6f 20 72 65 61 64 20 6f 72 20 ying to read or
16d82 77 72 69 74 65 20 70 61 73 74 20 74 68 65 20 65 write past the e
16d83 6e 64 20 6f 66 20 74 68 65 20 64 61 74 61 20 69 nd of the data i
16d84 73 20 61 6e 20 65 72 72 6f 72 20 2a 2f 0a 20 20 s an error */.
16d85 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
16d86 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 CORRUPT_BKPT;.
16d87 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 }.. /* Check if
16d88 20 64 61 74 61 20 6d 75 73 74 20 62 65 20 72 65 data must be re
16d89 61 64 2f 77 72 69 74 74 65 6e 20 74 6f 2f 66 72 ad/written to/fr
16d8a 6f 6d 20 74 68 65 20 62 74 72 65 65 20 70 61 67 om the btree pag
16d8b 65 20 69 74 73 65 6c 66 2e 20 2a 2f 0a 20 20 69 e itself. */. i
16d8c 66 28 20 6f 66 66 73 65 74 3c 70 43 75 72 2d 3e f( offset<pCur->
16d8d 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 29 7b 0a 20 info.nLocal ){.
16d8e 20 20 20 69 6e 74 20 61 20 3d 20 61 6d 74 3b 0a int a = amt;.
16d8f 20 20 20 20 69 66 28 20 61 2b 6f 66 66 73 65 74 if( a+offset
16d90 3e 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 >pCur->info.nLoc
16d91 61 6c 20 29 7b 0a 20 20 20 20 20 20 61 20 3d 20 al ){. a =
16d92 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 pCur->info.nLoca
16d93 6c 20 2d 20 6f 66 66 73 65 74 3b 0a 20 20 20 20 l - offset;.
16d94 7d 0a 20 20 20 20 72 63 20 3d 20 63 6f 70 79 50 }. rc = copyP
16d95 61 79 6c 6f 61 64 28 26 61 50 61 79 6c 6f 61 64 ayload(&aPayload
16d96 5b 6f 66 66 73 65 74 5d 2c 20 70 42 75 66 2c 20 [offset], pBuf,
16d97 61 2c 20 65 4f 70 2c 20 70 50 61 67 65 2d 3e 70 a, eOp, pPage->p
16d98 44 62 50 61 67 65 29 3b 0a 20 20 20 20 6f 66 66 DbPage);. off
16d99 73 65 74 20 3d 20 30 3b 0a 20 20 20 20 70 42 75 set = 0;. pBu
16d9a 66 20 2b 3d 20 61 3b 0a 20 20 20 20 61 6d 74 20 f += a;. amt
16d9b 2d 3d 20 61 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 -= a;. }else{.
16d9c 20 20 20 6f 66 66 73 65 74 20 2d 3d 20 70 43 75 offset -= pCu
16d9d 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 3b 0a r->info.nLocal;.
16d9e 20 20 7d 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 }.. if( rc==S
16d9f 51 4c 49 54 45 5f 4f 4b 20 26 26 20 61 6d 74 3e QLITE_OK && amt>
16da0 30 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 75 0 ){. const u
16da1 33 32 20 6f 76 66 6c 53 69 7a 65 20 3d 20 70 42 32 ovflSize = pB
16da2 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 t->usableSize -
16da3 34 3b 20 20 2f 2a 20 42 79 74 65 73 20 63 6f 6e 4; /* Bytes con
16da4 74 65 6e 74 20 70 65 72 20 6f 76 66 6c 20 70 61 tent per ovfl pa
16da5 67 65 20 2a 2f 0a 20 20 20 20 50 67 6e 6f 20 6e ge */. Pgno n
16da6 65 78 74 50 61 67 65 3b 0a 0a 20 20 20 20 6e 65 extPage;.. ne
16da7 78 74 50 61 67 65 20 3d 20 67 65 74 34 62 79 74 xtPage = get4byt
16da8 65 28 26 61 50 61 79 6c 6f 61 64 5b 70 43 75 72 e(&aPayload[pCur
16da9 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 5d 29 3b ->info.nLocal]);
16daa 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
16dab 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 _OMIT_INCRBLOB.
16dac 20 20 20 2f 2a 20 49 66 20 74 68 65 20 69 73 49 /* If the isI
16dad 6e 63 72 62 6c 6f 62 48 61 6e 64 6c 65 20 66 6c ncrblobHandle fl
16dae 61 67 20 69 73 20 73 65 74 20 61 6e 64 20 74 68 ag is set and th
16daf 65 20 42 74 43 75 72 73 6f 72 2e 61 4f 76 65 72 e BtCursor.aOver
16db0 66 6c 6f 77 5b 5d 0a 20 20 20 20 2a 2a 20 68 61 flow[]. ** ha
16db1 73 20 6e 6f 74 20 62 65 65 6e 20 61 6c 6c 6f 63 s not been alloc
16db2 61 74 65 64 2c 20 61 6c 6c 6f 63 61 74 65 20 69 ated, allocate i
16db3 74 20 6e 6f 77 2e 20 54 68 65 20 61 72 72 61 79 t now. The array
16db4 20 69 73 20 73 69 7a 65 64 20 61 74 0a 20 20 20 is sized at.
16db5 20 2a 2a 20 6f 6e 65 20 65 6e 74 72 79 20 66 6f ** one entry fo
16db6 72 20 65 61 63 68 20 6f 76 65 72 66 6c 6f 77 20 r each overflow
16db7 70 61 67 65 20 69 6e 20 74 68 65 20 6f 76 65 72 page in the over
16db8 66 6c 6f 77 20 63 68 61 69 6e 2e 20 54 68 65 0a flow chain. The.
16db9 20 20 20 20 2a 2a 20 70 61 67 65 20 6e 75 6d 62 ** page numb
16dba 65 72 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 er of the first
16dbb 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 69 73 overflow page is
16dbc 20 73 74 6f 72 65 64 20 69 6e 20 61 4f 76 65 72 stored in aOver
16dbd 66 6c 6f 77 5b 30 5d 2c 0a 20 20 20 20 2a 2a 20 flow[0],. **
16dbe 65 74 63 2e 20 41 20 76 61 6c 75 65 20 6f 66 20 etc. A value of
16dbf 30 20 69 6e 20 74 68 65 20 61 4f 76 65 72 66 6c 0 in the aOverfl
16dc0 6f 77 5b 5d 20 61 72 72 61 79 20 6d 65 61 6e 73 ow[] array means
16dc1 20 22 6e 6f 74 20 79 65 74 20 6b 6e 6f 77 6e 22 "not yet known"
16dc2 0a 20 20 20 20 2a 2a 20 28 74 68 65 20 63 61 63 . ** (the cac
16dc3 68 65 20 69 73 20 6c 61 7a 69 6c 79 20 70 6f 70 he is lazily pop
16dc4 75 6c 61 74 65 64 29 2e 0a 20 20 20 20 2a 2f 0a ulated).. */.
16dc5 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e 69 73 if( pCur->is
16dc6 49 6e 63 72 62 6c 6f 62 48 61 6e 64 6c 65 20 26 IncrblobHandle &
16dc7 26 20 21 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c & !pCur->aOverfl
16dc8 6f 77 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 ow ){. int
16dc9 6e 4f 76 66 6c 20 3d 20 28 70 43 75 72 2d 3e 69 nOvfl = (pCur->i
16dca 6e 66 6f 2e 6e 50 61 79 6c 6f 61 64 2d 70 43 75 nfo.nPayload-pCu
16dcb 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 2b 6f r->info.nLocal+o
16dcc 76 66 6c 53 69 7a 65 2d 31 29 2f 6f 76 66 6c 53 vflSize-1)/ovflS
16dcd 69 7a 65 3b 0a 20 20 20 20 20 20 70 43 75 72 2d ize;. pCur-
16dce 3e 61 4f 76 65 72 66 6c 6f 77 20 3d 20 28 50 67 >aOverflow = (Pg
16dcf 6e 6f 20 2a 29 73 71 6c 69 74 65 33 4d 61 6c 6c no *)sqlite3Mall
16dd0 6f 63 5a 65 72 6f 28 73 69 7a 65 6f 66 28 50 67 ocZero(sizeof(Pg
16dd1 6e 6f 29 2a 6e 4f 76 66 6c 29 3b 0a 20 20 20 20 no)*nOvfl);.
16dd2 20 20 2f 2a 20 6e 4f 76 66 6c 20 69 73 20 61 6c /* nOvfl is al
16dd3 77 61 79 73 20 70 6f 73 69 74 69 76 65 2e 20 20 ways positive.
16dd4 49 66 20 69 74 20 77 65 72 65 20 7a 65 72 6f 2c If it were zero,
16dd5 20 66 65 74 63 68 50 61 79 6c 6f 61 64 20 77 6f fetchPayload wo
16dd6 75 6c 64 20 68 61 76 65 0a 20 20 20 20 20 20 2a uld have. *
16dd7 2a 20 62 65 65 6e 20 75 73 65 64 20 69 6e 73 74 * been used inst
16dd8 65 61 64 20 6f 66 20 74 68 69 73 20 72 6f 75 74 ead of this rout
16dd9 69 6e 65 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66 ine. */. if
16dda 28 20 41 4c 57 41 59 53 28 6e 4f 76 66 6c 29 20 ( ALWAYS(nOvfl)
16ddb 26 26 20 21 70 43 75 72 2d 3e 61 4f 76 65 72 66 && !pCur->aOverf
16ddc 6c 6f 77 20 29 7b 0a 20 20 20 20 20 20 20 20 72 low ){. r
16ddd 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d c = SQLITE_NOMEM
16dde 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
16ddf 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 6f . /* If the o
16de0 76 65 72 66 6c 6f 77 20 70 61 67 65 2d 6c 69 73 verflow page-lis
16de1 74 20 63 61 63 68 65 20 68 61 73 20 62 65 65 6e t cache has been
16de2 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 74 allocated and t
16de3 68 65 0a 20 20 20 20 2a 2a 20 65 6e 74 72 79 20 he. ** entry
16de4 66 6f 72 20 74 68 65 20 66 69 72 73 74 20 72 65 for the first re
16de5 71 75 69 72 65 64 20 6f 76 65 72 66 6c 6f 77 20 quired overflow
16de6 70 61 67 65 20 69 73 20 76 61 6c 69 64 2c 20 73 page is valid, s
16de7 6b 69 70 0a 20 20 20 20 2a 2a 20 64 69 72 65 63 kip. ** direc
16de8 74 6c 79 20 74 6f 20 69 74 2e 0a 20 20 20 20 2a tly to it.. *
16de9 2f 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e /. if( pCur->
16dea 61 4f 76 65 72 66 6c 6f 77 20 26 26 20 70 43 75 aOverflow && pCu
16deb 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 6f 66 66 r->aOverflow[off
16dec 73 65 74 2f 6f 76 66 6c 53 69 7a 65 5d 20 29 7b set/ovflSize] ){
16ded 0a 20 20 20 20 20 20 69 49 64 78 20 3d 20 28 6f . iIdx = (o
16dee 66 66 73 65 74 2f 6f 76 66 6c 53 69 7a 65 29 3b ffset/ovflSize);
16def 0a 20 20 20 20 20 20 6e 65 78 74 50 61 67 65 20 . nextPage
16df0 3d 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f = pCur->aOverflo
16df1 77 5b 69 49 64 78 5d 3b 0a 20 20 20 20 20 20 6f w[iIdx];. o
16df2 66 66 73 65 74 20 3d 20 28 6f 66 66 73 65 74 25 ffset = (offset%
16df3 6f 76 66 6c 53 69 7a 65 29 3b 0a 20 20 20 20 7d ovflSize);. }
16df4 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 66 6f 72 .#endif.. for
16df5 28 20 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f ( ; rc==SQLITE_O
16df6 4b 20 26 26 20 61 6d 74 3e 30 20 26 26 20 6e 65 K && amt>0 && ne
16df7 78 74 50 61 67 65 3b 20 69 49 64 78 2b 2b 29 7b xtPage; iIdx++){
16df8 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
16df9 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 _OMIT_INCRBLOB.
16dfa 20 20 20 20 20 2f 2a 20 49 66 20 72 65 71 75 69 /* If requi
16dfb 72 65 64 2c 20 70 6f 70 75 6c 61 74 65 20 74 68 red, populate th
16dfc 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2d e overflow page-
16dfd 6c 69 73 74 20 63 61 63 68 65 2e 20 2a 2f 0a 20 list cache. */.
16dfe 20 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e 61 if( pCur->a
16dff 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20 Overflow ){.
16e00 20 20 20 20 61 73 73 65 72 74 28 21 70 43 75 72 assert(!pCur
16e01 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69 49 64 78 ->aOverflow[iIdx
16e02 5d 20 7c 7c 20 70 43 75 72 2d 3e 61 4f 76 65 72 ] || pCur->aOver
16e03 66 6c 6f 77 5b 69 49 64 78 5d 3d 3d 6e 65 78 74 flow[iIdx]==next
16e04 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 70 Page);. p
16e05 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69 Cur->aOverflow[i
16e06 49 64 78 5d 20 3d 20 6e 65 78 74 50 61 67 65 3b Idx] = nextPage;
16e07 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a . }.#endif.
16e08 0a 20 20 20 20 20 20 69 66 28 20 6f 66 66 73 65 . if( offse
16e09 74 3e 3d 6f 76 66 6c 53 69 7a 65 20 29 7b 0a 20 t>=ovflSize ){.
16e0a 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6f 6e /* The on
16e0b 6c 79 20 72 65 61 73 6f 6e 20 74 6f 20 72 65 61 ly reason to rea
16e0c 64 20 74 68 69 73 20 70 61 67 65 20 69 73 20 74 d this page is t
16e0d 6f 20 6f 62 74 61 69 6e 20 74 68 65 20 70 61 67 o obtain the pag
16e0e 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 6e 75 6d e. ** num
16e0f 62 65 72 20 66 6f 72 20 74 68 65 20 6e 65 78 74 ber for the next
16e10 20 70 61 67 65 20 69 6e 20 74 68 65 20 6f 76 65 page in the ove
16e11 72 66 6c 6f 77 20 63 68 61 69 6e 2e 20 54 68 65 rflow chain. The
16e12 20 70 61 67 65 0a 20 20 20 20 20 20 20 20 2a 2a page. **
16e13 20 64 61 74 61 20 69 73 20 6e 6f 74 20 72 65 71 data is not req
16e14 75 69 72 65 64 2e 20 53 6f 20 66 69 72 73 74 20 uired. So first
16e15 74 72 79 20 74 6f 20 6c 6f 6f 6b 75 70 20 74 68 try to lookup th
16e16 65 20 6f 76 65 72 66 6c 6f 77 0a 20 20 20 20 20 e overflow.
16e17 20 20 20 2a 2a 20 70 61 67 65 2d 6c 69 73 74 20 ** page-list
16e18 63 61 63 68 65 2c 20 69 66 20 61 6e 79 2c 20 74 cache, if any, t
16e19 68 65 6e 20 66 61 6c 6c 20 62 61 63 6b 20 74 6f hen fall back to
16e1a 20 74 68 65 20 67 65 74 4f 76 65 72 66 6c 6f 77 the getOverflow
16e1b 50 61 67 65 28 29 0a 20 20 20 20 20 20 20 20 2a Page(). *
16e1c 2a 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 20 20 * function..
16e1d 20 20 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 */.#ifndef S
16e1e 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 QLITE_OMIT_INCRB
16e1f 4c 4f 42 0a 20 20 20 20 20 20 20 20 69 66 28 20 LOB. if(
16e20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 pCur->aOverflow
16e21 26 26 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c && pCur->aOverfl
16e22 6f 77 5b 69 49 64 78 2b 31 5d 20 29 7b 0a 20 20 ow[iIdx+1] ){.
16e23 20 20 20 20 20 20 20 20 6e 65 78 74 50 61 67 65 nextPage
16e24 20 3d 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c = pCur->aOverfl
16e25 6f 77 5b 69 49 64 78 2b 31 5d 3b 0a 20 20 20 20 ow[iIdx+1];.
16e26 20 20 20 20 7d 20 65 6c 73 65 20 0a 23 65 6e 64 } else .#end
16e27 69 66 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 if. rc
16e28 3d 20 67 65 74 4f 76 65 72 66 6c 6f 77 50 61 67 = getOverflowPag
16e29 65 28 70 42 74 2c 20 6e 65 78 74 50 61 67 65 2c e(pBt, nextPage,
16e2a 20 30 2c 20 26 6e 65 78 74 50 61 67 65 29 3b 0a 0, &nextPage);.
16e2b 20 20 20 20 20 20 20 20 6f 66 66 73 65 74 20 2d offset -
16e2c 3d 20 6f 76 66 6c 53 69 7a 65 3b 0a 20 20 20 20 = ovflSize;.
16e2d 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
16e2e 20 2f 2a 20 4e 65 65 64 20 74 6f 20 72 65 61 64 /* Need to read
16e2f 20 74 68 69 73 20 70 61 67 65 20 70 72 6f 70 65 this page prope
16e30 72 6c 79 2e 20 49 74 20 63 6f 6e 74 61 69 6e 73 rly. It contains
16e31 20 73 6f 6d 65 20 6f 66 20 74 68 65 0a 20 20 20 some of the.
16e32 20 20 20 20 20 2a 2a 20 72 61 6e 67 65 20 6f 66 ** range of
16e33 20 64 61 74 61 20 74 68 61 74 20 69 73 20 62 65 data that is be
16e34 69 6e 67 20 72 65 61 64 20 28 65 4f 70 3d 3d 30 ing read (eOp==0
16e35 29 20 6f 72 20 77 72 69 74 74 65 6e 20 28 65 4f ) or written (eO
16e36 70 21 3d 30 29 2e 0a 20 20 20 20 20 20 20 20 2a p!=0).. *
16e37 2f 0a 20 20 20 20 20 20 20 20 44 62 50 61 67 65 /. DbPage
16e38 20 2a 70 44 62 50 61 67 65 3b 0a 20 20 20 20 20 *pDbPage;.
16e39 20 20 20 69 6e 74 20 61 20 3d 20 61 6d 74 3b 0a int a = amt;.
16e3a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c rc = sql
16e3b 69 74 65 33 50 61 67 65 72 47 65 74 28 70 42 74 ite3PagerGet(pBt
16e3c 2d 3e 70 50 61 67 65 72 2c 20 6e 65 78 74 50 61 ->pPager, nextPa
16e3d 67 65 2c 20 26 70 44 62 50 61 67 65 29 3b 0a 20 ge, &pDbPage);.
16e3e 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 if( rc==S
16e3f 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
16e40 20 20 20 20 20 20 61 50 61 79 6c 6f 61 64 20 3d aPayload =
16e41 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 sqlite3PagerGet
16e42 44 61 74 61 28 70 44 62 50 61 67 65 29 3b 0a 20 Data(pDbPage);.
16e43 20 20 20 20 20 20 20 20 20 6e 65 78 74 50 61 67 nextPag
16e44 65 20 3d 20 67 65 74 34 62 79 74 65 28 61 50 61 e = get4byte(aPa
16e45 79 6c 6f 61 64 29 3b 0a 20 20 20 20 20 20 20 20 yload);.
16e46 20 20 69 66 28 20 61 20 2b 20 6f 66 66 73 65 74 if( a + offset
16e47 20 3e 20 6f 76 66 6c 53 69 7a 65 20 29 7b 0a 20 > ovflSize ){.
16e48 20 20 20 20 20 20 20 20 20 20 20 61 20 3d 20 6f a = o
16e49 76 66 6c 53 69 7a 65 20 2d 20 6f 66 66 73 65 74 vflSize - offset
16e4a 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
16e4b 20 20 20 20 20 20 20 20 72 63 20 3d 20 63 6f 70 rc = cop
16e4c 79 50 61 79 6c 6f 61 64 28 26 61 50 61 79 6c 6f yPayload(&aPaylo
16e4d 61 64 5b 6f 66 66 73 65 74 2b 34 5d 2c 20 70 42 ad[offset+4], pB
16e4e 75 66 2c 20 61 2c 20 65 4f 70 2c 20 70 44 62 50 uf, a, eOp, pDbP
16e4f 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 age);.
16e50 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 sqlite3PagerUnre
16e51 66 28 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 f(pDbPage);.
16e52 20 20 20 20 20 20 6f 66 66 73 65 74 20 3d 20 30 offset = 0
16e53 3b 0a 20 20 20 20 20 20 20 20 20 20 61 6d 74 20 ;. amt
16e54 2d 3d 20 61 3b 0a 20 20 20 20 20 20 20 20 20 20 -= a;.
16e55 70 42 75 66 20 2b 3d 20 61 3b 0a 20 20 20 20 20 pBuf += a;.
16e56 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
16e57 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63 }. }.. if( rc
16e58 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 61 ==SQLITE_OK && a
16e59 6d 74 3e 30 20 29 7b 0a 20 20 20 20 72 65 74 75 mt>0 ){. retu
16e5a 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 rn SQLITE_CORRUP
16e5b 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20 20 72 65 T_BKPT;. }. re
16e5c 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
16e5d 2a 20 52 65 61 64 20 70 61 72 74 20 6f 66 20 74 * Read part of t
16e5e 68 65 20 6b 65 79 20 61 73 73 6f 63 69 61 74 65 he key associate
16e5f 64 20 77 69 74 68 20 63 75 72 73 6f 72 20 70 43 d with cursor pC
16e60 75 72 2e 20 20 45 78 61 63 74 6c 79 0a 2a 2a 20 ur. Exactly.**
16e61 22 61 6d 74 22 20 62 79 74 65 73 20 77 69 6c 6c "amt" bytes will
16e62 20 62 65 20 74 72 61 6e 73 66 65 72 65 64 20 69 be transfered i
16e63 6e 74 6f 20 70 42 75 66 5b 5d 2e 20 20 54 68 65 nto pBuf[]. The
16e64 20 74 72 61 6e 73 66 65 72 0a 2a 2a 20 62 65 67 transfer.** beg
16e65 69 6e 73 20 61 74 20 22 6f 66 66 73 65 74 22 2e ins at "offset".
16e66 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 65 .**.** The calle
16e67 72 20 6d 75 73 74 20 65 6e 73 75 72 65 20 74 68 r must ensure th
16e68 61 74 20 70 43 75 72 20 69 73 20 70 6f 69 6e 74 at pCur is point
16e69 69 6e 67 20 74 6f 20 61 20 76 61 6c 69 64 20 72 ing to a valid r
16e6a 6f 77 0a 2a 2a 20 69 6e 20 74 68 65 20 74 61 62 ow.** in the tab
16e6b 6c 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e le..**.** Return
16e6c 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 SQLITE_OK on su
16e6d 63 63 65 73 73 20 6f 72 20 61 6e 20 65 72 72 6f ccess or an erro
16e6e 72 20 63 6f 64 65 20 69 66 20 61 6e 79 74 68 69 r code if anythi
16e6f 6e 67 20 67 6f 65 73 0a 2a 2a 20 77 72 6f 6e 67 ng goes.** wrong
16e70 2e 20 20 41 6e 20 65 72 72 6f 72 20 69 73 20 72 . An error is r
16e71 65 74 75 72 6e 65 64 20 69 66 20 22 6f 66 66 73 eturned if "offs
16e72 65 74 2b 61 6d 74 22 20 69 73 20 6c 61 72 67 65 et+amt" is large
16e73 72 20 74 68 61 6e 0a 2a 2a 20 74 68 65 20 61 76 r than.** the av
16e74 61 69 6c 61 62 6c 65 20 70 61 79 6c 6f 61 64 2e ailable payload.
16e75 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
16e76 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
16e77 72 65 65 4b 65 79 28 42 74 43 75 72 73 6f 72 20 reeKey(BtCursor
16e78 2a 70 43 75 72 2c 20 75 33 32 20 6f 66 66 73 65 *pCur, u32 offse
16e79 74 2c 20 75 33 32 20 61 6d 74 2c 20 76 6f 69 64 t, u32 amt, void
16e7a 20 2a 70 42 75 66 29 7b 0a 20 20 61 73 73 65 72 *pBuf){. asser
16e7b 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 t( cursorHoldsMu
16e7c 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61 tex(pCur) );. a
16e7d 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 ssert( pCur->eSt
16e7e 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 ate==CURSOR_VALI
16e7f 44 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 D );. assert( p
16e80 43 75 72 2d 3e 69 50 61 67 65 3e 3d 30 20 26 26 Cur->iPage>=0 &&
16e81 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 pCur->apPage[pC
16e82 75 72 2d 3e 69 50 61 67 65 5d 20 29 3b 0a 20 20 ur->iPage] );.
16e83 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 69 assert( pCur->ai
16e84 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d Idx[pCur->iPage]
16e85 3c 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 <pCur->apPage[pC
16e86 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c ur->iPage]->nCel
16e87 6c 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 61 63 l );. return ac
16e88 63 65 73 73 50 61 79 6c 6f 61 64 28 70 43 75 72 cessPayload(pCur
16e89 2c 20 6f 66 66 73 65 74 2c 20 61 6d 74 2c 20 28 , offset, amt, (
16e8a 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 29 70 unsigned char*)p
16e8b 42 75 66 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a Buf, 0);.}../*.*
16e8c 2a 20 52 65 61 64 20 70 61 72 74 20 6f 66 20 74 * Read part of t
16e8d 68 65 20 64 61 74 61 20 61 73 73 6f 63 69 61 74 he data associat
16e8e 65 64 20 77 69 74 68 20 63 75 72 73 6f 72 20 70 ed with cursor p
16e8f 43 75 72 2e 20 20 45 78 61 63 74 6c 79 0a 2a 2a Cur. Exactly.**
16e90 20 22 61 6d 74 22 20 62 79 74 65 73 20 77 69 6c "amt" bytes wil
16e91 6c 20 62 65 20 74 72 61 6e 73 66 65 72 65 64 20 l be transfered
16e92 69 6e 74 6f 20 70 42 75 66 5b 5d 2e 20 20 54 68 into pBuf[]. Th
16e93 65 20 74 72 61 6e 73 66 65 72 0a 2a 2a 20 62 65 e transfer.** be
16e94 67 69 6e 73 20 61 74 20 22 6f 66 66 73 65 74 22 gins at "offset"
16e95 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 ..**.** Return S
16e96 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 QLITE_OK on succ
16e97 65 73 73 20 6f 72 20 61 6e 20 65 72 72 6f 72 20 ess or an error
16e98 63 6f 64 65 20 69 66 20 61 6e 79 74 68 69 6e 67 code if anything
16e99 20 67 6f 65 73 0a 2a 2a 20 77 72 6f 6e 67 2e 20 goes.** wrong.
16e9a 20 41 6e 20 65 72 72 6f 72 20 69 73 20 72 65 74 An error is ret
16e9b 75 72 6e 65 64 20 69 66 20 22 6f 66 66 73 65 74 urned if "offset
16e9c 2b 61 6d 74 22 20 69 73 20 6c 61 72 67 65 72 20 +amt" is larger
16e9d 74 68 61 6e 0a 2a 2a 20 74 68 65 20 61 76 61 69 than.** the avai
16e9e 6c 61 62 6c 65 20 70 61 79 6c 6f 61 64 2e 0a 2a lable payload..*
16e9f 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
16ea0 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
16ea1 65 44 61 74 61 28 42 74 43 75 72 73 6f 72 20 2a eData(BtCursor *
16ea2 70 43 75 72 2c 20 75 33 32 20 6f 66 66 73 65 74 pCur, u32 offset
16ea3 2c 20 75 33 32 20 61 6d 74 2c 20 76 6f 69 64 20 , u32 amt, void
16ea4 2a 70 42 75 66 29 7b 0a 20 20 69 6e 74 20 72 63 *pBuf){. int rc
16ea5 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 ;..#ifndef SQLIT
16ea6 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a E_OMIT_INCRBLOB.
16ea7 20 20 69 66 20 28 20 70 43 75 72 2d 3e 65 53 74 if ( pCur->eSt
16ea8 61 74 65 3d 3d 43 55 52 53 4f 52 5f 49 4e 56 41 ate==CURSOR_INVA
16ea9 4c 49 44 20 29 7b 0a 20 20 20 20 72 65 74 75 72 LID ){. retur
16eaa 6e 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 3b 0a n SQLITE_ABORT;.
16eab 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 61 73 }.#endif.. as
16eac 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 sert( cursorHold
16ead 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a sMutex(pCur) );.
16eae 20 20 72 63 20 3d 20 72 65 73 74 6f 72 65 43 75 rc = restoreCu
16eaf 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 43 75 rsorPosition(pCu
16eb0 72 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 r);. if( rc==SQ
16eb1 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 61 LITE_OK ){. a
16eb2 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 ssert( pCur->eSt
16eb3 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 ate==CURSOR_VALI
16eb4 44 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 D );. assert(
16eb5 20 70 43 75 72 2d 3e 69 50 61 67 65 3e 3d 30 20 pCur->iPage>=0
16eb6 26 26 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b && pCur->apPage[
16eb7 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 29 3b 0a pCur->iPage] );.
16eb8 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 assert( pCur
16eb9 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 ->aiIdx[pCur->iP
16eba 61 67 65 5d 3c 70 43 75 72 2d 3e 61 70 50 61 67 age]<pCur->apPag
16ebb 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e e[pCur->iPage]->
16ebc 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 20 72 63 20 nCell );. rc
16ebd 3d 20 61 63 63 65 73 73 50 61 79 6c 6f 61 64 28 = accessPayload(
16ebe 70 43 75 72 2c 20 6f 66 66 73 65 74 2c 20 61 6d pCur, offset, am
16ebf 74 2c 20 70 42 75 66 2c 20 30 29 3b 0a 20 20 7d t, pBuf, 0);. }
16ec0 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
16ec1 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 ./*.** Return a
16ec2 70 6f 69 6e 74 65 72 20 74 6f 20 70 61 79 6c 6f pointer to paylo
16ec3 61 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 ad information f
16ec4 72 6f 6d 20 74 68 65 20 65 6e 74 72 79 20 74 68 rom the entry th
16ec5 61 74 20 74 68 65 20 0a 2a 2a 20 70 43 75 72 20 at the .** pCur
16ec6 63 75 72 73 6f 72 20 69 73 20 70 6f 69 6e 74 69 cursor is pointi
16ec7 6e 67 20 74 6f 2e 20 20 54 68 65 20 70 6f 69 6e ng to. The poin
16ec8 74 65 72 20 69 73 20 74 6f 20 74 68 65 20 62 65 ter is to the be
16ec9 67 69 6e 6e 69 6e 67 20 6f 66 0a 2a 2a 20 74 68 ginning of.** th
16eca 65 20 6b 65 79 20 69 66 20 73 6b 69 70 4b 65 79 e key if skipKey
16ecb 3d 3d 30 20 61 6e 64 20 69 74 20 70 6f 69 6e 74 ==0 and it point
16ecc 73 20 74 6f 20 74 68 65 20 62 65 67 69 6e 6e 69 s to the beginni
16ecd 6e 67 20 6f 66 20 64 61 74 61 20 69 66 0a 2a 2a ng of data if.**
16ece 20 73 6b 69 70 4b 65 79 3d 3d 31 2e 20 20 54 68 skipKey==1. Th
16ecf 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 e number of byte
16ed0 73 20 6f 66 20 61 76 61 69 6c 61 62 6c 65 20 6b s of available k
16ed1 65 79 2f 64 61 74 61 20 69 73 20 77 72 69 74 74 ey/data is writt
16ed2 65 6e 0a 2a 2a 20 69 6e 74 6f 20 2a 70 41 6d 74 en.** into *pAmt
16ed3 2e 20 20 49 66 20 2a 70 41 6d 74 3d 3d 30 2c 20 . If *pAmt==0,
16ed4 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 72 then the value r
16ed5 65 74 75 72 6e 65 64 20 77 69 6c 6c 20 6e 6f 74 eturned will not
16ed6 20 62 65 0a 2a 2a 20 61 20 76 61 6c 69 64 20 70 be.** a valid p
16ed7 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 ointer..**.** Th
16ed8 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 6e is routine is an
16ed9 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2e 20 20 optimization.
16eda 49 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 66 6f 72 It is common for
16edb 20 74 68 65 20 65 6e 74 69 72 65 20 6b 65 79 0a the entire key.
16edc 2a 2a 20 61 6e 64 20 64 61 74 61 20 74 6f 20 66 ** and data to f
16edd 69 74 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20 it on the local
16ede 70 61 67 65 20 61 6e 64 20 66 6f 72 20 74 68 65 page and for the
16edf 72 65 20 74 6f 20 62 65 20 6e 6f 20 6f 76 65 72 re to be no over
16ee0 66 6c 6f 77 0a 2a 2a 20 70 61 67 65 73 2e 20 20 flow.** pages.
16ee1 57 68 65 6e 20 74 68 61 74 20 69 73 20 73 6f 2c When that is so,
16ee2 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 63 61 this routine ca
16ee3 6e 20 62 65 20 75 73 65 64 20 74 6f 20 61 63 63 n be used to acc
16ee4 65 73 73 20 74 68 65 0a 2a 2a 20 6b 65 79 20 61 ess the.** key a
16ee5 6e 64 20 64 61 74 61 20 77 69 74 68 6f 75 74 20 nd data without
16ee6 6d 61 6b 69 6e 67 20 61 20 63 6f 70 79 2e 20 20 making a copy.
16ee7 49 66 20 74 68 65 20 6b 65 79 20 61 6e 64 2f 6f If the key and/o
16ee8 72 20 64 61 74 61 20 73 70 69 6c 6c 73 0a 2a 2a r data spills.**
16ee9 20 6f 6e 74 6f 20 6f 76 65 72 66 6c 6f 77 20 70 onto overflow p
16eea 61 67 65 73 2c 20 74 68 65 6e 20 61 63 63 65 73 ages, then acces
16eeb 73 50 61 79 6c 6f 61 64 28 29 20 6d 75 73 74 20 sPayload() must
16eec 62 65 20 75 73 65 64 20 74 6f 20 72 65 61 73 73 be used to reass
16eed 65 6d 62 6c 65 0a 2a 2a 20 74 68 65 20 6b 65 79 emble.** the key
16eee 2f 64 61 74 61 20 61 6e 64 20 63 6f 70 79 20 69 /data and copy i
16eef 74 20 69 6e 74 6f 20 61 20 70 72 65 61 6c 6c 6f t into a preallo
16ef0 63 61 74 65 64 20 62 75 66 66 65 72 2e 0a 2a 2a cated buffer..**
16ef1 0a 2a 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 20 .** The pointer
16ef2 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 69 73 returned by this
16ef3 20 72 6f 75 74 69 6e 65 20 6c 6f 6f 6b 73 20 64 routine looks d
16ef4 69 72 65 63 74 6c 79 20 69 6e 74 6f 20 74 68 65 irectly into the
16ef5 20 63 61 63 68 65 64 0a 2a 2a 20 70 61 67 65 20 cached.** page
16ef6 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 2e of the database.
16ef7 20 20 54 68 65 20 64 61 74 61 20 6d 69 67 68 74 The data might
16ef8 20 63 68 61 6e 67 65 20 6f 72 20 6d 6f 76 65 20 change or move
16ef9 74 68 65 20 6e 65 78 74 20 74 69 6d 65 0a 2a 2a the next time.**
16efa 20 61 6e 79 20 62 74 72 65 65 20 72 6f 75 74 69 any btree routi
16efb 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f ne is called..*/
16efc 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e .static const un
16efd 73 69 67 6e 65 64 20 63 68 61 72 20 2a 66 65 74 signed char *fet
16efe 63 68 50 61 79 6c 6f 61 64 28 0a 20 20 42 74 43 chPayload(. BtC
16eff 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 20 20 20 ursor *pCur,
16f00 20 20 2f 2a 20 43 75 72 73 6f 72 20 70 6f 69 6e /* Cursor poin
16f01 74 69 6e 67 20 74 6f 20 65 6e 74 72 79 20 74 6f ting to entry to
16f02 20 72 65 61 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 read from */.
16f03 69 6e 74 20 2a 70 41 6d 74 2c 20 20 20 20 20 20 int *pAmt,
16f04 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 /* Write th
16f05 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 76 61 69 e number of avai
16f06 6c 61 62 6c 65 20 62 79 74 65 73 20 68 65 72 65 lable bytes here
16f07 20 2a 2f 0a 20 20 69 6e 74 20 73 6b 69 70 4b 65 */. int skipKe
16f08 79 20 20 20 20 20 20 20 20 20 20 2f 2a 20 72 65 y /* re
16f09 61 64 20 62 65 67 69 6e 6e 69 6e 67 20 61 74 20 ad beginning at
16f0a 64 61 74 61 20 69 66 20 74 68 69 73 20 69 73 20 data if this is
16f0b 74 72 75 65 20 2a 2f 0a 29 7b 0a 20 20 75 6e 73 true */.){. uns
16f0c 69 67 6e 65 64 20 63 68 61 72 20 2a 61 50 61 79 igned char *aPay
16f0d 6c 6f 61 64 3b 0a 20 20 4d 65 6d 50 61 67 65 20 load;. MemPage
16f0e 2a 70 50 61 67 65 3b 0a 20 20 75 33 32 20 6e 4b *pPage;. u32 nK
16f0f 65 79 3b 0a 20 20 75 33 32 20 6e 4c 6f 63 61 6c ey;. u32 nLocal
16f10 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 ;.. assert( pCu
16f11 72 21 3d 30 20 26 26 20 70 43 75 72 2d 3e 69 50 r!=0 && pCur->iP
16f12 61 67 65 3e 3d 30 20 26 26 20 70 43 75 72 2d 3e age>=0 && pCur->
16f13 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 apPage[pCur->iPa
16f14 67 65 5d 29 3b 0a 20 20 61 73 73 65 72 74 28 20 ge]);. assert(
16f15 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 pCur->eState==CU
16f16 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 RSOR_VALID );.
16f17 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f assert( cursorHo
16f18 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 ldsMutex(pCur) )
16f19 3b 0a 20 20 70 50 61 67 65 20 3d 20 70 43 75 72 ;. pPage = pCur
16f1a 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 ->apPage[pCur->i
16f1b 50 61 67 65 5d 3b 0a 20 20 61 73 73 65 72 74 28 Page];. assert(
16f1c 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 pCur->aiIdx[pCu
16f1d 72 2d 3e 69 50 61 67 65 5d 3c 70 50 61 67 65 2d r->iPage]<pPage-
16f1e 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 69 66 28 20 >nCell );. if(
16f1f 4e 45 56 45 52 28 70 43 75 72 2d 3e 69 6e 66 6f NEVER(pCur->info
16f20 2e 6e 53 69 7a 65 3d 3d 30 29 20 29 7b 0a 20 20 .nSize==0) ){.
16f21 20 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c btreeParseCell
16f22 28 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 (pCur->apPage[pC
16f23 75 72 2d 3e 69 50 61 67 65 5d 2c 20 70 43 75 72 ur->iPage], pCur
16f24 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 ->aiIdx[pCur->iP
16f25 61 67 65 5d 2c 0a 20 20 20 20 20 20 20 20 20 20 age],.
16f26 20 20 20 20 20 20 20 20 20 26 70 43 75 72 2d 3e &pCur->
16f27 69 6e 66 6f 29 3b 0a 20 20 7d 0a 20 20 61 50 61 info);. }. aPa
16f28 79 6c 6f 61 64 20 3d 20 70 43 75 72 2d 3e 69 6e yload = pCur->in
16f29 66 6f 2e 70 43 65 6c 6c 3b 0a 20 20 61 50 61 79 fo.pCell;. aPay
16f2a 6c 6f 61 64 20 2b 3d 20 70 43 75 72 2d 3e 69 6e load += pCur->in
16f2b 66 6f 2e 6e 48 65 61 64 65 72 3b 0a 20 20 69 66 fo.nHeader;. if
16f2c 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 ( pPage->intKey
16f2d 29 7b 0a 20 20 20 20 6e 4b 65 79 20 3d 20 30 3b ){. nKey = 0;
16f2e 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 4b . }else{. nK
16f2f 65 79 20 3d 20 28 69 6e 74 29 70 43 75 72 2d 3e ey = (int)pCur->
16f30 69 6e 66 6f 2e 6e 4b 65 79 3b 0a 20 20 7d 0a 20 info.nKey;. }.
16f31 20 69 66 28 20 73 6b 69 70 4b 65 79 20 29 7b 0a if( skipKey ){.
16f32 20 20 20 20 61 50 61 79 6c 6f 61 64 20 2b 3d 20 aPayload +=
16f33 6e 4b 65 79 3b 0a 20 20 20 20 6e 4c 6f 63 61 6c nKey;. nLocal
16f34 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c = pCur->info.nL
16f35 6f 63 61 6c 20 2d 20 6e 4b 65 79 3b 0a 20 20 7d ocal - nKey;. }
16f36 65 6c 73 65 7b 0a 20 20 20 20 6e 4c 6f 63 61 6c else{. nLocal
16f37 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c = pCur->info.nL
16f38 6f 63 61 6c 3b 0a 20 20 20 20 61 73 73 65 72 74 ocal;. assert
16f39 28 20 6e 4c 6f 63 61 6c 3c 3d 6e 4b 65 79 20 29 ( nLocal<=nKey )
16f3a 3b 0a 20 20 7d 0a 20 20 2a 70 41 6d 74 20 3d 20 ;. }. *pAmt =
16f3b 6e 4c 6f 63 61 6c 3b 0a 20 20 72 65 74 75 72 6e nLocal;. return
16f3c 20 61 50 61 79 6c 6f 61 64 3b 0a 7d 0a 0a 0a 2f aPayload;.}.../
16f3d 2a 0a 2a 2a 20 46 6f 72 20 74 68 65 20 65 6e 74 *.** For the ent
16f3e 72 79 20 74 68 61 74 20 63 75 72 73 6f 72 20 70 ry that cursor p
16f3f 43 75 72 20 69 73 20 70 6f 69 6e 74 20 74 6f 2c Cur is point to,
16f40 20 72 65 74 75 72 6e 20 61 73 0a 2a 2a 20 6d 61 return as.** ma
16f41 6e 79 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 ny bytes of the
16f42 6b 65 79 20 6f 72 20 64 61 74 61 20 61 73 20 61 key or data as a
16f43 72 65 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 re available on
16f44 74 68 65 20 6c 6f 63 61 6c 0a 2a 2a 20 62 2d 74 the local.** b-t
16f45 72 65 65 20 70 61 67 65 2e 20 20 57 72 69 74 65 ree page. Write
16f46 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 the number of a
16f47 76 61 69 6c 61 62 6c 65 20 62 79 74 65 73 20 69 vailable bytes i
16f48 6e 74 6f 20 2a 70 41 6d 74 2e 0a 2a 2a 0a 2a 2a nto *pAmt..**.**
16f49 20 54 68 65 20 70 6f 69 6e 74 65 72 20 72 65 74 The pointer ret
16f4a 75 72 6e 65 64 20 69 73 20 65 70 68 65 6d 65 72 urned is ephemer
16f4b 61 6c 2e 20 20 54 68 65 20 6b 65 79 2f 64 61 74 al. The key/dat
16f4c 61 20 6d 61 79 20 6d 6f 76 65 0a 2a 2a 20 6f 72 a may move.** or
16f4d 20 62 65 20 64 65 73 74 72 6f 79 65 64 20 6f 6e be destroyed on
16f4e 20 74 68 65 20 6e 65 78 74 20 63 61 6c 6c 20 74 the next call t
16f4f 6f 20 61 6e 79 20 42 74 72 65 65 20 72 6f 75 74 o any Btree rout
16f50 69 6e 65 2c 0a 2a 2a 20 69 6e 63 6c 75 64 69 6e ine,.** includin
16f51 67 20 63 61 6c 6c 73 20 66 72 6f 6d 20 6f 74 68 g calls from oth
16f52 65 72 20 74 68 72 65 61 64 73 20 61 67 61 69 6e er threads again
16f53 73 74 20 74 68 65 20 73 61 6d 65 20 63 61 63 68 st the same cach
16f54 65 2e 0a 2a 2a 20 48 65 6e 63 65 2c 20 61 20 6d e..** Hence, a m
16f55 75 74 65 78 20 6f 6e 20 74 68 65 20 42 74 53 68 utex on the BtSh
16f56 61 72 65 64 20 73 68 6f 75 6c 64 20 62 65 20 68 ared should be h
16f57 65 6c 64 20 70 72 69 6f 72 20 74 6f 20 63 61 6c eld prior to cal
16f58 6c 69 6e 67 0a 2a 2a 20 74 68 69 73 20 72 6f 75 ling.** this rou
16f59 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 tine..**.** Thes
16f5a 65 20 72 6f 75 74 69 6e 65 73 20 69 73 20 75 73 e routines is us
16f5b 65 64 20 74 6f 20 67 65 74 20 71 75 69 63 6b 20 ed to get quick
16f5c 61 63 63 65 73 73 20 74 6f 20 6b 65 79 20 61 6e access to key an
16f5d 64 20 64 61 74 61 0a 2a 2a 20 69 6e 20 74 68 65 d data.** in the
16f5e 20 63 6f 6d 6d 6f 6e 20 63 61 73 65 20 77 68 65 common case whe
16f5f 72 65 20 6e 6f 20 6f 76 65 72 66 6c 6f 77 20 70 re no overflow p
16f60 61 67 65 73 20 61 72 65 20 75 73 65 64 2e 0a 2a ages are used..*
16f61 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
16f62 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c const void *sql
16f63 69 74 65 33 42 74 72 65 65 4b 65 79 46 65 74 63 ite3BtreeKeyFetc
16f64 68 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 h(BtCursor *pCur
16f65 2c 20 69 6e 74 20 2a 70 41 6d 74 29 7b 0a 20 20 , int *pAmt){.
16f66 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 20 3d 20 const void *p =
16f67 30 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 0;. assert( sql
16f68 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
16f69 70 43 75 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 pCur->pBtree->db
16f6a 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 ->mutex) );. as
16f6b 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 sert( cursorHold
16f6c 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a sMutex(pCur) );.
16f6d 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 43 75 if( ALWAYS(pCu
16f6e 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f r->eState==CURSO
16f6f 52 5f 56 41 4c 49 44 29 20 29 7b 0a 20 20 20 20 R_VALID) ){.
16f70 70 20 3d 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a p = (const void*
16f71 29 66 65 74 63 68 50 61 79 6c 6f 61 64 28 70 43 )fetchPayload(pC
16f72 75 72 2c 20 70 41 6d 74 2c 20 30 29 3b 0a 20 20 ur, pAmt, 0);.
16f73 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a }. return p;.}.
16f74 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 SQLITE_PRIVATE c
16f75 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 onst void *sqlit
16f76 65 33 42 74 72 65 65 44 61 74 61 46 65 74 63 68 e3BtreeDataFetch
16f77 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c (BtCursor *pCur,
16f78 20 69 6e 74 20 2a 70 41 6d 74 29 7b 0a 20 20 63 int *pAmt){. c
16f79 6f 6e 73 74 20 76 6f 69 64 20 2a 70 20 3d 20 30 onst void *p = 0
16f7a 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
16f7b 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
16f7c 43 75 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d Cur->pBtree->db-
16f7d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 >mutex) );. ass
16f7e 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 ert( cursorHolds
16f7f 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 Mutex(pCur) );.
16f80 20 69 66 28 20 41 4c 57 41 59 53 28 70 43 75 72 if( ALWAYS(pCur
16f81 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 ->eState==CURSOR
16f82 5f 56 41 4c 49 44 29 20 29 7b 0a 20 20 20 20 70 _VALID) ){. p
16f83 20 3d 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 29 = (const void*)
16f84 66 65 74 63 68 50 61 79 6c 6f 61 64 28 70 43 75 fetchPayload(pCu
16f85 72 2c 20 70 41 6d 74 2c 20 31 29 3b 0a 20 20 7d r, pAmt, 1);. }
16f86 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a . return p;.}..
16f87 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20 ./*.** Move the
16f88 63 75 72 73 6f 72 20 64 6f 77 6e 20 74 6f 20 61 cursor down to a
16f89 20 6e 65 77 20 63 68 69 6c 64 20 70 61 67 65 2e new child page.
16f8a 20 20 54 68 65 20 6e 65 77 50 67 6e 6f 20 61 72 The newPgno ar
16f8b 67 75 6d 65 6e 74 20 69 73 20 74 68 65 0a 2a 2a gument is the.**
16f8c 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 page number of
16f8d 74 68 65 20 63 68 69 6c 64 20 70 61 67 65 20 74 the child page t
16f8e 6f 20 6d 6f 76 65 20 74 6f 2e 0a 2a 2a 0a 2a 2a o move to..**.**
16f8f 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 This function r
16f90 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 43 4f eturns SQLITE_CO
16f91 52 52 55 50 54 20 69 66 20 74 68 65 20 70 61 67 RRUPT if the pag
16f92 65 2d 68 65 61 64 65 72 20 66 6c 61 67 73 20 66 e-header flags f
16f93 69 65 6c 64 20 6f 66 0a 2a 2a 20 74 68 65 20 6e ield of.** the n
16f94 65 77 20 63 68 69 6c 64 20 70 61 67 65 20 64 6f ew child page do
16f95 65 73 20 6e 6f 74 20 6d 61 74 63 68 20 74 68 65 es not match the
16f96 20 66 6c 61 67 73 20 66 69 65 6c 64 20 6f 66 20 flags field of
16f97 74 68 65 20 70 61 72 65 6e 74 20 28 69 2e 65 2e the parent (i.e.
16f98 0a 2a 2a 20 69 66 20 61 6e 20 69 6e 74 6b 65 79 .** if an intkey
16f99 20 70 61 67 65 20 61 70 70 65 61 72 73 20 74 6f page appears to
16f9a 20 62 65 20 74 68 65 20 70 61 72 65 6e 74 20 6f be the parent o
16f9b 66 20 61 20 6e 6f 6e 2d 69 6e 74 6b 65 79 20 70 f a non-intkey p
16f9c 61 67 65 2c 20 6f 72 0a 2a 2a 20 76 69 63 65 2d age, or.** vice-
16f9d 76 65 72 73 61 29 2e 0a 2a 2f 0a 73 74 61 74 69 versa)..*/.stati
16f9e 63 20 69 6e 74 20 6d 6f 76 65 54 6f 43 68 69 6c c int moveToChil
16f9f 64 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 d(BtCursor *pCur
16fa0 2c 20 75 33 32 20 6e 65 77 50 67 6e 6f 29 7b 0a , u32 newPgno){.
16fa1 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74 20 int rc;. int
16fa2 69 20 3d 20 70 43 75 72 2d 3e 69 50 61 67 65 3b i = pCur->iPage;
16fa3 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 4e 65 77 . MemPage *pNew
16fa4 50 61 67 65 3b 0a 20 20 42 74 53 68 61 72 65 64 Page;. BtShared
16fa5 20 2a 70 42 74 20 3d 20 70 43 75 72 2d 3e 70 42 *pBt = pCur->pB
16fa6 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 t;.. assert( cu
16fa7 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 rsorHoldsMutex(p
16fa8 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 Cur) );. assert
16fa9 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d ( pCur->eState==
16faa 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a CURSOR_VALID );.
16fab 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e assert( pCur->
16fac 69 50 61 67 65 3c 42 54 43 55 52 53 4f 52 5f 4d iPage<BTCURSOR_M
16fad 41 58 5f 44 45 50 54 48 20 29 3b 0a 20 20 69 66 AX_DEPTH );. if
16fae 28 20 70 43 75 72 2d 3e 69 50 61 67 65 3e 3d 28 ( pCur->iPage>=(
16faf 42 54 43 55 52 53 4f 52 5f 4d 41 58 5f 44 45 50 BTCURSOR_MAX_DEP
16fb0 54 48 2d 31 29 20 29 7b 0a 20 20 20 20 72 65 74 TH-1) ){. ret
16fb1 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 urn SQLITE_CORRU
16fb2 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20 20 72 PT_BKPT;. }. r
16fb3 63 20 3d 20 67 65 74 41 6e 64 49 6e 69 74 50 61 c = getAndInitPa
16fb4 67 65 28 70 42 74 2c 20 6e 65 77 50 67 6e 6f 2c ge(pBt, newPgno,
16fb5 20 26 70 4e 65 77 50 61 67 65 29 3b 0a 20 20 69 &pNewPage);. i
16fb6 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 f( rc ) return r
16fb7 63 3b 0a 20 20 70 43 75 72 2d 3e 61 70 50 61 67 c;. pCur->apPag
16fb8 65 5b 69 2b 31 5d 20 3d 20 70 4e 65 77 50 61 67 e[i+1] = pNewPag
16fb9 65 3b 0a 20 20 70 43 75 72 2d 3e 61 69 49 64 78 e;. pCur->aiIdx
16fba 5b 69 2b 31 5d 20 3d 20 30 3b 0a 20 20 70 43 75 [i+1] = 0;. pCu
16fbb 72 2d 3e 69 50 61 67 65 2b 2b 3b 0a 0a 20 20 70 r->iPage++;.. p
16fbc 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 Cur->info.nSize
16fbd 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e 76 61 6c = 0;. pCur->val
16fbe 69 64 4e 4b 65 79 20 3d 20 30 3b 0a 20 20 69 66 idNKey = 0;. if
16fbf 28 20 70 4e 65 77 50 61 67 65 2d 3e 6e 43 65 6c ( pNewPage->nCel
16fc0 6c 3c 31 20 7c 7c 20 70 4e 65 77 50 61 67 65 2d l<1 || pNewPage-
16fc1 3e 69 6e 74 4b 65 79 21 3d 70 43 75 72 2d 3e 61 >intKey!=pCur->a
16fc2 70 50 61 67 65 5b 69 5d 2d 3e 69 6e 74 4b 65 79 pPage[i]->intKey
16fc3 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
16fc4 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b QLITE_CORRUPT_BK
16fc5 50 54 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e PT;. }. return
16fc6 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 SQLITE_OK;.}..#
16fc7 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 2f 2a ifndef NDEBUG./*
16fc8 0a 2a 2a 20 50 61 67 65 20 70 50 61 72 65 6e 74 .** Page pParent
16fc9 20 69 73 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20 is an internal
16fca 28 6e 6f 6e 2d 6c 65 61 66 29 20 74 72 65 65 20 (non-leaf) tree
16fcb 70 61 67 65 2e 20 54 68 69 73 20 66 75 6e 63 74 page. This funct
16fcc 69 6f 6e 20 0a 2a 2a 20 61 73 73 65 72 74 73 20 ion .** asserts
16fcd 74 68 61 74 20 70 61 67 65 20 6e 75 6d 62 65 72 that page number
16fce 20 69 43 68 69 6c 64 20 69 73 20 74 68 65 20 6c iChild is the l
16fcf 65 66 74 2d 63 68 69 6c 64 20 69 66 20 74 68 65 eft-child if the
16fd0 20 69 49 64 78 27 74 68 0a 2a 2a 20 63 65 6c 6c iIdx'th.** cell
16fd1 20 69 6e 20 70 61 67 65 20 70 50 61 72 65 6e 74 in page pParent
16fd2 2e 20 4f 72 2c 20 69 66 20 69 49 64 78 20 69 73 . Or, if iIdx is
16fd3 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 74 6f equal to the to
16fd4 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a tal number of.**
16fd5 20 63 65 6c 6c 73 20 69 6e 20 70 50 61 72 65 6e cells in pParen
16fd6 74 2c 20 74 68 61 74 20 70 61 67 65 20 6e 75 6d t, that page num
16fd7 62 65 72 20 69 43 68 69 6c 64 20 69 73 20 74 68 ber iChild is th
16fd8 65 20 72 69 67 68 74 2d 63 68 69 6c 64 20 6f 66 e right-child of
16fd9 0a 2a 2a 20 74 68 65 20 70 61 67 65 2e 0a 2a 2f .** the page..*/
16fda 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 73 73 .static void ass
16fdb 65 72 74 50 61 72 65 6e 74 49 6e 64 65 78 28 4d ertParentIndex(M
16fdc 65 6d 50 61 67 65 20 2a 70 50 61 72 65 6e 74 2c emPage *pParent,
16fdd 20 69 6e 74 20 69 49 64 78 2c 20 50 67 6e 6f 20 int iIdx, Pgno
16fde 69 43 68 69 6c 64 29 7b 0a 20 20 61 73 73 65 72 iChild){. asser
16fdf 74 28 20 69 49 64 78 3c 3d 70 50 61 72 65 6e 74 t( iIdx<=pParent
16fe0 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 69 66 28 ->nCell );. if(
16fe1 20 69 49 64 78 3d 3d 70 50 61 72 65 6e 74 2d 3e iIdx==pParent->
16fe2 6e 43 65 6c 6c 20 29 7b 0a 20 20 20 20 61 73 73 nCell ){. ass
16fe3 65 72 74 28 20 67 65 74 34 62 79 74 65 28 26 70 ert( get4byte(&p
16fe4 50 61 72 65 6e 74 2d 3e 61 44 61 74 61 5b 70 50 Parent->aData[pP
16fe5 61 72 65 6e 74 2d 3e 68 64 72 4f 66 66 73 65 74 arent->hdrOffset
16fe6 2b 38 5d 29 3d 3d 69 43 68 69 6c 64 20 29 3b 0a +8])==iChild );.
16fe7 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 }else{. ass
16fe8 65 72 74 28 20 67 65 74 34 62 79 74 65 28 66 69 ert( get4byte(fi
16fe9 6e 64 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 ndCell(pParent,
16fea 69 49 64 78 29 29 3d 3d 69 43 68 69 6c 64 20 29 iIdx))==iChild )
16feb 3b 0a 20 20 7d 0a 7d 0a 23 65 6c 73 65 0a 23 20 ;. }.}.#else.#
16fec 20 64 65 66 69 6e 65 20 61 73 73 65 72 74 50 61 define assertPa
16fed 72 65 6e 74 49 6e 64 65 78 28 78 2c 79 2c 7a 29 rentIndex(x,y,z)
16fee 20 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 .#endif../*.**
16fef 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f 72 20 Move the cursor
16ff0 75 70 20 74 6f 20 74 68 65 20 70 61 72 65 6e 74 up to the parent
16ff1 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 70 43 75 page..**.** pCu
16ff2 72 2d 3e 69 64 78 20 69 73 20 73 65 74 20 74 6f r->idx is set to
16ff3 20 74 68 65 20 63 65 6c 6c 20 69 6e 64 65 78 20 the cell index
16ff4 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 that contains th
16ff5 65 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 e pointer.** to
16ff6 74 68 65 20 70 61 67 65 20 77 65 20 61 72 65 20 the page we are
16ff7 63 6f 6d 69 6e 67 20 66 72 6f 6d 2e 20 20 49 66 coming from. If
16ff8 20 77 65 20 61 72 65 20 63 6f 6d 69 6e 67 20 66 we are coming f
16ff9 72 6f 6d 20 74 68 65 0a 2a 2a 20 72 69 67 68 74 rom the.** right
16ffa 2d 6d 6f 73 74 20 63 68 69 6c 64 20 70 61 67 65 -most child page
16ffb 20 74 68 65 6e 20 70 43 75 72 2d 3e 69 64 78 20 then pCur->idx
16ffc 69 73 20 73 65 74 20 74 6f 20 6f 6e 65 20 6d 6f is set to one mo
16ffd 72 65 20 74 68 61 6e 0a 2a 2a 20 74 68 65 20 6c re than.** the l
16ffe 61 72 67 65 73 74 20 63 65 6c 6c 20 69 6e 64 65 argest cell inde
16fff 78 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 x..*/.static voi
17000 64 20 6d 6f 76 65 54 6f 50 61 72 65 6e 74 28 42 d moveToParent(B
17001 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a tCursor *pCur){.
17002 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 assert( cursor
17003 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 HoldsMutex(pCur)
17004 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 );. assert( pC
17005 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 ur->eState==CURS
17006 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 61 73 OR_VALID );. as
17007 73 65 72 74 28 20 70 43 75 72 2d 3e 69 50 61 67 sert( pCur->iPag
17008 65 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 e>0 );. assert(
17009 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 pCur->apPage[pC
1700a 75 72 2d 3e 69 50 61 67 65 5d 20 29 3b 0a 20 20 ur->iPage] );.
1700b 61 73 73 65 72 74 50 61 72 65 6e 74 49 6e 64 65 assertParentInde
1700c 78 28 0a 20 20 20 20 70 43 75 72 2d 3e 61 70 50 x(. pCur->apP
1700d 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 2d age[pCur->iPage-
1700e 31 5d 2c 20 0a 20 20 20 20 70 43 75 72 2d 3e 61 1], . pCur->a
1700f 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 iIdx[pCur->iPage
17010 2d 31 5d 2c 20 0a 20 20 20 20 70 43 75 72 2d 3e -1], . pCur->
17011 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 apPage[pCur->iPa
17012 67 65 5d 2d 3e 70 67 6e 6f 0a 20 20 29 3b 0a 20 ge]->pgno. );.
17013 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 43 75 releasePage(pCu
17014 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e r->apPage[pCur->
17015 69 50 61 67 65 5d 29 3b 0a 20 20 70 43 75 72 2d iPage]);. pCur-
17016 3e 69 50 61 67 65 2d 2d 3b 0a 20 20 70 43 75 72 >iPage--;. pCur
17017 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 ->info.nSize = 0
17018 3b 0a 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e ;. pCur->validN
17019 4b 65 79 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a Key = 0;.}../*.*
1701a 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f * Move the curso
1701b 72 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 r to point to th
1701c 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 69 e root page of i
1701d 74 73 20 62 2d 74 72 65 65 20 73 74 72 75 63 74 ts b-tree struct
1701e 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 ure..**.** If th
1701f 65 20 74 61 62 6c 65 20 68 61 73 20 61 20 76 69 e table has a vi
17020 72 74 75 61 6c 20 72 6f 6f 74 20 70 61 67 65 2c rtual root page,
17021 20 74 68 65 6e 20 74 68 65 20 63 75 72 73 6f 72 then the cursor
17022 20 69 73 20 6d 6f 76 65 64 20 74 6f 20 70 6f 69 is moved to poi
17023 6e 74 0a 2a 2a 20 74 6f 20 74 68 65 20 76 69 72 nt.** to the vir
17024 74 75 61 6c 20 72 6f 6f 74 20 70 61 67 65 20 69 tual root page i
17025 6e 73 74 65 61 64 20 6f 66 20 74 68 65 20 61 63 nstead of the ac
17026 74 75 61 6c 20 72 6f 6f 74 20 70 61 67 65 2e 20 tual root page.
17027 41 20 74 61 62 6c 65 20 68 61 73 20 61 0a 2a 2a A table has a.**
17028 20 76 69 72 74 75 61 6c 20 72 6f 6f 74 20 70 61 virtual root pa
17029 67 65 20 77 68 65 6e 20 74 68 65 20 61 63 74 75 ge when the actu
1702a 61 6c 20 72 6f 6f 74 20 70 61 67 65 20 63 6f 6e al root page con
1702b 74 61 69 6e 73 20 6e 6f 20 63 65 6c 6c 73 20 61 tains no cells a
1702c 6e 64 20 61 20 0a 2a 2a 20 73 69 6e 67 6c 65 20 nd a .** single
1702d 63 68 69 6c 64 20 70 61 67 65 2e 20 54 68 69 73 child page. This
1702e 20 63 61 6e 20 6f 6e 6c 79 20 68 61 70 70 65 6e can only happen
1702f 20 77 69 74 68 20 74 68 65 20 74 61 62 6c 65 20 with the table
17030 72 6f 6f 74 65 64 20 61 74 20 70 61 67 65 20 31 rooted at page 1
17031 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 62 ..**.** If the b
17032 2d 74 72 65 65 20 73 74 72 75 63 74 75 72 65 20 -tree structure
17033 69 73 20 65 6d 70 74 79 2c 20 74 68 65 20 63 75 is empty, the cu
17034 72 73 6f 72 20 73 74 61 74 65 20 69 73 20 73 65 rsor state is se
17035 74 20 74 6f 20 0a 2a 2a 20 43 55 52 53 4f 52 5f t to .** CURSOR_
17036 49 4e 56 41 4c 49 44 2e 20 4f 74 68 65 72 77 69 INVALID. Otherwi
17037 73 65 2c 20 74 68 65 20 63 75 72 73 6f 72 20 69 se, the cursor i
17038 73 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 74 s set to point t
17039 6f 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 63 o the first.** c
1703a 65 6c 6c 20 6c 6f 63 61 74 65 64 20 6f 6e 20 74 ell located on t
1703b 68 65 20 72 6f 6f 74 20 28 6f 72 20 76 69 72 74 he root (or virt
1703c 75 61 6c 20 72 6f 6f 74 29 20 70 61 67 65 20 61 ual root) page a
1703d 6e 64 20 74 68 65 20 63 75 72 73 6f 72 20 73 74 nd the cursor st
1703e 61 74 65 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f ate.** is set to
1703f 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 2e 0a 2a CURSOR_VALID..*
17040 2a 0a 2a 2a 20 49 66 20 74 68 69 73 20 66 75 6e *.** If this fun
17041 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 73 75 ction returns su
17042 63 63 65 73 73 66 75 6c 6c 79 2c 20 69 74 20 6d ccessfully, it m
17043 61 79 20 62 65 20 61 73 73 75 6d 65 64 20 74 68 ay be assumed th
17044 61 74 20 74 68 65 0a 2a 2a 20 70 61 67 65 2d 68 at the.** page-h
17045 65 61 64 65 72 20 66 6c 61 67 73 20 69 6e 64 69 eader flags indi
17046 63 61 74 65 20 74 68 61 74 20 74 68 65 20 5b 76 cate that the [v
17047 69 72 74 75 61 6c 5d 20 72 6f 6f 74 2d 70 61 67 irtual] root-pag
17048 65 20 69 73 20 74 68 65 20 65 78 70 65 63 74 65 e is the expecte
17049 64 20 0a 2a 2a 20 6b 69 6e 64 20 6f 66 20 62 2d d .** kind of b-
1704a 74 72 65 65 20 70 61 67 65 20 28 69 2e 65 2e 20 tree page (i.e.
1704b 69 66 20 77 68 65 6e 20 6f 70 65 6e 69 6e 67 20 if when opening
1704c 74 68 65 20 63 75 72 73 6f 72 20 74 68 65 20 63 the cursor the c
1704d 61 6c 6c 65 72 20 64 69 64 20 6e 6f 74 0a 2a 2a aller did not.**
1704e 20 73 70 65 63 69 66 79 20 61 20 4b 65 79 49 6e specify a KeyIn
1704f 66 6f 20 73 74 72 75 63 74 75 72 65 20 74 68 65 fo structure the
17050 20 66 6c 61 67 73 20 62 79 74 65 20 69 73 20 73 flags byte is s
17051 65 74 20 74 6f 20 30 78 30 35 20 6f 72 20 30 78 et to 0x05 or 0x
17052 30 44 2c 0a 2a 2a 20 69 6e 64 69 63 61 74 69 6e 0D,.** indicatin
17053 67 20 61 20 74 61 62 6c 65 20 62 2d 74 72 65 65 g a table b-tree
17054 2c 20 6f 72 20 69 66 20 74 68 65 20 63 61 6c 6c , or if the call
17055 65 72 20 64 69 64 20 73 70 65 63 69 66 79 20 61 er did specify a
17056 20 4b 65 79 49 6e 66 6f 20 0a 2a 2a 20 73 74 72 KeyInfo .** str
17057 75 63 74 75 72 65 20 74 68 65 20 66 6c 61 67 73 ucture the flags
17058 20 62 79 74 65 20 69 73 20 73 65 74 20 74 6f 20 byte is set to
17059 30 78 30 32 20 6f 72 20 30 78 30 41 2c 20 69 6e 0x02 or 0x0A, in
1705a 64 69 63 61 74 69 6e 67 20 61 6e 20 69 6e 64 65 dicating an inde
1705b 78 0a 2a 2a 20 62 2d 74 72 65 65 29 2e 0a 2a 2f x.** b-tree)..*/
1705c 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 6f 76 65 .static int move
1705d 54 6f 52 6f 6f 74 28 42 74 43 75 72 73 6f 72 20 ToRoot(BtCursor
1705e 2a 70 43 75 72 29 7b 0a 20 20 4d 65 6d 50 61 67 *pCur){. MemPag
1705f 65 20 2a 70 52 6f 6f 74 3b 0a 20 20 69 6e 74 20 e *pRoot;. int
17060 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
17061 20 20 42 74 72 65 65 20 2a 70 20 3d 20 70 43 75 Btree *p = pCu
17062 72 2d 3e 70 42 74 72 65 65 3b 0a 20 20 42 74 53 r->pBtree;. BtS
17063 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e hared *pBt = p->
17064 70 42 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 pBt;.. assert(
17065 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 cursorHoldsMutex
17066 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 (pCur) );. asse
17067 72 74 28 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c rt( CURSOR_INVAL
17068 49 44 20 3c 20 43 55 52 53 4f 52 5f 52 45 51 55 ID < CURSOR_REQU
17069 49 52 45 53 45 45 4b 20 29 3b 0a 20 20 61 73 73 IRESEEK );. ass
1706a 65 72 74 28 20 43 55 52 53 4f 52 5f 56 41 4c 49 ert( CURSOR_VALI
1706b 44 20 20 20 3c 20 43 55 52 53 4f 52 5f 52 45 51 D < CURSOR_REQ
1706c 55 49 52 45 53 45 45 4b 20 29 3b 0a 20 20 61 73 UIRESEEK );. as
1706d 73 65 72 74 28 20 43 55 52 53 4f 52 5f 46 41 55 sert( CURSOR_FAU
1706e 4c 54 20 20 20 3e 20 43 55 52 53 4f 52 5f 52 45 LT > CURSOR_RE
1706f 51 55 49 52 45 53 45 45 4b 20 29 3b 0a 20 20 69 QUIRESEEK );. i
17070 66 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3e f( pCur->eState>
17071 3d 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 =CURSOR_REQUIRES
17072 45 45 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 70 EEK ){. if( p
17073 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 Cur->eState==CUR
17074 53 4f 52 5f 46 41 55 4c 54 20 29 7b 0a 20 20 20 SOR_FAULT ){.
17075 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d assert( pCur-
17076 3e 73 6b 69 70 4e 65 78 74 21 3d 53 51 4c 49 54 >skipNext!=SQLIT
17077 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 20 20 72 65 E_OK );. re
17078 74 75 72 6e 20 70 43 75 72 2d 3e 73 6b 69 70 4e turn pCur->skipN
17079 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 ext;. }. s
1707a 71 6c 69 74 65 33 42 74 72 65 65 43 6c 65 61 72 qlite3BtreeClear
1707b 43 75 72 73 6f 72 28 70 43 75 72 29 3b 0a 20 20 Cursor(pCur);.
1707c 7d 0a 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 69 }.. if( pCur->i
1707d 50 61 67 65 3e 3d 30 20 29 7b 0a 20 20 20 20 69 Page>=0 ){. i
1707e 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d nt i;. for(i=
1707f 31 3b 20 69 3c 3d 70 43 75 72 2d 3e 69 50 61 67 1; i<=pCur->iPag
17080 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 72 e; i++){. r
17081 65 6c 65 61 73 65 50 61 67 65 28 70 43 75 72 2d eleasePage(pCur-
17082 3e 61 70 50 61 67 65 5b 69 5d 29 3b 0a 20 20 20 >apPage[i]);.
17083 20 7d 0a 20 20 20 20 70 43 75 72 2d 3e 69 50 61 }. pCur->iPa
17084 67 65 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b ge = 0;. }else{
17085 0a 20 20 20 20 72 63 20 3d 20 67 65 74 41 6e 64 . rc = getAnd
17086 49 6e 69 74 50 61 67 65 28 70 42 74 2c 20 70 43 InitPage(pBt, pC
17087 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 26 70 ur->pgnoRoot, &p
17088 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 29 3b Cur->apPage[0]);
17089 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c . if( rc!=SQL
1708a 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
1708b 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 pCur->eState = C
1708c 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 20 URSOR_INVALID;.
1708d 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
1708e 20 20 20 20 7d 0a 20 20 20 20 70 43 75 72 2d 3e }. pCur->
1708f 69 50 61 67 65 20 3d 20 30 3b 0a 0a 20 20 20 20 iPage = 0;..
17090 2f 2a 20 49 66 20 70 43 75 72 2d 3e 70 4b 65 79 /* If pCur->pKey
17091 49 6e 66 6f 20 69 73 20 6e 6f 74 20 4e 55 4c 4c Info is not NULL
17092 2c 20 74 68 65 6e 20 74 68 65 20 63 61 6c 6c 65 , then the calle
17093 72 20 74 68 61 74 20 6f 70 65 6e 65 64 20 74 68 r that opened th
17094 69 73 20 63 75 72 73 6f 72 0a 20 20 20 20 2a 2a is cursor. **
17095 20 65 78 70 65 63 74 65 64 20 74 6f 20 6f 70 65 expected to ope
17096 6e 20 69 74 20 6f 6e 20 61 6e 20 69 6e 64 65 78 n it on an index
17097 20 62 2d 74 72 65 65 2e 20 4f 74 68 65 72 77 69 b-tree. Otherwi
17098 73 65 2c 20 69 66 20 70 4b 65 79 49 6e 66 6f 20 se, if pKeyInfo
17099 69 73 0a 20 20 20 20 2a 2a 20 4e 55 4c 4c 2c 20 is. ** NULL,
1709a 74 68 65 20 63 61 6c 6c 65 72 20 65 78 70 65 63 the caller expec
1709b 74 73 20 61 20 74 61 62 6c 65 20 62 2d 74 72 65 ts a table b-tre
1709c 65 2e 20 49 66 20 74 68 69 73 20 69 73 20 6e 6f e. If this is no
1709d 74 20 74 68 65 20 63 61 73 65 2c 0a 20 20 20 20 t the case,.
1709e 2a 2a 20 72 65 74 75 72 6e 20 61 6e 20 53 51 4c ** return an SQL
1709f 49 54 45 5f 43 4f 52 52 55 50 54 20 65 72 72 6f ITE_CORRUPT erro
170a0 72 2e 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 r. */. asser
170a1 74 28 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b t( pCur->apPage[
170a2 30 5d 2d 3e 69 6e 74 4b 65 79 3d 3d 31 20 7c 7c 0]->intKey==1 ||
170a3 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d pCur->apPage[0]
170a4 2d 3e 69 6e 74 4b 65 79 3d 3d 30 20 29 3b 0a 20 ->intKey==0 );.
170a5 20 20 20 69 66 28 20 28 70 43 75 72 2d 3e 70 4b if( (pCur->pK
170a6 65 79 49 6e 66 6f 3d 3d 30 29 21 3d 70 43 75 72 eyInfo==0)!=pCur
170a7 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e 69 6e 74 ->apPage[0]->int
170a8 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 72 65 74 Key ){. ret
170a9 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 urn SQLITE_CORRU
170aa 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 20 PT_BKPT;. }.
170ab 20 7d 0a 0a 20 20 2f 2a 20 41 73 73 65 72 74 20 }.. /* Assert
170ac 74 68 61 74 20 74 68 65 20 72 6f 6f 74 20 70 61 that the root pa
170ad 67 65 20 69 73 20 6f 66 20 74 68 65 20 63 6f 72 ge is of the cor
170ae 72 65 63 74 20 74 79 70 65 2e 20 54 68 69 73 20 rect type. This
170af 6d 75 73 74 20 62 65 20 74 68 65 0a 20 20 2a 2a must be the. **
170b0 20 63 61 73 65 20 61 73 20 74 68 65 20 63 61 6c case as the cal
170b1 6c 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 l to this functi
170b2 6f 6e 20 74 68 61 74 20 6c 6f 61 64 65 64 20 74 on that loaded t
170b3 68 65 20 72 6f 6f 74 2d 70 61 67 65 20 28 65 69 he root-page (ei
170b4 74 68 65 72 0a 20 20 2a 2a 20 74 68 69 73 20 63 ther. ** this c
170b5 61 6c 6c 20 6f 72 20 61 20 70 72 65 76 69 6f 75 all or a previou
170b6 73 20 69 6e 76 6f 63 61 74 69 6f 6e 29 20 77 6f s invocation) wo
170b7 75 6c 64 20 68 61 76 65 20 64 65 74 65 63 74 65 uld have detecte
170b8 64 20 63 6f 72 72 75 70 74 69 6f 6e 20 0a 20 20 d corruption .
170b9 2a 2a 20 69 66 20 74 68 65 20 61 73 73 75 6d 70 ** if the assump
170ba 74 69 6f 6e 20 77 65 72 65 20 6e 6f 74 20 74 72 tion were not tr
170bb 75 65 2c 20 61 6e 64 20 69 74 20 69 73 20 6e 6f ue, and it is no
170bc 74 20 70 6f 73 73 69 62 6c 65 20 66 6f 72 20 74 t possible for t
170bd 68 65 20 66 6c 61 67 73 20 0a 20 20 2a 2a 20 62 he flags . ** b
170be 79 74 65 20 74 6f 20 68 61 76 65 20 62 65 65 6e yte to have been
170bf 20 6d 6f 64 69 66 69 65 64 20 77 68 69 6c 65 20 modified while
170c0 74 68 69 73 20 63 75 72 73 6f 72 20 69 73 20 68 this cursor is h
170c1 6f 6c 64 69 6e 67 20 61 20 72 65 66 65 72 65 6e olding a referen
170c2 63 65 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20 70 ce. ** to the p
170c3 61 67 65 2e 20 20 2a 2f 0a 20 20 70 52 6f 6f 74 age. */. pRoot
170c4 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b = pCur->apPage[
170c5 30 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 52 0];. assert( pR
170c6 6f 6f 74 2d 3e 70 67 6e 6f 3d 3d 70 43 75 72 2d oot->pgno==pCur-
170c7 3e 70 67 6e 6f 52 6f 6f 74 20 29 3b 0a 20 20 61 >pgnoRoot );. a
170c8 73 73 65 72 74 28 20 70 52 6f 6f 74 2d 3e 69 73 ssert( pRoot->is
170c9 49 6e 69 74 20 26 26 20 28 70 43 75 72 2d 3e 70 Init && (pCur->p
170ca 4b 65 79 49 6e 66 6f 3d 3d 30 29 3d 3d 70 52 6f KeyInfo==0)==pRo
170cb 6f 74 2d 3e 69 6e 74 4b 65 79 20 29 3b 0a 0a 20 ot->intKey );..
170cc 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 30 5d 20 pCur->aiIdx[0]
170cd 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e 69 6e 66 = 0;. pCur->inf
170ce 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 70 o.nSize = 0;. p
170cf 43 75 72 2d 3e 61 74 4c 61 73 74 20 3d 20 30 3b Cur->atLast = 0;
170d0 0a 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b . pCur->validNK
170d1 65 79 20 3d 20 30 3b 0a 0a 20 20 69 66 28 20 70 ey = 0;.. if( p
170d2 52 6f 6f 74 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 26 Root->nCell==0 &
170d3 26 20 21 70 52 6f 6f 74 2d 3e 6c 65 61 66 20 29 & !pRoot->leaf )
170d4 7b 0a 20 20 20 20 50 67 6e 6f 20 73 75 62 70 61 {. Pgno subpa
170d5 67 65 3b 0a 20 20 20 20 69 66 28 20 70 52 6f 6f ge;. if( pRoo
170d6 74 2d 3e 70 67 6e 6f 21 3d 31 20 29 20 72 65 74 t->pgno!=1 ) ret
170d7 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 urn SQLITE_CORRU
170d8 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 73 75 62 PT_BKPT;. sub
170d9 70 61 67 65 20 3d 20 67 65 74 34 62 79 74 65 28 page = get4byte(
170da 26 70 52 6f 6f 74 2d 3e 61 44 61 74 61 5b 70 52 &pRoot->aData[pR
170db 6f 6f 74 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 oot->hdrOffset+8
170dc 5d 29 3b 0a 20 20 20 20 70 43 75 72 2d 3e 65 53 ]);. pCur->eS
170dd 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 56 41 tate = CURSOR_VA
170de 4c 49 44 3b 0a 20 20 20 20 72 63 20 3d 20 6d 6f LID;. rc = mo
170df 76 65 54 6f 43 68 69 6c 64 28 70 43 75 72 2c 20 veToChild(pCur,
170e0 73 75 62 70 61 67 65 29 3b 0a 20 20 7d 65 6c 73 subpage);. }els
170e1 65 7b 0a 20 20 20 20 70 43 75 72 2d 3e 65 53 74 e{. pCur->eSt
170e2 61 74 65 20 3d 20 28 28 70 52 6f 6f 74 2d 3e 6e ate = ((pRoot->n
170e3 43 65 6c 6c 3e 30 29 3f 43 55 52 53 4f 52 5f 56 Cell>0)?CURSOR_V
170e4 41 4c 49 44 3a 43 55 52 53 4f 52 5f 49 4e 56 41 ALID:CURSOR_INVA
170e5 4c 49 44 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 LID);. }. retu
170e6 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
170e7 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f 72 20 Move the cursor
170e8 64 6f 77 6e 20 74 6f 20 74 68 65 20 6c 65 66 74 down to the left
170e9 2d 6d 6f 73 74 20 6c 65 61 66 20 65 6e 74 72 79 -most leaf entry
170ea 20 62 65 6e 65 61 74 68 20 74 68 65 0a 2a 2a 20 beneath the.**
170eb 65 6e 74 72 79 20 74 6f 20 77 68 69 63 68 20 69 entry to which i
170ec 74 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70 t is currently p
170ed 6f 69 6e 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 ointing..**.** T
170ee 68 65 20 6c 65 66 74 2d 6d 6f 73 74 20 6c 65 61 he left-most lea
170ef 66 20 69 73 20 74 68 65 20 6f 6e 65 20 77 69 74 f is the one wit
170f0 68 20 74 68 65 20 73 6d 61 6c 6c 65 73 74 20 6b h the smallest k
170f1 65 79 20 2d 20 74 68 65 20 66 69 72 73 74 0a 2a ey - the first.*
170f2 2a 20 69 6e 20 61 73 63 65 6e 64 69 6e 67 20 6f * in ascending o
170f3 72 64 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 rder..*/.static
170f4 69 6e 74 20 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f int moveToLeftmo
170f5 73 74 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 st(BtCursor *pCu
170f6 72 29 7b 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 3b r){. Pgno pgno;
170f7 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
170f8 54 45 5f 4f 4b 3b 0a 20 20 4d 65 6d 50 61 67 65 TE_OK;. MemPage
170f9 20 2a 70 50 61 67 65 3b 0a 0a 20 20 61 73 73 65 *pPage;.. asse
170fa 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d rt( cursorHoldsM
170fb 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 utex(pCur) );.
170fc 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 assert( pCur->eS
170fd 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c tate==CURSOR_VAL
170fe 49 44 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 72 ID );. while( r
170ff 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c==SQLITE_OK &&
17100 21 28 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e !(pPage = pCur->
17101 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 apPage[pCur->iPa
17102 67 65 5d 29 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 ge])->leaf ){.
17103 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e assert( pCur->
17104 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 aiIdx[pCur->iPag
17105 65 5d 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 e]<pPage->nCell
17106 29 3b 0a 20 20 20 20 70 67 6e 6f 20 3d 20 67 65 );. pgno = ge
17107 74 34 62 79 74 65 28 66 69 6e 64 43 65 6c 6c 28 t4byte(findCell(
17108 70 50 61 67 65 2c 20 70 43 75 72 2d 3e 61 69 49 pPage, pCur->aiI
17109 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 29 dx[pCur->iPage])
1710a 29 3b 0a 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 );. rc = move
1710b 54 6f 43 68 69 6c 64 28 70 43 75 72 2c 20 70 67 ToChild(pCur, pg
1710c 6e 6f 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 no);. }. retur
1710d 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d n rc;.}../*.** M
1710e 6f 76 65 20 74 68 65 20 63 75 72 73 6f 72 20 64 ove the cursor d
1710f 6f 77 6e 20 74 6f 20 74 68 65 20 72 69 67 68 74 own to the right
17110 2d 6d 6f 73 74 20 6c 65 61 66 20 65 6e 74 72 79 -most leaf entry
17111 20 62 65 6e 65 61 74 68 20 74 68 65 0a 2a 2a 20 beneath the.**
17112 70 61 67 65 20 74 6f 20 77 68 69 63 68 20 69 74 page to which it
17113 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70 6f is currently po
17114 69 6e 74 69 6e 67 2e 20 20 4e 6f 74 69 63 65 20 inting. Notice
17115 74 68 65 20 64 69 66 66 65 72 65 6e 63 65 0a 2a the difference.*
17116 2a 20 62 65 74 77 65 65 6e 20 6d 6f 76 65 54 6f * between moveTo
17117 4c 65 66 74 6d 6f 73 74 28 29 20 61 6e 64 20 6d Leftmost() and m
17118 6f 76 65 54 6f 52 69 67 68 74 6d 6f 73 74 28 29 oveToRightmost()
17119 2e 20 20 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f 73 . moveToLeftmos
1711a 74 28 29 0a 2a 2a 20 66 69 6e 64 73 20 74 68 65 t().** finds the
1711b 20 6c 65 66 74 2d 6d 6f 73 74 20 65 6e 74 72 79 left-most entry
1711c 20 62 65 6e 65 61 74 68 20 74 68 65 20 2a 65 6e beneath the *en
1711d 74 72 79 2a 20 77 68 65 72 65 61 73 20 6d 6f 76 try* whereas mov
1711e 65 54 6f 52 69 67 68 74 6d 6f 73 74 28 29 0a 2a eToRightmost().*
1711f 2a 20 66 69 6e 64 73 20 74 68 65 20 72 69 67 68 * finds the righ
17120 74 2d 6d 6f 73 74 20 65 6e 74 72 79 20 62 65 6e t-most entry ben
17121 65 61 74 68 20 74 68 65 20 2a 70 61 67 65 2a 2e eath the *page*.
17122 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 69 67 68 74 .**.** The right
17123 2d 6d 6f 73 74 20 65 6e 74 72 79 20 69 73 20 74 -most entry is t
17124 68 65 20 6f 6e 65 20 77 69 74 68 20 74 68 65 20 he one with the
17125 6c 61 72 67 65 73 74 20 6b 65 79 20 2d 20 74 68 largest key - th
17126 65 20 6c 61 73 74 0a 2a 2a 20 6b 65 79 20 69 6e e last.** key in
17127 20 61 73 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 ascending order
17128 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
17129 6d 6f 76 65 54 6f 52 69 67 68 74 6d 6f 73 74 28 moveToRightmost(
1712a 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b BtCursor *pCur){
1712b 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 3b 0a 20 20 . Pgno pgno;.
1712c 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
1712d 4f 4b 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 OK;. MemPage *p
1712e 50 61 67 65 20 3d 20 30 3b 0a 0a 20 20 61 73 73 Page = 0;.. ass
1712f 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 ert( cursorHolds
17130 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 Mutex(pCur) );.
17131 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 assert( pCur->e
17132 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 State==CURSOR_VA
17133 4c 49 44 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 LID );. while(
17134 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
17135 20 21 28 70 50 61 67 65 20 3d 20 70 43 75 72 2d !(pPage = pCur-
17136 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 >apPage[pCur->iP
17137 61 67 65 5d 29 2d 3e 6c 65 61 66 20 29 7b 0a 20 age])->leaf ){.
17138 20 20 20 70 67 6e 6f 20 3d 20 67 65 74 34 62 79 pgno = get4by
17139 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61 te(&pPage->aData
1713a 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 [pPage->hdrOffse
1713b 74 2b 38 5d 29 3b 0a 20 20 20 20 70 43 75 72 2d t+8]);. pCur-
1713c 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 >aiIdx[pCur->iPa
1713d 67 65 5d 20 3d 20 70 50 61 67 65 2d 3e 6e 43 65 ge] = pPage->nCe
1713e 6c 6c 3b 0a 20 20 20 20 72 63 20 3d 20 6d 6f 76 ll;. rc = mov
1713f 65 54 6f 43 68 69 6c 64 28 70 43 75 72 2c 20 70 eToChild(pCur, p
17140 67 6e 6f 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 gno);. }. if(
17141 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
17142 0a 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64 78 . pCur->aiIdx
17143 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 3d 20 [pCur->iPage] =
17144 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 2d 31 3b 0a pPage->nCell-1;.
17145 20 20 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e pCur->info.n
17146 53 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20 70 43 Size = 0;. pC
17147 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 ur->validNKey =
17148 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 0;. }. return
17149 72 63 3b 0a 7d 0a 0a 2f 2a 20 4d 6f 76 65 20 74 rc;.}../* Move t
1714a 68 65 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 he cursor to the
1714b 20 66 69 72 73 74 20 65 6e 74 72 79 20 69 6e 20 first entry in
1714c 74 68 65 20 74 61 62 6c 65 2e 20 20 52 65 74 75 the table. Retu
1714d 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 rn SQLITE_OK.**
1714e 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 53 65 74 on success. Set
1714f 20 2a 70 52 65 73 20 74 6f 20 30 20 69 66 20 74 *pRes to 0 if t
17150 68 65 20 63 75 72 73 6f 72 20 61 63 74 75 61 6c he cursor actual
17151 6c 79 20 70 6f 69 6e 74 73 20 74 6f 20 73 6f 6d ly points to som
17152 65 74 68 69 6e 67 0a 2a 2a 20 6f 72 20 73 65 74 ething.** or set
17153 20 2a 70 52 65 73 20 74 6f 20 31 20 69 66 20 74 *pRes to 1 if t
17154 68 65 20 74 61 62 6c 65 20 69 73 20 65 6d 70 74 he table is empt
17155 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 y..*/.SQLITE_PRI
17156 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
17157 42 74 72 65 65 46 69 72 73 74 28 42 74 43 75 72 BtreeFirst(BtCur
17158 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a sor *pCur, int *
17159 70 52 65 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b pRes){. int rc;
1715a 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 .. assert( curs
1715b 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 orHoldsMutex(pCu
1715c 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 r) );. assert(
1715d 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
1715e 6c 64 28 70 43 75 72 2d 3e 70 42 74 72 65 65 2d ld(pCur->pBtree-
1715f 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 >db->mutex) );.
17160 20 72 63 20 3d 20 6d 6f 76 65 54 6f 52 6f 6f 74 rc = moveToRoot
17161 28 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72 63 (pCur);. if( rc
17162 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
17163 20 20 20 69 66 28 20 70 43 75 72 2d 3e 65 53 74 if( pCur->eSt
17164 61 74 65 3d 3d 43 55 52 53 4f 52 5f 49 4e 56 41 ate==CURSOR_INVA
17165 4c 49 44 20 29 7b 0a 20 20 20 20 20 20 61 73 73 LID ){. ass
17166 65 72 74 28 20 70 43 75 72 2d 3e 61 70 50 61 67 ert( pCur->apPag
17167 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e e[pCur->iPage]->
17168 6e 43 65 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 nCell==0 );.
17169 20 20 2a 70 52 65 73 20 3d 20 31 3b 0a 20 20 20 *pRes = 1;.
1716a 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f rc = SQLITE_O
1716b 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 K;. }else{.
1716c 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 assert( pCur
1716d 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 ->apPage[pCur->i
1716e 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 3e 30 20 29 Page]->nCell>0 )
1716f 3b 0a 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 ;. *pRes =
17170 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 6d 6f 0;. rc = mo
17171 76 65 54 6f 4c 65 66 74 6d 6f 73 74 28 70 43 75 veToLeftmost(pCu
17172 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 r);. }. }.
17173 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
17174 20 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f 72 Move the cursor
17175 20 74 6f 20 74 68 65 20 6c 61 73 74 20 65 6e 74 to the last ent
17176 72 79 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e ry in the table.
17177 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f Return SQLITE_
17178 4f 4b 0a 2a 2a 20 6f 6e 20 73 75 63 63 65 73 73 OK.** on success
17179 2e 20 20 53 65 74 20 2a 70 52 65 73 20 74 6f 20 . Set *pRes to
1717a 30 20 69 66 20 74 68 65 20 63 75 72 73 6f 72 20 0 if the cursor
1717b 61 63 74 75 61 6c 6c 79 20 70 6f 69 6e 74 73 20 actually points
1717c 74 6f 20 73 6f 6d 65 74 68 69 6e 67 0a 2a 2a 20 to something.**
1717d 6f 72 20 73 65 74 20 2a 70 52 65 73 20 74 6f 20 or set *pRes to
1717e 31 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 69 1 if the table i
1717f 73 20 65 6d 70 74 79 2e 0a 2a 2f 0a 53 51 4c 49 s empty..*/.SQLI
17180 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
17181 71 6c 69 74 65 33 42 74 72 65 65 4c 61 73 74 28 qlite3BtreeLast(
17182 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 BtCursor *pCur,
17183 69 6e 74 20 2a 70 52 65 73 29 7b 0a 20 20 69 6e int *pRes){. in
17184 74 20 72 63 3b 0a 20 0a 20 20 61 73 73 65 72 74 t rc;. . assert
17185 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 ( cursorHoldsMut
17186 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 ex(pCur) );. as
17187 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
17188 74 65 78 5f 68 65 6c 64 28 70 43 75 72 2d 3e 70 tex_held(pCur->p
17189 42 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 Btree->db->mutex
1718a 29 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 ) );.. /* If th
1718b 65 20 63 75 72 73 6f 72 20 61 6c 72 65 61 64 79 e cursor already
1718c 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 6c points to the l
1718d 61 73 74 20 65 6e 74 72 79 2c 20 74 68 69 73 20 ast entry, this
1718e 69 73 20 61 20 6e 6f 2d 6f 70 2e 20 2a 2f 0a 20 is a no-op. */.
1718f 20 69 66 28 20 43 55 52 53 4f 52 5f 56 41 4c 49 if( CURSOR_VALI
17190 44 3d 3d 70 43 75 72 2d 3e 65 53 74 61 74 65 20 D==pCur->eState
17191 26 26 20 70 43 75 72 2d 3e 61 74 4c 61 73 74 20 && pCur->atLast
17192 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 ){.#ifdef SQLITE
17193 5f 44 45 42 55 47 0a 20 20 20 20 2f 2a 20 54 68 _DEBUG. /* Th
17194 69 73 20 62 6c 6f 63 6b 20 73 65 72 76 65 73 20 is block serves
17195 74 6f 20 61 73 73 65 72 74 28 29 20 74 68 61 74 to assert() that
17196 20 74 68 65 20 63 75 72 73 6f 72 20 72 65 61 6c the cursor real
17197 6c 79 20 64 6f 65 73 20 70 6f 69 6e 74 20 0a 20 ly does point .
17198 20 20 20 2a 2a 20 74 6f 20 74 68 65 20 6c 61 73 ** to the las
17199 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 62 t entry in the b
1719a 2d 74 72 65 65 2e 20 2a 2f 0a 20 20 20 20 69 6e -tree. */. in
1719b 74 20 69 69 3b 0a 20 20 20 20 66 6f 72 28 69 69 t ii;. for(ii
1719c 3d 30 3b 20 69 69 3c 70 43 75 72 2d 3e 69 50 61 =0; ii<pCur->iPa
1719d 67 65 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20 ge; ii++){.
1719e 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 assert( pCur->a
1719f 69 49 64 78 5b 69 69 5d 3d 3d 70 43 75 72 2d 3e iIdx[ii]==pCur->
171a0 61 70 50 61 67 65 5b 69 69 5d 2d 3e 6e 43 65 6c apPage[ii]->nCel
171a1 6c 20 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 l );. }. a
171a2 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 69 49 ssert( pCur->aiI
171a3 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3d dx[pCur->iPage]=
171a4 3d 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 =pCur->apPage[pC
171a5 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c ur->iPage]->nCel
171a6 6c 2d 31 20 29 3b 0a 20 20 20 20 61 73 73 65 72 l-1 );. asser
171a7 74 28 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b t( pCur->apPage[
171a8 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6c 65 pCur->iPage]->le
171a9 61 66 20 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 af );.#endif.
171aa 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
171ab 4b 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 6d K;. }.. rc = m
171ac 6f 76 65 54 6f 52 6f 6f 74 28 70 43 75 72 29 3b oveToRoot(pCur);
171ad 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
171ae 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 E_OK ){. if(
171af 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3d 3d CURSOR_INVALID==
171b0 70 43 75 72 2d 3e 65 53 74 61 74 65 20 29 7b 0a pCur->eState ){.
171b1 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 43 assert( pC
171b2 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d ur->apPage[pCur-
171b3 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 3d 3d >iPage]->nCell==
171b4 30 20 29 3b 0a 20 20 20 20 20 20 2a 70 52 65 73 0 );. *pRes
171b5 20 3d 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b = 1;. }else{
171b6 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
171b7 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 Cur->eState==CUR
171b8 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 20 SOR_VALID );.
171b9 20 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 *pRes = 0;.
171ba 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 52 rc = moveToR
171bb 69 67 68 74 6d 6f 73 74 28 70 43 75 72 29 3b 0a ightmost(pCur);.
171bc 20 20 20 20 20 20 70 43 75 72 2d 3e 61 74 4c 61 pCur->atLa
171bd 73 74 20 3d 20 72 63 3d 3d 53 51 4c 49 54 45 5f st = rc==SQLITE_
171be 4f 4b 20 3f 31 3a 30 3b 0a 20 20 20 20 7d 0a 20 OK ?1:0;. }.
171bf 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
171c0 7d 0a 0a 2f 2a 20 4d 6f 76 65 20 74 68 65 20 63 }../* Move the c
171c1 75 72 73 6f 72 20 73 6f 20 74 68 61 74 20 69 74 ursor so that it
171c2 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e 20 65 6e points to an en
171c3 74 72 79 20 6e 65 61 72 20 74 68 65 20 6b 65 79 try near the key
171c4 20 0a 2a 2a 20 73 70 65 63 69 66 69 65 64 20 62 .** specified b
171c5 79 20 70 49 64 78 4b 65 79 20 6f 72 20 69 6e 74 y pIdxKey or int
171c6 4b 65 79 2e 20 20 20 52 65 74 75 72 6e 20 61 20 Key. Return a
171c7 73 75 63 63 65 73 73 20 63 6f 64 65 2e 0a 2a 2a success code..**
171c8 0a 2a 2a 20 46 6f 72 20 49 4e 54 4b 45 59 20 74 .** For INTKEY t
171c9 61 62 6c 65 73 2c 20 74 68 65 20 69 6e 74 4b 65 ables, the intKe
171ca 79 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 75 y parameter is u
171cb 73 65 64 2e 20 20 70 49 64 78 4b 65 79 20 0a 2a sed. pIdxKey .*
171cc 2a 20 6d 75 73 74 20 62 65 20 4e 55 4c 4c 2e 20 * must be NULL.
171cd 20 46 6f 72 20 69 6e 64 65 78 20 74 61 62 6c 65 For index table
171ce 73 2c 20 70 49 64 78 4b 65 79 20 69 73 20 75 73 s, pIdxKey is us
171cf 65 64 20 61 6e 64 20 69 6e 74 4b 65 79 0a 2a 2a ed and intKey.**
171d0 20 69 73 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a 0a is ignored..**.
171d1 2a 2a 20 49 66 20 61 6e 20 65 78 61 63 74 20 6d ** If an exact m
171d2 61 74 63 68 20 69 73 20 6e 6f 74 20 66 6f 75 6e atch is not foun
171d3 64 2c 20 74 68 65 6e 20 74 68 65 20 63 75 72 73 d, then the curs
171d4 6f 72 20 69 73 20 61 6c 77 61 79 73 0a 2a 2a 20 or is always.**
171d5 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 left pointing at
171d6 20 61 20 6c 65 61 66 20 70 61 67 65 20 77 68 69 a leaf page whi
171d7 63 68 20 77 6f 75 6c 64 20 68 6f 6c 64 20 74 68 ch would hold th
171d8 65 20 65 6e 74 72 79 20 69 66 20 69 74 0a 2a 2a e entry if it.**
171d9 20 77 65 72 65 20 70 72 65 73 65 6e 74 2e 20 20 were present.
171da 54 68 65 20 63 75 72 73 6f 72 20 6d 69 67 68 74 The cursor might
171db 20 70 6f 69 6e 74 20 74 6f 20 61 6e 20 65 6e 74 point to an ent
171dc 72 79 20 74 68 61 74 20 63 6f 6d 65 73 0a 2a 2a ry that comes.**
171dd 20 62 65 66 6f 72 65 20 6f 72 20 61 66 74 65 72 before or after
171de 20 74 68 65 20 6b 65 79 2e 0a 2a 2a 0a 2a 2a 20 the key..**.**
171df 41 6e 20 69 6e 74 65 67 65 72 20 69 73 20 77 72 An integer is wr
171e0 69 74 74 65 6e 20 69 6e 74 6f 20 2a 70 52 65 73 itten into *pRes
171e1 20 77 68 69 63 68 20 69 73 20 74 68 65 20 72 65 which is the re
171e2 73 75 6c 74 20 6f 66 0a 2a 2a 20 63 6f 6d 70 61 sult of.** compa
171e3 72 69 6e 67 20 74 68 65 20 6b 65 79 20 77 69 74 ring the key wit
171e4 68 20 74 68 65 20 65 6e 74 72 79 20 74 6f 20 77 h the entry to w
171e5 68 69 63 68 20 74 68 65 20 63 75 72 73 6f 72 20 hich the cursor
171e6 69 73 20 0a 2a 2a 20 70 6f 69 6e 74 69 6e 67 2e is .** pointing.
171e7 20 20 54 68 65 20 6d 65 61 6e 69 6e 67 20 6f 66 The meaning of
171e8 20 74 68 65 20 69 6e 74 65 67 65 72 20 77 72 69 the integer wri
171e9 74 74 65 6e 20 69 6e 74 6f 0a 2a 2a 20 2a 70 52 tten into.** *pR
171ea 65 73 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 es is as follows
171eb 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 70 52 65 :.**.** *pRe
171ec 73 3c 30 20 20 20 20 20 20 54 68 65 20 63 75 72 s<0 The cur
171ed 73 6f 72 20 69 73 20 6c 65 66 74 20 70 6f 69 6e sor is left poin
171ee 74 69 6e 67 20 61 74 20 61 6e 20 65 6e 74 72 79 ting at an entry
171ef 20 74 68 61 74 0a 2a 2a 20 20 20 20 20 20 20 20 that.**
171f0 20 20 20 20 20 20 20 20 20 20 69 73 20 73 6d 61 is sma
171f1 6c 6c 65 72 20 74 68 61 6e 20 69 6e 74 4b 65 79 ller than intKey
171f2 2f 70 49 64 78 4b 65 79 20 6f 72 20 69 66 20 74 /pIdxKey or if t
171f3 68 65 20 74 61 62 6c 65 20 69 73 20 65 6d 70 74 he table is empt
171f4 79 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 y.**
171f5 20 20 20 20 20 20 61 6e 64 20 74 68 65 20 63 75 and the cu
171f6 72 73 6f 72 20 69 73 20 74 68 65 72 65 66 6f 72 rsor is therefor
171f7 65 20 6c 65 66 74 20 70 6f 69 6e 74 20 74 6f 20 e left point to
171f8 6e 6f 74 68 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 20 nothing..**.**
171f9 20 20 20 2a 70 52 65 73 3d 3d 30 20 20 20 20 20 *pRes==0
171fa 54 68 65 20 63 75 72 73 6f 72 20 69 73 20 6c 65 The cursor is le
171fb 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 61 ft pointing at a
171fc 6e 20 65 6e 74 72 79 20 74 68 61 74 0a 2a 2a 20 n entry that.**
171fd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
171fe 20 65 78 61 63 74 6c 79 20 6d 61 74 63 68 65 73 exactly matches
171ff 20 69 6e 74 4b 65 79 2f 70 49 64 78 4b 65 79 2e intKey/pIdxKey.
17200 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 70 52 65 73 .**.** *pRes
17201 3e 30 20 20 20 20 20 20 54 68 65 20 63 75 72 73 >0 The curs
17202 6f 72 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 or is left point
17203 69 6e 67 20 61 74 20 61 6e 20 65 6e 74 72 79 20 ing at an entry
17204 74 68 61 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 that.**
17205 20 20 20 20 20 20 20 20 20 69 73 20 6c 61 72 67 is larg
17206 65 72 20 74 68 61 6e 20 69 6e 74 4b 65 79 2f 70 er than intKey/p
17207 49 64 78 4b 65 79 2e 0a 2a 2a 0a 2a 2f 0a 53 51 IdxKey..**.*/.SQ
17208 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
17209 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 sqlite3BtreeMov
1720a 65 74 6f 55 6e 70 61 63 6b 65 64 28 0a 20 20 42 etoUnpacked(. B
1720b 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 20 tCursor *pCur,
1720c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 /* The c
1720d 75 72 73 6f 72 20 74 6f 20 62 65 20 6d 6f 76 65 ursor to be move
1720e 64 20 2a 2f 0a 20 20 55 6e 70 61 63 6b 65 64 52 d */. UnpackedR
1720f 65 63 6f 72 64 20 2a 70 49 64 78 4b 65 79 2c 20 ecord *pIdxKey,
17210 2f 2a 20 55 6e 70 61 63 6b 65 64 20 69 6e 64 65 /* Unpacked inde
17211 78 20 6b 65 79 20 2a 2f 0a 20 20 69 36 34 20 69 x key */. i64 i
17212 6e 74 4b 65 79 2c 20 20 20 20 20 20 20 20 20 20 ntKey,
17213 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 /* The table
17214 20 6b 65 79 20 2a 2f 0a 20 20 69 6e 74 20 62 69 key */. int bi
17215 61 73 52 69 67 68 74 2c 20 20 20 20 20 20 20 20 asRight,
17216 20 20 20 2f 2a 20 49 66 20 74 72 75 65 2c 20 62 /* If true, b
17217 69 61 73 20 74 68 65 20 73 65 61 72 63 68 20 74 ias the search t
17218 6f 20 74 68 65 20 68 69 67 68 20 65 6e 64 20 2a o the high end *
17219 2f 0a 20 20 69 6e 74 20 2a 70 52 65 73 20 20 20 /. int *pRes
1721a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1721b 57 72 69 74 65 20 73 65 61 72 63 68 20 72 65 73 Write search res
1721c 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a 29 7b 0a ults here */.){.
1721d 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 int rc;.. ass
1721e 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 ert( cursorHolds
1721f 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 Mutex(pCur) );.
17220 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
17221 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 75 72 _mutex_held(pCur
17222 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 ->pBtree->db->mu
17223 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 tex) );. assert
17224 28 20 70 52 65 73 20 29 3b 0a 20 20 61 73 73 65 ( pRes );. asse
17225 72 74 28 20 28 70 49 64 78 4b 65 79 3d 3d 30 29 rt( (pIdxKey==0)
17226 3d 3d 28 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 ==(pCur->pKeyInf
17227 6f 3d 3d 30 29 20 29 3b 0a 0a 20 20 2f 2a 20 49 o==0) );.. /* I
17228 66 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 f the cursor is
17229 61 6c 72 65 61 64 79 20 70 6f 73 69 74 69 6f 6e already position
1722a 65 64 20 61 74 20 74 68 65 20 70 6f 69 6e 74 20 ed at the point
1722b 77 65 20 61 72 65 20 74 72 79 69 6e 67 0a 20 20 we are trying.
1722c 2a 2a 20 74 6f 20 6d 6f 76 65 20 74 6f 2c 20 74 ** to move to, t
1722d 68 65 6e 20 6a 75 73 74 20 72 65 74 75 72 6e 20 hen just return
1722e 77 69 74 68 6f 75 74 20 64 6f 69 6e 67 20 61 6e without doing an
1722f 79 20 77 6f 72 6b 20 2a 2f 0a 20 20 69 66 28 20 y work */. if(
17230 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 pCur->eState==CU
17231 52 53 4f 52 5f 56 41 4c 49 44 20 26 26 20 70 43 RSOR_VALID && pC
17232 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 0a 20 ur->validNKey .
17233 20 20 26 26 20 70 43 75 72 2d 3e 61 70 50 61 67 && pCur->apPag
17234 65 5b 30 5d 2d 3e 69 6e 74 4b 65 79 20 0a 20 20 e[0]->intKey .
17235 29 7b 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d ){. if( pCur-
17236 3e 69 6e 66 6f 2e 6e 4b 65 79 3d 3d 69 6e 74 4b >info.nKey==intK
17237 65 79 20 29 7b 0a 20 20 20 20 20 20 2a 70 52 65 ey ){. *pRe
17238 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 65 74 s = 0;. ret
17239 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
1723a 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 43 75 }. if( pCu
1723b 72 2d 3e 61 74 4c 61 73 74 20 26 26 20 70 43 75 r->atLast && pCu
1723c 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 3c 69 6e 74 r->info.nKey<int
1723d 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 2a 70 52 Key ){. *pR
1723e 65 73 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 72 es = -1;. r
1723f 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
17240 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 63 . }. }.. rc
17241 20 3d 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 70 43 = moveToRoot(pC
17242 75 72 29 3b 0a 20 20 69 66 28 20 72 63 20 29 7b ur);. if( rc ){
17243 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a . return rc;.
17244 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 43 }. assert( pC
17245 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d ur->apPage[pCur-
17246 3e 69 50 61 67 65 5d 20 29 3b 0a 20 20 61 73 73 >iPage] );. ass
17247 65 72 74 28 20 70 43 75 72 2d 3e 61 70 50 61 67 ert( pCur->apPag
17248 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e e[pCur->iPage]->
17249 69 73 49 6e 69 74 20 29 3b 0a 20 20 61 73 73 65 isInit );. asse
1724a 72 74 28 20 70 43 75 72 2d 3e 61 70 50 61 67 65 rt( pCur->apPage
1724b 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e [pCur->iPage]->n
1724c 43 65 6c 6c 3e 30 20 7c 7c 20 70 43 75 72 2d 3e Cell>0 || pCur->
1724d 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 49 eState==CURSOR_I
1724e 4e 56 41 4c 49 44 20 29 3b 0a 20 20 69 66 28 20 NVALID );. if(
1724f 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 pCur->eState==CU
17250 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 29 7b 0a RSOR_INVALID ){.
17251 20 20 20 20 2a 70 52 65 73 20 3d 20 2d 31 3b 0a *pRes = -1;.
17252 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 assert( pCur
17253 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 ->apPage[pCur->i
17254 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 Page]->nCell==0
17255 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 );. return SQ
17256 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 61 LITE_OK;. }. a
17257 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 70 50 ssert( pCur->apP
17258 61 67 65 5b 30 5d 2d 3e 69 6e 74 4b 65 79 20 7c age[0]->intKey |
17259 7c 20 70 49 64 78 4b 65 79 20 29 3b 0a 20 20 66 | pIdxKey );. f
1725a 6f 72 28 3b 3b 29 7b 0a 20 20 20 20 69 6e 74 20 or(;;){. int
1725b 6c 77 72 2c 20 75 70 72 3b 0a 20 20 20 20 50 67 lwr, upr;. Pg
1725c 6e 6f 20 63 68 6c 64 50 67 3b 0a 20 20 20 20 4d no chldPg;. M
1725d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 20 3d 20 emPage *pPage =
1725e 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 pCur->apPage[pCu
1725f 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 20 20 69 r->iPage];. i
17260 6e 74 20 63 3b 0a 0a 20 20 20 20 2f 2a 20 70 50 nt c;.. /* pP
17261 61 67 65 2d 3e 6e 43 65 6c 6c 20 6d 75 73 74 20 age->nCell must
17262 62 65 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 be greater than
17263 7a 65 72 6f 2e 20 49 66 20 74 68 69 73 20 69 73 zero. If this is
17264 20 74 68 65 20 72 6f 6f 74 2d 70 61 67 65 0a 20 the root-page.
17265 20 20 20 2a 2a 20 74 68 65 20 63 75 72 73 6f 72 ** the cursor
17266 20 77 6f 75 6c 64 20 68 61 76 65 20 62 65 65 6e would have been
17267 20 49 4e 56 41 4c 49 44 20 61 62 6f 76 65 20 61 INVALID above a
17268 6e 64 20 74 68 69 73 20 66 6f 72 28 3b 3b 29 20 nd this for(;;)
17269 6c 6f 6f 70 0a 20 20 20 20 2a 2a 20 6e 6f 74 20 loop. ** not
1726a 72 75 6e 2e 20 49 66 20 74 68 69 73 20 69 73 20 run. If this is
1726b 6e 6f 74 20 74 68 65 20 72 6f 6f 74 2d 70 61 67 not the root-pag
1726c 65 2c 20 74 68 65 6e 20 74 68 65 20 6d 6f 76 65 e, then the move
1726d 54 6f 43 68 69 6c 64 28 29 20 72 6f 75 74 69 6e ToChild() routin
1726e 65 0a 20 20 20 20 2a 2a 20 77 6f 75 6c 64 20 68 e. ** would h
1726f 61 76 65 20 61 6c 72 65 61 64 79 20 64 65 74 65 ave already dete
17270 63 74 65 64 20 64 62 20 63 6f 72 72 75 70 74 69 cted db corrupti
17271 6f 6e 2e 20 53 69 6d 69 6c 61 72 6c 79 2c 20 70 on. Similarly, p
17272 50 61 67 65 20 6d 75 73 74 0a 20 20 20 20 2a 2a Page must. **
17273 20 62 65 20 74 68 65 20 72 69 67 68 74 20 6b 69 be the right ki
17274 6e 64 20 28 69 6e 64 65 78 20 6f 72 20 74 61 62 nd (index or tab
17275 6c 65 29 20 6f 66 20 62 2d 74 72 65 65 20 70 61 le) of b-tree pa
17276 67 65 2e 20 4f 74 68 65 72 77 69 73 65 0a 20 20 ge. Otherwise.
17277 20 20 2a 2a 20 61 20 6d 6f 76 65 54 6f 43 68 69 ** a moveToChi
17278 6c 64 28 29 20 6f 72 20 6d 6f 76 65 54 6f 52 6f ld() or moveToRo
17279 6f 74 28 29 20 63 61 6c 6c 20 77 6f 75 6c 64 20 ot() call would
1727a 68 61 76 65 20 64 65 74 65 63 74 65 64 20 63 6f have detected co
1727b 72 72 75 70 74 69 6f 6e 2e 20 20 2a 2f 0a 20 20 rruption. */.
1727c 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d assert( pPage-
1727d 3e 6e 43 65 6c 6c 3e 30 20 29 3b 0a 20 20 20 20 >nCell>0 );.
1727e 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69 assert( pPage->i
1727f 6e 74 4b 65 79 3d 3d 28 70 49 64 78 4b 65 79 3d ntKey==(pIdxKey=
17280 3d 30 29 20 29 3b 0a 20 20 20 20 6c 77 72 20 3d =0) );. lwr =
17281 20 30 3b 0a 20 20 20 20 75 70 72 20 3d 20 70 50 0;. upr = pP
17282 61 67 65 2d 3e 6e 43 65 6c 6c 2d 31 3b 0a 20 20 age->nCell-1;.
17283 20 20 69 66 28 20 62 69 61 73 52 69 67 68 74 20 if( biasRight
17284 29 7b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 61 ){. pCur->a
17285 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 iIdx[pCur->iPage
17286 5d 20 3d 20 28 75 31 36 29 75 70 72 3b 0a 20 20 ] = (u16)upr;.
17287 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 }else{. p
17288 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d Cur->aiIdx[pCur-
17289 3e 69 50 61 67 65 5d 20 3d 20 28 75 31 36 29 28 >iPage] = (u16)(
1728a 28 75 70 72 2b 6c 77 72 29 2f 32 29 3b 0a 20 20 (upr+lwr)/2);.
1728b 20 20 7d 0a 20 20 20 20 66 6f 72 28 3b 3b 29 7b }. for(;;){
1728c 0a 20 20 20 20 20 20 69 6e 74 20 69 64 78 20 3d . int idx =
1728d 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 pCur->aiIdx[pCu
1728e 72 2d 3e 69 50 61 67 65 5d 3b 20 2f 2a 20 49 6e r->iPage]; /* In
1728f 64 65 78 20 6f 66 20 63 75 72 72 65 6e 74 20 63 dex of current c
17290 65 6c 6c 20 69 6e 20 70 50 61 67 65 20 2a 2f 0a ell in pPage */.
17291 20 20 20 20 20 20 75 38 20 2a 70 43 65 6c 6c 3b u8 *pCell;
17292 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17293 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 /* Poi
17294 6e 74 65 72 20 74 6f 20 63 75 72 72 65 6e 74 20 nter to current
17295 63 65 6c 6c 20 69 6e 20 70 50 61 67 65 20 2a 2f cell in pPage */
17296 0a 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 69 6e .. pCur->in
17297 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 fo.nSize = 0;.
17298 20 20 20 20 70 43 65 6c 6c 20 3d 20 66 69 6e 64 pCell = find
17299 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 64 78 29 Cell(pPage, idx)
1729a 20 2b 20 70 50 61 67 65 2d 3e 63 68 69 6c 64 50 + pPage->childP
1729b 74 72 53 69 7a 65 3b 0a 20 20 20 20 20 20 69 66 trSize;. if
1729c 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 ( pPage->intKey
1729d 29 7b 0a 20 20 20 20 20 20 20 20 69 36 34 20 6e ){. i64 n
1729e 43 65 6c 6c 4b 65 79 3b 0a 20 20 20 20 20 20 20 CellKey;.
1729f 20 69 66 28 20 70 50 61 67 65 2d 3e 68 61 73 44 if( pPage->hasD
172a0 61 74 61 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ata ){.
172a1 20 75 33 32 20 64 75 6d 6d 79 3b 0a 20 20 20 20 u32 dummy;.
172a2 20 20 20 20 20 20 70 43 65 6c 6c 20 2b 3d 20 67 pCell += g
172a3 65 74 56 61 72 69 6e 74 33 32 28 70 43 65 6c 6c etVarint32(pCell
172a4 2c 20 64 75 6d 6d 79 29 3b 0a 20 20 20 20 20 20 , dummy);.
172a5 20 20 7d 0a 20 20 20 20 20 20 20 20 67 65 74 56 }. getV
172a6 61 72 69 6e 74 28 70 43 65 6c 6c 2c 20 28 75 36 arint(pCell, (u6
172a7 34 2a 29 26 6e 43 65 6c 6c 4b 65 79 29 3b 0a 20 4*)&nCellKey);.
172a8 20 20 20 20 20 20 20 69 66 28 20 6e 43 65 6c 6c if( nCell
172a9 4b 65 79 3d 3d 69 6e 74 4b 65 79 20 29 7b 0a 20 Key==intKey ){.
172aa 20 20 20 20 20 20 20 20 20 63 20 3d 20 30 3b 0a c = 0;.
172ab 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 }else if
172ac 28 20 6e 43 65 6c 6c 4b 65 79 3c 69 6e 74 4b 65 ( nCellKey<intKe
172ad 79 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 63 y ){. c
172ae 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 20 20 7d = -1;. }
172af 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 else{.
172b0 61 73 73 65 72 74 28 20 6e 43 65 6c 6c 4b 65 79 assert( nCellKey
172b1 3e 69 6e 74 4b 65 79 20 29 3b 0a 20 20 20 20 20 >intKey );.
172b2 20 20 20 20 20 63 20 3d 20 2b 31 3b 0a 20 20 20 c = +1;.
172b3 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 70 }. p
172b4 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d Cur->validNKey =
172b5 20 31 3b 0a 20 20 20 20 20 20 20 20 70 43 75 72 1;. pCur
172b6 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 20 3d 20 6e 43 ->info.nKey = nC
172b7 65 6c 6c 4b 65 79 3b 0a 20 20 20 20 20 20 7d 65 ellKey;. }e
172b8 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 lse{. /*
172b9 54 68 65 20 6d 61 78 69 6d 75 6d 20 73 75 70 70 The maximum supp
172ba 6f 72 74 65 64 20 70 61 67 65 2d 73 69 7a 65 20 orted page-size
172bb 69 73 20 33 32 37 36 38 20 62 79 74 65 73 2e 20 is 32768 bytes.
172bc 54 68 69 73 20 6d 65 61 6e 73 20 74 68 61 74 0a This means that.
172bd 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6d ** the m
172be 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 aximum number of
172bf 20 72 65 63 6f 72 64 20 62 79 74 65 73 20 73 74 record bytes st
172c0 6f 72 65 64 20 6f 6e 20 61 6e 20 69 6e 64 65 78 ored on an index
172c1 20 42 2d 54 72 65 65 0a 20 20 20 20 20 20 20 20 B-Tree.
172c2 2a 2a 20 70 61 67 65 20 69 73 20 61 74 20 6d 6f ** page is at mo
172c3 73 74 20 38 31 39 38 20 62 79 74 65 73 2c 20 77 st 8198 bytes, w
172c4 68 69 63 68 20 6d 61 79 20 62 65 20 73 74 6f 72 hich may be stor
172c5 65 64 20 61 73 20 61 20 32 2d 62 79 74 65 0a 20 ed as a 2-byte.
172c6 20 20 20 20 20 20 20 2a 2a 20 76 61 72 69 6e 74 ** varint
172c7 2e 20 54 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 . This informati
172c8 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 61 74 on is used to at
172c9 74 65 6d 70 74 20 74 6f 20 61 76 6f 69 64 20 70 tempt to avoid p
172ca 61 72 73 69 6e 67 20 0a 20 20 20 20 20 20 20 20 arsing .
172cb 2a 2a 20 74 68 65 20 65 6e 74 69 72 65 20 63 65 ** the entire ce
172cc 6c 6c 20 62 79 20 63 68 65 63 6b 69 6e 67 20 66 ll by checking f
172cd 6f 72 20 74 68 65 20 63 61 73 65 73 20 77 68 65 or the cases whe
172ce 72 65 20 74 68 65 20 72 65 63 6f 72 64 20 69 73 re the record is
172cf 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 74 6f . ** sto
172d0 72 65 64 20 65 6e 74 69 72 65 6c 79 20 77 69 74 red entirely wit
172d1 68 69 6e 20 74 68 65 20 62 2d 74 72 65 65 20 70 hin the b-tree p
172d2 61 67 65 20 62 79 20 69 6e 73 70 65 63 74 69 6e age by inspectin
172d3 67 20 74 68 65 20 66 69 72 73 74 20 0a 20 20 20 g the first .
172d4 20 20 20 20 20 2a 2a 20 32 20 62 79 74 65 73 20 ** 2 bytes
172d5 6f 66 20 74 68 65 20 63 65 6c 6c 2e 0a 20 20 20 of the cell..
172d6 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 */.
172d7 69 6e 74 20 6e 43 65 6c 6c 20 3d 20 70 43 65 6c int nCell = pCel
172d8 6c 5b 30 5d 3b 0a 20 20 20 20 20 20 20 20 69 66 l[0];. if
172d9 28 20 21 28 6e 43 65 6c 6c 20 26 20 30 78 38 30 ( !(nCell & 0x80
172da 29 20 26 26 20 6e 43 65 6c 6c 3c 3d 70 50 61 67 ) && nCell<=pPag
172db 65 2d 3e 6d 61 78 4c 6f 63 61 6c 20 29 7b 0a 20 e->maxLocal ){.
172dc 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 /* This
172dd 20 62 72 61 6e 63 68 20 72 75 6e 73 20 69 66 20 branch runs if
172de 74 68 65 20 72 65 63 6f 72 64 2d 73 69 7a 65 20 the record-size
172df 66 69 65 6c 64 20 6f 66 20 74 68 65 20 63 65 6c field of the cel
172e0 6c 20 69 73 20 61 0a 20 20 20 20 20 20 20 20 20 l is a.
172e1 20 2a 2a 20 73 69 6e 67 6c 65 20 62 79 74 65 20 ** single byte
172e2 76 61 72 69 6e 74 20 61 6e 64 20 74 68 65 20 72 varint and the r
172e3 65 63 6f 72 64 20 66 69 74 73 20 65 6e 74 69 72 ecord fits entir
172e4 65 6c 79 20 6f 6e 20 74 68 65 20 6d 61 69 6e 0a ely on the main.
172e5 20 20 20 20 20 20 20 20 20 20 2a 2a 20 62 2d 74 ** b-t
172e6 72 65 65 20 70 61 67 65 2e 20 20 2a 2f 0a 20 20 ree page. */.
172e7 20 20 20 20 20 20 20 20 63 20 3d 20 73 71 6c 69 c = sqli
172e8 74 65 33 56 64 62 65 52 65 63 6f 72 64 43 6f 6d te3VdbeRecordCom
172e9 70 61 72 65 28 6e 43 65 6c 6c 2c 20 28 76 6f 69 pare(nCell, (voi
172ea 64 2a 29 26 70 43 65 6c 6c 5b 31 5d 2c 20 70 49 d*)&pCell[1], pI
172eb 64 78 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 20 dxKey);.
172ec 7d 65 6c 73 65 20 69 66 28 20 21 28 70 43 65 6c }else if( !(pCel
172ed 6c 5b 31 5d 20 26 20 30 78 38 30 29 20 0a 20 20 l[1] & 0x80) .
172ee 20 20 20 20 20 20 20 20 26 26 20 28 6e 43 65 6c && (nCel
172ef 6c 20 3d 20 28 28 6e 43 65 6c 6c 26 30 78 37 66 l = ((nCell&0x7f
172f0 29 3c 3c 37 29 20 2b 20 70 43 65 6c 6c 5b 31 5d )<<7) + pCell[1]
172f1 29 3c 3d 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 )<=pPage->maxLoc
172f2 61 6c 0a 20 20 20 20 20 20 20 20 29 7b 0a 20 20 al. ){.
172f3 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 /* The r
172f4 65 63 6f 72 64 2d 73 69 7a 65 20 66 69 65 6c 64 ecord-size field
172f5 20 69 73 20 61 20 32 20 62 79 74 65 20 76 61 72 is a 2 byte var
172f6 69 6e 74 20 61 6e 64 20 74 68 65 20 72 65 63 6f int and the reco
172f7 72 64 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2a rd . **
172f8 20 66 69 74 73 20 65 6e 74 69 72 65 6c 79 20 6f fits entirely o
172f9 6e 20 74 68 65 20 6d 61 69 6e 20 62 2d 74 72 65 n the main b-tre
172fa 65 20 70 61 67 65 2e 20 20 2a 2f 0a 20 20 20 20 e page. */.
172fb 20 20 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 c = sqlite
172fc 33 56 64 62 65 52 65 63 6f 72 64 43 6f 6d 70 61 3VdbeRecordCompa
172fd 72 65 28 6e 43 65 6c 6c 2c 20 28 76 6f 69 64 2a re(nCell, (void*
172fe 29 26 70 43 65 6c 6c 5b 32 5d 2c 20 70 49 64 78 )&pCell[2], pIdx
172ff 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 Key);. }e
17300 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2f lse{. /
17301 2a 20 54 68 65 20 72 65 63 6f 72 64 20 66 6c 6f * The record flo
17302 77 73 20 6f 76 65 72 20 6f 6e 74 6f 20 6f 6e 65 ws over onto one
17303 20 6f 72 20 6d 6f 72 65 20 6f 76 65 72 66 6c 6f or more overflo
17304 77 20 70 61 67 65 73 2e 20 49 6e 0a 20 20 20 20 w pages. In.
17305 20 20 20 20 20 20 2a 2a 20 74 68 69 73 20 63 61 ** this ca
17306 73 65 20 74 68 65 20 77 68 6f 6c 65 20 63 65 6c se the whole cel
17307 6c 20 6e 65 65 64 73 20 74 6f 20 62 65 20 70 61 l needs to be pa
17308 72 73 65 64 2c 20 61 20 62 75 66 66 65 72 20 61 rsed, a buffer a
17309 6c 6c 6f 63 61 74 65 64 0a 20 20 20 20 20 20 20 llocated.
1730a 20 20 20 2a 2a 20 61 6e 64 20 61 63 63 65 73 73 ** and access
1730b 50 61 79 6c 6f 61 64 28 29 20 75 73 65 64 20 74 Payload() used t
1730c 6f 20 72 65 74 72 69 65 76 65 20 74 68 65 20 72 o retrieve the r
1730d 65 63 6f 72 64 20 69 6e 74 6f 20 74 68 65 0a 20 ecord into the.
1730e 20 20 20 20 20 20 20 20 20 2a 2a 20 62 75 66 66 ** buff
1730f 65 72 20 62 65 66 6f 72 65 20 56 64 62 65 52 65 er before VdbeRe
17310 63 6f 72 64 43 6f 6d 70 61 72 65 28 29 20 63 61 cordCompare() ca
17311 6e 20 62 65 20 63 61 6c 6c 65 64 2e 20 2a 2f 0a n be called. */.
17312 20 20 20 20 20 20 20 20 20 20 76 6f 69 64 20 2a void *
17313 70 43 65 6c 6c 4b 65 79 3b 0a 20 20 20 20 20 20 pCellKey;.
17314 20 20 20 20 75 38 20 2a 20 63 6f 6e 73 74 20 70 u8 * const p
17315 43 65 6c 6c 42 6f 64 79 20 3d 20 70 43 65 6c 6c CellBody = pCell
17316 20 2d 20 70 50 61 67 65 2d 3e 63 68 69 6c 64 50 - pPage->childP
17317 74 72 53 69 7a 65 3b 0a 20 20 20 20 20 20 20 20 trSize;.
17318 20 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c btreeParseCell
17319 50 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c Ptr(pPage, pCell
1731a 42 6f 64 79 2c 20 26 70 43 75 72 2d 3e 69 6e 66 Body, &pCur->inf
1731b 6f 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6e 43 o);. nC
1731c 65 6c 6c 20 3d 20 28 69 6e 74 29 70 43 75 72 2d ell = (int)pCur-
1731d 3e 69 6e 66 6f 2e 6e 4b 65 79 3b 0a 20 20 20 20 >info.nKey;.
1731e 20 20 20 20 20 20 70 43 65 6c 6c 4b 65 79 20 3d pCellKey =
1731f 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20 sqlite3Malloc(
17320 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 20 20 20 20 nCell );.
17321 20 20 20 69 66 28 20 70 43 65 6c 6c 4b 65 79 3d if( pCellKey=
17322 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 =0 ){.
17323 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f rc = SQLITE_NO
17324 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 20 20 20 MEM;.
17325 20 67 6f 74 6f 20 6d 6f 76 65 74 6f 5f 66 69 6e goto moveto_fin
17326 69 73 68 3b 0a 20 20 20 20 20 20 20 20 20 20 7d ish;. }
17327 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 . rc =
17328 61 63 63 65 73 73 50 61 79 6c 6f 61 64 28 70 43 accessPayload(pC
17329 75 72 2c 20 30 2c 20 6e 43 65 6c 6c 2c 20 28 75 ur, 0, nCell, (u
1732a 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 29 70 43 nsigned char*)pC
1732b 65 6c 6c 4b 65 79 2c 20 30 29 3b 0a 20 20 20 20 ellKey, 0);.
1732c 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a if( rc ){.
1732d 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 sqli
1732e 74 65 33 5f 66 72 65 65 28 70 43 65 6c 6c 4b 65 te3_free(pCellKe
1732f 79 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 y);.
17330 67 6f 74 6f 20 6d 6f 76 65 74 6f 5f 66 69 6e 69 goto moveto_fini
17331 73 68 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a sh;. }.
17332 20 20 20 20 20 20 20 20 20 20 63 20 3d 20 73 71 c = sq
17333 6c 69 74 65 33 56 64 62 65 52 65 63 6f 72 64 43 lite3VdbeRecordC
17334 6f 6d 70 61 72 65 28 6e 43 65 6c 6c 2c 20 70 43 ompare(nCell, pC
17335 65 6c 6c 4b 65 79 2c 20 70 49 64 78 4b 65 79 29 ellKey, pIdxKey)
17336 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 ;. sqli
17337 74 65 33 5f 66 72 65 65 28 70 43 65 6c 6c 4b 65 te3_free(pCellKe
17338 79 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 y);. }.
17339 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 }. if(
1733a 63 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 c==0 ){.
1733b 69 66 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 if( pPage->intKe
1733c 79 20 26 26 20 21 70 50 61 67 65 2d 3e 6c 65 61 y && !pPage->lea
1733d 66 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6c f ){. l
1733e 77 72 20 3d 20 69 64 78 3b 0a 20 20 20 20 20 20 wr = idx;.
1733f 20 20 20 20 75 70 72 20 3d 20 6c 77 72 20 2d 20 upr = lwr -
17340 31 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 1;. bre
17341 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 ak;. }els
17342 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 70 52 e{. *pR
17343 65 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 es = 0;.
17344 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b rc = SQLITE_OK
17345 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f ;. goto
17346 20 6d 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3b 0a moveto_finish;.
17347 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
17348 7d 0a 20 20 20 20 20 20 69 66 28 20 63 3c 30 20 }. if( c<0
17349 29 7b 0a 20 20 20 20 20 20 20 20 6c 77 72 20 3d ){. lwr =
1734a 20 69 64 78 2b 31 3b 0a 20 20 20 20 20 20 7d 65 idx+1;. }e
1734b 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 75 70 72 lse{. upr
1734c 20 3d 20 69 64 78 2d 31 3b 0a 20 20 20 20 20 20 = idx-1;.
1734d 7d 0a 20 20 20 20 20 20 69 66 28 20 6c 77 72 3e }. if( lwr>
1734e 75 70 72 20 29 7b 0a 20 20 20 20 20 20 20 20 62 upr ){. b
1734f 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 reak;. }.
17350 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b pCur->aiIdx[
17351 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 3d 20 28 pCur->iPage] = (
17352 75 31 36 29 28 28 6c 77 72 2b 75 70 72 29 2f 32 u16)((lwr+upr)/2
17353 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 );. }. ass
17354 65 72 74 28 20 6c 77 72 3d 3d 75 70 72 2b 31 20 ert( lwr==upr+1
17355 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 );. assert( p
17356 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 29 3b 0a Page->isInit );.
17357 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 6c if( pPage->l
17358 65 61 66 20 29 7b 0a 20 20 20 20 20 20 63 68 6c eaf ){. chl
17359 64 50 67 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c dPg = 0;. }el
1735a 73 65 20 69 66 28 20 6c 77 72 3e 3d 70 50 61 67 se if( lwr>=pPag
1735b 65 2d 3e 6e 43 65 6c 6c 20 29 7b 0a 20 20 20 20 e->nCell ){.
1735c 20 20 63 68 6c 64 50 67 20 3d 20 67 65 74 34 62 chldPg = get4b
1735d 79 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61 74 yte(&pPage->aDat
1735e 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 a[pPage->hdrOffs
1735f 65 74 2b 38 5d 29 3b 0a 20 20 20 20 7d 65 6c 73 et+8]);. }els
17360 65 7b 0a 20 20 20 20 20 20 63 68 6c 64 50 67 20 e{. chldPg
17361 3d 20 67 65 74 34 62 79 74 65 28 66 69 6e 64 43 = get4byte(findC
17362 65 6c 6c 28 70 50 61 67 65 2c 20 6c 77 72 29 29 ell(pPage, lwr))
17363 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
17364 63 68 6c 64 50 67 3d 3d 30 20 29 7b 0a 20 20 20 chldPg==0 ){.
17365 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d assert( pCur-
17366 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 >aiIdx[pCur->iPa
17367 67 65 5d 3c 70 43 75 72 2d 3e 61 70 50 61 67 65 ge]<pCur->apPage
17368 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e [pCur->iPage]->n
17369 43 65 6c 6c 20 29 3b 0a 20 20 20 20 20 20 2a 70 Cell );. *p
1736a 52 65 73 20 3d 20 63 3b 0a 20 20 20 20 20 20 72 Res = c;. r
1736b 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
1736c 20 20 20 20 20 67 6f 74 6f 20 6d 6f 76 65 74 6f goto moveto
1736d 5f 66 69 6e 69 73 68 3b 0a 20 20 20 20 7d 0a 20 _finish;. }.
1736e 20 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 pCur->aiIdx[p
1736f 43 75 72 2d 3e 69 50 61 67 65 5d 20 3d 20 28 75 Cur->iPage] = (u
17370 31 36 29 6c 77 72 3b 0a 20 20 20 20 70 43 75 72 16)lwr;. pCur
17371 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 ->info.nSize = 0
17372 3b 0a 20 20 20 20 70 43 75 72 2d 3e 76 61 6c 69 ;. pCur->vali
17373 64 4e 4b 65 79 20 3d 20 30 3b 0a 20 20 20 20 72 dNKey = 0;. r
17374 63 20 3d 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 c = moveToChild(
17375 70 43 75 72 2c 20 63 68 6c 64 50 67 29 3b 0a 20 pCur, chldPg);.
17376 20 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f if( rc ) goto
17377 20 6d 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3b 0a moveto_finish;.
17378 20 20 7d 0a 6d 6f 76 65 74 6f 5f 66 69 6e 69 73 }.moveto_finis
17379 68 3a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a h:. return rc;.
1737a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e }.../*.** Return
1737b 20 54 52 55 45 20 69 66 20 74 68 65 20 63 75 72 TRUE if the cur
1737c 73 6f 72 20 69 73 20 6e 6f 74 20 70 6f 69 6e 74 sor is not point
1737d 69 6e 67 20 61 74 20 61 6e 20 65 6e 74 72 79 20 ing at an entry
1737e 6f 66 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2a of the table..**
1737f 0a 2a 2a 20 54 52 55 45 20 77 69 6c 6c 20 62 65 .** TRUE will be
17380 20 72 65 74 75 72 6e 65 64 20 61 66 74 65 72 20 returned after
17381 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 a call to sqlite
17382 33 42 74 72 65 65 4e 65 78 74 28 29 20 6d 6f 76 3BtreeNext() mov
17383 65 73 0a 2a 2a 20 70 61 73 74 20 74 68 65 20 6c es.** past the l
17384 61 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 ast entry in the
17385 20 74 61 62 6c 65 20 6f 72 20 73 71 6c 69 74 65 table or sqlite
17386 33 42 74 72 65 65 50 72 65 76 28 29 20 6d 6f 76 3BtreePrev() mov
17387 65 73 20 70 61 73 74 0a 2a 2a 20 74 68 65 20 66 es past.** the f
17388 69 72 73 74 20 65 6e 74 72 79 2e 20 20 54 52 55 irst entry. TRU
17389 45 20 69 73 20 61 6c 73 6f 20 72 65 74 75 72 6e E is also return
1738a 65 64 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 ed if the table
1738b 69 73 20 65 6d 70 74 79 2e 0a 2a 2f 0a 53 51 4c is empty..*/.SQL
1738c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
1738d 73 71 6c 69 74 65 33 42 74 72 65 65 45 6f 66 28 sqlite3BtreeEof(
1738e 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b BtCursor *pCur){
1738f 0a 20 20 2f 2a 20 54 4f 44 4f 3a 20 57 68 61 74 . /* TODO: What
17390 20 69 66 20 74 68 65 20 63 75 72 73 6f 72 20 69 if the cursor i
17391 73 20 69 6e 20 43 55 52 53 4f 52 5f 52 45 51 55 s in CURSOR_REQU
17392 49 52 45 53 45 45 4b 20 62 75 74 20 61 6c 6c 20 IRESEEK but all
17393 74 61 62 6c 65 20 65 6e 74 72 69 65 73 0a 20 20 table entries.
17394 2a 2a 20 68 61 76 65 20 62 65 65 6e 20 64 65 6c ** have been del
17395 65 74 65 64 3f 20 54 68 69 73 20 41 50 49 20 77 eted? This API w
17396 69 6c 6c 20 6e 65 65 64 20 74 6f 20 63 68 61 6e ill need to chan
17397 67 65 20 74 6f 20 72 65 74 75 72 6e 20 61 6e 20 ge to return an
17398 65 72 72 6f 72 20 63 6f 64 65 0a 20 20 2a 2a 20 error code. **
17399 61 73 20 77 65 6c 6c 20 61 73 20 74 68 65 20 62 as well as the b
1739a 6f 6f 6c 65 61 6e 20 72 65 73 75 6c 74 20 76 61 oolean result va
1739b 6c 75 65 2e 0a 20 20 2a 2f 0a 20 20 72 65 74 75 lue.. */. retu
1739c 72 6e 20 28 43 55 52 53 4f 52 5f 56 41 4c 49 44 rn (CURSOR_VALID
1739d 21 3d 70 43 75 72 2d 3e 65 53 74 61 74 65 29 3b !=pCur->eState);
1739e 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 76 61 6e 63 .}../*.** Advanc
1739f 65 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20 e the cursor to
173a0 74 68 65 20 6e 65 78 74 20 65 6e 74 72 79 20 69 the next entry i
173a1 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 n the database.
173a2 20 49 66 0a 2a 2a 20 73 75 63 63 65 73 73 66 75 If.** successfu
173a3 6c 20 74 68 65 6e 20 73 65 74 20 2a 70 52 65 73 l then set *pRes
173a4 3d 30 2e 20 20 49 66 20 74 68 65 20 63 75 72 73 =0. If the curs
173a5 6f 72 0a 2a 2a 20 77 61 73 20 61 6c 72 65 61 64 or.** was alread
173a6 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 y pointing to th
173a7 65 20 6c 61 73 74 20 65 6e 74 72 79 20 69 6e 20 e last entry in
173a8 74 68 65 20 64 61 74 61 62 61 73 65 20 62 65 66 the database bef
173a9 6f 72 65 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 ore.** this rout
173aa 69 6e 65 20 77 61 73 20 63 61 6c 6c 65 64 2c 20 ine was called,
173ab 74 68 65 6e 20 73 65 74 20 2a 70 52 65 73 3d 31 then set *pRes=1
173ac 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
173ad 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
173ae 74 72 65 65 4e 65 78 74 28 42 74 43 75 72 73 6f treeNext(BtCurso
173af 72 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70 52 r *pCur, int *pR
173b0 65 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 es){. int rc;.
173b1 20 69 6e 74 20 69 64 78 3b 0a 20 20 4d 65 6d 50 int idx;. MemP
173b2 61 67 65 20 2a 70 50 61 67 65 3b 0a 0a 20 20 61 age *pPage;.. a
173b3 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c ssert( cursorHol
173b4 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b dsMutex(pCur) );
173b5 0a 20 20 72 63 20 3d 20 72 65 73 74 6f 72 65 43 . rc = restoreC
173b6 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 43 ursorPosition(pC
173b7 75 72 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 ur);. if( rc!=S
173b8 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
173b9 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 return rc;. }.
173ba 20 61 73 73 65 72 74 28 20 70 52 65 73 21 3d 30 assert( pRes!=0
173bb 20 29 3b 0a 20 20 69 66 28 20 43 55 52 53 4f 52 );. if( CURSOR
173bc 5f 49 4e 56 41 4c 49 44 3d 3d 70 43 75 72 2d 3e _INVALID==pCur->
173bd 65 53 74 61 74 65 20 29 7b 0a 20 20 20 20 2a 70 eState ){. *p
173be 52 65 73 20 3d 20 31 3b 0a 20 20 20 20 72 65 74 Res = 1;. ret
173bf 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
173c0 20 7d 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 73 }. if( pCur->s
173c1 6b 69 70 4e 65 78 74 3e 30 20 29 7b 0a 20 20 20 kipNext>0 ){.
173c2 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78 74 20 pCur->skipNext
173c3 3d 20 30 3b 0a 20 20 20 20 2a 70 52 65 73 20 3d = 0;. *pRes =
173c4 20 30 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 0;. return S
173c5 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 QLITE_OK;. }.
173c6 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78 74 20 3d pCur->skipNext =
173c7 20 30 3b 0a 0a 20 20 70 50 61 67 65 20 3d 20 70 0;.. pPage = p
173c8 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 Cur->apPage[pCur
173c9 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 69 64 78 20 ->iPage];. idx
173ca 3d 20 2b 2b 70 43 75 72 2d 3e 61 69 49 64 78 5b = ++pCur->aiIdx[
173cb 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 pCur->iPage];.
173cc 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69 assert( pPage->i
173cd 73 49 6e 69 74 20 29 3b 0a 20 20 61 73 73 65 72 sInit );. asser
173ce 74 28 20 69 64 78 3c 3d 70 50 61 67 65 2d 3e 6e t( idx<=pPage->n
173cf 43 65 6c 6c 20 29 3b 0a 0a 20 20 70 43 75 72 2d Cell );.. pCur-
173d0 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b >info.nSize = 0;
173d1 0a 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b . pCur->validNK
173d2 65 79 20 3d 20 30 3b 0a 20 20 69 66 28 20 69 64 ey = 0;. if( id
173d3 78 3e 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 x>=pPage->nCell
173d4 29 7b 0a 20 20 20 20 69 66 28 20 21 70 50 61 67 ){. if( !pPag
173d5 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 e->leaf ){.
173d6 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 68 69 6c rc = moveToChil
173d7 64 28 70 43 75 72 2c 20 67 65 74 34 62 79 74 65 d(pCur, get4byte
173d8 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 (&pPage->aData[p
173d9 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b Page->hdrOffset+
173da 38 5d 29 29 3b 0a 20 20 20 20 20 20 69 66 28 20 8]));. if(
173db 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a rc ) return rc;.
173dc 20 20 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 rc = moveT
173dd 6f 4c 65 66 74 6d 6f 73 74 28 70 43 75 72 29 3b oLeftmost(pCur);
173de 0a 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 30 . *pRes = 0
173df 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 ;. return r
173e0 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 6f 7b c;. }. do{
173e1 0a 20 20 20 20 20 20 69 66 28 20 70 43 75 72 2d . if( pCur-
173e2 3e 69 50 61 67 65 3d 3d 30 20 29 7b 0a 20 20 20 >iPage==0 ){.
173e3 20 20 20 20 20 2a 70 52 65 73 20 3d 20 31 3b 0a *pRes = 1;.
173e4 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e 65 53 pCur->eS
173e5 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e tate = CURSOR_IN
173e6 56 41 4c 49 44 3b 0a 20 20 20 20 20 20 20 20 72 VALID;. r
173e7 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
173e8 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6d . }. m
173e9 6f 76 65 54 6f 50 61 72 65 6e 74 28 70 43 75 72 oveToParent(pCur
173ea 29 3b 0a 20 20 20 20 20 20 70 50 61 67 65 20 3d );. pPage =
173eb 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 pCur->apPage[pC
173ec 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 20 20 ur->iPage];.
173ed 7d 77 68 69 6c 65 28 20 70 43 75 72 2d 3e 61 69 }while( pCur->ai
173ee 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d Idx[pCur->iPage]
173ef 3e 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 >=pPage->nCell )
173f0 3b 0a 20 20 20 20 2a 70 52 65 73 20 3d 20 30 3b ;. *pRes = 0;
173f1 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e . if( pPage->
173f2 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 intKey ){.
173f3 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 rc = sqlite3Btre
173f4 65 4e 65 78 74 28 70 43 75 72 2c 20 70 52 65 73 eNext(pCur, pRes
173f5 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 );. }else{.
173f6 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
173f7 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 OK;. }. re
173f8 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 2a turn rc;. }. *
173f9 70 52 65 73 20 3d 20 30 3b 0a 20 20 69 66 28 20 pRes = 0;. if(
173fa 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 pPage->leaf ){.
173fb 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
173fc 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 _OK;. }. rc =
173fd 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f 73 74 28 70 moveToLeftmost(p
173fe 43 75 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 Cur);. return r
173ff 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 53 74 65 c;.}.../*.** Ste
17400 70 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20 p the cursor to
17401 74 68 65 20 62 61 63 6b 20 74 6f 20 74 68 65 20 the back to the
17402 70 72 65 76 69 6f 75 73 20 65 6e 74 72 79 20 69 previous entry i
17403 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 n the database.
17404 20 49 66 0a 2a 2a 20 73 75 63 63 65 73 73 66 75 If.** successfu
17405 6c 20 74 68 65 6e 20 73 65 74 20 2a 70 52 65 73 l then set *pRes
17406 3d 30 2e 20 20 49 66 20 74 68 65 20 63 75 72 73 =0. If the curs
17407 6f 72 0a 2a 2a 20 77 61 73 20 61 6c 72 65 61 64 or.** was alread
17408 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 y pointing to th
17409 65 20 66 69 72 73 74 20 65 6e 74 72 79 20 69 6e e first entry in
1740a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 62 65 the database be
1740b 66 6f 72 65 0a 2a 2a 20 74 68 69 73 20 72 6f 75 fore.** this rou
1740c 74 69 6e 65 20 77 61 73 20 63 61 6c 6c 65 64 2c tine was called,
1740d 20 74 68 65 6e 20 73 65 74 20 2a 70 52 65 73 3d then set *pRes=
1740e 31 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 1..*/.SQLITE_PRI
1740f 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
17410 42 74 72 65 65 50 72 65 76 69 6f 75 73 28 42 74 BtreePrevious(Bt
17411 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e Cursor *pCur, in
17412 74 20 2a 70 52 65 73 29 7b 0a 20 20 69 6e 74 20 t *pRes){. int
17413 72 63 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 rc;. MemPage *p
17414 50 61 67 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 Page;.. assert(
17415 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 cursorHoldsMute
17416 78 28 70 43 75 72 29 20 29 3b 0a 20 20 72 63 20 x(pCur) );. rc
17417 3d 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72 50 = restoreCursorP
17418 6f 73 69 74 69 6f 6e 28 70 43 75 72 29 3b 0a 20 osition(pCur);.
17419 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
1741a 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e OK ){. return
1741b 20 72 63 3b 0a 20 20 7d 0a 20 20 70 43 75 72 2d rc;. }. pCur-
1741c 3e 61 74 4c 61 73 74 20 3d 20 30 3b 0a 20 20 69 >atLast = 0;. i
1741d 66 28 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 f( CURSOR_INVALI
1741e 44 3d 3d 70 43 75 72 2d 3e 65 53 74 61 74 65 20 D==pCur->eState
1741f 29 7b 0a 20 20 20 20 2a 70 52 65 73 20 3d 20 31 ){. *pRes = 1
17420 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c ;. return SQL
17421 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 ITE_OK;. }. if
17422 28 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78 74 ( pCur->skipNext
17423 3c 30 20 29 7b 0a 20 20 20 20 70 43 75 72 2d 3e <0 ){. pCur->
17424 73 6b 69 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20 skipNext = 0;.
17425 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 20 *pRes = 0;.
17426 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
17427 4b 3b 0a 20 20 7d 0a 20 20 70 43 75 72 2d 3e 73 K;. }. pCur->s
17428 6b 69 70 4e 65 78 74 20 3d 20 30 3b 0a 0a 20 20 kipNext = 0;..
17429 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 pPage = pCur->ap
1742a 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 Page[pCur->iPage
1742b 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 ];. assert( pPa
1742c 67 65 2d 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20 ge->isInit );.
1742d 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 if( !pPage->leaf
1742e 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 64 78 20 ){. int idx
1742f 3d 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 = pCur->aiIdx[pC
17430 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 20 20 ur->iPage];.
17431 72 63 20 3d 20 6d 6f 76 65 54 6f 43 68 69 6c 64 rc = moveToChild
17432 28 70 43 75 72 2c 20 67 65 74 34 62 79 74 65 28 (pCur, get4byte(
17433 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 findCell(pPage,
17434 69 64 78 29 29 29 3b 0a 20 20 20 20 69 66 28 20 idx)));. if(
17435 72 63 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 rc ){. retu
17436 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 rn rc;. }.
17437 20 72 63 20 3d 20 6d 6f 76 65 54 6f 52 69 67 68 rc = moveToRigh
17438 74 6d 6f 73 74 28 70 43 75 72 29 3b 0a 20 20 7d tmost(pCur);. }
17439 65 6c 73 65 7b 0a 20 20 20 20 77 68 69 6c 65 28 else{. while(
1743a 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 pCur->aiIdx[pCu
1743b 72 2d 3e 69 50 61 67 65 5d 3d 3d 30 20 29 7b 0a r->iPage]==0 ){.
1743c 20 20 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e if( pCur->
1743d 69 50 61 67 65 3d 3d 30 20 29 7b 0a 20 20 20 20 iPage==0 ){.
1743e 20 20 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 pCur->eState
1743f 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 = CURSOR_INVALI
17440 44 3b 0a 20 20 20 20 20 20 20 20 2a 70 52 65 73 D;. *pRes
17441 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 72 65 = 1;. re
17442 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
17443 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6d 6f }. mo
17444 76 65 54 6f 50 61 72 65 6e 74 28 70 43 75 72 29 veToParent(pCur)
17445 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 43 75 72 ;. }. pCur
17446 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 ->info.nSize = 0
17447 3b 0a 20 20 20 20 70 43 75 72 2d 3e 76 61 6c 69 ;. pCur->vali
17448 64 4e 4b 65 79 20 3d 20 30 3b 0a 0a 20 20 20 20 dNKey = 0;..
17449 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 pCur->aiIdx[pCur
1744a 2d 3e 69 50 61 67 65 5d 2d 2d 3b 0a 20 20 20 20 ->iPage]--;.
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 20 20 69 66 28 20 70 50 61 67 65 ];. if( pPage
1744e 2d 3e 69 6e 74 4b 65 79 20 26 26 20 21 70 50 61 ->intKey && !pPa
1744f 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 ge->leaf ){.
17450 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 rc = sqlite3Bt
17451 72 65 65 50 72 65 76 69 6f 75 73 28 70 43 75 72 reePrevious(pCur
17452 2c 20 70 52 65 73 29 3b 0a 20 20 20 20 7d 65 6c , pRes);. }el
17453 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 se{. rc = S
17454 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a QLITE_OK;. }.
17455 20 20 7d 0a 20 20 2a 70 52 65 73 20 3d 20 30 3b }. *pRes = 0;
17456 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
17457 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 ./*.** Allocate
17458 61 20 6e 65 77 20 70 61 67 65 20 66 72 6f 6d 20 a new page from
17459 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
1745a 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 77 e..**.** The new
1745b 20 70 61 67 65 20 69 73 20 6d 61 72 6b 65 64 20 page is marked
1745c 61 73 20 64 69 72 74 79 2e 20 20 28 49 6e 20 6f as dirty. (In o
1745d 74 68 65 72 20 77 6f 72 64 73 2c 20 73 71 6c 69 ther words, sqli
1745e 74 65 33 50 61 67 65 72 57 72 69 74 65 28 29 0a te3PagerWrite().
1745f 2a 2a 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 ** has already b
17460 65 65 6e 20 63 61 6c 6c 65 64 20 6f 6e 20 74 68 een called on th
17461 65 20 6e 65 77 20 70 61 67 65 2e 29 20 20 54 68 e new page.) Th
17462 65 20 6e 65 77 20 70 61 67 65 20 68 61 73 20 61 e new page has a
17463 6c 73 6f 0a 2a 2a 20 62 65 65 6e 20 72 65 66 65 lso.** been refe
17464 72 65 6e 63 65 64 20 61 6e 64 20 74 68 65 20 63 renced and the c
17465 61 6c 6c 69 6e 67 20 72 6f 75 74 69 6e 65 20 69 alling routine i
17466 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f s responsible fo
17467 72 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 73 71 6c r calling.** sql
17468 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 29 ite3PagerUnref()
17469 20 6f 6e 20 74 68 65 20 6e 65 77 20 70 61 67 65 on the new page
1746a 20 77 68 65 6e 20 69 74 20 69 73 20 64 6f 6e 65 when it is done
1746b 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f ..**.** SQLITE_O
1746c 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20 6f 6e K is returned on
1746d 20 73 75 63 63 65 73 73 2e 20 20 41 6e 79 20 6f success. Any o
1746e 74 68 65 72 20 72 65 74 75 72 6e 20 76 61 6c 75 ther return valu
1746f 65 20 69 6e 64 69 63 61 74 65 73 0a 2a 2a 20 61 e indicates.** a
17470 6e 20 65 72 72 6f 72 2e 20 20 2a 70 70 50 61 67 n error. *ppPag
17471 65 20 61 6e 64 20 2a 70 50 67 6e 6f 20 61 72 65 e and *pPgno are
17472 20 75 6e 64 65 66 69 6e 65 64 20 69 6e 20 74 68 undefined in th
17473 65 20 65 76 65 6e 74 20 6f 66 20 61 6e 20 65 72 e event of an er
17474 72 6f 72 2e 0a 2a 2a 20 44 6f 20 6e 6f 74 20 69 ror..** Do not i
17475 6e 76 6f 6b 65 20 73 71 6c 69 74 65 33 50 61 67 nvoke sqlite3Pag
17476 65 72 55 6e 72 65 66 28 29 20 6f 6e 20 2a 70 70 erUnref() on *pp
17477 50 61 67 65 20 69 66 20 61 6e 20 65 72 72 6f 72 Page if an error
17478 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a is returned..**
17479 0a 2a 2a 20 49 66 20 74 68 65 20 22 6e 65 61 72 .** If the "near
1747a 62 79 22 20 70 61 72 61 6d 65 74 65 72 20 69 73 by" parameter is
1747b 20 6e 6f 74 20 30 2c 20 74 68 65 6e 20 61 20 28 not 0, then a (
1747c 66 65 65 62 6c 65 29 20 65 66 66 6f 72 74 20 69 feeble) effort i
1747d 73 20 6d 61 64 65 20 74 6f 20 0a 2a 2a 20 6c 6f s made to .** lo
1747e 63 61 74 65 20 61 20 70 61 67 65 20 63 6c 6f 73 cate a page clos
1747f 65 20 74 6f 20 74 68 65 20 70 61 67 65 20 6e 75 e to the page nu
17480 6d 62 65 72 20 22 6e 65 61 72 62 79 22 2e 20 20 mber "nearby".
17481 54 68 69 73 20 63 61 6e 20 62 65 20 75 73 65 64 This can be used
17482 20 69 6e 20 61 6e 0a 2a 2a 20 61 74 74 65 6d 70 in an.** attemp
17483 74 20 74 6f 20 6b 65 65 70 20 72 65 6c 61 74 65 t to keep relate
17484 64 20 70 61 67 65 73 20 63 6c 6f 73 65 20 74 6f d pages close to
17485 20 65 61 63 68 20 6f 74 68 65 72 20 69 6e 20 74 each other in t
17486 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
17487 2c 0a 2a 2a 20 77 68 69 63 68 20 69 6e 20 74 75 ,.** which in tu
17488 72 6e 20 63 61 6e 20 6d 61 6b 65 20 64 61 74 61 rn can make data
17489 62 61 73 65 20 61 63 63 65 73 73 20 66 61 73 74 base access fast
1748a 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 er..**.** If the
1748b 20 22 65 78 61 63 74 22 20 70 61 72 61 6d 65 74 "exact" paramet
1748c 65 72 20 69 73 20 6e 6f 74 20 30 2c 20 61 6e 64 er is not 0, and
1748d 20 74 68 65 20 70 61 67 65 2d 6e 75 6d 62 65 72 the page-number
1748e 20 6e 65 61 72 62 79 20 65 78 69 73 74 73 20 0a nearby exists .
1748f 2a 2a 20 61 6e 79 77 68 65 72 65 20 6f 6e 20 74 ** anywhere on t
17490 68 65 20 66 72 65 65 2d 6c 69 73 74 2c 20 74 68 he free-list, th
17491 65 6e 20 69 74 20 69 73 20 67 75 61 72 65 6e 74 en it is guarent
17492 65 65 64 20 74 6f 20 62 65 20 72 65 74 75 72 6e eed to be return
17493 65 64 2e 20 54 68 69 73 0a 2a 2a 20 69 73 20 6f ed. This.** is o
17494 6e 6c 79 20 75 73 65 64 20 62 79 20 61 75 74 6f nly used by auto
17495 2d 76 61 63 75 75 6d 20 64 61 74 61 62 61 73 65 -vacuum database
17496 73 20 77 68 65 6e 20 61 6c 6c 6f 63 61 74 69 6e s when allocatin
17497 67 20 61 20 6e 65 77 20 74 61 62 6c 65 2e 0a 2a g a new table..*
17498 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 6c 6c /.static int all
17499 6f 63 61 74 65 42 74 72 65 65 50 61 67 65 28 0a ocateBtreePage(.
1749a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c BtShared *pBt,
1749b 20 0a 20 20 4d 65 6d 50 61 67 65 20 2a 2a 70 70 . MemPage **pp
1749c 50 61 67 65 2c 20 0a 20 20 50 67 6e 6f 20 2a 70 Page, . Pgno *p
1749d 50 67 6e 6f 2c 20 0a 20 20 50 67 6e 6f 20 6e 65 Pgno, . Pgno ne
1749e 61 72 62 79 2c 0a 20 20 75 38 20 65 78 61 63 74 arby,. u8 exact
1749f 0a 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 .){. MemPage *p
174a0 50 61 67 65 31 3b 0a 20 20 69 6e 74 20 72 63 3b Page1;. int rc;
174a1 0a 20 20 75 33 32 20 6e 3b 20 20 20 20 20 2f 2a . u32 n; /*
174a2 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 Number of pages
174a3 20 6f 6e 20 74 68 65 20 66 72 65 65 6c 69 73 74 on the freelist
174a4 20 2a 2f 0a 20 20 75 33 32 20 6b 3b 20 20 20 20 */. u32 k;
174a5 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6c 65 /* Number of le
174a6 61 76 65 73 20 6f 6e 20 74 68 65 20 74 72 75 6e aves on the trun
174a7 6b 20 6f 66 20 74 68 65 20 66 72 65 65 6c 69 73 k of the freelis
174a8 74 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a t */. MemPage *
174a9 70 54 72 75 6e 6b 20 3d 20 30 3b 0a 20 20 4d 65 pTrunk = 0;. Me
174aa 6d 50 61 67 65 20 2a 70 50 72 65 76 54 72 75 6e mPage *pPrevTrun
174ab 6b 20 3d 20 30 3b 0a 20 20 50 67 6e 6f 20 6d 78 k = 0;. Pgno mx
174ac 50 61 67 65 3b 20 20 20 20 20 2f 2a 20 54 6f 74 Page; /* Tot
174ad 61 6c 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 al size of the d
174ae 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a atabase file */.
174af 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
174b0 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 e3_mutex_held(pB
174b1 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 t->mutex) );. p
174b2 50 61 67 65 31 20 3d 20 70 42 74 2d 3e 70 50 61 Page1 = pBt->pPa
174b3 67 65 31 3b 0a 20 20 6d 78 50 61 67 65 20 3d 20 ge1;. mxPage =
174b4 70 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 pagerPagecount(p
174b5 42 74 29 3b 0a 20 20 6e 20 3d 20 67 65 74 34 62 Bt);. n = get4b
174b6 79 74 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61 yte(&pPage1->aDa
174b7 74 61 5b 33 36 5d 29 3b 0a 20 20 74 65 73 74 63 ta[36]);. testc
174b8 61 73 65 28 20 6e 3d 3d 6d 78 50 61 67 65 2d 31 ase( n==mxPage-1
174b9 20 29 3b 0a 20 20 69 66 28 20 6e 3e 3d 6d 78 50 );. if( n>=mxP
174ba 61 67 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 age ){. retur
174bb 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 n SQLITE_CORRUPT
174bc 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20 20 69 66 28 _BKPT;. }. if(
174bd 20 6e 3e 30 20 29 7b 0a 20 20 20 20 2f 2a 20 54 n>0 ){. /* T
174be 68 65 72 65 20 61 72 65 20 70 61 67 65 73 20 6f here are pages o
174bf 6e 20 74 68 65 20 66 72 65 65 6c 69 73 74 2e 20 n the freelist.
174c0 20 52 65 75 73 65 20 6f 6e 65 20 6f 66 20 74 68 Reuse one of th
174c1 6f 73 65 20 70 61 67 65 73 2e 20 2a 2f 0a 20 20 ose pages. */.
174c2 20 20 50 67 6e 6f 20 69 54 72 75 6e 6b 3b 0a 20 Pgno iTrunk;.
174c3 20 20 20 75 38 20 73 65 61 72 63 68 4c 69 73 74 u8 searchList
174c4 20 3d 20 30 3b 20 2f 2a 20 49 66 20 74 68 65 20 = 0; /* If the
174c5 66 72 65 65 2d 6c 69 73 74 20 6d 75 73 74 20 62 free-list must b
174c6 65 20 73 65 61 72 63 68 65 64 20 66 6f 72 20 27 e searched for '
174c7 6e 65 61 72 62 79 27 20 2a 2f 0a 20 20 20 20 0a nearby' */. .
174c8 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 27 65 /* If the 'e
174c9 78 61 63 74 27 20 70 61 72 61 6d 65 74 65 72 20 xact' parameter
174ca 77 61 73 20 74 72 75 65 20 61 6e 64 20 61 20 71 was true and a q
174cb 75 65 72 79 20 6f 66 20 74 68 65 20 70 6f 69 6e uery of the poin
174cc 74 65 72 2d 6d 61 70 0a 20 20 20 20 2a 2a 20 73 ter-map. ** s
174cd 68 6f 77 73 20 74 68 61 74 20 74 68 65 20 70 61 hows that the pa
174ce 67 65 20 27 6e 65 61 72 62 79 27 20 69 73 20 73 ge 'nearby' is s
174cf 6f 6d 65 77 68 65 72 65 20 6f 6e 20 74 68 65 20 omewhere on the
174d0 66 72 65 65 2d 6c 69 73 74 2c 20 74 68 65 6e 0a free-list, then.
174d1 20 20 20 20 2a 2a 20 74 68 65 20 65 6e 74 69 72 ** the entir
174d2 65 2d 6c 69 73 74 20 77 69 6c 6c 20 62 65 20 73 e-list will be s
174d3 65 61 72 63 68 65 64 20 66 6f 72 20 74 68 61 74 earched for that
174d4 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f 0a 23 69 page.. */.#i
174d5 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
174d6 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 T_AUTOVACUUM.
174d7 20 69 66 28 20 65 78 61 63 74 20 26 26 20 6e 65 if( exact && ne
174d8 61 72 62 79 3c 3d 6d 78 50 61 67 65 20 29 7b 0a arby<=mxPage ){.
174d9 20 20 20 20 20 20 75 38 20 65 54 79 70 65 3b 0a u8 eType;.
174da 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 65 assert( ne
174db 61 72 62 79 3e 30 20 29 3b 0a 20 20 20 20 20 20 arby>0 );.
174dc 61 73 73 65 72 74 28 20 70 42 74 2d 3e 61 75 74 assert( pBt->aut
174dd 6f 56 61 63 75 75 6d 20 29 3b 0a 20 20 20 20 20 oVacuum );.
174de 20 72 63 20 3d 20 70 74 72 6d 61 70 47 65 74 28 rc = ptrmapGet(
174df 70 42 74 2c 20 6e 65 61 72 62 79 2c 20 26 65 54 pBt, nearby, &eT
174e0 79 70 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 ype, 0);. i
174e1 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 f( rc ) return r
174e2 63 3b 0a 20 20 20 20 20 20 69 66 28 20 65 54 79 c;. if( eTy
174e3 70 65 3d 3d 50 54 52 4d 41 50 5f 46 52 45 45 50 pe==PTRMAP_FREEP
174e4 41 47 45 20 29 7b 0a 20 20 20 20 20 20 20 20 73 AGE ){. s
174e5 65 61 72 63 68 4c 69 73 74 20 3d 20 31 3b 0a 20 earchList = 1;.
174e6 20 20 20 20 20 7d 0a 20 20 20 20 20 20 2a 70 50 }. *pP
174e7 67 6e 6f 20 3d 20 6e 65 61 72 62 79 3b 0a 20 20 gno = nearby;.
174e8 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 }.#endif..
174e9 2f 2a 20 44 65 63 72 65 6d 65 6e 74 20 74 68 65 /* Decrement the
174ea 20 66 72 65 65 2d 6c 69 73 74 20 63 6f 75 6e 74 free-list count
174eb 20 62 79 20 31 2e 20 53 65 74 20 69 54 72 75 6e by 1. Set iTrun
174ec 6b 20 74 6f 20 74 68 65 20 69 6e 64 65 78 20 6f k to the index o
174ed 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 66 69 72 f the. ** fir
174ee 73 74 20 66 72 65 65 2d 6c 69 73 74 20 74 72 75 st free-list tru
174ef 6e 6b 20 70 61 67 65 2e 20 69 50 72 65 76 54 72 nk page. iPrevTr
174f0 75 6e 6b 20 69 73 20 69 6e 69 74 69 61 6c 6c 79 unk is initially
174f1 20 31 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 1.. */. r
174f2 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
174f3 57 72 69 74 65 28 70 50 61 67 65 31 2d 3e 70 44 Write(pPage1->pD
174f4 62 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 bPage);. if(
174f5 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a rc ) return rc;.
174f6 20 20 20 20 70 75 74 34 62 79 74 65 28 26 70 50 put4byte(&pP
174f7 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 36 5d 2c age1->aData[36],
174f8 20 6e 2d 31 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 n-1);.. /* T
174f9 68 65 20 63 6f 64 65 20 77 69 74 68 69 6e 20 74 he code within t
174fa 68 69 73 20 6c 6f 6f 70 20 69 73 20 72 75 6e 20 his loop is run
174fb 6f 6e 6c 79 20 6f 6e 63 65 20 69 66 20 74 68 65 only once if the
174fc 20 27 73 65 61 72 63 68 4c 69 73 74 27 20 76 61 'searchList' va
174fd 72 69 61 62 6c 65 0a 20 20 20 20 2a 2a 20 69 73 riable. ** is
174fe 20 6e 6f 74 20 74 72 75 65 2e 20 4f 74 68 65 72 not true. Other
174ff 77 69 73 65 2c 20 69 74 20 72 75 6e 73 20 6f 6e wise, it runs on
17500 63 65 20 66 6f 72 20 65 61 63 68 20 74 72 75 6e ce for each trun
17501 6b 2d 70 61 67 65 20 6f 6e 20 74 68 65 0a 20 20 k-page on the.
17502 20 20 2a 2a 20 66 72 65 65 2d 6c 69 73 74 20 75 ** free-list u
17503 6e 74 69 6c 20 74 68 65 20 70 61 67 65 20 27 6e ntil the page 'n
17504 65 61 72 62 79 27 20 69 73 20 6c 6f 63 61 74 65 earby' is locate
17505 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 64 6f d.. */. do
17506 20 7b 0a 20 20 20 20 20 20 70 50 72 65 76 54 72 {. pPrevTr
17507 75 6e 6b 20 3d 20 70 54 72 75 6e 6b 3b 0a 20 20 unk = pTrunk;.
17508 20 20 20 20 69 66 28 20 70 50 72 65 76 54 72 75 if( pPrevTru
17509 6e 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 69 54 nk ){. iT
1750a 72 75 6e 6b 20 3d 20 67 65 74 34 62 79 74 65 28 runk = get4byte(
1750b 26 70 50 72 65 76 54 72 75 6e 6b 2d 3e 61 44 61 &pPrevTrunk->aDa
1750c 74 61 5b 30 5d 29 3b 0a 20 20 20 20 20 20 7d 65 ta[0]);. }e
1750d 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69 54 72 lse{. iTr
1750e 75 6e 6b 20 3d 20 67 65 74 34 62 79 74 65 28 26 unk = get4byte(&
1750f 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 32 pPage1->aData[32
17510 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ]);. }.
17511 20 20 74 65 73 74 63 61 73 65 28 20 69 54 72 75 testcase( iTru
17512 6e 6b 3d 3d 6d 78 50 61 67 65 20 29 3b 0a 20 20 nk==mxPage );.
17513 20 20 20 20 69 66 28 20 69 54 72 75 6e 6b 3e 6d if( iTrunk>m
17514 78 50 61 67 65 20 29 7b 0a 20 20 20 20 20 20 20 xPage ){.
17515 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 rc = SQLITE_COR
17516 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 RUPT_BKPT;.
17517 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
17518 72 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 rc = btreeGetPag
17519 65 28 70 42 74 2c 20 69 54 72 75 6e 6b 2c 20 26 e(pBt, iTrunk, &
1751a 70 54 72 75 6e 6b 2c 20 30 29 3b 0a 20 20 20 20 pTrunk, 0);.
1751b 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 72 63 }. if( rc
1751c 20 29 7b 0a 20 20 20 20 20 20 20 20 70 54 72 75 ){. pTru
1751d 6e 6b 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 nk = 0;.
1751e 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 goto end_allocat
1751f 65 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 7d 0a e_page;. }.
17520 0a 20 20 20 20 20 20 6b 20 3d 20 67 65 74 34 62 . k = get4b
17521 79 74 65 28 26 70 54 72 75 6e 6b 2d 3e 61 44 61 yte(&pTrunk->aDa
17522 74 61 5b 34 5d 29 3b 0a 20 20 20 20 20 20 69 66 ta[4]);. if
17523 28 20 6b 3d 3d 30 20 26 26 20 21 73 65 61 72 63 ( k==0 && !searc
17524 68 4c 69 73 74 20 29 7b 0a 20 20 20 20 20 20 20 hList ){.
17525 20 2f 2a 20 54 68 65 20 74 72 75 6e 6b 20 68 61 /* The trunk ha
17526 73 20 6e 6f 20 6c 65 61 76 65 73 20 61 6e 64 20 s no leaves and
17527 74 68 65 20 6c 69 73 74 20 69 73 20 6e 6f 74 20 the list is not
17528 62 65 69 6e 67 20 73 65 61 72 63 68 65 64 2e 20 being searched.
17529 0a 20 20 20 20 20 20 20 20 2a 2a 20 53 6f 20 65 . ** So e
1752a 78 74 72 61 63 74 20 74 68 65 20 74 72 75 6e 6b xtract the trunk
1752b 20 70 61 67 65 20 69 74 73 65 6c 66 20 61 6e 64 page itself and
1752c 20 75 73 65 20 69 74 20 61 73 20 74 68 65 20 6e use it as the n
1752d 65 77 6c 79 20 0a 20 20 20 20 20 20 20 20 2a 2a ewly . **
1752e 20 61 6c 6c 6f 63 61 74 65 64 20 70 61 67 65 20 allocated page
1752f 2a 2f 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 */. asser
17530 74 28 20 70 50 72 65 76 54 72 75 6e 6b 3d 3d 30 t( pPrevTrunk==0
17531 20 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d );. rc =
17532 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 sqlite3PagerWri
17533 74 65 28 70 54 72 75 6e 6b 2d 3e 70 44 62 50 61 te(pTrunk->pDbPa
17534 67 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 ge);. if(
17535 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20 rc ){.
17536 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 goto end_alloca
17537 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 20 te_page;.
17538 20 7d 0a 20 20 20 20 20 20 20 20 2a 70 50 67 6e }. *pPgn
17539 6f 20 3d 20 69 54 72 75 6e 6b 3b 0a 20 20 20 20 o = iTrunk;.
1753a 20 20 20 20 6d 65 6d 63 70 79 28 26 70 50 61 67 memcpy(&pPag
1753b 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d 2c 20 26 e1->aData[32], &
1753c 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d pTrunk->aData[0]
1753d 2c 20 34 29 3b 0a 20 20 20 20 20 20 20 20 2a 70 , 4);. *p
1753e 70 50 61 67 65 20 3d 20 70 54 72 75 6e 6b 3b 0a pPage = pTrunk;.
1753f 20 20 20 20 20 20 20 20 70 54 72 75 6e 6b 20 3d pTrunk =
17540 20 30 3b 0a 20 20 20 20 20 20 20 20 54 52 41 43 0;. TRAC
17541 45 28 28 22 41 4c 4c 4f 43 41 54 45 3a 20 25 64 E(("ALLOCATE: %d
17542 20 74 72 75 6e 6b 20 2d 20 25 64 20 66 72 65 65 trunk - %d free
17543 20 70 61 67 65 73 20 6c 65 66 74 5c 6e 22 2c 20 pages left\n",
17544 2a 70 50 67 6e 6f 2c 20 6e 2d 31 29 29 3b 0a 20 *pPgno, n-1));.
17545 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6b }else if( k
17546 3e 28 75 33 32 29 28 70 42 74 2d 3e 75 73 61 62 >(u32)(pBt->usab
17547 6c 65 53 69 7a 65 2f 34 20 2d 20 32 29 20 29 7b leSize/4 - 2) ){
17548 0a 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 . /* Valu
17549 65 20 6f 66 20 6b 20 69 73 20 6f 75 74 20 6f 66 e of k is out of
1754a 20 72 61 6e 67 65 2e 20 20 44 61 74 61 62 61 73 range. Databas
1754b 65 20 63 6f 72 72 75 70 74 69 6f 6e 20 2a 2f 0a e corruption */.
1754c 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c rc = SQL
1754d 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
1754e 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 ;. goto e
1754f 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 nd_allocate_page
17550 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ;.#ifndef SQLITE
17551 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d _OMIT_AUTOVACUUM
17552 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 . }else if(
17553 20 73 65 61 72 63 68 4c 69 73 74 20 26 26 20 6e searchList && n
17554 65 61 72 62 79 3d 3d 69 54 72 75 6e 6b 20 29 7b earby==iTrunk ){
17555 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 . /* The
17556 6c 69 73 74 20 69 73 20 62 65 69 6e 67 20 73 65 list is being se
17557 61 72 63 68 65 64 20 61 6e 64 20 74 68 69 73 20 arched and this
17558 74 72 75 6e 6b 20 70 61 67 65 20 69 73 20 74 68 trunk page is th
17559 65 20 70 61 67 65 0a 20 20 20 20 20 20 20 20 2a e page. *
1755a 2a 20 74 6f 20 61 6c 6c 6f 63 61 74 65 2c 20 72 * to allocate, r
1755b 65 67 61 72 64 6c 65 73 73 20 6f 66 20 77 68 65 egardless of whe
1755c 74 68 65 72 20 69 74 20 68 61 73 20 6c 65 61 76 ther it has leav
1755d 65 73 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 es.. */.
1755e 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 2a assert( *
1755f 70 50 67 6e 6f 3d 3d 69 54 72 75 6e 6b 20 29 3b pPgno==iTrunk );
17560 0a 20 20 20 20 20 20 20 20 2a 70 70 50 61 67 65 . *ppPage
17561 20 3d 20 70 54 72 75 6e 6b 3b 0a 20 20 20 20 20 = pTrunk;.
17562 20 20 20 73 65 61 72 63 68 4c 69 73 74 20 3d 20 searchList =
17563 30 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 0;. rc =
17564 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 sqlite3PagerWrit
17565 65 28 70 54 72 75 6e 6b 2d 3e 70 44 62 50 61 67 e(pTrunk->pDbPag
17566 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 e);. if(
17567 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 rc ){.
17568 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 goto end_allocat
17569 65 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 20 20 e_page;.
1756a 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 6b 3d }. if( k=
1756b 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 =0 ){.
1756c 69 66 28 20 21 70 50 72 65 76 54 72 75 6e 6b 20 if( !pPrevTrunk
1756d 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 6d ){. m
1756e 65 6d 63 70 79 28 26 70 50 61 67 65 31 2d 3e 61 emcpy(&pPage1->a
1756f 44 61 74 61 5b 33 32 5d 2c 20 26 70 54 72 75 6e Data[32], &pTrun
17570 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 34 29 3b k->aData[0], 4);
17571 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 . }else
17572 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 6d 65 {. me
17573 6d 63 70 79 28 26 70 50 72 65 76 54 72 75 6e 6b mcpy(&pPrevTrunk
17574 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 26 70 54 72 ->aData[0], &pTr
17575 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 34 unk->aData[0], 4
17576 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 );. }.
17577 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }else{.
17578 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 /* The t
17579 72 75 6e 6b 20 70 61 67 65 20 69 73 20 72 65 71 runk page is req
1757a 75 69 72 65 64 20 62 79 20 74 68 65 20 63 61 6c uired by the cal
1757b 6c 65 72 20 62 75 74 20 69 74 20 63 6f 6e 74 61 ler but it conta
1757c 69 6e 73 20 0a 20 20 20 20 20 20 20 20 20 20 2a ins . *
1757d 2a 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 66 72 * pointers to fr
1757e 65 65 2d 6c 69 73 74 20 6c 65 61 76 65 73 2e 20 ee-list leaves.
1757f 54 68 65 20 66 69 72 73 74 20 6c 65 61 66 20 62 The first leaf b
17580 65 63 6f 6d 65 73 20 61 20 74 72 75 6e 6b 0a 20 ecomes a trunk.
17581 20 20 20 20 20 20 20 20 20 2a 2a 20 70 61 67 65 ** page
17582 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a 20 in this case..
17583 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 */.
17584 20 20 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 MemPage *p
17585 4e 65 77 54 72 75 6e 6b 3b 0a 20 20 20 20 20 20 NewTrunk;.
17586 20 20 20 20 50 67 6e 6f 20 69 4e 65 77 54 72 75 Pgno iNewTru
17587 6e 6b 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 nk = get4byte(&p
17588 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 38 5d 29 Trunk->aData[8])
17589 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 ;. if(
1758a 69 4e 65 77 54 72 75 6e 6b 3e 6d 78 50 61 67 65 iNewTrunk>mxPage
1758b 20 29 7b 20 0a 20 20 20 20 20 20 20 20 20 20 20 ){ .
1758c 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 rc = SQLITE_COR
1758d 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 RUPT_BKPT;.
1758e 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f goto end_
1758f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 allocate_page;.
17590 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
17591 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 testcase( i
17592 4e 65 77 54 72 75 6e 6b 3d 3d 6d 78 50 61 67 65 NewTrunk==mxPage
17593 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63 );. rc
17594 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 28 = btreeGetPage(
17595 70 42 74 2c 20 69 4e 65 77 54 72 75 6e 6b 2c 20 pBt, iNewTrunk,
17596 26 70 4e 65 77 54 72 75 6e 6b 2c 20 30 29 3b 0a &pNewTrunk, 0);.
17597 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 if( rc
17598 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
17599 20 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 goto
1759a 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 end_allocate_pag
1759b 65 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 e;. }.
1759c 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 rc = sq
1759d 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 lite3PagerWrite(
1759e 70 4e 65 77 54 72 75 6e 6b 2d 3e 70 44 62 50 61 pNewTrunk->pDbPa
1759f 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 ge);. i
175a0 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
175a1 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
175a2 72 65 6c 65 61 73 65 50 61 67 65 28 70 4e 65 77 releasePage(pNew
175a3 54 72 75 6e 6b 29 3b 0a 20 20 20 20 20 20 20 20 Trunk);.
175a4 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c goto end_all
175a5 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 ocate_page;.
175a6 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
175a7 20 20 6d 65 6d 63 70 79 28 26 70 4e 65 77 54 72 memcpy(&pNewTr
175a8 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 26 unk->aData[0], &
175a9 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d pTrunk->aData[0]
175aa 2c 20 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 , 4);.
175ab 70 75 74 34 62 79 74 65 28 26 70 4e 65 77 54 72 put4byte(&pNewTr
175ac 75 6e 6b 2d 3e 61 44 61 74 61 5b 34 5d 2c 20 6b unk->aData[4], k
175ad 2d 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6d -1);. m
175ae 65 6d 63 70 79 28 26 70 4e 65 77 54 72 75 6e 6b emcpy(&pNewTrunk
175af 2d 3e 61 44 61 74 61 5b 38 5d 2c 20 26 70 54 72 ->aData[8], &pTr
175b0 75 6e 6b 2d 3e 61 44 61 74 61 5b 31 32 5d 2c 20 unk->aData[12],
175b1 28 6b 2d 31 29 2a 34 29 3b 0a 20 20 20 20 20 20 (k-1)*4);.
175b2 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 releasePage(
175b3 70 4e 65 77 54 72 75 6e 6b 29 3b 0a 20 20 20 20 pNewTrunk);.
175b4 20 20 20 20 20 20 69 66 28 20 21 70 50 72 65 76 if( !pPrev
175b5 54 72 75 6e 6b 20 29 7b 0a 20 20 20 20 20 20 20 Trunk ){.
175b6 20 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c assert( sql
175b7 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 ite3PagerIswrite
175b8 61 62 6c 65 28 70 50 61 67 65 31 2d 3e 70 44 62 able(pPage1->pDb
175b9 50 61 67 65 29 20 29 3b 0a 20 20 20 20 20 20 20 Page) );.
175ba 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26 70 put4byte(&p
175bb 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d Page1->aData[32]
175bc 2c 20 69 4e 65 77 54 72 75 6e 6b 29 3b 0a 20 20 , iNewTrunk);.
175bd 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }else{.
175be 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 rc =
175bf 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 sqlite3PagerWrit
175c0 65 28 70 50 72 65 76 54 72 75 6e 6b 2d 3e 70 44 e(pPrevTrunk->pD
175c1 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 bPage);.
175c2 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 if( rc ){.
175c3 20 20 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f goto
175c4 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 end_allocate_pa
175c5 67 65 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ge;.
175c6 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 75 }. pu
175c7 74 34 62 79 74 65 28 26 70 50 72 65 76 54 72 75 t4byte(&pPrevTru
175c8 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 69 4e nk->aData[0], iN
175c9 65 77 54 72 75 6e 6b 29 3b 0a 20 20 20 20 20 20 ewTrunk);.
175ca 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a }. }.
175cb 20 20 20 20 20 20 20 20 70 54 72 75 6e 6b 20 3d pTrunk =
175cc 20 30 3b 0a 20 20 20 20 20 20 20 20 54 52 41 43 0;. TRAC
175cd 45 28 28 22 41 4c 4c 4f 43 41 54 45 3a 20 25 64 E(("ALLOCATE: %d
175ce 20 74 72 75 6e 6b 20 2d 20 25 64 20 66 72 65 65 trunk - %d free
175cf 20 70 61 67 65 73 20 6c 65 66 74 5c 6e 22 2c 20 pages left\n",
175d0 2a 70 50 67 6e 6f 2c 20 6e 2d 31 29 29 3b 0a 23 *pPgno, n-1));.#
175d1 65 6e 64 69 66 0a 20 20 20 20 20 20 7d 65 6c 73 endif. }els
175d2 65 20 69 66 28 20 6b 3e 30 20 29 7b 0a 20 20 20 e if( k>0 ){.
175d3 20 20 20 20 20 2f 2a 20 45 78 74 72 61 63 74 20 /* Extract
175d4 61 20 6c 65 61 66 20 66 72 6f 6d 20 74 68 65 20 a leaf from the
175d5 74 72 75 6e 6b 20 2a 2f 0a 20 20 20 20 20 20 20 trunk */.
175d6 20 75 33 32 20 63 6c 6f 73 65 73 74 3b 0a 20 20 u32 closest;.
175d7 20 20 20 20 20 20 50 67 6e 6f 20 69 50 61 67 65 Pgno iPage
175d8 3b 0a 20 20 20 20 20 20 20 20 75 6e 73 69 67 6e ;. unsign
175d9 65 64 20 63 68 61 72 20 2a 61 44 61 74 61 20 3d ed char *aData =
175da 20 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 3b 0a pTrunk->aData;.
175db 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c rc = sql
175dc 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 ite3PagerWrite(p
175dd 54 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b Trunk->pDbPage);
175de 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 20 . if( rc
175df 29 7b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 ){. got
175e0 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 o end_allocate_p
175e1 61 67 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 age;. }.
175e2 20 20 20 20 20 20 20 69 66 28 20 6e 65 61 72 62 if( nearb
175e3 79 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 y>0 ){.
175e4 20 75 33 32 20 69 3b 0a 20 20 20 20 20 20 20 20 u32 i;.
175e5 20 20 69 6e 74 20 64 69 73 74 3b 0a 20 20 20 20 int dist;.
175e6 20 20 20 20 20 20 63 6c 6f 73 65 73 74 20 3d 20 closest =
175e7 30 3b 0a 20 20 20 20 20 20 20 20 20 20 64 69 73 0;. dis
175e8 74 20 3d 20 67 65 74 34 62 79 74 65 28 26 61 44 t = get4byte(&aD
175e9 61 74 61 5b 38 5d 29 20 2d 20 6e 65 61 72 62 79 ata[8]) - nearby
175ea 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 ;. if(
175eb 64 69 73 74 3c 30 20 29 20 64 69 73 74 20 3d 20 dist<0 ) dist =
175ec 2d 64 69 73 74 3b 0a 20 20 20 20 20 20 20 20 20 -dist;.
175ed 20 66 6f 72 28 69 3d 31 3b 20 69 3c 6b 3b 20 69 for(i=1; i<k; i
175ee 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 ++){.
175ef 20 69 6e 74 20 64 32 20 3d 20 67 65 74 34 62 79 int d2 = get4by
175f0 74 65 28 26 61 44 61 74 61 5b 38 2b 69 2a 34 5d te(&aData[8+i*4]
175f1 29 20 2d 20 6e 65 61 72 62 79 3b 0a 20 20 20 20 ) - nearby;.
175f2 20 20 20 20 20 20 20 20 69 66 28 20 64 32 3c 30 if( d2<0
175f3 20 29 20 64 32 20 3d 20 2d 64 32 3b 0a 20 20 20 ) d2 = -d2;.
175f4 20 20 20 20 20 20 20 20 20 69 66 28 20 64 32 3c if( d2<
175f5 64 69 73 74 20 29 7b 0a 20 20 20 20 20 20 20 20 dist ){.
175f6 20 20 20 20 20 20 63 6c 6f 73 65 73 74 20 3d 20 closest =
175f7 69 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 i;.
175f8 20 64 69 73 74 20 3d 20 64 32 3b 0a 20 20 20 20 dist = d2;.
175f9 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
175fa 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 }. }e
175fb 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 63 lse{. c
175fc 6c 6f 73 65 73 74 20 3d 20 30 3b 0a 20 20 20 20 losest = 0;.
175fd 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 69 }.. i
175fe 50 61 67 65 20 3d 20 67 65 74 34 62 79 74 65 28 Page = get4byte(
175ff 26 61 44 61 74 61 5b 38 2b 63 6c 6f 73 65 73 74 &aData[8+closest
17600 2a 34 5d 29 3b 0a 20 20 20 20 20 20 20 20 74 65 *4]);. te
17601 73 74 63 61 73 65 28 20 69 50 61 67 65 3d 3d 6d stcase( iPage==m
17602 78 50 61 67 65 20 29 3b 0a 20 20 20 20 20 20 20 xPage );.
17603 20 69 66 28 20 69 50 61 67 65 3e 6d 78 50 61 67 if( iPage>mxPag
17604 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 e ){. r
17605 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 c = SQLITE_CORRU
17606 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 20 PT_BKPT;.
17607 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f goto end_allo
17608 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 20 cate_page;.
17609 20 20 20 7d 0a 20 20 20 20 20 20 20 20 74 65 73 }. tes
1760a 74 63 61 73 65 28 20 69 50 61 67 65 3d 3d 6d 78 tcase( iPage==mx
1760b 50 61 67 65 20 29 3b 0a 20 20 20 20 20 20 20 20 Page );.
1760c 69 66 28 20 21 73 65 61 72 63 68 4c 69 73 74 20 if( !searchList
1760d 7c 7c 20 69 50 61 67 65 3d 3d 6e 65 61 72 62 79 || iPage==nearby
1760e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e ){. in
1760f 74 20 6e 6f 43 6f 6e 74 65 6e 74 3b 0a 20 20 20 t noContent;.
17610 20 20 20 20 20 20 20 2a 70 50 67 6e 6f 20 3d 20 *pPgno =
17611 69 50 61 67 65 3b 0a 20 20 20 20 20 20 20 20 20 iPage;.
17612 20 54 52 41 43 45 28 28 22 41 4c 4c 4f 43 41 54 TRACE(("ALLOCAT
17613 45 3a 20 25 64 20 77 61 73 20 6c 65 61 66 20 25 E: %d was leaf %
17614 64 20 6f 66 20 25 64 20 6f 6e 20 74 72 75 6e 6b d of %d on trunk
17615 20 25 64 22 0a 20 20 20 20 20 20 20 20 20 20 20 %d".
17616 20 20 20 20 20 20 22 3a 20 25 64 20 6d 6f 72 65 ": %d more
17617 20 66 72 65 65 20 70 61 67 65 73 5c 6e 22 2c 0a free pages\n",.
17618 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17619 20 2a 70 50 67 6e 6f 2c 20 63 6c 6f 73 65 73 74 *pPgno, closest
1761a 2b 31 2c 20 6b 2c 20 70 54 72 75 6e 6b 2d 3e 70 +1, k, pTrunk->p
1761b 67 6e 6f 2c 20 6e 2d 31 29 29 3b 0a 20 20 20 20 gno, n-1));.
1761c 20 20 20 20 20 20 69 66 28 20 63 6c 6f 73 65 73 if( closes
1761d 74 3c 6b 2d 31 20 29 7b 0a 20 20 20 20 20 20 20 t<k-1 ){.
1761e 20 20 20 20 20 6d 65 6d 63 70 79 28 26 61 44 61 memcpy(&aDa
1761f 74 61 5b 38 2b 63 6c 6f 73 65 73 74 2a 34 5d 2c ta[8+closest*4],
17620 20 26 61 44 61 74 61 5b 34 2b 6b 2a 34 5d 2c 20 &aData[4+k*4],
17621 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 4);. }.
17622 20 20 20 20 20 20 20 20 20 20 70 75 74 34 62 79 put4by
17623 74 65 28 26 61 44 61 74 61 5b 34 5d 2c 20 6b 2d te(&aData[4], k-
17624 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73 1);. as
17625 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 sert( sqlite3Pag
17626 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 54 erIswriteable(pT
17627 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 20 29 runk->pDbPage) )
17628 3b 0a 20 20 20 20 20 20 20 20 20 20 6e 6f 43 6f ;. noCo
17629 6e 74 65 6e 74 20 3d 20 21 62 74 72 65 65 47 65 ntent = !btreeGe
1762a 74 48 61 73 43 6f 6e 74 65 6e 74 28 70 42 74 2c tHasContent(pBt,
1762b 20 2a 70 50 67 6e 6f 29 3b 0a 20 20 20 20 20 20 *pPgno);.
1762c 20 20 20 20 72 63 20 3d 20 62 74 72 65 65 47 65 rc = btreeGe
1762d 74 50 61 67 65 28 70 42 74 2c 20 2a 70 50 67 6e tPage(pBt, *pPgn
1762e 6f 2c 20 70 70 50 61 67 65 2c 20 6e 6f 43 6f 6e o, ppPage, noCon
1762f 74 65 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 20 tent);.
17630 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
17631 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 OK ){.
17632 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
17633 67 65 72 57 72 69 74 65 28 28 2a 70 70 50 61 67 gerWrite((*ppPag
17634 65 29 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 e)->pDbPage);.
17635 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 if( rc
17636 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
17637 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65 6c rel
17638 65 61 73 65 50 61 67 65 28 2a 70 70 50 61 67 65 easePage(*ppPage
17639 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d );. }
1763a 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 . }.
1763b 20 20 20 20 20 20 20 73 65 61 72 63 68 4c 69 73 searchLis
1763c 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d t = 0;. }
1763d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 . }. r
1763e 65 6c 65 61 73 65 50 61 67 65 28 70 50 72 65 76 eleasePage(pPrev
1763f 54 72 75 6e 6b 29 3b 0a 20 20 20 20 20 20 70 50 Trunk);. pP
17640 72 65 76 54 72 75 6e 6b 20 3d 20 30 3b 0a 20 20 revTrunk = 0;.
17641 20 20 7d 77 68 69 6c 65 28 20 73 65 61 72 63 68 }while( search
17642 4c 69 73 74 20 29 3b 0a 20 20 7d 65 6c 73 65 7b List );. }else{
17643 0a 20 20 20 20 2f 2a 20 54 68 65 72 65 20 61 72 . /* There ar
17644 65 20 6e 6f 20 70 61 67 65 73 20 6f 6e 20 74 68 e no pages on th
17645 65 20 66 72 65 65 6c 69 73 74 2c 20 73 6f 20 63 e freelist, so c
17646 72 65 61 74 65 20 61 20 6e 65 77 20 70 61 67 65 reate a new page
17647 20 61 74 20 74 68 65 0a 20 20 20 20 2a 2a 20 65 at the. ** e
17648 6e 64 20 6f 66 20 74 68 65 20 66 69 6c 65 20 2a nd of the file *
17649 2f 0a 20 20 20 20 69 6e 74 20 6e 50 61 67 65 20 /. int nPage
1764a 3d 20 70 61 67 65 72 50 61 67 65 63 6f 75 6e 74 = pagerPagecount
1764b 28 70 42 74 29 3b 0a 20 20 20 20 2a 70 50 67 6e (pBt);. *pPgn
1764c 6f 20 3d 20 6e 50 61 67 65 20 2b 20 31 3b 0a 0a o = nPage + 1;..
1764d 20 20 20 20 69 66 28 20 2a 70 50 67 6e 6f 3d 3d if( *pPgno==
1764e 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 PENDING_BYTE_PAG
1764f 45 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 E(pBt) ){.
17650 28 2a 70 50 67 6e 6f 29 2b 2b 3b 0a 20 20 20 20 (*pPgno)++;.
17651 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 }..#ifndef SQLIT
17652 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 E_OMIT_AUTOVACUU
17653 4d 0a 20 20 20 20 69 66 28 20 70 42 74 2d 3e 61 M. if( pBt->a
17654 75 74 6f 56 61 63 75 75 6d 20 26 26 20 50 54 52 utoVacuum && PTR
17655 4d 41 50 5f 49 53 50 41 47 45 28 70 42 74 2c 20 MAP_ISPAGE(pBt,
17656 2a 70 50 67 6e 6f 29 20 29 7b 0a 20 20 20 20 20 *pPgno) ){.
17657 20 2f 2a 20 49 66 20 2a 70 50 67 6e 6f 20 72 65 /* If *pPgno re
17658 66 65 72 73 20 74 6f 20 61 20 70 6f 69 6e 74 65 fers to a pointe
17659 72 2d 6d 61 70 20 70 61 67 65 2c 20 61 6c 6c 6f r-map page, allo
1765a 63 61 74 65 20 74 77 6f 20 6e 65 77 20 70 61 67 cate two new pag
1765b 65 73 0a 20 20 20 20 20 20 2a 2a 20 61 74 20 74 es. ** at t
1765c 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 66 69 he end of the fi
1765d 6c 65 20 69 6e 73 74 65 61 64 20 6f 66 20 6f 6e le instead of on
1765e 65 2e 20 54 68 65 20 66 69 72 73 74 20 61 6c 6c e. The first all
1765f 6f 63 61 74 65 64 20 70 61 67 65 0a 20 20 20 20 ocated page.
17660 20 20 2a 2a 20 62 65 63 6f 6d 65 73 20 61 20 6e ** becomes a n
17661 65 77 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 ew pointer-map p
17662 61 67 65 2c 20 74 68 65 20 73 65 63 6f 6e 64 20 age, the second
17663 69 73 20 75 73 65 64 20 62 79 20 74 68 65 20 63 is used by the c
17664 61 6c 6c 65 72 2e 0a 20 20 20 20 20 20 2a 2f 0a aller.. */.
17665 20 20 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 MemPage *p
17666 50 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 54 52 Pg = 0;. TR
17667 41 43 45 28 28 22 41 4c 4c 4f 43 41 54 45 3a 20 ACE(("ALLOCATE:
17668 25 64 20 66 72 6f 6d 20 65 6e 64 20 6f 66 20 66 %d from end of f
17669 69 6c 65 20 28 70 6f 69 6e 74 65 72 2d 6d 61 70 ile (pointer-map
1766a 20 70 61 67 65 29 5c 6e 22 2c 20 2a 70 50 67 6e page)\n", *pPgn
1766b 6f 29 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 o));. asser
1766c 74 28 20 2a 70 50 67 6e 6f 21 3d 50 45 4e 44 49 t( *pPgno!=PENDI
1766d 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 NG_BYTE_PAGE(pBt
1766e 29 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 ) );. rc =
1766f 62 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74 btreeGetPage(pBt
17670 2c 20 2a 70 50 67 6e 6f 2c 20 26 70 50 67 2c 20 , *pPgno, &pPg,
17671 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 0);. if( rc
17672 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
17673 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
17674 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 te3PagerWrite(pP
17675 67 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 g->pDbPage);.
17676 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 releasePage
17677 28 70 50 67 29 3b 0a 20 20 20 20 20 20 7d 0a 20 (pPg);. }.
17678 20 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 if( rc ) re
17679 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 28 turn rc;. (
1767a 2a 70 50 67 6e 6f 29 2b 2b 3b 0a 20 20 20 20 20 *pPgno)++;.
1767b 20 69 66 28 20 2a 70 50 67 6e 6f 3d 3d 50 45 4e if( *pPgno==PEN
1767c 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 DING_BYTE_PAGE(p
1767d 42 74 29 20 29 7b 20 28 2a 70 50 67 6e 6f 29 2b Bt) ){ (*pPgno)+
1767e 2b 3b 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 +; }. }.#endi
1767f 66 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 2a f.. assert( *
17680 70 50 67 6e 6f 21 3d 50 45 4e 44 49 4e 47 5f 42 pPgno!=PENDING_B
17681 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 3b YTE_PAGE(pBt) );
17682 0a 20 20 20 20 72 63 20 3d 20 62 74 72 65 65 47 . rc = btreeG
17683 65 74 50 61 67 65 28 70 42 74 2c 20 2a 70 50 67 etPage(pBt, *pPg
17684 6e 6f 2c 20 70 70 50 61 67 65 2c 20 30 29 3b 0a no, ppPage, 0);.
17685 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 if( rc ) ret
17686 75 72 6e 20 72 63 3b 0a 20 20 20 20 72 63 20 3d urn rc;. rc =
17687 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 sqlite3PagerWri
17688 74 65 28 28 2a 70 70 50 61 67 65 29 2d 3e 70 44 te((*ppPage)->pD
17689 62 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 bPage);. if(
1768a 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
1768b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 . releasePa
1768c 67 65 28 2a 70 70 50 61 67 65 29 3b 0a 20 20 20 ge(*ppPage);.
1768d 20 7d 0a 20 20 20 20 54 52 41 43 45 28 28 22 41 }. TRACE(("A
1768e 4c 4c 4f 43 41 54 45 3a 20 25 64 20 66 72 6f 6d LLOCATE: %d from
1768f 20 65 6e 64 20 6f 66 20 66 69 6c 65 5c 6e 22 2c end of file\n",
17690 20 2a 70 50 67 6e 6f 29 29 3b 0a 20 20 7d 0a 0a *pPgno));. }..
17691 20 20 61 73 73 65 72 74 28 20 2a 70 50 67 6e 6f assert( *pPgno
17692 21 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 !=PENDING_BYTE_P
17693 41 47 45 28 70 42 74 29 20 29 3b 0a 0a 65 6e 64 AGE(pBt) );..end
17694 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3a 0a _allocate_page:.
17695 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 54 releasePage(pT
17696 72 75 6e 6b 29 3b 0a 20 20 72 65 6c 65 61 73 65 runk);. release
17697 50 61 67 65 28 70 50 72 65 76 54 72 75 6e 6b 29 Page(pPrevTrunk)
17698 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 ;. if( rc==SQLI
17699 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 TE_OK ){. if(
1769a 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 sqlite3PagerPag
1769b 65 52 65 66 63 6f 75 6e 74 28 28 2a 70 70 50 61 eRefcount((*ppPa
1769c 67 65 29 2d 3e 70 44 62 50 61 67 65 29 3e 31 20 ge)->pDbPage)>1
1769d 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 ){. release
1769e 50 61 67 65 28 2a 70 70 50 61 67 65 29 3b 0a 20 Page(*ppPage);.
1769f 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
176a0 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b TE_CORRUPT_BKPT;
176a1 0a 20 20 20 20 7d 0a 20 20 20 20 28 2a 70 70 50 . }. (*ppP
176a2 61 67 65 29 2d 3e 69 73 49 6e 69 74 20 3d 20 30 age)->isInit = 0
176a3 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2a ;. }else{. *
176a4 70 70 50 61 67 65 20 3d 20 30 3b 0a 20 20 7d 0a ppPage = 0;. }.
176a5 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
176a6 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 /*.** This funct
176a7 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 61 ion is used to a
176a8 64 64 20 70 61 67 65 20 69 50 61 67 65 20 74 6f dd page iPage to
176a9 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
176aa 6c 65 20 66 72 65 65 2d 6c 69 73 74 2e 20 0a 2a le free-list. .*
176ab 2a 20 49 74 20 69 73 20 61 73 73 75 6d 65 64 20 * It is assumed
176ac 74 68 61 74 20 74 68 65 20 70 61 67 65 20 69 73 that the page is
176ad 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 61 20 70 not already a p
176ae 61 72 74 20 6f 66 20 74 68 65 20 66 72 65 65 2d art of the free-
176af 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 list..**.** The
176b0 76 61 6c 75 65 20 70 61 73 73 65 64 20 61 73 20 value passed as
176b1 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d the second argum
176b2 65 6e 74 20 74 6f 20 74 68 69 73 20 66 75 6e 63 ent to this func
176b3 74 69 6f 6e 20 69 73 20 6f 70 74 69 6f 6e 61 6c tion is optional
176b4 2e 0a 2a 2a 20 49 66 20 74 68 65 20 63 61 6c 6c ..** If the call
176b5 65 72 20 68 61 70 70 65 6e 73 20 74 6f 20 68 61 er happens to ha
176b6 76 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 ve a pointer to
176b7 74 68 65 20 4d 65 6d 50 61 67 65 20 6f 62 6a 65 the MemPage obje
176b8 63 74 20 0a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e ct .** correspon
176b9 64 69 6e 67 20 74 6f 20 70 61 67 65 20 69 50 61 ding to page iPa
176ba 67 65 20 68 61 6e 64 79 2c 20 69 74 20 6d 61 79 ge handy, it may
176bb 20 70 61 73 73 20 69 74 20 61 73 20 74 68 65 20 pass it as the
176bc 73 65 63 6f 6e 64 20 76 61 6c 75 65 2e 20 0a 2a second value. .*
176bd 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 69 74 20 * Otherwise, it
176be 6d 61 79 20 70 61 73 73 20 4e 55 4c 4c 2e 0a 2a may pass NULL..*
176bf 2a 0a 2a 2a 20 49 66 20 61 20 70 6f 69 6e 74 65 *.** If a pointe
176c0 72 20 74 6f 20 61 20 4d 65 6d 50 61 67 65 20 6f r to a MemPage o
176c1 62 6a 65 63 74 20 69 73 20 70 61 73 73 65 64 20 bject is passed
176c2 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 as the second ar
176c3 67 75 6d 65 6e 74 2c 0a 2a 2a 20 69 74 73 20 72 gument,.** its r
176c4 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 69 eference count i
176c5 73 20 6e 6f 74 20 61 6c 74 65 72 65 64 20 62 79 s not altered by
176c6 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a this function..
176c7 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 72 */.static int fr
176c8 65 65 50 61 67 65 32 28 42 74 53 68 61 72 65 64 eePage2(BtShared
176c9 20 2a 70 42 74 2c 20 4d 65 6d 50 61 67 65 20 2a *pBt, MemPage *
176ca 70 4d 65 6d 50 61 67 65 2c 20 50 67 6e 6f 20 69 pMemPage, Pgno i
176cb 50 61 67 65 29 7b 0a 20 20 4d 65 6d 50 61 67 65 Page){. MemPage
176cc 20 2a 70 54 72 75 6e 6b 20 3d 20 30 3b 20 20 20 *pTrunk = 0;
176cd 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
176ce 46 72 65 65 2d 6c 69 73 74 20 74 72 75 6e 6b 20 Free-list trunk
176cf 70 61 67 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 page */. Pgno i
176d0 54 72 75 6e 6b 20 3d 20 30 3b 20 20 20 20 20 20 Trunk = 0;
176d1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
176d2 20 50 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 Page number of
176d3 66 72 65 65 2d 6c 69 73 74 20 74 72 75 6e 6b 20 free-list trunk
176d4 70 61 67 65 20 2a 2f 20 0a 20 20 4d 65 6d 50 61 page */ . MemPa
176d5 67 65 20 2a 70 50 61 67 65 31 20 3d 20 70 42 74 ge *pPage1 = pBt
176d6 2d 3e 70 50 61 67 65 31 3b 20 20 20 20 20 20 2f ->pPage1; /
176d7 2a 20 4c 6f 63 61 6c 20 72 65 66 65 72 65 6e 63 * Local referenc
176d8 65 20 74 6f 20 70 61 67 65 20 31 20 2a 2f 0a 20 e to page 1 */.
176d9 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b MemPage *pPage;
176da 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
176db 20 20 20 20 20 2f 2a 20 50 61 67 65 20 62 65 69 /* Page bei
176dc 6e 67 20 66 72 65 65 64 2e 20 4d 61 79 20 62 65 ng freed. May be
176dd 20 4e 55 4c 4c 2e 20 2a 2f 0a 20 20 69 6e 74 20 NULL. */. int
176de 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 rc;
176df 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
176e0 2f 2a 20 52 65 74 75 72 6e 20 43 6f 64 65 20 2a /* Return Code *
176e1 2f 0a 20 20 69 6e 74 20 6e 46 72 65 65 3b 20 20 /. int nFree;
176e2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
176e3 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 69 74 69 /* Initi
176e4 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 al number of pag
176e5 65 73 20 6f 6e 20 66 72 65 65 2d 6c 69 73 74 20 es on free-list
176e6 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 */.. assert( sq
176e7 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
176e8 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a (pBt->mutex) );.
176e9 20 20 61 73 73 65 72 74 28 20 69 50 61 67 65 3e assert( iPage>
176ea 31 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 1 );. assert( !
176eb 70 4d 65 6d 50 61 67 65 20 7c 7c 20 70 4d 65 6d pMemPage || pMem
176ec 50 61 67 65 2d 3e 70 67 6e 6f 3d 3d 69 50 61 67 Page->pgno==iPag
176ed 65 20 29 3b 0a 0a 20 20 69 66 28 20 70 4d 65 6d e );.. if( pMem
176ee 50 61 67 65 20 29 7b 0a 20 20 20 20 70 50 61 67 Page ){. pPag
176ef 65 20 3d 20 70 4d 65 6d 50 61 67 65 3b 0a 20 20 e = pMemPage;.
176f0 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 sqlite3PagerRe
176f1 66 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 f(pPage->pDbPage
176f2 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
176f3 70 50 61 67 65 20 3d 20 62 74 72 65 65 50 61 67 pPage = btreePag
176f4 65 4c 6f 6f 6b 75 70 28 70 42 74 2c 20 69 50 61 eLookup(pBt, iPa
176f5 67 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 ge);. }.. /* I
176f6 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 66 72 65 ncrement the fre
176f7 65 20 70 61 67 65 20 63 6f 75 6e 74 20 6f 6e 20 e page count on
176f8 70 50 61 67 65 31 20 2a 2f 0a 20 20 72 63 20 3d pPage1 */. rc =
176f9 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 sqlite3PagerWri
176fa 74 65 28 70 50 61 67 65 31 2d 3e 70 44 62 50 61 te(pPage1->pDbPa
176fb 67 65 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 ge);. if( rc )
176fc 67 6f 74 6f 20 66 72 65 65 70 61 67 65 5f 6f 75 goto freepage_ou
176fd 74 3b 0a 20 20 6e 46 72 65 65 20 3d 20 67 65 74 t;. nFree = get
176fe 34 62 79 74 65 28 26 70 50 61 67 65 31 2d 3e 61 4byte(&pPage1->a
176ff 44 61 74 61 5b 33 36 5d 29 3b 0a 20 20 70 75 74 Data[36]);. put
17700 34 62 79 74 65 28 26 70 50 61 67 65 31 2d 3e 61 4byte(&pPage1->a
17701 44 61 74 61 5b 33 36 5d 2c 20 6e 46 72 65 65 2b Data[36], nFree+
17702 31 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 1);..#ifdef SQLI
17703 54 45 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 45 TE_SECURE_DELETE
17704 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 53 51 4c . /* If the SQL
17705 49 54 45 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 ITE_SECURE_DELET
17706 45 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f E compile-time o
17707 70 74 69 6f 6e 20 69 73 20 65 6e 61 62 6c 65 64 ption is enabled
17708 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 61 6c 77 61 , then. ** alwa
17709 79 73 20 66 75 6c 6c 79 20 6f 76 65 72 77 72 69 ys fully overwri
1770a 74 65 20 64 65 6c 65 74 65 64 20 69 6e 66 6f 72 te deleted infor
1770b 6d 61 74 69 6f 6e 20 77 69 74 68 20 7a 65 72 6f mation with zero
1770c 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 21 s.. */. if( (!
1770d 70 50 61 67 65 20 26 26 20 28 72 63 20 3d 20 62 pPage && (rc = b
1770e 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c treeGetPage(pBt,
1770f 20 69 50 61 67 65 2c 20 26 70 50 61 67 65 2c 20 iPage, &pPage,
17710 30 29 29 29 0a 20 20 20 7c 7c 20 20 20 20 20 20 0))). ||
17711 20 20 20 20 20 20 28 72 63 20 3d 20 73 71 6c 69 (rc = sqli
17712 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 te3PagerWrite(pP
17713 61 67 65 2d 3e 70 44 62 50 61 67 65 29 29 0a 20 age->pDbPage)).
17714 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 66 72 65 ){. goto fre
17715 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 7d 0a 20 epage_out;. }.
17716 20 6d 65 6d 73 65 74 28 70 50 61 67 65 2d 3e 61 memset(pPage->a
17717 44 61 74 61 2c 20 30 2c 20 70 50 61 67 65 2d 3e Data, 0, pPage->
17718 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a pBt->pageSize);.
17719 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 66 20 #endif.. /* If
1771a 74 68 65 20 64 61 74 61 62 61 73 65 20 73 75 70 the database sup
1771b 70 6f 72 74 73 20 61 75 74 6f 2d 76 61 63 75 75 ports auto-vacuu
1771c 6d 2c 20 77 72 69 74 65 20 61 6e 20 65 6e 74 72 m, write an entr
1771d 79 20 69 6e 20 74 68 65 20 70 6f 69 6e 74 65 72 y in the pointer
1771e 2d 6d 61 70 0a 20 20 2a 2a 20 74 6f 20 69 6e 64 -map. ** to ind
1771f 69 63 61 74 65 20 74 68 61 74 20 74 68 65 20 70 icate that the p
17720 61 67 65 20 69 73 20 66 72 65 65 2e 0a 20 20 2a age is free.. *
17721 2f 0a 20 20 69 66 28 20 49 53 41 55 54 4f 56 41 /. if( ISAUTOVA
17722 43 55 55 4d 20 29 7b 0a 20 20 20 20 70 74 72 6d CUUM ){. ptrm
17723 61 70 50 75 74 28 70 42 74 2c 20 69 50 61 67 65 apPut(pBt, iPage
17724 2c 20 50 54 52 4d 41 50 5f 46 52 45 45 50 41 47 , PTRMAP_FREEPAG
17725 45 2c 20 30 2c 20 26 72 63 29 3b 0a 20 20 20 20 E, 0, &rc);.
17726 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 66 72 if( rc ) goto fr
17727 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 7d 0a eepage_out;. }.
17728 0a 20 20 2f 2a 20 4e 6f 77 20 6d 61 6e 69 70 75 . /* Now manipu
17729 6c 61 74 65 20 74 68 65 20 61 63 74 75 61 6c 20 late the actual
1772a 64 61 74 61 62 61 73 65 20 66 72 65 65 2d 6c 69 database free-li
1772b 73 74 20 73 74 72 75 63 74 75 72 65 2e 20 54 68 st structure. Th
1772c 65 72 65 20 61 72 65 20 74 77 6f 0a 20 20 2a 2a ere are two. **
1772d 20 70 6f 73 73 69 62 69 6c 69 74 69 65 73 2e 20 possibilities.
1772e 49 66 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 If the free-list
1772f 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 65 6d is currently em
17730 70 74 79 2c 20 6f 72 20 69 66 20 74 68 65 20 66 pty, or if the f
17731 69 72 73 74 0a 20 20 2a 2a 20 74 72 75 6e 6b 20 irst. ** trunk
17732 70 61 67 65 20 69 6e 20 74 68 65 20 66 72 65 65 page in the free
17733 2d 6c 69 73 74 20 69 73 20 66 75 6c 6c 2c 20 74 -list is full, t
17734 68 65 6e 20 74 68 69 73 20 70 61 67 65 20 77 69 hen this page wi
17735 6c 6c 20 62 65 63 6f 6d 65 20 61 0a 20 20 2a 2a ll become a. **
17736 20 6e 65 77 20 66 72 65 65 2d 6c 69 73 74 20 74 new free-list t
17737 72 75 6e 6b 20 70 61 67 65 2e 20 4f 74 68 65 72 runk page. Other
17738 77 69 73 65 2c 20 69 74 20 77 69 6c 6c 20 62 65 wise, it will be
17739 63 6f 6d 65 20 61 20 6c 65 61 66 20 6f 66 20 74 come a leaf of t
1773a 68 65 0a 20 20 2a 2a 20 66 69 72 73 74 20 74 72 he. ** first tr
1773b 75 6e 6b 20 70 61 67 65 20 69 6e 20 74 68 65 20 unk page in the
1773c 63 75 72 72 65 6e 74 20 66 72 65 65 2d 6c 69 73 current free-lis
1773d 74 2e 20 54 68 69 73 20 62 6c 6f 63 6b 20 74 65 t. This block te
1773e 73 74 73 20 69 66 20 69 74 0a 20 20 2a 2a 20 69 sts if it. ** i
1773f 73 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 61 64 s possible to ad
17740 64 20 74 68 65 20 70 61 67 65 20 61 73 20 61 20 d the page as a
17741 6e 65 77 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 new free-list le
17742 61 66 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e af.. */. if( n
17743 46 72 65 65 21 3d 30 20 29 7b 0a 20 20 20 20 75 Free!=0 ){. u
17744 33 32 20 6e 4c 65 61 66 3b 20 20 20 20 20 20 20 32 nLeaf;
17745 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 69 74 /* Init
17746 69 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 6c 65 ial number of le
17747 61 66 20 63 65 6c 6c 73 20 6f 6e 20 74 72 75 6e af cells on trun
17748 6b 20 70 61 67 65 20 2a 2f 0a 0a 20 20 20 20 69 k page */.. i
17749 54 72 75 6e 6b 20 3d 20 67 65 74 34 62 79 74 65 Trunk = get4byte
1774a 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b (&pPage1->aData[
1774b 33 32 5d 29 3b 0a 20 20 20 20 72 63 20 3d 20 62 32]);. rc = b
1774c 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c treeGetPage(pBt,
1774d 20 69 54 72 75 6e 6b 2c 20 26 70 54 72 75 6e 6b iTrunk, &pTrunk
1774e 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63 , 0);. if( rc
1774f 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
17750 20 20 20 20 20 67 6f 74 6f 20 66 72 65 65 70 61 goto freepa
17751 67 65 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 0a 20 ge_out;. }..
17752 20 20 20 6e 4c 65 61 66 20 3d 20 67 65 74 34 62 nLeaf = get4b
17753 79 74 65 28 26 70 54 72 75 6e 6b 2d 3e 61 44 61 yte(&pTrunk->aDa
17754 74 61 5b 34 5d 29 3b 0a 20 20 20 20 61 73 73 65 ta[4]);. asse
17755 72 74 28 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 rt( pBt->usableS
17756 69 7a 65 3e 33 32 20 29 3b 0a 20 20 20 20 69 66 ize>32 );. if
17757 28 20 6e 4c 65 61 66 20 3e 20 28 75 33 32 29 70 ( nLeaf > (u32)p
17758 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2f 34 Bt->usableSize/4
17759 20 2d 20 32 20 29 7b 0a 20 20 20 20 20 20 72 63 - 2 ){. rc
1775a 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 = SQLITE_CORRUP
1775b 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 67 6f T_BKPT;. go
1775c 74 6f 20 66 72 65 65 70 61 67 65 5f 6f 75 74 3b to freepage_out;
1775d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6e . }. if( n
1775e 4c 65 61 66 20 3c 20 28 75 33 32 29 70 42 74 2d Leaf < (u32)pBt-
1775f 3e 75 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20 >usableSize/4 -
17760 38 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 6e 8 ){. /* In
17761 20 74 68 69 73 20 63 61 73 65 20 74 68 65 72 65 this case there
17762 20 69 73 20 72 6f 6f 6d 20 6f 6e 20 74 68 65 20 is room on the
17763 74 72 75 6e 6b 20 70 61 67 65 20 74 6f 20 69 6e trunk page to in
17764 73 65 72 74 20 74 68 65 20 70 61 67 65 0a 20 20 sert the page.
17765 20 20 20 20 2a 2a 20 62 65 69 6e 67 20 66 72 65 ** being fre
17766 65 64 20 61 73 20 61 20 6e 65 77 20 6c 65 61 66 ed as a new leaf
17767 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 .. **.
17768 20 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68 ** Note that th
17769 65 20 74 72 75 6e 6b 20 70 61 67 65 20 69 73 20 e trunk page is
1776a 6e 6f 74 20 72 65 61 6c 6c 79 20 66 75 6c 6c 20 not really full
1776b 75 6e 74 69 6c 20 69 74 20 63 6f 6e 74 61 69 6e until it contain
1776c 73 0a 20 20 20 20 20 20 2a 2a 20 75 73 61 62 6c s. ** usabl
1776d 65 53 69 7a 65 2f 34 20 2d 20 32 20 65 6e 74 72 eSize/4 - 2 entr
1776e 69 65 73 2c 20 6e 6f 74 20 75 73 61 62 6c 65 53 ies, not usableS
1776f 69 7a 65 2f 34 20 2d 20 38 20 65 6e 74 72 69 65 ize/4 - 8 entrie
17770 73 20 61 73 20 77 65 20 68 61 76 65 0a 20 20 20 s as we have.
17771 20 20 20 2a 2a 20 63 6f 64 65 64 2e 20 20 42 75 ** coded. Bu
17772 74 20 64 75 65 20 74 6f 20 61 20 63 6f 64 69 6e t due to a codin
17773 67 20 65 72 72 6f 72 20 69 6e 20 76 65 72 73 69 g error in versi
17774 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 70 72 ons of SQLite pr
17775 69 6f 72 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 ior to. **
17776 33 2e 36 2e 30 2c 20 64 61 74 61 62 61 73 65 73 3.6.0, databases
17777 20 77 69 74 68 20 66 72 65 65 6c 69 73 74 20 74 with freelist t
17778 72 75 6e 6b 20 70 61 67 65 73 20 68 6f 6c 64 69 runk pages holdi
17779 6e 67 20 6d 6f 72 65 20 74 68 61 6e 0a 20 20 20 ng more than.
1777a 20 20 20 2a 2a 20 75 73 61 62 6c 65 53 69 7a 65 ** usableSize
1777b 2f 34 20 2d 20 38 20 65 6e 74 72 69 65 73 20 77 /4 - 8 entries w
1777c 69 6c 6c 20 62 65 20 72 65 70 6f 72 74 65 64 20 ill be reported
1777d 61 73 20 63 6f 72 72 75 70 74 2e 20 20 49 6e 20 as corrupt. In
1777e 6f 72 64 65 72 0a 20 20 20 20 20 20 2a 2a 20 74 order. ** t
1777f 6f 20 6d 61 69 6e 74 61 69 6e 20 62 61 63 6b 77 o maintain backw
17780 61 72 64 73 20 63 6f 6d 70 61 74 69 62 69 6c 69 ards compatibili
17781 74 79 20 77 69 74 68 20 6f 6c 64 65 72 20 76 65 ty with older ve
17782 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 rsions of SQLite
17783 2c 0a 20 20 20 20 20 20 2a 2a 20 77 65 20 77 69 ,. ** we wi
17784 6c 6c 20 63 6f 6e 74 69 6e 75 65 20 74 6f 20 72 ll continue to r
17785 65 73 74 72 69 63 74 20 74 68 65 20 6e 75 6d 62 estrict the numb
17786 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 74 6f er of entries to
17787 20 75 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20 usableSize/4 -
17788 38 0a 20 20 20 20 20 20 2a 2a 20 66 6f 72 20 6e 8. ** for n
17789 6f 77 2e 20 20 41 74 20 73 6f 6d 65 20 70 6f 69 ow. At some poi
1778a 6e 74 20 69 6e 20 74 68 65 20 66 75 74 75 72 65 nt in the future
1778b 20 28 6f 6e 63 65 20 65 76 65 72 79 6f 6e 65 20 (once everyone
1778c 68 61 73 20 75 70 67 72 61 64 65 64 0a 20 20 20 has upgraded.
1778d 20 20 20 2a 2a 20 74 6f 20 33 2e 36 2e 30 20 6f ** to 3.6.0 o
1778e 72 20 6c 61 74 65 72 29 20 77 65 20 73 68 6f 75 r later) we shou
1778f 6c 64 20 63 6f 6e 73 69 64 65 72 20 66 69 78 69 ld consider fixi
17790 6e 67 20 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e ng the condition
17791 61 6c 20 61 62 6f 76 65 0a 20 20 20 20 20 20 2a al above. *
17792 2a 20 74 6f 20 72 65 61 64 20 22 75 73 61 62 6c * to read "usabl
17793 65 53 69 7a 65 2f 34 2d 32 22 20 69 6e 73 74 65 eSize/4-2" inste
17794 61 64 20 6f 66 20 22 75 73 61 62 6c 65 53 69 7a ad of "usableSiz
17795 65 2f 34 2d 38 22 2e 0a 20 20 20 20 20 20 2a 2f e/4-8".. */
17796 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
17797 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 54 te3PagerWrite(pT
17798 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a runk->pDbPage);.
17799 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 if( rc==SQ
1779a 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
1779b 20 20 20 70 75 74 34 62 79 74 65 28 26 70 54 72 put4byte(&pTr
1779c 75 6e 6b 2d 3e 61 44 61 74 61 5b 34 5d 2c 20 6e unk->aData[4], n
1779d 4c 65 61 66 2b 31 29 3b 0a 20 20 20 20 20 20 20 Leaf+1);.
1779e 20 70 75 74 34 62 79 74 65 28 26 70 54 72 75 6e put4byte(&pTrun
1779f 6b 2d 3e 61 44 61 74 61 5b 38 2b 6e 4c 65 61 66 k->aData[8+nLeaf
177a0 2a 34 5d 2c 20 69 50 61 67 65 29 3b 0a 23 69 66 *4], iPage);.#if
177a1 6e 64 65 66 20 53 51 4c 49 54 45 5f 53 45 43 55 ndef SQLITE_SECU
177a2 52 45 5f 44 45 4c 45 54 45 0a 20 20 20 20 20 20 RE_DELETE.
177a3 20 20 69 66 28 20 70 50 61 67 65 20 29 7b 0a 20 if( pPage ){.
177a4 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
177a5 50 61 67 65 72 44 6f 6e 74 57 72 69 74 65 28 70 PagerDontWrite(p
177a6 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a Page->pDbPage);.
177a7 20 20 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 }.#endif
177a8 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 62 74 . rc = bt
177a9 72 65 65 53 65 74 48 61 73 43 6f 6e 74 65 6e 74 reeSetHasContent
177aa 28 70 42 74 2c 20 69 50 61 67 65 29 3b 0a 20 20 (pBt, iPage);.
177ab 20 20 20 20 7d 0a 20 20 20 20 20 20 54 52 41 43 }. TRAC
177ac 45 28 28 22 46 52 45 45 2d 50 41 47 45 3a 20 25 E(("FREE-PAGE: %
177ad 64 20 6c 65 61 66 20 6f 6e 20 74 72 75 6e 6b 20 d leaf on trunk
177ae 70 61 67 65 20 25 64 5c 6e 22 2c 70 50 61 67 65 page %d\n",pPage
177af 2d 3e 70 67 6e 6f 2c 70 54 72 75 6e 6b 2d 3e 70 ->pgno,pTrunk->p
177b0 67 6e 6f 29 29 3b 0a 20 20 20 20 20 20 67 6f 74 gno));. got
177b1 6f 20 66 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a o freepage_out;.
177b2 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 }. }.. /*
177b3 49 66 20 63 6f 6e 74 72 6f 6c 20 66 6c 6f 77 73 If control flows
177b4 20 74 6f 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 to this point,
177b5 74 68 65 6e 20 69 74 20 77 61 73 20 6e 6f 74 20 then it was not
177b6 70 6f 73 73 69 62 6c 65 20 74 6f 20 61 64 64 20 possible to add
177b7 74 68 65 0a 20 20 2a 2a 20 74 68 65 20 70 61 67 the. ** the pag
177b8 65 20 62 65 69 6e 67 20 66 72 65 65 64 20 61 73 e being freed as
177b9 20 61 20 6c 65 61 66 20 70 61 67 65 20 6f 66 20 a leaf page of
177ba 74 68 65 20 66 69 72 73 74 20 74 72 75 6e 6b 20 the first trunk
177bb 69 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 in the free-list
177bc 2e 0a 20 20 2a 2a 20 50 6f 73 73 69 62 6c 79 20 .. ** Possibly
177bd 62 65 63 61 75 73 65 20 74 68 65 20 66 72 65 65 because the free
177be 2d 6c 69 73 74 20 69 73 20 65 6d 70 74 79 2c 20 -list is empty,
177bf 6f 72 20 70 6f 73 73 69 62 6c 79 20 62 65 63 61 or possibly beca
177c0 75 73 65 20 74 68 65 20 0a 20 20 2a 2a 20 66 69 use the . ** fi
177c1 72 73 74 20 74 72 75 6e 6b 20 69 6e 20 74 68 65 rst trunk in the
177c2 20 66 72 65 65 2d 6c 69 73 74 20 69 73 20 66 75 free-list is fu
177c3 6c 6c 2e 20 45 69 74 68 65 72 20 77 61 79 2c 20 ll. Either way,
177c4 74 68 65 20 70 61 67 65 20 62 65 69 6e 67 20 66 the page being f
177c5 72 65 65 64 0a 20 20 2a 2a 20 77 69 6c 6c 20 62 reed. ** will b
177c6 65 63 6f 6d 65 20 74 68 65 20 6e 65 77 20 66 69 ecome the new fi
177c7 72 73 74 20 74 72 75 6e 6b 20 70 61 67 65 20 69 rst trunk page i
177c8 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 2e n the free-list.
177c9 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 67 . */. if( pPag
177ca 65 3d 3d 30 20 26 26 20 53 51 4c 49 54 45 5f 4f e==0 && SQLITE_O
177cb 4b 21 3d 28 72 63 20 3d 20 62 74 72 65 65 47 65 K!=(rc = btreeGe
177cc 74 50 61 67 65 28 70 42 74 2c 20 69 50 61 67 65 tPage(pBt, iPage
177cd 2c 20 26 70 50 61 67 65 2c 20 30 29 29 20 29 7b , &pPage, 0)) ){
177ce 0a 20 20 20 20 67 6f 74 6f 20 66 72 65 65 70 61 . goto freepa
177cf 67 65 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 72 63 ge_out;. }. rc
177d0 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 = sqlite3PagerW
177d1 72 69 74 65 28 70 50 61 67 65 2d 3e 70 44 62 50 rite(pPage->pDbP
177d2 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 21 3d age);. if( rc!=
177d3 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
177d4 20 67 6f 74 6f 20 66 72 65 65 70 61 67 65 5f 6f goto freepage_o
177d5 75 74 3b 0a 20 20 7d 0a 20 20 70 75 74 34 62 79 ut;. }. put4by
177d6 74 65 28 70 50 61 67 65 2d 3e 61 44 61 74 61 2c te(pPage->aData,
177d7 20 69 54 72 75 6e 6b 29 3b 0a 20 20 70 75 74 34 iTrunk);. put4
177d8 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61 byte(&pPage->aDa
177d9 74 61 5b 34 5d 2c 20 30 29 3b 0a 20 20 70 75 74 ta[4], 0);. put
177da 34 62 79 74 65 28 26 70 50 61 67 65 31 2d 3e 61 4byte(&pPage1->a
177db 44 61 74 61 5b 33 32 5d 2c 20 69 50 61 67 65 29 Data[32], iPage)
177dc 3b 0a 20 20 54 52 41 43 45 28 28 22 46 52 45 45 ;. TRACE(("FREE
177dd 2d 50 41 47 45 3a 20 25 64 20 6e 65 77 20 74 72 -PAGE: %d new tr
177de 75 6e 6b 20 70 61 67 65 20 72 65 70 6c 61 63 69 unk page replaci
177df 6e 67 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 2d ng %d\n", pPage-
177e0 3e 70 67 6e 6f 2c 20 69 54 72 75 6e 6b 29 29 3b >pgno, iTrunk));
177e1 0a 0a 66 72 65 65 70 61 67 65 5f 6f 75 74 3a 0a ..freepage_out:.
177e2 20 20 69 66 28 20 70 50 61 67 65 20 29 7b 0a 20 if( pPage ){.
177e3 20 20 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 pPage->isInit
177e4 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 6c 65 = 0;. }. rele
177e5 61 73 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a asePage(pPage);.
177e6 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 54 releasePage(pT
177e7 72 75 6e 6b 29 3b 0a 20 20 72 65 74 75 72 6e 20 runk);. return
177e8 72 63 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 rc;.}.static voi
177e9 64 20 66 72 65 65 50 61 67 65 28 4d 65 6d 50 61 d freePage(MemPa
177ea 67 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 20 2a ge *pPage, int *
177eb 70 52 43 29 7b 0a 20 20 69 66 28 20 28 2a 70 52 pRC){. if( (*pR
177ec 43 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b C)==SQLITE_OK ){
177ed 0a 20 20 20 20 2a 70 52 43 20 3d 20 66 72 65 65 . *pRC = free
177ee 50 61 67 65 32 28 70 50 61 67 65 2d 3e 70 42 74 Page2(pPage->pBt
177ef 2c 20 70 50 61 67 65 2c 20 70 50 61 67 65 2d 3e , pPage, pPage->
177f0 70 67 6e 6f 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a pgno);. }.}../*
177f1 0a 2a 2a 20 46 72 65 65 20 61 6e 79 20 6f 76 65 .** Free any ove
177f2 72 66 6c 6f 77 20 70 61 67 65 73 20 61 73 73 6f rflow pages asso
177f3 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 ciated with the
177f4 67 69 76 65 6e 20 43 65 6c 6c 2e 0a 2a 2f 0a 73 given Cell..*/.s
177f5 74 61 74 69 63 20 69 6e 74 20 63 6c 65 61 72 43 tatic int clearC
177f6 65 6c 6c 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 ell(MemPage *pPa
177f7 67 65 2c 20 75 6e 73 69 67 6e 65 64 20 63 68 61 ge, unsigned cha
177f8 72 20 2a 70 43 65 6c 6c 29 7b 0a 20 20 42 74 53 r *pCell){. BtS
177f9 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 50 61 hared *pBt = pPa
177fa 67 65 2d 3e 70 42 74 3b 0a 20 20 43 65 6c 6c 49 ge->pBt;. CellI
177fb 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20 50 67 6e 6f nfo info;. Pgno
177fc 20 6f 76 66 6c 50 67 6e 6f 3b 0a 20 20 69 6e 74 ovflPgno;. int
177fd 20 72 63 3b 0a 20 20 69 6e 74 20 6e 4f 76 66 6c rc;. int nOvfl
177fe 3b 0a 20 20 75 31 36 20 6f 76 66 6c 50 61 67 65 ;. u16 ovflPage
177ff 53 69 7a 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 Size;.. assert(
17800 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
17801 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e eld(pPage->pBt->
17802 6d 75 74 65 78 29 20 29 3b 0a 20 20 62 74 72 65 mutex) );. btre
17803 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70 50 eParseCellPtr(pP
17804 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 69 6e 66 age, pCell, &inf
17805 6f 29 3b 0a 20 20 69 66 28 20 69 6e 66 6f 2e 69 o);. if( info.i
17806 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 29 7b 0a 20 Overflow==0 ){.
17807 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
17808 5f 4f 4b 3b 20 20 2f 2a 20 4e 6f 20 6f 76 65 72 _OK; /* No over
17809 66 6c 6f 77 20 70 61 67 65 73 2e 20 52 65 74 75 flow pages. Retu
1780a 72 6e 20 77 69 74 68 6f 75 74 20 64 6f 69 6e 67 rn without doing
1780b 20 61 6e 79 74 68 69 6e 67 20 2a 2f 0a 20 20 7d anything */. }
1780c 0a 20 20 6f 76 66 6c 50 67 6e 6f 20 3d 20 67 65 . ovflPgno = ge
1780d 74 34 62 79 74 65 28 26 70 43 65 6c 6c 5b 69 6e t4byte(&pCell[in
1780e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d 29 3b 0a fo.iOverflow]);.
1780f 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 75 assert( pBt->u
17810 73 61 62 6c 65 53 69 7a 65 20 3e 20 34 20 29 3b sableSize > 4 );
17811 0a 20 20 6f 76 66 6c 50 61 67 65 53 69 7a 65 20 . ovflPageSize
17812 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a = pBt->usableSiz
17813 65 20 2d 20 34 3b 0a 20 20 6e 4f 76 66 6c 20 3d e - 4;. nOvfl =
17814 20 28 69 6e 66 6f 2e 6e 50 61 79 6c 6f 61 64 20 (info.nPayload
17815 2d 20 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 2b 20 - info.nLocal +
17816 6f 76 66 6c 50 61 67 65 53 69 7a 65 20 2d 20 31 ovflPageSize - 1
17817 29 2f 6f 76 66 6c 50 61 67 65 53 69 7a 65 3b 0a )/ovflPageSize;.
17818 20 20 61 73 73 65 72 74 28 20 6f 76 66 6c 50 67 assert( ovflPg
17819 6e 6f 3d 3d 30 20 7c 7c 20 6e 4f 76 66 6c 3e 30 no==0 || nOvfl>0
1781a 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 6e 4f 76 );. while( nOv
1781b 66 6c 2d 2d 20 29 7b 0a 20 20 20 20 50 67 6e 6f fl-- ){. Pgno
1781c 20 69 4e 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 iNext = 0;.
1781d 4d 65 6d 50 61 67 65 20 2a 70 4f 76 66 6c 20 3d MemPage *pOvfl =
1781e 20 30 3b 0a 20 20 20 20 69 66 28 20 6f 76 66 6c 0;. if( ovfl
1781f 50 67 6e 6f 3c 32 20 7c 7c 20 6f 76 66 6c 50 67 Pgno<2 || ovflPg
17820 6e 6f 3e 70 61 67 65 72 50 61 67 65 63 6f 75 6e no>pagerPagecoun
17821 74 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 t(pBt) ){.
17822 2f 2a 20 30 20 69 73 20 6e 6f 74 20 61 20 6c 65 /* 0 is not a le
17823 67 61 6c 20 70 61 67 65 20 6e 75 6d 62 65 72 20 gal page number
17824 61 6e 64 20 70 61 67 65 20 31 20 63 61 6e 6e 6f and page 1 canno
17825 74 20 62 65 20 61 6e 20 0a 20 20 20 20 20 20 2a t be an . *
17826 2a 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2e * overflow page.
17827 20 54 68 65 72 65 66 6f 72 65 20 69 66 20 6f 76 Therefore if ov
17828 66 6c 50 67 6e 6f 3c 32 20 6f 72 20 70 61 73 74 flPgno<2 or past
17829 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 the end of the
1782a 0a 20 20 20 20 20 20 2a 2a 20 66 69 6c 65 20 74 . ** file t
1782b 68 65 20 64 61 74 61 62 61 73 65 20 6d 75 73 74 he database must
1782c 20 62 65 20 63 6f 72 72 75 70 74 2e 20 2a 2f 0a be corrupt. */.
1782d 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
1782e 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
1782f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
17830 6e 4f 76 66 6c 20 29 7b 0a 20 20 20 20 20 20 72 nOvfl ){. r
17831 63 20 3d 20 67 65 74 4f 76 65 72 66 6c 6f 77 50 c = getOverflowP
17832 61 67 65 28 70 42 74 2c 20 6f 76 66 6c 50 67 6e age(pBt, ovflPgn
17833 6f 2c 20 26 70 4f 76 66 6c 2c 20 26 69 4e 65 78 o, &pOvfl, &iNex
17834 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 t);. if( rc
17835 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 ) return rc;.
17836 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 66 72 65 }. rc = fre
17837 65 50 61 67 65 32 28 70 42 74 2c 20 70 4f 76 66 ePage2(pBt, pOvf
17838 6c 2c 20 6f 76 66 6c 50 67 6e 6f 29 3b 0a 20 20 l, ovflPgno);.
17839 20 20 69 66 28 20 70 4f 76 66 6c 20 29 7b 0a 20 if( pOvfl ){.
1783a 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 sqlite3Page
1783b 72 55 6e 72 65 66 28 70 4f 76 66 6c 2d 3e 70 44 rUnref(pOvfl->pD
1783c 62 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20 20 bPage);. }.
1783d 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 if( rc ) retur
1783e 6e 20 72 63 3b 0a 20 20 20 20 6f 76 66 6c 50 67 n rc;. ovflPg
1783f 6e 6f 20 3d 20 69 4e 65 78 74 3b 0a 20 20 7d 0a no = iNext;. }.
17840 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
17841 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 OK;.}../*.** Cre
17842 61 74 65 20 74 68 65 20 62 79 74 65 20 73 65 71 ate the byte seq
17843 75 65 6e 63 65 20 75 73 65 64 20 74 6f 20 72 65 uence used to re
17844 70 72 65 73 65 6e 74 20 61 20 63 65 6c 6c 20 6f present a cell o
17845 6e 20 70 61 67 65 20 70 50 61 67 65 0a 2a 2a 20 n page pPage.**
17846 61 6e 64 20 77 72 69 74 65 20 74 68 61 74 20 62 and write that b
17847 79 74 65 20 73 65 71 75 65 6e 63 65 20 69 6e 74 yte sequence int
17848 6f 20 70 43 65 6c 6c 5b 5d 2e 20 20 4f 76 65 72 o pCell[]. Over
17849 66 6c 6f 77 20 70 61 67 65 73 20 61 72 65 0a 2a flow pages are.*
1784a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 * allocated and
1784b 66 69 6c 6c 65 64 20 69 6e 20 61 73 20 6e 65 63 filled in as nec
1784c 65 73 73 61 72 79 2e 20 20 54 68 65 20 63 61 6c essary. The cal
1784d 6c 69 6e 67 20 70 72 6f 63 65 64 75 72 65 0a 2a ling procedure.*
1784e 2a 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 * is responsible
1784f 20 66 6f 72 20 6d 61 6b 69 6e 67 20 73 75 72 65 for making sure
17850 20 73 75 66 66 69 63 69 65 6e 74 20 73 70 61 63 sufficient spac
17851 65 20 68 61 73 20 62 65 65 6e 20 61 6c 6c 6f 63 e has been alloc
17852 61 74 65 64 0a 2a 2a 20 66 6f 72 20 70 43 65 6c ated.** for pCel
17853 6c 5b 5d 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 l[]..**.** Note
17854 74 68 61 74 20 70 43 65 6c 6c 20 64 6f 65 73 20 that pCell does
17855 6e 6f 74 20 6e 65 63 65 73 73 61 72 79 20 6e 65 not necessary ne
17856 65 64 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 ed to point to t
17857 68 65 20 70 50 61 67 65 2d 3e 61 44 61 74 61 0a he pPage->aData.
17858 2a 2a 20 61 72 65 61 2e 20 20 70 43 65 6c 6c 20 ** area. pCell
17859 6d 69 67 68 74 20 70 6f 69 6e 74 20 74 6f 20 73 might point to s
1785a 6f 6d 65 20 74 65 6d 70 6f 72 61 72 79 20 73 74 ome temporary st
1785b 6f 72 61 67 65 2e 20 20 54 68 65 20 63 65 6c 6c orage. The cell
1785c 20 77 69 6c 6c 0a 2a 2a 20 62 65 20 63 6f 6e 73 will.** be cons
1785d 74 72 75 63 74 65 64 20 69 6e 20 74 68 69 73 20 tructed in this
1785e 74 65 6d 70 6f 72 61 72 79 20 61 72 65 61 20 74 temporary area t
1785f 68 65 6e 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 hen copied into
17860 70 50 61 67 65 2d 3e 61 44 61 74 61 0a 2a 2a 20 pPage->aData.**
17861 6c 61 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 later..*/.static
17862 20 69 6e 74 20 66 69 6c 6c 49 6e 43 65 6c 6c 28 int fillInCell(
17863 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 . MemPage *pPag
17864 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e,
17865 20 20 2f 2a 20 54 68 65 20 70 61 67 65 20 74 68 /* The page th
17866 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 at contains the
17867 63 65 6c 6c 20 2a 2f 0a 20 20 75 6e 73 69 67 6e cell */. unsign
17868 65 64 20 63 68 61 72 20 2a 70 43 65 6c 6c 2c 20 ed char *pCell,
17869 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6d 70 /* Comp
1786a 6c 65 74 65 20 74 65 78 74 20 6f 66 20 74 68 65 lete text of the
1786b 20 63 65 6c 6c 20 2a 2f 0a 20 20 63 6f 6e 73 74 cell */. const
1786c 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69 36 34 void *pKey, i64
1786d 20 6e 4b 65 79 2c 20 20 20 20 2f 2a 20 54 68 65 nKey, /* The
1786e 20 6b 65 79 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 key */. const
1786f 76 6f 69 64 20 2a 70 44 61 74 61 2c 69 6e 74 20 void *pData,int
17870 6e 44 61 74 61 2c 20 20 20 2f 2a 20 54 68 65 20 nData, /* The
17871 64 61 74 61 20 2a 2f 0a 20 20 69 6e 74 20 6e 5a data */. int nZ
17872 65 72 6f 2c 20 20 20 20 20 20 20 20 20 20 20 20 ero,
17873 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 74 72 /* Extr
17874 61 20 7a 65 72 6f 20 62 79 74 65 73 20 74 6f 20 a zero bytes to
17875 61 70 70 65 6e 64 20 74 6f 20 70 44 61 74 61 20 append to pData
17876 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 53 69 7a 65 */. int *pnSize
17877 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17878 20 20 20 20 2f 2a 20 57 72 69 74 65 20 63 65 6c /* Write cel
17879 6c 20 73 69 7a 65 20 68 65 72 65 20 2a 2f 0a 29 l size here */.)
1787a 7b 0a 20 20 69 6e 74 20 6e 50 61 79 6c 6f 61 64 {. int nPayload
1787b 3b 0a 20 20 63 6f 6e 73 74 20 75 38 20 2a 70 53 ;. const u8 *pS
1787c 72 63 3b 0a 20 20 69 6e 74 20 6e 53 72 63 2c 20 rc;. int nSrc,
1787d 6e 2c 20 72 63 3b 0a 20 20 69 6e 74 20 73 70 61 n, rc;. int spa
1787e 63 65 4c 65 66 74 3b 0a 20 20 4d 65 6d 50 61 67 ceLeft;. MemPag
1787f 65 20 2a 70 4f 76 66 6c 20 3d 20 30 3b 0a 20 20 e *pOvfl = 0;.
17880 4d 65 6d 50 61 67 65 20 2a 70 54 6f 52 65 6c 65 MemPage *pToRele
17881 61 73 65 20 3d 20 30 3b 0a 20 20 75 6e 73 69 67 ase = 0;. unsig
17882 6e 65 64 20 63 68 61 72 20 2a 70 50 72 69 6f 72 ned char *pPrior
17883 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 ;. unsigned cha
17884 72 20 2a 70 50 61 79 6c 6f 61 64 3b 0a 20 20 42 r *pPayload;. B
17885 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 tShared *pBt = p
17886 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 50 67 6e Page->pBt;. Pgn
17887 6f 20 70 67 6e 6f 4f 76 66 6c 20 3d 20 30 3b 0a o pgnoOvfl = 0;.
17888 20 20 69 6e 74 20 6e 48 65 61 64 65 72 3b 0a 20 int nHeader;.
17889 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a CellInfo info;.
1788a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
1788b 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 e3_mutex_held(pP
1788c 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 age->pBt->mutex)
1788d 20 29 3b 0a 0a 20 20 2f 2a 20 70 50 61 67 65 20 );.. /* pPage
1788e 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 is not necessari
1788f 6c 79 20 77 72 69 74 65 61 62 6c 65 20 73 69 6e ly writeable sin
17890 63 65 20 70 43 65 6c 6c 20 6d 69 67 68 74 20 62 ce pCell might b
17891 65 20 61 75 78 69 6c 69 61 72 79 0a 20 20 2a 2a e auxiliary. **
17892 20 62 75 66 66 65 72 20 73 70 61 63 65 20 74 68 buffer space th
17893 61 74 20 69 73 20 73 65 70 61 72 61 74 65 20 66 at is separate f
17894 72 6f 6d 20 74 68 65 20 70 50 61 67 65 20 62 75 rom the pPage bu
17895 66 66 65 72 20 61 72 65 61 20 2a 2f 0a 20 20 61 ffer area */. a
17896 73 73 65 72 74 28 20 70 43 65 6c 6c 3c 70 50 61 ssert( pCell<pPa
17897 67 65 2d 3e 61 44 61 74 61 20 7c 7c 20 70 43 65 ge->aData || pCe
17898 6c 6c 3e 3d 26 70 50 61 67 65 2d 3e 61 44 61 74 ll>=&pPage->aDat
17899 61 5b 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 5d a[pBt->pageSize]
1789a 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 . ||
1789b 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 sqlite3PagerIswr
1789c 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 iteable(pPage->p
1789d 44 62 50 61 67 65 29 20 29 3b 0a 0a 20 20 2f 2a DbPage) );.. /*
1789e 20 46 69 6c 6c 20 69 6e 20 74 68 65 20 68 65 61 Fill in the hea
1789f 64 65 72 2e 20 2a 2f 0a 20 20 6e 48 65 61 64 65 der. */. nHeade
178a0 72 20 3d 20 30 3b 0a 20 20 69 66 28 20 21 70 50 r = 0;. if( !pP
178a1 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 age->leaf ){.
178a2 20 6e 48 65 61 64 65 72 20 2b 3d 20 34 3b 0a 20 nHeader += 4;.
178a3 20 7d 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e }. if( pPage->
178a4 68 61 73 44 61 74 61 20 29 7b 0a 20 20 20 20 6e hasData ){. n
178a5 48 65 61 64 65 72 20 2b 3d 20 70 75 74 56 61 72 Header += putVar
178a6 69 6e 74 28 26 70 43 65 6c 6c 5b 6e 48 65 61 64 int(&pCell[nHead
178a7 65 72 5d 2c 20 6e 44 61 74 61 2b 6e 5a 65 72 6f er], nData+nZero
178a8 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
178a9 6e 44 61 74 61 20 3d 20 6e 5a 65 72 6f 20 3d 20 nData = nZero =
178aa 30 3b 0a 20 20 7d 0a 20 20 6e 48 65 61 64 65 72 0;. }. nHeader
178ab 20 2b 3d 20 70 75 74 56 61 72 69 6e 74 28 26 70 += putVarint(&p
178ac 43 65 6c 6c 5b 6e 48 65 61 64 65 72 5d 2c 20 2a Cell[nHeader], *
178ad 28 75 36 34 2a 29 26 6e 4b 65 79 29 3b 0a 20 20 (u64*)&nKey);.
178ae 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 btreeParseCellPt
178af 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20 r(pPage, pCell,
178b0 26 69 6e 66 6f 29 3b 0a 20 20 61 73 73 65 72 74 &info);. assert
178b1 28 20 69 6e 66 6f 2e 6e 48 65 61 64 65 72 3d 3d ( info.nHeader==
178b2 6e 48 65 61 64 65 72 20 29 3b 0a 20 20 61 73 73 nHeader );. ass
178b3 65 72 74 28 20 69 6e 66 6f 2e 6e 4b 65 79 3d 3d ert( info.nKey==
178b4 6e 4b 65 79 20 29 3b 0a 20 20 61 73 73 65 72 74 nKey );. assert
178b5 28 20 69 6e 66 6f 2e 6e 44 61 74 61 3d 3d 28 75 ( info.nData==(u
178b6 33 32 29 28 6e 44 61 74 61 2b 6e 5a 65 72 6f 29 32)(nData+nZero)
178b7 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 46 69 6c 6c );. . /* Fill
178b8 20 69 6e 20 74 68 65 20 70 61 79 6c 6f 61 64 20 in the payload
178b9 2a 2f 0a 20 20 6e 50 61 79 6c 6f 61 64 20 3d 20 */. nPayload =
178ba 6e 44 61 74 61 20 2b 20 6e 5a 65 72 6f 3b 0a 20 nData + nZero;.
178bb 20 69 66 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b if( pPage->intK
178bc 65 79 20 29 7b 0a 20 20 20 20 70 53 72 63 20 3d ey ){. pSrc =
178bd 20 70 44 61 74 61 3b 0a 20 20 20 20 6e 53 72 63 pData;. nSrc
178be 20 3d 20 6e 44 61 74 61 3b 0a 20 20 20 20 6e 44 = nData;. nD
178bf 61 74 61 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 ata = 0;. }else
178c0 7b 20 0a 20 20 20 20 69 66 28 20 4e 45 56 45 52 { . if( NEVER
178c1 28 6e 4b 65 79 3e 30 78 37 66 66 66 66 66 66 66 (nKey>0x7fffffff
178c2 20 7c 7c 20 70 4b 65 79 3d 3d 30 29 20 29 7b 0a || pKey==0) ){.
178c3 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
178c4 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 ITE_CORRUPT_BKPT
178c5 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e 50 61 79 ;. }. nPay
178c6 6c 6f 61 64 20 2b 3d 20 28 69 6e 74 29 6e 4b 65 load += (int)nKe
178c7 79 3b 0a 20 20 20 20 70 53 72 63 20 3d 20 70 4b y;. pSrc = pK
178c8 65 79 3b 0a 20 20 20 20 6e 53 72 63 20 3d 20 28 ey;. nSrc = (
178c9 69 6e 74 29 6e 4b 65 79 3b 0a 20 20 7d 0a 20 20 int)nKey;. }.
178ca 2a 70 6e 53 69 7a 65 20 3d 20 69 6e 66 6f 2e 6e *pnSize = info.n
178cb 53 69 7a 65 3b 0a 20 20 73 70 61 63 65 4c 65 66 Size;. spaceLef
178cc 74 20 3d 20 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 3b t = info.nLocal;
178cd 0a 20 20 70 50 61 79 6c 6f 61 64 20 3d 20 26 70 . pPayload = &p
178ce 43 65 6c 6c 5b 6e 48 65 61 64 65 72 5d 3b 0a 20 Cell[nHeader];.
178cf 20 70 50 72 69 6f 72 20 3d 20 26 70 43 65 6c 6c pPrior = &pCell
178d0 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d [info.iOverflow]
178d1 3b 0a 0a 20 20 77 68 69 6c 65 28 20 6e 50 61 79 ;.. while( nPay
178d2 6c 6f 61 64 3e 30 20 29 7b 0a 20 20 20 20 69 66 load>0 ){. if
178d3 28 20 73 70 61 63 65 4c 65 66 74 3d 3d 30 20 29 ( spaceLeft==0 )
178d4 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 {.#ifndef SQLITE
178d5 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d _OMIT_AUTOVACUUM
178d6 0a 20 20 20 20 20 20 50 67 6e 6f 20 70 67 6e 6f . Pgno pgno
178d7 50 74 72 6d 61 70 20 3d 20 70 67 6e 6f 4f 76 66 Ptrmap = pgnoOvf
178d8 6c 3b 20 2f 2a 20 4f 76 65 72 66 6c 6f 77 20 70 l; /* Overflow p
178d9 61 67 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 age pointer-map
178da 65 6e 74 72 79 20 70 61 67 65 20 2a 2f 0a 20 20 entry page */.
178db 20 20 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 if( pBt->aut
178dc 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 20 oVacuum ){.
178dd 20 20 20 64 6f 7b 0a 20 20 20 20 20 20 20 20 20 do{.
178de 20 70 67 6e 6f 4f 76 66 6c 2b 2b 3b 0a 20 20 20 pgnoOvfl++;.
178df 20 20 20 20 20 7d 20 77 68 69 6c 65 28 20 0a 20 } while( .
178e0 20 20 20 20 20 20 20 20 20 50 54 52 4d 41 50 5f PTRMAP_
178e1 49 53 50 41 47 45 28 70 42 74 2c 20 70 67 6e 6f ISPAGE(pBt, pgno
178e2 4f 76 66 6c 29 20 7c 7c 20 70 67 6e 6f 4f 76 66 Ovfl) || pgnoOvf
178e3 6c 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f l==PENDING_BYTE_
178e4 50 41 47 45 28 70 42 74 29 20 0a 20 20 20 20 20 PAGE(pBt) .
178e5 20 20 20 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 );. }.#e
178e6 6e 64 69 66 0a 20 20 20 20 20 20 72 63 20 3d 20 ndif. rc =
178e7 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61 67 allocateBtreePag
178e8 65 28 70 42 74 2c 20 26 70 4f 76 66 6c 2c 20 26 e(pBt, &pOvfl, &
178e9 70 67 6e 6f 4f 76 66 6c 2c 20 70 67 6e 6f 4f 76 pgnoOvfl, pgnoOv
178ea 66 6c 2c 20 30 29 3b 0a 23 69 66 6e 64 65 66 20 fl, 0);.#ifndef
178eb 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f SQLITE_OMIT_AUTO
178ec 56 41 43 55 55 4d 0a 20 20 20 20 20 20 2f 2a 20 VACUUM. /*
178ed 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 If the database
178ee 73 75 70 70 6f 72 74 73 20 61 75 74 6f 2d 76 61 supports auto-va
178ef 63 75 75 6d 2c 20 61 6e 64 20 74 68 65 20 73 65 cuum, and the se
178f0 63 6f 6e 64 20 6f 72 20 73 75 62 73 65 71 75 65 cond or subseque
178f1 6e 74 0a 20 20 20 20 20 20 2a 2a 20 6f 76 65 72 nt. ** over
178f2 66 6c 6f 77 20 70 61 67 65 20 69 73 20 62 65 69 flow page is bei
178f3 6e 67 20 61 6c 6c 6f 63 61 74 65 64 2c 20 61 64 ng allocated, ad
178f4 64 20 61 6e 20 65 6e 74 72 79 20 74 6f 20 74 68 d an entry to th
178f5 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 0a 20 20 e pointer-map.
178f6 20 20 20 20 2a 2a 20 66 6f 72 20 74 68 61 74 20 ** for that
178f7 70 61 67 65 20 6e 6f 77 2e 20 0a 20 20 20 20 20 page now. .
178f8 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 49 66 20 **. ** If
178f9 74 68 69 73 20 69 73 20 74 68 65 20 66 69 72 73 this is the firs
178fa 74 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2c t overflow page,
178fb 20 74 68 65 6e 20 77 72 69 74 65 20 61 20 70 61 then write a pa
178fc 72 74 69 61 6c 20 65 6e 74 72 79 20 0a 20 20 20 rtial entry .
178fd 20 20 20 2a 2a 20 74 6f 20 74 68 65 20 70 6f 69 ** to the poi
178fe 6e 74 65 72 2d 6d 61 70 2e 20 49 66 20 77 65 20 nter-map. If we
178ff 77 72 69 74 65 20 6e 6f 74 68 69 6e 67 20 74 6f write nothing to
17900 20 74 68 69 73 20 70 6f 69 6e 74 65 72 2d 6d 61 this pointer-ma
17901 70 20 73 6c 6f 74 2c 0a 20 20 20 20 20 20 2a 2a p slot,. **
17902 20 74 68 65 6e 20 74 68 65 20 6f 70 74 69 6d 69 then the optimi
17903 73 74 69 63 20 6f 76 65 72 66 6c 6f 77 20 63 68 stic overflow ch
17904 61 69 6e 20 70 72 6f 63 65 73 73 69 6e 67 20 69 ain processing i
17905 6e 20 63 6c 65 61 72 43 65 6c 6c 28 29 0a 20 20 n clearCell().
17906 20 20 20 20 2a 2a 20 6d 61 79 20 6d 69 73 69 6e ** may misin
17907 74 65 72 70 72 65 74 20 74 68 65 20 75 6e 69 6e terpret the unin
17908 69 74 69 61 6c 69 73 65 64 20 76 61 6c 75 65 73 itialised values
17909 20 61 6e 64 20 64 65 6c 65 74 65 20 74 68 65 0a and delete the.
1790a 20 20 20 20 20 20 2a 2a 20 77 72 6f 6e 67 20 70 ** wrong p
1790b 61 67 65 73 20 66 72 6f 6d 20 74 68 65 20 64 61 ages from the da
1790c 74 61 62 61 73 65 2e 0a 20 20 20 20 20 20 2a 2f tabase.. */
1790d 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 2d 3e . if( pBt->
1790e 61 75 74 6f 56 61 63 75 75 6d 20 26 26 20 72 63 autoVacuum && rc
1790f 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
17910 20 20 20 20 20 20 20 75 38 20 65 54 79 70 65 20 u8 eType
17911 3d 20 28 70 67 6e 6f 50 74 72 6d 61 70 3f 50 54 = (pgnoPtrmap?PT
17912 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32 3a 50 RMAP_OVERFLOW2:P
17913 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 29 TRMAP_OVERFLOW1)
17914 3b 0a 20 20 20 20 20 20 20 20 70 74 72 6d 61 70 ;. ptrmap
17915 50 75 74 28 70 42 74 2c 20 70 67 6e 6f 4f 76 66 Put(pBt, pgnoOvf
17916 6c 2c 20 65 54 79 70 65 2c 20 70 67 6e 6f 50 74 l, eType, pgnoPt
17917 72 6d 61 70 2c 20 26 72 63 29 3b 0a 20 20 20 20 rmap, &rc);.
17918 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 if( rc ){.
17919 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 releaseP
1791a 61 67 65 28 70 4f 76 66 6c 29 3b 0a 20 20 20 20 age(pOvfl);.
1791b 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 23 65 }. }.#e
1791c 6e 64 69 66 0a 20 20 20 20 20 20 69 66 28 20 72 ndif. if( r
1791d 63 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 6c c ){. rel
1791e 65 61 73 65 50 61 67 65 28 70 54 6f 52 65 6c 65 easePage(pToRele
1791f 61 73 65 29 3b 0a 20 20 20 20 20 20 20 20 72 65 ase);. re
17920 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d turn rc;. }
17921 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 70 54 .. /* If pT
17922 6f 52 65 6c 65 61 73 65 20 69 73 20 6e 6f 74 20 oRelease is not
17923 7a 65 72 6f 20 74 68 61 6e 20 70 50 72 69 6f 72 zero than pPrior
17924 20 70 6f 69 6e 74 73 20 69 6e 74 6f 20 74 68 65 points into the
17925 20 64 61 74 61 20 61 72 65 61 0a 20 20 20 20 20 data area.
17926 20 2a 2a 20 6f 66 20 70 54 6f 52 65 6c 65 61 73 ** of pToReleas
17927 65 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 70 54 e. Make sure pT
17928 6f 52 65 6c 65 61 73 65 20 69 73 20 73 74 69 6c oRelease is stil
17929 6c 20 77 72 69 74 65 61 62 6c 65 2e 20 2a 2f 0a l writeable. */.
1792a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 54 assert( pT
1792b 6f 52 65 6c 65 61 73 65 3d 3d 30 20 7c 7c 20 73 oRelease==0 || s
1792c 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 qlite3PagerIswri
1792d 74 65 61 62 6c 65 28 70 54 6f 52 65 6c 65 61 73 teable(pToReleas
1792e 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 0a e->pDbPage) );..
1792f 20 20 20 20 20 20 2f 2a 20 49 66 20 70 50 72 69 /* If pPri
17930 6f 72 20 69 73 20 70 61 72 74 20 6f 66 20 74 68 or is part of th
17931 65 20 64 61 74 61 20 61 72 65 61 20 6f 66 20 70 e data area of p
17932 50 61 67 65 2c 20 74 68 65 6e 20 6d 61 6b 65 20 Page, then make
17933 73 75 72 65 20 70 50 61 67 65 0a 20 20 20 20 20 sure pPage.
17934 20 2a 2a 20 69 73 20 73 74 69 6c 6c 20 77 72 69 ** is still wri
17935 74 65 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20 teable */.
17936 61 73 73 65 72 74 28 20 70 50 72 69 6f 72 3c 70 assert( pPrior<p
17937 50 61 67 65 2d 3e 61 44 61 74 61 20 7c 7c 20 70 Page->aData || p
17938 50 72 69 6f 72 3e 3d 26 70 50 61 67 65 2d 3e 61 Prior>=&pPage->a
17939 44 61 74 61 5b 70 42 74 2d 3e 70 61 67 65 53 69 Data[pBt->pageSi
1793a 7a 65 5d 0a 20 20 20 20 20 20 20 20 20 20 20 20 ze].
1793b 7c 7c 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 || sqlite3PagerI
1793c 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 swriteable(pPage
1793d 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 0a 20 ->pDbPage) );..
1793e 20 20 20 20 20 70 75 74 34 62 79 74 65 28 70 50 put4byte(pP
1793f 72 69 6f 72 2c 20 70 67 6e 6f 4f 76 66 6c 29 3b rior, pgnoOvfl);
17940 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 . releasePa
17941 67 65 28 70 54 6f 52 65 6c 65 61 73 65 29 3b 0a ge(pToRelease);.
17942 20 20 20 20 20 20 70 54 6f 52 65 6c 65 61 73 65 pToRelease
17943 20 3d 20 70 4f 76 66 6c 3b 0a 20 20 20 20 20 20 = pOvfl;.
17944 70 50 72 69 6f 72 20 3d 20 70 4f 76 66 6c 2d 3e pPrior = pOvfl->
17945 61 44 61 74 61 3b 0a 20 20 20 20 20 20 70 75 74 aData;. put
17946 34 62 79 74 65 28 70 50 72 69 6f 72 2c 20 30 29 4byte(pPrior, 0)
17947 3b 0a 20 20 20 20 20 20 70 50 61 79 6c 6f 61 64 ;. pPayload
17948 20 3d 20 26 70 4f 76 66 6c 2d 3e 61 44 61 74 61 = &pOvfl->aData
17949 5b 34 5d 3b 0a 20 20 20 20 20 20 73 70 61 63 65 [4];. space
1794a 4c 65 66 74 20 3d 20 70 42 74 2d 3e 75 73 61 62 Left = pBt->usab
1794b 6c 65 53 69 7a 65 20 2d 20 34 3b 0a 20 20 20 20 leSize - 4;.
1794c 7d 0a 20 20 20 20 6e 20 3d 20 6e 50 61 79 6c 6f }. n = nPaylo
1794d 61 64 3b 0a 20 20 20 20 69 66 28 20 6e 3e 73 70 ad;. if( n>sp
1794e 61 63 65 4c 65 66 74 20 29 20 6e 20 3d 20 73 70 aceLeft ) n = sp
1794f 61 63 65 4c 65 66 74 3b 0a 0a 20 20 20 20 2f 2a aceLeft;.. /*
17950 20 49 66 20 70 54 6f 52 65 6c 65 61 73 65 20 69 If pToRelease i
17951 73 20 6e 6f 74 20 7a 65 72 6f 20 74 68 61 6e 20 s not zero than
17952 70 50 61 79 6c 6f 61 64 20 70 6f 69 6e 74 73 20 pPayload points
17953 69 6e 74 6f 20 74 68 65 20 64 61 74 61 20 61 72 into the data ar
17954 65 61 0a 20 20 20 20 2a 2a 20 6f 66 20 70 54 6f ea. ** of pTo
17955 52 65 6c 65 61 73 65 2e 20 20 4d 61 6b 65 20 73 Release. Make s
17956 75 72 65 20 70 54 6f 52 65 6c 65 61 73 65 20 69 ure pToRelease i
17957 73 20 73 74 69 6c 6c 20 77 72 69 74 65 61 62 6c s still writeabl
17958 65 2e 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 e. */. assert
17959 28 20 70 54 6f 52 65 6c 65 61 73 65 3d 3d 30 20 ( pToRelease==0
1795a 7c 7c 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 || sqlite3PagerI
1795b 73 77 72 69 74 65 61 62 6c 65 28 70 54 6f 52 65 swriteable(pToRe
1795c 6c 65 61 73 65 2d 3e 70 44 62 50 61 67 65 29 20 lease->pDbPage)
1795d 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 70 50 );.. /* If pP
1795e 61 79 6c 6f 61 64 20 69 73 20 70 61 72 74 20 6f ayload is part o
1795f 66 20 74 68 65 20 64 61 74 61 20 61 72 65 61 20 f the data area
17960 6f 66 20 70 50 61 67 65 2c 20 74 68 65 6e 20 6d of pPage, then m
17961 61 6b 65 20 73 75 72 65 20 70 50 61 67 65 0a 20 ake sure pPage.
17962 20 20 20 2a 2a 20 69 73 20 73 74 69 6c 6c 20 77 ** is still w
17963 72 69 74 65 61 62 6c 65 20 2a 2f 0a 20 20 20 20 riteable */.
17964 61 73 73 65 72 74 28 20 70 50 61 79 6c 6f 61 64 assert( pPayload
17965 3c 70 50 61 67 65 2d 3e 61 44 61 74 61 20 7c 7c <pPage->aData ||
17966 20 70 50 61 79 6c 6f 61 64 3e 3d 26 70 50 61 67 pPayload>=&pPag
17967 65 2d 3e 61 44 61 74 61 5b 70 42 74 2d 3e 70 61 e->aData[pBt->pa
17968 67 65 53 69 7a 65 5d 0a 20 20 20 20 20 20 20 20 geSize].
17969 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 50 61 || sqlite3Pa
1796a 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 gerIswriteable(p
1796b 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 Page->pDbPage) )
1796c 3b 0a 0a 20 20 20 20 69 66 28 20 6e 53 72 63 3e ;.. if( nSrc>
1796d 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 6e 0 ){. if( n
1796e 3e 6e 53 72 63 20 29 20 6e 20 3d 20 6e 53 72 63 >nSrc ) n = nSrc
1796f 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
17970 70 53 72 63 20 29 3b 0a 20 20 20 20 20 20 6d 65 pSrc );. me
17971 6d 63 70 79 28 70 50 61 79 6c 6f 61 64 2c 20 70 mcpy(pPayload, p
17972 53 72 63 2c 20 6e 29 3b 0a 20 20 20 20 7d 65 6c Src, n);. }el
17973 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 se{. memset
17974 28 70 50 61 79 6c 6f 61 64 2c 20 30 2c 20 6e 29 (pPayload, 0, n)
17975 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e 50 61 79 ;. }. nPay
17976 6c 6f 61 64 20 2d 3d 20 6e 3b 0a 20 20 20 20 70 load -= n;. p
17977 50 61 79 6c 6f 61 64 20 2b 3d 20 6e 3b 0a 20 20 Payload += n;.
17978 20 20 70 53 72 63 20 2b 3d 20 6e 3b 0a 20 20 20 pSrc += n;.
17979 20 6e 53 72 63 20 2d 3d 20 6e 3b 0a 20 20 20 20 nSrc -= n;.
1797a 73 70 61 63 65 4c 65 66 74 20 2d 3d 20 6e 3b 0a spaceLeft -= n;.
1797b 20 20 20 20 69 66 28 20 6e 53 72 63 3d 3d 30 20 if( nSrc==0
1797c 29 7b 0a 20 20 20 20 20 20 6e 53 72 63 20 3d 20 ){. nSrc =
1797d 6e 44 61 74 61 3b 0a 20 20 20 20 20 20 70 53 72 nData;. pSr
1797e 63 20 3d 20 70 44 61 74 61 3b 0a 20 20 20 20 7d c = pData;. }
1797f 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73 65 50 61 . }. releasePa
17980 67 65 28 70 54 6f 52 65 6c 65 61 73 65 29 3b 0a ge(pToRelease);.
17981 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
17982 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d OK;.}../*.** Rem
17983 6f 76 65 20 74 68 65 20 69 2d 74 68 20 63 65 6c ove the i-th cel
17984 6c 20 66 72 6f 6d 20 70 50 61 67 65 2e 20 20 54 l from pPage. T
17985 68 69 73 20 72 6f 75 74 69 6e 65 20 65 66 66 65 his routine effe
17986 63 74 73 20 70 50 61 67 65 20 6f 6e 6c 79 2e 0a cts pPage only..
17987 2a 2a 20 54 68 65 20 63 65 6c 6c 20 63 6f 6e 74 ** The cell cont
17988 65 6e 74 20 69 73 20 6e 6f 74 20 66 72 65 65 64 ent is not freed
17989 20 6f 72 20 64 65 61 6c 6c 6f 63 61 74 65 64 2e or deallocated.
1798a 20 20 49 74 20 69 73 20 61 73 73 75 6d 65 64 20 It is assumed
1798b 74 68 61 74 0a 2a 2a 20 74 68 65 20 63 65 6c 6c that.** the cell
1798c 20 63 6f 6e 74 65 6e 74 20 68 61 73 20 62 65 65 content has bee
1798d 6e 20 63 6f 70 69 65 64 20 73 6f 6d 65 70 6c 61 n copied somepla
1798e 63 65 20 65 6c 73 65 2e 20 20 54 68 69 73 20 72 ce else. This r
1798f 6f 75 74 69 6e 65 20 6a 75 73 74 0a 2a 2a 20 72 outine just.** r
17990 65 6d 6f 76 65 73 20 74 68 65 20 72 65 66 65 72 emoves the refer
17991 65 6e 63 65 20 74 6f 20 74 68 65 20 63 65 6c 6c ence to the cell
17992 20 66 72 6f 6d 20 70 50 61 67 65 2e 0a 2a 2a 0a from pPage..**.
17993 2a 2a 20 22 73 7a 22 20 6d 75 73 74 20 62 65 20 ** "sz" must be
17994 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 the number of by
17995 74 65 73 20 69 6e 20 74 68 65 20 63 65 6c 6c 2e tes in the cell.
17996 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
17997 64 72 6f 70 43 65 6c 6c 28 4d 65 6d 50 61 67 65 dropCell(MemPage
17998 20 2a 70 50 61 67 65 2c 20 69 6e 74 20 69 64 78 *pPage, int idx
17999 2c 20 69 6e 74 20 73 7a 2c 20 69 6e 74 20 2a 70 , int sz, int *p
1799a 52 43 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 RC){. int i;
1799b 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 /* Loop c
1799c 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 ounter */. int
1799d 70 63 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4f pc; /* O
1799e 66 66 73 65 74 20 74 6f 20 63 65 6c 6c 20 63 6f ffset to cell co
1799f 6e 74 65 6e 74 20 6f 66 20 63 65 6c 6c 20 62 65 ntent of cell be
179a0 69 6e 67 20 64 65 6c 65 74 65 64 20 2a 2f 0a 20 ing deleted */.
179a1 20 75 38 20 2a 64 61 74 61 3b 20 20 20 20 20 20 u8 *data;
179a2 20 2f 2a 20 70 50 61 67 65 2d 3e 61 44 61 74 61 /* pPage->aData
179a3 20 2a 2f 0a 20 20 75 38 20 2a 70 74 72 3b 20 20 */. u8 *ptr;
179a4 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 74 6f /* Used to
179a5 20 6d 6f 76 65 20 62 79 74 65 73 20 61 72 6f 75 move bytes arou
179a6 6e 64 20 77 69 74 68 69 6e 20 64 61 74 61 5b 5d nd within data[]
179a7 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 */. int rc;
179a8 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 65 74 /* The ret
179a9 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 69 6e urn code */. in
179aa 74 20 68 64 72 3b 20 20 20 20 20 20 20 20 2f 2a t hdr; /*
179ab 20 42 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 Beginning of th
179ac 65 20 68 65 61 64 65 72 2e 20 20 30 20 6d 6f 73 e header. 0 mos
179ad 74 20 70 61 67 65 73 2e 20 20 31 30 30 20 70 61 t pages. 100 pa
179ae 67 65 20 31 20 2a 2f 0a 0a 20 20 69 66 28 20 2a ge 1 */.. if( *
179af 70 52 43 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 pRC ) return;..
179b0 20 61 73 73 65 72 74 28 20 69 64 78 3e 3d 30 20 assert( idx>=0
179b1 26 26 20 69 64 78 3c 70 50 61 67 65 2d 3e 6e 43 && idx<pPage->nC
179b2 65 6c 6c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ell );. assert(
179b3 20 73 7a 3d 3d 63 65 6c 6c 53 69 7a 65 28 70 50 sz==cellSize(pP
179b4 61 67 65 2c 20 69 64 78 29 20 29 3b 0a 20 20 61 age, idx) );. a
179b5 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 ssert( sqlite3Pa
179b6 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 gerIswriteable(p
179b7 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 Page->pDbPage) )
179b8 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
179b9 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
179ba 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 Page->pBt->mutex
179bb 29 20 29 3b 0a 20 20 64 61 74 61 20 3d 20 70 50 ) );. data = pP
179bc 61 67 65 2d 3e 61 44 61 74 61 3b 0a 20 20 70 74 age->aData;. pt
179bd 72 20 3d 20 26 64 61 74 61 5b 70 50 61 67 65 2d r = &data[pPage-
179be 3e 63 65 6c 6c 4f 66 66 73 65 74 20 2b 20 32 2a >cellOffset + 2*
179bf 69 64 78 5d 3b 0a 20 20 70 63 20 3d 20 67 65 74 idx];. pc = get
179c0 32 62 79 74 65 28 70 74 72 29 3b 0a 20 20 68 64 2byte(ptr);. hd
179c1 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 r = pPage->hdrOf
179c2 66 73 65 74 3b 0a 20 20 74 65 73 74 63 61 73 65 fset;. testcase
179c3 28 20 70 63 3d 3d 67 65 74 32 62 79 74 65 28 26 ( pc==get2byte(&
179c4 64 61 74 61 5b 68 64 72 2b 35 5d 29 20 29 3b 0a data[hdr+5]) );.
179c5 20 20 74 65 73 74 63 61 73 65 28 20 70 63 2b 73 testcase( pc+s
179c6 7a 3d 3d 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 z==pPage->pBt->u
179c7 73 61 62 6c 65 53 69 7a 65 20 29 3b 0a 20 20 69 sableSize );. i
179c8 66 28 20 70 63 20 3c 20 67 65 74 32 62 79 74 65 f( pc < get2byte
179c9 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 20 7c (&data[hdr+5]) |
179ca 7c 20 70 63 2b 73 7a 20 3e 20 70 50 61 67 65 2d | pc+sz > pPage-
179cb 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 >pBt->usableSize
179cc 20 29 7b 0a 20 20 20 20 2a 70 52 43 20 3d 20 53 ){. *pRC = S
179cd 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b QLITE_CORRUPT_BK
179ce 50 54 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a PT;. return;.
179cf 20 20 7d 0a 20 20 72 63 20 3d 20 66 72 65 65 53 }. rc = freeS
179d0 70 61 63 65 28 70 50 61 67 65 2c 20 70 63 2c 20 pace(pPage, pc,
179d1 73 7a 29 3b 0a 20 20 69 66 28 20 72 63 20 29 7b sz);. if( rc ){
179d2 0a 20 20 20 20 2a 70 52 43 20 3d 20 72 63 3b 0a . *pRC = rc;.
179d3 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a return;. }.
179d4 20 20 66 6f 72 28 69 3d 69 64 78 2b 31 3b 20 69 for(i=idx+1; i
179d5 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 20 69 <pPage->nCell; i
179d6 2b 2b 2c 20 70 74 72 2b 3d 32 29 7b 0a 20 20 20 ++, ptr+=2){.
179d7 20 70 74 72 5b 30 5d 20 3d 20 70 74 72 5b 32 5d ptr[0] = ptr[2]
179d8 3b 0a 20 20 20 20 70 74 72 5b 31 5d 20 3d 20 70 ;. ptr[1] = p
179d9 74 72 5b 33 5d 3b 0a 20 20 7d 0a 20 20 70 50 61 tr[3];. }. pPa
179da 67 65 2d 3e 6e 43 65 6c 6c 2d 2d 3b 0a 20 20 70 ge->nCell--;. p
179db 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 ut2byte(&data[hd
179dc 72 2b 33 5d 2c 20 70 50 61 67 65 2d 3e 6e 43 65 r+3], pPage->nCe
179dd 6c 6c 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 46 ll);. pPage->nF
179de 72 65 65 20 2b 3d 20 32 3b 0a 7d 0a 0a 2f 2a 0a ree += 2;.}../*.
179df 2a 2a 20 49 6e 73 65 72 74 20 61 20 6e 65 77 20 ** Insert a new
179e0 63 65 6c 6c 20 6f 6e 20 70 50 61 67 65 20 61 74 cell on pPage at
179e1 20 63 65 6c 6c 20 69 6e 64 65 78 20 22 69 22 2e cell index "i".
179e2 20 20 70 43 65 6c 6c 20 70 6f 69 6e 74 73 20 74 pCell points t
179e3 6f 20 74 68 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74 o the.** content
179e4 20 6f 66 20 74 68 65 20 63 65 6c 6c 2e 0a 2a 2a of the cell..**
179e5 0a 2a 2a 20 49 66 20 74 68 65 20 63 65 6c 6c 20 .** If the cell
179e6 63 6f 6e 74 65 6e 74 20 77 69 6c 6c 20 66 69 74 content will fit
179e7 20 6f 6e 20 74 68 65 20 70 61 67 65 2c 20 74 68 on the page, th
179e8 65 6e 20 70 75 74 20 69 74 20 74 68 65 72 65 2e en put it there.
179e9 20 20 49 66 20 69 74 0a 2a 2a 20 77 69 6c 6c 20 If it.** will
179ea 6e 6f 74 20 66 69 74 2c 20 74 68 65 6e 20 6d 61 not fit, then ma
179eb 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 ke a copy of the
179ec 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 69 6e cell content in
179ed 74 6f 20 70 54 65 6d 70 20 69 66 0a 2a 2a 20 70 to pTemp if.** p
179ee 54 65 6d 70 20 69 73 20 6e 6f 74 20 6e 75 6c 6c Temp is not null
179ef 2e 20 20 52 65 67 61 72 64 6c 65 73 73 20 6f 66 . Regardless of
179f0 20 70 54 65 6d 70 2c 20 61 6c 6c 6f 63 61 74 65 pTemp, allocate
179f1 20 61 20 6e 65 77 20 65 6e 74 72 79 0a 2a 2a 20 a new entry.**
179f2 69 6e 20 70 50 61 67 65 2d 3e 61 4f 76 66 6c 5b in pPage->aOvfl[
179f3 5d 20 61 6e 64 20 6d 61 6b 65 20 69 74 20 70 6f ] and make it po
179f4 69 6e 74 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 int to the cell
179f5 63 6f 6e 74 65 6e 74 20 28 65 69 74 68 65 72 0a content (either.
179f6 2a 2a 20 69 6e 20 70 54 65 6d 70 20 6f 72 20 74 ** in pTemp or t
179f7 68 65 20 6f 72 69 67 69 6e 61 6c 20 70 43 65 6c he original pCel
179f8 6c 29 20 61 6e 64 20 61 6c 73 6f 20 72 65 63 6f l) and also reco
179f9 72 64 20 69 74 73 20 69 6e 64 65 78 2e 20 0a 2a rd its index. .*
179fa 2a 20 41 6c 6c 6f 63 61 74 69 6e 67 20 61 20 6e * Allocating a n
179fb 65 77 20 65 6e 74 72 79 20 69 6e 20 70 50 61 67 ew entry in pPag
179fc 65 2d 3e 61 43 65 6c 6c 5b 5d 20 69 6d 70 6c 69 e->aCell[] impli
179fd 65 73 20 74 68 61 74 20 0a 2a 2a 20 70 50 61 67 es that .** pPag
179fe 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 69 73 20 e->nOverflow is
179ff 69 6e 63 72 65 6d 65 6e 74 65 64 2e 0a 2a 2a 0a incremented..**.
17a00 2a 2a 20 49 66 20 6e 53 6b 69 70 20 69 73 20 6e ** If nSkip is n
17a01 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 64 6f on-zero, then do
17a02 20 6e 6f 74 20 63 6f 70 79 20 74 68 65 20 66 69 not copy the fi
17a03 72 73 74 20 6e 53 6b 69 70 20 62 79 74 65 73 20 rst nSkip bytes
17a04 6f 66 20 74 68 65 0a 2a 2a 20 63 65 6c 6c 2e 20 of the.** cell.
17a05 54 68 65 20 63 61 6c 6c 65 72 20 77 69 6c 6c 20 The caller will
17a06 6f 76 65 72 77 72 69 74 65 20 74 68 65 6d 20 61 overwrite them a
17a07 66 74 65 72 20 74 68 69 73 20 66 75 6e 63 74 69 fter this functi
17a08 6f 6e 20 72 65 74 75 72 6e 73 2e 20 49 66 0a 2a on returns. If.*
17a09 2a 20 6e 53 6b 69 70 20 69 73 20 6e 6f 6e 2d 7a * nSkip is non-z
17a0a 65 72 6f 2c 20 74 68 65 6e 20 70 43 65 6c 6c 20 ero, then pCell
17a0b 6d 61 79 20 6e 6f 74 20 70 6f 69 6e 74 20 74 6f may not point to
17a0c 20 61 6e 20 69 6e 76 61 6c 69 64 20 6d 65 6d 6f an invalid memo
17a0d 72 79 20 6c 6f 63 61 74 69 6f 6e 20 0a 2a 2a 20 ry location .**
17a0e 28 62 75 74 20 70 43 65 6c 6c 2b 6e 53 6b 69 70 (but pCell+nSkip
17a0f 20 69 73 20 61 6c 77 61 79 73 20 76 61 6c 69 64 is always valid
17a10 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 )..*/.static voi
17a11 64 20 69 6e 73 65 72 74 43 65 6c 6c 28 0a 20 20 d insertCell(.
17a12 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 MemPage *pPage,
17a13 20 20 2f 2a 20 50 61 67 65 20 69 6e 74 6f 20 77 /* Page into w
17a14 68 69 63 68 20 77 65 20 61 72 65 20 63 6f 70 79 hich we are copy
17a15 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 69 2c 20 ing */. int i,
17a16 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 /* Ne
17a17 77 20 63 65 6c 6c 20 62 65 63 6f 6d 65 73 20 74 w cell becomes t
17a18 68 65 20 69 2d 74 68 20 63 65 6c 6c 20 6f 66 20 he i-th cell of
17a19 74 68 65 20 70 61 67 65 20 2a 2f 0a 20 20 75 38 the page */. u8
17a1a 20 2a 70 43 65 6c 6c 2c 20 20 20 20 20 20 20 20 *pCell,
17a1b 2f 2a 20 43 6f 6e 74 65 6e 74 20 6f 66 20 74 68 /* Content of th
17a1c 65 20 6e 65 77 20 63 65 6c 6c 20 2a 2f 0a 20 20 e new cell */.
17a1d 69 6e 74 20 73 7a 2c 20 20 20 20 20 20 20 20 20 int sz,
17a1e 20 20 2f 2a 20 42 79 74 65 73 20 6f 66 20 63 6f /* Bytes of co
17a1f 6e 74 65 6e 74 20 69 6e 20 70 43 65 6c 6c 20 2a ntent in pCell *
17a20 2f 0a 20 20 75 38 20 2a 70 54 65 6d 70 2c 20 20 /. u8 *pTemp,
17a21 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 20 73 74 /* Temp st
17a22 6f 72 61 67 65 20 73 70 61 63 65 20 66 6f 72 20 orage space for
17a23 70 43 65 6c 6c 2c 20 69 66 20 6e 65 65 64 65 64 pCell, if needed
17a24 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 43 68 69 6c */. Pgno iChil
17a25 64 2c 20 20 20 20 20 20 2f 2a 20 49 66 20 6e 6f d, /* If no
17a26 6e 2d 7a 65 72 6f 2c 20 72 65 70 6c 61 63 65 20 n-zero, replace
17a27 66 69 72 73 74 20 34 20 62 79 74 65 73 20 77 69 first 4 bytes wi
17a28 74 68 20 74 68 69 73 20 76 61 6c 75 65 20 2a 2f th this value */
17a29 0a 20 20 69 6e 74 20 2a 70 52 43 20 20 20 20 20 . int *pRC
17a2a 20 20 20 20 20 2f 2a 20 52 65 61 64 20 61 6e 64 /* Read and
17a2b 20 77 72 69 74 65 20 72 65 74 75 72 6e 20 63 6f write return co
17a2c 64 65 20 66 72 6f 6d 20 68 65 72 65 20 2a 2f 0a de from here */.
17a2d 29 7b 0a 20 20 69 6e 74 20 69 64 78 3b 20 20 20 ){. int idx;
17a2e 20 20 20 20 20 20 20 2f 2a 20 57 68 65 72 65 20 /* Where
17a2f 74 6f 20 77 72 69 74 65 20 6e 65 77 20 63 65 6c to write new cel
17a30 6c 20 63 6f 6e 74 65 6e 74 20 69 6e 20 64 61 74 l content in dat
17a31 61 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6a 3b 20 a[] */. int j;
17a32 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f /* Lo
17a33 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 op counter */.
17a34 69 6e 74 20 65 6e 64 3b 20 20 20 20 20 20 20 20 int end;
17a35 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74 65 20 /* First byte
17a36 70 61 73 74 20 74 68 65 20 6c 61 73 74 20 63 65 past the last ce
17a37 6c 6c 20 70 6f 69 6e 74 65 72 20 69 6e 20 64 61 ll pointer in da
17a38 74 61 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 6e ta[] */. int in
17a39 73 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 s; /* I
17a3a 6e 64 65 78 20 69 6e 20 64 61 74 61 5b 5d 20 77 ndex in data[] w
17a3b 68 65 72 65 20 6e 65 77 20 63 65 6c 6c 20 70 6f here new cell po
17a3c 69 6e 74 65 72 20 69 73 20 69 6e 73 65 72 74 65 inter is inserte
17a3d 64 20 2a 2f 0a 20 20 69 6e 74 20 63 65 6c 6c 4f d */. int cellO
17a3e 66 66 73 65 74 3b 20 20 20 2f 2a 20 41 64 64 72 ffset; /* Addr
17a3f 65 73 73 20 6f 66 20 66 69 72 73 74 20 63 65 6c ess of first cel
17a40 6c 20 70 6f 69 6e 74 65 72 20 69 6e 20 64 61 74 l pointer in dat
17a41 61 5b 5d 20 2a 2f 0a 20 20 75 38 20 2a 64 61 74 a[] */. u8 *dat
17a42 61 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 a; /* Th
17a43 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 e content of the
17a44 20 77 68 6f 6c 65 20 70 61 67 65 20 2a 2f 0a 20 whole page */.
17a45 20 75 38 20 2a 70 74 72 3b 20 20 20 20 20 20 20 u8 *ptr;
17a46 20 20 20 2f 2a 20 55 73 65 64 20 66 6f 72 20 6d /* Used for m
17a47 6f 76 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69 6f oving informatio
17a48 6e 20 61 72 6f 75 6e 64 20 69 6e 20 64 61 74 61 n around in data
17a49 5b 5d 20 2a 2f 0a 0a 20 20 69 6e 74 20 6e 53 6b [] */.. int nSk
17a4a 69 70 20 3d 20 28 69 43 68 69 6c 64 20 3f 20 34 ip = (iChild ? 4
17a4b 20 3a 20 30 29 3b 0a 0a 20 20 69 66 28 20 2a 70 : 0);.. if( *p
17a4c 52 43 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 RC ) return;..
17a4d 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 assert( i>=0 &&
17a4e 69 3c 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 2b i<=pPage->nCell+
17a4f 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 pPage->nOverflow
17a50 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 );. assert( pP
17a51 61 67 65 2d 3e 6e 43 65 6c 6c 3c 3d 4d 58 5f 43 age->nCell<=MX_C
17a52 45 4c 4c 28 70 50 61 67 65 2d 3e 70 42 74 29 20 ELL(pPage->pBt)
17a53 26 26 20 4d 58 5f 43 45 4c 4c 28 70 50 61 67 65 && MX_CELL(pPage
17a54 2d 3e 70 42 74 29 3c 3d 35 34 36 30 20 29 3b 0a ->pBt)<=5460 );.
17a55 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d assert( pPage-
17a56 3e 6e 4f 76 65 72 66 6c 6f 77 3c 3d 41 72 72 61 >nOverflow<=Arra
17a57 79 53 69 7a 65 28 70 50 61 67 65 2d 3e 61 4f 76 ySize(pPage->aOv
17a58 66 6c 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 fl) );. assert(
17a59 20 73 7a 3d 3d 63 65 6c 6c 53 69 7a 65 50 74 72 sz==cellSizePtr
17a5a 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 29 20 29 (pPage, pCell) )
17a5b 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
17a5c 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
17a5d 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 Page->pBt->mutex
17a5e 29 20 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 ) );. if( pPage
17a5f 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 7c 7c 20 73 ->nOverflow || s
17a60 7a 2b 32 3e 70 50 61 67 65 2d 3e 6e 46 72 65 65 z+2>pPage->nFree
17a61 20 29 7b 0a 20 20 20 20 69 66 28 20 70 54 65 6d ){. if( pTem
17a62 70 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 p ){. memcp
17a63 79 28 70 54 65 6d 70 2b 6e 53 6b 69 70 2c 20 70 y(pTemp+nSkip, p
17a64 43 65 6c 6c 2b 6e 53 6b 69 70 2c 20 73 7a 2d 6e Cell+nSkip, sz-n
17a65 53 6b 69 70 29 3b 0a 20 20 20 20 20 20 70 43 65 Skip);. pCe
17a66 6c 6c 20 3d 20 70 54 65 6d 70 3b 0a 20 20 20 20 ll = pTemp;.
17a67 7d 0a 20 20 20 20 69 66 28 20 69 43 68 69 6c 64 }. if( iChild
17a68 20 29 7b 0a 20 20 20 20 20 20 70 75 74 34 62 79 ){. put4by
17a69 74 65 28 70 43 65 6c 6c 2c 20 69 43 68 69 6c 64 te(pCell, iChild
17a6a 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6a 20 3d );. }. j =
17a6b 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f pPage->nOverflo
17a6c 77 2b 2b 3b 0a 20 20 20 20 61 73 73 65 72 74 28 w++;. assert(
17a6d 20 6a 3c 28 69 6e 74 29 28 73 69 7a 65 6f 66 28 j<(int)(sizeof(
17a6e 70 50 61 67 65 2d 3e 61 4f 76 66 6c 29 2f 73 69 pPage->aOvfl)/si
17a6f 7a 65 6f 66 28 70 50 61 67 65 2d 3e 61 4f 76 66 zeof(pPage->aOvf
17a70 6c 5b 30 5d 29 29 20 29 3b 0a 20 20 20 20 70 50 l[0])) );. pP
17a71 61 67 65 2d 3e 61 4f 76 66 6c 5b 6a 5d 2e 70 43 age->aOvfl[j].pC
17a72 65 6c 6c 20 3d 20 70 43 65 6c 6c 3b 0a 20 20 20 ell = pCell;.
17a73 20 70 50 61 67 65 2d 3e 61 4f 76 66 6c 5b 6a 5d pPage->aOvfl[j]
17a74 2e 69 64 78 20 3d 20 28 75 31 36 29 69 3b 0a 20 .idx = (u16)i;.
17a75 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 }else{. int
17a76 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
17a77 72 57 72 69 74 65 28 70 50 61 67 65 2d 3e 70 44 rWrite(pPage->pD
17a78 62 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 bPage);. if(
17a79 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
17a7a 0a 20 20 20 20 20 20 2a 70 52 43 20 3d 20 72 63 . *pRC = rc
17a7b 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a ;. return;.
17a7c 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 }. assert
17a7d 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 ( sqlite3PagerIs
17a7e 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d writeable(pPage-
17a7f 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 20 >pDbPage) );.
17a80 20 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 data = pPage->a
17a81 44 61 74 61 3b 0a 20 20 20 20 63 65 6c 6c 4f 66 Data;. cellOf
17a82 66 73 65 74 20 3d 20 70 50 61 67 65 2d 3e 63 65 fset = pPage->ce
17a83 6c 6c 4f 66 66 73 65 74 3b 0a 20 20 20 20 65 6e llOffset;. en
17a84 64 20 3d 20 63 65 6c 6c 4f 66 66 73 65 74 20 2b d = cellOffset +
17a85 20 32 2a 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 2*pPage->nCell;
17a86 0a 20 20 20 20 69 6e 73 20 3d 20 63 65 6c 6c 4f . ins = cellO
17a87 66 66 73 65 74 20 2b 20 32 2a 69 3b 0a 20 20 20 ffset + 2*i;.
17a88 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74 65 53 70 rc = allocateSp
17a89 61 63 65 28 70 50 61 67 65 2c 20 73 7a 2c 20 26 ace(pPage, sz, &
17a8a 69 64 78 29 3b 0a 20 20 20 20 69 66 28 20 72 63 idx);. if( rc
17a8b 20 29 7b 20 2a 70 52 43 20 3d 20 72 63 3b 20 72 ){ *pRC = rc; r
17a8c 65 74 75 72 6e 3b 20 7d 0a 20 20 20 20 2f 2a 20 eturn; }. /*
17a8d 54 68 65 20 61 6c 6c 6f 63 61 74 65 53 70 61 63 The allocateSpac
17a8e 65 28 29 20 72 6f 75 74 69 6e 65 20 67 75 61 72 e() routine guar
17a8f 61 6e 74 65 65 73 20 74 68 65 20 66 6f 6c 6c 6f antees the follo
17a90 77 69 6e 67 20 74 77 6f 20 70 72 6f 70 65 72 74 wing two propert
17a91 69 65 73 0a 20 20 20 20 2a 2a 20 69 66 20 69 74 ies. ** if it
17a92 20 72 65 74 75 72 6e 73 20 73 75 63 63 65 73 73 returns success
17a93 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 */. assert(
17a94 69 64 78 20 3e 3d 20 65 6e 64 2b 32 20 29 3b 0a idx >= end+2 );.
17a95 20 20 20 20 61 73 73 65 72 74 28 20 69 64 78 2b assert( idx+
17a96 73 7a 20 3c 3d 20 70 50 61 67 65 2d 3e 70 42 74 sz <= pPage->pBt
17a97 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 29 3b 0a ->usableSize );.
17a98 20 20 20 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c pPage->nCell
17a99 2b 2b 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6e ++;. pPage->n
17a9a 46 72 65 65 20 2d 3d 20 28 75 31 36 29 28 32 20 Free -= (u16)(2
17a9b 2b 20 73 7a 29 3b 0a 20 20 20 20 6d 65 6d 63 70 + sz);. memcp
17a9c 79 28 26 64 61 74 61 5b 69 64 78 2b 6e 53 6b 69 y(&data[idx+nSki
17a9d 70 5d 2c 20 70 43 65 6c 6c 2b 6e 53 6b 69 70 2c p], pCell+nSkip,
17a9e 20 73 7a 2d 6e 53 6b 69 70 29 3b 0a 20 20 20 20 sz-nSkip);.
17a9f 69 66 28 20 69 43 68 69 6c 64 20 29 7b 0a 20 20 if( iChild ){.
17aa0 20 20 20 20 70 75 74 34 62 79 74 65 28 26 64 61 put4byte(&da
17aa1 74 61 5b 69 64 78 5d 2c 20 69 43 68 69 6c 64 29 ta[idx], iChild)
17aa2 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 ;. }. for(
17aa3 6a 3d 65 6e 64 2c 20 70 74 72 3d 26 64 61 74 61 j=end, ptr=&data
17aa4 5b 6a 5d 3b 20 6a 3e 69 6e 73 3b 20 6a 2d 3d 32 [j]; j>ins; j-=2
17aa5 2c 20 70 74 72 2d 3d 32 29 7b 0a 20 20 20 20 20 , ptr-=2){.
17aa6 20 70 74 72 5b 30 5d 20 3d 20 70 74 72 5b 2d 32 ptr[0] = ptr[-2
17aa7 5d 3b 0a 20 20 20 20 20 20 70 74 72 5b 31 5d 20 ];. ptr[1]
17aa8 3d 20 70 74 72 5b 2d 31 5d 3b 0a 20 20 20 20 7d = ptr[-1];. }
17aa9 0a 20 20 20 20 70 75 74 32 62 79 74 65 28 26 64 . put2byte(&d
17aaa 61 74 61 5b 69 6e 73 5d 2c 20 69 64 78 29 3b 0a ata[ins], idx);.
17aab 20 20 20 20 70 75 74 32 62 79 74 65 28 26 64 61 put2byte(&da
17aac 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 ta[pPage->hdrOff
17aad 73 65 74 2b 33 5d 2c 20 70 50 61 67 65 2d 3e 6e set+3], pPage->n
17aae 43 65 6c 6c 29 3b 0a 23 69 66 6e 64 65 66 20 53 Cell);.#ifndef S
17aaf 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 QLITE_OMIT_AUTOV
17ab0 41 43 55 55 4d 0a 20 20 20 20 69 66 28 20 70 50 ACUUM. if( pP
17ab1 61 67 65 2d 3e 70 42 74 2d 3e 61 75 74 6f 56 61 age->pBt->autoVa
17ab2 63 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 2f 2a cuum ){. /*
17ab3 20 54 68 65 20 63 65 6c 6c 20 6d 61 79 20 63 6f The cell may co
17ab4 6e 74 61 69 6e 20 61 20 70 6f 69 6e 74 65 72 20 ntain a pointer
17ab5 74 6f 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 to an overflow p
17ab6 61 67 65 2e 20 49 66 20 73 6f 2c 20 77 72 69 74 age. If so, writ
17ab7 65 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 65 e. ** the e
17ab8 6e 74 72 79 20 66 6f 72 20 74 68 65 20 6f 76 65 ntry for the ove
17ab9 72 66 6c 6f 77 20 70 61 67 65 20 69 6e 74 6f 20 rflow page into
17aba 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 2e the pointer map.
17abb 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
17abc 70 74 72 6d 61 70 50 75 74 4f 76 66 6c 50 74 72 ptrmapPutOvflPtr
17abd 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20 70 (pPage, pCell, p
17abe 52 43 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 RC);. }.#endi
17abf 66 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 f. }.}../*.** A
17ac0 64 64 20 61 20 6c 69 73 74 20 6f 66 20 63 65 6c dd a list of cel
17ac1 6c 73 20 74 6f 20 61 20 70 61 67 65 2e 20 20 54 ls to a page. T
17ac2 68 65 20 70 61 67 65 20 73 68 6f 75 6c 64 20 62 he page should b
17ac3 65 20 69 6e 69 74 69 61 6c 6c 79 20 65 6d 70 74 e initially empt
17ac4 79 2e 0a 2a 2a 20 54 68 65 20 63 65 6c 6c 73 20 y..** The cells
17ac5 61 72 65 20 67 75 61 72 61 6e 74 65 65 64 20 74 are guaranteed t
17ac6 6f 20 66 69 74 20 6f 6e 20 74 68 65 20 70 61 67 o fit on the pag
17ac7 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 e..*/.static voi
17ac8 64 20 61 73 73 65 6d 62 6c 65 50 61 67 65 28 0a d assemblePage(.
17ac9 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 MemPage *pPage
17aca 2c 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65 20 , /* The page
17acb 74 6f 20 62 65 20 61 73 73 65 6d 62 6c 69 65 64 to be assemblied
17acc 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 2c */. int nCell,
17acd 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e /* The n
17ace 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 74 umber of cells t
17acf 6f 20 61 64 64 20 74 6f 20 74 68 69 73 20 70 61 o add to this pa
17ad0 67 65 20 2a 2f 0a 20 20 75 38 20 2a 2a 61 70 43 ge */. u8 **apC
17ad1 65 6c 6c 2c 20 20 20 20 20 20 2f 2a 20 50 6f 69 ell, /* Poi
17ad2 6e 74 65 72 73 20 74 6f 20 63 65 6c 6c 20 62 6f nters to cell bo
17ad3 64 69 65 73 20 2a 2f 0a 20 20 75 31 36 20 2a 61 dies */. u16 *a
17ad4 53 69 7a 65 20 20 20 20 20 20 20 20 2f 2a 20 53 Size /* S
17ad5 69 7a 65 73 20 6f 66 20 74 68 65 20 63 65 6c 6c izes of the cell
17ad6 73 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b s */.){. int i;
17ad7 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c /* L
17ad8 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 oop counter */.
17ad9 20 75 38 20 2a 70 43 65 6c 6c 70 74 72 3b 20 20 u8 *pCellptr;
17ada 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 /* Address of
17adb 20 6e 65 78 74 20 63 65 6c 6c 20 70 6f 69 6e 74 next cell point
17adc 65 72 20 2a 2f 0a 20 20 69 6e 74 20 63 65 6c 6c er */. int cell
17add 62 6f 64 79 3b 20 20 20 20 20 2f 2a 20 41 64 64 body; /* Add
17ade 72 65 73 73 20 6f 66 20 6e 65 78 74 20 63 65 6c ress of next cel
17adf 6c 20 62 6f 64 79 20 2a 2f 0a 20 20 75 38 20 2a l body */. u8 *
17ae0 20 63 6f 6e 73 74 20 64 61 74 61 20 3d 20 70 50 const data = pP
17ae1 61 67 65 2d 3e 61 44 61 74 61 3b 20 20 20 20 20 age->aData;
17ae2 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 /* Point
17ae3 65 72 20 74 6f 20 64 61 74 61 20 66 6f 72 20 70 er to data for p
17ae4 50 61 67 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 Page */. const
17ae5 69 6e 74 20 68 64 72 20 3d 20 70 50 61 67 65 2d int hdr = pPage-
17ae6 3e 68 64 72 4f 66 66 73 65 74 3b 20 20 20 20 20 >hdrOffset;
17ae7 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 /* Offset
17ae8 6f 66 20 68 65 61 64 65 72 20 6f 6e 20 70 50 61 of header on pPa
17ae9 67 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 69 6e ge */. const in
17aea 74 20 6e 55 73 61 62 6c 65 20 3d 20 70 50 61 67 t nUsable = pPag
17aeb 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 e->pBt->usableSi
17aec 7a 65 3b 20 2f 2a 20 55 73 61 62 6c 65 20 73 69 ze; /* Usable si
17aed 7a 65 20 6f 66 20 70 61 67 65 20 2a 2f 0a 0a 20 ze of page */..
17aee 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e assert( pPage->
17aef 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 29 3b 0a nOverflow==0 );.
17af0 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
17af1 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 3_mutex_held(pPa
17af2 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 ge->pBt->mutex)
17af3 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 43 65 );. assert( nCe
17af4 6c 6c 3e 3d 30 20 26 26 20 6e 43 65 6c 6c 3c 3d ll>=0 && nCell<=
17af5 4d 58 5f 43 45 4c 4c 28 70 50 61 67 65 2d 3e 70 MX_CELL(pPage->p
17af6 42 74 29 20 26 26 20 4d 58 5f 43 45 4c 4c 28 70 Bt) && MX_CELL(p
17af7 50 61 67 65 2d 3e 70 42 74 29 3c 3d 35 34 36 30 Page->pBt)<=5460
17af8 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 );. assert( sq
17af9 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 lite3PagerIswrit
17afa 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 eable(pPage->pDb
17afb 50 61 67 65 29 20 29 3b 0a 0a 20 20 2f 2a 20 43 Page) );.. /* C
17afc 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 70 61 heck that the pa
17afd 67 65 20 68 61 73 20 6a 75 73 74 20 62 65 65 6e ge has just been
17afe 20 7a 65 72 6f 65 64 20 62 79 20 7a 65 72 6f 50 zeroed by zeroP
17aff 61 67 65 28 29 20 2a 2f 0a 20 20 61 73 73 65 72 age() */. asser
17b00 74 28 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3d t( pPage->nCell=
17b01 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
17b02 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 get2byte(&data[h
17b03 64 72 2b 35 5d 29 3d 3d 6e 55 73 61 62 6c 65 20 dr+5])==nUsable
17b04 29 3b 0a 0a 20 20 70 43 65 6c 6c 70 74 72 20 3d );.. pCellptr =
17b05 20 26 64 61 74 61 5b 70 50 61 67 65 2d 3e 63 65 &data[pPage->ce
17b06 6c 6c 4f 66 66 73 65 74 20 2b 20 6e 43 65 6c 6c llOffset + nCell
17b07 2a 32 5d 3b 0a 20 20 63 65 6c 6c 62 6f 64 79 20 *2];. cellbody
17b08 3d 20 6e 55 73 61 62 6c 65 3b 0a 20 20 66 6f 72 = nUsable;. for
17b09 28 69 3d 6e 43 65 6c 6c 2d 31 3b 20 69 3e 3d 30 (i=nCell-1; i>=0
17b0a 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 70 43 65 6c ; i--){. pCel
17b0b 6c 70 74 72 20 2d 3d 20 32 3b 0a 20 20 20 20 63 lptr -= 2;. c
17b0c 65 6c 6c 62 6f 64 79 20 2d 3d 20 61 53 69 7a 65 ellbody -= aSize
17b0d 5b 69 5d 3b 0a 20 20 20 20 70 75 74 32 62 79 74 [i];. put2byt
17b0e 65 28 70 43 65 6c 6c 70 74 72 2c 20 63 65 6c 6c e(pCellptr, cell
17b0f 62 6f 64 79 29 3b 0a 20 20 20 20 6d 65 6d 63 70 body);. memcp
17b10 79 28 26 64 61 74 61 5b 63 65 6c 6c 62 6f 64 79 y(&data[cellbody
17b11 5d 2c 20 61 70 43 65 6c 6c 5b 69 5d 2c 20 61 53 ], apCell[i], aS
17b12 69 7a 65 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 70 ize[i]);. }. p
17b13 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 ut2byte(&data[hd
17b14 72 2b 33 5d 2c 20 6e 43 65 6c 6c 29 3b 0a 20 20 r+3], nCell);.
17b15 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 put2byte(&data[h
17b16 64 72 2b 35 5d 2c 20 63 65 6c 6c 62 6f 64 79 29 dr+5], cellbody)
17b17 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 ;. pPage->nFree
17b18 20 2d 3d 20 28 6e 43 65 6c 6c 2a 32 20 2b 20 6e -= (nCell*2 + n
17b19 55 73 61 62 6c 65 20 2d 20 63 65 6c 6c 62 6f 64 Usable - cellbod
17b1a 79 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 43 65 y);. pPage->nCe
17b1b 6c 6c 20 3d 20 28 75 31 36 29 6e 43 65 6c 6c 3b ll = (u16)nCell;
17b1c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f .}../*.** The fo
17b1d 6c 6c 6f 77 69 6e 67 20 70 61 72 61 6d 65 74 65 llowing paramete
17b1e 72 73 20 64 65 74 65 72 6d 69 6e 65 20 68 6f 77 rs determine how
17b1f 20 6d 61 6e 79 20 61 64 6a 61 63 65 6e 74 20 70 many adjacent p
17b20 61 67 65 73 20 67 65 74 20 69 6e 76 6f 6c 76 65 ages get involve
17b21 64 0a 2a 2a 20 69 6e 20 61 20 62 61 6c 61 6e 63 d.** in a balanc
17b22 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 ing operation.
17b23 4e 4e 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 NN is the number
17b24 20 6f 66 20 6e 65 69 67 68 62 6f 72 73 20 6f 6e of neighbors on
17b25 20 65 69 74 68 65 72 20 73 69 64 65 0a 2a 2a 20 either side.**
17b26 6f 66 20 74 68 65 20 70 61 67 65 20 74 68 61 74 of the page that
17b27 20 70 61 72 74 69 63 69 70 61 74 65 20 69 6e 20 participate in
17b28 74 68 65 20 62 61 6c 61 6e 63 69 6e 67 20 6f 70 the balancing op
17b29 65 72 61 74 69 6f 6e 2e 20 20 4e 42 20 69 73 20 eration. NB is
17b2a 74 68 65 0a 2a 2a 20 74 6f 74 61 6c 20 6e 75 6d the.** total num
17b2b 62 65 72 20 6f 66 20 70 61 67 65 73 20 74 68 61 ber of pages tha
17b2c 74 20 70 61 72 74 69 63 69 70 61 74 65 2c 20 69 t participate, i
17b2d 6e 63 6c 75 64 69 6e 67 20 74 68 65 20 74 61 72 ncluding the tar
17b2e 67 65 74 20 70 61 67 65 20 61 6e 64 0a 2a 2a 20 get page and.**
17b2f 4e 4e 20 6e 65 69 67 68 62 6f 72 73 20 6f 6e 20 NN neighbors on
17b30 65 69 74 68 65 72 20 73 69 64 65 2e 0a 2a 2a 0a either side..**.
17b31 2a 2a 20 54 68 65 20 6d 69 6e 69 6d 75 6d 20 76 ** The minimum v
17b32 61 6c 75 65 20 6f 66 20 4e 4e 20 69 73 20 31 20 alue of NN is 1
17b33 28 6f 66 20 63 6f 75 72 73 65 29 2e 20 20 49 6e (of course). In
17b34 63 72 65 61 73 69 6e 67 20 4e 4e 20 61 62 6f 76 creasing NN abov
17b35 65 20 31 0a 2a 2a 20 28 74 6f 20 32 20 6f 72 20 e 1.** (to 2 or
17b36 33 29 20 67 69 76 65 73 20 61 20 6d 6f 64 65 73 3) gives a modes
17b37 74 20 69 6d 70 72 6f 76 65 6d 65 6e 74 20 69 6e t improvement in
17b38 20 53 45 4c 45 43 54 20 61 6e 64 20 44 45 4c 45 SELECT and DELE
17b39 54 45 20 70 65 72 66 6f 72 6d 61 6e 63 65 0a 2a TE performance.*
17b3a 2a 20 69 6e 20 65 78 63 68 61 6e 67 65 20 66 6f * in exchange fo
17b3b 72 20 61 20 6c 61 72 67 65 72 20 64 65 67 72 61 r a larger degra
17b3c 64 61 74 69 6f 6e 20 69 6e 20 49 4e 53 45 52 54 dation in INSERT
17b3d 20 61 6e 64 20 55 50 44 41 54 45 20 70 65 72 66 and UPDATE perf
17b3e 6f 72 6d 61 6e 63 65 2e 0a 2a 2a 20 54 68 65 20 ormance..** The
17b3f 76 61 6c 75 65 20 6f 66 20 4e 4e 20 61 70 70 65 value of NN appe
17b40 61 72 73 20 74 6f 20 67 69 76 65 20 74 68 65 20 ars to give the
17b41 62 65 73 74 20 72 65 73 75 6c 74 73 20 6f 76 65 best results ove
17b42 72 61 6c 6c 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 rall..*/.#define
17b43 20 4e 4e 20 31 20 20 20 20 20 20 20 20 20 20 20 NN 1
17b44 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e /* Number of n
17b45 65 69 67 68 62 6f 72 73 20 6f 6e 20 65 69 74 68 eighbors on eith
17b46 65 72 20 73 69 64 65 20 6f 66 20 70 50 61 67 65 er side of pPage
17b47 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4e 42 20 28 */.#define NB (
17b48 4e 4e 2a 32 2b 31 29 20 20 20 20 20 20 2f 2a 20 NN*2+1) /*
17b49 54 6f 74 61 6c 20 70 61 67 65 73 20 69 6e 76 6f Total pages invo
17b4a 6c 76 65 64 20 69 6e 20 74 68 65 20 62 61 6c 61 lved in the bala
17b4b 6e 63 65 20 2a 2f 0a 0a 0a 23 69 66 6e 64 65 66 nce */...#ifndef
17b4c 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 51 55 49 SQLITE_OMIT_QUI
17b4d 43 4b 42 41 4c 41 4e 43 45 0a 2f 2a 0a 2a 2a 20 CKBALANCE./*.**
17b4e 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 This version of
17b4f 62 61 6c 61 6e 63 65 28 29 20 68 61 6e 64 6c 65 balance() handle
17b50 73 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 73 70 65 s the common spe
17b51 63 69 61 6c 20 63 61 73 65 20 77 68 65 72 65 0a cial case where.
17b52 2a 2a 20 61 20 6e 65 77 20 65 6e 74 72 79 20 69 ** a new entry i
17b53 73 20 62 65 69 6e 67 20 69 6e 73 65 72 74 65 64 s being inserted
17b54 20 6f 6e 20 74 68 65 20 65 78 74 72 65 6d 65 20 on the extreme
17b55 72 69 67 68 74 2d 65 6e 64 20 6f 66 20 74 68 65 right-end of the
17b56 0a 2a 2a 20 74 72 65 65 2c 20 69 6e 20 6f 74 68 .** tree, in oth
17b57 65 72 20 77 6f 72 64 73 2c 20 77 68 65 6e 20 74 er words, when t
17b58 68 65 20 6e 65 77 20 65 6e 74 72 79 20 77 69 6c he new entry wil
17b59 6c 20 62 65 63 6f 6d 65 20 74 68 65 20 6c 61 72 l become the lar
17b5a 67 65 73 74 0a 2a 2a 20 65 6e 74 72 79 20 69 6e gest.** entry in
17b5b 20 74 68 65 20 74 72 65 65 2e 0a 2a 2a 0a 2a 2a the tree..**.**
17b5c 20 49 6e 73 74 65 61 64 20 6f 66 20 74 72 79 69 Instead of tryi
17b5d 6e 67 20 74 6f 20 62 61 6c 61 6e 63 65 20 74 68 ng to balance th
17b5e 65 20 33 20 72 69 67 68 74 2d 6d 6f 73 74 20 6c e 3 right-most l
17b5f 65 61 66 20 70 61 67 65 73 2c 20 6a 75 73 74 20 eaf pages, just
17b60 61 64 64 0a 2a 2a 20 61 20 6e 65 77 20 70 61 67 add.** a new pag
17b61 65 20 74 6f 20 74 68 65 20 72 69 67 68 74 2d 68 e to the right-h
17b62 61 6e 64 20 73 69 64 65 20 61 6e 64 20 70 75 74 and side and put
17b63 20 74 68 65 20 6f 6e 65 20 6e 65 77 20 65 6e 74 the one new ent
17b64 72 79 20 69 6e 0a 2a 2a 20 74 68 61 74 20 70 61 ry in.** that pa
17b65 67 65 2e 20 20 54 68 69 73 20 6c 65 61 76 65 73 ge. This leaves
17b66 20 74 68 65 20 72 69 67 68 74 20 73 69 64 65 20 the right side
17b67 6f 66 20 74 68 65 20 74 72 65 65 20 73 6f 6d 65 of the tree some
17b68 77 68 61 74 0a 2a 2a 20 75 6e 62 61 6c 61 6e 63 what.** unbalanc
17b69 65 64 2e 20 20 42 75 74 20 6f 64 64 73 20 61 72 ed. But odds ar
17b6a 65 20 74 68 61 74 20 77 65 20 77 69 6c 6c 20 62 e that we will b
17b6b 65 20 69 6e 73 65 72 74 69 6e 67 20 6e 65 77 20 e inserting new
17b6c 65 6e 74 72 69 65 73 0a 2a 2a 20 61 74 20 74 68 entries.** at th
17b6d 65 20 65 6e 64 20 73 6f 6f 6e 20 61 66 74 65 72 e end soon after
17b6e 77 61 72 64 73 20 73 6f 20 74 68 65 20 6e 65 61 wards so the nea
17b6f 72 6c 79 20 65 6d 70 74 79 20 70 61 67 65 20 77 rly empty page w
17b70 69 6c 6c 20 71 75 69 63 6b 6c 79 0a 2a 2a 20 66 ill quickly.** f
17b71 69 6c 6c 20 75 70 2e 20 20 4f 6e 20 61 76 65 72 ill up. On aver
17b72 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 70 50 61 67 65 age..**.** pPage
17b73 20 69 73 20 74 68 65 20 6c 65 61 66 20 70 61 67 is the leaf pag
17b74 65 20 77 68 69 63 68 20 69 73 20 74 68 65 20 72 e which is the r
17b75 69 67 68 74 2d 6d 6f 73 74 20 70 61 67 65 20 69 ight-most page i
17b76 6e 20 74 68 65 20 74 72 65 65 2e 0a 2a 2a 20 70 n the tree..** p
17b77 50 61 72 65 6e 74 20 69 73 20 69 74 73 20 70 61 Parent is its pa
17b78 72 65 6e 74 2e 20 20 70 50 61 67 65 20 6d 75 73 rent. pPage mus
17b79 74 20 68 61 76 65 20 61 20 73 69 6e 67 6c 65 20 t have a single
17b7a 6f 76 65 72 66 6c 6f 77 20 65 6e 74 72 79 0a 2a overflow entry.*
17b7b 2a 20 77 68 69 63 68 20 69 73 20 61 6c 73 6f 20 * which is also
17b7c 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 65 the right-most e
17b7d 6e 74 72 79 20 6f 6e 20 74 68 65 20 70 61 67 65 ntry on the page
17b7e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 53 70 61 ..**.** The pSpa
17b7f 63 65 20 62 75 66 66 65 72 20 69 73 20 75 73 65 ce buffer is use
17b80 64 20 74 6f 20 73 74 6f 72 65 20 61 20 74 65 6d d to store a tem
17b81 70 6f 72 61 72 79 20 63 6f 70 79 20 6f 66 20 74 porary copy of t
17b82 68 65 20 64 69 76 69 64 65 72 0a 2a 2a 20 63 65 he divider.** ce
17b83 6c 6c 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 ll that will be
17b84 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 70 50 inserted into pP
17b85 61 72 65 6e 74 2e 20 53 75 63 68 20 61 20 63 65 arent. Such a ce
17b86 6c 6c 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 61 ll consists of a
17b87 20 34 0a 2a 2a 20 62 79 74 65 20 70 61 67 65 20 4.** byte page
17b88 6e 75 6d 62 65 72 20 66 6f 6c 6c 6f 77 65 64 20 number followed
17b89 62 79 20 61 20 76 61 72 69 61 62 6c 65 20 6c 65 by a variable le
17b8a 6e 67 74 68 20 69 6e 74 65 67 65 72 2e 20 49 6e ngth integer. In
17b8b 20 6f 74 68 65 72 0a 2a 2a 20 77 6f 72 64 73 2c other.** words,
17b8c 20 61 74 20 6d 6f 73 74 20 31 33 20 62 79 74 65 at most 13 byte
17b8d 73 2e 20 48 65 6e 63 65 20 74 68 65 20 70 53 70 s. Hence the pSp
17b8e 61 63 65 20 62 75 66 66 65 72 20 6d 75 73 74 20 ace buffer must
17b8f 62 65 20 61 74 0a 2a 2a 20 6c 65 61 73 74 20 31 be at.** least 1
17b90 33 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2e 3 bytes in size.
17b91 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 .*/.static int b
17b92 61 6c 61 6e 63 65 5f 71 75 69 63 6b 28 4d 65 6d alance_quick(Mem
17b93 50 61 67 65 20 2a 70 50 61 72 65 6e 74 2c 20 4d Page *pParent, M
17b94 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 75 emPage *pPage, u
17b95 38 20 2a 70 53 70 61 63 65 29 7b 0a 20 20 42 74 8 *pSpace){. Bt
17b96 53 68 61 72 65 64 20 2a 63 6f 6e 73 74 20 70 42 Shared *const pB
17b97 74 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b 20 t = pPage->pBt;
17b98 20 20 20 2f 2a 20 42 2d 54 72 65 65 20 44 61 74 /* B-Tree Dat
17b99 61 62 61 73 65 20 2a 2f 0a 20 20 4d 65 6d 50 61 abase */. MemPa
17b9a 67 65 20 2a 70 4e 65 77 3b 20 20 20 20 20 20 20 ge *pNew;
17b9b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17b9c 2f 2a 20 4e 65 77 6c 79 20 61 6c 6c 6f 63 61 74 /* Newly allocat
17b9d 65 64 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 ed page */. int
17b9e 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 rc;
17b9f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17ba0 20 20 2f 2a 20 52 65 74 75 72 6e 20 43 6f 64 65 /* Return Code
17ba1 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 4e */. Pgno pgnoN
17ba2 65 77 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ew;
17ba3 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 /* Pa
17ba4 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 4e 65 ge number of pNe
17ba5 77 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 w */.. assert(
17ba6 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
17ba7 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d ld(pPage->pBt->m
17ba8 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 utex) );. asser
17ba9 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 t( sqlite3PagerI
17baa 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 72 65 swriteable(pPare
17bab 6e 74 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a nt->pDbPage) );.
17bac 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d assert( pPage-
17bad 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 31 20 29 3b >nOverflow==1 );
17bae 0a 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e 6e .. if( pPage->n
17baf 43 65 6c 6c 3c 3d 30 20 29 20 72 65 74 75 72 6e Cell<=0 ) return
17bb0 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
17bb1 42 4b 50 54 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f BKPT;.. /* Allo
17bb2 63 61 74 65 20 61 20 6e 65 77 20 70 61 67 65 2e cate a new page.
17bb3 20 54 68 69 73 20 70 61 67 65 20 77 69 6c 6c 20 This page will
17bb4 62 65 63 6f 6d 65 20 74 68 65 20 72 69 67 68 74 become the right
17bb5 2d 73 69 62 6c 69 6e 67 20 6f 66 20 0a 20 20 2a -sibling of . *
17bb6 2a 20 70 50 61 67 65 2e 20 4d 61 6b 65 20 74 68 * pPage. Make th
17bb7 65 20 70 61 72 65 6e 74 20 70 61 67 65 20 77 72 e parent page wr
17bb8 69 74 61 62 6c 65 2c 20 73 6f 20 74 68 61 74 20 itable, so that
17bb9 74 68 65 20 6e 65 77 20 64 69 76 69 64 65 72 20 the new divider
17bba 63 65 6c 6c 0a 20 20 2a 2a 20 6d 61 79 20 62 65 cell. ** may be
17bbb 20 69 6e 73 65 72 74 65 64 2e 20 49 66 20 62 6f inserted. If bo
17bbc 74 68 20 74 68 65 73 65 20 6f 70 65 72 61 74 69 th these operati
17bbd 6f 6e 73 20 61 72 65 20 73 75 63 63 65 73 73 66 ons are successf
17bbe 75 6c 2c 20 70 72 6f 63 65 65 64 2e 0a 20 20 2a ul, proceed.. *
17bbf 2f 0a 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74 /. rc = allocat
17bc0 65 42 74 72 65 65 50 61 67 65 28 70 42 74 2c 20 eBtreePage(pBt,
17bc1 26 70 4e 65 77 2c 20 26 70 67 6e 6f 4e 65 77 2c &pNew, &pgnoNew,
17bc2 20 30 2c 20 30 29 3b 0a 0a 20 20 69 66 28 20 72 0, 0);.. if( r
17bc3 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
17bc4 0a 20 20 20 20 75 38 20 2a 70 4f 75 74 20 3d 20 . u8 *pOut =
17bc5 26 70 53 70 61 63 65 5b 34 5d 3b 0a 20 20 20 20 &pSpace[4];.
17bc6 75 38 20 2a 70 43 65 6c 6c 20 3d 20 70 50 61 67 u8 *pCell = pPag
17bc7 65 2d 3e 61 4f 76 66 6c 5b 30 5d 2e 70 43 65 6c e->aOvfl[0].pCel
17bc8 6c 3b 0a 20 20 20 20 75 31 36 20 73 7a 43 65 6c l;. u16 szCel
17bc9 6c 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 l = cellSizePtr(
17bca 70 50 61 67 65 2c 20 70 43 65 6c 6c 29 3b 0a 20 pPage, pCell);.
17bcb 20 20 20 75 38 20 2a 70 53 74 6f 70 3b 0a 0a 20 u8 *pStop;..
17bcc 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 assert( sqlit
17bcd 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 e3PagerIswriteab
17bce 6c 65 28 70 4e 65 77 2d 3e 70 44 62 50 61 67 65 le(pNew->pDbPage
17bcf 29 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 ) );. assert(
17bd0 20 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 30 5d pPage->aData[0]
17bd1 3d 3d 28 50 54 46 5f 49 4e 54 4b 45 59 7c 50 54 ==(PTF_INTKEY|PT
17bd2 46 5f 4c 45 41 46 44 41 54 41 7c 50 54 46 5f 4c F_LEAFDATA|PTF_L
17bd3 45 41 46 29 20 29 3b 0a 20 20 20 20 7a 65 72 6f EAF) );. zero
17bd4 50 61 67 65 28 70 4e 65 77 2c 20 50 54 46 5f 49 Page(pNew, PTF_I
17bd5 4e 54 4b 45 59 7c 50 54 46 5f 4c 45 41 46 44 41 NTKEY|PTF_LEAFDA
17bd6 54 41 7c 50 54 46 5f 4c 45 41 46 29 3b 0a 20 20 TA|PTF_LEAF);.
17bd7 20 20 61 73 73 65 6d 62 6c 65 50 61 67 65 28 70 assemblePage(p
17bd8 4e 65 77 2c 20 31 2c 20 26 70 43 65 6c 6c 2c 20 New, 1, &pCell,
17bd9 26 73 7a 43 65 6c 6c 29 3b 0a 0a 20 20 20 20 2f &szCell);.. /
17bda 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 * If this is an
17bdb 61 75 74 6f 2d 76 61 63 75 75 6d 20 64 61 74 61 auto-vacuum data
17bdc 62 61 73 65 2c 20 75 70 64 61 74 65 20 74 68 65 base, update the
17bdd 20 70 6f 69 6e 74 65 72 20 6d 61 70 0a 20 20 20 pointer map.
17bde 20 2a 2a 20 77 69 74 68 20 65 6e 74 72 69 65 73 ** with entries
17bdf 20 66 6f 72 20 74 68 65 20 6e 65 77 20 70 61 67 for the new pag
17be0 65 2c 20 61 6e 64 20 61 6e 79 20 70 6f 69 6e 74 e, and any point
17be1 65 72 20 66 72 6f 6d 20 74 68 65 20 0a 20 20 20 er from the .
17be2 20 2a 2a 20 63 65 6c 6c 20 6f 6e 20 74 68 65 20 ** cell on the
17be3 70 61 67 65 20 74 6f 20 61 6e 20 6f 76 65 72 66 page to an overf
17be4 6c 6f 77 20 70 61 67 65 2e 20 49 66 20 65 69 74 low page. If eit
17be5 68 65 72 20 6f 66 20 74 68 65 73 65 0a 20 20 20 her of these.
17be6 20 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 73 20 66 ** operations f
17be7 61 69 6c 73 2c 20 74 68 65 20 72 65 74 75 72 6e ails, the return
17be8 20 63 6f 64 65 20 69 73 20 73 65 74 2c 20 62 75 code is set, bu
17be9 74 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 0a 20 t the contents.
17bea 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 70 61 72 ** of the par
17beb 65 6e 74 20 70 61 67 65 20 61 72 65 20 73 74 69 ent page are sti
17bec 6c 6c 20 6d 61 6e 69 70 75 6c 61 74 65 64 20 62 ll manipulated b
17bed 79 20 74 68 68 20 63 6f 64 65 20 62 65 6c 6f 77 y thh code below
17bee 2e 0a 20 20 20 20 2a 2a 20 54 68 61 74 20 69 73 .. ** That is
17bef 20 4f 6b 2c 20 61 74 20 74 68 69 73 20 70 6f 69 Ok, at this poi
17bf0 6e 74 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 nt the parent pa
17bf1 67 65 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 ge is guaranteed
17bf2 20 74 6f 0a 20 20 20 20 2a 2a 20 62 65 20 6d 61 to. ** be ma
17bf3 72 6b 65 64 20 61 73 20 64 69 72 74 79 2e 20 52 rked as dirty. R
17bf4 65 74 75 72 6e 69 6e 67 20 61 6e 20 65 72 72 6f eturning an erro
17bf5 72 20 63 6f 64 65 20 77 69 6c 6c 20 63 61 75 73 r code will caus
17bf6 65 20 61 0a 20 20 20 20 2a 2a 20 72 6f 6c 6c 62 e a. ** rollb
17bf7 61 63 6b 2c 20 75 6e 64 6f 69 6e 67 20 61 6e 79 ack, undoing any
17bf8 20 63 68 61 6e 67 65 73 20 6d 61 64 65 20 74 6f changes made to
17bf9 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 the parent page
17bfa 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 .. */. if(
17bfb 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20 29 7b ISAUTOVACUUM ){
17bfc 0a 20 20 20 20 20 20 70 74 72 6d 61 70 50 75 74 . ptrmapPut
17bfd 28 70 42 74 2c 20 70 67 6e 6f 4e 65 77 2c 20 50 (pBt, pgnoNew, P
17bfe 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 70 50 61 TRMAP_BTREE, pPa
17bff 72 65 6e 74 2d 3e 70 67 6e 6f 2c 20 26 72 63 29 rent->pgno, &rc)
17c00 3b 0a 20 20 20 20 20 20 69 66 28 20 73 7a 43 65 ;. if( szCe
17c01 6c 6c 3e 70 4e 65 77 2d 3e 6d 69 6e 4c 6f 63 61 ll>pNew->minLoca
17c02 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 70 74 72 l ){. ptr
17c03 6d 61 70 50 75 74 4f 76 66 6c 50 74 72 28 70 4e mapPutOvflPtr(pN
17c04 65 77 2c 20 70 43 65 6c 6c 2c 20 26 72 63 29 3b ew, pCell, &rc);
17c05 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
17c06 20 0a 20 20 20 20 2f 2a 20 43 72 65 61 74 65 20 . /* Create
17c07 61 20 64 69 76 69 64 65 72 20 63 65 6c 6c 20 74 a divider cell t
17c08 6f 20 69 6e 73 65 72 74 20 69 6e 74 6f 20 70 50 o insert into pP
17c09 61 72 65 6e 74 2e 20 54 68 65 20 64 69 76 69 64 arent. The divid
17c0a 65 72 20 63 65 6c 6c 0a 20 20 20 20 2a 2a 20 63 er cell. ** c
17c0b 6f 6e 73 69 73 74 73 20 6f 66 20 61 20 34 2d 62 onsists of a 4-b
17c0c 79 74 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 yte page number
17c0d 28 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 (the page number
17c0e 20 6f 66 20 70 50 61 67 65 29 20 61 6e 64 0a 20 of pPage) and.
17c0f 20 20 20 2a 2a 20 61 20 76 61 72 69 61 62 6c 65 ** a variable
17c10 20 6c 65 6e 67 74 68 20 6b 65 79 20 76 61 6c 75 length key valu
17c11 65 20 28 77 68 69 63 68 20 6d 75 73 74 20 62 65 e (which must be
17c12 20 74 68 65 20 73 61 6d 65 20 76 61 6c 75 65 20 the same value
17c13 61 73 20 74 68 65 0a 20 20 20 20 2a 2a 20 6c 61 as the. ** la
17c14 72 67 65 73 74 20 6b 65 79 20 6f 6e 20 70 50 61 rgest key on pPa
17c15 67 65 29 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 ge).. **.
17c16 2a 2a 20 54 6f 20 66 69 6e 64 20 74 68 65 20 6c ** To find the l
17c17 61 72 67 65 73 74 20 6b 65 79 20 76 61 6c 75 65 argest key value
17c18 20 6f 6e 20 70 50 61 67 65 2c 20 66 69 72 73 74 on pPage, first
17c19 20 66 69 6e 64 20 74 68 65 20 72 69 67 68 74 2d find the right-
17c1a 6d 6f 73 74 20 0a 20 20 20 20 2a 2a 20 63 65 6c most . ** cel
17c1b 6c 20 6f 6e 20 70 50 61 67 65 2e 20 54 68 65 20 l on pPage. The
17c1c 66 69 72 73 74 20 74 77 6f 20 66 69 65 6c 64 73 first two fields
17c1d 20 6f 66 20 74 68 69 73 20 63 65 6c 6c 20 61 72 of this cell ar
17c1e 65 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 72 65 e the . ** re
17c1f 63 6f 72 64 2d 6c 65 6e 67 74 68 20 28 61 20 76 cord-length (a v
17c20 61 72 69 61 62 6c 65 20 6c 65 6e 67 74 68 20 69 ariable length i
17c21 6e 74 65 67 65 72 20 61 74 20 6d 6f 73 74 20 33 nteger at most 3
17c22 32 2d 62 69 74 73 20 69 6e 20 73 69 7a 65 29 0a 2-bits in size).
17c23 20 20 20 20 2a 2a 20 61 6e 64 20 74 68 65 20 6b ** and the k
17c24 65 79 20 76 61 6c 75 65 20 28 61 20 76 61 72 69 ey value (a vari
17c25 61 62 6c 65 20 6c 65 6e 67 74 68 20 69 6e 74 65 able length inte
17c26 67 65 72 2c 20 6d 61 79 20 68 61 76 65 20 61 6e ger, may have an
17c27 79 20 76 61 6c 75 65 29 2e 0a 20 20 20 20 2a 2a y value).. **
17c28 20 54 68 65 20 66 69 72 73 74 20 6f 66 20 74 68 The first of th
17c29 65 20 77 68 69 6c 65 28 2e 2e 2e 29 20 6c 6f 6f e while(...) loo
17c2a 70 73 20 62 65 6c 6f 77 20 73 6b 69 70 73 20 6f ps below skips o
17c2b 76 65 72 20 74 68 65 20 72 65 63 6f 72 64 2d 6c ver the record-l
17c2c 65 6e 67 74 68 0a 20 20 20 20 2a 2a 20 66 69 65 ength. ** fie
17c2d 6c 64 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 77 ld. The second w
17c2e 68 69 6c 65 28 2e 2e 2e 29 20 6c 6f 6f 70 20 63 hile(...) loop c
17c2f 6f 70 69 65 73 20 74 68 65 20 6b 65 79 20 76 61 opies the key va
17c30 6c 75 65 20 66 72 6f 6d 20 74 68 65 0a 20 20 20 lue from the.
17c31 20 2a 2a 20 63 65 6c 6c 20 6f 6e 20 70 50 61 67 ** cell on pPag
17c32 65 20 69 6e 74 6f 20 74 68 65 20 70 53 70 61 63 e into the pSpac
17c33 65 20 62 75 66 66 65 72 2e 0a 20 20 20 20 2a 2f e buffer.. */
17c34 0a 20 20 20 20 70 43 65 6c 6c 20 3d 20 66 69 6e . pCell = fin
17c35 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 70 50 61 dCell(pPage, pPa
17c36 67 65 2d 3e 6e 43 65 6c 6c 2d 31 29 3b 0a 20 20 ge->nCell-1);.
17c37 20 20 70 53 74 6f 70 20 3d 20 26 70 43 65 6c 6c pStop = &pCell
17c38 5b 39 5d 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 [9];. while(
17c39 28 2a 28 70 43 65 6c 6c 2b 2b 29 26 30 78 38 30 (*(pCell++)&0x80
17c3a 29 20 26 26 20 70 43 65 6c 6c 3c 70 53 74 6f 70 ) && pCell<pStop
17c3b 20 29 3b 0a 20 20 20 20 70 53 74 6f 70 20 3d 20 );. pStop =
17c3c 26 70 43 65 6c 6c 5b 39 5d 3b 0a 20 20 20 20 77 &pCell[9];. w
17c3d 68 69 6c 65 28 20 28 28 2a 28 70 4f 75 74 2b 2b hile( ((*(pOut++
17c3e 29 20 3d 20 2a 28 70 43 65 6c 6c 2b 2b 29 29 26 ) = *(pCell++))&
17c3f 30 78 38 30 29 20 26 26 20 70 43 65 6c 6c 3c 70 0x80) && pCell<p
17c40 53 74 6f 70 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 Stop );.. /*
17c41 49 6e 73 65 72 74 20 74 68 65 20 6e 65 77 20 64 Insert the new d
17c42 69 76 69 64 65 72 20 63 65 6c 6c 20 69 6e 74 6f ivider cell into
17c43 20 70 50 61 72 65 6e 74 2e 20 2a 2f 0a 20 20 20 pParent. */.
17c44 20 69 6e 73 65 72 74 43 65 6c 6c 28 70 50 61 72 insertCell(pPar
17c45 65 6e 74 2c 20 70 50 61 72 65 6e 74 2d 3e 6e 43 ent, pParent->nC
17c46 65 6c 6c 2c 20 70 53 70 61 63 65 2c 20 28 69 6e ell, pSpace, (in
17c47 74 29 28 70 4f 75 74 2d 70 53 70 61 63 65 29 2c t)(pOut-pSpace),
17c48 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
17c49 30 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 2c 20 0, pPage->pgno,
17c4a 26 72 63 29 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 &rc);.. /* Se
17c4b 74 20 74 68 65 20 72 69 67 68 74 2d 63 68 69 6c t the right-chil
17c4c 64 20 70 6f 69 6e 74 65 72 20 6f 66 20 70 50 61 d pointer of pPa
17c4d 72 65 6e 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f rent to point to
17c4e 20 74 68 65 20 6e 65 77 20 70 61 67 65 2e 20 2a the new page. *
17c4f 2f 0a 20 20 20 20 70 75 74 34 62 79 74 65 28 26 /. put4byte(&
17c50 70 50 61 72 65 6e 74 2d 3e 61 44 61 74 61 5b 70 pParent->aData[p
17c51 50 61 72 65 6e 74 2d 3e 68 64 72 4f 66 66 73 65 Parent->hdrOffse
17c52 74 2b 38 5d 2c 20 70 67 6e 6f 4e 65 77 29 3b 0a t+8], pgnoNew);.
17c53 20 20 0a 20 20 20 20 2f 2a 20 52 65 6c 65 61 73 . /* Releas
17c54 65 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 e the reference
17c55 74 6f 20 74 68 65 20 6e 65 77 20 70 61 67 65 2e to the new page.
17c56 20 2a 2f 0a 20 20 20 20 72 65 6c 65 61 73 65 50 */. releaseP
17c57 61 67 65 28 70 4e 65 77 29 3b 0a 20 20 7d 0a 0a age(pNew);. }..
17c58 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 return rc;.}.#
17c59 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
17c5a 4f 4d 49 54 5f 51 55 49 43 4b 42 41 4c 41 4e 43 OMIT_QUICKBALANC
17c5b 45 20 2a 2f 0a 0a 23 69 66 20 30 0a 2f 2a 0a 2a E */..#if 0./*.*
17c5c 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
17c5d 64 6f 65 73 20 6e 6f 74 20 63 6f 6e 74 72 69 62 does not contrib
17c5e 75 74 65 20 61 6e 79 74 68 69 6e 67 20 74 6f 20 ute anything to
17c5f 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 6f 66 the operation of
17c60 20 53 51 4c 69 74 65 2e 0a 2a 2a 20 69 74 20 69 SQLite..** it i
17c61 73 20 73 6f 6d 65 74 69 6d 65 73 20 61 63 74 69 s sometimes acti
17c62 76 61 74 65 64 20 74 65 6d 70 6f 72 61 72 69 6c vated temporaril
17c63 79 20 77 68 69 6c 65 20 64 65 62 75 67 67 69 6e y while debuggin
17c64 67 20 63 6f 64 65 20 72 65 73 70 6f 6e 73 69 62 g code responsib
17c65 6c 65 20 0a 2a 2a 20 66 6f 72 20 73 65 74 74 69 le .** for setti
17c66 6e 67 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 ng pointer-map e
17c67 6e 74 72 69 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 ntries..*/.stati
17c68 63 20 69 6e 74 20 70 74 72 6d 61 70 43 68 65 63 c int ptrmapChec
17c69 6b 50 61 67 65 73 28 4d 65 6d 50 61 67 65 20 2a kPages(MemPage *
17c6a 2a 61 70 50 61 67 65 2c 20 69 6e 74 20 6e 50 61 *apPage, int nPa
17c6b 67 65 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b ge){. int i, j;
17c6c 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 50 . for(i=0; i<nP
17c6d 61 67 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 50 age; i++){. P
17c6e 67 6e 6f 20 6e 3b 0a 20 20 20 20 75 38 20 65 3b gno n;. u8 e;
17c6f 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 . MemPage *pP
17c70 61 67 65 20 3d 20 61 70 50 61 67 65 5b 69 5d 3b age = apPage[i];
17c71 0a 20 20 20 20 42 74 53 68 61 72 65 64 20 2a 70 . BtShared *p
17c72 42 74 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b Bt = pPage->pBt;
17c73 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 . assert( pPa
17c74 67 65 2d 3e 69 73 49 6e 69 74 20 29 3b 0a 0a 20 ge->isInit );..
17c75 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 50 for(j=0; j<pP
17c76 61 67 65 2d 3e 6e 43 65 6c 6c 3b 20 6a 2b 2b 29 age->nCell; j++)
17c77 7b 0a 20 20 20 20 20 20 43 65 6c 6c 49 6e 66 6f {. CellInfo
17c78 20 69 6e 66 6f 3b 0a 20 20 20 20 20 20 75 38 20 info;. u8
17c79 2a 7a 3b 0a 20 20 20 20 20 0a 20 20 20 20 20 20 *z;. .
17c7a 7a 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 z = findCell(pPa
17c7b 67 65 2c 20 6a 29 3b 0a 20 20 20 20 20 20 62 74 ge, j);. bt
17c7c 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 reeParseCellPtr(
17c7d 70 50 61 67 65 2c 20 7a 2c 20 26 69 6e 66 6f 29 pPage, z, &info)
17c7e 3b 0a 20 20 20 20 20 20 69 66 28 20 69 6e 66 6f ;. if( info
17c7f 2e 69 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 .iOverflow ){.
17c80 20 20 20 20 20 20 50 67 6e 6f 20 6f 76 66 6c 20 Pgno ovfl
17c81 3d 20 67 65 74 34 62 79 74 65 28 26 7a 5b 69 6e = get4byte(&z[in
17c82 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d 29 3b 0a fo.iOverflow]);.
17c83 20 20 20 20 20 20 20 20 70 74 72 6d 61 70 47 65 ptrmapGe
17c84 74 28 70 42 74 2c 20 6f 76 66 6c 2c 20 26 65 2c t(pBt, ovfl, &e,
17c85 20 26 6e 29 3b 0a 20 20 20 20 20 20 20 20 61 73 &n);. as
17c86 73 65 72 74 28 20 6e 3d 3d 70 50 61 67 65 2d 3e sert( n==pPage->
17c87 70 67 6e 6f 20 26 26 20 65 3d 3d 50 54 52 4d 41 pgno && e==PTRMA
17c88 50 5f 4f 56 45 52 46 4c 4f 57 31 20 29 3b 0a 20 P_OVERFLOW1 );.
17c89 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 }. if(
17c8a 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b !pPage->leaf ){
17c8b 0a 20 20 20 20 20 20 20 20 50 67 6e 6f 20 63 68 . Pgno ch
17c8c 69 6c 64 20 3d 20 67 65 74 34 62 79 74 65 28 7a ild = get4byte(z
17c8d 29 3b 0a 20 20 20 20 20 20 20 20 70 74 72 6d 61 );. ptrma
17c8e 70 47 65 74 28 70 42 74 2c 20 63 68 69 6c 64 2c pGet(pBt, child,
17c8f 20 26 65 2c 20 26 6e 29 3b 0a 20 20 20 20 20 20 &e, &n);.
17c90 20 20 61 73 73 65 72 74 28 20 6e 3d 3d 70 50 61 assert( n==pPa
17c91 67 65 2d 3e 70 67 6e 6f 20 26 26 20 65 3d 3d 50 ge->pgno && e==P
17c92 54 52 4d 41 50 5f 42 54 52 45 45 20 29 3b 0a 20 TRMAP_BTREE );.
17c93 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 }. }.
17c94 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 if( !pPage->lea
17c95 66 20 29 7b 0a 20 20 20 20 20 20 50 67 6e 6f 20 f ){. Pgno
17c96 63 68 69 6c 64 20 3d 20 67 65 74 34 62 79 74 65 child = get4byte
17c97 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 (&pPage->aData[p
17c98 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b Page->hdrOffset+
17c99 38 5d 29 3b 0a 20 20 20 20 20 20 70 74 72 6d 61 8]);. ptrma
17c9a 70 47 65 74 28 70 42 74 2c 20 63 68 69 6c 64 2c pGet(pBt, child,
17c9b 20 26 65 2c 20 26 6e 29 3b 0a 20 20 20 20 20 20 &e, &n);.
17c9c 61 73 73 65 72 74 28 20 6e 3d 3d 70 50 61 67 65 assert( n==pPage
17c9d 2d 3e 70 67 6e 6f 20 26 26 20 65 3d 3d 50 54 52 ->pgno && e==PTR
17c9e 4d 41 50 5f 42 54 52 45 45 20 29 3b 0a 20 20 20 MAP_BTREE );.
17c9f 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
17ca0 31 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 1;.}.#endif../*.
17ca1 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
17ca2 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f 70 79 is used to copy
17ca3 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 the contents of
17ca4 20 74 68 65 20 62 2d 74 72 65 65 20 6e 6f 64 65 the b-tree node
17ca5 20 73 74 6f 72 65 64 20 0a 2a 2a 20 6f 6e 20 70 stored .** on p
17ca6 61 67 65 20 70 46 72 6f 6d 20 74 6f 20 70 61 67 age pFrom to pag
17ca7 65 20 70 54 6f 2e 20 49 66 20 70 61 67 65 20 70 e pTo. If page p
17ca8 46 72 6f 6d 20 77 61 73 20 6e 6f 74 20 61 20 6c From was not a l
17ca9 65 61 66 20 70 61 67 65 2c 20 74 68 65 6e 0a 2a eaf page, then.*
17caa 2a 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 * the pointer-ma
17cab 70 20 65 6e 74 72 69 65 73 20 66 6f 72 20 65 61 p entries for ea
17cac 63 68 20 63 68 69 6c 64 20 70 61 67 65 20 61 72 ch child page ar
17cad 65 20 75 70 64 61 74 65 64 20 73 6f 20 74 68 61 e updated so tha
17cae 74 20 74 68 65 0a 2a 2a 20 70 61 72 65 6e 74 20 t the.** parent
17caf 70 61 67 65 20 73 74 6f 72 65 64 20 69 6e 20 74 page stored in t
17cb0 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 69 he pointer map i
17cb1 73 20 70 61 67 65 20 70 54 6f 2e 20 49 66 20 70 s page pTo. If p
17cb2 46 72 6f 6d 20 63 6f 6e 74 61 69 6e 65 64 0a 2a From contained.*
17cb3 2a 20 61 6e 79 20 63 65 6c 6c 73 20 77 69 74 68 * any cells with
17cb4 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 70 overflow page p
17cb5 6f 69 6e 74 65 72 73 2c 20 74 68 65 6e 20 74 68 ointers, then th
17cb6 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 e corresponding
17cb7 70 6f 69 6e 74 65 72 0a 2a 2a 20 6d 61 70 20 65 pointer.** map e
17cb8 6e 74 72 69 65 73 20 61 72 65 20 61 6c 73 6f 20 ntries are also
17cb9 75 70 64 61 74 65 64 20 73 6f 20 74 68 61 74 20 updated so that
17cba 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 20 the parent page
17cbb 69 73 20 70 61 67 65 20 70 54 6f 2e 0a 2a 2a 0a is page pTo..**.
17cbc 2a 2a 20 49 66 20 70 46 72 6f 6d 20 69 73 20 63 ** If pFrom is c
17cbd 75 72 72 65 6e 74 6c 79 20 63 61 72 72 79 69 6e urrently carryin
17cbe 67 20 61 6e 79 20 6f 76 65 72 66 6c 6f 77 20 63 g any overflow c
17cbf 65 6c 6c 73 20 28 65 6e 74 72 69 65 73 20 69 6e ells (entries in
17cc0 20 74 68 65 0a 2a 2a 20 4d 65 6d 50 61 67 65 2e the.** MemPage.
17cc1 61 4f 76 66 6c 5b 5d 20 61 72 72 61 79 29 2c 20 aOvfl[] array),
17cc2 74 68 65 79 20 61 72 65 20 6e 6f 74 20 63 6f 70 they are not cop
17cc3 69 65 64 20 74 6f 20 70 54 6f 2e 20 0a 2a 2a 0a ied to pTo. .**.
17cc4 2a 2a 20 42 65 66 6f 72 65 20 72 65 74 75 72 6e ** Before return
17cc5 69 6e 67 2c 20 70 61 67 65 20 70 54 6f 20 69 73 ing, page pTo is
17cc6 20 72 65 69 6e 69 74 69 61 6c 69 7a 65 64 20 75 reinitialized u
17cc7 73 69 6e 67 20 62 74 72 65 65 49 6e 69 74 50 61 sing btreeInitPa
17cc8 67 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ge()..**.** The
17cc9 70 65 72 66 6f 72 6d 61 6e 63 65 20 6f 66 20 74 performance of t
17cca 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
17ccb 6e 6f 74 20 63 72 69 74 69 63 61 6c 2e 20 49 74 not critical. It
17ccc 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 62 79 is only used by
17ccd 20 0a 2a 2a 20 74 68 65 20 62 61 6c 61 6e 63 65 .** the balance
17cce 5f 73 68 61 6c 6c 6f 77 65 72 28 29 20 61 6e 64 _shallower() and
17ccf 20 62 61 6c 61 6e 63 65 5f 64 65 65 70 65 72 28 balance_deeper(
17cd0 29 20 70 72 6f 63 65 64 75 72 65 73 2c 20 6e 65 ) procedures, ne
17cd1 69 74 68 65 72 20 6f 66 0a 2a 2a 20 77 68 69 63 ither of.** whic
17cd2 68 20 61 72 65 20 63 61 6c 6c 65 64 20 6f 66 74 h are called oft
17cd3 65 6e 20 75 6e 64 65 72 20 6e 6f 72 6d 61 6c 20 en under normal
17cd4 63 69 72 63 75 6d 73 74 61 6e 63 65 73 2e 0a 2a circumstances..*
17cd5 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f /.static void co
17cd6 70 79 4e 6f 64 65 43 6f 6e 74 65 6e 74 28 4d 65 pyNodeContent(Me
17cd7 6d 50 61 67 65 20 2a 70 46 72 6f 6d 2c 20 4d 65 mPage *pFrom, Me
17cd8 6d 50 61 67 65 20 2a 70 54 6f 2c 20 69 6e 74 20 mPage *pTo, int
17cd9 2a 70 52 43 29 7b 0a 20 20 69 66 28 20 28 2a 70 *pRC){. if( (*p
17cda 52 43 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 RC)==SQLITE_OK )
17cdb 7b 0a 20 20 20 20 42 74 53 68 61 72 65 64 20 2a {. BtShared *
17cdc 20 63 6f 6e 73 74 20 70 42 74 20 3d 20 70 46 72 const pBt = pFr
17cdd 6f 6d 2d 3e 70 42 74 3b 0a 20 20 20 20 75 38 20 om->pBt;. u8
17cde 2a 20 63 6f 6e 73 74 20 61 46 72 6f 6d 20 3d 20 * const aFrom =
17cdf 70 46 72 6f 6d 2d 3e 61 44 61 74 61 3b 0a 20 20 pFrom->aData;.
17ce0 20 20 75 38 20 2a 20 63 6f 6e 73 74 20 61 54 6f u8 * const aTo
17ce1 20 3d 20 70 54 6f 2d 3e 61 44 61 74 61 3b 0a 20 = pTo->aData;.
17ce2 20 20 20 69 6e 74 20 63 6f 6e 73 74 20 69 46 72 int const iFr
17ce3 6f 6d 48 64 72 20 3d 20 70 46 72 6f 6d 2d 3e 68 omHdr = pFrom->h
17ce4 64 72 4f 66 66 73 65 74 3b 0a 20 20 20 20 69 6e drOffset;. in
17ce5 74 20 63 6f 6e 73 74 20 69 54 6f 48 64 72 20 3d t const iToHdr =
17ce6 20 28 28 70 54 6f 2d 3e 70 67 6e 6f 3d 3d 31 29 ((pTo->pgno==1)
17ce7 20 3f 20 31 30 30 20 3a 20 30 29 3b 0a 20 20 20 ? 100 : 0);.
17ce8 20 54 45 53 54 4f 4e 4c 59 28 69 6e 74 20 72 63 TESTONLY(int rc
17ce9 3b 29 0a 20 20 20 20 69 6e 74 20 69 44 61 74 61 ;). int iData
17cea 3b 0a 20 20 0a 20 20 0a 20 20 20 20 61 73 73 65 ;. . . asse
17ceb 72 74 28 20 70 46 72 6f 6d 2d 3e 69 73 49 6e 69 rt( pFrom->isIni
17cec 74 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 t );. assert(
17ced 20 70 46 72 6f 6d 2d 3e 6e 46 72 65 65 3e 3d 69 pFrom->nFree>=i
17cee 54 6f 48 64 72 20 29 3b 0a 20 20 20 20 61 73 73 ToHdr );. ass
17cef 65 72 74 28 20 67 65 74 32 62 79 74 65 28 26 61 ert( get2byte(&a
17cf0 46 72 6f 6d 5b 69 46 72 6f 6d 48 64 72 2b 35 5d From[iFromHdr+5]
17cf1 29 3c 3d 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 )<=pBt->usableSi
17cf2 7a 65 20 29 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 ze );. . /*
17cf3 43 6f 70 79 20 74 68 65 20 62 2d 74 72 65 65 20 Copy the b-tree
17cf4 6e 6f 64 65 20 63 6f 6e 74 65 6e 74 20 66 72 6f node content fro
17cf5 6d 20 70 61 67 65 20 70 46 72 6f 6d 20 74 6f 20 m page pFrom to
17cf6 70 61 67 65 20 70 54 6f 2e 20 2a 2f 0a 20 20 20 page pTo. */.
17cf7 20 69 44 61 74 61 20 3d 20 67 65 74 32 62 79 74 iData = get2byt
17cf8 65 28 26 61 46 72 6f 6d 5b 69 46 72 6f 6d 48 64 e(&aFrom[iFromHd
17cf9 72 2b 35 5d 29 3b 0a 20 20 20 20 6d 65 6d 63 70 r+5]);. memcp
17cfa 79 28 26 61 54 6f 5b 69 44 61 74 61 5d 2c 20 26 y(&aTo[iData], &
17cfb 61 46 72 6f 6d 5b 69 44 61 74 61 5d 2c 20 70 42 aFrom[iData], pB
17cfc 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2d 69 44 t->usableSize-iD
17cfd 61 74 61 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 ata);. memcpy
17cfe 28 26 61 54 6f 5b 69 54 6f 48 64 72 5d 2c 20 26 (&aTo[iToHdr], &
17cff 61 46 72 6f 6d 5b 69 46 72 6f 6d 48 64 72 5d 2c aFrom[iFromHdr],
17d00 20 70 46 72 6f 6d 2d 3e 63 65 6c 6c 4f 66 66 73 pFrom->cellOffs
17d01 65 74 20 2b 20 32 2a 70 46 72 6f 6d 2d 3e 6e 43 et + 2*pFrom->nC
17d02 65 6c 6c 29 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 ell);. . /*
17d03 52 65 69 6e 69 74 69 61 6c 69 7a 65 20 70 61 67 Reinitialize pag
17d04 65 20 70 54 6f 20 73 6f 20 74 68 61 74 20 74 68 e pTo so that th
17d05 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 e contents of th
17d06 65 20 4d 65 6d 50 61 67 65 20 73 74 72 75 63 74 e MemPage struct
17d07 75 72 65 0a 20 20 20 20 2a 2a 20 6d 61 74 63 68 ure. ** match
17d08 20 74 68 65 20 6e 65 77 20 64 61 74 61 2e 20 54 the new data. T
17d09 68 65 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f he initializatio
17d0a 6e 20 6f 66 20 70 54 6f 20 22 63 61 6e 6e 6f 74 n of pTo "cannot
17d0b 22 20 66 61 69 6c 2c 20 61 73 20 74 68 65 0a 20 " fail, as the.
17d0c 20 20 20 2a 2a 20 64 61 74 61 20 63 6f 70 69 65 ** data copie
17d0d 64 20 66 72 6f 6d 20 70 46 72 6f 6d 20 69 73 20 d from pFrom is
17d0e 6b 6e 6f 77 6e 20 74 6f 20 62 65 20 76 61 6c 69 known to be vali
17d0f 64 2e 20 20 2a 2f 0a 20 20 20 20 70 54 6f 2d 3e d. */. pTo->
17d10 69 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20 20 20 isInit = 0;.
17d11 54 45 53 54 4f 4e 4c 59 28 72 63 20 3d 20 29 20 TESTONLY(rc = )
17d12 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 70 54 btreeInitPage(pT
17d13 6f 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 o);. assert(
17d14 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b rc==SQLITE_OK );
17d15 0a 20 20 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 . . /* If th
17d16 69 73 20 69 73 20 61 6e 20 61 75 74 6f 2d 76 61 is is an auto-va
17d17 63 75 75 6d 20 64 61 74 61 62 61 73 65 2c 20 75 cuum database, u
17d18 70 64 61 74 65 20 74 68 65 20 70 6f 69 6e 74 65 pdate the pointe
17d19 72 2d 6d 61 70 20 65 6e 74 72 69 65 73 0a 20 20 r-map entries.
17d1a 20 20 2a 2a 20 66 6f 72 20 61 6e 79 20 62 2d 74 ** for any b-t
17d1b 72 65 65 20 6f 72 20 6f 76 65 72 66 6c 6f 77 20 ree or overflow
17d1c 70 61 67 65 73 20 74 68 61 74 20 70 54 6f 20 6e pages that pTo n
17d1d 6f 77 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 ow contains the
17d1e 70 6f 69 6e 74 65 72 73 20 74 6f 2e 0a 20 20 20 pointers to..
17d1f 20 2a 2f 0a 20 20 20 20 69 66 28 20 49 53 41 55 */. if( ISAU
17d20 54 4f 56 41 43 55 55 4d 20 29 7b 0a 20 20 20 20 TOVACUUM ){.
17d21 20 20 2a 70 52 43 20 3d 20 73 65 74 43 68 69 6c *pRC = setChil
17d22 64 50 74 72 6d 61 70 73 28 70 54 6f 29 3b 0a 20 dPtrmaps(pTo);.
17d23 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a }. }.}../*.*
17d24 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 * This routine r
17d25 65 64 69 73 74 72 69 62 75 74 65 73 20 63 65 6c edistributes cel
17d26 6c 73 20 6f 6e 20 74 68 65 20 69 50 61 72 65 6e ls on the iParen
17d27 74 49 64 78 27 74 68 20 63 68 69 6c 64 20 6f 66 tIdx'th child of
17d28 20 70 50 61 72 65 6e 74 0a 2a 2a 20 28 68 65 72 pParent.** (her
17d29 65 61 66 74 65 72 20 22 74 68 65 20 70 61 67 65 eafter "the page
17d2a 22 29 20 61 6e 64 20 75 70 20 74 6f 20 32 20 73 ") and up to 2 s
17d2b 69 62 6c 69 6e 67 73 20 73 6f 20 74 68 61 74 20 iblings so that
17d2c 61 6c 6c 20 70 61 67 65 73 20 68 61 76 65 20 61 all pages have a
17d2d 62 6f 75 74 20 74 68 65 0a 2a 2a 20 73 61 6d 65 bout the.** same
17d2e 20 61 6d 6f 75 6e 74 20 6f 66 20 66 72 65 65 20 amount of free
17d2f 73 70 61 63 65 2e 20 55 73 75 61 6c 6c 79 20 61 space. Usually a
17d30 20 73 69 6e 67 6c 65 20 73 69 62 6c 69 6e 67 20 single sibling
17d31 6f 6e 20 65 69 74 68 65 72 20 73 69 64 65 20 6f on either side o
17d32 66 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 61 72 f the.** page ar
17d33 65 20 75 73 65 64 20 69 6e 20 74 68 65 20 62 61 e used in the ba
17d34 6c 61 6e 63 69 6e 67 2c 20 74 68 6f 75 67 68 20 lancing, though
17d35 62 6f 74 68 20 73 69 62 6c 69 6e 67 73 20 6d 69 both siblings mi
17d36 67 68 74 20 63 6f 6d 65 20 66 72 6f 6d 20 6f 6e ght come from on
17d37 65 0a 2a 2a 20 73 69 64 65 20 69 66 20 74 68 65 e.** side if the
17d38 20 70 61 67 65 20 69 73 20 74 68 65 20 66 69 72 page is the fir
17d39 73 74 20 6f 72 20 6c 61 73 74 20 63 68 69 6c 64 st or last child
17d3a 20 6f 66 20 69 74 73 20 70 61 72 65 6e 74 2e 20 of its parent.
17d3b 49 66 20 74 68 65 20 70 61 67 65 20 0a 2a 2a 20 If the page .**
17d3c 68 61 73 20 66 65 77 65 72 20 74 68 61 6e 20 32 has fewer than 2
17d3d 20 73 69 62 6c 69 6e 67 73 20 28 73 6f 6d 65 74 siblings (somet
17d3e 68 69 6e 67 20 77 68 69 63 68 20 63 61 6e 20 6f hing which can o
17d3f 6e 6c 79 20 68 61 70 70 65 6e 20 69 66 20 74 68 nly happen if th
17d40 65 20 70 61 67 65 0a 2a 2a 20 69 73 20 61 20 72 e page.** is a r
17d41 6f 6f 74 20 70 61 67 65 20 6f 72 20 61 20 63 68 oot page or a ch
17d42 69 6c 64 20 6f 66 20 61 20 72 6f 6f 74 20 70 61 ild of a root pa
17d43 67 65 29 20 74 68 65 6e 20 61 6c 6c 20 61 76 61 ge) then all ava
17d44 69 6c 61 62 6c 65 20 73 69 62 6c 69 6e 67 73 0a ilable siblings.
17d45 2a 2a 20 70 61 72 74 69 63 69 70 61 74 65 20 69 ** participate i
17d46 6e 20 74 68 65 20 62 61 6c 61 6e 63 69 6e 67 2e n the balancing.
17d47 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 75 6d 62 65 .**.** The numbe
17d48 72 20 6f 66 20 73 69 62 6c 69 6e 67 73 20 6f 66 r of siblings of
17d49 20 74 68 65 20 70 61 67 65 20 6d 69 67 68 74 20 the page might
17d4a 62 65 20 69 6e 63 72 65 61 73 65 64 20 6f 72 20 be increased or
17d4b 64 65 63 72 65 61 73 65 64 20 62 79 20 0a 2a 2a decreased by .**
17d4c 20 6f 6e 65 20 6f 72 20 74 77 6f 20 69 6e 20 61 one or two in a
17d4d 6e 20 65 66 66 6f 72 74 20 74 6f 20 6b 65 65 70 n effort to keep
17d4e 20 70 61 67 65 73 20 6e 65 61 72 6c 79 20 66 75 pages nearly fu
17d4f 6c 6c 20 62 75 74 20 6e 6f 74 20 6f 76 65 72 20 ll but not over
17d50 66 75 6c 6c 2e 20 0a 2a 2a 0a 2a 2a 20 4e 6f 74 full. .**.** Not
17d51 65 20 74 68 61 74 20 77 68 65 6e 20 74 68 69 73 e that when this
17d52 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c routine is call
17d53 65 64 2c 20 73 6f 6d 65 20 6f 66 20 74 68 65 20 ed, some of the
17d54 63 65 6c 6c 73 20 6f 6e 20 74 68 65 20 70 61 67 cells on the pag
17d55 65 0a 2a 2a 20 6d 69 67 68 74 20 6e 6f 74 20 61 e.** might not a
17d56 63 74 75 61 6c 6c 79 20 62 65 20 73 74 6f 72 65 ctually be store
17d57 64 20 69 6e 20 4d 65 6d 50 61 67 65 2e 61 44 61 d in MemPage.aDa
17d58 74 61 5b 5d 2e 20 54 68 69 73 20 63 61 6e 20 68 ta[]. This can h
17d59 61 70 70 65 6e 0a 2a 2a 20 69 66 20 74 68 65 20 appen.** if the
17d5a 70 61 67 65 20 69 73 20 6f 76 65 72 66 75 6c 6c page is overfull
17d5b 2e 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 65 . This routine e
17d5c 6e 73 75 72 65 73 20 74 68 61 74 20 61 6c 6c 20 nsures that all
17d5d 63 65 6c 6c 73 20 61 6c 6c 6f 63 61 74 65 64 0a cells allocated.
17d5e 2a 2a 20 74 6f 20 74 68 65 20 70 61 67 65 20 61 ** to the page a
17d5f 6e 64 20 69 74 73 20 73 69 62 6c 69 6e 67 73 20 nd its siblings
17d60 66 69 74 20 69 6e 74 6f 20 4d 65 6d 50 61 67 65 fit into MemPage
17d61 2e 61 44 61 74 61 5b 5d 20 62 65 66 6f 72 65 20 .aData[] before
17d62 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a returning..**.**
17d63 20 49 6e 20 74 68 65 20 63 6f 75 72 73 65 20 6f In the course o
17d64 66 20 62 61 6c 61 6e 63 69 6e 67 20 74 68 65 20 f balancing the
17d65 70 61 67 65 20 61 6e 64 20 69 74 73 20 73 69 62 page and its sib
17d66 6c 69 6e 67 73 2c 20 63 65 6c 6c 73 20 6d 61 79 lings, cells may
17d67 20 62 65 0a 2a 2a 20 69 6e 73 65 72 74 65 64 20 be.** inserted
17d68 69 6e 74 6f 20 6f 72 20 72 65 6d 6f 76 65 64 20 into or removed
17d69 66 72 6f 6d 20 74 68 65 20 70 61 72 65 6e 74 20 from the parent
17d6a 70 61 67 65 20 28 70 50 61 72 65 6e 74 29 2e 20 page (pParent).
17d6b 44 6f 69 6e 67 20 73 6f 0a 2a 2a 20 6d 61 79 20 Doing so.** may
17d6c 63 61 75 73 65 20 74 68 65 20 70 61 72 65 6e 74 cause the parent
17d6d 20 70 61 67 65 20 74 6f 20 62 65 63 6f 6d 65 20 page to become
17d6e 6f 76 65 72 66 75 6c 6c 20 6f 72 20 75 6e 64 65 overfull or unde
17d6f 72 66 75 6c 6c 2e 20 49 66 20 74 68 69 73 0a 2a rfull. If this.*
17d70 2a 20 68 61 70 70 65 6e 73 2c 20 69 74 20 69 73 * happens, it is
17d71 20 74 68 65 20 72 65 73 70 6f 6e 73 69 62 69 6c the responsibil
17d72 69 74 79 20 6f 66 20 74 68 65 20 63 61 6c 6c 65 ity of the calle
17d73 72 20 74 6f 20 69 6e 76 6f 6b 65 20 74 68 65 20 r to invoke the
17d74 63 6f 72 72 65 63 74 0a 2a 2a 20 62 61 6c 61 6e correct.** balan
17d75 63 69 6e 67 20 72 6f 75 74 69 6e 65 20 74 6f 20 cing routine to
17d76 66 69 78 20 74 68 69 73 20 70 72 6f 62 6c 65 6d fix this problem
17d77 20 28 73 65 65 20 74 68 65 20 62 61 6c 61 6e 63 (see the balanc
17d78 65 28 29 20 72 6f 75 74 69 6e 65 29 2e 20 0a 2a e() routine). .*
17d79 2a 0a 2a 2a 20 49 66 20 74 68 69 73 20 72 6f 75 *.** If this rou
17d7a 74 69 6e 65 20 66 61 69 6c 73 20 66 6f 72 20 61 tine fails for a
17d7b 6e 79 20 72 65 61 73 6f 6e 2c 20 69 74 20 6d 69 ny reason, it mi
17d7c 67 68 74 20 6c 65 61 76 65 20 74 68 65 20 64 61 ght leave the da
17d7d 74 61 62 61 73 65 0a 2a 2a 20 69 6e 20 61 20 63 tabase.** in a c
17d7e 6f 72 72 75 70 74 65 64 20 73 74 61 74 65 2e 20 orrupted state.
17d7f 53 6f 20 69 66 20 74 68 69 73 20 72 6f 75 74 69 So if this routi
17d80 6e 65 20 66 61 69 6c 73 2c 20 74 68 65 20 64 61 ne fails, the da
17d81 74 61 62 61 73 65 20 73 68 6f 75 6c 64 0a 2a 2a tabase should.**
17d82 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e be rolled back.
17d83 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 68 69 72 64 .**.** The third
17d84 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 69 argument to thi
17d85 73 20 66 75 6e 63 74 69 6f 6e 2c 20 61 4f 76 66 s function, aOvf
17d86 6c 53 70 61 63 65 2c 20 69 73 20 61 20 70 6f 69 lSpace, is a poi
17d87 6e 74 65 72 20 74 6f 20 61 0a 2a 2a 20 62 75 66 nter to a.** buf
17d88 66 65 72 20 62 69 67 20 65 6e 6f 75 67 68 20 74 fer big enough t
17d89 6f 20 68 6f 6c 64 20 6f 6e 65 20 70 61 67 65 2e o hold one page.
17d8a 20 49 66 20 77 68 69 6c 65 20 69 6e 73 65 72 74 If while insert
17d8b 69 6e 67 20 63 65 6c 6c 73 20 69 6e 74 6f 20 74 ing cells into t
17d8c 68 65 20 70 61 72 65 6e 74 0a 2a 2a 20 70 61 67 he parent.** pag
17d8d 65 20 28 70 50 61 72 65 6e 74 29 20 74 68 65 20 e (pParent) the
17d8e 70 61 72 65 6e 74 20 70 61 67 65 20 62 65 63 6f parent page beco
17d8f 6d 65 73 20 6f 76 65 72 66 75 6c 6c 2c 20 74 68 mes overfull, th
17d90 69 73 20 62 75 66 66 65 72 20 69 73 0a 2a 2a 20 is buffer is.**
17d91 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68 used to store th
17d92 65 20 70 61 72 65 6e 74 27 73 20 6f 76 65 72 66 e parent's overf
17d93 6c 6f 77 20 63 65 6c 6c 73 2e 20 42 65 63 61 75 low cells. Becau
17d94 73 65 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e se this function
17d95 20 69 6e 73 65 72 74 73 0a 2a 2a 20 61 20 6d 61 inserts.** a ma
17d96 78 69 6d 75 6d 20 6f 66 20 66 6f 75 72 20 64 69 ximum of four di
17d97 76 69 64 65 72 20 63 65 6c 6c 73 20 69 6e 74 6f vider cells into
17d98 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 the parent page
17d99 2c 20 61 6e 64 20 74 68 65 20 6d 61 78 69 6d 75 , and the maximu
17d9a 6d 0a 2a 2a 20 73 69 7a 65 20 6f 66 20 61 20 63 m.** size of a c
17d9b 65 6c 6c 20 73 74 6f 72 65 64 20 77 69 74 68 69 ell stored withi
17d9c 6e 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20 6e 6f n an internal no
17d9d 64 65 20 69 73 20 61 6c 77 61 79 73 20 6c 65 73 de is always les
17d9e 73 20 74 68 61 6e 20 31 2f 34 0a 2a 2a 20 6f 66 s than 1/4.** of
17d9f 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65 2c 20 the page-size,
17da0 74 68 65 20 61 4f 76 66 6c 53 70 61 63 65 5b 5d the aOvflSpace[]
17da1 20 62 75 66 66 65 72 20 69 73 20 67 75 61 72 61 buffer is guara
17da2 6e 74 65 65 64 20 74 6f 20 62 65 20 6c 61 72 67 nteed to be larg
17da3 65 0a 2a 2a 20 65 6e 6f 75 67 68 20 66 6f 72 20 e.** enough for
17da4 61 6c 6c 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c all overflow cel
17da5 6c 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 4f 76 ls..**.** If aOv
17da6 66 6c 53 70 61 63 65 20 69 73 20 73 65 74 20 74 flSpace is set t
17da7 6f 20 61 20 6e 75 6c 6c 20 70 6f 69 6e 74 65 72 o a null pointer
17da8 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 , this function
17da9 72 65 74 75 72 6e 73 20 0a 2a 2a 20 53 51 4c 49 returns .** SQLI
17daa 54 45 5f 4e 4f 4d 45 4d 2e 0a 2a 2f 0a 73 74 61 TE_NOMEM..*/.sta
17dab 74 69 63 20 69 6e 74 20 62 61 6c 61 6e 63 65 5f tic int balance_
17dac 6e 6f 6e 72 6f 6f 74 28 0a 20 20 4d 65 6d 50 61 nonroot(. MemPa
17dad 67 65 20 2a 70 50 61 72 65 6e 74 2c 20 20 20 20 ge *pParent,
17dae 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 /* Pa
17daf 72 65 6e 74 20 70 61 67 65 20 6f 66 20 73 69 62 rent page of sib
17db0 6c 69 6e 67 73 20 62 65 69 6e 67 20 62 61 6c 61 lings being bala
17db1 6e 63 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 50 nced */. int iP
17db2 61 72 65 6e 74 49 64 78 2c 20 20 20 20 20 20 20 arentIdx,
17db3 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 /* Ind
17db4 65 78 20 6f 66 20 22 74 68 65 20 70 61 67 65 22 ex of "the page"
17db5 20 69 6e 20 70 50 61 72 65 6e 74 20 2a 2f 0a 20 in pParent */.
17db6 20 75 38 20 2a 61 4f 76 66 6c 53 70 61 63 65 2c u8 *aOvflSpace,
17db7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17db8 20 2f 2a 20 70 61 67 65 2d 73 69 7a 65 20 62 79 /* page-size by
17db9 74 65 73 20 6f 66 20 73 70 61 63 65 20 66 6f 72 tes of space for
17dba 20 70 61 72 65 6e 74 20 6f 76 66 6c 20 2a 2f 0a parent ovfl */.
17dbb 20 20 69 6e 74 20 69 73 52 6f 6f 74 20 20 20 20 int isRoot
17dbc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17dbd 20 20 2f 2a 20 54 72 75 65 20 69 66 20 70 50 61 /* True if pPa
17dbe 72 65 6e 74 20 69 73 20 61 20 72 6f 6f 74 2d 70 rent is a root-p
17dbf 61 67 65 20 2a 2f 0a 29 7b 0a 20 20 42 74 53 68 age */.){. BtSh
17dc0 61 72 65 64 20 2a 70 42 74 3b 20 20 20 20 20 20 ared *pBt;
17dc1 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
17dc2 77 68 6f 6c 65 20 64 61 74 61 62 61 73 65 20 2a whole database *
17dc3 2f 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 20 3d 20 /. int nCell =
17dc4 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0;
17dc5 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 65 /* Number of ce
17dc6 6c 6c 73 20 69 6e 20 61 70 43 65 6c 6c 5b 5d 20 lls in apCell[]
17dc7 2a 2f 0a 20 20 69 6e 74 20 6e 4d 61 78 43 65 6c */. int nMaxCel
17dc8 6c 73 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 ls = 0;
17dc9 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 64 20 73 /* Allocated s
17dca 69 7a 65 20 6f 66 20 61 70 43 65 6c 6c 2c 20 73 ize of apCell, s
17dcb 7a 43 65 6c 6c 2c 20 61 46 72 6f 6d 2e 20 2a 2f zCell, aFrom. */
17dcc 0a 20 20 69 6e 74 20 6e 4e 65 77 20 3d 20 30 3b . int nNew = 0;
17dcd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17dce 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 /* Number of pag
17dcf 65 73 20 69 6e 20 61 70 4e 65 77 5b 5d 20 2a 2f es in apNew[] */
17dd0 0a 20 20 69 6e 74 20 6e 4f 6c 64 3b 20 20 20 20 . int nOld;
17dd1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17dd2 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 /* Number of pag
17dd3 65 73 20 69 6e 20 61 70 4f 6c 64 5b 5d 20 2a 2f es in apOld[] */
17dd4 0a 20 20 69 6e 74 20 69 2c 20 6a 2c 20 6b 3b 20 . int i, j, k;
17dd5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17dd6 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 73 /* Loop counters
17dd7 20 2a 2f 0a 20 20 69 6e 74 20 6e 78 44 69 76 3b */. int nxDiv;
17dd8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17dd9 20 20 20 2f 2a 20 4e 65 78 74 20 64 69 76 69 64 /* Next divid
17dda 65 72 20 73 6c 6f 74 20 69 6e 20 70 50 61 72 65 er slot in pPare
17ddb 6e 74 2d 3e 61 43 65 6c 6c 5b 5d 20 2a 2f 0a 20 nt->aCell[] */.
17ddc 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
17ddd 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 2f 2a _OK; /*
17dde 20 54 68 65 20 72 65 74 75 72 6e 20 63 6f 64 65 The return code
17ddf 20 2a 2f 0a 20 20 75 31 36 20 6c 65 61 66 43 6f */. u16 leafCo
17de0 72 72 65 63 74 69 6f 6e 3b 20 20 20 20 20 20 20 rrection;
17de1 20 20 20 2f 2a 20 34 20 69 66 20 70 50 61 67 65 /* 4 if pPage
17de2 20 69 73 20 61 20 6c 65 61 66 2e 20 20 30 20 69 is a leaf. 0 i
17de3 66 20 6e 6f 74 20 2a 2f 0a 20 20 69 6e 74 20 6c f not */. int l
17de4 65 61 66 44 61 74 61 3b 20 20 20 20 20 20 20 20 eafData;
17de5 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
17de6 69 66 20 70 50 61 67 65 20 69 73 20 61 20 6c 65 if pPage is a le
17de7 61 66 20 6f 66 20 61 20 4c 45 41 46 44 41 54 41 af of a LEAFDATA
17de8 20 74 72 65 65 20 2a 2f 0a 20 20 69 6e 74 20 75 tree */. int u
17de9 73 61 62 6c 65 53 70 61 63 65 3b 20 20 20 20 20 sableSpace;
17dea 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 /* Bytes
17deb 20 69 6e 20 70 50 61 67 65 20 62 65 79 6f 6e 64 in pPage beyond
17dec 20 74 68 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 the header */.
17ded 20 69 6e 74 20 70 61 67 65 46 6c 61 67 73 3b 20 int pageFlags;
17dee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
17def 20 56 61 6c 75 65 20 6f 66 20 70 50 61 67 65 2d Value of pPage-
17df0 3e 61 44 61 74 61 5b 30 5d 20 2a 2f 0a 20 20 69 >aData[0] */. i
17df1 6e 74 20 73 75 62 74 6f 74 61 6c 3b 20 20 20 20 nt subtotal;
17df2 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 /* S
17df3 75 62 74 6f 74 61 6c 20 6f 66 20 62 79 74 65 73 ubtotal of bytes
17df4 20 69 6e 20 63 65 6c 6c 73 20 6f 6e 20 6f 6e 65 in cells on one
17df5 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 69 page */. int i
17df6 53 70 61 63 65 31 20 3d 20 30 3b 20 20 20 20 20 Space1 = 0;
17df7 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 /* First
17df8 20 75 6e 75 73 65 64 20 62 79 74 65 20 6f 66 20 unused byte of
17df9 61 53 70 61 63 65 31 5b 5d 20 2a 2f 0a 20 20 69 aSpace1[] */. i
17dfa 6e 74 20 69 4f 76 66 6c 53 70 61 63 65 20 3d 20 nt iOvflSpace =
17dfb 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 0; /* F
17dfc 69 72 73 74 20 75 6e 75 73 65 64 20 62 79 74 65 irst unused byte
17dfd 20 6f 66 20 61 4f 76 66 6c 53 70 61 63 65 5b 5d of aOvflSpace[]
17dfe 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 53 63 72 61 */. int szScra
17dff 74 63 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 tch;
17e00 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 73 63 /* Size of sc
17e01 72 61 74 63 68 20 6d 65 6d 6f 72 79 20 72 65 71 ratch memory req
17e02 75 65 73 74 65 64 20 2a 2f 0a 20 20 4d 65 6d 50 uested */. MemP
17e03 61 67 65 20 2a 61 70 4f 6c 64 5b 4e 42 5d 3b 20 age *apOld[NB];
17e04 20 20 20 20 20 20 20 20 20 2f 2a 20 70 50 61 67 /* pPag
17e05 65 20 61 6e 64 20 75 70 20 74 6f 20 74 77 6f 20 e and up to two
17e06 73 69 62 6c 69 6e 67 73 20 2a 2f 0a 20 20 4d 65 siblings */. Me
17e07 6d 50 61 67 65 20 2a 61 70 43 6f 70 79 5b 4e 42 mPage *apCopy[NB
17e08 5d 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 ]; /* Pr
17e09 69 76 61 74 65 20 63 6f 70 69 65 73 20 6f 66 20 ivate copies of
17e0a 61 70 4f 6c 64 5b 5d 20 70 61 67 65 73 20 2a 2f apOld[] pages */
17e0b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 61 70 4e 65 . MemPage *apNe
17e0c 77 5b 4e 42 2b 32 5d 3b 20 20 20 20 20 20 20 20 w[NB+2];
17e0d 2f 2a 20 70 50 61 67 65 20 61 6e 64 20 75 70 20 /* pPage and up
17e0e 74 6f 20 4e 42 20 73 69 62 6c 69 6e 67 73 20 61 to NB siblings a
17e0f 66 74 65 72 20 62 61 6c 61 6e 63 69 6e 67 20 2a fter balancing *
17e10 2f 0a 20 20 75 38 20 2a 70 52 69 67 68 74 3b 20 /. u8 *pRight;
17e11 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17e12 20 2f 2a 20 4c 6f 63 61 74 69 6f 6e 20 69 6e 20 /* Location in
17e13 70 61 72 65 6e 74 20 6f 66 20 72 69 67 68 74 2d parent of right-
17e14 73 69 62 6c 69 6e 67 20 70 6f 69 6e 74 65 72 20 sibling pointer
17e15 2a 2f 0a 20 20 75 38 20 2a 61 70 44 69 76 5b 4e */. u8 *apDiv[N
17e16 42 2d 31 5d 3b 20 20 20 20 20 20 20 20 20 20 20 B-1];
17e17 20 20 2f 2a 20 44 69 76 69 64 65 72 20 63 65 6c /* Divider cel
17e18 6c 73 20 69 6e 20 70 50 61 72 65 6e 74 20 2a 2f ls in pParent */
17e19 0a 20 20 69 6e 74 20 63 6e 74 4e 65 77 5b 4e 42 . int cntNew[NB
17e1a 2b 32 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20 +2];
17e1b 2f 2a 20 49 6e 64 65 78 20 69 6e 20 61 43 65 6c /* Index in aCel
17e1c 6c 5b 5d 20 6f 66 20 63 65 6c 6c 20 61 66 74 65 l[] of cell afte
17e1d 72 20 69 2d 74 68 20 70 61 67 65 20 2a 2f 0a 20 r i-th page */.
17e1e 20 69 6e 74 20 73 7a 4e 65 77 5b 4e 42 2b 32 5d int szNew[NB+2]
17e1f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
17e20 20 43 6f 6d 62 69 6e 65 64 20 73 69 7a 65 20 6f Combined size o
17e21 66 20 63 65 6c 6c 73 20 70 6c 61 63 65 20 6f 6e f cells place on
17e22 20 69 2d 74 68 20 70 61 67 65 20 2a 2f 0a 20 20 i-th page */.
17e23 75 38 20 2a 2a 61 70 43 65 6c 6c 20 3d 20 30 3b u8 **apCell = 0;
17e24 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
17e25 41 6c 6c 20 63 65 6c 6c 73 20 62 65 67 69 6e 20 All cells begin
17e26 62 61 6c 61 6e 63 65 64 20 2a 2f 0a 20 20 75 31 balanced */. u1
17e27 36 20 2a 73 7a 43 65 6c 6c 3b 20 20 20 20 20 20 6 *szCell;
17e28 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f /* Lo
17e29 63 61 6c 20 73 69 7a 65 20 6f 66 20 61 6c 6c 20 cal size of all
17e2a 63 65 6c 6c 73 20 69 6e 20 61 70 43 65 6c 6c 5b cells in apCell[
17e2b 5d 20 2a 2f 0a 20 20 75 38 20 2a 61 53 70 61 63 ] */. u8 *aSpac
17e2c 65 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 e1;
17e2d 20 20 20 20 2f 2a 20 53 70 61 63 65 20 66 6f 72 /* Space for
17e2e 20 63 6f 70 69 65 73 20 6f 66 20 64 69 76 69 64 copies of divid
17e2f 65 72 73 20 63 65 6c 6c 73 20 2a 2f 0a 20 20 50 ers cells */. P
17e30 67 6e 6f 20 70 67 6e 6f 3b 20 20 20 20 20 20 20 gno pgno;
17e31 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
17e32 65 6d 70 20 76 61 72 20 74 6f 20 73 74 6f 72 65 emp var to store
17e33 20 61 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 a page number i
17e34 6e 20 2a 2f 0a 0a 20 20 70 42 74 20 3d 20 70 50 n */.. pBt = pP
17e35 61 72 65 6e 74 2d 3e 70 42 74 3b 0a 20 20 61 73 arent->pBt;. as
17e36 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
17e37 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 tex_held(pBt->mu
17e38 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 tex) );. assert
17e39 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 ( sqlite3PagerIs
17e3a 77 72 69 74 65 61 62 6c 65 28 70 50 61 72 65 6e writeable(pParen
17e3b 74 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 0a t->pDbPage) );..
17e3c 23 69 66 20 30 0a 20 20 54 52 41 43 45 28 28 22 #if 0. TRACE(("
17e3d 42 41 4c 41 4e 43 45 3a 20 62 65 67 69 6e 20 70 BALANCE: begin p
17e3e 61 67 65 20 25 64 20 63 68 69 6c 64 20 6f 66 20 age %d child of
17e3f 25 64 5c 6e 22 2c 20 70 50 61 67 65 2d 3e 70 67 %d\n", pPage->pg
17e40 6e 6f 2c 20 70 50 61 72 65 6e 74 2d 3e 70 67 6e no, pParent->pgn
17e41 6f 29 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f o));.#endif.. /
17e42 2a 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74 20 * At this point
17e43 70 50 61 72 65 6e 74 20 6d 61 79 20 68 61 76 65 pParent may have
17e44 20 61 74 20 6d 6f 73 74 20 6f 6e 65 20 6f 76 65 at most one ove
17e45 72 66 6c 6f 77 20 63 65 6c 6c 2e 20 41 6e 64 20 rflow cell. And
17e46 69 66 0a 20 20 2a 2a 20 74 68 69 73 20 6f 76 65 if. ** this ove
17e47 72 66 6c 6f 77 20 63 65 6c 6c 20 69 73 20 70 72 rflow cell is pr
17e48 65 73 65 6e 74 2c 20 69 74 20 6d 75 73 74 20 62 esent, it must b
17e49 65 20 74 68 65 20 63 65 6c 6c 20 77 69 74 68 20 e the cell with
17e4a 0a 20 20 2a 2a 20 69 6e 64 65 78 20 69 50 61 72 . ** index iPar
17e4b 65 6e 74 49 64 78 2e 20 54 68 69 73 20 73 63 65 entIdx. This sce
17e4c 6e 61 72 69 6f 20 63 6f 6d 65 73 20 61 62 6f 75 nario comes abou
17e4d 74 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 t when this func
17e4e 74 69 6f 6e 0a 20 20 2a 2a 20 69 73 20 63 61 6c tion. ** is cal
17e4f 6c 65 64 20 28 69 6e 64 69 72 65 63 74 6c 79 29 led (indirectly)
17e50 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 42 74 72 from sqlite3Btr
17e51 65 65 44 65 6c 65 74 65 28 29 2e 0a 20 20 2a 2f eeDelete().. */
17e52 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72 65 . assert( pPare
17e53 6e 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 nt->nOverflow==0
17e54 20 7c 7c 20 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 || pParent->nOv
17e55 65 72 66 6c 6f 77 3d 3d 31 20 29 3b 0a 20 20 61 erflow==1 );. a
17e56 73 73 65 72 74 28 20 70 50 61 72 65 6e 74 2d 3e ssert( pParent->
17e57 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 7c 7c 20 nOverflow==0 ||
17e58 70 50 61 72 65 6e 74 2d 3e 61 4f 76 66 6c 5b 30 pParent->aOvfl[0
17e59 5d 2e 69 64 78 3d 3d 69 50 61 72 65 6e 74 49 64 ].idx==iParentId
17e5a 78 20 29 3b 0a 0a 20 20 69 66 28 20 21 61 4f 76 x );.. if( !aOv
17e5b 66 6c 53 70 61 63 65 20 29 7b 0a 20 20 20 20 72 flSpace ){. r
17e5c 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d eturn SQLITE_NOM
17e5d 45 4d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 EM;. }.. /* Fi
17e5e 6e 64 20 74 68 65 20 73 69 62 6c 69 6e 67 20 70 nd the sibling p
17e5f 61 67 65 73 20 74 6f 20 62 61 6c 61 6e 63 65 2e ages to balance.
17e60 20 41 6c 73 6f 20 6c 6f 63 61 74 65 20 74 68 65 Also locate the
17e61 20 63 65 6c 6c 73 20 69 6e 20 70 50 61 72 65 6e cells in pParen
17e62 74 20 0a 20 20 2a 2a 20 74 68 61 74 20 64 69 76 t . ** that div
17e63 69 64 65 20 74 68 65 20 73 69 62 6c 69 6e 67 73 ide the siblings
17e64 2e 20 41 6e 20 61 74 74 65 6d 70 74 20 69 73 20 . An attempt is
17e65 6d 61 64 65 20 74 6f 20 66 69 6e 64 20 4e 4e 20 made to find NN
17e66 73 69 62 6c 69 6e 67 73 20 6f 6e 20 0a 20 20 2a siblings on . *
17e67 2a 20 65 69 74 68 65 72 20 73 69 64 65 20 6f 66 * either side of
17e68 20 70 50 61 67 65 2e 20 4d 6f 72 65 20 73 69 62 pPage. More sib
17e69 6c 69 6e 67 73 20 61 72 65 20 74 61 6b 65 6e 20 lings are taken
17e6a 66 72 6f 6d 20 6f 6e 65 20 73 69 64 65 2c 20 68 from one side, h
17e6b 6f 77 65 76 65 72 2c 20 0a 20 20 2a 2a 20 69 66 owever, . ** if
17e6c 20 74 68 65 72 65 20 61 72 65 20 66 65 77 65 72 there are fewer
17e6d 20 74 68 61 6e 20 4e 4e 20 73 69 62 6c 69 6e 67 than NN sibling
17e6e 73 20 6f 6e 20 74 68 65 20 6f 74 68 65 72 20 73 s on the other s
17e6f 69 64 65 2e 20 49 66 20 70 50 61 72 65 6e 74 0a ide. If pParent.
17e70 20 20 2a 2a 20 68 61 73 20 4e 42 20 6f 72 20 66 ** has NB or f
17e71 65 77 65 72 20 63 68 69 6c 64 72 65 6e 20 74 68 ewer children th
17e72 65 6e 20 61 6c 6c 20 63 68 69 6c 64 72 65 6e 20 en all children
17e73 6f 66 20 70 50 61 72 65 6e 74 20 61 72 65 20 74 of pParent are t
17e74 61 6b 65 6e 2e 20 20 0a 20 20 2a 2a 0a 20 20 2a aken. . **. *
17e75 2a 20 54 68 69 73 20 6c 6f 6f 70 20 61 6c 73 6f * This loop also
17e76 20 64 72 6f 70 73 20 74 68 65 20 64 69 76 69 64 drops the divid
17e77 65 72 20 63 65 6c 6c 73 20 66 72 6f 6d 20 74 68 er cells from th
17e78 65 20 70 61 72 65 6e 74 20 70 61 67 65 2e 20 54 e parent page. T
17e79 68 69 73 0a 20 20 2a 2a 20 77 61 79 2c 20 74 68 his. ** way, th
17e7a 65 20 72 65 6d 61 69 6e 64 65 72 20 6f 66 20 74 e remainder of t
17e7b 68 65 20 66 75 6e 63 74 69 6f 6e 20 64 6f 65 73 he function does
17e7c 20 6e 6f 74 20 68 61 76 65 20 74 6f 20 64 65 61 not have to dea
17e7d 6c 20 77 69 74 68 20 61 6e 79 0a 20 20 2a 2a 20 l with any. **
17e7e 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 73 20 69 overflow cells i
17e7f 6e 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 n the parent pag
17e80 65 2c 20 73 69 6e 63 65 20 69 66 20 61 6e 79 20 e, since if any
17e81 65 78 69 73 74 65 64 20 74 68 65 79 20 77 69 6c existed they wil
17e82 6c 0a 20 20 2a 2a 20 68 61 76 65 20 61 6c 72 65 l. ** have alre
17e83 61 64 79 20 62 65 65 6e 20 72 65 6d 6f 76 65 64 ady been removed
17e84 2e 0a 20 20 2a 2f 0a 20 20 69 20 3d 20 70 50 61 .. */. i = pPa
17e85 72 65 6e 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 rent->nOverflow
17e86 2b 20 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c + pParent->nCell
17e87 3b 0a 20 20 69 66 28 20 69 3c 32 20 29 7b 0a 20 ;. if( i<2 ){.
17e88 20 20 20 6e 78 44 69 76 20 3d 20 30 3b 0a 20 20 nxDiv = 0;.
17e89 20 20 6e 4f 6c 64 20 3d 20 69 2b 31 3b 0a 20 20 nOld = i+1;.
17e8a 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 4f 6c 64 20 }else{. nOld
17e8b 3d 20 33 3b 0a 20 20 20 20 69 66 28 20 69 50 61 = 3;. if( iPa
17e8c 72 65 6e 74 49 64 78 3d 3d 30 20 29 7b 20 20 20 rentIdx==0 ){
17e8d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0a 20 .
17e8e 20 20 20 20 20 6e 78 44 69 76 20 3d 20 30 3b 0a nxDiv = 0;.
17e8f 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 69 50 }else if( iP
17e90 61 72 65 6e 74 49 64 78 3d 3d 69 20 29 7b 0a 20 arentIdx==i ){.
17e91 20 20 20 20 20 6e 78 44 69 76 20 3d 20 69 2d 32 nxDiv = i-2
17e92 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
17e93 20 20 20 6e 78 44 69 76 20 3d 20 69 50 61 72 65 nxDiv = iPare
17e94 6e 74 49 64 78 2d 31 3b 0a 20 20 20 20 7d 0a 20 ntIdx-1;. }.
17e95 20 20 20 69 20 3d 20 32 3b 0a 20 20 7d 0a 20 20 i = 2;. }.
17e96 69 66 28 20 28 69 2b 6e 78 44 69 76 2d 70 50 61 if( (i+nxDiv-pPa
17e97 72 65 6e 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 29 rent->nOverflow)
17e98 3d 3d 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c ==pParent->nCell
17e99 20 29 7b 0a 20 20 20 20 70 52 69 67 68 74 20 3d ){. pRight =
17e9a 20 26 70 50 61 72 65 6e 74 2d 3e 61 44 61 74 61 &pParent->aData
17e9b 5b 70 50 61 72 65 6e 74 2d 3e 68 64 72 4f 66 66 [pParent->hdrOff
17e9c 73 65 74 2b 38 5d 3b 0a 20 20 7d 65 6c 73 65 7b set+8];. }else{
17e9d 0a 20 20 20 20 70 52 69 67 68 74 20 3d 20 66 69 . pRight = fi
17e9e 6e 64 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 ndCell(pParent,
17e9f 69 2b 6e 78 44 69 76 2d 70 50 61 72 65 6e 74 2d i+nxDiv-pParent-
17ea0 3e 6e 4f 76 65 72 66 6c 6f 77 29 3b 0a 20 20 7d >nOverflow);. }
17ea1 0a 20 20 70 67 6e 6f 20 3d 20 67 65 74 34 62 79 . pgno = get4by
17ea2 74 65 28 70 52 69 67 68 74 29 3b 0a 20 20 77 68 te(pRight);. wh
17ea3 69 6c 65 28 20 31 20 29 7b 0a 20 20 20 20 72 63 ile( 1 ){. rc
17ea4 20 3d 20 67 65 74 41 6e 64 49 6e 69 74 50 61 67 = getAndInitPag
17ea5 65 28 70 42 74 2c 20 70 67 6e 6f 2c 20 26 61 70 e(pBt, pgno, &ap
17ea6 4f 6c 64 5b 69 5d 29 3b 0a 20 20 20 20 69 66 28 Old[i]);. if(
17ea7 20 72 63 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d rc ){. mem
17ea8 73 65 74 28 61 70 4f 6c 64 2c 20 30 2c 20 28 69 set(apOld, 0, (i
17ea9 2b 31 29 2a 73 69 7a 65 6f 66 28 4d 65 6d 50 61 +1)*sizeof(MemPa
17eaa 67 65 2a 29 29 3b 0a 20 20 20 20 20 20 67 6f 74 ge*));. got
17eab 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 o balance_cleanu
17eac 70 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e 4d 61 p;. }. nMa
17ead 78 43 65 6c 6c 73 20 2b 3d 20 31 2b 61 70 4f 6c xCells += 1+apOl
17eae 64 5b 69 5d 2d 3e 6e 43 65 6c 6c 2b 61 70 4f 6c d[i]->nCell+apOl
17eaf 64 5b 69 5d 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3b d[i]->nOverflow;
17eb0 0a 20 20 20 20 69 66 28 20 28 69 2d 2d 29 3d 3d . if( (i--)==
17eb1 30 20 29 20 62 72 65 61 6b 3b 0a 0a 20 20 20 20 0 ) break;..
17eb2 69 66 28 20 69 2b 6e 78 44 69 76 3d 3d 70 50 61 if( i+nxDiv==pPa
17eb3 72 65 6e 74 2d 3e 61 4f 76 66 6c 5b 30 5d 2e 69 rent->aOvfl[0].i
17eb4 64 78 20 26 26 20 70 50 61 72 65 6e 74 2d 3e 6e dx && pParent->n
17eb5 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20 Overflow ){.
17eb6 20 20 61 70 44 69 76 5b 69 5d 20 3d 20 70 50 61 apDiv[i] = pPa
17eb7 72 65 6e 74 2d 3e 61 4f 76 66 6c 5b 30 5d 2e 70 rent->aOvfl[0].p
17eb8 43 65 6c 6c 3b 0a 20 20 20 20 20 20 70 67 6e 6f Cell;. pgno
17eb9 20 3d 20 67 65 74 34 62 79 74 65 28 61 70 44 69 = get4byte(apDi
17eba 76 5b 69 5d 29 3b 0a 20 20 20 20 20 20 73 7a 4e v[i]);. szN
17ebb 65 77 5b 69 5d 20 3d 20 63 65 6c 6c 53 69 7a 65 ew[i] = cellSize
17ebc 50 74 72 28 70 50 61 72 65 6e 74 2c 20 61 70 44 Ptr(pParent, apD
17ebd 69 76 5b 69 5d 29 3b 0a 20 20 20 20 20 20 70 50 iv[i]);. pP
17ebe 61 72 65 6e 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 arent->nOverflow
17ebf 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b = 0;. }else{
17ec0 0a 20 20 20 20 20 20 61 70 44 69 76 5b 69 5d 20 . apDiv[i]
17ec1 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 72 65 = findCell(pPare
17ec2 6e 74 2c 20 69 2b 6e 78 44 69 76 2d 70 50 61 72 nt, i+nxDiv-pPar
17ec3 65 6e 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 29 3b ent->nOverflow);
17ec4 0a 20 20 20 20 20 20 70 67 6e 6f 20 3d 20 67 65 . pgno = ge
17ec5 74 34 62 79 74 65 28 61 70 44 69 76 5b 69 5d 29 t4byte(apDiv[i])
17ec6 3b 0a 20 20 20 20 20 20 73 7a 4e 65 77 5b 69 5d ;. szNew[i]
17ec7 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 = cellSizePtr(p
17ec8 50 61 72 65 6e 74 2c 20 61 70 44 69 76 5b 69 5d Parent, apDiv[i]
17ec9 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 44 72 6f );.. /* Dro
17eca 70 20 74 68 65 20 63 65 6c 6c 20 66 72 6f 6d 20 p the cell from
17ecb 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 2e the parent page.
17ecc 20 61 70 44 69 76 5b 69 5d 20 73 74 69 6c 6c 20 apDiv[i] still
17ecd 70 6f 69 6e 74 73 20 74 6f 0a 20 20 20 20 20 20 points to.
17ece 2a 2a 20 74 68 65 20 63 65 6c 6c 20 77 69 74 68 ** the cell with
17ecf 69 6e 20 74 68 65 20 70 61 72 65 6e 74 2c 20 65 in the parent, e
17ed0 76 65 6e 20 74 68 6f 75 67 68 20 69 74 20 68 61 ven though it ha
17ed1 73 20 62 65 65 6e 20 64 72 6f 70 70 65 64 2e 0a s been dropped..
17ed2 20 20 20 20 20 20 2a 2a 20 54 68 69 73 20 69 73 ** This is
17ed3 20 73 61 66 65 20 62 65 63 61 75 73 65 20 64 72 safe because dr
17ed4 6f 70 70 69 6e 67 20 61 20 63 65 6c 6c 20 6f 6e opping a cell on
17ed5 6c 79 20 6f 76 65 72 77 72 69 74 65 73 20 74 68 ly overwrites th
17ed6 65 20 66 69 72 73 74 0a 20 20 20 20 20 20 2a 2a e first. **
17ed7 20 66 6f 75 72 20 62 79 74 65 73 20 6f 66 20 69 four bytes of i
17ed8 74 2c 20 61 6e 64 20 74 68 69 73 20 66 75 6e 63 t, and this func
17ed9 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 6e 65 tion does not ne
17eda 65 64 20 74 68 65 20 66 69 72 73 74 0a 20 20 20 ed the first.
17edb 20 20 20 2a 2a 20 66 6f 75 72 20 62 79 74 65 73 ** four bytes
17edc 20 6f 66 20 74 68 65 20 64 69 76 69 64 65 72 20 of the divider
17edd 63 65 6c 6c 2e 20 53 6f 20 74 68 65 20 70 6f 69 cell. So the poi
17ede 6e 74 65 72 20 69 73 20 73 61 66 65 20 74 6f 20 nter is safe to
17edf 75 73 65 0a 20 20 20 20 20 20 2a 2a 20 6c 61 74 use. ** lat
17ee0 65 72 20 6f 6e 2e 20 20 0a 20 20 20 20 20 20 2a er on. . *
17ee1 2a 0a 20 20 20 20 20 20 2a 2a 20 55 6e 6c 65 73 *. ** Unles
17ee2 73 20 53 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 s SQLite is comp
17ee3 69 6c 65 64 20 69 6e 20 73 65 63 75 72 65 2d 64 iled in secure-d
17ee4 65 6c 65 74 65 20 6d 6f 64 65 2e 20 49 6e 20 74 elete mode. In t
17ee5 68 69 73 20 63 61 73 65 2c 0a 20 20 20 20 20 20 his case,.
17ee6 2a 2a 20 74 68 65 20 64 72 6f 70 43 65 6c 6c 28 ** the dropCell(
17ee7 29 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f ) routine will o
17ee8 76 65 72 77 72 69 74 65 20 74 68 65 20 65 6e 74 verwrite the ent
17ee9 69 72 65 20 63 65 6c 6c 20 77 69 74 68 20 7a 65 ire cell with ze
17eea 72 6f 65 73 2e 0a 20 20 20 20 20 20 2a 2a 20 49 roes.. ** I
17eeb 6e 20 74 68 69 73 20 63 61 73 65 2c 20 74 65 6d n this case, tem
17eec 70 6f 72 61 72 69 6c 79 20 63 6f 70 79 20 74 68 porarily copy th
17eed 65 20 63 65 6c 6c 20 69 6e 74 6f 20 74 68 65 20 e cell into the
17eee 61 4f 76 66 6c 53 70 61 63 65 5b 5d 0a 20 20 20 aOvflSpace[].
17eef 20 20 20 2a 2a 20 62 75 66 66 65 72 2e 20 49 74 ** buffer. It
17ef0 20 77 69 6c 6c 20 62 65 20 63 6f 70 69 65 64 20 will be copied
17ef1 6f 75 74 20 61 67 61 69 6e 20 61 73 20 73 6f 6f out again as soo
17ef2 6e 20 61 73 20 74 68 65 20 61 53 70 61 63 65 5b n as the aSpace[
17ef3 5d 20 62 75 66 66 65 72 0a 20 20 20 20 20 20 2a ] buffer. *
17ef4 2a 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 2e 20 * is allocated.
17ef5 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 */.#ifdef SQLIT
17ef6 45 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 45 0a E_SECURE_DELETE.
17ef7 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 61 4f memcpy(&aO
17ef8 76 66 6c 53 70 61 63 65 5b 61 70 44 69 76 5b 69 vflSpace[apDiv[i
17ef9 5d 2d 70 50 61 72 65 6e 74 2d 3e 61 44 61 74 61 ]-pParent->aData
17efa 5d 2c 20 61 70 44 69 76 5b 69 5d 2c 20 73 7a 4e ], apDiv[i], szN
17efb 65 77 5b 69 5d 29 3b 0a 20 20 20 20 20 20 61 70 ew[i]);. ap
17efc 44 69 76 5b 69 5d 20 3d 20 26 61 4f 76 66 6c 53 Div[i] = &aOvflS
17efd 70 61 63 65 5b 61 70 44 69 76 5b 69 5d 2d 70 50 pace[apDiv[i]-pP
17efe 61 72 65 6e 74 2d 3e 61 44 61 74 61 5d 3b 0a 23 arent->aData];.#
17eff 65 6e 64 69 66 0a 20 20 20 20 20 20 64 72 6f 70 endif. drop
17f00 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 69 2b Cell(pParent, i+
17f01 6e 78 44 69 76 2d 70 50 61 72 65 6e 74 2d 3e 6e nxDiv-pParent->n
17f02 4f 76 65 72 66 6c 6f 77 2c 20 73 7a 4e 65 77 5b Overflow, szNew[
17f03 69 5d 2c 20 26 72 63 29 3b 0a 20 20 20 20 7d 0a i], &rc);. }.
17f04 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 6e }.. /* Make n
17f05 4d 61 78 43 65 6c 6c 73 20 61 20 6d 75 6c 74 69 MaxCells a multi
17f06 70 6c 65 20 6f 66 20 34 20 69 6e 20 6f 72 64 65 ple of 4 in orde
17f07 72 20 74 6f 20 70 72 65 73 65 72 76 65 20 38 2d r to preserve 8-
17f08 62 79 74 65 0a 20 20 2a 2a 20 61 6c 69 67 6e 6d byte. ** alignm
17f09 65 6e 74 20 2a 2f 0a 20 20 6e 4d 61 78 43 65 6c ent */. nMaxCel
17f0a 6c 73 20 3d 20 28 6e 4d 61 78 43 65 6c 6c 73 20 ls = (nMaxCells
17f0b 2b 20 33 29 26 7e 33 3b 0a 0a 20 20 2f 2a 0a 20 + 3)&~3;.. /*.
17f0c 20 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 73 70 61 ** Allocate spa
17f0d 63 65 20 66 6f 72 20 6d 65 6d 6f 72 79 20 73 74 ce for memory st
17f0e 72 75 63 74 75 72 65 73 0a 20 20 2a 2f 0a 20 20 ructures. */.
17f0f 6b 20 3d 20 70 42 74 2d 3e 70 61 67 65 53 69 7a k = pBt->pageSiz
17f10 65 20 2b 20 52 4f 55 4e 44 38 28 73 69 7a 65 6f e + ROUND8(sizeo
17f11 66 28 4d 65 6d 50 61 67 65 29 29 3b 0a 20 20 73 f(MemPage));. s
17f12 7a 53 63 72 61 74 63 68 20 3d 0a 20 20 20 20 20 zScratch =.
17f13 20 20 6e 4d 61 78 43 65 6c 6c 73 2a 73 69 7a 65 nMaxCells*size
17f14 6f 66 28 75 38 2a 29 20 20 20 20 20 20 20 20 20 of(u8*)
17f15 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
17f16 20 61 70 43 65 6c 6c 20 2a 2f 0a 20 20 20 20 20 apCell */.
17f17 2b 20 6e 4d 61 78 43 65 6c 6c 73 2a 73 69 7a 65 + nMaxCells*size
17f18 6f 66 28 75 31 36 29 20 20 20 20 20 20 20 20 20 of(u16)
17f19 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
17f1a 20 73 7a 43 65 6c 6c 20 2a 2f 0a 20 20 20 20 20 szCell */.
17f1b 2b 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 + pBt->pageSize
17f1c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17f1d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
17f1e 20 61 53 70 61 63 65 31 20 2a 2f 0a 20 20 20 20 aSpace1 */.
17f1f 20 2b 20 6b 2a 6e 4f 6c 64 3b 20 20 20 20 20 20 + k*nOld;
17f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17f21 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
17f22 2a 20 50 61 67 65 20 63 6f 70 69 65 73 20 28 61 * Page copies (a
17f23 70 43 6f 70 79 29 20 2a 2f 0a 20 20 61 70 43 65 pCopy) */. apCe
17f24 6c 6c 20 3d 20 73 71 6c 69 74 65 33 53 63 72 61 ll = sqlite3Scra
17f25 74 63 68 4d 61 6c 6c 6f 63 28 20 73 7a 53 63 72 tchMalloc( szScr
17f26 61 74 63 68 20 29 3b 20 0a 20 20 69 66 28 20 61 atch ); . if( a
17f27 70 43 65 6c 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 pCell==0 ){.
17f28 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 rc = SQLITE_NOME
17f29 4d 3b 0a 20 20 20 20 67 6f 74 6f 20 62 61 6c 61 M;. goto bala
17f2a 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d nce_cleanup;. }
17f2b 0a 20 20 73 7a 43 65 6c 6c 20 3d 20 28 75 31 36 . szCell = (u16
17f2c 2a 29 26 61 70 43 65 6c 6c 5b 6e 4d 61 78 43 65 *)&apCell[nMaxCe
17f2d 6c 6c 73 5d 3b 0a 20 20 61 53 70 61 63 65 31 20 lls];. aSpace1
17f2e 3d 20 28 75 38 2a 29 26 73 7a 43 65 6c 6c 5b 6e = (u8*)&szCell[n
17f2f 4d 61 78 43 65 6c 6c 73 5d 3b 0a 20 20 61 73 73 MaxCells];. ass
17f30 65 72 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f ert( EIGHT_BYTE_
17f31 41 4c 49 47 4e 4d 45 4e 54 28 61 53 70 61 63 65 ALIGNMENT(aSpace
17f32 31 29 20 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 1) );.. /*. **
17f33 20 4c 6f 61 64 20 70 6f 69 6e 74 65 72 73 20 74 Load pointers t
17f34 6f 20 61 6c 6c 20 63 65 6c 6c 73 20 6f 6e 20 73 o all cells on s
17f35 69 62 6c 69 6e 67 20 70 61 67 65 73 20 61 6e 64 ibling pages and
17f36 20 74 68 65 20 64 69 76 69 64 65 72 20 63 65 6c the divider cel
17f37 6c 73 0a 20 20 2a 2a 20 69 6e 74 6f 20 74 68 65 ls. ** into the
17f38 20 6c 6f 63 61 6c 20 61 70 43 65 6c 6c 5b 5d 20 local apCell[]
17f39 61 72 72 61 79 2e 20 20 4d 61 6b 65 20 63 6f 70 array. Make cop
17f3a 69 65 73 20 6f 66 20 74 68 65 20 64 69 76 69 64 ies of the divid
17f3b 65 72 20 63 65 6c 6c 73 0a 20 20 2a 2a 20 69 6e er cells. ** in
17f3c 74 6f 20 73 70 61 63 65 20 6f 62 74 61 69 6e 65 to space obtaine
17f3d 64 20 66 72 6f 6d 20 61 53 70 61 63 65 31 5b 5d d from aSpace1[]
17f3e 20 61 6e 64 20 72 65 6d 6f 76 65 20 74 68 65 20 and remove the
17f3f 74 68 65 20 64 69 76 69 64 65 72 20 43 65 6c 6c the divider Cell
17f40 73 0a 20 20 2a 2a 20 66 72 6f 6d 20 70 50 61 72 s. ** from pPar
17f41 65 6e 74 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 ent.. **. ** I
17f42 66 20 74 68 65 20 73 69 62 6c 69 6e 67 73 20 61 f the siblings a
17f43 72 65 20 6f 6e 20 6c 65 61 66 20 70 61 67 65 73 re on leaf pages
17f44 2c 20 74 68 65 6e 20 74 68 65 20 63 68 69 6c 64 , then the child
17f45 20 70 6f 69 6e 74 65 72 73 20 6f 66 20 74 68 65 pointers of the
17f46 0a 20 20 2a 2a 20 64 69 76 69 64 65 72 20 63 65 . ** divider ce
17f47 6c 6c 73 20 61 72 65 20 73 74 72 69 70 70 65 64 lls are stripped
17f48 20 66 72 6f 6d 20 74 68 65 20 63 65 6c 6c 73 20 from the cells
17f49 62 65 66 6f 72 65 20 74 68 65 79 20 61 72 65 20 before they are
17f4a 63 6f 70 69 65 64 0a 20 20 2a 2a 20 69 6e 74 6f copied. ** into
17f4b 20 61 53 70 61 63 65 31 5b 5d 2e 20 20 49 6e 20 aSpace1[]. In
17f4c 74 68 69 73 20 77 61 79 2c 20 61 6c 6c 20 63 65 this way, all ce
17f4d 6c 6c 73 20 69 6e 20 61 70 43 65 6c 6c 5b 5d 20 lls in apCell[]
17f4e 61 72 65 20 77 69 74 68 6f 75 74 0a 20 20 2a 2a are without. **
17f4f 20 63 68 69 6c 64 20 70 6f 69 6e 74 65 72 73 2e child pointers.
17f50 20 20 49 66 20 73 69 62 6c 69 6e 67 73 20 61 72 If siblings ar
17f51 65 20 6e 6f 74 20 6c 65 61 76 65 73 2c 20 74 68 e not leaves, th
17f52 65 6e 20 61 6c 6c 20 63 65 6c 6c 20 69 6e 0a 20 en all cell in.
17f53 20 2a 2a 20 61 70 43 65 6c 6c 5b 5d 20 69 6e 63 ** apCell[] inc
17f54 6c 75 64 65 20 63 68 69 6c 64 20 70 6f 69 6e 74 lude child point
17f55 65 72 73 2e 20 20 45 69 74 68 65 72 20 77 61 79 ers. Either way
17f56 2c 20 61 6c 6c 20 63 65 6c 6c 73 20 69 6e 20 61 , all cells in a
17f57 70 43 65 6c 6c 5b 5d 0a 20 20 2a 2a 20 61 72 65 pCell[]. ** are
17f58 20 61 6c 69 6b 65 2e 0a 20 20 2a 2a 0a 20 20 2a alike.. **. *
17f59 2a 20 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e * leafCorrection
17f5a 3a 20 20 34 20 69 66 20 70 50 61 67 65 20 69 73 : 4 if pPage is
17f5b 20 61 20 6c 65 61 66 2e 20 20 30 20 69 66 20 70 a leaf. 0 if p
17f5c 50 61 67 65 20 69 73 20 6e 6f 74 20 61 20 6c 65 Page is not a le
17f5d 61 66 2e 0a 20 20 2a 2a 20 20 20 20 20 20 20 6c af.. ** l
17f5e 65 61 66 44 61 74 61 3a 20 20 31 20 69 66 20 70 eafData: 1 if p
17f5f 50 61 67 65 20 68 6f 6c 64 73 20 6b 65 79 2b 64 Page holds key+d
17f60 61 74 61 20 61 6e 64 20 70 50 61 72 65 6e 74 20 ata and pParent
17f61 68 6f 6c 64 73 20 6f 6e 6c 79 20 6b 65 79 73 2e holds only keys.
17f62 0a 20 20 2a 2f 0a 20 20 6c 65 61 66 43 6f 72 72 . */. leafCorr
17f63 65 63 74 69 6f 6e 20 3d 20 61 70 4f 6c 64 5b 30 ection = apOld[0
17f64 5d 2d 3e 6c 65 61 66 2a 34 3b 0a 20 20 6c 65 61 ]->leaf*4;. lea
17f65 66 44 61 74 61 20 3d 20 61 70 4f 6c 64 5b 30 5d fData = apOld[0]
17f66 2d 3e 68 61 73 44 61 74 61 3b 0a 20 20 66 6f 72 ->hasData;. for
17f67 28 69 3d 30 3b 20 69 3c 6e 4f 6c 64 3b 20 69 2b (i=0; i<nOld; i+
17f68 2b 29 7b 0a 20 20 20 20 69 6e 74 20 6c 69 6d 69 +){. int limi
17f69 74 3b 0a 20 20 20 20 0a 20 20 20 20 2f 2a 20 42 t;. . /* B
17f6a 65 66 6f 72 65 20 64 6f 69 6e 67 20 61 6e 79 74 efore doing anyt
17f6b 68 69 6e 67 20 65 6c 73 65 2c 20 74 61 6b 65 20 hing else, take
17f6c 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 69 27 a copy of the i'
17f6d 74 68 20 6f 72 69 67 69 6e 61 6c 20 73 69 62 6c th original sibl
17f6e 69 6e 67 0a 20 20 20 20 2a 2a 20 54 68 65 20 72 ing. ** The r
17f6f 65 73 74 20 6f 66 20 74 68 69 73 20 66 75 6e 63 est of this func
17f70 74 69 6f 6e 20 77 69 6c 6c 20 75 73 65 20 64 61 tion will use da
17f71 74 61 20 66 72 6f 6d 20 74 68 65 20 63 6f 70 69 ta from the copi
17f72 65 73 20 72 61 74 68 65 72 0a 20 20 20 20 2a 2a es rather. **
17f73 20 74 68 61 74 20 74 68 65 20 6f 72 69 67 69 6e that the origin
17f74 61 6c 20 70 61 67 65 73 20 73 69 6e 63 65 20 74 al pages since t
17f75 68 65 20 6f 72 69 67 69 6e 61 6c 20 70 61 67 65 he original page
17f76 73 20 77 69 6c 6c 20 62 65 20 69 6e 20 74 68 65 s will be in the
17f77 0a 20 20 20 20 2a 2a 20 70 72 6f 63 65 73 73 20 . ** process
17f78 6f 66 20 62 65 69 6e 67 20 6f 76 65 72 77 72 69 of being overwri
17f79 74 74 65 6e 2e 20 20 2a 2f 0a 20 20 20 20 4d 65 tten. */. Me
17f7a 6d 50 61 67 65 20 2a 70 4f 6c 64 20 3d 20 61 70 mPage *pOld = ap
17f7b 43 6f 70 79 5b 69 5d 20 3d 20 28 4d 65 6d 50 61 Copy[i] = (MemPa
17f7c 67 65 2a 29 26 61 53 70 61 63 65 31 5b 70 42 74 ge*)&aSpace1[pBt
17f7d 2d 3e 70 61 67 65 53 69 7a 65 20 2b 20 6b 2a 69 ->pageSize + k*i
17f7e 5d 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 4f ];. memcpy(pO
17f7f 6c 64 2c 20 61 70 4f 6c 64 5b 69 5d 2c 20 73 69 ld, apOld[i], si
17f80 7a 65 6f 66 28 4d 65 6d 50 61 67 65 29 29 3b 0a zeof(MemPage));.
17f81 20 20 20 20 70 4f 6c 64 2d 3e 61 44 61 74 61 20 pOld->aData
17f82 3d 20 28 76 6f 69 64 2a 29 26 70 4f 6c 64 5b 31 = (void*)&pOld[1
17f83 5d 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 4f ];. memcpy(pO
17f84 6c 64 2d 3e 61 44 61 74 61 2c 20 61 70 4f 6c 64 ld->aData, apOld
17f85 5b 69 5d 2d 3e 61 44 61 74 61 2c 20 70 42 74 2d [i]->aData, pBt-
17f86 3e 70 61 67 65 53 69 7a 65 29 3b 0a 0a 20 20 20 >pageSize);..
17f87 20 6c 69 6d 69 74 20 3d 20 70 4f 6c 64 2d 3e 6e limit = pOld->n
17f88 43 65 6c 6c 2b 70 4f 6c 64 2d 3e 6e 4f 76 65 72 Cell+pOld->nOver
17f89 66 6c 6f 77 3b 0a 20 20 20 20 66 6f 72 28 6a 3d flow;. for(j=
17f8a 30 3b 20 6a 3c 6c 69 6d 69 74 3b 20 6a 2b 2b 29 0; j<limit; j++)
17f8b 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 {. assert(
17f8c 6e 43 65 6c 6c 3c 6e 4d 61 78 43 65 6c 6c 73 20 nCell<nMaxCells
17f8d 29 3b 0a 20 20 20 20 20 20 61 70 43 65 6c 6c 5b );. apCell[
17f8e 6e 43 65 6c 6c 5d 20 3d 20 66 69 6e 64 4f 76 65 nCell] = findOve
17f8f 72 66 6c 6f 77 43 65 6c 6c 28 70 4f 6c 64 2c 20 rflowCell(pOld,
17f90 6a 29 3b 0a 20 20 20 20 20 20 73 7a 43 65 6c 6c j);. szCell
17f91 5b 6e 43 65 6c 6c 5d 20 3d 20 63 65 6c 6c 53 69 [nCell] = cellSi
17f92 7a 65 50 74 72 28 70 4f 6c 64 2c 20 61 70 43 65 zePtr(pOld, apCe
17f93 6c 6c 5b 6e 43 65 6c 6c 5d 29 3b 0a 20 20 20 20 ll[nCell]);.
17f94 20 20 6e 43 65 6c 6c 2b 2b 3b 0a 20 20 20 20 7d nCell++;. }
17f95 0a 20 20 20 20 69 66 28 20 69 3c 6e 4f 6c 64 2d . if( i<nOld-
17f96 31 20 26 26 20 21 6c 65 61 66 44 61 74 61 29 7b 1 && !leafData){
17f97 0a 20 20 20 20 20 20 75 31 36 20 73 7a 20 3d 20 . u16 sz =
17f98 28 75 31 36 29 73 7a 4e 65 77 5b 69 5d 3b 0a 20 (u16)szNew[i];.
17f99 20 20 20 20 20 75 38 20 2a 70 54 65 6d 70 3b 0a u8 *pTemp;.
17f9a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 43 assert( nC
17f9b 65 6c 6c 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b ell<nMaxCells );
17f9c 0a 20 20 20 20 20 20 73 7a 43 65 6c 6c 5b 6e 43 . szCell[nC
17f9d 65 6c 6c 5d 20 3d 20 73 7a 3b 0a 20 20 20 20 20 ell] = sz;.
17f9e 20 70 54 65 6d 70 20 3d 20 26 61 53 70 61 63 65 pTemp = &aSpace
17f9f 31 5b 69 53 70 61 63 65 31 5d 3b 0a 20 20 20 20 1[iSpace1];.
17fa0 20 20 69 53 70 61 63 65 31 20 2b 3d 20 73 7a 3b iSpace1 += sz;
17fa1 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 73 . assert( s
17fa2 7a 3c 3d 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 z<=pBt->pageSize
17fa3 2f 34 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 /4 );. asse
17fa4 72 74 28 20 69 53 70 61 63 65 31 3c 3d 70 42 74 rt( iSpace1<=pBt
17fa5 2d 3e 70 61 67 65 53 69 7a 65 20 29 3b 0a 20 20 ->pageSize );.
17fa6 20 20 20 20 6d 65 6d 63 70 79 28 70 54 65 6d 70 memcpy(pTemp
17fa7 2c 20 61 70 44 69 76 5b 69 5d 2c 20 73 7a 29 3b , apDiv[i], sz);
17fa8 0a 20 20 20 20 20 20 61 70 43 65 6c 6c 5b 6e 43 . apCell[nC
17fa9 65 6c 6c 5d 20 3d 20 70 54 65 6d 70 2b 6c 65 61 ell] = pTemp+lea
17faa 66 43 6f 72 72 65 63 74 69 6f 6e 3b 0a 20 20 20 fCorrection;.
17fab 20 20 20 61 73 73 65 72 74 28 20 6c 65 61 66 43 assert( leafC
17fac 6f 72 72 65 63 74 69 6f 6e 3d 3d 30 20 7c 7c 20 orrection==0 ||
17fad 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3d 3d leafCorrection==
17fae 34 20 29 3b 0a 20 20 20 20 20 20 73 7a 43 65 6c 4 );. szCel
17faf 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 73 7a 43 65 6c l[nCell] = szCel
17fb0 6c 5b 6e 43 65 6c 6c 5d 20 2d 20 6c 65 61 66 43 l[nCell] - leafC
17fb1 6f 72 72 65 63 74 69 6f 6e 3b 0a 20 20 20 20 20 orrection;.
17fb2 20 69 66 28 20 21 70 4f 6c 64 2d 3e 6c 65 61 66 if( !pOld->leaf
17fb3 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 ){. asse
17fb4 72 74 28 20 6c 65 61 66 43 6f 72 72 65 63 74 69 rt( leafCorrecti
17fb5 6f 6e 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 on==0 );.
17fb6 20 61 73 73 65 72 74 28 20 70 4f 6c 64 2d 3e 68 assert( pOld->h
17fb7 64 72 4f 66 66 73 65 74 3d 3d 30 20 29 3b 0a 20 drOffset==0 );.
17fb8 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 69 /* The ri
17fb9 67 68 74 20 70 6f 69 6e 74 65 72 20 6f 66 20 74 ght pointer of t
17fba 68 65 20 63 68 69 6c 64 20 70 61 67 65 20 70 4f he child page pO
17fbb 6c 64 20 62 65 63 6f 6d 65 73 20 74 68 65 20 6c ld becomes the l
17fbc 65 66 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 eft. ** p
17fbd 6f 69 6e 74 65 72 20 6f 66 20 74 68 65 20 64 69 ointer of the di
17fbe 76 69 64 65 72 20 63 65 6c 6c 20 2a 2f 0a 20 20 vider cell */.
17fbf 20 20 20 20 20 20 6d 65 6d 63 70 79 28 61 70 43 memcpy(apC
17fc0 65 6c 6c 5b 6e 43 65 6c 6c 5d 2c 20 26 70 4f 6c ell[nCell], &pOl
17fc1 64 2d 3e 61 44 61 74 61 5b 38 5d 2c 20 34 29 3b d->aData[8], 4);
17fc2 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
17fc3 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6c 65 assert( le
17fc4 61 66 43 6f 72 72 65 63 74 69 6f 6e 3d 3d 34 20 afCorrection==4
17fc5 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 );. if( s
17fc6 7a 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 3c 34 20 29 zCell[nCell]<4 )
17fc7 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 {. /* D
17fc8 6f 20 6e 6f 74 20 61 6c 6c 6f 77 20 61 6e 79 20 o not allow any
17fc9 63 65 6c 6c 73 20 73 6d 61 6c 6c 65 72 20 74 68 cells smaller th
17fca 61 6e 20 34 20 62 79 74 65 73 2e 20 2a 2f 0a 20 an 4 bytes. */.
17fcb 20 20 20 20 20 20 20 20 20 73 7a 43 65 6c 6c 5b szCell[
17fcc 6e 43 65 6c 6c 5d 20 3d 20 34 3b 0a 20 20 20 20 nCell] = 4;.
17fcd 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 }. }.
17fce 20 20 20 20 6e 43 65 6c 6c 2b 2b 3b 0a 20 20 20 nCell++;.
17fcf 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a }. }.. /*. *
17fd0 2a 20 46 69 67 75 72 65 20 6f 75 74 20 74 68 65 * Figure out the
17fd1 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 number of pages
17fd2 20 6e 65 65 64 65 64 20 74 6f 20 68 6f 6c 64 20 needed to hold
17fd3 61 6c 6c 20 6e 43 65 6c 6c 20 63 65 6c 6c 73 2e all nCell cells.
17fd4 0a 20 20 2a 2a 20 53 74 6f 72 65 20 74 68 69 73 . ** Store this
17fd5 20 6e 75 6d 62 65 72 20 69 6e 20 22 6b 22 2e 20 number in "k".
17fd6 20 41 6c 73 6f 20 63 6f 6d 70 75 74 65 20 73 7a Also compute sz
17fd7 4e 65 77 5b 5d 20 77 68 69 63 68 20 69 73 20 74 New[] which is t
17fd8 68 65 20 74 6f 74 61 6c 0a 20 20 2a 2a 20 73 69 he total. ** si
17fd9 7a 65 20 6f 66 20 61 6c 6c 20 63 65 6c 6c 73 20 ze of all cells
17fda 6f 6e 20 74 68 65 20 69 2d 74 68 20 70 61 67 65 on the i-th page
17fdb 20 61 6e 64 20 63 6e 74 4e 65 77 5b 5d 20 77 68 and cntNew[] wh
17fdc 69 63 68 20 69 73 20 74 68 65 20 69 6e 64 65 78 ich is the index
17fdd 0a 20 20 2a 2a 20 69 6e 20 61 70 43 65 6c 6c 5b . ** in apCell[
17fde 5d 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 74 68 ] of the cell th
17fdf 61 74 20 64 69 76 69 64 65 73 20 70 61 67 65 20 at divides page
17fe0 69 20 66 72 6f 6d 20 70 61 67 65 20 69 2b 31 2e i from page i+1.
17fe1 20 20 0a 20 20 2a 2a 20 63 6e 74 4e 65 77 5b 6b . ** cntNew[k
17fe2 5d 20 73 68 6f 75 6c 64 20 65 71 75 61 6c 20 6e ] should equal n
17fe3 43 65 6c 6c 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 Cell.. **. **
17fe4 56 61 6c 75 65 73 20 63 6f 6d 70 75 74 65 64 20 Values computed
17fe5 62 79 20 74 68 69 73 20 62 6c 6f 63 6b 3a 0a 20 by this block:.
17fe6 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 **. **
17fe7 20 20 20 6b 3a 20 54 68 65 20 74 6f 74 61 6c 20 k: The total
17fe8 6e 75 6d 62 65 72 20 6f 66 20 73 69 62 6c 69 6e number of siblin
17fe9 67 20 70 61 67 65 73 0a 20 20 2a 2a 20 20 20 20 g pages. **
17fea 73 7a 4e 65 77 5b 69 5d 3a 20 53 70 61 63 65 64 szNew[i]: Spaced
17feb 20 75 73 65 64 20 6f 6e 20 74 68 65 20 69 2d 74 used on the i-t
17fec 68 20 73 69 62 6c 69 6e 67 20 70 61 67 65 2e 0a h sibling page..
17fed 20 20 2a 2a 20 20 20 63 6e 74 4e 65 77 5b 69 5d ** cntNew[i]
17fee 3a 20 49 6e 64 65 78 20 69 6e 20 61 70 43 65 6c : Index in apCel
17fef 6c 5b 5d 20 61 6e 64 20 73 7a 43 65 6c 6c 5b 5d l[] and szCell[]
17ff0 20 66 6f 72 20 74 68 65 20 66 69 72 73 74 20 63 for the first c
17ff1 65 6c 6c 20 74 6f 0a 20 20 2a 2a 20 20 20 20 20 ell to. **
17ff2 20 20 20 20 20 20 20 20 20 74 68 65 20 72 69 67 the rig
17ff3 68 74 20 6f 66 20 74 68 65 20 69 2d 74 68 20 73 ht of the i-th s
17ff4 69 62 6c 69 6e 67 20 70 61 67 65 2e 0a 20 20 2a ibling page.. *
17ff5 2a 20 75 73 61 62 6c 65 53 70 61 63 65 3a 20 4e * usableSpace: N
17ff6 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f umber of bytes o
17ff7 66 20 73 70 61 63 65 20 61 76 61 69 6c 61 62 6c f space availabl
17ff8 65 20 6f 6e 20 65 61 63 68 20 73 69 62 6c 69 6e e on each siblin
17ff9 67 2e 0a 20 20 2a 2a 20 0a 20 20 2a 2f 0a 20 20 g.. ** . */.
17ffa 75 73 61 62 6c 65 53 70 61 63 65 20 3d 20 70 42 usableSpace = pB
17ffb 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 t->usableSize -
17ffc 31 32 20 2b 20 6c 65 61 66 43 6f 72 72 65 63 74 12 + leafCorrect
17ffd 69 6f 6e 3b 0a 20 20 66 6f 72 28 73 75 62 74 6f ion;. for(subto
17ffe 74 61 6c 3d 6b 3d 69 3d 30 3b 20 69 3c 6e 43 65 tal=k=i=0; i<nCe
17fff 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 61 73 ll; i++){. as
18000 73 65 72 74 28 20 69 3c 6e 4d 61 78 43 65 6c 6c sert( i<nMaxCell
18001 73 20 29 3b 0a 20 20 20 20 73 75 62 74 6f 74 61 s );. subtota
18002 6c 20 2b 3d 20 73 7a 43 65 6c 6c 5b 69 5d 20 2b l += szCell[i] +
18003 20 32 3b 0a 20 20 20 20 69 66 28 20 73 75 62 74 2;. if( subt
18004 6f 74 61 6c 20 3e 20 75 73 61 62 6c 65 53 70 61 otal > usableSpa
18005 63 65 20 29 7b 0a 20 20 20 20 20 20 73 7a 4e 65 ce ){. szNe
18006 77 5b 6b 5d 20 3d 20 73 75 62 74 6f 74 61 6c 20 w[k] = subtotal
18007 2d 20 73 7a 43 65 6c 6c 5b 69 5d 3b 0a 20 20 20 - szCell[i];.
18008 20 20 20 63 6e 74 4e 65 77 5b 6b 5d 20 3d 20 69 cntNew[k] = i
18009 3b 0a 20 20 20 20 20 20 69 66 28 20 6c 65 61 66 ;. if( leaf
1800a 44 61 74 61 20 29 7b 20 69 2d 2d 3b 20 7d 0a 20 Data ){ i--; }.
1800b 20 20 20 20 20 73 75 62 74 6f 74 61 6c 20 3d 20 subtotal =
1800c 30 3b 0a 20 20 20 20 20 20 6b 2b 2b 3b 0a 20 20 0;. k++;.
1800d 20 20 20 20 69 66 28 20 6b 3e 4e 42 2b 31 20 29 if( k>NB+1 )
1800e 7b 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f { rc = SQLITE_CO
1800f 52 52 55 50 54 3b 20 67 6f 74 6f 20 62 61 6c 61 RRUPT; goto bala
18010 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 20 7d 0a 20 nce_cleanup; }.
18011 20 20 20 7d 0a 20 20 7d 0a 20 20 73 7a 4e 65 77 }. }. szNew
18012 5b 6b 5d 20 3d 20 73 75 62 74 6f 74 61 6c 3b 0a [k] = subtotal;.
18013 20 20 63 6e 74 4e 65 77 5b 6b 5d 20 3d 20 6e 43 cntNew[k] = nC
18014 65 6c 6c 3b 0a 20 20 6b 2b 2b 3b 0a 0a 20 20 2f ell;. k++;.. /
18015 2a 0a 20 20 2a 2a 20 54 68 65 20 70 61 63 6b 69 *. ** The packi
18016 6e 67 20 63 6f 6d 70 75 74 65 64 20 62 79 20 74 ng computed by t
18017 68 65 20 70 72 65 76 69 6f 75 73 20 62 6c 6f 63 he previous bloc
18018 6b 20 69 73 20 62 69 61 73 65 64 20 74 6f 77 61 k is biased towa
18019 72 64 20 74 68 65 20 73 69 62 6c 69 6e 67 73 0a rd the siblings.
1801a 20 20 2a 2a 20 6f 6e 20 74 68 65 20 6c 65 66 74 ** on the left
1801b 20 73 69 64 65 2e 20 20 54 68 65 20 6c 65 66 74 side. The left
1801c 20 73 69 62 6c 69 6e 67 73 20 61 72 65 20 61 6c siblings are al
1801d 77 61 79 73 20 6e 65 61 72 6c 79 20 66 75 6c 6c ways nearly full
1801e 2c 20 77 68 69 6c 65 20 74 68 65 0a 20 20 2a 2a , while the. **
1801f 20 72 69 67 68 74 2d 6d 6f 73 74 20 73 69 62 6c right-most sibl
18020 69 6e 67 20 6d 69 67 68 74 20 62 65 20 6e 65 61 ing might be nea
18021 72 6c 79 20 65 6d 70 74 79 2e 20 20 54 68 69 73 rly empty. This
18022 20 62 6c 6f 63 6b 20 6f 66 20 63 6f 64 65 20 61 block of code a
18023 74 74 65 6d 70 74 73 0a 20 20 2a 2a 20 74 6f 20 ttempts. ** to
18024 61 64 6a 75 73 74 20 74 68 65 20 70 61 63 6b 69 adjust the packi
18025 6e 67 20 6f 66 20 73 69 62 6c 69 6e 67 73 20 74 ng of siblings t
18026 6f 20 67 65 74 20 61 20 62 65 74 74 65 72 20 62 o get a better b
18027 61 6c 61 6e 63 65 2e 0a 20 20 2a 2a 0a 20 20 2a alance.. **. *
18028 2a 20 54 68 69 73 20 61 64 6a 75 73 74 6d 65 6e * This adjustmen
18029 74 20 69 73 20 6d 6f 72 65 20 74 68 61 6e 20 61 t is more than a
1802a 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2e 20 n optimization.
1802b 20 54 68 65 20 70 61 63 6b 69 6e 67 20 61 62 6f The packing abo
1802c 76 65 20 6d 69 67 68 74 0a 20 20 2a 2a 20 62 65 ve might. ** be
1802d 20 73 6f 20 6f 75 74 20 6f 66 20 62 61 6c 61 6e so out of balan
1802e 63 65 20 61 73 20 74 6f 20 62 65 20 69 6c 6c 65 ce as to be ille
1802f 67 61 6c 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c gal. For exampl
18030 65 2c 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 e, the right-mos
18031 74 0a 20 20 2a 2a 20 73 69 62 6c 69 6e 67 20 6d t. ** sibling m
18032 69 67 68 74 20 62 65 20 63 6f 6d 70 6c 65 74 65 ight be complete
18033 6c 79 20 65 6d 70 74 79 2e 20 20 54 68 69 73 20 ly empty. This
18034 61 64 6a 75 73 74 6d 65 6e 74 20 69 73 20 6e 6f adjustment is no
18035 74 20 6f 70 74 69 6f 6e 61 6c 2e 0a 20 20 2a 2f t optional.. */
18036 0a 20 20 66 6f 72 28 69 3d 6b 2d 31 3b 20 69 3e . for(i=k-1; i>
18037 30 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 69 6e 74 0; i--){. int
18038 20 73 7a 52 69 67 68 74 20 3d 20 73 7a 4e 65 77 szRight = szNew
18039 5b 69 5d 3b 20 20 2f 2a 20 53 69 7a 65 20 6f 66 [i]; /* Size of
1803a 20 73 69 62 6c 69 6e 67 20 6f 6e 20 74 68 65 20 sibling on the
1803b 72 69 67 68 74 20 2a 2f 0a 20 20 20 20 69 6e 74 right */. int
1803c 20 73 7a 4c 65 66 74 20 3d 20 73 7a 4e 65 77 5b szLeft = szNew[
1803d 69 2d 31 5d 3b 20 2f 2a 20 53 69 7a 65 20 6f 66 i-1]; /* Size of
1803e 20 73 69 62 6c 69 6e 67 20 6f 6e 20 74 68 65 20 sibling on the
1803f 6c 65 66 74 20 2a 2f 0a 20 20 20 20 69 6e 74 20 left */. int
18040 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 r;
18041 2f 2a 20 49 6e 64 65 78 20 6f 66 20 72 69 67 68 /* Index of righ
18042 74 2d 6d 6f 73 74 20 63 65 6c 6c 20 69 6e 20 6c t-most cell in l
18043 65 66 74 20 73 69 62 6c 69 6e 67 20 2a 2f 0a 20 eft sibling */.
18044 20 20 20 69 6e 74 20 64 3b 20 20 20 20 20 20 20 int d;
18045 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 /* Index
18046 6f 66 20 66 69 72 73 74 20 63 65 6c 6c 20 74 6f of first cell to
18047 20 74 68 65 20 6c 65 66 74 20 6f 66 20 72 69 67 the left of rig
18048 68 74 20 73 69 62 6c 69 6e 67 20 2a 2f 0a 0a 20 ht sibling */..
18049 20 20 20 72 20 3d 20 63 6e 74 4e 65 77 5b 69 2d r = cntNew[i-
1804a 31 5d 20 2d 20 31 3b 0a 20 20 20 20 64 20 3d 20 1] - 1;. d =
1804b 72 20 2b 20 31 20 2d 20 6c 65 61 66 44 61 74 61 r + 1 - leafData
1804c 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 64 3c ;. assert( d<
1804d 6e 4d 61 78 43 65 6c 6c 73 20 29 3b 0a 20 20 20 nMaxCells );.
1804e 20 61 73 73 65 72 74 28 20 72 3c 6e 4d 61 78 43 assert( r<nMaxC
1804f 65 6c 6c 73 20 29 3b 0a 20 20 20 20 77 68 69 6c ells );. whil
18050 65 28 20 73 7a 52 69 67 68 74 3d 3d 30 20 7c 7c e( szRight==0 ||
18051 20 73 7a 52 69 67 68 74 2b 73 7a 43 65 6c 6c 5b szRight+szCell[
18052 64 5d 2b 32 3c 3d 73 7a 4c 65 66 74 2d 28 73 7a d]+2<=szLeft-(sz
18053 43 65 6c 6c 5b 72 5d 2b 32 29 20 29 7b 0a 20 20 Cell[r]+2) ){.
18054 20 20 20 20 73 7a 52 69 67 68 74 20 2b 3d 20 73 szRight += s
18055 7a 43 65 6c 6c 5b 64 5d 20 2b 20 32 3b 0a 20 20 zCell[d] + 2;.
18056 20 20 20 20 73 7a 4c 65 66 74 20 2d 3d 20 73 7a szLeft -= sz
18057 43 65 6c 6c 5b 72 5d 20 2b 20 32 3b 0a 20 20 20 Cell[r] + 2;.
18058 20 20 20 63 6e 74 4e 65 77 5b 69 2d 31 5d 2d 2d cntNew[i-1]--
18059 3b 0a 20 20 20 20 20 20 72 20 3d 20 63 6e 74 4e ;. r = cntN
1805a 65 77 5b 69 2d 31 5d 20 2d 20 31 3b 0a 20 20 20 ew[i-1] - 1;.
1805b 20 20 20 64 20 3d 20 72 20 2b 20 31 20 2d 20 6c d = r + 1 - l
1805c 65 61 66 44 61 74 61 3b 0a 20 20 20 20 7d 0a 20 eafData;. }.
1805d 20 20 20 73 7a 4e 65 77 5b 69 5d 20 3d 20 73 7a szNew[i] = sz
1805e 52 69 67 68 74 3b 0a 20 20 20 20 73 7a 4e 65 77 Right;. szNew
1805f 5b 69 2d 31 5d 20 3d 20 73 7a 4c 65 66 74 3b 0a [i-1] = szLeft;.
18060 20 20 7d 0a 0a 20 20 2f 2a 20 45 69 74 68 65 72 }.. /* Either
18061 20 77 65 20 66 6f 75 6e 64 20 6f 6e 65 20 6f 72 we found one or
18062 20 6d 6f 72 65 20 63 65 6c 6c 73 20 28 63 6e 74 more cells (cnt
18063 6e 65 77 5b 30 5d 29 3e 30 29 20 6f 72 20 70 50 new[0])>0) or pP
18064 61 67 65 20 69 73 0a 20 20 2a 2a 20 61 20 76 69 age is. ** a vi
18065 72 74 75 61 6c 20 72 6f 6f 74 20 70 61 67 65 2e rtual root page.
18066 20 20 41 20 76 69 72 74 75 61 6c 20 72 6f 6f 74 A virtual root
18067 20 70 61 67 65 20 69 73 20 77 68 65 6e 20 74 68 page is when th
18068 65 20 72 65 61 6c 20 72 6f 6f 74 0a 20 20 2a 2a e real root. **
18069 20 70 61 67 65 20 69 73 20 70 61 67 65 20 31 20 page is page 1
1806a 61 6e 64 20 77 65 20 61 72 65 20 74 68 65 20 6f and we are the o
1806b 6e 6c 79 20 63 68 69 6c 64 20 6f 66 20 74 68 61 nly child of tha
1806c 74 20 70 61 67 65 2e 0a 20 20 2a 2f 0a 20 20 61 t page.. */. a
1806d 73 73 65 72 74 28 20 63 6e 74 4e 65 77 5b 30 5d ssert( cntNew[0]
1806e 3e 30 20 7c 7c 20 28 70 50 61 72 65 6e 74 2d 3e >0 || (pParent->
1806f 70 67 6e 6f 3d 3d 31 20 26 26 20 70 50 61 72 65 pgno==1 && pPare
18070 6e 74 2d 3e 6e 43 65 6c 6c 3d 3d 30 29 20 29 3b nt->nCell==0) );
18071 0a 0a 20 20 54 52 41 43 45 28 28 22 42 41 4c 41 .. TRACE(("BALA
18072 4e 43 45 3a 20 6f 6c 64 3a 20 25 64 20 25 64 20 NCE: old: %d %d
18073 25 64 20 20 22 2c 0a 20 20 20 20 61 70 4f 6c 64 %d ",. apOld
18074 5b 30 5d 2d 3e 70 67 6e 6f 2c 20 0a 20 20 20 20 [0]->pgno, .
18075 6e 4f 6c 64 3e 3d 32 20 3f 20 61 70 4f 6c 64 5b nOld>=2 ? apOld[
18076 31 5d 2d 3e 70 67 6e 6f 20 3a 20 30 2c 0a 20 20 1]->pgno : 0,.
18077 20 20 6e 4f 6c 64 3e 3d 33 20 3f 20 61 70 4f 6c nOld>=3 ? apOl
18078 64 5b 32 5d 2d 3e 70 67 6e 6f 20 3a 20 30 0a 20 d[2]->pgno : 0.
18079 20 29 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 ));.. /*. **
1807a 41 6c 6c 6f 63 61 74 65 20 6b 20 6e 65 77 20 70 Allocate k new p
1807b 61 67 65 73 2e 20 20 52 65 75 73 65 20 6f 6c 64 ages. Reuse old
1807c 20 70 61 67 65 73 20 77 68 65 72 65 20 70 6f 73 pages where pos
1807d 73 69 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 sible.. */. if
1807e 28 20 61 70 4f 6c 64 5b 30 5d 2d 3e 70 67 6e 6f ( apOld[0]->pgno
1807f 3c 3d 31 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 <=1 ){. rc =
18080 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 3b 0a SQLITE_CORRUPT;.
18081 20 20 20 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 goto balance
18082 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 _cleanup;. }.
18083 70 61 67 65 46 6c 61 67 73 20 3d 20 61 70 4f 6c pageFlags = apOl
18084 64 5b 30 5d 2d 3e 61 44 61 74 61 5b 30 5d 3b 0a d[0]->aData[0];.
18085 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6b 3b 20 for(i=0; i<k;
18086 69 2b 2b 29 7b 0a 20 20 20 20 4d 65 6d 50 61 67 i++){. MemPag
18087 65 20 2a 70 4e 65 77 3b 0a 20 20 20 20 69 66 28 e *pNew;. if(
18088 20 69 3c 6e 4f 6c 64 20 29 7b 0a 20 20 20 20 20 i<nOld ){.
18089 20 70 4e 65 77 20 3d 20 61 70 4e 65 77 5b 69 5d pNew = apNew[i]
1808a 20 3d 20 61 70 4f 6c 64 5b 69 5d 3b 0a 20 20 20 = apOld[i];.
1808b 20 20 20 61 70 4f 6c 64 5b 69 5d 20 3d 20 30 3b apOld[i] = 0;
1808c 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
1808d 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 4e te3PagerWrite(pN
1808e 65 77 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 ew->pDbPage);.
1808f 20 20 20 20 6e 4e 65 77 2b 2b 3b 0a 20 20 20 20 nNew++;.
18090 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 if( rc ) goto
18091 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b balance_cleanup;
18092 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
18093 20 20 61 73 73 65 72 74 28 20 69 3e 30 20 29 3b assert( i>0 );
18094 0a 20 20 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f . rc = allo
18095 63 61 74 65 42 74 72 65 65 50 61 67 65 28 70 42 cateBtreePage(pB
18096 74 2c 20 26 70 4e 65 77 2c 20 26 70 67 6e 6f 2c t, &pNew, &pgno,
18097 20 70 67 6e 6f 2c 20 30 29 3b 0a 20 20 20 20 20 pgno, 0);.
18098 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 62 if( rc ) goto b
18099 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a alance_cleanup;.
1809a 20 20 20 20 20 20 61 70 4e 65 77 5b 69 5d 20 3d apNew[i] =
1809b 20 70 4e 65 77 3b 0a 20 20 20 20 20 20 6e 4e 65 pNew;. nNe
1809c 77 2b 2b 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 53 w++;.. /* S
1809d 65 74 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d et the pointer-m
1809e 61 70 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65 ap entry for the
1809f 20 6e 65 77 20 73 69 62 6c 69 6e 67 20 70 61 67 new sibling pag
180a0 65 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 e. */. if(
180a1 49 53 41 55 54 4f 56 41 43 55 55 4d 20 29 7b 0a ISAUTOVACUUM ){.
180a2 20 20 20 20 20 20 20 20 70 74 72 6d 61 70 50 75 ptrmapPu
180a3 74 28 70 42 74 2c 20 70 4e 65 77 2d 3e 70 67 6e t(pBt, pNew->pgn
180a4 6f 2c 20 50 54 52 4d 41 50 5f 42 54 52 45 45 2c o, PTRMAP_BTREE,
180a5 20 70 50 61 72 65 6e 74 2d 3e 70 67 6e 6f 2c 20 pParent->pgno,
180a6 26 72 63 29 3b 0a 20 20 20 20 20 20 20 20 69 66 &rc);. if
180a7 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
180a8 29 7b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 ){. got
180a9 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 o balance_cleanu
180aa 70 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 p;. }.
180ab 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a }. }. }..
180ac 20 20 2f 2a 20 46 72 65 65 20 61 6e 79 20 6f 6c /* Free any ol
180ad 64 20 70 61 67 65 73 20 74 68 61 74 20 77 65 72 d pages that wer
180ae 65 20 6e 6f 74 20 72 65 75 73 65 64 20 61 73 20 e not reused as
180af 6e 65 77 20 70 61 67 65 73 2e 0a 20 20 2a 2f 0a new pages.. */.
180b0 20 20 77 68 69 6c 65 28 20 69 3c 6e 4f 6c 64 20 while( i<nOld
180b1 29 7b 0a 20 20 20 20 66 72 65 65 50 61 67 65 28 ){. freePage(
180b2 61 70 4f 6c 64 5b 69 5d 2c 20 26 72 63 29 3b 0a apOld[i], &rc);.
180b3 20 20 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 if( rc ) got
180b4 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 o balance_cleanu
180b5 70 3b 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61 p;. releasePa
180b6 67 65 28 61 70 4f 6c 64 5b 69 5d 29 3b 0a 20 20 ge(apOld[i]);.
180b7 20 20 61 70 4f 6c 64 5b 69 5d 20 3d 20 30 3b 0a apOld[i] = 0;.
180b8 20 20 20 20 69 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 i++;. }..
180b9 2f 2a 0a 20 20 2a 2a 20 50 75 74 20 74 68 65 20 /*. ** Put the
180ba 6e 65 77 20 70 61 67 65 73 20 69 6e 20 61 63 63 new pages in acc
180bb 65 6e 64 69 6e 67 20 6f 72 64 65 72 2e 20 20 54 ending order. T
180bc 68 69 73 20 68 65 6c 70 73 20 74 6f 0a 20 20 2a his helps to. *
180bd 2a 20 6b 65 65 70 20 65 6e 74 72 69 65 73 20 69 * keep entries i
180be 6e 20 74 68 65 20 64 69 73 6b 20 66 69 6c 65 20 n the disk file
180bf 69 6e 20 6f 72 64 65 72 20 73 6f 20 74 68 61 74 in order so that
180c0 20 61 20 73 63 61 6e 0a 20 20 2a 2a 20 6f 66 20 a scan. ** of
180c1 74 68 65 20 74 61 62 6c 65 20 69 73 20 61 20 6c the table is a l
180c2 69 6e 65 61 72 20 73 63 61 6e 20 74 68 72 6f 75 inear scan throu
180c3 67 68 20 74 68 65 20 66 69 6c 65 2e 20 20 54 68 gh the file. Th
180c4 61 74 0a 20 20 2a 2a 20 69 6e 20 74 75 72 6e 20 at. ** in turn
180c5 68 65 6c 70 73 20 74 68 65 20 6f 70 65 72 61 74 helps the operat
180c6 69 6e 67 20 73 79 73 74 65 6d 20 74 6f 20 64 65 ing system to de
180c7 6c 69 76 65 72 20 70 61 67 65 73 0a 20 20 2a 2a liver pages. **
180c8 20 66 72 6f 6d 20 74 68 65 20 64 69 73 6b 20 6d from the disk m
180c9 6f 72 65 20 72 61 70 69 64 6c 79 2e 0a 20 20 2a ore rapidly.. *
180ca 2a 0a 20 20 2a 2a 20 41 6e 20 4f 28 6e 5e 32 29 *. ** An O(n^2)
180cb 20 69 6e 73 65 72 74 69 6f 6e 20 73 6f 72 74 20 insertion sort
180cc 61 6c 67 6f 72 69 74 68 6d 20 69 73 20 75 73 65 algorithm is use
180cd 64 2c 20 62 75 74 20 73 69 6e 63 65 0a 20 20 2a d, but since. *
180ce 2a 20 6e 20 69 73 20 6e 65 76 65 72 20 6d 6f 72 * n is never mor
180cf 65 20 74 68 61 6e 20 4e 42 20 28 61 20 73 6d 61 e than NB (a sma
180d0 6c 6c 20 63 6f 6e 73 74 61 6e 74 29 2c 20 74 68 ll constant), th
180d1 61 74 20 73 68 6f 75 6c 64 0a 20 20 2a 2a 20 6e at should. ** n
180d2 6f 74 20 62 65 20 61 20 70 72 6f 62 6c 65 6d 2e ot be a problem.
180d3 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57 68 65 6e 20 . **. ** When
180d4 4e 42 3d 3d 33 2c 20 74 68 69 73 20 6f 6e 65 20 NB==3, this one
180d5 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 6d 61 6b optimization mak
180d6 65 73 20 74 68 65 20 64 61 74 61 62 61 73 65 0a es the database.
180d7 20 20 2a 2a 20 61 62 6f 75 74 20 32 35 25 20 66 ** about 25% f
180d8 61 73 74 65 72 20 66 6f 72 20 6c 61 72 67 65 20 aster for large
180d9 69 6e 73 65 72 74 69 6f 6e 73 20 61 6e 64 20 64 insertions and d
180da 65 6c 65 74 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 eletions.. */.
180db 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6b 2d 31 3b for(i=0; i<k-1;
180dc 20 69 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 6d i++){. int m
180dd 69 6e 56 20 3d 20 61 70 4e 65 77 5b 69 5d 2d 3e inV = apNew[i]->
180de 70 67 6e 6f 3b 0a 20 20 20 20 69 6e 74 20 6d 69 pgno;. int mi
180df 6e 49 20 3d 20 69 3b 0a 20 20 20 20 66 6f 72 28 nI = i;. for(
180e0 6a 3d 69 2b 31 3b 20 6a 3c 6b 3b 20 6a 2b 2b 29 j=i+1; j<k; j++)
180e1 7b 0a 20 20 20 20 20 20 69 66 28 20 61 70 4e 65 {. if( apNe
180e2 77 5b 6a 5d 2d 3e 70 67 6e 6f 3c 28 75 6e 73 69 w[j]->pgno<(unsi
180e3 67 6e 65 64 29 6d 69 6e 56 20 29 7b 0a 20 20 20 gned)minV ){.
180e4 20 20 20 20 20 6d 69 6e 49 20 3d 20 6a 3b 0a 20 minI = j;.
180e5 20 20 20 20 20 20 20 6d 69 6e 56 20 3d 20 61 70 minV = ap
180e6 4e 65 77 5b 6a 5d 2d 3e 70 67 6e 6f 3b 0a 20 20 New[j]->pgno;.
180e7 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 }. }.
180e8 69 66 28 20 6d 69 6e 49 3e 69 20 29 7b 0a 20 20 if( minI>i ){.
180e9 20 20 20 20 69 6e 74 20 74 3b 0a 20 20 20 20 20 int t;.
180ea 20 4d 65 6d 50 61 67 65 20 2a 70 54 3b 0a 20 20 MemPage *pT;.
180eb 20 20 20 20 74 20 3d 20 61 70 4e 65 77 5b 69 5d t = apNew[i]
180ec 2d 3e 70 67 6e 6f 3b 0a 20 20 20 20 20 20 70 54 ->pgno;. pT
180ed 20 3d 20 61 70 4e 65 77 5b 69 5d 3b 0a 20 20 20 = apNew[i];.
180ee 20 20 20 61 70 4e 65 77 5b 69 5d 20 3d 20 61 70 apNew[i] = ap
180ef 4e 65 77 5b 6d 69 6e 49 5d 3b 0a 20 20 20 20 20 New[minI];.
180f0 20 61 70 4e 65 77 5b 6d 69 6e 49 5d 20 3d 20 70 apNew[minI] = p
180f1 54 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 54 T;. }. }. T
180f2 52 41 43 45 28 28 22 6e 65 77 3a 20 25 64 28 25 RACE(("new: %d(%
180f3 64 29 20 25 64 28 25 64 29 20 25 64 28 25 64 29 d) %d(%d) %d(%d)
180f4 20 25 64 28 25 64 29 20 25 64 28 25 64 29 5c 6e %d(%d) %d(%d)\n
180f5 22 2c 0a 20 20 20 20 61 70 4e 65 77 5b 30 5d 2d ",. apNew[0]-
180f6 3e 70 67 6e 6f 2c 20 73 7a 4e 65 77 5b 30 5d 2c >pgno, szNew[0],
180f7 0a 20 20 20 20 6e 4e 65 77 3e 3d 32 20 3f 20 61 . nNew>=2 ? a
180f8 70 4e 65 77 5b 31 5d 2d 3e 70 67 6e 6f 20 3a 20 pNew[1]->pgno :
180f9 30 2c 20 6e 4e 65 77 3e 3d 32 20 3f 20 73 7a 4e 0, nNew>=2 ? szN
180fa 65 77 5b 31 5d 20 3a 20 30 2c 0a 20 20 20 20 6e ew[1] : 0,. n
180fb 4e 65 77 3e 3d 33 20 3f 20 61 70 4e 65 77 5b 32 New>=3 ? apNew[2
180fc 5d 2d 3e 70 67 6e 6f 20 3a 20 30 2c 20 6e 4e 65 ]->pgno : 0, nNe
180fd 77 3e 3d 33 20 3f 20 73 7a 4e 65 77 5b 32 5d 20 w>=3 ? szNew[2]
180fe 3a 20 30 2c 0a 20 20 20 20 6e 4e 65 77 3e 3d 34 : 0,. nNew>=4
180ff 20 3f 20 61 70 4e 65 77 5b 33 5d 2d 3e 70 67 6e ? apNew[3]->pgn
18100 6f 20 3a 20 30 2c 20 6e 4e 65 77 3e 3d 34 20 3f o : 0, nNew>=4 ?
18101 20 73 7a 4e 65 77 5b 33 5d 20 3a 20 30 2c 0a 20 szNew[3] : 0,.
18102 20 20 20 6e 4e 65 77 3e 3d 35 20 3f 20 61 70 4e nNew>=5 ? apN
18103 65 77 5b 34 5d 2d 3e 70 67 6e 6f 20 3a 20 30 2c ew[4]->pgno : 0,
18104 20 6e 4e 65 77 3e 3d 35 20 3f 20 73 7a 4e 65 77 nNew>=5 ? szNew
18105 5b 34 5d 20 3a 20 30 29 29 3b 0a 0a 20 20 61 73 [4] : 0));.. as
18106 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 sert( sqlite3Pag
18107 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 erIswriteable(pP
18108 61 72 65 6e 74 2d 3e 70 44 62 50 61 67 65 29 20 arent->pDbPage)
18109 29 3b 0a 20 20 70 75 74 34 62 79 74 65 28 70 52 );. put4byte(pR
1810a 69 67 68 74 2c 20 61 70 4e 65 77 5b 6e 4e 65 77 ight, apNew[nNew
1810b 2d 31 5d 2d 3e 70 67 6e 6f 29 3b 0a 0a 20 20 2f -1]->pgno);.. /
1810c 2a 0a 20 20 2a 2a 20 45 76 65 6e 6c 79 20 64 69 *. ** Evenly di
1810d 73 74 72 69 62 75 74 65 20 74 68 65 20 64 61 74 stribute the dat
1810e 61 20 69 6e 20 61 70 43 65 6c 6c 5b 5d 20 61 63 a in apCell[] ac
1810f 72 6f 73 73 20 74 68 65 20 6e 65 77 20 70 61 67 ross the new pag
18110 65 73 2e 0a 20 20 2a 2a 20 49 6e 73 65 72 74 20 es.. ** Insert
18111 64 69 76 69 64 65 72 20 63 65 6c 6c 73 20 69 6e divider cells in
18112 74 6f 20 70 50 61 72 65 6e 74 20 61 73 20 6e 65 to pParent as ne
18113 63 65 73 73 61 72 79 2e 0a 20 20 2a 2f 0a 20 20 cessary.. */.
18114 6a 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30 j = 0;. for(i=0
18115 3b 20 69 3c 6e 4e 65 77 3b 20 69 2b 2b 29 7b 0a ; i<nNew; i++){.
18116 20 20 20 20 2f 2a 20 41 73 73 65 6d 62 6c 65 20 /* Assemble
18117 74 68 65 20 6e 65 77 20 73 69 62 6c 69 6e 67 20 the new sibling
18118 70 61 67 65 2e 20 2a 2f 0a 20 20 20 20 4d 65 6d page. */. Mem
18119 50 61 67 65 20 2a 70 4e 65 77 20 3d 20 61 70 4e Page *pNew = apN
1811a 65 77 5b 69 5d 3b 0a 20 20 20 20 61 73 73 65 72 ew[i];. asser
1811b 74 28 20 6a 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 t( j<nMaxCells )
1811c 3b 0a 20 20 20 20 7a 65 72 6f 50 61 67 65 28 70 ;. zeroPage(p
1811d 4e 65 77 2c 20 70 61 67 65 46 6c 61 67 73 29 3b New, pageFlags);
1811e 0a 20 20 20 20 61 73 73 65 6d 62 6c 65 50 61 67 . assemblePag
1811f 65 28 70 4e 65 77 2c 20 63 6e 74 4e 65 77 5b 69 e(pNew, cntNew[i
18120 5d 2d 6a 2c 20 26 61 70 43 65 6c 6c 5b 6a 5d 2c ]-j, &apCell[j],
18121 20 26 73 7a 43 65 6c 6c 5b 6a 5d 29 3b 0a 20 20 &szCell[j]);.
18122 20 20 61 73 73 65 72 74 28 20 70 4e 65 77 2d 3e assert( pNew->
18123 6e 43 65 6c 6c 3e 30 20 7c 7c 20 28 6e 4e 65 77 nCell>0 || (nNew
18124 3d 3d 31 20 26 26 20 63 6e 74 4e 65 77 5b 30 5d ==1 && cntNew[0]
18125 3d 3d 30 29 20 29 3b 0a 20 20 20 20 61 73 73 65 ==0) );. asse
18126 72 74 28 20 70 4e 65 77 2d 3e 6e 4f 76 65 72 66 rt( pNew->nOverf
18127 6c 6f 77 3d 3d 30 20 29 3b 0a 0a 20 20 20 20 6a low==0 );.. j
18128 20 3d 20 63 6e 74 4e 65 77 5b 69 5d 3b 0a 0a 20 = cntNew[i];..
18129 20 20 20 2f 2a 20 49 66 20 74 68 65 20 73 69 62 /* If the sib
1812a 6c 69 6e 67 20 70 61 67 65 20 61 73 73 65 6d 62 ling page assemb
1812b 6c 65 64 20 61 62 6f 76 65 20 77 61 73 20 6e 6f led above was no
1812c 74 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 t the right-most
1812d 20 73 69 62 6c 69 6e 67 2c 0a 20 20 20 20 2a 2a sibling,. **
1812e 20 69 6e 73 65 72 74 20 61 20 64 69 76 69 64 65 insert a divide
1812f 72 20 63 65 6c 6c 20 69 6e 74 6f 20 74 68 65 20 r cell into the
18130 70 61 72 65 6e 74 20 70 61 67 65 2e 0a 20 20 20 parent page..
18131 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 */. assert(
18132 69 3c 6e 4e 65 77 2d 31 20 7c 7c 20 6a 3d 3d 6e i<nNew-1 || j==n
18133 43 65 6c 6c 20 29 3b 0a 20 20 20 20 69 66 28 20 Cell );. if(
18134 6a 3c 6e 43 65 6c 6c 20 29 7b 0a 20 20 20 20 20 j<nCell ){.
18135 20 75 38 20 2a 70 43 65 6c 6c 3b 0a 20 20 20 20 u8 *pCell;.
18136 20 20 75 38 20 2a 70 54 65 6d 70 3b 0a 20 20 20 u8 *pTemp;.
18137 20 20 20 69 6e 74 20 73 7a 3b 0a 0a 20 20 20 20 int sz;..
18138 20 20 61 73 73 65 72 74 28 20 6a 3c 6e 4d 61 78 assert( j<nMax
18139 43 65 6c 6c 73 20 29 3b 0a 20 20 20 20 20 20 70 Cells );. p
1813a 43 65 6c 6c 20 3d 20 61 70 43 65 6c 6c 5b 6a 5d Cell = apCell[j]
1813b 3b 0a 20 20 20 20 20 20 73 7a 20 3d 20 73 7a 43 ;. sz = szC
1813c 65 6c 6c 5b 6a 5d 20 2b 20 6c 65 61 66 43 6f 72 ell[j] + leafCor
1813d 72 65 63 74 69 6f 6e 3b 0a 20 20 20 20 20 20 70 rection;. p
1813e 54 65 6d 70 20 3d 20 26 61 4f 76 66 6c 53 70 61 Temp = &aOvflSpa
1813f 63 65 5b 69 4f 76 66 6c 53 70 61 63 65 5d 3b 0a ce[iOvflSpace];.
18140 20 20 20 20 20 20 69 66 28 20 21 70 4e 65 77 2d if( !pNew-
18141 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20 20 >leaf ){.
18142 20 6d 65 6d 63 70 79 28 26 70 4e 65 77 2d 3e 61 memcpy(&pNew->a
18143 44 61 74 61 5b 38 5d 2c 20 70 43 65 6c 6c 2c 20 Data[8], pCell,
18144 34 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 4);. }else
18145 69 66 28 20 6c 65 61 66 44 61 74 61 20 29 7b 0a if( leafData ){.
18146 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 /* If th
18147 65 20 74 72 65 65 20 69 73 20 61 20 6c 65 61 66 e tree is a leaf
18148 2d 64 61 74 61 20 74 72 65 65 2c 20 61 6e 64 20 -data tree, and
18149 74 68 65 20 73 69 62 6c 69 6e 67 73 20 61 72 65 the siblings are
1814a 20 6c 65 61 76 65 73 2c 20 0a 20 20 20 20 20 20 leaves, .
1814b 20 20 2a 2a 20 74 68 65 6e 20 74 68 65 72 65 20 ** then there
1814c 69 73 20 6e 6f 20 64 69 76 69 64 65 72 20 63 65 is no divider ce
1814d 6c 6c 20 69 6e 20 61 70 43 65 6c 6c 5b 5d 2e 20 ll in apCell[].
1814e 49 6e 73 74 65 61 64 2c 20 74 68 65 20 64 69 76 Instead, the div
1814f 69 64 65 72 20 0a 20 20 20 20 20 20 20 20 2a 2a ider . **
18150 20 63 65 6c 6c 20 63 6f 6e 73 69 73 74 73 20 6f cell consists o
18151 66 20 74 68 65 20 69 6e 74 65 67 65 72 20 6b 65 f the integer ke
18152 79 20 66 6f 72 20 74 68 65 20 72 69 67 68 74 2d y for the right-
18153 6d 6f 73 74 20 63 65 6c 6c 20 6f 66 20 0a 20 20 most cell of .
18154 20 20 20 20 20 20 2a 2a 20 74 68 65 20 73 69 62 ** the sib
18155 6c 69 6e 67 2d 70 61 67 65 20 61 73 73 65 6d 62 ling-page assemb
18156 6c 65 64 20 61 62 6f 76 65 20 6f 6e 6c 79 2e 0a led above only..
18157 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 */.
18158 20 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f CellInfo info
18159 3b 0a 20 20 20 20 20 20 20 20 6a 2d 2d 3b 0a 20 ;. j--;.
1815a 20 20 20 20 20 20 20 62 74 72 65 65 50 61 72 73 btreePars
1815b 65 43 65 6c 6c 50 74 72 28 70 4e 65 77 2c 20 61 eCellPtr(pNew, a
1815c 70 43 65 6c 6c 5b 6a 5d 2c 20 26 69 6e 66 6f 29 pCell[j], &info)
1815d 3b 0a 20 20 20 20 20 20 20 20 70 43 65 6c 6c 20 ;. pCell
1815e 3d 20 70 54 65 6d 70 3b 0a 20 20 20 20 20 20 20 = pTemp;.
1815f 20 73 7a 20 3d 20 34 20 2b 20 70 75 74 56 61 72 sz = 4 + putVar
18160 69 6e 74 28 26 70 43 65 6c 6c 5b 34 5d 2c 20 69 int(&pCell[4], i
18161 6e 66 6f 2e 6e 4b 65 79 29 3b 0a 20 20 20 20 20 nfo.nKey);.
18162 20 20 20 70 54 65 6d 70 20 3d 20 30 3b 0a 20 20 pTemp = 0;.
18163 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
18164 20 20 20 70 43 65 6c 6c 20 2d 3d 20 34 3b 0a 20 pCell -= 4;.
18165 20 20 20 20 20 20 20 2f 2a 20 4f 62 73 63 75 72 /* Obscur
18166 65 20 63 61 73 65 20 66 6f 72 20 6e 6f 6e 2d 6c e case for non-l
18167 65 61 66 2d 64 61 74 61 20 74 72 65 65 73 3a 20 eaf-data trees:
18168 49 66 20 74 68 65 20 63 65 6c 6c 20 61 74 20 70 If the cell at p
18169 43 65 6c 6c 20 77 61 73 0a 20 20 20 20 20 20 20 Cell was.
1816a 20 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 73 ** previously s
1816b 74 6f 72 65 64 20 6f 6e 20 61 20 6c 65 61 66 20 tored on a leaf
1816c 6e 6f 64 65 2c 20 61 6e 64 20 69 74 73 20 72 65 node, and its re
1816d 70 6f 72 74 65 64 20 73 69 7a 65 20 77 61 73 20 ported size was
1816e 34 0a 20 20 20 20 20 20 20 20 2a 2a 20 62 79 74 4. ** byt
1816f 65 73 2c 20 74 68 65 6e 20 69 74 20 6d 61 79 20 es, then it may
18170 61 63 74 75 61 6c 6c 79 20 62 65 20 73 6d 61 6c actually be smal
18171 6c 65 72 20 74 68 61 6e 20 74 68 69 73 20 0a 20 ler than this .
18172 20 20 20 20 20 20 20 2a 2a 20 28 73 65 65 20 62 ** (see b
18173 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 treeParseCellPtr
18174 28 29 2c 20 34 20 62 79 74 65 73 20 69 73 20 74 (), 4 bytes is t
18175 68 65 20 6d 69 6e 69 6d 75 6d 20 73 69 7a 65 20 he minimum size
18176 6f 66 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 6e of. ** an
18177 79 20 63 65 6c 6c 29 2e 20 42 75 74 20 69 74 20 y cell). But it
18178 69 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 is important to
18179 70 61 73 73 20 74 68 65 20 63 6f 72 72 65 63 74 pass the correct
1817a 20 73 69 7a 65 20 74 6f 20 0a 20 20 20 20 20 20 size to .
1817b 20 20 2a 2a 20 69 6e 73 65 72 74 43 65 6c 6c 28 ** insertCell(
1817c 29 2c 20 73 6f 20 72 65 70 61 72 73 65 20 74 68 ), so reparse th
1817d 65 20 63 65 6c 6c 20 6e 6f 77 2e 0a 20 20 20 20 e cell now..
1817e 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a **. *
1817f 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68 69 73 * Note that this
18180 20 63 61 6e 20 6e 65 76 65 72 20 68 61 70 70 65 can never happe
18181 6e 20 69 6e 20 61 6e 20 53 51 4c 69 74 65 20 64 n in an SQLite d
18182 61 74 61 20 66 69 6c 65 2c 20 61 73 20 61 6c 6c ata file, as all
18183 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 65 6c 6c . ** cell
18184 73 20 61 72 65 20 61 74 20 6c 65 61 73 74 20 34 s are at least 4
18185 20 62 79 74 65 73 2e 20 49 74 20 6f 6e 6c 79 20 bytes. It only
18186 68 61 70 70 65 6e 73 20 69 6e 20 62 2d 74 72 65 happens in b-tre
18187 65 73 20 75 73 65 64 0a 20 20 20 20 20 20 20 20 es used.
18188 2a 2a 20 74 6f 20 65 76 61 6c 75 61 74 65 20 22 ** to evaluate "
18189 49 4e 20 28 53 45 4c 45 43 54 20 2e 2e 2e 29 22 IN (SELECT ...)"
1818a 20 61 6e 64 20 73 69 6d 69 6c 61 72 20 63 6c 61 and similar cla
1818b 75 73 65 73 2e 0a 20 20 20 20 20 20 20 20 2a 2f uses.. */
1818c 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 7a 43 . if( szC
1818d 65 6c 6c 5b 6a 5d 3d 3d 34 20 29 7b 0a 20 20 20 ell[j]==4 ){.
1818e 20 20 20 20 20 20 20 61 73 73 65 72 74 28 6c 65 assert(le
1818f 61 66 43 6f 72 72 65 63 74 69 6f 6e 3d 3d 34 29 afCorrection==4)
18190 3b 0a 20 20 20 20 20 20 20 20 20 20 73 7a 20 3d ;. sz =
18191 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 cellSizePtr(pPa
18192 72 65 6e 74 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 rent, pCell);.
18193 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
18194 20 20 20 20 20 20 69 4f 76 66 6c 53 70 61 63 65 iOvflSpace
18195 20 2b 3d 20 73 7a 3b 0a 20 20 20 20 20 20 61 73 += sz;. as
18196 73 65 72 74 28 20 73 7a 3c 3d 70 42 74 2d 3e 70 sert( sz<=pBt->p
18197 61 67 65 53 69 7a 65 2f 34 20 29 3b 0a 20 20 20 ageSize/4 );.
18198 20 20 20 61 73 73 65 72 74 28 20 69 4f 76 66 6c assert( iOvfl
18199 53 70 61 63 65 3c 3d 70 42 74 2d 3e 70 61 67 65 Space<=pBt->page
1819a 53 69 7a 65 20 29 3b 0a 20 20 20 20 20 20 69 6e Size );. in
1819b 73 65 72 74 43 65 6c 6c 28 70 50 61 72 65 6e 74 sertCell(pParent
1819c 2c 20 6e 78 44 69 76 2c 20 70 43 65 6c 6c 2c 20 , nxDiv, pCell,
1819d 73 7a 2c 20 70 54 65 6d 70 2c 20 70 4e 65 77 2d sz, pTemp, pNew-
1819e 3e 70 67 6e 6f 2c 20 26 72 63 29 3b 0a 20 20 20 >pgno, &rc);.
1819f 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
181a0 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 62 61 6c 61 E_OK ) goto bala
181a1 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 nce_cleanup;.
181a2 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 assert( sqlit
181a3 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 e3PagerIswriteab
181a4 6c 65 28 70 50 61 72 65 6e 74 2d 3e 70 44 62 50 le(pParent->pDbP
181a5 61 67 65 29 20 29 3b 0a 0a 20 20 20 20 20 20 6a age) );.. j
181a6 2b 2b 3b 0a 20 20 20 20 20 20 6e 78 44 69 76 2b ++;. nxDiv+
181a7 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61 +;. }. }. a
181a8 73 73 65 72 74 28 20 6a 3d 3d 6e 43 65 6c 6c 20 ssert( j==nCell
181a9 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 4f 6c );. assert( nOl
181aa 64 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 d>0 );. assert(
181ab 20 6e 4e 65 77 3e 30 20 29 3b 0a 20 20 69 66 28 nNew>0 );. if(
181ac 20 28 70 61 67 65 46 6c 61 67 73 20 26 20 50 54 (pageFlags & PT
181ad 46 5f 4c 45 41 46 29 3d 3d 30 20 29 7b 0a 20 20 F_LEAF)==0 ){.
181ae 20 20 75 38 20 2a 7a 43 68 69 6c 64 20 3d 20 26 u8 *zChild = &
181af 61 70 43 6f 70 79 5b 6e 4f 6c 64 2d 31 5d 2d 3e apCopy[nOld-1]->
181b0 61 44 61 74 61 5b 38 5d 3b 0a 20 20 20 20 6d 65 aData[8];. me
181b1 6d 63 70 79 28 26 61 70 4e 65 77 5b 6e 4e 65 77 mcpy(&apNew[nNew
181b2 2d 31 5d 2d 3e 61 44 61 74 61 5b 38 5d 2c 20 7a -1]->aData[8], z
181b3 43 68 69 6c 64 2c 20 34 29 3b 0a 20 20 7d 0a 0a Child, 4);. }..
181b4 20 20 69 66 28 20 69 73 52 6f 6f 74 20 26 26 20 if( isRoot &&
181b5 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 3d 3d pParent->nCell==
181b6 30 20 26 26 20 70 50 61 72 65 6e 74 2d 3e 68 64 0 && pParent->hd
181b7 72 4f 66 66 73 65 74 3c 3d 61 70 4e 65 77 5b 30 rOffset<=apNew[0
181b8 5d 2d 3e 6e 46 72 65 65 20 29 7b 0a 20 20 20 20 ]->nFree ){.
181b9 2f 2a 20 54 68 65 20 72 6f 6f 74 20 70 61 67 65 /* The root page
181ba 20 6f 66 20 74 68 65 20 62 2d 74 72 65 65 20 6e of the b-tree n
181bb 6f 77 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 20 63 ow contains no c
181bc 65 6c 6c 73 2e 20 54 68 65 20 6f 6e 6c 79 20 73 ells. The only s
181bd 69 62 6c 69 6e 67 0a 20 20 20 20 2a 2a 20 70 61 ibling. ** pa
181be 67 65 20 69 73 20 74 68 65 20 72 69 67 68 74 2d ge is the right-
181bf 63 68 69 6c 64 20 6f 66 20 74 68 65 20 70 61 72 child of the par
181c0 65 6e 74 2e 20 43 6f 70 79 20 74 68 65 20 63 6f ent. Copy the co
181c1 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 0a 20 20 ntents of the.
181c2 20 20 2a 2a 20 63 68 69 6c 64 20 70 61 67 65 20 ** child page
181c3 69 6e 74 6f 20 74 68 65 20 70 61 72 65 6e 74 2c into the parent,
181c4 20 64 65 63 72 65 61 73 69 6e 67 20 74 68 65 20 decreasing the
181c5 6f 76 65 72 61 6c 6c 20 68 65 69 67 68 74 20 6f overall height o
181c6 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 62 2d 74 f the. ** b-t
181c7 72 65 65 20 73 74 72 75 63 74 75 72 65 20 62 79 ree structure by
181c8 20 6f 6e 65 2e 20 54 68 69 73 20 69 73 20 64 65 one. This is de
181c9 73 63 72 69 62 65 64 20 61 73 20 74 68 65 20 22 scribed as the "
181ca 62 61 6c 61 6e 63 65 2d 73 68 61 6c 6c 6f 77 65 balance-shallowe
181cb 72 22 0a 20 20 20 20 2a 2a 20 73 75 62 2d 61 6c r". ** sub-al
181cc 67 6f 72 69 74 68 6d 20 69 6e 20 73 6f 6d 65 20 gorithm in some
181cd 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 2e 0a 20 documentation..
181ce 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 20 **. ** If
181cf 74 68 69 73 20 69 73 20 61 6e 20 61 75 74 6f 2d this is an auto-
181d0 76 61 63 75 75 6d 20 64 61 74 61 62 61 73 65 2c vacuum database,
181d1 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 63 6f 70 the call to cop
181d2 79 4e 6f 64 65 43 6f 6e 74 65 6e 74 28 29 20 0a yNodeContent() .
181d3 20 20 20 20 2a 2a 20 73 65 74 73 20 61 6c 6c 20 ** sets all
181d4 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 pointer-map entr
181d5 69 65 73 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e ies correspondin
181d6 67 20 74 6f 20 64 61 74 61 62 61 73 65 20 69 6d g to database im
181d7 61 67 65 20 70 61 67 65 73 20 0a 20 20 20 20 2a age pages . *
181d8 2a 20 66 6f 72 20 77 68 69 63 68 20 74 68 65 20 * for which the
181d9 70 6f 69 6e 74 65 72 20 69 73 20 73 74 6f 72 65 pointer is store
181da 64 20 77 69 74 68 69 6e 20 74 68 65 20 63 6f 6e d within the con
181db 74 65 6e 74 20 62 65 69 6e 67 20 63 6f 70 69 65 tent being copie
181dc 64 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a d.. **. **
181dd 20 54 68 65 20 73 65 63 6f 6e 64 20 61 73 73 65 The second asse
181de 72 74 20 62 65 6c 6f 77 20 76 65 72 69 66 69 65 rt below verifie
181df 73 20 74 68 61 74 20 74 68 65 20 63 68 69 6c 64 s that the child
181e0 20 70 61 67 65 20 69 73 20 64 65 66 72 61 67 6d page is defragm
181e1 65 6e 74 65 64 0a 20 20 20 20 2a 2a 20 28 69 74 ented. ** (it
181e2 20 6d 75 73 74 20 62 65 2c 20 61 73 20 69 74 20 must be, as it
181e3 77 61 73 20 6a 75 73 74 20 72 65 63 6f 6e 73 74 was just reconst
181e4 72 75 63 74 65 64 20 75 73 69 6e 67 20 61 73 73 ructed using ass
181e5 65 6d 62 6c 65 50 61 67 65 28 29 29 2e 20 54 68 emblePage()). Th
181e6 69 73 0a 20 20 20 20 2a 2a 20 69 73 20 69 6d 70 is. ** is imp
181e7 6f 72 74 61 6e 74 20 69 66 20 74 68 65 20 70 61 ortant if the pa
181e8 72 65 6e 74 20 70 61 67 65 20 68 61 70 70 65 6e rent page happen
181e9 73 20 74 6f 20 62 65 20 70 61 67 65 20 31 20 6f s to be page 1 o
181ea 66 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 20 f the database.
181eb 20 20 20 2a 2a 20 69 6d 61 67 65 2e 20 20 2a 2f ** image. */
181ec 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 4e 65 . assert( nNe
181ed 77 3d 3d 31 20 29 3b 0a 20 20 20 20 61 73 73 65 w==1 );. asse
181ee 72 74 28 20 61 70 4e 65 77 5b 30 5d 2d 3e 6e 46 rt( apNew[0]->nF
181ef 72 65 65 20 3d 3d 20 0a 20 20 20 20 20 20 20 20 ree == .
181f0 28 67 65 74 32 62 79 74 65 28 26 61 70 4e 65 77 (get2byte(&apNew
181f1 5b 30 5d 2d 3e 61 44 61 74 61 5b 35 5d 29 2d 61 [0]->aData[5])-a
181f2 70 4e 65 77 5b 30 5d 2d 3e 63 65 6c 6c 4f 66 66 pNew[0]->cellOff
181f3 73 65 74 2d 61 70 4e 65 77 5b 30 5d 2d 3e 6e 43 set-apNew[0]->nC
181f4 65 6c 6c 2a 32 29 20 0a 20 20 20 20 29 3b 0a 20 ell*2) . );.
181f5 20 20 20 63 6f 70 79 4e 6f 64 65 43 6f 6e 74 65 copyNodeConte
181f6 6e 74 28 61 70 4e 65 77 5b 30 5d 2c 20 70 50 61 nt(apNew[0], pPa
181f7 72 65 6e 74 2c 20 26 72 63 29 3b 0a 20 20 20 20 rent, &rc);.
181f8 66 72 65 65 50 61 67 65 28 61 70 4e 65 77 5b 30 freePage(apNew[0
181f9 5d 2c 20 26 72 63 29 3b 0a 20 20 7d 65 6c 73 65 ], &rc);. }else
181fa 20 69 66 28 20 49 53 41 55 54 4f 56 41 43 55 55 if( ISAUTOVACUU
181fb 4d 20 29 7b 0a 20 20 20 20 2f 2a 20 46 69 78 20 M ){. /* Fix
181fc 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 the pointer-map
181fd 65 6e 74 72 69 65 73 20 66 6f 72 20 61 6c 6c 20 entries for all
181fe 74 68 65 20 63 65 6c 6c 73 20 74 68 61 74 20 77 the cells that w
181ff 65 72 65 20 73 68 69 66 74 65 64 20 61 72 6f 75 ere shifted arou
18200 6e 64 2e 20 0a 20 20 20 20 2a 2a 20 54 68 65 72 nd. . ** Ther
18201 65 20 61 72 65 20 73 65 76 65 72 61 6c 20 64 69 e are several di
18202 66 66 65 72 65 6e 74 20 74 79 70 65 73 20 6f 66 fferent types of
18203 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 pointer-map ent
18204 72 69 65 73 20 74 68 61 74 20 6e 65 65 64 20 74 ries that need t
18205 6f 0a 20 20 20 20 2a 2a 20 62 65 20 64 65 61 6c o. ** be deal
18206 74 20 77 69 74 68 20 62 79 20 74 68 69 73 20 72 t with by this r
18207 6f 75 74 69 6e 65 2e 20 53 6f 6d 65 20 6f 66 20 outine. Some of
18208 74 68 65 73 65 20 68 61 76 65 20 62 65 65 6e 20 these have been
18209 73 65 74 20 61 6c 72 65 61 64 79 2c 20 62 75 74 set already, but
1820a 0a 20 20 20 20 2a 2a 20 6d 61 6e 79 20 68 61 76 . ** many hav
1820b 65 20 6e 6f 74 2e 20 54 68 65 20 66 6f 6c 6c 6f e not. The follo
1820c 77 69 6e 67 20 69 73 20 61 20 73 75 6d 6d 61 72 wing is a summar
1820d 79 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a y:. **. **
1820e 20 20 20 31 29 20 54 68 65 20 65 6e 74 72 69 65 1) The entrie
1820f 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 s associated wit
18210 68 20 6e 65 77 20 73 69 62 6c 69 6e 67 20 70 61 h new sibling pa
18211 67 65 73 20 74 68 61 74 20 77 65 72 65 20 6e 6f ges that were no
18212 74 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 73 69 t. ** si
18213 62 6c 69 6e 67 73 20 77 68 65 6e 20 74 68 69 73 blings when this
18214 20 66 75 6e 63 74 69 6f 6e 20 77 61 73 20 63 61 function was ca
18215 6c 6c 65 64 2e 20 54 68 65 73 65 20 68 61 76 65 lled. These have
18216 20 61 6c 72 65 61 64 79 0a 20 20 20 20 2a 2a 20 already. **
18217 20 20 20 20 20 62 65 65 6e 20 73 65 74 2e 20 57 been set. W
18218 65 20 64 6f 6e 27 74 20 6e 65 65 64 20 74 6f 20 e don't need to
18219 77 6f 72 72 79 20 61 62 6f 75 74 20 6f 6c 64 20 worry about old
1821a 73 69 62 6c 69 6e 67 73 20 74 68 61 74 20 77 65 siblings that we
1821b 72 65 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 6d re. ** m
1821c 6f 76 65 64 20 74 6f 20 74 68 65 20 66 72 65 65 oved to the free
1821d 2d 6c 69 73 74 20 2d 20 74 68 65 20 66 72 65 65 -list - the free
1821e 50 61 67 65 28 29 20 63 6f 64 65 20 68 61 73 20 Page() code has
1821f 74 61 6b 65 6e 20 63 61 72 65 0a 20 20 20 20 2a taken care. *
18220 2a 20 20 20 20 20 20 6f 66 20 74 68 6f 73 65 2e * of those.
18221 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 . **. **
18222 20 32 29 20 54 68 65 20 70 6f 69 6e 74 65 72 2d 2) The pointer-
18223 6d 61 70 20 65 6e 74 72 69 65 73 20 61 73 73 6f map entries asso
18224 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 ciated with the
18225 66 69 72 73 74 20 6f 76 65 72 66 6c 6f 77 0a 20 first overflow.
18226 20 20 20 2a 2a 20 20 20 20 20 20 70 61 67 65 20 ** page
18227 69 6e 20 61 6e 79 20 6f 76 65 72 66 6c 6f 77 20 in any overflow
18228 63 68 61 69 6e 73 20 75 73 65 64 20 62 79 20 6e chains used by n
18229 65 77 20 64 69 76 69 64 65 72 20 63 65 6c 6c 73 ew divider cells
1822a 2e 20 54 68 65 73 65 20 0a 20 20 20 20 2a 2a 20 . These . **
1822b 20 20 20 20 20 68 61 76 65 20 61 6c 73 6f 20 61 have also a
1822c 6c 72 65 61 64 79 20 62 65 65 6e 20 74 61 6b 65 lready been take
1822d 6e 20 63 61 72 65 20 6f 66 20 62 79 20 74 68 65 n care of by the
1822e 20 69 6e 73 65 72 74 43 65 6c 6c 28 29 20 63 6f insertCell() co
1822f 64 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a de.. **. *
18230 2a 20 20 20 33 29 20 49 66 20 74 68 65 20 73 69 * 3) If the si
18231 62 6c 69 6e 67 20 70 61 67 65 73 20 61 72 65 20 bling pages are
18232 6e 6f 74 20 6c 65 61 76 65 73 2c 20 74 68 65 6e not leaves, then
18233 20 74 68 65 20 63 68 69 6c 64 20 70 61 67 65 73 the child pages
18234 20 6f 66 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 of. **
18235 63 65 6c 6c 73 20 73 74 6f 72 65 64 20 6f 6e 20 cells stored on
18236 74 68 65 20 73 69 62 6c 69 6e 67 20 70 61 67 65 the sibling page
18237 73 20 6d 61 79 20 6e 65 65 64 20 74 6f 20 62 65 s may need to be
18238 20 75 70 64 61 74 65 64 2e 0a 20 20 20 20 2a 2a updated.. **
18239 0a 20 20 20 20 2a 2a 20 20 20 34 29 20 49 66 20 . ** 4) If
1823a 74 68 65 20 73 69 62 6c 69 6e 67 20 70 61 67 65 the sibling page
1823b 73 20 61 72 65 20 6e 6f 74 20 69 6e 74 65 72 6e s are not intern
1823c 61 6c 20 69 6e 74 6b 65 79 20 6e 6f 64 65 73 2c al intkey nodes,
1823d 20 74 68 65 6e 20 61 6e 79 0a 20 20 20 20 2a 2a then any. **
1823e 20 20 20 20 20 20 6f 76 65 72 66 6c 6f 77 20 70 overflow p
1823f 61 67 65 73 20 75 73 65 64 20 62 79 20 74 68 65 ages used by the
18240 73 65 20 63 65 6c 6c 73 20 6d 61 79 20 6e 65 65 se cells may nee
18241 64 20 74 6f 20 62 65 20 75 70 64 61 74 65 64 0a d to be updated.
18242 20 20 20 20 2a 2a 20 20 20 20 20 20 28 69 6e 74 ** (int
18243 65 72 6e 61 6c 20 69 6e 74 6b 65 79 20 6e 6f 64 ernal intkey nod
18244 65 73 20 6e 65 76 65 72 20 63 6f 6e 74 61 69 6e es never contain
18245 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 6f 76 65 pointers to ove
18246 72 66 6c 6f 77 20 70 61 67 65 73 29 2e 0a 20 20 rflow pages)..
18247 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 35 29 **. ** 5)
18248 20 49 66 20 74 68 65 20 73 69 62 6c 69 6e 67 20 If the sibling
18249 70 61 67 65 73 20 61 72 65 20 6e 6f 74 20 6c 65 pages are not le
1824a 61 76 65 73 2c 20 74 68 65 6e 20 74 68 65 20 70 aves, then the p
1824b 6f 69 6e 74 65 72 2d 6d 61 70 0a 20 20 20 20 2a ointer-map. *
1824c 2a 20 20 20 20 20 20 65 6e 74 72 69 65 73 20 66 * entries f
1824d 6f 72 20 74 68 65 20 72 69 67 68 74 2d 63 68 69 or the right-chi
1824e 6c 64 20 70 61 67 65 73 20 6f 66 20 65 61 63 68 ld pages of each
1824f 20 73 69 62 6c 69 6e 67 20 6d 61 79 20 6e 65 65 sibling may nee
18250 64 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 74 6f d. ** to
18251 20 62 65 20 75 70 64 61 74 65 64 2e 0a 20 20 20 be updated..
18252 20 2a 2a 0a 20 20 20 20 2a 2a 20 43 61 73 65 73 **. ** Cases
18253 20 31 20 61 6e 64 20 32 20 61 72 65 20 64 65 61 1 and 2 are dea
18254 6c 74 20 77 69 74 68 20 61 62 6f 76 65 20 62 79 lt with above by
18255 20 6f 74 68 65 72 20 63 6f 64 65 2e 20 54 68 65 other code. The
18256 20 6e 65 78 74 0a 20 20 20 20 2a 2a 20 62 6c 6f next. ** blo
18257 63 6b 20 64 65 61 6c 73 20 77 69 74 68 20 63 61 ck deals with ca
18258 73 65 73 20 33 20 61 6e 64 20 34 20 61 6e 64 20 ses 3 and 4 and
18259 74 68 65 20 6f 6e 65 20 61 66 74 65 72 20 74 68 the one after th
1825a 61 74 2c 20 63 61 73 65 20 35 2e 20 53 69 6e 63 at, case 5. Sinc
1825b 65 0a 20 20 20 20 2a 2a 20 73 65 74 74 69 6e 67 e. ** setting
1825c 20 61 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 65 a pointer map e
1825d 6e 74 72 79 20 69 73 20 61 20 72 65 6c 61 74 69 ntry is a relati
1825e 76 65 6c 79 20 65 78 70 65 6e 73 69 76 65 20 6f vely expensive o
1825f 70 65 72 61 74 69 6f 6e 2c 20 74 68 69 73 0a 20 peration, this.
18260 20 20 20 2a 2a 20 63 6f 64 65 20 6f 6e 6c 79 20 ** code only
18261 73 65 74 73 20 70 6f 69 6e 74 65 72 20 6d 61 70 sets pointer map
18262 20 65 6e 74 72 69 65 73 20 66 6f 72 20 63 68 69 entries for chi
18263 6c 64 20 6f 72 20 6f 76 65 72 66 6c 6f 77 20 70 ld or overflow p
18264 61 67 65 73 20 74 68 61 74 20 68 61 76 65 0a 20 ages that have.
18265 20 20 20 2a 2a 20 61 63 74 75 61 6c 6c 79 20 6d ** actually m
18266 6f 76 65 64 20 62 65 74 77 65 65 6e 20 70 61 67 oved between pag
18267 65 73 2e 20 20 2a 2f 0a 20 20 20 20 4d 65 6d 50 es. */. MemP
18268 61 67 65 20 2a 70 4e 65 77 20 3d 20 61 70 4e 65 age *pNew = apNe
18269 77 5b 30 5d 3b 0a 20 20 20 20 4d 65 6d 50 61 67 w[0];. MemPag
1826a 65 20 2a 70 4f 6c 64 20 3d 20 61 70 43 6f 70 79 e *pOld = apCopy
1826b 5b 30 5d 3b 0a 20 20 20 20 69 6e 74 20 6e 4f 76 [0];. int nOv
1826c 65 72 66 6c 6f 77 20 3d 20 70 4f 6c 64 2d 3e 6e erflow = pOld->n
1826d 4f 76 65 72 66 6c 6f 77 3b 0a 20 20 20 20 69 6e Overflow;. in
1826e 74 20 69 4e 65 78 74 4f 6c 64 20 3d 20 70 4f 6c t iNextOld = pOl
1826f 64 2d 3e 6e 43 65 6c 6c 20 2b 20 6e 4f 76 65 72 d->nCell + nOver
18270 66 6c 6f 77 3b 0a 20 20 20 20 69 6e 74 20 69 4f flow;. int iO
18271 76 65 72 66 6c 6f 77 20 3d 20 28 6e 4f 76 65 72 verflow = (nOver
18272 66 6c 6f 77 20 3f 20 70 4f 6c 64 2d 3e 61 4f 76 flow ? pOld->aOv
18273 66 6c 5b 30 5d 2e 69 64 78 20 3a 20 2d 31 29 3b fl[0].idx : -1);
18274 0a 20 20 20 20 6a 20 3d 20 30 3b 20 20 20 20 20 . j = 0;
18275 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18276 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 /* Curre
18277 6e 74 20 27 6f 6c 64 27 20 73 69 62 6c 69 6e 67 nt 'old' sibling
18278 20 70 61 67 65 20 2a 2f 0a 20 20 20 20 6b 20 3d page */. k =
18279 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 0;
1827a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1827b 2f 2a 20 43 75 72 72 65 6e 74 20 27 6e 65 77 27 /* Current 'new'
1827c 20 73 69 62 6c 69 6e 67 20 70 61 67 65 20 2a 2f sibling page */
1827d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c . for(i=0; i<
1827e 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 nCell; i++){.
1827f 20 20 20 69 6e 74 20 69 73 44 69 76 69 64 65 72 int isDivider
18280 20 3d 20 30 3b 0a 20 20 20 20 20 20 77 68 69 6c = 0;. whil
18281 65 28 20 69 3d 3d 69 4e 65 78 74 4f 6c 64 20 29 e( i==iNextOld )
18282 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 43 65 6c {. /* Cel
18283 6c 20 69 20 69 73 20 74 68 65 20 63 65 6c 6c 20 l i is the cell
18284 69 6d 6d 65 64 69 61 74 65 6c 79 20 66 6f 6c 6c immediately foll
18285 6f 77 69 6e 67 20 74 68 65 20 6c 61 73 74 20 63 owing the last c
18286 65 6c 6c 20 6f 6e 20 6f 6c 64 0a 20 20 20 20 20 ell on old.
18287 20 20 20 2a 2a 20 73 69 62 6c 69 6e 67 20 70 61 ** sibling pa
18288 67 65 20 6a 2e 20 49 66 20 74 68 65 20 73 69 62 ge j. If the sib
18289 6c 69 6e 67 73 20 61 72 65 20 6e 6f 74 20 6c 65 lings are not le
1828a 61 66 20 70 61 67 65 73 20 6f 66 20 61 6e 0a 20 af pages of an.
1828b 20 20 20 20 20 20 20 2a 2a 20 69 6e 74 6b 65 79 ** intkey
1828c 20 62 2d 74 72 65 65 2c 20 74 68 65 6e 20 63 65 b-tree, then ce
1828d 6c 6c 20 69 20 77 61 73 20 61 20 64 69 76 69 64 ll i was a divid
1828e 65 72 20 63 65 6c 6c 2e 20 2a 2f 0a 20 20 20 20 er cell. */.
1828f 20 20 20 20 70 4f 6c 64 20 3d 20 61 70 43 6f 70 pOld = apCop
18290 79 5b 2b 2b 6a 5d 3b 0a 20 20 20 20 20 20 20 20 y[++j];.
18291 69 4e 65 78 74 4f 6c 64 20 3d 20 69 20 2b 20 21 iNextOld = i + !
18292 6c 65 61 66 44 61 74 61 20 2b 20 70 4f 6c 64 2d leafData + pOld-
18293 3e 6e 43 65 6c 6c 20 2b 20 70 4f 6c 64 2d 3e 6e >nCell + pOld->n
18294 4f 76 65 72 66 6c 6f 77 3b 0a 20 20 20 20 20 20 Overflow;.
18295 20 20 69 66 28 20 70 4f 6c 64 2d 3e 6e 4f 76 65 if( pOld->nOve
18296 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20 20 20 20 rflow ){.
18297 20 20 20 6e 4f 76 65 72 66 6c 6f 77 20 3d 20 70 nOverflow = p
18298 4f 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3b 0a Old->nOverflow;.
18299 20 20 20 20 20 20 20 20 20 20 69 4f 76 65 72 66 iOverf
1829a 6c 6f 77 20 3d 20 69 20 2b 20 21 6c 65 61 66 44 low = i + !leafD
1829b 61 74 61 20 2b 20 70 4f 6c 64 2d 3e 61 4f 76 66 ata + pOld->aOvf
1829c 6c 5b 30 5d 2e 69 64 78 3b 0a 20 20 20 20 20 20 l[0].idx;.
1829d 20 20 7d 0a 20 20 20 20 20 20 20 20 69 73 44 69 }. isDi
1829e 76 69 64 65 72 20 3d 20 21 6c 65 61 66 44 61 74 vider = !leafDat
1829f 61 3b 20 20 0a 20 20 20 20 20 20 7d 0a 0a 20 20 a; . }..
182a0 20 20 20 20 61 73 73 65 72 74 28 6e 4f 76 65 72 assert(nOver
182a1 66 6c 6f 77 3e 30 20 7c 7c 20 69 4f 76 65 72 66 flow>0 || iOverf
182a2 6c 6f 77 3c 69 20 29 3b 0a 20 20 20 20 20 20 61 low<i );. a
182a3 73 73 65 72 74 28 6e 4f 76 65 72 66 6c 6f 77 3c ssert(nOverflow<
182a4 32 20 7c 7c 20 70 4f 6c 64 2d 3e 61 4f 76 66 6c 2 || pOld->aOvfl
182a5 5b 30 5d 2e 69 64 78 3d 3d 70 4f 6c 64 2d 3e 61 [0].idx==pOld->a
182a6 4f 76 66 6c 5b 31 5d 2e 69 64 78 2d 31 29 3b 0a Ovfl[1].idx-1);.
182a7 20 20 20 20 20 20 61 73 73 65 72 74 28 6e 4f 76 assert(nOv
182a8 65 72 66 6c 6f 77 3c 33 20 7c 7c 20 70 4f 6c 64 erflow<3 || pOld
182a9 2d 3e 61 4f 76 66 6c 5b 31 5d 2e 69 64 78 3d 3d ->aOvfl[1].idx==
182aa 70 4f 6c 64 2d 3e 61 4f 76 66 6c 5b 32 5d 2e 69 pOld->aOvfl[2].i
182ab 64 78 2d 31 29 3b 0a 20 20 20 20 20 20 69 66 28 dx-1);. if(
182ac 20 69 3d 3d 69 4f 76 65 72 66 6c 6f 77 20 29 7b i==iOverflow ){
182ad 0a 20 20 20 20 20 20 20 20 69 73 44 69 76 69 64 . isDivid
182ae 65 72 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 er = 1;.
182af 69 66 28 20 28 2d 2d 6e 4f 76 65 72 66 6c 6f 77 if( (--nOverflow
182b0 29 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 )>0 ){.
182b1 20 69 4f 76 65 72 66 6c 6f 77 2b 2b 3b 0a 20 20 iOverflow++;.
182b2 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
182b3 0a 20 20 20 20 20 20 69 66 28 20 69 3d 3d 63 6e . if( i==cn
182b4 74 4e 65 77 5b 6b 5d 20 29 7b 0a 20 20 20 20 20 tNew[k] ){.
182b5 20 20 20 2f 2a 20 43 65 6c 6c 20 69 20 69 73 20 /* Cell i is
182b6 74 68 65 20 63 65 6c 6c 20 69 6d 6d 65 64 69 61 the cell immedia
182b7 74 65 6c 79 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 tely following t
182b8 68 65 20 6c 61 73 74 20 63 65 6c 6c 20 6f 6e 20 he last cell on
182b9 6e 65 77 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 new. ** s
182ba 69 62 6c 69 6e 67 20 70 61 67 65 20 6b 2e 20 49 ibling page k. I
182bb 66 20 74 68 65 20 73 69 62 6c 69 6e 67 73 20 61 f the siblings a
182bc 72 65 20 6e 6f 74 20 6c 65 61 66 20 70 61 67 65 re not leaf page
182bd 73 20 6f 66 20 61 6e 0a 20 20 20 20 20 20 20 20 s of an.
182be 2a 2a 20 69 6e 74 6b 65 79 20 62 2d 74 72 65 65 ** intkey b-tree
182bf 2c 20 74 68 65 6e 20 63 65 6c 6c 20 69 20 69 73 , then cell i is
182c0 20 61 20 64 69 76 69 64 65 72 20 63 65 6c 6c 2e a divider cell.
182c1 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 4e 65 */. pNe
182c2 77 20 3d 20 61 70 4e 65 77 5b 2b 2b 6b 5d 3b 0a w = apNew[++k];.
182c3 20 20 20 20 20 20 20 20 69 66 28 20 21 6c 65 61 if( !lea
182c4 66 44 61 74 61 20 29 20 63 6f 6e 74 69 6e 75 65 fData ) continue
182c5 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
182c6 61 73 73 65 72 74 28 20 6a 3c 6e 4f 6c 64 20 29 assert( j<nOld )
182c7 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
182c8 6b 3c 6e 4e 65 77 20 29 3b 0a 0a 20 20 20 20 20 k<nNew );..
182c9 20 2f 2a 20 49 66 20 74 68 65 20 63 65 6c 6c 20 /* If the cell
182ca 77 61 73 20 6f 72 69 67 69 6e 61 6c 6c 79 20 64 was originally d
182cb 69 76 69 64 65 72 20 63 65 6c 6c 20 28 61 6e 64 ivider cell (and
182cc 20 69 73 20 6e 6f 74 20 6e 6f 77 29 20 6f 72 0a is not now) or.
182cd 20 20 20 20 20 20 2a 2a 20 61 6e 20 6f 76 65 72 ** an over
182ce 66 6c 6f 77 20 63 65 6c 6c 2c 20 6f 72 20 69 66 flow cell, or if
182cf 20 74 68 65 20 63 65 6c 6c 20 77 61 73 20 6c 6f the cell was lo
182d0 63 61 74 65 64 20 6f 6e 20 61 20 64 69 66 66 65 cated on a diffe
182d1 72 65 6e 74 20 73 69 62 6c 69 6e 67 0a 20 20 20 rent sibling.
182d2 20 20 20 2a 2a 20 70 61 67 65 20 62 65 66 6f 72 ** page befor
182d3 65 20 74 68 65 20 62 61 6c 61 6e 63 69 6e 67 2c e the balancing,
182d4 20 74 68 65 6e 20 74 68 65 20 70 6f 69 6e 74 65 then the pointe
182d5 72 20 6d 61 70 20 65 6e 74 72 69 65 73 20 61 73 r map entries as
182d6 73 6f 63 69 61 74 65 64 0a 20 20 20 20 20 20 2a sociated. *
182d7 2a 20 77 69 74 68 20 61 6e 79 20 63 68 69 6c 64 * with any child
182d8 20 6f 72 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 or overflow pag
182d9 65 73 20 6e 65 65 64 20 74 6f 20 62 65 20 75 70 es need to be up
182da 64 61 74 65 64 2e 20 20 2a 2f 0a 20 20 20 20 20 dated. */.
182db 20 69 66 28 20 69 73 44 69 76 69 64 65 72 20 7c if( isDivider |
182dc 7c 20 70 4f 6c 64 2d 3e 70 67 6e 6f 21 3d 70 4e | pOld->pgno!=pN
182dd 65 77 2d 3e 70 67 6e 6f 20 29 7b 0a 20 20 20 20 ew->pgno ){.
182de 20 20 20 20 69 66 28 20 21 6c 65 61 66 43 6f 72 if( !leafCor
182df 72 65 63 74 69 6f 6e 20 29 7b 0a 20 20 20 20 20 rection ){.
182e0 20 20 20 20 20 70 74 72 6d 61 70 50 75 74 28 70 ptrmapPut(p
182e1 42 74 2c 20 67 65 74 34 62 79 74 65 28 61 70 43 Bt, get4byte(apC
182e2 65 6c 6c 5b 69 5d 29 2c 20 50 54 52 4d 41 50 5f ell[i]), PTRMAP_
182e3 42 54 52 45 45 2c 20 70 4e 65 77 2d 3e 70 67 6e BTREE, pNew->pgn
182e4 6f 2c 20 26 72 63 29 3b 0a 20 20 20 20 20 20 20 o, &rc);.
182e5 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 }. if( s
182e6 7a 43 65 6c 6c 5b 69 5d 3e 70 4e 65 77 2d 3e 6d zCell[i]>pNew->m
182e7 69 6e 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 20 inLocal ){.
182e8 20 20 20 20 20 70 74 72 6d 61 70 50 75 74 4f 76 ptrmapPutOv
182e9 66 6c 50 74 72 28 70 4e 65 77 2c 20 61 70 43 65 flPtr(pNew, apCe
182ea 6c 6c 5b 69 5d 2c 20 26 72 63 29 3b 0a 20 20 20 ll[i], &rc);.
182eb 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 }. }.
182ec 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 21 6c }.. if( !l
182ed 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 20 29 7b eafCorrection ){
182ee 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 . for(i=0;
182ef 69 3c 6e 4e 65 77 3b 20 69 2b 2b 29 7b 0a 20 20 i<nNew; i++){.
182f0 20 20 20 20 20 20 75 33 32 20 6b 65 79 20 3d 20 u32 key =
182f1 67 65 74 34 62 79 74 65 28 26 61 70 4e 65 77 5b get4byte(&apNew[
182f2 69 5d 2d 3e 61 44 61 74 61 5b 38 5d 29 3b 0a 20 i]->aData[8]);.
182f3 20 20 20 20 20 20 20 70 74 72 6d 61 70 50 75 74 ptrmapPut
182f4 28 70 42 74 2c 20 6b 65 79 2c 20 50 54 52 4d 41 (pBt, key, PTRMA
182f5 50 5f 42 54 52 45 45 2c 20 61 70 4e 65 77 5b 69 P_BTREE, apNew[i
182f6 5d 2d 3e 70 67 6e 6f 2c 20 26 72 63 29 3b 0a 20 ]->pgno, &rc);.
182f7 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 23 69 }. }..#i
182f8 66 20 30 0a 20 20 20 20 2f 2a 20 54 68 65 20 70 f 0. /* The p
182f9 74 72 6d 61 70 43 68 65 63 6b 50 61 67 65 73 28 trmapCheckPages(
182fa 29 20 63 6f 6e 74 61 69 6e 73 20 61 73 73 65 72 ) contains asser
182fb 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 74 t() statements t
182fc 68 61 74 20 76 65 72 69 66 79 20 74 68 61 74 0a hat verify that.
182fd 20 20 20 20 2a 2a 20 61 6c 6c 20 70 6f 69 6e 74 ** all point
182fe 65 72 20 6d 61 70 20 70 61 67 65 73 20 61 72 65 er map pages are
182ff 20 73 65 74 20 63 6f 72 72 65 63 74 6c 79 2e 20 set correctly.
18300 54 68 69 73 20 69 73 20 68 65 6c 70 66 75 6c 20 This is helpful
18301 77 68 69 6c 65 20 0a 20 20 20 20 2a 2a 20 64 65 while . ** de
18302 62 75 67 67 69 6e 67 2e 20 54 68 69 73 20 69 73 bugging. This is
18303 20 75 73 75 61 6c 6c 79 20 64 69 73 61 62 6c 65 usually disable
18304 64 20 62 65 63 61 75 73 65 20 61 20 63 6f 72 72 d because a corr
18305 75 70 74 20 64 61 74 61 62 61 73 65 20 6d 61 79 upt database may
18306 0a 20 20 20 20 2a 2a 20 63 61 75 73 65 20 61 6e . ** cause an
18307 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d assert() statem
18308 65 6e 74 20 74 6f 20 66 61 69 6c 2e 20 20 2a 2f ent to fail. */
18309 0a 20 20 20 20 70 74 72 6d 61 70 43 68 65 63 6b . ptrmapCheck
1830a 50 61 67 65 73 28 61 70 4e 65 77 2c 20 6e 4e 65 Pages(apNew, nNe
1830b 77 29 3b 0a 20 20 20 20 70 74 72 6d 61 70 43 68 w);. ptrmapCh
1830c 65 63 6b 50 61 67 65 73 28 26 70 50 61 72 65 6e eckPages(&pParen
1830d 74 2c 20 31 29 3b 0a 23 65 6e 64 69 66 0a 20 20 t, 1);.#endif.
1830e 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 }.. assert( pPa
1830f 72 65 6e 74 2d 3e 69 73 49 6e 69 74 20 29 3b 0a rent->isInit );.
18310 20 20 54 52 41 43 45 28 28 22 42 41 4c 41 4e 43 TRACE(("BALANC
18311 45 3a 20 66 69 6e 69 73 68 65 64 3a 20 6f 6c 64 E: finished: old
18312 3d 25 64 20 6e 65 77 3d 25 64 20 63 65 6c 6c 73 =%d new=%d cells
18313 3d 25 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 =%d\n",.
18314 20 20 6e 4f 6c 64 2c 20 6e 4e 65 77 2c 20 6e 43 nOld, nNew, nC
18315 65 6c 6c 29 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a ell));.. /*. *
18316 2a 20 43 6c 65 61 6e 75 70 20 62 65 66 6f 72 65 * Cleanup before
18317 20 72 65 74 75 72 6e 69 6e 67 2e 0a 20 20 2a 2f returning.. */
18318 0a 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 .balance_cleanup
18319 3a 0a 20 20 73 71 6c 69 74 65 33 53 63 72 61 74 :. sqlite3Scrat
1831a 63 68 46 72 65 65 28 61 70 43 65 6c 6c 29 3b 0a chFree(apCell);.
1831b 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4f 6c for(i=0; i<nOl
1831c 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 72 65 6c d; i++){. rel
1831d 65 61 73 65 50 61 67 65 28 61 70 4f 6c 64 5b 69 easePage(apOld[i
1831e 5d 29 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d ]);. }. for(i=
1831f 30 3b 20 69 3c 6e 4e 65 77 3b 20 69 2b 2b 29 7b 0; i<nNew; i++){
18320 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 . releasePage
18321 28 61 70 4e 65 77 5b 69 5d 29 3b 0a 20 20 7d 0a (apNew[i]);. }.
18322 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
18323 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e ../*.** This fun
18324 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 ction is called
18325 77 68 65 6e 20 74 68 65 20 72 6f 6f 74 20 70 61 when the root pa
18326 67 65 20 6f 66 20 61 20 62 2d 74 72 65 65 20 73 ge of a b-tree s
18327 74 72 75 63 74 75 72 65 20 69 73 0a 2a 2a 20 6f tructure is.** o
18328 76 65 72 66 75 6c 6c 20 28 68 61 73 20 6f 6e 65 verfull (has one
18329 20 6f 72 20 6d 6f 72 65 20 6f 76 65 72 66 6c 6f or more overflo
1832a 77 20 70 61 67 65 73 29 2e 0a 2a 2a 0a 2a 2a 20 w pages)..**.**
1832b 41 20 6e 65 77 20 63 68 69 6c 64 20 70 61 67 65 A new child page
1832c 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e is allocated an
1832d 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f d the contents o
1832e 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f f the current ro
1832f 6f 74 0a 2a 2a 20 70 61 67 65 2c 20 69 6e 63 6c ot.** page, incl
18330 75 64 69 6e 67 20 6f 76 65 72 66 6c 6f 77 20 63 uding overflow c
18331 65 6c 6c 73 2c 20 61 72 65 20 63 6f 70 69 65 64 ells, are copied
18332 20 69 6e 74 6f 20 74 68 65 20 63 68 69 6c 64 2e into the child.
18333 20 54 68 65 20 72 6f 6f 74 0a 2a 2a 20 70 61 67 The root.** pag
18334 65 20 69 73 20 74 68 65 6e 20 6f 76 65 72 77 72 e is then overwr
18335 69 74 74 65 6e 20 74 6f 20 6d 61 6b 65 20 69 74 itten to make it
18336 20 61 6e 20 65 6d 70 74 79 20 70 61 67 65 20 77 an empty page w
18337 69 74 68 20 74 68 65 20 72 69 67 68 74 2d 63 68 ith the right-ch
18338 69 6c 64 20 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 ild .** pointer
18339 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 pointing to the
1833a 6e 65 77 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 new page..**.**
1833b 42 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 Before returning
1833c 2c 20 61 6c 6c 20 70 6f 69 6e 74 65 72 2d 6d 61 , all pointer-ma
1833d 70 20 65 6e 74 72 69 65 73 20 63 6f 72 72 65 73 p entries corres
1833e 70 6f 6e 64 69 6e 67 20 74 6f 20 70 61 67 65 73 ponding to pages
1833f 20 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 6e 65 .** that the ne
18340 77 20 63 68 69 6c 64 2d 70 61 67 65 20 6e 6f 77 w child-page now
18341 20 63 6f 6e 74 61 69 6e 73 20 70 6f 69 6e 74 65 contains pointe
18342 72 73 20 74 6f 20 61 72 65 20 75 70 64 61 74 65 rs to are update
18343 64 2e 20 54 68 65 0a 2a 2a 20 65 6e 74 72 79 20 d. The.** entry
18344 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f corresponding to
18345 20 74 68 65 20 6e 65 77 20 72 69 67 68 74 2d 63 the new right-c
18346 68 69 6c 64 20 70 6f 69 6e 74 65 72 20 6f 66 20 hild pointer of
18347 74 68 65 20 72 6f 6f 74 0a 2a 2a 20 70 61 67 65 the root.** page
18348 20 69 73 20 61 6c 73 6f 20 75 70 64 61 74 65 64 is also updated
18349 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 75 63 63 65 ..**.** If succe
1834a 73 73 66 75 6c 2c 20 2a 70 70 43 68 69 6c 64 20 ssful, *ppChild
1834b 69 73 20 73 65 74 20 74 6f 20 63 6f 6e 74 61 69 is set to contai
1834c 6e 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f n a reference to
1834d 20 74 68 65 20 63 68 69 6c 64 20 0a 2a 2a 20 70 the child .** p
1834e 61 67 65 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f age and SQLITE_O
1834f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49 K is returned. I
18350 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65 20 n this case the
18351 63 61 6c 6c 65 72 20 69 73 20 72 65 71 75 69 72 caller is requir
18352 65 64 0a 2a 2a 20 74 6f 20 63 61 6c 6c 20 72 65 ed.** to call re
18353 6c 65 61 73 65 50 61 67 65 28 29 20 6f 6e 20 2a leasePage() on *
18354 70 70 43 68 69 6c 64 20 65 78 61 63 74 6c 79 20 ppChild exactly
18355 6f 6e 63 65 2e 20 49 66 20 61 6e 20 65 72 72 6f once. If an erro
18356 72 20 6f 63 63 75 72 73 2c 0a 2a 2a 20 61 6e 20 r occurs,.** an
18357 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 error code is re
18358 74 75 72 6e 65 64 20 61 6e 64 20 2a 70 70 43 68 turned and *ppCh
18359 69 6c 64 20 69 73 20 73 65 74 20 74 6f 20 30 2e ild is set to 0.
1835a 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 .*/.static int b
1835b 61 6c 61 6e 63 65 5f 64 65 65 70 65 72 28 4d 65 alance_deeper(Me
1835c 6d 50 61 67 65 20 2a 70 52 6f 6f 74 2c 20 4d 65 mPage *pRoot, Me
1835d 6d 50 61 67 65 20 2a 2a 70 70 43 68 69 6c 64 29 mPage **ppChild)
1835e 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 {. int rc;
1835f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18360 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 76 61 6c /* Return val
18361 75 65 20 66 72 6f 6d 20 73 75 62 70 72 6f 63 65 ue from subproce
18362 64 75 72 65 73 20 2a 2f 0a 20 20 4d 65 6d 50 61 dures */. MemPa
18363 67 65 20 2a 70 43 68 69 6c 64 20 3d 20 30 3b 20 ge *pChild = 0;
18364 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 /* Poi
18365 6e 74 65 72 20 74 6f 20 61 20 6e 65 77 20 63 68 nter to a new ch
18366 69 6c 64 20 70 61 67 65 20 2a 2f 0a 20 20 50 67 ild page */. Pg
18367 6e 6f 20 70 67 6e 6f 43 68 69 6c 64 20 3d 20 30 no pgnoChild = 0
18368 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
18369 50 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 Page number of t
1836a 68 65 20 6e 65 77 20 63 68 69 6c 64 20 70 61 67 he new child pag
1836b 65 20 2a 2f 0a 20 20 42 74 53 68 61 72 65 64 20 e */. BtShared
1836c 2a 70 42 74 20 3d 20 70 52 6f 6f 74 2d 3e 70 42 *pBt = pRoot->pB
1836d 74 3b 20 20 20 20 2f 2a 20 54 68 65 20 42 54 72 t; /* The BTr
1836e 65 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 ee */.. assert(
1836f 20 70 52 6f 6f 74 2d 3e 6e 4f 76 65 72 66 6c 6f pRoot->nOverflo
18370 77 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 w>0 );. assert(
18371 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
18372 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 eld(pBt->mutex)
18373 29 3b 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 70 52 );.. /* Make pR
18374 6f 6f 74 2c 20 74 68 65 20 72 6f 6f 74 20 70 61 oot, the root pa
18375 67 65 20 6f 66 20 74 68 65 20 62 2d 74 72 65 65 ge of the b-tree
18376 2c 20 77 72 69 74 61 62 6c 65 2e 20 41 6c 6c 6f , writable. Allo
18377 63 61 74 65 20 61 20 6e 65 77 20 0a 20 20 2a 2a cate a new . **
18378 20 70 61 67 65 20 74 68 61 74 20 77 69 6c 6c 20 page that will
18379 62 65 63 6f 6d 65 20 74 68 65 20 6e 65 77 20 72 become the new r
1837a 69 67 68 74 2d 63 68 69 6c 64 20 6f 66 20 70 50 ight-child of pP
1837b 61 67 65 2e 20 43 6f 70 79 20 74 68 65 20 63 6f age. Copy the co
1837c 6e 74 65 6e 74 73 0a 20 20 2a 2a 20 6f 66 20 74 ntents. ** of t
1837d 68 65 20 6e 6f 64 65 20 73 74 6f 72 65 64 20 6f he node stored o
1837e 6e 20 70 52 6f 6f 74 20 69 6e 74 6f 20 74 68 65 n pRoot into the
1837f 20 6e 65 77 20 63 68 69 6c 64 20 70 61 67 65 2e new child page.
18380 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c . */. rc = sql
18381 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 ite3PagerWrite(p
18382 52 6f 6f 74 2d 3e 70 44 62 50 61 67 65 29 3b 0a Root->pDbPage);.
18383 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
18384 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 _OK ){. rc =
18385 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61 67 allocateBtreePag
18386 65 28 70 42 74 2c 26 70 43 68 69 6c 64 2c 26 70 e(pBt,&pChild,&p
18387 67 6e 6f 43 68 69 6c 64 2c 70 52 6f 6f 74 2d 3e gnoChild,pRoot->
18388 70 67 6e 6f 2c 30 29 3b 0a 20 20 20 20 63 6f 70 pgno,0);. cop
18389 79 4e 6f 64 65 43 6f 6e 74 65 6e 74 28 70 52 6f yNodeContent(pRo
1838a 6f 74 2c 20 70 43 68 69 6c 64 2c 20 26 72 63 29 ot, pChild, &rc)
1838b 3b 0a 20 20 20 20 69 66 28 20 49 53 41 55 54 4f ;. if( ISAUTO
1838c 56 41 43 55 55 4d 20 29 7b 0a 20 20 20 20 20 20 VACUUM ){.
1838d 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20 70 ptrmapPut(pBt, p
1838e 67 6e 6f 43 68 69 6c 64 2c 20 50 54 52 4d 41 50 gnoChild, PTRMAP
1838f 5f 42 54 52 45 45 2c 20 70 52 6f 6f 74 2d 3e 70 _BTREE, pRoot->p
18390 67 6e 6f 2c 20 26 72 63 29 3b 0a 20 20 20 20 7d gno, &rc);. }
18391 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 20 29 7b . }. if( rc ){
18392 0a 20 20 20 20 2a 70 70 43 68 69 6c 64 20 3d 20 . *ppChild =
18393 30 3b 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61 0;. releasePa
18394 67 65 28 70 43 68 69 6c 64 29 3b 0a 20 20 20 20 ge(pChild);.
18395 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 return rc;. }.
18396 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
18397 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 PagerIswriteable
18398 28 70 43 68 69 6c 64 2d 3e 70 44 62 50 61 67 65 (pChild->pDbPage
18399 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 ) );. assert( s
1839a 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 qlite3PagerIswri
1839b 74 65 61 62 6c 65 28 70 52 6f 6f 74 2d 3e 70 44 teable(pRoot->pD
1839c 62 50 61 67 65 29 20 29 3b 0a 20 20 61 73 73 65 bPage) );. asse
1839d 72 74 28 20 70 43 68 69 6c 64 2d 3e 6e 43 65 6c rt( pChild->nCel
1839e 6c 3d 3d 70 52 6f 6f 74 2d 3e 6e 43 65 6c 6c 20 l==pRoot->nCell
1839f 29 3b 0a 0a 20 20 54 52 41 43 45 28 28 22 42 41 );.. TRACE(("BA
183a0 4c 41 4e 43 45 3a 20 63 6f 70 79 20 72 6f 6f 74 LANCE: copy root
183a1 20 25 64 20 69 6e 74 6f 20 25 64 5c 6e 22 2c 20 %d into %d\n",
183a2 70 52 6f 6f 74 2d 3e 70 67 6e 6f 2c 20 70 43 68 pRoot->pgno, pCh
183a3 69 6c 64 2d 3e 70 67 6e 6f 29 29 3b 0a 0a 20 20 ild->pgno));..
183a4 2f 2a 20 43 6f 70 79 20 74 68 65 20 6f 76 65 72 /* Copy the over
183a5 66 6c 6f 77 20 63 65 6c 6c 73 20 66 72 6f 6d 20 flow cells from
183a6 70 52 6f 6f 74 20 74 6f 20 70 43 68 69 6c 64 20 pRoot to pChild
183a7 2a 2f 0a 20 20 6d 65 6d 63 70 79 28 70 43 68 69 */. memcpy(pChi
183a8 6c 64 2d 3e 61 4f 76 66 6c 2c 20 70 52 6f 6f 74 ld->aOvfl, pRoot
183a9 2d 3e 61 4f 76 66 6c 2c 20 70 52 6f 6f 74 2d 3e ->aOvfl, pRoot->
183aa 6e 4f 76 65 72 66 6c 6f 77 2a 73 69 7a 65 6f 66 nOverflow*sizeof
183ab 28 70 52 6f 6f 74 2d 3e 61 4f 76 66 6c 5b 30 5d (pRoot->aOvfl[0]
183ac 29 29 3b 0a 20 20 70 43 68 69 6c 64 2d 3e 6e 4f ));. pChild->nO
183ad 76 65 72 66 6c 6f 77 20 3d 20 70 52 6f 6f 74 2d verflow = pRoot-
183ae 3e 6e 4f 76 65 72 66 6c 6f 77 3b 0a 0a 20 20 2f >nOverflow;.. /
183af 2a 20 5a 65 72 6f 20 74 68 65 20 63 6f 6e 74 65 * Zero the conte
183b0 6e 74 73 20 6f 66 20 70 52 6f 6f 74 2e 20 54 68 nts of pRoot. Th
183b1 65 6e 20 69 6e 73 74 61 6c 6c 20 70 43 68 69 6c en install pChil
183b2 64 20 61 73 20 74 68 65 20 72 69 67 68 74 2d 63 d as the right-c
183b3 68 69 6c 64 2e 20 2a 2f 0a 20 20 7a 65 72 6f 50 hild. */. zeroP
183b4 61 67 65 28 70 52 6f 6f 74 2c 20 70 43 68 69 6c age(pRoot, pChil
183b5 64 2d 3e 61 44 61 74 61 5b 30 5d 20 26 20 7e 50 d->aData[0] & ~P
183b6 54 46 5f 4c 45 41 46 29 3b 0a 20 20 70 75 74 34 TF_LEAF);. put4
183b7 62 79 74 65 28 26 70 52 6f 6f 74 2d 3e 61 44 61 byte(&pRoot->aDa
183b8 74 61 5b 70 52 6f 6f 74 2d 3e 68 64 72 4f 66 66 ta[pRoot->hdrOff
183b9 73 65 74 2b 38 5d 2c 20 70 67 6e 6f 43 68 69 6c set+8], pgnoChil
183ba 64 29 3b 0a 0a 20 20 2a 70 70 43 68 69 6c 64 20 d);.. *ppChild
183bb 3d 20 70 43 68 69 6c 64 3b 0a 20 20 72 65 74 75 = pChild;. retu
183bc 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
183bd 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 20 ./*.** The page
183be 74 68 61 74 20 70 43 75 72 20 63 75 72 72 65 6e that pCur curren
183bf 74 6c 79 20 70 6f 69 6e 74 73 20 74 6f 20 68 61 tly points to ha
183c0 73 20 6a 75 73 74 20 62 65 65 6e 20 6d 6f 64 69 s just been modi
183c1 66 69 65 64 20 69 6e 0a 2a 2a 20 73 6f 6d 65 20 fied in.** some
183c2 77 61 79 2e 20 54 68 69 73 20 66 75 6e 63 74 69 way. This functi
183c3 6f 6e 20 66 69 67 75 72 65 73 20 6f 75 74 20 69 on figures out i
183c4 66 20 74 68 69 73 20 6d 6f 64 69 66 69 63 61 74 f this modificat
183c5 69 6f 6e 20 6d 65 61 6e 73 20 74 68 65 0a 2a 2a ion means the.**
183c6 20 74 72 65 65 20 6e 65 65 64 73 20 74 6f 20 62 tree needs to b
183c7 65 20 62 61 6c 61 6e 63 65 64 2c 20 61 6e 64 20 e balanced, and
183c8 69 66 20 73 6f 20 63 61 6c 6c 73 20 74 68 65 20 if so calls the
183c9 61 70 70 72 6f 70 72 69 61 74 65 20 62 61 6c 61 appropriate bala
183ca 6e 63 69 6e 67 20 0a 2a 2a 20 72 6f 75 74 69 6e ncing .** routin
183cb 65 2e 20 42 61 6c 61 6e 63 69 6e 67 20 72 6f 75 e. Balancing rou
183cc 74 69 6e 65 73 20 61 72 65 3a 0a 2a 2a 0a 2a 2a tines are:.**.**
183cd 20 20 20 62 61 6c 61 6e 63 65 5f 71 75 69 63 6b balance_quick
183ce 28 29 0a 2a 2a 20 20 20 62 61 6c 61 6e 63 65 5f ().** balance_
183cf 64 65 65 70 65 72 28 29 0a 2a 2a 20 20 20 62 61 deeper().** ba
183d0 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 29 0a lance_nonroot().
183d1 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 61 */.static int ba
183d2 6c 61 6e 63 65 28 42 74 43 75 72 73 6f 72 20 2a lance(BtCursor *
183d3 70 43 75 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 pCur){. int rc
183d4 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 63 = SQLITE_OK;. c
183d5 6f 6e 73 74 20 69 6e 74 20 6e 4d 69 6e 20 3d 20 onst int nMin =
183d6 70 43 75 72 2d 3e 70 42 74 2d 3e 75 73 61 62 6c pCur->pBt->usabl
183d7 65 53 69 7a 65 20 2a 20 32 20 2f 20 33 3b 0a 20 eSize * 2 / 3;.
183d8 20 75 38 20 61 42 61 6c 61 6e 63 65 51 75 69 63 u8 aBalanceQuic
183d9 6b 53 70 61 63 65 5b 31 33 5d 3b 0a 20 20 75 38 kSpace[13];. u8
183da 20 2a 70 46 72 65 65 20 3d 20 30 3b 0a 0a 20 20 *pFree = 0;..
183db 54 45 53 54 4f 4e 4c 59 28 20 69 6e 74 20 62 61 TESTONLY( int ba
183dc 6c 61 6e 63 65 5f 71 75 69 63 6b 5f 63 61 6c 6c lance_quick_call
183dd 65 64 20 3d 20 30 20 29 3b 0a 20 20 54 45 53 54 ed = 0 );. TEST
183de 4f 4e 4c 59 28 20 69 6e 74 20 62 61 6c 61 6e 63 ONLY( int balanc
183df 65 5f 64 65 65 70 65 72 5f 63 61 6c 6c 65 64 20 e_deeper_called
183e0 3d 20 30 20 29 3b 0a 0a 20 20 64 6f 20 7b 0a 20 = 0 );.. do {.
183e1 20 20 20 69 6e 74 20 69 50 61 67 65 20 3d 20 70 int iPage = p
183e2 43 75 72 2d 3e 69 50 61 67 65 3b 0a 20 20 20 20 Cur->iPage;.
183e3 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 20 3d MemPage *pPage =
183e4 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 50 pCur->apPage[iP
183e5 61 67 65 5d 3b 0a 0a 20 20 20 20 69 66 28 20 69 age];.. if( i
183e6 50 61 67 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 Page==0 ){.
183e7 20 69 66 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 if( pPage->nOve
183e8 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20 20 20 20 rflow ){.
183e9 20 2f 2a 20 54 68 65 20 72 6f 6f 74 20 70 61 67 /* The root pag
183ea 65 20 6f 66 20 74 68 65 20 62 2d 74 72 65 65 20 e of the b-tree
183eb 69 73 20 6f 76 65 72 66 75 6c 6c 2e 20 49 6e 20 is overfull. In
183ec 74 68 69 73 20 63 61 73 65 20 63 61 6c 6c 20 74 this case call t
183ed 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 62 61 he. ** ba
183ee 6c 61 6e 63 65 5f 64 65 65 70 65 72 28 29 20 66 lance_deeper() f
183ef 75 6e 63 74 69 6f 6e 20 74 6f 20 63 72 65 61 74 unction to creat
183f0 65 20 61 20 6e 65 77 20 63 68 69 6c 64 20 66 6f e a new child fo
183f1 72 20 74 68 65 20 72 6f 6f 74 2d 70 61 67 65 0a r the root-page.
183f2 20 20 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 63 ** and c
183f3 6f 70 79 20 74 68 65 20 63 75 72 72 65 6e 74 20 opy the current
183f4 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 contents of the
183f5 72 6f 6f 74 2d 70 61 67 65 20 74 6f 20 69 74 2e root-page to it.
183f6 20 54 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 The. **
183f7 6e 65 78 74 20 69 74 65 72 61 74 69 6f 6e 20 6f next iteration o
183f8 66 20 74 68 65 20 64 6f 2d 6c 6f 6f 70 20 77 69 f the do-loop wi
183f9 6c 6c 20 62 61 6c 61 6e 63 65 20 74 68 65 20 63 ll balance the c
183fa 68 69 6c 64 20 70 61 67 65 2e 0a 20 20 20 20 20 hild page..
183fb 20 20 20 2a 2f 20 0a 20 20 20 20 20 20 20 20 61 */ . a
183fc 73 73 65 72 74 28 20 28 62 61 6c 61 6e 63 65 5f ssert( (balance_
183fd 64 65 65 70 65 72 5f 63 61 6c 6c 65 64 2b 2b 29 deeper_called++)
183fe 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 72 ==0 );. r
183ff 63 20 3d 20 62 61 6c 61 6e 63 65 5f 64 65 65 70 c = balance_deep
18400 65 72 28 70 50 61 67 65 2c 20 26 70 43 75 72 2d er(pPage, &pCur-
18401 3e 61 70 50 61 67 65 5b 31 5d 29 3b 0a 20 20 20 >apPage[1]);.
18402 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c if( rc==SQL
18403 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
18404 20 20 20 20 70 43 75 72 2d 3e 69 50 61 67 65 20 pCur->iPage
18405 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 70 = 1;. p
18406 43 75 72 2d 3e 61 69 49 64 78 5b 30 5d 20 3d 20 Cur->aiIdx[0] =
18407 30 3b 0a 20 20 20 20 20 20 20 20 20 20 70 43 75 0;. pCu
18408 72 2d 3e 61 69 49 64 78 5b 31 5d 20 3d 20 30 3b r->aiIdx[1] = 0;
18409 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 . asser
1840a 74 28 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b t( pCur->apPage[
1840b 31 5d 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 29 3b 1]->nOverflow );
1840c 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
1840d 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
1840e 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 break;. }.
1840f 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 50 61 }else if( pPa
18410 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 ge->nOverflow==0
18411 20 26 26 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 && pPage->nFree
18412 3c 3d 6e 4d 69 6e 20 29 7b 0a 20 20 20 20 20 20 <=nMin ){.
18413 62 72 65 61 6b 3b 0a 20 20 20 20 7d 65 6c 73 65 break;. }else
18414 7b 0a 20 20 20 20 20 20 4d 65 6d 50 61 67 65 20 {. MemPage
18415 2a 20 63 6f 6e 73 74 20 70 50 61 72 65 6e 74 20 * const pParent
18416 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 = pCur->apPage[i
18417 50 61 67 65 2d 31 5d 3b 0a 20 20 20 20 20 20 69 Page-1];. i
18418 6e 74 20 63 6f 6e 73 74 20 69 49 64 78 20 3d 20 nt const iIdx =
18419 70 43 75 72 2d 3e 61 69 49 64 78 5b 69 50 61 67 pCur->aiIdx[iPag
1841a 65 2d 31 5d 3b 0a 0a 20 20 20 20 20 20 72 63 20 e-1];.. rc
1841b 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 = sqlite3PagerWr
1841c 69 74 65 28 70 50 61 72 65 6e 74 2d 3e 70 44 62 ite(pParent->pDb
1841d 50 61 67 65 29 3b 0a 20 20 20 20 20 20 69 66 28 Page);. if(
1841e 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
1841f 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 {.#ifndef SQLITE
18420 5f 4f 4d 49 54 5f 51 55 49 43 4b 42 41 4c 41 4e _OMIT_QUICKBALAN
18421 43 45 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 CE. if( p
18422 50 61 67 65 2d 3e 68 61 73 44 61 74 61 0a 20 20 Page->hasData.
18423 20 20 20 20 20 20 20 26 26 20 70 50 61 67 65 2d && pPage-
18424 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 31 0a 20 20 >nOverflow==1.
18425 20 20 20 20 20 20 20 26 26 20 70 50 61 67 65 2d && pPage-
18426 3e 61 4f 76 66 6c 5b 30 5d 2e 69 64 78 3d 3d 70 >aOvfl[0].idx==p
18427 50 61 67 65 2d 3e 6e 43 65 6c 6c 0a 20 20 20 20 Page->nCell.
18428 20 20 20 20 20 26 26 20 70 50 61 72 65 6e 74 2d && pParent-
18429 3e 70 67 6e 6f 21 3d 31 0a 20 20 20 20 20 20 20 >pgno!=1.
1842a 20 20 26 26 20 70 50 61 72 65 6e 74 2d 3e 6e 43 && pParent->nC
1842b 65 6c 6c 3d 3d 69 49 64 78 0a 20 20 20 20 20 20 ell==iIdx.
1842c 20 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f ){. /
1842d 2a 20 43 61 6c 6c 20 62 61 6c 61 6e 63 65 5f 71 * Call balance_q
1842e 75 69 63 6b 28 29 20 74 6f 20 63 72 65 61 74 65 uick() to create
1842f 20 61 20 6e 65 77 20 73 69 62 6c 69 6e 67 20 6f a new sibling o
18430 66 20 70 50 61 67 65 20 6f 6e 20 77 68 69 63 68 f pPage on which
18431 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 6f . ** to
18432 20 73 74 6f 72 65 20 74 68 65 20 6f 76 65 72 66 store the overf
18433 6c 6f 77 20 63 65 6c 6c 2e 20 62 61 6c 61 6e 63 low cell. balanc
18434 65 5f 71 75 69 63 6b 28 29 20 69 6e 73 65 72 74 e_quick() insert
18435 73 20 61 20 6e 65 77 20 63 65 6c 6c 0a 20 20 20 s a new cell.
18436 20 20 20 20 20 20 20 2a 2a 20 69 6e 74 6f 20 70 ** into p
18437 50 61 72 65 6e 74 2c 20 77 68 69 63 68 20 6d 61 Parent, which ma
18438 79 20 63 61 75 73 65 20 70 50 61 72 65 6e 74 20 y cause pParent
18439 6f 76 65 72 66 6c 6f 77 2e 20 49 66 20 74 68 69 overflow. If thi
1843a 73 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 68 s. ** h
1843b 61 70 70 65 6e 73 2c 20 74 68 65 20 6e 65 78 74 appens, the next
1843c 20 69 6e 74 65 72 61 74 69 6f 6e 20 6f 66 20 74 interation of t
1843d 68 65 20 64 6f 2d 6c 6f 6f 70 20 77 69 6c 6c 20 he do-loop will
1843e 62 61 6c 61 6e 63 65 20 70 50 61 72 65 6e 74 20 balance pParent
1843f 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 75 73 . ** us
18440 65 20 65 69 74 68 65 72 20 62 61 6c 61 6e 63 65 e either balance
18441 5f 6e 6f 6e 72 6f 6f 74 28 29 20 6f 72 20 62 61 _nonroot() or ba
18442 6c 61 6e 63 65 5f 64 65 65 70 65 72 28 29 2e 20 lance_deeper().
18443 55 6e 74 69 6c 20 74 68 69 73 0a 20 20 20 20 20 Until this.
18444 20 20 20 20 20 2a 2a 20 68 61 70 70 65 6e 73 2c ** happens,
18445 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 63 65 the overflow ce
18446 6c 6c 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 ll is stored in
18447 74 68 65 20 61 42 61 6c 61 6e 63 65 51 75 69 63 the aBalanceQuic
18448 6b 53 70 61 63 65 5b 5d 0a 20 20 20 20 20 20 20 kSpace[].
18449 20 20 20 2a 2a 20 62 75 66 66 65 72 2e 20 0a 20 ** buffer. .
1844a 20 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 **.
1844b 20 20 20 20 20 20 2a 2a 20 54 68 65 20 70 75 72 ** The pur
1844c 70 6f 73 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c pose of the foll
1844d 6f 77 69 6e 67 20 61 73 73 65 72 74 28 29 20 69 owing assert() i
1844e 73 20 74 6f 20 63 68 65 63 6b 20 74 68 61 74 20 s to check that
1844f 6f 6e 6c 79 20 61 0a 20 20 20 20 20 20 20 20 20 only a.
18450 20 2a 2a 20 73 69 6e 67 6c 65 20 63 61 6c 6c 20 ** single call
18451 74 6f 20 62 61 6c 61 6e 63 65 5f 71 75 69 63 6b to balance_quick
18452 28 29 20 69 73 20 6d 61 64 65 20 66 6f 72 20 65 () is made for e
18453 61 63 68 20 63 61 6c 6c 20 74 6f 20 74 68 69 73 ach call to this
18454 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 66 75 . ** fu
18455 6e 63 74 69 6f 6e 2e 20 49 66 20 74 68 69 73 20 nction. If this
18456 77 65 72 65 20 6e 6f 74 20 76 65 72 69 66 69 65 were not verifie
18457 64 2c 20 61 20 73 75 62 74 6c 65 20 62 75 67 20 d, a subtle bug
18458 69 6e 76 6f 6c 76 69 6e 67 20 72 65 75 73 65 0a involving reuse.
18459 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6f 66 20 ** of
1845a 74 68 65 20 61 42 61 6c 61 6e 63 65 51 75 69 63 the aBalanceQuic
1845b 6b 53 70 61 63 65 5b 5d 20 6d 69 67 68 74 20 73 kSpace[] might s
1845c 6e 65 61 6b 20 69 6e 2e 0a 20 20 20 20 20 20 20 neak in..
1845d 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 */.
1845e 61 73 73 65 72 74 28 20 28 62 61 6c 61 6e 63 65 assert( (balance
1845f 5f 71 75 69 63 6b 5f 63 61 6c 6c 65 64 2b 2b 29 _quick_called++)
18460 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 20 ==0 );.
18461 20 72 63 20 3d 20 62 61 6c 61 6e 63 65 5f 71 75 rc = balance_qu
18462 69 63 6b 28 70 50 61 72 65 6e 74 2c 20 70 50 61 ick(pParent, pPa
18463 67 65 2c 20 61 42 61 6c 61 6e 63 65 51 75 69 63 ge, aBalanceQuic
18464 6b 53 70 61 63 65 29 3b 0a 20 20 20 20 20 20 20 kSpace);.
18465 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 }else.#endif.
18466 20 20 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20 {.
18467 20 20 2f 2a 20 49 6e 20 74 68 69 73 20 63 61 73 /* In this cas
18468 65 2c 20 63 61 6c 6c 20 62 61 6c 61 6e 63 65 5f e, call balance_
18469 6e 6f 6e 72 6f 6f 74 28 29 20 74 6f 20 72 65 64 nonroot() to red
1846a 69 73 74 72 69 62 75 74 65 20 63 65 6c 6c 73 0a istribute cells.
1846b 20 20 20 20 20 20 20 20 20 20 2a 2a 20 62 65 74 ** bet
1846c 77 65 65 6e 20 70 50 61 67 65 20 61 6e 64 20 75 ween pPage and u
1846d 70 20 74 6f 20 32 20 6f 66 20 69 74 73 20 73 69 p to 2 of its si
1846e 62 6c 69 6e 67 20 70 61 67 65 73 2e 20 54 68 69 bling pages. Thi
1846f 73 20 69 6e 76 6f 6c 76 65 73 0a 20 20 20 20 20 s involves.
18470 20 20 20 20 20 2a 2a 20 6d 6f 64 69 66 79 69 6e ** modifyin
18471 67 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f g the contents o
18472 66 20 70 50 61 72 65 6e 74 2c 20 77 68 69 63 68 f pParent, which
18473 20 6d 61 79 20 63 61 75 73 65 20 70 50 61 72 65 may cause pPare
18474 6e 74 20 74 6f 0a 20 20 20 20 20 20 20 20 20 20 nt to.
18475 2a 2a 20 62 65 63 6f 6d 65 20 6f 76 65 72 66 75 ** become overfu
18476 6c 6c 20 6f 72 20 75 6e 64 65 72 66 75 6c 6c 2e ll or underfull.
18477 20 54 68 65 20 6e 65 78 74 20 69 74 65 72 61 74 The next iterat
18478 69 6f 6e 20 6f 66 20 74 68 65 20 64 6f 2d 6c 6f ion of the do-lo
18479 6f 70 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 op. **
1847a 77 69 6c 6c 20 62 61 6c 61 6e 63 65 20 74 68 65 will balance the
1847b 20 70 61 72 65 6e 74 20 70 61 67 65 20 74 6f 20 parent page to
1847c 63 6f 72 72 65 63 74 20 74 68 69 73 2e 0a 20 20 correct this..
1847d 20 20 20 20 20 20 20 20 2a 2a 20 0a 20 20 20 20 ** .
1847e 20 20 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20 ** If the
1847f 70 61 72 65 6e 74 20 70 61 67 65 20 62 65 63 6f parent page beco
18480 6d 65 73 20 6f 76 65 72 66 75 6c 6c 2c 20 74 68 mes overfull, th
18481 65 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 20 e overflow cell
18482 6f 72 20 63 65 6c 6c 73 0a 20 20 20 20 20 20 20 or cells.
18483 20 20 20 2a 2a 20 61 72 65 20 73 74 6f 72 65 64 ** are stored
18484 20 69 6e 20 74 68 65 20 70 53 70 61 63 65 20 62 in the pSpace b
18485 75 66 66 65 72 20 61 6c 6c 6f 63 61 74 65 64 20 uffer allocated
18486 69 6d 6d 65 64 69 61 74 65 6c 79 20 62 65 6c 6f immediately belo
18487 77 2e 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2a w. . **
18488 20 41 20 73 75 62 73 65 71 75 65 6e 74 20 69 74 A subsequent it
18489 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 64 eration of the d
1848a 6f 2d 6c 6f 6f 70 20 77 69 6c 6c 20 64 65 61 6c o-loop will deal
1848b 20 77 69 74 68 20 74 68 69 73 20 62 79 0a 20 20 with this by.
1848c 20 20 20 20 20 20 20 20 2a 2a 20 63 61 6c 6c 69 ** calli
1848d 6e 67 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f ng balance_nonro
1848e 6f 74 28 29 20 28 62 61 6c 61 6e 63 65 5f 64 65 ot() (balance_de
1848f 65 70 65 72 28 29 20 6d 61 79 20 62 65 20 63 61 eper() may be ca
18490 6c 6c 65 64 20 66 69 72 73 74 2c 0a 20 20 20 20 lled first,.
18491 20 20 20 20 20 20 2a 2a 20 62 75 74 20 69 74 20 ** but it
18492 64 6f 65 73 6e 27 74 20 64 65 61 6c 20 77 69 74 doesn't deal wit
18493 68 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 73 h overflow cells
18494 20 2d 20 6a 75 73 74 20 6d 6f 76 65 73 20 74 68 - just moves th
18495 65 6d 20 74 6f 20 61 0a 20 20 20 20 20 20 20 20 em to a.
18496 20 20 2a 2a 20 64 69 66 66 65 72 65 6e 74 20 70 ** different p
18497 61 67 65 29 2e 20 4f 6e 63 65 20 74 68 69 73 20 age). Once this
18498 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 20 subsequent call
18499 74 6f 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f to balance_nonro
1849a 6f 74 28 29 20 0a 20 20 20 20 20 20 20 20 20 20 ot() .
1849b 2a 2a 20 68 61 73 20 63 6f 6d 70 6c 65 74 65 64 ** has completed
1849c 2c 20 69 74 20 69 73 20 73 61 66 65 20 74 6f 20 , it is safe to
1849d 72 65 6c 65 61 73 65 20 74 68 65 20 70 53 70 61 release the pSpa
1849e 63 65 20 62 75 66 66 65 72 20 75 73 65 64 20 62 ce buffer used b
1849f 79 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 y. ** t
184a0 68 65 20 70 72 65 76 69 6f 75 73 20 63 61 6c 6c he previous call
184a1 2c 20 61 73 20 74 68 65 20 6f 76 65 72 66 6c 6f , as the overflo
184a2 77 20 63 65 6c 6c 20 64 61 74 61 20 77 69 6c 6c w cell data will
184a3 20 68 61 76 65 20 62 65 65 6e 20 0a 20 20 20 20 have been .
184a4 20 20 20 20 20 20 2a 2a 20 63 6f 70 69 65 64 20 ** copied
184a5 65 69 74 68 65 72 20 69 6e 74 6f 20 74 68 65 20 either into the
184a6 62 6f 64 79 20 6f 66 20 61 20 64 61 74 61 62 61 body of a databa
184a7 73 65 20 70 61 67 65 20 6f 72 20 69 6e 74 6f 20 se page or into
184a8 74 68 65 20 6e 65 77 0a 20 20 20 20 20 20 20 20 the new.
184a9 20 20 2a 2a 20 70 53 70 61 63 65 20 62 75 66 66 ** pSpace buff
184aa 65 72 20 70 61 73 73 65 64 20 74 6f 20 74 68 65 er passed to the
184ab 20 6c 61 74 74 65 72 20 63 61 6c 6c 20 74 6f 20 latter call to
184ac 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 balance_nonroot(
184ad 29 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a ).. */.
184ae 20 20 20 20 20 20 20 20 20 20 75 38 20 2a 70 53 u8 *pS
184af 70 61 63 65 20 3d 20 73 71 6c 69 74 65 33 50 61 pace = sqlite3Pa
184b0 67 65 4d 61 6c 6c 6f 63 28 70 43 75 72 2d 3e 70 geMalloc(pCur->p
184b1 42 74 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 Bt->pageSize);.
184b2 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 62 61 rc = ba
184b3 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 70 50 lance_nonroot(pP
184b4 61 72 65 6e 74 2c 20 69 49 64 78 2c 20 70 53 70 arent, iIdx, pSp
184b5 61 63 65 2c 20 69 50 61 67 65 3d 3d 31 29 3b 0a ace, iPage==1);.
184b6 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 46 if( pF
184b7 72 65 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ree ){.
184b8 20 20 20 2f 2a 20 49 66 20 70 46 72 65 65 20 69 /* If pFree i
184b9 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 69 74 20 70 s not NULL, it p
184ba 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 70 53 70 oints to the pSp
184bb 61 63 65 20 62 75 66 66 65 72 20 75 73 65 64 20 ace buffer used
184bc 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 . **
184bd 62 79 20 61 20 70 72 65 76 69 6f 75 73 20 63 61 by a previous ca
184be 6c 6c 20 74 6f 20 62 61 6c 61 6e 63 65 5f 6e 6f ll to balance_no
184bf 6e 72 6f 6f 74 28 29 2e 20 49 74 73 20 63 6f 6e nroot(). Its con
184c0 74 65 6e 74 73 20 61 72 65 0a 20 20 20 20 20 20 tents are.
184c1 20 20 20 20 20 20 2a 2a 20 6e 6f 77 20 73 74 6f ** now sto
184c2 72 65 64 20 65 69 74 68 65 72 20 6f 6e 20 72 65 red either on re
184c3 61 6c 20 64 61 74 61 62 61 73 65 20 70 61 67 65 al database page
184c4 73 20 6f 72 20 77 69 74 68 69 6e 20 74 68 65 20 s or within the
184c5 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 . **
184c6 6e 65 77 20 70 53 70 61 63 65 20 62 75 66 66 65 new pSpace buffe
184c7 72 2c 20 73 6f 20 69 74 20 6d 61 79 20 62 65 20 r, so it may be
184c8 73 61 66 65 6c 79 20 66 72 65 65 64 20 68 65 72 safely freed her
184c9 65 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 e. */.
184ca 20 20 73 71 6c 69 74 65 33 50 61 67 65 46 72 65 sqlite3PageFre
184cb 65 28 70 46 72 65 65 29 3b 0a 20 20 20 20 20 20 e(pFree);.
184cc 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 20 }..
184cd 20 2f 2a 20 54 68 65 20 70 53 70 61 63 65 20 62 /* The pSpace b
184ce 75 66 66 65 72 20 77 69 6c 6c 20 62 65 20 66 72 uffer will be fr
184cf 65 65 64 20 61 66 74 65 72 20 74 68 65 20 6e 65 eed after the ne
184d0 78 74 20 63 61 6c 6c 20 74 6f 0a 20 20 20 20 20 xt call to.
184d1 20 20 20 20 20 2a 2a 20 62 61 6c 61 6e 63 65 5f ** balance_
184d2 6e 6f 6e 72 6f 6f 74 28 29 2c 20 6f 72 20 6a 75 nonroot(), or ju
184d3 73 74 20 62 65 66 6f 72 65 20 74 68 69 73 20 66 st before this f
184d4 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 2c unction returns,
184d5 20 77 68 69 63 68 65 76 65 72 0a 20 20 20 20 20 whichever.
184d6 20 20 20 20 20 2a 2a 20 63 6f 6d 65 73 20 66 69 ** comes fi
184d7 72 73 74 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 rst. */.
184d8 20 20 70 46 72 65 65 20 3d 20 70 53 70 61 63 65 pFree = pSpace
184d9 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
184da 20 20 7d 0a 0a 20 20 20 20 20 20 70 50 61 67 65 }.. pPage
184db 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 3d 20 30 3b ->nOverflow = 0;
184dc 0a 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e .. /* The n
184dd 65 78 74 20 69 74 65 72 61 74 69 6f 6e 20 6f 66 ext iteration of
184de 20 74 68 65 20 64 6f 2d 6c 6f 6f 70 20 62 61 6c the do-loop bal
184df 61 6e 63 65 73 20 74 68 65 20 70 61 72 65 6e 74 ances the parent
184e0 20 70 61 67 65 2e 20 2a 2f 0a 20 20 20 20 20 20 page. */.
184e1 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 releasePage(pPag
184e2 65 29 3b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e e);. pCur->
184e3 69 50 61 67 65 2d 2d 3b 0a 20 20 20 20 7d 0a 20 iPage--;. }.
184e4 20 7d 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c }while( rc==SQL
184e5 49 54 45 5f 4f 4b 20 29 3b 0a 0a 20 20 69 66 28 ITE_OK );.. if(
184e6 20 70 46 72 65 65 20 29 7b 0a 20 20 20 20 73 71 pFree ){. sq
184e7 6c 69 74 65 33 50 61 67 65 46 72 65 65 28 70 46 lite3PageFree(pF
184e8 72 65 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 ree);. }. retu
184e9 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a rn rc;.}.../*.**
184ea 20 49 6e 73 65 72 74 20 61 20 6e 65 77 20 72 65 Insert a new re
184eb 63 6f 72 64 20 69 6e 74 6f 20 74 68 65 20 42 54 cord into the BT
184ec 72 65 65 2e 20 20 54 68 65 20 6b 65 79 20 69 73 ree. The key is
184ed 20 67 69 76 65 6e 20 62 79 20 28 70 4b 65 79 2c given by (pKey,
184ee 6e 4b 65 79 29 0a 2a 2a 20 61 6e 64 20 74 68 65 nKey).** and the
184ef 20 64 61 74 61 20 69 73 20 67 69 76 65 6e 20 62 data is given b
184f0 79 20 28 70 44 61 74 61 2c 6e 44 61 74 61 29 2e y (pData,nData).
184f1 20 20 54 68 65 20 63 75 72 73 6f 72 20 69 73 20 The cursor is
184f2 75 73 65 64 20 6f 6e 6c 79 20 74 6f 0a 2a 2a 20 used only to.**
184f3 64 65 66 69 6e 65 20 77 68 61 74 20 74 61 62 6c define what tabl
184f4 65 20 74 68 65 20 72 65 63 6f 72 64 20 73 68 6f e the record sho
184f5 75 6c 64 20 62 65 20 69 6e 73 65 72 74 65 64 20 uld be inserted
184f6 69 6e 74 6f 2e 20 20 54 68 65 20 63 75 72 73 6f into. The curso
184f7 72 0a 2a 2a 20 69 73 20 6c 65 66 74 20 70 6f 69 r.** is left poi
184f8 6e 74 69 6e 67 20 61 74 20 61 20 72 61 6e 64 6f nting at a rando
184f9 6d 20 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a m location..**.*
184fa 2a 20 46 6f 72 20 61 6e 20 49 4e 54 4b 45 59 20 * For an INTKEY
184fb 74 61 62 6c 65 2c 20 6f 6e 6c 79 20 74 68 65 20 table, only the
184fc 6e 4b 65 79 20 76 61 6c 75 65 20 6f 66 20 74 68 nKey value of th
184fd 65 20 6b 65 79 20 69 73 20 75 73 65 64 2e 20 20 e key is used.
184fe 70 4b 65 79 20 69 73 0a 2a 2a 20 69 67 6e 6f 72 pKey is.** ignor
184ff 65 64 2e 20 20 46 6f 72 20 61 20 5a 45 52 4f 44 ed. For a ZEROD
18500 41 54 41 20 74 61 62 6c 65 2c 20 74 68 65 20 70 ATA table, the p
18501 44 61 74 61 20 61 6e 64 20 6e 44 61 74 61 20 61 Data and nData a
18502 72 65 20 62 6f 74 68 20 69 67 6e 6f 72 65 64 2e re both ignored.
18503 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 73 65 .**.** If the se
18504 65 6b 52 65 73 75 6c 74 20 70 61 72 61 6d 65 74 ekResult paramet
18505 65 72 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 er is non-zero,
18506 74 68 65 6e 20 61 20 73 75 63 63 65 73 73 66 75 then a successfu
18507 6c 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 4d 6f 76 l call to.** Mov
18508 65 74 6f 55 6e 70 61 63 6b 65 64 28 29 20 74 6f etoUnpacked() to
18509 20 73 65 65 6b 20 63 75 72 73 6f 72 20 70 43 75 seek cursor pCu
1850a 72 20 74 6f 20 28 70 4b 65 79 2c 20 6e 4b 65 79 r to (pKey, nKey
1850b 29 20 68 61 73 20 61 6c 72 65 61 64 79 0a 2a 2a ) has already.**
1850c 20 62 65 65 6e 20 70 65 72 66 6f 72 6d 65 64 2e been performed.
1850d 20 73 65 65 6b 52 65 73 75 6c 74 20 69 73 20 74 seekResult is t
1850e 68 65 20 73 65 61 72 63 68 20 72 65 73 75 6c 74 he search result
1850f 20 72 65 74 75 72 6e 65 64 20 28 61 20 6e 65 67 returned (a neg
18510 61 74 69 76 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 ative.** number
18511 69 66 20 70 43 75 72 20 70 6f 69 6e 74 73 20 61 if pCur points a
18512 74 20 61 6e 20 65 6e 74 72 79 20 74 68 61 74 20 t an entry that
18513 69 73 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 is smaller than
18514 28 70 4b 65 79 2c 20 6e 4b 65 79 29 2c 20 6f 72 (pKey, nKey), or
18515 0a 2a 2a 20 61 20 70 6f 73 69 74 69 76 65 20 76 .** a positive v
18516 61 6c 75 65 20 69 66 20 70 43 75 72 20 70 6f 69 alue if pCur poi
18517 6e 74 73 20 61 74 20 61 6e 20 65 74 72 79 20 74 nts at an etry t
18518 68 61 74 20 69 73 20 6c 61 72 67 65 72 20 74 68 hat is larger th
18519 61 6e 20 0a 2a 2a 20 28 70 4b 65 79 2c 20 6e 4b an .** (pKey, nK
1851a 65 79 29 29 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 ey)). .**.** If
1851b 74 68 65 20 73 65 65 6b 52 65 73 75 6c 74 20 70 the seekResult p
1851c 61 72 61 6d 65 74 65 72 20 69 73 20 6e 6f 6e 2d arameter is non-
1851d 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 63 zero, then the c
1851e 61 6c 6c 65 72 20 67 75 61 72 61 6e 74 65 65 73 aller guarantees
1851f 20 74 68 61 74 0a 2a 2a 20 63 75 72 73 6f 72 20 that.** cursor
18520 70 43 75 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 pCur is pointing
18521 20 61 74 20 74 68 65 20 65 78 69 73 74 69 6e 67 at the existing
18522 20 63 6f 70 79 20 6f 66 20 61 20 72 6f 77 20 74 copy of a row t
18523 68 61 74 20 69 73 20 74 6f 20 62 65 0a 2a 2a 20 hat is to be.**
18524 6f 76 65 72 77 72 69 74 74 65 6e 2e 20 20 49 66 overwritten. If
18525 20 74 68 65 20 73 65 65 6b 52 65 73 75 6c 74 20 the seekResult
18526 70 61 72 61 6d 65 74 65 72 20 69 73 20 30 2c 20 parameter is 0,
18527 74 68 65 6e 20 63 75 72 73 6f 72 20 70 43 75 72 then cursor pCur
18528 20 6d 61 79 0a 2a 2a 20 70 6f 69 6e 74 20 74 6f may.** point to
18529 20 61 6e 79 20 65 6e 74 72 79 20 6f 72 20 74 6f any entry or to
1852a 20 6e 6f 20 65 6e 74 72 79 20 61 74 20 61 6c 6c no entry at all
1852b 20 61 6e 64 20 73 6f 20 74 68 69 73 20 66 75 6e and so this fun
1852c 63 74 69 6f 6e 20 68 61 73 20 74 6f 20 73 65 65 ction has to see
1852d 6b 0a 2a 2a 20 74 68 65 20 63 75 72 73 6f 72 20 k.** the cursor
1852e 62 65 66 6f 72 65 20 74 68 65 20 6e 65 77 20 6b before the new k
1852f 65 79 20 63 61 6e 20 62 65 20 69 6e 73 65 72 74 ey can be insert
18530 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ed..*/.SQLITE_PR
18531 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
18532 33 42 74 72 65 65 49 6e 73 65 72 74 28 0a 20 20 3BtreeInsert(.
18533 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 BtCursor *pCur,
18534 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
18535 2a 20 49 6e 73 65 72 74 20 64 61 74 61 20 69 6e * Insert data in
18536 74 6f 20 74 68 65 20 74 61 62 6c 65 20 6f 66 20 to the table of
18537 74 68 69 73 20 63 75 72 73 6f 72 20 2a 2f 0a 20 this cursor */.
18538 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 const void *pKe
18539 79 2c 20 69 36 34 20 6e 4b 65 79 2c 20 20 20 20 y, i64 nKey,
1853a 2f 2a 20 54 68 65 20 6b 65 79 20 6f 66 20 74 68 /* The key of th
1853b 65 20 6e 65 77 20 72 65 63 6f 72 64 20 2a 2f 0a e new record */.
1853c 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 44 const void *pD
1853d 61 74 61 2c 20 69 6e 74 20 6e 44 61 74 61 2c 20 ata, int nData,
1853e 20 2f 2a 20 54 68 65 20 64 61 74 61 20 6f 66 20 /* The data of
1853f 74 68 65 20 6e 65 77 20 72 65 63 6f 72 64 20 2a the new record *
18540 2f 0a 20 20 69 6e 74 20 6e 5a 65 72 6f 2c 20 20 /. int nZero,
18541 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18542 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
18543 65 78 74 72 61 20 30 20 62 79 74 65 73 20 74 6f extra 0 bytes to
18544 20 61 70 70 65 6e 64 20 74 6f 20 64 61 74 61 20 append to data
18545 2a 2f 0a 20 20 69 6e 74 20 61 70 70 65 6e 64 42 */. int appendB
18546 69 61 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 ias,
18547 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 /* True if t
18548 68 69 73 20 69 73 20 6c 69 6b 65 6c 79 20 61 6e his is likely an
18549 20 61 70 70 65 6e 64 20 2a 2f 0a 20 20 69 6e 74 append */. int
1854a 20 73 65 65 6b 52 65 73 75 6c 74 20 20 20 20 20 seekResult
1854b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 /* R
1854c 65 73 75 6c 74 20 6f 66 20 70 72 69 6f 72 20 4d esult of prior M
1854d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 29 20 ovetoUnpacked()
1854e 63 61 6c 6c 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 call */.){. int
1854f 20 72 63 3b 0a 20 20 69 6e 74 20 6c 6f 63 20 3d rc;. int loc =
18550 20 73 65 65 6b 52 65 73 75 6c 74 3b 20 20 20 20 seekResult;
18551 20 20 20 20 20 20 2f 2a 20 2d 31 3a 20 62 65 66 /* -1: bef
18552 6f 72 65 20 64 65 73 69 72 65 64 20 6c 6f 63 61 ore desired loca
18553 74 69 6f 6e 20 20 2b 31 3a 20 61 66 74 65 72 20 tion +1: after
18554 2a 2f 0a 20 20 69 6e 74 20 73 7a 4e 65 77 3b 0a */. int szNew;.
18555 20 20 69 6e 74 20 69 64 78 3b 0a 20 20 4d 65 6d int idx;. Mem
18556 50 61 67 65 20 2a 70 50 61 67 65 3b 0a 20 20 42 Page *pPage;. B
18557 74 72 65 65 20 2a 70 20 3d 20 70 43 75 72 2d 3e tree *p = pCur->
18558 70 42 74 72 65 65 3b 0a 20 20 42 74 53 68 61 72 pBtree;. BtShar
18559 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 ed *pBt = p->pBt
1855a 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 ;. unsigned cha
1855b 72 20 2a 6f 6c 64 43 65 6c 6c 3b 0a 20 20 75 6e r *oldCell;. un
1855c 73 69 67 6e 65 64 20 63 68 61 72 20 2a 6e 65 77 signed char *new
1855d 43 65 6c 6c 20 3d 20 30 3b 0a 0a 20 20 69 66 28 Cell = 0;.. if(
1855e 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 pCur->eState==C
1855f 55 52 53 4f 52 5f 46 41 55 4c 54 20 29 7b 0a 20 URSOR_FAULT ){.
18560 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d assert( pCur-
18561 3e 73 6b 69 70 4e 65 78 74 21 3d 53 51 4c 49 54 >skipNext!=SQLIT
18562 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 72 65 74 75 E_OK );. retu
18563 72 6e 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78 rn pCur->skipNex
18564 74 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 t;. }.. assert
18565 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 ( cursorHoldsMut
18566 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 ex(pCur) );. as
18567 73 65 72 74 28 20 70 43 75 72 2d 3e 77 72 46 6c sert( pCur->wrFl
18568 61 67 20 26 26 20 70 42 74 2d 3e 69 6e 54 72 61 ag && pBt->inTra
18569 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f nsaction==TRANS_
1856a 57 52 49 54 45 20 26 26 20 21 70 42 74 2d 3e 72 WRITE && !pBt->r
1856b 65 61 64 4f 6e 6c 79 20 29 3b 0a 20 20 61 73 73 eadOnly );. ass
1856c 65 72 74 28 20 68 61 73 53 68 61 72 65 64 43 61 ert( hasSharedCa
1856d 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 cheTableLock(p,
1856e 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 pCur->pgnoRoot,
1856f 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 21 3d pCur->pKeyInfo!=
18570 30 2c 20 32 29 20 29 3b 0a 0a 20 20 2f 2a 20 41 0, 2) );.. /* A
18571 73 73 65 72 74 20 74 68 61 74 20 74 68 65 20 63 ssert that the c
18572 61 6c 6c 65 72 20 68 61 73 20 62 65 65 6e 20 63 aller has been c
18573 6f 6e 73 69 73 74 65 6e 74 2e 20 49 66 20 74 68 onsistent. If th
18574 69 73 20 63 75 72 73 6f 72 20 77 61 73 20 6f 70 is cursor was op
18575 65 6e 65 64 0a 20 20 2a 2a 20 65 78 70 65 63 74 ened. ** expect
18576 69 6e 67 20 61 6e 20 69 6e 64 65 78 20 62 2d 74 ing an index b-t
18577 72 65 65 2c 20 74 68 65 6e 20 74 68 65 20 63 61 ree, then the ca
18578 6c 6c 65 72 20 73 68 6f 75 6c 64 20 62 65 20 69 ller should be i
18579 6e 73 65 72 74 69 6e 67 20 62 6c 6f 62 0a 20 20 nserting blob.
1857a 2a 2a 20 6b 65 79 73 20 77 69 74 68 20 6e 6f 20 ** keys with no
1857b 61 73 73 6f 63 69 61 74 65 64 20 64 61 74 61 2e associated data.
1857c 20 49 66 20 74 68 65 20 63 75 72 73 6f 72 20 77 If the cursor w
1857d 61 73 20 6f 70 65 6e 65 64 20 65 78 70 65 63 74 as opened expect
1857e 69 6e 67 20 61 6e 0a 20 20 2a 2a 20 69 6e 74 6b ing an. ** intk
1857f 65 79 20 74 61 62 6c 65 2c 20 74 68 65 20 63 61 ey table, the ca
18580 6c 6c 65 72 20 73 68 6f 75 6c 64 20 62 65 20 69 ller should be i
18581 6e 73 65 72 74 69 6e 67 20 69 6e 74 65 67 65 72 nserting integer
18582 20 6b 65 79 73 20 77 69 74 68 20 61 0a 20 20 2a keys with a. *
18583 2a 20 62 6c 6f 62 20 6f 66 20 61 73 73 6f 63 69 * blob of associ
18584 61 74 65 64 20 64 61 74 61 2e 20 20 2a 2f 0a 20 ated data. */.
18585 20 61 73 73 65 72 74 28 20 28 70 4b 65 79 3d 3d assert( (pKey==
18586 30 29 3d 3d 28 70 43 75 72 2d 3e 70 4b 65 79 49 0)==(pCur->pKeyI
18587 6e 66 6f 3d 3d 30 29 20 29 3b 0a 0a 20 20 2f 2a nfo==0) );.. /*
18588 20 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 69 If this is an i
18589 6e 73 65 72 74 20 69 6e 74 6f 20 61 20 74 61 62 nsert into a tab
1858a 6c 65 20 62 2d 74 72 65 65 2c 20 69 6e 76 61 6c le b-tree, inval
1858b 69 64 61 74 65 20 61 6e 79 20 69 6e 63 72 62 6c idate any incrbl
1858c 6f 62 20 0a 20 20 2a 2a 20 63 75 72 73 6f 72 73 ob . ** cursors
1858d 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20 72 6f 77 open on the row
1858e 20 62 65 69 6e 67 20 72 65 70 6c 61 63 65 64 20 being replaced
1858f 28 61 73 73 75 6d 69 6e 67 20 74 68 69 73 20 69 (assuming this i
18590 73 20 61 20 72 65 70 6c 61 63 65 0a 20 20 2a 2a s a replace. **
18591 20 6f 70 65 72 61 74 69 6f 6e 20 2d 20 69 66 20 operation - if
18592 69 74 20 69 73 20 6e 6f 74 2c 20 74 68 65 20 66 it is not, the f
18593 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 61 20 6e 6f ollowing is a no
18594 2d 6f 70 29 2e 20 20 2a 2f 0a 20 20 69 66 28 20 -op). */. if(
18595 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 3d 3d pCur->pKeyInfo==
18596 30 20 29 7b 0a 20 20 20 20 69 6e 76 61 6c 69 64 0 ){. invalid
18597 61 74 65 49 6e 63 72 62 6c 6f 62 43 75 72 73 6f ateIncrblobCurso
18598 72 73 28 70 2c 20 6e 4b 65 79 2c 20 30 29 3b 0a rs(p, nKey, 0);.
18599 20 20 7d 0a 0a 20 20 2f 2a 20 53 61 76 65 20 74 }.. /* Save t
1859a 68 65 20 70 6f 73 69 74 69 6f 6e 73 20 6f 66 20 he positions of
1859b 61 6e 79 20 6f 74 68 65 72 20 63 75 72 73 6f 72 any other cursor
1859c 73 20 6f 70 65 6e 20 6f 6e 20 74 68 69 73 20 74 s open on this t
1859d 61 62 6c 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 able.. **. **
1859e 49 6e 20 73 6f 6d 65 20 63 61 73 65 73 2c 20 74 In some cases, t
1859f 68 65 20 63 61 6c 6c 20 74 6f 20 62 74 72 65 65 he call to btree
185a0 4d 6f 76 65 74 6f 28 29 20 62 65 6c 6f 77 20 69 Moveto() below i
185a1 73 20 61 20 6e 6f 2d 6f 70 2e 20 46 6f 72 0a 20 s a no-op. For.
185a2 20 2a 2a 20 65 78 61 6d 70 6c 65 2c 20 77 68 65 ** example, whe
185a3 6e 20 69 6e 73 65 72 74 69 6e 67 20 64 61 74 61 n inserting data
185a4 20 69 6e 74 6f 20 61 20 74 61 62 6c 65 20 77 69 into a table wi
185a5 74 68 20 61 75 74 6f 2d 67 65 6e 65 72 61 74 65 th auto-generate
185a6 64 20 69 6e 74 65 67 65 72 0a 20 20 2a 2a 20 6b d integer. ** k
185a7 65 79 73 2c 20 74 68 65 20 56 44 42 45 20 6c 61 eys, the VDBE la
185a8 79 65 72 20 69 6e 76 6f 6b 65 73 20 73 71 6c 69 yer invokes sqli
185a9 74 65 33 42 74 72 65 65 4c 61 73 74 28 29 20 74 te3BtreeLast() t
185aa 6f 20 66 69 67 75 72 65 20 6f 75 74 20 74 68 65 o figure out the
185ab 20 0a 20 20 2a 2a 20 69 6e 74 65 67 65 72 20 6b . ** integer k
185ac 65 79 20 74 6f 20 75 73 65 2e 20 49 74 20 74 68 ey to use. It th
185ad 65 6e 20 63 61 6c 6c 73 20 74 68 69 73 20 66 75 en calls this fu
185ae 6e 63 74 69 6f 6e 20 74 6f 20 61 63 74 75 61 6c nction to actual
185af 6c 79 20 69 6e 73 65 72 74 20 74 68 65 20 0a 20 ly insert the .
185b0 20 2a 2a 20 64 61 74 61 20 69 6e 74 6f 20 74 68 ** data into th
185b1 65 20 69 6e 74 6b 65 79 20 42 2d 54 72 65 65 2e e intkey B-Tree.
185b2 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 62 74 In this case bt
185b3 72 65 65 4d 6f 76 65 74 6f 28 29 20 72 65 63 6f reeMoveto() reco
185b4 67 6e 69 7a 65 73 0a 20 20 2a 2a 20 74 68 61 74 gnizes. ** that
185b5 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 61 the cursor is a
185b6 6c 72 65 61 64 79 20 77 68 65 72 65 20 69 74 20 lready where it
185b7 6e 65 65 64 73 20 74 6f 20 62 65 20 61 6e 64 20 needs to be and
185b8 72 65 74 75 72 6e 73 20 77 69 74 68 6f 75 74 0a returns without.
185b9 20 20 2a 2a 20 64 6f 69 6e 67 20 61 6e 79 20 77 ** doing any w
185ba 6f 72 6b 2e 20 54 6f 20 61 76 6f 69 64 20 74 68 ork. To avoid th
185bb 77 61 72 74 69 6e 67 20 74 68 65 73 65 20 6f 70 warting these op
185bc 74 69 6d 69 7a 61 74 69 6f 6e 73 2c 20 69 74 20 timizations, it
185bd 69 73 20 69 6d 70 6f 72 74 61 6e 74 0a 20 20 2a is important. *
185be 2a 20 6e 6f 74 20 74 6f 20 63 6c 65 61 72 20 74 * not to clear t
185bf 68 65 20 63 75 72 73 6f 72 20 68 65 72 65 2e 0a he cursor here..
185c0 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 61 76 65 */. rc = save
185c1 41 6c 6c 43 75 72 73 6f 72 73 28 70 42 74 2c 20 AllCursors(pBt,
185c2 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 pCur->pgnoRoot,
185c3 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72 63 20 pCur);. if( rc
185c4 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 69 ) return rc;. i
185c5 66 28 20 21 6c 6f 63 20 29 7b 0a 20 20 20 20 72 f( !loc ){. r
185c6 63 20 3d 20 62 74 72 65 65 4d 6f 76 65 74 6f 28 c = btreeMoveto(
185c7 70 43 75 72 2c 20 70 4b 65 79 2c 20 6e 4b 65 79 pCur, pKey, nKey
185c8 2c 20 61 70 70 65 6e 64 42 69 61 73 2c 20 26 6c , appendBias, &l
185c9 6f 63 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 oc);. if( rc
185ca 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d ) return rc;. }
185cb 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d . assert( pCur-
185cc 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f >eState==CURSOR_
185cd 56 41 4c 49 44 20 7c 7c 20 28 70 43 75 72 2d 3e VALID || (pCur->
185ce 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 49 eState==CURSOR_I
185cf 4e 56 41 4c 49 44 20 26 26 20 6c 6f 63 29 20 29 NVALID && loc) )
185d0 3b 0a 0a 20 20 70 50 61 67 65 20 3d 20 70 43 75 ;.. pPage = pCu
185d1 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e r->apPage[pCur->
185d2 69 50 61 67 65 5d 3b 0a 20 20 61 73 73 65 72 74 iPage];. assert
185d3 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 ( pPage->intKey
185d4 7c 7c 20 6e 4b 65 79 3e 3d 30 20 29 3b 0a 20 20 || nKey>=0 );.
185d5 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6c assert( pPage->l
185d6 65 61 66 20 7c 7c 20 21 70 50 61 67 65 2d 3e 69 eaf || !pPage->i
185d7 6e 74 4b 65 79 20 29 3b 0a 0a 20 20 54 52 41 43 ntKey );.. TRAC
185d8 45 28 28 22 49 4e 53 45 52 54 3a 20 74 61 62 6c E(("INSERT: tabl
185d9 65 3d 25 64 20 6e 6b 65 79 3d 25 6c 6c 64 20 6e e=%d nkey=%lld n
185da 64 61 74 61 3d 25 64 20 70 61 67 65 3d 25 64 20 data=%d page=%d
185db 25 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 %s\n",.
185dc 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c pCur->pgnoRoot,
185dd 20 6e 4b 65 79 2c 20 6e 44 61 74 61 2c 20 70 50 nKey, nData, pP
185de 61 67 65 2d 3e 70 67 6e 6f 2c 0a 20 20 20 20 20 age->pgno,.
185df 20 20 20 20 20 6c 6f 63 3d 3d 30 20 3f 20 22 6f loc==0 ? "o
185e0 76 65 72 77 72 69 74 65 22 20 3a 20 22 6e 65 77 verwrite" : "new
185e1 20 65 6e 74 72 79 22 29 29 3b 0a 20 20 61 73 73 entry"));. ass
185e2 65 72 74 28 20 70 50 61 67 65 2d 3e 69 73 49 6e ert( pPage->isIn
185e3 69 74 20 29 3b 0a 20 20 61 6c 6c 6f 63 61 74 65 it );. allocate
185e4 54 65 6d 70 53 70 61 63 65 28 70 42 74 29 3b 0a TempSpace(pBt);.
185e5 20 20 6e 65 77 43 65 6c 6c 20 3d 20 70 42 74 2d newCell = pBt-
185e6 3e 70 54 6d 70 53 70 61 63 65 3b 0a 20 20 69 66 >pTmpSpace;. if
185e7 28 20 6e 65 77 43 65 6c 6c 3d 3d 30 20 29 20 72 ( newCell==0 ) r
185e8 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d eturn SQLITE_NOM
185e9 45 4d 3b 0a 20 20 72 63 20 3d 20 66 69 6c 6c 49 EM;. rc = fillI
185ea 6e 43 65 6c 6c 28 70 50 61 67 65 2c 20 6e 65 77 nCell(pPage, new
185eb 43 65 6c 6c 2c 20 70 4b 65 79 2c 20 6e 4b 65 79 Cell, pKey, nKey
185ec 2c 20 70 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 , pData, nData,
185ed 6e 5a 65 72 6f 2c 20 26 73 7a 4e 65 77 29 3b 0a nZero, &szNew);.
185ee 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 if( rc ) goto
185ef 65 6e 64 5f 69 6e 73 65 72 74 3b 0a 20 20 61 73 end_insert;. as
185f0 73 65 72 74 28 20 73 7a 4e 65 77 3d 3d 63 65 6c sert( szNew==cel
185f1 6c 53 69 7a 65 50 74 72 28 70 50 61 67 65 2c 20 lSizePtr(pPage,
185f2 6e 65 77 43 65 6c 6c 29 20 29 3b 0a 20 20 61 73 newCell) );. as
185f3 73 65 72 74 28 20 73 7a 4e 65 77 3c 3d 4d 58 5f sert( szNew<=MX_
185f4 43 45 4c 4c 5f 53 49 5a 45 28 70 42 74 29 20 29 CELL_SIZE(pBt) )
185f5 3b 0a 20 20 69 64 78 20 3d 20 70 43 75 72 2d 3e ;. idx = pCur->
185f6 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 aiIdx[pCur->iPag
185f7 65 5d 3b 0a 20 20 69 66 28 20 6c 6f 63 3d 3d 30 e];. if( loc==0
185f8 20 29 7b 0a 20 20 20 20 75 31 36 20 73 7a 4f 6c ){. u16 szOl
185f9 64 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 d;. assert( i
185fa 64 78 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 dx<pPage->nCell
185fb 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 );. rc = sqli
185fc 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 te3PagerWrite(pP
185fd 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 age->pDbPage);.
185fe 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 if( rc ){.
185ff 20 20 20 67 6f 74 6f 20 65 6e 64 5f 69 6e 73 65 goto end_inse
18600 72 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6f 6c rt;. }. ol
18601 64 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c dCell = findCell
18602 28 70 50 61 67 65 2c 20 69 64 78 29 3b 0a 20 20 (pPage, idx);.
18603 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 if( !pPage->le
18604 61 66 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 af ){. memc
18605 70 79 28 6e 65 77 43 65 6c 6c 2c 20 6f 6c 64 43 py(newCell, oldC
18606 65 6c 6c 2c 20 34 29 3b 0a 20 20 20 20 7d 0a 20 ell, 4);. }.
18607 20 20 20 73 7a 4f 6c 64 20 3d 20 63 65 6c 6c 53 szOld = cellS
18608 69 7a 65 50 74 72 28 70 50 61 67 65 2c 20 6f 6c izePtr(pPage, ol
18609 64 43 65 6c 6c 29 3b 0a 20 20 20 20 72 63 20 3d dCell);. rc =
1860a 20 63 6c 65 61 72 43 65 6c 6c 28 70 50 61 67 65 clearCell(pPage
1860b 2c 20 6f 6c 64 43 65 6c 6c 29 3b 0a 20 20 20 20 , oldCell);.
1860c 64 72 6f 70 43 65 6c 6c 28 70 50 61 67 65 2c 20 dropCell(pPage,
1860d 69 64 78 2c 20 73 7a 4f 6c 64 2c 20 26 72 63 29 idx, szOld, &rc)
1860e 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 67 ;. if( rc ) g
1860f 6f 74 6f 20 65 6e 64 5f 69 6e 73 65 72 74 3b 0a oto end_insert;.
18610 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 6f 63 3c }else if( loc<
18611 30 20 26 26 20 70 50 61 67 65 2d 3e 6e 43 65 6c 0 && pPage->nCel
18612 6c 3e 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 l>0 ){. asser
18613 74 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 t( pPage->leaf )
18614 3b 0a 20 20 20 20 69 64 78 20 3d 20 2b 2b 70 43 ;. idx = ++pC
18615 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e ur->aiIdx[pCur->
18616 69 50 61 67 65 5d 3b 0a 20 20 7d 65 6c 73 65 7b iPage];. }else{
18617 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 . assert( pPa
18618 67 65 2d 3e 6c 65 61 66 20 29 3b 0a 20 20 7d 0a ge->leaf );. }.
18619 20 20 69 6e 73 65 72 74 43 65 6c 6c 28 70 50 61 insertCell(pPa
1861a 67 65 2c 20 69 64 78 2c 20 6e 65 77 43 65 6c 6c ge, idx, newCell
1861b 2c 20 73 7a 4e 65 77 2c 20 30 2c 20 30 2c 20 26 , szNew, 0, 0, &
1861c 72 63 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 rc);. assert( r
1861d 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 c!=SQLITE_OK ||
1861e 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3e 30 20 7c pPage->nCell>0 |
1861f 7c 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c | pPage->nOverfl
18620 6f 77 3e 30 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 ow>0 );.. /* If
18621 20 6e 6f 20 65 72 72 6f 72 20 68 61 73 20 6f 63 no error has oc
18622 63 75 72 65 64 20 61 6e 64 20 70 50 61 67 65 20 cured and pPage
18623 68 61 73 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 has an overflow
18624 63 65 6c 6c 2c 20 63 61 6c 6c 20 62 61 6c 61 6e cell, call balan
18625 63 65 28 29 20 0a 20 20 2a 2a 20 74 6f 20 72 65 ce() . ** to re
18626 64 69 73 74 72 69 62 75 74 65 20 74 68 65 20 63 distribute the c
18627 65 6c 6c 73 20 77 69 74 68 69 6e 20 74 68 65 20 ells within the
18628 74 72 65 65 2e 20 53 69 6e 63 65 20 62 61 6c 61 tree. Since bala
18629 6e 63 65 28 29 20 6d 61 79 20 6d 6f 76 65 0a 20 nce() may move.
1862a 20 2a 2a 20 74 68 65 20 63 75 72 73 6f 72 2c 20 ** the cursor,
1862b 7a 65 72 6f 20 74 68 65 20 42 74 43 75 72 73 6f zero the BtCurso
1862c 72 2e 69 6e 66 6f 2e 6e 53 69 7a 65 20 61 6e 64 r.info.nSize and
1862d 20 42 74 43 75 72 73 6f 72 2e 76 61 6c 69 64 4e BtCursor.validN
1862e 4b 65 79 0a 20 20 2a 2a 20 76 61 72 69 61 62 6c Key. ** variabl
1862f 65 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 50 72 es.. **. ** Pr
18630 65 76 69 6f 75 73 20 76 65 72 73 69 6f 6e 73 20 evious versions
18631 6f 66 20 53 51 4c 69 74 65 20 63 61 6c 6c 65 64 of SQLite called
18632 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 29 20 74 6f moveToRoot() to
18633 20 6d 6f 76 65 20 74 68 65 20 63 75 72 73 6f 72 move the cursor
18634 0a 20 20 2a 2a 20 62 61 63 6b 20 74 6f 20 74 68 . ** back to th
18635 65 20 72 6f 6f 74 20 70 61 67 65 20 61 73 20 62 e root page as b
18636 61 6c 61 6e 63 65 28 29 20 75 73 65 64 20 74 6f alance() used to
18637 20 69 6e 76 61 6c 69 64 61 74 65 20 74 68 65 20 invalidate the
18638 63 6f 6e 74 65 6e 74 73 0a 20 20 2a 2a 20 6f 66 contents. ** of
18639 20 42 74 43 75 72 73 6f 72 2e 61 70 50 61 67 65 BtCursor.apPage
1863a 5b 5d 20 61 6e 64 20 42 74 43 75 72 73 6f 72 2e [] and BtCursor.
1863b 61 69 49 64 78 5b 5d 2e 20 49 6e 73 74 65 61 64 aiIdx[]. Instead
1863c 20 6f 66 20 64 6f 69 6e 67 20 74 68 61 74 2c 0a of doing that,.
1863d 20 20 2a 2a 20 73 65 74 20 74 68 65 20 63 75 72 ** set the cur
1863e 73 6f 72 20 73 74 61 74 65 20 74 6f 20 22 69 6e sor state to "in
1863f 76 61 6c 69 64 22 2e 20 54 68 69 73 20 6d 61 6b valid". This mak
18640 65 73 20 63 6f 6d 6d 6f 6e 20 69 6e 73 65 72 74 es common insert
18641 20 6f 70 65 72 61 74 69 6f 6e 73 0a 20 20 2a 2a operations. **
18642 20 73 6c 69 67 68 74 6c 79 20 66 61 73 74 65 72 slightly faster
18643 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 72 .. **. ** Ther
18644 65 20 69 73 20 61 20 73 75 62 74 6c 65 20 62 75 e is a subtle bu
18645 74 20 69 6d 70 6f 72 74 61 6e 74 20 6f 70 74 69 t important opti
18646 6d 69 7a 61 74 69 6f 6e 20 68 65 72 65 20 74 6f mization here to
18647 6f 2e 20 57 68 65 6e 20 69 6e 73 65 72 74 69 6e o. When insertin
18648 67 0a 20 20 2a 2a 20 6d 75 6c 74 69 70 6c 65 20 g. ** multiple
18649 72 65 63 6f 72 64 73 20 69 6e 74 6f 20 61 6e 20 records into an
1864a 69 6e 74 6b 65 79 20 62 2d 74 72 65 65 20 75 73 intkey b-tree us
1864b 69 6e 67 20 61 20 73 69 6e 67 6c 65 20 63 75 72 ing a single cur
1864c 73 6f 72 20 28 61 73 20 63 61 6e 0a 20 20 2a 2a sor (as can. **
1864d 20 68 61 70 70 65 6e 20 77 68 69 6c 65 20 70 72 happen while pr
1864e 6f 63 65 73 73 69 6e 67 20 61 6e 20 22 49 4e 53 ocessing an "INS
1864f 45 52 54 20 49 4e 54 4f 20 2e 2e 2e 20 53 45 4c ERT INTO ... SEL
18650 45 43 54 22 20 73 74 61 74 65 6d 65 6e 74 29 2c ECT" statement),
18651 20 69 74 0a 20 20 2a 2a 20 69 73 20 61 64 76 61 it. ** is adva
18652 6e 74 61 67 65 6f 75 73 20 74 6f 20 6c 65 61 76 ntageous to leav
18653 65 20 74 68 65 20 63 75 72 73 6f 72 20 70 6f 69 e the cursor poi
18654 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 6c 61 73 nting to the las
18655 74 20 65 6e 74 72 79 20 69 6e 0a 20 20 2a 2a 20 t entry in. **
18656 74 68 65 20 62 2d 74 72 65 65 20 69 66 20 70 6f the b-tree if po
18657 73 73 69 62 6c 65 2e 20 49 66 20 74 68 65 20 63 ssible. If the c
18658 75 72 73 6f 72 20 69 73 20 6c 65 66 74 20 70 6f ursor is left po
18659 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 6c 61 inting to the la
1865a 73 74 0a 20 20 2a 2a 20 65 6e 74 72 79 20 69 6e st. ** entry in
1865b 20 74 68 65 20 74 61 62 6c 65 2c 20 61 6e 64 20 the table, and
1865c 74 68 65 20 6e 65 78 74 20 72 6f 77 20 69 6e 73 the next row ins
1865d 65 72 74 65 64 20 68 61 73 20 61 6e 20 69 6e 74 erted has an int
1865e 65 67 65 72 20 6b 65 79 0a 20 20 2a 2a 20 6c 61 eger key. ** la
1865f 72 67 65 72 20 74 68 61 6e 20 74 68 65 20 6c 61 rger than the la
18660 72 67 65 73 74 20 65 78 69 73 74 69 6e 67 20 6b rgest existing k
18661 65 79 2c 20 69 74 20 69 73 20 70 6f 73 73 69 62 ey, it is possib
18662 6c 65 20 74 6f 20 69 6e 73 65 72 74 20 74 68 65 le to insert the
18663 0a 20 20 2a 2a 20 72 6f 77 20 77 69 74 68 6f 75 . ** row withou
18664 74 20 73 65 65 6b 69 6e 67 20 74 68 65 20 63 75 t seeking the cu
18665 72 73 6f 72 2e 20 54 68 69 73 20 63 61 6e 20 62 rsor. This can b
18666 65 20 61 20 62 69 67 20 70 65 72 66 6f 72 6d 61 e a big performa
18667 6e 63 65 20 62 6f 6f 73 74 2e 0a 20 20 2a 2f 0a nce boost.. */.
18668 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 pCur->info.nSi
18669 7a 65 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e ze = 0;. pCur->
1866a 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0a 20 validNKey = 0;.
1866b 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
1866c 4f 4b 20 26 26 20 70 50 61 67 65 2d 3e 6e 4f 76 OK && pPage->nOv
1866d 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20 72 63 erflow ){. rc
1866e 20 3d 20 62 61 6c 61 6e 63 65 28 70 43 75 72 29 = balance(pCur)
1866f 3b 0a 0a 20 20 20 20 2f 2a 20 4d 75 73 74 20 6d ;.. /* Must m
18670 61 6b 65 20 73 75 72 65 20 6e 4f 76 65 72 66 6c ake sure nOverfl
18671 6f 77 20 69 73 20 72 65 73 65 74 20 74 6f 20 7a ow is reset to z
18672 65 72 6f 20 65 76 65 6e 20 69 66 20 74 68 65 20 ero even if the
18673 62 61 6c 61 6e 63 65 28 29 0a 20 20 20 20 2a 2a balance(). **
18674 20 66 61 69 6c 73 2e 20 49 6e 74 65 72 6e 61 6c fails. Internal
18675 20 64 61 74 61 20 73 74 72 75 63 74 75 72 65 20 data structure
18676 63 6f 72 72 75 70 74 69 6f 6e 20 77 69 6c 6c 20 corruption will
18677 72 65 73 75 6c 74 20 6f 74 68 65 72 77 69 73 65 result otherwise
18678 2e 20 0a 20 20 20 20 2a 2a 20 41 6c 73 6f 2c 20 . . ** Also,
18679 73 65 74 20 74 68 65 20 63 75 72 73 6f 72 20 73 set the cursor s
1867a 74 61 74 65 20 74 6f 20 69 6e 76 61 6c 69 64 2e tate to invalid.
1867b 20 54 68 69 73 20 73 74 6f 70 73 20 73 61 76 65 This stops save
1867c 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 CursorPosition()
1867d 0a 20 20 20 20 2a 2a 20 66 72 6f 6d 20 74 72 79 . ** from try
1867e 69 6e 67 20 74 6f 20 73 61 76 65 20 74 68 65 20 ing to save the
1867f 63 75 72 72 65 6e 74 20 70 6f 73 69 74 69 6f 6e current position
18680 20 6f 66 20 74 68 65 20 63 75 72 73 6f 72 2e 20 of the cursor.
18681 20 2a 2f 0a 20 20 20 20 70 43 75 72 2d 3e 61 70 */. pCur->ap
18682 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 Page[pCur->iPage
18683 5d 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 3d 20 30 ]->nOverflow = 0
18684 3b 0a 20 20 20 20 70 43 75 72 2d 3e 65 53 74 61 ;. pCur->eSta
18685 74 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 te = CURSOR_INVA
18686 4c 49 44 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 LID;. }. asser
18687 74 28 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b t( pCur->apPage[
18688 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 4f pCur->iPage]->nO
18689 76 65 72 66 6c 6f 77 3d 3d 30 20 29 3b 0a 0a 65 verflow==0 );..e
1868a 6e 64 5f 69 6e 73 65 72 74 3a 0a 20 20 72 65 74 nd_insert:. ret
1868b 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
1868c 20 44 65 6c 65 74 65 20 74 68 65 20 65 6e 74 72 Delete the entr
1868d 79 20 74 68 61 74 20 74 68 65 20 63 75 72 73 6f y that the curso
1868e 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 74 6f r is pointing to
1868f 2e 20 20 54 68 65 20 63 75 72 73 6f 72 0a 2a 2a . The cursor.**
18690 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e is left pointin
18691 67 20 61 74 20 61 20 61 72 62 69 74 72 61 72 79 g at a arbitrary
18692 20 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 location..*/.SQ
18693 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
18694 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 65 6c sqlite3BtreeDel
18695 65 74 65 28 42 74 43 75 72 73 6f 72 20 2a 70 43 ete(BtCursor *pC
18696 75 72 29 7b 0a 20 20 42 74 72 65 65 20 2a 70 20 ur){. Btree *p
18697 3d 20 70 43 75 72 2d 3e 70 42 74 72 65 65 3b 0a = pCur->pBtree;.
18698 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 BtShared *pBt
18699 3d 20 70 2d 3e 70 42 74 3b 20 20 20 20 20 20 20 = p->pBt;
1869a 20 20 20 20 20 20 20 0a 20 20 69 6e 74 20 72 63 . int rc
1869b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1869c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1869d 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f * Return code */
1869e 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 . MemPage *pPag
1869f 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e;
186a0 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 /* Page
186a1 74 6f 20 64 65 6c 65 74 65 20 63 65 6c 6c 20 66 to delete cell f
186a2 72 6f 6d 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 rom */. unsigne
186a3 64 20 63 68 61 72 20 2a 70 43 65 6c 6c 3b 20 20 d char *pCell;
186a4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
186a5 20 50 6f 69 6e 74 65 72 20 74 6f 20 63 65 6c 6c Pointer to cell
186a6 20 74 6f 20 64 65 6c 65 74 65 20 2a 2f 0a 20 20 to delete */.
186a7 69 6e 74 20 69 43 65 6c 6c 49 64 78 3b 20 20 20 int iCellIdx;
186a8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
186a9 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 /* Index of
186aa 20 63 65 6c 6c 20 74 6f 20 64 65 6c 65 74 65 20 cell to delete
186ab 2a 2f 0a 20 20 69 6e 74 20 69 43 65 6c 6c 44 65 */. int iCellDe
186ac 70 74 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 pth;
186ad 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 70 /* Dep
186ae 74 68 20 6f 66 20 6e 6f 64 65 20 63 6f 6e 74 61 th of node conta
186af 69 6e 69 6e 67 20 70 43 65 6c 6c 20 2a 2f 20 0a ining pCell */ .
186b0 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f . assert( curso
186b1 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 rHoldsMutex(pCur
186b2 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 ) );. assert( p
186b3 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f Bt->inTransactio
186b4 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 n==TRANS_WRITE )
186b5 3b 0a 20 20 61 73 73 65 72 74 28 20 21 70 42 74 ;. assert( !pBt
186b6 2d 3e 72 65 61 64 4f 6e 6c 79 20 29 3b 0a 20 20 ->readOnly );.
186b7 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 77 72 assert( pCur->wr
186b8 46 6c 61 67 20 29 3b 0a 20 20 61 73 73 65 72 74 Flag );. assert
186b9 28 20 68 61 73 53 68 61 72 65 64 43 61 63 68 65 ( hasSharedCache
186ba 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 70 43 75 TableLock(p, pCu
186bb 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 70 43 75 r->pgnoRoot, pCu
186bc 72 2d 3e 70 4b 65 79 49 6e 66 6f 21 3d 30 2c 20 r->pKeyInfo!=0,
186bd 32 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 2) );. assert(
186be 21 68 61 73 52 65 61 64 43 6f 6e 66 6c 69 63 74 !hasReadConflict
186bf 73 28 70 2c 20 70 43 75 72 2d 3e 70 67 6e 6f 52 s(p, pCur->pgnoR
186c0 6f 6f 74 29 20 29 3b 0a 0a 20 20 69 66 28 20 4e oot) );.. if( N
186c1 45 56 45 52 28 70 43 75 72 2d 3e 61 69 49 64 78 EVER(pCur->aiIdx
186c2 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3e 3d 70 [pCur->iPage]>=p
186c3 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 Cur->apPage[pCur
186c4 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 29 ->iPage]->nCell)
186c5 20 0a 20 20 20 7c 7c 20 4e 45 56 45 52 28 70 43 . || NEVER(pC
186c6 75 72 2d 3e 65 53 74 61 74 65 21 3d 43 55 52 53 ur->eState!=CURS
186c7 4f 52 5f 56 41 4c 49 44 29 0a 20 20 29 7b 0a 20 OR_VALID). ){.
186c8 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
186c9 5f 45 52 52 4f 52 3b 20 20 2f 2a 20 53 6f 6d 65 _ERROR; /* Some
186ca 74 68 69 6e 67 20 68 61 73 20 67 6f 6e 65 20 61 thing has gone a
186cb 77 72 79 2e 20 2a 2f 0a 20 20 7d 0a 0a 20 20 2f wry. */. }.. /
186cc 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 20 64 * If this is a d
186cd 65 6c 65 74 65 20 6f 70 65 72 61 74 69 6f 6e 20 elete operation
186ce 74 6f 20 72 65 6d 6f 76 65 20 61 20 72 6f 77 20 to remove a row
186cf 66 72 6f 6d 20 61 20 74 61 62 6c 65 20 62 2d 74 from a table b-t
186d0 72 65 65 2c 0a 20 20 2a 2a 20 69 6e 76 61 6c 69 ree,. ** invali
186d1 64 61 74 65 20 61 6e 79 20 69 6e 63 72 62 6c 6f date any incrblo
186d2 62 20 63 75 72 73 6f 72 73 20 6f 70 65 6e 20 6f b cursors open o
186d3 6e 20 74 68 65 20 72 6f 77 20 62 65 69 6e 67 20 n the row being
186d4 64 65 6c 65 74 65 64 2e 20 20 2a 2f 0a 20 20 69 deleted. */. i
186d5 66 28 20 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 f( pCur->pKeyInf
186d6 6f 3d 3d 30 20 29 7b 0a 20 20 20 20 69 6e 76 61 o==0 ){. inva
186d7 6c 69 64 61 74 65 49 6e 63 72 62 6c 6f 62 43 75 lidateIncrblobCu
186d8 72 73 6f 72 73 28 70 2c 20 70 43 75 72 2d 3e 69 rsors(p, pCur->i
186d9 6e 66 6f 2e 6e 4b 65 79 2c 20 30 29 3b 0a 20 20 nfo.nKey, 0);.
186da 7d 0a 0a 20 20 69 43 65 6c 6c 44 65 70 74 68 20 }.. iCellDepth
186db 3d 20 70 43 75 72 2d 3e 69 50 61 67 65 3b 0a 20 = pCur->iPage;.
186dc 20 69 43 65 6c 6c 49 64 78 20 3d 20 70 43 75 72 iCellIdx = pCur
186dd 2d 3e 61 69 49 64 78 5b 69 43 65 6c 6c 44 65 70 ->aiIdx[iCellDep
186de 74 68 5d 3b 0a 20 20 70 50 61 67 65 20 3d 20 70 th];. pPage = p
186df 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 43 65 6c Cur->apPage[iCel
186e0 6c 44 65 70 74 68 5d 3b 0a 20 20 70 43 65 6c 6c lDepth];. pCell
186e1 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 = findCell(pPag
186e2 65 2c 20 69 43 65 6c 6c 49 64 78 29 3b 0a 0a 20 e, iCellIdx);..
186e3 20 2f 2a 20 49 66 20 74 68 65 20 70 61 67 65 20 /* If the page
186e4 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 65 containing the e
186e5 6e 74 72 79 20 74 6f 20 64 65 6c 65 74 65 20 69 ntry to delete i
186e6 73 20 6e 6f 74 20 61 20 6c 65 61 66 20 70 61 67 s not a leaf pag
186e7 65 2c 20 6d 6f 76 65 0a 20 20 2a 2a 20 74 68 65 e, move. ** the
186e8 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 6c cursor to the l
186e9 61 72 67 65 73 74 20 65 6e 74 72 79 20 69 6e 20 argest entry in
186ea 74 68 65 20 74 72 65 65 20 74 68 61 74 20 69 73 the tree that is
186eb 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 0a 20 20 smaller than.
186ec 2a 2a 20 74 68 65 20 65 6e 74 72 79 20 62 65 69 ** the entry bei
186ed 6e 67 20 64 65 6c 65 74 65 64 2e 20 54 68 69 73 ng deleted. This
186ee 20 63 65 6c 6c 20 77 69 6c 6c 20 72 65 70 6c 61 cell will repla
186ef 63 65 20 74 68 65 20 63 65 6c 6c 20 62 65 69 6e ce the cell bein
186f0 67 20 64 65 6c 65 74 65 64 0a 20 20 2a 2a 20 66 g deleted. ** f
186f1 72 6f 6d 20 74 68 65 20 69 6e 74 65 72 6e 61 6c rom the internal
186f2 20 6e 6f 64 65 2e 20 54 68 65 20 27 70 72 65 76 node. The 'prev
186f3 69 6f 75 73 27 20 65 6e 74 72 79 20 69 73 20 75 ious' entry is u
186f4 73 65 64 20 66 6f 72 20 74 68 69 73 20 69 6e 73 sed for this ins
186f5 74 65 61 64 0a 20 20 2a 2a 20 6f 66 20 74 68 65 tead. ** of the
186f6 20 27 6e 65 78 74 27 20 65 6e 74 72 79 2c 20 61 'next' entry, a
186f7 73 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 65 s the previous e
186f8 6e 74 72 79 20 69 73 20 61 6c 77 61 79 73 20 61 ntry is always a
186f9 20 70 61 72 74 20 6f 66 20 74 68 65 0a 20 20 2a part of the. *
186fa 2a 20 73 75 62 2d 74 72 65 65 20 68 65 61 64 65 * sub-tree heade
186fb 64 20 62 79 20 74 68 65 20 63 68 69 6c 64 20 70 d by the child p
186fc 61 67 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 age of the cell
186fd 62 65 69 6e 67 20 64 65 6c 65 74 65 64 2e 20 54 being deleted. T
186fe 68 69 73 20 6d 61 6b 65 73 0a 20 20 2a 2a 20 62 his makes. ** b
186ff 61 6c 61 6e 63 69 6e 67 20 74 68 65 20 74 72 65 alancing the tre
18700 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 e following the
18701 64 65 6c 65 74 65 20 6f 70 65 72 61 74 69 6f 6e delete operation
18702 20 65 61 73 69 65 72 2e 20 20 2a 2f 0a 20 20 69 easier. */. i
18703 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 f( !pPage->leaf
18704 29 7b 0a 20 20 20 20 69 6e 74 20 6e 6f 74 55 73 ){. int notUs
18705 65 64 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c ed;. rc = sql
18706 69 74 65 33 42 74 72 65 65 50 72 65 76 69 6f 75 ite3BtreePreviou
18707 73 28 70 43 75 72 2c 20 26 6e 6f 74 55 73 65 64 s(pCur, ¬Used
18708 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 );. if( rc )
18709 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a return rc;. }..
1870a 20 20 2f 2a 20 53 61 76 65 20 74 68 65 20 70 6f /* Save the po
1870b 73 69 74 69 6f 6e 73 20 6f 66 20 61 6e 79 20 6f sitions of any o
1870c 74 68 65 72 20 63 75 72 73 6f 72 73 20 6f 70 65 ther cursors ope
1870d 6e 20 6f 6e 20 74 68 69 73 20 74 61 62 6c 65 20 n on this table
1870e 62 65 66 6f 72 65 0a 20 20 2a 2a 20 6d 61 6b 69 before. ** maki
1870f 6e 67 20 61 6e 79 20 6d 6f 64 69 66 69 63 61 74 ng any modificat
18710 69 6f 6e 73 2e 20 4d 61 6b 65 20 74 68 65 20 70 ions. Make the p
18711 61 67 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 age containing t
18712 68 65 20 65 6e 74 72 79 20 74 6f 20 62 65 20 0a he entry to be .
18713 20 20 2a 2a 20 64 65 6c 65 74 65 64 20 77 72 69 ** deleted wri
18714 74 61 62 6c 65 2e 20 54 68 65 6e 20 66 72 65 65 table. Then free
18715 20 61 6e 79 20 6f 76 65 72 66 6c 6f 77 20 70 61 any overflow pa
18716 67 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 ges associated w
18717 69 74 68 20 74 68 65 20 0a 20 20 2a 2a 20 65 6e ith the . ** en
18718 74 72 79 20 61 6e 64 20 66 69 6e 61 6c 6c 79 20 try and finally
18719 72 65 6d 6f 76 65 20 74 68 65 20 63 65 6c 6c 20 remove the cell
1871a 69 74 73 65 6c 66 20 66 72 6f 6d 20 77 69 74 68 itself from with
1871b 69 6e 20 74 68 65 20 70 61 67 65 2e 20 20 0a 20 in the page. .
1871c 20 2a 2f 0a 20 20 72 63 20 3d 20 73 61 76 65 41 */. rc = saveA
1871d 6c 6c 43 75 72 73 6f 72 73 28 70 42 74 2c 20 70 llCursors(pBt, p
1871e 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 70 Cur->pgnoRoot, p
1871f 43 75 72 29 3b 0a 20 20 69 66 28 20 72 63 20 29 Cur);. if( rc )
18720 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 72 63 return rc;. rc
18721 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 = sqlite3PagerW
18722 72 69 74 65 28 70 50 61 67 65 2d 3e 70 44 62 50 rite(pPage->pDbP
18723 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 20 29 age);. if( rc )
18724 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 72 63 return rc;. rc
18725 20 3d 20 63 6c 65 61 72 43 65 6c 6c 28 70 50 61 = clearCell(pPa
18726 67 65 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 64 72 ge, pCell);. dr
18727 6f 70 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 43 opCell(pPage, iC
18728 65 6c 6c 49 64 78 2c 20 63 65 6c 6c 53 69 7a 65 ellIdx, cellSize
18729 50 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c Ptr(pPage, pCell
1872a 29 2c 20 26 72 63 29 3b 0a 20 20 69 66 28 20 72 ), &rc);. if( r
1872b 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a c ) return rc;..
1872c 20 20 2f 2a 20 49 66 20 74 68 65 20 63 65 6c 6c /* If the cell
1872d 20 64 65 6c 65 74 65 64 20 77 61 73 20 6e 6f 74 deleted was not
1872e 20 6c 6f 63 61 74 65 64 20 6f 6e 20 61 20 6c 65 located on a le
1872f 61 66 20 70 61 67 65 2c 20 74 68 65 6e 20 74 68 af page, then th
18730 65 20 63 75 72 73 6f 72 0a 20 20 2a 2a 20 69 73 e cursor. ** is
18731 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 currently point
18732 69 6e 67 20 74 6f 20 74 68 65 20 6c 61 72 67 65 ing to the large
18733 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 st entry in the
18734 73 75 62 2d 74 72 65 65 20 68 65 61 64 65 64 0a sub-tree headed.
18735 20 20 2a 2a 20 62 79 20 74 68 65 20 63 68 69 6c ** by the chil
18736 64 2d 70 61 67 65 20 6f 66 20 74 68 65 20 63 65 d-page of the ce
18737 6c 6c 20 74 68 61 74 20 77 61 73 20 6a 75 73 74 ll that was just
18738 20 64 65 6c 65 74 65 64 20 66 72 6f 6d 20 61 6e deleted from an
18739 20 69 6e 74 65 72 6e 61 6c 0a 20 20 2a 2a 20 6e internal. ** n
1873a 6f 64 65 2e 20 54 68 65 20 63 65 6c 6c 20 66 72 ode. The cell fr
1873b 6f 6d 20 74 68 65 20 6c 65 61 66 20 6e 6f 64 65 om the leaf node
1873c 20 6e 65 65 64 73 20 74 6f 20 62 65 20 6d 6f 76 needs to be mov
1873d 65 64 20 74 6f 20 74 68 65 20 69 6e 74 65 72 6e ed to the intern
1873e 61 6c 0a 20 20 2a 2a 20 6e 6f 64 65 20 74 6f 20 al. ** node to
1873f 72 65 70 6c 61 63 65 20 74 68 65 20 64 65 6c 65 replace the dele
18740 74 65 64 20 63 65 6c 6c 2e 20 20 2a 2f 0a 20 20 ted cell. */.
18741 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 if( !pPage->leaf
18742 20 29 7b 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 ){. MemPage
18743 2a 70 4c 65 61 66 20 3d 20 70 43 75 72 2d 3e 61 *pLeaf = pCur->a
18744 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 pPage[pCur->iPag
18745 65 5d 3b 0a 20 20 20 20 69 6e 74 20 6e 43 65 6c e];. int nCel
18746 6c 3b 0a 20 20 20 20 50 67 6e 6f 20 6e 20 3d 20 l;. Pgno n =
18747 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 43 65 pCur->apPage[iCe
18748 6c 6c 44 65 70 74 68 2b 31 5d 2d 3e 70 67 6e 6f llDepth+1]->pgno
18749 3b 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 ;. unsigned c
1874a 68 61 72 20 2a 70 54 6d 70 3b 0a 0a 20 20 20 20 har *pTmp;..
1874b 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c pCell = findCell
1874c 28 70 4c 65 61 66 2c 20 70 4c 65 61 66 2d 3e 6e (pLeaf, pLeaf->n
1874d 43 65 6c 6c 2d 31 29 3b 0a 20 20 20 20 6e 43 65 Cell-1);. nCe
1874e 6c 6c 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 ll = cellSizePtr
1874f 28 70 4c 65 61 66 2c 20 70 43 65 6c 6c 29 3b 0a (pLeaf, pCell);.
18750 20 20 20 20 61 73 73 65 72 74 28 20 4d 58 5f 43 assert( MX_C
18751 45 4c 4c 5f 53 49 5a 45 28 70 42 74 29 3e 3d 6e ELL_SIZE(pBt)>=n
18752 43 65 6c 6c 20 29 3b 0a 0a 20 20 20 20 61 6c 6c Cell );.. all
18753 6f 63 61 74 65 54 65 6d 70 53 70 61 63 65 28 70 ocateTempSpace(p
18754 42 74 29 3b 0a 20 20 20 20 70 54 6d 70 20 3d 20 Bt);. pTmp =
18755 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 3b 0a pBt->pTmpSpace;.
18756 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
18757 33 50 61 67 65 72 57 72 69 74 65 28 70 4c 65 61 3PagerWrite(pLea
18758 66 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 f->pDbPage);.
18759 20 69 6e 73 65 72 74 43 65 6c 6c 28 70 50 61 67 insertCell(pPag
1875a 65 2c 20 69 43 65 6c 6c 49 64 78 2c 20 70 43 65 e, iCellIdx, pCe
1875b 6c 6c 2d 34 2c 20 6e 43 65 6c 6c 2b 34 2c 20 70 ll-4, nCell+4, p
1875c 54 6d 70 2c 20 6e 2c 20 26 72 63 29 3b 0a 20 20 Tmp, n, &rc);.
1875d 20 20 64 72 6f 70 43 65 6c 6c 28 70 4c 65 61 66 dropCell(pLeaf
1875e 2c 20 70 4c 65 61 66 2d 3e 6e 43 65 6c 6c 2d 31 , pLeaf->nCell-1
1875f 2c 20 6e 43 65 6c 6c 2c 20 26 72 63 29 3b 0a 20 , nCell, &rc);.
18760 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 if( rc ) retu
18761 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a rn rc;. }.. /*
18762 20 42 61 6c 61 6e 63 65 20 74 68 65 20 74 72 65 Balance the tre
18763 65 2e 20 49 66 20 74 68 65 20 65 6e 74 72 79 20 e. If the entry
18764 64 65 6c 65 74 65 64 20 77 61 73 20 6c 6f 63 61 deleted was loca
18765 74 65 64 20 6f 6e 20 61 20 6c 65 61 66 20 70 61 ted on a leaf pa
18766 67 65 2c 0a 20 20 2a 2a 20 74 68 65 6e 20 74 68 ge,. ** then th
18767 65 20 63 75 72 73 6f 72 20 73 74 69 6c 6c 20 70 e cursor still p
18768 6f 69 6e 74 73 20 74 6f 20 74 68 61 74 20 70 61 oints to that pa
18769 67 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 ge. In this case
1876a 20 74 68 65 20 66 69 72 73 74 0a 20 20 2a 2a 20 the first. **
1876b 63 61 6c 6c 20 74 6f 20 62 61 6c 61 6e 63 65 28 call to balance(
1876c 29 20 72 65 70 61 69 72 73 20 74 68 65 20 74 72 ) repairs the tr
1876d 65 65 2c 20 61 6e 64 20 74 68 65 20 69 66 28 2e ee, and the if(.
1876e 2e 2e 29 20 63 6f 6e 64 69 74 69 6f 6e 20 69 73 ..) condition is
1876f 0a 20 20 2a 2a 20 6e 65 76 65 72 20 74 72 75 65 . ** never true
18770 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4f 74 68 65 .. **. ** Othe
18771 72 77 69 73 65 2c 20 69 66 20 74 68 65 20 65 6e rwise, if the en
18772 74 72 79 20 64 65 6c 65 74 65 64 20 77 61 73 20 try deleted was
18773 6f 6e 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20 6e on an internal n
18774 6f 64 65 20 70 61 67 65 2c 20 74 68 65 6e 0a 20 ode page, then.
18775 20 2a 2a 20 70 43 75 72 20 69 73 20 70 6f 69 6e ** pCur is poin
18776 74 69 6e 67 20 74 6f 20 74 68 65 20 6c 65 61 66 ting to the leaf
18777 20 70 61 67 65 20 66 72 6f 6d 20 77 68 69 63 68 page from which
18778 20 61 20 63 65 6c 6c 20 77 61 73 20 72 65 6d 6f a cell was remo
18779 76 65 64 20 74 6f 0a 20 20 2a 2a 20 72 65 70 6c ved to. ** repl
1877a 61 63 65 20 74 68 65 20 63 65 6c 6c 20 64 65 6c ace the cell del
1877b 65 74 65 64 20 66 72 6f 6d 20 74 68 65 20 69 6e eted from the in
1877c 74 65 72 6e 61 6c 20 6e 6f 64 65 2e 20 54 68 69 ternal node. Thi
1877d 73 20 69 73 20 73 6c 69 67 68 74 6c 79 0a 20 20 s is slightly.
1877e 2a 2a 20 74 72 69 63 6b 79 20 61 73 20 74 68 65 ** tricky as the
1877f 20 6c 65 61 66 20 6e 6f 64 65 20 6d 61 79 20 62 leaf node may b
18780 65 20 75 6e 64 65 72 66 75 6c 6c 2c 20 61 6e 64 e underfull, and
18781 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 6e 6f the internal no
18782 64 65 20 6d 61 79 0a 20 20 2a 2a 20 62 65 20 65 de may. ** be e
18783 69 74 68 65 72 20 75 6e 64 65 72 20 6f 72 20 6f ither under or o
18784 76 65 72 66 75 6c 6c 2e 20 49 6e 20 74 68 69 73 verfull. In this
18785 20 63 61 73 65 20 72 75 6e 20 74 68 65 20 62 61 case run the ba
18786 6c 61 6e 63 69 6e 67 20 61 6c 67 6f 72 69 74 68 lancing algorith
18787 6d 0a 20 20 2a 2a 20 6f 6e 20 74 68 65 20 6c 65 m. ** on the le
18788 61 66 20 6e 6f 64 65 20 66 69 72 73 74 2e 20 49 af node first. I
18789 66 20 74 68 65 20 62 61 6c 61 6e 63 65 20 70 72 f the balance pr
1878a 6f 63 65 65 64 73 20 66 61 72 20 65 6e 6f 75 67 oceeds far enoug
1878b 68 20 75 70 20 74 68 65 0a 20 20 2a 2a 20 74 72 h up the. ** tr
1878c 65 65 20 74 68 61 74 20 77 65 20 63 61 6e 20 62 ee that we can b
1878d 65 20 73 75 72 65 20 74 68 61 74 20 61 6e 79 20 e sure that any
1878e 70 72 6f 62 6c 65 6d 20 69 6e 20 74 68 65 20 69 problem in the i
1878f 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 20 68 61 73 nternal node has
18790 0a 20 20 2a 2a 20 62 65 65 6e 20 63 6f 72 72 65 . ** been corre
18791 63 74 65 64 2c 20 73 6f 20 62 65 20 69 74 2e 20 cted, so be it.
18792 4f 74 68 65 72 77 69 73 65 2c 20 61 66 74 65 72 Otherwise, after
18793 20 62 61 6c 61 6e 63 69 6e 67 20 74 68 65 20 6c balancing the l
18794 65 61 66 20 6e 6f 64 65 2c 0a 20 20 2a 2a 20 77 eaf node,. ** w
18795 61 6c 6b 20 74 68 65 20 63 75 72 73 6f 72 20 75 alk the cursor u
18796 70 20 74 68 65 20 74 72 65 65 20 74 6f 20 74 68 p the tree to th
18797 65 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 20 e internal node
18798 61 6e 64 20 62 61 6c 61 6e 63 65 20 69 74 20 61 and balance it a
18799 73 20 0a 20 20 2a 2a 20 77 65 6c 6c 2e 20 20 2a s . ** well. *
1879a 2f 0a 20 20 72 63 20 3d 20 62 61 6c 61 6e 63 65 /. rc = balance
1879b 28 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72 63 (pCur);. if( rc
1879c 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 ==SQLITE_OK && p
1879d 43 75 72 2d 3e 69 50 61 67 65 3e 69 43 65 6c 6c Cur->iPage>iCell
1879e 44 65 70 74 68 20 29 7b 0a 20 20 20 20 77 68 69 Depth ){. whi
1879f 6c 65 28 20 70 43 75 72 2d 3e 69 50 61 67 65 3e le( pCur->iPage>
187a0 69 43 65 6c 6c 44 65 70 74 68 20 29 7b 0a 20 20 iCellDepth ){.
187a1 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 releasePage(
187a2 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 pCur->apPage[pCu
187a3 72 2d 3e 69 50 61 67 65 2d 2d 5d 29 3b 0a 20 20 r->iPage--]);.
187a4 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 62 61 6c }. rc = bal
187a5 61 6e 63 65 28 70 43 75 72 29 3b 0a 20 20 7d 0a ance(pCur);. }.
187a6 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
187a7 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 6d 6f 76 65 E_OK ){. move
187a8 54 6f 52 6f 6f 74 28 70 43 75 72 29 3b 0a 20 20 ToRoot(pCur);.
187a9 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
187aa 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 ../*.** Create a
187ab 20 6e 65 77 20 42 54 72 65 65 20 74 61 62 6c 65 new BTree table
187ac 2e 20 20 57 72 69 74 65 20 69 6e 74 6f 20 2a 70 . Write into *p
187ad 69 54 61 62 6c 65 20 74 68 65 20 70 61 67 65 0a iTable the page.
187ae 2a 2a 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 ** number for th
187af 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 e root page of t
187b0 68 65 20 6e 65 77 20 74 61 62 6c 65 2e 0a 2a 2a he new table..**
187b1 0a 2a 2a 20 54 68 65 20 74 79 70 65 20 6f 66 20 .** The type of
187b2 74 79 70 65 20 69 73 20 64 65 74 65 72 6d 69 6e type is determin
187b3 65 64 20 62 79 20 74 68 65 20 66 6c 61 67 73 20 ed by the flags
187b4 70 61 72 61 6d 65 74 65 72 2e 20 20 4f 6e 6c 79 parameter. Only
187b5 20 74 68 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e the.** followin
187b6 67 20 76 61 6c 75 65 73 20 6f 66 20 66 6c 61 67 g values of flag
187b7 73 20 61 72 65 20 63 75 72 72 65 6e 74 6c 79 20 s are currently
187b8 69 6e 20 75 73 65 2e 20 20 4f 74 68 65 72 20 76 in use. Other v
187b9 61 6c 75 65 73 20 66 6f 72 0a 2a 2a 20 66 6c 61 alues for.** fla
187ba 67 73 20 6d 69 67 68 74 20 6e 6f 74 20 77 6f 72 gs might not wor
187bb 6b 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 42 54 52 k:.**.** BTR
187bc 45 45 5f 49 4e 54 4b 45 59 7c 42 54 52 45 45 5f EE_INTKEY|BTREE_
187bd 4c 45 41 46 44 41 54 41 20 20 20 20 20 55 73 65 LEAFDATA Use
187be 64 20 66 6f 72 20 53 51 4c 20 74 61 62 6c 65 73 d for SQL tables
187bf 20 77 69 74 68 20 72 6f 77 69 64 20 6b 65 79 73 with rowid keys
187c0 0a 2a 2a 20 20 20 20 20 42 54 52 45 45 5f 5a 45 .** BTREE_ZE
187c1 52 4f 44 41 54 41 20 20 20 20 20 20 20 20 20 20 RODATA
187c2 20 20 20 20 20 20 20 20 55 73 65 64 20 66 6f 72 Used for
187c3 20 53 51 4c 20 69 6e 64 69 63 65 73 0a 2a 2f 0a SQL indices.*/.
187c4 73 74 61 74 69 63 20 69 6e 74 20 62 74 72 65 65 static int btree
187c5 43 72 65 61 74 65 54 61 62 6c 65 28 42 74 72 65 CreateTable(Btre
187c6 65 20 2a 70 2c 20 69 6e 74 20 2a 70 69 54 61 62 e *p, int *piTab
187c7 6c 65 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a le, int flags){.
187c8 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 BtShared *pBt
187c9 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 4d 65 6d 50 = p->pBt;. MemP
187ca 61 67 65 20 2a 70 52 6f 6f 74 3b 0a 20 20 50 67 age *pRoot;. Pg
187cb 6e 6f 20 70 67 6e 6f 52 6f 6f 74 3b 0a 20 20 69 no pgnoRoot;. i
187cc 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 nt rc;.. assert
187cd 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f ( sqlite3BtreeHo
187ce 6c 64 73 4d 75 74 65 78 28 70 29 20 29 3b 0a 20 ldsMutex(p) );.
187cf 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 69 6e assert( pBt->in
187d0 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 Transaction==TRA
187d1 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 61 73 NS_WRITE );. as
187d2 73 65 72 74 28 20 21 70 42 74 2d 3e 72 65 61 64 sert( !pBt->read
187d3 4f 6e 6c 79 20 29 3b 0a 0a 23 69 66 64 65 66 20 Only );..#ifdef
187d4 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f SQLITE_OMIT_AUTO
187d5 56 41 43 55 55 4d 0a 20 20 72 63 20 3d 20 61 6c VACUUM. rc = al
187d6 6c 6f 63 61 74 65 42 74 72 65 65 50 61 67 65 28 locateBtreePage(
187d7 70 42 74 2c 20 26 70 52 6f 6f 74 2c 20 26 70 67 pBt, &pRoot, &pg
187d8 6e 6f 52 6f 6f 74 2c 20 31 2c 20 30 29 3b 0a 20 noRoot, 1, 0);.
187d9 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 72 if( rc ){. r
187da 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 23 65 eturn rc;. }.#e
187db 6c 73 65 0a 20 20 69 66 28 20 70 42 74 2d 3e 61 lse. if( pBt->a
187dc 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 utoVacuum ){.
187dd 20 50 67 6e 6f 20 70 67 6e 6f 4d 6f 76 65 3b 20 Pgno pgnoMove;
187de 20 20 20 20 20 2f 2a 20 4d 6f 76 65 20 61 20 70 /* Move a p
187df 61 67 65 20 68 65 72 65 20 74 6f 20 6d 61 6b 65 age here to make
187e0 20 72 6f 6f 6d 20 66 6f 72 20 74 68 65 20 72 6f room for the ro
187e1 6f 74 2d 70 61 67 65 20 2a 2f 0a 20 20 20 20 4d ot-page */. M
187e2 65 6d 50 61 67 65 20 2a 70 50 61 67 65 4d 6f 76 emPage *pPageMov
187e3 65 3b 20 2f 2a 20 54 68 65 20 70 61 67 65 20 74 e; /* The page t
187e4 6f 20 6d 6f 76 65 20 74 6f 2e 20 2a 2f 0a 0a 20 o move to. */..
187e5 20 20 20 2f 2a 20 43 72 65 61 74 69 6e 67 20 61 /* Creating a
187e6 20 6e 65 77 20 74 61 62 6c 65 20 6d 61 79 20 70 new table may p
187e7 72 6f 62 61 62 6c 79 20 72 65 71 75 69 72 65 20 robably require
187e8 6d 6f 76 69 6e 67 20 61 6e 20 65 78 69 73 74 69 moving an existi
187e9 6e 67 20 64 61 74 61 62 61 73 65 0a 20 20 20 20 ng database.
187ea 2a 2a 20 74 6f 20 6d 61 6b 65 20 72 6f 6f 6d 20 ** to make room
187eb 66 6f 72 20 74 68 65 20 6e 65 77 20 74 61 62 6c for the new tabl
187ec 65 73 20 72 6f 6f 74 20 70 61 67 65 2e 20 49 6e es root page. In
187ed 20 63 61 73 65 20 74 68 69 73 20 70 61 67 65 20 case this page
187ee 74 75 72 6e 73 0a 20 20 20 20 2a 2a 20 6f 75 74 turns. ** out
187ef 20 74 6f 20 62 65 20 61 6e 20 6f 76 65 72 66 6c to be an overfl
187f0 6f 77 20 70 61 67 65 2c 20 64 65 6c 65 74 65 20 ow page, delete
187f1 61 6c 6c 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 all overflow pag
187f2 65 2d 6d 61 70 20 63 61 63 68 65 73 0a 20 20 20 e-map caches.
187f3 20 2a 2a 20 68 65 6c 64 20 62 79 20 6f 70 65 6e ** held by open
187f4 20 63 75 72 73 6f 72 73 2e 0a 20 20 20 20 2a 2f cursors.. */
187f5 0a 20 20 20 20 69 6e 76 61 6c 69 64 61 74 65 41 . invalidateA
187f6 6c 6c 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28 llOverflowCache(
187f7 70 42 74 29 3b 0a 0a 20 20 20 20 2f 2a 20 52 65 pBt);.. /* Re
187f8 61 64 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 ad the value of
187f9 6d 65 74 61 5b 33 5d 20 66 72 6f 6d 20 74 68 65 meta[3] from the
187fa 20 64 61 74 61 62 61 73 65 20 74 6f 20 64 65 74 database to det
187fb 65 72 6d 69 6e 65 20 77 68 65 72 65 20 74 68 65 ermine where the
187fc 0a 20 20 20 20 2a 2a 20 72 6f 6f 74 20 70 61 67 . ** root pag
187fd 65 20 6f 66 20 74 68 65 20 6e 65 77 20 74 61 62 e of the new tab
187fe 6c 65 20 73 68 6f 75 6c 64 20 67 6f 2e 20 6d 65 le should go. me
187ff 74 61 5b 33 5d 20 69 73 20 74 68 65 20 6c 61 72 ta[3] is the lar
18800 67 65 73 74 20 72 6f 6f 74 2d 70 61 67 65 0a 20 gest root-page.
18801 20 20 20 2a 2a 20 63 72 65 61 74 65 64 20 73 6f ** created so
18802 20 66 61 72 2c 20 73 6f 20 74 68 65 20 6e 65 77 far, so the new
18803 20 72 6f 6f 74 2d 70 61 67 65 20 69 73 20 28 6d root-page is (m
18804 65 74 61 5b 33 5d 2b 31 29 2e 0a 20 20 20 20 2a eta[3]+1).. *
18805 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 /. sqlite3Btr
18806 65 65 47 65 74 4d 65 74 61 28 70 2c 20 42 54 52 eeGetMeta(p, BTR
18807 45 45 5f 4c 41 52 47 45 53 54 5f 52 4f 4f 54 5f EE_LARGEST_ROOT_
18808 50 41 47 45 2c 20 26 70 67 6e 6f 52 6f 6f 74 29 PAGE, &pgnoRoot)
18809 3b 0a 20 20 20 20 70 67 6e 6f 52 6f 6f 74 2b 2b ;. pgnoRoot++
1880a 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 ;.. /* The ne
1880b 77 20 72 6f 6f 74 2d 70 61 67 65 20 6d 61 79 20 w root-page may
1880c 6e 6f 74 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 not be allocated
1880d 20 6f 6e 20 61 20 70 6f 69 6e 74 65 72 2d 6d 61 on a pointer-ma
1880e 70 20 70 61 67 65 2c 20 6f 72 20 74 68 65 0a 20 p page, or the.
1880f 20 20 20 2a 2a 20 50 45 4e 44 49 4e 47 5f 42 59 ** PENDING_BY
18810 54 45 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f 0a TE page.. */.
18811 20 20 20 20 77 68 69 6c 65 28 20 70 67 6e 6f 52 while( pgnoR
18812 6f 6f 74 3d 3d 50 54 52 4d 41 50 5f 50 41 47 45 oot==PTRMAP_PAGE
18813 4e 4f 28 70 42 74 2c 20 70 67 6e 6f 52 6f 6f 74 NO(pBt, pgnoRoot
18814 29 20 7c 7c 0a 20 20 20 20 20 20 20 20 70 67 6e ) ||. pgn
18815 6f 52 6f 6f 74 3d 3d 50 45 4e 44 49 4e 47 5f 42 oRoot==PENDING_B
18816 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 7b YTE_PAGE(pBt) ){
18817 0a 20 20 20 20 20 20 70 67 6e 6f 52 6f 6f 74 2b . pgnoRoot+
18818 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 +;. }. ass
18819 65 72 74 28 20 70 67 6e 6f 52 6f 6f 74 3e 3d 33 ert( pgnoRoot>=3
1881a 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 41 6c 6c 6f );.. /* Allo
1881b 63 61 74 65 20 61 20 70 61 67 65 2e 20 54 68 65 cate a page. The
1881c 20 70 61 67 65 20 74 68 61 74 20 63 75 72 72 65 page that curre
1881d 6e 74 6c 79 20 72 65 73 69 64 65 73 20 61 74 20 ntly resides at
1881e 70 67 6e 6f 52 6f 6f 74 20 77 69 6c 6c 0a 20 20 pgnoRoot will.
1881f 20 20 2a 2a 20 62 65 20 6d 6f 76 65 64 20 74 6f ** be moved to
18820 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 70 the allocated p
18821 61 67 65 20 28 75 6e 6c 65 73 73 20 74 68 65 20 age (unless the
18822 61 6c 6c 6f 63 61 74 65 64 20 70 61 67 65 20 68 allocated page h
18823 61 70 70 65 6e 73 0a 20 20 20 20 2a 2a 20 74 6f appens. ** to
18824 20 72 65 73 69 64 65 20 61 74 20 70 67 6e 6f 52 reside at pgnoR
18825 6f 6f 74 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 oot).. */.
18826 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74 rc = allocateBt
18827 72 65 65 50 61 67 65 28 70 42 74 2c 20 26 70 50 reePage(pBt, &pP
18828 61 67 65 4d 6f 76 65 2c 20 26 70 67 6e 6f 4d 6f ageMove, &pgnoMo
18829 76 65 2c 20 70 67 6e 6f 52 6f 6f 74 2c 20 31 29 ve, pgnoRoot, 1)
1882a 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 ;. if( rc!=SQ
1882b 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
1882c 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
1882d 7d 0a 0a 20 20 20 20 69 66 28 20 70 67 6e 6f 4d }.. if( pgnoM
1882e 6f 76 65 21 3d 70 67 6e 6f 52 6f 6f 74 20 29 7b ove!=pgnoRoot ){
1882f 0a 20 20 20 20 20 20 2f 2a 20 70 67 6e 6f 52 6f . /* pgnoRo
18830 6f 74 20 69 73 20 74 68 65 20 70 61 67 65 20 74 ot is the page t
18831 68 61 74 20 77 69 6c 6c 20 62 65 20 75 73 65 64 hat will be used
18832 20 66 6f 72 20 74 68 65 20 72 6f 6f 74 2d 70 61 for the root-pa
18833 67 65 20 6f 66 0a 20 20 20 20 20 20 2a 2a 20 74 ge of. ** t
18834 68 65 20 6e 65 77 20 74 61 62 6c 65 20 28 61 73 he new table (as
18835 73 75 6d 69 6e 67 20 61 6e 20 65 72 72 6f 72 20 suming an error
18836 64 69 64 20 6e 6f 74 20 6f 63 63 75 72 29 2e 20 did not occur).
18837 42 75 74 20 77 65 20 77 65 72 65 0a 20 20 20 20 But we were.
18838 20 20 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 70 ** allocated p
18839 67 6e 6f 4d 6f 76 65 2e 20 49 66 20 72 65 71 75 gnoMove. If requ
1883a 69 72 65 64 20 28 69 2e 65 2e 20 69 66 20 69 74 ired (i.e. if it
1883b 20 77 61 73 20 6e 6f 74 20 61 6c 6c 6f 63 61 74 was not allocat
1883c 65 64 0a 20 20 20 20 20 20 2a 2a 20 62 79 20 65 ed. ** by e
1883d 78 74 65 6e 64 69 6e 67 20 74 68 65 20 66 69 6c xtending the fil
1883e 65 29 2c 20 74 68 65 20 63 75 72 72 65 6e 74 20 e), the current
1883f 70 61 67 65 20 61 74 20 70 6f 73 69 74 69 6f 6e page at position
18840 20 70 67 6e 6f 4d 6f 76 65 0a 20 20 20 20 20 20 pgnoMove.
18841 2a 2a 20 69 73 20 61 6c 72 65 61 64 79 20 6a 6f ** is already jo
18842 75 72 6e 61 6c 65 64 2e 0a 20 20 20 20 20 20 2a urnaled.. *
18843 2f 0a 20 20 20 20 20 20 75 38 20 65 54 79 70 65 /. u8 eType
18844 20 3d 20 30 3b 0a 20 20 20 20 20 20 50 67 6e 6f = 0;. Pgno
18845 20 69 50 74 72 50 61 67 65 20 3d 20 30 3b 0a 0a iPtrPage = 0;..
18846 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 releasePag
18847 65 28 70 50 61 67 65 4d 6f 76 65 29 3b 0a 0a 20 e(pPageMove);..
18848 20 20 20 20 20 2f 2a 20 4d 6f 76 65 20 74 68 65 /* Move the
18849 20 70 61 67 65 20 63 75 72 72 65 6e 74 6c 79 20 page currently
1884a 61 74 20 70 67 6e 6f 52 6f 6f 74 20 74 6f 20 70 at pgnoRoot to p
1884b 67 6e 6f 4d 6f 76 65 2e 20 2a 2f 0a 20 20 20 20 gnoMove. */.
1884c 20 20 72 63 20 3d 20 62 74 72 65 65 47 65 74 50 rc = btreeGetP
1884d 61 67 65 28 70 42 74 2c 20 70 67 6e 6f 52 6f 6f age(pBt, pgnoRoo
1884e 74 2c 20 26 70 52 6f 6f 74 2c 20 30 29 3b 0a 20 t, &pRoot, 0);.
1884f 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c if( rc!=SQL
18850 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
18851 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 return rc;.
18852 20 20 20 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 }. rc =
18853 70 74 72 6d 61 70 47 65 74 28 70 42 74 2c 20 70 ptrmapGet(pBt, p
18854 67 6e 6f 52 6f 6f 74 2c 20 26 65 54 79 70 65 2c gnoRoot, &eType,
18855 20 26 69 50 74 72 50 61 67 65 29 3b 0a 20 20 20 &iPtrPage);.
18856 20 20 20 69 66 28 20 65 54 79 70 65 3d 3d 50 54 if( eType==PT
18857 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 20 7c 7c RMAP_ROOTPAGE ||
18858 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 46 eType==PTRMAP_F
18859 52 45 45 50 41 47 45 20 29 7b 0a 20 20 20 20 20 REEPAGE ){.
1885a 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 rc = SQLITE_C
1885b 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 ORRUPT_BKPT;.
1885c 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 72 }. if( r
1885d 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
1885e 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 releaseP
1885f 61 67 65 28 70 52 6f 6f 74 29 3b 0a 20 20 20 20 age(pRoot);.
18860 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
18861 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73 }. ass
18862 65 72 74 28 20 65 54 79 70 65 21 3d 50 54 52 4d ert( eType!=PTRM
18863 41 50 5f 52 4f 4f 54 50 41 47 45 20 29 3b 0a 20 AP_ROOTPAGE );.
18864 20 20 20 20 20 61 73 73 65 72 74 28 20 65 54 79 assert( eTy
18865 70 65 21 3d 50 54 52 4d 41 50 5f 46 52 45 45 50 pe!=PTRMAP_FREEP
18866 41 47 45 20 29 3b 0a 20 20 20 20 20 20 72 63 20 AGE );. rc
18867 3d 20 72 65 6c 6f 63 61 74 65 50 61 67 65 28 70 = relocatePage(p
18868 42 74 2c 20 70 52 6f 6f 74 2c 20 65 54 79 70 65 Bt, pRoot, eType
18869 2c 20 69 50 74 72 50 61 67 65 2c 20 70 67 6e 6f , iPtrPage, pgno
1886a 4d 6f 76 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 Move, 0);.
1886b 72 65 6c 65 61 73 65 50 61 67 65 28 70 52 6f 6f releasePage(pRoo
1886c 74 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 4f 62 t);.. /* Ob
1886d 74 61 69 6e 20 74 68 65 20 70 61 67 65 20 61 74 tain the page at
1886e 20 70 67 6e 6f 52 6f 6f 74 20 2a 2f 0a 20 20 20 pgnoRoot */.
1886f 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
18870 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
18871 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 return rc;.
18872 20 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 62 74 }. rc = bt
18873 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 reeGetPage(pBt,
18874 70 67 6e 6f 52 6f 6f 74 2c 20 26 70 52 6f 6f 74 pgnoRoot, &pRoot
18875 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 , 0);. if(
18876 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
18877 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 . return
18878 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 rc;. }.
18879 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
1887a 67 65 72 57 72 69 74 65 28 70 52 6f 6f 74 2d 3e gerWrite(pRoot->
1887b 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 pDbPage);.
1887c 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
1887d 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 6c K ){. rel
1887e 65 61 73 65 50 61 67 65 28 70 52 6f 6f 74 29 3b easePage(pRoot);
1887f 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 . return
18880 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 rc;. }.
18881 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 52 6f }else{. pRo
18882 6f 74 20 3d 20 70 50 61 67 65 4d 6f 76 65 3b 0a ot = pPageMove;.
18883 20 20 20 20 7d 20 0a 0a 20 20 20 20 2f 2a 20 55 } .. /* U
18884 70 64 61 74 65 20 74 68 65 20 70 6f 69 6e 74 65 pdate the pointe
18885 72 2d 6d 61 70 20 61 6e 64 20 6d 65 74 61 2d 64 r-map and meta-d
18886 61 74 61 20 77 69 74 68 20 74 68 65 20 6e 65 77 ata with the new
18887 20 72 6f 6f 74 2d 70 61 67 65 20 6e 75 6d 62 65 root-page numbe
18888 72 2e 20 2a 2f 0a 20 20 20 20 70 74 72 6d 61 70 r. */. ptrmap
18889 50 75 74 28 70 42 74 2c 20 70 67 6e 6f 52 6f 6f Put(pBt, pgnoRoo
1888a 74 2c 20 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 t, PTRMAP_ROOTPA
1888b 47 45 2c 20 30 2c 20 26 72 63 29 3b 0a 20 20 20 GE, 0, &rc);.
1888c 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 if( rc ){.
1888d 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 52 6f releasePage(pRo
1888e 6f 74 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 ot);. retur
1888f 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 n rc;. }.
18890 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 rc = sqlite3Btre
18891 65 55 70 64 61 74 65 4d 65 74 61 28 70 2c 20 34 eUpdateMeta(p, 4
18892 2c 20 70 67 6e 6f 52 6f 6f 74 29 3b 0a 20 20 20 , pgnoRoot);.
18893 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 if( rc ){.
18894 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 52 6f releasePage(pRo
18895 6f 74 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 ot);. retur
18896 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 7d n rc;. }.. }
18897 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 61 else{. rc = a
18898 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61 67 65 llocateBtreePage
18899 28 70 42 74 2c 20 26 70 52 6f 6f 74 2c 20 26 70 (pBt, &pRoot, &p
1889a 67 6e 6f 52 6f 6f 74 2c 20 31 2c 20 30 29 3b 0a gnoRoot, 1, 0);.
1889b 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 if( rc ) ret
1889c 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 23 65 6e 64 urn rc;. }.#end
1889d 69 66 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c if. assert( sql
1889e 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 ite3PagerIswrite
1889f 61 62 6c 65 28 70 52 6f 6f 74 2d 3e 70 44 62 50 able(pRoot->pDbP
188a0 61 67 65 29 20 29 3b 0a 20 20 7a 65 72 6f 50 61 age) );. zeroPa
188a1 67 65 28 70 52 6f 6f 74 2c 20 66 6c 61 67 73 20 ge(pRoot, flags
188a2 7c 20 50 54 46 5f 4c 45 41 46 29 3b 0a 20 20 73 | PTF_LEAF);. s
188a3 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 qlite3PagerUnref
188a4 28 70 52 6f 6f 74 2d 3e 70 44 62 50 61 67 65 29 (pRoot->pDbPage)
188a5 3b 0a 20 20 2a 70 69 54 61 62 6c 65 20 3d 20 28 ;. *piTable = (
188a6 69 6e 74 29 70 67 6e 6f 52 6f 6f 74 3b 0a 20 20 int)pgnoRoot;.
188a7 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
188a8 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ;.}.SQLITE_PRIVA
188a9 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
188aa 72 65 65 43 72 65 61 74 65 54 61 62 6c 65 28 42 reeCreateTable(B
188ab 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 2a 70 69 tree *p, int *pi
188ac 54 61 62 6c 65 2c 20 69 6e 74 20 66 6c 61 67 73 Table, int flags
188ad 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 ){. int rc;. s
188ae 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 qlite3BtreeEnter
188af 28 70 29 3b 0a 20 20 72 63 20 3d 20 62 74 72 65 (p);. rc = btre
188b0 65 43 72 65 61 74 65 54 61 62 6c 65 28 70 2c 20 eCreateTable(p,
188b1 70 69 54 61 62 6c 65 2c 20 66 6c 61 67 73 29 3b piTable, flags);
188b2 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c . sqlite3BtreeL
188b3 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 eave(p);. retur
188b4 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 n rc;.}../*.** E
188b5 72 61 73 65 20 74 68 65 20 67 69 76 65 6e 20 64 rase the given d
188b6 61 74 61 62 61 73 65 20 70 61 67 65 20 61 6e 64 atabase page and
188b7 20 61 6c 6c 20 69 74 73 20 63 68 69 6c 64 72 65 all its childre
188b8 6e 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 74 68 n. Return.** th
188b9 65 20 70 61 67 65 20 74 6f 20 74 68 65 20 66 72 e page to the fr
188ba 65 65 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 eelist..*/.stati
188bb 63 20 69 6e 74 20 63 6c 65 61 72 44 61 74 61 62 c int clearDatab
188bc 61 73 65 50 61 67 65 28 0a 20 20 42 74 53 68 61 asePage(. BtSha
188bd 72 65 64 20 2a 70 42 74 2c 20 20 20 20 20 20 20 red *pBt,
188be 20 20 20 20 2f 2a 20 54 68 65 20 42 54 72 65 65 /* The BTree
188bf 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 that contains t
188c0 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 50 67 he table */. Pg
188c1 6e 6f 20 70 67 6e 6f 2c 20 20 20 20 20 20 20 20 no pgno,
188c2 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 /* Page numb
188c3 65 72 20 74 6f 20 63 6c 65 61 72 20 2a 2f 0a 20 er to clear */.
188c4 20 69 6e 74 20 66 72 65 65 50 61 67 65 46 6c 61 int freePageFla
188c5 67 2c 20 20 20 20 20 2f 2a 20 44 65 61 6c 6c 6f g, /* Deallo
188c6 63 61 74 65 20 70 61 67 65 20 69 66 20 74 72 75 cate page if tru
188c7 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 43 68 e */. int *pnCh
188c8 61 6e 67 65 0a 29 7b 0a 20 20 4d 65 6d 50 61 67 ange.){. MemPag
188c9 65 20 2a 70 50 61 67 65 3b 0a 20 20 69 6e 74 20 e *pPage;. int
188ca 72 63 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 rc;. unsigned c
188cb 68 61 72 20 2a 70 43 65 6c 6c 3b 0a 20 20 69 6e har *pCell;. in
188cc 74 20 69 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 t i;.. assert(
188cd 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
188ce 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 ld(pBt->mutex) )
188cf 3b 0a 20 20 69 66 28 20 70 67 6e 6f 3e 70 61 67 ;. if( pgno>pag
188d0 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29 erPagecount(pBt)
188d1 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
188d2 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b QLITE_CORRUPT_BK
188d3 50 54 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 PT;. }.. rc =
188d4 67 65 74 41 6e 64 49 6e 69 74 50 61 67 65 28 70 getAndInitPage(p
188d5 42 74 2c 20 70 67 6e 6f 2c 20 26 70 50 61 67 65 Bt, pgno, &pPage
188d6 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 );. if( rc ) re
188d7 74 75 72 6e 20 72 63 3b 0a 20 20 66 6f 72 28 69 turn rc;. for(i
188d8 3d 30 3b 20 69 3c 70 50 61 67 65 2d 3e 6e 43 65 =0; i<pPage->nCe
188d9 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 43 ll; i++){. pC
188da 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 ell = findCell(p
188db 50 61 67 65 2c 20 69 29 3b 0a 20 20 20 20 69 66 Page, i);. if
188dc 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 ( !pPage->leaf )
188dd 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 63 6c 65 {. rc = cle
188de 61 72 44 61 74 61 62 61 73 65 50 61 67 65 28 70 arDatabasePage(p
188df 42 74 2c 20 67 65 74 34 62 79 74 65 28 70 43 65 Bt, get4byte(pCe
188e0 6c 6c 29 2c 20 31 2c 20 70 6e 43 68 61 6e 67 65 ll), 1, pnChange
188e1 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 );. if( rc
188e2 29 20 67 6f 74 6f 20 63 6c 65 61 72 64 61 74 61 ) goto cleardata
188e3 62 61 73 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 basepage_out;.
188e4 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 63 6c 65 }. rc = cle
188e5 61 72 43 65 6c 6c 28 70 50 61 67 65 2c 20 70 43 arCell(pPage, pC
188e6 65 6c 6c 29 3b 0a 20 20 20 20 69 66 28 20 72 63 ell);. if( rc
188e7 20 29 20 67 6f 74 6f 20 63 6c 65 61 72 64 61 74 ) goto cleardat
188e8 61 62 61 73 65 70 61 67 65 5f 6f 75 74 3b 0a 20 abasepage_out;.
188e9 20 7d 0a 20 20 69 66 28 20 21 70 50 61 67 65 2d }. if( !pPage-
188ea 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 72 63 20 >leaf ){. rc
188eb 3d 20 63 6c 65 61 72 44 61 74 61 62 61 73 65 50 = clearDatabaseP
188ec 61 67 65 28 70 42 74 2c 20 67 65 74 34 62 79 74 age(pBt, get4byt
188ed 65 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b e(&pPage->aData[
188ee 38 5d 29 2c 20 31 2c 20 70 6e 43 68 61 6e 67 65 8]), 1, pnChange
188ef 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 );. if( rc )
188f0 67 6f 74 6f 20 63 6c 65 61 72 64 61 74 61 62 61 goto cleardataba
188f1 73 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 7d 65 sepage_out;. }e
188f2 6c 73 65 20 69 66 28 20 70 6e 43 68 61 6e 67 65 lse if( pnChange
188f3 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
188f4 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 3b pPage->intKey );
188f5 0a 20 20 20 20 2a 70 6e 43 68 61 6e 67 65 20 2b . *pnChange +
188f6 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a = pPage->nCell;.
188f7 20 20 7d 0a 20 20 69 66 28 20 66 72 65 65 50 61 }. if( freePa
188f8 67 65 46 6c 61 67 20 29 7b 0a 20 20 20 20 66 72 geFlag ){. fr
188f9 65 65 50 61 67 65 28 70 50 61 67 65 2c 20 26 72 eePage(pPage, &r
188fa 63 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 c);. }else if(
188fb 28 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 (rc = sqlite3Pag
188fc 65 72 57 72 69 74 65 28 70 50 61 67 65 2d 3e 70 erWrite(pPage->p
188fd 44 62 50 61 67 65 29 29 3d 3d 30 20 29 7b 0a 20 DbPage))==0 ){.
188fe 20 20 20 7a 65 72 6f 50 61 67 65 28 70 50 61 67 zeroPage(pPag
188ff 65 2c 20 70 50 61 67 65 2d 3e 61 44 61 74 61 5b e, pPage->aData[
18900 30 5d 20 7c 20 50 54 46 5f 4c 45 41 46 29 3b 0a 0] | PTF_LEAF);.
18901 20 20 7d 0a 0a 63 6c 65 61 72 64 61 74 61 62 61 }..cleardataba
18902 73 65 70 61 67 65 5f 6f 75 74 3a 0a 20 20 72 65 sepage_out:. re
18903 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65 29 leasePage(pPage)
18904 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
18905 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 ../*.** Delete a
18906 6c 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 ll information f
18907 72 6f 6d 20 61 20 73 69 6e 67 6c 65 20 74 61 62 rom a single tab
18908 6c 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 le in the databa
18909 73 65 2e 20 20 69 54 61 62 6c 65 20 69 73 0a 2a se. iTable is.*
1890a 2a 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 * the page numbe
1890b 72 20 6f 66 20 74 68 65 20 72 6f 6f 74 20 6f 66 r of the root of
1890c 20 74 68 65 20 74 61 62 6c 65 2e 20 20 41 66 74 the table. Aft
1890d 65 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 er this routine
1890e 72 65 74 75 72 6e 73 2c 0a 2a 2a 20 74 68 65 20 returns,.** the
1890f 72 6f 6f 74 20 70 61 67 65 20 69 73 20 65 6d 70 root page is emp
18910 74 79 2c 20 62 75 74 20 73 74 69 6c 6c 20 65 78 ty, but still ex
18911 69 73 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 ists..**.** This
18912 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 66 61 routine will fa
18913 69 6c 20 77 69 74 68 20 53 51 4c 49 54 45 5f 4c il with SQLITE_L
18914 4f 43 4b 45 44 20 69 66 20 74 68 65 72 65 20 61 OCKED if there a
18915 72 65 20 61 6e 79 20 6f 70 65 6e 0a 2a 2a 20 72 re any open.** r
18916 65 61 64 20 63 75 72 73 6f 72 73 20 6f 6e 20 74 ead cursors on t
18917 68 65 20 74 61 62 6c 65 2e 20 20 4f 70 65 6e 20 he table. Open
18918 77 72 69 74 65 20 63 75 72 73 6f 72 73 20 61 72 write cursors ar
18919 65 20 6d 6f 76 65 64 20 74 6f 20 74 68 65 0a 2a e moved to the.*
1891a 2a 20 72 6f 6f 74 20 6f 66 20 74 68 65 20 74 61 * root of the ta
1891b 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 6e ble..**.** If pn
1891c 43 68 61 6e 67 65 20 69 73 20 6e 6f 74 20 4e 55 Change is not NU
1891d 4c 4c 2c 20 74 68 65 6e 20 74 61 62 6c 65 20 69 LL, then table i
1891e 54 61 62 6c 65 20 6d 75 73 74 20 62 65 20 61 6e Table must be an
1891f 20 69 6e 74 6b 65 79 20 74 61 62 6c 65 2e 20 54 intkey table. T
18920 68 65 0a 2a 2a 20 69 6e 74 65 67 65 72 20 76 61 he.** integer va
18921 6c 75 65 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 lue pointed to b
18922 79 20 70 6e 43 68 61 6e 67 65 20 69 73 20 69 6e y pnChange is in
18923 63 72 65 6d 65 6e 74 65 64 20 62 79 20 74 68 65 cremented by the
18924 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 65 6e number of.** en
18925 74 72 69 65 73 20 69 6e 20 74 68 65 20 74 61 62 tries in the tab
18926 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 le..*/.SQLITE_PR
18927 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
18928 33 42 74 72 65 65 43 6c 65 61 72 54 61 62 6c 65 3BtreeClearTable
18929 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 69 (Btree *p, int i
1892a 54 61 62 6c 65 2c 20 69 6e 74 20 2a 70 6e 43 68 Table, int *pnCh
1892b 61 6e 67 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b ange){. int rc;
1892c 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 . BtShared *pBt
1892d 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 73 71 6c = p->pBt;. sql
1892e 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 ite3BtreeEnter(p
1892f 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e );. assert( p->
18930 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 inTrans==TRANS_W
18931 52 49 54 45 20 29 3b 0a 0a 20 20 2f 2a 20 49 6e RITE );.. /* In
18932 76 61 6c 69 64 61 74 65 20 61 6c 6c 20 69 6e 63 validate all inc
18933 72 62 6c 6f 62 20 63 75 72 73 6f 72 73 20 6f 70 rblob cursors op
18934 65 6e 20 6f 6e 20 74 61 62 6c 65 20 69 54 61 62 en on table iTab
18935 6c 65 20 28 61 73 73 75 6d 69 6e 67 20 69 54 61 le (assuming iTa
18936 62 6c 65 0a 20 20 2a 2a 20 69 73 20 74 68 65 20 ble. ** is the
18937 72 6f 6f 74 20 6f 66 20 61 20 74 61 62 6c 65 20 root of a table
18938 62 2d 74 72 65 65 20 2d 20 69 66 20 69 74 20 69 b-tree - if it i
18939 73 20 6e 6f 74 2c 20 74 68 65 20 66 6f 6c 6c 6f s not, the follo
1893a 77 69 6e 67 20 63 61 6c 6c 20 69 73 0a 20 20 2a wing call is. *
1893b 2a 20 61 20 6e 6f 2d 6f 70 29 2e 20 20 2a 2f 0a * a no-op). */.
1893c 20 20 69 6e 76 61 6c 69 64 61 74 65 49 6e 63 72 invalidateIncr
1893d 62 6c 6f 62 43 75 72 73 6f 72 73 28 70 2c 20 30 blobCursors(p, 0
1893e 2c 20 31 29 3b 0a 0a 20 20 72 63 20 3d 20 73 61 , 1);.. rc = sa
1893f 76 65 41 6c 6c 43 75 72 73 6f 72 73 28 70 42 74 veAllCursors(pBt
18940 2c 20 28 50 67 6e 6f 29 69 54 61 62 6c 65 2c 20 , (Pgno)iTable,
18941 30 29 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 45 0);. if( SQLITE
18942 5f 4f 4b 3d 3d 72 63 20 29 7b 0a 20 20 20 20 72 _OK==rc ){. r
18943 63 20 3d 20 63 6c 65 61 72 44 61 74 61 62 61 73 c = clearDatabas
18944 65 50 61 67 65 28 70 42 74 2c 20 28 50 67 6e 6f ePage(pBt, (Pgno
18945 29 69 54 61 62 6c 65 2c 20 30 2c 20 70 6e 43 68 )iTable, 0, pnCh
18946 61 6e 67 65 29 3b 0a 20 20 7d 0a 20 20 73 71 6c ange);. }. sql
18947 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 ite3BtreeLeave(p
18948 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a );. return rc;.
18949 7d 0a 0a 2f 2a 0a 2a 2a 20 45 72 61 73 65 20 61 }../*.** Erase a
1894a 6c 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 ll information i
1894b 6e 20 61 20 74 61 62 6c 65 20 61 6e 64 20 61 64 n a table and ad
1894c 64 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 74 68 d the root of th
1894d 65 20 74 61 62 6c 65 20 74 6f 0a 2a 2a 20 74 68 e table to.** th
1894e 65 20 66 72 65 65 6c 69 73 74 2e 20 20 45 78 63 e freelist. Exc
1894f 65 70 74 2c 20 74 68 65 20 72 6f 6f 74 20 6f 66 ept, the root of
18950 20 74 68 65 20 70 72 69 6e 63 69 70 6c 65 20 74 the principle t
18951 61 62 6c 65 20 28 74 68 65 20 6f 6e 65 20 6f 6e able (the one on
18952 0a 2a 2a 20 70 61 67 65 20 31 29 20 69 73 20 6e .** page 1) is n
18953 65 76 65 72 20 61 64 64 65 64 20 74 6f 20 74 68 ever added to th
18954 65 20 66 72 65 65 6c 69 73 74 2e 0a 2a 2a 0a 2a e freelist..**.*
18955 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 * This routine w
18956 69 6c 6c 20 66 61 69 6c 20 77 69 74 68 20 53 51 ill fail with SQ
18957 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 69 66 20 74 LITE_LOCKED if t
18958 68 65 72 65 20 61 72 65 20 61 6e 79 20 6f 70 65 here are any ope
18959 6e 0a 2a 2a 20 63 75 72 73 6f 72 73 20 6f 6e 20 n.** cursors on
1895a 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a the table..**.**
1895b 20 49 66 20 41 55 54 4f 56 41 43 55 55 4d 20 69 If AUTOVACUUM i
1895c 73 20 65 6e 61 62 6c 65 64 20 61 6e 64 20 74 68 s enabled and th
1895d 65 20 70 61 67 65 20 61 74 20 69 54 61 62 6c 65 e page at iTable
1895e 20 69 73 20 6e 6f 74 20 74 68 65 20 6c 61 73 74 is not the last
1895f 0a 2a 2a 20 72 6f 6f 74 20 70 61 67 65 20 69 6e .** root page in
18960 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
18961 6c 65 2c 20 74 68 65 6e 20 74 68 65 20 6c 61 73 le, then the las
18962 74 20 72 6f 6f 74 20 70 61 67 65 20 0a 2a 2a 20 t root page .**
18963 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 in the database
18964 66 69 6c 65 20 69 73 20 6d 6f 76 65 64 20 69 6e file is moved in
18965 74 6f 20 74 68 65 20 73 6c 6f 74 20 66 6f 72 6d to the slot form
18966 65 72 6c 79 20 6f 63 63 75 70 69 65 64 20 62 79 erly occupied by
18967 0a 2a 2a 20 69 54 61 62 6c 65 20 61 6e 64 20 74 .** iTable and t
18968 68 61 74 20 6c 61 73 74 20 73 6c 6f 74 20 66 6f hat last slot fo
18969 72 6d 65 72 6c 79 20 6f 63 63 75 70 69 65 64 20 rmerly occupied
1896a 62 79 20 74 68 65 20 6c 61 73 74 20 72 6f 6f 74 by the last root
1896b 20 70 61 67 65 0a 2a 2a 20 69 73 20 61 64 64 65 page.** is adde
1896c 64 20 74 6f 20 74 68 65 20 66 72 65 65 6c 69 73 d to the freelis
1896d 74 20 69 6e 73 74 65 61 64 20 6f 66 20 69 54 61 t instead of iTa
1896e 62 6c 65 2e 20 20 49 6e 20 74 68 69 73 20 73 61 ble. In this sa
1896f 79 2c 20 61 6c 6c 0a 2a 2a 20 72 6f 6f 74 20 70 y, all.** root p
18970 61 67 65 73 20 61 72 65 20 6b 65 70 74 20 61 74 ages are kept at
18971 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f the beginning o
18972 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 f the database f
18973 69 6c 65 2c 20 77 68 69 63 68 0a 2a 2a 20 69 73 ile, which.** is
18974 20 6e 65 63 65 73 73 61 72 79 20 66 6f 72 20 41 necessary for A
18975 55 54 4f 56 41 43 55 55 4d 20 74 6f 20 77 6f 72 UTOVACUUM to wor
18976 6b 20 72 69 67 68 74 2e 20 20 2a 70 69 4d 6f 76 k right. *piMov
18977 65 64 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 ed is set to the
18978 20 0a 2a 2a 20 70 61 67 65 20 6e 75 6d 62 65 72 .** page number
18979 20 74 68 61 74 20 75 73 65 64 20 74 6f 20 62 65 that used to be
1897a 20 74 68 65 20 6c 61 73 74 20 72 6f 6f 74 20 70 the last root p
1897b 61 67 65 20 69 6e 20 74 68 65 20 66 69 6c 65 20 age in the file
1897c 62 65 66 6f 72 65 0a 2a 2a 20 74 68 65 20 6d 6f before.** the mo
1897d 76 65 2e 20 20 49 66 20 6e 6f 20 70 61 67 65 20 ve. If no page
1897e 67 65 74 73 20 6d 6f 76 65 64 2c 20 2a 70 69 4d gets moved, *piM
1897f 6f 76 65 64 20 69 73 20 73 65 74 20 74 6f 20 30 oved is set to 0
18980 2e 0a 2a 2a 20 54 68 65 20 6c 61 73 74 20 72 6f ..** The last ro
18981 6f 74 20 70 61 67 65 20 69 73 20 72 65 63 6f 72 ot page is recor
18982 64 65 64 20 69 6e 20 6d 65 74 61 5b 33 5d 20 61 ded in meta[3] a
18983 6e 64 20 74 68 65 20 76 61 6c 75 65 20 6f 66 0a nd the value of.
18984 2a 2a 20 6d 65 74 61 5b 33 5d 20 69 73 20 75 70 ** meta[3] is up
18985 64 61 74 65 64 20 62 79 20 74 68 69 73 20 70 72 dated by this pr
18986 6f 63 65 64 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 ocedure..*/.stat
18987 69 63 20 69 6e 74 20 62 74 72 65 65 44 72 6f 70 ic int btreeDrop
18988 54 61 62 6c 65 28 42 74 72 65 65 20 2a 70 2c 20 Table(Btree *p,
18989 50 67 6e 6f 20 69 54 61 62 6c 65 2c 20 69 6e 74 Pgno iTable, int
1898a 20 2a 70 69 4d 6f 76 65 64 29 7b 0a 20 20 69 6e *piMoved){. in
1898b 74 20 72 63 3b 0a 20 20 4d 65 6d 50 61 67 65 20 t rc;. MemPage
1898c 2a 70 50 61 67 65 20 3d 20 30 3b 0a 20 20 42 74 *pPage = 0;. Bt
1898d 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d Shared *pBt = p-
1898e 3e 70 42 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 >pBt;.. assert(
1898f 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c sqlite3BtreeHol
18990 64 73 4d 75 74 65 78 28 70 29 20 29 3b 0a 20 20 dsMutex(p) );.
18991 61 73 73 65 72 74 28 20 70 2d 3e 69 6e 54 72 61 assert( p->inTra
18992 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 ns==TRANS_WRITE
18993 29 3b 0a 0a 20 20 2f 2a 20 49 74 20 69 73 20 69 );.. /* It is i
18994 6c 6c 65 67 61 6c 20 74 6f 20 64 72 6f 70 20 61 llegal to drop a
18995 20 74 61 62 6c 65 20 69 66 20 61 6e 79 20 63 75 table if any cu
18996 72 73 6f 72 73 20 61 72 65 20 6f 70 65 6e 20 6f rsors are open o
18997 6e 20 74 68 65 0a 20 20 2a 2a 20 64 61 74 61 62 n the. ** datab
18998 61 73 65 2e 20 54 68 69 73 20 69 73 20 62 65 63 ase. This is bec
18999 61 75 73 65 20 69 6e 20 61 75 74 6f 2d 76 61 63 ause in auto-vac
1899a 75 75 6d 20 6d 6f 64 65 20 74 68 65 20 62 61 63 uum mode the bac
1899b 6b 65 6e 64 20 6d 61 79 0a 20 20 2a 2a 20 6e 65 kend may. ** ne
1899c 65 64 20 74 6f 20 6d 6f 76 65 20 61 6e 6f 74 68 ed to move anoth
1899d 65 72 20 72 6f 6f 74 2d 70 61 67 65 20 74 6f 20 er root-page to
1899e 66 69 6c 6c 20 61 20 67 61 70 20 6c 65 66 74 20 fill a gap left
1899f 62 79 20 74 68 65 20 64 65 6c 65 74 65 64 0a 20 by the deleted.
189a0 20 2a 2a 20 72 6f 6f 74 20 70 61 67 65 2e 20 49 ** root page. I
189a1 66 20 61 6e 20 6f 70 65 6e 20 63 75 72 73 6f 72 f an open cursor
189a2 20 77 61 73 20 75 73 69 6e 67 20 74 68 69 73 20 was using this
189a3 70 61 67 65 20 61 20 70 72 6f 62 6c 65 6d 20 77 page a problem w
189a4 6f 75 6c 64 20 0a 20 20 2a 2a 20 6f 63 63 75 72 ould . ** occur
189a5 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 .. **. ** This
189a6 20 65 72 72 6f 72 20 69 73 20 63 61 75 67 68 74 error is caught
189a7 20 6c 6f 6e 67 20 62 65 66 6f 72 65 20 63 6f 6e long before con
189a8 74 72 6f 6c 20 72 65 61 63 68 65 73 20 74 68 69 trol reaches thi
189a9 73 20 70 6f 69 6e 74 2e 0a 20 20 2a 2f 0a 20 20 s point.. */.
189aa 69 66 28 20 4e 45 56 45 52 28 70 42 74 2d 3e 70 if( NEVER(pBt->p
189ab 43 75 72 73 6f 72 29 20 29 7b 0a 20 20 20 20 73 Cursor) ){. s
189ac 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e qlite3Connection
189ad 42 6c 6f 63 6b 65 64 28 70 2d 3e 64 62 2c 20 70 Blocked(p->db, p
189ae 42 74 2d 3e 70 43 75 72 73 6f 72 2d 3e 70 42 74 Bt->pCursor->pBt
189af 72 65 65 2d 3e 64 62 29 3b 0a 20 20 20 20 72 65 ree->db);. re
189b0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b turn SQLITE_LOCK
189b1 45 44 5f 53 48 41 52 45 44 43 41 43 48 45 3b 0a ED_SHAREDCACHE;.
189b2 20 20 7d 0a 0a 20 20 72 63 20 3d 20 62 74 72 65 }.. rc = btre
189b3 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 28 50 eGetPage(pBt, (P
189b4 67 6e 6f 29 69 54 61 62 6c 65 2c 20 26 70 50 61 gno)iTable, &pPa
189b5 67 65 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 ge, 0);. if( rc
189b6 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 ) return rc;.
189b7 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 rc = sqlite3Btre
189b8 65 43 6c 65 61 72 54 61 62 6c 65 28 70 2c 20 69 eClearTable(p, i
189b9 54 61 62 6c 65 2c 20 30 29 3b 0a 20 20 69 66 28 Table, 0);. if(
189ba 20 72 63 20 29 7b 0a 20 20 20 20 72 65 6c 65 61 rc ){. relea
189bb 73 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 sePage(pPage);.
189bc 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
189bd 7d 0a 0a 20 20 2a 70 69 4d 6f 76 65 64 20 3d 20 }.. *piMoved =
189be 30 3b 0a 0a 20 20 69 66 28 20 69 54 61 62 6c 65 0;.. if( iTable
189bf 3e 31 20 29 7b 0a 23 69 66 64 65 66 20 53 51 4c >1 ){.#ifdef SQL
189c0 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 ITE_OMIT_AUTOVAC
189c1 55 55 4d 0a 20 20 20 20 66 72 65 65 50 61 67 65 UUM. freePage
189c2 28 70 50 61 67 65 2c 20 26 72 63 29 3b 0a 20 20 (pPage, &rc);.
189c3 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 releasePage(pP
189c4 61 67 65 29 3b 0a 23 65 6c 73 65 0a 20 20 20 20 age);.#else.
189c5 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 if( pBt->autoVac
189c6 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 50 67 6e uum ){. Pgn
189c7 6f 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 3b 0a 20 o maxRootPgno;.
189c8 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 sqlite3Btre
189c9 65 47 65 74 4d 65 74 61 28 70 2c 20 42 54 52 45 eGetMeta(p, BTRE
189ca 45 5f 4c 41 52 47 45 53 54 5f 52 4f 4f 54 5f 50 E_LARGEST_ROOT_P
189cb 41 47 45 2c 20 26 6d 61 78 52 6f 6f 74 50 67 6e AGE, &maxRootPgn
189cc 6f 29 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 69 o);.. if( i
189cd 54 61 62 6c 65 3d 3d 6d 61 78 52 6f 6f 74 50 67 Table==maxRootPg
189ce 6e 6f 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a no ){. /*
189cf 20 49 66 20 74 68 65 20 74 61 62 6c 65 20 62 65 If the table be
189d0 69 6e 67 20 64 72 6f 70 70 65 64 20 69 73 20 74 ing dropped is t
189d1 68 65 20 74 61 62 6c 65 20 77 69 74 68 20 74 68 he table with th
189d2 65 20 6c 61 72 67 65 73 74 20 72 6f 6f 74 2d 70 e largest root-p
189d3 61 67 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 6e age. ** n
189d4 75 6d 62 65 72 20 69 6e 20 74 68 65 20 64 61 74 umber in the dat
189d5 61 62 61 73 65 2c 20 70 75 74 20 74 68 65 20 72 abase, put the r
189d6 6f 6f 74 20 70 61 67 65 20 6f 6e 20 74 68 65 20 oot page on the
189d7 66 72 65 65 20 6c 69 73 74 2e 20 0a 20 20 20 20 free list. .
189d8 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 66 */. f
189d9 72 65 65 50 61 67 65 28 70 50 61 67 65 2c 20 26 reePage(pPage, &
189da 72 63 29 3b 0a 20 20 20 20 20 20 20 20 72 65 6c rc);. rel
189db 65 61 73 65 50 61 67 65 28 70 50 61 67 65 29 3b easePage(pPage);
189dc 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 . if( rc!
189dd 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
189de 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 return r
189df 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 c;. }.
189e0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
189e1 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 62 /* The table b
189e2 65 69 6e 67 20 64 72 6f 70 70 65 64 20 64 6f 65 eing dropped doe
189e3 73 20 6e 6f 74 20 68 61 76 65 20 74 68 65 20 6c s not have the l
189e4 61 72 67 65 73 74 20 72 6f 6f 74 2d 70 61 67 65 argest root-page
189e5 0a 20 20 20 20 20 20 20 20 2a 2a 20 6e 75 6d 62 . ** numb
189e6 65 72 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 er in the databa
189e7 73 65 2e 20 53 6f 20 6d 6f 76 65 20 74 68 65 20 se. So move the
189e8 70 61 67 65 20 74 68 61 74 20 64 6f 65 73 20 69 page that does i
189e9 6e 74 6f 20 74 68 65 20 0a 20 20 20 20 20 20 20 nto the .
189ea 20 2a 2a 20 67 61 70 20 6c 65 66 74 20 62 79 20 ** gap left by
189eb 74 68 65 20 64 65 6c 65 74 65 64 20 72 6f 6f 74 the deleted root
189ec 2d 70 61 67 65 2e 0a 20 20 20 20 20 20 20 20 2a -page.. *
189ed 2f 0a 20 20 20 20 20 20 20 20 4d 65 6d 50 61 67 /. MemPag
189ee 65 20 2a 70 4d 6f 76 65 3b 0a 20 20 20 20 20 20 e *pMove;.
189ef 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 releasePage(pP
189f0 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 72 63 age);. rc
189f1 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 28 = btreeGetPage(
189f2 70 42 74 2c 20 6d 61 78 52 6f 6f 74 50 67 6e 6f pBt, maxRootPgno
189f3 2c 20 26 70 4d 6f 76 65 2c 20 30 29 3b 0a 20 20 , &pMove, 0);.
189f4 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 if( rc!=SQ
189f5 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
189f6 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
189f7 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
189f8 20 20 72 63 20 3d 20 72 65 6c 6f 63 61 74 65 50 rc = relocateP
189f9 61 67 65 28 70 42 74 2c 20 70 4d 6f 76 65 2c 20 age(pBt, pMove,
189fa 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 2c PTRMAP_ROOTPAGE,
189fb 20 30 2c 20 69 54 61 62 6c 65 2c 20 30 29 3b 0a 0, iTable, 0);.
189fc 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 releaseP
189fd 61 67 65 28 70 4d 6f 76 65 29 3b 0a 20 20 20 20 age(pMove);.
189fe 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
189ff 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
18a00 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
18a01 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
18a02 70 4d 6f 76 65 20 3d 20 30 3b 0a 20 20 20 20 20 pMove = 0;.
18a03 20 20 20 72 63 20 3d 20 62 74 72 65 65 47 65 74 rc = btreeGet
18a04 50 61 67 65 28 70 42 74 2c 20 6d 61 78 52 6f 6f Page(pBt, maxRoo
18a05 74 50 67 6e 6f 2c 20 26 70 4d 6f 76 65 2c 20 30 tPgno, &pMove, 0
18a06 29 3b 0a 20 20 20 20 20 20 20 20 66 72 65 65 50 );. freeP
18a07 61 67 65 28 70 4d 6f 76 65 2c 20 26 72 63 29 3b age(pMove, &rc);
18a08 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 . release
18a09 50 61 67 65 28 70 4d 6f 76 65 29 3b 0a 20 20 20 Page(pMove);.
18a0a 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c if( rc!=SQL
18a0b 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
18a0c 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
18a0d 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
18a0e 20 2a 70 69 4d 6f 76 65 64 20 3d 20 6d 61 78 52 *piMoved = maxR
18a0f 6f 6f 74 50 67 6e 6f 3b 0a 20 20 20 20 20 20 7d ootPgno;. }
18a10 0a 0a 20 20 20 20 20 20 2f 2a 20 53 65 74 20 74 .. /* Set t
18a11 68 65 20 6e 65 77 20 27 6d 61 78 2d 72 6f 6f 74 he new 'max-root
18a12 2d 70 61 67 65 27 20 76 61 6c 75 65 20 69 6e 20 -page' value in
18a13 74 68 65 20 64 61 74 61 62 61 73 65 20 68 65 61 the database hea
18a14 64 65 72 2e 20 54 68 69 73 0a 20 20 20 20 20 20 der. This.
18a15 2a 2a 20 69 73 20 74 68 65 20 6f 6c 64 20 76 61 ** is the old va
18a16 6c 75 65 20 6c 65 73 73 20 6f 6e 65 2c 20 6c 65 lue less one, le
18a17 73 73 20 6f 6e 65 20 6d 6f 72 65 20 69 66 20 74 ss one more if t
18a18 68 61 74 20 68 61 70 70 65 6e 73 20 74 6f 0a 20 hat happens to.
18a19 20 20 20 20 20 2a 2a 20 62 65 20 61 20 72 6f 6f ** be a roo
18a1a 74 2d 70 61 67 65 20 6e 75 6d 62 65 72 2c 20 6c t-page number, l
18a1b 65 73 73 20 6f 6e 65 20 61 67 61 69 6e 20 69 66 ess one again if
18a1c 20 74 68 61 74 20 69 73 20 74 68 65 0a 20 20 20 that is the.
18a1d 20 20 20 2a 2a 20 50 45 4e 44 49 4e 47 5f 42 59 ** PENDING_BY
18a1e 54 45 5f 50 41 47 45 2e 0a 20 20 20 20 20 20 2a TE_PAGE.. *
18a1f 2f 0a 20 20 20 20 20 20 6d 61 78 52 6f 6f 74 50 /. maxRootP
18a20 67 6e 6f 2d 2d 3b 0a 20 20 20 20 20 20 77 68 69 gno--;. whi
18a21 6c 65 28 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 3d le( maxRootPgno=
18a22 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 =PENDING_BYTE_PA
18a23 47 45 28 70 42 74 29 0a 20 20 20 20 20 20 20 20 GE(pBt).
18a24 20 20 20 20 20 7c 7c 20 50 54 52 4d 41 50 5f 49 || PTRMAP_I
18a25 53 50 41 47 45 28 70 42 74 2c 20 6d 61 78 52 6f SPAGE(pBt, maxRo
18a26 6f 74 50 67 6e 6f 29 20 29 7b 0a 20 20 20 20 20 otPgno) ){.
18a27 20 20 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 2d 2d maxRootPgno--
18a28 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
18a29 61 73 73 65 72 74 28 20 6d 61 78 52 6f 6f 74 50 assert( maxRootP
18a2a 67 6e 6f 21 3d 50 45 4e 44 49 4e 47 5f 42 59 54 gno!=PENDING_BYT
18a2b 45 5f 50 41 47 45 28 70 42 74 29 20 29 3b 0a 0a E_PAGE(pBt) );..
18a2c 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
18a2d 65 33 42 74 72 65 65 55 70 64 61 74 65 4d 65 74 e3BtreeUpdateMet
18a2e 61 28 70 2c 20 34 2c 20 6d 61 78 52 6f 6f 74 50 a(p, 4, maxRootP
18a2f 67 6e 6f 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b gno);. }else{
18a30 0a 20 20 20 20 20 20 66 72 65 65 50 61 67 65 28 . freePage(
18a31 70 50 61 67 65 2c 20 26 72 63 29 3b 0a 20 20 20 pPage, &rc);.
18a32 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 releasePage(p
18a33 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a 23 65 6e Page);. }.#en
18a34 64 69 66 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 dif. }else{.
18a35 20 2f 2a 20 49 66 20 73 71 6c 69 74 65 33 42 74 /* If sqlite3Bt
18a36 72 65 65 44 72 6f 70 54 61 62 6c 65 20 77 61 73 reeDropTable was
18a37 20 63 61 6c 6c 65 64 20 6f 6e 20 70 61 67 65 20 called on page
18a38 31 2e 0a 20 20 20 20 2a 2a 20 54 68 69 73 20 72 1.. ** This r
18a39 65 61 6c 6c 79 20 6e 65 76 65 72 20 73 68 6f 75 eally never shou
18a3a 6c 64 20 68 61 70 70 65 6e 20 65 78 63 65 70 74 ld happen except
18a3b 20 69 6e 20 61 20 63 6f 72 72 75 70 74 0a 20 20 in a corrupt.
18a3c 20 20 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 0a ** database. .
18a3d 20 20 20 20 2a 2f 0a 20 20 20 20 7a 65 72 6f 50 */. zeroP
18a3e 61 67 65 28 70 50 61 67 65 2c 20 50 54 46 5f 49 age(pPage, PTF_I
18a3f 4e 54 4b 45 59 7c 50 54 46 5f 4c 45 41 46 20 29 NTKEY|PTF_LEAF )
18a40 3b 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 ;. releasePag
18a41 65 28 70 50 61 67 65 29 3b 0a 20 20 7d 0a 20 20 e(pPage);. }.
18a42 72 65 74 75 72 6e 20 72 63 3b 20 20 0a 7d 0a 53 return rc; .}.S
18a43 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
18a44 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 72 t sqlite3BtreeDr
18a45 6f 70 54 61 62 6c 65 28 42 74 72 65 65 20 2a 70 opTable(Btree *p
18a46 2c 20 69 6e 74 20 69 54 61 62 6c 65 2c 20 69 6e , int iTable, in
18a47 74 20 2a 70 69 4d 6f 76 65 64 29 7b 0a 20 20 69 t *piMoved){. i
18a48 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 nt rc;. sqlite3
18a49 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 BtreeEnter(p);.
18a4a 20 72 63 20 3d 20 62 74 72 65 65 44 72 6f 70 54 rc = btreeDropT
18a4b 61 62 6c 65 28 70 2c 20 69 54 61 62 6c 65 2c 20 able(p, iTable,
18a4c 70 69 4d 6f 76 65 64 29 3b 0a 20 20 73 71 6c 69 piMoved);. sqli
18a4d 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 te3BtreeLeave(p)
18a4e 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
18a4f 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 .../*.** This fu
18a50 6e 63 74 69 6f 6e 20 6d 61 79 20 6f 6e 6c 79 20 nction may only
18a51 62 65 20 63 61 6c 6c 65 64 20 69 66 20 74 68 65 be called if the
18a52 20 62 2d 74 72 65 65 20 63 6f 6e 6e 65 63 74 69 b-tree connecti
18a53 6f 6e 20 61 6c 72 65 61 64 79 0a 2a 2a 20 68 61 on already.** ha
18a54 73 20 61 20 72 65 61 64 20 6f 72 20 77 72 69 74 s a read or writ
18a55 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70 e transaction op
18a56 65 6e 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 en on the databa
18a57 73 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 61 64 20 74 se..**.** Read t
18a58 68 65 20 6d 65 74 61 2d 69 6e 66 6f 72 6d 61 74 he meta-informat
18a59 69 6f 6e 20 6f 75 74 20 6f 66 20 61 20 64 61 74 ion out of a dat
18a5a 61 62 61 73 65 20 66 69 6c 65 2e 20 20 4d 65 74 abase file. Met
18a5b 61 5b 30 5d 0a 2a 2a 20 69 73 20 74 68 65 20 6e a[0].** is the n
18a5c 75 6d 62 65 72 20 6f 66 20 66 72 65 65 20 70 61 umber of free pa
18a5d 67 65 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e ges currently in
18a5e 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 the database.
18a5f 4d 65 74 61 5b 31 5d 0a 2a 2a 20 74 68 72 6f 75 Meta[1].** throu
18a60 67 68 20 6d 65 74 61 5b 31 35 5d 20 61 72 65 20 gh meta[15] are
18a61 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 75 73 available for us
18a62 65 20 62 79 20 68 69 67 68 65 72 20 6c 61 79 65 e by higher laye
18a63 72 73 2e 20 20 4d 65 74 61 5b 30 5d 0a 2a 2a 20 rs. Meta[0].**
18a64 69 73 20 72 65 61 64 2d 6f 6e 6c 79 2c 20 74 68 is read-only, th
18a65 65 20 6f 74 68 65 72 73 20 61 72 65 20 72 65 61 e others are rea
18a66 64 2f 77 72 69 74 65 2e 0a 2a 2a 20 0a 2a 2a 20 d/write..** .**
18a67 54 68 65 20 73 63 68 65 6d 61 20 6c 61 79 65 72 The schema layer
18a68 20 6e 75 6d 62 65 72 73 20 6d 65 74 61 20 76 61 numbers meta va
18a69 6c 75 65 73 20 64 69 66 66 65 72 65 6e 74 6c 79 lues differently
18a6a 2e 20 20 41 74 20 74 68 65 20 73 63 68 65 6d 61 . At the schema
18a6b 0a 2a 2a 20 6c 61 79 65 72 20 28 61 6e 64 20 74 .** layer (and t
18a6c 68 65 20 53 65 74 43 6f 6f 6b 69 65 20 61 6e 64 he SetCookie and
18a6d 20 52 65 61 64 43 6f 6f 6b 69 65 20 6f 70 63 6f ReadCookie opco
18a6e 64 65 73 29 20 74 68 65 20 6e 75 6d 62 65 72 20 des) the number
18a6f 6f 66 0a 2a 2a 20 66 72 65 65 20 70 61 67 65 73 of.** free pages
18a70 20 69 73 20 6e 6f 74 20 76 69 73 69 62 6c 65 2e is not visible.
18a71 20 20 53 6f 20 43 6f 6f 6b 69 65 5b 30 5d 20 69 So Cookie[0] i
18a72 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 4d 65 s the same as Me
18a73 74 61 5b 31 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ta[1]..*/.SQLITE
18a74 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
18a75 6c 69 74 65 33 42 74 72 65 65 47 65 74 4d 65 74 lite3BtreeGetMet
18a76 61 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 a(Btree *p, int
18a77 69 64 78 2c 20 75 33 32 20 2a 70 4d 65 74 61 29 idx, u32 *pMeta)
18a78 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 {. BtShared *pB
18a79 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 0a 20 20 73 t = p->pBt;.. s
18a7a 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 qlite3BtreeEnter
18a7b 28 70 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 (p);. assert( p
18a7c 2d 3e 69 6e 54 72 61 6e 73 3e 54 52 41 4e 53 5f ->inTrans>TRANS_
18a7d 4e 4f 4e 45 20 29 3b 0a 20 20 61 73 73 65 72 74 NONE );. assert
18a7e 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 71 75 65 ( SQLITE_OK==que
18a7f 72 79 53 68 61 72 65 64 43 61 63 68 65 54 61 62 rySharedCacheTab
18a80 6c 65 4c 6f 63 6b 28 70 2c 20 4d 41 53 54 45 52 leLock(p, MASTER
18a81 5f 52 4f 4f 54 2c 20 52 45 41 44 5f 4c 4f 43 4b _ROOT, READ_LOCK
18a82 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 ) );. assert( p
18a83 42 74 2d 3e 70 50 61 67 65 31 20 29 3b 0a 20 20 Bt->pPage1 );.
18a84 61 73 73 65 72 74 28 20 69 64 78 3e 3d 30 20 26 assert( idx>=0 &
18a85 26 20 69 64 78 3c 3d 31 35 20 29 3b 0a 0a 20 20 & idx<=15 );..
18a86 2a 70 4d 65 74 61 20 3d 20 67 65 74 34 62 79 74 *pMeta = get4byt
18a87 65 28 26 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e e(&pBt->pPage1->
18a88 61 44 61 74 61 5b 33 36 20 2b 20 69 64 78 2a 34 aData[36 + idx*4
18a89 5d 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 61 75 74 ]);.. /* If aut
18a8a 6f 2d 76 61 63 75 75 6d 20 69 73 20 64 69 73 61 o-vacuum is disa
18a8b 62 6c 65 64 20 69 6e 20 74 68 69 73 20 62 75 69 bled in this bui
18a8c 6c 64 20 61 6e 64 20 74 68 69 73 20 69 73 20 61 ld and this is a
18a8d 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 0a 20 20 n auto-vacuum.
18a8e 2a 2a 20 64 61 74 61 62 61 73 65 2c 20 6d 61 72 ** database, mar
18a8f 6b 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61 k the database a
18a90 73 20 72 65 61 64 2d 6f 6e 6c 79 2e 20 20 2a 2f s read-only. */
18a91 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f .#ifdef SQLITE_O
18a92 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 MIT_AUTOVACUUM.
18a93 20 69 66 28 20 69 64 78 3d 3d 42 54 52 45 45 5f if( idx==BTREE_
18a94 4c 41 52 47 45 53 54 5f 52 4f 4f 54 5f 50 41 47 LARGEST_ROOT_PAG
18a95 45 20 26 26 20 2a 70 4d 65 74 61 3e 30 20 29 20 E && *pMeta>0 )
18a96 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 3d 20 pBt->readOnly =
18a97 31 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 73 71 6c 1;.#endif.. sql
18a98 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 ite3BtreeLeave(p
18a99 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 );.}../*.** Writ
18a9a 65 20 6d 65 74 61 2d 69 6e 66 6f 72 6d 61 74 69 e meta-informati
18a9b 6f 6e 20 62 61 63 6b 20 69 6e 74 6f 20 74 68 65 on back into the
18a9c 20 64 61 74 61 62 61 73 65 2e 20 20 4d 65 74 61 database. Meta
18a9d 5b 30 5d 20 69 73 0a 2a 2a 20 72 65 61 64 2d 6f [0] is.** read-o
18a9e 6e 6c 79 20 61 6e 64 20 6d 61 79 20 6e 6f 74 20 nly and may not
18a9f 62 65 20 77 72 69 74 74 65 6e 2e 0a 2a 2f 0a 53 be written..*/.S
18aa0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
18aa1 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 55 70 t sqlite3BtreeUp
18aa2 64 61 74 65 4d 65 74 61 28 42 74 72 65 65 20 2a dateMeta(Btree *
18aa3 70 2c 20 69 6e 74 20 69 64 78 2c 20 75 33 32 20 p, int idx, u32
18aa4 69 4d 65 74 61 29 7b 0a 20 20 42 74 53 68 61 72 iMeta){. BtShar
18aa5 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 ed *pBt = p->pBt
18aa6 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 ;. unsigned cha
18aa7 72 20 2a 70 50 31 3b 0a 20 20 69 6e 74 20 72 63 r *pP1;. int rc
18aa8 3b 0a 20 20 61 73 73 65 72 74 28 20 69 64 78 3e ;. assert( idx>
18aa9 3d 31 20 26 26 20 69 64 78 3c 3d 31 35 20 29 3b =1 && idx<=15 );
18aaa 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 . sqlite3BtreeE
18aab 6e 74 65 72 28 70 29 3b 0a 20 20 61 73 73 65 72 nter(p);. asser
18aac 74 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 t( p->inTrans==T
18aad 52 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 RANS_WRITE );.
18aae 61 73 73 65 72 74 28 20 70 42 74 2d 3e 70 50 61 assert( pBt->pPa
18aaf 67 65 31 21 3d 30 20 29 3b 0a 20 20 70 50 31 20 ge1!=0 );. pP1
18ab0 3d 20 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 = pBt->pPage1->a
18ab1 44 61 74 61 3b 0a 20 20 72 63 20 3d 20 73 71 6c Data;. rc = sql
18ab2 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 ite3PagerWrite(p
18ab3 42 74 2d 3e 70 50 61 67 65 31 2d 3e 70 44 62 50 Bt->pPage1->pDbP
18ab4 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d age);. if( rc==
18ab5 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
18ab6 20 70 75 74 34 62 79 74 65 28 26 70 50 31 5b 33 put4byte(&pP1[3
18ab7 36 20 2b 20 69 64 78 2a 34 5d 2c 20 69 4d 65 74 6 + idx*4], iMet
18ab8 61 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 a);.#ifndef SQLI
18ab9 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 TE_OMIT_AUTOVACU
18aba 55 4d 0a 20 20 20 20 69 66 28 20 69 64 78 3d 3d UM. if( idx==
18abb 42 54 52 45 45 5f 49 4e 43 52 5f 56 41 43 55 55 BTREE_INCR_VACUU
18abc 4d 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 M ){. asser
18abd 74 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 t( pBt->autoVacu
18abe 75 6d 20 7c 7c 20 69 4d 65 74 61 3d 3d 30 20 29 um || iMeta==0 )
18abf 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
18ac0 69 4d 65 74 61 3d 3d 30 20 7c 7c 20 69 4d 65 74 iMeta==0 || iMet
18ac1 61 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 70 42 a==1 );. pB
18ac2 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 20 3d 20 t->incrVacuum =
18ac3 28 75 38 29 69 4d 65 74 61 3b 0a 20 20 20 20 7d (u8)iMeta;. }
18ac4 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 73 71 .#endif. }. sq
18ac5 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 lite3BtreeLeave(
18ac6 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b p);. return rc;
18ac7 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 .}..#ifndef SQLI
18ac8 54 45 5f 4f 4d 49 54 5f 42 54 52 45 45 43 4f 55 TE_OMIT_BTREECOU
18ac9 4e 54 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 69 72 NT./*.** The fir
18aca 73 74 20 61 72 67 75 6d 65 6e 74 2c 20 70 43 75 st argument, pCu
18acb 72 2c 20 69 73 20 61 20 63 75 72 73 6f 72 20 6f r, is a cursor o
18acc 70 65 6e 65 64 20 6f 6e 20 73 6f 6d 65 20 62 2d pened on some b-
18acd 74 72 65 65 2e 20 43 6f 75 6e 74 20 74 68 65 0a tree. Count the.
18ace 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 ** number of ent
18acf 72 69 65 73 20 69 6e 20 74 68 65 20 62 2d 74 72 ries in the b-tr
18ad0 65 65 20 61 6e 64 20 77 72 69 74 65 20 74 68 65 ee and write the
18ad1 20 72 65 73 75 6c 74 20 74 6f 20 2a 70 6e 45 6e result to *pnEn
18ad2 74 72 79 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 try..**.** SQLIT
18ad3 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 E_OK is returned
18ad4 20 69 66 20 74 68 65 20 6f 70 65 72 61 74 69 6f if the operatio
18ad5 6e 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 6c n is successfull
18ad6 79 20 65 78 65 63 75 74 65 64 2e 20 0a 2a 2a 20 y executed. .**
18ad7 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 61 6e Otherwise, if an
18ad8 20 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e error is encoun
18ad9 74 65 72 65 64 20 28 69 2e 65 2e 20 61 6e 20 49 tered (i.e. an I
18ada 4f 20 65 72 72 6f 72 20 6f 72 20 64 61 74 61 62 O error or datab
18adb 61 73 65 0a 2a 2a 20 63 6f 72 72 75 70 74 69 6f ase.** corruptio
18adc 6e 29 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 n) an SQLite err
18add 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 or code is retur
18ade 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ned..*/.SQLITE_P
18adf 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
18ae0 65 33 42 74 72 65 65 43 6f 75 6e 74 28 42 74 43 e3BtreeCount(BtC
18ae1 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 36 34 ursor *pCur, i64
18ae2 20 2a 70 6e 45 6e 74 72 79 29 7b 0a 20 20 69 36 *pnEntry){. i6
18ae3 34 20 6e 45 6e 74 72 79 20 3d 20 30 3b 20 20 20 4 nEntry = 0;
18ae4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18ae5 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20 72 /* Value to r
18ae6 65 74 75 72 6e 20 69 6e 20 2a 70 6e 45 6e 74 72 eturn in *pnEntr
18ae7 79 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 y */. int rc;
18ae8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18ae9 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 /* R
18aea 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 eturn code */.
18aeb 72 63 20 3d 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 rc = moveToRoot(
18aec 70 43 75 72 29 3b 0a 0a 20 20 2f 2a 20 55 6e 6c pCur);.. /* Unl
18aed 65 73 73 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 ess an error occ
18aee 75 72 73 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 urs, the followi
18aef 6e 67 20 6c 6f 6f 70 20 72 75 6e 73 20 6f 6e 65 ng loop runs one
18af0 20 69 74 65 72 61 74 69 6f 6e 20 66 6f 72 20 65 iteration for e
18af1 61 63 68 0a 20 20 2a 2a 20 70 61 67 65 20 69 6e ach. ** page in
18af2 20 74 68 65 20 42 2d 54 72 65 65 20 73 74 72 75 the B-Tree stru
18af3 63 74 75 72 65 20 28 6e 6f 74 20 69 6e 63 6c 75 cture (not inclu
18af4 64 69 6e 67 20 6f 76 65 72 66 6c 6f 77 20 70 61 ding overflow pa
18af5 67 65 73 29 2e 20 0a 20 20 2a 2f 0a 20 20 77 68 ges). . */. wh
18af6 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f ile( rc==SQLITE_
18af7 4f 4b 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 49 OK ){. int iI
18af8 64 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 dx;
18af9 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
18afa 49 6e 64 65 78 20 6f 66 20 63 68 69 6c 64 20 6e Index of child n
18afb 6f 64 65 20 69 6e 20 70 61 72 65 6e 74 20 2a 2f ode in parent */
18afc 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 . MemPage *pP
18afd 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 age;
18afe 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 /* Curre
18aff 6e 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 62 nt page of the b
18b00 2d 74 72 65 65 20 2a 2f 0a 0a 20 20 20 20 2f 2a -tree */.. /*
18b01 20 49 66 20 74 68 69 73 20 69 73 20 61 20 6c 65 If this is a le
18b02 61 66 20 70 61 67 65 20 6f 72 20 74 68 65 20 74 af page or the t
18b03 72 65 65 20 69 73 20 6e 6f 74 20 61 6e 20 69 6e ree is not an in
18b04 74 2d 6b 65 79 20 74 72 65 65 2c 20 74 68 65 6e t-key tree, then
18b05 20 0a 20 20 20 20 2a 2a 20 74 68 69 73 20 70 61 . ** this pa
18b06 67 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 75 6e ge contains coun
18b07 74 61 62 6c 65 20 65 6e 74 72 69 65 73 2e 20 49 table entries. I
18b08 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 65 6e 74 ncrement the ent
18b09 72 79 20 63 6f 75 6e 74 65 72 0a 20 20 20 20 2a ry counter. *
18b0a 2a 20 61 63 63 6f 72 64 69 6e 67 6c 79 2e 0a 20 * accordingly..
18b0b 20 20 20 2a 2f 0a 20 20 20 20 70 50 61 67 65 20 */. pPage
18b0c 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 = pCur->apPage[p
18b0d 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 20 Cur->iPage];.
18b0e 20 69 66 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 if( pPage->leaf
18b0f 20 7c 7c 20 21 70 50 61 67 65 2d 3e 69 6e 74 4b || !pPage->intK
18b10 65 79 20 29 7b 0a 20 20 20 20 20 20 6e 45 6e 74 ey ){. nEnt
18b11 72 79 20 2b 3d 20 70 50 61 67 65 2d 3e 6e 43 65 ry += pPage->nCe
18b12 6c 6c 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f ll;. }.. /
18b13 2a 20 70 50 61 67 65 20 69 73 20 61 20 6c 65 61 * pPage is a lea
18b14 66 20 6e 6f 64 65 2e 20 54 68 69 73 20 6c 6f 6f f node. This loo
18b15 70 20 6e 61 76 69 67 61 74 65 73 20 74 68 65 20 p navigates the
18b16 63 75 72 73 6f 72 20 73 6f 20 74 68 61 74 20 69 cursor so that i
18b17 74 20 0a 20 20 20 20 2a 2a 20 70 6f 69 6e 74 73 t . ** points
18b18 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 69 6e to the first in
18b19 74 65 72 69 6f 72 20 63 65 6c 6c 20 74 68 61 74 terior cell that
18b1a 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 it points to th
18b1b 65 20 70 61 72 65 6e 74 20 6f 66 0a 20 20 20 20 e parent of.
18b1c 2a 2a 20 74 68 65 20 6e 65 78 74 20 70 61 67 65 ** the next page
18b1d 20 69 6e 20 74 68 65 20 74 72 65 65 20 74 68 61 in the tree tha
18b1e 74 20 68 61 73 20 6e 6f 74 20 79 65 74 20 62 65 t has not yet be
18b1f 65 6e 20 76 69 73 69 74 65 64 2e 20 54 68 65 0a en visited. The.
18b20 20 20 20 20 2a 2a 20 70 43 75 72 2d 3e 61 69 49 ** pCur->aiI
18b21 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 dx[pCur->iPage]
18b22 76 61 6c 75 65 20 69 73 20 73 65 74 20 74 6f 20 value is set to
18b23 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 the index of the
18b24 20 70 61 72 65 6e 74 20 63 65 6c 6c 0a 20 20 20 parent cell.
18b25 20 2a 2a 20 6f 66 20 74 68 65 20 70 61 67 65 2c ** of the page,
18b26 20 6f 72 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 or to the numbe
18b27 72 20 6f 66 20 63 65 6c 6c 73 20 69 6e 20 74 68 r of cells in th
18b28 65 20 70 61 67 65 20 69 66 20 74 68 65 20 6e 65 e page if the ne
18b29 78 74 20 70 61 67 65 0a 20 20 20 20 2a 2a 20 74 xt page. ** t
18b2a 6f 20 76 69 73 69 74 20 69 73 20 74 68 65 20 72 o visit is the r
18b2b 69 67 68 74 2d 63 68 69 6c 64 20 6f 66 20 69 74 ight-child of it
18b2c 73 20 70 61 72 65 6e 74 2e 0a 20 20 20 20 2a 2a s parent.. **
18b2d 0a 20 20 20 20 2a 2a 20 49 66 20 61 6c 6c 20 70 . ** If all p
18b2e 61 67 65 73 20 69 6e 20 74 68 65 20 74 72 65 65 ages in the tree
18b2f 20 68 61 76 65 20 62 65 65 6e 20 76 69 73 69 74 have been visit
18b30 65 64 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ed, return SQLIT
18b31 45 5f 4f 4b 20 74 6f 20 74 68 65 0a 20 20 20 20 E_OK to the.
18b32 2a 2a 20 63 61 6c 6c 65 72 2e 0a 20 20 20 20 2a ** caller.. *
18b33 2f 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 2d /. if( pPage-
18b34 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20 64 >leaf ){. d
18b35 6f 20 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 o {. if(
18b36 70 43 75 72 2d 3e 69 50 61 67 65 3d 3d 30 20 29 pCur->iPage==0 )
18b37 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 {. /* A
18b38 6c 6c 20 70 61 67 65 73 20 6f 66 20 74 68 65 20 ll pages of the
18b39 62 2d 74 72 65 65 20 68 61 76 65 20 62 65 65 6e b-tree have been
18b3a 20 76 69 73 69 74 65 64 2e 20 52 65 74 75 72 6e visited. Return
18b3b 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2e 20 2a successfully. *
18b3c 2f 0a 20 20 20 20 20 20 20 20 20 20 2a 70 6e 45 /. *pnE
18b3d 6e 74 72 79 20 3d 20 6e 45 6e 74 72 79 3b 0a 20 ntry = nEntry;.
18b3e 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 return
18b3f 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 SQLITE_OK;.
18b40 20 20 20 7d 0a 20 20 20 20 20 20 20 20 6d 6f 76 }. mov
18b41 65 54 6f 50 61 72 65 6e 74 28 70 43 75 72 29 3b eToParent(pCur);
18b42 0a 20 20 20 20 20 20 7d 77 68 69 6c 65 20 28 20 . }while (
18b43 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 pCur->aiIdx[pCur
18b44 2d 3e 69 50 61 67 65 5d 3e 3d 70 43 75 72 2d 3e ->iPage]>=pCur->
18b45 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 apPage[pCur->iPa
18b46 67 65 5d 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 0a 20 ge]->nCell );..
18b47 20 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64 78 pCur->aiIdx
18b48 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2b 2b 3b [pCur->iPage]++;
18b49 0a 20 20 20 20 20 20 70 50 61 67 65 20 3d 20 70 . pPage = p
18b4a 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 Cur->apPage[pCur
18b4b 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 20 20 7d 0a ->iPage];. }.
18b4c 0a 20 20 20 20 2f 2a 20 44 65 73 63 65 6e 64 20 . /* Descend
18b4d 74 6f 20 74 68 65 20 63 68 69 6c 64 20 6e 6f 64 to the child nod
18b4e 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 74 68 e of the cell th
18b4f 61 74 20 74 68 65 20 63 75 72 73 6f 72 20 63 75 at the cursor cu
18b50 72 72 65 6e 74 6c 79 20 0a 20 20 20 20 2a 2a 20 rrently . **
18b51 70 6f 69 6e 74 73 20 61 74 2e 20 54 68 69 73 20 points at. This
18b52 69 73 20 74 68 65 20 72 69 67 68 74 2d 63 68 69 is the right-chi
18b53 6c 64 20 69 66 20 28 69 49 64 78 3d 3d 70 50 61 ld if (iIdx==pPa
18b54 67 65 2d 3e 6e 43 65 6c 6c 29 2e 0a 20 20 20 20 ge->nCell)..
18b55 2a 2f 0a 20 20 20 20 69 49 64 78 20 3d 20 70 43 */. iIdx = pC
18b56 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e ur->aiIdx[pCur->
18b57 69 50 61 67 65 5d 3b 0a 20 20 20 20 69 66 28 20 iPage];. if(
18b58 69 49 64 78 3d 3d 70 50 61 67 65 2d 3e 6e 43 65 iIdx==pPage->nCe
18b59 6c 6c 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d ll ){. rc =
18b5a 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 75 moveToChild(pCu
18b5b 72 2c 20 67 65 74 34 62 79 74 65 28 26 70 50 61 r, get4byte(&pPa
18b5c 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d ge->aData[pPage-
18b5d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 29 3b >hdrOffset+8]));
18b5e 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
18b5f 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 68 69 rc = moveToChi
18b60 6c 64 28 70 43 75 72 2c 20 67 65 74 34 62 79 74 ld(pCur, get4byt
18b61 65 28 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 e(findCell(pPage
18b62 2c 20 69 49 64 78 29 29 29 3b 0a 20 20 20 20 7d , iIdx)));. }
18b63 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 6e 20 65 72 . }.. /* An er
18b64 72 6f 72 20 68 61 73 20 6f 63 63 75 72 72 65 64 ror has occurred
18b65 2e 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f . Return an erro
18b66 72 20 63 6f 64 65 2e 20 2a 2f 0a 20 20 72 65 74 r code. */. ret
18b67 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 urn rc;.}.#endif
18b68 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ../*.** Return t
18b69 68 65 20 70 61 67 65 72 20 61 73 73 6f 63 69 61 he pager associa
18b6a 74 65 64 20 77 69 74 68 20 61 20 42 54 72 65 65 ted with a BTree
18b6b 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 . This routine
18b6c 69 73 20 75 73 65 64 20 66 6f 72 0a 2a 2a 20 74 is used for.** t
18b6d 65 73 74 69 6e 67 20 61 6e 64 20 64 65 62 75 67 esting and debug
18b6e 67 69 6e 67 20 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 ging only..*/.SQ
18b6f 4c 49 54 45 5f 50 52 49 56 41 54 45 20 50 61 67 LITE_PRIVATE Pag
18b70 65 72 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 er *sqlite3Btree
18b71 50 61 67 65 72 28 42 74 72 65 65 20 2a 70 29 7b Pager(Btree *p){
18b72 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 70 42 74 . return p->pBt
18b73 2d 3e 70 50 61 67 65 72 3b 0a 7d 0a 0a 23 69 66 ->pPager;.}..#if
18b74 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
18b75 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b _INTEGRITY_CHECK
18b76 0a 2f 2a 0a 2a 2a 20 41 70 70 65 6e 64 20 61 20 ./*.** Append a
18b77 6d 65 73 73 61 67 65 20 74 6f 20 74 68 65 20 65 message to the e
18b78 72 72 6f 72 20 6d 65 73 73 61 67 65 20 73 74 72 rror message str
18b79 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ing..*/.static v
18b7a 6f 69 64 20 63 68 65 63 6b 41 70 70 65 6e 64 4d oid checkAppendM
18b7b 73 67 28 0a 20 20 49 6e 74 65 67 72 69 74 79 43 sg(. IntegrityC
18b7c 6b 20 2a 70 43 68 65 63 6b 2c 0a 20 20 63 68 61 k *pCheck,. cha
18b7d 72 20 2a 7a 4d 73 67 31 2c 0a 20 20 63 6f 6e 73 r *zMsg1,. cons
18b7e 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c t char *zFormat,
18b7f 0a 20 20 2e 2e 2e 0a 29 7b 0a 20 20 76 61 5f 6c . ....){. va_l
18b80 69 73 74 20 61 70 3b 0a 20 20 69 66 28 20 21 70 ist ap;. if( !p
18b81 43 68 65 63 6b 2d 3e 6d 78 45 72 72 20 29 20 72 Check->mxErr ) r
18b82 65 74 75 72 6e 3b 0a 20 20 70 43 68 65 63 6b 2d eturn;. pCheck-
18b83 3e 6d 78 45 72 72 2d 2d 3b 0a 20 20 70 43 68 65 >mxErr--;. pChe
18b84 63 6b 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 76 61 ck->nErr++;. va
18b85 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d _start(ap, zForm
18b86 61 74 29 3b 0a 20 20 69 66 28 20 70 43 68 65 63 at);. if( pChec
18b87 6b 2d 3e 65 72 72 4d 73 67 2e 6e 43 68 61 72 20 k->errMsg.nChar
18b88 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 ){. sqlite3St
18b89 72 41 63 63 75 6d 41 70 70 65 6e 64 28 26 70 43 rAccumAppend(&pC
18b8a 68 65 63 6b 2d 3e 65 72 72 4d 73 67 2c 20 22 5c heck->errMsg, "\
18b8b 6e 22 2c 20 31 29 3b 0a 20 20 7d 0a 20 20 69 66 n", 1);. }. if
18b8c 28 20 7a 4d 73 67 31 20 29 7b 0a 20 20 20 20 73 ( zMsg1 ){. s
18b8d 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 qlite3StrAccumAp
18b8e 70 65 6e 64 28 26 70 43 68 65 63 6b 2d 3e 65 72 pend(&pCheck->er
18b8f 72 4d 73 67 2c 20 7a 4d 73 67 31 2c 20 2d 31 29 rMsg, zMsg1, -1)
18b90 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 ;. }. sqlite3V
18b91 58 50 72 69 6e 74 66 28 26 70 43 68 65 63 6b 2d XPrintf(&pCheck-
18b92 3e 65 72 72 4d 73 67 2c 20 31 2c 20 7a 46 6f 72 >errMsg, 1, zFor
18b93 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65 mat, ap);. va_e
18b94 6e 64 28 61 70 29 3b 0a 20 20 69 66 28 20 70 43 nd(ap);. if( pC
18b95 68 65 63 6b 2d 3e 65 72 72 4d 73 67 2e 6d 61 6c heck->errMsg.mal
18b96 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 locFailed ){.
18b97 20 70 43 68 65 63 6b 2d 3e 6d 61 6c 6c 6f 63 46 pCheck->mallocF
18b98 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 7d ailed = 1;. }.}
18b99 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
18b9a 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 E_OMIT_INTEGRITY
18b9b 5f 43 48 45 43 4b 20 2a 2f 0a 0a 23 69 66 6e 64 _CHECK */..#ifnd
18b9c 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 ef SQLITE_OMIT_I
18b9d 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b 0a 2f NTEGRITY_CHECK./
18b9e 2a 0a 2a 2a 20 41 64 64 20 31 20 74 6f 20 74 68 *.** Add 1 to th
18b9f 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e e reference coun
18ba0 74 20 66 6f 72 20 70 61 67 65 20 69 50 61 67 65 t for page iPage
18ba1 2e 20 20 49 66 20 74 68 69 73 20 69 73 20 74 68 . If this is th
18ba2 65 20 73 65 63 6f 6e 64 0a 2a 2a 20 72 65 66 65 e second.** refe
18ba3 72 65 6e 63 65 20 74 6f 20 74 68 65 20 70 61 67 rence to the pag
18ba4 65 2c 20 61 64 64 20 61 6e 20 65 72 72 6f 72 20 e, add an error
18ba5 6d 65 73 73 61 67 65 20 74 6f 20 70 43 68 65 63 message to pChec
18ba6 6b 2d 3e 7a 45 72 72 4d 73 67 2e 0a 2a 2a 20 52 k->zErrMsg..** R
18ba7 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 72 65 eturn 1 if there
18ba8 20 61 72 65 20 32 20 6f 72 65 20 6d 6f 72 65 20 are 2 ore more
18ba9 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68 references to th
18baa 65 20 70 61 67 65 20 61 6e 64 20 30 20 69 66 0a e page and 0 if.
18bab 2a 2a 20 69 66 20 74 68 69 73 20 69 73 20 74 68 ** if this is th
18bac 65 20 66 69 72 73 74 20 72 65 66 65 72 65 6e 63 e first referenc
18bad 65 20 74 6f 20 74 68 65 20 70 61 67 65 2e 0a 2a e to the page..*
18bae 2a 0a 2a 2a 20 41 6c 73 6f 20 63 68 65 63 6b 20 *.** Also check
18baf 74 68 61 74 20 74 68 65 20 70 61 67 65 20 6e 75 that the page nu
18bb0 6d 62 65 72 20 69 73 20 69 6e 20 62 6f 75 6e 64 mber is in bound
18bb1 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 s..*/.static int
18bb2 20 63 68 65 63 6b 52 65 66 28 49 6e 74 65 67 72 checkRef(Integr
18bb3 69 74 79 43 6b 20 2a 70 43 68 65 63 6b 2c 20 50 ityCk *pCheck, P
18bb4 67 6e 6f 20 69 50 61 67 65 2c 20 63 68 61 72 20 gno iPage, char
18bb5 2a 7a 43 6f 6e 74 65 78 74 29 7b 0a 20 20 69 66 *zContext){. if
18bb6 28 20 69 50 61 67 65 3d 3d 30 20 29 20 72 65 74 ( iPage==0 ) ret
18bb7 75 72 6e 20 31 3b 0a 20 20 69 66 28 20 69 50 61 urn 1;. if( iPa
18bb8 67 65 3e 70 43 68 65 63 6b 2d 3e 6e 50 61 67 65 ge>pCheck->nPage
18bb9 20 29 7b 0a 20 20 20 20 63 68 65 63 6b 41 70 70 ){. checkApp
18bba 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a endMsg(pCheck, z
18bbb 43 6f 6e 74 65 78 74 2c 20 22 69 6e 76 61 6c 69 Context, "invali
18bbc 64 20 70 61 67 65 20 6e 75 6d 62 65 72 20 25 64 d page number %d
18bbd 22 2c 20 69 50 61 67 65 29 3b 0a 20 20 20 20 72 ", iPage);. r
18bbe 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 69 eturn 1;. }. i
18bbf 66 28 20 70 43 68 65 63 6b 2d 3e 61 6e 52 65 66 f( pCheck->anRef
18bc0 5b 69 50 61 67 65 5d 3d 3d 31 20 29 7b 0a 20 20 [iPage]==1 ){.
18bc1 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 checkAppendMsg
18bc2 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 (pCheck, zContex
18bc3 74 2c 20 22 32 6e 64 20 72 65 66 65 72 65 6e 63 t, "2nd referenc
18bc4 65 20 74 6f 20 70 61 67 65 20 25 64 22 2c 20 69 e to page %d", i
18bc5 50 61 67 65 29 3b 0a 20 20 20 20 72 65 74 75 72 Page);. retur
18bc6 6e 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 n 1;. }. retur
18bc7 6e 20 20 28 70 43 68 65 63 6b 2d 3e 61 6e 52 65 n (pCheck->anRe
18bc8 66 5b 69 50 61 67 65 5d 2b 2b 29 3e 31 3b 0a 7d f[iPage]++)>1;.}
18bc9 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
18bca 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d _OMIT_AUTOVACUUM
18bcb 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68 61 ./*.** Check tha
18bcc 74 20 74 68 65 20 65 6e 74 72 79 20 69 6e 20 74 t the entry in t
18bcd 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 66 he pointer-map f
18bce 6f 72 20 70 61 67 65 20 69 43 68 69 6c 64 20 6d or page iChild m
18bcf 61 70 73 20 74 6f 20 0a 2a 2a 20 70 61 67 65 20 aps to .** page
18bd0 69 50 61 72 65 6e 74 2c 20 70 6f 69 6e 74 65 72 iParent, pointer
18bd1 20 74 79 70 65 20 70 74 72 54 79 70 65 2e 20 49 type ptrType. I
18bd2 66 20 6e 6f 74 2c 20 61 70 70 65 6e 64 20 61 6e f not, append an
18bd3 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 0a 2a error message.*
18bd4 2a 20 74 6f 20 70 43 68 65 63 6b 2e 0a 2a 2f 0a * to pCheck..*/.
18bd5 73 74 61 74 69 63 20 76 6f 69 64 20 63 68 65 63 static void chec
18bd6 6b 50 74 72 6d 61 70 28 0a 20 20 49 6e 74 65 67 kPtrmap(. Integ
18bd7 72 69 74 79 43 6b 20 2a 70 43 68 65 63 6b 2c 20 rityCk *pCheck,
18bd8 20 20 2f 2a 20 49 6e 74 65 67 72 69 74 79 20 63 /* Integrity c
18bd9 68 65 63 6b 20 63 6f 6e 74 65 78 74 20 2a 2f 0a heck context */.
18bda 20 20 50 67 6e 6f 20 69 43 68 69 6c 64 2c 20 20 Pgno iChild,
18bdb 20 20 20 20 20 20 20 20 20 2f 2a 20 43 68 69 6c /* Chil
18bdc 64 20 70 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f d page number */
18bdd 0a 20 20 75 38 20 65 54 79 70 65 2c 20 20 20 20 . u8 eType,
18bde 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70 /* Exp
18bdf 65 63 74 65 64 20 70 6f 69 6e 74 65 72 20 6d 61 ected pointer ma
18be0 70 20 74 79 70 65 20 2a 2f 0a 20 20 50 67 6e 6f p type */. Pgno
18be1 20 69 50 61 72 65 6e 74 2c 20 20 20 20 20 20 20 iParent,
18be2 20 20 20 2f 2a 20 45 78 70 65 63 74 65 64 20 70 /* Expected p
18be3 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 72 65 6e ointer map paren
18be4 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f t page number */
18be5 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6e 74 65 78 . char *zContex
18be6 74 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e t /* Con
18be7 74 65 78 74 20 64 65 73 63 72 69 70 74 69 6f 6e text description
18be8 20 28 75 73 65 64 20 66 6f 72 20 65 72 72 6f 72 (used for error
18be9 20 6d 73 67 29 20 2a 2f 0a 29 7b 0a 20 20 69 6e msg) */.){. in
18bea 74 20 72 63 3b 0a 20 20 75 38 20 65 50 74 72 6d t rc;. u8 ePtrm
18beb 61 70 54 79 70 65 3b 0a 20 20 50 67 6e 6f 20 69 apType;. Pgno i
18bec 50 74 72 6d 61 70 50 61 72 65 6e 74 3b 0a 0a 20 PtrmapParent;..
18bed 20 72 63 20 3d 20 70 74 72 6d 61 70 47 65 74 28 rc = ptrmapGet(
18bee 70 43 68 65 63 6b 2d 3e 70 42 74 2c 20 69 43 68 pCheck->pBt, iCh
18bef 69 6c 64 2c 20 26 65 50 74 72 6d 61 70 54 79 70 ild, &ePtrmapTyp
18bf0 65 2c 20 26 69 50 74 72 6d 61 70 50 61 72 65 6e e, &iPtrmapParen
18bf1 74 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 t);. if( rc!=SQ
18bf2 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 LITE_OK ){. i
18bf3 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f f( rc==SQLITE_NO
18bf4 4d 45 4d 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 MEM || rc==SQLIT
18bf5 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 20 E_IOERR_NOMEM )
18bf6 70 43 68 65 63 6b 2d 3e 6d 61 6c 6c 6f 63 46 61 pCheck->mallocFa
18bf7 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 63 68 iled = 1;. ch
18bf8 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 eckAppendMsg(pCh
18bf9 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22 eck, zContext, "
18bfa 46 61 69 6c 65 64 20 74 6f 20 72 65 61 64 20 70 Failed to read p
18bfb 74 72 6d 61 70 20 6b 65 79 3d 25 64 22 2c 20 69 trmap key=%d", i
18bfc 43 68 69 6c 64 29 3b 0a 20 20 20 20 72 65 74 75 Child);. retu
18bfd 72 6e 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 65 rn;. }.. if( e
18bfe 50 74 72 6d 61 70 54 79 70 65 21 3d 65 54 79 70 PtrmapType!=eTyp
18bff 65 20 7c 7c 20 69 50 74 72 6d 61 70 50 61 72 65 e || iPtrmapPare
18c00 6e 74 21 3d 69 50 61 72 65 6e 74 20 29 7b 0a 20 nt!=iParent ){.
18c01 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 checkAppendMs
18c02 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 g(pCheck, zConte
18c03 78 74 2c 20 0a 20 20 20 20 20 20 22 42 61 64 20 xt, . "Bad
18c04 70 74 72 20 6d 61 70 20 65 6e 74 72 79 20 6b 65 ptr map entry ke
18c05 79 3d 25 64 20 65 78 70 65 63 74 65 64 3d 28 25 y=%d expected=(%
18c06 64 2c 25 64 29 20 67 6f 74 3d 28 25 64 2c 25 64 d,%d) got=(%d,%d
18c07 29 22 2c 20 0a 20 20 20 20 20 20 69 43 68 69 6c )", . iChil
18c08 64 2c 20 65 54 79 70 65 2c 20 69 50 61 72 65 6e d, eType, iParen
18c09 74 2c 20 65 50 74 72 6d 61 70 54 79 70 65 2c 20 t, ePtrmapType,
18c0a 69 50 74 72 6d 61 70 50 61 72 65 6e 74 29 3b 0a iPtrmapParent);.
18c0b 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a }.}.#endif../*
18c0c 0a 2a 2a 20 43 68 65 63 6b 20 74 68 65 20 69 6e .** Check the in
18c0d 74 65 67 72 69 74 79 20 6f 66 20 74 68 65 20 66 tegrity of the f
18c0e 72 65 65 6c 69 73 74 20 6f 72 20 6f 66 20 61 6e reelist or of an
18c0f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 6c overflow page l
18c10 69 73 74 2e 0a 2a 2a 20 56 65 72 69 66 79 20 74 ist..** Verify t
18c11 68 61 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f hat the number o
18c12 66 20 70 61 67 65 73 20 6f 6e 20 74 68 65 20 6c f pages on the l
18c13 69 73 74 20 69 73 20 4e 2e 0a 2a 2f 0a 73 74 61 ist is N..*/.sta
18c14 74 69 63 20 76 6f 69 64 20 63 68 65 63 6b 4c 69 tic void checkLi
18c15 73 74 28 0a 20 20 49 6e 74 65 67 72 69 74 79 43 st(. IntegrityC
18c16 6b 20 2a 70 43 68 65 63 6b 2c 20 20 2f 2a 20 49 k *pCheck, /* I
18c17 6e 74 65 67 72 69 74 79 20 63 68 65 63 6b 69 6e ntegrity checkin
18c18 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 g context */. i
18c19 6e 74 20 69 73 46 72 65 65 4c 69 73 74 2c 20 20 nt isFreeList,
18c1a 20 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 /* True for
18c1b 20 61 20 66 72 65 65 6c 69 73 74 2e 20 20 46 61 a freelist. Fa
18c1c 6c 73 65 20 66 6f 72 20 6f 76 65 72 66 6c 6f 77 lse for overflow
18c1d 20 70 61 67 65 20 6c 69 73 74 20 2a 2f 0a 20 20 page list */.
18c1e 69 6e 74 20 69 50 61 67 65 2c 20 20 20 20 20 20 int iPage,
18c1f 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 /* Page nu
18c20 6d 62 65 72 20 66 6f 72 20 66 69 72 73 74 20 70 mber for first p
18c21 61 67 65 20 69 6e 20 74 68 65 20 6c 69 73 74 20 age in the list
18c22 2a 2f 0a 20 20 69 6e 74 20 4e 2c 20 20 20 20 20 */. int N,
18c23 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 /* Ex
18c24 70 65 63 74 65 64 20 6e 75 6d 62 65 72 20 6f 66 pected number of
18c25 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 6c 69 pages in the li
18c26 73 74 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 43 st */. char *zC
18c27 6f 6e 74 65 78 74 20 20 20 20 20 20 20 20 2f 2a ontext /*
18c28 20 43 6f 6e 74 65 78 74 20 66 6f 72 20 65 72 72 Context for err
18c29 6f 72 20 6d 65 73 73 61 67 65 73 20 2a 2f 0a 29 or messages */.)
18c2a 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 {. int i;. int
18c2b 20 65 78 70 65 63 74 65 64 20 3d 20 4e 3b 0a 20 expected = N;.
18c2c 20 69 6e 74 20 69 46 69 72 73 74 20 3d 20 69 50 int iFirst = iP
18c2d 61 67 65 3b 0a 20 20 77 68 69 6c 65 28 20 4e 2d age;. while( N-
18c2e 2d 20 3e 20 30 20 26 26 20 70 43 68 65 63 6b 2d - > 0 && pCheck-
18c2f 3e 6d 78 45 72 72 20 29 7b 0a 20 20 20 20 44 62 >mxErr ){. Db
18c30 50 61 67 65 20 2a 70 4f 76 66 6c 50 61 67 65 3b Page *pOvflPage;
18c31 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 . unsigned ch
18c32 61 72 20 2a 70 4f 76 66 6c 44 61 74 61 3b 0a 20 ar *pOvflData;.
18c33 20 20 20 69 66 28 20 69 50 61 67 65 3c 31 20 29 if( iPage<1 )
18c34 7b 0a 20 20 20 20 20 20 63 68 65 63 6b 41 70 70 {. checkApp
18c35 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a endMsg(pCheck, z
18c36 43 6f 6e 74 65 78 74 2c 0a 20 20 20 20 20 20 20 Context,.
18c37 20 20 22 25 64 20 6f 66 20 25 64 20 70 61 67 65 "%d of %d page
18c38 73 20 6d 69 73 73 69 6e 67 20 66 72 6f 6d 20 6f s missing from o
18c39 76 65 72 66 6c 6f 77 20 6c 69 73 74 20 73 74 61 verflow list sta
18c3a 72 74 69 6e 67 20 61 74 20 25 64 22 2c 0a 20 20 rting at %d",.
18c3b 20 20 20 20 20 20 20 20 4e 2b 31 2c 20 65 78 70 N+1, exp
18c3c 65 63 74 65 64 2c 20 69 46 69 72 73 74 29 3b 0a ected, iFirst);.
18c3d 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
18c3e 20 7d 0a 20 20 20 20 69 66 28 20 63 68 65 63 6b }. if( check
18c3f 52 65 66 28 70 43 68 65 63 6b 2c 20 69 50 61 67 Ref(pCheck, iPag
18c40 65 2c 20 7a 43 6f 6e 74 65 78 74 29 20 29 20 62 e, zContext) ) b
18c41 72 65 61 6b 3b 0a 20 20 20 20 69 66 28 20 73 71 reak;. if( sq
18c42 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 70 43 lite3PagerGet(pC
18c43 68 65 63 6b 2d 3e 70 50 61 67 65 72 2c 20 28 50 heck->pPager, (P
18c44 67 6e 6f 29 69 50 61 67 65 2c 20 26 70 4f 76 66 gno)iPage, &pOvf
18c45 6c 50 61 67 65 29 20 29 7b 0a 20 20 20 20 20 20 lPage) ){.
18c46 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 checkAppendMsg(p
18c47 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c Check, zContext,
18c48 20 22 66 61 69 6c 65 64 20 74 6f 20 67 65 74 20 "failed to get
18c49 70 61 67 65 20 25 64 22 2c 20 69 50 61 67 65 29 page %d", iPage)
18c4a 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
18c4b 20 20 20 7d 0a 20 20 20 20 70 4f 76 66 6c 44 61 }. pOvflDa
18c4c 74 61 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 ta = (unsigned c
18c4d 68 61 72 20 2a 29 73 71 6c 69 74 65 33 50 61 67 har *)sqlite3Pag
18c4e 65 72 47 65 74 44 61 74 61 28 70 4f 76 66 6c 50 erGetData(pOvflP
18c4f 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 69 73 age);. if( is
18c50 46 72 65 65 4c 69 73 74 20 29 7b 0a 20 20 20 20 FreeList ){.
18c51 20 20 69 6e 74 20 6e 20 3d 20 67 65 74 34 62 79 int n = get4by
18c52 74 65 28 26 70 4f 76 66 6c 44 61 74 61 5b 34 5d te(&pOvflData[4]
18c53 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 );.#ifndef SQLIT
18c54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 E_OMIT_AUTOVACUU
18c55 4d 0a 20 20 20 20 20 20 69 66 28 20 70 43 68 65 M. if( pChe
18c56 63 6b 2d 3e 70 42 74 2d 3e 61 75 74 6f 56 61 63 ck->pBt->autoVac
18c57 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 63 uum ){. c
18c58 68 65 63 6b 50 74 72 6d 61 70 28 70 43 68 65 63 heckPtrmap(pChec
18c59 6b 2c 20 69 50 61 67 65 2c 20 50 54 52 4d 41 50 k, iPage, PTRMAP
18c5a 5f 46 52 45 45 50 41 47 45 2c 20 30 2c 20 7a 43 _FREEPAGE, 0, zC
18c5b 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 7d ontext);. }
18c5c 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 69 66 .#endif. if
18c5d 28 20 6e 3e 70 43 68 65 63 6b 2d 3e 70 42 74 2d ( n>pCheck->pBt-
18c5e 3e 75 73 61 62 6c 65 53 69 7a 65 2f 34 2d 32 20 >usableSize/4-2
18c5f 29 7b 0a 20 20 20 20 20 20 20 20 63 68 65 63 6b ){. check
18c60 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b AppendMsg(pCheck
18c61 2c 20 7a 43 6f 6e 74 65 78 74 2c 0a 20 20 20 20 , zContext,.
18c62 20 20 20 20 20 20 20 22 66 72 65 65 6c 69 73 74 "freelist
18c63 20 6c 65 61 66 20 63 6f 75 6e 74 20 74 6f 6f 20 leaf count too
18c64 62 69 67 20 6f 6e 20 70 61 67 65 20 25 64 22 2c big on page %d",
18c65 20 69 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 iPage);.
18c66 20 4e 2d 2d 3b 0a 20 20 20 20 20 20 7d 65 6c 73 N--;. }els
18c67 65 7b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 e{. for(i
18c68 3d 30 3b 20 69 3c 6e 3b 20 69 2b 2b 29 7b 0a 20 =0; i<n; i++){.
18c69 20 20 20 20 20 20 20 20 20 50 67 6e 6f 20 69 46 Pgno iF
18c6a 72 65 65 50 61 67 65 20 3d 20 67 65 74 34 62 79 reePage = get4by
18c6b 74 65 28 26 70 4f 76 66 6c 44 61 74 61 5b 38 2b te(&pOvflData[8+
18c6c 69 2a 34 5d 29 3b 0a 23 69 66 6e 64 65 66 20 53 i*4]);.#ifndef S
18c6d 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 QLITE_OMIT_AUTOV
18c6e 41 43 55 55 4d 0a 20 20 20 20 20 20 20 20 20 20 ACUUM.
18c6f 69 66 28 20 70 43 68 65 63 6b 2d 3e 70 42 74 2d if( pCheck->pBt-
18c70 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 >autoVacuum ){.
18c71 20 20 20 20 20 20 20 20 20 20 20 63 68 65 63 6b check
18c72 50 74 72 6d 61 70 28 70 43 68 65 63 6b 2c 20 69 Ptrmap(pCheck, i
18c73 46 72 65 65 50 61 67 65 2c 20 50 54 52 4d 41 50 FreePage, PTRMAP
18c74 5f 46 52 45 45 50 41 47 45 2c 20 30 2c 20 7a 43 _FREEPAGE, 0, zC
18c75 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 20 ontext);.
18c76 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 }.#endif.
18c77 20 20 20 20 20 20 63 68 65 63 6b 52 65 66 28 70 checkRef(p
18c78 43 68 65 63 6b 2c 20 69 46 72 65 65 50 61 67 65 Check, iFreePage
18c79 2c 20 7a 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20 , zContext);.
18c7a 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 4e }. N
18c7b 20 2d 3d 20 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 -= n;. }.
18c7c 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c }.#ifndef SQL
18c7d 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 ITE_OMIT_AUTOVAC
18c7e 55 55 4d 0a 20 20 20 20 65 6c 73 65 7b 0a 20 20 UUM. else{.
18c7f 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 64 /* If this d
18c80 61 74 61 62 61 73 65 20 73 75 70 70 6f 72 74 73 atabase supports
18c81 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 61 6e 64 auto-vacuum and
18c82 20 69 50 61 67 65 20 69 73 20 6e 6f 74 20 74 68 iPage is not th
18c83 65 20 6c 61 73 74 0a 20 20 20 20 20 20 2a 2a 20 e last. **
18c84 70 61 67 65 20 69 6e 20 74 68 69 73 20 6f 76 65 page in this ove
18c85 72 66 6c 6f 77 20 6c 69 73 74 2c 20 63 68 65 63 rflow list, chec
18c86 6b 20 74 68 61 74 20 74 68 65 20 70 6f 69 6e 74 k that the point
18c87 65 72 2d 6d 61 70 20 65 6e 74 72 79 20 66 6f 72 er-map entry for
18c88 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 66 6f . ** the fo
18c89 6c 6c 6f 77 69 6e 67 20 70 61 67 65 20 6d 61 74 llowing page mat
18c8a 63 68 65 73 20 69 50 61 67 65 2e 0a 20 20 20 20 ches iPage..
18c8b 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 */. if( p
18c8c 43 68 65 63 6b 2d 3e 70 42 74 2d 3e 61 75 74 6f Check->pBt->auto
18c8d 56 61 63 75 75 6d 20 26 26 20 4e 3e 30 20 29 7b Vacuum && N>0 ){
18c8e 0a 20 20 20 20 20 20 20 20 69 20 3d 20 67 65 74 . i = get
18c8f 34 62 79 74 65 28 70 4f 76 66 6c 44 61 74 61 29 4byte(pOvflData)
18c90 3b 0a 20 20 20 20 20 20 20 20 63 68 65 63 6b 50 ;. checkP
18c91 74 72 6d 61 70 28 70 43 68 65 63 6b 2c 20 69 2c trmap(pCheck, i,
18c92 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 PTRMAP_OVERFLOW
18c93 32 2c 20 69 50 61 67 65 2c 20 7a 43 6f 6e 74 65 2, iPage, zConte
18c94 78 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 xt);. }.
18c95 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 69 50 }.#endif. iP
18c96 61 67 65 20 3d 20 67 65 74 34 62 79 74 65 28 70 age = get4byte(p
18c97 4f 76 66 6c 44 61 74 61 29 3b 0a 20 20 20 20 73 OvflData);. s
18c98 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 qlite3PagerUnref
18c99 28 70 4f 76 66 6c 50 61 67 65 29 3b 0a 20 20 7d (pOvflPage);. }
18c9a 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c .}.#endif /* SQL
18c9b 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 ITE_OMIT_INTEGRI
18c9c 54 59 5f 43 48 45 43 4b 20 2a 2f 0a 0a 23 69 66 TY_CHECK */..#if
18c9d 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
18c9e 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b _INTEGRITY_CHECK
18c9f 0a 2f 2a 0a 2a 2a 20 44 6f 20 76 61 72 69 6f 75 ./*.** Do variou
18ca0 73 20 73 61 6e 69 74 79 20 63 68 65 63 6b 73 20 s sanity checks
18ca1 6f 6e 20 61 20 73 69 6e 67 6c 65 20 70 61 67 65 on a single page
18ca2 20 6f 66 20 61 20 74 72 65 65 2e 20 20 52 65 74 of a tree. Ret
18ca3 75 72 6e 0a 2a 2a 20 74 68 65 20 74 72 65 65 20 urn.** the tree
18ca4 64 65 70 74 68 2e 20 20 52 6f 6f 74 20 70 61 67 depth. Root pag
18ca5 65 73 20 72 65 74 75 72 6e 20 30 2e 20 20 50 61 es return 0. Pa
18ca6 72 65 6e 74 73 20 6f 66 20 72 6f 6f 74 20 70 61 rents of root pa
18ca7 67 65 73 0a 2a 2a 20 72 65 74 75 72 6e 20 31 2c ges.** return 1,
18ca8 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 2e 0a 2a and so forth..*
18ca9 2a 20 0a 2a 2a 20 54 68 65 73 65 20 63 68 65 63 * .** These chec
18caa 6b 73 20 61 72 65 20 64 6f 6e 65 3a 0a 2a 2a 0a ks are done:.**.
18cab 2a 2a 20 20 20 20 20 20 31 2e 20 20 4d 61 6b 65 ** 1. Make
18cac 20 73 75 72 65 20 74 68 61 74 20 63 65 6c 6c 73 sure that cells
18cad 20 61 6e 64 20 66 72 65 65 62 6c 6f 63 6b 73 20 and freeblocks
18cae 64 6f 20 6e 6f 74 20 6f 76 65 72 6c 61 70 0a 2a do not overlap.*
18caf 2a 20 20 20 20 20 20 20 20 20 20 62 75 74 20 63 * but c
18cb0 6f 6d 62 69 6e 65 20 74 6f 20 63 6f 6d 70 6c 65 ombine to comple
18cb1 74 65 6c 79 20 63 6f 76 65 72 20 74 68 65 20 70 tely cover the p
18cb2 61 67 65 2e 0a 2a 2a 20 20 4e 4f 20 20 32 2e 20 age..** NO 2.
18cb3 20 4d 61 6b 65 20 73 75 72 65 20 63 65 6c 6c 20 Make sure cell
18cb4 6b 65 79 73 20 61 72 65 20 69 6e 20 6f 72 64 65 keys are in orde
18cb5 72 2e 0a 2a 2a 20 20 4e 4f 20 20 33 2e 20 20 4d r..** NO 3. M
18cb6 61 6b 65 20 73 75 72 65 20 6e 6f 20 6b 65 79 20 ake sure no key
18cb7 69 73 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 is less than or
18cb8 65 71 75 61 6c 20 74 6f 20 7a 4c 6f 77 65 72 42 equal to zLowerB
18cb9 6f 75 6e 64 2e 0a 2a 2a 20 20 4e 4f 20 20 34 2e ound..** NO 4.
18cba 20 20 4d 61 6b 65 20 73 75 72 65 20 6e 6f 20 6b Make sure no k
18cbb 65 79 20 69 73 20 67 72 65 61 74 65 72 20 74 68 ey is greater th
18cbc 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 7a an or equal to z
18cbd 55 70 70 65 72 42 6f 75 6e 64 2e 0a 2a 2a 20 20 UpperBound..**
18cbe 20 20 20 20 35 2e 20 20 43 68 65 63 6b 20 74 68 5. Check th
18cbf 65 20 69 6e 74 65 67 72 69 74 79 20 6f 66 20 6f e integrity of o
18cc0 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 0a 2a verflow pages..*
18cc1 2a 20 20 20 20 20 20 36 2e 20 20 52 65 63 75 72 * 6. Recur
18cc2 73 69 76 65 6c 79 20 63 61 6c 6c 20 63 68 65 63 sively call chec
18cc3 6b 54 72 65 65 50 61 67 65 20 6f 6e 20 61 6c 6c kTreePage on all
18cc4 20 63 68 69 6c 64 72 65 6e 2e 0a 2a 2a 20 20 20 children..**
18cc5 20 20 20 37 2e 20 20 56 65 72 69 66 79 20 74 68 7. Verify th
18cc6 61 74 20 74 68 65 20 64 65 70 74 68 20 6f 66 20 at the depth of
18cc7 61 6c 6c 20 63 68 69 6c 64 72 65 6e 20 69 73 20 all children is
18cc8 74 68 65 20 73 61 6d 65 2e 0a 2a 2a 20 20 20 20 the same..**
18cc9 20 20 38 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 8. Make sure
18cca 74 68 69 73 20 70 61 67 65 20 69 73 20 61 74 20 this page is at
18ccb 6c 65 61 73 74 20 33 33 25 20 66 75 6c 6c 20 6f least 33% full o
18ccc 72 20 65 6c 73 65 20 69 74 20 69 73 0a 2a 2a 20 r else it is.**
18ccd 20 20 20 20 20 20 20 20 20 74 68 65 20 72 6f 6f the roo
18cce 74 20 6f 66 20 74 68 65 20 74 72 65 65 2e 0a 2a t of the tree..*
18ccf 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 68 65 /.static int che
18cd0 63 6b 54 72 65 65 50 61 67 65 28 0a 20 20 49 6e ckTreePage(. In
18cd1 74 65 67 72 69 74 79 43 6b 20 2a 70 43 68 65 63 tegrityCk *pChec
18cd2 6b 2c 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20 66 k, /* Context f
18cd3 6f 72 20 74 68 65 20 73 61 6e 69 74 79 20 63 68 or the sanity ch
18cd4 65 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 69 50 61 eck */. int iPa
18cd5 67 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f ge, /
18cd6 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 * Page number of
18cd7 20 74 68 65 20 70 61 67 65 20 74 6f 20 63 68 65 the page to che
18cd8 63 6b 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 50 ck */. char *zP
18cd9 61 72 65 6e 74 43 6f 6e 74 65 78 74 20 20 2f 2a arentContext /*
18cda 20 50 61 72 65 6e 74 20 63 6f 6e 74 65 78 74 20 Parent context
18cdb 2a 2f 0a 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 */.){. MemPage
18cdc 2a 70 50 61 67 65 3b 0a 20 20 69 6e 74 20 69 2c *pPage;. int i,
18cdd 20 72 63 2c 20 64 65 70 74 68 2c 20 64 32 2c 20 rc, depth, d2,
18cde 70 67 6e 6f 2c 20 63 6e 74 3b 0a 20 20 69 6e 74 pgno, cnt;. int
18cdf 20 68 64 72 2c 20 63 65 6c 6c 53 74 61 72 74 3b hdr, cellStart;
18ce0 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 3b 0a 20 20 . int nCell;.
18ce1 75 38 20 2a 64 61 74 61 3b 0a 20 20 42 74 53 68 u8 *data;. BtSh
18ce2 61 72 65 64 20 2a 70 42 74 3b 0a 20 20 69 6e 74 ared *pBt;. int
18ce3 20 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20 63 usableSize;. c
18ce4 68 61 72 20 7a 43 6f 6e 74 65 78 74 5b 31 30 30 har zContext[100
18ce5 5d 3b 0a 20 20 63 68 61 72 20 2a 68 69 74 20 3d ];. char *hit =
18ce6 20 30 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 73 0;.. sqlite3_s
18ce7 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a nprintf(sizeof(z
18ce8 43 6f 6e 74 65 78 74 29 2c 20 7a 43 6f 6e 74 65 Context), zConte
18ce9 78 74 2c 20 22 50 61 67 65 20 25 64 3a 20 22 2c xt, "Page %d: ",
18cea 20 69 50 61 67 65 29 3b 0a 0a 20 20 2f 2a 20 43 iPage);.. /* C
18ceb 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 70 61 heck that the pa
18cec 67 65 20 65 78 69 73 74 73 0a 20 20 2a 2f 0a 20 ge exists. */.
18ced 20 70 42 74 20 3d 20 70 43 68 65 63 6b 2d 3e 70 pBt = pCheck->p
18cee 42 74 3b 0a 20 20 75 73 61 62 6c 65 53 69 7a 65 Bt;. usableSize
18cef 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 = pBt->usableSi
18cf0 7a 65 3b 0a 20 20 69 66 28 20 69 50 61 67 65 3d ze;. if( iPage=
18cf1 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 =0 ) return 0;.
18cf2 20 69 66 28 20 63 68 65 63 6b 52 65 66 28 70 43 if( checkRef(pC
18cf3 68 65 63 6b 2c 20 69 50 61 67 65 2c 20 7a 50 61 heck, iPage, zPa
18cf4 72 65 6e 74 43 6f 6e 74 65 78 74 29 20 29 20 72 rentContext) ) r
18cf5 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 28 eturn 0;. if( (
18cf6 72 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 rc = btreeGetPag
18cf7 65 28 70 42 74 2c 20 28 50 67 6e 6f 29 69 50 61 e(pBt, (Pgno)iPa
18cf8 67 65 2c 20 26 70 50 61 67 65 2c 20 30 29 29 21 ge, &pPage, 0))!
18cf9 3d 30 20 29 7b 0a 20 20 20 20 63 68 65 63 6b 41 =0 ){. checkA
18cfa 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c ppendMsg(pCheck,
18cfb 20 7a 43 6f 6e 74 65 78 74 2c 0a 20 20 20 20 20 zContext,.
18cfc 20 20 22 75 6e 61 62 6c 65 20 74 6f 20 67 65 74 "unable to get
18cfd 20 74 68 65 20 70 61 67 65 2e 20 65 72 72 6f 72 the page. error
18cfe 20 63 6f 64 65 3d 25 64 22 2c 20 72 63 29 3b 0a code=%d", rc);.
18cff 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 return 0;.
18d00 7d 0a 0a 20 20 2f 2a 20 43 6c 65 61 72 20 4d 65 }.. /* Clear Me
18d01 6d 50 61 67 65 2e 69 73 49 6e 69 74 20 74 6f 20 mPage.isInit to
18d02 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20 63 6f make sure the co
18d03 72 72 75 70 74 69 6f 6e 20 64 65 74 65 63 74 69 rruption detecti
18d04 6f 6e 20 63 6f 64 65 20 69 6e 0a 20 20 2a 2a 20 on code in. **
18d05 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 29 20 btreeInitPage()
18d06 69 73 20 65 78 65 63 75 74 65 64 2e 20 20 2a 2f is executed. */
18d07 0a 20 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 . pPage->isInit
18d08 20 3d 20 30 3b 0a 20 20 69 66 28 20 28 72 63 20 = 0;. if( (rc
18d09 3d 20 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 = btreeInitPage(
18d0a 70 50 61 67 65 29 29 21 3d 30 20 29 7b 0a 20 20 pPage))!=0 ){.
18d0b 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 assert( rc==SQ
18d0c 4c 49 54 45 5f 43 4f 52 52 55 50 54 20 29 3b 20 LITE_CORRUPT );
18d0d 20 2f 2a 20 54 68 65 20 6f 6e 6c 79 20 70 6f 73 /* The only pos
18d0e 73 69 62 6c 65 20 65 72 72 6f 72 20 66 72 6f 6d sible error from
18d0f 20 49 6e 69 74 50 61 67 65 20 2a 2f 0a 20 20 20 InitPage */.
18d10 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 checkAppendMsg(
18d11 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 pCheck, zContext
18d12 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 , .
18d13 20 20 20 20 20 20 22 62 74 72 65 65 49 6e 69 74 "btreeInit
18d14 50 61 67 65 28 29 20 72 65 74 75 72 6e 73 20 65 Page() returns e
18d15 72 72 6f 72 20 63 6f 64 65 20 25 64 22 2c 20 72 rror code %d", r
18d16 63 29 3b 0a 20 20 20 20 72 65 6c 65 61 73 65 50 c);. releaseP
18d17 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 age(pPage);.
18d18 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 return 0;. }..
18d19 20 2f 2a 20 43 68 65 63 6b 20 6f 75 74 20 61 6c /* Check out al
18d1a 6c 20 74 68 65 20 63 65 6c 6c 73 2e 0a 20 20 2a l the cells.. *
18d1b 2f 0a 20 20 64 65 70 74 68 20 3d 20 30 3b 0a 20 /. depth = 0;.
18d1c 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50 61 67 for(i=0; i<pPag
18d1d 65 2d 3e 6e 43 65 6c 6c 20 26 26 20 70 43 68 65 e->nCell && pChe
18d1e 63 6b 2d 3e 6d 78 45 72 72 3b 20 69 2b 2b 29 7b ck->mxErr; i++){
18d1f 0a 20 20 20 20 75 38 20 2a 70 43 65 6c 6c 3b 0a . u8 *pCell;.
18d20 20 20 20 20 75 33 32 20 73 7a 3b 0a 20 20 20 20 u32 sz;.
18d21 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 0a CellInfo info;..
18d22 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 70 61 79 /* Check pay
18d23 6c 6f 61 64 20 6f 76 65 72 66 6c 6f 77 20 70 61 load overflow pa
18d24 67 65 73 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 ges. */. s
18d25 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
18d26 73 69 7a 65 6f 66 28 7a 43 6f 6e 74 65 78 74 29 sizeof(zContext)
18d27 2c 20 7a 43 6f 6e 74 65 78 74 2c 0a 20 20 20 20 , zContext,.
18d28 20 20 20 20 20 20 20 20 20 22 4f 6e 20 74 72 65 "On tre
18d29 65 20 70 61 67 65 20 25 64 20 63 65 6c 6c 20 25 e page %d cell %
18d2a 64 3a 20 22 2c 20 69 50 61 67 65 2c 20 69 29 3b d: ", iPage, i);
18d2b 0a 20 20 20 20 70 43 65 6c 6c 20 3d 20 66 69 6e . pCell = fin
18d2c 64 43 65 6c 6c 28 70 50 61 67 65 2c 69 29 3b 0a dCell(pPage,i);.
18d2d 20 20 20 20 62 74 72 65 65 50 61 72 73 65 43 65 btreeParseCe
18d2e 6c 6c 50 74 72 28 70 50 61 67 65 2c 20 70 43 65 llPtr(pPage, pCe
18d2f 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 20 20 ll, &info);.
18d30 73 7a 20 3d 20 69 6e 66 6f 2e 6e 44 61 74 61 3b sz = info.nData;
18d31 0a 20 20 20 20 69 66 28 20 21 70 50 61 67 65 2d . if( !pPage-
18d32 3e 69 6e 74 4b 65 79 20 29 20 73 7a 20 2b 3d 20 >intKey ) sz +=
18d33 28 69 6e 74 29 69 6e 66 6f 2e 6e 4b 65 79 3b 0a (int)info.nKey;.
18d34 20 20 20 20 61 73 73 65 72 74 28 20 73 7a 3d 3d assert( sz==
18d35 69 6e 66 6f 2e 6e 50 61 79 6c 6f 61 64 20 29 3b info.nPayload );
18d36 0a 20 20 20 20 69 66 28 20 28 73 7a 3e 69 6e 66 . if( (sz>inf
18d37 6f 2e 6e 4c 6f 63 61 6c 29 20 0a 20 20 20 20 20 o.nLocal) .
18d38 26 26 20 28 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e && (&pCell[info.
18d39 69 4f 76 65 72 66 6c 6f 77 5d 3c 3d 26 70 50 61 iOverflow]<=&pPa
18d3a 67 65 2d 3e 61 44 61 74 61 5b 70 42 74 2d 3e 75 ge->aData[pBt->u
18d3b 73 61 62 6c 65 53 69 7a 65 5d 29 0a 20 20 20 20 sableSize]).
18d3c 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 50 61 ){. int nPa
18d3d 67 65 20 3d 20 28 73 7a 20 2d 20 69 6e 66 6f 2e ge = (sz - info.
18d3e 6e 4c 6f 63 61 6c 20 2b 20 75 73 61 62 6c 65 53 nLocal + usableS
18d3f 69 7a 65 20 2d 20 35 29 2f 28 75 73 61 62 6c 65 ize - 5)/(usable
18d40 53 69 7a 65 20 2d 20 34 29 3b 0a 20 20 20 20 20 Size - 4);.
18d41 20 50 67 6e 6f 20 70 67 6e 6f 4f 76 66 6c 20 3d Pgno pgnoOvfl =
18d42 20 67 65 74 34 62 79 74 65 28 26 70 43 65 6c 6c get4byte(&pCell
18d43 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d [info.iOverflow]
18d44 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 );.#ifndef SQLIT
18d45 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 E_OMIT_AUTOVACUU
18d46 4d 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 2d M. if( pBt-
18d47 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 >autoVacuum ){.
18d48 20 20 20 20 20 20 20 63 68 65 63 6b 50 74 72 6d checkPtrm
18d49 61 70 28 70 43 68 65 63 6b 2c 20 70 67 6e 6f 4f ap(pCheck, pgnoO
18d4a 76 66 6c 2c 20 50 54 52 4d 41 50 5f 4f 56 45 52 vfl, PTRMAP_OVER
18d4b 46 4c 4f 57 31 2c 20 69 50 61 67 65 2c 20 7a 43 FLOW1, iPage, zC
18d4c 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 7d ontext);. }
18d4d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 63 68 .#endif. ch
18d4e 65 63 6b 4c 69 73 74 28 70 43 68 65 63 6b 2c 20 eckList(pCheck,
18d4f 30 2c 20 70 67 6e 6f 4f 76 66 6c 2c 20 6e 50 61 0, pgnoOvfl, nPa
18d50 67 65 2c 20 7a 43 6f 6e 74 65 78 74 29 3b 0a 20 ge, zContext);.
18d51 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 68 65 }.. /* Che
18d52 63 6b 20 73 61 6e 69 74 79 20 6f 66 20 6c 65 66 ck sanity of lef
18d53 74 20 63 68 69 6c 64 20 70 61 67 65 2e 0a 20 20 t child page..
18d54 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 70 50 */. if( !pP
18d55 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 age->leaf ){.
18d56 20 20 20 70 67 6e 6f 20 3d 20 67 65 74 34 62 79 pgno = get4by
18d57 74 65 28 70 43 65 6c 6c 29 3b 0a 23 69 66 6e 64 te(pCell);.#ifnd
18d58 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ef SQLITE_OMIT_A
18d59 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 20 UTOVACUUM.
18d5a 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 if( pBt->autoVac
18d5b 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 63 uum ){. c
18d5c 68 65 63 6b 50 74 72 6d 61 70 28 70 43 68 65 63 heckPtrmap(pChec
18d5d 6b 2c 20 70 67 6e 6f 2c 20 50 54 52 4d 41 50 5f k, pgno, PTRMAP_
18d5e 42 54 52 45 45 2c 20 69 50 61 67 65 2c 20 7a 43 BTREE, iPage, zC
18d5f 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 7d ontext);. }
18d60 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 64 32 .#endif. d2
18d61 20 3d 20 63 68 65 63 6b 54 72 65 65 50 61 67 65 = checkTreePage
18d62 28 70 43 68 65 63 6b 2c 20 70 67 6e 6f 2c 20 7a (pCheck, pgno, z
18d63 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 Context);.
18d64 69 66 28 20 69 3e 30 20 26 26 20 64 32 21 3d 64 if( i>0 && d2!=d
18d65 65 70 74 68 20 29 7b 0a 20 20 20 20 20 20 20 20 epth ){.
18d66 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 checkAppendMsg(p
18d67 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c Check, zContext,
18d68 20 22 43 68 69 6c 64 20 70 61 67 65 20 64 65 70 "Child page dep
18d69 74 68 20 64 69 66 66 65 72 73 22 29 3b 0a 20 20 th differs");.
18d6a 20 20 20 20 7d 0a 20 20 20 20 20 20 64 65 70 74 }. dept
18d6b 68 20 3d 20 64 32 3b 0a 20 20 20 20 7d 0a 20 20 h = d2;. }.
18d6c 7d 0a 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e }. if( !pPage->
18d6d 6c 65 61 66 20 29 7b 0a 20 20 20 20 70 67 6e 6f leaf ){. pgno
18d6e 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 61 = get4byte(&pPa
18d6f 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d ge->aData[pPage-
18d70 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0a >hdrOffset+8]);.
18d71 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 sqlite3_snpr
18d72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 43 6f 6e intf(sizeof(zCon
18d73 74 65 78 74 29 2c 20 7a 43 6f 6e 74 65 78 74 2c text), zContext,
18d74 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
18d75 20 20 20 20 20 20 20 22 4f 6e 20 70 61 67 65 20 "On page
18d76 25 64 20 61 74 20 72 69 67 68 74 20 63 68 69 6c %d at right chil
18d77 64 3a 20 22 2c 20 69 50 61 67 65 29 3b 0a 23 69 d: ", iPage);.#i
18d78 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
18d79 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 T_AUTOVACUUM.
18d7a 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 if( pBt->autoVa
18d7b 63 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 63 68 cuum ){. ch
18d7c 65 63 6b 50 74 72 6d 61 70 28 70 43 68 65 63 6b eckPtrmap(pCheck
18d7d 2c 20 70 67 6e 6f 2c 20 50 54 52 4d 41 50 5f 42 , pgno, PTRMAP_B
18d7e 54 52 45 45 2c 20 69 50 61 67 65 2c 20 30 29 3b TREE, iPage, 0);
18d7f 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 . }.#endif.
18d80 20 20 63 68 65 63 6b 54 72 65 65 50 61 67 65 28 checkTreePage(
18d81 70 43 68 65 63 6b 2c 20 70 67 6e 6f 2c 20 7a 43 pCheck, pgno, zC
18d82 6f 6e 74 65 78 74 29 3b 0a 20 20 7d 0a 20 0a 20 ontext);. }. .
18d83 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72 20 63 6f /* Check for co
18d84 6d 70 6c 65 74 65 20 63 6f 76 65 72 61 67 65 20 mplete coverage
18d85 6f 66 20 74 68 65 20 70 61 67 65 0a 20 20 2a 2f of the page. */
18d86 0a 20 20 64 61 74 61 20 3d 20 70 50 61 67 65 2d . data = pPage-
18d87 3e 61 44 61 74 61 3b 0a 20 20 68 64 72 20 3d 20 >aData;. hdr =
18d88 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 pPage->hdrOffset
18d89 3b 0a 20 20 68 69 74 20 3d 20 73 71 6c 69 74 65 ;. hit = sqlite
18d8a 33 50 61 67 65 4d 61 6c 6c 6f 63 28 20 70 42 74 3PageMalloc( pBt
18d8b 2d 3e 70 61 67 65 53 69 7a 65 20 29 3b 0a 20 20 ->pageSize );.
18d8c 69 66 28 20 68 69 74 3d 3d 30 20 29 7b 0a 20 20 if( hit==0 ){.
18d8d 20 20 70 43 68 65 63 6b 2d 3e 6d 61 6c 6c 6f 63 pCheck->malloc
18d8e 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 65 Failed = 1;. }e
18d8f 6c 73 65 7b 0a 20 20 20 20 75 31 36 20 63 6f 6e lse{. u16 con
18d90 74 65 6e 74 4f 66 66 73 65 74 20 3d 20 67 65 74 tentOffset = get
18d91 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 2byte(&data[hdr+
18d92 35 5d 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 5]);. assert(
18d93 20 63 6f 6e 74 65 6e 74 4f 66 66 73 65 74 3c 3d contentOffset<=
18d94 75 73 61 62 6c 65 53 69 7a 65 20 29 3b 20 20 2f usableSize ); /
18d95 2a 20 45 6e 66 6f 72 63 65 64 20 62 79 20 62 74 * Enforced by bt
18d96 72 65 65 49 6e 69 74 50 61 67 65 28 29 20 2a 2f reeInitPage() */
18d97 0a 20 20 20 20 6d 65 6d 73 65 74 28 68 69 74 2b . memset(hit+
18d98 63 6f 6e 74 65 6e 74 4f 66 66 73 65 74 2c 20 30 contentOffset, 0
18d99 2c 20 75 73 61 62 6c 65 53 69 7a 65 2d 63 6f 6e , usableSize-con
18d9a 74 65 6e 74 4f 66 66 73 65 74 29 3b 0a 20 20 20 tentOffset);.
18d9b 20 6d 65 6d 73 65 74 28 68 69 74 2c 20 31 2c 20 memset(hit, 1,
18d9c 63 6f 6e 74 65 6e 74 4f 66 66 73 65 74 29 3b 0a contentOffset);.
18d9d 20 20 20 20 6e 43 65 6c 6c 20 3d 20 67 65 74 32 nCell = get2
18d9e 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 33 byte(&data[hdr+3
18d9f 5d 29 3b 0a 20 20 20 20 63 65 6c 6c 53 74 61 72 ]);. cellStar
18da0 74 20 3d 20 68 64 72 20 2b 20 31 32 20 2d 20 34 t = hdr + 12 - 4
18da1 2a 70 50 61 67 65 2d 3e 6c 65 61 66 3b 0a 20 20 *pPage->leaf;.
18da2 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 65 for(i=0; i<nCe
18da3 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 ll; i++){.
18da4 69 6e 74 20 70 63 20 3d 20 67 65 74 32 62 79 74 int pc = get2byt
18da5 65 28 26 64 61 74 61 5b 63 65 6c 6c 53 74 61 72 e(&data[cellStar
18da6 74 2b 69 2a 32 5d 29 3b 0a 20 20 20 20 20 20 75 t+i*2]);. u
18da7 31 36 20 73 69 7a 65 20 3d 20 31 30 32 34 3b 0a 16 size = 1024;.
18da8 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 int j;.
18da9 20 20 20 69 66 28 20 70 63 3c 3d 75 73 61 62 6c if( pc<=usabl
18daa 65 53 69 7a 65 2d 34 20 29 7b 0a 20 20 20 20 20 eSize-4 ){.
18dab 20 20 20 73 69 7a 65 20 3d 20 63 65 6c 6c 53 69 size = cellSi
18dac 7a 65 50 74 72 28 70 50 61 67 65 2c 20 26 64 61 zePtr(pPage, &da
18dad 74 61 5b 70 63 5d 29 3b 0a 20 20 20 20 20 20 7d ta[pc]);. }
18dae 0a 20 20 20 20 20 20 69 66 28 20 28 70 63 2b 73 . if( (pc+s
18daf 69 7a 65 2d 31 29 3e 3d 75 73 61 62 6c 65 53 69 ize-1)>=usableSi
18db0 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 ze ){. ch
18db1 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 eckAppendMsg(pCh
18db2 65 63 6b 2c 20 30 2c 20 0a 20 20 20 20 20 20 20 eck, 0, .
18db3 20 20 20 20 20 22 43 6f 72 72 75 70 74 69 6f 6e "Corruption
18db4 20 64 65 74 65 63 74 65 64 20 69 6e 20 63 65 6c detected in cel
18db5 6c 20 25 64 20 6f 6e 20 70 61 67 65 20 25 64 22 l %d on page %d"
18db6 2c 69 2c 69 50 61 67 65 2c 30 29 3b 0a 20 20 20 ,i,iPage,0);.
18db7 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
18db8 20 20 66 6f 72 28 6a 3d 70 63 2b 73 69 7a 65 2d for(j=pc+size-
18db9 31 3b 20 6a 3e 3d 70 63 3b 20 6a 2d 2d 29 20 68 1; j>=pc; j--) h
18dba 69 74 5b 6a 5d 2b 2b 3b 0a 20 20 20 20 20 20 7d it[j]++;. }
18dbb 0a 20 20 20 20 7d 0a 20 20 20 20 69 20 3d 20 67 . }. i = g
18dbc 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 et2byte(&data[hd
18dbd 72 2b 31 5d 29 3b 0a 20 20 20 20 77 68 69 6c 65 r+1]);. while
18dbe 28 20 69 3e 30 20 29 7b 0a 20 20 20 20 20 20 69 ( i>0 ){. i
18dbf 6e 74 20 73 69 7a 65 2c 20 6a 3b 0a 20 20 20 20 nt size, j;.
18dc0 20 20 61 73 73 65 72 74 28 20 69 3c 3d 75 73 61 assert( i<=usa
18dc1 62 6c 65 53 69 7a 65 2d 34 20 29 3b 20 20 20 20 bleSize-4 );
18dc2 20 2f 2a 20 45 6e 66 6f 72 63 65 64 20 62 79 20 /* Enforced by
18dc3 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 29 20 btreeInitPage()
18dc4 2a 2f 0a 20 20 20 20 20 20 73 69 7a 65 20 3d 20 */. size =
18dc5 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 69 get2byte(&data[i
18dc6 2b 32 5d 29 3b 0a 20 20 20 20 20 20 61 73 73 65 +2]);. asse
18dc7 72 74 28 20 69 2b 73 69 7a 65 3c 3d 75 73 61 62 rt( i+size<=usab
18dc8 6c 65 53 69 7a 65 20 29 3b 20 20 2f 2a 20 45 6e leSize ); /* En
18dc9 66 6f 72 63 65 64 20 62 79 20 62 74 72 65 65 49 forced by btreeI
18dca 6e 69 74 50 61 67 65 28 29 20 2a 2f 0a 20 20 20 nitPage() */.
18dcb 20 20 20 66 6f 72 28 6a 3d 69 2b 73 69 7a 65 2d for(j=i+size-
18dcc 31 3b 20 6a 3e 3d 69 3b 20 6a 2d 2d 29 20 68 69 1; j>=i; j--) hi
18dcd 74 5b 6a 5d 2b 2b 3b 0a 20 20 20 20 20 20 6a 20 t[j]++;. j
18dce 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 = get2byte(&data
18dcf 5b 69 5d 29 3b 0a 20 20 20 20 20 20 61 73 73 65 [i]);. asse
18dd0 72 74 28 20 6a 3d 3d 30 20 7c 7c 20 6a 3e 69 2b rt( j==0 || j>i+
18dd1 73 69 7a 65 20 29 3b 20 20 2f 2a 20 45 6e 66 6f size ); /* Enfo
18dd2 72 63 65 64 20 62 79 20 62 74 72 65 65 49 6e 69 rced by btreeIni
18dd3 74 50 61 67 65 28 29 20 2a 2f 0a 20 20 20 20 20 tPage() */.
18dd4 20 61 73 73 65 72 74 28 20 6a 3c 3d 75 73 61 62 assert( j<=usab
18dd5 6c 65 53 69 7a 65 2d 34 20 29 3b 20 20 20 2f 2a leSize-4 ); /*
18dd6 20 45 6e 66 6f 72 63 65 64 20 62 79 20 62 74 72 Enforced by btr
18dd7 65 65 49 6e 69 74 50 61 67 65 28 29 20 2a 2f 0a eeInitPage() */.
18dd8 20 20 20 20 20 20 69 20 3d 20 6a 3b 0a 20 20 20 i = j;.
18dd9 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d 63 6e 74 }. for(i=cnt
18dda 3d 30 3b 20 69 3c 75 73 61 62 6c 65 53 69 7a 65 =0; i<usableSize
18ddb 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 ; i++){. if
18ddc 28 20 68 69 74 5b 69 5d 3d 3d 30 20 29 7b 0a 20 ( hit[i]==0 ){.
18ddd 20 20 20 20 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 cnt++;.
18dde 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 68 69 }else if( hi
18ddf 74 5b 69 5d 3e 31 20 29 7b 0a 20 20 20 20 20 20 t[i]>1 ){.
18de0 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 checkAppendMsg
18de1 28 70 43 68 65 63 6b 2c 20 30 2c 0a 20 20 20 20 (pCheck, 0,.
18de2 20 20 20 20 20 20 22 4d 75 6c 74 69 70 6c 65 20 "Multiple
18de3 75 73 65 73 20 66 6f 72 20 62 79 74 65 20 25 64 uses for byte %d
18de4 20 6f 66 20 70 61 67 65 20 25 64 22 2c 20 69 2c of page %d", i,
18de5 20 69 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 iPage);.
18de6 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a break;. }.
18de7 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 63 6e }. if( cn
18de8 74 21 3d 64 61 74 61 5b 68 64 72 2b 37 5d 20 29 t!=data[hdr+7] )
18de9 7b 0a 20 20 20 20 20 20 63 68 65 63 6b 41 70 70 {. checkApp
18dea 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 30 endMsg(pCheck, 0
18deb 2c 20 0a 20 20 20 20 20 20 20 20 20 20 22 46 72 , . "Fr
18dec 61 67 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 25 agmentation of %
18ded 64 20 62 79 74 65 73 20 72 65 70 6f 72 74 65 64 d bytes reported
18dee 20 61 73 20 25 64 20 6f 6e 20 70 61 67 65 20 25 as %d on page %
18def 64 22 2c 0a 20 20 20 20 20 20 20 20 20 20 63 6e d",. cn
18df0 74 2c 20 64 61 74 61 5b 68 64 72 2b 37 5d 2c 20 t, data[hdr+7],
18df1 69 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20 20 iPage);. }.
18df2 7d 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 46 }. sqlite3PageF
18df3 72 65 65 28 68 69 74 29 3b 0a 20 20 72 65 6c 65 ree(hit);. rele
18df4 61 73 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a asePage(pPage);.
18df5 20 20 72 65 74 75 72 6e 20 64 65 70 74 68 2b 31 return depth+1
18df6 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 ;.}.#endif /* SQ
18df7 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 LITE_OMIT_INTEGR
18df8 49 54 59 5f 43 48 45 43 4b 20 2a 2f 0a 0a 23 69 ITY_CHECK */..#i
18df9 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
18dfa 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43 T_INTEGRITY_CHEC
18dfb 4b 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 K./*.** This rou
18dfc 74 69 6e 65 20 64 6f 65 73 20 61 20 63 6f 6d 70 tine does a comp
18dfd 6c 65 74 65 20 63 68 65 63 6b 20 6f 66 20 74 68 lete check of th
18dfe 65 20 67 69 76 65 6e 20 42 54 72 65 65 20 66 69 e given BTree fi
18dff 6c 65 2e 20 20 61 52 6f 6f 74 5b 5d 20 69 73 0a le. aRoot[] is.
18e00 2a 2a 20 61 6e 20 61 72 72 61 79 20 6f 66 20 70 ** an array of p
18e01 61 67 65 73 20 6e 75 6d 62 65 72 73 20 77 65 72 ages numbers wer
18e02 65 20 65 61 63 68 20 70 61 67 65 20 6e 75 6d 62 e each page numb
18e03 65 72 20 69 73 20 74 68 65 20 72 6f 6f 74 20 70 er is the root p
18e04 61 67 65 20 6f 66 0a 2a 2a 20 61 20 74 61 62 6c age of.** a tabl
18e05 65 2e 20 20 6e 52 6f 6f 74 20 69 73 20 74 68 65 e. nRoot is the
18e06 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 number of entri
18e07 65 73 20 69 6e 20 61 52 6f 6f 74 2e 0a 2a 2a 0a es in aRoot..**.
18e08 2a 2a 20 41 20 72 65 61 64 2d 6f 6e 6c 79 20 6f ** A read-only o
18e09 72 20 72 65 61 64 2d 77 72 69 74 65 20 74 72 61 r read-write tra
18e0a 6e 73 61 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 nsaction must be
18e0b 20 6f 70 65 6e 65 64 20 62 65 66 6f 72 65 20 63 opened before c
18e0c 61 6c 6c 69 6e 67 0a 2a 2a 20 74 68 69 73 20 66 alling.** this f
18e0d 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 57 unction..**.** W
18e0e 72 69 74 65 20 74 68 65 20 6e 75 6d 62 65 72 20 rite the number
18e0f 6f 66 20 65 72 72 6f 72 20 73 65 65 6e 20 69 6e of error seen in
18e10 20 2a 70 6e 45 72 72 2e 20 20 45 78 63 65 70 74 *pnErr. Except
18e11 20 66 6f 72 20 73 6f 6d 65 20 6d 65 6d 6f 72 79 for some memory
18e12 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 .** allocation e
18e13 72 72 6f 72 73 2c 20 20 61 6e 20 65 72 72 6f 72 rrors, an error
18e14 20 6d 65 73 73 61 67 65 20 68 65 6c 64 20 69 6e message held in
18e15 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 memory obtained
18e16 20 66 72 6f 6d 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 from.** malloc
18e17 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 2a is returned if *
18e18 70 6e 45 72 72 20 69 73 20 6e 6f 6e 2d 7a 65 72 pnErr is non-zer
18e19 6f 2e 20 20 49 66 20 2a 70 6e 45 72 72 3d 3d 30 o. If *pnErr==0
18e1a 20 74 68 65 6e 20 4e 55 4c 4c 20 69 73 0a 2a 2a then NULL is.**
18e1b 20 72 65 74 75 72 6e 65 64 2e 20 20 49 66 20 61 returned. If a
18e1c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
18e1d 6f 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c on error occurs,
18e1e 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 NULL is returne
18e1f 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 d..*/.SQLITE_PRI
18e20 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 VATE char *sqlit
18e21 65 33 42 74 72 65 65 49 6e 74 65 67 72 69 74 79 e3BtreeIntegrity
18e22 43 68 65 63 6b 28 0a 20 20 42 74 72 65 65 20 2a Check(. Btree *
18e23 70 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 62 74 p, /* The bt
18e24 72 65 65 20 74 6f 20 62 65 20 63 68 65 63 6b 65 ree to be checke
18e25 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 52 6f 6f d */. int *aRoo
18e26 74 2c 20 20 20 2f 2a 20 41 6e 20 61 72 72 61 79 t, /* An array
18e27 20 6f 66 20 72 6f 6f 74 20 70 61 67 65 73 20 6e of root pages n
18e28 75 6d 62 65 72 73 20 66 6f 72 20 69 6e 64 69 76 umbers for indiv
18e29 69 64 75 61 6c 20 74 72 65 65 73 20 2a 2f 0a 20 idual trees */.
18e2a 20 69 6e 74 20 6e 52 6f 6f 74 2c 20 20 20 20 2f int nRoot, /
18e2b 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 * Number of entr
18e2c 69 65 73 20 69 6e 20 61 52 6f 6f 74 5b 5d 20 2a ies in aRoot[] *
18e2d 2f 0a 20 20 69 6e 74 20 6d 78 45 72 72 2c 20 20 /. int mxErr,
18e2e 20 20 2f 2a 20 53 74 6f 70 20 72 65 70 6f 72 74 /* Stop report
18e2f 69 6e 67 20 65 72 72 6f 72 73 20 61 66 74 65 72 ing errors after
18e30 20 74 68 69 73 20 6d 61 6e 79 20 2a 2f 0a 20 20 this many */.
18e31 69 6e 74 20 2a 70 6e 45 72 72 20 20 20 20 2f 2a int *pnErr /*
18e32 20 57 72 69 74 65 20 6e 75 6d 62 65 72 20 6f 66 Write number of
18e33 20 65 72 72 6f 72 73 20 73 65 65 6e 20 74 6f 20 errors seen to
18e34 74 68 69 73 20 76 61 72 69 61 62 6c 65 20 2a 2f this variable */
18e35 0a 29 7b 0a 20 20 50 67 6e 6f 20 69 3b 0a 20 20 .){. Pgno i;.
18e36 69 6e 74 20 6e 52 65 66 3b 0a 20 20 49 6e 74 65 int nRef;. Inte
18e37 67 72 69 74 79 43 6b 20 73 43 68 65 63 6b 3b 0a grityCk sCheck;.
18e38 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 BtShared *pBt
18e39 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 63 68 61 72 = p->pBt;. char
18e3a 20 7a 45 72 72 5b 31 30 30 5d 3b 0a 0a 20 20 73 zErr[100];.. s
18e3b 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 qlite3BtreeEnter
18e3c 28 70 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 (p);. assert( p
18e3d 2d 3e 69 6e 54 72 61 6e 73 3e 54 52 41 4e 53 5f ->inTrans>TRANS_
18e3e 4e 4f 4e 45 20 26 26 20 70 42 74 2d 3e 69 6e 54 NONE && pBt->inT
18e3f 72 61 6e 73 61 63 74 69 6f 6e 3e 54 52 41 4e 53 ransaction>TRANS
18e40 5f 4e 4f 4e 45 20 29 3b 0a 20 20 6e 52 65 66 20 _NONE );. nRef
18e41 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 = sqlite3PagerRe
18e42 66 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 fcount(pBt->pPag
18e43 65 72 29 3b 0a 20 20 73 43 68 65 63 6b 2e 70 42 er);. sCheck.pB
18e44 74 20 3d 20 70 42 74 3b 0a 20 20 73 43 68 65 63 t = pBt;. sChec
18e45 6b 2e 70 50 61 67 65 72 20 3d 20 70 42 74 2d 3e k.pPager = pBt->
18e46 70 50 61 67 65 72 3b 0a 20 20 73 43 68 65 63 6b pPager;. sCheck
18e47 2e 6e 50 61 67 65 20 3d 20 70 61 67 65 72 50 61 .nPage = pagerPa
18e48 67 65 63 6f 75 6e 74 28 73 43 68 65 63 6b 2e 70 gecount(sCheck.p
18e49 42 74 29 3b 0a 20 20 73 43 68 65 63 6b 2e 6d 78 Bt);. sCheck.mx
18e4a 45 72 72 20 3d 20 6d 78 45 72 72 3b 0a 20 20 73 Err = mxErr;. s
18e4b 43 68 65 63 6b 2e 6e 45 72 72 20 3d 20 30 3b 0a Check.nErr = 0;.
18e4c 20 20 73 43 68 65 63 6b 2e 6d 61 6c 6c 6f 63 46 sCheck.mallocF
18e4d 61 69 6c 65 64 20 3d 20 30 3b 0a 20 20 2a 70 6e ailed = 0;. *pn
18e4e 45 72 72 20 3d 20 30 3b 0a 20 20 69 66 28 20 73 Err = 0;. if( s
18e4f 43 68 65 63 6b 2e 6e 50 61 67 65 3d 3d 30 20 29 Check.nPage==0 )
18e50 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 {. sqlite3Btr
18e51 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 20 20 eeLeave(p);.
18e52 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 return 0;. }.
18e53 73 43 68 65 63 6b 2e 61 6e 52 65 66 20 3d 20 73 sCheck.anRef = s
18e54 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20 28 73 qlite3Malloc( (s
18e55 43 68 65 63 6b 2e 6e 50 61 67 65 2b 31 29 2a 73 Check.nPage+1)*s
18e56 69 7a 65 6f 66 28 73 43 68 65 63 6b 2e 61 6e 52 izeof(sCheck.anR
18e57 65 66 5b 30 5d 29 20 29 3b 0a 20 20 69 66 28 20 ef[0]) );. if(
18e58 21 73 43 68 65 63 6b 2e 61 6e 52 65 66 20 29 7b !sCheck.anRef ){
18e59 0a 20 20 20 20 2a 70 6e 45 72 72 20 3d 20 31 3b . *pnErr = 1;
18e5a 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 . sqlite3Btre
18e5b 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 20 20 72 eLeave(p);. r
18e5c 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 66 eturn 0;. }. f
18e5d 6f 72 28 69 3d 30 3b 20 69 3c 3d 73 43 68 65 63 or(i=0; i<=sChec
18e5e 6b 2e 6e 50 61 67 65 3b 20 69 2b 2b 29 7b 20 73 k.nPage; i++){ s
18e5f 43 68 65 63 6b 2e 61 6e 52 65 66 5b 69 5d 20 3d Check.anRef[i] =
18e60 20 30 3b 20 7d 0a 20 20 69 20 3d 20 50 45 4e 44 0; }. i = PEND
18e61 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 ING_BYTE_PAGE(pB
18e62 74 29 3b 0a 20 20 69 66 28 20 69 3c 3d 73 43 68 t);. if( i<=sCh
18e63 65 63 6b 2e 6e 50 61 67 65 20 29 7b 0a 20 20 20 eck.nPage ){.
18e64 20 73 43 68 65 63 6b 2e 61 6e 52 65 66 5b 69 5d sCheck.anRef[i]
18e65 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 = 1;. }. sqli
18e66 74 65 33 53 74 72 41 63 63 75 6d 49 6e 69 74 28 te3StrAccumInit(
18e67 26 73 43 68 65 63 6b 2e 65 72 72 4d 73 67 2c 20 &sCheck.errMsg,
18e68 7a 45 72 72 2c 20 73 69 7a 65 6f 66 28 7a 45 72 zErr, sizeof(zEr
18e69 72 29 2c 20 32 30 30 30 30 29 3b 0a 0a 20 20 2f r), 20000);.. /
18e6a 2a 20 43 68 65 63 6b 20 74 68 65 20 69 6e 74 65 * Check the inte
18e6b 67 72 69 74 79 20 6f 66 20 74 68 65 20 66 72 65 grity of the fre
18e6c 65 6c 69 73 74 0a 20 20 2a 2f 0a 20 20 63 68 65 elist. */. che
18e6d 63 6b 4c 69 73 74 28 26 73 43 68 65 63 6b 2c 20 ckList(&sCheck,
18e6e 31 2c 20 67 65 74 34 62 79 74 65 28 26 70 42 74 1, get4byte(&pBt
18e6f 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b ->pPage1->aData[
18e70 33 32 5d 29 2c 0a 20 20 20 20 20 20 20 20 20 20 32]),.
18e71 20 20 67 65 74 34 62 79 74 65 28 26 70 42 74 2d get4byte(&pBt-
18e72 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 >pPage1->aData[3
18e73 36 5d 29 2c 20 22 4d 61 69 6e 20 66 72 65 65 6c 6]), "Main freel
18e74 69 73 74 3a 20 22 29 3b 0a 0a 20 20 2f 2a 20 43 ist: ");.. /* C
18e75 68 65 63 6b 20 61 6c 6c 20 74 68 65 20 74 61 62 heck all the tab
18e76 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 les.. */. for(
18e77 69 3d 30 3b 20 28 69 6e 74 29 69 3c 6e 52 6f 6f i=0; (int)i<nRoo
18e78 74 20 26 26 20 73 43 68 65 63 6b 2e 6d 78 45 72 t && sCheck.mxEr
18e79 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 r; i++){. if(
18e7a 20 61 52 6f 6f 74 5b 69 5d 3d 3d 30 20 29 20 63 aRoot[i]==0 ) c
18e7b 6f 6e 74 69 6e 75 65 3b 0a 23 69 66 6e 64 65 66 ontinue;.#ifndef
18e7c 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 SQLITE_OMIT_AUT
18e7d 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28 20 OVACUUM. if(
18e7e 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 pBt->autoVacuum
18e7f 26 26 20 61 52 6f 6f 74 5b 69 5d 3e 31 20 29 7b && aRoot[i]>1 ){
18e80 0a 20 20 20 20 20 20 63 68 65 63 6b 50 74 72 6d . checkPtrm
18e81 61 70 28 26 73 43 68 65 63 6b 2c 20 61 52 6f 6f ap(&sCheck, aRoo
18e82 74 5b 69 5d 2c 20 50 54 52 4d 41 50 5f 52 4f 4f t[i], PTRMAP_ROO
18e83 54 50 41 47 45 2c 20 30 2c 20 30 29 3b 0a 20 20 TPAGE, 0, 0);.
18e84 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 63 }.#endif. c
18e85 68 65 63 6b 54 72 65 65 50 61 67 65 28 26 73 43 heckTreePage(&sC
18e86 68 65 63 6b 2c 20 61 52 6f 6f 74 5b 69 5d 2c 20 heck, aRoot[i],
18e87 22 4c 69 73 74 20 6f 66 20 74 72 65 65 20 72 6f "List of tree ro
18e88 6f 74 73 3a 20 22 29 3b 0a 20 20 7d 0a 0a 20 20 ots: ");. }..
18e89 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 65 76 65 /* Make sure eve
18e8a 72 79 20 70 61 67 65 20 69 6e 20 74 68 65 20 66 ry page in the f
18e8b 69 6c 65 20 69 73 20 72 65 66 65 72 65 6e 63 65 ile is reference
18e8c 64 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 31 d. */. for(i=1
18e8d 3b 20 69 3c 3d 73 43 68 65 63 6b 2e 6e 50 61 67 ; i<=sCheck.nPag
18e8e 65 20 26 26 20 73 43 68 65 63 6b 2e 6d 78 45 72 e && sCheck.mxEr
18e8f 72 3b 20 69 2b 2b 29 7b 0a 23 69 66 64 65 66 20 r; i++){.#ifdef
18e90 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f SQLITE_OMIT_AUTO
18e91 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28 20 73 VACUUM. if( s
18e92 43 68 65 63 6b 2e 61 6e 52 65 66 5b 69 5d 3d 3d Check.anRef[i]==
18e93 30 20 29 7b 0a 20 20 20 20 20 20 63 68 65 63 6b 0 ){. check
18e94 41 70 70 65 6e 64 4d 73 67 28 26 73 43 68 65 63 AppendMsg(&sChec
18e95 6b 2c 20 30 2c 20 22 50 61 67 65 20 25 64 20 69 k, 0, "Page %d i
18e96 73 20 6e 65 76 65 72 20 75 73 65 64 22 2c 20 69 s never used", i
18e97 29 3b 0a 20 20 20 20 7d 0a 23 65 6c 73 65 0a 20 );. }.#else.
18e98 20 20 20 2f 2a 20 49 66 20 74 68 65 20 64 61 74 /* If the dat
18e99 61 62 61 73 65 20 73 75 70 70 6f 72 74 73 20 61 abase supports a
18e9a 75 74 6f 2d 76 61 63 75 75 6d 2c 20 6d 61 6b 65 uto-vacuum, make
18e9b 20 73 75 72 65 20 6e 6f 20 74 61 62 6c 65 73 20 sure no tables
18e9c 63 6f 6e 74 61 69 6e 0a 20 20 20 20 2a 2a 20 72 contain. ** r
18e9d 65 66 65 72 65 6e 63 65 73 20 74 6f 20 70 6f 69 eferences to poi
18e9e 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 73 2e 0a nter-map pages..
18e9f 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 73 */. if( s
18ea0 43 68 65 63 6b 2e 61 6e 52 65 66 5b 69 5d 3d 3d Check.anRef[i]==
18ea1 30 20 26 26 20 0a 20 20 20 20 20 20 20 28 50 54 0 && . (PT
18ea2 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70 42 74 2c RMAP_PAGENO(pBt,
18ea3 20 69 29 21 3d 69 20 7c 7c 20 21 70 42 74 2d 3e i)!=i || !pBt->
18ea4 61 75 74 6f 56 61 63 75 75 6d 29 20 29 7b 0a 20 autoVacuum) ){.
18ea5 20 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 checkAppend
18ea6 4d 73 67 28 26 73 43 68 65 63 6b 2c 20 30 2c 20 Msg(&sCheck, 0,
18ea7 22 50 61 67 65 20 25 64 20 69 73 20 6e 65 76 65 "Page %d is neve
18ea8 72 20 75 73 65 64 22 2c 20 69 29 3b 0a 20 20 20 r used", i);.
18ea9 20 7d 0a 20 20 20 20 69 66 28 20 73 43 68 65 63 }. if( sChec
18eaa 6b 2e 61 6e 52 65 66 5b 69 5d 21 3d 30 20 26 26 k.anRef[i]!=0 &&
18eab 20 0a 20 20 20 20 20 20 20 28 50 54 52 4d 41 50 . (PTRMAP
18eac 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20 69 29 3d _PAGENO(pBt, i)=
18ead 3d 69 20 26 26 20 70 42 74 2d 3e 61 75 74 6f 56 =i && pBt->autoV
18eae 61 63 75 75 6d 29 20 29 7b 0a 20 20 20 20 20 20 acuum) ){.
18eaf 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 26 checkAppendMsg(&
18eb0 73 43 68 65 63 6b 2c 20 30 2c 20 22 50 6f 69 6e sCheck, 0, "Poin
18eb1 74 65 72 20 6d 61 70 20 70 61 67 65 20 25 64 20 ter map page %d
18eb2 69 73 20 72 65 66 65 72 65 6e 63 65 64 22 2c 20 is referenced",
18eb3 69 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 i);. }.#endif
18eb4 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 . }.. /* Make
18eb5 73 75 72 65 20 74 68 69 73 20 61 6e 61 6c 79 73 sure this analys
18eb6 69 73 20 64 69 64 20 6e 6f 74 20 6c 65 61 76 65 is did not leave
18eb7 20 61 6e 79 20 75 6e 72 65 66 28 29 20 70 61 67 any unref() pag
18eb8 65 73 2e 0a 20 20 2a 2a 20 54 68 69 73 20 69 73 es.. ** This is
18eb9 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20 63 6f 6e an internal con
18eba 73 69 73 74 65 6e 63 79 20 63 68 65 63 6b 3b 20 sistency check;
18ebb 61 6e 20 69 6e 74 65 67 72 69 74 79 20 63 68 65 an integrity che
18ebc 63 6b 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 69 ck. ** of the i
18ebd 6e 74 65 67 72 69 74 79 20 63 68 65 63 6b 2e 0a ntegrity check..
18ebe 20 20 2a 2f 0a 20 20 69 66 28 20 4e 45 56 45 52 */. if( NEVER
18ebf 28 6e 52 65 66 20 21 3d 20 73 71 6c 69 74 65 33 (nRef != sqlite3
18ec0 50 61 67 65 72 52 65 66 63 6f 75 6e 74 28 70 42 PagerRefcount(pB
18ec1 74 2d 3e 70 50 61 67 65 72 29 29 20 29 7b 0a 20 t->pPager)) ){.
18ec2 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 checkAppendMs
18ec3 67 28 26 73 43 68 65 63 6b 2c 20 30 2c 20 0a 20 g(&sCheck, 0, .
18ec4 20 20 20 20 20 22 4f 75 74 73 74 61 6e 64 69 6e "Outstandin
18ec5 67 20 70 61 67 65 20 63 6f 75 6e 74 20 67 6f 65 g page count goe
18ec6 73 20 66 72 6f 6d 20 25 64 20 74 6f 20 25 64 20 s from %d to %d
18ec7 64 75 72 69 6e 67 20 74 68 69 73 20 61 6e 61 6c during this anal
18ec8 79 73 69 73 22 2c 0a 20 20 20 20 20 20 6e 52 65 ysis",. nRe
18ec9 66 2c 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 f, sqlite3PagerR
18eca 65 66 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61 efcount(pBt->pPa
18ecb 67 65 72 29 0a 20 20 20 20 29 3b 0a 20 20 7d 0a ger). );. }.
18ecc 0a 20 20 2f 2a 20 43 6c 65 61 6e 20 20 75 70 20 . /* Clean up
18ecd 61 6e 64 20 72 65 70 6f 72 74 20 65 72 72 6f 72 and report error
18ece 73 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 s.. */. sqlite
18ecf 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 3BtreeLeave(p);.
18ed0 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 73 sqlite3_free(s
18ed1 43 68 65 63 6b 2e 61 6e 52 65 66 29 3b 0a 20 20 Check.anRef);.
18ed2 69 66 28 20 73 43 68 65 63 6b 2e 6d 61 6c 6c 6f if( sCheck.mallo
18ed3 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 73 cFailed ){. s
18ed4 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 52 65 qlite3StrAccumRe
18ed5 73 65 74 28 26 73 43 68 65 63 6b 2e 65 72 72 4d set(&sCheck.errM
18ed6 73 67 29 3b 0a 20 20 20 20 2a 70 6e 45 72 72 20 sg);. *pnErr
18ed7 3d 20 73 43 68 65 63 6b 2e 6e 45 72 72 2b 31 3b = sCheck.nErr+1;
18ed8 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 . return 0;.
18ed9 20 7d 0a 20 20 2a 70 6e 45 72 72 20 3d 20 73 43 }. *pnErr = sC
18eda 68 65 63 6b 2e 6e 45 72 72 3b 0a 20 20 69 66 28 heck.nErr;. if(
18edb 20 73 43 68 65 63 6b 2e 6e 45 72 72 3d 3d 30 20 sCheck.nErr==0
18edc 29 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 ) sqlite3StrAccu
18edd 6d 52 65 73 65 74 28 26 73 43 68 65 63 6b 2e 65 mReset(&sCheck.e
18ede 72 72 4d 73 67 29 3b 0a 20 20 72 65 74 75 72 6e rrMsg);. return
18edf 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d sqlite3StrAccum
18ee0 46 69 6e 69 73 68 28 26 73 43 68 65 63 6b 2e 65 Finish(&sCheck.e
18ee1 72 72 4d 73 67 29 3b 0a 7d 0a 23 65 6e 64 69 66 rrMsg);.}.#endif
18ee2 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f /* SQLITE_OMIT_
18ee3 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b 20 INTEGRITY_CHECK
18ee4 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e */../*.** Return
18ee5 20 74 68 65 20 66 75 6c 6c 20 70 61 74 68 6e 61 the full pathna
18ee6 6d 65 20 6f 66 20 74 68 65 20 75 6e 64 65 72 6c me of the underl
18ee7 79 69 6e 67 20 64 61 74 61 62 61 73 65 20 66 69 ying database fi
18ee8 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 le..**.** The pa
18ee9 67 65 72 20 66 69 6c 65 6e 61 6d 65 20 69 73 20 ger filename is
18eea 69 6e 76 61 72 69 61 6e 74 20 61 73 20 6c 6f 6e invariant as lon
18eeb 67 20 61 73 20 74 68 65 20 70 61 67 65 72 20 69 g as the pager i
18eec 73 0a 2a 2a 20 6f 70 65 6e 20 73 6f 20 69 74 20 s.** open so it
18eed 69 73 20 73 61 66 65 20 74 6f 20 61 63 63 65 73 is safe to acces
18eee 73 20 77 69 74 68 6f 75 74 20 74 68 65 20 42 74 s without the Bt
18eef 53 68 61 72 65 64 20 6d 75 74 65 78 2e 0a 2a 2f Shared mutex..*/
18ef0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
18ef1 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 const char *sqli
18ef2 74 65 33 42 74 72 65 65 47 65 74 46 69 6c 65 6e te3BtreeGetFilen
18ef3 61 6d 65 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 ame(Btree *p){.
18ef4 20 61 73 73 65 72 74 28 20 70 2d 3e 70 42 74 2d assert( p->pBt-
18ef5 3e 70 50 61 67 65 72 21 3d 30 20 29 3b 0a 20 20 >pPager!=0 );.
18ef6 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 50 61 return sqlite3Pa
18ef7 67 65 72 46 69 6c 65 6e 61 6d 65 28 70 2d 3e 70 gerFilename(p->p
18ef8 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 7d 0a 0a Bt->pPager);.}..
18ef9 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 /*.** Return the
18efa 20 70 61 74 68 6e 61 6d 65 20 6f 66 20 74 68 65 pathname of the
18efb 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f journal file fo
18efc 72 20 74 68 69 73 20 64 61 74 61 62 61 73 65 2e r this database.
18efd 20 54 68 65 20 72 65 74 75 72 6e 0a 2a 2a 20 76 The return.** v
18efe 61 6c 75 65 20 6f 66 20 74 68 69 73 20 72 6f 75 alue of this rou
18eff 74 69 6e 65 20 69 73 20 74 68 65 20 73 61 6d 65 tine is the same
18f00 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 77 regardless of w
18f01 68 65 74 68 65 72 20 74 68 65 20 6a 6f 75 72 6e hether the journ
18f02 61 6c 20 66 69 6c 65 0a 2a 2a 20 68 61 73 20 62 al file.** has b
18f03 65 65 6e 20 63 72 65 61 74 65 64 20 6f 72 20 6e een created or n
18f04 6f 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 ot..**.** The pa
18f05 67 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ger journal file
18f06 6e 61 6d 65 20 69 73 20 69 6e 76 61 72 69 61 6e name is invarian
18f07 74 20 61 73 20 6c 6f 6e 67 20 61 73 20 74 68 65 t as long as the
18f08 20 70 61 67 65 72 20 69 73 0a 2a 2a 20 6f 70 65 pager is.** ope
18f09 6e 20 73 6f 20 69 74 20 69 73 20 73 61 66 65 20 n so it is safe
18f0a 74 6f 20 61 63 63 65 73 73 20 77 69 74 68 6f 75 to access withou
18f0b 74 20 74 68 65 20 42 74 53 68 61 72 65 64 20 6d t the BtShared m
18f0c 75 74 65 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f utex..*/.SQLITE_
18f0d 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 PRIVATE const ch
18f0e 61 72 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 ar *sqlite3Btree
18f0f 47 65 74 4a 6f 75 72 6e 61 6c 6e 61 6d 65 28 42 GetJournalname(B
18f10 74 72 65 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 tree *p){. asse
18f11 72 74 28 20 70 2d 3e 70 42 74 2d 3e 70 50 61 67 rt( p->pBt->pPag
18f12 65 72 21 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 er!=0 );. retur
18f13 6e 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f n sqlite3PagerJo
18f14 75 72 6e 61 6c 6e 61 6d 65 28 70 2d 3e 70 42 74 urnalname(p->pBt
18f15 2d 3e 70 50 61 67 65 72 29 3b 0a 7d 0a 0a 2f 2a ->pPager);.}../*
18f16 0a 2a 2a 20 52 65 74 75 72 6e 20 6e 6f 6e 2d 7a .** Return non-z
18f17 65 72 6f 20 69 66 20 61 20 74 72 61 6e 73 61 63 ero if a transac
18f18 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65 2e 0a tion is active..
18f19 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
18f1a 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
18f1b 65 65 49 73 49 6e 54 72 61 6e 73 28 42 74 72 65 eeIsInTrans(Btre
18f1c 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 e *p){. assert(
18f1d 20 70 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 p==0 || sqlite3
18f1e 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 _mutex_held(p->d
18f1f 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 b->mutex) );. r
18f20 65 74 75 72 6e 20 28 70 20 26 26 20 28 70 2d 3e eturn (p && (p->
18f21 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 inTrans==TRANS_W
18f22 52 49 54 45 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a RITE));.}../*.**
18f23 20 52 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f Return non-zero
18f24 20 69 66 20 61 20 72 65 61 64 20 28 6f 72 20 77 if a read (or w
18f25 72 69 74 65 29 20 74 72 61 6e 73 61 63 74 69 6f rite) transactio
18f26 6e 20 69 73 20 61 63 74 69 76 65 2e 0a 2a 2f 0a n is active..*/.
18f27 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
18f28 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 nt sqlite3BtreeI
18f29 73 49 6e 52 65 61 64 54 72 61 6e 73 28 42 74 72 sInReadTrans(Btr
18f2a 65 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 ee *p){. assert
18f2b 28 20 70 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ( p );. assert(
18f2c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
18f2d 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 eld(p->db->mutex
18f2e 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 2d ) );. return p-
18f2f 3e 69 6e 54 72 61 6e 73 21 3d 54 52 41 4e 53 5f >inTrans!=TRANS_
18f30 4e 4f 4e 45 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f NONE;.}..SQLITE_
18f31 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
18f32 74 65 33 42 74 72 65 65 49 73 49 6e 42 61 63 6b te3BtreeIsInBack
18f33 75 70 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 up(Btree *p){.
18f34 61 73 73 65 72 74 28 20 70 20 29 3b 0a 20 20 61 assert( p );. a
18f35 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
18f36 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d utex_held(p->db-
18f37 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 65 74 >mutex) );. ret
18f38 75 72 6e 20 70 2d 3e 6e 42 61 63 6b 75 70 21 3d urn p->nBackup!=
18f39 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 0;.}../*.** This
18f3a 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e function return
18f3b 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 s a pointer to a
18f3c 20 62 6c 6f 62 20 6f 66 20 6d 65 6d 6f 72 79 20 blob of memory
18f3d 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 0a associated with.
18f3e 2a 2a 20 61 20 73 69 6e 67 6c 65 20 73 68 61 72 ** a single shar
18f3f 65 64 2d 62 74 72 65 65 2e 20 54 68 65 20 6d 65 ed-btree. The me
18f40 6d 6f 72 79 20 69 73 20 75 73 65 64 20 62 79 20 mory is used by
18f41 63 6c 69 65 6e 74 20 63 6f 64 65 20 66 6f 72 20 client code for
18f42 69 74 73 20 6f 77 6e 0a 2a 2a 20 70 75 72 70 6f its own.** purpo
18f43 73 65 73 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 ses (for example
18f44 2c 20 74 6f 20 73 74 6f 72 65 20 61 20 68 69 67 , to store a hig
18f45 68 2d 6c 65 76 65 6c 20 73 63 68 65 6d 61 20 61 h-level schema a
18f46 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 0a ssociated with .
18f47 2a 2a 20 74 68 65 20 73 68 61 72 65 64 2d 62 74 ** the shared-bt
18f48 72 65 65 29 2e 20 54 68 65 20 62 74 72 65 65 20 ree). The btree
18f49 6c 61 79 65 72 20 6d 61 6e 61 67 65 73 20 72 65 layer manages re
18f4a 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 69 6e 67 ference counting
18f4b 20 69 73 73 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 issues..**.** T
18f4c 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 74 68 he first time th
18f4d 69 73 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 20 is is called on
18f4e 61 20 73 68 61 72 65 64 2d 62 74 72 65 65 2c 20 a shared-btree,
18f4f 6e 42 79 74 65 73 20 62 79 74 65 73 20 6f 66 20 nBytes bytes of
18f50 6d 65 6d 6f 72 79 0a 2a 2a 20 61 72 65 20 61 6c memory.** are al
18f51 6c 6f 63 61 74 65 64 2c 20 7a 65 72 6f 65 64 2c located, zeroed,
18f52 20 61 6e 64 20 72 65 74 75 72 6e 65 64 20 74 6f and returned to
18f53 20 74 68 65 20 63 61 6c 6c 65 72 2e 20 46 6f 72 the caller. For
18f54 20 65 61 63 68 20 73 75 62 73 65 71 75 65 6e 74 each subsequent
18f55 20 0a 2a 2a 20 63 61 6c 6c 20 74 68 65 20 6e 42 .** call the nB
18f56 79 74 65 73 20 70 61 72 61 6d 65 74 65 72 20 69 ytes parameter i
18f57 73 20 69 67 6e 6f 72 65 64 20 61 6e 64 20 61 20 s ignored and a
18f58 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 73 pointer to the s
18f59 61 6d 65 20 62 6c 6f 62 0a 2a 2a 20 6f 66 20 6d ame blob.** of m
18f5a 65 6d 6f 72 79 20 72 65 74 75 72 6e 65 64 2e 20 emory returned.
18f5b 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e 42 .**.** If the nB
18f5c 79 74 65 73 20 70 61 72 61 6d 65 74 65 72 20 69 ytes parameter i
18f5d 73 20 30 20 61 6e 64 20 74 68 65 20 62 6c 6f 62 s 0 and the blob
18f5e 20 6f 66 20 6d 65 6d 6f 72 79 20 68 61 73 20 6e of memory has n
18f5f 6f 74 20 79 65 74 20 62 65 65 6e 0a 2a 2a 20 61 ot yet been.** a
18f60 6c 6c 6f 63 61 74 65 64 2c 20 61 20 6e 75 6c 6c llocated, a null
18f61 20 70 6f 69 6e 74 65 72 20 69 73 20 72 65 74 75 pointer is retu
18f62 72 6e 65 64 2e 20 49 66 20 74 68 65 20 62 6c 6f rned. If the blo
18f63 62 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 b has already be
18f64 65 6e 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 2c en.** allocated,
18f65 20 69 74 20 69 73 20 72 65 74 75 72 6e 65 64 20 it is returned
18f66 61 73 20 6e 6f 72 6d 61 6c 2e 0a 2a 2a 0a 2a 2a as normal..**.**
18f67 20 4a 75 73 74 20 62 65 66 6f 72 65 20 74 68 65 Just before the
18f68 20 73 68 61 72 65 64 2d 62 74 72 65 65 20 69 73 shared-btree is
18f69 20 63 6c 6f 73 65 64 2c 20 74 68 65 20 66 75 6e closed, the fun
18f6a 63 74 69 6f 6e 20 70 61 73 73 65 64 20 61 73 20 ction passed as
18f6b 74 68 65 20 0a 2a 2a 20 78 46 72 65 65 20 61 72 the .** xFree ar
18f6c 67 75 6d 65 6e 74 20 77 68 65 6e 20 74 68 65 20 gument when the
18f6d 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
18f6e 6e 20 77 61 73 20 6d 61 64 65 20 69 73 20 69 6e n was made is in
18f6f 76 6f 6b 65 64 20 6f 6e 20 74 68 65 20 0a 2a 2a voked on the .**
18f70 20 62 6c 6f 62 20 6f 66 20 61 6c 6c 6f 63 61 74 blob of allocat
18f71 65 64 20 6d 65 6d 6f 72 79 2e 20 54 68 69 73 20 ed memory. This
18f72 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 function should
18f73 6e 6f 74 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 not call sqlite3
18f74 5f 66 72 65 65 28 29 0a 2a 2a 20 6f 6e 20 74 68 _free().** on th
18f75 65 20 6d 65 6d 6f 72 79 2c 20 74 68 65 20 62 74 e memory, the bt
18f76 72 65 65 20 6c 61 79 65 72 20 64 6f 65 73 20 74 ree layer does t
18f77 68 61 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 hat..*/.SQLITE_P
18f78 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c RIVATE void *sql
18f79 69 74 65 33 42 74 72 65 65 53 63 68 65 6d 61 28 ite3BtreeSchema(
18f7a 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 6e 42 Btree *p, int nB
18f7b 79 74 65 73 2c 20 76 6f 69 64 28 2a 78 46 72 65 ytes, void(*xFre
18f7c 65 29 28 76 6f 69 64 20 2a 29 29 7b 0a 20 20 42 e)(void *)){. B
18f7d 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 tShared *pBt = p
18f7e 2d 3e 70 42 74 3b 0a 20 20 73 71 6c 69 74 65 33 ->pBt;. sqlite3
18f7f 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 BtreeEnter(p);.
18f80 20 69 66 28 20 21 70 42 74 2d 3e 70 53 63 68 65 if( !pBt->pSche
18f81 6d 61 20 26 26 20 6e 42 79 74 65 73 20 29 7b 0a ma && nBytes ){.
18f82 20 20 20 20 70 42 74 2d 3e 70 53 63 68 65 6d 61 pBt->pSchema
18f83 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 = sqlite3Malloc
18f84 5a 65 72 6f 28 6e 42 79 74 65 73 29 3b 0a 20 20 Zero(nBytes);.
18f85 20 20 70 42 74 2d 3e 78 46 72 65 65 53 63 68 65 pBt->xFreeSche
18f86 6d 61 20 3d 20 78 46 72 65 65 3b 0a 20 20 7d 0a ma = xFree;. }.
18f87 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 sqlite3BtreeLe
18f88 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e ave(p);. return
18f89 20 70 42 74 2d 3e 70 53 63 68 65 6d 61 3b 0a 7d pBt->pSchema;.}
18f8a 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 ../*.** Return S
18f8b 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 QLITE_LOCKED_SHA
18f8c 52 45 44 43 41 43 48 45 20 69 66 20 61 6e 6f 74 REDCACHE if anot
18f8d 68 65 72 20 75 73 65 72 20 6f 66 20 74 68 65 20 her user of the
18f8e 73 61 6d 65 20 73 68 61 72 65 64 20 0a 2a 2a 20 same shared .**
18f8f 62 74 72 65 65 20 61 73 20 74 68 65 20 61 72 67 btree as the arg
18f90 75 6d 65 6e 74 20 68 61 6e 64 6c 65 20 68 6f 6c ument handle hol
18f91 64 73 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 ds an exclusive
18f92 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 0a 2a 2a 20 lock on the .**
18f93 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 sqlite_master ta
18f94 62 6c 65 2e 20 4f 74 68 65 72 77 69 73 65 20 53 ble. Otherwise S
18f95 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c QLITE_OK..*/.SQL
18f96 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
18f97 73 71 6c 69 74 65 33 42 74 72 65 65 53 63 68 65 sqlite3BtreeSche
18f98 6d 61 4c 6f 63 6b 65 64 28 42 74 72 65 65 20 2a maLocked(Btree *
18f99 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 p){. int rc;.
18f9a 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
18f9b 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 mutex_held(p->db
18f9c 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71 ->mutex) );. sq
18f9d 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 lite3BtreeEnter(
18f9e 70 29 3b 0a 20 20 72 63 20 3d 20 71 75 65 72 79 p);. rc = query
18f9f 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 SharedCacheTable
18fa0 4c 6f 63 6b 28 70 2c 20 4d 41 53 54 45 52 5f 52 Lock(p, MASTER_R
18fa1 4f 4f 54 2c 20 52 45 41 44 5f 4c 4f 43 4b 29 3b OOT, READ_LOCK);
18fa2 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 . assert( rc==S
18fa3 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 72 63 3d 3d QLITE_OK || rc==
18fa4 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 SQLITE_LOCKED_SH
18fa5 41 52 45 44 43 41 43 48 45 20 29 3b 0a 20 20 73 AREDCACHE );. s
18fa6 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 qlite3BtreeLeave
18fa7 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 (p);. return rc
18fa8 3b 0a 7d 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 ;.}...#ifndef SQ
18fa9 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 LITE_OMIT_SHARED
18faa 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a 20 4f 62 74 _CACHE./*.** Obt
18fab 61 69 6e 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 ain a lock on th
18fac 65 20 74 61 62 6c 65 20 77 68 6f 73 65 20 72 6f e table whose ro
18fad 6f 74 20 70 61 67 65 20 69 73 20 69 54 61 62 2e ot page is iTab.
18fae 20 20 54 68 65 0a 2a 2a 20 6c 6f 63 6b 20 69 73 The.** lock is
18faf 20 61 20 77 72 69 74 65 20 6c 6f 63 6b 20 69 66 a write lock if
18fb0 20 69 73 57 72 69 74 65 6c 6f 63 6b 20 69 73 20 isWritelock is
18fb1 74 72 75 65 20 6f 72 20 61 20 72 65 61 64 20 6c true or a read l
18fb2 6f 63 6b 0a 2a 2a 20 69 66 20 69 74 20 69 73 20 ock.** if it is
18fb3 66 61 6c 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 false..*/.SQLITE
18fb4 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
18fb5 69 74 65 33 42 74 72 65 65 4c 6f 63 6b 54 61 62 ite3BtreeLockTab
18fb6 6c 65 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 le(Btree *p, int
18fb7 20 69 54 61 62 2c 20 75 38 20 69 73 57 72 69 74 iTab, u8 isWrit
18fb8 65 4c 6f 63 6b 29 7b 0a 20 20 69 6e 74 20 72 63 eLock){. int rc
18fb9 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
18fba 61 73 73 65 72 74 28 20 70 2d 3e 69 6e 54 72 61 assert( p->inTra
18fbb 6e 73 21 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 ns!=TRANS_NONE )
18fbc 3b 0a 20 20 69 66 28 20 70 2d 3e 73 68 61 72 61 ;. if( p->shara
18fbd 62 6c 65 20 29 7b 0a 20 20 20 20 75 38 20 6c 6f ble ){. u8 lo
18fbe 63 6b 54 79 70 65 20 3d 20 52 45 41 44 5f 4c 4f ckType = READ_LO
18fbf 43 4b 20 2b 20 69 73 57 72 69 74 65 4c 6f 63 6b CK + isWriteLock
18fc0 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 52 45 ;. assert( RE
18fc1 41 44 5f 4c 4f 43 4b 2b 31 3d 3d 57 52 49 54 45 AD_LOCK+1==WRITE
18fc2 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 61 73 73 _LOCK );. ass
18fc3 65 72 74 28 20 69 73 57 72 69 74 65 4c 6f 63 6b ert( isWriteLock
18fc4 3d 3d 30 20 7c 7c 20 69 73 57 72 69 74 65 4c 6f ==0 || isWriteLo
18fc5 63 6b 3d 3d 31 20 29 3b 0a 0a 20 20 20 20 73 71 ck==1 );.. sq
18fc6 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 lite3BtreeEnter(
18fc7 70 29 3b 0a 20 20 20 20 72 63 20 3d 20 71 75 65 p);. rc = que
18fc8 72 79 53 68 61 72 65 64 43 61 63 68 65 54 61 62 rySharedCacheTab
18fc9 6c 65 4c 6f 63 6b 28 70 2c 20 69 54 61 62 2c 20 leLock(p, iTab,
18fca 6c 6f 63 6b 54 79 70 65 29 3b 0a 20 20 20 20 69 lockType);. i
18fcb 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
18fcc 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 ){. rc = s
18fcd 65 74 53 68 61 72 65 64 43 61 63 68 65 54 61 62 etSharedCacheTab
18fce 6c 65 4c 6f 63 6b 28 70 2c 20 69 54 61 62 2c 20 leLock(p, iTab,
18fcf 6c 6f 63 6b 54 79 70 65 29 3b 0a 20 20 20 20 7d lockType);. }
18fd0 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 . sqlite3Btre
18fd1 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 7d 0a 20 eLeave(p);. }.
18fd2 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 return rc;.}.#e
18fd3 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 ndif..#ifndef SQ
18fd4 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c LITE_OMIT_INCRBL
18fd5 4f 42 0a 2f 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e OB./*.** Argumen
18fd6 74 20 70 43 73 72 20 6d 75 73 74 20 62 65 20 61 t pCsr must be a
18fd7 20 63 75 72 73 6f 72 20 6f 70 65 6e 65 64 20 66 cursor opened f
18fd8 6f 72 20 77 72 69 74 69 6e 67 20 6f 6e 20 61 6e or writing on an
18fd9 20 0a 2a 2a 20 49 4e 54 4b 45 59 20 74 61 62 6c .** INTKEY tabl
18fda 65 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e e currently poin
18fdb 74 69 6e 67 20 61 74 20 61 20 76 61 6c 69 64 20 ting at a valid
18fdc 74 61 62 6c 65 20 65 6e 74 72 79 2e 20 0a 2a 2a table entry. .**
18fdd 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d This function m
18fde 6f 64 69 66 69 65 73 20 74 68 65 20 64 61 74 61 odifies the data
18fdf 20 73 74 6f 72 65 64 20 61 73 20 70 61 72 74 20 stored as part
18fe0 6f 66 20 74 68 61 74 20 65 6e 74 72 79 2e 0a 2a of that entry..*
18fe1 2a 0a 2a 2a 20 4f 6e 6c 79 20 74 68 65 20 64 61 *.** Only the da
18fe2 74 61 20 63 6f 6e 74 65 6e 74 20 6d 61 79 20 6f ta content may o
18fe3 6e 6c 79 20 62 65 20 6d 6f 64 69 66 69 65 64 2c nly be modified,
18fe4 20 69 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 it is not possi
18fe5 62 6c 65 20 74 6f 20 0a 2a 2a 20 63 68 61 6e 67 ble to .** chang
18fe6 65 20 74 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 e the length of
18fe7 74 68 65 20 64 61 74 61 20 73 74 6f 72 65 64 2e the data stored.
18fe8 20 49 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f If this functio
18fe9 6e 20 69 73 20 63 61 6c 6c 65 64 20 77 69 74 68 n is called with
18fea 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 73 20 74 .** parameters t
18feb 68 61 74 20 61 74 74 65 6d 70 74 20 74 6f 20 77 hat attempt to w
18fec 72 69 74 65 20 70 61 73 74 20 74 68 65 20 65 6e rite past the en
18fed 64 20 6f 66 20 74 68 65 20 65 78 69 73 74 69 6e d of the existin
18fee 67 20 64 61 74 61 2c 0a 2a 2a 20 6e 6f 20 6d 6f g data,.** no mo
18fef 64 69 66 69 63 61 74 69 6f 6e 73 20 61 72 65 20 difications are
18ff0 6d 61 64 65 20 61 6e 64 20 53 51 4c 49 54 45 5f made and SQLITE_
18ff1 43 4f 52 52 55 50 54 20 69 73 20 72 65 74 75 72 CORRUPT is retur
18ff2 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ned..*/.SQLITE_P
18ff3 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
18ff4 65 33 42 74 72 65 65 50 75 74 44 61 74 61 28 42 e3BtreePutData(B
18ff5 74 43 75 72 73 6f 72 20 2a 70 43 73 72 2c 20 75 tCursor *pCsr, u
18ff6 33 32 20 6f 66 66 73 65 74 2c 20 75 33 32 20 61 32 offset, u32 a
18ff7 6d 74 2c 20 76 6f 69 64 20 2a 7a 29 7b 0a 20 20 mt, void *z){.
18ff8 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74 int rc;. assert
18ff9 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 ( cursorHoldsMut
18ffa 65 78 28 70 43 73 72 29 20 29 3b 0a 20 20 61 73 ex(pCsr) );. as
18ffb 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
18ffc 74 65 78 5f 68 65 6c 64 28 70 43 73 72 2d 3e 70 tex_held(pCsr->p
18ffd 42 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 Btree->db->mutex
18ffe 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 ) );. assert( p
18fff 43 73 72 2d 3e 69 73 49 6e 63 72 62 6c 6f 62 48 Csr->isIncrblobH
19000 61 6e 64 6c 65 20 29 3b 0a 0a 20 20 72 63 20 3d andle );.. rc =
19001 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f restoreCursorPo
19002 73 69 74 69 6f 6e 28 70 43 73 72 29 3b 0a 20 20 sition(pCsr);.
19003 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
19004 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 K ){. return
19005 72 63 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 rc;. }. assert
19006 28 20 70 43 73 72 2d 3e 65 53 74 61 74 65 21 3d ( pCsr->eState!=
19007 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 CURSOR_REQUIRESE
19008 45 4b 20 29 3b 0a 20 20 69 66 28 20 70 43 73 72 EK );. if( pCsr
19009 2d 3e 65 53 74 61 74 65 21 3d 43 55 52 53 4f 52 ->eState!=CURSOR
1900a 5f 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 72 65 _VALID ){. re
1900b 74 75 72 6e 20 53 51 4c 49 54 45 5f 41 42 4f 52 turn SQLITE_ABOR
1900c 54 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 T;. }.. /* Che
1900d 63 6b 20 73 6f 6d 65 20 61 73 73 75 6d 70 74 69 ck some assumpti
1900e 6f 6e 73 3a 20 0a 20 20 2a 2a 20 20 20 28 61 29 ons: . ** (a)
1900f 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 6f the cursor is o
19010 70 65 6e 20 66 6f 72 20 77 72 69 74 69 6e 67 2c pen for writing,
19011 0a 20 20 2a 2a 20 20 20 28 62 29 20 74 68 65 72 . ** (b) ther
19012 65 20 69 73 20 61 20 72 65 61 64 2f 77 72 69 74 e is a read/writ
19013 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70 e transaction op
19014 65 6e 2c 0a 20 20 2a 2a 20 20 20 28 63 29 20 74 en,. ** (c) t
19015 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 6f he connection ho
19016 6c 64 73 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b lds a write-lock
19017 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 28 69 on the table (i
19018 66 20 72 65 71 75 69 72 65 64 29 2c 0a 20 20 2a f required),. *
19019 2a 20 20 20 28 64 29 20 74 68 65 72 65 20 61 72 * (d) there ar
1901a 65 20 6e 6f 20 63 6f 6e 66 6c 69 63 74 69 6e 67 e no conflicting
1901b 20 72 65 61 64 2d 6c 6f 63 6b 73 2c 20 61 6e 64 read-locks, and
1901c 0a 20 20 2a 2a 20 20 20 28 65 29 20 74 68 65 20 . ** (e) the
1901d 63 75 72 73 6f 72 20 70 6f 69 6e 74 73 20 61 74 cursor points at
1901e 20 61 20 76 61 6c 69 64 20 72 6f 77 20 6f 66 20 a valid row of
1901f 61 6e 20 69 6e 74 4b 65 79 20 74 61 62 6c 65 2e an intKey table.
19020 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 43 73 . */. if( !pCs
19021 72 2d 3e 77 72 46 6c 61 67 20 29 7b 0a 20 20 20 r->wrFlag ){.
19022 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 52 return SQLITE_R
19023 45 41 44 4f 4e 4c 59 3b 0a 20 20 7d 0a 20 20 61 EADONLY;. }. a
19024 73 73 65 72 74 28 20 21 70 43 73 72 2d 3e 70 42 ssert( !pCsr->pB
19025 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 26 26 20 70 t->readOnly && p
19026 43 73 72 2d 3e 70 42 74 2d 3e 69 6e 54 72 61 6e Csr->pBt->inTran
19027 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 saction==TRANS_W
19028 52 49 54 45 20 29 3b 0a 20 20 61 73 73 65 72 74 RITE );. assert
19029 28 20 68 61 73 53 68 61 72 65 64 43 61 63 68 65 ( hasSharedCache
1902a 54 61 62 6c 65 4c 6f 63 6b 28 70 43 73 72 2d 3e TableLock(pCsr->
1902b 70 42 74 72 65 65 2c 20 70 43 73 72 2d 3e 70 67 pBtree, pCsr->pg
1902c 6e 6f 52 6f 6f 74 2c 20 30 2c 20 32 29 20 29 3b noRoot, 0, 2) );
1902d 0a 20 20 61 73 73 65 72 74 28 20 21 68 61 73 52 . assert( !hasR
1902e 65 61 64 43 6f 6e 66 6c 69 63 74 73 28 70 43 73 eadConflicts(pCs
1902f 72 2d 3e 70 42 74 72 65 65 2c 20 70 43 73 72 2d r->pBtree, pCsr-
19030 3e 70 67 6e 6f 52 6f 6f 74 29 20 29 3b 0a 20 20 >pgnoRoot) );.
19031 61 73 73 65 72 74 28 20 70 43 73 72 2d 3e 61 70 assert( pCsr->ap
19032 50 61 67 65 5b 70 43 73 72 2d 3e 69 50 61 67 65 Page[pCsr->iPage
19033 5d 2d 3e 69 6e 74 4b 65 79 20 29 3b 0a 0a 20 20 ]->intKey );..
19034 72 65 74 75 72 6e 20 61 63 63 65 73 73 50 61 79 return accessPay
19035 6c 6f 61 64 28 70 43 73 72 2c 20 6f 66 66 73 65 load(pCsr, offse
19036 74 2c 20 61 6d 74 2c 20 28 75 6e 73 69 67 6e 65 t, amt, (unsigne
19037 64 20 63 68 61 72 20 2a 29 7a 2c 20 31 29 3b 0a d char *)z, 1);.
19038 7d 0a 0a 2f 2a 20 0a 2a 2a 20 53 65 74 20 61 20 }../* .** Set a
19039 66 6c 61 67 20 6f 6e 20 74 68 69 73 20 63 75 72 flag on this cur
1903a 73 6f 72 20 74 6f 20 63 61 63 68 65 20 74 68 65 sor to cache the
1903b 20 6c 6f 63 61 74 69 6f 6e 73 20 6f 66 20 70 61 locations of pa
1903c 67 65 73 20 66 72 6f 6d 20 74 68 65 20 0a 2a 2a ges from the .**
1903d 20 6f 76 65 72 66 6c 6f 77 20 6c 69 73 74 20 66 overflow list f
1903e 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 72 or the current r
1903f 6f 77 2e 20 54 68 69 73 20 69 73 20 75 73 65 64 ow. This is used
19040 20 62 79 20 63 75 72 73 6f 72 73 20 6f 70 65 6e by cursors open
19041 65 64 0a 2a 2a 20 66 6f 72 20 69 6e 63 72 65 6d ed.** for increm
19042 65 6e 74 61 6c 20 62 6c 6f 62 20 49 4f 20 6f 6e ental blob IO on
19043 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 ly..**.** This f
19044 75 6e 63 74 69 6f 6e 20 73 65 74 73 20 61 20 66 unction sets a f
19045 6c 61 67 20 6f 6e 6c 79 2e 20 54 68 65 20 61 63 lag only. The ac
19046 74 75 61 6c 20 70 61 67 65 20 6c 6f 63 61 74 69 tual page locati
19047 6f 6e 20 63 61 63 68 65 0a 2a 2a 20 28 73 74 6f on cache.** (sto
19048 72 65 64 20 69 6e 20 42 74 43 75 72 73 6f 72 2e red in BtCursor.
19049 61 4f 76 65 72 66 6c 6f 77 5b 5d 29 20 69 73 20 aOverflow[]) is
1904a 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 75 73 allocated and us
1904b 65 64 20 62 79 20 66 75 6e 63 74 69 6f 6e 0a 2a ed by function.*
1904c 2a 20 61 63 63 65 73 73 50 61 79 6c 6f 61 64 28 * accessPayload(
1904d 29 20 28 74 68 65 20 77 6f 72 6b 65 72 20 66 75 ) (the worker fu
1904e 6e 63 74 69 6f 6e 20 66 6f 72 20 73 71 6c 69 74 nction for sqlit
1904f 65 33 42 74 72 65 65 44 61 74 61 28 29 20 61 6e e3BtreeData() an
19050 64 0a 2a 2a 20 73 71 6c 69 74 65 33 42 74 72 65 d.** sqlite3Btre
19051 65 50 75 74 44 61 74 61 28 29 29 2e 0a 2a 2f 0a ePutData())..*/.
19052 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
19053 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 oid sqlite3Btree
19054 43 61 63 68 65 4f 76 65 72 66 6c 6f 77 28 42 74 CacheOverflow(Bt
19055 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 Cursor *pCur){.
19056 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 assert( cursorH
19057 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 oldsMutex(pCur)
19058 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c );. assert( sql
19059 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
1905a 70 43 75 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 pCur->pBtree->db
1905b 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 ->mutex) );. as
1905c 73 65 72 74 28 21 70 43 75 72 2d 3e 69 73 49 6e sert(!pCur->isIn
1905d 63 72 62 6c 6f 62 48 61 6e 64 6c 65 29 3b 0a 20 crblobHandle);.
1905e 20 61 73 73 65 72 74 28 21 70 43 75 72 2d 3e 61 assert(!pCur->a
1905f 4f 76 65 72 66 6c 6f 77 29 3b 0a 20 20 70 43 75 Overflow);. pCu
19060 72 2d 3e 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e r->isIncrblobHan
19061 64 6c 65 20 3d 20 31 3b 0a 7d 0a 23 65 6e 64 69 dle = 1;.}.#endi
19062 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a f../************
19063 2a 2a 20 45 6e 64 20 6f 66 20 62 74 72 65 65 2e ** End of btree.
19064 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
19065 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19066 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19067 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
19068 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 62 61 ** Begin file ba
19069 63 6b 75 70 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a ckup.c *********
1906a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1906b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1906c 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 39 20 4a 61 */./*.** 2009 Ja
1906d 6e 75 61 72 79 20 32 38 0a 2a 2a 0a 2a 2a 20 54 nuary 28.**.** T
1906e 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 he author discla
1906f 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f ims copyright to
19070 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 this source cod
19071 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a e. In place of.
19072 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 ** a legal notic
19073 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 e, here is a ble
19074 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 ssing:.**.**
19075 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 May you do good
19076 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a and not evil..**
19077 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 May you find
19078 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 forgiveness for
19079 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f yourself and fo
1907a 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a rgive others..**
1907b 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 May you shar
1907c 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 e freely, never
1907d 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e taking more than
1907e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a you give..**.**
1907f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19081 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19082 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19083 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 *******.** This
19084 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 file contains th
19085 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e e implementation
19086 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f of the sqlite3_
19087 62 61 63 6b 75 70 5f 58 58 58 28 29 20 0a 2a 2a backup_XXX() .**
19088 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 73 20 61 API functions a
19089 6e 64 20 74 68 65 20 72 65 6c 61 74 65 64 20 66 nd the related f
1908a 65 61 74 75 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 24 eatures..**.** $
1908b 49 64 3a 20 62 61 63 6b 75 70 2e 63 2c 76 20 31 Id: backup.c,v 1
1908c 2e 31 39 20 32 30 30 39 2f 30 37 2f 30 36 20 31 .19 2009/07/06 1
1908d 39 3a 30 33 3a 31 33 20 64 72 68 20 45 78 70 20 9:03:13 drh Exp
1908e 24 0a 2a 2f 0a 0a 2f 2a 20 4d 61 63 72 6f 20 74 $.*/../* Macro t
1908f 6f 20 66 69 6e 64 20 74 68 65 20 6d 69 6e 69 6d o find the minim
19090 75 6d 20 6f 66 20 74 77 6f 20 6e 75 6d 65 72 69 um of two numeri
19091 63 20 76 61 6c 75 65 73 2e 0a 2a 2f 0a 23 69 66 c values..*/.#if
19092 6e 64 65 66 20 4d 49 4e 0a 23 20 64 65 66 69 6e ndef MIN.# defin
19093 65 20 4d 49 4e 28 78 2c 79 29 20 28 28 78 29 3c e MIN(x,y) ((x)<
19094 28 79 29 3f 28 78 29 3a 28 79 29 29 0a 23 65 6e (y)?(x):(y)).#en
19095 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 53 74 72 75 63 dif../*.** Struc
19096 74 75 72 65 20 61 6c 6c 6f 63 61 74 65 64 20 66 ture allocated f
19097 6f 72 20 65 61 63 68 20 62 61 63 6b 75 70 20 6f or each backup o
19098 70 65 72 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 peration..*/.str
19099 75 63 74 20 73 71 6c 69 74 65 33 5f 62 61 63 6b uct sqlite3_back
1909a 75 70 20 7b 0a 20 20 73 71 6c 69 74 65 33 2a 20 up {. sqlite3*
1909b 70 44 65 73 74 44 62 3b 20 20 20 20 20 20 20 20 pDestDb;
1909c 2f 2a 20 44 65 73 74 69 6e 61 74 69 6f 6e 20 64 /* Destination d
1909d 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a atabase handle *
1909e 2f 0a 20 20 42 74 72 65 65 20 2a 70 44 65 73 74 /. Btree *pDest
1909f 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
190a0 44 65 73 74 69 6e 61 74 69 6f 6e 20 62 2d 74 72 Destination b-tr
190a1 65 65 20 66 69 6c 65 20 2a 2f 0a 20 20 75 33 32 ee file */. u32
190a2 20 69 44 65 73 74 53 63 68 65 6d 61 3b 20 20 20 iDestSchema;
190a3 20 20 20 20 20 20 2f 2a 20 4f 72 69 67 69 6e 61 /* Origina
190a4 6c 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 l schema cookie
190a5 69 6e 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 2a in destination *
190a6 2f 0a 20 20 69 6e 74 20 62 44 65 73 74 4c 6f 63 /. int bDestLoc
190a7 6b 65 64 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 ked; /*
190a8 54 72 75 65 20 6f 6e 63 65 20 61 20 77 72 69 74 True once a writ
190a9 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 e-transaction is
190aa 20 6f 70 65 6e 20 6f 6e 20 70 44 65 73 74 20 2a open on pDest *
190ab 2f 0a 0a 20 20 50 67 6e 6f 20 69 4e 65 78 74 3b /.. Pgno iNext;
190ac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
190ad 20 50 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 Page number of
190ae 74 68 65 20 6e 65 78 74 20 73 6f 75 72 63 65 20 the next source
190af 70 61 67 65 20 74 6f 20 63 6f 70 79 20 2a 2f 0a page to copy */.
190b0 20 20 73 71 6c 69 74 65 33 2a 20 70 53 72 63 44 sqlite3* pSrcD
190b1 62 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53 6f b; /* So
190b2 75 72 63 65 20 64 61 74 61 62 61 73 65 20 68 61 urce database ha
190b3 6e 64 6c 65 20 2a 2f 0a 20 20 42 74 72 65 65 20 ndle */. Btree
190b4 2a 70 53 72 63 3b 20 20 20 20 20 20 20 20 20 20 *pSrc;
190b5 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 62 2d 74 /* Source b-t
190b6 72 65 65 20 66 69 6c 65 20 2a 2f 0a 0a 20 20 69 ree file */.. i
190b7 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 nt rc;
190b8 20 20 20 20 20 20 20 20 2f 2a 20 42 61 63 6b 75 /* Backu
190b9 70 20 70 72 6f 63 65 73 73 20 65 72 72 6f 72 20 p process error
190ba 63 6f 64 65 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 code */.. /* Th
190bb 65 73 65 20 74 77 6f 20 76 61 72 69 61 62 6c 65 ese two variable
190bc 73 20 61 72 65 20 73 65 74 20 62 79 20 65 76 65 s are set by eve
190bd 72 79 20 63 61 6c 6c 20 74 6f 20 62 61 63 6b 75 ry call to backu
190be 70 5f 73 74 65 70 28 29 2e 20 54 68 65 79 20 61 p_step(). They a
190bf 72 65 0a 20 20 2a 2a 20 72 65 61 64 20 62 79 20 re. ** read by
190c0 63 61 6c 6c 73 20 74 6f 20 62 61 63 6b 75 70 5f calls to backup_
190c1 72 65 6d 61 69 6e 69 6e 67 28 29 20 61 6e 64 20 remaining() and
190c2 62 61 63 6b 75 70 5f 70 61 67 65 63 6f 75 6e 74 backup_pagecount
190c3 28 29 2e 0a 20 20 2a 2f 0a 20 20 50 67 6e 6f 20 ().. */. Pgno
190c4 6e 52 65 6d 61 69 6e 69 6e 67 3b 20 20 20 20 20 nRemaining;
190c5 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
190c6 20 70 61 67 65 73 20 6c 65 66 74 20 74 6f 20 63 pages left to c
190c7 6f 70 79 20 2a 2f 0a 20 20 50 67 6e 6f 20 6e 50 opy */. Pgno nP
190c8 61 67 65 63 6f 75 6e 74 3b 20 20 20 20 20 20 20 agecount;
190c9 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 /* Total numbe
190ca 72 20 6f 66 20 70 61 67 65 73 20 74 6f 20 63 6f r of pages to co
190cb 70 79 20 2a 2f 0a 0a 20 20 69 6e 74 20 69 73 41 py */.. int isA
190cc 74 74 61 63 68 65 64 3b 20 20 20 20 20 20 20 20 ttached;
190cd 20 20 2f 2a 20 54 72 75 65 20 6f 6e 63 65 20 62 /* True once b
190ce 61 63 6b 75 70 20 68 61 73 20 62 65 65 6e 20 72 ackup has been r
190cf 65 67 69 73 74 65 72 65 64 20 77 69 74 68 20 70 egistered with p
190d0 61 67 65 72 20 2a 2f 0a 20 20 73 71 6c 69 74 65 ager */. sqlite
190d1 33 5f 62 61 63 6b 75 70 20 2a 70 4e 65 78 74 3b 3_backup *pNext;
190d2 20 20 20 2f 2a 20 4e 65 78 74 20 62 61 63 6b 75 /* Next backu
190d3 70 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 p associated wit
190d4 68 20 73 6f 75 72 63 65 20 70 61 67 65 72 20 2a h source pager *
190d5 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 48 52 45 /.};../*.** THRE
190d6 41 44 20 53 41 46 45 54 59 20 4e 4f 54 45 53 3a AD SAFETY NOTES:
190d7 0a 2a 2a 0a 2a 2a 20 20 20 4f 6e 63 65 20 69 74 .**.** Once it
190d8 20 68 61 73 20 62 65 65 6e 20 63 72 65 61 74 65 has been create
190d9 64 20 75 73 69 6e 67 20 62 61 63 6b 75 70 5f 69 d using backup_i
190da 6e 69 74 28 29 2c 20 61 20 73 69 6e 67 6c 65 20 nit(), a single
190db 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 0a 2a sqlite3_backup.*
190dc 2a 20 20 20 73 74 72 75 63 74 75 72 65 20 6d 61 * structure ma
190dd 79 20 62 65 20 61 63 63 65 73 73 65 64 20 76 69 y be accessed vi
190de 61 20 74 77 6f 20 67 72 6f 75 70 73 20 6f 66 20 a two groups of
190df 74 68 72 65 61 64 2d 73 61 66 65 20 65 6e 74 72 thread-safe entr
190e0 79 20 70 6f 69 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 y points:.**.**
190e1 20 20 20 20 2a 20 56 69 61 20 74 68 65 20 73 71 * Via the sq
190e2 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 58 58 58 lite3_backup_XXX
190e3 28 29 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 20 () API function
190e4 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20 61 6e backup_step() an
190e5 64 20 0a 2a 2a 20 20 20 20 20 20 20 62 61 63 6b d .** back
190e6 75 70 5f 66 69 6e 69 73 68 28 29 2e 20 42 6f 74 up_finish(). Bot
190e7 68 20 74 68 65 73 65 20 66 75 6e 63 74 69 6f 6e h these function
190e8 73 20 6f 62 74 61 69 6e 20 74 68 65 20 73 6f 75 s obtain the sou
190e9 72 63 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 rce database.**
190ea 20 20 20 20 20 20 68 61 6e 64 6c 65 20 6d 75 74 handle mut
190eb 65 78 20 61 6e 64 20 74 68 65 20 6d 75 74 65 78 ex and the mutex
190ec 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
190ed 20 74 68 65 20 73 6f 75 72 63 65 20 42 74 53 68 the source BtSh
190ee 61 72 65 64 20 0a 2a 2a 20 20 20 20 20 20 20 73 ared .** s
190ef 74 72 75 63 74 75 72 65 2c 20 69 6e 20 74 68 61 tructure, in tha
190f0 74 20 6f 72 64 65 72 2e 0a 2a 2a 0a 2a 2a 20 20 t order..**.**
190f1 20 20 20 2a 20 56 69 61 20 74 68 65 20 42 61 63 * Via the Bac
190f2 6b 75 70 55 70 64 61 74 65 28 29 20 61 6e 64 20 kupUpdate() and
190f3 42 61 63 6b 75 70 52 65 73 74 61 72 74 28 29 20 BackupRestart()
190f4 66 75 6e 63 74 69 6f 6e 73 2c 20 77 68 69 63 68 functions, which
190f5 20 61 72 65 0a 2a 2a 20 20 20 20 20 20 20 69 6e are.** in
190f6 76 6f 6b 65 64 20 62 79 20 74 68 65 20 70 61 67 voked by the pag
190f7 65 72 20 6c 61 79 65 72 20 74 6f 20 72 65 70 6f er layer to repo
190f8 72 74 20 76 61 72 69 6f 75 73 20 73 74 61 74 65 rt various state
190f9 20 63 68 61 6e 67 65 73 20 69 6e 0a 2a 2a 20 20 changes in.**
190fa 20 20 20 20 20 74 68 65 20 70 61 67 65 20 63 61 the page ca
190fb 63 68 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 che associated w
190fc 69 74 68 20 74 68 65 20 73 6f 75 72 63 65 20 64 ith the source d
190fd 61 74 61 62 61 73 65 2e 20 54 68 65 20 6d 75 74 atabase. The mut
190fe 65 78 0a 2a 2a 20 20 20 20 20 20 20 61 73 73 6f ex.** asso
190ff 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 ciated with the
19100 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 20 source database
19101 42 74 53 68 61 72 65 64 20 73 74 72 75 63 74 75 BtShared structu
19102 72 65 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 0a re will always .
19103 2a 2a 20 20 20 20 20 20 20 62 65 20 68 65 6c 64 ** be held
19104 20 77 68 65 6e 20 65 69 74 68 65 72 20 6f 66 20 when either of
19105 74 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 20 these functions
19106 61 72 65 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2a 0a are invoked..**.
19107 2a 2a 20 20 20 54 68 65 20 6f 74 68 65 72 20 73 ** The other s
19108 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 58 58 qlite3_backup_XX
19109 58 28 29 20 41 50 49 20 66 75 6e 63 74 69 6f 6e X() API function
1910a 73 2c 20 62 61 63 6b 75 70 5f 72 65 6d 61 69 6e s, backup_remain
1910b 69 6e 67 28 29 20 61 6e 64 0a 2a 2a 20 20 20 62 ing() and.** b
1910c 61 63 6b 75 70 5f 70 61 67 65 63 6f 75 6e 74 28 ackup_pagecount(
1910d 29 20 61 72 65 20 6e 6f 74 20 74 68 72 65 61 64 ) are not thread
1910e 2d 73 61 66 65 20 66 75 6e 63 74 69 6f 6e 73 2e -safe functions.
1910f 20 49 66 20 74 68 65 79 20 61 72 65 20 63 61 6c If they are cal
19110 6c 65 64 0a 2a 2a 20 20 20 77 68 69 6c 65 20 73 led.** while s
19111 6f 6d 65 20 6f 74 68 65 72 20 74 68 72 65 61 64 ome other thread
19112 20 69 73 20 63 61 6c 6c 69 6e 67 20 62 61 63 6b is calling back
19113 75 70 5f 73 74 65 70 28 29 20 6f 72 20 62 61 63 up_step() or bac
19114 6b 75 70 5f 66 69 6e 69 73 68 28 29 2c 0a 2a 2a kup_finish(),.**
19115 20 20 20 74 68 65 20 76 61 6c 75 65 73 20 72 65 the values re
19116 74 75 72 6e 65 64 20 6d 61 79 20 62 65 20 69 6e turned may be in
19117 76 61 6c 69 64 2e 20 54 68 65 72 65 20 69 73 20 valid. There is
19118 6e 6f 20 77 61 79 20 66 6f 72 20 61 20 63 61 6c no way for a cal
19119 6c 20 74 6f 0a 2a 2a 20 20 20 42 61 63 6b 75 70 l to.** Backup
1911a 55 70 64 61 74 65 28 29 20 6f 72 20 42 61 63 6b Update() or Back
1911b 75 70 52 65 73 74 61 72 74 28 29 20 74 6f 20 69 upRestart() to i
1911c 6e 74 65 72 66 65 72 65 20 77 69 74 68 20 62 61 nterfere with ba
1911d 63 6b 75 70 5f 72 65 6d 61 69 6e 69 6e 67 28 29 ckup_remaining()
1911e 0a 2a 2a 20 20 20 6f 72 20 62 61 63 6b 75 70 5f .** or backup_
1911f 70 61 67 65 63 6f 75 6e 74 28 29 2e 0a 2a 2a 0a pagecount()..**.
19120 2a 2a 20 20 20 44 65 70 65 6e 64 69 6e 67 20 6f ** Depending o
19121 6e 20 74 68 65 20 53 51 4c 69 74 65 20 63 6f 6e n the SQLite con
19122 66 69 67 75 72 61 74 69 6f 6e 2c 20 74 68 65 20 figuration, the
19123 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 73 database handles
19124 20 61 6e 64 2f 6f 72 0a 2a 2a 20 20 20 74 68 65 and/or.** the
19125 20 42 74 72 65 65 20 6f 62 6a 65 63 74 73 20 6d Btree objects m
19126 61 79 20 68 61 76 65 20 74 68 65 69 72 20 6f 77 ay have their ow
19127 6e 20 6d 75 74 65 78 65 73 20 74 68 61 74 20 72 n mutexes that r
19128 65 71 75 69 72 65 20 6c 6f 63 6b 69 6e 67 2e 0a equire locking..
19129 2a 2a 20 20 20 4e 6f 6e 2d 73 68 61 72 61 62 6c ** Non-sharabl
1912a 65 20 42 74 72 65 65 73 20 28 69 6e 2d 6d 65 6d e Btrees (in-mem
1912b 6f 72 79 20 64 61 74 61 62 61 73 65 73 20 66 6f ory databases fo
1912c 72 20 65 78 61 6d 70 6c 65 29 2c 20 64 6f 20 6e r example), do n
1912d 6f 74 20 68 61 76 65 0a 2a 2a 20 20 20 61 73 73 ot have.** ass
1912e 6f 63 69 61 74 65 64 20 6d 75 74 65 78 65 73 2e ociated mutexes.
1912f 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 .*/../*.** Retur
19130 6e 20 61 20 70 6f 69 6e 74 65 72 20 63 6f 72 72 n a pointer corr
19131 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 64 61 74 esponding to dat
19132 61 62 61 73 65 20 7a 44 62 20 28 69 2e 65 2e 20 abase zDb (i.e.
19133 22 6d 61 69 6e 22 2c 20 22 74 65 6d 70 22 29 0a "main", "temp").
19134 2a 2a 20 69 6e 20 63 6f 6e 6e 65 63 74 69 6f 6e ** in connection
19135 20 68 61 6e 64 6c 65 20 70 44 62 2e 20 49 66 20 handle pDb. If
19136 73 75 63 68 20 61 20 64 61 74 61 62 61 73 65 20 such a database
19137 63 61 6e 6e 6f 74 20 62 65 20 66 6f 75 6e 64 2c cannot be found,
19138 20 72 65 74 75 72 6e 0a 2a 2a 20 61 20 4e 55 4c return.** a NUL
19139 4c 20 70 6f 69 6e 74 65 72 20 61 6e 64 20 77 72 L pointer and wr
1913a 69 74 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 ite an error mes
1913b 73 61 67 65 20 74 6f 20 70 45 72 72 6f 72 44 62 sage to pErrorDb
1913c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 22 ..**.** If the "
1913d 74 65 6d 70 22 20 64 61 74 61 62 61 73 65 20 69 temp" database i
1913e 73 20 72 65 71 75 65 73 74 65 64 2c 20 69 74 20 s requested, it
1913f 6d 61 79 20 6e 65 65 64 20 74 6f 20 62 65 20 6f may need to be o
19140 70 65 6e 65 64 20 62 79 20 74 68 69 73 20 0a 2a pened by this .*
19141 2a 20 66 75 6e 63 74 69 6f 6e 2e 20 49 66 20 61 * function. If a
19142 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77 n error occurs w
19143 68 69 6c 65 20 64 6f 69 6e 67 20 73 6f 2c 20 72 hile doing so, r
19144 65 74 75 72 6e 20 30 20 61 6e 64 20 77 72 69 74 eturn 0 and writ
19145 65 20 61 6e 20 0a 2a 2a 20 65 72 72 6f 72 20 6d e an .** error m
19146 65 73 73 61 67 65 20 74 6f 20 70 45 72 72 6f 72 essage to pError
19147 44 62 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 42 74 Db..*/.static Bt
19148 72 65 65 20 2a 66 69 6e 64 42 74 72 65 65 28 73 ree *findBtree(s
19149 71 6c 69 74 65 33 20 2a 70 45 72 72 6f 72 44 62 qlite3 *pErrorDb
1914a 2c 20 73 71 6c 69 74 65 33 20 2a 70 44 62 2c 20 , sqlite3 *pDb,
1914b 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 29 const char *zDb)
1914c 7b 0a 20 20 69 6e 74 20 69 20 3d 20 73 71 6c 69 {. int i = sqli
1914d 74 65 33 46 69 6e 64 44 62 4e 61 6d 65 28 70 44 te3FindDbName(pD
1914e 62 2c 20 7a 44 62 29 3b 0a 0a 20 20 69 66 28 20 b, zDb);.. if(
1914f 69 3d 3d 31 20 29 7b 0a 20 20 20 20 50 61 72 73 i==1 ){. Pars
19150 65 20 2a 70 50 61 72 73 65 3b 0a 20 20 20 20 69 e *pParse;. i
19151 6e 74 20 72 63 20 3d 20 30 3b 0a 20 20 20 20 70 nt rc = 0;. p
19152 50 61 72 73 65 20 3d 20 73 71 6c 69 74 65 33 53 Parse = sqlite3S
19153 74 61 63 6b 41 6c 6c 6f 63 5a 65 72 6f 28 70 45 tackAllocZero(pE
19154 72 72 6f 72 44 62 2c 20 73 69 7a 65 6f 66 28 2a rrorDb, sizeof(*
19155 70 50 61 72 73 65 29 29 3b 0a 20 20 20 20 69 66 pParse));. if
19156 28 20 70 50 61 72 73 65 3d 3d 30 20 29 7b 0a 20 ( pParse==0 ){.
19157 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f sqlite3Erro
19158 72 28 70 45 72 72 6f 72 44 62 2c 20 53 51 4c 49 r(pErrorDb, SQLI
19159 54 45 5f 4e 4f 4d 45 4d 2c 20 22 6f 75 74 20 6f TE_NOMEM, "out o
1915a 66 20 6d 65 6d 6f 72 79 22 29 3b 0a 20 20 20 20 f memory");.
1915b 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f rc = SQLITE_NO
1915c 4d 45 4d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a MEM;. }else{.
1915d 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 64 62 pParse->db
1915e 20 3d 20 70 44 62 3b 0a 20 20 20 20 20 20 69 66 = pDb;. if
1915f 28 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 65 6d ( sqlite3OpenTem
19160 70 44 61 74 61 62 61 73 65 28 70 50 61 72 73 65 pDatabase(pParse
19161 29 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c ) ){. sql
19162 69 74 65 33 45 72 72 6f 72 43 6c 65 61 72 28 70 ite3ErrorClear(p
19163 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 20 20 Parse);.
19164 73 71 6c 69 74 65 33 45 72 72 6f 72 28 70 45 72 sqlite3Error(pEr
19165 72 6f 72 44 62 2c 20 70 50 61 72 73 65 2d 3e 72 rorDb, pParse->r
19166 63 2c 20 22 25 73 22 2c 20 70 50 61 72 73 65 2d c, "%s", pParse-
19167 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 20 >zErrMsg);.
19168 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 rc = SQLITE_E
19169 52 52 4f 52 3b 0a 20 20 20 20 20 20 7d 0a 20 20 RROR;. }.
1916a 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 63 6b sqlite3Stack
1916b 46 72 65 65 28 70 45 72 72 6f 72 44 62 2c 20 70 Free(pErrorDb, p
1916c 50 61 72 73 65 29 3b 0a 20 20 20 20 7d 0a 20 20 Parse);. }.
1916d 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 if( rc ){.
1916e 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 return 0;.
1916f 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 69 3c 30 }. }.. if( i<0
19170 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 ){. sqlite3E
19171 72 72 6f 72 28 70 45 72 72 6f 72 44 62 2c 20 53 rror(pErrorDb, S
19172 51 4c 49 54 45 5f 45 52 52 4f 52 2c 20 22 75 6e QLITE_ERROR, "un
19173 6b 6e 6f 77 6e 20 64 61 74 61 62 61 73 65 20 25 known database %
19174 73 22 2c 20 7a 44 62 29 3b 0a 20 20 20 20 72 65 s", zDb);. re
19175 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 72 turn 0;. }.. r
19176 65 74 75 72 6e 20 70 44 62 2d 3e 61 44 62 5b 69 eturn pDb->aDb[i
19177 5d 2e 70 42 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 ].pBt;.}../*.**
19178 43 72 65 61 74 65 20 61 6e 20 73 71 6c 69 74 65 Create an sqlite
19179 33 5f 62 61 63 6b 75 70 20 70 72 6f 63 65 73 73 3_backup process
1917a 20 74 6f 20 63 6f 70 79 20 74 68 65 20 63 6f 6e to copy the con
1917b 74 65 6e 74 73 20 6f 66 20 7a 53 72 63 44 62 20 tents of zSrcDb
1917c 66 72 6f 6d 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 from.** connecti
1917d 6f 6e 20 68 61 6e 64 6c 65 20 70 53 72 63 44 62 on handle pSrcDb
1917e 20 74 6f 20 7a 44 65 73 74 44 62 20 69 6e 20 70 to zDestDb in p
1917f 44 65 73 74 44 62 2e 20 49 66 20 73 75 63 63 65 DestDb. If succe
19180 73 73 66 75 6c 2c 20 72 65 74 75 72 6e 0a 2a 2a ssful, return.**
19181 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 a pointer to th
19182 65 20 6e 65 77 20 73 71 6c 69 74 65 33 5f 62 61 e new sqlite3_ba
19183 63 6b 75 70 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a ckup object..**.
19184 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f ** If an error o
19185 63 63 75 72 73 2c 20 4e 55 4c 4c 20 69 73 20 72 ccurs, NULL is r
19186 65 74 75 72 6e 65 64 20 61 6e 64 20 61 6e 20 65 eturned and an e
19187 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 20 65 72 rror code and er
19188 72 6f 72 20 6d 65 73 73 61 67 65 0a 2a 2a 20 73 ror message.** s
19189 74 6f 72 65 64 20 69 6e 20 64 61 74 61 62 61 73 tored in databas
1918a 65 20 68 61 6e 64 6c 65 20 70 44 65 73 74 44 62 e handle pDestDb
1918b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
1918c 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a sqlite3_backup *
1918d 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 69 sqlite3_backup_i
1918e 6e 69 74 28 0a 20 20 73 71 6c 69 74 65 33 2a 20 nit(. sqlite3*
1918f 70 44 65 73 74 44 62 2c 20 20 20 20 20 20 20 20 pDestDb,
19190 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
19191 44 61 74 61 62 61 73 65 20 74 6f 20 77 72 69 74 Database to writ
19192 65 20 74 6f 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 e to */. const
19193 63 68 61 72 20 2a 7a 44 65 73 74 44 62 2c 20 20 char *zDestDb,
19194 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19195 2f 2a 20 4e 61 6d 65 20 6f 66 20 64 61 74 61 62 /* Name of datab
19196 61 73 65 20 77 69 74 68 69 6e 20 70 44 65 73 74 ase within pDest
19197 44 62 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 2a Db */. sqlite3*
19198 20 70 53 72 63 44 62 2c 20 20 20 20 20 20 20 20 pSrcDb,
19199 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1919a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 Database connec
1919b 74 69 6f 6e 20 74 6f 20 72 65 61 64 20 66 72 6f tion to read fro
1919c 6d 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 m */. const cha
1919d 72 20 2a 7a 53 72 63 44 62 20 20 20 20 20 20 20 r *zSrcDb
1919e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1919f 4e 61 6d 65 20 6f 66 20 64 61 74 61 62 61 73 65 Name of database
191a0 20 77 69 74 68 69 6e 20 70 53 72 63 44 62 20 2a within pSrcDb *
191a1 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 62 /.){. sqlite3_b
191a2 61 63 6b 75 70 20 2a 70 3b 20 20 20 20 20 20 20 ackup *p;
191a3 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
191a4 56 61 6c 75 65 20 74 6f 20 72 65 74 75 72 6e 20 Value to return
191a5 2a 2f 0a 0a 20 20 2f 2a 20 4c 6f 63 6b 20 74 68 */.. /* Lock th
191a6 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 e source databas
191a7 65 20 68 61 6e 64 6c 65 2e 20 54 68 65 20 64 65 e handle. The de
191a8 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 stination databa
191a9 73 65 0a 20 20 2a 2a 20 68 61 6e 64 6c 65 20 69 se. ** handle i
191aa 73 20 6e 6f 74 20 6c 6f 63 6b 65 64 20 69 6e 20 s not locked in
191ab 74 68 69 73 20 72 6f 75 74 69 6e 65 2c 20 62 75 this routine, bu
191ac 74 20 69 74 20 69 73 20 6c 6f 63 6b 65 64 20 69 t it is locked i
191ad 6e 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 62 n. ** sqlite3_b
191ae 61 63 6b 75 70 5f 73 74 65 70 28 29 2e 20 54 68 ackup_step(). Th
191af 65 20 75 73 65 72 20 69 73 20 72 65 71 75 69 72 e user is requir
191b0 65 64 20 74 6f 20 65 6e 73 75 72 65 20 74 68 61 ed to ensure tha
191b1 74 20 6e 6f 0a 20 20 2a 2a 20 6f 74 68 65 72 20 t no. ** other
191b2 74 68 72 65 61 64 20 61 63 63 65 73 73 65 73 20 thread accesses
191b3 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 the destination
191b4 68 61 6e 64 6c 65 20 66 6f 72 20 74 68 65 20 64 handle for the d
191b5 75 72 61 74 69 6f 6e 0a 20 20 2a 2a 20 6f 66 20 uration. ** of
191b6 74 68 65 20 62 61 63 6b 75 70 20 6f 70 65 72 61 the backup opera
191b7 74 69 6f 6e 2e 20 20 41 6e 79 20 61 74 74 65 6d tion. Any attem
191b8 70 74 20 74 6f 20 75 73 65 20 74 68 65 20 64 65 pt to use the de
191b9 73 74 69 6e 61 74 69 6f 6e 0a 20 20 2a 2a 20 64 stination. ** d
191ba 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
191bb 6f 6e 20 77 68 69 6c 65 20 61 20 62 61 63 6b 75 on while a backu
191bc 70 20 69 73 20 69 6e 20 70 72 6f 67 72 65 73 73 p is in progress
191bd 20 6d 61 79 20 63 61 75 73 65 0a 20 20 2a 2a 20 may cause. **
191be 61 20 6d 61 6c 66 75 6e 63 74 69 6f 6e 20 6f 72 a malfunction or
191bf 20 61 20 64 65 61 64 6c 6f 63 6b 2e 0a 20 20 2a a deadlock.. *
191c0 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 /. sqlite3_mute
191c1 78 5f 65 6e 74 65 72 28 70 53 72 63 44 62 2d 3e x_enter(pSrcDb->
191c2 6d 75 74 65 78 29 3b 0a 20 20 73 71 6c 69 74 65 mutex);. sqlite
191c3 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 44 3_mutex_enter(pD
191c4 65 73 74 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 0a estDb->mutex);..
191c5 20 20 69 66 28 20 70 53 72 63 44 62 3d 3d 70 44 if( pSrcDb==pD
191c6 65 73 74 44 62 20 29 7b 0a 20 20 20 20 73 71 6c estDb ){. sql
191c7 69 74 65 33 45 72 72 6f 72 28 0a 20 20 20 20 20 ite3Error(.
191c8 20 20 20 70 44 65 73 74 44 62 2c 20 53 51 4c 49 pDestDb, SQLI
191c9 54 45 5f 45 52 52 4f 52 2c 20 22 73 6f 75 72 63 TE_ERROR, "sourc
191ca 65 20 61 6e 64 20 64 65 73 74 69 6e 61 74 69 6f e and destinatio
191cb 6e 20 6d 75 73 74 20 62 65 20 64 69 73 74 69 6e n must be distin
191cc 63 74 22 0a 20 20 20 20 29 3b 0a 20 20 20 20 70 ct". );. p
191cd 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20 7b 0a = 0;. }else {.
191ce 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 /* Allocate
191cf 73 70 61 63 65 20 66 6f 72 20 61 20 6e 65 77 20 space for a new
191d0 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 6f sqlite3_backup o
191d1 62 6a 65 63 74 20 2a 2f 0a 20 20 20 20 70 20 3d bject */. p =
191d2 20 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 (sqlite3_backup
191d3 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f *)sqlite3_mallo
191d4 63 28 73 69 7a 65 6f 66 28 73 71 6c 69 74 65 33 c(sizeof(sqlite3
191d5 5f 62 61 63 6b 75 70 29 29 3b 0a 20 20 20 20 69 _backup));. i
191d6 66 28 20 21 70 20 29 7b 0a 20 20 20 20 20 20 73 f( !p ){. s
191d7 71 6c 69 74 65 33 45 72 72 6f 72 28 70 44 65 73 qlite3Error(pDes
191d8 74 44 62 2c 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 tDb, SQLITE_NOME
191d9 4d 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 7d M, 0);. }. }
191da 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 61 6c .. /* If the al
191db 6c 6f 63 61 74 69 6f 6e 20 73 75 63 63 65 65 64 location succeed
191dc 65 64 2c 20 70 6f 70 75 6c 61 74 65 20 74 68 65 ed, populate the
191dd 20 6e 65 77 20 6f 62 6a 65 63 74 2e 20 2a 2f 0a new object. */.
191de 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 6d if( p ){. m
191df 65 6d 73 65 74 28 70 2c 20 30 2c 20 73 69 7a 65 emset(p, 0, size
191e0 6f 66 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 of(sqlite3_backu
191e1 70 29 29 3b 0a 20 20 20 20 70 2d 3e 70 53 72 63 p));. p->pSrc
191e2 20 3d 20 66 69 6e 64 42 74 72 65 65 28 70 44 65 = findBtree(pDe
191e3 73 74 44 62 2c 20 70 53 72 63 44 62 2c 20 7a 53 stDb, pSrcDb, zS
191e4 72 63 44 62 29 3b 0a 20 20 20 20 70 2d 3e 70 44 rcDb);. p->pD
191e5 65 73 74 20 3d 20 66 69 6e 64 42 74 72 65 65 28 est = findBtree(
191e6 70 44 65 73 74 44 62 2c 20 70 44 65 73 74 44 62 pDestDb, pDestDb
191e7 2c 20 7a 44 65 73 74 44 62 29 3b 0a 20 20 20 20 , zDestDb);.
191e8 70 2d 3e 70 44 65 73 74 44 62 20 3d 20 70 44 65 p->pDestDb = pDe
191e9 73 74 44 62 3b 0a 20 20 20 20 70 2d 3e 70 53 72 stDb;. p->pSr
191ea 63 44 62 20 3d 20 70 53 72 63 44 62 3b 0a 20 20 cDb = pSrcDb;.
191eb 20 20 70 2d 3e 69 4e 65 78 74 20 3d 20 31 3b 0a p->iNext = 1;.
191ec 20 20 20 20 70 2d 3e 69 73 41 74 74 61 63 68 65 p->isAttache
191ed 64 20 3d 20 30 3b 0a 0a 20 20 20 20 69 66 28 20 d = 0;.. if(
191ee 30 3d 3d 70 2d 3e 70 53 72 63 20 7c 7c 20 30 3d 0==p->pSrc || 0=
191ef 3d 70 2d 3e 70 44 65 73 74 20 29 7b 0a 20 20 20 =p->pDest ){.
191f0 20 20 20 2f 2a 20 4f 6e 65 20 28 6f 72 20 62 6f /* One (or bo
191f1 74 68 29 20 6f 66 20 74 68 65 20 6e 61 6d 65 64 th) of the named
191f2 20 64 61 74 61 62 61 73 65 73 20 64 69 64 20 6e databases did n
191f3 6f 74 20 65 78 69 73 74 2e 20 41 6e 20 65 72 72 ot exist. An err
191f4 6f 72 20 68 61 73 0a 20 20 20 20 20 20 2a 2a 20 or has. **
191f5 61 6c 72 65 61 64 79 20 62 65 65 6e 20 77 72 69 already been wri
191f6 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 70 44 tten into the pD
191f7 65 73 74 44 62 20 68 61 6e 64 6c 65 2e 20 41 6c estDb handle. Al
191f8 6c 20 74 68 61 74 20 69 73 20 6c 65 66 74 0a 20 l that is left.
191f9 20 20 20 20 20 2a 2a 20 74 6f 20 64 6f 20 68 65 ** to do he
191fa 72 65 20 69 73 20 66 72 65 65 20 74 68 65 20 73 re is free the s
191fb 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 73 74 qlite3_backup st
191fc 72 75 63 74 75 72 65 2e 0a 20 20 20 20 20 20 2a ructure.. *
191fd 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f /. sqlite3_
191fe 66 72 65 65 28 70 29 3b 0a 20 20 20 20 20 20 70 free(p);. p
191ff 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a = 0;. }. }.
19200 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 70 if( p ){. p
19201 2d 3e 70 53 72 63 2d 3e 6e 42 61 63 6b 75 70 2b ->pSrc->nBackup+
19202 2b 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 +;. }.. sqlite
19203 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 44 3_mutex_leave(pD
19204 65 73 74 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 estDb->mutex);.
19205 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
19206 65 61 76 65 28 70 53 72 63 44 62 2d 3e 6d 75 74 eave(pSrcDb->mut
19207 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b ex);. return p;
19208 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 72 67 75 6d 65 .}../*.** Argume
19209 6e 74 20 72 63 20 69 73 20 61 6e 20 53 51 4c 69 nt rc is an SQLi
1920a 74 65 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 52 te error code. R
1920b 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 eturn true if th
1920c 69 73 20 65 72 72 6f 72 20 69 73 20 0a 2a 2a 20 is error is .**
1920d 63 6f 6e 73 69 64 65 72 65 64 20 66 61 74 61 6c considered fatal
1920e 20 69 66 20 65 6e 63 6f 75 6e 74 65 72 65 64 20 if encountered
1920f 64 75 72 69 6e 67 20 61 20 62 61 63 6b 75 70 20 during a backup
19210 6f 70 65 72 61 74 69 6f 6e 2e 20 41 6c 6c 20 65 operation. All e
19211 72 72 6f 72 73 0a 2a 2a 20 61 72 65 20 63 6f 6e rrors.** are con
19212 73 69 64 65 72 65 64 20 66 61 74 61 6c 20 65 78 sidered fatal ex
19213 63 65 70 74 20 66 6f 72 20 53 51 4c 49 54 45 5f cept for SQLITE_
19214 42 55 53 59 20 61 6e 64 20 53 51 4c 49 54 45 5f BUSY and SQLITE_
19215 4c 4f 43 4b 45 44 2e 0a 2a 2f 0a 73 74 61 74 69 LOCKED..*/.stati
19216 63 20 69 6e 74 20 69 73 46 61 74 61 6c 45 72 72 c int isFatalErr
19217 6f 72 28 69 6e 74 20 72 63 29 7b 0a 20 20 72 65 or(int rc){. re
19218 74 75 72 6e 20 28 72 63 21 3d 53 51 4c 49 54 45 turn (rc!=SQLITE
19219 5f 4f 4b 20 26 26 20 72 63 21 3d 53 51 4c 49 54 _OK && rc!=SQLIT
1921a 45 5f 42 55 53 59 20 26 26 20 41 4c 57 41 59 53 E_BUSY && ALWAYS
1921b 28 72 63 21 3d 53 51 4c 49 54 45 5f 4c 4f 43 4b (rc!=SQLITE_LOCK
1921c 45 44 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 ED));.}../*.** P
1921d 61 72 61 6d 65 74 65 72 20 7a 53 72 63 44 61 74 arameter zSrcDat
1921e 61 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 62 75 a points to a bu
1921f 66 66 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20 ffer containing
19220 74 68 65 20 64 61 74 61 20 66 6f 72 20 0a 2a 2a the data for .**
19221 20 70 61 67 65 20 69 53 72 63 50 67 20 66 72 6f page iSrcPg fro
19222 6d 20 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 m the source dat
19223 61 62 61 73 65 2e 20 43 6f 70 79 20 74 68 69 73 abase. Copy this
19224 20 64 61 74 61 20 69 6e 74 6f 20 74 68 65 20 0a data into the .
19225 2a 2a 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 ** destination d
19226 61 74 61 62 61 73 65 2e 0a 2a 2f 0a 73 74 61 74 atabase..*/.stat
19227 69 63 20 69 6e 74 20 62 61 63 6b 75 70 4f 6e 65 ic int backupOne
19228 50 61 67 65 28 73 71 6c 69 74 65 33 5f 62 61 63 Page(sqlite3_bac
19229 6b 75 70 20 2a 70 2c 20 50 67 6e 6f 20 69 53 72 kup *p, Pgno iSr
1922a 63 50 67 2c 20 63 6f 6e 73 74 20 75 38 20 2a 7a cPg, const u8 *z
1922b 53 72 63 44 61 74 61 29 7b 0a 20 20 50 61 67 65 SrcData){. Page
1922c 72 20 2a 20 63 6f 6e 73 74 20 70 44 65 73 74 50 r * const pDestP
1922d 61 67 65 72 20 3d 20 73 71 6c 69 74 65 33 42 74 ager = sqlite3Bt
1922e 72 65 65 50 61 67 65 72 28 70 2d 3e 70 44 65 73 reePager(p->pDes
1922f 74 29 3b 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 t);. const int
19230 6e 53 72 63 50 67 73 7a 20 3d 20 73 71 6c 69 74 nSrcPgsz = sqlit
19231 65 33 42 74 72 65 65 47 65 74 50 61 67 65 53 69 e3BtreeGetPageSi
19232 7a 65 28 70 2d 3e 70 53 72 63 29 3b 0a 20 20 69 ze(p->pSrc);. i
19233 6e 74 20 6e 44 65 73 74 50 67 73 7a 20 3d 20 73 nt nDestPgsz = s
19234 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61 qlite3BtreeGetPa
19235 67 65 53 69 7a 65 28 70 2d 3e 70 44 65 73 74 29 geSize(p->pDest)
19236 3b 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 6e 43 ;. const int nC
19237 6f 70 79 20 3d 20 4d 49 4e 28 6e 53 72 63 50 67 opy = MIN(nSrcPg
19238 73 7a 2c 20 6e 44 65 73 74 50 67 73 7a 29 3b 0a sz, nDestPgsz);.
19239 20 20 63 6f 6e 73 74 20 69 36 34 20 69 45 6e 64 const i64 iEnd
1923a 20 3d 20 28 69 36 34 29 69 53 72 63 50 67 2a 28 = (i64)iSrcPg*(
1923b 69 36 34 29 6e 53 72 63 50 67 73 7a 3b 0a 0a 20 i64)nSrcPgsz;..
1923c 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
1923d 5f 4f 4b 3b 0a 20 20 69 36 34 20 69 4f 66 66 3b _OK;. i64 iOff;
1923e 0a 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 62 .. assert( p->b
1923f 44 65 73 74 4c 6f 63 6b 65 64 20 29 3b 0a 20 20 DestLocked );.
19240 61 73 73 65 72 74 28 20 21 69 73 46 61 74 61 6c assert( !isFatal
19241 45 72 72 6f 72 28 70 2d 3e 72 63 29 20 29 3b 0a Error(p->rc) );.
19242 20 20 61 73 73 65 72 74 28 20 69 53 72 63 50 67 assert( iSrcPg
19243 21 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 !=PENDING_BYTE_P
19244 41 47 45 28 70 2d 3e 70 53 72 63 2d 3e 70 42 74 AGE(p->pSrc->pBt
19245 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 7a ) );. assert( z
19246 53 72 63 44 61 74 61 20 29 3b 0a 0a 20 20 2f 2a SrcData );.. /*
19247 20 43 61 74 63 68 20 74 68 65 20 63 61 73 65 20 Catch the case
19248 77 68 65 72 65 20 74 68 65 20 64 65 73 74 69 6e where the destin
19249 61 74 69 6f 6e 20 69 73 20 61 6e 20 69 6e 2d 6d ation is an in-m
1924a 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20 61 emory database a
1924b 6e 64 20 74 68 65 0a 20 20 2a 2a 20 70 61 67 65 nd the. ** page
1924c 20 73 69 7a 65 73 20 6f 66 20 74 68 65 20 73 6f sizes of the so
1924d 75 72 63 65 20 61 6e 64 20 64 65 73 74 69 6e 61 urce and destina
1924e 74 69 6f 6e 20 64 69 66 66 65 72 2e 20 0a 20 20 tion differ. .
1924f 2a 2f 0a 20 20 69 66 28 20 6e 53 72 63 50 67 73 */. if( nSrcPgs
19250 7a 21 3d 6e 44 65 73 74 50 67 73 7a 20 26 26 20 z!=nDestPgsz &&
19251 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 4d 65 sqlite3PagerIsMe
19252 6d 64 62 28 73 71 6c 69 74 65 33 42 74 72 65 65 mdb(sqlite3Btree
19253 50 61 67 65 72 28 70 2d 3e 70 44 65 73 74 29 29 Pager(p->pDest))
19254 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c ){. rc = SQL
19255 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 ITE_READONLY;.
19256 7d 0a 0a 20 20 2f 2a 20 54 68 69 73 20 6c 6f 6f }.. /* This loo
19257 70 20 72 75 6e 73 20 6f 6e 63 65 20 66 6f 72 20 p runs once for
19258 65 61 63 68 20 64 65 73 74 69 6e 61 74 69 6f 6e each destination
19259 20 70 61 67 65 20 73 70 61 6e 6e 65 64 20 62 79 page spanned by
1925a 20 74 68 65 20 73 6f 75 72 63 65 20 0a 20 20 2a the source . *
1925b 2a 20 70 61 67 65 2e 20 46 6f 72 20 65 61 63 68 * page. For each
1925c 20 69 74 65 72 61 74 69 6f 6e 2c 20 76 61 72 69 iteration, vari
1925d 61 62 6c 65 20 69 4f 66 66 20 69 73 20 73 65 74 able iOff is set
1925e 20 74 6f 20 74 68 65 20 62 79 74 65 20 6f 66 66 to the byte off
1925f 73 65 74 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 set. ** of the
19260 64 65 73 74 69 6e 61 74 69 6f 6e 20 70 61 67 65 destination page
19261 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 4f 66 .. */. for(iOf
19262 66 3d 69 45 6e 64 2d 28 69 36 34 29 6e 53 72 63 f=iEnd-(i64)nSrc
19263 50 67 73 7a 3b 20 72 63 3d 3d 53 51 4c 49 54 45 Pgsz; rc==SQLITE
19264 5f 4f 4b 20 26 26 20 69 4f 66 66 3c 69 45 6e 64 _OK && iOff<iEnd
19265 3b 20 69 4f 66 66 2b 3d 6e 44 65 73 74 50 67 73 ; iOff+=nDestPgs
19266 7a 29 7b 0a 20 20 20 20 44 62 50 61 67 65 20 2a z){. DbPage *
19267 70 44 65 73 74 50 67 20 3d 20 30 3b 0a 20 20 20 pDestPg = 0;.
19268 20 50 67 6e 6f 20 69 44 65 73 74 20 3d 20 28 50 Pgno iDest = (P
19269 67 6e 6f 29 28 69 4f 66 66 2f 6e 44 65 73 74 50 gno)(iOff/nDestP
1926a 67 73 7a 29 2b 31 3b 0a 20 20 20 20 69 66 28 20 gsz)+1;. if(
1926b 69 44 65 73 74 3d 3d 50 45 4e 44 49 4e 47 5f 42 iDest==PENDING_B
1926c 59 54 45 5f 50 41 47 45 28 70 2d 3e 70 44 65 73 YTE_PAGE(p->pDes
1926d 74 2d 3e 70 42 74 29 20 29 20 63 6f 6e 74 69 6e t->pBt) ) contin
1926e 75 65 3b 0a 20 20 20 20 69 66 28 20 53 51 4c 49 ue;. if( SQLI
1926f 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c TE_OK==(rc = sql
19270 69 74 65 33 50 61 67 65 72 47 65 74 28 70 44 65 ite3PagerGet(pDe
19271 73 74 50 61 67 65 72 2c 20 69 44 65 73 74 2c 20 stPager, iDest,
19272 26 70 44 65 73 74 50 67 29 29 0a 20 20 20 20 20 &pDestPg)).
19273 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 && SQLITE_OK==(r
19274 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
19275 57 72 69 74 65 28 70 44 65 73 74 50 67 29 29 0a Write(pDestPg)).
19276 20 20 20 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e ){. con
19277 73 74 20 75 38 20 2a 7a 49 6e 20 3d 20 26 7a 53 st u8 *zIn = &zS
19278 72 63 44 61 74 61 5b 69 4f 66 66 25 6e 53 72 63 rcData[iOff%nSrc
19279 50 67 73 7a 5d 3b 0a 20 20 20 20 20 20 75 38 20 Pgsz];. u8
1927a 2a 7a 44 65 73 74 44 61 74 61 20 3d 20 73 71 6c *zDestData = sql
1927b 69 74 65 33 50 61 67 65 72 47 65 74 44 61 74 61 ite3PagerGetData
1927c 28 70 44 65 73 74 50 67 29 3b 0a 20 20 20 20 20 (pDestPg);.
1927d 20 75 38 20 2a 7a 4f 75 74 20 3d 20 26 7a 44 65 u8 *zOut = &zDe
1927e 73 74 44 61 74 61 5b 69 4f 66 66 25 6e 44 65 73 stData[iOff%nDes
1927f 74 50 67 73 7a 5d 3b 0a 0a 20 20 20 20 20 20 2f tPgsz];.. /
19280 2a 20 43 6f 70 79 20 74 68 65 20 64 61 74 61 20 * Copy the data
19281 66 72 6f 6d 20 74 68 65 20 73 6f 75 72 63 65 20 from the source
19282 70 61 67 65 20 69 6e 74 6f 20 74 68 65 20 64 65 page into the de
19283 73 74 69 6e 61 74 69 6f 6e 20 70 61 67 65 2e 0a stination page..
19284 20 20 20 20 20 20 2a 2a 20 54 68 65 6e 20 63 6c ** Then cl
19285 65 61 72 20 74 68 65 20 42 74 72 65 65 20 6c 61 ear the Btree la
19286 79 65 72 20 4d 65 6d 50 61 67 65 2e 69 73 49 6e yer MemPage.isIn
19287 69 74 20 66 6c 61 67 2e 20 42 6f 74 68 20 74 68 it flag. Both th
19288 69 73 20 6d 6f 64 75 6c 65 0a 20 20 20 20 20 20 is module.
19289 2a 2a 20 61 6e 64 20 74 68 65 20 70 61 67 65 72 ** and the pager
1928a 20 63 6f 64 65 20 75 73 65 20 74 68 69 73 20 74 code use this t
1928b 72 69 63 6b 20 28 63 6c 65 61 72 69 6e 67 20 74 rick (clearing t
1928c 68 65 20 66 69 72 73 74 20 62 79 74 65 0a 20 20 he first byte.
1928d 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 70 61 ** of the pa
1928e 67 65 20 27 65 78 74 72 61 27 20 73 70 61 63 65 ge 'extra' space
1928f 20 74 6f 20 69 6e 76 61 6c 69 64 61 74 65 20 74 to invalidate t
19290 68 65 20 42 74 72 65 65 20 6c 61 79 65 72 73 0a he Btree layers.
19291 20 20 20 20 20 20 2a 2a 20 63 61 63 68 65 64 20 ** cached
19292 70 61 72 73 65 20 6f 66 20 74 68 65 20 70 61 67 parse of the pag
19293 65 29 2e 20 4d 65 6d 50 61 67 65 2e 69 73 49 6e e). MemPage.isIn
19294 69 74 20 69 73 20 6d 61 72 6b 65 64 20 0a 20 20 it is marked .
19295 20 20 20 20 2a 2a 20 22 4d 55 53 54 20 42 45 20 ** "MUST BE
19296 46 49 52 53 54 22 20 66 6f 72 20 74 68 69 73 20 FIRST" for this
19297 70 75 72 70 6f 73 65 2e 0a 20 20 20 20 20 20 2a purpose.. *
19298 2f 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 7a /. memcpy(z
19299 4f 75 74 2c 20 7a 49 6e 2c 20 6e 43 6f 70 79 29 Out, zIn, nCopy)
1929a 3b 0a 20 20 20 20 20 20 28 28 75 38 20 2a 29 73 ;. ((u8 *)s
1929b 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 45 78 qlite3PagerGetEx
1929c 74 72 61 28 70 44 65 73 74 50 67 29 29 5b 30 5d tra(pDestPg))[0]
1929d 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 = 0;. }.
1929e 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 sqlite3PagerUnre
1929f 66 28 70 44 65 73 74 50 67 29 3b 0a 20 20 7d 0a f(pDestPg);. }.
192a0 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
192a1 0a 2f 2a 0a 2a 2a 20 49 66 20 70 46 69 6c 65 20 ./*.** If pFile
192a2 69 73 20 63 75 72 72 65 6e 74 6c 79 20 6c 61 72 is currently lar
192a3 67 65 72 20 74 68 61 6e 20 69 53 69 7a 65 20 62 ger than iSize b
192a4 79 74 65 73 2c 20 74 68 65 6e 20 74 72 75 6e 63 ytes, then trunc
192a5 61 74 65 20 69 74 20 74 6f 0a 2a 2a 20 65 78 61 ate it to.** exa
192a6 63 74 6c 79 20 69 53 69 7a 65 20 62 79 74 65 73 ctly iSize bytes
192a7 2e 20 49 66 20 70 46 69 6c 65 20 69 73 20 6e 6f . If pFile is no
192a8 74 20 6c 61 72 67 65 72 20 74 68 61 6e 20 69 53 t larger than iS
192a9 69 7a 65 20 62 79 74 65 73 2c 20 74 68 65 6e 0a ize bytes, then.
192aa 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e ** this function
192ab 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a is a no-op..**.
192ac 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 ** Return SQLITE
192ad 5f 4f 4b 20 69 66 20 65 76 65 72 79 74 68 69 6e _OK if everythin
192ae 67 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 2c g is successful,
192af 20 6f 72 20 61 6e 20 53 51 4c 69 74 65 20 65 72 or an SQLite er
192b0 72 6f 72 20 0a 2a 2a 20 63 6f 64 65 20 69 66 20 ror .** code if
192b1 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e an error occurs.
192b2 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 .*/.static int b
192b3 61 63 6b 75 70 54 72 75 6e 63 61 74 65 46 69 6c ackupTruncateFil
192b4 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a e(sqlite3_file *
192b5 70 46 69 6c 65 2c 20 69 36 34 20 69 53 69 7a 65 pFile, i64 iSize
192b6 29 7b 0a 20 20 69 36 34 20 69 43 75 72 72 65 6e ){. i64 iCurren
192b7 74 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 73 71 t;. int rc = sq
192b8 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 lite3OsFileSize(
192b9 70 46 69 6c 65 2c 20 26 69 43 75 72 72 65 6e 74 pFile, &iCurrent
192ba 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c );. if( rc==SQL
192bb 49 54 45 5f 4f 4b 20 26 26 20 69 43 75 72 72 65 ITE_OK && iCurre
192bc 6e 74 3e 69 53 69 7a 65 20 29 7b 0a 20 20 20 20 nt>iSize ){.
192bd 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 54 72 rc = sqlite3OsTr
192be 75 6e 63 61 74 65 28 70 46 69 6c 65 2c 20 69 53 uncate(pFile, iS
192bf 69 7a 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 ize);. }. retu
192c0 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
192c1 52 65 67 69 73 74 65 72 20 74 68 69 73 20 62 61 Register this ba
192c2 63 6b 75 70 20 6f 62 6a 65 63 74 20 77 69 74 68 ckup object with
192c3 20 74 68 65 20 61 73 73 6f 63 69 61 74 65 64 20 the associated
192c4 73 6f 75 72 63 65 20 70 61 67 65 72 20 66 6f 72 source pager for
192c5 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 73 20 77 68 .** callbacks wh
192c6 65 6e 20 70 61 67 65 73 20 61 72 65 20 63 68 61 en pages are cha
192c7 6e 67 65 64 20 6f 72 20 74 68 65 20 63 61 63 68 nged or the cach
192c8 65 20 69 6e 76 61 6c 69 64 61 74 65 64 2e 0a 2a e invalidated..*
192c9 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 74 /.static void at
192ca 74 61 63 68 42 61 63 6b 75 70 4f 62 6a 65 63 74 tachBackupObject
192cb 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 (sqlite3_backup
192cc 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 62 *p){. sqlite3_b
192cd 61 63 6b 75 70 20 2a 2a 70 70 3b 0a 20 20 61 73 ackup **pp;. as
192ce 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 sert( sqlite3Btr
192cf 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 2d 3e eeHoldsMutex(p->
192d0 70 53 72 63 29 20 29 3b 0a 20 20 70 70 20 3d 20 pSrc) );. pp =
192d1 73 71 6c 69 74 65 33 50 61 67 65 72 42 61 63 6b sqlite3PagerBack
192d2 75 70 50 74 72 28 73 71 6c 69 74 65 33 42 74 72 upPtr(sqlite3Btr
192d3 65 65 50 61 67 65 72 28 70 2d 3e 70 53 72 63 29 eePager(p->pSrc)
192d4 29 3b 0a 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 );. p->pNext =
192d5 2a 70 70 3b 0a 20 20 2a 70 70 20 3d 20 70 3b 0a *pp;. *pp = p;.
192d6 20 20 70 2d 3e 69 73 41 74 74 61 63 68 65 64 20 p->isAttached
192d7 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f = 1;.}../*.** Co
192d8 70 79 20 6e 50 61 67 65 20 70 61 67 65 73 20 66 py nPage pages f
192d9 72 6f 6d 20 74 68 65 20 73 6f 75 72 63 65 20 62 rom the source b
192da 2d 74 72 65 65 20 74 6f 20 74 68 65 20 64 65 73 -tree to the des
192db 74 69 6e 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c tination..*/.SQL
192dc 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
192dd 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 te3_backup_step(
192de 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a sqlite3_backup *
192df 70 2c 20 69 6e 74 20 6e 50 61 67 65 29 7b 0a 20 p, int nPage){.
192e0 20 69 6e 74 20 72 63 3b 0a 0a 20 20 73 71 6c 69 int rc;.. sqli
192e1 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 te3_mutex_enter(
192e2 70 2d 3e 70 53 72 63 44 62 2d 3e 6d 75 74 65 78 p->pSrcDb->mutex
192e3 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 );. sqlite3Btre
192e4 65 45 6e 74 65 72 28 70 2d 3e 70 53 72 63 29 3b eEnter(p->pSrc);
192e5 0a 20 20 69 66 28 20 70 2d 3e 70 44 65 73 74 44 . if( p->pDestD
192e6 62 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 b ){. sqlite3
192e7 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e _mutex_enter(p->
192e8 70 44 65 73 74 44 62 2d 3e 6d 75 74 65 78 29 3b pDestDb->mutex);
192e9 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 70 2d 3e . }.. rc = p->
192ea 72 63 3b 0a 20 20 69 66 28 20 21 69 73 46 61 74 rc;. if( !isFat
192eb 61 6c 45 72 72 6f 72 28 72 63 29 20 29 7b 0a 20 alError(rc) ){.
192ec 20 20 20 50 61 67 65 72 20 2a 20 63 6f 6e 73 74 Pager * const
192ed 20 70 53 72 63 50 61 67 65 72 20 3d 20 73 71 6c pSrcPager = sql
192ee 69 74 65 33 42 74 72 65 65 50 61 67 65 72 28 70 ite3BtreePager(p
192ef 2d 3e 70 53 72 63 29 3b 20 20 20 20 20 2f 2a 20 ->pSrc); /*
192f0 53 6f 75 72 63 65 20 70 61 67 65 72 20 2a 2f 0a Source pager */.
192f1 20 20 20 20 50 61 67 65 72 20 2a 20 63 6f 6e 73 Pager * cons
192f2 74 20 70 44 65 73 74 50 61 67 65 72 20 3d 20 73 t pDestPager = s
192f3 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 72 qlite3BtreePager
192f4 28 70 2d 3e 70 44 65 73 74 29 3b 20 20 20 2f 2a (p->pDest); /*
192f5 20 44 65 73 74 20 70 61 67 65 72 20 2a 2f 0a 20 Dest pager */.
192f6 20 20 20 69 6e 74 20 69 69 3b 20 20 20 20 20 20 int ii;
192f7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
192f8 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f /* Iterato
192f9 72 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20 r variable */.
192fa 20 20 69 6e 74 20 6e 53 72 63 50 61 67 65 20 3d int nSrcPage =
192fb 20 2d 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 -1;
192fc 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 /* Size of
192fd 73 6f 75 72 63 65 20 64 62 20 69 6e 20 70 61 67 source db in pag
192fe 65 73 20 2a 2f 0a 20 20 20 20 69 6e 74 20 62 43 es */. int bC
192ff 6c 6f 73 65 54 72 61 6e 73 20 3d 20 30 3b 20 20 loseTrans = 0;
19300 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
19301 54 72 75 65 20 69 66 20 73 72 63 20 64 62 20 72 True if src db r
19302 65 71 75 69 72 65 73 20 75 6e 6c 6f 63 6b 69 6e equires unlockin
19303 67 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 49 66 20 g */.. /* If
19304 74 68 65 20 73 6f 75 72 63 65 20 70 61 67 65 72 the source pager
19305 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e is currently in
19306 20 61 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 a write-transac
19307 74 69 6f 6e 2c 20 72 65 74 75 72 6e 0a 20 20 20 tion, return.
19308 20 2a 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 20 ** SQLITE_BUSY
19309 69 6d 6d 65 64 69 61 74 65 6c 79 2e 0a 20 20 20 immediately..
1930a 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 */. if( p->p
1930b 44 65 73 74 44 62 20 26 26 20 70 2d 3e 70 53 72 DestDb && p->pSr
1930c 63 2d 3e 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 c->pBt->inTransa
1930d 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 ction==TRANS_WRI
1930e 54 45 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d TE ){. rc =
1930f 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 SQLITE_BUSY;.
19310 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 }else{. r
19311 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
19312 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 4c 6f 63 }.. /* Loc
19313 6b 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f k the destinatio
19314 6e 20 64 61 74 61 62 61 73 65 2c 20 69 66 20 69 n database, if i
19315 74 20 69 73 20 6e 6f 74 20 6c 6f 63 6b 65 64 20 t is not locked
19316 61 6c 72 65 61 64 79 2e 20 2a 2f 0a 20 20 20 20 already. */.
19317 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 72 if( SQLITE_OK==r
19318 63 20 26 26 20 70 2d 3e 62 44 65 73 74 4c 6f 63 c && p->bDestLoc
19319 6b 65 64 3d 3d 30 0a 20 20 20 20 20 26 26 20 53 ked==0. && S
1931a 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 QLITE_OK==(rc =
1931b 73 71 6c 69 74 65 33 42 74 72 65 65 42 65 67 69 sqlite3BtreeBegi
1931c 6e 54 72 61 6e 73 28 70 2d 3e 70 44 65 73 74 2c nTrans(p->pDest,
1931d 20 32 29 29 20 0a 20 20 20 20 29 7b 0a 20 20 20 2)) . ){.
1931e 20 20 20 70 2d 3e 62 44 65 73 74 4c 6f 63 6b 65 p->bDestLocke
1931f 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 73 71 6c d = 1;. sql
19320 69 74 65 33 42 74 72 65 65 47 65 74 4d 65 74 61 ite3BtreeGetMeta
19321 28 70 2d 3e 70 44 65 73 74 2c 20 42 54 52 45 45 (p->pDest, BTREE
19322 5f 53 43 48 45 4d 41 5f 56 45 52 53 49 4f 4e 2c _SCHEMA_VERSION,
19323 20 26 70 2d 3e 69 44 65 73 74 53 63 68 65 6d 61 &p->iDestSchema
19324 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a );. }.. /*
19325 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 If there is no
19326 6f 70 65 6e 20 72 65 61 64 2d 74 72 61 6e 73 61 open read-transa
19327 63 74 69 6f 6e 20 6f 6e 20 74 68 65 20 73 6f 75 ction on the sou
19328 72 63 65 20 64 61 74 61 62 61 73 65 2c 20 6f 70 rce database, op
19329 65 6e 0a 20 20 20 20 2a 2a 20 6f 6e 65 20 6e 6f en. ** one no
1932a 77 2e 20 49 66 20 61 20 74 72 61 6e 73 61 63 74 w. If a transact
1932b 69 6f 6e 20 69 73 20 6f 70 65 6e 65 64 20 68 65 ion is opened he
1932c 72 65 2c 20 74 68 65 6e 20 69 74 20 77 69 6c 6c re, then it will
1932d 20 62 65 20 63 6c 6f 73 65 64 0a 20 20 20 20 2a be closed. *
1932e 2a 20 62 65 66 6f 72 65 20 74 68 69 73 20 66 75 * before this fu
1932f 6e 63 74 69 6f 6e 20 65 78 69 74 73 2e 0a 20 20 nction exits..
19330 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d */. if( rc=
19331 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 30 3d =SQLITE_OK && 0=
19332 3d 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 =sqlite3BtreeIsI
19333 6e 52 65 61 64 54 72 61 6e 73 28 70 2d 3e 70 53 nReadTrans(p->pS
19334 72 63 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20 rc) ){. rc
19335 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 42 65 = sqlite3BtreeBe
19336 67 69 6e 54 72 61 6e 73 28 70 2d 3e 70 53 72 63 ginTrans(p->pSrc
19337 2c 20 30 29 3b 0a 20 20 20 20 20 20 62 43 6c 6f , 0);. bClo
19338 73 65 54 72 61 6e 73 20 3d 20 31 3b 0a 20 20 20 seTrans = 1;.
19339 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 4e 6f 77 }. . /* Now
1933a 20 74 68 61 74 20 74 68 65 72 65 20 69 73 20 61 that there is a
1933b 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 74 68 read-lock on th
1933c 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 e source databas
1933d 65 2c 20 71 75 65 72 79 20 74 68 65 0a 20 20 20 e, query the.
1933e 20 2a 2a 20 73 6f 75 72 63 65 20 70 61 67 65 72 ** source pager
1933f 20 66 6f 72 20 74 68 65 20 6e 75 6d 62 65 72 20 for the number
19340 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 of pages in the
19341 64 61 74 61 62 61 73 65 2e 0a 20 20 20 20 2a 2f database.. */
19342 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c . if( rc==SQL
19343 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
19344 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
19345 72 50 61 67 65 63 6f 75 6e 74 28 70 53 72 63 50 rPagecount(pSrcP
19346 61 67 65 72 2c 20 26 6e 53 72 63 50 61 67 65 29 ager, &nSrcPage)
19347 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 ;. }. for(
19348 69 69 3d 30 3b 20 28 6e 50 61 67 65 3c 30 20 7c ii=0; (nPage<0 |
19349 7c 20 69 69 3c 6e 50 61 67 65 29 20 26 26 20 70 | ii<nPage) && p
1934a 2d 3e 69 4e 65 78 74 3c 3d 28 50 67 6e 6f 29 6e ->iNext<=(Pgno)n
1934b 53 72 63 50 61 67 65 20 26 26 20 21 72 63 3b 20 SrcPage && !rc;
1934c 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 63 6f 6e ii++){. con
1934d 73 74 20 50 67 6e 6f 20 69 53 72 63 50 67 20 3d st Pgno iSrcPg =
1934e 20 70 2d 3e 69 4e 65 78 74 3b 20 20 20 20 20 20 p->iNext;
1934f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 6f /* So
19350 75 72 63 65 20 70 61 67 65 20 6e 75 6d 62 65 72 urce page number
19351 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 69 53 */. if( iS
19352 72 63 50 67 21 3d 50 45 4e 44 49 4e 47 5f 42 59 rcPg!=PENDING_BY
19353 54 45 5f 50 41 47 45 28 70 2d 3e 70 53 72 63 2d TE_PAGE(p->pSrc-
19354 3e 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 20 >pBt) ){.
19355 20 44 62 50 61 67 65 20 2a 70 53 72 63 50 67 3b DbPage *pSrcPg;
19356 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19357 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
19358 53 6f 75 72 63 65 20 70 61 67 65 20 6f 62 6a 65 Source page obje
19359 63 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 63 ct */. rc
1935a 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 = sqlite3PagerG
1935b 65 74 28 70 53 72 63 50 61 67 65 72 2c 20 69 53 et(pSrcPager, iS
1935c 72 63 50 67 2c 20 26 70 53 72 63 50 67 29 3b 0a rcPg, &pSrcPg);.
1935d 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d if( rc==
1935e 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
1935f 20 20 20 20 20 20 20 72 63 20 3d 20 62 61 63 6b rc = back
19360 75 70 4f 6e 65 50 61 67 65 28 70 2c 20 69 53 72 upOnePage(p, iSr
19361 63 50 67 2c 20 73 71 6c 69 74 65 33 50 61 67 65 cPg, sqlite3Page
19362 72 47 65 74 44 61 74 61 28 70 53 72 63 50 67 29 rGetData(pSrcPg)
19363 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c );. sql
19364 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 ite3PagerUnref(p
19365 53 72 63 50 67 29 3b 0a 20 20 20 20 20 20 20 20 SrcPg);.
19366 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }. }.
19367 70 2d 3e 69 4e 65 78 74 2b 2b 3b 0a 20 20 20 20 p->iNext++;.
19368 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 }. if( rc==SQ
19369 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
1936a 20 70 2d 3e 6e 50 61 67 65 63 6f 75 6e 74 20 3d p->nPagecount =
1936b 20 6e 53 72 63 50 61 67 65 3b 0a 20 20 20 20 20 nSrcPage;.
1936c 20 70 2d 3e 6e 52 65 6d 61 69 6e 69 6e 67 20 3d p->nRemaining =
1936d 20 6e 53 72 63 50 61 67 65 2b 31 2d 70 2d 3e 69 nSrcPage+1-p->i
1936e 4e 65 78 74 3b 0a 20 20 20 20 20 20 69 66 28 20 Next;. if(
1936f 70 2d 3e 69 4e 65 78 74 3e 28 50 67 6e 6f 29 6e p->iNext>(Pgno)n
19370 53 72 63 50 61 67 65 20 29 7b 0a 20 20 20 20 20 SrcPage ){.
19371 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 44 rc = SQLITE_D
19372 4f 4e 45 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 ONE;. }else
19373 20 69 66 28 20 21 70 2d 3e 69 73 41 74 74 61 63 if( !p->isAttac
19374 68 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20 61 hed ){. a
19375 74 74 61 63 68 42 61 63 6b 75 70 4f 62 6a 65 63 ttachBackupObjec
19376 74 28 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 t(p);. }.
19377 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 55 70 }. . /* Up
19378 64 61 74 65 20 74 68 65 20 73 63 68 65 6d 61 20 date the schema
19379 76 65 72 73 69 6f 6e 20 66 69 65 6c 64 20 69 6e version field in
1937a 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e the destination
1937b 20 64 61 74 61 62 61 73 65 2e 20 54 68 69 73 0a database. This.
1937c 20 20 20 20 2a 2a 20 69 73 20 74 6f 20 6d 61 6b ** is to mak
1937d 65 20 73 75 72 65 20 74 68 61 74 20 74 68 65 20 e sure that the
1937e 73 63 68 65 6d 61 2d 76 65 72 73 69 6f 6e 20 72 schema-version r
1937f 65 61 6c 6c 79 20 64 6f 65 73 20 63 68 61 6e 67 eally does chang
19380 65 20 69 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20 e in. ** the
19381 63 61 73 65 20 77 68 65 72 65 20 74 68 65 20 73 case where the s
19382 6f 75 72 63 65 20 61 6e 64 20 64 65 73 74 69 6e ource and destin
19383 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 73 20 ation databases
19384 68 61 76 65 20 74 68 65 0a 20 20 20 20 2a 2a 20 have the. **
19385 73 61 6d 65 20 73 63 68 65 6d 61 20 76 65 72 73 same schema vers
19386 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ion.. */.
19387 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 if( rc==SQLITE_D
19388 4f 4e 45 20 0a 20 20 20 20 20 26 26 20 28 72 63 ONE . && (rc
19389 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 55 = sqlite3BtreeU
1938a 70 64 61 74 65 4d 65 74 61 28 70 2d 3e 70 44 65 pdateMeta(p->pDe
1938b 73 74 2c 31 2c 70 2d 3e 69 44 65 73 74 53 63 68 st,1,p->iDestSch
1938c 65 6d 61 2b 31 29 29 3d 3d 53 51 4c 49 54 45 5f ema+1))==SQLITE_
1938d 4f 4b 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 OK. ){.
1938e 63 6f 6e 73 74 20 69 6e 74 20 6e 53 72 63 50 61 const int nSrcPa
1938f 67 65 73 69 7a 65 20 3d 20 73 71 6c 69 74 65 33 gesize = sqlite3
19390 42 74 72 65 65 47 65 74 50 61 67 65 53 69 7a 65 BtreeGetPageSize
19391 28 70 2d 3e 70 53 72 63 29 3b 0a 20 20 20 20 20 (p->pSrc);.
19392 20 63 6f 6e 73 74 20 69 6e 74 20 6e 44 65 73 74 const int nDest
19393 50 61 67 65 73 69 7a 65 20 3d 20 73 71 6c 69 74 Pagesize = sqlit
19394 65 33 42 74 72 65 65 47 65 74 50 61 67 65 53 69 e3BtreeGetPageSi
19395 7a 65 28 70 2d 3e 70 44 65 73 74 29 3b 0a 20 20 ze(p->pDest);.
19396 20 20 20 20 69 6e 74 20 6e 44 65 73 74 54 72 75 int nDestTru
19397 6e 63 61 74 65 3b 0a 20 20 0a 20 20 20 20 20 20 ncate;. .
19398 69 66 28 20 70 2d 3e 70 44 65 73 74 44 62 20 29 if( p->pDestDb )
19399 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
1939a 33 52 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 3ResetInternalSc
1939b 68 65 6d 61 28 70 2d 3e 70 44 65 73 74 44 62 2c hema(p->pDestDb,
1939c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 0);. }..
1939d 20 20 20 20 2f 2a 20 53 65 74 20 6e 44 65 73 74 /* Set nDest
1939e 54 72 75 6e 63 61 74 65 20 74 6f 20 74 68 65 20 Truncate to the
1939f 66 69 6e 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 final number of
193a0 70 61 67 65 73 20 69 6e 20 74 68 65 20 64 65 73 pages in the des
193a1 74 69 6e 61 74 69 6f 6e 0a 20 20 20 20 20 20 2a tination. *
193a2 2a 20 64 61 74 61 62 61 73 65 2e 20 54 68 65 20 * database. The
193a3 63 6f 6d 70 6c 69 63 61 74 69 6f 6e 20 68 65 72 complication her
193a4 65 20 69 73 20 74 68 61 74 20 74 68 65 20 64 65 e is that the de
193a5 73 74 69 6e 61 74 69 6f 6e 20 70 61 67 65 0a 20 stination page.
193a6 20 20 20 20 20 2a 2a 20 73 69 7a 65 20 6d 61 79 ** size may
193a7 20 62 65 20 64 69 66 66 65 72 65 6e 74 20 74 6f be different to
193a8 20 74 68 65 20 73 6f 75 72 63 65 20 70 61 67 65 the source page
193a9 20 73 69 7a 65 2e 20 0a 20 20 20 20 20 20 2a 2a size. . **
193aa 0a 20 20 20 20 20 20 2a 2a 20 49 66 20 74 68 65 . ** If the
193ab 20 73 6f 75 72 63 65 20 70 61 67 65 20 73 69 7a source page siz
193ac 65 20 69 73 20 73 6d 61 6c 6c 65 72 20 74 68 61 e is smaller tha
193ad 6e 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f n the destinatio
193ae 6e 20 70 61 67 65 20 73 69 7a 65 2c 20 0a 20 20 n page size, .
193af 20 20 20 20 2a 2a 20 72 6f 75 6e 64 20 75 70 2e ** round up.
193b0 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 In this case th
193b1 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 e call to sqlite
193b2 33 4f 73 54 72 75 6e 63 61 74 65 28 29 20 62 65 3OsTruncate() be
193b3 6c 6f 77 20 77 69 6c 6c 0a 20 20 20 20 20 20 2a low will. *
193b4 2a 20 66 69 78 20 74 68 65 20 73 69 7a 65 20 6f * fix the size o
193b5 66 20 74 68 65 20 66 69 6c 65 2e 20 48 6f 77 65 f the file. Howe
193b6 76 65 72 20 69 74 20 69 73 20 69 6d 70 6f 72 74 ver it is import
193b7 61 6e 74 20 74 6f 20 63 61 6c 6c 0a 20 20 20 20 ant to call.
193b8 20 20 2a 2a 20 73 71 6c 69 74 65 33 50 61 67 65 ** sqlite3Page
193b9 72 54 72 75 6e 63 61 74 65 49 6d 61 67 65 28 29 rTruncateImage()
193ba 20 68 65 72 65 20 73 6f 20 74 68 61 74 20 61 6e here so that an
193bb 79 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 0a y pages in the .
193bc 20 20 20 20 20 20 2a 2a 20 64 65 73 74 69 6e 61 ** destina
193bd 74 69 6f 6e 20 66 69 6c 65 20 74 68 61 74 20 6c tion file that l
193be 69 65 20 62 65 79 6f 6e 64 20 74 68 65 20 6e 44 ie beyond the nD
193bf 65 73 74 54 72 75 6e 63 61 74 65 20 70 61 67 65 estTruncate page
193c0 20 6d 61 72 6b 20 61 72 65 0a 20 20 20 20 20 20 mark are.
193c1 2a 2a 20 6a 6f 75 72 6e 61 6c 6c 65 64 20 62 79 ** journalled by
193c2 20 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 PagerCommitPhas
193c3 65 4f 6e 65 28 29 20 62 65 66 6f 72 65 20 74 68 eOne() before th
193c4 65 79 20 61 72 65 20 64 65 73 74 72 6f 79 65 64 ey are destroyed
193c5 0a 20 20 20 20 20 20 2a 2a 20 62 79 20 74 68 65 . ** by the
193c6 20 66 69 6c 65 20 74 72 75 6e 63 61 74 69 6f 6e file truncation
193c7 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 .. */.
193c8 20 69 66 28 20 6e 53 72 63 50 61 67 65 73 69 7a if( nSrcPagesiz
193c9 65 3c 6e 44 65 73 74 50 61 67 65 73 69 7a 65 20 e<nDestPagesize
193ca 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 72 ){. int r
193cb 61 74 69 6f 20 3d 20 6e 44 65 73 74 50 61 67 65 atio = nDestPage
193cc 73 69 7a 65 2f 6e 53 72 63 50 61 67 65 73 69 7a size/nSrcPagesiz
193cd 65 3b 0a 20 20 20 20 20 20 20 20 6e 44 65 73 74 e;. nDest
193ce 54 72 75 6e 63 61 74 65 20 3d 20 28 6e 53 72 63 Truncate = (nSrc
193cf 50 61 67 65 2b 72 61 74 69 6f 2d 31 29 2f 72 61 Page+ratio-1)/ra
193d0 74 69 6f 3b 0a 20 20 20 20 20 20 20 20 69 66 28 tio;. if(
193d1 20 6e 44 65 73 74 54 72 75 6e 63 61 74 65 3d 3d nDestTruncate==
193d2 28 69 6e 74 29 50 45 4e 44 49 4e 47 5f 42 59 54 (int)PENDING_BYT
193d3 45 5f 50 41 47 45 28 70 2d 3e 70 44 65 73 74 2d E_PAGE(p->pDest-
193d4 3e 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 20 >pBt) ){.
193d5 20 20 20 6e 44 65 73 74 54 72 75 6e 63 61 74 65 nDestTruncate
193d6 2d 2d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 --;. }.
193d7 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
193d8 20 20 20 6e 44 65 73 74 54 72 75 6e 63 61 74 65 nDestTruncate
193d9 20 3d 20 6e 53 72 63 50 61 67 65 20 2a 20 28 6e = nSrcPage * (n
193da 53 72 63 50 61 67 65 73 69 7a 65 2f 6e 44 65 73 SrcPagesize/nDes
193db 74 50 61 67 65 73 69 7a 65 29 3b 0a 20 20 20 20 tPagesize);.
193dc 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 }. sqlite
193dd 33 50 61 67 65 72 54 72 75 6e 63 61 74 65 49 6d 3PagerTruncateIm
193de 61 67 65 28 70 44 65 73 74 50 61 67 65 72 2c 20 age(pDestPager,
193df 6e 44 65 73 74 54 72 75 6e 63 61 74 65 29 3b 0a nDestTruncate);.
193e0 0a 20 20 20 20 20 20 69 66 28 20 6e 53 72 63 50 . if( nSrcP
193e1 61 67 65 73 69 7a 65 3c 6e 44 65 73 74 50 61 67 agesize<nDestPag
193e2 65 73 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 esize ){.
193e3 20 2f 2a 20 49 66 20 74 68 65 20 73 6f 75 72 63 /* If the sourc
193e4 65 20 70 61 67 65 2d 73 69 7a 65 20 69 73 20 73 e page-size is s
193e5 6d 61 6c 6c 65 72 20 74 68 61 6e 20 74 68 65 20 maller than the
193e6 64 65 73 74 69 6e 61 74 69 6f 6e 20 70 61 67 65 destination page
193e7 2d 73 69 7a 65 2c 0a 20 20 20 20 20 20 20 20 2a -size,. *
193e8 2a 20 74 77 6f 20 65 78 74 72 61 20 74 68 69 6e * two extra thin
193e9 67 73 20 6d 61 79 20 6e 65 65 64 20 74 6f 20 68 gs may need to h
193ea 61 70 70 65 6e 3a 0a 20 20 20 20 20 20 20 20 2a appen:. *
193eb 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 20 20 2a *. ** *
193ec 20 54 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e The destination
193ed 20 6d 61 79 20 6e 65 65 64 20 74 6f 20 62 65 20 may need to be
193ee 74 72 75 6e 63 61 74 65 64 2c 20 61 6e 64 0a 20 truncated, and.
193ef 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 **.
193f0 20 20 2a 2a 20 20 20 2a 20 44 61 74 61 20 73 74 ** * Data st
193f1 6f 72 65 64 20 6f 6e 20 74 68 65 20 70 61 67 65 ored on the page
193f2 73 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 66 6f s immediately fo
193f3 6c 6c 6f 77 69 6e 67 20 74 68 65 20 0a 20 20 20 llowing the .
193f4 20 20 20 20 20 2a 2a 20 20 20 20 20 70 65 6e 64 ** pend
193f5 69 6e 67 2d 62 79 74 65 20 70 61 67 65 20 69 6e ing-byte page in
193f6 20 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 the source data
193f7 62 61 73 65 20 6d 61 79 20 6e 65 65 64 20 74 6f base may need to
193f8 20 62 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 20 be. **
193f9 20 20 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 74 copied into t
193fa 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 he destination d
193fb 61 74 61 62 61 73 65 2e 0a 20 20 20 20 20 20 20 atabase..
193fc 20 2a 2f 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 */. cons
193fd 74 20 69 36 34 20 69 53 69 7a 65 20 3d 20 28 69 t i64 iSize = (i
193fe 36 34 29 6e 53 72 63 50 61 67 65 73 69 7a 65 20 64)nSrcPagesize
193ff 2a 20 28 69 36 34 29 6e 53 72 63 50 61 67 65 3b * (i64)nSrcPage;
19400 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
19401 5f 66 69 6c 65 20 2a 20 63 6f 6e 73 74 20 70 46 _file * const pF
19402 69 6c 65 20 3d 20 73 71 6c 69 74 65 33 50 61 67 ile = sqlite3Pag
19403 65 72 46 69 6c 65 28 70 44 65 73 74 50 61 67 65 erFile(pDestPage
19404 72 29 3b 0a 0a 20 20 20 20 20 20 20 20 61 73 73 r);.. ass
19405 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 ert( pFile );.
19406 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28 69 assert( (i
19407 36 34 29 6e 44 65 73 74 54 72 75 6e 63 61 74 65 64)nDestTruncate
19408 2a 28 69 36 34 29 6e 44 65 73 74 50 61 67 65 73 *(i64)nDestPages
19409 69 7a 65 20 3e 3d 20 69 53 69 7a 65 20 7c 7c 20 ize >= iSize ||
1940a 28 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 (.
1940b 6e 44 65 73 74 54 72 75 6e 63 61 74 65 3d 3d 28 nDestTruncate==(
1940c 69 6e 74 29 28 50 45 4e 44 49 4e 47 5f 42 59 54 int)(PENDING_BYT
1940d 45 5f 50 41 47 45 28 70 2d 3e 70 44 65 73 74 2d E_PAGE(p->pDest-
1940e 3e 70 42 74 29 2d 31 29 0a 20 20 20 20 20 20 20 >pBt)-1).
1940f 20 20 20 20 26 26 20 69 53 69 7a 65 3e 3d 50 45 && iSize>=PE
19410 4e 44 49 4e 47 5f 42 59 54 45 20 26 26 20 69 53 NDING_BYTE && iS
19411 69 7a 65 3c 3d 50 45 4e 44 49 4e 47 5f 42 59 54 ize<=PENDING_BYT
19412 45 2b 6e 44 65 73 74 50 61 67 65 73 69 7a 65 0a E+nDestPagesize.
19413 20 20 20 20 20 20 20 20 29 29 3b 0a 20 20 20 20 ));.
19414 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f if( SQLITE_O
19415 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33 K==(rc = sqlite3
19416 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 PagerCommitPhase
19417 4f 6e 65 28 70 44 65 73 74 50 61 67 65 72 2c 20 One(pDestPager,
19418 30 2c 20 31 29 29 0a 20 20 20 20 20 20 20 20 20 0, 1)).
19419 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 && SQLITE_OK==(r
1941a 63 20 3d 20 62 61 63 6b 75 70 54 72 75 6e 63 61 c = backupTrunca
1941b 74 65 46 69 6c 65 28 70 46 69 6c 65 2c 20 69 53 teFile(pFile, iS
1941c 69 7a 65 29 29 0a 20 20 20 20 20 20 20 20 20 26 ize)). &
1941d 26 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 & SQLITE_OK==(rc
1941e 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 = sqlite3PagerS
1941f 79 6e 63 28 70 44 65 73 74 50 61 67 65 72 29 29 ync(pDestPager))
19420 0a 20 20 20 20 20 20 20 20 29 7b 0a 20 20 20 20 . ){.
19421 20 20 20 20 20 20 69 36 34 20 69 4f 66 66 3b 0a i64 iOff;.
19422 20 20 20 20 20 20 20 20 20 20 69 36 34 20 69 45 i64 iE
19423 6e 64 20 3d 20 4d 49 4e 28 50 45 4e 44 49 4e 47 nd = MIN(PENDING
19424 5f 42 59 54 45 20 2b 20 6e 44 65 73 74 50 61 67 _BYTE + nDestPag
19425 65 73 69 7a 65 2c 20 69 53 69 7a 65 29 3b 0a 20 esize, iSize);.
19426 20 20 20 20 20 20 20 20 20 66 6f 72 28 0a 20 20 for(.
19427 20 20 20 20 20 20 20 20 20 20 69 4f 66 66 3d 50 iOff=P
19428 45 4e 44 49 4e 47 5f 42 59 54 45 2b 6e 53 72 63 ENDING_BYTE+nSrc
19429 50 61 67 65 73 69 7a 65 3b 20 0a 20 20 20 20 20 Pagesize; .
1942a 20 20 20 20 20 20 20 72 63 3d 3d 53 51 4c 49 54 rc==SQLIT
1942b 45 5f 4f 4b 20 26 26 20 69 4f 66 66 3c 69 45 6e E_OK && iOff<iEn
1942c 64 3b 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 d; .
1942d 69 4f 66 66 2b 3d 6e 53 72 63 50 61 67 65 73 69 iOff+=nSrcPagesi
1942e 7a 65 0a 20 20 20 20 20 20 20 20 20 20 29 7b 0a ze. ){.
1942f 20 20 20 20 20 20 20 20 20 20 20 20 50 67 48 64 PgHd
19430 72 20 2a 70 53 72 63 50 67 20 3d 20 30 3b 0a 20 r *pSrcPg = 0;.
19431 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 const
19432 20 50 67 6e 6f 20 69 53 72 63 50 67 20 3d 20 28 Pgno iSrcPg = (
19433 50 67 6e 6f 29 28 28 69 4f 66 66 2f 6e 53 72 63 Pgno)((iOff/nSrc
19434 50 61 67 65 73 69 7a 65 29 2b 31 29 3b 0a 20 20 Pagesize)+1);.
19435 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 rc = s
19436 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 70 qlite3PagerGet(p
19437 53 72 63 50 61 67 65 72 2c 20 69 53 72 63 50 67 SrcPager, iSrcPg
19438 2c 20 26 70 53 72 63 50 67 29 3b 0a 20 20 20 20 , &pSrcPg);.
19439 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d if( rc==
1943a 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
1943b 20 20 20 20 20 20 20 20 20 20 20 75 38 20 2a 7a u8 *z
1943c 44 61 74 61 20 3d 20 73 71 6c 69 74 65 33 50 61 Data = sqlite3Pa
1943d 67 65 72 47 65 74 44 61 74 61 28 70 53 72 63 50 gerGetData(pSrcP
1943e 67 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 g);.
1943f 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
19440 57 72 69 74 65 28 70 46 69 6c 65 2c 20 7a 44 61 Write(pFile, zDa
19441 74 61 2c 20 6e 53 72 63 50 61 67 65 73 69 7a 65 ta, nSrcPagesize
19442 2c 20 69 4f 66 66 29 3b 0a 20 20 20 20 20 20 20 , iOff);.
19443 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 }.
19444 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 sqlite3PagerU
19445 6e 72 65 66 28 70 53 72 63 50 67 29 3b 0a 20 20 nref(pSrcPg);.
19446 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
19447 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b }. }else{
19448 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 . rc = sq
19449 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 lite3PagerCommit
1944a 50 68 61 73 65 4f 6e 65 28 70 44 65 73 74 50 61 PhaseOne(pDestPa
1944b 67 65 72 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 ger, 0, 0);.
1944c 20 20 7d 0a 20 20 0a 20 20 20 20 20 20 2f 2a 20 }. . /*
1944d 46 69 6e 69 73 68 20 63 6f 6d 6d 69 74 74 69 6e Finish committin
1944e 67 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f g the transactio
1944f 6e 20 74 6f 20 74 68 65 20 64 65 73 74 69 6e 61 n to the destina
19450 74 69 6f 6e 20 64 61 74 61 62 61 73 65 2e 20 2a tion database. *
19451 2f 0a 20 20 20 20 20 20 69 66 28 20 53 51 4c 49 /. if( SQLI
19452 54 45 5f 4f 4b 3d 3d 72 63 0a 20 20 20 20 20 20 TE_OK==rc.
19453 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 && SQLITE_OK==(
19454 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 rc = sqlite3Btre
19455 65 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 eCommitPhaseTwo(
19456 70 2d 3e 70 44 65 73 74 29 29 0a 20 20 20 20 20 p->pDest)).
19457 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d ){. rc =
19458 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 SQLITE_DONE;.
19459 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 }. }. .
1945a 20 20 20 2f 2a 20 49 66 20 62 43 6c 6f 73 65 54 /* If bCloseT
1945b 72 61 6e 73 20 69 73 20 74 72 75 65 2c 20 74 68 rans is true, th
1945c 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e en this function
1945d 20 6f 70 65 6e 65 64 20 61 20 72 65 61 64 20 74 opened a read t
1945e 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20 20 20 2a ransaction. *
1945f 2a 20 6f 6e 20 74 68 65 20 73 6f 75 72 63 65 20 * on the source
19460 64 61 74 61 62 61 73 65 2e 20 43 6c 6f 73 65 20 database. Close
19461 74 68 65 20 72 65 61 64 20 74 72 61 6e 73 61 63 the read transac
19462 74 69 6f 6e 20 68 65 72 65 2e 20 54 68 65 72 65 tion here. There
19463 20 69 73 0a 20 20 20 20 2a 2a 20 6e 6f 20 6e 65 is. ** no ne
19464 65 64 20 74 6f 20 63 68 65 63 6b 20 74 68 65 20 ed to check the
19465 72 65 74 75 72 6e 20 76 61 6c 75 65 73 20 6f 66 return values of
19466 20 74 68 65 20 62 74 72 65 65 20 6d 65 74 68 6f the btree metho
19467 64 73 20 68 65 72 65 2c 20 61 73 0a 20 20 20 20 ds here, as.
19468 2a 2a 20 22 63 6f 6d 6d 69 74 74 69 6e 67 22 20 ** "committing"
19469 61 20 72 65 61 64 2d 6f 6e 6c 79 20 74 72 61 6e a read-only tran
1946a 73 61 63 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 66 saction cannot f
1946b 61 69 6c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ail.. */.
1946c 69 66 28 20 62 43 6c 6f 73 65 54 72 61 6e 73 20 if( bCloseTrans
1946d 29 7b 0a 20 20 20 20 20 20 54 45 53 54 4f 4e 4c ){. TESTONL
1946e 59 28 20 69 6e 74 20 72 63 32 20 29 3b 0a 20 20 Y( int rc2 );.
1946f 20 20 20 20 54 45 53 54 4f 4e 4c 59 28 20 72 63 TESTONLY( rc
19470 32 20 20 3d 20 29 20 73 71 6c 69 74 65 33 42 74 2 = ) sqlite3Bt
19471 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e reeCommitPhaseOn
19472 65 28 70 2d 3e 70 53 72 63 2c 20 30 29 3b 0a 20 e(p->pSrc, 0);.
19473 20 20 20 20 20 54 45 53 54 4f 4e 4c 59 28 20 72 TESTONLY( r
19474 63 32 20 7c 3d 20 29 20 73 71 6c 69 74 65 33 42 c2 |= ) sqlite3B
19475 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 54 treeCommitPhaseT
19476 77 6f 28 70 2d 3e 70 53 72 63 29 3b 0a 20 20 20 wo(p->pSrc);.
19477 20 20 20 61 73 73 65 72 74 28 20 72 63 32 3d 3d assert( rc2==
19478 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 SQLITE_OK );.
19479 20 7d 0a 20 20 0a 20 20 20 20 70 2d 3e 72 63 20 }. . p->rc
1947a 3d 20 72 63 3b 0a 20 20 7d 0a 20 20 69 66 28 20 = rc;. }. if(
1947b 70 2d 3e 70 44 65 73 74 44 62 20 29 7b 0a 20 20 p->pDestDb ){.
1947c 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
1947d 6c 65 61 76 65 28 70 2d 3e 70 44 65 73 74 44 62 leave(p->pDestDb
1947e 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 ->mutex);. }.
1947f 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 sqlite3BtreeLeav
19480 65 28 70 2d 3e 70 53 72 63 29 3b 0a 20 20 73 71 e(p->pSrc);. sq
19481 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
19482 65 28 70 2d 3e 70 53 72 63 44 62 2d 3e 6d 75 74 e(p->pSrcDb->mut
19483 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 ex);. return rc
19484 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 ;.}../*.** Relea
19485 73 65 20 61 6c 6c 20 72 65 73 6f 75 72 63 65 73 se all resources
19486 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
19487 20 61 6e 20 73 71 6c 69 74 65 33 5f 62 61 63 6b an sqlite3_back
19488 75 70 2a 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 53 up* handle..*/.S
19489 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
1948a 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e lite3_backup_fin
1948b 69 73 68 28 73 71 6c 69 74 65 33 5f 62 61 63 6b ish(sqlite3_back
1948c 75 70 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 up *p){. sqlite
1948d 33 5f 62 61 63 6b 75 70 20 2a 2a 70 70 3b 20 20 3_backup **pp;
1948e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1948f 2a 20 50 74 72 20 74 6f 20 68 65 61 64 20 6f 66 * Ptr to head of
19490 20 70 61 67 65 72 73 20 62 61 63 6b 75 70 20 6c pagers backup l
19491 69 73 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 ist */. sqlite3
19492 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b 20 20 _mutex *mutex;
19493 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
19494 20 4d 75 74 65 78 20 74 6f 20 70 72 6f 74 65 63 Mutex to protec
19495 74 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 t source databas
19496 65 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 e */. int rc;
19497 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19498 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 /* V
19499 61 6c 75 65 20 74 6f 20 72 65 74 75 72 6e 20 2a alue to return *
1949a 2f 0a 0a 20 20 2f 2a 20 45 6e 74 65 72 20 74 68 /.. /* Enter th
1949b 65 20 6d 75 74 65 78 65 73 20 2a 2f 0a 20 20 69 e mutexes */. i
1949c 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e f( p==0 ) return
1949d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 73 71 SQLITE_OK;. sq
1949e 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 lite3_mutex_ente
1949f 72 28 70 2d 3e 70 53 72 63 44 62 2d 3e 6d 75 74 r(p->pSrcDb->mut
194a0 65 78 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 ex);. sqlite3Bt
194a1 72 65 65 45 6e 74 65 72 28 70 2d 3e 70 53 72 63 reeEnter(p->pSrc
194a2 29 3b 0a 20 20 6d 75 74 65 78 20 3d 20 70 2d 3e );. mutex = p->
194a3 70 53 72 63 44 62 2d 3e 6d 75 74 65 78 3b 0a 20 pSrcDb->mutex;.
194a4 20 69 66 28 20 70 2d 3e 70 44 65 73 74 44 62 20 if( p->pDestDb
194a5 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d ){. sqlite3_m
194a6 75 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e 70 44 utex_enter(p->pD
194a7 65 73 74 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 estDb->mutex);.
194a8 20 7d 0a 0a 20 20 2f 2a 20 44 65 74 61 63 68 20 }.. /* Detach
194a9 74 68 69 73 20 62 61 63 6b 75 70 20 66 72 6f 6d this backup from
194aa 20 74 68 65 20 73 6f 75 72 63 65 20 70 61 67 65 the source page
194ab 72 2e 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70 r. */. if( p->p
194ac 44 65 73 74 44 62 20 29 7b 0a 20 20 20 20 70 2d DestDb ){. p-
194ad 3e 70 53 72 63 2d 3e 6e 42 61 63 6b 75 70 2d 2d >pSrc->nBackup--
194ae 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 69 ;. }. if( p->i
194af 73 41 74 74 61 63 68 65 64 20 29 7b 0a 20 20 20 sAttached ){.
194b0 20 70 70 20 3d 20 73 71 6c 69 74 65 33 50 61 67 pp = sqlite3Pag
194b1 65 72 42 61 63 6b 75 70 50 74 72 28 73 71 6c 69 erBackupPtr(sqli
194b2 74 65 33 42 74 72 65 65 50 61 67 65 72 28 70 2d te3BtreePager(p-
194b3 3e 70 53 72 63 29 29 3b 0a 20 20 20 20 77 68 69 >pSrc));. whi
194b4 6c 65 28 20 2a 70 70 21 3d 70 20 29 7b 0a 20 20 le( *pp!=p ){.
194b5 20 20 20 20 70 70 20 3d 20 26 28 2a 70 70 29 2d pp = &(*pp)-
194b6 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 >pNext;. }.
194b7 20 20 2a 70 70 20 3d 20 70 2d 3e 70 4e 65 78 74 *pp = p->pNext
194b8 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 61 ;. }.. /* If a
194b9 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 transaction is
194ba 73 74 69 6c 6c 20 6f 70 65 6e 20 6f 6e 20 74 68 still open on th
194bb 65 20 42 74 72 65 65 2c 20 72 6f 6c 6c 20 69 74 e Btree, roll it
194bc 20 62 61 63 6b 2e 20 2a 2f 0a 20 20 73 71 6c 69 back. */. sqli
194bd 74 65 33 42 74 72 65 65 52 6f 6c 6c 62 61 63 6b te3BtreeRollback
194be 28 70 2d 3e 70 44 65 73 74 29 3b 0a 0a 20 20 2f (p->pDest);.. /
194bf 2a 20 53 65 74 20 74 68 65 20 65 72 72 6f 72 20 * Set the error
194c0 63 6f 64 65 20 6f 66 20 74 68 65 20 64 65 73 74 code of the dest
194c1 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 ination database
194c2 20 68 61 6e 64 6c 65 2e 20 2a 2f 0a 20 20 72 63 handle. */. rc
194c3 20 3d 20 28 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 = (p->rc==SQLIT
194c4 45 5f 44 4f 4e 45 29 20 3f 20 53 51 4c 49 54 45 E_DONE) ? SQLITE
194c5 5f 4f 4b 20 3a 20 70 2d 3e 72 63 3b 0a 20 20 73 _OK : p->rc;. s
194c6 71 6c 69 74 65 33 45 72 72 6f 72 28 70 2d 3e 70 qlite3Error(p->p
194c7 44 65 73 74 44 62 2c 20 72 63 2c 20 30 29 3b 0a DestDb, rc, 0);.
194c8 0a 20 20 2f 2a 20 45 78 69 74 20 74 68 65 20 6d . /* Exit the m
194c9 75 74 65 78 65 73 20 61 6e 64 20 66 72 65 65 20 utexes and free
194ca 74 68 65 20 62 61 63 6b 75 70 20 63 6f 6e 74 65 the backup conte
194cb 78 74 20 73 74 72 75 63 74 75 72 65 2e 20 2a 2f xt structure. */
194cc 0a 20 20 69 66 28 20 70 2d 3e 70 44 65 73 74 44 . if( p->pDestD
194cd 62 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 b ){. sqlite3
194ce 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e _mutex_leave(p->
194cf 70 44 65 73 74 44 62 2d 3e 6d 75 74 65 78 29 3b pDestDb->mutex);
194d0 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 . }. sqlite3Bt
194d1 72 65 65 4c 65 61 76 65 28 70 2d 3e 70 53 72 63 reeLeave(p->pSrc
194d2 29 3b 0a 20 20 69 66 28 20 70 2d 3e 70 44 65 73 );. if( p->pDes
194d3 74 44 62 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 tDb ){. sqlit
194d4 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 7d 0a e3_free(p);. }.
194d5 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
194d6 6c 65 61 76 65 28 6d 75 74 65 78 29 3b 0a 20 20 leave(mutex);.
194d7 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
194d8 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e .** Return the n
194d9 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 73 umber of pages s
194da 74 69 6c 6c 20 74 6f 20 62 65 20 62 61 63 6b 65 till to be backe
194db 64 20 75 70 20 61 73 20 6f 66 20 74 68 65 20 6d d up as of the m
194dc 6f 73 74 20 72 65 63 65 6e 74 0a 2a 2a 20 63 61 ost recent.** ca
194dd 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 ll to sqlite3_ba
194de 63 6b 75 70 5f 73 74 65 70 28 29 2e 0a 2a 2f 0a ckup_step()..*/.
194df 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
194e0 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 72 65 qlite3_backup_re
194e1 6d 61 69 6e 69 6e 67 28 73 71 6c 69 74 65 33 5f maining(sqlite3_
194e2 62 61 63 6b 75 70 20 2a 70 29 7b 0a 20 20 72 65 backup *p){. re
194e3 74 75 72 6e 20 70 2d 3e 6e 52 65 6d 61 69 6e 69 turn p->nRemaini
194e4 6e 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 ng;.}../*.** Ret
194e5 75 72 6e 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 urn the total nu
194e6 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e mber of pages in
194e7 20 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 the source data
194e8 62 61 73 65 20 61 73 20 6f 66 20 74 68 65 20 6d base as of the m
194e9 6f 73 74 20 0a 2a 2a 20 72 65 63 65 6e 74 20 63 ost .** recent c
194ea 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 all to sqlite3_b
194eb 61 63 6b 75 70 5f 73 74 65 70 28 29 2e 0a 2a 2f ackup_step()..*/
194ec 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
194ed 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 70 sqlite3_backup_p
194ee 61 67 65 63 6f 75 6e 74 28 73 71 6c 69 74 65 33 agecount(sqlite3
194ef 5f 62 61 63 6b 75 70 20 2a 70 29 7b 0a 20 20 72 _backup *p){. r
194f0 65 74 75 72 6e 20 70 2d 3e 6e 50 61 67 65 63 6f eturn p->nPageco
194f1 75 6e 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 unt;.}../*.** Th
194f2 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 is function is c
194f3 61 6c 6c 65 64 20 61 66 74 65 72 20 74 68 65 20 alled after the
194f4 63 6f 6e 74 65 6e 74 73 20 6f 66 20 70 61 67 65 contents of page
194f5 20 69 50 61 67 65 20 6f 66 20 74 68 65 0a 2a 2a iPage of the.**
194f6 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 source database
194f7 20 68 61 76 65 20 62 65 65 6e 20 6d 6f 64 69 66 have been modif
194f8 69 65 64 2e 20 49 66 20 70 61 67 65 20 69 50 61 ied. If page iPa
194f9 67 65 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 ge has already b
194fa 65 65 6e 20 0a 2a 2a 20 63 6f 70 69 65 64 20 69 een .** copied i
194fb 6e 74 6f 20 74 68 65 20 64 65 73 74 69 6e 61 74 nto the destinat
194fc 69 6f 6e 20 64 61 74 61 62 61 73 65 2c 20 74 68 ion database, th
194fd 65 6e 20 74 68 65 20 64 61 74 61 20 77 72 69 74 en the data writ
194fe 74 65 6e 20 74 6f 20 74 68 65 0a 2a 2a 20 64 65 ten to the.** de
194ff 73 74 69 6e 61 74 69 6f 6e 20 69 73 20 6e 6f 77 stination is now
19500 20 69 6e 76 61 6c 69 64 61 74 65 64 2e 20 54 68 invalidated. Th
19501 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 63 6f e destination co
19502 70 79 20 6f 66 20 69 50 61 67 65 20 6e 65 65 64 py of iPage need
19503 73 0a 2a 2a 20 74 6f 20 62 65 20 75 70 64 61 74 s.** to be updat
19504 65 64 20 77 69 74 68 20 74 68 65 20 6e 65 77 20 ed with the new
19505 64 61 74 61 20 62 65 66 6f 72 65 20 74 68 65 20 data before the
19506 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e backup operation
19507 20 69 73 0a 2a 2a 20 63 6f 6d 70 6c 65 74 65 2e is.** complete.
19508 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 61 73 73 .**.** It is ass
19509 75 6d 65 64 20 74 68 61 74 20 74 68 65 20 6d 75 umed that the mu
1950a 74 65 78 20 61 73 73 6f 63 69 61 74 65 64 20 77 tex associated w
1950b 69 74 68 20 74 68 65 20 42 74 53 68 61 72 65 64 ith the BtShared
1950c 20 6f 62 6a 65 63 74 0a 2a 2a 20 63 6f 72 72 65 object.** corre
1950d 73 70 6f 6e 64 69 6e 67 20 74 6f 20 74 68 65 20 sponding to the
1950e 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 20 source database
1950f 69 73 20 68 65 6c 64 20 77 68 65 6e 20 74 68 69 is held when thi
19510 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 0a 2a 2a s function is.**
19511 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 called..*/.SQLI
19512 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
19513 73 71 6c 69 74 65 33 42 61 63 6b 75 70 55 70 64 sqlite3BackupUpd
19514 61 74 65 28 73 71 6c 69 74 65 33 5f 62 61 63 6b ate(sqlite3_back
19515 75 70 20 2a 70 42 61 63 6b 75 70 2c 20 50 67 6e up *pBackup, Pgn
19516 6f 20 69 50 61 67 65 2c 20 63 6f 6e 73 74 20 75 o iPage, const u
19517 38 20 2a 61 44 61 74 61 29 7b 0a 20 20 73 71 6c 8 *aData){. sql
19518 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 3b 20 ite3_backup *p;
19519 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1951a 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61 /* Iterator va
1951b 72 69 61 62 6c 65 20 2a 2f 0a 20 20 66 6f 72 28 riable */. for(
1951c 70 3d 70 42 61 63 6b 75 70 3b 20 70 3b 20 70 3d p=pBackup; p; p=
1951d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 61 p->pNext){. a
1951e 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
1951f 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 70 53 72 utex_held(p->pSr
19520 63 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 c->pBt->mutex) )
19521 3b 0a 20 20 20 20 69 66 28 20 21 69 73 46 61 74 ;. if( !isFat
19522 61 6c 45 72 72 6f 72 28 70 2d 3e 72 63 29 20 26 alError(p->rc) &
19523 26 20 69 50 61 67 65 3c 70 2d 3e 69 4e 65 78 74 & iPage<p->iNext
19524 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 ){. /* The
19525 20 62 61 63 6b 75 70 20 70 72 6f 63 65 73 73 20 backup process
19526 70 20 68 61 73 20 61 6c 72 65 61 64 79 20 63 6f p has already co
19527 70 69 65 64 20 70 61 67 65 20 69 50 61 67 65 2e pied page iPage.
19528 20 42 75 74 20 6e 6f 77 20 69 74 0a 20 20 20 20 But now it.
19529 20 20 2a 2a 20 68 61 73 20 62 65 65 6e 20 6d 6f ** has been mo
1952a 64 69 66 69 65 64 20 62 79 20 61 20 74 72 61 6e dified by a tran
1952b 73 61 63 74 69 6f 6e 20 6f 6e 20 74 68 65 20 73 saction on the s
1952c 6f 75 72 63 65 20 70 61 67 65 72 2e 20 43 6f 70 ource pager. Cop
1952d 79 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6e y. ** the n
1952e 65 77 20 64 61 74 61 20 69 6e 74 6f 20 74 68 65 ew data into the
1952f 20 62 61 63 6b 75 70 2e 0a 20 20 20 20 20 20 2a backup.. *
19530 2f 0a 20 20 20 20 20 20 69 6e 74 20 72 63 20 3d /. int rc =
19531 20 62 61 63 6b 75 70 4f 6e 65 50 61 67 65 28 70 backupOnePage(p
19532 2c 20 69 50 61 67 65 2c 20 61 44 61 74 61 29 3b , iPage, aData);
19533 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72 . assert( r
19534 63 21 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 26 c!=SQLITE_BUSY &
19535 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 4c 4f 43 & rc!=SQLITE_LOC
19536 4b 45 44 20 29 3b 0a 20 20 20 20 20 20 69 66 28 KED );. if(
19537 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
19538 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 72 63 20 {. p->rc
19539 3d 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 = rc;. }.
1953a 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a }. }.}../*.**
1953b 20 52 65 73 74 61 72 74 20 74 68 65 20 62 61 63 Restart the bac
1953c 6b 75 70 20 70 72 6f 63 65 73 73 2e 20 54 68 69 kup process. Thi
1953d 73 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e s is called when
1953e 20 74 68 65 20 70 61 67 65 72 20 6c 61 79 65 72 the pager layer
1953f 0a 2a 2a 20 64 65 74 65 63 74 73 20 74 68 61 74 .** detects that
19540 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61 the database ha
19541 73 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 20 s been modified
19542 62 79 20 61 6e 20 65 78 74 65 72 6e 61 6c 20 64 by an external d
19543 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 atabase.** conne
19544 63 74 69 6f 6e 2e 20 49 6e 20 74 68 69 73 20 63 ction. In this c
19545 61 73 65 20 74 68 65 72 65 20 69 73 20 6e 6f 20 ase there is no
19546 77 61 79 20 6f 66 20 6b 6e 6f 77 69 6e 67 20 77 way of knowing w
19547 68 69 63 68 20 6f 66 20 74 68 65 0a 2a 2a 20 70 hich of the.** p
19548 61 67 65 73 20 74 68 61 74 20 68 61 76 65 20 62 ages that have b
19549 65 65 6e 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 een copied into
1954a 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 the destination
1954b 64 61 74 61 62 61 73 65 20 61 72 65 20 73 74 69 database are sti
1954c 6c 6c 20 0a 2a 2a 20 76 61 6c 69 64 20 61 6e 64 ll .** valid and
1954d 20 77 68 69 63 68 20 61 72 65 20 6e 6f 74 2c 20 which are not,
1954e 73 6f 20 74 68 65 20 65 6e 74 69 72 65 20 70 72 so the entire pr
1954f 6f 63 65 73 73 20 6e 65 65 64 73 20 74 6f 20 62 ocess needs to b
19550 65 20 72 65 73 74 61 72 74 65 64 2e 0a 2a 2a 0a e restarted..**.
19551 2a 2a 20 49 74 20 69 73 20 61 73 73 75 6d 65 64 ** It is assumed
19552 20 74 68 61 74 20 74 68 65 20 6d 75 74 65 78 20 that the mutex
19553 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
19554 74 68 65 20 42 74 53 68 61 72 65 64 20 6f 62 6a the BtShared obj
19555 65 63 74 0a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e ect.** correspon
19556 64 69 6e 67 20 74 6f 20 74 68 65 20 73 6f 75 72 ding to the sour
19557 63 65 20 64 61 74 61 62 61 73 65 20 69 73 20 68 ce database is h
19558 65 6c 64 20 77 68 65 6e 20 74 68 69 73 20 66 75 eld when this fu
19559 6e 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 63 61 6c nction is.** cal
1955a 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 led..*/.SQLITE_P
1955b 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
1955c 74 65 33 42 61 63 6b 75 70 52 65 73 74 61 72 74 te3BackupRestart
1955d 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 (sqlite3_backup
1955e 2a 70 42 61 63 6b 75 70 29 7b 0a 20 20 73 71 6c *pBackup){. sql
1955f 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 3b 20 ite3_backup *p;
19560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19561 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61 /* Iterator va
19562 72 69 61 62 6c 65 20 2a 2f 0a 20 20 66 6f 72 28 riable */. for(
19563 70 3d 70 42 61 63 6b 75 70 3b 20 70 3b 20 70 3d p=pBackup; p; p=
19564 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 61 p->pNext){. a
19565 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
19566 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 70 53 72 utex_held(p->pSr
19567 63 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 c->pBt->mutex) )
19568 3b 0a 20 20 20 20 70 2d 3e 69 4e 65 78 74 20 3d ;. p->iNext =
19569 20 31 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 1;. }.}..#ifnd
1956a 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 ef SQLITE_OMIT_V
1956b 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 43 6f 70 79 ACUUM./*.** Copy
1956c 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 63 6f the complete co
1956d 6e 74 65 6e 74 20 6f 66 20 70 42 74 46 72 6f 6d ntent of pBtFrom
1956e 20 69 6e 74 6f 20 70 42 74 54 6f 2e 20 20 41 20 into pBtTo. A
1956f 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 6d transaction.** m
19570 75 73 74 20 62 65 20 61 63 74 69 76 65 20 66 6f ust be active fo
19571 72 20 62 6f 74 68 20 66 69 6c 65 73 2e 0a 2a 2a r both files..**
19572 0a 2a 2a 20 54 68 65 20 73 69 7a 65 20 6f 66 20 .** The size of
19573 66 69 6c 65 20 70 54 6f 20 6d 61 79 20 62 65 20 file pTo may be
19574 72 65 64 75 63 65 64 20 62 79 20 74 68 69 73 20 reduced by this
19575 6f 70 65 72 61 74 69 6f 6e 2e 20 49 66 20 61 6e operation. If an
19576 79 74 68 69 6e 67 20 0a 2a 2a 20 67 6f 65 73 20 ything .** goes
19577 77 72 6f 6e 67 2c 20 74 68 65 20 74 72 61 6e 73 wrong, the trans
19578 61 63 74 69 6f 6e 20 6f 6e 20 70 54 6f 20 69 73 action on pTo is
19579 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 49 66 rolled back. If
1957a 20 73 75 63 63 65 73 73 66 75 6c 2c 20 74 68 65 successful, the
1957b 20 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e .** transaction
1957c 20 69 73 20 63 6f 6d 6d 69 74 74 65 64 20 62 65 is committed be
1957d 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a fore returning..
1957e 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1957f 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
19580 65 65 43 6f 70 79 46 69 6c 65 28 42 74 72 65 65 eeCopyFile(Btree
19581 20 2a 70 54 6f 2c 20 42 74 72 65 65 20 2a 70 46 *pTo, Btree *pF
19582 72 6f 6d 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a rom){. int rc;.
19583 20 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 sqlite3_backup
19584 20 62 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 b;. sqlite3Btr
19585 65 65 45 6e 74 65 72 28 70 54 6f 29 3b 0a 20 20 eeEnter(pTo);.
19586 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 sqlite3BtreeEnte
19587 72 28 70 46 72 6f 6d 29 3b 0a 0a 20 20 2f 2a 20 r(pFrom);.. /*
19588 53 65 74 20 75 70 20 61 6e 20 73 71 6c 69 74 65 Set up an sqlite
19589 33 5f 62 61 63 6b 75 70 20 6f 62 6a 65 63 74 2e 3_backup object.
1958a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 2e sqlite3_backup.
1958b 70 44 65 73 74 44 62 20 6d 75 73 74 20 62 65 20 pDestDb must be
1958c 73 65 74 0a 20 20 2a 2a 20 74 6f 20 30 2e 20 54 set. ** to 0. T
1958d 68 69 73 20 69 73 20 75 73 65 64 20 62 79 20 74 his is used by t
1958e 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f he implementatio
1958f 6e 73 20 6f 66 20 73 71 6c 69 74 65 33 5f 62 61 ns of sqlite3_ba
19590 63 6b 75 70 5f 73 74 65 70 28 29 0a 20 20 2a 2a ckup_step(). **
19591 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 62 61 63 and sqlite3_bac
19592 6b 75 70 5f 66 69 6e 69 73 68 28 29 20 74 6f 20 kup_finish() to
19593 64 65 74 65 63 74 20 74 68 61 74 20 74 68 65 79 detect that they
19594 20 61 72 65 20 62 65 69 6e 67 20 63 61 6c 6c 65 are being calle
19595 64 0a 20 20 2a 2a 20 66 72 6f 6d 20 74 68 69 73 d. ** from this
19596 20 66 75 6e 63 74 69 6f 6e 2c 20 6e 6f 74 20 64 function, not d
19597 69 72 65 63 74 6c 79 20 62 79 20 74 68 65 20 75 irectly by the u
19598 73 65 72 2e 0a 20 20 2a 2f 0a 20 20 6d 65 6d 73 ser.. */. mems
19599 65 74 28 26 62 2c 20 30 2c 20 73 69 7a 65 6f 66 et(&b, 0, sizeof
1959a 28 62 29 29 3b 0a 20 20 62 2e 70 53 72 63 44 62 (b));. b.pSrcDb
1959b 20 3d 20 70 46 72 6f 6d 2d 3e 64 62 3b 0a 20 20 = pFrom->db;.
1959c 62 2e 70 53 72 63 20 3d 20 70 46 72 6f 6d 3b 0a b.pSrc = pFrom;.
1959d 20 20 62 2e 70 44 65 73 74 20 3d 20 70 54 6f 3b b.pDest = pTo;
1959e 0a 20 20 62 2e 69 4e 65 78 74 20 3d 20 31 3b 0a . b.iNext = 1;.
1959f 0a 20 20 2f 2a 20 30 78 37 46 46 46 46 46 46 46 . /* 0x7FFFFFFF
195a0 20 69 73 20 74 68 65 20 68 61 72 64 20 6c 69 6d is the hard lim
195a1 69 74 20 66 6f 72 20 74 68 65 20 6e 75 6d 62 65 it for the numbe
195a2 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 61 20 r of pages in a
195a3 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20 66 69 database. ** fi
195a4 6c 65 2e 20 42 79 20 70 61 73 73 69 6e 67 20 74 le. By passing t
195a5 68 69 73 20 61 73 20 74 68 65 20 6e 75 6d 62 65 his as the numbe
195a6 72 20 6f 66 20 70 61 67 65 73 20 74 6f 20 63 6f r of pages to co
195a7 70 79 20 74 6f 0a 20 20 2a 2a 20 73 71 6c 69 74 py to. ** sqlit
195a8 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 e3_backup_step()
195a9 2c 20 77 65 20 63 61 6e 20 67 75 61 72 61 6e 74 , we can guarant
195aa 65 65 20 74 68 61 74 20 74 68 65 20 63 6f 70 79 ee that the copy
195ab 20 66 69 6e 69 73 68 65 73 20 0a 20 20 2a 2a 20 finishes . **
195ac 77 69 74 68 69 6e 20 61 20 73 69 6e 67 6c 65 20 within a single
195ad 63 61 6c 6c 20 28 75 6e 6c 65 73 73 20 61 6e 20 call (unless an
195ae 65 72 72 6f 72 20 6f 63 63 75 72 73 29 2e 20 54 error occurs). T
195af 68 65 20 61 73 73 65 72 74 28 29 20 73 74 61 74 he assert() stat
195b0 65 6d 65 6e 74 0a 20 20 2a 2a 20 63 68 65 63 6b ement. ** check
195b1 73 20 74 68 69 73 20 61 73 73 75 6d 70 74 69 6f s this assumptio
195b2 6e 20 2d 20 28 70 2d 3e 72 63 29 20 73 68 6f 75 n - (p->rc) shou
195b3 6c 64 20 62 65 20 73 65 74 20 74 6f 20 65 69 74 ld be set to eit
195b4 68 65 72 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 her SQLITE_DONE
195b5 0a 20 20 2a 2a 20 6f 72 20 61 6e 20 65 72 72 6f . ** or an erro
195b6 72 20 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 73 r code.. */. s
195b7 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 qlite3_backup_st
195b8 65 70 28 26 62 2c 20 30 78 37 46 46 46 46 46 46 ep(&b, 0x7FFFFFF
195b9 46 29 3b 0a 20 20 61 73 73 65 72 74 28 20 62 2e F);. assert( b.
195ba 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b rc!=SQLITE_OK );
195bb 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f . rc = sqlite3_
195bc 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 26 62 backup_finish(&b
195bd 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c );. if( rc==SQL
195be 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 54 ITE_OK ){. pT
195bf 6f 2d 3e 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 o->pBt->pageSize
195c0 46 69 78 65 64 20 3d 20 30 3b 0a 20 20 7d 0a 0a Fixed = 0;. }..
195c1 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 sqlite3BtreeLe
195c2 61 76 65 28 70 46 72 6f 6d 29 3b 0a 20 20 73 71 ave(pFrom);. sq
195c3 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 lite3BtreeLeave(
195c4 70 54 6f 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 pTo);. return r
195c5 63 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 c;.}.#endif /* S
195c6 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55 QLITE_OMIT_VACUU
195c7 4d 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a M */../*********
195c8 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 62 61 63 ***** End of bac
195c9 6b 75 70 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a kup.c **********
195ca 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
195cb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
195cc 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
195cd 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
195ce 20 76 64 62 65 6d 65 6d 2e 63 20 2a 2a 2a 2a 2a vdbemem.c *****
195cf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
195d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
195d1 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 ****/./*.** 2004
195d2 20 4d 61 79 20 32 36 0a 2a 2a 0a 2a 2a 20 54 68 May 26.**.** Th
195d3 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 e author disclai
195d4 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 ms copyright to
195d5 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 this source code
195d6 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a . In place of.*
195d7 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 * a legal notice
195d8 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 , here is a bles
195d9 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d sing:.**.** M
195da 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 ay you do good a
195db 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 nd not evil..**
195dc 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 May you find
195dd 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 forgiveness for
195de 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 yourself and for
195df 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 give others..**
195e0 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 May you share
195e1 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 freely, never t
195e2 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 aking more than
195e3 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a you give..**.***
195e4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
195e5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
195e6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
195e7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
195e8 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 ******.**.** Thi
195e9 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 s file contains
195ea 63 6f 64 65 20 75 73 65 20 74 6f 20 6d 61 6e 69 code use to mani
195eb 70 75 6c 61 74 65 20 22 4d 65 6d 22 20 73 74 72 pulate "Mem" str
195ec 75 63 74 75 72 65 2e 20 20 41 20 22 4d 65 6d 22 ucture. A "Mem"
195ed 0a 2a 2a 20 73 74 6f 72 65 73 20 61 20 73 69 6e .** stores a sin
195ee 67 6c 65 20 76 61 6c 75 65 20 69 6e 20 74 68 65 gle value in the
195ef 20 56 44 42 45 2e 20 20 4d 65 6d 20 69 73 20 61 VDBE. Mem is a
195f0 6e 20 6f 70 61 71 75 65 20 73 74 72 75 63 74 75 n opaque structu
195f1 72 65 20 76 69 73 69 62 6c 65 0a 2a 2a 20 6f 6e re visible.** on
195f2 6c 79 20 77 69 74 68 69 6e 20 74 68 65 20 56 44 ly within the VD
195f3 42 45 2e 20 20 49 6e 74 65 72 66 61 63 65 20 72 BE. Interface r
195f4 6f 75 74 69 6e 65 73 20 72 65 66 65 72 20 74 6f outines refer to
195f5 20 61 20 4d 65 6d 20 75 73 69 6e 67 20 74 68 65 a Mem using the
195f6 0a 2a 2a 20 6e 61 6d 65 20 73 71 6c 69 74 65 5f .** name sqlite_
195f7 76 61 6c 75 65 0a 2a 2a 0a 2a 2a 20 24 49 64 3a value.**.** $Id:
195f8 20 76 64 62 65 6d 65 6d 2e 63 2c 76 20 31 2e 31 vdbemem.c,v 1.1
195f9 35 32 20 32 30 30 39 2f 30 37 2f 32 32 20 31 38 52 2009/07/22 18
195fa 3a 30 37 3a 34 31 20 64 72 68 20 45 78 70 20 24 :07:41 drh Exp $
195fb 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 .*/../*.** Call
195fc 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 45 78 sqlite3VdbeMemEx
195fd 70 61 6e 64 42 6c 6f 62 28 29 20 6f 6e 20 74 68 pandBlob() on th
195fe 65 20 73 75 70 70 6c 69 65 64 20 76 61 6c 75 65 e supplied value
195ff 20 28 74 79 70 65 20 4d 65 6d 2a 29 0a 2a 2a 20 (type Mem*).**
19600 50 20 69 66 20 72 65 71 75 69 72 65 64 2e 0a 2a P if required..*
19601 2f 0a 23 64 65 66 69 6e 65 20 65 78 70 61 6e 64 /.#define expand
19602 42 6c 6f 62 28 50 29 20 28 28 28 50 29 2d 3e 66 Blob(P) (((P)->f
19603 6c 61 67 73 26 4d 45 4d 5f 5a 65 72 6f 29 3f 73 lags&MEM_Zero)?s
19604 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 45 78 70 qlite3VdbeMemExp
19605 61 6e 64 42 6c 6f 62 28 50 29 3a 30 29 0a 0a 2f andBlob(P):0)../
19606 2a 0a 2a 2a 20 49 66 20 70 4d 65 6d 20 69 73 20 *.** If pMem is
19607 61 6e 20 6f 62 6a 65 63 74 20 77 69 74 68 20 61 an object with a
19608 20 76 61 6c 69 64 20 73 74 72 69 6e 67 20 72 65 valid string re
19609 70 72 65 73 65 6e 74 61 74 69 6f 6e 2c 20 74 68 presentation, th
1960a 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 65 6e is routine.** en
1960b 73 75 72 65 73 20 74 68 65 20 69 6e 74 65 72 6e sures the intern
1960c 61 6c 20 65 6e 63 6f 64 69 6e 67 20 66 6f 72 20 al encoding for
1960d 74 68 65 20 73 74 72 69 6e 67 20 72 65 70 72 65 the string repre
1960e 73 65 6e 74 61 74 69 6f 6e 20 69 73 0a 2a 2a 20 sentation is.**
1960f 27 64 65 73 69 72 65 64 45 6e 63 27 2c 20 6f 6e 'desiredEnc', on
19610 65 20 6f 66 20 53 51 4c 49 54 45 5f 55 54 46 38 e of SQLITE_UTF8
19611 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 , SQLITE_UTF16LE
19612 20 6f 72 20 53 51 4c 49 54 45 5f 55 54 46 31 36 or SQLITE_UTF16
19613 42 45 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 4d 65 BE..**.** If pMe
19614 6d 20 69 73 20 6e 6f 74 20 61 20 73 74 72 69 6e m is not a strin
19615 67 20 6f 62 6a 65 63 74 2c 20 6f 72 20 74 68 65 g object, or the
19616 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 74 68 65 encoding of the
19617 20 73 74 72 69 6e 67 0a 2a 2a 20 72 65 70 72 65 string.** repre
19618 73 65 6e 74 61 74 69 6f 6e 20 69 73 20 61 6c 72 sentation is alr
19619 65 61 64 79 20 73 74 6f 72 65 64 20 75 73 69 6e eady stored usin
1961a 67 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 g the requested
1961b 65 6e 63 6f 64 69 6e 67 2c 20 74 68 65 6e 20 74 encoding, then t
1961c 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 69 his.** routine i
1961d 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a s a no-op..**.**
1961e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 SQLITE_OK is re
1961f 74 75 72 6e 65 64 20 69 66 20 74 68 65 20 63 6f turned if the co
19620 6e 76 65 72 73 69 6f 6e 20 69 73 20 73 75 63 63 nversion is succ
19621 65 73 73 66 75 6c 20 28 6f 72 20 6e 6f 74 20 72 essful (or not r
19622 65 71 75 69 72 65 64 29 2e 0a 2a 2a 20 53 51 4c equired)..** SQL
19623 49 54 45 5f 4e 4f 4d 45 4d 20 6d 61 79 20 62 65 ITE_NOMEM may be
19624 20 72 65 74 75 72 6e 65 64 20 69 66 20 61 20 6d returned if a m
19625 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73 20 64 75 alloc() fails du
19626 72 69 6e 67 20 63 6f 6e 76 65 72 73 69 6f 6e 0a ring conversion.
19627 2a 2a 20 62 65 74 77 65 65 6e 20 66 6f 72 6d 61 ** between forma
19628 74 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ts..*/.SQLITE_PR
19629 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
1962a 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 3VdbeChangeEncod
1962b 69 6e 67 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 ing(Mem *pMem, i
1962c 6e 74 20 64 65 73 69 72 65 64 45 6e 63 29 7b 0a nt desiredEnc){.
1962d 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 int rc;. asse
1962e 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 rt( (pMem->flags
1962f 26 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 &MEM_RowSet)==0
19630 29 3b 0a 20 20 61 73 73 65 72 74 28 20 64 65 73 );. assert( des
19631 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f iredEnc==SQLITE_
19632 55 54 46 38 20 7c 7c 20 64 65 73 69 72 65 64 45 UTF8 || desiredE
19633 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 nc==SQLITE_UTF16
19634 4c 45 0a 20 20 20 20 20 20 20 20 20 20 20 7c 7c LE. ||
19635 20 64 65 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c desiredEnc==SQL
19636 49 54 45 5f 55 54 46 31 36 42 45 20 29 3b 0a 20 ITE_UTF16BE );.
19637 20 69 66 28 20 21 28 70 4d 65 6d 2d 3e 66 6c 61 if( !(pMem->fla
19638 67 73 26 4d 45 4d 5f 53 74 72 29 20 7c 7c 20 70 gs&MEM_Str) || p
19639 4d 65 6d 2d 3e 65 6e 63 3d 3d 64 65 73 69 72 65 Mem->enc==desire
1963a 64 45 6e 63 20 29 7b 0a 20 20 20 20 72 65 74 75 dEnc ){. retu
1963b 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
1963c 7d 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d }. assert( pMem
1963d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 ->db==0 || sqlit
1963e 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d e3_mutex_held(pM
1963f 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 em->db->mutex) )
19640 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ;.#ifdef SQLITE_
19641 4f 4d 49 54 5f 55 54 46 31 36 0a 20 20 72 65 74 OMIT_UTF16. ret
19642 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 urn SQLITE_ERROR
19643 3b 0a 23 65 6c 73 65 0a 0a 20 20 2f 2a 20 4d 65 ;.#else.. /* Me
19644 6d 54 72 61 6e 73 6c 61 74 65 28 29 20 6d 61 79 mTranslate() may
19645 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
19646 4b 20 6f 72 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 K or SQLITE_NOME
19647 4d 2e 20 49 66 20 4e 4f 4d 45 4d 20 69 73 20 72 M. If NOMEM is r
19648 65 74 75 72 6e 65 64 2c 0a 20 20 2a 2a 20 74 68 eturned,. ** th
19649 65 6e 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 en the encoding
1964a 6f 66 20 74 68 65 20 76 61 6c 75 65 20 6d 61 79 of the value may
1964b 20 6e 6f 74 20 68 61 76 65 20 63 68 61 6e 67 65 not have change
1964c 64 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 d.. */. rc = s
1964d 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 54 72 61 qlite3VdbeMemTra
1964e 6e 73 6c 61 74 65 28 70 4d 65 6d 2c 20 28 75 38 nslate(pMem, (u8
1964f 29 64 65 73 69 72 65 64 45 6e 63 29 3b 0a 20 20 )desiredEnc);.
19650 61 73 73 65 72 74 28 72 63 3d 3d 53 51 4c 49 54 assert(rc==SQLIT
19651 45 5f 4f 4b 20 20 20 20 7c 7c 20 72 63 3d 3d 53 E_OK || rc==S
19652 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 20 20 QLITE_NOMEM);.
19653 61 73 73 65 72 74 28 72 63 3d 3d 53 51 4c 49 54 assert(rc==SQLIT
19654 45 5f 4f 4b 20 20 20 20 7c 7c 20 70 4d 65 6d 2d E_OK || pMem-
19655 3e 65 6e 63 21 3d 64 65 73 69 72 65 64 45 6e 63 >enc!=desiredEnc
19656 29 3b 0a 20 20 61 73 73 65 72 74 28 72 63 3d 3d );. assert(rc==
19657 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 SQLITE_NOMEM ||
19658 70 4d 65 6d 2d 3e 65 6e 63 3d 3d 64 65 73 69 72 pMem->enc==desir
19659 65 64 45 6e 63 29 3b 0a 20 20 72 65 74 75 72 6e edEnc);. return
1965a 20 72 63 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f rc;.#endif.}../
1965b 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 70 *.** Make sure p
1965c 4d 65 6d 2d 3e 7a 20 70 6f 69 6e 74 73 20 74 6f Mem->z points to
1965d 20 61 20 77 72 69 74 61 62 6c 65 20 61 6c 6c 6f a writable allo
1965e 63 61 74 69 6f 6e 20 6f 66 20 61 74 20 6c 65 61 cation of at lea
1965f 73 74 20 0a 2a 2a 20 6e 20 62 79 74 65 73 2e 0a st .** n bytes..
19660 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6d 65 6d **.** If the mem
19661 6f 72 79 20 63 65 6c 6c 20 63 75 72 72 65 6e 74 ory cell current
19662 6c 79 20 63 6f 6e 74 61 69 6e 73 20 73 74 72 69 ly contains stri
19663 6e 67 20 6f 72 20 62 6c 6f 62 20 64 61 74 61 0a ng or blob data.
19664 2a 2a 20 61 6e 64 20 74 68 65 20 74 68 69 72 64 ** and the third
19665 20 61 72 67 75 6d 65 6e 74 20 70 61 73 73 65 64 argument passed
19666 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f to this functio
19667 6e 20 69 73 20 74 72 75 65 2c 20 74 68 65 20 0a n is true, the .
19668 2a 2a 20 63 75 72 72 65 6e 74 20 63 6f 6e 74 65 ** current conte
19669 6e 74 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 69 nt of the cell i
1966a 73 20 70 72 65 73 65 72 76 65 64 2e 20 4f 74 68 s preserved. Oth
1966b 65 72 77 69 73 65 2c 20 69 74 20 6d 61 79 0a 2a erwise, it may.*
1966c 2a 20 62 65 20 64 69 73 63 61 72 64 65 64 2e 20 * be discarded.
1966d 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e .**.** This fun
1966e 63 74 69 6f 6e 20 73 65 74 73 20 74 68 65 20 4d ction sets the M
1966f 45 4d 5f 44 79 6e 20 66 6c 61 67 20 61 6e 64 20 EM_Dyn flag and
19670 63 6c 65 61 72 73 20 61 6e 79 20 78 44 65 6c 20 clears any xDel
19671 63 61 6c 6c 62 61 63 6b 2e 0a 2a 2a 20 49 74 20 callback..** It
19672 61 6c 73 6f 20 63 6c 65 61 72 73 20 4d 45 4d 5f also clears MEM_
19673 45 70 68 65 6d 20 61 6e 64 20 4d 45 4d 5f 53 74 Ephem and MEM_St
19674 61 74 69 63 2e 20 49 66 20 74 68 65 20 70 72 65 atic. If the pre
19675 73 65 72 76 65 20 66 6c 61 67 20 69 73 20 0a 2a serve flag is .*
19676 2a 20 6e 6f 74 20 73 65 74 2c 20 4d 65 6d 2e 6e * not set, Mem.n
19677 20 69 73 20 7a 65 72 6f 65 64 2e 0a 2a 2f 0a 53 is zeroed..*/.S
19678 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
19679 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d t sqlite3VdbeMem
1967a 47 72 6f 77 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 Grow(Mem *pMem,
1967b 69 6e 74 20 6e 2c 20 69 6e 74 20 70 72 65 73 65 int n, int prese
1967c 72 76 65 29 7b 0a 20 20 61 73 73 65 72 74 28 20 rve){. assert(
1967d 31 20 3e 3d 0a 20 20 20 20 28 28 70 4d 65 6d 2d 1 >=. ((pMem-
1967e 3e 7a 4d 61 6c 6c 6f 63 20 26 26 20 70 4d 65 6d >zMalloc && pMem
1967f 2d 3e 7a 4d 61 6c 6c 6f 63 3d 3d 70 4d 65 6d 2d ->zMalloc==pMem-
19680 3e 7a 29 20 3f 20 31 20 3a 20 30 29 20 2b 0a 20 >z) ? 1 : 0) +.
19681 20 20 20 28 28 28 70 4d 65 6d 2d 3e 66 6c 61 67 (((pMem->flag
19682 73 26 4d 45 4d 5f 44 79 6e 29 26 26 70 4d 65 6d s&MEM_Dyn)&&pMem
19683 2d 3e 78 44 65 6c 29 20 3f 20 31 20 3a 20 30 29 ->xDel) ? 1 : 0)
19684 20 2b 20 0a 20 20 20 20 28 28 70 4d 65 6d 2d 3e + . ((pMem->
19685 66 6c 61 67 73 26 4d 45 4d 5f 45 70 68 65 6d 29 flags&MEM_Ephem)
19686 20 3f 20 31 20 3a 20 30 29 20 2b 20 0a 20 20 20 ? 1 : 0) + .
19687 20 28 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d ((pMem->flags&M
19688 45 4d 5f 53 74 61 74 69 63 29 20 3f 20 31 20 3a EM_Static) ? 1 :
19689 20 30 29 0a 20 20 29 3b 0a 20 20 61 73 73 65 72 0). );. asser
1968a 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 t( (pMem->flags&
1968b 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 MEM_RowSet)==0 )
1968c 3b 0a 0a 20 20 69 66 28 20 6e 3c 33 32 20 29 20 ;.. if( n<32 )
1968d 6e 20 3d 20 33 32 3b 0a 20 20 69 66 28 20 73 71 n = 32;. if( sq
1968e 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a lite3DbMallocSiz
1968f 65 28 70 4d 65 6d 2d 3e 64 62 2c 20 70 4d 65 6d e(pMem->db, pMem
19690 2d 3e 7a 4d 61 6c 6c 6f 63 29 3c 6e 20 29 7b 0a ->zMalloc)<n ){.
19691 20 20 20 20 69 66 28 20 70 72 65 73 65 72 76 65 if( preserve
19692 20 26 26 20 70 4d 65 6d 2d 3e 7a 3d 3d 70 4d 65 && pMem->z==pMe
19693 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 m->zMalloc ){.
19694 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 70 4d pMem->z = pM
19695 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 73 71 em->zMalloc = sq
19696 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 4f 72 lite3DbReallocOr
19697 46 72 65 65 28 70 4d 65 6d 2d 3e 64 62 2c 20 70 Free(pMem->db, p
19698 4d 65 6d 2d 3e 7a 2c 20 6e 29 3b 0a 20 20 20 20 Mem->z, n);.
19699 20 20 70 72 65 73 65 72 76 65 20 3d 20 30 3b 0a preserve = 0;.
1969a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1969b 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 sqlite3DbFree(p
1969c 4d 65 6d 2d 3e 64 62 2c 20 70 4d 65 6d 2d 3e 7a Mem->db, pMem->z
1969d 4d 61 6c 6c 6f 63 29 3b 0a 20 20 20 20 20 20 70 Malloc);. p
1969e 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 73 Mem->zMalloc = s
1969f 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 qlite3DbMallocRa
196a0 77 28 70 4d 65 6d 2d 3e 64 62 2c 20 6e 29 3b 0a w(pMem->db, n);.
196a1 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 }. }.. if(
196a2 20 70 4d 65 6d 2d 3e 7a 20 26 26 20 70 72 65 73 pMem->z && pres
196a3 65 72 76 65 20 26 26 20 70 4d 65 6d 2d 3e 7a 4d erve && pMem->zM
196a4 61 6c 6c 6f 63 20 26 26 20 70 4d 65 6d 2d 3e 7a alloc && pMem->z
196a5 21 3d 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 !=pMem->zMalloc
196a6 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 4d ){. memcpy(pM
196a7 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 2c 20 70 4d 65 em->zMalloc, pMe
196a8 6d 2d 3e 7a 2c 20 70 4d 65 6d 2d 3e 6e 29 3b 0a m->z, pMem->n);.
196a9 20 20 7d 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e }. if( pMem->
196aa 66 6c 61 67 73 26 4d 45 4d 5f 44 79 6e 20 26 26 flags&MEM_Dyn &&
196ab 20 70 4d 65 6d 2d 3e 78 44 65 6c 20 29 7b 0a 20 pMem->xDel ){.
196ac 20 20 20 70 4d 65 6d 2d 3e 78 44 65 6c 28 28 76 pMem->xDel((v
196ad 6f 69 64 20 2a 29 28 70 4d 65 6d 2d 3e 7a 29 29 oid *)(pMem->z))
196ae 3b 0a 20 20 7d 0a 0a 20 20 70 4d 65 6d 2d 3e 7a ;. }.. pMem->z
196af 20 3d 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 = pMem->zMalloc
196b0 3b 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 7a 3d ;. if( pMem->z=
196b1 3d 30 20 29 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e =0 ){. pMem->
196b2 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c flags = MEM_Null
196b3 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 ;. }else{. p
196b4 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 Mem->flags &= ~(
196b5 4d 45 4d 5f 45 70 68 65 6d 7c 4d 45 4d 5f 53 74 MEM_Ephem|MEM_St
196b6 61 74 69 63 29 3b 0a 20 20 7d 0a 20 20 70 4d 65 atic);. }. pMe
196b7 6d 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 72 m->xDel = 0;. r
196b8 65 74 75 72 6e 20 28 70 4d 65 6d 2d 3e 7a 20 3f eturn (pMem->z ?
196b9 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c SQLITE_OK : SQL
196ba 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 7d 0a 0a 2f ITE_NOMEM);.}../
196bb 2a 0a 2a 2a 20 4d 61 6b 65 20 74 68 65 20 67 69 *.** Make the gi
196bc 76 65 6e 20 4d 65 6d 20 6f 62 6a 65 63 74 20 4d ven Mem object M
196bd 45 4d 5f 44 79 6e 2e 20 20 49 6e 20 6f 74 68 65 EM_Dyn. In othe
196be 72 20 77 6f 72 64 73 2c 20 6d 61 6b 65 20 69 74 r words, make it
196bf 20 73 6f 0a 2a 2a 20 74 68 61 74 20 61 6e 79 20 so.** that any
196c0 54 45 58 54 20 6f 72 20 42 4c 4f 42 20 63 6f 6e TEXT or BLOB con
196c1 74 65 6e 74 20 69 73 20 73 74 6f 72 65 64 20 69 tent is stored i
196c2 6e 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 n memory obtaine
196c3 64 20 66 72 6f 6d 0a 2a 2a 20 6d 61 6c 6c 6f 63 d from.** malloc
196c4 28 29 2e 20 20 49 6e 20 74 68 69 73 20 77 61 79 (). In this way
196c5 2c 20 77 65 20 6b 6e 6f 77 20 74 68 61 74 20 74 , we know that t
196c6 68 65 20 6d 65 6d 6f 72 79 20 69 73 20 73 61 66 he memory is saf
196c7 65 20 74 6f 20 62 65 0a 2a 2a 20 6f 76 65 72 77 e to be.** overw
196c8 72 69 74 74 65 6e 20 6f 72 20 61 6c 74 65 72 65 ritten or altere
196c9 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 d..**.** Return
196ca 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 SQLITE_OK on suc
196cb 63 65 73 73 20 6f 72 20 53 51 4c 49 54 45 5f 4e cess or SQLITE_N
196cc 4f 4d 45 4d 20 69 66 20 6d 61 6c 6c 6f 63 20 66 OMEM if malloc f
196cd 61 69 6c 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ails..*/.SQLITE_
196ce 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
196cf 74 65 33 56 64 62 65 4d 65 6d 4d 61 6b 65 57 72 te3VdbeMemMakeWr
196d0 69 74 65 61 62 6c 65 28 4d 65 6d 20 2a 70 4d 65 iteable(Mem *pMe
196d1 6d 29 7b 0a 20 20 69 6e 74 20 66 3b 0a 20 20 61 m){. int f;. a
196d2 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d ssert( pMem->db=
196d3 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 =0 || sqlite3_mu
196d4 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 tex_held(pMem->d
196d5 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 b->mutex) );. a
196d6 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c ssert( (pMem->fl
196d7 61 67 73 26 4d 45 4d 5f 52 6f 77 53 65 74 29 3d ags&MEM_RowSet)=
196d8 3d 30 20 29 3b 0a 20 20 65 78 70 61 6e 64 42 6c =0 );. expandBl
196d9 6f 62 28 70 4d 65 6d 29 3b 0a 20 20 66 20 3d 20 ob(pMem);. f =
196da 70 4d 65 6d 2d 3e 66 6c 61 67 73 3b 0a 20 20 69 pMem->flags;. i
196db 66 28 20 28 66 26 28 4d 45 4d 5f 53 74 72 7c 4d f( (f&(MEM_Str|M
196dc 45 4d 5f 42 6c 6f 62 29 29 20 26 26 20 70 4d 65 EM_Blob)) && pMe
196dd 6d 2d 3e 7a 21 3d 70 4d 65 6d 2d 3e 7a 4d 61 6c m->z!=pMem->zMal
196de 6c 6f 63 20 29 7b 0a 20 20 20 20 69 66 28 20 73 loc ){. if( s
196df 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f qlite3VdbeMemGro
196e0 77 28 70 4d 65 6d 2c 20 70 4d 65 6d 2d 3e 6e 20 w(pMem, pMem->n
196e1 2b 20 32 2c 20 31 29 20 29 7b 0a 20 20 20 20 20 + 2, 1) ){.
196e2 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e return SQLITE_N
196e3 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 OMEM;. }.
196e4 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e 5d pMem->z[pMem->n]
196e5 20 3d 20 30 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e = 0;. pMem->
196e6 7a 5b 70 4d 65 6d 2d 3e 6e 2b 31 5d 20 3d 20 30 z[pMem->n+1] = 0
196e7 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 ;. pMem->flag
196e8 73 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20 s |= MEM_Term;.
196e9 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c }.. return SQL
196ea 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ITE_OK;.}../*.**
196eb 20 49 66 20 74 68 65 20 67 69 76 65 6e 20 4d 65 If the given Me
196ec 6d 2a 20 68 61 73 20 61 20 7a 65 72 6f 2d 66 69 m* has a zero-fi
196ed 6c 6c 65 64 20 74 61 69 6c 2c 20 74 75 72 6e 20 lled tail, turn
196ee 69 74 20 69 6e 74 6f 20 61 6e 20 6f 72 64 69 6e it into an ordin
196ef 61 72 79 0a 2a 2a 20 62 6c 6f 62 20 73 74 6f 72 ary.** blob stor
196f0 65 64 20 69 6e 20 64 79 6e 61 6d 69 63 61 6c 6c ed in dynamicall
196f1 79 20 61 6c 6c 6f 63 61 74 65 64 20 73 70 61 63 y allocated spac
196f2 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 e..*/.#ifndef SQ
196f3 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c LITE_OMIT_INCRBL
196f4 4f 42 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 OB.SQLITE_PRIVAT
196f5 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
196f6 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62 28 4d eMemExpandBlob(M
196f7 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 69 66 28 em *pMem){. if(
196f8 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d pMem->flags & M
196f9 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20 69 EM_Zero ){. i
196fa 6e 74 20 6e 42 79 74 65 3b 0a 20 20 20 20 61 73 nt nByte;. as
196fb 73 65 72 74 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 sert( pMem->flag
196fc 73 26 4d 45 4d 5f 42 6c 6f 62 20 29 3b 0a 20 20 s&MEM_Blob );.
196fd 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d assert( (pMem-
196fe 3e 66 6c 61 67 73 26 4d 45 4d 5f 52 6f 77 53 65 >flags&MEM_RowSe
196ff 74 29 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 t)==0 );. ass
19700 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 ert( pMem->db==0
19701 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 || sqlite3_mute
19702 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d x_held(pMem->db-
19703 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 20 20 >mutex) );..
19704 2f 2a 20 53 65 74 20 6e 42 79 74 65 20 74 6f 20 /* Set nByte to
19705 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 the number of by
19706 74 65 73 20 72 65 71 75 69 72 65 64 20 74 6f 20 tes required to
19707 73 74 6f 72 65 20 74 68 65 20 65 78 70 61 6e 64 store the expand
19708 65 64 20 62 6c 6f 62 2e 20 2a 2f 0a 20 20 20 20 ed blob. */.
19709 6e 42 79 74 65 20 3d 20 70 4d 65 6d 2d 3e 6e 20 nByte = pMem->n
1970a 2b 20 70 4d 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 3b + pMem->u.nZero;
1970b 0a 20 20 20 20 69 66 28 20 6e 42 79 74 65 3c 3d . if( nByte<=
1970c 30 20 29 7b 0a 20 20 20 20 20 20 6e 42 79 74 65 0 ){. nByte
1970d 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 = 1;. }.
1970e 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d if( sqlite3VdbeM
1970f 65 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 6e 42 79 emGrow(pMem, nBy
19710 74 65 2c 20 31 29 20 29 7b 0a 20 20 20 20 20 20 te, 1) ){.
19711 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f return SQLITE_NO
19712 4d 45 4d 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 MEM;. }..
19713 6d 65 6d 73 65 74 28 26 70 4d 65 6d 2d 3e 7a 5b memset(&pMem->z[
19714 70 4d 65 6d 2d 3e 6e 5d 2c 20 30 2c 20 70 4d 65 pMem->n], 0, pMe
19715 6d 2d 3e 75 2e 6e 5a 65 72 6f 29 3b 0a 20 20 20 m->u.nZero);.
19716 20 70 4d 65 6d 2d 3e 6e 20 2b 3d 20 70 4d 65 6d pMem->n += pMem
19717 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 20 20 70 ->u.nZero;. p
19718 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 Mem->flags &= ~(
19719 4d 45 4d 5f 5a 65 72 6f 7c 4d 45 4d 5f 54 65 72 MEM_Zero|MEM_Ter
1971a 6d 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e m);. }. return
1971b 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 SQLITE_OK;.}.#e
1971c 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b ndif.../*.** Mak
1971d 65 20 73 75 72 65 20 74 68 65 20 67 69 76 65 6e e sure the given
1971e 20 4d 65 6d 20 69 73 20 5c 75 30 30 30 30 20 74 Mem is \u0000 t
1971f 65 72 6d 69 6e 61 74 65 64 2e 0a 2a 2f 0a 53 51 erminated..*/.SQ
19720 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
19721 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e sqlite3VdbeMemN
19722 75 6c 54 65 72 6d 69 6e 61 74 65 28 4d 65 6d 20 ulTerminate(Mem
19723 2a 70 4d 65 6d 29 7b 0a 20 20 61 73 73 65 72 74 *pMem){. assert
19724 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c ( pMem->db==0 ||
19725 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
19726 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 eld(pMem->db->mu
19727 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 28 70 tex) );. if( (p
19728 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d Mem->flags & MEM
19729 5f 54 65 72 6d 29 21 3d 30 20 7c 7c 20 28 70 4d _Term)!=0 || (pM
1972a 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f em->flags & MEM_
1972b 53 74 72 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72 Str)==0 ){. r
1972c 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
1972d 20 20 20 2f 2a 20 4e 6f 74 68 69 6e 67 20 74 6f /* Nothing to
1972e 20 64 6f 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 do */. }. if(
1972f 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 sqlite3VdbeMemG
19730 72 6f 77 28 70 4d 65 6d 2c 20 70 4d 65 6d 2d 3e row(pMem, pMem->
19731 6e 2b 32 2c 20 31 29 20 29 7b 0a 20 20 20 20 72 n+2, 1) ){. r
19732 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d eturn SQLITE_NOM
19733 45 4d 3b 0a 20 20 7d 0a 20 20 70 4d 65 6d 2d 3e EM;. }. pMem->
19734 7a 5b 70 4d 65 6d 2d 3e 6e 5d 20 3d 20 30 3b 0a z[pMem->n] = 0;.
19735 20 20 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e pMem->z[pMem->
19736 6e 2b 31 5d 20 3d 20 30 3b 0a 20 20 70 4d 65 6d n+1] = 0;. pMem
19737 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 54 ->flags |= MEM_T
19738 65 72 6d 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 erm;. return SQ
19739 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a LITE_OK;.}../*.*
1973a 2a 20 41 64 64 20 4d 45 4d 5f 53 74 72 20 74 6f * Add MEM_Str to
1973b 20 74 68 65 20 73 65 74 20 6f 66 20 72 65 70 72 the set of repr
1973c 65 73 65 6e 74 61 74 69 6f 6e 73 20 66 6f 72 20 esentations for
1973d 74 68 65 20 67 69 76 65 6e 20 4d 65 6d 2e 20 20 the given Mem.
1973e 4e 75 6d 62 65 72 73 0a 2a 2a 20 61 72 65 20 63 Numbers.** are c
1973f 6f 6e 76 65 72 74 65 64 20 75 73 69 6e 67 20 73 onverted using s
19740 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
19741 29 2e 20 20 43 6f 6e 76 65 72 74 69 6e 67 20 61 ). Converting a
19742 20 42 4c 4f 42 20 74 6f 20 61 20 73 74 72 69 6e BLOB to a strin
19743 67 0a 2a 2a 20 69 73 20 61 20 6e 6f 2d 6f 70 2e g.** is a no-op.
19744 0a 2a 2a 0a 2a 2a 20 45 78 69 73 74 69 6e 67 20 .**.** Existing
19745 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 73 20 representations
19746 4d 45 4d 5f 49 6e 74 20 61 6e 64 20 4d 45 4d 5f MEM_Int and MEM_
19747 52 65 61 6c 20 61 72 65 20 2a 6e 6f 74 2a 20 69 Real are *not* i
19748 6e 76 61 6c 69 64 61 74 65 64 2e 0a 2a 2a 0a 2a nvalidated..**.*
19749 2a 20 41 20 4d 45 4d 5f 4e 75 6c 6c 20 76 61 6c * A MEM_Null val
1974a 75 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 65 ue will never be
1974b 20 70 61 73 73 65 64 20 74 6f 20 74 68 69 73 20 passed to this
1974c 66 75 6e 63 74 69 6f 6e 2e 20 54 68 69 73 20 66 function. This f
1974d 75 6e 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 75 73 unction is.** us
1974e 65 64 20 66 6f 72 20 63 6f 6e 76 65 72 74 69 6e ed for convertin
1974f 67 20 76 61 6c 75 65 73 20 74 6f 20 74 65 78 74 g values to text
19750 20 66 6f 72 20 72 65 74 75 72 6e 69 6e 67 20 74 for returning t
19751 6f 20 74 68 65 20 75 73 65 72 20 28 69 2e 65 2e o the user (i.e.
19752 20 76 69 61 0a 2a 2a 20 73 71 6c 69 74 65 33 5f via.** sqlite3_
19753 76 61 6c 75 65 5f 74 65 78 74 28 29 29 2c 20 6f value_text()), o
19754 72 20 66 6f 72 20 65 6e 73 75 72 69 6e 67 20 74 r for ensuring t
19755 68 61 74 20 76 61 6c 75 65 73 20 74 6f 20 62 65 hat values to be
19756 20 75 73 65 64 20 61 73 20 62 74 72 65 65 0a 2a used as btree.*
19757 2a 20 6b 65 79 73 20 61 72 65 20 73 74 72 69 6e * keys are strin
19758 67 73 2e 20 49 6e 20 74 68 65 20 66 6f 72 6d 65 gs. In the forme
19759 72 20 63 61 73 65 20 61 20 4e 55 4c 4c 20 70 6f r case a NULL po
1975a 69 6e 74 65 72 20 69 73 20 72 65 74 75 72 6e 65 inter is returne
1975b 64 20 74 68 65 0a 2a 2a 20 75 73 65 72 20 61 6e d the.** user an
1975c 64 20 74 68 65 20 6c 61 74 65 72 20 69 73 20 61 d the later is a
1975d 6e 20 69 6e 74 65 72 6e 61 6c 20 70 72 6f 67 72 n internal progr
1975e 61 6d 6d 69 6e 67 20 65 72 72 6f 72 2e 0a 2a 2f amming error..*/
1975f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
19760 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d int sqlite3VdbeM
19761 65 6d 53 74 72 69 6e 67 69 66 79 28 4d 65 6d 20 emStringify(Mem
19762 2a 70 4d 65 6d 2c 20 69 6e 74 20 65 6e 63 29 7b *pMem, int enc){
19763 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
19764 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 66 67 20 TE_OK;. int fg
19765 3d 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 3b 0a 20 = pMem->flags;.
19766 20 63 6f 6e 73 74 20 69 6e 74 20 6e 42 79 74 65 const int nByte
19767 20 3d 20 33 32 3b 0a 0a 20 20 61 73 73 65 72 74 = 32;.. assert
19768 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c ( pMem->db==0 ||
19769 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
1976a 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 eld(pMem->db->mu
1976b 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 tex) );. assert
1976c 28 20 21 28 66 67 26 4d 45 4d 5f 5a 65 72 6f 29 ( !(fg&MEM_Zero)
1976d 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 28 );. assert( !(
1976e 66 67 26 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f fg&(MEM_Str|MEM_
1976f 42 6c 6f 62 29 29 20 29 3b 0a 20 20 61 73 73 65 Blob)) );. asse
19770 72 74 28 20 66 67 26 28 4d 45 4d 5f 49 6e 74 7c rt( fg&(MEM_Int|
19771 4d 45 4d 5f 52 65 61 6c 29 20 29 3b 0a 20 20 61 MEM_Real) );. a
19772 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c ssert( (pMem->fl
19773 61 67 73 26 4d 45 4d 5f 52 6f 77 53 65 74 29 3d ags&MEM_RowSet)=
19774 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
19775 45 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e EIGHT_BYTE_ALIGN
19776 4d 45 4e 54 28 70 4d 65 6d 29 20 29 3b 0a 0a 0a MENT(pMem) );...
19777 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 if( sqlite3Vdb
19778 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 6e eMemGrow(pMem, n
19779 42 79 74 65 2c 20 30 29 20 29 7b 0a 20 20 20 20 Byte, 0) ){.
1977a 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f return SQLITE_NO
1977b 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 MEM;. }.. /* F
1977c 6f 72 20 61 20 52 65 61 6c 20 6f 72 20 49 6e 74 or a Real or Int
1977d 65 67 65 72 2c 20 75 73 65 20 73 71 6c 69 74 65 eger, use sqlite
1977e 33 5f 6d 70 72 69 6e 74 66 28 29 20 74 6f 20 70 3_mprintf() to p
1977f 72 6f 64 75 63 65 20 74 68 65 20 55 54 46 2d 38 roduce the UTF-8
19780 0a 20 20 2a 2a 20 73 74 72 69 6e 67 20 72 65 70 . ** string rep
19781 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 resentation of t
19782 68 65 20 76 61 6c 75 65 2e 20 54 68 65 6e 2c 20 he value. Then,
19783 69 66 20 74 68 65 20 72 65 71 75 69 72 65 64 20 if the required
19784 65 6e 63 6f 64 69 6e 67 0a 20 20 2a 2a 20 69 73 encoding. ** is
19785 20 55 54 46 2d 31 36 6c 65 20 6f 72 20 55 54 46 UTF-16le or UTF
19786 2d 31 36 62 65 20 64 6f 20 61 20 74 72 61 6e 73 -16be do a trans
19787 6c 61 74 69 6f 6e 2e 0a 20 20 2a 2a 20 0a 20 20 lation.. ** .
19788 2a 2a 20 46 49 58 20 4d 45 3a 20 49 74 20 77 6f ** FIX ME: It wo
19789 75 6c 64 20 62 65 20 62 65 74 74 65 72 20 69 66 uld be better if
1978a 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 sqlite3_snprint
1978b 66 28 29 20 63 6f 75 6c 64 20 64 6f 20 55 54 46 f() could do UTF
1978c 2d 31 36 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 -16.. */. if(
1978d 66 67 20 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a fg & MEM_Int ){.
1978e 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 sqlite3_snpr
1978f 69 6e 74 66 28 6e 42 79 74 65 2c 20 70 4d 65 6d intf(nByte, pMem
19790 2d 3e 7a 2c 20 22 25 6c 6c 64 22 2c 20 70 4d 65 ->z, "%lld", pMe
19791 6d 2d 3e 75 2e 69 29 3b 0a 20 20 7d 65 6c 73 65 m->u.i);. }else
19792 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 66 67 {. assert( fg
19793 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29 3b 0a 20 & MEM_Real );.
19794 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 sqlite3_snpri
19795 6e 74 66 28 6e 42 79 74 65 2c 20 70 4d 65 6d 2d ntf(nByte, pMem-
19796 3e 7a 2c 20 22 25 21 2e 31 35 67 22 2c 20 70 4d >z, "%!.15g", pM
19797 65 6d 2d 3e 72 29 3b 0a 20 20 7d 0a 20 20 70 4d em->r);. }. pM
19798 65 6d 2d 3e 6e 20 3d 20 73 71 6c 69 74 65 33 53 em->n = sqlite3S
19799 74 72 6c 65 6e 33 30 28 70 4d 65 6d 2d 3e 7a 29 trlen30(pMem->z)
1979a 3b 0a 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 ;. pMem->enc =
1979b 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a 20 20 70 SQLITE_UTF8;. p
1979c 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 Mem->flags |= ME
1979d 4d 5f 53 74 72 7c 4d 45 4d 5f 54 65 72 6d 3b 0a M_Str|MEM_Term;.
1979e 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 sqlite3VdbeCha
1979f 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70 4d 65 6d ngeEncoding(pMem
197a0 2c 20 65 6e 63 29 3b 0a 20 20 72 65 74 75 72 6e , enc);. return
197a1 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65 rc;.}../*.** Me
197a2 6d 6f 72 79 20 63 65 6c 6c 20 70 4d 65 6d 20 63 mory cell pMem c
197a3 6f 6e 74 61 69 6e 73 20 74 68 65 20 63 6f 6e 74 ontains the cont
197a4 65 78 74 20 6f 66 20 61 6e 20 61 67 67 72 65 67 ext of an aggreg
197a5 61 74 65 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a ate function..**
197a6 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 61 This routine ca
197a7 6c 6c 73 20 74 68 65 20 66 69 6e 61 6c 69 7a 65 lls the finalize
197a8 20 6d 65 74 68 6f 64 20 66 6f 72 20 74 68 61 74 method for that
197a9 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 0a function. The.
197aa 2a 2a 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 ** result of the
197ab 20 61 67 67 72 65 67 61 74 65 20 69 73 20 73 74 aggregate is st
197ac 6f 72 65 64 20 62 61 63 6b 20 69 6e 74 6f 20 70 ored back into p
197ad 4d 65 6d 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 Mem..**.** Retur
197ae 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 69 n SQLITE_ERROR i
197af 66 20 74 68 65 20 66 69 6e 61 6c 69 7a 65 72 20 f the finalizer
197b0 72 65 70 6f 72 74 73 20 61 6e 20 65 72 72 6f 72 reports an error
197b1 2e 20 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 . SQLITE_OK.**
197b2 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2f 0a 53 51 otherwise..*/.SQ
197b3 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
197b4 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 sqlite3VdbeMemF
197b5 69 6e 61 6c 69 7a 65 28 4d 65 6d 20 2a 70 4d 65 inalize(Mem *pMe
197b6 6d 2c 20 46 75 6e 63 44 65 66 20 2a 70 46 75 6e m, FuncDef *pFun
197b7 63 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 c){. int rc = S
197b8 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 QLITE_OK;. if(
197b9 41 4c 57 41 59 53 28 70 46 75 6e 63 20 26 26 20 ALWAYS(pFunc &&
197ba 70 46 75 6e 63 2d 3e 78 46 69 6e 61 6c 69 7a 65 pFunc->xFinalize
197bb 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 ) ){. sqlite3
197bc 5f 63 6f 6e 74 65 78 74 20 63 74 78 3b 0a 20 20 _context ctx;.
197bd 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d assert( (pMem-
197be 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c >flags & MEM_Nul
197bf 6c 29 21 3d 30 20 7c 7c 20 70 46 75 6e 63 3d 3d l)!=0 || pFunc==
197c0 70 4d 65 6d 2d 3e 75 2e 70 44 65 66 20 29 3b 0a pMem->u.pDef );.
197c1 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d assert( pMem
197c2 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 ->db==0 || sqlit
197c3 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d e3_mutex_held(pM
197c4 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 em->db->mutex) )
197c5 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 63 74 ;. memset(&ct
197c6 78 2c 20 30 2c 20 73 69 7a 65 6f 66 28 63 74 78 x, 0, sizeof(ctx
197c7 29 29 3b 0a 20 20 20 20 63 74 78 2e 73 2e 66 6c ));. ctx.s.fl
197c8 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a ags = MEM_Null;.
197c9 20 20 20 20 63 74 78 2e 73 2e 64 62 20 3d 20 70 ctx.s.db = p
197ca 4d 65 6d 2d 3e 64 62 3b 0a 20 20 20 20 63 74 78 Mem->db;. ctx
197cb 2e 70 4d 65 6d 20 3d 20 70 4d 65 6d 3b 0a 20 20 .pMem = pMem;.
197cc 20 20 63 74 78 2e 70 46 75 6e 63 20 3d 20 70 46 ctx.pFunc = pF
197cd 75 6e 63 3b 0a 20 20 20 20 70 46 75 6e 63 2d 3e unc;. pFunc->
197ce 78 46 69 6e 61 6c 69 7a 65 28 26 63 74 78 29 3b xFinalize(&ctx);
197cf 0a 20 20 20 20 61 73 73 65 72 74 28 20 30 3d 3d . assert( 0==
197d0 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d (pMem->flags&MEM
197d1 5f 44 79 6e 29 20 26 26 20 21 70 4d 65 6d 2d 3e _Dyn) && !pMem->
197d2 78 44 65 6c 20 29 3b 0a 20 20 20 20 73 71 6c 69 xDel );. sqli
197d3 74 65 33 44 62 46 72 65 65 28 70 4d 65 6d 2d 3e te3DbFree(pMem->
197d4 64 62 2c 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f db, pMem->zMallo
197d5 63 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 c);. memcpy(p
197d6 4d 65 6d 2c 20 26 63 74 78 2e 73 2c 20 73 69 7a Mem, &ctx.s, siz
197d7 65 6f 66 28 63 74 78 2e 73 29 29 3b 0a 20 20 20 eof(ctx.s));.
197d8 20 72 63 20 3d 20 63 74 78 2e 69 73 45 72 72 6f rc = ctx.isErro
197d9 72 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 r;. }. return
197da 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 rc;.}../*.** If
197db 74 68 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 the memory cell
197dc 63 6f 6e 74 61 69 6e 73 20 61 20 73 74 72 69 6e contains a strin
197dd 67 20 76 61 6c 75 65 20 74 68 61 74 20 6d 75 73 g value that mus
197de 74 20 62 65 20 66 72 65 65 64 20 62 79 0a 2a 2a t be freed by.**
197df 20 69 6e 76 6f 6b 69 6e 67 20 61 6e 20 65 78 74 invoking an ext
197e0 65 72 6e 61 6c 20 63 61 6c 6c 62 61 63 6b 2c 20 ernal callback,
197e1 66 72 65 65 20 69 74 20 6e 6f 77 2e 20 43 61 6c free it now. Cal
197e2 6c 69 6e 67 20 74 68 69 73 20 66 75 6e 63 74 69 ling this functi
197e3 6f 6e 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 66 on.** does not f
197e4 72 65 65 20 61 6e 79 20 4d 65 6d 2e 7a 4d 61 6c ree any Mem.zMal
197e5 6c 6f 63 20 62 75 66 66 65 72 2e 0a 2a 2f 0a 53 loc buffer..*/.S
197e6 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
197e7 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 id sqlite3VdbeMe
197e8 6d 52 65 6c 65 61 73 65 45 78 74 65 72 6e 61 6c mReleaseExternal
197e9 28 4d 65 6d 20 2a 70 29 7b 0a 20 20 61 73 73 65 (Mem *p){. asse
197ea 72 74 28 20 70 2d 3e 64 62 3d 3d 30 20 7c 7c 20 rt( p->db==0 ||
197eb 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
197ec 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 ld(p->db->mutex)
197ed 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 );. testcase(
197ee 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 41 p->flags & MEM_A
197ef 67 67 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 gg );. testcase
197f0 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d ( p->flags & MEM
197f1 5f 44 79 6e 20 29 3b 0a 20 20 74 65 73 74 63 61 _Dyn );. testca
197f2 73 65 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 4d se( p->flags & M
197f3 45 4d 5f 52 6f 77 53 65 74 20 29 3b 0a 20 20 74 EM_RowSet );. t
197f4 65 73 74 63 61 73 65 28 20 70 2d 3e 66 6c 61 67 estcase( p->flag
197f5 73 20 26 20 4d 45 4d 5f 46 72 61 6d 65 20 29 3b s & MEM_Frame );
197f6 0a 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 26 . if( p->flags&
197f7 28 4d 45 4d 5f 41 67 67 7c 4d 45 4d 5f 44 79 6e (MEM_Agg|MEM_Dyn
197f8 7c 4d 45 4d 5f 52 6f 77 53 65 74 7c 4d 45 4d 5f |MEM_RowSet|MEM_
197f9 46 72 61 6d 65 29 20 29 7b 0a 20 20 20 20 69 66 Frame) ){. if
197fa 28 20 70 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 41 ( p->flags&MEM_A
197fb 67 67 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 gg ){. sqli
197fc 74 65 33 56 64 62 65 4d 65 6d 46 69 6e 61 6c 69 te3VdbeMemFinali
197fd 7a 65 28 70 2c 20 70 2d 3e 75 2e 70 44 65 66 29 ze(p, p->u.pDef)
197fe 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
197ff 28 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f (p->flags & MEM_
19800 41 67 67 29 3d 3d 30 20 29 3b 0a 20 20 20 20 20 Agg)==0 );.
19801 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 sqlite3VdbeMemR
19802 65 6c 65 61 73 65 28 70 29 3b 0a 20 20 20 20 7d elease(p);. }
19803 65 6c 73 65 20 69 66 28 20 70 2d 3e 66 6c 61 67 else if( p->flag
19804 73 26 4d 45 4d 5f 44 79 6e 20 26 26 20 70 2d 3e s&MEM_Dyn && p->
19805 78 44 65 6c 20 29 7b 0a 20 20 20 20 20 20 61 73 xDel ){. as
19806 73 65 72 74 28 20 28 70 2d 3e 66 6c 61 67 73 26 sert( (p->flags&
19807 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 MEM_RowSet)==0 )
19808 3b 0a 20 20 20 20 20 20 70 2d 3e 78 44 65 6c 28 ;. p->xDel(
19809 28 76 6f 69 64 20 2a 29 70 2d 3e 7a 29 3b 0a 20 (void *)p->z);.
1980a 20 20 20 20 20 70 2d 3e 78 44 65 6c 20 3d 20 30 p->xDel = 0
1980b 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 ;. }else if(
1980c 70 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 52 6f 77 p->flags&MEM_Row
1980d 53 65 74 20 29 7b 0a 20 20 20 20 20 20 73 71 6c Set ){. sql
1980e 69 74 65 33 52 6f 77 53 65 74 43 6c 65 61 72 28 ite3RowSetClear(
1980f 70 2d 3e 75 2e 70 52 6f 77 53 65 74 29 3b 0a 20 p->u.pRowSet);.
19810 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e }else if( p->
19811 66 6c 61 67 73 26 4d 45 4d 5f 46 72 61 6d 65 20 flags&MEM_Frame
19812 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
19813 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 VdbeMemSetNull(p
19814 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a );. }. }.}..
19815 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61 6e /*.** Release an
19816 79 20 6d 65 6d 6f 72 79 20 68 65 6c 64 20 62 79 y memory held by
19817 20 74 68 65 20 4d 65 6d 2e 20 54 68 69 73 20 6d the Mem. This m
19818 61 79 20 6c 65 61 76 65 20 74 68 65 20 4d 65 6d ay leave the Mem
19819 20 69 6e 20 61 6e 0a 2a 2a 20 69 6e 63 6f 6e 73 in an.** incons
1981a 69 73 74 65 6e 74 20 73 74 61 74 65 2c 20 66 6f istent state, fo
1981b 72 20 65 78 61 6d 70 6c 65 20 77 69 74 68 20 28 r example with (
1981c 4d 65 6d 2e 7a 3d 3d 30 29 20 61 6e 64 0a 2a 2a Mem.z==0) and.**
1981d 20 28 4d 65 6d 2e 74 79 70 65 3d 3d 53 51 4c 49 (Mem.type==SQLI
1981e 54 45 5f 54 45 58 54 29 2e 0a 2a 2f 0a 53 51 4c TE_TEXT)..*/.SQL
1981f 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
19820 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 sqlite3VdbeMemR
19821 65 6c 65 61 73 65 28 4d 65 6d 20 2a 70 29 7b 0a elease(Mem *p){.
19822 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
19823 52 65 6c 65 61 73 65 45 78 74 65 72 6e 61 6c 28 ReleaseExternal(
19824 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 p);. sqlite3DbF
19825 72 65 65 28 70 2d 3e 64 62 2c 20 70 2d 3e 7a 4d ree(p->db, p->zM
19826 61 6c 6c 6f 63 29 3b 0a 20 20 70 2d 3e 7a 20 3d alloc);. p->z =
19827 20 30 3b 0a 20 20 70 2d 3e 7a 4d 61 6c 6c 6f 63 0;. p->zMalloc
19828 20 3d 20 30 3b 0a 20 20 70 2d 3e 78 44 65 6c 20 = 0;. p->xDel
19829 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f = 0;.}../*.** Co
1982a 6e 76 65 72 74 20 61 20 36 34 2d 62 69 74 20 49 nvert a 64-bit I
1982b 45 45 45 20 64 6f 75 62 6c 65 20 69 6e 74 6f 20 EEE double into
1982c 61 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20 a 64-bit signed
1982d 69 6e 74 65 67 65 72 2e 0a 2a 2a 20 49 66 20 74 integer..** If t
1982e 68 65 20 64 6f 75 62 6c 65 20 69 73 20 74 6f 6f he double is too
1982f 20 6c 61 72 67 65 2c 20 72 65 74 75 72 6e 20 30 large, return 0
19830 78 38 30 30 30 30 30 30 30 30 30 30 30 30 30 30 x800000000000000
19831 30 2e 0a 2a 2a 0a 2a 2a 20 4d 6f 73 74 20 73 79 0..**.** Most sy
19832 73 74 65 6d 73 20 61 70 70 65 61 72 20 74 6f 20 stems appear to
19833 64 6f 20 74 68 69 73 20 73 69 6d 70 6c 79 20 62 do this simply b
19834 79 20 61 73 73 69 67 6e 69 6e 67 0a 2a 2a 20 76 y assigning.** v
19835 61 72 69 61 62 6c 65 73 20 61 6e 64 20 77 69 74 ariables and wit
19836 68 6f 75 74 20 74 68 65 20 65 78 74 72 61 20 72 hout the extra r
19837 61 6e 67 65 20 74 65 73 74 73 2e 20 20 42 75 74 ange tests. But
19838 0a 2a 2a 20 74 68 65 72 65 20 61 72 65 20 72 65 .** there are re
19839 70 6f 72 74 73 20 74 68 61 74 20 77 69 6e 64 6f ports that windo
1983a 77 73 20 74 68 72 6f 77 73 20 61 6e 20 65 78 70 ws throws an exp
1983b 65 63 74 69 6f 6e 0a 2a 2a 20 69 66 20 74 68 65 ection.** if the
1983c 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 floating point
1983d 76 61 6c 75 65 20 69 73 20 6f 75 74 20 6f 66 20 value is out of
1983e 72 61 6e 67 65 2e 20 28 53 65 65 20 74 69 63 6b range. (See tick
1983f 65 74 20 23 32 38 38 30 2e 29 0a 2a 2a 20 42 65 et #2880.).** Be
19840 63 61 75 73 65 20 77 65 20 64 6f 20 6e 6f 74 20 cause we do not
19841 63 6f 6d 70 6c 65 74 65 6c 79 20 75 6e 64 65 72 completely under
19842 73 74 61 6e 64 20 74 68 65 20 70 72 6f 62 6c 65 stand the proble
19843 6d 2c 20 77 65 20 77 69 6c 6c 0a 2a 2a 20 74 61 m, we will.** ta
19844 6b 65 20 74 68 65 20 63 6f 6e 73 65 72 76 61 74 ke the conservat
19845 69 76 65 20 61 70 70 72 6f 61 63 68 20 61 6e 64 ive approach and
19846 20 61 6c 77 61 79 73 20 64 6f 20 72 61 6e 67 65 always do range
19847 20 74 65 73 74 73 0a 2a 2a 20 62 65 66 6f 72 65 tests.** before
19848 20 61 74 74 65 6d 70 74 69 6e 67 20 74 68 65 20 attempting the
19849 63 6f 6e 76 65 72 73 69 6f 6e 2e 0a 2a 2f 0a 73 conversion..*/.s
1984a 74 61 74 69 63 20 69 36 34 20 64 6f 75 62 6c 65 tatic i64 double
1984b 54 6f 49 6e 74 36 34 28 64 6f 75 62 6c 65 20 72 ToInt64(double r
1984c 29 7b 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4d 61 6e ){. /*. ** Man
1984d 79 20 63 6f 6d 70 69 6c 65 72 73 20 77 65 20 65 y compilers we e
1984e 6e 63 6f 75 6e 74 65 72 20 64 6f 20 6e 6f 74 20 ncounter do not
1984f 64 65 66 69 6e 65 20 63 6f 6e 73 74 61 6e 74 73 define constants
19850 20 66 6f 72 20 74 68 65 0a 20 20 2a 2a 20 6d 69 for the. ** mi
19851 6e 69 6d 75 6d 20 61 6e 64 20 6d 61 78 69 6d 75 nimum and maximu
19852 6d 20 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72 m 64-bit integer
19853 73 2c 20 6f 72 20 74 68 65 79 20 64 65 66 69 6e s, or they defin
19854 65 20 74 68 65 6d 0a 20 20 2a 2a 20 69 6e 63 6f e them. ** inco
19855 6e 73 69 73 74 65 6e 74 6c 79 2e 20 20 41 6e 64 nsistently. And
19856 20 6d 61 6e 79 20 64 6f 20 6e 6f 74 20 75 6e 64 many do not und
19857 65 72 73 74 61 6e 64 20 74 68 65 20 22 4c 4c 22 erstand the "LL"
19858 20 6e 6f 74 61 74 69 6f 6e 2e 0a 20 20 2a 2a 20 notation.. **
19859 53 6f 20 77 65 20 64 65 66 69 6e 65 20 6f 75 72 So we define our
1985a 20 6f 77 6e 20 73 74 61 74 69 63 20 63 6f 6e 73 own static cons
1985b 74 61 6e 74 73 20 68 65 72 65 20 75 73 69 6e 67 tants here using
1985c 20 6e 6f 74 68 69 6e 67 0a 20 20 2a 2a 20 6c 61 nothing. ** la
1985d 72 67 65 72 20 74 68 61 6e 20 61 20 33 32 2d 62 rger than a 32-b
1985e 69 74 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 it integer const
1985f 61 6e 74 2e 0a 20 20 2a 2f 0a 20 20 73 74 61 74 ant.. */. stat
19860 69 63 20 63 6f 6e 73 74 20 69 36 34 20 6d 61 78 ic const i64 max
19861 49 6e 74 20 3d 20 4c 41 52 47 45 53 54 5f 49 4e Int = LARGEST_IN
19862 54 36 34 3b 0a 20 20 73 74 61 74 69 63 20 63 6f T64;. static co
19863 6e 73 74 20 69 36 34 20 6d 69 6e 49 6e 74 20 3d nst i64 minInt =
19864 20 53 4d 41 4c 4c 45 53 54 5f 49 4e 54 36 34 3b SMALLEST_INT64;
19865 0a 0a 20 20 69 66 28 20 72 3c 28 64 6f 75 62 6c .. if( r<(doubl
19866 65 29 6d 69 6e 49 6e 74 20 29 7b 0a 20 20 20 20 e)minInt ){.
19867 72 65 74 75 72 6e 20 6d 69 6e 49 6e 74 3b 0a 20 return minInt;.
19868 20 7d 65 6c 73 65 20 69 66 28 20 72 3e 28 64 6f }else if( r>(do
19869 75 62 6c 65 29 6d 61 78 49 6e 74 20 29 7b 0a 20 uble)maxInt ){.
1986a 20 20 20 2f 2a 20 6d 69 6e 49 6e 74 20 69 73 20 /* minInt is
1986b 63 6f 72 72 65 63 74 20 68 65 72 65 20 2d 20 6e correct here - n
1986c 6f 74 20 6d 61 78 49 6e 74 2e 20 20 49 74 20 74 ot maxInt. It t
1986d 75 72 6e 73 20 6f 75 74 20 74 68 61 74 20 61 73 urns out that as
1986e 73 69 67 6e 69 6e 67 0a 20 20 20 20 2a 2a 20 61 signing. ** a
1986f 20 76 65 72 79 20 6c 61 72 67 65 20 70 6f 73 69 very large posi
19870 74 69 76 65 20 6e 75 6d 62 65 72 20 74 6f 20 61 tive number to a
19871 6e 20 69 6e 74 65 67 65 72 20 72 65 73 75 6c 74 n integer result
19872 73 20 69 6e 20 61 20 76 65 72 79 20 6c 61 72 67 s in a very larg
19873 65 0a 20 20 20 20 2a 2a 20 6e 65 67 61 74 69 76 e. ** negativ
19874 65 20 69 6e 74 65 67 65 72 2e 20 20 54 68 69 73 e integer. This
19875 20 6d 61 6b 65 73 20 6e 6f 20 73 65 6e 73 65 2c makes no sense,
19876 20 62 75 74 20 69 74 20 69 73 20 77 68 61 74 20 but it is what
19877 78 38 36 20 68 61 72 64 77 61 72 65 0a 20 20 20 x86 hardware.
19878 20 2a 2a 20 64 6f 65 73 20 73 6f 20 66 6f 72 20 ** does so for
19879 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 77 65 compatibility we
1987a 20 77 69 6c 6c 20 64 6f 20 74 68 65 20 73 61 6d will do the sam
1987b 65 20 69 6e 20 73 6f 66 74 77 61 72 65 2e 20 2a e in software. *
1987c 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 6d 69 6e /. return min
1987d 49 6e 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 Int;. }else{.
1987e 20 20 72 65 74 75 72 6e 20 28 69 36 34 29 72 3b return (i64)r;
1987f 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 . }.}../*.** Re
19880 74 75 72 6e 20 73 6f 6d 65 20 6b 69 6e 64 20 6f turn some kind o
19881 66 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20 f integer value
19882 77 68 69 63 68 20 69 73 20 74 68 65 20 62 65 73 which is the bes
19883 74 20 77 65 20 63 61 6e 20 64 6f 0a 2a 2a 20 61 t we can do.** a
19884 74 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20 74 t representing t
19885 68 65 20 76 61 6c 75 65 20 74 68 61 74 20 2a 70 he value that *p
19886 4d 65 6d 20 64 65 73 63 72 69 62 65 73 20 61 73 Mem describes as
19887 20 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2a 20 an integer..**
19888 49 66 20 70 4d 65 6d 20 69 73 20 61 6e 20 69 6e If pMem is an in
19889 74 65 67 65 72 2c 20 74 68 65 6e 20 74 68 65 20 teger, then the
1988a 76 61 6c 75 65 20 69 73 20 65 78 61 63 74 2e 20 value is exact.
1988b 20 49 66 20 70 4d 65 6d 20 69 73 0a 2a 2a 20 61 If pMem is.** a
1988c 20 66 6c 6f 61 74 69 6e 67 2d 70 6f 69 6e 74 20 floating-point
1988d 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 72 then the value r
1988e 65 74 75 72 6e 65 64 20 69 73 20 74 68 65 20 69 eturned is the i
1988f 6e 74 65 67 65 72 20 70 61 72 74 2e 0a 2a 2a 20 nteger part..**
19890 49 66 20 70 4d 65 6d 20 69 73 20 61 20 73 74 72 If pMem is a str
19891 69 6e 67 20 6f 72 20 62 6c 6f 62 2c 20 74 68 65 ing or blob, the
19892 6e 20 77 65 20 6d 61 6b 65 20 61 6e 20 61 74 74 n we make an att
19893 65 6d 70 74 20 74 6f 20 63 6f 6e 76 65 72 74 0a empt to convert.
19894 2a 2a 20 69 74 20 69 6e 74 6f 20 61 20 69 6e 74 ** it into a int
19895 65 67 65 72 20 61 6e 64 20 72 65 74 75 72 6e 20 eger and return
19896 74 68 61 74 2e 20 20 49 66 20 70 4d 65 6d 20 72 that. If pMem r
19897 65 70 72 65 73 65 6e 74 73 20 61 6e 0a 2a 2a 20 epresents an.**
19898 61 6e 20 53 51 4c 2d 4e 55 4c 4c 20 76 61 6c 75 an SQL-NULL valu
19899 65 2c 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2a 0a e, return 0..**.
1989a 2a 2a 20 49 66 20 70 4d 65 6d 20 72 65 70 72 65 ** If pMem repre
1989b 73 65 6e 74 73 20 61 20 73 74 72 69 6e 67 20 76 sents a string v
1989c 61 6c 75 65 2c 20 69 74 73 20 65 6e 63 6f 64 69 alue, its encodi
1989d 6e 67 20 6d 69 67 68 74 20 62 65 20 63 68 61 6e ng might be chan
1989e 67 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ged..*/.SQLITE_P
1989f 52 49 56 41 54 45 20 69 36 34 20 73 71 6c 69 74 RIVATE i64 sqlit
198a0 65 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28 4d e3VdbeIntValue(M
198a1 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 69 6e 74 em *pMem){. int
198a2 20 66 6c 61 67 73 3b 0a 20 20 61 73 73 65 72 74 flags;. assert
198a3 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c ( pMem->db==0 ||
198a4 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
198a5 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 eld(pMem->db->mu
198a6 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 tex) );. assert
198a7 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 ( EIGHT_BYTE_ALI
198a8 47 4e 4d 45 4e 54 28 70 4d 65 6d 29 20 29 3b 0a GNMENT(pMem) );.
198a9 20 20 66 6c 61 67 73 20 3d 20 70 4d 65 6d 2d 3e flags = pMem->
198aa 66 6c 61 67 73 3b 0a 20 20 69 66 28 20 66 6c 61 flags;. if( fla
198ab 67 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a gs & MEM_Int ){.
198ac 20 20 20 20 72 65 74 75 72 6e 20 70 4d 65 6d 2d return pMem-
198ad 3e 75 2e 69 3b 0a 20 20 7d 65 6c 73 65 20 69 66 >u.i;. }else if
198ae 28 20 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 ( flags & MEM_Re
198af 61 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e al ){. return
198b0 20 64 6f 75 62 6c 65 54 6f 49 6e 74 36 34 28 70 doubleToInt64(p
198b1 4d 65 6d 2d 3e 72 29 3b 0a 20 20 7d 65 6c 73 65 Mem->r);. }else
198b2 20 69 66 28 20 66 6c 61 67 73 20 26 20 28 4d 45 if( flags & (ME
198b3 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20 M_Str|MEM_Blob)
198b4 29 7b 0a 20 20 20 20 69 36 34 20 76 61 6c 75 65 ){. i64 value
198b5 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 ;. pMem->flag
198b6 73 20 7c 3d 20 4d 45 4d 5f 53 74 72 3b 0a 20 20 s |= MEM_Str;.
198b7 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 if( sqlite3Vdb
198b8 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 eChangeEncoding(
198b9 70 4d 65 6d 2c 20 53 51 4c 49 54 45 5f 55 54 46 pMem, SQLITE_UTF
198ba 38 29 0a 20 20 20 20 20 20 20 7c 7c 20 73 71 6c 8). || sql
198bb 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 6c 54 65 ite3VdbeMemNulTe
198bc 72 6d 69 6e 61 74 65 28 70 4d 65 6d 29 20 29 7b rminate(pMem) ){
198bd 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b . return 0;
198be 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 . }. asser
198bf 74 28 20 70 4d 65 6d 2d 3e 7a 20 29 3b 0a 20 20 t( pMem->z );.
198c0 20 20 73 71 6c 69 74 65 33 41 74 6f 69 36 34 28 sqlite3Atoi64(
198c1 70 4d 65 6d 2d 3e 7a 2c 20 26 76 61 6c 75 65 29 pMem->z, &value)
198c2 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 76 61 6c ;. return val
198c3 75 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ue;. }else{.
198c4 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 7d return 0;. }.}
198c5 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ../*.** Return t
198c6 68 65 20 62 65 73 74 20 72 65 70 72 65 73 65 6e he best represen
198c7 74 61 74 69 6f 6e 20 6f 66 20 70 4d 65 6d 20 74 tation of pMem t
198c8 68 61 74 20 77 65 20 63 61 6e 20 67 65 74 20 69 hat we can get i
198c9 6e 74 6f 20 61 0a 2a 2a 20 64 6f 75 62 6c 65 2e nto a.** double.
198ca 20 20 49 66 20 70 4d 65 6d 20 69 73 20 61 6c 72 If pMem is alr
198cb 65 61 64 79 20 61 20 64 6f 75 62 6c 65 20 6f 72 eady a double or
198cc 20 61 6e 20 69 6e 74 65 67 65 72 2c 20 72 65 74 an integer, ret
198cd 75 72 6e 20 69 74 73 0a 2a 2a 20 76 61 6c 75 65 urn its.** value
198ce 2e 20 20 49 66 20 69 74 20 69 73 20 61 20 73 74 . If it is a st
198cf 72 69 6e 67 20 6f 72 20 62 6c 6f 62 2c 20 74 72 ring or blob, tr
198d0 79 20 74 6f 20 63 6f 6e 76 65 72 74 20 69 74 20 y to convert it
198d1 74 6f 20 61 20 64 6f 75 62 6c 65 2e 0a 2a 2a 20 to a double..**
198d2 49 66 20 69 74 20 69 73 20 61 20 4e 55 4c 4c 2c If it is a NULL,
198d3 20 72 65 74 75 72 6e 20 30 2e 30 2e 0a 2a 2f 0a return 0.0..*/.
198d4 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 64 SQLITE_PRIVATE d
198d5 6f 75 62 6c 65 20 73 71 6c 69 74 65 33 56 64 62 ouble sqlite3Vdb
198d6 65 52 65 61 6c 56 61 6c 75 65 28 4d 65 6d 20 2a eRealValue(Mem *
198d7 70 4d 65 6d 29 7b 0a 20 20 61 73 73 65 72 74 28 pMem){. assert(
198d8 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 pMem->db==0 ||
198d9 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
198da 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 ld(pMem->db->mut
198db 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ex) );. assert(
198dc 20 45 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 EIGHT_BYTE_ALIG
198dd 4e 4d 45 4e 54 28 70 4d 65 6d 29 20 29 3b 0a 20 NMENT(pMem) );.
198de 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 if( pMem->flags
198df 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 & MEM_Real ){.
198e0 20 20 20 72 65 74 75 72 6e 20 70 4d 65 6d 2d 3e return pMem->
198e1 72 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 r;. }else if( p
198e2 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d Mem->flags & MEM
198e3 5f 49 6e 74 20 29 7b 0a 20 20 20 20 72 65 74 75 _Int ){. retu
198e4 72 6e 20 28 64 6f 75 62 6c 65 29 70 4d 65 6d 2d rn (double)pMem-
198e5 3e 75 2e 69 3b 0a 20 20 7d 65 6c 73 65 20 69 66 >u.i;. }else if
198e6 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 ( pMem->flags &
198e7 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f (MEM_Str|MEM_Blo
198e8 62 29 20 29 7b 0a 20 20 20 20 2f 2a 20 28 64 6f b) ){. /* (do
198e9 75 62 6c 65 29 30 20 49 6e 20 63 61 73 65 20 6f uble)0 In case o
198ea 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c f SQLITE_OMIT_FL
198eb 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 2e 2e 2e 20 OATING_POINT...
198ec 2a 2f 0a 20 20 20 20 64 6f 75 62 6c 65 20 76 61 */. double va
198ed 6c 20 3d 20 28 64 6f 75 62 6c 65 29 30 3b 0a 20 l = (double)0;.
198ee 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c pMem->flags |
198ef 3d 20 4d 45 4d 5f 53 74 72 3b 0a 20 20 20 20 69 = MEM_Str;. i
198f0 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 f( sqlite3VdbeCh
198f1 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70 4d 65 angeEncoding(pMe
198f2 6d 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 0a m, SQLITE_UTF8).
198f3 20 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 || sqlite
198f4 33 56 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 3VdbeMemNulTermi
198f5 6e 61 74 65 28 70 4d 65 6d 29 20 29 7b 0a 20 20 nate(pMem) ){.
198f6 20 20 20 20 2f 2a 20 28 64 6f 75 62 6c 65 29 30 /* (double)0
198f7 20 49 6e 20 63 61 73 65 20 6f 66 20 53 51 4c 49 In case of SQLI
198f8 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 TE_OMIT_FLOATING
198f9 5f 50 4f 49 4e 54 2e 2e 2e 20 2a 2f 0a 20 20 20 _POINT... */.
198fa 20 20 20 72 65 74 75 72 6e 20 28 64 6f 75 62 6c return (doubl
198fb 65 29 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 e)0;. }. a
198fc 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 7a 20 29 ssert( pMem->z )
198fd 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 41 74 6f ;. sqlite3Ato
198fe 46 28 70 4d 65 6d 2d 3e 7a 2c 20 26 76 61 6c 29 F(pMem->z, &val)
198ff 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 76 61 6c ;. return val
19900 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f ;. }else{. /
19901 2a 20 28 64 6f 75 62 6c 65 29 30 20 49 6e 20 63 * (double)0 In c
19902 61 73 65 20 6f 66 20 53 51 4c 49 54 45 5f 4f 4d ase of SQLITE_OM
19903 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e IT_FLOATING_POIN
19904 54 2e 2e 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 T... */. retu
19905 72 6e 20 28 64 6f 75 62 6c 65 29 30 3b 0a 20 20 rn (double)0;.
19906 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 4d }.}../*.** The M
19907 45 4d 20 73 74 72 75 63 74 75 72 65 20 69 73 20 EM structure is
19908 61 6c 72 65 61 64 79 20 61 20 4d 45 4d 5f 52 65 already a MEM_Re
19909 61 6c 2e 20 20 54 72 79 20 74 6f 20 61 6c 73 6f al. Try to also
1990a 20 6d 61 6b 65 20 69 74 20 61 0a 2a 2a 20 4d 45 make it a.** ME
1990b 4d 5f 49 6e 74 20 69 66 20 77 65 20 63 61 6e 2e M_Int if we can.
1990c 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1990d 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
1990e 64 62 65 49 6e 74 65 67 65 72 41 66 66 69 6e 69 dbeIntegerAffini
1990f 74 79 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 ty(Mem *pMem){.
19910 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 66 assert( pMem->f
19911 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 20 lags & MEM_Real
19912 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d );. assert( (pM
19913 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f em->flags & MEM_
19914 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 RowSet)==0 );.
19915 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 assert( pMem->db
19916 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d ==0 || sqlite3_m
19917 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e utex_held(pMem->
19918 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 db->mutex) );.
19919 61 73 73 65 72 74 28 20 45 49 47 48 54 5f 42 59 assert( EIGHT_BY
1991a 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 70 4d 65 TE_ALIGNMENT(pMe
1991b 6d 29 20 29 3b 0a 0a 20 20 70 4d 65 6d 2d 3e 75 m) );.. pMem->u
1991c 2e 69 20 3d 20 64 6f 75 62 6c 65 54 6f 49 6e 74 .i = doubleToInt
1991d 36 34 28 70 4d 65 6d 2d 3e 72 29 3b 0a 0a 20 20 64(pMem->r);..
1991e 2f 2a 20 4f 6e 6c 79 20 6d 61 72 6b 20 74 68 65 /* Only mark the
1991f 20 76 61 6c 75 65 20 61 73 20 61 6e 20 69 6e 74 value as an int
19920 65 67 65 72 20 69 66 0a 20 20 2a 2a 0a 20 20 2a eger if. **. *
19921 2a 20 20 20 20 28 31 29 20 74 68 65 20 72 6f 75 * (1) the rou
19922 6e 64 2d 74 72 69 70 20 63 6f 6e 76 65 72 73 69 nd-trip conversi
19923 6f 6e 20 72 65 61 6c 2d 3e 69 6e 74 2d 3e 72 65 on real->int->re
19924 61 6c 20 69 73 20 61 20 6e 6f 2d 6f 70 2c 20 61 al is a no-op, a
19925 6e 64 0a 20 20 2a 2a 20 20 20 20 28 32 29 20 54 nd. ** (2) T
19926 68 65 20 69 6e 74 65 67 65 72 20 69 73 20 6e 65 he integer is ne
19927 69 74 68 65 72 20 74 68 65 20 6c 61 72 67 65 73 ither the larges
19928 74 20 6e 6f 72 20 74 68 65 20 73 6d 61 6c 6c 65 t nor the smalle
19929 73 74 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 70 st. ** p
1992a 6f 73 73 69 62 6c 65 20 69 6e 74 65 67 65 72 20 ossible integer
1992b 28 74 69 63 6b 65 74 20 23 33 39 32 32 29 0a 20 (ticket #3922).
1992c 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 73 65 63 **. ** The sec
1992d 6f 6e 64 20 61 6e 64 20 74 68 69 72 64 20 74 65 ond and third te
1992e 72 6d 73 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f rms in the follo
1992f 77 69 6e 67 20 63 6f 6e 64 69 74 69 6f 6e 61 6c wing conditional
19930 20 65 6e 66 6f 72 63 65 73 0a 20 20 2a 2a 20 74 enforces. ** t
19931 68 65 20 73 65 63 6f 6e 64 20 63 6f 6e 64 69 74 he second condit
19932 69 6f 6e 20 75 6e 64 65 72 20 74 68 65 20 61 73 ion under the as
19933 73 75 6d 70 74 69 6f 6e 20 74 68 61 74 20 61 64 sumption that ad
19934 64 69 74 69 6f 6e 20 6f 76 65 72 66 6c 6f 77 20 dition overflow
19935 63 61 75 73 65 73 0a 20 20 2a 2a 20 76 61 6c 75 causes. ** valu
19936 65 73 20 74 6f 20 77 72 61 70 20 61 72 6f 75 6e es to wrap aroun
19937 64 2e 20 20 4f 6e 20 78 38 36 20 68 61 72 64 77 d. On x86 hardw
19938 61 72 65 2c 20 74 68 65 20 74 68 69 72 64 20 74 are, the third t
19939 65 72 6d 20 69 73 20 61 6c 77 61 79 73 0a 20 20 erm is always.
1993a 2a 2a 20 74 72 75 65 20 61 6e 64 20 63 6f 75 6c ** true and coul
1993b 64 20 62 65 20 6f 6d 69 74 74 65 64 2e 20 20 42 d be omitted. B
1993c 75 74 20 77 65 20 6c 65 61 76 65 20 69 74 20 69 ut we leave it i
1993d 6e 20 62 65 63 61 75 73 65 20 6f 74 68 65 72 0a n because other.
1993e 20 20 2a 2a 20 61 72 63 68 69 74 65 63 74 75 72 ** architectur
1993f 65 73 20 6d 69 67 68 74 20 62 65 68 61 76 65 20 es might behave
19940 64 69 66 66 65 72 65 6e 74 6c 79 2e 0a 20 20 2a differently.. *
19941 2f 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 72 3d /. if( pMem->r=
19942 3d 28 64 6f 75 62 6c 65 29 70 4d 65 6d 2d 3e 75 =(double)pMem->u
19943 2e 69 20 26 26 20 70 4d 65 6d 2d 3e 75 2e 69 3e .i && pMem->u.i>
19944 53 4d 41 4c 4c 45 53 54 5f 49 4e 54 36 34 0a 20 SMALLEST_INT64.
19945 20 20 20 20 20 26 26 20 41 4c 57 41 59 53 28 70 && ALWAYS(p
19946 4d 65 6d 2d 3e 75 2e 69 3c 4c 41 52 47 45 53 54 Mem->u.i<LARGEST
19947 5f 49 4e 54 36 34 29 20 29 7b 0a 20 20 20 20 70 _INT64) ){. p
19948 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 Mem->flags |= ME
19949 4d 5f 49 6e 74 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a M_Int;. }.}../*
1994a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 70 4d 65 6d .** Convert pMem
1994b 20 74 6f 20 74 79 70 65 20 69 6e 74 65 67 65 72 to type integer
1994c 2e 20 20 49 6e 76 61 6c 69 64 61 74 65 20 61 6e . Invalidate an
1994d 79 20 70 72 69 6f 72 20 72 65 70 72 65 73 65 6e y prior represen
1994e 74 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 tations..*/.SQLI
1994f 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
19950 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 qlite3VdbeMemInt
19951 65 67 65 72 69 66 79 28 4d 65 6d 20 2a 70 4d 65 egerify(Mem *pMe
19952 6d 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 4d m){. assert( pM
19953 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c em->db==0 || sql
19954 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
19955 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 pMem->db->mutex)
19956 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 );. assert( (p
19957 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d Mem->flags & MEM
19958 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20 _RowSet)==0 );.
19959 20 61 73 73 65 72 74 28 20 45 49 47 48 54 5f 42 assert( EIGHT_B
1995a 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 70 4d YTE_ALIGNMENT(pM
1995b 65 6d 29 20 29 3b 0a 0a 20 20 70 4d 65 6d 2d 3e em) );.. pMem->
1995c 75 2e 69 20 3d 20 73 71 6c 69 74 65 33 56 64 62 u.i = sqlite3Vdb
1995d 65 49 6e 74 56 61 6c 75 65 28 70 4d 65 6d 29 3b eIntValue(pMem);
1995e 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 . MemSetTypeFla
1995f 67 28 70 4d 65 6d 2c 20 4d 45 4d 5f 49 6e 74 29 g(pMem, MEM_Int)
19960 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
19961 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 E_OK;.}../*.** C
19962 6f 6e 76 65 72 74 20 70 4d 65 6d 20 73 6f 20 74 onvert pMem so t
19963 68 61 74 20 69 74 20 69 73 20 6f 66 20 74 79 70 hat it is of typ
19964 65 20 4d 45 4d 5f 52 65 61 6c 2e 0a 2a 2a 20 49 e MEM_Real..** I
19965 6e 76 61 6c 69 64 61 74 65 20 61 6e 79 20 70 72 nvalidate any pr
19966 69 6f 72 20 72 65 70 72 65 73 65 6e 74 61 74 69 ior representati
19967 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ons..*/.SQLITE_P
19968 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
19969 65 33 56 64 62 65 4d 65 6d 52 65 61 6c 69 66 79 e3VdbeMemRealify
1996a 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 61 (Mem *pMem){. a
1996b 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d ssert( pMem->db=
1996c 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 =0 || sqlite3_mu
1996d 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 tex_held(pMem->d
1996e 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 b->mutex) );. a
1996f 73 73 65 72 74 28 20 45 49 47 48 54 5f 42 59 54 ssert( EIGHT_BYT
19970 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 70 4d 65 6d E_ALIGNMENT(pMem
19971 29 20 29 3b 0a 0a 20 20 70 4d 65 6d 2d 3e 72 20 ) );.. pMem->r
19972 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 61 = sqlite3VdbeRea
19973 6c 56 61 6c 75 65 28 70 4d 65 6d 29 3b 0a 20 20 lValue(pMem);.
19974 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 MemSetTypeFlag(p
19975 4d 65 6d 2c 20 4d 45 4d 5f 52 65 61 6c 29 3b 0a Mem, MEM_Real);.
19976 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
19977 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e OK;.}../*.** Con
19978 76 65 72 74 20 70 4d 65 6d 20 73 6f 20 74 68 61 vert pMem so tha
19979 74 20 69 74 20 68 61 73 20 74 79 70 65 73 20 4d t it has types M
1997a 45 4d 5f 52 65 61 6c 20 6f 72 20 4d 45 4d 5f 49 EM_Real or MEM_I
1997b 6e 74 20 6f 72 20 62 6f 74 68 2e 0a 2a 2a 20 49 nt or both..** I
1997c 6e 76 61 6c 69 64 61 74 65 20 61 6e 79 20 70 72 nvalidate any pr
1997d 69 6f 72 20 72 65 70 72 65 73 65 6e 74 61 74 69 ior representati
1997e 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ons..*/.SQLITE_P
1997f 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
19980 65 33 56 64 62 65 4d 65 6d 4e 75 6d 65 72 69 66 e3VdbeMemNumerif
19981 79 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 y(Mem *pMem){.
19982 64 6f 75 62 6c 65 20 72 31 2c 20 72 32 3b 0a 20 double r1, r2;.
19983 20 69 36 34 20 69 3b 0a 20 20 61 73 73 65 72 74 i64 i;. assert
19984 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 ( (pMem->flags &
19985 20 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 (MEM_Int|MEM_Re
19986 61 6c 7c 4d 45 4d 5f 4e 75 6c 6c 29 29 3d 3d 30 al|MEM_Null))==0
19987 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 );. assert( (p
19988 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 Mem->flags & (ME
19989 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 53 74 72 29 29 M_Blob|MEM_Str))
1998a 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 !=0 );. assert(
1998b 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 pMem->db==0 ||
1998c 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
1998d 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 ld(pMem->db->mut
1998e 65 78 29 20 29 3b 0a 20 20 72 31 20 3d 20 73 71 ex) );. r1 = sq
1998f 6c 69 74 65 33 56 64 62 65 52 65 61 6c 56 61 6c lite3VdbeRealVal
19990 75 65 28 70 4d 65 6d 29 3b 0a 20 20 69 20 3d 20 ue(pMem);. i =
19991 64 6f 75 62 6c 65 54 6f 49 6e 74 36 34 28 72 31 doubleToInt64(r1
19992 29 3b 0a 20 20 72 32 20 3d 20 28 64 6f 75 62 6c );. r2 = (doubl
19993 65 29 69 3b 0a 20 20 69 66 28 20 72 31 3d 3d 72 e)i;. if( r1==r
19994 32 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 2 ){. sqlite3
19995 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 VdbeMemIntegerif
19996 79 28 70 4d 65 6d 29 3b 0a 20 20 7d 65 6c 73 65 y(pMem);. }else
19997 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 72 20 3d 20 {. pMem->r =
19998 72 31 3b 0a 20 20 20 20 4d 65 6d 53 65 74 54 79 r1;. MemSetTy
19999 70 65 46 6c 61 67 28 70 4d 65 6d 2c 20 4d 45 4d peFlag(pMem, MEM
1999a 5f 52 65 61 6c 29 3b 0a 20 20 7d 0a 20 20 72 65 _Real);. }. re
1999b 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
1999c 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 }../*.** Delete
1999d 61 6e 79 20 70 72 65 76 69 6f 75 73 20 76 61 6c any previous val
1999e 75 65 20 61 6e 64 20 73 65 74 20 74 68 65 20 76 ue and set the v
1999f 61 6c 75 65 20 73 74 6f 72 65 64 20 69 6e 20 2a alue stored in *
199a0 70 4d 65 6d 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f pMem to NULL..*/
199a1 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
199a2 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
199a3 4d 65 6d 53 65 74 4e 75 6c 6c 28 4d 65 6d 20 2a MemSetNull(Mem *
199a4 70 4d 65 6d 29 7b 0a 20 20 69 66 28 20 70 4d 65 pMem){. if( pMe
199a5 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 46 m->flags & MEM_F
199a6 72 61 6d 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 rame ){. sqli
199a7 74 65 33 56 64 62 65 46 72 61 6d 65 44 65 6c 65 te3VdbeFrameDele
199a8 74 65 28 70 4d 65 6d 2d 3e 75 2e 70 46 72 61 6d te(pMem->u.pFram
199a9 65 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4d e);. }. if( pM
199aa 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f em->flags & MEM_
199ab 52 6f 77 53 65 74 20 29 7b 0a 20 20 20 20 73 71 RowSet ){. sq
199ac 6c 69 74 65 33 52 6f 77 53 65 74 43 6c 65 61 72 lite3RowSetClear
199ad 28 70 4d 65 6d 2d 3e 75 2e 70 52 6f 77 53 65 74 (pMem->u.pRowSet
199ae 29 3b 0a 20 20 7d 0a 20 20 4d 65 6d 53 65 74 54 );. }. MemSetT
199af 79 70 65 46 6c 61 67 28 70 4d 65 6d 2c 20 4d 45 ypeFlag(pMem, ME
199b0 4d 5f 4e 75 6c 6c 29 3b 0a 20 20 70 4d 65 6d 2d M_Null);. pMem-
199b1 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 4e >type = SQLITE_N
199b2 55 4c 4c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 ULL;.}../*.** De
199b3 6c 65 74 65 20 61 6e 79 20 70 72 65 76 69 6f 75 lete any previou
199b4 73 20 76 61 6c 75 65 20 61 6e 64 20 73 65 74 20 s value and set
199b5 74 68 65 20 76 61 6c 75 65 20 74 6f 20 62 65 20 the value to be
199b6 61 20 42 4c 4f 42 20 6f 66 20 6c 65 6e 67 74 68 a BLOB of length
199b7 0a 2a 2a 20 6e 20 63 6f 6e 74 61 69 6e 69 6e 67 .** n containing
199b8 20 61 6c 6c 20 7a 65 72 6f 73 2e 0a 2a 2f 0a 53 all zeros..*/.S
199b9 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
199ba 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 id sqlite3VdbeMe
199bb 6d 53 65 74 5a 65 72 6f 42 6c 6f 62 28 4d 65 6d mSetZeroBlob(Mem
199bc 20 2a 70 4d 65 6d 2c 20 69 6e 74 20 6e 29 7b 0a *pMem, int n){.
199bd 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
199be 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 Release(pMem);.
199bf 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d pMem->flags = M
199c0 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 5a 65 72 6f EM_Blob|MEM_Zero
199c1 3b 0a 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d ;. pMem->type =
199c2 20 53 51 4c 49 54 45 5f 42 4c 4f 42 3b 0a 20 20 SQLITE_BLOB;.
199c3 70 4d 65 6d 2d 3e 6e 20 3d 20 30 3b 0a 20 20 69 pMem->n = 0;. i
199c4 66 28 20 6e 3c 30 20 29 20 6e 20 3d 20 30 3b 0a f( n<0 ) n = 0;.
199c5 20 20 70 4d 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 20 pMem->u.nZero
199c6 3d 20 6e 3b 0a 20 20 70 4d 65 6d 2d 3e 65 6e 63 = n;. pMem->enc
199c7 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a = SQLITE_UTF8;.
199c8 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f .#ifdef SQLITE_O
199c9 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 73 MIT_INCRBLOB. s
199ca 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f qlite3VdbeMemGro
199cb 77 28 70 4d 65 6d 2c 20 6e 2c 20 30 29 3b 0a 20 w(pMem, n, 0);.
199cc 20 69 66 28 20 70 4d 65 6d 2d 3e 7a 20 29 7b 0a if( pMem->z ){.
199cd 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 6e 3b pMem->n = n;
199ce 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 4d 65 6d . memset(pMem
199cf 2d 3e 7a 2c 20 30 2c 20 6e 29 3b 0a 20 20 7d 0a ->z, 0, n);. }.
199d0 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 #endif.}../*.**
199d1 44 65 6c 65 74 65 20 61 6e 79 20 70 72 65 76 69 Delete any previ
199d2 6f 75 73 20 76 61 6c 75 65 20 61 6e 64 20 73 65 ous value and se
199d3 74 20 74 68 65 20 76 61 6c 75 65 20 73 74 6f 72 t the value stor
199d4 65 64 20 69 6e 20 2a 70 4d 65 6d 20 74 6f 20 76 ed in *pMem to v
199d5 61 6c 2c 0a 2a 2a 20 6d 61 6e 69 66 65 73 74 20 al,.** manifest
199d6 74 79 70 65 20 49 4e 54 45 47 45 52 2e 0a 2a 2f type INTEGER..*/
199d7 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
199d8 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
199d9 4d 65 6d 53 65 74 49 6e 74 36 34 28 4d 65 6d 20 MemSetInt64(Mem
199da 2a 70 4d 65 6d 2c 20 69 36 34 20 76 61 6c 29 7b *pMem, i64 val){
199db 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 . sqlite3VdbeMe
199dc 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a mRelease(pMem);.
199dd 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 76 61 pMem->u.i = va
199de 6c 3b 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 l;. pMem->flags
199df 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 70 4d = MEM_Int;. pM
199e0 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 em->type = SQLIT
199e1 45 5f 49 4e 54 45 47 45 52 3b 0a 7d 0a 0a 2f 2a E_INTEGER;.}../*
199e2 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 79 20 70 .** Delete any p
199e3 72 65 76 69 6f 75 73 20 76 61 6c 75 65 20 61 6e revious value an
199e4 64 20 73 65 74 20 74 68 65 20 76 61 6c 75 65 20 d set the value
199e5 73 74 6f 72 65 64 20 69 6e 20 2a 70 4d 65 6d 20 stored in *pMem
199e6 74 6f 20 76 61 6c 2c 0a 2a 2a 20 6d 61 6e 69 66 to val,.** manif
199e7 65 73 74 20 74 79 70 65 20 52 45 41 4c 2e 0a 2a est type REAL..*
199e8 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
199e9 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
199ea 65 4d 65 6d 53 65 74 44 6f 75 62 6c 65 28 4d 65 eMemSetDouble(Me
199eb 6d 20 2a 70 4d 65 6d 2c 20 64 6f 75 62 6c 65 20 m *pMem, double
199ec 76 61 6c 29 7b 0a 20 20 69 66 28 20 73 71 6c 69 val){. if( sqli
199ed 74 65 33 49 73 4e 61 4e 28 76 61 6c 29 20 29 7b te3IsNaN(val) ){
199ee 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
199ef 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 4d 65 6d 29 MemSetNull(pMem)
199f0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 ;. }else{. s
199f1 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c qlite3VdbeMemRel
199f2 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20 ease(pMem);.
199f3 70 4d 65 6d 2d 3e 72 20 3d 20 76 61 6c 3b 0a 20 pMem->r = val;.
199f4 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d pMem->flags =
199f5 20 4d 45 4d 5f 52 65 61 6c 3b 0a 20 20 20 20 70 MEM_Real;. p
199f6 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 Mem->type = SQLI
199f7 54 45 5f 46 4c 4f 41 54 3b 0a 20 20 7d 0a 7d 0a TE_FLOAT;. }.}.
199f8 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e ./*.** Delete an
199f9 79 20 70 72 65 76 69 6f 75 73 20 76 61 6c 75 65 y previous value
199fa 20 61 6e 64 20 73 65 74 20 74 68 65 20 76 61 6c and set the val
199fb 75 65 20 6f 66 20 70 4d 65 6d 20 74 6f 20 62 65 ue of pMem to be
199fc 20 61 6e 0a 2a 2a 20 65 6d 70 74 79 20 62 6f 6f an.** empty boo
199fd 6c 65 61 6e 20 69 6e 64 65 78 2e 0a 2a 2f 0a 53 lean index..*/.S
199fe 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
199ff 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 id sqlite3VdbeMe
19a00 6d 53 65 74 52 6f 77 53 65 74 28 4d 65 6d 20 2a mSetRowSet(Mem *
19a01 70 4d 65 6d 29 7b 0a 20 20 73 71 6c 69 74 65 33 pMem){. sqlite3
19a02 20 2a 64 62 20 3d 20 70 4d 65 6d 2d 3e 64 62 3b *db = pMem->db;
19a03 0a 20 20 61 73 73 65 72 74 28 20 64 62 21 3d 30 . assert( db!=0
19a04 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 );. assert( (p
19a05 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d Mem->flags & MEM
19a06 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20 _RowSet)==0 );.
19a07 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 sqlite3VdbeMemR
19a08 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 elease(pMem);.
19a09 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 pMem->zMalloc =
19a0a 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 sqlite3DbMallocR
19a0b 61 77 28 64 62 2c 20 36 34 29 3b 0a 20 20 69 66 aw(db, 64);. if
19a0c 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c ( db->mallocFail
19a0d 65 64 20 29 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e ed ){. pMem->
19a0e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c flags = MEM_Null
19a0f 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 ;. }else{. a
19a10 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 7a 4d 61 ssert( pMem->zMa
19a11 6c 6c 6f 63 20 29 3b 0a 20 20 20 20 70 4d 65 6d lloc );. pMem
19a12 2d 3e 75 2e 70 52 6f 77 53 65 74 20 3d 20 73 71 ->u.pRowSet = sq
19a13 6c 69 74 65 33 52 6f 77 53 65 74 49 6e 69 74 28 lite3RowSetInit(
19a14 64 62 2c 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f db, pMem->zMallo
19a15 63 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 c, .
19a16 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19a17 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 sqlit
19a18 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 64 e3DbMallocSize(d
19a19 62 2c 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 b, pMem->zMalloc
19a1a 29 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ));. assert(
19a1b 70 4d 65 6d 2d 3e 75 2e 70 52 6f 77 53 65 74 21 pMem->u.pRowSet!
19a1c 3d 30 20 29 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e =0 );. pMem->
19a1d 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 52 6f 77 53 flags = MEM_RowS
19a1e 65 74 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a et;. }.}../*.**
19a1f 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 Return true if
19a20 74 68 65 20 4d 65 6d 20 6f 62 6a 65 63 74 20 63 the Mem object c
19a21 6f 6e 74 61 69 6e 73 20 61 20 54 45 58 54 20 6f ontains a TEXT o
19a22 72 20 42 4c 4f 42 20 74 68 61 74 20 69 73 0a 2a r BLOB that is.*
19a23 2a 20 74 6f 6f 20 6c 61 72 67 65 20 2d 20 77 68 * too large - wh
19a24 6f 73 65 20 73 69 7a 65 20 65 78 63 65 65 64 73 ose size exceeds
19a25 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 SQLITE_MAX_LENG
19a26 54 48 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 TH..*/.SQLITE_PR
19a27 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
19a28 33 56 64 62 65 4d 65 6d 54 6f 6f 42 69 67 28 4d 3VdbeMemTooBig(M
19a29 65 6d 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 em *p){. assert
19a2a 28 20 70 2d 3e 64 62 21 3d 30 20 29 3b 0a 20 20 ( p->db!=0 );.
19a2b 69 66 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 28 if( p->flags & (
19a2c 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 MEM_Str|MEM_Blob
19a2d 29 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 20 3d ) ){. int n =
19a2e 20 70 2d 3e 6e 3b 0a 20 20 20 20 69 66 28 20 70 p->n;. if( p
19a2f 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 ->flags & MEM_Ze
19a30 72 6f 20 29 7b 0a 20 20 20 20 20 20 6e 20 2b 3d ro ){. n +=
19a31 20 70 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 20 p->u.nZero;.
19a32 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 6e 3e }. return n>
19a33 70 2d 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 p->db->aLimit[SQ
19a34 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 LITE_LIMIT_LENGT
19a35 48 5d 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e H];. }. return
19a36 20 30 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 69 0; .}../*.** Si
19a37 7a 65 20 6f 66 20 73 74 72 75 63 74 20 4d 65 6d ze of struct Mem
19a38 20 6e 6f 74 20 69 6e 63 6c 75 64 69 6e 67 20 74 not including t
19a39 68 65 20 4d 65 6d 2e 7a 4d 61 6c 6c 6f 63 20 6d he Mem.zMalloc m
19a3a 65 6d 62 65 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e ember..*/.#defin
19a3b 65 20 4d 45 4d 43 45 4c 4c 53 49 5a 45 20 28 73 e MEMCELLSIZE (s
19a3c 69 7a 65 5f 74 29 28 26 28 28 28 4d 65 6d 20 2a ize_t)(&(((Mem *
19a3d 29 30 29 2d 3e 7a 4d 61 6c 6c 6f 63 29 29 0a 0a )0)->zMalloc))..
19a3e 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 61 6e 20 73 68 /*.** Make an sh
19a3f 61 6c 6c 6f 77 20 63 6f 70 79 20 6f 66 20 70 46 allow copy of pF
19a40 72 6f 6d 20 69 6e 74 6f 20 70 54 6f 2e 20 20 50 rom into pTo. P
19a41 72 69 6f 72 20 63 6f 6e 74 65 6e 74 73 20 6f 66 rior contents of
19a42 0a 2a 2a 20 70 54 6f 20 61 72 65 20 66 72 65 65 .** pTo are free
19a43 64 2e 20 20 54 68 65 20 70 46 72 6f 6d 2d 3e 7a d. The pFrom->z
19a44 20 66 69 65 6c 64 20 69 73 20 6e 6f 74 20 64 75 field is not du
19a45 70 6c 69 63 61 74 65 64 2e 20 20 49 66 0a 2a 2a plicated. If.**
19a46 20 70 46 72 6f 6d 2d 3e 7a 20 69 73 20 75 73 65 pFrom->z is use
19a47 64 2c 20 74 68 65 6e 20 70 54 6f 2d 3e 7a 20 70 d, then pTo->z p
19a48 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 73 61 6d oints to the sam
19a49 65 20 74 68 69 6e 67 20 61 73 20 70 46 72 6f 6d e thing as pFrom
19a4a 2d 3e 7a 0a 2a 2a 20 61 6e 64 20 66 6c 61 67 73 ->z.** and flags
19a4b 20 67 65 74 73 20 73 72 63 54 79 70 65 20 28 65 gets srcType (e
19a4c 69 74 68 65 72 20 4d 45 4d 5f 45 70 68 65 6d 20 ither MEM_Ephem
19a4d 6f 72 20 4d 45 4d 5f 53 74 61 74 69 63 29 2e 0a or MEM_Static)..
19a4e 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
19a4f 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 E void sqlite3Vd
19a50 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 beMemShallowCopy
19a51 28 4d 65 6d 20 2a 70 54 6f 2c 20 63 6f 6e 73 74 (Mem *pTo, const
19a52 20 4d 65 6d 20 2a 70 46 72 6f 6d 2c 20 69 6e 74 Mem *pFrom, int
19a53 20 73 72 63 54 79 70 65 29 7b 0a 20 20 61 73 73 srcType){. ass
19a54 65 72 74 28 20 28 70 46 72 6f 6d 2d 3e 66 6c 61 ert( (pFrom->fla
19a55 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 gs & MEM_RowSet)
19a56 3d 3d 30 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 ==0 );. sqlite3
19a57 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 45 78 VdbeMemReleaseEx
19a58 74 65 72 6e 61 6c 28 70 54 6f 29 3b 0a 20 20 6d ternal(pTo);. m
19a59 65 6d 63 70 79 28 70 54 6f 2c 20 70 46 72 6f 6d emcpy(pTo, pFrom
19a5a 2c 20 4d 45 4d 43 45 4c 4c 53 49 5a 45 29 3b 0a , MEMCELLSIZE);.
19a5b 20 20 70 54 6f 2d 3e 78 44 65 6c 20 3d 20 30 3b pTo->xDel = 0;
19a5c 0a 20 20 69 66 28 20 28 70 46 72 6f 6d 2d 3e 66 . if( (pFrom->f
19a5d 6c 61 67 73 26 4d 45 4d 5f 44 79 6e 29 21 3d 30 lags&MEM_Dyn)!=0
19a5e 20 7c 7c 20 70 46 72 6f 6d 2d 3e 7a 3d 3d 70 46 || pFrom->z==pF
19a5f 72 6f 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 29 7b 0a rom->zMalloc ){.
19a60 20 20 20 20 70 54 6f 2d 3e 66 6c 61 67 73 20 26 pTo->flags &
19a61 3d 20 7e 28 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f = ~(MEM_Dyn|MEM_
19a62 53 74 61 74 69 63 7c 4d 45 4d 5f 45 70 68 65 6d Static|MEM_Ephem
19a63 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 );. assert( s
19a64 72 63 54 79 70 65 3d 3d 4d 45 4d 5f 45 70 68 65 rcType==MEM_Ephe
19a65 6d 20 7c 7c 20 73 72 63 54 79 70 65 3d 3d 4d 45 m || srcType==ME
19a66 4d 5f 53 74 61 74 69 63 20 29 3b 0a 20 20 20 20 M_Static );.
19a67 70 54 6f 2d 3e 66 6c 61 67 73 20 7c 3d 20 73 72 pTo->flags |= sr
19a68 63 54 79 70 65 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a cType;. }.}../*
19a69 0a 2a 2a 20 4d 61 6b 65 20 61 20 66 75 6c 6c 20 .** Make a full
19a6a 63 6f 70 79 20 6f 66 20 70 46 72 6f 6d 20 69 6e copy of pFrom in
19a6b 74 6f 20 70 54 6f 2e 20 20 50 72 69 6f 72 20 63 to pTo. Prior c
19a6c 6f 6e 74 65 6e 74 73 20 6f 66 20 70 54 6f 20 61 ontents of pTo a
19a6d 72 65 0a 2a 2a 20 66 72 65 65 64 20 62 65 66 6f re.** freed befo
19a6e 72 65 20 74 68 65 20 63 6f 70 79 20 69 73 20 6d re the copy is m
19a6f 61 64 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ade..*/.SQLITE_P
19a70 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
19a71 65 33 56 64 62 65 4d 65 6d 43 6f 70 79 28 4d 65 e3VdbeMemCopy(Me
19a72 6d 20 2a 70 54 6f 2c 20 63 6f 6e 73 74 20 4d 65 m *pTo, const Me
19a73 6d 20 2a 70 46 72 6f 6d 29 7b 0a 20 20 69 6e 74 m *pFrom){. int
19a74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
19a75 0a 0a 20 20 61 73 73 65 72 74 28 20 28 70 46 72 .. assert( (pFr
19a76 6f 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f om->flags & MEM_
19a77 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 RowSet)==0 );.
19a78 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 sqlite3VdbeMemRe
19a79 6c 65 61 73 65 45 78 74 65 72 6e 61 6c 28 70 54 leaseExternal(pT
19a7a 6f 29 3b 0a 20 20 6d 65 6d 63 70 79 28 70 54 6f o);. memcpy(pTo
19a7b 2c 20 70 46 72 6f 6d 2c 20 4d 45 4d 43 45 4c 4c , pFrom, MEMCELL
19a7c 53 49 5a 45 29 3b 0a 20 20 70 54 6f 2d 3e 66 6c SIZE);. pTo->fl
19a7d 61 67 73 20 26 3d 20 7e 4d 45 4d 5f 44 79 6e 3b ags &= ~MEM_Dyn;
19a7e 0a 0a 20 20 69 66 28 20 70 54 6f 2d 3e 66 6c 61 .. if( pTo->fla
19a7f 67 73 26 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f gs&(MEM_Str|MEM_
19a80 42 6c 6f 62 29 20 29 7b 0a 20 20 20 20 69 66 28 Blob) ){. if(
19a81 20 30 3d 3d 28 70 46 72 6f 6d 2d 3e 66 6c 61 67 0==(pFrom->flag
19a82 73 26 4d 45 4d 5f 53 74 61 74 69 63 29 20 29 7b s&MEM_Static) ){
19a83 0a 20 20 20 20 20 20 70 54 6f 2d 3e 66 6c 61 67 . pTo->flag
19a84 73 20 7c 3d 20 4d 45 4d 5f 45 70 68 65 6d 3b 0a s |= MEM_Ephem;.
19a85 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
19a86 65 33 56 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69 e3VdbeMemMakeWri
19a87 74 65 61 62 6c 65 28 70 54 6f 29 3b 0a 20 20 20 teable(pTo);.
19a88 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e }. }.. return
19a89 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 rc;.}../*.** Tr
19a8a 61 6e 73 66 65 72 20 74 68 65 20 63 6f 6e 74 65 ansfer the conte
19a8b 6e 74 73 20 6f 66 20 70 46 72 6f 6d 20 74 6f 20 nts of pFrom to
19a8c 70 54 6f 2e 20 41 6e 79 20 65 78 69 73 74 69 6e pTo. Any existin
19a8d 67 20 76 61 6c 75 65 20 69 6e 20 70 54 6f 20 69 g value in pTo i
19a8e 73 0a 2a 2a 20 66 72 65 65 64 2e 20 49 66 20 70 s.** freed. If p
19a8f 46 72 6f 6d 20 63 6f 6e 74 61 69 6e 73 20 65 70 From contains ep
19a90 68 65 6d 65 72 61 6c 20 64 61 74 61 2c 20 61 20 hemeral data, a
19a91 63 6f 70 79 20 69 73 20 6d 61 64 65 2e 0a 2a 2a copy is made..**
19a92 0a 2a 2a 20 70 46 72 6f 6d 20 63 6f 6e 74 61 69 .** pFrom contai
19a93 6e 73 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 20 77 ns an SQL NULL w
19a94 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 hen this routine
19a95 20 72 65 74 75 72 6e 73 2e 0a 2a 2f 0a 53 51 4c returns..*/.SQL
19a96 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
19a97 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d sqlite3VdbeMemM
19a98 6f 76 65 28 4d 65 6d 20 2a 70 54 6f 2c 20 4d 65 ove(Mem *pTo, Me
19a99 6d 20 2a 70 46 72 6f 6d 29 7b 0a 20 20 61 73 73 m *pFrom){. ass
19a9a 65 72 74 28 20 70 46 72 6f 6d 2d 3e 64 62 3d 3d ert( pFrom->db==
19a9b 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 0 || sqlite3_mut
19a9c 65 78 5f 68 65 6c 64 28 70 46 72 6f 6d 2d 3e 64 ex_held(pFrom->d
19a9d 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 b->mutex) );. a
19a9e 73 73 65 72 74 28 20 70 54 6f 2d 3e 64 62 3d 3d ssert( pTo->db==
19a9f 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 0 || sqlite3_mut
19aa0 65 78 5f 68 65 6c 64 28 70 54 6f 2d 3e 64 62 2d ex_held(pTo->db-
19aa1 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 >mutex) );. ass
19aa2 65 72 74 28 20 70 46 72 6f 6d 2d 3e 64 62 3d 3d ert( pFrom->db==
19aa3 30 20 7c 7c 20 70 54 6f 2d 3e 64 62 3d 3d 30 20 0 || pTo->db==0
19aa4 7c 7c 20 70 46 72 6f 6d 2d 3e 64 62 3d 3d 70 54 || pFrom->db==pT
19aa5 6f 2d 3e 64 62 20 29 3b 0a 0a 20 20 73 71 6c 69 o->db );.. sqli
19aa6 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 te3VdbeMemReleas
19aa7 65 28 70 54 6f 29 3b 0a 20 20 6d 65 6d 63 70 79 e(pTo);. memcpy
19aa8 28 70 54 6f 2c 20 70 46 72 6f 6d 2c 20 73 69 7a (pTo, pFrom, siz
19aa9 65 6f 66 28 4d 65 6d 29 29 3b 0a 20 20 70 46 72 eof(Mem));. pFr
19aaa 6f 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f om->flags = MEM_
19aab 4e 75 6c 6c 3b 0a 20 20 70 46 72 6f 6d 2d 3e 78 Null;. pFrom->x
19aac 44 65 6c 20 3d 20 30 3b 0a 20 20 70 46 72 6f 6d Del = 0;. pFrom
19aad 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 7d ->zMalloc = 0;.}
19aae 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 ../*.** Change t
19aaf 68 65 20 76 61 6c 75 65 20 6f 66 20 61 20 4d 65 he value of a Me
19ab0 6d 20 74 6f 20 62 65 20 61 20 73 74 72 69 6e 67 m to be a string
19ab1 20 6f 72 20 61 20 42 4c 4f 42 2e 0a 2a 2a 0a 2a or a BLOB..**.*
19ab2 2a 20 54 68 65 20 6d 65 6d 6f 72 79 20 6d 61 6e * The memory man
19ab3 61 67 65 6d 65 6e 74 20 73 74 72 61 74 65 67 79 agement strategy
19ab4 20 64 65 70 65 6e 64 73 20 6f 6e 20 74 68 65 20 depends on the
19ab5 76 61 6c 75 65 20 6f 66 20 74 68 65 20 78 44 65 value of the xDe
19ab6 6c 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 2e 20 l.** parameter.
19ab7 49 66 20 74 68 65 20 76 61 6c 75 65 20 70 61 73 If the value pas
19ab8 73 65 64 20 69 73 20 53 51 4c 49 54 45 5f 54 52 sed is SQLITE_TR
19ab9 41 4e 53 49 45 4e 54 2c 20 74 68 65 6e 20 74 68 ANSIENT, then th
19aba 65 20 0a 2a 2a 20 73 74 72 69 6e 67 20 69 73 20 e .** string is
19abb 63 6f 70 69 65 64 20 69 6e 74 6f 20 61 20 28 70 copied into a (p
19abc 6f 73 73 69 62 6c 79 20 65 78 69 73 74 69 6e 67 ossibly existing
19abd 29 20 62 75 66 66 65 72 20 6d 61 6e 61 67 65 64 ) buffer managed
19abe 20 62 79 20 74 68 65 20 0a 2a 2a 20 4d 65 6d 20 by the .** Mem
19abf 73 74 72 75 63 74 75 72 65 2e 20 4f 74 68 65 72 structure. Other
19ac0 77 69 73 65 2c 20 61 6e 79 20 65 78 69 73 74 69 wise, any existi
19ac1 6e 67 20 62 75 66 66 65 72 20 69 73 20 66 72 65 ng buffer is fre
19ac2 65 64 20 61 6e 64 20 74 68 65 0a 2a 2a 20 70 6f ed and the.** po
19ac3 69 6e 74 65 72 20 63 6f 70 69 65 64 2e 0a 2a 2a inter copied..**
19ac4 0a 2a 2a 20 49 66 20 74 68 65 20 73 74 72 69 6e .** If the strin
19ac5 67 20 69 73 20 74 6f 6f 20 6c 61 72 67 65 20 28 g is too large (
19ac6 69 66 20 69 74 20 65 78 63 65 65 64 73 20 74 68 if it exceeds th
19ac7 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c e SQLITE_LIMIT_L
19ac8 45 4e 47 54 48 0a 2a 2a 20 73 69 7a 65 20 6c 69 ENGTH.** size li
19ac9 6d 69 74 29 20 74 68 65 6e 20 6e 6f 20 6d 65 6d mit) then no mem
19aca 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f ory allocation o
19acb 63 63 75 72 73 2e 20 20 49 66 20 74 68 65 20 73 ccurs. If the s
19acc 74 72 69 6e 67 20 63 61 6e 20 62 65 0a 2a 2a 20 tring can be.**
19acd 73 74 6f 72 65 64 20 77 69 74 68 6f 75 74 20 61 stored without a
19ace 6c 6c 6f 63 61 74 69 6e 67 20 6d 65 6d 6f 72 79 llocating memory
19acf 2c 20 74 68 65 6e 20 69 74 20 69 73 2e 20 20 49 , then it is. I
19ad0 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 f a memory alloc
19ad1 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 72 65 71 75 ation.** is requ
19ad2 69 72 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68 ired to store th
19ad3 65 20 73 74 72 69 6e 67 2c 20 74 68 65 6e 20 76 e string, then v
19ad4 61 6c 75 65 20 6f 66 20 70 4d 65 6d 20 69 73 20 alue of pMem is
19ad5 75 6e 63 68 61 6e 67 65 64 2e 20 20 49 6e 0a 2a unchanged. In.*
19ad6 2a 20 65 69 74 68 65 72 20 63 61 73 65 2c 20 53 * either case, S
19ad7 51 4c 49 54 45 5f 54 4f 4f 42 49 47 20 69 73 20 QLITE_TOOBIG is
19ad8 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c returned..*/.SQL
19ad9 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
19ada 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 sqlite3VdbeMemSe
19adb 74 53 74 72 28 0a 20 20 4d 65 6d 20 2a 70 4d 65 tStr(. Mem *pMe
19adc 6d 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d m, /* M
19add 65 6d 6f 72 79 20 63 65 6c 6c 20 74 6f 20 73 65 emory cell to se
19ade 74 20 74 6f 20 73 74 72 69 6e 67 20 76 61 6c 75 t to string valu
19adf 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 e */. const cha
19ae0 72 20 2a 7a 2c 20 20 20 20 20 20 2f 2a 20 53 74 r *z, /* St
19ae1 72 69 6e 67 20 70 6f 69 6e 74 65 72 20 2a 2f 0a ring pointer */.
19ae2 20 20 69 6e 74 20 6e 2c 20 20 20 20 20 20 20 20 int n,
19ae3 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 69 /* Bytes i
19ae4 6e 20 73 74 72 69 6e 67 2c 20 6f 72 20 6e 65 67 n string, or neg
19ae5 61 74 69 76 65 20 2a 2f 0a 20 20 75 38 20 65 6e ative */. u8 en
19ae6 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f c, /
19ae7 2a 20 45 6e 63 6f 64 69 6e 67 20 6f 66 20 7a 2e * Encoding of z.
19ae8 20 20 30 20 66 6f 72 20 42 4c 4f 42 73 20 2a 2f 0 for BLOBs */
19ae9 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 . void (*xDel)(
19aea 76 6f 69 64 2a 29 20 2f 2a 20 44 65 73 74 72 75 void*) /* Destru
19aeb 63 74 6f 72 20 66 75 6e 63 74 69 6f 6e 20 2a 2f ctor function */
19aec 0a 29 7b 0a 20 20 69 6e 74 20 6e 42 79 74 65 20 .){. int nByte
19aed 3d 20 6e 3b 20 20 20 20 20 20 2f 2a 20 4e 65 77 = n; /* New
19aee 20 76 61 6c 75 65 20 66 6f 72 20 70 4d 65 6d 2d value for pMem-
19aef 3e 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 4c 69 6d >n */. int iLim
19af0 69 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4d it; /* M
19af1 61 78 69 6d 75 6d 20 61 6c 6c 6f 77 65 64 20 73 aximum allowed s
19af2 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 73 69 tring or blob si
19af3 7a 65 20 2a 2f 0a 20 20 75 31 36 20 66 6c 61 67 ze */. u16 flag
19af4 73 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 4e s = 0; /* N
19af5 65 77 20 76 61 6c 75 65 20 66 6f 72 20 70 4d 65 ew value for pMe
19af6 6d 2d 3e 66 6c 61 67 73 20 2a 2f 0a 0a 20 20 61 m->flags */.. a
19af7 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d ssert( pMem->db=
19af8 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 =0 || sqlite3_mu
19af9 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 tex_held(pMem->d
19afa 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 b->mutex) );. a
19afb 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c ssert( (pMem->fl
19afc 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 ags & MEM_RowSet
19afd 29 3d 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 )==0 );.. /* If
19afe 20 7a 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 z is a NULL poi
19aff 6e 74 65 72 2c 20 73 65 74 20 70 4d 65 6d 20 74 nter, set pMem t
19b00 6f 20 63 6f 6e 74 61 69 6e 20 61 6e 20 53 51 4c o contain an SQL
19b01 20 4e 55 4c 4c 2e 20 2a 2f 0a 20 20 69 66 28 20 NULL. */. if(
19b02 21 7a 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 !z ){. sqlite
19b03 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 3VdbeMemSetNull(
19b04 70 4d 65 6d 29 3b 0a 20 20 20 20 72 65 74 75 72 pMem);. retur
19b05 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d n SQLITE_OK;. }
19b06 0a 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 64 62 .. if( pMem->db
19b07 20 29 7b 0a 20 20 20 20 69 4c 69 6d 69 74 20 3d ){. iLimit =
19b08 20 70 4d 65 6d 2d 3e 64 62 2d 3e 61 4c 69 6d 69 pMem->db->aLimi
19b09 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c t[SQLITE_LIMIT_L
19b0a 45 4e 47 54 48 5d 3b 0a 20 20 7d 65 6c 73 65 7b ENGTH];. }else{
19b0b 0a 20 20 20 20 69 4c 69 6d 69 74 20 3d 20 53 51 . iLimit = SQ
19b0c 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 3b LITE_MAX_LENGTH;
19b0d 0a 20 20 7d 0a 20 20 66 6c 61 67 73 20 3d 20 28 . }. flags = (
19b0e 65 6e 63 3d 3d 30 3f 4d 45 4d 5f 42 6c 6f 62 3a enc==0?MEM_Blob:
19b0f 4d 45 4d 5f 53 74 72 29 3b 0a 20 20 69 66 28 20 MEM_Str);. if(
19b10 6e 42 79 74 65 3c 30 20 29 7b 0a 20 20 20 20 61 nByte<0 ){. a
19b11 73 73 65 72 74 28 20 65 6e 63 21 3d 30 20 29 3b ssert( enc!=0 );
19b12 0a 20 20 20 20 69 66 28 20 65 6e 63 3d 3d 53 51 . if( enc==SQ
19b13 4c 49 54 45 5f 55 54 46 38 20 29 7b 0a 20 20 20 LITE_UTF8 ){.
19b14 20 20 20 66 6f 72 28 6e 42 79 74 65 3d 30 3b 20 for(nByte=0;
19b15 6e 42 79 74 65 3c 3d 69 4c 69 6d 69 74 20 26 26 nByte<=iLimit &&
19b16 20 7a 5b 6e 42 79 74 65 5d 3b 20 6e 42 79 74 65 z[nByte]; nByte
19b17 2b 2b 29 7b 7d 0a 20 20 20 20 7d 65 6c 73 65 7b ++){}. }else{
19b18 0a 20 20 20 20 20 20 66 6f 72 28 6e 42 79 74 65 . for(nByte
19b19 3d 30 3b 20 6e 42 79 74 65 3c 3d 69 4c 69 6d 69 =0; nByte<=iLimi
19b1a 74 20 26 26 20 28 7a 5b 6e 42 79 74 65 5d 20 7c t && (z[nByte] |
19b1b 20 7a 5b 6e 42 79 74 65 2b 31 5d 29 3b 20 6e 42 z[nByte+1]); nB
19b1c 79 74 65 2b 3d 32 29 7b 7d 0a 20 20 20 20 7d 0a yte+=2){}. }.
19b1d 20 20 20 20 66 6c 61 67 73 20 7c 3d 20 4d 45 4d flags |= MEM
19b1e 5f 54 65 72 6d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a _Term;. }.. /*
19b1f 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 The following b
19b20 6c 6f 63 6b 20 73 65 74 73 20 74 68 65 20 6e 65 lock sets the ne
19b21 77 20 76 61 6c 75 65 73 20 6f 66 20 4d 65 6d 2e w values of Mem.
19b22 7a 20 61 6e 64 20 4d 65 6d 2e 78 44 65 6c 2e 20 z and Mem.xDel.
19b23 49 74 0a 20 20 2a 2a 20 61 6c 73 6f 20 73 65 74 It. ** also set
19b24 73 20 61 20 66 6c 61 67 20 69 6e 20 6c 6f 63 61 s a flag in loca
19b25 6c 20 76 61 72 69 61 62 6c 65 20 22 66 6c 61 67 l variable "flag
19b26 73 22 20 74 6f 20 69 6e 64 69 63 61 74 65 20 74 s" to indicate t
19b27 68 65 20 6d 65 6d 6f 72 79 0a 20 20 2a 2a 20 6d he memory. ** m
19b28 61 6e 61 67 65 6d 65 6e 74 20 28 6f 6e 65 20 6f anagement (one o
19b29 66 20 4d 45 4d 5f 44 79 6e 20 6f 72 20 4d 45 4d f MEM_Dyn or MEM
19b2a 5f 53 74 61 74 69 63 29 2e 0a 20 20 2a 2f 0a 20 _Static).. */.
19b2b 20 69 66 28 20 78 44 65 6c 3d 3d 53 51 4c 49 54 if( xDel==SQLIT
19b2c 45 5f 54 52 41 4e 53 49 45 4e 54 20 29 7b 0a 20 E_TRANSIENT ){.
19b2d 20 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 20 3d 20 int nAlloc =
19b2e 6e 42 79 74 65 3b 0a 20 20 20 20 69 66 28 20 66 nByte;. if( f
19b2f 6c 61 67 73 26 4d 45 4d 5f 54 65 72 6d 20 29 7b lags&MEM_Term ){
19b30 0a 20 20 20 20 20 20 6e 41 6c 6c 6f 63 20 2b 3d . nAlloc +=
19b31 20 28 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 (enc==SQLITE_UT
19b32 46 38 3f 31 3a 32 29 3b 0a 20 20 20 20 7d 0a 20 F8?1:2);. }.
19b33 20 20 20 69 66 28 20 6e 42 79 74 65 3e 69 4c 69 if( nByte>iLi
19b34 6d 69 74 20 29 7b 0a 20 20 20 20 20 20 72 65 74 mit ){. ret
19b35 75 72 6e 20 53 51 4c 49 54 45 5f 54 4f 4f 42 49 urn SQLITE_TOOBI
19b36 47 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 G;. }. if(
19b37 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 sqlite3VdbeMemG
19b38 72 6f 77 28 70 4d 65 6d 2c 20 6e 41 6c 6c 6f 63 row(pMem, nAlloc
19b39 2c 20 30 29 20 29 7b 0a 20 20 20 20 20 20 72 65 , 0) ){. re
19b3a 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 turn SQLITE_NOME
19b3b 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d M;. }. mem
19b3c 63 70 79 28 70 4d 65 6d 2d 3e 7a 2c 20 7a 2c 20 cpy(pMem->z, z,
19b3d 6e 41 6c 6c 6f 63 29 3b 0a 20 20 7d 65 6c 73 65 nAlloc);. }else
19b3e 20 69 66 28 20 78 44 65 6c 3d 3d 53 51 4c 49 54 if( xDel==SQLIT
19b3f 45 5f 44 59 4e 41 4d 49 43 20 29 7b 0a 20 20 20 E_DYNAMIC ){.
19b40 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 sqlite3VdbeMemR
19b41 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 elease(pMem);.
19b42 20 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 pMem->zMalloc
19b43 3d 20 70 4d 65 6d 2d 3e 7a 20 3d 20 28 63 68 61 = pMem->z = (cha
19b44 72 20 2a 29 7a 3b 0a 20 20 20 20 70 4d 65 6d 2d r *)z;. pMem-
19b45 3e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 7d 65 6c >xDel = 0;. }el
19b46 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 se{. sqlite3V
19b47 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 4d dbeMemRelease(pM
19b48 65 6d 29 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a em);. pMem->z
19b49 20 3d 20 28 63 68 61 72 20 2a 29 7a 3b 0a 20 20 = (char *)z;.
19b4a 20 20 70 4d 65 6d 2d 3e 78 44 65 6c 20 3d 20 78 pMem->xDel = x
19b4b 44 65 6c 3b 0a 20 20 20 20 66 6c 61 67 73 20 7c Del;. flags |
19b4c 3d 20 28 28 78 44 65 6c 3d 3d 53 51 4c 49 54 45 = ((xDel==SQLITE
19b4d 5f 53 54 41 54 49 43 29 3f 4d 45 4d 5f 53 74 61 _STATIC)?MEM_Sta
19b4e 74 69 63 3a 4d 45 4d 5f 44 79 6e 29 3b 0a 20 20 tic:MEM_Dyn);.
19b4f 7d 0a 0a 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 6e }.. pMem->n = n
19b50 42 79 74 65 3b 0a 20 20 70 4d 65 6d 2d 3e 66 6c Byte;. pMem->fl
19b51 61 67 73 20 3d 20 66 6c 61 67 73 3b 0a 20 20 70 ags = flags;. p
19b52 4d 65 6d 2d 3e 65 6e 63 20 3d 20 28 65 6e 63 3d Mem->enc = (enc=
19b53 3d 30 20 3f 20 53 51 4c 49 54 45 5f 55 54 46 38 =0 ? SQLITE_UTF8
19b54 20 3a 20 65 6e 63 29 3b 0a 20 20 70 4d 65 6d 2d : enc);. pMem-
19b55 3e 74 79 70 65 20 3d 20 28 65 6e 63 3d 3d 30 20 >type = (enc==0
19b56 3f 20 53 51 4c 49 54 45 5f 42 4c 4f 42 20 3a 20 ? SQLITE_BLOB :
19b57 53 51 4c 49 54 45 5f 54 45 58 54 29 3b 0a 0a 23 SQLITE_TEXT);..#
19b58 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
19b59 49 54 5f 55 54 46 31 36 0a 20 20 69 66 28 20 70 IT_UTF16. if( p
19b5a 4d 65 6d 2d 3e 65 6e 63 21 3d 53 51 4c 49 54 45 Mem->enc!=SQLITE
19b5b 5f 55 54 46 38 20 26 26 20 73 71 6c 69 74 65 33 _UTF8 && sqlite3
19b5c 56 64 62 65 4d 65 6d 48 61 6e 64 6c 65 42 6f 6d VdbeMemHandleBom
19b5d 28 70 4d 65 6d 29 20 29 7b 0a 20 20 20 20 72 65 (pMem) ){. re
19b5e 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 turn SQLITE_NOME
19b5f 4d 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 M;. }.#endif..
19b60 20 69 66 28 20 6e 42 79 74 65 3e 69 4c 69 6d 69 if( nByte>iLimi
19b61 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 t ){. return
19b62 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47 3b 0a 20 SQLITE_TOOBIG;.
19b63 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c }.. return SQL
19b64 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ITE_OK;.}../*.**
19b65 20 43 6f 6d 70 61 72 65 20 74 68 65 20 76 61 6c Compare the val
19b66 75 65 73 20 63 6f 6e 74 61 69 6e 65 64 20 62 79 ues contained by
19b67 20 74 68 65 20 74 77 6f 20 6d 65 6d 6f 72 79 20 the two memory
19b68 63 65 6c 6c 73 2c 20 72 65 74 75 72 6e 69 6e 67 cells, returning
19b69 0a 2a 2a 20 6e 65 67 61 74 69 76 65 2c 20 7a 65 .** negative, ze
19b6a 72 6f 20 6f 72 20 70 6f 73 69 74 69 76 65 20 69 ro or positive i
19b6b 66 20 70 4d 65 6d 31 20 69 73 20 6c 65 73 73 20 f pMem1 is less
19b6c 74 68 61 6e 2c 20 65 71 75 61 6c 20 74 6f 2c 20 than, equal to,
19b6d 6f 72 20 67 72 65 61 74 65 72 0a 2a 2a 20 74 68 or greater.** th
19b6e 61 6e 20 70 4d 65 6d 32 2e 20 53 6f 72 74 69 6e an pMem2. Sortin
19b6f 67 20 6f 72 64 65 72 20 69 73 20 4e 55 4c 4c 27 g order is NULL'
19b70 73 20 66 69 72 73 74 2c 20 66 6f 6c 6c 6f 77 65 s first, followe
19b71 64 20 62 79 20 6e 75 6d 62 65 72 73 20 28 69 6e d by numbers (in
19b72 74 65 67 65 72 73 0a 2a 2a 20 61 6e 64 20 72 65 tegers.** and re
19b73 61 6c 73 29 20 73 6f 72 74 65 64 20 6e 75 6d 65 als) sorted nume
19b74 72 69 63 61 6c 6c 79 2c 20 66 6f 6c 6c 6f 77 65 rically, followe
19b75 64 20 62 79 20 74 65 78 74 20 6f 72 64 65 72 65 d by text ordere
19b76 64 20 62 79 20 74 68 65 20 63 6f 6c 6c 61 74 69 d by the collati
19b77 6e 67 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20 70 ng.** sequence p
19b78 43 6f 6c 6c 20 61 6e 64 20 66 69 6e 61 6c 6c 79 Coll and finally
19b79 20 62 6c 6f 62 27 73 20 6f 72 64 65 72 65 64 20 blob's ordered
19b7a 62 79 20 6d 65 6d 63 6d 70 28 29 2e 0a 2a 2a 0a by memcmp()..**.
19b7b 2a 2a 20 54 77 6f 20 4e 55 4c 4c 20 76 61 6c 75 ** Two NULL valu
19b7c 65 73 20 61 72 65 20 63 6f 6e 73 69 64 65 72 65 es are considere
19b7d 64 20 65 71 75 61 6c 20 62 79 20 74 68 69 73 20 d equal by this
19b7e 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c function..*/.SQL
19b7f 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
19b80 73 71 6c 69 74 65 33 4d 65 6d 43 6f 6d 70 61 72 sqlite3MemCompar
19b81 65 28 63 6f 6e 73 74 20 4d 65 6d 20 2a 70 4d 65 e(const Mem *pMe
19b82 6d 31 2c 20 63 6f 6e 73 74 20 4d 65 6d 20 2a 70 m1, const Mem *p
19b83 4d 65 6d 32 2c 20 63 6f 6e 73 74 20 43 6f 6c 6c Mem2, const Coll
19b84 53 65 71 20 2a 70 43 6f 6c 6c 29 7b 0a 20 20 69 Seq *pColl){. i
19b85 6e 74 20 72 63 3b 0a 20 20 69 6e 74 20 66 31 2c nt rc;. int f1,
19b86 20 66 32 3b 0a 20 20 69 6e 74 20 63 6f 6d 62 69 f2;. int combi
19b87 6e 65 64 5f 66 6c 61 67 73 3b 0a 0a 20 20 2f 2a ned_flags;.. /*
19b88 20 49 6e 74 65 72 63 68 61 6e 67 65 20 70 4d 65 Interchange pMe
19b89 6d 31 20 61 6e 64 20 70 4d 65 6d 32 20 69 66 20 m1 and pMem2 if
19b8a 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 the collating se
19b8b 71 75 65 6e 63 65 20 73 70 65 63 69 66 69 65 73 quence specifies
19b8c 0a 20 20 2a 2a 20 44 45 53 43 20 6f 72 64 65 72 . ** DESC order
19b8d 2e 0a 20 20 2a 2f 0a 20 20 66 31 20 3d 20 70 4d .. */. f1 = pM
19b8e 65 6d 31 2d 3e 66 6c 61 67 73 3b 0a 20 20 66 32 em1->flags;. f2
19b8f 20 3d 20 70 4d 65 6d 32 2d 3e 66 6c 61 67 73 3b = pMem2->flags;
19b90 0a 20 20 63 6f 6d 62 69 6e 65 64 5f 66 6c 61 67 . combined_flag
19b91 73 20 3d 20 66 31 7c 66 32 3b 0a 20 20 61 73 73 s = f1|f2;. ass
19b92 65 72 74 28 20 28 63 6f 6d 62 69 6e 65 64 5f 66 ert( (combined_f
19b93 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 lags & MEM_RowSe
19b94 74 29 3d 3d 30 20 29 3b 0a 20 0a 20 20 2f 2a 20 t)==0 );. . /*
19b95 49 66 20 6f 6e 65 20 76 61 6c 75 65 20 69 73 20 If one value is
19b96 4e 55 4c 4c 2c 20 69 74 20 69 73 20 6c 65 73 73 NULL, it is less
19b97 20 74 68 61 6e 20 74 68 65 20 6f 74 68 65 72 2e than the other.
19b98 20 49 66 20 62 6f 74 68 20 76 61 6c 75 65 73 0a If both values.
19b99 20 20 2a 2a 20 61 72 65 20 4e 55 4c 4c 2c 20 72 ** are NULL, r
19b9a 65 74 75 72 6e 20 30 2e 0a 20 20 2a 2f 0a 20 20 eturn 0.. */.
19b9b 69 66 28 20 63 6f 6d 62 69 6e 65 64 5f 66 6c 61 if( combined_fla
19b9c 67 73 26 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 gs&MEM_Null ){.
19b9d 20 20 20 72 65 74 75 72 6e 20 28 66 32 26 4d 45 return (f2&ME
19b9e 4d 5f 4e 75 6c 6c 29 20 2d 20 28 66 31 26 4d 45 M_Null) - (f1&ME
19b9f 4d 5f 4e 75 6c 6c 29 3b 0a 20 20 7d 0a 0a 20 20 M_Null);. }..
19ba0 2f 2a 20 49 66 20 6f 6e 65 20 76 61 6c 75 65 20 /* If one value
19ba1 69 73 20 61 20 6e 75 6d 62 65 72 20 61 6e 64 20 is a number and
19ba2 74 68 65 20 6f 74 68 65 72 20 69 73 20 6e 6f 74 the other is not
19ba3 2c 20 74 68 65 20 6e 75 6d 62 65 72 20 69 73 20 , the number is
19ba4 6c 65 73 73 2e 0a 20 20 2a 2a 20 49 66 20 62 6f less.. ** If bo
19ba5 74 68 20 61 72 65 20 6e 75 6d 62 65 72 73 2c 20 th are numbers,
19ba6 63 6f 6d 70 61 72 65 20 61 73 20 72 65 61 6c 73 compare as reals
19ba7 20 69 66 20 6f 6e 65 20 69 73 20 61 20 72 65 61 if one is a rea
19ba8 6c 2c 20 6f 72 20 61 73 20 69 6e 74 65 67 65 72 l, or as integer
19ba9 73 0a 20 20 2a 2a 20 69 66 20 62 6f 74 68 20 76 s. ** if both v
19baa 61 6c 75 65 73 20 61 72 65 20 69 6e 74 65 67 65 alues are intege
19bab 72 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 63 rs.. */. if( c
19bac 6f 6d 62 69 6e 65 64 5f 66 6c 61 67 73 26 28 4d ombined_flags&(M
19bad 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 29 EM_Int|MEM_Real)
19bae 20 29 7b 0a 20 20 20 20 69 66 28 20 21 28 66 31 ){. if( !(f1
19baf 26 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 &(MEM_Int|MEM_Re
19bb0 61 6c 29 29 20 29 7b 0a 20 20 20 20 20 20 72 65 al)) ){. re
19bb1 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 turn 1;. }.
19bb2 20 20 69 66 28 20 21 28 66 32 26 28 4d 45 4d 5f if( !(f2&(MEM_
19bb3 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 29 29 20 29 Int|MEM_Real)) )
19bb4 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 2d {. return -
19bb5 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 1;. }. if(
19bb6 20 28 66 31 20 26 20 66 32 20 26 20 4d 45 4d 5f (f1 & f2 & MEM_
19bb7 49 6e 74 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 Int)==0 ){.
19bb8 20 64 6f 75 62 6c 65 20 72 31 2c 20 72 32 3b 0a double r1, r2;.
19bb9 20 20 20 20 20 20 69 66 28 20 28 66 31 26 4d 45 if( (f1&ME
19bba 4d 5f 52 65 61 6c 29 3d 3d 30 20 29 7b 0a 20 20 M_Real)==0 ){.
19bbb 20 20 20 20 20 20 72 31 20 3d 20 28 64 6f 75 62 r1 = (doub
19bbc 6c 65 29 70 4d 65 6d 31 2d 3e 75 2e 69 3b 0a 20 le)pMem1->u.i;.
19bbd 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
19bbe 20 20 20 20 72 31 20 3d 20 70 4d 65 6d 31 2d 3e r1 = pMem1->
19bbf 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 r;. }.
19bc0 20 69 66 28 20 28 66 32 26 4d 45 4d 5f 52 65 61 if( (f2&MEM_Rea
19bc1 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 l)==0 ){.
19bc2 20 72 32 20 3d 20 28 64 6f 75 62 6c 65 29 70 4d r2 = (double)pM
19bc3 65 6d 32 2d 3e 75 2e 69 3b 0a 20 20 20 20 20 20 em2->u.i;.
19bc4 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72 }else{. r
19bc5 32 20 3d 20 70 4d 65 6d 32 2d 3e 72 3b 0a 20 20 2 = pMem2->r;.
19bc6 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 }. if(
19bc7 72 31 3c 72 32 20 29 20 72 65 74 75 72 6e 20 2d r1<r2 ) return -
19bc8 31 3b 0a 20 20 20 20 20 20 69 66 28 20 72 31 3e 1;. if( r1>
19bc9 72 32 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 r2 ) return 1;.
19bca 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 return 0;.
19bcb 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
19bcc 61 73 73 65 72 74 28 20 66 31 26 4d 45 4d 5f 49 assert( f1&MEM_I
19bcd 6e 74 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 nt );. asse
19bce 72 74 28 20 66 32 26 4d 45 4d 5f 49 6e 74 20 29 rt( f2&MEM_Int )
19bcf 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4d 65 6d ;. if( pMem
19bd0 31 2d 3e 75 2e 69 20 3c 20 70 4d 65 6d 32 2d 3e 1->u.i < pMem2->
19bd1 75 2e 69 20 29 20 72 65 74 75 72 6e 20 2d 31 3b u.i ) return -1;
19bd2 0a 20 20 20 20 20 20 69 66 28 20 70 4d 65 6d 31 . if( pMem1
19bd3 2d 3e 75 2e 69 20 3e 20 70 4d 65 6d 32 2d 3e 75 ->u.i > pMem2->u
19bd4 2e 69 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 .i ) return 1;.
19bd5 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 return 0;.
19bd6 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 }. }.. /* I
19bd7 66 20 6f 6e 65 20 76 61 6c 75 65 20 69 73 20 61 f one value is a
19bd8 20 73 74 72 69 6e 67 20 61 6e 64 20 74 68 65 20 string and the
19bd9 6f 74 68 65 72 20 69 73 20 61 20 62 6c 6f 62 2c other is a blob,
19bda 20 74 68 65 20 73 74 72 69 6e 67 20 69 73 20 6c the string is l
19bdb 65 73 73 2e 0a 20 20 2a 2a 20 49 66 20 62 6f 74 ess.. ** If bot
19bdc 68 20 61 72 65 20 73 74 72 69 6e 67 73 2c 20 63 h are strings, c
19bdd 6f 6d 70 61 72 65 20 75 73 69 6e 67 20 74 68 65 ompare using the
19bde 20 63 6f 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74 collating funct
19bdf 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 ions.. */. if(
19be0 20 63 6f 6d 62 69 6e 65 64 5f 66 6c 61 67 73 26 combined_flags&
19be1 4d 45 4d 5f 53 74 72 20 29 7b 0a 20 20 20 20 69 MEM_Str ){. i
19be2 66 28 20 28 66 31 20 26 20 4d 45 4d 5f 53 74 72 f( (f1 & MEM_Str
19be3 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 )==0 ){. re
19be4 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 turn 1;. }.
19be5 20 20 69 66 28 20 28 66 32 20 26 20 4d 45 4d 5f if( (f2 & MEM_
19be6 53 74 72 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 Str)==0 ){.
19be7 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20 return -1;.
19be8 7d 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 }.. assert( p
19be9 4d 65 6d 31 2d 3e 65 6e 63 3d 3d 70 4d 65 6d 32 Mem1->enc==pMem2
19bea 2d 3e 65 6e 63 20 29 3b 0a 20 20 20 20 61 73 73 ->enc );. ass
19beb 65 72 74 28 20 70 4d 65 6d 31 2d 3e 65 6e 63 3d ert( pMem1->enc=
19bec 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 7c 7c 20 =SQLITE_UTF8 ||
19bed 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 4d 65 . pMe
19bee 6d 31 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f m1->enc==SQLITE_
19bef 55 54 46 31 36 4c 45 20 7c 7c 20 70 4d 65 6d 31 UTF16LE || pMem1
19bf0 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 ->enc==SQLITE_UT
19bf1 46 31 36 42 45 20 29 3b 0a 0a 20 20 20 20 2f 2a F16BE );.. /*
19bf2 20 54 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 The collation s
19bf3 65 71 75 65 6e 63 65 20 6d 75 73 74 20 62 65 20 equence must be
19bf4 64 65 66 69 6e 65 64 20 61 74 20 74 68 69 73 20 defined at this
19bf5 70 6f 69 6e 74 2c 20 65 76 65 6e 20 69 66 0a 20 point, even if.
19bf6 20 20 20 2a 2a 20 74 68 65 20 75 73 65 72 20 64 ** the user d
19bf7 65 6c 65 74 65 73 20 74 68 65 20 63 6f 6c 6c 61 eletes the colla
19bf8 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 61 66 tion sequence af
19bf9 74 65 72 20 74 68 65 20 76 64 62 65 20 70 72 6f ter the vdbe pro
19bfa 67 72 61 6d 20 69 73 0a 20 20 20 20 2a 2a 20 63 gram is. ** c
19bfb 6f 6d 70 69 6c 65 64 20 28 74 68 69 73 20 77 61 ompiled (this wa
19bfc 73 20 6e 6f 74 20 61 6c 77 61 79 73 20 74 68 65 s not always the
19bfd 20 63 61 73 65 29 2e 0a 20 20 20 20 2a 2f 0a 20 case).. */.
19bfe 20 20 20 61 73 73 65 72 74 28 20 21 70 43 6f 6c assert( !pCol
19bff 6c 20 7c 7c 20 70 43 6f 6c 6c 2d 3e 78 43 6d 70 l || pColl->xCmp
19c00 20 29 3b 0a 0a 20 20 20 20 69 66 28 20 70 43 6f );.. if( pCo
19c01 6c 6c 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 ll ){. if(
19c02 70 4d 65 6d 31 2d 3e 65 6e 63 3d 3d 70 43 6f 6c pMem1->enc==pCol
19c03 6c 2d 3e 65 6e 63 20 29 7b 0a 20 20 20 20 20 20 l->enc ){.
19c04 20 20 2f 2a 20 54 68 65 20 73 74 72 69 6e 67 73 /* The strings
19c05 20 61 72 65 20 61 6c 72 65 61 64 79 20 69 6e 20 are already in
19c06 74 68 65 20 63 6f 72 72 65 63 74 20 65 6e 63 6f the correct enco
19c07 64 69 6e 67 2e 20 20 43 61 6c 6c 20 74 68 65 0a ding. Call the.
19c08 20 20 20 20 20 20 20 20 2a 2a 20 63 6f 6d 70 61 ** compa
19c09 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 64 rison function d
19c0a 69 72 65 63 74 6c 79 20 2a 2f 0a 20 20 20 20 20 irectly */.
19c0b 20 20 20 72 65 74 75 72 6e 20 70 43 6f 6c 6c 2d return pColl-
19c0c 3e 78 43 6d 70 28 70 43 6f 6c 6c 2d 3e 70 55 73 >xCmp(pColl->pUs
19c0d 65 72 2c 70 4d 65 6d 31 2d 3e 6e 2c 70 4d 65 6d er,pMem1->n,pMem
19c0e 31 2d 3e 7a 2c 70 4d 65 6d 32 2d 3e 6e 2c 70 4d 1->z,pMem2->n,pM
19c0f 65 6d 32 2d 3e 7a 29 3b 0a 20 20 20 20 20 20 7d em2->z);. }
19c10 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 63 6f else{. co
19c11 6e 73 74 20 76 6f 69 64 20 2a 76 31 2c 20 2a 76 nst void *v1, *v
19c12 32 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6e 2;. int n
19c13 31 2c 20 6e 32 3b 0a 20 20 20 20 20 20 20 20 4d 1, n2;. M
19c14 65 6d 20 63 31 3b 0a 20 20 20 20 20 20 20 20 4d em c1;. M
19c15 65 6d 20 63 32 3b 0a 20 20 20 20 20 20 20 20 6d em c2;. m
19c16 65 6d 73 65 74 28 26 63 31 2c 20 30 2c 20 73 69 emset(&c1, 0, si
19c17 7a 65 6f 66 28 63 31 29 29 3b 0a 20 20 20 20 20 zeof(c1));.
19c18 20 20 20 6d 65 6d 73 65 74 28 26 63 32 2c 20 30 memset(&c2, 0
19c19 2c 20 73 69 7a 65 6f 66 28 63 32 29 29 3b 0a 20 , sizeof(c2));.
19c1a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 sqlite3Vd
19c1b 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 beMemShallowCopy
19c1c 28 26 63 31 2c 20 70 4d 65 6d 31 2c 20 4d 45 4d (&c1, pMem1, MEM
19c1d 5f 45 70 68 65 6d 29 3b 0a 20 20 20 20 20 20 20 _Ephem);.
19c1e 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
19c1f 68 61 6c 6c 6f 77 43 6f 70 79 28 26 63 32 2c 20 hallowCopy(&c2,
19c20 70 4d 65 6d 32 2c 20 4d 45 4d 5f 45 70 68 65 6d pMem2, MEM_Ephem
19c21 29 3b 0a 20 20 20 20 20 20 20 20 76 31 20 3d 20 );. v1 =
19c22 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74 sqlite3ValueText
19c23 28 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a ((sqlite3_value*
19c24 29 26 63 31 2c 20 70 43 6f 6c 6c 2d 3e 65 6e 63 )&c1, pColl->enc
19c25 29 3b 0a 20 20 20 20 20 20 20 20 6e 31 20 3d 20 );. n1 =
19c26 76 31 3d 3d 30 20 3f 20 30 20 3a 20 63 31 2e 6e v1==0 ? 0 : c1.n
19c27 3b 0a 20 20 20 20 20 20 20 20 76 32 20 3d 20 73 ;. v2 = s
19c28 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74 28 qlite3ValueText(
19c29 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 (sqlite3_value*)
19c2a 26 63 32 2c 20 70 43 6f 6c 6c 2d 3e 65 6e 63 29 &c2, pColl->enc)
19c2b 3b 0a 20 20 20 20 20 20 20 20 6e 32 20 3d 20 76 ;. n2 = v
19c2c 32 3d 3d 30 20 3f 20 30 20 3a 20 63 32 2e 6e 3b 2==0 ? 0 : c2.n;
19c2d 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 43 . rc = pC
19c2e 6f 6c 6c 2d 3e 78 43 6d 70 28 70 43 6f 6c 6c 2d oll->xCmp(pColl-
19c2f 3e 70 55 73 65 72 2c 20 6e 31 2c 20 76 31 2c 20 >pUser, n1, v1,
19c30 6e 32 2c 20 76 32 29 3b 0a 20 20 20 20 20 20 20 n2, v2);.
19c31 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 sqlite3VdbeMemR
19c32 65 6c 65 61 73 65 28 26 63 31 29 3b 0a 20 20 20 elease(&c1);.
19c33 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
19c34 4d 65 6d 52 65 6c 65 61 73 65 28 26 63 32 29 3b MemRelease(&c2);
19c35 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 . return
19c36 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 rc;. }.
19c37 7d 0a 20 20 20 20 2f 2a 20 49 66 20 61 20 4e 55 }. /* If a NU
19c38 4c 4c 20 70 6f 69 6e 74 65 72 20 77 61 73 20 70 LL pointer was p
19c39 61 73 73 65 64 20 61 73 20 74 68 65 20 63 6f 6c assed as the col
19c3a 6c 61 74 65 20 66 75 6e 63 74 69 6f 6e 2c 20 66 late function, f
19c3b 61 6c 6c 20 74 68 72 6f 75 67 68 0a 20 20 20 20 all through.
19c3c 2a 2a 20 74 6f 20 74 68 65 20 62 6c 6f 62 20 63 ** to the blob c
19c3d 61 73 65 20 61 6e 64 20 75 73 65 20 6d 65 6d 63 ase and use memc
19c3e 6d 70 28 29 2e 20 20 2a 2f 0a 20 20 7d 0a 20 0a mp(). */. }. .
19c3f 20 20 2f 2a 20 42 6f 74 68 20 76 61 6c 75 65 73 /* Both values
19c40 20 6d 75 73 74 20 62 65 20 62 6c 6f 62 73 2e 20 must be blobs.
19c41 20 43 6f 6d 70 61 72 65 20 75 73 69 6e 67 20 6d Compare using m
19c42 65 6d 63 6d 70 28 29 2e 20 20 2a 2f 0a 20 20 72 emcmp(). */. r
19c43 63 20 3d 20 6d 65 6d 63 6d 70 28 70 4d 65 6d 31 c = memcmp(pMem1
19c44 2d 3e 7a 2c 20 70 4d 65 6d 32 2d 3e 7a 2c 20 28 ->z, pMem2->z, (
19c45 70 4d 65 6d 31 2d 3e 6e 3e 70 4d 65 6d 32 2d 3e pMem1->n>pMem2->
19c46 6e 29 3f 70 4d 65 6d 32 2d 3e 6e 3a 70 4d 65 6d n)?pMem2->n:pMem
19c47 31 2d 3e 6e 29 3b 0a 20 20 69 66 28 20 72 63 3d 1->n);. if( rc=
19c48 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 =0 ){. rc = p
19c49 4d 65 6d 31 2d 3e 6e 20 2d 20 70 4d 65 6d 32 2d Mem1->n - pMem2-
19c4a 3e 6e 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e >n;. }. return
19c4b 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f rc;.}../*.** Mo
19c4c 76 65 20 64 61 74 61 20 6f 75 74 20 6f 66 20 61 ve data out of a
19c4d 20 62 74 72 65 65 20 6b 65 79 20 6f 72 20 64 61 btree key or da
19c4e 74 61 20 66 69 65 6c 64 20 61 6e 64 20 69 6e 74 ta field and int
19c4f 6f 20 61 20 4d 65 6d 20 73 74 72 75 63 74 75 72 o a Mem structur
19c50 65 2e 0a 2a 2a 20 54 68 65 20 64 61 74 61 20 6f e..** The data o
19c51 72 20 6b 65 79 20 69 73 20 74 61 6b 65 6e 20 66 r key is taken f
19c52 72 6f 6d 20 74 68 65 20 65 6e 74 72 79 20 74 68 rom the entry th
19c53 61 74 20 70 43 75 72 20 69 73 20 63 75 72 72 65 at pCur is curre
19c54 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 0a 2a 2a ntly pointing.**
19c55 20 74 6f 2e 20 20 6f 66 66 73 65 74 20 61 6e 64 to. offset and
19c56 20 61 6d 74 20 64 65 74 65 72 6d 69 6e 65 20 77 amt determine w
19c57 68 61 74 20 70 6f 72 74 69 6f 6e 20 6f 66 20 74 hat portion of t
19c58 68 65 20 64 61 74 61 20 6f 72 20 6b 65 79 20 74 he data or key t
19c59 6f 20 72 65 74 72 69 65 76 65 2e 0a 2a 2a 20 6b o retrieve..** k
19c5a 65 79 20 69 73 20 74 72 75 65 20 74 6f 20 67 65 ey is true to ge
19c5b 74 20 74 68 65 20 6b 65 79 20 6f 72 20 66 61 6c t the key or fal
19c5c 73 65 20 74 6f 20 67 65 74 20 64 61 74 61 2e 20 se to get data.
19c5d 20 54 68 65 20 72 65 73 75 6c 74 20 69 73 20 77 The result is w
19c5e 72 69 74 74 65 6e 0a 2a 2a 20 69 6e 74 6f 20 74 ritten.** into t
19c5f 68 65 20 70 4d 65 6d 20 65 6c 65 6d 65 6e 74 2e he pMem element.
19c60 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 4d 65 6d 20 .**.** The pMem
19c61 73 74 72 75 63 74 75 72 65 20 69 73 20 61 73 73 structure is ass
19c62 75 6d 65 64 20 74 6f 20 62 65 20 75 6e 69 6e 69 umed to be unini
19c63 74 69 61 6c 69 7a 65 64 2e 20 20 41 6e 79 20 70 tialized. Any p
19c64 72 69 6f 72 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20 rior content.**
19c65 69 73 20 6f 76 65 72 77 72 69 74 74 65 6e 20 77 is overwritten w
19c66 69 74 68 6f 75 74 20 62 65 69 6e 67 20 66 72 65 ithout being fre
19c67 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 69 ed..**.** If thi
19c68 73 20 72 6f 75 74 69 6e 65 20 66 61 69 6c 73 20 s routine fails
19c69 66 6f 72 20 61 6e 79 20 72 65 61 73 6f 6e 20 28 for any reason (
19c6a 6d 61 6c 6c 6f 63 20 72 65 74 75 72 6e 73 20 4e malloc returns N
19c6b 55 4c 4c 20 6f 72 20 75 6e 61 62 6c 65 0a 2a 2a ULL or unable.**
19c6c 20 74 6f 20 72 65 61 64 20 66 72 6f 6d 20 74 68 to read from th
19c6d 65 20 64 69 73 6b 29 20 74 68 65 6e 20 74 68 65 e disk) then the
19c6e 20 70 4d 65 6d 20 69 73 20 6c 65 66 74 20 69 6e pMem is left in
19c6f 20 61 6e 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 an inconsistent
19c70 20 73 74 61 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54 state..*/.SQLIT
19c71 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
19c72 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 72 6f 6d lite3VdbeMemFrom
19c73 42 74 72 65 65 28 0a 20 20 42 74 43 75 72 73 6f Btree(. BtCurso
19c74 72 20 2a 70 43 75 72 2c 20 20 20 2f 2a 20 43 75 r *pCur, /* Cu
19c75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 61 74 rsor pointing at
19c76 20 72 65 63 6f 72 64 20 74 6f 20 72 65 74 72 69 record to retri
19c77 65 76 65 2e 20 2a 2f 0a 20 20 69 6e 74 20 6f 66 eve. */. int of
19c78 66 73 65 74 2c 20 20 20 20 20 20 20 2f 2a 20 4f fset, /* O
19c79 66 66 73 65 74 20 66 72 6f 6d 20 74 68 65 20 73 ffset from the s
19c7a 74 61 72 74 20 6f 66 20 64 61 74 61 20 74 6f 20 tart of data to
19c7b 72 65 74 75 72 6e 20 62 79 74 65 73 20 66 72 6f return bytes fro
19c7c 6d 2e 20 2a 2f 0a 20 20 69 6e 74 20 61 6d 74 2c m. */. int amt,
19c7d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
19c7e 62 65 72 20 6f 66 20 62 79 74 65 73 20 74 6f 20 ber of bytes to
19c7f 72 65 74 75 72 6e 2e 20 2a 2f 0a 20 20 69 6e 74 return. */. int
19c80 20 6b 65 79 2c 20 20 20 20 20 20 20 20 20 20 2f key, /
19c81 2a 20 49 66 20 74 72 75 65 2c 20 72 65 74 72 69 * If true, retri
19c82 65 76 65 20 66 72 6f 6d 20 74 68 65 20 62 74 72 eve from the btr
19c83 65 65 20 6b 65 79 2c 20 6e 6f 74 20 64 61 74 61 ee key, not data
19c84 2e 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d . */. Mem *pMem
19c85 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a /* OUT:
19c86 20 52 65 74 75 72 6e 20 64 61 74 61 20 69 6e 20 Return data in
19c87 74 68 69 73 20 4d 65 6d 20 73 74 72 75 63 74 75 this Mem structu
19c88 72 65 2e 20 2a 2f 0a 29 7b 0a 20 20 63 68 61 72 re. */.){. char
19c89 20 2a 7a 44 61 74 61 3b 20 20 20 20 20 20 20 20 *zData;
19c8a 2f 2a 20 44 61 74 61 20 66 72 6f 6d 20 74 68 65 /* Data from the
19c8b 20 62 74 72 65 65 20 6c 61 79 65 72 20 2a 2f 0a btree layer */.
19c8c 20 20 69 6e 74 20 61 76 61 69 6c 61 62 6c 65 20 int available
19c8d 3d 20 30 3b 20 20 2f 2a 20 4e 75 6d 62 65 72 20 = 0; /* Number
19c8e 6f 66 20 62 79 74 65 73 20 61 76 61 69 6c 61 62 of bytes availab
19c8f 6c 65 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20 le on the local
19c90 62 74 72 65 65 20 70 61 67 65 20 2a 2f 0a 20 20 btree page */.
19c91 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
19c92 4f 4b 3b 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f OK; /* Return co
19c93 64 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 de */.. assert(
19c94 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 sqlite3BtreeCur
19c95 73 6f 72 49 73 56 61 6c 69 64 28 70 43 75 72 29 sorIsValid(pCur)
19c96 20 29 3b 0a 0a 20 20 2f 2a 20 4e 6f 74 65 3a 20 );.. /* Note:
19c97 74 68 65 20 63 61 6c 6c 73 20 74 6f 20 42 74 72 the calls to Btr
19c98 65 65 4b 65 79 46 65 74 63 68 28 29 20 61 6e 64 eeKeyFetch() and
19c99 20 44 61 74 61 46 65 74 63 68 28 29 20 62 65 6c DataFetch() bel
19c9a 6f 77 20 61 73 73 65 72 74 28 29 20 0a 20 20 2a ow assert() . *
19c9b 2a 20 74 68 61 74 20 62 6f 74 68 20 74 68 65 20 * that both the
19c9c 42 74 53 68 61 72 65 64 20 61 6e 64 20 64 61 74 BtShared and dat
19c9d 61 62 61 73 65 20 68 61 6e 64 6c 65 20 6d 75 74 abase handle mut
19c9e 65 78 65 73 20 61 72 65 20 68 65 6c 64 2e 20 2a exes are held. *
19c9f 2f 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 /. assert( (pMe
19ca0 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 m->flags & MEM_R
19ca1 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 69 owSet)==0 );. i
19ca2 66 28 20 6b 65 79 20 29 7b 0a 20 20 20 20 7a 44 f( key ){. zD
19ca3 61 74 61 20 3d 20 28 63 68 61 72 20 2a 29 73 71 ata = (char *)sq
19ca4 6c 69 74 65 33 42 74 72 65 65 4b 65 79 46 65 74 lite3BtreeKeyFet
19ca5 63 68 28 70 43 75 72 2c 20 26 61 76 61 69 6c 61 ch(pCur, &availa
19ca6 62 6c 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 ble);. }else{.
19ca7 20 20 20 7a 44 61 74 61 20 3d 20 28 63 68 61 72 zData = (char
19ca8 20 2a 29 73 71 6c 69 74 65 33 42 74 72 65 65 44 *)sqlite3BtreeD
19ca9 61 74 61 46 65 74 63 68 28 70 43 75 72 2c 20 26 ataFetch(pCur, &
19caa 61 76 61 69 6c 61 62 6c 65 29 3b 0a 20 20 7d 0a available);. }.
19cab 20 20 61 73 73 65 72 74 28 20 7a 44 61 74 61 21 assert( zData!
19cac 3d 30 20 29 3b 0a 0a 20 20 69 66 28 20 6f 66 66 =0 );.. if( off
19cad 73 65 74 2b 61 6d 74 3c 3d 61 76 61 69 6c 61 62 set+amt<=availab
19cae 6c 65 20 26 26 20 28 70 4d 65 6d 2d 3e 66 6c 61 le && (pMem->fla
19caf 67 73 26 4d 45 4d 5f 44 79 6e 29 3d 3d 30 20 29 gs&MEM_Dyn)==0 )
19cb0 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 {. sqlite3Vdb
19cb1 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d eMemRelease(pMem
19cb2 29 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d );. pMem->z =
19cb3 20 26 7a 44 61 74 61 5b 6f 66 66 73 65 74 5d 3b &zData[offset];
19cb4 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 . pMem->flags
19cb5 20 3d 20 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f = MEM_Blob|MEM_
19cb6 45 70 68 65 6d 3b 0a 20 20 7d 65 6c 73 65 20 69 Ephem;. }else i
19cb7 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 f( SQLITE_OK==(r
19cb8 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d c = sqlite3VdbeM
19cb9 65 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 61 6d 74 emGrow(pMem, amt
19cba 2b 32 2c 20 30 29 29 20 29 7b 0a 20 20 20 20 70 +2, 0)) ){. p
19cbb 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d Mem->flags = MEM
19cbc 5f 42 6c 6f 62 7c 4d 45 4d 5f 44 79 6e 7c 4d 45 _Blob|MEM_Dyn|ME
19cbd 4d 5f 54 65 72 6d 3b 0a 20 20 20 20 70 4d 65 6d M_Term;. pMem
19cbe 2d 3e 65 6e 63 20 3d 20 30 3b 0a 20 20 20 20 70 ->enc = 0;. p
19cbf 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 Mem->type = SQLI
19cc0 54 45 5f 42 4c 4f 42 3b 0a 20 20 20 20 69 66 28 TE_BLOB;. if(
19cc1 20 6b 65 79 20 29 7b 0a 20 20 20 20 20 20 72 63 key ){. rc
19cc2 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b = sqlite3BtreeK
19cc3 65 79 28 70 43 75 72 2c 20 6f 66 66 73 65 74 2c ey(pCur, offset,
19cc4 20 61 6d 74 2c 20 70 4d 65 6d 2d 3e 7a 29 3b 0a amt, pMem->z);.
19cc5 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
19cc6 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
19cc7 65 65 44 61 74 61 28 70 43 75 72 2c 20 6f 66 66 eeData(pCur, off
19cc8 73 65 74 2c 20 61 6d 74 2c 20 70 4d 65 6d 2d 3e set, amt, pMem->
19cc9 7a 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d z);. }. pM
19cca 65 6d 2d 3e 7a 5b 61 6d 74 5d 20 3d 20 30 3b 0a em->z[amt] = 0;.
19ccb 20 20 20 20 70 4d 65 6d 2d 3e 7a 5b 61 6d 74 2b pMem->z[amt+
19ccc 31 5d 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 1] = 0;. if(
19ccd 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
19cce 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
19ccf 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 4d 65 beMemRelease(pMe
19cd0 6d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 m);. }. }.
19cd1 70 4d 65 6d 2d 3e 6e 20 3d 20 61 6d 74 3b 0a 0a pMem->n = amt;..
19cd2 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
19cd3 2f 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e /* This function
19cd4 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 is only availab
19cd5 6c 65 20 69 6e 74 65 72 6e 61 6c 6c 79 2c 20 69 le internally, i
19cd6 74 20 69 73 20 6e 6f 74 20 70 61 72 74 20 6f 66 t is not part of
19cd7 20 74 68 65 0a 2a 2a 20 65 78 74 65 72 6e 61 6c the.** external
19cd8 20 41 50 49 2e 20 49 74 20 77 6f 72 6b 73 20 69 API. It works i
19cd9 6e 20 61 20 73 69 6d 69 6c 61 72 20 77 61 79 20 n a similar way
19cda 74 6f 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 to sqlite3_value
19cdb 5f 74 65 78 74 28 29 2c 0a 2a 2a 20 65 78 63 65 _text(),.** exce
19cdc 70 74 20 74 68 65 20 64 61 74 61 20 72 65 74 75 pt the data retu
19cdd 72 6e 65 64 20 69 73 20 69 6e 20 74 68 65 20 65 rned is in the e
19cde 6e 63 6f 64 69 6e 67 20 73 70 65 63 69 66 69 65 ncoding specifie
19cdf 64 20 62 79 20 74 68 65 20 73 65 63 6f 6e 64 0a d by the second.
19ce0 2a 2a 20 70 61 72 61 6d 65 74 65 72 2c 20 77 68 ** parameter, wh
19ce1 69 63 68 20 6d 75 73 74 20 62 65 20 6f 6e 65 20 ich must be one
19ce2 6f 66 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42 of SQLITE_UTF16B
19ce3 45 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c E, SQLITE_UTF16L
19ce4 45 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 45 5f 55 E or.** SQLITE_U
19ce5 54 46 38 2e 0a 2a 2a 0a 2a 2a 20 28 32 30 30 36 TF8..**.** (2006
19ce6 2d 30 32 2d 31 36 3a 29 20 20 54 68 65 20 65 6e -02-16:) The en
19ce7 63 20 76 61 6c 75 65 20 63 61 6e 20 62 65 20 6f c value can be o
19ce8 72 2d 65 64 20 77 69 74 68 20 53 51 4c 49 54 45 r-ed with SQLITE
19ce9 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 2e 0a _UTF16_ALIGNED..
19cea 2a 2a 20 49 66 20 74 68 61 74 20 69 73 20 74 68 ** If that is th
19ceb 65 20 63 61 73 65 2c 20 74 68 65 6e 20 74 68 65 e case, then the
19cec 20 72 65 73 75 6c 74 20 6d 75 73 74 20 62 65 20 result must be
19ced 61 6c 69 67 6e 65 64 20 6f 6e 20 61 6e 20 65 76 aligned on an ev
19cee 65 6e 20 62 79 74 65 0a 2a 2a 20 62 6f 75 6e 64 en byte.** bound
19cef 61 72 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ary..*/.SQLITE_P
19cf0 52 49 56 41 54 45 20 63 6f 6e 73 74 20 76 6f 69 RIVATE const voi
19cf1 64 20 2a 73 71 6c 69 74 65 33 56 61 6c 75 65 54 d *sqlite3ValueT
19cf2 65 78 74 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 ext(sqlite3_valu
19cf3 65 2a 20 70 56 61 6c 2c 20 75 38 20 65 6e 63 29 e* pVal, u8 enc)
19cf4 7b 0a 20 20 69 66 28 20 21 70 56 61 6c 20 29 20 {. if( !pVal )
19cf5 72 65 74 75 72 6e 20 30 3b 0a 0a 20 20 61 73 73 return 0;.. ass
19cf6 65 72 74 28 20 70 56 61 6c 2d 3e 64 62 3d 3d 30 ert( pVal->db==0
19cf7 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 || sqlite3_mute
19cf8 78 5f 68 65 6c 64 28 70 56 61 6c 2d 3e 64 62 2d x_held(pVal->db-
19cf9 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 >mutex) );. ass
19cfa 65 72 74 28 20 28 65 6e 63 26 33 29 3d 3d 28 65 ert( (enc&3)==(e
19cfb 6e 63 26 7e 53 51 4c 49 54 45 5f 55 54 46 31 36 nc&~SQLITE_UTF16
19cfc 5f 41 4c 49 47 4e 45 44 29 20 29 3b 0a 20 20 61 _ALIGNED) );. a
19cfd 73 73 65 72 74 28 20 28 70 56 61 6c 2d 3e 66 6c ssert( (pVal->fl
19cfe 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 ags & MEM_RowSet
19cff 29 3d 3d 30 20 29 3b 0a 0a 20 20 69 66 28 20 70 )==0 );.. if( p
19d00 56 61 6c 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 4e Val->flags&MEM_N
19d01 75 6c 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ull ){. retur
19d02 6e 20 30 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 n 0;. }. asser
19d03 74 28 20 28 4d 45 4d 5f 42 6c 6f 62 3e 3e 33 29 t( (MEM_Blob>>3)
19d04 20 3d 3d 20 4d 45 4d 5f 53 74 72 20 29 3b 0a 20 == MEM_Str );.
19d05 20 70 56 61 6c 2d 3e 66 6c 61 67 73 20 7c 3d 20 pVal->flags |=
19d06 28 70 56 61 6c 2d 3e 66 6c 61 67 73 20 26 20 4d (pVal->flags & M
19d07 45 4d 5f 42 6c 6f 62 29 3e 3e 33 3b 0a 20 20 65 EM_Blob)>>3;. e
19d08 78 70 61 6e 64 42 6c 6f 62 28 70 56 61 6c 29 3b xpandBlob(pVal);
19d09 0a 20 20 69 66 28 20 70 56 61 6c 2d 3e 66 6c 61 . if( pVal->fla
19d0a 67 73 26 4d 45 4d 5f 53 74 72 20 29 7b 0a 20 20 gs&MEM_Str ){.
19d0b 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 sqlite3VdbeCha
19d0c 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70 56 61 6c ngeEncoding(pVal
19d0d 2c 20 65 6e 63 20 26 20 7e 53 51 4c 49 54 45 5f , enc & ~SQLITE_
19d0e 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 29 3b 0a UTF16_ALIGNED);.
19d0f 20 20 20 20 69 66 28 20 28 65 6e 63 20 26 20 53 if( (enc & S
19d10 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 QLITE_UTF16_ALIG
19d11 4e 45 44 29 21 3d 30 20 26 26 20 31 3d 3d 28 31 NED)!=0 && 1==(1
19d12 26 53 51 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49 &SQLITE_PTR_TO_I
19d13 4e 54 28 70 56 61 6c 2d 3e 7a 29 29 20 29 7b 0a NT(pVal->z)) ){.
19d14 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28 70 assert( (p
19d15 56 61 6c 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 Val->flags & (ME
19d16 4d 5f 45 70 68 65 6d 7c 4d 45 4d 5f 53 74 61 74 M_Ephem|MEM_Stat
19d17 69 63 29 29 21 3d 30 20 29 3b 0a 20 20 20 20 20 ic))!=0 );.
19d18 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 if( sqlite3Vdbe
19d19 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 MemMakeWriteable
19d1a 28 70 56 61 6c 29 21 3d 53 51 4c 49 54 45 5f 4f (pVal)!=SQLITE_O
19d1b 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 K ){. ret
19d1c 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 urn 0;. }.
19d1d 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 }. sqlite3
19d1e 56 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e VdbeMemNulTermin
19d1f 61 74 65 28 70 56 61 6c 29 3b 0a 20 20 7d 65 6c ate(pVal);. }el
19d20 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 se{. assert(
19d21 28 70 56 61 6c 2d 3e 66 6c 61 67 73 26 4d 45 4d (pVal->flags&MEM
19d22 5f 42 6c 6f 62 29 3d 3d 30 20 29 3b 0a 20 20 20 _Blob)==0 );.
19d23 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
19d24 74 72 69 6e 67 69 66 79 28 70 56 61 6c 2c 20 65 tringify(pVal, e
19d25 6e 63 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 nc);. assert(
19d26 20 30 3d 3d 28 31 26 53 51 4c 49 54 45 5f 50 54 0==(1&SQLITE_PT
19d27 52 5f 54 4f 5f 49 4e 54 28 70 56 61 6c 2d 3e 7a R_TO_INT(pVal->z
19d28 29 29 20 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 )) );. }. asse
19d29 72 74 28 70 56 61 6c 2d 3e 65 6e 63 3d 3d 28 65 rt(pVal->enc==(e
19d2a 6e 63 20 26 20 7e 53 51 4c 49 54 45 5f 55 54 46 nc & ~SQLITE_UTF
19d2b 31 36 5f 41 4c 49 47 4e 45 44 29 20 7c 7c 20 70 16_ALIGNED) || p
19d2c 56 61 6c 2d 3e 64 62 3d 3d 30 0a 20 20 20 20 20 Val->db==0.
19d2d 20 20 20 20 20 20 20 20 20 7c 7c 20 70 56 61 6c || pVal
19d2e 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c ->db->mallocFail
19d2f 65 64 20 29 3b 0a 20 20 69 66 28 20 70 56 61 6c ed );. if( pVal
19d30 2d 3e 65 6e 63 3d 3d 28 65 6e 63 20 26 20 7e 53 ->enc==(enc & ~S
19d31 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 QLITE_UTF16_ALIG
19d32 4e 45 44 29 20 29 7b 0a 20 20 20 20 72 65 74 75 NED) ){. retu
19d33 72 6e 20 70 56 61 6c 2d 3e 7a 3b 0a 20 20 7d 65 rn pVal->z;. }e
19d34 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 lse{. return
19d35 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 0;. }.}../*.**
19d36 43 72 65 61 74 65 20 61 20 6e 65 77 20 73 71 6c Create a new sql
19d37 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 ite3_value objec
19d38 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 t..*/.SQLITE_PRI
19d39 56 41 54 45 20 73 71 6c 69 74 65 33 5f 76 61 6c VATE sqlite3_val
19d3a 75 65 20 2a 73 71 6c 69 74 65 33 56 61 6c 75 65 ue *sqlite3Value
19d3b 4e 65 77 28 73 71 6c 69 74 65 33 20 2a 64 62 29 New(sqlite3 *db)
19d3c 7b 0a 20 20 4d 65 6d 20 2a 70 20 3d 20 73 71 6c {. Mem *p = sql
19d3d 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f ite3DbMallocZero
19d3e 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 29 29 (db, sizeof(*p))
19d3f 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 ;. if( p ){.
19d40 20 70 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f p->flags = MEM_
19d41 4e 75 6c 6c 3b 0a 20 20 20 20 70 2d 3e 74 79 70 Null;. p->typ
19d42 65 20 3d 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3b e = SQLITE_NULL;
19d43 0a 20 20 20 20 70 2d 3e 64 62 20 3d 20 64 62 3b . p->db = db;
19d44 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b . }. return p;
19d45 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 .}../*.** Create
19d46 20 61 20 6e 65 77 20 73 71 6c 69 74 65 33 5f 76 a new sqlite3_v
19d47 61 6c 75 65 20 6f 62 6a 65 63 74 2c 20 63 6f 6e alue object, con
19d48 74 61 69 6e 69 6e 67 20 74 68 65 20 76 61 6c 75 taining the valu
19d49 65 20 6f 66 20 70 45 78 70 72 2e 0a 2a 2a 0a 2a e of pExpr..**.*
19d4a 2a 20 54 68 69 73 20 6f 6e 6c 79 20 77 6f 72 6b * This only work
19d4b 73 20 66 6f 72 20 76 65 72 79 20 73 69 6d 70 6c s for very simpl
19d4c 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20 74 68 e expressions th
19d4d 61 74 20 63 6f 6e 73 69 73 74 20 6f 66 20 6f 6e at consist of on
19d4e 65 20 63 6f 6e 73 74 61 6e 74 0a 2a 2a 20 74 6f e constant.** to
19d4f 6b 65 6e 20 28 69 2e 65 2e 20 22 35 22 2c 20 22 ken (i.e. "5", "
19d50 35 2e 31 22 2c 20 22 27 61 20 73 74 72 69 6e 67 5.1", "'a string
19d51 27 22 29 2e 20 49 66 20 74 68 65 20 65 78 70 72 '"). If the expr
19d52 65 73 73 69 6f 6e 20 63 61 6e 0a 2a 2a 20 62 65 ession can.** be
19d53 20 63 6f 6e 76 65 72 74 65 64 20 64 69 72 65 63 converted direc
19d54 74 6c 79 20 69 6e 74 6f 20 61 20 76 61 6c 75 65 tly into a value
19d55 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65 , then the value
19d56 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e is allocated an
19d57 64 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 77 d.** a pointer w
19d58 72 69 74 74 65 6e 20 74 6f 20 2a 70 70 56 61 6c ritten to *ppVal
19d59 2e 20 54 68 65 20 63 61 6c 6c 65 72 20 69 73 20 . The caller is
19d5a 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 responsible for
19d5b 64 65 61 6c 6c 6f 63 61 74 69 6e 67 0a 2a 2a 20 deallocating.**
19d5c 74 68 65 20 76 61 6c 75 65 20 62 79 20 70 61 73 the value by pas
19d5d 73 69 6e 67 20 69 74 20 74 6f 20 73 71 6c 69 74 sing it to sqlit
19d5e 65 33 56 61 6c 75 65 46 72 65 65 28 29 20 6c 61 e3ValueFree() la
19d5f 74 65 72 20 6f 6e 2e 20 49 66 20 74 68 65 20 65 ter on. If the e
19d60 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 63 61 6e xpression.** can
19d61 6e 6f 74 20 62 65 20 63 6f 6e 76 65 72 74 65 64 not be converted
19d62 20 74 6f 20 61 20 76 61 6c 75 65 2c 20 74 68 65 to a value, the
19d63 6e 20 2a 70 70 56 61 6c 20 69 73 20 73 65 74 20 n *ppVal is set
19d64 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c 49 to NULL..*/.SQLI
19d65 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
19d66 71 6c 69 74 65 33 56 61 6c 75 65 46 72 6f 6d 45 qlite3ValueFromE
19d67 78 70 72 28 0a 20 20 73 71 6c 69 74 65 33 20 2a xpr(. sqlite3 *
19d68 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 db,
19d69 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 /* The database
19d6a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 connection */.
19d6b 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 20 20 Expr *pExpr,
19d6c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
19d6d 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 e expression to
19d6e 65 76 61 6c 75 61 74 65 20 2a 2f 0a 20 20 75 38 evaluate */. u8
19d6f 20 65 6e 63 2c 20 20 20 20 20 20 20 20 20 20 20 enc,
19d70 20 20 20 20 20 20 20 20 2f 2a 20 45 6e 63 6f 64 /* Encod
19d71 69 6e 67 20 74 6f 20 75 73 65 20 2a 2f 0a 20 20 ing to use */.
19d72 75 38 20 61 66 66 69 6e 69 74 79 2c 20 20 20 20 u8 affinity,
19d73 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 66 66 /* Aff
19d74 69 6e 69 74 79 20 74 6f 20 75 73 65 20 2a 2f 0a inity to use */.
19d75 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 sqlite3_value
19d76 2a 2a 70 70 56 61 6c 20 20 20 20 20 2f 2a 20 57 **ppVal /* W
19d77 72 69 74 65 20 74 68 65 20 6e 65 77 20 76 61 6c rite the new val
19d78 75 65 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 ue here */.){.
19d79 69 6e 74 20 6f 70 3b 0a 20 20 63 68 61 72 20 2a int op;. char *
19d7a 7a 56 61 6c 20 3d 20 30 3b 0a 20 20 73 71 6c 69 zVal = 0;. sqli
19d7b 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 20 te3_value *pVal
19d7c 3d 20 30 3b 0a 0a 20 20 69 66 28 20 21 70 45 78 = 0;.. if( !pEx
19d7d 70 72 20 29 7b 0a 20 20 20 20 2a 70 70 56 61 6c pr ){. *ppVal
19d7e 20 3d 20 30 3b 0a 20 20 20 20 72 65 74 75 72 6e = 0;. return
19d7f 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a SQLITE_OK;. }.
19d80 20 20 6f 70 20 3d 20 70 45 78 70 72 2d 3e 6f 70 op = pExpr->op
19d81 3b 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 52 ;. if( op==TK_R
19d82 45 47 49 53 54 45 52 20 29 7b 0a 20 20 20 20 6f EGISTER ){. o
19d83 70 20 3d 20 70 45 78 70 72 2d 3e 6f 70 32 3b 0a p = pExpr->op2;.
19d84 20 20 7d 0a 0a 20 20 69 66 28 20 6f 70 3d 3d 54 }.. if( op==T
19d85 4b 5f 53 54 52 49 4e 47 20 7c 7c 20 6f 70 3d 3d K_STRING || op==
19d86 54 4b 5f 46 4c 4f 41 54 20 7c 7c 20 6f 70 3d 3d TK_FLOAT || op==
19d87 54 4b 5f 49 4e 54 45 47 45 52 20 29 7b 0a 20 20 TK_INTEGER ){.
19d88 20 20 70 56 61 6c 20 3d 20 73 71 6c 69 74 65 33 pVal = sqlite3
19d89 56 61 6c 75 65 4e 65 77 28 64 62 29 3b 0a 20 20 ValueNew(db);.
19d8a 20 20 69 66 28 20 70 56 61 6c 3d 3d 30 20 29 20 if( pVal==0 )
19d8b 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 goto no_mem;.
19d8c 20 69 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 if( ExprHasProp
19d8d 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 49 erty(pExpr, EP_I
19d8e 6e 74 56 61 6c 75 65 29 20 29 7b 0a 20 20 20 20 ntValue) ){.
19d8f 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
19d90 53 65 74 49 6e 74 36 34 28 70 56 61 6c 2c 20 28 SetInt64(pVal, (
19d91 69 36 34 29 70 45 78 70 72 2d 3e 75 2e 69 56 61 i64)pExpr->u.iVa
19d92 6c 75 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b lue);. }else{
19d93 0a 20 20 20 20 20 20 7a 56 61 6c 20 3d 20 73 71 . zVal = sq
19d94 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62 lite3DbStrDup(db
19d95 2c 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 , pExpr->u.zToke
19d96 6e 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 56 n);. if( zV
19d97 61 6c 3d 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f al==0 ) goto no_
19d98 6d 65 6d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 mem;. sqlit
19d99 65 33 56 61 6c 75 65 53 65 74 53 74 72 28 70 56 e3ValueSetStr(pV
19d9a 61 6c 2c 20 2d 31 2c 20 7a 56 61 6c 2c 20 53 51 al, -1, zVal, SQ
19d9b 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 LITE_UTF8, SQLIT
19d9c 45 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 E_DYNAMIC);.
19d9d 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 46 4c 4f if( op==TK_FLO
19d9e 41 54 20 29 20 70 56 61 6c 2d 3e 74 79 70 65 20 AT ) pVal->type
19d9f 3d 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 3b 0a = SQLITE_FLOAT;.
19da0 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 28 6f }. if( (o
19da1 70 3d 3d 54 4b 5f 49 4e 54 45 47 45 52 20 7c 7c p==TK_INTEGER ||
19da2 20 6f 70 3d 3d 54 4b 5f 46 4c 4f 41 54 20 29 20 op==TK_FLOAT )
19da3 26 26 20 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c && affinity==SQL
19da4 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 20 29 7b 0a ITE_AFF_NONE ){.
19da5 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c sqlite3Val
19da6 75 65 41 70 70 6c 79 41 66 66 69 6e 69 74 79 28 ueApplyAffinity(
19da7 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f 41 46 46 pVal, SQLITE_AFF
19da8 5f 4e 55 4d 45 52 49 43 2c 20 53 51 4c 49 54 45 _NUMERIC, SQLITE
19da9 5f 55 54 46 38 29 3b 0a 20 20 20 20 7d 65 6c 73 _UTF8);. }els
19daa 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 e{. sqlite3
19dab 56 61 6c 75 65 41 70 70 6c 79 41 66 66 69 6e 69 ValueApplyAffini
19dac 74 79 28 70 56 61 6c 2c 20 61 66 66 69 6e 69 74 ty(pVal, affinit
19dad 79 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 3b y, SQLITE_UTF8);
19dae 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 65 . }. if( e
19daf 6e 63 21 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 nc!=SQLITE_UTF8
19db0 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
19db1 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 VdbeChangeEncodi
19db2 6e 67 28 70 56 61 6c 2c 20 65 6e 63 29 3b 0a 20 ng(pVal, enc);.
19db3 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 }. }else if(
19db4 20 6f 70 3d 3d 54 4b 5f 55 4d 49 4e 55 53 20 29 op==TK_UMINUS )
19db5 20 7b 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54 {. if( SQLIT
19db6 45 5f 4f 4b 3d 3d 73 71 6c 69 74 65 33 56 61 6c E_OK==sqlite3Val
19db7 75 65 46 72 6f 6d 45 78 70 72 28 64 62 2c 70 45 ueFromExpr(db,pE
19db8 78 70 72 2d 3e 70 4c 65 66 74 2c 65 6e 63 2c 61 xpr->pLeft,enc,a
19db9 66 66 69 6e 69 74 79 2c 26 70 56 61 6c 29 20 29 ffinity,&pVal) )
19dba 7b 0a 20 20 20 20 20 20 70 56 61 6c 2d 3e 75 2e {. pVal->u.
19dbb 69 20 3d 20 2d 31 20 2a 20 70 56 61 6c 2d 3e 75 i = -1 * pVal->u
19dbc 2e 69 3b 0a 20 20 20 20 20 20 2f 2a 20 28 64 6f .i;. /* (do
19dbd 75 62 6c 65 29 2d 31 20 49 6e 20 63 61 73 65 20 uble)-1 In case
19dbe 6f 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 of SQLITE_OMIT_F
19dbf 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 2e 2e 2e LOATING_POINT...
19dc0 20 2a 2f 0a 20 20 20 20 20 20 70 56 61 6c 2d 3e */. pVal->
19dc1 72 20 3d 20 28 64 6f 75 62 6c 65 29 2d 31 20 2a r = (double)-1 *
19dc2 20 70 56 61 6c 2d 3e 72 3b 0a 20 20 20 20 7d 0a pVal->r;. }.
19dc3 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 }.#ifndef SQLI
19dc4 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 TE_OMIT_BLOB_LIT
19dc5 45 52 41 4c 0a 20 20 65 6c 73 65 20 69 66 28 20 ERAL. else if(
19dc6 6f 70 3d 3d 54 4b 5f 42 4c 4f 42 20 29 7b 0a 20 op==TK_BLOB ){.
19dc7 20 20 20 69 6e 74 20 6e 56 61 6c 3b 0a 20 20 20 int nVal;.
19dc8 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e assert( pExpr->
19dc9 75 2e 7a 54 6f 6b 65 6e 5b 30 5d 3d 3d 27 78 27 u.zToken[0]=='x'
19dca 20 7c 7c 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f || pExpr->u.zTo
19dcb 6b 65 6e 5b 30 5d 3d 3d 27 58 27 20 29 3b 0a 20 ken[0]=='X' );.
19dcc 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 assert( pExpr
19dcd 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 31 5d 3d 3d 27 ->u.zToken[1]=='
19dce 5c 27 27 20 29 3b 0a 20 20 20 20 70 56 61 6c 20 \'' );. pVal
19dcf 3d 20 73 71 6c 69 74 65 33 56 61 6c 75 65 4e 65 = sqlite3ValueNe
19dd0 77 28 64 62 29 3b 0a 20 20 20 20 69 66 28 20 21 w(db);. if( !
19dd1 70 56 61 6c 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d pVal ) goto no_m
19dd2 65 6d 3b 0a 20 20 20 20 7a 56 61 6c 20 3d 20 26 em;. zVal = &
19dd3 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b pExpr->u.zToken[
19dd4 32 5d 3b 0a 20 20 20 20 6e 56 61 6c 20 3d 20 73 2];. nVal = s
19dd5 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a qlite3Strlen30(z
19dd6 56 61 6c 29 2d 31 3b 0a 20 20 20 20 61 73 73 65 Val)-1;. asse
19dd7 72 74 28 20 7a 56 61 6c 5b 6e 56 61 6c 5d 3d 3d rt( zVal[nVal]==
19dd8 27 5c 27 27 20 29 3b 0a 20 20 20 20 73 71 6c 69 '\'' );. sqli
19dd9 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 te3VdbeMemSetStr
19dda 28 70 56 61 6c 2c 20 73 71 6c 69 74 65 33 48 65 (pVal, sqlite3He
19ddb 78 54 6f 42 6c 6f 62 28 64 62 2c 20 7a 56 61 6c xToBlob(db, zVal
19ddc 2c 20 6e 56 61 6c 29 2c 20 6e 56 61 6c 2f 32 2c , nVal), nVal/2,
19ddd 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
19dde 20 20 20 20 20 20 20 20 20 20 30 2c 20 53 51 4c 0, SQL
19ddf 49 54 45 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 ITE_DYNAMIC);.
19de0 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2a 70 70 56 }.#endif.. *ppV
19de1 61 6c 20 3d 20 70 56 61 6c 3b 0a 20 20 72 65 74 al = pVal;. ret
19de2 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a urn SQLITE_OK;..
19de3 6e 6f 5f 6d 65 6d 3a 0a 20 20 64 62 2d 3e 6d 61 no_mem:. db->ma
19de4 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a llocFailed = 1;.
19de5 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
19de6 64 62 2c 20 7a 56 61 6c 29 3b 0a 20 20 73 71 6c db, zVal);. sql
19de7 69 74 65 33 56 61 6c 75 65 46 72 65 65 28 70 56 ite3ValueFree(pV
19de8 61 6c 29 3b 0a 20 20 2a 70 70 56 61 6c 20 3d 20 al);. *ppVal =
19de9 30 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 0;. return SQLI
19dea 54 45 5f 4e 4f 4d 45 4d 3b 0a 7d 0a 0a 2f 2a 0a TE_NOMEM;.}../*.
19deb 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 73 74 ** Change the st
19dec 72 69 6e 67 20 76 61 6c 75 65 20 6f 66 20 61 6e ring value of an
19ded 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f sqlite3_value o
19dee 62 6a 65 63 74 0a 2a 2f 0a 53 51 4c 49 54 45 5f bject.*/.SQLITE_
19def 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
19df0 69 74 65 33 56 61 6c 75 65 53 65 74 53 74 72 28 ite3ValueSetStr(
19df1 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 . sqlite3_value
19df2 20 2a 76 2c 20 20 20 20 20 2f 2a 20 56 61 6c 75 *v, /* Valu
19df3 65 20 74 6f 20 62 65 20 73 65 74 20 2a 2f 0a 20 e to be set */.
19df4 20 69 6e 74 20 6e 2c 20 20 20 20 20 20 20 20 20 int n,
19df5 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 /* Length
19df6 20 6f 66 20 73 74 72 69 6e 67 20 7a 20 2a 2f 0a of string z */.
19df7 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 2c const void *z,
19df8 20 20 20 20 20 20 20 20 2f 2a 20 54 65 78 74 20 /* Text
19df9 6f 66 20 74 68 65 20 6e 65 77 20 73 74 72 69 6e of the new strin
19dfa 67 20 2a 2f 0a 20 20 75 38 20 65 6e 63 2c 20 20 g */. u8 enc,
19dfb 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
19dfc 45 6e 63 6f 64 69 6e 67 20 74 6f 20 75 73 65 20 Encoding to use
19dfd 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c */. void (*xDel
19dfe 29 28 76 6f 69 64 2a 29 20 20 20 2f 2a 20 44 65 )(void*) /* De
19dff 73 74 72 75 63 74 6f 72 20 66 6f 72 20 74 68 65 structor for the
19e00 20 73 74 72 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 string */.){.
19e01 69 66 28 20 76 20 29 20 73 71 6c 69 74 65 33 56 if( v ) sqlite3V
19e02 64 62 65 4d 65 6d 53 65 74 53 74 72 28 28 4d 65 dbeMemSetStr((Me
19e03 6d 20 2a 29 76 2c 20 7a 2c 20 6e 2c 20 65 6e 63 m *)v, z, n, enc
19e04 2c 20 78 44 65 6c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a , xDel);.}../*.*
19e05 2a 20 46 72 65 65 20 61 6e 20 73 71 6c 69 74 65 * Free an sqlite
19e06 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 0a 2a 3_value object.*
19e07 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
19e08 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 61 6c void sqlite3Val
19e09 75 65 46 72 65 65 28 73 71 6c 69 74 65 33 5f 76 ueFree(sqlite3_v
19e0a 61 6c 75 65 20 2a 76 29 7b 0a 20 20 69 66 28 20 alue *v){. if(
19e0b 21 76 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 73 !v ) return;. s
19e0c 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c qlite3VdbeMemRel
19e0d 65 61 73 65 28 28 4d 65 6d 20 2a 29 76 29 3b 0a ease((Mem *)v);.
19e0e 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
19e0f 28 28 4d 65 6d 2a 29 76 29 2d 3e 64 62 2c 20 76 ((Mem*)v)->db, v
19e10 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 );.}../*.** Retu
19e11 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 rn the number of
19e12 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 73 71 bytes in the sq
19e13 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 lite3_value obje
19e14 63 74 20 61 73 73 75 6d 69 6e 67 0a 2a 2a 20 74 ct assuming.** t
19e15 68 61 74 20 69 74 20 75 73 65 73 20 74 68 65 20 hat it uses the
19e16 65 6e 63 6f 64 69 6e 67 20 22 65 6e 63 22 0a 2a encoding "enc".*
19e17 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
19e18 20 69 6e 74 20 73 71 6c 69 74 65 33 56 61 6c 75 int sqlite3Valu
19e19 65 42 79 74 65 73 28 73 71 6c 69 74 65 33 5f 76 eBytes(sqlite3_v
19e1a 61 6c 75 65 20 2a 70 56 61 6c 2c 20 75 38 20 65 alue *pVal, u8 e
19e1b 6e 63 29 7b 0a 20 20 4d 65 6d 20 2a 70 20 3d 20 nc){. Mem *p =
19e1c 28 4d 65 6d 2a 29 70 56 61 6c 3b 0a 20 20 69 66 (Mem*)pVal;. if
19e1d 28 20 28 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45 ( (p->flags & ME
19e1e 4d 5f 42 6c 6f 62 29 21 3d 30 20 7c 7c 20 73 71 M_Blob)!=0 || sq
19e1f 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74 28 70 lite3ValueText(p
19e20 56 61 6c 2c 20 65 6e 63 29 20 29 7b 0a 20 20 20 Val, enc) ){.
19e21 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 if( p->flags &
19e22 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20 MEM_Zero ){.
19e23 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e 20 2b 20 return p->n +
19e24 70 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 20 20 p->u.nZero;.
19e25 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 65 74 }else{. ret
19e26 75 72 6e 20 70 2d 3e 6e 3b 0a 20 20 20 20 7d 0a urn p->n;. }.
19e27 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a }. return 0;.
19e28 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a }../************
19e29 2a 2a 20 45 6e 64 20 6f 66 20 76 64 62 65 6d 65 ** End of vdbeme
19e2a 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a m.c ************
19e2b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19e2c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19e2d 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
19e2e 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 76 64 ** Begin file vd
19e2f 62 65 61 75 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a beaux.c ********
19e30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19e31 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19e32 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 33 20 53 65 */./*.** 2003 Se
19e33 70 74 65 6d 62 65 72 20 36 0a 2a 2a 0a 2a 2a 20 ptember 6.**.**
19e34 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c The author discl
19e35 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 aims copyright t
19e36 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f o this source co
19e37 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 de. In place of
19e38 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 .** a legal noti
19e39 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c ce, here is a bl
19e3a 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 essing:.**.**
19e3b 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 May you do good
19e3c 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a and not evil..*
19e3d 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e * May you fin
19e3e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f d forgiveness fo
19e3f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 r yourself and f
19e40 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a orgive others..*
19e41 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 * May you sha
19e42 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 re freely, never
19e43 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 taking more tha
19e44 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a n you give..**.*
19e45 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19e46 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19e47 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19e48 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19e49 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 ********.** This
19e4a 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 file contains c
19e4b 6f 64 65 20 75 73 65 64 20 66 6f 72 20 63 72 65 ode used for cre
19e4c 61 74 69 6e 67 2c 20 64 65 73 74 72 6f 79 69 6e ating, destroyin
19e4d 67 2c 20 61 6e 64 20 70 6f 70 75 6c 61 74 69 6e g, and populatin
19e4e 67 0a 2a 2a 20 61 20 56 44 42 45 20 28 6f 72 20 g.** a VDBE (or
19e4f 61 6e 20 22 73 71 6c 69 74 65 33 5f 73 74 6d 74 an "sqlite3_stmt
19e50 22 20 61 73 20 69 74 20 69 73 20 6b 6e 6f 77 6e " as it is known
19e51 20 74 6f 20 74 68 65 20 6f 75 74 73 69 64 65 20 to the outside
19e52 77 6f 72 6c 64 2e 29 20 20 50 72 69 6f 72 0a 2a world.) Prior.*
19e53 2a 20 74 6f 20 76 65 72 73 69 6f 6e 20 32 2e 38 * to version 2.8
19e54 2e 37 2c 20 61 6c 6c 20 74 68 69 73 20 63 6f 64 .7, all this cod
19e55 65 20 77 61 73 20 63 6f 6d 62 69 6e 65 64 20 69 e was combined i
19e56 6e 74 6f 20 74 68 65 20 76 64 62 65 2e 63 20 73 nto the vdbe.c s
19e57 6f 75 72 63 65 20 66 69 6c 65 2e 0a 2a 2a 20 42 ource file..** B
19e58 75 74 20 74 68 61 74 20 66 69 6c 65 20 77 61 73 ut that file was
19e59 20 67 65 74 74 69 6e 67 20 74 6f 6f 20 62 69 67 getting too big
19e5a 20 73 6f 20 74 68 69 73 20 73 75 62 72 6f 75 74 so this subrout
19e5b 69 6e 65 73 20 77 65 72 65 20 73 70 6c 69 74 20 ines were split
19e5c 6f 75 74 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 out..**.** $Id:
19e5d 76 64 62 65 61 75 78 2e 63 2c 76 20 31 2e 34 38 vdbeaux.c,v 1.48
19e5e 30 20 32 30 30 39 2f 30 38 2f 30 38 20 31 38 3a 0 2009/08/08 18:
19e5f 30 31 3a 30 38 20 64 72 68 20 45 78 70 20 24 0a 01:08 drh Exp $.
19e60 2a 2f 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e */..../*.** When
19e61 20 64 65 62 75 67 67 69 6e 67 20 74 68 65 20 63 debugging the c
19e62 6f 64 65 20 67 65 6e 65 72 61 74 6f 72 20 69 6e ode generator in
19e63 20 61 20 73 79 6d 62 6f 6c 69 63 20 64 65 62 75 a symbolic debu
19e64 67 67 65 72 2c 20 6f 6e 65 20 63 61 6e 0a 2a 2a gger, one can.**
19e65 20 73 65 74 20 74 68 65 20 73 71 6c 69 74 65 33 set the sqlite3
19e66 56 64 62 65 41 64 64 6f 70 54 72 61 63 65 20 74 VdbeAddopTrace t
19e67 6f 20 31 20 61 6e 64 20 61 6c 6c 20 6f 70 63 6f o 1 and all opco
19e68 64 65 73 20 77 69 6c 6c 20 62 65 20 70 72 69 6e des will be prin
19e69 74 65 64 0a 2a 2a 20 61 73 20 74 68 65 79 20 61 ted.** as they a
19e6a 72 65 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 re added to the
19e6b 69 6e 73 74 72 75 63 74 69 6f 6e 20 73 74 72 65 instruction stre
19e6c 61 6d 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 am..*/.#ifdef SQ
19e6d 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 LITE_DEBUG.SQLIT
19e6e 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
19e6f 6c 69 74 65 33 56 64 62 65 41 64 64 6f 70 54 72 lite3VdbeAddopTr
19e70 61 63 65 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a ace = 0;.#endif.
19e71 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 ../*.** Create a
19e72 20 6e 65 77 20 76 69 72 74 75 61 6c 20 64 61 74 new virtual dat
19e73 61 62 61 73 65 20 65 6e 67 69 6e 65 2e 0a 2a 2f abase engine..*/
19e74 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
19e75 56 64 62 65 20 2a 73 71 6c 69 74 65 33 56 64 62 Vdbe *sqlite3Vdb
19e76 65 43 72 65 61 74 65 28 73 71 6c 69 74 65 33 20 eCreate(sqlite3
19e77 2a 64 62 29 7b 0a 20 20 56 64 62 65 20 2a 70 3b *db){. Vdbe *p;
19e78 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33 44 62 . p = sqlite3Db
19e79 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 MallocZero(db, s
19e7a 69 7a 65 6f 66 28 56 64 62 65 29 20 29 3b 0a 20 izeof(Vdbe) );.
19e7b 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 if( p==0 ) retu
19e7c 72 6e 20 30 3b 0a 20 20 70 2d 3e 64 62 20 3d 20 rn 0;. p->db =
19e7d 64 62 3b 0a 20 20 69 66 28 20 64 62 2d 3e 70 56 db;. if( db->pV
19e7e 64 62 65 20 29 7b 0a 20 20 20 20 64 62 2d 3e 70 dbe ){. db->p
19e7f 56 64 62 65 2d 3e 70 50 72 65 76 20 3d 20 70 3b Vdbe->pPrev = p;
19e80 0a 20 20 7d 0a 20 20 70 2d 3e 70 4e 65 78 74 20 . }. p->pNext
19e81 3d 20 64 62 2d 3e 70 56 64 62 65 3b 0a 20 20 70 = db->pVdbe;. p
19e82 2d 3e 70 50 72 65 76 20 3d 20 30 3b 0a 20 20 64 ->pPrev = 0;. d
19e83 62 2d 3e 70 56 64 62 65 20 3d 20 70 3b 0a 20 20 b->pVdbe = p;.
19e84 70 2d 3e 6d 61 67 69 63 20 3d 20 56 44 42 45 5f p->magic = VDBE_
19e85 4d 41 47 49 43 5f 49 4e 49 54 3b 0a 20 20 72 65 MAGIC_INIT;. re
19e86 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a turn p;.}../*.**
19e87 20 52 65 6d 65 6d 62 65 72 20 74 68 65 20 53 51 Remember the SQ
19e88 4c 20 73 74 72 69 6e 67 20 66 6f 72 20 61 20 70 L string for a p
19e89 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
19e8a 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 t..*/.SQLITE_PRI
19e8b 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
19e8c 33 56 64 62 65 53 65 74 53 71 6c 28 56 64 62 65 3VdbeSetSql(Vdbe
19e8d 20 2a 70 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 *p, const char
19e8e 2a 7a 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 20 69 *z, int n, int i
19e8f 73 50 72 65 70 61 72 65 56 32 29 7b 0a 20 20 69 sPrepareV2){. i
19e90 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e f( p==0 ) return
19e91 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ;.#ifdef SQLITE_
19e92 4f 4d 49 54 5f 54 52 41 43 45 0a 20 20 69 66 28 OMIT_TRACE. if(
19e93 20 21 69 73 50 72 65 70 61 72 65 56 32 20 29 20 !isPrepareV2 )
19e94 72 65 74 75 72 6e 3b 0a 23 65 6e 64 69 66 0a 20 return;.#endif.
19e95 20 61 73 73 65 72 74 28 20 70 2d 3e 7a 53 71 6c assert( p->zSql
19e96 3d 3d 30 20 29 3b 0a 20 20 70 2d 3e 7a 53 71 6c ==0 );. p->zSql
19e97 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 4e = sqlite3DbStrN
19e98 44 75 70 28 70 2d 3e 64 62 2c 20 7a 2c 20 6e 29 Dup(p->db, z, n)
19e99 3b 0a 20 20 70 2d 3e 69 73 50 72 65 70 61 72 65 ;. p->isPrepare
19e9a 56 32 20 3d 20 69 73 50 72 65 70 61 72 65 56 32 V2 = isPrepareV2
19e9b 20 3f 20 31 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a 0a ? 1 : 0;.}../*.
19e9c 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 53 51 ** Return the SQ
19e9d 4c 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 L associated wit
19e9e 68 20 61 20 70 72 65 70 61 72 65 64 20 73 74 61 h a prepared sta
19e9f 74 65 6d 65 6e 74 0a 2a 2f 0a 53 51 4c 49 54 45 tement.*/.SQLITE
19ea0 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 _API const char
19ea1 2a 73 71 6c 69 74 65 33 5f 73 71 6c 28 73 71 6c *sqlite3_sql(sql
19ea2 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 ite3_stmt *pStmt
19ea3 29 7b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 ){. Vdbe *p = (
19ea4 56 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 20 Vdbe *)pStmt;.
19ea5 72 65 74 75 72 6e 20 28 70 2d 3e 69 73 50 72 65 return (p->isPre
19ea6 70 61 72 65 56 32 20 3f 20 70 2d 3e 7a 53 71 6c pareV2 ? p->zSql
19ea7 20 3a 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 : 0);.}../*.**
19ea8 53 77 61 70 20 61 6c 6c 20 63 6f 6e 74 65 6e 74 Swap all content
19ea9 20 62 65 74 77 65 65 6e 20 74 77 6f 20 56 44 42 between two VDB
19eaa 45 20 73 74 72 75 63 74 75 72 65 73 2e 0a 2a 2f E structures..*/
19eab 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
19eac 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
19ead 53 77 61 70 28 56 64 62 65 20 2a 70 41 2c 20 56 Swap(Vdbe *pA, V
19eae 64 62 65 20 2a 70 42 29 7b 0a 20 20 56 64 62 65 dbe *pB){. Vdbe
19eaf 20 74 6d 70 2c 20 2a 70 54 6d 70 3b 0a 20 20 63 tmp, *pTmp;. c
19eb0 68 61 72 20 2a 7a 54 6d 70 3b 0a 20 20 74 6d 70 har *zTmp;. tmp
19eb1 20 3d 20 2a 70 41 3b 0a 20 20 2a 70 41 20 3d 20 = *pA;. *pA =
19eb2 2a 70 42 3b 0a 20 20 2a 70 42 20 3d 20 74 6d 70 *pB;. *pB = tmp
19eb3 3b 0a 20 20 70 54 6d 70 20 3d 20 70 41 2d 3e 70 ;. pTmp = pA->p
19eb4 4e 65 78 74 3b 0a 20 20 70 41 2d 3e 70 4e 65 78 Next;. pA->pNex
19eb5 74 20 3d 20 70 42 2d 3e 70 4e 65 78 74 3b 0a 20 t = pB->pNext;.
19eb6 20 70 42 2d 3e 70 4e 65 78 74 20 3d 20 70 54 6d pB->pNext = pTm
19eb7 70 3b 0a 20 20 70 54 6d 70 20 3d 20 70 41 2d 3e p;. pTmp = pA->
19eb8 70 50 72 65 76 3b 0a 20 20 70 41 2d 3e 70 50 72 pPrev;. pA->pPr
19eb9 65 76 20 3d 20 70 42 2d 3e 70 50 72 65 76 3b 0a ev = pB->pPrev;.
19eba 20 20 70 42 2d 3e 70 50 72 65 76 20 3d 20 70 54 pB->pPrev = pT
19ebb 6d 70 3b 0a 20 20 7a 54 6d 70 20 3d 20 70 41 2d mp;. zTmp = pA-
19ebc 3e 7a 53 71 6c 3b 0a 20 20 70 41 2d 3e 7a 53 71 >zSql;. pA->zSq
19ebd 6c 20 3d 20 70 42 2d 3e 7a 53 71 6c 3b 0a 20 20 l = pB->zSql;.
19ebe 70 42 2d 3e 7a 53 71 6c 20 3d 20 7a 54 6d 70 3b pB->zSql = zTmp;
19ebf 0a 20 20 70 42 2d 3e 69 73 50 72 65 70 61 72 65 . pB->isPrepare
19ec0 56 32 20 3d 20 70 41 2d 3e 69 73 50 72 65 70 61 V2 = pA->isPrepa
19ec1 72 65 56 32 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 reV2;.}..#ifdef
19ec2 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 2f 2a 0a SQLITE_DEBUG./*.
19ec3 2a 2a 20 54 75 72 6e 20 74 72 61 63 69 6e 67 20 ** Turn tracing
19ec4 6f 6e 20 6f 72 20 6f 66 66 0a 2a 2f 0a 53 51 4c on or off.*/.SQL
19ec5 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
19ec6 20 73 71 6c 69 74 65 33 56 64 62 65 54 72 61 63 sqlite3VdbeTrac
19ec7 65 28 56 64 62 65 20 2a 70 2c 20 46 49 4c 45 20 e(Vdbe *p, FILE
19ec8 2a 74 72 61 63 65 29 7b 0a 20 20 70 2d 3e 74 72 *trace){. p->tr
19ec9 61 63 65 20 3d 20 74 72 61 63 65 3b 0a 7d 0a 23 ace = trace;.}.#
19eca 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 endif../*.** Res
19ecb 69 7a 65 20 74 68 65 20 56 64 62 65 2e 61 4f 70 ize the Vdbe.aOp
19ecc 20 61 72 72 61 79 20 73 6f 20 74 68 61 74 20 69 array so that i
19ecd 74 20 69 73 20 61 74 20 6c 65 61 73 74 20 6f 6e t is at least on
19ece 65 20 6f 70 20 6c 61 72 67 65 72 20 74 68 61 6e e op larger than
19ecf 20 0a 2a 2a 20 69 74 20 77 61 73 2e 0a 2a 2a 0a .** it was..**.
19ed0 2a 2a 20 49 66 20 61 6e 20 6f 75 74 2d 6f 66 2d ** If an out-of-
19ed1 6d 65 6d 6f 72 79 20 65 72 72 6f 72 20 6f 63 63 memory error occ
19ed2 75 72 73 20 77 68 69 6c 65 20 72 65 73 69 7a 69 urs while resizi
19ed3 6e 67 20 74 68 65 20 61 72 72 61 79 2c 20 72 65 ng the array, re
19ed4 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e turn.** SQLITE_N
19ed5 4f 4d 45 4d 2e 20 49 6e 20 74 68 69 73 20 63 61 OMEM. In this ca
19ed6 73 65 20 56 64 62 65 2e 61 4f 70 20 61 6e 64 20 se Vdbe.aOp and
19ed7 56 64 62 65 2e 6e 4f 70 41 6c 6c 6f 63 20 72 65 Vdbe.nOpAlloc re
19ed8 6d 61 69 6e 20 0a 2a 2a 20 75 6e 63 68 61 6e 67 main .** unchang
19ed9 65 64 20 28 74 68 69 73 20 69 73 20 73 6f 20 74 ed (this is so t
19eda 68 61 74 20 61 6e 79 20 6f 70 63 6f 64 65 73 20 hat any opcodes
19edb 61 6c 72 65 61 64 79 20 61 6c 6c 6f 63 61 74 65 already allocate
19edc 64 20 63 61 6e 20 62 65 20 0a 2a 2a 20 63 6f 72 d can be .** cor
19edd 72 65 63 74 6c 79 20 64 65 61 6c 6c 6f 63 61 74 rectly deallocat
19ede 65 64 20 61 6c 6f 6e 67 20 77 69 74 68 20 74 68 ed along with th
19edf 65 20 72 65 73 74 20 6f 66 20 74 68 65 20 56 64 e rest of the Vd
19ee0 62 65 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 be)..*/.static i
19ee1 6e 74 20 67 72 6f 77 4f 70 41 72 72 61 79 28 56 nt growOpArray(V
19ee2 64 62 65 20 2a 70 29 7b 0a 20 20 56 64 62 65 4f dbe *p){. VdbeO
19ee3 70 20 2a 70 4e 65 77 3b 0a 20 20 69 6e 74 20 6e p *pNew;. int n
19ee4 4e 65 77 20 3d 20 28 70 2d 3e 6e 4f 70 41 6c 6c New = (p->nOpAll
19ee5 6f 63 20 3f 20 70 2d 3e 6e 4f 70 41 6c 6c 6f 63 oc ? p->nOpAlloc
19ee6 2a 32 20 3a 20 28 69 6e 74 29 28 31 30 32 34 2f *2 : (int)(1024/
19ee7 73 69 7a 65 6f 66 28 4f 70 29 29 29 3b 0a 20 20 sizeof(Op)));.
19ee8 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 pNew = sqlite3Db
19ee9 52 65 61 6c 6c 6f 63 28 70 2d 3e 64 62 2c 20 70 Realloc(p->db, p
19eea 2d 3e 61 4f 70 2c 20 6e 4e 65 77 2a 73 69 7a 65 ->aOp, nNew*size
19eeb 6f 66 28 4f 70 29 29 3b 0a 20 20 69 66 28 20 70 of(Op));. if( p
19eec 4e 65 77 20 29 7b 0a 20 20 20 20 70 2d 3e 6e 4f New ){. p->nO
19eed 70 41 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 65 33 pAlloc = sqlite3
19eee 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 2d 3e DbMallocSize(p->
19eef 64 62 2c 20 70 4e 65 77 29 2f 73 69 7a 65 6f 66 db, pNew)/sizeof
19ef0 28 4f 70 29 3b 0a 20 20 20 20 70 2d 3e 61 4f 70 (Op);. p->aOp
19ef1 20 3d 20 70 4e 65 77 3b 0a 20 20 7d 0a 20 20 72 = pNew;. }. r
19ef2 65 74 75 72 6e 20 28 70 4e 65 77 20 3f 20 53 51 eturn (pNew ? SQ
19ef3 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 LITE_OK : SQLITE
19ef4 5f 4e 4f 4d 45 4d 29 3b 0a 7d 0a 0a 2f 2a 0a 2a _NOMEM);.}../*.*
19ef5 2a 20 41 64 64 20 61 20 6e 65 77 20 69 6e 73 74 * Add a new inst
19ef6 72 75 63 74 69 6f 6e 20 74 6f 20 74 68 65 20 6c ruction to the l
19ef7 69 73 74 20 6f 66 20 69 6e 73 74 72 75 63 74 69 ist of instructi
19ef8 6f 6e 73 20 63 75 72 72 65 6e 74 20 69 6e 20 74 ons current in t
19ef9 68 65 0a 2a 2a 20 56 44 42 45 2e 20 20 52 65 74 he.** VDBE. Ret
19efa 75 72 6e 20 74 68 65 20 61 64 64 72 65 73 73 20 urn the address
19efb 6f 66 20 74 68 65 20 6e 65 77 20 69 6e 73 74 72 of the new instr
19efc 75 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 50 61 uction..**.** Pa
19efd 72 61 6d 65 74 65 72 73 3a 0a 2a 2a 0a 2a 2a 20 rameters:.**.**
19efe 20 20 20 70 20 20 20 20 20 20 20 20 20 20 20 20 p
19eff 20 20 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 Pointer to th
19f00 65 20 56 44 42 45 0a 2a 2a 0a 2a 2a 20 20 20 20 e VDBE.**.**
19f01 6f 70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 op
19f02 54 68 65 20 6f 70 63 6f 64 65 20 66 6f 72 20 74 The opcode for t
19f03 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 0a his instruction.
19f04 2a 2a 0a 2a 2a 20 20 20 20 70 31 2c 20 70 32 2c **.** p1, p2,
19f05 20 70 33 20 20 20 20 20 20 4f 70 65 72 61 6e 64 p3 Operand
19f06 73 0a 2a 2a 0a 2a 2a 20 55 73 65 20 74 68 65 20 s.**.** Use the
19f07 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c sqlite3VdbeResol
19f08 76 65 4c 61 62 65 6c 28 29 20 66 75 6e 63 74 69 veLabel() functi
19f09 6f 6e 20 74 6f 20 66 69 78 20 61 6e 20 61 64 64 on to fix an add
19f0a 72 65 73 73 20 61 6e 64 0a 2a 2a 20 74 68 65 20 ress and.** the
19f0b 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 sqlite3VdbeChang
19f0c 65 50 34 28 29 20 66 75 6e 63 74 69 6f 6e 20 74 eP4() function t
19f0d 6f 20 63 68 61 6e 67 65 20 74 68 65 20 76 61 6c o change the val
19f0e 75 65 20 6f 66 20 74 68 65 20 50 34 0a 2a 2a 20 ue of the P4.**
19f0f 6f 70 65 72 61 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 operand..*/.SQLI
19f10 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
19f11 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 qlite3VdbeAddOp3
19f12 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 6f 70 (Vdbe *p, int op
19f13 2c 20 69 6e 74 20 70 31 2c 20 69 6e 74 20 70 32 , int p1, int p2
19f14 2c 20 69 6e 74 20 70 33 29 7b 0a 20 20 69 6e 74 , int p3){. int
19f15 20 69 3b 0a 20 20 56 64 62 65 4f 70 20 2a 70 4f i;. VdbeOp *pO
19f16 70 3b 0a 0a 20 20 69 20 3d 20 70 2d 3e 6e 4f 70 p;.. i = p->nOp
19f17 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6d ;. assert( p->m
19f18 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 agic==VDBE_MAGIC
19f19 5f 49 4e 49 54 20 29 3b 0a 20 20 61 73 73 65 72 _INIT );. asser
19f1a 74 28 20 6f 70 3e 30 20 26 26 20 6f 70 3c 30 78 t( op>0 && op<0x
19f1b 66 66 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e ff );. if( p->n
19f1c 4f 70 41 6c 6c 6f 63 3c 3d 69 20 29 7b 0a 20 20 OpAlloc<=i ){.
19f1d 20 20 69 66 28 20 67 72 6f 77 4f 70 41 72 72 61 if( growOpArra
19f1e 79 28 70 29 20 29 7b 0a 20 20 20 20 20 20 72 65 y(p) ){. re
19f1f 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 turn 1;. }.
19f20 7d 0a 20 20 70 2d 3e 6e 4f 70 2b 2b 3b 0a 20 20 }. p->nOp++;.
19f21 70 4f 70 20 3d 20 26 70 2d 3e 61 4f 70 5b 69 5d pOp = &p->aOp[i]
19f22 3b 0a 20 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 ;. pOp->opcode
19f23 3d 20 28 75 38 29 6f 70 3b 0a 20 20 70 4f 70 2d = (u8)op;. pOp-
19f24 3e 70 35 20 3d 20 30 3b 0a 20 20 70 4f 70 2d 3e >p5 = 0;. pOp->
19f25 70 31 20 3d 20 70 31 3b 0a 20 20 70 4f 70 2d 3e p1 = p1;. pOp->
19f26 70 32 20 3d 20 70 32 3b 0a 20 20 70 4f 70 2d 3e p2 = p2;. pOp->
19f27 70 33 20 3d 20 70 33 3b 0a 20 20 70 4f 70 2d 3e p3 = p3;. pOp->
19f28 70 34 2e 70 20 3d 20 30 3b 0a 20 20 70 4f 70 2d p4.p = 0;. pOp-
19f29 3e 70 34 74 79 70 65 20 3d 20 50 34 5f 4e 4f 54 >p4type = P4_NOT
19f2a 55 53 45 44 3b 0a 20 20 70 2d 3e 65 78 70 69 72 USED;. p->expir
19f2b 65 64 20 3d 20 30 3b 0a 23 69 66 64 65 66 20 53 ed = 0;.#ifdef S
19f2c 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 70 4f QLITE_DEBUG. pO
19f2d 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 20 3d 20 30 3b p->zComment = 0;
19f2e 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 . if( sqlite3Vd
19f2f 62 65 41 64 64 6f 70 54 72 61 63 65 20 29 20 73 beAddopTrace ) s
19f30 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e 74 4f qlite3VdbePrintO
19f31 70 28 30 2c 20 69 2c 20 26 70 2d 3e 61 4f 70 5b p(0, i, &p->aOp[
19f32 69 5d 29 3b 0a 23 65 6e 64 69 66 0a 23 69 66 64 i]);.#endif.#ifd
19f33 65 66 20 56 44 42 45 5f 50 52 4f 46 49 4c 45 0a ef VDBE_PROFILE.
19f34 20 20 70 4f 70 2d 3e 63 79 63 6c 65 73 20 3d 20 pOp->cycles =
19f35 30 3b 0a 20 20 70 4f 70 2d 3e 63 6e 74 20 3d 20 0;. pOp->cnt =
19f36 30 3b 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 0;.#endif. retu
19f37 72 6e 20 69 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 rn i;.}.SQLITE_P
19f38 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
19f39 65 33 56 64 62 65 41 64 64 4f 70 30 28 56 64 62 e3VdbeAddOp0(Vdb
19f3a 65 20 2a 70 2c 20 69 6e 74 20 6f 70 29 7b 0a 20 e *p, int op){.
19f3b 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56 return sqlite3V
19f3c 64 62 65 41 64 64 4f 70 33 28 70 2c 20 6f 70 2c dbeAddOp3(p, op,
19f3d 20 30 2c 20 30 2c 20 30 29 3b 0a 7d 0a 53 51 4c 0, 0, 0);.}.SQL
19f3e 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
19f3f 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
19f40 31 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 6f 1(Vdbe *p, int o
19f41 70 2c 20 69 6e 74 20 70 31 29 7b 0a 20 20 72 65 p, int p1){. re
19f42 74 75 72 6e 20 73 71 6c 69 74 65 33 56 64 62 65 turn sqlite3Vdbe
19f43 41 64 64 4f 70 33 28 70 2c 20 6f 70 2c 20 70 31 AddOp3(p, op, p1
19f44 2c 20 30 2c 20 30 29 3b 0a 7d 0a 53 51 4c 49 54 , 0, 0);.}.SQLIT
19f45 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
19f46 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 lite3VdbeAddOp2(
19f47 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 6f 70 2c Vdbe *p, int op,
19f48 20 69 6e 74 20 70 31 2c 20 69 6e 74 20 70 32 29 int p1, int p2)
19f49 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 {. return sqlit
19f4a 65 33 56 64 62 65 41 64 64 4f 70 33 28 70 2c 20 e3VdbeAddOp3(p,
19f4b 6f 70 2c 20 70 31 2c 20 70 32 2c 20 30 29 3b 0a op, p1, p2, 0);.
19f4c 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 6e }.../*.** Add an
19f4d 20 6f 70 63 6f 64 65 20 74 68 61 74 20 69 6e 63 opcode that inc
19f4e 6c 75 64 65 73 20 74 68 65 20 70 34 20 76 61 6c ludes the p4 val
19f4f 75 65 20 61 73 20 61 20 70 6f 69 6e 74 65 72 2e ue as a pointer.
19f50 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
19f51 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
19f52 62 65 41 64 64 4f 70 34 28 0a 20 20 56 64 62 65 beAddOp4(. Vdbe
19f53 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 *p,
19f54 2f 2a 20 41 64 64 20 74 68 65 20 6f 70 63 6f 64 /* Add the opcod
19f55 65 20 74 6f 20 74 68 69 73 20 56 4d 20 2a 2f 0a e to this VM */.
19f56 20 20 69 6e 74 20 6f 70 2c 20 20 20 20 20 20 20 int op,
19f57 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 /* The new
19f58 20 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 opcode */. int
19f59 20 70 31 2c 20 20 20 20 20 20 20 20 20 20 20 20 p1,
19f5a 20 2f 2a 20 54 68 65 20 50 31 20 6f 70 65 72 61 /* The P1 opera
19f5b 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 70 32 2c 20 nd */. int p2,
19f5c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
19f5d 68 65 20 50 32 20 6f 70 65 72 61 6e 64 20 2a 2f he P2 operand */
19f5e 0a 20 20 69 6e 74 20 70 33 2c 20 20 20 20 20 20 . int p3,
19f5f 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 50 33 /* The P3
19f60 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 63 6f operand */. co
19f61 6e 73 74 20 63 68 61 72 20 2a 7a 50 34 2c 20 20 nst char *zP4,
19f62 20 20 2f 2a 20 54 68 65 20 50 34 20 6f 70 65 72 /* The P4 oper
19f63 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 70 34 74 and */. int p4t
19f64 79 70 65 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ype /*
19f65 50 34 20 6f 70 65 72 61 6e 64 20 74 79 70 65 20 P4 operand type
19f66 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 61 64 64 72 */.){. int addr
19f67 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 = sqlite3VdbeAd
19f68 64 4f 70 33 28 70 2c 20 6f 70 2c 20 70 31 2c 20 dOp3(p, op, p1,
19f69 70 32 2c 20 70 33 29 3b 0a 20 20 73 71 6c 69 74 p2, p3);. sqlit
19f6a 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 70 e3VdbeChangeP4(p
19f6b 2c 20 61 64 64 72 2c 20 7a 50 34 2c 20 70 34 74 , addr, zP4, p4t
19f6c 79 70 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 61 ype);. return a
19f6d 64 64 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 ddr;.}../*.** Cr
19f6e 65 61 74 65 20 61 20 6e 65 77 20 73 79 6d 62 6f eate a new symbo
19f6f 6c 69 63 20 6c 61 62 65 6c 20 66 6f 72 20 61 6e lic label for an
19f70 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 68 61 instruction tha
19f71 74 20 68 61 73 20 79 65 74 20 74 6f 20 62 65 0a t has yet to be.
19f72 2a 2a 20 63 6f 64 65 64 2e 20 20 54 68 65 20 73 ** coded. The s
19f73 79 6d 62 6f 6c 69 63 20 6c 61 62 65 6c 20 69 73 ymbolic label is
19f74 20 72 65 61 6c 6c 79 20 6a 75 73 74 20 61 20 6e really just a n
19f75 65 67 61 74 69 76 65 20 6e 75 6d 62 65 72 2e 20 egative number.
19f76 20 54 68 65 0a 2a 2a 20 6c 61 62 65 6c 20 63 61 The.** label ca
19f77 6e 20 62 65 20 75 73 65 64 20 61 73 20 74 68 65 n be used as the
19f78 20 50 32 20 76 61 6c 75 65 20 6f 66 20 61 6e 20 P2 value of an
19f79 6f 70 65 72 61 74 69 6f 6e 2e 20 20 4c 61 74 65 operation. Late
19f7a 72 2c 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20 6c r, when.** the l
19f7b 61 62 65 6c 20 69 73 20 72 65 73 6f 6c 76 65 64 abel is resolved
19f7c 20 74 6f 20 61 20 73 70 65 63 69 66 69 63 20 61 to a specific a
19f7d 64 64 72 65 73 73 2c 20 74 68 65 20 56 44 42 45 ddress, the VDBE
19f7e 20 77 69 6c 6c 20 73 63 61 6e 0a 2a 2a 20 74 68 will scan.** th
19f7f 72 6f 75 67 68 20 69 74 73 20 6f 70 65 72 61 74 rough its operat
19f80 69 6f 6e 20 6c 69 73 74 20 61 6e 64 20 63 68 61 ion list and cha
19f81 6e 67 65 20 61 6c 6c 20 76 61 6c 75 65 73 20 6f nge all values o
19f82 66 20 50 32 20 77 68 69 63 68 20 6d 61 74 63 68 f P2 which match
19f83 0a 2a 2a 20 74 68 65 20 6c 61 62 65 6c 20 69 6e .** the label in
19f84 74 6f 20 74 68 65 20 72 65 73 6f 6c 76 65 64 20 to the resolved
19f85 61 64 64 72 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 54 address..**.** T
19f86 68 65 20 56 44 42 45 20 6b 6e 6f 77 73 20 74 68 he VDBE knows th
19f87 61 74 20 61 20 50 32 20 76 61 6c 75 65 20 69 73 at a P2 value is
19f88 20 61 20 6c 61 62 65 6c 20 62 65 63 61 75 73 65 a label because
19f89 20 6c 61 62 65 6c 73 20 61 72 65 0a 2a 2a 20 61 labels are.** a
19f8a 6c 77 61 79 73 20 6e 65 67 61 74 69 76 65 20 61 lways negative a
19f8b 6e 64 20 50 32 20 76 61 6c 75 65 73 20 61 72 65 nd P2 values are
19f8c 20 73 75 70 70 6f 73 65 20 74 6f 20 62 65 20 6e suppose to be n
19f8d 6f 6e 2d 6e 65 67 61 74 69 76 65 2e 0a 2a 2a 20 on-negative..**
19f8e 48 65 6e 63 65 2c 20 61 20 6e 65 67 61 74 69 76 Hence, a negativ
19f8f 65 20 50 32 20 76 61 6c 75 65 20 69 73 20 61 20 e P2 value is a
19f90 6c 61 62 65 6c 20 74 68 61 74 20 68 61 73 20 79 label that has y
19f91 65 74 20 74 6f 20 62 65 20 72 65 73 6f 6c 76 65 et to be resolve
19f92 64 2e 0a 2a 2a 0a 2a 2a 20 5a 65 72 6f 20 69 73 d..**.** Zero is
19f93 20 72 65 74 75 72 6e 65 64 20 69 66 20 61 20 6d returned if a m
19f94 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73 2e 0a 2a alloc() fails..*
19f95 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
19f96 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 int sqlite3Vdbe
19f97 4d 61 6b 65 4c 61 62 65 6c 28 56 64 62 65 20 2a MakeLabel(Vdbe *
19f98 70 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 p){. int i;. i
19f99 20 3d 20 70 2d 3e 6e 4c 61 62 65 6c 2b 2b 3b 0a = p->nLabel++;.
19f9a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 assert( p->mag
19f9b 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 ic==VDBE_MAGIC_I
19f9c 4e 49 54 20 29 3b 0a 20 20 69 66 28 20 69 3e 3d NIT );. if( i>=
19f9d 70 2d 3e 6e 4c 61 62 65 6c 41 6c 6c 6f 63 20 29 p->nLabelAlloc )
19f9e 7b 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20 70 2d {. int n = p-
19f9f 3e 6e 4c 61 62 65 6c 41 6c 6c 6f 63 2a 32 20 2b >nLabelAlloc*2 +
19fa0 20 35 3b 0a 20 20 20 20 70 2d 3e 61 4c 61 62 65 5;. p->aLabe
19fa1 6c 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61 l = sqlite3DbRea
19fa2 6c 6c 6f 63 4f 72 46 72 65 65 28 70 2d 3e 64 62 llocOrFree(p->db
19fa3 2c 20 70 2d 3e 61 4c 61 62 65 6c 2c 0a 20 20 20 , p->aLabel,.
19fa4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19fa5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19fa6 20 20 20 20 6e 2a 73 69 7a 65 6f 66 28 70 2d 3e n*sizeof(p->
19fa7 61 4c 61 62 65 6c 5b 30 5d 29 29 3b 0a 20 20 20 aLabel[0]));.
19fa8 20 70 2d 3e 6e 4c 61 62 65 6c 41 6c 6c 6f 63 20 p->nLabelAlloc
19fa9 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f = sqlite3DbMallo
19faa 63 53 69 7a 65 28 70 2d 3e 64 62 2c 20 70 2d 3e cSize(p->db, p->
19fab 61 4c 61 62 65 6c 29 2f 73 69 7a 65 6f 66 28 70 aLabel)/sizeof(p
19fac 2d 3e 61 4c 61 62 65 6c 5b 30 5d 29 3b 0a 20 20 ->aLabel[0]);.
19fad 7d 0a 20 20 69 66 28 20 70 2d 3e 61 4c 61 62 65 }. if( p->aLabe
19fae 6c 20 29 7b 0a 20 20 20 20 70 2d 3e 61 4c 61 62 l ){. p->aLab
19faf 65 6c 5b 69 5d 20 3d 20 2d 31 3b 0a 20 20 7d 0a el[i] = -1;. }.
19fb0 20 20 72 65 74 75 72 6e 20 2d 31 2d 69 3b 0a 7d return -1-i;.}
19fb1 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 6f 6c 76 65 20 ../*.** Resolve
19fb2 6c 61 62 65 6c 20 22 78 22 20 74 6f 20 62 65 20 label "x" to be
19fb3 74 68 65 20 61 64 64 72 65 73 73 20 6f 66 20 74 the address of t
19fb4 68 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74 he next instruct
19fb5 69 6f 6e 20 74 6f 0a 2a 2a 20 62 65 20 69 6e 73 ion to.** be ins
19fb6 65 72 74 65 64 2e 20 20 54 68 65 20 70 61 72 61 erted. The para
19fb7 6d 65 74 65 72 20 22 78 22 20 6d 75 73 74 20 68 meter "x" must h
19fb8 61 76 65 20 62 65 65 6e 20 6f 62 74 61 69 6e 65 ave been obtaine
19fb9 64 20 66 72 6f 6d 0a 2a 2a 20 61 20 70 72 69 6f d from.** a prio
19fba 72 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 r call to sqlite
19fbb 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 29 3VdbeMakeLabel()
19fbc 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
19fbd 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
19fbe 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c VdbeResolveLabel
19fbf 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 78 29 (Vdbe *p, int x)
19fc0 7b 0a 20 20 69 6e 74 20 6a 20 3d 20 2d 31 2d 78 {. int j = -1-x
19fc1 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6d ;. assert( p->m
19fc2 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 agic==VDBE_MAGIC
19fc3 5f 49 4e 49 54 20 29 3b 0a 20 20 61 73 73 65 72 _INIT );. asser
19fc4 74 28 20 6a 3e 3d 30 20 26 26 20 6a 3c 70 2d 3e t( j>=0 && j<p->
19fc5 6e 4c 61 62 65 6c 20 29 3b 0a 20 20 69 66 28 20 nLabel );. if(
19fc6 70 2d 3e 61 4c 61 62 65 6c 20 29 7b 0a 20 20 20 p->aLabel ){.
19fc7 20 70 2d 3e 61 4c 61 62 65 6c 5b 6a 5d 20 3d 20 p->aLabel[j] =
19fc8 70 2d 3e 6e 4f 70 3b 0a 20 20 7d 0a 7d 0a 0a 23 p->nOp;. }.}..#
19fc9 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 ifdef SQLITE_DEB
19fca 55 47 20 2f 2a 20 73 71 6c 69 74 65 33 41 73 73 UG /* sqlite3Ass
19fcb 65 72 74 4d 61 79 41 62 6f 72 74 28 29 20 6c 6f ertMayAbort() lo
19fcc 67 69 63 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 gic */../*.** Th
19fcd 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 79 70 65 e following type
19fce 20 61 6e 64 20 66 75 6e 63 74 69 6f 6e 20 61 72 and function ar
19fcf 65 20 75 73 65 64 20 74 6f 20 69 74 65 72 61 74 e used to iterat
19fd0 65 20 74 68 72 6f 75 67 68 20 61 6c 6c 20 6f 70 e through all op
19fd1 63 6f 64 65 73 0a 2a 2a 20 69 6e 20 61 20 56 64 codes.** in a Vd
19fd2 62 65 20 6d 61 69 6e 20 70 72 6f 67 72 61 6d 20 be main program
19fd3 61 6e 64 20 65 61 63 68 20 6f 66 20 74 68 65 20 and each of the
19fd4 73 75 62 2d 70 72 6f 67 72 61 6d 73 20 28 74 72 sub-programs (tr
19fd5 69 67 67 65 72 73 29 20 69 74 20 6d 61 79 20 0a iggers) it may .
19fd6 2a 2a 20 69 6e 76 6f 6b 65 20 64 69 72 65 63 74 ** invoke direct
19fd7 6c 79 20 6f 72 20 69 6e 64 69 72 65 63 74 6c 79 ly or indirectly
19fd8 2e 20 49 74 20 73 68 6f 75 6c 64 20 62 65 20 75 . It should be u
19fd9 73 65 64 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a sed as follows:.
19fda 2a 2a 0a 2a 2a 20 20 20 4f 70 20 2a 70 4f 70 3b **.** Op *pOp;
19fdb 0a 2a 2a 20 20 20 56 64 62 65 4f 70 49 74 65 72 .** VdbeOpIter
19fdc 20 73 49 74 65 72 3b 0a 2a 2a 0a 2a 2a 20 20 20 sIter;.**.**
19fdd 6d 65 6d 73 65 74 28 26 73 49 74 65 72 2c 20 30 memset(&sIter, 0
19fde 2c 20 73 69 7a 65 6f 66 28 73 49 74 65 72 29 29 , sizeof(sIter))
19fdf 3b 0a 2a 2a 20 20 20 73 49 74 65 72 2e 76 20 3d ;.** sIter.v =
19fe0 20 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 v;
19fe1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
19fe2 2f 20 76 20 69 73 20 6f 66 20 74 79 70 65 20 56 / v is of type V
19fe3 64 62 65 2a 20 0a 2a 2a 20 20 20 77 68 69 6c 65 dbe* .** while
19fe4 28 20 28 70 4f 70 20 3d 20 6f 70 49 74 65 72 4e ( (pOp = opIterN
19fe5 65 78 74 28 26 73 49 74 65 72 29 29 20 29 7b 0a ext(&sIter)) ){.
19fe6 2a 2a 20 20 20 20 20 2f 2f 20 44 6f 20 73 6f 6d ** // Do som
19fe7 65 74 68 69 6e 67 20 77 69 74 68 20 70 4f 70 0a ething with pOp.
19fe8 2a 2a 20 20 20 7d 0a 2a 2a 20 20 20 73 71 6c 69 ** }.** sqli
19fe9 74 65 33 44 62 46 72 65 65 28 76 2d 3e 64 62 2c te3DbFree(v->db,
19fea 20 73 49 74 65 72 2e 61 70 53 75 62 29 3b 0a 2a sIter.apSub);.*
19feb 2a 20 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 * .*/.typedef st
19fec 72 75 63 74 20 56 64 62 65 4f 70 49 74 65 72 20 ruct VdbeOpIter
19fed 56 64 62 65 4f 70 49 74 65 72 3b 0a 73 74 72 75 VdbeOpIter;.stru
19fee 63 74 20 56 64 62 65 4f 70 49 74 65 72 20 7b 0a ct VdbeOpIter {.
19fef 20 20 56 64 62 65 20 2a 76 3b 20 20 20 20 20 20 Vdbe *v;
19ff0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
19ff1 56 64 62 65 20 74 6f 20 69 74 65 72 61 74 65 20 Vdbe to iterate
19ff2 74 68 72 6f 75 67 68 20 74 68 65 20 6f 70 63 6f through the opco
19ff3 64 65 73 20 6f 66 20 2a 2f 0a 20 20 53 75 62 50 des of */. SubP
19ff4 72 6f 67 72 61 6d 20 2a 2a 61 70 53 75 62 3b 20 rogram **apSub;
19ff5 20 20 20 20 20 20 20 2f 2a 20 41 72 72 61 79 20 /* Array
19ff6 6f 66 20 73 75 62 70 72 6f 67 72 61 6d 73 20 2a of subprograms *
19ff7 2f 0a 20 20 69 6e 74 20 6e 53 75 62 3b 20 20 20 /. int nSub;
19ff8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
19ff9 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 * Number of entr
19ffa 69 65 73 20 69 6e 20 61 70 53 75 62 20 2a 2f 0a ies in apSub */.
19ffb 20 20 69 6e 74 20 69 41 64 64 72 3b 20 20 20 20 int iAddr;
19ffc 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
19ffd 41 64 64 72 65 73 73 20 6f 66 20 6e 65 78 74 20 Address of next
19ffe 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 6f 20 72 instruction to r
19fff 65 74 75 72 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 eturn */. int i
1a000 53 75 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 Sub;
1a001 20 20 20 20 20 20 2f 2a 20 30 20 3d 20 6d 61 69 /* 0 = mai
1a002 6e 20 70 72 6f 67 72 61 6d 2c 20 31 20 3d 20 66 n program, 1 = f
1a003 69 72 73 74 20 73 75 62 2d 70 72 6f 67 72 61 6d irst sub-program
1a004 20 65 74 63 2e 20 2a 2f 0a 7d 3b 0a 73 74 61 74 etc. */.};.stat
1a005 69 63 20 4f 70 20 2a 6f 70 49 74 65 72 4e 65 78 ic Op *opIterNex
1a006 74 28 56 64 62 65 4f 70 49 74 65 72 20 2a 70 29 t(VdbeOpIter *p)
1a007 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 2d {. Vdbe *v = p-
1a008 3e 76 3b 0a 20 20 4f 70 20 2a 70 52 65 74 20 3d >v;. Op *pRet =
1a009 20 30 3b 0a 20 20 4f 70 20 2a 61 4f 70 3b 0a 20 0;. Op *aOp;.
1a00a 20 69 6e 74 20 6e 4f 70 3b 0a 0a 20 20 69 66 28 int nOp;.. if(
1a00b 20 70 2d 3e 69 53 75 62 3c 3d 70 2d 3e 6e 53 75 p->iSub<=p->nSu
1a00c 62 20 29 7b 0a 0a 20 20 20 20 69 66 28 20 70 2d b ){.. if( p-
1a00d 3e 69 53 75 62 3d 3d 30 20 29 7b 0a 20 20 20 20 >iSub==0 ){.
1a00e 20 20 61 4f 70 20 3d 20 76 2d 3e 61 4f 70 3b 0a aOp = v->aOp;.
1a00f 20 20 20 20 20 20 6e 4f 70 20 3d 20 76 2d 3e 6e nOp = v->n
1a010 4f 70 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 Op;. }else{.
1a011 20 20 20 20 20 61 4f 70 20 3d 20 70 2d 3e 61 70 aOp = p->ap
1a012 53 75 62 5b 70 2d 3e 69 53 75 62 2d 31 5d 2d 3e Sub[p->iSub-1]->
1a013 61 4f 70 3b 0a 20 20 20 20 20 20 6e 4f 70 20 3d aOp;. nOp =
1a014 20 70 2d 3e 61 70 53 75 62 5b 70 2d 3e 69 53 75 p->apSub[p->iSu
1a015 62 2d 31 5d 2d 3e 6e 4f 70 3b 0a 20 20 20 20 7d b-1]->nOp;. }
1a016 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e . assert( p->
1a017 69 41 64 64 72 3c 6e 4f 70 20 29 3b 0a 0a 20 20 iAddr<nOp );..
1a018 20 20 70 52 65 74 20 3d 20 26 61 4f 70 5b 70 2d pRet = &aOp[p-
1a019 3e 69 41 64 64 72 5d 3b 0a 20 20 20 20 70 2d 3e >iAddr];. p->
1a01a 69 41 64 64 72 2b 2b 3b 0a 20 20 20 20 69 66 28 iAddr++;. if(
1a01b 20 70 2d 3e 69 41 64 64 72 3d 3d 6e 4f 70 20 29 p->iAddr==nOp )
1a01c 7b 0a 20 20 20 20 20 20 70 2d 3e 69 53 75 62 2b {. p->iSub+
1a01d 2b 3b 0a 20 20 20 20 20 20 70 2d 3e 69 41 64 64 +;. p->iAdd
1a01e 72 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 0a r = 0;. }. .
1a01f 20 20 20 20 69 66 28 20 70 52 65 74 2d 3e 70 34 if( pRet->p4
1a020 74 79 70 65 3d 3d 50 34 5f 53 55 42 50 52 4f 47 type==P4_SUBPROG
1a021 52 41 4d 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 RAM ){. int
1a022 20 6e 42 79 74 65 20 3d 20 28 70 2d 3e 6e 53 75 nByte = (p->nSu
1a023 62 2b 31 29 2a 73 69 7a 65 6f 66 28 53 75 62 50 b+1)*sizeof(SubP
1a024 72 6f 67 72 61 6d 2a 29 3b 0a 20 20 20 20 20 20 rogram*);.
1a025 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 66 6f 72 int j;. for
1a026 28 6a 3d 30 3b 20 6a 3c 70 2d 3e 6e 53 75 62 3b (j=0; j<p->nSub;
1a027 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 j++){. i
1a028 66 28 20 70 2d 3e 61 70 53 75 62 5b 6a 5d 3d 3d f( p->apSub[j]==
1a029 70 52 65 74 2d 3e 70 34 2e 70 50 72 6f 67 72 61 pRet->p4.pProgra
1a02a 6d 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 m ) break;.
1a02b 20 7d 0a 20 20 20 20 20 20 69 66 28 20 6a 3d 3d }. if( j==
1a02c 70 2d 3e 6e 53 75 62 20 29 7b 0a 20 20 20 20 20 p->nSub ){.
1a02d 20 20 20 70 2d 3e 61 70 53 75 62 20 3d 20 73 71 p->apSub = sq
1a02e 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 4f 72 lite3DbReallocOr
1a02f 46 72 65 65 28 76 2d 3e 64 62 2c 20 70 2d 3e 61 Free(v->db, p->a
1a030 70 53 75 62 2c 20 6e 42 79 74 65 29 3b 0a 20 20 pSub, nByte);.
1a031 20 20 20 20 20 20 69 66 28 20 21 70 2d 3e 61 70 if( !p->ap
1a032 53 75 62 20 29 7b 0a 20 20 20 20 20 20 20 20 20 Sub ){.
1a033 20 70 52 65 74 20 3d 20 30 3b 0a 20 20 20 20 20 pRet = 0;.
1a034 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1a035 20 20 20 20 70 2d 3e 61 70 53 75 62 5b 70 2d 3e p->apSub[p->
1a036 6e 53 75 62 2b 2b 5d 20 3d 20 70 52 65 74 2d 3e nSub++] = pRet->
1a037 70 34 2e 70 50 72 6f 67 72 61 6d 3b 0a 20 20 20 p4.pProgram;.
1a038 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 }. }.
1a039 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 }. }.. retu
1a03a 72 6e 20 70 52 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a rn pRet;.}../*.*
1a03b 2a 20 43 68 65 63 6b 20 69 66 20 74 68 65 20 70 * Check if the p
1a03c 72 6f 67 72 61 6d 20 73 74 6f 72 65 64 20 69 6e rogram stored in
1a03d 20 74 68 65 20 56 4d 20 61 73 73 6f 63 69 61 74 the VM associat
1a03e 65 64 20 77 69 74 68 20 70 50 61 72 73 65 20 6d ed with pParse m
1a03f 61 79 0a 2a 2a 20 74 68 72 6f 77 20 61 6e 20 41 ay.** throw an A
1a040 42 4f 52 54 20 65 78 63 65 70 74 69 6f 6e 20 28 BORT exception (
1a041 63 61 75 73 69 6e 67 20 74 68 65 20 73 74 61 74 causing the stat
1a042 65 6d 65 6e 74 2c 20 62 75 74 20 6e 6f 74 20 65 ement, but not e
1a043 6e 74 69 72 65 20 74 72 61 6e 73 61 63 74 69 6f ntire transactio
1a044 6e 0a 2a 2a 20 74 6f 20 62 65 20 72 6f 6c 6c 65 n.** to be rolle
1a045 64 20 62 61 63 6b 29 2e 20 54 68 69 73 20 63 6f d back). This co
1a046 6e 64 69 74 69 6f 6e 20 69 73 20 74 72 75 65 20 ndition is true
1a047 69 66 20 74 68 65 20 6d 61 69 6e 20 70 72 6f 67 if the main prog
1a048 72 61 6d 20 6f 72 20 61 6e 79 0a 2a 2a 20 73 75 ram or any.** su
1a049 62 2d 70 72 6f 67 72 61 6d 73 20 63 6f 6e 74 61 b-programs conta
1a04a 69 6e 73 20 61 6e 79 20 6f 66 20 74 68 65 20 66 ins any of the f
1a04b 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 ollowing:.**.**
1a04c 20 20 2a 20 20 4f 50 5f 48 61 6c 74 20 77 69 74 * OP_Halt wit
1a04d 68 20 50 31 3d 53 51 4c 49 54 45 5f 43 4f 4e 53 h P1=SQLITE_CONS
1a04e 54 52 41 49 4e 54 20 61 6e 64 20 50 32 3d 4f 45 TRAINT and P2=OE
1a04f 5f 41 62 6f 72 74 2e 0a 2a 2a 20 20 20 2a 20 20 _Abort..** *
1a050 4f 50 5f 48 61 6c 74 49 66 4e 75 6c 6c 20 77 69 OP_HaltIfNull wi
1a051 74 68 20 50 31 3d 53 51 4c 49 54 45 5f 43 4f 4e th P1=SQLITE_CON
1a052 53 54 52 41 49 4e 54 20 61 6e 64 20 50 32 3d 4f STRAINT and P2=O
1a053 45 5f 41 62 6f 72 74 2e 0a 2a 2a 20 20 20 2a 20 E_Abort..** *
1a054 20 4f 50 5f 44 65 73 74 72 6f 79 0a 2a 2a 20 20 OP_Destroy.**
1a055 20 2a 20 20 4f 50 5f 56 55 70 64 61 74 65 0a 2a * OP_VUpdate.*
1a056 2a 20 20 20 2a 20 20 4f 50 5f 56 52 65 6e 61 6d * * OP_VRenam
1a057 65 0a 2a 2a 20 20 20 2a 20 20 4f 50 5f 46 6b 43 e.** * OP_FkC
1a058 6f 75 6e 74 65 72 20 77 69 74 68 20 50 32 3d 3d ounter with P2==
1a059 30 20 28 69 6d 6d 65 64 69 61 74 65 20 66 6f 72 0 (immediate for
1a05a 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 eign key constra
1a05b 69 6e 74 29 0a 2a 2a 0a 2a 2a 20 54 68 65 6e 20 int).**.** Then
1a05c 63 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 76 check that the v
1a05d 61 6c 75 65 20 6f 66 20 50 61 72 73 65 2e 6d 61 alue of Parse.ma
1a05e 79 41 62 6f 72 74 20 69 73 20 74 72 75 65 20 69 yAbort is true i
1a05f 66 20 61 6e 0a 2a 2a 20 41 42 4f 52 54 20 6d 61 f an.** ABORT ma
1a060 79 20 62 65 20 74 68 72 6f 77 6e 2c 20 6f 72 20 y be thrown, or
1a061 66 61 6c 73 65 20 6f 74 68 65 72 77 69 73 65 2e false otherwise.
1a062 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 Return true if
1a063 69 74 20 64 6f 65 73 0a 2a 2a 20 6d 61 74 63 68 it does.** match
1a064 2c 20 6f 72 20 66 61 6c 73 65 20 6f 74 68 65 72 , or false other
1a065 77 69 73 65 2e 20 54 68 69 73 20 66 75 6e 63 74 wise. This funct
1a066 69 6f 6e 20 69 73 20 69 6e 74 65 6e 64 65 64 20 ion is intended
1a067 74 6f 20 62 65 20 75 73 65 64 20 61 73 0a 2a 2a to be used as.**
1a068 20 70 61 72 74 20 6f 66 20 61 6e 20 61 73 73 65 part of an asse
1a069 72 74 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 20 rt statement in
1a06a 74 68 65 20 63 6f 6d 70 69 6c 65 72 2e 20 53 69 the compiler. Si
1a06b 6d 69 6c 61 72 20 74 6f 3a 0a 2a 2a 0a 2a 2a 20 milar to:.**.**
1a06c 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
1a06d 33 56 64 62 65 41 73 73 65 72 74 4d 61 79 41 62 3VdbeAssertMayAb
1a06e 6f 72 74 28 70 50 61 72 73 65 2d 3e 70 56 64 62 ort(pParse->pVdb
1a06f 65 2c 20 70 50 61 72 73 65 2d 3e 6d 61 79 41 62 e, pParse->mayAb
1a070 6f 72 74 29 20 29 3b 0a 2a 2f 0a 53 51 4c 49 54 ort) );.*/.SQLIT
1a071 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
1a072 6c 69 74 65 33 56 64 62 65 41 73 73 65 72 74 4d lite3VdbeAssertM
1a073 61 79 41 62 6f 72 74 28 56 64 62 65 20 2a 76 2c ayAbort(Vdbe *v,
1a074 20 69 6e 74 20 6d 61 79 41 62 6f 72 74 29 7b 0a int mayAbort){.
1a075 20 20 69 6e 74 20 68 61 73 41 62 6f 72 74 20 3d int hasAbort =
1a076 20 30 3b 0a 20 20 4f 70 20 2a 70 4f 70 3b 0a 20 0;. Op *pOp;.
1a077 20 56 64 62 65 4f 70 49 74 65 72 20 73 49 74 65 VdbeOpIter sIte
1a078 72 3b 0a 20 20 6d 65 6d 73 65 74 28 26 73 49 74 r;. memset(&sIt
1a079 65 72 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 49 er, 0, sizeof(sI
1a07a 74 65 72 29 29 3b 0a 20 20 73 49 74 65 72 2e 76 ter));. sIter.v
1a07b 20 3d 20 76 3b 0a 0a 20 20 77 68 69 6c 65 28 20 = v;.. while(
1a07c 28 70 4f 70 20 3d 20 6f 70 49 74 65 72 4e 65 78 (pOp = opIterNex
1a07d 74 28 26 73 49 74 65 72 29 29 21 3d 30 20 29 7b t(&sIter))!=0 ){
1a07e 0a 20 20 20 20 69 6e 74 20 6f 70 63 6f 64 65 20 . int opcode
1a07f 3d 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3b 0a 20 = pOp->opcode;.
1a080 20 20 20 69 66 28 20 6f 70 63 6f 64 65 3d 3d 4f if( opcode==O
1a081 50 5f 44 65 73 74 72 6f 79 20 7c 7c 20 6f 70 63 P_Destroy || opc
1a082 6f 64 65 3d 3d 4f 50 5f 56 55 70 64 61 74 65 20 ode==OP_VUpdate
1a083 7c 7c 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56 52 || opcode==OP_VR
1a084 65 6e 61 6d 65 20 0a 23 69 66 6e 64 65 66 20 53 ename .#ifndef S
1a085 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 QLITE_OMIT_FOREI
1a086 47 4e 5f 4b 45 59 0a 20 20 20 20 20 7c 7c 20 28 GN_KEY. || (
1a087 6f 70 63 6f 64 65 3d 3d 4f 50 5f 46 6b 43 6f 75 opcode==OP_FkCou
1a088 6e 74 65 72 20 26 26 20 70 4f 70 2d 3e 70 31 3d nter && pOp->p1=
1a089 3d 30 20 26 26 20 70 4f 70 2d 3e 70 32 3d 3d 31 =0 && pOp->p2==1
1a08a 29 20 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 7c ) .#endif. |
1a08b 7c 20 28 28 6f 70 63 6f 64 65 3d 3d 4f 50 5f 48 | ((opcode==OP_H
1a08c 61 6c 74 20 7c 7c 20 6f 70 63 6f 64 65 3d 3d 4f alt || opcode==O
1a08d 50 5f 48 61 6c 74 49 66 4e 75 6c 6c 29 20 0a 20 P_HaltIfNull) .
1a08e 20 20 20 20 20 26 26 20 28 70 4f 70 2d 3e 70 31 && (pOp->p1
1a08f 3d 3d 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 ==SQLITE_CONSTRA
1a090 49 4e 54 20 26 26 20 70 4f 70 2d 3e 70 32 3d 3d INT && pOp->p2==
1a091 4f 45 5f 41 62 6f 72 74 29 29 0a 20 20 20 20 29 OE_Abort)). )
1a092 7b 0a 20 20 20 20 20 20 68 61 73 41 62 6f 72 74 {. hasAbort
1a093 20 3d 20 31 3b 0a 20 20 20 20 20 20 62 72 65 61 = 1;. brea
1a094 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 k;. }. }. s
1a095 71 6c 69 74 65 33 44 62 46 72 65 65 28 76 2d 3e qlite3DbFree(v->
1a096 64 62 2c 20 73 49 74 65 72 2e 61 70 53 75 62 29 db, sIter.apSub)
1a097 3b 0a 0a 20 20 2f 2a 20 52 65 74 75 72 6e 20 74 ;.. /* Return t
1a098 72 75 65 20 69 66 20 68 61 73 41 62 6f 72 74 3d rue if hasAbort=
1a099 3d 6d 61 79 41 62 6f 72 74 2e 20 4f 72 20 69 66 =mayAbort. Or if
1a09a 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 a malloc failur
1a09b 65 20 6f 63 63 75 72 65 64 2e 0a 20 20 2a 2a 20 e occured.. **
1a09c 49 66 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 65 64 If malloc failed
1a09d 2c 20 74 68 65 6e 20 74 68 65 20 77 68 69 6c 65 , then the while
1a09e 28 29 20 6c 6f 6f 70 20 61 62 6f 76 65 20 6d 61 () loop above ma
1a09f 79 20 6e 6f 74 20 68 61 76 65 20 69 74 65 72 61 y not have itera
1a0a0 74 65 64 0a 20 20 2a 2a 20 74 68 72 6f 75 67 68 ted. ** through
1a0a1 20 61 6c 6c 20 6f 70 63 6f 64 65 73 20 61 6e 64 all opcodes and
1a0a2 20 68 61 73 41 62 6f 72 74 20 6d 61 79 20 62 65 hasAbort may be
1a0a3 20 73 65 74 20 69 6e 63 6f 72 72 65 63 74 6c 79 set incorrectly
1a0a4 2e 20 52 65 74 75 72 6e 0a 20 20 2a 2a 20 74 72 . Return. ** tr
1a0a5 75 65 20 66 6f 72 20 74 68 69 73 20 63 61 73 65 ue for this case
1a0a6 20 74 6f 20 70 72 65 76 65 6e 74 20 74 68 65 20 to prevent the
1a0a7 61 73 73 65 72 74 28 29 20 69 6e 20 74 68 65 20 assert() in the
1a0a8 63 61 6c 6c 65 72 73 20 66 72 61 6d 65 0a 20 20 callers frame.
1a0a9 2a 2a 20 66 72 6f 6d 20 66 61 69 6c 69 6e 67 2e ** from failing.
1a0aa 20 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 28 20 */. return (
1a0ab 76 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 v->db->mallocFai
1a0ac 6c 65 64 20 7c 7c 20 68 61 73 41 62 6f 72 74 3d led || hasAbort=
1a0ad 3d 6d 61 79 41 62 6f 72 74 20 29 3b 0a 7d 0a 23 =mayAbort );.}.#
1a0ae 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
1a0af 44 45 42 55 47 20 2d 20 74 68 65 20 73 71 6c 69 DEBUG - the sqli
1a0b0 74 65 33 41 73 73 65 72 74 4d 61 79 41 62 6f 72 te3AssertMayAbor
1a0b1 74 28 29 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a t() function */.
1a0b2 0a 2f 2a 0a 2a 2a 20 4c 6f 6f 70 20 74 68 72 6f ./*.** Loop thro
1a0b3 75 67 68 20 74 68 65 20 70 72 6f 67 72 61 6d 20 ugh the program
1a0b4 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 50 32 20 76 looking for P2 v
1a0b5 61 6c 75 65 73 20 74 68 61 74 20 61 72 65 20 6e alues that are n
1a0b6 65 67 61 74 69 76 65 0a 2a 2a 20 6f 6e 20 6a 75 egative.** on ju
1a0b7 6d 70 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 2e mp instructions.
1a0b8 20 20 45 61 63 68 20 73 75 63 68 20 76 61 6c 75 Each such valu
1a0b9 65 20 69 73 20 61 20 6c 61 62 65 6c 2e 20 20 52 e is a label. R
1a0ba 65 73 6f 6c 76 65 20 74 68 65 0a 2a 2a 20 6c 61 esolve the.** la
1a0bb 62 65 6c 20 62 79 20 73 65 74 74 69 6e 67 20 74 bel by setting t
1a0bc 68 65 20 50 32 20 76 61 6c 75 65 20 74 6f 20 69 he P2 value to i
1a0bd 74 73 20 63 6f 72 72 65 63 74 20 6e 6f 6e 2d 7a ts correct non-z
1a0be 65 72 6f 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a ero value..**.**
1a0bf 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 This routine is
1a0c0 20 63 61 6c 6c 65 64 20 6f 6e 63 65 20 61 66 74 called once aft
1a0c1 65 72 20 61 6c 6c 20 6f 70 63 6f 64 65 73 20 68 er all opcodes h
1a0c2 61 76 65 20 62 65 65 6e 20 69 6e 73 65 72 74 65 ave been inserte
1a0c3 64 2e 0a 2a 2a 0a 2a 2a 20 56 61 72 69 61 62 6c d..**.** Variabl
1a0c4 65 20 2a 70 4d 61 78 46 75 6e 63 41 72 67 73 20 e *pMaxFuncArgs
1a0c5 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 6d 61 is set to the ma
1a0c6 78 69 6d 75 6d 20 76 61 6c 75 65 20 6f 66 20 61 ximum value of a
1a0c7 6e 79 20 50 32 20 61 72 67 75 6d 65 6e 74 20 0a ny P2 argument .
1a0c8 2a 2a 20 74 6f 20 61 6e 20 4f 50 5f 46 75 6e 63 ** to an OP_Func
1a0c9 74 69 6f 6e 2c 20 4f 50 5f 41 67 67 53 74 65 70 tion, OP_AggStep
1a0ca 20 6f 72 20 4f 50 5f 56 46 69 6c 74 65 72 20 6f or OP_VFilter o
1a0cb 70 63 6f 64 65 2e 20 54 68 69 73 20 69 73 20 75 pcode. This is u
1a0cc 73 65 64 20 62 79 20 0a 2a 2a 20 73 71 6c 69 74 sed by .** sqlit
1a0cd 65 33 56 64 62 65 4d 61 6b 65 52 65 61 64 79 28 e3VdbeMakeReady(
1a0ce 29 20 74 6f 20 73 69 7a 65 20 74 68 65 20 56 64 ) to size the Vd
1a0cf 62 65 2e 61 70 41 72 67 5b 5d 20 61 72 72 61 79 be.apArg[] array
1a0d0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
1a0d1 20 72 65 73 6f 6c 76 65 50 32 56 61 6c 75 65 73 resolveP2Values
1a0d2 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 2a 70 (Vdbe *p, int *p
1a0d3 4d 61 78 46 75 6e 63 41 72 67 73 29 7b 0a 20 20 MaxFuncArgs){.
1a0d4 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e 4d 61 int i;. int nMa
1a0d5 78 41 72 67 73 20 3d 20 2a 70 4d 61 78 46 75 6e xArgs = *pMaxFun
1a0d6 63 41 72 67 73 3b 0a 20 20 4f 70 20 2a 70 4f 70 cArgs;. Op *pOp
1a0d7 3b 0a 20 20 69 6e 74 20 2a 61 4c 61 62 65 6c 20 ;. int *aLabel
1a0d8 3d 20 70 2d 3e 61 4c 61 62 65 6c 3b 0a 20 20 70 = p->aLabel;. p
1a0d9 2d 3e 72 65 61 64 4f 6e 6c 79 20 3d 20 31 3b 0a ->readOnly = 1;.
1a0da 20 20 66 6f 72 28 70 4f 70 3d 70 2d 3e 61 4f 70 for(pOp=p->aOp
1a0db 2c 20 69 3d 70 2d 3e 6e 4f 70 2d 31 3b 20 69 3e , i=p->nOp-1; i>
1a0dc 3d 30 3b 20 69 2d 2d 2c 20 70 4f 70 2b 2b 29 7b =0; i--, pOp++){
1a0dd 0a 20 20 20 20 75 38 20 6f 70 63 6f 64 65 20 3d . u8 opcode =
1a0de 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3b 0a 0a 20 pOp->opcode;..
1a0df 20 20 20 69 66 28 20 6f 70 63 6f 64 65 3d 3d 4f if( opcode==O
1a0e0 50 5f 46 75 6e 63 74 69 6f 6e 20 7c 7c 20 6f 70 P_Function || op
1a0e1 63 6f 64 65 3d 3d 4f 50 5f 41 67 67 53 74 65 70 code==OP_AggStep
1a0e2 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 4f ){. if( pO
1a0e3 70 2d 3e 70 35 3e 6e 4d 61 78 41 72 67 73 20 29 p->p5>nMaxArgs )
1a0e4 20 6e 4d 61 78 41 72 67 73 20 3d 20 70 4f 70 2d nMaxArgs = pOp-
1a0e5 3e 70 35 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c >p5;.#ifndef SQL
1a0e6 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c ITE_OMIT_VIRTUAL
1a0e7 54 41 42 4c 45 0a 20 20 20 20 7d 65 6c 73 65 20 TABLE. }else
1a0e8 69 66 28 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56 if( opcode==OP_V
1a0e9 55 70 64 61 74 65 20 29 7b 0a 20 20 20 20 20 20 Update ){.
1a0ea 69 66 28 20 70 4f 70 2d 3e 70 32 3e 6e 4d 61 78 if( pOp->p2>nMax
1a0eb 41 72 67 73 20 29 20 6e 4d 61 78 41 72 67 73 20 Args ) nMaxArgs
1a0ec 3d 20 70 4f 70 2d 3e 70 32 3b 0a 23 65 6e 64 69 = pOp->p2;.#endi
1a0ed 66 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 f. }else if(
1a0ee 6f 70 63 6f 64 65 3d 3d 4f 50 5f 54 72 61 6e 73 opcode==OP_Trans
1a0ef 61 63 74 69 6f 6e 20 26 26 20 70 4f 70 2d 3e 70 action && pOp->p
1a0f0 32 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 2d 2!=0 ){. p-
1a0f1 3e 72 65 61 64 4f 6e 6c 79 20 3d 20 30 3b 0a 23 >readOnly = 0;.#
1a0f2 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1a0f3 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a IT_VIRTUALTABLE.
1a0f4 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 }else if( op
1a0f5 63 6f 64 65 3d 3d 4f 50 5f 56 46 69 6c 74 65 72 code==OP_VFilter
1a0f6 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 3b ){. int n;
1a0f7 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
1a0f8 2d 3e 6e 4f 70 20 2d 20 69 20 3e 3d 20 33 20 29 ->nOp - i >= 3 )
1a0f9 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
1a0fa 70 4f 70 5b 2d 31 5d 2e 6f 70 63 6f 64 65 3d 3d pOp[-1].opcode==
1a0fb 4f 50 5f 49 6e 74 65 67 65 72 20 29 3b 0a 20 20 OP_Integer );.
1a0fc 20 20 20 20 6e 20 3d 20 70 4f 70 5b 2d 31 5d 2e n = pOp[-1].
1a0fd 70 31 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 3e p1;. if( n>
1a0fe 6e 4d 61 78 41 72 67 73 20 29 20 6e 4d 61 78 41 nMaxArgs ) nMaxA
1a0ff 72 67 73 20 3d 20 6e 3b 0a 23 65 6e 64 69 66 0a rgs = n;.#endif.
1a100 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 73 }.. if( s
1a101 71 6c 69 74 65 33 56 64 62 65 4f 70 63 6f 64 65 qlite3VdbeOpcode
1a102 48 61 73 50 72 6f 70 65 72 74 79 28 6f 70 63 6f HasProperty(opco
1a103 64 65 2c 20 4f 50 46 4c 47 5f 4a 55 4d 50 29 20 de, OPFLG_JUMP)
1a104 26 26 20 70 4f 70 2d 3e 70 32 3c 30 20 29 7b 0a && pOp->p2<0 ){.
1a105 20 20 20 20 20 20 61 73 73 65 72 74 28 20 2d 31 assert( -1
1a106 2d 70 4f 70 2d 3e 70 32 3c 70 2d 3e 6e 4c 61 62 -pOp->p2<p->nLab
1a107 65 6c 20 29 3b 0a 20 20 20 20 20 20 70 4f 70 2d el );. pOp-
1a108 3e 70 32 20 3d 20 61 4c 61 62 65 6c 5b 2d 31 2d >p2 = aLabel[-1-
1a109 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 7d 0a pOp->p2];. }.
1a10a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 62 46 }. sqlite3DbF
1a10b 72 65 65 28 70 2d 3e 64 62 2c 20 70 2d 3e 61 4c ree(p->db, p->aL
1a10c 61 62 65 6c 29 3b 0a 20 20 70 2d 3e 61 4c 61 62 abel);. p->aLab
1a10d 65 6c 20 3d 20 30 3b 0a 0a 20 20 2a 70 4d 61 78 el = 0;.. *pMax
1a10e 46 75 6e 63 41 72 67 73 20 3d 20 6e 4d 61 78 41 FuncArgs = nMaxA
1a10f 72 67 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 rgs;.}../*.** Re
1a110 74 75 72 6e 20 74 68 65 20 61 64 64 72 65 73 73 turn the address
1a111 20 6f 66 20 74 68 65 20 6e 65 78 74 20 69 6e 73 of the next ins
1a112 74 72 75 63 74 69 6f 6e 20 74 6f 20 62 65 20 69 truction to be i
1a113 6e 73 65 72 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 nserted..*/.SQLI
1a114 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
1a115 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e qlite3VdbeCurren
1a116 74 41 64 64 72 28 56 64 62 65 20 2a 70 29 7b 0a tAddr(Vdbe *p){.
1a117 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 assert( p->mag
1a118 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 ic==VDBE_MAGIC_I
1a119 4e 49 54 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 NIT );. return
1a11a 70 2d 3e 6e 4f 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a p->nOp;.}../*.**
1a11b 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 This function r
1a11c 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 eturns a pointer
1a11d 20 74 6f 20 74 68 65 20 61 72 72 61 79 20 6f 66 to the array of
1a11e 20 6f 70 63 6f 64 65 73 20 61 73 73 6f 63 69 61 opcodes associa
1a11f 74 65 64 20 77 69 74 68 0a 2a 2a 20 74 68 65 20 ted with.** the
1a120 56 64 62 65 20 70 61 73 73 65 64 20 61 73 20 74 Vdbe passed as t
1a121 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e he first argumen
1a122 74 2e 20 49 74 20 69 73 20 74 68 65 20 63 61 6c t. It is the cal
1a123 6c 65 72 73 20 72 65 73 70 6f 6e 73 69 62 69 6c lers responsibil
1a124 69 74 79 0a 2a 2a 20 74 6f 20 61 72 72 61 6e 67 ity.** to arrang
1a125 65 20 66 6f 72 20 74 68 65 20 72 65 74 75 72 6e e for the return
1a126 65 64 20 61 72 72 61 79 20 74 6f 20 62 65 20 65 ed array to be e
1a127 76 65 6e 74 75 61 6c 6c 79 20 66 72 65 65 64 20 ventually freed
1a128 75 73 69 6e 67 20 74 68 65 20 0a 2a 2a 20 76 64 using the .** vd
1a129 62 65 46 72 65 65 4f 70 41 72 72 61 79 28 29 20 beFreeOpArray()
1a12a 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 function..**.**
1a12b 42 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 Before returning
1a12c 2c 20 2a 70 6e 4f 70 20 69 73 20 73 65 74 20 74 , *pnOp is set t
1a12d 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 o the number of
1a12e 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20 72 entries in the r
1a12f 65 74 75 72 6e 65 64 0a 2a 2a 20 61 72 72 61 79 eturned.** array
1a130 2e 20 41 6c 73 6f 2c 20 2a 70 6e 4d 61 78 41 72 . Also, *pnMaxAr
1a131 67 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 g is set to the
1a132 6c 61 72 67 65 72 20 6f 66 20 69 74 73 20 63 75 larger of its cu
1a133 72 72 65 6e 74 20 76 61 6c 75 65 20 61 6e 64 20 rrent value and
1a134 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f .** the number o
1a135 66 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 f entries in the
1a136 20 56 64 62 65 2e 61 70 41 72 67 5b 5d 20 61 72 Vdbe.apArg[] ar
1a137 72 61 79 20 72 65 71 75 69 72 65 64 20 74 6f 20 ray required to
1a138 65 78 65 63 75 74 65 20 74 68 65 20 0a 2a 2a 20 execute the .**
1a139 72 65 74 75 72 6e 65 64 20 70 72 6f 67 72 61 6d returned program
1a13a 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1a13b 41 54 45 20 56 64 62 65 4f 70 20 2a 73 71 6c 69 ATE VdbeOp *sqli
1a13c 74 65 33 56 64 62 65 54 61 6b 65 4f 70 41 72 72 te3VdbeTakeOpArr
1a13d 61 79 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 ay(Vdbe *p, int
1a13e 2a 70 6e 4f 70 2c 20 69 6e 74 20 2a 70 6e 4d 61 *pnOp, int *pnMa
1a13f 78 41 72 67 29 7b 0a 20 20 56 64 62 65 4f 70 20 xArg){. VdbeOp
1a140 2a 61 4f 70 20 3d 20 70 2d 3e 61 4f 70 3b 0a 20 *aOp = p->aOp;.
1a141 20 61 73 73 65 72 74 28 20 61 4f 70 20 26 26 20 assert( aOp &&
1a142 21 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 !p->db->mallocFa
1a143 69 6c 65 64 20 29 3b 0a 0a 20 20 2f 2a 20 43 68 iled );.. /* Ch
1a144 65 63 6b 20 74 68 61 74 20 73 71 6c 69 74 65 33 eck that sqlite3
1a145 56 64 62 65 55 73 65 73 42 74 72 65 65 28 29 20 VdbeUsesBtree()
1a146 77 61 73 20 6e 6f 74 20 63 61 6c 6c 65 64 20 6f was not called o
1a147 6e 20 74 68 69 73 20 56 4d 20 2a 2f 0a 20 20 61 n this VM */. a
1a148 73 73 65 72 74 28 20 70 2d 3e 61 4d 75 74 65 78 ssert( p->aMutex
1a149 2e 6e 4d 75 74 65 78 3d 3d 30 20 29 3b 0a 0a 20 .nMutex==0 );..
1a14a 20 72 65 73 6f 6c 76 65 50 32 56 61 6c 75 65 73 resolveP2Values
1a14b 28 70 2c 20 70 6e 4d 61 78 41 72 67 29 3b 0a 20 (p, pnMaxArg);.
1a14c 20 2a 70 6e 4f 70 20 3d 20 70 2d 3e 6e 4f 70 3b *pnOp = p->nOp;
1a14d 0a 20 20 70 2d 3e 61 4f 70 20 3d 20 30 3b 0a 20 . p->aOp = 0;.
1a14e 20 72 65 74 75 72 6e 20 61 4f 70 3b 0a 7d 0a 0a return aOp;.}..
1a14f 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 77 68 6f 6c /*.** Add a whol
1a150 65 20 6c 69 73 74 20 6f 66 20 6f 70 65 72 61 74 e list of operat
1a151 69 6f 6e 73 20 74 6f 20 74 68 65 20 6f 70 65 72 ions to the oper
1a152 61 74 69 6f 6e 20 73 74 61 63 6b 2e 20 20 52 65 ation stack. Re
1a153 74 75 72 6e 20 74 68 65 0a 2a 2a 20 61 64 64 72 turn the.** addr
1a154 65 73 73 20 6f 66 20 74 68 65 20 66 69 72 73 74 ess of the first
1a155 20 6f 70 65 72 61 74 69 6f 6e 20 61 64 64 65 64 operation added
1a156 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1a157 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
1a158 64 62 65 41 64 64 4f 70 4c 69 73 74 28 56 64 62 dbeAddOpList(Vdb
1a159 65 20 2a 70 2c 20 69 6e 74 20 6e 4f 70 2c 20 56 e *p, int nOp, V
1a15a 64 62 65 4f 70 4c 69 73 74 20 63 6f 6e 73 74 20 dbeOpList const
1a15b 2a 61 4f 70 29 7b 0a 20 20 69 6e 74 20 61 64 64 *aOp){. int add
1a15c 72 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e r;. assert( p->
1a15d 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 magic==VDBE_MAGI
1a15e 43 5f 49 4e 49 54 20 29 3b 0a 20 20 69 66 28 20 C_INIT );. if(
1a15f 70 2d 3e 6e 4f 70 20 2b 20 6e 4f 70 20 3e 20 70 p->nOp + nOp > p
1a160 2d 3e 6e 4f 70 41 6c 6c 6f 63 20 26 26 20 67 72 ->nOpAlloc && gr
1a161 6f 77 4f 70 41 72 72 61 79 28 70 29 20 29 7b 0a owOpArray(p) ){.
1a162 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 return 0;.
1a163 7d 0a 20 20 61 64 64 72 20 3d 20 70 2d 3e 6e 4f }. addr = p->nO
1a164 70 3b 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 p;. if( ALWAYS(
1a165 6e 4f 70 3e 30 29 20 29 7b 0a 20 20 20 20 69 6e nOp>0) ){. in
1a166 74 20 69 3b 0a 20 20 20 20 56 64 62 65 4f 70 4c t i;. VdbeOpL
1a167 69 73 74 20 63 6f 6e 73 74 20 2a 70 49 6e 20 3d ist const *pIn =
1a168 20 61 4f 70 3b 0a 20 20 20 20 66 6f 72 28 69 3d aOp;. for(i=
1a169 30 3b 20 69 3c 6e 4f 70 3b 20 69 2b 2b 2c 20 70 0; i<nOp; i++, p
1a16a 49 6e 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74 In++){. int
1a16b 20 70 32 20 3d 20 70 49 6e 2d 3e 70 32 3b 0a 20 p2 = pIn->p2;.
1a16c 20 20 20 20 20 56 64 62 65 4f 70 20 2a 70 4f 75 VdbeOp *pOu
1a16d 74 20 3d 20 26 70 2d 3e 61 4f 70 5b 69 2b 61 64 t = &p->aOp[i+ad
1a16e 64 72 5d 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d dr];. pOut-
1a16f 3e 6f 70 63 6f 64 65 20 3d 20 70 49 6e 2d 3e 6f >opcode = pIn->o
1a170 70 63 6f 64 65 3b 0a 20 20 20 20 20 20 70 4f 75 pcode;. pOu
1a171 74 2d 3e 70 31 20 3d 20 70 49 6e 2d 3e 70 31 3b t->p1 = pIn->p1;
1a172 0a 20 20 20 20 20 20 69 66 28 20 70 32 3c 30 20 . if( p2<0
1a173 26 26 20 73 71 6c 69 74 65 33 56 64 62 65 4f 70 && sqlite3VdbeOp
1a174 63 6f 64 65 48 61 73 50 72 6f 70 65 72 74 79 28 codeHasProperty(
1a175 70 4f 75 74 2d 3e 6f 70 63 6f 64 65 2c 20 4f 50 pOut->opcode, OP
1a176 46 4c 47 5f 4a 55 4d 50 29 20 29 7b 0a 20 20 20 FLG_JUMP) ){.
1a177 20 20 20 20 20 70 4f 75 74 2d 3e 70 32 20 3d 20 pOut->p2 =
1a178 61 64 64 72 20 2b 20 41 44 44 52 28 70 32 29 3b addr + ADDR(p2);
1a179 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
1a17a 20 20 20 20 20 20 70 4f 75 74 2d 3e 70 32 20 3d pOut->p2 =
1a17b 20 70 32 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 p2;. }.
1a17c 20 20 20 70 4f 75 74 2d 3e 70 33 20 3d 20 70 49 pOut->p3 = pI
1a17d 6e 2d 3e 70 33 3b 0a 20 20 20 20 20 20 70 4f 75 n->p3;. pOu
1a17e 74 2d 3e 70 34 74 79 70 65 20 3d 20 50 34 5f 4e t->p4type = P4_N
1a17f 4f 54 55 53 45 44 3b 0a 20 20 20 20 20 20 70 4f OTUSED;. pO
1a180 75 74 2d 3e 70 34 2e 70 20 3d 20 30 3b 0a 20 20 ut->p4.p = 0;.
1a181 20 20 20 20 70 4f 75 74 2d 3e 70 35 20 3d 20 30 pOut->p5 = 0
1a182 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ;.#ifdef SQLITE_
1a183 44 45 42 55 47 0a 20 20 20 20 20 20 70 4f 75 74 DEBUG. pOut
1a184 2d 3e 7a 43 6f 6d 6d 65 6e 74 20 3d 20 30 3b 0a ->zComment = 0;.
1a185 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 if( sqlite
1a186 33 56 64 62 65 41 64 64 6f 70 54 72 61 63 65 20 3VdbeAddopTrace
1a187 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 ){. sqlit
1a188 65 33 56 64 62 65 50 72 69 6e 74 4f 70 28 30 2c e3VdbePrintOp(0,
1a189 20 69 2b 61 64 64 72 2c 20 26 70 2d 3e 61 4f 70 i+addr, &p->aOp
1a18a 5b 69 2b 61 64 64 72 5d 29 3b 0a 20 20 20 20 20 [i+addr]);.
1a18b 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a }.#endif. }.
1a18c 20 20 20 20 70 2d 3e 6e 4f 70 20 2b 3d 20 6e 4f p->nOp += nO
1a18d 70 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 p;. }. return
1a18e 61 64 64 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 addr;.}../*.** C
1a18f 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65 20 hange the value
1a190 6f 66 20 74 68 65 20 50 31 20 6f 70 65 72 61 6e of the P1 operan
1a191 64 20 66 6f 72 20 61 20 73 70 65 63 69 66 69 63 d for a specific
1a192 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a instruction..**
1a193 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 This routine is
1a194 20 75 73 65 66 75 6c 20 77 68 65 6e 20 61 20 6c useful when a l
1a195 61 72 67 65 20 70 72 6f 67 72 61 6d 20 69 73 20 arge program is
1a196 6c 6f 61 64 65 64 20 66 72 6f 6d 20 61 0a 2a 2a loaded from a.**
1a197 20 73 74 61 74 69 63 20 61 72 72 61 79 20 75 73 static array us
1a198 69 6e 67 20 73 71 6c 69 74 65 33 56 64 62 65 41 ing sqlite3VdbeA
1a199 64 64 4f 70 4c 69 73 74 20 62 75 74 20 77 65 20 ddOpList but we
1a19a 77 61 6e 74 20 74 6f 20 6d 61 6b 65 20 61 0a 2a want to make a.*
1a19b 2a 20 66 65 77 20 6d 69 6e 6f 72 20 63 68 61 6e * few minor chan
1a19c 67 65 73 20 74 6f 20 74 68 65 20 70 72 6f 67 72 ges to the progr
1a19d 61 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 am..*/.SQLITE_PR
1a19e 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
1a19f 65 33 56 64 62 65 43 68 61 6e 67 65 50 31 28 56 e3VdbeChangeP1(V
1a1a0 64 62 65 20 2a 70 2c 20 69 6e 74 20 61 64 64 72 dbe *p, int addr
1a1a1 2c 20 69 6e 74 20 76 61 6c 29 7b 0a 20 20 61 73 , int val){. as
1a1a2 73 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20 20 sert( p!=0 );.
1a1a3 61 73 73 65 72 74 28 20 61 64 64 72 3e 3d 30 20 assert( addr>=0
1a1a4 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 4f 70 3e );. if( p->nOp>
1a1a5 61 64 64 72 20 29 7b 0a 20 20 20 20 70 2d 3e 61 addr ){. p->a
1a1a6 4f 70 5b 61 64 64 72 5d 2e 70 31 20 3d 20 76 61 Op[addr].p1 = va
1a1a7 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 l;. }.}../*.**
1a1a8 43 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65 Change the value
1a1a9 20 6f 66 20 74 68 65 20 50 32 20 6f 70 65 72 61 of the P2 opera
1a1aa 6e 64 20 66 6f 72 20 61 20 73 70 65 63 69 66 69 nd for a specifi
1a1ab 63 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a c instruction..*
1a1ac 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 * This routine i
1a1ad 73 20 75 73 65 66 75 6c 20 66 6f 72 20 73 65 74 s useful for set
1a1ae 74 69 6e 67 20 61 20 6a 75 6d 70 20 64 65 73 74 ting a jump dest
1a1af 69 6e 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 ination..*/.SQLI
1a1b0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
1a1b1 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 sqlite3VdbeChang
1a1b2 65 50 32 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 eP2(Vdbe *p, int
1a1b3 20 61 64 64 72 2c 20 69 6e 74 20 76 61 6c 29 7b addr, int val){
1a1b4 0a 20 20 61 73 73 65 72 74 28 20 70 21 3d 30 20 . assert( p!=0
1a1b5 29 3b 0a 20 20 61 73 73 65 72 74 28 20 61 64 64 );. assert( add
1a1b6 72 3e 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 2d r>=0 );. if( p-
1a1b7 3e 6e 4f 70 3e 61 64 64 72 20 29 7b 0a 20 20 20 >nOp>addr ){.
1a1b8 20 70 2d 3e 61 4f 70 5b 61 64 64 72 5d 2e 70 32 p->aOp[addr].p2
1a1b9 20 3d 20 76 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f = val;. }.}../
1a1ba 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 *.** Change the
1a1bb 76 61 6c 75 65 20 6f 66 20 74 68 65 20 50 33 20 value of the P3
1a1bc 6f 70 65 72 61 6e 64 20 66 6f 72 20 61 20 73 70 operand for a sp
1a1bd 65 63 69 66 69 63 20 69 6e 73 74 72 75 63 74 69 ecific instructi
1a1be 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 on..*/.SQLITE_PR
1a1bf 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
1a1c0 65 33 56 64 62 65 43 68 61 6e 67 65 50 33 28 56 e3VdbeChangeP3(V
1a1c1 64 62 65 20 2a 70 2c 20 69 6e 74 20 61 64 64 72 dbe *p, int addr
1a1c2 2c 20 69 6e 74 20 76 61 6c 29 7b 0a 20 20 61 73 , int val){. as
1a1c3 73 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20 20 sert( p!=0 );.
1a1c4 61 73 73 65 72 74 28 20 61 64 64 72 3e 3d 30 20 assert( addr>=0
1a1c5 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 4f 70 3e );. if( p->nOp>
1a1c6 61 64 64 72 20 29 7b 0a 20 20 20 20 70 2d 3e 61 addr ){. p->a
1a1c7 4f 70 5b 61 64 64 72 5d 2e 70 33 20 3d 20 76 61 Op[addr].p3 = va
1a1c8 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 l;. }.}../*.**
1a1c9 43 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65 Change the value
1a1ca 20 6f 66 20 74 68 65 20 50 35 20 6f 70 65 72 61 of the P5 opera
1a1cb 6e 64 20 66 6f 72 20 74 68 65 20 6d 6f 73 74 20 nd for the most
1a1cc 72 65 63 65 6e 74 6c 79 0a 2a 2a 20 61 64 64 65 recently.** adde
1a1cd 64 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a 2f 0a d operation..*/.
1a1ce 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
1a1cf 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 oid sqlite3VdbeC
1a1d0 68 61 6e 67 65 50 35 28 56 64 62 65 20 2a 70 2c hangeP5(Vdbe *p,
1a1d1 20 75 38 20 76 61 6c 29 7b 0a 20 20 61 73 73 65 u8 val){. asse
1a1d2 72 74 28 20 70 21 3d 30 20 29 3b 0a 20 20 69 66 rt( p!=0 );. if
1a1d3 28 20 70 2d 3e 61 4f 70 20 29 7b 0a 20 20 20 20 ( p->aOp ){.
1a1d4 61 73 73 65 72 74 28 20 70 2d 3e 6e 4f 70 3e 30 assert( p->nOp>0
1a1d5 20 29 3b 0a 20 20 20 20 70 2d 3e 61 4f 70 5b 70 );. p->aOp[p
1a1d6 2d 3e 6e 4f 70 2d 31 5d 2e 70 35 20 3d 20 76 61 ->nOp-1].p5 = va
1a1d7 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 l;. }.}../*.**
1a1d8 43 68 61 6e 67 65 20 74 68 65 20 50 32 20 6f 70 Change the P2 op
1a1d9 65 72 61 6e 64 20 6f 66 20 69 6e 73 74 72 75 63 erand of instruc
1a1da 74 69 6f 6e 20 61 64 64 72 20 73 6f 20 74 68 61 tion addr so tha
1a1db 74 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 0a 2a t it points to.*
1a1dc 2a 20 74 68 65 20 61 64 64 72 65 73 73 20 6f 66 * the address of
1a1dd 20 74 68 65 20 6e 65 78 74 20 69 6e 73 74 72 75 the next instru
1a1de 63 74 69 6f 6e 20 74 6f 20 62 65 20 63 6f 64 65 ction to be code
1a1df 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 d..*/.SQLITE_PRI
1a1e0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
1a1e1 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 56 64 3VdbeJumpHere(Vd
1a1e2 62 65 20 2a 70 2c 20 69 6e 74 20 61 64 64 72 29 be *p, int addr)
1a1e3 7b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 {. sqlite3VdbeC
1a1e4 68 61 6e 67 65 50 32 28 70 2c 20 61 64 64 72 2c hangeP2(p, addr,
1a1e5 20 70 2d 3e 6e 4f 70 29 3b 0a 7d 0a 0a 0a 2f 2a p->nOp);.}.../*
1a1e6 0a 2a 2a 20 49 66 20 74 68 65 20 69 6e 70 75 74 .** If the input
1a1e7 20 46 75 6e 63 44 65 66 20 73 74 72 75 63 74 75 FuncDef structu
1a1e8 72 65 20 69 73 20 65 70 68 65 6d 65 72 61 6c 2c re is ephemeral,
1a1e9 20 74 68 65 6e 20 66 72 65 65 20 69 74 2e 20 20 then free it.
1a1ea 49 66 0a 2a 2a 20 74 68 65 20 46 75 6e 63 44 65 If.** the FuncDe
1a1eb 66 20 69 73 20 6e 6f 74 20 65 70 68 65 72 6d 61 f is not epherma
1a1ec 6c 2c 20 74 68 65 6e 20 64 6f 20 6e 6f 74 68 69 l, then do nothi
1a1ed 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ng..*/.static vo
1a1ee 69 64 20 66 72 65 65 45 70 68 65 6d 65 72 61 6c id freeEphemeral
1a1ef 46 75 6e 63 74 69 6f 6e 28 73 71 6c 69 74 65 33 Function(sqlite3
1a1f0 20 2a 64 62 2c 20 46 75 6e 63 44 65 66 20 2a 70 *db, FuncDef *p
1a1f1 44 65 66 29 7b 0a 20 20 69 66 28 20 41 4c 57 41 Def){. if( ALWA
1a1f2 59 53 28 70 44 65 66 29 20 26 26 20 28 70 44 65 YS(pDef) && (pDe
1a1f3 66 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 f->flags & SQLIT
1a1f4 45 5f 46 55 4e 43 5f 45 50 48 45 4d 29 21 3d 30 E_FUNC_EPHEM)!=0
1a1f5 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 ){. sqlite3D
1a1f6 62 46 72 65 65 28 64 62 2c 20 70 44 65 66 29 3b bFree(db, pDef);
1a1f7 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 . }.}../*.** De
1a1f8 6c 65 74 65 20 61 20 50 34 20 76 61 6c 75 65 20 lete a P4 value
1a1f9 69 66 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2f if necessary..*/
1a1fa 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 72 65 .static void fre
1a1fb 65 50 34 28 73 71 6c 69 74 65 33 20 2a 64 62 2c eP4(sqlite3 *db,
1a1fc 20 69 6e 74 20 70 34 74 79 70 65 2c 20 76 6f 69 int p4type, voi
1a1fd 64 20 2a 70 34 29 7b 0a 20 20 69 66 28 20 70 34 d *p4){. if( p4
1a1fe 20 29 7b 0a 20 20 20 20 73 77 69 74 63 68 28 20 ){. switch(
1a1ff 70 34 74 79 70 65 20 29 7b 0a 20 20 20 20 20 20 p4type ){.
1a200 63 61 73 65 20 50 34 5f 52 45 41 4c 3a 0a 20 20 case P4_REAL:.
1a201 20 20 20 20 63 61 73 65 20 50 34 5f 49 4e 54 36 case P4_INT6
1a202 34 3a 0a 20 20 20 20 20 20 63 61 73 65 20 50 34 4:. case P4
1a203 5f 4d 50 52 49 4e 54 46 3a 0a 20 20 20 20 20 20 _MPRINTF:.
1a204 63 61 73 65 20 50 34 5f 44 59 4e 41 4d 49 43 3a case P4_DYNAMIC:
1a205 0a 20 20 20 20 20 20 63 61 73 65 20 50 34 5f 4b . case P4_K
1a206 45 59 49 4e 46 4f 3a 0a 20 20 20 20 20 20 63 61 EYINFO:. ca
1a207 73 65 20 50 34 5f 49 4e 54 41 52 52 41 59 3a 0a se P4_INTARRAY:.
1a208 20 20 20 20 20 20 63 61 73 65 20 50 34 5f 4b 45 case P4_KE
1a209 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 3a 20 7b YINFO_HANDOFF: {
1a20a 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
1a20b 44 62 46 72 65 65 28 64 62 2c 20 70 34 29 3b 0a DbFree(db, p4);.
1a20c 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
1a20d 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73 }. cas
1a20e 65 20 50 34 5f 56 44 42 45 46 55 4e 43 3a 20 7b e P4_VDBEFUNC: {
1a20f 0a 20 20 20 20 20 20 20 20 56 64 62 65 46 75 6e . VdbeFun
1a210 63 20 2a 70 56 64 62 65 46 75 6e 63 20 3d 20 28 c *pVdbeFunc = (
1a211 56 64 62 65 46 75 6e 63 20 2a 29 70 34 3b 0a 20 VdbeFunc *)p4;.
1a212 20 20 20 20 20 20 20 66 72 65 65 45 70 68 65 6d freeEphem
1a213 65 72 61 6c 46 75 6e 63 74 69 6f 6e 28 64 62 2c eralFunction(db,
1a214 20 70 56 64 62 65 46 75 6e 63 2d 3e 70 46 75 6e pVdbeFunc->pFun
1a215 63 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 c);. sqli
1a216 74 65 33 56 64 62 65 44 65 6c 65 74 65 41 75 78 te3VdbeDeleteAux
1a217 44 61 74 61 28 70 56 64 62 65 46 75 6e 63 2c 20 Data(pVdbeFunc,
1a218 30 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 0);. sqli
1a219 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 56 te3DbFree(db, pV
1a21a 64 62 65 46 75 6e 63 29 3b 0a 20 20 20 20 20 20 dbeFunc);.
1a21b 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d break;. }
1a21c 0a 20 20 20 20 20 20 63 61 73 65 20 50 34 5f 46 . case P4_F
1a21d 55 4e 43 44 45 46 3a 20 7b 0a 20 20 20 20 20 20 UNCDEF: {.
1a21e 20 20 66 72 65 65 45 70 68 65 6d 65 72 61 6c 46 freeEphemeralF
1a21f 75 6e 63 74 69 6f 6e 28 64 62 2c 20 28 46 75 6e unction(db, (Fun
1a220 63 44 65 66 2a 29 70 34 29 3b 0a 20 20 20 20 20 cDef*)p4);.
1a221 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
1a222 7d 0a 20 20 20 20 20 20 63 61 73 65 20 50 34 5f }. case P4_
1a223 4d 45 4d 3a 20 7b 0a 20 20 20 20 20 20 20 20 73 MEM: {. s
1a224 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65 28 qlite3ValueFree(
1a225 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 (sqlite3_value*)
1a226 70 34 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 p4);. bre
1a227 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ak;. }.
1a228 20 20 63 61 73 65 20 50 34 5f 56 54 41 42 20 3a case P4_VTAB :
1a229 20 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 {. sqlit
1a22a 65 33 56 74 61 62 55 6e 6c 6f 63 6b 28 28 56 54 e3VtabUnlock((VT
1a22b 61 62 6c 65 20 2a 29 70 34 29 3b 0a 20 20 20 20 able *)p4);.
1a22c 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
1a22d 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 50 34 }. case P4
1a22e 5f 53 55 42 50 52 4f 47 52 41 4d 20 3a 20 7b 0a _SUBPROGRAM : {.
1a22f 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
1a230 64 62 65 50 72 6f 67 72 61 6d 44 65 6c 65 74 65 dbeProgramDelete
1a231 28 64 62 2c 20 28 53 75 62 50 72 6f 67 72 61 6d (db, (SubProgram
1a232 20 2a 29 70 34 2c 20 31 29 3b 0a 20 20 20 20 20 *)p4, 1);.
1a233 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
1a234 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f }. }. }.}../
1a235 2a 0a 2a 2a 20 46 72 65 65 20 74 68 65 20 73 70 *.** Free the sp
1a236 61 63 65 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f ace allocated fo
1a237 72 20 61 4f 70 20 61 6e 64 20 61 6e 79 20 70 34 r aOp and any p4
1a238 20 76 61 6c 75 65 73 20 61 6c 6c 6f 63 61 74 65 values allocate
1a239 64 20 66 6f 72 20 74 68 65 0a 2a 2a 20 6f 70 63 d for the.** opc
1a23a 6f 64 65 73 20 63 6f 6e 74 61 69 6e 65 64 20 77 odes contained w
1a23b 69 74 68 69 6e 2e 20 49 66 20 61 4f 70 20 69 73 ithin. If aOp is
1a23c 20 6e 6f 74 20 4e 55 4c 4c 20 69 74 20 69 73 20 not NULL it is
1a23d 61 73 73 75 6d 65 64 20 74 6f 20 63 6f 6e 74 61 assumed to conta
1a23e 69 6e 20 0a 2a 2a 20 6e 4f 70 20 65 6e 74 72 69 in .** nOp entri
1a23f 65 73 2e 20 0a 2a 2f 0a 73 74 61 74 69 63 20 76 es. .*/.static v
1a240 6f 69 64 20 76 64 62 65 46 72 65 65 4f 70 41 72 oid vdbeFreeOpAr
1a241 72 61 79 28 73 71 6c 69 74 65 33 20 2a 64 62 2c ray(sqlite3 *db,
1a242 20 4f 70 20 2a 61 4f 70 2c 20 69 6e 74 20 6e 4f Op *aOp, int nO
1a243 70 29 7b 0a 20 20 69 66 28 20 61 4f 70 20 29 7b p){. if( aOp ){
1a244 0a 20 20 20 20 4f 70 20 2a 70 4f 70 3b 0a 20 20 . Op *pOp;.
1a245 20 20 66 6f 72 28 70 4f 70 3d 61 4f 70 3b 20 70 for(pOp=aOp; p
1a246 4f 70 3c 26 61 4f 70 5b 6e 4f 70 5d 3b 20 70 4f Op<&aOp[nOp]; pO
1a247 70 2b 2b 29 7b 0a 20 20 20 20 20 20 66 72 65 65 p++){. free
1a248 50 34 28 64 62 2c 20 70 4f 70 2d 3e 70 34 74 79 P4(db, pOp->p4ty
1a249 70 65 2c 20 70 4f 70 2d 3e 70 34 2e 70 29 3b 0a pe, pOp->p4.p);.
1a24a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 #ifdef SQLITE_DE
1a24b 42 55 47 0a 20 20 20 20 20 20 73 71 6c 69 74 65 BUG. sqlite
1a24c 33 44 62 46 72 65 65 28 64 62 2c 20 70 4f 70 2d 3DbFree(db, pOp-
1a24d 3e 7a 43 6f 6d 6d 65 6e 74 29 3b 0a 23 65 6e 64 >zComment);.#end
1a24e 69 66 20 20 20 20 20 0a 20 20 20 20 7d 0a 20 20 if . }.
1a24f 7d 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 }. sqlite3DbFre
1a250 65 28 64 62 2c 20 61 4f 70 29 3b 0a 7d 0a 0a 2f e(db, aOp);.}../
1a251 2a 0a 2a 2a 20 44 65 63 72 65 6d 65 6e 74 20 74 *.** Decrement t
1a252 68 65 20 72 65 66 2d 63 6f 75 6e 74 20 6f 6e 20 he ref-count on
1a253 74 68 65 20 53 75 62 50 72 6f 67 72 61 6d 20 73 the SubProgram s
1a254 74 72 75 63 74 75 72 65 20 70 61 73 73 65 64 20 tructure passed
1a255 61 73 20 74 68 65 0a 2a 2a 20 73 65 63 6f 6e 64 as the.** second
1a256 20 61 72 67 75 6d 65 6e 74 2e 20 49 66 20 74 68 argument. If th
1a257 65 20 72 65 66 2d 63 6f 75 6e 74 20 72 65 61 63 e ref-count reac
1a258 68 65 73 20 7a 65 72 6f 2c 20 66 72 65 65 20 74 hes zero, free t
1a259 68 65 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a he structure..**
1a25a 0a 2a 2a 20 54 68 65 20 61 72 72 61 79 20 6f 66 .** The array of
1a25b 20 56 44 42 45 20 6f 70 63 6f 64 65 73 20 73 74 VDBE opcodes st
1a25c 6f 72 65 64 20 61 73 20 53 75 62 50 72 6f 67 72 ored as SubProgr
1a25d 61 6d 2e 61 4f 70 20 69 73 20 66 72 65 65 64 20 am.aOp is freed
1a25e 69 66 0a 2a 2a 20 65 69 74 68 65 72 20 74 68 65 if.** either the
1a25f 20 72 65 66 2d 63 6f 75 6e 74 20 72 65 61 63 68 ref-count reach
1a260 65 73 20 7a 65 72 6f 20 6f 72 20 70 61 72 61 6d es zero or param
1a261 65 74 65 72 20 66 72 65 65 6f 70 20 69 73 20 6e eter freeop is n
1a262 6f 6e 2d 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 53 on-zero..**.** S
1a263 69 6e 63 65 20 74 68 65 20 61 72 72 61 79 20 6f ince the array o
1a264 66 20 6f 70 63 6f 64 65 73 20 70 6f 69 6e 74 65 f opcodes pointe
1a265 64 20 74 6f 20 62 79 20 53 75 62 50 72 6f 67 72 d to by SubProgr
1a266 61 6d 2e 61 4f 70 20 6d 61 79 20 64 69 72 65 63 am.aOp may direc
1a267 74 6c 79 0a 2a 2a 20 6f 72 20 69 6e 64 69 72 65 tly.** or indire
1a268 63 74 6c 79 20 63 6f 6e 74 61 69 6e 20 61 20 72 ctly contain a r
1a269 65 66 65 72 65 6e 63 65 20 74 6f 20 74 68 65 20 eference to the
1a26a 53 75 62 50 72 6f 67 72 61 6d 20 73 74 72 75 63 SubProgram struc
1a26b 74 75 72 65 20 69 74 73 65 6c 66 2e 0a 2a 2a 20 ture itself..**
1a26c 42 79 20 70 61 73 73 69 6e 67 20 61 20 6e 6f 6e By passing a non
1a26d 2d 7a 65 72 6f 20 66 72 65 65 6f 70 20 70 61 72 -zero freeop par
1a26e 61 6d 65 74 65 72 2c 20 74 68 65 20 63 61 6c 6c ameter, the call
1a26f 65 72 20 6d 61 79 20 65 6e 73 75 72 65 20 74 68 er may ensure th
1a270 61 74 20 61 6c 6c 0a 2a 2a 20 53 75 62 50 72 6f at all.** SubPro
1a271 67 72 61 6d 20 73 74 72 75 63 74 75 72 65 73 20 gram structures
1a272 61 6e 64 20 74 68 65 69 72 20 61 4f 70 20 61 72 and their aOp ar
1a273 72 61 79 73 20 61 72 65 20 66 72 65 65 64 2c 20 rays are freed,
1a274 65 76 65 6e 20 77 68 65 6e 20 74 68 65 72 65 0a even when there.
1a275 2a 2a 20 61 72 65 20 73 75 63 68 20 63 69 72 63 ** are such circ
1a276 75 6c 61 72 20 72 65 66 65 72 65 6e 63 65 73 2e ular references.
1a277 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1a278 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
1a279 64 62 65 50 72 6f 67 72 61 6d 44 65 6c 65 74 65 dbeProgramDelete
1a27a 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 53 75 (sqlite3 *db, Su
1a27b 62 50 72 6f 67 72 61 6d 20 2a 70 2c 20 69 6e 74 bProgram *p, int
1a27c 20 66 72 65 65 6f 70 29 7b 0a 20 20 69 66 28 20 freeop){. if(
1a27d 70 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 p ){. assert(
1a27e 20 70 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 20 p->nRef>0 );.
1a27f 20 20 69 66 28 20 66 72 65 65 6f 70 20 7c 7c 20 if( freeop ||
1a280 70 2d 3e 6e 52 65 66 3d 3d 31 20 29 7b 0a 20 20 p->nRef==1 ){.
1a281 20 20 20 20 4f 70 20 2a 61 4f 70 20 3d 20 70 2d Op *aOp = p-
1a282 3e 61 4f 70 3b 0a 20 20 20 20 20 20 70 2d 3e 61 >aOp;. p->a
1a283 4f 70 20 3d 20 30 3b 0a 20 20 20 20 20 20 76 64 Op = 0;. vd
1a284 62 65 46 72 65 65 4f 70 41 72 72 61 79 28 64 62 beFreeOpArray(db
1a285 2c 20 61 4f 70 2c 20 70 2d 3e 6e 4f 70 29 3b 0a , aOp, p->nOp);.
1a286 20 20 20 20 20 20 70 2d 3e 6e 4f 70 20 3d 20 30 p->nOp = 0
1a287 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e 6e ;. }. p->n
1a288 52 65 66 2d 2d 3b 0a 20 20 20 20 69 66 28 20 70 Ref--;. if( p
1a289 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a 20 20 20 ->nRef==0 ){.
1a28a 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 sqlite3DbFree
1a28b 28 64 62 2c 20 70 29 3b 0a 20 20 20 20 7d 0a 20 (db, p);. }.
1a28c 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 }.}.../*.** Cha
1a28d 6e 67 65 20 4e 20 6f 70 63 6f 64 65 73 20 73 74 nge N opcodes st
1a28e 61 72 74 69 6e 67 20 61 74 20 61 64 64 72 20 74 arting at addr t
1a28f 6f 20 4e 6f 2d 6f 70 73 2e 0a 2a 2f 0a 53 51 4c o No-ops..*/.SQL
1a290 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
1a291 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e sqlite3VdbeChan
1a292 67 65 54 6f 4e 6f 6f 70 28 56 64 62 65 20 2a 70 geToNoop(Vdbe *p
1a293 2c 20 69 6e 74 20 61 64 64 72 2c 20 69 6e 74 20 , int addr, int
1a294 4e 29 7b 0a 20 20 69 66 28 20 70 2d 3e 61 4f 70 N){. if( p->aOp
1a295 20 29 7b 0a 20 20 20 20 56 64 62 65 4f 70 20 2a ){. VdbeOp *
1a296 70 4f 70 20 3d 20 26 70 2d 3e 61 4f 70 5b 61 64 pOp = &p->aOp[ad
1a297 64 72 5d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 dr];. sqlite3
1a298 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 *db = p->db;.
1a299 20 20 77 68 69 6c 65 28 20 4e 2d 2d 20 29 7b 0a while( N-- ){.
1a29a 20 20 20 20 20 20 66 72 65 65 50 34 28 64 62 2c freeP4(db,
1a29b 20 70 4f 70 2d 3e 70 34 74 79 70 65 2c 20 70 4f pOp->p4type, pO
1a29c 70 2d 3e 70 34 2e 70 29 3b 0a 20 20 20 20 20 20 p->p4.p);.
1a29d 6d 65 6d 73 65 74 28 70 4f 70 2c 20 30 2c 20 73 memset(pOp, 0, s
1a29e 69 7a 65 6f 66 28 70 4f 70 5b 30 5d 29 29 3b 0a izeof(pOp[0]));.
1a29f 20 20 20 20 20 20 70 4f 70 2d 3e 6f 70 63 6f 64 pOp->opcod
1a2a0 65 20 3d 20 4f 50 5f 4e 6f 6f 70 3b 0a 20 20 20 e = OP_Noop;.
1a2a1 20 20 20 70 4f 70 2b 2b 3b 0a 20 20 20 20 7d 0a pOp++;. }.
1a2a2 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 }.}../*.** Cha
1a2a3 6e 67 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 nge the value of
1a2a4 20 74 68 65 20 50 34 20 6f 70 65 72 61 6e 64 20 the P4 operand
1a2a5 66 6f 72 20 61 20 73 70 65 63 69 66 69 63 20 69 for a specific i
1a2a6 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 20 54 nstruction..** T
1a2a7 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 his routine is u
1a2a8 73 65 66 75 6c 20 77 68 65 6e 20 61 20 6c 61 72 seful when a lar
1a2a9 67 65 20 70 72 6f 67 72 61 6d 20 69 73 20 6c 6f ge program is lo
1a2aa 61 64 65 64 20 66 72 6f 6d 20 61 0a 2a 2a 20 73 aded from a.** s
1a2ab 74 61 74 69 63 20 61 72 72 61 79 20 75 73 69 6e tatic array usin
1a2ac 67 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 g sqlite3VdbeAdd
1a2ad 4f 70 4c 69 73 74 20 62 75 74 20 77 65 20 77 61 OpList but we wa
1a2ae 6e 74 20 74 6f 20 6d 61 6b 65 20 61 0a 2a 2a 20 nt to make a.**
1a2af 66 65 77 20 6d 69 6e 6f 72 20 63 68 61 6e 67 65 few minor change
1a2b0 73 20 74 6f 20 74 68 65 20 70 72 6f 67 72 61 6d s to the program
1a2b1 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 3e 3d 30 20 ..**.** If n>=0
1a2b2 74 68 65 6e 20 74 68 65 20 50 34 20 6f 70 65 72 then the P4 oper
1a2b3 61 6e 64 20 69 73 20 64 79 6e 61 6d 69 63 2c 20 and is dynamic,
1a2b4 6d 65 61 6e 69 6e 67 20 74 68 61 74 20 61 20 63 meaning that a c
1a2b5 6f 70 79 20 6f 66 0a 2a 2a 20 74 68 65 20 73 74 opy of.** the st
1a2b6 72 69 6e 67 20 69 73 20 6d 61 64 65 20 69 6e 74 ring is made int
1a2b7 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 o memory obtaine
1a2b8 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d d from sqlite3_m
1a2b9 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 20 41 20 76 61 alloc()..** A va
1a2ba 6c 75 65 20 6f 66 20 6e 3d 3d 30 20 6d 65 61 6e lue of n==0 mean
1a2bb 73 20 63 6f 70 79 20 62 79 74 65 73 20 6f 66 20 s copy bytes of
1a2bc 7a 50 34 20 75 70 20 74 6f 20 61 6e 64 20 69 6e zP4 up to and in
1a2bd 63 6c 75 64 69 6e 67 20 74 68 65 0a 2a 2a 20 66 cluding the.** f
1a2be 69 72 73 74 20 6e 75 6c 6c 20 62 79 74 65 2e 20 irst null byte.
1a2bf 20 49 66 20 6e 3e 30 20 74 68 65 6e 20 63 6f 70 If n>0 then cop
1a2c0 79 20 6e 2b 31 20 62 79 74 65 73 20 6f 66 20 7a y n+1 bytes of z
1a2c1 50 34 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 3d 3d P4..**.** If n==
1a2c2 50 34 5f 4b 45 59 49 4e 46 4f 20 69 74 20 6d 65 P4_KEYINFO it me
1a2c3 61 6e 73 20 74 68 61 74 20 7a 50 34 20 69 73 20 ans that zP4 is
1a2c4 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 4b a pointer to a K
1a2c5 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 eyInfo structure
1a2c6 2e 0a 2a 2a 20 41 20 63 6f 70 79 20 69 73 20 6d ..** A copy is m
1a2c7 61 64 65 20 6f 66 20 74 68 65 20 4b 65 79 49 6e ade of the KeyIn
1a2c8 66 6f 20 73 74 72 75 63 74 75 72 65 20 69 6e 74 fo structure int
1a2c9 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 o memory obtaine
1a2ca 64 20 66 72 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65 d from.** sqlite
1a2cb 33 5f 6d 61 6c 6c 6f 63 2c 20 74 6f 20 62 65 20 3_malloc, to be
1a2cc 66 72 65 65 64 20 77 68 65 6e 20 74 68 65 20 56 freed when the V
1a2cd 64 62 65 20 69 73 20 66 69 6e 61 6c 69 7a 65 64 dbe is finalized
1a2ce 2e 0a 2a 2a 20 6e 3d 3d 50 34 5f 4b 45 59 49 4e ..** n==P4_KEYIN
1a2cf 46 4f 5f 48 41 4e 44 4f 46 46 20 69 6e 64 69 63 FO_HANDOFF indic
1a2d0 61 74 65 73 20 74 68 61 74 20 7a 50 34 20 70 6f ates that zP4 po
1a2d1 69 6e 74 73 20 74 6f 20 61 20 4b 65 79 49 6e 66 ints to a KeyInf
1a2d2 6f 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 73 o structure.** s
1a2d3 74 6f 72 65 64 20 69 6e 20 6d 65 6d 6f 72 79 20 tored in memory
1a2d4 74 68 61 74 20 74 68 65 20 63 61 6c 6c 65 72 20 that the caller
1a2d5 68 61 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f has obtained fro
1a2d6 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 m sqlite3_malloc
1a2d7 2e 20 54 68 65 20 0a 2a 2a 20 63 61 6c 6c 65 72 . The .** caller
1a2d8 20 73 68 6f 75 6c 64 20 6e 6f 74 20 66 72 65 65 should not free
1a2d9 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c the allocation,
1a2da 20 69 74 20 77 69 6c 6c 20 62 65 20 66 72 65 65 it will be free
1a2db 64 20 77 68 65 6e 20 74 68 65 20 56 64 62 65 20 d when the Vdbe
1a2dc 69 73 0a 2a 2a 20 66 69 6e 61 6c 69 7a 65 64 2e is.** finalized.
1a2dd 0a 2a 2a 20 0a 2a 2a 20 4f 74 68 65 72 20 76 61 .** .** Other va
1a2de 6c 75 65 73 20 6f 66 20 6e 20 28 50 34 5f 53 54 lues of n (P4_ST
1a2df 41 54 49 43 2c 20 50 34 5f 43 4f 4c 4c 53 45 51 ATIC, P4_COLLSEQ
1a2e0 20 65 74 63 2e 29 20 69 6e 64 69 63 61 74 65 20 etc.) indicate
1a2e1 74 68 61 74 20 7a 50 34 20 70 6f 69 6e 74 73 0a that zP4 points.
1a2e2 2a 2a 20 74 6f 20 61 20 73 74 72 69 6e 67 20 6f ** to a string o
1a2e3 72 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74 r structure that
1a2e4 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 74 is guaranteed t
1a2e5 6f 20 65 78 69 73 74 20 66 6f 72 20 74 68 65 20 o exist for the
1a2e6 6c 69 66 65 74 69 6d 65 20 6f 66 0a 2a 2a 20 74 lifetime of.** t
1a2e7 68 65 20 56 64 62 65 2e 20 49 6e 20 74 68 65 73 he Vdbe. In thes
1a2e8 65 20 63 61 73 65 73 20 77 65 20 63 61 6e 20 6a e cases we can j
1a2e9 75 73 74 20 63 6f 70 79 20 74 68 65 20 70 6f 69 ust copy the poi
1a2ea 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 nter..**.** If a
1a2eb 64 64 72 3c 30 20 74 68 65 6e 20 63 68 61 6e 67 ddr<0 then chang
1a2ec 65 20 50 34 20 6f 6e 20 74 68 65 20 6d 6f 73 74 e P4 on the most
1a2ed 20 72 65 63 65 6e 74 6c 79 20 69 6e 73 65 72 74 recently insert
1a2ee 65 64 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a ed instruction..
1a2ef 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1a2f0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 E void sqlite3Vd
1a2f1 62 65 43 68 61 6e 67 65 50 34 28 56 64 62 65 20 beChangeP4(Vdbe
1a2f2 2a 70 2c 20 69 6e 74 20 61 64 64 72 2c 20 63 6f *p, int addr, co
1a2f3 6e 73 74 20 63 68 61 72 20 2a 7a 50 34 2c 20 69 nst char *zP4, i
1a2f4 6e 74 20 6e 29 7b 0a 20 20 4f 70 20 2a 70 4f 70 nt n){. Op *pOp
1a2f5 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b ;. sqlite3 *db;
1a2f6 0a 20 20 61 73 73 65 72 74 28 20 70 21 3d 30 20 . assert( p!=0
1a2f7 29 3b 0a 20 20 64 62 20 3d 20 70 2d 3e 64 62 3b );. db = p->db;
1a2f8 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 . assert( p->ma
1a2f9 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f gic==VDBE_MAGIC_
1a2fa 49 4e 49 54 20 29 3b 0a 20 20 69 66 28 20 70 2d INIT );. if( p-
1a2fb 3e 61 4f 70 3d 3d 30 20 7c 7c 20 64 62 2d 3e 6d >aOp==0 || db->m
1a2fc 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 allocFailed ){.
1a2fd 20 20 20 69 66 20 28 20 6e 21 3d 50 34 5f 4b 45 if ( n!=P4_KE
1a2fe 59 49 4e 46 4f 20 26 26 20 6e 21 3d 50 34 5f 56 YINFO && n!=P4_V
1a2ff 54 41 42 20 29 20 7b 0a 20 20 20 20 20 20 66 72 TAB ) {. fr
1a300 65 65 50 34 28 64 62 2c 20 6e 2c 20 28 76 6f 69 eeP4(db, n, (voi
1a301 64 2a 29 2a 28 63 68 61 72 2a 2a 29 26 7a 50 34 d*)*(char**)&zP4
1a302 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 );. }. ret
1a303 75 72 6e 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 urn;. }. asser
1a304 74 28 20 70 2d 3e 6e 4f 70 3e 30 20 29 3b 0a 20 t( p->nOp>0 );.
1a305 20 61 73 73 65 72 74 28 20 61 64 64 72 3c 70 2d assert( addr<p-
1a306 3e 6e 4f 70 20 29 3b 0a 20 20 69 66 28 20 61 64 >nOp );. if( ad
1a307 64 72 3c 30 20 29 7b 0a 20 20 20 20 61 64 64 72 dr<0 ){. addr
1a308 20 3d 20 70 2d 3e 6e 4f 70 20 2d 20 31 3b 0a 20 = p->nOp - 1;.
1a309 20 7d 0a 20 20 70 4f 70 20 3d 20 26 70 2d 3e 61 }. pOp = &p->a
1a30a 4f 70 5b 61 64 64 72 5d 3b 0a 20 20 66 72 65 65 Op[addr];. free
1a30b 50 34 28 64 62 2c 20 70 4f 70 2d 3e 70 34 74 79 P4(db, pOp->p4ty
1a30c 70 65 2c 20 70 4f 70 2d 3e 70 34 2e 70 29 3b 0a pe, pOp->p4.p);.
1a30d 20 20 70 4f 70 2d 3e 70 34 2e 70 20 3d 20 30 3b pOp->p4.p = 0;
1a30e 0a 20 20 69 66 28 20 6e 3d 3d 50 34 5f 49 4e 54 . if( n==P4_INT
1a30f 33 32 20 29 7b 0a 20 20 20 20 2f 2a 20 4e 6f 74 32 ){. /* Not
1a310 65 3a 20 74 68 69 73 20 63 61 73 74 20 69 73 20 e: this cast is
1a311 73 61 66 65 2c 20 62 65 63 61 75 73 65 20 74 68 safe, because th
1a312 65 20 6f 72 69 67 69 6e 20 64 61 74 61 20 70 6f e origin data po
1a313 69 6e 74 20 77 61 73 20 61 6e 20 69 6e 74 0a 20 int was an int.
1a314 20 20 20 2a 2a 20 74 68 61 74 20 77 61 73 20 63 ** that was c
1a315 61 73 74 20 74 6f 20 61 20 28 63 6f 6e 73 74 20 ast to a (const
1a316 63 68 61 72 20 2a 29 2e 20 2a 2f 0a 20 20 20 20 char *). */.
1a317 70 4f 70 2d 3e 70 34 2e 69 20 3d 20 53 51 4c 49 pOp->p4.i = SQLI
1a318 54 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 28 7a 50 TE_PTR_TO_INT(zP
1a319 34 29 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 74 4);. pOp->p4t
1a31a 79 70 65 20 3d 20 50 34 5f 49 4e 54 33 32 3b 0a ype = P4_INT32;.
1a31b 20 20 7d 65 6c 73 65 20 69 66 28 20 7a 50 34 3d }else if( zP4=
1a31c 3d 30 20 29 7b 0a 20 20 20 20 70 4f 70 2d 3e 70 =0 ){. pOp->p
1a31d 34 2e 70 20 3d 20 30 3b 0a 20 20 20 20 70 4f 70 4.p = 0;. pOp
1a31e 2d 3e 70 34 74 79 70 65 20 3d 20 50 34 5f 4e 4f ->p4type = P4_NO
1a31f 54 55 53 45 44 3b 0a 20 20 7d 65 6c 73 65 20 69 TUSED;. }else i
1a320 66 28 20 6e 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f f( n==P4_KEYINFO
1a321 20 29 7b 0a 20 20 20 20 4b 65 79 49 6e 66 6f 20 ){. KeyInfo
1a322 2a 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 69 *pKeyInfo;. i
1a323 6e 74 20 6e 46 69 65 6c 64 2c 20 6e 42 79 74 65 nt nField, nByte
1a324 3b 0a 0a 20 20 20 20 6e 46 69 65 6c 64 20 3d 20 ;.. nField =
1a325 28 28 4b 65 79 49 6e 66 6f 2a 29 7a 50 34 29 2d ((KeyInfo*)zP4)-
1a326 3e 6e 46 69 65 6c 64 3b 0a 20 20 20 20 6e 42 79 >nField;. nBy
1a327 74 65 20 3d 20 73 69 7a 65 6f 66 28 2a 70 4b 65 te = sizeof(*pKe
1a328 79 49 6e 66 6f 29 20 2b 20 28 6e 46 69 65 6c 64 yInfo) + (nField
1a329 2d 31 29 2a 73 69 7a 65 6f 66 28 70 4b 65 79 49 -1)*sizeof(pKeyI
1a32a 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 30 5d 29 20 2b nfo->aColl[0]) +
1a32b 20 6e 46 69 65 6c 64 3b 0a 20 20 20 20 70 4b 65 nField;. pKe
1a32c 79 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33 4d yInfo = sqlite3M
1a32d 61 6c 6c 6f 63 28 20 6e 42 79 74 65 20 29 3b 0a alloc( nByte );.
1a32e 20 20 20 20 70 4f 70 2d 3e 70 34 2e 70 4b 65 79 pOp->p4.pKey
1a32f 49 6e 66 6f 20 3d 20 70 4b 65 79 49 6e 66 6f 3b Info = pKeyInfo;
1a330 0a 20 20 20 20 69 66 28 20 70 4b 65 79 49 6e 66 . if( pKeyInf
1a331 6f 20 29 7b 0a 20 20 20 20 20 20 75 38 20 2a 61 o ){. u8 *a
1a332 53 6f 72 74 4f 72 64 65 72 3b 0a 20 20 20 20 20 SortOrder;.
1a333 20 6d 65 6d 63 70 79 28 70 4b 65 79 49 6e 66 6f memcpy(pKeyInfo
1a334 2c 20 7a 50 34 2c 20 6e 42 79 74 65 29 3b 0a 20 , zP4, nByte);.
1a335 20 20 20 20 20 61 53 6f 72 74 4f 72 64 65 72 20 aSortOrder
1a336 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 = pKeyInfo->aSor
1a337 74 4f 72 64 65 72 3b 0a 20 20 20 20 20 20 69 66 tOrder;. if
1a338 28 20 61 53 6f 72 74 4f 72 64 65 72 20 29 7b 0a ( aSortOrder ){.
1a339 20 20 20 20 20 20 20 20 70 4b 65 79 49 6e 66 6f pKeyInfo
1a33a 2d 3e 61 53 6f 72 74 4f 72 64 65 72 20 3d 20 28 ->aSortOrder = (
1a33b 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 29 26 unsigned char*)&
1a33c 70 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b pKeyInfo->aColl[
1a33d 6e 46 69 65 6c 64 5d 3b 0a 20 20 20 20 20 20 20 nField];.
1a33e 20 6d 65 6d 63 70 79 28 70 4b 65 79 49 6e 66 6f memcpy(pKeyInfo
1a33f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 2c 20 61 53 ->aSortOrder, aS
1a340 6f 72 74 4f 72 64 65 72 2c 20 6e 46 69 65 6c 64 ortOrder, nField
1a341 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
1a342 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 50 pOp->p4type = P
1a343 34 5f 4b 45 59 49 4e 46 4f 3b 0a 20 20 20 20 7d 4_KEYINFO;. }
1a344 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 2d 3e 64 else{. p->d
1a345 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
1a346 3d 20 31 3b 0a 20 20 20 20 20 20 70 4f 70 2d 3e = 1;. pOp->
1a347 70 34 74 79 70 65 20 3d 20 50 34 5f 4e 4f 54 55 p4type = P4_NOTU
1a348 53 45 44 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c SED;. }. }el
1a349 73 65 20 69 66 28 20 6e 3d 3d 50 34 5f 4b 45 59 se if( n==P4_KEY
1a34a 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 20 29 7b 0a INFO_HANDOFF ){.
1a34b 20 20 20 20 70 4f 70 2d 3e 70 34 2e 70 20 3d 20 pOp->p4.p =
1a34c 28 76 6f 69 64 2a 29 7a 50 34 3b 0a 20 20 20 20 (void*)zP4;.
1a34d 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 50 34 pOp->p4type = P4
1a34e 5f 4b 45 59 49 4e 46 4f 3b 0a 20 20 7d 65 6c 73 _KEYINFO;. }els
1a34f 65 20 69 66 28 20 6e 3d 3d 50 34 5f 56 54 41 42 e if( n==P4_VTAB
1a350 20 29 7b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 2e ){. pOp->p4.
1a351 70 20 3d 20 28 76 6f 69 64 2a 29 7a 50 34 3b 0a p = (void*)zP4;.
1a352 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 pOp->p4type
1a353 3d 20 50 34 5f 56 54 41 42 3b 0a 20 20 20 20 73 = P4_VTAB;. s
1a354 71 6c 69 74 65 33 56 74 61 62 4c 6f 63 6b 28 28 qlite3VtabLock((
1a355 56 54 61 62 6c 65 20 2a 29 7a 50 34 29 3b 0a 20 VTable *)zP4);.
1a356 20 20 20 61 73 73 65 72 74 28 20 28 28 56 54 61 assert( ((VTa
1a357 62 6c 65 20 2a 29 7a 50 34 29 2d 3e 64 62 3d 3d ble *)zP4)->db==
1a358 70 2d 3e 64 62 20 29 3b 0a 20 20 7d 65 6c 73 65 p->db );. }else
1a359 20 69 66 28 20 6e 3c 30 20 29 7b 0a 20 20 20 20 if( n<0 ){.
1a35a 70 4f 70 2d 3e 70 34 2e 70 20 3d 20 28 76 6f 69 pOp->p4.p = (voi
1a35b 64 2a 29 7a 50 34 3b 0a 20 20 20 20 70 4f 70 2d d*)zP4;. pOp-
1a35c 3e 70 34 74 79 70 65 20 3d 20 28 73 69 67 6e 65 >p4type = (signe
1a35d 64 20 63 68 61 72 29 6e 3b 0a 20 20 7d 65 6c 73 d char)n;. }els
1a35e 65 7b 0a 20 20 20 20 69 66 28 20 6e 3d 3d 30 20 e{. if( n==0
1a35f 29 20 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 ) n = sqlite3Str
1a360 6c 65 6e 33 30 28 7a 50 34 29 3b 0a 20 20 20 20 len30(zP4);.
1a361 70 4f 70 2d 3e 70 34 2e 7a 20 3d 20 73 71 6c 69 pOp->p4.z = sqli
1a362 74 65 33 44 62 53 74 72 4e 44 75 70 28 70 2d 3e te3DbStrNDup(p->
1a363 64 62 2c 20 7a 50 34 2c 20 6e 29 3b 0a 20 20 20 db, zP4, n);.
1a364 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 50 pOp->p4type = P
1a365 34 5f 44 59 4e 41 4d 49 43 3b 0a 20 20 7d 0a 7d 4_DYNAMIC;. }.}
1a366 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 ..#ifndef NDEBUG
1a367 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 ./*.** Change th
1a368 65 20 63 6f 6d 6d 65 6e 74 20 6f 6e 20 74 68 65 e comment on the
1a369 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 the most recent
1a36a 6c 79 20 63 6f 64 65 64 20 69 6e 73 74 72 75 63 ly coded instruc
1a36b 74 69 6f 6e 2e 20 20 4f 72 0a 2a 2a 20 69 6e 73 tion. Or.** ins
1a36c 65 72 74 20 61 20 4e 6f 2d 6f 70 20 61 6e 64 20 ert a No-op and
1a36d 61 64 64 20 74 68 65 20 63 6f 6d 6d 65 6e 74 20 add the comment
1a36e 74 6f 20 74 68 61 74 20 6e 65 77 20 69 6e 73 74 to that new inst
1a36f 72 75 63 74 69 6f 6e 2e 20 20 54 68 69 73 0a 2a ruction. This.*
1a370 2a 20 6d 61 6b 65 73 20 74 68 65 20 63 6f 64 65 * makes the code
1a371 20 65 61 73 69 65 72 20 74 6f 20 72 65 61 64 20 easier to read
1a372 64 75 72 69 6e 67 20 64 65 62 75 67 67 69 6e 67 during debugging
1a373 2e 20 20 4e 6f 6e 65 20 6f 66 20 74 68 69 73 20 . None of this
1a374 68 61 70 70 65 6e 73 0a 2a 2a 20 69 6e 20 61 20 happens.** in a
1a375 70 72 6f 64 75 63 74 69 6f 6e 20 62 75 69 6c 64 production build
1a376 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1a377 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
1a378 56 64 62 65 43 6f 6d 6d 65 6e 74 28 56 64 62 65 VdbeComment(Vdbe
1a379 20 2a 70 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 *p, const char
1a37a 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a *zFormat, ...){.
1a37b 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 va_list ap;.
1a37c 69 66 28 20 21 70 20 29 20 72 65 74 75 72 6e 3b if( !p ) return;
1a37d 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 4f . assert( p->nO
1a37e 70 3e 30 20 7c 7c 20 70 2d 3e 61 4f 70 3d 3d 30 p>0 || p->aOp==0
1a37f 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d );. assert( p-
1a380 3e 61 4f 70 3d 3d 30 20 7c 7c 20 70 2d 3e 61 4f >aOp==0 || p->aO
1a381 70 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e 7a 43 6f 6d p[p->nOp-1].zCom
1a382 6d 65 6e 74 3d 3d 30 20 7c 7c 20 70 2d 3e 64 62 ment==0 || p->db
1a383 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 ->mallocFailed )
1a384 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 4f 70 20 29 ;. if( p->nOp )
1a385 7b 0a 20 20 20 20 63 68 61 72 20 2a 2a 70 7a 20 {. char **pz
1a386 3d 20 26 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70 = &p->aOp[p->nOp
1a387 2d 31 5d 2e 7a 43 6f 6d 6d 65 6e 74 3b 0a 20 20 -1].zComment;.
1a388 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a va_start(ap, z
1a389 46 6f 72 6d 61 74 29 3b 0a 20 20 20 20 73 71 6c Format);. sql
1a38a 69 74 65 33 44 62 46 72 65 65 28 70 2d 3e 64 62 ite3DbFree(p->db
1a38b 2c 20 2a 70 7a 29 3b 0a 20 20 20 20 2a 70 7a 20 , *pz);. *pz
1a38c 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 = sqlite3VMPrint
1a38d 66 28 70 2d 3e 64 62 2c 20 7a 46 6f 72 6d 61 74 f(p->db, zFormat
1a38e 2c 20 61 70 29 3b 0a 20 20 20 20 76 61 5f 65 6e , ap);. va_en
1a38f 64 28 61 70 29 3b 0a 20 20 7d 0a 7d 0a 53 51 4c d(ap);. }.}.SQL
1a390 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
1a391 20 73 71 6c 69 74 65 33 56 64 62 65 4e 6f 6f 70 sqlite3VdbeNoop
1a392 43 6f 6d 6d 65 6e 74 28 56 64 62 65 20 2a 70 2c Comment(Vdbe *p,
1a393 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f const char *zFo
1a394 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 rmat, ...){. va
1a395 5f 6c 69 73 74 20 61 70 3b 0a 20 20 69 66 28 20 _list ap;. if(
1a396 21 70 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 73 !p ) return;. s
1a397 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 30 qlite3VdbeAddOp0
1a398 28 70 2c 20 4f 50 5f 4e 6f 6f 70 29 3b 0a 20 20 (p, OP_Noop);.
1a399 61 73 73 65 72 74 28 20 70 2d 3e 6e 4f 70 3e 30 assert( p->nOp>0
1a39a 20 7c 7c 20 70 2d 3e 61 4f 70 3d 3d 30 20 29 3b || p->aOp==0 );
1a39b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61 4f . assert( p->aO
1a39c 70 3d 3d 30 20 7c 7c 20 70 2d 3e 61 4f 70 5b 70 p==0 || p->aOp[p
1a39d 2d 3e 6e 4f 70 2d 31 5d 2e 7a 43 6f 6d 6d 65 6e ->nOp-1].zCommen
1a39e 74 3d 3d 30 20 7c 7c 20 70 2d 3e 64 62 2d 3e 6d t==0 || p->db->m
1a39f 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 allocFailed );.
1a3a0 20 69 66 28 20 70 2d 3e 6e 4f 70 20 29 7b 0a 20 if( p->nOp ){.
1a3a1 20 20 20 63 68 61 72 20 2a 2a 70 7a 20 3d 20 26 char **pz = &
1a3a2 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70 2d 31 5d p->aOp[p->nOp-1]
1a3a3 2e 7a 43 6f 6d 6d 65 6e 74 3b 0a 20 20 20 20 76 .zComment;. v
1a3a4 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 a_start(ap, zFor
1a3a5 6d 61 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 mat);. sqlite
1a3a6 33 44 62 46 72 65 65 28 70 2d 3e 64 62 2c 20 2a 3DbFree(p->db, *
1a3a7 70 7a 29 3b 0a 20 20 20 20 2a 70 7a 20 3d 20 73 pz);. *pz = s
1a3a8 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 70 qlite3VMPrintf(p
1a3a9 2d 3e 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 ->db, zFormat, a
1a3aa 70 29 3b 0a 20 20 20 20 76 61 5f 65 6e 64 28 61 p);. va_end(a
1a3ab 70 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 p);. }.}.#endif
1a3ac 20 20 2f 2a 20 4e 44 45 42 55 47 20 2a 2f 0a 0a /* NDEBUG */..
1a3ad 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 /*.** Return the
1a3ae 20 6f 70 63 6f 64 65 20 66 6f 72 20 61 20 67 69 opcode for a gi
1a3af 76 65 6e 20 61 64 64 72 65 73 73 2e 20 20 49 66 ven address. If
1a3b0 20 74 68 65 20 61 64 64 72 65 73 73 20 69 73 20 the address is
1a3b1 2d 31 2c 20 74 68 65 6e 0a 2a 2a 20 72 65 74 75 -1, then.** retu
1a3b2 72 6e 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 rn the most rece
1a3b3 6e 74 6c 79 20 69 6e 73 65 72 74 65 64 20 6f 70 ntly inserted op
1a3b4 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 code..**.** If a
1a3b5 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
1a3b6 6f 6e 20 65 72 72 6f 72 20 68 61 73 20 6f 63 63 on error has occ
1a3b7 75 72 72 65 64 20 70 72 69 6f 72 20 74 6f 20 74 urred prior to t
1a3b8 68 65 20 63 61 6c 6c 69 6e 67 20 6f 66 20 74 68 he calling of th
1a3b9 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 2c 20 74 is.** routine, t
1a3ba 68 65 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f hen a pointer to
1a3bb 20 61 20 64 75 6d 6d 79 20 56 64 62 65 4f 70 20 a dummy VdbeOp
1a3bc 77 69 6c 6c 20 62 65 20 72 65 74 75 72 6e 65 64 will be returned
1a3bd 2e 20 20 54 68 61 74 20 6f 70 63 6f 64 65 0a 2a . That opcode.*
1a3be 2a 20 69 73 20 72 65 61 64 61 62 6c 65 20 61 6e * is readable an
1a3bf 64 20 77 72 69 74 61 62 6c 65 2c 20 62 75 74 20 d writable, but
1a3c0 69 74 20 68 61 73 20 6e 6f 20 65 66 66 65 63 74 it has no effect
1a3c1 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20 6f 66 . The return of
1a3c2 20 61 20 64 75 6d 6d 79 0a 2a 2a 20 6f 70 63 6f a dummy.** opco
1a3c3 64 65 20 61 6c 6c 6f 77 73 20 74 68 65 20 63 61 de allows the ca
1a3c4 6c 6c 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 66 ll to continue f
1a3c5 75 6e 63 74 69 6f 6e 69 6e 67 20 61 66 74 65 72 unctioning after
1a3c6 20 61 20 4f 4f 4d 20 66 61 75 6c 74 20 77 69 74 a OOM fault wit
1a3c7 68 6f 75 74 0a 2a 2a 20 68 61 76 69 6e 67 20 74 hout.** having t
1a3c8 6f 20 63 68 65 63 6b 20 74 6f 20 73 65 65 20 69 o check to see i
1a3c9 66 20 74 68 65 20 72 65 74 75 72 6e 20 66 72 6f f the return fro
1a3ca 6d 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 m this routine i
1a3cb 73 20 61 20 76 61 6c 69 64 20 70 6f 69 6e 74 65 s a valid pointe
1a3cc 72 2e 0a 2a 2a 0a 2a 2a 20 41 62 6f 75 74 20 74 r..**.** About t
1a3cd 68 65 20 23 69 66 64 65 66 20 53 51 4c 49 54 45 he #ifdef SQLITE
1a3ce 5f 4f 4d 49 54 5f 54 52 41 43 45 3a 20 20 4e 6f _OMIT_TRACE: No
1a3cf 72 6d 61 6c 6c 79 2c 20 74 68 69 73 20 72 6f 75 rmally, this rou
1a3d0 74 69 6e 65 20 69 73 20 6e 65 76 65 72 20 63 61 tine is never ca
1a3d1 6c 6c 65 64 0a 2a 2a 20 75 6e 6c 65 73 73 20 70 lled.** unless p
1a3d2 2d 3e 6e 4f 70 3e 30 2e 20 20 54 68 69 73 20 69 ->nOp>0. This i
1a3d3 73 20 62 65 63 61 75 73 65 20 69 6e 20 74 68 65 s because in the
1a3d4 20 61 62 73 65 6e 73 65 20 6f 66 20 53 51 4c 49 absense of SQLI
1a3d5 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 2c 0a 2a TE_OMIT_TRACE,.*
1a3d6 2a 20 61 6e 20 4f 50 5f 54 72 61 63 65 20 69 6e * an OP_Trace in
1a3d7 73 74 72 75 63 74 69 6f 6e 20 69 73 20 61 6c 77 struction is alw
1a3d8 61 79 73 20 69 6e 73 65 72 74 65 64 20 62 79 20 ays inserted by
1a3d9 73 71 6c 69 74 65 33 56 64 62 65 47 65 74 28 29 sqlite3VdbeGet()
1a3da 20 61 73 20 73 6f 6f 6e 20 61 73 0a 2a 2a 20 61 as soon as.** a
1a3db 20 6e 65 77 20 56 44 42 45 20 69 73 20 63 72 65 new VDBE is cre
1a3dc 61 74 65 64 2e 20 20 53 6f 20 77 65 20 61 72 65 ated. So we are
1a3dd 20 66 72 65 65 20 74 6f 20 73 65 74 20 61 64 64 free to set add
1a3de 72 20 74 6f 20 70 2d 3e 6e 4f 70 2d 31 20 77 69 r to p->nOp-1 wi
1a3df 74 68 6f 75 74 0a 2a 2a 20 68 61 76 69 6e 67 20 thout.** having
1a3e0 74 6f 20 64 6f 75 62 6c 65 2d 63 68 65 63 6b 20 to double-check
1a3e1 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 to make sure tha
1a3e2 74 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 t the result is
1a3e3 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2e 20 42 75 non-negative. Bu
1a3e4 74 0a 2a 2a 20 69 66 20 53 51 4c 49 54 45 5f 4f t.** if SQLITE_O
1a3e5 4d 49 54 5f 54 52 41 43 45 20 69 73 20 64 65 66 MIT_TRACE is def
1a3e6 69 6e 65 64 2c 20 74 68 65 20 4f 50 5f 54 72 61 ined, the OP_Tra
1a3e7 63 65 20 69 73 20 6f 6d 69 74 74 65 64 20 61 6e ce is omitted an
1a3e8 64 20 77 65 20 64 6f 20 6e 65 65 64 20 74 6f 0a d we do need to.
1a3e9 2a 2a 20 63 68 65 63 6b 20 74 68 65 20 76 61 6c ** check the val
1a3ea 75 65 20 6f 66 20 70 2d 3e 6e 4f 70 2d 31 20 62 ue of p->nOp-1 b
1a3eb 65 66 6f 72 65 20 63 6f 6e 74 69 6e 75 69 6e 67 efore continuing
1a3ec 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1a3ed 41 54 45 20 56 64 62 65 4f 70 20 2a 73 71 6c 69 ATE VdbeOp *sqli
1a3ee 74 65 33 56 64 62 65 47 65 74 4f 70 28 56 64 62 te3VdbeGetOp(Vdb
1a3ef 65 20 2a 70 2c 20 69 6e 74 20 61 64 64 72 29 7b e *p, int addr){
1a3f0 0a 20 20 73 74 61 74 69 63 20 56 64 62 65 4f 70 . static VdbeOp
1a3f1 20 64 75 6d 6d 79 3b 0a 20 20 61 73 73 65 72 74 dummy;. assert
1a3f2 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 ( p->magic==VDBE
1a3f3 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 _MAGIC_INIT );.
1a3f4 20 69 66 28 20 61 64 64 72 3c 30 20 29 7b 0a 23 if( addr<0 ){.#
1a3f5 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 ifdef SQLITE_OMI
1a3f6 54 5f 54 52 41 43 45 0a 20 20 20 20 69 66 28 20 T_TRACE. if(
1a3f7 70 2d 3e 6e 4f 70 3d 3d 30 20 29 20 72 65 74 75 p->nOp==0 ) retu
1a3f8 72 6e 20 26 64 75 6d 6d 79 3b 0a 23 65 6e 64 69 rn &dummy;.#endi
1a3f9 66 0a 20 20 20 20 61 64 64 72 20 3d 20 70 2d 3e f. addr = p->
1a3fa 6e 4f 70 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 61 nOp - 1;. }. a
1a3fb 73 73 65 72 74 28 20 28 61 64 64 72 3e 3d 30 20 ssert( (addr>=0
1a3fc 26 26 20 61 64 64 72 3c 70 2d 3e 6e 4f 70 29 20 && addr<p->nOp)
1a3fd 7c 7c 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 || p->db->malloc
1a3fe 46 61 69 6c 65 64 20 29 3b 0a 20 20 69 66 28 20 Failed );. if(
1a3ff 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 p->db->mallocFai
1a400 6c 65 64 20 29 7b 0a 20 20 20 20 72 65 74 75 72 led ){. retur
1a401 6e 20 26 64 75 6d 6d 79 3b 0a 20 20 7d 65 6c 73 n &dummy;. }els
1a402 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 26 70 e{. return &p
1a403 2d 3e 61 4f 70 5b 61 64 64 72 5d 3b 0a 20 20 7d ->aOp[addr];. }
1a404 0a 7d 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 .}..#if !defined
1a405 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 (SQLITE_OMIT_EXP
1a406 4c 41 49 4e 29 20 7c 7c 20 21 64 65 66 69 6e 65 LAIN) || !define
1a407 64 28 4e 44 45 42 55 47 29 20 5c 0a 20 20 20 20 d(NDEBUG) \.
1a408 20 7c 7c 20 64 65 66 69 6e 65 64 28 56 44 42 45 || defined(VDBE
1a409 5f 50 52 4f 46 49 4c 45 29 20 7c 7c 20 64 65 66 _PROFILE) || def
1a40a 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 ined(SQLITE_DEBU
1a40b 47 29 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 G)./*.** Compute
1a40c 20 61 20 73 74 72 69 6e 67 20 74 68 61 74 20 64 a string that d
1a40d 65 73 63 72 69 62 65 73 20 74 68 65 20 50 34 20 escribes the P4
1a40e 70 61 72 61 6d 65 74 65 72 20 66 6f 72 20 61 6e parameter for an
1a40f 20 6f 70 63 6f 64 65 2e 0a 2a 2a 20 55 73 65 20 opcode..** Use
1a410 7a 54 65 6d 70 20 66 6f 72 20 61 6e 79 20 72 65 zTemp for any re
1a411 71 75 69 72 65 64 20 74 65 6d 70 6f 72 61 72 79 quired temporary
1a412 20 62 75 66 66 65 72 20 73 70 61 63 65 2e 0a 2a buffer space..*
1a413 2f 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a 64 /.static char *d
1a414 69 73 70 6c 61 79 50 34 28 4f 70 20 2a 70 4f 70 isplayP4(Op *pOp
1a415 2c 20 63 68 61 72 20 2a 7a 54 65 6d 70 2c 20 69 , char *zTemp, i
1a416 6e 74 20 6e 54 65 6d 70 29 7b 0a 20 20 63 68 61 nt nTemp){. cha
1a417 72 20 2a 7a 50 34 20 3d 20 7a 54 65 6d 70 3b 0a r *zP4 = zTemp;.
1a418 20 20 61 73 73 65 72 74 28 20 6e 54 65 6d 70 3e assert( nTemp>
1a419 3d 32 30 20 29 3b 0a 20 20 73 77 69 74 63 68 28 =20 );. switch(
1a41a 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 29 7b 0a pOp->p4type ){.
1a41b 20 20 20 20 63 61 73 65 20 50 34 5f 4b 45 59 49 case P4_KEYI
1a41c 4e 46 4f 5f 53 54 41 54 49 43 3a 0a 20 20 20 20 NFO_STATIC:.
1a41d 63 61 73 65 20 50 34 5f 4b 45 59 49 4e 46 4f 3a case P4_KEYINFO:
1a41e 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 2c 20 {. int i,
1a41f 6a 3b 0a 20 20 20 20 20 20 4b 65 79 49 6e 66 6f j;. KeyInfo
1a420 20 2a 70 4b 65 79 49 6e 66 6f 20 3d 20 70 4f 70 *pKeyInfo = pOp
1a421 2d 3e 70 34 2e 70 4b 65 79 49 6e 66 6f 3b 0a 20 ->p4.pKeyInfo;.
1a422 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 sqlite3_snp
1a423 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 rintf(nTemp, zTe
1a424 6d 70 2c 20 22 6b 65 79 69 6e 66 6f 28 25 64 22 mp, "keyinfo(%d"
1a425 2c 20 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 , pKeyInfo->nFie
1a426 6c 64 29 3b 0a 20 20 20 20 20 20 69 20 3d 20 73 ld);. i = s
1a427 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a qlite3Strlen30(z
1a428 54 65 6d 70 29 3b 0a 20 20 20 20 20 20 66 6f 72 Temp);. for
1a429 28 6a 3d 30 3b 20 6a 3c 70 4b 65 79 49 6e 66 6f (j=0; j<pKeyInfo
1a42a 2d 3e 6e 46 69 65 6c 64 3b 20 6a 2b 2b 29 7b 0a ->nField; j++){.
1a42b 20 20 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20 CollSeq
1a42c 2a 70 43 6f 6c 6c 20 3d 20 70 4b 65 79 49 6e 66 *pColl = pKeyInf
1a42d 6f 2d 3e 61 43 6f 6c 6c 5b 6a 5d 3b 0a 20 20 20 o->aColl[j];.
1a42e 20 20 20 20 20 69 66 28 20 70 43 6f 6c 6c 20 29 if( pColl )
1a42f 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 {. int
1a430 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 n = sqlite3Strle
1a431 6e 33 30 28 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 n30(pColl->zName
1a432 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 );. if(
1a433 20 69 2b 6e 3e 6e 54 65 6d 70 2d 36 20 29 7b 0a i+n>nTemp-6 ){.
1a434 20 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 memc
1a435 70 79 28 26 7a 54 65 6d 70 5b 69 5d 2c 22 2c 2e py(&zTemp[i],",.
1a436 2e 2e 22 2c 34 29 3b 0a 20 20 20 20 20 20 20 20 ..",4);.
1a437 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
1a438 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 }.
1a439 20 7a 54 65 6d 70 5b 69 2b 2b 5d 20 3d 20 27 2c zTemp[i++] = ',
1a43a 27 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 ';. if(
1a43b 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 74 pKeyInfo->aSort
1a43c 4f 72 64 65 72 20 26 26 20 70 4b 65 79 49 6e 66 Order && pKeyInf
1a43d 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 6a 5d o->aSortOrder[j]
1a43e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
1a43f 7a 54 65 6d 70 5b 69 2b 2b 5d 20 3d 20 27 2d 27 zTemp[i++] = '-'
1a440 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
1a441 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 memcpy(&
1a442 7a 54 65 6d 70 5b 69 5d 2c 20 70 43 6f 6c 6c 2d zTemp[i], pColl-
1a443 3e 7a 4e 61 6d 65 2c 6e 2b 31 29 3b 0a 20 20 20 >zName,n+1);.
1a444 20 20 20 20 20 20 20 69 20 2b 3d 20 6e 3b 0a 20 i += n;.
1a445 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 }else if(
1a446 20 69 2b 34 3c 6e 54 65 6d 70 2d 36 20 29 7b 0a i+4<nTemp-6 ){.
1a447 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 memcpy
1a448 28 26 7a 54 65 6d 70 5b 69 5d 2c 22 2c 6e 69 6c (&zTemp[i],",nil
1a449 22 2c 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 ",4);.
1a44a 69 20 2b 3d 20 34 3b 0a 20 20 20 20 20 20 20 20 i += 4;.
1a44b 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }. }.
1a44c 7a 54 65 6d 70 5b 69 2b 2b 5d 20 3d 20 27 29 27 zTemp[i++] = ')'
1a44d 3b 0a 20 20 20 20 20 20 7a 54 65 6d 70 5b 69 5d ;. zTemp[i]
1a44e 20 3d 20 30 3b 0a 20 20 20 20 20 20 61 73 73 65 = 0;. asse
1a44f 72 74 28 20 69 3c 6e 54 65 6d 70 20 29 3b 0a 20 rt( i<nTemp );.
1a450 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
1a451 7d 0a 20 20 20 20 63 61 73 65 20 50 34 5f 43 4f }. case P4_CO
1a452 4c 4c 53 45 51 3a 20 7b 0a 20 20 20 20 20 20 43 LLSEQ: {. C
1a453 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 ollSeq *pColl =
1a454 70 4f 70 2d 3e 70 34 2e 70 43 6f 6c 6c 3b 0a 20 pOp->p4.pColl;.
1a455 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 sqlite3_snp
1a456 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 rintf(nTemp, zTe
1a457 6d 70 2c 20 22 63 6f 6c 6c 73 65 71 28 25 2e 32 mp, "collseq(%.2
1a458 30 73 29 22 2c 20 70 43 6f 6c 6c 2d 3e 7a 4e 61 0s)", pColl->zNa
1a459 6d 65 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b me);. break
1a45a 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 ;. }. case
1a45b 20 50 34 5f 46 55 4e 43 44 45 46 3a 20 7b 0a 20 P4_FUNCDEF: {.
1a45c 20 20 20 20 20 46 75 6e 63 44 65 66 20 2a 70 44 FuncDef *pD
1a45d 65 66 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 46 75 ef = pOp->p4.pFu
1a45e 6e 63 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 nc;. sqlite
1a45f 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 3_snprintf(nTemp
1a460 2c 20 7a 54 65 6d 70 2c 20 22 25 73 28 25 64 29 , zTemp, "%s(%d)
1a461 22 2c 20 70 44 65 66 2d 3e 7a 4e 61 6d 65 2c 20 ", pDef->zName,
1a462 70 44 65 66 2d 3e 6e 41 72 67 29 3b 0a 20 20 20 pDef->nArg);.
1a463 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
1a464 20 20 20 20 63 61 73 65 20 50 34 5f 49 4e 54 36 case P4_INT6
1a465 34 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 4: {. sqlit
1a466 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d e3_snprintf(nTem
1a467 70 2c 20 7a 54 65 6d 70 2c 20 22 25 6c 6c 64 22 p, zTemp, "%lld"
1a468 2c 20 2a 70 4f 70 2d 3e 70 34 2e 70 49 36 34 29 , *pOp->p4.pI64)
1a469 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
1a46a 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 50 34 }. case P4
1a46b 5f 49 4e 54 33 32 3a 20 7b 0a 20 20 20 20 20 20 _INT32: {.
1a46c 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
1a46d 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 (nTemp, zTemp, "
1a46e 25 64 22 2c 20 70 4f 70 2d 3e 70 34 2e 69 29 3b %d", pOp->p4.i);
1a46f 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
1a470 20 20 7d 0a 20 20 20 20 63 61 73 65 20 50 34 5f }. case P4_
1a471 52 45 41 4c 3a 20 7b 0a 20 20 20 20 20 20 73 71 REAL: {. sq
1a472 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e lite3_snprintf(n
1a473 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 25 2e Temp, zTemp, "%.
1a474 31 36 67 22 2c 20 2a 70 4f 70 2d 3e 70 34 2e 70 16g", *pOp->p4.p
1a475 52 65 61 6c 29 3b 0a 20 20 20 20 20 20 62 72 65 Real);. bre
1a476 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 ak;. }. ca
1a477 73 65 20 50 34 5f 4d 45 4d 3a 20 7b 0a 20 20 20 se P4_MEM: {.
1a478 20 20 20 4d 65 6d 20 2a 70 4d 65 6d 20 3d 20 70 Mem *pMem = p
1a479 4f 70 2d 3e 70 34 2e 70 4d 65 6d 3b 0a 20 20 20 Op->p4.pMem;.
1a47a 20 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d assert( (pMem
1a47b 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 ->flags & MEM_Nu
1a47c 6c 6c 29 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 ll)==0 );.
1a47d 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 if( pMem->flags
1a47e 26 20 4d 45 4d 5f 53 74 72 20 29 7b 0a 20 20 20 & MEM_Str ){.
1a47f 20 20 20 20 20 7a 50 34 20 3d 20 70 4d 65 6d 2d zP4 = pMem-
1a480 3e 7a 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 >z;. }else
1a481 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 if( pMem->flags
1a482 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 & MEM_Int ){.
1a483 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 sqlite3_snp
1a484 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 rintf(nTemp, zTe
1a485 6d 70 2c 20 22 25 6c 6c 64 22 2c 20 70 4d 65 6d mp, "%lld", pMem
1a486 2d 3e 75 2e 69 29 3b 0a 20 20 20 20 20 20 7d 65 ->u.i);. }e
1a487 6c 73 65 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c lse if( pMem->fl
1a488 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29 ags & MEM_Real )
1a489 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
1a48a 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 3_snprintf(nTemp
1a48b 2c 20 7a 54 65 6d 70 2c 20 22 25 2e 31 36 67 22 , zTemp, "%.16g"
1a48c 2c 20 70 4d 65 6d 2d 3e 72 29 3b 0a 20 20 20 20 , pMem->r);.
1a48d 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
1a48e 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 66 assert( pMem->f
1a48f 6c 61 67 73 20 26 20 4d 45 4d 5f 42 6c 6f 62 20 lags & MEM_Blob
1a490 29 3b 0a 20 20 20 20 20 20 20 20 7a 50 34 20 3d );. zP4 =
1a491 20 22 28 62 6c 6f 62 29 22 3b 0a 20 20 20 20 20 "(blob)";.
1a492 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a }. break;.
1a493 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 }.#ifndef SQ
1a494 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 LITE_OMIT_VIRTUA
1a495 4c 54 41 42 4c 45 0a 20 20 20 20 63 61 73 65 20 LTABLE. case
1a496 50 34 5f 56 54 41 42 3a 20 7b 0a 20 20 20 20 20 P4_VTAB: {.
1a497 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 sqlite3_vtab *p
1a498 56 74 61 62 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 Vtab = pOp->p4.p
1a499 56 74 61 62 2d 3e 70 56 74 61 62 3b 0a 20 20 20 Vtab->pVtab;.
1a49a 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 sqlite3_snpri
1a49b 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 ntf(nTemp, zTemp
1a49c 2c 20 22 76 74 61 62 3a 25 70 3a 25 70 22 2c 20 , "vtab:%p:%p",
1a49d 70 56 74 61 62 2c 20 70 56 74 61 62 2d 3e 70 4d pVtab, pVtab->pM
1a49e 6f 64 75 6c 65 29 3b 0a 20 20 20 20 20 20 62 72 odule);. br
1a49f 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 eak;. }.#endi
1a4a0 66 0a 20 20 20 20 63 61 73 65 20 50 34 5f 49 4e f. case P4_IN
1a4a1 54 41 52 52 41 59 3a 20 7b 0a 20 20 20 20 20 20 TARRAY: {.
1a4a2 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
1a4a3 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 (nTemp, zTemp, "
1a4a4 69 6e 74 61 72 72 61 79 22 29 3b 0a 20 20 20 20 intarray");.
1a4a5 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
1a4a6 20 20 20 63 61 73 65 20 50 34 5f 53 55 42 50 52 case P4_SUBPR
1a4a7 4f 47 52 41 4d 3a 20 7b 0a 20 20 20 20 20 20 73 OGRAM: {. s
1a4a8 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
1a4a9 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 70 nTemp, zTemp, "p
1a4aa 72 6f 67 72 61 6d 22 29 3b 0a 20 20 20 20 20 20 rogram");.
1a4ab 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 break;. }.
1a4ac 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 default: {.
1a4ad 20 20 7a 50 34 20 3d 20 70 4f 70 2d 3e 70 34 2e zP4 = pOp->p4.
1a4ae 7a 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 50 34 z;. if( zP4
1a4af 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 7a ==0 ){. z
1a4b0 50 34 20 3d 20 7a 54 65 6d 70 3b 0a 20 20 20 20 P4 = zTemp;.
1a4b1 20 20 20 20 7a 54 65 6d 70 5b 30 5d 20 3d 20 30 zTemp[0] = 0
1a4b2 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
1a4b3 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 7a 50 }. assert( zP
1a4b4 34 21 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 4!=0 );. return
1a4b5 20 7a 50 34 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a zP4;.}.#endif..
1a4b6 2f 2a 0a 2a 2a 20 44 65 63 6c 61 72 65 20 74 6f /*.** Declare to
1a4b7 20 74 68 65 20 56 64 62 65 20 74 68 61 74 20 74 the Vdbe that t
1a4b8 68 65 20 42 54 72 65 65 20 6f 62 6a 65 63 74 20 he BTree object
1a4b9 61 74 20 64 62 2d 3e 61 44 62 5b 69 5d 20 69 73 at db->aDb[i] is
1a4ba 20 75 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 used..*/.SQLITE
1a4bb 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
1a4bc 6c 69 74 65 33 56 64 62 65 55 73 65 73 42 74 72 lite3VdbeUsesBtr
1a4bd 65 65 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 ee(Vdbe *p, int
1a4be 69 29 7b 0a 20 20 69 6e 74 20 6d 61 73 6b 3b 0a i){. int mask;.
1a4bf 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 assert( i>=0 &
1a4c0 26 20 69 3c 70 2d 3e 64 62 2d 3e 6e 44 62 20 26 & i<p->db->nDb &
1a4c1 26 20 69 3c 73 69 7a 65 6f 66 28 75 33 32 29 2a & i<sizeof(u32)*
1a4c2 38 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 8 );. assert( i
1a4c3 3c 28 69 6e 74 29 73 69 7a 65 6f 66 28 70 2d 3e <(int)sizeof(p->
1a4c4 62 74 72 65 65 4d 61 73 6b 29 2a 38 20 29 3b 0a btreeMask)*8 );.
1a4c5 20 20 6d 61 73 6b 20 3d 20 28 28 75 33 32 29 31 mask = ((u32)1
1a4c6 29 3c 3c 69 3b 0a 20 20 69 66 28 20 28 70 2d 3e )<<i;. if( (p->
1a4c7 62 74 72 65 65 4d 61 73 6b 20 26 20 6d 61 73 6b btreeMask & mask
1a4c8 29 3d 3d 30 20 29 7b 0a 20 20 20 20 70 2d 3e 62 )==0 ){. p->b
1a4c9 74 72 65 65 4d 61 73 6b 20 7c 3d 20 6d 61 73 6b treeMask |= mask
1a4ca 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 ;. sqlite3Btr
1a4cb 65 65 4d 75 74 65 78 41 72 72 61 79 49 6e 73 65 eeMutexArrayInse
1a4cc 72 74 28 26 70 2d 3e 61 4d 75 74 65 78 2c 20 70 rt(&p->aMutex, p
1a4cd 2d 3e 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 ->db->aDb[i].pBt
1a4ce 29 3b 0a 20 20 7d 0a 7d 0a 0a 0a 23 69 66 20 64 );. }.}...#if d
1a4cf 65 66 69 6e 65 64 28 56 44 42 45 5f 50 52 4f 46 efined(VDBE_PROF
1a4d0 49 4c 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 ILE) || defined(
1a4d1 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0a 2f 2a SQLITE_DEBUG)./*
1a4d2 0a 2a 2a 20 50 72 69 6e 74 20 61 20 73 69 6e 67 .** Print a sing
1a4d3 6c 65 20 6f 70 63 6f 64 65 2e 20 20 54 68 69 73 le opcode. This
1a4d4 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 routine is used
1a4d5 20 66 6f 72 20 64 65 62 75 67 67 69 6e 67 20 6f for debugging o
1a4d6 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 nly..*/.SQLITE_P
1a4d7 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
1a4d8 74 65 33 56 64 62 65 50 72 69 6e 74 4f 70 28 46 te3VdbePrintOp(F
1a4d9 49 4c 45 20 2a 70 4f 75 74 2c 20 69 6e 74 20 70 ILE *pOut, int p
1a4da 63 2c 20 4f 70 20 2a 70 4f 70 29 7b 0a 20 20 63 c, Op *pOp){. c
1a4db 68 61 72 20 2a 7a 50 34 3b 0a 20 20 63 68 61 72 har *zP4;. char
1a4dc 20 7a 50 74 72 5b 35 30 5d 3b 0a 20 20 73 74 61 zPtr[50];. sta
1a4dd 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a tic const char *
1a4de 7a 46 6f 72 6d 61 74 31 20 3d 20 22 25 34 64 20 zFormat1 = "%4d
1a4df 25 2d 31 33 73 20 25 34 64 20 25 34 64 20 25 34 %-13s %4d %4d %4
1a4e0 64 20 25 2d 34 73 20 25 2e 32 58 20 25 73 5c 6e d %-4s %.2X %s\n
1a4e1 22 3b 0a 20 20 69 66 28 20 70 4f 75 74 3d 3d 30 ";. if( pOut==0
1a4e2 20 29 20 70 4f 75 74 20 3d 20 73 74 64 6f 75 74 ) pOut = stdout
1a4e3 3b 0a 20 20 7a 50 34 20 3d 20 64 69 73 70 6c 61 ;. zP4 = displa
1a4e4 79 50 34 28 70 4f 70 2c 20 7a 50 74 72 2c 20 73 yP4(pOp, zPtr, s
1a4e5 69 7a 65 6f 66 28 7a 50 74 72 29 29 3b 0a 20 20 izeof(zPtr));.
1a4e6 66 70 72 69 6e 74 66 28 70 4f 75 74 2c 20 7a 46 fprintf(pOut, zF
1a4e7 6f 72 6d 61 74 31 2c 20 70 63 2c 20 0a 20 20 20 ormat1, pc, .
1a4e8 20 20 20 73 71 6c 69 74 65 33 4f 70 63 6f 64 65 sqlite3Opcode
1a4e9 4e 61 6d 65 28 70 4f 70 2d 3e 6f 70 63 6f 64 65 Name(pOp->opcode
1a4ea 29 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d ), pOp->p1, pOp-
1a4eb 3e 70 32 2c 20 70 4f 70 2d 3e 70 33 2c 20 7a 50 >p2, pOp->p3, zP
1a4ec 34 2c 20 70 4f 70 2d 3e 70 35 2c 0a 23 69 66 64 4, pOp->p5,.#ifd
1a4ed 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a ef SQLITE_DEBUG.
1a4ee 20 20 20 20 20 20 70 4f 70 2d 3e 7a 43 6f 6d 6d pOp->zComm
1a4ef 65 6e 74 20 3f 20 70 4f 70 2d 3e 7a 43 6f 6d 6d ent ? pOp->zComm
1a4f0 65 6e 74 20 3a 20 22 22 0a 23 65 6c 73 65 0a 20 ent : "".#else.
1a4f1 20 20 20 20 20 22 22 0a 23 65 6e 64 69 66 0a 20 "".#endif.
1a4f2 20 29 3b 0a 20 20 66 66 6c 75 73 68 28 70 4f 75 );. fflush(pOu
1a4f3 74 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a t);.}.#endif../*
1a4f4 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61 6e 20 61 .** Release an a
1a4f5 72 72 61 79 20 6f 66 20 4e 20 4d 65 6d 20 65 6c rray of N Mem el
1a4f6 65 6d 65 6e 74 73 0a 2a 2f 0a 73 74 61 74 69 63 ements.*/.static
1a4f7 20 76 6f 69 64 20 72 65 6c 65 61 73 65 4d 65 6d void releaseMem
1a4f8 41 72 72 61 79 28 4d 65 6d 20 2a 70 2c 20 69 6e Array(Mem *p, in
1a4f9 74 20 4e 29 7b 0a 20 20 69 66 28 20 70 20 26 26 t N){. if( p &&
1a4fa 20 4e 20 29 7b 0a 20 20 20 20 4d 65 6d 20 2a 70 N ){. Mem *p
1a4fb 45 6e 64 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 End;. sqlite3
1a4fc 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 *db = p->db;.
1a4fd 20 20 75 38 20 6d 61 6c 6c 6f 63 5f 66 61 69 6c u8 malloc_fail
1a4fe 65 64 20 3d 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 ed = db->mallocF
1a4ff 61 69 6c 65 64 3b 0a 20 20 20 20 66 6f 72 28 70 ailed;. for(p
1a500 45 6e 64 3d 26 70 5b 4e 5d 3b 20 70 3c 70 45 6e End=&p[N]; p<pEn
1a501 64 3b 20 70 2b 2b 29 7b 0a 20 20 20 20 20 20 61 d; p++){. a
1a502 73 73 65 72 74 28 20 28 26 70 5b 31 5d 29 3d 3d ssert( (&p[1])==
1a503 70 45 6e 64 20 7c 7c 20 70 5b 30 5d 2e 64 62 3d pEnd || p[0].db=
1a504 3d 70 5b 31 5d 2e 64 62 20 29 3b 0a 0a 20 20 20 =p[1].db );..
1a505 20 20 20 2f 2a 20 54 68 69 73 20 62 6c 6f 63 6b /* This block
1a506 20 69 73 20 72 65 61 6c 6c 79 20 61 6e 20 69 6e is really an in
1a507 6c 69 6e 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 lined version of
1a508 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 sqlite3VdbeMemR
1a509 65 6c 65 61 73 65 28 29 0a 20 20 20 20 20 20 2a elease(). *
1a50a 2a 20 74 68 61 74 20 74 61 6b 65 73 20 61 64 76 * that takes adv
1a50b 61 6e 74 61 67 65 20 6f 66 20 74 68 65 20 66 61 antage of the fa
1a50c 63 74 20 74 68 61 74 20 74 68 65 20 6d 65 6d 6f ct that the memo
1a50d 72 79 20 63 65 6c 6c 20 76 61 6c 75 65 20 69 73 ry cell value is
1a50e 20 0a 20 20 20 20 20 20 2a 2a 20 62 65 69 6e 67 . ** being
1a50f 20 73 65 74 20 74 6f 20 4e 55 4c 4c 20 61 66 74 set to NULL aft
1a510 65 72 20 72 65 6c 65 61 73 69 6e 67 20 61 6e 79 er releasing any
1a511 20 64 79 6e 61 6d 69 63 20 72 65 73 6f 75 72 63 dynamic resourc
1a512 65 73 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 es.. **.
1a513 20 20 20 2a 2a 20 54 68 65 20 6a 75 73 74 69 66 ** The justif
1a514 69 63 61 74 69 6f 6e 20 66 6f 72 20 64 75 70 6c ication for dupl
1a515 69 63 61 74 69 6e 67 20 63 6f 64 65 20 69 73 20 icating code is
1a516 74 68 61 74 20 61 63 63 6f 72 64 69 6e 67 20 74 that according t
1a517 6f 20 0a 20 20 20 20 20 20 2a 2a 20 63 61 6c 6c o . ** call
1a518 67 72 69 6e 64 2c 20 74 68 69 73 20 63 61 75 73 grind, this caus
1a519 65 73 20 61 20 63 65 72 74 61 69 6e 20 74 65 73 es a certain tes
1a51a 74 20 63 61 73 65 20 74 6f 20 68 69 74 20 74 68 t case to hit th
1a51b 65 20 43 50 55 20 34 2e 37 20 0a 20 20 20 20 20 e CPU 4.7 .
1a51c 20 2a 2a 20 70 65 72 63 65 6e 74 20 6c 65 73 73 ** percent less
1a51d 20 28 78 38 36 20 6c 69 6e 75 78 2c 20 67 63 63 (x86 linux, gcc
1a51e 20 76 65 72 73 69 6f 6e 20 34 2e 31 2e 32 2c 20 version 4.1.2,
1a51f 2d 4f 36 29 20 74 68 61 6e 20 69 66 20 0a 20 20 -O6) than if .
1a520 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 4d 65 ** sqlite3Me
1a521 6d 52 65 6c 65 61 73 65 28 29 20 77 65 72 65 20 mRelease() were
1a522 63 61 6c 6c 65 64 20 66 72 6f 6d 20 68 65 72 65 called from here
1a523 2e 20 57 69 74 68 20 2d 4f 32 2c 20 74 68 69 73 . With -O2, this
1a524 20 6a 75 6d 70 73 0a 20 20 20 20 20 20 2a 2a 20 jumps. **
1a525 74 6f 20 36 2e 36 20 70 65 72 63 65 6e 74 2e 20 to 6.6 percent.
1a526 54 68 65 20 74 65 73 74 20 63 61 73 65 20 69 73 The test case is
1a527 20 69 6e 73 65 72 74 69 6e 67 20 31 30 30 30 20 inserting 1000
1a528 72 6f 77 73 20 69 6e 74 6f 20 61 20 74 61 62 6c rows into a tabl
1a529 65 20 0a 20 20 20 20 20 20 2a 2a 20 77 69 74 68 e . ** with
1a52a 20 6e 6f 20 69 6e 64 65 78 65 73 20 75 73 69 6e no indexes usin
1a52b 67 20 61 20 73 69 6e 67 6c 65 20 70 72 65 70 61 g a single prepa
1a52c 72 65 64 20 49 4e 53 45 52 54 20 73 74 61 74 65 red INSERT state
1a52d 6d 65 6e 74 2c 20 62 69 6e 64 28 29 20 0a 20 20 ment, bind() .
1a52e 20 20 20 20 2a 2a 20 61 6e 64 20 72 65 73 65 74 ** and reset
1a52f 28 29 2e 20 49 6e 73 65 72 74 73 20 61 72 65 20 (). Inserts are
1a530 67 72 6f 75 70 65 64 20 69 6e 74 6f 20 61 20 74 grouped into a t
1a531 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 20 20 ransaction..
1a532 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 */. if( p
1a533 2d 3e 66 6c 61 67 73 26 28 4d 45 4d 5f 41 67 67 ->flags&(MEM_Agg
1a534 7c 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 46 72 61 |MEM_Dyn|MEM_Fra
1a535 6d 65 7c 4d 45 4d 5f 52 6f 77 53 65 74 29 20 29 me|MEM_RowSet) )
1a536 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
1a537 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 3VdbeMemRelease(
1a538 70 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 p);. }else
1a539 69 66 28 20 70 2d 3e 7a 4d 61 6c 6c 6f 63 20 29 if( p->zMalloc )
1a53a 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
1a53b 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 3DbFree(db, p->z
1a53c 4d 61 6c 6c 6f 63 29 3b 0a 20 20 20 20 20 20 20 Malloc);.
1a53d 20 70 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b p->zMalloc = 0;
1a53e 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 . }..
1a53f 70 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e p->flags = MEM_N
1a540 75 6c 6c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 ull;. }. d
1a541 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
1a542 3d 20 6d 61 6c 6c 6f 63 5f 66 61 69 6c 65 64 3b = malloc_failed;
1a543 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 . }.}../*.** De
1a544 6c 65 74 65 20 61 20 56 64 62 65 46 72 61 6d 65 lete a VdbeFrame
1a545 20 6f 62 6a 65 63 74 20 61 6e 64 20 69 74 73 20 object and its
1a546 63 6f 6e 74 65 6e 74 73 2e 20 56 64 62 65 46 72 contents. VdbeFr
1a547 61 6d 65 20 6f 62 6a 65 63 74 73 20 61 72 65 0a ame objects are.
1a548 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 ** allocated by
1a549 74 68 65 20 4f 50 5f 50 72 6f 67 72 61 6d 20 6f the OP_Program o
1a54a 70 63 6f 64 65 20 69 6e 20 73 71 6c 69 74 65 33 pcode in sqlite3
1a54b 56 64 62 65 45 78 65 63 28 29 2e 0a 2a 2f 0a 53 VdbeExec()..*/.S
1a54c 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
1a54d 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 46 72 id sqlite3VdbeFr
1a54e 61 6d 65 44 65 6c 65 74 65 28 56 64 62 65 46 72 ameDelete(VdbeFr
1a54f 61 6d 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 ame *p){. int i
1a550 3b 0a 20 20 4d 65 6d 20 2a 61 4d 65 6d 20 3d 20 ;. Mem *aMem =
1a551 56 64 62 65 46 72 61 6d 65 4d 65 6d 28 70 29 3b VdbeFrameMem(p);
1a552 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 2a . VdbeCursor **
1a553 61 70 43 73 72 20 3d 20 28 56 64 62 65 43 75 72 apCsr = (VdbeCur
1a554 73 6f 72 20 2a 2a 29 26 61 4d 65 6d 5b 70 2d 3e sor **)&aMem[p->
1a555 6e 43 68 69 6c 64 4d 65 6d 5d 3b 0a 20 20 66 6f nChildMem];. fo
1a556 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 68 69 r(i=0; i<p->nChi
1a557 6c 64 43 73 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 ldCsr; i++){.
1a558 20 73 71 6c 69 74 65 33 56 64 62 65 46 72 65 65 sqlite3VdbeFree
1a559 43 75 72 73 6f 72 28 70 2d 3e 76 2c 20 61 70 43 Cursor(p->v, apC
1a55a 73 72 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 72 65 sr[i]);. }. re
1a55b 6c 65 61 73 65 4d 65 6d 41 72 72 61 79 28 61 4d leaseMemArray(aM
1a55c 65 6d 2c 20 70 2d 3e 6e 43 68 69 6c 64 4d 65 6d em, p->nChildMem
1a55d 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 );. sqlite3DbFr
1a55e 65 65 28 70 2d 3e 76 2d 3e 64 62 2c 20 70 29 3b ee(p->v->db, p);
1a55f 0a 7d 0a 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 .}...#ifdef SQLI
1a560 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 TE_ENABLE_MEMORY
1a561 5f 4d 41 4e 41 47 45 4d 45 4e 54 0a 53 51 4c 49 _MANAGEMENT.SQLI
1a562 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
1a563 71 6c 69 74 65 33 56 64 62 65 52 65 6c 65 61 73 qlite3VdbeReleas
1a564 65 42 75 66 66 65 72 73 28 56 64 62 65 20 2a 70 eBuffers(Vdbe *p
1a565 29 7b 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20 69 ){. int ii;. i
1a566 6e 74 20 6e 46 72 65 65 20 3d 20 30 3b 0a 20 20 nt nFree = 0;.
1a567 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
1a568 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 mutex_held(p->db
1a569 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 66 6f ->mutex) );. fo
1a56a 72 28 69 69 3d 31 3b 20 69 69 3c 3d 70 2d 3e 6e r(ii=1; ii<=p->n
1a56b 4d 65 6d 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 Mem; ii++){.
1a56c 4d 65 6d 20 2a 70 4d 65 6d 20 3d 20 26 70 2d 3e Mem *pMem = &p->
1a56d 61 4d 65 6d 5b 69 69 5d 3b 0a 20 20 20 20 69 66 aMem[ii];. if
1a56e 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 ( pMem->flags &
1a56f 4d 45 4d 5f 52 6f 77 53 65 74 20 29 7b 0a 20 20 MEM_RowSet ){.
1a570 20 20 20 20 73 71 6c 69 74 65 33 52 6f 77 53 65 sqlite3RowSe
1a571 74 43 6c 65 61 72 28 70 4d 65 6d 2d 3e 75 2e 70 tClear(pMem->u.p
1a572 52 6f 77 53 65 74 29 3b 0a 20 20 20 20 7d 0a 20 RowSet);. }.
1a573 20 20 20 69 66 28 20 70 4d 65 6d 2d 3e 7a 20 26 if( pMem->z &
1a574 26 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 & pMem->flags&ME
1a575 4d 5f 44 79 6e 20 29 7b 0a 20 20 20 20 20 20 61 M_Dyn ){. a
1a576 73 73 65 72 74 28 20 21 70 4d 65 6d 2d 3e 78 44 ssert( !pMem->xD
1a577 65 6c 20 29 3b 0a 20 20 20 20 20 20 6e 46 72 65 el );. nFre
1a578 65 20 2b 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 e += sqlite3DbMa
1a579 6c 6c 6f 63 53 69 7a 65 28 70 4d 65 6d 2d 3e 64 llocSize(pMem->d
1a57a 62 2c 20 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 20 b, pMem->z);.
1a57b 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 sqlite3VdbeMe
1a57c 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a mRelease(pMem);.
1a57d 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
1a57e 72 6e 20 6e 46 72 65 65 3b 0a 7d 0a 23 65 6e 64 rn nFree;.}.#end
1a57f 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 if..#ifndef SQLI
1a580 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 0a TE_OMIT_EXPLAIN.
1a581 2f 2a 0a 2a 2a 20 47 69 76 65 20 61 20 6c 69 73 /*.** Give a lis
1a582 74 69 6e 67 20 6f 66 20 74 68 65 20 70 72 6f 67 ting of the prog
1a583 72 61 6d 20 69 6e 20 74 68 65 20 76 69 72 74 75 ram in the virtu
1a584 61 6c 20 6d 61 63 68 69 6e 65 2e 0a 2a 2a 0a 2a al machine..**.*
1a585 2a 20 54 68 65 20 69 6e 74 65 72 66 61 63 65 20 * The interface
1a586 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 73 is the same as s
1a587 71 6c 69 74 65 33 56 64 62 65 45 78 65 63 28 29 qlite3VdbeExec()
1a588 2e 20 20 42 75 74 20 69 6e 73 74 65 61 64 20 6f . But instead o
1a589 66 0a 2a 2a 20 72 75 6e 6e 69 6e 67 20 74 68 65 f.** running the
1a58a 20 63 6f 64 65 2c 20 69 74 20 69 6e 76 6f 6b 65 code, it invoke
1a58b 73 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 6f s the callback o
1a58c 6e 63 65 20 66 6f 72 20 65 61 63 68 20 69 6e 73 nce for each ins
1a58d 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69 truction..** Thi
1a58e 73 20 66 65 61 74 75 72 65 20 69 73 20 75 73 65 s feature is use
1a58f 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 22 d to implement "
1a590 45 58 50 4c 41 49 4e 22 2e 0a 2a 2a 0a 2a 2a 20 EXPLAIN"..**.**
1a591 57 68 65 6e 20 70 2d 3e 65 78 70 6c 61 69 6e 3d When p->explain=
1a592 3d 31 2c 20 65 61 63 68 20 69 6e 73 74 72 75 63 =1, each instruc
1a593 74 69 6f 6e 20 69 73 20 6c 69 73 74 65 64 2e 20 tion is listed.
1a594 20 57 68 65 6e 0a 2a 2a 20 70 2d 3e 65 78 70 6c When.** p->expl
1a595 61 69 6e 3d 3d 32 2c 20 6f 6e 6c 79 20 4f 50 5f ain==2, only OP_
1a596 45 78 70 6c 61 69 6e 20 69 6e 73 74 72 75 63 74 Explain instruct
1a597 69 6f 6e 73 20 61 72 65 20 6c 69 73 74 65 64 20 ions are listed
1a598 61 6e 64 20 74 68 65 73 65 0a 2a 2a 20 61 72 65 and these.** are
1a599 20 73 68 6f 77 6e 20 69 6e 20 61 20 64 69 66 66 shown in a diff
1a59a 65 72 65 6e 74 20 66 6f 72 6d 61 74 2e 20 20 70 erent format. p
1a59b 2d 3e 65 78 70 6c 61 69 6e 3d 3d 32 20 69 73 20 ->explain==2 is
1a59c 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e used to implemen
1a59d 74 0a 2a 2a 20 45 58 50 4c 41 49 4e 20 51 55 45 t.** EXPLAIN QUE
1a59e 52 59 20 50 4c 41 4e 2e 0a 2a 2f 0a 53 51 4c 49 RY PLAN..*/.SQLI
1a59f 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
1a5a0 71 6c 69 74 65 33 56 64 62 65 4c 69 73 74 28 0a qlite3VdbeList(.
1a5a1 20 20 56 64 62 65 20 2a 70 20 20 20 20 20 20 20 Vdbe *p
1a5a2 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
1a5a3 68 65 20 56 44 42 45 20 2a 2f 0a 29 7b 0a 20 20 he VDBE */.){.
1a5a4 69 6e 74 20 6e 52 6f 77 3b 20 20 20 20 20 20 20 int nRow;
1a5a5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a5a6 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 /* Total nu
1a5a7 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 74 6f 20 mber of rows to
1a5a8 72 65 74 75 72 6e 20 2a 2f 0a 20 20 69 6e 74 20 return */. int
1a5a9 6e 53 75 62 20 3d 20 30 3b 20 20 20 20 20 20 20 nSub = 0;
1a5aa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a5ab 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 75 /* Number of su
1a5ac 62 2d 76 64 62 65 73 20 73 65 65 6e 20 73 6f 20 b-vdbes seen so
1a5ad 66 61 72 20 2a 2f 0a 20 20 53 75 62 50 72 6f 67 far */. SubProg
1a5ae 72 61 6d 20 2a 2a 61 70 53 75 62 20 3d 20 30 3b ram **apSub = 0;
1a5af 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1a5b0 20 41 72 72 61 79 20 6f 66 20 73 75 62 2d 76 64 Array of sub-vd
1a5b1 62 65 73 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 53 bes */. Mem *pS
1a5b2 75 62 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 ub = 0;. sqlite
1a5b3 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 3 *db = p->db;.
1a5b4 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 72 63 int i;. int rc
1a5b5 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
1a5b6 4d 65 6d 20 2a 70 4d 65 6d 20 3d 20 70 2d 3e 70 Mem *pMem = p->p
1a5b7 52 65 73 75 6c 74 53 65 74 20 3d 20 26 70 2d 3e ResultSet = &p->
1a5b8 61 4d 65 6d 5b 31 5d 3b 0a 0a 20 20 61 73 73 65 aMem[1];.. asse
1a5b9 72 74 28 20 70 2d 3e 65 78 70 6c 61 69 6e 20 29 rt( p->explain )
1a5ba 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6d ;. assert( p->m
1a5bb 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 agic==VDBE_MAGIC
1a5bc 5f 52 55 4e 20 29 3b 0a 20 20 61 73 73 65 72 74 _RUN );. assert
1a5bd 28 20 64 62 2d 3e 6d 61 67 69 63 3d 3d 53 51 4c ( db->magic==SQL
1a5be 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 20 29 ITE_MAGIC_BUSY )
1a5bf 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 72 ;. assert( p->r
1a5c0 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 c==SQLITE_OK ||
1a5c1 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 p->rc==SQLITE_BU
1a5c2 53 59 20 7c 7c 20 70 2d 3e 72 63 3d 3d 53 51 4c SY || p->rc==SQL
1a5c3 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 0a 20 20 ITE_NOMEM );..
1a5c4 2f 2a 20 45 76 65 6e 20 74 68 6f 75 67 68 20 74 /* Even though t
1a5c5 68 69 73 20 6f 70 63 6f 64 65 20 64 6f 65 73 20 his opcode does
1a5c6 6e 6f 74 20 75 73 65 20 64 79 6e 61 6d 69 63 20 not use dynamic
1a5c7 73 74 72 69 6e 67 73 20 66 6f 72 0a 20 20 2a 2a strings for. **
1a5c8 20 74 68 65 20 72 65 73 75 6c 74 2c 20 72 65 73 the result, res
1a5c9 75 6c 74 20 63 6f 6c 75 6d 6e 73 20 6d 61 79 20 ult columns may
1a5ca 62 65 63 6f 6d 65 20 64 79 6e 61 6d 69 63 20 69 become dynamic i
1a5cb 66 20 74 68 65 20 75 73 65 72 20 63 61 6c 6c 73 f the user calls
1a5cc 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f . ** sqlite3_co
1a5cd 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 2c 20 63 lumn_text16(), c
1a5ce 61 75 73 69 6e 67 20 61 20 74 72 61 6e 73 6c 61 ausing a transla
1a5cf 74 69 6f 6e 20 74 6f 20 55 54 46 2d 31 36 20 65 tion to UTF-16 e
1a5d0 6e 63 6f 64 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 ncoding.. */.
1a5d1 72 65 6c 65 61 73 65 4d 65 6d 41 72 72 61 79 28 releaseMemArray(
1a5d2 70 4d 65 6d 2c 20 38 29 3b 0a 0a 20 20 69 66 28 pMem, 8);.. if(
1a5d3 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4e p->rc==SQLITE_N
1a5d4 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 2f 2a 20 54 OMEM ){. /* T
1a5d5 68 69 73 20 68 61 70 70 65 6e 73 20 69 66 20 61 his happens if a
1a5d6 20 6d 61 6c 6c 6f 63 28 29 20 69 6e 73 69 64 65 malloc() inside
1a5d7 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 a call to sqlit
1a5d8 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 e3_column_text()
1a5d9 20 6f 72 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 or. ** sqlit
1a5da 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 e3_column_text16
1a5db 28 29 20 66 61 69 6c 65 64 2e 20 20 2a 2f 0a 20 () failed. */.
1a5dc 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 db->mallocFai
1a5dd 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 72 65 74 led = 1;. ret
1a5de 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 urn SQLITE_ERROR
1a5df 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 67 75 ;. }.. /* Figu
1a5e0 72 65 20 6f 75 74 20 74 6f 74 61 6c 20 6e 75 6d re out total num
1a5e1 62 65 72 20 6f 66 20 72 6f 77 73 20 74 68 61 74 ber of rows that
1a5e2 20 77 69 6c 6c 20 62 65 20 72 65 74 75 72 6e 65 will be returne
1a5e3 64 20 62 79 20 74 68 69 73 20 0a 20 20 2a 2a 20 d by this . **
1a5e4 45 58 50 4c 41 49 4e 20 70 72 6f 67 72 61 6d 2e EXPLAIN program.
1a5e5 20 20 2a 2f 0a 20 20 6e 52 6f 77 20 3d 20 70 2d */. nRow = p-
1a5e6 3e 6e 4f 70 3b 0a 20 20 69 66 28 20 70 2d 3e 65 >nOp;. if( p->e
1a5e7 78 70 6c 61 69 6e 3d 3d 31 20 29 7b 0a 20 20 20 xplain==1 ){.
1a5e8 20 70 53 75 62 20 3d 20 26 70 2d 3e 61 4d 65 6d pSub = &p->aMem
1a5e9 5b 39 5d 3b 0a 20 20 20 20 69 66 28 20 70 53 75 [9];. if( pSu
1a5ea 62 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 42 6c 6f b->flags&MEM_Blo
1a5eb 62 20 29 7b 0a 20 20 20 20 20 20 6e 53 75 62 20 b ){. nSub
1a5ec 3d 20 70 53 75 62 2d 3e 6e 2f 73 69 7a 65 6f 66 = pSub->n/sizeof
1a5ed 28 56 64 62 65 2a 29 3b 0a 20 20 20 20 20 20 61 (Vdbe*);. a
1a5ee 70 53 75 62 20 3d 20 28 53 75 62 50 72 6f 67 72 pSub = (SubProgr
1a5ef 61 6d 20 2a 2a 29 70 53 75 62 2d 3e 7a 3b 0a 20 am **)pSub->z;.
1a5f0 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30 }. for(i=0
1a5f1 3b 20 69 3c 6e 53 75 62 3b 20 69 2b 2b 29 7b 0a ; i<nSub; i++){.
1a5f2 20 20 20 20 20 20 6e 52 6f 77 20 2b 3d 20 61 70 nRow += ap
1a5f3 53 75 62 5b 69 5d 2d 3e 6e 4f 70 3b 0a 20 20 20 Sub[i]->nOp;.
1a5f4 20 7d 0a 20 20 7d 0a 0a 20 20 64 6f 7b 0a 20 20 }. }.. do{.
1a5f5 20 20 69 20 3d 20 70 2d 3e 70 63 2b 2b 3b 0a 20 i = p->pc++;.
1a5f6 20 7d 77 68 69 6c 65 28 20 69 3c 6e 52 6f 77 20 }while( i<nRow
1a5f7 26 26 20 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 32 && p->explain==2
1a5f8 20 26 26 20 70 2d 3e 61 4f 70 5b 69 5d 2e 6f 70 && p->aOp[i].op
1a5f9 63 6f 64 65 21 3d 4f 50 5f 45 78 70 6c 61 69 6e code!=OP_Explain
1a5fa 20 29 3b 0a 20 20 69 66 28 20 69 3e 3d 6e 52 6f );. if( i>=nRo
1a5fb 77 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d w ){. p->rc =
1a5fc 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 SQLITE_OK;.
1a5fd 72 63 20 3d 20 53 51 4c 49 54 45 5f 44 4f 4e 45 rc = SQLITE_DONE
1a5fe 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 64 62 ;. }else if( db
1a5ff 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72 75 70 74 ->u1.isInterrupt
1a600 65 64 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 ed ){. p->rc
1a601 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 = SQLITE_INTERRU
1a602 50 54 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c PT;. rc = SQL
1a603 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 73 ITE_ERROR;. s
1a604 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 qlite3SetString(
1a605 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c &p->zErrMsg, db,
1a606 20 22 25 73 22 2c 20 73 71 6c 69 74 65 33 45 72 "%s", sqlite3Er
1a607 72 53 74 72 28 70 2d 3e 72 63 29 29 3b 0a 20 20 rStr(p->rc));.
1a608 7d 65 6c 73 65 7b 0a 20 20 20 20 63 68 61 72 20 }else{. char
1a609 2a 7a 3b 0a 20 20 20 20 4f 70 20 2a 70 4f 70 3b *z;. Op *pOp;
1a60a 0a 20 20 20 20 69 66 28 20 69 3c 70 2d 3e 6e 4f . if( i<p->nO
1a60b 70 20 29 7b 0a 20 20 20 20 20 20 70 4f 70 20 3d p ){. pOp =
1a60c 20 26 70 2d 3e 61 4f 70 5b 69 5d 3b 0a 20 20 20 &p->aOp[i];.
1a60d 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 6e }else{. in
1a60e 74 20 6a 3b 0a 20 20 20 20 20 20 69 20 2d 3d 20 t j;. i -=
1a60f 70 2d 3e 6e 4f 70 3b 0a 20 20 20 20 20 20 66 6f p->nOp;. fo
1a610 72 28 6a 3d 30 3b 20 69 3e 3d 61 70 53 75 62 5b r(j=0; i>=apSub[
1a611 6a 5d 2d 3e 6e 4f 70 3b 20 6a 2b 2b 29 7b 0a 20 j]->nOp; j++){.
1a612 20 20 20 20 20 20 20 69 20 2d 3d 20 61 70 53 75 i -= apSu
1a613 62 5b 6a 5d 2d 3e 6e 4f 70 3b 0a 20 20 20 20 20 b[j]->nOp;.
1a614 20 7d 0a 20 20 20 20 20 20 70 4f 70 20 3d 20 26 }. pOp = &
1a615 61 70 53 75 62 5b 6a 5d 2d 3e 61 4f 70 5b 69 5d apSub[j]->aOp[i]
1a616 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
1a617 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 31 20 29 7b p->explain==1 ){
1a618 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 . pMem->fla
1a619 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 gs = MEM_Int;.
1a61a 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d pMem->type =
1a61b 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 3b SQLITE_INTEGER;
1a61c 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 . pMem->u.i
1a61d 20 3d 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 = i;
1a61e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a61f 20 20 20 20 20 2f 2a 20 50 72 6f 67 72 61 6d 20 /* Program
1a620 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 20 20 20 counter */.
1a621 20 70 4d 65 6d 2b 2b 3b 0a 20 20 0a 20 20 20 20 pMem++;. .
1a622 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 pMem->flags =
1a623 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 53 MEM_Static|MEM_S
1a624 74 72 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 20 tr|MEM_Term;.
1a625 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 28 63 68 pMem->z = (ch
1a626 61 72 2a 29 73 71 6c 69 74 65 33 4f 70 63 6f 64 ar*)sqlite3Opcod
1a627 65 4e 61 6d 65 28 70 4f 70 2d 3e 6f 70 63 6f 64 eName(pOp->opcod
1a628 65 29 3b 20 20 2f 2a 20 4f 70 63 6f 64 65 20 2a e); /* Opcode *
1a629 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 /. assert(
1a62a 70 4d 65 6d 2d 3e 7a 21 3d 30 20 29 3b 0a 20 20 pMem->z!=0 );.
1a62b 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 73 71 pMem->n = sq
1a62c 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 4d lite3Strlen30(pM
1a62d 65 6d 2d 3e 7a 29 3b 0a 20 20 20 20 20 20 70 4d em->z);. pM
1a62e 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 em->type = SQLIT
1a62f 45 5f 54 45 58 54 3b 0a 20 20 20 20 20 20 70 4d E_TEXT;. pM
1a630 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54 45 em->enc = SQLITE
1a631 5f 55 54 46 38 3b 0a 20 20 20 20 20 20 70 4d 65 _UTF8;. pMe
1a632 6d 2b 2b 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 m++;.. if(
1a633 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f pOp->p4type==P4_
1a634 53 55 42 50 52 4f 47 52 41 4d 20 29 7b 0a 20 20 SUBPROGRAM ){.
1a635 20 20 20 20 20 20 69 6e 74 20 6e 42 79 74 65 20 int nByte
1a636 3d 20 28 6e 53 75 62 2b 31 29 2a 73 69 7a 65 6f = (nSub+1)*sizeo
1a637 66 28 53 75 62 50 72 6f 67 72 61 6d 2a 29 3b 0a f(SubProgram*);.
1a638 20 20 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 int j;.
1a639 20 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 for(j=0;
1a63a 6a 3c 6e 53 75 62 3b 20 6a 2b 2b 29 7b 0a 20 20 j<nSub; j++){.
1a63b 20 20 20 20 20 20 20 20 69 66 28 20 61 70 53 75 if( apSu
1a63c 62 5b 6a 5d 3d 3d 70 4f 70 2d 3e 70 34 2e 70 50 b[j]==pOp->p4.pP
1a63d 72 6f 67 72 61 6d 20 29 20 62 72 65 61 6b 3b 0a rogram ) break;.
1a63e 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1a63f 20 20 69 66 28 20 6a 3d 3d 6e 53 75 62 20 26 26 if( j==nSub &&
1a640 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 73 71 6c 69 SQLITE_OK==sqli
1a641 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 te3VdbeMemGrow(p
1a642 53 75 62 2c 20 6e 42 79 74 65 2c 20 31 29 20 29 Sub, nByte, 1) )
1a643 7b 0a 20 20 20 20 20 20 20 20 20 20 61 70 53 75 {. apSu
1a644 62 20 3d 20 28 53 75 62 50 72 6f 67 72 61 6d 20 b = (SubProgram
1a645 2a 2a 29 70 53 75 62 2d 3e 7a 3b 0a 20 20 20 20 **)pSub->z;.
1a646 20 20 20 20 20 20 61 70 53 75 62 5b 6e 53 75 62 apSub[nSub
1a647 2b 2b 5d 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 50 ++] = pOp->p4.pP
1a648 72 6f 67 72 61 6d 3b 0a 20 20 20 20 20 20 20 20 rogram;.
1a649 20 20 70 53 75 62 2d 3e 66 6c 61 67 73 20 7c 3d pSub->flags |=
1a64a 20 4d 45 4d 5f 42 6c 6f 62 3b 0a 20 20 20 20 20 MEM_Blob;.
1a64b 20 20 20 20 20 70 53 75 62 2d 3e 6e 20 3d 20 6e pSub->n = n
1a64c 53 75 62 2a 73 69 7a 65 6f 66 28 53 75 62 50 72 Sub*sizeof(SubPr
1a64d 6f 67 72 61 6d 2a 29 3b 0a 20 20 20 20 20 20 20 ogram*);.
1a64e 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d }. }. }
1a64f 0a 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 .. pMem->flag
1a650 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 s = MEM_Int;.
1a651 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 70 4f 70 pMem->u.i = pOp
1a652 2d 3e 70 31 3b 20 20 20 20 20 20 20 20 20 20 20 ->p1;
1a653 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1a654 2a 20 50 31 20 2a 2f 0a 20 20 20 20 70 4d 65 6d * P1 */. pMem
1a655 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f ->type = SQLITE_
1a656 49 4e 54 45 47 45 52 3b 0a 20 20 20 20 70 4d 65 INTEGER;. pMe
1a657 6d 2b 2b 3b 0a 0a 20 20 20 20 70 4d 65 6d 2d 3e m++;.. pMem->
1a658 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b flags = MEM_Int;
1a659 0a 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d . pMem->u.i =
1a65a 20 70 4f 70 2d 3e 70 32 3b 20 20 20 20 20 20 20 pOp->p2;
1a65b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a65c 20 20 20 2f 2a 20 50 32 20 2a 2f 0a 20 20 20 20 /* P2 */.
1a65d 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c pMem->type = SQL
1a65e 49 54 45 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 ITE_INTEGER;.
1a65f 20 70 4d 65 6d 2b 2b 3b 0a 0a 20 20 20 20 69 66 pMem++;.. if
1a660 28 20 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 31 20 ( p->explain==1
1a661 29 7b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 ){. pMem->f
1a662 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a lags = MEM_Int;.
1a663 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 pMem->u.i
1a664 3d 20 70 4f 70 2d 3e 70 33 3b 20 20 20 20 20 20 = pOp->p3;
1a665 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a666 20 20 20 20 2f 2a 20 50 33 20 2a 2f 0a 20 20 20 /* P3 */.
1a667 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 pMem->type =
1a668 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 3b 0a SQLITE_INTEGER;.
1a669 20 20 20 20 20 20 70 4d 65 6d 2b 2b 3b 0a 20 20 pMem++;.
1a66a 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 73 71 6c }.. if( sql
1a66b 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 ite3VdbeMemGrow(
1a66c 70 4d 65 6d 2c 20 33 32 2c 20 30 29 20 29 7b 20 pMem, 32, 0) ){
1a66d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 34 /* P4
1a66e 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 */. assert
1a66f 28 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 ( p->db->mallocF
1a670 61 69 6c 65 64 20 29 3b 0a 20 20 20 20 20 20 72 ailed );. r
1a671 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 eturn SQLITE_ERR
1a672 4f 52 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d OR;. }. pM
1a673 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f em->flags = MEM_
1a674 44 79 6e 7c 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f Dyn|MEM_Str|MEM_
1a675 54 65 72 6d 3b 0a 20 20 20 20 7a 20 3d 20 64 69 Term;. z = di
1a676 73 70 6c 61 79 50 34 28 70 4f 70 2c 20 70 4d 65 splayP4(pOp, pMe
1a677 6d 2d 3e 7a 2c 20 33 32 29 3b 0a 20 20 20 20 69 m->z, 32);. i
1a678 66 28 20 7a 21 3d 70 4d 65 6d 2d 3e 7a 20 29 7b f( z!=pMem->z ){
1a679 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
1a67a 62 65 4d 65 6d 53 65 74 53 74 72 28 70 4d 65 6d beMemSetStr(pMem
1a67b 2c 20 7a 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f , z, -1, SQLITE_
1a67c 55 54 46 38 2c 20 30 29 3b 0a 20 20 20 20 7d 65 UTF8, 0);. }e
1a67d 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 lse{. asser
1a67e 74 28 20 70 4d 65 6d 2d 3e 7a 21 3d 30 20 29 3b t( pMem->z!=0 );
1a67f 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d . pMem->n =
1a680 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 sqlite3Strlen30
1a681 28 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 20 20 20 (pMem->z);.
1a682 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c pMem->enc = SQL
1a683 49 54 45 5f 55 54 46 38 3b 0a 20 20 20 20 7d 0a ITE_UTF8;. }.
1a684 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d pMem->type =
1a685 20 53 51 4c 49 54 45 5f 54 45 58 54 3b 0a 20 20 SQLITE_TEXT;.
1a686 20 20 70 4d 65 6d 2b 2b 3b 0a 0a 20 20 20 20 69 pMem++;.. i
1a687 66 28 20 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 31 f( p->explain==1
1a688 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 ){. if( sq
1a689 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 lite3VdbeMemGrow
1a68a 28 70 4d 65 6d 2c 20 34 2c 20 30 29 20 29 7b 0a (pMem, 4, 0) ){.
1a68b 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
1a68c 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 p->db->mallocFai
1a68d 6c 65 64 20 29 3b 0a 20 20 20 20 20 20 20 20 72 led );. r
1a68e 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 eturn SQLITE_ERR
1a68f 4f 52 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 OR;. }.
1a690 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 pMem->flags =
1a691 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 53 74 72 7c MEM_Dyn|MEM_Str|
1a692 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 20 20 20 20 MEM_Term;.
1a693 70 4d 65 6d 2d 3e 6e 20 3d 20 32 3b 0a 20 20 20 pMem->n = 2;.
1a694 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 sqlite3_snpri
1a695 6e 74 66 28 33 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 ntf(3, pMem->z,
1a696 22 25 2e 32 78 22 2c 20 70 4f 70 2d 3e 70 35 29 "%.2x", pOp->p5)
1a697 3b 20 20 20 2f 2a 20 50 35 20 2a 2f 0a 20 20 20 ; /* P5 */.
1a698 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 pMem->type =
1a699 53 51 4c 49 54 45 5f 54 45 58 54 3b 0a 20 20 20 SQLITE_TEXT;.
1a69a 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 pMem->enc = S
1a69b 51 4c 49 54 45 5f 55 54 46 38 3b 0a 20 20 20 20 QLITE_UTF8;.
1a69c 20 20 70 4d 65 6d 2b 2b 3b 0a 20 20 0a 23 69 66 pMem++;. .#if
1a69d 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 def SQLITE_DEBUG
1a69e 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70 2d 3e . if( pOp->
1a69f 7a 43 6f 6d 6d 65 6e 74 20 29 7b 0a 20 20 20 20 zComment ){.
1a6a0 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 pMem->flags
1a6a1 3d 20 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 54 65 = MEM_Str|MEM_Te
1a6a2 72 6d 3b 0a 20 20 20 20 20 20 20 20 70 4d 65 6d rm;. pMem
1a6a3 2d 3e 7a 20 3d 20 70 4f 70 2d 3e 7a 43 6f 6d 6d ->z = pOp->zComm
1a6a4 65 6e 74 3b 0a 20 20 20 20 20 20 20 20 70 4d 65 ent;. pMe
1a6a5 6d 2d 3e 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 m->n = sqlite3St
1a6a6 72 6c 65 6e 33 30 28 70 4d 65 6d 2d 3e 7a 29 3b rlen30(pMem->z);
1a6a7 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 65 . pMem->e
1a6a8 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 nc = SQLITE_UTF8
1a6a9 3b 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e ;. pMem->
1a6aa 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 54 45 type = SQLITE_TE
1a6ab 58 54 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 0a XT;. }else.
1a6ac 23 65 6e 64 69 66 0a 20 20 20 20 20 20 7b 0a 20 #endif. {.
1a6ad 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 pMem->fla
1a6ae 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 20 20 gs = MEM_Null;
1a6af 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a6b0 20 20 20 20 20 2f 2a 20 43 6f 6d 6d 65 6e 74 20 /* Comment
1a6b1 2a 2f 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d */. pMem-
1a6b2 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 4e >type = SQLITE_N
1a6b3 55 4c 4c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 ULL;. }.
1a6b4 20 7d 0a 0a 20 20 20 20 70 2d 3e 6e 52 65 73 43 }.. p->nResC
1a6b5 6f 6c 75 6d 6e 20 3d 20 38 20 2d 20 35 2a 28 70 olumn = 8 - 5*(p
1a6b6 2d 3e 65 78 70 6c 61 69 6e 2d 31 29 3b 0a 20 20 ->explain-1);.
1a6b7 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 p->rc = SQLITE
1a6b8 5f 4f 4b 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 _OK;. rc = SQ
1a6b9 4c 49 54 45 5f 52 4f 57 3b 0a 20 20 7d 0a 20 20 LITE_ROW;. }.
1a6ba 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e return rc;.}.#en
1a6bb 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d dif /* SQLITE_OM
1a6bc 49 54 5f 45 58 50 4c 41 49 4e 20 2a 2f 0a 0a 23 IT_EXPLAIN */..#
1a6bd 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 ifdef SQLITE_DEB
1a6be 55 47 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 74 UG./*.** Print t
1a6bf 68 65 20 53 51 4c 20 74 68 61 74 20 77 61 73 20 he SQL that was
1a6c0 75 73 65 64 20 74 6f 20 67 65 6e 65 72 61 74 65 used to generate
1a6c1 20 61 20 56 44 42 45 20 70 72 6f 67 72 61 6d 2e a VDBE program.
1a6c2 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1a6c3 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
1a6c4 64 62 65 50 72 69 6e 74 53 71 6c 28 56 64 62 65 dbePrintSql(Vdbe
1a6c5 20 2a 70 29 7b 0a 20 20 69 6e 74 20 6e 4f 70 20 *p){. int nOp
1a6c6 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 20 56 64 62 65 = p->nOp;. Vdbe
1a6c7 4f 70 20 2a 70 4f 70 3b 0a 20 20 69 66 28 20 6e Op *pOp;. if( n
1a6c8 4f 70 3c 31 20 29 20 72 65 74 75 72 6e 3b 0a 20 Op<1 ) return;.
1a6c9 20 70 4f 70 20 3d 20 26 70 2d 3e 61 4f 70 5b 30 pOp = &p->aOp[0
1a6ca 5d 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 ];. if( pOp->op
1a6cb 63 6f 64 65 3d 3d 4f 50 5f 54 72 61 63 65 20 26 code==OP_Trace &
1a6cc 26 20 70 4f 70 2d 3e 70 34 2e 7a 21 3d 30 20 29 & pOp->p4.z!=0 )
1a6cd 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 {. const char
1a6ce 20 2a 7a 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b *z = pOp->p4.z;
1a6cf 0a 20 20 20 20 77 68 69 6c 65 28 20 73 71 6c 69 . while( sqli
1a6d0 74 65 33 49 73 73 70 61 63 65 28 2a 7a 29 20 29 te3Isspace(*z) )
1a6d1 20 7a 2b 2b 3b 0a 20 20 20 20 70 72 69 6e 74 66 z++;. printf
1a6d2 28 22 53 51 4c 3a 20 5b 25 73 5d 5c 6e 22 2c 20 ("SQL: [%s]\n",
1a6d3 7a 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 z);. }.}.#endif
1a6d4 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 ..#if !defined(S
1a6d5 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 QLITE_OMIT_TRACE
1a6d6 29 20 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c ) && defined(SQL
1a6d7 49 54 45 5f 45 4e 41 42 4c 45 5f 49 4f 54 52 41 ITE_ENABLE_IOTRA
1a6d8 43 45 29 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 CE)./*.** Print
1a6d9 61 6e 20 49 4f 54 52 41 43 45 20 6d 65 73 73 61 an IOTRACE messa
1a6da 67 65 20 73 68 6f 77 69 6e 67 20 53 51 4c 20 63 ge showing SQL c
1a6db 6f 6e 74 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 ontent..*/.SQLIT
1a6dc 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
1a6dd 71 6c 69 74 65 33 56 64 62 65 49 4f 54 72 61 63 qlite3VdbeIOTrac
1a6de 65 53 71 6c 28 56 64 62 65 20 2a 70 29 7b 0a 20 eSql(Vdbe *p){.
1a6df 20 69 6e 74 20 6e 4f 70 20 3d 20 70 2d 3e 6e 4f int nOp = p->nO
1a6e0 70 3b 0a 20 20 56 64 62 65 4f 70 20 2a 70 4f 70 p;. VdbeOp *pOp
1a6e1 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 49 ;. if( sqlite3I
1a6e2 6f 54 72 61 63 65 3d 3d 30 20 29 20 72 65 74 75 oTrace==0 ) retu
1a6e3 72 6e 3b 0a 20 20 69 66 28 20 6e 4f 70 3c 31 20 rn;. if( nOp<1
1a6e4 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 4f 70 20 ) return;. pOp
1a6e5 3d 20 26 70 2d 3e 61 4f 70 5b 30 5d 3b 0a 20 20 = &p->aOp[0];.
1a6e6 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d if( pOp->opcode=
1a6e7 3d 4f 50 5f 54 72 61 63 65 20 26 26 20 70 4f 70 =OP_Trace && pOp
1a6e8 2d 3e 70 34 2e 7a 21 3d 30 20 29 7b 0a 20 20 20 ->p4.z!=0 ){.
1a6e9 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 20 20 63 int i, j;. c
1a6ea 68 61 72 20 7a 5b 31 30 30 30 5d 3b 0a 20 20 20 har z[1000];.
1a6eb 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 sqlite3_snprint
1a6ec 66 28 73 69 7a 65 6f 66 28 7a 29 2c 20 7a 2c 20 f(sizeof(z), z,
1a6ed 22 25 73 22 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29 "%s", pOp->p4.z)
1a6ee 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 73 ;. for(i=0; s
1a6ef 71 6c 69 74 65 33 49 73 73 70 61 63 65 28 7a 5b qlite3Isspace(z[
1a6f0 69 5d 29 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20 20 i]); i++){}.
1a6f1 66 6f 72 28 6a 3d 30 3b 20 7a 5b 69 5d 3b 20 69 for(j=0; z[i]; i
1a6f2 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 ++){. if( s
1a6f3 71 6c 69 74 65 33 49 73 73 70 61 63 65 28 7a 5b qlite3Isspace(z[
1a6f4 69 5d 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69 i]) ){. i
1a6f5 66 28 20 7a 5b 69 2d 31 5d 21 3d 27 20 27 20 29 f( z[i-1]!=' ' )
1a6f6 7b 0a 20 20 20 20 20 20 20 20 20 20 7a 5b 6a 2b {. z[j+
1a6f7 2b 5d 20 3d 20 27 20 27 3b 0a 20 20 20 20 20 20 +] = ' ';.
1a6f8 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b }. }else{
1a6f9 0a 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 . z[j++]
1a6fa 3d 20 7a 5b 69 5d 3b 0a 20 20 20 20 20 20 7d 0a = z[i];. }.
1a6fb 20 20 20 20 7d 0a 20 20 20 20 7a 5b 6a 5d 20 3d }. z[j] =
1a6fc 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 49 0;. sqlite3I
1a6fd 6f 54 72 61 63 65 28 22 53 51 4c 20 25 73 5c 6e oTrace("SQL %s\n
1a6fe 22 2c 20 7a 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e ", z);. }.}.#en
1a6ff 64 69 66 20 2f 2a 20 21 53 51 4c 49 54 45 5f 4f dif /* !SQLITE_O
1a700 4d 49 54 5f 54 52 41 43 45 20 26 26 20 53 51 4c MIT_TRACE && SQL
1a701 49 54 45 5f 45 4e 41 42 4c 45 5f 49 4f 54 52 41 ITE_ENABLE_IOTRA
1a702 43 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c CE */../*.** All
1a703 6f 63 61 74 65 20 73 70 61 63 65 20 66 72 6f 6d ocate space from
1a704 20 61 20 66 69 78 65 64 20 73 69 7a 65 20 62 75 a fixed size bu
1a705 66 66 65 72 2e 20 20 4d 61 6b 65 20 2a 70 70 20 ffer. Make *pp
1a706 70 6f 69 6e 74 20 74 6f 20 74 68 65 0a 2a 2a 20 point to the.**
1a707 61 6c 6c 6f 63 61 74 65 64 20 73 70 61 63 65 2e allocated space.
1a708 20 20 28 4e 6f 74 65 3a 20 20 70 70 20 69 73 20 (Note: pp is
1a709 61 20 63 68 61 72 2a 20 72 61 74 68 65 72 20 74 a char* rather t
1a70a 68 61 6e 20 61 20 76 6f 69 64 2a 2a 20 74 6f 0a han a void** to.
1a70b 2a 2a 20 77 6f 72 6b 20 61 72 6f 75 6e 64 20 74 ** work around t
1a70c 68 65 20 70 6f 69 6e 74 65 72 20 61 6c 69 61 73 he pointer alias
1a70d 69 6e 67 20 72 75 6c 65 73 20 6f 66 20 43 2e 29 ing rules of C.)
1a70e 20 20 2a 70 70 20 73 68 6f 75 6c 64 20 69 6e 69 *pp should ini
1a70f 74 69 61 6c 6c 79 0a 2a 2a 20 62 65 20 7a 65 72 tially.** be zer
1a710 6f 2e 20 20 49 66 20 2a 70 70 20 69 73 20 6e 6f o. If *pp is no
1a711 74 20 7a 65 72 6f 2c 20 74 68 61 74 20 6d 65 61 t zero, that mea
1a712 6e 73 20 74 68 61 74 20 74 68 65 20 73 70 61 63 ns that the spac
1a713 65 20 68 61 73 20 61 6c 72 65 61 64 79 0a 2a 2a e has already.**
1a714 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 been allocated
1a715 61 6e 64 20 74 68 69 73 20 72 6f 75 74 69 6e 65 and this routine
1a716 20 69 73 20 61 20 6e 6f 6f 70 2e 0a 2a 2a 0a 2a is a noop..**.*
1a717 2a 20 6e 42 79 74 65 20 69 73 20 74 68 65 20 6e * nByte is the n
1a718 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f umber of bytes o
1a719 66 20 73 70 61 63 65 20 6e 65 65 64 65 64 2e 0a f space needed..
1a71a 2a 2a 0a 2a 2a 20 2a 70 70 46 72 6f 6d 20 70 6f **.** *ppFrom po
1a71b 69 6e 74 20 74 6f 20 61 76 61 69 6c 61 62 6c 65 int to available
1a71c 20 73 70 61 63 65 20 61 6e 64 20 70 45 6e 64 20 space and pEnd
1a71d 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 65 6e points to the en
1a71e 64 20 6f 66 20 74 68 65 0a 2a 2a 20 61 76 61 69 d of the.** avai
1a71f 6c 61 62 6c 65 20 73 70 61 63 65 2e 0a 2a 2a 0a lable space..**.
1a720 2a 2a 20 2a 70 6e 42 79 74 65 20 69 73 20 61 20 ** *pnByte is a
1a721 63 6f 75 6e 74 65 72 20 6f 66 20 74 68 65 20 6e counter of the n
1a722 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f umber of bytes o
1a723 66 20 73 70 61 63 65 20 74 68 61 74 20 68 61 76 f space that hav
1a724 65 20 66 61 69 6c 65 64 0a 2a 2a 20 74 6f 20 61 e failed.** to a
1a725 6c 6c 6f 63 61 74 65 2e 20 20 49 66 20 74 68 65 llocate. If the
1a726 72 65 20 69 73 20 69 6e 73 75 66 66 69 63 69 65 re is insufficie
1a727 6e 74 20 73 70 61 63 65 20 69 6e 20 2a 70 70 46 nt space in *ppF
1a728 72 6f 6d 20 74 6f 20 73 61 74 69 73 66 79 20 74 rom to satisfy t
1a729 68 65 0a 2a 2a 20 72 65 71 75 65 73 74 2c 20 74 he.** request, t
1a72a 68 65 6e 20 69 6e 63 72 65 6d 65 6e 74 20 2a 70 hen increment *p
1a72b 6e 42 79 74 65 20 62 79 20 74 68 65 20 61 6d 6f nByte by the amo
1a72c 75 6e 74 20 6f 66 20 74 68 65 20 72 65 71 75 65 unt of the reque
1a72d 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f st..*/.static vo
1a72e 69 64 20 61 6c 6c 6f 63 53 70 61 63 65 28 0a 20 id allocSpace(.
1a72f 20 63 68 61 72 20 2a 70 70 2c 20 20 20 20 20 20 char *pp,
1a730 20 20 20 20 20 20 2f 2a 20 49 4e 2f 4f 55 54 3a /* IN/OUT:
1a731 20 53 65 74 20 2a 70 70 20 74 6f 20 70 6f 69 6e Set *pp to poin
1a732 74 20 74 6f 20 61 6c 6c 6f 63 61 74 65 64 20 62 t to allocated b
1a733 75 66 66 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e uffer */. int n
1a734 42 79 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 Byte,
1a735 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 /* Number of byt
1a736 65 73 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 2a es to allocate *
1a737 2f 0a 20 20 75 38 20 2a 2a 70 70 46 72 6f 6d 2c /. u8 **ppFrom,
1a738 20 20 20 20 20 20 20 20 20 2f 2a 20 49 4e 2f 4f /* IN/O
1a739 55 54 3a 20 41 6c 6c 6f 63 61 74 65 20 66 72 6f UT: Allocate fro
1a73a 6d 20 2a 70 70 46 72 6f 6d 20 2a 2f 0a 20 20 75 m *ppFrom */. u
1a73b 38 20 2a 70 45 6e 64 2c 20 20 20 20 20 20 20 20 8 *pEnd,
1a73c 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 /* Pointer t
1a73d 6f 20 31 20 62 79 74 65 20 70 61 73 74 20 74 68 o 1 byte past th
1a73e 65 20 65 6e 64 20 6f 66 20 2a 70 70 46 72 6f 6d e end of *ppFrom
1a73f 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 69 6e 74 buffer */. int
1a740 20 2a 70 6e 42 79 74 65 20 20 20 20 20 20 20 20 *pnByte
1a741 20 20 2f 2a 20 49 66 20 61 6c 6c 6f 63 61 74 69 /* If allocati
1a742 6f 6e 20 63 61 6e 6e 6f 74 20 62 65 20 6d 61 64 on cannot be mad
1a743 65 2c 20 69 6e 63 72 65 6d 65 6e 74 20 2a 70 6e e, increment *pn
1a744 42 79 74 65 20 2a 2f 0a 29 7b 0a 20 20 61 73 73 Byte */.){. ass
1a745 65 72 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f ert( EIGHT_BYTE_
1a746 41 4c 49 47 4e 4d 45 4e 54 28 2a 70 70 46 72 6f ALIGNMENT(*ppFro
1a747 6d 29 20 29 3b 0a 20 20 69 66 28 20 28 2a 28 76 m) );. if( (*(v
1a748 6f 69 64 2a 2a 29 70 70 29 3d 3d 30 20 29 7b 0a oid**)pp)==0 ){.
1a749 20 20 20 20 6e 42 79 74 65 20 3d 20 52 4f 55 4e nByte = ROUN
1a74a 44 38 28 6e 42 79 74 65 29 3b 0a 20 20 20 20 69 D8(nByte);. i
1a74b 66 28 20 26 28 2a 70 70 46 72 6f 6d 29 5b 6e 42 f( &(*ppFrom)[nB
1a74c 79 74 65 5d 20 3c 3d 20 70 45 6e 64 20 29 7b 0a yte] <= pEnd ){.
1a74d 20 20 20 20 20 20 2a 28 76 6f 69 64 2a 2a 29 70 *(void**)p
1a74e 70 20 3d 20 28 76 6f 69 64 20 2a 29 2a 70 70 46 p = (void *)*ppF
1a74f 72 6f 6d 3b 0a 20 20 20 20 20 20 2a 70 70 46 72 rom;. *ppFr
1a750 6f 6d 20 2b 3d 20 6e 42 79 74 65 3b 0a 20 20 20 om += nByte;.
1a751 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2a 70 }else{. *p
1a752 6e 42 79 74 65 20 2b 3d 20 6e 42 79 74 65 3b 0a nByte += nByte;.
1a753 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a }. }.}../*.
1a754 2a 2a 20 50 72 65 70 61 72 65 20 61 20 76 69 72 ** Prepare a vir
1a755 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 66 6f 72 tual machine for
1a756 20 65 78 65 63 75 74 69 6f 6e 2e 20 20 54 68 69 execution. Thi
1a757 73 20 69 6e 76 6f 6c 76 65 73 20 74 68 69 6e 67 s involves thing
1a758 73 20 73 75 63 68 0a 2a 2a 20 61 73 20 61 6c 6c s such.** as all
1a759 6f 63 61 74 69 6e 67 20 73 74 61 63 6b 20 73 70 ocating stack sp
1a75a 61 63 65 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 ace and initiali
1a75b 7a 69 6e 67 20 74 68 65 20 70 72 6f 67 72 61 6d zing the program
1a75c 20 63 6f 75 6e 74 65 72 2e 0a 2a 2a 20 41 66 74 counter..** Aft
1a75d 65 72 20 74 68 65 20 56 44 42 45 20 68 61 73 20 er the VDBE has
1a75e 62 65 20 70 72 65 70 70 65 64 2c 20 69 74 20 63 be prepped, it c
1a75f 61 6e 20 62 65 20 65 78 65 63 75 74 65 64 20 62 an be executed b
1a760 79 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 0a 2a 2a y one or more.**
1a761 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 calls to sqlite
1a762 33 56 64 62 65 45 78 65 63 28 29 2e 20 20 0a 2a 3VdbeExec(). .*
1a763 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65 *.** This is the
1a764 20 6f 6e 6c 79 20 77 61 79 20 74 6f 20 6d 6f 76 only way to mov
1a765 65 20 61 20 56 44 42 45 20 66 72 6f 6d 20 56 44 e a VDBE from VD
1a766 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 74 6f BE_MAGIC_INIT to
1a767 0a 2a 2a 20 56 44 42 45 5f 4d 41 47 49 43 5f 52 .** VDBE_MAGIC_R
1a768 55 4e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 UN..**.** This f
1a769 75 6e 63 74 69 6f 6e 20 6d 61 79 20 62 65 20 63 unction may be c
1a76a 61 6c 6c 65 64 20 6d 6f 72 65 20 74 68 61 6e 20 alled more than
1a76b 6f 6e 63 65 20 6f 6e 20 61 20 73 69 6e 67 6c 65 once on a single
1a76c 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 virtual machine
1a76d 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 63 ..** The first c
1a76e 61 6c 6c 20 69 73 20 6d 61 64 65 20 77 68 69 6c all is made whil
1a76f 65 20 63 6f 6d 70 69 6c 69 6e 67 20 74 68 65 20 e compiling the
1a770 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 53 SQL statement. S
1a771 75 62 73 65 71 75 65 6e 74 0a 2a 2a 20 63 61 6c ubsequent.** cal
1a772 6c 73 20 61 72 65 20 6d 61 64 65 20 61 73 20 70 ls are made as p
1a773 61 72 74 20 6f 66 20 74 68 65 20 70 72 6f 63 65 art of the proce
1a774 73 73 20 6f 66 20 72 65 73 65 74 74 69 6e 67 20 ss of resetting
1a775 61 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 62 a statement to b
1a776 65 0a 2a 2a 20 72 65 2d 65 78 65 63 75 74 65 64 e.** re-executed
1a777 20 28 66 72 6f 6d 20 61 20 63 61 6c 6c 20 74 6f (from a call to
1a778 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 sqlite3_reset()
1a779 29 2e 20 54 68 65 20 6e 56 61 72 2c 20 6e 4d 65 ). The nVar, nMe
1a77a 6d 2c 20 6e 43 75 72 73 6f 72 20 0a 2a 2a 20 61 m, nCursor .** a
1a77b 6e 64 20 69 73 45 78 70 6c 61 69 6e 20 70 61 72 nd isExplain par
1a77c 61 6d 65 74 65 72 73 20 61 72 65 20 6f 6e 6c 79 ameters are only
1a77d 20 70 61 73 73 65 64 20 63 6f 72 72 65 63 74 20 passed correct
1a77e 76 61 6c 75 65 73 20 74 68 65 20 66 69 72 73 74 values the first
1a77f 20 74 69 6d 65 0a 2a 2a 20 74 68 65 20 66 75 6e time.** the fun
1a780 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2e ction is called.
1a781 20 4f 6e 20 73 75 62 73 65 71 75 65 6e 74 20 63 On subsequent c
1a782 61 6c 6c 73 2c 20 66 72 6f 6d 20 73 71 6c 69 74 alls, from sqlit
1a783 65 33 5f 72 65 73 65 74 28 29 2c 20 6e 56 61 72 e3_reset(), nVar
1a784 0a 2a 2a 20 69 73 20 70 61 73 73 65 64 20 2d 31 .** is passed -1
1a785 20 61 6e 64 20 6e 4d 65 6d 2c 20 6e 43 75 72 73 and nMem, nCurs
1a786 6f 72 20 61 6e 64 20 69 73 45 78 70 6c 61 69 6e or and isExplain
1a787 20 61 72 65 20 61 6c 6c 20 70 61 73 73 65 64 20 are all passed
1a788 7a 65 72 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f zero..*/.SQLITE_
1a789 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
1a78a 69 74 65 33 56 64 62 65 4d 61 6b 65 52 65 61 64 ite3VdbeMakeRead
1a78b 79 28 0a 20 20 56 64 62 65 20 2a 70 2c 20 20 20 y(. Vdbe *p,
1a78c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a78d 20 20 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20 /* The VDBE
1a78e 2a 2f 0a 20 20 69 6e 74 20 6e 56 61 72 2c 20 20 */. int nVar,
1a78f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a790 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
1a791 20 27 3f 27 20 73 65 65 20 69 6e 20 74 68 65 20 '?' see in the
1a792 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f SQL statement */
1a793 0a 20 20 69 6e 74 20 6e 4d 65 6d 2c 20 20 20 20 . int nMem,
1a794 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a795 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d /* Number of m
1a796 65 6d 6f 72 79 20 63 65 6c 6c 73 20 74 6f 20 61 emory cells to a
1a797 6c 6c 6f 63 61 74 65 20 2a 2f 0a 20 20 69 6e 74 llocate */. int
1a798 20 6e 43 75 72 73 6f 72 2c 20 20 20 20 20 20 20 nCursor,
1a799 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
1a79a 75 6d 62 65 72 20 6f 66 20 63 75 72 73 6f 72 73 umber of cursors
1a79b 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 2a 2f 0a to allocate */.
1a79c 20 20 69 6e 74 20 6e 41 72 67 2c 20 20 20 20 20 int nArg,
1a79d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a79e 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6e 75 6d 62 /* Maximum numb
1a79f 65 72 20 6f 66 20 61 72 67 73 20 69 6e 20 53 75 er of args in Su
1a7a0 62 50 72 6f 67 72 61 6d 73 20 2a 2f 0a 20 20 69 bPrograms */. i
1a7a1 6e 74 20 69 73 45 78 70 6c 61 69 6e 2c 20 20 20 nt isExplain,
1a7a2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1a7a3 20 54 72 75 65 20 69 66 20 74 68 65 20 45 58 50 True if the EXP
1a7a4 4c 41 49 4e 20 6b 65 79 77 6f 72 64 73 20 69 73 LAIN keywords is
1a7a5 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 69 6e present */. in
1a7a6 74 20 75 73 65 73 53 74 6d 74 4a 6f 75 72 6e 61 t usesStmtJourna
1a7a7 6c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 l /*
1a7a8 54 72 75 65 20 74 6f 20 73 65 74 20 56 64 62 65 True to set Vdbe
1a7a9 2e 75 73 65 73 53 74 6d 74 4a 6f 75 72 6e 61 6c .usesStmtJournal
1a7aa 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a */.){. int n;.
1a7ab 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 sqlite3 *db =
1a7ac 70 2d 3e 64 62 3b 0a 0a 20 20 61 73 73 65 72 74 p->db;.. assert
1a7ad 28 20 70 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 ( p!=0 );. asse
1a7ae 72 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 rt( p->magic==VD
1a7af 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b BE_MAGIC_INIT );
1a7b0 0a 0a 20 20 2f 2a 20 54 68 65 72 65 20 73 68 6f .. /* There sho
1a7b1 75 6c 64 20 62 65 20 61 74 20 6c 65 61 73 74 20 uld be at least
1a7b2 6f 6e 65 20 6f 70 63 6f 64 65 2e 0a 20 20 2a 2f one opcode.. */
1a7b3 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 4f . assert( p->nO
1a7b4 70 3e 30 20 29 3b 0a 0a 20 20 2f 2a 20 53 65 74 p>0 );.. /* Set
1a7b5 20 74 68 65 20 6d 61 67 69 63 20 74 6f 20 56 44 the magic to VD
1a7b6 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 73 6f 6f BE_MAGIC_RUN soo
1a7b7 6e 65 72 20 72 61 74 68 65 72 20 74 68 61 6e 20 ner rather than
1a7b8 6c 61 74 65 72 2e 20 2a 2f 0a 20 20 70 2d 3e 6d later. */. p->m
1a7b9 61 67 69 63 20 3d 20 56 44 42 45 5f 4d 41 47 49 agic = VDBE_MAGI
1a7ba 43 5f 52 55 4e 3b 0a 0a 20 20 2f 2a 20 46 6f 72 C_RUN;.. /* For
1a7bb 20 65 61 63 68 20 63 75 72 73 6f 72 20 72 65 71 each cursor req
1a7bc 75 69 72 65 64 2c 20 61 6c 73 6f 20 61 6c 6c 6f uired, also allo
1a7bd 63 61 74 65 20 61 20 6d 65 6d 6f 72 79 20 63 65 cate a memory ce
1a7be 6c 6c 2e 20 4d 65 6d 6f 72 79 0a 20 20 2a 2a 20 ll. Memory. **
1a7bf 63 65 6c 6c 73 20 28 6e 4d 65 6d 2b 31 2d 6e 43 cells (nMem+1-nC
1a7c0 75 72 73 6f 72 29 2e 2e 6e 4d 65 6d 2c 20 69 6e ursor)..nMem, in
1a7c1 63 6c 75 73 69 76 65 2c 20 77 69 6c 6c 20 6e 65 clusive, will ne
1a7c2 76 65 72 20 62 65 20 75 73 65 64 20 62 79 0a 20 ver be used by.
1a7c3 20 2a 2a 20 74 68 65 20 76 64 62 65 20 70 72 6f ** the vdbe pro
1a7c4 67 72 61 6d 2e 20 49 6e 73 74 65 61 64 20 74 68 gram. Instead th
1a7c5 65 79 20 61 72 65 20 75 73 65 64 20 74 6f 20 61 ey are used to a
1a7c6 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 20 66 6f llocate space fo
1a7c7 72 0a 20 20 2a 2a 20 56 64 62 65 43 75 72 73 6f r. ** VdbeCurso
1a7c8 72 2f 42 74 43 75 72 73 6f 72 20 73 74 72 75 63 r/BtCursor struc
1a7c9 74 75 72 65 73 2e 20 54 68 65 20 62 6c 6f 62 20 tures. The blob
1a7ca 6f 66 20 6d 65 6d 6f 72 79 20 61 73 73 6f 63 69 of memory associ
1a7cb 61 74 65 64 20 77 69 74 68 20 0a 20 20 2a 2a 20 ated with . **
1a7cc 63 75 72 73 6f 72 20 30 20 69 73 20 73 74 6f 72 cursor 0 is stor
1a7cd 65 64 20 69 6e 20 6d 65 6d 6f 72 79 20 63 65 6c ed in memory cel
1a7ce 6c 20 6e 4d 65 6d 2e 20 4d 65 6d 6f 72 79 20 63 l nMem. Memory c
1a7cf 65 6c 6c 20 28 6e 4d 65 6d 2d 31 29 0a 20 20 2a ell (nMem-1). *
1a7d0 2a 20 73 74 6f 72 65 73 20 74 68 65 20 62 6c 6f * stores the blo
1a7d1 62 20 6f 66 20 6d 65 6d 6f 72 79 20 61 73 73 6f b of memory asso
1a7d2 63 69 61 74 65 64 20 77 69 74 68 20 63 75 72 73 ciated with curs
1a7d3 6f 72 20 31 2c 20 65 74 63 2e 0a 20 20 2a 2a 0a or 1, etc.. **.
1a7d4 20 20 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 61 ** See also: a
1a7d5 6c 6c 6f 63 61 74 65 43 75 72 73 6f 72 28 29 2e llocateCursor().
1a7d6 0a 20 20 2a 2f 0a 20 20 6e 4d 65 6d 20 2b 3d 20 . */. nMem +=
1a7d7 6e 43 75 72 73 6f 72 3b 0a 0a 20 20 2f 2a 20 41 nCursor;.. /* A
1a7d8 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 20 66 6f llocate space fo
1a7d9 72 20 6d 65 6d 6f 72 79 20 72 65 67 69 73 74 65 r memory registe
1a7da 72 73 2c 20 53 51 4c 20 76 61 72 69 61 62 6c 65 rs, SQL variable
1a7db 73 2c 20 56 44 42 45 20 63 75 72 73 6f 72 73 20 s, VDBE cursors
1a7dc 61 6e 64 20 0a 20 20 2a 2a 20 61 6e 20 61 72 72 and . ** an arr
1a7dd 61 79 20 74 6f 20 6d 61 72 73 68 61 6c 20 53 51 ay to marshal SQ
1a7de 4c 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d L function argum
1a7df 65 6e 74 73 20 69 6e 2e 20 54 68 69 73 20 69 73 ents in. This is
1a7e0 20 6f 6e 6c 79 20 64 6f 6e 65 20 74 68 65 0a 20 only done the.
1a7e1 20 2a 2a 20 66 69 72 73 74 20 74 69 6d 65 20 74 ** first time t
1a7e2 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
1a7e3 63 61 6c 6c 65 64 20 66 6f 72 20 61 20 67 69 76 called for a giv
1a7e4 65 6e 20 56 44 42 45 2c 20 6e 6f 74 20 77 68 65 en VDBE, not whe
1a7e5 6e 20 69 74 20 69 73 0a 20 20 2a 2a 20 62 65 69 n it is. ** bei
1a7e6 6e 67 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 73 ng called from s
1a7e7 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 20 74 qlite3_reset() t
1a7e8 6f 20 72 65 73 65 74 20 74 68 65 20 76 69 72 74 o reset the virt
1a7e9 75 61 6c 20 6d 61 63 68 69 6e 65 2e 0a 20 20 2a ual machine.. *
1a7ea 2f 0a 20 20 69 66 28 20 6e 56 61 72 3e 3d 30 20 /. if( nVar>=0
1a7eb 26 26 20 41 4c 57 41 59 53 28 64 62 2d 3e 6d 61 && ALWAYS(db->ma
1a7ec 6c 6c 6f 63 46 61 69 6c 65 64 3d 3d 30 29 20 29 llocFailed==0) )
1a7ed 7b 0a 20 20 20 20 75 38 20 2a 7a 43 73 72 20 3d {. u8 *zCsr =
1a7ee 20 28 75 38 20 2a 29 26 70 2d 3e 61 4f 70 5b 70 (u8 *)&p->aOp[p
1a7ef 2d 3e 6e 4f 70 5d 3b 0a 20 20 20 20 75 38 20 2a ->nOp];. u8 *
1a7f0 7a 45 6e 64 20 3d 20 28 75 38 20 2a 29 26 70 2d zEnd = (u8 *)&p-
1a7f1 3e 61 4f 70 5b 70 2d 3e 6e 4f 70 41 6c 6c 6f 63 >aOp[p->nOpAlloc
1a7f2 5d 3b 0a 20 20 20 20 69 6e 74 20 6e 42 79 74 65 ];. int nByte
1a7f3 3b 0a 20 20 20 20 72 65 73 6f 6c 76 65 50 32 56 ;. resolveP2V
1a7f4 61 6c 75 65 73 28 70 2c 20 26 6e 41 72 67 29 3b alues(p, &nArg);
1a7f5 0a 20 20 20 20 70 2d 3e 75 73 65 73 53 74 6d 74 . p->usesStmt
1a7f6 4a 6f 75 72 6e 61 6c 20 3d 20 28 75 38 29 75 73 Journal = (u8)us
1a7f7 65 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 3b 0a 20 esStmtJournal;.
1a7f8 20 20 20 69 66 28 20 69 73 45 78 70 6c 61 69 6e if( isExplain
1a7f9 20 26 26 20 6e 4d 65 6d 3c 31 30 20 29 7b 0a 20 && nMem<10 ){.
1a7fa 20 20 20 20 20 6e 4d 65 6d 20 3d 20 31 30 3b 0a nMem = 10;.
1a7fb 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 73 65 74 }. memset
1a7fc 28 7a 43 73 72 2c 20 30 2c 20 7a 45 6e 64 2d 7a (zCsr, 0, zEnd-z
1a7fd 43 73 72 29 3b 0a 20 20 20 20 7a 43 73 72 20 2b Csr);. zCsr +
1a7fe 3d 20 28 7a 43 73 72 20 2d 20 28 75 38 2a 29 30 = (zCsr - (u8*)0
1a7ff 29 26 37 3b 0a 20 20 20 20 61 73 73 65 72 74 28 )&7;. assert(
1a800 20 45 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 EIGHT_BYTE_ALIG
1a801 4e 4d 45 4e 54 28 7a 43 73 72 29 20 29 3b 0a 0a NMENT(zCsr) );..
1a802 20 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 20 6e do {. n
1a803 42 79 74 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 Byte = 0;.
1a804 61 6c 6c 6f 63 53 70 61 63 65 28 28 63 68 61 72 allocSpace((char
1a805 2a 29 26 70 2d 3e 61 4d 65 6d 2c 20 6e 4d 65 6d *)&p->aMem, nMem
1a806 2a 73 69 7a 65 6f 66 28 4d 65 6d 29 2c 20 26 7a *sizeof(Mem), &z
1a807 43 73 72 2c 20 7a 45 6e 64 2c 20 26 6e 42 79 74 Csr, zEnd, &nByt
1a808 65 29 3b 0a 20 20 20 20 20 20 61 6c 6c 6f 63 53 e);. allocS
1a809 70 61 63 65 28 28 63 68 61 72 2a 29 26 70 2d 3e pace((char*)&p->
1a80a 61 56 61 72 2c 20 6e 56 61 72 2a 73 69 7a 65 6f aVar, nVar*sizeo
1a80b 66 28 4d 65 6d 29 2c 20 26 7a 43 73 72 2c 20 7a f(Mem), &zCsr, z
1a80c 45 6e 64 2c 20 26 6e 42 79 74 65 29 3b 0a 20 20 End, &nByte);.
1a80d 20 20 20 20 61 6c 6c 6f 63 53 70 61 63 65 28 28 allocSpace((
1a80e 63 68 61 72 2a 29 26 70 2d 3e 61 70 41 72 67 2c char*)&p->apArg,
1a80f 20 6e 41 72 67 2a 73 69 7a 65 6f 66 28 4d 65 6d nArg*sizeof(Mem
1a810 2a 29 2c 20 26 7a 43 73 72 2c 20 7a 45 6e 64 2c *), &zCsr, zEnd,
1a811 20 26 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 20 &nByte);.
1a812 61 6c 6c 6f 63 53 70 61 63 65 28 28 63 68 61 72 allocSpace((char
1a813 2a 29 26 70 2d 3e 61 7a 56 61 72 2c 20 6e 56 61 *)&p->azVar, nVa
1a814 72 2a 73 69 7a 65 6f 66 28 63 68 61 72 2a 29 2c r*sizeof(char*),
1a815 20 26 7a 43 73 72 2c 20 7a 45 6e 64 2c 20 26 6e &zCsr, zEnd, &n
1a816 42 79 74 65 29 3b 0a 20 20 20 20 20 20 61 6c 6c Byte);. all
1a817 6f 63 53 70 61 63 65 28 28 63 68 61 72 2a 29 26 ocSpace((char*)&
1a818 70 2d 3e 61 70 43 73 72 2c 20 0a 20 20 20 20 20 p->apCsr, .
1a819 20 20 20 20 20 20 20 20 20 20 20 20 6e 43 75 72 nCur
1a81a 73 6f 72 2a 73 69 7a 65 6f 66 28 56 64 62 65 43 sor*sizeof(VdbeC
1a81b 75 72 73 6f 72 2a 29 2c 20 26 7a 43 73 72 2c 20 ursor*), &zCsr,
1a81c 7a 45 6e 64 2c 20 26 6e 42 79 74 65 0a 20 20 20 zEnd, &nByte.
1a81d 20 20 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 );. if(
1a81e 6e 42 79 74 65 20 29 7b 0a 20 20 20 20 20 20 20 nByte ){.
1a81f 20 70 2d 3e 70 46 72 65 65 20 3d 20 73 71 6c 69 p->pFree = sqli
1a820 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 te3DbMallocZero(
1a821 64 62 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 db, nByte);.
1a822 20 20 7d 0a 20 20 20 20 20 20 7a 43 73 72 20 3d }. zCsr =
1a823 20 70 2d 3e 70 46 72 65 65 3b 0a 20 20 20 20 20 p->pFree;.
1a824 20 7a 45 6e 64 20 3d 20 26 7a 43 73 72 5b 6e 42 zEnd = &zCsr[nB
1a825 79 74 65 5d 3b 0a 20 20 20 20 7d 77 68 69 6c 65 yte];. }while
1a826 28 20 6e 42 79 74 65 20 26 26 20 21 64 62 2d 3e ( nByte && !db->
1a827 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a mallocFailed );.
1a828 0a 20 20 20 20 70 2d 3e 6e 43 75 72 73 6f 72 20 . p->nCursor
1a829 3d 20 28 75 31 36 29 6e 43 75 72 73 6f 72 3b 0a = (u16)nCursor;.
1a82a 20 20 20 20 69 66 28 20 70 2d 3e 61 56 61 72 20 if( p->aVar
1a82b 29 7b 0a 20 20 20 20 20 20 70 2d 3e 6e 56 61 72 ){. p->nVar
1a82c 20 3d 20 28 75 31 36 29 6e 56 61 72 3b 0a 20 20 = (u16)nVar;.
1a82d 20 20 20 20 66 6f 72 28 6e 3d 30 3b 20 6e 3c 6e for(n=0; n<n
1a82e 56 61 72 3b 20 6e 2b 2b 29 7b 0a 20 20 20 20 20 Var; n++){.
1a82f 20 20 20 70 2d 3e 61 56 61 72 5b 6e 5d 2e 66 6c p->aVar[n].fl
1a830 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a ags = MEM_Null;.
1a831 20 20 20 20 20 20 20 20 70 2d 3e 61 56 61 72 5b p->aVar[
1a832 6e 5d 2e 64 62 20 3d 20 64 62 3b 0a 20 20 20 20 n].db = db;.
1a833 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 }. }. if
1a834 28 20 70 2d 3e 61 4d 65 6d 20 29 7b 0a 20 20 20 ( p->aMem ){.
1a835 20 20 20 70 2d 3e 61 4d 65 6d 2d 2d 3b 20 20 20 p->aMem--;
1a836 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a837 20 20 20 2f 2a 20 61 4d 65 6d 5b 5d 20 67 6f 65 /* aMem[] goe
1a838 73 20 66 72 6f 6d 20 31 2e 2e 6e 4d 65 6d 20 2a s from 1..nMem *
1a839 2f 0a 20 20 20 20 20 20 70 2d 3e 6e 4d 65 6d 20 /. p->nMem
1a83a 3d 20 6e 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 = nMem;
1a83b 20 20 20 20 20 20 20 20 2f 2a 20 20 20 20 20 20 /*
1a83c 20 6e 6f 74 20 66 72 6f 6d 20 30 2e 2e 6e 4d 65 not from 0..nMe
1a83d 6d 2d 31 20 2a 2f 0a 20 20 20 20 20 20 66 6f 72 m-1 */. for
1a83e 28 6e 3d 31 3b 20 6e 3c 3d 6e 4d 65 6d 3b 20 6e (n=1; n<=nMem; n
1a83f 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e ++){. p->
1a840 61 4d 65 6d 5b 6e 5d 2e 66 6c 61 67 73 20 3d 20 aMem[n].flags =
1a841 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 20 20 MEM_Null;.
1a842 20 20 70 2d 3e 61 4d 65 6d 5b 6e 5d 2e 64 62 20 p->aMem[n].db
1a843 3d 20 64 62 3b 0a 20 20 20 20 20 20 7d 0a 20 20 = db;. }.
1a844 20 20 7d 0a 20 20 7d 0a 23 69 66 64 65 66 20 53 }. }.#ifdef S
1a845 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 66 6f QLITE_DEBUG. fo
1a846 72 28 6e 3d 31 3b 20 6e 3c 70 2d 3e 6e 4d 65 6d r(n=1; n<p->nMem
1a847 3b 20 6e 2b 2b 29 7b 0a 20 20 20 20 61 73 73 65 ; n++){. asse
1a848 72 74 28 20 70 2d 3e 61 4d 65 6d 5b 6e 5d 2e 64 rt( p->aMem[n].d
1a849 62 3d 3d 64 62 20 29 3b 0a 20 20 7d 0a 23 65 6e b==db );. }.#en
1a84a 64 69 66 0a 0a 20 20 70 2d 3e 70 63 20 3d 20 2d dif.. p->pc = -
1a84b 31 3b 0a 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c 1;. p->rc = SQL
1a84c 49 54 45 5f 4f 4b 3b 0a 20 20 70 2d 3e 65 72 72 ITE_OK;. p->err
1a84d 6f 72 41 63 74 69 6f 6e 20 3d 20 4f 45 5f 41 62 orAction = OE_Ab
1a84e 6f 72 74 3b 0a 20 20 70 2d 3e 65 78 70 6c 61 69 ort;. p->explai
1a84f 6e 20 7c 3d 20 69 73 45 78 70 6c 61 69 6e 3b 0a n |= isExplain;.
1a850 20 20 70 2d 3e 6d 61 67 69 63 20 3d 20 56 44 42 p->magic = VDB
1a851 45 5f 4d 41 47 49 43 5f 52 55 4e 3b 0a 20 20 70 E_MAGIC_RUN;. p
1a852 2d 3e 6e 43 68 61 6e 67 65 20 3d 20 30 3b 0a 20 ->nChange = 0;.
1a853 20 70 2d 3e 63 61 63 68 65 43 74 72 20 3d 20 31 p->cacheCtr = 1
1a854 3b 0a 20 20 70 2d 3e 6d 69 6e 57 72 69 74 65 46 ;. p->minWriteF
1a855 69 6c 65 46 6f 72 6d 61 74 20 3d 20 32 35 35 3b ileFormat = 255;
1a856 0a 20 20 70 2d 3e 69 53 74 61 74 65 6d 65 6e 74 . p->iStatement
1a857 20 3d 20 30 3b 0a 23 69 66 64 65 66 20 56 44 42 = 0;.#ifdef VDB
1a858 45 5f 50 52 4f 46 49 4c 45 0a 20 20 7b 0a 20 20 E_PROFILE. {.
1a859 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 int i;. for
1a85a 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4f 70 3b 20 (i=0; i<p->nOp;
1a85b 69 2b 2b 29 7b 0a 20 20 20 20 20 20 70 2d 3e 61 i++){. p->a
1a85c 4f 70 5b 69 5d 2e 63 6e 74 20 3d 20 30 3b 0a 20 Op[i].cnt = 0;.
1a85d 20 20 20 20 20 70 2d 3e 61 4f 70 5b 69 5d 2e 63 p->aOp[i].c
1a85e 79 63 6c 65 73 20 3d 20 30 3b 0a 20 20 20 20 7d ycles = 0;. }
1a85f 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f . }.#endif.}../
1a860 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 56 44 42 *.** Close a VDB
1a861 45 20 63 75 72 73 6f 72 20 61 6e 64 20 72 65 6c E cursor and rel
1a862 65 61 73 65 20 61 6c 6c 20 74 68 65 20 72 65 73 ease all the res
1a863 6f 75 72 63 65 73 20 74 68 61 74 20 63 75 72 73 ources that curs
1a864 6f 72 20 0a 2a 2a 20 68 61 70 70 65 6e 73 20 74 or .** happens t
1a865 6f 20 68 6f 6c 64 2e 0a 2a 2f 0a 53 51 4c 49 54 o hold..*/.SQLIT
1a866 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
1a867 71 6c 69 74 65 33 56 64 62 65 46 72 65 65 43 75 qlite3VdbeFreeCu
1a868 72 73 6f 72 28 56 64 62 65 20 2a 70 2c 20 56 64 rsor(Vdbe *p, Vd
1a869 62 65 43 75 72 73 6f 72 20 2a 70 43 78 29 7b 0a beCursor *pCx){.
1a86a 20 20 69 66 28 20 70 43 78 3d 3d 30 20 29 7b 0a if( pCx==0 ){.
1a86b 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a return;. }.
1a86c 20 20 69 66 28 20 70 43 78 2d 3e 70 42 74 20 29 if( pCx->pBt )
1a86d 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 {. sqlite3Btr
1a86e 65 65 43 6c 6f 73 65 28 70 43 78 2d 3e 70 42 74 eeClose(pCx->pBt
1a86f 29 3b 0a 20 20 20 20 2f 2a 20 54 68 65 20 70 43 );. /* The pC
1a870 78 2d 3e 70 43 75 72 73 6f 72 20 77 69 6c 6c 20 x->pCursor will
1a871 62 65 20 63 6c 6f 73 65 20 61 75 74 6f 6d 61 74 be close automat
1a872 69 63 61 6c 6c 79 2c 20 69 66 20 69 74 20 65 78 ically, if it ex
1a873 69 73 74 73 2c 20 62 79 0a 20 20 20 20 2a 2a 20 ists, by. **
1a874 74 68 65 20 63 61 6c 6c 20 61 62 6f 76 65 2e 20 the call above.
1a875 2a 2f 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 */. }else if( p
1a876 43 78 2d 3e 70 43 75 72 73 6f 72 20 29 7b 0a 20 Cx->pCursor ){.
1a877 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 sqlite3BtreeC
1a878 6c 6f 73 65 43 75 72 73 6f 72 28 70 43 78 2d 3e loseCursor(pCx->
1a879 70 43 75 72 73 6f 72 29 3b 0a 20 20 7d 0a 23 69 pCursor);. }.#i
1a87a 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
1a87b 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 T_VIRTUALTABLE.
1a87c 20 69 66 28 20 70 43 78 2d 3e 70 56 74 61 62 43 if( pCx->pVtabC
1a87d 75 72 73 6f 72 20 29 7b 0a 20 20 20 20 73 71 6c ursor ){. sql
1a87e 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 ite3_vtab_cursor
1a87f 20 2a 70 56 74 61 62 43 75 72 73 6f 72 20 3d 20 *pVtabCursor =
1a880 70 43 78 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 pCx->pVtabCursor
1a881 3b 0a 20 20 20 20 63 6f 6e 73 74 20 73 71 6c 69 ;. const sqli
1a882 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 te3_module *pMod
1a883 75 6c 65 20 3d 20 70 43 78 2d 3e 70 4d 6f 64 75 ule = pCx->pModu
1a884 6c 65 3b 0a 20 20 20 20 70 2d 3e 69 6e 56 74 61 le;. p->inVta
1a885 62 4d 65 74 68 6f 64 20 3d 20 31 3b 0a 20 20 20 bMethod = 1;.
1a886 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 (void)sqlite3Sa
1a887 66 65 74 79 4f 66 66 28 70 2d 3e 64 62 29 3b 0a fetyOff(p->db);.
1a888 20 20 20 20 70 4d 6f 64 75 6c 65 2d 3e 78 43 6c pModule->xCl
1a889 6f 73 65 28 70 56 74 61 62 43 75 72 73 6f 72 29 ose(pVtabCursor)
1a88a 3b 0a 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 ;. (void)sqli
1a88b 74 65 33 53 61 66 65 74 79 4f 6e 28 70 2d 3e 64 te3SafetyOn(p->d
1a88c 62 29 3b 0a 20 20 20 20 70 2d 3e 69 6e 56 74 61 b);. p->inVta
1a88d 62 4d 65 74 68 6f 64 20 3d 20 30 3b 0a 20 20 7d bMethod = 0;. }
1a88e 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a .#endif.}../*.**
1a88f 20 43 6f 70 79 20 74 68 65 20 76 61 6c 75 65 73 Copy the values
1a890 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 56 stored in the V
1a891 64 62 65 46 72 61 6d 65 20 73 74 72 75 63 74 75 dbeFrame structu
1a892 72 65 20 74 6f 20 69 74 73 20 56 64 62 65 2e 20 re to its Vdbe.
1a893 54 68 69 73 0a 2a 2a 20 69 73 20 75 73 65 64 2c This.** is used,
1a894 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 77 68 for example, wh
1a895 65 6e 20 61 20 74 72 69 67 67 65 72 20 73 75 62 en a trigger sub
1a896 2d 70 72 6f 67 72 61 6d 20 69 73 20 68 61 6c 74 -program is halt
1a897 65 64 20 74 6f 20 72 65 73 74 6f 72 65 0a 2a 2a ed to restore.**
1a898 20 63 6f 6e 74 72 6f 6c 20 74 6f 20 74 68 65 20 control to the
1a899 6d 61 69 6e 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f main program..*/
1a89a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1a89b 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 46 int sqlite3VdbeF
1a89c 72 61 6d 65 52 65 73 74 6f 72 65 28 56 64 62 65 rameRestore(Vdbe
1a89d 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 29 7b 0a Frame *pFrame){.
1a89e 20 20 56 64 62 65 20 2a 76 20 3d 20 70 46 72 61 Vdbe *v = pFra
1a89f 6d 65 2d 3e 76 3b 0a 20 20 76 2d 3e 61 4f 70 20 me->v;. v->aOp
1a8a0 3d 20 70 46 72 61 6d 65 2d 3e 61 4f 70 3b 0a 20 = pFrame->aOp;.
1a8a1 20 76 2d 3e 6e 4f 70 20 3d 20 70 46 72 61 6d 65 v->nOp = pFrame
1a8a2 2d 3e 6e 4f 70 3b 0a 20 20 76 2d 3e 61 4d 65 6d ->nOp;. v->aMem
1a8a3 20 3d 20 70 46 72 61 6d 65 2d 3e 61 4d 65 6d 3b = pFrame->aMem;
1a8a4 0a 20 20 76 2d 3e 6e 4d 65 6d 20 3d 20 70 46 72 . v->nMem = pFr
1a8a5 61 6d 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 76 2d 3e ame->nMem;. v->
1a8a6 61 70 43 73 72 20 3d 20 70 46 72 61 6d 65 2d 3e apCsr = pFrame->
1a8a7 61 70 43 73 72 3b 0a 20 20 76 2d 3e 6e 43 75 72 apCsr;. v->nCur
1a8a8 73 6f 72 20 3d 20 70 46 72 61 6d 65 2d 3e 6e 43 sor = pFrame->nC
1a8a9 75 72 73 6f 72 3b 0a 20 20 76 2d 3e 64 62 2d 3e ursor;. v->db->
1a8aa 6c 61 73 74 52 6f 77 69 64 20 3d 20 70 46 72 61 lastRowid = pFra
1a8ab 6d 65 2d 3e 6c 61 73 74 52 6f 77 69 64 3b 0a 20 me->lastRowid;.
1a8ac 20 76 2d 3e 6e 43 68 61 6e 67 65 20 3d 20 70 46 v->nChange = pF
1a8ad 72 61 6d 65 2d 3e 6e 43 68 61 6e 67 65 3b 0a 20 rame->nChange;.
1a8ae 20 72 65 74 75 72 6e 20 70 46 72 61 6d 65 2d 3e return pFrame->
1a8af 70 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f pc;.}../*.** Clo
1a8b0 73 65 20 61 6c 6c 20 63 75 72 73 6f 72 73 2e 0a se all cursors..
1a8b1 2a 2a 0a 2a 2a 20 41 6c 73 6f 20 72 65 6c 65 61 **.** Also relea
1a8b2 73 65 20 61 6e 79 20 64 79 6e 61 6d 69 63 20 6d se any dynamic m
1a8b3 65 6d 6f 72 79 20 68 65 6c 64 20 62 79 20 74 68 emory held by th
1a8b4 65 20 56 4d 20 69 6e 20 74 68 65 20 56 64 62 65 e VM in the Vdbe
1a8b5 2e 61 4d 65 6d 20 6d 65 6d 6f 72 79 20 0a 2a 2a .aMem memory .**
1a8b6 20 63 65 6c 6c 20 61 72 72 61 79 2e 20 54 68 69 cell array. Thi
1a8b7 73 20 69 73 20 6e 65 63 65 73 73 61 72 79 20 61 s is necessary a
1a8b8 73 20 74 68 65 20 6d 65 6d 6f 72 79 20 63 65 6c s the memory cel
1a8b9 6c 20 61 72 72 61 79 20 6d 61 79 20 63 6f 6e 74 l array may cont
1a8ba 61 69 6e 0a 2a 2a 20 70 6f 69 6e 74 65 72 73 20 ain.** pointers
1a8bb 74 6f 20 56 64 62 65 46 72 61 6d 65 20 6f 62 6a to VdbeFrame obj
1a8bc 65 63 74 73 2c 20 77 68 69 63 68 20 6d 61 79 20 ects, which may
1a8bd 69 6e 20 74 75 72 6e 20 63 6f 6e 74 61 69 6e 20 in turn contain
1a8be 70 6f 69 6e 74 65 72 73 20 74 6f 0a 2a 2a 20 6f pointers to.** o
1a8bf 70 65 6e 20 63 75 72 73 6f 72 73 2e 0a 2a 2f 0a pen cursors..*/.
1a8c0 73 74 61 74 69 63 20 76 6f 69 64 20 63 6c 6f 73 static void clos
1a8c1 65 41 6c 6c 43 75 72 73 6f 72 73 28 56 64 62 65 eAllCursors(Vdbe
1a8c2 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e 70 *p){. if( p->p
1a8c3 46 72 61 6d 65 20 29 7b 0a 20 20 20 20 56 64 62 Frame ){. Vdb
1a8c4 65 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 20 3d eFrame *pFrame =
1a8c5 20 70 2d 3e 70 46 72 61 6d 65 3b 0a 20 20 20 20 p->pFrame;.
1a8c6 66 6f 72 28 70 46 72 61 6d 65 3d 70 2d 3e 70 46 for(pFrame=p->pF
1a8c7 72 61 6d 65 3b 20 70 46 72 61 6d 65 2d 3e 70 50 rame; pFrame->pP
1a8c8 61 72 65 6e 74 3b 20 70 46 72 61 6d 65 3d 70 46 arent; pFrame=pF
1a8c9 72 61 6d 65 2d 3e 70 50 61 72 65 6e 74 29 3b 0a rame->pParent);.
1a8ca 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 46 sqlite3VdbeF
1a8cb 72 61 6d 65 52 65 73 74 6f 72 65 28 70 46 72 61 rameRestore(pFra
1a8cc 6d 65 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 46 me);. }. p->pF
1a8cd 72 61 6d 65 20 3d 20 30 3b 0a 20 20 70 2d 3e 6e rame = 0;. p->n
1a8ce 46 72 61 6d 65 20 3d 20 30 3b 0a 0a 20 20 69 66 Frame = 0;.. if
1a8cf 28 20 70 2d 3e 61 70 43 73 72 20 29 7b 0a 20 20 ( p->apCsr ){.
1a8d0 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 int i;. for
1a8d1 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 75 72 73 (i=0; i<p->nCurs
1a8d2 6f 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 or; i++){.
1a8d3 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 20 3d VdbeCursor *pC =
1a8d4 20 70 2d 3e 61 70 43 73 72 5b 69 5d 3b 0a 20 20 p->apCsr[i];.
1a8d5 20 20 20 20 69 66 28 20 70 43 20 29 7b 0a 20 20 if( pC ){.
1a8d6 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
1a8d7 65 46 72 65 65 43 75 72 73 6f 72 28 70 2c 20 70 eFreeCursor(p, p
1a8d8 43 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 61 C);. p->a
1a8d9 70 43 73 72 5b 69 5d 20 3d 20 30 3b 0a 20 20 20 pCsr[i] = 0;.
1a8da 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 }. }. }.
1a8db 20 69 66 28 20 70 2d 3e 61 4d 65 6d 20 29 7b 0a if( p->aMem ){.
1a8dc 20 20 20 20 72 65 6c 65 61 73 65 4d 65 6d 41 72 releaseMemAr
1a8dd 72 61 79 28 26 70 2d 3e 61 4d 65 6d 5b 31 5d 2c ray(&p->aMem[1],
1a8de 20 70 2d 3e 6e 4d 65 6d 29 3b 0a 20 20 7d 0a 7d p->nMem);. }.}
1a8df 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 6e 20 75 70 ../*.** Clean up
1a8e0 20 74 68 65 20 56 4d 20 61 66 74 65 72 20 65 78 the VM after ex
1a8e1 65 63 75 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 ecution..**.** T
1a8e2 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c his routine will
1a8e3 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 63 automatically c
1a8e4 6c 6f 73 65 20 61 6e 79 20 63 75 72 73 6f 72 73 lose any cursors
1a8e5 2c 20 6c 69 73 74 73 2c 20 61 6e 64 2f 6f 72 0a , lists, and/or.
1a8e6 2a 2a 20 73 6f 72 74 65 72 73 20 74 68 61 74 20 ** sorters that
1a8e7 77 65 72 65 20 6c 65 66 74 20 6f 70 65 6e 2e 20 were left open.
1a8e8 20 49 74 20 61 6c 73 6f 20 64 65 6c 65 74 65 73 It also deletes
1a8e9 20 74 68 65 20 76 61 6c 75 65 73 20 6f 66 0a 2a the values of.*
1a8ea 2a 20 76 61 72 69 61 62 6c 65 73 20 69 6e 20 74 * variables in t
1a8eb 68 65 20 61 56 61 72 5b 5d 20 61 72 72 61 79 2e he aVar[] array.
1a8ec 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
1a8ed 43 6c 65 61 6e 75 70 28 56 64 62 65 20 2a 70 29 Cleanup(Vdbe *p)
1a8ee 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 {. sqlite3 *db
1a8ef 3d 20 70 2d 3e 64 62 3b 0a 0a 23 69 66 64 65 66 = p->db;..#ifdef
1a8f0 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 SQLITE_DEBUG.
1a8f1 2f 2a 20 45 78 65 63 75 74 65 20 61 73 73 65 72 /* Execute asser
1a8f2 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 74 t() statements t
1a8f3 6f 20 65 6e 73 75 72 65 20 74 68 61 74 20 74 68 o ensure that th
1a8f4 65 20 56 64 62 65 2e 61 70 43 73 72 5b 5d 20 61 e Vdbe.apCsr[] a
1a8f5 6e 64 20 0a 20 20 2a 2a 20 56 64 62 65 2e 61 4d nd . ** Vdbe.aM
1a8f6 65 6d 5b 5d 20 61 72 72 61 79 73 20 68 61 76 65 em[] arrays have
1a8f7 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 63 6c already been cl
1a8f8 65 61 6e 65 64 20 75 70 2e 20 20 2a 2f 0a 20 20 eaned up. */.
1a8f9 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 int i;. for(i=0
1a8fa 3b 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 3b 20 ; i<p->nCursor;
1a8fb 69 2b 2b 29 20 61 73 73 65 72 74 28 20 70 2d 3e i++) assert( p->
1a8fc 61 70 43 73 72 3d 3d 30 20 7c 7c 20 70 2d 3e 61 apCsr==0 || p->a
1a8fd 70 43 73 72 5b 69 5d 3d 3d 30 20 29 3b 0a 20 20 pCsr[i]==0 );.
1a8fe 66 6f 72 28 69 3d 31 3b 20 69 3c 3d 70 2d 3e 6e for(i=1; i<=p->n
1a8ff 4d 65 6d 3b 20 69 2b 2b 29 20 61 73 73 65 72 74 Mem; i++) assert
1a900 28 20 70 2d 3e 61 4d 65 6d 3d 3d 30 20 7c 7c 20 ( p->aMem==0 ||
1a901 70 2d 3e 61 4d 65 6d 5b 69 5d 2e 66 6c 61 67 73 p->aMem[i].flags
1a902 3d 3d 4d 45 4d 5f 4e 75 6c 6c 20 29 3b 0a 23 65 ==MEM_Null );.#e
1a903 6e 64 69 66 0a 0a 20 20 73 71 6c 69 74 65 33 44 ndif.. sqlite3D
1a904 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 bFree(db, p->zEr
1a905 72 4d 73 67 29 3b 0a 20 20 70 2d 3e 7a 45 72 72 rMsg);. p->zErr
1a906 4d 73 67 20 3d 20 30 3b 0a 20 20 70 2d 3e 70 52 Msg = 0;. p->pR
1a907 65 73 75 6c 74 53 65 74 20 3d 20 30 3b 0a 7d 0a esultSet = 0;.}.
1a908 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 6e ./*.** Set the n
1a909 75 6d 62 65 72 20 6f 66 20 72 65 73 75 6c 74 20 umber of result
1a90a 63 6f 6c 75 6d 6e 73 20 74 68 61 74 20 77 69 6c columns that wil
1a90b 6c 20 62 65 20 72 65 74 75 72 6e 65 64 20 62 79 l be returned by
1a90c 20 74 68 69 73 20 53 51 4c 0a 2a 2a 20 73 74 61 this SQL.** sta
1a90d 74 65 6d 65 6e 74 2e 20 54 68 69 73 20 69 73 20 tement. This is
1a90e 6e 6f 77 20 73 65 74 20 61 74 20 63 6f 6d 70 69 now set at compi
1a90f 6c 65 20 74 69 6d 65 2c 20 72 61 74 68 65 72 20 le time, rather
1a910 74 68 61 6e 20 64 75 72 69 6e 67 0a 2a 2a 20 65 than during.** e
1a911 78 65 63 75 74 69 6f 6e 20 6f 66 20 74 68 65 20 xecution of the
1a912 76 64 62 65 20 70 72 6f 67 72 61 6d 20 73 6f 20 vdbe program so
1a913 74 68 61 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c that sqlite3_col
1a914 75 6d 6e 5f 63 6f 75 6e 74 28 29 20 63 61 6e 0a umn_count() can.
1a915 2a 2a 20 62 65 20 63 61 6c 6c 65 64 20 6f 6e 20 ** be called on
1a916 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 an SQL statement
1a917 20 62 65 66 6f 72 65 20 73 71 6c 69 74 65 33 5f before sqlite3_
1a918 73 74 65 70 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 step()..*/.SQLIT
1a919 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
1a91a 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d qlite3VdbeSetNum
1a91b 43 6f 6c 73 28 56 64 62 65 20 2a 70 2c 20 69 6e Cols(Vdbe *p, in
1a91c 74 20 6e 52 65 73 43 6f 6c 75 6d 6e 29 7b 0a 20 t nResColumn){.
1a91d 20 4d 65 6d 20 2a 70 43 6f 6c 4e 61 6d 65 3b 0a Mem *pColName;.
1a91e 20 20 69 6e 74 20 6e 3b 0a 20 20 73 71 6c 69 74 int n;. sqlit
1a91f 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 0a e3 *db = p->db;.
1a920 0a 20 20 72 65 6c 65 61 73 65 4d 65 6d 41 72 72 . releaseMemArr
1a921 61 79 28 70 2d 3e 61 43 6f 6c 4e 61 6d 65 2c 20 ay(p->aColName,
1a922 70 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 2a 43 4f p->nResColumn*CO
1a923 4c 4e 41 4d 45 5f 4e 29 3b 0a 20 20 73 71 6c 69 LNAME_N);. sqli
1a924 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d te3DbFree(db, p-
1a925 3e 61 43 6f 6c 4e 61 6d 65 29 3b 0a 20 20 6e 20 >aColName);. n
1a926 3d 20 6e 52 65 73 43 6f 6c 75 6d 6e 2a 43 4f 4c = nResColumn*COL
1a927 4e 41 4d 45 5f 4e 3b 0a 20 20 70 2d 3e 6e 52 65 NAME_N;. p->nRe
1a928 73 43 6f 6c 75 6d 6e 20 3d 20 28 75 31 36 29 6e sColumn = (u16)n
1a929 52 65 73 43 6f 6c 75 6d 6e 3b 0a 20 20 70 2d 3e ResColumn;. p->
1a92a 61 43 6f 6c 4e 61 6d 65 20 3d 20 70 43 6f 6c 4e aColName = pColN
1a92b 61 6d 65 20 3d 20 28 4d 65 6d 2a 29 73 71 6c 69 ame = (Mem*)sqli
1a92c 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 te3DbMallocZero(
1a92d 64 62 2c 20 73 69 7a 65 6f 66 28 4d 65 6d 29 2a db, sizeof(Mem)*
1a92e 6e 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 61 43 n );. if( p->aC
1a92f 6f 6c 4e 61 6d 65 3d 3d 30 20 29 20 72 65 74 75 olName==0 ) retu
1a930 72 6e 3b 0a 20 20 77 68 69 6c 65 28 20 6e 2d 2d rn;. while( n--
1a931 20 3e 20 30 20 29 7b 0a 20 20 20 20 70 43 6f 6c > 0 ){. pCol
1a932 4e 61 6d 65 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 Name->flags = ME
1a933 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 70 43 6f 6c M_Null;. pCol
1a934 4e 61 6d 65 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 Name->db = p->db
1a935 3b 0a 20 20 20 20 70 43 6f 6c 4e 61 6d 65 2b 2b ;. pColName++
1a936 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 ;. }.}../*.** S
1a937 65 74 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 et the name of t
1a938 68 65 20 69 64 78 27 74 68 20 63 6f 6c 75 6d 6e he idx'th column
1a939 20 74 6f 20 62 65 20 72 65 74 75 72 6e 65 64 20 to be returned
1a93a 62 79 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 by the SQL state
1a93b 6d 65 6e 74 2e 0a 2a 2a 20 7a 4e 61 6d 65 20 6d ment..** zName m
1a93c 75 73 74 20 62 65 20 61 20 70 6f 69 6e 74 65 72 ust be a pointer
1a93d 20 74 6f 20 61 20 6e 75 6c 20 74 65 72 6d 69 6e to a nul termin
1a93e 61 74 65 64 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a ated string..**.
1a93f 2a 2a 20 54 68 69 73 20 63 61 6c 6c 20 6d 75 73 ** This call mus
1a940 74 20 62 65 20 6d 61 64 65 20 61 66 74 65 72 20 t be made after
1a941 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 a call to sqlite
1a942 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 3VdbeSetNumCols(
1a943 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 6e )..**.** The fin
1a944 61 6c 20 70 61 72 61 6d 65 74 65 72 2c 20 78 44 al parameter, xD
1a945 65 6c 2c 20 6d 75 73 74 20 62 65 20 6f 6e 65 20 el, must be one
1a946 6f 66 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 of SQLITE_DYNAMI
1a947 43 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 C, SQLITE_STATIC
1a948 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f 54 52 .** or SQLITE_TR
1a949 41 4e 53 49 45 4e 54 2e 20 49 66 20 69 74 20 69 ANSIENT. If it i
1a94a 73 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 s SQLITE_DYNAMIC
1a94b 2c 20 74 68 65 6e 20 74 68 65 20 62 75 66 66 65 , then the buffe
1a94c 72 20 70 6f 69 6e 74 65 64 0a 2a 2a 20 74 6f 20 r pointed.** to
1a94d 62 79 20 7a 4e 61 6d 65 20 77 69 6c 6c 20 62 65 by zName will be
1a94e 20 66 72 65 65 64 20 62 79 20 73 71 6c 69 74 65 freed by sqlite
1a94f 33 44 62 46 72 65 65 28 29 20 77 68 65 6e 20 74 3DbFree() when t
1a950 68 65 20 76 64 62 65 20 69 73 20 64 65 73 74 72 he vdbe is destr
1a951 6f 79 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f oyed..*/.SQLITE_
1a952 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
1a953 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d te3VdbeSetColNam
1a954 65 28 0a 20 20 56 64 62 65 20 2a 70 2c 20 20 20 e(. Vdbe *p,
1a955 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a956 20 20 20 20 20 20 2f 2a 20 56 64 62 65 20 62 65 /* Vdbe be
1a957 69 6e 67 20 63 6f 6e 66 69 67 75 72 65 64 20 2a ing configured *
1a958 2f 0a 20 20 69 6e 74 20 69 64 78 2c 20 20 20 20 /. int idx,
1a959 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a95a 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 /* Index of
1a95b 20 63 6f 6c 75 6d 6e 20 7a 4e 61 6d 65 20 61 70 column zName ap
1a95c 70 6c 69 65 73 20 74 6f 20 2a 2f 0a 20 20 69 6e plies to */. in
1a95d 74 20 76 61 72 2c 20 20 20 20 20 20 20 20 20 20 t var,
1a95e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1a95f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 43 4f 4c * One of the COL
1a960 4e 41 4d 45 5f 2a 20 63 6f 6e 73 74 61 6e 74 73 NAME_* constants
1a961 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 */. const char
1a962 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 *zName,
1a963 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 /* Pointe
1a964 72 20 74 6f 20 62 75 66 66 65 72 20 63 6f 6e 74 r to buffer cont
1a965 61 69 6e 69 6e 67 20 6e 61 6d 65 20 2a 2f 0a 20 aining name */.
1a966 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f void (*xDel)(vo
1a967 69 64 2a 29 20 20 20 20 20 20 20 20 20 20 20 20 id*)
1a968 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 6d 61 6e 61 /* Memory mana
1a969 67 65 6d 65 6e 74 20 73 74 72 61 74 65 67 79 20 gement strategy
1a96a 66 6f 72 20 7a 4e 61 6d 65 20 2a 2f 0a 29 7b 0a for zName */.){.
1a96b 20 20 69 6e 74 20 72 63 3b 0a 20 20 4d 65 6d 20 int rc;. Mem
1a96c 2a 70 43 6f 6c 4e 61 6d 65 3b 0a 20 20 61 73 73 *pColName;. ass
1a96d 65 72 74 28 20 69 64 78 3c 70 2d 3e 6e 52 65 73 ert( idx<p->nRes
1a96e 43 6f 6c 75 6d 6e 20 29 3b 0a 20 20 61 73 73 65 Column );. asse
1a96f 72 74 28 20 76 61 72 3c 43 4f 4c 4e 41 4d 45 5f rt( var<COLNAME_
1a970 4e 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 64 62 N );. if( p->db
1a971 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 ->mallocFailed )
1a972 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 7a {. assert( !z
1a973 4e 61 6d 65 20 7c 7c 20 78 44 65 6c 21 3d 53 51 Name || xDel!=SQ
1a974 4c 49 54 45 5f 44 59 4e 41 4d 49 43 20 29 3b 0a LITE_DYNAMIC );.
1a975 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
1a976 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 61 E_NOMEM;. }. a
1a977 73 73 65 72 74 28 20 70 2d 3e 61 43 6f 6c 4e 61 ssert( p->aColNa
1a978 6d 65 21 3d 30 20 29 3b 0a 20 20 70 43 6f 6c 4e me!=0 );. pColN
1a979 61 6d 65 20 3d 20 26 28 70 2d 3e 61 43 6f 6c 4e ame = &(p->aColN
1a97a 61 6d 65 5b 69 64 78 2b 76 61 72 2a 70 2d 3e 6e ame[idx+var*p->n
1a97b 52 65 73 43 6f 6c 75 6d 6e 5d 29 3b 0a 20 20 72 ResColumn]);. r
1a97c 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d c = sqlite3VdbeM
1a97d 65 6d 53 65 74 53 74 72 28 70 43 6f 6c 4e 61 6d emSetStr(pColNam
1a97e 65 2c 20 7a 4e 61 6d 65 2c 20 2d 31 2c 20 53 51 e, zName, -1, SQ
1a97f 4c 49 54 45 5f 55 54 46 38 2c 20 78 44 65 6c 29 LITE_UTF8, xDel)
1a980 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 21 3d ;. assert( rc!=
1a981 30 20 7c 7c 20 21 7a 4e 61 6d 65 20 7c 7c 20 28 0 || !zName || (
1a982 70 43 6f 6c 4e 61 6d 65 2d 3e 66 6c 61 67 73 26 pColName->flags&
1a983 4d 45 4d 5f 54 65 72 6d 29 21 3d 30 20 29 3b 0a MEM_Term)!=0 );.
1a984 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
1a985 2f 2a 0a 2a 2a 20 41 20 72 65 61 64 20 6f 72 20 /*.** A read or
1a986 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f write transactio
1a987 6e 20 6d 61 79 20 6f 72 20 6d 61 79 20 6e 6f 74 n may or may not
1a988 20 62 65 20 61 63 74 69 76 65 20 6f 6e 20 64 61 be active on da
1a989 74 61 62 61 73 65 20 68 61 6e 64 6c 65 0a 2a 2a tabase handle.**
1a98a 20 64 62 2e 20 49 66 20 61 20 74 72 61 6e 73 61 db. If a transa
1a98b 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65 2c ction is active,
1a98c 20 63 6f 6d 6d 69 74 20 69 74 2e 20 49 66 20 74 commit it. If t
1a98d 68 65 72 65 20 69 73 20 61 0a 2a 2a 20 77 72 69 here is a.** wri
1a98e 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 73 te-transaction s
1a98f 70 61 6e 6e 69 6e 67 20 6d 6f 72 65 20 74 68 61 panning more tha
1a990 6e 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20 66 n one database f
1a991 69 6c 65 2c 20 74 68 69 73 20 72 6f 75 74 69 6e ile, this routin
1a992 65 0a 2a 2a 20 74 61 6b 65 73 20 63 61 72 65 20 e.** takes care
1a993 6f 66 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f of the master jo
1a994 75 72 6e 61 6c 20 74 72 69 63 6b 65 72 79 2e 0a urnal trickery..
1a995 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 76 64 */.static int vd
1a996 62 65 43 6f 6d 6d 69 74 28 73 71 6c 69 74 65 33 beCommit(sqlite3
1a997 20 2a 64 62 2c 20 56 64 62 65 20 2a 70 29 7b 0a *db, Vdbe *p){.
1a998 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e int i;. int n
1a999 54 72 61 6e 73 20 3d 20 30 3b 20 20 2f 2a 20 4e Trans = 0; /* N
1a99a 75 6d 62 65 72 20 6f 66 20 64 61 74 61 62 61 73 umber of databas
1a99b 65 73 20 77 69 74 68 20 61 6e 20 61 63 74 69 76 es with an activ
1a99c 65 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 e write-transact
1a99d 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 ion */. int rc
1a99e 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 = SQLITE_OK;. i
1a99f 6e 74 20 6e 65 65 64 58 63 6f 6d 6d 69 74 20 3d nt needXcommit =
1a9a0 20 30 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 0;..#ifdef SQLI
1a9a1 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 TE_OMIT_VIRTUALT
1a9a2 41 42 4c 45 0a 20 20 2f 2a 20 57 69 74 68 20 74 ABLE. /* With t
1a9a3 68 69 73 20 6f 70 74 69 6f 6e 2c 20 73 71 6c 69 his option, sqli
1a9a4 74 65 33 56 74 61 62 53 79 6e 63 28 29 20 69 73 te3VtabSync() is
1a9a5 20 64 65 66 69 6e 65 64 20 74 6f 20 62 65 20 73 defined to be s
1a9a6 69 6d 70 6c 79 20 0a 20 20 2a 2a 20 53 51 4c 49 imply . ** SQLI
1a9a7 54 45 5f 4f 4b 20 73 6f 20 70 20 69 73 20 6e 6f TE_OK so p is no
1a9a8 74 20 75 73 65 64 2e 20 0a 20 20 2a 2f 0a 20 20 t used. . */.
1a9a9 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
1a9aa 28 70 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f (p);.#endif.. /
1a9ab 2a 20 42 65 66 6f 72 65 20 64 6f 69 6e 67 20 61 * Before doing a
1a9ac 6e 79 74 68 69 6e 67 20 65 6c 73 65 2c 20 63 61 nything else, ca
1a9ad 6c 6c 20 74 68 65 20 78 53 79 6e 63 28 29 20 63 ll the xSync() c
1a9ae 61 6c 6c 62 61 63 6b 20 66 6f 72 20 61 6e 79 0a allback for any.
1a9af 20 20 2a 2a 20 76 69 72 74 75 61 6c 20 6d 6f 64 ** virtual mod
1a9b0 75 6c 65 20 74 61 62 6c 65 73 20 77 72 69 74 74 ule tables writt
1a9b1 65 6e 20 69 6e 20 74 68 69 73 20 74 72 61 6e 73 en in this trans
1a9b2 61 63 74 69 6f 6e 2e 20 54 68 69 73 20 68 61 73 action. This has
1a9b3 20 74 6f 0a 20 20 2a 2a 20 62 65 20 64 6f 6e 65 to. ** be done
1a9b4 20 62 65 66 6f 72 65 20 64 65 74 65 72 6d 69 6e before determin
1a9b5 69 6e 67 20 77 68 65 74 68 65 72 20 61 20 6d 61 ing whether a ma
1a9b6 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c ster journal fil
1a9b7 65 20 69 73 20 0a 20 20 2a 2a 20 72 65 71 75 69 e is . ** requi
1a9b8 72 65 64 2c 20 61 73 20 61 6e 20 78 53 79 6e 63 red, as an xSync
1a9b9 28 29 20 63 61 6c 6c 62 61 63 6b 20 6d 61 79 20 () callback may
1a9ba 61 64 64 20 61 6e 20 61 74 74 61 63 68 65 64 20 add an attached
1a9bb 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20 74 6f database. ** to
1a9bc 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e the transaction
1a9bd 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 .. */. rc = sq
1a9be 6c 69 74 65 33 56 74 61 62 53 79 6e 63 28 64 62 lite3VtabSync(db
1a9bf 2c 20 26 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a , &p->zErrMsg);.
1a9c0 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
1a9c1 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 _OK ){. retur
1a9c2 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 n rc;. }.. /*
1a9c3 54 68 69 73 20 6c 6f 6f 70 20 64 65 74 65 72 6d This loop determ
1a9c4 69 6e 65 73 20 28 61 29 20 69 66 20 74 68 65 20 ines (a) if the
1a9c5 63 6f 6d 6d 69 74 20 68 6f 6f 6b 20 73 68 6f 75 commit hook shou
1a9c6 6c 64 20 62 65 20 69 6e 76 6f 6b 65 64 20 61 6e ld be invoked an
1a9c7 64 0a 20 20 2a 2a 20 28 62 29 20 68 6f 77 20 6d d. ** (b) how m
1a9c8 61 6e 79 20 64 61 74 61 62 61 73 65 20 66 69 6c any database fil
1a9c9 65 73 20 68 61 76 65 20 6f 70 65 6e 20 77 72 69 es have open wri
1a9ca 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 2c te transactions,
1a9cb 20 6e 6f 74 20 0a 20 20 2a 2a 20 69 6e 63 6c 75 not . ** inclu
1a9cc 64 69 6e 67 20 74 68 65 20 74 65 6d 70 20 64 61 ding the temp da
1a9cd 74 61 62 61 73 65 2e 20 28 62 29 20 69 73 20 69 tabase. (b) is i
1a9ce 6d 70 6f 72 74 61 6e 74 20 62 65 63 61 75 73 65 mportant because
1a9cf 20 69 66 20 6d 6f 72 65 20 74 68 61 6e 20 0a 20 if more than .
1a9d0 20 2a 2a 20 6f 6e 65 20 64 61 74 61 62 61 73 65 ** one database
1a9d1 20 66 69 6c 65 20 68 61 73 20 61 6e 20 6f 70 65 file has an ope
1a9d2 6e 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 n write transact
1a9d3 69 6f 6e 2c 20 61 20 6d 61 73 74 65 72 20 6a 6f ion, a master jo
1a9d4 75 72 6e 61 6c 0a 20 20 2a 2a 20 66 69 6c 65 20 urnal. ** file
1a9d5 69 73 20 72 65 71 75 69 72 65 64 20 66 6f 72 20 is required for
1a9d6 61 6e 20 61 74 6f 6d 69 63 20 63 6f 6d 6d 69 74 an atomic commit
1a9d7 2e 0a 20 20 2a 2f 20 0a 20 20 66 6f 72 28 69 3d .. */ . for(i=
1a9d8 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 0; i<db->nDb; i+
1a9d9 2b 29 7b 20 0a 20 20 20 20 42 74 72 65 65 20 2a +){ . Btree *
1a9da 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d pBt = db->aDb[i]
1a9db 2e 70 42 74 3b 0a 20 20 20 20 69 66 28 20 73 71 .pBt;. if( sq
1a9dc 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e 54 72 lite3BtreeIsInTr
1a9dd 61 6e 73 28 70 42 74 29 20 29 7b 0a 20 20 20 20 ans(pBt) ){.
1a9de 20 20 6e 65 65 64 58 63 6f 6d 6d 69 74 20 3d 20 needXcommit =
1a9df 31 3b 0a 20 20 20 20 20 20 69 66 28 20 69 21 3d 1;. if( i!=
1a9e0 31 20 29 20 6e 54 72 61 6e 73 2b 2b 3b 0a 20 20 1 ) nTrans++;.
1a9e1 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 }. }.. /* If
1a9e2 20 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 77 there are any w
1a9e3 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e rite-transaction
1a9e4 73 20 61 74 20 61 6c 6c 2c 20 69 6e 76 6f 6b 65 s at all, invoke
1a9e5 20 74 68 65 20 63 6f 6d 6d 69 74 20 68 6f 6f 6b the commit hook
1a9e6 20 2a 2f 0a 20 20 69 66 28 20 6e 65 65 64 58 63 */. if( needXc
1a9e7 6f 6d 6d 69 74 20 26 26 20 64 62 2d 3e 78 43 6f ommit && db->xCo
1a9e8 6d 6d 69 74 43 61 6c 6c 62 61 63 6b 20 29 7b 0a mmitCallback ){.
1a9e9 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 (void)sqlite
1a9ea 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 3SafetyOff(db);.
1a9eb 20 20 20 20 72 63 20 3d 20 64 62 2d 3e 78 43 6f rc = db->xCo
1a9ec 6d 6d 69 74 43 61 6c 6c 62 61 63 6b 28 64 62 2d mmitCallback(db-
1a9ed 3e 70 43 6f 6d 6d 69 74 41 72 67 29 3b 0a 20 20 >pCommitArg);.
1a9ee 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 (void)sqlite3S
1a9ef 61 66 65 74 79 4f 6e 28 64 62 29 3b 0a 20 20 20 afetyOn(db);.
1a9f0 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 if( rc ){.
1a9f1 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
1a9f2 4f 4e 53 54 52 41 49 4e 54 3b 0a 20 20 20 20 7d ONSTRAINT;. }
1a9f3 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 73 . }.. /* The s
1a9f4 69 6d 70 6c 65 20 63 61 73 65 20 2d 20 6e 6f 20 imple case - no
1a9f5 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 64 61 more than one da
1a9f6 74 61 62 61 73 65 20 66 69 6c 65 20 28 6e 6f 74 tabase file (not
1a9f7 20 63 6f 75 6e 74 69 6e 67 20 74 68 65 0a 20 20 counting the.
1a9f8 2a 2a 20 54 45 4d 50 20 64 61 74 61 62 61 73 65 ** TEMP database
1a9f9 29 20 68 61 73 20 61 20 74 72 61 6e 73 61 63 74 ) has a transact
1a9fa 69 6f 6e 20 61 63 74 69 76 65 2e 20 20 20 54 68 ion active. Th
1a9fb 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 20 66 ere is no need f
1a9fc 6f 72 20 74 68 65 0a 20 20 2a 2a 20 6d 61 73 74 or the. ** mast
1a9fd 65 72 2d 6a 6f 75 72 6e 61 6c 2e 0a 20 20 2a 2a er-journal.. **
1a9fe 0a 20 20 2a 2a 20 49 66 20 74 68 65 20 72 65 74 . ** If the ret
1a9ff 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 73 71 6c urn value of sql
1aa00 69 74 65 33 42 74 72 65 65 47 65 74 46 69 6c 65 ite3BtreeGetFile
1aa01 6e 61 6d 65 28 29 20 69 73 20 61 20 7a 65 72 6f name() is a zero
1aa02 20 6c 65 6e 67 74 68 0a 20 20 2a 2a 20 73 74 72 length. ** str
1aa03 69 6e 67 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 ing, it means th
1aa04 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 e main database
1aa05 69 73 20 3a 6d 65 6d 6f 72 79 3a 20 6f 72 20 61 is :memory: or a
1aa06 20 74 65 6d 70 20 66 69 6c 65 2e 20 20 49 6e 20 temp file. In
1aa07 0a 20 20 2a 2a 20 74 68 61 74 20 63 61 73 65 20 . ** that case
1aa08 77 65 20 64 6f 20 6e 6f 74 20 73 75 70 70 6f 72 we do not suppor
1aa09 74 20 61 74 6f 6d 69 63 20 6d 75 6c 74 69 2d 66 t atomic multi-f
1aa0a 69 6c 65 20 63 6f 6d 6d 69 74 73 2c 20 73 6f 20 ile commits, so
1aa0b 75 73 65 20 74 68 65 20 0a 20 20 2a 2a 20 73 69 use the . ** si
1aa0c 6d 70 6c 65 20 63 61 73 65 20 74 68 65 6e 20 74 mple case then t
1aa0d 6f 6f 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 30 oo.. */. if( 0
1aa0e 3d 3d 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 ==sqlite3Strlen3
1aa0f 30 28 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 0(sqlite3BtreeGe
1aa10 74 46 69 6c 65 6e 61 6d 65 28 64 62 2d 3e 61 44 tFilename(db->aD
1aa11 62 5b 30 5d 2e 70 42 74 29 29 0a 20 20 20 7c 7c b[0].pBt)). ||
1aa12 20 6e 54 72 61 6e 73 3c 3d 31 0a 20 20 29 7b 0a nTrans<=1. ){.
1aa13 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 72 63 3d for(i=0; rc=
1aa14 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 3c =SQLITE_OK && i<
1aa15 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 db->nDb; i++){.
1aa16 20 20 20 20 20 42 74 72 65 65 20 2a 70 42 74 20 Btree *pBt
1aa17 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 = db->aDb[i].pBt
1aa18 3b 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 20 ;. if( pBt
1aa19 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 ){. rc =
1aa1a 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d sqlite3BtreeComm
1aa1b 69 74 50 68 61 73 65 4f 6e 65 28 70 42 74 2c 20 itPhaseOne(pBt,
1aa1c 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 0);. }.
1aa1d 7d 0a 0a 20 20 20 20 2f 2a 20 44 6f 20 74 68 65 }.. /* Do the
1aa1e 20 63 6f 6d 6d 69 74 20 6f 6e 6c 79 20 69 66 20 commit only if
1aa1f 61 6c 6c 20 64 61 74 61 62 61 73 65 73 20 73 75 all databases su
1aa20 63 63 65 73 73 66 75 6c 6c 79 20 63 6f 6d 70 6c ccessfully compl
1aa21 65 74 65 20 70 68 61 73 65 20 31 2e 20 0a 20 20 ete phase 1. .
1aa22 20 20 2a 2a 20 49 66 20 6f 6e 65 20 6f 66 20 74 ** If one of t
1aa23 68 65 20 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 he BtreeCommitPh
1aa24 61 73 65 4f 6e 65 28 29 20 63 61 6c 6c 73 20 66 aseOne() calls f
1aa25 61 69 6c 73 2c 20 74 68 69 73 20 69 6e 64 69 63 ails, this indic
1aa26 61 74 65 73 20 61 6e 0a 20 20 20 20 2a 2a 20 49 ates an. ** I
1aa27 4f 20 65 72 72 6f 72 20 77 68 69 6c 65 20 64 65 O error while de
1aa28 6c 65 74 69 6e 67 20 6f 72 20 74 72 75 6e 63 61 leting or trunca
1aa29 74 69 6e 67 20 61 20 6a 6f 75 72 6e 61 6c 20 66 ting a journal f
1aa2a 69 6c 65 2e 20 49 74 20 69 73 20 75 6e 6c 69 6b ile. It is unlik
1aa2b 65 6c 79 2c 0a 20 20 20 20 2a 2a 20 62 75 74 20 ely,. ** but
1aa2c 63 6f 75 6c 64 20 68 61 70 70 65 6e 2e 20 49 6e could happen. In
1aa2d 20 74 68 69 73 20 63 61 73 65 20 61 62 61 6e 64 this case aband
1aa2e 6f 6e 20 70 72 6f 63 65 73 73 69 6e 67 20 61 6e on processing an
1aa2f 64 20 72 65 74 75 72 6e 20 74 68 65 20 65 72 72 d return the err
1aa30 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 or.. */. f
1aa31 6f 72 28 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 or(i=0; rc==SQLI
1aa32 54 45 5f 4f 4b 20 26 26 20 69 3c 64 62 2d 3e 6e TE_OK && i<db->n
1aa33 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 Db; i++){.
1aa34 42 74 72 65 65 20 2a 70 42 74 20 3d 20 64 62 2d Btree *pBt = db-
1aa35 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 >aDb[i].pBt;.
1aa36 20 20 20 69 66 28 20 70 42 74 20 29 7b 0a 20 20 if( pBt ){.
1aa37 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
1aa38 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 e3BtreeCommitPha
1aa39 73 65 54 77 6f 28 70 42 74 29 3b 0a 20 20 20 20 seTwo(pBt);.
1aa3a 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 }. }. if
1aa3b 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
1aa3c 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
1aa3d 56 74 61 62 43 6f 6d 6d 69 74 28 64 62 29 3b 0a VtabCommit(db);.
1aa3e 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 }. }.. /*
1aa3f 54 68 65 20 63 6f 6d 70 6c 65 78 20 63 61 73 65 The complex case
1aa40 20 2d 20 54 68 65 72 65 20 69 73 20 61 20 6d 75 - There is a mu
1aa41 6c 74 69 2d 66 69 6c 65 20 77 72 69 74 65 2d 74 lti-file write-t
1aa42 72 61 6e 73 61 63 74 69 6f 6e 20 61 63 74 69 76 ransaction activ
1aa43 65 2e 0a 20 20 2a 2a 20 54 68 69 73 20 72 65 71 e.. ** This req
1aa44 75 69 72 65 73 20 61 20 6d 61 73 74 65 72 20 6a uires a master j
1aa45 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 6f 20 65 ournal file to e
1aa46 6e 73 75 72 65 20 74 68 65 20 74 72 61 6e 73 61 nsure the transa
1aa47 63 74 69 6f 6e 20 69 73 0a 20 20 2a 2a 20 63 6f ction is. ** co
1aa48 6d 6d 69 74 74 65 64 20 61 74 6f 6d 69 63 6c 79 mmitted atomicly
1aa49 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 .. */.#ifndef S
1aa4a 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 QLITE_OMIT_DISKI
1aa4b 4f 0a 20 20 65 6c 73 65 7b 0a 20 20 20 20 73 71 O. else{. sq
1aa4c 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 20 lite3_vfs *pVfs
1aa4d 3d 20 64 62 2d 3e 70 56 66 73 3b 0a 20 20 20 20 = db->pVfs;.
1aa4e 69 6e 74 20 6e 65 65 64 53 79 6e 63 20 3d 20 30 int needSync = 0
1aa4f 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 4d 61 73 ;. char *zMas
1aa50 74 65 72 20 3d 20 30 3b 20 20 20 2f 2a 20 46 69 ter = 0; /* Fi
1aa51 6c 65 2d 6e 61 6d 65 20 66 6f 72 20 74 68 65 20 le-name for the
1aa52 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 2a master journal *
1aa53 2f 0a 20 20 20 20 63 68 61 72 20 63 6f 6e 73 74 /. char const
1aa54 20 2a 7a 4d 61 69 6e 46 69 6c 65 20 3d 20 73 71 *zMainFile = sq
1aa55 6c 69 74 65 33 42 74 72 65 65 47 65 74 46 69 6c lite3BtreeGetFil
1aa56 65 6e 61 6d 65 28 64 62 2d 3e 61 44 62 5b 30 5d ename(db->aDb[0]
1aa57 2e 70 42 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 .pBt);. sqlit
1aa58 65 33 5f 66 69 6c 65 20 2a 70 4d 61 73 74 65 72 e3_file *pMaster
1aa59 20 3d 20 30 3b 0a 20 20 20 20 69 36 34 20 6f 66 = 0;. i64 of
1aa5a 66 73 65 74 20 3d 20 30 3b 0a 20 20 20 20 69 6e fset = 0;. in
1aa5b 74 20 72 65 73 3b 0a 0a 20 20 20 20 2f 2a 20 53 t res;.. /* S
1aa5c 65 6c 65 63 74 20 61 20 6d 61 73 74 65 72 20 6a elect a master j
1aa5d 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 ournal file name
1aa5e 20 2a 2f 0a 20 20 20 20 64 6f 20 7b 0a 20 20 20 */. do {.
1aa5f 20 20 20 75 33 32 20 69 52 61 6e 64 6f 6d 3b 0a u32 iRandom;.
1aa60 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 sqlite3DbF
1aa61 72 65 65 28 64 62 2c 20 7a 4d 61 73 74 65 72 29 ree(db, zMaster)
1aa62 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f ;. sqlite3_
1aa63 72 61 6e 64 6f 6d 6e 65 73 73 28 73 69 7a 65 6f randomness(sizeo
1aa64 66 28 69 52 61 6e 64 6f 6d 29 2c 20 26 69 52 61 f(iRandom), &iRa
1aa65 6e 64 6f 6d 29 3b 0a 20 20 20 20 20 20 7a 4d 61 ndom);. zMa
1aa66 73 74 65 72 20 3d 20 73 71 6c 69 74 65 33 4d 50 ster = sqlite3MP
1aa67 72 69 6e 74 66 28 64 62 2c 20 22 25 73 2d 6d 6a rintf(db, "%s-mj
1aa68 25 30 38 58 22 2c 20 7a 4d 61 69 6e 46 69 6c 65 %08X", zMainFile
1aa69 2c 20 69 52 61 6e 64 6f 6d 26 30 78 37 66 66 66 , iRandom&0x7fff
1aa6a 66 66 66 66 29 3b 0a 20 20 20 20 20 20 69 66 28 ffff);. if(
1aa6b 20 21 7a 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 !zMaster ){.
1aa6c 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
1aa6d 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 TE_NOMEM;.
1aa6e 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c }. rc = sql
1aa6f 69 74 65 33 4f 73 41 63 63 65 73 73 28 70 56 66 ite3OsAccess(pVf
1aa70 73 2c 20 7a 4d 61 73 74 65 72 2c 20 53 51 4c 49 s, zMaster, SQLI
1aa71 54 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 TE_ACCESS_EXISTS
1aa72 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 7d 77 68 , &res);. }wh
1aa73 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f ile( rc==SQLITE_
1aa74 4f 4b 20 26 26 20 72 65 73 20 29 3b 0a 20 20 20 OK && res );.
1aa75 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
1aa76 4f 4b 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 4f OK ){. /* O
1aa77 70 65 6e 20 74 68 65 20 6d 61 73 74 65 72 20 6a pen the master j
1aa78 6f 75 72 6e 61 6c 2e 20 2a 2f 0a 20 20 20 20 20 ournal. */.
1aa79 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f rc = sqlite3OsO
1aa7a 70 65 6e 4d 61 6c 6c 6f 63 28 70 56 66 73 2c 20 penMalloc(pVfs,
1aa7b 7a 4d 61 73 74 65 72 2c 20 26 70 4d 61 73 74 65 zMaster, &pMaste
1aa7c 72 2c 20 0a 20 20 20 20 20 20 20 20 20 20 53 51 r, . SQ
1aa7d 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 LITE_OPEN_READWR
1aa7e 49 54 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f ITE|SQLITE_OPEN_
1aa7f 43 52 45 41 54 45 7c 0a 20 20 20 20 20 20 20 20 CREATE|.
1aa80 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 SQLITE_OPEN_EX
1aa81 43 4c 55 53 49 56 45 7c 53 51 4c 49 54 45 5f 4f CLUSIVE|SQLITE_O
1aa82 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 4e PEN_MASTER_JOURN
1aa83 41 4c 2c 20 30 0a 20 20 20 20 20 20 29 3b 0a 20 AL, 0. );.
1aa84 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 21 }. if( rc!
1aa85 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
1aa86 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
1aa87 65 28 64 62 2c 20 7a 4d 61 73 74 65 72 29 3b 0a e(db, zMaster);.
1aa88 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b return rc;
1aa89 0a 20 20 20 20 7d 0a 20 0a 20 20 20 20 2f 2a 20 . }. . /*
1aa8a 57 72 69 74 65 20 74 68 65 20 6e 61 6d 65 20 6f Write the name o
1aa8b 66 20 65 61 63 68 20 64 61 74 61 62 61 73 65 20 f each database
1aa8c 66 69 6c 65 20 69 6e 20 74 68 65 20 74 72 61 6e file in the tran
1aa8d 73 61 63 74 69 6f 6e 20 69 6e 74 6f 20 74 68 65 saction into the
1aa8e 20 6e 65 77 0a 20 20 20 20 2a 2a 20 6d 61 73 74 new. ** mast
1aa8f 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e er journal file.
1aa90 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 If an error occ
1aa91 75 72 73 20 61 74 20 74 68 69 73 20 70 6f 69 6e urs at this poin
1aa92 74 20 63 6c 6f 73 65 0a 20 20 20 20 2a 2a 20 61 t close. ** a
1aa93 6e 64 20 64 65 6c 65 74 65 20 74 68 65 20 6d 61 nd delete the ma
1aa94 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c ster journal fil
1aa95 65 2e 20 41 6c 6c 20 74 68 65 20 69 6e 64 69 76 e. All the indiv
1aa96 69 64 75 61 6c 20 6a 6f 75 72 6e 61 6c 20 66 69 idual journal fi
1aa97 6c 65 73 0a 20 20 20 20 2a 2a 20 73 74 69 6c 6c les. ** still
1aa98 20 68 61 76 65 20 27 6e 75 6c 6c 27 20 61 73 20 have 'null' as
1aa99 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e the master journ
1aa9a 61 6c 20 70 6f 69 6e 74 65 72 2c 20 73 6f 20 74 al pointer, so t
1aa9b 68 65 79 20 77 69 6c 6c 20 72 6f 6c 6c 0a 20 20 hey will roll.
1aa9c 20 20 2a 2a 20 62 61 63 6b 20 69 6e 64 65 70 65 ** back indepe
1aa9d 6e 64 65 6e 74 6c 79 20 69 66 20 61 20 66 61 69 ndently if a fai
1aa9e 6c 75 72 65 20 6f 63 63 75 72 73 2e 0a 20 20 20 lure occurs..
1aa9f 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b */. for(i=0;
1aaa0 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 i<db->nDb; i++)
1aaa1 7b 0a 20 20 20 20 20 20 42 74 72 65 65 20 2a 70 {. Btree *p
1aaa2 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e Bt = db->aDb[i].
1aaa3 70 42 74 3b 0a 20 20 20 20 20 20 69 66 28 20 69 pBt;. if( i
1aaa4 3d 3d 31 20 29 20 63 6f 6e 74 69 6e 75 65 3b 20 ==1 ) continue;
1aaa5 20 20 2f 2a 20 49 67 6e 6f 72 65 20 74 68 65 20 /* Ignore the
1aaa6 54 45 4d 50 20 64 61 74 61 62 61 73 65 20 2a 2f TEMP database */
1aaa7 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 . if( sqlit
1aaa8 65 33 42 74 72 65 65 49 73 49 6e 54 72 61 6e 73 e3BtreeIsInTrans
1aaa9 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 20 (pBt) ){.
1aaaa 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 46 69 char const *zFi
1aaab 6c 65 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 le = sqlite3Btre
1aaac 65 47 65 74 4a 6f 75 72 6e 61 6c 6e 61 6d 65 28 eGetJournalname(
1aaad 70 42 74 29 3b 0a 20 20 20 20 20 20 20 20 69 66 pBt);. if
1aaae 28 20 7a 46 69 6c 65 5b 30 5d 3d 3d 30 20 29 20 ( zFile[0]==0 )
1aaaf 63 6f 6e 74 69 6e 75 65 3b 20 20 2f 2a 20 49 67 continue; /* Ig
1aab0 6e 6f 72 65 20 3a 6d 65 6d 6f 72 79 3a 20 64 61 nore :memory: da
1aab1 74 61 62 61 73 65 73 20 2a 2f 0a 20 20 20 20 20 tabases */.
1aab2 20 20 20 69 66 28 20 21 6e 65 65 64 53 79 6e 63 if( !needSync
1aab3 20 26 26 20 21 73 71 6c 69 74 65 33 42 74 72 65 && !sqlite3Btre
1aab4 65 53 79 6e 63 44 69 73 61 62 6c 65 64 28 70 42 eSyncDisabled(pB
1aab5 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 t) ){.
1aab6 6e 65 65 64 53 79 6e 63 20 3d 20 31 3b 0a 20 20 needSync = 1;.
1aab7 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
1aab8 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 rc = sqlite3OsWr
1aab9 69 74 65 28 70 4d 61 73 74 65 72 2c 20 7a 46 69 ite(pMaster, zFi
1aaba 6c 65 2c 20 73 71 6c 69 74 65 33 53 74 72 6c 65 le, sqlite3Strle
1aabb 6e 33 30 28 7a 46 69 6c 65 29 2b 31 2c 20 6f 66 n30(zFile)+1, of
1aabc 66 73 65 74 29 3b 0a 20 20 20 20 20 20 20 20 6f fset);. o
1aabd 66 66 73 65 74 20 2b 3d 20 73 71 6c 69 74 65 33 ffset += sqlite3
1aabe 53 74 72 6c 65 6e 33 30 28 7a 46 69 6c 65 29 2b Strlen30(zFile)+
1aabf 31 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 1;. if( r
1aac0 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
1aac1 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
1aac2 33 4f 73 43 6c 6f 73 65 46 72 65 65 28 70 4d 61 3OsCloseFree(pMa
1aac3 73 74 65 72 29 3b 0a 20 20 20 20 20 20 20 20 20 ster);.
1aac4 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 sqlite3OsDelete
1aac5 28 70 56 66 73 2c 20 7a 4d 61 73 74 65 72 2c 20 (pVfs, zMaster,
1aac6 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 0);. sq
1aac7 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 lite3DbFree(db,
1aac8 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20 zMaster);.
1aac9 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
1aaca 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
1aacb 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 53 . }.. /* S
1aacc 79 6e 63 20 74 68 65 20 6d 61 73 74 65 72 20 6a ync the master j
1aacd 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 66 20 ournal file. If
1aace 74 68 65 20 49 4f 43 41 50 5f 53 45 51 55 45 4e the IOCAP_SEQUEN
1aacf 54 49 41 4c 20 64 65 76 69 63 65 0a 20 20 20 20 TIAL device.
1aad0 2a 2a 20 66 6c 61 67 20 69 73 20 73 65 74 20 74 ** flag is set t
1aad1 68 69 73 20 69 73 20 6e 6f 74 20 72 65 71 75 69 his is not requi
1aad2 72 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 red.. */.
1aad3 69 66 28 20 6e 65 65 64 53 79 6e 63 20 0a 20 20 if( needSync .
1aad4 20 20 20 26 26 20 30 3d 3d 28 73 71 6c 69 74 65 && 0==(sqlite
1aad5 33 4f 73 44 65 76 69 63 65 43 68 61 72 61 63 74 3OsDeviceCharact
1aad6 65 72 69 73 74 69 63 73 28 70 4d 61 73 74 65 72 eristics(pMaster
1aad7 29 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 )&SQLITE_IOCAP_S
1aad8 45 51 55 45 4e 54 49 41 4c 29 0a 20 20 20 20 20 EQUENTIAL).
1aad9 26 26 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 && SQLITE_OK!=(r
1aada 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 79 6e c = sqlite3OsSyn
1aadb 63 28 70 4d 61 73 74 65 72 2c 20 53 51 4c 49 54 c(pMaster, SQLIT
1aadc 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c 29 29 0a E_SYNC_NORMAL)).
1aadd 20 20 20 20 29 7b 0a 20 20 20 20 20 20 73 71 6c ){. sql
1aade 69 74 65 33 4f 73 43 6c 6f 73 65 46 72 65 65 28 ite3OsCloseFree(
1aadf 70 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20 pMaster);.
1aae0 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28 sqlite3OsDelete(
1aae1 70 56 66 73 2c 20 7a 4d 61 73 74 65 72 2c 20 30 pVfs, zMaster, 0
1aae2 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
1aae3 44 62 46 72 65 65 28 64 62 2c 20 7a 4d 61 73 74 DbFree(db, zMast
1aae4 65 72 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 er);. retur
1aae5 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 n rc;. }..
1aae6 20 2f 2a 20 53 79 6e 63 20 61 6c 6c 20 74 68 65 /* Sync all the
1aae7 20 64 62 20 66 69 6c 65 73 20 69 6e 76 6f 6c 76 db files involv
1aae8 65 64 20 69 6e 20 74 68 65 20 74 72 61 6e 73 61 ed in the transa
1aae9 63 74 69 6f 6e 2e 20 54 68 65 20 73 61 6d 65 20 ction. The same
1aaea 63 61 6c 6c 0a 20 20 20 20 2a 2a 20 73 65 74 73 call. ** sets
1aaeb 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 the master jour
1aaec 6e 61 6c 20 70 6f 69 6e 74 65 72 20 69 6e 20 65 nal pointer in e
1aaed 61 63 68 20 69 6e 64 69 76 69 64 75 61 6c 20 6a ach individual j
1aaee 6f 75 72 6e 61 6c 2e 20 49 66 0a 20 20 20 20 2a ournal. If. *
1aaef 2a 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 * an error occur
1aaf0 73 20 68 65 72 65 2c 20 64 6f 20 6e 6f 74 20 64 s here, do not d
1aaf1 65 6c 65 74 65 20 74 68 65 20 6d 61 73 74 65 72 elete the master
1aaf2 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 20 journal file..
1aaf3 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 20 **. ** If
1aaf4 74 68 65 20 65 72 72 6f 72 20 6f 63 63 75 72 73 the error occurs
1aaf5 20 64 75 72 69 6e 67 20 74 68 65 20 66 69 72 73 during the firs
1aaf6 74 20 63 61 6c 6c 20 74 6f 0a 20 20 20 20 2a 2a t call to. **
1aaf7 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d sqlite3BtreeCom
1aaf8 6d 69 74 50 68 61 73 65 4f 6e 65 28 29 2c 20 74 mitPhaseOne(), t
1aaf9 68 65 6e 20 74 68 65 72 65 20 69 73 20 61 20 63 hen there is a c
1aafa 68 61 6e 63 65 20 74 68 61 74 20 74 68 65 0a 20 hance that the.
1aafb 20 20 20 2a 2a 20 6d 61 73 74 65 72 20 6a 6f 75 ** master jou
1aafc 72 6e 61 6c 20 66 69 6c 65 20 77 69 6c 6c 20 62 rnal file will b
1aafd 65 20 6f 72 70 68 61 6e 65 64 2e 20 42 75 74 20 e orphaned. But
1aafe 77 65 20 63 61 6e 6e 6f 74 20 64 65 6c 65 74 65 we cannot delete
1aaff 20 69 74 2c 0a 20 20 20 20 2a 2a 20 69 6e 20 63 it,. ** in c
1ab00 61 73 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a ase the master j
1ab01 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 ournal file name
1ab02 20 77 61 73 20 77 72 69 74 74 65 6e 20 69 6e 74 was written int
1ab03 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 20 20 o the journal.
1ab04 20 20 2a 2a 20 66 69 6c 65 20 62 65 66 6f 72 65 ** file before
1ab05 20 74 68 65 20 66 61 69 6c 75 72 65 20 6f 63 63 the failure occ
1ab06 75 72 72 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 urred.. */.
1ab07 20 20 66 6f 72 28 69 3d 30 3b 20 72 63 3d 3d 53 for(i=0; rc==S
1ab08 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 3c 64 62 QLITE_OK && i<db
1ab09 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 20 0a 20 20 ->nDb; i++){ .
1ab0a 20 20 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d Btree *pBt =
1ab0b 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b db->aDb[i].pBt;
1ab0c 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 20 29 . if( pBt )
1ab0d 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 {. rc = s
1ab0e 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 qlite3BtreeCommi
1ab0f 74 50 68 61 73 65 4f 6e 65 28 70 42 74 2c 20 7a tPhaseOne(pBt, z
1ab10 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20 7d Master);. }
1ab11 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 . }. sqlit
1ab12 65 33 4f 73 43 6c 6f 73 65 46 72 65 65 28 70 4d e3OsCloseFree(pM
1ab13 61 73 74 65 72 29 3b 0a 20 20 20 20 69 66 28 20 aster);. if(
1ab14 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
1ab15 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 . sqlite3Db
1ab16 46 72 65 65 28 64 62 2c 20 7a 4d 61 73 74 65 72 Free(db, zMaster
1ab17 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 );. return
1ab18 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f rc;. }.. /
1ab19 2a 20 44 65 6c 65 74 65 20 74 68 65 20 6d 61 73 * Delete the mas
1ab1a 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ter journal file
1ab1b 2e 20 54 68 69 73 20 63 6f 6d 6d 69 74 73 20 74 . This commits t
1ab1c 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 he transaction.
1ab1d 41 66 74 65 72 0a 20 20 20 20 2a 2a 20 64 6f 69 After. ** doi
1ab1e 6e 67 20 74 68 69 73 20 74 68 65 20 64 69 72 65 ng this the dire
1ab1f 63 74 6f 72 79 20 69 73 20 73 79 6e 63 65 64 20 ctory is synced
1ab20 61 67 61 69 6e 20 62 65 66 6f 72 65 20 61 6e 79 again before any
1ab21 20 69 6e 64 69 76 69 64 75 61 6c 0a 20 20 20 20 individual.
1ab22 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 66 ** transaction f
1ab23 69 6c 65 73 20 61 72 65 20 64 65 6c 65 74 65 64 iles are deleted
1ab24 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 .. */. rc
1ab25 3d 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 = sqlite3OsDelet
1ab26 65 28 70 56 66 73 2c 20 7a 4d 61 73 74 65 72 2c e(pVfs, zMaster,
1ab27 20 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 1);. sqlite3
1ab28 44 62 46 72 65 65 28 64 62 2c 20 7a 4d 61 73 74 DbFree(db, zMast
1ab29 65 72 29 3b 0a 20 20 20 20 7a 4d 61 73 74 65 72 er);. zMaster
1ab2a 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 72 63 = 0;. if( rc
1ab2b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
1ab2c 20 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 rc;. }..
1ab2d 2f 2a 20 41 6c 6c 20 66 69 6c 65 73 20 61 6e 64 /* All files and
1ab2e 20 64 69 72 65 63 74 6f 72 69 65 73 20 68 61 76 directories hav
1ab2f 65 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 73 e already been s
1ab30 79 6e 63 65 64 2c 20 73 6f 20 74 68 65 20 66 6f ynced, so the fo
1ab31 6c 6c 6f 77 69 6e 67 0a 20 20 20 20 2a 2a 20 63 llowing. ** c
1ab32 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 42 alls to sqlite3B
1ab33 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 54 treeCommitPhaseT
1ab34 77 6f 28 29 20 61 72 65 20 6f 6e 6c 79 20 63 6c wo() are only cl
1ab35 6f 73 69 6e 67 20 66 69 6c 65 73 20 61 6e 64 0a osing files and.
1ab36 20 20 20 20 2a 2a 20 64 65 6c 65 74 69 6e 67 20 ** deleting
1ab37 6f 72 20 74 72 75 6e 63 61 74 69 6e 67 20 6a 6f or truncating jo
1ab38 75 72 6e 61 6c 73 2e 20 49 66 20 73 6f 6d 65 74 urnals. If somet
1ab39 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 20 hing goes wrong
1ab3a 77 68 69 6c 65 0a 20 20 20 20 2a 2a 20 74 68 69 while. ** thi
1ab3b 73 20 69 73 20 68 61 70 70 65 6e 69 6e 67 20 77 s is happening w
1ab3c 65 20 64 6f 6e 27 74 20 72 65 61 6c 6c 79 20 63 e don't really c
1ab3d 61 72 65 2e 20 54 68 65 20 69 6e 74 65 67 72 69 are. The integri
1ab3e 74 79 20 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a ty of the. **
1ab3f 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 transaction is
1ab40 61 6c 72 65 61 64 79 20 67 75 61 72 61 6e 74 65 already guarante
1ab41 65 64 2c 20 62 75 74 20 73 6f 6d 65 20 73 74 72 ed, but some str
1ab42 61 79 20 27 63 6f 6c 64 27 20 6a 6f 75 72 6e 61 ay 'cold' journa
1ab43 6c 73 0a 20 20 20 20 2a 2a 20 6d 61 79 20 62 65 ls. ** may be
1ab44 20 6c 79 69 6e 67 20 61 72 6f 75 6e 64 2e 20 52 lying around. R
1ab45 65 74 75 72 6e 69 6e 67 20 61 6e 20 65 72 72 6f eturning an erro
1ab46 72 20 63 6f 64 65 20 77 6f 6e 27 74 20 68 65 6c r code won't hel
1ab47 70 20 6d 61 74 74 65 72 73 2e 0a 20 20 20 20 2a p matters.. *
1ab48 2f 0a 20 20 20 20 64 69 73 61 62 6c 65 5f 73 69 /. disable_si
1ab49 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 mulated_io_error
1ab4a 73 28 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 s();. sqlite3
1ab4b 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f BeginBenignMallo
1ab4c 63 28 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 c();. for(i=0
1ab4d 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b ; i<db->nDb; i++
1ab4e 29 7b 20 0a 20 20 20 20 20 20 42 74 72 65 65 20 ){ . Btree
1ab4f 2a 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69 *pBt = db->aDb[i
1ab50 5d 2e 70 42 74 3b 0a 20 20 20 20 20 20 69 66 28 ].pBt;. if(
1ab51 20 70 42 74 20 29 7b 0a 20 20 20 20 20 20 20 20 pBt ){.
1ab52 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d sqlite3BtreeComm
1ab53 69 74 50 68 61 73 65 54 77 6f 28 70 42 74 29 3b itPhaseTwo(pBt);
1ab54 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
1ab55 20 20 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e sqlite3EndBen
1ab56 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 ignMalloc();.
1ab57 20 65 6e 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 enable_simulate
1ab58 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29 3b 0a 0a d_io_errors();..
1ab59 20 20 20 20 73 71 6c 69 74 65 33 56 74 61 62 43 sqlite3VtabC
1ab5a 6f 6d 6d 69 74 28 64 62 29 3b 0a 20 20 7d 0a 23 ommit(db);. }.#
1ab5b 65 6e 64 69 66 0a 0a 20 20 72 65 74 75 72 6e 20 endif.. return
1ab5c 72 63 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 54 68 rc;.}../* .** Th
1ab5d 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b is routine check
1ab5e 73 20 74 68 61 74 20 74 68 65 20 73 71 6c 69 74 s that the sqlit
1ab5f 65 33 2e 61 63 74 69 76 65 56 64 62 65 43 6e 74 e3.activeVdbeCnt
1ab60 20 63 6f 75 6e 74 20 76 61 72 69 61 62 6c 65 0a count variable.
1ab61 2a 2a 20 6d 61 74 63 68 65 73 20 74 68 65 20 6e ** matches the n
1ab62 75 6d 62 65 72 20 6f 66 20 76 64 62 65 27 73 20 umber of vdbe's
1ab63 69 6e 20 74 68 65 20 6c 69 73 74 20 73 71 6c 69 in the list sqli
1ab64 74 65 33 2e 70 56 64 62 65 20 74 68 61 74 20 61 te3.pVdbe that a
1ab65 72 65 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 re.** currently
1ab66 61 63 74 69 76 65 2e 20 41 6e 20 61 73 73 65 72 active. An asser
1ab67 74 69 6f 6e 20 66 61 69 6c 73 20 69 66 20 74 68 tion fails if th
1ab68 65 20 74 77 6f 20 63 6f 75 6e 74 73 20 64 6f 20 e two counts do
1ab69 6e 6f 74 20 6d 61 74 63 68 2e 0a 2a 2a 20 54 68 not match..** Th
1ab6a 69 73 20 69 73 20 61 6e 20 69 6e 74 65 72 6e 61 is is an interna
1ab6b 6c 20 73 65 6c 66 2d 63 68 65 63 6b 20 6f 6e 6c l self-check onl
1ab6c 79 20 2d 20 69 74 20 69 73 20 6e 6f 74 20 61 6e y - it is not an
1ab6d 20 65 73 73 65 6e 74 69 61 6c 20 70 72 6f 63 65 essential proce
1ab6e 73 73 69 6e 67 0a 2a 2a 20 73 74 65 70 2e 0a 2a ssing.** step..*
1ab6f 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 20 6e *.** This is a n
1ab70 6f 2d 6f 70 20 69 66 20 4e 44 45 42 55 47 20 69 o-op if NDEBUG i
1ab71 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 23 69 s defined..*/.#i
1ab72 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 73 74 61 fndef NDEBUG.sta
1ab73 74 69 63 20 76 6f 69 64 20 63 68 65 63 6b 41 63 tic void checkAc
1ab74 74 69 76 65 56 64 62 65 43 6e 74 28 73 71 6c 69 tiveVdbeCnt(sqli
1ab75 74 65 33 20 2a 64 62 29 7b 0a 20 20 56 64 62 65 te3 *db){. Vdbe
1ab76 20 2a 70 3b 0a 20 20 69 6e 74 20 63 6e 74 20 3d *p;. int cnt =
1ab77 20 30 3b 0a 20 20 69 6e 74 20 6e 57 72 69 74 65 0;. int nWrite
1ab78 20 3d 20 30 3b 0a 20 20 70 20 3d 20 64 62 2d 3e = 0;. p = db->
1ab79 70 56 64 62 65 3b 0a 20 20 77 68 69 6c 65 28 20 pVdbe;. while(
1ab7a 70 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e p ){. if( p->
1ab7b 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 magic==VDBE_MAGI
1ab7c 43 5f 52 55 4e 20 26 26 20 70 2d 3e 70 63 3e 3d C_RUN && p->pc>=
1ab7d 30 20 29 7b 0a 20 20 20 20 20 20 63 6e 74 2b 2b 0 ){. cnt++
1ab7e 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 72 ;. if( p->r
1ab7f 65 61 64 4f 6e 6c 79 3d 3d 30 20 29 20 6e 57 72 eadOnly==0 ) nWr
1ab80 69 74 65 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 ite++;. }.
1ab81 20 70 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 p = p->pNext;.
1ab82 20 7d 0a 20 20 61 73 73 65 72 74 28 20 63 6e 74 }. assert( cnt
1ab83 3d 3d 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65 ==db->activeVdbe
1ab84 43 6e 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28 Cnt );. assert(
1ab85 20 6e 57 72 69 74 65 3d 3d 64 62 2d 3e 77 72 69 nWrite==db->wri
1ab86 74 65 56 64 62 65 43 6e 74 20 29 3b 0a 7d 0a 23 teVdbeCnt );.}.#
1ab87 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 63 68 65 else.#define che
1ab88 63 6b 41 63 74 69 76 65 56 64 62 65 43 6e 74 28 ckActiveVdbeCnt(
1ab89 78 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a x).#endif../*.**
1ab8a 20 46 6f 72 20 65 76 65 72 79 20 42 74 72 65 65 For every Btree
1ab8b 20 74 68 61 74 20 69 6e 20 64 61 74 61 62 61 73 that in databas
1ab8c 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 64 62 20 e connection db
1ab8d 77 68 69 63 68 20 0a 2a 2a 20 68 61 73 20 62 65 which .** has be
1ab8e 65 6e 20 6d 6f 64 69 66 69 65 64 2c 20 22 74 72 en modified, "tr
1ab8f 69 70 22 20 6f 72 20 69 6e 76 61 6c 69 64 61 74 ip" or invalidat
1ab90 65 20 65 61 63 68 20 63 75 72 73 6f 72 20 69 6e e each cursor in
1ab91 0a 2a 2a 20 74 68 61 74 20 42 74 72 65 65 20 6d .** that Btree m
1ab92 69 67 68 74 20 68 61 76 65 20 62 65 65 6e 20 6d ight have been m
1ab93 6f 64 69 66 69 65 64 20 73 6f 20 74 68 61 74 20 odified so that
1ab94 74 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 63 61 the cursor.** ca
1ab95 6e 20 6e 65 76 65 72 20 62 65 20 75 73 65 64 20 n never be used
1ab96 61 67 61 69 6e 2e 20 20 54 68 69 73 20 68 61 70 again. This hap
1ab97 70 65 6e 73 20 77 68 65 6e 20 61 20 72 6f 6c 6c pens when a roll
1ab98 62 61 63 6b 0a 2a 2a 2a 20 6f 63 63 75 72 73 2e back.*** occurs.
1ab99 20 20 57 65 20 68 61 76 65 20 74 6f 20 74 72 69 We have to tri
1ab9a 70 20 61 6c 6c 20 74 68 65 20 6f 74 68 65 72 20 p all the other
1ab9b 63 75 72 73 6f 72 73 2c 20 65 76 65 6e 0a 2a 2a cursors, even.**
1ab9c 20 63 75 72 73 6f 72 20 66 72 6f 6d 20 6f 74 68 cursor from oth
1ab9d 65 72 20 56 4d 73 20 69 6e 20 64 69 66 66 65 72 er VMs in differ
1ab9e 65 6e 74 20 64 61 74 61 62 61 73 65 20 63 6f 6e ent database con
1ab9f 6e 65 63 74 69 6f 6e 73 2c 0a 2a 2a 20 73 6f 20 nections,.** so
1aba0 74 68 61 74 20 6e 6f 6e 65 20 6f 66 20 74 68 65 that none of the
1aba1 6d 20 74 72 79 20 74 6f 20 75 73 65 20 74 68 65 m try to use the
1aba2 20 64 61 74 61 20 61 74 20 77 68 69 63 68 20 74 data at which t
1aba3 68 65 79 0a 2a 2a 20 77 65 72 65 20 70 6f 69 6e hey.** were poin
1aba4 74 69 6e 67 20 61 6e 64 20 77 68 69 63 68 20 6e ting and which n
1aba5 6f 77 20 6d 61 79 20 68 61 76 65 20 62 65 65 6e ow may have been
1aba6 20 63 68 61 6e 67 65 64 20 64 75 65 0a 2a 2a 20 changed due.**
1aba7 74 6f 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 2e to the rollback.
1aba8 0a 2a 2a 0a 2a 2a 20 52 65 6d 65 6d 62 65 72 20 .**.** Remember
1aba9 74 68 61 74 20 61 20 72 6f 6c 6c 62 61 63 6b 20 that a rollback
1abaa 63 61 6e 20 64 65 6c 65 74 65 20 74 61 62 6c 65 can delete table
1abab 73 20 63 6f 6d 70 6c 65 74 65 20 61 6e 64 0a 2a s complete and.*
1abac 2a 20 72 65 6f 72 64 65 72 20 72 6f 6f 74 70 61 * reorder rootpa
1abad 67 65 73 2e 20 20 53 6f 20 69 74 20 69 73 20 6e ges. So it is n
1abae 6f 74 20 73 75 66 66 69 63 69 65 6e 74 20 6a 75 ot sufficient ju
1abaf 73 74 20 74 6f 20 73 61 76 65 0a 2a 2a 20 74 68 st to save.** th
1abb0 65 20 73 74 61 74 65 20 6f 66 20 74 68 65 20 63 e state of the c
1abb1 75 72 73 6f 72 2e 20 20 57 65 20 68 61 76 65 20 ursor. We have
1abb2 74 6f 20 69 6e 76 61 6c 69 64 61 74 65 20 74 68 to invalidate th
1abb3 65 20 63 75 72 73 6f 72 0a 2a 2a 20 73 6f 20 74 e cursor.** so t
1abb4 68 61 74 20 69 74 20 69 73 20 6e 65 76 65 72 20 hat it is never
1abb5 75 73 65 64 20 61 67 61 69 6e 2e 0a 2a 2f 0a 73 used again..*/.s
1abb6 74 61 74 69 63 20 76 6f 69 64 20 69 6e 76 61 6c tatic void inval
1abb7 69 64 61 74 65 43 75 72 73 6f 72 73 4f 6e 4d 6f idateCursorsOnMo
1abb8 64 69 66 69 65 64 42 74 72 65 65 73 28 73 71 6c difiedBtrees(sql
1abb9 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74 ite3 *db){. int
1abba 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 i;. for(i=0; i
1abbb 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a <db->nDb; i++){.
1abbc 20 20 20 20 42 74 72 65 65 20 2a 70 20 3d 20 64 Btree *p = d
1abbd 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 b->aDb[i].pBt;.
1abbe 20 20 20 69 66 28 20 70 20 26 26 20 73 71 6c 69 if( p && sqli
1abbf 74 65 33 42 74 72 65 65 49 73 49 6e 54 72 61 6e te3BtreeIsInTran
1abc0 73 28 70 29 20 29 7b 0a 20 20 20 20 20 20 73 71 s(p) ){. sq
1abc1 6c 69 74 65 33 42 74 72 65 65 54 72 69 70 41 6c lite3BtreeTripAl
1abc2 6c 43 75 72 73 6f 72 73 28 70 2c 20 53 51 4c 49 lCursors(p, SQLI
1abc3 54 45 5f 41 42 4f 52 54 29 3b 0a 20 20 20 20 7d TE_ABORT);. }
1abc4 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 . }.}../*.** If
1abc5 20 74 68 65 20 56 64 62 65 20 70 61 73 73 65 64 the Vdbe passed
1abc6 20 61 73 20 74 68 65 20 66 69 72 73 74 20 61 72 as the first ar
1abc7 67 75 6d 65 6e 74 20 6f 70 65 6e 65 64 20 61 20 gument opened a
1abc8 73 74 61 74 65 6d 65 6e 74 2d 74 72 61 6e 73 61 statement-transa
1abc9 63 74 69 6f 6e 2c 0a 2a 2a 20 63 6c 6f 73 65 20 ction,.** close
1abca 69 74 20 6e 6f 77 2e 20 41 72 67 75 6d 65 6e 74 it now. Argument
1abcb 20 65 4f 70 20 6d 75 73 74 20 62 65 20 65 69 74 eOp must be eit
1abcc 68 65 72 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f her SAVEPOINT_RO
1abcd 4c 4c 42 41 43 4b 20 6f 72 0a 2a 2a 20 53 41 56 LLBACK or.** SAV
1abce 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 2e 20 EPOINT_RELEASE.
1abcf 49 66 20 69 74 20 69 73 20 53 41 56 45 50 4f 49 If it is SAVEPOI
1abd0 4e 54 5f 52 4f 4c 4c 42 41 43 4b 2c 20 74 68 65 NT_ROLLBACK, the
1abd1 6e 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 0a n the statement.
1abd2 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 ** transaction i
1abd3 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 49 s rolled back. I
1abd4 66 20 65 4f 70 20 69 73 20 53 41 56 45 50 4f 49 f eOp is SAVEPOI
1abd5 4e 54 5f 52 45 4c 45 41 53 45 2c 20 74 68 65 6e NT_RELEASE, then
1abd6 20 74 68 65 20 0a 2a 2a 20 73 74 61 74 65 6d 65 the .** stateme
1abd7 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 nt transaction i
1abd8 73 20 63 6f 6d 6d 74 74 65 64 2e 0a 2a 2a 0a 2a s commtted..**.*
1abd9 2a 20 49 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 * If an IO error
1abda 20 6f 63 63 75 72 73 2c 20 61 6e 20 53 51 4c 49 occurs, an SQLI
1abdb 54 45 5f 49 4f 45 52 52 5f 58 58 58 20 65 72 72 TE_IOERR_XXX err
1abdc 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 or code is retur
1abdd 6e 65 64 2e 20 0a 2a 2a 20 4f 74 68 65 72 77 69 ned. .** Otherwi
1abde 73 65 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f se SQLITE_OK..*/
1abdf 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1abe0 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 43 int sqlite3VdbeC
1abe1 6c 6f 73 65 53 74 61 74 65 6d 65 6e 74 28 56 64 loseStatement(Vd
1abe2 62 65 20 2a 70 2c 20 69 6e 74 20 65 4f 70 29 7b be *p, int eOp){
1abe3 0a 20 20 73 71 6c 69 74 65 33 20 2a 63 6f 6e 73 . sqlite3 *cons
1abe4 74 20 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 t db = p->db;.
1abe5 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
1abe6 4f 4b 3b 0a 0a 20 20 2f 2a 20 49 66 20 70 2d 3e OK;.. /* If p->
1abe7 69 53 74 61 74 65 6d 65 6e 74 20 69 73 20 67 72 iStatement is gr
1abe8 65 61 74 65 72 20 74 68 61 6e 20 7a 65 72 6f 2c eater than zero,
1abe9 20 74 68 65 6e 20 74 68 69 73 20 56 64 62 65 20 then this Vdbe
1abea 6f 70 65 6e 65 64 20 61 20 0a 20 20 2a 2a 20 73 opened a . ** s
1abeb 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 tatement transac
1abec 74 69 6f 6e 20 74 68 61 74 20 73 68 6f 75 6c 64 tion that should
1abed 20 62 65 20 63 6c 6f 73 65 64 20 68 65 72 65 2e be closed here.
1abee 20 54 68 65 20 6f 6e 6c 79 20 65 78 63 65 70 74 The only except
1abef 69 6f 6e 0a 20 20 2a 2a 20 69 73 20 74 68 61 74 ion. ** is that
1abf0 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 6d 61 79 an IO error may
1abf1 20 68 61 76 65 20 6f 63 63 75 72 65 64 2c 20 63 have occured, c
1abf2 61 75 73 69 6e 67 20 61 6e 20 65 6d 65 72 67 65 ausing an emerge
1abf3 6e 63 79 20 72 6f 6c 6c 62 61 63 6b 2e 0a 20 20 ncy rollback..
1abf4 2a 2a 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 ** In this case
1abf5 28 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 3d (db->nStatement=
1abf6 3d 30 29 2c 20 61 6e 64 20 74 68 65 72 65 20 69 =0), and there i
1abf7 73 20 6e 6f 74 68 69 6e 67 20 74 6f 20 64 6f 2e s nothing to do.
1abf8 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e . */. if( db->
1abf9 6e 53 74 61 74 65 6d 65 6e 74 20 26 26 20 70 2d nStatement && p-
1abfa 3e 69 53 74 61 74 65 6d 65 6e 74 20 29 7b 0a 20 >iStatement ){.
1abfb 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 63 6f int i;. co
1abfc 6e 73 74 20 69 6e 74 20 69 53 61 76 65 70 6f 69 nst int iSavepoi
1abfd 6e 74 20 3d 20 70 2d 3e 69 53 74 61 74 65 6d 65 nt = p->iStateme
1abfe 6e 74 2d 31 3b 0a 0a 20 20 20 20 61 73 73 65 72 nt-1;.. asser
1abff 74 28 20 65 4f 70 3d 3d 53 41 56 45 50 4f 49 4e t( eOp==SAVEPOIN
1ac00 54 5f 52 4f 4c 4c 42 41 43 4b 20 7c 7c 20 65 4f T_ROLLBACK || eO
1ac01 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c p==SAVEPOINT_REL
1ac02 45 41 53 45 29 3b 0a 20 20 20 20 61 73 73 65 72 EASE);. asser
1ac03 74 28 20 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e t( db->nStatemen
1ac04 74 3e 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 t>0 );. asser
1ac05 74 28 20 70 2d 3e 69 53 74 61 74 65 6d 65 6e 74 t( p->iStatement
1ac06 3d 3d 28 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e ==(db->nStatemen
1ac07 74 2b 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 t+db->nSavepoint
1ac08 29 20 29 3b 0a 0a 20 20 20 20 66 6f 72 28 69 3d ) );.. for(i=
1ac09 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 0; i<db->nDb; i+
1ac0a 2b 29 7b 20 0a 20 20 20 20 20 20 69 6e 74 20 72 +){ . int r
1ac0b 63 32 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a c2 = SQLITE_OK;.
1ac0c 20 20 20 20 20 20 42 74 72 65 65 20 2a 70 42 74 Btree *pBt
1ac0d 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 = db->aDb[i].pB
1ac0e 74 3b 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 t;. if( pBt
1ac0f 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 ){. if(
1ac10 65 4f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 eOp==SAVEPOINT_R
1ac11 4f 4c 4c 42 41 43 4b 20 29 7b 0a 20 20 20 20 20 OLLBACK ){.
1ac12 20 20 20 20 20 72 63 32 20 3d 20 73 71 6c 69 74 rc2 = sqlit
1ac13 65 33 42 74 72 65 65 53 61 76 65 70 6f 69 6e 74 e3BtreeSavepoint
1ac14 28 70 42 74 2c 20 53 41 56 45 50 4f 49 4e 54 5f (pBt, SAVEPOINT_
1ac15 52 4f 4c 4c 42 41 43 4b 2c 20 69 53 61 76 65 70 ROLLBACK, iSavep
1ac16 6f 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 7d oint);. }
1ac17 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 32 . if( rc2
1ac18 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
1ac19 20 20 20 20 20 20 20 20 20 72 63 32 20 3d 20 73 rc2 = s
1ac1a 71 6c 69 74 65 33 42 74 72 65 65 53 61 76 65 70 qlite3BtreeSavep
1ac1b 6f 69 6e 74 28 70 42 74 2c 20 53 41 56 45 50 4f oint(pBt, SAVEPO
1ac1c 49 4e 54 5f 52 45 4c 45 41 53 45 2c 20 69 53 61 INT_RELEASE, iSa
1ac1d 76 65 70 6f 69 6e 74 29 3b 0a 20 20 20 20 20 20 vepoint);.
1ac1e 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 }. if(
1ac1f 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
1ac20 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 . rc =
1ac21 72 63 32 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 rc2;. }.
1ac22 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 }. }.
1ac23 20 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 2d db->nStatement-
1ac24 2d 3b 0a 20 20 20 20 70 2d 3e 69 53 74 61 74 65 -;. p->iState
1ac25 6d 65 6e 74 20 3d 20 30 3b 0a 0a 20 20 20 20 2f ment = 0;.. /
1ac26 2a 20 49 66 20 74 68 65 20 73 74 61 74 65 6d 65 * If the stateme
1ac27 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 nt transaction i
1ac28 73 20 62 65 69 6e 67 20 72 6f 6c 6c 65 64 20 62 s being rolled b
1ac29 61 63 6b 2c 20 61 6c 73 6f 20 72 65 73 74 6f 72 ack, also restor
1ac2a 65 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 64 61 e the . ** da
1ac2b 74 61 62 61 73 65 20 68 61 6e 64 6c 65 73 20 64 tabase handles d
1ac2c 65 66 65 72 72 65 64 20 63 6f 6e 73 74 72 61 69 eferred constrai
1ac2d 6e 74 20 63 6f 75 6e 74 65 72 20 74 6f 20 74 68 nt counter to th
1ac2e 65 20 76 61 6c 75 65 20 69 74 20 68 61 64 20 77 e value it had w
1ac2f 68 65 6e 20 0a 20 20 20 20 2a 2a 20 74 68 65 20 hen . ** the
1ac30 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 statement transa
1ac31 63 74 69 6f 6e 20 77 61 73 20 6f 70 65 6e 65 64 ction was opened
1ac32 2e 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 65 4f . */. if( eO
1ac33 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c p==SAVEPOINT_ROL
1ac34 4c 42 41 43 4b 20 29 7b 0a 20 20 20 20 20 20 64 LBACK ){. d
1ac35 62 2d 3e 6e 44 65 66 65 72 72 65 64 43 6f 6e 73 b->nDeferredCons
1ac36 20 3d 20 70 2d 3e 6e 53 74 6d 74 44 65 66 43 6f = p->nStmtDefCo
1ac37 6e 73 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 ns;. }. }.
1ac38 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
1ac39 0a 2a 2a 20 49 66 20 53 51 4c 69 74 65 20 69 73 .** If SQLite is
1ac3a 20 63 6f 6d 70 69 6c 65 64 20 74 6f 20 73 75 70 compiled to sup
1ac3b 70 6f 72 74 20 73 68 61 72 65 64 2d 63 61 63 68 port shared-cach
1ac3c 65 20 6d 6f 64 65 20 61 6e 64 20 74 6f 20 62 65 e mode and to be
1ac3d 20 74 68 72 65 61 64 73 61 66 65 2c 0a 2a 2a 20 threadsafe,.**
1ac3e 74 68 69 73 20 72 6f 75 74 69 6e 65 20 6f 62 74 this routine obt
1ac3f 61 69 6e 73 20 74 68 65 20 6d 75 74 65 78 20 61 ains the mutex a
1ac40 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 65 ssociated with e
1ac41 61 63 68 20 42 74 53 68 61 72 65 64 20 73 74 72 ach BtShared str
1ac42 75 63 74 75 72 65 0a 2a 2a 20 74 68 61 74 20 6d ucture.** that m
1ac43 61 79 20 62 65 20 61 63 63 65 73 73 65 64 20 62 ay be accessed b
1ac44 79 20 74 68 65 20 56 4d 20 70 61 73 73 65 64 20 y the VM passed
1ac45 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 2e 20 as an argument.
1ac46 49 6e 20 64 6f 69 6e 67 20 73 6f 20 69 74 0a 2a In doing so it.*
1ac47 2a 20 73 65 74 73 20 74 68 65 20 42 74 53 68 61 * sets the BtSha
1ac48 72 65 64 2e 64 62 20 6d 65 6d 62 65 72 20 6f 66 red.db member of
1ac49 20 65 61 63 68 20 6f 66 20 74 68 65 20 42 74 53 each of the BtS
1ac4a 68 61 72 65 64 20 73 74 72 75 63 74 75 72 65 73 hared structures
1ac4b 2c 20 65 6e 73 75 72 69 6e 67 0a 2a 2a 20 74 68 , ensuring.** th
1ac4c 61 74 20 74 68 65 20 63 6f 72 72 65 63 74 20 62 at the correct b
1ac4d 75 73 79 2d 68 61 6e 64 6c 65 72 20 63 61 6c 6c usy-handler call
1ac4e 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20 back is invoked
1ac4f 69 66 20 72 65 71 75 69 72 65 64 2e 0a 2a 2a 0a if required..**.
1ac50 2a 2a 20 49 66 20 53 51 4c 69 74 65 20 69 73 20 ** If SQLite is
1ac51 6e 6f 74 20 74 68 72 65 61 64 73 61 66 65 20 62 not threadsafe b
1ac52 75 74 20 64 6f 65 73 20 73 75 70 70 6f 72 74 20 ut does support
1ac53 73 68 61 72 65 64 2d 63 61 63 68 65 20 6d 6f 64 shared-cache mod
1ac54 65 2c 20 74 68 65 6e 0a 2a 2a 20 73 71 6c 69 74 e, then.** sqlit
1ac55 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 e3BtreeEnterAll(
1ac56 29 20 69 73 20 69 6e 76 6f 6b 65 64 20 74 6f 20 ) is invoked to
1ac57 73 65 74 20 74 68 65 20 42 74 53 68 61 72 65 64 set the BtShared
1ac58 2e 64 62 20 76 61 72 69 61 62 6c 65 73 0a 2a 2a .db variables.**
1ac59 20 6f 66 20 61 6c 6c 20 6f 66 20 42 74 53 68 61 of all of BtSha
1ac5a 72 65 64 20 73 74 72 75 63 74 75 72 65 73 20 61 red structures a
1ac5b 63 63 65 73 73 69 62 6c 65 20 76 69 61 20 74 68 ccessible via th
1ac5c 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c e database handl
1ac5d 65 20 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 64 e .** associated
1ac5e 20 77 69 74 68 20 74 68 65 20 56 4d 2e 20 4f 66 with the VM. Of
1ac5f 20 63 6f 75 72 73 65 20 6f 6e 6c 79 20 61 20 73 course only a s
1ac60 75 62 73 65 74 20 6f 66 20 74 68 65 73 65 20 73 ubset of these s
1ac61 74 72 75 63 74 75 72 65 73 0a 2a 2a 20 77 69 6c tructures.** wil
1ac62 6c 20 62 65 20 61 63 63 65 73 73 65 64 20 62 79 l be accessed by
1ac63 20 74 68 65 20 56 4d 2c 20 61 6e 64 20 77 65 20 the VM, and we
1ac64 63 6f 75 6c 64 20 75 73 65 20 56 64 62 65 2e 62 could use Vdbe.b
1ac65 74 72 65 65 4d 61 73 6b 20 74 6f 20 66 69 67 75 treeMask to figu
1ac66 72 65 0a 2a 2a 20 74 68 61 74 20 73 75 62 73 65 re.** that subse
1ac67 74 20 6f 75 74 2c 20 62 75 74 20 74 68 65 72 65 t out, but there
1ac68 20 69 73 20 6e 6f 20 61 64 76 61 6e 74 61 67 65 is no advantage
1ac69 20 74 6f 20 64 6f 69 6e 67 20 73 6f 2e 0a 2a 2a to doing so..**
1ac6a 0a 2a 2a 20 49 66 20 53 51 4c 69 74 65 20 69 73 .** If SQLite is
1ac6b 20 6e 6f 74 20 74 68 72 65 61 64 73 61 66 65 20 not threadsafe
1ac6c 61 6e 64 20 64 6f 65 73 20 6e 6f 74 20 73 75 70 and does not sup
1ac6d 70 6f 72 74 20 73 68 61 72 65 64 2d 63 61 63 68 port shared-cach
1ac6e 65 20 6d 6f 64 65 2c 20 74 68 69 73 0a 2a 2a 20 e mode, this.**
1ac6f 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f function is a no
1ac70 2d 6f 70 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 -op..*/.#ifndef
1ac71 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 SQLITE_OMIT_SHAR
1ac72 45 44 5f 43 41 43 48 45 0a 53 51 4c 49 54 45 5f ED_CACHE.SQLITE_
1ac73 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
1ac74 69 74 65 33 56 64 62 65 4d 75 74 65 78 41 72 72 ite3VdbeMutexArr
1ac75 61 79 45 6e 74 65 72 28 56 64 62 65 20 2a 70 29 ayEnter(Vdbe *p)
1ac76 7b 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 {.#if SQLITE_THR
1ac77 45 41 44 53 41 46 45 0a 20 20 73 71 6c 69 74 65 EADSAFE. sqlite
1ac78 33 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 3BtreeMutexArray
1ac79 45 6e 74 65 72 28 26 70 2d 3e 61 4d 75 74 65 78 Enter(&p->aMutex
1ac7a 29 3b 0a 23 65 6c 73 65 0a 20 20 73 71 6c 69 74 );.#else. sqlit
1ac7b 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 e3BtreeEnterAll(
1ac7c 70 2d 3e 64 62 29 3b 0a 23 65 6e 64 69 66 0a 7d p->db);.#endif.}
1ac7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 .#endif../*.** T
1ac7e 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
1ac7f 63 61 6c 6c 65 64 20 77 68 65 6e 20 61 20 74 72 called when a tr
1ac80 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e 65 64 ansaction opened
1ac81 20 62 79 20 74 68 65 20 64 61 74 61 62 61 73 65 by the database
1ac82 20 0a 2a 2a 20 68 61 6e 64 6c 65 20 61 73 73 6f .** handle asso
1ac83 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 ciated with the
1ac84 56 4d 20 70 61 73 73 65 64 20 61 73 20 61 6e 20 VM passed as an
1ac85 61 72 67 75 6d 65 6e 74 20 69 73 20 61 62 6f 75 argument is abou
1ac86 74 20 74 6f 20 62 65 20 0a 2a 2a 20 63 6f 6d 6d t to be .** comm
1ac87 69 74 74 65 64 2e 20 49 66 20 74 68 65 72 65 20 itted. If there
1ac88 61 72 65 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 are outstanding
1ac89 64 65 66 65 72 72 65 64 20 66 6f 72 65 69 67 6e deferred foreign
1ac8a 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 0a key constraint.
1ac8b 2a 2a 20 76 69 6f 6c 61 74 69 6f 6e 73 2c 20 72 ** violations, r
1ac8c 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 eturn SQLITE_ERR
1ac8d 4f 52 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 53 OR. Otherwise, S
1ac8e 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20 QLITE_OK..**.**
1ac8f 49 66 20 74 68 65 72 65 20 61 72 65 20 6f 75 74 If there are out
1ac90 73 74 61 6e 64 69 6e 67 20 46 4b 20 76 69 6f 6c standing FK viol
1ac91 61 74 69 6f 6e 73 20 61 6e 64 20 74 68 69 73 20 ations and this
1ac92 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 function returns
1ac93 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 45 52 52 4f .** SQLITE_ERRO
1ac94 52 2c 20 73 65 74 20 74 68 65 20 72 65 73 75 6c R, set the resul
1ac95 74 20 6f 66 20 74 68 65 20 56 4d 20 74 6f 20 53 t of the VM to S
1ac96 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 QLITE_CONSTRAINT
1ac97 20 61 6e 64 20 77 72 69 74 65 0a 2a 2a 20 61 6e and write.** an
1ac98 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74 error message t
1ac99 6f 20 69 74 2e 20 54 68 65 6e 20 72 65 74 75 72 o it. Then retur
1ac9a 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e 0a n SQLITE_ERROR..
1ac9b 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 */.#ifndef SQLIT
1ac9c 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b E_OMIT_FOREIGN_K
1ac9d 45 59 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 EY.SQLITE_PRIVAT
1ac9e 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
1ac9f 65 43 68 65 63 6b 46 6b 28 56 64 62 65 20 2a 70 eCheckFk(Vdbe *p
1aca0 2c 20 69 6e 74 20 64 65 66 65 72 72 65 64 29 7b , int deferred){
1aca1 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d . sqlite3 *db =
1aca2 20 70 2d 3e 64 62 3b 0a 20 20 69 66 28 20 28 64 p->db;. if( (d
1aca3 65 66 65 72 72 65 64 20 26 26 20 64 62 2d 3e 6e eferred && db->n
1aca4 44 65 66 65 72 72 65 64 43 6f 6e 73 3e 30 29 20 DeferredCons>0)
1aca5 7c 7c 20 28 21 64 65 66 65 72 72 65 64 20 26 26 || (!deferred &&
1aca6 20 70 2d 3e 6e 46 6b 43 6f 6e 73 74 72 61 69 6e p->nFkConstrain
1aca7 74 3e 30 29 20 29 7b 0a 20 20 20 20 70 2d 3e 72 t>0) ){. p->r
1aca8 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 c = SQLITE_CONST
1aca9 52 41 49 4e 54 3b 0a 20 20 20 20 70 2d 3e 65 72 RAINT;. p->er
1acaa 72 6f 72 41 63 74 69 6f 6e 20 3d 20 4f 45 5f 41 rorAction = OE_A
1acab 62 6f 72 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 bort;. sqlite
1acac 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 3SetString(&p->z
1acad 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 66 6f 72 ErrMsg, db, "for
1acae 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 eign key constra
1acaf 69 6e 74 20 66 61 69 6c 65 64 22 29 3b 0a 20 20 int failed");.
1acb0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
1acb1 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 72 65 74 ERROR;. }. ret
1acb2 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
1acb3 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 .#endif../*.** T
1acb4 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 his routine is c
1acb5 61 6c 6c 65 64 20 74 68 65 20 77 68 65 6e 20 61 alled the when a
1acb6 20 56 44 42 45 20 74 72 69 65 73 20 74 6f 20 68 VDBE tries to h
1acb7 61 6c 74 2e 20 20 49 66 20 74 68 65 20 56 44 42 alt. If the VDB
1acb8 45 0a 2a 2a 20 68 61 73 20 6d 61 64 65 20 63 68 E.** has made ch
1acb9 61 6e 67 65 73 20 61 6e 64 20 69 73 20 69 6e 20 anges and is in
1acba 61 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 2c autocommit mode,
1acbb 20 74 68 65 6e 20 63 6f 6d 6d 69 74 20 74 68 6f then commit tho
1acbc 73 65 0a 2a 2a 20 63 68 61 6e 67 65 73 2e 20 20 se.** changes.
1acbd 49 66 20 61 20 72 6f 6c 6c 62 61 63 6b 20 69 73 If a rollback is
1acbe 20 6e 65 65 64 65 64 2c 20 74 68 65 6e 20 64 6f needed, then do
1acbf 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a the rollback..*
1acc0 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
1acc1 65 20 69 73 20 74 68 65 20 6f 6e 6c 79 20 77 61 e is the only wa
1acc2 79 20 74 6f 20 6d 6f 76 65 20 74 68 65 20 73 74 y to move the st
1acc3 61 74 65 20 6f 66 20 61 20 56 4d 20 66 72 6f 6d ate of a VM from
1acc4 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 .** SQLITE_MAGIC
1acc5 5f 52 55 4e 20 74 6f 20 53 51 4c 49 54 45 5f 4d _RUN to SQLITE_M
1acc6 41 47 49 43 5f 48 41 4c 54 2e 20 20 49 74 20 69 AGIC_HALT. It i
1acc7 73 20 68 61 72 6d 6c 65 73 73 20 74 6f 0a 2a 2a s harmless to.**
1acc8 20 63 61 6c 6c 20 74 68 69 73 20 6f 6e 20 61 20 call this on a
1acc9 56 4d 20 74 68 61 74 20 69 73 20 69 6e 20 74 68 VM that is in th
1acca 65 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 48 e SQLITE_MAGIC_H
1accb 41 4c 54 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a ALT state..**.**
1accc 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 Return an error
1accd 20 63 6f 64 65 2e 20 20 49 66 20 74 68 65 20 63 code. If the c
1acce 6f 6d 6d 69 74 20 63 6f 75 6c 64 20 6e 6f 74 20 ommit could not
1accf 63 6f 6d 70 6c 65 74 65 20 62 65 63 61 75 73 65 complete because
1acd0 20 6f 66 0a 2a 2a 20 6c 6f 63 6b 20 63 6f 6e 74 of.** lock cont
1acd1 65 6e 74 69 6f 6e 2c 20 72 65 74 75 72 6e 20 53 ention, return S
1acd2 51 4c 49 54 45 5f 42 55 53 59 2e 20 20 49 66 20 QLITE_BUSY. If
1acd3 53 51 4c 49 54 45 5f 42 55 53 59 20 69 73 20 72 SQLITE_BUSY is r
1acd4 65 74 75 72 6e 65 64 2c 20 69 74 0a 2a 2a 20 6d eturned, it.** m
1acd5 65 61 6e 73 20 74 68 65 20 63 6c 6f 73 65 20 64 eans the close d
1acd6 69 64 20 6e 6f 74 20 68 61 70 70 65 6e 20 61 6e id not happen an
1acd7 64 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 65 d needs to be re
1acd8 70 65 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 peated..*/.SQLIT
1acd9 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
1acda 6c 69 74 65 33 56 64 62 65 48 61 6c 74 28 56 64 lite3VdbeHalt(Vd
1acdb 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 be *p){. int rc
1acdc 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1acdd 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 /* Use
1acde 64 20 74 6f 20 73 74 6f 72 65 20 74 72 61 6e 73 d to store trans
1acdf 69 65 6e 74 20 72 65 74 75 72 6e 20 63 6f 64 65 ient return code
1ace0 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a s */. sqlite3 *
1ace1 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 0a 20 20 2f db = p->db;.. /
1ace2 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
1ace3 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 6c 6f 67 contains the log
1ace4 69 63 20 74 68 61 74 20 64 65 74 65 72 6d 69 6e ic that determin
1ace5 65 73 20 69 66 20 61 20 73 74 61 74 65 6d 65 6e es if a statemen
1ace6 74 20 6f 72 0a 20 20 2a 2a 20 74 72 61 6e 73 61 t or. ** transa
1ace7 63 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 63 6f ction will be co
1ace8 6d 6d 69 74 74 65 64 20 6f 72 20 72 6f 6c 6c 65 mmitted or rolle
1ace9 64 20 62 61 63 6b 20 61 73 20 61 20 72 65 73 75 d back as a resu
1acea 6c 74 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 65 lt of the. ** e
1aceb 78 65 63 75 74 69 6f 6e 20 6f 66 20 74 68 69 73 xecution of this
1acec 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 virtual machine
1aced 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 . . **. ** If
1acee 61 6e 79 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f any of the follo
1acef 77 69 6e 67 20 65 72 72 6f 72 73 20 6f 63 63 75 wing errors occu
1acf0 72 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 r:. **. **
1acf1 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a 20 20 SQLITE_NOMEM.
1acf2 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 49 4f ** SQLITE_IO
1acf3 45 52 52 0a 20 20 2a 2a 20 20 20 20 20 53 51 4c ERR. ** SQL
1acf4 49 54 45 5f 46 55 4c 4c 0a 20 20 2a 2a 20 20 20 ITE_FULL. **
1acf5 20 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 SQLITE_INTERRU
1acf6 50 54 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 PT. **. ** The
1acf7 6e 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 63 n the internal c
1acf8 61 63 68 65 20 6d 69 67 68 74 20 68 61 76 65 20 ache might have
1acf9 62 65 65 6e 20 6c 65 66 74 20 69 6e 20 61 6e 20 been left in an
1acfa 69 6e 63 6f 6e 73 69 73 74 65 6e 74 0a 20 20 2a inconsistent. *
1acfb 2a 20 73 74 61 74 65 2e 20 20 57 65 20 6e 65 65 * state. We nee
1acfc 64 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 74 68 d to rollback th
1acfd 65 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e e statement tran
1acfe 73 61 63 74 69 6f 6e 2c 20 69 66 20 74 68 65 72 saction, if ther
1acff 65 20 69 73 0a 20 20 2a 2a 20 6f 6e 65 2c 20 6f e is. ** one, o
1ad00 72 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 74 r the complete t
1ad01 72 61 6e 73 61 63 74 69 6f 6e 20 69 66 20 74 68 ransaction if th
1ad02 65 72 65 20 69 73 20 6e 6f 20 73 74 61 74 65 6d ere is no statem
1ad03 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e ent transaction.
1ad04 0a 20 20 2a 2f 0a 0a 20 20 69 66 28 20 70 2d 3e . */.. if( p->
1ad05 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
1ad06 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20 ){. p->rc =
1ad07 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
1ad08 7d 0a 20 20 63 6c 6f 73 65 41 6c 6c 43 75 72 73 }. closeAllCurs
1ad09 6f 72 73 28 70 29 3b 0a 20 20 69 66 28 20 70 2d ors(p);. if( p-
1ad0a 3e 6d 61 67 69 63 21 3d 56 44 42 45 5f 4d 41 47 >magic!=VDBE_MAG
1ad0b 49 43 5f 52 55 4e 20 29 7b 0a 20 20 20 20 72 65 IC_RUN ){. re
1ad0c 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
1ad0d 20 20 7d 0a 20 20 63 68 65 63 6b 41 63 74 69 76 }. checkActiv
1ad0e 65 56 64 62 65 43 6e 74 28 64 62 29 3b 0a 0a 20 eVdbeCnt(db);..
1ad0f 20 2f 2a 20 4e 6f 20 63 6f 6d 6d 69 74 20 6f 72 /* No commit or
1ad10 20 72 6f 6c 6c 62 61 63 6b 20 6e 65 65 64 65 64 rollback needed
1ad11 20 69 66 20 74 68 65 20 70 72 6f 67 72 61 6d 20 if the program
1ad12 6e 65 76 65 72 20 73 74 61 72 74 65 64 20 2a 2f never started */
1ad13 0a 20 20 69 66 28 20 70 2d 3e 70 63 3e 3d 30 20 . if( p->pc>=0
1ad14 29 7b 0a 20 20 20 20 69 6e 74 20 6d 72 63 3b 20 ){. int mrc;
1ad15 20 20 2f 2a 20 50 72 69 6d 61 72 79 20 65 72 72 /* Primary err
1ad16 6f 72 20 63 6f 64 65 20 66 72 6f 6d 20 70 2d 3e or code from p->
1ad17 72 63 20 2a 2f 0a 20 20 20 20 69 6e 74 20 65 53 rc */. int eS
1ad18 74 61 74 65 6d 65 6e 74 4f 70 20 3d 20 30 3b 0a tatementOp = 0;.
1ad19 20 20 20 20 69 6e 74 20 69 73 53 70 65 63 69 61 int isSpecia
1ad1a 6c 45 72 72 6f 72 3b 20 20 20 20 20 20 20 20 20 lError;
1ad1b 20 20 20 2f 2a 20 53 65 74 20 74 6f 20 74 72 75 /* Set to tru
1ad1c 65 20 69 66 20 61 20 27 73 70 65 63 69 61 6c 27 e if a 'special'
1ad1d 20 65 72 72 6f 72 20 2a 2f 0a 0a 20 20 20 20 2f error */.. /
1ad1e 2a 20 4c 6f 63 6b 20 61 6c 6c 20 62 74 72 65 65 * Lock all btree
1ad1f 73 20 75 73 65 64 20 62 79 20 74 68 65 20 73 74 s used by the st
1ad20 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 20 20 73 atement */. s
1ad21 71 6c 69 74 65 33 56 64 62 65 4d 75 74 65 78 41 qlite3VdbeMutexA
1ad22 72 72 61 79 45 6e 74 65 72 28 70 29 3b 0a 0a 20 rrayEnter(p);..
1ad23 20 20 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72 20 /* Check for
1ad24 6f 6e 65 20 6f 66 20 74 68 65 20 73 70 65 63 69 one of the speci
1ad25 61 6c 20 65 72 72 6f 72 73 20 2a 2f 0a 20 20 20 al errors */.
1ad26 20 6d 72 63 20 3d 20 70 2d 3e 72 63 20 26 20 30 mrc = p->rc & 0
1ad27 78 66 66 3b 0a 20 20 20 20 61 73 73 65 72 74 28 xff;. assert(
1ad28 20 70 2d 3e 72 63 21 3d 53 51 4c 49 54 45 5f 49 p->rc!=SQLITE_I
1ad29 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 20 29 3b 20 OERR_BLOCKED );
1ad2a 20 2f 2a 20 54 68 69 73 20 65 72 72 6f 72 20 6e /* This error n
1ad2b 6f 20 6c 6f 6e 67 65 72 20 65 78 69 73 74 73 20 o longer exists
1ad2c 2a 2f 0a 20 20 20 20 69 73 53 70 65 63 69 61 6c */. isSpecial
1ad2d 45 72 72 6f 72 20 3d 20 6d 72 63 3d 3d 53 51 4c Error = mrc==SQL
1ad2e 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 6d 72 63 ITE_NOMEM || mrc
1ad2f 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 0a 20 ==SQLITE_IOERR.
1ad30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ad31 20 20 20 20 7c 7c 20 6d 72 63 3d 3d 53 51 4c 49 || mrc==SQLI
1ad32 54 45 5f 49 4e 54 45 52 52 55 50 54 20 7c 7c 20 TE_INTERRUPT ||
1ad33 6d 72 63 3d 3d 53 51 4c 49 54 45 5f 46 55 4c 4c mrc==SQLITE_FULL
1ad34 3b 0a 20 20 20 20 69 66 28 20 69 73 53 70 65 63 ;. if( isSpec
1ad35 69 61 6c 45 72 72 6f 72 20 29 7b 0a 20 20 20 20 ialError ){.
1ad36 20 20 2f 2a 20 49 66 20 74 68 65 20 71 75 65 72 /* If the quer
1ad37 79 20 77 61 73 20 72 65 61 64 2d 6f 6e 6c 79 2c y was read-only,
1ad38 20 77 65 20 6e 65 65 64 20 64 6f 20 6e 6f 20 72 we need do no r
1ad39 6f 6c 6c 62 61 63 6b 20 61 74 20 61 6c 6c 2e 20 ollback at all.
1ad3a 4f 74 68 65 72 77 69 73 65 2c 0a 20 20 20 20 20 Otherwise,.
1ad3b 20 2a 2a 20 70 72 6f 63 65 65 64 20 77 69 74 68 ** proceed with
1ad3c 20 74 68 65 20 73 70 65 63 69 61 6c 20 68 61 6e the special han
1ad3d 64 6c 69 6e 67 2e 0a 20 20 20 20 20 20 2a 2f 0a dling.. */.
1ad3e 20 20 20 20 20 20 69 66 28 20 21 70 2d 3e 72 65 if( !p->re
1ad3f 61 64 4f 6e 6c 79 20 7c 7c 20 6d 72 63 21 3d 53 adOnly || mrc!=S
1ad40 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 20 QLITE_INTERRUPT
1ad41 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 28 ){. if( (
1ad42 6d 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 mrc==SQLITE_NOME
1ad43 4d 20 7c 7c 20 6d 72 63 3d 3d 53 51 4c 49 54 45 M || mrc==SQLITE
1ad44 5f 46 55 4c 4c 29 20 26 26 20 70 2d 3e 75 73 65 _FULL) && p->use
1ad45 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 20 29 7b 0a sStmtJournal ){.
1ad46 20 20 20 20 20 20 20 20 20 20 65 53 74 61 74 65 eState
1ad47 6d 65 6e 74 4f 70 20 3d 20 53 41 56 45 50 4f 49 mentOp = SAVEPOI
1ad48 4e 54 5f 52 4f 4c 4c 42 41 43 4b 3b 0a 20 20 20 NT_ROLLBACK;.
1ad49 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
1ad4a 20 20 20 20 20 20 2f 2a 20 57 65 20 61 72 65 20 /* We are
1ad4b 66 6f 72 63 65 64 20 74 6f 20 72 6f 6c 6c 20 62 forced to roll b
1ad4c 61 63 6b 20 74 68 65 20 61 63 74 69 76 65 20 74 ack the active t
1ad4d 72 61 6e 73 61 63 74 69 6f 6e 2e 20 42 65 66 6f ransaction. Befo
1ad4e 72 65 20 64 6f 69 6e 67 0a 20 20 20 20 20 20 20 re doing.
1ad4f 20 20 20 2a 2a 20 73 6f 2c 20 61 62 6f 72 74 20 ** so, abort
1ad50 61 6e 79 20 6f 74 68 65 72 20 73 74 61 74 65 6d any other statem
1ad51 65 6e 74 73 20 74 68 69 73 20 68 61 6e 64 6c 65 ents this handle
1ad52 20 63 75 72 72 65 6e 74 6c 79 20 68 61 73 20 61 currently has a
1ad53 63 74 69 76 65 2e 0a 20 20 20 20 20 20 20 20 20 ctive..
1ad54 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 69 6e */. in
1ad55 76 61 6c 69 64 61 74 65 43 75 72 73 6f 72 73 4f validateCursorsO
1ad56 6e 4d 6f 64 69 66 69 65 64 42 74 72 65 65 73 28 nModifiedBtrees(
1ad57 64 62 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 db);. s
1ad58 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b 41 6c qlite3RollbackAl
1ad59 6c 28 64 62 29 3b 0a 20 20 20 20 20 20 20 20 20 l(db);.
1ad5a 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 53 61 76 sqlite3CloseSav
1ad5b 65 70 6f 69 6e 74 73 28 64 62 29 3b 0a 20 20 20 epoints(db);.
1ad5c 20 20 20 20 20 20 20 64 62 2d 3e 61 75 74 6f 43 db->autoC
1ad5d 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20 20 20 20 20 ommit = 1;.
1ad5e 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
1ad5f 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b }.. /* Check
1ad60 20 66 6f 72 20 69 6d 6d 65 64 69 61 74 65 20 66 for immediate f
1ad61 6f 72 65 69 67 6e 20 6b 65 79 20 76 69 6f 6c 61 oreign key viola
1ad62 74 69 6f 6e 73 2e 20 2a 2f 0a 20 20 20 20 69 66 tions. */. if
1ad63 28 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f ( p->rc==SQLITE_
1ad64 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 OK ){. sqli
1ad65 74 65 33 56 64 62 65 43 68 65 63 6b 46 6b 28 70 te3VdbeCheckFk(p
1ad66 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 , 0);. }. .
1ad67 20 20 20 2f 2a 20 49 66 20 74 68 65 20 61 75 74 /* If the aut
1ad68 6f 2d 63 6f 6d 6d 69 74 20 66 6c 61 67 20 69 73 o-commit flag is
1ad69 20 73 65 74 20 61 6e 64 20 74 68 69 73 20 69 73 set and this is
1ad6a 20 74 68 65 20 6f 6e 6c 79 20 61 63 74 69 76 65 the only active
1ad6b 20 77 72 69 74 65 72 20 0a 20 20 20 20 2a 2a 20 writer . **
1ad6c 56 4d 2c 20 74 68 65 6e 20 77 65 20 64 6f 20 65 VM, then we do e
1ad6d 69 74 68 65 72 20 61 20 63 6f 6d 6d 69 74 20 6f ither a commit o
1ad6e 72 20 72 6f 6c 6c 62 61 63 6b 20 6f 66 20 74 68 r rollback of th
1ad6f 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 e current transa
1ad70 63 74 69 6f 6e 2e 20 0a 20 20 20 20 2a 2a 0a 20 ction. . **.
1ad71 20 20 20 2a 2a 20 4e 6f 74 65 3a 20 54 68 69 73 ** Note: This
1ad72 20 62 6c 6f 63 6b 20 61 6c 73 6f 20 72 75 6e 73 block also runs
1ad73 20 69 66 20 6f 6e 65 20 6f 66 20 74 68 65 20 73 if one of the s
1ad74 70 65 63 69 61 6c 20 65 72 72 6f 72 73 20 68 61 pecial errors ha
1ad75 6e 64 6c 65 64 20 0a 20 20 20 20 2a 2a 20 61 62 ndled . ** ab
1ad76 6f 76 65 20 68 61 73 20 6f 63 63 75 72 72 65 64 ove has occurred
1ad77 2e 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 . . */. if
1ad78 28 20 21 73 71 6c 69 74 65 33 56 74 61 62 49 6e ( !sqlite3VtabIn
1ad79 53 79 6e 63 28 64 62 29 20 0a 20 20 20 20 20 26 Sync(db) . &
1ad7a 26 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 & db->autoCommit
1ad7b 20 0a 20 20 20 20 20 26 26 20 64 62 2d 3e 77 72 . && db->wr
1ad7c 69 74 65 56 64 62 65 43 6e 74 3d 3d 28 70 2d 3e iteVdbeCnt==(p->
1ad7d 72 65 61 64 4f 6e 6c 79 3d 3d 30 29 20 0a 20 20 readOnly==0) .
1ad7e 20 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 ){. if( p
1ad7f 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ->rc==SQLITE_OK
1ad80 7c 7c 20 28 70 2d 3e 65 72 72 6f 72 41 63 74 69 || (p->errorActi
1ad81 6f 6e 3d 3d 4f 45 5f 46 61 69 6c 20 26 26 20 21 on==OE_Fail && !
1ad82 69 73 53 70 65 63 69 61 6c 45 72 72 6f 72 29 20 isSpecialError)
1ad83 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 ){. if( s
1ad84 71 6c 69 74 65 33 56 64 62 65 43 68 65 63 6b 46 qlite3VdbeCheckF
1ad85 6b 28 70 2c 20 31 29 20 29 7b 0a 20 20 20 20 20 k(p, 1) ){.
1ad86 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 sqlite3Btre
1ad87 65 4d 75 74 65 78 41 72 72 61 79 4c 65 61 76 65 eMutexArrayLeave
1ad88 28 26 70 2d 3e 61 4d 75 74 65 78 29 3b 0a 20 20 (&p->aMutex);.
1ad89 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 return S
1ad8a 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 QLITE_ERROR;.
1ad8b 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2f }. /
1ad8c 2a 20 54 68 65 20 61 75 74 6f 2d 63 6f 6d 6d 69 * The auto-commi
1ad8d 74 20 66 6c 61 67 20 69 73 20 74 72 75 65 2c 20 t flag is true,
1ad8e 74 68 65 20 76 64 62 65 20 70 72 6f 67 72 61 6d the vdbe program
1ad8f 20 77 61 73 20 73 75 63 63 65 73 73 66 75 6c 20 was successful
1ad90 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 72 20 68 . ** or h
1ad91 69 74 20 61 6e 20 27 4f 52 20 46 41 49 4c 27 20 it an 'OR FAIL'
1ad92 63 6f 6e 73 74 72 61 69 6e 74 20 61 6e 64 20 74 constraint and t
1ad93 68 65 72 65 20 61 72 65 20 6e 6f 20 64 65 66 65 here are no defe
1ad94 72 72 65 64 20 66 6f 72 65 69 67 6e 0a 20 20 20 rred foreign.
1ad95 20 20 20 20 20 2a 2a 20 6b 65 79 20 63 6f 6e 73 ** key cons
1ad96 74 72 61 69 6e 74 73 20 74 6f 20 68 6f 6c 64 20 traints to hold
1ad97 75 70 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 up the transacti
1ad98 6f 6e 2e 20 54 68 69 73 20 6d 65 61 6e 73 20 61 on. This means a
1ad99 20 63 6f 6d 6d 69 74 20 0a 20 20 20 20 20 20 20 commit .
1ad9a 20 2a 2a 20 69 73 20 72 65 71 75 69 72 65 64 2e ** is required.
1ad9b 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 63 20 */. rc
1ad9c 3d 20 76 64 62 65 43 6f 6d 6d 69 74 28 64 62 2c = vdbeCommit(db,
1ad9d 20 70 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 p);. if(
1ad9e 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 rc==SQLITE_BUSY
1ad9f 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 ){. sq
1ada0 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 41 lite3BtreeMutexA
1ada1 72 72 61 79 4c 65 61 76 65 28 26 70 2d 3e 61 4d rrayLeave(&p->aM
1ada2 75 74 65 78 29 3b 0a 20 20 20 20 20 20 20 20 20 utex);.
1ada3 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 return SQLITE_B
1ada4 55 53 59 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c USY;. }el
1ada5 73 65 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 se if( rc!=SQLIT
1ada6 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
1ada7 20 20 70 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20 p->rc = rc;.
1ada8 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 sqlite3R
1ada9 6f 6c 6c 62 61 63 6b 41 6c 6c 28 64 62 29 3b 0a ollbackAll(db);.
1adaa 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }else{.
1adab 20 20 20 20 20 20 20 20 20 64 62 2d 3e 6e 44 65 db->nDe
1adac 66 65 72 72 65 64 43 6f 6e 73 20 3d 20 30 3b 0a ferredCons = 0;.
1adad 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
1adae 33 43 6f 6d 6d 69 74 49 6e 74 65 72 6e 61 6c 43 3CommitInternalC
1adaf 68 61 6e 67 65 73 28 64 62 29 3b 0a 20 20 20 20 hanges(db);.
1adb0 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 }. }els
1adb1 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 e{. sqlit
1adb2 65 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 64 62 e3RollbackAll(db
1adb3 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
1adb4 20 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 20 db->nStatement
1adb5 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 = 0;. }else i
1adb6 66 28 20 65 53 74 61 74 65 6d 65 6e 74 4f 70 3d f( eStatementOp=
1adb7 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 =0 ){. if(
1adb8 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b p->rc==SQLITE_OK
1adb9 20 7c 7c 20 70 2d 3e 65 72 72 6f 72 41 63 74 69 || p->errorActi
1adba 6f 6e 3d 3d 4f 45 5f 46 61 69 6c 20 29 7b 0a 20 on==OE_Fail ){.
1adbb 20 20 20 20 20 20 20 65 53 74 61 74 65 6d 65 6e eStatemen
1adbc 74 4f 70 20 3d 20 53 41 56 45 50 4f 49 4e 54 5f tOp = SAVEPOINT_
1adbd 52 45 4c 45 41 53 45 3b 0a 20 20 20 20 20 20 7d RELEASE;. }
1adbe 65 6c 73 65 20 69 66 28 20 70 2d 3e 65 72 72 6f else if( p->erro
1adbf 72 41 63 74 69 6f 6e 3d 3d 4f 45 5f 41 62 6f 72 rAction==OE_Abor
1adc0 74 20 29 7b 0a 20 20 20 20 20 20 20 20 65 53 74 t ){. eSt
1adc1 61 74 65 6d 65 6e 74 4f 70 20 3d 20 53 41 56 45 atementOp = SAVE
1adc2 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 3b 0a POINT_ROLLBACK;.
1adc3 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
1adc4 20 20 20 20 20 69 6e 76 61 6c 69 64 61 74 65 43 invalidateC
1adc5 75 72 73 6f 72 73 4f 6e 4d 6f 64 69 66 69 65 64 ursorsOnModified
1adc6 42 74 72 65 65 73 28 64 62 29 3b 0a 20 20 20 20 Btrees(db);.
1adc7 20 20 20 20 73 71 6c 69 74 65 33 52 6f 6c 6c 62 sqlite3Rollb
1adc8 61 63 6b 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20 ackAll(db);.
1adc9 20 20 20 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 sqlite3Close
1adca 53 61 76 65 70 6f 69 6e 74 73 28 64 62 29 3b 0a Savepoints(db);.
1adcb 20 20 20 20 20 20 20 20 64 62 2d 3e 61 75 74 6f db->auto
1adcc 43 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20 20 20 20 Commit = 1;.
1adcd 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 }. }. .
1adce 20 2f 2a 20 49 66 20 65 53 74 61 74 65 6d 65 6e /* If eStatemen
1adcf 74 4f 70 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c tOp is non-zero,
1add0 20 74 68 65 6e 20 61 20 73 74 61 74 65 6d 65 6e then a statemen
1add1 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6e 65 t transaction ne
1add2 65 64 73 20 74 6f 0a 20 20 20 20 2a 2a 20 62 65 eds to. ** be
1add3 20 63 6f 6d 6d 69 74 74 65 64 20 6f 72 20 72 6f committed or ro
1add4 6c 6c 65 64 20 62 61 63 6b 2e 20 43 61 6c 6c 20 lled back. Call
1add5 73 71 6c 69 74 65 33 56 64 62 65 43 6c 6f 73 65 sqlite3VdbeClose
1add6 53 74 61 74 65 6d 65 6e 74 28 29 20 74 6f 0a 20 Statement() to.
1add7 20 20 20 2a 2a 20 64 6f 20 73 6f 2e 20 49 66 20 ** do so. If
1add8 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 20 72 this operation r
1add9 65 74 75 72 6e 73 20 61 6e 20 65 72 72 6f 72 2c eturns an error,
1adda 20 61 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74 and the current
1addb 20 73 74 61 74 65 6d 65 6e 74 0a 20 20 20 20 2a statement. *
1addc 2a 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 * error code is
1addd 53 51 4c 49 54 45 5f 4f 4b 20 6f 72 20 53 51 4c SQLITE_OK or SQL
1adde 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 2c 20 ITE_CONSTRAINT,
1addf 74 68 65 6e 20 73 65 74 20 74 68 65 20 65 72 72 then set the err
1ade0 6f 72 0a 20 20 20 20 2a 2a 20 63 6f 64 65 20 74 or. ** code t
1ade1 6f 20 74 68 65 20 6e 65 77 20 76 61 6c 75 65 2e o the new value.
1ade2 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 . */. if(
1ade3 65 53 74 61 74 65 6d 65 6e 74 4f 70 20 29 7b 0a eStatementOp ){.
1ade4 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
1ade5 65 33 56 64 62 65 43 6c 6f 73 65 53 74 61 74 65 e3VdbeCloseState
1ade6 6d 65 6e 74 28 70 2c 20 65 53 74 61 74 65 6d 65 ment(p, eStateme
1ade7 6e 74 4f 70 29 3b 0a 20 20 20 20 20 20 69 66 28 ntOp);. if(
1ade8 20 72 63 20 26 26 20 28 70 2d 3e 72 63 3d 3d 53 rc && (p->rc==S
1ade9 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 2d 3e 72 QLITE_OK || p->r
1adea 63 3d 3d 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 c==SQLITE_CONSTR
1adeb 41 49 4e 54 29 20 29 7b 0a 20 20 20 20 20 20 20 AINT) ){.
1adec 20 70 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20 20 p->rc = rc;.
1aded 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 sqlite3DbFr
1adee 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 ee(db, p->zErrMs
1adef 67 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 7a g);. p->z
1adf0 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 20 20 ErrMsg = 0;.
1adf1 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 }. }. .
1adf2 20 2f 2a 20 49 66 20 74 68 69 73 20 77 61 73 20 /* If this was
1adf3 61 6e 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54 an INSERT, UPDAT
1adf4 45 20 6f 72 20 44 45 4c 45 54 45 20 61 6e 64 20 E or DELETE and
1adf5 6e 6f 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 no statement tra
1adf6 6e 73 61 63 74 69 6f 6e 0a 20 20 20 20 2a 2a 20 nsaction. **
1adf7 68 61 73 20 62 65 65 6e 20 72 6f 6c 6c 65 64 20 has been rolled
1adf8 62 61 63 6b 2c 20 75 70 64 61 74 65 20 74 68 65 back, update the
1adf9 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
1adfa 74 69 6f 6e 20 63 68 61 6e 67 65 2d 63 6f 75 6e tion change-coun
1adfb 74 65 72 2e 20 0a 20 20 20 20 2a 2f 0a 20 20 20 ter. . */.
1adfc 20 69 66 28 20 70 2d 3e 63 68 61 6e 67 65 43 6e if( p->changeCn
1adfd 74 4f 6e 20 29 7b 0a 20 20 20 20 20 20 69 66 28 tOn ){. if(
1adfe 20 65 53 74 61 74 65 6d 65 6e 74 4f 70 21 3d 53 eStatementOp!=S
1adff 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 AVEPOINT_ROLLBAC
1ae00 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c K ){. sql
1ae01 69 74 65 33 56 64 62 65 53 65 74 43 68 61 6e 67 ite3VdbeSetChang
1ae02 65 73 28 64 62 2c 20 70 2d 3e 6e 43 68 61 6e 67 es(db, p->nChang
1ae03 65 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b e);. }else{
1ae04 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
1ae05 56 64 62 65 53 65 74 43 68 61 6e 67 65 73 28 64 VdbeSetChanges(d
1ae06 62 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 b, 0);. }.
1ae07 20 20 20 20 20 70 2d 3e 6e 43 68 61 6e 67 65 20 p->nChange
1ae08 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 = 0;. }. .
1ae09 20 20 2f 2a 20 52 6f 6c 6c 62 61 63 6b 20 6f 72 /* Rollback or
1ae0a 20 63 6f 6d 6d 69 74 20 61 6e 79 20 73 63 68 65 commit any sche
1ae0b 6d 61 20 63 68 61 6e 67 65 73 20 74 68 61 74 20 ma changes that
1ae0c 6f 63 63 75 72 72 65 64 2e 20 2a 2f 0a 20 20 20 occurred. */.
1ae0d 20 69 66 28 20 70 2d 3e 72 63 21 3d 53 51 4c 49 if( p->rc!=SQLI
1ae0e 54 45 5f 4f 4b 20 26 26 20 64 62 2d 3e 66 6c 61 TE_OK && db->fla
1ae0f 67 73 26 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e gs&SQLITE_Intern
1ae10 43 68 61 6e 67 65 73 20 29 7b 0a 20 20 20 20 20 Changes ){.
1ae11 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74 sqlite3ResetInt
1ae12 65 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20 ernalSchema(db,
1ae13 30 29 3b 0a 20 20 20 20 20 20 64 62 2d 3e 66 6c 0);. db->fl
1ae14 61 67 73 20 3d 20 28 64 62 2d 3e 66 6c 61 67 73 ags = (db->flags
1ae15 20 7c 20 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e | SQLITE_Intern
1ae16 43 68 61 6e 67 65 73 29 3b 0a 20 20 20 20 7d 0a Changes);. }.
1ae17 0a 20 20 20 20 2f 2a 20 52 65 6c 65 61 73 65 20 . /* Release
1ae18 74 68 65 20 6c 6f 63 6b 73 20 2a 2f 0a 20 20 20 the locks */.
1ae19 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 sqlite3BtreeMut
1ae1a 65 78 41 72 72 61 79 4c 65 61 76 65 28 26 70 2d exArrayLeave(&p-
1ae1b 3e 61 4d 75 74 65 78 29 3b 0a 20 20 7d 0a 0a 20 >aMutex);. }..
1ae1c 20 2f 2a 20 57 65 20 68 61 76 65 20 73 75 63 63 /* We have succ
1ae1d 65 73 73 66 75 6c 6c 79 20 68 61 6c 74 65 64 20 essfully halted
1ae1e 61 6e 64 20 63 6c 6f 73 65 64 20 74 68 65 20 56 and closed the V
1ae1f 4d 2e 20 20 52 65 63 6f 72 64 20 74 68 69 73 20 M. Record this
1ae20 66 61 63 74 2e 20 2a 2f 0a 20 20 69 66 28 20 70 fact. */. if( p
1ae21 2d 3e 70 63 3e 3d 30 20 29 7b 0a 20 20 20 20 64 ->pc>=0 ){. d
1ae22 62 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 b->activeVdbeCnt
1ae23 2d 2d 3b 0a 20 20 20 20 69 66 28 20 21 70 2d 3e --;. if( !p->
1ae24 72 65 61 64 4f 6e 6c 79 20 29 7b 0a 20 20 20 20 readOnly ){.
1ae25 20 20 64 62 2d 3e 77 72 69 74 65 56 64 62 65 43 db->writeVdbeC
1ae26 6e 74 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 nt--;. }.
1ae27 61 73 73 65 72 74 28 20 64 62 2d 3e 61 63 74 69 assert( db->acti
1ae28 76 65 56 64 62 65 43 6e 74 3e 3d 64 62 2d 3e 77 veVdbeCnt>=db->w
1ae29 72 69 74 65 56 64 62 65 43 6e 74 20 29 3b 0a 20 riteVdbeCnt );.
1ae2a 20 7d 0a 20 20 70 2d 3e 6d 61 67 69 63 20 3d 20 }. p->magic =
1ae2b 56 44 42 45 5f 4d 41 47 49 43 5f 48 41 4c 54 3b VDBE_MAGIC_HALT;
1ae2c 0a 20 20 63 68 65 63 6b 41 63 74 69 76 65 56 64 . checkActiveVd
1ae2d 62 65 43 6e 74 28 64 62 29 3b 0a 20 20 69 66 28 beCnt(db);. if(
1ae2e 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 p->db->mallocFa
1ae2f 69 6c 65 64 20 29 7b 0a 20 20 20 20 70 2d 3e 72 iled ){. p->r
1ae30 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d c = SQLITE_NOMEM
1ae31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 ;. }.. /* If t
1ae32 68 65 20 61 75 74 6f 2d 63 6f 6d 6d 69 74 20 66 he auto-commit f
1ae33 6c 61 67 20 69 73 20 73 65 74 20 74 6f 20 74 72 lag is set to tr
1ae34 75 65 2c 20 74 68 65 6e 20 61 6e 79 20 6c 6f 63 ue, then any loc
1ae35 6b 73 20 74 68 61 74 20 77 65 72 65 20 68 65 6c ks that were hel
1ae36 64 0a 20 20 2a 2a 20 62 79 20 63 6f 6e 6e 65 63 d. ** by connec
1ae37 74 69 6f 6e 20 64 62 20 68 61 76 65 20 6e 6f 77 tion db have now
1ae38 20 62 65 65 6e 20 72 65 6c 65 61 73 65 64 2e 20 been released.
1ae39 43 61 6c 6c 20 73 71 6c 69 74 65 33 43 6f 6e 6e Call sqlite3Conn
1ae3a 65 63 74 69 6f 6e 55 6e 6c 6f 63 6b 65 64 28 29 ectionUnlocked()
1ae3b 20 0a 20 20 2a 2a 20 74 6f 20 69 6e 76 6f 6b 65 . ** to invoke
1ae3c 20 61 6e 79 20 72 65 71 75 69 72 65 64 20 75 6e any required un
1ae3d 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c lock-notify call
1ae3e 62 61 63 6b 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 backs.. */. if
1ae3f 28 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 ( db->autoCommit
1ae40 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 ){. sqlite3C
1ae41 6f 6e 6e 65 63 74 69 6f 6e 55 6e 6c 6f 63 6b 65 onnectionUnlocke
1ae42 64 28 64 62 29 3b 0a 20 20 7d 0a 0a 20 20 61 73 d(db);. }.. as
1ae43 73 65 72 74 28 20 64 62 2d 3e 61 63 74 69 76 65 sert( db->active
1ae44 56 64 62 65 43 6e 74 3e 30 20 7c 7c 20 64 62 2d VdbeCnt>0 || db-
1ae45 3e 61 75 74 6f 43 6f 6d 6d 69 74 3d 3d 30 20 7c >autoCommit==0 |
1ae46 7c 20 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 | db->nStatement
1ae47 3d 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 ==0 );. return
1ae48 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f SQLITE_OK;.}.../
1ae49 2a 0a 2a 2a 20 45 61 63 68 20 56 44 42 45 20 68 *.** Each VDBE h
1ae4a 6f 6c 64 73 20 74 68 65 20 72 65 73 75 6c 74 20 olds the result
1ae4b 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 of the most rece
1ae4c 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 nt sqlite3_step(
1ae4d 29 20 63 61 6c 6c 0a 2a 2a 20 69 6e 20 70 2d 3e ) call.** in p->
1ae4e 72 63 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e rc. This routin
1ae4f 65 20 73 65 74 73 20 74 68 61 74 20 72 65 73 75 e sets that resu
1ae50 6c 74 20 62 61 63 6b 20 74 6f 20 53 51 4c 49 54 lt back to SQLIT
1ae51 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f E_OK..*/.SQLITE_
1ae52 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
1ae53 69 74 65 33 56 64 62 65 52 65 73 65 74 53 74 65 ite3VdbeResetSte
1ae54 70 52 65 73 75 6c 74 28 56 64 62 65 20 2a 70 29 pResult(Vdbe *p)
1ae55 7b 0a 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 {. p->rc = SQLI
1ae56 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 TE_OK;.}../*.**
1ae57 43 6c 65 61 6e 20 75 70 20 61 20 56 44 42 45 20 Clean up a VDBE
1ae58 61 66 74 65 72 20 65 78 65 63 75 74 69 6f 6e 20 after execution
1ae59 62 75 74 20 64 6f 20 6e 6f 74 20 64 65 6c 65 74 but do not delet
1ae5a 65 20 74 68 65 20 56 44 42 45 20 6a 75 73 74 20 e the VDBE just
1ae5b 79 65 74 2e 0a 2a 2a 20 57 72 69 74 65 20 61 6e yet..** Write an
1ae5c 79 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 y error messages
1ae5d 20 69 6e 74 6f 20 2a 70 7a 45 72 72 4d 73 67 2e into *pzErrMsg.
1ae5e 20 20 52 65 74 75 72 6e 20 74 68 65 20 72 65 73 Return the res
1ae5f 75 6c 74 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 ult code..**.**
1ae60 41 66 74 65 72 20 74 68 69 73 20 72 6f 75 74 69 After this routi
1ae61 6e 65 20 69 73 20 72 75 6e 2c 20 74 68 65 20 56 ne is run, the V
1ae62 44 42 45 20 73 68 6f 75 6c 64 20 62 65 20 72 65 DBE should be re
1ae63 61 64 79 20 74 6f 20 62 65 20 65 78 65 63 75 74 ady to be execut
1ae64 65 64 0a 2a 2a 20 61 67 61 69 6e 2e 0a 2a 2a 0a ed.** again..**.
1ae65 2a 2a 20 54 6f 20 6c 6f 6f 6b 20 61 74 20 69 74 ** To look at it
1ae66 20 61 6e 6f 74 68 65 72 20 77 61 79 2c 20 74 68 another way, th
1ae67 69 73 20 72 6f 75 74 69 6e 65 20 72 65 73 65 74 is routine reset
1ae68 73 20 74 68 65 20 73 74 61 74 65 20 6f 66 20 74 s the state of t
1ae69 68 65 0a 2a 2a 20 76 69 72 74 75 61 6c 20 6d 61 he.** virtual ma
1ae6a 63 68 69 6e 65 20 66 72 6f 6d 20 56 44 42 45 5f chine from VDBE_
1ae6b 4d 41 47 49 43 5f 52 55 4e 20 6f 72 20 56 44 42 MAGIC_RUN or VDB
1ae6c 45 5f 4d 41 47 49 43 5f 48 41 4c 54 20 62 61 63 E_MAGIC_HALT bac
1ae6d 6b 20 74 6f 0a 2a 2a 20 56 44 42 45 5f 4d 41 47 k to.** VDBE_MAG
1ae6e 49 43 5f 49 4e 49 54 2e 0a 2a 2f 0a 53 51 4c 49 IC_INIT..*/.SQLI
1ae6f 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
1ae70 71 6c 69 74 65 33 56 64 62 65 52 65 73 65 74 28 qlite3VdbeReset(
1ae71 56 64 62 65 20 2a 70 29 7b 0a 20 20 73 71 6c 69 Vdbe *p){. sqli
1ae72 74 65 33 20 2a 64 62 3b 0a 20 20 64 62 20 3d 20 te3 *db;. db =
1ae73 70 2d 3e 64 62 3b 0a 0a 20 20 2f 2a 20 49 66 20 p->db;.. /* If
1ae74 74 68 65 20 56 4d 20 64 69 64 20 6e 6f 74 20 72 the VM did not r
1ae75 75 6e 20 74 6f 20 63 6f 6d 70 6c 65 74 69 6f 6e un to completion
1ae76 20 6f 72 20 69 66 20 69 74 20 65 6e 63 6f 75 6e or if it encoun
1ae77 74 65 72 65 64 20 61 6e 0a 20 20 2a 2a 20 65 72 tered an. ** er
1ae78 72 6f 72 2c 20 74 68 65 6e 20 69 74 20 6d 69 67 ror, then it mig
1ae79 68 74 20 6e 6f 74 20 68 61 76 65 20 62 65 65 6e ht not have been
1ae7a 20 68 61 6c 74 65 64 20 70 72 6f 70 65 72 6c 79 halted properly
1ae7b 2e 20 20 53 6f 20 68 61 6c 74 0a 20 20 2a 2a 20 . So halt. **
1ae7c 69 74 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 20 28 it now.. */. (
1ae7d 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 void)sqlite3Safe
1ae7e 74 79 4f 6e 28 64 62 29 3b 0a 20 20 73 71 6c 69 tyOn(db);. sqli
1ae7f 74 65 33 56 64 62 65 48 61 6c 74 28 70 29 3b 0a te3VdbeHalt(p);.
1ae80 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 (void)sqlite3S
1ae81 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 0a 20 afetyOff(db);..
1ae82 20 2f 2a 20 49 66 20 74 68 65 20 56 44 42 45 20 /* If the VDBE
1ae83 68 61 73 20 62 65 20 72 75 6e 20 65 76 65 6e 20 has be run even
1ae84 70 61 72 74 69 61 6c 6c 79 2c 20 74 68 65 6e 20 partially, then
1ae85 74 72 61 6e 73 66 65 72 20 74 68 65 20 65 72 72 transfer the err
1ae86 6f 72 20 63 6f 64 65 0a 20 20 2a 2a 20 61 6e 64 or code. ** and
1ae87 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 66 error message f
1ae88 72 6f 6d 20 74 68 65 20 56 44 42 45 20 69 6e 74 rom the VDBE int
1ae89 6f 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 o the main datab
1ae8a 61 73 65 20 73 74 72 75 63 74 75 72 65 2e 20 20 ase structure.
1ae8b 42 75 74 0a 20 20 2a 2a 20 69 66 20 74 68 65 20 But. ** if the
1ae8c 56 44 42 45 20 68 61 73 20 6a 75 73 74 20 62 65 VDBE has just be
1ae8d 65 6e 20 73 65 74 20 74 6f 20 72 75 6e 20 62 75 en set to run bu
1ae8e 74 20 68 61 73 20 6e 6f 74 20 61 63 74 75 61 6c t has not actual
1ae8f 6c 79 20 65 78 65 63 75 74 65 64 20 61 6e 79 0a ly executed any.
1ae90 20 20 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e ** instruction
1ae91 73 20 79 65 74 2c 20 6c 65 61 76 65 20 74 68 65 s yet, leave the
1ae92 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 65 main database e
1ae93 72 72 6f 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e rror information
1ae94 20 75 6e 63 68 61 6e 67 65 64 2e 0a 20 20 2a 2f unchanged.. */
1ae95 0a 20 20 69 66 28 20 70 2d 3e 70 63 3e 3d 30 20 . if( p->pc>=0
1ae96 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 7a 45 ){. if( p->zE
1ae97 72 72 4d 73 67 20 29 7b 0a 20 20 20 20 20 20 73 rrMsg ){. s
1ae98 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 qlite3BeginBenig
1ae99 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20 nMalloc();.
1ae9a 20 73 71 6c 69 74 65 33 56 61 6c 75 65 53 65 74 sqlite3ValueSet
1ae9b 53 74 72 28 64 62 2d 3e 70 45 72 72 2c 2d 31 2c Str(db->pErr,-1,
1ae9c 70 2d 3e 7a 45 72 72 4d 73 67 2c 53 51 4c 49 54 p->zErrMsg,SQLIT
1ae9d 45 5f 55 54 46 38 2c 53 51 4c 49 54 45 5f 54 52 E_UTF8,SQLITE_TR
1ae9e 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20 20 20 ANSIENT);.
1ae9f 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e sqlite3EndBenign
1aea0 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20 Malloc();.
1aea1 64 62 2d 3e 65 72 72 43 6f 64 65 20 3d 20 70 2d db->errCode = p-
1aea2 3e 72 63 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 >rc;. sqlit
1aea3 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e e3DbFree(db, p->
1aea4 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 20 20 zErrMsg);.
1aea5 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a p->zErrMsg = 0;.
1aea6 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d }else if( p-
1aea7 3e 72 63 20 29 7b 0a 20 20 20 20 20 20 73 71 6c >rc ){. sql
1aea8 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 70 2d ite3Error(db, p-
1aea9 3e 72 63 2c 20 30 29 3b 0a 20 20 20 20 7d 65 6c >rc, 0);. }el
1aeaa 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 se{. sqlite
1aeab 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 3Error(db, SQLIT
1aeac 45 5f 4f 4b 2c 20 30 29 3b 0a 20 20 20 20 7d 0a E_OK, 0);. }.
1aead 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 72 }else if( p->r
1aeae 63 20 26 26 20 70 2d 3e 65 78 70 69 72 65 64 20 c && p->expired
1aeaf 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 65 78 ){. /* The ex
1aeb0 70 69 72 65 64 20 66 6c 61 67 20 77 61 73 20 73 pired flag was s
1aeb1 65 74 20 6f 6e 20 74 68 65 20 56 44 42 45 20 62 et on the VDBE b
1aeb2 65 66 6f 72 65 20 74 68 65 20 66 69 72 73 74 20 efore the first
1aeb3 63 61 6c 6c 0a 20 20 20 20 2a 2a 20 74 6f 20 73 call. ** to s
1aeb4 71 6c 69 74 65 33 5f 73 74 65 70 28 29 2e 20 46 qlite3_step(). F
1aeb5 6f 72 20 63 6f 6e 73 69 73 74 65 6e 63 79 20 28 or consistency (
1aeb6 73 69 6e 63 65 20 73 71 6c 69 74 65 33 5f 73 74 since sqlite3_st
1aeb7 65 70 28 29 20 77 61 73 0a 20 20 20 20 2a 2a 20 ep() was. **
1aeb8 63 61 6c 6c 65 64 29 2c 20 73 65 74 20 74 68 65 called), set the
1aeb9 20 64 61 74 61 62 61 73 65 20 65 72 72 6f 72 20 database error
1aeba 69 6e 20 74 68 69 73 20 63 61 73 65 20 61 73 20 in this case as
1aebb 77 65 6c 6c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 well.. */.
1aebc 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 sqlite3Error(db
1aebd 2c 20 70 2d 3e 72 63 2c 20 30 29 3b 0a 20 20 20 , p->rc, 0);.
1aebe 20 73 71 6c 69 74 65 33 56 61 6c 75 65 53 65 74 sqlite3ValueSet
1aebf 53 74 72 28 64 62 2d 3e 70 45 72 72 2c 20 2d 31 Str(db->pErr, -1
1aec0 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 53 51 , p->zErrMsg, SQ
1aec1 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 LITE_UTF8, SQLIT
1aec2 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 E_TRANSIENT);.
1aec3 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
1aec4 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b db, p->zErrMsg);
1aec5 0a 20 20 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 . p->zErrMsg
1aec6 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 = 0;. }.. /* R
1aec7 65 63 6c 61 69 6d 20 61 6c 6c 20 6d 65 6d 6f 72 eclaim all memor
1aec8 79 20 75 73 65 64 20 62 79 20 74 68 65 20 56 44 y used by the VD
1aec9 42 45 0a 20 20 2a 2f 0a 20 20 43 6c 65 61 6e 75 BE. */. Cleanu
1aeca 70 28 70 29 3b 0a 0a 20 20 2f 2a 20 53 61 76 65 p(p);.. /* Save
1aecb 20 70 72 6f 66 69 6c 69 6e 67 20 69 6e 66 6f 72 profiling infor
1aecc 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 74 68 69 73 mation from this
1aecd 20 56 44 42 45 20 72 75 6e 2e 0a 20 20 2a 2f 0a VDBE run.. */.
1aece 23 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f 46 #ifdef VDBE_PROF
1aecf 49 4c 45 0a 20 20 7b 0a 20 20 20 20 46 49 4c 45 ILE. {. FILE
1aed0 20 2a 6f 75 74 20 3d 20 66 6f 70 65 6e 28 22 76 *out = fopen("v
1aed1 64 62 65 5f 70 72 6f 66 69 6c 65 2e 6f 75 74 22 dbe_profile.out"
1aed2 2c 20 22 61 22 29 3b 0a 20 20 20 20 69 66 28 20 , "a");. if(
1aed3 6f 75 74 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 out ){. int
1aed4 20 69 3b 0a 20 20 20 20 20 20 66 70 72 69 6e 74 i;. fprint
1aed5 66 28 6f 75 74 2c 20 22 2d 2d 2d 2d 20 22 29 3b f(out, "---- ");
1aed6 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 . for(i=0;
1aed7 69 3c 70 2d 3e 6e 4f 70 3b 20 69 2b 2b 29 7b 0a i<p->nOp; i++){.
1aed8 20 20 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 fprintf(
1aed9 6f 75 74 2c 20 22 25 30 32 78 22 2c 20 70 2d 3e out, "%02x", p->
1aeda 61 4f 70 5b 69 5d 2e 6f 70 63 6f 64 65 29 3b 0a aOp[i].opcode);.
1aedb 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 66 70 }. fp
1aedc 72 69 6e 74 66 28 6f 75 74 2c 20 22 5c 6e 22 29 rintf(out, "\n")
1aedd 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b ;. for(i=0;
1aede 20 69 3c 70 2d 3e 6e 4f 70 3b 20 69 2b 2b 29 7b i<p->nOp; i++){
1aedf 0a 20 20 20 20 20 20 20 20 66 70 72 69 6e 74 66 . fprintf
1aee0 28 6f 75 74 2c 20 22 25 36 64 20 25 31 30 6c 6c (out, "%6d %10ll
1aee1 64 20 25 38 6c 6c 64 20 22 2c 0a 20 20 20 20 20 d %8lld ",.
1aee2 20 20 20 20 20 20 70 2d 3e 61 4f 70 5b 69 5d 2e p->aOp[i].
1aee3 63 6e 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 cnt,.
1aee4 70 2d 3e 61 4f 70 5b 69 5d 2e 63 79 63 6c 65 73 p->aOp[i].cycles
1aee5 2c 0a 20 20 20 20 20 20 20 20 20 20 20 70 2d 3e ,. p->
1aee6 61 4f 70 5b 69 5d 2e 63 6e 74 3e 30 20 3f 20 70 aOp[i].cnt>0 ? p
1aee7 2d 3e 61 4f 70 5b 69 5d 2e 63 79 63 6c 65 73 2f ->aOp[i].cycles/
1aee8 70 2d 3e 61 4f 70 5b 69 5d 2e 63 6e 74 20 3a 20 p->aOp[i].cnt :
1aee9 30 0a 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20 0. );.
1aeea 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1aeeb 50 72 69 6e 74 4f 70 28 6f 75 74 2c 20 69 2c 20 PrintOp(out, i,
1aeec 26 70 2d 3e 61 4f 70 5b 69 5d 29 3b 0a 20 20 20 &p->aOp[i]);.
1aeed 20 20 20 7d 0a 20 20 20 20 20 20 66 63 6c 6f 73 }. fclos
1aeee 65 28 6f 75 74 29 3b 0a 20 20 20 20 7d 0a 20 20 e(out);. }.
1aeef 7d 0a 23 65 6e 64 69 66 0a 20 20 70 2d 3e 6d 61 }.#endif. p->ma
1aef0 67 69 63 20 3d 20 56 44 42 45 5f 4d 41 47 49 43 gic = VDBE_MAGIC
1aef1 5f 49 4e 49 54 3b 0a 20 20 72 65 74 75 72 6e 20 _INIT;. return
1aef2 70 2d 3e 72 63 20 26 20 64 62 2d 3e 65 72 72 4d p->rc & db->errM
1aef3 61 73 6b 3b 0a 7d 0a 20 0a 2f 2a 0a 2a 2a 20 43 ask;.}. ./*.** C
1aef4 6c 65 61 6e 20 75 70 20 61 6e 64 20 64 65 6c 65 lean up and dele
1aef5 74 65 20 61 20 56 44 42 45 20 61 66 74 65 72 20 te a VDBE after
1aef6 65 78 65 63 75 74 69 6f 6e 2e 20 20 52 65 74 75 execution. Retu
1aef7 72 6e 20 61 6e 20 69 6e 74 65 67 65 72 20 77 68 rn an integer wh
1aef8 69 63 68 20 69 73 0a 2a 2a 20 74 68 65 20 72 65 ich is.** the re
1aef9 73 75 6c 74 20 63 6f 64 65 2e 20 20 57 72 69 74 sult code. Writ
1aefa 65 20 61 6e 79 20 65 72 72 6f 72 20 6d 65 73 73 e any error mess
1aefb 61 67 65 20 74 65 78 74 20 69 6e 74 6f 20 2a 70 age text into *p
1aefc 7a 45 72 72 4d 73 67 2e 0a 2a 2f 0a 53 51 4c 49 zErrMsg..*/.SQLI
1aefd 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
1aefe 71 6c 69 74 65 33 56 64 62 65 46 69 6e 61 6c 69 qlite3VdbeFinali
1aeff 7a 65 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 ze(Vdbe *p){. i
1af00 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
1af01 4b 3b 0a 20 20 69 66 28 20 70 2d 3e 6d 61 67 69 K;. if( p->magi
1af02 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 c==VDBE_MAGIC_RU
1af03 4e 20 7c 7c 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 N || p->magic==V
1af04 44 42 45 5f 4d 41 47 49 43 5f 48 41 4c 54 20 29 DBE_MAGIC_HALT )
1af05 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 {. rc = sqlit
1af06 65 33 56 64 62 65 52 65 73 65 74 28 70 29 3b 0a e3VdbeReset(p);.
1af07 20 20 20 20 61 73 73 65 72 74 28 20 28 72 63 20 assert( (rc
1af08 26 20 70 2d 3e 64 62 2d 3e 65 72 72 4d 61 73 6b & p->db->errMask
1af09 29 3d 3d 72 63 20 29 3b 0a 20 20 7d 0a 20 20 73 )==rc );. }. s
1af0a 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 65 qlite3VdbeDelete
1af0b 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 (p);. return rc
1af0c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 ;.}../*.** Call
1af0d 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 66 the destructor f
1af0e 6f 72 20 65 61 63 68 20 61 75 78 64 61 74 61 20 or each auxdata
1af0f 65 6e 74 72 79 20 69 6e 20 70 56 64 62 65 46 75 entry in pVdbeFu
1af10 6e 63 20 66 6f 72 20 77 68 69 63 68 0a 2a 2a 20 nc for which.**
1af11 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e the correspondin
1af12 67 20 62 69 74 20 69 6e 20 6d 61 73 6b 20 69 73 g bit in mask is
1af13 20 63 6c 65 61 72 2e 20 20 41 75 78 64 61 74 61 clear. Auxdata
1af14 20 65 6e 74 72 69 65 73 20 62 65 79 6f 6e 64 20 entries beyond
1af15 33 31 0a 2a 2a 20 61 72 65 20 61 6c 77 61 79 73 31.** are always
1af16 20 64 65 73 74 72 6f 79 65 64 2e 20 20 54 6f 20 destroyed. To
1af17 64 65 73 74 72 6f 79 20 61 6c 6c 20 61 75 78 64 destroy all auxd
1af18 61 74 61 20 65 6e 74 72 69 65 73 2c 20 63 61 6c ata entries, cal
1af19 6c 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e l this.** routin
1af1a 65 20 77 69 74 68 20 6d 61 73 6b 3d 3d 30 2e 0a e with mask==0..
1af1b 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1af1c 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 E void sqlite3Vd
1af1d 62 65 44 65 6c 65 74 65 41 75 78 44 61 74 61 28 beDeleteAuxData(
1af1e 56 64 62 65 46 75 6e 63 20 2a 70 56 64 62 65 46 VdbeFunc *pVdbeF
1af1f 75 6e 63 2c 20 69 6e 74 20 6d 61 73 6b 29 7b 0a unc, int mask){.
1af20 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 int i;. for(i
1af21 3d 30 3b 20 69 3c 70 56 64 62 65 46 75 6e 63 2d =0; i<pVdbeFunc-
1af22 3e 6e 41 75 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 >nAux; i++){.
1af23 20 73 74 72 75 63 74 20 41 75 78 44 61 74 61 20 struct AuxData
1af24 2a 70 41 75 78 20 3d 20 26 70 56 64 62 65 46 75 *pAux = &pVdbeFu
1af25 6e 63 2d 3e 61 70 41 75 78 5b 69 5d 3b 0a 20 20 nc->apAux[i];.
1af26 20 20 69 66 28 20 28 69 3e 33 31 20 7c 7c 20 21 if( (i>31 || !
1af27 28 6d 61 73 6b 26 28 28 28 75 33 32 29 31 29 3c (mask&(((u32)1)<
1af28 3c 69 29 29 29 20 26 26 20 70 41 75 78 2d 3e 70 <i))) && pAux->p
1af29 41 75 78 20 29 7b 0a 20 20 20 20 20 20 69 66 28 Aux ){. if(
1af2a 20 70 41 75 78 2d 3e 78 44 65 6c 65 74 65 20 29 pAux->xDelete )
1af2b 7b 0a 20 20 20 20 20 20 20 20 70 41 75 78 2d 3e {. pAux->
1af2c 78 44 65 6c 65 74 65 28 70 41 75 78 2d 3e 70 41 xDelete(pAux->pA
1af2d 75 78 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 ux);. }.
1af2e 20 20 20 70 41 75 78 2d 3e 70 41 75 78 20 3d 20 pAux->pAux =
1af2f 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 0;. }. }.}..
1af30 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 20 /*.** Delete an
1af31 65 6e 74 69 72 65 20 56 44 42 45 2e 0a 2a 2f 0a entire VDBE..*/.
1af32 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
1af33 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 44 oid sqlite3VdbeD
1af34 65 6c 65 74 65 28 56 64 62 65 20 2a 70 29 7b 0a elete(Vdbe *p){.
1af35 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 0a sqlite3 *db;..
1af36 20 20 69 66 28 20 4e 45 56 45 52 28 70 3d 3d 30 if( NEVER(p==0
1af37 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 64 62 ) ) return;. db
1af38 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 69 66 28 20 = p->db;. if(
1af39 70 2d 3e 70 50 72 65 76 20 29 7b 0a 20 20 20 20 p->pPrev ){.
1af3a 70 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 20 p->pPrev->pNext
1af3b 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 65 = p->pNext;. }e
1af3c 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 lse{. assert(
1af3d 20 64 62 2d 3e 70 56 64 62 65 3d 3d 70 20 29 3b db->pVdbe==p );
1af3e 0a 20 20 20 20 64 62 2d 3e 70 56 64 62 65 20 3d . db->pVdbe =
1af3f 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 p->pNext;. }.
1af40 20 69 66 28 20 70 2d 3e 70 4e 65 78 74 20 29 7b if( p->pNext ){
1af41 0a 20 20 20 20 70 2d 3e 70 4e 65 78 74 2d 3e 70 . p->pNext->p
1af42 50 72 65 76 20 3d 20 70 2d 3e 70 50 72 65 76 3b Prev = p->pPrev;
1af43 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73 65 4d 65 . }. releaseMe
1af44 6d 41 72 72 61 79 28 70 2d 3e 61 56 61 72 2c 20 mArray(p->aVar,
1af45 70 2d 3e 6e 56 61 72 29 3b 0a 20 20 72 65 6c 65 p->nVar);. rele
1af46 61 73 65 4d 65 6d 41 72 72 61 79 28 70 2d 3e 61 aseMemArray(p->a
1af47 43 6f 6c 4e 61 6d 65 2c 20 70 2d 3e 6e 52 65 73 ColName, p->nRes
1af48 43 6f 6c 75 6d 6e 2a 43 4f 4c 4e 41 4d 45 5f 4e Column*COLNAME_N
1af49 29 3b 0a 20 20 76 64 62 65 46 72 65 65 4f 70 41 );. vdbeFreeOpA
1af4a 72 72 61 79 28 64 62 2c 20 70 2d 3e 61 4f 70 2c rray(db, p->aOp,
1af4b 20 70 2d 3e 6e 4f 70 29 3b 0a 20 20 73 71 6c 69 p->nOp);. sqli
1af4c 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d te3DbFree(db, p-
1af4d 3e 61 4c 61 62 65 6c 29 3b 0a 20 20 73 71 6c 69 >aLabel);. sqli
1af4e 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d te3DbFree(db, p-
1af4f 3e 61 43 6f 6c 4e 61 6d 65 29 3b 0a 20 20 73 71 >aColName);. sq
1af50 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 lite3DbFree(db,
1af51 70 2d 3e 7a 53 71 6c 29 3b 0a 20 20 70 2d 3e 6d p->zSql);. p->m
1af52 61 67 69 63 20 3d 20 56 44 42 45 5f 4d 41 47 49 agic = VDBE_MAGI
1af53 43 5f 44 45 41 44 3b 0a 20 20 73 71 6c 69 74 65 C_DEAD;. sqlite
1af54 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 70 3DbFree(db, p->p
1af55 46 72 65 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 Free);. sqlite3
1af56 44 62 46 72 65 65 28 64 62 2c 20 70 29 3b 0a 7d DbFree(db, p);.}
1af57 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 ../*.** Make sur
1af58 65 20 74 68 65 20 63 75 72 73 6f 72 20 70 20 69 e the cursor p i
1af59 73 20 72 65 61 64 79 20 74 6f 20 72 65 61 64 20 s ready to read
1af5a 6f 72 20 77 72 69 74 65 20 74 68 65 20 72 6f 77 or write the row
1af5b 20 74 6f 20 77 68 69 63 68 20 69 74 0a 2a 2a 20 to which it.**
1af5c 77 61 73 20 6c 61 73 74 20 70 6f 73 69 74 69 6f was last positio
1af5d 6e 65 64 2e 20 20 52 65 74 75 72 6e 20 61 6e 20 ned. Return an
1af5e 65 72 72 6f 72 20 63 6f 64 65 20 69 66 20 61 6e error code if an
1af5f 20 4f 4f 4d 20 66 61 75 6c 74 20 6f 72 20 49 2f OOM fault or I/
1af60 4f 20 65 72 72 6f 72 0a 2a 2a 20 70 72 65 76 65 O error.** preve
1af61 6e 74 73 20 75 73 20 66 72 6f 6d 20 70 6f 73 69 nts us from posi
1af62 74 69 6f 6e 69 6e 67 20 74 68 65 20 63 75 72 73 tioning the curs
1af63 6f 72 20 74 6f 20 69 74 73 20 63 6f 72 72 65 63 or to its correc
1af64 74 20 70 6f 73 69 74 69 6f 6e 2e 0a 2a 2a 0a 2a t position..**.*
1af65 2a 20 49 66 20 61 20 4d 6f 76 65 54 6f 20 6f 70 * If a MoveTo op
1af66 65 72 61 74 69 6f 6e 20 69 73 20 70 65 6e 64 69 eration is pendi
1af67 6e 67 20 6f 6e 20 74 68 65 20 67 69 76 65 6e 20 ng on the given
1af68 63 75 72 73 6f 72 2c 20 74 68 65 6e 20 64 6f 20 cursor, then do
1af69 74 68 61 74 0a 2a 2a 20 4d 6f 76 65 54 6f 20 6e that.** MoveTo n
1af6a 6f 77 2e 20 20 49 66 20 6e 6f 20 6d 6f 76 65 20 ow. If no move
1af6b 69 73 20 70 65 6e 64 69 6e 67 2c 20 63 68 65 63 is pending, chec
1af6c 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68 65 20 k to see if the
1af6d 72 6f 77 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 row has been.**
1af6e 64 65 6c 65 74 65 64 20 6f 75 74 20 66 72 6f 6d deleted out from
1af6f 20 75 6e 64 65 72 20 74 68 65 20 63 75 72 73 6f under the curso
1af70 72 20 61 6e 64 20 69 66 20 69 74 20 68 61 73 2c r and if it has,
1af71 20 6d 61 72 6b 20 74 68 65 20 72 6f 77 20 61 73 mark the row as
1af72 0a 2a 2a 20 61 20 4e 55 4c 4c 20 72 6f 77 2e 0a .** a NULL row..
1af73 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 63 75 72 **.** If the cur
1af74 73 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20 70 sor is already p
1af75 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 63 ointing to the c
1af76 6f 72 72 65 63 74 20 72 6f 77 20 61 6e 64 20 74 orrect row and t
1af77 68 61 74 20 72 6f 77 20 68 61 73 0a 2a 2a 20 6e hat row has.** n
1af78 6f 74 20 62 65 65 6e 20 64 65 6c 65 74 65 64 20 ot been deleted
1af79 6f 75 74 20 66 72 6f 6d 20 75 6e 64 65 72 20 74 out from under t
1af7a 68 65 20 63 75 72 73 6f 72 2c 20 74 68 65 6e 20 he cursor, then
1af7b 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 this routine is
1af7c 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 53 51 4c 49 a no-op..*/.SQLI
1af7d 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
1af7e 71 6c 69 74 65 33 56 64 62 65 43 75 72 73 6f 72 qlite3VdbeCursor
1af7f 4d 6f 76 65 74 6f 28 56 64 62 65 43 75 72 73 6f Moveto(VdbeCurso
1af80 72 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e r *p){. if( p->
1af81 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20 29 deferredMoveto )
1af82 7b 0a 20 20 20 20 69 6e 74 20 72 65 73 2c 20 72 {. int res, r
1af83 63 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 c;.#ifdef SQLITE
1af84 5f 54 45 53 54 0a 20 20 20 20 65 78 74 65 72 6e _TEST. extern
1af85 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 65 61 int sqlite3_sea
1af86 72 63 68 5f 63 6f 75 6e 74 3b 0a 23 65 6e 64 69 rch_count;.#endi
1af87 66 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d f. assert( p-
1af88 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 >isTable );.
1af89 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 rc = sqlite3Btre
1af8a 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 eMovetoUnpacked(
1af8b 70 2d 3e 70 43 75 72 73 6f 72 2c 20 30 2c 20 70 p->pCursor, 0, p
1af8c 2d 3e 6d 6f 76 65 74 6f 54 61 72 67 65 74 2c 20 ->movetoTarget,
1af8d 30 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 69 66 0, &res);. if
1af8e 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 ( rc ) return rc
1af8f 3b 0a 20 20 20 20 70 2d 3e 6c 61 73 74 52 6f 77 ;. p->lastRow
1af90 69 64 20 3d 20 70 2d 3e 6d 6f 76 65 74 6f 54 61 id = p->movetoTa
1af91 72 67 65 74 3b 0a 20 20 20 20 70 2d 3e 72 6f 77 rget;. p->row
1af92 69 64 49 73 56 61 6c 69 64 20 3d 20 41 4c 57 41 idIsValid = ALWA
1af93 59 53 28 72 65 73 3d 3d 30 29 20 3f 31 3a 30 3b YS(res==0) ?1:0;
1af94 0a 20 20 20 20 69 66 28 20 4e 45 56 45 52 28 72 . if( NEVER(r
1af95 65 73 3c 30 29 20 29 7b 0a 20 20 20 20 20 20 72 es<0) ){. r
1af96 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 c = sqlite3Btree
1af97 4e 65 78 74 28 70 2d 3e 70 43 75 72 73 6f 72 2c Next(p->pCursor,
1af98 20 26 72 65 73 29 3b 0a 20 20 20 20 20 20 69 66 &res);. if
1af99 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 ( rc ) return rc
1af9a 3b 0a 20 20 20 20 7d 0a 23 69 66 64 65 66 20 53 ;. }.#ifdef S
1af9b 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 20 20 73 QLITE_TEST. s
1af9c 71 6c 69 74 65 33 5f 73 65 61 72 63 68 5f 63 6f qlite3_search_co
1af9d 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 unt++;.#endif.
1af9e 20 20 70 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 p->deferredMov
1af9f 65 74 6f 20 3d 20 30 3b 0a 20 20 20 20 70 2d 3e eto = 0;. p->
1afa0 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 43 41 cacheStatus = CA
1afa1 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 7d 65 6c CHE_STALE;. }el
1afa2 73 65 20 69 66 28 20 41 4c 57 41 59 53 28 70 2d se if( ALWAYS(p-
1afa3 3e 70 43 75 72 73 6f 72 29 20 29 7b 0a 20 20 20 >pCursor) ){.
1afa4 20 69 6e 74 20 68 61 73 4d 6f 76 65 64 3b 0a 20 int hasMoved;.
1afa5 20 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 int rc = sqli
1afa6 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 48 61 te3BtreeCursorHa
1afa7 73 4d 6f 76 65 64 28 70 2d 3e 70 43 75 72 73 6f sMoved(p->pCurso
1afa8 72 2c 20 26 68 61 73 4d 6f 76 65 64 29 3b 0a 20 r, &hasMoved);.
1afa9 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 if( rc ) retu
1afaa 72 6e 20 72 63 3b 0a 20 20 20 20 69 66 28 20 68 rn rc;. if( h
1afab 61 73 4d 6f 76 65 64 20 29 7b 0a 20 20 20 20 20 asMoved ){.
1afac 20 70 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 p->cacheStatus
1afad 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 = CACHE_STALE;.
1afae 20 20 20 20 20 70 2d 3e 6e 75 6c 6c 52 6f 77 20 p->nullRow
1afaf 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 = 1;. }. }.
1afb0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
1afb1 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 K;.}../*.** The
1afb2 66 6f 6c 6c 6f 77 69 6e 67 20 66 75 6e 63 74 69 following functi
1afb3 6f 6e 73 3a 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74 ons:.**.** sqlit
1afb4 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 e3VdbeSerialType
1afb5 28 29 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 ().** sqlite3Vdb
1afb6 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 29 eSerialTypeLen()
1afb7 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 53 .** sqlite3VdbeS
1afb8 65 72 69 61 6c 4c 65 6e 28 29 0a 2a 2a 20 73 71 erialLen().** sq
1afb9 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 50 lite3VdbeSerialP
1afba 75 74 28 29 0a 2a 2a 20 73 71 6c 69 74 65 33 56 ut().** sqlite3V
1afbb 64 62 65 53 65 72 69 61 6c 47 65 74 28 29 0a 2a dbeSerialGet().*
1afbc 2a 0a 2a 2a 20 65 6e 63 61 70 73 75 6c 61 74 65 *.** encapsulate
1afbd 20 74 68 65 20 63 6f 64 65 20 74 68 61 74 20 73 the code that s
1afbe 65 72 69 61 6c 69 7a 65 73 20 76 61 6c 75 65 73 erializes values
1afbf 20 66 6f 72 20 73 74 6f 72 61 67 65 20 69 6e 20 for storage in
1afc0 53 51 4c 69 74 65 0a 2a 2a 20 64 61 74 61 20 61 SQLite.** data a
1afc1 6e 64 20 69 6e 64 65 78 20 72 65 63 6f 72 64 73 nd index records
1afc2 2e 20 45 61 63 68 20 73 65 72 69 61 6c 69 7a 65 . Each serialize
1afc3 64 20 76 61 6c 75 65 20 63 6f 6e 73 69 73 74 73 d value consists
1afc4 20 6f 66 20 61 0a 2a 2a 20 27 73 65 72 69 61 6c of a.** 'serial
1afc5 2d 74 79 70 65 27 20 61 6e 64 20 61 20 62 6c 6f -type' and a blo
1afc6 62 20 6f 66 20 64 61 74 61 2e 20 54 68 65 20 73 b of data. The s
1afc7 65 72 69 61 6c 20 74 79 70 65 20 69 73 20 61 6e erial type is an
1afc8 20 38 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 64 8-byte unsigned
1afc9 0a 2a 2a 20 69 6e 74 65 67 65 72 2c 20 73 74 6f .** integer, sto
1afca 72 65 64 20 61 73 20 61 20 76 61 72 69 6e 74 2e red as a varint.
1afcb 0a 2a 2a 0a 2a 2a 20 49 6e 20 61 6e 20 53 51 4c .**.** In an SQL
1afcc 69 74 65 20 69 6e 64 65 78 20 72 65 63 6f 72 64 ite index record
1afcd 2c 20 74 68 65 20 73 65 72 69 61 6c 20 74 79 70 , the serial typ
1afce 65 20 69 73 20 73 74 6f 72 65 64 20 64 69 72 65 e is stored dire
1afcf 63 74 6c 79 20 62 65 66 6f 72 65 0a 2a 2a 20 74 ctly before.** t
1afd0 68 65 20 62 6c 6f 62 20 6f 66 20 64 61 74 61 20 he blob of data
1afd1 74 68 61 74 20 69 74 20 63 6f 72 72 65 73 70 6f that it correspo
1afd2 6e 64 73 20 74 6f 2e 20 49 6e 20 61 20 74 61 62 nds to. In a tab
1afd3 6c 65 20 72 65 63 6f 72 64 2c 20 61 6c 6c 20 73 le record, all s
1afd4 65 72 69 61 6c 0a 2a 2a 20 74 79 70 65 73 20 61 erial.** types a
1afd5 72 65 20 73 74 6f 72 65 64 20 61 74 20 74 68 65 re stored at the
1afd6 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 72 65 start of the re
1afd7 63 6f 72 64 2c 20 61 6e 64 20 74 68 65 20 62 6c cord, and the bl
1afd8 6f 62 73 20 6f 66 20 64 61 74 61 20 61 74 0a 2a obs of data at.*
1afd9 2a 20 74 68 65 20 65 6e 64 2e 20 48 65 6e 63 65 * the end. Hence
1afda 20 74 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 these functions
1afdb 20 61 6c 6c 6f 77 20 74 68 65 20 63 61 6c 6c 65 allow the calle
1afdc 72 20 74 6f 20 68 61 6e 64 6c 65 20 74 68 65 0a r to handle the.
1afdd 2a 2a 20 73 65 72 69 61 6c 2d 74 79 70 65 20 61 ** serial-type a
1afde 6e 64 20 64 61 74 61 20 62 6c 6f 62 20 73 65 70 nd data blob sep
1afdf 65 72 61 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 erately..**.** T
1afe0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 61 62 he following tab
1afe1 6c 65 20 64 65 73 63 72 69 62 65 73 20 74 68 65 le describes the
1afe2 20 76 61 72 69 6f 75 73 20 73 74 6f 72 61 67 65 various storage
1afe3 20 63 6c 61 73 73 65 73 20 66 6f 72 20 64 61 74 classes for dat
1afe4 61 3a 0a 2a 2a 0a 2a 2a 20 20 20 73 65 72 69 61 a:.**.** seria
1afe5 6c 20 74 79 70 65 20 20 20 20 20 20 20 20 62 79 l type by
1afe6 74 65 73 20 6f 66 20 64 61 74 61 20 20 20 20 20 tes of data
1afe7 20 74 79 70 65 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d type.** -----
1afe8 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20 20 20 2d 2d --------- --
1afe9 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20 -------------
1afea 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ---------------
1afeb 0a 2a 2a 20 20 20 20 20 20 30 20 20 20 20 20 20 .** 0
1afec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 0
1afed 20 20 20 20 20 20 20 20 20 20 20 20 4e 55 4c 4c NULL
1afee 0a 2a 2a 20 20 20 20 20 20 31 20 20 20 20 20 20 .** 1
1afef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
1aff0 20 20 20 20 20 20 20 20 20 20 20 20 73 69 67 6e sign
1aff1 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 ed integer.**
1aff2 20 20 20 32 20 20 20 20 20 20 20 20 20 20 20 20 2
1aff3 20 20 20 20 20 20 20 20 20 32 20 20 20 20 20 20 2
1aff4 20 20 20 20 20 20 73 69 67 6e 65 64 20 69 6e 74 signed int
1aff5 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 33 20 20 eger.** 3
1aff6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1aff7 20 20 20 33 20 20 20 20 20 20 20 20 20 20 20 20 3
1aff8 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a signed integer.*
1aff9 2a 20 20 20 20 20 20 34 20 20 20 20 20 20 20 20 * 4
1affa 20 20 20 20 20 20 20 20 20 20 20 20 20 34 20 20 4
1affb 20 20 20 20 20 20 20 20 20 20 73 69 67 6e 65 64 signed
1affc 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 integer.**
1affd 20 35 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5
1affe 20 20 20 20 20 20 20 36 20 20 20 20 20 20 20 20 6
1afff 20 20 20 20 73 69 67 6e 65 64 20 69 6e 74 65 67 signed integ
1b000 65 72 0a 2a 2a 20 20 20 20 20 20 36 20 20 20 20 er.** 6
1b001 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b002 20 38 20 20 20 20 20 20 20 20 20 20 20 20 73 69 8 si
1b003 67 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 gned integer.**
1b004 20 20 20 20 20 37 20 20 20 20 20 20 20 20 20 20 7
1b005 20 20 20 20 20 20 20 20 20 20 20 38 20 20 20 20 8
1b006 20 20 20 20 20 20 20 20 49 45 45 45 20 66 6c 6f IEEE flo
1b007 61 74 0a 2a 2a 20 20 20 20 20 20 38 20 20 20 20 at.** 8
1b008 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b009 20 30 20 20 20 20 20 20 20 20 20 20 20 20 49 6e 0 In
1b00a 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 20 30 teger constant 0
1b00b 0a 2a 2a 20 20 20 20 20 20 39 20 20 20 20 20 20 .** 9
1b00c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 0
1b00d 20 20 20 20 20 20 20 20 20 20 20 20 49 6e 74 65 Inte
1b00e 67 65 72 20 63 6f 6e 73 74 61 6e 74 20 31 0a 2a ger constant 1.*
1b00f 2a 20 20 20 20 20 31 30 2c 31 31 20 20 20 20 20 * 10,11
1b010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b011 20 20 20 20 20 20 20 20 20 20 72 65 73 65 72 76 reserv
1b012 65 64 20 66 6f 72 20 65 78 70 61 6e 73 69 6f 6e ed for expansion
1b013 0a 2a 2a 20 20 20 20 4e 3e 3d 31 32 20 61 6e 64 .** N>=12 and
1b014 20 65 76 65 6e 20 20 20 20 20 20 20 28 4e 2d 31 even (N-1
1b015 32 29 2f 32 20 20 20 20 20 20 20 20 42 4c 4f 42 2)/2 BLOB
1b016 0a 2a 2a 20 20 20 20 4e 3e 3d 31 33 20 61 6e 64 .** N>=13 and
1b017 20 6f 64 64 20 20 20 20 20 20 20 20 28 4e 2d 31 odd (N-1
1b018 33 29 2f 32 20 20 20 20 20 20 20 20 74 65 78 74 3)/2 text
1b019 0a 2a 2a 0a 2a 2a 20 54 68 65 20 38 20 61 6e 64 .**.** The 8 and
1b01a 20 39 20 74 79 70 65 73 20 77 65 72 65 20 61 64 9 types were ad
1b01b 64 65 64 20 69 6e 20 33 2e 33 2e 30 2c 20 66 69 ded in 3.3.0, fi
1b01c 6c 65 20 66 6f 72 6d 61 74 20 34 2e 20 20 50 72 le format 4. Pr
1b01d 69 6f 72 20 76 65 72 73 69 6f 6e 73 0a 2a 2a 20 ior versions.**
1b01e 6f 66 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e of SQLite will n
1b01f 6f 74 20 75 6e 64 65 72 73 74 61 6e 64 20 74 68 ot understand th
1b020 6f 73 65 20 73 65 72 69 61 6c 20 74 79 70 65 73 ose serial types
1b021 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 ..*/../*.** Retu
1b022 72 6e 20 74 68 65 20 73 65 72 69 61 6c 2d 74 79 rn the serial-ty
1b023 70 65 20 66 6f 72 20 74 68 65 20 76 61 6c 75 65 pe for the value
1b024 20 73 74 6f 72 65 64 20 69 6e 20 70 4d 65 6d 2e stored in pMem.
1b025 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1b026 54 45 20 75 33 32 20 73 71 6c 69 74 65 33 56 64 TE u32 sqlite3Vd
1b027 62 65 53 65 72 69 61 6c 54 79 70 65 28 4d 65 6d beSerialType(Mem
1b028 20 2a 70 4d 65 6d 2c 20 69 6e 74 20 66 69 6c 65 *pMem, int file
1b029 5f 66 6f 72 6d 61 74 29 7b 0a 20 20 69 6e 74 20 _format){. int
1b02a 66 6c 61 67 73 20 3d 20 70 4d 65 6d 2d 3e 66 6c flags = pMem->fl
1b02b 61 67 73 3b 0a 20 20 69 6e 74 20 6e 3b 0a 0a 20 ags;. int n;..
1b02c 20 69 66 28 20 66 6c 61 67 73 26 4d 45 4d 5f 4e if( flags&MEM_N
1b02d 75 6c 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ull ){. retur
1b02e 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 66 n 0;. }. if( f
1b02f 6c 61 67 73 26 4d 45 4d 5f 49 6e 74 20 29 7b 0a lags&MEM_Int ){.
1b030 20 20 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 /* Figure ou
1b031 74 20 77 68 65 74 68 65 72 20 74 6f 20 75 73 65 t whether to use
1b032 20 31 2c 20 32 2c 20 34 2c 20 36 20 6f 72 20 38 1, 2, 4, 6 or 8
1b033 20 62 79 74 65 73 2e 20 2a 2f 0a 23 20 20 20 64 bytes. */.# d
1b034 65 66 69 6e 65 20 4d 41 58 5f 36 42 59 54 45 20 efine MAX_6BYTE
1b035 28 28 28 28 69 36 34 29 30 78 30 30 30 30 38 30 ((((i64)0x000080
1b036 30 30 29 3c 3c 33 32 29 2d 31 29 0a 20 20 20 20 00)<<32)-1).
1b037 69 36 34 20 69 20 3d 20 70 4d 65 6d 2d 3e 75 2e i64 i = pMem->u.
1b038 69 3b 0a 20 20 20 20 75 36 34 20 75 3b 0a 20 20 i;. u64 u;.
1b039 20 20 69 66 28 20 66 69 6c 65 5f 66 6f 72 6d 61 if( file_forma
1b03a 74 3e 3d 34 20 26 26 20 28 69 26 31 29 3d 3d 69 t>=4 && (i&1)==i
1b03b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
1b03c 20 38 2b 28 75 33 32 29 69 3b 0a 20 20 20 20 7d 8+(u32)i;. }
1b03d 0a 20 20 20 20 75 20 3d 20 69 3c 30 20 3f 20 2d . u = i<0 ? -
1b03e 69 20 3a 20 69 3b 0a 20 20 20 20 69 66 28 20 75 i : i;. if( u
1b03f 3c 3d 31 32 37 20 29 20 72 65 74 75 72 6e 20 31 <=127 ) return 1
1b040 3b 0a 20 20 20 20 69 66 28 20 75 3c 3d 33 32 37 ;. if( u<=327
1b041 36 37 20 29 20 72 65 74 75 72 6e 20 32 3b 0a 20 67 ) return 2;.
1b042 20 20 20 69 66 28 20 75 3c 3d 38 33 38 38 36 30 if( u<=838860
1b043 37 20 29 20 72 65 74 75 72 6e 20 33 3b 0a 20 20 7 ) return 3;.
1b044 20 20 69 66 28 20 75 3c 3d 32 31 34 37 34 38 33 if( u<=2147483
1b045 36 34 37 20 29 20 72 65 74 75 72 6e 20 34 3b 0a 647 ) return 4;.
1b046 20 20 20 20 69 66 28 20 75 3c 3d 4d 41 58 5f 36 if( u<=MAX_6
1b047 42 59 54 45 20 29 20 72 65 74 75 72 6e 20 35 3b BYTE ) return 5;
1b048 0a 20 20 20 20 72 65 74 75 72 6e 20 36 3b 0a 20 . return 6;.
1b049 20 7d 0a 20 20 69 66 28 20 66 6c 61 67 73 26 4d }. if( flags&M
1b04a 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20 72 EM_Real ){. r
1b04b 65 74 75 72 6e 20 37 3b 0a 20 20 7d 0a 20 20 61 eturn 7;. }. a
1b04c 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 2d ssert( pMem->db-
1b04d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 7c 7c >mallocFailed ||
1b04e 20 66 6c 61 67 73 26 28 4d 45 4d 5f 53 74 72 7c flags&(MEM_Str|
1b04f 4d 45 4d 5f 42 6c 6f 62 29 20 29 3b 0a 20 20 6e MEM_Blob) );. n
1b050 20 3d 20 70 4d 65 6d 2d 3e 6e 3b 0a 20 20 69 66 = pMem->n;. if
1b051 28 20 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 ( flags & MEM_Ze
1b052 72 6f 20 29 7b 0a 20 20 20 20 6e 20 2b 3d 20 70 ro ){. n += p
1b053 4d 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 Mem->u.nZero;.
1b054 7d 0a 20 20 61 73 73 65 72 74 28 20 6e 3e 3d 30 }. assert( n>=0
1b055 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 28 6e );. return ((n
1b056 2a 32 29 20 2b 20 31 32 20 2b 20 28 28 66 6c 61 *2) + 12 + ((fla
1b057 67 73 26 4d 45 4d 5f 53 74 72 29 21 3d 30 29 29 gs&MEM_Str)!=0))
1b058 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 ;.}../*.** Retur
1b059 6e 20 74 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 n the length of
1b05a 74 68 65 20 64 61 74 61 20 63 6f 72 72 65 73 70 the data corresp
1b05b 6f 6e 64 69 6e 67 20 74 6f 20 74 68 65 20 73 75 onding to the su
1b05c 70 70 6c 69 65 64 20 73 65 72 69 61 6c 2d 74 79 pplied serial-ty
1b05d 70 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 pe..*/.SQLITE_PR
1b05e 49 56 41 54 45 20 75 33 32 20 73 71 6c 69 74 65 IVATE u32 sqlite
1b05f 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 4c 3VdbeSerialTypeL
1b060 65 6e 28 75 33 32 20 73 65 72 69 61 6c 5f 74 79 en(u32 serial_ty
1b061 70 65 29 7b 0a 20 20 69 66 28 20 73 65 72 69 61 pe){. if( seria
1b062 6c 5f 74 79 70 65 3e 3d 31 32 20 29 7b 0a 20 20 l_type>=12 ){.
1b063 20 20 72 65 74 75 72 6e 20 28 73 65 72 69 61 6c return (serial
1b064 5f 74 79 70 65 2d 31 32 29 2f 32 3b 0a 20 20 7d _type-12)/2;. }
1b065 65 6c 73 65 7b 0a 20 20 20 20 73 74 61 74 69 63 else{. static
1b066 20 63 6f 6e 73 74 20 75 38 20 61 53 69 7a 65 5b const u8 aSize[
1b067 5d 20 3d 20 7b 20 30 2c 20 31 2c 20 32 2c 20 33 ] = { 0, 1, 2, 3
1b068 2c 20 34 2c 20 36 2c 20 38 2c 20 38 2c 20 30 2c , 4, 6, 8, 8, 0,
1b069 20 30 2c 20 30 2c 20 30 20 7d 3b 0a 20 20 20 20 0, 0, 0 };.
1b06a 72 65 74 75 72 6e 20 61 53 69 7a 65 5b 73 65 72 return aSize[ser
1b06b 69 61 6c 5f 74 79 70 65 5d 3b 0a 20 20 7d 0a 7d ial_type];. }.}
1b06c 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 61 72 ../*.** If we ar
1b06d 65 20 6f 6e 20 61 6e 20 61 72 63 68 69 74 65 63 e on an architec
1b06e 74 75 72 65 20 77 69 74 68 20 6d 69 78 65 64 2d ture with mixed-
1b06f 65 6e 64 69 61 6e 20 66 6c 6f 61 74 69 6e 67 20 endian floating
1b070 0a 2a 2a 20 70 6f 69 6e 74 73 20 28 65 78 3a 20 .** points (ex:
1b071 41 52 4d 37 29 20 74 68 65 6e 20 73 77 61 70 20 ARM7) then swap
1b072 74 68 65 20 6c 6f 77 65 72 20 34 20 62 79 74 65 the lower 4 byte
1b073 73 20 77 69 74 68 20 74 68 65 20 0a 2a 2a 20 75 s with the .** u
1b074 70 70 65 72 20 34 20 62 79 74 65 73 2e 20 20 52 pper 4 bytes. R
1b075 65 74 75 72 6e 20 74 68 65 20 72 65 73 75 6c 74 eturn the result
1b076 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 6d 6f 73 74 ..**.** For most
1b077 20 61 72 63 68 69 74 65 63 74 75 72 65 73 2c 20 architectures,
1b078 74 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 2e this is a no-op.
1b079 0a 2a 2a 0a 2a 2a 20 28 6c 61 74 65 72 29 3a 20 .**.** (later):
1b07a 20 49 74 20 69 73 20 72 65 70 6f 72 74 65 64 20 It is reported
1b07b 74 6f 20 6d 65 20 74 68 61 74 20 74 68 65 20 6d to me that the m
1b07c 69 78 65 64 2d 65 6e 64 69 61 6e 20 70 72 6f 62 ixed-endian prob
1b07d 6c 65 6d 0a 2a 2a 20 6f 6e 20 41 52 4d 37 20 69 lem.** on ARM7 i
1b07e 73 20 61 6e 20 69 73 73 75 65 20 77 69 74 68 20 s an issue with
1b07f 47 43 43 2c 20 6e 6f 74 20 77 69 74 68 20 74 68 GCC, not with th
1b080 65 20 41 52 4d 37 20 63 68 69 70 2e 20 20 49 74 e ARM7 chip. It
1b081 20 73 65 65 6d 73 0a 2a 2a 20 74 68 61 74 20 65 seems.** that e
1b082 61 72 6c 79 20 76 65 72 73 69 6f 6e 73 20 6f 66 arly versions of
1b083 20 47 43 43 20 73 74 6f 72 65 64 20 74 68 65 20 GCC stored the
1b084 74 77 6f 20 77 6f 72 64 73 20 6f 66 20 61 20 36 two words of a 6
1b085 34 2d 62 69 74 0a 2a 2a 20 66 6c 6f 61 74 20 69 4-bit.** float i
1b086 6e 20 74 68 65 20 77 72 6f 6e 67 20 6f 72 64 65 n the wrong orde
1b087 72 2e 20 20 41 6e 64 20 74 68 61 74 20 65 72 72 r. And that err
1b088 6f 72 20 68 61 73 20 62 65 65 6e 20 70 72 6f 70 or has been prop
1b089 61 67 61 74 65 64 0a 2a 2a 20 65 76 65 72 20 73 agated.** ever s
1b08a 69 6e 63 65 2e 20 20 54 68 65 20 62 6c 61 6d 65 ince. The blame
1b08b 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 is not necessar
1b08c 69 6c 79 20 77 69 74 68 20 47 43 43 2c 20 74 68 ily with GCC, th
1b08d 6f 75 67 68 2e 0a 2a 2a 20 47 43 43 20 6d 69 67 ough..** GCC mig
1b08e 68 74 20 68 61 76 65 20 6a 75 73 74 20 63 6f 70 ht have just cop
1b08f 79 69 6e 67 20 74 68 65 20 70 72 6f 62 6c 65 6d ying the problem
1b090 20 66 72 6f 6d 20 61 20 70 72 69 6f 72 20 63 6f from a prior co
1b091 6d 70 69 6c 65 72 2e 0a 2a 2a 20 49 20 61 6d 20 mpiler..** I am
1b092 61 6c 73 6f 20 74 6f 6c 64 20 74 68 61 74 20 6e also told that n
1b093 65 77 65 72 20 76 65 72 73 69 6f 6e 73 20 6f 66 ewer versions of
1b094 20 47 43 43 20 74 68 61 74 20 66 6f 6c 6c 6f 77 GCC that follow
1b095 20 61 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 a different.**
1b096 41 42 49 20 67 65 74 20 74 68 65 20 62 79 74 65 ABI get the byte
1b097 20 6f 72 64 65 72 20 72 69 67 68 74 2e 0a 2a 2a order right..**
1b098 0a 2a 2a 20 44 65 76 65 6c 6f 70 65 72 73 20 75 .** Developers u
1b099 73 69 6e 67 20 53 51 4c 69 74 65 20 6f 6e 20 61 sing SQLite on a
1b09a 6e 20 41 52 4d 37 20 73 68 6f 75 6c 64 20 63 6f n ARM7 should co
1b09b 6d 70 69 6c 65 20 61 6e 64 20 72 75 6e 20 74 68 mpile and run th
1b09c 65 69 72 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 eir.** applicati
1b09d 6f 6e 20 75 73 69 6e 67 20 2d 44 53 51 4c 49 54 on using -DSQLIT
1b09e 45 5f 44 45 42 55 47 3d 31 20 61 74 20 6c 65 61 E_DEBUG=1 at lea
1b09f 73 74 20 6f 6e 63 65 2e 20 20 57 69 74 68 20 44 st once. With D
1b0a0 45 42 55 47 0a 2a 2a 20 65 6e 61 62 6c 65 64 2c EBUG.** enabled,
1b0a1 20 73 6f 6d 65 20 61 73 73 65 72 74 73 20 62 65 some asserts be
1b0a2 6c 6f 77 20 77 69 6c 6c 20 65 6e 73 75 72 65 20 low will ensure
1b0a3 74 68 61 74 20 74 68 65 20 62 79 74 65 20 6f 72 that the byte or
1b0a4 64 65 72 20 6f 66 0a 2a 2a 20 66 6c 6f 61 74 69 der of.** floati
1b0a5 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 73 20 ng point values
1b0a6 69 73 20 63 6f 72 72 65 63 74 2e 0a 2a 2a 0a 2a is correct..**.*
1b0a7 2a 20 28 32 30 30 37 2d 30 38 2d 33 30 29 20 20 * (2007-08-30)
1b0a8 46 72 61 6e 6b 20 76 61 6e 20 56 75 67 74 20 68 Frank van Vugt h
1b0a9 61 73 20 73 74 75 64 69 65 64 20 74 68 69 73 20 as studied this
1b0aa 70 72 6f 62 6c 65 6d 20 63 6c 6f 73 65 6c 79 0a problem closely.
1b0ab 2a 2a 20 61 6e 64 20 68 61 73 20 73 65 6e 64 20 ** and has send
1b0ac 68 69 73 20 66 69 6e 64 69 6e 67 73 20 74 6f 20 his findings to
1b0ad 74 68 65 20 53 51 4c 69 74 65 20 64 65 76 65 6c the SQLite devel
1b0ae 6f 70 65 72 73 2e 20 20 46 72 61 6e 6b 0a 2a 2a opers. Frank.**
1b0af 20 77 72 69 74 65 73 20 74 68 61 74 20 73 6f 6d writes that som
1b0b0 65 20 4c 69 6e 75 78 20 6b 65 72 6e 65 6c 73 20 e Linux kernels
1b0b1 6f 66 66 65 72 20 66 6c 6f 61 74 69 6e 67 20 70 offer floating p
1b0b2 6f 69 6e 74 20 68 61 72 64 77 61 72 65 0a 2a 2a oint hardware.**
1b0b3 20 65 6d 75 6c 61 74 69 6f 6e 20 74 68 61 74 20 emulation that
1b0b4 75 73 65 73 20 6f 6e 6c 79 20 33 32 2d 62 69 74 uses only 32-bit
1b0b5 20 6d 61 6e 74 69 73 73 61 73 20 69 6e 73 74 65 mantissas inste
1b0b6 61 64 20 6f 66 20 61 20 66 75 6c 6c 20 0a 2a 2a ad of a full .**
1b0b7 20 34 38 2d 62 69 74 73 20 61 73 20 72 65 71 75 48-bits as requ
1b0b8 69 72 65 64 20 62 79 20 74 68 65 20 49 45 45 45 ired by the IEEE
1b0b9 20 73 74 61 6e 64 61 72 64 2e 20 20 28 54 68 69 standard. (Thi
1b0ba 73 20 69 73 20 74 68 65 0a 2a 2a 20 43 4f 4e 46 s is the.** CONF
1b0bb 49 47 5f 46 50 45 5f 46 41 53 54 46 50 45 20 6f IG_FPE_FASTFPE o
1b0bc 70 74 69 6f 6e 2e 29 20 20 4f 6e 20 73 75 63 68 ption.) On such
1b0bd 20 73 79 73 74 65 6d 73 2c 20 66 6c 6f 61 74 69 systems, floati
1b0be 6e 67 20 70 6f 69 6e 74 0a 2a 2a 20 62 79 74 65 ng point.** byte
1b0bf 20 73 77 61 70 70 69 6e 67 20 62 65 63 6f 6d 65 swapping become
1b0c0 73 20 76 65 72 79 20 63 6f 6d 70 6c 69 63 61 74 s very complicat
1b0c1 65 64 2e 20 20 54 6f 20 61 76 6f 69 64 20 70 72 ed. To avoid pr
1b0c2 6f 62 6c 65 6d 73 2c 0a 2a 2a 20 74 68 65 20 6e oblems,.** the n
1b0c3 65 63 65 73 73 61 72 79 20 62 79 74 65 20 73 77 ecessary byte sw
1b0c4 61 70 70 69 6e 67 20 69 73 20 63 61 72 72 69 65 apping is carrie
1b0c5 64 20 6f 75 74 20 75 73 69 6e 67 20 61 20 36 34 d out using a 64
1b0c6 2d 62 69 74 20 69 6e 74 65 67 65 72 0a 2a 2a 20 -bit integer.**
1b0c7 72 61 74 68 65 72 20 74 68 61 6e 20 61 20 36 34 rather than a 64
1b0c8 2d 62 69 74 20 66 6c 6f 61 74 2e 20 20 46 72 61 -bit float. Fra
1b0c9 6e 6b 20 61 73 73 75 72 65 73 20 75 73 20 74 68 nk assures us th
1b0ca 61 74 20 74 68 65 20 63 6f 64 65 20 68 65 72 65 at the code here
1b0cb 0a 2a 2a 20 77 6f 72 6b 73 20 66 6f 72 20 68 69 .** works for hi
1b0cc 6d 2e 20 20 57 65 2c 20 74 68 65 20 64 65 76 65 m. We, the deve
1b0cd 6c 6f 70 65 72 73 2c 20 68 61 76 65 20 6e 6f 20 lopers, have no
1b0ce 77 61 79 20 74 6f 20 69 6e 64 65 70 65 6e 64 65 way to independe
1b0cf 6e 74 6c 79 0a 2a 2a 20 76 65 72 69 66 79 20 74 ntly.** verify t
1b0d0 68 69 73 2c 20 62 75 74 20 46 72 61 6e 6b 20 73 his, but Frank s
1b0d1 65 65 6d 73 20 74 6f 20 6b 6e 6f 77 20 77 68 61 eems to know wha
1b0d2 74 20 68 65 20 69 73 20 74 61 6c 6b 69 6e 67 20 t he is talking
1b0d3 61 62 6f 75 74 0a 2a 2a 20 73 6f 20 77 65 20 74 about.** so we t
1b0d4 72 75 73 74 20 68 69 6d 2e 0a 2a 2f 0a 23 69 66 rust him..*/.#if
1b0d5 64 65 66 20 53 51 4c 49 54 45 5f 4d 49 58 45 44 def SQLITE_MIXED
1b0d6 5f 45 4e 44 49 41 4e 5f 36 34 42 49 54 5f 46 4c _ENDIAN_64BIT_FL
1b0d7 4f 41 54 0a 73 74 61 74 69 63 20 75 36 34 20 66 OAT.static u64 f
1b0d8 6c 6f 61 74 53 77 61 70 28 75 36 34 20 69 6e 29 loatSwap(u64 in)
1b0d9 7b 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 20 {. union {.
1b0da 75 36 34 20 72 3b 0a 20 20 20 20 75 33 32 20 69 u64 r;. u32 i
1b0db 5b 32 5d 3b 0a 20 20 7d 20 75 3b 0a 20 20 75 33 [2];. } u;. u3
1b0dc 32 20 74 3b 0a 0a 20 20 75 2e 72 20 3d 20 69 6e 2 t;.. u.r = in
1b0dd 3b 0a 20 20 74 20 3d 20 75 2e 69 5b 30 5d 3b 0a ;. t = u.i[0];.
1b0de 20 20 75 2e 69 5b 30 5d 20 3d 20 75 2e 69 5b 31 u.i[0] = u.i[1
1b0df 5d 3b 0a 20 20 75 2e 69 5b 31 5d 20 3d 20 74 3b ];. u.i[1] = t;
1b0e0 0a 20 20 72 65 74 75 72 6e 20 75 2e 72 3b 0a 7d . return u.r;.}
1b0e1 0a 23 20 64 65 66 69 6e 65 20 73 77 61 70 4d 69 .# define swapMi
1b0e2 78 65 64 45 6e 64 69 61 6e 46 6c 6f 61 74 28 58 xedEndianFloat(X
1b0e3 29 20 20 58 20 3d 20 66 6c 6f 61 74 53 77 61 70 ) X = floatSwap
1b0e4 28 58 29 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 (X).#else.# defi
1b0e5 6e 65 20 73 77 61 70 4d 69 78 65 64 45 6e 64 69 ne swapMixedEndi
1b0e6 61 6e 46 6c 6f 61 74 28 58 29 0a 23 65 6e 64 69 anFloat(X).#endi
1b0e7 66 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 74 f../*.** Write t
1b0e8 68 65 20 73 65 72 69 61 6c 69 7a 65 64 20 64 61 he serialized da
1b0e9 74 61 20 62 6c 6f 62 20 66 6f 72 20 74 68 65 20 ta blob for the
1b0ea 76 61 6c 75 65 20 73 74 6f 72 65 64 20 69 6e 20 value stored in
1b0eb 70 4d 65 6d 20 69 6e 74 6f 20 0a 2a 2a 20 62 75 pMem into .** bu
1b0ec 66 2e 20 49 74 20 69 73 20 61 73 73 75 6d 65 64 f. It is assumed
1b0ed 20 74 68 61 74 20 74 68 65 20 63 61 6c 6c 65 72 that the caller
1b0ee 20 68 61 73 20 61 6c 6c 6f 63 61 74 65 64 20 73 has allocated s
1b0ef 75 66 66 69 63 69 65 6e 74 20 73 70 61 63 65 2e ufficient space.
1b0f0 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e .** Return the n
1b0f1 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 77 umber of bytes w
1b0f2 72 69 74 74 65 6e 2e 0a 2a 2a 0a 2a 2a 20 6e 42 ritten..**.** nB
1b0f3 75 66 20 69 73 20 74 68 65 20 61 6d 6f 75 6e 74 uf is the amount
1b0f4 20 6f 66 20 73 70 61 63 65 20 6c 65 66 74 20 69 of space left i
1b0f5 6e 20 62 75 66 5b 5d 2e 20 20 6e 42 75 66 20 6d n buf[]. nBuf m
1b0f6 75 73 74 20 61 6c 77 61 79 73 20 62 65 0a 2a 2a ust always be.**
1b0f7 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 20 74 6f large enough to
1b0f8 20 68 6f 6c 64 20 74 68 65 20 65 6e 74 69 72 65 hold the entire
1b0f9 20 66 69 65 6c 64 2e 20 20 45 78 63 65 70 74 2c field. Except,
1b0fa 20 69 66 20 74 68 65 20 66 69 65 6c 64 20 69 73 if the field is
1b0fb 0a 2a 2a 20 61 20 62 6c 6f 62 20 77 69 74 68 20 .** a blob with
1b0fc 61 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 74 61 a zero-filled ta
1b0fd 69 6c 2c 20 74 68 65 6e 20 62 75 66 5b 5d 20 6d il, then buf[] m
1b0fe 69 67 68 74 20 62 65 20 6a 75 73 74 20 74 68 65 ight be just the
1b0ff 20 72 69 67 68 74 0a 2a 2a 20 73 69 7a 65 20 74 right.** size t
1b100 6f 20 68 6f 6c 64 20 65 76 65 72 79 74 68 69 6e o hold everythin
1b101 67 20 65 78 63 65 70 74 20 66 6f 72 20 74 68 65 g except for the
1b102 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 74 61 69 zero-filled tai
1b103 6c 2e 20 20 49 66 20 62 75 66 5b 5d 0a 2a 2a 20 l. If buf[].**
1b104 69 73 20 6f 6e 6c 79 20 62 69 67 20 65 6e 6f 75 is only big enou
1b105 67 68 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 6e gh to hold the n
1b106 6f 6e 2d 7a 65 72 6f 20 70 72 65 66 69 78 2c 20 on-zero prefix,
1b107 74 68 65 6e 20 6f 6e 6c 79 20 77 72 69 74 65 20 then only write
1b108 74 68 61 74 0a 2a 2a 20 70 72 65 66 69 78 20 69 that.** prefix i
1b109 6e 74 6f 20 62 75 66 5b 5d 2e 20 20 42 75 74 20 nto buf[]. But
1b10a 69 66 20 62 75 66 5b 5d 20 69 73 20 6c 61 72 67 if buf[] is larg
1b10b 65 20 65 6e 6f 75 67 68 20 74 6f 20 68 6f 6c 64 e enough to hold
1b10c 20 62 6f 74 68 20 74 68 65 0a 2a 2a 20 70 72 65 both the.** pre
1b10d 66 69 78 20 61 6e 64 20 74 68 65 20 74 61 69 6c fix and the tail
1b10e 20 74 68 65 6e 20 77 72 69 74 65 20 74 68 65 20 then write the
1b10f 70 72 65 66 69 78 20 61 6e 64 20 73 65 74 20 74 prefix and set t
1b110 68 65 20 74 61 69 6c 20 74 6f 20 61 6c 6c 0a 2a he tail to all.*
1b111 2a 20 7a 65 72 6f 73 2e 0a 2a 2a 0a 2a 2a 20 52 * zeros..**.** R
1b112 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 eturn the number
1b113 20 6f 66 20 62 79 74 65 73 20 61 63 74 75 61 6c of bytes actual
1b114 6c 79 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 ly written into
1b115 62 75 66 5b 5d 2e 20 20 54 68 65 20 6e 75 6d 62 buf[]. The numb
1b116 65 72 0a 2a 2a 20 6f 66 20 62 79 74 65 73 20 69 er.** of bytes i
1b117 6e 20 74 68 65 20 7a 65 72 6f 2d 66 69 6c 6c 65 n the zero-fille
1b118 64 20 74 61 69 6c 20 69 73 20 69 6e 63 6c 75 64 d tail is includ
1b119 65 64 20 69 6e 20 74 68 65 20 72 65 74 75 72 6e ed in the return
1b11a 20 76 61 6c 75 65 20 6f 6e 6c 79 0a 2a 2a 20 69 value only.** i
1b11b 66 20 74 68 6f 73 65 20 62 79 74 65 73 20 77 65 f those bytes we
1b11c 72 65 20 7a 65 72 6f 65 64 20 69 6e 20 62 75 66 re zeroed in buf
1b11d 5b 5d 2e 0a 2a 2f 20 0a 53 51 4c 49 54 45 5f 50 []..*/ .SQLITE_P
1b11e 52 49 56 41 54 45 20 75 33 32 20 73 71 6c 69 74 RIVATE u32 sqlit
1b11f 65 33 56 64 62 65 53 65 72 69 61 6c 50 75 74 28 e3VdbeSerialPut(
1b120 75 38 20 2a 62 75 66 2c 20 69 6e 74 20 6e 42 75 u8 *buf, int nBu
1b121 66 2c 20 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e f, Mem *pMem, in
1b122 74 20 66 69 6c 65 5f 66 6f 72 6d 61 74 29 7b 0a t file_format){.
1b123 20 20 75 33 32 20 73 65 72 69 61 6c 5f 74 79 70 u32 serial_typ
1b124 65 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 e = sqlite3VdbeS
1b125 65 72 69 61 6c 54 79 70 65 28 70 4d 65 6d 2c 20 erialType(pMem,
1b126 66 69 6c 65 5f 66 6f 72 6d 61 74 29 3b 0a 20 20 file_format);.
1b127 75 33 32 20 6c 65 6e 3b 0a 0a 20 20 2f 2a 20 49 u32 len;.. /* I
1b128 6e 74 65 67 65 72 20 61 6e 64 20 52 65 61 6c 20 nteger and Real
1b129 2a 2f 0a 20 20 69 66 28 20 73 65 72 69 61 6c 5f */. if( serial_
1b12a 74 79 70 65 3c 3d 37 20 26 26 20 73 65 72 69 61 type<=7 && seria
1b12b 6c 5f 74 79 70 65 3e 30 20 29 7b 0a 20 20 20 20 l_type>0 ){.
1b12c 75 36 34 20 76 3b 0a 20 20 20 20 75 33 32 20 69 u64 v;. u32 i
1b12d 3b 0a 20 20 20 20 69 66 28 20 73 65 72 69 61 6c ;. if( serial
1b12e 5f 74 79 70 65 3d 3d 37 20 29 7b 0a 20 20 20 20 _type==7 ){.
1b12f 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 assert( sizeof
1b130 28 76 29 3d 3d 73 69 7a 65 6f 66 28 70 4d 65 6d (v)==sizeof(pMem
1b131 2d 3e 72 29 20 29 3b 0a 20 20 20 20 20 20 6d 65 ->r) );. me
1b132 6d 63 70 79 28 26 76 2c 20 26 70 4d 65 6d 2d 3e mcpy(&v, &pMem->
1b133 72 2c 20 73 69 7a 65 6f 66 28 76 29 29 3b 0a 20 r, sizeof(v));.
1b134 20 20 20 20 20 73 77 61 70 4d 69 78 65 64 45 6e swapMixedEn
1b135 64 69 61 6e 46 6c 6f 61 74 28 76 29 3b 0a 20 20 dianFloat(v);.
1b136 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 76 }else{. v
1b137 20 3d 20 70 4d 65 6d 2d 3e 75 2e 69 3b 0a 20 20 = pMem->u.i;.
1b138 20 20 7d 0a 20 20 20 20 6c 65 6e 20 3d 20 69 20 }. len = i
1b139 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 = sqlite3VdbeSer
1b13a 69 61 6c 54 79 70 65 4c 65 6e 28 73 65 72 69 61 ialTypeLen(seria
1b13b 6c 5f 74 79 70 65 29 3b 0a 20 20 20 20 61 73 73 l_type);. ass
1b13c 65 72 74 28 20 6c 65 6e 3c 3d 28 75 33 32 29 6e ert( len<=(u32)n
1b13d 42 75 66 20 29 3b 0a 20 20 20 20 77 68 69 6c 65 Buf );. while
1b13e 28 20 69 2d 2d 20 29 7b 0a 20 20 20 20 20 20 62 ( i-- ){. b
1b13f 75 66 5b 69 5d 20 3d 20 28 75 38 29 28 76 26 30 uf[i] = (u8)(v&0
1b140 78 46 46 29 3b 0a 20 20 20 20 20 20 76 20 3e 3e xFF);. v >>
1b141 3d 20 38 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 = 8;. }. r
1b142 65 74 75 72 6e 20 6c 65 6e 3b 0a 20 20 7d 0a 0a eturn len;. }..
1b143 20 20 2f 2a 20 53 74 72 69 6e 67 20 6f 72 20 62 /* String or b
1b144 6c 6f 62 20 2a 2f 0a 20 20 69 66 28 20 73 65 72 lob */. if( ser
1b145 69 61 6c 5f 74 79 70 65 3e 3d 31 32 20 29 7b 0a ial_type>=12 ){.
1b146 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d assert( pMem
1b147 2d 3e 6e 20 2b 20 28 28 70 4d 65 6d 2d 3e 66 6c ->n + ((pMem->fl
1b148 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 29 3f ags & MEM_Zero)?
1b149 70 4d 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 3a 30 29 pMem->u.nZero:0)
1b14a 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 3d 3d . ==
1b14b 20 28 69 6e 74 29 73 71 6c 69 74 65 33 56 64 62 (int)sqlite3Vdb
1b14c 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 73 eSerialTypeLen(s
1b14d 65 72 69 61 6c 5f 74 79 70 65 29 20 29 3b 0a 20 erial_type) );.
1b14e 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d assert( pMem-
1b14f 3e 6e 3c 3d 6e 42 75 66 20 29 3b 0a 20 20 20 20 >n<=nBuf );.
1b150 6c 65 6e 20 3d 20 70 4d 65 6d 2d 3e 6e 3b 0a 20 len = pMem->n;.
1b151 20 20 20 6d 65 6d 63 70 79 28 62 75 66 2c 20 70 memcpy(buf, p
1b152 4d 65 6d 2d 3e 7a 2c 20 6c 65 6e 29 3b 0a 20 20 Mem->z, len);.
1b153 20 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 if( pMem->flag
1b154 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a s & MEM_Zero ){.
1b155 20 20 20 20 20 20 6c 65 6e 20 2b 3d 20 70 4d 65 len += pMe
1b156 6d 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 20 20 m->u.nZero;.
1b157 20 20 61 73 73 65 72 74 28 20 6e 42 75 66 3e 3d assert( nBuf>=
1b158 30 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6c 0 );. if( l
1b159 65 6e 20 3e 20 28 75 33 32 29 6e 42 75 66 20 29 en > (u32)nBuf )
1b15a 7b 0a 20 20 20 20 20 20 20 20 6c 65 6e 20 3d 20 {. len =
1b15b 28 75 33 32 29 6e 42 75 66 3b 0a 20 20 20 20 20 (u32)nBuf;.
1b15c 20 7d 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28 }. memset(
1b15d 26 62 75 66 5b 70 4d 65 6d 2d 3e 6e 5d 2c 20 30 &buf[pMem->n], 0
1b15e 2c 20 6c 65 6e 2d 70 4d 65 6d 2d 3e 6e 29 3b 0a , len-pMem->n);.
1b15f 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e }. return
1b160 20 6c 65 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 len;. }.. /*
1b161 4e 55 4c 4c 20 6f 72 20 63 6f 6e 73 74 61 6e 74 NULL or constant
1b162 73 20 30 20 6f 72 20 31 20 2a 2f 0a 20 20 72 65 s 0 or 1 */. re
1b163 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a turn 0;.}../*.**
1b164 20 44 65 73 65 72 69 61 6c 69 7a 65 20 74 68 65 Deserialize the
1b165 20 64 61 74 61 20 62 6c 6f 62 20 70 6f 69 6e 74 data blob point
1b166 65 64 20 74 6f 20 62 79 20 62 75 66 20 61 73 20 ed to by buf as
1b167 73 65 72 69 61 6c 20 74 79 70 65 20 73 65 72 69 serial type seri
1b168 61 6c 5f 74 79 70 65 0a 2a 2a 20 61 6e 64 20 73 al_type.** and s
1b169 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 tore the result
1b16a 69 6e 20 70 4d 65 6d 2e 20 20 52 65 74 75 72 6e in pMem. Return
1b16b 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 the number of b
1b16c 79 74 65 73 20 72 65 61 64 2e 0a 2a 2f 20 0a 53 ytes read..*/ .S
1b16d 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75 33 QLITE_PRIVATE u3
1b16e 32 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 2 sqlite3VdbeSer
1b16f 69 61 6c 47 65 74 28 0a 20 20 63 6f 6e 73 74 20 ialGet(. const
1b170 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 62 unsigned char *b
1b171 75 66 2c 20 20 20 20 20 2f 2a 20 42 75 66 66 65 uf, /* Buffe
1b172 72 20 74 6f 20 64 65 73 65 72 69 61 6c 69 7a 65 r to deserialize
1b173 20 66 72 6f 6d 20 2a 2f 0a 20 20 75 33 32 20 73 from */. u32 s
1b174 65 72 69 61 6c 5f 74 79 70 65 2c 20 20 20 20 20 erial_type,
1b175 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 72 69 /* Seri
1b176 61 6c 20 74 79 70 65 20 74 6f 20 64 65 73 65 72 al type to deser
1b177 69 61 6c 69 7a 65 20 2a 2f 0a 20 20 4d 65 6d 20 ialize */. Mem
1b178 2a 70 4d 65 6d 20 20 20 20 20 20 20 20 20 20 20 *pMem
1b179 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d /* Mem
1b17a 6f 72 79 20 63 65 6c 6c 20 74 6f 20 77 72 69 74 ory cell to writ
1b17b 65 20 76 61 6c 75 65 20 69 6e 74 6f 20 2a 2f 0a e value into */.
1b17c 29 7b 0a 20 20 73 77 69 74 63 68 28 20 73 65 72 ){. switch( ser
1b17d 69 61 6c 5f 74 79 70 65 20 29 7b 0a 20 20 20 20 ial_type ){.
1b17e 63 61 73 65 20 31 30 3a 20 20 20 2f 2a 20 52 65 case 10: /* Re
1b17f 73 65 72 76 65 64 20 66 6f 72 20 66 75 74 75 72 served for futur
1b180 65 20 75 73 65 20 2a 2f 0a 20 20 20 20 63 61 73 e use */. cas
1b181 65 20 31 31 3a 20 20 20 2f 2a 20 52 65 73 65 72 e 11: /* Reser
1b182 76 65 64 20 66 6f 72 20 66 75 74 75 72 65 20 75 ved for future u
1b183 73 65 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 30 se */. case 0
1b184 3a 20 7b 20 20 2f 2a 20 4e 55 4c 4c 20 2a 2f 0a : { /* NULL */.
1b185 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 pMem->flag
1b186 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 s = MEM_Null;.
1b187 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
1b188 0a 20 20 20 20 63 61 73 65 20 31 3a 20 7b 20 2f . case 1: { /
1b189 2a 20 31 2d 62 79 74 65 20 73 69 67 6e 65 64 20 * 1-byte signed
1b18a 69 6e 74 65 67 65 72 20 2a 2f 0a 20 20 20 20 20 integer */.
1b18b 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 28 73 69 pMem->u.i = (si
1b18c 67 6e 65 64 20 63 68 61 72 29 62 75 66 5b 30 5d gned char)buf[0]
1b18d 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c ;. pMem->fl
1b18e 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 ags = MEM_Int;.
1b18f 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 return 1;.
1b190 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 32 3a }. case 2:
1b191 20 7b 20 2f 2a 20 32 2d 62 79 74 65 20 73 69 67 { /* 2-byte sig
1b192 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 20 ned integer */.
1b193 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d pMem->u.i =
1b194 20 28 28 28 73 69 67 6e 65 64 20 63 68 61 72 29 (((signed char)
1b195 62 75 66 5b 30 5d 29 3c 3c 38 29 20 7c 20 62 75 buf[0])<<8) | bu
1b196 66 5b 31 5d 3b 0a 20 20 20 20 20 20 70 4d 65 6d f[1];. pMem
1b197 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e ->flags = MEM_In
1b198 74 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 t;. return
1b199 32 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 2;. }. cas
1b19a 65 20 33 3a 20 7b 20 2f 2a 20 33 2d 62 79 74 65 e 3: { /* 3-byte
1b19b 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 signed integer
1b19c 2a 2f 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 */. pMem->u
1b19d 2e 69 20 3d 20 28 28 28 73 69 67 6e 65 64 20 63 .i = (((signed c
1b19e 68 61 72 29 62 75 66 5b 30 5d 29 3c 3c 31 36 29 har)buf[0])<<16)
1b19f 20 7c 20 28 62 75 66 5b 31 5d 3c 3c 38 29 20 7c | (buf[1]<<8) |
1b1a0 20 62 75 66 5b 32 5d 3b 0a 20 20 20 20 20 20 70 buf[2];. p
1b1a1 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d Mem->flags = MEM
1b1a2 5f 49 6e 74 3b 0a 20 20 20 20 20 20 72 65 74 75 _Int;. retu
1b1a3 72 6e 20 33 3b 0a 20 20 20 20 7d 0a 20 20 20 20 rn 3;. }.
1b1a4 63 61 73 65 20 34 3a 20 7b 20 2f 2a 20 34 2d 62 case 4: { /* 4-b
1b1a5 79 74 65 20 73 69 67 6e 65 64 20 69 6e 74 65 67 yte signed integ
1b1a6 65 72 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65 6d er */. pMem
1b1a7 2d 3e 75 2e 69 20 3d 20 28 62 75 66 5b 30 5d 3c ->u.i = (buf[0]<
1b1a8 3c 32 34 29 20 7c 20 28 62 75 66 5b 31 5d 3c 3c <24) | (buf[1]<<
1b1a9 31 36 29 20 7c 20 28 62 75 66 5b 32 5d 3c 3c 38 16) | (buf[2]<<8
1b1aa 29 20 7c 20 62 75 66 5b 33 5d 3b 0a 20 20 20 20 ) | buf[3];.
1b1ab 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 pMem->flags =
1b1ac 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20 72 MEM_Int;. r
1b1ad 65 74 75 72 6e 20 34 3b 0a 20 20 20 20 7d 0a 20 eturn 4;. }.
1b1ae 20 20 20 63 61 73 65 20 35 3a 20 7b 20 2f 2a 20 case 5: { /*
1b1af 36 2d 62 79 74 65 20 73 69 67 6e 65 64 20 69 6e 6-byte signed in
1b1b0 74 65 67 65 72 20 2a 2f 0a 20 20 20 20 20 20 75 teger */. u
1b1b1 36 34 20 78 20 3d 20 28 28 28 73 69 67 6e 65 64 64 x = (((signed
1b1b2 20 63 68 61 72 29 62 75 66 5b 30 5d 29 3c 3c 38 char)buf[0])<<8
1b1b3 29 20 7c 20 62 75 66 5b 31 5d 3b 0a 20 20 20 20 ) | buf[1];.
1b1b4 20 20 75 33 32 20 79 20 3d 20 28 62 75 66 5b 32 u32 y = (buf[2
1b1b5 5d 3c 3c 32 34 29 20 7c 20 28 62 75 66 5b 33 5d ]<<24) | (buf[3]
1b1b6 3c 3c 31 36 29 20 7c 20 28 62 75 66 5b 34 5d 3c <<16) | (buf[4]<
1b1b7 3c 38 29 20 7c 20 62 75 66 5b 35 5d 3b 0a 20 20 <8) | buf[5];.
1b1b8 20 20 20 20 78 20 3d 20 28 78 3c 3c 33 32 29 20 x = (x<<32)
1b1b9 7c 20 79 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d | y;. pMem-
1b1ba 3e 75 2e 69 20 3d 20 2a 28 69 36 34 2a 29 26 78 >u.i = *(i64*)&x
1b1bb 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c ;. pMem->fl
1b1bc 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 ags = MEM_Int;.
1b1bd 20 20 20 20 20 72 65 74 75 72 6e 20 36 3b 0a 20 return 6;.
1b1be 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 36 3a }. case 6:
1b1bf 20 20 20 2f 2a 20 38 2d 62 79 74 65 20 73 69 67 /* 8-byte sig
1b1c0 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 20 ned integer */.
1b1c1 20 20 20 63 61 73 65 20 37 3a 20 7b 20 2f 2a 20 case 7: { /*
1b1c2 49 45 45 45 20 66 6c 6f 61 74 69 6e 67 20 70 6f IEEE floating po
1b1c3 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20 75 36 34 int */. u64
1b1c4 20 78 3b 0a 20 20 20 20 20 20 75 33 32 20 79 3b x;. u32 y;
1b1c5 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 4e 44 .#if !defined(ND
1b1c6 45 42 55 47 29 20 26 26 20 21 64 65 66 69 6e 65 EBUG) && !define
1b1c7 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c d(SQLITE_OMIT_FL
1b1c8 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 29 0a 20 20 OATING_POINT).
1b1c9 20 20 20 20 2f 2a 20 56 65 72 69 66 79 20 74 68 /* Verify th
1b1ca 61 74 20 69 6e 74 65 67 65 72 73 20 61 6e 64 20 at integers and
1b1cb 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 floating point v
1b1cc 61 6c 75 65 73 20 75 73 65 20 74 68 65 20 73 61 alues use the sa
1b1cd 6d 65 0a 20 20 20 20 20 20 2a 2a 20 62 79 74 65 me. ** byte
1b1ce 20 6f 72 64 65 72 2e 20 20 4f 72 2c 20 74 68 61 order. Or, tha
1b1cf 74 20 69 66 20 53 51 4c 49 54 45 5f 4d 49 58 45 t if SQLITE_MIXE
1b1d0 44 5f 45 4e 44 49 41 4e 5f 36 34 42 49 54 5f 46 D_ENDIAN_64BIT_F
1b1d1 4c 4f 41 54 20 69 73 0a 20 20 20 20 20 20 2a 2a LOAT is. **
1b1d2 20 64 65 66 69 6e 65 64 20 74 68 61 74 20 36 34 defined that 64
1b1d3 2d 62 69 74 20 66 6c 6f 61 74 69 6e 67 20 70 6f -bit floating po
1b1d4 69 6e 74 20 76 61 6c 75 65 73 20 72 65 61 6c 6c int values reall
1b1d5 79 20 61 72 65 20 6d 69 78 65 64 0a 20 20 20 20 y are mixed.
1b1d6 20 20 2a 2a 20 65 6e 64 69 61 6e 2e 0a 20 20 20 ** endian..
1b1d7 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 74 61 74 */. stat
1b1d8 69 63 20 63 6f 6e 73 74 20 75 36 34 20 74 31 20 ic const u64 t1
1b1d9 3d 20 28 28 75 36 34 29 30 78 33 66 66 30 30 30 = ((u64)0x3ff000
1b1da 30 30 29 3c 3c 33 32 3b 0a 20 20 20 20 20 20 73 00)<<32;. s
1b1db 74 61 74 69 63 20 63 6f 6e 73 74 20 64 6f 75 62 tatic const doub
1b1dc 6c 65 20 72 31 20 3d 20 31 2e 30 3b 0a 20 20 20 le r1 = 1.0;.
1b1dd 20 20 20 75 36 34 20 74 32 20 3d 20 74 31 3b 0a u64 t2 = t1;.
1b1de 20 20 20 20 20 20 73 77 61 70 4d 69 78 65 64 45 swapMixedE
1b1df 6e 64 69 61 6e 46 6c 6f 61 74 28 74 32 29 3b 0a ndianFloat(t2);.
1b1e0 20 20 20 20 20 20 61 73 73 65 72 74 28 20 73 69 assert( si
1b1e1 7a 65 6f 66 28 72 31 29 3d 3d 73 69 7a 65 6f 66 zeof(r1)==sizeof
1b1e2 28 74 32 29 20 26 26 20 6d 65 6d 63 6d 70 28 26 (t2) && memcmp(&
1b1e3 72 31 2c 20 26 74 32 2c 20 73 69 7a 65 6f 66 28 r1, &t2, sizeof(
1b1e4 72 31 29 29 3d 3d 30 20 29 3b 0a 23 65 6e 64 69 r1))==0 );.#endi
1b1e5 66 0a 0a 20 20 20 20 20 20 78 20 3d 20 28 62 75 f.. x = (bu
1b1e6 66 5b 30 5d 3c 3c 32 34 29 20 7c 20 28 62 75 66 f[0]<<24) | (buf
1b1e7 5b 31 5d 3c 3c 31 36 29 20 7c 20 28 62 75 66 5b [1]<<16) | (buf[
1b1e8 32 5d 3c 3c 38 29 20 7c 20 62 75 66 5b 33 5d 3b 2]<<8) | buf[3];
1b1e9 0a 20 20 20 20 20 20 79 20 3d 20 28 62 75 66 5b . y = (buf[
1b1ea 34 5d 3c 3c 32 34 29 20 7c 20 28 62 75 66 5b 35 4]<<24) | (buf[5
1b1eb 5d 3c 3c 31 36 29 20 7c 20 28 62 75 66 5b 36 5d ]<<16) | (buf[6]
1b1ec 3c 3c 38 29 20 7c 20 62 75 66 5b 37 5d 3b 0a 20 <<8) | buf[7];.
1b1ed 20 20 20 20 20 78 20 3d 20 28 78 3c 3c 33 32 29 x = (x<<32)
1b1ee 20 7c 20 79 3b 0a 20 20 20 20 20 20 69 66 28 20 | y;. if(
1b1ef 73 65 72 69 61 6c 5f 74 79 70 65 3d 3d 36 20 29 serial_type==6 )
1b1f0 7b 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e {. pMem->
1b1f1 75 2e 69 20 3d 20 2a 28 69 36 34 2a 29 26 78 3b u.i = *(i64*)&x;
1b1f2 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 . pMem->f
1b1f3 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a lags = MEM_Int;.
1b1f4 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
1b1f5 20 20 20 20 20 61 73 73 65 72 74 28 20 73 69 7a assert( siz
1b1f6 65 6f 66 28 78 29 3d 3d 38 20 26 26 20 73 69 7a eof(x)==8 && siz
1b1f7 65 6f 66 28 70 4d 65 6d 2d 3e 72 29 3d 3d 38 20 eof(pMem->r)==8
1b1f8 29 3b 0a 20 20 20 20 20 20 20 20 73 77 61 70 4d );. swapM
1b1f9 69 78 65 64 45 6e 64 69 61 6e 46 6c 6f 61 74 28 ixedEndianFloat(
1b1fa 78 29 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 x);. memc
1b1fb 70 79 28 26 70 4d 65 6d 2d 3e 72 2c 20 26 78 2c py(&pMem->r, &x,
1b1fc 20 73 69 7a 65 6f 66 28 78 29 29 3b 0a 20 20 20 sizeof(x));.
1b1fd 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 pMem->flags
1b1fe 20 3d 20 73 71 6c 69 74 65 33 49 73 4e 61 4e 28 = sqlite3IsNaN(
1b1ff 70 4d 65 6d 2d 3e 72 29 20 3f 20 4d 45 4d 5f 4e pMem->r) ? MEM_N
1b200 75 6c 6c 20 3a 20 4d 45 4d 5f 52 65 61 6c 3b 0a ull : MEM_Real;.
1b201 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 }. re
1b202 74 75 72 6e 20 38 3b 0a 20 20 20 20 7d 0a 20 20 turn 8;. }.
1b203 20 20 63 61 73 65 20 38 3a 20 20 20 20 2f 2a 20 case 8: /*
1b204 49 6e 74 65 67 65 72 20 30 20 2a 2f 0a 20 20 20 Integer 0 */.
1b205 20 63 61 73 65 20 39 3a 20 7b 20 20 2f 2a 20 49 case 9: { /* I
1b206 6e 74 65 67 65 72 20 31 20 2a 2f 0a 20 20 20 20 nteger 1 */.
1b207 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 73 65 pMem->u.i = se
1b208 72 69 61 6c 5f 74 79 70 65 2d 38 3b 0a 20 20 20 rial_type-8;.
1b209 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d pMem->flags =
1b20a 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20 MEM_Int;.
1b20b 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a return 0;. }.
1b20c 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 default: {.
1b20d 20 20 20 20 20 75 33 32 20 6c 65 6e 20 3d 20 28 u32 len = (
1b20e 73 65 72 69 61 6c 5f 74 79 70 65 2d 31 32 29 2f serial_type-12)/
1b20f 32 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 2;. pMem->z
1b210 20 3d 20 28 63 68 61 72 20 2a 29 62 75 66 3b 0a = (char *)buf;.
1b211 20 20 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 pMem->n =
1b212 6c 65 6e 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d len;. pMem-
1b213 3e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 20 20 20 >xDel = 0;.
1b214 20 69 66 28 20 73 65 72 69 61 6c 5f 74 79 70 65 if( serial_type
1b215 26 30 78 30 31 20 29 7b 0a 20 20 20 20 20 20 20 &0x01 ){.
1b216 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d pMem->flags = M
1b217 45 4d 5f 53 74 72 20 7c 20 4d 45 4d 5f 45 70 68 EM_Str | MEM_Eph
1b218 65 6d 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b em;. }else{
1b219 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 . pMem->f
1b21a 6c 61 67 73 20 3d 20 4d 45 4d 5f 42 6c 6f 62 20 lags = MEM_Blob
1b21b 7c 20 4d 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 20 | MEM_Ephem;.
1b21c 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72 }. retur
1b21d 6e 20 6c 65 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d n len;. }. }
1b21e 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a . return 0;.}..
1b21f 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 74 68 65 ./*.** Given the
1b220 20 6e 4b 65 79 2d 62 79 74 65 20 65 6e 63 6f 64 nKey-byte encod
1b221 69 6e 67 20 6f 66 20 61 20 72 65 63 6f 72 64 20 ing of a record
1b222 69 6e 20 70 4b 65 79 5b 5d 2c 20 70 61 72 73 65 in pKey[], parse
1b223 20 74 68 65 0a 2a 2a 20 72 65 63 6f 72 64 20 69 the.** record i
1b224 6e 74 6f 20 61 20 55 6e 70 61 63 6b 65 64 52 65 nto a UnpackedRe
1b225 63 6f 72 64 20 73 74 72 75 63 74 75 72 65 2e 20 cord structure.
1b226 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 Return a pointe
1b227 72 20 74 6f 0a 2a 2a 20 74 68 61 74 20 73 74 72 r to.** that str
1b228 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 ucture..**.** Th
1b229 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 e calling functi
1b22a 6f 6e 20 6d 69 67 68 74 20 70 72 6f 76 69 64 65 on might provide
1b22b 20 73 7a 53 70 61 63 65 20 62 79 74 65 73 20 6f szSpace bytes o
1b22c 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 73 70 61 63 f memory.** spac
1b22d 65 20 61 74 20 70 53 70 61 63 65 2e 20 20 54 68 e at pSpace. Th
1b22e 69 73 20 73 70 61 63 65 20 63 61 6e 20 62 65 20 is space can be
1b22f 75 73 65 64 20 74 6f 20 68 6f 6c 64 20 74 68 65 used to hold the
1b230 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 56 44 62 returned.** VDb
1b231 65 50 61 72 73 65 64 52 65 63 6f 72 64 20 73 74 eParsedRecord st
1b232 72 75 63 74 75 72 65 20 69 66 20 69 74 20 69 73 ructure if it is
1b233 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 2e 20 20 large enough.
1b234 49 66 20 69 74 20 69 73 0a 2a 2a 20 6e 6f 74 20 If it is.** not
1b235 62 69 67 20 65 6e 6f 75 67 68 2c 20 73 70 61 63 big enough, spac
1b236 65 20 69 73 20 6f 62 74 61 69 6e 65 64 20 66 72 e is obtained fr
1b237 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f om sqlite3_mallo
1b238 63 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 c()..**.** The r
1b239 65 74 75 72 6e 65 64 20 73 74 72 75 63 74 75 72 eturned structur
1b23a 65 20 73 68 6f 75 6c 64 20 62 65 20 63 6c 6f 73 e should be clos
1b23b 65 64 20 62 79 20 61 20 63 61 6c 6c 20 74 6f 0a ed by a call to.
1b23c 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 ** sqlite3VdbeDe
1b23d 6c 65 74 65 55 6e 70 61 63 6b 65 64 52 65 63 6f leteUnpackedReco
1b23e 72 64 28 29 2e 0a 2a 2f 20 0a 53 51 4c 49 54 45 rd()..*/ .SQLITE
1b23f 5f 50 52 49 56 41 54 45 20 55 6e 70 61 63 6b 65 _PRIVATE Unpacke
1b240 64 52 65 63 6f 72 64 20 2a 73 71 6c 69 74 65 33 dRecord *sqlite3
1b241 56 64 62 65 52 65 63 6f 72 64 55 6e 70 61 63 6b VdbeRecordUnpack
1b242 28 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 (. KeyInfo *pKe
1b243 79 49 6e 66 6f 2c 20 20 20 20 20 2f 2a 20 49 6e yInfo, /* In
1b244 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 formation about
1b245 74 68 65 20 72 65 63 6f 72 64 20 66 6f 72 6d 61 the record forma
1b246 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 4b 65 79 2c t */. int nKey,
1b247 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1b248 20 53 69 7a 65 20 6f 66 20 74 68 65 20 62 69 6e Size of the bin
1b249 61 72 79 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 ary record */.
1b24a 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 const void *pKey
1b24b 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 62 69 , /* The bi
1b24c 6e 61 72 79 20 72 65 63 6f 72 64 20 2a 2f 0a 20 nary record */.
1b24d 20 63 68 61 72 20 2a 70 53 70 61 63 65 2c 20 20 char *pSpace,
1b24e 20 20 20 20 20 20 20 20 2f 2a 20 55 6e 61 6c 69 /* Unali
1b24f 67 6e 65 64 20 73 70 61 63 65 20 61 76 61 69 6c gned space avail
1b250 61 62 6c 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 able to hold the
1b251 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 69 6e 74 object */. int
1b252 20 73 7a 53 70 61 63 65 20 20 20 20 20 20 20 20 szSpace
1b253 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 70 /* Size of p
1b254 53 70 61 63 65 5b 5d 20 69 6e 20 62 79 74 65 73 Space[] in bytes
1b255 20 2a 2f 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 75 */.){. const u
1b256 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 61 4b nsigned char *aK
1b257 65 79 20 3d 20 28 63 6f 6e 73 74 20 75 6e 73 69 ey = (const unsi
1b258 67 6e 65 64 20 63 68 61 72 20 2a 29 70 4b 65 79 gned char *)pKey
1b259 3b 0a 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f ;. UnpackedReco
1b25a 72 64 20 2a 70 3b 20 20 2f 2a 20 54 68 65 20 75 rd *p; /* The u
1b25b 6e 70 61 63 6b 65 64 20 72 65 63 6f 72 64 20 74 npacked record t
1b25c 68 61 74 20 77 65 20 77 69 6c 6c 20 72 65 74 75 hat we will retu
1b25d 72 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 rn */. int nByt
1b25e 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d e; /* M
1b25f 65 6d 6f 72 79 20 73 70 61 63 65 20 6e 65 65 64 emory space need
1b260 65 64 20 74 6f 20 68 6f 6c 64 20 70 2c 20 69 6e ed to hold p, in
1b261 20 62 79 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20 bytes */. int
1b262 64 3b 0a 20 20 75 33 32 20 69 64 78 3b 0a 20 20 d;. u32 idx;.
1b263 75 31 36 20 75 3b 20 20 20 20 20 20 20 20 20 20 u16 u;
1b264 20 20 20 20 2f 2a 20 55 6e 73 69 67 6e 65 64 20 /* Unsigned
1b265 6c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a loop counter */.
1b266 20 20 75 33 32 20 73 7a 48 64 72 3b 0a 20 20 4d u32 szHdr;. M
1b267 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 69 6e 74 20 em *pMem;. int
1b268 6e 4f 66 66 3b 20 20 20 20 20 20 20 20 20 20 20 nOff;
1b269 2f 2a 20 49 6e 63 72 65 61 73 65 20 70 53 70 61 /* Increase pSpa
1b26a 63 65 20 62 79 20 74 68 69 73 20 6d 75 63 68 20 ce by this much
1b26b 74 6f 20 38 2d 62 79 74 65 20 61 6c 69 67 6e 20 to 8-byte align
1b26c 69 74 20 2a 2f 0a 20 20 0a 20 20 2f 2a 0a 20 20 it */. . /*.
1b26d 2a 2a 20 57 65 20 77 61 6e 74 20 74 6f 20 73 68 ** We want to sh
1b26e 69 66 74 20 74 68 65 20 70 6f 69 6e 74 65 72 20 ift the pointer
1b26f 70 53 70 61 63 65 20 75 70 20 73 75 63 68 20 74 pSpace up such t
1b270 68 61 74 20 69 74 20 69 73 20 38 2d 62 79 74 65 hat it is 8-byte
1b271 20 61 6c 69 67 6e 65 64 2e 0a 20 20 2a 2a 20 54 aligned.. ** T
1b272 68 75 73 2c 20 77 65 20 6e 65 65 64 20 74 6f 20 hus, we need to
1b273 63 61 6c 63 75 6c 61 74 65 20 61 20 76 61 6c 75 calculate a valu
1b274 65 2c 20 6e 4f 66 66 2c 20 62 65 74 77 65 65 6e e, nOff, between
1b275 20 30 20 61 6e 64 20 37 2c 20 74 6f 20 73 68 69 0 and 7, to shi
1b276 66 74 20 0a 20 20 2a 2a 20 69 74 20 62 79 2e 20 ft . ** it by.
1b277 20 49 66 20 70 53 70 61 63 65 20 69 73 20 61 6c If pSpace is al
1b278 72 65 61 64 79 20 38 2d 62 79 74 65 20 61 6c 69 ready 8-byte ali
1b279 67 6e 65 64 2c 20 6e 4f 66 66 20 73 68 6f 75 6c gned, nOff shoul
1b27a 64 20 62 65 20 7a 65 72 6f 2e 0a 20 20 2a 2f 0a d be zero.. */.
1b27b 20 20 6e 4f 66 66 20 3d 20 28 38 20 2d 20 28 53 nOff = (8 - (S
1b27c 51 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 QLITE_PTR_TO_INT
1b27d 28 70 53 70 61 63 65 29 20 26 20 37 29 29 20 26 (pSpace) & 7)) &
1b27e 20 37 3b 0a 20 20 70 53 70 61 63 65 20 2b 3d 20 7;. pSpace +=
1b27f 6e 4f 66 66 3b 0a 20 20 73 7a 53 70 61 63 65 20 nOff;. szSpace
1b280 2d 3d 20 6e 4f 66 66 3b 0a 20 20 6e 42 79 74 65 -= nOff;. nByte
1b281 20 3d 20 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 = ROUND8(sizeof
1b282 28 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 29 (UnpackedRecord)
1b283 29 20 2b 20 73 69 7a 65 6f 66 28 4d 65 6d 29 2a ) + sizeof(Mem)*
1b284 28 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c (pKeyInfo->nFiel
1b285 64 2b 31 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 d+1);. if( nByt
1b286 65 3e 73 7a 53 70 61 63 65 20 29 7b 0a 20 20 20 e>szSpace ){.
1b287 20 70 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 p = sqlite3DbMa
1b288 6c 6c 6f 63 52 61 77 28 70 4b 65 79 49 6e 66 6f llocRaw(pKeyInfo
1b289 2d 3e 64 62 2c 20 6e 42 79 74 65 29 3b 0a 20 20 ->db, nByte);.
1b28a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 if( p==0 ) ret
1b28b 75 72 6e 20 30 3b 0a 20 20 20 20 70 2d 3e 66 6c urn 0;. p->fl
1b28c 61 67 73 20 3d 20 55 4e 50 41 43 4b 45 44 5f 4e ags = UNPACKED_N
1b28d 45 45 44 5f 46 52 45 45 20 7c 20 55 4e 50 41 43 EED_FREE | UNPAC
1b28e 4b 45 44 5f 4e 45 45 44 5f 44 45 53 54 52 4f 59 KED_NEED_DESTROY
1b28f 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 ;. }else{. p
1b290 20 3d 20 28 55 6e 70 61 63 6b 65 64 52 65 63 6f = (UnpackedReco
1b291 72 64 2a 29 70 53 70 61 63 65 3b 0a 20 20 20 20 rd*)pSpace;.
1b292 70 2d 3e 66 6c 61 67 73 20 3d 20 55 4e 50 41 43 p->flags = UNPAC
1b293 4b 45 44 5f 4e 45 45 44 5f 44 45 53 54 52 4f 59 KED_NEED_DESTROY
1b294 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 4b 65 79 49 ;. }. p->pKeyI
1b295 6e 66 6f 20 3d 20 70 4b 65 79 49 6e 66 6f 3b 0a nfo = pKeyInfo;.
1b296 20 20 70 2d 3e 6e 46 69 65 6c 64 20 3d 20 70 4b p->nField = pK
1b297 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 20 2b eyInfo->nField +
1b298 20 31 3b 0a 20 20 70 2d 3e 61 4d 65 6d 20 3d 20 1;. p->aMem =
1b299 70 4d 65 6d 20 3d 20 28 4d 65 6d 2a 29 26 28 28 pMem = (Mem*)&((
1b29a 63 68 61 72 2a 29 70 29 5b 52 4f 55 4e 44 38 28 char*)p)[ROUND8(
1b29b 73 69 7a 65 6f 66 28 55 6e 70 61 63 6b 65 64 52 sizeof(UnpackedR
1b29c 65 63 6f 72 64 29 29 5d 3b 0a 20 20 61 73 73 65 ecord))];. asse
1b29d 72 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41 rt( EIGHT_BYTE_A
1b29e 4c 49 47 4e 4d 45 4e 54 28 70 4d 65 6d 29 20 29 LIGNMENT(pMem) )
1b29f 3b 0a 20 20 69 64 78 20 3d 20 67 65 74 56 61 72 ;. idx = getVar
1b2a0 69 6e 74 33 32 28 61 4b 65 79 2c 20 73 7a 48 64 int32(aKey, szHd
1b2a1 72 29 3b 0a 20 20 64 20 3d 20 73 7a 48 64 72 3b r);. d = szHdr;
1b2a2 0a 20 20 75 20 3d 20 30 3b 0a 20 20 77 68 69 6c . u = 0;. whil
1b2a3 65 28 20 69 64 78 3c 73 7a 48 64 72 20 26 26 20 e( idx<szHdr &&
1b2a4 75 3c 70 2d 3e 6e 46 69 65 6c 64 20 26 26 20 64 u<p->nField && d
1b2a5 3c 3d 6e 4b 65 79 20 29 7b 0a 20 20 20 20 75 33 <=nKey ){. u3
1b2a6 32 20 73 65 72 69 61 6c 5f 74 79 70 65 3b 0a 0a 2 serial_type;..
1b2a7 20 20 20 20 69 64 78 20 2b 3d 20 67 65 74 56 61 idx += getVa
1b2a8 72 69 6e 74 33 32 28 26 61 4b 65 79 5b 69 64 78 rint32(&aKey[idx
1b2a9 5d 2c 20 73 65 72 69 61 6c 5f 74 79 70 65 29 3b ], serial_type);
1b2aa 0a 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d . pMem->enc =
1b2ab 20 70 4b 65 79 49 6e 66 6f 2d 3e 65 6e 63 3b 0a pKeyInfo->enc;.
1b2ac 20 20 20 20 70 4d 65 6d 2d 3e 64 62 20 3d 20 70 pMem->db = p
1b2ad 4b 65 79 49 6e 66 6f 2d 3e 64 62 3b 0a 20 20 20 KeyInfo->db;.
1b2ae 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 30 pMem->flags = 0
1b2af 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a 4d 61 6c ;. pMem->zMal
1b2b0 6c 6f 63 20 3d 20 30 3b 0a 20 20 20 20 64 20 2b loc = 0;. d +
1b2b1 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 = sqlite3VdbeSer
1b2b2 69 61 6c 47 65 74 28 26 61 4b 65 79 5b 64 5d 2c ialGet(&aKey[d],
1b2b3 20 73 65 72 69 61 6c 5f 74 79 70 65 2c 20 70 4d serial_type, pM
1b2b4 65 6d 29 3b 0a 20 20 20 20 70 4d 65 6d 2b 2b 3b em);. pMem++;
1b2b5 0a 20 20 20 20 75 2b 2b 3b 0a 20 20 7d 0a 20 20 . u++;. }.
1b2b6 61 73 73 65 72 74 28 20 75 3c 3d 70 4b 65 79 49 assert( u<=pKeyI
1b2b7 6e 66 6f 2d 3e 6e 46 69 65 6c 64 20 2b 20 31 20 nfo->nField + 1
1b2b8 29 3b 0a 20 20 70 2d 3e 6e 46 69 65 6c 64 20 3d );. p->nField =
1b2b9 20 75 3b 0a 20 20 72 65 74 75 72 6e 20 28 76 6f u;. return (vo
1b2ba 69 64 2a 29 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 id*)p;.}../*.**
1b2bb 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 65 73 This routine des
1b2bc 74 72 6f 79 73 20 61 20 55 6e 70 61 63 6b 65 64 troys a Unpacked
1b2bd 52 65 63 6f 72 64 20 6f 62 6a 65 63 74 2e 0a 2a Record object..*
1b2be 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1b2bf 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
1b2c0 65 44 65 6c 65 74 65 55 6e 70 61 63 6b 65 64 52 eDeleteUnpackedR
1b2c1 65 63 6f 72 64 28 55 6e 70 61 63 6b 65 64 52 65 ecord(UnpackedRe
1b2c2 63 6f 72 64 20 2a 70 29 7b 0a 20 20 69 6e 74 20 cord *p){. int
1b2c3 69 3b 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a i;. Mem *pMem;.
1b2c4 0a 20 20 61 73 73 65 72 74 28 20 70 21 3d 30 20 . assert( p!=0
1b2c5 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e );. assert( p->
1b2c6 66 6c 61 67 73 20 26 20 55 4e 50 41 43 4b 45 44 flags & UNPACKED
1b2c7 5f 4e 45 45 44 5f 44 45 53 54 52 4f 59 20 29 3b _NEED_DESTROY );
1b2c8 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 4d 65 6d . for(i=0, pMem
1b2c9 3d 70 2d 3e 61 4d 65 6d 3b 20 69 3c 70 2d 3e 6e =p->aMem; i<p->n
1b2ca 46 69 65 6c 64 3b 20 69 2b 2b 2c 20 70 4d 65 6d Field; i++, pMem
1b2cb 2b 2b 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 ++){. /* The
1b2cc 75 6e 70 61 63 6b 65 64 20 72 65 63 6f 72 64 20 unpacked record
1b2cd 69 73 20 61 6c 77 61 79 73 20 63 6f 6e 73 74 72 is always constr
1b2ce 75 63 74 65 64 20 62 79 20 74 68 65 0a 20 20 20 ucted by the.
1b2cf 20 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 55 ** sqlite3VdbeU
1b2d0 6e 70 61 63 6b 52 65 63 6f 72 64 28 29 20 66 75 npackRecord() fu
1b2d1 6e 63 74 69 6f 6e 20 61 62 6f 76 65 2c 20 77 68 nction above, wh
1b2d2 69 63 68 20 6d 61 6b 65 73 20 61 6c 6c 0a 20 20 ich makes all.
1b2d3 20 20 2a 2a 20 73 74 72 69 6e 67 73 20 61 6e 64 ** strings and
1b2d4 20 62 6c 6f 62 73 20 73 74 61 74 69 63 2e 20 20 blobs static.
1b2d5 41 6e 64 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 And none of the
1b2d6 65 6c 65 6d 65 6e 74 73 20 61 72 65 0a 20 20 20 elements are.
1b2d7 20 2a 2a 20 65 76 65 72 20 74 72 61 6e 73 66 6f ** ever transfo
1b2d8 72 6d 65 64 2c 20 73 6f 20 74 68 65 72 65 20 69 rmed, so there i
1b2d9 73 20 6e 65 76 65 72 20 61 6e 79 74 68 69 6e 67 s never anything
1b2da 20 74 6f 20 64 65 6c 65 74 65 2e 0a 20 20 20 20 to delete..
1b2db 2a 2f 0a 20 20 20 20 69 66 28 20 4e 45 56 45 52 */. if( NEVER
1b2dc 28 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 29 20 (pMem->zMalloc)
1b2dd 29 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d ) sqlite3VdbeMem
1b2de 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 Release(pMem);.
1b2df 20 7d 0a 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 }. if( p->flag
1b2e0 73 20 26 20 55 4e 50 41 43 4b 45 44 5f 4e 45 45 s & UNPACKED_NEE
1b2e1 44 5f 46 52 45 45 20 29 7b 0a 20 20 20 20 73 71 D_FREE ){. sq
1b2e2 6c 69 74 65 33 44 62 46 72 65 65 28 70 2d 3e 70 lite3DbFree(p->p
1b2e3 4b 65 79 49 6e 66 6f 2d 3e 64 62 2c 20 70 29 3b KeyInfo->db, p);
1b2e4 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 . }.}../*.** Th
1b2e5 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6d 70 is function comp
1b2e6 61 72 65 73 20 74 68 65 20 74 77 6f 20 74 61 62 ares the two tab
1b2e7 6c 65 20 72 6f 77 73 20 6f 72 20 69 6e 64 65 78 le rows or index
1b2e8 20 72 65 63 6f 72 64 73 0a 2a 2a 20 73 70 65 63 records.** spec
1b2e9 69 66 69 65 64 20 62 79 20 7b 6e 4b 65 79 31 2c ified by {nKey1,
1b2ea 20 70 4b 65 79 31 7d 20 61 6e 64 20 70 50 4b 65 pKey1} and pPKe
1b2eb 79 32 2e 20 20 49 74 20 72 65 74 75 72 6e 73 20 y2. It returns
1b2ec 61 20 6e 65 67 61 74 69 76 65 2c 20 7a 65 72 6f a negative, zero
1b2ed 0a 2a 2a 20 6f 72 20 70 6f 73 69 74 69 76 65 20 .** or positive
1b2ee 69 6e 74 65 67 65 72 20 69 66 20 6b 65 79 31 20 integer if key1
1b2ef 69 73 20 6c 65 73 73 20 74 68 61 6e 2c 20 65 71 is less than, eq
1b2f0 75 61 6c 20 74 6f 20 6f 72 20 0a 2a 2a 20 67 72 ual to or .** gr
1b2f1 65 61 74 65 72 20 74 68 61 6e 20 6b 65 79 32 2e eater than key2.
1b2f2 20 20 54 68 65 20 7b 6e 4b 65 79 31 2c 20 70 4b The {nKey1, pK
1b2f3 65 79 31 7d 20 6b 65 79 20 6d 75 73 74 20 62 65 ey1} key must be
1b2f4 20 61 20 62 6c 6f 62 0a 2a 2a 20 63 72 65 61 74 a blob.** creat
1b2f5 65 64 20 62 79 20 74 68 20 4f 50 5f 4d 61 6b 65 ed by th OP_Make
1b2f6 52 65 63 6f 72 64 20 6f 70 63 6f 64 65 20 6f 66 Record opcode of
1b2f7 20 74 68 65 20 56 44 42 45 2e 20 20 54 68 65 20 the VDBE. The
1b2f8 70 50 4b 65 79 32 0a 2a 2a 20 6b 65 79 20 6d 75 pPKey2.** key mu
1b2f9 73 74 20 62 65 20 61 20 70 61 72 73 65 64 20 6b st be a parsed k
1b2fa 65 79 20 73 75 63 68 20 61 73 20 6f 62 74 61 69 ey such as obtai
1b2fb 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 73 71 6c 69 ned from.** sqli
1b2fc 74 65 33 56 64 62 65 50 61 72 73 65 52 65 63 6f te3VdbeParseReco
1b2fd 72 64 2e 0a 2a 2a 0a 2a 2a 20 4b 65 79 31 20 61 rd..**.** Key1 a
1b2fe 6e 64 20 4b 65 79 32 20 64 6f 20 6e 6f 74 20 68 nd Key2 do not h
1b2ff 61 76 65 20 74 6f 20 63 6f 6e 74 61 69 6e 20 74 ave to contain t
1b300 68 65 20 73 61 6d 65 20 6e 75 6d 62 65 72 20 6f he same number o
1b301 66 20 66 69 65 6c 64 73 2e 0a 2a 2a 20 54 68 65 f fields..** The
1b302 20 6b 65 79 20 77 69 74 68 20 66 65 77 65 72 20 key with fewer
1b303 66 69 65 6c 64 73 20 69 73 20 75 73 75 61 6c 6c fields is usuall
1b304 79 20 63 6f 6d 70 61 72 65 73 20 6c 65 73 73 20 y compares less
1b305 74 68 61 6e 20 74 68 65 20 0a 2a 2a 20 6c 6f 6e than the .** lon
1b306 67 65 72 20 6b 65 79 2e 20 20 48 6f 77 65 76 65 ger key. Howeve
1b307 72 20 69 66 20 74 68 65 20 55 4e 50 41 43 4b 45 r if the UNPACKE
1b308 44 5f 49 4e 43 52 4b 45 59 20 66 6c 61 67 73 20 D_INCRKEY flags
1b309 69 6e 20 70 50 4b 65 79 32 20 69 73 20 73 65 74 in pPKey2 is set
1b30a 0a 2a 2a 20 61 6e 64 20 74 68 65 20 63 6f 6d 6d .** and the comm
1b30b 6f 6e 20 70 72 65 66 69 78 65 73 20 61 72 65 20 on prefixes are
1b30c 65 71 75 61 6c 2c 20 74 68 65 6e 20 6b 65 79 31 equal, then key1
1b30d 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 6b 65 is less than ke
1b30e 79 32 2e 0a 2a 2a 20 4f 72 20 69 66 20 74 68 65 y2..** Or if the
1b30f 20 55 4e 50 41 43 4b 45 44 5f 4d 41 54 43 48 5f UNPACKED_MATCH_
1b310 50 52 45 46 49 58 20 66 6c 61 67 20 69 73 20 73 PREFIX flag is s
1b311 65 74 20 61 6e 64 20 74 68 65 20 70 72 65 66 69 et and the prefi
1b312 78 65 73 20 61 72 65 0a 2a 2a 20 65 71 75 61 6c xes are.** equal
1b313 2c 20 74 68 65 6e 20 74 68 65 20 6b 65 79 73 20 , then the keys
1b314 61 72 65 20 63 6f 6e 73 69 64 65 72 65 64 20 74 are considered t
1b315 6f 20 62 65 20 65 71 75 61 6c 20 61 6e 64 0a 2a o be equal and.*
1b316 2a 20 74 68 65 20 70 61 72 74 73 20 62 65 79 6f * the parts beyo
1b317 6e 64 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 70 72 nd the common pr
1b318 65 66 69 78 20 61 72 65 20 69 67 6e 6f 72 65 64 efix are ignored
1b319 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 55 ..**.** If the U
1b31a 4e 50 41 43 4b 45 44 5f 49 47 4e 4f 52 45 5f 52 NPACKED_IGNORE_R
1b31b 4f 57 49 44 20 66 6c 61 67 20 69 73 20 73 65 74 OWID flag is set
1b31c 2c 20 74 68 65 6e 20 74 68 65 20 6c 61 73 74 20 , then the last
1b31d 62 79 74 65 20 6f 66 0a 2a 2a 20 74 68 65 20 68 byte of.** the h
1b31e 65 61 64 65 72 20 6f 66 20 70 4b 65 79 31 20 69 eader of pKey1 i
1b31f 73 20 69 67 6e 6f 72 65 64 2e 20 20 49 74 20 69 s ignored. It i
1b320 73 20 61 73 73 75 6d 65 64 20 74 68 61 74 20 70 s assumed that p
1b321 4b 65 79 31 20 69 73 0a 2a 2a 20 61 6e 20 69 6e Key1 is.** an in
1b322 64 65 78 20 6b 65 79 2c 20 61 6e 64 20 74 68 75 dex key, and thu
1b323 73 20 65 6e 64 73 20 77 69 74 68 20 61 20 72 6f s ends with a ro
1b324 77 69 64 20 76 61 6c 75 65 2e 20 20 54 68 65 20 wid value. The
1b325 6c 61 73 74 20 62 79 74 65 0a 2a 2a 20 6f 66 20 last byte.** of
1b326 74 68 65 20 68 65 61 64 65 72 20 77 69 6c 6c 20 the header will
1b327 74 68 65 72 65 66 6f 72 65 20 62 65 20 74 68 65 therefore be the
1b328 20 73 65 72 69 61 6c 20 74 79 70 65 20 6f 66 20 serial type of
1b329 74 68 65 20 72 6f 77 69 64 3a 0a 2a 2a 20 6f 6e the rowid:.** on
1b32a 65 20 6f 66 20 31 2c 20 32 2c 20 33 2c 20 34 2c e of 1, 2, 3, 4,
1b32b 20 35 2c 20 36 2c 20 38 2c 20 6f 72 20 39 20 2d 5, 6, 8, or 9 -
1b32c 20 74 68 65 20 69 6e 74 65 67 65 72 20 73 65 72 the integer ser
1b32d 69 61 6c 20 74 79 70 65 73 2e 0a 2a 2a 20 54 68 ial types..** Th
1b32e 65 20 73 65 72 69 61 6c 20 74 79 70 65 20 6f 66 e serial type of
1b32f 20 74 68 65 20 66 69 6e 61 6c 20 72 6f 77 69 64 the final rowid
1b330 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 62 65 20 will always be
1b331 61 20 73 69 6e 67 6c 65 20 62 79 74 65 2e 0a 2a a single byte..*
1b332 2a 20 42 79 20 69 67 6e 6f 72 69 6e 67 20 74 68 * By ignoring th
1b333 69 73 20 6c 61 73 74 20 62 79 74 65 20 6f 66 20 is last byte of
1b334 74 68 65 20 68 65 61 64 65 72 2c 20 77 65 20 66 the header, we f
1b335 6f 72 63 65 20 74 68 65 20 63 6f 6d 70 61 72 69 orce the compari
1b336 73 6f 6e 0a 2a 2a 20 74 6f 20 69 67 6e 6f 72 65 son.** to ignore
1b337 20 74 68 65 20 72 6f 77 69 64 20 61 74 20 74 68 the rowid at th
1b338 65 20 65 6e 64 20 6f 66 20 6b 65 79 31 2e 0a 2a e end of key1..*
1b339 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1b33a 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 int sqlite3Vdbe
1b33b 52 65 63 6f 72 64 43 6f 6d 70 61 72 65 28 0a 20 RecordCompare(.
1b33c 20 69 6e 74 20 6e 4b 65 79 31 2c 20 63 6f 6e 73 int nKey1, cons
1b33d 74 20 76 6f 69 64 20 2a 70 4b 65 79 31 2c 20 2f t void *pKey1, /
1b33e 2a 20 4c 65 66 74 20 6b 65 79 20 2a 2f 0a 20 20 * Left key */.
1b33f 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a UnpackedRecord *
1b340 70 50 4b 65 79 32 20 20 20 20 20 20 20 20 2f 2a pPKey2 /*
1b341 20 52 69 67 68 74 20 6b 65 79 20 2a 2f 0a 29 7b Right key */.){
1b342 0a 20 20 69 6e 74 20 64 31 3b 20 20 20 20 20 20 . int d1;
1b343 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 /* Offset
1b344 69 6e 74 6f 20 61 4b 65 79 5b 5d 20 6f 66 20 6e into aKey[] of n
1b345 65 78 74 20 64 61 74 61 20 65 6c 65 6d 65 6e 74 ext data element
1b346 20 2a 2f 0a 20 20 75 33 32 20 69 64 78 31 3b 20 */. u32 idx1;
1b347 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 /* Offs
1b348 65 74 20 69 6e 74 6f 20 61 4b 65 79 5b 5d 20 6f et into aKey[] o
1b349 66 20 6e 65 78 74 20 68 65 61 64 65 72 20 65 6c f next header el
1b34a 65 6d 65 6e 74 20 2a 2f 0a 20 20 75 33 32 20 73 ement */. u32 s
1b34b 7a 48 64 72 31 3b 20 20 20 20 20 20 20 20 2f 2a zHdr1; /*
1b34c 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 Number of bytes
1b34d 20 69 6e 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 in header */.
1b34e 69 6e 74 20 69 20 3d 20 30 3b 0a 20 20 69 6e 74 int i = 0;. int
1b34f 20 6e 46 69 65 6c 64 3b 0a 20 20 69 6e 74 20 72 nField;. int r
1b350 63 20 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20 75 c = 0;. const u
1b351 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 61 4b nsigned char *aK
1b352 65 79 31 20 3d 20 28 63 6f 6e 73 74 20 75 6e 73 ey1 = (const uns
1b353 69 67 6e 65 64 20 63 68 61 72 20 2a 29 70 4b 65 igned char *)pKe
1b354 79 31 3b 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70 y1;. KeyInfo *p
1b355 4b 65 79 49 6e 66 6f 3b 0a 20 20 4d 65 6d 20 6d KeyInfo;. Mem m
1b356 65 6d 31 3b 0a 0a 20 20 70 4b 65 79 49 6e 66 6f em1;.. pKeyInfo
1b357 20 3d 20 70 50 4b 65 79 32 2d 3e 70 4b 65 79 49 = pPKey2->pKeyI
1b358 6e 66 6f 3b 0a 20 20 6d 65 6d 31 2e 65 6e 63 20 nfo;. mem1.enc
1b359 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 65 6e 63 3b = pKeyInfo->enc;
1b35a 0a 20 20 6d 65 6d 31 2e 64 62 20 3d 20 70 4b 65 . mem1.db = pKe
1b35b 79 49 6e 66 6f 2d 3e 64 62 3b 0a 20 20 6d 65 6d yInfo->db;. mem
1b35c 31 2e 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20 6d 1.flags = 0;. m
1b35d 65 6d 31 2e 75 2e 69 20 3d 20 30 3b 20 20 2f 2a em1.u.i = 0; /*
1b35e 20 6e 6f 74 20 6e 65 65 64 65 64 2c 20 68 65 72 not needed, her
1b35f 65 20 74 6f 20 73 69 6c 65 6e 63 65 20 63 6f 6d e to silence com
1b360 70 69 6c 65 72 20 77 61 72 6e 69 6e 67 20 2a 2f piler warning */
1b361 0a 20 20 6d 65 6d 31 2e 7a 4d 61 6c 6c 6f 63 20 . mem1.zMalloc
1b362 3d 20 30 3b 0a 20 20 0a 20 20 69 64 78 31 20 3d = 0;. . idx1 =
1b363 20 67 65 74 56 61 72 69 6e 74 33 32 28 61 4b 65 getVarint32(aKe
1b364 79 31 2c 20 73 7a 48 64 72 31 29 3b 0a 20 20 64 y1, szHdr1);. d
1b365 31 20 3d 20 73 7a 48 64 72 31 3b 0a 20 20 69 66 1 = szHdr1;. if
1b366 28 20 70 50 4b 65 79 32 2d 3e 66 6c 61 67 73 20 ( pPKey2->flags
1b367 26 20 55 4e 50 41 43 4b 45 44 5f 49 47 4e 4f 52 & UNPACKED_IGNOR
1b368 45 5f 52 4f 57 49 44 20 29 7b 0a 20 20 20 20 73 E_ROWID ){. s
1b369 7a 48 64 72 31 2d 2d 3b 0a 20 20 7d 0a 20 20 6e zHdr1--;. }. n
1b36a 46 69 65 6c 64 20 3d 20 70 4b 65 79 49 6e 66 6f Field = pKeyInfo
1b36b 2d 3e 6e 46 69 65 6c 64 3b 0a 20 20 77 68 69 6c ->nField;. whil
1b36c 65 28 20 69 64 78 31 3c 73 7a 48 64 72 31 20 26 e( idx1<szHdr1 &
1b36d 26 20 69 3c 70 50 4b 65 79 32 2d 3e 6e 46 69 65 & i<pPKey2->nFie
1b36e 6c 64 20 29 7b 0a 20 20 20 20 75 33 32 20 73 65 ld ){. u32 se
1b36f 72 69 61 6c 5f 74 79 70 65 31 3b 0a 0a 20 20 20 rial_type1;..
1b370 20 2f 2a 20 52 65 61 64 20 74 68 65 20 73 65 72 /* Read the ser
1b371 69 61 6c 20 74 79 70 65 73 20 66 6f 72 20 74 68 ial types for th
1b372 65 20 6e 65 78 74 20 65 6c 65 6d 65 6e 74 20 69 e next element i
1b373 6e 20 65 61 63 68 20 6b 65 79 2e 20 2a 2f 0a 20 n each key. */.
1b374 20 20 20 69 64 78 31 20 2b 3d 20 67 65 74 56 61 idx1 += getVa
1b375 72 69 6e 74 33 32 28 20 61 4b 65 79 31 2b 69 64 rint32( aKey1+id
1b376 78 31 2c 20 73 65 72 69 61 6c 5f 74 79 70 65 31 x1, serial_type1
1b377 20 29 3b 0a 20 20 20 20 69 66 28 20 64 31 3e 3d );. if( d1>=
1b378 6e 4b 65 79 31 20 26 26 20 73 71 6c 69 74 65 33 nKey1 && sqlite3
1b379 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65 VdbeSerialTypeLe
1b37a 6e 28 73 65 72 69 61 6c 5f 74 79 70 65 31 29 3e n(serial_type1)>
1b37b 30 20 29 20 62 72 65 61 6b 3b 0a 0a 20 20 20 20 0 ) break;..
1b37c 2f 2a 20 45 78 74 72 61 63 74 20 74 68 65 20 76 /* Extract the v
1b37d 61 6c 75 65 73 20 74 6f 20 62 65 20 63 6f 6d 70 alues to be comp
1b37e 61 72 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 ared.. */.
1b37f 20 64 31 20 2b 3d 20 73 71 6c 69 74 65 33 56 64 d1 += sqlite3Vd
1b380 62 65 53 65 72 69 61 6c 47 65 74 28 26 61 4b 65 beSerialGet(&aKe
1b381 79 31 5b 64 31 5d 2c 20 73 65 72 69 61 6c 5f 74 y1[d1], serial_t
1b382 79 70 65 31 2c 20 26 6d 65 6d 31 29 3b 0a 0a 20 ype1, &mem1);..
1b383 20 20 20 2f 2a 20 44 6f 20 74 68 65 20 63 6f 6d /* Do the com
1b384 70 61 72 69 73 6f 6e 0a 20 20 20 20 2a 2f 0a 20 parison. */.
1b385 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4d rc = sqlite3M
1b386 65 6d 43 6f 6d 70 61 72 65 28 26 6d 65 6d 31 2c emCompare(&mem1,
1b387 20 26 70 50 4b 65 79 32 2d 3e 61 4d 65 6d 5b 69 &pPKey2->aMem[i
1b388 5d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ],.
1b389 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 3c i<
1b38a 6e 46 69 65 6c 64 20 3f 20 70 4b 65 79 49 6e 66 nField ? pKeyInf
1b38b 6f 2d 3e 61 43 6f 6c 6c 5b 69 5d 20 3a 20 30 29 o->aColl[i] : 0)
1b38c 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 30 20 ;. if( rc!=0
1b38d 29 7b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a ){. break;.
1b38e 20 20 20 20 7d 0a 20 20 20 20 69 2b 2b 3b 0a 20 }. i++;.
1b38f 20 7d 0a 0a 20 20 2f 2a 20 4e 6f 20 6d 65 6d 6f }.. /* No memo
1b390 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 ry allocation is
1b391 20 65 76 65 72 20 75 73 65 64 20 6f 6e 20 6d 65 ever used on me
1b392 6d 31 2e 20 2a 2f 0a 20 20 69 66 28 20 4e 45 56 m1. */. if( NEV
1b393 45 52 28 6d 65 6d 31 2e 7a 4d 61 6c 6c 6f 63 29 ER(mem1.zMalloc)
1b394 20 29 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 ) sqlite3VdbeMe
1b395 6d 52 65 6c 65 61 73 65 28 26 6d 65 6d 31 29 3b mRelease(&mem1);
1b396 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 50 52 .. /* If the PR
1b397 45 46 49 58 5f 53 45 41 52 43 48 20 66 6c 61 67 EFIX_SEARCH flag
1b398 20 69 73 20 73 65 74 20 61 6e 64 20 61 6c 6c 20 is set and all
1b399 66 69 65 6c 64 73 20 65 78 63 65 70 74 20 74 68 fields except th
1b39a 65 20 66 69 6e 61 6c 0a 20 20 2a 2a 20 72 6f 77 e final. ** row
1b39b 69 64 20 66 69 65 6c 64 20 77 65 72 65 20 65 71 id field were eq
1b39c 75 61 6c 2c 20 74 68 65 6e 20 63 6c 65 61 72 20 ual, then clear
1b39d 74 68 65 20 50 52 45 46 49 58 5f 53 45 41 52 43 the PREFIX_SEARC
1b39e 48 20 66 6c 61 67 20 61 6e 64 20 73 65 74 20 0a H flag and set .
1b39f 20 20 2a 2a 20 70 50 4b 65 79 32 2d 3e 72 6f 77 ** pPKey2->row
1b3a0 69 64 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 id to the value
1b3a1 6f 66 20 74 68 65 20 72 6f 77 69 64 20 66 69 65 of the rowid fie
1b3a2 6c 64 20 69 6e 20 28 70 4b 65 79 31 2c 20 6e 4b ld in (pKey1, nK
1b3a3 65 79 31 29 2e 0a 20 20 2a 2a 20 54 68 69 73 20 ey1).. ** This
1b3a4 69 73 20 75 73 65 64 20 62 79 20 74 68 65 20 4f is used by the O
1b3a5 50 5f 49 73 55 6e 69 71 75 65 20 6f 70 63 6f 64 P_IsUnique opcod
1b3a6 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 70 e.. */. if( (p
1b3a7 50 4b 65 79 32 2d 3e 66 6c 61 67 73 20 26 20 55 PKey2->flags & U
1b3a8 4e 50 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 53 NPACKED_PREFIX_S
1b3a9 45 41 52 43 48 29 20 26 26 20 69 3d 3d 28 70 50 EARCH) && i==(pP
1b3aa 4b 65 79 32 2d 3e 6e 46 69 65 6c 64 2d 31 29 20 Key2->nField-1)
1b3ab 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 ){. assert( i
1b3ac 64 78 31 3d 3d 73 7a 48 64 72 31 20 26 26 20 72 dx1==szHdr1 && r
1b3ad 63 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 c );. assert(
1b3ae 20 6d 65 6d 31 2e 66 6c 61 67 73 20 26 20 4d 45 mem1.flags & ME
1b3af 4d 5f 49 6e 74 20 29 3b 0a 20 20 20 20 70 50 4b M_Int );. pPK
1b3b0 65 79 32 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 55 ey2->flags &= ~U
1b3b1 4e 50 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 53 NPACKED_PREFIX_S
1b3b2 45 41 52 43 48 3b 0a 20 20 20 20 70 50 4b 65 79 EARCH;. pPKey
1b3b3 32 2d 3e 72 6f 77 69 64 20 3d 20 6d 65 6d 31 2e 2->rowid = mem1.
1b3b4 75 2e 69 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 u.i;. }.. if(
1b3b5 72 63 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 rc==0 ){. /*
1b3b6 72 63 3d 3d 30 20 68 65 72 65 20 6d 65 61 6e 73 rc==0 here means
1b3b7 20 74 68 61 74 20 6f 6e 65 20 6f 66 20 74 68 65 that one of the
1b3b8 20 6b 65 79 73 20 72 61 6e 20 6f 75 74 20 6f 66 keys ran out of
1b3b9 20 66 69 65 6c 64 73 20 61 6e 64 0a 20 20 20 20 fields and.
1b3ba 2a 2a 20 61 6c 6c 20 74 68 65 20 66 69 65 6c 64 ** all the field
1b3bb 73 20 75 70 20 74 6f 20 74 68 61 74 20 70 6f 69 s up to that poi
1b3bc 6e 74 20 77 65 72 65 20 65 71 75 61 6c 2e 20 49 nt were equal. I
1b3bd 66 20 74 68 65 20 55 4e 50 41 43 4b 45 44 5f 49 f the UNPACKED_I
1b3be 4e 43 52 4b 45 59 0a 20 20 20 20 2a 2a 20 66 6c NCRKEY. ** fl
1b3bf 61 67 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 ag is set, then
1b3c0 62 72 65 61 6b 20 74 68 65 20 74 69 65 20 62 79 break the tie by
1b3c1 20 74 72 65 61 74 69 6e 67 20 6b 65 79 32 20 61 treating key2 a
1b3c2 73 20 6c 61 72 67 65 72 2e 0a 20 20 20 20 2a 2a s larger.. **
1b3c3 20 49 66 20 74 68 65 20 55 50 41 43 4b 45 44 5f If the UPACKED_
1b3c4 50 52 45 46 49 58 5f 4d 41 54 43 48 20 66 6c 61 PREFIX_MATCH fla
1b3c5 67 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 6b g is set, then k
1b3c6 65 79 73 20 77 69 74 68 20 63 6f 6d 6d 6f 6e 20 eys with common
1b3c7 70 72 65 66 69 78 65 73 0a 20 20 20 20 2a 2a 20 prefixes. **
1b3c8 61 72 65 20 63 6f 6e 73 69 64 65 72 65 64 20 74 are considered t
1b3c9 6f 20 62 65 20 65 71 75 61 6c 2e 20 20 4f 74 68 o be equal. Oth
1b3ca 65 72 77 69 73 65 2c 20 74 68 65 20 6c 6f 6e 67 erwise, the long
1b3cb 65 72 20 6b 65 79 20 69 73 20 74 68 65 20 0a 20 er key is the .
1b3cc 20 20 20 2a 2a 20 6c 61 72 67 65 72 2e 20 20 41 ** larger. A
1b3cd 73 20 69 74 20 68 61 70 70 65 6e 73 2c 20 74 68 s it happens, th
1b3ce 65 20 70 50 4b 65 79 32 20 77 69 6c 6c 20 61 6c e pPKey2 will al
1b3cf 77 61 79 73 20 62 65 20 74 68 65 20 6c 6f 6e 67 ways be the long
1b3d0 65 72 0a 20 20 20 20 2a 2a 20 69 66 20 74 68 65 er. ** if the
1b3d1 72 65 20 69 73 20 61 20 64 69 66 66 65 72 65 6e re is a differen
1b3d2 63 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 ce.. */. i
1b3d3 66 28 20 70 50 4b 65 79 32 2d 3e 66 6c 61 67 73 f( pPKey2->flags
1b3d4 20 26 20 55 4e 50 41 43 4b 45 44 5f 49 4e 43 52 & UNPACKED_INCR
1b3d5 4b 45 59 20 29 7b 0a 20 20 20 20 20 20 72 63 20 KEY ){. rc
1b3d6 3d 20 2d 31 3b 0a 20 20 20 20 7d 65 6c 73 65 20 = -1;. }else
1b3d7 69 66 28 20 70 50 4b 65 79 32 2d 3e 66 6c 61 67 if( pPKey2->flag
1b3d8 73 20 26 20 55 4e 50 41 43 4b 45 44 5f 50 52 45 s & UNPACKED_PRE
1b3d9 46 49 58 5f 4d 41 54 43 48 20 29 7b 0a 20 20 20 FIX_MATCH ){.
1b3da 20 20 20 2f 2a 20 4c 65 61 76 65 20 72 63 3d 3d /* Leave rc==
1b3db 30 20 2a 2f 0a 20 20 20 20 7d 65 6c 73 65 20 69 0 */. }else i
1b3dc 66 28 20 69 64 78 31 3c 73 7a 48 64 72 31 20 29 f( idx1<szHdr1 )
1b3dd 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 31 3b 0a {. rc = 1;.
1b3de 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 }. }else if
1b3df 28 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 ( pKeyInfo->aSor
1b3e0 74 4f 72 64 65 72 20 26 26 20 69 3c 70 4b 65 79 tOrder && i<pKey
1b3e1 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 0a 20 20 20 Info->nField.
1b3e2 20 20 20 20 20 20 20 20 20 20 20 20 26 26 20 70 && p
1b3e3 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 KeyInfo->aSortOr
1b3e4 64 65 72 5b 69 5d 20 29 7b 0a 20 20 20 20 72 63 der[i] ){. rc
1b3e5 20 3d 20 2d 72 63 3b 0a 20 20 7d 0a 0a 20 20 72 = -rc;. }.. r
1b3e6 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 20 0a 0a 2f eturn rc;.}. ../
1b3e7 2a 0a 2a 2a 20 70 43 75 72 20 70 6f 69 6e 74 73 *.** pCur points
1b3e8 20 61 74 20 61 6e 20 69 6e 64 65 78 20 65 6e 74 at an index ent
1b3e9 72 79 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 ry created using
1b3ea 20 74 68 65 20 4f 50 5f 4d 61 6b 65 52 65 63 6f the OP_MakeReco
1b3eb 72 64 20 6f 70 63 6f 64 65 2e 0a 2a 2a 20 52 65 rd opcode..** Re
1b3ec 61 64 20 74 68 65 20 72 6f 77 69 64 20 28 74 68 ad the rowid (th
1b3ed 65 20 6c 61 73 74 20 66 69 65 6c 64 20 69 6e 20 e last field in
1b3ee 74 68 65 20 72 65 63 6f 72 64 29 20 61 6e 64 20 the record) and
1b3ef 73 74 6f 72 65 20 69 74 20 69 6e 20 2a 72 6f 77 store it in *row
1b3f0 69 64 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 id..** Return SQ
1b3f1 4c 49 54 45 5f 4f 4b 20 69 66 20 65 76 65 72 79 LITE_OK if every
1b3f2 74 68 69 6e 67 20 77 6f 72 6b 73 2c 20 6f 72 20 thing works, or
1b3f3 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 6f 74 an error code ot
1b3f4 68 65 72 77 69 73 65 2e 0a 2a 2a 0a 2a 2a 20 70 herwise..**.** p
1b3f5 43 75 72 20 6d 69 67 68 74 20 62 65 20 70 6f 69 Cur might be poi
1b3f6 6e 74 69 6e 67 20 74 6f 20 74 65 78 74 20 6f 62 nting to text ob
1b3f7 74 61 69 6e 65 64 20 66 72 6f 6d 20 61 20 63 6f tained from a co
1b3f8 72 72 75 70 74 20 64 61 74 61 62 61 73 65 20 66 rrupt database f
1b3f9 69 6c 65 2e 0a 2a 2a 20 53 6f 20 74 68 65 20 63 ile..** So the c
1b3fa 6f 6e 74 65 6e 74 20 63 61 6e 6e 6f 74 20 62 65 ontent cannot be
1b3fb 20 74 72 75 73 74 65 64 2e 20 20 44 6f 20 61 70 trusted. Do ap
1b3fc 70 72 6f 70 72 69 61 74 65 20 63 68 65 63 6b 73 propriate checks
1b3fd 20 6f 6e 20 74 68 65 20 63 6f 6e 74 65 6e 74 2e on the content.
1b3fe 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1b3ff 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
1b400 62 65 49 64 78 52 6f 77 69 64 28 73 71 6c 69 74 beIdxRowid(sqlit
1b401 65 33 20 2a 64 62 2c 20 42 74 43 75 72 73 6f 72 e3 *db, BtCursor
1b402 20 2a 70 43 75 72 2c 20 69 36 34 20 2a 72 6f 77 *pCur, i64 *row
1b403 69 64 29 7b 0a 20 20 69 36 34 20 6e 43 65 6c 6c id){. i64 nCell
1b404 4b 65 79 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 Key = 0;. int r
1b405 63 3b 0a 20 20 75 33 32 20 73 7a 48 64 72 3b 20 c;. u32 szHdr;
1b406 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f /* Size o
1b407 66 20 74 68 65 20 68 65 61 64 65 72 20 2a 2f 0a f the header */.
1b408 20 20 75 33 32 20 74 79 70 65 52 6f 77 69 64 3b u32 typeRowid;
1b409 20 20 20 20 2f 2a 20 53 65 72 69 61 6c 20 74 79 /* Serial ty
1b40a 70 65 20 6f 66 20 74 68 65 20 72 6f 77 69 64 20 pe of the rowid
1b40b 2a 2f 0a 20 20 75 33 32 20 6c 65 6e 52 6f 77 69 */. u32 lenRowi
1b40c 64 3b 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f d; /* Size o
1b40d 66 20 74 68 65 20 72 6f 77 69 64 20 2a 2f 0a 20 f the rowid */.
1b40e 20 4d 65 6d 20 6d 2c 20 76 3b 0a 0a 20 20 55 4e Mem m, v;.. UN
1b40f 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 64 USED_PARAMETER(d
1b410 62 29 3b 0a 0a 20 20 2f 2a 20 47 65 74 20 74 68 b);.. /* Get th
1b411 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 69 6e e size of the in
1b412 64 65 78 20 65 6e 74 72 79 2e 20 20 4f 6e 6c 79 dex entry. Only
1b413 20 69 6e 64 69 63 65 73 20 65 6e 74 72 69 65 73 indices entries
1b414 20 6f 66 20 6c 65 73 73 0a 20 20 2a 2a 20 74 68 of less. ** th
1b415 61 6e 20 32 47 69 42 20 61 72 65 20 73 75 70 70 an 2GiB are supp
1b416 6f 72 74 20 2d 20 61 6e 79 74 68 69 6e 67 20 6c ort - anything l
1b417 61 72 67 65 20 6d 75 73 74 20 62 65 20 64 61 74 arge must be dat
1b418 61 62 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e abase corruption
1b419 2e 0a 20 20 2a 2a 20 41 6e 79 20 63 6f 72 72 75 .. ** Any corru
1b41a 70 74 69 6f 6e 20 69 73 20 64 65 74 65 63 74 65 ption is detecte
1b41b 64 20 69 6e 20 73 71 6c 69 74 65 33 42 74 72 65 d in sqlite3Btre
1b41c 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 29 2c eParseCellPtr(),
1b41d 20 74 68 6f 75 67 68 2c 20 73 6f 0a 20 20 2a 2a though, so. **
1b41e 20 74 68 69 73 20 63 6f 64 65 20 63 61 6e 20 73 this code can s
1b41f 61 66 65 6c 79 20 61 73 73 75 6d 65 20 74 68 61 afely assume tha
1b420 74 20 6e 43 65 6c 6c 4b 65 79 20 69 73 20 33 32 t nCellKey is 32
1b421 2d 62 69 74 73 20 20 0a 20 20 2a 2f 0a 20 20 61 -bits . */. a
1b422 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 ssert( sqlite3Bt
1b423 72 65 65 43 75 72 73 6f 72 49 73 56 61 6c 69 64 reeCursorIsValid
1b424 28 70 43 75 72 29 20 29 3b 0a 20 20 72 63 20 3d (pCur) );. rc =
1b425 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79 sqlite3BtreeKey
1b426 53 69 7a 65 28 70 43 75 72 2c 20 26 6e 43 65 6c Size(pCur, &nCel
1b427 6c 4b 65 79 29 3b 0a 20 20 61 73 73 65 72 74 28 lKey);. assert(
1b428 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
1b429 3b 20 20 20 20 20 2f 2a 20 70 43 75 72 20 69 73 ; /* pCur is
1b42a 20 61 6c 77 61 79 73 20 76 61 6c 69 64 20 73 6f always valid so
1b42b 20 4b 65 79 53 69 7a 65 20 63 61 6e 6e 6f 74 20 KeySize cannot
1b42c 66 61 69 6c 20 2a 2f 0a 20 20 61 73 73 65 72 74 fail */. assert
1b42d 28 20 28 6e 43 65 6c 6c 4b 65 79 20 26 20 53 51 ( (nCellKey & SQ
1b42e 4c 49 54 45 5f 4d 41 58 5f 55 33 32 29 3d 3d 28 LITE_MAX_U32)==(
1b42f 75 36 34 29 6e 43 65 6c 6c 4b 65 79 20 29 3b 0a u64)nCellKey );.
1b430 0a 20 20 2f 2a 20 52 65 61 64 20 69 6e 20 74 68 . /* Read in th
1b431 65 20 63 6f 6d 70 6c 65 74 65 20 63 6f 6e 74 65 e complete conte
1b432 6e 74 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 nt of the index
1b433 65 6e 74 72 79 20 2a 2f 0a 20 20 6d 65 6d 73 65 entry */. memse
1b434 74 28 26 6d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 t(&m, 0, sizeof(
1b435 6d 29 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 m));. rc = sqli
1b436 74 65 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42 74 te3VdbeMemFromBt
1b437 72 65 65 28 70 43 75 72 2c 20 30 2c 20 28 69 6e ree(pCur, 0, (in
1b438 74 29 6e 43 65 6c 6c 4b 65 79 2c 20 31 2c 20 26 t)nCellKey, 1, &
1b439 6d 29 3b 0a 20 20 69 66 28 20 72 63 20 29 7b 0a m);. if( rc ){.
1b43a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
1b43b 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 69 6e 64 }.. /* The ind
1b43c 65 78 20 65 6e 74 72 79 20 6d 75 73 74 20 62 65 ex entry must be
1b43d 67 69 6e 20 77 69 74 68 20 61 20 68 65 61 64 65 gin with a heade
1b43e 72 20 73 69 7a 65 20 2a 2f 0a 20 20 28 76 6f 69 r size */. (voi
1b43f 64 29 67 65 74 56 61 72 69 6e 74 33 32 28 28 75 d)getVarint32((u
1b440 38 2a 29 6d 2e 7a 2c 20 73 7a 48 64 72 29 3b 0a 8*)m.z, szHdr);.
1b441 20 20 74 65 73 74 63 61 73 65 28 20 73 7a 48 64 testcase( szHd
1b442 72 3d 3d 33 20 29 3b 0a 20 20 74 65 73 74 63 61 r==3 );. testca
1b443 73 65 28 20 73 7a 48 64 72 3d 3d 6d 2e 6e 20 29 se( szHdr==m.n )
1b444 3b 0a 20 20 69 66 28 20 75 6e 6c 69 6b 65 6c 79 ;. if( unlikely
1b445 28 73 7a 48 64 72 3c 33 20 7c 7c 20 28 69 6e 74 (szHdr<3 || (int
1b446 29 73 7a 48 64 72 3e 6d 2e 6e 29 20 29 7b 0a 20 )szHdr>m.n) ){.
1b447 20 20 20 67 6f 74 6f 20 69 64 78 5f 72 6f 77 69 goto idx_rowi
1b448 64 5f 63 6f 72 72 75 70 74 69 6f 6e 3b 0a 20 20 d_corruption;.
1b449 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 6c 61 73 74 }.. /* The last
1b44a 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20 69 6e field of the in
1b44b 64 65 78 20 73 68 6f 75 6c 64 20 62 65 20 61 6e dex should be an
1b44c 20 69 6e 74 65 67 65 72 20 2d 20 74 68 65 20 52 integer - the R
1b44d 4f 57 49 44 2e 0a 20 20 2a 2a 20 56 65 72 69 66 OWID.. ** Verif
1b44e 79 20 74 68 61 74 20 74 68 65 20 6c 61 73 74 20 y that the last
1b44f 65 6e 74 72 79 20 72 65 61 6c 6c 79 20 69 73 20 entry really is
1b450 61 6e 20 69 6e 74 65 67 65 72 2e 20 2a 2f 0a 20 an integer. */.
1b451 20 28 76 6f 69 64 29 67 65 74 56 61 72 69 6e 74 (void)getVarint
1b452 33 32 28 28 75 38 2a 29 26 6d 2e 7a 5b 73 7a 48 32((u8*)&m.z[szH
1b453 64 72 2d 31 5d 2c 20 74 79 70 65 52 6f 77 69 64 dr-1], typeRowid
1b454 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 74 );. testcase( t
1b455 79 70 65 52 6f 77 69 64 3d 3d 31 20 29 3b 0a 20 ypeRowid==1 );.
1b456 20 74 65 73 74 63 61 73 65 28 20 74 79 70 65 52 testcase( typeR
1b457 6f 77 69 64 3d 3d 32 20 29 3b 0a 20 20 74 65 73 owid==2 );. tes
1b458 74 63 61 73 65 28 20 74 79 70 65 52 6f 77 69 64 tcase( typeRowid
1b459 3d 3d 33 20 29 3b 0a 20 20 74 65 73 74 63 61 73 ==3 );. testcas
1b45a 65 28 20 74 79 70 65 52 6f 77 69 64 3d 3d 34 20 e( typeRowid==4
1b45b 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 74 );. testcase( t
1b45c 79 70 65 52 6f 77 69 64 3d 3d 35 20 29 3b 0a 20 ypeRowid==5 );.
1b45d 20 74 65 73 74 63 61 73 65 28 20 74 79 70 65 52 testcase( typeR
1b45e 6f 77 69 64 3d 3d 36 20 29 3b 0a 20 20 74 65 73 owid==6 );. tes
1b45f 74 63 61 73 65 28 20 74 79 70 65 52 6f 77 69 64 tcase( typeRowid
1b460 3d 3d 38 20 29 3b 0a 20 20 74 65 73 74 63 61 73 ==8 );. testcas
1b461 65 28 20 74 79 70 65 52 6f 77 69 64 3d 3d 39 20 e( typeRowid==9
1b462 29 3b 0a 20 20 69 66 28 20 75 6e 6c 69 6b 65 6c );. if( unlikel
1b463 79 28 74 79 70 65 52 6f 77 69 64 3c 31 20 7c 7c y(typeRowid<1 ||
1b464 20 74 79 70 65 52 6f 77 69 64 3e 39 20 7c 7c 20 typeRowid>9 ||
1b465 74 79 70 65 52 6f 77 69 64 3d 3d 37 29 20 29 7b typeRowid==7) ){
1b466 0a 20 20 20 20 67 6f 74 6f 20 69 64 78 5f 72 6f . goto idx_ro
1b467 77 69 64 5f 63 6f 72 72 75 70 74 69 6f 6e 3b 0a wid_corruption;.
1b468 20 20 7d 0a 20 20 6c 65 6e 52 6f 77 69 64 20 3d }. lenRowid =
1b469 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 sqlite3VdbeSeri
1b46a 61 6c 54 79 70 65 4c 65 6e 28 74 79 70 65 52 6f alTypeLen(typeRo
1b46b 77 69 64 29 3b 0a 20 20 74 65 73 74 63 61 73 65 wid);. testcase
1b46c 28 20 28 75 33 32 29 6d 2e 6e 3d 3d 73 7a 48 64 ( (u32)m.n==szHd
1b46d 72 2b 6c 65 6e 52 6f 77 69 64 20 29 3b 0a 20 20 r+lenRowid );.
1b46e 69 66 28 20 75 6e 6c 69 6b 65 6c 79 28 28 75 33 if( unlikely((u3
1b46f 32 29 6d 2e 6e 3c 73 7a 48 64 72 2b 6c 65 6e 52 2)m.n<szHdr+lenR
1b470 6f 77 69 64 29 20 29 7b 0a 20 20 20 20 67 6f 74 owid) ){. got
1b471 6f 20 69 64 78 5f 72 6f 77 69 64 5f 63 6f 72 72 o idx_rowid_corr
1b472 75 70 74 69 6f 6e 3b 0a 20 20 7d 0a 0a 20 20 2f uption;. }.. /
1b473 2a 20 46 65 74 63 68 20 74 68 65 20 69 6e 74 65 * Fetch the inte
1b474 67 65 72 20 6f 66 66 20 74 68 65 20 65 6e 64 20 ger off the end
1b475 6f 66 20 74 68 65 20 69 6e 64 65 78 20 72 65 63 of the index rec
1b476 6f 72 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 ord */. sqlite3
1b477 56 64 62 65 53 65 72 69 61 6c 47 65 74 28 28 75 VdbeSerialGet((u
1b478 38 2a 29 26 6d 2e 7a 5b 6d 2e 6e 2d 6c 65 6e 52 8*)&m.z[m.n-lenR
1b479 6f 77 69 64 5d 2c 20 74 79 70 65 52 6f 77 69 64 owid], typeRowid
1b47a 2c 20 26 76 29 3b 0a 20 20 2a 72 6f 77 69 64 20 , &v);. *rowid
1b47b 3d 20 76 2e 75 2e 69 3b 0a 20 20 73 71 6c 69 74 = v.u.i;. sqlit
1b47c 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 e3VdbeMemRelease
1b47d 28 26 6d 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 (&m);. return S
1b47e 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20 QLITE_OK;.. /*
1b47f 4a 75 6d 70 20 68 65 72 65 20 69 66 20 64 61 74 Jump here if dat
1b480 61 62 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e abase corruption
1b481 20 69 73 20 64 65 74 65 63 74 65 64 20 61 66 74 is detected aft
1b482 65 72 20 6d 20 68 61 73 20 62 65 65 6e 0a 20 20 er m has been.
1b483 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 46 ** allocated. F
1b484 72 65 65 20 74 68 65 20 6d 20 6f 62 6a 65 63 74 ree the m object
1b485 20 61 6e 64 20 72 65 74 75 72 6e 20 53 51 4c 49 and return SQLI
1b486 54 45 5f 43 4f 52 52 55 50 54 2e 20 2a 2f 0a 69 TE_CORRUPT. */.i
1b487 64 78 5f 72 6f 77 69 64 5f 63 6f 72 72 75 70 74 dx_rowid_corrupt
1b488 69 6f 6e 3a 0a 20 20 74 65 73 74 63 61 73 65 28 ion:. testcase(
1b489 20 6d 2e 7a 4d 61 6c 6c 6f 63 21 3d 30 20 29 3b m.zMalloc!=0 );
1b48a 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 . sqlite3VdbeMe
1b48b 6d 52 65 6c 65 61 73 65 28 26 6d 29 3b 0a 20 20 mRelease(&m);.
1b48c 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f return SQLITE_CO
1b48d 52 52 55 50 54 5f 42 4b 50 54 3b 0a 7d 0a 0a 2f RRUPT_BKPT;.}../
1b48e 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65 20 74 68 65 *.** Compare the
1b48f 20 6b 65 79 20 6f 66 20 74 68 65 20 69 6e 64 65 key of the inde
1b490 78 20 65 6e 74 72 79 20 74 68 61 74 20 63 75 72 x entry that cur
1b491 73 6f 72 20 70 43 20 69 73 20 70 6f 69 6e 74 69 sor pC is pointi
1b492 6e 67 20 74 6f 20 61 67 61 69 6e 73 74 0a 2a 2a ng to against.**
1b493 20 74 68 65 20 6b 65 79 20 73 74 72 69 6e 67 20 the key string
1b494 69 6e 20 70 55 6e 70 61 63 6b 65 64 2e 20 20 57 in pUnpacked. W
1b495 72 69 74 65 20 69 6e 74 6f 20 2a 70 52 65 73 20 rite into *pRes
1b496 61 20 6e 75 6d 62 65 72 0a 2a 2a 20 74 68 61 74 a number.** that
1b497 20 69 73 20 6e 65 67 61 74 69 76 65 2c 20 7a 65 is negative, ze
1b498 72 6f 2c 20 6f 72 20 70 6f 73 69 74 69 76 65 20 ro, or positive
1b499 69 66 20 70 43 20 69 73 20 6c 65 73 73 20 74 68 if pC is less th
1b49a 61 6e 2c 20 65 71 75 61 6c 20 74 6f 2c 0a 2a 2a an, equal to,.**
1b49b 20 6f 72 20 67 72 65 61 74 65 72 20 74 68 61 6e or greater than
1b49c 20 70 55 6e 70 61 63 6b 65 64 2e 20 20 52 65 74 pUnpacked. Ret
1b49d 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e urn SQLITE_OK on
1b49e 20 73 75 63 63 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 success..**.**
1b49f 70 55 6e 70 61 63 6b 65 64 20 69 73 20 65 69 74 pUnpacked is eit
1b4a0 68 65 72 20 63 72 65 61 74 65 64 20 77 69 74 68 her created with
1b4a1 6f 75 74 20 61 20 72 6f 77 69 64 20 6f 72 20 69 out a rowid or i
1b4a2 73 20 74 72 75 6e 63 61 74 65 64 20 73 6f 20 74 s truncated so t
1b4a3 68 61 74 20 69 74 0a 2a 2a 20 6f 6d 69 74 73 20 hat it.** omits
1b4a4 74 68 65 20 72 6f 77 69 64 20 61 74 20 74 68 65 the rowid at the
1b4a5 20 65 6e 64 2e 20 20 54 68 65 20 72 6f 77 69 64 end. The rowid
1b4a6 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 at the end of t
1b4a7 68 65 20 69 6e 64 65 78 20 65 6e 74 72 79 0a 2a he index entry.*
1b4a8 2a 20 69 73 20 69 67 6e 6f 72 65 64 20 61 73 20 * is ignored as
1b4a9 77 65 6c 6c 2e 20 20 48 65 6e 63 65 2c 20 74 68 well. Hence, th
1b4aa 69 73 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 is routine only
1b4ab 63 6f 6d 70 61 72 65 73 20 74 68 65 20 70 72 65 compares the pre
1b4ac 66 69 78 65 73 20 0a 2a 2a 20 6f 66 20 74 68 65 fixes .** of the
1b4ad 20 6b 65 79 73 20 70 72 69 6f 72 20 74 6f 20 74 keys prior to t
1b4ae 68 65 20 66 69 6e 61 6c 20 72 6f 77 69 64 2c 20 he final rowid,
1b4af 6e 6f 74 20 74 68 65 20 65 6e 74 69 72 65 20 6b not the entire k
1b4b0 65 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ey..*/.SQLITE_PR
1b4b1 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
1b4b2 33 56 64 62 65 49 64 78 4b 65 79 43 6f 6d 70 61 3VdbeIdxKeyCompa
1b4b3 72 65 28 0a 20 20 56 64 62 65 43 75 72 73 6f 72 re(. VdbeCursor
1b4b4 20 2a 70 43 2c 20 20 20 20 20 20 20 20 20 20 20 *pC,
1b4b5 20 20 2f 2a 20 54 68 65 20 63 75 72 73 6f 72 20 /* The cursor
1b4b6 74 6f 20 63 6f 6d 70 61 72 65 20 61 67 61 69 6e to compare again
1b4b7 73 74 20 2a 2f 0a 20 20 55 6e 70 61 63 6b 65 64 st */. Unpacked
1b4b8 52 65 63 6f 72 64 20 2a 70 55 6e 70 61 63 6b 65 Record *pUnpacke
1b4b9 64 2c 20 20 2f 2a 20 55 6e 70 61 63 6b 65 64 20 d, /* Unpacked
1b4ba 76 65 72 73 69 6f 6e 20 6f 66 20 6b 65 79 20 74 version of key t
1b4bb 6f 20 63 6f 6d 70 61 72 65 20 61 67 61 69 6e 73 o compare agains
1b4bc 74 20 2a 2f 0a 20 20 69 6e 74 20 2a 72 65 73 20 t */. int *res
1b4bd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b4be 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 /* Write the
1b4bf 63 6f 6d 70 61 72 69 73 6f 6e 20 72 65 73 75 6c comparison resul
1b4c0 74 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 t here */.){. i
1b4c1 36 34 20 6e 43 65 6c 6c 4b 65 79 20 3d 20 30 3b 64 nCellKey = 0;
1b4c2 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 42 74 43 . int rc;. BtC
1b4c3 75 72 73 6f 72 20 2a 70 43 75 72 20 3d 20 70 43 ursor *pCur = pC
1b4c4 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 4d 65 6d ->pCursor;. Mem
1b4c5 20 6d 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 m;.. assert( s
1b4c6 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f qlite3BtreeCurso
1b4c7 72 49 73 56 61 6c 69 64 28 70 43 75 72 29 20 29 rIsValid(pCur) )
1b4c8 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 ;. rc = sqlite3
1b4c9 42 74 72 65 65 4b 65 79 53 69 7a 65 28 70 43 75 BtreeKeySize(pCu
1b4ca 72 2c 20 26 6e 43 65 6c 6c 4b 65 79 29 3b 0a 20 r, &nCellKey);.
1b4cb 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c assert( rc==SQL
1b4cc 49 54 45 5f 4f 4b 20 29 3b 20 20 20 20 2f 2a 20 ITE_OK ); /*
1b4cd 70 43 75 72 20 69 73 20 61 6c 77 61 79 73 20 76 pCur is always v
1b4ce 61 6c 69 64 20 73 6f 20 4b 65 79 53 69 7a 65 20 alid so KeySize
1b4cf 63 61 6e 6e 6f 74 20 66 61 69 6c 20 2a 2f 0a 20 cannot fail */.
1b4d0 20 2f 2a 20 6e 43 65 6c 6c 4b 65 79 20 77 69 6c /* nCellKey wil
1b4d1 6c 20 61 6c 77 61 79 73 20 62 65 20 62 65 74 77 l always be betw
1b4d2 65 65 6e 20 30 20 61 6e 64 20 30 78 66 66 66 66 een 0 and 0xffff
1b4d3 66 66 66 66 20 62 65 63 61 75 73 65 20 6f 66 20 ffff because of
1b4d4 74 68 65 20 73 61 79 0a 20 20 2a 2a 20 74 68 61 the say. ** tha
1b4d5 74 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c t btreeParseCell
1b4d6 50 74 72 28 29 20 61 6e 64 20 73 71 6c 69 74 65 Ptr() and sqlite
1b4d7 33 47 65 74 56 61 72 69 6e 74 33 32 28 29 20 61 3GetVarint32() a
1b4d8 72 65 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 2a re implemented *
1b4d9 2f 0a 20 20 69 66 28 20 6e 43 65 6c 6c 4b 65 79 /. if( nCellKey
1b4da 3c 3d 30 20 7c 7c 20 6e 43 65 6c 6c 4b 65 79 3e <=0 || nCellKey>
1b4db 30 78 37 66 66 66 66 66 66 66 20 29 7b 0a 20 20 0x7fffffff ){.
1b4dc 20 20 2a 72 65 73 20 3d 20 30 3b 0a 20 20 20 20 *res = 0;.
1b4dd 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f return SQLITE_CO
1b4de 52 52 55 50 54 3b 0a 20 20 7d 0a 20 20 6d 65 6d RRUPT;. }. mem
1b4df 73 65 74 28 26 6d 2c 20 30 2c 20 73 69 7a 65 6f set(&m, 0, sizeo
1b4e0 66 28 6d 29 29 3b 0a 20 20 72 63 20 3d 20 73 71 f(m));. rc = sq
1b4e1 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 72 6f 6d lite3VdbeMemFrom
1b4e2 42 74 72 65 65 28 70 43 2d 3e 70 43 75 72 73 6f Btree(pC->pCurso
1b4e3 72 2c 20 30 2c 20 28 69 6e 74 29 6e 43 65 6c 6c r, 0, (int)nCell
1b4e4 4b 65 79 2c 20 31 2c 20 26 6d 29 3b 0a 20 20 69 Key, 1, &m);. i
1b4e5 66 28 20 72 63 20 29 7b 0a 20 20 20 20 72 65 74 f( rc ){. ret
1b4e6 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 61 73 urn rc;. }. as
1b4e7 73 65 72 74 28 20 70 55 6e 70 61 63 6b 65 64 2d sert( pUnpacked-
1b4e8 3e 66 6c 61 67 73 20 26 20 55 4e 50 41 43 4b 45 >flags & UNPACKE
1b4e9 44 5f 49 47 4e 4f 52 45 5f 52 4f 57 49 44 20 29 D_IGNORE_ROWID )
1b4ea 3b 0a 20 20 2a 72 65 73 20 3d 20 73 71 6c 69 74 ;. *res = sqlit
1b4eb 65 33 56 64 62 65 52 65 63 6f 72 64 43 6f 6d 70 e3VdbeRecordComp
1b4ec 61 72 65 28 6d 2e 6e 2c 20 6d 2e 7a 2c 20 70 55 are(m.n, m.z, pU
1b4ed 6e 70 61 63 6b 65 64 29 3b 0a 20 20 73 71 6c 69 npacked);. sqli
1b4ee 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 te3VdbeMemReleas
1b4ef 65 28 26 6d 29 3b 0a 20 20 72 65 74 75 72 6e 20 e(&m);. return
1b4f0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a SQLITE_OK;.}../*
1b4f1 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
1b4f2 20 73 65 74 73 20 74 68 65 20 76 61 6c 75 65 20 sets the value
1b4f3 74 6f 20 62 65 20 72 65 74 75 72 6e 65 64 20 62 to be returned b
1b4f4 79 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c y subsequent cal
1b4f5 6c 73 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 ls to.** sqlite3
1b4f6 5f 63 68 61 6e 67 65 73 28 29 20 6f 6e 20 74 68 _changes() on th
1b4f7 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c e database handl
1b4f8 65 20 27 64 62 27 2e 20 0a 2a 2f 0a 53 51 4c 49 e 'db'. .*/.SQLI
1b4f9 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
1b4fa 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 68 sqlite3VdbeSetCh
1b4fb 61 6e 67 65 73 28 73 71 6c 69 74 65 33 20 2a 64 anges(sqlite3 *d
1b4fc 62 2c 20 69 6e 74 20 6e 43 68 61 6e 67 65 29 7b b, int nChange){
1b4fd 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
1b4fe 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 e3_mutex_held(db
1b4ff 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 64 62 ->mutex) );. db
1b500 2d 3e 6e 43 68 61 6e 67 65 20 3d 20 6e 43 68 61 ->nChange = nCha
1b501 6e 67 65 3b 0a 20 20 64 62 2d 3e 6e 54 6f 74 61 nge;. db->nTota
1b502 6c 43 68 61 6e 67 65 20 2b 3d 20 6e 43 68 61 6e lChange += nChan
1b503 67 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 ge;.}../*.** Set
1b504 20 61 20 66 6c 61 67 20 69 6e 20 74 68 65 20 76 a flag in the v
1b505 64 62 65 20 74 6f 20 75 70 64 61 74 65 20 74 68 dbe to update th
1b506 65 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 e change counter
1b507 20 77 68 65 6e 20 69 74 20 69 73 20 66 69 6e 61 when it is fina
1b508 6c 69 73 65 64 0a 2a 2a 20 6f 72 20 72 65 73 65 lised.** or rese
1b509 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 t..*/.SQLITE_PRI
1b50a 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
1b50b 33 56 64 62 65 43 6f 75 6e 74 43 68 61 6e 67 65 3VdbeCountChange
1b50c 73 28 56 64 62 65 20 2a 76 29 7b 0a 20 20 76 2d s(Vdbe *v){. v-
1b50d 3e 63 68 61 6e 67 65 43 6e 74 4f 6e 20 3d 20 31 >changeCntOn = 1
1b50e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 72 6b 20 ;.}../*.** Mark
1b50f 65 76 65 72 79 20 70 72 65 70 61 72 65 64 20 73 every prepared s
1b510 74 61 74 65 6d 65 6e 74 20 61 73 73 6f 63 69 61 tatement associa
1b511 74 65 64 20 77 69 74 68 20 61 20 64 61 74 61 62 ted with a datab
1b512 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a ase connection.*
1b513 2a 20 61 73 20 65 78 70 69 72 65 64 2e 0a 2a 2a * as expired..**
1b514 0a 2a 2a 20 41 6e 20 65 78 70 69 72 65 64 20 73 .** An expired s
1b515 74 61 74 65 6d 65 6e 74 20 6d 65 61 6e 73 20 74 tatement means t
1b516 68 61 74 20 72 65 63 6f 6d 70 69 6c 61 74 69 6f hat recompilatio
1b517 6e 20 6f 66 20 74 68 65 20 73 74 61 74 65 6d 65 n of the stateme
1b518 6e 74 20 69 73 0a 2a 2a 20 72 65 63 6f 6d 6d 65 nt is.** recomme
1b519 6e 64 2e 20 20 53 74 61 74 65 6d 65 6e 74 73 20 nd. Statements
1b51a 65 78 70 69 72 65 20 77 68 65 6e 20 74 68 69 6e expire when thin
1b51b 67 73 20 68 61 70 70 65 6e 20 74 68 61 74 20 6d gs happen that m
1b51c 61 6b 65 20 74 68 65 69 72 0a 2a 2a 20 70 72 6f ake their.** pro
1b51d 67 72 61 6d 73 20 6f 62 73 6f 6c 65 74 65 2e 20 grams obsolete.
1b51e 20 52 65 6d 6f 76 69 6e 67 20 75 73 65 72 2d 64 Removing user-d
1b51f 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 73 efined functions
1b520 20 6f 72 20 63 6f 6c 6c 61 74 69 6e 67 0a 2a 2a or collating.**
1b521 20 73 65 71 75 65 6e 63 65 73 2c 20 6f 72 20 63 sequences, or c
1b522 68 61 6e 67 69 6e 67 20 61 6e 20 61 75 74 68 6f hanging an autho
1b523 72 69 7a 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f rization functio
1b524 6e 20 61 72 65 20 74 68 65 20 74 79 70 65 73 20 n are the types
1b525 6f 66 0a 2a 2a 20 74 68 69 6e 67 73 20 74 68 61 of.** things tha
1b526 74 20 6d 61 6b 65 20 70 72 65 70 61 72 65 64 20 t make prepared
1b527 73 74 61 74 65 6d 65 6e 74 73 20 6f 62 73 6f 6c statements obsol
1b528 65 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ete..*/.SQLITE_P
1b529 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
1b52a 74 65 33 45 78 70 69 72 65 50 72 65 70 61 72 65 te3ExpirePrepare
1b52b 64 53 74 61 74 65 6d 65 6e 74 73 28 73 71 6c 69 dStatements(sqli
1b52c 74 65 33 20 2a 64 62 29 7b 0a 20 20 56 64 62 65 te3 *db){. Vdbe
1b52d 20 2a 70 3b 0a 20 20 66 6f 72 28 70 20 3d 20 64 *p;. for(p = d
1b52e 62 2d 3e 70 56 64 62 65 3b 20 70 3b 20 70 3d 70 b->pVdbe; p; p=p
1b52f 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 70 2d ->pNext){. p-
1b530 3e 65 78 70 69 72 65 64 20 3d 20 31 3b 0a 20 20 >expired = 1;.
1b531 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 }.}../*.** Retur
1b532 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61 n the database a
1b533 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 ssociated with t
1b534 68 65 20 56 64 62 65 2e 0a 2a 2f 0a 53 51 4c 49 he Vdbe..*/.SQLI
1b535 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74 TE_PRIVATE sqlit
1b536 65 33 20 2a 73 71 6c 69 74 65 33 56 64 62 65 44 e3 *sqlite3VdbeD
1b537 62 28 56 64 62 65 20 2a 76 29 7b 0a 20 20 72 65 b(Vdbe *v){. re
1b538 74 75 72 6e 20 76 2d 3e 64 62 3b 0a 7d 0a 0a 2f turn v->db;.}../
1b539 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
1b53a 6e 64 20 6f 66 20 76 64 62 65 61 75 78 2e 63 20 nd of vdbeaux.c
1b53b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b53c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b53d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
1b53e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 ************** B
1b53f 65 67 69 6e 20 66 69 6c 65 20 76 64 62 65 61 70 egin file vdbeap
1b540 69 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a i.c ************
1b541 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b542 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
1b543 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79 20 32 36 *.** 2004 May 26
1b544 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
1b545 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
1b546 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
1b547 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
1b548 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
1b549 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
1b54a 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
1b54b 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
1b54c 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
1b54d 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
1b54e 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
1b54f 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
1b550 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
1b551 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
1b552 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
1b553 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
1b554 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
1b555 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
1b556 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b557 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b558 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b559 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
1b55a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 **.** This file
1b55b 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 75 73 contains code us
1b55c 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 41 e to implement A
1b55d 50 49 73 20 74 68 61 74 20 61 72 65 20 70 61 72 PIs that are par
1b55e 74 20 6f 66 20 74 68 65 0a 2a 2a 20 56 44 42 45 t of the.** VDBE
1b55f 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76 64 62 ..**.** $Id: vdb
1b560 65 61 70 69 2e 63 2c 76 20 31 2e 31 36 37 20 32 eapi.c,v 1.167 2
1b561 30 30 39 2f 30 36 2f 32 35 20 30 31 3a 34 37 3a 009/06/25 01:47:
1b562 31 32 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 12 drh Exp $.*/.
1b563 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1b564 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45 44 0a OMIT_DEPRECATED.
1b565 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 /*.** Return TRU
1b566 45 20 28 6e 6f 6e 2d 7a 65 72 6f 29 20 6f 66 20 E (non-zero) of
1b567 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 73 75 the statement su
1b568 70 70 6c 69 65 64 20 61 73 20 61 6e 20 61 72 67 pplied as an arg
1b569 75 6d 65 6e 74 20 6e 65 65 64 73 0a 2a 2a 20 74 ument needs.** t
1b56a 6f 20 62 65 20 72 65 63 6f 6d 70 69 6c 65 64 2e o be recompiled.
1b56b 20 20 41 20 73 74 61 74 65 6d 65 6e 74 20 6e 65 A statement ne
1b56c 65 64 73 20 74 6f 20 62 65 20 72 65 63 6f 6d 70 eds to be recomp
1b56d 69 6c 65 64 20 77 68 65 6e 65 76 65 72 20 74 68 iled whenever th
1b56e 65 0a 2a 2a 20 65 78 65 63 75 74 69 6f 6e 20 65 e.** execution e
1b56f 6e 76 69 72 6f 6e 6d 65 6e 74 20 63 68 61 6e 67 nvironment chang
1b570 65 73 20 69 6e 20 61 20 77 61 79 20 74 68 61 74 es in a way that
1b571 20 77 6f 75 6c 64 20 61 6c 74 65 72 20 74 68 65 would alter the
1b572 20 70 72 6f 67 72 61 6d 0a 2a 2a 20 74 68 61 74 program.** that
1b573 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 sqlite3_prepare
1b574 28 29 20 67 65 6e 65 72 61 74 65 73 2e 20 20 46 () generates. F
1b575 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 6e or example, if n
1b576 65 77 20 66 75 6e 63 74 69 6f 6e 73 20 6f 72 0a ew functions or.
1b577 2a 2a 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 ** collating seq
1b578 75 65 6e 63 65 73 20 61 72 65 20 72 65 67 69 73 uences are regis
1b579 74 65 72 65 64 20 6f 72 20 69 66 20 61 6e 20 61 tered or if an a
1b57a 75 74 68 6f 72 69 7a 65 72 20 66 75 6e 63 74 69 uthorizer functi
1b57b 6f 6e 20 69 73 0a 2a 2a 20 61 64 64 65 64 20 6f on is.** added o
1b57c 72 20 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a 53 51 r changed..*/.SQ
1b57d 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
1b57e 69 74 65 33 5f 65 78 70 69 72 65 64 28 73 71 6c ite3_expired(sql
1b57f 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 ite3_stmt *pStmt
1b580 29 7b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 ){. Vdbe *p = (
1b581 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20 72 Vdbe*)pStmt;. r
1b582 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 70 2d eturn p==0 || p-
1b583 3e 65 78 70 69 72 65 64 3b 0a 7d 0a 23 65 6e 64 >expired;.}.#end
1b584 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f if../*.** The fo
1b585 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 20 llowing routine
1b586 64 65 73 74 72 6f 79 73 20 61 20 76 69 72 74 75 destroys a virtu
1b587 61 6c 20 6d 61 63 68 69 6e 65 20 74 68 61 74 20 al machine that
1b588 69 73 20 63 72 65 61 74 65 64 20 62 79 0a 2a 2a is created by.**
1b589 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6d the sqlite3_com
1b58a 70 69 6c 65 28 29 20 72 6f 75 74 69 6e 65 2e 20 pile() routine.
1b58b 54 68 65 20 69 6e 74 65 67 65 72 20 72 65 74 75 The integer retu
1b58c 72 6e 65 64 20 69 73 20 61 6e 20 53 51 4c 49 54 rned is an SQLIT
1b58d 45 5f 0a 2a 2a 20 73 75 63 63 65 73 73 2f 66 61 E_.** success/fa
1b58e 69 6c 75 72 65 20 63 6f 64 65 20 74 68 61 74 20 ilure code that
1b58f 64 65 73 63 72 69 62 65 73 20 74 68 65 20 72 65 describes the re
1b590 73 75 6c 74 20 6f 66 20 65 78 65 63 75 74 69 6e sult of executin
1b591 67 20 74 68 65 20 76 69 72 74 75 61 6c 0a 2a 2a g the virtual.**
1b592 20 6d 61 63 68 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 machine..**.**
1b593 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74 This routine set
1b594 73 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 s the error code
1b595 20 61 6e 64 20 73 74 72 69 6e 67 20 72 65 74 75 and string retu
1b596 72 6e 65 64 20 62 79 0a 2a 2a 20 73 71 6c 69 74 rned by.** sqlit
1b597 65 33 5f 65 72 72 63 6f 64 65 28 29 2c 20 73 71 e3_errcode(), sq
1b598 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 20 61 lite3_errmsg() a
1b599 6e 64 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 nd sqlite3_errms
1b59a 67 31 36 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 g16()..*/.SQLITE
1b59b 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
1b59c 5f 66 69 6e 61 6c 69 7a 65 28 73 71 6c 69 74 65 _finalize(sqlite
1b59d 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 3_stmt *pStmt){.
1b59e 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 66 28 20 int rc;. if(
1b59f 70 53 74 6d 74 3d 3d 30 20 29 7b 0a 20 20 20 20 pStmt==0 ){.
1b5a0 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
1b5a1 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 56 64 62 }else{. Vdb
1b5a2 65 20 2a 76 20 3d 20 28 56 64 62 65 2a 29 70 53 e *v = (Vdbe*)pS
1b5a3 74 6d 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 tmt;. sqlite3
1b5a4 20 2a 64 62 20 3d 20 76 2d 3e 64 62 3b 0a 23 69 *db = v->db;.#i
1b5a5 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 f SQLITE_THREADS
1b5a6 41 46 45 0a 20 20 20 20 73 71 6c 69 74 65 33 5f AFE. sqlite3_
1b5a7 6d 75 74 65 78 20 2a 6d 75 74 65 78 20 3d 20 76 mutex *mutex = v
1b5a8 2d 3e 64 62 2d 3e 6d 75 74 65 78 3b 0a 23 65 6e ->db->mutex;.#en
1b5a9 64 69 66 0a 20 20 20 20 73 71 6c 69 74 65 33 5f dif. sqlite3_
1b5aa 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 mutex_enter(mute
1b5ab 78 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c x);. rc = sql
1b5ac 69 74 65 33 56 64 62 65 46 69 6e 61 6c 69 7a 65 ite3VdbeFinalize
1b5ad 28 76 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 (v);. rc = sq
1b5ae 6c 69 74 65 33 41 70 69 45 78 69 74 28 64 62 2c lite3ApiExit(db,
1b5af 20 72 63 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 rc);. sqlite
1b5b0 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 3_mutex_leave(mu
1b5b1 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 tex);. }. retu
1b5b2 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
1b5b3 54 65 72 6d 69 6e 61 74 65 20 74 68 65 20 63 75 Terminate the cu
1b5b4 72 72 65 6e 74 20 65 78 65 63 75 74 69 6f 6e 20 rrent execution
1b5b5 6f 66 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d of an SQL statem
1b5b6 65 6e 74 20 61 6e 64 20 72 65 73 65 74 20 69 74 ent and reset it
1b5b7 0a 2a 2a 20 62 61 63 6b 20 74 6f 20 69 74 73 20 .** back to its
1b5b8 73 74 61 72 74 69 6e 67 20 73 74 61 74 65 20 73 starting state s
1b5b9 6f 20 74 68 61 74 20 69 74 20 63 61 6e 20 62 65 o that it can be
1b5ba 20 72 65 75 73 65 64 2e 20 41 20 73 75 63 63 65 reused. A succe
1b5bb 73 73 20 63 6f 64 65 20 66 72 6f 6d 0a 2a 2a 20 ss code from.**
1b5bc 74 68 65 20 70 72 69 6f 72 20 65 78 65 63 75 74 the prior execut
1b5bd 69 6f 6e 20 69 73 20 72 65 74 75 72 6e 65 64 2e ion is returned.
1b5be 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
1b5bf 69 6e 65 20 73 65 74 73 20 74 68 65 20 65 72 72 ine sets the err
1b5c0 6f 72 20 63 6f 64 65 20 61 6e 64 20 73 74 72 69 or code and stri
1b5c1 6e 67 20 72 65 74 75 72 6e 65 64 20 62 79 0a 2a ng returned by.*
1b5c2 2a 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 * sqlite3_errcod
1b5c3 65 28 29 2c 20 73 71 6c 69 74 65 33 5f 65 72 72 e(), sqlite3_err
1b5c4 6d 73 67 28 29 20 61 6e 64 20 73 71 6c 69 74 65 msg() and sqlite
1b5c5 33 5f 65 72 72 6d 73 67 31 36 28 29 2e 0a 2a 2f 3_errmsg16()..*/
1b5c6 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
1b5c7 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 73 71 sqlite3_reset(sq
1b5c8 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d lite3_stmt *pStm
1b5c9 74 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 t){. int rc;.
1b5ca 69 66 28 20 70 53 74 6d 74 3d 3d 30 20 29 7b 0a if( pStmt==0 ){.
1b5cb 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
1b5cc 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 OK;. }else{.
1b5cd 20 56 64 62 65 20 2a 76 20 3d 20 28 56 64 62 65 Vdbe *v = (Vdbe
1b5ce 2a 29 70 53 74 6d 74 3b 0a 20 20 20 20 73 71 6c *)pStmt;. sql
1b5cf 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
1b5d0 28 76 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a (v->db->mutex);.
1b5d1 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1b5d2 56 64 62 65 52 65 73 65 74 28 76 29 3b 0a 20 20 VdbeReset(v);.
1b5d3 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b sqlite3VdbeMak
1b5d4 65 52 65 61 64 79 28 76 2c 20 2d 31 2c 20 30 2c eReady(v, -1, 0,
1b5d5 20 30 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 0, 0, 0, 0);.
1b5d6 20 20 61 73 73 65 72 74 28 20 28 72 63 20 26 20 assert( (rc &
1b5d7 28 76 2d 3e 64 62 2d 3e 65 72 72 4d 61 73 6b 29 (v->db->errMask)
1b5d8 29 3d 3d 72 63 20 29 3b 0a 20 20 20 20 72 63 20 )==rc );. rc
1b5d9 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 = sqlite3ApiExit
1b5da 28 76 2d 3e 64 62 2c 20 72 63 29 3b 0a 20 20 20 (v->db, rc);.
1b5db 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
1b5dc 65 61 76 65 28 76 2d 3e 64 62 2d 3e 6d 75 74 65 eave(v->db->mute
1b5dd 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e x);. }. return
1b5de 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 rc;.}../*.** Se
1b5df 74 20 61 6c 6c 20 74 68 65 20 70 61 72 61 6d 65 t all the parame
1b5e0 74 65 72 73 20 69 6e 20 74 68 65 20 63 6f 6d 70 ters in the comp
1b5e1 69 6c 65 64 20 53 51 4c 20 73 74 61 74 65 6d 65 iled SQL stateme
1b5e2 6e 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53 nt to NULL..*/.S
1b5e3 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
1b5e4 6c 69 74 65 33 5f 63 6c 65 61 72 5f 62 69 6e 64 lite3_clear_bind
1b5e5 69 6e 67 73 28 73 71 6c 69 74 65 33 5f 73 74 6d ings(sqlite3_stm
1b5e6 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20 69 6e 74 t *pStmt){. int
1b5e7 20 69 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 i;. int rc = S
1b5e8 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 56 64 62 65 QLITE_OK;. Vdbe
1b5e9 20 2a 70 20 3d 20 28 56 64 62 65 2a 29 70 53 74 *p = (Vdbe*)pSt
1b5ea 6d 74 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 mt;.#if SQLITE_T
1b5eb 48 52 45 41 44 53 41 46 45 0a 20 20 73 71 6c 69 HREADSAFE. sqli
1b5ec 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 te3_mutex *mutex
1b5ed 20 3d 20 28 28 56 64 62 65 2a 29 70 53 74 6d 74 = ((Vdbe*)pStmt
1b5ee 29 2d 3e 64 62 2d 3e 6d 75 74 65 78 3b 0a 23 65 )->db->mutex;.#e
1b5ef 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33 5f 6d ndif. sqlite3_m
1b5f0 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 utex_enter(mutex
1b5f1 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c );. for(i=0; i<
1b5f2 70 2d 3e 6e 56 61 72 3b 20 69 2b 2b 29 7b 0a 20 p->nVar; i++){.
1b5f3 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 sqlite3VdbeMe
1b5f4 6d 52 65 6c 65 61 73 65 28 26 70 2d 3e 61 56 61 mRelease(&p->aVa
1b5f5 72 5b 69 5d 29 3b 0a 20 20 20 20 70 2d 3e 61 56 r[i]);. p->aV
1b5f6 61 72 5b 69 5d 2e 66 6c 61 67 73 20 3d 20 4d 45 ar[i].flags = ME
1b5f7 4d 5f 4e 75 6c 6c 3b 0a 20 20 7d 0a 20 20 73 71 M_Null;. }. sq
1b5f8 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
1b5f9 65 28 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 e(mutex);. retu
1b5fa 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 2a 2a 2a rn rc;.}.../****
1b5fb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b5fc 2a 2a 2a 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65 33 ******** sqlite3
1b5fd 5f 76 61 6c 75 65 5f 20 20 2a 2a 2a 2a 2a 2a 2a _value_ *******
1b5fe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b5ff 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 ********.** The
1b600 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e following routin
1b601 65 73 20 65 78 74 72 61 63 74 20 69 6e 66 6f 72 es extract infor
1b602 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 61 20 4d 65 mation from a Me
1b603 6d 20 6f 72 20 73 71 6c 69 74 65 33 5f 76 61 6c m or sqlite3_val
1b604 75 65 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e ue.** structure.
1b605 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 .*/.SQLITE_API c
1b606 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 onst void *sqlit
1b607 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 73 71 e3_value_blob(sq
1b608 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 lite3_value *pVa
1b609 6c 29 7b 0a 20 20 4d 65 6d 20 2a 70 20 3d 20 28 l){. Mem *p = (
1b60a 4d 65 6d 2a 29 70 56 61 6c 3b 0a 20 20 69 66 28 Mem*)pVal;. if(
1b60b 20 70 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d p->flags & (MEM
1b60c 5f 42 6c 6f 62 7c 4d 45 4d 5f 53 74 72 29 20 29 _Blob|MEM_Str) )
1b60d 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 {. sqlite3Vdb
1b60e 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62 28 70 eMemExpandBlob(p
1b60f 29 3b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20 );. p->flags
1b610 26 3d 20 7e 4d 45 4d 5f 53 74 72 3b 0a 20 20 20 &= ~MEM_Str;.
1b611 20 70 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d p->flags |= MEM
1b612 5f 42 6c 6f 62 3b 0a 20 20 20 20 72 65 74 75 72 _Blob;. retur
1b613 6e 20 70 2d 3e 7a 3b 0a 20 20 7d 65 6c 73 65 7b n p->z;. }else{
1b614 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 . return sqli
1b615 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 70 te3_value_text(p
1b616 56 61 6c 29 3b 0a 20 20 7d 0a 7d 0a 53 51 4c 49 Val);. }.}.SQLI
1b617 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
1b618 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 73 e3_value_bytes(s
1b619 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 qlite3_value *pV
1b61a 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 al){. return sq
1b61b 6c 69 74 65 33 56 61 6c 75 65 42 79 74 65 73 28 lite3ValueBytes(
1b61c 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 pVal, SQLITE_UTF
1b61d 38 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 8);.}.SQLITE_API
1b61e 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c int sqlite3_val
1b61f 75 65 5f 62 79 74 65 73 31 36 28 73 71 6c 69 74 ue_bytes16(sqlit
1b620 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b e3_value *pVal){
1b621 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 . return sqlite
1b622 33 56 61 6c 75 65 42 79 74 65 73 28 70 56 61 6c 3ValueBytes(pVal
1b623 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 , SQLITE_UTF16NA
1b624 54 49 56 45 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f TIVE);.}.SQLITE_
1b625 41 50 49 20 64 6f 75 62 6c 65 20 73 71 6c 69 74 API double sqlit
1b626 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28 e3_value_double(
1b627 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 sqlite3_value *p
1b628 56 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 Val){. return s
1b629 71 6c 69 74 65 33 56 64 62 65 52 65 61 6c 56 61 qlite3VdbeRealVa
1b62a 6c 75 65 28 28 4d 65 6d 2a 29 70 56 61 6c 29 3b lue((Mem*)pVal);
1b62b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e .}.SQLITE_API in
1b62c 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f t sqlite3_value_
1b62d 69 6e 74 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 int(sqlite3_valu
1b62e 65 20 2a 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 e *pVal){. retu
1b62f 72 6e 20 28 69 6e 74 29 73 71 6c 69 74 65 33 56 rn (int)sqlite3V
1b630 64 62 65 49 6e 74 56 61 6c 75 65 28 28 4d 65 6d dbeIntValue((Mem
1b631 2a 29 70 56 61 6c 29 3b 0a 7d 0a 53 51 4c 49 54 *)pVal);.}.SQLIT
1b632 45 5f 41 50 49 20 73 71 6c 69 74 65 5f 69 6e 74 E_API sqlite_int
1b633 36 34 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 64 sqlite3_value
1b634 5f 69 6e 74 36 34 28 73 71 6c 69 74 65 33 5f 76 _int64(sqlite3_v
1b635 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20 72 alue *pVal){. r
1b636 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56 64 62 eturn sqlite3Vdb
1b637 65 49 6e 74 56 61 6c 75 65 28 28 4d 65 6d 2a 29 eIntValue((Mem*)
1b638 70 56 61 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f pVal);.}.SQLITE_
1b639 41 50 49 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e API const unsign
1b63a 65 64 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 ed char *sqlite3
1b63b 5f 76 61 6c 75 65 5f 74 65 78 74 28 73 71 6c 69 _value_text(sqli
1b63c 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29 te3_value *pVal)
1b63d 7b 0a 20 20 72 65 74 75 72 6e 20 28 63 6f 6e 73 {. return (cons
1b63e 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 t unsigned char
1b63f 2a 29 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65 *)sqlite3ValueTe
1b640 78 74 28 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f xt(pVal, SQLITE_
1b641 55 54 46 38 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 UTF8);.}.#ifndef
1b642 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 SQLITE_OMIT_UTF
1b643 31 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 16.SQLITE_API co
1b644 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 nst void *sqlite
1b645 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 28 73 3_value_text16(s
1b646 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 20 70 56 qlite3_value* pV
1b647 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 al){. return sq
1b648 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74 28 70 lite3ValueText(p
1b649 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 Val, SQLITE_UTF1
1b64a 36 4e 41 54 49 56 45 29 3b 0a 7d 0a 53 51 4c 49 6NATIVE);.}.SQLI
1b64b 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 TE_API const voi
1b64c 64 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 d *sqlite3_value
1b64d 5f 74 65 78 74 31 36 62 65 28 73 71 6c 69 74 65 _text16be(sqlite
1b64e 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 3_value *pVal){.
1b64f 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 return sqlite3
1b650 56 61 6c 75 65 54 65 78 74 28 70 56 61 6c 2c 20 ValueText(pVal,
1b651 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 29 3b SQLITE_UTF16BE);
1b652 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f .}.SQLITE_API co
1b653 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 nst void *sqlite
1b654 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 6c 65 3_value_text16le
1b655 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a (sqlite3_value *
1b656 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 pVal){. return
1b657 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74 sqlite3ValueText
1b658 28 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54 (pVal, SQLITE_UT
1b659 46 31 36 4c 45 29 3b 0a 7d 0a 23 65 6e 64 69 66 F16LE);.}.#endif
1b65a 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f /* SQLITE_OMIT_
1b65b 55 54 46 31 36 20 2a 2f 0a 53 51 4c 49 54 45 5f UTF16 */.SQLITE_
1b65c 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
1b65d 76 61 6c 75 65 5f 74 79 70 65 28 73 71 6c 69 74 value_type(sqlit
1b65e 65 33 5f 76 61 6c 75 65 2a 20 70 56 61 6c 29 7b e3_value* pVal){
1b65f 0a 20 20 72 65 74 75 72 6e 20 70 56 61 6c 2d 3e . return pVal->
1b660 74 79 70 65 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a type;.}../******
1b661 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b662 2a 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65 33 5f 72 ****** sqlite3_r
1b663 65 73 75 6c 74 5f 20 20 2a 2a 2a 2a 2a 2a 2a 2a esult_ ********
1b664 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b665 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 66 *******.** The f
1b666 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 ollowing routine
1b667 73 20 61 72 65 20 75 73 65 64 20 62 79 20 75 73 s are used by us
1b668 65 72 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 er-defined funct
1b669 69 6f 6e 73 20 74 6f 20 73 70 65 63 69 66 79 0a ions to specify.
1b66a 2a 2a 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 ** the function
1b66b 72 65 73 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 result..**.** Th
1b66c 65 20 73 65 74 53 74 72 4f 72 45 72 72 6f 72 28 e setStrOrError(
1b66d 29 20 66 75 6e 74 69 6f 6e 20 63 61 6c 6c 73 20 ) funtion calls
1b66e 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 sqlite3VdbeMemSe
1b66f 74 53 74 72 28 29 20 74 6f 20 73 74 6f 72 65 20 tStr() to store
1b670 74 68 65 0a 2a 2a 20 72 65 73 75 6c 74 20 61 73 the.** result as
1b671 20 61 20 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f a string or blo
1b672 62 20 62 75 74 20 69 66 20 74 68 65 20 73 74 72 b but if the str
1b673 69 6e 67 20 6f 72 20 62 6c 6f 62 20 69 73 20 74 ing or blob is t
1b674 6f 6f 20 6c 61 72 67 65 2c 20 69 74 0a 2a 2a 20 oo large, it.**
1b675 74 68 65 6e 20 73 65 74 73 20 74 68 65 20 65 72 then sets the er
1b676 72 6f 72 20 63 6f 64 65 20 74 6f 20 53 51 4c 49 ror code to SQLI
1b677 54 45 5f 54 4f 4f 42 49 47 0a 2a 2f 0a 73 74 61 TE_TOOBIG.*/.sta
1b678 74 69 63 20 76 6f 69 64 20 73 65 74 52 65 73 75 tic void setResu
1b679 6c 74 53 74 72 4f 72 45 72 72 6f 72 28 0a 20 20 ltStrOrError(.
1b67a 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 sqlite3_context
1b67b 2a 70 43 74 78 2c 20 20 2f 2a 20 46 75 6e 63 74 *pCtx, /* Funct
1b67c 69 6f 6e 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 ion context */.
1b67d 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 const char *z,
1b67e 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 72 69 /* Stri
1b67f 6e 67 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 ng pointer */.
1b680 69 6e 74 20 6e 2c 20 20 20 20 20 20 20 20 20 20 int n,
1b681 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 /* Bytes
1b682 20 69 6e 20 73 74 72 69 6e 67 2c 20 6f 72 20 6e in string, or n
1b683 65 67 61 74 69 76 65 20 2a 2f 0a 20 20 75 38 20 egative */. u8
1b684 65 6e 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 enc,
1b685 20 20 20 20 20 2f 2a 20 45 6e 63 6f 64 69 6e 67 /* Encoding
1b686 20 6f 66 20 7a 2e 20 20 30 20 66 6f 72 20 42 4c of z. 0 for BL
1b687 4f 42 73 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a OBs */. void (*
1b688 78 44 65 6c 29 28 76 6f 69 64 2a 29 20 20 20 20 xDel)(void*)
1b689 20 2f 2a 20 44 65 73 74 72 75 63 74 6f 72 20 66 /* Destructor f
1b68a 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 unction */.){.
1b68b 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d if( sqlite3VdbeM
1b68c 65 6d 53 65 74 53 74 72 28 26 70 43 74 78 2d 3e emSetStr(&pCtx->
1b68d 73 2c 20 7a 2c 20 6e 2c 20 65 6e 63 2c 20 78 44 s, z, n, enc, xD
1b68e 65 6c 29 3d 3d 53 51 4c 49 54 45 5f 54 4f 4f 42 el)==SQLITE_TOOB
1b68f 49 47 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 IG ){. sqlite
1b690 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74 3_result_error_t
1b691 6f 6f 62 69 67 28 70 43 74 78 29 3b 0a 20 20 7d oobig(pCtx);. }
1b692 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f .}.SQLITE_API vo
1b693 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c id sqlite3_resul
1b694 74 5f 62 6c 6f 62 28 0a 20 20 73 71 6c 69 74 65 t_blob(. sqlite
1b695 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 3_context *pCtx,
1b696 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a . const void *
1b697 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c 20 0a 20 20 z, . int n, .
1b698 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 void (*xDel)(voi
1b699 64 20 2a 29 0a 29 7b 0a 20 20 61 73 73 65 72 74 d *).){. assert
1b69a 28 20 6e 3e 3d 30 20 29 3b 0a 20 20 61 73 73 65 ( n>=0 );. asse
1b69b 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
1b69c 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 x_held(pCtx->s.d
1b69d 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 b->mutex) );. s
1b69e 65 74 52 65 73 75 6c 74 53 74 72 4f 72 45 72 72 etResultStrOrErr
1b69f 6f 72 28 70 43 74 78 2c 20 7a 2c 20 6e 2c 20 30 or(pCtx, z, n, 0
1b6a0 2c 20 78 44 65 6c 29 3b 0a 7d 0a 53 51 4c 49 54 , xDel);.}.SQLIT
1b6a1 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 E_API void sqlit
1b6a2 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 e3_result_double
1b6a3 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 (sqlite3_context
1b6a4 20 2a 70 43 74 78 2c 20 64 6f 75 62 6c 65 20 72 *pCtx, double r
1b6a5 56 61 6c 29 7b 0a 20 20 61 73 73 65 72 74 28 20 Val){. assert(
1b6a6 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
1b6a7 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d ld(pCtx->s.db->m
1b6a8 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 utex) );. sqlit
1b6a9 65 33 56 64 62 65 4d 65 6d 53 65 74 44 6f 75 62 e3VdbeMemSetDoub
1b6aa 6c 65 28 26 70 43 74 78 2d 3e 73 2c 20 72 56 61 le(&pCtx->s, rVa
1b6ab 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 l);.}.SQLITE_API
1b6ac 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 void sqlite3_re
1b6ad 73 75 6c 74 5f 65 72 72 6f 72 28 73 71 6c 69 74 sult_error(sqlit
1b6ae 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 e3_context *pCtx
1b6af 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c , const char *z,
1b6b0 20 69 6e 74 20 6e 29 7b 0a 20 20 61 73 73 65 72 int n){. asser
1b6b1 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
1b6b2 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 _held(pCtx->s.db
1b6b3 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 43 ->mutex) );. pC
1b6b4 74 78 2d 3e 69 73 45 72 72 6f 72 20 3d 20 53 51 tx->isError = SQ
1b6b5 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 73 71 LITE_ERROR;. sq
1b6b6 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 lite3VdbeMemSetS
1b6b7 74 72 28 26 70 43 74 78 2d 3e 73 2c 20 7a 2c 20 tr(&pCtx->s, z,
1b6b8 6e 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 n, SQLITE_UTF8,
1b6b9 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 SQLITE_TRANSIENT
1b6ba 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c );.}.#ifndef SQL
1b6bb 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 ITE_OMIT_UTF16.S
1b6bc 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 QLITE_API void s
1b6bd 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 qlite3_result_er
1b6be 72 6f 72 31 36 28 73 71 6c 69 74 65 33 5f 63 6f ror16(sqlite3_co
1b6bf 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 63 6f 6e ntext *pCtx, con
1b6c0 73 74 20 76 6f 69 64 20 2a 7a 2c 20 69 6e 74 20 st void *z, int
1b6c1 6e 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 n){. assert( sq
1b6c2 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
1b6c3 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 (pCtx->s.db->mut
1b6c4 65 78 29 20 29 3b 0a 20 20 70 43 74 78 2d 3e 69 ex) );. pCtx->i
1b6c5 73 45 72 72 6f 72 20 3d 20 53 51 4c 49 54 45 5f sError = SQLITE_
1b6c6 45 52 52 4f 52 3b 0a 20 20 73 71 6c 69 74 65 33 ERROR;. sqlite3
1b6c7 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 26 70 VdbeMemSetStr(&p
1b6c8 43 74 78 2d 3e 73 2c 20 7a 2c 20 6e 2c 20 53 51 Ctx->s, z, n, SQ
1b6c9 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 LITE_UTF16NATIVE
1b6ca 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 , SQLITE_TRANSIE
1b6cb 4e 54 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 53 51 NT);.}.#endif.SQ
1b6cc 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 LITE_API void sq
1b6cd 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 lite3_result_int
1b6ce 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 (sqlite3_context
1b6cf 20 2a 70 43 74 78 2c 20 69 6e 74 20 69 56 61 6c *pCtx, int iVal
1b6d0 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c ){. assert( sql
1b6d1 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
1b6d2 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 pCtx->s.db->mute
1b6d3 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 x) );. sqlite3V
1b6d4 64 62 65 4d 65 6d 53 65 74 49 6e 74 36 34 28 26 dbeMemSetInt64(&
1b6d5 70 43 74 78 2d 3e 73 2c 20 28 69 36 34 29 69 56 pCtx->s, (i64)iV
1b6d6 61 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 al);.}.SQLITE_AP
1b6d7 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 I void sqlite3_r
1b6d8 65 73 75 6c 74 5f 69 6e 74 36 34 28 73 71 6c 69 esult_int64(sqli
1b6d9 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 te3_context *pCt
1b6da 78 2c 20 69 36 34 20 69 56 61 6c 29 7b 0a 20 20 x, i64 iVal){.
1b6db 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
1b6dc 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d mutex_held(pCtx-
1b6dd 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b >s.db->mutex) );
1b6de 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 . sqlite3VdbeMe
1b6df 6d 53 65 74 49 6e 74 36 34 28 26 70 43 74 78 2d mSetInt64(&pCtx-
1b6e0 3e 73 2c 20 69 56 61 6c 29 3b 0a 7d 0a 53 51 4c >s, iVal);.}.SQL
1b6e1 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c ITE_API void sql
1b6e2 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c ite3_result_null
1b6e3 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 (sqlite3_context
1b6e4 20 2a 70 43 74 78 29 7b 0a 20 20 61 73 73 65 72 *pCtx){. asser
1b6e5 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
1b6e6 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 _held(pCtx->s.db
1b6e7 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71 ->mutex) );. sq
1b6e8 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e lite3VdbeMemSetN
1b6e9 75 6c 6c 28 26 70 43 74 78 2d 3e 73 29 3b 0a 7d ull(&pCtx->s);.}
1b6ea 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 .SQLITE_API void
1b6eb 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
1b6ec 74 65 78 74 28 0a 20 20 73 71 6c 69 74 65 33 5f text(. sqlite3_
1b6ed 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 0a context *pCtx, .
1b6ee 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c const char *z,
1b6ef 20 0a 20 20 69 6e 74 20 6e 2c 0a 20 20 76 6f 69 . int n,. voi
1b6f0 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 20 2a d (*xDel)(void *
1b6f1 29 0a 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 ).){. assert( s
1b6f2 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
1b6f3 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 d(pCtx->s.db->mu
1b6f4 74 65 78 29 20 29 3b 0a 20 20 73 65 74 52 65 73 tex) );. setRes
1b6f5 75 6c 74 53 74 72 4f 72 45 72 72 6f 72 28 70 43 ultStrOrError(pC
1b6f6 74 78 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 tx, z, n, SQLITE
1b6f7 5f 55 54 46 38 2c 20 78 44 65 6c 29 3b 0a 7d 0a _UTF8, xDel);.}.
1b6f8 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1b6f9 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 MIT_UTF16.SQLITE
1b6fa 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 _API void sqlite
1b6fb 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 28 3_result_text16(
1b6fc 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 . sqlite3_conte
1b6fd 78 74 20 2a 70 43 74 78 2c 20 0a 20 20 63 6f 6e xt *pCtx, . con
1b6fe 73 74 20 76 6f 69 64 20 2a 7a 2c 20 0a 20 20 69 st void *z, . i
1b6ff 6e 74 20 6e 2c 20 0a 20 20 76 6f 69 64 20 28 2a nt n, . void (*
1b700 78 44 65 6c 29 28 76 6f 69 64 20 2a 29 0a 29 7b xDel)(void *).){
1b701 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
1b702 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 e3_mutex_held(pC
1b703 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 tx->s.db->mutex)
1b704 20 29 3b 0a 20 20 73 65 74 52 65 73 75 6c 74 53 );. setResultS
1b705 74 72 4f 72 45 72 72 6f 72 28 70 43 74 78 2c 20 trOrError(pCtx,
1b706 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55 54 46 z, n, SQLITE_UTF
1b707 31 36 4e 41 54 49 56 45 2c 20 78 44 65 6c 29 3b 16NATIVE, xDel);
1b708 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f .}.SQLITE_API vo
1b709 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c id sqlite3_resul
1b70a 74 5f 74 65 78 74 31 36 62 65 28 0a 20 20 73 71 t_text16be(. sq
1b70b 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 lite3_context *p
1b70c 43 74 78 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f Ctx, . const vo
1b70d 69 64 20 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c id *z, . int n,
1b70e 20 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 . void (*xDel)
1b70f 28 76 6f 69 64 20 2a 29 0a 29 7b 0a 20 20 61 73 (void *).){. as
1b710 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
1b711 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 tex_held(pCtx->s
1b712 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 .db->mutex) );.
1b713 20 73 65 74 52 65 73 75 6c 74 53 74 72 4f 72 45 setResultStrOrE
1b714 72 72 6f 72 28 70 43 74 78 2c 20 7a 2c 20 6e 2c rror(pCtx, z, n,
1b715 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 2c SQLITE_UTF16BE,
1b716 20 78 44 65 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 xDel);.}.SQLITE
1b717 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 _API void sqlite
1b718 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 6c 3_result_text16l
1b719 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e e(. sqlite3_con
1b71a 74 65 78 74 20 2a 70 43 74 78 2c 20 0a 20 20 63 text *pCtx, . c
1b71b 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 2c 20 0a 20 onst void *z, .
1b71c 20 69 6e 74 20 6e 2c 20 0a 20 20 76 6f 69 64 20 int n, . void
1b71d 28 2a 78 44 65 6c 29 28 76 6f 69 64 20 2a 29 0a (*xDel)(void *).
1b71e 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c ){. assert( sql
1b71f 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
1b720 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 pCtx->s.db->mute
1b721 78 29 20 29 3b 0a 20 20 73 65 74 52 65 73 75 6c x) );. setResul
1b722 74 53 74 72 4f 72 45 72 72 6f 72 28 70 43 74 78 tStrOrError(pCtx
1b723 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55 , z, n, SQLITE_U
1b724 54 46 31 36 4c 45 2c 20 78 44 65 6c 29 3b 0a 7d TF16LE, xDel);.}
1b725 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
1b726 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a E_OMIT_UTF16 */.
1b727 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 SQLITE_API void
1b728 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 sqlite3_result_v
1b729 61 6c 75 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e alue(sqlite3_con
1b72a 74 65 78 74 20 2a 70 43 74 78 2c 20 73 71 6c 69 text *pCtx, sqli
1b72b 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 75 te3_value *pValu
1b72c 65 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 e){. assert( sq
1b72d 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
1b72e 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 (pCtx->s.db->mut
1b72f 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 ex) );. sqlite3
1b730 56 64 62 65 4d 65 6d 43 6f 70 79 28 26 70 43 74 VdbeMemCopy(&pCt
1b731 78 2d 3e 73 2c 20 70 56 61 6c 75 65 29 3b 0a 7d x->s, pValue);.}
1b732 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 .SQLITE_API void
1b733 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
1b734 7a 65 72 6f 62 6c 6f 62 28 73 71 6c 69 74 65 33 zeroblob(sqlite3
1b735 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 _context *pCtx,
1b736 69 6e 74 20 6e 29 7b 0a 20 20 61 73 73 65 72 74 int n){. assert
1b737 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
1b738 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d held(pCtx->s.db-
1b739 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c >mutex) );. sql
1b73a 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 5a 65 ite3VdbeMemSetZe
1b73b 72 6f 42 6c 6f 62 28 26 70 43 74 78 2d 3e 73 2c roBlob(&pCtx->s,
1b73c 20 6e 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 n);.}.SQLITE_AP
1b73d 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 I void sqlite3_r
1b73e 65 73 75 6c 74 5f 65 72 72 6f 72 5f 63 6f 64 65 esult_error_code
1b73f 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 (sqlite3_context
1b740 20 2a 70 43 74 78 2c 20 69 6e 74 20 65 72 72 43 *pCtx, int errC
1b741 6f 64 65 29 7b 0a 20 20 70 43 74 78 2d 3e 69 73 ode){. pCtx->is
1b742 45 72 72 6f 72 20 3d 20 65 72 72 43 6f 64 65 3b Error = errCode;
1b743 0a 20 20 69 66 28 20 70 43 74 78 2d 3e 73 2e 66 . if( pCtx->s.f
1b744 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 lags & MEM_Null
1b745 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 ){. sqlite3Vd
1b746 62 65 4d 65 6d 53 65 74 53 74 72 28 26 70 43 74 beMemSetStr(&pCt
1b747 78 2d 3e 73 2c 20 73 71 6c 69 74 65 33 45 72 72 x->s, sqlite3Err
1b748 53 74 72 28 65 72 72 43 6f 64 65 29 2c 20 2d 31 Str(errCode), -1
1b749 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 , .
1b74a 20 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 SQLI
1b74b 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f TE_UTF8, SQLITE_
1b74c 53 54 41 54 49 43 29 3b 0a 20 20 7d 0a 7d 0a 0a STATIC);. }.}..
1b74d 2f 2a 20 46 6f 72 63 65 20 61 6e 20 53 51 4c 49 /* Force an SQLI
1b74e 54 45 5f 54 4f 4f 42 49 47 20 65 72 72 6f 72 2e TE_TOOBIG error.
1b74f 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 */.SQLITE_API v
1b750 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 oid sqlite3_resu
1b751 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67 28 lt_error_toobig(
1b752 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 sqlite3_context
1b753 2a 70 43 74 78 29 7b 0a 20 20 61 73 73 65 72 74 *pCtx){. assert
1b754 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
1b755 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d held(pCtx->s.db-
1b756 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 43 74 >mutex) );. pCt
1b757 78 2d 3e 69 73 45 72 72 6f 72 20 3d 20 53 51 4c x->isError = SQL
1b758 49 54 45 5f 54 4f 4f 42 49 47 3b 0a 20 20 73 71 ITE_TOOBIG;. sq
1b759 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 lite3VdbeMemSetS
1b75a 74 72 28 26 70 43 74 78 2d 3e 73 2c 20 22 73 74 tr(&pCtx->s, "st
1b75b 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 74 6f 6f ring or blob too
1b75c 20 62 69 67 22 2c 20 2d 31 2c 20 0a 20 20 20 20 big", -1, .
1b75d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b75e 20 20 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 SQLITE_UTF8,
1b75f 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a SQLITE_STATIC);.
1b760 7d 0a 0a 2f 2a 20 41 6e 20 53 51 4c 49 54 45 5f }../* An SQLITE_
1b761 4e 4f 4d 45 4d 20 65 72 72 6f 72 2e 20 2a 2f 0a NOMEM error. */.
1b762 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 SQLITE_API void
1b763 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 sqlite3_result_e
1b764 72 72 6f 72 5f 6e 6f 6d 65 6d 28 73 71 6c 69 74 rror_nomem(sqlit
1b765 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 e3_context *pCtx
1b766 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c ){. assert( sql
1b767 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
1b768 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 pCtx->s.db->mute
1b769 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 x) );. sqlite3V
1b76a 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 26 70 dbeMemSetNull(&p
1b76b 43 74 78 2d 3e 73 29 3b 0a 20 20 70 43 74 78 2d Ctx->s);. pCtx-
1b76c 3e 69 73 45 72 72 6f 72 20 3d 20 53 51 4c 49 54 >isError = SQLIT
1b76d 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 70 43 74 78 2d E_NOMEM;. pCtx-
1b76e 3e 73 2e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 >s.db->mallocFai
1b76f 6c 65 64 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a led = 1;.}../*.*
1b770 2a 20 45 78 65 63 75 74 65 20 74 68 65 20 73 74 * Execute the st
1b771 61 74 65 6d 65 6e 74 20 70 53 74 6d 74 2c 20 65 atement pStmt, e
1b772 69 74 68 65 72 20 75 6e 74 69 6c 20 61 20 72 6f ither until a ro
1b773 77 20 6f 66 20 64 61 74 61 20 69 73 20 72 65 61 w of data is rea
1b774 64 79 2c 20 74 68 65 0a 2a 2a 20 73 74 61 74 65 dy, the.** state
1b775 6d 65 6e 74 20 69 73 20 63 6f 6d 70 6c 65 74 65 ment is complete
1b776 6c 79 20 65 78 65 63 75 74 65 64 20 6f 72 20 61 ly executed or a
1b777 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 0a n error occurs..
1b778 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
1b779 6e 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 ne implements th
1b77a 65 20 62 75 6c 6b 20 6f 66 20 74 68 65 20 6c 6f e bulk of the lo
1b77b 67 69 63 20 62 65 68 69 6e 64 20 74 68 65 20 73 gic behind the s
1b77c 71 6c 69 74 65 5f 73 74 65 70 28 29 0a 2a 2a 20 qlite_step().**
1b77d 41 50 49 2e 20 20 54 68 65 20 6f 6e 6c 79 20 74 API. The only t
1b77e 68 69 6e 67 20 6f 6d 69 74 74 65 64 20 69 73 20 hing omitted is
1b77f 74 68 65 20 61 75 74 6f 6d 61 74 69 63 20 72 65 the automatic re
1b780 63 6f 6d 70 69 6c 65 20 69 66 20 61 20 0a 2a 2a compile if a .**
1b781 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 20 68 schema change h
1b782 61 73 20 6f 63 63 75 72 72 65 64 2e 20 20 54 68 as occurred. Th
1b783 61 74 20 64 65 74 61 69 6c 20 69 73 20 68 61 6e at detail is han
1b784 64 6c 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 6f dled by the.** o
1b785 75 74 65 72 20 73 71 6c 69 74 65 33 5f 73 74 65 uter sqlite3_ste
1b786 70 28 29 20 77 72 61 70 70 65 72 20 70 72 6f 63 p() wrapper proc
1b787 65 64 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 edure..*/.static
1b788 20 69 6e 74 20 73 71 6c 69 74 65 33 53 74 65 70 int sqlite3Step
1b789 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 73 71 6c (Vdbe *p){. sql
1b78a 69 74 65 33 20 2a 64 62 3b 0a 20 20 69 6e 74 20 ite3 *db;. int
1b78b 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 70 29 rc;.. assert(p)
1b78c 3b 0a 20 20 69 66 28 20 70 2d 3e 6d 61 67 69 63 ;. if( p->magic
1b78d 21 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e !=VDBE_MAGIC_RUN
1b78e 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
1b78f 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 QLITE_MISUSE;.
1b790 7d 0a 0a 20 20 2f 2a 20 41 73 73 65 72 74 20 74 }.. /* Assert t
1b791 68 61 74 20 6d 61 6c 6c 6f 63 28 29 20 68 61 73 hat malloc() has
1b792 20 6e 6f 74 20 66 61 69 6c 65 64 20 2a 2f 0a 20 not failed */.
1b793 20 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 69 db = p->db;. i
1b794 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 f( db->mallocFai
1b795 6c 65 64 20 29 7b 0a 20 20 20 20 72 65 74 75 72 led ){. retur
1b796 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a n SQLITE_NOMEM;.
1b797 20 20 7d 0a 0a 20 20 69 66 28 20 70 2d 3e 70 63 }.. if( p->pc
1b798 3c 3d 30 20 26 26 20 70 2d 3e 65 78 70 69 72 65 <=0 && p->expire
1b799 64 20 29 7b 0a 20 20 20 20 69 66 28 20 41 4c 57 d ){. if( ALW
1b79a 41 59 53 28 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 AYS(p->rc==SQLIT
1b79b 45 5f 4f 4b 29 20 29 7b 0a 20 20 20 20 20 20 70 E_OK) ){. p
1b79c 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 53 43 ->rc = SQLITE_SC
1b79d 48 45 4d 41 3b 0a 20 20 20 20 7d 0a 20 20 20 20 HEMA;. }.
1b79e 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f rc = SQLITE_ERRO
1b79f 52 3b 0a 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f R;. goto end_
1b7a0 6f 66 5f 73 74 65 70 3b 0a 20 20 7d 0a 20 20 69 of_step;. }. i
1b7a1 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 f( sqlite3Safety
1b7a2 4f 6e 28 64 62 29 20 29 7b 0a 20 20 20 20 70 2d On(db) ){. p-
1b7a3 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4d 49 53 >rc = SQLITE_MIS
1b7a4 55 53 45 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 USE;. return
1b7a5 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 SQLITE_MISUSE;.
1b7a6 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70 63 3c 30 }. if( p->pc<0
1b7a7 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 ){. /* If th
1b7a8 65 72 65 20 61 72 65 20 6e 6f 20 6f 74 68 65 72 ere are no other
1b7a9 20 73 74 61 74 65 6d 65 6e 74 73 20 63 75 72 72 statements curr
1b7aa 65 6e 74 6c 79 20 72 75 6e 6e 69 6e 67 2c 20 74 ently running, t
1b7ab 68 65 6e 0a 20 20 20 20 2a 2a 20 72 65 73 65 74 hen. ** reset
1b7ac 20 74 68 65 20 69 6e 74 65 72 72 75 70 74 20 66 the interrupt f
1b7ad 6c 61 67 2e 20 20 54 68 69 73 20 70 72 65 76 65 lag. This preve
1b7ae 6e 74 73 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 nts a call to sq
1b7af 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 0a lite3_interrupt.
1b7b0 20 20 20 20 2a 2a 20 66 72 6f 6d 20 69 6e 74 65 ** from inte
1b7b1 72 72 75 70 74 69 6e 67 20 61 20 73 74 61 74 65 rrupting a state
1b7b2 6d 65 6e 74 20 74 68 61 74 20 68 61 73 20 6e 6f ment that has no
1b7b3 74 20 79 65 74 20 73 74 61 72 74 65 64 2e 0a 20 t yet started..
1b7b4 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 64 62 */. if( db
1b7b5 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 3d ->activeVdbeCnt=
1b7b6 3d 30 20 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e =0 ){. db->
1b7b7 75 31 2e 69 73 49 6e 74 65 72 72 75 70 74 65 64 u1.isInterrupted
1b7b8 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 = 0;. }..
1b7b9 20 61 73 73 65 72 74 28 20 64 62 2d 3e 77 72 69 assert( db->wri
1b7ba 74 65 56 64 62 65 43 6e 74 3e 30 20 7c 7c 20 64 teVdbeCnt>0 || d
1b7bb 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 3d 3d 30 b->autoCommit==0
1b7bc 20 7c 7c 20 64 62 2d 3e 6e 44 65 66 65 72 72 65 || db->nDeferre
1b7bd 64 43 6f 6e 73 3d 3d 30 20 29 3b 0a 0a 23 69 66 dCons==0 );..#if
1b7be 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1b7bf 5f 54 52 41 43 45 0a 20 20 20 20 69 66 28 20 64 _TRACE. if( d
1b7c0 62 2d 3e 78 50 72 6f 66 69 6c 65 20 26 26 20 21 b->xProfile && !
1b7c1 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29 7b db->init.busy ){
1b7c2 0a 20 20 20 20 20 20 64 6f 75 62 6c 65 20 72 4e . double rN
1b7c3 6f 77 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ow;. sqlite
1b7c4 33 4f 73 43 75 72 72 65 6e 74 54 69 6d 65 28 64 3OsCurrentTime(d
1b7c5 62 2d 3e 70 56 66 73 2c 20 26 72 4e 6f 77 29 3b b->pVfs, &rNow);
1b7c6 0a 20 20 20 20 20 20 70 2d 3e 73 74 61 72 74 54 . p->startT
1b7c7 69 6d 65 20 3d 20 28 75 36 34 29 28 28 72 4e 6f ime = (u64)((rNo
1b7c8 77 20 2d 20 28 69 6e 74 29 72 4e 6f 77 29 2a 33 w - (int)rNow)*3
1b7c9 36 30 30 2e 30 2a 32 34 2e 30 2a 31 30 30 30 30 600.0*24.0*10000
1b7ca 30 30 30 30 30 2e 30 29 3b 0a 20 20 20 20 7d 0a 00000.0);. }.
1b7cb 23 65 6e 64 69 66 0a 0a 20 20 20 20 64 62 2d 3e #endif.. db->
1b7cc 61 63 74 69 76 65 56 64 62 65 43 6e 74 2b 2b 3b activeVdbeCnt++;
1b7cd 0a 20 20 20 20 69 66 28 20 70 2d 3e 72 65 61 64 . if( p->read
1b7ce 4f 6e 6c 79 3d 3d 30 20 29 20 64 62 2d 3e 77 72 Only==0 ) db->wr
1b7cf 69 74 65 56 64 62 65 43 6e 74 2b 2b 3b 0a 20 20 iteVdbeCnt++;.
1b7d0 20 20 70 2d 3e 70 63 20 3d 20 30 3b 0a 20 20 7d p->pc = 0;. }
1b7d1 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1b7d2 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 0a 20 20 69 OMIT_EXPLAIN. i
1b7d3 66 28 20 70 2d 3e 65 78 70 6c 61 69 6e 20 29 7b f( p->explain ){
1b7d4 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
1b7d5 33 56 64 62 65 4c 69 73 74 28 70 29 3b 0a 20 20 3VdbeList(p);.
1b7d6 7d 65 6c 73 65 0a 23 65 6e 64 69 66 20 2f 2a 20 }else.#endif /*
1b7d7 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c SQLITE_OMIT_EXPL
1b7d8 41 49 4e 20 2a 2f 0a 20 20 7b 0a 20 20 20 20 72 AIN */. {. r
1b7d9 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 45 c = sqlite3VdbeE
1b7da 78 65 63 28 70 29 3b 0a 20 20 7d 0a 0a 20 20 69 xec(p);. }.. i
1b7db 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 f( sqlite3Safety
1b7dc 4f 66 66 28 64 62 29 20 29 7b 0a 20 20 20 20 72 Off(db) ){. r
1b7dd 63 20 3d 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 c = SQLITE_MISUS
1b7de 45 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 E;. }..#ifndef
1b7df 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 SQLITE_OMIT_TRAC
1b7e0 45 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 E. /* Invoke th
1b7e1 65 20 70 72 6f 66 69 6c 65 20 63 61 6c 6c 62 61 e profile callba
1b7e2 63 6b 20 69 66 20 74 68 65 72 65 20 69 73 20 6f ck if there is o
1b7e3 6e 65 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 63 ne. */. if( rc
1b7e4 21 3d 53 51 4c 49 54 45 5f 52 4f 57 20 26 26 20 !=SQLITE_ROW &&
1b7e5 64 62 2d 3e 78 50 72 6f 66 69 6c 65 20 26 26 20 db->xProfile &&
1b7e6 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 26 !db->init.busy &
1b7e7 26 20 70 2d 3e 7a 53 71 6c 20 29 7b 0a 20 20 20 & p->zSql ){.
1b7e8 20 64 6f 75 62 6c 65 20 72 4e 6f 77 3b 0a 20 20 double rNow;.
1b7e9 20 20 75 36 34 20 65 6c 61 70 73 65 54 69 6d 65 u64 elapseTime
1b7ea 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 ;.. sqlite3Os
1b7eb 43 75 72 72 65 6e 74 54 69 6d 65 28 64 62 2d 3e CurrentTime(db->
1b7ec 70 56 66 73 2c 20 26 72 4e 6f 77 29 3b 0a 20 20 pVfs, &rNow);.
1b7ed 20 20 65 6c 61 70 73 65 54 69 6d 65 20 3d 20 28 elapseTime = (
1b7ee 75 36 34 29 28 28 72 4e 6f 77 20 2d 20 28 69 6e u64)((rNow - (in
1b7ef 74 29 72 4e 6f 77 29 2a 33 36 30 30 2e 30 2a 32 t)rNow)*3600.0*2
1b7f0 34 2e 30 2a 31 30 30 30 30 30 30 30 30 30 2e 30 4.0*1000000000.0
1b7f1 29 3b 0a 20 20 20 20 65 6c 61 70 73 65 54 69 6d );. elapseTim
1b7f2 65 20 2d 3d 20 70 2d 3e 73 74 61 72 74 54 69 6d e -= p->startTim
1b7f3 65 3b 0a 20 20 20 20 64 62 2d 3e 78 50 72 6f 66 e;. db->xProf
1b7f4 69 6c 65 28 64 62 2d 3e 70 50 72 6f 66 69 6c 65 ile(db->pProfile
1b7f5 41 72 67 2c 20 70 2d 3e 7a 53 71 6c 2c 20 65 6c Arg, p->zSql, el
1b7f6 61 70 73 65 54 69 6d 65 29 3b 0a 20 20 7d 0a 23 apseTime);. }.#
1b7f7 65 6e 64 69 66 0a 0a 20 20 64 62 2d 3e 65 72 72 endif.. db->err
1b7f8 43 6f 64 65 20 3d 20 72 63 3b 0a 20 20 69 66 28 Code = rc;. if(
1b7f9 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3d 3d 73 SQLITE_NOMEM==s
1b7fa 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 70 2d qlite3ApiExit(p-
1b7fb 3e 64 62 2c 20 70 2d 3e 72 63 29 20 29 7b 0a 20 >db, p->rc) ){.
1b7fc 20 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 p->rc = SQLIT
1b7fd 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 65 6e 64 E_NOMEM;. }.end
1b7fe 5f 6f 66 5f 73 74 65 70 3a 0a 20 20 2f 2a 20 41 _of_step:. /* A
1b7ff 74 20 74 68 69 73 20 70 6f 69 6e 74 20 6c 6f 63 t this point loc
1b800 61 6c 20 76 61 72 69 61 62 6c 65 20 72 63 20 68 al variable rc h
1b801 6f 6c 64 73 20 74 68 65 20 76 61 6c 75 65 20 74 olds the value t
1b802 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 0a 20 hat should be .
1b803 20 2a 2a 20 72 65 74 75 72 6e 65 64 20 69 66 20 ** returned if
1b804 74 68 69 73 20 73 74 61 74 65 6d 65 6e 74 20 77 this statement w
1b805 61 73 20 63 6f 6d 70 69 6c 65 64 20 75 73 69 6e as compiled usin
1b806 67 20 74 68 65 20 6c 65 67 61 63 79 20 0a 20 20 g the legacy .
1b807 2a 2a 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 ** sqlite3_prepa
1b808 72 65 28 29 20 69 6e 74 65 72 66 61 63 65 2e 20 re() interface.
1b809 41 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 According to the
1b80a 20 64 6f 63 73 2c 20 74 68 69 73 20 63 61 6e 20 docs, this can
1b80b 6f 6e 6c 79 0a 20 20 2a 2a 20 62 65 20 6f 6e 65 only. ** be one
1b80c 20 6f 66 20 74 68 65 20 76 61 6c 75 65 73 20 69 of the values i
1b80d 6e 20 74 68 65 20 66 69 72 73 74 20 61 73 73 65 n the first asse
1b80e 72 74 28 29 20 62 65 6c 6f 77 2e 20 56 61 72 69 rt() below. Vari
1b80f 61 62 6c 65 20 70 2d 3e 72 63 20 0a 20 20 2a 2a able p->rc . **
1b810 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 76 61 contains the va
1b811 6c 75 65 20 74 68 61 74 20 77 6f 75 6c 64 20 62 lue that would b
1b812 65 20 72 65 74 75 72 6e 65 64 20 69 66 20 73 71 e returned if sq
1b813 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 lite3_finalize()
1b814 20 0a 20 20 2a 2a 20 77 65 72 65 20 63 61 6c 6c . ** were call
1b815 65 64 20 6f 6e 20 73 74 61 74 65 6d 65 6e 74 20 ed on statement
1b816 70 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 p.. */. assert
1b817 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 ( rc==SQLITE_ROW
1b818 20 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f || rc==SQLITE_
1b819 44 4f 4e 45 20 20 20 7c 7c 20 72 63 3d 3d 53 51 DONE || rc==SQ
1b81a 4c 49 54 45 5f 45 52 52 4f 52 20 0a 20 20 20 20 LITE_ERROR .
1b81b 20 20 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 || rc==SQLITE
1b81c 5f 42 55 53 59 20 7c 7c 20 72 63 3d 3d 53 51 4c _BUSY || rc==SQL
1b81d 49 54 45 5f 4d 49 53 55 53 45 0a 20 20 29 3b 0a ITE_MISUSE. );.
1b81e 20 20 61 73 73 65 72 74 28 20 70 2d 3e 72 63 21 assert( p->rc!
1b81f 3d 53 51 4c 49 54 45 5f 52 4f 57 20 26 26 20 70 =SQLITE_ROW && p
1b820 2d 3e 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e ->rc!=SQLITE_DON
1b821 45 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 69 73 E );. if( p->is
1b822 50 72 65 70 61 72 65 56 32 20 26 26 20 72 63 21 PrepareV2 && rc!
1b823 3d 53 51 4c 49 54 45 5f 52 4f 57 20 26 26 20 72 =SQLITE_ROW && r
1b824 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 c!=SQLITE_DONE )
1b825 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 {. /* If this
1b826 20 73 74 61 74 65 6d 65 6e 74 20 77 61 73 20 70 statement was p
1b827 72 65 70 61 72 65 64 20 75 73 69 6e 67 20 73 71 repared using sq
1b828 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 lite3_prepare_v2
1b829 28 29 2c 20 61 6e 64 20 61 6e 0a 20 20 20 20 2a (), and an. *
1b82a 2a 20 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75 * error has occu
1b82b 72 65 64 2c 20 74 68 65 6e 20 72 65 74 75 72 6e red, then return
1b82c 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 the error code
1b82d 69 6e 20 70 2d 3e 72 63 20 74 6f 20 74 68 65 0a in p->rc to the.
1b82e 20 20 20 20 2a 2a 20 63 61 6c 6c 65 72 2e 20 53 ** caller. S
1b82f 65 74 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 et the error cod
1b830 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 e in the databas
1b831 65 20 68 61 6e 64 6c 65 20 74 6f 20 74 68 65 20 e handle to the
1b832 73 61 6d 65 20 76 61 6c 75 65 2e 0a 20 20 20 20 same value..
1b833 2a 2f 20 0a 20 20 20 20 72 63 20 3d 20 64 62 2d */ . rc = db-
1b834 3e 65 72 72 43 6f 64 65 20 3d 20 70 2d 3e 72 63 >errCode = p->rc
1b835 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 ;. }. return (
1b836 72 63 26 64 62 2d 3e 65 72 72 4d 61 73 6b 29 3b rc&db->errMask);
1b837 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 .}../*.** This i
1b838 73 20 74 68 65 20 74 6f 70 2d 6c 65 76 65 6c 20 s the top-level
1b839 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f implementation o
1b83a 66 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 f sqlite3_step()
1b83b 2e 20 20 43 61 6c 6c 0a 2a 2a 20 73 71 6c 69 74 . Call.** sqlit
1b83c 65 33 53 74 65 70 28 29 20 74 6f 20 64 6f 20 6d e3Step() to do m
1b83d 6f 73 74 20 6f 66 20 74 68 65 20 77 6f 72 6b 2e ost of the work.
1b83e 20 20 49 66 20 61 20 73 63 68 65 6d 61 20 65 72 If a schema er
1b83f 72 6f 72 20 6f 63 63 75 72 73 2c 0a 2a 2a 20 63 ror occurs,.** c
1b840 61 6c 6c 20 73 71 6c 69 74 65 33 52 65 70 72 65 all sqlite3Repre
1b841 70 61 72 65 28 29 20 61 6e 64 20 74 72 79 20 61 pare() and try a
1b842 67 61 69 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f gain..*/.SQLITE_
1b843 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
1b844 73 74 65 70 28 73 71 6c 69 74 65 33 5f 73 74 6d step(sqlite3_stm
1b845 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20 69 6e 74 t *pStmt){. int
1b846 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4d 49 53 rc = SQLITE_MIS
1b847 55 53 45 3b 0a 20 20 69 66 28 20 70 53 74 6d 74 USE;. if( pStmt
1b848 20 29 7b 0a 20 20 20 20 69 6e 74 20 63 6e 74 20 ){. int cnt
1b849 3d 20 30 3b 0a 20 20 20 20 56 64 62 65 20 2a 76 = 0;. Vdbe *v
1b84a 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d 74 3b = (Vdbe*)pStmt;
1b84b 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 . sqlite3 *db
1b84c 20 3d 20 76 2d 3e 64 62 3b 0a 20 20 20 20 73 71 = v->db;. sq
1b84d 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 lite3_mutex_ente
1b84e 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 r(db->mutex);.
1b84f 20 20 77 68 69 6c 65 28 20 28 72 63 20 3d 20 73 while( (rc = s
1b850 71 6c 69 74 65 33 53 74 65 70 28 76 29 29 3d 3d qlite3Step(v))==
1b851 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 0a 20 20 SQLITE_SCHEMA.
1b852 20 20 20 20 20 20 20 20 20 26 26 20 63 6e 74 2b && cnt+
1b853 2b 20 3c 20 35 0a 20 20 20 20 20 20 20 20 20 20 + < 5.
1b854 20 26 26 20 28 72 63 20 3d 20 73 71 6c 69 74 65 && (rc = sqlite
1b855 33 52 65 70 72 65 70 61 72 65 28 76 29 29 3d 3d 3Reprepare(v))==
1b856 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
1b857 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 sqlite3_reset
1b858 28 70 53 74 6d 74 29 3b 0a 20 20 20 20 20 20 76 (pStmt);. v
1b859 2d 3e 65 78 70 69 72 65 64 20 3d 20 30 3b 0a 20 ->expired = 0;.
1b85a 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d }. if( rc=
1b85b 3d 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 20 26 =SQLITE_SCHEMA &
1b85c 26 20 41 4c 57 41 59 53 28 76 2d 3e 69 73 50 72 & ALWAYS(v->isPr
1b85d 65 70 61 72 65 56 32 29 20 26 26 20 41 4c 57 41 epareV2) && ALWA
1b85e 59 53 28 64 62 2d 3e 70 45 72 72 29 20 29 7b 0a YS(db->pErr) ){.
1b85f 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 63 61 /* This ca
1b860 73 65 20 6f 63 63 75 72 73 20 61 66 74 65 72 20 se occurs after
1b861 66 61 69 6c 69 6e 67 20 74 6f 20 72 65 63 6f 6d failing to recom
1b862 70 69 6c 65 20 61 6e 20 73 71 6c 20 73 74 61 74 pile an sql stat
1b863 65 6d 65 6e 74 2e 20 0a 20 20 20 20 20 20 2a 2a ement. . **
1b864 20 54 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 The error messa
1b865 67 65 20 66 72 6f 6d 20 74 68 65 20 53 51 4c 20 ge from the SQL
1b866 63 6f 6d 70 69 6c 65 72 20 68 61 73 20 61 6c 72 compiler has alr
1b867 65 61 64 79 20 62 65 65 6e 20 6c 6f 61 64 65 64 eady been loaded
1b868 20 0a 20 20 20 20 20 20 2a 2a 20 69 6e 74 6f 20 . ** into
1b869 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61 6e the database han
1b86a 64 6c 65 2e 20 54 68 69 73 20 62 6c 6f 63 6b 20 dle. This block
1b86b 63 6f 70 69 65 73 20 74 68 65 20 65 72 72 6f 72 copies the error
1b86c 20 6d 65 73 73 61 67 65 20 0a 20 20 20 20 20 20 message .
1b86d 2a 2a 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 ** from the data
1b86e 62 61 73 65 20 68 61 6e 64 6c 65 20 69 6e 74 6f base handle into
1b86f 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 61 the statement a
1b870 6e 64 20 73 65 74 73 20 74 68 65 20 73 74 61 74 nd sets the stat
1b871 65 6d 65 6e 74 0a 20 20 20 20 20 20 2a 2a 20 70 ement. ** p
1b872 72 6f 67 72 61 6d 20 63 6f 75 6e 74 65 72 20 74 rogram counter t
1b873 6f 20 30 20 74 6f 20 65 6e 73 75 72 65 20 74 68 o 0 to ensure th
1b874 61 74 20 77 68 65 6e 20 74 68 65 20 73 74 61 74 at when the stat
1b875 65 6d 65 6e 74 20 69 73 20 0a 20 20 20 20 20 20 ement is .
1b876 2a 2a 20 66 69 6e 61 6c 69 7a 65 64 20 6f 72 20 ** finalized or
1b877 72 65 73 65 74 20 74 68 65 20 70 61 72 73 65 72 reset the parser
1b878 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 error message i
1b879 73 20 61 76 61 69 6c 61 62 6c 65 20 76 69 61 0a s available via.
1b87a 20 20 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 ** sqlite3
1b87b 5f 65 72 72 6d 73 67 28 29 20 61 6e 64 20 73 71 _errmsg() and sq
1b87c 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29 2e lite3_errcode().
1b87d 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
1b87e 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 45 72 72 const char *zErr
1b87f 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a = (const char *
1b880 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 )sqlite3_value_t
1b881 65 78 74 28 64 62 2d 3e 70 45 72 72 29 3b 20 0a ext(db->pErr); .
1b882 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 sqlite3DbF
1b883 72 65 65 28 64 62 2c 20 76 2d 3e 7a 45 72 72 4d ree(db, v->zErrM
1b884 73 67 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 sg);. if( !
1b885 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
1b886 20 29 7b 0a 20 20 20 20 20 20 20 20 76 2d 3e 7a ){. v->z
1b887 45 72 72 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 ErrMsg = sqlite3
1b888 44 62 53 74 72 44 75 70 28 64 62 2c 20 7a 45 72 DbStrDup(db, zEr
1b889 72 29 3b 0a 20 20 20 20 20 20 7d 20 65 6c 73 65 r);. } else
1b88a 20 7b 0a 20 20 20 20 20 20 20 20 76 2d 3e 7a 45 {. v->zE
1b88b 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 20 20 20 rrMsg = 0;.
1b88c 20 20 20 76 2d 3e 72 63 20 3d 20 53 51 4c 49 54 v->rc = SQLIT
1b88d 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d E_NOMEM;. }
1b88e 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 . }. rc =
1b88f 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 64 sqlite3ApiExit(d
1b890 62 2c 20 72 63 29 3b 0a 20 20 20 20 73 71 6c 69 b, rc);. sqli
1b891 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
1b892 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a db->mutex);. }.
1b893 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
1b894 2f 2a 0a 2a 2a 20 45 78 74 72 61 63 74 20 74 68 /*.** Extract th
1b895 65 20 75 73 65 72 20 64 61 74 61 20 66 72 6f 6d e user data from
1b896 20 61 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 a sqlite3_conte
1b897 78 74 20 73 74 72 75 63 74 75 72 65 20 61 6e 64 xt structure and
1b898 20 72 65 74 75 72 6e 20 61 0a 2a 2a 20 70 6f 69 return a.** poi
1b899 6e 74 65 72 20 74 6f 20 69 74 2e 0a 2a 2f 0a 53 nter to it..*/.S
1b89a 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a QLITE_API void *
1b89b 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 sqlite3_user_dat
1b89c 61 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 a(sqlite3_contex
1b89d 74 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 t *p){. assert(
1b89e 20 70 20 26 26 20 70 2d 3e 70 46 75 6e 63 20 29 p && p->pFunc )
1b89f 3b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 70 46 ;. return p->pF
1b8a0 75 6e 63 2d 3e 70 55 73 65 72 44 61 74 61 3b 0a unc->pUserData;.
1b8a1 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 72 61 63 74 }../*.** Extract
1b8a2 20 74 68 65 20 75 73 65 72 20 64 61 74 61 20 66 the user data f
1b8a3 72 6f 6d 20 61 20 73 71 6c 69 74 65 33 5f 63 6f rom a sqlite3_co
1b8a4 6e 74 65 78 74 20 73 74 72 75 63 74 75 72 65 20 ntext structure
1b8a5 61 6e 64 20 72 65 74 75 72 6e 20 61 0a 2a 2a 20 and return a.**
1b8a6 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e 0a 2a pointer to it..*
1b8a7 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c /.SQLITE_API sql
1b8a8 69 74 65 33 20 2a 73 71 6c 69 74 65 33 5f 63 6f ite3 *sqlite3_co
1b8a9 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 ntext_db_handle(
1b8aa 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 sqlite3_context
1b8ab 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 *p){. assert( p
1b8ac 20 26 26 20 70 2d 3e 70 46 75 6e 63 20 29 3b 0a && p->pFunc );.
1b8ad 20 20 72 65 74 75 72 6e 20 70 2d 3e 73 2e 64 62 return p->s.db
1b8ae 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 ;.}../*.** The f
1b8af 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 74 68 65 20 ollowing is the
1b8b0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f implementation o
1b8b1 66 20 61 6e 20 53 51 4c 20 66 75 6e 63 74 69 6f f an SQL functio
1b8b2 6e 20 74 68 61 74 20 61 6c 77 61 79 73 0a 2a 2a n that always.**
1b8b3 20 66 61 69 6c 73 20 77 69 74 68 20 61 6e 20 65 fails with an e
1b8b4 72 72 6f 72 20 6d 65 73 73 61 67 65 20 73 74 61 rror message sta
1b8b5 74 69 6e 67 20 74 68 61 74 20 74 68 65 20 66 75 ting that the fu
1b8b6 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 69 nction is used i
1b8b7 6e 20 74 68 65 0a 2a 2a 20 77 72 6f 6e 67 20 63 n the.** wrong c
1b8b8 6f 6e 74 65 78 74 2e 20 20 54 68 65 20 73 71 6c ontext. The sql
1b8b9 69 74 65 33 5f 6f 76 65 72 6c 6f 61 64 5f 66 75 ite3_overload_fu
1b8ba 6e 63 74 69 6f 6e 28 29 20 41 50 49 20 6d 69 67 nction() API mig
1b8bb 68 74 20 63 6f 6e 73 74 72 75 63 74 0a 2a 2a 20 ht construct.**
1b8bc 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 SQL function tha
1b8bd 74 20 75 73 65 20 74 68 69 73 20 72 6f 75 74 69 t use this routi
1b8be 6e 65 20 73 6f 20 74 68 61 74 20 74 68 65 20 66 ne so that the f
1b8bf 75 6e 63 74 69 6f 6e 73 20 77 69 6c 6c 20 65 78 unctions will ex
1b8c0 69 73 74 0a 2a 2a 20 66 6f 72 20 6e 61 6d 65 20 ist.** for name
1b8c1 72 65 73 6f 6c 75 74 69 6f 6e 20 62 75 74 20 61 resolution but a
1b8c2 72 65 20 61 63 74 75 61 6c 6c 79 20 6f 76 65 72 re actually over
1b8c3 6c 6f 61 64 65 64 20 62 79 20 74 68 65 20 78 46 loaded by the xF
1b8c4 69 6e 64 46 75 6e 63 74 69 6f 6e 0a 2a 2a 20 6d indFunction.** m
1b8c5 65 74 68 6f 64 20 6f 66 20 76 69 72 74 75 61 6c ethod of virtual
1b8c6 20 74 61 62 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49 tables..*/.SQLI
1b8c7 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
1b8c8 73 71 6c 69 74 65 33 49 6e 76 61 6c 69 64 46 75 sqlite3InvalidFu
1b8c9 6e 63 74 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65 nction(. sqlite
1b8ca 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 3_context *conte
1b8cb 78 74 2c 20 20 2f 2a 20 54 68 65 20 66 75 6e 63 xt, /* The func
1b8cc 74 69 6f 6e 20 63 61 6c 6c 69 6e 67 20 63 6f 6e tion calling con
1b8cd 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 4e 6f text */. int No
1b8ce 74 55 73 65 64 2c 20 20 20 20 20 20 20 20 20 20 tUsed,
1b8cf 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
1b8d0 66 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 f arguments to t
1b8d1 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 he function */.
1b8d2 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a sqlite3_value *
1b8d3 2a 4e 6f 74 55 73 65 64 32 20 20 20 2f 2a 20 56 *NotUsed2 /* V
1b8d4 61 6c 75 65 20 6f 66 20 65 61 63 68 20 61 72 67 alue of each arg
1b8d5 75 6d 65 6e 74 20 2a 2f 0a 29 7b 0a 20 20 63 6f ument */.){. co
1b8d6 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 nst char *zName
1b8d7 3d 20 63 6f 6e 74 65 78 74 2d 3e 70 46 75 6e 63 = context->pFunc
1b8d8 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 63 68 61 72 20 ->zName;. char
1b8d9 2a 7a 45 72 72 3b 0a 20 20 55 4e 55 53 45 44 5f *zErr;. UNUSED_
1b8da 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 PARAMETER2(NotUs
1b8db 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20 ed, NotUsed2);.
1b8dc 20 7a 45 72 72 20 3d 20 73 71 6c 69 74 65 33 5f zErr = sqlite3_
1b8dd 6d 70 72 69 6e 74 66 28 0a 20 20 20 20 20 20 22 mprintf(. "
1b8de 75 6e 61 62 6c 65 20 74 6f 20 75 73 65 20 66 75 unable to use fu
1b8df 6e 63 74 69 6f 6e 20 25 73 20 69 6e 20 74 68 65 nction %s in the
1b8e0 20 72 65 71 75 65 73 74 65 64 20 63 6f 6e 74 65 requested conte
1b8e1 78 74 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 73 xt", zName);. s
1b8e2 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 qlite3_result_er
1b8e3 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 7a 45 72 ror(context, zEr
1b8e4 72 2c 20 2d 31 29 3b 0a 20 20 73 71 6c 69 74 65 r, -1);. sqlite
1b8e5 33 5f 66 72 65 65 28 7a 45 72 72 29 3b 0a 7d 0a 3_free(zErr);.}.
1b8e6 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 ./*.** Allocate
1b8e7 6f 72 20 72 65 74 75 72 6e 20 74 68 65 20 61 67 or return the ag
1b8e8 67 72 65 67 61 74 65 20 63 6f 6e 74 65 78 74 20 gregate context
1b8e9 66 6f 72 20 61 20 75 73 65 72 20 66 75 6e 63 74 for a user funct
1b8ea 69 6f 6e 2e 20 20 41 20 6e 65 77 0a 2a 2a 20 63 ion. A new.** c
1b8eb 6f 6e 74 65 78 74 20 69 73 20 61 6c 6c 6f 63 61 ontext is alloca
1b8ec 74 65 64 20 6f 6e 20 74 68 65 20 66 69 72 73 74 ted on the first
1b8ed 20 63 61 6c 6c 2e 20 20 53 75 62 73 65 71 75 65 call. Subseque
1b8ee 6e 74 20 63 61 6c 6c 73 20 72 65 74 75 72 6e 20 nt calls return
1b8ef 74 68 65 0a 2a 2a 20 73 61 6d 65 20 63 6f 6e 74 the.** same cont
1b8f0 65 78 74 20 74 68 61 74 20 77 61 73 20 72 65 74 ext that was ret
1b8f1 75 72 6e 65 64 20 6f 6e 20 70 72 69 6f 72 20 63 urned on prior c
1b8f2 61 6c 6c 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f alls..*/.SQLITE_
1b8f3 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 API void *sqlite
1b8f4 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 3_aggregate_cont
1b8f5 65 78 74 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 ext(sqlite3_cont
1b8f6 65 78 74 20 2a 70 2c 20 69 6e 74 20 6e 42 79 74 ext *p, int nByt
1b8f7 65 29 7b 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b e){. Mem *pMem;
1b8f8 0a 20 20 61 73 73 65 72 74 28 20 70 20 26 26 20 . assert( p &&
1b8f9 70 2d 3e 70 46 75 6e 63 20 26 26 20 70 2d 3e 70 p->pFunc && p->p
1b8fa 46 75 6e 63 2d 3e 78 53 74 65 70 20 29 3b 0a 20 Func->xStep );.
1b8fb 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
1b8fc 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 73 _mutex_held(p->s
1b8fd 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 .db->mutex) );.
1b8fe 20 70 4d 65 6d 20 3d 20 70 2d 3e 70 4d 65 6d 3b pMem = p->pMem;
1b8ff 0a 20 20 69 66 28 20 28 70 4d 65 6d 2d 3e 66 6c . if( (pMem->fl
1b900 61 67 73 20 26 20 4d 45 4d 5f 41 67 67 29 3d 3d ags & MEM_Agg)==
1b901 30 20 29 7b 0a 20 20 20 20 69 66 28 20 6e 42 79 0 ){. if( nBy
1b902 74 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 te==0 ){. s
1b903 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c qlite3VdbeMemRel
1b904 65 61 73 65 45 78 74 65 72 6e 61 6c 28 70 4d 65 easeExternal(pMe
1b905 6d 29 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e m);. pMem->
1b906 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c flags = MEM_Null
1b907 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 ;. pMem->z
1b908 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a = 0;. }else{.
1b909 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
1b90a 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 6e eMemGrow(pMem, n
1b90b 42 79 74 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 Byte, 0);.
1b90c 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 pMem->flags = ME
1b90d 4d 5f 41 67 67 3b 0a 20 20 20 20 20 20 70 4d 65 M_Agg;. pMe
1b90e 6d 2d 3e 75 2e 70 44 65 66 20 3d 20 70 2d 3e 70 m->u.pDef = p->p
1b90f 46 75 6e 63 3b 0a 20 20 20 20 20 20 69 66 28 20 Func;. if(
1b910 70 4d 65 6d 2d 3e 7a 20 29 7b 0a 20 20 20 20 20 pMem->z ){.
1b911 20 20 20 6d 65 6d 73 65 74 28 70 4d 65 6d 2d 3e memset(pMem->
1b912 7a 2c 20 30 2c 20 6e 42 79 74 65 29 3b 0a 20 20 z, 0, nByte);.
1b913 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a }. }. }.
1b914 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 2a 29 return (void*)
1b915 70 4d 65 6d 2d 3e 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a pMem->z;.}../*.*
1b916 2a 20 52 65 74 75 72 6e 20 74 68 65 20 61 75 78 * Return the aux
1b917 69 6c 61 72 79 20 64 61 74 61 20 70 6f 69 6e 74 ilary data point
1b918 65 72 2c 20 69 66 20 61 6e 79 2c 20 66 6f 72 20 er, if any, for
1b919 74 68 65 20 69 41 72 67 27 74 68 20 61 72 67 75 the iArg'th argu
1b91a 6d 65 6e 74 20 74 6f 0a 2a 2a 20 74 68 65 20 75 ment to.** the u
1b91b 73 65 72 2d 66 75 6e 63 74 69 6f 6e 20 64 65 66 ser-function def
1b91c 69 6e 65 64 20 62 79 20 70 43 74 78 2e 0a 2a 2f ined by pCtx..*/
1b91d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 .SQLITE_API void
1b91e 20 2a 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 *sqlite3_get_au
1b91f 78 64 61 74 61 28 73 71 6c 69 74 65 33 5f 63 6f xdata(sqlite3_co
1b920 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 69 6e 74 ntext *pCtx, int
1b921 20 69 41 72 67 29 7b 0a 20 20 56 64 62 65 46 75 iArg){. VdbeFu
1b922 6e 63 20 2a 70 56 64 62 65 46 75 6e 63 3b 0a 0a nc *pVdbeFunc;..
1b923 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
1b924 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 3_mutex_held(pCt
1b925 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 x->s.db->mutex)
1b926 29 3b 0a 20 20 70 56 64 62 65 46 75 6e 63 20 3d );. pVdbeFunc =
1b927 20 70 43 74 78 2d 3e 70 56 64 62 65 46 75 6e 63 pCtx->pVdbeFunc
1b928 3b 0a 20 20 69 66 28 20 21 70 56 64 62 65 46 75 ;. if( !pVdbeFu
1b929 6e 63 20 7c 7c 20 69 41 72 67 3e 3d 70 56 64 62 nc || iArg>=pVdb
1b92a 65 46 75 6e 63 2d 3e 6e 41 75 78 20 7c 7c 20 69 eFunc->nAux || i
1b92b 41 72 67 3c 30 20 29 7b 0a 20 20 20 20 72 65 74 Arg<0 ){. ret
1b92c 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 urn 0;. }. ret
1b92d 75 72 6e 20 70 56 64 62 65 46 75 6e 63 2d 3e 61 urn pVdbeFunc->a
1b92e 70 41 75 78 5b 69 41 72 67 5d 2e 70 41 75 78 3b pAux[iArg].pAux;
1b92f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 .}../*.** Set th
1b930 65 20 61 75 78 69 6c 61 72 79 20 64 61 74 61 20 e auxilary data
1b931 70 6f 69 6e 74 65 72 20 61 6e 64 20 64 65 6c 65 pointer and dele
1b932 74 65 20 66 75 6e 63 74 69 6f 6e 2c 20 66 6f 72 te function, for
1b933 20 74 68 65 20 69 41 72 67 27 74 68 0a 2a 2a 20 the iArg'th.**
1b934 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 argument to the
1b935 75 73 65 72 2d 66 75 6e 63 74 69 6f 6e 20 64 65 user-function de
1b936 66 69 6e 65 64 20 62 79 20 70 43 74 78 2e 20 41 fined by pCtx. A
1b937 6e 79 20 70 72 65 76 69 6f 75 73 20 76 61 6c 75 ny previous valu
1b938 65 20 69 73 0a 2a 2a 20 64 65 6c 65 74 65 64 20 e is.** deleted
1b939 62 79 20 63 61 6c 6c 69 6e 67 20 74 68 65 20 64 by calling the d
1b93a 65 6c 65 74 65 20 66 75 6e 63 74 69 6f 6e 20 73 elete function s
1b93b 70 65 63 69 66 69 65 64 20 77 68 65 6e 20 69 74 pecified when it
1b93c 20 77 61 73 20 73 65 74 2e 0a 2a 2f 0a 53 51 4c was set..*/.SQL
1b93d 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c ITE_API void sql
1b93e 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61 74 61 ite3_set_auxdata
1b93f 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 (. sqlite3_cont
1b940 65 78 74 20 2a 70 43 74 78 2c 20 0a 20 20 69 6e ext *pCtx, . in
1b941 74 20 69 41 72 67 2c 20 0a 20 20 76 6f 69 64 20 t iArg, . void
1b942 2a 70 41 75 78 2c 20 0a 20 20 76 6f 69 64 20 28 *pAux, . void (
1b943 2a 78 44 65 6c 65 74 65 29 28 76 6f 69 64 2a 29 *xDelete)(void*)
1b944 0a 29 7b 0a 20 20 73 74 72 75 63 74 20 41 75 78 .){. struct Aux
1b945 44 61 74 61 20 2a 70 41 75 78 44 61 74 61 3b 0a Data *pAuxData;.
1b946 20 20 56 64 62 65 46 75 6e 63 20 2a 70 56 64 62 VdbeFunc *pVdb
1b947 65 46 75 6e 63 3b 0a 20 20 69 66 28 20 69 41 72 eFunc;. if( iAr
1b948 67 3c 30 20 29 20 67 6f 74 6f 20 66 61 69 6c 65 g<0 ) goto faile
1b949 64 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 d;.. assert( sq
1b94a 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
1b94b 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 (pCtx->s.db->mut
1b94c 65 78 29 20 29 3b 0a 20 20 70 56 64 62 65 46 75 ex) );. pVdbeFu
1b94d 6e 63 20 3d 20 70 43 74 78 2d 3e 70 56 64 62 65 nc = pCtx->pVdbe
1b94e 46 75 6e 63 3b 0a 20 20 69 66 28 20 21 70 56 64 Func;. if( !pVd
1b94f 62 65 46 75 6e 63 20 7c 7c 20 70 56 64 62 65 46 beFunc || pVdbeF
1b950 75 6e 63 2d 3e 6e 41 75 78 3c 3d 69 41 72 67 20 unc->nAux<=iArg
1b951 29 7b 0a 20 20 20 20 69 6e 74 20 6e 41 75 78 20 ){. int nAux
1b952 3d 20 28 70 56 64 62 65 46 75 6e 63 20 3f 20 70 = (pVdbeFunc ? p
1b953 56 64 62 65 46 75 6e 63 2d 3e 6e 41 75 78 20 3a VdbeFunc->nAux :
1b954 20 30 29 3b 0a 20 20 20 20 69 6e 74 20 6e 4d 61 0);. int nMa
1b955 6c 6c 6f 63 20 3d 20 73 69 7a 65 6f 66 28 56 64 lloc = sizeof(Vd
1b956 62 65 46 75 6e 63 29 20 2b 20 73 69 7a 65 6f 66 beFunc) + sizeof
1b957 28 73 74 72 75 63 74 20 41 75 78 44 61 74 61 29 (struct AuxData)
1b958 2a 69 41 72 67 3b 0a 20 20 20 20 70 56 64 62 65 *iArg;. pVdbe
1b959 46 75 6e 63 20 3d 20 73 71 6c 69 74 65 33 44 62 Func = sqlite3Db
1b95a 52 65 61 6c 6c 6f 63 28 70 43 74 78 2d 3e 73 2e Realloc(pCtx->s.
1b95b 64 62 2c 20 70 56 64 62 65 46 75 6e 63 2c 20 6e db, pVdbeFunc, n
1b95c 4d 61 6c 6c 6f 63 29 3b 0a 20 20 20 20 69 66 28 Malloc);. if(
1b95d 20 21 70 56 64 62 65 46 75 6e 63 20 29 7b 0a 20 !pVdbeFunc ){.
1b95e 20 20 20 20 20 67 6f 74 6f 20 66 61 69 6c 65 64 goto failed
1b95f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 43 74 78 ;. }. pCtx
1b960 2d 3e 70 56 64 62 65 46 75 6e 63 20 3d 20 70 56 ->pVdbeFunc = pV
1b961 64 62 65 46 75 6e 63 3b 0a 20 20 20 20 6d 65 6d dbeFunc;. mem
1b962 73 65 74 28 26 70 56 64 62 65 46 75 6e 63 2d 3e set(&pVdbeFunc->
1b963 61 70 41 75 78 5b 6e 41 75 78 5d 2c 20 30 2c 20 apAux[nAux], 0,
1b964 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20 41 75 sizeof(struct Au
1b965 78 44 61 74 61 29 2a 28 69 41 72 67 2b 31 2d 6e xData)*(iArg+1-n
1b966 41 75 78 29 29 3b 0a 20 20 20 20 70 56 64 62 65 Aux));. pVdbe
1b967 46 75 6e 63 2d 3e 6e 41 75 78 20 3d 20 69 41 72 Func->nAux = iAr
1b968 67 2b 31 3b 0a 20 20 20 20 70 56 64 62 65 46 75 g+1;. pVdbeFu
1b969 6e 63 2d 3e 70 46 75 6e 63 20 3d 20 70 43 74 78 nc->pFunc = pCtx
1b96a 2d 3e 70 46 75 6e 63 3b 0a 20 20 7d 0a 0a 20 20 ->pFunc;. }..
1b96b 70 41 75 78 44 61 74 61 20 3d 20 26 70 56 64 62 pAuxData = &pVdb
1b96c 65 46 75 6e 63 2d 3e 61 70 41 75 78 5b 69 41 72 eFunc->apAux[iAr
1b96d 67 5d 3b 0a 20 20 69 66 28 20 70 41 75 78 44 61 g];. if( pAuxDa
1b96e 74 61 2d 3e 70 41 75 78 20 26 26 20 70 41 75 78 ta->pAux && pAux
1b96f 44 61 74 61 2d 3e 78 44 65 6c 65 74 65 20 29 7b Data->xDelete ){
1b970 0a 20 20 20 20 70 41 75 78 44 61 74 61 2d 3e 78 . pAuxData->x
1b971 44 65 6c 65 74 65 28 70 41 75 78 44 61 74 61 2d Delete(pAuxData-
1b972 3e 70 41 75 78 29 3b 0a 20 20 7d 0a 20 20 70 41 >pAux);. }. pA
1b973 75 78 44 61 74 61 2d 3e 70 41 75 78 20 3d 20 70 uxData->pAux = p
1b974 41 75 78 3b 0a 20 20 70 41 75 78 44 61 74 61 2d Aux;. pAuxData-
1b975 3e 78 44 65 6c 65 74 65 20 3d 20 78 44 65 6c 65 >xDelete = xDele
1b976 74 65 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 0a 66 te;. return;..f
1b977 61 69 6c 65 64 3a 0a 20 20 69 66 28 20 78 44 65 ailed:. if( xDe
1b978 6c 65 74 65 20 29 7b 0a 20 20 20 20 78 44 65 6c lete ){. xDel
1b979 65 74 65 28 70 41 75 78 29 3b 0a 20 20 7d 0a 7d ete(pAux);. }.}
1b97a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
1b97b 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45 44 _OMIT_DEPRECATED
1b97c 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ./*.** Return th
1b97d 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 e number of time
1b97e 73 20 74 68 65 20 53 74 65 70 20 66 75 6e 63 74 s the Step funct
1b97f 69 6f 6e 20 6f 66 20 61 20 61 67 67 72 65 67 61 ion of a aggrega
1b980 74 65 20 68 61 73 20 62 65 65 6e 20 0a 2a 2a 20 te has been .**
1b981 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 called..**.** Th
1b982 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 64 is function is d
1b983 65 70 72 65 63 61 74 65 64 2e 20 20 44 6f 20 6e eprecated. Do n
1b984 6f 74 20 75 73 65 20 69 74 20 66 6f 72 20 6e 65 ot use it for ne
1b985 77 20 63 6f 64 65 2e 20 20 49 74 20 69 73 0a 2a w code. It is.*
1b986 2a 20 70 72 6f 76 69 64 65 20 6f 6e 6c 79 20 74 * provide only t
1b987 6f 20 61 76 6f 69 64 20 62 72 65 61 6b 69 6e 67 o avoid breaking
1b988 20 6c 65 67 61 63 79 20 63 6f 64 65 2e 20 20 4e legacy code. N
1b989 65 77 20 61 67 67 72 65 67 61 74 65 20 66 75 6e ew aggregate fun
1b98a 63 74 69 6f 6e 0a 2a 2a 20 69 6d 70 6c 65 6d 65 ction.** impleme
1b98b 6e 74 61 74 69 6f 6e 73 20 73 68 6f 75 6c 64 20 ntations should
1b98c 6b 65 65 70 20 74 68 65 69 72 20 6f 77 6e 20 63 keep their own c
1b98d 6f 75 6e 74 73 20 77 69 74 68 69 6e 20 74 68 65 ounts within the
1b98e 69 72 20 61 67 67 72 65 67 61 74 65 0a 2a 2a 20 ir aggregate.**
1b98f 63 6f 6e 74 65 78 74 2e 0a 2a 2f 0a 53 51 4c 49 context..*/.SQLI
1b990 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
1b991 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 75 e3_aggregate_cou
1b992 6e 74 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 nt(sqlite3_conte
1b993 78 74 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 xt *p){. assert
1b994 28 20 70 20 26 26 20 70 2d 3e 70 4d 65 6d 20 26 ( p && p->pMem &
1b995 26 20 70 2d 3e 70 46 75 6e 63 20 26 26 20 70 2d & p->pFunc && p-
1b996 3e 70 46 75 6e 63 2d 3e 78 53 74 65 70 20 29 3b >pFunc->xStep );
1b997 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 70 4d 65 . return p->pMe
1b998 6d 2d 3e 6e 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a m->n;.}.#endif..
1b999 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 /*.** Return the
1b99a 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d number of colum
1b99b 6e 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 ns in the result
1b99c 20 73 65 74 20 66 6f 72 20 74 68 65 20 73 74 61 set for the sta
1b99d 74 65 6d 65 6e 74 20 70 53 74 6d 74 2e 0a 2a 2f tement pStmt..*/
1b99e 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
1b99f 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63 sqlite3_column_c
1b9a0 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d ount(sqlite3_stm
1b9a1 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20 56 64 62 t *pStmt){. Vdb
1b9a2 65 20 2a 70 56 6d 20 3d 20 28 56 64 62 65 20 2a e *pVm = (Vdbe *
1b9a3 29 70 53 74 6d 74 3b 0a 20 20 72 65 74 75 72 6e )pStmt;. return
1b9a4 20 70 56 6d 20 3f 20 70 56 6d 2d 3e 6e 52 65 73 pVm ? pVm->nRes
1b9a5 43 6f 6c 75 6d 6e 20 3a 20 30 3b 0a 7d 0a 0a 2f Column : 0;.}../
1b9a6 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
1b9a7 6e 75 6d 62 65 72 20 6f 66 20 76 61 6c 75 65 73 number of values
1b9a8 20 61 76 61 69 6c 61 62 6c 65 20 66 72 6f 6d 20 available from
1b9a9 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 20 the current row
1b9aa 6f 66 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e of the.** curren
1b9ab 74 6c 79 20 65 78 65 63 75 74 69 6e 67 20 73 74 tly executing st
1b9ac 61 74 65 6d 65 6e 74 20 70 53 74 6d 74 2e 0a 2a atement pStmt..*
1b9ad 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
1b9ae 20 73 71 6c 69 74 65 33 5f 64 61 74 61 5f 63 6f sqlite3_data_co
1b9af 75 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 unt(sqlite3_stmt
1b9b0 20 2a 70 53 74 6d 74 29 7b 0a 20 20 56 64 62 65 *pStmt){. Vdbe
1b9b1 20 2a 70 56 6d 20 3d 20 28 56 64 62 65 20 2a 29 *pVm = (Vdbe *)
1b9b2 70 53 74 6d 74 3b 0a 20 20 69 66 28 20 70 56 6d pStmt;. if( pVm
1b9b3 3d 3d 30 20 7c 7c 20 70 56 6d 2d 3e 70 52 65 73 ==0 || pVm->pRes
1b9b4 75 6c 74 53 65 74 3d 3d 30 20 29 20 72 65 74 75 ultSet==0 ) retu
1b9b5 72 6e 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 70 rn 0;. return p
1b9b6 56 6d 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 3b 0a Vm->nResColumn;.
1b9b7 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 }.../*.** Check
1b9b8 74 6f 20 73 65 65 20 69 66 20 63 6f 6c 75 6d 6e to see if column
1b9b9 20 69 43 6f 6c 20 6f 66 20 74 68 65 20 67 69 76 iCol of the giv
1b9ba 65 6e 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 en statement is
1b9bb 76 61 6c 69 64 2e 20 20 49 66 0a 2a 2a 20 69 74 valid. If.** it
1b9bc 20 69 73 2c 20 72 65 74 75 72 6e 20 61 20 70 6f is, return a po
1b9bd 69 6e 74 65 72 20 74 6f 20 74 68 65 20 4d 65 6d inter to the Mem
1b9be 20 66 6f 72 20 74 68 65 20 76 61 6c 75 65 20 6f for the value o
1b9bf 66 20 74 68 61 74 20 63 6f 6c 75 6d 6e 2e 0a 2a f that column..*
1b9c0 2a 20 49 66 20 69 43 6f 6c 20 69 73 20 6e 6f 74 * If iCol is not
1b9c1 20 76 61 6c 69 64 2c 20 72 65 74 75 72 6e 20 61 valid, return a
1b9c2 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 4d 65 pointer to a Me
1b9c3 6d 20 77 68 69 63 68 20 68 61 73 20 61 20 76 61 m which has a va
1b9c4 6c 75 65 0a 2a 2a 20 6f 66 20 4e 55 4c 4c 2e 0a lue.** of NULL..
1b9c5 2a 2f 0a 73 74 61 74 69 63 20 4d 65 6d 20 2a 63 */.static Mem *c
1b9c6 6f 6c 75 6d 6e 4d 65 6d 28 73 71 6c 69 74 65 33 olumnMem(sqlite3
1b9c7 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e _stmt *pStmt, in
1b9c8 74 20 69 29 7b 0a 20 20 56 64 62 65 20 2a 70 56 t i){. Vdbe *pV
1b9c9 6d 3b 0a 20 20 69 6e 74 20 76 61 6c 73 3b 0a 20 m;. int vals;.
1b9ca 20 4d 65 6d 20 2a 70 4f 75 74 3b 0a 0a 20 20 70 Mem *pOut;.. p
1b9cb 56 6d 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74 Vm = (Vdbe *)pSt
1b9cc 6d 74 3b 0a 20 20 69 66 28 20 70 56 6d 20 26 26 mt;. if( pVm &&
1b9cd 20 70 56 6d 2d 3e 70 52 65 73 75 6c 74 53 65 74 pVm->pResultSet
1b9ce 21 3d 30 20 26 26 20 69 3c 70 56 6d 2d 3e 6e 52 !=0 && i<pVm->nR
1b9cf 65 73 43 6f 6c 75 6d 6e 20 26 26 20 69 3e 3d 30 esColumn && i>=0
1b9d0 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f ){. sqlite3_
1b9d1 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 56 6d 2d mutex_enter(pVm-
1b9d2 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 >db->mutex);.
1b9d3 20 76 61 6c 73 20 3d 20 73 71 6c 69 74 65 33 5f vals = sqlite3_
1b9d4 64 61 74 61 5f 63 6f 75 6e 74 28 70 53 74 6d 74 data_count(pStmt
1b9d5 29 3b 0a 20 20 20 20 70 4f 75 74 20 3d 20 26 70 );. pOut = &p
1b9d6 56 6d 2d 3e 70 52 65 73 75 6c 74 53 65 74 5b 69 Vm->pResultSet[i
1b9d7 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 ];. }else{.
1b9d8 2f 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 /* If the value
1b9d9 70 61 73 73 65 64 20 61 73 20 74 68 65 20 73 65 passed as the se
1b9da 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 cond argument is
1b9db 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 2c 20 72 out of range, r
1b9dc 65 74 75 72 6e 0a 20 20 20 20 2a 2a 20 61 20 70 eturn. ** a p
1b9dd 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 66 6f ointer to the fo
1b9de 6c 6c 6f 77 69 6e 67 20 73 74 61 74 69 63 20 4d llowing static M
1b9df 65 6d 20 6f 62 6a 65 63 74 20 77 68 69 63 68 20 em object which
1b9e0 63 6f 6e 74 61 69 6e 73 20 74 68 65 0a 20 20 20 contains the.
1b9e1 20 2a 2a 20 76 61 6c 75 65 20 53 51 4c 20 4e 55 ** value SQL NU
1b9e2 4c 4c 2e 20 45 76 65 6e 20 74 68 6f 75 67 68 20 LL. Even though
1b9e3 74 68 65 20 4d 65 6d 20 73 74 72 75 63 74 75 72 the Mem structur
1b9e4 65 20 63 6f 6e 74 61 69 6e 73 20 61 6e 20 65 6c e contains an el
1b9e5 65 6d 65 6e 74 0a 20 20 20 20 2a 2a 20 6f 66 20 ement. ** of
1b9e6 74 79 70 65 20 69 36 34 2c 20 6f 6e 20 63 65 72 type i64, on cer
1b9e7 74 61 69 6e 20 61 72 63 68 69 74 65 63 74 75 72 tain architectur
1b9e8 65 20 28 78 38 36 29 20 77 69 74 68 20 63 65 72 e (x86) with cer
1b9e9 74 61 69 6e 20 63 6f 6d 70 69 6c 65 72 0a 20 20 tain compiler.
1b9ea 20 20 2a 2a 20 73 77 69 74 63 68 65 73 20 28 2d ** switches (-
1b9eb 4f 73 29 2c 20 67 63 63 20 6d 61 79 20 61 6c 69 Os), gcc may ali
1b9ec 67 6e 20 74 68 69 73 20 4d 65 6d 20 6f 62 6a 65 gn this Mem obje
1b9ed 63 74 20 6f 6e 20 61 20 34 2d 62 79 74 65 20 62 ct on a 4-byte b
1b9ee 6f 75 6e 64 61 72 79 0a 20 20 20 20 2a 2a 20 69 oundary. ** i
1b9ef 6e 73 74 65 61 64 20 6f 66 20 61 6e 20 38 2d 62 nstead of an 8-b
1b9f0 79 74 65 20 6f 6e 65 2e 20 54 68 69 73 20 61 6c yte one. This al
1b9f1 6c 20 77 6f 72 6b 73 20 66 69 6e 65 2c 20 65 78 l works fine, ex
1b9f2 63 65 70 74 20 74 68 61 74 20 77 68 65 6e 0a 20 cept that when.
1b9f3 20 20 20 2a 2a 20 72 75 6e 6e 69 6e 67 20 77 69 ** running wi
1b9f4 74 68 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 th SQLITE_DEBUG
1b9f5 64 65 66 69 6e 65 64 20 74 68 65 20 53 51 4c 69 defined the SQLi
1b9f6 74 65 20 63 6f 64 65 20 73 6f 6d 65 74 69 6d 65 te code sometime
1b9f7 73 20 61 73 73 65 72 74 28 29 73 0a 20 20 20 20 s assert()s.
1b9f8 2a 2a 20 74 68 61 74 20 61 20 4d 65 6d 20 73 74 ** that a Mem st
1b9f9 72 75 63 74 75 72 65 20 69 73 20 6c 6f 63 61 74 ructure is locat
1b9fa 65 64 20 6f 6e 20 61 6e 20 38 2d 62 79 74 65 20 ed on an 8-byte
1b9fb 62 6f 75 6e 64 61 72 79 2e 20 54 6f 20 70 72 65 boundary. To pre
1b9fc 76 65 6e 74 0a 20 20 20 20 2a 2a 20 74 68 69 73 vent. ** this
1b9fd 20 61 73 73 65 72 74 28 29 20 66 72 6f 6d 20 66 assert() from f
1b9fe 61 69 6c 69 6e 67 2c 20 77 68 65 6e 20 62 75 69 ailing, when bui
1b9ff 6c 64 69 6e 67 20 77 69 74 68 20 53 51 4c 49 54 lding with SQLIT
1ba00 45 5f 44 45 42 55 47 20 64 65 66 69 6e 65 64 0a E_DEBUG defined.
1ba01 20 20 20 20 2a 2a 20 75 73 69 6e 67 20 67 63 63 ** using gcc
1ba02 2c 20 66 6f 72 63 65 20 6e 75 6c 6c 4d 65 6d 20 , force nullMem
1ba03 74 6f 20 62 65 20 38 2d 62 79 74 65 20 61 6c 69 to be 8-byte ali
1ba04 67 6e 65 64 20 75 73 69 6e 67 20 74 68 65 20 6d gned using the m
1ba05 61 67 69 63 61 6c 0a 20 20 20 20 2a 2a 20 5f 5f agical. ** __
1ba06 61 74 74 72 69 62 75 74 65 5f 5f 28 28 61 6c 69 attribute__((ali
1ba07 67 6e 65 64 28 38 29 29 29 20 6d 61 63 72 6f 2e gned(8))) macro.
1ba08 20 20 2a 2f 0a 20 20 20 20 73 74 61 74 69 63 20 */. static
1ba09 63 6f 6e 73 74 20 4d 65 6d 20 6e 75 6c 6c 4d 65 const Mem nullMe
1ba0a 6d 20 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 m .#if defined(S
1ba0b 51 4c 49 54 45 5f 44 45 42 55 47 29 20 26 26 20 QLITE_DEBUG) &&
1ba0c 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f defined(__GNUC__
1ba0d 29 0a 20 20 20 20 20 20 5f 5f 61 74 74 72 69 62 ). __attrib
1ba0e 75 74 65 5f 5f 28 28 61 6c 69 67 6e 65 64 28 38 ute__((aligned(8
1ba0f 29 29 29 20 0a 23 65 6e 64 69 66 0a 20 20 20 20 ))) .#endif.
1ba10 20 20 3d 20 7b 7b 30 7d 2c 20 28 64 6f 75 62 6c = {{0}, (doubl
1ba11 65 29 30 2c 20 30 2c 20 22 22 2c 20 30 2c 20 4d e)0, 0, "", 0, M
1ba12 45 4d 5f 4e 75 6c 6c 2c 20 53 51 4c 49 54 45 5f EM_Null, SQLITE_
1ba13 4e 55 4c 4c 2c 20 30 2c 20 30 2c 20 30 20 7d 3b NULL, 0, 0, 0 };
1ba14 0a 0a 20 20 20 20 69 66 28 20 70 56 6d 20 26 26 .. if( pVm &&
1ba15 20 41 4c 57 41 59 53 28 70 56 6d 2d 3e 64 62 29 ALWAYS(pVm->db)
1ba16 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
1ba17 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 56 3_mutex_enter(pV
1ba18 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 m->db->mutex);.
1ba19 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f sqlite3Erro
1ba1a 72 28 70 56 6d 2d 3e 64 62 2c 20 53 51 4c 49 54 r(pVm->db, SQLIT
1ba1b 45 5f 52 41 4e 47 45 2c 20 30 29 3b 0a 20 20 20 E_RANGE, 0);.
1ba1c 20 7d 0a 20 20 20 20 70 4f 75 74 20 3d 20 28 4d }. pOut = (M
1ba1d 65 6d 2a 29 26 6e 75 6c 6c 4d 65 6d 3b 0a 20 20 em*)&nullMem;.
1ba1e 7d 0a 20 20 72 65 74 75 72 6e 20 70 4f 75 74 3b }. return pOut;
1ba1f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 .}../*.** This f
1ba20 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 unction is calle
1ba21 64 20 61 66 74 65 72 20 69 6e 76 6f 6b 69 6e 67 d after invoking
1ba22 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 an sqlite3_valu
1ba23 65 5f 58 58 58 20 66 75 6e 63 74 69 6f 6e 20 6f e_XXX function o
1ba24 6e 20 61 20 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 76 n a .** column v
1ba25 61 6c 75 65 20 28 69 2e 65 2e 20 61 20 76 61 6c alue (i.e. a val
1ba26 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 65 ue returned by e
1ba27 76 61 6c 75 61 74 69 6e 67 20 61 6e 20 53 51 4c valuating an SQL
1ba28 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 74 expression in t
1ba29 68 65 0a 2a 2a 20 73 65 6c 65 63 74 20 6c 69 73 he.** select lis
1ba2a 74 20 6f 66 20 61 20 53 45 4c 45 43 54 20 73 74 t of a SELECT st
1ba2b 61 74 65 6d 65 6e 74 29 20 74 68 61 74 20 6d 61 atement) that ma
1ba2c 79 20 63 61 75 73 65 20 61 20 6d 61 6c 6c 6f 63 y cause a malloc
1ba2d 28 29 20 66 61 69 6c 75 72 65 2e 20 49 66 20 0a () failure. If .
1ba2e 2a 2a 20 6d 61 6c 6c 6f 63 28 29 20 68 61 73 20 ** malloc() has
1ba2f 66 61 69 6c 65 64 2c 20 74 68 65 20 74 68 72 65 failed, the thre
1ba30 61 64 73 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 ads mallocFailed
1ba31 20 66 6c 61 67 20 69 73 20 63 6c 65 61 72 65 64 flag is cleared
1ba32 20 61 6e 64 20 74 68 65 20 72 65 73 75 6c 74 0a and the result.
1ba33 2a 2a 20 63 6f 64 65 20 6f 66 20 73 74 61 74 65 ** code of state
1ba34 6d 65 6e 74 20 70 53 74 6d 74 20 73 65 74 20 74 ment pStmt set t
1ba35 6f 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 0a o SQLITE_NOMEM..
1ba36 2a 2a 0a 2a 2a 20 53 70 65 63 69 66 69 63 61 6c **.** Specifical
1ba37 6c 79 2c 20 74 68 69 73 20 69 73 20 63 61 6c 6c ly, this is call
1ba38 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 3a 0a ed from within:.
1ba39 2a 2a 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 **.** sqlite
1ba3a 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 29 0a 2a 3_column_int().*
1ba3b 2a 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f * sqlite3_co
1ba3c 6c 75 6d 6e 5f 69 6e 74 36 34 28 29 0a 2a 2a 20 lumn_int64().**
1ba3d 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 sqlite3_colu
1ba3e 6d 6e 5f 74 65 78 74 28 29 0a 2a 2a 20 20 20 20 mn_text().**
1ba3f 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
1ba40 74 65 78 74 31 36 28 29 0a 2a 2a 20 20 20 20 20 text16().**
1ba41 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 72 sqlite3_column_r
1ba42 65 61 6c 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c eal().** sql
1ba43 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 ite3_column_byte
1ba44 73 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 s().** sqlit
1ba45 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 e3_column_bytes1
1ba46 36 28 29 0a 2a 2a 0a 2a 2a 20 42 75 74 20 6e 6f 6().**.** But no
1ba47 74 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 63 6f t for sqlite3_co
1ba48 6c 75 6d 6e 5f 62 6c 6f 62 28 29 2c 20 77 68 69 lumn_blob(), whi
1ba49 63 68 20 6e 65 76 65 72 20 63 61 6c 6c 73 20 6d ch never calls m
1ba4a 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 alloc()..*/.stat
1ba4b 69 63 20 76 6f 69 64 20 63 6f 6c 75 6d 6e 4d 61 ic void columnMa
1ba4c 6c 6c 6f 63 46 61 69 6c 75 72 65 28 73 71 6c 69 llocFailure(sqli
1ba4d 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 te3_stmt *pStmt)
1ba4e 0a 7b 0a 20 20 2f 2a 20 49 66 20 6d 61 6c 6c 6f .{. /* If mallo
1ba4f 63 28 29 20 66 61 69 6c 65 64 20 64 75 72 69 6e c() failed durin
1ba50 67 20 61 6e 20 65 6e 63 6f 64 69 6e 67 20 63 6f g an encoding co
1ba51 6e 76 65 72 73 69 6f 6e 20 77 69 74 68 69 6e 20 nversion within
1ba52 61 6e 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f an. ** sqlite3_
1ba53 63 6f 6c 75 6d 6e 5f 58 58 58 20 41 50 49 2c 20 column_XXX API,
1ba54 74 68 65 6e 20 73 65 74 20 74 68 65 20 72 65 74 then set the ret
1ba55 75 72 6e 20 63 6f 64 65 20 6f 66 20 74 68 65 20 urn code of the
1ba56 73 74 61 74 65 6d 65 6e 74 20 74 6f 0a 20 20 2a statement to. *
1ba57 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 20 * SQLITE_NOMEM.
1ba58 54 68 65 20 6e 65 78 74 20 63 61 6c 6c 20 74 6f The next call to
1ba59 20 5f 73 74 65 70 28 29 20 28 69 66 20 61 6e 79 _step() (if any
1ba5a 29 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 53 51 ) will return SQ
1ba5b 4c 49 54 45 5f 45 52 52 4f 52 0a 20 20 2a 2a 20 LITE_ERROR. **
1ba5c 61 6e 64 20 5f 66 69 6e 61 6c 69 7a 65 28 29 20 and _finalize()
1ba5d 77 69 6c 6c 20 72 65 74 75 72 6e 20 4e 4f 4d 45 will return NOME
1ba5e 4d 2e 0a 20 20 2a 2f 0a 20 20 56 64 62 65 20 2a M.. */. Vdbe *
1ba5f 70 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74 6d p = (Vdbe *)pStm
1ba60 74 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 t;. if( p ){.
1ba61 20 20 70 2d 3e 72 63 20 3d 20 73 71 6c 69 74 65 p->rc = sqlite
1ba62 33 41 70 69 45 78 69 74 28 70 2d 3e 64 62 2c 20 3ApiExit(p->db,
1ba63 70 2d 3e 72 63 29 3b 0a 20 20 20 20 73 71 6c 69 p->rc);. sqli
1ba64 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
1ba65 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 p->db->mutex);.
1ba66 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a }.}../*********
1ba67 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ba68 2a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 *** sqlite3_colu
1ba69 6d 6e 5f 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a mn_ ***********
1ba6a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1ba6b 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c ****.** The foll
1ba6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 73 20 61 owing routines a
1ba6d 72 65 20 75 73 65 64 20 74 6f 20 61 63 63 65 73 re used to acces
1ba6e 73 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 s elements of th
1ba6f 65 20 63 75 72 72 65 6e 74 20 72 6f 77 0a 2a 2a e current row.**
1ba70 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 in the result s
1ba71 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 et..*/.SQLITE_AP
1ba72 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 I const void *sq
1ba73 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f lite3_column_blo
1ba74 62 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a b(sqlite3_stmt *
1ba75 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 pStmt, int i){.
1ba76 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 76 61 6c const void *val
1ba77 3b 0a 20 20 76 61 6c 20 3d 20 73 71 6c 69 74 65 ;. val = sqlite
1ba78 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 20 63 6f 3_value_blob( co
1ba79 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 lumnMem(pStmt,i)
1ba7a 20 29 3b 0a 20 20 2f 2a 20 45 76 65 6e 20 74 68 );. /* Even th
1ba7b 6f 75 67 68 20 74 68 65 72 65 20 69 73 20 6e 6f ough there is no
1ba7c 20 65 6e 63 6f 64 69 6e 67 20 63 6f 6e 76 65 72 encoding conver
1ba7d 73 69 6f 6e 2c 20 76 61 6c 75 65 5f 62 6c 6f 62 sion, value_blob
1ba7e 28 29 20 6d 69 67 68 74 0a 20 20 2a 2a 20 6e 65 () might. ** ne
1ba7f 65 64 20 74 6f 20 63 61 6c 6c 20 6d 61 6c 6c 6f ed to call mallo
1ba80 63 28 29 20 74 6f 20 65 78 70 61 6e 64 20 74 68 c() to expand th
1ba81 65 20 72 65 73 75 6c 74 20 6f 66 20 61 20 7a 65 e result of a ze
1ba82 72 6f 62 6c 6f 62 28 29 20 0a 20 20 2a 2a 20 65 roblob() . ** e
1ba83 78 70 72 65 73 73 69 6f 6e 2e 20 0a 20 20 2a 2f xpression. . */
1ba84 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 . columnMallocF
1ba85 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20 ailure(pStmt);.
1ba86 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53 return val;.}.S
1ba87 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
1ba88 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 lite3_column_byt
1ba89 65 73 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 es(sqlite3_stmt
1ba8a 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a *pStmt, int i){.
1ba8b 20 20 69 6e 74 20 76 61 6c 20 3d 20 73 71 6c 69 int val = sqli
1ba8c 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 te3_value_bytes(
1ba8d 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 columnMem(pStmt
1ba8e 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d ,i) );. columnM
1ba8f 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70 53 74 allocFailure(pSt
1ba90 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 76 61 mt);. return va
1ba91 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 l;.}.SQLITE_API
1ba92 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 int sqlite3_colu
1ba93 6d 6e 5f 62 79 74 65 73 31 36 28 73 71 6c 69 74 mn_bytes16(sqlit
1ba94 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 e3_stmt *pStmt,
1ba95 69 6e 74 20 69 29 7b 0a 20 20 69 6e 74 20 76 61 int i){. int va
1ba96 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 l = sqlite3_valu
1ba97 65 5f 62 79 74 65 73 31 36 28 20 63 6f 6c 75 6d e_bytes16( colum
1ba98 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b nMem(pStmt,i) );
1ba99 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 . columnMallocF
1ba9a 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20 ailure(pStmt);.
1ba9b 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53 return val;.}.S
1ba9c 51 4c 49 54 45 5f 41 50 49 20 64 6f 75 62 6c 65 QLITE_API double
1ba9d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
1ba9e 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 5f 73 double(sqlite3_s
1ba9f 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 tmt *pStmt, int
1baa0 69 29 7b 0a 20 20 64 6f 75 62 6c 65 20 76 61 6c i){. double val
1baa1 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 = sqlite3_value
1baa2 5f 64 6f 75 62 6c 65 28 20 63 6f 6c 75 6d 6e 4d _double( columnM
1baa3 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20 em(pStmt,i) );.
1baa4 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 columnMallocFai
1baa5 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 72 lure(pStmt);. r
1baa6 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c eturn val;.}.SQL
1baa7 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
1baa8 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 73 te3_column_int(s
1baa9 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 qlite3_stmt *pSt
1baaa 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 69 6e mt, int i){. in
1baab 74 20 76 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f t val = sqlite3_
1baac 76 61 6c 75 65 5f 69 6e 74 28 20 63 6f 6c 75 6d value_int( colum
1baad 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b nMem(pStmt,i) );
1baae 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 . columnMallocF
1baaf 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20 ailure(pStmt);.
1bab0 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53 return val;.}.S
1bab1 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 QLITE_API sqlite
1bab2 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 63 _int64 sqlite3_c
1bab3 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 73 71 6c 69 olumn_int64(sqli
1bab4 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c te3_stmt *pStmt,
1bab5 20 69 6e 74 20 69 29 7b 0a 20 20 73 71 6c 69 74 int i){. sqlit
1bab6 65 5f 69 6e 74 36 34 20 76 61 6c 20 3d 20 73 71 e_int64 val = sq
1bab7 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 lite3_value_int6
1bab8 34 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 4( columnMem(pSt
1bab9 6d 74 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75 6d mt,i) );. colum
1baba 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70 nMallocFailure(p
1babb 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 Stmt);. return
1babc 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 val;.}.SQLITE_AP
1babd 49 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 I const unsigned
1babe 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 char *sqlite3_c
1babf 6f 6c 75 6d 6e 5f 74 65 78 74 28 73 71 6c 69 74 olumn_text(sqlit
1bac0 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 e3_stmt *pStmt,
1bac1 69 6e 74 20 69 29 7b 0a 20 20 63 6f 6e 73 74 20 int i){. const
1bac2 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 76 unsigned char *v
1bac3 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c al = sqlite3_val
1bac4 75 65 5f 74 65 78 74 28 20 63 6f 6c 75 6d 6e 4d ue_text( columnM
1bac5 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20 em(pStmt,i) );.
1bac6 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 columnMallocFai
1bac7 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 72 lure(pStmt);. r
1bac8 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c eturn val;.}.SQL
1bac9 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f ITE_API sqlite3_
1baca 76 61 6c 75 65 20 2a 73 71 6c 69 74 65 33 5f 63 value *sqlite3_c
1bacb 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28 73 71 6c 69 olumn_value(sqli
1bacc 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c te3_stmt *pStmt,
1bacd 20 69 6e 74 20 69 29 7b 0a 20 20 4d 65 6d 20 2a int i){. Mem *
1bace 70 4f 75 74 20 3d 20 63 6f 6c 75 6d 6e 4d 65 6d pOut = columnMem
1bacf 28 70 53 74 6d 74 2c 20 69 29 3b 0a 20 20 69 66 (pStmt, i);. if
1bad0 28 20 70 4f 75 74 2d 3e 66 6c 61 67 73 26 4d 45 ( pOut->flags&ME
1bad1 4d 5f 53 74 61 74 69 63 20 29 7b 0a 20 20 20 20 M_Static ){.
1bad2 70 4f 75 74 2d 3e 66 6c 61 67 73 20 26 3d 20 7e pOut->flags &= ~
1bad3 4d 45 4d 5f 53 74 61 74 69 63 3b 0a 20 20 20 20 MEM_Static;.
1bad4 70 4f 75 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d pOut->flags |= M
1bad5 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 7d 0a 20 20 EM_Ephem;. }.
1bad6 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c columnMallocFail
1bad7 75 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 ure(pStmt);. re
1bad8 74 75 72 6e 20 28 73 71 6c 69 74 65 33 5f 76 61 turn (sqlite3_va
1bad9 6c 75 65 20 2a 29 70 4f 75 74 3b 0a 7d 0a 23 69 lue *)pOut;.}.#i
1bada 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
1badb 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 5f 41 T_UTF16.SQLITE_A
1badc 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 PI const void *s
1badd 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 qlite3_column_te
1bade 78 74 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d xt16(sqlite3_stm
1badf 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 t *pStmt, int i)
1bae0 7b 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a {. const void *
1bae1 76 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 val = sqlite3_va
1bae2 6c 75 65 5f 74 65 78 74 31 36 28 20 63 6f 6c 75 lue_text16( colu
1bae3 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 mnMem(pStmt,i) )
1bae4 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 ;. columnMalloc
1bae5 46 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a Failure(pStmt);.
1bae6 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a return val;.}.
1bae7 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
1bae8 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 53 _OMIT_UTF16 */.S
1bae9 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
1baea 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 lite3_column_typ
1baeb 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a e(sqlite3_stmt *
1baec 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 pStmt, int i){.
1baed 20 69 6e 74 20 69 54 79 70 65 20 3d 20 73 71 6c int iType = sql
1baee 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 ite3_value_type(
1baef 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 columnMem(pStmt
1baf0 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d ,i) );. columnM
1baf1 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70 53 74 allocFailure(pSt
1baf2 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 54 mt);. return iT
1baf3 79 70 65 3b 0a 7d 0a 0a 2f 2a 20 54 68 65 20 66 ype;.}../* The f
1baf4 6f 6c 6c 6f 77 69 6e 67 20 66 75 6e 63 74 69 6f ollowing functio
1baf5 6e 20 69 73 20 65 78 70 65 72 69 6d 65 6e 74 61 n is experimenta
1baf6 6c 20 61 6e 64 20 73 75 62 6a 65 63 74 20 74 6f l and subject to
1baf7 20 63 68 61 6e 67 65 20 6f 72 0a 2a 2a 20 72 65 change or.** re
1baf8 6d 6f 76 61 6c 20 2a 2f 0a 2f 2a 69 6e 74 20 73 moval */./*int s
1baf9 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 75 qlite3_column_nu
1bafa 6d 65 72 69 63 5f 74 79 70 65 28 73 71 6c 69 74 meric_type(sqlit
1bafb 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 e3_stmt *pStmt,
1bafc 69 6e 74 20 69 29 7b 0a 2a 2a 20 20 72 65 74 75 int i){.** retu
1bafd 72 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 rn sqlite3_value
1bafe 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65 28 20 63 _numeric_type( c
1baff 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 olumnMem(pStmt,i
1bb00 29 20 29 3b 0a 2a 2a 7d 0a 2a 2f 0a 0a 2f 2a 0a ) );.**}.*/../*.
1bb01 2a 2a 20 43 6f 6e 76 65 72 74 20 74 68 65 20 4e ** Convert the N
1bb02 2d 74 68 20 65 6c 65 6d 65 6e 74 20 6f 66 20 70 -th element of p
1bb03 53 74 6d 74 2d 3e 70 43 6f 6c 4e 61 6d 65 5b 5d Stmt->pColName[]
1bb04 20 69 6e 74 6f 20 61 20 73 74 72 69 6e 67 20 75 into a string u
1bb05 73 69 6e 67 0a 2a 2a 20 78 46 75 6e 63 28 29 20 sing.** xFunc()
1bb06 74 68 65 6e 20 72 65 74 75 72 6e 20 74 68 61 74 then return that
1bb07 20 73 74 72 69 6e 67 2e 20 20 49 66 20 4e 20 69 string. If N i
1bb08 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 2c 20 s out of range,
1bb09 72 65 74 75 72 6e 20 30 2e 0a 2a 2a 0a 2a 2a 20 return 0..**.**
1bb0a 54 68 65 72 65 20 61 72 65 20 75 70 20 74 6f 20 There are up to
1bb0b 35 20 6e 61 6d 65 73 20 66 6f 72 20 65 61 63 68 5 names for each
1bb0c 20 63 6f 6c 75 6d 6e 2e 20 20 75 73 65 54 79 70 column. useTyp
1bb0d 65 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68 69 e determines whi
1bb0e 63 68 0a 2a 2a 20 6e 61 6d 65 20 69 73 20 72 65 ch.** name is re
1bb0f 74 75 72 6e 65 64 2e 20 20 48 65 72 65 20 61 72 turned. Here ar
1bb10 65 20 74 68 65 20 6e 61 6d 65 73 3a 0a 2a 2a 0a e the names:.**.
1bb11 2a 2a 20 20 20 20 30 20 20 20 20 20 20 54 68 65 ** 0 The
1bb12 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 61 73 20 column name as
1bb13 69 74 20 73 68 6f 75 6c 64 20 62 65 20 64 69 73 it should be dis
1bb14 70 6c 61 79 65 64 20 66 6f 72 20 6f 75 74 70 75 played for outpu
1bb15 74 0a 2a 2a 20 20 20 20 31 20 20 20 20 20 20 54 t.** 1 T
1bb16 68 65 20 64 61 74 61 74 79 70 65 20 6e 61 6d 65 he datatype name
1bb17 20 66 6f 72 20 74 68 65 20 63 6f 6c 75 6d 6e 0a for the column.
1bb18 2a 2a 20 20 20 20 32 20 20 20 20 20 20 54 68 65 ** 2 The
1bb19 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 name of the dat
1bb1a 61 62 61 73 65 20 74 68 61 74 20 74 68 65 20 63 abase that the c
1bb1b 6f 6c 75 6d 6e 20 64 65 72 69 76 65 73 20 66 72 olumn derives fr
1bb1c 6f 6d 0a 2a 2a 20 20 20 20 33 20 20 20 20 20 20 om.** 3
1bb1d 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 The name of the
1bb1e 74 61 62 6c 65 20 74 68 61 74 20 74 68 65 20 63 table that the c
1bb1f 6f 6c 75 6d 6e 20 64 65 72 69 76 65 73 20 66 72 olumn derives fr
1bb20 6f 6d 0a 2a 2a 20 20 20 20 34 20 20 20 20 20 20 om.** 4
1bb21 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 The name of the
1bb22 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 74 68 61 table column tha
1bb23 74 20 74 68 65 20 72 65 73 75 6c 74 20 63 6f 6c t the result col
1bb24 75 6d 6e 20 64 65 72 69 76 65 73 20 66 72 6f 6d umn derives from
1bb25 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 .**.** If the re
1bb26 73 75 6c 74 20 69 73 20 6e 6f 74 20 61 20 73 69 sult is not a si
1bb27 6d 70 6c 65 20 63 6f 6c 75 6d 6e 20 72 65 66 65 mple column refe
1bb28 72 65 6e 63 65 20 28 69 66 20 69 74 20 69 73 20 rence (if it is
1bb29 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a an expression.**
1bb2a 20 6f 72 20 61 20 63 6f 6e 73 74 61 6e 74 29 20 or a constant)
1bb2b 74 68 65 6e 20 75 73 65 54 79 70 65 73 20 32 2c then useTypes 2,
1bb2c 20 33 2c 20 61 6e 64 20 34 20 72 65 74 75 72 6e 3, and 4 return
1bb2d 20 4e 55 4c 4c 2e 0a 2a 2f 0a 73 74 61 74 69 63 NULL..*/.static
1bb2e 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 63 6f 6c const void *col
1bb2f 75 6d 6e 4e 61 6d 65 28 0a 20 20 73 71 6c 69 74 umnName(. sqlit
1bb30 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 0a e3_stmt *pStmt,.
1bb31 20 20 69 6e 74 20 4e 2c 0a 20 20 63 6f 6e 73 74 int N,. const
1bb32 20 76 6f 69 64 20 2a 28 2a 78 46 75 6e 63 29 28 void *(*xFunc)(
1bb33 4d 65 6d 2a 29 2c 0a 20 20 69 6e 74 20 75 73 65 Mem*),. int use
1bb34 54 79 70 65 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 Type.){. const
1bb35 76 6f 69 64 20 2a 72 65 74 20 3d 20 30 3b 0a 20 void *ret = 0;.
1bb36 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 Vdbe *p = (Vdbe
1bb37 20 2a 29 70 53 74 6d 74 3b 0a 20 20 69 6e 74 20 *)pStmt;. int
1bb38 6e 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 n;. sqlite3 *db
1bb39 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 0a 20 20 61 = p->db;. . a
1bb3a 73 73 65 72 74 28 20 64 62 21 3d 30 20 29 3b 0a ssert( db!=0 );.
1bb3b 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f n = sqlite3_co
1bb3c 6c 75 6d 6e 5f 63 6f 75 6e 74 28 70 53 74 6d 74 lumn_count(pStmt
1bb3d 29 3b 0a 20 20 69 66 28 20 4e 3c 6e 20 26 26 20 );. if( N<n &&
1bb3e 4e 3e 3d 30 20 29 7b 0a 20 20 20 20 4e 20 2b 3d N>=0 ){. N +=
1bb3f 20 75 73 65 54 79 70 65 2a 6e 3b 0a 20 20 20 20 useType*n;.
1bb40 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e sqlite3_mutex_en
1bb41 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a ter(db->mutex);.
1bb42 20 20 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e assert( db->
1bb43 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3d 3d 30 20 mallocFailed==0
1bb44 29 3b 0a 20 20 20 20 72 65 74 20 3d 20 78 46 75 );. ret = xFu
1bb45 6e 63 28 26 70 2d 3e 61 43 6f 6c 4e 61 6d 65 5b nc(&p->aColName[
1bb46 4e 5d 29 3b 0a 20 20 20 20 20 2f 2a 20 41 20 6d N]);. /* A m
1bb47 61 6c 6c 6f 63 20 6d 61 79 20 68 61 76 65 20 66 alloc may have f
1bb48 61 69 6c 65 64 20 69 6e 73 69 64 65 20 6f 66 20 ailed inside of
1bb49 74 68 65 20 78 46 75 6e 63 28 29 20 63 61 6c 6c the xFunc() call
1bb4a 2e 20 49 66 20 74 68 69 73 0a 20 20 20 20 2a 2a . If this. **
1bb4b 20 69 73 20 74 68 65 20 63 61 73 65 2c 20 63 6c is the case, cl
1bb4c 65 61 72 20 74 68 65 20 6d 61 6c 6c 6f 63 46 61 ear the mallocFa
1bb4d 69 6c 65 64 20 66 6c 61 67 20 61 6e 64 20 72 65 iled flag and re
1bb4e 74 75 72 6e 20 4e 55 4c 4c 2e 0a 20 20 20 20 2a turn NULL.. *
1bb4f 2f 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 /. if( db->ma
1bb50 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 llocFailed ){.
1bb51 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 db->mallocFa
1bb52 69 6c 65 64 20 3d 20 30 3b 0a 20 20 20 20 20 20 iled = 0;.
1bb53 72 65 74 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 ret = 0;. }.
1bb54 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 sqlite3_mutex
1bb55 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 _leave(db->mutex
1bb56 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
1bb57 72 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 ret;.}../*.** Re
1bb58 74 75 72 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 turn the name of
1bb59 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 the Nth column
1bb5a 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 of the result se
1bb5b 74 20 72 65 74 75 72 6e 65 64 20 62 79 20 53 51 t returned by SQ
1bb5c 4c 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 70 L.** statement p
1bb5d 53 74 6d 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f Stmt..*/.SQLITE_
1bb5e 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a API const char *
1bb5f 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e sqlite3_column_n
1bb60 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 ame(sqlite3_stmt
1bb61 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b *pStmt, int N){
1bb62 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e . return column
1bb63 4e 61 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d Name(. pStm
1bb64 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 t, N, (const voi
1bb65 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 d*(*)(Mem*))sqli
1bb66 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 2c 20 te3_value_text,
1bb67 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 29 3b 0a 7d COLNAME_NAME);.}
1bb68 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1bb69 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 OMIT_UTF16.SQLIT
1bb6a 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 E_API const void
1bb6b 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e *sqlite3_column
1bb6c 5f 6e 61 6d 65 31 36 28 73 71 6c 69 74 65 33 5f _name16(sqlite3_
1bb6d 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 stmt *pStmt, int
1bb6e 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f N){. return co
1bb6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20 lumnName(.
1bb70 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 pStmt, N, (const
1bb71 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 void*(*)(Mem*))
1bb72 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 sqlite3_value_te
1bb73 78 74 31 36 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 xt16, COLNAME_NA
1bb74 4d 45 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f ME);.}.#endif../
1bb75 2a 0a 2a 2a 20 43 6f 6e 73 74 72 61 69 6e 74 3a *.** Constraint:
1bb76 20 20 49 66 20 79 6f 75 20 68 61 76 65 20 45 4e If you have EN
1bb77 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 ABLE_COLUMN_META
1bb78 44 41 54 41 20 74 68 65 6e 20 79 6f 75 20 6d 75 DATA then you mu
1bb79 73 74 0a 2a 2a 20 6e 6f 74 20 64 65 66 69 6e 65 st.** not define
1bb7a 20 4f 4d 49 54 5f 44 45 43 4c 54 59 50 45 2e 0a OMIT_DECLTYPE..
1bb7b 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 */.#if defined(S
1bb7c 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 43 4c 54 QLITE_OMIT_DECLT
1bb7d 59 50 45 29 20 26 26 20 64 65 66 69 6e 65 64 28 YPE) && defined(
1bb7e 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f SQLITE_ENABLE_CO
1bb7f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 29 0a 23 LUMN_METADATA).#
1bb80 20 65 72 72 6f 72 20 22 4d 75 73 74 20 6e 6f 74 error "Must not
1bb81 20 64 65 66 69 6e 65 20 62 6f 74 68 20 53 51 4c define both SQL
1bb82 49 54 45 5f 4f 4d 49 54 5f 44 45 43 4c 54 59 50 ITE_OMIT_DECLTYP
1bb83 45 20 5c 0a 20 20 20 20 20 20 20 20 20 61 6e 64 E \. and
1bb84 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43 SQLITE_ENABLE_C
1bb85 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 22 0a OLUMN_METADATA".
1bb86 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 #endif..#ifndef
1bb87 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 43 4c SQLITE_OMIT_DECL
1bb88 54 59 50 45 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 TYPE./*.** Retur
1bb89 6e 20 74 68 65 20 63 6f 6c 75 6d 6e 20 64 65 63 n the column dec
1bb8a 6c 61 72 61 74 69 6f 6e 20 74 79 70 65 20 28 69 laration type (i
1bb8b 66 20 61 70 70 6c 69 63 61 62 6c 65 29 20 6f 66 f applicable) of
1bb8c 20 74 68 65 20 27 69 27 74 68 20 63 6f 6c 75 6d the 'i'th colum
1bb8d 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 72 65 73 75 n.** of the resu
1bb8e 6c 74 20 73 65 74 20 6f 66 20 53 51 4c 20 73 74 lt set of SQL st
1bb8f 61 74 65 6d 65 6e 74 20 70 53 74 6d 74 2e 0a 2a atement pStmt..*
1bb90 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e /.SQLITE_API con
1bb91 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 st char *sqlite3
1bb92 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65 _column_decltype
1bb93 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 (sqlite3_stmt *p
1bb94 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 Stmt, int N){.
1bb95 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d return columnNam
1bb96 65 28 0a 20 20 20 20 20 20 70 53 74 6d 74 2c 20 e(. pStmt,
1bb97 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 N, (const void*(
1bb98 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 *)(Mem*))sqlite3
1bb99 5f 76 61 6c 75 65 5f 74 65 78 74 2c 20 43 4f 4c _value_text, COL
1bb9a 4e 41 4d 45 5f 44 45 43 4c 54 59 50 45 29 3b 0a NAME_DECLTYPE);.
1bb9b 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 }.#ifndef SQLITE
1bb9c 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 _OMIT_UTF16.SQLI
1bb9d 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 TE_API const voi
1bb9e 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d d *sqlite3_colum
1bb9f 6e 5f 64 65 63 6c 74 79 70 65 31 36 28 73 71 6c n_decltype16(sql
1bba0 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 ite3_stmt *pStmt
1bba1 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75 , int N){. retu
1bba2 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 rn columnName(.
1bba3 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28 pStmt, N, (
1bba4 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d const void*(*)(M
1bba5 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 6c em*))sqlite3_val
1bba6 75 65 5f 74 65 78 74 31 36 2c 20 43 4f 4c 4e 41 ue_text16, COLNA
1bba7 4d 45 5f 44 45 43 4c 54 59 50 45 29 3b 0a 7d 0a ME_DECLTYPE);.}.
1bba8 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
1bba9 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 23 _OMIT_UTF16 */.#
1bbaa 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
1bbab 4f 4d 49 54 5f 44 45 43 4c 54 59 50 45 20 2a 2f OMIT_DECLTYPE */
1bbac 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ..#ifdef SQLITE_
1bbad 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 ENABLE_COLUMN_ME
1bbae 54 41 44 41 54 41 0a 2f 2a 0a 2a 2a 20 52 65 74 TADATA./*.** Ret
1bbaf 75 72 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 urn the name of
1bbb0 74 68 65 20 64 61 74 61 62 61 73 65 20 66 72 6f the database fro
1bbb1 6d 20 77 68 69 63 68 20 61 20 72 65 73 75 6c 74 m which a result
1bbb2 20 63 6f 6c 75 6d 6e 20 64 65 72 69 76 65 73 2e column derives.
1bbb3 0a 2a 2a 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 .** NULL is retu
1bbb4 72 6e 65 64 20 69 66 20 74 68 65 20 72 65 73 75 rned if the resu
1bbb5 6c 74 20 63 6f 6c 75 6d 6e 20 69 73 20 61 6e 20 lt column is an
1bbb6 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 20 63 6f expression or co
1bbb7 6e 73 74 61 6e 74 20 6f 72 0a 2a 2a 20 61 6e 79 nstant or.** any
1bbb8 74 68 69 6e 67 20 65 6c 73 65 20 77 68 69 63 68 thing else which
1bbb9 20 69 73 20 6e 6f 74 20 61 6e 20 75 6e 61 62 69 is not an unabi
1bbba 67 75 6f 75 73 20 72 65 66 65 72 65 6e 63 65 20 guous reference
1bbbb 74 6f 20 61 20 64 61 74 61 62 61 73 65 20 63 6f to a database co
1bbbc 6c 75 6d 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f lumn..*/.SQLITE_
1bbbd 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a API const char *
1bbbe 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 sqlite3_column_d
1bbbf 61 74 61 62 61 73 65 5f 6e 61 6d 65 28 73 71 6c atabase_name(sql
1bbc0 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 ite3_stmt *pStmt
1bbc1 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75 , int N){. retu
1bbc2 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 rn columnName(.
1bbc3 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28 pStmt, N, (
1bbc4 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d const void*(*)(M
1bbc5 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 6c em*))sqlite3_val
1bbc6 75 65 5f 74 65 78 74 2c 20 43 4f 4c 4e 41 4d 45 ue_text, COLNAME
1bbc7 5f 44 41 54 41 42 41 53 45 29 3b 0a 7d 0a 23 69 _DATABASE);.}.#i
1bbc8 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
1bbc9 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 5f 41 T_UTF16.SQLITE_A
1bbca 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 PI const void *s
1bbcb 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 qlite3_column_da
1bbcc 74 61 62 61 73 65 5f 6e 61 6d 65 31 36 28 73 71 tabase_name16(sq
1bbcd 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d lite3_stmt *pStm
1bbce 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 t, int N){. ret
1bbcf 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a urn columnName(.
1bbd0 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 pStmt, N,
1bbd1 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 (const void*(*)(
1bbd2 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 Mem*))sqlite3_va
1bbd3 6c 75 65 5f 74 65 78 74 31 36 2c 20 43 4f 4c 4e lue_text16, COLN
1bbd4 41 4d 45 5f 44 41 54 41 42 41 53 45 29 3b 0a 7d AME_DATABASE);.}
1bbd5 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
1bbd6 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a E_OMIT_UTF16 */.
1bbd7 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ./*.** Return th
1bbd8 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 e name of the ta
1bbd9 62 6c 65 20 66 72 6f 6d 20 77 68 69 63 68 20 61 ble from which a
1bbda 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 64 result column d
1bbdb 65 72 69 76 65 73 2e 0a 2a 2a 20 4e 55 4c 4c 20 erives..** NULL
1bbdc 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 74 is returned if t
1bbdd 68 65 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e he result column
1bbde 20 69 73 20 61 6e 20 65 78 70 72 65 73 73 69 6f is an expressio
1bbdf 6e 20 6f 72 20 63 6f 6e 73 74 61 6e 74 20 6f 72 n or constant or
1bbe0 0a 2a 2a 20 61 6e 79 74 68 69 6e 67 20 65 6c 73 .** anything els
1bbe1 65 20 77 68 69 63 68 20 69 73 20 6e 6f 74 20 61 e which is not a
1bbe2 6e 20 75 6e 61 62 69 67 75 6f 75 73 20 72 65 66 n unabiguous ref
1bbe3 65 72 65 6e 63 65 20 74 6f 20 61 20 64 61 74 61 erence to a data
1bbe4 62 61 73 65 20 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a base column..*/.
1bbe5 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 SQLITE_API const
1bbe6 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 char *sqlite3_c
1bbe7 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 olumn_table_name
1bbe8 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 (sqlite3_stmt *p
1bbe9 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 Stmt, int N){.
1bbea 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d return columnNam
1bbeb 65 28 0a 20 20 20 20 20 20 70 53 74 6d 74 2c 20 e(. pStmt,
1bbec 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 N, (const void*(
1bbed 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 *)(Mem*))sqlite3
1bbee 5f 76 61 6c 75 65 5f 74 65 78 74 2c 20 43 4f 4c _value_text, COL
1bbef 4e 41 4d 45 5f 54 41 42 4c 45 29 3b 0a 7d 0a 23 NAME_TABLE);.}.#
1bbf0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1bbf1 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 5f IT_UTF16.SQLITE_
1bbf2 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a API const void *
1bbf3 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 sqlite3_column_t
1bbf4 61 62 6c 65 5f 6e 61 6d 65 31 36 28 73 71 6c 69 able_name16(sqli
1bbf5 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c te3_stmt *pStmt,
1bbf6 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75 72 int N){. retur
1bbf7 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 n columnName(.
1bbf8 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28 63 pStmt, N, (c
1bbf9 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d 65 onst void*(*)(Me
1bbfa 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 m*))sqlite3_valu
1bbfb 65 5f 74 65 78 74 31 36 2c 20 43 4f 4c 4e 41 4d e_text16, COLNAM
1bbfc 45 5f 54 41 42 4c 45 29 3b 0a 7d 0a 23 65 6e 64 E_TABLE);.}.#end
1bbfd 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 if /* SQLITE_OMI
1bbfe 54 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f 2a 0a 2a T_UTF16 */../*.*
1bbff 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 61 6d * Return the nam
1bc00 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 63 e of the table c
1bc01 6f 6c 75 6d 6e 20 66 72 6f 6d 20 77 68 69 63 68 olumn from which
1bc02 20 61 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e a result column
1bc03 20 64 65 72 69 76 65 73 2e 0a 2a 2a 20 4e 55 4c derives..** NUL
1bc04 4c 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 L is returned if
1bc05 20 74 68 65 20 72 65 73 75 6c 74 20 63 6f 6c 75 the result colu
1bc06 6d 6e 20 69 73 20 61 6e 20 65 78 70 72 65 73 73 mn is an express
1bc07 69 6f 6e 20 6f 72 20 63 6f 6e 73 74 61 6e 74 20 ion or constant
1bc08 6f 72 0a 2a 2a 20 61 6e 79 74 68 69 6e 67 20 65 or.** anything e
1bc09 6c 73 65 20 77 68 69 63 68 20 69 73 20 6e 6f 74 lse which is not
1bc0a 20 61 6e 20 75 6e 61 62 69 67 75 6f 75 73 20 72 an unabiguous r
1bc0b 65 66 65 72 65 6e 63 65 20 74 6f 20 61 20 64 61 eference to a da
1bc0c 74 61 62 61 73 65 20 63 6f 6c 75 6d 6e 2e 0a 2a tabase column..*
1bc0d 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e /.SQLITE_API con
1bc0e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 st char *sqlite3
1bc0f 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e _column_origin_n
1bc10 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 ame(sqlite3_stmt
1bc11 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b *pStmt, int N){
1bc12 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e . return column
1bc13 4e 61 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d Name(. pStm
1bc14 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 t, N, (const voi
1bc15 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 d*(*)(Mem*))sqli
1bc16 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 2c 20 te3_value_text,
1bc17 43 4f 4c 4e 41 4d 45 5f 43 4f 4c 55 4d 4e 29 3b COLNAME_COLUMN);
1bc18 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 .}.#ifndef SQLIT
1bc19 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c E_OMIT_UTF16.SQL
1bc1a 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f ITE_API const vo
1bc1b 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 id *sqlite3_colu
1bc1c 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 31 36 mn_origin_name16
1bc1d 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 (sqlite3_stmt *p
1bc1e 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 Stmt, int N){.
1bc1f 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d return columnNam
1bc20 65 28 0a 20 20 20 20 20 20 70 53 74 6d 74 2c 20 e(. pStmt,
1bc21 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 N, (const void*(
1bc22 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 *)(Mem*))sqlite3
1bc23 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 2c 20 43 _value_text16, C
1bc24 4f 4c 4e 41 4d 45 5f 43 4f 4c 55 4d 4e 29 3b 0a OLNAME_COLUMN);.
1bc25 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 }.#endif /* SQLI
1bc26 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f TE_OMIT_UTF16 */
1bc27 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
1bc28 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f E_ENABLE_COLUMN_
1bc29 4d 45 54 41 44 41 54 41 20 2a 2f 0a 0a 0a 2f 2a METADATA */.../*
1bc2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bc2b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 73 ************** s
1bc2c 71 6c 69 74 65 33 5f 62 69 6e 64 5f 20 20 2a 2a qlite3_bind_ **
1bc2d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bc2e 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 0a 2a 2a *********.** .**
1bc2f 20 52 6f 75 74 69 6e 65 73 20 75 73 65 64 20 74 Routines used t
1bc30 6f 20 61 74 74 61 63 68 20 76 61 6c 75 65 73 20 o attach values
1bc31 74 6f 20 77 69 6c 64 63 61 72 64 73 20 69 6e 20 to wildcards in
1bc32 61 20 63 6f 6d 70 69 6c 65 64 20 53 51 4c 20 73 a compiled SQL s
1bc33 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 2f 2a 0a tatement..*/./*.
1bc34 2a 2a 20 55 6e 62 69 6e 64 20 74 68 65 20 76 61 ** Unbind the va
1bc35 6c 75 65 20 62 6f 75 6e 64 20 74 6f 20 76 61 72 lue bound to var
1bc36 69 61 62 6c 65 20 69 20 69 6e 20 76 69 72 74 75 iable i in virtu
1bc37 61 6c 20 6d 61 63 68 69 6e 65 20 70 2e 20 54 68 al machine p. Th
1bc38 69 73 20 69 73 20 74 68 65 20 0a 2a 2a 20 74 68 is is the .** th
1bc39 65 20 73 61 6d 65 20 61 73 20 62 69 6e 64 69 6e e same as bindin
1bc3a 67 20 61 20 4e 55 4c 4c 20 76 61 6c 75 65 20 74 g a NULL value t
1bc3b 6f 20 74 68 65 20 63 6f 6c 75 6d 6e 2e 20 49 66 o the column. If
1bc3c 20 74 68 65 20 22 69 22 20 70 61 72 61 6d 65 74 the "i" paramet
1bc3d 65 72 20 69 73 0a 2a 2a 20 6f 75 74 20 6f 66 20 er is.** out of
1bc3e 72 61 6e 67 65 2c 20 74 68 65 6e 20 53 51 4c 49 range, then SQLI
1bc3f 54 45 5f 52 41 4e 47 45 20 69 73 20 72 65 74 75 TE_RANGE is retu
1bc40 72 6e 65 64 2e 20 4f 74 68 65 77 69 73 65 20 53 rned. Othewise S
1bc41 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20 QLITE_OK..**.**
1bc42 41 20 73 75 63 63 65 73 73 66 75 6c 20 65 76 61 A successful eva
1bc43 6c 75 61 74 69 6f 6e 20 6f 66 20 74 68 69 73 20 luation of this
1bc44 72 6f 75 74 69 6e 65 20 61 63 71 75 69 72 65 73 routine acquires
1bc45 20 74 68 65 20 6d 75 74 65 78 20 6f 6e 20 70 2e the mutex on p.
1bc46 0a 2a 2a 20 74 68 65 20 6d 75 74 65 78 20 69 73 .** the mutex is
1bc47 20 72 65 6c 65 61 73 65 64 20 69 66 20 61 6e 79 released if any
1bc48 20 6b 69 6e 64 20 6f 66 20 65 72 72 6f 72 20 6f kind of error o
1bc49 63 63 75 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ccurs..**.** The
1bc4a 20 65 72 72 6f 72 20 63 6f 64 65 20 73 74 6f 72 error code stor
1bc4b 65 64 20 69 6e 20 64 61 74 61 62 61 73 65 20 70 ed in database p
1bc4c 2d 3e 64 62 20 69 73 20 6f 76 65 72 77 72 69 74 ->db is overwrit
1bc4d 74 65 6e 20 77 69 74 68 20 74 68 65 20 72 65 74 ten with the ret
1bc4e 75 72 6e 0a 2a 2a 20 76 61 6c 75 65 20 69 6e 20 urn.** value in
1bc4f 61 6e 79 20 63 61 73 65 2e 0a 2a 2f 0a 73 74 61 any case..*/.sta
1bc50 74 69 63 20 69 6e 74 20 76 64 62 65 55 6e 62 69 tic int vdbeUnbi
1bc51 6e 64 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 nd(Vdbe *p, int
1bc52 69 29 7b 0a 20 20 4d 65 6d 20 2a 70 56 61 72 3b i){. Mem *pVar;
1bc53 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 . if( p==0 ) re
1bc54 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 turn SQLITE_MISU
1bc55 53 45 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 SE;. sqlite3_mu
1bc56 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e 64 62 2d tex_enter(p->db-
1bc57 3e 6d 75 74 65 78 29 3b 0a 20 20 69 66 28 20 70 >mutex);. if( p
1bc58 2d 3e 6d 61 67 69 63 21 3d 56 44 42 45 5f 4d 41 ->magic!=VDBE_MA
1bc59 47 49 43 5f 52 55 4e 20 7c 7c 20 70 2d 3e 70 63 GIC_RUN || p->pc
1bc5a 3e 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 >=0 ){. sqlit
1bc5b 65 33 45 72 72 6f 72 28 70 2d 3e 64 62 2c 20 53 e3Error(p->db, S
1bc5c 51 4c 49 54 45 5f 4d 49 53 55 53 45 2c 20 30 29 QLITE_MISUSE, 0)
1bc5d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 ;. sqlite3_mu
1bc5e 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d tex_leave(p->db-
1bc5f 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 72 65 74 >mutex);. ret
1bc60 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 urn SQLITE_MISUS
1bc61 45 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 3c 31 E;. }. if( i<1
1bc62 20 7c 7c 20 69 3e 70 2d 3e 6e 56 61 72 20 29 7b || i>p->nVar ){
1bc63 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f . sqlite3Erro
1bc64 72 28 70 2d 3e 64 62 2c 20 53 51 4c 49 54 45 5f r(p->db, SQLITE_
1bc65 52 41 4e 47 45 2c 20 30 29 3b 0a 20 20 20 20 73 RANGE, 0);. s
1bc66 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
1bc67 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 ve(p->db->mutex)
1bc68 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c ;. return SQL
1bc69 49 54 45 5f 52 41 4e 47 45 3b 0a 20 20 7d 0a 20 ITE_RANGE;. }.
1bc6a 20 69 2d 2d 3b 0a 20 20 70 56 61 72 20 3d 20 26 i--;. pVar = &
1bc6b 70 2d 3e 61 56 61 72 5b 69 5d 3b 0a 20 20 73 71 p->aVar[i];. sq
1bc6c 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 lite3VdbeMemRele
1bc6d 61 73 65 28 70 56 61 72 29 3b 0a 20 20 70 56 61 ase(pVar);. pVa
1bc6e 72 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e r->flags = MEM_N
1bc6f 75 6c 6c 3b 0a 20 20 73 71 6c 69 74 65 33 45 72 ull;. sqlite3Er
1bc70 72 6f 72 28 70 2d 3e 64 62 2c 20 53 51 4c 49 54 ror(p->db, SQLIT
1bc71 45 5f 4f 4b 2c 20 30 29 3b 0a 20 20 72 65 74 75 E_OK, 0);. retu
1bc72 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
1bc73 0a 2f 2a 0a 2a 2a 20 42 69 6e 64 20 61 20 74 65 ./*.** Bind a te
1bc74 78 74 20 6f 72 20 42 4c 4f 42 20 76 61 6c 75 65 xt or BLOB value
1bc75 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
1bc76 62 69 6e 64 54 65 78 74 28 0a 20 20 73 71 6c 69 bindText(. sqli
1bc77 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c te3_stmt *pStmt,
1bc78 20 20 20 2f 2a 20 54 68 65 20 73 74 61 74 65 6d /* The statem
1bc79 65 6e 74 20 74 6f 20 62 69 6e 64 20 61 67 61 69 ent to bind agai
1bc7a 6e 73 74 20 2a 2f 0a 20 20 69 6e 74 20 69 2c 20 nst */. int i,
1bc7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1bc7c 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74 68 65 20 /* Index of the
1bc7d 70 61 72 61 6d 65 74 65 72 20 74 6f 20 62 69 6e parameter to bin
1bc7e 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 d */. const voi
1bc7f 64 20 2a 7a 44 61 74 61 2c 20 20 20 20 20 2f 2a d *zData, /*
1bc80 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 Pointer to the
1bc81 64 61 74 61 20 74 6f 20 62 65 20 62 6f 75 6e 64 data to be bound
1bc82 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 61 74 61 2c */. int nData,
1bc83 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1bc84 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 Number of bytes
1bc85 6f 66 20 64 61 74 61 20 74 6f 20 62 65 20 62 6f of data to be bo
1bc86 75 6e 64 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a und */. void (*
1bc87 78 44 65 6c 29 28 76 6f 69 64 2a 29 2c 20 20 20 xDel)(void*),
1bc88 2f 2a 20 44 65 73 74 72 75 63 74 6f 72 20 66 6f /* Destructor fo
1bc89 72 20 74 68 65 20 64 61 74 61 20 2a 2f 0a 20 20 r the data */.
1bc8a 75 38 20 65 6e 63 6f 64 69 6e 67 20 20 20 20 20 u8 encoding
1bc8b 20 20 20 20 20 20 20 2f 2a 20 45 6e 63 6f 64 69 /* Encodi
1bc8c 6e 67 20 66 6f 72 20 74 68 65 20 64 61 74 61 20 ng for the data
1bc8d 2a 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a 70 20 */.){. Vdbe *p
1bc8e 3d 20 28 56 64 62 65 20 2a 29 70 53 74 6d 74 3b = (Vdbe *)pStmt;
1bc8f 0a 20 20 4d 65 6d 20 2a 70 56 61 72 3b 0a 20 20 . Mem *pVar;.
1bc90 69 6e 74 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 int rc;.. rc =
1bc91 76 64 62 65 55 6e 62 69 6e 64 28 70 2c 20 69 29 vdbeUnbind(p, i)
1bc92 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 ;. if( rc==SQLI
1bc93 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 TE_OK ){. if(
1bc94 20 7a 44 61 74 61 21 3d 30 20 29 7b 0a 20 20 20 zData!=0 ){.
1bc95 20 20 20 70 56 61 72 20 3d 20 26 70 2d 3e 61 56 pVar = &p->aV
1bc96 61 72 5b 69 2d 31 5d 3b 0a 20 20 20 20 20 20 72 ar[i-1];. r
1bc97 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d c = sqlite3VdbeM
1bc98 65 6d 53 65 74 53 74 72 28 70 56 61 72 2c 20 7a emSetStr(pVar, z
1bc99 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 65 6e 63 Data, nData, enc
1bc9a 6f 64 69 6e 67 2c 20 78 44 65 6c 29 3b 0a 20 20 oding, xDel);.
1bc9b 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
1bc9c 54 45 5f 4f 4b 20 26 26 20 65 6e 63 6f 64 69 6e TE_OK && encodin
1bc9d 67 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 g!=0 ){.
1bc9e 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 rc = sqlite3Vdbe
1bc9f 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70 ChangeEncoding(p
1bca0 56 61 72 2c 20 45 4e 43 28 70 2d 3e 64 62 29 29 Var, ENC(p->db))
1bca1 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
1bca2 73 71 6c 69 74 65 33 45 72 72 6f 72 28 70 2d 3e sqlite3Error(p->
1bca3 64 62 2c 20 72 63 2c 20 30 29 3b 0a 20 20 20 20 db, rc, 0);.
1bca4 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 rc = sqlite3Ap
1bca5 69 45 78 69 74 28 70 2d 3e 64 62 2c 20 72 63 29 iExit(p->db, rc)
1bca6 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 ;. }. sqli
1bca7 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
1bca8 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 p->db->mutex);.
1bca9 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
1bcaa 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 42 69 6e 64 20 61 }.../*.** Bind a
1bcab 20 62 6c 6f 62 20 76 61 6c 75 65 20 74 6f 20 61 blob value to a
1bcac 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 n SQL statement
1bcad 76 61 72 69 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c variable..*/.SQL
1bcae 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
1bcaf 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 28 0a 20 te3_bind_blob(.
1bcb0 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 sqlite3_stmt *p
1bcb1 53 74 6d 74 2c 20 0a 20 20 69 6e 74 20 69 2c 20 Stmt, . int i,
1bcb2 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a . const void *z
1bcb3 44 61 74 61 2c 20 0a 20 20 69 6e 74 20 6e 44 61 Data, . int nDa
1bcb4 74 61 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78 44 ta, . void (*xD
1bcb5 65 6c 29 28 76 6f 69 64 2a 29 0a 29 7b 0a 20 20 el)(void*).){.
1bcb6 72 65 74 75 72 6e 20 62 69 6e 64 54 65 78 74 28 return bindText(
1bcb7 70 53 74 6d 74 2c 20 69 2c 20 7a 44 61 74 61 2c pStmt, i, zData,
1bcb8 20 6e 44 61 74 61 2c 20 78 44 65 6c 2c 20 30 29 nData, xDel, 0)
1bcb9 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 ;.}.SQLITE_API i
1bcba 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f nt sqlite3_bind_
1bcbb 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 5f 73 double(sqlite3_s
1bcbc 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 tmt *pStmt, int
1bcbd 69 2c 20 64 6f 75 62 6c 65 20 72 56 61 6c 75 65 i, double rValue
1bcbe 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 56 ){. int rc;. V
1bcbf 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 20 2a dbe *p = (Vdbe *
1bcc0 29 70 53 74 6d 74 3b 0a 20 20 72 63 20 3d 20 76 )pStmt;. rc = v
1bcc1 64 62 65 55 6e 62 69 6e 64 28 70 2c 20 69 29 3b dbeUnbind(p, i);
1bcc2 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
1bcc3 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 E_OK ){. sqli
1bcc4 74 65 33 56 64 62 65 4d 65 6d 53 65 74 44 6f 75 te3VdbeMemSetDou
1bcc5 62 6c 65 28 26 70 2d 3e 61 56 61 72 5b 69 2d 31 ble(&p->aVar[i-1
1bcc6 5d 2c 20 72 56 61 6c 75 65 29 3b 0a 20 20 20 20 ], rValue);.
1bcc7 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 sqlite3_mutex_le
1bcc8 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 ave(p->db->mutex
1bcc9 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
1bcca 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 rc;.}.SQLITE_API
1bccb 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e int sqlite3_bin
1bccc 64 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 d_int(sqlite3_st
1bccd 6d 74 20 2a 70 2c 20 69 6e 74 20 69 2c 20 69 6e mt *p, int i, in
1bcce 74 20 69 56 61 6c 75 65 29 7b 0a 20 20 72 65 74 t iValue){. ret
1bccf 75 72 6e 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 urn sqlite3_bind
1bcd0 5f 69 6e 74 36 34 28 70 2c 20 69 2c 20 28 69 36 _int64(p, i, (i6
1bcd1 34 29 69 56 61 6c 75 65 29 3b 0a 7d 0a 53 51 4c 4)iValue);.}.SQL
1bcd2 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
1bcd3 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 73 te3_bind_int64(s
1bcd4 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 qlite3_stmt *pSt
1bcd5 6d 74 2c 20 69 6e 74 20 69 2c 20 73 71 6c 69 74 mt, int i, sqlit
1bcd6 65 5f 69 6e 74 36 34 20 69 56 61 6c 75 65 29 7b e_int64 iValue){
1bcd7 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 56 64 62 . int rc;. Vdb
1bcd8 65 20 2a 70 20 3d 20 28 56 64 62 65 20 2a 29 70 e *p = (Vdbe *)p
1bcd9 53 74 6d 74 3b 0a 20 20 72 63 20 3d 20 76 64 62 Stmt;. rc = vdb
1bcda 65 55 6e 62 69 6e 64 28 70 2c 20 69 29 3b 0a 20 eUnbind(p, i);.
1bcdb 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
1bcdc 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 OK ){. sqlite
1bcdd 33 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 36 34 3VdbeMemSetInt64
1bcde 28 26 70 2d 3e 61 56 61 72 5b 69 2d 31 5d 2c 20 (&p->aVar[i-1],
1bcdf 69 56 61 6c 75 65 29 3b 0a 20 20 20 20 73 71 6c iValue);. sql
1bce0 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 ite3_mutex_leave
1bce1 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a (p->db->mutex);.
1bce2 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
1bce3 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e .}.SQLITE_API in
1bce4 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 6e t sqlite3_bind_n
1bce5 75 6c 6c 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 ull(sqlite3_stmt
1bce6 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b *pStmt, int i){
1bce7 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 56 64 62 . int rc;. Vdb
1bce8 65 20 2a 70 20 3d 20 28 56 64 62 65 2a 29 70 53 e *p = (Vdbe*)pS
1bce9 74 6d 74 3b 0a 20 20 72 63 20 3d 20 76 64 62 65 tmt;. rc = vdbe
1bcea 55 6e 62 69 6e 64 28 70 2c 20 69 29 3b 0a 20 20 Unbind(p, i);.
1bceb 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
1bcec 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 K ){. sqlite3
1bced 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e _mutex_leave(p->
1bcee 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a db->mutex);. }.
1bcef 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 return rc;.}.S
1bcf0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
1bcf1 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 28 lite3_bind_text(
1bcf2 20 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 . sqlite3_stmt
1bcf3 20 2a 70 53 74 6d 74 2c 20 0a 20 20 69 6e 74 20 *pStmt, . int
1bcf4 69 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 i, . const char
1bcf5 20 2a 7a 44 61 74 61 2c 20 0a 20 20 69 6e 74 20 *zData, . int
1bcf6 6e 44 61 74 61 2c 20 0a 20 20 76 6f 69 64 20 28 nData, . void (
1bcf7 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 0a 29 7b *xDel)(void*).){
1bcf8 0a 20 20 72 65 74 75 72 6e 20 62 69 6e 64 54 65 . return bindTe
1bcf9 78 74 28 70 53 74 6d 74 2c 20 69 2c 20 7a 44 61 xt(pStmt, i, zDa
1bcfa 74 61 2c 20 6e 44 61 74 61 2c 20 78 44 65 6c 2c ta, nData, xDel,
1bcfb 20 53 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a 7d SQLITE_UTF8);.}
1bcfc 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1bcfd 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 OMIT_UTF16.SQLIT
1bcfe 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
1bcff 33 5f 62 69 6e 64 5f 74 65 78 74 31 36 28 0a 20 3_bind_text16(.
1bd00 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 sqlite3_stmt *p
1bd01 53 74 6d 74 2c 20 0a 20 20 69 6e 74 20 69 2c 20 Stmt, . int i,
1bd02 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a . const void *z
1bd03 44 61 74 61 2c 20 0a 20 20 69 6e 74 20 6e 44 61 Data, . int nDa
1bd04 74 61 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78 44 ta, . void (*xD
1bd05 65 6c 29 28 76 6f 69 64 2a 29 0a 29 7b 0a 20 20 el)(void*).){.
1bd06 72 65 74 75 72 6e 20 62 69 6e 64 54 65 78 74 28 return bindText(
1bd07 70 53 74 6d 74 2c 20 69 2c 20 7a 44 61 74 61 2c pStmt, i, zData,
1bd08 20 6e 44 61 74 61 2c 20 78 44 65 6c 2c 20 53 51 nData, xDel, SQ
1bd09 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 LITE_UTF16NATIVE
1bd0a 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 );.}.#endif /* S
1bd0b 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 QLITE_OMIT_UTF16
1bd0c 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 */.SQLITE_API i
1bd0d 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f nt sqlite3_bind_
1bd0e 76 61 6c 75 65 28 73 71 6c 69 74 65 33 5f 73 74 value(sqlite3_st
1bd0f 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 mt *pStmt, int i
1bd10 2c 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f , const sqlite3_
1bd11 76 61 6c 75 65 20 2a 70 56 61 6c 75 65 29 7b 0a value *pValue){.
1bd12 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 77 69 74 int rc;. swit
1bd13 63 68 28 20 70 56 61 6c 75 65 2d 3e 74 79 70 65 ch( pValue->type
1bd14 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c ){. case SQL
1bd15 49 54 45 5f 49 4e 54 45 47 45 52 3a 20 7b 0a 20 ITE_INTEGER: {.
1bd16 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
1bd17 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70 53 74 3_bind_int64(pSt
1bd18 6d 74 2c 20 69 2c 20 70 56 61 6c 75 65 2d 3e 75 mt, i, pValue->u
1bd19 2e 69 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b .i);. break
1bd1a 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 ;. }. case
1bd1b 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 3a 20 7b SQLITE_FLOAT: {
1bd1c 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
1bd1d 74 65 33 5f 62 69 6e 64 5f 64 6f 75 62 6c 65 28 te3_bind_double(
1bd1e 70 53 74 6d 74 2c 20 69 2c 20 70 56 61 6c 75 65 pStmt, i, pValue
1bd1f 2d 3e 72 29 3b 0a 20 20 20 20 20 20 62 72 65 61 ->r);. brea
1bd20 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 k;. }. cas
1bd21 65 20 53 51 4c 49 54 45 5f 42 4c 4f 42 3a 20 7b e SQLITE_BLOB: {
1bd22 0a 20 20 20 20 20 20 69 66 28 20 70 56 61 6c 75 . if( pValu
1bd23 65 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a e->flags & MEM_Z
1bd24 65 72 6f 20 29 7b 0a 20 20 20 20 20 20 20 20 72 ero ){. r
1bd25 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 c = sqlite3_bind
1bd26 5f 7a 65 72 6f 62 6c 6f 62 28 70 53 74 6d 74 2c _zeroblob(pStmt,
1bd27 20 69 2c 20 70 56 61 6c 75 65 2d 3e 75 2e 6e 5a i, pValue->u.nZ
1bd28 65 72 6f 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 ero);. }els
1bd29 65 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 e{. rc =
1bd2a 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f sqlite3_bind_blo
1bd2b 62 28 70 53 74 6d 74 2c 20 69 2c 20 70 56 61 6c b(pStmt, i, pVal
1bd2c 75 65 2d 3e 7a 2c 20 70 56 61 6c 75 65 2d 3e 6e ue->z, pValue->n
1bd2d 2c 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e ,SQLITE_TRANSIEN
1bd2e 54 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 T);. }.
1bd2f 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
1bd30 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 54 case SQLITE_T
1bd31 45 58 54 3a 20 7b 0a 20 20 20 20 20 20 72 63 20 EXT: {. rc
1bd32 3d 20 62 69 6e 64 54 65 78 74 28 70 53 74 6d 74 = bindText(pStmt
1bd33 2c 69 2c 20 20 70 56 61 6c 75 65 2d 3e 7a 2c 20 ,i, pValue->z,
1bd34 70 56 61 6c 75 65 2d 3e 6e 2c 20 53 51 4c 49 54 pValue->n, SQLIT
1bd35 45 5f 54 52 41 4e 53 49 45 4e 54 2c 0a 20 20 20 E_TRANSIENT,.
1bd36 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1bd37 20 20 20 20 20 20 20 20 20 20 20 70 56 61 6c 75 pValu
1bd38 65 2d 3e 65 6e 63 29 3b 0a 20 20 20 20 20 20 62 e->enc);. b
1bd39 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
1bd3a 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 default: {.
1bd3b 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69 rc = sqlite3_bi
1bd3c 6e 64 5f 6e 75 6c 6c 28 70 53 74 6d 74 2c 20 69 nd_null(pStmt, i
1bd3d 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a );. break;.
1bd3e 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
1bd3f 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f rn rc;.}.SQLITE_
1bd40 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
1bd41 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 28 73 71 bind_zeroblob(sq
1bd42 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d lite3_stmt *pStm
1bd43 74 2c 20 69 6e 74 20 69 2c 20 69 6e 74 20 6e 29 t, int i, int n)
1bd44 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 56 64 {. int rc;. Vd
1bd45 62 65 20 2a 70 20 3d 20 28 56 64 62 65 20 2a 29 be *p = (Vdbe *)
1bd46 70 53 74 6d 74 3b 0a 20 20 72 63 20 3d 20 76 64 pStmt;. rc = vd
1bd47 62 65 55 6e 62 69 6e 64 28 70 2c 20 69 29 3b 0a beUnbind(p, i);.
1bd48 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
1bd49 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 _OK ){. sqlit
1bd4a 65 33 56 64 62 65 4d 65 6d 53 65 74 5a 65 72 6f e3VdbeMemSetZero
1bd4b 42 6c 6f 62 28 26 70 2d 3e 61 56 61 72 5b 69 2d Blob(&p->aVar[i-
1bd4c 31 5d 2c 20 6e 29 3b 0a 20 20 20 20 73 71 6c 69 1], n);. sqli
1bd4d 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
1bd4e 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 p->db->mutex);.
1bd4f 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
1bd50 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
1bd51 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 77 69 the number of wi
1bd52 6c 64 63 61 72 64 73 20 74 68 61 74 20 63 61 6e ldcards that can
1bd53 20 62 65 20 70 6f 74 65 6e 74 69 61 6c 6c 79 20 be potentially
1bd54 62 6f 75 6e 64 20 74 6f 2e 0a 2a 2a 20 54 68 69 bound to..** Thi
1bd55 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 64 64 s routine is add
1bd56 65 64 20 74 6f 20 73 75 70 70 6f 72 74 20 44 42 ed to support DB
1bd57 44 3a 3a 53 51 4c 69 74 65 2e 20 20 0a 2a 2f 0a D::SQLite. .*/.
1bd58 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
1bd59 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 qlite3_bind_para
1bd5a 6d 65 74 65 72 5f 63 6f 75 6e 74 28 73 71 6c 69 meter_count(sqli
1bd5b 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 te3_stmt *pStmt)
1bd5c 7b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56 {. Vdbe *p = (V
1bd5d 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20 72 65 dbe*)pStmt;. re
1bd5e 74 75 72 6e 20 70 20 3f 20 70 2d 3e 6e 56 61 72 turn p ? p->nVar
1bd5f 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 : 0;.}../*.** C
1bd60 72 65 61 74 65 20 61 20 6d 61 70 70 69 6e 67 20 reate a mapping
1bd61 66 72 6f 6d 20 76 61 72 69 61 62 6c 65 20 6e 75 from variable nu
1bd62 6d 62 65 72 73 20 74 6f 20 76 61 72 69 61 62 6c mbers to variabl
1bd63 65 20 6e 61 6d 65 73 0a 2a 2a 20 69 6e 20 74 68 e names.** in th
1bd64 65 20 56 64 62 65 2e 61 7a 56 61 72 5b 5d 20 61 e Vdbe.azVar[] a
1bd65 72 72 61 79 2c 20 69 66 20 73 75 63 68 20 61 20 rray, if such a
1bd66 6d 61 70 70 69 6e 67 20 64 6f 65 73 20 6e 6f 74 mapping does not
1bd67 20 61 6c 72 65 61 64 79 0a 2a 2a 20 65 78 69 73 already.** exis
1bd68 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 t..*/.static voi
1bd69 64 20 63 72 65 61 74 65 56 61 72 4d 61 70 28 56 d createVarMap(V
1bd6a 64 62 65 20 2a 70 29 7b 0a 20 20 69 66 28 20 21 dbe *p){. if( !
1bd6b 70 2d 3e 6f 6b 56 61 72 20 29 7b 0a 20 20 20 20 p->okVar ){.
1bd6c 69 6e 74 20 6a 3b 0a 20 20 20 20 4f 70 20 2a 70 int j;. Op *p
1bd6d 4f 70 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f Op;. sqlite3_
1bd6e 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e 64 mutex_enter(p->d
1bd6f 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 2f b->mutex);. /
1bd70 2a 20 54 68 65 20 72 61 63 65 20 63 6f 6e 64 69 * The race condi
1bd71 74 69 6f 6e 20 68 65 72 65 20 69 73 20 68 61 72 tion here is har
1bd72 6d 6c 65 73 73 2e 20 20 49 66 20 74 77 6f 20 74 mless. If two t
1bd73 68 72 65 61 64 73 20 63 61 6c 6c 20 74 68 69 73 hreads call this
1bd74 0a 20 20 20 20 2a 2a 20 72 6f 75 74 69 6e 65 20 . ** routine
1bd75 6f 6e 20 74 68 65 20 73 61 6d 65 20 56 64 62 65 on the same Vdbe
1bd76 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d at the same tim
1bd77 65 2c 20 74 68 65 79 20 62 6f 74 68 20 6d 69 67 e, they both mig
1bd78 68 74 20 65 6e 64 0a 20 20 20 20 2a 2a 20 75 70 ht end. ** up
1bd79 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 74 68 initializing th
1bd7a 65 20 56 64 62 65 2e 61 7a 56 61 72 5b 5d 20 61 e Vdbe.azVar[] a
1bd7b 72 72 61 79 2e 20 20 54 68 61 74 20 69 73 20 61 rray. That is a
1bd7c 20 6c 69 74 74 6c 65 20 65 78 74 72 61 0a 20 20 little extra.
1bd7d 20 20 2a 2a 20 77 6f 72 6b 20 62 75 74 20 69 74 ** work but it
1bd7e 20 72 65 73 75 6c 74 73 20 69 6e 20 74 68 65 20 results in the
1bd7f 73 61 6d 65 20 61 6e 73 77 65 72 2e 0a 20 20 20 same answer..
1bd80 20 2a 2f 0a 20 20 20 20 66 6f 72 28 6a 3d 30 2c */. for(j=0,
1bd81 20 70 4f 70 3d 70 2d 3e 61 4f 70 3b 20 6a 3c 70 pOp=p->aOp; j<p
1bd82 2d 3e 6e 4f 70 3b 20 6a 2b 2b 2c 20 70 4f 70 2b ->nOp; j++, pOp+
1bd83 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 4f +){. if( pO
1bd84 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56 61 p->opcode==OP_Va
1bd85 72 69 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 riable ){.
1bd86 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
1bd87 31 3e 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 3d 1>0 && pOp->p1<=
1bd88 70 2d 3e 6e 56 61 72 20 29 3b 0a 20 20 20 20 20 p->nVar );.
1bd89 20 20 20 70 2d 3e 61 7a 56 61 72 5b 70 4f 70 2d p->azVar[pOp-
1bd8a 3e 70 31 2d 31 5d 20 3d 20 70 4f 70 2d 3e 70 34 >p1-1] = pOp->p4
1bd8b 2e 7a 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 .z;. }.
1bd8c 7d 0a 20 20 20 20 70 2d 3e 6f 6b 56 61 72 20 3d }. p->okVar =
1bd8d 20 31 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 1;. sqlite3_
1bd8e 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 64 mutex_leave(p->d
1bd8f 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 7d b->mutex);. }.}
1bd90 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ../*.** Return t
1bd91 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 77 69 6c he name of a wil
1bd92 64 63 61 72 64 20 70 61 72 61 6d 65 74 65 72 2e dcard parameter.
1bd93 20 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 Return NULL if
1bd94 20 74 68 65 20 69 6e 64 65 78 0a 2a 2a 20 69 73 the index.** is
1bd95 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 20 6f 72 out of range or
1bd96 20 69 66 20 74 68 65 20 77 69 6c 64 63 61 72 64 if the wildcard
1bd97 20 69 73 20 75 6e 6e 61 6d 65 64 2e 0a 2a 2a 0a is unnamed..**.
1bd98 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 20 69 73 ** The result is
1bd99 20 61 6c 77 61 79 73 20 55 54 46 2d 38 2e 0a 2a always UTF-8..*
1bd9a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e /.SQLITE_API con
1bd9b 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 st char *sqlite3
1bd9c 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f _bind_parameter_
1bd9d 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d name(sqlite3_stm
1bd9e 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 t *pStmt, int i)
1bd9f 7b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56 {. Vdbe *p = (V
1bda0 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20 69 66 dbe*)pStmt;. if
1bda1 28 20 70 3d 3d 30 20 7c 7c 20 69 3c 31 20 7c 7c ( p==0 || i<1 ||
1bda2 20 69 3e 70 2d 3e 6e 56 61 72 20 29 7b 0a 20 20 i>p->nVar ){.
1bda3 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a return 0;. }.
1bda4 20 20 63 72 65 61 74 65 56 61 72 4d 61 70 28 70 createVarMap(p
1bda5 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 61 );. return p->a
1bda6 7a 56 61 72 5b 69 2d 31 5d 3b 0a 7d 0a 0a 2f 2a zVar[i-1];.}../*
1bda7 0a 2a 2a 20 47 69 76 65 6e 20 61 20 77 69 6c 64 .** Given a wild
1bda8 63 61 72 64 20 70 61 72 61 6d 65 74 65 72 20 6e card parameter n
1bda9 61 6d 65 2c 20 72 65 74 75 72 6e 20 74 68 65 20 ame, return the
1bdaa 69 6e 64 65 78 20 6f 66 20 74 68 65 20 76 61 72 index of the var
1bdab 69 61 62 6c 65 0a 2a 2a 20 77 69 74 68 20 74 68 iable.** with th
1bdac 61 74 20 6e 61 6d 65 2e 20 20 49 66 20 74 68 65 at name. If the
1bdad 72 65 20 69 73 20 6e 6f 20 76 61 72 69 61 62 6c re is no variabl
1bdae 65 20 77 69 74 68 20 74 68 65 20 67 69 76 65 6e e with the given
1bdaf 20 6e 61 6d 65 2c 0a 2a 2a 20 72 65 74 75 72 6e name,.** return
1bdb0 20 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 0..*/.SQLITE_AP
1bdb1 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 I int sqlite3_bi
1bdb2 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e 64 nd_parameter_ind
1bdb3 65 78 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 ex(sqlite3_stmt
1bdb4 2a 70 53 74 6d 74 2c 20 63 6f 6e 73 74 20 63 68 *pStmt, const ch
1bdb5 61 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20 56 64 ar *zName){. Vd
1bdb6 62 65 20 2a 70 20 3d 20 28 56 64 62 65 2a 29 70 be *p = (Vdbe*)p
1bdb7 53 74 6d 74 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 Stmt;. int i;.
1bdb8 20 69 66 28 20 70 3d 3d 30 20 29 7b 0a 20 20 20 if( p==0 ){.
1bdb9 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 return 0;. }.
1bdba 20 63 72 65 61 74 65 56 61 72 4d 61 70 28 70 29 createVarMap(p)
1bdbb 3b 20 0a 20 20 69 66 28 20 7a 4e 61 6d 65 20 29 ; . if( zName )
1bdbc 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 {. for(i=0; i
1bdbd 3c 70 2d 3e 6e 56 61 72 3b 20 69 2b 2b 29 7b 0a <p->nVar; i++){.
1bdbe 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 const char
1bdbf 20 2a 7a 20 3d 20 70 2d 3e 61 7a 56 61 72 5b 69 *z = p->azVar[i
1bdc0 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 20 26 ];. if( z &
1bdc1 26 20 73 74 72 63 6d 70 28 7a 2c 7a 4e 61 6d 65 & strcmp(z,zName
1bdc2 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 )==0 ){.
1bdc3 72 65 74 75 72 6e 20 69 2b 31 3b 0a 20 20 20 20 return i+1;.
1bdc4 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 }. }. }.
1bdc5 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a return 0;.}../*.
1bdc6 2a 2a 20 54 72 61 6e 73 66 65 72 20 61 6c 6c 20 ** Transfer all
1bdc7 62 69 6e 64 69 6e 67 73 20 66 72 6f 6d 20 74 68 bindings from th
1bdc8 65 20 66 69 72 73 74 20 73 74 61 74 65 6d 65 6e e first statemen
1bdc9 74 20 6f 76 65 72 20 74 6f 20 74 68 65 20 73 65 t over to the se
1bdca 63 6f 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f cond..*/.SQLITE_
1bdcb 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
1bdcc 74 65 33 54 72 61 6e 73 66 65 72 42 69 6e 64 69 te3TransferBindi
1bdcd 6e 67 73 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 ngs(sqlite3_stmt
1bdce 20 2a 70 46 72 6f 6d 53 74 6d 74 2c 20 73 71 6c *pFromStmt, sql
1bdcf 69 74 65 33 5f 73 74 6d 74 20 2a 70 54 6f 53 74 ite3_stmt *pToSt
1bdd0 6d 74 29 7b 0a 20 20 56 64 62 65 20 2a 70 46 72 mt){. Vdbe *pFr
1bdd1 6f 6d 20 3d 20 28 56 64 62 65 2a 29 70 46 72 6f om = (Vdbe*)pFro
1bdd2 6d 53 74 6d 74 3b 0a 20 20 56 64 62 65 20 2a 70 mStmt;. Vdbe *p
1bdd3 54 6f 20 3d 20 28 56 64 62 65 2a 29 70 54 6f 53 To = (Vdbe*)pToS
1bdd4 74 6d 74 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 tmt;. int i;.
1bdd5 61 73 73 65 72 74 28 20 70 54 6f 2d 3e 64 62 3d assert( pTo->db=
1bdd6 3d 70 46 72 6f 6d 2d 3e 64 62 20 29 3b 0a 20 20 =pFrom->db );.
1bdd7 61 73 73 65 72 74 28 20 70 54 6f 2d 3e 6e 56 61 assert( pTo->nVa
1bdd8 72 3d 3d 70 46 72 6f 6d 2d 3e 6e 56 61 72 20 29 r==pFrom->nVar )
1bdd9 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 ;. sqlite3_mute
1bdda 78 5f 65 6e 74 65 72 28 70 54 6f 2d 3e 64 62 2d x_enter(pTo->db-
1bddb 3e 6d 75 74 65 78 29 3b 0a 20 20 66 6f 72 28 69 >mutex);. for(i
1bddc 3d 30 3b 20 69 3c 70 46 72 6f 6d 2d 3e 6e 56 61 =0; i<pFrom->nVa
1bddd 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c r; i++){. sql
1bdde 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28 ite3VdbeMemMove(
1bddf 26 70 54 6f 2d 3e 61 56 61 72 5b 69 5d 2c 20 26 &pTo->aVar[i], &
1bde0 70 46 72 6f 6d 2d 3e 61 56 61 72 5b 69 5d 29 3b pFrom->aVar[i]);
1bde1 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d . }. sqlite3_m
1bde2 75 74 65 78 5f 6c 65 61 76 65 28 70 54 6f 2d 3e utex_leave(pTo->
1bde3 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 db->mutex);. re
1bde4 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
1bde5 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 }..#ifndef SQLIT
1bde6 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45 E_OMIT_DEPRECATE
1bde7 44 0a 2f 2a 0a 2a 2a 20 44 65 70 72 65 63 61 74 D./*.** Deprecat
1bde8 65 64 20 65 78 74 65 72 6e 61 6c 20 69 6e 74 65 ed external inte
1bde9 72 66 61 63 65 2e 20 20 49 6e 74 65 72 6e 61 6c rface. Internal
1bdea 2f 63 6f 72 65 20 53 51 4c 69 74 65 20 63 6f 64 /core SQLite cod
1bdeb 65 0a 2a 2a 20 73 68 6f 75 6c 64 20 63 61 6c 6c e.** should call
1bdec 20 73 71 6c 69 74 65 33 54 72 61 6e 73 66 65 72 sqlite3Transfer
1bded 42 69 6e 64 69 6e 67 73 2e 0a 2a 2a 0a 2a 2a 20 Bindings..**.**
1bdee 49 73 20 69 73 20 6d 69 73 75 73 65 20 74 6f 20 Is is misuse to
1bdef 63 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 69 6e call this routin
1bdf0 65 20 77 69 74 68 20 73 74 61 74 65 6d 65 6e 74 e with statement
1bdf1 73 20 66 72 6f 6d 20 64 69 66 66 65 72 65 6e 74 s from different
1bdf2 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e .** database con
1bdf3 6e 65 63 74 69 6f 6e 73 2e 20 20 42 75 74 20 61 nections. But a
1bdf4 73 20 74 68 69 73 20 69 73 20 61 20 64 65 70 72 s this is a depr
1bdf5 65 63 61 74 65 64 20 69 6e 74 65 72 66 61 63 65 ecated interface
1bdf6 2c 20 77 65 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 , we.** will not
1bdf7 20 62 6f 74 68 65 72 20 74 6f 20 63 68 65 63 6b bother to check
1bdf8 20 66 6f 72 20 74 68 61 74 20 63 6f 6e 64 69 74 for that condit
1bdf9 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 ion..**.** If th
1bdfa 65 20 74 77 6f 20 73 74 61 74 65 6d 65 6e 74 73 e two statements
1bdfb 20 63 6f 6e 74 61 69 6e 20 61 20 64 69 66 66 65 contain a diffe
1bdfc 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20 62 rent number of b
1bdfd 69 6e 64 69 6e 67 73 2c 20 74 68 65 6e 0a 2a 2a indings, then.**
1bdfe 20 61 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 an SQLITE_ERROR
1bdff 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 4e is returned. N
1be00 6f 74 68 69 6e 67 20 65 6c 73 65 20 63 61 6e 20 othing else can
1be01 67 6f 20 77 72 6f 6e 67 2c 20 73 6f 20 6f 74 68 go wrong, so oth
1be02 65 72 77 69 73 65 0a 2a 2a 20 53 51 4c 49 54 45 erwise.** SQLITE
1be03 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e _OK is returned.
1be04 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
1be05 6e 74 20 73 71 6c 69 74 65 33 5f 74 72 61 6e 73 nt sqlite3_trans
1be06 66 65 72 5f 62 69 6e 64 69 6e 67 73 28 73 71 6c fer_bindings(sql
1be07 69 74 65 33 5f 73 74 6d 74 20 2a 70 46 72 6f 6d ite3_stmt *pFrom
1be08 53 74 6d 74 2c 20 73 71 6c 69 74 65 33 5f 73 74 Stmt, sqlite3_st
1be09 6d 74 20 2a 70 54 6f 53 74 6d 74 29 7b 0a 20 20 mt *pToStmt){.
1be0a 56 64 62 65 20 2a 70 46 72 6f 6d 20 3d 20 28 56 Vdbe *pFrom = (V
1be0b 64 62 65 2a 29 70 46 72 6f 6d 53 74 6d 74 3b 0a dbe*)pFromStmt;.
1be0c 20 20 56 64 62 65 20 2a 70 54 6f 20 3d 20 28 56 Vdbe *pTo = (V
1be0d 64 62 65 2a 29 70 54 6f 53 74 6d 74 3b 0a 20 20 dbe*)pToStmt;.
1be0e 69 66 28 20 70 46 72 6f 6d 2d 3e 6e 56 61 72 21 if( pFrom->nVar!
1be0f 3d 70 54 6f 2d 3e 6e 56 61 72 20 29 7b 0a 20 20 =pTo->nVar ){.
1be10 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
1be11 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 72 65 74 ERROR;. }. ret
1be12 75 72 6e 20 73 71 6c 69 74 65 33 54 72 61 6e 73 urn sqlite3Trans
1be13 66 65 72 42 69 6e 64 69 6e 67 73 28 70 46 72 6f ferBindings(pFro
1be14 6d 53 74 6d 74 2c 20 70 54 6f 53 74 6d 74 29 3b mStmt, pToStmt);
1be15 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a .}.#endif../*.**
1be16 20 52 65 74 75 72 6e 20 74 68 65 20 73 71 6c 69 Return the sqli
1be17 74 65 33 2a 20 64 61 74 61 62 61 73 65 20 68 61 te3* database ha
1be18 6e 64 6c 65 20 74 6f 20 77 68 69 63 68 20 74 68 ndle to which th
1be19 65 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 e prepared state
1be1a 6d 65 6e 74 20 67 69 76 65 6e 0a 2a 2a 20 69 6e ment given.** in
1be1b 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 62 65 the argument be
1be1c 6c 6f 6e 67 73 2e 20 20 54 68 69 73 20 69 73 20 longs. This is
1be1d 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 the same databas
1be1e 65 20 68 61 6e 64 6c 65 20 74 68 61 74 20 77 61 e handle that wa
1be1f 73 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 61 s.** the first a
1be20 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 73 rgument to the s
1be21 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 qlite3_prepare()
1be22 20 74 68 61 74 20 77 61 73 20 75 73 65 64 20 74 that was used t
1be23 6f 20 63 72 65 61 74 65 0a 2a 2a 20 74 68 65 20 o create.** the
1be24 73 74 61 74 65 6d 65 6e 74 20 69 6e 20 74 68 65 statement in the
1be25 20 66 69 72 73 74 20 70 6c 61 63 65 2e 0a 2a 2f first place..*/
1be26 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 .SQLITE_API sqli
1be27 74 65 33 20 2a 73 71 6c 69 74 65 33 5f 64 62 5f te3 *sqlite3_db_
1be28 68 61 6e 64 6c 65 28 73 71 6c 69 74 65 33 5f 73 handle(sqlite3_s
1be29 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20 72 tmt *pStmt){. r
1be2a 65 74 75 72 6e 20 70 53 74 6d 74 20 3f 20 28 28 eturn pStmt ? ((
1be2b 56 64 62 65 2a 29 70 53 74 6d 74 29 2d 3e 64 62 Vdbe*)pStmt)->db
1be2c 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 : 0;.}../*.** R
1be2d 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 eturn a pointer
1be2e 74 6f 20 74 68 65 20 6e 65 78 74 20 70 72 65 70 to the next prep
1be2f 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 61 ared statement a
1be30 66 74 65 72 20 70 53 74 6d 74 20 61 73 73 6f 63 fter pStmt assoc
1be31 69 61 74 65 64 0a 2a 2a 20 77 69 74 68 20 64 61 iated.** with da
1be32 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
1be33 6e 20 70 44 62 2e 20 20 49 66 20 70 53 74 6d 74 n pDb. If pStmt
1be34 20 69 73 20 4e 55 4c 4c 2c 20 72 65 74 75 72 6e is NULL, return
1be35 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 70 72 the first.** pr
1be36 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 epared statement
1be37 20 66 6f 72 20 74 68 65 20 64 61 74 61 62 61 73 for the databas
1be38 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 52 e connection. R
1be39 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 74 68 eturn NULL if th
1be3a 65 72 65 0a 2a 2a 20 61 72 65 20 6e 6f 20 6d 6f ere.** are no mo
1be3b 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 re..*/.SQLITE_AP
1be3c 49 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a I sqlite3_stmt *
1be3d 73 71 6c 69 74 65 33 5f 6e 65 78 74 5f 73 74 6d sqlite3_next_stm
1be3e 74 28 73 71 6c 69 74 65 33 20 2a 70 44 62 2c 20 t(sqlite3 *pDb,
1be3f 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 sqlite3_stmt *pS
1be40 74 6d 74 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f tmt){. sqlite3_
1be41 73 74 6d 74 20 2a 70 4e 65 78 74 3b 0a 20 20 73 stmt *pNext;. s
1be42 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 qlite3_mutex_ent
1be43 65 72 28 70 44 62 2d 3e 6d 75 74 65 78 29 3b 0a er(pDb->mutex);.
1be44 20 20 69 66 28 20 70 53 74 6d 74 3d 3d 30 20 29 if( pStmt==0 )
1be45 7b 0a 20 20 20 20 70 4e 65 78 74 20 3d 20 28 73 {. pNext = (s
1be46 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 70 44 62 qlite3_stmt*)pDb
1be47 2d 3e 70 56 64 62 65 3b 0a 20 20 7d 65 6c 73 65 ->pVdbe;. }else
1be48 7b 0a 20 20 20 20 70 4e 65 78 74 20 3d 20 28 73 {. pNext = (s
1be49 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 28 28 56 qlite3_stmt*)((V
1be4a 64 62 65 2a 29 70 53 74 6d 74 29 2d 3e 70 4e 65 dbe*)pStmt)->pNe
1be4b 78 74 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 xt;. }. sqlite
1be4c 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 44 3_mutex_leave(pD
1be4d 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 b->mutex);. ret
1be4e 75 72 6e 20 70 4e 65 78 74 3b 0a 7d 0a 0a 2f 2a urn pNext;.}../*
1be4f 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 76 .** Return the v
1be50 61 6c 75 65 20 6f 66 20 61 20 73 74 61 74 75 73 alue of a status
1be51 20 63 6f 75 6e 74 65 72 20 66 6f 72 20 61 20 70 counter for a p
1be52 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
1be53 74 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 t.*/.SQLITE_API
1be54 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 int sqlite3_stmt
1be55 5f 73 74 61 74 75 73 28 73 71 6c 69 74 65 33 5f _status(sqlite3_
1be56 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 stmt *pStmt, int
1be57 20 6f 70 2c 20 69 6e 74 20 72 65 73 65 74 46 6c op, int resetFl
1be58 61 67 29 7b 0a 20 20 56 64 62 65 20 2a 70 56 64 ag){. Vdbe *pVd
1be59 62 65 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d be = (Vdbe*)pStm
1be5a 74 3b 0a 20 20 69 6e 74 20 76 20 3d 20 70 56 64 t;. int v = pVd
1be5b 62 65 2d 3e 61 43 6f 75 6e 74 65 72 5b 6f 70 2d be->aCounter[op-
1be5c 31 5d 3b 0a 20 20 69 66 28 20 72 65 73 65 74 46 1];. if( resetF
1be5d 6c 61 67 20 29 20 70 56 64 62 65 2d 3e 61 43 6f lag ) pVdbe->aCo
1be5e 75 6e 74 65 72 5b 6f 70 2d 31 5d 20 3d 20 30 3b unter[op-1] = 0;
1be5f 0a 20 20 72 65 74 75 72 6e 20 76 3b 0a 7d 0a 0a . return v;.}..
1be60 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
1be61 45 6e 64 20 6f 66 20 76 64 62 65 61 70 69 2e 63 End of vdbeapi.c
1be62 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
1be63 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1be64 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
1be65 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
1be66 42 65 67 69 6e 20 66 69 6c 65 20 76 64 62 65 2e Begin file vdbe.
1be67 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
1be68 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1be69 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
1be6a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 /*.** 2001 Septe
1be6b 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 mber 15.**.** Th
1be6c 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 e author disclai
1be6d 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 ms copyright to
1be6e 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 this source code
1be6f 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a . In place of.*
1be70 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 * a legal notice
1be71 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 , here is a bles
1be72 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d sing:.**.** M
1be73 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 ay you do good a
1be74 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 nd not evil..**
1be75 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 May you find
1be76 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 forgiveness for
1be77 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 yourself and for
1be78 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 give others..**
1be79 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 May you share
1be7a 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 freely, never t
1be7b 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 aking more than
1be7c 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a you give..**.***
1be7d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1be7e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1be7f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1be80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1be81 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f ******.** The co
1be82 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 de in this file
1be83 69 6d 70 6c 65 6d 65 6e 74 73 20 65 78 65 63 75 implements execu
1be84 74 69 6f 6e 20 6d 65 74 68 6f 64 20 6f 66 20 74 tion method of t
1be85 68 65 20 0a 2a 2a 20 56 69 72 74 75 61 6c 20 44 he .** Virtual D
1be86 61 74 61 62 61 73 65 20 45 6e 67 69 6e 65 20 28 atabase Engine (
1be87 56 44 42 45 29 2e 20 20 41 20 73 65 70 61 72 61 VDBE). A separa
1be88 74 65 20 66 69 6c 65 20 28 22 76 64 62 65 61 75 te file ("vdbeau
1be89 78 2e 63 22 29 0a 2a 2a 20 68 61 6e 64 6c 65 73 x.c").** handles
1be8a 20 68 6f 75 73 65 6b 65 65 70 69 6e 67 20 64 65 housekeeping de
1be8b 74 61 69 6c 73 20 73 75 63 68 20 61 73 20 63 72 tails such as cr
1be8c 65 61 74 69 6e 67 20 61 6e 64 20 64 65 6c 65 74 eating and delet
1be8d 69 6e 67 0a 2a 2a 20 56 44 42 45 20 69 6e 73 74 ing.** VDBE inst
1be8e 61 6e 63 65 73 2e 20 20 54 68 69 73 20 66 69 6c ances. This fil
1be8f 65 20 69 73 20 73 6f 6c 65 6c 79 20 69 6e 74 65 e is solely inte
1be90 72 65 73 74 65 64 20 69 6e 20 65 78 65 63 75 74 rested in execut
1be91 69 6e 67 0a 2a 2a 20 74 68 65 20 56 44 42 45 20 ing.** the VDBE
1be92 70 72 6f 67 72 61 6d 2e 0a 2a 2a 0a 2a 2a 20 49 program..**.** I
1be93 6e 20 74 68 65 20 65 78 74 65 72 6e 61 6c 20 69 n the external i
1be94 6e 74 65 72 66 61 63 65 2c 20 61 6e 20 22 73 71 nterface, an "sq
1be95 6c 69 74 65 33 5f 73 74 6d 74 2a 22 20 69 73 20 lite3_stmt*" is
1be96 61 6e 20 6f 70 61 71 75 65 20 70 6f 69 6e 74 65 an opaque pointe
1be97 72 0a 2a 2a 20 74 6f 20 61 20 56 44 42 45 2e 0a r.** to a VDBE..
1be98 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 20 70 61 **.** The SQL pa
1be99 72 73 65 72 20 67 65 6e 65 72 61 74 65 73 20 61 rser generates a
1be9a 20 70 72 6f 67 72 61 6d 20 77 68 69 63 68 20 69 program which i
1be9b 73 20 74 68 65 6e 20 65 78 65 63 75 74 65 64 20 s then executed
1be9c 62 79 0a 2a 2a 20 74 68 65 20 56 44 42 45 20 74 by.** the VDBE t
1be9d 6f 20 64 6f 20 74 68 65 20 77 6f 72 6b 20 6f 66 o do the work of
1be9e 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 the SQL stateme
1be9f 6e 74 2e 20 20 56 44 42 45 20 70 72 6f 67 72 61 nt. VDBE progra
1bea0 6d 73 20 61 72 65 20 0a 2a 2a 20 73 69 6d 69 6c ms are .** simil
1bea1 61 72 20 69 6e 20 66 6f 72 6d 20 74 6f 20 61 73 ar in form to as
1bea2 73 65 6d 62 6c 79 20 6c 61 6e 67 75 61 67 65 2e sembly language.
1bea3 20 20 54 68 65 20 70 72 6f 67 72 61 6d 20 63 6f The program co
1bea4 6e 73 69 73 74 73 20 6f 66 0a 2a 2a 20 61 20 6c nsists of.** a l
1bea5 69 6e 65 61 72 20 73 65 71 75 65 6e 63 65 20 6f inear sequence o
1bea6 66 20 6f 70 65 72 61 74 69 6f 6e 73 2e 20 20 45 f operations. E
1bea7 61 63 68 20 6f 70 65 72 61 74 69 6f 6e 20 68 61 ach operation ha
1bea8 73 20 61 6e 20 6f 70 63 6f 64 65 20 0a 2a 2a 20 s an opcode .**
1bea9 61 6e 64 20 35 20 6f 70 65 72 61 6e 64 73 2e 20 and 5 operands.
1beaa 20 4f 70 65 72 61 6e 64 73 20 50 31 2c 20 50 32 Operands P1, P2
1beab 2c 20 61 6e 64 20 50 33 20 61 72 65 20 69 6e 74 , and P3 are int
1beac 65 67 65 72 73 2e 20 20 4f 70 65 72 61 6e 64 20 egers. Operand
1bead 50 34 20 0a 2a 2a 20 69 73 20 61 20 6e 75 6c 6c P4 .** is a null
1beae 2d 74 65 72 6d 69 6e 61 74 65 64 20 73 74 72 69 -terminated stri
1beaf 6e 67 2e 20 20 4f 70 65 72 61 6e 64 20 50 35 20 ng. Operand P5
1beb0 69 73 20 61 6e 20 75 6e 73 69 67 6e 65 64 20 63 is an unsigned c
1beb1 68 61 72 61 63 74 65 72 2e 0a 2a 2a 20 46 65 77 haracter..** Few
1beb2 20 6f 70 63 6f 64 65 73 20 75 73 65 20 61 6c 6c opcodes use all
1beb3 20 35 20 6f 70 65 72 61 6e 64 73 2e 0a 2a 2a 0a 5 operands..**.
1beb4 2a 2a 20 43 6f 6d 70 75 74 61 74 69 6f 6e 20 72 ** Computation r
1beb5 65 73 75 6c 74 73 20 61 72 65 20 73 74 6f 72 65 esults are store
1beb6 64 20 6f 6e 20 61 20 73 65 74 20 6f 66 20 72 65 d on a set of re
1beb7 67 69 73 74 65 72 73 20 6e 75 6d 62 65 72 65 64 gisters numbered
1beb8 20 62 65 67 69 6e 6e 69 6e 67 0a 2a 2a 20 77 69 beginning.** wi
1beb9 74 68 20 31 20 61 6e 64 20 67 6f 69 6e 67 20 75 th 1 and going u
1beba 70 20 74 6f 20 56 64 62 65 2e 6e 4d 65 6d 2e 20 p to Vdbe.nMem.
1bebb 20 45 61 63 68 20 72 65 67 69 73 74 65 72 20 63 Each register c
1bebc 61 6e 20 73 74 6f 72 65 0a 2a 2a 20 65 69 74 68 an store.** eith
1bebd 65 72 20 61 6e 20 69 6e 74 65 67 65 72 2c 20 61 er an integer, a
1bebe 20 6e 75 6c 6c 2d 74 65 72 6d 69 6e 61 74 65 64 null-terminated
1bebf 20 73 74 72 69 6e 67 2c 20 61 20 66 6c 6f 61 74 string, a float
1bec0 69 6e 67 20 70 6f 69 6e 74 0a 2a 2a 20 6e 75 6d ing point.** num
1bec1 62 65 72 2c 20 6f 72 20 74 68 65 20 53 51 4c 20 ber, or the SQL
1bec2 22 4e 55 4c 4c 22 20 76 61 6c 75 65 2e 20 20 41 "NULL" value. A
1bec3 6e 20 69 6d 70 6c 69 63 69 74 20 63 6f 6e 76 65 n implicit conve
1bec4 72 73 69 6f 6e 20 66 72 6f 6d 20 6f 6e 65 0a 2a rsion from one.*
1bec5 2a 20 74 79 70 65 20 74 6f 20 74 68 65 20 6f 74 * type to the ot
1bec6 68 65 72 20 6f 63 63 75 72 73 20 61 73 20 6e 65 her occurs as ne
1bec7 63 65 73 73 61 72 79 2e 0a 2a 2a 20 0a 2a 2a 20 cessary..** .**
1bec8 4d 6f 73 74 20 6f 66 20 74 68 65 20 63 6f 64 65 Most of the code
1bec9 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 69 73 in this file is
1beca 20 74 61 6b 65 6e 20 75 70 20 62 79 20 74 68 65 taken up by the
1becb 20 73 71 6c 69 74 65 33 56 64 62 65 45 78 65 63 sqlite3VdbeExec
1becc 28 29 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 77 ().** function w
1becd 68 69 63 68 20 64 6f 65 73 20 74 68 65 20 77 6f hich does the wo
1bece 72 6b 20 6f 66 20 69 6e 74 65 72 70 72 65 74 69 rk of interpreti
1becf 6e 67 20 61 20 56 44 42 45 20 70 72 6f 67 72 61 ng a VDBE progra
1bed0 6d 2e 0a 2a 2a 20 42 75 74 20 6f 74 68 65 72 20 m..** But other
1bed1 72 6f 75 74 69 6e 65 73 20 61 72 65 20 61 6c 73 routines are als
1bed2 6f 20 70 72 6f 76 69 64 65 64 20 74 6f 20 68 65 o provided to he
1bed3 6c 70 20 69 6e 20 62 75 69 6c 64 69 6e 67 20 75 lp in building u
1bed4 70 0a 2a 2a 20 61 20 70 72 6f 67 72 61 6d 20 69 p.** a program i
1bed5 6e 73 74 72 75 63 74 69 6f 6e 20 62 79 20 69 6e nstruction by in
1bed6 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a struction..**.**
1bed7 20 56 61 72 69 6f 75 73 20 73 63 72 69 70 74 73 Various scripts
1bed8 20 73 63 61 6e 20 74 68 69 73 20 73 6f 75 72 63 scan this sourc
1bed9 65 20 66 69 6c 65 20 69 6e 20 6f 72 64 65 72 20 e file in order
1beda 74 6f 20 67 65 6e 65 72 61 74 65 20 48 54 4d 4c to generate HTML
1bedb 0a 2a 2a 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f .** documentatio
1bedc 6e 2c 20 68 65 61 64 65 72 73 20 66 69 6c 65 73 n, headers files
1bedd 2c 20 6f 72 20 6f 74 68 65 72 20 64 65 72 69 76 , or other deriv
1bede 65 64 20 66 69 6c 65 73 2e 20 20 54 68 65 20 66 ed files. The f
1bedf 6f 72 6d 61 74 74 69 6e 67 0a 2a 2a 20 6f 66 20 ormatting.** of
1bee0 74 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73 the code in this
1bee1 20 66 69 6c 65 20 69 73 2c 20 74 68 65 72 65 66 file is, theref
1bee2 6f 72 65 2c 20 69 6d 70 6f 72 74 61 6e 74 2e 20 ore, important.
1bee3 20 53 65 65 20 6f 74 68 65 72 20 63 6f 6d 6d 65 See other comme
1bee4 6e 74 73 0a 2a 2a 20 69 6e 20 74 68 69 73 20 66 nts.** in this f
1bee5 69 6c 65 20 66 6f 72 20 64 65 74 61 69 6c 73 2e ile for details.
1bee6 20 20 49 66 20 69 6e 20 64 6f 75 62 74 2c 20 64 If in doubt, d
1bee7 6f 20 6e 6f 74 20 64 65 76 69 61 74 65 20 66 72 o not deviate fr
1bee8 6f 6d 20 65 78 69 73 74 69 6e 67 0a 2a 2a 20 63 om existing.** c
1bee9 6f 6d 6d 65 6e 74 69 6e 67 20 61 6e 64 20 69 6e ommenting and in
1beea 64 65 6e 74 61 74 69 6f 6e 20 70 72 61 63 74 69 dentation practi
1beeb 63 65 73 20 77 68 65 6e 20 63 68 61 6e 67 69 6e ces when changin
1beec 67 20 6f 72 20 61 64 64 69 6e 67 20 63 6f 64 65 g or adding code
1beed 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76 64 62 ..**.** $Id: vdb
1beee 65 2e 63 2c 76 20 31 2e 38 37 34 20 32 30 30 39 e.c,v 1.874 2009
1beef 2f 30 37 2f 32 34 20 31 37 3a 35 38 3a 35 33 20 /07/24 17:58:53
1bef0 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 danielk1977 Exp
1bef1 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 $.*/../*.** The
1bef2 66 6f 6c 6c 6f 77 69 6e 67 20 67 6c 6f 62 61 6c following global
1bef3 20 76 61 72 69 61 62 6c 65 20 69 73 20 69 6e 63 variable is inc
1bef4 72 65 6d 65 6e 74 65 64 20 65 76 65 72 79 20 74 remented every t
1bef5 69 6d 65 20 61 20 63 75 72 73 6f 72 0a 2a 2a 20 ime a cursor.**
1bef6 6d 6f 76 65 73 2c 20 65 69 74 68 65 72 20 62 79 moves, either by
1bef7 20 74 68 65 20 4f 50 5f 53 65 65 6b 58 58 2c 20 the OP_SeekXX,
1bef8 4f 50 5f 4e 65 78 74 2c 20 6f 72 20 4f 50 5f 50 OP_Next, or OP_P
1bef9 72 65 76 20 6f 70 63 6f 64 65 73 2e 20 20 54 68 rev opcodes. Th
1befa 65 20 74 65 73 74 0a 2a 2a 20 70 72 6f 63 65 64 e test.** proced
1befb 75 72 65 73 20 75 73 65 20 74 68 69 73 20 69 6e ures use this in
1befc 66 6f 72 6d 61 74 69 6f 6e 20 74 6f 20 6d 61 6b formation to mak
1befd 65 20 73 75 72 65 20 74 68 61 74 20 69 6e 64 69 e sure that indi
1befe 63 65 73 20 61 72 65 0a 2a 2a 20 77 6f 72 6b 69 ces are.** worki
1beff 6e 67 20 63 6f 72 72 65 63 74 6c 79 2e 20 20 54 ng correctly. T
1bf00 68 69 73 20 76 61 72 69 61 62 6c 65 20 68 61 73 his variable has
1bf01 20 6e 6f 20 66 75 6e 63 74 69 6f 6e 20 6f 74 68 no function oth
1bf02 65 72 20 74 68 61 6e 20 74 6f 0a 2a 2a 20 68 65 er than to.** he
1bf03 6c 70 20 76 65 72 69 66 79 20 74 68 65 20 63 6f lp verify the co
1bf04 72 72 65 63 74 20 6f 70 65 72 61 74 69 6f 6e 20 rrect operation
1bf05 6f 66 20 74 68 65 20 6c 69 62 72 61 72 79 2e 0a of the library..
1bf06 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
1bf07 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 _TEST.SQLITE_API
1bf08 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 65 61 int sqlite3_sea
1bf09 72 63 68 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 rch_count = 0;.#
1bf0a 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 endif../*.** Whe
1bf0b 6e 20 74 68 69 73 20 67 6c 6f 62 61 6c 20 76 61 n this global va
1bf0c 72 69 61 62 6c 65 20 69 73 20 70 6f 73 69 74 69 riable is positi
1bf0d 76 65 2c 20 69 74 20 67 65 74 73 20 64 65 63 72 ve, it gets decr
1bf0e 65 6d 65 6e 74 65 64 20 6f 6e 63 65 20 62 65 66 emented once bef
1bf0f 6f 72 65 0a 2a 2a 20 65 61 63 68 20 69 6e 73 74 ore.** each inst
1bf10 72 75 63 74 69 6f 6e 20 69 6e 20 74 68 65 20 56 ruction in the V
1bf11 44 42 45 2e 20 20 57 68 65 6e 20 72 65 61 63 68 DBE. When reach
1bf12 65 73 20 7a 65 72 6f 2c 20 74 68 65 20 75 31 2e es zero, the u1.
1bf13 69 73 49 6e 74 65 72 72 75 70 74 65 64 0a 2a 2a isInterrupted.**
1bf14 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20 73 71 field of the sq
1bf15 6c 69 74 65 33 20 73 74 72 75 63 74 75 72 65 20 lite3 structure
1bf16 69 73 20 73 65 74 20 69 6e 20 6f 72 64 65 72 20 is set in order
1bf17 74 6f 20 73 69 6d 75 6c 61 74 65 20 61 6e 64 20 to simulate and
1bf18 69 6e 74 65 72 72 75 70 74 2e 0a 2a 2a 0a 2a 2a interrupt..**.**
1bf19 20 54 68 69 73 20 66 61 63 69 6c 69 74 79 20 69 This facility i
1bf1a 73 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 s used for testi
1bf1b 6e 67 20 70 75 72 70 6f 73 65 73 20 6f 6e 6c 79 ng purposes only
1bf1c 2e 20 20 49 74 20 64 6f 65 73 20 6e 6f 74 20 66 . It does not f
1bf1d 75 6e 63 74 69 6f 6e 0a 2a 2a 20 69 6e 20 61 6e unction.** in an
1bf1e 20 6f 72 64 69 6e 61 72 79 20 62 75 69 6c 64 2e ordinary build.
1bf1f 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 .*/.#ifdef SQLIT
1bf20 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 E_TEST.SQLITE_AP
1bf21 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6e I int sqlite3_in
1bf22 74 65 72 72 75 70 74 5f 63 6f 75 6e 74 20 3d 20 terrupt_count =
1bf23 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 0;.#endif../*.**
1bf24 20 54 68 65 20 6e 65 78 74 20 67 6c 6f 62 61 6c The next global
1bf25 20 76 61 72 69 61 62 6c 65 20 69 73 20 69 6e 63 variable is inc
1bf26 72 65 6d 65 6e 74 65 64 20 65 61 63 68 20 74 79 remented each ty
1bf27 70 65 20 74 68 65 20 4f 50 5f 53 6f 72 74 20 6f pe the OP_Sort o
1bf28 70 63 6f 64 65 0a 2a 2a 20 69 73 20 65 78 65 63 pcode.** is exec
1bf29 75 74 65 64 2e 20 20 54 68 65 20 74 65 73 74 20 uted. The test
1bf2a 70 72 6f 63 65 64 75 72 65 73 20 75 73 65 20 74 procedures use t
1bf2b 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 his information
1bf2c 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 to make sure tha
1bf2d 74 0a 2a 2a 20 73 6f 72 74 69 6e 67 20 69 73 20 t.** sorting is
1bf2e 6f 63 63 75 72 72 69 6e 67 20 6f 72 20 6e 6f 74 occurring or not
1bf2f 20 6f 63 63 75 72 72 69 6e 67 20 61 74 20 61 70 occurring at ap
1bf30 70 72 6f 70 72 69 61 74 65 20 74 69 6d 65 73 2e propriate times.
1bf31 20 20 20 54 68 69 73 20 76 61 72 69 61 62 6c 65 This variable
1bf32 0a 2a 2a 20 68 61 73 20 6e 6f 20 66 75 6e 63 74 .** has no funct
1bf33 69 6f 6e 20 6f 74 68 65 72 20 74 68 61 6e 20 74 ion other than t
1bf34 6f 20 68 65 6c 70 20 76 65 72 69 66 79 20 74 68 o help verify th
1bf35 65 20 63 6f 72 72 65 63 74 20 6f 70 65 72 61 74 e correct operat
1bf36 69 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 6c 69 ion of the.** li
1bf37 62 72 61 72 79 2e 0a 2a 2f 0a 23 69 66 64 65 66 brary..*/.#ifdef
1bf38 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c SQLITE_TEST.SQL
1bf39 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
1bf3a 74 65 33 5f 73 6f 72 74 5f 63 6f 75 6e 74 20 3d te3_sort_count =
1bf3b 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 0;.#endif../*.*
1bf3c 2a 20 54 68 65 20 6e 65 78 74 20 67 6c 6f 62 61 * The next globa
1bf3d 6c 20 76 61 72 69 61 62 6c 65 20 72 65 63 6f 72 l variable recor
1bf3e 64 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 ds the size of t
1bf3f 68 65 20 6c 61 72 67 65 73 74 20 4d 45 4d 5f 42 he largest MEM_B
1bf40 6c 6f 62 0a 2a 2a 20 6f 72 20 4d 45 4d 5f 53 74 lob.** or MEM_St
1bf41 72 20 74 68 61 74 20 68 61 73 20 62 65 65 6e 20 r that has been
1bf42 75 73 65 64 20 62 79 20 61 20 56 44 42 45 20 6f used by a VDBE o
1bf43 70 63 6f 64 65 2e 20 20 54 68 65 20 74 65 73 74 pcode. The test
1bf44 20 70 72 6f 63 65 64 75 72 65 73 0a 2a 2a 20 75 procedures.** u
1bf45 73 65 20 74 68 69 73 20 69 6e 66 6f 72 6d 61 74 se this informat
1bf46 69 6f 6e 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 ion to make sure
1bf47 20 74 68 61 74 20 74 68 65 20 7a 65 72 6f 2d 62 that the zero-b
1bf48 6c 6f 62 20 66 75 6e 63 74 69 6f 6e 61 6c 69 74 lob functionalit
1bf49 79 0a 2a 2a 20 69 73 20 77 6f 72 6b 69 6e 67 20 y.** is working
1bf4a 63 6f 72 72 65 63 74 6c 79 2e 20 20 20 54 68 69 correctly. Thi
1bf4b 73 20 76 61 72 69 61 62 6c 65 20 68 61 73 20 6e s variable has n
1bf4c 6f 20 66 75 6e 63 74 69 6f 6e 20 6f 74 68 65 72 o function other
1bf4d 20 74 68 61 6e 20 74 6f 0a 2a 2a 20 68 65 6c 70 than to.** help
1bf4e 20 76 65 72 69 66 79 20 74 68 65 20 63 6f 72 72 verify the corr
1bf4f 65 63 74 20 6f 70 65 72 61 74 69 6f 6e 20 6f 66 ect operation of
1bf50 20 74 68 65 20 6c 69 62 72 61 72 79 2e 0a 2a 2f the library..*/
1bf51 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 .#ifdef SQLITE_T
1bf52 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 EST.SQLITE_API i
1bf53 6e 74 20 73 71 6c 69 74 65 33 5f 6d 61 78 5f 62 nt sqlite3_max_b
1bf54 6c 6f 62 73 69 7a 65 20 3d 20 30 3b 0a 73 74 61 lobsize = 0;.sta
1bf55 74 69 63 20 76 6f 69 64 20 75 70 64 61 74 65 4d tic void updateM
1bf56 61 78 42 6c 6f 62 73 69 7a 65 28 4d 65 6d 20 2a axBlobsize(Mem *
1bf57 70 29 7b 0a 20 20 69 66 28 20 28 70 2d 3e 66 6c p){. if( (p->fl
1bf58 61 67 73 20 26 20 28 4d 45 4d 5f 53 74 72 7c 4d ags & (MEM_Str|M
1bf59 45 4d 5f 42 6c 6f 62 29 29 21 3d 30 20 26 26 20 EM_Blob))!=0 &&
1bf5a 70 2d 3e 6e 3e 73 71 6c 69 74 65 33 5f 6d 61 78 p->n>sqlite3_max
1bf5b 5f 62 6c 6f 62 73 69 7a 65 20 29 7b 0a 20 20 20 _blobsize ){.
1bf5c 20 73 71 6c 69 74 65 33 5f 6d 61 78 5f 62 6c 6f sqlite3_max_blo
1bf5d 62 73 69 7a 65 20 3d 20 70 2d 3e 6e 3b 0a 20 20 bsize = p->n;.
1bf5e 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a }.}.#endif../*.*
1bf5f 2a 20 54 68 65 20 6e 65 78 74 20 67 6c 6f 62 61 * The next globa
1bf60 6c 20 76 61 72 69 61 62 6c 65 20 69 73 20 69 6e l variable is in
1bf61 63 72 65 6d 65 6e 74 65 64 20 65 61 63 68 20 74 cremented each t
1bf62 79 70 65 20 74 68 65 20 4f 50 5f 46 6f 75 6e 64 ype the OP_Found
1bf63 20 6f 70 63 6f 64 65 0a 2a 2a 20 69 73 20 65 78 opcode.** is ex
1bf64 65 63 75 74 65 64 2e 20 54 68 69 73 20 69 73 20 ecuted. This is
1bf65 75 73 65 64 20 74 6f 20 74 65 73 74 20 77 68 65 used to test whe
1bf66 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 ther or not the
1bf67 66 6f 72 65 69 67 6e 20 6b 65 79 0a 2a 2a 20 6f foreign key.** o
1bf68 70 65 72 61 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 peration impleme
1bf69 6e 74 65 64 20 75 73 69 6e 67 20 4f 50 5f 46 6b nted using OP_Fk
1bf6a 49 73 5a 65 72 6f 20 69 73 20 77 6f 72 6b 69 6e IsZero is workin
1bf6b 67 2e 20 54 68 69 73 20 76 61 72 69 61 62 6c 65 g. This variable
1bf6c 0a 2a 2a 20 68 61 73 20 6e 6f 20 66 75 6e 63 74 .** has no funct
1bf6d 69 6f 6e 20 6f 74 68 65 72 20 74 68 61 6e 20 74 ion other than t
1bf6e 6f 20 68 65 6c 70 20 76 65 72 69 66 79 20 74 68 o help verify th
1bf6f 65 20 63 6f 72 72 65 63 74 20 6f 70 65 72 61 74 e correct operat
1bf70 69 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 6c 69 ion of the.** li
1bf71 62 72 61 72 79 2e 0a 2a 2f 0a 23 69 66 64 65 66 brary..*/.#ifdef
1bf72 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c SQLITE_TEST.SQL
1bf73 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
1bf74 74 65 33 5f 66 6f 75 6e 64 5f 63 6f 75 6e 74 20 te3_found_count
1bf75 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a = 0;.#endif../*.
1bf76 2a 2a 20 54 65 73 74 20 61 20 72 65 67 69 73 74 ** Test a regist
1bf77 65 72 20 74 6f 20 73 65 65 20 69 66 20 69 74 20 er to see if it
1bf78 65 78 63 65 65 64 73 20 74 68 65 20 63 75 72 72 exceeds the curr
1bf79 65 6e 74 20 6d 61 78 69 6d 75 6d 20 62 6c 6f 62 ent maximum blob
1bf7a 20 73 69 7a 65 2e 0a 2a 2a 20 49 66 20 69 74 20 size..** If it
1bf7b 64 6f 65 73 2c 20 72 65 63 6f 72 64 20 74 68 65 does, record the
1bf7c 20 6e 65 77 20 6d 61 78 69 6d 75 6d 20 62 6c 6f new maximum blo
1bf7d 62 20 73 69 7a 65 2e 0a 2a 2f 0a 23 69 66 20 64 b size..*/.#if d
1bf7e 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 efined(SQLITE_TE
1bf7f 53 54 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 ST) && !defined(
1bf80 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c SQLITE_OMIT_BUIL
1bf81 54 49 4e 5f 54 45 53 54 29 0a 23 20 64 65 66 69 TIN_TEST).# defi
1bf82 6e 65 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c ne UPDATE_MAX_BL
1bf83 4f 42 53 49 5a 45 28 50 29 20 20 75 70 64 61 74 OBSIZE(P) updat
1bf84 65 4d 61 78 42 6c 6f 62 73 69 7a 65 28 50 29 0a eMaxBlobsize(P).
1bf85 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 55 #else.# define U
1bf86 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 PDATE_MAX_BLOBSI
1bf87 5a 45 28 50 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a ZE(P).#endif../*
1bf88 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 74 68 65 20 .** Convert the
1bf89 67 69 76 65 6e 20 72 65 67 69 73 74 65 72 20 69 given register i
1bf8a 6e 74 6f 20 61 20 73 74 72 69 6e 67 20 69 66 20 nto a string if
1bf8b 69 74 20 69 73 6e 27 74 20 6f 6e 65 0a 2a 2a 20 it isn't one.**
1bf8c 61 6c 72 65 61 64 79 2e 20 52 65 74 75 72 6e 20 already. Return
1bf8d 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20 61 20 6d 61 non-zero if a ma
1bf8e 6c 6c 6f 63 28 29 20 66 61 69 6c 73 2e 0a 2a 2f lloc() fails..*/
1bf8f 0a 23 64 65 66 69 6e 65 20 53 74 72 69 6e 67 69 .#define Stringi
1bf90 66 79 28 50 2c 20 65 6e 63 29 20 5c 0a 20 20 20 fy(P, enc) \.
1bf91 69 66 28 28 28 50 29 2d 3e 66 6c 61 67 73 26 28 if(((P)->flags&(
1bf92 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 MEM_Str|MEM_Blob
1bf93 29 29 3d 3d 30 20 26 26 20 73 71 6c 69 74 65 33 ))==0 && sqlite3
1bf94 56 64 62 65 4d 65 6d 53 74 72 69 6e 67 69 66 79 VdbeMemStringify
1bf95 28 50 2c 65 6e 63 29 29 20 5c 0a 20 20 20 20 20 (P,enc)) \.
1bf96 7b 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 20 7d { goto no_mem; }
1bf97 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 65 70 68 65 6d ../*.** An ephem
1bf98 65 72 61 6c 20 73 74 72 69 6e 67 20 76 61 6c 75 eral string valu
1bf99 65 20 28 73 69 67 6e 69 66 69 65 64 20 62 79 20 e (signified by
1bf9a 74 68 65 20 4d 45 4d 5f 45 70 68 65 6d 20 66 6c the MEM_Ephem fl
1bf9b 61 67 29 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 ag) contains.**
1bf9c 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 64 a pointer to a d
1bf9d 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 ynamically alloc
1bf9e 61 74 65 64 20 73 74 72 69 6e 67 20 77 68 65 72 ated string wher
1bf9f 65 20 73 6f 6d 65 20 6f 74 68 65 72 20 65 6e 74 e some other ent
1bfa0 69 74 79 0a 2a 2a 20 69 73 20 72 65 73 70 6f 6e ity.** is respon
1bfa1 73 69 62 6c 65 20 66 6f 72 20 64 65 61 6c 6c 6f sible for deallo
1bfa2 63 61 74 69 6e 67 20 74 68 61 74 20 73 74 72 69 cating that stri
1bfa3 6e 67 2e 20 20 42 65 63 61 75 73 65 20 74 68 65 ng. Because the
1bfa4 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 64 6f 65 register.** doe
1bfa5 73 20 6e 6f 74 20 63 6f 6e 74 72 6f 6c 20 74 68 s not control th
1bfa6 65 20 73 74 72 69 6e 67 2c 20 69 74 20 6d 69 67 e string, it mig
1bfa7 68 74 20 62 65 20 64 65 6c 65 74 65 64 20 77 69 ht be deleted wi
1bfa8 74 68 6f 75 74 20 74 68 65 20 72 65 67 69 73 74 thout the regist
1bfa9 65 72 0a 2a 2a 20 6b 6e 6f 77 69 6e 67 20 69 74 er.** knowing it
1bfaa 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
1bfab 74 69 6e 65 20 63 6f 6e 76 65 72 74 73 20 61 6e tine converts an
1bfac 20 65 70 68 65 6d 65 72 61 6c 20 73 74 72 69 6e ephemeral strin
1bfad 67 20 69 6e 74 6f 20 61 20 64 79 6e 61 6d 69 63 g into a dynamic
1bfae 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 0a 2a ally allocated.*
1bfaf 2a 20 73 74 72 69 6e 67 20 74 68 61 74 20 74 68 * string that th
1bfb0 65 20 72 65 67 69 73 74 65 72 20 69 74 73 65 6c e register itsel
1bfb1 66 20 63 6f 6e 74 72 6f 6c 73 2e 20 20 49 6e 20 f controls. In
1bfb2 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 69 74 0a other words, it.
1bfb3 2a 2a 20 63 6f 6e 76 65 72 74 73 20 61 6e 20 4d ** converts an M
1bfb4 45 4d 5f 45 70 68 65 6d 20 73 74 72 69 6e 67 20 EM_Ephem string
1bfb5 69 6e 74 6f 20 61 6e 20 4d 45 4d 5f 44 79 6e 20 into an MEM_Dyn
1bfb6 73 74 72 69 6e 67 2e 0a 2a 2f 0a 23 64 65 66 69 string..*/.#defi
1bfb7 6e 65 20 44 65 65 70 68 65 6d 65 72 61 6c 69 7a ne Deephemeraliz
1bfb8 65 28 50 29 20 5c 0a 20 20 20 69 66 28 20 28 28 e(P) \. if( ((
1bfb9 50 29 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 45 70 P)->flags&MEM_Ep
1bfba 68 65 6d 29 21 3d 30 20 5c 0a 20 20 20 20 20 20 hem)!=0 \.
1bfbb 20 26 26 20 73 71 6c 69 74 65 33 56 64 62 65 4d && sqlite3VdbeM
1bfbc 65 6d 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 28 emMakeWriteable(
1bfbd 50 29 20 29 7b 20 67 6f 74 6f 20 6e 6f 5f 6d 65 P) ){ goto no_me
1bfbe 6d 3b 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 m;}../*.** Call
1bfbf 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 45 78 sqlite3VdbeMemEx
1bfc0 70 61 6e 64 42 6c 6f 62 28 29 20 6f 6e 20 74 68 pandBlob() on th
1bfc1 65 20 73 75 70 70 6c 69 65 64 20 76 61 6c 75 65 e supplied value
1bfc2 20 28 74 79 70 65 20 4d 65 6d 2a 29 0a 2a 2a 20 (type Mem*).**
1bfc3 50 20 69 66 20 72 65 71 75 69 72 65 64 2e 0a 2a P if required..*
1bfc4 2f 0a 23 64 65 66 69 6e 65 20 45 78 70 61 6e 64 /.#define Expand
1bfc5 42 6c 6f 62 28 50 29 20 28 28 28 50 29 2d 3e 66 Blob(P) (((P)->f
1bfc6 6c 61 67 73 26 4d 45 4d 5f 5a 65 72 6f 29 3f 73 lags&MEM_Zero)?s
1bfc7 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 45 78 70 qlite3VdbeMemExp
1bfc8 61 6e 64 42 6c 6f 62 28 50 29 3a 30 29 0a 0a 2f andBlob(P):0)../
1bfc9 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74 20 70 4d *.** Argument pM
1bfca 65 6d 20 70 6f 69 6e 74 73 20 61 74 20 61 20 72 em points at a r
1bfcb 65 67 69 73 74 65 72 20 74 68 61 74 20 77 69 6c egister that wil
1bfcc 6c 20 62 65 20 70 61 73 73 65 64 20 74 6f 20 61 l be passed to a
1bfcd 0a 2a 2a 20 75 73 65 72 2d 64 65 66 69 6e 65 64 .** user-defined
1bfce 20 66 75 6e 63 74 69 6f 6e 20 6f 72 20 72 65 74 function or ret
1bfcf 75 72 6e 65 64 20 74 6f 20 74 68 65 20 75 73 65 urned to the use
1bfd0 72 20 61 73 20 74 68 65 20 72 65 73 75 6c 74 20 r as the result
1bfd1 6f 66 20 61 20 71 75 65 72 79 2e 0a 2a 2a 20 54 of a query..** T
1bfd2 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 he second argume
1bfd3 6e 74 2c 20 27 64 62 5f 65 6e 63 27 20 69 73 20 nt, 'db_enc' is
1bfd4 74 68 65 20 74 65 78 74 20 65 6e 63 6f 64 69 6e the text encodin
1bfd5 67 20 75 73 65 64 20 62 79 20 74 68 65 20 76 64 g used by the vd
1bfd6 62 65 20 66 6f 72 0a 2a 2a 20 72 65 67 69 73 74 be for.** regist
1bfd7 65 72 20 76 61 72 69 61 62 6c 65 73 2e 20 20 54 er variables. T
1bfd8 68 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73 his routine sets
1bfd9 20 74 68 65 20 70 4d 65 6d 2d 3e 65 6e 63 20 61 the pMem->enc a
1bfda 6e 64 20 70 4d 65 6d 2d 3e 74 79 70 65 0a 2a 2a nd pMem->type.**
1bfdb 20 76 61 72 69 61 62 6c 65 73 20 75 73 65 64 20 variables used
1bfdc 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76 by the sqlite3_v
1bfdd 61 6c 75 65 5f 2a 28 29 20 72 6f 75 74 69 6e 65 alue_*() routine
1bfde 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 73 74 s..*/.#define st
1bfdf 6f 72 65 54 79 70 65 49 6e 66 6f 28 41 2c 42 29 oreTypeInfo(A,B)
1bfe0 20 5f 73 74 6f 72 65 54 79 70 65 49 6e 66 6f 28 _storeTypeInfo(
1bfe1 41 29 0a 73 74 61 74 69 63 20 76 6f 69 64 20 5f A).static void _
1bfe2 73 74 6f 72 65 54 79 70 65 49 6e 66 6f 28 4d 65 storeTypeInfo(Me
1bfe3 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 69 6e 74 20 m *pMem){. int
1bfe4 66 6c 61 67 73 20 3d 20 70 4d 65 6d 2d 3e 66 6c flags = pMem->fl
1bfe5 61 67 73 3b 0a 20 20 69 66 28 20 66 6c 61 67 73 ags;. if( flags
1bfe6 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 & MEM_Null ){.
1bfe7 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 pMem->type =
1bfe8 53 51 4c 49 54 45 5f 4e 55 4c 4c 3b 0a 20 20 7d SQLITE_NULL;. }
1bfe9 0a 20 20 65 6c 73 65 20 69 66 28 20 66 6c 61 67 . else if( flag
1bfea 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 s & MEM_Int ){.
1bfeb 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 pMem->type =
1bfec 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 3b 0a SQLITE_INTEGER;.
1bfed 20 20 7d 0a 20 20 65 6c 73 65 20 69 66 28 20 66 }. else if( f
1bfee 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 20 lags & MEM_Real
1bfef 29 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 ){. pMem->typ
1bff0 65 20 3d 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 e = SQLITE_FLOAT
1bff1 3b 0a 20 20 7d 0a 20 20 65 6c 73 65 20 69 66 28 ;. }. else if(
1bff2 20 66 6c 61 67 73 20 26 20 4d 45 4d 5f 53 74 72 flags & MEM_Str
1bff3 20 29 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 79 ){. pMem->ty
1bff4 70 65 20 3d 20 53 51 4c 49 54 45 5f 54 45 58 54 pe = SQLITE_TEXT
1bff5 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 ;. }else{. p
1bff6 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 Mem->type = SQLI
1bff7 54 45 5f 42 4c 4f 42 3b 0a 20 20 7d 0a 7d 0a 0a TE_BLOB;. }.}..
1bff8 2f 2a 0a 2a 2a 20 50 72 6f 70 65 72 74 69 65 73 /*.** Properties
1bff9 20 6f 66 20 6f 70 63 6f 64 65 73 2e 20 20 54 68 of opcodes. Th
1bffa 65 20 4f 50 46 4c 47 5f 49 4e 49 54 49 41 4c 49 e OPFLG_INITIALI
1bffb 5a 45 52 20 6d 61 63 72 6f 20 69 73 0a 2a 2a 20 ZER macro is.**
1bffc 63 72 65 61 74 65 64 20 62 79 20 6d 6b 6f 70 63 created by mkopc
1bffd 6f 64 65 68 2e 61 77 6b 20 64 75 72 69 6e 67 20 odeh.awk during
1bffe 63 6f 6d 70 69 6c 61 74 69 6f 6e 2e 20 20 44 61 compilation. Da
1bfff 74 61 20 69 73 20 6f 62 74 61 69 6e 65 64 0a 2a ta is obtained.*
1c000 2a 20 66 72 6f 6d 20 74 68 65 20 63 6f 6d 6d 65 * from the comme
1c001 6e 74 73 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 nts following th
1c002 65 20 22 63 61 73 65 20 4f 50 5f 78 78 78 78 3a e "case OP_xxxx:
1c003 22 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 0a " statements in.
1c004 2a 2a 20 74 68 69 73 20 66 69 6c 65 2e 20 20 0a ** this file. .
1c005 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 */.static const
1c006 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 6f 70 unsigned char op
1c007 63 6f 64 65 50 72 6f 70 65 72 74 79 5b 5d 20 3d codeProperty[] =
1c008 20 4f 50 46 4c 47 5f 49 4e 49 54 49 41 4c 49 5a OPFLG_INITIALIZ
1c009 45 52 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 ER;../*.** Retur
1c00a 6e 20 74 72 75 65 20 69 66 20 61 6e 20 6f 70 63 n true if an opc
1c00b 6f 64 65 20 68 61 73 20 61 6e 79 20 6f 66 20 74 ode has any of t
1c00c 68 65 20 4f 50 46 4c 47 5f 78 78 78 20 70 72 6f he OPFLG_xxx pro
1c00d 70 65 72 74 69 65 73 0a 2a 2a 20 73 70 65 63 69 perties.** speci
1c00e 66 69 65 64 20 62 79 20 6d 61 73 6b 2e 0a 2a 2f fied by mask..*/
1c00f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1c010 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4f int sqlite3VdbeO
1c011 70 63 6f 64 65 48 61 73 50 72 6f 70 65 72 74 79 pcodeHasProperty
1c012 28 69 6e 74 20 6f 70 63 6f 64 65 2c 20 69 6e 74 (int opcode, int
1c013 20 6d 61 73 6b 29 7b 0a 20 20 61 73 73 65 72 74 mask){. assert
1c014 28 20 6f 70 63 6f 64 65 3e 30 20 26 26 20 6f 70 ( opcode>0 && op
1c015 63 6f 64 65 3c 28 69 6e 74 29 73 69 7a 65 6f 66 code<(int)sizeof
1c016 28 6f 70 63 6f 64 65 50 72 6f 70 65 72 74 79 29 (opcodeProperty)
1c017 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 6f 70 );. return (op
1c018 63 6f 64 65 50 72 6f 70 65 72 74 79 5b 6f 70 63 codeProperty[opc
1c019 6f 64 65 5d 26 6d 61 73 6b 29 21 3d 30 3b 0a 7d ode]&mask)!=0;.}
1c01a 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 ../*.** Allocate
1c01b 20 56 64 62 65 43 75 72 73 6f 72 20 6e 75 6d 62 VdbeCursor numb
1c01c 65 72 20 69 43 75 72 2e 20 20 52 65 74 75 72 6e er iCur. Return
1c01d 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 a pointer to it
1c01e 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c 4c 0a 2a . Return NULL.*
1c01f 2a 20 69 66 20 77 65 20 72 75 6e 20 6f 75 74 20 * if we run out
1c020 6f 66 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 73 74 of memory..*/.st
1c021 61 74 69 63 20 56 64 62 65 43 75 72 73 6f 72 20 atic VdbeCursor
1c022 2a 61 6c 6c 6f 63 61 74 65 43 75 72 73 6f 72 28 *allocateCursor(
1c023 0a 20 20 56 64 62 65 20 2a 70 2c 20 20 20 20 20 . Vdbe *p,
1c024 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
1c025 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 virtual machine
1c026 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72 2c 20 20 */. int iCur,
1c027 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e /* In
1c028 64 65 78 20 6f 66 20 74 68 65 20 6e 65 77 20 56 dex of the new V
1c029 64 62 65 43 75 72 73 6f 72 20 2a 2f 0a 20 20 69 dbeCursor */. i
1c02a 6e 74 20 6e 46 69 65 6c 64 2c 20 20 20 20 20 20 nt nField,
1c02b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
1c02c 66 20 66 69 65 6c 64 73 20 69 6e 20 74 68 65 20 f fields in the
1c02d 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 2a table or index *
1c02e 2f 0a 20 20 69 6e 74 20 69 44 62 2c 20 20 20 20 /. int iDb,
1c02f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 68 65 /* Whe
1c030 6e 20 64 61 74 61 62 61 73 65 20 74 68 65 20 63 n database the c
1c031 75 72 73 6f 72 20 62 65 6c 6f 6e 67 73 20 74 6f ursor belongs to
1c032 2c 20 6f 72 20 2d 31 20 2a 2f 0a 20 20 69 6e 74 , or -1 */. int
1c033 20 69 73 42 74 72 65 65 43 75 72 73 6f 72 20 20 isBtreeCursor
1c034 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 42 /* True for B
1c035 2d 54 72 65 65 2e 20 20 46 61 6c 73 65 20 66 6f -Tree. False fo
1c036 72 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 6f r pseudo-table o
1c037 72 20 76 74 61 62 20 2a 2f 0a 29 7b 0a 20 20 2f r vtab */.){. /
1c038 2a 20 46 69 6e 64 20 74 68 65 20 6d 65 6d 6f 72 * Find the memor
1c039 79 20 63 65 6c 6c 20 74 68 61 74 20 77 69 6c 6c y cell that will
1c03a 20 62 65 20 75 73 65 64 20 74 6f 20 73 74 6f 72 be used to stor
1c03b 65 20 74 68 65 20 62 6c 6f 62 20 6f 66 20 6d 65 e the blob of me
1c03c 6d 6f 72 79 0a 20 20 2a 2a 20 72 65 71 75 69 72 mory. ** requir
1c03d 65 64 20 66 6f 72 20 74 68 69 73 20 56 64 62 65 ed for this Vdbe
1c03e 43 75 72 73 6f 72 20 73 74 72 75 63 74 75 72 65 Cursor structure
1c03f 2e 20 49 74 20 69 73 20 63 6f 6e 76 65 6e 69 65 . It is convenie
1c040 6e 74 20 74 6f 20 75 73 65 20 61 20 0a 20 20 2a nt to use a . *
1c041 2a 20 76 64 62 65 20 6d 65 6d 6f 72 79 20 63 65 * vdbe memory ce
1c042 6c 6c 20 74 6f 20 6d 61 6e 61 67 65 20 74 68 65 ll to manage the
1c043 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
1c044 6f 6e 20 72 65 71 75 69 72 65 64 20 66 6f 72 20 on required for
1c045 61 0a 20 20 2a 2a 20 56 64 62 65 43 75 72 73 6f a. ** VdbeCurso
1c046 72 20 73 74 72 75 63 74 75 72 65 20 66 6f 72 20 r structure for
1c047 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 65 the following re
1c048 61 73 6f 6e 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a asons:. **. **
1c049 20 20 20 2a 20 53 6f 6d 65 74 69 6d 65 73 20 63 * Sometimes c
1c04a 75 72 73 6f 72 20 6e 75 6d 62 65 72 73 20 61 72 ursor numbers ar
1c04b 65 20 75 73 65 64 20 66 6f 72 20 61 20 63 6f 75 e used for a cou
1c04c 70 6c 65 20 6f 66 20 64 69 66 66 65 72 65 6e 74 ple of different
1c04d 0a 20 20 2a 2a 20 20 20 20 20 70 75 72 70 6f 73 . ** purpos
1c04e 65 73 20 69 6e 20 61 20 76 64 62 65 20 70 72 6f es in a vdbe pro
1c04f 67 72 61 6d 2e 20 54 68 65 20 64 69 66 66 65 72 gram. The differ
1c050 65 6e 74 20 75 73 65 73 20 6d 69 67 68 74 20 72 ent uses might r
1c051 65 71 75 69 72 65 0a 20 20 2a 2a 20 20 20 20 20 equire. **
1c052 64 69 66 66 65 72 65 6e 74 20 73 69 7a 65 64 20 different sized
1c053 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e 20 4d 65 6d allocations. Mem
1c054 6f 72 79 20 63 65 6c 6c 73 20 70 72 6f 76 69 64 ory cells provid
1c055 65 20 67 72 6f 77 61 62 6c 65 0a 20 20 2a 2a 20 e growable. **
1c056 20 20 20 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e allocations.
1c057 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 2a 20 57 . **. ** * W
1c058 68 65 6e 20 75 73 69 6e 67 20 45 4e 41 42 4c 45 hen using ENABLE
1c059 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 _MEMORY_MANAGEME
1c05a 4e 54 2c 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 NT, memory cell
1c05b 62 75 66 66 65 72 73 20 63 61 6e 0a 20 20 2a 2a buffers can. **
1c05c 20 20 20 20 20 62 65 20 66 72 65 65 64 20 6c 61 be freed la
1c05d 7a 69 6c 79 20 76 69 61 20 74 68 65 20 73 71 6c zily via the sql
1c05e 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d ite3_release_mem
1c05f 6f 72 79 28 29 20 41 50 49 2e 20 54 68 69 73 0a ory() API. This.
1c060 20 20 2a 2a 20 20 20 20 20 6d 69 6e 69 6d 69 7a ** minimiz
1c061 65 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 es the number of
1c062 20 6d 61 6c 6c 6f 63 20 63 61 6c 6c 73 20 6d 61 malloc calls ma
1c063 64 65 20 62 79 20 74 68 65 20 73 79 73 74 65 6d de by the system
1c064 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4d 65 6d 6f .. **. ** Memo
1c065 72 79 20 63 65 6c 6c 73 20 66 6f 72 20 63 75 72 ry cells for cur
1c066 73 6f 72 73 20 61 72 65 20 61 6c 6c 6f 63 61 74 sors are allocat
1c067 65 64 20 61 74 20 74 68 65 20 74 6f 70 20 6f 66 ed at the top of
1c068 20 74 68 65 20 61 64 64 72 65 73 73 0a 20 20 2a the address. *
1c069 2a 20 73 70 61 63 65 2e 20 4d 65 6d 6f 72 79 20 * space. Memory
1c06a 63 65 6c 6c 20 28 70 2d 3e 6e 4d 65 6d 29 20 63 cell (p->nMem) c
1c06b 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 63 75 orresponds to cu
1c06c 72 73 6f 72 20 30 2e 20 53 70 61 63 65 20 66 6f rsor 0. Space fo
1c06d 72 0a 20 20 2a 2a 20 63 75 72 73 6f 72 20 31 20 r. ** cursor 1
1c06e 69 73 20 6d 61 6e 61 67 65 64 20 62 79 20 6d 65 is managed by me
1c06f 6d 6f 72 79 20 63 65 6c 6c 20 28 70 2d 3e 6e 4d mory cell (p->nM
1c070 65 6d 2d 31 29 2c 20 65 74 63 2e 0a 20 20 2a 2f em-1), etc.. */
1c071 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 20 3d 20 26 . Mem *pMem = &
1c072 70 2d 3e 61 4d 65 6d 5b 70 2d 3e 6e 4d 65 6d 2d p->aMem[p->nMem-
1c073 69 43 75 72 5d 3b 0a 0a 20 20 69 6e 74 20 6e 42 iCur];.. int nB
1c074 79 74 65 3b 0a 20 20 56 64 62 65 43 75 72 73 6f yte;. VdbeCurso
1c075 72 20 2a 70 43 78 20 3d 20 30 3b 0a 20 20 6e 42 r *pCx = 0;. nB
1c076 79 74 65 20 3d 20 0a 20 20 20 20 20 20 73 69 7a yte = . siz
1c077 65 6f 66 28 56 64 62 65 43 75 72 73 6f 72 29 20 eof(VdbeCursor)
1c078 2b 20 0a 20 20 20 20 20 20 28 69 73 42 74 72 65 + . (isBtre
1c079 65 43 75 72 73 6f 72 3f 73 71 6c 69 74 65 33 42 eCursor?sqlite3B
1c07a 74 72 65 65 43 75 72 73 6f 72 53 69 7a 65 28 29 treeCursorSize()
1c07b 3a 30 29 20 2b 20 0a 20 20 20 20 20 20 32 2a 6e :0) + . 2*n
1c07c 46 69 65 6c 64 2a 73 69 7a 65 6f 66 28 75 33 32 Field*sizeof(u32
1c07d 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 43 );.. assert( iC
1c07e 75 72 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b ur<p->nCursor );
1c07f 0a 20 20 69 66 28 20 70 2d 3e 61 70 43 73 72 5b . if( p->apCsr[
1c080 69 43 75 72 5d 20 29 7b 0a 20 20 20 20 73 71 6c iCur] ){. sql
1c081 69 74 65 33 56 64 62 65 46 72 65 65 43 75 72 73 ite3VdbeFreeCurs
1c082 6f 72 28 70 2c 20 70 2d 3e 61 70 43 73 72 5b 69 or(p, p->apCsr[i
1c083 43 75 72 5d 29 3b 0a 20 20 20 20 70 2d 3e 61 70 Cur]);. p->ap
1c084 43 73 72 5b 69 43 75 72 5d 20 3d 20 30 3b 0a 20 Csr[iCur] = 0;.
1c085 20 7d 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f }. if( SQLITE_
1c086 4f 4b 3d 3d 73 71 6c 69 74 65 33 56 64 62 65 4d OK==sqlite3VdbeM
1c087 65 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 6e 42 79 emGrow(pMem, nBy
1c088 74 65 2c 20 30 29 20 29 7b 0a 20 20 20 20 70 2d te, 0) ){. p-
1c089 3e 61 70 43 73 72 5b 69 43 75 72 5d 20 3d 20 70 >apCsr[iCur] = p
1c08a 43 78 20 3d 20 28 56 64 62 65 43 75 72 73 6f 72 Cx = (VdbeCursor
1c08b 2a 29 70 4d 65 6d 2d 3e 7a 3b 0a 20 20 20 20 6d *)pMem->z;. m
1c08c 65 6d 73 65 74 28 70 4d 65 6d 2d 3e 7a 2c 20 30 emset(pMem->z, 0
1c08d 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 70 43 , nByte);. pC
1c08e 78 2d 3e 69 44 62 20 3d 20 69 44 62 3b 0a 20 20 x->iDb = iDb;.
1c08f 20 20 70 43 78 2d 3e 6e 46 69 65 6c 64 20 3d 20 pCx->nField =
1c090 6e 46 69 65 6c 64 3b 0a 20 20 20 20 69 66 28 20 nField;. if(
1c091 6e 46 69 65 6c 64 20 29 7b 0a 20 20 20 20 20 20 nField ){.
1c092 70 43 78 2d 3e 61 54 79 70 65 20 3d 20 28 75 33 pCx->aType = (u3
1c093 32 20 2a 29 26 70 4d 65 6d 2d 3e 7a 5b 73 69 7a 2 *)&pMem->z[siz
1c094 65 6f 66 28 56 64 62 65 43 75 72 73 6f 72 29 5d eof(VdbeCursor)]
1c095 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
1c096 69 73 42 74 72 65 65 43 75 72 73 6f 72 20 29 7b isBtreeCursor ){
1c097 0a 20 20 20 20 20 20 70 43 78 2d 3e 70 43 75 72 . pCx->pCur
1c098 73 6f 72 20 3d 20 28 42 74 43 75 72 73 6f 72 2a sor = (BtCursor*
1c099 29 0a 20 20 20 20 20 20 20 20 20 20 26 70 4d 65 ). &pMe
1c09a 6d 2d 3e 7a 5b 73 69 7a 65 6f 66 28 56 64 62 65 m->z[sizeof(Vdbe
1c09b 43 75 72 73 6f 72 29 2b 32 2a 6e 46 69 65 6c 64 Cursor)+2*nField
1c09c 2a 73 69 7a 65 6f 66 28 75 33 32 29 5d 3b 0a 20 *sizeof(u32)];.
1c09d 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
1c09e 6e 20 70 43 78 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 n pCx;.}../*.**
1c09f 54 72 79 20 74 6f 20 63 6f 6e 76 65 72 74 20 61 Try to convert a
1c0a0 20 76 61 6c 75 65 20 69 6e 74 6f 20 61 20 6e 75 value into a nu
1c0a1 6d 65 72 69 63 20 72 65 70 72 65 73 65 6e 74 61 meric representa
1c0a2 74 69 6f 6e 20 69 66 20 77 65 20 63 61 6e 0a 2a tion if we can.*
1c0a3 2a 20 64 6f 20 73 6f 20 77 69 74 68 6f 75 74 20 * do so without
1c0a4 6c 6f 73 73 20 6f 66 20 69 6e 66 6f 72 6d 61 74 loss of informat
1c0a5 69 6f 6e 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 ion. In other w
1c0a6 6f 72 64 73 2c 20 69 66 20 74 68 65 20 73 74 72 ords, if the str
1c0a7 69 6e 67 0a 2a 2a 20 6c 6f 6f 6b 73 20 6c 69 6b ing.** looks lik
1c0a8 65 20 61 20 6e 75 6d 62 65 72 2c 20 63 6f 6e 76 e a number, conv
1c0a9 65 72 74 20 69 74 20 69 6e 74 6f 20 61 20 6e 75 ert it into a nu
1c0aa 6d 62 65 72 2e 20 20 49 66 20 69 74 20 64 6f 65 mber. If it doe
1c0ab 73 20 6e 6f 74 0a 2a 2a 20 6c 6f 6f 6b 20 6c 69 s not.** look li
1c0ac 6b 65 20 61 20 6e 75 6d 62 65 72 2c 20 6c 65 61 ke a number, lea
1c0ad 76 65 20 69 74 20 61 6c 6f 6e 65 2e 0a 2a 2f 0a ve it alone..*/.
1c0ae 73 74 61 74 69 63 20 76 6f 69 64 20 61 70 70 6c static void appl
1c0af 79 4e 75 6d 65 72 69 63 41 66 66 69 6e 69 74 79 yNumericAffinity
1c0b0 28 4d 65 6d 20 2a 70 52 65 63 29 7b 0a 20 20 69 (Mem *pRec){. i
1c0b1 66 28 20 28 70 52 65 63 2d 3e 66 6c 61 67 73 20 f( (pRec->flags
1c0b2 26 20 28 4d 45 4d 5f 52 65 61 6c 7c 4d 45 4d 5f & (MEM_Real|MEM_
1c0b3 49 6e 74 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20 Int))==0 ){.
1c0b4 69 6e 74 20 72 65 61 6c 6e 75 6d 3b 0a 20 20 20 int realnum;.
1c0b5 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e sqlite3VdbeMemN
1c0b6 75 6c 54 65 72 6d 69 6e 61 74 65 28 70 52 65 63 ulTerminate(pRec
1c0b7 29 3b 0a 20 20 20 20 69 66 28 20 28 70 52 65 63 );. if( (pRec
1c0b8 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 72 29 ->flags&MEM_Str)
1c0b9 0a 20 20 20 20 20 20 20 20 20 26 26 20 73 71 6c . && sql
1c0ba 69 74 65 33 49 73 4e 75 6d 62 65 72 28 70 52 65 ite3IsNumber(pRe
1c0bb 63 2d 3e 7a 2c 20 26 72 65 61 6c 6e 75 6d 2c 20 c->z, &realnum,
1c0bc 70 52 65 63 2d 3e 65 6e 63 29 20 29 7b 0a 20 20 pRec->enc) ){.
1c0bd 20 20 20 20 69 36 34 20 76 61 6c 75 65 3b 0a 20 i64 value;.
1c0be 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1c0bf 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70 ChangeEncoding(p
1c0c0 52 65 63 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 Rec, SQLITE_UTF8
1c0c1 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 72 65 );. if( !re
1c0c2 61 6c 6e 75 6d 20 26 26 20 73 71 6c 69 74 65 33 alnum && sqlite3
1c0c3 41 74 6f 69 36 34 28 70 52 65 63 2d 3e 7a 2c 20 Atoi64(pRec->z,
1c0c4 26 76 61 6c 75 65 29 20 29 7b 0a 20 20 20 20 20 &value) ){.
1c0c5 20 20 20 70 52 65 63 2d 3e 75 2e 69 20 3d 20 76 pRec->u.i = v
1c0c6 61 6c 75 65 3b 0a 20 20 20 20 20 20 20 20 4d 65 alue;. Me
1c0c7 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 52 65 mSetTypeFlag(pRe
1c0c8 63 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 20 c, MEM_Int);.
1c0c9 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1c0ca 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1c0cb 52 65 61 6c 69 66 79 28 70 52 65 63 29 3b 0a 20 Realify(pRec);.
1c0cc 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d }. }. }
1c0cd 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 63 65 73 .}../*.** Proces
1c0ce 73 69 6e 67 20 69 73 20 64 65 74 65 72 6d 69 6e sing is determin
1c0cf 65 20 62 79 20 74 68 65 20 61 66 66 69 6e 69 74 e by the affinit
1c0d0 79 20 70 61 72 61 6d 65 74 65 72 3a 0a 2a 2a 0a y parameter:.**.
1c0d1 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e ** SQLITE_AFF_IN
1c0d2 54 45 47 45 52 3a 0a 2a 2a 20 53 51 4c 49 54 45 TEGER:.** SQLITE
1c0d3 5f 41 46 46 5f 52 45 41 4c 3a 0a 2a 2a 20 53 51 _AFF_REAL:.** SQ
1c0d4 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 LITE_AFF_NUMERIC
1c0d5 3a 0a 2a 2a 20 20 20 20 54 72 79 20 74 6f 20 63 :.** Try to c
1c0d6 6f 6e 76 65 72 74 20 70 52 65 63 20 74 6f 20 61 onvert pRec to a
1c0d7 6e 20 69 6e 74 65 67 65 72 20 72 65 70 72 65 73 n integer repres
1c0d8 65 6e 74 61 74 69 6f 6e 20 6f 72 20 61 20 0a 2a entation or a .*
1c0d9 2a 20 20 20 20 66 6c 6f 61 74 69 6e 67 2d 70 6f * floating-po
1c0da 69 6e 74 20 72 65 70 72 65 73 65 6e 74 61 74 69 int representati
1c0db 6f 6e 20 69 66 20 61 6e 20 69 6e 74 65 67 65 72 on if an integer
1c0dc 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 0a representation.
1c0dd 2a 2a 20 20 20 20 69 73 20 6e 6f 74 20 70 6f 73 ** is not pos
1c0de 73 69 62 6c 65 2e 20 20 4e 6f 74 65 20 74 68 61 sible. Note tha
1c0df 74 20 74 68 65 20 69 6e 74 65 67 65 72 20 72 65 t the integer re
1c0e0 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 69 73 0a presentation is.
1c0e1 2a 2a 20 20 20 20 61 6c 77 61 79 73 20 70 72 65 ** always pre
1c0e2 66 65 72 72 65 64 2c 20 65 76 65 6e 20 69 66 20 ferred, even if
1c0e3 74 68 65 20 61 66 66 69 6e 69 74 79 20 69 73 20 the affinity is
1c0e4 52 45 41 4c 2c 20 62 65 63 61 75 73 65 0a 2a 2a REAL, because.**
1c0e5 20 20 20 20 61 6e 20 69 6e 74 65 67 65 72 20 72 an integer r
1c0e6 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 69 73 epresentation is
1c0e7 20 6d 6f 72 65 20 73 70 61 63 65 20 65 66 66 69 more space effi
1c0e8 63 69 65 6e 74 20 6f 6e 20 64 69 73 6b 2e 0a 2a cient on disk..*
1c0e9 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f *.** SQLITE_AFF_
1c0ea 54 45 58 54 3a 0a 2a 2a 20 20 20 20 43 6f 6e 76 TEXT:.** Conv
1c0eb 65 72 74 20 70 52 65 63 20 74 6f 20 61 20 74 65 ert pRec to a te
1c0ec 78 74 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f xt representatio
1c0ed 6e 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f n..**.** SQLITE_
1c0ee 41 46 46 5f 4e 4f 4e 45 3a 0a 2a 2a 20 20 20 20 AFF_NONE:.**
1c0ef 4e 6f 2d 6f 70 2e 20 20 70 52 65 63 20 69 73 20 No-op. pRec is
1c0f0 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a 73 74 unchanged..*/.st
1c0f1 61 74 69 63 20 76 6f 69 64 20 61 70 70 6c 79 41 atic void applyA
1c0f2 66 66 69 6e 69 74 79 28 0a 20 20 4d 65 6d 20 2a ffinity(. Mem *
1c0f3 70 52 65 63 2c 20 20 20 20 20 20 20 20 20 20 2f pRec, /
1c0f4 2a 20 54 68 65 20 76 61 6c 75 65 20 74 6f 20 61 * The value to a
1c0f5 70 70 6c 79 20 61 66 66 69 6e 69 74 79 20 74 6f pply affinity to
1c0f6 20 2a 2f 0a 20 20 63 68 61 72 20 61 66 66 69 6e */. char affin
1c0f7 69 74 79 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 ity, /* The
1c0f8 20 61 66 66 69 6e 69 74 79 20 74 6f 20 62 65 20 affinity to be
1c0f9 61 70 70 6c 69 65 64 20 2a 2f 0a 20 20 75 38 20 applied */. u8
1c0fa 65 6e 63 20 20 20 20 20 20 20 20 20 20 20 20 20 enc
1c0fb 20 2f 2a 20 55 73 65 20 74 68 69 73 20 74 65 78 /* Use this tex
1c0fc 74 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 29 7b t encoding */.){
1c0fd 0a 20 20 69 66 28 20 61 66 66 69 6e 69 74 79 3d . if( affinity=
1c0fe 3d 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 =SQLITE_AFF_TEXT
1c0ff 20 29 7b 0a 20 20 20 20 2f 2a 20 4f 6e 6c 79 20 ){. /* Only
1c100 61 74 74 65 6d 70 74 20 74 68 65 20 63 6f 6e 76 attempt the conv
1c101 65 72 73 69 6f 6e 20 74 6f 20 54 45 58 54 20 69 ersion to TEXT i
1c102 66 20 74 68 65 72 65 20 69 73 20 61 6e 20 69 6e f there is an in
1c103 74 65 67 65 72 20 6f 72 20 72 65 61 6c 0a 20 20 teger or real.
1c104 20 20 2a 2a 20 72 65 70 72 65 73 65 6e 74 61 74 ** representat
1c105 69 6f 6e 20 28 62 6c 6f 62 20 61 6e 64 20 4e 55 ion (blob and NU
1c106 4c 4c 20 64 6f 20 6e 6f 74 20 67 65 74 20 63 6f LL do not get co
1c107 6e 76 65 72 74 65 64 29 20 62 75 74 20 6e 6f 20 nverted) but no
1c108 73 74 72 69 6e 67 0a 20 20 20 20 2a 2a 20 72 65 string. ** re
1c109 70 72 65 73 65 6e 74 61 74 69 6f 6e 2e 0a 20 20 presentation..
1c10a 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 30 3d 3d */. if( 0==
1c10b 28 70 52 65 63 2d 3e 66 6c 61 67 73 26 4d 45 4d (pRec->flags&MEM
1c10c 5f 53 74 72 29 20 26 26 20 28 70 52 65 63 2d 3e _Str) && (pRec->
1c10d 66 6c 61 67 73 26 28 4d 45 4d 5f 52 65 61 6c 7c flags&(MEM_Real|
1c10e 4d 45 4d 5f 49 6e 74 29 29 20 29 7b 0a 20 20 20 MEM_Int)) ){.
1c10f 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 sqlite3VdbeMe
1c110 6d 53 74 72 69 6e 67 69 66 79 28 70 52 65 63 2c mStringify(pRec,
1c111 20 65 6e 63 29 3b 0a 20 20 20 20 7d 0a 20 20 20 enc);. }.
1c112 20 70 52 65 63 2d 3e 66 6c 61 67 73 20 26 3d 20 pRec->flags &=
1c113 7e 28 4d 45 4d 5f 52 65 61 6c 7c 4d 45 4d 5f 49 ~(MEM_Real|MEM_I
1c114 6e 74 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 nt);. }else if(
1c115 20 61 66 66 69 6e 69 74 79 21 3d 53 51 4c 49 54 affinity!=SQLIT
1c116 45 5f 41 46 46 5f 4e 4f 4e 45 20 29 7b 0a 20 20 E_AFF_NONE ){.
1c117 20 20 61 73 73 65 72 74 28 20 61 66 66 69 6e 69 assert( affini
1c118 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 49 ty==SQLITE_AFF_I
1c119 4e 54 45 47 45 52 20 7c 7c 20 61 66 66 69 6e 69 NTEGER || affini
1c11a 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 52 ty==SQLITE_AFF_R
1c11b 45 41 4c 0a 20 20 20 20 20 20 20 20 20 20 20 20 EAL.
1c11c 20 7c 7c 20 61 66 66 69 6e 69 74 79 3d 3d 53 51 || affinity==SQ
1c11d 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 LITE_AFF_NUMERIC
1c11e 20 29 3b 0a 20 20 20 20 61 70 70 6c 79 4e 75 6d );. applyNum
1c11f 65 72 69 63 41 66 66 69 6e 69 74 79 28 70 52 65 ericAffinity(pRe
1c120 63 29 3b 0a 20 20 20 20 69 66 28 20 70 52 65 63 c);. if( pRec
1c121 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 ->flags & MEM_Re
1c122 61 6c 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 al ){. sqli
1c123 74 65 33 56 64 62 65 49 6e 74 65 67 65 72 41 66 te3VdbeIntegerAf
1c124 66 69 6e 69 74 79 28 70 52 65 63 29 3b 0a 20 20 finity(pRec);.
1c125 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a }. }.}../*.**
1c126 20 54 72 79 20 74 6f 20 63 6f 6e 76 65 72 74 20 Try to convert
1c127 74 68 65 20 74 79 70 65 20 6f 66 20 61 20 66 75 the type of a fu
1c128 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 20 nction argument
1c129 6f 72 20 61 20 72 65 73 75 6c 74 20 63 6f 6c 75 or a result colu
1c12a 6d 6e 0a 2a 2a 20 69 6e 74 6f 20 61 20 6e 75 6d mn.** into a num
1c12b 65 72 69 63 20 72 65 70 72 65 73 65 6e 74 61 74 eric representat
1c12c 69 6f 6e 2e 20 20 55 73 65 20 65 69 74 68 65 72 ion. Use either
1c12d 20 49 4e 54 45 47 45 52 20 6f 72 20 52 45 41 4c INTEGER or REAL
1c12e 20 77 68 69 63 68 65 76 65 72 0a 2a 2a 20 69 73 whichever.** is
1c12f 20 61 70 70 72 6f 70 72 69 61 74 65 2e 20 20 42 appropriate. B
1c130 75 74 20 6f 6e 6c 79 20 64 6f 20 74 68 65 20 63 ut only do the c
1c131 6f 6e 76 65 72 73 69 6f 6e 20 69 66 20 69 74 20 onversion if it
1c132 69 73 20 70 6f 73 73 69 62 6c 65 20 77 69 74 68 is possible with
1c133 6f 75 74 0a 2a 2a 20 6c 6f 73 73 20 6f 66 20 69 out.** loss of i
1c134 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 6e 64 20 72 nformation and r
1c135 65 74 75 72 6e 20 74 68 65 20 72 65 76 69 73 65 eturn the revise
1c136 64 20 74 79 70 65 20 6f 66 20 74 68 65 20 61 72 d type of the ar
1c137 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 gument..**.** Th
1c138 69 73 20 69 73 20 61 6e 20 45 58 50 45 52 49 4d is is an EXPERIM
1c139 45 4e 54 41 4c 20 61 70 69 20 61 6e 64 20 69 73 ENTAL api and is
1c13a 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68 61 6e subject to chan
1c13b 67 65 20 6f 72 20 72 65 6d 6f 76 61 6c 2e 0a 2a ge or removal..*
1c13c 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
1c13d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 6e sqlite3_value_n
1c13e 75 6d 65 72 69 63 5f 74 79 70 65 28 73 71 6c 69 umeric_type(sqli
1c13f 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29 te3_value *pVal)
1c140 7b 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 20 3d 20 {. Mem *pMem =
1c141 28 4d 65 6d 2a 29 70 56 61 6c 3b 0a 20 20 61 70 (Mem*)pVal;. ap
1c142 70 6c 79 4e 75 6d 65 72 69 63 41 66 66 69 6e 69 plyNumericAffini
1c143 74 79 28 70 4d 65 6d 29 3b 0a 20 20 73 74 6f 72 ty(pMem);. stor
1c144 65 54 79 70 65 49 6e 66 6f 28 70 4d 65 6d 2c 20 eTypeInfo(pMem,
1c145 30 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 4d 65 0);. return pMe
1c146 6d 2d 3e 74 79 70 65 3b 0a 7d 0a 0a 2f 2a 0a 2a m->type;.}../*.*
1c147 2a 20 45 78 70 6f 72 74 65 64 20 76 65 72 73 69 * Exported versi
1c148 6f 6e 20 6f 66 20 61 70 70 6c 79 41 66 66 69 6e on of applyAffin
1c149 69 74 79 28 29 2e 20 54 68 69 73 20 6f 6e 65 20 ity(). This one
1c14a 77 6f 72 6b 73 20 6f 6e 20 73 71 6c 69 74 65 33 works on sqlite3
1c14b 5f 76 61 6c 75 65 2a 2c 20 0a 2a 2a 20 6e 6f 74 _value*, .** not
1c14c 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 4d 65 the internal Me
1c14d 6d 2a 20 74 79 70 65 2e 0a 2a 2f 0a 53 51 4c 49 m* type..*/.SQLI
1c14e 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
1c14f 73 71 6c 69 74 65 33 56 61 6c 75 65 41 70 70 6c sqlite3ValueAppl
1c150 79 41 66 66 69 6e 69 74 79 28 0a 20 20 73 71 6c yAffinity(. sql
1c151 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c ite3_value *pVal
1c152 2c 20 0a 20 20 75 38 20 61 66 66 69 6e 69 74 79 , . u8 affinity
1c153 2c 20 0a 20 20 75 38 20 65 6e 63 0a 29 7b 0a 20 , . u8 enc.){.
1c154 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28 28 applyAffinity((
1c155 4d 65 6d 20 2a 29 70 56 61 6c 2c 20 61 66 66 69 Mem *)pVal, affi
1c156 6e 69 74 79 2c 20 65 6e 63 29 3b 0a 7d 0a 0a 23 nity, enc);.}..#
1c157 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 ifdef SQLITE_DEB
1c158 55 47 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 61 UG./*.** Write a
1c159 20 6e 69 63 65 20 73 74 72 69 6e 67 20 72 65 70 nice string rep
1c15a 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 resentation of t
1c15b 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 63 he contents of c
1c15c 65 6c 6c 20 70 4d 65 6d 0a 2a 2a 20 69 6e 74 6f ell pMem.** into
1c15d 20 62 75 66 66 65 72 20 7a 42 75 66 2c 20 6c 65 buffer zBuf, le
1c15e 6e 67 74 68 20 6e 42 75 66 2e 0a 2a 2f 0a 53 51 ngth nBuf..*/.SQ
1c15f 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
1c160 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d d sqlite3VdbeMem
1c161 50 72 65 74 74 79 50 72 69 6e 74 28 4d 65 6d 20 PrettyPrint(Mem
1c162 2a 70 4d 65 6d 2c 20 63 68 61 72 20 2a 7a 42 75 *pMem, char *zBu
1c163 66 29 7b 0a 20 20 63 68 61 72 20 2a 7a 43 73 72 f){. char *zCsr
1c164 20 3d 20 7a 42 75 66 3b 0a 20 20 69 6e 74 20 66 = zBuf;. int f
1c165 20 3d 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 3b 0a = pMem->flags;.
1c166 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 . static const
1c167 63 68 61 72 20 2a 63 6f 6e 73 74 20 65 6e 63 6e char *const encn
1c168 61 6d 65 73 5b 5d 20 3d 20 7b 22 28 58 29 22 2c ames[] = {"(X)",
1c169 20 22 28 38 29 22 2c 20 22 28 31 36 4c 45 29 22 "(8)", "(16LE)"
1c16a 2c 20 22 28 31 36 42 45 29 22 7d 3b 0a 0a 20 20 , "(16BE)"};..
1c16b 69 66 28 20 66 26 4d 45 4d 5f 42 6c 6f 62 20 29 if( f&MEM_Blob )
1c16c 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 {. int i;.
1c16d 20 63 68 61 72 20 63 3b 0a 20 20 20 20 69 66 28 char c;. if(
1c16e 20 66 20 26 20 4d 45 4d 5f 44 79 6e 20 29 7b 0a f & MEM_Dyn ){.
1c16f 20 20 20 20 20 20 63 20 3d 20 27 7a 27 3b 0a 20 c = 'z';.
1c170 20 20 20 20 20 61 73 73 65 72 74 28 20 28 66 20 assert( (f
1c171 26 20 28 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 & (MEM_Static|ME
1c172 4d 5f 45 70 68 65 6d 29 29 3d 3d 30 20 29 3b 0a M_Ephem))==0 );.
1c173 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 66 20 }else if( f
1c174 26 20 4d 45 4d 5f 53 74 61 74 69 63 20 29 7b 0a & MEM_Static ){.
1c175 20 20 20 20 20 20 63 20 3d 20 27 74 27 3b 0a 20 c = 't';.
1c176 20 20 20 20 20 61 73 73 65 72 74 28 20 28 66 20 assert( (f
1c177 26 20 28 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 45 & (MEM_Dyn|MEM_E
1c178 70 68 65 6d 29 29 3d 3d 30 20 29 3b 0a 20 20 20 phem))==0 );.
1c179 20 7d 65 6c 73 65 20 69 66 28 20 66 20 26 20 4d }else if( f & M
1c17a 45 4d 5f 45 70 68 65 6d 20 29 7b 0a 20 20 20 20 EM_Ephem ){.
1c17b 20 20 63 20 3d 20 27 65 27 3b 0a 20 20 20 20 20 c = 'e';.
1c17c 20 61 73 73 65 72 74 28 20 28 66 20 26 20 28 4d assert( (f & (M
1c17d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 44 79 EM_Static|MEM_Dy
1c17e 6e 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20 7d 65 n))==0 );. }e
1c17f 6c 73 65 7b 0a 20 20 20 20 20 20 63 20 3d 20 27 lse{. c = '
1c180 73 27 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73 s';. }.. s
1c181 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
1c182 31 30 30 2c 20 7a 43 73 72 2c 20 22 25 63 22 2c 100, zCsr, "%c",
1c183 20 63 29 3b 0a 20 20 20 20 7a 43 73 72 20 2b 3d c);. zCsr +=
1c184 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 sqlite3Strlen30
1c185 28 7a 43 73 72 29 3b 0a 20 20 20 20 73 71 6c 69 (zCsr);. sqli
1c186 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 31 30 30 te3_snprintf(100
1c187 2c 20 7a 43 73 72 2c 20 22 25 64 5b 22 2c 20 70 , zCsr, "%d[", p
1c188 4d 65 6d 2d 3e 6e 29 3b 0a 20 20 20 20 7a 43 73 Mem->n);. zCs
1c189 72 20 2b 3d 20 73 71 6c 69 74 65 33 53 74 72 6c r += sqlite3Strl
1c18a 65 6e 33 30 28 7a 43 73 72 29 3b 0a 20 20 20 20 en30(zCsr);.
1c18b 66 6f 72 28 69 3d 30 3b 20 69 3c 31 36 20 26 26 for(i=0; i<16 &&
1c18c 20 69 3c 70 4d 65 6d 2d 3e 6e 3b 20 69 2b 2b 29 i<pMem->n; i++)
1c18d 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f {. sqlite3_
1c18e 73 6e 70 72 69 6e 74 66 28 31 30 30 2c 20 7a 43 snprintf(100, zC
1c18f 73 72 2c 20 22 25 30 32 58 22 2c 20 28 28 69 6e sr, "%02X", ((in
1c190 74 29 70 4d 65 6d 2d 3e 7a 5b 69 5d 20 26 20 30 t)pMem->z[i] & 0
1c191 78 46 46 29 29 3b 0a 20 20 20 20 20 20 7a 43 73 xFF));. zCs
1c192 72 20 2b 3d 20 73 71 6c 69 74 65 33 53 74 72 6c r += sqlite3Strl
1c193 65 6e 33 30 28 7a 43 73 72 29 3b 0a 20 20 20 20 en30(zCsr);.
1c194 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 }. for(i=0; i
1c195 3c 31 36 20 26 26 20 69 3c 70 4d 65 6d 2d 3e 6e <16 && i<pMem->n
1c196 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 63 68 ; i++){. ch
1c197 61 72 20 7a 20 3d 20 70 4d 65 6d 2d 3e 7a 5b 69 ar z = pMem->z[i
1c198 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 3c 33 ];. if( z<3
1c199 32 20 7c 7c 20 7a 3e 31 32 36 20 29 20 2a 7a 43 2 || z>126 ) *zC
1c19a 73 72 2b 2b 20 3d 20 27 2e 27 3b 0a 20 20 20 20 sr++ = '.';.
1c19b 20 20 65 6c 73 65 20 2a 7a 43 73 72 2b 2b 20 3d else *zCsr++ =
1c19c 20 7a 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73 z;. }.. s
1c19d 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
1c19e 31 30 30 2c 20 7a 43 73 72 2c 20 22 5d 25 73 22 100, zCsr, "]%s"
1c19f 2c 20 65 6e 63 6e 61 6d 65 73 5b 70 4d 65 6d 2d , encnames[pMem-
1c1a0 3e 65 6e 63 5d 29 3b 0a 20 20 20 20 7a 43 73 72 >enc]);. zCsr
1c1a1 20 2b 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 += sqlite3Strle
1c1a2 6e 33 30 28 7a 43 73 72 29 3b 0a 20 20 20 20 69 n30(zCsr);. i
1c1a3 66 28 20 66 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 f( f & MEM_Zero
1c1a4 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
1c1a5 5f 73 6e 70 72 69 6e 74 66 28 31 30 30 2c 20 7a _snprintf(100, z
1c1a6 43 73 72 2c 22 2b 25 64 7a 22 2c 70 4d 65 6d 2d Csr,"+%dz",pMem-
1c1a7 3e 75 2e 6e 5a 65 72 6f 29 3b 0a 20 20 20 20 20 >u.nZero);.
1c1a8 20 7a 43 73 72 20 2b 3d 20 73 71 6c 69 74 65 33 zCsr += sqlite3
1c1a9 53 74 72 6c 65 6e 33 30 28 7a 43 73 72 29 3b 0a Strlen30(zCsr);.
1c1aa 20 20 20 20 7d 0a 20 20 20 20 2a 7a 43 73 72 20 }. *zCsr
1c1ab 3d 20 27 5c 30 27 3b 0a 20 20 7d 65 6c 73 65 20 = '\0';. }else
1c1ac 69 66 28 20 66 20 26 20 4d 45 4d 5f 53 74 72 20 if( f & MEM_Str
1c1ad 29 7b 0a 20 20 20 20 69 6e 74 20 6a 2c 20 6b 3b ){. int j, k;
1c1ae 0a 20 20 20 20 7a 42 75 66 5b 30 5d 20 3d 20 27 . zBuf[0] = '
1c1af 20 27 3b 0a 20 20 20 20 69 66 28 20 66 20 26 20 ';. if( f &
1c1b0 4d 45 4d 5f 44 79 6e 20 29 7b 0a 20 20 20 20 20 MEM_Dyn ){.
1c1b1 20 7a 42 75 66 5b 31 5d 20 3d 20 27 7a 27 3b 0a zBuf[1] = 'z';.
1c1b2 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28 66 assert( (f
1c1b3 20 26 20 28 4d 45 4d 5f 53 74 61 74 69 63 7c 4d & (MEM_Static|M
1c1b4 45 4d 5f 45 70 68 65 6d 29 29 3d 3d 30 20 29 3b EM_Ephem))==0 );
1c1b5 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 66 . }else if( f
1c1b6 20 26 20 4d 45 4d 5f 53 74 61 74 69 63 20 29 7b & MEM_Static ){
1c1b7 0a 20 20 20 20 20 20 7a 42 75 66 5b 31 5d 20 3d . zBuf[1] =
1c1b8 20 27 74 27 3b 0a 20 20 20 20 20 20 61 73 73 65 't';. asse
1c1b9 72 74 28 20 28 66 20 26 20 28 4d 45 4d 5f 44 79 rt( (f & (MEM_Dy
1c1ba 6e 7c 4d 45 4d 5f 45 70 68 65 6d 29 29 3d 3d 30 n|MEM_Ephem))==0
1c1bb 20 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 );. }else if
1c1bc 28 20 66 20 26 20 4d 45 4d 5f 45 70 68 65 6d 20 ( f & MEM_Ephem
1c1bd 29 7b 0a 20 20 20 20 20 20 7a 42 75 66 5b 31 5d ){. zBuf[1]
1c1be 20 3d 20 27 65 27 3b 0a 20 20 20 20 20 20 61 73 = 'e';. as
1c1bf 73 65 72 74 28 20 28 66 20 26 20 28 4d 45 4d 5f sert( (f & (MEM_
1c1c0 53 74 61 74 69 63 7c 4d 45 4d 5f 44 79 6e 29 29 Static|MEM_Dyn))
1c1c1 3d 3d 30 20 29 3b 0a 20 20 20 20 7d 65 6c 73 65 ==0 );. }else
1c1c2 7b 0a 20 20 20 20 20 20 7a 42 75 66 5b 31 5d 20 {. zBuf[1]
1c1c3 3d 20 27 73 27 3b 0a 20 20 20 20 7d 0a 20 20 20 = 's';. }.
1c1c4 20 6b 20 3d 20 32 3b 0a 20 20 20 20 73 71 6c 69 k = 2;. sqli
1c1c5 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 31 30 30 te3_snprintf(100
1c1c6 2c 20 26 7a 42 75 66 5b 6b 5d 2c 20 22 25 64 22 , &zBuf[k], "%d"
1c1c7 2c 20 70 4d 65 6d 2d 3e 6e 29 3b 0a 20 20 20 20 , pMem->n);.
1c1c8 6b 20 2b 3d 20 73 71 6c 69 74 65 33 53 74 72 6c k += sqlite3Strl
1c1c9 65 6e 33 30 28 26 7a 42 75 66 5b 6b 5d 29 3b 0a en30(&zBuf[k]);.
1c1ca 20 20 20 20 7a 42 75 66 5b 6b 2b 2b 5d 20 3d 20 zBuf[k++] =
1c1cb 27 5b 27 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 30 '[';. for(j=0
1c1cc 3b 20 6a 3c 31 35 20 26 26 20 6a 3c 70 4d 65 6d ; j<15 && j<pMem
1c1cd 2d 3e 6e 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 ->n; j++){.
1c1ce 20 75 38 20 63 20 3d 20 70 4d 65 6d 2d 3e 7a 5b u8 c = pMem->z[
1c1cf 6a 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 63 3e j];. if( c>
1c1d0 3d 30 78 32 30 20 26 26 20 63 3c 30 78 37 66 20 =0x20 && c<0x7f
1c1d1 29 7b 0a 20 20 20 20 20 20 20 20 7a 42 75 66 5b ){. zBuf[
1c1d2 6b 2b 2b 5d 20 3d 20 63 3b 0a 20 20 20 20 20 20 k++] = c;.
1c1d3 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 7a }else{. z
1c1d4 42 75 66 5b 6b 2b 2b 5d 20 3d 20 27 2e 27 3b 0a Buf[k++] = '.';.
1c1d5 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
1c1d6 20 20 7a 42 75 66 5b 6b 2b 2b 5d 20 3d 20 27 5d zBuf[k++] = ']
1c1d7 27 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 ';. sqlite3_s
1c1d8 6e 70 72 69 6e 74 66 28 31 30 30 2c 26 7a 42 75 nprintf(100,&zBu
1c1d9 66 5b 6b 5d 2c 20 65 6e 63 6e 61 6d 65 73 5b 70 f[k], encnames[p
1c1da 4d 65 6d 2d 3e 65 6e 63 5d 29 3b 0a 20 20 20 20 Mem->enc]);.
1c1db 6b 20 2b 3d 20 73 71 6c 69 74 65 33 53 74 72 6c k += sqlite3Strl
1c1dc 65 6e 33 30 28 26 7a 42 75 66 5b 6b 5d 29 3b 0a en30(&zBuf[k]);.
1c1dd 20 20 20 20 7a 42 75 66 5b 6b 2b 2b 5d 20 3d 20 zBuf[k++] =
1c1de 30 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0;. }.}.#endif.
1c1df 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 .#ifdef SQLITE_D
1c1e0 45 42 55 47 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 EBUG./*.** Print
1c1e1 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 61 20 the value of a
1c1e2 72 65 67 69 73 74 65 72 20 66 6f 72 20 74 72 61 register for tra
1c1e3 63 69 6e 67 20 70 75 72 70 6f 73 65 73 3a 0a 2a cing purposes:.*
1c1e4 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 /.static void me
1c1e5 6d 54 72 61 63 65 50 72 69 6e 74 28 46 49 4c 45 mTracePrint(FILE
1c1e6 20 2a 6f 75 74 2c 20 4d 65 6d 20 2a 70 29 7b 0a *out, Mem *p){.
1c1e7 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 20 26 if( p->flags &
1c1e8 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 MEM_Null ){.
1c1e9 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 fprintf(out, "
1c1ea 4e 55 4c 4c 22 29 3b 0a 20 20 7d 65 6c 73 65 20 NULL");. }else
1c1eb 69 66 28 20 28 70 2d 3e 66 6c 61 67 73 20 26 20 if( (p->flags &
1c1ec 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 53 74 72 (MEM_Int|MEM_Str
1c1ed 29 29 3d 3d 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d ))==(MEM_Int|MEM
1c1ee 5f 53 74 72 29 20 29 7b 0a 20 20 20 20 66 70 72 _Str) ){. fpr
1c1ef 69 6e 74 66 28 6f 75 74 2c 20 22 20 73 69 3a 25 intf(out, " si:%
1c1f0 6c 6c 64 22 2c 20 70 2d 3e 75 2e 69 29 3b 0a 20 lld", p->u.i);.
1c1f1 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 66 6c }else if( p->fl
1c1f2 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29 7b ags & MEM_Int ){
1c1f3 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 . fprintf(out
1c1f4 2c 20 22 20 69 3a 25 6c 6c 64 22 2c 20 70 2d 3e , " i:%lld", p->
1c1f5 75 2e 69 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 u.i);.#ifndef SQ
1c1f6 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 LITE_OMIT_FLOATI
1c1f7 4e 47 5f 50 4f 49 4e 54 0a 20 20 7d 65 6c 73 65 NG_POINT. }else
1c1f8 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 if( p->flags &
1c1f9 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20 MEM_Real ){.
1c1fa 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 72 fprintf(out, " r
1c1fb 3a 25 67 22 2c 20 70 2d 3e 72 29 3b 0a 23 65 6e :%g", p->r);.#en
1c1fc 64 69 66 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 dif. }else if(
1c1fd 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 p->flags & MEM_R
1c1fe 6f 77 53 65 74 20 29 7b 0a 20 20 20 20 66 70 72 owSet ){. fpr
1c1ff 69 6e 74 66 28 6f 75 74 2c 20 22 20 28 72 6f 77 intf(out, " (row
1c200 73 65 74 29 22 29 3b 0a 20 20 7d 65 6c 73 65 7b set)");. }else{
1c201 0a 20 20 20 20 63 68 61 72 20 7a 42 75 66 5b 32 . char zBuf[2
1c202 30 30 5d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 00];. sqlite3
1c203 56 64 62 65 4d 65 6d 50 72 65 74 74 79 50 72 69 VdbeMemPrettyPri
1c204 6e 74 28 70 2c 20 7a 42 75 66 29 3b 0a 20 20 20 nt(p, zBuf);.
1c205 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 fprintf(out, "
1c206 22 29 3b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 ");. fprintf(
1c207 6f 75 74 2c 20 22 25 73 22 2c 20 7a 42 75 66 29 out, "%s", zBuf)
1c208 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 ;. }.}.static v
1c209 6f 69 64 20 72 65 67 69 73 74 65 72 54 72 61 63 oid registerTrac
1c20a 65 28 46 49 4c 45 20 2a 6f 75 74 2c 20 69 6e 74 e(FILE *out, int
1c20b 20 69 52 65 67 2c 20 4d 65 6d 20 2a 70 29 7b 0a iReg, Mem *p){.
1c20c 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 fprintf(out, "
1c20d 52 45 47 5b 25 64 5d 20 3d 20 22 2c 20 69 52 65 REG[%d] = ", iRe
1c20e 67 29 3b 0a 20 20 6d 65 6d 54 72 61 63 65 50 72 g);. memTracePr
1c20f 69 6e 74 28 6f 75 74 2c 20 70 29 3b 0a 20 20 66 int(out, p);. f
1c210 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 5c 6e 22 printf(out, "\n"
1c211 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 );.}.#endif..#if
1c212 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 def SQLITE_DEBUG
1c213 0a 23 20 20 64 65 66 69 6e 65 20 52 45 47 49 53 .# define REGIS
1c214 54 45 52 5f 54 52 41 43 45 28 52 2c 4d 29 20 69 TER_TRACE(R,M) i
1c215 66 28 70 2d 3e 74 72 61 63 65 29 72 65 67 69 73 f(p->trace)regis
1c216 74 65 72 54 72 61 63 65 28 70 2d 3e 74 72 61 63 terTrace(p->trac
1c217 65 2c 52 2c 4d 29 0a 23 65 6c 73 65 0a 23 20 20 e,R,M).#else.#
1c218 64 65 66 69 6e 65 20 52 45 47 49 53 54 45 52 5f define REGISTER_
1c219 54 52 41 43 45 28 52 2c 4d 29 0a 23 65 6e 64 69 TRACE(R,M).#endi
1c21a 66 0a 0a 0a 23 69 66 64 65 66 20 56 44 42 45 5f f...#ifdef VDBE_
1c21b 50 52 4f 46 49 4c 45 0a 0a 2f 2a 20 0a 2a 2a 20 PROFILE../* .**
1c21c 68 77 74 69 6d 65 2e 68 20 63 6f 6e 74 61 69 6e hwtime.h contain
1c21d 73 20 69 6e 6c 69 6e 65 20 61 73 73 65 6d 62 6c s inline assembl
1c21e 65 72 20 63 6f 64 65 20 66 6f 72 20 69 6d 70 6c er code for impl
1c21f 65 6d 65 6e 74 69 6e 67 20 0a 2a 2a 20 68 69 67 ementing .** hig
1c220 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 69 h-performance ti
1c221 6d 69 6e 67 20 72 6f 75 74 69 6e 65 73 2e 0a 2a ming routines..*
1c222 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
1c223 2a 20 49 6e 63 6c 75 64 65 20 68 77 74 69 6d 65 * Include hwtime
1c224 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 .h in the middle
1c225 20 6f 66 20 76 64 62 65 2e 63 20 2a 2a 2a 2a 2a of vdbe.c *****
1c226 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c227 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
1c228 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 68 77 74 * Begin file hwt
1c229 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ime.h **********
1c22a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c22b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c22c 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4d 61 79 /./*.** 2008 May
1c22d 20 32 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 27.**.** The au
1c22e 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 thor disclaims c
1c22f 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 opyright to this
1c230 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 source code. I
1c231 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 n place of.** a
1c232 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 legal notice, he
1c233 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 re is a blessing
1c234 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 :.**.** May y
1c235 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e ou do good and n
1c236 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d ot evil..** M
1c237 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 ay you find forg
1c238 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 iveness for your
1c239 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 self and forgive
1c23a 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d others..** M
1c23b 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 ay you share fre
1c23c 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e ely, never takin
1c23d 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 g more than you
1c23e 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a give..**.*******
1c23f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c240 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c241 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c242 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c243 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 *******.**.** Th
1c244 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 is file contains
1c245 20 69 6e 6c 69 6e 65 20 61 73 6d 20 63 6f 64 65 inline asm code
1c246 20 66 6f 72 20 72 65 74 72 69 65 76 69 6e 67 20 for retrieving
1c247 22 68 69 67 68 2d 70 65 72 66 6f 72 6d 61 6e 63 "high-performanc
1c248 65 22 0a 2a 2a 20 63 6f 75 6e 74 65 72 73 20 66 e".** counters f
1c249 6f 72 20 78 38 36 20 63 6c 61 73 73 20 43 50 55 or x86 class CPU
1c24a 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 68 77 s..**.** $Id: hw
1c24b 74 69 6d 65 2e 68 2c 76 20 31 2e 33 20 32 30 30 time.h,v 1.3 200
1c24c 38 2f 30 38 2f 30 31 20 31 34 3a 33 33 3a 31 35 8/08/01 14:33:15
1c24d 20 73 68 61 6e 65 20 45 78 70 20 24 0a 2a 2f 0a shane Exp $.*/.
1c24e 23 69 66 6e 64 65 66 20 5f 48 57 54 49 4d 45 5f #ifndef _HWTIME_
1c24f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 48 57 54 49 H_.#define _HWTI
1c250 4d 45 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 ME_H_../*.** The
1c251 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 following routi
1c252 6e 65 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e ne only works on
1c253 20 70 65 6e 74 69 75 6d 2d 63 6c 61 73 73 20 28 pentium-class (
1c254 6f 72 20 6e 65 77 65 72 29 20 70 72 6f 63 65 73 or newer) proces
1c255 73 6f 72 73 2e 0a 2a 2a 20 49 74 20 75 73 65 73 sors..** It uses
1c256 20 74 68 65 20 52 44 54 53 43 20 6f 70 63 6f 64 the RDTSC opcod
1c257 65 20 74 6f 20 72 65 61 64 20 74 68 65 20 63 79 e to read the cy
1c258 63 6c 65 20 63 6f 75 6e 74 20 76 61 6c 75 65 20 cle count value
1c259 6f 75 74 20 6f 66 20 74 68 65 0a 2a 2a 20 70 72 out of the.** pr
1c25a 6f 63 65 73 73 6f 72 20 61 6e 64 20 72 65 74 75 ocessor and retu
1c25b 72 6e 73 20 74 68 61 74 20 76 61 6c 75 65 2e 20 rns that value.
1c25c 20 54 68 69 73 20 63 61 6e 20 62 65 20 75 73 65 This can be use
1c25d 64 20 66 6f 72 20 68 69 67 68 2d 72 65 73 0a 2a d for high-res.*
1c25e 2a 20 70 72 6f 66 69 6c 69 6e 67 2e 0a 2a 2f 0a * profiling..*/.
1c25f 23 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 #if (defined(__G
1c260 4e 55 43 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 NUC__) || define
1c261 64 28 5f 4d 53 43 5f 56 45 52 29 29 20 26 26 20 d(_MSC_VER)) &&
1c262 5c 0a 20 20 20 20 20 20 28 64 65 66 69 6e 65 64 \. (defined
1c263 28 69 33 38 36 29 20 7c 7c 20 64 65 66 69 6e 65 (i386) || define
1c264 64 28 5f 5f 69 33 38 36 5f 5f 29 20 7c 7c 20 64 d(__i386__) || d
1c265 65 66 69 6e 65 64 28 5f 4d 5f 49 58 38 36 29 29 efined(_M_IX86))
1c266 0a 0a 20 20 23 69 66 20 64 65 66 69 6e 65 64 28 .. #if defined(
1c267 5f 5f 47 4e 55 43 5f 5f 29 0a 0a 20 20 5f 5f 69 __GNUC__).. __i
1c268 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 nline__ sqlite_u
1c269 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 int64 sqlite3Hwt
1c26a 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 ime(void){.
1c26b 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6c 6f 2c unsigned int lo,
1c26c 20 68 69 3b 0a 20 20 20 20 20 5f 5f 61 73 6d 5f hi;. __asm_
1c26d 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 _ __volatile__ (
1c26e 22 72 64 74 73 63 22 20 3a 20 22 3d 61 22 20 28 "rdtsc" : "=a" (
1c26f 6c 6f 29 2c 20 22 3d 64 22 20 28 68 69 29 29 3b lo), "=d" (hi));
1c270 0a 20 20 20 20 20 72 65 74 75 72 6e 20 28 73 71 . return (sq
1c271 6c 69 74 65 5f 75 69 6e 74 36 34 29 68 69 20 3c lite_uint64)hi <
1c272 3c 20 33 32 20 7c 20 6c 6f 3b 0a 20 20 7d 0a 0a < 32 | lo;. }..
1c273 20 20 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28 #elif defined(
1c274 5f 4d 53 43 5f 56 45 52 29 0a 0a 20 20 5f 5f 64 _MSC_VER).. __d
1c275 65 63 6c 73 70 65 63 28 6e 61 6b 65 64 29 20 5f eclspec(naked) _
1c276 5f 69 6e 6c 69 6e 65 20 73 71 6c 69 74 65 5f 75 _inline sqlite_u
1c277 69 6e 74 36 34 20 5f 5f 63 64 65 63 6c 20 73 71 int64 __cdecl sq
1c278 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 lite3Hwtime(void
1c279 29 7b 0a 20 20 20 20 20 5f 5f 61 73 6d 20 7b 0a ){. __asm {.
1c27a 20 20 20 20 20 20 20 20 72 64 74 73 63 0a 20 20 rdtsc.
1c27b 20 20 20 20 20 20 72 65 74 20 20 20 20 20 20 20 ret
1c27c 3b 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 61 ; return value a
1c27d 74 20 45 44 58 3a 45 41 58 0a 20 20 20 20 20 7d t EDX:EAX. }
1c27e 0a 20 20 7d 0a 0a 20 20 23 65 6e 64 69 66 0a 0a . }.. #endif..
1c27f 23 65 6c 69 66 20 28 64 65 66 69 6e 65 64 28 5f #elif (defined(_
1c280 5f 47 4e 55 43 5f 5f 29 20 26 26 20 64 65 66 69 _GNUC__) && defi
1c281 6e 65 64 28 5f 5f 78 38 36 5f 36 34 5f 5f 29 29 ned(__x86_64__))
1c282 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 .. __inline__ s
1c283 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c qlite_uint64 sql
1c284 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 ite3Hwtime(void)
1c285 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 {. unsigned
1c286 20 6c 6f 6e 67 20 76 61 6c 3b 0a 20 20 20 20 20 long val;.
1c287 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 __asm__ __volat
1c288 69 6c 65 5f 5f 20 28 22 72 64 74 73 63 22 20 3a ile__ ("rdtsc" :
1c289 20 22 3d 41 22 20 28 76 61 6c 29 29 3b 0a 20 20 "=A" (val));.
1c28a 20 20 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a return val;.
1c28b 20 20 7d 0a 20 0a 23 65 6c 69 66 20 28 64 65 66 }. .#elif (def
1c28c 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 ined(__GNUC__) &
1c28d 26 20 64 65 66 69 6e 65 64 28 5f 5f 70 70 63 5f & defined(__ppc_
1c28e 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f _)).. __inline_
1c28f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 _ sqlite_uint64
1c290 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f sqlite3Hwtime(vo
1c291 69 64 29 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 id){. unsig
1c292 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 72 65 ned long long re
1c293 74 76 61 6c 3b 0a 20 20 20 20 20 20 75 6e 73 69 tval;. unsi
1c294 67 6e 65 64 20 6c 6f 6e 67 20 6a 75 6e 6b 3b 0a gned long junk;.
1c295 20 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f __asm__ __
1c296 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 5c 6e 5c volatile__ ("\n\
1c297 0a 20 20 20 20 20 20 20 20 20 20 31 3a 20 20 20 . 1:
1c298 20 20 20 6d 66 74 62 75 20 20 20 25 31 5c 6e 5c mftbu %1\n\
1c299 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1c29a 20 20 20 6d 66 74 62 20 20 20 20 25 4c 30 5c 6e mftb %L0\n
1c29b 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 \.
1c29c 20 20 20 20 6d 66 74 62 75 20 20 20 25 30 5c 6e mftbu %0\n
1c29d 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 \.
1c29e 20 20 20 20 63 6d 70 77 20 20 20 20 25 30 2c 25 cmpw %0,%
1c29f 31 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 1\n\.
1c2a0 20 20 20 20 20 20 20 62 6e 65 20 20 20 20 20 31 bne 1
1c2a1 62 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 b".
1c2a2 20 20 20 20 20 3a 20 22 3d 72 22 20 28 72 65 74 : "=r" (ret
1c2a3 76 61 6c 29 2c 20 22 3d 72 22 20 28 6a 75 6e 6b val), "=r" (junk
1c2a4 29 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ));. return
1c2a5 20 72 65 74 76 61 6c 3b 0a 20 20 7d 0a 0a 23 65 retval;. }..#e
1c2a6 6c 73 65 0a 0a 20 20 23 65 72 72 6f 72 20 4e 65 lse.. #error Ne
1c2a7 65 64 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ed implementatio
1c2a8 6e 20 6f 66 20 73 71 6c 69 74 65 33 48 77 74 69 n of sqlite3Hwti
1c2a9 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 20 70 6c me() for your pl
1c2aa 61 74 66 6f 72 6d 2e 0a 0a 20 20 2f 2a 0a 20 20 atform... /*.
1c2ab 2a 2a 20 54 6f 20 63 6f 6d 70 69 6c 65 20 77 69 ** To compile wi
1c2ac 74 68 6f 75 74 20 69 6d 70 6c 65 6d 65 6e 74 69 thout implementi
1c2ad 6e 67 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 ng sqlite3Hwtime
1c2ae 28 29 20 66 6f 72 20 79 6f 75 72 20 70 6c 61 74 () for your plat
1c2af 66 6f 72 6d 2c 0a 20 20 2a 2a 20 79 6f 75 20 63 form,. ** you c
1c2b0 61 6e 20 72 65 6d 6f 76 65 20 74 68 65 20 61 62 an remove the ab
1c2b1 6f 76 65 20 23 65 72 72 6f 72 20 61 6e 64 20 75 ove #error and u
1c2b2 73 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 se the following
1c2b3 0a 20 20 2a 2a 20 73 74 75 62 20 66 75 6e 63 74 . ** stub funct
1c2b4 69 6f 6e 2e 20 20 59 6f 75 20 77 69 6c 6c 20 6c ion. You will l
1c2b5 6f 73 65 20 74 69 6d 69 6e 67 20 73 75 70 70 6f ose timing suppo
1c2b6 72 74 20 66 6f 72 20 6d 61 6e 79 0a 20 20 2a 2a rt for many. **
1c2b7 20 6f 66 20 74 68 65 20 64 65 62 75 67 67 69 6e of the debuggin
1c2b8 67 20 61 6e 64 20 74 65 73 74 69 6e 67 20 75 74 g and testing ut
1c2b9 69 6c 69 74 69 65 73 2c 20 62 75 74 20 69 74 20 ilities, but it
1c2ba 73 68 6f 75 6c 64 20 61 74 0a 20 20 2a 2a 20 6c should at. ** l
1c2bb 65 61 73 74 20 63 6f 6d 70 69 6c 65 20 61 6e 64 east compile and
1c2bc 20 72 75 6e 2e 0a 20 20 2a 2f 0a 53 51 4c 49 54 run.. */.SQLIT
1c2bd 45 5f 50 52 49 56 41 54 45 20 20 20 73 71 6c 69 E_PRIVATE sqli
1c2be 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 te_uint64 sqlite
1c2bf 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 20 72 3Hwtime(void){ r
1c2c0 65 74 75 72 6e 20 28 28 73 71 6c 69 74 65 5f 75 eturn ((sqlite_u
1c2c1 69 6e 74 36 34 29 30 29 3b 20 7d 0a 0a 23 65 6e int64)0); }..#en
1c2c2 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 dif..#endif /* !
1c2c3 64 65 66 69 6e 65 64 28 5f 48 57 54 49 4d 45 5f defined(_HWTIME_
1c2c4 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a H_) */../*******
1c2c5 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 68 ******* End of h
1c2c6 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a wtime.h ********
1c2c7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c2c8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c2c9 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
1c2ca 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 ******* Continui
1c2cb 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 ng where we left
1c2cc 20 6f 66 66 20 69 6e 20 76 64 62 65 2e 63 20 2a off in vdbe.c *
1c2cd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c2ce 2a 2a 2a 2a 2a 2a 2f 0a 0a 23 65 6e 64 69 66 0a ******/..#endif.
1c2cf 0a 2f 2a 0a 2a 2a 20 54 68 65 20 43 48 45 43 4b ./*.** The CHECK
1c2d0 5f 46 4f 52 5f 49 4e 54 45 52 52 55 50 54 20 6d _FOR_INTERRUPT m
1c2d1 61 63 72 6f 20 64 65 66 69 6e 65 64 20 68 65 72 acro defined her
1c2d2 65 20 6c 6f 6f 6b 73 20 74 6f 20 73 65 65 20 69 e looks to see i
1c2d3 66 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 f the.** sqlite3
1c2d4 5f 69 6e 74 65 72 72 75 70 74 28 29 20 72 6f 75 _interrupt() rou
1c2d5 74 69 6e 65 20 68 61 73 20 62 65 65 6e 20 63 61 tine has been ca
1c2d6 6c 6c 65 64 2e 20 20 49 66 20 69 74 20 68 61 73 lled. If it has
1c2d7 20 62 65 65 6e 2c 20 74 68 65 6e 0a 2a 2a 20 70 been, then.** p
1c2d8 72 6f 63 65 73 73 69 6e 67 20 6f 66 20 74 68 65 rocessing of the
1c2d9 20 56 44 42 45 20 70 72 6f 67 72 61 6d 20 69 73 VDBE program is
1c2da 20 69 6e 74 65 72 72 75 70 74 65 64 2e 0a 2a 2a interrupted..**
1c2db 0a 2a 2a 20 54 68 69 73 20 6d 61 63 72 6f 20 61 .** This macro a
1c2dc 64 64 65 64 20 74 6f 20 65 76 65 72 79 20 69 6e dded to every in
1c2dd 73 74 72 75 63 74 69 6f 6e 20 74 68 61 74 20 64 struction that d
1c2de 6f 65 73 20 61 20 6a 75 6d 70 20 69 6e 20 6f 72 oes a jump in or
1c2df 64 65 72 20 74 6f 0a 2a 2a 20 69 6d 70 6c 65 6d der to.** implem
1c2e0 65 6e 74 20 61 20 6c 6f 6f 70 2e 20 20 54 68 69 ent a loop. Thi
1c2e1 73 20 74 65 73 74 20 75 73 65 64 20 74 6f 20 62 s test used to b
1c2e2 65 20 6f 6e 20 65 76 65 72 79 20 73 69 6e 67 6c e on every singl
1c2e3 65 20 69 6e 73 74 72 75 63 74 69 6f 6e 2c 0a 2a e instruction,.*
1c2e4 2a 20 62 75 74 20 74 68 61 74 20 6d 65 61 6e 74 * but that meant
1c2e5 20 77 65 20 6d 6f 72 65 20 74 65 73 74 69 6e 67 we more testing
1c2e6 20 74 68 61 74 20 77 65 20 6e 65 65 64 65 64 2e that we needed.
1c2e7 20 20 42 79 20 6f 6e 6c 79 20 74 65 73 74 69 6e By only testin
1c2e8 67 20 74 68 65 0a 2a 2a 20 66 6c 61 67 20 6f 6e g the.** flag on
1c2e9 20 6a 75 6d 70 20 69 6e 73 74 72 75 63 74 69 6f jump instructio
1c2ea 6e 73 2c 20 77 65 20 67 65 74 20 61 20 28 73 6d ns, we get a (sm
1c2eb 61 6c 6c 29 20 73 70 65 65 64 20 69 6d 70 72 6f all) speed impro
1c2ec 76 65 6d 65 6e 74 2e 0a 2a 2f 0a 23 64 65 66 69 vement..*/.#defi
1c2ed 6e 65 20 43 48 45 43 4b 5f 46 4f 52 5f 49 4e 54 ne CHECK_FOR_INT
1c2ee 45 52 52 55 50 54 20 5c 0a 20 20 20 69 66 28 20 ERRUPT \. if(
1c2ef 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72 75 db->u1.isInterru
1c2f0 70 74 65 64 20 29 20 67 6f 74 6f 20 61 62 6f 72 pted ) goto abor
1c2f1 74 5f 64 75 65 5f 74 6f 5f 69 6e 74 65 72 72 75 t_due_to_interru
1c2f2 70 74 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 pt;..#ifdef SQLI
1c2f3 54 45 5f 44 45 42 55 47 0a 73 74 61 74 69 63 20 TE_DEBUG.static
1c2f4 69 6e 74 20 66 69 6c 65 45 78 69 73 74 73 28 73 int fileExists(s
1c2f5 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 qlite3 *db, cons
1c2f6 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 29 7b 0a t char *zFile){.
1c2f7 20 20 69 6e 74 20 72 65 73 20 3d 20 30 3b 0a 20 int res = 0;.
1c2f8 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
1c2f9 5f 4f 4b 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 _OK;.#ifdef SQLI
1c2fa 54 45 5f 54 45 53 54 0a 20 20 2f 2a 20 49 66 20 TE_TEST. /* If
1c2fb 77 65 20 61 72 65 20 63 75 72 72 65 6e 74 6c 79 we are currently
1c2fc 20 74 65 73 74 69 6e 67 20 49 4f 20 65 72 72 6f testing IO erro
1c2fd 72 73 2c 20 74 68 65 6e 20 64 6f 20 6e 6f 74 20 rs, then do not
1c2fe 63 61 6c 6c 20 4f 73 41 63 63 65 73 73 28 29 20 call OsAccess()
1c2ff 74 6f 0a 20 20 2a 2a 20 74 65 73 74 20 66 6f 72 to. ** test for
1c300 20 74 68 65 20 70 72 65 73 65 6e 63 65 20 6f 66 the presence of
1c301 20 7a 46 69 6c 65 2e 20 54 68 69 73 20 69 73 20 zFile. This is
1c302 62 65 63 61 75 73 65 20 61 6e 79 20 49 4f 20 65 because any IO e
1c303 72 72 6f 72 20 74 68 61 74 0a 20 20 2a 2a 20 6f rror that. ** o
1c304 63 63 75 72 73 20 68 65 72 65 20 77 69 6c 6c 20 ccurs here will
1c305 6e 6f 74 20 62 65 20 72 65 70 6f 72 74 65 64 2c not be reported,
1c306 20 63 61 75 73 69 6e 67 20 74 68 65 20 74 65 73 causing the tes
1c307 74 20 74 6f 20 66 61 69 6c 2e 0a 20 20 2a 2f 0a t to fail.. */.
1c308 20 20 65 78 74 65 72 6e 20 69 6e 74 20 73 71 6c extern int sql
1c309 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 ite3_io_error_pe
1c30a 6e 64 69 6e 67 3b 0a 20 20 69 66 28 20 73 71 6c nding;. if( sql
1c30b 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 ite3_io_error_pe
1c30c 6e 64 69 6e 67 3c 3d 30 20 29 0a 23 65 6e 64 69 nding<=0 ).#endi
1c30d 66 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 f. rc = sqlit
1c30e 65 33 4f 73 41 63 63 65 73 73 28 64 62 2d 3e 70 e3OsAccess(db->p
1c30f 56 66 73 2c 20 7a 46 69 6c 65 2c 20 53 51 4c 49 Vfs, zFile, SQLI
1c310 54 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 TE_ACCESS_EXISTS
1c311 2c 20 26 72 65 73 29 3b 0a 20 20 72 65 74 75 72 , &res);. retur
1c312 6e 20 28 72 65 73 20 26 26 20 72 63 3d 3d 53 51 n (res && rc==SQ
1c313 4c 49 54 45 5f 4f 4b 29 3b 0a 7d 0a 23 65 6e 64 LITE_OK);.}.#end
1c314 69 66 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 if..#ifndef NDEB
1c315 55 47 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 UG./*.** This fu
1c316 6e 63 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 63 nction is only c
1c317 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74 68 69 alled from withi
1c318 6e 20 61 6e 20 61 73 73 65 72 74 28 29 20 65 78 n an assert() ex
1c319 70 72 65 73 73 69 6f 6e 2e 20 49 74 0a 2a 2a 20 pression. It.**
1c31a 63 68 65 63 6b 73 20 74 68 61 74 20 74 68 65 20 checks that the
1c31b 73 71 6c 69 74 65 33 2e 6e 54 72 61 6e 73 61 63 sqlite3.nTransac
1c31c 74 69 6f 6e 20 76 61 72 69 61 62 6c 65 20 69 73 tion variable is
1c31d 20 63 6f 72 72 65 63 74 6c 79 20 73 65 74 20 74 correctly set t
1c31e 6f 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 o.** the number
1c31f 6f 66 20 6e 6f 6e 2d 74 72 61 6e 73 61 63 74 69 of non-transacti
1c320 6f 6e 20 73 61 76 65 70 6f 69 6e 74 73 20 63 75 on savepoints cu
1c321 72 72 65 6e 74 6c 79 20 69 6e 20 74 68 65 20 0a rrently in the .
1c322 2a 2a 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 73 ** linked list s
1c323 74 61 72 74 69 6e 67 20 61 74 20 73 71 6c 69 74 tarting at sqlit
1c324 65 33 2e 70 53 61 76 65 70 6f 69 6e 74 2e 0a 2a e3.pSavepoint..*
1c325 2a 20 0a 2a 2a 20 55 73 61 67 65 3a 0a 2a 2a 0a * .** Usage:.**.
1c326 2a 2a 20 20 20 20 20 61 73 73 65 72 74 28 20 63 ** assert( c
1c327 68 65 63 6b 53 61 76 65 70 6f 69 6e 74 43 6f 75 heckSavepointCou
1c328 6e 74 28 64 62 29 20 29 3b 0a 2a 2f 0a 73 74 61 nt(db) );.*/.sta
1c329 74 69 63 20 69 6e 74 20 63 68 65 63 6b 53 61 76 tic int checkSav
1c32a 65 70 6f 69 6e 74 43 6f 75 6e 74 28 73 71 6c 69 epointCount(sqli
1c32b 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74 20 te3 *db){. int
1c32c 6e 20 3d 20 30 3b 0a 20 20 53 61 76 65 70 6f 69 n = 0;. Savepoi
1c32d 6e 74 20 2a 70 3b 0a 20 20 66 6f 72 28 70 3d 64 nt *p;. for(p=d
1c32e 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 3b 20 70 b->pSavepoint; p
1c32f 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 20 6e 2b ; p=p->pNext) n+
1c330 2b 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 3d 3d +;. assert( n==
1c331 28 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 (db->nSavepoint
1c332 2b 20 64 62 2d 3e 69 73 54 72 61 6e 73 61 63 74 + db->isTransact
1c333 69 6f 6e 53 61 76 65 70 6f 69 6e 74 29 20 29 3b ionSavepoint) );
1c334 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 . return 1;.}.#
1c335 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 45 78 65 endif../*.** Exe
1c336 63 75 74 65 20 61 73 20 6d 75 63 68 20 6f 66 20 cute as much of
1c337 61 20 56 44 42 45 20 70 72 6f 67 72 61 6d 20 61 a VDBE program a
1c338 73 20 77 65 20 63 61 6e 20 74 68 65 6e 20 72 65 s we can then re
1c339 74 75 72 6e 2e 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 turn..**.** sqli
1c33a 74 65 33 56 64 62 65 4d 61 6b 65 52 65 61 64 79 te3VdbeMakeReady
1c33b 28 29 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 () must be calle
1c33c 64 20 62 65 66 6f 72 65 20 74 68 69 73 20 72 6f d before this ro
1c33d 75 74 69 6e 65 20 69 6e 20 6f 72 64 65 72 20 74 utine in order t
1c33e 6f 0a 2a 2a 20 63 6c 6f 73 65 20 74 68 65 20 70 o.** close the p
1c33f 72 6f 67 72 61 6d 20 77 69 74 68 20 61 20 66 69 rogram with a fi
1c340 6e 61 6c 20 4f 50 5f 48 61 6c 74 20 61 6e 64 20 nal OP_Halt and
1c341 74 6f 20 73 65 74 20 75 70 20 74 68 65 20 63 61 to set up the ca
1c342 6c 6c 62 61 63 6b 73 0a 2a 2a 20 61 6e 64 20 74 llbacks.** and t
1c343 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 he error message
1c344 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 pointer..**.**
1c345 57 68 65 6e 65 76 65 72 20 61 20 72 6f 77 20 6f Whenever a row o
1c346 72 20 72 65 73 75 6c 74 20 64 61 74 61 20 69 73 r result data is
1c347 20 61 76 61 69 6c 61 62 6c 65 2c 20 74 68 69 73 available, this
1c348 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 65 69 routine will ei
1c349 74 68 65 72 0a 2a 2a 20 69 6e 76 6f 6b 65 20 74 ther.** invoke t
1c34a 68 65 20 72 65 73 75 6c 74 20 63 61 6c 6c 62 61 he result callba
1c34b 63 6b 20 28 69 66 20 74 68 65 72 65 20 69 73 20 ck (if there is
1c34c 6f 6e 65 29 20 6f 72 20 72 65 74 75 72 6e 20 77 one) or return w
1c34d 69 74 68 0a 2a 2a 20 53 51 4c 49 54 45 5f 52 4f ith.** SQLITE_RO
1c34e 57 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 61 W..**.** If an a
1c34f 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65 20 74 ttempt is made t
1c350 6f 20 6f 70 65 6e 20 61 20 6c 6f 63 6b 65 64 20 o open a locked
1c351 64 61 74 61 62 61 73 65 2c 20 74 68 65 6e 20 74 database, then t
1c352 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 77 his routine.** w
1c353 69 6c 6c 20 65 69 74 68 65 72 20 69 6e 76 6f 6b ill either invok
1c354 65 20 74 68 65 20 62 75 73 79 20 63 61 6c 6c 62 e the busy callb
1c355 61 63 6b 20 28 69 66 20 74 68 65 72 65 20 69 73 ack (if there is
1c356 20 6f 6e 65 29 20 6f 72 20 69 74 20 77 69 6c 6c one) or it will
1c357 0a 2a 2a 20 72 65 74 75 72 6e 20 53 51 4c 49 54 .** return SQLIT
1c358 45 5f 42 55 53 59 2e 0a 2a 2a 0a 2a 2a 20 49 66 E_BUSY..**.** If
1c359 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 an error occurs
1c35a 2c 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 , an error messa
1c35b 67 65 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f ge is written to
1c35c 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 memory obtained
1c35d 0a 2a 2a 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 .** from sqlite3
1c35e 5f 6d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 70 2d _malloc() and p-
1c35f 3e 7a 45 72 72 4d 73 67 20 69 73 20 6d 61 64 65 >zErrMsg is made
1c360 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 61 to point to tha
1c361 74 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 20 54 68 65 t memory..** The
1c362 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 73 error code is s
1c363 74 6f 72 65 64 20 69 6e 20 70 2d 3e 72 63 20 61 tored in p->rc a
1c364 6e 64 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 nd this routine
1c365 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 45 returns SQLITE_E
1c366 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 RROR..**.** If t
1c367 68 65 20 63 61 6c 6c 62 61 63 6b 20 65 76 65 72 he callback ever
1c368 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 returns non-zer
1c369 6f 2c 20 74 68 65 6e 20 74 68 65 20 70 72 6f 67 o, then the prog
1c36a 72 61 6d 20 65 78 69 74 73 0a 2a 2a 20 69 6d 6d ram exits.** imm
1c36b 65 64 69 61 74 65 6c 79 2e 20 20 54 68 65 72 65 ediately. There
1c36c 20 77 69 6c 6c 20 62 65 20 6e 6f 20 65 72 72 6f will be no erro
1c36d 72 20 6d 65 73 73 61 67 65 20 62 75 74 20 74 68 r message but th
1c36e 65 20 70 2d 3e 72 63 20 66 69 65 6c 64 20 69 73 e p->rc field is
1c36f 0a 2a 2a 20 73 65 74 20 74 6f 20 53 51 4c 49 54 .** set to SQLIT
1c370 45 5f 41 42 4f 52 54 20 61 6e 64 20 74 68 69 73 E_ABORT and this
1c371 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 72 65 routine will re
1c372 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f turn SQLITE_ERRO
1c373 52 2e 0a 2a 2a 0a 2a 2a 20 41 20 6d 65 6d 6f 72 R..**.** A memor
1c374 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 y allocation err
1c375 6f 72 20 63 61 75 73 65 73 20 70 2d 3e 72 63 20 or causes p->rc
1c376 74 6f 20 62 65 20 73 65 74 20 74 6f 20 53 51 4c to be set to SQL
1c377 49 54 45 5f 4e 4f 4d 45 4d 20 61 6e 64 20 74 68 ITE_NOMEM and th
1c378 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 74 6f is.** routine to
1c379 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 return SQLITE_E
1c37a 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 RROR..**.** Othe
1c37b 72 20 66 61 74 61 6c 20 65 72 72 6f 72 73 20 72 r fatal errors r
1c37c 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 eturn SQLITE_ERR
1c37d 4f 52 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 OR..**.** After
1c37e 74 68 69 73 20 72 6f 75 74 69 6e 65 20 68 61 73 this routine has
1c37f 20 66 69 6e 69 73 68 65 64 2c 20 73 71 6c 69 74 finished, sqlit
1c380 65 33 56 64 62 65 46 69 6e 61 6c 69 7a 65 28 29 e3VdbeFinalize()
1c381 20 73 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 75 73 should be.** us
1c382 65 64 20 74 6f 20 63 6c 65 61 6e 20 75 70 20 74 ed to clean up t
1c383 68 65 20 6d 65 73 73 20 74 68 61 74 20 77 61 73 he mess that was
1c384 20 6c 65 66 74 20 62 65 68 69 6e 64 2e 0a 2a 2f left behind..*/
1c385 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1c386 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 45 int sqlite3VdbeE
1c387 78 65 63 28 0a 20 20 56 64 62 65 20 2a 70 20 20 xec(. Vdbe *p
1c388 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c389 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20 2a 2f /* The VDBE */
1c38a 0a 29 7b 0a 20 20 69 6e 74 20 70 63 3b 20 20 20 .){. int pc;
1c38b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c38c 20 2f 2a 20 54 68 65 20 70 72 6f 67 72 61 6d 20 /* The program
1c38d 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 4f 70 20 counter */. Op
1c38e 2a 70 4f 70 3b 20 20 20 20 20 20 20 20 20 20 20 *pOp;
1c38f 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 /* Curre
1c390 6e 74 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a nt operation */.
1c391 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
1c392 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 2f 2a 20 E_OK; /*
1c393 56 61 6c 75 65 20 74 6f 20 72 65 74 75 72 6e 20 Value to return
1c394 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 */. sqlite3 *db
1c395 20 3d 20 70 2d 3e 64 62 3b 20 20 20 20 20 20 20 = p->db;
1c396 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 /* The database
1c397 2a 2f 0a 20 20 75 38 20 65 6e 63 6f 64 69 6e 67 */. u8 encoding
1c398 20 3d 20 45 4e 43 28 64 62 29 3b 20 20 20 20 20 = ENC(db);
1c399 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 /* The database
1c39a 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20 20 4d 65 encoding */. Me
1c39b 6d 20 2a 70 49 6e 31 20 3d 20 30 3b 20 20 20 20 m *pIn1 = 0;
1c39c 20 20 20 20 20 20 20 20 20 2f 2a 20 31 73 74 20 /* 1st
1c39d 69 6e 70 75 74 20 6f 70 65 72 61 6e 64 20 2a 2f input operand */
1c39e 0a 20 20 4d 65 6d 20 2a 70 49 6e 32 20 3d 20 30 . Mem *pIn2 = 0
1c39f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
1c3a0 20 32 6e 64 20 69 6e 70 75 74 20 6f 70 65 72 61 2nd input opera
1c3a1 6e 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 49 6e nd */. Mem *pIn
1c3a2 33 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 3 = 0;
1c3a3 20 20 20 2f 2a 20 33 72 64 20 69 6e 70 75 74 20 /* 3rd input
1c3a4 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 4d 65 6d operand */. Mem
1c3a5 20 2a 70 4f 75 74 20 3d 20 30 3b 20 20 20 20 20 *pOut = 0;
1c3a6 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 74 70 75 /* Outpu
1c3a7 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 75 t operand */. u
1c3a8 38 20 6f 70 50 72 6f 70 65 72 74 79 3b 0a 20 20 8 opProperty;.
1c3a9 69 6e 74 20 69 43 6f 6d 70 61 72 65 20 3d 20 30 int iCompare = 0
1c3aa 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 ; /* Re
1c3ab 73 75 6c 74 20 6f 66 20 6c 61 73 74 20 4f 50 5f sult of last OP_
1c3ac 43 6f 6d 70 61 72 65 20 6f 70 65 72 61 74 69 6f Compare operatio
1c3ad 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 50 65 72 n */. int *aPer
1c3ae 6d 75 74 65 20 3d 20 30 3b 20 20 20 20 20 20 20 mute = 0;
1c3af 20 20 2f 2a 20 50 65 72 6d 75 74 61 74 69 6f 6e /* Permutation
1c3b0 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 66 6f 72 20 of columns for
1c3b1 4f 50 5f 43 6f 6d 70 61 72 65 20 2a 2f 0a 23 69 OP_Compare */.#i
1c3b2 66 64 65 66 20 56 44 42 45 5f 50 52 4f 46 49 4c fdef VDBE_PROFIL
1c3b3 45 0a 20 20 75 36 34 20 73 74 61 72 74 3b 20 20 E. u64 start;
1c3b4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1c3b5 2a 20 43 50 55 20 63 6c 6f 63 6b 20 63 6f 75 6e * CPU clock coun
1c3b6 74 20 61 74 20 73 74 61 72 74 20 6f 66 20 6f 70 t at start of op
1c3b7 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 6f 72 code */. int or
1c3b8 69 67 50 63 3b 20 20 20 20 20 20 20 20 20 20 20 igPc;
1c3b9 20 20 20 20 20 2f 2a 20 50 72 6f 67 72 61 6d 20 /* Program
1c3ba 63 6f 75 6e 74 65 72 20 61 74 20 73 74 61 72 74 counter at start
1c3bb 20 6f 66 20 6f 70 63 6f 64 65 20 2a 2f 0a 23 65 of opcode */.#e
1c3bc 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c ndif.#ifndef SQL
1c3bd 49 54 45 5f 4f 4d 49 54 5f 50 52 4f 47 52 45 53 ITE_OMIT_PROGRES
1c3be 53 5f 43 41 4c 4c 42 41 43 4b 0a 20 20 69 6e 74 S_CALLBACK. int
1c3bf 20 6e 50 72 6f 67 72 65 73 73 4f 70 73 20 3d 20 nProgressOps =
1c3c0 30 3b 20 20 20 20 20 20 2f 2a 20 4f 70 63 6f 64 0; /* Opcod
1c3c1 65 73 20 65 78 65 63 75 74 65 64 20 73 69 6e 63 es executed sinc
1c3c2 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 e progress callb
1c3c3 61 63 6b 2e 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 ack. */.#endif.
1c3c4 20 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /**************
1c3c5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c3c6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c3c7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c3c8 2a 2a 2a 2a 2a 2a 0a 20 20 2a 2a 20 41 75 74 6f ******. ** Auto
1c3c9 6d 61 74 69 63 61 6c 6c 79 20 67 65 6e 65 72 61 matically genera
1c3ca 74 65 64 20 63 6f 64 65 0a 20 20 2a 2a 0a 20 20 ted code. **.
1c3cb 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 ** The following
1c3cc 20 75 6e 69 6f 6e 20 69 73 20 61 75 74 6f 6d 61 union is automa
1c3cd 74 69 63 61 6c 6c 79 20 67 65 6e 65 72 61 74 65 tically generate
1c3ce 64 20 62 79 20 74 68 65 0a 20 20 2a 2a 20 76 64 d by the. ** vd
1c3cf 62 65 2d 63 6f 6d 70 72 65 73 73 2e 74 63 6c 20 be-compress.tcl
1c3d0 73 63 72 69 70 74 2e 20 20 54 68 65 20 70 75 72 script. The pur
1c3d1 70 6f 73 65 20 6f 66 20 74 68 69 73 20 75 6e 69 pose of this uni
1c3d2 6f 6e 20 69 73 20 74 6f 0a 20 20 2a 2a 20 72 65 on is to. ** re
1c3d3 64 75 63 65 20 74 68 65 20 61 6d 6f 75 6e 74 20 duce the amount
1c3d4 6f 66 20 73 74 61 63 6b 20 73 70 61 63 65 20 72 of stack space r
1c3d5 65 71 75 69 72 65 64 20 62 79 20 74 68 69 73 20 equired by this
1c3d6 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 2a 2a 20 53 function.. ** S
1c3d7 65 65 20 63 6f 6d 6d 65 6e 74 73 20 69 6e 20 74 ee comments in t
1c3d8 68 65 20 76 64 62 65 2d 63 6f 6d 70 72 65 73 73 he vdbe-compress
1c3d9 2e 74 63 6c 20 73 63 72 69 70 74 20 66 6f 72 20 .tcl script for
1c3da 64 65 74 61 69 6c 73 2e 0a 20 20 2a 2f 0a 20 20 details.. */.
1c3db 75 6e 69 6f 6e 20 76 64 62 65 45 78 65 63 55 6e union vdbeExecUn
1c3dc 69 6f 6e 20 7b 0a 20 20 20 20 73 74 72 75 63 74 ion {. struct
1c3dd 20 4f 50 5f 59 69 65 6c 64 5f 73 74 61 63 6b 5f OP_Yield_stack_
1c3de 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 vars {. int
1c3df 20 70 63 44 65 73 74 3b 0a 20 20 20 20 7d 20 61 pcDest;. } a
1c3e0 61 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 a;. struct OP
1c3e1 5f 56 61 72 69 61 62 6c 65 5f 73 74 61 63 6b 5f _Variable_stack_
1c3e2 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 vars {. int
1c3e3 20 70 31 3b 20 20 20 20 20 20 20 20 20 20 2f 2a p1; /*
1c3e4 20 56 61 72 69 61 62 6c 65 20 74 6f 20 63 6f 70 Variable to cop
1c3e5 79 20 66 72 6f 6d 20 2a 2f 0a 20 20 20 20 20 20 y from */.
1c3e6 69 6e 74 20 70 32 3b 20 20 20 20 20 20 20 20 20 int p2;
1c3e7 20 2f 2a 20 52 65 67 69 73 74 65 72 20 74 6f 20 /* Register to
1c3e8 63 6f 70 79 20 74 6f 20 2a 2f 0a 20 20 20 20 20 copy to */.
1c3e9 20 69 6e 74 20 6e 3b 20 20 20 20 20 20 20 20 20 int n;
1c3ea 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 76 /* Number of v
1c3eb 61 6c 75 65 73 20 6c 65 66 74 20 74 6f 20 63 6f alues left to co
1c3ec 70 79 20 2a 2f 0a 20 20 20 20 20 20 4d 65 6d 20 py */. Mem
1c3ed 2a 70 56 61 72 3b 20 20 20 20 20 20 20 2f 2a 20 *pVar; /*
1c3ee 56 61 6c 75 65 20 62 65 69 6e 67 20 74 72 61 6e Value being tran
1c3ef 73 66 65 72 72 65 64 20 2a 2f 0a 20 20 20 20 7d sferred */. }
1c3f0 20 61 62 3b 0a 20 20 20 20 73 74 72 75 63 74 20 ab;. struct
1c3f1 4f 50 5f 4d 6f 76 65 5f 73 74 61 63 6b 5f 76 61 OP_Move_stack_va
1c3f2 72 73 20 7b 0a 20 20 20 20 20 20 63 68 61 72 20 rs {. char
1c3f3 2a 7a 4d 61 6c 6c 6f 63 3b 20 20 20 2f 2a 20 48 *zMalloc; /* H
1c3f4 6f 6c 64 69 6e 67 20 76 61 72 69 61 62 6c 65 20 olding variable
1c3f5 66 6f 72 20 61 6c 6c 6f 63 61 74 65 64 20 6d 65 for allocated me
1c3f6 6d 6f 72 79 20 2a 2f 0a 20 20 20 20 20 20 69 6e mory */. in
1c3f7 74 20 6e 3b 20 20 20 20 20 20 20 20 20 20 20 2f t n; /
1c3f8 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 67 69 * Number of regi
1c3f9 73 74 65 72 73 20 6c 65 66 74 20 74 6f 20 63 6f sters left to co
1c3fa 70 79 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 py */. int
1c3fb 70 31 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 p1; /*
1c3fc 52 65 67 69 73 74 65 72 20 74 6f 20 63 6f 70 79 Register to copy
1c3fd 20 66 72 6f 6d 20 2a 2f 0a 20 20 20 20 20 20 69 from */. i
1c3fe 6e 74 20 70 32 3b 20 20 20 20 20 20 20 20 20 20 nt p2;
1c3ff 2f 2a 20 52 65 67 69 73 74 65 72 20 74 6f 20 63 /* Register to c
1c400 6f 70 79 20 74 6f 20 2a 2f 0a 20 20 20 20 7d 20 opy to */. }
1c401 61 63 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f ac;. struct O
1c402 50 5f 52 65 73 75 6c 74 52 6f 77 5f 73 74 61 63 P_ResultRow_stac
1c403 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 4d k_vars {. M
1c404 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 20 20 20 20 em *pMem;.
1c405 69 6e 74 20 69 3b 0a 20 20 20 20 7d 20 61 64 3b int i;. } ad;
1c406 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 43 . struct OP_C
1c407 6f 6e 63 61 74 5f 73 74 61 63 6b 5f 76 61 72 73 oncat_stack_vars
1c408 20 7b 0a 20 20 20 20 20 20 69 36 34 20 6e 42 79 {. i64 nBy
1c409 74 65 3b 0a 20 20 20 20 7d 20 61 65 3b 0a 20 20 te;. } ae;.
1c40a 20 20 73 74 72 75 63 74 20 4f 50 5f 52 65 6d 61 struct OP_Rema
1c40b 69 6e 64 65 72 5f 73 74 61 63 6b 5f 76 61 72 73 inder_stack_vars
1c40c 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 66 6c 61 {. int fla
1c40d 67 73 3b 20 20 20 20 20 20 2f 2a 20 43 6f 6d 62 gs; /* Comb
1c40e 69 6e 65 64 20 4d 45 4d 5f 2a 20 66 6c 61 67 73 ined MEM_* flags
1c40f 20 66 72 6f 6d 20 62 6f 74 68 20 69 6e 70 75 74 from both input
1c410 73 20 2a 2f 0a 20 20 20 20 20 20 69 36 34 20 69 s */. i64 i
1c411 41 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e A; /* In
1c412 74 65 67 65 72 20 76 61 6c 75 65 20 6f 66 20 6c teger value of l
1c413 65 66 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 eft operand */.
1c414 20 20 20 20 20 69 36 34 20 69 42 3b 20 20 20 20 i64 iB;
1c415 20 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 /* Integer
1c416 76 61 6c 75 65 20 6f 66 20 72 69 67 68 74 20 6f value of right o
1c417 70 65 72 61 6e 64 20 2a 2f 0a 20 20 20 20 20 20 perand */.
1c418 64 6f 75 62 6c 65 20 72 41 3b 20 20 20 20 20 20 double rA;
1c419 2f 2a 20 52 65 61 6c 20 76 61 6c 75 65 20 6f 66 /* Real value of
1c41a 20 6c 65 66 74 20 6f 70 65 72 61 6e 64 20 2a 2f left operand */
1c41b 0a 20 20 20 20 20 20 64 6f 75 62 6c 65 20 72 42 . double rB
1c41c 3b 20 20 20 20 20 20 2f 2a 20 52 65 61 6c 20 76 ; /* Real v
1c41d 61 6c 75 65 20 6f 66 20 72 69 67 68 74 20 6f 70 alue of right op
1c41e 65 72 61 6e 64 20 2a 2f 0a 20 20 20 20 7d 20 61 erand */. } a
1c41f 66 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 f;. struct OP
1c420 5f 46 75 6e 63 74 69 6f 6e 5f 73 74 61 63 6b 5f _Function_stack_
1c421 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 vars {. int
1c422 20 69 3b 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 i;. Mem *p
1c423 41 72 67 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 Arg;. sqlit
1c424 65 33 5f 63 6f 6e 74 65 78 74 20 63 74 78 3b 0a e3_context ctx;.
1c425 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 61 sqlite3_va
1c426 6c 75 65 20 2a 2a 61 70 56 61 6c 3b 0a 20 20 20 lue **apVal;.
1c427 20 20 20 69 6e 74 20 6e 3b 0a 20 20 20 20 7d 20 int n;. }
1c428 61 67 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f ag;. struct O
1c429 50 5f 53 68 69 66 74 52 69 67 68 74 5f 73 74 61 P_ShiftRight_sta
1c42a 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 ck_vars {.
1c42b 69 36 34 20 61 3b 0a 20 20 20 20 20 20 69 36 34 i64 a;. i64
1c42c 20 62 3b 0a 20 20 20 20 7d 20 61 68 3b 0a 20 20 b;. } ah;.
1c42d 20 20 73 74 72 75 63 74 20 4f 50 5f 47 65 5f 73 struct OP_Ge_s
1c42e 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 tack_vars {.
1c42f 20 20 69 6e 74 20 72 65 73 3b 20 20 20 20 20 20 int res;
1c430 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 /* Result
1c431 6f 66 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f of the compariso
1c432 6e 20 6f 66 20 70 49 6e 31 20 61 67 61 69 6e 73 n of pIn1 agains
1c433 74 20 70 49 6e 33 20 2a 2f 0a 20 20 20 20 20 20 t pIn3 */.
1c434 63 68 61 72 20 61 66 66 69 6e 69 74 79 3b 20 20 char affinity;
1c435 20 20 20 20 2f 2a 20 41 66 66 69 6e 69 74 79 20 /* Affinity
1c436 74 6f 20 75 73 65 20 66 6f 72 20 63 6f 6d 70 61 to use for compa
1c437 72 69 73 6f 6e 20 2a 2f 0a 20 20 20 20 7d 20 61 rison */. } a
1c438 69 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 i;. struct OP
1c439 5f 43 6f 6d 70 61 72 65 5f 73 74 61 63 6b 5f 76 _Compare_stack_v
1c43a 61 72 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 ars {. int
1c43b 6e 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a n;. int i;.
1c43c 20 20 20 20 20 20 69 6e 74 20 70 31 3b 0a 20 20 int p1;.
1c43d 20 20 20 20 69 6e 74 20 70 32 3b 0a 20 20 20 20 int p2;.
1c43e 20 20 63 6f 6e 73 74 20 4b 65 79 49 6e 66 6f 20 const KeyInfo
1c43f 2a 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 20 *pKeyInfo;.
1c440 20 69 6e 74 20 69 64 78 3b 0a 20 20 20 20 20 20 int idx;.
1c441 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 20 CollSeq *pColl;
1c442 20 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67 20 /* Collating
1c443 73 65 71 75 65 6e 63 65 20 74 6f 20 75 73 65 20 sequence to use
1c444 6f 6e 20 74 68 69 73 20 74 65 72 6d 20 2a 2f 0a on this term */.
1c445 20 20 20 20 20 20 69 6e 74 20 62 52 65 76 3b 20 int bRev;
1c446 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
1c447 20 66 6f 72 20 44 45 53 43 45 4e 44 49 4e 47 20 for DESCENDING
1c448 73 6f 72 74 20 6f 72 64 65 72 20 2a 2f 0a 20 20 sort order */.
1c449 20 20 7d 20 61 6a 3b 0a 20 20 20 20 73 74 72 75 } aj;. stru
1c44a 63 74 20 4f 50 5f 4f 72 5f 73 74 61 63 6b 5f 76 ct OP_Or_stack_v
1c44b 61 72 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 ars {. int
1c44c 76 31 3b 20 20 20 20 2f 2a 20 4c 65 66 74 20 6f v1; /* Left o
1c44d 70 65 72 61 6e 64 3a 20 20 30 3d 3d 46 41 4c 53 perand: 0==FALS
1c44e 45 2c 20 31 3d 3d 54 52 55 45 2c 20 32 3d 3d 55 E, 1==TRUE, 2==U
1c44f 4e 4b 4e 4f 57 4e 20 6f 72 20 4e 55 4c 4c 20 2a NKNOWN or NULL *
1c450 2f 0a 20 20 20 20 20 20 69 6e 74 20 76 32 3b 20 /. int v2;
1c451 20 20 20 2f 2a 20 52 69 67 68 74 20 6f 70 65 72 /* Right oper
1c452 61 6e 64 3a 20 30 3d 3d 46 41 4c 53 45 2c 20 31 and: 0==FALSE, 1
1c453 3d 3d 54 52 55 45 2c 20 32 3d 3d 55 4e 4b 4e 4f ==TRUE, 2==UNKNO
1c454 57 4e 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 WN or NULL */.
1c455 20 20 7d 20 61 6b 3b 0a 20 20 20 20 73 74 72 75 } ak;. stru
1c456 63 74 20 4f 50 5f 49 66 4e 6f 74 5f 73 74 61 63 ct OP_IfNot_stac
1c457 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69 k_vars {. i
1c458 6e 74 20 63 3b 0a 20 20 20 20 7d 20 61 6c 3b 0a nt c;. } al;.
1c459 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 43 6f struct OP_Co
1c45a 6c 75 6d 6e 5f 73 74 61 63 6b 5f 76 61 72 73 20 lumn_stack_vars
1c45b 7b 0a 20 20 20 20 20 20 75 33 32 20 70 61 79 6c {. u32 payl
1c45c 6f 61 64 53 69 7a 65 3b 20 20 20 2f 2a 20 4e 75 oadSize; /* Nu
1c45d 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e mber of bytes in
1c45e 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 the record */.
1c45f 20 20 20 20 20 69 36 34 20 70 61 79 6c 6f 61 64 i64 payload
1c460 53 69 7a 65 36 34 3b 20 2f 2a 20 4e 75 6d 62 65 Size64; /* Numbe
1c461 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 r of bytes in th
1c462 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20 e record */.
1c463 20 20 69 6e 74 20 70 31 3b 20 20 20 20 20 20 20 int p1;
1c464 20 20 20 20 20 2f 2a 20 50 31 20 76 61 6c 75 65 /* P1 value
1c465 20 6f 66 20 74 68 65 20 6f 70 63 6f 64 65 20 2a of the opcode *
1c466 2f 0a 20 20 20 20 20 20 69 6e 74 20 70 32 3b 20 /. int p2;
1c467 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 63 6f /* co
1c468 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 74 6f 20 72 lumn number to r
1c469 65 74 72 69 65 76 65 20 2a 2f 0a 20 20 20 20 20 etrieve */.
1c46a 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b VdbeCursor *pC;
1c46b 20 20 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20 /* The VDBE
1c46c 63 75 72 73 6f 72 20 2a 2f 0a 20 20 20 20 20 20 cursor */.
1c46d 63 68 61 72 20 2a 7a 52 65 63 3b 20 20 20 20 20 char *zRec;
1c46e 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f /* Pointer to
1c46f 20 63 6f 6d 70 6c 65 74 65 20 72 65 63 6f 72 64 complete record
1c470 2d 64 61 74 61 20 2a 2f 0a 20 20 20 20 20 20 42 -data */. B
1c471 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 20 tCursor *pCrsr;
1c472 20 20 2f 2a 20 54 68 65 20 42 54 72 65 65 20 63 /* The BTree c
1c473 75 72 73 6f 72 20 2a 2f 0a 20 20 20 20 20 20 75 ursor */. u
1c474 33 32 20 2a 61 54 79 70 65 3b 20 20 20 20 20 20 32 *aType;
1c475 20 20 2f 2a 20 61 54 79 70 65 5b 69 5d 20 68 6f /* aType[i] ho
1c476 6c 64 73 20 74 68 65 20 6e 75 6d 65 72 69 63 20 lds the numeric
1c477 74 79 70 65 20 6f 66 20 74 68 65 20 69 2d 74 68 type of the i-th
1c478 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 20 20 20 column */.
1c479 20 75 33 32 20 2a 61 4f 66 66 73 65 74 3b 20 20 u32 *aOffset;
1c47a 20 20 20 20 2f 2a 20 61 4f 66 66 73 65 74 5b 69 /* aOffset[i
1c47b 5d 20 69 73 20 6f 66 66 73 65 74 20 74 6f 20 73 ] is offset to s
1c47c 74 61 72 74 20 6f 66 20 64 61 74 61 20 66 6f 72 tart of data for
1c47d 20 69 2d 74 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a i-th column */.
1c47e 20 20 20 20 20 20 69 6e 74 20 6e 46 69 65 6c 64 int nField
1c47f 3b 20 20 20 20 20 20 20 20 2f 2a 20 6e 75 6d 62 ; /* numb
1c480 65 72 20 6f 66 20 66 69 65 6c 64 73 20 69 6e 20 er of fields in
1c481 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 the record */.
1c482 20 20 20 20 69 6e 74 20 6c 65 6e 3b 20 20 20 20 int len;
1c483 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 65 /* The le
1c484 6e 67 74 68 20 6f 66 20 74 68 65 20 73 65 72 69 ngth of the seri
1c485 61 6c 69 7a 65 64 20 64 61 74 61 20 66 6f 72 20 alized data for
1c486 74 68 65 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 the column */.
1c487 20 20 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 int i;
1c488 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 /* Loop c
1c489 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 20 20 20 20 ounter */.
1c48a 63 68 61 72 20 2a 7a 44 61 74 61 3b 20 20 20 20 char *zData;
1c48b 20 20 20 2f 2a 20 50 61 72 74 20 6f 66 20 74 68 /* Part of th
1c48c 65 20 72 65 63 6f 72 64 20 62 65 69 6e 67 20 64 e record being d
1c48d 65 63 6f 64 65 64 20 2a 2f 0a 20 20 20 20 20 20 ecoded */.
1c48e 4d 65 6d 20 2a 70 44 65 73 74 3b 20 20 20 20 20 Mem *pDest;
1c48f 20 20 20 2f 2a 20 57 68 65 72 65 20 74 6f 20 77 /* Where to w
1c490 72 69 74 65 20 74 68 65 20 65 78 74 72 61 63 74 rite the extract
1c491 65 64 20 76 61 6c 75 65 20 2a 2f 0a 20 20 20 20 ed value */.
1c492 20 20 4d 65 6d 20 73 4d 65 6d 3b 20 20 20 20 20 Mem sMem;
1c493 20 20 20 20 20 2f 2a 20 46 6f 72 20 73 74 6f 72 /* For stor
1c494 69 6e 67 20 74 68 65 20 72 65 63 6f 72 64 20 62 ing the record b
1c495 65 69 6e 67 20 64 65 63 6f 64 65 64 20 2a 2f 0a eing decoded */.
1c496 20 20 20 20 20 20 75 38 20 2a 7a 49 64 78 3b 20 u8 *zIdx;
1c497 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 /* Inde
1c498 78 20 69 6e 74 6f 20 68 65 61 64 65 72 20 2a 2f x into header */
1c499 0a 20 20 20 20 20 20 75 38 20 2a 7a 45 6e 64 48 . u8 *zEndH
1c49a 64 72 3b 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 dr; /* Poi
1c49b 6e 74 65 72 20 74 6f 20 66 69 72 73 74 20 62 79 nter to first by
1c49c 74 65 20 61 66 74 65 72 20 74 68 65 20 68 65 61 te after the hea
1c49d 64 65 72 20 2a 2f 0a 20 20 20 20 20 20 75 33 32 der */. u32
1c49e 20 6f 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 offset;
1c49f 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74 6f 20 74 /* Offset into t
1c4a0 68 65 20 64 61 74 61 20 2a 2f 0a 20 20 20 20 20 he data */.
1c4a1 20 75 36 34 20 6f 66 66 73 65 74 36 34 3b 20 20 u64 offset64;
1c4a2 20 20 20 20 2f 2a 20 36 34 2d 62 69 74 20 6f 66 /* 64-bit of
1c4a3 66 73 65 74 2e 20 20 36 34 20 62 69 74 73 20 6e fset. 64 bits n
1c4a4 65 65 64 65 64 20 74 6f 20 63 61 74 63 68 20 6f eeded to catch o
1c4a5 76 65 72 66 6c 6f 77 20 2a 2f 0a 20 20 20 20 20 verflow */.
1c4a6 20 69 6e 74 20 73 7a 48 64 72 3b 20 20 20 20 20 int szHdr;
1c4a7 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 /* Size of t
1c4a8 68 65 20 68 65 61 64 65 72 20 73 69 7a 65 20 66 he header size f
1c4a9 69 65 6c 64 20 61 74 20 73 74 61 72 74 20 6f 66 ield at start of
1c4aa 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20 20 record */.
1c4ab 20 69 6e 74 20 61 76 61 69 6c 3b 20 20 20 20 20 int avail;
1c4ac 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
1c4ad 20 62 79 74 65 73 20 6f 66 20 61 76 61 69 6c 61 bytes of availa
1c4ae 62 6c 65 20 64 61 74 61 20 2a 2f 0a 20 20 20 20 ble data */.
1c4af 20 20 4d 65 6d 20 2a 70 52 65 67 3b 20 20 20 20 Mem *pReg;
1c4b0 20 20 20 20 20 2f 2a 20 50 73 65 75 64 6f 54 61 /* PseudoTa
1c4b1 62 6c 65 20 69 6e 70 75 74 20 72 65 67 69 73 74 ble input regist
1c4b2 65 72 20 2a 2f 0a 20 20 20 20 7d 20 61 6d 3b 0a er */. } am;.
1c4b3 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 41 66 struct OP_Af
1c4b4 66 69 6e 69 74 79 5f 73 74 61 63 6b 5f 76 61 72 finity_stack_var
1c4b5 73 20 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a s {. char *
1c4b6 7a 41 66 66 69 6e 69 74 79 3b 20 20 20 2f 2a 20 zAffinity; /*
1c4b7 54 68 65 20 61 66 66 69 6e 69 74 79 20 74 6f 20 The affinity to
1c4b8 62 65 20 61 70 70 6c 69 65 64 20 2a 2f 0a 20 20 be applied */.
1c4b9 20 20 20 20 4d 65 6d 20 2a 70 44 61 74 61 30 3b Mem *pData0;
1c4ba 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 /* First
1c4bb 72 65 67 69 73 74 65 72 20 74 6f 20 77 68 69 63 register to whic
1c4bc 68 20 74 6f 20 61 70 70 6c 79 20 61 66 66 69 6e h to apply affin
1c4bd 69 74 79 20 2a 2f 0a 20 20 20 20 20 20 4d 65 6d ity */. Mem
1c4be 20 2a 70 4c 61 73 74 3b 20 20 20 20 20 20 20 20 *pLast;
1c4bf 2f 2a 20 4c 61 73 74 20 72 65 67 69 73 74 65 72 /* Last register
1c4c0 20 74 6f 20 77 68 69 63 68 20 74 6f 20 61 70 70 to which to app
1c4c1 6c 79 20 61 66 66 69 6e 69 74 79 20 2a 2f 0a 20 ly affinity */.
1c4c2 20 20 20 20 20 4d 65 6d 20 2a 70 52 65 63 3b 20 Mem *pRec;
1c4c3 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 /* Curre
1c4c4 6e 74 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 20 nt register */.
1c4c5 20 20 20 7d 20 61 6e 3b 0a 20 20 20 20 73 74 72 } an;. str
1c4c6 75 63 74 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 uct OP_MakeRecor
1c4c7 64 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 d_stack_vars {.
1c4c8 20 20 20 20 20 75 38 20 2a 7a 4e 65 77 52 65 63 u8 *zNewRec
1c4c9 6f 72 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 41 ord; /* A
1c4ca 20 62 75 66 66 65 72 20 74 6f 20 68 6f 6c 64 20 buffer to hold
1c4cb 74 68 65 20 64 61 74 61 20 66 6f 72 20 74 68 65 the data for the
1c4cc 20 6e 65 77 20 72 65 63 6f 72 64 20 2a 2f 0a 20 new record */.
1c4cd 20 20 20 20 20 4d 65 6d 20 2a 70 52 65 63 3b 20 Mem *pRec;
1c4ce 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
1c4cf 68 65 20 6e 65 77 20 72 65 63 6f 72 64 20 2a 2f he new record */
1c4d0 0a 20 20 20 20 20 20 75 36 34 20 6e 44 61 74 61 . u64 nData
1c4d1 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
1c4d2 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 Number of bytes
1c4d3 20 6f 66 20 64 61 74 61 20 73 70 61 63 65 20 2a of data space *
1c4d4 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 48 64 72 /. int nHdr
1c4d5 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
1c4d6 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 * Number of byte
1c4d7 73 20 6f 66 20 68 65 61 64 65 72 20 73 70 61 63 s of header spac
1c4d8 65 20 2a 2f 0a 20 20 20 20 20 20 69 36 34 20 6e e */. i64 n
1c4d9 42 79 74 65 3b 20 20 20 20 20 20 20 20 20 20 20 Byte;
1c4da 20 20 2f 2a 20 44 61 74 61 20 73 70 61 63 65 20 /* Data space
1c4db 72 65 71 75 69 72 65 64 20 66 6f 72 20 74 68 69 required for thi
1c4dc 73 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20 s record */.
1c4dd 20 20 69 6e 74 20 6e 5a 65 72 6f 3b 20 20 20 20 int nZero;
1c4de 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
1c4df 65 72 20 6f 66 20 7a 65 72 6f 20 62 79 74 65 73 er of zero bytes
1c4e0 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 at the end of t
1c4e1 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 20 he record */.
1c4e2 20 20 20 69 6e 74 20 6e 56 61 72 69 6e 74 3b 20 int nVarint;
1c4e3 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
1c4e4 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 ber of bytes in
1c4e5 61 20 76 61 72 69 6e 74 20 2a 2f 0a 20 20 20 20 a varint */.
1c4e6 20 20 75 33 32 20 73 65 72 69 61 6c 5f 74 79 70 u32 serial_typ
1c4e7 65 3b 20 20 20 20 20 20 20 2f 2a 20 54 79 70 65 e; /* Type
1c4e8 20 66 69 65 6c 64 20 2a 2f 0a 20 20 20 20 20 20 field */.
1c4e9 4d 65 6d 20 2a 70 44 61 74 61 30 3b 20 20 20 20 Mem *pData0;
1c4ea 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 /* First
1c4eb 66 69 65 6c 64 20 74 6f 20 62 65 20 63 6f 6d 62 field to be comb
1c4ec 69 6e 65 64 20 69 6e 74 6f 20 74 68 65 20 72 65 ined into the re
1c4ed 63 6f 72 64 20 2a 2f 0a 20 20 20 20 20 20 4d 65 cord */. Me
1c4ee 6d 20 2a 70 4c 61 73 74 3b 20 20 20 20 20 20 20 m *pLast;
1c4ef 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 66 69 65 /* Last fie
1c4f0 6c 64 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64 ld of the record
1c4f1 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 46 */. int nF
1c4f2 69 65 6c 64 3b 20 20 20 20 20 20 20 20 20 20 20 ield;
1c4f3 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 69 /* Number of fi
1c4f4 65 6c 64 73 20 69 6e 20 74 68 65 20 72 65 63 6f elds in the reco
1c4f5 72 64 20 2a 2f 0a 20 20 20 20 20 20 63 68 61 72 rd */. char
1c4f6 20 2a 7a 41 66 66 69 6e 69 74 79 3b 20 20 20 20 *zAffinity;
1c4f7 20 20 20 2f 2a 20 54 68 65 20 61 66 66 69 6e 69 /* The affini
1c4f8 74 79 20 73 74 72 69 6e 67 20 66 6f 72 20 74 68 ty string for th
1c4f9 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20 e record */.
1c4fa 20 20 69 6e 74 20 66 69 6c 65 5f 66 6f 72 6d 61 int file_forma
1c4fb 74 3b 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 t; /* File
1c4fc 20 66 6f 72 6d 61 74 20 74 6f 20 75 73 65 20 66 format to use f
1c4fd 6f 72 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20 or encoding */.
1c4fe 20 20 20 20 20 69 6e 74 20 69 3b 20 20 20 20 20 int i;
1c4ff 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 /* S
1c500 70 61 63 65 20 75 73 65 64 20 69 6e 20 7a 4e 65 pace used in zNe
1c501 77 52 65 63 6f 72 64 5b 5d 20 2a 2f 0a 20 20 20 wRecord[] */.
1c502 20 20 20 69 6e 74 20 6c 65 6e 3b 20 20 20 20 20 int len;
1c503 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e /* Len
1c504 67 74 68 20 6f 66 20 61 20 66 69 65 6c 64 20 2a gth of a field *
1c505 2f 0a 20 20 20 20 7d 20 61 6f 3b 0a 20 20 20 20 /. } ao;.
1c506 73 74 72 75 63 74 20 4f 50 5f 43 6f 75 6e 74 5f struct OP_Count_
1c507 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 stack_vars {.
1c508 20 20 20 69 36 34 20 6e 45 6e 74 72 79 3b 0a 20 i64 nEntry;.
1c509 20 20 20 20 20 42 74 43 75 72 73 6f 72 20 2a 70 BtCursor *p
1c50a 43 72 73 72 3b 0a 20 20 20 20 7d 20 61 70 3b 0a Crsr;. } ap;.
1c50b 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 53 61 struct OP_Sa
1c50c 76 65 70 6f 69 6e 74 5f 73 74 61 63 6b 5f 76 61 vepoint_stack_va
1c50d 72 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 70 rs {. int p
1c50e 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 1;
1c50f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 /* Va
1c510 6c 75 65 20 6f 66 20 50 31 20 6f 70 65 72 61 6e lue of P1 operan
1c511 64 20 2a 2f 0a 20 20 20 20 20 20 63 68 61 72 20 d */. char
1c512 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 *zName;
1c513 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 /* Na
1c514 6d 65 20 6f 66 20 73 61 76 65 70 6f 69 6e 74 20 me of savepoint
1c515 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 4e 61 */. int nNa
1c516 6d 65 3b 0a 20 20 20 20 20 20 53 61 76 65 70 6f me;. Savepo
1c517 69 6e 74 20 2a 70 4e 65 77 3b 0a 20 20 20 20 20 int *pNew;.
1c518 20 53 61 76 65 70 6f 69 6e 74 20 2a 70 53 61 76 Savepoint *pSav
1c519 65 70 6f 69 6e 74 3b 0a 20 20 20 20 20 20 53 61 epoint;. Sa
1c51a 76 65 70 6f 69 6e 74 20 2a 70 54 6d 70 3b 0a 20 vepoint *pTmp;.
1c51b 20 20 20 20 20 69 6e 74 20 69 53 61 76 65 70 6f int iSavepo
1c51c 69 6e 74 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 int;. int i
1c51d 69 3b 0a 20 20 20 20 7d 20 61 71 3b 0a 20 20 20 i;. } aq;.
1c51e 20 73 74 72 75 63 74 20 4f 50 5f 41 75 74 6f 43 struct OP_AutoC
1c51f 6f 6d 6d 69 74 5f 73 74 61 63 6b 5f 76 61 72 73 ommit_stack_vars
1c520 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 64 65 73 {. int des
1c521 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 3b 0a iredAutoCommit;.
1c522 20 20 20 20 20 20 69 6e 74 20 69 52 6f 6c 6c 62 int iRollb
1c523 61 63 6b 3b 0a 20 20 20 20 20 20 69 6e 74 20 74 ack;. int t
1c524 75 72 6e 4f 6e 41 43 3b 0a 20 20 20 20 7d 20 61 urnOnAC;. } a
1c525 72 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 r;. struct OP
1c526 5f 54 72 61 6e 73 61 63 74 69 6f 6e 5f 73 74 61 _Transaction_sta
1c527 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 ck_vars {.
1c528 42 74 72 65 65 20 2a 70 42 74 3b 0a 20 20 20 20 Btree *pBt;.
1c529 7d 20 61 73 3b 0a 20 20 20 20 73 74 72 75 63 74 } as;. struct
1c52a 20 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65 5f 73 OP_ReadCookie_s
1c52b 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 tack_vars {.
1c52c 20 20 69 6e 74 20 69 4d 65 74 61 3b 0a 20 20 20 int iMeta;.
1c52d 20 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 20 20 int iDb;.
1c52e 20 20 69 6e 74 20 69 43 6f 6f 6b 69 65 3b 0a 20 int iCookie;.
1c52f 20 20 20 7d 20 61 74 3b 0a 20 20 20 20 73 74 72 } at;. str
1c530 75 63 74 20 4f 50 5f 53 65 74 43 6f 6f 6b 69 65 uct OP_SetCookie
1c531 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 _stack_vars {.
1c532 20 20 20 20 44 62 20 2a 70 44 62 3b 0a 20 20 20 Db *pDb;.
1c533 20 7d 20 61 75 3b 0a 20 20 20 20 73 74 72 75 63 } au;. struc
1c534 74 20 4f 50 5f 56 65 72 69 66 79 43 6f 6f 6b 69 t OP_VerifyCooki
1c535 65 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 e_stack_vars {.
1c536 20 20 20 20 20 69 6e 74 20 69 4d 65 74 61 3b 0a int iMeta;.
1c537 20 20 20 20 20 20 42 74 72 65 65 20 2a 70 42 74 Btree *pBt
1c538 3b 0a 20 20 20 20 7d 20 61 76 3b 0a 20 20 20 20 ;. } av;.
1c539 73 74 72 75 63 74 20 4f 50 5f 4f 70 65 6e 57 72 struct OP_OpenWr
1c53a 69 74 65 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b ite_stack_vars {
1c53b 0a 20 20 20 20 20 20 69 6e 74 20 6e 46 69 65 6c . int nFiel
1c53c 64 3b 0a 20 20 20 20 20 20 4b 65 79 49 6e 66 6f d;. KeyInfo
1c53d 20 2a 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 *pKeyInfo;.
1c53e 20 20 69 6e 74 20 70 32 3b 0a 20 20 20 20 20 20 int p2;.
1c53f 69 6e 74 20 69 44 62 3b 0a 20 20 20 20 20 20 69 int iDb;. i
1c540 6e 74 20 77 72 46 6c 61 67 3b 0a 20 20 20 20 20 nt wrFlag;.
1c541 20 42 74 72 65 65 20 2a 70 58 3b 0a 20 20 20 20 Btree *pX;.
1c542 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 VdbeCursor *pC
1c543 75 72 3b 0a 20 20 20 20 20 20 44 62 20 2a 70 44 ur;. Db *pD
1c544 62 3b 0a 20 20 20 20 7d 20 61 77 3b 0a 20 20 20 b;. } aw;.
1c545 20 73 74 72 75 63 74 20 4f 50 5f 4f 70 65 6e 45 struct OP_OpenE
1c546 70 68 65 6d 65 72 61 6c 5f 73 74 61 63 6b 5f 76 phemeral_stack_v
1c547 61 72 73 20 7b 0a 20 20 20 20 20 20 56 64 62 65 ars {. Vdbe
1c548 43 75 72 73 6f 72 20 2a 70 43 78 3b 0a 20 20 20 Cursor *pCx;.
1c549 20 7d 20 61 78 3b 0a 20 20 20 20 73 74 72 75 63 } ax;. struc
1c54a 74 20 4f 50 5f 4f 70 65 6e 50 73 65 75 64 6f 5f t OP_OpenPseudo_
1c54b 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 stack_vars {.
1c54c 20 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 VdbeCursor *p
1c54d 43 78 3b 0a 20 20 20 20 7d 20 61 79 3b 0a 20 20 Cx;. } ay;.
1c54e 20 20 73 74 72 75 63 74 20 4f 50 5f 53 65 65 6b struct OP_Seek
1c54f 47 74 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a Gt_stack_vars {.
1c550 20 20 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 int res;.
1c551 20 20 20 20 20 69 6e 74 20 6f 63 3b 0a 20 20 20 int oc;.
1c552 20 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 VdbeCursor *p
1c553 43 3b 0a 20 20 20 20 20 20 55 6e 70 61 63 6b 65 C;. Unpacke
1c554 64 52 65 63 6f 72 64 20 72 3b 0a 20 20 20 20 20 dRecord r;.
1c555 20 69 6e 74 20 6e 46 69 65 6c 64 3b 0a 20 20 20 int nField;.
1c556 20 20 20 69 36 34 20 69 4b 65 79 3b 20 20 20 20 i64 iKey;
1c557 20 20 2f 2a 20 54 68 65 20 72 6f 77 69 64 20 77 /* The rowid w
1c558 65 20 61 72 65 20 74 6f 20 73 65 65 6b 20 74 6f e are to seek to
1c559 20 2a 2f 0a 20 20 20 20 7d 20 61 7a 3b 0a 20 20 */. } az;.
1c55a 20 20 73 74 72 75 63 74 20 4f 50 5f 53 65 65 6b struct OP_Seek
1c55b 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 _stack_vars {.
1c55c 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a VdbeCursor *
1c55d 70 43 3b 0a 20 20 20 20 7d 20 62 61 3b 0a 20 20 pC;. } ba;.
1c55e 20 20 73 74 72 75 63 74 20 4f 50 5f 46 6f 75 6e struct OP_Foun
1c55f 64 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 d_stack_vars {.
1c560 20 20 20 20 20 69 6e 74 20 61 6c 72 65 61 64 79 int already
1c561 45 78 69 73 74 73 3b 0a 20 20 20 20 20 20 56 64 Exists;. Vd
1c562 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 beCursor *pC;.
1c563 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20 int res;.
1c564 20 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 UnpackedRecor
1c565 64 20 2a 70 49 64 78 4b 65 79 3b 0a 20 20 20 20 d *pIdxKey;.
1c566 20 20 63 68 61 72 20 61 54 65 6d 70 52 65 63 5b char aTempRec[
1c567 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 55 6e ROUND8(sizeof(Un
1c568 70 61 63 6b 65 64 52 65 63 6f 72 64 29 29 20 2b packedRecord)) +
1c569 20 73 69 7a 65 6f 66 28 4d 65 6d 29 2a 33 20 2b sizeof(Mem)*3 +
1c56a 20 37 5d 3b 0a 20 20 20 20 7d 20 62 62 3b 0a 20 7];. } bb;.
1c56b 20 20 20 73 74 72 75 63 74 20 4f 50 5f 49 73 55 struct OP_IsU
1c56c 6e 69 71 75 65 5f 73 74 61 63 6b 5f 76 61 72 73 nique_stack_vars
1c56d 20 7b 0a 20 20 20 20 20 20 75 31 36 20 69 69 3b {. u16 ii;
1c56e 0a 20 20 20 20 20 20 56 64 62 65 43 75 72 73 6f . VdbeCurso
1c56f 72 20 2a 70 43 78 3b 0a 20 20 20 20 20 20 42 74 r *pCx;. Bt
1c570 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 Cursor *pCrsr;.
1c571 20 20 20 20 20 75 31 36 20 6e 46 69 65 6c 64 3b u16 nField;
1c572 0a 20 20 20 20 20 20 4d 65 6d 20 2a 61 4d 65 6d . Mem *aMem
1c573 3b 0a 20 20 20 20 20 20 55 6e 70 61 63 6b 65 64 ;. Unpacked
1c574 52 65 63 6f 72 64 20 72 3b 20 20 20 20 20 20 20 Record r;
1c575 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 2d /* B-
1c576 54 72 65 65 20 69 6e 64 65 78 20 73 65 61 72 63 Tree index searc
1c577 68 20 6b 65 79 20 2a 2f 0a 20 20 20 20 20 20 69 h key */. i
1c578 36 34 20 52 3b 20 20 20 20 20 20 20 20 20 20 20 64 R;
1c579 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c57a 20 20 2f 2a 20 52 6f 77 69 64 20 73 74 6f 72 65 /* Rowid store
1c57b 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 d in register P3
1c57c 20 2a 2f 0a 20 20 20 20 7d 20 62 63 3b 0a 20 20 */. } bc;.
1c57d 20 20 73 74 72 75 63 74 20 4f 50 5f 4e 6f 74 45 struct OP_NotE
1c57e 78 69 73 74 73 5f 73 74 61 63 6b 5f 76 61 72 73 xists_stack_vars
1c57f 20 7b 0a 20 20 20 20 20 20 56 64 62 65 43 75 72 {. VdbeCur
1c580 73 6f 72 20 2a 70 43 3b 0a 20 20 20 20 20 20 42 sor *pC;. B
1c581 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a tCursor *pCrsr;.
1c582 20 20 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 int res;.
1c583 20 20 20 20 20 75 36 34 20 69 4b 65 79 3b 0a 20 u64 iKey;.
1c584 20 20 20 7d 20 62 64 3b 0a 20 20 20 20 73 74 72 } bd;. str
1c585 75 63 74 20 4f 50 5f 4e 65 77 52 6f 77 69 64 5f uct OP_NewRowid_
1c586 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 stack_vars {.
1c587 20 20 20 69 36 34 20 76 3b 20 20 20 20 20 20 20 i64 v;
1c588 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
1c589 20 6e 65 77 20 72 6f 77 69 64 20 2a 2f 0a 20 20 new rowid */.
1c58a 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a VdbeCursor *
1c58b 70 43 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 75 pC; /* Cu
1c58c 72 73 6f 72 20 6f 66 20 74 61 62 6c 65 20 74 6f rsor of table to
1c58d 20 67 65 74 20 74 68 65 20 6e 65 77 20 72 6f 77 get the new row
1c58e 69 64 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 id */. int
1c58f 72 65 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 res;
1c590 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66 20 /* Result of
1c591 61 6e 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c an sqlite3BtreeL
1c592 61 73 74 28 29 20 2a 2f 0a 20 20 20 20 20 20 69 ast() */. i
1c593 6e 74 20 63 6e 74 3b 20 20 20 20 20 20 20 20 20 nt cnt;
1c594 20 20 20 20 20 20 2f 2a 20 43 6f 75 6e 74 65 72 /* Counter
1c595 20 74 6f 20 6c 69 6d 69 74 20 74 68 65 20 6e 75 to limit the nu
1c596 6d 62 65 72 20 6f 66 20 73 65 61 72 63 68 65 73 mber of searches
1c597 20 2a 2f 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 */. Mem *p
1c598 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 Mem;
1c599 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c /* Register hol
1c59a 64 69 6e 67 20 6c 61 72 67 65 73 74 20 72 6f 77 ding largest row
1c59b 69 64 20 66 6f 72 20 41 55 54 4f 49 4e 43 52 45 id for AUTOINCRE
1c59c 4d 45 4e 54 20 2a 2f 0a 20 20 20 20 20 20 56 64 MENT */. Vd
1c59d 62 65 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 3b beFrame *pFrame;
1c59e 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 66 72 61 /* Root fra
1c59f 6d 65 20 6f 66 20 56 44 42 45 20 2a 2f 0a 20 20 me of VDBE */.
1c5a0 20 20 7d 20 62 65 3b 0a 20 20 20 20 73 74 72 75 } be;. stru
1c5a1 63 74 20 4f 50 5f 49 6e 73 65 72 74 5f 73 74 61 ct OP_Insert_sta
1c5a2 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 ck_vars {.
1c5a3 4d 65 6d 20 2a 70 44 61 74 61 3b 20 20 20 20 20 Mem *pData;
1c5a4 20 20 2f 2a 20 4d 45 4d 20 63 65 6c 6c 20 68 6f /* MEM cell ho
1c5a5 6c 64 69 6e 67 20 64 61 74 61 20 66 6f 72 20 74 lding data for t
1c5a6 68 65 20 72 65 63 6f 72 64 20 74 6f 20 62 65 20 he record to be
1c5a7 69 6e 73 65 72 74 65 64 20 2a 2f 0a 20 20 20 20 inserted */.
1c5a8 20 20 4d 65 6d 20 2a 70 4b 65 79 3b 20 20 20 20 Mem *pKey;
1c5a9 20 20 20 20 2f 2a 20 4d 45 4d 20 63 65 6c 6c 20 /* MEM cell
1c5aa 68 6f 6c 64 69 6e 67 20 6b 65 79 20 20 66 6f 72 holding key for
1c5ab 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 the record */.
1c5ac 20 20 20 20 20 69 36 34 20 69 4b 65 79 3b 20 20 i64 iKey;
1c5ad 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69 6e /* The in
1c5ae 74 65 67 65 72 20 52 4f 57 49 44 20 6f 72 20 6b teger ROWID or k
1c5af 65 79 20 66 6f 72 20 74 68 65 20 72 65 63 6f 72 ey for the recor
1c5b0 64 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65 64 d to be inserted
1c5b1 20 2a 2f 0a 20 20 20 20 20 20 56 64 62 65 43 75 */. VdbeCu
1c5b2 72 73 6f 72 20 2a 70 43 3b 20 20 20 2f 2a 20 43 rsor *pC; /* C
1c5b3 75 72 73 6f 72 20 74 6f 20 74 61 62 6c 65 20 69 ursor to table i
1c5b4 6e 74 6f 20 77 68 69 63 68 20 69 6e 73 65 72 74 nto which insert
1c5b5 20 69 73 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20 is written */.
1c5b6 20 20 20 20 20 69 6e 74 20 6e 5a 65 72 6f 3b 20 int nZero;
1c5b7 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
1c5b8 20 6f 66 20 7a 65 72 6f 2d 62 79 74 65 73 20 74 of zero-bytes t
1c5b9 6f 20 61 70 70 65 6e 64 20 2a 2f 0a 20 20 20 20 o append */.
1c5ba 20 20 69 6e 74 20 73 65 65 6b 52 65 73 75 6c 74 int seekResult
1c5bb 3b 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66 ; /* Result of
1c5bc 20 70 72 69 6f 72 20 73 65 65 6b 20 6f 72 20 30 prior seek or 0
1c5bd 20 69 66 20 6e 6f 20 55 53 45 53 45 45 4b 52 45 if no USESEEKRE
1c5be 53 55 4c 54 20 66 6c 61 67 20 2a 2f 0a 20 20 20 SULT flag */.
1c5bf 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a const char *z
1c5c0 44 62 3b 20 20 2f 2a 20 64 61 74 61 62 61 73 65 Db; /* database
1c5c1 20 6e 61 6d 65 20 2d 20 75 73 65 64 20 62 79 20 name - used by
1c5c2 74 68 65 20 75 70 64 61 74 65 20 68 6f 6f 6b 20 the update hook
1c5c3 2a 2f 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 */. const c
1c5c4 68 61 72 20 2a 7a 54 62 6c 3b 20 2f 2a 20 54 61 har *zTbl; /* Ta
1c5c5 62 6c 65 20 6e 61 6d 65 20 2d 20 75 73 65 64 20 ble name - used
1c5c6 62 79 20 74 68 65 20 6f 70 64 61 74 65 20 68 6f by the opdate ho
1c5c7 6f 6b 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 ok */. int
1c5c8 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a op; /*
1c5c9 20 4f 70 63 6f 64 65 20 66 6f 72 20 75 70 64 61 Opcode for upda
1c5ca 74 65 20 68 6f 6f 6b 3a 20 53 51 4c 49 54 45 5f te hook: SQLITE_
1c5cb 55 50 44 41 54 45 20 6f 72 20 53 51 4c 49 54 45 UPDATE or SQLITE
1c5cc 5f 49 4e 53 45 52 54 20 2a 2f 0a 20 20 20 20 7d _INSERT */. }
1c5cd 20 62 66 3b 0a 20 20 20 20 73 74 72 75 63 74 20 bf;. struct
1c5ce 4f 50 5f 44 65 6c 65 74 65 5f 73 74 61 63 6b 5f OP_Delete_stack_
1c5cf 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69 36 34 vars {. i64
1c5d0 20 69 4b 65 79 3b 0a 20 20 20 20 20 20 56 64 62 iKey;. Vdb
1c5d1 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 20 eCursor *pC;.
1c5d2 20 7d 20 62 67 3b 0a 20 20 20 20 73 74 72 75 63 } bg;. struc
1c5d3 74 20 4f 50 5f 52 6f 77 44 61 74 61 5f 73 74 61 t OP_RowData_sta
1c5d4 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 ck_vars {.
1c5d5 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a VdbeCursor *pC;.
1c5d6 20 20 20 20 20 20 42 74 43 75 72 73 6f 72 20 2a BtCursor *
1c5d7 70 43 72 73 72 3b 0a 20 20 20 20 20 20 75 33 32 pCrsr;. u32
1c5d8 20 6e 3b 0a 20 20 20 20 20 20 69 36 34 20 6e 36 n;. i64 n6
1c5d9 34 3b 0a 20 20 20 20 7d 20 62 68 3b 0a 20 20 20 4;. } bh;.
1c5da 20 73 74 72 75 63 74 20 4f 50 5f 52 6f 77 69 64 struct OP_Rowid
1c5db 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 _stack_vars {.
1c5dc 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a VdbeCursor *
1c5dd 70 43 3b 0a 20 20 20 20 20 20 69 36 34 20 76 3b pC;. i64 v;
1c5de 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 . sqlite3_v
1c5df 74 61 62 20 2a 70 56 74 61 62 3b 0a 20 20 20 20 tab *pVtab;.
1c5e0 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f const sqlite3_
1c5e1 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b module *pModule;
1c5e2 0a 20 20 20 20 7d 20 62 69 3b 0a 20 20 20 20 73 . } bi;. s
1c5e3 74 72 75 63 74 20 4f 50 5f 4e 75 6c 6c 52 6f 77 truct OP_NullRow
1c5e4 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 _stack_vars {.
1c5e5 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a VdbeCursor *
1c5e6 70 43 3b 0a 20 20 20 20 7d 20 62 6a 3b 0a 20 20 pC;. } bj;.
1c5e7 20 20 73 74 72 75 63 74 20 4f 50 5f 4c 61 73 74 struct OP_Last
1c5e8 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 _stack_vars {.
1c5e9 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a VdbeCursor *
1c5ea 70 43 3b 0a 20 20 20 20 20 20 42 74 43 75 72 73 pC;. BtCurs
1c5eb 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 20 20 20 or *pCrsr;.
1c5ec 20 69 6e 74 20 72 65 73 3b 0a 20 20 20 20 7d 20 int res;. }
1c5ed 62 6b 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f bk;. struct O
1c5ee 50 5f 52 65 77 69 6e 64 5f 73 74 61 63 6b 5f 76 P_Rewind_stack_v
1c5ef 61 72 73 20 7b 0a 20 20 20 20 20 20 56 64 62 65 ars {. Vdbe
1c5f0 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 20 20 Cursor *pC;.
1c5f1 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 BtCursor *pCrs
1c5f2 72 3b 0a 20 20 20 20 20 20 69 6e 74 20 72 65 73 r;. int res
1c5f3 3b 0a 20 20 20 20 7d 20 62 6c 3b 0a 20 20 20 20 ;. } bl;.
1c5f4 73 74 72 75 63 74 20 4f 50 5f 4e 65 78 74 5f 73 struct OP_Next_s
1c5f5 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 tack_vars {.
1c5f6 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 VdbeCursor *pC
1c5f7 3b 0a 20 20 20 20 20 20 42 74 43 75 72 73 6f 72 ;. BtCursor
1c5f8 20 2a 70 43 72 73 72 3b 0a 20 20 20 20 20 20 69 *pCrsr;. i
1c5f9 6e 74 20 72 65 73 3b 0a 20 20 20 20 7d 20 62 6d nt res;. } bm
1c5fa 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f ;. struct OP_
1c5fb 49 64 78 49 6e 73 65 72 74 5f 73 74 61 63 6b 5f IdxInsert_stack_
1c5fc 76 61 72 73 20 7b 0a 20 20 20 20 20 20 56 64 62 vars {. Vdb
1c5fd 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 20 eCursor *pC;.
1c5fe 20 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 BtCursor *pCr
1c5ff 73 72 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e 4b sr;. int nK
1c600 65 79 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 ey;. const
1c601 63 68 61 72 20 2a 7a 4b 65 79 3b 0a 20 20 20 20 char *zKey;.
1c602 7d 20 62 6e 3b 0a 20 20 20 20 73 74 72 75 63 74 } bn;. struct
1c603 20 4f 50 5f 49 64 78 44 65 6c 65 74 65 5f 73 74 OP_IdxDelete_st
1c604 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 ack_vars {.
1c605 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b VdbeCursor *pC;
1c606 0a 20 20 20 20 20 20 42 74 43 75 72 73 6f 72 20 . BtCursor
1c607 2a 70 43 72 73 72 3b 0a 20 20 20 20 20 20 69 6e *pCrsr;. in
1c608 74 20 72 65 73 3b 0a 20 20 20 20 20 20 55 6e 70 t res;. Unp
1c609 61 63 6b 65 64 52 65 63 6f 72 64 20 72 3b 0a 20 ackedRecord r;.
1c60a 20 20 20 7d 20 62 6f 3b 0a 20 20 20 20 73 74 72 } bo;. str
1c60b 75 63 74 20 4f 50 5f 49 64 78 52 6f 77 69 64 5f uct OP_IdxRowid_
1c60c 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 stack_vars {.
1c60d 20 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 BtCursor *pCr
1c60e 73 72 3b 0a 20 20 20 20 20 20 56 64 62 65 43 75 sr;. VdbeCu
1c60f 72 73 6f 72 20 2a 70 43 3b 0a 20 20 20 20 20 20 rsor *pC;.
1c610 69 36 34 20 72 6f 77 69 64 3b 0a 20 20 20 20 7d i64 rowid;. }
1c611 20 62 70 3b 0a 20 20 20 20 73 74 72 75 63 74 20 bp;. struct
1c612 4f 50 5f 49 64 78 47 45 5f 73 74 61 63 6b 5f 76 OP_IdxGE_stack_v
1c613 61 72 73 20 7b 0a 20 20 20 20 20 20 56 64 62 65 ars {. Vdbe
1c614 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 20 20 Cursor *pC;.
1c615 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20 20 20 int res;.
1c616 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 UnpackedRecord
1c617 72 3b 0a 20 20 20 20 7d 20 62 71 3b 0a 20 20 20 r;. } bq;.
1c618 20 73 74 72 75 63 74 20 4f 50 5f 44 65 73 74 72 struct OP_Destr
1c619 6f 79 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a oy_stack_vars {.
1c61a 20 20 20 20 20 20 69 6e 74 20 69 4d 6f 76 65 64 int iMoved
1c61b 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 43 6e 74 ;. int iCnt
1c61c 3b 0a 20 20 20 20 20 20 56 64 62 65 20 2a 70 56 ;. Vdbe *pV
1c61d 64 62 65 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 dbe;. int i
1c61e 44 62 3b 0a 20 20 20 20 7d 20 62 72 3b 0a 20 20 Db;. } br;.
1c61f 20 20 73 74 72 75 63 74 20 4f 50 5f 43 6c 65 61 struct OP_Clea
1c620 72 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 r_stack_vars {.
1c621 20 20 20 20 20 69 6e 74 20 6e 43 68 61 6e 67 65 int nChange
1c622 3b 0a 20 20 20 20 7d 20 62 73 3b 0a 20 20 20 20 ;. } bs;.
1c623 73 74 72 75 63 74 20 4f 50 5f 43 72 65 61 74 65 struct OP_Create
1c624 54 61 62 6c 65 5f 73 74 61 63 6b 5f 76 61 72 73 Table_stack_vars
1c625 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 70 67 6e {. int pgn
1c626 6f 3b 0a 20 20 20 20 20 20 69 6e 74 20 66 6c 61 o;. int fla
1c627 67 73 3b 0a 20 20 20 20 20 20 44 62 20 2a 70 44 gs;. Db *pD
1c628 62 3b 0a 20 20 20 20 7d 20 62 74 3b 0a 20 20 20 b;. } bt;.
1c629 20 73 74 72 75 63 74 20 4f 50 5f 50 61 72 73 65 struct OP_Parse
1c62a 53 63 68 65 6d 61 5f 73 74 61 63 6b 5f 76 61 72 Schema_stack_var
1c62b 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 44 s {. int iD
1c62c 62 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 b;. const c
1c62d 68 61 72 20 2a 7a 4d 61 73 74 65 72 3b 0a 20 20 har *zMaster;.
1c62e 20 20 20 20 63 68 61 72 20 2a 7a 53 71 6c 3b 0a char *zSql;.
1c62f 20 20 20 20 20 20 49 6e 69 74 44 61 74 61 20 69 InitData i
1c630 6e 69 74 44 61 74 61 3b 0a 20 20 20 20 7d 20 62 nitData;. } b
1c631 75 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 u;. struct OP
1c632 5f 49 6e 74 65 67 72 69 74 79 43 6b 5f 73 74 61 _IntegrityCk_sta
1c633 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 ck_vars {.
1c634 69 6e 74 20 6e 52 6f 6f 74 3b 20 20 20 20 20 20 int nRoot;
1c635 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 61 62 /* Number of tab
1c636 6c 65 73 20 74 6f 20 63 68 65 63 6b 2e 20 20 28 les to check. (
1c637 4e 75 6d 62 65 72 20 6f 66 20 72 6f 6f 74 20 70 Number of root p
1c638 61 67 65 73 2e 29 20 2a 2f 0a 20 20 20 20 20 20 ages.) */.
1c639 69 6e 74 20 2a 61 52 6f 6f 74 3b 20 20 20 20 20 int *aRoot;
1c63a 2f 2a 20 41 72 72 61 79 20 6f 66 20 72 6f 6f 74 /* Array of root
1c63b 70 61 67 65 20 6e 75 6d 62 65 72 73 20 66 6f 72 page numbers for
1c63c 20 74 61 62 6c 65 73 20 74 6f 20 62 65 20 63 68 tables to be ch
1c63d 65 63 6b 65 64 20 2a 2f 0a 20 20 20 20 20 20 69 ecked */. i
1c63e 6e 74 20 6a 3b 20 20 20 20 20 20 20 20 20 20 2f nt j; /
1c63f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a * Loop counter *
1c640 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 45 72 72 /. int nErr
1c641 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 ; /* Numbe
1c642 72 20 6f 66 20 65 72 72 6f 72 73 20 72 65 70 6f r of errors repo
1c643 72 74 65 64 20 2a 2f 0a 20 20 20 20 20 20 63 68 rted */. ch
1c644 61 72 20 2a 7a 3b 20 20 20 20 20 20 20 20 2f 2a ar *z; /*
1c645 20 54 65 78 74 20 6f 66 20 74 68 65 20 65 72 72 Text of the err
1c646 6f 72 20 72 65 70 6f 72 74 20 2a 2f 0a 20 20 20 or report */.
1c647 20 20 20 4d 65 6d 20 2a 70 6e 45 72 72 3b 20 20 Mem *pnErr;
1c648 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 6b /* Register k
1c649 65 65 70 69 6e 67 20 74 72 61 63 6b 20 6f 66 20 eeping track of
1c64a 65 72 72 6f 72 73 20 72 65 6d 61 69 6e 69 6e 67 errors remaining
1c64b 20 2a 2f 0a 20 20 20 20 7d 20 62 76 3b 0a 20 20 */. } bv;.
1c64c 20 20 73 74 72 75 63 74 20 4f 50 5f 52 6f 77 53 struct OP_RowS
1c64d 65 74 41 64 64 5f 73 74 61 63 6b 5f 76 61 72 73 etAdd_stack_vars
1c64e 20 7b 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 49 {. Mem *pI
1c64f 64 78 3b 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 dx;. Mem *p
1c650 56 61 6c 3b 0a 20 20 20 20 7d 20 62 77 3b 0a 20 Val;. } bw;.
1c651 20 20 20 73 74 72 75 63 74 20 4f 50 5f 52 6f 77 struct OP_Row
1c652 53 65 74 52 65 61 64 5f 73 74 61 63 6b 5f 76 61 SetRead_stack_va
1c653 72 73 20 7b 0a 20 20 20 20 20 20 4d 65 6d 20 2a rs {. Mem *
1c654 70 49 64 78 3b 0a 20 20 20 20 20 20 69 36 34 20 pIdx;. i64
1c655 76 61 6c 3b 0a 20 20 20 20 7d 20 62 78 3b 0a 20 val;. } bx;.
1c656 20 20 20 73 74 72 75 63 74 20 4f 50 5f 52 6f 77 struct OP_Row
1c657 53 65 74 54 65 73 74 5f 73 74 61 63 6b 5f 76 61 SetTest_stack_va
1c658 72 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 rs {. int i
1c659 53 65 74 3b 0a 20 20 20 20 20 20 69 6e 74 20 65 Set;. int e
1c65a 78 69 73 74 73 3b 0a 20 20 20 20 7d 20 62 79 3b xists;. } by;
1c65b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 50 . struct OP_P
1c65c 72 6f 67 72 61 6d 5f 73 74 61 63 6b 5f 76 61 72 rogram_stack_var
1c65d 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 4d s {. int nM
1c65e 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 em;
1c65f 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d /* Number of m
1c660 65 6d 6f 72 79 20 72 65 67 69 73 74 65 72 73 20 emory registers
1c661 66 6f 72 20 73 75 62 2d 70 72 6f 67 72 61 6d 20 for sub-program
1c662 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 42 79 */. int nBy
1c663 74 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 te;
1c664 20 2f 2a 20 42 79 74 65 73 20 6f 66 20 72 75 6e /* Bytes of run
1c665 74 69 6d 65 20 73 70 61 63 65 20 72 65 71 75 69 time space requi
1c666 72 65 64 20 66 6f 72 20 73 75 62 2d 70 72 6f 67 red for sub-prog
1c667 72 61 6d 20 2a 2f 0a 20 20 20 20 20 20 4d 65 6d ram */. Mem
1c668 20 2a 70 52 74 3b 20 20 20 20 20 20 20 20 20 20 *pRt;
1c669 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 /* Register
1c66a 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 72 75 6e to allocate run
1c66b 74 69 6d 65 20 73 70 61 63 65 20 2a 2f 0a 20 20 time space */.
1c66c 20 20 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 20 20 Mem *pMem;
1c66d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 /* U
1c66e 73 65 64 20 74 6f 20 69 74 65 72 61 74 65 20 74 sed to iterate t
1c66f 68 72 6f 75 67 68 20 6d 65 6d 6f 72 79 20 63 65 hrough memory ce
1c670 6c 6c 73 20 2a 2f 0a 20 20 20 20 20 20 4d 65 6d lls */. Mem
1c671 20 2a 70 45 6e 64 3b 20 20 20 20 20 20 20 20 20 *pEnd;
1c672 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 6d 65 6d /* Last mem
1c673 6f 72 79 20 63 65 6c 6c 20 69 6e 20 6e 65 77 20 ory cell in new
1c674 61 72 72 61 79 20 2a 2f 0a 20 20 20 20 20 20 56 array */. V
1c675 64 62 65 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 dbeFrame *pFrame
1c676 3b 20 20 20 20 20 20 2f 2a 20 4e 65 77 20 76 64 ; /* New vd
1c677 62 65 20 66 72 61 6d 65 20 74 6f 20 65 78 65 63 be frame to exec
1c678 75 74 65 20 69 6e 20 2a 2f 0a 20 20 20 20 20 20 ute in */.
1c679 53 75 62 50 72 6f 67 72 61 6d 20 2a 70 50 72 6f SubProgram *pPro
1c67a 67 72 61 6d 3b 20 20 20 2f 2a 20 53 75 62 2d 70 gram; /* Sub-p
1c67b 72 6f 67 72 61 6d 20 74 6f 20 65 78 65 63 75 74 rogram to execut
1c67c 65 20 2a 2f 0a 20 20 20 20 20 20 76 6f 69 64 20 e */. void
1c67d 2a 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 *t;
1c67e 20 20 20 2f 2a 20 54 6f 6b 65 6e 20 69 64 65 6e /* Token iden
1c67f 74 69 66 79 69 6e 67 20 74 72 69 67 67 65 72 20 tifying trigger
1c680 2a 2f 0a 20 20 20 20 7d 20 62 7a 3b 0a 20 20 20 */. } bz;.
1c681 20 73 74 72 75 63 74 20 4f 50 5f 50 61 72 61 6d struct OP_Param
1c682 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 _stack_vars {.
1c683 20 20 20 20 56 64 62 65 46 72 61 6d 65 20 2a 70 VdbeFrame *p
1c684 46 72 61 6d 65 3b 0a 20 20 20 20 20 20 4d 65 6d Frame;. Mem
1c685 20 2a 70 49 6e 3b 0a 20 20 20 20 7d 20 63 61 3b *pIn;. } ca;
1c686 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 4d . struct OP_M
1c687 65 6d 4d 61 78 5f 73 74 61 63 6b 5f 76 61 72 73 emMax_stack_vars
1c688 20 7b 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 49 {. Mem *pI
1c689 6e 31 3b 0a 20 20 20 20 20 20 56 64 62 65 46 72 n1;. VdbeFr
1c68a 61 6d 65 20 2a 70 46 72 61 6d 65 3b 0a 20 20 20 ame *pFrame;.
1c68b 20 7d 20 63 62 3b 0a 20 20 20 20 73 74 72 75 63 } cb;. struc
1c68c 74 20 4f 50 5f 41 67 67 53 74 65 70 5f 73 74 61 t OP_AggStep_sta
1c68d 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 ck_vars {.
1c68e 69 6e 74 20 6e 3b 0a 20 20 20 20 20 20 69 6e 74 int n;. int
1c68f 20 69 3b 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 i;. Mem *p
1c690 4d 65 6d 3b 0a 20 20 20 20 20 20 4d 65 6d 20 2a Mem;. Mem *
1c691 70 52 65 63 3b 0a 20 20 20 20 20 20 73 71 6c 69 pRec;. sqli
1c692 74 65 33 5f 63 6f 6e 74 65 78 74 20 63 74 78 3b te3_context ctx;
1c693 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 . sqlite3_v
1c694 61 6c 75 65 20 2a 2a 61 70 56 61 6c 3b 0a 20 20 alue **apVal;.
1c695 20 20 7d 20 63 63 3b 0a 20 20 20 20 73 74 72 75 } cc;. stru
1c696 63 74 20 4f 50 5f 41 67 67 46 69 6e 61 6c 5f 73 ct OP_AggFinal_s
1c697 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 tack_vars {.
1c698 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 20 Mem *pMem;.
1c699 20 7d 20 63 64 3b 0a 20 20 20 20 73 74 72 75 63 } cd;. struc
1c69a 74 20 4f 50 5f 49 6e 63 72 56 61 63 75 75 6d 5f t OP_IncrVacuum_
1c69b 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 stack_vars {.
1c69c 20 20 20 42 74 72 65 65 20 2a 70 42 74 3b 0a 20 Btree *pBt;.
1c69d 20 20 20 7d 20 63 65 3b 0a 20 20 20 20 73 74 72 } ce;. str
1c69e 75 63 74 20 4f 50 5f 56 42 65 67 69 6e 5f 73 74 uct OP_VBegin_st
1c69f 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 ack_vars {.
1c6a0 20 56 54 61 62 6c 65 20 2a 70 56 54 61 62 3b 0a VTable *pVTab;.
1c6a1 20 20 20 20 7d 20 63 66 3b 0a 20 20 20 20 73 74 } cf;. st
1c6a2 72 75 63 74 20 4f 50 5f 56 4f 70 65 6e 5f 73 74 ruct OP_VOpen_st
1c6a3 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 ack_vars {.
1c6a4 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 75 VdbeCursor *pCu
1c6a5 72 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 r;. sqlite3
1c6a6 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 70 56 _vtab_cursor *pV
1c6a7 74 61 62 43 75 72 73 6f 72 3b 0a 20 20 20 20 20 tabCursor;.
1c6a8 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 sqlite3_vtab *p
1c6a9 56 74 61 62 3b 0a 20 20 20 20 20 20 73 71 6c 69 Vtab;. sqli
1c6aa 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 te3_module *pMod
1c6ab 75 6c 65 3b 0a 20 20 20 20 7d 20 63 67 3b 0a 20 ule;. } cg;.
1c6ac 20 20 20 73 74 72 75 63 74 20 4f 50 5f 56 46 69 struct OP_VFi
1c6ad 6c 74 65 72 5f 73 74 61 63 6b 5f 76 61 72 73 20 lter_stack_vars
1c6ae 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 41 72 67 {. int nArg
1c6af 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 51 75 65 ;. int iQue
1c6b0 72 79 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 ry;. const
1c6b1 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a sqlite3_module *
1c6b2 70 4d 6f 64 75 6c 65 3b 0a 20 20 20 20 20 20 4d pModule;. M
1c6b3 65 6d 20 2a 70 51 75 65 72 79 3b 0a 20 20 20 20 em *pQuery;.
1c6b4 20 20 4d 65 6d 20 2a 70 41 72 67 63 3b 0a 20 20 Mem *pArgc;.
1c6b5 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 sqlite3_vtab
1c6b6 5f 63 75 72 73 6f 72 20 2a 70 56 74 61 62 43 75 _cursor *pVtabCu
1c6b7 72 73 6f 72 3b 0a 20 20 20 20 20 20 73 71 6c 69 rsor;. sqli
1c6b8 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b te3_vtab *pVtab;
1c6b9 0a 20 20 20 20 20 20 56 64 62 65 43 75 72 73 6f . VdbeCurso
1c6ba 72 20 2a 70 43 75 72 3b 0a 20 20 20 20 20 20 69 r *pCur;. i
1c6bb 6e 74 20 72 65 73 3b 0a 20 20 20 20 20 20 69 6e nt res;. in
1c6bc 74 20 69 3b 0a 20 20 20 20 20 20 4d 65 6d 20 2a t i;. Mem *
1c6bd 2a 61 70 41 72 67 3b 0a 20 20 20 20 7d 20 63 68 *apArg;. } ch
1c6be 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f ;. struct OP_
1c6bf 56 43 6f 6c 75 6d 6e 5f 73 74 61 63 6b 5f 76 61 VColumn_stack_va
1c6c0 72 73 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 rs {. sqlit
1c6c1 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b 0a e3_vtab *pVtab;.
1c6c2 20 20 20 20 20 20 63 6f 6e 73 74 20 73 71 6c 69 const sqli
1c6c3 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 te3_module *pMod
1c6c4 75 6c 65 3b 0a 20 20 20 20 20 20 4d 65 6d 20 2a ule;. Mem *
1c6c5 70 44 65 73 74 3b 0a 20 20 20 20 20 20 73 71 6c pDest;. sql
1c6c6 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 73 43 6f ite3_context sCo
1c6c7 6e 74 65 78 74 3b 0a 20 20 20 20 7d 20 63 69 3b ntext;. } ci;
1c6c8 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 56 . struct OP_V
1c6c9 4e 65 78 74 5f 73 74 61 63 6b 5f 76 61 72 73 20 Next_stack_vars
1c6ca 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f {. sqlite3_
1c6cb 76 74 61 62 20 2a 70 56 74 61 62 3b 0a 20 20 20 vtab *pVtab;.
1c6cc 20 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 const sqlite3
1c6cd 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 _module *pModule
1c6ce 3b 0a 20 20 20 20 20 20 69 6e 74 20 72 65 73 3b ;. int res;
1c6cf 0a 20 20 20 20 20 20 56 64 62 65 43 75 72 73 6f . VdbeCurso
1c6d0 72 20 2a 70 43 75 72 3b 0a 20 20 20 20 7d 20 63 r *pCur;. } c
1c6d1 6a 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 j;. struct OP
1c6d2 5f 56 52 65 6e 61 6d 65 5f 73 74 61 63 6b 5f 76 _VRename_stack_v
1c6d3 61 72 73 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 ars {. sqli
1c6d4 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b te3_vtab *pVtab;
1c6d5 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 4e 61 6d . Mem *pNam
1c6d6 65 3b 0a 20 20 20 20 7d 20 63 6b 3b 0a 20 20 20 e;. } ck;.
1c6d7 20 73 74 72 75 63 74 20 4f 50 5f 56 55 70 64 61 struct OP_VUpda
1c6d8 74 65 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a te_stack_vars {.
1c6d9 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74 sqlite3_vt
1c6da 61 62 20 2a 70 56 74 61 62 3b 0a 20 20 20 20 20 ab *pVtab;.
1c6db 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 sqlite3_module
1c6dc 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20 20 20 20 20 *pModule;.
1c6dd 69 6e 74 20 6e 41 72 67 3b 0a 20 20 20 20 20 20 int nArg;.
1c6de 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 73 71 6c int i;. sql
1c6df 69 74 65 5f 69 6e 74 36 34 20 72 6f 77 69 64 3b ite_int64 rowid;
1c6e0 0a 20 20 20 20 20 20 4d 65 6d 20 2a 2a 61 70 41 . Mem **apA
1c6e1 72 67 3b 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 rg;. Mem *p
1c6e2 58 3b 0a 20 20 20 20 7d 20 63 6c 3b 0a 20 20 20 X;. } cl;.
1c6e3 20 73 74 72 75 63 74 20 4f 50 5f 50 61 67 65 63 struct OP_Pagec
1c6e4 6f 75 6e 74 5f 73 74 61 63 6b 5f 76 61 72 73 20 ount_stack_vars
1c6e5 7b 0a 20 20 20 20 20 20 69 6e 74 20 70 31 3b 0a {. int p1;.
1c6e6 20 20 20 20 20 20 69 6e 74 20 6e 50 61 67 65 3b int nPage;
1c6e7 0a 20 20 20 20 20 20 50 61 67 65 72 20 2a 70 50 . Pager *pP
1c6e8 61 67 65 72 3b 0a 20 20 20 20 7d 20 63 6d 3b 0a ager;. } cm;.
1c6e9 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 54 72 struct OP_Tr
1c6ea 61 63 65 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b ace_stack_vars {
1c6eb 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 54 72 . char *zTr
1c6ec 61 63 65 3b 0a 20 20 20 20 7d 20 63 6e 3b 0a 20 ace;. } cn;.
1c6ed 20 7d 20 75 3b 0a 20 20 2f 2a 20 45 6e 64 20 61 } u;. /* End a
1c6ee 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 67 65 6e utomatically gen
1c6ef 65 72 61 74 65 64 20 63 6f 64 65 0a 20 20 2a 2a erated code. **
1c6f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c6f1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c6f2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c6f3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c6f4 2a 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 **/.. assert( p
1c6f5 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 ->magic==VDBE_MA
1c6f6 47 49 43 5f 52 55 4e 20 29 3b 20 20 2f 2a 20 73 GIC_RUN ); /* s
1c6f7 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 76 65 qlite3_step() ve
1c6f8 72 69 66 69 65 73 20 74 68 69 73 20 2a 2f 0a 20 rifies this */.
1c6f9 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6d 61 67 assert( db->mag
1c6fa 69 63 3d 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43 ic==SQLITE_MAGIC
1c6fb 5f 42 55 53 59 20 29 3b 0a 20 20 73 71 6c 69 74 _BUSY );. sqlit
1c6fc 65 33 56 64 62 65 4d 75 74 65 78 41 72 72 61 79 e3VdbeMutexArray
1c6fd 45 6e 74 65 72 28 70 29 3b 0a 20 20 69 66 28 20 Enter(p);. if(
1c6fe 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f p->rc==SQLITE_NO
1c6ff 4d 45 4d 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 MEM ){. /* Th
1c700 69 73 20 68 61 70 70 65 6e 73 20 69 66 20 61 20 is happens if a
1c701 6d 61 6c 6c 6f 63 28 29 20 69 6e 73 69 64 65 20 malloc() inside
1c702 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 a call to sqlite
1c703 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 20 3_column_text()
1c704 6f 72 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 or. ** sqlite
1c705 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 3_column_text16(
1c706 29 20 66 61 69 6c 65 64 2e 20 20 2a 2f 0a 20 20 ) failed. */.
1c707 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 goto no_mem;.
1c708 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e }. assert( p->
1c709 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c rc==SQLITE_OK ||
1c70a 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 42 p->rc==SQLITE_B
1c70b 55 53 59 20 29 3b 0a 20 20 70 2d 3e 72 63 20 3d USY );. p->rc =
1c70c 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61 73 SQLITE_OK;. as
1c70d 73 65 72 74 28 20 70 2d 3e 65 78 70 6c 61 69 6e sert( p->explain
1c70e 3d 3d 30 20 29 3b 0a 20 20 70 2d 3e 70 52 65 73 ==0 );. p->pRes
1c70f 75 6c 74 53 65 74 20 3d 20 30 3b 0a 20 20 64 62 ultSet = 0;. db
1c710 2d 3e 62 75 73 79 48 61 6e 64 6c 65 72 2e 6e 42 ->busyHandler.nB
1c711 75 73 79 20 3d 20 30 3b 0a 20 20 43 48 45 43 4b usy = 0;. CHECK
1c712 5f 46 4f 52 5f 49 4e 54 45 52 52 55 50 54 3b 0a _FOR_INTERRUPT;.
1c713 20 20 73 71 6c 69 74 65 33 56 64 62 65 49 4f 54 sqlite3VdbeIOT
1c714 72 61 63 65 53 71 6c 28 70 29 3b 0a 23 69 66 64 raceSql(p);.#ifd
1c715 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a ef SQLITE_DEBUG.
1c716 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 sqlite3BeginBe
1c717 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 nignMalloc();.
1c718 69 66 28 20 70 2d 3e 70 63 3d 3d 30 20 0a 20 20 if( p->pc==0 .
1c719 20 26 26 20 28 28 70 2d 3e 64 62 2d 3e 66 6c 61 && ((p->db->fla
1c71a 67 73 20 26 20 53 51 4c 49 54 45 5f 56 64 62 65 gs & SQLITE_Vdbe
1c71b 4c 69 73 74 69 6e 67 29 20 7c 7c 20 66 69 6c 65 Listing) || file
1c71c 45 78 69 73 74 73 28 64 62 2c 20 22 76 64 62 65 Exists(db, "vdbe
1c71d 5f 65 78 70 6c 61 69 6e 22 29 29 0a 20 20 29 7b _explain")). ){
1c71e 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 . int i;.
1c71f 70 72 69 6e 74 66 28 22 56 44 42 45 20 50 72 6f printf("VDBE Pro
1c720 67 72 61 6d 20 4c 69 73 74 69 6e 67 3a 5c 6e 22 gram Listing:\n"
1c721 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 );. sqlite3Vd
1c722 62 65 50 72 69 6e 74 53 71 6c 28 70 29 3b 0a 20 bePrintSql(p);.
1c723 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d for(i=0; i<p-
1c724 3e 6e 4f 70 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 >nOp; i++){.
1c725 20 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69 sqlite3VdbePri
1c726 6e 74 4f 70 28 73 74 64 6f 75 74 2c 20 69 2c 20 ntOp(stdout, i,
1c727 26 70 2d 3e 61 4f 70 5b 69 5d 29 3b 0a 20 20 20 &p->aOp[i]);.
1c728 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 66 69 6c }. }. if( fil
1c729 65 45 78 69 73 74 73 28 64 62 2c 20 22 76 64 62 eExists(db, "vdb
1c72a 65 5f 74 72 61 63 65 22 29 20 29 7b 0a 20 20 20 e_trace") ){.
1c72b 20 70 2d 3e 74 72 61 63 65 20 3d 20 73 74 64 6f p->trace = stdo
1c72c 75 74 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 ut;. }. sqlite
1c72d 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 3EndBenignMalloc
1c72e 28 29 3b 0a 23 65 6e 64 69 66 0a 20 20 66 6f 72 ();.#endif. for
1c72f 28 70 63 3d 70 2d 3e 70 63 3b 20 72 63 3d 3d 53 (pc=p->pc; rc==S
1c730 51 4c 49 54 45 5f 4f 4b 3b 20 70 63 2b 2b 29 7b QLITE_OK; pc++){
1c731 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 63 3e . assert( pc>
1c732 3d 30 20 26 26 20 70 63 3c 70 2d 3e 6e 4f 70 20 =0 && pc<p->nOp
1c733 29 3b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d );. if( db->m
1c734 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 67 6f allocFailed ) go
1c735 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 23 69 66 64 65 to no_mem;.#ifde
1c736 66 20 56 44 42 45 5f 50 52 4f 46 49 4c 45 0a 20 f VDBE_PROFILE.
1c737 20 20 20 6f 72 69 67 50 63 20 3d 20 70 63 3b 0a origPc = pc;.
1c738 20 20 20 20 73 74 61 72 74 20 3d 20 73 71 6c 69 start = sqli
1c739 74 65 33 48 77 74 69 6d 65 28 29 3b 0a 23 65 6e te3Hwtime();.#en
1c73a 64 69 66 0a 20 20 20 20 70 4f 70 20 3d 20 26 70 dif. pOp = &p
1c73b 2d 3e 61 4f 70 5b 70 63 5d 3b 0a 0a 20 20 20 20 ->aOp[pc];..
1c73c 2f 2a 20 4f 6e 6c 79 20 61 6c 6c 6f 77 20 74 72 /* Only allow tr
1c73d 61 63 69 6e 67 20 69 66 20 53 51 4c 49 54 45 5f acing if SQLITE_
1c73e 44 45 42 55 47 20 69 73 20 64 65 66 69 6e 65 64 DEBUG is defined
1c73f 2e 0a 20 20 20 20 2a 2f 0a 23 69 66 64 65 66 20 .. */.#ifdef
1c740 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20 SQLITE_DEBUG.
1c741 20 69 66 28 20 70 2d 3e 74 72 61 63 65 20 29 7b if( p->trace ){
1c742 0a 20 20 20 20 20 20 69 66 28 20 70 63 3d 3d 30 . if( pc==0
1c743 20 29 7b 0a 20 20 20 20 20 20 20 20 70 72 69 6e ){. prin
1c744 74 66 28 22 56 44 42 45 20 45 78 65 63 75 74 69 tf("VDBE Executi
1c745 6f 6e 20 54 72 61 63 65 3a 5c 6e 22 29 3b 0a 20 on Trace:\n");.
1c746 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 sqlite3Vd
1c747 62 65 50 72 69 6e 74 53 71 6c 28 70 29 3b 0a 20 bePrintSql(p);.
1c748 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c }. sql
1c749 69 74 65 33 56 64 62 65 50 72 69 6e 74 4f 70 28 ite3VdbePrintOp(
1c74a 70 2d 3e 74 72 61 63 65 2c 20 70 63 2c 20 70 4f p->trace, pc, pO
1c74b 70 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 p);. }. if
1c74c 28 20 70 2d 3e 74 72 61 63 65 3d 3d 30 20 26 26 ( p->trace==0 &&
1c74d 20 70 63 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 pc==0 ){.
1c74e 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 sqlite3BeginBeni
1c74f 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 gnMalloc();.
1c750 20 20 69 66 28 20 66 69 6c 65 45 78 69 73 74 73 if( fileExists
1c751 28 64 62 2c 20 22 76 64 62 65 5f 73 71 6c 74 72 (db, "vdbe_sqltr
1c752 61 63 65 22 29 20 29 7b 0a 20 20 20 20 20 20 20 ace") ){.
1c753 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e sqlite3VdbePrin
1c754 74 53 71 6c 28 70 29 3b 0a 20 20 20 20 20 20 7d tSql(p);. }
1c755 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 6e . sqlite3En
1c756 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b dBenignMalloc();
1c757 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 . }.#endif.
1c758 20 20 20 20 0a 0a 20 20 20 20 2f 2a 20 43 68 65 .. /* Che
1c759 63 6b 20 74 6f 20 73 65 65 20 69 66 20 77 65 20 ck to see if we
1c75a 6e 65 65 64 20 74 6f 20 73 69 6d 75 6c 61 74 65 need to simulate
1c75b 20 61 6e 20 69 6e 74 65 72 72 75 70 74 2e 20 20 an interrupt.
1c75c 54 68 69 73 20 6f 6e 6c 79 20 68 61 70 70 65 6e This only happen
1c75d 73 0a 20 20 20 20 2a 2a 20 69 66 20 77 65 20 68 s. ** if we h
1c75e 61 76 65 20 61 20 73 70 65 63 69 61 6c 20 74 65 ave a special te
1c75f 73 74 20 62 75 69 6c 64 2e 0a 20 20 20 20 2a 2f st build.. */
1c760 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 .#ifdef SQLITE_T
1c761 45 53 54 0a 20 20 20 20 69 66 28 20 73 71 6c 69 EST. if( sqli
1c762 74 65 33 5f 69 6e 74 65 72 72 75 70 74 5f 63 6f te3_interrupt_co
1c763 75 6e 74 3e 30 20 29 7b 0a 20 20 20 20 20 20 73 unt>0 ){. s
1c764 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 qlite3_interrupt
1c765 5f 63 6f 75 6e 74 2d 2d 3b 0a 20 20 20 20 20 20 _count--;.
1c766 69 66 28 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 if( sqlite3_inte
1c767 72 72 75 70 74 5f 63 6f 75 6e 74 3d 3d 30 20 29 rrupt_count==0 )
1c768 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
1c769 33 5f 69 6e 74 65 72 72 75 70 74 28 64 62 29 3b 3_interrupt(db);
1c76a 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 . }. }.#
1c76b 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 endif..#ifndef S
1c76c 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 52 4f 47 52 QLITE_OMIT_PROGR
1c76d 45 53 53 5f 43 41 4c 4c 42 41 43 4b 0a 20 20 20 ESS_CALLBACK.
1c76e 20 2f 2a 20 43 61 6c 6c 20 74 68 65 20 70 72 6f /* Call the pro
1c76f 67 72 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 69 gress callback i
1c770 66 20 69 74 20 69 73 20 63 6f 6e 66 69 67 75 72 f it is configur
1c771 65 64 20 61 6e 64 20 74 68 65 20 72 65 71 75 69 ed and the requi
1c772 72 65 64 20 6e 75 6d 62 65 72 0a 20 20 20 20 2a red number. *
1c773 2a 20 6f 66 20 56 44 42 45 20 6f 70 73 20 68 61 * of VDBE ops ha
1c774 76 65 20 62 65 65 6e 20 65 78 65 63 75 74 65 64 ve been executed
1c775 20 28 65 69 74 68 65 72 20 73 69 6e 63 65 20 74 (either since t
1c776 68 69 73 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f his invocation o
1c777 66 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 f. ** sqlite3
1c778 56 64 62 65 45 78 65 63 28 29 20 6f 72 20 73 69 VdbeExec() or si
1c779 6e 63 65 20 6c 61 73 74 20 74 69 6d 65 20 74 68 nce last time th
1c77a 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 e progress callb
1c77b 61 63 6b 20 77 61 73 20 63 61 6c 6c 65 64 29 2e ack was called).
1c77c 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20 70 . ** If the p
1c77d 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 63 6b rogress callback
1c77e 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 returns non-zer
1c77f 6f 2c 20 65 78 69 74 20 74 68 65 20 76 69 72 74 o, exit the virt
1c780 75 61 6c 20 6d 61 63 68 69 6e 65 20 77 69 74 68 ual machine with
1c781 0a 20 20 20 20 2a 2a 20 61 20 72 65 74 75 72 6e . ** a return
1c782 20 63 6f 64 65 20 53 51 4c 49 54 45 5f 41 42 4f code SQLITE_ABO
1c783 52 54 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 RT.. */. i
1c784 66 28 20 64 62 2d 3e 78 50 72 6f 67 72 65 73 73 f( db->xProgress
1c785 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 64 62 ){. if( db
1c786 2d 3e 6e 50 72 6f 67 72 65 73 73 4f 70 73 3d 3d ->nProgressOps==
1c787 6e 50 72 6f 67 72 65 73 73 4f 70 73 20 29 7b 0a nProgressOps ){.
1c788 20 20 20 20 20 20 20 20 69 6e 74 20 70 72 63 3b int prc;
1c789 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c . if( sql
1c78a 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 ite3SafetyOff(db
1c78b 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 ) ) goto abort_d
1c78c 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 ue_to_misuse;.
1c78d 20 20 20 20 20 20 70 72 63 20 3d 64 62 2d 3e 78 prc =db->x
1c78e 50 72 6f 67 72 65 73 73 28 64 62 2d 3e 70 50 72 Progress(db->pPr
1c78f 6f 67 72 65 73 73 41 72 67 29 3b 0a 20 20 20 20 ogressArg);.
1c790 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 if( sqlite3S
1c791 61 66 65 74 79 4f 6e 28 64 62 29 20 29 20 67 6f afetyOn(db) ) go
1c792 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f to abort_due_to_
1c793 6d 69 73 75 73 65 3b 0a 20 20 20 20 20 20 20 20 misuse;.
1c794 69 66 28 20 70 72 63 21 3d 30 20 29 7b 0a 20 20 if( prc!=0 ){.
1c795 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c rc = SQL
1c796 49 54 45 5f 49 4e 54 45 52 52 55 50 54 3b 0a 20 ITE_INTERRUPT;.
1c797 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 76 64 goto vd
1c798 62 65 5f 65 72 72 6f 72 5f 68 61 6c 74 3b 0a 20 be_error_halt;.
1c799 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
1c79a 20 6e 50 72 6f 67 72 65 73 73 4f 70 73 20 3d 20 nProgressOps =
1c79b 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 0;. }.
1c79c 20 6e 50 72 6f 67 72 65 73 73 4f 70 73 2b 2b 3b nProgressOps++;
1c79d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 . }.#endif..
1c79e 20 20 20 2f 2a 20 44 6f 20 63 6f 6d 6d 6f 6e 20 /* Do common
1c79f 73 65 74 75 70 20 70 72 6f 63 65 73 73 69 6e 67 setup processing
1c7a0 20 66 6f 72 20 61 6e 79 20 6f 70 63 6f 64 65 20 for any opcode
1c7a1 74 68 61 74 20 69 73 20 6d 61 72 6b 65 64 0a 20 that is marked.
1c7a2 20 20 20 2a 2a 20 77 69 74 68 20 74 68 65 20 22 ** with the "
1c7a3 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 22 out2-prerelease"
1c7a4 20 74 61 67 2e 20 20 53 75 63 68 20 6f 70 63 6f tag. Such opco
1c7a5 64 65 73 20 68 61 76 65 20 61 20 73 69 6e 67 6c des have a singl
1c7a6 65 0a 20 20 20 20 2a 2a 20 6f 75 74 70 75 74 20 e. ** output
1c7a7 77 68 69 63 68 20 69 73 20 73 70 65 63 69 66 69 which is specifi
1c7a8 65 64 20 62 79 20 74 68 65 20 50 32 20 70 61 72 ed by the P2 par
1c7a9 61 6d 65 74 65 72 2e 20 20 54 68 65 20 50 32 20 ameter. The P2
1c7aa 72 65 67 69 73 74 65 72 0a 20 20 20 20 2a 2a 20 register. **
1c7ab 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 is initialized t
1c7ac 6f 20 61 20 4e 55 4c 4c 2e 0a 20 20 20 20 2a 2f o a NULL.. */
1c7ad 0a 20 20 20 20 6f 70 50 72 6f 70 65 72 74 79 20 . opProperty
1c7ae 3d 20 6f 70 63 6f 64 65 50 72 6f 70 65 72 74 79 = opcodeProperty
1c7af 5b 70 4f 70 2d 3e 6f 70 63 6f 64 65 5d 3b 0a 20 [pOp->opcode];.
1c7b0 20 20 20 69 66 28 20 28 6f 70 50 72 6f 70 65 72 if( (opProper
1c7b1 74 79 20 26 20 4f 50 46 4c 47 5f 4f 55 54 32 5f ty & OPFLG_OUT2_
1c7b2 50 52 45 52 45 4c 45 41 53 45 29 21 3d 30 20 29 PRERELEASE)!=0 )
1c7b3 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 {. assert(
1c7b4 70 4f 70 2d 3e 70 32 3e 30 20 29 3b 0a 20 20 20 pOp->p2>0 );.
1c7b5 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e assert( pOp->
1c7b6 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 p2<=p->nMem );.
1c7b7 20 20 20 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e pOut = &p->
1c7b8 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 aMem[pOp->p2];.
1c7b9 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1c7ba 4d 65 6d 52 65 6c 65 61 73 65 45 78 74 65 72 6e MemReleaseExtern
1c7bb 61 6c 28 70 4f 75 74 29 3b 0a 20 20 20 20 20 20 al(pOut);.
1c7bc 70 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 pOut->flags = ME
1c7bd 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 20 20 70 4f M_Null;. pO
1c7be 75 74 2d 3e 6e 20 3d 20 30 3b 0a 20 20 20 20 7d ut->n = 0;. }
1c7bf 65 6c 73 65 0a 20 0a 20 20 20 20 2f 2a 20 44 6f else. . /* Do
1c7c0 20 63 6f 6d 6d 6f 6e 20 73 65 74 75 70 20 66 6f common setup fo
1c7c1 72 20 6f 70 63 6f 64 65 73 20 6d 61 72 6b 65 64 r opcodes marked
1c7c2 20 77 69 74 68 20 6f 6e 65 20 6f 66 20 74 68 65 with one of the
1c7c3 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 20 20 2a following. *
1c7c4 2a 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 73 20 6f * combinations o
1c7c5 66 20 70 72 6f 70 65 72 74 69 65 73 2e 0a 20 20 f properties..
1c7c6 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 **. **
1c7c7 20 20 20 20 20 20 69 6e 31 0a 20 20 20 20 2a 2a in1. **
1c7c8 20 20 20 20 20 20 20 20 20 20 20 69 6e 31 20 69 in1 i
1c7c9 6e 32 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 n2. **
1c7ca 20 20 20 20 69 6e 31 20 69 6e 32 20 6f 75 74 33 in1 in2 out3
1c7cb 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 . **
1c7cc 20 20 69 6e 31 20 69 6e 33 0a 20 20 20 20 2a 2a in1 in3. **
1c7cd 0a 20 20 20 20 2a 2a 20 56 61 72 69 61 62 6c 65 . ** Variable
1c7ce 73 20 70 49 6e 31 2c 20 70 49 6e 32 2c 20 61 6e s pIn1, pIn2, an
1c7cf 64 20 70 49 6e 33 20 61 72 65 20 6d 61 64 65 20 d pIn3 are made
1c7d0 74 6f 20 70 6f 69 6e 74 20 74 6f 20 61 70 70 72 to point to appr
1c7d1 6f 70 72 69 61 74 65 0a 20 20 20 20 2a 2a 20 72 opriate. ** r
1c7d2 65 67 69 73 74 65 72 73 20 66 6f 72 20 69 6e 70 egisters for inp
1c7d3 75 74 73 2e 20 20 56 61 72 69 61 62 6c 65 20 70 uts. Variable p
1c7d4 4f 75 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 Out points to th
1c7d5 65 20 6f 75 74 70 75 74 20 72 65 67 69 73 74 65 e output registe
1c7d6 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 r.. */. if
1c7d7 28 20 28 6f 70 50 72 6f 70 65 72 74 79 20 26 20 ( (opProperty &
1c7d8 4f 50 46 4c 47 5f 49 4e 31 29 21 3d 30 20 29 7b OPFLG_IN1)!=0 ){
1c7d9 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
1c7da 4f 70 2d 3e 70 31 3e 30 20 29 3b 0a 20 20 20 20 Op->p1>0 );.
1c7db 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
1c7dc 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 1<=p->nMem );.
1c7dd 20 20 20 20 70 49 6e 31 20 3d 20 26 70 2d 3e 61 pIn1 = &p->a
1c7de 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 Mem[pOp->p1];.
1c7df 20 20 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 REGISTER_TRA
1c7e0 43 45 28 70 4f 70 2d 3e 70 31 2c 20 70 49 6e 31 CE(pOp->p1, pIn1
1c7e1 29 3b 0a 20 20 20 20 20 20 69 66 28 20 28 6f 70 );. if( (op
1c7e2 50 72 6f 70 65 72 74 79 20 26 20 4f 50 46 4c 47 Property & OPFLG
1c7e3 5f 49 4e 32 29 21 3d 30 20 29 7b 0a 20 20 20 20 _IN2)!=0 ){.
1c7e4 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d assert( pOp-
1c7e5 3e 70 32 3e 30 20 29 3b 0a 20 20 20 20 20 20 20 >p2>0 );.
1c7e6 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 assert( pOp->p2
1c7e7 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 <=p->nMem );.
1c7e8 20 20 20 20 20 70 49 6e 32 20 3d 20 26 70 2d 3e pIn2 = &p->
1c7e9 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 aMem[pOp->p2];.
1c7ea 20 20 20 20 20 20 20 52 45 47 49 53 54 45 52 5f REGISTER_
1c7eb 54 52 41 43 45 28 70 4f 70 2d 3e 70 32 2c 20 70 TRACE(pOp->p2, p
1c7ec 49 6e 32 29 3b 0a 20 20 20 20 20 20 20 20 2f 2a In2);. /*
1c7ed 20 41 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6d As currently im
1c7ee 70 6c 65 6d 65 6e 74 65 64 2c 20 69 6e 32 20 69 plemented, in2 i
1c7ef 6d 70 6c 69 65 73 20 6f 75 74 33 2e 20 20 54 68 mplies out3. Th
1c7f0 65 72 65 20 69 73 20 6e 6f 20 72 65 61 73 6f 6e ere is no reason
1c7f1 0a 20 20 20 20 20 20 20 20 2a 2a 20 77 68 79 20 . ** why
1c7f2 74 68 69 73 20 68 61 73 20 74 6f 20 62 65 2c 20 this has to be,
1c7f3 69 74 20 6a 75 73 74 20 77 6f 72 6b 65 64 20 6f it just worked o
1c7f4 75 74 20 74 68 61 74 20 77 61 79 2e 20 2a 2f 0a ut that way. */.
1c7f5 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
1c7f6 28 6f 70 50 72 6f 70 65 72 74 79 20 26 20 4f 50 (opProperty & OP
1c7f7 46 4c 47 5f 4f 55 54 33 29 21 3d 30 20 29 3b 0a FLG_OUT3)!=0 );.
1c7f8 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
1c7f9 70 4f 70 2d 3e 70 33 3e 30 20 29 3b 0a 20 20 20 pOp->p3>0 );.
1c7fa 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 assert( pOp
1c7fb 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b ->p3<=p->nMem );
1c7fc 0a 20 20 20 20 20 20 20 20 70 4f 75 74 20 3d 20 . pOut =
1c7fd 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 &p->aMem[pOp->p3
1c7fe 5d 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 ];. }else i
1c7ff 66 28 20 28 6f 70 50 72 6f 70 65 72 74 79 20 26 f( (opProperty &
1c800 20 4f 50 46 4c 47 5f 49 4e 33 29 21 3d 30 20 29 OPFLG_IN3)!=0 )
1c801 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 {. assert
1c802 28 20 70 4f 70 2d 3e 70 33 3e 30 20 29 3b 0a 20 ( pOp->p3>0 );.
1c803 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 assert( p
1c804 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 Op->p3<=p->nMem
1c805 29 3b 0a 20 20 20 20 20 20 20 20 70 49 6e 33 20 );. pIn3
1c806 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e = &p->aMem[pOp->
1c807 70 33 5d 3b 0a 20 20 20 20 20 20 20 20 52 45 47 p3];. REG
1c808 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d ISTER_TRACE(pOp-
1c809 3e 70 33 2c 20 70 49 6e 33 29 3b 0a 20 20 20 20 >p3, pIn3);.
1c80a 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 }. }else if
1c80b 28 20 28 6f 70 50 72 6f 70 65 72 74 79 20 26 20 ( (opProperty &
1c80c 4f 50 46 4c 47 5f 49 4e 32 29 21 3d 30 20 29 7b OPFLG_IN2)!=0 ){
1c80d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
1c80e 4f 70 2d 3e 70 32 3e 30 20 29 3b 0a 20 20 20 20 Op->p2>0 );.
1c80f 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
1c810 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 2<=p->nMem );.
1c811 20 20 20 20 70 49 6e 32 20 3d 20 26 70 2d 3e 61 pIn2 = &p->a
1c812 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 Mem[pOp->p2];.
1c813 20 20 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 REGISTER_TRA
1c814 43 45 28 70 4f 70 2d 3e 70 32 2c 20 70 49 6e 32 CE(pOp->p2, pIn2
1c815 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 );. }else if(
1c816 20 28 6f 70 50 72 6f 70 65 72 74 79 20 26 20 4f (opProperty & O
1c817 50 46 4c 47 5f 49 4e 33 29 21 3d 30 20 29 7b 0a PFLG_IN3)!=0 ){.
1c818 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f assert( pO
1c819 70 2d 3e 70 33 3e 30 20 29 3b 0a 20 20 20 20 20 p->p3>0 );.
1c81a 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 assert( pOp->p3
1c81b 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 <=p->nMem );.
1c81c 20 20 20 70 49 6e 33 20 3d 20 26 70 2d 3e 61 4d pIn3 = &p->aM
1c81d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20 em[pOp->p3];.
1c81e 20 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 REGISTER_TRAC
1c81f 45 28 70 4f 70 2d 3e 70 33 2c 20 70 49 6e 33 29 E(pOp->p3, pIn3)
1c820 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73 77 69 ;. }.. swi
1c821 74 63 68 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 tch( pOp->opcode
1c822 20 29 7b 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ){../**********
1c823 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c824 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c825 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c826 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c827 2a 2a 2a 0a 2a 2a 20 57 68 61 74 20 66 6f 6c 6c ***.** What foll
1c828 6f 77 73 20 69 73 20 61 20 6d 61 73 73 69 76 65 ows is a massive
1c829 20 73 77 69 74 63 68 20 73 74 61 74 65 6d 65 6e switch statemen
1c82a 74 20 77 68 65 72 65 20 65 61 63 68 20 63 61 73 t where each cas
1c82b 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 0a 2a e implements a.*
1c82c 2a 20 73 65 70 61 72 61 74 65 20 69 6e 73 74 72 * separate instr
1c82d 75 63 74 69 6f 6e 20 69 6e 20 74 68 65 20 76 69 uction in the vi
1c82e 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e 20 20 rtual machine.
1c82f 49 66 20 77 65 20 66 6f 6c 6c 6f 77 20 74 68 65 If we follow the
1c830 20 75 73 75 61 6c 0a 2a 2a 20 69 6e 64 65 6e 74 usual.** indent
1c831 61 74 69 6f 6e 20 63 6f 6e 76 65 6e 74 69 6f 6e ation convention
1c832 73 2c 20 65 61 63 68 20 63 61 73 65 20 73 68 6f s, each case sho
1c833 75 6c 64 20 62 65 20 69 6e 64 65 6e 74 65 64 20 uld be indented
1c834 62 79 20 36 20 73 70 61 63 65 73 2e 20 20 42 75 by 6 spaces. Bu
1c835 74 0a 2a 2a 20 74 68 61 74 20 69 73 20 61 20 6c t.** that is a l
1c836 6f 74 20 6f 66 20 77 61 73 74 65 64 20 73 70 61 ot of wasted spa
1c837 63 65 20 6f 6e 20 74 68 65 20 6c 65 66 74 20 6d ce on the left m
1c838 61 72 67 69 6e 2e 20 20 53 6f 20 74 68 65 20 63 argin. So the c
1c839 6f 64 65 20 77 69 74 68 69 6e 0a 2a 2a 20 74 68 ode within.** th
1c83a 65 20 73 77 69 74 63 68 20 73 74 61 74 65 6d 65 e switch stateme
1c83b 6e 74 20 77 69 6c 6c 20 62 72 65 61 6b 20 77 69 nt will break wi
1c83c 74 68 20 63 6f 6e 76 65 6e 74 69 6f 6e 20 61 6e th convention an
1c83d 64 20 62 65 20 66 6c 75 73 68 2d 6c 65 66 74 2e d be flush-left.
1c83e 20 41 6e 6f 74 68 65 72 0a 2a 2a 20 62 69 67 20 Another.** big
1c83f 63 6f 6d 6d 65 6e 74 20 28 73 69 6d 69 6c 61 72 comment (similar
1c840 20 74 6f 20 74 68 69 73 20 6f 6e 65 29 20 77 69 to this one) wi
1c841 6c 6c 20 6d 61 72 6b 20 74 68 65 20 70 6f 69 6e ll mark the poin
1c842 74 20 69 6e 20 74 68 65 20 63 6f 64 65 20 77 68 t in the code wh
1c843 65 72 65 0a 2a 2a 20 77 65 20 74 72 61 6e 73 69 ere.** we transi
1c844 74 69 6f 6e 20 62 61 63 6b 20 74 6f 20 6e 6f 72 tion back to nor
1c845 6d 61 6c 20 69 6e 64 65 6e 74 61 74 69 6f 6e 2e mal indentation.
1c846 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 72 6d 61 .**.** The forma
1c847 74 74 69 6e 67 20 6f 66 20 65 61 63 68 20 63 61 tting of each ca
1c848 73 65 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 2e se is important.
1c849 20 20 54 68 65 20 6d 61 6b 65 66 69 6c 65 20 66 The makefile f
1c84a 6f 72 20 53 51 4c 69 74 65 0a 2a 2a 20 67 65 6e or SQLite.** gen
1c84b 65 72 61 74 65 73 20 74 77 6f 20 43 20 66 69 6c erates two C fil
1c84c 65 73 20 22 6f 70 63 6f 64 65 73 2e 68 22 20 61 es "opcodes.h" a
1c84d 6e 64 20 22 6f 70 63 6f 64 65 73 2e 63 22 20 62 nd "opcodes.c" b
1c84e 79 20 73 63 61 6e 6e 69 6e 67 20 74 68 69 73 0a y scanning this.
1c84f 2a 2a 20 66 69 6c 65 20 6c 6f 6f 6b 69 6e 67 20 ** file looking
1c850 66 6f 72 20 6c 69 6e 65 73 20 74 68 61 74 20 62 for lines that b
1c851 65 67 69 6e 20 77 69 74 68 20 22 63 61 73 65 20 egin with "case
1c852 4f 50 5f 22 2e 20 20 54 68 65 20 6f 70 63 6f 64 OP_". The opcod
1c853 65 73 2e 68 20 66 69 6c 65 73 0a 2a 2a 20 77 69 es.h files.** wi
1c854 6c 6c 20 62 65 20 66 69 6c 6c 65 64 20 77 69 74 ll be filled wit
1c855 68 20 23 64 65 66 69 6e 65 73 20 74 68 61 74 20 h #defines that
1c856 67 69 76 65 20 75 6e 69 71 75 65 20 69 6e 74 65 give unique inte
1c857 67 65 72 20 76 61 6c 75 65 73 20 74 6f 20 65 61 ger values to ea
1c858 63 68 0a 2a 2a 20 6f 70 63 6f 64 65 20 61 6e 64 ch.** opcode and
1c859 20 74 68 65 20 6f 70 63 6f 64 65 73 2e 63 20 66 the opcodes.c f
1c85a 69 6c 65 20 69 73 20 66 69 6c 6c 65 64 20 77 69 ile is filled wi
1c85b 74 68 20 61 6e 20 61 72 72 61 79 20 6f 66 20 73 th an array of s
1c85c 74 72 69 6e 67 73 20 77 68 65 72 65 0a 2a 2a 20 trings where.**
1c85d 65 61 63 68 20 73 74 72 69 6e 67 20 69 73 20 74 each string is t
1c85e 68 65 20 73 79 6d 62 6f 6c 69 63 20 6e 61 6d 65 he symbolic name
1c85f 20 66 6f 72 20 74 68 65 20 63 6f 72 72 65 73 70 for the corresp
1c860 6f 6e 64 69 6e 67 20 6f 70 63 6f 64 65 2e 20 20 onding opcode.
1c861 49 66 20 74 68 65 0a 2a 2a 20 63 61 73 65 20 73 If the.** case s
1c862 74 61 74 65 6d 65 6e 74 20 69 73 20 66 6f 6c 6c tatement is foll
1c863 6f 77 65 64 20 62 79 20 61 20 63 6f 6d 6d 65 6e owed by a commen
1c864 74 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 2f t of the form "/
1c865 23 20 73 61 6d 65 20 61 73 20 2e 2e 2e 20 23 2f # same as ... #/
1c866 22 0a 2a 2a 20 74 68 61 74 20 63 6f 6d 6d 65 6e ".** that commen
1c867 74 20 69 73 20 75 73 65 64 20 74 6f 20 64 65 74 t is used to det
1c868 65 72 6d 69 6e 65 20 74 68 65 20 70 61 72 74 69 ermine the parti
1c869 63 75 6c 61 72 20 76 61 6c 75 65 20 6f 66 20 74 cular value of t
1c86a 68 65 20 6f 70 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a he opcode..**.**
1c86b 20 4f 74 68 65 72 20 6b 65 79 77 6f 72 64 73 20 Other keywords
1c86c 69 6e 20 74 68 65 20 63 6f 6d 6d 65 6e 74 20 74 in the comment t
1c86d 68 61 74 20 66 6f 6c 6c 6f 77 73 20 65 61 63 68 hat follows each
1c86e 20 63 61 73 65 20 61 72 65 20 75 73 65 64 20 74 case are used t
1c86f 6f 0a 2a 2a 20 63 6f 6e 73 74 72 75 63 74 20 74 o.** construct t
1c870 68 65 20 4f 50 46 4c 47 5f 49 4e 49 54 49 41 4c he OPFLG_INITIAL
1c871 49 5a 45 52 20 76 61 6c 75 65 20 74 68 61 74 20 IZER value that
1c872 69 6e 69 74 69 61 6c 69 7a 65 73 20 6f 70 63 6f initializes opco
1c873 64 65 50 72 6f 70 65 72 74 79 5b 5d 2e 0a 2a 2a deProperty[]..**
1c874 20 4b 65 79 77 6f 72 64 73 20 69 6e 63 6c 75 64 Keywords includ
1c875 65 3a 20 69 6e 31 2c 20 69 6e 32 2c 20 69 6e 33 e: in1, in2, in3
1c876 2c 20 6f 75 74 32 5f 70 72 65 72 65 6c 65 61 73 , out2_prereleas
1c877 65 2c 20 6f 75 74 32 2c 20 6f 75 74 33 2e 20 20 e, out2, out3.
1c878 53 65 65 0a 2a 2a 20 74 68 65 20 6d 6b 6f 70 63 See.** the mkopc
1c879 6f 64 65 68 2e 61 77 6b 20 73 63 72 69 70 74 20 odeh.awk script
1c87a 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 for additional i
1c87b 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a nformation..**.*
1c87c 2a 20 44 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 * Documentation
1c87d 61 62 6f 75 74 20 56 44 42 45 20 6f 70 63 6f 64 about VDBE opcod
1c87e 65 73 20 69 73 20 67 65 6e 65 72 61 74 65 64 20 es is generated
1c87f 62 79 20 73 63 61 6e 6e 69 6e 67 20 74 68 69 73 by scanning this
1c880 20 66 69 6c 65 0a 2a 2a 20 66 6f 72 20 6c 69 6e file.** for lin
1c881 65 73 20 6f 66 20 74 68 61 74 20 63 6f 6e 74 61 es of that conta
1c882 69 6e 20 22 4f 70 63 6f 64 65 3a 22 2e 20 20 54 in "Opcode:". T
1c883 68 61 74 20 6c 69 6e 65 20 61 6e 64 20 61 6c 6c hat line and all
1c884 20 73 75 62 73 65 71 75 65 6e 74 0a 2a 2a 20 63 subsequent.** c
1c885 6f 6d 6d 65 6e 74 20 6c 69 6e 65 73 20 61 72 65 omment lines are
1c886 20 75 73 65 64 20 69 6e 20 74 68 65 20 67 65 6e used in the gen
1c887 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6f eration of the o
1c888 70 63 6f 64 65 2e 68 74 6d 6c 20 64 6f 63 75 6d pcode.html docum
1c889 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 66 69 6c 65 entation.** file
1c88a 2e 0a 2a 2a 0a 2a 2a 20 53 55 4d 4d 41 52 59 3a ..**.** SUMMARY:
1c88b 0a 2a 2a 0a 2a 2a 20 20 20 20 20 46 6f 72 6d 61 .**.** Forma
1c88c 74 74 69 6e 67 20 69 73 20 69 6d 70 6f 72 74 61 tting is importa
1c88d 6e 74 20 74 6f 20 73 63 72 69 70 74 73 20 74 68 nt to scripts th
1c88e 61 74 20 73 63 61 6e 20 74 68 69 73 20 66 69 6c at scan this fil
1c88f 65 2e 0a 2a 2a 20 20 20 20 20 44 6f 20 6e 6f 74 e..** Do not
1c890 20 64 65 76 69 61 74 65 20 66 72 6f 6d 20 74 68 deviate from th
1c891 65 20 66 6f 72 6d 61 74 74 69 6e 67 20 73 74 79 e formatting sty
1c892 6c 65 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 le currently in
1c893 75 73 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a use..**.********
1c894 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c895 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c896 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c897 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c898 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f 64 *****/../* Opcod
1c899 65 3a 20 20 47 6f 74 6f 20 2a 20 50 32 20 2a 20 e: Goto * P2 *
1c89a 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41 6e 20 75 6e 63 * *.**.** An unc
1c89b 6f 6e 64 69 74 69 6f 6e 61 6c 20 6a 75 6d 70 20 onditional jump
1c89c 74 6f 20 61 64 64 72 65 73 73 20 50 32 2e 0a 2a to address P2..*
1c89d 2a 20 54 68 65 20 6e 65 78 74 20 69 6e 73 74 72 * The next instr
1c89e 75 63 74 69 6f 6e 20 65 78 65 63 75 74 65 64 20 uction executed
1c89f 77 69 6c 6c 20 62 65 20 0a 2a 2a 20 74 68 65 20 will be .** the
1c8a0 6f 6e 65 20 61 74 20 69 6e 64 65 78 20 50 32 20 one at index P2
1c8a1 66 72 6f 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 from the beginni
1c8a2 6e 67 20 6f 66 0a 2a 2a 20 74 68 65 20 70 72 6f ng of.** the pro
1c8a3 67 72 61 6d 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 gram..*/.case OP
1c8a4 5f 47 6f 74 6f 3a 20 7b 20 20 20 20 20 20 20 20 _Goto: {
1c8a5 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a /* jump */.
1c8a6 20 20 43 48 45 43 4b 5f 46 4f 52 5f 49 4e 54 45 CHECK_FOR_INTE
1c8a7 52 52 55 50 54 3b 0a 20 20 70 63 20 3d 20 70 4f RRUPT;. pc = pO
1c8a8 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 62 72 65 p->p2 - 1;. bre
1c8a9 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 ak;.}../* Opcode
1c8aa 3a 20 20 47 6f 73 75 62 20 50 31 20 50 32 20 2a : Gosub P1 P2 *
1c8ab 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 * *.**.** Write
1c8ac 20 74 68 65 20 63 75 72 72 65 6e 74 20 61 64 64 the current add
1c8ad 72 65 73 73 20 6f 6e 74 6f 20 72 65 67 69 73 74 ress onto regist
1c8ae 65 72 20 50 31 0a 2a 2a 20 61 6e 64 20 74 68 65 er P1.** and the
1c8af 6e 20 6a 75 6d 70 20 74 6f 20 61 64 64 72 65 73 n jump to addres
1c8b0 73 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 s P2..*/.case OP
1c8b1 5f 47 6f 73 75 62 3a 20 7b 20 20 20 20 20 20 20 _Gosub: {
1c8b2 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a /* jump */.
1c8b3 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
1c8b4 31 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 1>0 );. assert(
1c8b5 20 70 4f 70 2d 3e 70 31 3c 3d 70 2d 3e 6e 4d 65 pOp->p1<=p->nMe
1c8b6 6d 20 29 3b 0a 20 20 70 49 6e 31 20 3d 20 26 70 m );. pIn1 = &p
1c8b7 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b ->aMem[pOp->p1];
1c8b8 0a 20 20 61 73 73 65 72 74 28 20 28 70 49 6e 31 . assert( (pIn1
1c8b9 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 44 79 ->flags & MEM_Dy
1c8ba 6e 29 3d 3d 30 20 29 3b 0a 20 20 70 49 6e 31 2d n)==0 );. pIn1-
1c8bb 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 >flags = MEM_Int
1c8bc 3b 0a 20 20 70 49 6e 31 2d 3e 75 2e 69 20 3d 20 ;. pIn1->u.i =
1c8bd 70 63 3b 0a 20 20 52 45 47 49 53 54 45 52 5f 54 pc;. REGISTER_T
1c8be 52 41 43 45 28 70 4f 70 2d 3e 70 31 2c 20 70 49 RACE(pOp->p1, pI
1c8bf 6e 31 29 3b 0a 20 20 70 63 20 3d 20 70 4f 70 2d n1);. pc = pOp-
1c8c0 3e 70 32 20 2d 20 31 3b 0a 20 20 62 72 65 61 6b >p2 - 1;. break
1c8c1 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ;.}../* Opcode:
1c8c2 20 52 65 74 75 72 6e 20 50 31 20 2a 20 2a 20 2a Return P1 * * *
1c8c3 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f *.**.** Jump to
1c8c4 20 74 68 65 20 6e 65 78 74 20 69 6e 73 74 72 75 the next instru
1c8c5 63 74 69 6f 6e 20 61 66 74 65 72 20 74 68 65 20 ction after the
1c8c6 61 64 64 72 65 73 73 20 69 6e 20 72 65 67 69 73 address in regis
1c8c7 74 65 72 20 50 31 2e 0a 2a 2f 0a 63 61 73 65 20 ter P1..*/.case
1c8c8 4f 50 5f 52 65 74 75 72 6e 3a 20 7b 20 20 20 20 OP_Return: {
1c8c9 20 20 20 20 20 20 20 2f 2a 20 69 6e 31 20 2a 2f /* in1 */
1c8ca 0a 20 20 61 73 73 65 72 74 28 20 70 49 6e 31 2d . assert( pIn1-
1c8cb 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 >flags & MEM_Int
1c8cc 20 29 3b 0a 20 20 70 63 20 3d 20 28 69 6e 74 29 );. pc = (int)
1c8cd 70 49 6e 31 2d 3e 75 2e 69 3b 0a 20 20 62 72 65 pIn1->u.i;. bre
1c8ce 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 ak;.}../* Opcode
1c8cf 3a 20 20 59 69 65 6c 64 20 50 31 20 2a 20 2a 20 : Yield P1 * *
1c8d0 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 77 61 70 20 74 * *.**.** Swap t
1c8d1 68 65 20 70 72 6f 67 72 61 6d 20 63 6f 75 6e 74 he program count
1c8d2 65 72 20 77 69 74 68 20 74 68 65 20 76 61 6c 75 er with the valu
1c8d3 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 e in register P1
1c8d4 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 59 69 65 ..*/.case OP_Yie
1c8d5 6c 64 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 ld: {
1c8d6 20 2f 2a 20 69 6e 31 20 2a 2f 0a 23 69 66 20 30 /* in1 */.#if 0
1c8d7 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 /* local varia
1c8d8 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 bles moved into
1c8d9 75 2e 61 61 20 2a 2f 0a 20 20 69 6e 74 20 70 63 u.aa */. int pc
1c8da 44 65 73 74 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 Dest;.#endif /*
1c8db 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 local variables
1c8dc 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 61 20 moved into u.aa
1c8dd 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 28 70 49 */. assert( (pI
1c8de 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f n1->flags & MEM_
1c8df 44 79 6e 29 3d 3d 30 20 29 3b 0a 20 20 70 49 6e Dyn)==0 );. pIn
1c8e0 31 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 1->flags = MEM_I
1c8e1 6e 74 3b 0a 20 20 75 2e 61 61 2e 70 63 44 65 73 nt;. u.aa.pcDes
1c8e2 74 20 3d 20 28 69 6e 74 29 70 49 6e 31 2d 3e 75 t = (int)pIn1->u
1c8e3 2e 69 3b 0a 20 20 70 49 6e 31 2d 3e 75 2e 69 20 .i;. pIn1->u.i
1c8e4 3d 20 70 63 3b 0a 20 20 52 45 47 49 53 54 45 52 = pc;. REGISTER
1c8e5 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 31 2c 20 _TRACE(pOp->p1,
1c8e6 70 49 6e 31 29 3b 0a 20 20 70 63 20 3d 20 75 2e pIn1);. pc = u.
1c8e7 61 61 2e 70 63 44 65 73 74 3b 0a 20 20 62 72 65 aa.pcDest;. bre
1c8e8 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 ak;.}../* Opcode
1c8e9 3a 20 20 48 61 6c 74 49 66 4e 75 6c 6c 20 20 50 : HaltIfNull P
1c8ea 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 1 P2 P3 P4 *.**.
1c8eb 2a 2a 20 43 68 65 63 6b 20 74 68 65 20 76 61 6c ** Check the val
1c8ec 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 ue in register P
1c8ed 33 2e 20 20 49 66 20 69 73 20 69 73 20 4e 55 4c 3. If is is NUL
1c8ee 4c 20 74 68 65 6e 20 48 61 6c 74 20 75 73 69 6e L then Halt usin
1c8ef 67 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 50 g.** parameter P
1c8f0 31 2c 20 50 32 2c 20 61 6e 64 20 50 34 20 61 73 1, P2, and P4 as
1c8f1 20 69 66 20 74 68 69 73 20 77 65 72 65 20 61 20 if this were a
1c8f2 48 61 6c 74 20 69 6e 73 74 72 75 63 74 69 6f 6e Halt instruction
1c8f3 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20 76 61 6c . If the.** val
1c8f4 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 ue in register P
1c8f5 33 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 3 is not NULL, t
1c8f6 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 hen this routine
1c8f7 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a is a no-op..*/.
1c8f8 63 61 73 65 20 4f 50 5f 48 61 6c 74 49 66 4e 75 case OP_HaltIfNu
1c8f9 6c 6c 3a 20 7b 20 20 20 20 20 20 2f 2a 20 69 6e ll: { /* in
1c8fa 33 20 2a 2f 0a 20 20 69 66 28 20 28 70 49 6e 33 3 */. if( (pIn3
1c8fb 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 ->flags & MEM_Nu
1c8fc 6c 6c 29 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a ll)==0 ) break;.
1c8fd 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72 6f 75 67 /* Fall throug
1c8fe 68 20 69 6e 74 6f 20 4f 50 5f 48 61 6c 74 20 2a h into OP_Halt *
1c8ff 2f 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 /.}../* Opcode:
1c900 20 48 61 6c 74 20 50 31 20 50 32 20 2a 20 50 34 Halt P1 P2 * P4
1c901 20 2a 0a 2a 2a 0a 2a 2a 20 45 78 69 74 20 69 6d *.**.** Exit im
1c902 6d 65 64 69 61 74 65 6c 79 2e 20 20 41 6c 6c 20 mediately. All
1c903 6f 70 65 6e 20 63 75 72 73 6f 72 73 2c 20 65 74 open cursors, et
1c904 63 20 61 72 65 20 63 6c 6f 73 65 64 0a 2a 2a 20 c are closed.**
1c905 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 2e 0a 2a automatically..*
1c906 2a 0a 2a 2a 20 50 31 20 69 73 20 74 68 65 20 72 *.** P1 is the r
1c907 65 73 75 6c 74 20 63 6f 64 65 20 72 65 74 75 72 esult code retur
1c908 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 65 ned by sqlite3_e
1c909 78 65 63 28 29 2c 20 73 71 6c 69 74 65 33 5f 72 xec(), sqlite3_r
1c90a 65 73 65 74 28 29 2c 0a 2a 2a 20 6f 72 20 73 71 eset(),.** or sq
1c90b 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 lite3_finalize()
1c90c 2e 20 20 46 6f 72 20 61 20 6e 6f 72 6d 61 6c 20 . For a normal
1c90d 68 61 6c 74 2c 20 74 68 69 73 20 73 68 6f 75 6c halt, this shoul
1c90e 64 20 62 65 20 53 51 4c 49 54 45 5f 4f 4b 20 28 d be SQLITE_OK (
1c90f 30 29 2e 0a 2a 2a 20 46 6f 72 20 65 72 72 6f 72 0)..** For error
1c910 73 2c 20 69 74 20 63 61 6e 20 62 65 20 73 6f 6d s, it can be som
1c911 65 20 6f 74 68 65 72 20 76 61 6c 75 65 2e 20 20 e other value.
1c912 49 66 20 50 31 21 3d 30 20 74 68 65 6e 20 50 32 If P1!=0 then P2
1c913 20 77 69 6c 6c 20 64 65 74 65 72 6d 69 6e 65 0a will determine.
1c914 2a 2a 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f ** whether or no
1c915 74 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 74 68 t to rollback th
1c916 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 e current transa
1c917 63 74 69 6f 6e 2e 20 20 44 6f 20 6e 6f 74 20 72 ction. Do not r
1c918 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 69 66 20 50 32 ollback.** if P2
1c919 3d 3d 4f 45 5f 46 61 69 6c 2e 20 44 6f 20 74 68 ==OE_Fail. Do th
1c91a 65 20 72 6f 6c 6c 62 61 63 6b 20 69 66 20 50 32 e rollback if P2
1c91b 3d 3d 4f 45 5f 52 6f 6c 6c 62 61 63 6b 2e 20 20 ==OE_Rollback.
1c91c 49 66 20 50 32 3d 3d 4f 45 5f 41 62 6f 72 74 2c If P2==OE_Abort,
1c91d 0a 2a 2a 20 74 68 65 6e 20 62 61 63 6b 20 6f 75 .** then back ou
1c91e 74 20 61 6c 6c 20 63 68 61 6e 67 65 73 20 74 68 t all changes th
1c91f 61 74 20 68 61 76 65 20 6f 63 63 75 72 72 65 64 at have occurred
1c920 20 64 75 72 69 6e 67 20 74 68 69 73 20 65 78 65 during this exe
1c921 63 75 74 69 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a cution of the.**
1c922 20 56 44 42 45 2c 20 62 75 74 20 64 6f 20 6e 6f VDBE, but do no
1c923 74 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20 74 t rollback the t
1c924 72 61 6e 73 61 63 74 69 6f 6e 2e 20 0a 2a 2a 0a ransaction. .**.
1c925 2a 2a 20 49 66 20 50 34 20 69 73 20 6e 6f 74 20 ** If P4 is not
1c926 6e 75 6c 6c 20 74 68 65 6e 20 69 74 20 69 73 20 null then it is
1c927 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 an error message
1c928 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 string..**.** T
1c929 68 65 72 65 20 69 73 20 61 6e 20 69 6d 70 6c 69 here is an impli
1c92a 65 64 20 22 48 61 6c 74 20 30 20 30 20 30 22 20 ed "Halt 0 0 0"
1c92b 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 6e 73 65 instruction inse
1c92c 72 74 65 64 20 61 74 20 74 68 65 20 76 65 72 79 rted at the very
1c92d 20 65 6e 64 20 6f 66 0a 2a 2a 20 65 76 65 72 79 end of.** every
1c92e 20 70 72 6f 67 72 61 6d 2e 20 20 53 6f 20 61 20 program. So a
1c92f 6a 75 6d 70 20 70 61 73 74 20 74 68 65 20 6c 61 jump past the la
1c930 73 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 6f st instruction o
1c931 66 20 74 68 65 20 70 72 6f 67 72 61 6d 0a 2a 2a f the program.**
1c932 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 is the same as
1c933 65 78 65 63 75 74 69 6e 67 20 48 61 6c 74 2e 0a executing Halt..
1c934 2a 2f 0a 63 61 73 65 20 4f 50 5f 48 61 6c 74 3a */.case OP_Halt:
1c935 20 7b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 31 {. if( pOp->p1
1c936 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 ==SQLITE_OK && p
1c937 2d 3e 70 46 72 61 6d 65 20 29 7b 0a 20 20 20 20 ->pFrame ){.
1c938 2f 2a 20 48 61 6c 74 20 74 68 65 20 73 75 62 2d /* Halt the sub-
1c939 70 72 6f 67 72 61 6d 2e 20 52 65 74 75 72 6e 20 program. Return
1c93a 63 6f 6e 74 72 6f 6c 20 74 6f 20 74 68 65 20 70 control to the p
1c93b 61 72 65 6e 74 20 66 72 61 6d 65 2e 20 2a 2f 0a arent frame. */.
1c93c 20 20 20 20 56 64 62 65 46 72 61 6d 65 20 2a 70 VdbeFrame *p
1c93d 46 72 61 6d 65 20 3d 20 70 2d 3e 70 46 72 61 6d Frame = p->pFram
1c93e 65 3b 0a 20 20 20 20 70 2d 3e 70 46 72 61 6d 65 e;. p->pFrame
1c93f 20 3d 20 70 46 72 61 6d 65 2d 3e 70 50 61 72 65 = pFrame->pPare
1c940 6e 74 3b 0a 20 20 20 20 70 2d 3e 6e 46 72 61 6d nt;. p->nFram
1c941 65 2d 2d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 e--;. sqlite3
1c942 56 64 62 65 53 65 74 43 68 61 6e 67 65 73 28 64 VdbeSetChanges(d
1c943 62 2c 20 70 2d 3e 6e 43 68 61 6e 67 65 29 3b 0a b, p->nChange);.
1c944 20 20 20 20 70 63 20 3d 20 73 71 6c 69 74 65 33 pc = sqlite3
1c945 56 64 62 65 46 72 61 6d 65 52 65 73 74 6f 72 65 VdbeFrameRestore
1c946 28 70 46 72 61 6d 65 29 3b 0a 20 20 20 20 69 66 (pFrame);. if
1c947 28 20 70 4f 70 2d 3e 70 32 3d 3d 4f 45 5f 49 67 ( pOp->p2==OE_Ig
1c948 6e 6f 72 65 20 29 7b 0a 20 20 20 20 20 20 2f 2a nore ){. /*
1c949 20 49 6e 73 74 72 75 63 74 69 6f 6e 20 70 63 20 Instruction pc
1c94a 69 73 20 74 68 65 20 4f 50 5f 50 72 6f 67 72 61 is the OP_Progra
1c94b 6d 20 74 68 61 74 20 69 6e 76 6f 6b 65 64 20 74 m that invoked t
1c94c 68 65 20 73 75 62 2d 70 72 6f 67 72 61 6d 20 0a he sub-program .
1c94d 20 20 20 20 20 20 2a 2a 20 63 75 72 72 65 6e 74 ** current
1c94e 6c 79 20 62 65 69 6e 67 20 68 61 6c 74 65 64 2e ly being halted.
1c94f 20 49 66 20 74 68 65 20 70 32 20 69 6e 73 74 72 If the p2 instr
1c950 75 63 74 69 6f 6e 20 6f 66 20 74 68 69 73 20 4f uction of this O
1c951 50 5f 48 61 6c 74 0a 20 20 20 20 20 20 2a 2a 20 P_Halt. **
1c952 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20 73 instruction is s
1c953 65 74 20 74 6f 20 4f 45 5f 49 67 6e 6f 72 65 2c et to OE_Ignore,
1c954 20 74 68 65 6e 20 74 68 65 20 73 75 62 2d 70 72 then the sub-pr
1c955 6f 67 72 61 6d 20 69 73 20 74 68 72 6f 77 69 6e ogram is throwin
1c956 67 0a 20 20 20 20 20 20 2a 2a 20 61 6e 20 49 47 g. ** an IG
1c957 4e 4f 52 45 20 65 78 63 65 70 74 69 6f 6e 2e 20 NORE exception.
1c958 49 6e 20 74 68 69 73 20 63 61 73 65 20 6a 75 6d In this case jum
1c959 70 20 74 6f 20 74 68 65 20 61 64 64 72 65 73 73 p to the address
1c95a 20 73 70 65 63 69 66 69 65 64 0a 20 20 20 20 20 specified.
1c95b 20 2a 2a 20 61 73 20 74 68 65 20 70 32 20 6f 66 ** as the p2 of
1c95c 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 4f 50 5f the calling OP_
1c95d 50 72 6f 67 72 61 6d 2e 20 20 2a 2f 0a 20 20 20 Program. */.
1c95e 20 20 20 70 63 20 3d 20 70 2d 3e 61 4f 70 5b 70 pc = p->aOp[p
1c95f 63 5d 2e 70 32 2d 31 3b 0a 20 20 20 20 7d 0a 20 c].p2-1;. }.
1c960 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 break;. }..
1c961 20 70 2d 3e 72 63 20 3d 20 70 4f 70 2d 3e 70 31 p->rc = pOp->p1
1c962 3b 0a 20 20 70 2d 3e 65 72 72 6f 72 41 63 74 69 ;. p->errorActi
1c963 6f 6e 20 3d 20 28 75 38 29 70 4f 70 2d 3e 70 32 on = (u8)pOp->p2
1c964 3b 0a 20 20 70 2d 3e 70 63 20 3d 20 70 63 3b 0a ;. p->pc = pc;.
1c965 20 20 69 66 28 20 70 4f 70 2d 3e 70 34 2e 7a 20 if( pOp->p4.z
1c966 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 ){. sqlite3Se
1c967 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 tString(&p->zErr
1c968 4d 73 67 2c 20 64 62 2c 20 22 25 73 22 2c 20 70 Msg, db, "%s", p
1c969 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 7d 0a 20 Op->p4.z);. }.
1c96a 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 rc = sqlite3Vdb
1c96b 65 48 61 6c 74 28 70 29 3b 0a 20 20 61 73 73 65 eHalt(p);. asse
1c96c 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42 rt( rc==SQLITE_B
1c96d 55 53 59 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 USY || rc==SQLIT
1c96e 45 5f 4f 4b 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 E_OK || rc==SQLI
1c96f 54 45 5f 45 52 52 4f 52 20 29 3b 0a 20 20 69 66 TE_ERROR );. if
1c970 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 ( rc==SQLITE_BUS
1c971 59 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d Y ){. p->rc =
1c972 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 rc = SQLITE_BUS
1c973 59 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 Y;. }else{.
1c974 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 assert( rc==SQLI
1c975 54 45 5f 4f 4b 20 7c 7c 20 70 2d 3e 72 63 3d 3d TE_OK || p->rc==
1c976 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e SQLITE_CONSTRAIN
1c977 54 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 T );. assert(
1c978 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c rc==SQLITE_OK |
1c979 7c 20 64 62 2d 3e 6e 44 65 66 65 72 72 65 64 43 | db->nDeferredC
1c97a 6f 6e 73 3e 30 20 29 3b 0a 20 20 20 20 72 63 20 ons>0 );. rc
1c97b 3d 20 70 2d 3e 72 63 20 3f 20 53 51 4c 49 54 45 = p->rc ? SQLITE
1c97c 5f 45 52 52 4f 52 20 3a 20 53 51 4c 49 54 45 5f _ERROR : SQLITE_
1c97d 44 4f 4e 45 3b 0a 20 20 7d 0a 20 20 67 6f 74 6f DONE;. }. goto
1c97e 20 76 64 62 65 5f 72 65 74 75 72 6e 3b 0a 7d 0a vdbe_return;.}.
1c97f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 6e 74 65 ./* Opcode: Inte
1c980 67 65 72 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a ger P1 P2 * * *.
1c981 2a 2a 0a 2a 2a 20 54 68 65 20 33 32 2d 62 69 74 **.** The 32-bit
1c982 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20 50 integer value P
1c983 31 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e 74 1 is written int
1c984 6f 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a o register P2..*
1c985 2f 0a 63 61 73 65 20 4f 50 5f 49 6e 74 65 67 65 /.case OP_Intege
1c986 72 3a 20 7b 20 20 20 20 20 20 20 20 20 2f 2a 20 r: { /*
1c987 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 out2-prerelease
1c988 2a 2f 0a 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 */. pOut->flags
1c989 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 70 4f = MEM_Int;. pO
1c98a 75 74 2d 3e 75 2e 69 20 3d 20 70 4f 70 2d 3e 70 ut->u.i = pOp->p
1c98b 31 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 1;. break;.}../
1c98c 2a 20 4f 70 63 6f 64 65 3a 20 49 6e 74 36 34 20 * Opcode: Int64
1c98d 2a 20 50 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a * P2 * P4 *.**.*
1c98e 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 * P4 is a pointe
1c98f 72 20 74 6f 20 61 20 36 34 2d 62 69 74 20 69 6e r to a 64-bit in
1c990 74 65 67 65 72 20 76 61 6c 75 65 2e 0a 2a 2a 20 teger value..**
1c991 57 72 69 74 65 20 74 68 61 74 20 76 61 6c 75 65 Write that value
1c992 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50 into register P
1c993 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 6e 2..*/.case OP_In
1c994 74 36 34 3a 20 7b 20 20 20 20 20 20 20 20 20 20 t64: {
1c995 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 /* out2-prerele
1c996 61 73 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 ase */. assert(
1c997 20 70 4f 70 2d 3e 70 34 2e 70 49 36 34 21 3d 30 pOp->p4.pI64!=0
1c998 20 29 3b 0a 20 20 70 4f 75 74 2d 3e 66 6c 61 67 );. pOut->flag
1c999 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 70 s = MEM_Int;. p
1c99a 4f 75 74 2d 3e 75 2e 69 20 3d 20 2a 70 4f 70 2d Out->u.i = *pOp-
1c99b 3e 70 34 2e 70 49 36 34 3b 0a 20 20 62 72 65 61 >p4.pI64;. brea
1c99c 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a k;.}../* Opcode:
1c99d 20 52 65 61 6c 20 2a 20 50 32 20 2a 20 50 34 20 Real * P2 * P4
1c99e 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 61 20 *.**.** P4 is a
1c99f 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 36 34 2d pointer to a 64-
1c9a0 62 69 74 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 bit floating poi
1c9a1 6e 74 20 76 61 6c 75 65 2e 0a 2a 2a 20 57 72 69 nt value..** Wri
1c9a2 74 65 20 74 68 61 74 20 76 61 6c 75 65 20 69 6e te that value in
1c9a3 74 6f 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a to register P2..
1c9a4 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 65 61 6c 3a */.case OP_Real:
1c9a5 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a { /*
1c9a6 20 73 61 6d 65 20 61 73 20 54 4b 5f 46 4c 4f 41 same as TK_FLOA
1c9a7 54 2c 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 T, out2-prerelea
1c9a8 73 65 20 2a 2f 0a 20 20 70 4f 75 74 2d 3e 66 6c se */. pOut->fl
1c9a9 61 67 73 20 3d 20 4d 45 4d 5f 52 65 61 6c 3b 0a ags = MEM_Real;.
1c9aa 20 20 61 73 73 65 72 74 28 20 21 73 71 6c 69 74 assert( !sqlit
1c9ab 65 33 49 73 4e 61 4e 28 2a 70 4f 70 2d 3e 70 34 e3IsNaN(*pOp->p4
1c9ac 2e 70 52 65 61 6c 29 20 29 3b 0a 20 20 70 4f 75 .pReal) );. pOu
1c9ad 74 2d 3e 72 20 3d 20 2a 70 4f 70 2d 3e 70 34 2e t->r = *pOp->p4.
1c9ae 70 52 65 61 6c 3b 0a 20 20 62 72 65 61 6b 3b 0a pReal;. break;.
1c9af 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 74 }../* Opcode: St
1c9b0 72 69 6e 67 38 20 2a 20 50 32 20 2a 20 50 34 20 ring8 * P2 * P4
1c9b1 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 70 6f 69 6e 74 *.**.** P4 point
1c9b2 73 20 74 6f 20 61 20 6e 75 6c 20 74 65 72 6d 69 s to a nul termi
1c9b3 6e 61 74 65 64 20 55 54 46 2d 38 20 73 74 72 69 nated UTF-8 stri
1c9b4 6e 67 2e 20 54 68 69 73 20 6f 70 63 6f 64 65 20 ng. This opcode
1c9b5 69 73 20 74 72 61 6e 73 66 6f 72 6d 65 64 20 0a is transformed .
1c9b6 2a 2a 20 69 6e 74 6f 20 61 6e 20 4f 50 5f 53 74 ** into an OP_St
1c9b7 72 69 6e 67 20 62 65 66 6f 72 65 20 69 74 20 69 ring before it i
1c9b8 73 20 65 78 65 63 75 74 65 64 20 66 6f 72 20 74 s executed for t
1c9b9 68 65 20 66 69 72 73 74 20 74 69 6d 65 2e 0a 2a he first time..*
1c9ba 2f 0a 63 61 73 65 20 4f 50 5f 53 74 72 69 6e 67 /.case OP_String
1c9bb 38 3a 20 7b 20 20 20 20 20 20 20 20 20 2f 2a 20 8: { /*
1c9bc 73 61 6d 65 20 61 73 20 54 4b 5f 53 54 52 49 4e same as TK_STRIN
1c9bd 47 2c 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 G, out2-prerelea
1c9be 73 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 se */. assert(
1c9bf 70 4f 70 2d 3e 70 34 2e 7a 21 3d 30 20 29 3b 0a pOp->p4.z!=0 );.
1c9c0 20 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 3d 20 pOp->opcode =
1c9c1 4f 50 5f 53 74 72 69 6e 67 3b 0a 20 20 70 4f 70 OP_String;. pOp
1c9c2 2d 3e 70 31 20 3d 20 73 71 6c 69 74 65 33 53 74 ->p1 = sqlite3St
1c9c3 72 6c 65 6e 33 30 28 70 4f 70 2d 3e 70 34 2e 7a rlen30(pOp->p4.z
1c9c4 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 );..#ifndef SQLI
1c9c5 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 20 20 TE_OMIT_UTF16.
1c9c6 69 66 28 20 65 6e 63 6f 64 69 6e 67 21 3d 53 51 if( encoding!=SQ
1c9c7 4c 49 54 45 5f 55 54 46 38 20 29 7b 0a 20 20 20 LITE_UTF8 ){.
1c9c8 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 rc = sqlite3Vdb
1c9c9 65 4d 65 6d 53 65 74 53 74 72 28 70 4f 75 74 2c eMemSetStr(pOut,
1c9ca 20 70 4f 70 2d 3e 70 34 2e 7a 2c 20 2d 31 2c 20 pOp->p4.z, -1,
1c9cb 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c SQLITE_UTF8, SQL
1c9cc 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 ITE_STATIC);.
1c9cd 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
1c9ce 54 4f 4f 42 49 47 20 29 20 67 6f 74 6f 20 74 6f TOOBIG ) goto to
1c9cf 6f 5f 62 69 67 3b 0a 20 20 20 20 69 66 28 20 53 o_big;. if( S
1c9d0 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 QLITE_OK!=sqlite
1c9d1 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 3VdbeChangeEncod
1c9d2 69 6e 67 28 70 4f 75 74 2c 20 65 6e 63 6f 64 69 ing(pOut, encodi
1c9d3 6e 67 29 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 ng) ) goto no_me
1c9d4 6d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 m;. assert( p
1c9d5 4f 75 74 2d 3e 7a 4d 61 6c 6c 6f 63 3d 3d 70 4f Out->zMalloc==pO
1c9d6 75 74 2d 3e 7a 20 29 3b 0a 20 20 20 20 61 73 73 ut->z );. ass
1c9d7 65 72 74 28 20 70 4f 75 74 2d 3e 66 6c 61 67 73 ert( pOut->flags
1c9d8 20 26 20 4d 45 4d 5f 44 79 6e 20 29 3b 0a 20 20 & MEM_Dyn );.
1c9d9 20 20 70 4f 75 74 2d 3e 7a 4d 61 6c 6c 6f 63 20 pOut->zMalloc
1c9da 3d 20 30 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 66 = 0;. pOut->f
1c9db 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 53 74 61 74 lags |= MEM_Stat
1c9dc 69 63 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 66 6c ic;. pOut->fl
1c9dd 61 67 73 20 26 3d 20 7e 4d 45 4d 5f 44 79 6e 3b ags &= ~MEM_Dyn;
1c9de 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 34 . if( pOp->p4
1c9df 74 79 70 65 3d 3d 50 34 5f 44 59 4e 41 4d 49 43 type==P4_DYNAMIC
1c9e0 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
1c9e1 33 44 62 46 72 65 65 28 64 62 2c 20 70 4f 70 2d 3DbFree(db, pOp-
1c9e2 3e 70 34 2e 7a 29 3b 0a 20 20 20 20 7d 0a 20 20 >p4.z);. }.
1c9e3 20 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 pOp->p4type =
1c9e4 50 34 5f 44 59 4e 41 4d 49 43 3b 0a 20 20 20 20 P4_DYNAMIC;.
1c9e5 70 4f 70 2d 3e 70 34 2e 7a 20 3d 20 70 4f 75 74 pOp->p4.z = pOut
1c9e6 2d 3e 7a 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 31 ->z;. pOp->p1
1c9e7 20 3d 20 70 4f 75 74 2d 3e 6e 3b 0a 20 20 7d 0a = pOut->n;. }.
1c9e8 23 65 6e 64 69 66 0a 20 20 69 66 28 20 70 4f 70 #endif. if( pOp
1c9e9 2d 3e 70 31 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b ->p1>db->aLimit[
1c9ea 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e SQLITE_LIMIT_LEN
1c9eb 47 54 48 5d 20 29 7b 0a 20 20 20 20 67 6f 74 6f GTH] ){. goto
1c9ec 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a 20 20 too_big;. }.
1c9ed 2f 2a 20 46 61 6c 6c 20 74 68 72 6f 75 67 68 20 /* Fall through
1c9ee 74 6f 20 74 68 65 20 6e 65 78 74 20 63 61 73 65 to the next case
1c9ef 2c 20 4f 50 5f 53 74 72 69 6e 67 20 2a 2f 0a 7d , OP_String */.}
1c9f0 0a 20 20 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 . ./* Opcode: S
1c9f1 74 72 69 6e 67 20 50 31 20 50 32 20 2a 20 50 34 tring P1 P2 * P4
1c9f2 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 74 72 *.**.** The str
1c9f3 69 6e 67 20 76 61 6c 75 65 20 50 34 20 6f 66 20 ing value P4 of
1c9f4 6c 65 6e 67 74 68 20 50 31 20 28 62 79 74 65 73 length P1 (bytes
1c9f5 29 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 72 ) is stored in r
1c9f6 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2f 0a 63 egister P2..*/.c
1c9f7 61 73 65 20 4f 50 5f 53 74 72 69 6e 67 3a 20 7b ase OP_String: {
1c9f8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 /* out
1c9f9 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 2-prerelease */.
1c9fa 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
1c9fb 34 2e 7a 21 3d 30 20 29 3b 0a 20 20 70 4f 75 74 4.z!=0 );. pOut
1c9fc 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 53 74 ->flags = MEM_St
1c9fd 72 7c 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d r|MEM_Static|MEM
1c9fe 5f 54 65 72 6d 3b 0a 20 20 70 4f 75 74 2d 3e 7a _Term;. pOut->z
1c9ff 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 = pOp->p4.z;.
1ca00 70 4f 75 74 2d 3e 6e 20 3d 20 70 4f 70 2d 3e 70 pOut->n = pOp->p
1ca01 31 3b 0a 20 20 70 4f 75 74 2d 3e 65 6e 63 20 3d 1;. pOut->enc =
1ca02 20 65 6e 63 6f 64 69 6e 67 3b 0a 20 20 55 50 44 encoding;. UPD
1ca03 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 ATE_MAX_BLOBSIZE
1ca04 28 70 4f 75 74 29 3b 0a 20 20 62 72 65 61 6b 3b (pOut);. break;
1ca05 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e .}../* Opcode: N
1ca06 75 6c 6c 20 2a 20 50 32 20 2a 20 2a 20 2a 0a 2a ull * P2 * * *.*
1ca07 2a 0a 2a 2a 20 57 72 69 74 65 20 61 20 4e 55 4c *.** Write a NUL
1ca08 4c 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 L into register
1ca09 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e P2..*/.case OP_N
1ca0a 75 6c 6c 3a 20 7b 20 20 20 20 20 20 20 20 20 20 ull: {
1ca0b 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 /* out2-prerele
1ca0c 61 73 65 20 2a 2f 0a 20 20 62 72 65 61 6b 3b 0a ase */. break;.
1ca0d 7d 0a 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 42 }.../* Opcode: B
1ca0e 6c 6f 62 20 50 31 20 50 32 20 2a 20 50 34 0a 2a lob P1 P2 * P4.*
1ca0f 2a 0a 2a 2a 20 50 34 20 70 6f 69 6e 74 73 20 74 *.** P4 points t
1ca10 6f 20 61 20 62 6c 6f 62 20 6f 66 20 64 61 74 61 o a blob of data
1ca11 20 50 31 20 62 79 74 65 73 20 6c 6f 6e 67 2e 20 P1 bytes long.
1ca12 20 53 74 6f 72 65 20 74 68 69 73 0a 2a 2a 20 62 Store this.** b
1ca13 6c 6f 62 20 69 6e 20 72 65 67 69 73 74 65 72 20 lob in register
1ca14 50 32 2e 20 54 68 69 73 20 69 6e 73 74 72 75 63 P2. This instruc
1ca15 74 69 6f 6e 20 69 73 20 6e 6f 74 20 63 6f 64 65 tion is not code
1ca16 64 20 64 69 72 65 63 74 6c 79 0a 2a 2a 20 62 79 d directly.** by
1ca17 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 2e 20 49 the compiler. I
1ca18 6e 73 74 65 61 64 2c 20 74 68 65 20 63 6f 6d 70 nstead, the comp
1ca19 69 6c 65 72 20 6c 61 79 65 72 20 73 70 65 63 69 iler layer speci
1ca1a 66 69 65 73 0a 2a 2a 20 61 6e 20 4f 50 5f 48 65 fies.** an OP_He
1ca1b 78 42 6c 6f 62 20 6f 70 63 6f 64 65 2c 20 77 69 xBlob opcode, wi
1ca1c 74 68 20 74 68 65 20 68 65 78 20 73 74 72 69 6e th the hex strin
1ca1d 67 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e g representation
1ca1e 20 6f 66 0a 2a 2a 20 74 68 65 20 62 6c 6f 62 20 of.** the blob
1ca1f 61 73 20 50 34 2e 20 54 68 69 73 20 6f 70 63 6f as P4. This opco
1ca20 64 65 20 69 73 20 74 72 61 6e 73 66 6f 72 6d 65 de is transforme
1ca21 64 20 74 6f 20 61 6e 20 4f 50 5f 42 6c 6f 62 0a d to an OP_Blob.
1ca22 2a 2a 20 74 68 65 20 66 69 72 73 74 20 74 69 6d ** the first tim
1ca23 65 20 69 74 20 69 73 20 65 78 65 63 75 74 65 64 e it is executed
1ca24 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 42 6c 6f ..*/.case OP_Blo
1ca25 62 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 b: {
1ca26 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 /* out2-prer
1ca27 65 6c 65 61 73 65 20 2a 2f 0a 20 20 61 73 73 65 elease */. asse
1ca28 72 74 28 20 70 4f 70 2d 3e 70 31 20 3c 3d 20 53 rt( pOp->p1 <= S
1ca29 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 QLITE_MAX_LENGTH
1ca2a 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 );. sqlite3Vdb
1ca2b 65 4d 65 6d 53 65 74 53 74 72 28 70 4f 75 74 2c eMemSetStr(pOut,
1ca2c 20 70 4f 70 2d 3e 70 34 2e 7a 2c 20 70 4f 70 2d pOp->p4.z, pOp-
1ca2d 3e 70 31 2c 20 30 2c 20 30 29 3b 0a 20 20 70 4f >p1, 0, 0);. pO
1ca2e 75 74 2d 3e 65 6e 63 20 3d 20 65 6e 63 6f 64 69 ut->enc = encodi
1ca2f 6e 67 3b 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 ng;. UPDATE_MAX
1ca30 5f 42 4c 4f 42 53 49 5a 45 28 70 4f 75 74 29 3b _BLOBSIZE(pOut);
1ca31 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 . break;.}../*
1ca32 4f 70 63 6f 64 65 3a 20 56 61 72 69 61 62 6c 65 Opcode: Variable
1ca33 20 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a P1 P2 P3 P4 *.*
1ca34 2a 0a 2a 2a 20 54 72 61 6e 73 66 65 72 20 74 68 *.** Transfer th
1ca35 65 20 76 61 6c 75 65 73 20 6f 66 20 62 6f 75 6e e values of boun
1ca36 64 20 70 61 72 61 6d 65 74 65 72 73 20 50 31 2e d parameters P1.
1ca37 2e 50 31 2b 50 33 2d 31 20 69 6e 74 6f 20 72 65 .P1+P3-1 into re
1ca38 67 69 73 74 65 72 73 0a 2a 2a 20 50 32 2e 2e 50 gisters.** P2..P
1ca39 32 2b 50 33 2d 31 2e 0a 2a 2a 0a 2a 2a 20 49 66 2+P3-1..**.** If
1ca3a 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 20 69 the parameter i
1ca3b 73 20 6e 61 6d 65 64 2c 20 74 68 65 6e 20 69 74 s named, then it
1ca3c 73 20 6e 61 6d 65 20 61 70 70 65 61 72 73 20 69 s name appears i
1ca3d 6e 20 50 34 20 61 6e 64 20 50 33 3d 3d 31 2e 0a n P4 and P3==1..
1ca3e 2a 2a 20 54 68 65 20 50 34 20 76 61 6c 75 65 20 ** The P4 value
1ca3f 69 73 20 75 73 65 64 20 62 79 20 73 71 6c 69 74 is used by sqlit
1ca40 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 e3_bind_paramete
1ca41 72 5f 6e 61 6d 65 28 29 2e 0a 2a 2f 0a 63 61 73 r_name()..*/.cas
1ca42 65 20 4f 50 5f 56 61 72 69 61 62 6c 65 3a 20 7b e OP_Variable: {
1ca43 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c .#if 0 /* local
1ca44 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1ca45 20 69 6e 74 6f 20 75 2e 61 62 20 2a 2f 0a 20 20 into u.ab */.
1ca46 69 6e 74 20 70 31 3b 20 20 20 20 20 20 20 20 20 int p1;
1ca47 20 2f 2a 20 56 61 72 69 61 62 6c 65 20 74 6f 20 /* Variable to
1ca48 63 6f 70 79 20 66 72 6f 6d 20 2a 2f 0a 20 20 69 copy from */. i
1ca49 6e 74 20 70 32 3b 20 20 20 20 20 20 20 20 20 20 nt p2;
1ca4a 2f 2a 20 52 65 67 69 73 74 65 72 20 74 6f 20 63 /* Register to c
1ca4b 6f 70 79 20 74 6f 20 2a 2f 0a 20 20 69 6e 74 20 opy to */. int
1ca4c 6e 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 n; /*
1ca4d 4e 75 6d 62 65 72 20 6f 66 20 76 61 6c 75 65 73 Number of values
1ca4e 20 6c 65 66 74 20 74 6f 20 63 6f 70 79 20 2a 2f left to copy */
1ca4f 0a 20 20 4d 65 6d 20 2a 70 56 61 72 3b 20 20 20 . Mem *pVar;
1ca50 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 62 65 69 /* Value bei
1ca51 6e 67 20 74 72 61 6e 73 66 65 72 72 65 64 20 2a ng transferred *
1ca52 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 /.#endif /* loca
1ca53 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 l variables move
1ca54 64 20 69 6e 74 6f 20 75 2e 61 62 20 2a 2f 0a 0a d into u.ab */..
1ca55 20 20 75 2e 61 62 2e 70 31 20 3d 20 70 4f 70 2d u.ab.p1 = pOp-
1ca56 3e 70 31 20 2d 20 31 3b 0a 20 20 75 2e 61 62 2e >p1 - 1;. u.ab.
1ca57 70 32 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 p2 = pOp->p2;.
1ca58 75 2e 61 62 2e 6e 20 3d 20 70 4f 70 2d 3e 70 33 u.ab.n = pOp->p3
1ca59 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 62 ;. assert( u.ab
1ca5a 2e 70 31 3e 3d 30 20 26 26 20 75 2e 61 62 2e 70 .p1>=0 && u.ab.p
1ca5b 31 2b 75 2e 61 62 2e 6e 3c 3d 70 2d 3e 6e 56 61 1+u.ab.n<=p->nVa
1ca5c 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75 r );. assert( u
1ca5d 2e 61 62 2e 70 32 3e 3d 31 20 26 26 20 75 2e 61 .ab.p2>=1 && u.a
1ca5e 62 2e 70 32 2b 75 2e 61 62 2e 6e 2d 31 3c 3d 70 b.p2+u.ab.n-1<=p
1ca5f 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 61 73 73 65 ->nMem );. asse
1ca60 72 74 28 20 70 4f 70 2d 3e 70 34 2e 7a 3d 3d 30 rt( pOp->p4.z==0
1ca61 20 7c 7c 20 70 4f 70 2d 3e 70 33 3d 3d 31 20 29 || pOp->p3==1 )
1ca62 3b 0a 0a 20 20 77 68 69 6c 65 28 20 75 2e 61 62 ;.. while( u.ab
1ca63 2e 6e 2d 2d 20 3e 20 30 20 29 7b 0a 20 20 20 20 .n-- > 0 ){.
1ca64 75 2e 61 62 2e 70 56 61 72 20 3d 20 26 70 2d 3e u.ab.pVar = &p->
1ca65 61 56 61 72 5b 75 2e 61 62 2e 70 31 2b 2b 5d 3b aVar[u.ab.p1++];
1ca66 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 . if( sqlite3
1ca67 56 64 62 65 4d 65 6d 54 6f 6f 42 69 67 28 75 2e VdbeMemTooBig(u.
1ca68 61 62 2e 70 56 61 72 29 20 29 7b 0a 20 20 20 20 ab.pVar) ){.
1ca69 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a goto too_big;.
1ca6a 20 20 20 20 7d 0a 20 20 20 20 70 4f 75 74 20 3d }. pOut =
1ca6b 20 26 70 2d 3e 61 4d 65 6d 5b 75 2e 61 62 2e 70 &p->aMem[u.ab.p
1ca6c 32 2b 2b 5d 3b 0a 20 20 20 20 73 71 6c 69 74 65 2++];. sqlite
1ca6d 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 45 3VdbeMemReleaseE
1ca6e 78 74 65 72 6e 61 6c 28 70 4f 75 74 29 3b 0a 20 xternal(pOut);.
1ca6f 20 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 3d pOut->flags =
1ca70 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 73 MEM_Null;. s
1ca71 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 68 61 qlite3VdbeMemSha
1ca72 6c 6c 6f 77 43 6f 70 79 28 70 4f 75 74 2c 20 75 llowCopy(pOut, u
1ca73 2e 61 62 2e 70 56 61 72 2c 20 4d 45 4d 5f 53 74 .ab.pVar, MEM_St
1ca74 61 74 69 63 29 3b 0a 20 20 20 20 55 50 44 41 54 atic);. UPDAT
1ca75 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 E_MAX_BLOBSIZE(p
1ca76 4f 75 74 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 Out);. }. brea
1ca77 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a k;.}../* Opcode:
1ca78 20 4d 6f 76 65 20 50 31 20 50 32 20 50 33 20 2a Move P1 P2 P3 *
1ca79 20 2a 0a 2a 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 *.**.** Move th
1ca7a 65 20 76 61 6c 75 65 73 20 69 6e 20 72 65 67 69 e values in regi
1ca7b 73 74 65 72 20 50 31 2e 2e 50 31 2b 50 33 2d 31 ster P1..P1+P3-1
1ca7c 20 6f 76 65 72 20 69 6e 74 6f 0a 2a 2a 20 72 65 over into.** re
1ca7d 67 69 73 74 65 72 73 20 50 32 2e 2e 50 32 2b 50 gisters P2..P2+P
1ca7e 33 2d 31 2e 20 20 52 65 67 69 73 74 65 72 73 20 3-1. Registers
1ca7f 50 31 2e 2e 50 31 2b 50 31 2d 31 20 61 72 65 0a P1..P1+P1-1 are.
1ca80 2a 2a 20 6c 65 66 74 20 68 6f 6c 64 69 6e 67 20 ** left holding
1ca81 61 20 4e 55 4c 4c 2e 20 20 49 74 20 69 73 20 61 a NULL. It is a
1ca82 6e 20 65 72 72 6f 72 20 66 6f 72 20 72 65 67 69 n error for regi
1ca83 73 74 65 72 20 72 61 6e 67 65 73 0a 2a 2a 20 50 ster ranges.** P
1ca84 31 2e 2e 50 31 2b 50 33 2d 31 20 61 6e 64 20 50 1..P1+P3-1 and P
1ca85 32 2e 2e 50 32 2b 50 33 2d 31 20 74 6f 20 6f 76 2..P2+P3-1 to ov
1ca86 65 72 6c 61 70 2e 0a 2a 2f 0a 63 61 73 65 20 4f erlap..*/.case O
1ca87 50 5f 4d 6f 76 65 3a 20 7b 0a 23 69 66 20 30 20 P_Move: {.#if 0
1ca88 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1ca89 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1ca8a 2e 61 63 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a .ac */. char *z
1ca8b 4d 61 6c 6c 6f 63 3b 20 20 20 2f 2a 20 48 6f 6c Malloc; /* Hol
1ca8c 64 69 6e 67 20 76 61 72 69 61 62 6c 65 20 66 6f ding variable fo
1ca8d 72 20 61 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f r allocated memo
1ca8e 72 79 20 2a 2f 0a 20 20 69 6e 74 20 6e 3b 20 20 ry */. int n;
1ca8f 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
1ca90 65 72 20 6f 66 20 72 65 67 69 73 74 65 72 73 20 er of registers
1ca91 6c 65 66 74 20 74 6f 20 63 6f 70 79 20 2a 2f 0a left to copy */.
1ca92 20 20 69 6e 74 20 70 31 3b 20 20 20 20 20 20 20 int p1;
1ca93 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 74 /* Register t
1ca94 6f 20 63 6f 70 79 20 66 72 6f 6d 20 2a 2f 0a 20 o copy from */.
1ca95 20 69 6e 74 20 70 32 3b 20 20 20 20 20 20 20 20 int p2;
1ca96 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 74 6f /* Register to
1ca97 20 63 6f 70 79 20 74 6f 20 2a 2f 0a 23 65 6e 64 copy to */.#end
1ca98 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 if /* local vari
1ca99 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1ca9a 20 75 2e 61 63 20 2a 2f 0a 0a 20 20 75 2e 61 63 u.ac */.. u.ac
1ca9b 2e 6e 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 .n = pOp->p3;.
1ca9c 75 2e 61 63 2e 70 31 20 3d 20 70 4f 70 2d 3e 70 u.ac.p1 = pOp->p
1ca9d 31 3b 0a 20 20 75 2e 61 63 2e 70 32 20 3d 20 70 1;. u.ac.p2 = p
1ca9e 4f 70 2d 3e 70 32 3b 0a 20 20 61 73 73 65 72 74 Op->p2;. assert
1ca9f 28 20 75 2e 61 63 2e 6e 3e 30 20 26 26 20 75 2e ( u.ac.n>0 && u.
1caa0 61 63 2e 70 31 3e 30 20 26 26 20 75 2e 61 63 2e ac.p1>0 && u.ac.
1caa1 70 32 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 p2>0 );. assert
1caa2 28 20 75 2e 61 63 2e 70 31 2b 75 2e 61 63 2e 6e ( u.ac.p1+u.ac.n
1caa3 3c 3d 75 2e 61 63 2e 70 32 20 7c 7c 20 75 2e 61 <=u.ac.p2 || u.a
1caa4 63 2e 70 32 2b 75 2e 61 63 2e 6e 3c 3d 75 2e 61 c.p2+u.ac.n<=u.a
1caa5 63 2e 70 31 20 29 3b 0a 0a 20 20 70 49 6e 31 20 c.p1 );.. pIn1
1caa6 3d 20 26 70 2d 3e 61 4d 65 6d 5b 75 2e 61 63 2e = &p->aMem[u.ac.
1caa7 70 31 5d 3b 0a 20 20 70 4f 75 74 20 3d 20 26 70 p1];. pOut = &p
1caa8 2d 3e 61 4d 65 6d 5b 75 2e 61 63 2e 70 32 5d 3b ->aMem[u.ac.p2];
1caa9 0a 20 20 77 68 69 6c 65 28 20 75 2e 61 63 2e 6e . while( u.ac.n
1caaa 2d 2d 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 -- ){. assert
1caab 28 20 70 4f 75 74 3c 3d 26 70 2d 3e 61 4d 65 6d ( pOut<=&p->aMem
1caac 5b 70 2d 3e 6e 4d 65 6d 5d 20 29 3b 0a 20 20 20 [p->nMem] );.
1caad 20 61 73 73 65 72 74 28 20 70 49 6e 31 3c 3d 26 assert( pIn1<=&
1caae 70 2d 3e 61 4d 65 6d 5b 70 2d 3e 6e 4d 65 6d 5d p->aMem[p->nMem]
1caaf 20 29 3b 0a 20 20 20 20 75 2e 61 63 2e 7a 4d 61 );. u.ac.zMa
1cab0 6c 6c 6f 63 20 3d 20 70 4f 75 74 2d 3e 7a 4d 61 lloc = pOut->zMa
1cab1 6c 6c 6f 63 3b 0a 20 20 20 20 70 4f 75 74 2d 3e lloc;. pOut->
1cab2 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 20 zMalloc = 0;.
1cab3 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d sqlite3VdbeMemM
1cab4 6f 76 65 28 70 4f 75 74 2c 20 70 49 6e 31 29 3b ove(pOut, pIn1);
1cab5 0a 20 20 20 20 70 49 6e 31 2d 3e 7a 4d 61 6c 6c . pIn1->zMall
1cab6 6f 63 20 3d 20 75 2e 61 63 2e 7a 4d 61 6c 6c 6f oc = u.ac.zMallo
1cab7 63 3b 0a 20 20 20 20 52 45 47 49 53 54 45 52 5f c;. REGISTER_
1cab8 54 52 41 43 45 28 75 2e 61 63 2e 70 32 2b 2b 2c TRACE(u.ac.p2++,
1cab9 20 70 4f 75 74 29 3b 0a 20 20 20 20 70 49 6e 31 pOut);. pIn1
1caba 2b 2b 3b 0a 20 20 20 20 70 4f 75 74 2b 2b 3b 0a ++;. pOut++;.
1cabb 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a }. break;.}..
1cabc 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6f 70 79 20 /* Opcode: Copy
1cabd 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a P1 P2 * * *.**.*
1cabe 2a 20 4d 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 * Make a copy of
1cabf 20 72 65 67 69 73 74 65 72 20 50 31 20 69 6e 74 register P1 int
1cac0 6f 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a o register P2..*
1cac1 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 73 74 72 75 *.** This instru
1cac2 63 74 69 6f 6e 20 6d 61 6b 65 73 20 61 20 64 65 ction makes a de
1cac3 65 70 20 63 6f 70 79 20 6f 66 20 74 68 65 20 76 ep copy of the v
1cac4 61 6c 75 65 2e 20 20 41 20 64 75 70 6c 69 63 61 alue. A duplica
1cac5 74 65 0a 2a 2a 20 69 73 20 6d 61 64 65 20 6f 66 te.** is made of
1cac6 20 61 6e 79 20 73 74 72 69 6e 67 20 6f 72 20 62 any string or b
1cac7 6c 6f 62 20 63 6f 6e 73 74 61 6e 74 2e 20 20 53 lob constant. S
1cac8 65 65 20 61 6c 73 6f 20 4f 50 5f 53 43 6f 70 79 ee also OP_SCopy
1cac9 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 6f 70 ..*/.case OP_Cop
1caca 79 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 y: {
1cacb 20 2f 2a 20 69 6e 31 20 2a 2f 0a 20 20 61 73 73 /* in1 */. ass
1cacc 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20 29 ert( pOp->p2>0 )
1cacd 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d ;. assert( pOp-
1cace 3e 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a >p2<=p->nMem );.
1cacf 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65 pOut = &p->aMe
1cad0 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 61 73 m[pOp->p2];. as
1cad1 73 65 72 74 28 20 70 4f 75 74 21 3d 70 49 6e 31 sert( pOut!=pIn1
1cad2 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 );. sqlite3Vdb
1cad3 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28 eMemShallowCopy(
1cad4 70 4f 75 74 2c 20 70 49 6e 31 2c 20 4d 45 4d 5f pOut, pIn1, MEM_
1cad5 45 70 68 65 6d 29 3b 0a 20 20 44 65 65 70 68 65 Ephem);. Deephe
1cad6 6d 65 72 61 6c 69 7a 65 28 70 4f 75 74 29 3b 0a meralize(pOut);.
1cad7 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 REGISTER_TRACE
1cad8 28 70 4f 70 2d 3e 70 32 2c 20 70 4f 75 74 29 3b (pOp->p2, pOut);
1cad9 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 . break;.}../*
1cada 4f 70 63 6f 64 65 3a 20 53 43 6f 70 79 20 50 31 Opcode: SCopy P1
1cadb 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 P2 * * *.**.**
1cadc 4d 61 6b 65 20 61 20 73 68 61 6c 6c 6f 77 20 63 Make a shallow c
1cadd 6f 70 79 20 6f 66 20 72 65 67 69 73 74 65 72 20 opy of register
1cade 50 31 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 P1 into register
1cadf 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 P2..**.** This
1cae0 69 6e 73 74 72 75 63 74 69 6f 6e 20 6d 61 6b 65 instruction make
1cae1 73 20 61 20 73 68 61 6c 6c 6f 77 20 63 6f 70 79 s a shallow copy
1cae2 20 6f 66 20 74 68 65 20 76 61 6c 75 65 2e 20 20 of the value.
1cae3 49 66 20 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20 If the value.**
1cae4 69 73 20 61 20 73 74 72 69 6e 67 20 6f 72 20 62 is a string or b
1cae5 6c 6f 62 2c 20 74 68 65 6e 20 74 68 65 20 63 6f lob, then the co
1cae6 70 79 20 69 73 20 6f 6e 6c 79 20 61 20 70 6f 69 py is only a poi
1cae7 6e 74 65 72 20 74 6f 20 74 68 65 0a 2a 2a 20 6f nter to the.** o
1cae8 72 69 67 69 6e 61 6c 20 61 6e 64 20 68 65 6e 63 riginal and henc
1cae9 65 20 69 66 20 74 68 65 20 6f 72 69 67 69 6e 61 e if the origina
1caea 6c 20 63 68 61 6e 67 65 73 20 73 6f 20 77 69 6c l changes so wil
1caeb 6c 20 74 68 65 20 63 6f 70 79 2e 0a 2a 2a 20 57 l the copy..** W
1caec 6f 72 73 65 2c 20 69 66 20 74 68 65 20 6f 72 69 orse, if the ori
1caed 67 69 6e 61 6c 20 69 73 20 64 65 61 6c 6c 6f 63 ginal is dealloc
1caee 61 74 65 64 2c 20 74 68 65 20 63 6f 70 79 20 62 ated, the copy b
1caef 65 63 6f 6d 65 73 20 69 6e 76 61 6c 69 64 2e 0a ecomes invalid..
1caf0 2a 2a 20 54 68 75 73 20 74 68 65 20 70 72 6f 67 ** Thus the prog
1caf1 72 61 6d 20 6d 75 73 74 20 67 75 61 72 61 6e 74 ram must guarant
1caf2 65 65 20 74 68 61 74 20 74 68 65 20 6f 72 69 67 ee that the orig
1caf3 69 6e 61 6c 20 77 69 6c 6c 20 6e 6f 74 20 63 68 inal will not ch
1caf4 61 6e 67 65 0a 2a 2a 20 64 75 72 69 6e 67 20 74 ange.** during t
1caf5 68 65 20 6c 69 66 65 74 69 6d 65 20 6f 66 20 74 he lifetime of t
1caf6 68 65 20 63 6f 70 79 2e 20 20 55 73 65 20 4f 50 he copy. Use OP
1caf7 5f 43 6f 70 79 20 74 6f 20 6d 61 6b 65 20 61 20 _Copy to make a
1caf8 63 6f 6d 70 6c 65 74 65 0a 2a 2a 20 63 6f 70 79 complete.** copy
1caf9 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 43 6f ..*/.case OP_SCo
1cafa 70 79 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 py: {
1cafb 20 2f 2a 20 69 6e 31 20 2a 2f 0a 20 20 52 45 47 /* in1 */. REG
1cafc 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d ISTER_TRACE(pOp-
1cafd 3e 70 31 2c 20 70 49 6e 31 29 3b 0a 20 20 61 73 >p1, pIn1);. as
1cafe 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20 sert( pOp->p2>0
1caff 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 );. assert( pOp
1cb00 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b ->p2<=p->nMem );
1cb01 0a 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d . pOut = &p->aM
1cb02 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 61 em[pOp->p2];. a
1cb03 73 73 65 72 74 28 20 70 4f 75 74 21 3d 70 49 6e ssert( pOut!=pIn
1cb04 31 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 1 );. sqlite3Vd
1cb05 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 beMemShallowCopy
1cb06 28 70 4f 75 74 2c 20 70 49 6e 31 2c 20 4d 45 4d (pOut, pIn1, MEM
1cb07 5f 45 70 68 65 6d 29 3b 0a 20 20 52 45 47 49 53 _Ephem);. REGIS
1cb08 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 TER_TRACE(pOp->p
1cb09 32 2c 20 70 4f 75 74 29 3b 0a 20 20 62 72 65 61 2, pOut);. brea
1cb0a 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a k;.}../* Opcode:
1cb0b 20 52 65 73 75 6c 74 52 6f 77 20 50 31 20 50 32 ResultRow P1 P2
1cb0c 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 * * *.**.** The
1cb0d 20 72 65 67 69 73 74 65 72 73 20 50 31 20 74 68 registers P1 th
1cb0e 72 6f 75 67 68 20 50 31 2b 50 32 2d 31 20 63 6f rough P1+P2-1 co
1cb0f 6e 74 61 69 6e 20 61 20 73 69 6e 67 6c 65 20 72 ntain a single r
1cb10 6f 77 20 6f 66 0a 2a 2a 20 72 65 73 75 6c 74 73 ow of.** results
1cb11 2e 20 54 68 69 73 20 6f 70 63 6f 64 65 20 63 61 . This opcode ca
1cb12 75 73 65 73 20 74 68 65 20 73 71 6c 69 74 65 33 uses the sqlite3
1cb13 5f 73 74 65 70 28 29 20 63 61 6c 6c 20 74 6f 20 _step() call to
1cb14 74 65 72 6d 69 6e 61 74 65 0a 2a 2a 20 77 69 74 terminate.** wit
1cb15 68 20 61 6e 20 53 51 4c 49 54 45 5f 52 4f 57 20 h an SQLITE_ROW
1cb16 72 65 74 75 72 6e 20 63 6f 64 65 20 61 6e 64 20 return code and
1cb17 69 74 20 73 65 74 73 20 75 70 20 74 68 65 20 73 it sets up the s
1cb18 71 6c 69 74 65 33 5f 73 74 6d 74 0a 2a 2a 20 73 qlite3_stmt.** s
1cb19 74 72 75 63 74 75 72 65 20 74 6f 20 70 72 6f 76 tructure to prov
1cb1a 69 64 65 20 61 63 63 65 73 73 20 74 6f 20 74 68 ide access to th
1cb1b 65 20 74 6f 70 20 50 31 20 76 61 6c 75 65 73 20 e top P1 values
1cb1c 61 73 20 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a as the result.**
1cb1d 20 72 6f 77 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 row..*/.case OP
1cb1e 5f 52 65 73 75 6c 74 52 6f 77 3a 20 7b 0a 23 69 _ResultRow: {.#i
1cb1f 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 f 0 /* local va
1cb20 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1cb21 74 6f 20 75 2e 61 64 20 2a 2f 0a 20 20 4d 65 6d to u.ad */. Mem
1cb22 20 2a 70 4d 65 6d 3b 0a 20 20 69 6e 74 20 69 3b *pMem;. int i;
1cb23 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c .#endif /* local
1cb24 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1cb25 20 69 6e 74 6f 20 75 2e 61 64 20 2a 2f 0a 20 20 into u.ad */.
1cb26 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 73 43 assert( p->nResC
1cb27 6f 6c 75 6d 6e 3d 3d 70 4f 70 2d 3e 70 32 20 29 olumn==pOp->p2 )
1cb28 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d ;. assert( pOp-
1cb29 3e 70 31 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 >p1>0 );. asser
1cb2a 74 28 20 70 4f 70 2d 3e 70 31 2b 70 4f 70 2d 3e t( pOp->p1+pOp->
1cb2b 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 2b 31 20 29 3b p2<=p->nMem+1 );
1cb2c 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 73 .. /* If this s
1cb2d 74 61 74 65 6d 65 6e 74 20 68 61 73 20 76 69 6f tatement has vio
1cb2e 6c 61 74 65 64 20 69 6d 6d 65 64 69 61 74 65 20 lated immediate
1cb2f 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 foreign key cons
1cb30 74 72 61 69 6e 74 73 2c 20 64 6f 0a 20 20 2a 2a traints, do. **
1cb31 20 6e 6f 74 20 72 65 74 75 72 6e 20 74 68 65 20 not return the
1cb32 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 6d number of rows m
1cb33 6f 64 69 66 69 65 64 2e 20 41 6e 64 20 64 6f 20 odified. And do
1cb34 6e 6f 74 20 52 45 4c 45 41 53 45 20 74 68 65 20 not RELEASE the
1cb35 73 74 61 74 65 6d 65 6e 74 0a 20 20 2a 2a 20 74 statement. ** t
1cb36 72 61 6e 73 61 63 74 69 6f 6e 2e 20 49 74 20 6e ransaction. It n
1cb37 65 65 64 73 20 74 6f 20 62 65 20 72 6f 6c 6c 65 eeds to be rolle
1cb38 64 20 62 61 63 6b 2e 20 20 2a 2f 0a 20 20 69 66 d back. */. if
1cb39 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 ( SQLITE_OK!=(rc
1cb3a 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 = sqlite3VdbeCh
1cb3b 65 63 6b 46 6b 28 70 2c 20 30 29 29 20 29 7b 0a eckFk(p, 0)) ){.
1cb3c 20 20 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e assert( db->
1cb3d 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 43 6f 75 flags&SQLITE_Cou
1cb3e 6e 74 52 6f 77 73 20 29 3b 0a 20 20 20 20 61 73 ntRows );. as
1cb3f 73 65 72 74 28 20 70 2d 3e 75 73 65 73 53 74 6d sert( p->usesStm
1cb40 74 4a 6f 75 72 6e 61 6c 20 29 3b 0a 20 20 20 20 tJournal );.
1cb41 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a break;. }.. /*
1cb42 20 49 66 20 74 68 65 20 53 51 4c 49 54 45 5f 43 If the SQLITE_C
1cb43 6f 75 6e 74 52 6f 77 73 20 66 6c 61 67 20 69 73 ountRows flag is
1cb44 20 73 65 74 20 69 6e 20 73 71 6c 69 74 65 33 2e set in sqlite3.
1cb45 66 6c 61 67 73 20 6d 61 73 6b 2c 20 74 68 65 6e flags mask, then
1cb46 0a 20 20 2a 2a 20 44 4d 4c 20 73 74 61 74 65 6d . ** DML statem
1cb47 65 6e 74 73 20 69 6e 76 6f 6b 65 20 74 68 69 73 ents invoke this
1cb48 20 6f 70 63 6f 64 65 20 74 6f 20 72 65 74 75 72 opcode to retur
1cb49 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 n the number of
1cb4a 72 6f 77 73 0a 20 20 2a 2a 20 6d 6f 64 69 66 69 rows. ** modifi
1cb4b 65 64 20 74 6f 20 74 68 65 20 75 73 65 72 2e 20 ed to the user.
1cb4c 54 68 69 73 20 69 73 20 74 68 65 20 6f 6e 6c 79 This is the only
1cb4d 20 77 61 79 20 74 68 61 74 20 61 20 56 4d 20 74 way that a VM t
1cb4e 68 61 74 0a 20 20 2a 2a 20 6f 70 65 6e 73 20 61 hat. ** opens a
1cb4f 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 statement trans
1cb50 61 63 74 69 6f 6e 20 6d 61 79 20 69 6e 76 6f 6b action may invok
1cb51 65 20 74 68 69 73 20 6f 70 63 6f 64 65 2e 0a 20 e this opcode..
1cb52 20 2a 2a 0a 20 20 2a 2a 20 49 6e 20 63 61 73 65 **. ** In case
1cb53 20 74 68 69 73 20 69 73 20 73 75 63 68 20 61 20 this is such a
1cb54 73 74 61 74 65 6d 65 6e 74 2c 20 63 6c 6f 73 65 statement, close
1cb55 20 61 6e 79 20 73 74 61 74 65 6d 65 6e 74 20 74 any statement t
1cb56 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20 2a 2a 20 ransaction. **
1cb57 6f 70 65 6e 65 64 20 62 79 20 74 68 69 73 20 56 opened by this V
1cb58 4d 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 M before returni
1cb59 6e 67 20 63 6f 6e 74 72 6f 6c 20 74 6f 20 74 68 ng control to th
1cb5a 65 20 75 73 65 72 2e 20 54 68 69 73 20 69 73 20 e user. This is
1cb5b 74 6f 0a 20 20 2a 2a 20 65 6e 73 75 72 65 20 74 to. ** ensure t
1cb5c 68 61 74 20 73 74 61 74 65 6d 65 6e 74 2d 74 72 hat statement-tr
1cb5d 61 6e 73 61 63 74 69 6f 6e 73 20 61 72 65 20 61 ansactions are a
1cb5e 6c 77 61 79 73 20 6e 65 73 74 65 64 2c 20 6e 6f lways nested, no
1cb5f 74 20 6f 76 65 72 6c 61 70 70 69 6e 67 2e 0a 20 t overlapping..
1cb60 20 2a 2a 20 49 66 20 74 68 65 20 6f 70 65 6e 20 ** If the open
1cb61 73 74 61 74 65 6d 65 6e 74 2d 74 72 61 6e 73 61 statement-transa
1cb62 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20 63 6c 6f ction is not clo
1cb63 73 65 64 20 68 65 72 65 2c 20 74 68 65 6e 20 74 sed here, then t
1cb64 68 65 20 75 73 65 72 0a 20 20 2a 2a 20 6d 61 79 he user. ** may
1cb65 20 73 74 65 70 20 61 6e 6f 74 68 65 72 20 56 4d step another VM
1cb66 20 74 68 61 74 20 6f 70 65 6e 73 20 69 74 73 20 that opens its
1cb67 6f 77 6e 20 73 74 61 74 65 6d 65 6e 74 20 74 72 own statement tr
1cb68 61 6e 73 61 63 74 69 6f 6e 2e 20 54 68 69 73 0a ansaction. This.
1cb69 20 20 2a 2a 20 6d 61 79 20 6c 65 61 64 20 74 6f ** may lead to
1cb6a 20 6f 76 65 72 6c 61 70 70 69 6e 67 20 73 74 61 overlapping sta
1cb6b 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 tement transacti
1cb6c 6f 6e 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 ons.. **. ** T
1cb6d 68 65 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 he statement tra
1cb6e 6e 73 61 63 74 69 6f 6e 20 69 73 20 6e 65 76 65 nsaction is neve
1cb6f 72 20 61 20 74 6f 70 2d 6c 65 76 65 6c 20 74 72 r a top-level tr
1cb70 61 6e 73 61 63 74 69 6f 6e 2e 20 20 48 65 6e 63 ansaction. Henc
1cb71 65 0a 20 20 2a 2a 20 74 68 65 20 52 45 4c 45 41 e. ** the RELEA
1cb72 53 45 20 63 61 6c 6c 20 62 65 6c 6f 77 20 63 61 SE call below ca
1cb73 6e 20 6e 65 76 65 72 20 66 61 69 6c 2e 0a 20 20 n never fail..
1cb74 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e */. assert( p->
1cb75 69 53 74 61 74 65 6d 65 6e 74 3d 3d 30 20 7c 7c iStatement==0 ||
1cb76 20 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 db->flags&SQLIT
1cb77 45 5f 43 6f 75 6e 74 52 6f 77 73 20 29 3b 0a 20 E_CountRows );.
1cb78 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 rc = sqlite3Vdb
1cb79 65 43 6c 6f 73 65 53 74 61 74 65 6d 65 6e 74 28 eCloseStatement(
1cb7a 70 2c 20 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c p, SAVEPOINT_REL
1cb7b 45 41 53 45 29 3b 0a 20 20 69 66 28 20 4e 45 56 EASE);. if( NEV
1cb7c 45 52 28 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b ER(rc!=SQLITE_OK
1cb7d 29 20 29 7b 0a 20 20 20 20 62 72 65 61 6b 3b 0a ) ){. break;.
1cb7e 20 20 7d 0a 0a 20 20 2f 2a 20 49 6e 76 61 6c 69 }.. /* Invali
1cb7f 64 61 74 65 20 61 6c 6c 20 65 70 68 65 6d 65 72 date all ephemer
1cb80 61 6c 20 63 75 72 73 6f 72 20 72 6f 77 20 63 61 al cursor row ca
1cb81 63 68 65 73 20 2a 2f 0a 20 20 70 2d 3e 63 61 63 ches */. p->cac
1cb82 68 65 43 74 72 20 3d 20 28 70 2d 3e 63 61 63 68 heCtr = (p->cach
1cb83 65 43 74 72 20 2b 20 32 29 7c 31 3b 0a 0a 20 20 eCtr + 2)|1;..
1cb84 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 /* Make sure the
1cb85 20 72 65 73 75 6c 74 73 20 6f 66 20 74 68 65 20 results of the
1cb86 63 75 72 72 65 6e 74 20 72 6f 77 20 61 72 65 20 current row are
1cb87 5c 30 30 30 20 74 65 72 6d 69 6e 61 74 65 64 0a \000 terminated.
1cb88 20 20 2a 2a 20 61 6e 64 20 68 61 76 65 20 61 6e ** and have an
1cb89 20 61 73 73 69 67 6e 65 64 20 74 79 70 65 2e 20 assigned type.
1cb8a 20 54 68 65 20 72 65 73 75 6c 74 73 20 61 72 65 The results are
1cb8b 20 64 65 2d 65 70 68 65 6d 65 72 61 6c 69 7a 65 de-ephemeralize
1cb8c 64 20 61 73 0a 20 20 2a 2a 20 61 73 20 73 69 64 d as. ** as sid
1cb8d 65 20 65 66 66 65 63 74 2e 0a 20 20 2a 2f 0a 20 e effect.. */.
1cb8e 20 75 2e 61 64 2e 70 4d 65 6d 20 3d 20 70 2d 3e u.ad.pMem = p->
1cb8f 70 52 65 73 75 6c 74 53 65 74 20 3d 20 26 70 2d pResultSet = &p-
1cb90 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a >aMem[pOp->p1];.
1cb91 20 20 66 6f 72 28 75 2e 61 64 2e 69 3d 30 3b 20 for(u.ad.i=0;
1cb92 75 2e 61 64 2e 69 3c 70 4f 70 2d 3e 70 32 3b 20 u.ad.i<pOp->p2;
1cb93 75 2e 61 64 2e 69 2b 2b 29 7b 0a 20 20 20 20 73 u.ad.i++){. s
1cb94 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 6c qlite3VdbeMemNul
1cb95 54 65 72 6d 69 6e 61 74 65 28 26 75 2e 61 64 2e Terminate(&u.ad.
1cb96 70 4d 65 6d 5b 75 2e 61 64 2e 69 5d 29 3b 0a 20 pMem[u.ad.i]);.
1cb97 20 20 20 73 74 6f 72 65 54 79 70 65 49 6e 66 6f storeTypeInfo
1cb98 28 26 75 2e 61 64 2e 70 4d 65 6d 5b 75 2e 61 64 (&u.ad.pMem[u.ad
1cb99 2e 69 5d 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a .i], encoding);.
1cb9a 20 20 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 REGISTER_TRA
1cb9b 43 45 28 70 4f 70 2d 3e 70 31 2b 75 2e 61 64 2e CE(pOp->p1+u.ad.
1cb9c 69 2c 20 26 75 2e 61 64 2e 70 4d 65 6d 5b 75 2e i, &u.ad.pMem[u.
1cb9d 61 64 2e 69 5d 29 3b 0a 20 20 7d 0a 20 20 69 66 ad.i]);. }. if
1cb9e 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c ( db->mallocFail
1cb9f 65 64 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d ed ) goto no_mem
1cba0 3b 0a 0a 20 20 2f 2a 20 52 65 74 75 72 6e 20 53 ;.. /* Return S
1cba1 51 4c 49 54 45 5f 52 4f 57 0a 20 20 2a 2f 0a 20 QLITE_ROW. */.
1cba2 20 70 2d 3e 70 63 20 3d 20 70 63 20 2b 20 31 3b p->pc = pc + 1;
1cba3 0a 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 52 . rc = SQLITE_R
1cba4 4f 57 3b 0a 20 20 67 6f 74 6f 20 76 64 62 65 5f OW;. goto vdbe_
1cba5 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 20 4f 70 return;.}../* Op
1cba6 63 6f 64 65 3a 20 43 6f 6e 63 61 74 20 50 31 20 code: Concat P1
1cba7 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 P2 P3 * *.**.**
1cba8 41 64 64 20 74 68 65 20 74 65 78 74 20 69 6e 20 Add the text in
1cba9 72 65 67 69 73 74 65 72 20 50 31 20 6f 6e 74 6f register P1 onto
1cbaa 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 the end of the
1cbab 74 65 78 74 20 69 6e 0a 2a 2a 20 72 65 67 69 73 text in.** regis
1cbac 74 65 72 20 50 32 20 61 6e 64 20 73 74 6f 72 65 ter P2 and store
1cbad 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20 72 the result in r
1cbae 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a 20 49 egister P3..** I
1cbaf 66 20 65 69 74 68 65 72 20 74 68 65 20 50 31 20 f either the P1
1cbb0 6f 72 20 50 32 20 74 65 78 74 20 61 72 65 20 4e or P2 text are N
1cbb1 55 4c 4c 20 74 68 65 6e 20 73 74 6f 72 65 20 4e ULL then store N
1cbb2 55 4c 4c 20 69 6e 20 50 33 2e 0a 2a 2a 0a 2a 2a ULL in P3..**.**
1cbb3 20 20 20 50 33 20 3d 20 50 32 20 7c 7c 20 50 31 P3 = P2 || P1
1cbb4 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 69 6c 6c .**.** It is ill
1cbb5 65 67 61 6c 20 66 6f 72 20 50 31 20 61 6e 64 20 egal for P1 and
1cbb6 50 33 20 74 6f 20 62 65 20 74 68 65 20 73 61 6d P3 to be the sam
1cbb7 65 20 72 65 67 69 73 74 65 72 2e 20 53 6f 6d 65 e register. Some
1cbb8 74 69 6d 65 73 2c 0a 2a 2a 20 69 66 20 50 33 20 times,.** if P3
1cbb9 69 73 20 74 68 65 20 73 61 6d 65 20 72 65 67 69 is the same regi
1cbba 73 74 65 72 20 61 73 20 50 32 2c 20 74 68 65 20 ster as P2, the
1cbbb 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 implementation i
1cbbc 73 20 61 62 6c 65 0a 2a 2a 20 74 6f 20 61 76 6f s able.** to avo
1cbbd 69 64 20 61 20 6d 65 6d 63 70 79 28 29 2e 0a 2a id a memcpy()..*
1cbbe 2f 0a 63 61 73 65 20 4f 50 5f 43 6f 6e 63 61 74 /.case OP_Concat
1cbbf 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 2f 2a : { /*
1cbc0 20 73 61 6d 65 20 61 73 20 54 4b 5f 43 4f 4e 43 same as TK_CONC
1cbc1 41 54 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 AT, in1, in2, ou
1cbc2 74 33 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 t3 */.#if 0 /*
1cbc3 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 local variables
1cbc4 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 65 20 moved into u.ae
1cbc5 2a 2f 0a 20 20 69 36 34 20 6e 42 79 74 65 3b 0a */. i64 nByte;.
1cbc6 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 #endif /* local
1cbc7 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 variables moved
1cbc8 69 6e 74 6f 20 75 2e 61 65 20 2a 2f 0a 0a 20 20 into u.ae */..
1cbc9 61 73 73 65 72 74 28 20 70 49 6e 31 21 3d 70 4f assert( pIn1!=pO
1cbca 75 74 20 29 3b 0a 20 20 69 66 28 20 28 70 49 6e ut );. if( (pIn
1cbcb 31 2d 3e 66 6c 61 67 73 20 7c 20 70 49 6e 32 2d 1->flags | pIn2-
1cbcc 3e 66 6c 61 67 73 29 20 26 20 4d 45 4d 5f 4e 75 >flags) & MEM_Nu
1cbcd 6c 6c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 ll ){. sqlite
1cbce 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 3VdbeMemSetNull(
1cbcf 70 4f 75 74 29 3b 0a 20 20 20 20 62 72 65 61 6b pOut);. break
1cbd0 3b 0a 20 20 7d 0a 20 20 69 66 28 20 45 78 70 61 ;. }. if( Expa
1cbd1 6e 64 42 6c 6f 62 28 70 49 6e 31 29 20 7c 7c 20 ndBlob(pIn1) ||
1cbd2 45 78 70 61 6e 64 42 6c 6f 62 28 70 49 6e 32 29 ExpandBlob(pIn2)
1cbd3 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a ) goto no_mem;.
1cbd4 20 20 53 74 72 69 6e 67 69 66 79 28 70 49 6e 31 Stringify(pIn1
1cbd5 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 53 , encoding);. S
1cbd6 74 72 69 6e 67 69 66 79 28 70 49 6e 32 2c 20 65 tringify(pIn2, e
1cbd7 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 75 2e 61 65 ncoding);. u.ae
1cbd8 2e 6e 42 79 74 65 20 3d 20 70 49 6e 31 2d 3e 6e .nByte = pIn1->n
1cbd9 20 2b 20 70 49 6e 32 2d 3e 6e 3b 0a 20 20 69 66 + pIn2->n;. if
1cbda 28 20 75 2e 61 65 2e 6e 42 79 74 65 3e 64 62 2d ( u.ae.nByte>db-
1cbdb 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c >aLimit[SQLITE_L
1cbdc 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a IMIT_LENGTH] ){.
1cbdd 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 goto too_big
1cbde 3b 0a 20 20 7d 0a 20 20 4d 65 6d 53 65 74 54 79 ;. }. MemSetTy
1cbdf 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d peFlag(pOut, MEM
1cbe0 5f 53 74 72 29 3b 0a 20 20 69 66 28 20 73 71 6c _Str);. if( sql
1cbe1 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 ite3VdbeMemGrow(
1cbe2 70 4f 75 74 2c 20 28 69 6e 74 29 75 2e 61 65 2e pOut, (int)u.ae.
1cbe3 6e 42 79 74 65 2b 32 2c 20 70 4f 75 74 3d 3d 70 nByte+2, pOut==p
1cbe4 49 6e 32 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f In2) ){. goto
1cbe5 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 20 69 no_mem;. }. i
1cbe6 66 28 20 70 4f 75 74 21 3d 70 49 6e 32 20 29 7b f( pOut!=pIn2 ){
1cbe7 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 4f 75 74 . memcpy(pOut
1cbe8 2d 3e 7a 2c 20 70 49 6e 32 2d 3e 7a 2c 20 70 49 ->z, pIn2->z, pI
1cbe9 6e 32 2d 3e 6e 29 3b 0a 20 20 7d 0a 20 20 6d 65 n2->n);. }. me
1cbea 6d 63 70 79 28 26 70 4f 75 74 2d 3e 7a 5b 70 49 mcpy(&pOut->z[pI
1cbeb 6e 32 2d 3e 6e 5d 2c 20 70 49 6e 31 2d 3e 7a 2c n2->n], pIn1->z,
1cbec 20 70 49 6e 31 2d 3e 6e 29 3b 0a 20 20 70 4f 75 pIn1->n);. pOu
1cbed 74 2d 3e 7a 5b 75 2e 61 65 2e 6e 42 79 74 65 5d t->z[u.ae.nByte]
1cbee 20 3d 20 30 3b 0a 20 20 70 4f 75 74 2d 3e 7a 5b = 0;. pOut->z[
1cbef 75 2e 61 65 2e 6e 42 79 74 65 2b 31 5d 20 3d 20 u.ae.nByte+1] =
1cbf0 30 3b 0a 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 0;. pOut->flags
1cbf1 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 |= MEM_Term;.
1cbf2 70 4f 75 74 2d 3e 6e 20 3d 20 28 69 6e 74 29 75 pOut->n = (int)u
1cbf3 2e 61 65 2e 6e 42 79 74 65 3b 0a 20 20 70 4f 75 .ae.nByte;. pOu
1cbf4 74 2d 3e 65 6e 63 20 3d 20 65 6e 63 6f 64 69 6e t->enc = encodin
1cbf5 67 3b 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f g;. UPDATE_MAX_
1cbf6 42 4c 4f 42 53 49 5a 45 28 70 4f 75 74 29 3b 0a BLOBSIZE(pOut);.
1cbf7 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f break;.}../* O
1cbf8 70 63 6f 64 65 3a 20 41 64 64 20 50 31 20 50 32 pcode: Add P1 P2
1cbf9 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41 64 P3 * *.**.** Ad
1cbfa 64 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 d the value in r
1cbfb 65 67 69 73 74 65 72 20 50 31 20 74 6f 20 74 68 egister P1 to th
1cbfc 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 e value in regis
1cbfd 74 65 72 20 50 32 0a 2a 2a 20 61 6e 64 20 73 74 ter P2.** and st
1cbfe 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 69 ore the result i
1cbff 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a n register P3..*
1cc00 2a 20 49 66 20 65 69 74 68 65 72 20 69 6e 70 75 * If either inpu
1cc01 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 t is NULL, the r
1cc02 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a esult is NULL..*
1cc03 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4d 75 6c /./* Opcode: Mul
1cc04 74 69 70 6c 79 20 50 31 20 50 32 20 50 33 20 2a tiply P1 P2 P3 *
1cc05 20 2a 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 4d 75 6c 74 *.**.**.** Mult
1cc06 69 70 6c 79 20 74 68 65 20 76 61 6c 75 65 20 69 iply the value i
1cc07 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 62 79 n register P1 by
1cc08 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 the value in re
1cc09 67 69 73 74 65 72 20 50 32 0a 2a 2a 20 61 6e 64 gister P2.** and
1cc0a 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c store the resul
1cc0b 74 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 t in register P3
1cc0c 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 69 ..** If either i
1cc0d 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 nput is NULL, th
1cc0e 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c e result is NULL
1cc0f 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ..*/./* Opcode:
1cc10 53 75 62 74 72 61 63 74 20 50 31 20 50 32 20 50 Subtract P1 P2 P
1cc11 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 75 62 74 3 * *.**.** Subt
1cc12 72 61 63 74 20 74 68 65 20 76 61 6c 75 65 20 69 ract the value i
1cc13 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 66 72 n register P1 fr
1cc14 6f 6d 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 om the value in
1cc15 72 65 67 69 73 74 65 72 20 50 32 0a 2a 2a 20 61 register P2.** a
1cc16 6e 64 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 nd store the res
1cc17 75 6c 74 20 69 6e 20 72 65 67 69 73 74 65 72 20 ult in register
1cc18 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 P3..** If either
1cc19 20 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2c 20 input is NULL,
1cc1a 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55 the result is NU
1cc1b 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 LL..*/./* Opcode
1cc1c 3a 20 44 69 76 69 64 65 20 50 31 20 50 32 20 50 : Divide P1 P2 P
1cc1d 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 44 69 76 69 3 * *.**.** Divi
1cc1e 64 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 de the value in
1cc1f 72 65 67 69 73 74 65 72 20 50 31 20 62 79 20 74 register P1 by t
1cc20 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 he value in regi
1cc21 73 74 65 72 20 50 32 0a 2a 2a 20 61 6e 64 20 73 ster P2.** and s
1cc22 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 tore the result
1cc23 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 20 28 in register P3 (
1cc24 50 33 3d 50 32 2f 50 31 29 2e 20 49 66 20 74 68 P3=P2/P1). If th
1cc25 65 20 76 61 6c 75 65 20 69 6e 20 0a 2a 2a 20 72 e value in .** r
1cc26 65 67 69 73 74 65 72 20 50 31 20 69 73 20 7a 65 egister P1 is ze
1cc27 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 72 65 73 ro, then the res
1cc28 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 20 49 66 20 ult is NULL. If
1cc29 65 69 74 68 65 72 20 69 6e 70 75 74 20 69 73 20 either input is
1cc2a 0a 2a 2a 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 .** NULL, the re
1cc2b 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f sult is NULL..*/
1cc2c 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 65 6d 61 ./* Opcode: Rema
1cc2d 69 6e 64 65 72 20 50 31 20 50 32 20 50 33 20 2a inder P1 P2 P3 *
1cc2e 20 2a 0a 2a 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 *.**.** Compute
1cc2f 20 74 68 65 20 72 65 6d 61 69 6e 64 65 72 20 61 the remainder a
1cc30 66 74 65 72 20 69 6e 74 65 67 65 72 20 64 69 76 fter integer div
1cc31 69 73 69 6f 6e 20 6f 66 20 74 68 65 20 76 61 6c ision of the val
1cc32 75 65 20 69 6e 0a 2a 2a 20 72 65 67 69 73 74 65 ue in.** registe
1cc33 72 20 50 31 20 62 79 20 74 68 65 20 76 61 6c 75 r P1 by the valu
1cc34 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 e in register P2
1cc35 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20 72 and store the r
1cc36 65 73 75 6c 74 20 69 6e 20 50 33 2e 20 0a 2a 2a esult in P3. .**
1cc37 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 69 6e If the value in
1cc38 20 72 65 67 69 73 74 65 72 20 50 32 20 69 73 20 register P2 is
1cc39 7a 65 72 6f 20 74 68 65 20 72 65 73 75 6c 74 20 zero the result
1cc3a 69 73 20 4e 55 4c 4c 2e 0a 2a 2a 20 49 66 20 65 is NULL..** If e
1cc3b 69 74 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73 ither operand is
1cc3c 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73 75 6c NULL, the resul
1cc3d 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 t is NULL..*/.ca
1cc3e 73 65 20 4f 50 5f 41 64 64 3a 20 20 20 20 20 20 se OP_Add:
1cc3f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1cc40 73 61 6d 65 20 61 73 20 54 4b 5f 50 4c 55 53 2c same as TK_PLUS,
1cc41 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 in1, in2, out3
1cc42 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 75 62 74 72 */.case OP_Subtr
1cc43 61 63 74 3a 20 20 20 20 20 20 20 20 20 20 20 20 act:
1cc44 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f /* same as TK_
1cc45 4d 49 4e 55 53 2c 20 69 6e 31 2c 20 69 6e 32 2c MINUS, in1, in2,
1cc46 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50 out3 */.case OP
1cc47 5f 4d 75 6c 74 69 70 6c 79 3a 20 20 20 20 20 20 _Multiply:
1cc48 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 /* same
1cc49 61 73 20 54 4b 5f 53 54 41 52 2c 20 69 6e 31 2c as TK_STAR, in1,
1cc4a 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 in2, out3 */.ca
1cc4b 73 65 20 4f 50 5f 44 69 76 69 64 65 3a 20 20 20 se OP_Divide:
1cc4c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1cc4d 73 61 6d 65 20 61 73 20 54 4b 5f 53 4c 41 53 48 same as TK_SLASH
1cc4e 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 , in1, in2, out3
1cc4f 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 65 6d 61 */.case OP_Rema
1cc50 69 6e 64 65 72 3a 20 7b 20 20 20 20 20 20 20 20 inder: {
1cc51 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b /* same as TK
1cc52 5f 52 45 4d 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 _REM, in1, in2,
1cc53 6f 75 74 33 20 2a 2f 0a 23 69 66 20 30 20 20 2f out3 */.#if 0 /
1cc54 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 * local variable
1cc55 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 s moved into u.a
1cc56 66 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 f */. int flags
1cc57 3b 20 20 20 20 20 20 2f 2a 20 43 6f 6d 62 69 6e ; /* Combin
1cc58 65 64 20 4d 45 4d 5f 2a 20 66 6c 61 67 73 20 66 ed MEM_* flags f
1cc59 72 6f 6d 20 62 6f 74 68 20 69 6e 70 75 74 73 20 rom both inputs
1cc5a 2a 2f 0a 20 20 69 36 34 20 69 41 3b 20 20 20 20 */. i64 iA;
1cc5b 20 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 /* Integer
1cc5c 76 61 6c 75 65 20 6f 66 20 6c 65 66 74 20 6f 70 value of left op
1cc5d 65 72 61 6e 64 20 2a 2f 0a 20 20 69 36 34 20 69 erand */. i64 i
1cc5e 42 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e B; /* In
1cc5f 74 65 67 65 72 20 76 61 6c 75 65 20 6f 66 20 72 teger value of r
1cc60 69 67 68 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a ight operand */.
1cc61 20 20 64 6f 75 62 6c 65 20 72 41 3b 20 20 20 20 double rA;
1cc62 20 20 2f 2a 20 52 65 61 6c 20 76 61 6c 75 65 20 /* Real value
1cc63 6f 66 20 6c 65 66 74 20 6f 70 65 72 61 6e 64 20 of left operand
1cc64 2a 2f 0a 20 20 64 6f 75 62 6c 65 20 72 42 3b 20 */. double rB;
1cc65 20 20 20 20 20 2f 2a 20 52 65 61 6c 20 76 61 6c /* Real val
1cc66 75 65 20 6f 66 20 72 69 67 68 74 20 6f 70 65 72 ue of right oper
1cc67 61 6e 64 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a and */.#endif /*
1cc68 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 local variables
1cc69 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 66 moved into u.af
1cc6a 20 2a 2f 0a 0a 20 20 61 70 70 6c 79 4e 75 6d 65 */.. applyNume
1cc6b 72 69 63 41 66 66 69 6e 69 74 79 28 70 49 6e 31 ricAffinity(pIn1
1cc6c 29 3b 0a 20 20 61 70 70 6c 79 4e 75 6d 65 72 69 );. applyNumeri
1cc6d 63 41 66 66 69 6e 69 74 79 28 70 49 6e 32 29 3b cAffinity(pIn2);
1cc6e 0a 20 20 75 2e 61 66 2e 66 6c 61 67 73 20 3d 20 . u.af.flags =
1cc6f 70 49 6e 31 2d 3e 66 6c 61 67 73 20 7c 20 70 49 pIn1->flags | pI
1cc70 6e 32 2d 3e 66 6c 61 67 73 3b 0a 20 20 69 66 28 n2->flags;. if(
1cc71 20 28 75 2e 61 66 2e 66 6c 61 67 73 20 26 20 4d (u.af.flags & M
1cc72 45 4d 5f 4e 75 6c 6c 29 21 3d 30 20 29 20 67 6f EM_Null)!=0 ) go
1cc73 74 6f 20 61 72 69 74 68 6d 65 74 69 63 5f 72 65 to arithmetic_re
1cc74 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3b 0a 20 20 sult_is_null;.
1cc75 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 if( (pIn1->flags
1cc76 20 26 20 70 49 6e 32 2d 3e 66 6c 61 67 73 20 26 & pIn2->flags &
1cc77 20 4d 45 4d 5f 49 6e 74 29 3d 3d 4d 45 4d 5f 49 MEM_Int)==MEM_I
1cc78 6e 74 20 29 7b 0a 20 20 20 20 75 2e 61 66 2e 69 nt ){. u.af.i
1cc79 41 20 3d 20 70 49 6e 31 2d 3e 75 2e 69 3b 0a 20 A = pIn1->u.i;.
1cc7a 20 20 20 75 2e 61 66 2e 69 42 20 3d 20 70 49 6e u.af.iB = pIn
1cc7b 32 2d 3e 75 2e 69 3b 0a 20 20 20 20 73 77 69 74 2->u.i;. swit
1cc7c 63 68 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 ch( pOp->opcode
1cc7d 29 7b 0a 20 20 20 20 20 20 63 61 73 65 20 4f 50 ){. case OP
1cc7e 5f 41 64 64 3a 20 20 20 20 20 20 20 20 20 75 2e _Add: u.
1cc7f 61 66 2e 69 42 20 2b 3d 20 75 2e 61 66 2e 69 41 af.iB += u.af.iA
1cc80 3b 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ; break;.
1cc81 20 20 20 20 20 63 61 73 65 20 4f 50 5f 53 75 62 case OP_Sub
1cc82 74 72 61 63 74 3a 20 20 20 20 75 2e 61 66 2e 69 tract: u.af.i
1cc83 42 20 2d 3d 20 75 2e 61 66 2e 69 41 3b 20 20 20 B -= u.af.iA;
1cc84 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
1cc85 20 63 61 73 65 20 4f 50 5f 4d 75 6c 74 69 70 6c case OP_Multipl
1cc86 79 3a 20 20 20 20 75 2e 61 66 2e 69 42 20 2a 3d y: u.af.iB *=
1cc87 20 75 2e 61 66 2e 69 41 3b 20 20 20 20 20 20 20 u.af.iA;
1cc88 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 break;. cas
1cc89 65 20 4f 50 5f 44 69 76 69 64 65 3a 20 7b 0a 20 e OP_Divide: {.
1cc8a 20 20 20 20 20 20 20 69 66 28 20 75 2e 61 66 2e if( u.af.
1cc8b 69 41 3d 3d 30 20 29 20 67 6f 74 6f 20 61 72 69 iA==0 ) goto ari
1cc8c 74 68 6d 65 74 69 63 5f 72 65 73 75 6c 74 5f 69 thmetic_result_i
1cc8d 73 5f 6e 75 6c 6c 3b 0a 20 20 20 20 20 20 20 20 s_null;.
1cc8e 2f 2a 20 44 69 76 69 64 69 6e 67 20 74 68 65 20 /* Dividing the
1cc8f 6c 61 72 67 65 73 74 20 70 6f 73 73 69 62 6c 65 largest possible
1cc90 20 6e 65 67 61 74 69 76 65 20 36 34 2d 62 69 74 negative 64-bit
1cc91 20 69 6e 74 65 67 65 72 20 28 31 3c 3c 36 33 29 integer (1<<63)
1cc92 20 62 79 0a 20 20 20 20 20 20 20 20 2a 2a 20 2d by. ** -
1cc93 31 20 72 65 74 75 72 6e 73 20 61 6e 20 69 6e 74 1 returns an int
1cc94 65 67 65 72 20 74 6f 6f 20 6c 61 72 67 65 20 74 eger too large t
1cc95 6f 20 73 74 6f 72 65 20 69 6e 20 61 20 36 34 2d o store in a 64-
1cc96 62 69 74 20 64 61 74 61 2d 74 79 70 65 2e 20 4f bit data-type. O
1cc97 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 6f 6d n. ** som
1cc98 65 20 61 72 63 68 69 74 65 63 74 75 72 65 73 2c e architectures,
1cc99 20 74 68 65 20 76 61 6c 75 65 20 6f 76 65 72 66 the value overf
1cc9a 6c 6f 77 73 20 74 6f 20 28 31 3c 3c 36 33 29 2e lows to (1<<63).
1cc9b 20 4f 6e 20 6f 74 68 65 72 73 2c 0a 20 20 20 20 On others,.
1cc9c 20 20 20 20 2a 2a 20 61 20 53 49 47 46 50 45 20 ** a SIGFPE
1cc9d 69 73 20 69 73 73 75 65 64 2e 20 54 68 65 20 66 is issued. The f
1cc9e 6f 6c 6c 6f 77 69 6e 67 20 73 74 61 74 65 6d 65 ollowing stateme
1cc9f 6e 74 20 6e 6f 72 6d 61 6c 69 7a 65 73 20 74 68 nt normalizes th
1cca0 69 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 62 65 is. ** be
1cca1 68 61 76 69 6f 72 20 73 6f 20 74 68 61 74 20 61 havior so that a
1cca2 6c 6c 20 61 72 63 68 69 74 65 63 74 75 72 65 73 ll architectures
1cca3 20 62 65 68 61 76 65 20 61 73 20 69 66 20 69 6e behave as if in
1cca4 74 65 67 65 72 0a 20 20 20 20 20 20 20 20 2a 2a teger. **
1cca5 20 6f 76 65 72 66 6c 6f 77 20 6f 63 63 75 72 72 overflow occurr
1cca6 65 64 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 ed.. */.
1cca7 20 20 20 20 20 20 20 69 66 28 20 75 2e 61 66 2e if( u.af.
1cca8 69 41 3d 3d 2d 31 20 26 26 20 75 2e 61 66 2e 69 iA==-1 && u.af.i
1cca9 42 3d 3d 53 4d 41 4c 4c 45 53 54 5f 49 4e 54 36 B==SMALLEST_INT6
1ccaa 34 20 29 20 75 2e 61 66 2e 69 41 20 3d 20 31 3b 4 ) u.af.iA = 1;
1ccab 0a 20 20 20 20 20 20 20 20 75 2e 61 66 2e 69 42 . u.af.iB
1ccac 20 2f 3d 20 75 2e 61 66 2e 69 41 3b 0a 20 20 20 /= u.af.iA;.
1ccad 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
1ccae 20 20 7d 0a 20 20 20 20 20 20 64 65 66 61 75 6c }. defaul
1ccaf 74 3a 20 7b 0a 20 20 20 20 20 20 20 20 69 66 28 t: {. if(
1ccb0 20 75 2e 61 66 2e 69 41 3d 3d 30 20 29 20 67 6f u.af.iA==0 ) go
1ccb1 74 6f 20 61 72 69 74 68 6d 65 74 69 63 5f 72 65 to arithmetic_re
1ccb2 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3b 0a 20 20 sult_is_null;.
1ccb3 20 20 20 20 20 20 69 66 28 20 75 2e 61 66 2e 69 if( u.af.i
1ccb4 41 3d 3d 2d 31 20 29 20 75 2e 61 66 2e 69 41 20 A==-1 ) u.af.iA
1ccb5 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 75 2e 61 = 1;. u.a
1ccb6 66 2e 69 42 20 25 3d 20 75 2e 61 66 2e 69 41 3b f.iB %= u.af.iA;
1ccb7 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a . break;.
1ccb8 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
1ccb9 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 2e pOut->u.i = u.
1ccba 61 66 2e 69 42 3b 0a 20 20 20 20 4d 65 6d 53 65 af.iB;. MemSe
1ccbb 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 tTypeFlag(pOut,
1ccbc 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 7d 65 6c 73 MEM_Int);. }els
1ccbd 65 7b 0a 20 20 20 20 75 2e 61 66 2e 72 41 20 3d e{. u.af.rA =
1ccbe 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 61 6c sqlite3VdbeReal
1ccbf 56 61 6c 75 65 28 70 49 6e 31 29 3b 0a 20 20 20 Value(pIn1);.
1ccc0 20 75 2e 61 66 2e 72 42 20 3d 20 73 71 6c 69 74 u.af.rB = sqlit
1ccc1 65 33 56 64 62 65 52 65 61 6c 56 61 6c 75 65 28 e3VdbeRealValue(
1ccc2 70 49 6e 32 29 3b 0a 20 20 20 20 73 77 69 74 63 pIn2);. switc
1ccc3 68 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 29 h( pOp->opcode )
1ccc4 7b 0a 20 20 20 20 20 20 63 61 73 65 20 4f 50 5f {. case OP_
1ccc5 41 64 64 3a 20 20 20 20 20 20 20 20 20 75 2e 61 Add: u.a
1ccc6 66 2e 72 42 20 2b 3d 20 75 2e 61 66 2e 72 41 3b f.rB += u.af.rA;
1ccc7 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
1ccc8 20 20 20 20 63 61 73 65 20 4f 50 5f 53 75 62 74 case OP_Subt
1ccc9 72 61 63 74 3a 20 20 20 20 75 2e 61 66 2e 72 42 ract: u.af.rB
1ccca 20 2d 3d 20 75 2e 61 66 2e 72 41 3b 20 20 20 20 -= u.af.rA;
1cccb 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
1cccc 63 61 73 65 20 4f 50 5f 4d 75 6c 74 69 70 6c 79 case OP_Multiply
1cccd 3a 20 20 20 20 75 2e 61 66 2e 72 42 20 2a 3d 20 : u.af.rB *=
1ccce 75 2e 61 66 2e 72 41 3b 20 20 20 20 20 20 20 62 u.af.rA; b
1cccf 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 reak;. case
1ccd0 20 4f 50 5f 44 69 76 69 64 65 3a 20 7b 0a 20 20 OP_Divide: {.
1ccd1 20 20 20 20 20 20 2f 2a 20 28 64 6f 75 62 6c 65 /* (double
1ccd2 29 30 20 49 6e 20 63 61 73 65 20 6f 66 20 53 51 )0 In case of SQ
1ccd3 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 LITE_OMIT_FLOATI
1ccd4 4e 47 5f 50 4f 49 4e 54 2e 2e 2e 20 2a 2f 0a 20 NG_POINT... */.
1ccd5 20 20 20 20 20 20 20 69 66 28 20 75 2e 61 66 2e if( u.af.
1ccd6 72 41 3d 3d 28 64 6f 75 62 6c 65 29 30 20 29 20 rA==(double)0 )
1ccd7 67 6f 74 6f 20 61 72 69 74 68 6d 65 74 69 63 5f goto arithmetic_
1ccd8 72 65 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3b 0a result_is_null;.
1ccd9 20 20 20 20 20 20 20 20 75 2e 61 66 2e 72 42 20 u.af.rB
1ccda 2f 3d 20 75 2e 61 66 2e 72 41 3b 0a 20 20 20 20 /= u.af.rA;.
1ccdb 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
1ccdc 20 7d 0a 20 20 20 20 20 20 64 65 66 61 75 6c 74 }. default
1ccdd 3a 20 7b 0a 20 20 20 20 20 20 20 20 75 2e 61 66 : {. u.af
1ccde 2e 69 41 20 3d 20 28 69 36 34 29 75 2e 61 66 2e .iA = (i64)u.af.
1ccdf 72 41 3b 0a 20 20 20 20 20 20 20 20 75 2e 61 66 rA;. u.af
1cce0 2e 69 42 20 3d 20 28 69 36 34 29 75 2e 61 66 2e .iB = (i64)u.af.
1cce1 72 42 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 rB;. if(
1cce2 75 2e 61 66 2e 69 41 3d 3d 30 20 29 20 67 6f 74 u.af.iA==0 ) got
1cce3 6f 20 61 72 69 74 68 6d 65 74 69 63 5f 72 65 73 o arithmetic_res
1cce4 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3b 0a 20 20 20 ult_is_null;.
1cce5 20 20 20 20 20 69 66 28 20 75 2e 61 66 2e 69 41 if( u.af.iA
1cce6 3d 3d 2d 31 20 29 20 75 2e 61 66 2e 69 41 20 3d ==-1 ) u.af.iA =
1cce7 20 31 3b 0a 20 20 20 20 20 20 20 20 75 2e 61 66 1;. u.af
1cce8 2e 72 42 20 3d 20 28 64 6f 75 62 6c 65 29 28 75 .rB = (double)(u
1cce9 2e 61 66 2e 69 42 20 25 20 75 2e 61 66 2e 69 41 .af.iB % u.af.iA
1ccea 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b );. break
1cceb 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
1ccec 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 49 if( sqlite3I
1cced 73 4e 61 4e 28 75 2e 61 66 2e 72 42 29 20 29 7b sNaN(u.af.rB) ){
1ccee 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 72 69 74 . goto arit
1ccef 68 6d 65 74 69 63 5f 72 65 73 75 6c 74 5f 69 73 hmetic_result_is
1ccf0 5f 6e 75 6c 6c 3b 0a 20 20 20 20 7d 0a 20 20 20 _null;. }.
1ccf1 20 70 4f 75 74 2d 3e 72 20 3d 20 75 2e 61 66 2e pOut->r = u.af.
1ccf2 72 42 3b 0a 20 20 20 20 4d 65 6d 53 65 74 54 79 rB;. MemSetTy
1ccf3 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d peFlag(pOut, MEM
1ccf4 5f 52 65 61 6c 29 3b 0a 20 20 20 20 69 66 28 20 _Real);. if(
1ccf5 28 75 2e 61 66 2e 66 6c 61 67 73 20 26 20 4d 45 (u.af.flags & ME
1ccf6 4d 5f 52 65 61 6c 29 3d 3d 30 20 29 7b 0a 20 20 M_Real)==0 ){.
1ccf7 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 49 sqlite3VdbeI
1ccf8 6e 74 65 67 65 72 41 66 66 69 6e 69 74 79 28 70 ntegerAffinity(p
1ccf9 4f 75 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a Out);. }. }.
1ccfa 20 20 62 72 65 61 6b 3b 0a 0a 61 72 69 74 68 6d break;..arithm
1ccfb 65 74 69 63 5f 72 65 73 75 6c 74 5f 69 73 5f 6e etic_result_is_n
1ccfc 75 6c 6c 3a 0a 20 20 73 71 6c 69 74 65 33 56 64 ull:. sqlite3Vd
1ccfd 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 4f 75 beMemSetNull(pOu
1ccfe 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a t);. break;.}..
1ccff 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6f 6c 6c 53 /* Opcode: CollS
1cd00 65 71 20 2a 20 2a 20 50 34 0a 2a 2a 0a 2a 2a 20 eq * * P4.**.**
1cd01 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 P4 is a pointer
1cd02 74 6f 20 61 20 43 6f 6c 6c 53 65 71 20 73 74 72 to a CollSeq str
1cd03 75 63 74 2e 20 49 66 20 74 68 65 20 6e 65 78 74 uct. If the next
1cd04 20 63 61 6c 6c 20 74 6f 20 61 20 75 73 65 72 20 call to a user
1cd05 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 6f 72 20 61 function.** or a
1cd06 67 67 72 65 67 61 74 65 20 63 61 6c 6c 73 20 73 ggregate calls s
1cd07 71 6c 69 74 65 33 47 65 74 46 75 6e 63 43 6f 6c qlite3GetFuncCol
1cd08 6c 53 65 71 28 29 2c 20 74 68 69 73 20 63 6f 6c lSeq(), this col
1cd09 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 lation sequence
1cd0a 77 69 6c 6c 0a 2a 2a 20 62 65 20 72 65 74 75 72 will.** be retur
1cd0b 6e 65 64 2e 20 54 68 69 73 20 69 73 20 75 73 65 ned. This is use
1cd0c 64 20 62 79 20 74 68 65 20 62 75 69 6c 74 2d 69 d by the built-i
1cd0d 6e 20 6d 69 6e 28 29 2c 20 6d 61 78 28 29 20 61 n min(), max() a
1cd0e 6e 64 20 6e 75 6c 6c 69 66 28 29 0a 2a 2a 20 66 nd nullif().** f
1cd0f 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 unctions..**.**
1cd10 54 68 65 20 69 6e 74 65 72 66 61 63 65 20 75 73 The interface us
1cd11 65 64 20 62 79 20 74 68 65 20 69 6d 70 6c 65 6d ed by the implem
1cd12 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 entation of the
1cd13 61 66 6f 72 65 6d 65 6e 74 69 6f 6e 65 64 20 66 aforementioned f
1cd14 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 74 6f 20 72 unctions.** to r
1cd15 65 74 72 69 65 76 65 20 74 68 65 20 63 6f 6c 6c etrieve the coll
1cd16 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 73 ation sequence s
1cd17 65 74 20 62 79 20 74 68 69 73 20 6f 70 63 6f 64 et by this opcod
1cd18 65 20 69 73 20 6e 6f 74 20 61 76 61 69 6c 61 62 e is not availab
1cd19 6c 65 0a 2a 2a 20 70 75 62 6c 69 63 6c 79 2c 20 le.** publicly,
1cd1a 6f 6e 6c 79 20 74 6f 20 75 73 65 72 20 66 75 6e only to user fun
1cd1b 63 74 69 6f 6e 73 20 64 65 66 69 6e 65 64 20 69 ctions defined i
1cd1c 6e 20 66 75 6e 63 2e 63 2e 0a 2a 2f 0a 63 61 73 n func.c..*/.cas
1cd1d 65 20 4f 50 5f 43 6f 6c 6c 53 65 71 3a 20 7b 0a e OP_CollSeq: {.
1cd1e 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
1cd1f 34 74 79 70 65 3d 3d 50 34 5f 43 4f 4c 4c 53 45 4type==P4_COLLSE
1cd20 51 20 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a Q );. break;.}.
1cd21 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 46 75 6e 63 ./* Opcode: Func
1cd22 74 69 6f 6e 20 50 31 20 50 32 20 50 33 20 50 34 tion P1 P2 P3 P4
1cd23 20 50 35 0a 2a 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 P5.**.** Invoke
1cd24 20 61 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e a user function
1cd25 20 28 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 (P4 is a pointe
1cd26 72 20 74 6f 20 61 20 46 75 6e 63 74 69 6f 6e 20 r to a Function
1cd27 73 74 72 75 63 74 75 72 65 20 74 68 61 74 0a 2a structure that.*
1cd28 2a 20 64 65 66 69 6e 65 73 20 74 68 65 20 66 75 * defines the fu
1cd29 6e 63 74 69 6f 6e 29 20 77 69 74 68 20 50 35 20 nction) with P5
1cd2a 61 72 67 75 6d 65 6e 74 73 20 74 61 6b 65 6e 20 arguments taken
1cd2b 66 72 6f 6d 20 72 65 67 69 73 74 65 72 20 50 32 from register P2
1cd2c 20 61 6e 64 0a 2a 2a 20 73 75 63 63 65 73 73 6f and.** successo
1cd2d 72 73 2e 20 20 54 68 65 20 72 65 73 75 6c 74 20 rs. The result
1cd2e 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 of the function
1cd2f 69 73 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 is stored in reg
1cd30 69 73 74 65 72 20 50 33 2e 0a 2a 2a 20 52 65 67 ister P3..** Reg
1cd31 69 73 74 65 72 20 50 33 20 6d 75 73 74 20 6e 6f ister P3 must no
1cd32 74 20 62 65 20 6f 6e 65 20 6f 66 20 74 68 65 20 t be one of the
1cd33 66 75 6e 63 74 69 6f 6e 20 69 6e 70 75 74 73 2e function inputs.
1cd34 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 61 20 33 .**.** P1 is a 3
1cd35 32 2d 62 69 74 20 62 69 74 6d 61 73 6b 20 69 6e 2-bit bitmask in
1cd36 64 69 63 61 74 69 6e 67 20 77 68 65 74 68 65 72 dicating whether
1cd37 20 6f 72 20 6e 6f 74 20 65 61 63 68 20 61 72 67 or not each arg
1cd38 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 0a 2a 2a ument to the .**
1cd39 20 66 75 6e 63 74 69 6f 6e 20 77 61 73 20 64 65 function was de
1cd3a 74 65 72 6d 69 6e 65 64 20 74 6f 20 62 65 20 63 termined to be c
1cd3b 6f 6e 73 74 61 6e 74 20 61 74 20 63 6f 6d 70 69 onstant at compi
1cd3c 6c 65 20 74 69 6d 65 2e 20 49 66 20 74 68 65 20 le time. If the
1cd3d 66 69 72 73 74 0a 2a 2a 20 61 72 67 75 6d 65 6e first.** argumen
1cd3e 74 20 77 61 73 20 63 6f 6e 73 74 61 6e 74 20 74 t was constant t
1cd3f 68 65 6e 20 62 69 74 20 30 20 6f 66 20 50 31 20 hen bit 0 of P1
1cd40 69 73 20 73 65 74 2e 20 54 68 69 73 20 69 73 20 is set. This is
1cd41 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e used to determin
1cd42 65 0a 2a 2a 20 77 68 65 74 68 65 72 20 6d 65 74 e.** whether met
1cd43 61 20 64 61 74 61 20 61 73 73 6f 63 69 61 74 65 a data associate
1cd44 64 20 77 69 74 68 20 61 20 75 73 65 72 20 66 75 d with a user fu
1cd45 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 20 nction argument
1cd46 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 73 71 6c using the.** sql
1cd47 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61 74 61 ite3_set_auxdata
1cd48 28 29 20 41 50 49 20 6d 61 79 20 62 65 20 73 61 () API may be sa
1cd49 66 65 6c 79 20 72 65 74 61 69 6e 65 64 20 75 6e fely retained un
1cd4a 74 69 6c 20 74 68 65 20 6e 65 78 74 0a 2a 2a 20 til the next.**
1cd4b 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20 74 68 invocation of th
1cd4c 69 73 20 6f 70 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a is opcode..**.**
1cd4d 20 53 65 65 20 61 6c 73 6f 3a 20 41 67 67 53 74 See also: AggSt
1cd4e 65 70 20 61 6e 64 20 41 67 67 46 69 6e 61 6c 0a ep and AggFinal.
1cd4f 2a 2f 0a 63 61 73 65 20 4f 50 5f 46 75 6e 63 74 */.case OP_Funct
1cd50 69 6f 6e 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a ion: {.#if 0 /*
1cd51 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 local variables
1cd52 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 67 moved into u.ag
1cd53 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 0a 20 20 4d */. int i;. M
1cd54 65 6d 20 2a 70 41 72 67 3b 0a 20 20 73 71 6c 69 em *pArg;. sqli
1cd55 74 65 33 5f 63 6f 6e 74 65 78 74 20 63 74 78 3b te3_context ctx;
1cd56 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 . sqlite3_value
1cd57 20 2a 2a 61 70 56 61 6c 3b 0a 20 20 69 6e 74 20 **apVal;. int
1cd58 6e 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 n;.#endif /* loc
1cd59 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1cd5a 65 64 20 69 6e 74 6f 20 75 2e 61 67 20 2a 2f 0a ed into u.ag */.
1cd5b 0a 20 20 75 2e 61 67 2e 6e 20 3d 20 70 4f 70 2d . u.ag.n = pOp-
1cd5c 3e 70 35 3b 0a 20 20 75 2e 61 67 2e 61 70 56 61 >p5;. u.ag.apVa
1cd5d 6c 20 3d 20 70 2d 3e 61 70 41 72 67 3b 0a 20 20 l = p->apArg;.
1cd5e 61 73 73 65 72 74 28 20 75 2e 61 67 2e 61 70 56 assert( u.ag.apV
1cd5f 61 6c 20 7c 7c 20 75 2e 61 67 2e 6e 3d 3d 30 20 al || u.ag.n==0
1cd60 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 75 2e );.. assert( u.
1cd61 61 67 2e 6e 3d 3d 30 20 7c 7c 20 28 70 4f 70 2d ag.n==0 || (pOp-
1cd62 3e 70 32 3e 30 20 26 26 20 70 4f 70 2d 3e 70 32 >p2>0 && pOp->p2
1cd63 2b 75 2e 61 67 2e 6e 3c 3d 70 2d 3e 6e 4d 65 6d +u.ag.n<=p->nMem
1cd64 2b 31 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 +1) );. assert(
1cd65 20 70 4f 70 2d 3e 70 33 3c 70 4f 70 2d 3e 70 32 pOp->p3<pOp->p2
1cd66 20 7c 7c 20 70 4f 70 2d 3e 70 33 3e 3d 70 4f 70 || pOp->p3>=pOp
1cd67 2d 3e 70 32 2b 75 2e 61 67 2e 6e 20 29 3b 0a 20 ->p2+u.ag.n );.
1cd68 20 75 2e 61 67 2e 70 41 72 67 20 3d 20 26 70 2d u.ag.pArg = &p-
1cd69 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a >aMem[pOp->p2];.
1cd6a 20 20 66 6f 72 28 75 2e 61 67 2e 69 3d 30 3b 20 for(u.ag.i=0;
1cd6b 75 2e 61 67 2e 69 3c 75 2e 61 67 2e 6e 3b 20 75 u.ag.i<u.ag.n; u
1cd6c 2e 61 67 2e 69 2b 2b 2c 20 75 2e 61 67 2e 70 41 .ag.i++, u.ag.pA
1cd6d 72 67 2b 2b 29 7b 0a 20 20 20 20 75 2e 61 67 2e rg++){. u.ag.
1cd6e 61 70 56 61 6c 5b 75 2e 61 67 2e 69 5d 20 3d 20 apVal[u.ag.i] =
1cd6f 75 2e 61 67 2e 70 41 72 67 3b 0a 20 20 20 20 73 u.ag.pArg;. s
1cd70 74 6f 72 65 54 79 70 65 49 6e 66 6f 28 75 2e 61 toreTypeInfo(u.a
1cd71 67 2e 70 41 72 67 2c 20 65 6e 63 6f 64 69 6e 67 g.pArg, encoding
1cd72 29 3b 0a 20 20 20 20 52 45 47 49 53 54 45 52 5f );. REGISTER_
1cd73 54 52 41 43 45 28 70 4f 70 2d 3e 70 32 2c 20 75 TRACE(pOp->p2, u
1cd74 2e 61 67 2e 70 41 72 67 29 3b 0a 20 20 7d 0a 0a .ag.pArg);. }..
1cd75 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
1cd76 34 74 79 70 65 3d 3d 50 34 5f 46 55 4e 43 44 45 4type==P4_FUNCDE
1cd77 46 20 7c 7c 20 70 4f 70 2d 3e 70 34 74 79 70 65 F || pOp->p4type
1cd78 3d 3d 50 34 5f 56 44 42 45 46 55 4e 43 20 29 3b ==P4_VDBEFUNC );
1cd79 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 34 74 79 . if( pOp->p4ty
1cd7a 70 65 3d 3d 50 34 5f 46 55 4e 43 44 45 46 20 29 pe==P4_FUNCDEF )
1cd7b 7b 0a 20 20 20 20 75 2e 61 67 2e 63 74 78 2e 70 {. u.ag.ctx.p
1cd7c 46 75 6e 63 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 Func = pOp->p4.p
1cd7d 46 75 6e 63 3b 0a 20 20 20 20 75 2e 61 67 2e 63 Func;. u.ag.c
1cd7e 74 78 2e 70 56 64 62 65 46 75 6e 63 20 3d 20 30 tx.pVdbeFunc = 0
1cd7f 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 ;. }else{. u
1cd80 2e 61 67 2e 63 74 78 2e 70 56 64 62 65 46 75 6e .ag.ctx.pVdbeFun
1cd81 63 20 3d 20 28 56 64 62 65 46 75 6e 63 2a 29 70 c = (VdbeFunc*)p
1cd82 4f 70 2d 3e 70 34 2e 70 56 64 62 65 46 75 6e 63 Op->p4.pVdbeFunc
1cd83 3b 0a 20 20 20 20 75 2e 61 67 2e 63 74 78 2e 70 ;. u.ag.ctx.p
1cd84 46 75 6e 63 20 3d 20 75 2e 61 67 2e 63 74 78 2e Func = u.ag.ctx.
1cd85 70 56 64 62 65 46 75 6e 63 2d 3e 70 46 75 6e 63 pVdbeFunc->pFunc
1cd86 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 ;. }.. assert(
1cd87 20 70 4f 70 2d 3e 70 33 3e 30 20 26 26 20 70 4f pOp->p3>0 && pO
1cd88 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 p->p3<=p->nMem )
1cd89 3b 0a 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 ;. pOut = &p->a
1cd8a 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 Mem[pOp->p3];.
1cd8b 75 2e 61 67 2e 63 74 78 2e 73 2e 66 6c 61 67 73 u.ag.ctx.s.flags
1cd8c 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 75 = MEM_Null;. u
1cd8d 2e 61 67 2e 63 74 78 2e 73 2e 64 62 20 3d 20 64 .ag.ctx.s.db = d
1cd8e 62 3b 0a 20 20 75 2e 61 67 2e 63 74 78 2e 73 2e b;. u.ag.ctx.s.
1cd8f 78 44 65 6c 20 3d 20 30 3b 0a 20 20 75 2e 61 67 xDel = 0;. u.ag
1cd90 2e 63 74 78 2e 73 2e 7a 4d 61 6c 6c 6f 63 20 3d .ctx.s.zMalloc =
1cd91 20 30 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 6f 75 0;.. /* The ou
1cd92 74 70 75 74 20 63 65 6c 6c 20 6d 61 79 20 61 6c tput cell may al
1cd93 72 65 61 64 79 20 68 61 76 65 20 61 20 62 75 66 ready have a buf
1cd94 66 65 72 20 61 6c 6c 6f 63 61 74 65 64 2e 20 4d fer allocated. M
1cd95 6f 76 65 0a 20 20 2a 2a 20 74 68 65 20 70 6f 69 ove. ** the poi
1cd96 6e 74 65 72 20 74 6f 20 75 2e 61 67 2e 63 74 78 nter to u.ag.ctx
1cd97 2e 73 20 73 6f 20 69 6e 20 63 61 73 65 20 74 68 .s so in case th
1cd98 65 20 75 73 65 72 2d 66 75 6e 63 74 69 6f 6e 20 e user-function
1cd99 63 61 6e 20 75 73 65 0a 20 20 2a 2a 20 74 68 65 can use. ** the
1cd9a 20 61 6c 72 65 61 64 79 20 61 6c 6c 6f 63 61 74 already allocat
1cd9b 65 64 20 62 75 66 66 65 72 20 69 6e 73 74 65 61 ed buffer instea
1cd9c 64 20 6f 66 20 61 6c 6c 6f 63 61 74 69 6e 67 20 d of allocating
1cd9d 61 20 6e 65 77 20 6f 6e 65 2e 0a 20 20 2a 2f 0a a new one.. */.
1cd9e 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1cd9f 4d 6f 76 65 28 26 75 2e 61 67 2e 63 74 78 2e 73 Move(&u.ag.ctx.s
1cda0 2c 20 70 4f 75 74 29 3b 0a 20 20 4d 65 6d 53 65 , pOut);. MemSe
1cda1 74 54 79 70 65 46 6c 61 67 28 26 75 2e 61 67 2e tTypeFlag(&u.ag.
1cda2 63 74 78 2e 73 2c 20 4d 45 4d 5f 4e 75 6c 6c 29 ctx.s, MEM_Null)
1cda3 3b 0a 0a 20 20 75 2e 61 67 2e 63 74 78 2e 69 73 ;.. u.ag.ctx.is
1cda4 45 72 72 6f 72 20 3d 20 30 3b 0a 20 20 69 66 28 Error = 0;. if(
1cda5 20 75 2e 61 67 2e 63 74 78 2e 70 46 75 6e 63 2d u.ag.ctx.pFunc-
1cda6 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f >flags & SQLITE_
1cda7 46 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c 20 29 7b FUNC_NEEDCOLL ){
1cda8 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 . assert( pOp
1cda9 3e 70 2d 3e 61 4f 70 20 29 3b 0a 20 20 20 20 61 >p->aOp );. a
1cdaa 73 73 65 72 74 28 20 70 4f 70 5b 2d 31 5d 2e 70 ssert( pOp[-1].p
1cdab 34 74 79 70 65 3d 3d 50 34 5f 43 4f 4c 4c 53 45 4type==P4_COLLSE
1cdac 51 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 Q );. assert(
1cdad 20 70 4f 70 5b 2d 31 5d 2e 6f 70 63 6f 64 65 3d pOp[-1].opcode=
1cdae 3d 4f 50 5f 43 6f 6c 6c 53 65 71 20 29 3b 0a 20 =OP_CollSeq );.
1cdaf 20 20 20 75 2e 61 67 2e 63 74 78 2e 70 43 6f 6c u.ag.ctx.pCol
1cdb0 6c 20 3d 20 70 4f 70 5b 2d 31 5d 2e 70 34 2e 70 l = pOp[-1].p4.p
1cdb1 43 6f 6c 6c 3b 0a 20 20 7d 0a 20 20 69 66 28 20 Coll;. }. if(
1cdb2 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 sqlite3SafetyOff
1cdb3 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 (db) ) goto abor
1cdb4 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b t_due_to_misuse;
1cdb5 0a 20 20 28 2a 75 2e 61 67 2e 63 74 78 2e 70 46 . (*u.ag.ctx.pF
1cdb6 75 6e 63 2d 3e 78 46 75 6e 63 29 28 26 75 2e 61 unc->xFunc)(&u.a
1cdb7 67 2e 63 74 78 2c 20 75 2e 61 67 2e 6e 2c 20 75 g.ctx, u.ag.n, u
1cdb8 2e 61 67 2e 61 70 56 61 6c 29 3b 0a 20 20 69 66 .ag.apVal);. if
1cdb9 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f ( sqlite3SafetyO
1cdba 6e 28 64 62 29 20 29 7b 0a 20 20 20 20 73 71 6c n(db) ){. sql
1cdbb 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 ite3VdbeMemRelea
1cdbc 73 65 28 26 75 2e 61 67 2e 63 74 78 2e 73 29 3b se(&u.ag.ctx.s);
1cdbd 0a 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f . goto abort_
1cdbe 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 due_to_misuse;.
1cdbf 20 7d 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c }. if( db->mal
1cdc0 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 locFailed ){.
1cdc1 20 2f 2a 20 45 76 65 6e 20 74 68 6f 75 67 68 20 /* Even though
1cdc2 61 20 6d 61 6c 6c 6f 63 28 29 20 68 61 73 20 66 a malloc() has f
1cdc3 61 69 6c 65 64 2c 20 74 68 65 20 69 6d 70 6c 65 ailed, the imple
1cdc4 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 mentation of the
1cdc5 0a 20 20 20 20 2a 2a 20 75 73 65 72 20 66 75 6e . ** user fun
1cdc6 63 74 69 6f 6e 20 6d 61 79 20 68 61 76 65 20 63 ction may have c
1cdc7 61 6c 6c 65 64 20 61 6e 20 73 71 6c 69 74 65 33 alled an sqlite3
1cdc8 5f 72 65 73 75 6c 74 5f 58 58 58 28 29 20 66 75 _result_XXX() fu
1cdc9 6e 63 74 69 6f 6e 0a 20 20 20 20 2a 2a 20 74 6f nction. ** to
1cdca 20 72 65 74 75 72 6e 20 61 20 76 61 6c 75 65 2e return a value.
1cdcb 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 The following c
1cdcc 61 6c 6c 20 72 65 6c 65 61 73 65 73 20 61 6e 79 all releases any
1cdcd 20 72 65 73 6f 75 72 63 65 73 0a 20 20 20 20 2a resources. *
1cdce 2a 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 * associated wit
1cdcf 68 20 73 75 63 68 20 61 20 76 61 6c 75 65 2e 0a h such a value..
1cdd0 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 4e 6f **. ** No
1cdd1 74 65 3a 20 4d 61 79 62 65 20 4d 65 6d 52 65 6c te: Maybe MemRel
1cdd2 65 61 73 65 28 29 20 73 68 6f 75 6c 64 20 62 65 ease() should be
1cdd3 20 63 61 6c 6c 65 64 20 69 66 20 73 71 6c 69 74 called if sqlit
1cdd4 65 33 53 61 66 65 74 79 4f 6e 28 29 0a 20 20 20 e3SafetyOn().
1cdd5 20 2a 2a 20 66 61 69 6c 73 20 61 6c 73 6f 20 28 ** fails also (
1cdd6 74 68 65 20 69 66 28 2e 2e 2e 29 20 73 74 61 74 the if(...) stat
1cdd7 65 6d 65 6e 74 20 61 62 6f 76 65 29 2e 20 42 75 ement above). Bu
1cdd8 74 20 69 66 20 70 65 6f 70 6c 65 20 61 72 65 0a t if people are.
1cdd9 20 20 20 20 2a 2a 20 6d 69 73 75 73 69 6e 67 20 ** misusing
1cdda 73 71 6c 69 74 65 2c 20 74 68 65 79 20 68 61 76 sqlite, they hav
1cddb 65 20 62 69 67 67 65 72 20 70 72 6f 62 6c 65 6d e bigger problem
1cddc 73 20 74 68 61 6e 20 61 20 6c 65 61 6b 65 64 20 s than a leaked
1cddd 76 61 6c 75 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 value.. */.
1cdde 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1cddf 52 65 6c 65 61 73 65 28 26 75 2e 61 67 2e 63 74 Release(&u.ag.ct
1cde0 78 2e 73 29 3b 0a 20 20 20 20 67 6f 74 6f 20 6e x.s);. goto n
1cde1 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a o_mem;. }.. /*
1cde2 20 49 66 20 61 6e 79 20 61 75 78 69 6c 69 61 72 If any auxiliar
1cde3 79 20 64 61 74 61 20 66 75 6e 63 74 69 6f 6e 73 y data functions
1cde4 20 68 61 76 65 20 62 65 65 6e 20 63 61 6c 6c 65 have been calle
1cde5 64 20 62 79 20 74 68 69 73 20 75 73 65 72 20 66 d by this user f
1cde6 75 6e 63 74 69 6f 6e 2c 0a 20 20 2a 2a 20 69 6d unction,. ** im
1cde7 6d 65 64 69 61 74 65 6c 79 20 63 61 6c 6c 20 74 mediately call t
1cde8 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 66 6f he destructor fo
1cde9 72 20 61 6e 79 20 6e 6f 6e 2d 73 74 61 74 69 63 r any non-static
1cdea 20 76 61 6c 75 65 73 2e 0a 20 20 2a 2f 0a 20 20 values.. */.
1cdeb 69 66 28 20 75 2e 61 67 2e 63 74 78 2e 70 56 64 if( u.ag.ctx.pVd
1cdec 62 65 46 75 6e 63 20 29 7b 0a 20 20 20 20 73 71 beFunc ){. sq
1cded 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 65 41 lite3VdbeDeleteA
1cdee 75 78 44 61 74 61 28 75 2e 61 67 2e 63 74 78 2e uxData(u.ag.ctx.
1cdef 70 56 64 62 65 46 75 6e 63 2c 20 70 4f 70 2d 3e pVdbeFunc, pOp->
1cdf0 70 31 29 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 p1);. pOp->p4
1cdf1 2e 70 56 64 62 65 46 75 6e 63 20 3d 20 75 2e 61 .pVdbeFunc = u.a
1cdf2 67 2e 63 74 78 2e 70 56 64 62 65 46 75 6e 63 3b g.ctx.pVdbeFunc;
1cdf3 0a 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 70 65 . pOp->p4type
1cdf4 20 3d 20 50 34 5f 56 44 42 45 46 55 4e 43 3b 0a = P4_VDBEFUNC;.
1cdf5 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 }.. /* If the
1cdf6 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e function return
1cdf7 65 64 20 61 6e 20 65 72 72 6f 72 2c 20 74 68 72 ed an error, thr
1cdf8 6f 77 20 61 6e 20 65 78 63 65 70 74 69 6f 6e 20 ow an exception
1cdf9 2a 2f 0a 20 20 69 66 28 20 75 2e 61 67 2e 63 74 */. if( u.ag.ct
1cdfa 78 2e 69 73 45 72 72 6f 72 20 29 7b 0a 20 20 20 x.isError ){.
1cdfb 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e sqlite3SetStrin
1cdfc 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 g(&p->zErrMsg, d
1cdfd 62 2c 20 22 25 73 22 2c 20 73 71 6c 69 74 65 33 b, "%s", sqlite3
1cdfe 5f 76 61 6c 75 65 5f 74 65 78 74 28 26 75 2e 61 _value_text(&u.a
1cdff 67 2e 63 74 78 2e 73 29 29 3b 0a 20 20 20 20 72 g.ctx.s));. r
1ce00 63 20 3d 20 75 2e 61 67 2e 63 74 78 2e 69 73 45 c = u.ag.ctx.isE
1ce01 72 72 6f 72 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 rror;. }.. /*
1ce02 43 6f 70 79 20 74 68 65 20 72 65 73 75 6c 74 20 Copy the result
1ce03 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 of the function
1ce04 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50 33 into register P3
1ce05 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62 */. sqlite3Vdb
1ce06 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 eChangeEncoding(
1ce07 26 75 2e 61 67 2e 63 74 78 2e 73 2c 20 65 6e 63 &u.ag.ctx.s, enc
1ce08 6f 64 69 6e 67 29 3b 0a 20 20 73 71 6c 69 74 65 oding);. sqlite
1ce09 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28 70 4f 75 3VdbeMemMove(pOu
1ce0a 74 2c 20 26 75 2e 61 67 2e 63 74 78 2e 73 29 3b t, &u.ag.ctx.s);
1ce0b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 . if( sqlite3Vd
1ce0c 62 65 4d 65 6d 54 6f 6f 42 69 67 28 70 4f 75 74 beMemTooBig(pOut
1ce0d 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74 6f ) ){. goto to
1ce0e 6f 5f 62 69 67 3b 0a 20 20 7d 0a 20 20 52 45 47 o_big;. }. REG
1ce0f 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d ISTER_TRACE(pOp-
1ce10 3e 70 33 2c 20 70 4f 75 74 29 3b 0a 20 20 55 50 >p3, pOut);. UP
1ce11 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a DATE_MAX_BLOBSIZ
1ce12 45 28 70 4f 75 74 29 3b 0a 20 20 62 72 65 61 6b E(pOut);. break
1ce13 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ;.}../* Opcode:
1ce14 42 69 74 41 6e 64 20 50 31 20 50 32 20 50 33 20 BitAnd P1 P2 P3
1ce15 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 61 6b 65 20 74 * *.**.** Take t
1ce16 68 65 20 62 69 74 2d 77 69 73 65 20 41 4e 44 20 he bit-wise AND
1ce17 6f 66 20 74 68 65 20 76 61 6c 75 65 73 20 69 6e of the values in
1ce18 20 72 65 67 69 73 74 65 72 20 50 31 20 61 6e 64 register P1 and
1ce19 20 50 32 20 61 6e 64 0a 2a 2a 20 73 74 6f 72 65 P2 and.** store
1ce1a 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20 72 the result in r
1ce1b 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a 20 49 egister P3..** I
1ce1c 66 20 65 69 74 68 65 72 20 69 6e 70 75 74 20 69 f either input i
1ce1d 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73 75 s NULL, the resu
1ce1e 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f lt is NULL..*/./
1ce1f 2a 20 4f 70 63 6f 64 65 3a 20 42 69 74 4f 72 20 * Opcode: BitOr
1ce20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a P1 P2 P3 * *.**.
1ce21 2a 2a 20 54 61 6b 65 20 74 68 65 20 62 69 74 2d ** Take the bit-
1ce22 77 69 73 65 20 4f 52 20 6f 66 20 74 68 65 20 76 wise OR of the v
1ce23 61 6c 75 65 73 20 69 6e 20 72 65 67 69 73 74 65 alues in registe
1ce24 72 20 50 31 20 61 6e 64 20 50 32 20 61 6e 64 0a r P1 and P2 and.
1ce25 2a 2a 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 ** store the res
1ce26 75 6c 74 20 69 6e 20 72 65 67 69 73 74 65 72 20 ult in register
1ce27 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 P3..** If either
1ce28 20 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2c 20 input is NULL,
1ce29 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55 the result is NU
1ce2a 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 LL..*/./* Opcode
1ce2b 3a 20 53 68 69 66 74 4c 65 66 74 20 50 31 20 50 : ShiftLeft P1 P
1ce2c 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 2 P3 * *.**.** S
1ce2d 68 69 66 74 20 74 68 65 20 69 6e 74 65 67 65 72 hift the integer
1ce2e 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 value in regist
1ce2f 65 72 20 50 32 20 74 6f 20 74 68 65 20 6c 65 66 er P2 to the lef
1ce30 74 20 62 79 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 t by the.** numb
1ce31 65 72 20 6f 66 20 62 69 74 73 20 73 70 65 63 69 er of bits speci
1ce32 66 69 65 64 20 62 79 20 74 68 65 20 69 6e 74 65 fied by the inte
1ce33 67 65 72 20 69 6e 20 72 65 67 69 73 65 72 20 50 ger in regiser P
1ce34 31 2e 0a 2a 2a 20 53 74 6f 72 65 20 74 68 65 20 1..** Store the
1ce35 72 65 73 75 6c 74 20 69 6e 20 72 65 67 69 73 74 result in regist
1ce36 65 72 20 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74 er P3..** If eit
1ce37 68 65 72 20 69 6e 70 75 74 20 69 73 20 4e 55 4c her input is NUL
1ce38 4c 2c 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 L, the result is
1ce39 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 NULL..*/./* Opc
1ce3a 6f 64 65 3a 20 53 68 69 66 74 52 69 67 68 74 20 ode: ShiftRight
1ce3b 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a P1 P2 P3 * *.**.
1ce3c 2a 2a 20 53 68 69 66 74 20 74 68 65 20 69 6e 74 ** Shift the int
1ce3d 65 67 65 72 20 76 61 6c 75 65 20 69 6e 20 72 65 eger value in re
1ce3e 67 69 73 74 65 72 20 50 32 20 74 6f 20 74 68 65 gister P2 to the
1ce3f 20 72 69 67 68 74 20 62 79 20 74 68 65 0a 2a 2a right by the.**
1ce40 20 6e 75 6d 62 65 72 20 6f 66 20 62 69 74 73 20 number of bits
1ce41 73 70 65 63 69 66 69 65 64 20 62 79 20 74 68 65 specified by the
1ce42 20 69 6e 74 65 67 65 72 20 69 6e 20 72 65 67 69 integer in regi
1ce43 73 74 65 72 20 50 31 2e 0a 2a 2a 20 53 74 6f 72 ster P1..** Stor
1ce44 65 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20 e the result in
1ce45 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a 20 register P3..**
1ce46 49 66 20 65 69 74 68 65 72 20 69 6e 70 75 74 20 If either input
1ce47 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73 is NULL, the res
1ce48 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a ult is NULL..*/.
1ce49 63 61 73 65 20 4f 50 5f 42 69 74 41 6e 64 3a 20 case OP_BitAnd:
1ce4a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ce4b 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 42 49 /* same as TK_BI
1ce4c 54 41 4e 44 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 TAND, in1, in2,
1ce4d 6f 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f out3 */.case OP_
1ce4e 42 69 74 4f 72 3a 20 20 20 20 20 20 20 20 20 20 BitOr:
1ce4f 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 /* same
1ce50 61 73 20 54 4b 5f 42 49 54 4f 52 2c 20 69 6e 31 as TK_BITOR, in1
1ce51 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 63 , in2, out3 */.c
1ce52 61 73 65 20 4f 50 5f 53 68 69 66 74 4c 65 66 74 ase OP_ShiftLeft
1ce53 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f : /
1ce54 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4c 53 48 * same as TK_LSH
1ce55 49 46 54 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f IFT, in1, in2, o
1ce56 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 ut3 */.case OP_S
1ce57 68 69 66 74 52 69 67 68 74 3a 20 7b 20 20 20 20 hiftRight: {
1ce58 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 /* same a
1ce59 73 20 54 4b 5f 52 53 48 49 46 54 2c 20 69 6e 31 s TK_RSHIFT, in1
1ce5a 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 23 , in2, out3 */.#
1ce5b 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 if 0 /* local v
1ce5c 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 ariables moved i
1ce5d 6e 74 6f 20 75 2e 61 68 20 2a 2f 0a 20 20 69 36 nto u.ah */. i6
1ce5e 34 20 61 3b 0a 20 20 69 36 34 20 62 3b 0a 23 65 4 a;. i64 b;.#e
1ce5f 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 ndif /* local va
1ce60 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1ce61 74 6f 20 75 2e 61 68 20 2a 2f 0a 0a 20 20 69 66 to u.ah */.. if
1ce62 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 7c ( (pIn1->flags |
1ce63 20 70 49 6e 32 2d 3e 66 6c 61 67 73 29 20 26 20 pIn2->flags) &
1ce64 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 MEM_Null ){.
1ce65 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 sqlite3VdbeMemSe
1ce66 74 4e 75 6c 6c 28 70 4f 75 74 29 3b 0a 20 20 20 tNull(pOut);.
1ce67 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 75 2e break;. }. u.
1ce68 61 68 2e 61 20 3d 20 73 71 6c 69 74 65 33 56 64 ah.a = sqlite3Vd
1ce69 62 65 49 6e 74 56 61 6c 75 65 28 70 49 6e 32 29 beIntValue(pIn2)
1ce6a 3b 0a 20 20 75 2e 61 68 2e 62 20 3d 20 73 71 6c ;. u.ah.b = sql
1ce6b 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75 65 ite3VdbeIntValue
1ce6c 28 70 49 6e 31 29 3b 0a 20 20 73 77 69 74 63 68 (pIn1);. switch
1ce6d 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 29 7b ( pOp->opcode ){
1ce6e 0a 20 20 20 20 63 61 73 65 20 4f 50 5f 42 69 74 . case OP_Bit
1ce6f 41 6e 64 3a 20 20 20 20 20 20 75 2e 61 68 2e 61 And: u.ah.a
1ce70 20 26 3d 20 75 2e 61 68 2e 62 3b 20 20 20 20 20 &= u.ah.b;
1ce71 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 break;. case
1ce72 4f 50 5f 42 69 74 4f 72 3a 20 20 20 20 20 20 20 OP_BitOr:
1ce73 75 2e 61 68 2e 61 20 7c 3d 20 75 2e 61 68 2e 62 u.ah.a |= u.ah.b
1ce74 3b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 ; break;.
1ce75 20 63 61 73 65 20 4f 50 5f 53 68 69 66 74 4c 65 case OP_ShiftLe
1ce76 66 74 3a 20 20 20 75 2e 61 68 2e 61 20 3c 3c 3d ft: u.ah.a <<=
1ce77 20 75 2e 61 68 2e 62 3b 20 20 20 20 62 72 65 61 u.ah.b; brea
1ce78 6b 3b 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 k;. default:
1ce79 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 6f 70 assert( pOp->op
1ce7a 63 6f 64 65 3d 3d 4f 50 5f 53 68 69 66 74 52 69 code==OP_ShiftRi
1ce7b 67 68 74 20 29 3b 0a 20 20 20 20 20 20 20 20 20 ght );.
1ce7c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ce7d 75 2e 61 68 2e 61 20 3e 3e 3d 20 75 2e 61 68 2e u.ah.a >>= u.ah.
1ce7e 62 3b 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d b; break;. }
1ce7f 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 . pOut->u.i = u
1ce80 2e 61 68 2e 61 3b 0a 20 20 4d 65 6d 53 65 74 54 .ah.a;. MemSetT
1ce81 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 ypeFlag(pOut, ME
1ce82 4d 5f 49 6e 74 29 3b 0a 20 20 62 72 65 61 6b 3b M_Int);. break;
1ce83 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 41 .}../* Opcode: A
1ce84 64 64 49 6d 6d 20 20 50 31 20 50 32 20 2a 20 2a ddImm P1 P2 * *
1ce85 20 2a 0a 2a 2a 20 0a 2a 2a 20 41 64 64 20 74 68 *.** .** Add th
1ce86 65 20 63 6f 6e 73 74 61 6e 74 20 50 32 20 74 6f e constant P2 to
1ce87 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 the value in re
1ce88 67 69 73 74 65 72 20 50 31 2e 0a 2a 2a 20 54 68 gister P1..** Th
1ce89 65 20 72 65 73 75 6c 74 20 69 73 20 61 6c 77 61 e result is alwa
1ce8a 79 73 20 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a ys an integer..*
1ce8b 2a 0a 2a 2a 20 54 6f 20 66 6f 72 63 65 20 61 6e *.** To force an
1ce8c 79 20 72 65 67 69 73 74 65 72 20 74 6f 20 62 65 y register to be
1ce8d 20 61 6e 20 69 6e 74 65 67 65 72 2c 20 6a 75 73 an integer, jus
1ce8e 74 20 61 64 64 20 30 2e 0a 2a 2f 0a 63 61 73 65 t add 0..*/.case
1ce8f 20 4f 50 5f 41 64 64 49 6d 6d 3a 20 7b 20 20 20 OP_AddImm: {
1ce90 20 20 20 20 20 20 20 20 20 2f 2a 20 69 6e 31 20 /* in1
1ce91 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 */. sqlite3Vdbe
1ce92 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28 70 49 MemIntegerify(pI
1ce93 6e 31 29 3b 0a 20 20 70 49 6e 31 2d 3e 75 2e 69 n1);. pIn1->u.i
1ce94 20 2b 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 62 += pOp->p2;. b
1ce95 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1ce96 64 65 3a 20 4d 75 73 74 42 65 49 6e 74 20 50 31 de: MustBeInt P1
1ce97 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 20 0a 2a 2a P2 * * *.** .**
1ce98 20 46 6f 72 63 65 20 74 68 65 20 76 61 6c 75 65 Force the value
1ce99 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 in register P1
1ce9a 74 6f 20 62 65 20 61 6e 20 69 6e 74 65 67 65 72 to be an integer
1ce9b 2e 20 20 49 66 20 74 68 65 20 76 61 6c 75 65 0a . If the value.
1ce9c 2a 2a 20 69 6e 20 50 31 20 69 73 20 6e 6f 74 20 ** in P1 is not
1ce9d 61 6e 20 69 6e 74 65 67 65 72 20 61 6e 64 20 63 an integer and c
1ce9e 61 6e 6e 6f 74 20 62 65 20 63 6f 6e 76 65 72 74 annot be convert
1ce9f 65 64 20 69 6e 74 6f 20 61 6e 20 69 6e 74 65 67 ed into an integ
1cea0 65 72 0a 2a 2a 20 77 69 74 68 6f 75 74 20 64 61 er.** without da
1cea1 74 61 20 6c 6f 73 73 2c 20 74 68 65 6e 20 6a 75 ta loss, then ju
1cea2 6d 70 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 74 mp immediately t
1cea3 6f 20 50 32 2c 20 6f 72 20 69 66 20 50 32 3d 3d o P2, or if P2==
1cea4 30 0a 2a 2a 20 72 61 69 73 65 20 61 6e 20 53 51 0.** raise an SQ
1cea5 4c 49 54 45 5f 4d 49 53 4d 41 54 43 48 20 65 78 LITE_MISMATCH ex
1cea6 63 65 70 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 ception..*/.case
1cea7 20 4f 50 5f 4d 75 73 74 42 65 49 6e 74 3a 20 7b OP_MustBeInt: {
1cea8 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6a /* j
1cea9 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 20 61 70 ump, in1 */. ap
1ceaa 70 6c 79 41 66 66 69 6e 69 74 79 28 70 49 6e 31 plyAffinity(pIn1
1ceab 2c 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d , SQLITE_AFF_NUM
1ceac 45 52 49 43 2c 20 65 6e 63 6f 64 69 6e 67 29 3b ERIC, encoding);
1cead 0a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c . if( (pIn1->fl
1ceae 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 29 3d 3d ags & MEM_Int)==
1ceaf 30 20 29 7b 0a 20 20 20 20 69 66 28 20 70 4f 70 0 ){. if( pOp
1ceb0 2d 3e 70 32 3d 3d 30 20 29 7b 0a 20 20 20 20 20 ->p2==0 ){.
1ceb1 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4d 49 53 rc = SQLITE_MIS
1ceb2 4d 41 54 43 48 3b 0a 20 20 20 20 20 20 67 6f 74 MATCH;. got
1ceb3 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 o abort_due_to_e
1ceb4 72 72 6f 72 3b 0a 20 20 20 20 7d 65 6c 73 65 7b rror;. }else{
1ceb5 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d . pc = pOp-
1ceb6 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 7d 0a 20 >p2 - 1;. }.
1ceb7 20 7d 65 6c 73 65 7b 0a 20 20 20 20 4d 65 6d 53 }else{. MemS
1ceb8 65 74 54 79 70 65 46 6c 61 67 28 70 49 6e 31 2c etTypeFlag(pIn1,
1ceb9 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 7d 0a 20 MEM_Int);. }.
1ceba 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 break;.}../* Op
1cebb 63 6f 64 65 3a 20 52 65 61 6c 41 66 66 69 6e 69 code: RealAffini
1cebc 74 79 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a ty P1 * * * *.**
1cebd 0a 2a 2a 20 49 66 20 72 65 67 69 73 74 65 72 20 .** If register
1cebe 50 31 20 68 6f 6c 64 73 20 61 6e 20 69 6e 74 65 P1 holds an inte
1cebf 67 65 72 20 63 6f 6e 76 65 72 74 20 69 74 20 74 ger convert it t
1cec0 6f 20 61 20 72 65 61 6c 20 76 61 6c 75 65 2e 0a o a real value..
1cec1 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 **.** This opcod
1cec2 65 20 69 73 20 75 73 65 64 20 77 68 65 6e 20 65 e is used when e
1cec3 78 74 72 61 63 74 69 6e 67 20 69 6e 66 6f 72 6d xtracting inform
1cec4 61 74 69 6f 6e 20 66 72 6f 6d 20 61 20 63 6f 6c ation from a col
1cec5 75 6d 6e 20 74 68 61 74 0a 2a 2a 20 68 61 73 20 umn that.** has
1cec6 52 45 41 4c 20 61 66 66 69 6e 69 74 79 2e 20 20 REAL affinity.
1cec7 53 75 63 68 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 Such column valu
1cec8 65 73 20 6d 61 79 20 73 74 69 6c 6c 20 62 65 20 es may still be
1cec9 73 74 6f 72 65 64 20 61 73 0a 2a 2a 20 69 6e 74 stored as.** int
1ceca 65 67 65 72 73 2c 20 66 6f 72 20 73 70 61 63 65 egers, for space
1cecb 20 65 66 66 69 63 69 65 6e 63 79 2c 20 62 75 74 efficiency, but
1cecc 20 61 66 74 65 72 20 65 78 74 72 61 63 74 69 6f after extractio
1cecd 6e 20 77 65 20 77 61 6e 74 20 74 68 65 6d 0a 2a n we want them.*
1cece 2a 20 74 6f 20 68 61 76 65 20 6f 6e 6c 79 20 61 * to have only a
1cecf 20 72 65 61 6c 20 76 61 6c 75 65 2e 0a 2a 2f 0a real value..*/.
1ced0 63 61 73 65 20 4f 50 5f 52 65 61 6c 41 66 66 69 case OP_RealAffi
1ced1 6e 69 74 79 3a 20 7b 20 20 20 20 20 20 20 20 20 nity: {
1ced2 20 20 20 20 20 20 20 20 20 2f 2a 20 69 6e 31 20 /* in1
1ced3 2a 2f 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 66 */. if( pIn1->f
1ced4 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29 lags & MEM_Int )
1ced5 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 {. sqlite3Vdb
1ced6 65 4d 65 6d 52 65 61 6c 69 66 79 28 70 49 6e 31 eMemRealify(pIn1
1ced7 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a );. }. break;.
1ced8 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 }..#ifndef SQLIT
1ced9 45 5f 4f 4d 49 54 5f 43 41 53 54 0a 2f 2a 20 4f E_OMIT_CAST./* O
1ceda 70 63 6f 64 65 3a 20 54 6f 54 65 78 74 20 50 31 pcode: ToText P1
1cedb 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 * * * *.**.** F
1cedc 6f 72 63 65 20 74 68 65 20 76 61 6c 75 65 20 69 orce the value i
1cedd 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 74 6f n register P1 to
1cede 20 62 65 20 74 65 78 74 2e 0a 2a 2a 20 49 66 20 be text..** If
1cedf 74 68 65 20 76 61 6c 75 65 20 69 73 20 6e 75 6d the value is num
1cee0 65 72 69 63 2c 20 63 6f 6e 76 65 72 74 20 69 74 eric, convert it
1cee1 20 74 6f 20 61 20 73 74 72 69 6e 67 20 75 73 69 to a string usi
1cee2 6e 67 20 74 68 65 0a 2a 2a 20 65 71 75 69 76 61 ng the.** equiva
1cee3 6c 65 6e 74 20 6f 66 20 70 72 69 6e 74 66 28 29 lent of printf()
1cee4 2e 20 20 42 6c 6f 62 20 76 61 6c 75 65 73 20 61 . Blob values a
1cee5 72 65 20 75 6e 63 68 61 6e 67 65 64 20 61 6e 64 re unchanged and
1cee6 0a 2a 2a 20 61 72 65 20 61 66 74 65 72 77 61 72 .** are afterwar
1cee7 64 73 20 73 69 6d 70 6c 79 20 69 6e 74 65 72 70 ds simply interp
1cee8 72 65 74 65 64 20 61 73 20 74 65 78 74 2e 0a 2a reted as text..*
1cee9 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20 76 61 6c 75 *.** A NULL valu
1ceea 65 20 69 73 20 6e 6f 74 20 63 68 61 6e 67 65 64 e is not changed
1ceeb 20 62 79 20 74 68 69 73 20 72 6f 75 74 69 6e 65 by this routine
1ceec 2e 20 20 49 74 20 72 65 6d 61 69 6e 73 20 4e 55 . It remains NU
1ceed 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 54 LL..*/.case OP_T
1ceee 6f 54 65 78 74 3a 20 7b 20 20 20 20 20 20 20 20 oText: {
1ceef 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d /* sam
1cef0 65 20 61 73 20 54 4b 5f 54 4f 5f 54 45 58 54 2c e as TK_TO_TEXT,
1cef1 20 69 6e 31 20 2a 2f 0a 20 20 69 66 28 20 70 49 in1 */. if( pI
1cef2 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f n1->flags & MEM_
1cef3 4e 75 6c 6c 20 29 20 62 72 65 61 6b 3b 0a 20 20 Null ) break;.
1cef4 61 73 73 65 72 74 28 20 4d 45 4d 5f 53 74 72 3d assert( MEM_Str=
1cef5 3d 28 4d 45 4d 5f 42 6c 6f 62 3e 3e 33 29 20 29 =(MEM_Blob>>3) )
1cef6 3b 0a 20 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 ;. pIn1->flags
1cef7 7c 3d 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 26 |= (pIn1->flags&
1cef8 4d 45 4d 5f 42 6c 6f 62 29 3e 3e 33 3b 0a 20 20 MEM_Blob)>>3;.
1cef9 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28 70 49 applyAffinity(pI
1cefa 6e 31 2c 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 n1, SQLITE_AFF_T
1cefb 45 58 54 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a EXT, encoding);.
1cefc 20 20 72 63 20 3d 20 45 78 70 61 6e 64 42 6c 6f rc = ExpandBlo
1cefd 62 28 70 49 6e 31 29 3b 0a 20 20 61 73 73 65 72 b(pIn1);. asser
1cefe 74 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 t( pIn1->flags &
1ceff 20 4d 45 4d 5f 53 74 72 20 7c 7c 20 64 62 2d 3e MEM_Str || db->
1cf00 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a mallocFailed );.
1cf01 20 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 3d pIn1->flags &=
1cf02 20 7e 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 ~(MEM_Int|MEM_R
1cf03 65 61 6c 7c 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d eal|MEM_Blob|MEM
1cf04 5f 5a 65 72 6f 29 3b 0a 20 20 55 50 44 41 54 45 _Zero);. UPDATE
1cf05 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 49 _MAX_BLOBSIZE(pI
1cf06 6e 31 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a n1);. break;.}.
1cf07 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 6f 42 6c ./* Opcode: ToBl
1cf08 6f 62 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a ob P1 * * * *.**
1cf09 0a 2a 2a 20 46 6f 72 63 65 20 74 68 65 20 76 61 .** Force the va
1cf0a 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 lue in register
1cf0b 50 31 20 74 6f 20 62 65 20 61 20 42 4c 4f 42 2e P1 to be a BLOB.
1cf0c 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65 .** If the value
1cf0d 20 69 73 20 6e 75 6d 65 72 69 63 2c 20 63 6f 6e is numeric, con
1cf0e 76 65 72 74 20 69 74 20 74 6f 20 61 20 73 74 72 vert it to a str
1cf0f 69 6e 67 20 66 69 72 73 74 2e 0a 2a 2a 20 53 74 ing first..** St
1cf10 72 69 6e 67 73 20 61 72 65 20 73 69 6d 70 6c 79 rings are simply
1cf11 20 72 65 69 6e 74 65 72 70 72 65 74 65 64 20 61 reinterpreted a
1cf12 73 20 62 6c 6f 62 73 20 77 69 74 68 20 6e 6f 20 s blobs with no
1cf13 63 68 61 6e 67 65 0a 2a 2a 20 74 6f 20 74 68 65 change.** to the
1cf14 20 75 6e 64 65 72 6c 79 69 6e 67 20 64 61 74 61 underlying data
1cf15 2e 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20 76 ..**.** A NULL v
1cf16 61 6c 75 65 20 69 73 20 6e 6f 74 20 63 68 61 6e alue is not chan
1cf17 67 65 64 20 62 79 20 74 68 69 73 20 72 6f 75 74 ged by this rout
1cf18 69 6e 65 2e 20 20 49 74 20 72 65 6d 61 69 6e 73 ine. It remains
1cf19 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f NULL..*/.case O
1cf1a 50 5f 54 6f 42 6c 6f 62 3a 20 7b 20 20 20 20 20 P_ToBlob: {
1cf1b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1cf1c 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 42 4c same as TK_TO_BL
1cf1d 4f 42 2c 20 69 6e 31 20 2a 2f 0a 20 20 69 66 28 OB, in1 */. if(
1cf1e 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d pIn1->flags & M
1cf1f 45 4d 5f 4e 75 6c 6c 20 29 20 62 72 65 61 6b 3b EM_Null ) break;
1cf20 0a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c . if( (pIn1->fl
1cf21 61 67 73 20 26 20 4d 45 4d 5f 42 6c 6f 62 29 3d ags & MEM_Blob)=
1cf22 3d 30 20 29 7b 0a 20 20 20 20 61 70 70 6c 79 41 =0 ){. applyA
1cf23 66 66 69 6e 69 74 79 28 70 49 6e 31 2c 20 53 51 ffinity(pIn1, SQ
1cf24 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 2c 20 65 LITE_AFF_TEXT, e
1cf25 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 20 20 61 73 ncoding);. as
1cf26 73 65 72 74 28 20 70 49 6e 31 2d 3e 66 6c 61 67 sert( pIn1->flag
1cf27 73 20 26 20 4d 45 4d 5f 53 74 72 20 7c 7c 20 64 s & MEM_Str || d
1cf28 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
1cf29 29 3b 0a 20 20 20 20 4d 65 6d 53 65 74 54 79 70 );. MemSetTyp
1cf2a 65 46 6c 61 67 28 70 49 6e 31 2c 20 4d 45 4d 5f eFlag(pIn1, MEM_
1cf2b 42 6c 6f 62 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a Blob);. }else{.
1cf2c 20 20 20 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 pIn1->flags
1cf2d 26 3d 20 7e 28 4d 45 4d 5f 54 79 70 65 4d 61 73 &= ~(MEM_TypeMas
1cf2e 6b 26 7e 4d 45 4d 5f 42 6c 6f 62 29 3b 0a 20 20 k&~MEM_Blob);.
1cf2f 7d 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 }. UPDATE_MAX_B
1cf30 4c 4f 42 53 49 5a 45 28 70 49 6e 31 29 3b 0a 20 LOBSIZE(pIn1);.
1cf31 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 break;.}../* Op
1cf32 63 6f 64 65 3a 20 54 6f 4e 75 6d 65 72 69 63 20 code: ToNumeric
1cf33 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a P1 * * * *.**.**
1cf34 20 46 6f 72 63 65 20 74 68 65 20 76 61 6c 75 65 Force the value
1cf35 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 in register P1
1cf36 74 6f 20 62 65 20 6e 75 6d 65 72 69 63 20 28 65 to be numeric (e
1cf37 69 74 68 65 72 20 61 6e 0a 2a 2a 20 69 6e 74 65 ither an.** inte
1cf38 67 65 72 20 6f 72 20 61 20 66 6c 6f 61 74 69 6e ger or a floatin
1cf39 67 2d 70 6f 69 6e 74 20 6e 75 6d 62 65 72 2e 29 g-point number.)
1cf3a 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65 .** If the value
1cf3b 20 69 73 20 74 65 78 74 20 6f 72 20 62 6c 6f 62 is text or blob
1cf3c 2c 20 74 72 79 20 74 6f 20 63 6f 6e 76 65 72 74 , try to convert
1cf3d 20 69 74 20 74 6f 20 61 6e 20 75 73 69 6e 67 20 it to an using
1cf3e 74 68 65 0a 2a 2a 20 65 71 75 69 76 61 6c 65 6e the.** equivalen
1cf3f 74 20 6f 66 20 61 74 6f 69 28 29 20 6f 72 20 61 t of atoi() or a
1cf40 74 6f 66 28 29 20 61 6e 64 20 73 74 6f 72 65 20 tof() and store
1cf41 30 20 69 66 20 6e 6f 20 73 75 63 68 20 63 6f 6e 0 if no such con
1cf42 76 65 72 73 69 6f 6e 20 0a 2a 2a 20 69 73 20 70 version .** is p
1cf43 6f 73 73 69 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 ossible..**.** A
1cf44 20 4e 55 4c 4c 20 76 61 6c 75 65 20 69 73 20 6e NULL value is n
1cf45 6f 74 20 63 68 61 6e 67 65 64 20 62 79 20 74 68 ot changed by th
1cf46 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 49 74 20 is routine. It
1cf47 72 65 6d 61 69 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f remains NULL..*/
1cf48 0a 63 61 73 65 20 4f 50 5f 54 6f 4e 75 6d 65 72 .case OP_ToNumer
1cf49 69 63 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 ic: {
1cf4a 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 /* same a
1cf4b 73 20 54 4b 5f 54 4f 5f 4e 55 4d 45 52 49 43 2c s TK_TO_NUMERIC,
1cf4c 20 69 6e 31 20 2a 2f 0a 20 20 69 66 28 20 28 70 in1 */. if( (p
1cf4d 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 In1->flags & (ME
1cf4e 4d 5f 4e 75 6c 6c 7c 4d 45 4d 5f 49 6e 74 7c 4d M_Null|MEM_Int|M
1cf4f 45 4d 5f 52 65 61 6c 29 29 3d 3d 30 20 29 7b 0a EM_Real))==0 ){.
1cf50 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
1cf51 65 6d 4e 75 6d 65 72 69 66 79 28 70 49 6e 31 29 emNumerify(pIn1)
1cf52 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d ;. }. break;.}
1cf53 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
1cf54 45 5f 4f 4d 49 54 5f 43 41 53 54 20 2a 2f 0a 0a E_OMIT_CAST */..
1cf55 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 6f 49 6e 74 /* Opcode: ToInt
1cf56 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a P1 * * * *.**.*
1cf57 2a 20 46 6f 72 63 65 20 74 68 65 20 76 61 6c 75 * Force the valu
1cf58 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 e in register P1
1cf59 20 62 65 20 61 6e 20 69 6e 74 65 67 65 72 2e 20 be an integer.
1cf5a 20 49 66 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 If.** The value
1cf5b 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 61 20 is currently a
1cf5c 72 65 61 6c 20 6e 75 6d 62 65 72 2c 20 64 72 6f real number, dro
1cf5d 70 20 69 74 73 20 66 72 61 63 74 69 6f 6e 61 6c p its fractional
1cf5e 20 70 61 72 74 2e 0a 2a 2a 20 49 66 20 74 68 65 part..** If the
1cf5f 20 76 61 6c 75 65 20 69 73 20 74 65 78 74 20 6f value is text o
1cf60 72 20 62 6c 6f 62 2c 20 74 72 79 20 74 6f 20 63 r blob, try to c
1cf61 6f 6e 76 65 72 74 20 69 74 20 74 6f 20 61 6e 20 onvert it to an
1cf62 69 6e 74 65 67 65 72 20 75 73 69 6e 67 20 74 68 integer using th
1cf63 65 0a 2a 2a 20 65 71 75 69 76 61 6c 65 6e 74 20 e.** equivalent
1cf64 6f 66 20 61 74 6f 69 28 29 20 61 6e 64 20 73 74 of atoi() and st
1cf65 6f 72 65 20 30 20 69 66 20 6e 6f 20 73 75 63 68 ore 0 if no such
1cf66 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20 70 conversion is p
1cf67 6f 73 73 69 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 ossible..**.** A
1cf68 20 4e 55 4c 4c 20 76 61 6c 75 65 20 69 73 20 6e NULL value is n
1cf69 6f 74 20 63 68 61 6e 67 65 64 20 62 79 20 74 68 ot changed by th
1cf6a 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 49 74 20 is routine. It
1cf6b 72 65 6d 61 69 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f remains NULL..*/
1cf6c 0a 63 61 73 65 20 4f 50 5f 54 6f 49 6e 74 3a 20 .case OP_ToInt:
1cf6d 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {
1cf6e 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b /* same as TK
1cf6f 5f 54 4f 5f 49 4e 54 2c 20 69 6e 31 20 2a 2f 0a _TO_INT, in1 */.
1cf70 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 if( (pIn1->fla
1cf71 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d gs & MEM_Null)==
1cf72 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 0 ){. sqlite3
1cf73 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 VdbeMemIntegerif
1cf74 79 28 70 49 6e 31 29 3b 0a 20 20 7d 0a 20 20 62 y(pIn1);. }. b
1cf75 72 65 61 6b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 reak;.}..#ifndef
1cf76 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 41 53 SQLITE_OMIT_CAS
1cf77 54 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 6f 52 T./* Opcode: ToR
1cf78 65 61 6c 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a eal P1 * * * *.*
1cf79 2a 0a 2a 2a 20 46 6f 72 63 65 20 74 68 65 20 76 *.** Force the v
1cf7a 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 alue in register
1cf7b 20 50 31 20 74 6f 20 62 65 20 61 20 66 6c 6f 61 P1 to be a floa
1cf7c 74 69 6e 67 20 70 6f 69 6e 74 20 6e 75 6d 62 65 ting point numbe
1cf7d 72 2e 0a 2a 2a 20 49 66 20 54 68 65 20 76 61 6c r..** If The val
1cf7e 75 65 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 ue is currently
1cf7f 61 6e 20 69 6e 74 65 67 65 72 2c 20 63 6f 6e 76 an integer, conv
1cf80 65 72 74 20 69 74 2e 0a 2a 2a 20 49 66 20 74 68 ert it..** If th
1cf81 65 20 76 61 6c 75 65 20 69 73 20 74 65 78 74 20 e value is text
1cf82 6f 72 20 62 6c 6f 62 2c 20 74 72 79 20 74 6f 20 or blob, try to
1cf83 63 6f 6e 76 65 72 74 20 69 74 20 74 6f 20 61 6e convert it to an
1cf84 20 69 6e 74 65 67 65 72 20 75 73 69 6e 67 20 74 integer using t
1cf85 68 65 0a 2a 2a 20 65 71 75 69 76 61 6c 65 6e 74 he.** equivalent
1cf86 20 6f 66 20 61 74 6f 69 28 29 20 61 6e 64 20 73 of atoi() and s
1cf87 74 6f 72 65 20 30 2e 30 20 69 66 20 6e 6f 20 73 tore 0.0 if no s
1cf88 75 63 68 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 uch conversion i
1cf89 73 20 70 6f 73 73 69 62 6c 65 2e 0a 2a 2a 0a 2a s possible..**.*
1cf8a 2a 20 41 20 4e 55 4c 4c 20 76 61 6c 75 65 20 69 * A NULL value i
1cf8b 73 20 6e 6f 74 20 63 68 61 6e 67 65 64 20 62 79 s not changed by
1cf8c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 this routine.
1cf8d 49 74 20 72 65 6d 61 69 6e 73 20 4e 55 4c 4c 2e It remains NULL.
1cf8e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 54 6f 52 65 .*/.case OP_ToRe
1cf8f 61 6c 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 al: {
1cf90 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 /* same a
1cf91 73 20 54 4b 5f 54 4f 5f 52 45 41 4c 2c 20 69 6e s TK_TO_REAL, in
1cf92 31 20 2a 2f 0a 20 20 69 66 28 20 28 70 49 6e 31 1 */. if( (pIn1
1cf93 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 ->flags & MEM_Nu
1cf94 6c 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 ll)==0 ){. sq
1cf95 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 61 6c lite3VdbeMemReal
1cf96 69 66 79 28 70 49 6e 31 29 3b 0a 20 20 7d 0a 20 ify(pIn1);. }.
1cf97 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 break;.}.#endif
1cf98 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f /* SQLITE_OMIT_
1cf99 43 41 53 54 20 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f CAST */../* Opco
1cf9a 64 65 3a 20 4c 74 20 50 31 20 50 32 20 50 33 20 de: Lt P1 P2 P3
1cf9b 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 43 6f 6d 70 P4 P5.**.** Comp
1cf9c 61 72 65 20 74 68 65 20 76 61 6c 75 65 73 20 69 are the values i
1cf9d 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 61 6e n register P1 an
1cf9e 64 20 50 33 2e 20 20 49 66 20 72 65 67 28 50 33 d P3. If reg(P3
1cf9f 29 3c 72 65 67 28 50 31 29 20 74 68 65 6e 0a 2a )<reg(P1) then.*
1cfa0 2a 20 6a 75 6d 70 20 74 6f 20 61 64 64 72 65 73 * jump to addres
1cfa1 73 20 50 32 2e 20 20 0a 2a 2a 0a 2a 2a 20 49 66 s P2. .**.** If
1cfa2 20 74 68 65 20 53 51 4c 49 54 45 5f 4a 55 4d 50 the SQLITE_JUMP
1cfa3 49 46 4e 55 4c 4c 20 62 69 74 20 6f 66 20 50 35 IFNULL bit of P5
1cfa4 20 69 73 20 73 65 74 20 61 6e 64 20 65 69 74 68 is set and eith
1cfa5 65 72 20 72 65 67 28 50 31 29 20 6f 72 0a 2a 2a er reg(P1) or.**
1cfa6 20 72 65 67 28 50 33 29 20 69 73 20 4e 55 4c 4c reg(P3) is NULL
1cfa7 20 74 68 65 6e 20 74 61 6b 65 20 74 68 65 20 6a then take the j
1cfa8 75 6d 70 2e 20 20 49 66 20 74 68 65 20 53 51 4c ump. If the SQL
1cfa9 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20 0a ITE_JUMPIFNULL .
1cfaa 2a 2a 20 62 69 74 20 69 73 20 63 6c 65 61 72 20 ** bit is clear
1cfab 74 68 65 6e 20 66 61 6c 6c 20 74 68 72 75 20 69 then fall thru i
1cfac 66 20 65 69 74 68 65 72 20 6f 70 65 72 61 6e 64 f either operand
1cfad 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 is NULL..**.**
1cfae 54 68 65 20 53 51 4c 49 54 45 5f 41 46 46 5f 4d The SQLITE_AFF_M
1cfaf 41 53 4b 20 70 6f 72 74 69 6f 6e 20 6f 66 20 50 ASK portion of P
1cfb0 35 20 6d 75 73 74 20 62 65 20 61 6e 20 61 66 66 5 must be an aff
1cfb1 69 6e 69 74 79 20 63 68 61 72 61 63 74 65 72 20 inity character
1cfb2 2d 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f -.** SQLITE_AFF_
1cfb3 54 45 58 54 2c 20 53 51 4c 49 54 45 5f 41 46 46 TEXT, SQLITE_AFF
1cfb4 5f 49 4e 54 45 47 45 52 2c 20 61 6e 64 20 73 6f _INTEGER, and so
1cfb5 20 66 6f 72 74 68 2e 20 41 6e 20 61 74 74 65 6d forth. An attem
1cfb6 70 74 20 69 73 20 6d 61 64 65 20 0a 2a 2a 20 74 pt is made .** t
1cfb7 6f 20 63 6f 65 72 63 65 20 62 6f 74 68 20 69 6e o coerce both in
1cfb8 70 75 74 73 20 61 63 63 6f 72 64 69 6e 67 20 74 puts according t
1cfb9 6f 20 74 68 69 73 20 61 66 66 69 6e 69 74 79 20 o this affinity
1cfba 62 65 66 6f 72 65 20 74 68 65 0a 2a 2a 20 63 6f before the.** co
1cfbb 6d 70 61 72 69 73 6f 6e 20 69 73 20 6d 61 64 65 mparison is made
1cfbc 2e 20 49 66 20 74 68 65 20 53 51 4c 49 54 45 5f . If the SQLITE_
1cfbd 41 46 46 5f 4d 41 53 4b 20 69 73 20 30 78 30 30 AFF_MASK is 0x00
1cfbe 2c 20 74 68 65 6e 20 6e 75 6d 65 72 69 63 0a 2a , then numeric.*
1cfbf 2a 20 61 66 66 69 6e 69 74 79 20 69 73 20 75 73 * affinity is us
1cfc0 65 64 2e 20 4e 6f 74 65 20 74 68 61 74 20 74 68 ed. Note that th
1cfc1 65 20 61 66 66 69 6e 69 74 79 20 63 6f 6e 76 65 e affinity conve
1cfc2 72 73 69 6f 6e 73 20 61 72 65 20 73 74 6f 72 65 rsions are store
1cfc3 64 0a 2a 2a 20 62 61 63 6b 20 69 6e 74 6f 20 74 d.** back into t
1cfc4 68 65 20 69 6e 70 75 74 20 72 65 67 69 73 74 65 he input registe
1cfc5 72 73 20 50 31 20 61 6e 64 20 50 33 2e 20 20 53 rs P1 and P3. S
1cfc6 6f 20 74 68 69 73 20 6f 70 63 6f 64 65 20 63 61 o this opcode ca
1cfc7 6e 20 63 61 75 73 65 0a 2a 2a 20 70 65 72 73 69 n cause.** persi
1cfc8 73 74 65 6e 74 20 63 68 61 6e 67 65 73 20 74 6f stent changes to
1cfc9 20 72 65 67 69 73 74 65 72 73 20 50 31 20 61 6e registers P1 an
1cfca 64 20 50 33 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 63 65 d P3..**.** Once
1cfcb 20 61 6e 79 20 63 6f 6e 76 65 72 73 69 6f 6e 73 any conversions
1cfcc 20 68 61 76 65 20 74 61 6b 65 6e 20 70 6c 61 63 have taken plac
1cfcd 65 2c 20 61 6e 64 20 6e 65 69 74 68 65 72 20 76 e, and neither v
1cfce 61 6c 75 65 20 69 73 20 4e 55 4c 4c 2c 20 0a 2a alue is NULL, .*
1cfcf 2a 20 74 68 65 20 76 61 6c 75 65 73 20 61 72 65 * the values are
1cfd0 20 63 6f 6d 70 61 72 65 64 2e 20 49 66 20 62 6f compared. If bo
1cfd1 74 68 20 76 61 6c 75 65 73 20 61 72 65 20 62 6c th values are bl
1cfd2 6f 62 73 20 74 68 65 6e 20 6d 65 6d 63 6d 70 28 obs then memcmp(
1cfd3 29 20 69 73 0a 2a 2a 20 75 73 65 64 20 74 6f 20 ) is.** used to
1cfd4 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20 72 65 determine the re
1cfd5 73 75 6c 74 73 20 6f 66 20 74 68 65 20 63 6f 6d sults of the com
1cfd6 70 61 72 69 73 6f 6e 2e 20 20 49 66 20 62 6f 74 parison. If bot
1cfd7 68 20 76 61 6c 75 65 73 0a 2a 2a 20 61 72 65 20 h values.** are
1cfd8 74 65 78 74 2c 20 74 68 65 6e 20 74 68 65 20 61 text, then the a
1cfd9 70 70 72 6f 70 72 69 61 74 65 20 63 6f 6c 6c 61 ppropriate colla
1cfda 74 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 73 70 ting function sp
1cfdb 65 63 69 66 69 65 64 20 69 6e 0a 2a 2a 20 50 34 ecified in.** P4
1cfdc 20 69 73 20 20 75 73 65 64 20 74 6f 20 64 6f 20 is used to do
1cfdd 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 2e 20 the comparison.
1cfde 20 49 66 20 50 34 20 69 73 20 6e 6f 74 20 73 70 If P4 is not sp
1cfdf 65 63 69 66 69 65 64 20 74 68 65 6e 0a 2a 2a 20 ecified then.**
1cfe0 6d 65 6d 63 6d 70 28 29 20 69 73 20 75 73 65 64 memcmp() is used
1cfe1 20 74 6f 20 63 6f 6d 70 61 72 65 20 74 65 78 74 to compare text
1cfe2 20 73 74 72 69 6e 67 2e 20 20 49 66 20 62 6f 74 string. If bot
1cfe3 68 20 76 61 6c 75 65 73 20 61 72 65 0a 2a 2a 20 h values are.**
1cfe4 6e 75 6d 65 72 69 63 2c 20 74 68 65 6e 20 61 20 numeric, then a
1cfe5 6e 75 6d 65 72 69 63 20 63 6f 6d 70 61 72 69 73 numeric comparis
1cfe6 6f 6e 20 69 73 20 75 73 65 64 2e 20 49 66 20 74 on is used. If t
1cfe7 68 65 20 74 77 6f 20 76 61 6c 75 65 73 0a 2a 2a he two values.**
1cfe8 20 61 72 65 20 6f 66 20 64 69 66 66 65 72 65 6e are of differen
1cfe9 74 20 74 79 70 65 73 2c 20 74 68 65 6e 20 6e 75 t types, then nu
1cfea 6d 62 65 72 73 20 61 72 65 20 63 6f 6e 73 69 64 mbers are consid
1cfeb 65 72 65 64 20 6c 65 73 73 20 74 68 61 6e 0a 2a ered less than.*
1cfec 2a 20 73 74 72 69 6e 67 73 20 61 6e 64 20 73 74 * strings and st
1cfed 72 69 6e 67 73 20 61 72 65 20 63 6f 6e 73 69 64 rings are consid
1cfee 65 72 65 64 20 6c 65 73 73 20 74 68 61 6e 20 62 ered less than b
1cfef 6c 6f 62 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 lobs..**.** If t
1cff0 68 65 20 53 51 4c 49 54 45 5f 53 54 4f 52 45 50 he SQLITE_STOREP
1cff1 32 20 62 69 74 20 6f 66 20 50 35 20 69 73 20 73 2 bit of P5 is s
1cff2 65 74 2c 20 74 68 65 6e 20 64 6f 20 6e 6f 74 20 et, then do not
1cff3 6a 75 6d 70 2e 20 20 49 6e 73 74 65 61 64 2c 0a jump. Instead,.
1cff4 2a 2a 20 73 74 6f 72 65 20 61 20 62 6f 6f 6c 65 ** store a boole
1cff5 61 6e 20 72 65 73 75 6c 74 20 28 65 69 74 68 65 an result (eithe
1cff6 72 20 30 2c 20 6f 72 20 31 2c 20 6f 72 20 4e 55 r 0, or 1, or NU
1cff7 4c 4c 29 20 69 6e 20 72 65 67 69 73 74 65 72 20 LL) in register
1cff8 50 32 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 P2..*/./* Opcode
1cff9 3a 20 4e 65 20 50 31 20 50 32 20 50 33 20 50 34 : Ne P1 P2 P3 P4
1cffa 20 50 35 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 P5.**.** This w
1cffb 6f 72 6b 73 20 6a 75 73 74 20 6c 69 6b 65 20 74 orks just like t
1cffc 68 65 20 4c 74 20 6f 70 63 6f 64 65 20 65 78 63 he Lt opcode exc
1cffd 65 70 74 20 74 68 61 74 20 74 68 65 20 6a 75 6d ept that the jum
1cffe 70 20 69 73 20 74 61 6b 65 6e 20 69 66 0a 2a 2a p is taken if.**
1cfff 20 74 68 65 20 6f 70 65 72 61 6e 64 73 20 69 6e the operands in
1d000 20 72 65 67 69 73 74 65 72 73 20 50 31 20 61 6e registers P1 an
1d001 64 20 50 33 20 61 72 65 20 6e 6f 74 20 65 71 75 d P3 are not equ
1d002 61 6c 2e 20 20 53 65 65 20 74 68 65 20 4c 74 20 al. See the Lt
1d003 6f 70 63 6f 64 65 20 66 6f 72 0a 2a 2a 20 61 64 opcode for.** ad
1d004 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 ditional informa
1d005 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 53 tion..**.** If S
1d006 51 4c 49 54 45 5f 4e 55 4c 4c 45 51 20 69 73 20 QLITE_NULLEQ is
1d007 73 65 74 20 69 6e 20 50 35 20 74 68 65 6e 20 74 set in P5 then t
1d008 68 65 20 72 65 73 75 6c 74 20 6f 66 20 63 6f 6d he result of com
1d009 70 61 72 69 73 6f 6e 20 69 73 20 61 6c 77 61 79 parison is alway
1d00a 73 20 65 69 74 68 65 72 0a 2a 2a 20 74 72 75 65 s either.** true
1d00b 20 6f 72 20 66 61 6c 73 65 20 61 6e 64 20 69 73 or false and is
1d00c 20 6e 65 76 65 72 20 4e 55 4c 4c 2e 20 20 49 66 never NULL. If
1d00d 20 62 6f 74 68 20 6f 70 65 72 61 6e 64 73 20 61 both operands a
1d00e 72 65 20 4e 55 4c 4c 20 74 68 65 6e 20 74 68 65 re NULL then the
1d00f 20 72 65 73 75 6c 74 0a 2a 2a 20 6f 66 20 63 6f result.** of co
1d010 6d 70 61 72 69 73 6f 6e 20 69 73 20 66 61 6c 73 mparison is fals
1d011 65 2e 20 20 49 66 20 65 69 74 68 65 72 20 6f 70 e. If either op
1d012 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c 20 74 68 erand is NULL th
1d013 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 en the result is
1d014 20 74 72 75 65 2e 0a 2a 2a 20 49 66 20 6e 65 69 true..** If nei
1d015 74 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73 20 ther operand is
1d016 4e 55 4c 4c 20 74 68 65 20 74 68 65 20 72 65 73 NULL the the res
1d017 75 6c 74 20 69 73 20 74 68 65 20 73 61 6d 65 20 ult is the same
1d018 61 73 20 69 74 20 77 6f 75 6c 64 20 62 65 20 69 as it would be i
1d019 66 0a 2a 2a 20 74 68 65 20 53 51 4c 49 54 45 5f f.** the SQLITE_
1d01a 4e 55 4c 4c 45 51 20 66 6c 61 67 20 77 65 72 65 NULLEQ flag were
1d01b 20 6f 6d 69 74 74 65 64 20 66 72 6f 6d 20 50 35 omitted from P5
1d01c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ..*/./* Opcode:
1d01d 45 71 20 50 31 20 50 32 20 50 33 20 50 34 20 50 Eq P1 P2 P3 P4 P
1d01e 35 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 6f 72 5.**.** This wor
1d01f 6b 73 20 6a 75 73 74 20 6c 69 6b 65 20 74 68 65 ks just like the
1d020 20 4c 74 20 6f 70 63 6f 64 65 20 65 78 63 65 70 Lt opcode excep
1d021 74 20 74 68 61 74 20 74 68 65 20 6a 75 6d 70 20 t that the jump
1d022 69 73 20 74 61 6b 65 6e 20 69 66 0a 2a 2a 20 74 is taken if.** t
1d023 68 65 20 6f 70 65 72 61 6e 64 73 20 69 6e 20 72 he operands in r
1d024 65 67 69 73 74 65 72 73 20 50 31 20 61 6e 64 20 egisters P1 and
1d025 50 33 20 61 72 65 20 65 71 75 61 6c 2e 0a 2a 2a P3 are equal..**
1d026 20 53 65 65 20 74 68 65 20 4c 74 20 6f 70 63 6f See the Lt opco
1d027 64 65 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 de for additiona
1d028 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a l information..*
1d029 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f 4e *.** If SQLITE_N
1d02a 55 4c 4c 45 51 20 69 73 20 73 65 74 20 69 6e 20 ULLEQ is set in
1d02b 50 35 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 P5 then the resu
1d02c 6c 74 20 6f 66 20 63 6f 6d 70 61 72 69 73 6f 6e lt of comparison
1d02d 20 69 73 20 61 6c 77 61 79 73 20 65 69 74 68 65 is always eithe
1d02e 72 0a 2a 2a 20 74 72 75 65 20 6f 72 20 66 61 6c r.** true or fal
1d02f 73 65 20 61 6e 64 20 69 73 20 6e 65 76 65 72 20 se and is never
1d030 4e 55 4c 4c 2e 20 20 49 66 20 62 6f 74 68 20 6f NULL. If both o
1d031 70 65 72 61 6e 64 73 20 61 72 65 20 4e 55 4c 4c perands are NULL
1d032 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 then the result
1d033 0a 2a 2a 20 6f 66 20 63 6f 6d 70 61 72 69 73 6f .** of compariso
1d034 6e 20 69 73 20 74 72 75 65 2e 20 20 49 66 20 65 n is true. If e
1d035 69 74 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73 ither operand is
1d036 20 4e 55 4c 4c 20 74 68 65 6e 20 74 68 65 20 72 NULL then the r
1d037 65 73 75 6c 74 20 69 73 20 66 61 6c 73 65 2e 0a esult is false..
1d038 2a 2a 20 49 66 20 6e 65 69 74 68 65 72 20 6f 70 ** If neither op
1d039 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c 20 74 68 erand is NULL th
1d03a 65 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 e the result is
1d03b 74 68 65 20 73 61 6d 65 20 61 73 20 69 74 20 77 the same as it w
1d03c 6f 75 6c 64 20 62 65 20 69 66 0a 2a 2a 20 74 68 ould be if.** th
1d03d 65 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 45 51 20 e SQLITE_NULLEQ
1d03e 66 6c 61 67 20 77 65 72 65 20 6f 6d 69 74 74 65 flag were omitte
1d03f 64 20 66 72 6f 6d 20 50 35 2e 0a 2a 2f 0a 2f 2a d from P5..*/./*
1d040 20 4f 70 63 6f 64 65 3a 20 4c 65 20 50 31 20 50 Opcode: Le P1 P
1d041 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 2 P3 P4 P5.**.**
1d042 20 54 68 69 73 20 77 6f 72 6b 73 20 6a 75 73 74 This works just
1d043 20 6c 69 6b 65 20 74 68 65 20 4c 74 20 6f 70 63 like the Lt opc
1d044 6f 64 65 20 65 78 63 65 70 74 20 74 68 61 74 20 ode except that
1d045 74 68 65 20 6a 75 6d 70 20 69 73 20 74 61 6b 65 the jump is take
1d046 6e 20 69 66 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 n if.** the cont
1d047 65 6e 74 20 6f 66 20 72 65 67 69 73 74 65 72 20 ent of register
1d048 50 33 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 P3 is less than
1d049 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 or equal to the
1d04a 63 6f 6e 74 65 6e 74 20 6f 66 0a 2a 2a 20 72 65 content of.** re
1d04b 67 69 73 74 65 72 20 50 31 2e 20 20 53 65 65 20 gister P1. See
1d04c 74 68 65 20 4c 74 20 6f 70 63 6f 64 65 20 66 6f the Lt opcode fo
1d04d 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 r additional inf
1d04e 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 2f 2a 20 ormation..*/./*
1d04f 4f 70 63 6f 64 65 3a 20 47 74 20 50 31 20 50 32 Opcode: Gt P1 P2
1d050 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 P3 P4 P5.**.**
1d051 54 68 69 73 20 77 6f 72 6b 73 20 6a 75 73 74 20 This works just
1d052 6c 69 6b 65 20 74 68 65 20 4c 74 20 6f 70 63 6f like the Lt opco
1d053 64 65 20 65 78 63 65 70 74 20 74 68 61 74 20 74 de except that t
1d054 68 65 20 6a 75 6d 70 20 69 73 20 74 61 6b 65 6e he jump is taken
1d055 20 69 66 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 if.** the conte
1d056 6e 74 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 nt of register P
1d057 33 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 3 is greater tha
1d058 6e 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 n the content of
1d059 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 50 31 2e .** register P1.
1d05a 20 20 53 65 65 20 74 68 65 20 4c 74 20 6f 70 63 See the Lt opc
1d05b 6f 64 65 20 66 6f 72 20 61 64 64 69 74 69 6f 6e ode for addition
1d05c 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a al information..
1d05d 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 47 65 */./* Opcode: Ge
1d05e 20 50 31 20 50 32 20 50 33 20 50 34 20 50 35 0a P1 P2 P3 P4 P5.
1d05f 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 6f 72 6b 73 **.** This works
1d060 20 6a 75 73 74 20 6c 69 6b 65 20 74 68 65 20 4c just like the L
1d061 74 20 6f 70 63 6f 64 65 20 65 78 63 65 70 74 20 t opcode except
1d062 74 68 61 74 20 74 68 65 20 6a 75 6d 70 20 69 73 that the jump is
1d063 20 74 61 6b 65 6e 20 69 66 0a 2a 2a 20 74 68 65 taken if.** the
1d064 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65 67 69 content of regi
1d065 73 74 65 72 20 50 33 20 69 73 20 67 72 65 61 74 ster P3 is great
1d066 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c er than or equal
1d067 20 74 6f 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 to the content
1d068 6f 66 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 50 of.** register P
1d069 31 2e 20 20 53 65 65 20 74 68 65 20 4c 74 20 6f 1. See the Lt o
1d06a 70 63 6f 64 65 20 66 6f 72 20 61 64 64 69 74 69 pcode for additi
1d06b 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e onal information
1d06c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 45 71 3a ..*/.case OP_Eq:
1d06d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1d06e 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 45 51 2c * same as TK_EQ,
1d06f 20 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20 jump, in1, in3
1d070 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 65 3a 20 20 */.case OP_Ne:
1d071 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1d072 73 61 6d 65 20 61 73 20 54 4b 5f 4e 45 2c 20 6a same as TK_NE, j
1d073 75 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20 2a 2f ump, in1, in3 */
1d074 0a 63 61 73 65 20 4f 50 5f 4c 74 3a 20 20 20 20 .case OP_Lt:
1d075 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 /* sa
1d076 6d 65 20 61 73 20 54 4b 5f 4c 54 2c 20 6a 75 6d me as TK_LT, jum
1d077 70 2c 20 69 6e 31 2c 20 69 6e 33 20 2a 2f 0a 63 p, in1, in3 */.c
1d078 61 73 65 20 4f 50 5f 4c 65 3a 20 20 20 20 20 20 ase OP_Le:
1d079 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 /* same
1d07a 20 61 73 20 54 4b 5f 4c 45 2c 20 6a 75 6d 70 2c as TK_LE, jump,
1d07b 20 69 6e 31 2c 20 69 6e 33 20 2a 2f 0a 63 61 73 in1, in3 */.cas
1d07c 65 20 4f 50 5f 47 74 3a 20 20 20 20 20 20 20 20 e OP_Gt:
1d07d 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 /* same a
1d07e 73 20 54 4b 5f 47 54 2c 20 6a 75 6d 70 2c 20 69 s TK_GT, jump, i
1d07f 6e 31 2c 20 69 6e 33 20 2a 2f 0a 63 61 73 65 20 n1, in3 */.case
1d080 4f 50 5f 47 65 3a 20 7b 20 20 20 20 20 20 20 20 OP_Ge: {
1d081 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 /* same as
1d082 54 4b 5f 47 45 2c 20 6a 75 6d 70 2c 20 69 6e 31 TK_GE, jump, in1
1d083 2c 20 69 6e 33 20 2a 2f 0a 23 69 66 20 30 20 20 , in3 */.#if 0
1d084 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c /* local variabl
1d085 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e es moved into u.
1d086 61 69 20 2a 2f 0a 20 20 69 6e 74 20 72 65 73 3b ai */. int res;
1d087 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 /* R
1d088 65 73 75 6c 74 20 6f 66 20 74 68 65 20 63 6f 6d esult of the com
1d089 70 61 72 69 73 6f 6e 20 6f 66 20 70 49 6e 31 20 parison of pIn1
1d08a 61 67 61 69 6e 73 74 20 70 49 6e 33 20 2a 2f 0a against pIn3 */.
1d08b 20 20 63 68 61 72 20 61 66 66 69 6e 69 74 79 3b char affinity;
1d08c 20 20 20 20 20 20 2f 2a 20 41 66 66 69 6e 69 74 /* Affinit
1d08d 79 20 74 6f 20 75 73 65 20 66 6f 72 20 63 6f 6d y to use for com
1d08e 70 61 72 69 73 6f 6e 20 2a 2f 0a 23 65 6e 64 69 parison */.#endi
1d08f 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 f /* local varia
1d090 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 bles moved into
1d091 75 2e 61 69 20 2a 2f 0a 0a 20 20 69 66 28 20 28 u.ai */.. if( (
1d092 70 49 6e 31 2d 3e 66 6c 61 67 73 20 7c 20 70 49 pIn1->flags | pI
1d093 6e 33 2d 3e 66 6c 61 67 73 29 26 4d 45 4d 5f 4e n3->flags)&MEM_N
1d094 75 6c 6c 20 29 7b 0a 20 20 20 20 2f 2a 20 4f 6e ull ){. /* On
1d095 65 20 6f 72 20 62 6f 74 68 20 6f 70 65 72 61 6e e or both operan
1d096 64 73 20 61 72 65 20 4e 55 4c 4c 20 2a 2f 0a 20 ds are NULL */.
1d097 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 26 if( pOp->p5 &
1d098 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 45 51 20 29 SQLITE_NULLEQ )
1d099 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 53 51 {. /* If SQ
1d09a 4c 49 54 45 5f 4e 55 4c 4c 45 51 20 69 73 20 73 LITE_NULLEQ is s
1d09b 65 74 20 28 77 68 69 63 68 20 77 69 6c 6c 20 6f et (which will o
1d09c 6e 6c 79 20 68 61 70 70 65 6e 20 69 66 20 74 68 nly happen if th
1d09d 65 20 6f 70 65 72 61 74 6f 72 20 69 73 0a 20 20 e operator is.
1d09e 20 20 20 20 2a 2a 20 4f 50 5f 45 71 20 6f 72 20 ** OP_Eq or
1d09f 4f 50 5f 4e 65 29 20 74 68 65 6e 20 74 61 6b 65 OP_Ne) then take
1d0a0 20 74 68 65 20 6a 75 6d 70 20 6f 72 20 6e 6f 74 the jump or not
1d0a1 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 77 68 depending on wh
1d0a2 65 74 68 65 72 0a 20 20 20 20 20 20 2a 2a 20 6f ether. ** o
1d0a3 72 20 6e 6f 74 20 62 6f 74 68 20 6f 70 65 72 61 r not both opera
1d0a4 6e 64 73 20 61 72 65 20 6e 75 6c 6c 2e 0a 20 20 nds are null..
1d0a5 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 */. ass
1d0a6 65 72 74 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 ert( pOp->opcode
1d0a7 3d 3d 4f 50 5f 45 71 20 7c 7c 20 70 4f 70 2d 3e ==OP_Eq || pOp->
1d0a8 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4e 65 20 29 3b opcode==OP_Ne );
1d0a9 0a 20 20 20 20 20 20 75 2e 61 69 2e 72 65 73 20 . u.ai.res
1d0aa 3d 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 = (pIn1->flags &
1d0ab 20 70 49 6e 33 2d 3e 66 6c 61 67 73 20 26 20 4d pIn3->flags & M
1d0ac 45 4d 5f 4e 75 6c 6c 29 3d 3d 30 3b 0a 20 20 20 EM_Null)==0;.
1d0ad 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a }else{. /*
1d0ae 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 45 51 20 69 SQLITE_NULLEQ i
1d0af 73 20 63 6c 65 61 72 20 61 6e 64 20 61 74 20 6c s clear and at l
1d0b0 65 61 73 74 20 6f 6e 65 20 6f 70 65 72 61 6e 64 east one operand
1d0b1 20 69 73 20 4e 55 4c 4c 2c 0a 20 20 20 20 20 20 is NULL,.
1d0b2 2a 2a 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 ** then the resu
1d0b3 6c 74 20 69 73 20 61 6c 77 61 79 73 20 4e 55 4c lt is always NUL
1d0b4 4c 2e 0a 20 20 20 20 20 20 2a 2a 20 54 68 65 20 L.. ** The
1d0b5 6a 75 6d 70 20 69 73 20 74 61 6b 65 6e 20 69 66 jump is taken if
1d0b6 20 74 68 65 20 53 51 4c 49 54 45 5f 4a 55 4d 50 the SQLITE_JUMP
1d0b7 49 46 4e 55 4c 4c 20 62 69 74 20 69 73 20 73 65 IFNULL bit is se
1d0b8 74 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 t.. */.
1d0b9 20 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 26 20 if( pOp->p5 &
1d0ba 53 51 4c 49 54 45 5f 53 54 4f 52 45 50 32 20 29 SQLITE_STOREP2 )
1d0bb 7b 0a 20 20 20 20 20 20 20 20 70 4f 75 74 20 3d {. pOut =
1d0bc 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 &p->aMem[pOp->p
1d0bd 32 5d 3b 0a 20 20 20 20 20 20 20 20 4d 65 6d 53 2];. MemS
1d0be 65 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c etTypeFlag(pOut,
1d0bf 20 4d 45 4d 5f 4e 75 6c 6c 29 3b 0a 20 20 20 20 MEM_Null);.
1d0c0 20 20 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 REGISTER_TRA
1d0c1 43 45 28 70 4f 70 2d 3e 70 32 2c 20 70 4f 75 74 CE(pOp->p2, pOut
1d0c2 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 );. }else i
1d0c3 66 28 20 70 4f 70 2d 3e 70 35 20 26 20 53 51 4c f( pOp->p5 & SQL
1d0c4 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20 29 ITE_JUMPIFNULL )
1d0c5 7b 0a 20 20 20 20 20 20 20 20 70 63 20 3d 20 70 {. pc = p
1d0c6 4f 70 2d 3e 70 32 2d 31 3b 0a 20 20 20 20 20 20 Op->p2-1;.
1d0c7 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 }. break;.
1d0c8 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 }. }else{.
1d0c9 20 20 2f 2a 20 4e 65 69 74 68 65 72 20 6f 70 65 /* Neither ope
1d0ca 72 61 6e 64 20 69 73 20 4e 55 4c 4c 2e 20 20 44 rand is NULL. D
1d0cb 6f 20 61 20 63 6f 6d 70 61 72 69 73 6f 6e 2e 20 o a comparison.
1d0cc 2a 2f 0a 20 20 20 20 75 2e 61 69 2e 61 66 66 69 */. u.ai.affi
1d0cd 6e 69 74 79 20 3d 20 70 4f 70 2d 3e 70 35 20 26 nity = pOp->p5 &
1d0ce 20 53 51 4c 49 54 45 5f 41 46 46 5f 4d 41 53 4b SQLITE_AFF_MASK
1d0cf 3b 0a 20 20 20 20 69 66 28 20 75 2e 61 69 2e 61 ;. if( u.ai.a
1d0d0 66 66 69 6e 69 74 79 20 29 7b 0a 20 20 20 20 20 ffinity ){.
1d0d1 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28 70 applyAffinity(p
1d0d2 49 6e 31 2c 20 75 2e 61 69 2e 61 66 66 69 6e 69 In1, u.ai.affini
1d0d3 74 79 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 ty, encoding);.
1d0d4 20 20 20 20 20 61 70 70 6c 79 41 66 66 69 6e 69 applyAffini
1d0d5 74 79 28 70 49 6e 33 2c 20 75 2e 61 69 2e 61 66 ty(pIn3, u.ai.af
1d0d6 66 69 6e 69 74 79 2c 20 65 6e 63 6f 64 69 6e 67 finity, encoding
1d0d7 29 3b 0a 20 20 20 20 20 20 69 66 28 20 64 62 2d );. if( db-
1d0d8 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 >mallocFailed )
1d0d9 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 goto no_mem;.
1d0da 20 7d 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 }.. assert(
1d0db 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f pOp->p4type==P4_
1d0dc 43 4f 4c 4c 53 45 51 20 7c 7c 20 70 4f 70 2d 3e COLLSEQ || pOp->
1d0dd 70 34 2e 70 43 6f 6c 6c 3d 3d 30 20 29 3b 0a 20 p4.pColl==0 );.
1d0de 20 20 20 45 78 70 61 6e 64 42 6c 6f 62 28 70 49 ExpandBlob(pI
1d0df 6e 31 29 3b 0a 20 20 20 20 45 78 70 61 6e 64 42 n1);. ExpandB
1d0e0 6c 6f 62 28 70 49 6e 33 29 3b 0a 20 20 20 20 75 lob(pIn3);. u
1d0e1 2e 61 69 2e 72 65 73 20 3d 20 73 71 6c 69 74 65 .ai.res = sqlite
1d0e2 33 4d 65 6d 43 6f 6d 70 61 72 65 28 70 49 6e 33 3MemCompare(pIn3
1d0e3 2c 20 70 49 6e 31 2c 20 70 4f 70 2d 3e 70 34 2e , pIn1, pOp->p4.
1d0e4 70 43 6f 6c 6c 29 3b 0a 20 20 7d 0a 20 20 73 77 pColl);. }. sw
1d0e5 69 74 63 68 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 itch( pOp->opcod
1d0e6 65 20 29 7b 0a 20 20 20 20 63 61 73 65 20 4f 50 e ){. case OP
1d0e7 5f 45 71 3a 20 20 20 20 75 2e 61 69 2e 72 65 73 _Eq: u.ai.res
1d0e8 20 3d 20 75 2e 61 69 2e 72 65 73 3d 3d 30 3b 20 = u.ai.res==0;
1d0e9 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 break;. c
1d0ea 61 73 65 20 4f 50 5f 4e 65 3a 20 20 20 20 75 2e ase OP_Ne: u.
1d0eb 61 69 2e 72 65 73 20 3d 20 75 2e 61 69 2e 72 65 ai.res = u.ai.re
1d0ec 73 21 3d 30 3b 20 20 20 20 20 62 72 65 61 6b 3b s!=0; break;
1d0ed 0a 20 20 20 20 63 61 73 65 20 4f 50 5f 4c 74 3a . case OP_Lt:
1d0ee 20 20 20 20 75 2e 61 69 2e 72 65 73 20 3d 20 75 u.ai.res = u
1d0ef 2e 61 69 2e 72 65 73 3c 30 3b 20 20 20 20 20 20 .ai.res<0;
1d0f0 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 break;. case
1d0f1 4f 50 5f 4c 65 3a 20 20 20 20 75 2e 61 69 2e 72 OP_Le: u.ai.r
1d0f2 65 73 20 3d 20 75 2e 61 69 2e 72 65 73 3c 3d 30 es = u.ai.res<=0
1d0f3 3b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 ; break;.
1d0f4 20 63 61 73 65 20 4f 50 5f 47 74 3a 20 20 20 20 case OP_Gt:
1d0f5 75 2e 61 69 2e 72 65 73 20 3d 20 75 2e 61 69 2e u.ai.res = u.ai.
1d0f6 72 65 73 3e 30 3b 20 20 20 20 20 20 62 72 65 61 res>0; brea
1d0f7 6b 3b 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 k;. default:
1d0f8 20 20 20 20 20 20 75 2e 61 69 2e 72 65 73 20 3d u.ai.res =
1d0f9 20 75 2e 61 69 2e 72 65 73 3e 3d 30 3b 20 20 20 u.ai.res>=0;
1d0fa 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 break;. }..
1d0fb 69 66 28 20 70 4f 70 2d 3e 70 35 20 26 20 53 51 if( pOp->p5 & SQ
1d0fc 4c 49 54 45 5f 53 54 4f 52 45 50 32 20 29 7b 0a LITE_STOREP2 ){.
1d0fd 20 20 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 pOut = &p->a
1d0fe 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 Mem[pOp->p2];.
1d0ff 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 MemSetTypeFlag
1d100 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b (pOut, MEM_Int);
1d101 0a 20 20 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d . pOut->u.i =
1d102 20 75 2e 61 69 2e 72 65 73 3b 0a 20 20 20 20 52 u.ai.res;. R
1d103 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f EGISTER_TRACE(pO
1d104 70 2d 3e 70 32 2c 20 70 4f 75 74 29 3b 0a 20 20 p->p2, pOut);.
1d105 7d 65 6c 73 65 20 69 66 28 20 75 2e 61 69 2e 72 }else if( u.ai.r
1d106 65 73 20 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 es ){. pc = p
1d107 4f 70 2d 3e 70 32 2d 31 3b 0a 20 20 7d 0a 20 20 Op->p2-1;. }.
1d108 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 break;.}../* Opc
1d109 6f 64 65 3a 20 50 65 72 6d 75 74 61 74 69 6f 6e ode: Permutation
1d10a 20 2a 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a * * * P4 *.**.*
1d10b 2a 20 53 65 74 20 74 68 65 20 70 65 72 6d 75 74 * Set the permut
1d10c 61 74 69 6f 6e 20 75 73 65 64 20 62 79 20 74 68 ation used by th
1d10d 65 20 4f 50 5f 43 6f 6d 70 61 72 65 20 6f 70 65 e OP_Compare ope
1d10e 72 61 74 6f 72 20 74 6f 20 62 65 20 74 68 65 20 rator to be the
1d10f 61 72 72 61 79 0a 2a 2a 20 6f 66 20 69 6e 74 65 array.** of inte
1d110 67 65 72 73 20 69 6e 20 50 34 2e 0a 2a 2a 0a 2a gers in P4..**.*
1d111 2a 20 54 68 65 20 70 65 72 6d 75 74 61 74 69 6f * The permutatio
1d112 6e 20 69 73 20 6f 6e 6c 79 20 76 61 6c 69 64 20 n is only valid
1d113 75 6e 74 69 6c 20 74 68 65 20 6e 65 78 74 20 4f until the next O
1d114 50 5f 50 65 72 6d 75 74 61 74 69 6f 6e 2c 20 4f P_Permutation, O
1d115 50 5f 43 6f 6d 70 61 72 65 2c 0a 2a 2a 20 4f 50 P_Compare,.** OP
1d116 5f 48 61 6c 74 2c 20 6f 72 20 4f 50 5f 52 65 73 _Halt, or OP_Res
1d117 75 6c 74 52 6f 77 2e 20 20 54 79 70 69 63 61 6c ultRow. Typical
1d118 6c 79 20 74 68 65 20 4f 50 5f 50 65 72 6d 75 74 ly the OP_Permut
1d119 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6f 63 63 ation should occ
1d11a 75 72 0a 2a 2a 20 69 6d 6d 65 64 69 61 74 65 6c ur.** immediatel
1d11b 79 20 70 72 69 6f 72 20 74 6f 20 74 68 65 20 4f y prior to the O
1d11c 50 5f 43 6f 6d 70 61 72 65 2e 0a 2a 2f 0a 63 61 P_Compare..*/.ca
1d11d 73 65 20 4f 50 5f 50 65 72 6d 75 74 61 74 69 6f se OP_Permutatio
1d11e 6e 3a 20 7b 0a 20 20 61 73 73 65 72 74 28 20 70 n: {. assert( p
1d11f 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 49 Op->p4type==P4_I
1d120 4e 54 41 52 52 41 59 20 29 3b 0a 20 20 61 73 73 NTARRAY );. ass
1d121 65 72 74 28 20 70 4f 70 2d 3e 70 34 2e 61 69 20 ert( pOp->p4.ai
1d122 29 3b 0a 20 20 61 50 65 72 6d 75 74 65 20 3d 20 );. aPermute =
1d123 70 4f 70 2d 3e 70 34 2e 61 69 3b 0a 20 20 62 72 pOp->p4.ai;. br
1d124 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1d125 65 3a 20 43 6f 6d 70 61 72 65 20 50 31 20 50 32 e: Compare P1 P2
1d126 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 43 P3 P4 *.**.** C
1d127 6f 6d 70 61 72 65 20 74 6f 20 76 65 63 74 6f 72 ompare to vector
1d128 73 20 6f 66 20 72 65 67 69 73 74 65 72 73 20 69 s of registers i
1d129 6e 20 72 65 67 28 50 31 29 2e 2e 72 65 67 28 50 n reg(P1)..reg(P
1d12a 31 2b 50 33 2d 31 29 20 28 61 6c 6c 20 74 68 69 1+P3-1) (all thi
1d12b 73 0a 2a 2a 20 6f 6e 65 20 22 41 22 29 20 61 6e s.** one "A") an
1d12c 64 20 69 6e 20 72 65 67 28 50 32 29 2e 2e 72 65 d in reg(P2)..re
1d12d 67 28 50 32 2b 50 33 2d 31 29 20 28 22 42 22 29 g(P2+P3-1) ("B")
1d12e 2e 20 20 53 61 76 65 20 74 68 65 20 72 65 73 75 . Save the resu
1d12f 6c 74 20 6f 66 0a 2a 2a 20 74 68 65 20 63 6f 6d lt of.** the com
1d130 70 61 72 69 73 6f 6e 20 66 6f 72 20 75 73 65 20 parison for use
1d131 62 79 20 74 68 65 20 6e 65 78 74 20 4f 50 5f 4a by the next OP_J
1d132 75 6d 70 20 69 6e 73 74 72 75 63 74 2e 0a 2a 2a ump instruct..**
1d133 0a 2a 2a 20 50 34 20 69 73 20 61 20 4b 65 79 49 .** P4 is a KeyI
1d134 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 74 68 nfo structure th
1d135 61 74 20 64 65 66 69 6e 65 73 20 63 6f 6c 6c 61 at defines colla
1d136 74 69 6e 67 20 73 65 71 75 65 6e 63 65 73 20 61 ting sequences a
1d137 6e 64 20 73 6f 72 74 0a 2a 2a 20 6f 72 64 65 72 nd sort.** order
1d138 73 20 66 6f 72 20 74 68 65 20 63 6f 6d 70 61 72 s for the compar
1d139 69 73 6f 6e 2e 20 20 54 68 65 20 70 65 72 6d 75 ison. The permu
1d13a 74 61 74 69 6f 6e 20 61 70 70 6c 69 65 73 20 74 tation applies t
1d13b 6f 20 72 65 67 69 73 74 65 72 73 0a 2a 2a 20 6f o registers.** o
1d13c 6e 6c 79 2e 20 20 54 68 65 20 4b 65 79 49 6e 66 nly. The KeyInf
1d13d 6f 20 65 6c 65 6d 65 6e 74 73 20 61 72 65 20 75 o elements are u
1d13e 73 65 64 20 73 65 71 75 65 6e 74 69 61 6c 6c 79 sed sequentially
1d13f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6d 70 ..**.** The comp
1d140 61 72 69 73 6f 6e 20 69 73 20 61 20 73 6f 72 74 arison is a sort
1d141 20 63 6f 6d 70 61 72 69 73 6f 6e 2c 20 73 6f 20 comparison, so
1d142 4e 55 4c 4c 73 20 63 6f 6d 70 61 72 65 20 65 71 NULLs compare eq
1d143 75 61 6c 2c 0a 2a 2a 20 4e 55 4c 4c 73 20 61 72 ual,.** NULLs ar
1d144 65 20 6c 65 73 73 20 74 68 61 6e 20 6e 75 6d 62 e less than numb
1d145 65 72 73 2c 20 6e 75 6d 62 65 72 73 20 61 72 65 ers, numbers are
1d146 20 6c 65 73 73 20 74 68 61 6e 20 73 74 72 69 6e less than strin
1d147 67 73 2c 0a 2a 2a 20 61 6e 64 20 73 74 72 69 6e gs,.** and strin
1d148 67 73 20 61 72 65 20 6c 65 73 73 20 74 68 61 6e gs are less than
1d149 20 62 6c 6f 62 73 2e 0a 2a 2f 0a 63 61 73 65 20 blobs..*/.case
1d14a 4f 50 5f 43 6f 6d 70 61 72 65 3a 20 7b 0a 23 69 OP_Compare: {.#i
1d14b 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 f 0 /* local va
1d14c 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1d14d 74 6f 20 75 2e 61 6a 20 2a 2f 0a 20 20 69 6e 74 to u.aj */. int
1d14e 20 6e 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 n;. int i;. i
1d14f 6e 74 20 70 31 3b 0a 20 20 69 6e 74 20 70 32 3b nt p1;. int p2;
1d150 0a 20 20 63 6f 6e 73 74 20 4b 65 79 49 6e 66 6f . const KeyInfo
1d151 20 2a 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 69 6e *pKeyInfo;. in
1d152 74 20 69 64 78 3b 0a 20 20 43 6f 6c 6c 53 65 71 t idx;. CollSeq
1d153 20 2a 70 43 6f 6c 6c 3b 20 20 20 20 2f 2a 20 43 *pColl; /* C
1d154 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 ollating sequenc
1d155 65 20 74 6f 20 75 73 65 20 6f 6e 20 74 68 69 73 e to use on this
1d156 20 74 65 72 6d 20 2a 2f 0a 20 20 69 6e 74 20 62 term */. int b
1d157 52 65 76 3b 20 20 20 20 20 20 20 20 20 20 2f 2a Rev; /*
1d158 20 54 72 75 65 20 66 6f 72 20 44 45 53 43 45 4e True for DESCEN
1d159 44 49 4e 47 20 73 6f 72 74 20 6f 72 64 65 72 20 DING sort order
1d15a 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 */.#endif /* loc
1d15b 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1d15c 65 64 20 69 6e 74 6f 20 75 2e 61 6a 20 2a 2f 0a ed into u.aj */.
1d15d 0a 20 20 75 2e 61 6a 2e 6e 20 3d 20 70 4f 70 2d . u.aj.n = pOp-
1d15e 3e 70 33 3b 0a 20 20 75 2e 61 6a 2e 70 4b 65 79 >p3;. u.aj.pKey
1d15f 49 6e 66 6f 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 Info = pOp->p4.p
1d160 4b 65 79 49 6e 66 6f 3b 0a 20 20 61 73 73 65 72 KeyInfo;. asser
1d161 74 28 20 75 2e 61 6a 2e 6e 3e 30 20 29 3b 0a 20 t( u.aj.n>0 );.
1d162 20 61 73 73 65 72 74 28 20 75 2e 61 6a 2e 70 4b assert( u.aj.pK
1d163 65 79 49 6e 66 6f 21 3d 30 20 29 3b 0a 20 20 75 eyInfo!=0 );. u
1d164 2e 61 6a 2e 70 31 20 3d 20 70 4f 70 2d 3e 70 31 .aj.p1 = pOp->p1
1d165 3b 0a 20 20 75 2e 61 6a 2e 70 32 20 3d 20 70 4f ;. u.aj.p2 = pO
1d166 70 2d 3e 70 32 3b 0a 23 69 66 20 53 51 4c 49 54 p->p2;.#if SQLIT
1d167 45 5f 44 45 42 55 47 0a 20 20 69 66 28 20 61 50 E_DEBUG. if( aP
1d168 65 72 6d 75 74 65 20 29 7b 0a 20 20 20 20 69 6e ermute ){. in
1d169 74 20 6b 2c 20 6d 78 20 3d 20 30 3b 0a 20 20 20 t k, mx = 0;.
1d16a 20 66 6f 72 28 6b 3d 30 3b 20 6b 3c 75 2e 61 6a for(k=0; k<u.aj
1d16b 2e 6e 3b 20 6b 2b 2b 29 20 69 66 28 20 61 50 65 .n; k++) if( aPe
1d16c 72 6d 75 74 65 5b 6b 5d 3e 6d 78 20 29 20 6d 78 rmute[k]>mx ) mx
1d16d 20 3d 20 61 50 65 72 6d 75 74 65 5b 6b 5d 3b 0a = aPermute[k];.
1d16e 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 61 6a assert( u.aj
1d16f 2e 70 31 3e 30 20 26 26 20 75 2e 61 6a 2e 70 31 .p1>0 && u.aj.p1
1d170 2b 6d 78 3c 3d 70 2d 3e 6e 4d 65 6d 2b 31 20 29 +mx<=p->nMem+1 )
1d171 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 2e ;. assert( u.
1d172 61 6a 2e 70 32 3e 30 20 26 26 20 75 2e 61 6a 2e aj.p2>0 && u.aj.
1d173 70 32 2b 6d 78 3c 3d 70 2d 3e 6e 4d 65 6d 2b 31 p2+mx<=p->nMem+1
1d174 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 );. }else{.
1d175 20 61 73 73 65 72 74 28 20 75 2e 61 6a 2e 70 31 assert( u.aj.p1
1d176 3e 30 20 26 26 20 75 2e 61 6a 2e 70 31 2b 75 2e >0 && u.aj.p1+u.
1d177 61 6a 2e 6e 3c 3d 70 2d 3e 6e 4d 65 6d 2b 31 20 aj.n<=p->nMem+1
1d178 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 );. assert( u
1d179 2e 61 6a 2e 70 32 3e 30 20 26 26 20 75 2e 61 6a .aj.p2>0 && u.aj
1d17a 2e 70 32 2b 75 2e 61 6a 2e 6e 3c 3d 70 2d 3e 6e .p2+u.aj.n<=p->n
1d17b 4d 65 6d 2b 31 20 29 3b 0a 20 20 7d 0a 23 65 6e Mem+1 );. }.#en
1d17c 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 44 45 dif /* SQLITE_DE
1d17d 42 55 47 20 2a 2f 0a 20 20 66 6f 72 28 75 2e 61 BUG */. for(u.a
1d17e 6a 2e 69 3d 30 3b 20 75 2e 61 6a 2e 69 3c 75 2e j.i=0; u.aj.i<u.
1d17f 61 6a 2e 6e 3b 20 75 2e 61 6a 2e 69 2b 2b 29 7b aj.n; u.aj.i++){
1d180 0a 20 20 20 20 75 2e 61 6a 2e 69 64 78 20 3d 20 . u.aj.idx =
1d181 61 50 65 72 6d 75 74 65 20 3f 20 61 50 65 72 6d aPermute ? aPerm
1d182 75 74 65 5b 75 2e 61 6a 2e 69 5d 20 3a 20 75 2e ute[u.aj.i] : u.
1d183 61 6a 2e 69 3b 0a 20 20 20 20 52 45 47 49 53 54 aj.i;. REGIST
1d184 45 52 5f 54 52 41 43 45 28 75 2e 61 6a 2e 70 31 ER_TRACE(u.aj.p1
1d185 2b 75 2e 61 6a 2e 69 64 78 2c 20 26 70 2d 3e 61 +u.aj.idx, &p->a
1d186 4d 65 6d 5b 75 2e 61 6a 2e 70 31 2b 75 2e 61 6a Mem[u.aj.p1+u.aj
1d187 2e 69 64 78 5d 29 3b 0a 20 20 20 20 52 45 47 49 .idx]);. REGI
1d188 53 54 45 52 5f 54 52 41 43 45 28 75 2e 61 6a 2e STER_TRACE(u.aj.
1d189 70 32 2b 75 2e 61 6a 2e 69 64 78 2c 20 26 70 2d p2+u.aj.idx, &p-
1d18a 3e 61 4d 65 6d 5b 75 2e 61 6a 2e 70 32 2b 75 2e >aMem[u.aj.p2+u.
1d18b 61 6a 2e 69 64 78 5d 29 3b 0a 20 20 20 20 61 73 aj.idx]);. as
1d18c 73 65 72 74 28 20 75 2e 61 6a 2e 69 3c 75 2e 61 sert( u.aj.i<u.a
1d18d 6a 2e 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 j.pKeyInfo->nFie
1d18e 6c 64 20 29 3b 0a 20 20 20 20 75 2e 61 6a 2e 70 ld );. u.aj.p
1d18f 43 6f 6c 6c 20 3d 20 75 2e 61 6a 2e 70 4b 65 79 Coll = u.aj.pKey
1d190 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 75 2e 61 6a Info->aColl[u.aj
1d191 2e 69 5d 3b 0a 20 20 20 20 75 2e 61 6a 2e 62 52 .i];. u.aj.bR
1d192 65 76 20 3d 20 75 2e 61 6a 2e 70 4b 65 79 49 6e ev = u.aj.pKeyIn
1d193 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 75 fo->aSortOrder[u
1d194 2e 61 6a 2e 69 5d 3b 0a 20 20 20 20 69 43 6f 6d .aj.i];. iCom
1d195 70 61 72 65 20 3d 20 73 71 6c 69 74 65 33 4d 65 pare = sqlite3Me
1d196 6d 43 6f 6d 70 61 72 65 28 26 70 2d 3e 61 4d 65 mCompare(&p->aMe
1d197 6d 5b 75 2e 61 6a 2e 70 31 2b 75 2e 61 6a 2e 69 m[u.aj.p1+u.aj.i
1d198 64 78 5d 2c 20 26 70 2d 3e 61 4d 65 6d 5b 75 2e dx], &p->aMem[u.
1d199 61 6a 2e 70 32 2b 75 2e 61 6a 2e 69 64 78 5d 2c aj.p2+u.aj.idx],
1d19a 20 75 2e 61 6a 2e 70 43 6f 6c 6c 29 3b 0a 20 20 u.aj.pColl);.
1d19b 20 20 69 66 28 20 69 43 6f 6d 70 61 72 65 20 29 if( iCompare )
1d19c 7b 0a 20 20 20 20 20 20 69 66 28 20 75 2e 61 6a {. if( u.aj
1d19d 2e 62 52 65 76 20 29 20 69 43 6f 6d 70 61 72 65 .bRev ) iCompare
1d19e 20 3d 20 2d 69 43 6f 6d 70 61 72 65 3b 0a 20 20 = -iCompare;.
1d19f 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
1d1a0 0a 20 20 7d 0a 20 20 61 50 65 72 6d 75 74 65 20 . }. aPermute
1d1a1 3d 20 30 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a = 0;. break;.}.
1d1a2 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4a 75 6d 70 ./* Opcode: Jump
1d1a3 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a P1 P2 P3 * *.**
1d1a4 0a 2a 2a 20 4a 75 6d 70 20 74 6f 20 74 68 65 20 .** Jump to the
1d1a5 69 6e 73 74 72 75 63 74 69 6f 6e 20 61 74 20 61 instruction at a
1d1a6 64 64 72 65 73 73 20 50 31 2c 20 50 32 2c 20 6f ddress P1, P2, o
1d1a7 72 20 50 33 20 64 65 70 65 6e 64 69 6e 67 20 6f r P3 depending o
1d1a8 6e 20 77 68 65 74 68 65 72 0a 2a 2a 20 69 6e 20 n whether.** in
1d1a9 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 the most recent
1d1aa 4f 50 5f 43 6f 6d 70 61 72 65 20 69 6e 73 74 72 OP_Compare instr
1d1ab 75 63 74 69 6f 6e 20 74 68 65 20 50 31 20 76 65 uction the P1 ve
1d1ac 63 74 6f 72 20 77 61 73 20 6c 65 73 73 20 74 68 ctor was less th
1d1ad 61 6e 0a 2a 2a 20 65 71 75 61 6c 20 74 6f 2c 20 an.** equal to,
1d1ae 6f 72 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 or greater than
1d1af 74 68 65 20 50 32 20 76 65 63 74 6f 72 2c 20 72 the P2 vector, r
1d1b0 65 73 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2f 0a espectively..*/.
1d1b1 63 61 73 65 20 4f 50 5f 4a 75 6d 70 3a 20 7b 20 case OP_Jump: {
1d1b2 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6a /* j
1d1b3 75 6d 70 20 2a 2f 0a 20 20 69 66 28 20 69 43 6f ump */. if( iCo
1d1b4 6d 70 61 72 65 3c 30 20 29 7b 0a 20 20 20 20 70 mpare<0 ){. p
1d1b5 63 20 3d 20 70 4f 70 2d 3e 70 31 20 2d 20 31 3b c = pOp->p1 - 1;
1d1b6 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 69 43 6f . }else if( iCo
1d1b7 6d 70 61 72 65 3d 3d 30 20 29 7b 0a 20 20 20 20 mpare==0 ){.
1d1b8 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 pc = pOp->p2 - 1
1d1b9 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 ;. }else{. p
1d1ba 63 20 3d 20 70 4f 70 2d 3e 70 33 20 2d 20 31 3b c = pOp->p3 - 1;
1d1bb 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a . }. break;.}.
1d1bc 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 41 6e 64 20 ./* Opcode: And
1d1bd 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a P1 P2 P3 * *.**.
1d1be 2a 2a 20 54 61 6b 65 20 74 68 65 20 6c 6f 67 69 ** Take the logi
1d1bf 63 61 6c 20 41 4e 44 20 6f 66 20 74 68 65 20 76 cal AND of the v
1d1c0 61 6c 75 65 73 20 69 6e 20 72 65 67 69 73 74 65 alues in registe
1d1c1 72 73 20 50 31 20 61 6e 64 20 50 32 20 61 6e 64 rs P1 and P2 and
1d1c2 0a 2a 2a 20 77 72 69 74 65 20 74 68 65 20 72 65 .** write the re
1d1c3 73 75 6c 74 20 69 6e 74 6f 20 72 65 67 69 73 74 sult into regist
1d1c4 65 72 20 50 33 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 er P3..**.** If
1d1c5 65 69 74 68 65 72 20 50 31 20 6f 72 20 50 32 20 either P1 or P2
1d1c6 69 73 20 30 20 28 66 61 6c 73 65 29 20 74 68 65 is 0 (false) the
1d1c7 6e 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 n the result is
1d1c8 30 20 65 76 65 6e 20 69 66 0a 2a 2a 20 74 68 65 0 even if.** the
1d1c9 20 6f 74 68 65 72 20 69 6e 70 75 74 20 69 73 20 other input is
1d1ca 4e 55 4c 4c 2e 20 20 41 20 4e 55 4c 4c 20 61 6e NULL. A NULL an
1d1cb 64 20 74 72 75 65 20 6f 72 20 74 77 6f 20 4e 55 d true or two NU
1d1cc 4c 4c 73 20 67 69 76 65 0a 2a 2a 20 61 20 4e 55 LLs give.** a NU
1d1cd 4c 4c 20 6f 75 74 70 75 74 2e 0a 2a 2f 0a 2f 2a LL output..*/./*
1d1ce 20 4f 70 63 6f 64 65 3a 20 4f 72 20 50 31 20 50 Opcode: Or P1 P
1d1cf 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 2 P3 * *.**.** T
1d1d0 61 6b 65 20 74 68 65 20 6c 6f 67 69 63 61 6c 20 ake the logical
1d1d1 4f 52 20 6f 66 20 74 68 65 20 76 61 6c 75 65 73 OR of the values
1d1d2 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 in register P1
1d1d3 61 6e 64 20 50 32 20 61 6e 64 0a 2a 2a 20 73 74 and P2 and.** st
1d1d4 6f 72 65 20 74 68 65 20 61 6e 73 77 65 72 20 69 ore the answer i
1d1d5 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a n register P3..*
1d1d6 2a 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 50 *.** If either P
1d1d7 31 20 6f 72 20 50 32 20 69 73 20 6e 6f 6e 7a 65 1 or P2 is nonze
1d1d8 72 6f 20 28 74 72 75 65 29 20 74 68 65 6e 20 74 ro (true) then t
1d1d9 68 65 20 72 65 73 75 6c 74 20 69 73 20 31 20 28 he result is 1 (
1d1da 74 72 75 65 29 0a 2a 2a 20 65 76 65 6e 20 69 66 true).** even if
1d1db 20 74 68 65 20 6f 74 68 65 72 20 69 6e 70 75 74 the other input
1d1dc 20 69 73 20 4e 55 4c 4c 2e 20 20 41 20 4e 55 4c is NULL. A NUL
1d1dd 4c 20 61 6e 64 20 66 61 6c 73 65 20 6f 72 20 74 L and false or t
1d1de 77 6f 20 4e 55 4c 4c 73 0a 2a 2a 20 67 69 76 65 wo NULLs.** give
1d1df 20 61 20 4e 55 4c 4c 20 6f 75 74 70 75 74 2e 0a a NULL output..
1d1e0 2a 2f 0a 63 61 73 65 20 4f 50 5f 41 6e 64 3a 20 */.case OP_And:
1d1e1 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1d1e2 73 61 6d 65 20 61 73 20 54 4b 5f 41 4e 44 2c 20 same as TK_AND,
1d1e3 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a in1, in2, out3 *
1d1e4 2f 0a 63 61 73 65 20 4f 50 5f 4f 72 3a 20 7b 20 /.case OP_Or: {
1d1e5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 /* s
1d1e6 61 6d 65 20 61 73 20 54 4b 5f 4f 52 2c 20 69 6e ame as TK_OR, in
1d1e7 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 1, in2, out3 */.
1d1e8 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 #if 0 /* local
1d1e9 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 variables moved
1d1ea 69 6e 74 6f 20 75 2e 61 6b 20 2a 2f 0a 20 20 69 into u.ak */. i
1d1eb 6e 74 20 76 31 3b 20 20 20 20 2f 2a 20 4c 65 66 nt v1; /* Lef
1d1ec 74 20 6f 70 65 72 61 6e 64 3a 20 20 30 3d 3d 46 t operand: 0==F
1d1ed 41 4c 53 45 2c 20 31 3d 3d 54 52 55 45 2c 20 32 ALSE, 1==TRUE, 2
1d1ee 3d 3d 55 4e 4b 4e 4f 57 4e 20 6f 72 20 4e 55 4c ==UNKNOWN or NUL
1d1ef 4c 20 2a 2f 0a 20 20 69 6e 74 20 76 32 3b 20 20 L */. int v2;
1d1f0 20 20 2f 2a 20 52 69 67 68 74 20 6f 70 65 72 61 /* Right opera
1d1f1 6e 64 3a 20 30 3d 3d 46 41 4c 53 45 2c 20 31 3d nd: 0==FALSE, 1=
1d1f2 3d 54 52 55 45 2c 20 32 3d 3d 55 4e 4b 4e 4f 57 =TRUE, 2==UNKNOW
1d1f3 4e 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 23 65 6e N or NULL */.#en
1d1f4 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 dif /* local var
1d1f5 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 iables moved int
1d1f6 6f 20 75 2e 61 6b 20 2a 2f 0a 0a 20 20 69 66 28 o u.ak */.. if(
1d1f7 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d pIn1->flags & M
1d1f8 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 75 EM_Null ){. u
1d1f9 2e 61 6b 2e 76 31 20 3d 20 32 3b 0a 20 20 7d 65 .ak.v1 = 2;. }e
1d1fa 6c 73 65 7b 0a 20 20 20 20 75 2e 61 6b 2e 76 31 lse{. u.ak.v1
1d1fb 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e = sqlite3VdbeIn
1d1fc 74 56 61 6c 75 65 28 70 49 6e 31 29 21 3d 30 3b tValue(pIn1)!=0;
1d1fd 0a 20 20 7d 0a 20 20 69 66 28 20 70 49 6e 32 2d . }. if( pIn2-
1d1fe 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c >flags & MEM_Nul
1d1ff 6c 20 29 7b 0a 20 20 20 20 75 2e 61 6b 2e 76 32 l ){. u.ak.v2
1d200 20 3d 20 32 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 = 2;. }else{.
1d201 20 20 20 75 2e 61 6b 2e 76 32 20 3d 20 73 71 6c u.ak.v2 = sql
1d202 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75 65 ite3VdbeIntValue
1d203 28 70 49 6e 32 29 21 3d 30 3b 0a 20 20 7d 0a 20 (pIn2)!=0;. }.
1d204 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 if( pOp->opcode
1d205 3d 3d 4f 50 5f 41 6e 64 20 29 7b 0a 20 20 20 20 ==OP_And ){.
1d206 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 static const uns
1d207 69 67 6e 65 64 20 63 68 61 72 20 61 6e 64 5f 6c igned char and_l
1d208 6f 67 69 63 5b 5d 20 3d 20 7b 20 30 2c 20 30 2c ogic[] = { 0, 0,
1d209 20 30 2c 20 30 2c 20 31 2c 20 32 2c 20 30 2c 20 0, 0, 1, 2, 0,
1d20a 32 2c 20 32 20 7d 3b 0a 20 20 20 20 75 2e 61 6b 2, 2 };. u.ak
1d20b 2e 76 31 20 3d 20 61 6e 64 5f 6c 6f 67 69 63 5b .v1 = and_logic[
1d20c 75 2e 61 6b 2e 76 31 2a 33 2b 75 2e 61 6b 2e 76 u.ak.v1*3+u.ak.v
1d20d 32 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 2];. }else{.
1d20e 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e static const un
1d20f 73 69 67 6e 65 64 20 63 68 61 72 20 6f 72 5f 6c signed char or_l
1d210 6f 67 69 63 5b 5d 20 3d 20 7b 20 30 2c 20 31 2c ogic[] = { 0, 1,
1d211 20 32 2c 20 31 2c 20 31 2c 20 31 2c 20 32 2c 20 2, 1, 1, 1, 2,
1d212 31 2c 20 32 20 7d 3b 0a 20 20 20 20 75 2e 61 6b 1, 2 };. u.ak
1d213 2e 76 31 20 3d 20 6f 72 5f 6c 6f 67 69 63 5b 75 .v1 = or_logic[u
1d214 2e 61 6b 2e 76 31 2a 33 2b 75 2e 61 6b 2e 76 32 .ak.v1*3+u.ak.v2
1d215 5d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 75 2e 61 ];. }. if( u.a
1d216 6b 2e 76 31 3d 3d 32 20 29 7b 0a 20 20 20 20 4d k.v1==2 ){. M
1d217 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f emSetTypeFlag(pO
1d218 75 74 2c 20 4d 45 4d 5f 4e 75 6c 6c 29 3b 0a 20 ut, MEM_Null);.
1d219 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4f 75 74 }else{. pOut
1d21a 2d 3e 75 2e 69 20 3d 20 75 2e 61 6b 2e 76 31 3b ->u.i = u.ak.v1;
1d21b 0a 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 . MemSetTypeF
1d21c 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e lag(pOut, MEM_In
1d21d 74 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b t);. }. break;
1d21e 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e .}../* Opcode: N
1d21f 6f 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a ot P1 P2 * * *.*
1d220 2a 0a 2a 2a 20 49 6e 74 65 72 70 72 65 74 20 74 *.** Interpret t
1d221 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 he value in regi
1d222 73 74 65 72 20 50 31 20 61 73 20 61 20 62 6f 6f ster P1 as a boo
1d223 6c 65 61 6e 20 76 61 6c 75 65 2e 20 20 53 74 6f lean value. Sto
1d224 72 65 20 74 68 65 0a 2a 2a 20 62 6f 6f 6c 65 61 re the.** boolea
1d225 6e 20 63 6f 6d 70 6c 65 6d 65 6e 74 20 69 6e 20 n complement in
1d226 72 65 67 69 73 74 65 72 20 50 32 2e 20 20 49 66 register P2. If
1d227 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 the value in re
1d228 67 69 73 74 65 72 20 50 31 20 69 73 20 0a 2a 2a gister P1 is .**
1d229 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 61 20 4e 55 NULL, then a NU
1d22a 4c 4c 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 LL is stored in
1d22b 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e P2..*/.case OP_N
1d22c 6f 74 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 ot: {
1d22d 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 /* same as
1d22e 54 4b 5f 4e 4f 54 2c 20 69 6e 31 20 2a 2f 0a 20 TK_NOT, in1 */.
1d22f 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d pOut = &p->aMem
1d230 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 69 66 28 [pOp->p2];. if(
1d231 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d pIn1->flags & M
1d232 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 73 EM_Null ){. s
1d233 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 qlite3VdbeMemSet
1d234 4e 75 6c 6c 28 70 4f 75 74 29 3b 0a 20 20 7d 65 Null(pOut);. }e
1d235 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 lse{. sqlite3
1d236 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 36 34 28 VdbeMemSetInt64(
1d237 70 4f 75 74 2c 20 21 73 71 6c 69 74 65 33 56 64 pOut, !sqlite3Vd
1d238 62 65 49 6e 74 56 61 6c 75 65 28 70 49 6e 31 29 beIntValue(pIn1)
1d239 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a );. }. break;.
1d23a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 42 69 }../* Opcode: Bi
1d23b 74 4e 6f 74 20 50 31 20 50 32 20 2a 20 2a 20 2a tNot P1 P2 * * *
1d23c 0a 2a 2a 0a 2a 2a 20 49 6e 74 65 72 70 72 65 74 .**.** Interpret
1d23d 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 the content of
1d23e 72 65 67 69 73 74 65 72 20 50 31 20 61 73 20 61 register P1 as a
1d23f 6e 20 69 6e 74 65 67 65 72 2e 20 20 53 74 6f 72 n integer. Stor
1d240 65 20 74 68 65 0a 2a 2a 20 6f 6e 65 73 2d 63 6f e the.** ones-co
1d241 6d 70 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 mplement of the
1d242 50 31 20 76 61 6c 75 65 20 69 6e 74 6f 20 72 65 P1 value into re
1d243 67 69 73 74 65 72 20 50 32 2e 20 20 49 66 20 50 gister P2. If P
1d244 31 20 68 6f 6c 64 73 0a 2a 2a 20 61 20 4e 55 4c 1 holds.** a NUL
1d245 4c 20 74 68 65 6e 20 73 74 6f 72 65 20 61 20 4e L then store a N
1d246 55 4c 4c 20 69 6e 20 50 32 2e 0a 2a 2f 0a 63 61 ULL in P2..*/.ca
1d247 73 65 20 4f 50 5f 42 69 74 4e 6f 74 3a 20 7b 20 se OP_BitNot: {
1d248 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 /* s
1d249 61 6d 65 20 61 73 20 54 4b 5f 42 49 54 4e 4f 54 ame as TK_BITNOT
1d24a 2c 20 69 6e 31 20 2a 2f 0a 20 20 70 4f 75 74 20 , in1 */. pOut
1d24b 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e = &p->aMem[pOp->
1d24c 70 32 5d 3b 0a 20 20 69 66 28 20 70 49 6e 31 2d p2];. if( pIn1-
1d24d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c >flags & MEM_Nul
1d24e 6c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 l ){. sqlite3
1d24f 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 VdbeMemSetNull(p
1d250 4f 75 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 Out);. }else{.
1d251 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 sqlite3VdbeMe
1d252 6d 53 65 74 49 6e 74 36 34 28 70 4f 75 74 2c 20 mSetInt64(pOut,
1d253 7e 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56 ~sqlite3VdbeIntV
1d254 61 6c 75 65 28 70 49 6e 31 29 29 3b 0a 20 20 7d alue(pIn1));. }
1d255 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 . break;.}../*
1d256 4f 70 63 6f 64 65 3a 20 49 66 20 50 31 20 50 32 Opcode: If P1 P2
1d257 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75 P3 * *.**.** Ju
1d258 6d 70 20 74 6f 20 50 32 20 69 66 20 74 68 65 20 mp to P2 if the
1d259 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 value in registe
1d25a 72 20 50 31 20 69 73 20 74 72 75 65 2e 20 20 54 r P1 is true. T
1d25b 68 65 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 69 he value is.** i
1d25c 73 20 63 6f 6e 73 69 64 65 72 65 64 20 74 72 75 s considered tru
1d25d 65 20 69 66 20 69 74 20 69 73 20 6e 75 6d 65 72 e if it is numer
1d25e 69 63 20 61 6e 64 20 6e 6f 6e 2d 7a 65 72 6f 2e ic and non-zero.
1d25f 20 20 49 66 20 74 68 65 20 76 61 6c 75 65 0a 2a If the value.*
1d260 2a 20 69 6e 20 50 31 20 69 73 20 4e 55 4c 4c 20 * in P1 is NULL
1d261 74 68 65 6e 20 74 61 6b 65 20 74 68 65 20 6a 75 then take the ju
1d262 6d 70 20 69 66 20 50 33 20 69 73 20 74 72 75 65 mp if P3 is true
1d263 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ..*/./* Opcode:
1d264 49 66 4e 6f 74 20 50 31 20 50 32 20 50 33 20 2a IfNot P1 P2 P3 *
1d265 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f *.**.** Jump to
1d266 20 50 32 20 69 66 20 74 68 65 20 76 61 6c 75 65 P2 if the value
1d267 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 in register P1
1d268 69 73 20 46 61 6c 73 65 2e 20 20 54 68 65 20 76 is False. The v
1d269 61 6c 75 65 20 69 73 0a 2a 2a 20 69 73 20 63 6f alue is.** is co
1d26a 6e 73 69 64 65 72 65 64 20 74 72 75 65 20 69 66 nsidered true if
1d26b 20 69 74 20 68 61 73 20 61 20 6e 75 6d 65 72 69 it has a numeri
1d26c 63 20 76 61 6c 75 65 20 6f 66 20 7a 65 72 6f 2e c value of zero.
1d26d 20 20 49 66 20 74 68 65 20 76 61 6c 75 65 0a 2a If the value.*
1d26e 2a 20 69 6e 20 50 31 20 69 73 20 4e 55 4c 4c 20 * in P1 is NULL
1d26f 74 68 65 6e 20 74 61 6b 65 20 74 68 65 20 6a 75 then take the ju
1d270 6d 70 20 69 66 20 50 33 20 69 73 20 74 72 75 65 mp if P3 is true
1d271 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 66 3a ..*/.case OP_If:
1d272 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d273 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f /* jump, in1 */
1d274 0a 63 61 73 65 20 4f 50 5f 49 66 4e 6f 74 3a 20 .case OP_IfNot:
1d275 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 { /*
1d276 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a 23 69 66 jump, in1 */.#if
1d277 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 0 /* local var
1d278 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 iables moved int
1d279 6f 20 75 2e 61 6c 20 2a 2f 0a 20 20 69 6e 74 20 o u.al */. int
1d27a 63 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 c;.#endif /* loc
1d27b 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1d27c 65 64 20 69 6e 74 6f 20 75 2e 61 6c 20 2a 2f 0a ed into u.al */.
1d27d 20 20 69 66 28 20 70 49 6e 31 2d 3e 66 6c 61 67 if( pIn1->flag
1d27e 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a s & MEM_Null ){.
1d27f 20 20 20 20 75 2e 61 6c 2e 63 20 3d 20 70 4f 70 u.al.c = pOp
1d280 2d 3e 70 33 3b 0a 20 20 7d 65 6c 73 65 7b 0a 23 ->p3;. }else{.#
1d281 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 ifdef SQLITE_OMI
1d282 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 T_FLOATING_POINT
1d283 0a 20 20 20 20 75 2e 61 6c 2e 63 20 3d 20 73 71 . u.al.c = sq
1d284 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75 lite3VdbeIntValu
1d285 65 28 70 49 6e 31 29 21 3d 30 3b 0a 23 65 6c 73 e(pIn1)!=0;.#els
1d286 65 0a 20 20 20 20 75 2e 61 6c 2e 63 20 3d 20 73 e. u.al.c = s
1d287 71 6c 69 74 65 33 56 64 62 65 52 65 61 6c 56 61 qlite3VdbeRealVa
1d288 6c 75 65 28 70 49 6e 31 29 21 3d 30 2e 30 3b 0a lue(pIn1)!=0.0;.
1d289 23 65 6e 64 69 66 0a 20 20 20 20 69 66 28 20 70 #endif. if( p
1d28a 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 49 Op->opcode==OP_I
1d28b 66 4e 6f 74 20 29 20 75 2e 61 6c 2e 63 20 3d 20 fNot ) u.al.c =
1d28c 21 75 2e 61 6c 2e 63 3b 0a 20 20 7d 0a 20 20 69 !u.al.c;. }. i
1d28d 66 28 20 75 2e 61 6c 2e 63 20 29 7b 0a 20 20 20 f( u.al.c ){.
1d28e 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 2d 31 3b pc = pOp->p2-1;
1d28f 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a . }. break;.}.
1d290 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 73 4e 75 ./* Opcode: IsNu
1d291 6c 6c 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a ll P1 P2 * * *.*
1d292 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f 20 50 32 20 *.** Jump to P2
1d293 69 66 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 if the value in
1d294 72 65 67 69 73 74 65 72 20 50 31 20 69 73 20 4e register P1 is N
1d295 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f ULL..*/.case OP_
1d296 49 73 4e 75 6c 6c 3a 20 7b 20 20 20 20 20 20 20 IsNull: {
1d297 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 /* same as
1d298 54 4b 5f 49 53 4e 55 4c 4c 2c 20 6a 75 6d 70 2c TK_ISNULL, jump,
1d299 20 69 6e 31 20 2a 2f 0a 20 20 69 66 28 20 28 70 in1 */. if( (p
1d29a 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d In1->flags & MEM
1d29b 5f 4e 75 6c 6c 29 21 3d 30 20 29 7b 0a 20 20 20 _Null)!=0 ){.
1d29c 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 pc = pOp->p2 -
1d29d 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 1;. }. break;.
1d29e 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 6f }../* Opcode: No
1d29f 74 4e 75 6c 6c 20 50 31 20 50 32 20 2a 20 2a 20 tNull P1 P2 * *
1d2a0 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f 20 *.**.** Jump to
1d2a1 50 32 20 69 66 20 74 68 65 20 76 61 6c 75 65 20 P2 if the value
1d2a2 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 69 in register P1 i
1d2a3 73 20 6e 6f 74 20 4e 55 4c 4c 2e 20 20 0a 2a 2f s not NULL. .*/
1d2a4 0a 63 61 73 65 20 4f 50 5f 4e 6f 74 4e 75 6c 6c .case OP_NotNull
1d2a5 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f : { /
1d2a6 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4e 4f 54 * same as TK_NOT
1d2a7 4e 55 4c 4c 2c 20 6a 75 6d 70 2c 20 69 6e 31 20 NULL, jump, in1
1d2a8 2a 2f 0a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e */. if( (pIn1->
1d2a9 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c flags & MEM_Null
1d2aa 29 3d 3d 30 20 29 7b 0a 20 20 20 20 70 63 20 3d )==0 ){. pc =
1d2ab 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 pOp->p2 - 1;.
1d2ac 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a }. break;.}../*
1d2ad 20 4f 70 63 6f 64 65 3a 20 43 6f 6c 75 6d 6e 20 Opcode: Column
1d2ae 50 31 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a P1 P2 P3 P4 P5.*
1d2af 2a 0a 2a 2a 20 49 6e 74 65 72 70 72 65 74 20 74 *.** Interpret t
1d2b0 68 65 20 64 61 74 61 20 74 68 61 74 20 63 75 72 he data that cur
1d2b1 73 6f 72 20 50 31 20 70 6f 69 6e 74 73 20 74 6f sor P1 points to
1d2b2 20 61 73 20 61 20 73 74 72 75 63 74 75 72 65 20 as a structure
1d2b3 62 75 69 6c 74 20 75 73 69 6e 67 0a 2a 2a 20 74 built using.** t
1d2b4 68 65 20 4d 61 6b 65 52 65 63 6f 72 64 20 69 6e he MakeRecord in
1d2b5 73 74 72 75 63 74 69 6f 6e 2e 20 20 28 53 65 65 struction. (See
1d2b6 20 74 68 65 20 4d 61 6b 65 52 65 63 6f 72 64 20 the MakeRecord
1d2b7 6f 70 63 6f 64 65 20 66 6f 72 20 61 64 64 69 74 opcode for addit
1d2b8 69 6f 6e 61 6c 0a 2a 2a 20 69 6e 66 6f 72 6d 61 ional.** informa
1d2b9 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 66 tion about the f
1d2ba 6f 72 6d 61 74 20 6f 66 20 74 68 65 20 64 61 74 ormat of the dat
1d2bb 61 2e 29 20 20 45 78 74 72 61 63 74 20 74 68 65 a.) Extract the
1d2bc 20 50 32 2d 74 68 20 63 6f 6c 75 6d 6e 0a 2a 2a P2-th column.**
1d2bd 20 66 72 6f 6d 20 74 68 69 73 20 72 65 63 6f 72 from this recor
1d2be 64 2e 20 20 49 66 20 74 68 65 72 65 20 61 72 65 d. If there are
1d2bf 20 6c 65 73 73 20 74 68 61 74 20 28 50 32 2b 31 less that (P2+1
1d2c0 29 20 0a 2a 2a 20 76 61 6c 75 65 73 20 69 6e 20 ) .** values in
1d2c1 74 68 65 20 72 65 63 6f 72 64 2c 20 65 78 74 72 the record, extr
1d2c2 61 63 74 20 61 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a act a NULL..**.*
1d2c3 2a 20 54 68 65 20 76 61 6c 75 65 20 65 78 74 72 * The value extr
1d2c4 61 63 74 65 64 20 69 73 20 73 74 6f 72 65 64 20 acted is stored
1d2c5 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a in register P3..
1d2c6 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 63 6f 6c **.** If the col
1d2c7 75 6d 6e 20 63 6f 6e 74 61 69 6e 73 20 66 65 77 umn contains few
1d2c8 65 72 20 74 68 61 6e 20 50 32 20 66 69 65 6c 64 er than P2 field
1d2c9 73 2c 20 74 68 65 6e 20 65 78 74 72 61 63 74 20 s, then extract
1d2ca 61 20 4e 55 4c 4c 2e 20 20 4f 72 2c 0a 2a 2a 20 a NULL. Or,.**
1d2cb 69 66 20 74 68 65 20 50 34 20 61 72 67 75 6d 65 if the P4 argume
1d2cc 6e 74 20 69 73 20 61 20 50 34 5f 4d 45 4d 20 75 nt is a P4_MEM u
1d2cd 73 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 se the value of
1d2ce 74 68 65 20 50 34 20 61 72 67 75 6d 65 6e 74 20 the P4 argument
1d2cf 61 73 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 as.** the result
1d2d0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4f ..**.** If the O
1d2d1 50 46 4c 41 47 5f 43 4c 45 41 52 43 41 43 48 45 PFLAG_CLEARCACHE
1d2d2 20 62 69 74 20 69 73 20 73 65 74 20 6f 6e 20 50 bit is set on P
1d2d3 35 20 61 6e 64 20 50 31 20 69 73 20 61 20 70 73 5 and P1 is a ps
1d2d4 65 75 64 6f 2d 74 61 62 6c 65 20 63 75 72 73 6f eudo-table curso
1d2d5 72 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 63 r,.** then the c
1d2d6 61 63 68 65 20 6f 66 20 74 68 65 20 63 75 72 73 ache of the curs
1d2d7 6f 72 20 69 73 20 72 65 73 65 74 20 70 72 69 6f or is reset prio
1d2d8 72 20 74 6f 20 65 78 74 72 61 63 74 69 6e 67 20 r to extracting
1d2d9 74 68 65 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 20 54 the column..** T
1d2da 68 65 20 66 69 72 73 74 20 4f 50 5f 43 6f 6c 75 he first OP_Colu
1d2db 6d 6e 20 61 67 61 69 6e 73 74 20 61 20 70 73 65 mn against a pse
1d2dc 75 64 6f 2d 74 61 62 6c 65 20 61 66 74 65 72 20 udo-table after
1d2dd 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 the value of the
1d2de 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20 72 65 67 69 content.** regi
1d2df 73 74 65 72 20 68 61 73 20 63 68 61 6e 67 65 64 ster has changed
1d2e0 20 73 68 6f 75 6c 64 20 68 61 76 65 20 74 68 69 should have thi
1d2e1 73 20 62 69 74 20 73 65 74 2e 0a 2a 2f 0a 63 61 s bit set..*/.ca
1d2e2 73 65 20 4f 50 5f 43 6f 6c 75 6d 6e 3a 20 7b 0a se OP_Column: {.
1d2e3 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 #if 0 /* local
1d2e4 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 variables moved
1d2e5 69 6e 74 6f 20 75 2e 61 6d 20 2a 2f 0a 20 20 75 into u.am */. u
1d2e6 33 32 20 70 61 79 6c 6f 61 64 53 69 7a 65 3b 20 32 payloadSize;
1d2e7 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 /* Number of b
1d2e8 79 74 65 73 20 69 6e 20 74 68 65 20 72 65 63 6f ytes in the reco
1d2e9 72 64 20 2a 2f 0a 20 20 69 36 34 20 70 61 79 6c rd */. i64 payl
1d2ea 6f 61 64 53 69 7a 65 36 34 3b 20 2f 2a 20 4e 75 oadSize64; /* Nu
1d2eb 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e mber of bytes in
1d2ec 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 the record */.
1d2ed 20 69 6e 74 20 70 31 3b 20 20 20 20 20 20 20 20 int p1;
1d2ee 20 20 20 20 2f 2a 20 50 31 20 76 61 6c 75 65 20 /* P1 value
1d2ef 6f 66 20 74 68 65 20 6f 70 63 6f 64 65 20 2a 2f of the opcode */
1d2f0 0a 20 20 69 6e 74 20 70 32 3b 20 20 20 20 20 20 . int p2;
1d2f1 20 20 20 20 20 20 2f 2a 20 63 6f 6c 75 6d 6e 20 /* column
1d2f2 6e 75 6d 62 65 72 20 74 6f 20 72 65 74 72 69 65 number to retrie
1d2f3 76 65 20 2a 2f 0a 20 20 56 64 62 65 43 75 72 73 ve */. VdbeCurs
1d2f4 6f 72 20 2a 70 43 3b 20 20 20 20 2f 2a 20 54 68 or *pC; /* Th
1d2f5 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 2a 2f e VDBE cursor */
1d2f6 0a 20 20 63 68 61 72 20 2a 7a 52 65 63 3b 20 20 . char *zRec;
1d2f7 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 /* Pointer
1d2f8 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20 72 65 63 to complete rec
1d2f9 6f 72 64 2d 64 61 74 61 20 2a 2f 0a 20 20 42 74 ord-data */. Bt
1d2fa 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 20 20 Cursor *pCrsr;
1d2fb 20 2f 2a 20 54 68 65 20 42 54 72 65 65 20 63 75 /* The BTree cu
1d2fc 72 73 6f 72 20 2a 2f 0a 20 20 75 33 32 20 2a 61 rsor */. u32 *a
1d2fd 54 79 70 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 Type; /*
1d2fe 61 54 79 70 65 5b 69 5d 20 68 6f 6c 64 73 20 74 aType[i] holds t
1d2ff 68 65 20 6e 75 6d 65 72 69 63 20 74 79 70 65 20 he numeric type
1d300 6f 66 20 74 68 65 20 69 2d 74 68 20 63 6f 6c 75 of the i-th colu
1d301 6d 6e 20 2a 2f 0a 20 20 75 33 32 20 2a 61 4f 66 mn */. u32 *aOf
1d302 66 73 65 74 3b 20 20 20 20 20 20 2f 2a 20 61 4f fset; /* aO
1d303 66 66 73 65 74 5b 69 5d 20 69 73 20 6f 66 66 73 ffset[i] is offs
1d304 65 74 20 74 6f 20 73 74 61 72 74 20 6f 66 20 64 et to start of d
1d305 61 74 61 20 66 6f 72 20 69 2d 74 68 20 63 6f 6c ata for i-th col
1d306 75 6d 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 69 umn */. int nFi
1d307 65 6c 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 6e eld; /* n
1d308 75 6d 62 65 72 20 6f 66 20 66 69 65 6c 64 73 20 umber of fields
1d309 69 6e 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f in the record */
1d30a 0a 20 20 69 6e 74 20 6c 65 6e 3b 20 20 20 20 20 . int len;
1d30b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 65 6e /* The len
1d30c 67 74 68 20 6f 66 20 74 68 65 20 73 65 72 69 61 gth of the seria
1d30d 6c 69 7a 65 64 20 64 61 74 61 20 66 6f 72 20 74 lized data for t
1d30e 68 65 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 69 he column */. i
1d30f 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 nt i;
1d310 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 /* Loop counte
1d311 72 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 44 61 r */. char *zDa
1d312 74 61 3b 20 20 20 20 20 20 20 2f 2a 20 50 61 72 ta; /* Par
1d313 74 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64 20 t of the record
1d314 62 65 69 6e 67 20 64 65 63 6f 64 65 64 20 2a 2f being decoded */
1d315 0a 20 20 4d 65 6d 20 2a 70 44 65 73 74 3b 20 20 . Mem *pDest;
1d316 20 20 20 20 20 20 2f 2a 20 57 68 65 72 65 20 74 /* Where t
1d317 6f 20 77 72 69 74 65 20 74 68 65 20 65 78 74 72 o write the extr
1d318 61 63 74 65 64 20 76 61 6c 75 65 20 2a 2f 0a 20 acted value */.
1d319 20 4d 65 6d 20 73 4d 65 6d 3b 20 20 20 20 20 20 Mem sMem;
1d31a 20 20 20 20 2f 2a 20 46 6f 72 20 73 74 6f 72 69 /* For stori
1d31b 6e 67 20 74 68 65 20 72 65 63 6f 72 64 20 62 65 ng the record be
1d31c 69 6e 67 20 64 65 63 6f 64 65 64 20 2a 2f 0a 20 ing decoded */.
1d31d 20 75 38 20 2a 7a 49 64 78 3b 20 20 20 20 20 20 u8 *zIdx;
1d31e 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 74 /* Index int
1d31f 6f 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 75 38 o header */. u8
1d320 20 2a 7a 45 6e 64 48 64 72 3b 20 20 20 20 20 20 *zEndHdr;
1d321 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 66 /* Pointer to f
1d322 69 72 73 74 20 62 79 74 65 20 61 66 74 65 72 20 irst byte after
1d323 74 68 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 the header */.
1d324 75 33 32 20 6f 66 66 73 65 74 3b 20 20 20 20 20 u32 offset;
1d325 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74 /* Offset int
1d326 6f 20 74 68 65 20 64 61 74 61 20 2a 2f 0a 20 20 o the data */.
1d327 75 36 34 20 6f 66 66 73 65 74 36 34 3b 20 20 20 u64 offset64;
1d328 20 20 20 2f 2a 20 36 34 2d 62 69 74 20 6f 66 66 /* 64-bit off
1d329 73 65 74 2e 20 20 36 34 20 62 69 74 73 20 6e 65 set. 64 bits ne
1d32a 65 64 65 64 20 74 6f 20 63 61 74 63 68 20 6f 76 eded to catch ov
1d32b 65 72 66 6c 6f 77 20 2a 2f 0a 20 20 69 6e 74 20 erflow */. int
1d32c 73 7a 48 64 72 3b 20 20 20 20 20 20 20 20 20 2f szHdr; /
1d32d 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20 68 65 * Size of the he
1d32e 61 64 65 72 20 73 69 7a 65 20 66 69 65 6c 64 20 ader size field
1d32f 61 74 20 73 74 61 72 74 20 6f 66 20 72 65 63 6f at start of reco
1d330 72 64 20 2a 2f 0a 20 20 69 6e 74 20 61 76 61 69 rd */. int avai
1d331 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 l; /* Nu
1d332 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 mber of bytes of
1d333 20 61 76 61 69 6c 61 62 6c 65 20 64 61 74 61 20 available data
1d334 2a 2f 0a 20 20 4d 65 6d 20 2a 70 52 65 67 3b 20 */. Mem *pReg;
1d335 20 20 20 20 20 20 20 20 2f 2a 20 50 73 65 75 64 /* Pseud
1d336 6f 54 61 62 6c 65 20 69 6e 70 75 74 20 72 65 67 oTable input reg
1d337 69 73 74 65 72 20 2a 2f 0a 23 65 6e 64 69 66 20 ister */.#endif
1d338 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c /* local variabl
1d339 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e es moved into u.
1d33a 61 6d 20 2a 2f 0a 0a 0a 20 20 75 2e 61 6d 2e 70 am */... u.am.p
1d33b 31 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 75 1 = pOp->p1;. u
1d33c 2e 61 6d 2e 70 32 20 3d 20 70 4f 70 2d 3e 70 32 .am.p2 = pOp->p2
1d33d 3b 0a 20 20 75 2e 61 6d 2e 70 43 20 3d 20 30 3b ;. u.am.pC = 0;
1d33e 0a 20 20 6d 65 6d 73 65 74 28 26 75 2e 61 6d 2e . memset(&u.am.
1d33f 73 4d 65 6d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 sMem, 0, sizeof(
1d340 75 2e 61 6d 2e 73 4d 65 6d 29 29 3b 0a 20 20 61 u.am.sMem));. a
1d341 73 73 65 72 74 28 20 75 2e 61 6d 2e 70 31 3c 70 ssert( u.am.p1<p
1d342 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 61 ->nCursor );. a
1d343 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30 ssert( pOp->p3>0
1d344 20 26 26 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e && pOp->p3<=p->
1d345 6e 4d 65 6d 20 29 3b 0a 20 20 75 2e 61 6d 2e 70 nMem );. u.am.p
1d346 44 65 73 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b Dest = &p->aMem[
1d347 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 4d 65 6d 53 pOp->p3];. MemS
1d348 65 74 54 79 70 65 46 6c 61 67 28 75 2e 61 6d 2e etTypeFlag(u.am.
1d349 70 44 65 73 74 2c 20 4d 45 4d 5f 4e 75 6c 6c 29 pDest, MEM_Null)
1d34a 3b 0a 20 20 75 2e 61 6d 2e 7a 52 65 63 20 3d 20 ;. u.am.zRec =
1d34b 30 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 62 6c 0;.. /* This bl
1d34c 6f 63 6b 20 73 65 74 73 20 74 68 65 20 76 61 72 ock sets the var
1d34d 69 61 62 6c 65 20 75 2e 61 6d 2e 70 61 79 6c 6f iable u.am.paylo
1d34e 61 64 53 69 7a 65 20 74 6f 20 62 65 20 74 68 65 adSize to be the
1d34f 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 total number of
1d350 0a 20 20 2a 2a 20 62 79 74 65 73 20 69 6e 20 74 . ** bytes in t
1d351 68 65 20 72 65 63 6f 72 64 2e 0a 20 20 2a 2a 0a he record.. **.
1d352 20 20 2a 2a 20 75 2e 61 6d 2e 7a 52 65 63 20 69 ** u.am.zRec i
1d353 73 20 73 65 74 20 74 6f 20 62 65 20 74 68 65 20 s set to be the
1d354 63 6f 6d 70 6c 65 74 65 20 74 65 78 74 20 6f 66 complete text of
1d355 20 74 68 65 20 72 65 63 6f 72 64 20 69 66 20 69 the record if i
1d356 74 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2e 0a t is available..
1d357 20 20 2a 2a 20 54 68 65 20 63 6f 6d 70 6c 65 74 ** The complet
1d358 65 20 72 65 63 6f 72 64 20 74 65 78 74 20 69 73 e record text is
1d359 20 61 6c 77 61 79 73 20 61 76 61 69 6c 61 62 6c always availabl
1d35a 65 20 66 6f 72 20 70 73 65 75 64 6f 2d 74 61 62 e for pseudo-tab
1d35b 6c 65 73 0a 20 20 2a 2a 20 49 66 20 74 68 65 20 les. ** If the
1d35c 72 65 63 6f 72 64 20 69 73 20 73 74 6f 72 65 64 record is stored
1d35d 20 69 6e 20 61 20 63 75 72 73 6f 72 2c 20 74 68 in a cursor, th
1d35e 65 20 63 6f 6d 70 6c 65 74 65 20 72 65 63 6f 72 e complete recor
1d35f 64 20 74 65 78 74 0a 20 20 2a 2a 20 6d 69 67 68 d text. ** migh
1d360 74 20 62 65 20 61 76 61 69 6c 61 62 6c 65 20 69 t be available i
1d361 6e 20 74 68 65 20 20 75 2e 61 6d 2e 70 43 2d 3e n the u.am.pC->
1d362 61 52 6f 77 20 63 61 63 68 65 2e 20 20 4f 72 20 aRow cache. Or
1d363 69 74 20 6d 69 67 68 74 20 6e 6f 74 20 62 65 2e it might not be.
1d364 0a 20 20 2a 2a 20 49 66 20 74 68 65 20 64 61 74 . ** If the dat
1d365 61 20 69 73 20 75 6e 61 76 61 69 6c 61 62 6c 65 a is unavailable
1d366 2c 20 20 75 2e 61 6d 2e 7a 52 65 63 20 69 73 20 , u.am.zRec is
1d367 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a 20 20 2a set to NULL.. *
1d368 2a 0a 20 20 2a 2a 20 57 65 20 61 6c 73 6f 20 63 *. ** We also c
1d369 6f 6d 70 75 74 65 20 74 68 65 20 6e 75 6d 62 65 ompute the numbe
1d36a 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 r of columns in
1d36b 74 68 65 20 72 65 63 6f 72 64 2e 20 20 46 6f 72 the record. For
1d36c 20 63 75 72 73 6f 72 73 2c 0a 20 20 2a 2a 20 74 cursors,. ** t
1d36d 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c he number of col
1d36e 75 6d 6e 73 20 69 73 20 73 74 6f 72 65 64 20 69 umns is stored i
1d36f 6e 20 74 68 65 20 56 64 62 65 43 75 72 73 6f 72 n the VdbeCursor
1d370 2e 6e 46 69 65 6c 64 20 65 6c 65 6d 65 6e 74 2e .nField element.
1d371 0a 20 20 2a 2f 0a 20 20 75 2e 61 6d 2e 70 43 20 . */. u.am.pC
1d372 3d 20 70 2d 3e 61 70 43 73 72 5b 75 2e 61 6d 2e = p->apCsr[u.am.
1d373 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75 p1];. assert( u
1d374 2e 61 6d 2e 70 43 21 3d 30 20 29 3b 0a 23 69 66 .am.pC!=0 );.#if
1d375 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1d376 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 _VIRTUALTABLE.
1d377 61 73 73 65 72 74 28 20 75 2e 61 6d 2e 70 43 2d assert( u.am.pC-
1d378 3e 70 56 74 61 62 43 75 72 73 6f 72 3d 3d 30 20 >pVtabCursor==0
1d379 29 3b 0a 23 65 6e 64 69 66 0a 20 20 75 2e 61 6d );.#endif. u.am
1d37a 2e 70 43 72 73 72 20 3d 20 75 2e 61 6d 2e 70 43 .pCrsr = u.am.pC
1d37b 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 69 66 28 ->pCursor;. if(
1d37c 20 75 2e 61 6d 2e 70 43 72 73 72 21 3d 30 20 29 u.am.pCrsr!=0 )
1d37d 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 72 65 63 {. /* The rec
1d37e 6f 72 64 20 69 73 20 73 74 6f 72 65 64 20 69 6e ord is stored in
1d37f 20 61 20 42 2d 54 72 65 65 20 2a 2f 0a 20 20 20 a B-Tree */.
1d380 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 rc = sqlite3Vdb
1d381 65 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28 75 2e eCursorMoveto(u.
1d382 61 6d 2e 70 43 29 3b 0a 20 20 20 20 69 66 28 20 am.pC);. if(
1d383 72 63 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f rc ) goto abort_
1d384 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 due_to_error;.
1d385 20 20 69 66 28 20 75 2e 61 6d 2e 70 43 2d 3e 6e if( u.am.pC->n
1d386 75 6c 6c 52 6f 77 20 29 7b 0a 20 20 20 20 20 20 ullRow ){.
1d387 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 u.am.payloadSize
1d388 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20 = 0;. }else
1d389 69 66 28 20 75 2e 61 6d 2e 70 43 2d 3e 63 61 63 if( u.am.pC->cac
1d38a 68 65 53 74 61 74 75 73 3d 3d 70 2d 3e 63 61 63 heStatus==p->cac
1d38b 68 65 43 74 72 20 29 7b 0a 20 20 20 20 20 20 75 heCtr ){. u
1d38c 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 20 .am.payloadSize
1d38d 3d 20 75 2e 61 6d 2e 70 43 2d 3e 70 61 79 6c 6f = u.am.pC->paylo
1d38e 61 64 53 69 7a 65 3b 0a 20 20 20 20 20 20 75 2e adSize;. u.
1d38f 61 6d 2e 7a 52 65 63 20 3d 20 28 63 68 61 72 2a am.zRec = (char*
1d390 29 75 2e 61 6d 2e 70 43 2d 3e 61 52 6f 77 3b 0a )u.am.pC->aRow;.
1d391 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 75 2e }else if( u.
1d392 61 6d 2e 70 43 2d 3e 69 73 49 6e 64 65 78 20 29 am.pC->isIndex )
1d393 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 {. assert(
1d394 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 sqlite3BtreeCurs
1d395 6f 72 49 73 56 61 6c 69 64 28 75 2e 61 6d 2e 70 orIsValid(u.am.p
1d396 43 72 73 72 29 20 29 3b 0a 20 20 20 20 20 20 72 Crsr) );. r
1d397 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 c = sqlite3Btree
1d398 4b 65 79 53 69 7a 65 28 75 2e 61 6d 2e 70 43 72 KeySize(u.am.pCr
1d399 73 72 2c 20 26 75 2e 61 6d 2e 70 61 79 6c 6f 61 sr, &u.am.payloa
1d39a 64 53 69 7a 65 36 34 29 3b 0a 20 20 20 20 20 20 dSize64);.
1d39b 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 assert( rc==SQLI
1d39c 54 45 5f 4f 4b 20 29 3b 20 20 20 2f 2a 20 54 72 TE_OK ); /* Tr
1d39d 75 65 20 62 65 63 61 75 73 65 20 6f 66 20 43 75 ue because of Cu
1d39e 72 73 6f 72 4d 6f 76 65 74 6f 28 29 20 63 61 6c rsorMoveto() cal
1d39f 6c 20 61 62 6f 76 65 20 2a 2f 0a 20 20 20 20 20 l above */.
1d3a0 20 2f 2a 20 73 71 6c 69 74 65 33 42 74 72 65 65 /* sqlite3Btree
1d3a1 50 61 72 73 65 43 65 6c 6c 50 74 72 28 29 20 75 ParseCellPtr() u
1d3a2 73 65 73 20 67 65 74 56 61 72 69 6e 74 33 32 28 ses getVarint32(
1d3a3 29 20 74 6f 20 65 78 74 72 61 63 74 20 74 68 65 ) to extract the
1d3a4 0a 20 20 20 20 20 20 2a 2a 20 70 61 79 6c 6f 61 . ** payloa
1d3a5 64 20 73 69 7a 65 2c 20 73 6f 20 69 74 20 69 73 d size, so it is
1d3a6 20 69 6d 70 6f 73 73 69 62 6c 65 20 66 6f 72 20 impossible for
1d3a7 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 u.am.payloadSize
1d3a8 36 34 20 74 6f 20 62 65 0a 20 20 20 20 20 20 2a 64 to be. *
1d3a9 2a 20 6c 61 72 67 65 72 20 74 68 61 6e 20 33 32 * larger than 32
1d3aa 20 62 69 74 73 2e 20 2a 2f 0a 20 20 20 20 20 20 bits. */.
1d3ab 61 73 73 65 72 74 28 20 28 75 2e 61 6d 2e 70 61 assert( (u.am.pa
1d3ac 79 6c 6f 61 64 53 69 7a 65 36 34 20 26 20 53 51 yloadSize64 & SQ
1d3ad 4c 49 54 45 5f 4d 41 58 5f 55 33 32 29 3d 3d 28 LITE_MAX_U32)==(
1d3ae 75 36 34 29 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 u64)u.am.payload
1d3af 53 69 7a 65 36 34 20 29 3b 0a 20 20 20 20 20 20 Size64 );.
1d3b0 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 u.am.payloadSize
1d3b1 20 3d 20 28 75 33 32 29 75 2e 61 6d 2e 70 61 79 = (u32)u.am.pay
1d3b2 6c 6f 61 64 53 69 7a 65 36 34 3b 0a 20 20 20 20 loadSize64;.
1d3b3 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 }else{. ass
1d3b4 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 ert( sqlite3Btre
1d3b5 65 43 75 72 73 6f 72 49 73 56 61 6c 69 64 28 75 eCursorIsValid(u
1d3b6 2e 61 6d 2e 70 43 72 73 72 29 20 29 3b 0a 20 20 .am.pCrsr) );.
1d3b7 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1d3b8 42 74 72 65 65 44 61 74 61 53 69 7a 65 28 75 2e BtreeDataSize(u.
1d3b9 61 6d 2e 70 43 72 73 72 2c 20 26 75 2e 61 6d 2e am.pCrsr, &u.am.
1d3ba 70 61 79 6c 6f 61 64 53 69 7a 65 29 3b 0a 20 20 payloadSize);.
1d3bb 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d assert( rc==
1d3bc 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 20 20 20 2f SQLITE_OK ); /
1d3bd 2a 20 44 61 74 61 53 69 7a 65 28 29 20 63 61 6e * DataSize() can
1d3be 6e 6f 74 20 66 61 69 6c 20 2a 2f 0a 20 20 20 20 not fail */.
1d3bf 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 75 2e }. }else if( u.
1d3c0 61 6d 2e 70 43 2d 3e 70 73 65 75 64 6f 54 61 62 am.pC->pseudoTab
1d3c1 6c 65 52 65 67 3e 30 20 29 7b 0a 20 20 20 20 75 leReg>0 ){. u
1d3c2 2e 61 6d 2e 70 52 65 67 20 3d 20 26 70 2d 3e 61 .am.pReg = &p->a
1d3c3 4d 65 6d 5b 75 2e 61 6d 2e 70 43 2d 3e 70 73 65 Mem[u.am.pC->pse
1d3c4 75 64 6f 54 61 62 6c 65 52 65 67 5d 3b 0a 20 20 udoTableReg];.
1d3c5 20 20 61 73 73 65 72 74 28 20 75 2e 61 6d 2e 70 assert( u.am.p
1d3c6 52 65 67 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d Reg->flags & MEM
1d3c7 5f 42 6c 6f 62 20 29 3b 0a 20 20 20 20 75 2e 61 _Blob );. u.a
1d3c8 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 20 3d 20 m.payloadSize =
1d3c9 75 2e 61 6d 2e 70 52 65 67 2d 3e 6e 3b 0a 20 20 u.am.pReg->n;.
1d3ca 20 20 75 2e 61 6d 2e 7a 52 65 63 20 3d 20 75 2e u.am.zRec = u.
1d3cb 61 6d 2e 70 52 65 67 2d 3e 7a 3b 0a 20 20 20 20 am.pReg->z;.
1d3cc 75 2e 61 6d 2e 70 43 2d 3e 63 61 63 68 65 53 74 u.am.pC->cacheSt
1d3cd 61 74 75 73 20 3d 20 28 70 4f 70 2d 3e 70 35 26 atus = (pOp->p5&
1d3ce 4f 50 46 4c 41 47 5f 43 4c 45 41 52 43 41 43 48 OPFLAG_CLEARCACH
1d3cf 45 29 20 3f 20 43 41 43 48 45 5f 53 54 41 4c 45 E) ? CACHE_STALE
1d3d0 20 3a 20 70 2d 3e 63 61 63 68 65 43 74 72 3b 0a : p->cacheCtr;.
1d3d1 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 61 6d assert( u.am
1d3d2 2e 70 61 79 6c 6f 61 64 53 69 7a 65 3d 3d 30 20 .payloadSize==0
1d3d3 7c 7c 20 75 2e 61 6d 2e 7a 52 65 63 21 3d 30 20 || u.am.zRec!=0
1d3d4 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
1d3d5 2f 2a 20 43 6f 6e 73 69 64 65 72 20 74 68 65 20 /* Consider the
1d3d6 72 6f 77 20 74 6f 20 62 65 20 4e 55 4c 4c 20 2a row to be NULL *
1d3d7 2f 0a 20 20 20 20 75 2e 61 6d 2e 70 61 79 6c 6f /. u.am.paylo
1d3d8 61 64 53 69 7a 65 20 3d 20 30 3b 0a 20 20 7d 0a adSize = 0;. }.
1d3d9 0a 20 20 2f 2a 20 49 66 20 75 2e 61 6d 2e 70 61 . /* If u.am.pa
1d3da 79 6c 6f 61 64 53 69 7a 65 20 69 73 20 30 2c 20 yloadSize is 0,
1d3db 74 68 65 6e 20 6a 75 73 74 20 73 74 6f 72 65 20 then just store
1d3dc 61 20 4e 55 4c 4c 20 2a 2f 0a 20 20 69 66 28 20 a NULL */. if(
1d3dd 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 u.am.payloadSize
1d3de 3d 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 ==0 ){. asser
1d3df 74 28 20 75 2e 61 6d 2e 70 44 65 73 74 2d 3e 66 t( u.am.pDest->f
1d3e0 6c 61 67 73 26 4d 45 4d 5f 4e 75 6c 6c 20 29 3b lags&MEM_Null );
1d3e1 0a 20 20 20 20 67 6f 74 6f 20 6f 70 5f 63 6f 6c . goto op_col
1d3e2 75 6d 6e 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 61 umn_out;. }. a
1d3e3 73 73 65 72 74 28 20 64 62 2d 3e 61 4c 69 6d 69 ssert( db->aLimi
1d3e4 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c t[SQLITE_LIMIT_L
1d3e5 45 4e 47 54 48 5d 3e 3d 30 20 29 3b 0a 20 20 69 ENGTH]>=0 );. i
1d3e6 66 28 20 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 f( u.am.payloadS
1d3e7 69 7a 65 20 3e 20 28 75 33 32 29 64 62 2d 3e 61 ize > (u32)db->a
1d3e8 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d Limit[SQLITE_LIM
1d3e9 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 IT_LENGTH] ){.
1d3ea 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a goto too_big;.
1d3eb 20 20 7d 0a 0a 20 20 75 2e 61 6d 2e 6e 46 69 65 }.. u.am.nFie
1d3ec 6c 64 20 3d 20 75 2e 61 6d 2e 70 43 2d 3e 6e 46 ld = u.am.pC->nF
1d3ed 69 65 6c 64 3b 0a 20 20 61 73 73 65 72 74 28 20 ield;. assert(
1d3ee 75 2e 61 6d 2e 70 32 3c 75 2e 61 6d 2e 6e 46 69 u.am.p2<u.am.nFi
1d3ef 65 6c 64 20 29 3b 0a 0a 20 20 2f 2a 20 52 65 61 eld );.. /* Rea
1d3f0 64 20 61 6e 64 20 70 61 72 73 65 20 74 68 65 20 d and parse the
1d3f1 74 61 62 6c 65 20 68 65 61 64 65 72 2e 20 20 53 table header. S
1d3f2 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 73 tore the results
1d3f3 20 6f 66 20 74 68 65 20 70 61 72 73 65 0a 20 20 of the parse.
1d3f4 2a 2a 20 69 6e 74 6f 20 74 68 65 20 72 65 63 6f ** into the reco
1d3f5 72 64 20 68 65 61 64 65 72 20 63 61 63 68 65 20 rd header cache
1d3f6 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20 63 75 fields of the cu
1d3f7 72 73 6f 72 2e 0a 20 20 2a 2f 0a 20 20 75 2e 61 rsor.. */. u.a
1d3f8 6d 2e 61 54 79 70 65 20 3d 20 75 2e 61 6d 2e 70 m.aType = u.am.p
1d3f9 43 2d 3e 61 54 79 70 65 3b 0a 20 20 69 66 28 20 C->aType;. if(
1d3fa 75 2e 61 6d 2e 70 43 2d 3e 63 61 63 68 65 53 74 u.am.pC->cacheSt
1d3fb 61 74 75 73 3d 3d 70 2d 3e 63 61 63 68 65 43 74 atus==p->cacheCt
1d3fc 72 20 29 7b 0a 20 20 20 20 75 2e 61 6d 2e 61 4f r ){. u.am.aO
1d3fd 66 66 73 65 74 20 3d 20 75 2e 61 6d 2e 70 43 2d ffset = u.am.pC-
1d3fe 3e 61 4f 66 66 73 65 74 3b 0a 20 20 7d 65 6c 73 >aOffset;. }els
1d3ff 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 75 2e e{. assert(u.
1d400 61 6d 2e 61 54 79 70 65 29 3b 0a 20 20 20 20 75 am.aType);. u
1d401 2e 61 6d 2e 61 76 61 69 6c 20 3d 20 30 3b 0a 20 .am.avail = 0;.
1d402 20 20 20 75 2e 61 6d 2e 70 43 2d 3e 61 4f 66 66 u.am.pC->aOff
1d403 73 65 74 20 3d 20 75 2e 61 6d 2e 61 4f 66 66 73 set = u.am.aOffs
1d404 65 74 20 3d 20 26 75 2e 61 6d 2e 61 54 79 70 65 et = &u.am.aType
1d405 5b 75 2e 61 6d 2e 6e 46 69 65 6c 64 5d 3b 0a 20 [u.am.nField];.
1d406 20 20 20 75 2e 61 6d 2e 70 43 2d 3e 70 61 79 6c u.am.pC->payl
1d407 6f 61 64 53 69 7a 65 20 3d 20 75 2e 61 6d 2e 70 oadSize = u.am.p
1d408 61 79 6c 6f 61 64 53 69 7a 65 3b 0a 20 20 20 20 ayloadSize;.
1d409 75 2e 61 6d 2e 70 43 2d 3e 63 61 63 68 65 53 74 u.am.pC->cacheSt
1d40a 61 74 75 73 20 3d 20 70 2d 3e 63 61 63 68 65 43 atus = p->cacheC
1d40b 74 72 3b 0a 0a 20 20 20 20 2f 2a 20 46 69 67 75 tr;.. /* Figu
1d40c 72 65 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20 re out how many
1d40d 62 79 74 65 73 20 61 72 65 20 69 6e 20 74 68 65 bytes are in the
1d40e 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 20 20 69 header */. i
1d40f 66 28 20 75 2e 61 6d 2e 7a 52 65 63 20 29 7b 0a f( u.am.zRec ){.
1d410 20 20 20 20 20 20 75 2e 61 6d 2e 7a 44 61 74 61 u.am.zData
1d411 20 3d 20 75 2e 61 6d 2e 7a 52 65 63 3b 0a 20 20 = u.am.zRec;.
1d412 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 }else{. i
1d413 66 28 20 75 2e 61 6d 2e 70 43 2d 3e 69 73 49 6e f( u.am.pC->isIn
1d414 64 65 78 20 29 7b 0a 20 20 20 20 20 20 20 20 75 dex ){. u
1d415 2e 61 6d 2e 7a 44 61 74 61 20 3d 20 28 63 68 61 .am.zData = (cha
1d416 72 2a 29 73 71 6c 69 74 65 33 42 74 72 65 65 4b r*)sqlite3BtreeK
1d417 65 79 46 65 74 63 68 28 75 2e 61 6d 2e 70 43 72 eyFetch(u.am.pCr
1d418 73 72 2c 20 26 75 2e 61 6d 2e 61 76 61 69 6c 29 sr, &u.am.avail)
1d419 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ;. }else{.
1d41a 20 20 20 20 20 20 20 75 2e 61 6d 2e 7a 44 61 74 u.am.zDat
1d41b 61 20 3d 20 28 63 68 61 72 2a 29 73 71 6c 69 74 a = (char*)sqlit
1d41c 65 33 42 74 72 65 65 44 61 74 61 46 65 74 63 68 e3BtreeDataFetch
1d41d 28 75 2e 61 6d 2e 70 43 72 73 72 2c 20 26 75 2e (u.am.pCrsr, &u.
1d41e 61 6d 2e 61 76 61 69 6c 29 3b 0a 20 20 20 20 20 am.avail);.
1d41f 20 7d 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 4b }. /* If K
1d420 65 79 46 65 74 63 68 28 29 2f 44 61 74 61 46 65 eyFetch()/DataFe
1d421 74 63 68 28 29 20 6d 61 6e 61 67 65 64 20 74 6f tch() managed to
1d422 20 67 65 74 20 74 68 65 20 65 6e 74 69 72 65 20 get the entire
1d423 70 61 79 6c 6f 61 64 2c 0a 20 20 20 20 20 20 2a payload,. *
1d424 2a 20 73 61 76 65 20 74 68 65 20 70 61 79 6c 6f * save the paylo
1d425 61 64 20 69 6e 20 74 68 65 20 75 2e 61 6d 2e 70 ad in the u.am.p
1d426 43 2d 3e 61 52 6f 77 20 63 61 63 68 65 2e 20 20 C->aRow cache.
1d427 54 68 61 74 20 77 69 6c 6c 20 73 61 76 65 20 75 That will save u
1d428 73 20 66 72 6f 6d 0a 20 20 20 20 20 20 2a 2a 20 s from. **
1d429 68 61 76 69 6e 67 20 74 6f 20 6d 61 6b 65 20 61 having to make a
1d42a 64 64 69 74 69 6f 6e 61 6c 20 63 61 6c 6c 73 20 dditional calls
1d42b 74 6f 20 66 65 74 63 68 20 74 68 65 20 63 6f 6e to fetch the con
1d42c 74 65 6e 74 20 70 6f 72 74 69 6f 6e 20 6f 66 0a tent portion of.
1d42d 20 20 20 20 20 20 2a 2a 20 74 68 65 20 72 65 63 ** the rec
1d42e 6f 72 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 ord.. */.
1d42f 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 61 6d assert( u.am
1d430 2e 61 76 61 69 6c 3e 3d 30 20 29 3b 0a 20 20 20 .avail>=0 );.
1d431 20 20 20 69 66 28 20 75 2e 61 6d 2e 70 61 79 6c if( u.am.payl
1d432 6f 61 64 53 69 7a 65 20 3c 3d 20 28 75 33 32 29 oadSize <= (u32)
1d433 75 2e 61 6d 2e 61 76 61 69 6c 20 29 7b 0a 20 20 u.am.avail ){.
1d434 20 20 20 20 20 20 75 2e 61 6d 2e 7a 52 65 63 20 u.am.zRec
1d435 3d 20 75 2e 61 6d 2e 7a 44 61 74 61 3b 0a 20 20 = u.am.zData;.
1d436 20 20 20 20 20 20 75 2e 61 6d 2e 70 43 2d 3e 61 u.am.pC->a
1d437 52 6f 77 20 3d 20 28 75 38 2a 29 75 2e 61 6d 2e Row = (u8*)u.am.
1d438 7a 44 61 74 61 3b 0a 20 20 20 20 20 20 7d 65 6c zData;. }el
1d439 73 65 7b 0a 20 20 20 20 20 20 20 20 75 2e 61 6d se{. u.am
1d43a 2e 70 43 2d 3e 61 52 6f 77 20 3d 20 30 3b 0a 20 .pC->aRow = 0;.
1d43b 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 }. }.
1d43c 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e /* The followin
1d43d 67 20 61 73 73 65 72 74 20 69 73 20 74 72 75 65 g assert is true
1d43e 20 69 6e 20 61 6c 6c 20 63 61 73 65 73 20 61 63 in all cases ac
1d43f 63 65 70 74 20 77 68 65 6e 0a 20 20 20 20 2a 2a cept when. **
1d440 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
1d441 6c 65 20 68 61 73 20 62 65 65 6e 20 63 6f 72 72 le has been corr
1d442 75 70 74 65 64 20 65 78 74 65 72 6e 61 6c 6c 79 upted externally
1d443 2e 0a 20 20 20 20 2a 2a 20 20 20 20 61 73 73 65 .. ** asse
1d444 72 74 28 20 75 2e 61 6d 2e 7a 52 65 63 21 3d 30 rt( u.am.zRec!=0
1d445 20 7c 7c 20 75 2e 61 6d 2e 61 76 61 69 6c 3e 3d || u.am.avail>=
1d446 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 u.am.payloadSize
1d447 20 7c 7c 20 75 2e 61 6d 2e 61 76 61 69 6c 3e 3d || u.am.avail>=
1d448 39 20 29 3b 20 2a 2f 0a 20 20 20 20 75 2e 61 6d 9 ); */. u.am
1d449 2e 73 7a 48 64 72 20 3d 20 67 65 74 56 61 72 69 .szHdr = getVari
1d44a 6e 74 33 32 28 28 75 38 2a 29 75 2e 61 6d 2e 7a nt32((u8*)u.am.z
1d44b 44 61 74 61 2c 20 75 2e 61 6d 2e 6f 66 66 73 65 Data, u.am.offse
1d44c 74 29 3b 0a 0a 20 20 20 20 2f 2a 20 4d 61 6b 65 t);.. /* Make
1d44d 20 73 75 72 65 20 61 20 63 6f 72 72 75 70 74 20 sure a corrupt
1d44e 64 61 74 61 62 61 73 65 20 68 61 73 20 6e 6f 74 database has not
1d44f 20 67 69 76 65 6e 20 75 73 20 61 6e 20 6f 76 65 given us an ove
1d450 72 73 69 7a 65 20 68 65 61 64 65 72 2e 0a 20 20 rsize header..
1d451 20 20 2a 2a 20 44 6f 20 74 68 69 73 20 6e 6f 77 ** Do this now
1d452 20 74 6f 20 61 76 6f 69 64 20 61 6e 20 6f 76 65 to avoid an ove
1d453 72 73 69 7a 65 20 6d 65 6d 6f 72 79 20 61 6c 6c rsize memory all
1d454 6f 63 61 74 69 6f 6e 2e 0a 20 20 20 20 2a 2a 0a ocation.. **.
1d455 20 20 20 20 2a 2a 20 54 79 70 65 20 65 6e 74 72 ** Type entr
1d456 69 65 73 20 63 61 6e 20 62 65 20 62 65 74 77 65 ies can be betwe
1d457 65 6e 20 31 20 61 6e 64 20 35 20 62 79 74 65 73 en 1 and 5 bytes
1d458 20 65 61 63 68 2e 20 20 42 75 74 20 34 20 61 6e each. But 4 an
1d459 64 20 35 20 62 79 74 65 0a 20 20 20 20 2a 2a 20 d 5 byte. **
1d45a 74 79 70 65 73 20 75 73 65 20 73 6f 20 6d 75 63 types use so muc
1d45b 68 20 64 61 74 61 20 73 70 61 63 65 20 74 68 61 h data space tha
1d45c 74 20 74 68 65 72 65 20 63 61 6e 20 6f 6e 6c 79 t there can only
1d45d 20 62 65 20 34 30 39 36 20 61 6e 64 20 33 32 20 be 4096 and 32
1d45e 6f 66 0a 20 20 20 20 2a 2a 20 74 68 65 6d 2c 20 of. ** them,
1d45f 72 65 73 70 65 63 74 69 76 65 6c 79 2e 20 20 53 respectively. S
1d460 6f 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 68 65 o the maximum he
1d461 61 64 65 72 20 6c 65 6e 67 74 68 20 72 65 73 75 ader length resu
1d462 6c 74 73 20 66 72 6f 6d 20 61 0a 20 20 20 20 2a lts from a. *
1d463 2a 20 33 2d 62 79 74 65 20 74 79 70 65 20 66 6f * 3-byte type fo
1d464 72 20 65 61 63 68 20 6f 66 20 74 68 65 20 6d 61 r each of the ma
1d465 78 69 6d 75 6d 20 6f 66 20 33 32 37 36 38 20 63 ximum of 32768 c
1d466 6f 6c 75 6d 6e 73 20 70 6c 75 73 20 74 68 72 65 olumns plus thre
1d467 65 0a 20 20 20 20 2a 2a 20 65 78 74 72 61 20 62 e. ** extra b
1d468 79 74 65 73 20 66 6f 72 20 74 68 65 20 68 65 61 ytes for the hea
1d469 64 65 72 20 6c 65 6e 67 74 68 20 69 74 73 65 6c der length itsel
1d46a 66 2e 20 20 33 32 37 36 38 2a 33 20 2b 20 33 20 f. 32768*3 + 3
1d46b 3d 20 39 38 33 30 37 2e 0a 20 20 20 20 2a 2f 0a = 98307.. */.
1d46c 20 20 20 20 69 66 28 20 75 2e 61 6d 2e 6f 66 66 if( u.am.off
1d46d 73 65 74 20 3e 20 39 38 33 30 37 20 29 7b 0a 20 set > 98307 ){.
1d46e 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
1d46f 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 _CORRUPT_BKPT;.
1d470 20 20 20 20 20 67 6f 74 6f 20 6f 70 5f 63 6f 6c goto op_col
1d471 75 6d 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 0a umn_out;. }..
1d472 20 20 20 20 2f 2a 20 43 6f 6d 70 75 74 65 20 69 /* Compute i
1d473 6e 20 75 2e 61 6d 2e 6c 65 6e 20 74 68 65 20 6e n u.am.len the n
1d474 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f umber of bytes o
1d475 66 20 64 61 74 61 20 77 65 20 6e 65 65 64 20 74 f data we need t
1d476 6f 20 72 65 61 64 20 69 6e 20 6f 72 64 65 72 0a o read in order.
1d477 20 20 20 20 2a 2a 20 74 6f 20 67 65 74 20 75 2e ** to get u.
1d478 61 6d 2e 6e 46 69 65 6c 64 20 74 79 70 65 20 76 am.nField type v
1d479 61 6c 75 65 73 2e 20 20 75 2e 61 6d 2e 6f 66 66 alues. u.am.off
1d47a 73 65 74 20 69 73 20 61 6e 20 75 70 70 65 72 20 set is an upper
1d47b 62 6f 75 6e 64 20 6f 6e 20 74 68 69 73 2e 20 20 bound on this.
1d47c 42 75 74 0a 20 20 20 20 2a 2a 20 75 2e 61 6d 2e But. ** u.am.
1d47d 6e 46 69 65 6c 64 20 6d 69 67 68 74 20 62 65 20 nField might be
1d47e 73 69 67 6e 69 66 69 63 61 6e 74 6c 79 20 6c 65 significantly le
1d47f 73 73 20 74 68 61 6e 20 74 68 65 20 74 72 75 65 ss than the true
1d480 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d number of colum
1d481 6e 73 0a 20 20 20 20 2a 2a 20 69 6e 20 74 68 65 ns. ** in the
1d482 20 74 61 62 6c 65 2c 20 61 6e 64 20 69 6e 20 74 table, and in t
1d483 68 61 74 20 63 61 73 65 2c 20 35 2a 75 2e 61 6d hat case, 5*u.am
1d484 2e 6e 46 69 65 6c 64 2b 33 20 6d 69 67 68 74 20 .nField+3 might
1d485 62 65 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 be smaller than
1d486 75 2e 61 6d 2e 6f 66 66 73 65 74 2e 0a 20 20 20 u.am.offset..
1d487 20 2a 2a 20 57 65 20 77 61 6e 74 20 74 6f 20 6d ** We want to m
1d488 69 6e 69 6d 69 7a 65 20 75 2e 61 6d 2e 6c 65 6e inimize u.am.len
1d489 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6c 69 6d in order to lim
1d48a 69 74 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 it the size of t
1d48b 68 65 20 6d 65 6d 6f 72 79 0a 20 20 20 20 2a 2a he memory. **
1d48c 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 65 73 70 allocation, esp
1d48d 65 63 69 61 6c 6c 79 20 69 66 20 61 20 63 6f 72 ecially if a cor
1d48e 72 75 70 74 20 64 61 74 61 62 61 73 65 20 66 69 rupt database fi
1d48f 6c 65 20 68 61 73 20 63 61 75 73 65 64 20 75 2e le has caused u.
1d490 61 6d 2e 6f 66 66 73 65 74 0a 20 20 20 20 2a 2a am.offset. **
1d491 20 74 6f 20 62 65 20 6f 76 65 72 73 69 7a 65 64 to be oversized
1d492 2e 20 4f 66 66 73 65 74 20 69 73 20 6c 69 6d 69 . Offset is limi
1d493 74 65 64 20 74 6f 20 39 38 33 30 37 20 61 62 6f ted to 98307 abo
1d494 76 65 2e 20 20 42 75 74 20 39 38 33 30 37 20 6d ve. But 98307 m
1d495 69 67 68 74 0a 20 20 20 20 2a 2a 20 73 74 69 6c ight. ** stil
1d496 6c 20 65 78 63 65 65 64 20 52 6f 62 73 6f 6e 20 l exceed Robson
1d497 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
1d498 6e 20 6c 69 6d 69 74 73 20 6f 6e 20 73 6f 6d 65 n limits on some
1d499 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 73 2e configurations.
1d49a 0a 20 20 20 20 2a 2a 20 4f 6e 20 73 79 73 74 65 . ** On syste
1d49b 6d 73 20 74 68 61 74 20 63 61 6e 6e 6f 74 20 74 ms that cannot t
1d49c 6f 6c 65 72 61 74 65 20 6c 61 72 67 65 20 6d 65 olerate large me
1d49d 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 mory allocations
1d49e 2c 20 75 2e 61 6d 2e 6e 46 69 65 6c 64 2a 35 2b , u.am.nField*5+
1d49f 33 0a 20 20 20 20 2a 2a 20 77 69 6c 6c 20 6c 69 3. ** will li
1d4a0 6b 65 6c 79 20 62 65 20 6d 75 63 68 20 73 6d 61 kely be much sma
1d4a1 6c 6c 65 72 20 73 69 6e 63 65 20 75 2e 61 6d 2e ller since u.am.
1d4a2 6e 46 69 65 6c 64 20 77 69 6c 6c 20 6c 69 6b 65 nField will like
1d4a3 6c 79 20 62 65 20 6c 65 73 73 20 74 68 61 6e 0a ly be less than.
1d4a4 20 20 20 20 2a 2a 20 32 30 20 6f 72 20 73 6f 2e ** 20 or so.
1d4a5 20 20 54 68 69 73 20 69 6e 73 75 72 65 73 20 74 This insures t
1d4a6 68 61 74 20 52 6f 62 73 6f 6e 20 6d 65 6d 6f 72 hat Robson memor
1d4a7 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6c 69 6d y allocation lim
1d4a8 69 74 73 20 61 72 65 0a 20 20 20 20 2a 2a 20 6e its are. ** n
1d4a9 6f 74 20 65 78 63 65 65 64 65 64 20 65 76 65 6e ot exceeded even
1d4aa 20 66 6f 72 20 63 6f 72 72 75 70 74 20 64 61 74 for corrupt dat
1d4ab 61 62 61 73 65 20 66 69 6c 65 73 2e 0a 20 20 20 abase files..
1d4ac 20 2a 2f 0a 20 20 20 20 75 2e 61 6d 2e 6c 65 6e */. u.am.len
1d4ad 20 3d 20 75 2e 61 6d 2e 6e 46 69 65 6c 64 2a 35 = u.am.nField*5
1d4ae 20 2b 20 33 3b 0a 20 20 20 20 69 66 28 20 75 2e + 3;. if( u.
1d4af 61 6d 2e 6c 65 6e 20 3e 20 28 69 6e 74 29 75 2e am.len > (int)u.
1d4b0 61 6d 2e 6f 66 66 73 65 74 20 29 20 75 2e 61 6d am.offset ) u.am
1d4b1 2e 6c 65 6e 20 3d 20 28 69 6e 74 29 75 2e 61 6d .len = (int)u.am
1d4b2 2e 6f 66 66 73 65 74 3b 0a 0a 20 20 20 20 2f 2a .offset;.. /*
1d4b3 20 54 68 65 20 4b 65 79 46 65 74 63 68 28 29 20 The KeyFetch()
1d4b4 6f 72 20 44 61 74 61 46 65 74 63 68 28 29 20 61 or DataFetch() a
1d4b5 62 6f 76 65 20 61 72 65 20 66 61 73 74 20 61 6e bove are fast an
1d4b6 64 20 77 69 6c 6c 20 67 65 74 20 74 68 65 20 65 d will get the e
1d4b7 6e 74 69 72 65 0a 20 20 20 20 2a 2a 20 72 65 63 ntire. ** rec
1d4b8 6f 72 64 20 68 65 61 64 65 72 20 69 6e 20 6d 6f ord header in mo
1d4b9 73 74 20 63 61 73 65 73 2e 20 20 42 75 74 20 74 st cases. But t
1d4ba 68 65 79 20 77 69 6c 6c 20 66 61 69 6c 20 74 6f hey will fail to
1d4bb 20 67 65 74 20 74 68 65 20 63 6f 6d 70 6c 65 74 get the complet
1d4bc 65 0a 20 20 20 20 2a 2a 20 72 65 63 6f 72 64 20 e. ** record
1d4bd 68 65 61 64 65 72 20 69 66 20 74 68 65 20 72 65 header if the re
1d4be 63 6f 72 64 20 68 65 61 64 65 72 20 64 6f 65 73 cord header does
1d4bf 20 6e 6f 74 20 66 69 74 20 6f 6e 20 61 20 73 69 not fit on a si
1d4c0 6e 67 6c 65 20 70 61 67 65 0a 20 20 20 20 2a 2a ngle page. **
1d4c1 20 69 6e 20 74 68 65 20 42 2d 54 72 65 65 2e 20 in the B-Tree.
1d4c2 20 57 68 65 6e 20 74 68 61 74 20 68 61 70 70 65 When that happe
1d4c3 6e 73 2c 20 75 73 65 20 73 71 6c 69 74 65 33 56 ns, use sqlite3V
1d4c4 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28 dbeMemFromBtree(
1d4c5 29 20 74 6f 0a 20 20 20 20 2a 2a 20 61 63 71 75 ) to. ** acqu
1d4c6 69 72 65 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 ire the complete
1d4c7 20 68 65 61 64 65 72 20 74 65 78 74 2e 0a 20 20 header text..
1d4c8 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 75 2e */. if( !u.
1d4c9 61 6d 2e 7a 52 65 63 20 26 26 20 75 2e 61 6d 2e am.zRec && u.am.
1d4ca 61 76 61 69 6c 3c 75 2e 61 6d 2e 6c 65 6e 20 29 avail<u.am.len )
1d4cb 7b 0a 20 20 20 20 20 20 75 2e 61 6d 2e 73 4d 65 {. u.am.sMe
1d4cc 6d 2e 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20 20 m.flags = 0;.
1d4cd 20 20 20 75 2e 61 6d 2e 73 4d 65 6d 2e 64 62 20 u.am.sMem.db
1d4ce 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 = 0;. rc =
1d4cf 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 72 sqlite3VdbeMemFr
1d4d0 6f 6d 42 74 72 65 65 28 75 2e 61 6d 2e 70 43 72 omBtree(u.am.pCr
1d4d1 73 72 2c 20 30 2c 20 75 2e 61 6d 2e 6c 65 6e 2c sr, 0, u.am.len,
1d4d2 20 75 2e 61 6d 2e 70 43 2d 3e 69 73 49 6e 64 65 u.am.pC->isInde
1d4d3 78 2c 20 26 75 2e 61 6d 2e 73 4d 65 6d 29 3b 0a x, &u.am.sMem);.
1d4d4 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 if( rc!=SQ
1d4d5 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
1d4d6 20 20 20 67 6f 74 6f 20 6f 70 5f 63 6f 6c 75 6d goto op_colum
1d4d7 6e 5f 6f 75 74 3b 0a 20 20 20 20 20 20 7d 0a 20 n_out;. }.
1d4d8 20 20 20 20 20 75 2e 61 6d 2e 7a 44 61 74 61 20 u.am.zData
1d4d9 3d 20 75 2e 61 6d 2e 73 4d 65 6d 2e 7a 3b 0a 20 = u.am.sMem.z;.
1d4da 20 20 20 7d 0a 20 20 20 20 75 2e 61 6d 2e 7a 45 }. u.am.zE
1d4db 6e 64 48 64 72 20 3d 20 28 75 38 20 2a 29 26 75 ndHdr = (u8 *)&u
1d4dc 2e 61 6d 2e 7a 44 61 74 61 5b 75 2e 61 6d 2e 6c .am.zData[u.am.l
1d4dd 65 6e 5d 3b 0a 20 20 20 20 75 2e 61 6d 2e 7a 49 en];. u.am.zI
1d4de 64 78 20 3d 20 28 75 38 20 2a 29 26 75 2e 61 6d dx = (u8 *)&u.am
1d4df 2e 7a 44 61 74 61 5b 75 2e 61 6d 2e 73 7a 48 64 .zData[u.am.szHd
1d4e0 72 5d 3b 0a 0a 20 20 20 20 2f 2a 20 53 63 61 6e r];.. /* Scan
1d4e1 20 74 68 65 20 68 65 61 64 65 72 20 61 6e 64 20 the header and
1d4e2 75 73 65 20 69 74 20 74 6f 20 66 69 6c 6c 20 69 use it to fill i
1d4e3 6e 20 74 68 65 20 75 2e 61 6d 2e 61 54 79 70 65 n the u.am.aType
1d4e4 5b 5d 20 61 6e 64 20 75 2e 61 6d 2e 61 4f 66 66 [] and u.am.aOff
1d4e5 73 65 74 5b 5d 0a 20 20 20 20 2a 2a 20 61 72 72 set[]. ** arr
1d4e6 61 79 73 2e 20 20 75 2e 61 6d 2e 61 54 79 70 65 ays. u.am.aType
1d4e7 5b 75 2e 61 6d 2e 69 5d 20 77 69 6c 6c 20 63 6f [u.am.i] will co
1d4e8 6e 74 61 69 6e 20 74 68 65 20 74 79 70 65 20 69 ntain the type i
1d4e9 6e 74 65 67 65 72 20 66 6f 72 20 74 68 65 20 75 nteger for the u
1d4ea 2e 61 6d 2e 69 2d 74 68 0a 20 20 20 20 2a 2a 20 .am.i-th. **
1d4eb 63 6f 6c 75 6d 6e 20 61 6e 64 20 75 2e 61 6d 2e column and u.am.
1d4ec 61 4f 66 66 73 65 74 5b 75 2e 61 6d 2e 69 5d 20 aOffset[u.am.i]
1d4ed 77 69 6c 6c 20 63 6f 6e 74 61 69 6e 20 74 68 65 will contain the
1d4ee 20 75 2e 61 6d 2e 6f 66 66 73 65 74 20 66 72 6f u.am.offset fro
1d4ef 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 0a m the beginning.
1d4f0 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 72 65 ** of the re
1d4f1 63 6f 72 64 20 74 6f 20 74 68 65 20 73 74 61 72 cord to the star
1d4f2 74 20 6f 66 20 74 68 65 20 64 61 74 61 20 66 6f t of the data fo
1d4f3 72 20 74 68 65 20 75 2e 61 6d 2e 69 2d 74 68 20 r the u.am.i-th
1d4f4 63 6f 6c 75 6d 6e 0a 20 20 20 20 2a 2f 0a 20 20 column. */.
1d4f5 20 20 75 2e 61 6d 2e 6f 66 66 73 65 74 36 34 20 u.am.offset64
1d4f6 3d 20 75 2e 61 6d 2e 6f 66 66 73 65 74 3b 0a 20 = u.am.offset;.
1d4f7 20 20 20 66 6f 72 28 75 2e 61 6d 2e 69 3d 30 3b for(u.am.i=0;
1d4f8 20 75 2e 61 6d 2e 69 3c 75 2e 61 6d 2e 6e 46 69 u.am.i<u.am.nFi
1d4f9 65 6c 64 3b 20 75 2e 61 6d 2e 69 2b 2b 29 7b 0a eld; u.am.i++){.
1d4fa 20 20 20 20 20 20 69 66 28 20 75 2e 61 6d 2e 7a if( u.am.z
1d4fb 49 64 78 3c 75 2e 61 6d 2e 7a 45 6e 64 48 64 72 Idx<u.am.zEndHdr
1d4fc 20 29 7b 0a 20 20 20 20 20 20 20 20 75 2e 61 6d ){. u.am
1d4fd 2e 61 4f 66 66 73 65 74 5b 75 2e 61 6d 2e 69 5d .aOffset[u.am.i]
1d4fe 20 3d 20 28 75 33 32 29 75 2e 61 6d 2e 6f 66 66 = (u32)u.am.off
1d4ff 73 65 74 36 34 3b 0a 20 20 20 20 20 20 20 20 75 set64;. u
1d500 2e 61 6d 2e 7a 49 64 78 20 2b 3d 20 67 65 74 56 .am.zIdx += getV
1d501 61 72 69 6e 74 33 32 28 75 2e 61 6d 2e 7a 49 64 arint32(u.am.zId
1d502 78 2c 20 75 2e 61 6d 2e 61 54 79 70 65 5b 75 2e x, u.am.aType[u.
1d503 61 6d 2e 69 5d 29 3b 0a 20 20 20 20 20 20 20 20 am.i]);.
1d504 75 2e 61 6d 2e 6f 66 66 73 65 74 36 34 20 2b 3d u.am.offset64 +=
1d505 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 sqlite3VdbeSeri
1d506 61 6c 54 79 70 65 4c 65 6e 28 75 2e 61 6d 2e 61 alTypeLen(u.am.a
1d507 54 79 70 65 5b 75 2e 61 6d 2e 69 5d 29 3b 0a 20 Type[u.am.i]);.
1d508 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
1d509 20 20 20 20 2f 2a 20 49 66 20 75 2e 61 6d 2e 69 /* If u.am.i
1d50a 20 69 73 20 6c 65 73 73 20 74 68 61 74 20 75 2e is less that u.
1d50b 61 6d 2e 6e 46 69 65 6c 64 2c 20 74 68 65 6e 20 am.nField, then
1d50c 74 68 65 72 65 20 61 72 65 20 6c 65 73 73 20 66 there are less f
1d50d 69 65 6c 64 73 20 69 6e 20 74 68 69 73 0a 20 20 ields in this.
1d50e 20 20 20 20 20 20 2a 2a 20 72 65 63 6f 72 64 20 ** record
1d50f 74 68 61 6e 20 53 65 74 4e 75 6d 43 6f 6c 75 6d than SetNumColum
1d510 6e 73 20 69 6e 64 69 63 61 74 65 64 20 74 68 65 ns indicated the
1d511 72 65 20 61 72 65 20 63 6f 6c 75 6d 6e 73 20 69 re are columns i
1d512 6e 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a n the. **
1d513 20 74 61 62 6c 65 2e 20 53 65 74 20 74 68 65 20 table. Set the
1d514 75 2e 61 6d 2e 6f 66 66 73 65 74 20 66 6f 72 20 u.am.offset for
1d515 61 6e 79 20 65 78 74 72 61 20 63 6f 6c 75 6d 6e any extra column
1d516 73 20 6e 6f 74 20 70 72 65 73 65 6e 74 20 69 6e s not present in
1d517 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20 . ** the
1d518 72 65 63 6f 72 64 20 74 6f 20 30 2e 20 54 68 69 record to 0. Thi
1d519 73 20 74 65 6c 6c 73 20 63 6f 64 65 20 62 65 6c s tells code bel
1d51a 6f 77 20 74 6f 20 73 74 6f 72 65 20 61 20 4e 55 ow to store a NU
1d51b 4c 4c 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 6e LL. ** in
1d51c 73 74 65 61 64 20 6f 66 20 64 65 73 65 72 69 61 stead of deseria
1d51d 6c 69 7a 69 6e 67 20 61 20 76 61 6c 75 65 20 66 lizing a value f
1d51e 72 6f 6d 20 74 68 65 20 72 65 63 6f 72 64 2e 0a rom the record..
1d51f 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 */.
1d520 20 20 20 75 2e 61 6d 2e 61 4f 66 66 73 65 74 5b u.am.aOffset[
1d521 75 2e 61 6d 2e 69 5d 20 3d 20 30 3b 0a 20 20 20 u.am.i] = 0;.
1d522 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 }. }. s
1d523 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c qlite3VdbeMemRel
1d524 65 61 73 65 28 26 75 2e 61 6d 2e 73 4d 65 6d 29 ease(&u.am.sMem)
1d525 3b 0a 20 20 20 20 75 2e 61 6d 2e 73 4d 65 6d 2e ;. u.am.sMem.
1d526 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c flags = MEM_Null
1d527 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 77 65 20 ;.. /* If we
1d528 68 61 76 65 20 72 65 61 64 20 6d 6f 72 65 20 68 have read more h
1d529 65 61 64 65 72 20 64 61 74 61 20 74 68 61 6e 20 eader data than
1d52a 77 61 73 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e was contained in
1d52b 20 74 68 65 20 68 65 61 64 65 72 2c 0a 20 20 20 the header,.
1d52c 20 2a 2a 20 6f 72 20 69 66 20 74 68 65 20 65 6e ** or if the en
1d52d 64 20 6f 66 20 74 68 65 20 6c 61 73 74 20 66 69 d of the last fi
1d52e 65 6c 64 20 61 70 70 65 61 72 73 20 74 6f 20 62 eld appears to b
1d52f 65 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f e past the end o
1d530 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 72 65 63 f the. ** rec
1d531 6f 72 64 2c 20 6f 72 20 69 66 20 74 68 65 20 65 ord, or if the e
1d532 6e 64 20 6f 66 20 74 68 65 20 6c 61 73 74 20 66 nd of the last f
1d533 69 65 6c 64 20 61 70 70 65 61 72 73 20 74 6f 20 ield appears to
1d534 62 65 20 62 65 66 6f 72 65 20 74 68 65 20 65 6e be before the en
1d535 64 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 d. ** of the
1d536 72 65 63 6f 72 64 20 28 77 68 65 6e 20 61 6c 6c record (when all
1d537 20 66 69 65 6c 64 73 20 70 72 65 73 65 6e 74 29 fields present)
1d538 2c 20 74 68 65 6e 20 77 65 20 6d 75 73 74 20 62 , then we must b
1d539 65 20 64 65 61 6c 69 6e 67 0a 20 20 20 20 2a 2a e dealing. **
1d53a 20 77 69 74 68 20 61 20 63 6f 72 72 75 70 74 20 with a corrupt
1d53b 64 61 74 61 62 61 73 65 2e 0a 20 20 20 20 2a 2f database.. */
1d53c 0a 20 20 20 20 69 66 28 20 28 75 2e 61 6d 2e 7a . if( (u.am.z
1d53d 49 64 78 20 3e 20 75 2e 61 6d 2e 7a 45 6e 64 48 Idx > u.am.zEndH
1d53e 64 72 29 7c 7c 20 28 75 2e 61 6d 2e 6f 66 66 73 dr)|| (u.am.offs
1d53f 65 74 36 34 20 3e 20 75 2e 61 6d 2e 70 61 79 6c et64 > u.am.payl
1d540 6f 61 64 53 69 7a 65 29 0a 20 20 20 20 20 7c 7c oadSize). ||
1d541 20 28 75 2e 61 6d 2e 7a 49 64 78 3d 3d 75 2e 61 (u.am.zIdx==u.a
1d542 6d 2e 7a 45 6e 64 48 64 72 20 26 26 20 75 2e 61 m.zEndHdr && u.a
1d543 6d 2e 6f 66 66 73 65 74 36 34 21 3d 28 75 36 34 m.offset64!=(u64
1d544 29 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a )u.am.payloadSiz
1d545 65 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d e) ){. rc =
1d546 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
1d547 42 4b 50 54 3b 0a 20 20 20 20 20 20 67 6f 74 6f BKPT;. goto
1d548 20 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f 75 74 3b 0a op_column_out;.
1d549 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 }. }.. /*
1d54a 47 65 74 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69 Get the column i
1d54b 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 49 66 20 75 nformation. If u
1d54c 2e 61 6d 2e 61 4f 66 66 73 65 74 5b 75 2e 61 6d .am.aOffset[u.am
1d54d 2e 70 32 5d 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f .p2] is non-zero
1d54e 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 64 65 73 65 , then. ** dese
1d54f 72 69 61 6c 69 7a 65 20 74 68 65 20 76 61 6c 75 rialize the valu
1d550 65 20 66 72 6f 6d 20 74 68 65 20 72 65 63 6f 72 e from the recor
1d551 64 2e 20 49 66 20 75 2e 61 6d 2e 61 4f 66 66 73 d. If u.am.aOffs
1d552 65 74 5b 75 2e 61 6d 2e 70 32 5d 20 69 73 20 7a et[u.am.p2] is z
1d553 65 72 6f 2c 0a 20 20 2a 2a 20 74 68 65 6e 20 74 ero,. ** then t
1d554 68 65 72 65 20 61 72 65 20 6e 6f 74 20 65 6e 6f here are not eno
1d555 75 67 68 20 66 69 65 6c 64 73 20 69 6e 20 74 68 ugh fields in th
1d556 65 20 72 65 63 6f 72 64 20 74 6f 20 73 61 74 69 e record to sati
1d557 73 66 79 20 74 68 65 0a 20 20 2a 2a 20 72 65 71 sfy the. ** req
1d558 75 65 73 74 2e 20 20 49 6e 20 74 68 69 73 20 63 uest. In this c
1d559 61 73 65 2c 20 73 65 74 20 74 68 65 20 76 61 6c ase, set the val
1d55a 75 65 20 4e 55 4c 4c 20 6f 72 20 74 6f 20 50 34 ue NULL or to P4
1d55b 20 69 66 20 50 34 20 69 73 0a 20 20 2a 2a 20 61 if P4 is. ** a
1d55c 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 4d 65 pointer to a Me
1d55d 6d 20 6f 62 6a 65 63 74 2e 0a 20 20 2a 2f 0a 20 m object.. */.
1d55e 20 69 66 28 20 75 2e 61 6d 2e 61 4f 66 66 73 65 if( u.am.aOffse
1d55f 74 5b 75 2e 61 6d 2e 70 32 5d 20 29 7b 0a 20 20 t[u.am.p2] ){.
1d560 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 assert( rc==SQ
1d561 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 69 LITE_OK );. i
1d562 66 28 20 75 2e 61 6d 2e 7a 52 65 63 20 29 7b 0a f( u.am.zRec ){.
1d563 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
1d564 65 4d 65 6d 52 65 6c 65 61 73 65 45 78 74 65 72 eMemReleaseExter
1d565 6e 61 6c 28 75 2e 61 6d 2e 70 44 65 73 74 29 3b nal(u.am.pDest);
1d566 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
1d567 62 65 53 65 72 69 61 6c 47 65 74 28 28 75 38 20 beSerialGet((u8
1d568 2a 29 26 75 2e 61 6d 2e 7a 52 65 63 5b 75 2e 61 *)&u.am.zRec[u.a
1d569 6d 2e 61 4f 66 66 73 65 74 5b 75 2e 61 6d 2e 70 m.aOffset[u.am.p
1d56a 32 5d 5d 2c 20 75 2e 61 6d 2e 61 54 79 70 65 5b 2]], u.am.aType[
1d56b 75 2e 61 6d 2e 70 32 5d 2c 20 75 2e 61 6d 2e 70 u.am.p2], u.am.p
1d56c 44 65 73 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 Dest);. }else
1d56d 7b 0a 20 20 20 20 20 20 75 2e 61 6d 2e 6c 65 6e {. u.am.len
1d56e 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 = sqlite3VdbeSe
1d56f 72 69 61 6c 54 79 70 65 4c 65 6e 28 75 2e 61 6d rialTypeLen(u.am
1d570 2e 61 54 79 70 65 5b 75 2e 61 6d 2e 70 32 5d 29 .aType[u.am.p2])
1d571 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 ;. sqlite3V
1d572 64 62 65 4d 65 6d 4d 6f 76 65 28 26 75 2e 61 6d dbeMemMove(&u.am
1d573 2e 73 4d 65 6d 2c 20 75 2e 61 6d 2e 70 44 65 73 .sMem, u.am.pDes
1d574 74 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 t);. rc = s
1d575 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 72 6f qlite3VdbeMemFro
1d576 6d 42 74 72 65 65 28 75 2e 61 6d 2e 70 43 72 73 mBtree(u.am.pCrs
1d577 72 2c 20 75 2e 61 6d 2e 61 4f 66 66 73 65 74 5b r, u.am.aOffset[
1d578 75 2e 61 6d 2e 70 32 5d 2c 20 75 2e 61 6d 2e 6c u.am.p2], u.am.l
1d579 65 6e 2c 20 75 2e 61 6d 2e 70 43 2d 3e 69 73 49 en, u.am.pC->isI
1d57a 6e 64 65 78 2c 20 26 75 2e 61 6d 2e 73 4d 65 6d ndex, &u.am.sMem
1d57b 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 );. if( rc!
1d57c 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
1d57d 20 20 20 20 20 20 67 6f 74 6f 20 6f 70 5f 63 6f goto op_co
1d57e 6c 75 6d 6e 5f 6f 75 74 3b 0a 20 20 20 20 20 20 lumn_out;.
1d57f 7d 0a 20 20 20 20 20 20 75 2e 61 6d 2e 7a 44 61 }. u.am.zDa
1d580 74 61 20 3d 20 75 2e 61 6d 2e 73 4d 65 6d 2e 7a ta = u.am.sMem.z
1d581 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 ;. sqlite3V
1d582 64 62 65 53 65 72 69 61 6c 47 65 74 28 28 75 38 dbeSerialGet((u8
1d583 2a 29 75 2e 61 6d 2e 7a 44 61 74 61 2c 20 75 2e *)u.am.zData, u.
1d584 61 6d 2e 61 54 79 70 65 5b 75 2e 61 6d 2e 70 32 am.aType[u.am.p2
1d585 5d 2c 20 75 2e 61 6d 2e 70 44 65 73 74 29 3b 0a ], u.am.pDest);.
1d586 20 20 20 20 7d 0a 20 20 20 20 75 2e 61 6d 2e 70 }. u.am.p
1d587 44 65 73 74 2d 3e 65 6e 63 20 3d 20 65 6e 63 6f Dest->enc = enco
1d588 64 69 6e 67 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 ding;. }else{.
1d589 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 34 74 79 if( pOp->p4ty
1d58a 70 65 3d 3d 50 34 5f 4d 45 4d 20 29 7b 0a 20 20 pe==P4_MEM ){.
1d58b 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
1d58c 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28 75 2e emShallowCopy(u.
1d58d 61 6d 2e 70 44 65 73 74 2c 20 70 4f 70 2d 3e 70 am.pDest, pOp->p
1d58e 34 2e 70 4d 65 6d 2c 20 4d 45 4d 5f 53 74 61 74 4.pMem, MEM_Stat
1d58f 69 63 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a ic);. }else{.
1d590 20 20 20 20 20 20 61 73 73 65 72 74 28 20 75 2e assert( u.
1d591 61 6d 2e 70 44 65 73 74 2d 3e 66 6c 61 67 73 26 am.pDest->flags&
1d592 4d 45 4d 5f 4e 75 6c 6c 20 29 3b 0a 20 20 20 20 MEM_Null );.
1d593 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 77 }. }.. /* If w
1d594 65 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c e dynamically al
1d595 6c 6f 63 61 74 65 64 20 73 70 61 63 65 20 74 6f located space to
1d596 20 68 6f 6c 64 20 74 68 65 20 64 61 74 61 20 28 hold the data (
1d597 69 6e 20 74 68 65 0a 20 20 2a 2a 20 73 71 6c 69 in the. ** sqli
1d598 74 65 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42 74 te3VdbeMemFromBt
1d599 72 65 65 28 29 20 63 61 6c 6c 20 61 62 6f 76 65 ree() call above
1d59a 29 20 74 68 65 6e 20 74 72 61 6e 73 66 65 72 20 ) then transfer
1d59b 63 6f 6e 74 72 6f 6c 20 6f 66 20 74 68 61 74 0a control of that.
1d59c 20 20 2a 2a 20 64 79 6e 61 6d 69 63 61 6c 6c 79 ** dynamically
1d59d 20 61 6c 6c 6f 63 61 74 65 64 20 73 70 61 63 65 allocated space
1d59e 20 6f 76 65 72 20 74 6f 20 74 68 65 20 75 2e 61 over to the u.a
1d59f 6d 2e 70 44 65 73 74 20 73 74 72 75 63 74 75 72 m.pDest structur
1d5a0 65 2e 0a 20 20 2a 2a 20 54 68 69 73 20 70 72 65 e.. ** This pre
1d5a1 76 65 6e 74 73 20 61 20 6d 65 6d 6f 72 79 20 63 vents a memory c
1d5a2 6f 70 79 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 opy.. */. if(
1d5a3 75 2e 61 6d 2e 73 4d 65 6d 2e 7a 4d 61 6c 6c 6f u.am.sMem.zMallo
1d5a4 63 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 c ){. assert(
1d5a5 20 75 2e 61 6d 2e 73 4d 65 6d 2e 7a 3d 3d 75 2e u.am.sMem.z==u.
1d5a6 61 6d 2e 73 4d 65 6d 2e 7a 4d 61 6c 6c 6f 63 20 am.sMem.zMalloc
1d5a7 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 );. assert( !
1d5a8 28 75 2e 61 6d 2e 70 44 65 73 74 2d 3e 66 6c 61 (u.am.pDest->fla
1d5a9 67 73 20 26 20 4d 45 4d 5f 44 79 6e 29 20 29 3b gs & MEM_Dyn) );
1d5aa 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 28 75 . assert( !(u
1d5ab 2e 61 6d 2e 70 44 65 73 74 2d 3e 66 6c 61 67 73 .am.pDest->flags
1d5ac 20 26 20 28 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d & (MEM_Blob|MEM
1d5ad 5f 53 74 72 29 29 20 7c 7c 20 75 2e 61 6d 2e 70 _Str)) || u.am.p
1d5ae 44 65 73 74 2d 3e 7a 3d 3d 75 2e 61 6d 2e 73 4d Dest->z==u.am.sM
1d5af 65 6d 2e 7a 20 29 3b 0a 20 20 20 20 75 2e 61 6d em.z );. u.am
1d5b0 2e 70 44 65 73 74 2d 3e 66 6c 61 67 73 20 26 3d .pDest->flags &=
1d5b1 20 7e 28 4d 45 4d 5f 45 70 68 65 6d 7c 4d 45 4d ~(MEM_Ephem|MEM
1d5b2 5f 53 74 61 74 69 63 29 3b 0a 20 20 20 20 75 2e _Static);. u.
1d5b3 61 6d 2e 70 44 65 73 74 2d 3e 66 6c 61 67 73 20 am.pDest->flags
1d5b4 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 20 |= MEM_Term;.
1d5b5 20 75 2e 61 6d 2e 70 44 65 73 74 2d 3e 7a 20 3d u.am.pDest->z =
1d5b6 20 75 2e 61 6d 2e 73 4d 65 6d 2e 7a 3b 0a 20 20 u.am.sMem.z;.
1d5b7 20 20 75 2e 61 6d 2e 70 44 65 73 74 2d 3e 7a 4d u.am.pDest->zM
1d5b8 61 6c 6c 6f 63 20 3d 20 75 2e 61 6d 2e 73 4d 65 alloc = u.am.sMe
1d5b9 6d 2e 7a 4d 61 6c 6c 6f 63 3b 0a 20 20 7d 0a 0a m.zMalloc;. }..
1d5ba 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 rc = sqlite3Vd
1d5bb 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61 62 beMemMakeWriteab
1d5bc 6c 65 28 75 2e 61 6d 2e 70 44 65 73 74 29 3b 0a le(u.am.pDest);.
1d5bd 0a 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f 75 74 3a 0a .op_column_out:.
1d5be 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f UPDATE_MAX_BLO
1d5bf 42 53 49 5a 45 28 75 2e 61 6d 2e 70 44 65 73 74 BSIZE(u.am.pDest
1d5c0 29 3b 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52 );. REGISTER_TR
1d5c1 41 43 45 28 70 4f 70 2d 3e 70 33 2c 20 75 2e 61 ACE(pOp->p3, u.a
1d5c2 6d 2e 70 44 65 73 74 29 3b 0a 20 20 62 72 65 61 m.pDest);. brea
1d5c3 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a k;.}../* Opcode:
1d5c4 20 41 66 66 69 6e 69 74 79 20 50 31 20 50 32 20 Affinity P1 P2
1d5c5 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 41 70 70 * P4 *.**.** App
1d5c6 6c 79 20 61 66 66 69 6e 69 74 69 65 73 20 74 6f ly affinities to
1d5c7 20 61 20 72 61 6e 67 65 20 6f 66 20 50 32 20 72 a range of P2 r
1d5c8 65 67 69 73 74 65 72 73 20 73 74 61 72 74 69 6e egisters startin
1d5c9 67 20 77 69 74 68 20 50 31 2e 0a 2a 2a 0a 2a 2a g with P1..**.**
1d5ca 20 50 34 20 69 73 20 61 20 73 74 72 69 6e 67 20 P4 is a string
1d5cb 74 68 61 74 20 69 73 20 50 32 20 63 68 61 72 61 that is P2 chara
1d5cc 63 74 65 72 73 20 6c 6f 6e 67 2e 20 54 68 65 20 cters long. The
1d5cd 6e 74 68 20 63 68 61 72 61 63 74 65 72 20 6f 66 nth character of
1d5ce 20 74 68 65 0a 2a 2a 20 73 74 72 69 6e 67 20 69 the.** string i
1d5cf 6e 64 69 63 61 74 65 73 20 74 68 65 20 63 6f 6c ndicates the col
1d5d0 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 74 68 61 umn affinity tha
1d5d1 74 20 73 68 6f 75 6c 64 20 62 65 20 75 73 65 64 t should be used
1d5d2 20 66 6f 72 20 74 68 65 20 6e 74 68 0a 2a 2a 20 for the nth.**
1d5d3 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 69 6e 20 74 memory cell in t
1d5d4 68 65 20 72 61 6e 67 65 2e 0a 2a 2f 0a 63 61 73 he range..*/.cas
1d5d5 65 20 4f 50 5f 41 66 66 69 6e 69 74 79 3a 20 7b e OP_Affinity: {
1d5d6 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c .#if 0 /* local
1d5d7 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1d5d8 20 69 6e 74 6f 20 75 2e 61 6e 20 2a 2f 0a 20 20 into u.an */.
1d5d9 63 68 61 72 20 2a 7a 41 66 66 69 6e 69 74 79 3b char *zAffinity;
1d5da 20 20 20 2f 2a 20 54 68 65 20 61 66 66 69 6e 69 /* The affini
1d5db 74 79 20 74 6f 20 62 65 20 61 70 70 6c 69 65 64 ty to be applied
1d5dc 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 44 61 74 61 */. Mem *pData
1d5dd 30 3b 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 0; /* Firs
1d5de 74 20 72 65 67 69 73 74 65 72 20 74 6f 20 77 68 t register to wh
1d5df 69 63 68 20 74 6f 20 61 70 70 6c 79 20 61 66 66 ich to apply aff
1d5e0 69 6e 69 74 79 20 2a 2f 0a 20 20 4d 65 6d 20 2a inity */. Mem *
1d5e1 70 4c 61 73 74 3b 20 20 20 20 20 20 20 20 2f 2a pLast; /*
1d5e2 20 4c 61 73 74 20 72 65 67 69 73 74 65 72 20 74 Last register t
1d5e3 6f 20 77 68 69 63 68 20 74 6f 20 61 70 70 6c 79 o which to apply
1d5e4 20 61 66 66 69 6e 69 74 79 20 2a 2f 0a 20 20 4d affinity */. M
1d5e5 65 6d 20 2a 70 52 65 63 3b 20 20 20 20 20 20 20 em *pRec;
1d5e6 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 72 65 67 /* Current reg
1d5e7 69 73 74 65 72 20 2a 2f 0a 23 65 6e 64 69 66 20 ister */.#endif
1d5e8 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c /* local variabl
1d5e9 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e es moved into u.
1d5ea 61 6e 20 2a 2f 0a 0a 20 20 75 2e 61 6e 2e 7a 41 an */.. u.an.zA
1d5eb 66 66 69 6e 69 74 79 20 3d 20 70 4f 70 2d 3e 70 ffinity = pOp->p
1d5ec 34 2e 7a 3b 0a 20 20 75 2e 61 6e 2e 70 44 61 74 4.z;. u.an.pDat
1d5ed 61 30 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f a0 = &p->aMem[pO
1d5ee 70 2d 3e 70 31 5d 3b 0a 20 20 75 2e 61 6e 2e 70 p->p1];. u.an.p
1d5ef 4c 61 73 74 20 3d 20 26 75 2e 61 6e 2e 70 44 61 Last = &u.an.pDa
1d5f0 74 61 30 5b 70 4f 70 2d 3e 70 32 2d 31 5d 3b 0a ta0[pOp->p2-1];.
1d5f1 20 20 66 6f 72 28 75 2e 61 6e 2e 70 52 65 63 3d for(u.an.pRec=
1d5f2 75 2e 61 6e 2e 70 44 61 74 61 30 3b 20 75 2e 61 u.an.pData0; u.a
1d5f3 6e 2e 70 52 65 63 3c 3d 75 2e 61 6e 2e 70 4c 61 n.pRec<=u.an.pLa
1d5f4 73 74 3b 20 75 2e 61 6e 2e 70 52 65 63 2b 2b 29 st; u.an.pRec++)
1d5f5 7b 0a 20 20 20 20 45 78 70 61 6e 64 42 6c 6f 62 {. ExpandBlob
1d5f6 28 75 2e 61 6e 2e 70 52 65 63 29 3b 0a 20 20 20 (u.an.pRec);.
1d5f7 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28 75 applyAffinity(u
1d5f8 2e 61 6e 2e 70 52 65 63 2c 20 75 2e 61 6e 2e 7a .an.pRec, u.an.z
1d5f9 41 66 66 69 6e 69 74 79 5b 75 2e 61 6e 2e 70 52 Affinity[u.an.pR
1d5fa 65 63 2d 75 2e 61 6e 2e 70 44 61 74 61 30 5d 2c ec-u.an.pData0],
1d5fb 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 7d 0a encoding);. }.
1d5fc 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f break;.}../* O
1d5fd 70 63 6f 64 65 3a 20 4d 61 6b 65 52 65 63 6f 72 pcode: MakeRecor
1d5fe 64 20 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a d P1 P2 P3 P4 *.
1d5ff 2a 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 50 32 **.** Convert P2
1d600 20 72 65 67 69 73 74 65 72 73 20 62 65 67 69 6e registers begin
1d601 6e 69 6e 67 20 77 69 74 68 20 50 31 20 69 6e 74 ning with P1 int
1d602 6f 20 61 20 73 69 6e 67 6c 65 20 65 6e 74 72 79 o a single entry
1d603 0a 2a 2a 20 73 75 69 74 61 62 6c 65 20 66 6f 72 .** suitable for
1d604 20 75 73 65 20 61 73 20 61 20 64 61 74 61 20 72 use as a data r
1d605 65 63 6f 72 64 20 69 6e 20 61 20 64 61 74 61 62 ecord in a datab
1d606 61 73 65 20 74 61 62 6c 65 20 6f 72 20 61 73 20 ase table or as
1d607 61 20 6b 65 79 0a 2a 2a 20 69 6e 20 61 6e 20 69 a key.** in an i
1d608 6e 64 65 78 2e 20 20 54 68 65 20 64 65 74 61 69 ndex. The detai
1d609 6c 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 61 74 ls of the format
1d60a 20 61 72 65 20 69 72 72 65 6c 65 76 61 6e 74 20 are irrelevant
1d60b 61 73 20 6c 6f 6e 67 20 61 73 0a 2a 2a 20 74 68 as long as.** th
1d60c 65 20 4f 50 5f 43 6f 6c 75 6d 6e 20 6f 70 63 6f e OP_Column opco
1d60d 64 65 20 63 61 6e 20 64 65 63 6f 64 65 20 74 68 de can decode th
1d60e 65 20 72 65 63 6f 72 64 20 6c 61 74 65 72 2e 0a e record later..
1d60f 2a 2a 20 52 65 66 65 72 20 74 6f 20 73 6f 75 72 ** Refer to sour
1d610 63 65 20 63 6f 64 65 20 63 6f 6d 6d 65 6e 74 73 ce code comments
1d611 20 66 6f 72 20 74 68 65 20 64 65 74 61 69 6c 73 for the details
1d612 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64 0a 2a of the record.*
1d613 2a 20 66 6f 72 6d 61 74 2e 0a 2a 2a 0a 2a 2a 20 * format..**.**
1d614 50 34 20 6d 61 79 20 62 65 20 61 20 73 74 72 69 P4 may be a stri
1d615 6e 67 20 74 68 61 74 20 69 73 20 50 32 20 63 68 ng that is P2 ch
1d616 61 72 61 63 74 65 72 73 20 6c 6f 6e 67 2e 20 20 aracters long.
1d617 54 68 65 20 6e 74 68 20 63 68 61 72 61 63 74 65 The nth characte
1d618 72 20 6f 66 20 74 68 65 0a 2a 2a 20 73 74 72 69 r of the.** stri
1d619 6e 67 20 69 6e 64 69 63 61 74 65 73 20 74 68 65 ng indicates the
1d61a 20 63 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 column affinity
1d61b 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 that should be
1d61c 75 73 65 64 20 66 6f 72 20 74 68 65 20 6e 74 68 used for the nth
1d61d 0a 2a 2a 20 66 69 65 6c 64 20 6f 66 20 74 68 65 .** field of the
1d61e 20 69 6e 64 65 78 20 6b 65 79 2e 0a 2a 2a 0a 2a index key..**.*
1d61f 2a 20 54 68 65 20 6d 61 70 70 69 6e 67 20 66 72 * The mapping fr
1d620 6f 6d 20 63 68 61 72 61 63 74 65 72 20 74 6f 20 om character to
1d621 61 66 66 69 6e 69 74 79 20 69 73 20 67 69 76 65 affinity is give
1d622 6e 20 62 79 20 74 68 65 20 53 51 4c 49 54 45 5f n by the SQLITE_
1d623 41 46 46 5f 0a 2a 2a 20 6d 61 63 72 6f 73 20 64 AFF_.** macros d
1d624 65 66 69 6e 65 64 20 69 6e 20 73 71 6c 69 74 65 efined in sqlite
1d625 49 6e 74 2e 68 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 Int.h..**.** If
1d626 50 34 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 P4 is NULL then
1d627 61 6c 6c 20 69 6e 64 65 78 20 66 69 65 6c 64 73 all index fields
1d628 20 68 61 76 65 20 74 68 65 20 61 66 66 69 6e 69 have the affini
1d629 74 79 20 4e 4f 4e 45 2e 0a 2a 2f 0a 63 61 73 65 ty NONE..*/.case
1d62a 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 3a 20 OP_MakeRecord:
1d62b 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 {.#if 0 /* loca
1d62c 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 l variables move
1d62d 64 20 69 6e 74 6f 20 75 2e 61 6f 20 2a 2f 0a 20 d into u.ao */.
1d62e 20 75 38 20 2a 7a 4e 65 77 52 65 63 6f 72 64 3b u8 *zNewRecord;
1d62f 20 20 20 20 20 20 20 20 2f 2a 20 41 20 62 75 66 /* A buf
1d630 66 65 72 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 fer to hold the
1d631 64 61 74 61 20 66 6f 72 20 74 68 65 20 6e 65 77 data for the new
1d632 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 4d 65 6d record */. Mem
1d633 20 2a 70 52 65 63 3b 20 20 20 20 20 20 20 20 20 *pRec;
1d634 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 72 /* The new r
1d635 65 63 6f 72 64 20 2a 2f 0a 20 20 75 36 34 20 6e ecord */. u64 n
1d636 44 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 Data;
1d637 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 /* Number of b
1d638 79 74 65 73 20 6f 66 20 64 61 74 61 20 73 70 61 ytes of data spa
1d639 63 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 48 64 72 ce */. int nHdr
1d63a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
1d63b 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 * Number of byte
1d63c 73 20 6f 66 20 68 65 61 64 65 72 20 73 70 61 63 s of header spac
1d63d 65 20 2a 2f 0a 20 20 69 36 34 20 6e 42 79 74 65 e */. i64 nByte
1d63e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
1d63f 20 44 61 74 61 20 73 70 61 63 65 20 72 65 71 75 Data space requ
1d640 69 72 65 64 20 66 6f 72 20 74 68 69 73 20 72 65 ired for this re
1d641 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 5a cord */. int nZ
1d642 65 72 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20 ero;
1d643 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 7a 65 /* Number of ze
1d644 72 6f 20 62 79 74 65 73 20 61 74 20 74 68 65 20 ro bytes at the
1d645 65 6e 64 20 6f 66 20 74 68 65 20 72 65 63 6f 72 end of the recor
1d646 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 56 61 72 69 d */. int nVari
1d647 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a nt; /*
1d648 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 Number of bytes
1d649 20 69 6e 20 61 20 76 61 72 69 6e 74 20 2a 2f 0a in a varint */.
1d64a 20 20 75 33 32 20 73 65 72 69 61 6c 5f 74 79 70 u32 serial_typ
1d64b 65 3b 20 20 20 20 20 20 20 2f 2a 20 54 79 70 65 e; /* Type
1d64c 20 66 69 65 6c 64 20 2a 2f 0a 20 20 4d 65 6d 20 field */. Mem
1d64d 2a 70 44 61 74 61 30 3b 20 20 20 20 20 20 20 20 *pData0;
1d64e 20 20 20 2f 2a 20 46 69 72 73 74 20 66 69 65 6c /* First fiel
1d64f 64 20 74 6f 20 62 65 20 63 6f 6d 62 69 6e 65 64 d to be combined
1d650 20 69 6e 74 6f 20 74 68 65 20 72 65 63 6f 72 64 into the record
1d651 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4c 61 73 74 */. Mem *pLast
1d652 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
1d653 4c 61 73 74 20 66 69 65 6c 64 20 6f 66 20 74 68 Last field of th
1d654 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e e record */. in
1d655 74 20 6e 46 69 65 6c 64 3b 20 20 20 20 20 20 20 t nField;
1d656 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
1d657 66 20 66 69 65 6c 64 73 20 69 6e 20 74 68 65 20 f fields in the
1d658 72 65 63 6f 72 64 20 2a 2f 0a 20 20 63 68 61 72 record */. char
1d659 20 2a 7a 41 66 66 69 6e 69 74 79 3b 20 20 20 20 *zAffinity;
1d65a 20 20 20 2f 2a 20 54 68 65 20 61 66 66 69 6e 69 /* The affini
1d65b 74 79 20 73 74 72 69 6e 67 20 66 6f 72 20 74 68 ty string for th
1d65c 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e e record */. in
1d65d 74 20 66 69 6c 65 5f 66 6f 72 6d 61 74 3b 20 20 t file_format;
1d65e 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 66 6f 72 /* File for
1d65f 6d 61 74 20 74 6f 20 75 73 65 20 66 6f 72 20 65 mat to use for e
1d660 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 ncoding */. int
1d661 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 i;
1d662 20 20 20 20 2f 2a 20 53 70 61 63 65 20 75 73 65 /* Space use
1d663 64 20 69 6e 20 7a 4e 65 77 52 65 63 6f 72 64 5b d in zNewRecord[
1d664 5d 20 2a 2f 0a 20 20 69 6e 74 20 6c 65 6e 3b 20 ] */. int len;
1d665 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1d666 20 4c 65 6e 67 74 68 20 6f 66 20 61 20 66 69 65 Length of a fie
1d667 6c 64 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 ld */.#endif /*
1d668 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 local variables
1d669 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 6f 20 moved into u.ao
1d66a 2a 2f 0a 0a 20 20 2f 2a 20 41 73 73 75 6d 69 6e */.. /* Assumin
1d66b 67 20 74 68 65 20 72 65 63 6f 72 64 20 63 6f 6e g the record con
1d66c 74 61 69 6e 73 20 4e 20 66 69 65 6c 64 73 2c 20 tains N fields,
1d66d 74 68 65 20 72 65 63 6f 72 64 20 66 6f 72 6d 61 the record forma
1d66e 74 20 6c 6f 6f 6b 73 0a 20 20 2a 2a 20 6c 69 6b t looks. ** lik
1d66f 65 20 74 68 69 73 3a 0a 20 20 2a 2a 0a 20 20 2a e this:. **. *
1d670 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d * --------------
1d671 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1d672 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1d673 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1d674 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 20 20 2a 2a 20 ----------. **
1d675 7c 20 68 64 72 2d 73 69 7a 65 20 7c 20 74 79 70 | hdr-size | typ
1d676 65 20 30 20 7c 20 74 79 70 65 20 31 20 7c 20 2e e 0 | type 1 | .
1d677 2e 2e 20 7c 20 74 79 70 65 20 4e 2d 31 20 7c 20 .. | type N-1 |
1d678 64 61 74 61 30 20 7c 20 2e 2e 2e 20 7c 20 64 61 data0 | ... | da
1d679 74 61 20 4e 2d 31 20 7c 0a 20 20 2a 2a 20 2d 2d ta N-1 |. ** --
1d67a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1d67b 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1d67c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1d67d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1d67e 2d 2d 2d 2d 2d 2d 0a 20 20 2a 2a 0a 20 20 2a 2a ------. **. **
1d67f 20 44 61 74 61 28 30 29 20 69 73 20 74 61 6b 65 Data(0) is take
1d680 6e 20 66 72 6f 6d 20 72 65 67 69 73 74 65 72 20 n from register
1d681 50 31 2e 20 20 44 61 74 61 28 31 29 20 63 6f 6d P1. Data(1) com
1d682 65 73 20 66 72 6f 6d 20 72 65 67 69 73 74 65 72 es from register
1d683 20 50 31 2b 31 0a 20 20 2a 2a 20 61 6e 64 20 73 P1+1. ** and s
1d684 6f 20 66 72 6f 74 68 2e 0a 20 20 2a 2a 0a 20 20 o froth.. **.
1d685 2a 2a 20 45 61 63 68 20 74 79 70 65 20 66 69 65 ** Each type fie
1d686 6c 64 20 69 73 20 61 20 76 61 72 69 6e 74 20 72 ld is a varint r
1d687 65 70 72 65 73 65 6e 74 69 6e 67 20 74 68 65 20 epresenting the
1d688 73 65 72 69 61 6c 20 74 79 70 65 20 6f 66 20 74 serial type of t
1d689 68 65 0a 20 20 2a 2a 20 63 6f 72 72 65 73 70 6f he. ** correspo
1d68a 6e 64 69 6e 67 20 64 61 74 61 20 65 6c 65 6d 65 nding data eleme
1d68b 6e 74 20 28 73 65 65 20 73 71 6c 69 74 65 33 56 nt (see sqlite3V
1d68c 64 62 65 53 65 72 69 61 6c 54 79 70 65 28 29 29 dbeSerialType())
1d68d 2e 20 54 68 65 0a 20 20 2a 2a 20 68 64 72 2d 73 . The. ** hdr-s
1d68e 69 7a 65 20 66 69 65 6c 64 20 69 73 20 61 6c 73 ize field is als
1d68f 6f 20 61 20 76 61 72 69 6e 74 20 77 68 69 63 68 o a varint which
1d690 20 69 73 20 74 68 65 20 6f 66 66 73 65 74 20 66 is the offset f
1d691 72 6f 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e rom the beginnin
1d692 67 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 72 65 g. ** of the re
1d693 63 6f 72 64 20 74 6f 20 64 61 74 61 30 2e 0a 20 cord to data0..
1d694 20 2a 2f 0a 20 20 75 2e 61 6f 2e 6e 44 61 74 61 */. u.ao.nData
1d695 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a = 0; /*
1d696 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 Number of bytes
1d697 20 6f 66 20 64 61 74 61 20 73 70 61 63 65 20 2a of data space *
1d698 2f 0a 20 20 75 2e 61 6f 2e 6e 48 64 72 20 3d 20 /. u.ao.nHdr =
1d699 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 0; /* N
1d69a 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f umber of bytes o
1d69b 66 20 68 65 61 64 65 72 20 73 70 61 63 65 20 2a f header space *
1d69c 2f 0a 20 20 75 2e 61 6f 2e 6e 42 79 74 65 20 3d /. u.ao.nByte =
1d69d 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 44 0; /* D
1d69e 61 74 61 20 73 70 61 63 65 20 72 65 71 75 69 72 ata space requir
1d69f 65 64 20 66 6f 72 20 74 68 69 73 20 72 65 63 6f ed for this reco
1d6a0 72 64 20 2a 2f 0a 20 20 75 2e 61 6f 2e 6e 5a 65 rd */. u.ao.nZe
1d6a1 72 6f 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 ro = 0;
1d6a2 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 7a 65 72 /* Number of zer
1d6a3 6f 20 62 79 74 65 73 20 61 74 20 74 68 65 20 65 o bytes at the e
1d6a4 6e 64 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64 nd of the record
1d6a5 20 2a 2f 0a 20 20 75 2e 61 6f 2e 6e 46 69 65 6c */. u.ao.nFiel
1d6a6 64 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 75 d = pOp->p1;. u
1d6a7 2e 61 6f 2e 7a 41 66 66 69 6e 69 74 79 20 3d 20 .ao.zAffinity =
1d6a8 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 61 73 73 pOp->p4.z;. ass
1d6a9 65 72 74 28 20 75 2e 61 6f 2e 6e 46 69 65 6c 64 ert( u.ao.nField
1d6aa 3e 30 20 26 26 20 70 4f 70 2d 3e 70 32 3e 30 20 >0 && pOp->p2>0
1d6ab 26 26 20 70 4f 70 2d 3e 70 32 2b 75 2e 61 6f 2e && pOp->p2+u.ao.
1d6ac 6e 46 69 65 6c 64 3c 3d 70 2d 3e 6e 4d 65 6d 2b nField<=p->nMem+
1d6ad 31 20 29 3b 0a 20 20 75 2e 61 6f 2e 70 44 61 74 1 );. u.ao.pDat
1d6ae 61 30 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 75 2e a0 = &p->aMem[u.
1d6af 61 6f 2e 6e 46 69 65 6c 64 5d 3b 0a 20 20 75 2e ao.nField];. u.
1d6b0 61 6f 2e 6e 46 69 65 6c 64 20 3d 20 70 4f 70 2d ao.nField = pOp-
1d6b1 3e 70 32 3b 0a 20 20 75 2e 61 6f 2e 70 4c 61 73 >p2;. u.ao.pLas
1d6b2 74 20 3d 20 26 75 2e 61 6f 2e 70 44 61 74 61 30 t = &u.ao.pData0
1d6b3 5b 75 2e 61 6f 2e 6e 46 69 65 6c 64 2d 31 5d 3b [u.ao.nField-1];
1d6b4 0a 20 20 75 2e 61 6f 2e 66 69 6c 65 5f 66 6f 72 . u.ao.file_for
1d6b5 6d 61 74 20 3d 20 70 2d 3e 6d 69 6e 57 72 69 74 mat = p->minWrit
1d6b6 65 46 69 6c 65 46 6f 72 6d 61 74 3b 0a 0a 20 20 eFileFormat;..
1d6b7 2f 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20 /* Loop through
1d6b8 74 68 65 20 65 6c 65 6d 65 6e 74 73 20 74 68 61 the elements tha
1d6b9 74 20 77 69 6c 6c 20 6d 61 6b 65 20 75 70 20 74 t will make up t
1d6ba 68 65 20 72 65 63 6f 72 64 20 74 6f 20 66 69 67 he record to fig
1d6bb 75 72 65 0a 20 20 2a 2a 20 6f 75 74 20 68 6f 77 ure. ** out how
1d6bc 20 6d 75 63 68 20 73 70 61 63 65 20 69 73 20 72 much space is r
1d6bd 65 71 75 69 72 65 64 20 66 6f 72 20 74 68 65 20 equired for the
1d6be 6e 65 77 20 72 65 63 6f 72 64 2e 0a 20 20 2a 2f new record.. */
1d6bf 0a 20 20 66 6f 72 28 75 2e 61 6f 2e 70 52 65 63 . for(u.ao.pRec
1d6c0 3d 75 2e 61 6f 2e 70 44 61 74 61 30 3b 20 75 2e =u.ao.pData0; u.
1d6c1 61 6f 2e 70 52 65 63 3c 3d 75 2e 61 6f 2e 70 4c ao.pRec<=u.ao.pL
1d6c2 61 73 74 3b 20 75 2e 61 6f 2e 70 52 65 63 2b 2b ast; u.ao.pRec++
1d6c3 29 7b 0a 20 20 20 20 69 66 28 20 75 2e 61 6f 2e ){. if( u.ao.
1d6c4 7a 41 66 66 69 6e 69 74 79 20 29 7b 0a 20 20 20 zAffinity ){.
1d6c5 20 20 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79 applyAffinity
1d6c6 28 75 2e 61 6f 2e 70 52 65 63 2c 20 75 2e 61 6f (u.ao.pRec, u.ao
1d6c7 2e 7a 41 66 66 69 6e 69 74 79 5b 75 2e 61 6f 2e .zAffinity[u.ao.
1d6c8 70 52 65 63 2d 75 2e 61 6f 2e 70 44 61 74 61 30 pRec-u.ao.pData0
1d6c9 5d 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 ], encoding);.
1d6ca 20 20 7d 0a 20 20 20 20 69 66 28 20 75 2e 61 6f }. if( u.ao
1d6cb 2e 70 52 65 63 2d 3e 66 6c 61 67 73 26 4d 45 4d .pRec->flags&MEM
1d6cc 5f 5a 65 72 6f 20 26 26 20 75 2e 61 6f 2e 70 52 _Zero && u.ao.pR
1d6cd 65 63 2d 3e 6e 3e 30 20 29 7b 0a 20 20 20 20 20 ec->n>0 ){.
1d6ce 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 45 sqlite3VdbeMemE
1d6cf 78 70 61 6e 64 42 6c 6f 62 28 75 2e 61 6f 2e 70 xpandBlob(u.ao.p
1d6d0 52 65 63 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 Rec);. }.
1d6d1 75 2e 61 6f 2e 73 65 72 69 61 6c 5f 74 79 70 65 u.ao.serial_type
1d6d2 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 = sqlite3VdbeSe
1d6d3 72 69 61 6c 54 79 70 65 28 75 2e 61 6f 2e 70 52 rialType(u.ao.pR
1d6d4 65 63 2c 20 75 2e 61 6f 2e 66 69 6c 65 5f 66 6f ec, u.ao.file_fo
1d6d5 72 6d 61 74 29 3b 0a 20 20 20 20 75 2e 61 6f 2e rmat);. u.ao.
1d6d6 6c 65 6e 20 3d 20 73 71 6c 69 74 65 33 56 64 62 len = sqlite3Vdb
1d6d7 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 75 eSerialTypeLen(u
1d6d8 2e 61 6f 2e 73 65 72 69 61 6c 5f 74 79 70 65 29 .ao.serial_type)
1d6d9 3b 0a 20 20 20 20 75 2e 61 6f 2e 6e 44 61 74 61 ;. u.ao.nData
1d6da 20 2b 3d 20 75 2e 61 6f 2e 6c 65 6e 3b 0a 20 20 += u.ao.len;.
1d6db 20 20 75 2e 61 6f 2e 6e 48 64 72 20 2b 3d 20 73 u.ao.nHdr += s
1d6dc 71 6c 69 74 65 33 56 61 72 69 6e 74 4c 65 6e 28 qlite3VarintLen(
1d6dd 75 2e 61 6f 2e 73 65 72 69 61 6c 5f 74 79 70 65 u.ao.serial_type
1d6de 29 3b 0a 20 20 20 20 69 66 28 20 75 2e 61 6f 2e );. if( u.ao.
1d6df 70 52 65 63 2d 3e 66 6c 61 67 73 20 26 20 4d 45 pRec->flags & ME
1d6e0 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20 20 20 M_Zero ){.
1d6e1 2f 2a 20 4f 6e 6c 79 20 70 75 72 65 20 7a 65 72 /* Only pure zer
1d6e2 6f 2d 66 69 6c 6c 65 64 20 42 4c 4f 42 73 20 63 o-filled BLOBs c
1d6e3 61 6e 20 62 65 20 69 6e 70 75 74 20 74 6f 20 74 an be input to t
1d6e4 68 69 73 20 4f 70 63 6f 64 65 2e 0a 20 20 20 20 his Opcode..
1d6e5 20 20 2a 2a 20 57 65 20 64 6f 20 6e 6f 74 20 61 ** We do not a
1d6e6 6c 6c 6f 77 20 62 6c 6f 62 73 20 77 69 74 68 20 llow blobs with
1d6e7 61 20 70 72 65 66 69 78 20 61 6e 64 20 61 20 7a a prefix and a z
1d6e8 65 72 6f 2d 66 69 6c 6c 65 64 20 74 61 69 6c 2e ero-filled tail.
1d6e9 20 2a 2f 0a 20 20 20 20 20 20 75 2e 61 6f 2e 6e */. u.ao.n
1d6ea 5a 65 72 6f 20 2b 3d 20 75 2e 61 6f 2e 70 52 65 Zero += u.ao.pRe
1d6eb 63 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 20 20 c->u.nZero;.
1d6ec 7d 65 6c 73 65 20 69 66 28 20 75 2e 61 6f 2e 6c }else if( u.ao.l
1d6ed 65 6e 20 29 7b 0a 20 20 20 20 20 20 75 2e 61 6f en ){. u.ao
1d6ee 2e 6e 5a 65 72 6f 20 3d 20 30 3b 0a 20 20 20 20 .nZero = 0;.
1d6ef 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 64 64 20 }. }.. /* Add
1d6f0 74 68 65 20 69 6e 69 74 69 61 6c 20 68 65 61 64 the initial head
1d6f1 65 72 20 76 61 72 69 6e 74 20 61 6e 64 20 74 6f er varint and to
1d6f2 74 61 6c 20 74 68 65 20 73 69 7a 65 20 2a 2f 0a tal the size */.
1d6f3 20 20 75 2e 61 6f 2e 6e 48 64 72 20 2b 3d 20 75 u.ao.nHdr += u
1d6f4 2e 61 6f 2e 6e 56 61 72 69 6e 74 20 3d 20 73 71 .ao.nVarint = sq
1d6f5 6c 69 74 65 33 56 61 72 69 6e 74 4c 65 6e 28 75 lite3VarintLen(u
1d6f6 2e 61 6f 2e 6e 48 64 72 29 3b 0a 20 20 69 66 28 .ao.nHdr);. if(
1d6f7 20 75 2e 61 6f 2e 6e 56 61 72 69 6e 74 3c 73 71 u.ao.nVarint<sq
1d6f8 6c 69 74 65 33 56 61 72 69 6e 74 4c 65 6e 28 75 lite3VarintLen(u
1d6f9 2e 61 6f 2e 6e 48 64 72 29 20 29 7b 0a 20 20 20 .ao.nHdr) ){.
1d6fa 20 75 2e 61 6f 2e 6e 48 64 72 2b 2b 3b 0a 20 20 u.ao.nHdr++;.
1d6fb 7d 0a 20 20 75 2e 61 6f 2e 6e 42 79 74 65 20 3d }. u.ao.nByte =
1d6fc 20 75 2e 61 6f 2e 6e 48 64 72 2b 75 2e 61 6f 2e u.ao.nHdr+u.ao.
1d6fd 6e 44 61 74 61 2d 75 2e 61 6f 2e 6e 5a 65 72 6f nData-u.ao.nZero
1d6fe 3b 0a 20 20 69 66 28 20 75 2e 61 6f 2e 6e 42 79 ;. if( u.ao.nBy
1d6ff 74 65 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 te>db->aLimit[SQ
1d700 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 LITE_LIMIT_LENGT
1d701 48 5d 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74 H] ){. goto t
1d702 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a 0a 20 20 2f oo_big;. }.. /
1d703 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 * Make sure the
1d704 6f 75 74 70 75 74 20 72 65 67 69 73 74 65 72 20 output register
1d705 68 61 73 20 61 20 62 75 66 66 65 72 20 6c 61 72 has a buffer lar
1d706 67 65 20 65 6e 6f 75 67 68 20 74 6f 20 73 74 6f ge enough to sto
1d707 72 65 0a 20 20 2a 2a 20 74 68 65 20 6e 65 77 20 re. ** the new
1d708 72 65 63 6f 72 64 2e 20 54 68 65 20 6f 75 74 70 record. The outp
1d709 75 74 20 72 65 67 69 73 74 65 72 20 28 70 4f 70 ut register (pOp
1d70a 2d 3e 70 33 29 20 69 73 20 6e 6f 74 20 61 6c 6c ->p3) is not all
1d70b 6f 77 65 64 20 74 6f 0a 20 20 2a 2a 20 62 65 20 owed to. ** be
1d70c 6f 6e 65 20 6f 66 20 74 68 65 20 69 6e 70 75 74 one of the input
1d70d 20 72 65 67 69 73 74 65 72 73 20 28 62 65 63 61 registers (beca
1d70e 75 73 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e use the followin
1d70f 67 20 63 61 6c 6c 20 74 6f 0a 20 20 2a 2a 20 73 g call to. ** s
1d710 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f qlite3VdbeMemGro
1d711 77 28 29 20 63 6f 75 6c 64 20 63 6c 6f 62 62 65 w() could clobbe
1d712 72 20 74 68 65 20 76 61 6c 75 65 20 62 65 66 6f r the value befo
1d713 72 65 20 69 74 20 69 73 20 75 73 65 64 29 2e 0a re it is used)..
1d714 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 */. assert( p
1d715 4f 70 2d 3e 70 33 3c 70 4f 70 2d 3e 70 31 20 7c Op->p3<pOp->p1 |
1d716 7c 20 70 4f 70 2d 3e 70 33 3e 3d 70 4f 70 2d 3e | pOp->p3>=pOp->
1d717 70 31 2b 70 4f 70 2d 3e 70 32 20 29 3b 0a 20 20 p1+pOp->p2 );.
1d718 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b pOut = &p->aMem[
1d719 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 69 66 28 20 pOp->p3];. if(
1d71a 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 sqlite3VdbeMemGr
1d71b 6f 77 28 70 4f 75 74 2c 20 28 69 6e 74 29 75 2e ow(pOut, (int)u.
1d71c 61 6f 2e 6e 42 79 74 65 2c 20 30 29 20 29 7b 0a ao.nByte, 0) ){.
1d71d 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b goto no_mem;
1d71e 0a 20 20 7d 0a 20 20 75 2e 61 6f 2e 7a 4e 65 77 . }. u.ao.zNew
1d71f 52 65 63 6f 72 64 20 3d 20 28 75 38 20 2a 29 70 Record = (u8 *)p
1d720 4f 75 74 2d 3e 7a 3b 0a 0a 20 20 2f 2a 20 57 72 Out->z;.. /* Wr
1d721 69 74 65 20 74 68 65 20 72 65 63 6f 72 64 20 2a ite the record *
1d722 2f 0a 20 20 75 2e 61 6f 2e 69 20 3d 20 70 75 74 /. u.ao.i = put
1d723 56 61 72 69 6e 74 33 32 28 75 2e 61 6f 2e 7a 4e Varint32(u.ao.zN
1d724 65 77 52 65 63 6f 72 64 2c 20 75 2e 61 6f 2e 6e ewRecord, u.ao.n
1d725 48 64 72 29 3b 0a 20 20 66 6f 72 28 75 2e 61 6f Hdr);. for(u.ao
1d726 2e 70 52 65 63 3d 75 2e 61 6f 2e 70 44 61 74 61 .pRec=u.ao.pData
1d727 30 3b 20 75 2e 61 6f 2e 70 52 65 63 3c 3d 75 2e 0; u.ao.pRec<=u.
1d728 61 6f 2e 70 4c 61 73 74 3b 20 75 2e 61 6f 2e 70 ao.pLast; u.ao.p
1d729 52 65 63 2b 2b 29 7b 0a 20 20 20 20 75 2e 61 6f Rec++){. u.ao
1d72a 2e 73 65 72 69 61 6c 5f 74 79 70 65 20 3d 20 73 .serial_type = s
1d72b 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c qlite3VdbeSerial
1d72c 54 79 70 65 28 75 2e 61 6f 2e 70 52 65 63 2c 20 Type(u.ao.pRec,
1d72d 75 2e 61 6f 2e 66 69 6c 65 5f 66 6f 72 6d 61 74 u.ao.file_format
1d72e 29 3b 0a 20 20 20 20 75 2e 61 6f 2e 69 20 2b 3d );. u.ao.i +=
1d72f 20 70 75 74 56 61 72 69 6e 74 33 32 28 26 75 2e putVarint32(&u.
1d730 61 6f 2e 7a 4e 65 77 52 65 63 6f 72 64 5b 75 2e ao.zNewRecord[u.
1d731 61 6f 2e 69 5d 2c 20 75 2e 61 6f 2e 73 65 72 69 ao.i], u.ao.seri
1d732 61 6c 5f 74 79 70 65 29 3b 20 20 20 20 20 20 2f al_type); /
1d733 2a 20 73 65 72 69 61 6c 20 74 79 70 65 20 2a 2f * serial type */
1d734 0a 20 20 7d 0a 20 20 66 6f 72 28 75 2e 61 6f 2e . }. for(u.ao.
1d735 70 52 65 63 3d 75 2e 61 6f 2e 70 44 61 74 61 30 pRec=u.ao.pData0
1d736 3b 20 75 2e 61 6f 2e 70 52 65 63 3c 3d 75 2e 61 ; u.ao.pRec<=u.a
1d737 6f 2e 70 4c 61 73 74 3b 20 75 2e 61 6f 2e 70 52 o.pLast; u.ao.pR
1d738 65 63 2b 2b 29 7b 20 20 2f 2a 20 73 65 72 69 61 ec++){ /* seria
1d739 6c 20 64 61 74 61 20 2a 2f 0a 20 20 20 20 75 2e l data */. u.
1d73a 61 6f 2e 69 20 2b 3d 20 73 71 6c 69 74 65 33 56 ao.i += sqlite3V
1d73b 64 62 65 53 65 72 69 61 6c 50 75 74 28 26 75 2e dbeSerialPut(&u.
1d73c 61 6f 2e 7a 4e 65 77 52 65 63 6f 72 64 5b 75 2e ao.zNewRecord[u.
1d73d 61 6f 2e 69 5d 2c 20 28 69 6e 74 29 28 75 2e 61 ao.i], (int)(u.a
1d73e 6f 2e 6e 42 79 74 65 2d 75 2e 61 6f 2e 69 29 2c o.nByte-u.ao.i),
1d73f 20 75 2e 61 6f 2e 70 52 65 63 2c 75 2e 61 6f 2e u.ao.pRec,u.ao.
1d740 66 69 6c 65 5f 66 6f 72 6d 61 74 29 3b 0a 20 20 file_format);.
1d741 7d 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 6f }. assert( u.ao
1d742 2e 69 3d 3d 75 2e 61 6f 2e 6e 42 79 74 65 20 29 .i==u.ao.nByte )
1d743 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 ;.. assert( pOp
1d744 2d 3e 70 33 3e 30 20 26 26 20 70 4f 70 2d 3e 70 ->p3>0 && pOp->p
1d745 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 3<=p->nMem );.
1d746 70 4f 75 74 2d 3e 6e 20 3d 20 28 69 6e 74 29 75 pOut->n = (int)u
1d747 2e 61 6f 2e 6e 42 79 74 65 3b 0a 20 20 70 4f 75 .ao.nByte;. pOu
1d748 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 42 t->flags = MEM_B
1d749 6c 6f 62 20 7c 20 4d 45 4d 5f 44 79 6e 3b 0a 20 lob | MEM_Dyn;.
1d74a 20 70 4f 75 74 2d 3e 78 44 65 6c 20 3d 20 30 3b pOut->xDel = 0;
1d74b 0a 20 20 69 66 28 20 75 2e 61 6f 2e 6e 5a 65 72 . if( u.ao.nZer
1d74c 6f 20 29 7b 0a 20 20 20 20 70 4f 75 74 2d 3e 75 o ){. pOut->u
1d74d 2e 6e 5a 65 72 6f 20 3d 20 75 2e 61 6f 2e 6e 5a .nZero = u.ao.nZ
1d74e 65 72 6f 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 66 ero;. pOut->f
1d74f 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 5a 65 72 6f lags |= MEM_Zero
1d750 3b 0a 20 20 7d 0a 20 20 70 4f 75 74 2d 3e 65 6e ;. }. pOut->en
1d751 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 3b c = SQLITE_UTF8;
1d752 20 20 2f 2a 20 49 6e 20 63 61 73 65 20 74 68 65 /* In case the
1d753 20 62 6c 6f 62 20 69 73 20 65 76 65 72 20 63 6f blob is ever co
1d754 6e 76 65 72 74 65 64 20 74 6f 20 74 65 78 74 20 nverted to text
1d755 2a 2f 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52 */. REGISTER_TR
1d756 41 43 45 28 70 4f 70 2d 3e 70 33 2c 20 70 4f 75 ACE(pOp->p3, pOu
1d757 74 29 3b 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 t);. UPDATE_MAX
1d758 5f 42 4c 4f 42 53 49 5a 45 28 70 4f 75 74 29 3b _BLOBSIZE(pOut);
1d759 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 . break;.}../*
1d75a 4f 70 63 6f 64 65 3a 20 43 6f 75 6e 74 20 50 31 Opcode: Count P1
1d75b 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 P2 * * *.**.**
1d75c 53 74 6f 72 65 20 74 68 65 20 6e 75 6d 62 65 72 Store the number
1d75d 20 6f 66 20 65 6e 74 72 69 65 73 20 28 61 6e 20 of entries (an
1d75e 69 6e 74 65 67 65 72 20 76 61 6c 75 65 29 20 69 integer value) i
1d75f 6e 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 69 n the table or i
1d760 6e 64 65 78 20 0a 2a 2a 20 6f 70 65 6e 65 64 20 ndex .** opened
1d761 62 79 20 63 75 72 73 6f 72 20 50 31 20 69 6e 20 by cursor P1 in
1d762 72 65 67 69 73 74 65 72 20 50 32 0a 2a 2f 0a 23 register P2.*/.#
1d763 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1d764 49 54 5f 42 54 52 45 45 43 4f 55 4e 54 0a 63 61 IT_BTREECOUNT.ca
1d765 73 65 20 4f 50 5f 43 6f 75 6e 74 3a 20 7b 20 20 se OP_Count: {
1d766 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 /* out2-p
1d767 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 23 69 66 rerelease */.#if
1d768 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 0 /* local var
1d769 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 iables moved int
1d76a 6f 20 75 2e 61 70 20 2a 2f 0a 20 20 69 36 34 20 o u.ap */. i64
1d76b 6e 45 6e 74 72 79 3b 0a 20 20 42 74 43 75 72 73 nEntry;. BtCurs
1d76c 6f 72 20 2a 70 43 72 73 72 3b 0a 23 65 6e 64 69 or *pCrsr;.#endi
1d76d 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 f /* local varia
1d76e 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 bles moved into
1d76f 75 2e 61 70 20 2a 2f 0a 0a 20 20 75 2e 61 70 2e u.ap */.. u.ap.
1d770 70 43 72 73 72 20 3d 20 70 2d 3e 61 70 43 73 72 pCrsr = p->apCsr
1d771 5b 70 4f 70 2d 3e 70 31 5d 2d 3e 70 43 75 72 73 [pOp->p1]->pCurs
1d772 6f 72 3b 0a 20 20 69 66 28 20 75 2e 61 70 2e 70 or;. if( u.ap.p
1d773 43 72 73 72 20 29 7b 0a 20 20 20 20 72 63 20 3d Crsr ){. rc =
1d774 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 75 sqlite3BtreeCou
1d775 6e 74 28 75 2e 61 70 2e 70 43 72 73 72 2c 20 26 nt(u.ap.pCrsr, &
1d776 75 2e 61 70 2e 6e 45 6e 74 72 79 29 3b 0a 20 20 u.ap.nEntry);.
1d777 7d 65 6c 73 65 7b 0a 20 20 20 20 75 2e 61 70 2e }else{. u.ap.
1d778 6e 45 6e 74 72 79 20 3d 20 30 3b 0a 20 20 7d 0a nEntry = 0;. }.
1d779 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20 pOut->flags =
1d77a 4d 45 4d 5f 49 6e 74 3b 0a 20 20 70 4f 75 74 2d MEM_Int;. pOut-
1d77b 3e 75 2e 69 20 3d 20 75 2e 61 70 2e 6e 45 6e 74 >u.i = u.ap.nEnt
1d77c 72 79 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 ry;. break;.}.#
1d77d 65 6e 64 69 66 0a 0a 2f 2a 20 4f 70 63 6f 64 65 endif../* Opcode
1d77e 3a 20 53 61 76 65 70 6f 69 6e 74 20 50 31 20 2a : Savepoint P1 *
1d77f 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 4f 70 * P4 *.**.** Op
1d780 65 6e 2c 20 72 65 6c 65 61 73 65 20 6f 72 20 72 en, release or r
1d781 6f 6c 6c 62 61 63 6b 20 74 68 65 20 73 61 76 65 ollback the save
1d782 70 6f 69 6e 74 20 6e 61 6d 65 64 20 62 79 20 70 point named by p
1d783 61 72 61 6d 65 74 65 72 20 50 34 2c 20 64 65 70 arameter P4, dep
1d784 65 6e 64 69 6e 67 0a 2a 2a 20 6f 6e 20 74 68 65 ending.** on the
1d785 20 76 61 6c 75 65 20 6f 66 20 50 31 2e 20 54 6f value of P1. To
1d786 20 6f 70 65 6e 20 61 20 6e 65 77 20 73 61 76 65 open a new save
1d787 70 6f 69 6e 74 2c 20 50 31 3d 3d 30 2e 20 54 6f point, P1==0. To
1d788 20 72 65 6c 65 61 73 65 20 28 63 6f 6d 6d 69 74 release (commit
1d789 29 20 61 6e 0a 2a 2a 20 65 78 69 73 74 69 6e 67 ) an.** existing
1d78a 20 73 61 76 65 70 6f 69 6e 74 2c 20 50 31 3d 3d savepoint, P1==
1d78b 31 2c 20 6f 72 20 74 6f 20 72 6f 6c 6c 62 61 63 1, or to rollbac
1d78c 6b 20 61 6e 20 65 78 69 73 74 69 6e 67 20 73 61 k an existing sa
1d78d 76 65 70 6f 69 6e 74 20 50 31 3d 3d 32 2e 0a 2a vepoint P1==2..*
1d78e 2f 0a 63 61 73 65 20 4f 50 5f 53 61 76 65 70 6f /.case OP_Savepo
1d78f 69 6e 74 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a int: {.#if 0 /*
1d790 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 local variables
1d791 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 71 moved into u.aq
1d792 20 2a 2f 0a 20 20 69 6e 74 20 70 31 3b 20 20 20 */. int p1;
1d793 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d794 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 6f /* Value o
1d795 66 20 50 31 20 6f 70 65 72 61 6e 64 20 2a 2f 0a f P1 operand */.
1d796 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 char *zName;
1d797 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d798 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 73 61 76 /* Name of sav
1d799 65 70 6f 69 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 epoint */. int
1d79a 6e 4e 61 6d 65 3b 0a 20 20 53 61 76 65 70 6f 69 nName;. Savepoi
1d79b 6e 74 20 2a 70 4e 65 77 3b 0a 20 20 53 61 76 65 nt *pNew;. Save
1d79c 70 6f 69 6e 74 20 2a 70 53 61 76 65 70 6f 69 6e point *pSavepoin
1d79d 74 3b 0a 20 20 53 61 76 65 70 6f 69 6e 74 20 2a t;. Savepoint *
1d79e 70 54 6d 70 3b 0a 20 20 69 6e 74 20 69 53 61 76 pTmp;. int iSav
1d79f 65 70 6f 69 6e 74 3b 0a 20 20 69 6e 74 20 69 69 epoint;. int ii
1d7a0 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 ;.#endif /* loca
1d7a1 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 l variables move
1d7a2 64 20 69 6e 74 6f 20 75 2e 61 71 20 2a 2f 0a 0a d into u.aq */..
1d7a3 20 20 75 2e 61 71 2e 70 31 20 3d 20 70 4f 70 2d u.aq.p1 = pOp-
1d7a4 3e 70 31 3b 0a 20 20 75 2e 61 71 2e 7a 4e 61 6d >p1;. u.aq.zNam
1d7a5 65 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 0a e = pOp->p4.z;..
1d7a6 20 20 2f 2a 20 41 73 73 65 72 74 20 74 68 61 74 /* Assert that
1d7a7 20 74 68 65 20 75 2e 61 71 2e 70 31 20 70 61 72 the u.aq.p1 par
1d7a8 61 6d 65 74 65 72 20 69 73 20 76 61 6c 69 64 2e ameter is valid.
1d7a9 20 41 6c 73 6f 20 74 68 61 74 20 69 66 20 74 68 Also that if th
1d7aa 65 72 65 20 69 73 20 6e 6f 20 6f 70 65 6e 0a 20 ere is no open.
1d7ab 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c ** transaction,
1d7ac 20 74 68 65 6e 20 74 68 65 72 65 20 63 61 6e 6e then there cann
1d7ad 6f 74 20 62 65 20 61 6e 79 20 73 61 76 65 70 6f ot be any savepo
1d7ae 69 6e 74 73 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 ints.. */. ass
1d7af 65 72 74 28 20 64 62 2d 3e 70 53 61 76 65 70 6f ert( db->pSavepo
1d7b0 69 6e 74 3d 3d 30 20 7c 7c 20 64 62 2d 3e 61 75 int==0 || db->au
1d7b1 74 6f 43 6f 6d 6d 69 74 3d 3d 30 20 29 3b 0a 20 toCommit==0 );.
1d7b2 20 61 73 73 65 72 74 28 20 75 2e 61 71 2e 70 31 assert( u.aq.p1
1d7b3 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 42 45 47 49 ==SAVEPOINT_BEGI
1d7b4 4e 7c 7c 75 2e 61 71 2e 70 31 3d 3d 53 41 56 45 N||u.aq.p1==SAVE
1d7b5 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 7c 7c 75 POINT_RELEASE||u
1d7b6 2e 61 71 2e 70 31 3d 3d 53 41 56 45 50 4f 49 4e .aq.p1==SAVEPOIN
1d7b7 54 5f 52 4f 4c 4c 42 41 43 4b 20 29 3b 0a 20 20 T_ROLLBACK );.
1d7b8 61 73 73 65 72 74 28 20 64 62 2d 3e 70 53 61 76 assert( db->pSav
1d7b9 65 70 6f 69 6e 74 20 7c 7c 20 64 62 2d 3e 69 73 epoint || db->is
1d7ba 54 72 61 6e 73 61 63 74 69 6f 6e 53 61 76 65 70 TransactionSavep
1d7bb 6f 69 6e 74 3d 3d 30 20 29 3b 0a 20 20 61 73 73 oint==0 );. ass
1d7bc 65 72 74 28 20 63 68 65 63 6b 53 61 76 65 70 6f ert( checkSavepo
1d7bd 69 6e 74 43 6f 75 6e 74 28 64 62 29 20 29 3b 0a intCount(db) );.
1d7be 0a 20 20 69 66 28 20 75 2e 61 71 2e 70 31 3d 3d . if( u.aq.p1==
1d7bf 53 41 56 45 50 4f 49 4e 54 5f 42 45 47 49 4e 20 SAVEPOINT_BEGIN
1d7c0 29 7b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 77 ){. if( db->w
1d7c1 72 69 74 65 56 64 62 65 43 6e 74 3e 30 20 29 7b riteVdbeCnt>0 ){
1d7c2 0a 20 20 20 20 20 20 2f 2a 20 41 20 6e 65 77 20 . /* A new
1d7c3 73 61 76 65 70 6f 69 6e 74 20 63 61 6e 6e 6f 74 savepoint cannot
1d7c4 20 62 65 20 63 72 65 61 74 65 64 20 69 66 20 74 be created if t
1d7c5 68 65 72 65 20 61 72 65 20 61 63 74 69 76 65 20 here are active
1d7c6 77 72 69 74 65 0a 20 20 20 20 20 20 2a 2a 20 73 write. ** s
1d7c7 74 61 74 65 6d 65 6e 74 73 20 28 69 2e 65 2e 20 tatements (i.e.
1d7c8 6f 70 65 6e 20 72 65 61 64 2f 77 72 69 74 65 20 open read/write
1d7c9 69 6e 63 72 65 6d 65 6e 74 61 6c 20 62 6c 6f 62 incremental blob
1d7ca 20 68 61 6e 64 6c 65 73 29 2e 0a 20 20 20 20 20 handles)..
1d7cb 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 */. sqlite
1d7cc 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 3SetString(&p->z
1d7cd 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 63 61 6e ErrMsg, db, "can
1d7ce 6e 6f 74 20 6f 70 65 6e 20 73 61 76 65 70 6f 69 not open savepoi
1d7cf 6e 74 20 2d 20 22 0a 20 20 20 20 20 20 20 20 22 nt - ". "
1d7d0 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 69 SQL statements i
1d7d1 6e 20 70 72 6f 67 72 65 73 73 22 29 3b 0a 20 20 n progress");.
1d7d2 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
1d7d3 42 55 53 59 3b 0a 20 20 20 20 7d 65 6c 73 65 7b BUSY;. }else{
1d7d4 0a 20 20 20 20 20 20 75 2e 61 71 2e 6e 4e 61 6d . u.aq.nNam
1d7d5 65 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 e = sqlite3Strle
1d7d6 6e 33 30 28 75 2e 61 71 2e 7a 4e 61 6d 65 29 3b n30(u.aq.zName);
1d7d7 0a 0a 20 20 20 20 20 20 2f 2a 20 43 72 65 61 74 .. /* Creat
1d7d8 65 20 61 20 6e 65 77 20 73 61 76 65 70 6f 69 6e e a new savepoin
1d7d9 74 20 73 74 72 75 63 74 75 72 65 2e 20 2a 2f 0a t structure. */.
1d7da 20 20 20 20 20 20 75 2e 61 71 2e 70 4e 65 77 20 u.aq.pNew
1d7db 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f = sqlite3DbMallo
1d7dc 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28 cRaw(db, sizeof(
1d7dd 53 61 76 65 70 6f 69 6e 74 29 2b 75 2e 61 71 2e Savepoint)+u.aq.
1d7de 6e 4e 61 6d 65 2b 31 29 3b 0a 20 20 20 20 20 20 nName+1);.
1d7df 69 66 28 20 75 2e 61 71 2e 70 4e 65 77 20 29 7b if( u.aq.pNew ){
1d7e0 0a 20 20 20 20 20 20 20 20 75 2e 61 71 2e 70 4e . u.aq.pN
1d7e1 65 77 2d 3e 7a 4e 61 6d 65 20 3d 20 28 63 68 61 ew->zName = (cha
1d7e2 72 20 2a 29 26 75 2e 61 71 2e 70 4e 65 77 5b 31 r *)&u.aq.pNew[1
1d7e3 5d 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 ];. memcp
1d7e4 79 28 75 2e 61 71 2e 70 4e 65 77 2d 3e 7a 4e 61 y(u.aq.pNew->zNa
1d7e5 6d 65 2c 20 75 2e 61 71 2e 7a 4e 61 6d 65 2c 20 me, u.aq.zName,
1d7e6 75 2e 61 71 2e 6e 4e 61 6d 65 2b 31 29 3b 0a 0a u.aq.nName+1);..
1d7e7 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 /* If th
1d7e8 65 72 65 20 69 73 20 6e 6f 20 6f 70 65 6e 20 74 ere is no open t
1d7e9 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65 6e ransaction, then
1d7ea 20 6d 61 72 6b 20 74 68 69 73 20 61 73 20 61 20 mark this as a
1d7eb 73 70 65 63 69 61 6c 0a 20 20 20 20 20 20 20 20 special.
1d7ec 2a 2a 20 22 74 72 61 6e 73 61 63 74 69 6f 6e 20 ** "transaction
1d7ed 73 61 76 65 70 6f 69 6e 74 22 2e 20 2a 2f 0a 20 savepoint". */.
1d7ee 20 20 20 20 20 20 20 69 66 28 20 64 62 2d 3e 61 if( db->a
1d7ef 75 74 6f 43 6f 6d 6d 69 74 20 29 7b 0a 20 20 20 utoCommit ){.
1d7f0 20 20 20 20 20 20 20 64 62 2d 3e 61 75 74 6f 43 db->autoC
1d7f1 6f 6d 6d 69 74 20 3d 20 30 3b 0a 20 20 20 20 20 ommit = 0;.
1d7f2 20 20 20 20 20 64 62 2d 3e 69 73 54 72 61 6e 73 db->isTrans
1d7f3 61 63 74 69 6f 6e 53 61 76 65 70 6f 69 6e 74 20 actionSavepoint
1d7f4 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c = 1;. }el
1d7f5 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 64 62 se{. db
1d7f6 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 2b 2b 3b 0a ->nSavepoint++;.
1d7f7 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 }..
1d7f8 20 20 20 2f 2a 20 4c 69 6e 6b 20 74 68 65 20 6e /* Link the n
1d7f9 65 77 20 73 61 76 65 70 6f 69 6e 74 20 69 6e 74 ew savepoint int
1d7fa 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68 o the database h
1d7fb 61 6e 64 6c 65 27 73 20 6c 69 73 74 2e 20 2a 2f andle's list. */
1d7fc 0a 20 20 20 20 20 20 20 20 75 2e 61 71 2e 70 4e . u.aq.pN
1d7fd 65 77 2d 3e 70 4e 65 78 74 20 3d 20 64 62 2d 3e ew->pNext = db->
1d7fe 70 53 61 76 65 70 6f 69 6e 74 3b 0a 20 20 20 20 pSavepoint;.
1d7ff 20 20 20 20 64 62 2d 3e 70 53 61 76 65 70 6f 69 db->pSavepoi
1d800 6e 74 20 3d 20 75 2e 61 71 2e 70 4e 65 77 3b 0a nt = u.aq.pNew;.
1d801 20 20 20 20 20 20 20 20 75 2e 61 71 2e 70 4e 65 u.aq.pNe
1d802 77 2d 3e 6e 44 65 66 65 72 72 65 64 43 6f 6e 73 w->nDeferredCons
1d803 20 3d 20 64 62 2d 3e 6e 44 65 66 65 72 72 65 64 = db->nDeferred
1d804 43 6f 6e 73 3b 0a 20 20 20 20 20 20 7d 0a 20 20 Cons;. }.
1d805 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }. }else{.
1d806 20 75 2e 61 71 2e 69 53 61 76 65 70 6f 69 6e 74 u.aq.iSavepoint
1d807 20 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a 20 46 69 = 0;.. /* Fi
1d808 6e 64 20 74 68 65 20 6e 61 6d 65 64 20 73 61 76 nd the named sav
1d809 65 70 6f 69 6e 74 2e 20 49 66 20 74 68 65 72 65 epoint. If there
1d80a 20 69 73 20 6e 6f 20 73 75 63 68 20 73 61 76 65 is no such save
1d80b 70 6f 69 6e 74 2c 20 74 68 65 6e 20 61 6e 0a 20 point, then an.
1d80c 20 20 20 2a 2a 20 61 6e 20 65 72 72 6f 72 20 69 ** an error i
1d80d 73 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 s returned to th
1d80e 65 20 75 73 65 72 2e 20 20 2a 2f 0a 20 20 20 20 e user. */.
1d80f 66 6f 72 28 0a 20 20 20 20 20 20 75 2e 61 71 2e for(. u.aq.
1d810 70 53 61 76 65 70 6f 69 6e 74 20 3d 20 64 62 2d pSavepoint = db-
1d811 3e 70 53 61 76 65 70 6f 69 6e 74 3b 0a 20 20 20 >pSavepoint;.
1d812 20 20 20 75 2e 61 71 2e 70 53 61 76 65 70 6f 69 u.aq.pSavepoi
1d813 6e 74 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 nt && sqlite3Str
1d814 49 43 6d 70 28 75 2e 61 71 2e 70 53 61 76 65 70 ICmp(u.aq.pSavep
1d815 6f 69 6e 74 2d 3e 7a 4e 61 6d 65 2c 20 75 2e 61 oint->zName, u.a
1d816 71 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 q.zName);.
1d817 75 2e 61 71 2e 70 53 61 76 65 70 6f 69 6e 74 20 u.aq.pSavepoint
1d818 3d 20 75 2e 61 71 2e 70 53 61 76 65 70 6f 69 6e = u.aq.pSavepoin
1d819 74 2d 3e 70 4e 65 78 74 0a 20 20 20 20 29 7b 0a t->pNext. ){.
1d81a 20 20 20 20 20 20 75 2e 61 71 2e 69 53 61 76 65 u.aq.iSave
1d81b 70 6f 69 6e 74 2b 2b 3b 0a 20 20 20 20 7d 0a 20 point++;. }.
1d81c 20 20 20 69 66 28 20 21 75 2e 61 71 2e 70 53 61 if( !u.aq.pSa
1d81d 76 65 70 6f 69 6e 74 20 29 7b 0a 20 20 20 20 20 vepoint ){.
1d81e 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e sqlite3SetStrin
1d81f 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 g(&p->zErrMsg, d
1d820 62 2c 20 22 6e 6f 20 73 75 63 68 20 73 61 76 65 b, "no such save
1d821 70 6f 69 6e 74 3a 20 25 73 22 2c 20 75 2e 61 71 point: %s", u.aq
1d822 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 72 .zName);. r
1d823 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 c = SQLITE_ERROR
1d824 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 0a ;. }else if(.
1d825 20 20 20 20 20 20 20 20 64 62 2d 3e 77 72 69 74 db->writ
1d826 65 56 64 62 65 43 6e 74 3e 30 20 7c 7c 20 28 75 eVdbeCnt>0 || (u
1d827 2e 61 71 2e 70 31 3d 3d 53 41 56 45 50 4f 49 4e .aq.p1==SAVEPOIN
1d828 54 5f 52 4f 4c 4c 42 41 43 4b 20 26 26 20 64 62 T_ROLLBACK && db
1d829 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 3e ->activeVdbeCnt>
1d82a 31 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 1). ){.
1d82b 2f 2a 20 49 74 20 69 73 20 6e 6f 74 20 70 6f 73 /* It is not pos
1d82c 73 69 62 6c 65 20 74 6f 20 72 65 6c 65 61 73 65 sible to release
1d82d 20 28 63 6f 6d 6d 69 74 29 20 61 20 73 61 76 65 (commit) a save
1d82e 70 6f 69 6e 74 20 69 66 20 74 68 65 72 65 20 61 point if there a
1d82f 72 65 0a 20 20 20 20 20 20 2a 2a 20 61 63 74 69 re. ** acti
1d830 76 65 20 77 72 69 74 65 20 73 74 61 74 65 6d 65 ve write stateme
1d831 6e 74 73 2e 20 49 74 20 69 73 20 6e 6f 74 20 70 nts. It is not p
1d832 6f 73 73 69 62 6c 65 20 74 6f 20 72 6f 6c 6c 62 ossible to rollb
1d833 61 63 6b 20 61 20 73 61 76 65 70 6f 69 6e 74 0a ack a savepoint.
1d834 20 20 20 20 20 20 2a 2a 20 69 66 20 74 68 65 72 ** if ther
1d835 65 20 61 72 65 20 61 6e 79 20 61 63 74 69 76 65 e are any active
1d836 20 73 74 61 74 65 6d 65 6e 74 73 20 61 74 20 61 statements at a
1d837 6c 6c 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 ll.. */.
1d838 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 sqlite3SetStr
1d839 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c ing(&p->zErrMsg,
1d83a 20 64 62 2c 0a 20 20 20 20 20 20 20 20 22 63 61 db,. "ca
1d83b 6e 6e 6f 74 20 25 73 20 73 61 76 65 70 6f 69 6e nnot %s savepoin
1d83c 74 20 2d 20 53 51 4c 20 73 74 61 74 65 6d 65 6e t - SQL statemen
1d83d 74 73 20 69 6e 20 70 72 6f 67 72 65 73 73 22 2c ts in progress",
1d83e 0a 20 20 20 20 20 20 20 20 28 75 2e 61 71 2e 70 . (u.aq.p
1d83f 31 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 1==SAVEPOINT_ROL
1d840 4c 42 41 43 4b 20 3f 20 22 72 6f 6c 6c 62 61 63 LBACK ? "rollbac
1d841 6b 22 3a 20 22 72 65 6c 65 61 73 65 22 29 0a 20 k": "release").
1d842 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 72 63 );. rc
1d843 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a = SQLITE_BUSY;.
1d844 20 20 20 20 7d 65 6c 73 65 7b 0a 0a 20 20 20 20 }else{..
1d845 20 20 2f 2a 20 44 65 74 65 72 6d 69 6e 65 20 77 /* Determine w
1d846 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 hether or not th
1d847 69 73 20 69 73 20 61 20 74 72 61 6e 73 61 63 74 is is a transact
1d848 69 6f 6e 20 73 61 76 65 70 6f 69 6e 74 2e 20 49 ion savepoint. I
1d849 66 20 73 6f 2c 0a 20 20 20 20 20 20 2a 2a 20 61 f so,. ** a
1d84a 6e 64 20 74 68 69 73 20 69 73 20 61 20 52 45 4c nd this is a REL
1d84b 45 41 53 45 20 63 6f 6d 6d 61 6e 64 2c 20 74 68 EASE command, th
1d84c 65 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 en the current t
1d84d 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20 20 20 20 ransaction.
1d84e 20 2a 2a 20 69 73 20 63 6f 6d 6d 69 74 74 65 64 ** is committed
1d84f 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 .. */.
1d850 20 69 6e 74 20 69 73 54 72 61 6e 73 61 63 74 69 int isTransacti
1d851 6f 6e 20 3d 20 75 2e 61 71 2e 70 53 61 76 65 70 on = u.aq.pSavep
1d852 6f 69 6e 74 2d 3e 70 4e 65 78 74 3d 3d 30 20 26 oint->pNext==0 &
1d853 26 20 64 62 2d 3e 69 73 54 72 61 6e 73 61 63 74 & db->isTransact
1d854 69 6f 6e 53 61 76 65 70 6f 69 6e 74 3b 0a 20 20 ionSavepoint;.
1d855 20 20 20 20 69 66 28 20 69 73 54 72 61 6e 73 61 if( isTransa
1d856 63 74 69 6f 6e 20 26 26 20 75 2e 61 71 2e 70 31 ction && u.aq.p1
1d857 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 ==SAVEPOINT_RELE
1d858 41 53 45 20 29 7b 0a 20 20 20 20 20 20 20 20 69 ASE ){. i
1d859 66 28 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 f( (rc = sqlite3
1d85a 56 64 62 65 43 68 65 63 6b 46 6b 28 70 2c 20 31 VdbeCheckFk(p, 1
1d85b 29 29 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b ))!=SQLITE_OK ){
1d85c 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 . goto
1d85d 76 64 62 65 5f 72 65 74 75 72 6e 3b 0a 20 20 20 vdbe_return;.
1d85e 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 64 }. d
1d85f 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 b->autoCommit =
1d860 31 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 1;. if( s
1d861 71 6c 69 74 65 33 56 64 62 65 48 61 6c 74 28 70 qlite3VdbeHalt(p
1d862 29 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 29 )==SQLITE_BUSY )
1d863 7b 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70 {. p->p
1d864 63 20 3d 20 70 63 3b 0a 20 20 20 20 20 20 20 20 c = pc;.
1d865 20 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 db->autoCommit
1d866 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 = 0;.
1d867 70 2d 3e 72 63 20 3d 20 72 63 20 3d 20 53 51 4c p->rc = rc = SQL
1d868 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 20 20 ITE_BUSY;.
1d869 20 20 20 20 67 6f 74 6f 20 76 64 62 65 5f 72 65 goto vdbe_re
1d86a 74 75 72 6e 3b 0a 20 20 20 20 20 20 20 20 7d 0a turn;. }.
1d86b 20 20 20 20 20 20 20 20 64 62 2d 3e 69 73 54 72 db->isTr
1d86c 61 6e 73 61 63 74 69 6f 6e 53 61 76 65 70 6f 69 ansactionSavepoi
1d86d 6e 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 nt = 0;.
1d86e 72 63 20 3d 20 70 2d 3e 72 63 3b 0a 20 20 20 20 rc = p->rc;.
1d86f 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
1d870 20 75 2e 61 71 2e 69 53 61 76 65 70 6f 69 6e 74 u.aq.iSavepoint
1d871 20 3d 20 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e = db->nSavepoin
1d872 74 20 2d 20 75 2e 61 71 2e 69 53 61 76 65 70 6f t - u.aq.iSavepo
1d873 69 6e 74 20 2d 20 31 3b 0a 20 20 20 20 20 20 20 int - 1;.
1d874 20 66 6f 72 28 75 2e 61 71 2e 69 69 3d 30 3b 20 for(u.aq.ii=0;
1d875 75 2e 61 71 2e 69 69 3c 64 62 2d 3e 6e 44 62 3b u.aq.ii<db->nDb;
1d876 20 75 2e 61 71 2e 69 69 2b 2b 29 7b 0a 20 20 20 u.aq.ii++){.
1d877 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
1d878 74 65 33 42 74 72 65 65 53 61 76 65 70 6f 69 6e te3BtreeSavepoin
1d879 74 28 64 62 2d 3e 61 44 62 5b 75 2e 61 71 2e 69 t(db->aDb[u.aq.i
1d87a 69 5d 2e 70 42 74 2c 20 75 2e 61 71 2e 70 31 2c i].pBt, u.aq.p1,
1d87b 20 75 2e 61 71 2e 69 53 61 76 65 70 6f 69 6e 74 u.aq.iSavepoint
1d87c 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 );. if(
1d87d 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
1d87e 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 67 6f {. go
1d87f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f to abort_due_to_
1d880 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 20 20 20 error;.
1d881 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
1d882 20 20 20 20 20 69 66 28 20 75 2e 61 71 2e 70 31 if( u.aq.p1
1d883 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c ==SAVEPOINT_ROLL
1d884 42 41 43 4b 20 26 26 20 28 64 62 2d 3e 66 6c 61 BACK && (db->fla
1d885 67 73 26 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e gs&SQLITE_Intern
1d886 43 68 61 6e 67 65 73 29 21 3d 30 20 29 7b 0a 20 Changes)!=0 ){.
1d887 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
1d888 45 78 70 69 72 65 50 72 65 70 61 72 65 64 53 74 ExpirePreparedSt
1d889 61 74 65 6d 65 6e 74 73 28 64 62 29 3b 0a 20 20 atements(db);.
1d88a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 sqlite3R
1d88b 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 esetInternalSche
1d88c 6d 61 28 64 62 2c 20 30 29 3b 0a 20 20 20 20 20 ma(db, 0);.
1d88d 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 20 20 }. }..
1d88e 20 20 20 20 2f 2a 20 52 65 67 61 72 64 6c 65 73 /* Regardles
1d88f 73 20 6f 66 20 77 68 65 74 68 65 72 20 74 68 69 s of whether thi
1d890 73 20 69 73 20 61 20 52 45 4c 45 41 53 45 20 6f s is a RELEASE o
1d891 72 20 52 4f 4c 4c 42 41 43 4b 2c 20 64 65 73 74 r ROLLBACK, dest
1d892 72 6f 79 20 61 6c 6c 0a 20 20 20 20 20 20 2a 2a roy all. **
1d893 20 73 61 76 65 70 6f 69 6e 74 73 20 6e 65 73 74 savepoints nest
1d894 65 64 20 69 6e 73 69 64 65 20 6f 66 20 74 68 65 ed inside of the
1d895 20 73 61 76 65 70 6f 69 6e 74 20 62 65 69 6e 67 savepoint being
1d896 20 6f 70 65 72 61 74 65 64 20 6f 6e 2e 20 2a 2f operated on. */
1d897 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 64 62 . while( db
1d898 2d 3e 70 53 61 76 65 70 6f 69 6e 74 21 3d 75 2e ->pSavepoint!=u.
1d899 61 71 2e 70 53 61 76 65 70 6f 69 6e 74 20 29 7b aq.pSavepoint ){
1d89a 0a 20 20 20 20 20 20 20 20 75 2e 61 71 2e 70 54 . u.aq.pT
1d89b 6d 70 20 3d 20 64 62 2d 3e 70 53 61 76 65 70 6f mp = db->pSavepo
1d89c 69 6e 74 3b 0a 20 20 20 20 20 20 20 20 64 62 2d int;. db-
1d89d 3e 70 53 61 76 65 70 6f 69 6e 74 20 3d 20 75 2e >pSavepoint = u.
1d89e 61 71 2e 70 54 6d 70 2d 3e 70 4e 65 78 74 3b 0a aq.pTmp->pNext;.
1d89f 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 sqlite3D
1d8a0 62 46 72 65 65 28 64 62 2c 20 75 2e 61 71 2e 70 bFree(db, u.aq.p
1d8a1 54 6d 70 29 3b 0a 20 20 20 20 20 20 20 20 64 62 Tmp);. db
1d8a2 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 2d 2d 3b 0a ->nSavepoint--;.
1d8a3 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f }.. /
1d8a4 2a 20 49 66 20 69 74 20 69 73 20 61 20 52 45 4c * If it is a REL
1d8a5 45 41 53 45 2c 20 74 68 65 6e 20 64 65 73 74 72 EASE, then destr
1d8a6 6f 79 20 74 68 65 20 73 61 76 65 70 6f 69 6e 74 oy the savepoint
1d8a7 20 62 65 69 6e 67 20 6f 70 65 72 61 74 65 64 20 being operated
1d8a8 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 74 6f 6f 2e on. ** too.
1d8a9 20 49 66 20 69 74 20 69 73 20 61 20 52 4f 4c 4c If it is a ROLL
1d8aa 42 41 43 4b 20 54 4f 2c 20 74 68 65 6e 20 73 65 BACK TO, then se
1d8ab 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 t the number of
1d8ac 64 65 66 65 72 72 65 64 0a 20 20 20 20 20 20 2a deferred. *
1d8ad 2a 20 63 6f 6e 73 74 72 61 69 6e 74 20 76 69 6f * constraint vio
1d8ae 6c 61 74 69 6f 6e 73 20 70 72 65 73 65 6e 74 20 lations present
1d8af 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 in the database
1d8b0 74 6f 20 74 68 65 20 76 61 6c 75 65 20 73 74 6f to the value sto
1d8b1 72 65 64 0a 20 20 20 20 20 20 2a 2a 20 77 68 65 red. ** whe
1d8b2 6e 20 74 68 65 20 73 61 76 65 70 6f 69 6e 74 20 n the savepoint
1d8b3 77 61 73 20 63 72 65 61 74 65 64 2e 20 20 2a 2f was created. */
1d8b4 0a 20 20 20 20 20 20 69 66 28 20 75 2e 61 71 2e . if( u.aq.
1d8b5 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 45 p1==SAVEPOINT_RE
1d8b6 4c 45 41 53 45 20 29 7b 0a 20 20 20 20 20 20 20 LEASE ){.
1d8b7 20 61 73 73 65 72 74 28 20 75 2e 61 71 2e 70 53 assert( u.aq.pS
1d8b8 61 76 65 70 6f 69 6e 74 3d 3d 64 62 2d 3e 70 53 avepoint==db->pS
1d8b9 61 76 65 70 6f 69 6e 74 20 29 3b 0a 20 20 20 20 avepoint );.
1d8ba 20 20 20 20 64 62 2d 3e 70 53 61 76 65 70 6f 69 db->pSavepoi
1d8bb 6e 74 20 3d 20 75 2e 61 71 2e 70 53 61 76 65 70 nt = u.aq.pSavep
1d8bc 6f 69 6e 74 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 oint->pNext;.
1d8bd 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 sqlite3DbFr
1d8be 65 65 28 64 62 2c 20 75 2e 61 71 2e 70 53 61 76 ee(db, u.aq.pSav
1d8bf 65 70 6f 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 epoint);.
1d8c0 20 69 66 28 20 21 69 73 54 72 61 6e 73 61 63 74 if( !isTransact
1d8c1 69 6f 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ion ){.
1d8c2 20 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 2d db->nSavepoint-
1d8c3 2d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 -;. }.
1d8c4 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1d8c5 20 20 64 62 2d 3e 6e 44 65 66 65 72 72 65 64 43 db->nDeferredC
1d8c6 6f 6e 73 20 3d 20 75 2e 61 71 2e 70 53 61 76 65 ons = u.aq.pSave
1d8c7 70 6f 69 6e 74 2d 3e 6e 44 65 66 65 72 72 65 64 point->nDeferred
1d8c8 43 6f 6e 73 3b 0a 20 20 20 20 20 20 7d 0a 20 20 Cons;. }.
1d8c9 20 20 7d 0a 20 20 7d 0a 0a 20 20 62 72 65 61 6b }. }.. break
1d8ca 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ;.}../* Opcode:
1d8cb 41 75 74 6f 43 6f 6d 6d 69 74 20 50 31 20 50 32 AutoCommit P1 P2
1d8cc 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 65 74 * * *.**.** Set
1d8cd 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61 75 the database au
1d8ce 74 6f 2d 63 6f 6d 6d 69 74 20 66 6c 61 67 20 74 to-commit flag t
1d8cf 6f 20 50 31 20 28 31 20 6f 72 20 30 29 2e 20 49 o P1 (1 or 0). I
1d8d0 66 20 50 32 20 69 73 20 74 72 75 65 2c 20 72 6f f P2 is true, ro
1d8d1 6c 6c 0a 2a 2a 20 62 61 63 6b 20 61 6e 79 20 63 ll.** back any c
1d8d2 75 72 72 65 6e 74 6c 79 20 61 63 74 69 76 65 20 urrently active
1d8d3 62 74 72 65 65 20 74 72 61 6e 73 61 63 74 69 6f btree transactio
1d8d4 6e 73 2e 20 49 66 20 74 68 65 72 65 20 61 72 65 ns. If there are
1d8d5 20 61 6e 79 20 61 63 74 69 76 65 0a 2a 2a 20 56 any active.** V
1d8d6 4d 73 20 28 61 70 61 72 74 20 66 72 6f 6d 20 74 Ms (apart from t
1d8d7 68 69 73 20 6f 6e 65 29 2c 20 74 68 65 6e 20 61 his one), then a
1d8d8 20 52 4f 4c 4c 42 41 43 4b 20 66 61 69 6c 73 2e ROLLBACK fails.
1d8d9 20 20 41 20 43 4f 4d 4d 49 54 20 66 61 69 6c 73 A COMMIT fails
1d8da 20 69 66 0a 2a 2a 20 74 68 65 72 65 20 61 72 65 if.** there are
1d8db 20 61 63 74 69 76 65 20 77 72 69 74 69 6e 67 20 active writing
1d8dc 56 4d 73 20 6f 72 20 61 63 74 69 76 65 20 56 4d VMs or active VM
1d8dd 73 20 74 68 61 74 20 75 73 65 20 73 68 61 72 65 s that use share
1d8de 64 20 63 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 54 d cache..**.** T
1d8df 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 his instruction
1d8e0 63 61 75 73 65 73 20 74 68 65 20 56 4d 20 74 6f causes the VM to
1d8e1 20 68 61 6c 74 2e 0a 2a 2f 0a 63 61 73 65 20 4f halt..*/.case O
1d8e2 50 5f 41 75 74 6f 43 6f 6d 6d 69 74 3a 20 7b 0a P_AutoCommit: {.
1d8e3 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 #if 0 /* local
1d8e4 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 variables moved
1d8e5 69 6e 74 6f 20 75 2e 61 72 20 2a 2f 0a 20 20 69 into u.ar */. i
1d8e6 6e 74 20 64 65 73 69 72 65 64 41 75 74 6f 43 6f nt desiredAutoCo
1d8e7 6d 6d 69 74 3b 0a 20 20 69 6e 74 20 69 52 6f 6c mmit;. int iRol
1d8e8 6c 62 61 63 6b 3b 0a 20 20 69 6e 74 20 74 75 72 lback;. int tur
1d8e9 6e 4f 6e 41 43 3b 0a 23 65 6e 64 69 66 20 2f 2a nOnAC;.#endif /*
1d8ea 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 local variables
1d8eb 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 72 moved into u.ar
1d8ec 20 2a 2f 0a 0a 20 20 75 2e 61 72 2e 64 65 73 69 */.. u.ar.desi
1d8ed 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 redAutoCommit =
1d8ee 70 4f 70 2d 3e 70 31 3b 0a 20 20 75 2e 61 72 2e pOp->p1;. u.ar.
1d8ef 69 52 6f 6c 6c 62 61 63 6b 20 3d 20 70 4f 70 2d iRollback = pOp-
1d8f0 3e 70 32 3b 0a 20 20 75 2e 61 72 2e 74 75 72 6e >p2;. u.ar.turn
1d8f1 4f 6e 41 43 20 3d 20 75 2e 61 72 2e 64 65 73 69 OnAC = u.ar.desi
1d8f2 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 20 26 26 redAutoCommit &&
1d8f3 20 21 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 !db->autoCommit
1d8f4 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 72 ;. assert( u.ar
1d8f5 2e 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d .desiredAutoComm
1d8f6 69 74 3d 3d 31 20 7c 7c 20 75 2e 61 72 2e 64 65 it==1 || u.ar.de
1d8f7 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 3d siredAutoCommit=
1d8f8 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
1d8f9 75 2e 61 72 2e 64 65 73 69 72 65 64 41 75 74 6f u.ar.desiredAuto
1d8fa 43 6f 6d 6d 69 74 3d 3d 31 20 7c 7c 20 75 2e 61 Commit==1 || u.a
1d8fb 72 2e 69 52 6f 6c 6c 62 61 63 6b 3d 3d 30 20 29 r.iRollback==0 )
1d8fc 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e ;. assert( db->
1d8fd 61 63 74 69 76 65 56 64 62 65 43 6e 74 3e 30 20 activeVdbeCnt>0
1d8fe 29 3b 20 20 2f 2a 20 41 74 20 6c 65 61 73 74 20 ); /* At least
1d8ff 74 68 69 73 20 6f 6e 65 20 56 4d 20 69 73 20 61 this one VM is a
1d900 63 74 69 76 65 20 2a 2f 0a 0a 20 20 69 66 28 20 ctive */.. if(
1d901 75 2e 61 72 2e 74 75 72 6e 4f 6e 41 43 20 26 26 u.ar.turnOnAC &&
1d902 20 75 2e 61 72 2e 69 52 6f 6c 6c 62 61 63 6b 20 u.ar.iRollback
1d903 26 26 20 64 62 2d 3e 61 63 74 69 76 65 56 64 62 && db->activeVdb
1d904 65 43 6e 74 3e 31 20 29 7b 0a 20 20 20 20 2f 2a eCnt>1 ){. /*
1d905 20 49 66 20 74 68 69 73 20 69 6e 73 74 72 75 63 If this instruc
1d906 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 73 20 tion implements
1d907 61 20 52 4f 4c 4c 42 41 43 4b 20 61 6e 64 20 6f a ROLLBACK and o
1d908 74 68 65 72 20 56 4d 73 20 61 72 65 0a 20 20 20 ther VMs are.
1d909 20 2a 2a 20 73 74 69 6c 6c 20 72 75 6e 6e 69 6e ** still runnin
1d90a 67 2c 20 61 6e 64 20 61 20 74 72 61 6e 73 61 63 g, and a transac
1d90b 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65 2c 20 tion is active,
1d90c 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 return an error
1d90d 69 6e 64 69 63 61 74 69 6e 67 0a 20 20 20 20 2a indicating. *
1d90e 2a 20 74 68 61 74 20 74 68 65 20 6f 74 68 65 72 * that the other
1d90f 20 56 4d 73 20 6d 75 73 74 20 63 6f 6d 70 6c 65 VMs must comple
1d910 74 65 20 66 69 72 73 74 2e 0a 20 20 20 20 2a 2f te first.. */
1d911 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 . sqlite3SetS
1d912 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 tring(&p->zErrMs
1d913 67 2c 20 64 62 2c 20 22 63 61 6e 6e 6f 74 20 72 g, db, "cannot r
1d914 6f 6c 6c 62 61 63 6b 20 74 72 61 6e 73 61 63 74 ollback transact
1d915 69 6f 6e 20 2d 20 22 0a 20 20 20 20 20 20 20 20 ion - ".
1d916 22 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 "SQL statements
1d917 69 6e 20 70 72 6f 67 72 65 73 73 22 29 3b 0a 20 in progress");.
1d918 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 rc = SQLITE_B
1d919 55 53 59 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 USY;. }else if(
1d91a 20 75 2e 61 72 2e 74 75 72 6e 4f 6e 41 43 20 26 u.ar.turnOnAC &
1d91b 26 20 21 75 2e 61 72 2e 69 52 6f 6c 6c 62 61 63 & !u.ar.iRollbac
1d91c 6b 20 26 26 20 64 62 2d 3e 77 72 69 74 65 56 64 k && db->writeVd
1d91d 62 65 43 6e 74 3e 30 20 29 7b 0a 20 20 20 20 2f beCnt>0 ){. /
1d91e 2a 20 49 66 20 74 68 69 73 20 69 6e 73 74 72 75 * If this instru
1d91f 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 73 ction implements
1d920 20 61 20 43 4f 4d 4d 49 54 20 61 6e 64 20 6f 74 a COMMIT and ot
1d921 68 65 72 20 56 4d 73 20 61 72 65 20 77 72 69 74 her VMs are writ
1d922 69 6e 67 0a 20 20 20 20 2a 2a 20 72 65 74 75 72 ing. ** retur
1d923 6e 20 61 6e 20 65 72 72 6f 72 20 69 6e 64 69 63 n an error indic
1d924 61 74 69 6e 67 20 74 68 61 74 20 74 68 65 20 6f ating that the o
1d925 74 68 65 72 20 56 4d 73 20 6d 75 73 74 20 63 6f ther VMs must co
1d926 6d 70 6c 65 74 65 20 66 69 72 73 74 2e 0a 20 20 mplete first..
1d927 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 */. sqlite3
1d928 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 SetString(&p->zE
1d929 72 72 4d 73 67 2c 20 64 62 2c 20 22 63 61 6e 6e rrMsg, db, "cann
1d92a 6f 74 20 63 6f 6d 6d 69 74 20 74 72 61 6e 73 61 ot commit transa
1d92b 63 74 69 6f 6e 20 2d 20 22 0a 20 20 20 20 20 20 ction - ".
1d92c 20 20 22 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 "SQL statement
1d92d 73 20 69 6e 20 70 72 6f 67 72 65 73 73 22 29 3b s in progress");
1d92e 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 . rc = SQLITE
1d92f 5f 42 55 53 59 3b 0a 20 20 7d 65 6c 73 65 20 69 _BUSY;. }else i
1d930 66 28 20 75 2e 61 72 2e 64 65 73 69 72 65 64 41 f( u.ar.desiredA
1d931 75 74 6f 43 6f 6d 6d 69 74 21 3d 64 62 2d 3e 61 utoCommit!=db->a
1d932 75 74 6f 43 6f 6d 6d 69 74 20 29 7b 0a 20 20 20 utoCommit ){.
1d933 20 69 66 28 20 75 2e 61 72 2e 69 52 6f 6c 6c 62 if( u.ar.iRollb
1d934 61 63 6b 20 29 7b 0a 20 20 20 20 20 20 61 73 73 ack ){. ass
1d935 65 72 74 28 20 75 2e 61 72 2e 64 65 73 69 72 65 ert( u.ar.desire
1d936 64 41 75 74 6f 43 6f 6d 6d 69 74 3d 3d 31 20 29 dAutoCommit==1 )
1d937 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 ;. sqlite3R
1d938 6f 6c 6c 62 61 63 6b 41 6c 6c 28 64 62 29 3b 0a ollbackAll(db);.
1d939 20 20 20 20 20 20 64 62 2d 3e 61 75 74 6f 43 6f db->autoCo
1d93a 6d 6d 69 74 20 3d 20 31 3b 0a 20 20 20 20 7d 65 mmit = 1;. }e
1d93b 6c 73 65 20 69 66 28 20 28 72 63 20 3d 20 73 71 lse if( (rc = sq
1d93c 6c 69 74 65 33 56 64 62 65 43 68 65 63 6b 46 6b lite3VdbeCheckFk
1d93d 28 70 2c 20 31 29 29 21 3d 53 51 4c 49 54 45 5f (p, 1))!=SQLITE_
1d93e 4f 4b 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f OK ){. goto
1d93f 20 76 64 62 65 5f 72 65 74 75 72 6e 3b 0a 20 20 vdbe_return;.
1d940 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 64 }else{. d
1d941 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 b->autoCommit =
1d942 28 75 38 29 75 2e 61 72 2e 64 65 73 69 72 65 64 (u8)u.ar.desired
1d943 41 75 74 6f 43 6f 6d 6d 69 74 3b 0a 20 20 20 20 AutoCommit;.
1d944 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 if( sqlite3Vdb
1d945 65 48 61 6c 74 28 70 29 3d 3d 53 51 4c 49 54 45 eHalt(p)==SQLITE
1d946 5f 42 55 53 59 20 29 7b 0a 20 20 20 20 20 20 20 _BUSY ){.
1d947 20 70 2d 3e 70 63 20 3d 20 70 63 3b 0a 20 20 20 p->pc = pc;.
1d948 20 20 20 20 20 64 62 2d 3e 61 75 74 6f 43 6f 6d db->autoCom
1d949 6d 69 74 20 3d 20 28 75 38 29 28 31 2d 75 2e 61 mit = (u8)(1-u.a
1d94a 72 2e 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d r.desiredAutoCom
1d94b 6d 69 74 29 3b 0a 20 20 20 20 20 20 20 20 70 2d mit);. p-
1d94c 3e 72 63 20 3d 20 72 63 20 3d 20 53 51 4c 49 54 >rc = rc = SQLIT
1d94d 45 5f 42 55 53 59 3b 0a 20 20 20 20 20 20 20 20 E_BUSY;.
1d94e 67 6f 74 6f 20 76 64 62 65 5f 72 65 74 75 72 6e goto vdbe_return
1d94f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
1d950 20 20 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e assert( db->
1d951 6e 53 74 61 74 65 6d 65 6e 74 3d 3d 30 20 29 3b nStatement==0 );
1d952 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6c 6f 73 . sqlite3Clos
1d953 65 53 61 76 65 70 6f 69 6e 74 73 28 64 62 29 3b eSavepoints(db);
1d954 0a 20 20 20 20 69 66 28 20 70 2d 3e 72 63 3d 3d . if( p->rc==
1d955 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
1d956 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 44 rc = SQLITE_D
1d957 4f 4e 45 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a ONE;. }else{.
1d958 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 rc = SQLIT
1d959 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7d 0a 20 E_ERROR;. }.
1d95a 20 20 20 67 6f 74 6f 20 76 64 62 65 5f 72 65 74 goto vdbe_ret
1d95b 75 72 6e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 urn;. }else{.
1d95c 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 sqlite3SetStri
1d95d 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 ng(&p->zErrMsg,
1d95e 64 62 2c 0a 20 20 20 20 20 20 20 20 28 21 75 2e db,. (!u.
1d95f 61 72 2e 64 65 73 69 72 65 64 41 75 74 6f 43 6f ar.desiredAutoCo
1d960 6d 6d 69 74 29 3f 22 63 61 6e 6e 6f 74 20 73 74 mmit)?"cannot st
1d961 61 72 74 20 61 20 74 72 61 6e 73 61 63 74 69 6f art a transactio
1d962 6e 20 77 69 74 68 69 6e 20 61 20 74 72 61 6e 73 n within a trans
1d963 61 63 74 69 6f 6e 22 3a 28 0a 20 20 20 20 20 20 action":(.
1d964 20 20 28 75 2e 61 72 2e 69 52 6f 6c 6c 62 61 63 (u.ar.iRollbac
1d965 6b 29 3f 22 63 61 6e 6e 6f 74 20 72 6f 6c 6c 62 k)?"cannot rollb
1d966 61 63 6b 20 2d 20 6e 6f 20 74 72 61 6e 73 61 63 ack - no transac
1d967 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65 22 3a tion is active":
1d968 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1d969 20 20 20 20 22 63 61 6e 6e 6f 74 20 63 6f 6d 6d "cannot comm
1d96a 69 74 20 2d 20 6e 6f 20 74 72 61 6e 73 61 63 74 it - no transact
1d96b 69 6f 6e 20 69 73 20 61 63 74 69 76 65 22 29 29 ion is active"))
1d96c 3b 0a 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 ;.. rc = SQLI
1d96d 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 TE_ERROR;. }.
1d96e 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 break;.}../* Opc
1d96f 6f 64 65 3a 20 54 72 61 6e 73 61 63 74 69 6f 6e ode: Transaction
1d970 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a P1 P2 * * *.**.
1d971 2a 2a 20 42 65 67 69 6e 20 61 20 74 72 61 6e 73 ** Begin a trans
1d972 61 63 74 69 6f 6e 2e 20 20 54 68 65 20 74 72 61 action. The tra
1d973 6e 73 61 63 74 69 6f 6e 20 65 6e 64 73 20 77 68 nsaction ends wh
1d974 65 6e 20 61 20 43 6f 6d 6d 69 74 20 6f 72 20 52 en a Commit or R
1d975 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 6f 70 63 6f 64 ollback.** opcod
1d976 65 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 e is encountered
1d977 2e 20 20 44 65 70 65 6e 64 69 6e 67 20 6f 6e 20 . Depending on
1d978 74 68 65 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 the ON CONFLICT
1d979 73 65 74 74 69 6e 67 2c 20 74 68 65 0a 2a 2a 20 setting, the.**
1d97a 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 69 67 68 transaction migh
1d97b 74 20 61 6c 73 6f 20 62 65 20 72 6f 6c 6c 65 64 t also be rolled
1d97c 20 62 61 63 6b 20 69 66 20 61 6e 20 65 72 72 6f back if an erro
1d97d 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 r is encountered
1d97e 2e 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 74 68 ..**.** P1 is th
1d97f 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 64 e index of the d
1d980 61 74 61 62 61 73 65 20 66 69 6c 65 20 6f 6e 20 atabase file on
1d981 77 68 69 63 68 20 74 68 65 20 74 72 61 6e 73 61 which the transa
1d982 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 73 74 61 72 ction is.** star
1d983 74 65 64 2e 20 20 49 6e 64 65 78 20 30 20 69 73 ted. Index 0 is
1d984 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 the main databa
1d985 73 65 20 66 69 6c 65 20 61 6e 64 20 69 6e 64 65 se file and inde
1d986 78 20 31 20 69 73 20 74 68 65 0a 2a 2a 20 66 69 x 1 is the.** fi
1d987 6c 65 20 75 73 65 64 20 66 6f 72 20 74 65 6d 70 le used for temp
1d988 6f 72 61 72 79 20 74 61 62 6c 65 73 2e 20 20 49 orary tables. I
1d989 6e 64 69 63 65 73 20 6f 66 20 32 20 6f 72 20 6d ndices of 2 or m
1d98a 6f 72 65 20 61 72 65 20 75 73 65 64 20 66 6f 72 ore are used for
1d98b 0a 2a 2a 20 61 74 74 61 63 68 65 64 20 64 61 74 .** attached dat
1d98c 61 62 61 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 abases..**.** If
1d98d 20 50 32 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c P2 is non-zero,
1d98e 20 74 68 65 6e 20 61 20 77 72 69 74 65 2d 74 72 then a write-tr
1d98f 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 74 61 ansaction is sta
1d990 72 74 65 64 2e 20 20 41 20 52 45 53 45 52 56 45 rted. A RESERVE
1d991 44 20 6c 6f 63 6b 20 69 73 0a 2a 2a 20 6f 62 74 D lock is.** obt
1d992 61 69 6e 65 64 20 6f 6e 20 74 68 65 20 64 61 74 ained on the dat
1d993 61 62 61 73 65 20 66 69 6c 65 20 77 68 65 6e 20 abase file when
1d994 61 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 a write-transact
1d995 69 6f 6e 20 69 73 20 73 74 61 72 74 65 64 2e 20 ion is started.
1d996 20 4e 6f 0a 2a 2a 20 6f 74 68 65 72 20 70 72 6f No.** other pro
1d997 63 65 73 73 20 63 61 6e 20 73 74 61 72 74 20 61 cess can start a
1d998 6e 6f 74 68 65 72 20 77 72 69 74 65 20 74 72 61 nother write tra
1d999 6e 73 61 63 74 69 6f 6e 20 77 68 69 6c 65 20 74 nsaction while t
1d99a 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 his transaction
1d99b 69 73 0a 2a 2a 20 75 6e 64 65 72 77 61 79 2e 20 is.** underway.
1d99c 20 53 74 61 72 74 69 6e 67 20 61 20 77 72 69 74 Starting a writ
1d99d 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 6c e transaction al
1d99e 73 6f 20 63 72 65 61 74 65 73 20 61 20 72 6f 6c so creates a rol
1d99f 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e 20 41 lback journal. A
1d9a0 0a 2a 2a 20 77 72 69 74 65 20 74 72 61 6e 73 61 .** write transa
1d9a1 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 73 74 ction must be st
1d9a2 61 72 74 65 64 20 62 65 66 6f 72 65 20 61 6e 79 arted before any
1d9a3 20 63 68 61 6e 67 65 73 20 63 61 6e 20 62 65 20 changes can be
1d9a4 6d 61 64 65 20 74 6f 20 74 68 65 0a 2a 2a 20 64 made to the.** d
1d9a5 61 74 61 62 61 73 65 2e 20 20 49 66 20 50 32 20 atabase. If P2
1d9a6 69 73 20 32 20 6f 72 20 67 72 65 61 74 65 72 20 is 2 or greater
1d9a7 74 68 65 6e 20 61 6e 20 45 58 43 4c 55 53 49 56 then an EXCLUSIV
1d9a8 45 20 6c 6f 63 6b 20 69 73 20 61 6c 73 6f 20 6f E lock is also o
1d9a9 62 74 61 69 6e 65 64 0a 2a 2a 20 6f 6e 20 74 68 btained.** on th
1d9aa 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 e file..**.** If
1d9ab 20 61 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 a write-transac
1d9ac 74 69 6f 6e 20 69 73 20 73 74 61 72 74 65 64 20 tion is started
1d9ad 61 6e 64 20 74 68 65 20 56 64 62 65 2e 75 73 65 and the Vdbe.use
1d9ae 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 20 66 6c 61 sStmtJournal fla
1d9af 67 20 69 73 0a 2a 2a 20 74 72 75 65 20 28 74 68 g is.** true (th
1d9b0 69 73 20 66 6c 61 67 20 69 73 20 73 65 74 20 69 is flag is set i
1d9b1 66 20 74 68 65 20 56 64 62 65 20 6d 61 79 20 6d f the Vdbe may m
1d9b2 6f 64 69 66 79 20 6d 6f 72 65 20 74 68 61 6e 20 odify more than
1d9b3 6f 6e 65 20 72 6f 77 20 61 6e 64 20 6d 61 79 0a one row and may.
1d9b4 2a 2a 20 74 68 72 6f 77 20 61 6e 20 41 42 4f 52 ** throw an ABOR
1d9b5 54 20 65 78 63 65 70 74 69 6f 6e 29 2c 20 61 20 T exception), a
1d9b6 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 statement transa
1d9b7 63 74 69 6f 6e 20 6d 61 79 20 61 6c 73 6f 20 62 ction may also b
1d9b8 65 20 6f 70 65 6e 65 64 2e 0a 2a 2a 20 4d 6f 72 e opened..** Mor
1d9b9 65 20 73 70 65 63 69 66 69 63 61 6c 6c 79 2c 20 e specifically,
1d9ba 61 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e a statement tran
1d9bb 73 61 63 74 69 6f 6e 20 69 73 20 6f 70 65 6e 65 saction is opene
1d9bc 64 20 69 66 66 20 74 68 65 20 64 61 74 61 62 61 d iff the databa
1d9bd 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e se.** connection
1d9be 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 6e 6f is currently no
1d9bf 74 20 69 6e 20 61 75 74 6f 63 6f 6d 6d 69 74 20 t in autocommit
1d9c0 6d 6f 64 65 2c 20 6f 72 20 69 66 20 74 68 65 72 mode, or if ther
1d9c1 65 20 61 72 65 20 6f 74 68 65 72 0a 2a 2a 20 61 e are other.** a
1d9c2 63 74 69 76 65 20 73 74 61 74 65 6d 65 6e 74 73 ctive statements
1d9c3 2e 20 41 20 73 74 61 74 65 6d 65 6e 74 20 74 72 . A statement tr
1d9c4 61 6e 73 61 63 74 69 6f 6e 20 61 6c 6c 6f 77 73 ansaction allows
1d9c5 20 74 68 65 20 61 66 66 65 63 74 73 20 6f 66 20 the affects of
1d9c6 74 68 69 73 0a 2a 2a 20 56 44 42 45 20 74 6f 20 this.** VDBE to
1d9c7 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61 be rolled back a
1d9c8 66 74 65 72 20 61 6e 20 65 72 72 6f 72 20 77 69 fter an error wi
1d9c9 74 68 6f 75 74 20 68 61 76 69 6e 67 20 74 6f 20 thout having to
1d9ca 72 6f 6c 6c 20 62 61 63 6b 20 74 68 65 0a 2a 2a roll back the.**
1d9cb 20 65 6e 74 69 72 65 20 74 72 61 6e 73 61 63 74 entire transact
1d9cc 69 6f 6e 2e 20 49 66 20 6e 6f 20 65 72 72 6f 72 ion. If no error
1d9cd 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2c is encountered,
1d9ce 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 74 the statement t
1d9cf 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 77 69 ransaction.** wi
1d9d0 6c 6c 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 ll automatically
1d9d1 20 63 6f 6d 6d 69 74 20 77 68 65 6e 20 74 68 65 commit when the
1d9d2 20 56 44 42 45 20 68 61 6c 74 73 2e 0a 2a 2a 0a VDBE halts..**.
1d9d3 2a 2a 20 49 66 20 50 32 20 69 73 20 7a 65 72 6f ** If P2 is zero
1d9d4 2c 20 74 68 65 6e 20 61 20 72 65 61 64 2d 6c 6f , then a read-lo
1d9d5 63 6b 20 69 73 20 6f 62 74 61 69 6e 65 64 20 6f ck is obtained o
1d9d6 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 n the database f
1d9d7 69 6c 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f ile..*/.case OP_
1d9d8 54 72 61 6e 73 61 63 74 69 6f 6e 3a 20 7b 0a 23 Transaction: {.#
1d9d9 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 if 0 /* local v
1d9da 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 ariables moved i
1d9db 6e 74 6f 20 75 2e 61 73 20 2a 2f 0a 20 20 42 74 nto u.as */. Bt
1d9dc 72 65 65 20 2a 70 42 74 3b 0a 23 65 6e 64 69 66 ree *pBt;.#endif
1d9dd 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1d9de 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1d9df 2e 61 73 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 .as */.. assert
1d9e0 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 ( pOp->p1>=0 &&
1d9e1 70 4f 70 2d 3e 70 31 3c 64 62 2d 3e 6e 44 62 20 pOp->p1<db->nDb
1d9e2 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 2d );. assert( (p-
1d9e3 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28 31 3c >btreeMask & (1<
1d9e4 3c 70 4f 70 2d 3e 70 31 29 29 21 3d 30 20 29 3b <pOp->p1))!=0 );
1d9e5 0a 20 20 75 2e 61 73 2e 70 42 74 20 3d 20 64 62 . u.as.pBt = db
1d9e6 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 31 5d 2e 70 ->aDb[pOp->p1].p
1d9e7 42 74 3b 0a 0a 20 20 69 66 28 20 75 2e 61 73 2e Bt;.. if( u.as.
1d9e8 70 42 74 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 pBt ){. rc =
1d9e9 73 71 6c 69 74 65 33 42 74 72 65 65 42 65 67 69 sqlite3BtreeBegi
1d9ea 6e 54 72 61 6e 73 28 75 2e 61 73 2e 70 42 74 2c nTrans(u.as.pBt,
1d9eb 20 70 4f 70 2d 3e 70 32 29 3b 0a 20 20 20 20 69 pOp->p2);. i
1d9ec 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 f( rc==SQLITE_BU
1d9ed 53 59 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 70 SY ){. p->p
1d9ee 63 20 3d 20 70 63 3b 0a 20 20 20 20 20 20 70 2d c = pc;. p-
1d9ef 3e 72 63 20 3d 20 72 63 20 3d 20 53 51 4c 49 54 >rc = rc = SQLIT
1d9f0 45 5f 42 55 53 59 3b 0a 20 20 20 20 20 20 67 6f E_BUSY;. go
1d9f1 74 6f 20 76 64 62 65 5f 72 65 74 75 72 6e 3b 0a to vdbe_return;.
1d9f2 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 }. if( rc
1d9f3 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
1d9f4 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f goto abort_
1d9f5 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 due_to_error;.
1d9f6 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 70 4f 70 }.. if( pOp
1d9f7 2d 3e 70 32 20 26 26 20 70 2d 3e 75 73 65 73 53 ->p2 && p->usesS
1d9f8 74 6d 74 4a 6f 75 72 6e 61 6c 0a 20 20 20 20 20 tmtJournal.
1d9f9 26 26 20 28 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d && (db->autoComm
1d9fa 69 74 3d 3d 30 20 7c 7c 20 64 62 2d 3e 61 63 74 it==0 || db->act
1d9fb 69 76 65 56 64 62 65 43 6e 74 3e 31 29 0a 20 20 iveVdbeCnt>1).
1d9fc 20 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 ){. asser
1d9fd 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 t( sqlite3BtreeI
1d9fe 73 49 6e 54 72 61 6e 73 28 75 2e 61 73 2e 70 42 sInTrans(u.as.pB
1d9ff 74 29 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 t) );. if(
1da00 70 2d 3e 69 53 74 61 74 65 6d 65 6e 74 3d 3d 30 p->iStatement==0
1da01 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 ){. asse
1da02 72 74 28 20 64 62 2d 3e 6e 53 74 61 74 65 6d 65 rt( db->nStateme
1da03 6e 74 3e 3d 30 20 26 26 20 64 62 2d 3e 6e 53 61 nt>=0 && db->nSa
1da04 76 65 70 6f 69 6e 74 3e 3d 30 20 29 3b 0a 20 20 vepoint>=0 );.
1da05 20 20 20 20 20 20 64 62 2d 3e 6e 53 74 61 74 65 db->nState
1da06 6d 65 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 20 20 ment++;.
1da07 70 2d 3e 69 53 74 61 74 65 6d 65 6e 74 20 3d 20 p->iStatement =
1da08 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 2b db->nSavepoint +
1da09 20 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 3b db->nStatement;
1da0a 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 . }. r
1da0b 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 c = sqlite3Btree
1da0c 42 65 67 69 6e 53 74 6d 74 28 75 2e 61 73 2e 70 BeginStmt(u.as.p
1da0d 42 74 2c 20 70 2d 3e 69 53 74 61 74 65 6d 65 6e Bt, p->iStatemen
1da0e 74 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 53 74 t);.. /* St
1da0f 6f 72 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 ore the current
1da10 76 61 6c 75 65 20 6f 66 20 74 68 65 20 64 61 74 value of the dat
1da11 61 62 61 73 65 20 68 61 6e 64 6c 65 73 20 64 65 abase handles de
1da12 66 65 72 72 65 64 20 63 6f 6e 73 74 72 61 69 6e ferred constrain
1da13 74 0a 20 20 20 20 20 20 2a 2a 20 63 6f 75 6e 74 t. ** count
1da14 65 72 2e 20 49 66 20 74 68 65 20 73 74 61 74 65 er. If the state
1da15 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e ment transaction
1da16 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 6f 6c needs to be rol
1da17 6c 65 64 20 62 61 63 6b 2c 0a 20 20 20 20 20 20 led back,.
1da18 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 ** the value of
1da19 74 68 69 73 20 63 6f 75 6e 74 65 72 20 6e 65 65 this counter nee
1da1a 64 73 20 74 6f 20 62 65 20 72 65 73 74 6f 72 65 ds to be restore
1da1b 64 20 74 6f 6f 2e 20 20 2a 2f 0a 20 20 20 20 20 d too. */.
1da1c 20 70 2d 3e 6e 53 74 6d 74 44 65 66 43 6f 6e 73 p->nStmtDefCons
1da1d 20 3d 20 64 62 2d 3e 6e 44 65 66 65 72 72 65 64 = db->nDeferred
1da1e 43 6f 6e 73 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a Cons;. }. }.
1da1f 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f break;.}../* O
1da20 70 63 6f 64 65 3a 20 52 65 61 64 43 6f 6f 6b 69 pcode: ReadCooki
1da21 65 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a e P1 P2 P3 * *.*
1da22 2a 0a 2a 2a 20 52 65 61 64 20 63 6f 6f 6b 69 65 *.** Read cookie
1da23 20 6e 75 6d 62 65 72 20 50 33 20 66 72 6f 6d 20 number P3 from
1da24 64 61 74 61 62 61 73 65 20 50 31 20 61 6e 64 20 database P1 and
1da25 77 72 69 74 65 20 69 74 20 69 6e 74 6f 20 72 65 write it into re
1da26 67 69 73 74 65 72 20 50 32 2e 0a 2a 2a 20 50 33 gister P2..** P3
1da27 3d 3d 31 20 69 73 20 74 68 65 20 73 63 68 65 6d ==1 is the schem
1da28 61 20 76 65 72 73 69 6f 6e 2e 20 20 50 33 3d 3d a version. P3==
1da29 32 20 69 73 20 74 68 65 20 64 61 74 61 62 61 73 2 is the databas
1da2a 65 20 66 6f 72 6d 61 74 2e 0a 2a 2a 20 50 33 3d e format..** P3=
1da2b 3d 33 20 69 73 20 74 68 65 20 72 65 63 6f 6d 6d =3 is the recomm
1da2c 65 6e 64 65 64 20 70 61 67 65 72 20 63 61 63 68 ended pager cach
1da2d 65 20 73 69 7a 65 2c 20 61 6e 64 20 73 6f 20 66 e size, and so f
1da2e 6f 72 74 68 2e 20 20 50 31 3d 3d 30 20 69 73 0a orth. P1==0 is.
1da2f 2a 2a 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 ** the main data
1da30 62 61 73 65 20 66 69 6c 65 20 61 6e 64 20 50 31 base file and P1
1da31 3d 3d 31 20 69 73 20 74 68 65 20 64 61 74 61 62 ==1 is the datab
1da32 61 73 65 20 66 69 6c 65 20 75 73 65 64 20 74 6f ase file used to
1da33 20 73 74 6f 72 65 0a 2a 2a 20 74 65 6d 70 6f 72 store.** tempor
1da34 61 72 79 20 74 61 62 6c 65 73 2e 0a 2a 2a 0a 2a ary tables..**.*
1da35 2a 20 54 68 65 72 65 20 6d 75 73 74 20 62 65 20 * There must be
1da36 61 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 74 a read-lock on t
1da37 68 65 20 64 61 74 61 62 61 73 65 20 28 65 69 74 he database (eit
1da38 68 65 72 20 61 20 74 72 61 6e 73 61 63 74 69 6f her a transactio
1da39 6e 0a 2a 2a 20 6d 75 73 74 20 62 65 20 73 74 61 n.** must be sta
1da3a 72 74 65 64 20 6f 72 20 74 68 65 72 65 20 6d 75 rted or there mu
1da3b 73 74 20 62 65 20 61 6e 20 6f 70 65 6e 20 63 75 st be an open cu
1da3c 72 73 6f 72 29 20 62 65 66 6f 72 65 0a 2a 2a 20 rsor) before.**
1da3d 65 78 65 63 75 74 69 6e 67 20 74 68 69 73 20 69 executing this i
1da3e 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 63 nstruction..*/.c
1da3f 61 73 65 20 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 ase OP_ReadCooki
1da40 65 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 e: {
1da41 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 /* out2-prere
1da42 6c 65 61 73 65 20 2a 2f 0a 23 69 66 20 30 20 20 lease */.#if 0
1da43 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c /* local variabl
1da44 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e es moved into u.
1da45 61 74 20 2a 2f 0a 20 20 69 6e 74 20 69 4d 65 74 at */. int iMet
1da46 61 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 a;. int iDb;.
1da47 69 6e 74 20 69 43 6f 6f 6b 69 65 3b 0a 23 65 6e int iCookie;.#en
1da48 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 dif /* local var
1da49 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 iables moved int
1da4a 6f 20 75 2e 61 74 20 2a 2f 0a 0a 20 20 75 2e 61 o u.at */.. u.a
1da4b 74 2e 69 44 62 20 3d 20 70 4f 70 2d 3e 70 31 3b t.iDb = pOp->p1;
1da4c 0a 20 20 75 2e 61 74 2e 69 43 6f 6f 6b 69 65 20 . u.at.iCookie
1da4d 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 61 73 73 = pOp->p3;. ass
1da4e 65 72 74 28 20 70 4f 70 2d 3e 70 33 3c 53 51 4c ert( pOp->p3<SQL
1da4f 49 54 45 5f 4e 5f 42 54 52 45 45 5f 4d 45 54 41 ITE_N_BTREE_META
1da50 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e );. assert( u.
1da51 61 74 2e 69 44 62 3e 3d 30 20 26 26 20 75 2e 61 at.iDb>=0 && u.a
1da52 74 2e 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b t.iDb<db->nDb );
1da53 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 61 . assert( db->a
1da54 44 62 5b 75 2e 61 74 2e 69 44 62 5d 2e 70 42 74 Db[u.at.iDb].pBt
1da55 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 !=0 );. assert(
1da56 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 (p->btreeMask &
1da57 20 28 31 3c 3c 75 2e 61 74 2e 69 44 62 29 29 21 (1<<u.at.iDb))!
1da58 3d 30 20 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33 =0 );.. sqlite3
1da59 42 74 72 65 65 47 65 74 4d 65 74 61 28 64 62 2d BtreeGetMeta(db-
1da5a 3e 61 44 62 5b 75 2e 61 74 2e 69 44 62 5d 2e 70 >aDb[u.at.iDb].p
1da5b 42 74 2c 20 75 2e 61 74 2e 69 43 6f 6f 6b 69 65 Bt, u.at.iCookie
1da5c 2c 20 28 75 33 32 20 2a 29 26 75 2e 61 74 2e 69 , (u32 *)&u.at.i
1da5d 4d 65 74 61 29 3b 0a 20 20 70 4f 75 74 2d 3e 75 Meta);. pOut->u
1da5e 2e 69 20 3d 20 75 2e 61 74 2e 69 4d 65 74 61 3b .i = u.at.iMeta;
1da5f 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 . MemSetTypeFla
1da60 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 g(pOut, MEM_Int)
1da61 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a ;. break;.}../*
1da62 20 4f 70 63 6f 64 65 3a 20 53 65 74 43 6f 6f 6b Opcode: SetCook
1da63 69 65 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a ie P1 P2 P3 * *.
1da64 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20 **.** Write the
1da65 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65 67 69 73 content of regis
1da66 74 65 72 20 50 33 20 28 69 6e 74 65 72 70 72 65 ter P3 (interpre
1da67 74 65 64 20 61 73 20 61 6e 20 69 6e 74 65 67 65 ted as an intege
1da68 72 29 0a 2a 2a 20 69 6e 74 6f 20 63 6f 6f 6b 69 r).** into cooki
1da69 65 20 6e 75 6d 62 65 72 20 50 32 20 6f 66 20 64 e number P2 of d
1da6a 61 74 61 62 61 73 65 20 50 31 2e 20 20 50 32 3d atabase P1. P2=
1da6b 3d 31 20 69 73 20 74 68 65 20 73 63 68 65 6d 61 =1 is the schema
1da6c 20 76 65 72 73 69 6f 6e 2e 20 20 0a 2a 2a 20 50 version. .** P
1da6d 32 3d 3d 32 20 69 73 20 74 68 65 20 64 61 74 61 2==2 is the data
1da6e 62 61 73 65 20 66 6f 72 6d 61 74 2e 20 50 32 3d base format. P2=
1da6f 3d 33 20 69 73 20 74 68 65 20 72 65 63 6f 6d 6d =3 is the recomm
1da70 65 6e 64 65 64 20 70 61 67 65 72 20 63 61 63 68 ended pager cach
1da71 65 20 0a 2a 2a 20 73 69 7a 65 2c 20 61 6e 64 20 e .** size, and
1da72 73 6f 20 66 6f 72 74 68 2e 20 20 50 31 3d 3d 30 so forth. P1==0
1da73 20 69 73 20 74 68 65 20 6d 61 69 6e 20 64 61 74 is the main dat
1da74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 20 50 abase file and P
1da75 31 3d 3d 31 20 69 73 20 74 68 65 20 0a 2a 2a 20 1==1 is the .**
1da76 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 75 73 database file us
1da77 65 64 20 74 6f 20 73 74 6f 72 65 20 74 65 6d 70 ed to store temp
1da78 6f 72 61 72 79 20 74 61 62 6c 65 73 2e 0a 2a 2a orary tables..**
1da79 0a 2a 2a 20 41 20 74 72 61 6e 73 61 63 74 69 6f .** A transactio
1da7a 6e 20 6d 75 73 74 20 62 65 20 73 74 61 72 74 65 n must be starte
1da7b 64 20 62 65 66 6f 72 65 20 65 78 65 63 75 74 69 d before executi
1da7c 6e 67 20 74 68 69 73 20 6f 70 63 6f 64 65 2e 0a ng this opcode..
1da7d 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65 74 43 6f */.case OP_SetCo
1da7e 6f 6b 69 65 3a 20 7b 20 20 20 20 20 20 20 2f 2a okie: { /*
1da7f 20 69 6e 33 20 2a 2f 0a 23 69 66 20 30 20 20 2f in3 */.#if 0 /
1da80 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 * local variable
1da81 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 s moved into u.a
1da82 75 20 2a 2f 0a 20 20 44 62 20 2a 70 44 62 3b 0a u */. Db *pDb;.
1da83 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 #endif /* local
1da84 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 variables moved
1da85 69 6e 74 6f 20 75 2e 61 75 20 2a 2f 0a 20 20 61 into u.au */. a
1da86 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3c 53 ssert( pOp->p2<S
1da87 51 4c 49 54 45 5f 4e 5f 42 54 52 45 45 5f 4d 45 QLITE_N_BTREE_ME
1da88 54 41 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 TA );. assert(
1da89 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f pOp->p1>=0 && pO
1da8a 70 2d 3e 70 31 3c 64 62 2d 3e 6e 44 62 20 29 3b p->p1<db->nDb );
1da8b 0a 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62 . assert( (p->b
1da8c 74 72 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 70 treeMask & (1<<p
1da8d 4f 70 2d 3e 70 31 29 29 21 3d 30 20 29 3b 0a 20 Op->p1))!=0 );.
1da8e 20 75 2e 61 75 2e 70 44 62 20 3d 20 26 64 62 2d u.au.pDb = &db-
1da8f 3e 61 44 62 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 >aDb[pOp->p1];.
1da90 20 61 73 73 65 72 74 28 20 75 2e 61 75 2e 70 44 assert( u.au.pD
1da91 62 2d 3e 70 42 74 21 3d 30 20 29 3b 0a 20 20 73 b->pBt!=0 );. s
1da92 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 qlite3VdbeMemInt
1da93 65 67 65 72 69 66 79 28 70 49 6e 33 29 3b 0a 20 egerify(pIn3);.
1da94 20 2f 2a 20 53 65 65 20 6e 6f 74 65 20 61 62 6f /* See note abo
1da95 75 74 20 69 6e 64 65 78 20 73 68 69 66 74 69 6e ut index shiftin
1da96 67 20 6f 6e 20 4f 50 5f 52 65 61 64 43 6f 6f 6b g on OP_ReadCook
1da97 69 65 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c ie */. rc = sql
1da98 69 74 65 33 42 74 72 65 65 55 70 64 61 74 65 4d ite3BtreeUpdateM
1da99 65 74 61 28 75 2e 61 75 2e 70 44 62 2d 3e 70 42 eta(u.au.pDb->pB
1da9a 74 2c 20 70 4f 70 2d 3e 70 32 2c 20 28 69 6e 74 t, pOp->p2, (int
1da9b 29 70 49 6e 33 2d 3e 75 2e 69 29 3b 0a 20 20 69 )pIn3->u.i);. i
1da9c 66 28 20 70 4f 70 2d 3e 70 32 3d 3d 42 54 52 45 f( pOp->p2==BTRE
1da9d 45 5f 53 43 48 45 4d 41 5f 56 45 52 53 49 4f 4e E_SCHEMA_VERSION
1da9e 20 29 7b 0a 20 20 20 20 2f 2a 20 57 68 65 6e 20 ){. /* When
1da9f 74 68 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 the schema cooki
1daa0 65 20 63 68 61 6e 67 65 73 2c 20 72 65 63 6f 72 e changes, recor
1daa1 64 20 74 68 65 20 6e 65 77 20 63 6f 6f 6b 69 65 d the new cookie
1daa2 20 69 6e 74 65 72 6e 61 6c 6c 79 20 2a 2f 0a 20 internally */.
1daa3 20 20 20 75 2e 61 75 2e 70 44 62 2d 3e 70 53 63 u.au.pDb->pSc
1daa4 68 65 6d 61 2d 3e 73 63 68 65 6d 61 5f 63 6f 6f hema->schema_coo
1daa5 6b 69 65 20 3d 20 28 69 6e 74 29 70 49 6e 33 2d kie = (int)pIn3-
1daa6 3e 75 2e 69 3b 0a 20 20 20 20 64 62 2d 3e 66 6c >u.i;. db->fl
1daa7 61 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f 49 6e ags |= SQLITE_In
1daa8 74 65 72 6e 43 68 61 6e 67 65 73 3b 0a 20 20 7d ternChanges;. }
1daa9 65 6c 73 65 20 69 66 28 20 70 4f 70 2d 3e 70 32 else if( pOp->p2
1daaa 3d 3d 42 54 52 45 45 5f 46 49 4c 45 5f 46 4f 52 ==BTREE_FILE_FOR
1daab 4d 41 54 20 29 7b 0a 20 20 20 20 2f 2a 20 52 65 MAT ){. /* Re
1daac 63 6f 72 64 20 63 68 61 6e 67 65 73 20 69 6e 20 cord changes in
1daad 74 68 65 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 the file format
1daae 2a 2f 0a 20 20 20 20 75 2e 61 75 2e 70 44 62 2d */. u.au.pDb-
1daaf 3e 70 53 63 68 65 6d 61 2d 3e 66 69 6c 65 5f 66 >pSchema->file_f
1dab0 6f 72 6d 61 74 20 3d 20 28 75 38 29 70 49 6e 33 ormat = (u8)pIn3
1dab1 2d 3e 75 2e 69 3b 0a 20 20 7d 0a 20 20 69 66 28 ->u.i;. }. if(
1dab2 20 70 4f 70 2d 3e 70 31 3d 3d 31 20 29 7b 0a 20 pOp->p1==1 ){.
1dab3 20 20 20 2f 2a 20 49 6e 76 61 6c 69 64 61 74 65 /* Invalidate
1dab4 20 61 6c 6c 20 70 72 65 70 61 72 65 64 20 73 74 all prepared st
1dab5 61 74 65 6d 65 6e 74 73 20 77 68 65 6e 65 76 65 atements wheneve
1dab6 72 20 74 68 65 20 54 45 4d 50 20 64 61 74 61 62 r the TEMP datab
1dab7 61 73 65 0a 20 20 20 20 2a 2a 20 73 63 68 65 6d ase. ** schem
1dab8 61 20 69 73 20 63 68 61 6e 67 65 64 2e 20 20 54 a is changed. T
1dab9 69 63 6b 65 74 20 23 31 36 34 34 20 2a 2f 0a 20 icket #1644 */.
1daba 20 20 20 73 71 6c 69 74 65 33 45 78 70 69 72 65 sqlite3Expire
1dabb 50 72 65 70 61 72 65 64 53 74 61 74 65 6d 65 6e PreparedStatemen
1dabc 74 73 28 64 62 29 3b 0a 20 20 7d 0a 20 20 62 72 ts(db);. }. br
1dabd 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1dabe 65 3a 20 56 65 72 69 66 79 43 6f 6f 6b 69 65 20 e: VerifyCookie
1dabf 50 31 20 50 32 20 2a 0a 2a 2a 0a 2a 2a 20 43 68 P1 P2 *.**.** Ch
1dac0 65 63 6b 20 74 68 65 20 76 61 6c 75 65 20 6f 66 eck the value of
1dac1 20 67 6c 6f 62 61 6c 20 64 61 74 61 62 61 73 65 global database
1dac2 20 70 61 72 61 6d 65 74 65 72 20 6e 75 6d 62 65 parameter numbe
1dac3 72 20 30 20 28 74 68 65 0a 2a 2a 20 73 63 68 65 r 0 (the.** sche
1dac4 6d 61 20 76 65 72 73 69 6f 6e 29 20 61 6e 64 20 ma version) and
1dac5 6d 61 6b 65 20 73 75 72 65 20 69 74 20 69 73 20 make sure it is
1dac6 65 71 75 61 6c 20 74 6f 20 50 32 2e 20 20 0a 2a equal to P2. .*
1dac7 2a 20 50 31 20 69 73 20 74 68 65 20 64 61 74 61 * P1 is the data
1dac8 62 61 73 65 20 6e 75 6d 62 65 72 20 77 68 69 63 base number whic
1dac9 68 20 69 73 20 30 20 66 6f 72 20 74 68 65 20 6d h is 0 for the m
1daca 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c ain database fil
1dacb 65 0a 2a 2a 20 61 6e 64 20 31 20 66 6f 72 20 74 e.** and 1 for t
1dacc 68 65 20 66 69 6c 65 20 68 6f 6c 64 69 6e 67 20 he file holding
1dacd 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 temporary tables
1dace 20 61 6e 64 20 73 6f 6d 65 20 68 69 67 68 65 72 and some higher
1dacf 20 6e 75 6d 62 65 72 0a 2a 2a 20 66 6f 72 20 61 number.** for a
1dad0 75 78 69 6c 69 61 72 79 20 64 61 74 61 62 61 73 uxiliary databas
1dad1 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f es..**.** The co
1dad2 6f 6b 69 65 20 63 68 61 6e 67 65 73 20 69 74 73 okie changes its
1dad3 20 76 61 6c 75 65 20 77 68 65 6e 65 76 65 72 20 value whenever
1dad4 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68 the database sch
1dad5 65 6d 61 20 63 68 61 6e 67 65 73 2e 0a 2a 2a 20 ema changes..**
1dad6 54 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 20 69 This operation i
1dad7 73 20 75 73 65 64 20 74 6f 20 64 65 74 65 63 74 s used to detect
1dad8 20 77 68 65 6e 20 74 68 61 74 20 74 68 65 20 63 when that the c
1dad9 6f 6f 6b 69 65 20 68 61 73 20 63 68 61 6e 67 65 ookie has change
1dada 64 0a 2a 2a 20 61 6e 64 20 74 68 61 74 20 74 68 d.** and that th
1dadb 65 20 63 75 72 72 65 6e 74 20 70 72 6f 63 65 73 e current proces
1dadc 73 20 6e 65 65 64 73 20 74 6f 20 72 65 72 65 61 s needs to rerea
1dadd 64 20 74 68 65 20 73 63 68 65 6d 61 2e 0a 2a 2a d the schema..**
1dade 0a 2a 2a 20 45 69 74 68 65 72 20 61 20 74 72 61 .** Either a tra
1dadf 6e 73 61 63 74 69 6f 6e 20 6e 65 65 64 73 20 74 nsaction needs t
1dae0 6f 20 68 61 76 65 20 62 65 65 6e 20 73 74 61 72 o have been star
1dae1 74 65 64 20 6f 72 20 61 6e 20 4f 50 5f 4f 70 65 ted or an OP_Ope
1dae2 6e 20 6e 65 65 64 73 0a 2a 2a 20 74 6f 20 62 65 n needs.** to be
1dae3 20 65 78 65 63 75 74 65 64 20 28 74 6f 20 65 73 executed (to es
1dae4 74 61 62 6c 69 73 68 20 61 20 72 65 61 64 20 6c tablish a read l
1dae5 6f 63 6b 29 20 62 65 66 6f 72 65 20 74 68 69 73 ock) before this
1dae6 20 6f 70 63 6f 64 65 20 69 73 0a 2a 2a 20 69 6e opcode is.** in
1dae7 76 6f 6b 65 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f voked..*/.case O
1dae8 50 5f 56 65 72 69 66 79 43 6f 6f 6b 69 65 3a 20 P_VerifyCookie:
1dae9 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 {.#if 0 /* loca
1daea 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 l variables move
1daeb 64 20 69 6e 74 6f 20 75 2e 61 76 20 2a 2f 0a 20 d into u.av */.
1daec 20 69 6e 74 20 69 4d 65 74 61 3b 0a 20 20 42 74 int iMeta;. Bt
1daed 72 65 65 20 2a 70 42 74 3b 0a 23 65 6e 64 69 66 ree *pBt;.#endif
1daee 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1daef 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1daf0 2e 61 76 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 .av */. assert(
1daf1 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 pOp->p1>=0 && p
1daf2 4f 70 2d 3e 70 31 3c 64 62 2d 3e 6e 44 62 20 29 Op->p1<db->nDb )
1daf3 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e ;. assert( (p->
1daf4 62 74 72 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c btreeMask & (1<<
1daf5 70 4f 70 2d 3e 70 31 29 29 21 3d 30 20 29 3b 0a pOp->p1))!=0 );.
1daf6 20 20 75 2e 61 76 2e 70 42 74 20 3d 20 64 62 2d u.av.pBt = db-
1daf7 3e 61 44 62 5b 70 4f 70 2d 3e 70 31 5d 2e 70 42 >aDb[pOp->p1].pB
1daf8 74 3b 0a 20 20 69 66 28 20 75 2e 61 76 2e 70 42 t;. if( u.av.pB
1daf9 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 t ){. sqlite3
1dafa 42 74 72 65 65 47 65 74 4d 65 74 61 28 75 2e 61 BtreeGetMeta(u.a
1dafb 76 2e 70 42 74 2c 20 42 54 52 45 45 5f 53 43 48 v.pBt, BTREE_SCH
1dafc 45 4d 41 5f 56 45 52 53 49 4f 4e 2c 20 28 75 33 EMA_VERSION, (u3
1dafd 32 20 2a 29 26 75 2e 61 76 2e 69 4d 65 74 61 29 2 *)&u.av.iMeta)
1dafe 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 ;. }else{. u
1daff 2e 61 76 2e 69 4d 65 74 61 20 3d 20 30 3b 0a 20 .av.iMeta = 0;.
1db00 20 7d 0a 20 20 69 66 28 20 75 2e 61 76 2e 69 4d }. if( u.av.iM
1db01 65 74 61 21 3d 70 4f 70 2d 3e 70 32 20 29 7b 0a eta!=pOp->p2 ){.
1db02 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
1db03 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 e(db, p->zErrMsg
1db04 29 3b 0a 20 20 20 20 70 2d 3e 7a 45 72 72 4d 73 );. p->zErrMs
1db05 67 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 g = sqlite3DbStr
1db06 44 75 70 28 64 62 2c 20 22 64 61 74 61 62 61 73 Dup(db, "databas
1db07 65 20 73 63 68 65 6d 61 20 68 61 73 20 63 68 61 e schema has cha
1db08 6e 67 65 64 22 29 3b 0a 20 20 20 20 2f 2a 20 49 nged");. /* I
1db09 66 20 74 68 65 20 73 63 68 65 6d 61 2d 63 6f 6f f the schema-coo
1db0a 6b 69 65 20 66 72 6f 6d 20 74 68 65 20 64 61 74 kie from the dat
1db0b 61 62 61 73 65 20 66 69 6c 65 20 6d 61 74 63 68 abase file match
1db0c 65 73 20 74 68 65 20 63 6f 6f 6b 69 65 0a 20 20 es the cookie.
1db0d 20 20 2a 2a 20 73 74 6f 72 65 64 20 77 69 74 68 ** stored with
1db0e 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 the in-memory r
1db0f 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 epresentation of
1db10 20 74 68 65 20 73 63 68 65 6d 61 2c 20 64 6f 0a the schema, do.
1db11 20 20 20 20 2a 2a 20 6e 6f 74 20 72 65 6c 6f 61 ** not reloa
1db12 64 20 74 68 65 20 73 63 68 65 6d 61 20 66 72 6f d the schema fro
1db13 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 m the database f
1db14 69 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 ile.. **.
1db15 2a 2a 20 49 66 20 76 69 72 74 75 61 6c 2d 74 61 ** If virtual-ta
1db16 62 6c 65 73 20 61 72 65 20 69 6e 20 75 73 65 2c bles are in use,
1db17 20 74 68 69 73 20 69 73 20 6e 6f 74 20 6a 75 73 this is not jus
1db18 74 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f t an optimizatio
1db19 6e 2e 0a 20 20 20 20 2a 2a 20 4f 66 74 65 6e 2c n.. ** Often,
1db1a 20 76 2d 74 61 62 6c 65 73 20 73 74 6f 72 65 20 v-tables store
1db1b 74 68 65 69 72 20 64 61 74 61 20 69 6e 20 6f 74 their data in ot
1db1c 68 65 72 20 53 51 4c 69 74 65 20 74 61 62 6c 65 her SQLite table
1db1d 73 2c 20 77 68 69 63 68 0a 20 20 20 20 2a 2a 20 s, which. **
1db1e 61 72 65 20 71 75 65 72 69 65 64 20 66 72 6f 6d are queried from
1db1f 20 77 69 74 68 69 6e 20 78 4e 65 78 74 28 29 20 within xNext()
1db20 61 6e 64 20 6f 74 68 65 72 20 76 2d 74 61 62 6c and other v-tabl
1db21 65 20 6d 65 74 68 6f 64 73 20 75 73 69 6e 67 0a e methods using.
1db22 20 20 20 20 2a 2a 20 70 72 65 70 61 72 65 64 20 ** prepared
1db23 71 75 65 72 69 65 73 2e 20 49 66 20 73 75 63 68 queries. If such
1db24 20 61 20 71 75 65 72 79 20 69 73 20 6f 75 74 2d a query is out-
1db25 6f 66 2d 64 61 74 65 2c 20 77 65 20 64 6f 20 6e of-date, we do n
1db26 6f 74 20 77 61 6e 74 20 74 6f 0a 20 20 20 20 2a ot want to. *
1db27 2a 20 64 69 73 63 61 72 64 20 74 68 65 20 64 61 * discard the da
1db28 74 61 62 61 73 65 20 73 63 68 65 6d 61 2c 20 61 tabase schema, a
1db29 73 20 74 68 65 20 75 73 65 72 20 63 6f 64 65 20 s the user code
1db2a 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 74 68 65 implementing the
1db2b 0a 20 20 20 20 2a 2a 20 76 2d 74 61 62 6c 65 20 . ** v-table
1db2c 77 6f 75 6c 64 20 68 61 76 65 20 74 6f 20 62 65 would have to be
1db2d 20 72 65 61 64 79 20 66 6f 72 20 74 68 65 20 73 ready for the s
1db2e 71 6c 69 74 65 33 5f 76 74 61 62 20 73 74 72 75 qlite3_vtab stru
1db2f 63 74 75 72 65 20 69 74 73 65 6c 66 0a 20 20 20 cture itself.
1db30 20 2a 2a 20 74 6f 20 62 65 20 69 6e 76 61 6c 69 ** to be invali
1db31 64 61 74 65 64 20 77 68 65 6e 65 76 65 72 20 73 dated whenever s
1db32 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 69 73 qlite3_step() is
1db33 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74 called from wit
1db34 68 69 6e 0a 20 20 20 20 2a 2a 20 61 20 76 2d 74 hin. ** a v-t
1db35 61 62 6c 65 20 6d 65 74 68 6f 64 2e 0a 20 20 20 able method..
1db36 20 2a 2f 0a 20 20 20 20 69 66 28 20 64 62 2d 3e */. if( db->
1db37 61 44 62 5b 70 4f 70 2d 3e 70 31 5d 2e 70 53 63 aDb[pOp->p1].pSc
1db38 68 65 6d 61 2d 3e 73 63 68 65 6d 61 5f 63 6f 6f hema->schema_coo
1db39 6b 69 65 21 3d 75 2e 61 76 2e 69 4d 65 74 61 20 kie!=u.av.iMeta
1db3a 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
1db3b 52 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 ResetInternalSch
1db3c 65 6d 61 28 64 62 2c 20 70 4f 70 2d 3e 70 31 29 ema(db, pOp->p1)
1db3d 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c ;. }.. sql
1db3e 69 74 65 33 45 78 70 69 72 65 50 72 65 70 61 72 ite3ExpirePrepar
1db3f 65 64 53 74 61 74 65 6d 65 6e 74 73 28 64 62 29 edStatements(db)
1db40 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 ;. rc = SQLIT
1db41 45 5f 53 43 48 45 4d 41 3b 0a 20 20 7d 0a 20 20 E_SCHEMA;. }.
1db42 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 break;.}../* Opc
1db43 6f 64 65 3a 20 4f 70 65 6e 52 65 61 64 20 50 31 ode: OpenRead P1
1db44 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a P2 P3 P4 P5.**.
1db45 2a 2a 20 4f 70 65 6e 20 61 20 72 65 61 64 2d 6f ** Open a read-o
1db46 6e 6c 79 20 63 75 72 73 6f 72 20 66 6f 72 20 74 nly cursor for t
1db47 68 65 20 64 61 74 61 62 61 73 65 20 74 61 62 6c he database tabl
1db48 65 20 77 68 6f 73 65 20 72 6f 6f 74 20 70 61 67 e whose root pag
1db49 65 20 69 73 0a 2a 2a 20 50 32 20 69 6e 20 61 20 e is.** P2 in a
1db4a 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 database file.
1db4b 54 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c The database fil
1db4c 65 20 69 73 20 64 65 74 65 72 6d 69 6e 65 64 20 e is determined
1db4d 62 79 20 50 33 2e 20 0a 2a 2a 20 50 33 3d 3d 30 by P3. .** P3==0
1db4e 20 6d 65 61 6e 73 20 74 68 65 20 6d 61 69 6e 20 means the main
1db4f 64 61 74 61 62 61 73 65 2c 20 50 33 3d 3d 31 20 database, P3==1
1db50 6d 65 61 6e 73 20 74 68 65 20 64 61 74 61 62 61 means the databa
1db51 73 65 20 75 73 65 64 20 66 6f 72 20 0a 2a 2a 20 se used for .**
1db52 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 temporary tables
1db53 2c 20 61 6e 64 20 50 33 3e 31 20 6d 65 61 6e 73 , and P3>1 means
1db54 20 75 73 65 64 20 74 68 65 20 63 6f 72 72 65 73 used the corres
1db55 70 6f 6e 64 69 6e 67 20 61 74 74 61 63 68 65 64 ponding attached
1db56 0a 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 20 47 .** database. G
1db57 69 76 65 20 74 68 65 20 6e 65 77 20 63 75 72 73 ive the new curs
1db58 6f 72 20 61 6e 20 69 64 65 6e 74 69 66 69 65 72 or an identifier
1db59 20 6f 66 20 50 31 2e 20 20 54 68 65 20 50 31 0a of P1. The P1.
1db5a 2a 2a 20 76 61 6c 75 65 73 20 6e 65 65 64 20 6e ** values need n
1db5b 6f 74 20 62 65 20 63 6f 6e 74 69 67 75 6f 75 73 ot be contiguous
1db5c 20 62 75 74 20 61 6c 6c 20 50 31 20 76 61 6c 75 but all P1 valu
1db5d 65 73 20 73 68 6f 75 6c 64 20 62 65 20 73 6d 61 es should be sma
1db5e 6c 6c 20 69 6e 74 65 67 65 72 73 2e 0a 2a 2a 20 ll integers..**
1db5f 49 74 20 69 73 20 61 6e 20 65 72 72 6f 72 20 66 It is an error f
1db60 6f 72 20 50 31 20 74 6f 20 62 65 20 6e 65 67 61 or P1 to be nega
1db61 74 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 tive..**.** If P
1db62 35 21 3d 30 20 74 68 65 6e 20 75 73 65 20 74 68 5!=0 then use th
1db63 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65 67 e content of reg
1db64 69 73 74 65 72 20 50 32 20 61 73 20 74 68 65 20 ister P2 as the
1db65 72 6f 6f 74 20 70 61 67 65 2c 20 6e 6f 74 0a 2a root page, not.*
1db66 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 50 * the value of P
1db67 32 20 69 74 73 65 6c 66 2e 0a 2a 2a 0a 2a 2a 20 2 itself..**.**
1db68 54 68 65 72 65 20 77 69 6c 6c 20 62 65 20 61 20 There will be a
1db69 72 65 61 64 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 read lock on the
1db6a 20 64 61 74 61 62 61 73 65 20 77 68 65 6e 65 76 database whenev
1db6b 65 72 20 74 68 65 72 65 20 69 73 20 61 6e 0a 2a er there is an.*
1db6c 2a 20 6f 70 65 6e 20 63 75 72 73 6f 72 2e 20 20 * open cursor.
1db6d 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 If the database
1db6e 77 61 73 20 75 6e 6c 6f 63 6b 65 64 20 70 72 69 was unlocked pri
1db6f 6f 72 20 74 6f 20 74 68 69 73 20 69 6e 73 74 72 or to this instr
1db70 75 63 74 69 6f 6e 0a 2a 2a 20 74 68 65 6e 20 61 uction.** then a
1db71 20 72 65 61 64 20 6c 6f 63 6b 20 69 73 20 61 63 read lock is ac
1db72 71 75 69 72 65 64 20 61 73 20 70 61 72 74 20 6f quired as part o
1db73 66 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 f this instructi
1db74 6f 6e 2e 20 20 41 20 72 65 61 64 0a 2a 2a 20 6c on. A read.** l
1db75 6f 63 6b 20 61 6c 6c 6f 77 73 20 6f 74 68 65 72 ock allows other
1db76 20 70 72 6f 63 65 73 73 65 73 20 74 6f 20 72 65 processes to re
1db77 61 64 20 74 68 65 20 64 61 74 61 62 61 73 65 20 ad the database
1db78 62 75 74 20 70 72 6f 68 69 62 69 74 73 0a 2a 2a but prohibits.**
1db79 20 61 6e 79 20 6f 74 68 65 72 20 70 72 6f 63 65 any other proce
1db7a 73 73 20 66 72 6f 6d 20 6d 6f 64 69 66 79 69 6e ss from modifyin
1db7b 67 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 g the database.
1db7c 20 54 68 65 20 72 65 61 64 20 6c 6f 63 6b 20 69 The read lock i
1db7d 73 0a 2a 2a 20 72 65 6c 65 61 73 65 64 20 77 68 s.** released wh
1db7e 65 6e 20 61 6c 6c 20 63 75 72 73 6f 72 73 20 61 en all cursors a
1db7f 72 65 20 63 6c 6f 73 65 64 2e 20 20 49 66 20 74 re closed. If t
1db80 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 his instruction
1db81 61 74 74 65 6d 70 74 73 0a 2a 2a 20 74 6f 20 67 attempts.** to g
1db82 65 74 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 62 et a read lock b
1db83 75 74 20 66 61 69 6c 73 2c 20 74 68 65 20 73 63 ut fails, the sc
1db84 72 69 70 74 20 74 65 72 6d 69 6e 61 74 65 73 20 ript terminates
1db85 77 69 74 68 20 61 6e 0a 2a 2a 20 53 51 4c 49 54 with an.** SQLIT
1db86 45 5f 42 55 53 59 20 65 72 72 6f 72 20 63 6f 64 E_BUSY error cod
1db87 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 34 20 e..**.** The P4
1db88 76 61 6c 75 65 20 6d 61 79 20 62 65 20 65 69 74 value may be eit
1db89 68 65 72 20 61 6e 20 69 6e 74 65 67 65 72 20 28 her an integer (
1db8a 50 34 5f 49 4e 54 33 32 29 20 6f 72 20 61 20 70 P4_INT32) or a p
1db8b 6f 69 6e 74 65 72 20 74 6f 0a 2a 2a 20 61 20 4b ointer to.** a K
1db8c 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 eyInfo structure
1db8d 20 28 50 34 5f 4b 45 59 49 4e 46 4f 29 2e 20 49 (P4_KEYINFO). I
1db8e 66 20 69 74 20 69 73 20 61 20 70 6f 69 6e 74 65 f it is a pointe
1db8f 72 20 74 6f 20 61 20 4b 65 79 49 6e 66 6f 20 0a r to a KeyInfo .
1db90 2a 2a 20 73 74 72 75 63 74 75 72 65 2c 20 74 68 ** structure, th
1db91 65 6e 20 73 61 69 64 20 73 74 72 75 63 74 75 72 en said structur
1db92 65 20 64 65 66 69 6e 65 73 20 74 68 65 20 63 6f e defines the co
1db93 6e 74 65 6e 74 20 61 6e 64 20 63 6f 6c 6c 61 74 ntent and collat
1db94 69 6e 67 20 0a 2a 2a 20 73 65 71 75 65 6e 63 65 ing .** sequence
1db95 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 62 65 of the index be
1db96 69 6e 67 20 6f 70 65 6e 65 64 2e 20 4f 74 68 65 ing opened. Othe
1db97 72 77 69 73 65 2c 20 69 66 20 50 34 20 69 73 20 rwise, if P4 is
1db98 61 6e 20 69 6e 74 65 67 65 72 20 0a 2a 2a 20 76 an integer .** v
1db99 61 6c 75 65 2c 20 69 74 20 69 73 20 73 65 74 20 alue, it is set
1db9a 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 to the number of
1db9b 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 columns in the
1db9c 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 table..**.** See
1db9d 20 61 6c 73 6f 20 4f 70 65 6e 57 72 69 74 65 2e also OpenWrite.
1db9e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4f .*/./* Opcode: O
1db9f 70 65 6e 57 72 69 74 65 20 50 31 20 50 32 20 50 penWrite P1 P2 P
1dba0 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 4f 70 3 P4 P5.**.** Op
1dba1 65 6e 20 61 20 72 65 61 64 2f 77 72 69 74 65 20 en a read/write
1dba2 63 75 72 73 6f 72 20 6e 61 6d 65 64 20 50 31 20 cursor named P1
1dba3 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 on the table or
1dba4 69 6e 64 65 78 20 77 68 6f 73 65 20 72 6f 6f 74 index whose root
1dba5 0a 2a 2a 20 70 61 67 65 20 69 73 20 50 32 2e 20 .** page is P2.
1dba6 20 4f 72 20 69 66 20 50 35 21 3d 30 20 75 73 65 Or if P5!=0 use
1dba7 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 the content of
1dba8 72 65 67 69 73 74 65 72 20 50 32 20 74 6f 20 66 register P2 to f
1dba9 69 6e 64 20 74 68 65 0a 2a 2a 20 72 6f 6f 74 20 ind the.** root
1dbaa 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 page..**.** The
1dbab 50 34 20 76 61 6c 75 65 20 6d 61 79 20 62 65 20 P4 value may be
1dbac 65 69 74 68 65 72 20 61 6e 20 69 6e 74 65 67 65 either an intege
1dbad 72 20 28 50 34 5f 49 4e 54 33 32 29 20 6f 72 20 r (P4_INT32) or
1dbae 61 20 70 6f 69 6e 74 65 72 20 74 6f 0a 2a 2a 20 a pointer to.**
1dbaf 61 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 a KeyInfo struct
1dbb0 75 72 65 20 28 50 34 5f 4b 45 59 49 4e 46 4f 29 ure (P4_KEYINFO)
1dbb1 2e 20 49 66 20 69 74 20 69 73 20 61 20 70 6f 69 . If it is a poi
1dbb2 6e 74 65 72 20 74 6f 20 61 20 4b 65 79 49 6e 66 nter to a KeyInf
1dbb3 6f 20 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2c o .** structure,
1dbb4 20 74 68 65 6e 20 73 61 69 64 20 73 74 72 75 63 then said struc
1dbb5 74 75 72 65 20 64 65 66 69 6e 65 73 20 74 68 65 ture defines the
1dbb6 20 63 6f 6e 74 65 6e 74 20 61 6e 64 20 63 6f 6c content and col
1dbb7 6c 61 74 69 6e 67 20 0a 2a 2a 20 73 65 71 75 65 lating .** seque
1dbb8 6e 63 65 20 6f 66 20 74 68 65 20 69 6e 64 65 78 nce of the index
1dbb9 20 62 65 69 6e 67 20 6f 70 65 6e 65 64 2e 20 4f being opened. O
1dbba 74 68 65 72 77 69 73 65 2c 20 69 66 20 50 34 20 therwise, if P4
1dbbb 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 0a 2a is an integer .*
1dbbc 2a 20 76 61 6c 75 65 2c 20 69 74 20 69 73 20 73 * value, it is s
1dbbd 65 74 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 et to the number
1dbbe 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 of columns in t
1dbbf 68 65 20 74 61 62 6c 65 2c 20 6f 72 20 74 6f 20 he table, or to
1dbc0 74 68 65 0a 2a 2a 20 6c 61 72 67 65 73 74 20 69 the.** largest i
1dbc1 6e 64 65 78 20 6f 66 20 61 6e 79 20 63 6f 6c 75 ndex of any colu
1dbc2 6d 6e 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 mn of the table
1dbc3 74 68 61 74 20 69 73 20 61 63 74 75 61 6c 6c 79 that is actually
1dbc4 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 used..**.** Thi
1dbc5 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 77 6f s instruction wo
1dbc6 72 6b 73 20 6a 75 73 74 20 6c 69 6b 65 20 4f 70 rks just like Op
1dbc7 65 6e 52 65 61 64 20 65 78 63 65 70 74 20 74 68 enRead except th
1dbc8 61 74 20 69 74 20 6f 70 65 6e 73 20 74 68 65 20 at it opens the
1dbc9 63 75 72 73 6f 72 0a 2a 2a 20 69 6e 20 72 65 61 cursor.** in rea
1dbca 64 2f 77 72 69 74 65 20 6d 6f 64 65 2e 20 20 46 d/write mode. F
1dbcb 6f 72 20 61 20 67 69 76 65 6e 20 74 61 62 6c 65 or a given table
1dbcc 2c 20 74 68 65 72 65 20 63 61 6e 20 62 65 20 6f , there can be o
1dbcd 6e 65 20 6f 72 20 6d 6f 72 65 20 72 65 61 64 2d ne or more read-
1dbce 6f 6e 6c 79 0a 2a 2a 20 63 75 72 73 6f 72 73 20 only.** cursors
1dbcf 6f 72 20 61 20 73 69 6e 67 6c 65 20 72 65 61 64 or a single read
1dbd0 2f 77 72 69 74 65 20 63 75 72 73 6f 72 20 62 75 /write cursor bu
1dbd1 74 20 6e 6f 74 20 62 6f 74 68 2e 0a 2a 2a 0a 2a t not both..**.*
1dbd2 2a 20 53 65 65 20 61 6c 73 6f 20 4f 70 65 6e 52 * See also OpenR
1dbd3 65 61 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f ead..*/.case OP_
1dbd4 4f 70 65 6e 52 65 61 64 3a 0a 63 61 73 65 20 4f OpenRead:.case O
1dbd5 50 5f 4f 70 65 6e 57 72 69 74 65 3a 20 7b 0a 23 P_OpenWrite: {.#
1dbd6 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 if 0 /* local v
1dbd7 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 ariables moved i
1dbd8 6e 74 6f 20 75 2e 61 77 20 2a 2f 0a 20 20 69 6e nto u.aw */. in
1dbd9 74 20 6e 46 69 65 6c 64 3b 0a 20 20 4b 65 79 49 t nField;. KeyI
1dbda 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 0a 20 nfo *pKeyInfo;.
1dbdb 20 69 6e 74 20 70 32 3b 0a 20 20 69 6e 74 20 69 int p2;. int i
1dbdc 44 62 3b 0a 20 20 69 6e 74 20 77 72 46 6c 61 67 Db;. int wrFlag
1dbdd 3b 0a 20 20 42 74 72 65 65 20 2a 70 58 3b 0a 20 ;. Btree *pX;.
1dbde 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 75 VdbeCursor *pCu
1dbdf 72 3b 0a 20 20 44 62 20 2a 70 44 62 3b 0a 23 65 r;. Db *pDb;.#e
1dbe0 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 ndif /* local va
1dbe1 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1dbe2 74 6f 20 75 2e 61 77 20 2a 2f 0a 0a 20 20 75 2e to u.aw */.. u.
1dbe3 61 77 2e 6e 46 69 65 6c 64 20 3d 20 30 3b 0a 20 aw.nField = 0;.
1dbe4 20 75 2e 61 77 2e 70 4b 65 79 49 6e 66 6f 20 3d u.aw.pKeyInfo =
1dbe5 20 30 3b 0a 20 20 75 2e 61 77 2e 70 32 20 3d 20 0;. u.aw.p2 =
1dbe6 70 4f 70 2d 3e 70 32 3b 0a 20 20 75 2e 61 77 2e pOp->p2;. u.aw.
1dbe7 69 44 62 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 iDb = pOp->p3;.
1dbe8 20 61 73 73 65 72 74 28 20 75 2e 61 77 2e 69 44 assert( u.aw.iD
1dbe9 62 3e 3d 30 20 26 26 20 75 2e 61 77 2e 69 44 62 b>=0 && u.aw.iDb
1dbea 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 <db->nDb );. as
1dbeb 73 65 72 74 28 20 28 70 2d 3e 62 74 72 65 65 4d sert( (p->btreeM
1dbec 61 73 6b 20 26 20 28 31 3c 3c 75 2e 61 77 2e 69 ask & (1<<u.aw.i
1dbed 44 62 29 29 21 3d 30 20 29 3b 0a 20 20 75 2e 61 Db))!=0 );. u.a
1dbee 77 2e 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 w.pDb = &db->aDb
1dbef 5b 75 2e 61 77 2e 69 44 62 5d 3b 0a 20 20 75 2e [u.aw.iDb];. u.
1dbf0 61 77 2e 70 58 20 3d 20 75 2e 61 77 2e 70 44 62 aw.pX = u.aw.pDb
1dbf1 2d 3e 70 42 74 3b 0a 20 20 61 73 73 65 72 74 28 ->pBt;. assert(
1dbf2 20 75 2e 61 77 2e 70 58 21 3d 30 20 29 3b 0a 20 u.aw.pX!=0 );.
1dbf3 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 if( pOp->opcode
1dbf4 3d 3d 4f 50 5f 4f 70 65 6e 57 72 69 74 65 20 29 ==OP_OpenWrite )
1dbf5 7b 0a 20 20 20 20 75 2e 61 77 2e 77 72 46 6c 61 {. u.aw.wrFla
1dbf6 67 20 3d 20 31 3b 0a 20 20 20 20 69 66 28 20 75 g = 1;. if( u
1dbf7 2e 61 77 2e 70 44 62 2d 3e 70 53 63 68 65 6d 61 .aw.pDb->pSchema
1dbf8 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61 74 20 3c 20 ->file_format <
1dbf9 70 2d 3e 6d 69 6e 57 72 69 74 65 46 69 6c 65 46 p->minWriteFileF
1dbfa 6f 72 6d 61 74 20 29 7b 0a 20 20 20 20 20 20 70 ormat ){. p
1dbfb 2d 3e 6d 69 6e 57 72 69 74 65 46 69 6c 65 46 6f ->minWriteFileFo
1dbfc 72 6d 61 74 20 3d 20 75 2e 61 77 2e 70 44 62 2d rmat = u.aw.pDb-
1dbfd 3e 70 53 63 68 65 6d 61 2d 3e 66 69 6c 65 5f 66 >pSchema->file_f
1dbfe 6f 72 6d 61 74 3b 0a 20 20 20 20 7d 0a 20 20 7d ormat;. }. }
1dbff 65 6c 73 65 7b 0a 20 20 20 20 75 2e 61 77 2e 77 else{. u.aw.w
1dc00 72 46 6c 61 67 20 3d 20 30 3b 0a 20 20 7d 0a 20 rFlag = 0;. }.
1dc01 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 29 7b 0a if( pOp->p5 ){.
1dc02 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 61 77 assert( u.aw
1dc03 2e 70 32 3e 30 20 29 3b 0a 20 20 20 20 61 73 73 .p2>0 );. ass
1dc04 65 72 74 28 20 75 2e 61 77 2e 70 32 3c 3d 70 2d ert( u.aw.p2<=p-
1dc05 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 70 49 6e >nMem );. pIn
1dc06 32 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 75 2e 61 2 = &p->aMem[u.a
1dc07 77 2e 70 32 5d 3b 0a 20 20 20 20 73 71 6c 69 74 w.p2];. sqlit
1dc08 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 e3VdbeMemInteger
1dc09 69 66 79 28 70 49 6e 32 29 3b 0a 20 20 20 20 75 ify(pIn2);. u
1dc0a 2e 61 77 2e 70 32 20 3d 20 28 69 6e 74 29 70 49 .aw.p2 = (int)pI
1dc0b 6e 32 2d 3e 75 2e 69 3b 0a 20 20 20 20 2f 2a 20 n2->u.i;. /*
1dc0c 54 68 65 20 75 2e 61 77 2e 70 32 20 76 61 6c 75 The u.aw.p2 valu
1dc0d 65 20 61 6c 77 61 79 73 20 63 6f 6d 65 73 20 66 e always comes f
1dc0e 72 6f 6d 20 61 20 70 72 69 6f 72 20 4f 50 5f 43 rom a prior OP_C
1dc0f 72 65 61 74 65 54 61 62 6c 65 20 6f 70 63 6f 64 reateTable opcod
1dc10 65 20 61 6e 64 0a 20 20 20 20 2a 2a 20 74 68 61 e and. ** tha
1dc11 74 20 6f 70 63 6f 64 65 20 77 69 6c 6c 20 61 6c t opcode will al
1dc12 77 61 79 73 20 73 65 74 20 74 68 65 20 75 2e 61 ways set the u.a
1dc13 77 2e 70 32 20 76 61 6c 75 65 20 74 6f 20 32 20 w.p2 value to 2
1dc14 6f 72 20 6d 6f 72 65 20 6f 72 20 65 6c 73 65 20 or more or else
1dc15 66 61 69 6c 2e 0a 20 20 20 20 2a 2a 20 49 66 20 fail.. ** If
1dc16 74 68 65 72 65 20 77 65 72 65 20 61 20 66 61 69 there were a fai
1dc17 6c 75 72 65 2c 20 74 68 65 20 70 72 65 70 61 72 lure, the prepar
1dc18 65 64 20 73 74 61 74 65 6d 65 6e 74 20 77 6f 75 ed statement wou
1dc19 6c 64 20 68 61 76 65 20 68 61 6c 74 65 64 0a 20 ld have halted.
1dc1a 20 20 20 2a 2a 20 62 65 66 6f 72 65 20 72 65 61 ** before rea
1dc1b 63 68 69 6e 67 20 74 68 69 73 20 69 6e 73 74 72 ching this instr
1dc1c 75 63 74 69 6f 6e 2e 20 2a 2f 0a 20 20 20 20 69 uction. */. i
1dc1d 66 28 20 4e 45 56 45 52 28 75 2e 61 77 2e 70 32 f( NEVER(u.aw.p2
1dc1e 3c 32 29 20 29 20 7b 0a 20 20 20 20 20 20 72 63 <2) ) {. rc
1dc1f 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 = SQLITE_CORRUP
1dc20 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 67 6f T_BKPT;. go
1dc21 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f to abort_due_to_
1dc22 65 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 7d error;. }. }
1dc23 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 34 74 79 . if( pOp->p4ty
1dc24 70 65 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f 20 29 pe==P4_KEYINFO )
1dc25 7b 0a 20 20 20 20 75 2e 61 77 2e 70 4b 65 79 49 {. u.aw.pKeyI
1dc26 6e 66 6f 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 4b nfo = pOp->p4.pK
1dc27 65 79 49 6e 66 6f 3b 0a 20 20 20 20 75 2e 61 77 eyInfo;. u.aw
1dc28 2e 70 4b 65 79 49 6e 66 6f 2d 3e 65 6e 63 20 3d .pKeyInfo->enc =
1dc29 20 45 4e 43 28 70 2d 3e 64 62 29 3b 0a 20 20 20 ENC(p->db);.
1dc2a 20 75 2e 61 77 2e 6e 46 69 65 6c 64 20 3d 20 75 u.aw.nField = u
1dc2b 2e 61 77 2e 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 .aw.pKeyInfo->nF
1dc2c 69 65 6c 64 2b 31 3b 0a 20 20 7d 65 6c 73 65 20 ield+1;. }else
1dc2d 69 66 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d if( pOp->p4type=
1dc2e 3d 50 34 5f 49 4e 54 33 32 20 29 7b 0a 20 20 20 =P4_INT32 ){.
1dc2f 20 75 2e 61 77 2e 6e 46 69 65 6c 64 20 3d 20 70 u.aw.nField = p
1dc30 4f 70 2d 3e 70 34 2e 69 3b 0a 20 20 7d 0a 20 20 Op->p4.i;. }.
1dc31 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e assert( pOp->p1>
1dc32 3d 30 20 29 3b 0a 20 20 75 2e 61 77 2e 70 43 75 =0 );. u.aw.pCu
1dc33 72 20 3d 20 61 6c 6c 6f 63 61 74 65 43 75 72 73 r = allocateCurs
1dc34 6f 72 28 70 2c 20 70 4f 70 2d 3e 70 31 2c 20 75 or(p, pOp->p1, u
1dc35 2e 61 77 2e 6e 46 69 65 6c 64 2c 20 75 2e 61 77 .aw.nField, u.aw
1dc36 2e 69 44 62 2c 20 31 29 3b 0a 20 20 69 66 28 20 .iDb, 1);. if(
1dc37 75 2e 61 77 2e 70 43 75 72 3d 3d 30 20 29 20 67 u.aw.pCur==0 ) g
1dc38 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 75 2e oto no_mem;. u.
1dc39 61 77 2e 70 43 75 72 2d 3e 6e 75 6c 6c 52 6f 77 aw.pCur->nullRow
1dc3a 20 3d 20 31 3b 0a 20 20 72 63 20 3d 20 73 71 6c = 1;. rc = sql
1dc3b 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 28 ite3BtreeCursor(
1dc3c 75 2e 61 77 2e 70 58 2c 20 75 2e 61 77 2e 70 32 u.aw.pX, u.aw.p2
1dc3d 2c 20 75 2e 61 77 2e 77 72 46 6c 61 67 2c 20 75 , u.aw.wrFlag, u
1dc3e 2e 61 77 2e 70 4b 65 79 49 6e 66 6f 2c 20 75 2e .aw.pKeyInfo, u.
1dc3f 61 77 2e 70 43 75 72 2d 3e 70 43 75 72 73 6f 72 aw.pCur->pCursor
1dc40 29 3b 0a 20 20 75 2e 61 77 2e 70 43 75 72 2d 3e );. u.aw.pCur->
1dc41 70 4b 65 79 49 6e 66 6f 20 3d 20 75 2e 61 77 2e pKeyInfo = u.aw.
1dc42 70 4b 65 79 49 6e 66 6f 3b 0a 0a 20 20 2f 2a 20 pKeyInfo;.. /*
1dc43 53 69 6e 63 65 20 69 74 20 70 65 72 66 6f 72 6d Since it perform
1dc44 73 20 6e 6f 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f s no memory allo
1dc45 63 61 74 69 6f 6e 20 6f 72 20 49 4f 2c 20 74 68 cation or IO, th
1dc46 65 20 6f 6e 6c 79 20 76 61 6c 75 65 73 20 74 68 e only values th
1dc47 61 74 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 42 at. ** sqlite3B
1dc48 74 72 65 65 43 75 72 73 6f 72 28 29 20 6d 61 79 treeCursor() may
1dc49 20 72 65 74 75 72 6e 20 61 72 65 20 53 51 4c 49 return are SQLI
1dc4a 54 45 5f 45 4d 50 54 59 20 61 6e 64 20 53 51 4c TE_EMPTY and SQL
1dc4b 49 54 45 5f 4f 4b 2e 0a 20 20 2a 2a 20 53 51 4c ITE_OK.. ** SQL
1dc4c 49 54 45 5f 45 4d 50 54 59 20 69 73 20 6f 6e 6c ITE_EMPTY is onl
1dc4d 79 20 72 65 74 75 72 6e 65 64 20 77 68 65 6e 20 y returned when
1dc4e 61 74 74 65 6d 70 74 69 6e 67 20 74 6f 20 6f 70 attempting to op
1dc4f 65 6e 20 74 68 65 20 74 61 62 6c 65 0a 20 20 2a en the table. *
1dc50 2a 20 72 6f 6f 74 65 64 20 61 74 20 70 61 67 65 * rooted at page
1dc51 20 31 20 6f 66 20 61 20 7a 65 72 6f 2d 62 79 74 1 of a zero-byt
1dc52 65 20 64 61 74 61 62 61 73 65 2e 20 20 2a 2f 0a e database. */.
1dc53 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 assert( rc==SQ
1dc54 4c 49 54 45 5f 45 4d 50 54 59 20 7c 7c 20 72 63 LITE_EMPTY || rc
1dc55 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 ==SQLITE_OK );.
1dc56 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
1dc57 45 4d 50 54 59 20 29 7b 0a 20 20 20 20 75 2e 61 EMPTY ){. u.a
1dc58 77 2e 70 43 75 72 2d 3e 70 43 75 72 73 6f 72 20 w.pCur->pCursor
1dc59 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 = 0;. rc = SQ
1dc5a 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 LITE_OK;. }..
1dc5b 2f 2a 20 53 65 74 20 74 68 65 20 56 64 62 65 43 /* Set the VdbeC
1dc5c 75 72 73 6f 72 2e 69 73 54 61 62 6c 65 20 61 6e ursor.isTable an
1dc5d 64 20 69 73 49 6e 64 65 78 20 76 61 72 69 61 62 d isIndex variab
1dc5e 6c 65 73 2e 20 50 72 65 76 69 6f 75 73 20 76 65 les. Previous ve
1dc5f 72 73 69 6f 6e 73 20 6f 66 0a 20 20 2a 2a 20 53 rsions of. ** S
1dc60 51 4c 69 74 65 20 75 73 65 64 20 74 6f 20 63 68 QLite used to ch
1dc61 65 63 6b 20 69 66 20 74 68 65 20 72 6f 6f 74 2d eck if the root-
1dc62 70 61 67 65 20 66 6c 61 67 73 20 77 65 72 65 20 page flags were
1dc63 73 61 6e 65 20 61 74 20 74 68 69 73 20 70 6f 69 sane at this poi
1dc64 6e 74 0a 20 20 2a 2a 20 61 6e 64 20 72 65 70 6f nt. ** and repo
1dc65 72 74 20 64 61 74 61 62 61 73 65 20 63 6f 72 72 rt database corr
1dc66 75 70 74 69 6f 6e 20 69 66 20 74 68 65 79 20 77 uption if they w
1dc67 65 72 65 20 6e 6f 74 2c 20 62 75 74 20 74 68 69 ere not, but thi
1dc68 73 20 63 68 65 63 6b 20 68 61 73 0a 20 20 2a 2a s check has. **
1dc69 20 73 69 6e 63 65 20 6d 6f 76 65 64 20 69 6e 74 since moved int
1dc6a 6f 20 74 68 65 20 62 74 72 65 65 20 6c 61 79 65 o the btree laye
1dc6b 72 2e 20 20 2a 2f 0a 20 20 75 2e 61 77 2e 70 43 r. */. u.aw.pC
1dc6c 75 72 2d 3e 69 73 54 61 62 6c 65 20 3d 20 70 4f ur->isTable = pO
1dc6d 70 2d 3e 70 34 74 79 70 65 21 3d 50 34 5f 4b 45 p->p4type!=P4_KE
1dc6e 59 49 4e 46 4f 3b 0a 20 20 75 2e 61 77 2e 70 43 YINFO;. u.aw.pC
1dc6f 75 72 2d 3e 69 73 49 6e 64 65 78 20 3d 20 21 75 ur->isIndex = !u
1dc70 2e 61 77 2e 70 43 75 72 2d 3e 69 73 54 61 62 6c .aw.pCur->isTabl
1dc71 65 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f e;. break;.}../
1dc72 2a 20 4f 70 63 6f 64 65 3a 20 4f 70 65 6e 45 70 * Opcode: OpenEp
1dc73 68 65 6d 65 72 61 6c 20 50 31 20 50 32 20 2a 20 hemeral P1 P2 *
1dc74 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 4f 70 65 6e 20 P4 *.**.** Open
1dc75 61 20 6e 65 77 20 63 75 72 73 6f 72 20 50 31 20 a new cursor P1
1dc76 74 6f 20 61 20 74 72 61 6e 73 69 65 6e 74 20 74 to a transient t
1dc77 61 62 6c 65 2e 0a 2a 2a 20 54 68 65 20 63 75 72 able..** The cur
1dc78 73 6f 72 20 69 73 20 61 6c 77 61 79 73 20 6f 70 sor is always op
1dc79 65 6e 65 64 20 72 65 61 64 2f 77 72 69 74 65 20 ened read/write
1dc7a 65 76 65 6e 20 69 66 20 0a 2a 2a 20 74 68 65 20 even if .** the
1dc7b 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 69 73 main database is
1dc7c 20 72 65 61 64 2d 6f 6e 6c 79 2e 20 20 54 68 65 read-only. The
1dc7d 20 74 72 61 6e 73 69 65 6e 74 20 6f 72 20 76 69 transient or vi
1dc7e 72 74 75 61 6c 0a 2a 2a 20 74 61 62 6c 65 20 69 rtual.** table i
1dc7f 73 20 64 65 6c 65 74 65 64 20 61 75 74 6f 6d 61 s deleted automa
1dc80 74 69 63 61 6c 6c 79 20 77 68 65 6e 20 74 68 65 tically when the
1dc81 20 63 75 72 73 6f 72 20 69 73 20 63 6c 6f 73 65 cursor is close
1dc82 64 2e 0a 2a 2a 0a 2a 2a 20 50 32 20 69 73 20 74 d..**.** P2 is t
1dc83 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c he number of col
1dc84 75 6d 6e 73 20 69 6e 20 74 68 65 20 76 69 72 74 umns in the virt
1dc85 75 61 6c 20 74 61 62 6c 65 2e 0a 2a 2a 20 54 68 ual table..** Th
1dc86 65 20 63 75 72 73 6f 72 20 70 6f 69 6e 74 73 20 e cursor points
1dc87 74 6f 20 61 20 42 54 72 65 65 20 74 61 62 6c 65 to a BTree table
1dc88 20 69 66 20 50 34 3d 3d 30 20 61 6e 64 20 74 6f if P4==0 and to
1dc89 20 61 20 42 54 72 65 65 20 69 6e 64 65 78 0a 2a a BTree index.*
1dc8a 2a 20 69 66 20 50 34 20 69 73 20 6e 6f 74 20 30 * if P4 is not 0
1dc8b 2e 20 20 49 66 20 50 34 20 69 73 20 6e 6f 74 20 . If P4 is not
1dc8c 4e 55 4c 4c 2c 20 69 74 20 70 6f 69 6e 74 73 20 NULL, it points
1dc8d 74 6f 20 61 20 4b 65 79 49 6e 66 6f 20 73 74 72 to a KeyInfo str
1dc8e 75 63 74 75 72 65 0a 2a 2a 20 74 68 61 74 20 64 ucture.** that d
1dc8f 65 66 69 6e 65 73 20 74 68 65 20 66 6f 72 6d 61 efines the forma
1dc90 74 20 6f 66 20 6b 65 79 73 20 69 6e 20 74 68 65 t of keys in the
1dc91 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 54 68 index..**.** Th
1dc92 69 73 20 6f 70 63 6f 64 65 20 77 61 73 20 6f 6e is opcode was on
1dc93 63 65 20 63 61 6c 6c 65 64 20 4f 70 65 6e 54 65 ce called OpenTe
1dc94 6d 70 2e 20 20 42 75 74 20 74 68 61 74 20 63 72 mp. But that cr
1dc95 65 61 74 65 64 0a 2a 2a 20 63 6f 6e 66 75 73 69 eated.** confusi
1dc96 6f 6e 20 62 65 63 61 75 73 65 20 74 68 65 20 74 on because the t
1dc97 65 72 6d 20 22 74 65 6d 70 20 74 61 62 6c 65 22 erm "temp table"
1dc98 2c 20 6d 69 67 68 74 20 72 65 66 65 72 20 65 69 , might refer ei
1dc99 74 68 65 72 0a 2a 2a 20 74 6f 20 61 20 54 45 4d ther.** to a TEM
1dc9a 50 20 74 61 62 6c 65 20 61 74 20 74 68 65 20 53 P table at the S
1dc9b 51 4c 20 6c 65 76 65 6c 2c 20 6f 72 20 74 6f 20 QL level, or to
1dc9c 61 20 74 61 62 6c 65 20 6f 70 65 6e 65 64 20 62 a table opened b
1dc9d 79 0a 2a 2a 20 74 68 69 73 20 6f 70 63 6f 64 65 y.** this opcode
1dc9e 2e 20 20 54 68 65 6e 20 74 68 69 73 20 6f 70 63 . Then this opc
1dc9f 6f 64 65 20 77 61 73 20 63 61 6c 6c 20 4f 70 65 ode was call Ope
1dca0 6e 56 69 72 74 75 61 6c 2e 20 20 42 75 74 0a 2a nVirtual. But.*
1dca1 2a 20 74 68 61 74 20 63 72 65 61 74 65 64 20 63 * that created c
1dca2 6f 6e 66 75 73 69 6f 6e 20 77 69 74 68 20 74 68 onfusion with th
1dca3 65 20 77 68 6f 6c 65 20 76 69 72 74 75 61 6c 2d e whole virtual-
1dca4 74 61 62 6c 65 20 69 64 65 61 2e 0a 2a 2f 0a 63 table idea..*/.c
1dca5 61 73 65 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d ase OP_OpenEphem
1dca6 65 72 61 6c 3a 20 7b 0a 23 69 66 20 30 20 20 2f eral: {.#if 0 /
1dca7 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 * local variable
1dca8 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 s moved into u.a
1dca9 78 20 2a 2f 0a 20 20 56 64 62 65 43 75 72 73 6f x */. VdbeCurso
1dcaa 72 20 2a 70 43 78 3b 0a 23 65 6e 64 69 66 20 2f r *pCx;.#endif /
1dcab 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 * local variable
1dcac 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 s moved into u.a
1dcad 78 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 63 6f x */. static co
1dcae 6e 73 74 20 69 6e 74 20 6f 70 65 6e 46 6c 61 67 nst int openFlag
1dcaf 73 20 3d 0a 20 20 20 20 20 20 53 51 4c 49 54 45 s =. SQLITE
1dcb0 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20 _OPEN_READWRITE
1dcb1 7c 0a 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f |. SQLITE_O
1dcb2 50 45 4e 5f 43 52 45 41 54 45 20 7c 0a 20 20 20 PEN_CREATE |.
1dcb3 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 SQLITE_OPEN_E
1dcb4 58 43 4c 55 53 49 56 45 20 7c 0a 20 20 20 20 20 XCLUSIVE |.
1dcb5 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c SQLITE_OPEN_DEL
1dcb6 45 54 45 4f 4e 43 4c 4f 53 45 20 7c 0a 20 20 20 ETEONCLOSE |.
1dcb7 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 SQLITE_OPEN_T
1dcb8 52 41 4e 53 49 45 4e 54 5f 44 42 3b 0a 0a 20 20 RANSIENT_DB;..
1dcb9 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e assert( pOp->p1>
1dcba 3d 30 20 29 3b 0a 20 20 75 2e 61 78 2e 70 43 78 =0 );. u.ax.pCx
1dcbb 20 3d 20 61 6c 6c 6f 63 61 74 65 43 75 72 73 6f = allocateCurso
1dcbc 72 28 70 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 4f r(p, pOp->p1, pO
1dcbd 70 2d 3e 70 32 2c 20 2d 31 2c 20 31 29 3b 0a 20 p->p2, -1, 1);.
1dcbe 20 69 66 28 20 75 2e 61 78 2e 70 43 78 3d 3d 30 if( u.ax.pCx==0
1dcbf 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a ) goto no_mem;.
1dcc0 20 20 75 2e 61 78 2e 70 43 78 2d 3e 6e 75 6c 6c u.ax.pCx->null
1dcc1 52 6f 77 20 3d 20 31 3b 0a 20 20 72 63 20 3d 20 Row = 1;. rc =
1dcc2 73 71 6c 69 74 65 33 42 74 72 65 65 46 61 63 74 sqlite3BtreeFact
1dcc3 6f 72 79 28 64 62 2c 20 30 2c 20 31 2c 20 53 51 ory(db, 0, 1, SQ
1dcc4 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 54 45 4d LITE_DEFAULT_TEM
1dcc5 50 5f 43 41 43 48 45 5f 53 49 5a 45 2c 20 6f 70 P_CACHE_SIZE, op
1dcc6 65 6e 46 6c 61 67 73 2c 0a 20 20 20 20 20 20 20 enFlags,.
1dcc7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1dcc8 20 20 20 20 26 75 2e 61 78 2e 70 43 78 2d 3e 70 &u.ax.pCx->p
1dcc9 42 74 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 Bt);. if( rc==S
1dcca 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
1dccb 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 rc = sqlite3Btre
1dccc 65 42 65 67 69 6e 54 72 61 6e 73 28 75 2e 61 78 eBeginTrans(u.ax
1dccd 2e 70 43 78 2d 3e 70 42 74 2c 20 31 29 3b 0a 20 .pCx->pBt, 1);.
1dcce 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c }. if( rc==SQL
1dccf 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2f 2a ITE_OK ){. /*
1dcd0 20 49 66 20 61 20 74 72 61 6e 73 69 65 6e 74 20 If a transient
1dcd1 69 6e 64 65 78 20 69 73 20 72 65 71 75 69 72 65 index is require
1dcd2 64 2c 20 63 72 65 61 74 65 20 69 74 20 62 79 20 d, create it by
1dcd3 63 61 6c 6c 69 6e 67 0a 20 20 20 20 2a 2a 20 73 calling. ** s
1dcd4 71 6c 69 74 65 33 42 74 72 65 65 43 72 65 61 74 qlite3BtreeCreat
1dcd5 65 54 61 62 6c 65 28 29 20 77 69 74 68 20 74 68 eTable() with th
1dcd6 65 20 42 54 52 45 45 5f 5a 45 52 4f 44 41 54 41 e BTREE_ZERODATA
1dcd7 20 66 6c 61 67 20 62 65 66 6f 72 65 0a 20 20 20 flag before.
1dcd8 20 2a 2a 20 6f 70 65 6e 69 6e 67 20 69 74 2e 20 ** opening it.
1dcd9 49 66 20 61 20 74 72 61 6e 73 69 65 6e 74 20 74 If a transient t
1dcda 61 62 6c 65 20 69 73 20 72 65 71 75 69 72 65 64 able is required
1dcdb 2c 20 6a 75 73 74 20 75 73 65 20 74 68 65 0a 20 , just use the.
1dcdc 20 20 20 2a 2a 20 61 75 74 6f 6d 61 74 69 63 61 ** automatica
1dcdd 6c 6c 79 20 63 72 65 61 74 65 64 20 74 61 62 6c lly created tabl
1dcde 65 20 77 69 74 68 20 72 6f 6f 74 2d 70 61 67 65 e with root-page
1dcdf 20 31 20 28 61 6e 20 49 4e 54 4b 45 59 20 74 61 1 (an INTKEY ta
1dce0 62 6c 65 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 ble).. */.
1dce1 20 69 66 28 20 70 4f 70 2d 3e 70 34 2e 70 4b 65 if( pOp->p4.pKe
1dce2 79 49 6e 66 6f 20 29 7b 0a 20 20 20 20 20 20 69 yInfo ){. i
1dce3 6e 74 20 70 67 6e 6f 3b 0a 20 20 20 20 20 20 61 nt pgno;. a
1dce4 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 ssert( pOp->p4ty
1dce5 70 65 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f 20 29 pe==P4_KEYINFO )
1dce6 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c ;. rc = sql
1dce7 69 74 65 33 42 74 72 65 65 43 72 65 61 74 65 54 ite3BtreeCreateT
1dce8 61 62 6c 65 28 75 2e 61 78 2e 70 43 78 2d 3e 70 able(u.ax.pCx->p
1dce9 42 74 2c 20 26 70 67 6e 6f 2c 20 42 54 52 45 45 Bt, &pgno, BTREE
1dcea 5f 5a 45 52 4f 44 41 54 41 29 3b 0a 20 20 20 20 _ZERODATA);.
1dceb 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
1dcec 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 61 _OK ){. a
1dced 73 73 65 72 74 28 20 70 67 6e 6f 3d 3d 4d 41 53 ssert( pgno==MAS
1dcee 54 45 52 5f 52 4f 4f 54 2b 31 20 29 3b 0a 20 20 TER_ROOT+1 );.
1dcef 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
1dcf0 65 33 42 74 72 65 65 43 75 72 73 6f 72 28 75 2e e3BtreeCursor(u.
1dcf1 61 78 2e 70 43 78 2d 3e 70 42 74 2c 20 70 67 6e ax.pCx->pBt, pgn
1dcf2 6f 2c 20 31 2c 0a 20 20 20 20 20 20 20 20 20 20 o, 1,.
1dcf3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1dcf4 20 20 20 20 20 20 28 4b 65 79 49 6e 66 6f 2a 29 (KeyInfo*)
1dcf5 70 4f 70 2d 3e 70 34 2e 7a 2c 20 75 2e 61 78 2e pOp->p4.z, u.ax.
1dcf6 70 43 78 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20 pCx->pCursor);.
1dcf7 20 20 20 20 20 20 20 75 2e 61 78 2e 70 43 78 2d u.ax.pCx-
1dcf8 3e 70 4b 65 79 49 6e 66 6f 20 3d 20 70 4f 70 2d >pKeyInfo = pOp-
1dcf9 3e 70 34 2e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 >p4.pKeyInfo;.
1dcfa 20 20 20 20 20 20 75 2e 61 78 2e 70 43 78 2d 3e u.ax.pCx->
1dcfb 70 4b 65 79 49 6e 66 6f 2d 3e 65 6e 63 20 3d 20 pKeyInfo->enc =
1dcfc 45 4e 43 28 70 2d 3e 64 62 29 3b 0a 20 20 20 20 ENC(p->db);.
1dcfd 20 20 7d 0a 20 20 20 20 20 20 75 2e 61 78 2e 70 }. u.ax.p
1dcfe 43 78 2d 3e 69 73 54 61 62 6c 65 20 3d 20 30 3b Cx->isTable = 0;
1dcff 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
1dd00 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 rc = sqlite3Bt
1dd01 72 65 65 43 75 72 73 6f 72 28 75 2e 61 78 2e 70 reeCursor(u.ax.p
1dd02 43 78 2d 3e 70 42 74 2c 20 4d 41 53 54 45 52 5f Cx->pBt, MASTER_
1dd03 52 4f 4f 54 2c 20 31 2c 20 30 2c 20 75 2e 61 78 ROOT, 1, 0, u.ax
1dd04 2e 70 43 78 2d 3e 70 43 75 72 73 6f 72 29 3b 0a .pCx->pCursor);.
1dd05 20 20 20 20 20 20 75 2e 61 78 2e 70 43 78 2d 3e u.ax.pCx->
1dd06 69 73 54 61 62 6c 65 20 3d 20 31 3b 0a 20 20 20 isTable = 1;.
1dd07 20 7d 0a 20 20 7d 0a 20 20 75 2e 61 78 2e 70 43 }. }. u.ax.pC
1dd08 78 2d 3e 69 73 49 6e 64 65 78 20 3d 20 21 75 2e x->isIndex = !u.
1dd09 61 78 2e 70 43 78 2d 3e 69 73 54 61 62 6c 65 3b ax.pCx->isTable;
1dd0a 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 . break;.}../*
1dd0b 4f 70 63 6f 64 65 3a 20 4f 70 65 6e 50 73 65 75 Opcode: OpenPseu
1dd0c 64 6f 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a do P1 P2 P3 * *.
1dd0d 2a 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 6e 65 77 **.** Open a new
1dd0e 20 63 75 72 73 6f 72 20 74 68 61 74 20 70 6f 69 cursor that poi
1dd0f 6e 74 73 20 74 6f 20 61 20 66 61 6b 65 20 74 61 nts to a fake ta
1dd10 62 6c 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e ble that contain
1dd11 73 20 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 72 6f s a single.** ro
1dd12 77 20 6f 66 20 64 61 74 61 2e 20 20 54 68 65 20 w of data. The
1dd13 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 61 74 20 content of that
1dd14 6f 6e 65 20 72 6f 77 20 69 6e 20 74 68 65 20 63 one row in the c
1dd15 6f 6e 74 65 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 ontent of memory
1dd16 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 50 32 2e .** register P2.
1dd17 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 In other words
1dd18 2c 20 63 75 72 73 6f 72 20 50 31 20 62 65 63 6f , cursor P1 beco
1dd19 6d 65 73 20 61 6e 20 61 6c 69 61 73 20 66 6f 72 mes an alias for
1dd1a 20 74 68 65 20 0a 2a 2a 20 4d 45 4d 5f 42 6c 6f the .** MEM_Blo
1dd1b 62 20 63 6f 6e 74 65 6e 74 20 63 6f 6e 74 61 69 b content contai
1dd1c 6e 65 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 ned in register
1dd1d 50 32 2e 0a 2a 2a 0a 2a 2a 20 41 20 70 73 65 75 P2..**.** A pseu
1dd1e 64 6f 2d 74 61 62 6c 65 20 63 72 65 61 74 65 64 do-table created
1dd1f 20 62 79 20 74 68 69 73 20 6f 70 63 6f 64 65 20 by this opcode
1dd20 69 73 20 75 73 65 64 20 74 6f 20 68 6f 6c 64 20 is used to hold
1dd21 74 68 65 20 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 the a single.**
1dd22 72 6f 77 20 6f 75 74 70 75 74 20 66 72 6f 6d 20 row output from
1dd23 74 68 65 20 73 6f 72 74 65 72 20 73 6f 20 74 68 the sorter so th
1dd24 61 74 20 74 68 65 20 72 6f 77 20 63 61 6e 20 62 at the row can b
1dd25 65 20 64 65 63 6f 6d 70 6f 73 65 64 20 69 6e 74 e decomposed int
1dd26 6f 0a 2a 2a 20 69 6e 64 69 76 69 64 75 61 6c 20 o.** individual
1dd27 63 6f 6c 75 6d 6e 73 20 75 73 69 6e 67 20 74 68 columns using th
1dd28 65 20 4f 50 5f 43 6f 6c 75 6d 6e 20 6f 70 63 6f e OP_Column opco
1dd29 64 65 2e 20 20 54 68 65 20 4f 50 5f 43 6f 6c 75 de. The OP_Colu
1dd2a 6d 6e 20 6f 70 63 6f 64 65 0a 2a 2a 20 69 73 20 mn opcode.** is
1dd2b 74 68 65 20 6f 6e 6c 79 20 63 75 72 73 6f 72 20 the only cursor
1dd2c 6f 70 63 6f 64 65 20 74 68 61 74 20 77 6f 72 6b opcode that work
1dd2d 73 20 77 69 74 68 20 61 20 70 73 65 75 64 6f 2d s with a pseudo-
1dd2e 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 50 33 20 table..**.** P3
1dd2f 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 is the number of
1dd30 20 66 69 65 6c 64 73 20 69 6e 20 74 68 65 20 72 fields in the r
1dd31 65 63 6f 72 64 73 20 74 68 61 74 20 77 69 6c 6c ecords that will
1dd32 20 62 65 20 73 74 6f 72 65 64 20 62 79 0a 2a 2a be stored by.**
1dd33 20 74 68 65 20 70 73 65 75 64 6f 2d 74 61 62 6c the pseudo-tabl
1dd34 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4f 70 e..*/.case OP_Op
1dd35 65 6e 50 73 65 75 64 6f 3a 20 7b 0a 23 69 66 20 enPseudo: {.#if
1dd36 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 0 /* local vari
1dd37 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1dd38 20 75 2e 61 79 20 2a 2f 0a 20 20 56 64 62 65 43 u.ay */. VdbeC
1dd39 75 72 73 6f 72 20 2a 70 43 78 3b 0a 23 65 6e 64 ursor *pCx;.#end
1dd3a 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 if /* local vari
1dd3b 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1dd3c 20 75 2e 61 79 20 2a 2f 0a 0a 20 20 61 73 73 65 u.ay */.. asse
1dd3d 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 29 rt( pOp->p1>=0 )
1dd3e 3b 0a 20 20 75 2e 61 79 2e 70 43 78 20 3d 20 61 ;. u.ay.pCx = a
1dd3f 6c 6c 6f 63 61 74 65 43 75 72 73 6f 72 28 70 2c llocateCursor(p,
1dd40 20 70 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70 pOp->p1, pOp->p
1dd41 33 2c 20 2d 31 2c 20 30 29 3b 0a 20 20 69 66 28 3, -1, 0);. if(
1dd42 20 75 2e 61 79 2e 70 43 78 3d 3d 30 20 29 20 67 u.ay.pCx==0 ) g
1dd43 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 75 2e oto no_mem;. u.
1dd44 61 79 2e 70 43 78 2d 3e 6e 75 6c 6c 52 6f 77 20 ay.pCx->nullRow
1dd45 3d 20 31 3b 0a 20 20 75 2e 61 79 2e 70 43 78 2d = 1;. u.ay.pCx-
1dd46 3e 70 73 65 75 64 6f 54 61 62 6c 65 52 65 67 20 >pseudoTableReg
1dd47 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 75 2e 61 = pOp->p2;. u.a
1dd48 79 2e 70 43 78 2d 3e 69 73 54 61 62 6c 65 20 3d y.pCx->isTable =
1dd49 20 31 3b 0a 20 20 75 2e 61 79 2e 70 43 78 2d 3e 1;. u.ay.pCx->
1dd4a 69 73 49 6e 64 65 78 20 3d 20 30 3b 0a 20 20 62 isIndex = 0;. b
1dd4b 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1dd4c 64 65 3a 20 43 6c 6f 73 65 20 50 31 20 2a 20 2a de: Close P1 * *
1dd4d 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 43 6c 6f 73 65 * *.**.** Close
1dd4e 20 61 20 63 75 72 73 6f 72 20 70 72 65 76 69 6f a cursor previo
1dd4f 75 73 6c 79 20 6f 70 65 6e 65 64 20 61 73 20 50 usly opened as P
1dd50 31 2e 20 20 49 66 20 50 31 20 69 73 20 6e 6f 74 1. If P1 is not
1dd51 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 6f 70 .** currently op
1dd52 65 6e 2c 20 74 68 69 73 20 69 6e 73 74 72 75 63 en, this instruc
1dd53 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e tion is a no-op.
1dd54 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 6c 6f 73 .*/.case OP_Clos
1dd55 65 3a 20 7b 0a 20 20 61 73 73 65 72 74 28 20 70 e: {. assert( p
1dd56 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 Op->p1>=0 && pOp
1dd57 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 ->p1<p->nCursor
1dd58 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 );. sqlite3Vdbe
1dd59 46 72 65 65 43 75 72 73 6f 72 28 70 2c 20 70 2d FreeCursor(p, p-
1dd5a 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 29 >apCsr[pOp->p1])
1dd5b 3b 0a 20 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 ;. p->apCsr[pOp
1dd5c 2d 3e 70 31 5d 20 3d 20 30 3b 0a 20 20 62 72 65 ->p1] = 0;. bre
1dd5d 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 ak;.}../* Opcode
1dd5e 3a 20 53 65 65 6b 47 65 20 50 31 20 50 32 20 50 : SeekGe P1 P2 P
1dd5f 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 3 P4 *.**.** If
1dd60 63 75 72 73 6f 72 20 50 31 20 72 65 66 65 72 73 cursor P1 refers
1dd61 20 74 6f 20 61 6e 20 53 51 4c 20 74 61 62 6c 65 to an SQL table
1dd62 20 28 42 2d 54 72 65 65 20 74 68 61 74 20 75 73 (B-Tree that us
1dd63 65 73 20 69 6e 74 65 67 65 72 20 6b 65 79 73 29 es integer keys)
1dd64 2c 20 0a 2a 2a 20 75 73 65 20 74 68 65 20 76 61 , .** use the va
1dd65 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 lue in register
1dd66 50 33 20 61 73 20 74 68 65 20 6b 65 79 2e 20 20 P3 as the key.
1dd67 49 66 20 63 75 72 73 6f 72 20 50 31 20 72 65 66 If cursor P1 ref
1dd68 65 72 73 20 0a 2a 2a 20 74 6f 20 61 6e 20 53 51 ers .** to an SQ
1dd69 4c 20 69 6e 64 65 78 2c 20 74 68 65 6e 20 50 33 L index, then P3
1dd6a 20 69 73 20 74 68 65 20 66 69 72 73 74 20 69 6e is the first in
1dd6b 20 61 6e 20 61 72 72 61 79 20 6f 66 20 50 34 20 an array of P4
1dd6c 72 65 67 69 73 74 65 72 73 20 0a 2a 2a 20 74 68 registers .** th
1dd6d 61 74 20 61 72 65 20 75 73 65 64 20 61 73 20 61 at are used as a
1dd6e 6e 20 75 6e 70 61 63 6b 65 64 20 69 6e 64 65 78 n unpacked index
1dd6f 20 6b 65 79 2e 20 0a 2a 2a 0a 2a 2a 20 52 65 70 key. .**.** Rep
1dd70 6f 73 69 74 69 6f 6e 20 63 75 72 73 6f 72 20 50 osition cursor P
1dd71 31 20 73 6f 20 74 68 61 74 20 20 69 74 20 70 6f 1 so that it po
1dd72 69 6e 74 73 20 74 6f 20 74 68 65 20 73 6d 61 6c ints to the smal
1dd73 6c 65 73 74 20 65 6e 74 72 79 20 74 68 61 74 20 lest entry that
1dd74 0a 2a 2a 20 69 73 20 67 72 65 61 74 65 72 20 74 .** is greater t
1dd75 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 han or equal to
1dd76 74 68 65 20 6b 65 79 20 76 61 6c 75 65 2e 20 49 the key value. I
1dd77 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 72 f there are no r
1dd78 65 63 6f 72 64 73 20 0a 2a 2a 20 67 72 65 61 74 ecords .** great
1dd79 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c er than or equal
1dd7a 20 74 6f 20 74 68 65 20 6b 65 79 20 61 6e 64 20 to the key and
1dd7b 50 32 20 69 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 P2 is not zero,
1dd7c 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e then jump to P2.
1dd7d 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a .**.** See also:
1dd7e 20 46 6f 75 6e 64 2c 20 4e 6f 74 46 6f 75 6e 64 Found, NotFound
1dd7f 2c 20 44 69 73 74 69 6e 63 74 2c 20 53 65 65 6b , Distinct, Seek
1dd80 4c 74 2c 20 53 65 65 6b 47 74 2c 20 53 65 65 6b Lt, SeekGt, Seek
1dd81 4c 65 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a Le.*/./* Opcode:
1dd82 20 53 65 65 6b 47 74 20 50 31 20 50 32 20 50 33 SeekGt P1 P2 P3
1dd83 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 63 P4 *.**.** If c
1dd84 75 72 73 6f 72 20 50 31 20 72 65 66 65 72 73 20 ursor P1 refers
1dd85 74 6f 20 61 6e 20 53 51 4c 20 74 61 62 6c 65 20 to an SQL table
1dd86 28 42 2d 54 72 65 65 20 74 68 61 74 20 75 73 65 (B-Tree that use
1dd87 73 20 69 6e 74 65 67 65 72 20 6b 65 79 73 29 2c s integer keys),
1dd88 20 0a 2a 2a 20 75 73 65 20 74 68 65 20 76 61 6c .** use the val
1dd89 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 ue in register P
1dd8a 33 20 61 73 20 61 20 6b 65 79 2e 20 49 66 20 63 3 as a key. If c
1dd8b 75 72 73 6f 72 20 50 31 20 72 65 66 65 72 73 20 ursor P1 refers
1dd8c 0a 2a 2a 20 74 6f 20 61 6e 20 53 51 4c 20 69 6e .** to an SQL in
1dd8d 64 65 78 2c 20 74 68 65 6e 20 50 33 20 69 73 20 dex, then P3 is
1dd8e 74 68 65 20 66 69 72 73 74 20 69 6e 20 61 6e 20 the first in an
1dd8f 61 72 72 61 79 20 6f 66 20 50 34 20 72 65 67 69 array of P4 regi
1dd90 73 74 65 72 73 20 0a 2a 2a 20 74 68 61 74 20 61 sters .** that a
1dd91 72 65 20 75 73 65 64 20 61 73 20 61 6e 20 75 6e re used as an un
1dd92 70 61 63 6b 65 64 20 69 6e 64 65 78 20 6b 65 79 packed index key
1dd93 2e 20 0a 2a 2a 0a 2a 2a 20 52 65 70 6f 73 69 74 . .**.** Reposit
1dd94 69 6f 6e 20 63 75 72 73 6f 72 20 50 31 20 73 6f ion cursor P1 so
1dd95 20 74 68 61 74 20 20 69 74 20 70 6f 69 6e 74 73 that it points
1dd96 20 74 6f 20 74 68 65 20 73 6d 61 6c 6c 65 73 74 to the smallest
1dd97 20 65 6e 74 72 79 20 74 68 61 74 20 0a 2a 2a 20 entry that .**
1dd98 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 is greater than
1dd99 74 68 65 20 6b 65 79 20 76 61 6c 75 65 2e 20 49 the key value. I
1dd9a 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 72 f there are no r
1dd9b 65 63 6f 72 64 73 20 67 72 65 61 74 65 72 20 74 ecords greater t
1dd9c 68 61 6e 20 0a 2a 2a 20 74 68 65 20 6b 65 79 20 han .** the key
1dd9d 61 6e 64 20 50 32 20 69 73 20 6e 6f 74 20 7a 65 and P2 is not ze
1dd9e 72 6f 2c 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f ro, then jump to
1dd9f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 P2..**.** See a
1dda0 6c 73 6f 3a 20 46 6f 75 6e 64 2c 20 4e 6f 74 46 lso: Found, NotF
1dda1 6f 75 6e 64 2c 20 44 69 73 74 69 6e 63 74 2c 20 ound, Distinct,
1dda2 53 65 65 6b 4c 74 2c 20 53 65 65 6b 47 65 2c 20 SeekLt, SeekGe,
1dda3 53 65 65 6b 4c 65 0a 2a 2f 0a 2f 2a 20 4f 70 63 SeekLe.*/./* Opc
1dda4 6f 64 65 3a 20 53 65 65 6b 4c 74 20 50 31 20 50 ode: SeekLt P1 P
1dda5 32 20 50 33 20 50 34 20 2a 20 0a 2a 2a 0a 2a 2a 2 P3 P4 * .**.**
1dda6 20 49 66 20 63 75 72 73 6f 72 20 50 31 20 72 65 If cursor P1 re
1dda7 66 65 72 73 20 74 6f 20 61 6e 20 53 51 4c 20 74 fers to an SQL t
1dda8 61 62 6c 65 20 28 42 2d 54 72 65 65 20 74 68 61 able (B-Tree tha
1dda9 74 20 75 73 65 73 20 69 6e 74 65 67 65 72 20 6b t uses integer k
1ddaa 65 79 73 29 2c 20 0a 2a 2a 20 75 73 65 20 74 68 eys), .** use th
1ddab 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 e value in regis
1ddac 74 65 72 20 50 33 20 61 73 20 61 20 6b 65 79 2e ter P3 as a key.
1ddad 20 49 66 20 63 75 72 73 6f 72 20 50 31 20 72 65 If cursor P1 re
1ddae 66 65 72 73 20 0a 2a 2a 20 74 6f 20 61 6e 20 53 fers .** to an S
1ddaf 51 4c 20 69 6e 64 65 78 2c 20 74 68 65 6e 20 50 QL index, then P
1ddb0 33 20 69 73 20 74 68 65 20 66 69 72 73 74 20 69 3 is the first i
1ddb1 6e 20 61 6e 20 61 72 72 61 79 20 6f 66 20 50 34 n an array of P4
1ddb2 20 72 65 67 69 73 74 65 72 73 20 0a 2a 2a 20 74 registers .** t
1ddb3 68 61 74 20 61 72 65 20 75 73 65 64 20 61 73 20 hat are used as
1ddb4 61 6e 20 75 6e 70 61 63 6b 65 64 20 69 6e 64 65 an unpacked inde
1ddb5 78 20 6b 65 79 2e 20 0a 2a 2a 0a 2a 2a 20 52 65 x key. .**.** Re
1ddb6 70 6f 73 69 74 69 6f 6e 20 63 75 72 73 6f 72 20 position cursor
1ddb7 50 31 20 73 6f 20 74 68 61 74 20 20 69 74 20 70 P1 so that it p
1ddb8 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 6c 61 72 oints to the lar
1ddb9 67 65 73 74 20 65 6e 74 72 79 20 74 68 61 74 20 gest entry that
1ddba 0a 2a 2a 20 69 73 20 6c 65 73 73 20 74 68 61 6e .** is less than
1ddbb 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65 2e 20 the key value.
1ddbc 49 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 If there are no
1ddbd 72 65 63 6f 72 64 73 20 6c 65 73 73 20 74 68 61 records less tha
1ddbe 6e 20 0a 2a 2a 20 74 68 65 20 6b 65 79 20 61 6e n .** the key an
1ddbf 64 20 50 32 20 69 73 20 6e 6f 74 20 7a 65 72 6f d P2 is not zero
1ddc0 2c 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 50 , then jump to P
1ddc1 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 2..**.** See als
1ddc2 6f 3a 20 46 6f 75 6e 64 2c 20 4e 6f 74 46 6f 75 o: Found, NotFou
1ddc3 6e 64 2c 20 44 69 73 74 69 6e 63 74 2c 20 53 65 nd, Distinct, Se
1ddc4 65 6b 47 74 2c 20 53 65 65 6b 47 65 2c 20 53 65 ekGt, SeekGe, Se
1ddc5 65 6b 4c 65 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 ekLe.*/./* Opcod
1ddc6 65 3a 20 53 65 65 6b 4c 65 20 50 31 20 50 32 20 e: SeekLe P1 P2
1ddc7 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 P3 P4 *.**.** If
1ddc8 20 63 75 72 73 6f 72 20 50 31 20 72 65 66 65 72 cursor P1 refer
1ddc9 73 20 74 6f 20 61 6e 20 53 51 4c 20 74 61 62 6c s to an SQL tabl
1ddca 65 20 28 42 2d 54 72 65 65 20 74 68 61 74 20 75 e (B-Tree that u
1ddcb 73 65 73 20 69 6e 74 65 67 65 72 20 6b 65 79 73 ses integer keys
1ddcc 29 2c 20 0a 2a 2a 20 75 73 65 20 74 68 65 20 76 ), .** use the v
1ddcd 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 alue in register
1ddce 20 50 33 20 61 73 20 61 20 6b 65 79 2e 20 49 66 P3 as a key. If
1ddcf 20 63 75 72 73 6f 72 20 50 31 20 72 65 66 65 72 cursor P1 refer
1ddd0 73 20 0a 2a 2a 20 74 6f 20 61 6e 20 53 51 4c 20 s .** to an SQL
1ddd1 69 6e 64 65 78 2c 20 74 68 65 6e 20 50 33 20 69 index, then P3 i
1ddd2 73 20 74 68 65 20 66 69 72 73 74 20 69 6e 20 61 s the first in a
1ddd3 6e 20 61 72 72 61 79 20 6f 66 20 50 34 20 72 65 n array of P4 re
1ddd4 67 69 73 74 65 72 73 20 0a 2a 2a 20 74 68 61 74 gisters .** that
1ddd5 20 61 72 65 20 75 73 65 64 20 61 73 20 61 6e 20 are used as an
1ddd6 75 6e 70 61 63 6b 65 64 20 69 6e 64 65 78 20 6b unpacked index k
1ddd7 65 79 2e 20 0a 2a 2a 0a 2a 2a 20 52 65 70 6f 73 ey. .**.** Repos
1ddd8 69 74 69 6f 6e 20 63 75 72 73 6f 72 20 50 31 20 ition cursor P1
1ddd9 73 6f 20 74 68 61 74 20 69 74 20 70 6f 69 6e 74 so that it point
1ddda 73 20 74 6f 20 74 68 65 20 6c 61 72 67 65 73 74 s to the largest
1dddb 20 65 6e 74 72 79 20 74 68 61 74 20 0a 2a 2a 20 entry that .**
1dddc 69 73 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 is less than or
1dddd 65 71 75 61 6c 20 74 6f 20 74 68 65 20 6b 65 79 equal to the key
1ddde 20 76 61 6c 75 65 2e 20 49 66 20 74 68 65 72 65 value. If there
1dddf 20 61 72 65 20 6e 6f 20 72 65 63 6f 72 64 73 20 are no records
1dde0 0a 2a 2a 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 .** less than or
1dde1 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 6b 65 equal to the ke
1dde2 79 20 61 6e 64 20 50 32 20 69 73 20 6e 6f 74 20 y and P2 is not
1dde3 7a 65 72 6f 2c 20 74 68 65 6e 20 6a 75 6d 70 20 zero, then jump
1dde4 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 to P2..**.** See
1dde5 20 61 6c 73 6f 3a 20 46 6f 75 6e 64 2c 20 4e 6f also: Found, No
1dde6 74 46 6f 75 6e 64 2c 20 44 69 73 74 69 6e 63 74 tFound, Distinct
1dde7 2c 20 53 65 65 6b 47 74 2c 20 53 65 65 6b 47 65 , SeekGt, SeekGe
1dde8 2c 20 53 65 65 6b 4c 74 0a 2a 2f 0a 63 61 73 65 , SeekLt.*/.case
1dde9 20 4f 50 5f 53 65 65 6b 4c 74 3a 20 20 20 20 20 OP_SeekLt:
1ddea 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 /* jump, in3
1ddeb 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65 65 6b */.case OP_Seek
1ddec 4c 65 3a 20 20 20 20 20 20 20 20 20 2f 2a 20 6a Le: /* j
1dded 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a 63 61 73 65 ump, in3 */.case
1ddee 20 4f 50 5f 53 65 65 6b 47 65 3a 20 20 20 20 20 OP_SeekGe:
1ddef 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 /* jump, in3
1ddf0 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65 65 6b */.case OP_Seek
1ddf1 47 74 3a 20 7b 20 20 20 20 20 20 20 2f 2a 20 6a Gt: { /* j
1ddf2 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a 23 69 66 20 ump, in3 */.#if
1ddf3 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 0 /* local vari
1ddf4 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1ddf5 20 75 2e 61 7a 20 2a 2f 0a 20 20 69 6e 74 20 72 u.az */. int r
1ddf6 65 73 3b 0a 20 20 69 6e 74 20 6f 63 3b 0a 20 20 es;. int oc;.
1ddf7 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a VdbeCursor *pC;.
1ddf8 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 UnpackedRecord
1ddf9 20 72 3b 0a 20 20 69 6e 74 20 6e 46 69 65 6c 64 r;. int nField
1ddfa 3b 0a 20 20 69 36 34 20 69 4b 65 79 3b 20 20 20 ;. i64 iKey;
1ddfb 20 20 20 2f 2a 20 54 68 65 20 72 6f 77 69 64 20 /* The rowid
1ddfc 77 65 20 61 72 65 20 74 6f 20 73 65 65 6b 20 74 we are to seek t
1ddfd 6f 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 6c o */.#endif /* l
1ddfe 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1ddff 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 7a 20 2a oved into u.az *
1de00 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 /.. assert( pOp
1de01 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e ->p1>=0 && pOp->
1de02 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b p1<p->nCursor );
1de03 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e . assert( pOp->
1de04 70 32 21 3d 30 20 29 3b 0a 20 20 75 2e 61 7a 2e p2!=0 );. u.az.
1de05 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f pC = p->apCsr[pO
1de06 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 p->p1];. assert
1de07 28 20 75 2e 61 7a 2e 70 43 21 3d 30 20 29 3b 0a ( u.az.pC!=0 );.
1de08 20 20 61 73 73 65 72 74 28 20 75 2e 61 7a 2e 70 assert( u.az.p
1de09 43 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65 52 65 C->pseudoTableRe
1de0a 67 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 75 2e g==0 );. if( u.
1de0b 61 7a 2e 70 43 2d 3e 70 43 75 72 73 6f 72 21 3d az.pC->pCursor!=
1de0c 30 20 29 7b 0a 20 20 20 20 75 2e 61 7a 2e 6f 63 0 ){. u.az.oc
1de0d 20 3d 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3b 0a = pOp->opcode;.
1de0e 20 20 20 20 75 2e 61 7a 2e 70 43 2d 3e 6e 75 6c u.az.pC->nul
1de0f 6c 52 6f 77 20 3d 20 30 3b 0a 20 20 20 20 69 66 lRow = 0;. if
1de10 28 20 75 2e 61 7a 2e 70 43 2d 3e 69 73 54 61 62 ( u.az.pC->isTab
1de11 6c 65 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 le ){. /* T
1de12 68 65 20 69 6e 70 75 74 20 76 61 6c 75 65 20 69 he input value i
1de13 6e 20 50 33 20 6d 69 67 68 74 20 62 65 20 6f 66 n P3 might be of
1de14 20 61 6e 79 20 74 79 70 65 3a 20 69 6e 74 65 67 any type: integ
1de15 65 72 2c 20 72 65 61 6c 2c 20 73 74 72 69 6e 67 er, real, string
1de16 2c 0a 20 20 20 20 20 20 2a 2a 20 62 6c 6f 62 2c ,. ** blob,
1de17 20 6f 72 20 4e 55 4c 4c 2e 20 20 42 75 74 20 69 or NULL. But i
1de18 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 61 6e t needs to be an
1de19 20 69 6e 74 65 67 65 72 20 62 65 66 6f 72 65 20 integer before
1de1a 77 65 20 63 61 6e 20 64 6f 0a 20 20 20 20 20 20 we can do.
1de1b 2a 2a 20 74 68 65 20 73 65 65 6b 2c 20 73 6f 20 ** the seek, so
1de1c 63 6f 76 65 72 74 20 69 74 2e 20 2a 2f 0a 20 20 covert it. */.
1de1d 20 20 20 20 61 70 70 6c 79 4e 75 6d 65 72 69 63 applyNumeric
1de1e 41 66 66 69 6e 69 74 79 28 70 49 6e 33 29 3b 0a Affinity(pIn3);.
1de1f 20 20 20 20 20 20 75 2e 61 7a 2e 69 4b 65 79 20 u.az.iKey
1de20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 = sqlite3VdbeInt
1de21 56 61 6c 75 65 28 70 49 6e 33 29 3b 0a 20 20 20 Value(pIn3);.
1de22 20 20 20 75 2e 61 7a 2e 70 43 2d 3e 72 6f 77 69 u.az.pC->rowi
1de23 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 0a 20 dIsValid = 0;..
1de24 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 50 /* If the P
1de25 33 20 76 61 6c 75 65 20 63 6f 75 6c 64 20 6e 6f 3 value could no
1de26 74 20 62 65 20 63 6f 6e 76 65 72 74 65 64 20 69 t be converted i
1de27 6e 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 20 77 nto an integer w
1de28 69 74 68 6f 75 74 0a 20 20 20 20 20 20 2a 2a 20 ithout. **
1de29 6c 6f 73 73 20 6f 66 20 69 6e 66 6f 72 6d 61 74 loss of informat
1de2a 69 6f 6e 2c 20 74 68 65 6e 20 73 70 65 63 69 61 ion, then specia
1de2b 6c 20 70 72 6f 63 65 73 73 69 6e 67 20 69 73 20 l processing is
1de2c 72 65 71 75 69 72 65 64 2e 2e 2e 20 2a 2f 0a 20 required... */.
1de2d 20 20 20 20 20 69 66 28 20 28 70 49 6e 33 2d 3e if( (pIn3->
1de2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 29 flags & MEM_Int)
1de2f 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69 ==0 ){. i
1de30 66 28 20 28 70 49 6e 33 2d 3e 66 6c 61 67 73 20 f( (pIn3->flags
1de31 26 20 4d 45 4d 5f 52 65 61 6c 29 3d 3d 30 20 29 & MEM_Real)==0 )
1de32 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 {. /* I
1de33 66 20 74 68 65 20 50 33 20 76 61 6c 75 65 20 63 f the P3 value c
1de34 61 6e 6e 6f 74 20 62 65 20 63 6f 6e 76 65 72 74 annot be convert
1de35 65 64 20 69 6e 74 6f 20 61 6e 79 20 6b 69 6e 64 ed into any kind
1de36 20 6f 66 20 61 20 6e 75 6d 62 65 72 2c 0a 20 20 of a number,.
1de37 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 ** then
1de38 74 68 65 20 73 65 65 6b 20 69 73 20 6e 6f 74 20 the seek is not
1de39 70 6f 73 73 69 62 6c 65 2c 20 73 6f 20 6a 75 6d possible, so jum
1de3a 70 20 74 6f 20 50 32 20 2a 2f 0a 20 20 20 20 20 p to P2 */.
1de3b 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 pc = pOp->p
1de3c 32 20 2d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 2 - 1;.
1de3d 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 break;.
1de3e 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 }. /* If
1de3f 77 65 20 72 65 61 63 68 20 74 68 69 73 20 70 6f we reach this po
1de40 69 6e 74 2c 20 74 68 65 6e 20 74 68 65 20 50 33 int, then the P3
1de41 20 76 61 6c 75 65 20 6d 75 73 74 20 62 65 20 61 value must be a
1de42 20 66 6c 6f 61 74 69 6e 67 0a 20 20 20 20 20 20 floating.
1de43 20 20 2a 2a 20 70 6f 69 6e 74 20 6e 75 6d 62 65 ** point numbe
1de44 72 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 73 r. */. as
1de45 73 65 72 74 28 20 28 70 49 6e 33 2d 3e 66 6c 61 sert( (pIn3->fla
1de46 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 29 21 3d gs & MEM_Real)!=
1de47 30 20 29 3b 0a 0a 20 20 20 20 20 20 20 20 69 66 0 );.. if
1de48 28 20 75 2e 61 7a 2e 69 4b 65 79 3d 3d 53 4d 41 ( u.az.iKey==SMA
1de49 4c 4c 45 53 54 5f 49 4e 54 36 34 20 26 26 20 28 LLEST_INT64 && (
1de4a 70 49 6e 33 2d 3e 72 3c 28 64 6f 75 62 6c 65 29 pIn3->r<(double)
1de4b 75 2e 61 7a 2e 69 4b 65 79 20 7c 7c 20 70 49 6e u.az.iKey || pIn
1de4c 33 2d 3e 72 3e 30 29 20 29 7b 0a 20 20 20 20 20 3->r>0) ){.
1de4d 20 20 20 20 20 2f 2a 20 54 68 65 20 50 33 20 76 /* The P3 v
1de4e 61 6c 75 65 20 69 73 20 74 6f 6f 20 6c 61 72 67 alue is too larg
1de4f 65 20 69 6e 20 6d 61 67 6e 69 74 75 64 65 20 74 e in magnitude t
1de50 6f 20 62 65 20 65 78 70 72 65 73 73 65 64 20 61 o be expressed a
1de51 73 20 61 6e 0a 20 20 20 20 20 20 20 20 20 20 2a s an. *
1de52 2a 20 69 6e 74 65 67 65 72 2e 20 2a 2f 0a 20 20 * integer. */.
1de53 20 20 20 20 20 20 20 20 75 2e 61 7a 2e 72 65 73 u.az.res
1de54 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 = 1;.
1de55 69 66 28 20 70 49 6e 33 2d 3e 72 3c 30 20 29 7b if( pIn3->r<0 ){
1de56 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 . if(
1de57 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 u.az.oc==OP_See
1de58 6b 47 74 20 7c 7c 20 75 2e 61 7a 2e 6f 63 3d 3d kGt || u.az.oc==
1de59 4f 50 5f 53 65 65 6b 47 65 20 29 7b 0a 20 20 20 OP_SeekGe ){.
1de5a 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 rc =
1de5b 73 71 6c 69 74 65 33 42 74 72 65 65 46 69 72 73 sqlite3BtreeFirs
1de5c 74 28 75 2e 61 7a 2e 70 43 2d 3e 70 43 75 72 73 t(u.az.pC->pCurs
1de5d 6f 72 2c 20 26 75 2e 61 7a 2e 72 65 73 29 3b 0a or, &u.az.res);.
1de5e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66 if
1de5f 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
1de60 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 ) goto abort_due
1de61 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 _to_error;.
1de62 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
1de63 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1de64 20 20 20 20 20 20 69 66 28 20 75 2e 61 7a 2e 6f if( u.az.o
1de65 63 3d 3d 4f 50 5f 53 65 65 6b 4c 74 20 7c 7c 20 c==OP_SeekLt ||
1de66 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b u.az.oc==OP_Seek
1de67 4c 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 Le ){.
1de68 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1de69 42 74 72 65 65 4c 61 73 74 28 75 2e 61 7a 2e 70 BtreeLast(u.az.p
1de6a 43 2d 3e 70 43 75 72 73 6f 72 2c 20 26 75 2e 61 C->pCursor, &u.a
1de6b 7a 2e 72 65 73 29 3b 0a 20 20 20 20 20 20 20 20 z.res);.
1de6c 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 if( rc!=SQ
1de6d 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 61 LITE_OK ) goto a
1de6e 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f bort_due_to_erro
1de6f 72 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d r;. }
1de70 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 . }.
1de71 20 20 20 20 20 20 20 69 66 28 20 75 2e 61 7a 2e if( u.az.
1de72 72 65 73 20 29 7b 0a 20 20 20 20 20 20 20 20 20 res ){.
1de73 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 pc = pOp->p2
1de74 2d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 7d - 1;. }
1de75 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b . break
1de76 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 ;. }else
1de77 69 66 28 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f if( u.az.oc==OP_
1de78 53 65 65 6b 4c 74 20 7c 7c 20 75 2e 61 7a 2e 6f SeekLt || u.az.o
1de79 63 3d 3d 4f 50 5f 53 65 65 6b 47 65 20 29 7b 0a c==OP_SeekGe ){.
1de7a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 /* Use
1de7b 20 74 68 65 20 63 65 69 6c 69 6e 67 28 29 20 66 the ceiling() f
1de7c 75 6e 63 74 69 6f 6e 20 74 6f 20 63 6f 6e 76 65 unction to conve
1de7d 72 74 20 72 65 61 6c 2d 3e 69 6e 74 20 2a 2f 0a rt real->int */.
1de7e 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 49 if( pI
1de7f 6e 33 2d 3e 72 20 3e 20 28 64 6f 75 62 6c 65 29 n3->r > (double)
1de80 75 2e 61 7a 2e 69 4b 65 79 20 29 20 75 2e 61 7a u.az.iKey ) u.az
1de81 2e 69 4b 65 79 2b 2b 3b 0a 20 20 20 20 20 20 20 .iKey++;.
1de82 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
1de83 20 20 2f 2a 20 55 73 65 20 74 68 65 20 66 6c 6f /* Use the flo
1de84 6f 72 28 29 20 66 75 6e 63 74 69 6f 6e 20 74 6f or() function to
1de85 20 63 6f 6e 76 65 72 74 20 72 65 61 6c 2d 3e 69 convert real->i
1de86 6e 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 nt */.
1de87 61 73 73 65 72 74 28 20 75 2e 61 7a 2e 6f 63 3d assert( u.az.oc=
1de88 3d 4f 50 5f 53 65 65 6b 4c 65 20 7c 7c 20 75 2e =OP_SeekLe || u.
1de89 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 47 74 az.oc==OP_SeekGt
1de8a 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 );. if
1de8b 28 20 70 49 6e 33 2d 3e 72 20 3c 20 28 64 6f 75 ( pIn3->r < (dou
1de8c 62 6c 65 29 75 2e 61 7a 2e 69 4b 65 79 20 29 20 ble)u.az.iKey )
1de8d 75 2e 61 7a 2e 69 4b 65 79 2d 2d 3b 0a 20 20 20 u.az.iKey--;.
1de8e 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 }. }.
1de8f 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
1de90 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 3BtreeMovetoUnpa
1de91 63 6b 65 64 28 75 2e 61 7a 2e 70 43 2d 3e 70 43 cked(u.az.pC->pC
1de92 75 72 73 6f 72 2c 20 30 2c 20 28 75 36 34 29 75 ursor, 0, (u64)u
1de93 2e 61 7a 2e 69 4b 65 79 2c 20 30 2c 20 26 75 2e .az.iKey, 0, &u.
1de94 61 7a 2e 72 65 73 29 3b 0a 20 20 20 20 20 20 69 az.res);. i
1de95 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
1de96 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f ){. goto
1de97 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 abort_due_to_er
1de98 72 6f 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 ror;. }.
1de99 20 20 20 69 66 28 20 75 2e 61 7a 2e 72 65 73 3d if( u.az.res=
1de9a 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 75 2e =0 ){. u.
1de9b 61 7a 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 az.pC->rowidIsVa
1de9c 6c 69 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 lid = 1;.
1de9d 20 75 2e 61 7a 2e 70 43 2d 3e 6c 61 73 74 52 6f u.az.pC->lastRo
1de9e 77 69 64 20 3d 20 75 2e 61 7a 2e 69 4b 65 79 3b wid = u.az.iKey;
1de9f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c . }. }el
1dea0 73 65 7b 0a 20 20 20 20 20 20 75 2e 61 7a 2e 6e se{. u.az.n
1dea1 46 69 65 6c 64 20 3d 20 70 4f 70 2d 3e 70 34 2e Field = pOp->p4.
1dea2 69 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 i;. assert(
1dea3 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 pOp->p4type==P4
1dea4 5f 49 4e 54 33 32 20 29 3b 0a 20 20 20 20 20 20 _INT32 );.
1dea5 61 73 73 65 72 74 28 20 75 2e 61 7a 2e 6e 46 69 assert( u.az.nFi
1dea6 65 6c 64 3e 30 20 29 3b 0a 20 20 20 20 20 20 75 eld>0 );. u
1dea7 2e 61 7a 2e 72 2e 70 4b 65 79 49 6e 66 6f 20 3d .az.r.pKeyInfo =
1dea8 20 75 2e 61 7a 2e 70 43 2d 3e 70 4b 65 79 49 6e u.az.pC->pKeyIn
1dea9 66 6f 3b 0a 20 20 20 20 20 20 75 2e 61 7a 2e 72 fo;. u.az.r
1deaa 2e 6e 46 69 65 6c 64 20 3d 20 28 75 31 36 29 75 .nField = (u16)u
1deab 2e 61 7a 2e 6e 46 69 65 6c 64 3b 0a 20 20 20 20 .az.nField;.
1deac 20 20 69 66 28 20 75 2e 61 7a 2e 6f 63 3d 3d 4f if( u.az.oc==O
1dead 50 5f 53 65 65 6b 47 74 20 7c 7c 20 75 2e 61 7a P_SeekGt || u.az
1deae 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 4c 65 20 29 .oc==OP_SeekLe )
1deaf 7b 0a 20 20 20 20 20 20 20 20 75 2e 61 7a 2e 72 {. u.az.r
1deb0 2e 66 6c 61 67 73 20 3d 20 55 4e 50 41 43 4b 45 .flags = UNPACKE
1deb1 44 5f 49 4e 43 52 4b 45 59 3b 0a 20 20 20 20 20 D_INCRKEY;.
1deb2 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
1deb3 75 2e 61 7a 2e 72 2e 66 6c 61 67 73 20 3d 20 30 u.az.r.flags = 0
1deb4 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
1deb5 75 2e 61 7a 2e 72 2e 61 4d 65 6d 20 3d 20 26 70 u.az.r.aMem = &p
1deb6 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b ->aMem[pOp->p3];
1deb7 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
1deb8 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e te3BtreeMovetoUn
1deb9 70 61 63 6b 65 64 28 75 2e 61 7a 2e 70 43 2d 3e packed(u.az.pC->
1deba 70 43 75 72 73 6f 72 2c 20 26 75 2e 61 7a 2e 72 pCursor, &u.az.r
1debb 2c 20 30 2c 20 30 2c 20 26 75 2e 61 7a 2e 72 65 , 0, 0, &u.az.re
1debc 73 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 s);. if( rc
1debd 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
1debe 20 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 goto abor
1debf 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a t_due_to_error;.
1dec0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 75 2e }. u.
1dec1 61 7a 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 az.pC->rowidIsVa
1dec2 6c 69 64 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 lid = 0;. }.
1dec3 20 20 20 75 2e 61 7a 2e 70 43 2d 3e 64 65 66 65 u.az.pC->defe
1dec4 72 72 65 64 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a rredMoveto = 0;.
1dec5 20 20 20 20 75 2e 61 7a 2e 70 43 2d 3e 63 61 63 u.az.pC->cac
1dec6 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 45 heStatus = CACHE
1dec7 5f 53 54 41 4c 45 3b 0a 23 69 66 64 65 66 20 53 _STALE;.#ifdef S
1dec8 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 20 20 73 QLITE_TEST. s
1dec9 71 6c 69 74 65 33 5f 73 65 61 72 63 68 5f 63 6f qlite3_search_co
1deca 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 unt++;.#endif.
1decb 20 20 69 66 28 20 75 2e 61 7a 2e 6f 63 3d 3d 4f if( u.az.oc==O
1decc 50 5f 53 65 65 6b 47 65 20 7c 7c 20 75 2e 61 7a P_SeekGe || u.az
1decd 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 47 74 20 29 .oc==OP_SeekGt )
1dece 7b 0a 20 20 20 20 20 20 69 66 28 20 75 2e 61 7a {. if( u.az
1decf 2e 72 65 73 3c 30 20 7c 7c 20 28 75 2e 61 7a 2e .res<0 || (u.az.
1ded0 72 65 73 3d 3d 30 20 26 26 20 75 2e 61 7a 2e 6f res==0 && u.az.o
1ded1 63 3d 3d 4f 50 5f 53 65 65 6b 47 74 29 20 29 7b c==OP_SeekGt) ){
1ded2 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 . rc = sq
1ded3 6c 69 74 65 33 42 74 72 65 65 4e 65 78 74 28 75 lite3BtreeNext(u
1ded4 2e 61 7a 2e 70 43 2d 3e 70 43 75 72 73 6f 72 2c .az.pC->pCursor,
1ded5 20 26 75 2e 61 7a 2e 72 65 73 29 3b 0a 20 20 20 &u.az.res);.
1ded6 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c if( rc!=SQL
1ded7 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 61 62 ITE_OK ) goto ab
1ded8 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 ort_due_to_error
1ded9 3b 0a 20 20 20 20 20 20 20 20 75 2e 61 7a 2e 70 ;. u.az.p
1deda 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 C->rowidIsValid
1dedb 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 = 0;. }else
1dedc 7b 0a 20 20 20 20 20 20 20 20 75 2e 61 7a 2e 72 {. u.az.r
1dedd 65 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a es = 0;. }.
1dede 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1dedf 20 61 73 73 65 72 74 28 20 75 2e 61 7a 2e 6f 63 assert( u.az.oc
1dee0 3d 3d 4f 50 5f 53 65 65 6b 4c 74 20 7c 7c 20 75 ==OP_SeekLt || u
1dee1 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 4c .az.oc==OP_SeekL
1dee2 65 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 75 e );. if( u
1dee3 2e 61 7a 2e 72 65 73 3e 30 20 7c 7c 20 28 75 2e .az.res>0 || (u.
1dee4 61 7a 2e 72 65 73 3d 3d 30 20 26 26 20 75 2e 61 az.res==0 && u.a
1dee5 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 4c 74 29 z.oc==OP_SeekLt)
1dee6 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d ){. rc =
1dee7 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 72 65 sqlite3BtreePre
1dee8 76 69 6f 75 73 28 75 2e 61 7a 2e 70 43 2d 3e 70 vious(u.az.pC->p
1dee9 43 75 72 73 6f 72 2c 20 26 75 2e 61 7a 2e 72 65 Cursor, &u.az.re
1deea 73 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 s);. if(
1deeb 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 rc!=SQLITE_OK )
1deec 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 goto abort_due_t
1deed 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 20 o_error;.
1deee 20 75 2e 61 7a 2e 70 43 2d 3e 72 6f 77 69 64 49 u.az.pC->rowidI
1deef 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 20 20 sValid = 0;.
1def0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
1def1 20 2f 2a 20 75 2e 61 7a 2e 72 65 73 20 6d 69 67 /* u.az.res mig
1def2 68 74 20 62 65 20 6e 65 67 61 74 69 76 65 20 62 ht be negative b
1def3 65 63 61 75 73 65 20 74 68 65 20 74 61 62 6c 65 ecause the table
1def4 20 69 73 20 65 6d 70 74 79 2e 20 20 43 68 65 63 is empty. Chec
1def5 6b 20 74 6f 0a 20 20 20 20 20 20 20 20 2a 2a 20 k to. **
1def6 73 65 65 20 69 66 20 74 68 69 73 20 69 73 20 74 see if this is t
1def7 68 65 20 63 61 73 65 2e 0a 20 20 20 20 20 20 20 he case..
1def8 20 2a 2f 0a 20 20 20 20 20 20 20 20 75 2e 61 7a */. u.az
1def9 2e 72 65 73 20 3d 20 73 71 6c 69 74 65 33 42 74 .res = sqlite3Bt
1defa 72 65 65 45 6f 66 28 75 2e 61 7a 2e 70 43 2d 3e reeEof(u.az.pC->
1defb 70 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 20 20 pCursor);.
1defc 7d 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 }. }. asse
1defd 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20 29 3b rt( pOp->p2>0 );
1defe 0a 20 20 20 20 69 66 28 20 75 2e 61 7a 2e 72 65 . if( u.az.re
1deff 73 20 29 7b 0a 20 20 20 20 20 20 70 63 20 3d 20 s ){. pc =
1df00 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 pOp->p2 - 1;.
1df01 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }. }else{.
1df02 2f 2a 20 54 68 69 73 20 68 61 70 70 65 6e 73 20 /* This happens
1df03 77 68 65 6e 20 61 74 74 65 6d 70 74 69 6e 67 20 when attempting
1df04 74 6f 20 6f 70 65 6e 20 74 68 65 20 73 71 6c 69 to open the sqli
1df05 74 65 33 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 te3_master table
1df06 0a 20 20 20 20 2a 2a 20 66 6f 72 20 72 65 61 64 . ** for read
1df07 20 61 63 63 65 73 73 20 72 65 74 75 72 6e 73 20 access returns
1df08 53 51 4c 49 54 45 5f 45 4d 50 54 59 2e 20 49 6e SQLITE_EMPTY. In
1df09 20 74 68 69 73 20 63 61 73 65 20 61 6c 77 61 79 this case alway
1df0a 73 0a 20 20 20 20 2a 2a 20 74 61 6b 65 20 74 68 s. ** take th
1df0b 65 20 6a 75 6d 70 20 28 73 69 6e 63 65 20 74 68 e jump (since th
1df0c 65 72 65 20 61 72 65 20 6e 6f 20 72 65 63 6f 72 ere are no recor
1df0d 64 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 29 ds in the table)
1df0e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 63 20 .. */. pc
1df0f 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 = pOp->p2 - 1;.
1df10 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f }. break;.}../
1df11 2a 20 4f 70 63 6f 64 65 3a 20 53 65 65 6b 20 50 * Opcode: Seek P
1df12 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 1 P2 * * *.**.**
1df13 20 50 31 20 69 73 20 61 6e 20 6f 70 65 6e 20 74 P1 is an open t
1df14 61 62 6c 65 20 63 75 72 73 6f 72 20 61 6e 64 20 able cursor and
1df15 50 32 20 69 73 20 61 20 72 6f 77 69 64 20 69 6e P2 is a rowid in
1df16 74 65 67 65 72 2e 20 20 41 72 72 61 6e 67 65 0a teger. Arrange.
1df17 2a 2a 20 66 6f 72 20 50 31 20 74 6f 20 6d 6f 76 ** for P1 to mov
1df18 65 20 73 6f 20 74 68 61 74 20 69 74 20 70 6f 69 e so that it poi
1df19 6e 74 73 20 74 6f 20 74 68 65 20 72 6f 77 69 64 nts to the rowid
1df1a 20 67 69 76 65 6e 20 62 79 20 50 32 2e 0a 2a 2a given by P2..**
1df1b 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 63 74 75 .** This is actu
1df1c 61 6c 6c 79 20 61 20 64 65 66 65 72 72 65 64 20 ally a deferred
1df1d 73 65 65 6b 2e 20 20 4e 6f 74 68 69 6e 67 20 61 seek. Nothing a
1df1e 63 74 75 61 6c 6c 79 20 68 61 70 70 65 6e 73 20 ctually happens
1df1f 75 6e 74 69 6c 0a 2a 2a 20 74 68 65 20 63 75 72 until.** the cur
1df20 73 6f 72 20 69 73 20 75 73 65 64 20 74 6f 20 72 sor is used to r
1df21 65 61 64 20 61 20 72 65 63 6f 72 64 2e 20 20 54 ead a record. T
1df22 68 61 74 20 77 61 79 2c 20 69 66 20 6e 6f 20 72 hat way, if no r
1df23 65 61 64 73 0a 2a 2a 20 6f 63 63 75 72 2c 20 6e eads.** occur, n
1df24 6f 20 75 6e 6e 65 63 65 73 73 61 72 79 20 49 2f o unnecessary I/
1df25 4f 20 68 61 70 70 65 6e 73 2e 0a 2a 2f 0a 63 61 O happens..*/.ca
1df26 73 65 20 4f 50 5f 53 65 65 6b 3a 20 7b 20 20 20 se OP_Seek: {
1df27 20 2f 2a 20 69 6e 32 20 2a 2f 0a 23 69 66 20 30 /* in2 */.#if 0
1df28 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 /* local varia
1df29 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 bles moved into
1df2a 75 2e 62 61 20 2a 2f 0a 20 20 56 64 62 65 43 75 u.ba */. VdbeCu
1df2b 72 73 6f 72 20 2a 70 43 3b 0a 23 65 6e 64 69 66 rsor *pC;.#endif
1df2c 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1df2d 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1df2e 2e 62 61 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 .ba */.. assert
1df2f 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 ( pOp->p1>=0 &&
1df30 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 pOp->p1<p->nCurs
1df31 6f 72 20 29 3b 0a 20 20 75 2e 62 61 2e 70 43 20 or );. u.ba.pC
1df32 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e = p->apCsr[pOp->
1df33 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75 p1];. assert( u
1df34 2e 62 61 2e 70 43 21 3d 30 20 29 3b 0a 20 20 69 .ba.pC!=0 );. i
1df35 66 28 20 41 4c 57 41 59 53 28 75 2e 62 61 2e 70 f( ALWAYS(u.ba.p
1df36 43 2d 3e 70 43 75 72 73 6f 72 21 3d 30 29 20 29 C->pCursor!=0) )
1df37 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 2e {. assert( u.
1df38 62 61 2e 70 43 2d 3e 69 73 54 61 62 6c 65 20 29 ba.pC->isTable )
1df39 3b 0a 20 20 20 20 75 2e 62 61 2e 70 43 2d 3e 6e ;. u.ba.pC->n
1df3a 75 6c 6c 52 6f 77 20 3d 20 30 3b 0a 20 20 20 20 ullRow = 0;.
1df3b 75 2e 62 61 2e 70 43 2d 3e 6d 6f 76 65 74 6f 54 u.ba.pC->movetoT
1df3c 61 72 67 65 74 20 3d 20 73 71 6c 69 74 65 33 56 arget = sqlite3V
1df3d 64 62 65 49 6e 74 56 61 6c 75 65 28 70 49 6e 32 dbeIntValue(pIn2
1df3e 29 3b 0a 20 20 20 20 75 2e 62 61 2e 70 43 2d 3e );. u.ba.pC->
1df3f 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20 30 rowidIsValid = 0
1df40 3b 0a 20 20 20 20 75 2e 62 61 2e 70 43 2d 3e 64 ;. u.ba.pC->d
1df41 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20 3d 20 eferredMoveto =
1df42 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 1;. }. break;.
1df43 7d 0a 20 20 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a }. ../* Opcode:
1df44 20 46 6f 75 6e 64 20 50 31 20 50 32 20 50 33 20 Found P1 P2 P3
1df45 2a 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 67 69 73 74 * *.**.** Regist
1df46 65 72 20 50 33 20 68 6f 6c 64 73 20 61 20 62 6c er P3 holds a bl
1df47 6f 62 20 63 6f 6e 73 74 72 75 63 74 65 64 20 62 ob constructed b
1df48 79 20 4d 61 6b 65 52 65 63 6f 72 64 2e 20 20 50 y MakeRecord. P
1df49 31 20 69 73 20 61 6e 20 69 6e 64 65 78 2e 0a 2a 1 is an index..*
1df4a 2a 20 49 66 20 61 6e 20 65 6e 74 72 79 20 74 68 * If an entry th
1df4b 61 74 20 6d 61 74 63 68 65 73 20 74 68 65 20 76 at matches the v
1df4c 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 alue in register
1df4d 20 70 33 20 65 78 69 73 74 73 20 69 6e 20 50 31 p3 exists in P1
1df4e 20 74 68 65 6e 0a 2a 2a 20 6a 75 6d 70 20 74 6f then.** jump to
1df4f 20 50 32 2e 20 20 49 66 20 74 68 65 20 50 33 20 P2. If the P3
1df50 76 61 6c 75 65 20 64 6f 65 73 20 6e 6f 74 20 6d value does not m
1df51 61 74 63 68 20 61 6e 79 20 65 6e 74 72 79 20 69 atch any entry i
1df52 6e 20 50 31 0a 2a 2a 20 74 68 65 6e 20 66 61 6c n P1.** then fal
1df53 6c 20 74 68 72 75 2e 20 20 54 68 65 20 50 31 20 l thru. The P1
1df54 63 75 72 73 6f 72 20 69 73 20 6c 65 66 74 20 70 cursor is left p
1df55 6f 69 6e 74 69 6e 67 20 61 74 20 74 68 65 20 6d ointing at the m
1df56 61 74 63 68 69 6e 67 20 65 6e 74 72 79 0a 2a 2a atching entry.**
1df57 20 69 66 20 69 74 20 65 78 69 73 74 73 2e 0a 2a if it exists..*
1df58 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 73 74 72 75 *.** This instru
1df59 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f ction is used to
1df5a 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 49 implement the I
1df5b 4e 20 6f 70 65 72 61 74 6f 72 20 77 68 65 72 65 N operator where
1df5c 20 74 68 65 0a 2a 2a 20 6c 65 66 74 2d 68 61 6e the.** left-han
1df5d 64 20 73 69 64 65 20 69 73 20 61 20 53 45 4c 45 d side is a SELE
1df5e 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 50 CT statement. P
1df5f 31 20 6d 61 79 20 62 65 20 61 20 74 72 75 65 20 1 may be a true
1df60 69 6e 64 65 78 2c 20 6f 72 20 69 74 0a 2a 2a 20 index, or it.**
1df61 6d 61 79 20 62 65 20 61 20 74 65 6d 70 6f 72 61 may be a tempora
1df62 72 79 20 69 6e 64 65 78 20 74 68 61 74 20 68 6f ry index that ho
1df63 6c 64 73 20 74 68 65 20 72 65 73 75 6c 74 73 20 lds the results
1df64 6f 66 20 74 68 65 20 53 45 4c 45 43 54 0a 2a 2a of the SELECT.**
1df65 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 20 54 68 statement. Th
1df66 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 is instruction i
1df67 73 20 61 6c 73 6f 20 75 73 65 64 20 74 6f 20 69 s also used to i
1df68 6d 70 6c 65 6d 65 6e 74 20 74 68 65 0a 2a 2a 20 mplement the.**
1df69 44 49 53 54 49 4e 43 54 20 6b 65 79 77 6f 72 64 DISTINCT keyword
1df6a 20 69 6e 20 53 45 4c 45 43 54 20 73 74 61 74 65 in SELECT state
1df6b 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 ments..**.** Thi
1df6c 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 63 68 s instruction ch
1df6d 65 63 6b 73 20 69 66 20 69 6e 64 65 78 20 50 31 ecks if index P1
1df6e 20 63 6f 6e 74 61 69 6e 73 20 61 20 72 65 63 6f contains a reco
1df6f 72 64 20 66 6f 72 20 77 68 69 63 68 20 0a 2a 2a rd for which .**
1df70 20 74 68 65 20 66 69 72 73 74 20 4e 20 73 65 72 the first N ser
1df71 69 61 6c 69 7a 65 64 20 76 61 6c 75 65 73 20 65 ialized values e
1df72 78 61 63 74 6c 79 20 6d 61 74 63 68 20 74 68 65 xactly match the
1df73 20 4e 20 73 65 72 69 61 6c 69 7a 65 64 20 76 61 N serialized va
1df74 6c 75 65 73 0a 2a 2a 20 69 6e 20 74 68 65 20 72 lues.** in the r
1df75 65 63 6f 72 64 20 69 6e 20 72 65 67 69 73 74 65 ecord in registe
1df76 72 20 50 33 2c 20 77 68 65 72 65 20 4e 20 69 73 r P3, where N is
1df77 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 the total numbe
1df78 72 20 6f 66 20 76 61 6c 75 65 73 20 69 6e 0a 2a r of values in.*
1df79 2a 20 74 68 65 20 50 33 20 72 65 63 6f 72 64 20 * the P3 record
1df7a 28 74 68 65 20 50 33 20 72 65 63 6f 72 64 20 69 (the P3 record i
1df7b 73 20 61 20 70 72 65 66 69 78 20 6f 66 20 74 68 s a prefix of th
1df7c 65 20 50 31 20 72 65 63 6f 72 64 29 2e 20 0a 2a e P1 record). .*
1df7d 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 4e *.** See also: N
1df7e 6f 74 46 6f 75 6e 64 2c 20 49 73 55 6e 69 71 75 otFound, IsUniqu
1df7f 65 2c 20 4e 6f 74 45 78 69 73 74 73 0a 2a 2f 0a e, NotExists.*/.
1df80 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 6f 74 46 6f /* Opcode: NotFo
1df81 75 6e 64 20 50 31 20 50 32 20 50 33 20 2a 20 2a und P1 P2 P3 * *
1df82 0a 2a 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 .**.** Register
1df83 50 33 20 68 6f 6c 64 73 20 61 20 62 6c 6f 62 20 P3 holds a blob
1df84 63 6f 6e 73 74 72 75 63 74 65 64 20 62 79 20 4d constructed by M
1df85 61 6b 65 52 65 63 6f 72 64 2e 20 20 50 31 20 69 akeRecord. P1 i
1df86 73 0a 2a 2a 20 61 6e 20 69 6e 64 65 78 2e 20 20 s.** an index.
1df87 49 66 20 6e 6f 20 65 6e 74 72 79 20 65 78 69 73 If no entry exis
1df88 74 73 20 69 6e 20 50 31 20 74 68 61 74 20 6d 61 ts in P1 that ma
1df89 74 63 68 65 73 20 74 68 65 20 62 6c 6f 62 20 74 tches the blob t
1df8a 68 65 6e 20 6a 75 6d 70 0a 2a 2a 20 74 6f 20 50 hen jump.** to P
1df8b 32 2e 20 20 49 66 20 61 6e 20 65 6e 74 72 79 20 2. If an entry
1df8c 64 6f 65 73 20 65 78 69 73 74 69 6e 67 2c 20 66 does existing, f
1df8d 61 6c 6c 20 74 68 72 6f 75 67 68 2e 20 20 54 68 all through. Th
1df8e 65 20 63 75 72 73 6f 72 20 69 73 20 6c 65 66 74 e cursor is left
1df8f 0a 2a 2a 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 .** pointing to
1df90 74 68 65 20 65 6e 74 72 79 20 74 68 61 74 20 6d the entry that m
1df91 61 74 63 68 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 atches..**.** Se
1df92 65 20 61 6c 73 6f 3a 20 46 6f 75 6e 64 2c 20 4e e also: Found, N
1df93 6f 74 45 78 69 73 74 73 2c 20 49 73 55 6e 69 71 otExists, IsUniq
1df94 75 65 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 6f ue.*/.case OP_No
1df95 74 46 6f 75 6e 64 3a 20 20 20 20 20 20 20 2f 2a tFound: /*
1df96 20 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a 63 61 jump, in3 */.ca
1df97 73 65 20 4f 50 5f 46 6f 75 6e 64 3a 20 7b 20 20 se OP_Found: {
1df98 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 /* jump, i
1df99 6e 33 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 n3 */.#if 0 /*
1df9a 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 local variables
1df9b 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 62 20 moved into u.bb
1df9c 2a 2f 0a 20 20 69 6e 74 20 61 6c 72 65 61 64 79 */. int already
1df9d 45 78 69 73 74 73 3b 0a 20 20 56 64 62 65 43 75 Exists;. VdbeCu
1df9e 72 73 6f 72 20 2a 70 43 3b 0a 20 20 69 6e 74 20 rsor *pC;. int
1df9f 72 65 73 3b 0a 20 20 55 6e 70 61 63 6b 65 64 52 res;. UnpackedR
1dfa0 65 63 6f 72 64 20 2a 70 49 64 78 4b 65 79 3b 0a ecord *pIdxKey;.
1dfa1 20 20 63 68 61 72 20 61 54 65 6d 70 52 65 63 5b char aTempRec[
1dfa2 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 55 6e ROUND8(sizeof(Un
1dfa3 70 61 63 6b 65 64 52 65 63 6f 72 64 29 29 20 2b packedRecord)) +
1dfa4 20 73 69 7a 65 6f 66 28 4d 65 6d 29 2a 33 20 2b sizeof(Mem)*3 +
1dfa5 20 37 5d 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 7];.#endif /* l
1dfa6 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1dfa7 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 62 20 2a oved into u.bb *
1dfa8 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 /..#ifdef SQLITE
1dfa9 5f 54 45 53 54 0a 20 20 73 71 6c 69 74 65 33 5f _TEST. sqlite3_
1dfaa 66 6f 75 6e 64 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 found_count++;.#
1dfab 65 6e 64 69 66 0a 0a 20 20 75 2e 62 62 2e 61 6c endif.. u.bb.al
1dfac 72 65 61 64 79 45 78 69 73 74 73 20 3d 20 30 3b readyExists = 0;
1dfad 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e . assert( pOp->
1dfae 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 p1>=0 && pOp->p1
1dfaf 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 <p->nCursor );.
1dfb0 20 75 2e 62 62 2e 70 43 20 3d 20 70 2d 3e 61 70 u.bb.pC = p->ap
1dfb1 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 Csr[pOp->p1];.
1dfb2 61 73 73 65 72 74 28 20 75 2e 62 62 2e 70 43 21 assert( u.bb.pC!
1dfb3 3d 30 20 29 3b 0a 20 20 69 66 28 20 41 4c 57 41 =0 );. if( ALWA
1dfb4 59 53 28 75 2e 62 62 2e 70 43 2d 3e 70 43 75 72 YS(u.bb.pC->pCur
1dfb5 73 6f 72 21 3d 30 29 20 29 7b 0a 0a 20 20 20 20 sor!=0) ){..
1dfb6 61 73 73 65 72 74 28 20 75 2e 62 62 2e 70 43 2d assert( u.bb.pC-
1dfb7 3e 69 73 54 61 62 6c 65 3d 3d 30 20 29 3b 0a 20 >isTable==0 );.
1dfb8 20 20 20 61 73 73 65 72 74 28 20 70 49 6e 33 2d assert( pIn3-
1dfb9 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 42 6c 6f >flags & MEM_Blo
1dfba 62 20 29 3b 0a 20 20 20 20 45 78 70 61 6e 64 42 b );. ExpandB
1dfbb 6c 6f 62 28 70 49 6e 33 29 3b 0a 20 20 20 20 75 lob(pIn3);. u
1dfbc 2e 62 62 2e 70 49 64 78 4b 65 79 20 3d 20 73 71 .bb.pIdxKey = sq
1dfbd 6c 69 74 65 33 56 64 62 65 52 65 63 6f 72 64 55 lite3VdbeRecordU
1dfbe 6e 70 61 63 6b 28 75 2e 62 62 2e 70 43 2d 3e 70 npack(u.bb.pC->p
1dfbf 4b 65 79 49 6e 66 6f 2c 20 70 49 6e 33 2d 3e 6e KeyInfo, pIn3->n
1dfc0 2c 20 70 49 6e 33 2d 3e 7a 2c 0a 20 20 20 20 20 , pIn3->z,.
1dfc1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1dfc2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1dfc3 20 75 2e 62 62 2e 61 54 65 6d 70 52 65 63 2c 20 u.bb.aTempRec,
1dfc4 73 69 7a 65 6f 66 28 75 2e 62 62 2e 61 54 65 6d sizeof(u.bb.aTem
1dfc5 70 52 65 63 29 29 3b 0a 20 20 20 20 69 66 28 20 pRec));. if(
1dfc6 75 2e 62 62 2e 70 49 64 78 4b 65 79 3d 3d 30 20 u.bb.pIdxKey==0
1dfc7 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 6e 6f ){. goto no
1dfc8 5f 6d 65 6d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 _mem;. }.
1dfc9 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d if( pOp->opcode=
1dfca 3d 4f 50 5f 46 6f 75 6e 64 20 29 7b 0a 20 20 20 =OP_Found ){.
1dfcb 20 20 20 75 2e 62 62 2e 70 49 64 78 4b 65 79 2d u.bb.pIdxKey-
1dfcc 3e 66 6c 61 67 73 20 7c 3d 20 55 4e 50 41 43 4b >flags |= UNPACK
1dfcd 45 44 5f 50 52 45 46 49 58 5f 4d 41 54 43 48 3b ED_PREFIX_MATCH;
1dfce 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 . }. rc =
1dfcf 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65 sqlite3BtreeMove
1dfd0 74 6f 55 6e 70 61 63 6b 65 64 28 75 2e 62 62 2e toUnpacked(u.bb.
1dfd1 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 75 2e 62 pC->pCursor, u.b
1dfd2 62 2e 70 49 64 78 4b 65 79 2c 20 30 2c 20 30 2c b.pIdxKey, 0, 0,
1dfd3 20 26 75 2e 62 62 2e 72 65 73 29 3b 0a 20 20 20 &u.bb.res);.
1dfd4 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 sqlite3VdbeDele
1dfd5 74 65 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 teUnpackedRecord
1dfd6 28 75 2e 62 62 2e 70 49 64 78 4b 65 79 29 3b 0a (u.bb.pIdxKey);.
1dfd7 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
1dfd8 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 62 TE_OK ){. b
1dfd9 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
1dfda 75 2e 62 62 2e 61 6c 72 65 61 64 79 45 78 69 73 u.bb.alreadyExis
1dfdb 74 73 20 3d 20 28 75 2e 62 62 2e 72 65 73 3d 3d ts = (u.bb.res==
1dfdc 30 29 3b 0a 20 20 20 20 75 2e 62 62 2e 70 43 2d 0);. u.bb.pC-
1dfdd 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20 >deferredMoveto
1dfde 3d 20 30 3b 0a 20 20 20 20 75 2e 62 62 2e 70 43 = 0;. u.bb.pC
1dfdf 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 ->cacheStatus =
1dfe0 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 7d CACHE_STALE;. }
1dfe1 0a 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f . if( pOp->opco
1dfe2 64 65 3d 3d 4f 50 5f 46 6f 75 6e 64 20 29 7b 0a de==OP_Found ){.
1dfe3 20 20 20 20 69 66 28 20 75 2e 62 62 2e 61 6c 72 if( u.bb.alr
1dfe4 65 61 64 79 45 78 69 73 74 73 20 29 20 70 63 20 eadyExists ) pc
1dfe5 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 = pOp->p2 - 1;.
1dfe6 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66 28 20 }else{. if(
1dfe7 21 75 2e 62 62 2e 61 6c 72 65 61 64 79 45 78 69 !u.bb.alreadyExi
1dfe8 73 74 73 20 29 20 70 63 20 3d 20 70 4f 70 2d 3e sts ) pc = pOp->
1dfe9 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 p2 - 1;. }. br
1dfea 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1dfeb 65 3a 20 49 73 55 6e 69 71 75 65 20 50 31 20 50 e: IsUnique P1 P
1dfec 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 2 P3 P4 *.**.**
1dfed 43 75 72 73 6f 72 20 50 31 20 69 73 20 6f 70 65 Cursor P1 is ope
1dfee 6e 20 6f 6e 20 61 6e 20 69 6e 64 65 78 2e 20 20 n on an index.
1dfef 53 6f 20 69 74 20 68 61 73 20 6e 6f 20 64 61 74 So it has no dat
1dff0 61 20 61 6e 64 20 69 74 73 20 6b 65 79 20 63 6f a and its key co
1dff1 6e 73 69 73 74 73 20 0a 2a 2a 20 6f 66 20 61 20 nsists .** of a
1dff2 72 65 63 6f 72 64 20 67 65 6e 65 72 61 74 65 64 record generated
1dff3 20 62 79 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 by OP_MakeRecor
1dff4 64 20 77 68 65 72 65 20 74 68 65 20 6c 61 73 74 d where the last
1dff5 20 66 69 65 6c 64 20 69 73 20 74 68 65 20 0a 2a field is the .*
1dff6 2a 20 72 6f 77 69 64 20 6f 66 20 74 68 65 20 65 * rowid of the e
1dff7 6e 74 72 79 20 74 68 61 74 20 74 68 65 20 69 6e ntry that the in
1dff8 64 65 78 20 72 65 66 65 72 73 20 74 6f 2e 0a 2a dex refers to..*
1dff9 2a 0a 2a 2a 20 54 68 65 20 50 33 20 72 65 67 69 *.** The P3 regi
1dffa 73 74 65 72 20 63 6f 6e 74 61 69 6e 73 20 61 6e ster contains an
1dffb 20 69 6e 74 65 67 65 72 20 72 65 63 6f 72 64 20 integer record
1dffc 6e 75 6d 62 65 72 2e 20 43 61 6c 6c 20 74 68 69 number. Call thi
1dffd 73 20 72 65 63 6f 72 64 20 0a 2a 2a 20 6e 75 6d s record .** num
1dffe 62 65 72 20 52 2e 20 52 65 67 69 73 74 65 72 20 ber R. Register
1dfff 50 34 20 69 73 20 74 68 65 20 66 69 72 73 74 20 P4 is the first
1e000 69 6e 20 61 20 73 65 74 20 6f 66 20 4e 20 63 6f in a set of N co
1e001 6e 74 69 67 75 6f 75 73 20 72 65 67 69 73 74 65 ntiguous registe
1e002 72 73 0a 2a 2a 20 74 68 61 74 20 6d 61 6b 65 20 rs.** that make
1e003 75 70 20 61 6e 20 75 6e 70 61 63 6b 65 64 20 69 up an unpacked i
1e004 6e 64 65 78 20 6b 65 79 20 74 68 61 74 20 63 61 ndex key that ca
1e005 6e 20 62 65 20 75 73 65 64 20 77 69 74 68 20 63 n be used with c
1e006 75 72 73 6f 72 20 50 31 2e 0a 2a 2a 20 54 68 65 ursor P1..** The
1e007 20 76 61 6c 75 65 20 6f 66 20 4e 20 63 61 6e 20 value of N can
1e008 62 65 20 69 6e 66 65 72 72 65 64 20 66 72 6f 6d be inferred from
1e009 20 74 68 65 20 63 75 72 73 6f 72 2e 20 4e 20 69 the cursor. N i
1e00a 6e 63 6c 75 64 65 73 20 74 68 65 20 72 6f 77 69 ncludes the rowi
1e00b 64 0a 2a 2a 20 76 61 6c 75 65 20 61 70 70 65 6e d.** value appen
1e00c 64 65 64 20 74 6f 20 74 68 65 20 65 6e 64 20 6f ded to the end o
1e00d 66 20 74 68 65 20 69 6e 64 65 78 20 72 65 63 6f f the index reco
1e00e 72 64 2e 20 54 68 69 73 20 72 6f 77 69 64 20 76 rd. This rowid v
1e00f 61 6c 75 65 20 6d 61 79 0a 2a 2a 20 6f 72 20 6d alue may.** or m
1e010 61 79 20 6e 6f 74 20 62 65 20 74 68 65 20 73 61 ay not be the sa
1e011 6d 65 20 61 73 20 52 2e 0a 2a 2a 0a 2a 2a 20 49 me as R..**.** I
1e012 66 20 61 6e 79 20 6f 66 20 74 68 65 20 4e 20 72 f any of the N r
1e013 65 67 69 73 74 65 72 73 20 62 65 67 69 6e 6e 69 egisters beginni
1e014 6e 67 20 77 69 74 68 20 72 65 67 69 73 74 65 72 ng with register
1e015 20 50 34 20 63 6f 6e 74 61 69 6e 73 20 61 20 4e P4 contains a N
1e016 55 4c 4c 0a 2a 2a 20 76 61 6c 75 65 2c 20 6a 75 ULL.** value, ju
1e017 6d 70 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 74 mp immediately t
1e018 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 o P2..**.** Othe
1e019 72 77 69 73 65 2c 20 74 68 69 73 20 69 6e 73 74 rwise, this inst
1e01a 72 75 63 74 69 6f 6e 20 63 68 65 63 6b 73 20 69 ruction checks i
1e01b 66 20 63 75 72 73 6f 72 20 50 31 20 63 6f 6e 74 f cursor P1 cont
1e01c 61 69 6e 73 20 61 6e 20 65 6e 74 72 79 0a 2a 2a ains an entry.**
1e01d 20 77 68 65 72 65 20 74 68 65 20 66 69 72 73 74 where the first
1e01e 20 28 4e 2d 31 29 20 66 69 65 6c 64 73 20 6d 61 (N-1) fields ma
1e01f 74 63 68 20 62 75 74 20 74 68 65 20 72 6f 77 69 tch but the rowi
1e020 64 20 76 61 6c 75 65 20 61 74 20 74 68 65 20 65 d value at the e
1e021 6e 64 0a 2a 2a 20 6f 66 20 74 68 65 20 69 6e 64 nd.** of the ind
1e022 65 78 20 65 6e 74 72 79 20 69 73 20 6e 6f 74 20 ex entry is not
1e023 52 2e 20 49 66 20 74 68 65 72 65 20 69 73 20 6e R. If there is n
1e024 6f 20 73 75 63 68 20 65 6e 74 72 79 2c 20 63 6f o such entry, co
1e025 6e 74 72 6f 6c 20 6a 75 6d 70 73 0a 2a 2a 20 74 ntrol jumps.** t
1e026 6f 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 50 32 o instruction P2
1e027 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 . Otherwise, the
1e028 20 72 6f 77 69 64 20 6f 66 20 74 68 65 20 63 6f rowid of the co
1e029 6e 66 6c 69 63 74 69 6e 67 20 69 6e 64 65 78 0a nflicting index.
1e02a 2a 2a 20 65 6e 74 72 79 20 69 73 20 63 6f 70 69 ** entry is copi
1e02b 65 64 20 74 6f 20 72 65 67 69 73 74 65 72 20 50 ed to register P
1e02c 33 20 61 6e 64 20 63 6f 6e 74 72 6f 6c 20 66 61 3 and control fa
1e02d 6c 6c 73 20 74 68 72 6f 75 67 68 20 74 6f 20 74 lls through to t
1e02e 68 65 20 6e 65 78 74 0a 2a 2a 20 69 6e 73 74 72 he next.** instr
1e02f 75 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 53 65 uction..**.** Se
1e030 65 20 61 6c 73 6f 3a 20 4e 6f 74 46 6f 75 6e 64 e also: NotFound
1e031 2c 20 4e 6f 74 45 78 69 73 74 73 2c 20 46 6f 75 , NotExists, Fou
1e032 6e 64 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 73 nd.*/.case OP_Is
1e033 55 6e 69 71 75 65 3a 20 7b 20 20 20 20 20 20 20 Unique: {
1e034 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f /* jump, in3 */
1e035 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c .#if 0 /* local
1e036 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1e037 20 69 6e 74 6f 20 75 2e 62 63 20 2a 2f 0a 20 20 into u.bc */.
1e038 75 31 36 20 69 69 3b 0a 20 20 56 64 62 65 43 75 u16 ii;. VdbeCu
1e039 72 73 6f 72 20 2a 70 43 78 3b 0a 20 20 42 74 43 rsor *pCx;. BtC
1e03a 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 ursor *pCrsr;.
1e03b 75 31 36 20 6e 46 69 65 6c 64 3b 0a 20 20 4d 65 u16 nField;. Me
1e03c 6d 20 2a 61 4d 65 6d 3b 0a 20 20 55 6e 70 61 63 m *aMem;. Unpac
1e03d 6b 65 64 52 65 63 6f 72 64 20 72 3b 20 20 20 20 kedRecord r;
1e03e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1e03f 20 42 2d 54 72 65 65 20 69 6e 64 65 78 20 73 65 B-Tree index se
1e040 61 72 63 68 20 6b 65 79 20 2a 2f 0a 20 20 69 36 arch key */. i6
1e041 34 20 52 3b 20 20 20 20 20 20 20 20 20 20 20 20 4 R;
1e042 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e043 20 2f 2a 20 52 6f 77 69 64 20 73 74 6f 72 65 64 /* Rowid stored
1e044 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 20 in register P3
1e045 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 */.#endif /* loc
1e046 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1e047 65 64 20 69 6e 74 6f 20 75 2e 62 63 20 2a 2f 0a ed into u.bc */.
1e048 0a 20 20 75 2e 62 63 2e 61 4d 65 6d 20 3d 20 26 . u.bc.aMem = &
1e049 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 34 2e p->aMem[pOp->p4.
1e04a 69 5d 3b 0a 20 20 2f 2a 20 41 73 73 65 72 74 20 i];. /* Assert
1e04b 74 68 61 74 20 74 68 65 20 76 61 6c 75 65 73 20 that the values
1e04c 6f 66 20 70 61 72 61 6d 65 74 65 72 73 20 50 31 of parameters P1
1e04d 20 61 6e 64 20 50 34 20 61 72 65 20 69 6e 20 72 and P4 are in r
1e04e 61 6e 67 65 2e 20 2a 2f 0a 20 20 61 73 73 65 72 ange. */. asser
1e04f 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d t( pOp->p4type==
1e050 50 34 5f 49 4e 54 33 32 20 29 3b 0a 20 20 61 73 P4_INT32 );. as
1e051 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 2e 69 3e sert( pOp->p4.i>
1e052 30 20 26 26 20 70 4f 70 2d 3e 70 34 2e 69 3c 3d 0 && pOp->p4.i<=
1e053 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 61 73 73 p->nMem );. ass
1e054 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 ert( pOp->p1>=0
1e055 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 && pOp->p1<p->nC
1e056 75 72 73 6f 72 20 29 3b 0a 0a 20 20 2f 2a 20 46 ursor );.. /* F
1e057 69 6e 64 20 74 68 65 20 69 6e 64 65 78 20 63 75 ind the index cu
1e058 72 73 6f 72 2e 20 2a 2f 0a 20 20 75 2e 62 63 2e rsor. */. u.bc.
1e059 70 43 78 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 pCx = p->apCsr[p
1e05a 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 Op->p1];. asser
1e05b 74 28 20 75 2e 62 63 2e 70 43 78 2d 3e 64 65 66 t( u.bc.pCx->def
1e05c 65 72 72 65 64 4d 6f 76 65 74 6f 3d 3d 30 20 29 erredMoveto==0 )
1e05d 3b 0a 20 20 75 2e 62 63 2e 70 43 78 2d 3e 73 65 ;. u.bc.pCx->se
1e05e 65 6b 52 65 73 75 6c 74 20 3d 20 30 3b 0a 20 20 ekResult = 0;.
1e05f 75 2e 62 63 2e 70 43 78 2d 3e 63 61 63 68 65 53 u.bc.pCx->cacheS
1e060 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 tatus = CACHE_ST
1e061 41 4c 45 3b 0a 20 20 75 2e 62 63 2e 70 43 72 73 ALE;. u.bc.pCrs
1e062 72 20 3d 20 75 2e 62 63 2e 70 43 78 2d 3e 70 43 r = u.bc.pCx->pC
1e063 75 72 73 6f 72 3b 0a 0a 20 20 2f 2a 20 49 66 20 ursor;.. /* If
1e064 61 6e 79 20 6f 66 20 74 68 65 20 76 61 6c 75 65 any of the value
1e065 73 20 61 72 65 20 4e 55 4c 4c 2c 20 74 61 6b 65 s are NULL, take
1e066 20 74 68 65 20 6a 75 6d 70 2e 20 2a 2f 0a 20 20 the jump. */.
1e067 75 2e 62 63 2e 6e 46 69 65 6c 64 20 3d 20 75 2e u.bc.nField = u.
1e068 62 63 2e 70 43 78 2d 3e 70 4b 65 79 49 6e 66 6f bc.pCx->pKeyInfo
1e069 2d 3e 6e 46 69 65 6c 64 3b 0a 20 20 66 6f 72 28 ->nField;. for(
1e06a 75 2e 62 63 2e 69 69 3d 30 3b 20 75 2e 62 63 2e u.bc.ii=0; u.bc.
1e06b 69 69 3c 75 2e 62 63 2e 6e 46 69 65 6c 64 3b 20 ii<u.bc.nField;
1e06c 75 2e 62 63 2e 69 69 2b 2b 29 7b 0a 20 20 20 20 u.bc.ii++){.
1e06d 69 66 28 20 75 2e 62 63 2e 61 4d 65 6d 5b 75 2e if( u.bc.aMem[u.
1e06e 62 63 2e 69 69 5d 2e 66 6c 61 67 73 20 26 20 4d bc.ii].flags & M
1e06f 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 20 EM_Null ){.
1e070 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 pc = pOp->p2 -
1e071 31 3b 0a 20 20 20 20 20 20 75 2e 62 63 2e 70 43 1;. u.bc.pC
1e072 72 73 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 62 rsr = 0;. b
1e073 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a reak;. }. }.
1e074 20 20 61 73 73 65 72 74 28 20 28 75 2e 62 63 2e assert( (u.bc.
1e075 61 4d 65 6d 5b 75 2e 62 63 2e 6e 46 69 65 6c 64 aMem[u.bc.nField
1e076 5d 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 ].flags & MEM_Nu
1e077 6c 6c 29 3d 3d 30 20 29 3b 0a 0a 20 20 69 66 28 ll)==0 );.. if(
1e078 20 75 2e 62 63 2e 70 43 72 73 72 21 3d 30 20 29 u.bc.pCrsr!=0 )
1e079 7b 0a 20 20 20 20 2f 2a 20 50 6f 70 75 6c 61 74 {. /* Populat
1e07a 65 20 74 68 65 20 69 6e 64 65 78 20 73 65 61 72 e the index sear
1e07b 63 68 20 6b 65 79 2e 20 2a 2f 0a 20 20 20 20 75 ch key. */. u
1e07c 2e 62 63 2e 72 2e 70 4b 65 79 49 6e 66 6f 20 3d .bc.r.pKeyInfo =
1e07d 20 75 2e 62 63 2e 70 43 78 2d 3e 70 4b 65 79 49 u.bc.pCx->pKeyI
1e07e 6e 66 6f 3b 0a 20 20 20 20 75 2e 62 63 2e 72 2e nfo;. u.bc.r.
1e07f 6e 46 69 65 6c 64 20 3d 20 75 2e 62 63 2e 6e 46 nField = u.bc.nF
1e080 69 65 6c 64 20 2b 20 31 3b 0a 20 20 20 20 75 2e ield + 1;. u.
1e081 62 63 2e 72 2e 66 6c 61 67 73 20 3d 20 55 4e 50 bc.r.flags = UNP
1e082 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 53 45 41 ACKED_PREFIX_SEA
1e083 52 43 48 3b 0a 20 20 20 20 75 2e 62 63 2e 72 2e RCH;. u.bc.r.
1e084 61 4d 65 6d 20 3d 20 75 2e 62 63 2e 61 4d 65 6d aMem = u.bc.aMem
1e085 3b 0a 0a 20 20 20 20 2f 2a 20 45 78 74 72 61 63 ;.. /* Extrac
1e086 74 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 75 t the value of u
1e087 2e 62 63 2e 52 20 66 72 6f 6d 20 72 65 67 69 73 .bc.R from regis
1e088 74 65 72 20 50 33 2e 20 2a 2f 0a 20 20 20 20 73 ter P3. */. s
1e089 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 qlite3VdbeMemInt
1e08a 65 67 65 72 69 66 79 28 70 49 6e 33 29 3b 0a 20 egerify(pIn3);.
1e08b 20 20 20 75 2e 62 63 2e 52 20 3d 20 70 49 6e 33 u.bc.R = pIn3
1e08c 2d 3e 75 2e 69 3b 0a 0a 20 20 20 20 2f 2a 20 53 ->u.i;.. /* S
1e08d 65 61 72 63 68 20 74 68 65 20 42 2d 54 72 65 65 earch the B-Tree
1e08e 20 69 6e 64 65 78 2e 20 49 66 20 6e 6f 20 63 6f index. If no co
1e08f 6e 66 6c 69 63 74 69 6e 67 20 72 65 63 6f 72 64 nflicting record
1e090 20 69 73 20 66 6f 75 6e 64 2c 20 6a 75 6d 70 0a is found, jump.
1e091 20 20 20 20 2a 2a 20 74 6f 20 50 32 2e 20 4f 74 ** to P2. Ot
1e092 68 65 72 77 69 73 65 2c 20 63 6f 70 79 20 74 68 herwise, copy th
1e093 65 20 72 6f 77 69 64 20 6f 66 20 74 68 65 20 63 e rowid of the c
1e094 6f 6e 66 6c 69 63 74 69 6e 67 20 72 65 63 6f 72 onflicting recor
1e095 64 20 74 6f 0a 20 20 20 20 2a 2a 20 72 65 67 69 d to. ** regi
1e096 73 74 65 72 20 50 33 20 61 6e 64 20 66 61 6c 6c ster P3 and fall
1e097 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 through to the
1e098 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e next instruction
1e099 2e 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 . */. rc = s
1e09a 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65 74 qlite3BtreeMovet
1e09b 6f 55 6e 70 61 63 6b 65 64 28 75 2e 62 63 2e 70 oUnpacked(u.bc.p
1e09c 43 72 73 72 2c 20 26 75 2e 62 63 2e 72 2c 20 30 Crsr, &u.bc.r, 0
1e09d 2c 20 30 2c 20 26 75 2e 62 63 2e 70 43 78 2d 3e , 0, &u.bc.pCx->
1e09e 73 65 65 6b 52 65 73 75 6c 74 29 3b 0a 20 20 20 seekResult);.
1e09f 20 69 66 28 20 28 75 2e 62 63 2e 72 2e 66 6c 61 if( (u.bc.r.fla
1e0a0 67 73 20 26 20 55 4e 50 41 43 4b 45 44 5f 50 52 gs & UNPACKED_PR
1e0a1 45 46 49 58 5f 53 45 41 52 43 48 29 20 7c 7c 20 EFIX_SEARCH) ||
1e0a2 75 2e 62 63 2e 72 2e 72 6f 77 69 64 3d 3d 75 2e u.bc.r.rowid==u.
1e0a3 62 63 2e 52 20 29 7b 0a 20 20 20 20 20 20 70 63 bc.R ){. pc
1e0a4 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a = pOp->p2 - 1;.
1e0a5 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1e0a6 20 70 49 6e 33 2d 3e 75 2e 69 20 3d 20 75 2e 62 pIn3->u.i = u.b
1e0a7 63 2e 72 2e 72 6f 77 69 64 3b 0a 20 20 20 20 7d c.r.rowid;. }
1e0a8 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a . }. break;.}.
1e0a9 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 6f 74 45 ./* Opcode: NotE
1e0aa 78 69 73 74 73 20 50 31 20 50 32 20 50 33 20 2a xists P1 P2 P3 *
1e0ab 20 2a 0a 2a 2a 0a 2a 2a 20 55 73 65 20 74 68 65 *.**.** Use the
1e0ac 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65 67 69 content of regi
1e0ad 73 74 65 72 20 50 33 20 61 73 20 61 20 69 6e 74 ster P3 as a int
1e0ae 65 67 65 72 20 6b 65 79 2e 20 20 49 66 20 61 20 eger key. If a
1e0af 72 65 63 6f 72 64 20 0a 2a 2a 20 77 69 74 68 20 record .** with
1e0b0 74 68 61 74 20 6b 65 79 20 64 6f 65 73 20 6e 6f that key does no
1e0b1 74 20 65 78 69 73 74 20 69 6e 20 74 61 62 6c 65 t exist in table
1e0b2 20 6f 66 20 50 31 2c 20 74 68 65 6e 20 6a 75 6d of P1, then jum
1e0b3 70 20 74 6f 20 50 32 2e 20 0a 2a 2a 20 49 66 20 p to P2. .** If
1e0b4 74 68 65 20 72 65 63 6f 72 64 20 64 6f 65 73 20 the record does
1e0b5 65 78 69 73 74 2c 20 74 68 65 6e 20 66 61 6c 6c exist, then fall
1e0b6 20 74 68 72 75 2e 20 20 54 68 65 20 63 75 72 73 thru. The curs
1e0b7 6f 72 20 69 73 20 6c 65 66 74 20 0a 2a 2a 20 70 or is left .** p
1e0b8 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 72 ointing to the r
1e0b9 65 63 6f 72 64 20 69 66 20 69 74 20 65 78 69 73 ecord if it exis
1e0ba 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 69 ts..**.** The di
1e0bb 66 66 65 72 65 6e 63 65 20 62 65 74 77 65 65 6e fference between
1e0bc 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 20 this operation
1e0bd 61 6e 64 20 4e 6f 74 46 6f 75 6e 64 20 69 73 20 and NotFound is
1e0be 74 68 61 74 20 74 68 69 73 0a 2a 2a 20 6f 70 65 that this.** ope
1e0bf 72 61 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 ration assumes t
1e0c0 68 65 20 6b 65 79 20 69 73 20 61 6e 20 69 6e 74 he key is an int
1e0c1 65 67 65 72 20 61 6e 64 20 74 68 61 74 20 50 31 eger and that P1
1e0c2 20 69 73 20 61 20 74 61 62 6c 65 20 77 68 65 72 is a table wher
1e0c3 65 61 73 0a 2a 2a 20 4e 6f 74 46 6f 75 6e 64 20 eas.** NotFound
1e0c4 61 73 73 75 6d 65 73 20 6b 65 79 20 69 73 20 61 assumes key is a
1e0c5 20 62 6c 6f 62 20 63 6f 6e 73 74 72 75 63 74 65 blob constructe
1e0c6 64 20 66 72 6f 6d 20 4d 61 6b 65 52 65 63 6f 72 d from MakeRecor
1e0c7 64 20 61 6e 64 0a 2a 2a 20 50 31 20 69 73 20 61 d and.** P1 is a
1e0c8 6e 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 53 n index..**.** S
1e0c9 65 65 20 61 6c 73 6f 3a 20 46 6f 75 6e 64 2c 20 ee also: Found,
1e0ca 4e 6f 74 46 6f 75 6e 64 2c 20 49 73 55 6e 69 71 NotFound, IsUniq
1e0cb 75 65 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 6f ue.*/.case OP_No
1e0cc 74 45 78 69 73 74 73 3a 20 7b 20 20 20 20 20 20 tExists: {
1e0cd 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a /* jump, in3 *
1e0ce 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 /.#if 0 /* loca
1e0cf 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 l variables move
1e0d0 64 20 69 6e 74 6f 20 75 2e 62 64 20 2a 2f 0a 20 d into u.bd */.
1e0d1 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b VdbeCursor *pC;
1e0d2 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 . BtCursor *pCr
1e0d3 73 72 3b 0a 20 20 69 6e 74 20 72 65 73 3b 0a 20 sr;. int res;.
1e0d4 20 75 36 34 20 69 4b 65 79 3b 0a 23 65 6e 64 69 u64 iKey;.#endi
1e0d5 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 f /* local varia
1e0d6 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 bles moved into
1e0d7 75 2e 62 64 20 2a 2f 0a 0a 20 20 61 73 73 65 72 u.bd */.. asser
1e0d8 74 28 20 70 49 6e 33 2d 3e 66 6c 61 67 73 20 26 t( pIn3->flags &
1e0d9 20 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 61 73 MEM_Int );. as
1e0da 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 sert( pOp->p1>=0
1e0db 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e && pOp->p1<p->n
1e0dc 43 75 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 64 Cursor );. u.bd
1e0dd 2e 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 .pC = p->apCsr[p
1e0de 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 Op->p1];. asser
1e0df 74 28 20 75 2e 62 64 2e 70 43 21 3d 30 20 29 3b t( u.bd.pC!=0 );
1e0e0 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 64 2e . assert( u.bd.
1e0e1 70 43 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 pC->isTable );.
1e0e2 20 61 73 73 65 72 74 28 20 75 2e 62 64 2e 70 43 assert( u.bd.pC
1e0e3 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65 52 65 67 ->pseudoTableReg
1e0e4 3d 3d 30 20 29 3b 0a 20 20 75 2e 62 64 2e 70 43 ==0 );. u.bd.pC
1e0e5 72 73 72 20 3d 20 75 2e 62 64 2e 70 43 2d 3e 70 rsr = u.bd.pC->p
1e0e6 43 75 72 73 6f 72 3b 0a 20 20 69 66 28 20 75 2e Cursor;. if( u.
1e0e7 62 64 2e 70 43 72 73 72 21 3d 30 20 29 7b 0a 20 bd.pCrsr!=0 ){.
1e0e8 20 20 20 75 2e 62 64 2e 72 65 73 20 3d 20 30 3b u.bd.res = 0;
1e0e9 0a 20 20 20 20 75 2e 62 64 2e 69 4b 65 79 20 3d . u.bd.iKey =
1e0ea 20 70 49 6e 33 2d 3e 75 2e 69 3b 0a 20 20 20 20 pIn3->u.i;.
1e0eb 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 rc = sqlite3Btre
1e0ec 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 eMovetoUnpacked(
1e0ed 75 2e 62 64 2e 70 43 72 73 72 2c 20 30 2c 20 75 u.bd.pCrsr, 0, u
1e0ee 2e 62 64 2e 69 4b 65 79 2c 20 30 2c 20 26 75 2e .bd.iKey, 0, &u.
1e0ef 62 64 2e 72 65 73 29 3b 0a 20 20 20 20 75 2e 62 bd.res);. u.b
1e0f0 64 2e 70 43 2d 3e 6c 61 73 74 52 6f 77 69 64 20 d.pC->lastRowid
1e0f1 3d 20 70 49 6e 33 2d 3e 75 2e 69 3b 0a 20 20 20 = pIn3->u.i;.
1e0f2 20 75 2e 62 64 2e 70 43 2d 3e 72 6f 77 69 64 49 u.bd.pC->rowidI
1e0f3 73 56 61 6c 69 64 20 3d 20 75 2e 62 64 2e 72 65 sValid = u.bd.re
1e0f4 73 3d 3d 30 20 3f 31 3a 30 3b 0a 20 20 20 20 75 s==0 ?1:0;. u
1e0f5 2e 62 64 2e 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 .bd.pC->nullRow
1e0f6 3d 20 30 3b 0a 20 20 20 20 75 2e 62 64 2e 70 43 = 0;. u.bd.pC
1e0f7 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 ->cacheStatus =
1e0f8 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 20 CACHE_STALE;.
1e0f9 20 75 2e 62 64 2e 70 43 2d 3e 64 65 66 65 72 72 u.bd.pC->deferr
1e0fa 65 64 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a 20 20 edMoveto = 0;.
1e0fb 20 20 69 66 28 20 75 2e 62 64 2e 72 65 73 21 3d if( u.bd.res!=
1e0fc 30 20 29 7b 0a 20 20 20 20 20 20 70 63 20 3d 20 0 ){. pc =
1e0fd 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 pOp->p2 - 1;.
1e0fe 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 64 2e assert( u.bd.
1e0ff 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 pC->rowidIsValid
1e100 3d 3d 30 20 29 3b 0a 20 20 20 20 7d 0a 20 20 20 ==0 );. }.
1e101 20 75 2e 62 64 2e 70 43 2d 3e 73 65 65 6b 52 65 u.bd.pC->seekRe
1e102 73 75 6c 74 20 3d 20 75 2e 62 64 2e 72 65 73 3b sult = u.bd.res;
1e103 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a . }else{. /*
1e104 20 54 68 69 73 20 68 61 70 70 65 6e 73 20 77 68 This happens wh
1e105 65 6e 20 61 6e 20 61 74 74 65 6d 70 74 20 74 6f en an attempt to
1e106 20 6f 70 65 6e 20 61 20 72 65 61 64 20 63 75 72 open a read cur
1e107 73 6f 72 20 6f 6e 20 74 68 65 0a 20 20 20 20 2a sor on the. *
1e108 2a 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 * sqlite_master
1e109 74 61 62 6c 65 20 72 65 74 75 72 6e 73 20 53 51 table returns SQ
1e10a 4c 49 54 45 5f 45 4d 50 54 59 2e 0a 20 20 20 20 LITE_EMPTY..
1e10b 2a 2f 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d */. pc = pOp-
1e10c 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 61 73 73 >p2 - 1;. ass
1e10d 65 72 74 28 20 75 2e 62 64 2e 70 43 2d 3e 72 6f ert( u.bd.pC->ro
1e10e 77 69 64 49 73 56 61 6c 69 64 3d 3d 30 20 29 3b widIsValid==0 );
1e10f 0a 20 20 20 20 75 2e 62 64 2e 70 43 2d 3e 73 65 . u.bd.pC->se
1e110 65 6b 52 65 73 75 6c 74 20 3d 20 30 3b 0a 20 20 ekResult = 0;.
1e111 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a }. break;.}../*
1e112 20 4f 70 63 6f 64 65 3a 20 53 65 71 75 65 6e 63 Opcode: Sequenc
1e113 65 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a e P1 P2 * * *.**
1e114 0a 2a 2a 20 46 69 6e 64 20 74 68 65 20 6e 65 78 .** Find the nex
1e115 74 20 61 76 61 69 6c 61 62 6c 65 20 73 65 71 75 t available sequ
1e116 65 6e 63 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 ence number for
1e117 63 75 72 73 6f 72 20 50 31 2e 0a 2a 2a 20 57 72 cursor P1..** Wr
1e118 69 74 65 20 74 68 65 20 73 65 71 75 65 6e 63 65 ite the sequence
1e119 20 6e 75 6d 62 65 72 20 69 6e 74 6f 20 72 65 67 number into reg
1e11a 69 73 74 65 72 20 50 32 2e 0a 2a 2a 20 54 68 65 ister P2..** The
1e11b 20 73 65 71 75 65 6e 63 65 20 6e 75 6d 62 65 72 sequence number
1e11c 20 6f 6e 20 74 68 65 20 63 75 72 73 6f 72 20 69 on the cursor i
1e11d 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 61 66 s incremented af
1e11e 74 65 72 20 74 68 69 73 0a 2a 2a 20 69 6e 73 74 ter this.** inst
1e11f 72 75 63 74 69 6f 6e 2e 20 20 0a 2a 2f 0a 63 61 ruction. .*/.ca
1e120 73 65 20 4f 50 5f 53 65 71 75 65 6e 63 65 3a 20 se OP_Sequence:
1e121 7b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6f { /* o
1e122 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a ut2-prerelease *
1e123 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d /. assert( pOp-
1e124 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 >p1>=0 && pOp->p
1e125 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 1<p->nCursor );.
1e126 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61 70 43 assert( p->apC
1e127 73 72 5b 70 4f 70 2d 3e 70 31 5d 21 3d 30 20 29 sr[pOp->p1]!=0 )
1e128 3b 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 ;. pOut->u.i =
1e129 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 p->apCsr[pOp->p1
1e12a 5d 2d 3e 73 65 71 43 6f 75 6e 74 2b 2b 3b 0a 20 ]->seqCount++;.
1e12b 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 MemSetTypeFlag(
1e12c 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a pOut, MEM_Int);.
1e12d 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0a 2f 2a 20 break;.}.../*
1e12e 4f 70 63 6f 64 65 3a 20 4e 65 77 52 6f 77 69 64 Opcode: NewRowid
1e12f 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a P1 P2 P3 * *.**
1e130 0a 2a 2a 20 47 65 74 20 61 20 6e 65 77 20 69 6e .** Get a new in
1e131 74 65 67 65 72 20 72 65 63 6f 72 64 20 6e 75 6d teger record num
1e132 62 65 72 20 28 61 2e 6b 2e 61 20 22 72 6f 77 69 ber (a.k.a "rowi
1e133 64 22 29 20 75 73 65 64 20 61 73 20 74 68 65 20 d") used as the
1e134 6b 65 79 20 74 6f 20 61 20 74 61 62 6c 65 2e 0a key to a table..
1e135 2a 2a 20 54 68 65 20 72 65 63 6f 72 64 20 6e 75 ** The record nu
1e136 6d 62 65 72 20 69 73 20 6e 6f 74 20 70 72 65 76 mber is not prev
1e137 69 6f 75 73 6c 79 20 75 73 65 64 20 61 73 20 61 iously used as a
1e138 20 6b 65 79 20 69 6e 20 74 68 65 20 64 61 74 61 key in the data
1e139 62 61 73 65 0a 2a 2a 20 74 61 62 6c 65 20 74 68 base.** table th
1e13a 61 74 20 63 75 72 73 6f 72 20 50 31 20 70 6f 69 at cursor P1 poi
1e13b 6e 74 73 20 74 6f 2e 20 20 54 68 65 20 6e 65 77 nts to. The new
1e13c 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 69 record number i
1e13d 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20 77 72 69 s written.** wri
1e13e 74 74 65 6e 20 74 6f 20 72 65 67 69 73 74 65 72 tten to register
1e13f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 33 P2..**.** If P3
1e140 3e 30 20 74 68 65 6e 20 50 33 20 69 73 20 61 20 >0 then P3 is a
1e141 72 65 67 69 73 74 65 72 20 69 6e 20 74 68 65 20 register in the
1e142 72 6f 6f 74 20 66 72 61 6d 65 20 6f 66 20 74 68 root frame of th
1e143 69 73 20 56 44 42 45 20 74 68 61 74 20 68 6f 6c is VDBE that hol
1e144 64 73 20 0a 2a 2a 20 74 68 65 20 6c 61 72 67 65 ds .** the large
1e145 73 74 20 70 72 65 76 69 6f 75 73 6c 79 20 67 65 st previously ge
1e146 6e 65 72 61 74 65 64 20 72 65 63 6f 72 64 20 6e nerated record n
1e147 75 6d 62 65 72 2e 20 4e 6f 20 6e 65 77 20 72 65 umber. No new re
1e148 63 6f 72 64 20 6e 75 6d 62 65 72 73 20 61 72 65 cord numbers are
1e149 0a 2a 2a 20 61 6c 6c 6f 77 65 64 20 74 6f 20 62 .** allowed to b
1e14a 65 20 6c 65 73 73 20 74 68 61 6e 20 74 68 69 73 e less than this
1e14b 20 76 61 6c 75 65 2e 20 57 68 65 6e 20 74 68 69 value. When thi
1e14c 73 20 76 61 6c 75 65 20 72 65 61 63 68 65 73 20 s value reaches
1e14d 69 74 73 20 6d 61 78 69 6d 75 6d 2c 20 0a 2a 2a its maximum, .**
1e14e 20 61 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 65 a SQLITE_FULL e
1e14f 72 72 6f 72 20 69 73 20 67 65 6e 65 72 61 74 65 rror is generate
1e150 64 2e 20 54 68 65 20 50 33 20 72 65 67 69 73 74 d. The P3 regist
1e151 65 72 20 69 73 20 75 70 64 61 74 65 64 20 77 69 er is updated wi
1e152 74 68 20 74 68 65 20 27 0a 2a 2a 20 67 65 6e 65 th the '.** gene
1e153 72 61 74 65 64 20 72 65 63 6f 72 64 20 6e 75 6d rated record num
1e154 62 65 72 2e 20 54 68 69 73 20 50 33 20 6d 65 63 ber. This P3 mec
1e155 68 61 6e 69 73 6d 20 69 73 20 75 73 65 64 20 74 hanism is used t
1e156 6f 20 68 65 6c 70 20 69 6d 70 6c 65 6d 65 6e 74 o help implement
1e157 20 74 68 65 0a 2a 2a 20 41 55 54 4f 49 4e 43 52 the.** AUTOINCR
1e158 45 4d 45 4e 54 20 66 65 61 74 75 72 65 2e 0a 2a EMENT feature..*
1e159 2f 0a 63 61 73 65 20 4f 50 5f 4e 65 77 52 6f 77 /.case OP_NewRow
1e15a 69 64 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 id: {
1e15b 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 /* out2-prerelea
1e15c 73 65 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 se */.#if 0 /*
1e15d 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 local variables
1e15e 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 65 20 moved into u.be
1e15f 2a 2f 0a 20 20 69 36 34 20 76 3b 20 20 20 20 20 */. i64 v;
1e160 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
1e161 68 65 20 6e 65 77 20 72 6f 77 69 64 20 2a 2f 0a he new rowid */.
1e162 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 VdbeCursor *pC
1e163 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73 ; /* Curs
1e164 6f 72 20 6f 66 20 74 61 62 6c 65 20 74 6f 20 67 or of table to g
1e165 65 74 20 74 68 65 20 6e 65 77 20 72 6f 77 69 64 et the new rowid
1e166 20 2a 2f 0a 20 20 69 6e 74 20 72 65 73 3b 20 20 */. int res;
1e167 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1e168 52 65 73 75 6c 74 20 6f 66 20 61 6e 20 73 71 6c Result of an sql
1e169 69 74 65 33 42 74 72 65 65 4c 61 73 74 28 29 20 ite3BtreeLast()
1e16a 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 3b 20 20 20 */. int cnt;
1e16b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 /* C
1e16c 6f 75 6e 74 65 72 20 74 6f 20 6c 69 6d 69 74 20 ounter to limit
1e16d 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 65 the number of se
1e16e 61 72 63 68 65 73 20 2a 2f 0a 20 20 4d 65 6d 20 arches */. Mem
1e16f 2a 70 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 *pMem;
1e170 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68 /* Register h
1e171 6f 6c 64 69 6e 67 20 6c 61 72 67 65 73 74 20 72 olding largest r
1e172 6f 77 69 64 20 66 6f 72 20 41 55 54 4f 49 4e 43 owid for AUTOINC
1e173 52 45 4d 45 4e 54 20 2a 2f 0a 20 20 56 64 62 65 REMENT */. Vdbe
1e174 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 3b 20 20 Frame *pFrame;
1e175 20 20 20 2f 2a 20 52 6f 6f 74 20 66 72 61 6d 65 /* Root frame
1e176 20 6f 66 20 56 44 42 45 20 2a 2f 0a 23 65 6e 64 of VDBE */.#end
1e177 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 if /* local vari
1e178 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1e179 20 75 2e 62 65 20 2a 2f 0a 0a 20 20 75 2e 62 65 u.be */.. u.be
1e17a 2e 76 20 3d 20 30 3b 0a 20 20 75 2e 62 65 2e 72 .v = 0;. u.be.r
1e17b 65 73 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 es = 0;. assert
1e17c 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 ( pOp->p1>=0 &&
1e17d 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 pOp->p1<p->nCurs
1e17e 6f 72 20 29 3b 0a 20 20 75 2e 62 65 2e 70 43 20 or );. u.be.pC
1e17f 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e = p->apCsr[pOp->
1e180 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75 p1];. assert( u
1e181 2e 62 65 2e 70 43 21 3d 30 20 29 3b 0a 20 20 69 .be.pC!=0 );. i
1e182 66 28 20 4e 45 56 45 52 28 75 2e 62 65 2e 70 43 f( NEVER(u.be.pC
1e183 2d 3e 70 43 75 72 73 6f 72 3d 3d 30 29 20 29 7b ->pCursor==0) ){
1e184 0a 20 20 20 20 2f 2a 20 54 68 65 20 7a 65 72 6f . /* The zero
1e185 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 initialization
1e186 61 62 6f 76 65 20 69 73 20 61 6c 6c 20 74 68 61 above is all tha
1e187 74 20 69 73 20 6e 65 65 64 65 64 20 2a 2f 0a 20 t is needed */.
1e188 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54 }else{. /* T
1e189 68 65 20 6e 65 78 74 20 72 6f 77 69 64 20 6f 72 he next rowid or
1e18a 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 28 record number (
1e18b 64 69 66 66 65 72 65 6e 74 20 74 65 72 6d 73 20 different terms
1e18c 66 6f 72 20 74 68 65 20 73 61 6d 65 0a 20 20 20 for the same.
1e18d 20 2a 2a 20 74 68 69 6e 67 29 20 69 73 20 6f 62 ** thing) is ob
1e18e 74 61 69 6e 65 64 20 69 6e 20 61 20 74 77 6f 2d tained in a two-
1e18f 73 74 65 70 20 61 6c 67 6f 72 69 74 68 6d 2e 0a step algorithm..
1e190 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 46 69 **. ** Fi
1e191 72 73 74 20 77 65 20 61 74 74 65 6d 70 74 20 74 rst we attempt t
1e192 6f 20 66 69 6e 64 20 74 68 65 20 6c 61 72 67 65 o find the large
1e193 73 74 20 65 78 69 73 74 69 6e 67 20 72 6f 77 69 st existing rowi
1e194 64 20 61 6e 64 20 61 64 64 20 6f 6e 65 0a 20 20 d and add one.
1e195 20 20 2a 2a 20 74 6f 20 74 68 61 74 2e 20 20 42 ** to that. B
1e196 75 74 20 69 66 20 74 68 65 20 6c 61 72 67 65 73 ut if the larges
1e197 74 20 65 78 69 73 74 69 6e 67 20 72 6f 77 69 64 t existing rowid
1e198 20 69 73 20 61 6c 72 65 61 64 79 20 74 68 65 20 is already the
1e199 6d 61 78 69 6d 75 6d 0a 20 20 20 20 2a 2a 20 70 maximum. ** p
1e19a 6f 73 69 74 69 76 65 20 69 6e 74 65 67 65 72 2c ositive integer,
1e19b 20 77 65 20 68 61 76 65 20 74 6f 20 66 61 6c 6c we have to fall
1e19c 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 through to the
1e19d 73 65 63 6f 6e 64 0a 20 20 20 20 2a 2a 20 70 72 second. ** pr
1e19e 6f 62 61 62 69 6c 69 73 74 69 63 20 61 6c 67 6f obabilistic algo
1e19f 72 69 74 68 6d 0a 20 20 20 20 2a 2a 0a 20 20 20 rithm. **.
1e1a0 20 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 61 ** The second a
1e1a1 6c 67 6f 72 69 74 68 6d 20 69 73 20 74 6f 20 73 lgorithm is to s
1e1a2 65 6c 65 63 74 20 61 20 72 6f 77 69 64 20 61 74 elect a rowid at
1e1a3 20 72 61 6e 64 6f 6d 20 61 6e 64 20 73 65 65 20 random and see
1e1a4 69 66 0a 20 20 20 20 2a 2a 20 69 74 20 61 6c 72 if. ** it alr
1e1a5 65 61 64 79 20 65 78 69 73 74 73 20 69 6e 20 74 eady exists in t
1e1a6 68 65 20 74 61 62 6c 65 2e 20 20 49 66 20 69 74 he table. If it
1e1a7 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 2c does not exist,
1e1a8 20 77 65 20 68 61 76 65 0a 20 20 20 20 2a 2a 20 we have. **
1e1a9 73 75 63 63 65 65 64 65 64 2e 20 20 49 66 20 74 succeeded. If t
1e1aa 68 65 20 72 61 6e 64 6f 6d 20 72 6f 77 69 64 20 he random rowid
1e1ab 64 6f 65 73 20 65 78 69 73 74 2c 20 77 65 20 73 does exist, we s
1e1ac 65 6c 65 63 74 20 61 20 6e 65 77 20 6f 6e 65 0a elect a new one.
1e1ad 20 20 20 20 2a 2a 20 61 6e 64 20 74 72 79 20 61 ** and try a
1e1ae 67 61 69 6e 2c 20 75 70 20 74 6f 20 31 30 30 20 gain, up to 100
1e1af 74 69 6d 65 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 times.. */.
1e1b0 20 20 61 73 73 65 72 74 28 20 75 2e 62 65 2e 70 assert( u.be.p
1e1b1 43 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 C->isTable );.
1e1b2 20 20 75 2e 62 65 2e 63 6e 74 20 3d 20 30 3b 0a u.be.cnt = 0;.
1e1b3 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 33 .#ifdef SQLITE_3
1e1b4 32 42 49 54 5f 52 4f 57 49 44 0a 23 20 20 20 64 2BIT_ROWID.# d
1e1b5 65 66 69 6e 65 20 4d 41 58 5f 52 4f 57 49 44 20 efine MAX_ROWID
1e1b6 30 78 37 66 66 66 66 66 66 66 0a 23 65 6c 73 65 0x7fffffff.#else
1e1b7 0a 20 20 20 20 2f 2a 20 53 6f 6d 65 20 63 6f 6d . /* Some com
1e1b8 70 69 6c 65 72 73 20 63 6f 6d 70 6c 61 69 6e 20 pilers complain
1e1b9 61 62 6f 75 74 20 63 6f 6e 73 74 61 6e 74 73 20 about constants
1e1ba 6f 66 20 74 68 65 20 66 6f 72 6d 20 30 78 37 66 of the form 0x7f
1e1bb 66 66 66 66 66 66 66 66 66 66 66 66 66 66 2e 0a ffffffffffffff..
1e1bc 20 20 20 20 2a 2a 20 4f 74 68 65 72 73 20 63 6f ** Others co
1e1bd 6d 70 6c 61 69 6e 20 61 62 6f 75 74 20 30 78 37 mplain about 0x7
1e1be 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66 ffffffffffffffff
1e1bf 4c 4c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 LL. The followi
1e1c0 6e 67 20 6d 61 63 72 6f 20 73 65 65 6d 73 0a 20 ng macro seems.
1e1c1 20 20 20 2a 2a 20 74 6f 20 70 72 6f 76 69 64 65 ** to provide
1e1c2 20 74 68 65 20 63 6f 6e 73 74 61 6e 74 20 77 68 the constant wh
1e1c3 69 6c 65 20 6d 61 6b 69 6e 67 20 61 6c 6c 20 63 ile making all c
1e1c4 6f 6d 70 69 6c 65 72 73 20 68 61 70 70 79 2e 0a ompilers happy..
1e1c5 20 20 20 20 2a 2f 0a 23 20 20 20 64 65 66 69 6e */.# defin
1e1c6 65 20 4d 41 58 5f 52 4f 57 49 44 20 20 28 69 36 e MAX_ROWID (i6
1e1c7 34 29 28 20 28 28 28 75 36 34 29 30 78 37 66 66 4)( (((u64)0x7ff
1e1c8 66 66 66 66 66 29 3c 3c 33 32 29 20 7c 20 28 75 fffff)<<32) | (u
1e1c9 36 34 29 30 78 66 66 66 66 66 66 66 66 20 29 0a 64)0xffffffff ).
1e1ca 23 65 6e 64 69 66 0a 0a 20 20 20 20 69 66 28 20 #endif.. if(
1e1cb 21 75 2e 62 65 2e 70 43 2d 3e 75 73 65 52 61 6e !u.be.pC->useRan
1e1cc 64 6f 6d 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 domRowid ){.
1e1cd 20 20 75 2e 62 65 2e 76 20 3d 20 73 71 6c 69 74 u.be.v = sqlit
1e1ce 65 33 42 74 72 65 65 47 65 74 43 61 63 68 65 64 e3BtreeGetCached
1e1cf 52 6f 77 69 64 28 75 2e 62 65 2e 70 43 2d 3e 70 Rowid(u.be.pC->p
1e1d0 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 20 20 69 Cursor);. i
1e1d1 66 28 20 75 2e 62 65 2e 76 3d 3d 30 20 29 7b 0a f( u.be.v==0 ){.
1e1d2 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c rc = sql
1e1d3 69 74 65 33 42 74 72 65 65 4c 61 73 74 28 75 2e ite3BtreeLast(u.
1e1d4 62 65 2e 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 be.pC->pCursor,
1e1d5 26 75 2e 62 65 2e 72 65 73 29 3b 0a 20 20 20 20 &u.be.res);.
1e1d6 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
1e1d7 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
1e1d8 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 goto abort_du
1e1d9 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 e_to_error;.
1e1da 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 }. if
1e1db 28 20 75 2e 62 65 2e 72 65 73 20 29 7b 0a 20 20 ( u.be.res ){.
1e1dc 20 20 20 20 20 20 20 20 75 2e 62 65 2e 76 20 3d u.be.v =
1e1dd 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 1;. }els
1e1de 65 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 e{. ass
1e1df 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 ert( sqlite3Btre
1e1e0 65 43 75 72 73 6f 72 49 73 56 61 6c 69 64 28 75 eCursorIsValid(u
1e1e1 2e 62 65 2e 70 43 2d 3e 70 43 75 72 73 6f 72 29 .be.pC->pCursor)
1e1e2 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63 );. rc
1e1e3 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b = sqlite3BtreeK
1e1e4 65 79 53 69 7a 65 28 75 2e 62 65 2e 70 43 2d 3e eySize(u.be.pC->
1e1e5 70 43 75 72 73 6f 72 2c 20 26 75 2e 62 65 2e 76 pCursor, &u.be.v
1e1e6 29 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 );. ass
1e1e7 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f ert( rc==SQLITE_
1e1e8 4f 4b 20 29 3b 20 20 20 2f 2a 20 43 61 6e 6e 6f OK ); /* Canno
1e1e9 74 20 66 61 69 6c 20 66 6f 6c 6c 6f 77 69 6e 67 t fail following
1e1ea 20 42 74 72 65 65 4c 61 73 74 28 29 20 2a 2f 0a BtreeLast() */.
1e1eb 20 20 20 20 20 20 20 20 20 20 69 66 28 20 75 2e if( u.
1e1ec 62 65 2e 76 3d 3d 4d 41 58 5f 52 4f 57 49 44 20 be.v==MAX_ROWID
1e1ed 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 75 ){. u
1e1ee 2e 62 65 2e 70 43 2d 3e 75 73 65 52 61 6e 64 6f .be.pC->useRando
1e1ef 6d 52 6f 77 69 64 20 3d 20 31 3b 0a 20 20 20 20 mRowid = 1;.
1e1f0 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
1e1f1 20 20 20 20 20 20 20 20 20 75 2e 62 65 2e 76 2b u.be.v+
1e1f2 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 +;. }.
1e1f3 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
1e1f4 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
1e1f5 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d _OMIT_AUTOINCREM
1e1f6 45 4e 54 0a 20 20 20 20 20 20 69 66 28 20 70 4f ENT. if( pO
1e1f7 70 2d 3e 70 33 20 29 7b 0a 20 20 20 20 20 20 20 p->p3 ){.
1e1f8 20 2f 2a 20 41 73 73 65 72 74 20 74 68 61 74 20 /* Assert that
1e1f9 50 33 20 69 73 20 61 20 76 61 6c 69 64 20 6d 65 P3 is a valid me
1e1fa 6d 6f 72 79 20 63 65 6c 6c 2e 20 2a 2f 0a 20 20 mory cell. */.
1e1fb 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f assert( pO
1e1fc 70 2d 3e 70 33 3e 30 20 29 3b 0a 20 20 20 20 20 p->p3>0 );.
1e1fd 20 20 20 69 66 28 20 70 2d 3e 70 46 72 61 6d 65 if( p->pFrame
1e1fe 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66 6f ){. fo
1e1ff 72 28 75 2e 62 65 2e 70 46 72 61 6d 65 3d 70 2d r(u.be.pFrame=p-
1e200 3e 70 46 72 61 6d 65 3b 20 75 2e 62 65 2e 70 46 >pFrame; u.be.pF
1e201 72 61 6d 65 2d 3e 70 50 61 72 65 6e 74 3b 20 75 rame->pParent; u
1e202 2e 62 65 2e 70 46 72 61 6d 65 3d 75 2e 62 65 2e .be.pFrame=u.be.
1e203 70 46 72 61 6d 65 2d 3e 70 50 61 72 65 6e 74 29 pFrame->pParent)
1e204 3b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 ;. /* A
1e205 73 73 65 72 74 20 74 68 61 74 20 50 33 20 69 73 ssert that P3 is
1e206 20 61 20 76 61 6c 69 64 20 6d 65 6d 6f 72 79 20 a valid memory
1e207 63 65 6c 6c 2e 20 2a 2f 0a 20 20 20 20 20 20 20 cell. */.
1e208 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e assert( pOp->
1e209 70 33 3c 3d 75 2e 62 65 2e 70 46 72 61 6d 65 2d p3<=u.be.pFrame-
1e20a 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20 20 20 >nMem );.
1e20b 20 20 20 75 2e 62 65 2e 70 4d 65 6d 20 3d 20 26 u.be.pMem = &
1e20c 75 2e 62 65 2e 70 46 72 61 6d 65 2d 3e 61 4d 65 u.be.pFrame->aMe
1e20d 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20 20 m[pOp->p3];.
1e20e 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1e20f 20 20 20 20 20 2f 2a 20 41 73 73 65 72 74 20 74 /* Assert t
1e210 68 61 74 20 50 33 20 69 73 20 61 20 76 61 6c 69 hat P3 is a vali
1e211 64 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 2e 20 2a d memory cell. *
1e212 2f 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 /. asse
1e213 72 74 28 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e rt( pOp->p3<=p->
1e214 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20 20 20 20 nMem );.
1e215 20 20 75 2e 62 65 2e 70 4d 65 6d 20 3d 20 26 70 u.be.pMem = &p
1e216 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b ->aMem[pOp->p3];
1e217 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 . }..
1e218 20 20 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 REGISTER_TRA
1e219 43 45 28 70 4f 70 2d 3e 70 33 2c 20 75 2e 62 65 CE(pOp->p3, u.be
1e21a 2e 70 4d 65 6d 29 3b 0a 20 20 20 20 20 20 20 20 .pMem);.
1e21b 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e sqlite3VdbeMemIn
1e21c 74 65 67 65 72 69 66 79 28 75 2e 62 65 2e 70 4d tegerify(u.be.pM
1e21d 65 6d 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 em);. ass
1e21e 65 72 74 28 20 28 75 2e 62 65 2e 70 4d 65 6d 2d ert( (u.be.pMem-
1e21f 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 >flags & MEM_Int
1e220 29 21 3d 30 20 29 3b 20 20 2f 2a 20 6d 65 6d 28 )!=0 ); /* mem(
1e221 50 33 29 20 68 6f 6c 64 73 20 61 6e 20 69 6e 74 P3) holds an int
1e222 65 67 65 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 eger */.
1e223 69 66 28 20 75 2e 62 65 2e 70 4d 65 6d 2d 3e 75 if( u.be.pMem->u
1e224 2e 69 3d 3d 4d 41 58 5f 52 4f 57 49 44 20 7c 7c .i==MAX_ROWID ||
1e225 20 75 2e 62 65 2e 70 43 2d 3e 75 73 65 52 61 6e u.be.pC->useRan
1e226 64 6f 6d 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 domRowid ){.
1e227 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 rc = SQLIT
1e228 45 5f 46 55 4c 4c 3b 0a 20 20 20 20 20 20 20 20 E_FULL;.
1e229 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 goto abort_due
1e22a 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 _to_error;.
1e22b 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 }. if(
1e22c 20 75 2e 62 65 2e 76 3c 75 2e 62 65 2e 70 4d 65 u.be.v<u.be.pMe
1e22d 6d 2d 3e 75 2e 69 2b 31 20 29 7b 0a 20 20 20 20 m->u.i+1 ){.
1e22e 20 20 20 20 20 20 75 2e 62 65 2e 76 20 3d 20 75 u.be.v = u
1e22f 2e 62 65 2e 70 4d 65 6d 2d 3e 75 2e 69 20 2b 20 .be.pMem->u.i +
1e230 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 1;. }.
1e231 20 20 20 20 20 75 2e 62 65 2e 70 4d 65 6d 2d 3e u.be.pMem->
1e232 75 2e 69 20 3d 20 75 2e 62 65 2e 76 3b 0a 20 20 u.i = u.be.v;.
1e233 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 }.#endif..
1e234 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 sqlite3Btree
1e235 53 65 74 43 61 63 68 65 64 52 6f 77 69 64 28 75 SetCachedRowid(u
1e236 2e 62 65 2e 70 43 2d 3e 70 43 75 72 73 6f 72 2c .be.pC->pCursor,
1e237 20 75 2e 62 65 2e 76 3c 4d 41 58 5f 52 4f 57 49 u.be.v<MAX_ROWI
1e238 44 20 3f 20 75 2e 62 65 2e 76 2b 31 20 3a 20 30 D ? u.be.v+1 : 0
1e239 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 );. }. if(
1e23a 20 75 2e 62 65 2e 70 43 2d 3e 75 73 65 52 61 6e u.be.pC->useRan
1e23b 64 6f 6d 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 domRowid ){.
1e23c 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
1e23d 33 3d 3d 30 20 29 3b 20 20 2f 2a 20 57 65 20 63 3==0 ); /* We c
1e23e 61 6e 6e 6f 74 20 62 65 20 69 6e 20 72 61 6e 64 annot be in rand
1e23f 6f 6d 20 72 6f 77 69 64 20 6d 6f 64 65 20 69 66 om rowid mode if
1e240 20 74 68 69 73 20 69 73 0a 20 20 20 20 20 20 20 this is.
1e241 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e242 20 20 20 20 20 20 2a 2a 20 61 6e 20 41 55 54 4f ** an AUTO
1e243 49 4e 43 52 45 4d 45 4e 54 20 74 61 62 6c 65 2e INCREMENT table.
1e244 20 2a 2f 0a 20 20 20 20 20 20 75 2e 62 65 2e 76 */. u.be.v
1e245 20 3d 20 64 62 2d 3e 6c 61 73 74 52 6f 77 69 64 = db->lastRowid
1e246 3b 0a 20 20 20 20 20 20 75 2e 62 65 2e 63 6e 74 ;. u.be.cnt
1e247 20 3d 20 30 3b 0a 20 20 20 20 20 20 64 6f 7b 0a = 0;. do{.
1e248 20 20 20 20 20 20 20 20 69 66 28 20 75 2e 62 65 if( u.be
1e249 2e 63 6e 74 3d 3d 30 20 26 26 20 28 75 2e 62 65 .cnt==0 && (u.be
1e24a 2e 76 26 30 78 66 66 66 66 66 66 29 3d 3d 75 2e .v&0xffffff)==u.
1e24b 62 65 2e 76 20 29 7b 0a 20 20 20 20 20 20 20 20 be.v ){.
1e24c 20 20 75 2e 62 65 2e 76 2b 2b 3b 0a 20 20 20 20 u.be.v++;.
1e24d 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1e24e 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 61 6e sqlite3_ran
1e24f 64 6f 6d 6e 65 73 73 28 73 69 7a 65 6f 66 28 75 domness(sizeof(u
1e250 2e 62 65 2e 76 29 2c 20 26 75 2e 62 65 2e 76 29 .be.v), &u.be.v)
1e251 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 ;. if(
1e252 75 2e 62 65 2e 63 6e 74 3c 35 20 29 20 75 2e 62 u.be.cnt<5 ) u.b
1e253 65 2e 76 20 26 3d 20 30 78 66 66 66 66 66 66 3b e.v &= 0xffffff;
1e254 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
1e255 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 rc = sqlite3B
1e256 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b treeMovetoUnpack
1e257 65 64 28 75 2e 62 65 2e 70 43 2d 3e 70 43 75 72 ed(u.be.pC->pCur
1e258 73 6f 72 2c 20 30 2c 20 28 75 36 34 29 75 2e 62 sor, 0, (u64)u.b
1e259 65 2e 76 2c 20 30 2c 20 26 75 2e 62 65 2e 72 65 e.v, 0, &u.be.re
1e25a 73 29 3b 0a 20 20 20 20 20 20 20 20 75 2e 62 65 s);. u.be
1e25b 2e 63 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 7d 77 .cnt++;. }w
1e25c 68 69 6c 65 28 20 75 2e 62 65 2e 63 6e 74 3c 31 hile( u.be.cnt<1
1e25d 30 30 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 00 && rc==SQLITE
1e25e 5f 4f 4b 20 26 26 20 75 2e 62 65 2e 72 65 73 3d _OK && u.be.res=
1e25f 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 =0 );. if(
1e260 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
1e261 20 75 2e 62 65 2e 72 65 73 3d 3d 30 20 29 7b 0a u.be.res==0 ){.
1e262 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c rc = SQL
1e263 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 20 20 20 20 ITE_FULL;.
1e264 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 goto abort_due
1e265 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 _to_error;.
1e266 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 75 2e 62 }. }. u.b
1e267 65 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c e.pC->rowidIsVal
1e268 69 64 20 3d 20 30 3b 0a 20 20 20 20 75 2e 62 65 id = 0;. u.be
1e269 2e 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 .pC->deferredMov
1e26a 65 74 6f 20 3d 20 30 3b 0a 20 20 20 20 75 2e 62 eto = 0;. u.b
1e26b 65 2e 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75 e.pC->cacheStatu
1e26c 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b s = CACHE_STALE;
1e26d 0a 20 20 7d 0a 20 20 4d 65 6d 53 65 74 54 79 70 . }. MemSetTyp
1e26e 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f eFlag(pOut, MEM_
1e26f 49 6e 74 29 3b 0a 20 20 70 4f 75 74 2d 3e 75 2e Int);. pOut->u.
1e270 69 20 3d 20 75 2e 62 65 2e 76 3b 0a 20 20 62 72 i = u.be.v;. br
1e271 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1e272 65 3a 20 49 6e 73 65 72 74 20 50 31 20 50 32 20 e: Insert P1 P2
1e273 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 57 P3 P4 P5.**.** W
1e274 72 69 74 65 20 61 6e 20 65 6e 74 72 79 20 69 6e rite an entry in
1e275 74 6f 20 74 68 65 20 74 61 62 6c 65 20 6f 66 20 to the table of
1e276 63 75 72 73 6f 72 20 50 31 2e 20 20 41 20 6e 65 cursor P1. A ne
1e277 77 20 65 6e 74 72 79 20 69 73 0a 2a 2a 20 63 72 w entry is.** cr
1e278 65 61 74 65 64 20 69 66 20 69 74 20 64 6f 65 73 eated if it does
1e279 6e 27 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 n't already exis
1e27a 74 20 6f 72 20 74 68 65 20 64 61 74 61 20 66 6f t or the data fo
1e27b 72 20 61 6e 20 65 78 69 73 74 69 6e 67 0a 2a 2a r an existing.**
1e27c 20 65 6e 74 72 79 20 69 73 20 6f 76 65 72 77 72 entry is overwr
1e27d 69 74 74 65 6e 2e 20 20 54 68 65 20 64 61 74 61 itten. The data
1e27e 20 69 73 20 74 68 65 20 76 61 6c 75 65 20 4d 45 is the value ME
1e27f 4d 5f 42 6c 6f 62 20 73 74 6f 72 65 64 20 69 6e M_Blob stored in
1e280 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 6e 75 6d register.** num
1e281 62 65 72 20 50 32 2e 20 54 68 65 20 6b 65 79 20 ber P2. The key
1e282 69 73 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 is stored in reg
1e283 69 73 74 65 72 20 50 33 2e 20 54 68 65 20 6b 65 ister P3. The ke
1e284 79 20 6d 75 73 74 0a 2a 2a 20 62 65 20 61 20 4d y must.** be a M
1e285 45 4d 5f 49 6e 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 EM_Int..**.** If
1e286 20 74 68 65 20 4f 50 46 4c 41 47 5f 4e 43 48 41 the OPFLAG_NCHA
1e287 4e 47 45 20 66 6c 61 67 20 6f 66 20 50 35 20 69 NGE flag of P5 i
1e288 73 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20 s set, then the
1e289 72 6f 77 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 row change count
1e28a 20 69 73 0a 2a 2a 20 69 6e 63 72 65 6d 65 6e 74 is.** increment
1e28b 65 64 20 28 6f 74 68 65 72 77 69 73 65 20 6e 6f ed (otherwise no
1e28c 74 29 2e 20 20 49 66 20 74 68 65 20 4f 50 46 4c t). If the OPFL
1e28d 41 47 5f 4c 41 53 54 52 4f 57 49 44 20 66 6c 61 AG_LASTROWID fla
1e28e 67 20 6f 66 20 50 35 20 69 73 20 73 65 74 2c 0a g of P5 is set,.
1e28f 2a 2a 20 74 68 65 6e 20 72 6f 77 69 64 20 69 73 ** then rowid is
1e290 20 73 74 6f 72 65 64 20 66 6f 72 20 73 75 62 73 stored for subs
1e291 65 71 75 65 6e 74 20 72 65 74 75 72 6e 20 62 79 equent return by
1e292 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f the.** sqlite3_
1e293 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 last_insert_rowi
1e294 64 28 29 20 66 75 6e 63 74 69 6f 6e 20 28 6f 74 d() function (ot
1e295 68 65 72 77 69 73 65 20 69 74 20 69 73 20 75 6e herwise it is un
1e296 6d 6f 64 69 66 69 65 64 29 2e 0a 2a 2a 0a 2a 2a modified)..**.**
1e297 20 49 66 20 74 68 65 20 4f 50 46 4c 41 47 5f 55 If the OPFLAG_U
1e298 53 45 53 45 45 4b 52 45 53 55 4c 54 20 66 6c 61 SESEEKRESULT fla
1e299 67 20 6f 66 20 50 35 20 69 73 20 73 65 74 20 61 g of P5 is set a
1e29a 6e 64 20 69 66 20 74 68 65 20 72 65 73 75 6c 74 nd if the result
1e29b 20 6f 66 0a 2a 2a 20 74 68 65 20 6c 61 73 74 20 of.** the last
1e29c 73 65 65 6b 20 6f 70 65 72 61 74 69 6f 6e 20 28 seek operation (
1e29d 4f 50 5f 4e 6f 74 45 78 69 73 74 73 29 20 77 61 OP_NotExists) wa
1e29e 73 20 61 20 73 75 63 63 65 73 73 2c 20 74 68 65 s a success, the
1e29f 6e 20 74 68 69 73 0a 2a 2a 20 6f 70 65 72 61 74 n this.** operat
1e2a0 69 6f 6e 20 77 69 6c 6c 20 6e 6f 74 20 61 74 74 ion will not att
1e2a1 65 6d 70 74 20 74 6f 20 66 69 6e 64 20 74 68 65 empt to find the
1e2a2 20 61 70 70 72 6f 70 72 69 61 74 65 20 72 6f 77 appropriate row
1e2a3 20 62 65 66 6f 72 65 20 64 6f 69 6e 67 0a 2a 2a before doing.**
1e2a4 20 74 68 65 20 69 6e 73 65 72 74 20 62 75 74 20 the insert but
1e2a5 77 69 6c 6c 20 69 6e 73 74 65 61 64 20 6f 76 65 will instead ove
1e2a6 72 77 72 69 74 65 20 74 68 65 20 72 6f 77 20 74 rwrite the row t
1e2a7 68 61 74 20 74 68 65 20 63 75 72 73 6f 72 20 69 hat the cursor i
1e2a8 73 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 70 s.** currently p
1e2a9 6f 69 6e 74 69 6e 67 20 74 6f 2e 20 20 50 72 65 ointing to. Pre
1e2aa 73 75 6d 61 62 6c 79 2c 20 74 68 65 20 70 72 69 sumably, the pri
1e2ab 6f 72 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 20 or OP_NotExists
1e2ac 6f 70 63 6f 64 65 0a 2a 2a 20 68 61 73 20 61 6c opcode.** has al
1e2ad 72 65 61 64 79 20 70 6f 73 69 74 69 6f 6e 65 64 ready positioned
1e2ae 20 74 68 65 20 63 75 72 73 6f 72 20 63 6f 72 72 the cursor corr
1e2af 65 63 74 6c 79 2e 20 20 54 68 69 73 20 69 73 20 ectly. This is
1e2b0 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 0a an optimization.
1e2b1 2a 2a 20 74 68 61 74 20 62 6f 6f 73 74 73 20 70 ** that boosts p
1e2b2 65 72 66 6f 72 6d 61 6e 63 65 20 62 79 20 61 76 erformance by av
1e2b3 6f 69 64 69 6e 67 20 72 65 64 75 6e 64 61 6e 74 oiding redundant
1e2b4 20 73 65 65 6b 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 seeks..**.** If
1e2b5 20 74 68 65 20 4f 50 46 4c 41 47 5f 49 53 55 50 the OPFLAG_ISUP
1e2b6 44 41 54 45 20 66 6c 61 67 20 69 73 20 73 65 74 DATE flag is set
1e2b7 2c 20 74 68 65 6e 20 74 68 69 73 20 6f 70 63 6f , then this opco
1e2b8 64 65 20 69 73 20 70 61 72 74 20 6f 66 20 61 6e de is part of an
1e2b9 0a 2a 2a 20 55 50 44 41 54 45 20 6f 70 65 72 61 .** UPDATE opera
1e2ba 74 69 6f 6e 2e 20 20 4f 74 68 65 72 77 69 73 65 tion. Otherwise
1e2bb 20 28 69 66 20 74 68 65 20 66 6c 61 67 20 69 73 (if the flag is
1e2bc 20 63 6c 65 61 72 29 20 74 68 65 6e 20 74 68 69 clear) then thi
1e2bd 73 20 6f 70 63 6f 64 65 0a 2a 2a 20 69 73 20 70 s opcode.** is p
1e2be 61 72 74 20 6f 66 20 61 6e 20 49 4e 53 45 52 54 art of an INSERT
1e2bf 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 54 68 65 operation. The
1e2c0 20 64 69 66 66 65 72 65 6e 63 65 20 69 73 20 6f difference is o
1e2c1 6e 6c 79 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f nly important to
1e2c2 0a 2a 2a 20 74 68 65 20 75 70 64 61 74 65 20 68 .** the update h
1e2c3 6f 6f 6b 2e 0a 2a 2a 0a 2a 2a 20 50 61 72 61 6d ook..**.** Param
1e2c4 65 74 65 72 20 50 34 20 6d 61 79 20 70 6f 69 6e eter P4 may poin
1e2c5 74 20 74 6f 20 61 20 73 74 72 69 6e 67 20 63 6f t to a string co
1e2c6 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 74 61 62 ntaining the tab
1e2c7 6c 65 2d 6e 61 6d 65 2c 20 6f 72 0a 2a 2a 20 6d le-name, or.** m
1e2c8 61 79 20 62 65 20 4e 55 4c 4c 2e 20 49 66 20 69 ay be NULL. If i
1e2c9 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 t is not NULL, t
1e2ca 68 65 6e 20 74 68 65 20 75 70 64 61 74 65 2d 68 hen the update-h
1e2cb 6f 6f 6b 20 0a 2a 2a 20 28 73 71 6c 69 74 65 33 ook .** (sqlite3
1e2cc 2e 78 55 70 64 61 74 65 43 61 6c 6c 62 61 63 6b .xUpdateCallback
1e2cd 29 20 69 73 20 69 6e 76 6f 6b 65 64 20 66 6f 6c ) is invoked fol
1e2ce 6c 6f 77 69 6e 67 20 61 20 73 75 63 63 65 73 73 lowing a success
1e2cf 66 75 6c 20 69 6e 73 65 72 74 2e 0a 2a 2a 0a 2a ful insert..**.*
1e2d0 2a 20 28 57 41 52 4e 49 4e 47 2f 54 4f 44 4f 3a * (WARNING/TODO:
1e2d1 20 49 66 20 50 31 20 69 73 20 61 20 70 73 65 75 If P1 is a pseu
1e2d2 64 6f 2d 63 75 72 73 6f 72 20 61 6e 64 20 50 32 do-cursor and P2
1e2d3 20 69 73 20 64 79 6e 61 6d 69 63 61 6c 6c 79 0a is dynamically.
1e2d4 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 2c 20 74 68 ** allocated, th
1e2d5 65 6e 20 6f 77 6e 65 72 73 68 69 70 20 6f 66 20 en ownership of
1e2d6 50 32 20 69 73 20 74 72 61 6e 73 66 65 72 72 65 P2 is transferre
1e2d7 64 20 74 6f 20 74 68 65 20 70 73 65 75 64 6f 2d d to the pseudo-
1e2d8 63 75 72 73 6f 72 0a 2a 2a 20 61 6e 64 20 72 65 cursor.** and re
1e2d9 67 69 73 74 65 72 20 50 32 20 62 65 63 6f 6d 65 gister P2 become
1e2da 73 20 65 70 68 65 6d 65 72 61 6c 2e 20 20 49 66 s ephemeral. If
1e2db 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 63 the cursor is c
1e2dc 68 61 6e 67 65 64 2c 20 74 68 65 0a 2a 2a 20 76 hanged, the.** v
1e2dd 61 6c 75 65 20 6f 66 20 72 65 67 69 73 74 65 72 alue of register
1e2de 20 50 32 20 77 69 6c 6c 20 74 68 65 6e 20 63 68 P2 will then ch
1e2df 61 6e 67 65 2e 20 20 4d 61 6b 65 20 73 75 72 65 ange. Make sure
1e2e0 20 74 68 69 73 20 64 6f 65 73 20 6e 6f 74 0a 2a this does not.*
1e2e1 2a 20 63 61 75 73 65 20 61 6e 79 20 70 72 6f 62 * cause any prob
1e2e2 6c 65 6d 73 2e 29 0a 2a 2a 0a 2a 2a 20 54 68 69 lems.).**.** Thi
1e2e3 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 6f 6e s instruction on
1e2e4 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 74 61 62 6c ly works on tabl
1e2e5 65 73 2e 20 20 54 68 65 20 65 71 75 69 76 61 6c es. The equival
1e2e6 65 6e 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 0a ent instruction.
1e2e7 2a 2a 20 66 6f 72 20 69 6e 64 69 63 65 73 20 69 ** for indices i
1e2e8 73 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 2e 0a s OP_IdxInsert..
1e2e9 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 6e 73 65 72 */.case OP_Inser
1e2ea 74 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c t: {.#if 0 /* l
1e2eb 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1e2ec 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 66 20 2a oved into u.bf *
1e2ed 2f 0a 20 20 4d 65 6d 20 2a 70 44 61 74 61 3b 20 /. Mem *pData;
1e2ee 20 20 20 20 20 20 2f 2a 20 4d 45 4d 20 63 65 6c /* MEM cel
1e2ef 6c 20 68 6f 6c 64 69 6e 67 20 64 61 74 61 20 66 l holding data f
1e2f0 6f 72 20 74 68 65 20 72 65 63 6f 72 64 20 74 6f or the record to
1e2f1 20 62 65 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a be inserted */.
1e2f2 20 20 4d 65 6d 20 2a 70 4b 65 79 3b 20 20 20 20 Mem *pKey;
1e2f3 20 20 20 20 2f 2a 20 4d 45 4d 20 63 65 6c 6c 20 /* MEM cell
1e2f4 68 6f 6c 64 69 6e 67 20 6b 65 79 20 20 66 6f 72 holding key for
1e2f5 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 the record */.
1e2f6 20 69 36 34 20 69 4b 65 79 3b 20 20 20 20 20 20 i64 iKey;
1e2f7 20 20 20 2f 2a 20 54 68 65 20 69 6e 74 65 67 65 /* The intege
1e2f8 72 20 52 4f 57 49 44 20 6f 72 20 6b 65 79 20 66 r ROWID or key f
1e2f9 6f 72 20 74 68 65 20 72 65 63 6f 72 64 20 74 6f or the record to
1e2fa 20 62 65 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a be inserted */.
1e2fb 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 VdbeCursor *pC
1e2fc 3b 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 74 6f ; /* Cursor to
1e2fd 20 74 61 62 6c 65 20 69 6e 74 6f 20 77 68 69 63 table into whic
1e2fe 68 20 69 6e 73 65 72 74 20 69 73 20 77 72 69 74 h insert is writ
1e2ff 74 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 5a 65 ten */. int nZe
1e300 72 6f 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 ro; /* Nu
1e301 6d 62 65 72 20 6f 66 20 7a 65 72 6f 2d 62 79 74 mber of zero-byt
1e302 65 73 20 74 6f 20 61 70 70 65 6e 64 20 2a 2f 0a es to append */.
1e303 20 20 69 6e 74 20 73 65 65 6b 52 65 73 75 6c 74 int seekResult
1e304 3b 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66 ; /* Result of
1e305 20 70 72 69 6f 72 20 73 65 65 6b 20 6f 72 20 30 prior seek or 0
1e306 20 69 66 20 6e 6f 20 55 53 45 53 45 45 4b 52 45 if no USESEEKRE
1e307 53 55 4c 54 20 66 6c 61 67 20 2a 2f 0a 20 20 63 SULT flag */. c
1e308 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 3b 20 onst char *zDb;
1e309 20 2f 2a 20 64 61 74 61 62 61 73 65 20 6e 61 6d /* database nam
1e30a 65 20 2d 20 75 73 65 64 20 62 79 20 74 68 65 20 e - used by the
1e30b 75 70 64 61 74 65 20 68 6f 6f 6b 20 2a 2f 0a 20 update hook */.
1e30c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 62 const char *zTb
1e30d 6c 3b 20 2f 2a 20 54 61 62 6c 65 20 6e 61 6d 65 l; /* Table name
1e30e 20 2d 20 75 73 65 64 20 62 79 20 74 68 65 20 6f - used by the o
1e30f 70 64 61 74 65 20 68 6f 6f 6b 20 2a 2f 0a 20 20 pdate hook */.
1e310 69 6e 74 20 6f 70 3b 20 20 20 20 20 20 20 20 20 int op;
1e311 20 20 2f 2a 20 4f 70 63 6f 64 65 20 66 6f 72 20 /* Opcode for
1e312 75 70 64 61 74 65 20 68 6f 6f 6b 3a 20 53 51 4c update hook: SQL
1e313 49 54 45 5f 55 50 44 41 54 45 20 6f 72 20 53 51 ITE_UPDATE or SQ
1e314 4c 49 54 45 5f 49 4e 53 45 52 54 20 2a 2f 0a 23 LITE_INSERT */.#
1e315 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 endif /* local v
1e316 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 ariables moved i
1e317 6e 74 6f 20 75 2e 62 66 20 2a 2f 0a 0a 20 20 75 nto u.bf */.. u
1e318 2e 62 66 2e 70 44 61 74 61 20 3d 20 26 70 2d 3e .bf.pData = &p->
1e319 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 aMem[pOp->p2];.
1e31a 20 75 2e 62 66 2e 70 4b 65 79 20 3d 20 26 70 2d u.bf.pKey = &p-
1e31b 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a >aMem[pOp->p3];.
1e31c 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
1e31d 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 1>=0 && pOp->p1<
1e31e 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 p->nCursor );.
1e31f 75 2e 62 66 2e 70 43 20 3d 20 70 2d 3e 61 70 43 u.bf.pC = p->apC
1e320 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 sr[pOp->p1];. a
1e321 73 73 65 72 74 28 20 75 2e 62 66 2e 70 43 21 3d ssert( u.bf.pC!=
1e322 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75 0 );. assert( u
1e323 2e 62 66 2e 70 43 2d 3e 70 43 75 72 73 6f 72 21 .bf.pC->pCursor!
1e324 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
1e325 75 2e 62 66 2e 70 43 2d 3e 70 73 65 75 64 6f 54 u.bf.pC->pseudoT
1e326 61 62 6c 65 52 65 67 3d 3d 30 20 29 3b 0a 20 20 ableReg==0 );.
1e327 61 73 73 65 72 74 28 20 75 2e 62 66 2e 70 4b 65 assert( u.bf.pKe
1e328 79 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 y->flags & MEM_I
1e329 6e 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 nt );. assert(
1e32a 75 2e 62 66 2e 70 43 2d 3e 69 73 54 61 62 6c 65 u.bf.pC->isTable
1e32b 20 29 3b 0a 20 20 52 45 47 49 53 54 45 52 5f 54 );. REGISTER_T
1e32c 52 41 43 45 28 70 4f 70 2d 3e 70 32 2c 20 75 2e RACE(pOp->p2, u.
1e32d 62 66 2e 70 44 61 74 61 29 3b 0a 20 20 52 45 47 bf.pData);. REG
1e32e 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d ISTER_TRACE(pOp-
1e32f 3e 70 33 2c 20 75 2e 62 66 2e 70 4b 65 79 29 3b >p3, u.bf.pKey);
1e330 0a 0a 20 20 75 2e 62 66 2e 69 4b 65 79 20 3d 20 .. u.bf.iKey =
1e331 75 2e 62 66 2e 70 4b 65 79 2d 3e 75 2e 69 3b 0a u.bf.pKey->u.i;.
1e332 20 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 26 20 if( pOp->p5 &
1e333 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45 20 29 OPFLAG_NCHANGE )
1e334 20 70 2d 3e 6e 43 68 61 6e 67 65 2b 2b 3b 0a 20 p->nChange++;.
1e335 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 26 20 4f if( pOp->p5 & O
1e336 50 46 4c 41 47 5f 4c 41 53 54 52 4f 57 49 44 20 PFLAG_LASTROWID
1e337 29 20 64 62 2d 3e 6c 61 73 74 52 6f 77 69 64 20 ) db->lastRowid
1e338 3d 20 75 2e 62 66 2e 70 4b 65 79 2d 3e 75 2e 69 = u.bf.pKey->u.i
1e339 3b 0a 20 20 69 66 28 20 75 2e 62 66 2e 70 44 61 ;. if( u.bf.pDa
1e33a 74 61 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f ta->flags & MEM_
1e33b 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 75 2e 62 66 Null ){. u.bf
1e33c 2e 70 44 61 74 61 2d 3e 7a 20 3d 20 30 3b 0a 20 .pData->z = 0;.
1e33d 20 20 20 75 2e 62 66 2e 70 44 61 74 61 2d 3e 6e u.bf.pData->n
1e33e 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 = 0;. }else{.
1e33f 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 66 2e assert( u.bf.
1e340 70 44 61 74 61 2d 3e 66 6c 61 67 73 20 26 20 28 pData->flags & (
1e341 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 53 74 72 MEM_Blob|MEM_Str
1e342 29 20 29 3b 0a 20 20 7d 0a 20 20 75 2e 62 66 2e ) );. }. u.bf.
1e343 73 65 65 6b 52 65 73 75 6c 74 20 3d 20 28 28 70 seekResult = ((p
1e344 4f 70 2d 3e 70 35 20 26 20 4f 50 46 4c 41 47 5f Op->p5 & OPFLAG_
1e345 55 53 45 53 45 45 4b 52 45 53 55 4c 54 29 20 3f USESEEKRESULT) ?
1e346 20 75 2e 62 66 2e 70 43 2d 3e 73 65 65 6b 52 65 u.bf.pC->seekRe
1e347 73 75 6c 74 20 3a 20 30 29 3b 0a 20 20 69 66 28 sult : 0);. if(
1e348 20 75 2e 62 66 2e 70 44 61 74 61 2d 3e 66 6c 61 u.bf.pData->fla
1e349 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b gs & MEM_Zero ){
1e34a 0a 20 20 20 20 75 2e 62 66 2e 6e 5a 65 72 6f 20 . u.bf.nZero
1e34b 3d 20 75 2e 62 66 2e 70 44 61 74 61 2d 3e 75 2e = u.bf.pData->u.
1e34c 6e 5a 65 72 6f 3b 0a 20 20 7d 65 6c 73 65 7b 0a nZero;. }else{.
1e34d 20 20 20 20 75 2e 62 66 2e 6e 5a 65 72 6f 20 3d u.bf.nZero =
1e34e 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 0;. }. sqlite
1e34f 33 42 74 72 65 65 53 65 74 43 61 63 68 65 64 52 3BtreeSetCachedR
1e350 6f 77 69 64 28 75 2e 62 66 2e 70 43 2d 3e 70 43 owid(u.bf.pC->pC
1e351 75 72 73 6f 72 2c 20 30 29 3b 0a 20 20 72 63 20 ursor, 0);. rc
1e352 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e = sqlite3BtreeIn
1e353 73 65 72 74 28 75 2e 62 66 2e 70 43 2d 3e 70 43 sert(u.bf.pC->pC
1e354 75 72 73 6f 72 2c 20 30 2c 20 75 2e 62 66 2e 69 ursor, 0, u.bf.i
1e355 4b 65 79 2c 0a 20 20 20 20 20 20 20 20 20 20 20 Key,.
1e356 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 75 u
1e357 2e 62 66 2e 70 44 61 74 61 2d 3e 7a 2c 20 75 2e .bf.pData->z, u.
1e358 62 66 2e 70 44 61 74 61 2d 3e 6e 2c 20 75 2e 62 bf.pData->n, u.b
1e359 66 2e 6e 5a 65 72 6f 2c 0a 20 20 20 20 20 20 20 f.nZero,.
1e35a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e35b 20 20 20 70 4f 70 2d 3e 70 35 20 26 20 4f 50 46 pOp->p5 & OPF
1e35c 4c 41 47 5f 41 50 50 45 4e 44 2c 20 75 2e 62 66 LAG_APPEND, u.bf
1e35d 2e 73 65 65 6b 52 65 73 75 6c 74 0a 20 20 29 3b .seekResult. );
1e35e 0a 20 20 75 2e 62 66 2e 70 43 2d 3e 72 6f 77 69 . u.bf.pC->rowi
1e35f 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 dIsValid = 0;.
1e360 75 2e 62 66 2e 70 43 2d 3e 64 65 66 65 72 72 65 u.bf.pC->deferre
1e361 64 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a 20 20 75 dMoveto = 0;. u
1e362 2e 62 66 2e 70 43 2d 3e 63 61 63 68 65 53 74 61 .bf.pC->cacheSta
1e363 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c tus = CACHE_STAL
1e364 45 3b 0a 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 E;.. /* Invoke
1e365 74 68 65 20 75 70 64 61 74 65 2d 68 6f 6f 6b 20 the update-hook
1e366 69 66 20 72 65 71 75 69 72 65 64 2e 20 2a 2f 0a if required. */.
1e367 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
1e368 5f 4f 4b 20 26 26 20 64 62 2d 3e 78 55 70 64 61 _OK && db->xUpda
1e369 74 65 43 61 6c 6c 62 61 63 6b 20 26 26 20 70 4f teCallback && pO
1e36a 70 2d 3e 70 34 2e 7a 20 29 7b 0a 20 20 20 20 75 p->p4.z ){. u
1e36b 2e 62 66 2e 7a 44 62 20 3d 20 64 62 2d 3e 61 44 .bf.zDb = db->aD
1e36c 62 5b 75 2e 62 66 2e 70 43 2d 3e 69 44 62 5d 2e b[u.bf.pC->iDb].
1e36d 7a 4e 61 6d 65 3b 0a 20 20 20 20 75 2e 62 66 2e zName;. u.bf.
1e36e 7a 54 62 6c 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a zTbl = pOp->p4.z
1e36f 3b 0a 20 20 20 20 75 2e 62 66 2e 6f 70 20 3d 20 ;. u.bf.op =
1e370 28 28 70 4f 70 2d 3e 70 35 20 26 20 4f 50 46 4c ((pOp->p5 & OPFL
1e371 41 47 5f 49 53 55 50 44 41 54 45 29 20 3f 20 53 AG_ISUPDATE) ? S
1e372 51 4c 49 54 45 5f 55 50 44 41 54 45 20 3a 20 53 QLITE_UPDATE : S
1e373 51 4c 49 54 45 5f 49 4e 53 45 52 54 29 3b 0a 20 QLITE_INSERT);.
1e374 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 66 2e assert( u.bf.
1e375 70 43 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 pC->isTable );.
1e376 20 20 20 64 62 2d 3e 78 55 70 64 61 74 65 43 61 db->xUpdateCa
1e377 6c 6c 62 61 63 6b 28 64 62 2d 3e 70 55 70 64 61 llback(db->pUpda
1e378 74 65 41 72 67 2c 20 75 2e 62 66 2e 6f 70 2c 20 teArg, u.bf.op,
1e379 75 2e 62 66 2e 7a 44 62 2c 20 75 2e 62 66 2e 7a u.bf.zDb, u.bf.z
1e37a 54 62 6c 2c 20 75 2e 62 66 2e 69 4b 65 79 29 3b Tbl, u.bf.iKey);
1e37b 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 . assert( u.b
1e37c 66 2e 70 43 2d 3e 69 44 62 3e 3d 30 20 29 3b 0a f.pC->iDb>=0 );.
1e37d 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a }. break;.}..
1e37e 2f 2a 20 4f 70 63 6f 64 65 3a 20 44 65 6c 65 74 /* Opcode: Delet
1e37f 65 20 50 31 20 50 32 20 2a 20 50 34 20 2a 0a 2a e P1 P2 * P4 *.*
1e380 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 74 68 65 20 *.** Delete the
1e381 72 65 63 6f 72 64 20 61 74 20 77 68 69 63 68 20 record at which
1e382 74 68 65 20 50 31 20 63 75 72 73 6f 72 20 69 73 the P1 cursor is
1e383 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 currently point
1e384 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 ing..**.** The c
1e385 75 72 73 6f 72 20 77 69 6c 6c 20 62 65 20 6c 65 ursor will be le
1e386 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 65 ft pointing at e
1e387 69 74 68 65 72 20 74 68 65 20 6e 65 78 74 20 6f ither the next o
1e388 72 20 74 68 65 20 70 72 65 76 69 6f 75 73 0a 2a r the previous.*
1e389 2a 20 72 65 63 6f 72 64 20 69 6e 20 74 68 65 20 * record in the
1e38a 74 61 62 6c 65 2e 20 49 66 20 69 74 20 69 73 20 table. If it is
1e38b 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 left pointing at
1e38c 20 74 68 65 20 6e 65 78 74 20 72 65 63 6f 72 64 the next record
1e38d 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 6e 65 , then.** the ne
1e38e 78 74 20 4e 65 78 74 20 69 6e 73 74 72 75 63 74 xt Next instruct
1e38f 69 6f 6e 20 77 69 6c 6c 20 62 65 20 61 20 6e 6f ion will be a no
1e390 2d 6f 70 2e 20 20 48 65 6e 63 65 20 69 74 20 69 -op. Hence it i
1e391 73 20 4f 4b 20 74 6f 20 64 65 6c 65 74 65 0a 2a s OK to delete.*
1e392 2a 20 61 20 72 65 63 6f 72 64 20 66 72 6f 6d 20 * a record from
1e393 77 69 74 68 69 6e 20 61 6e 20 4e 65 78 74 20 6c within an Next l
1e394 6f 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 oop..**.** If th
1e395 65 20 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45 e OPFLAG_NCHANGE
1e396 20 66 6c 61 67 20 6f 66 20 50 32 20 69 73 20 73 flag of P2 is s
1e397 65 74 2c 20 74 68 65 6e 20 74 68 65 20 72 6f 77 et, then the row
1e398 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 20 69 73 change count is
1e399 0a 2a 2a 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 .** incremented
1e39a 28 6f 74 68 65 72 77 69 73 65 20 6e 6f 74 29 2e (otherwise not).
1e39b 0a 2a 2a 0a 2a 2a 20 50 31 20 6d 75 73 74 20 6e .**.** P1 must n
1e39c 6f 74 20 62 65 20 70 73 65 75 64 6f 2d 74 61 62 ot be pseudo-tab
1e39d 6c 65 2e 20 20 49 74 20 68 61 73 20 74 6f 20 62 le. It has to b
1e39e 65 20 61 20 72 65 61 6c 20 74 61 62 6c 65 20 77 e a real table w
1e39f 69 74 68 0a 2a 2a 20 6d 75 6c 74 69 70 6c 65 20 ith.** multiple
1e3a0 72 6f 77 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 rows..**.** If P
1e3a1 34 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 4 is not NULL, t
1e3a2 68 65 6e 20 69 74 20 69 73 20 74 68 65 20 6e 61 hen it is the na
1e3a3 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 me of the table
1e3a4 74 68 61 74 20 50 31 20 69 73 0a 2a 2a 20 70 6f that P1 is.** po
1e3a5 69 6e 74 69 6e 67 20 74 6f 2e 20 20 54 68 65 20 inting to. The
1e3a6 75 70 64 61 74 65 20 68 6f 6f 6b 20 77 69 6c 6c update hook will
1e3a7 20 62 65 20 69 6e 76 6f 6b 65 64 2c 20 69 66 20 be invoked, if
1e3a8 69 74 20 65 78 69 73 74 73 2e 0a 2a 2a 20 49 66 it exists..** If
1e3a9 20 50 34 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 P4 is not NULL
1e3aa 74 68 65 6e 20 74 68 65 20 50 31 20 63 75 72 73 then the P1 curs
1e3ab 6f 72 20 6d 75 73 74 20 68 61 76 65 20 62 65 65 or must have bee
1e3ac 6e 20 70 6f 73 69 74 69 6f 6e 65 64 0a 2a 2a 20 n positioned.**
1e3ad 75 73 69 6e 67 20 4f 50 5f 4e 6f 74 46 6f 75 6e using OP_NotFoun
1e3ae 64 20 70 72 69 6f 72 20 74 6f 20 69 6e 76 6f 6b d prior to invok
1e3af 69 6e 67 20 74 68 69 73 20 6f 70 63 6f 64 65 2e ing this opcode.
1e3b0 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 44 65 6c 65 .*/.case OP_Dele
1e3b1 74 65 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 te: {.#if 0 /*
1e3b2 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 local variables
1e3b3 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 67 20 moved into u.bg
1e3b4 2a 2f 0a 20 20 69 36 34 20 69 4b 65 79 3b 0a 20 */. i64 iKey;.
1e3b5 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b VdbeCursor *pC;
1e3b6 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c .#endif /* local
1e3b7 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1e3b8 20 69 6e 74 6f 20 75 2e 62 67 20 2a 2f 0a 0a 20 into u.bg */..
1e3b9 20 75 2e 62 67 2e 69 4b 65 79 20 3d 20 30 3b 0a u.bg.iKey = 0;.
1e3ba 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
1e3bb 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 1>=0 && pOp->p1<
1e3bc 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 p->nCursor );.
1e3bd 75 2e 62 67 2e 70 43 20 3d 20 70 2d 3e 61 70 43 u.bg.pC = p->apC
1e3be 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 sr[pOp->p1];. a
1e3bf 73 73 65 72 74 28 20 75 2e 62 67 2e 70 43 21 3d ssert( u.bg.pC!=
1e3c0 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75 0 );. assert( u
1e3c1 2e 62 67 2e 70 43 2d 3e 70 43 75 72 73 6f 72 21 .bg.pC->pCursor!
1e3c2 3d 30 20 29 3b 20 20 2f 2a 20 4f 6e 6c 79 20 76 =0 ); /* Only v
1e3c3 61 6c 69 64 20 66 6f 72 20 72 65 61 6c 20 74 61 alid for real ta
1e3c4 62 6c 65 73 2c 20 6e 6f 20 70 73 65 75 64 6f 74 bles, no pseudot
1e3c5 61 62 6c 65 73 20 2a 2f 0a 0a 20 20 2f 2a 20 49 ables */.. /* I
1e3c6 66 20 74 68 65 20 75 70 64 61 74 65 2d 68 6f 6f f the update-hoo
1e3c7 6b 20 77 69 6c 6c 20 62 65 20 69 6e 76 6f 6b 65 k will be invoke
1e3c8 64 2c 20 73 65 74 20 75 2e 62 67 2e 69 4b 65 79 d, set u.bg.iKey
1e3c9 20 74 6f 20 74 68 65 20 72 6f 77 69 64 20 6f 66 to the rowid of
1e3ca 20 74 68 65 0a 20 20 2a 2a 20 72 6f 77 20 62 65 the. ** row be
1e3cb 69 6e 67 20 64 65 6c 65 74 65 64 2e 0a 20 20 2a ing deleted.. *
1e3cc 2f 0a 20 20 69 66 28 20 64 62 2d 3e 78 55 70 64 /. if( db->xUpd
1e3cd 61 74 65 43 61 6c 6c 62 61 63 6b 20 26 26 20 70 ateCallback && p
1e3ce 4f 70 2d 3e 70 34 2e 7a 20 29 7b 0a 20 20 20 20 Op->p4.z ){.
1e3cf 61 73 73 65 72 74 28 20 75 2e 62 67 2e 70 43 2d assert( u.bg.pC-
1e3d0 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 >isTable );.
1e3d1 61 73 73 65 72 74 28 20 75 2e 62 67 2e 70 43 2d assert( u.bg.pC-
1e3d2 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 29 3b >rowidIsValid );
1e3d3 20 20 2f 2a 20 6c 61 73 74 52 6f 77 69 64 20 73 /* lastRowid s
1e3d4 65 74 20 62 79 20 70 72 65 76 69 6f 75 73 20 4f et by previous O
1e3d5 50 5f 4e 6f 74 46 6f 75 6e 64 20 2a 2f 0a 20 20 P_NotFound */.
1e3d6 20 20 75 2e 62 67 2e 69 4b 65 79 20 3d 20 75 2e u.bg.iKey = u.
1e3d7 62 67 2e 70 43 2d 3e 6c 61 73 74 52 6f 77 69 64 bg.pC->lastRowid
1e3d8 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 ;. }.. /* The
1e3d9 4f 50 5f 44 65 6c 65 74 65 20 6f 70 63 6f 64 65 OP_Delete opcode
1e3da 20 61 6c 77 61 79 73 20 66 6f 6c 6c 6f 77 73 20 always follows
1e3db 61 6e 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 20 an OP_NotExists
1e3dc 6f 72 20 4f 50 5f 4c 61 73 74 20 6f 72 0a 20 20 or OP_Last or.
1e3dd 2a 2a 20 4f 50 5f 43 6f 6c 75 6d 6e 20 6f 6e 20 ** OP_Column on
1e3de 74 68 65 20 73 61 6d 65 20 74 61 62 6c 65 20 77 the same table w
1e3df 69 74 68 6f 75 74 20 61 6e 79 20 69 6e 74 65 72 ithout any inter
1e3e0 76 65 6e 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e vening operation
1e3e1 73 20 74 68 61 74 0a 20 20 2a 2a 20 6d 69 67 68 s that. ** migh
1e3e2 74 20 6d 6f 76 65 20 6f 72 20 69 6e 76 61 6c 69 t move or invali
1e3e3 64 61 74 65 20 74 68 65 20 63 75 72 73 6f 72 2e date the cursor.
1e3e4 20 20 48 65 6e 63 65 20 63 75 72 73 6f 72 20 75 Hence cursor u
1e3e5 2e 62 67 2e 70 43 20 69 73 20 61 6c 77 61 79 73 .bg.pC is always
1e3e6 20 70 6f 69 6e 74 69 6e 67 0a 20 20 2a 2a 20 74 pointing. ** t
1e3e7 6f 20 74 68 65 20 72 6f 77 20 74 6f 20 62 65 20 o the row to be
1e3e8 64 65 6c 65 74 65 64 20 61 6e 64 20 74 68 65 20 deleted and the
1e3e9 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 73 6f sqlite3VdbeCurso
1e3ea 72 4d 6f 76 65 74 6f 28 29 20 6f 70 65 72 61 74 rMoveto() operat
1e3eb 69 6f 6e 0a 20 20 2a 2a 20 62 65 6c 6f 77 20 69 ion. ** below i
1e3ec 73 20 61 6c 77 61 79 73 20 61 20 6e 6f 2d 6f 70 s always a no-op
1e3ed 20 61 6e 64 20 63 61 6e 6e 6f 74 20 66 61 69 6c and cannot fail
1e3ee 2e 20 20 57 65 20 77 69 6c 6c 20 72 75 6e 20 69 . We will run i
1e3ef 74 20 61 6e 79 68 6f 77 2c 20 74 68 6f 75 67 68 t anyhow, though
1e3f0 2c 0a 20 20 2a 2a 20 74 6f 20 67 75 61 72 64 20 ,. ** to guard
1e3f1 61 67 61 69 6e 73 74 20 66 75 74 75 72 65 20 63 against future c
1e3f2 68 61 6e 67 65 73 20 74 6f 20 74 68 65 20 63 6f hanges to the co
1e3f3 64 65 20 67 65 6e 65 72 61 74 6f 72 2e 0a 20 20 de generator..
1e3f4 2a 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 75 2e **/. assert( u.
1e3f5 62 67 2e 70 43 2d 3e 64 65 66 65 72 72 65 64 4d bg.pC->deferredM
1e3f6 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a 20 20 72 63 oveto==0 );. rc
1e3f7 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 = sqlite3VdbeCu
1e3f8 72 73 6f 72 4d 6f 76 65 74 6f 28 75 2e 62 67 2e rsorMoveto(u.bg.
1e3f9 70 43 29 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 pC);. if( NEVER
1e3fa 28 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 (rc!=SQLITE_OK)
1e3fb 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 ) goto abort_due
1e3fc 5f 74 6f 5f 65 72 72 6f 72 3b 0a 0a 20 20 73 71 _to_error;.. sq
1e3fd 6c 69 74 65 33 42 74 72 65 65 53 65 74 43 61 63 lite3BtreeSetCac
1e3fe 68 65 64 52 6f 77 69 64 28 75 2e 62 67 2e 70 43 hedRowid(u.bg.pC
1e3ff 2d 3e 70 43 75 72 73 6f 72 2c 20 30 29 3b 0a 20 ->pCursor, 0);.
1e400 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
1e401 65 65 44 65 6c 65 74 65 28 75 2e 62 67 2e 70 43 eeDelete(u.bg.pC
1e402 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 75 2e ->pCursor);. u.
1e403 62 67 2e 70 43 2d 3e 63 61 63 68 65 53 74 61 74 bg.pC->cacheStat
1e404 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 us = CACHE_STALE
1e405 3b 0a 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 ;.. /* Invoke t
1e406 68 65 20 75 70 64 61 74 65 2d 68 6f 6f 6b 20 69 he update-hook i
1e407 66 20 72 65 71 75 69 72 65 64 2e 20 2a 2f 0a 20 f required. */.
1e408 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
1e409 4f 4b 20 26 26 20 64 62 2d 3e 78 55 70 64 61 74 OK && db->xUpdat
1e40a 65 43 61 6c 6c 62 61 63 6b 20 26 26 20 70 4f 70 eCallback && pOp
1e40b 2d 3e 70 34 2e 7a 20 29 7b 0a 20 20 20 20 63 6f ->p4.z ){. co
1e40c 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 20 3d 20 nst char *zDb =
1e40d 64 62 2d 3e 61 44 62 5b 75 2e 62 67 2e 70 43 2d db->aDb[u.bg.pC-
1e40e 3e 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 >iDb].zName;.
1e40f 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 62 const char *zTb
1e410 6c 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 l = pOp->p4.z;.
1e411 20 20 20 64 62 2d 3e 78 55 70 64 61 74 65 43 61 db->xUpdateCa
1e412 6c 6c 62 61 63 6b 28 64 62 2d 3e 70 55 70 64 61 llback(db->pUpda
1e413 74 65 41 72 67 2c 20 53 51 4c 49 54 45 5f 44 45 teArg, SQLITE_DE
1e414 4c 45 54 45 2c 20 7a 44 62 2c 20 7a 54 62 6c 2c LETE, zDb, zTbl,
1e415 20 75 2e 62 67 2e 69 4b 65 79 29 3b 0a 20 20 20 u.bg.iKey);.
1e416 20 61 73 73 65 72 74 28 20 75 2e 62 67 2e 70 43 assert( u.bg.pC
1e417 2d 3e 69 44 62 3e 3d 30 20 29 3b 0a 20 20 7d 0a ->iDb>=0 );. }.
1e418 20 20 69 66 28 20 70 4f 70 2d 3e 70 32 20 26 20 if( pOp->p2 &
1e419 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45 20 29 OPFLAG_NCHANGE )
1e41a 20 70 2d 3e 6e 43 68 61 6e 67 65 2b 2b 3b 0a 20 p->nChange++;.
1e41b 20 62 72 65 61 6b 3b 0a 7d 0a 2f 2a 20 4f 70 63 break;.}./* Opc
1e41c 6f 64 65 3a 20 52 65 73 65 74 43 6f 75 6e 74 20 ode: ResetCount
1e41d 2a 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 * * * * *.**.**
1e41e 54 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 The value of the
1e41f 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 20 change counter
1e420 69 73 20 63 6f 70 69 65 64 20 74 6f 20 74 68 65 is copied to the
1e421 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 database handle
1e422 0a 2a 2a 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 .** change count
1e423 65 72 20 28 72 65 74 75 72 6e 65 64 20 62 79 20 er (returned by
1e424 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 subsequent calls
1e425 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 68 61 6e to sqlite3_chan
1e426 67 65 73 28 29 29 2e 0a 2a 2a 20 54 68 65 6e 20 ges())..** Then
1e427 74 68 65 20 56 4d 73 20 69 6e 74 65 72 6e 61 6c the VMs internal
1e428 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 20 change counter
1e429 72 65 73 65 74 73 20 74 6f 20 30 2e 0a 2a 2a 20 resets to 0..**
1e42a 54 68 69 73 20 69 73 20 75 73 65 64 20 62 79 20 This is used by
1e42b 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 73 trigger programs
1e42c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 65 73 ..*/.case OP_Res
1e42d 65 74 43 6f 75 6e 74 3a 20 7b 0a 20 20 73 71 6c etCount: {. sql
1e42e 69 74 65 33 56 64 62 65 53 65 74 43 68 61 6e 67 ite3VdbeSetChang
1e42f 65 73 28 64 62 2c 20 70 2d 3e 6e 43 68 61 6e 67 es(db, p->nChang
1e430 65 29 3b 0a 20 20 70 2d 3e 6e 43 68 61 6e 67 65 e);. p->nChange
1e431 20 3d 20 30 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d = 0;. break;.}
1e432 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 6f 77 ../* Opcode: Row
1e433 44 61 74 61 20 50 31 20 50 32 20 2a 20 2a 20 2a Data P1 P2 * * *
1e434 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 69 6e 74 .**.** Write int
1e435 6f 20 72 65 67 69 73 74 65 72 20 50 32 20 74 68 o register P2 th
1e436 65 20 63 6f 6d 70 6c 65 74 65 20 72 6f 77 20 64 e complete row d
1e437 61 74 61 20 66 6f 72 20 63 75 72 73 6f 72 20 50 ata for cursor P
1e438 31 2e 0a 2a 2a 20 54 68 65 72 65 20 69 73 20 6e 1..** There is n
1e439 6f 20 69 6e 74 65 72 70 72 65 74 61 74 69 6f 6e o interpretation
1e43a 20 6f 66 20 74 68 65 20 64 61 74 61 2e 20 20 0a of the data. .
1e43b 2a 2a 20 49 74 20 69 73 20 6a 75 73 74 20 63 6f ** It is just co
1e43c 70 69 65 64 20 6f 6e 74 6f 20 74 68 65 20 50 32 pied onto the P2
1e43d 20 72 65 67 69 73 74 65 72 20 65 78 61 63 74 6c register exactl
1e43e 79 20 61 73 20 0a 2a 2a 20 69 74 20 69 73 20 66 y as .** it is f
1e43f 6f 75 6e 64 20 69 6e 20 74 68 65 20 64 61 74 61 ound in the data
1e440 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a base file..**.**
1e441 20 49 66 20 74 68 65 20 50 31 20 63 75 72 73 6f If the P1 curso
1e442 72 20 6d 75 73 74 20 62 65 20 70 6f 69 6e 74 69 r must be pointi
1e443 6e 67 20 74 6f 20 61 20 76 61 6c 69 64 20 72 6f ng to a valid ro
1e444 77 20 28 6e 6f 74 20 61 20 4e 55 4c 4c 20 72 6f w (not a NULL ro
1e445 77 29 0a 2a 2a 20 6f 66 20 61 20 72 65 61 6c 20 w).** of a real
1e446 74 61 62 6c 65 2c 20 6e 6f 74 20 61 20 70 73 65 table, not a pse
1e447 75 64 6f 2d 74 61 62 6c 65 2e 0a 2a 2f 0a 2f 2a udo-table..*/./*
1e448 20 4f 70 63 6f 64 65 3a 20 52 6f 77 4b 65 79 20 Opcode: RowKey
1e449 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a P1 P2 * * *.**.*
1e44a 2a 20 57 72 69 74 65 20 69 6e 74 6f 20 72 65 67 * Write into reg
1e44b 69 73 74 65 72 20 50 32 20 74 68 65 20 63 6f 6d ister P2 the com
1e44c 70 6c 65 74 65 20 72 6f 77 20 6b 65 79 20 66 6f plete row key fo
1e44d 72 20 63 75 72 73 6f 72 20 50 31 2e 0a 2a 2a 20 r cursor P1..**
1e44e 54 68 65 72 65 20 69 73 20 6e 6f 20 69 6e 74 65 There is no inte
1e44f 72 70 72 65 74 61 74 69 6f 6e 20 6f 66 20 74 68 rpretation of th
1e450 65 20 64 61 74 61 2e 20 20 0a 2a 2a 20 54 68 65 e data. .** The
1e451 20 6b 65 79 20 69 73 20 63 6f 70 69 65 64 20 6f key is copied o
1e452 6e 74 6f 20 74 68 65 20 50 33 20 72 65 67 69 73 nto the P3 regis
1e453 74 65 72 20 65 78 61 63 74 6c 79 20 61 73 20 0a ter exactly as .
1e454 2a 2a 20 69 74 20 69 73 20 66 6f 75 6e 64 20 69 ** it is found i
1e455 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 n the database f
1e456 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 ile..**.** If th
1e457 65 20 50 31 20 63 75 72 73 6f 72 20 6d 75 73 74 e P1 cursor must
1e458 20 62 65 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 be pointing to
1e459 61 20 76 61 6c 69 64 20 72 6f 77 20 28 6e 6f 74 a valid row (not
1e45a 20 61 20 4e 55 4c 4c 20 72 6f 77 29 0a 2a 2a 20 a NULL row).**
1e45b 6f 66 20 61 20 72 65 61 6c 20 74 61 62 6c 65 2c of a real table,
1e45c 20 6e 6f 74 20 61 20 70 73 65 75 64 6f 2d 74 61 not a pseudo-ta
1e45d 62 6c 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f ble..*/.case OP_
1e45e 52 6f 77 4b 65 79 3a 0a 63 61 73 65 20 4f 50 5f RowKey:.case OP_
1e45f 52 6f 77 44 61 74 61 3a 20 7b 0a 23 69 66 20 30 RowData: {.#if 0
1e460 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 /* local varia
1e461 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 bles moved into
1e462 75 2e 62 68 20 2a 2f 0a 20 20 56 64 62 65 43 75 u.bh */. VdbeCu
1e463 72 73 6f 72 20 2a 70 43 3b 0a 20 20 42 74 43 75 rsor *pC;. BtCu
1e464 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 75 rsor *pCrsr;. u
1e465 33 32 20 6e 3b 0a 20 20 69 36 34 20 6e 36 34 3b 32 n;. i64 n64;
1e466 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c .#endif /* local
1e467 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1e468 20 69 6e 74 6f 20 75 2e 62 68 20 2a 2f 0a 0a 20 into u.bh */..
1e469 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d pOut = &p->aMem
1e46a 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 0a 20 20 2f 2a [pOp->p2];.. /*
1e46b 20 4e 6f 74 65 20 74 68 61 74 20 52 6f 77 4b 65 Note that RowKe
1e46c 79 20 61 6e 64 20 52 6f 77 44 61 74 61 20 61 72 y and RowData ar
1e46d 65 20 72 65 61 6c 6c 79 20 65 78 61 63 74 6c 79 e really exactly
1e46e 20 74 68 65 20 73 61 6d 65 20 69 6e 73 74 72 75 the same instru
1e46f 63 74 69 6f 6e 20 2a 2f 0a 20 20 61 73 73 65 72 ction */. asser
1e470 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 t( pOp->p1>=0 &&
1e471 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 pOp->p1<p->nCur
1e472 73 6f 72 20 29 3b 0a 20 20 75 2e 62 68 2e 70 43 sor );. u.bh.pC
1e473 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d = p->apCsr[pOp-
1e474 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 >p1];. assert(
1e475 75 2e 62 68 2e 70 43 2d 3e 69 73 54 61 62 6c 65 u.bh.pC->isTable
1e476 20 7c 7c 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d || pOp->opcode=
1e477 3d 4f 50 5f 52 6f 77 4b 65 79 20 29 3b 0a 20 20 =OP_RowKey );.
1e478 61 73 73 65 72 74 28 20 75 2e 62 68 2e 70 43 2d assert( u.bh.pC-
1e479 3e 69 73 49 6e 64 65 78 20 7c 7c 20 70 4f 70 2d >isIndex || pOp-
1e47a 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 52 6f 77 44 >opcode==OP_RowD
1e47b 61 74 61 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ata );. assert(
1e47c 20 75 2e 62 68 2e 70 43 21 3d 30 20 29 3b 0a 20 u.bh.pC!=0 );.
1e47d 20 61 73 73 65 72 74 28 20 75 2e 62 68 2e 70 43 assert( u.bh.pC
1e47e 2d 3e 6e 75 6c 6c 52 6f 77 3d 3d 30 20 29 3b 0a ->nullRow==0 );.
1e47f 20 20 61 73 73 65 72 74 28 20 75 2e 62 68 2e 70 assert( u.bh.p
1e480 43 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65 52 65 C->pseudoTableRe
1e481 67 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 g==0 );. assert
1e482 28 20 75 2e 62 68 2e 70 43 2d 3e 70 43 75 72 73 ( u.bh.pC->pCurs
1e483 6f 72 21 3d 30 20 29 3b 0a 20 20 75 2e 62 68 2e or!=0 );. u.bh.
1e484 70 43 72 73 72 20 3d 20 75 2e 62 68 2e 70 43 2d pCrsr = u.bh.pC-
1e485 3e 70 43 75 72 73 6f 72 3b 0a 20 20 61 73 73 65 >pCursor;. asse
1e486 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 rt( sqlite3Btree
1e487 43 75 72 73 6f 72 49 73 56 61 6c 69 64 28 75 2e CursorIsValid(u.
1e488 62 68 2e 70 43 72 73 72 29 20 29 3b 0a 0a 20 20 bh.pCrsr) );..
1e489 2f 2a 20 54 68 65 20 4f 50 5f 52 6f 77 4b 65 79 /* The OP_RowKey
1e48a 20 61 6e 64 20 4f 50 5f 52 6f 77 44 61 74 61 20 and OP_RowData
1e48b 6f 70 63 6f 64 65 73 20 61 6c 77 61 79 73 20 66 opcodes always f
1e48c 6f 6c 6c 6f 77 20 4f 50 5f 4e 6f 74 45 78 69 73 ollow OP_NotExis
1e48d 74 73 20 6f 72 0a 20 20 2a 2a 20 4f 50 5f 52 65 ts or. ** OP_Re
1e48e 77 69 6e 64 2f 4f 70 5f 4e 65 78 74 20 77 69 74 wind/Op_Next wit
1e48f 68 20 6e 6f 20 69 6e 74 65 72 76 65 6e 69 6e 67 h no intervening
1e490 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 74 68 instructions th
1e491 61 74 20 6d 69 67 68 74 20 69 6e 76 61 6c 69 64 at might invalid
1e492 61 74 65 0a 20 20 2a 2a 20 74 68 65 20 63 75 72 ate. ** the cur
1e493 73 6f 72 2e 20 20 48 65 6e 63 65 20 74 68 65 20 sor. Hence the
1e494 66 6f 6c 6c 6f 77 69 6e 67 20 73 71 6c 69 74 65 following sqlite
1e495 33 56 64 62 65 43 75 72 73 6f 72 4d 6f 76 65 74 3VdbeCursorMovet
1e496 6f 28 29 20 63 61 6c 6c 20 69 73 20 61 6c 77 61 o() call is alwa
1e497 79 73 0a 20 20 2a 2a 20 61 20 6e 6f 2d 6f 70 20 ys. ** a no-op
1e498 61 6e 64 20 63 61 6e 20 6e 65 76 65 72 20 66 61 and can never fa
1e499 69 6c 2e 20 20 42 75 74 20 77 65 20 6c 65 61 76 il. But we leav
1e49a 65 20 69 74 20 69 6e 20 70 6c 61 63 65 20 61 73 e it in place as
1e49b 20 61 20 73 61 66 65 74 79 2e 0a 20 20 2a 2f 0a a safety.. */.
1e49c 20 20 61 73 73 65 72 74 28 20 75 2e 62 68 2e 70 assert( u.bh.p
1e49d 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 C->deferredMovet
1e49e 6f 3d 3d 30 20 29 3b 0a 20 20 72 63 20 3d 20 73 o==0 );. rc = s
1e49f 71 6c 69 74 65 33 56 64 62 65 43 75 72 73 6f 72 qlite3VdbeCursor
1e4a0 4d 6f 76 65 74 6f 28 75 2e 62 68 2e 70 43 29 3b Moveto(u.bh.pC);
1e4a1 0a 20 20 69 66 28 20 4e 45 56 45 52 28 72 63 21 . if( NEVER(rc!
1e4a2 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 29 20 67 6f =SQLITE_OK) ) go
1e4a3 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f to abort_due_to_
1e4a4 65 72 72 6f 72 3b 0a 0a 20 20 69 66 28 20 75 2e error;.. if( u.
1e4a5 62 68 2e 70 43 2d 3e 69 73 49 6e 64 65 78 20 29 bh.pC->isIndex )
1e4a6 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 75 {. assert( !u
1e4a7 2e 62 68 2e 70 43 2d 3e 69 73 54 61 62 6c 65 20 .bh.pC->isTable
1e4a8 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 );. rc = sqli
1e4a9 74 65 33 42 74 72 65 65 4b 65 79 53 69 7a 65 28 te3BtreeKeySize(
1e4aa 75 2e 62 68 2e 70 43 72 73 72 2c 20 26 75 2e 62 u.bh.pCrsr, &u.b
1e4ab 68 2e 6e 36 34 29 3b 0a 20 20 20 20 61 73 73 65 h.n64);. asse
1e4ac 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f rt( rc==SQLITE_O
1e4ad 4b 20 29 3b 20 20 20 20 2f 2a 20 54 72 75 65 20 K ); /* True
1e4ae 62 65 63 61 75 73 65 20 6f 66 20 43 75 72 73 6f because of Curso
1e4af 72 4d 6f 76 65 74 6f 28 29 20 63 61 6c 6c 20 61 rMoveto() call a
1e4b0 62 6f 76 65 20 2a 2f 0a 20 20 20 20 69 66 28 20 bove */. if(
1e4b1 75 2e 62 68 2e 6e 36 34 3e 64 62 2d 3e 61 4c 69 u.bh.n64>db->aLi
1e4b2 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 mit[SQLITE_LIMIT
1e4b3 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20 _LENGTH] ){.
1e4b4 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a goto too_big;.
1e4b5 20 20 20 20 7d 0a 20 20 20 20 75 2e 62 68 2e 6e }. u.bh.n
1e4b6 20 3d 20 28 75 33 32 29 75 2e 62 68 2e 6e 36 34 = (u32)u.bh.n64
1e4b7 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 ;. }else{. r
1e4b8 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 c = sqlite3Btree
1e4b9 44 61 74 61 53 69 7a 65 28 75 2e 62 68 2e 70 43 DataSize(u.bh.pC
1e4ba 72 73 72 2c 20 26 75 2e 62 68 2e 6e 29 3b 0a 20 rsr, &u.bh.n);.
1e4bb 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 assert( rc==S
1e4bc 51 4c 49 54 45 5f 4f 4b 20 29 3b 20 20 20 20 2f QLITE_OK ); /
1e4bd 2a 20 44 61 74 61 53 69 7a 65 28 29 20 63 61 6e * DataSize() can
1e4be 6e 6f 74 20 66 61 69 6c 20 2a 2f 0a 20 20 20 20 not fail */.
1e4bf 69 66 28 20 75 2e 62 68 2e 6e 3e 28 75 33 32 29 if( u.bh.n>(u32)
1e4c0 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 db->aLimit[SQLIT
1e4c1 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 E_LIMIT_LENGTH]
1e4c2 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 74 6f ){. goto to
1e4c3 6f 5f 62 69 67 3b 0a 20 20 20 20 7d 0a 20 20 7d o_big;. }. }
1e4c4 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 . if( sqlite3Vd
1e4c5 62 65 4d 65 6d 47 72 6f 77 28 70 4f 75 74 2c 20 beMemGrow(pOut,
1e4c6 75 2e 62 68 2e 6e 2c 20 30 29 20 29 7b 0a 20 20 u.bh.n, 0) ){.
1e4c7 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 goto no_mem;.
1e4c8 20 7d 0a 20 20 70 4f 75 74 2d 3e 6e 20 3d 20 75 }. pOut->n = u
1e4c9 2e 62 68 2e 6e 3b 0a 20 20 4d 65 6d 53 65 74 54 .bh.n;. MemSetT
1e4ca 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 ypeFlag(pOut, ME
1e4cb 4d 5f 42 6c 6f 62 29 3b 0a 20 20 69 66 28 20 75 M_Blob);. if( u
1e4cc 2e 62 68 2e 70 43 2d 3e 69 73 49 6e 64 65 78 20 .bh.pC->isIndex
1e4cd 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 ){. rc = sqli
1e4ce 74 65 33 42 74 72 65 65 4b 65 79 28 75 2e 62 68 te3BtreeKey(u.bh
1e4cf 2e 70 43 72 73 72 2c 20 30 2c 20 75 2e 62 68 2e .pCrsr, 0, u.bh.
1e4d0 6e 2c 20 70 4f 75 74 2d 3e 7a 29 3b 0a 20 20 7d n, pOut->z);. }
1e4d1 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 73 else{. rc = s
1e4d2 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 28 qlite3BtreeData(
1e4d3 75 2e 62 68 2e 70 43 72 73 72 2c 20 30 2c 20 75 u.bh.pCrsr, 0, u
1e4d4 2e 62 68 2e 6e 2c 20 70 4f 75 74 2d 3e 7a 29 3b .bh.n, pOut->z);
1e4d5 0a 20 20 7d 0a 20 20 70 4f 75 74 2d 3e 65 6e 63 . }. pOut->enc
1e4d6 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 3b 20 = SQLITE_UTF8;
1e4d7 20 2f 2a 20 49 6e 20 63 61 73 65 20 74 68 65 20 /* In case the
1e4d8 62 6c 6f 62 20 69 73 20 65 76 65 72 20 63 61 73 blob is ever cas
1e4d9 74 20 74 6f 20 74 65 78 74 20 2a 2f 0a 20 20 55 t to text */. U
1e4da 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 PDATE_MAX_BLOBSI
1e4db 5a 45 28 70 4f 75 74 29 3b 0a 20 20 62 72 65 61 ZE(pOut);. brea
1e4dc 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a k;.}../* Opcode:
1e4dd 20 52 6f 77 69 64 20 50 31 20 50 32 20 2a 20 2a Rowid P1 P2 * *
1e4de 20 2a 0a 2a 2a 0a 2a 2a 20 53 74 6f 72 65 20 69 *.**.** Store i
1e4df 6e 20 72 65 67 69 73 74 65 72 20 50 32 20 61 6e n register P2 an
1e4e0 20 69 6e 74 65 67 65 72 20 77 68 69 63 68 20 69 integer which i
1e4e1 73 20 74 68 65 20 6b 65 79 20 6f 66 20 74 68 65 s the key of the
1e4e2 20 74 61 62 6c 65 20 65 6e 74 72 79 20 74 68 61 table entry tha
1e4e3 74 0a 2a 2a 20 50 31 20 69 73 20 63 75 72 72 65 t.** P1 is curre
1e4e4 6e 74 6c 79 20 70 6f 69 6e 74 20 74 6f 2e 0a 2a ntly point to..*
1e4e5 2a 0a 2a 2a 20 50 31 20 63 61 6e 20 62 65 20 65 *.** P1 can be e
1e4e6 69 74 68 65 72 20 61 6e 20 6f 72 64 69 6e 61 72 ither an ordinar
1e4e7 79 20 74 61 62 6c 65 20 6f 72 20 61 20 76 69 72 y table or a vir
1e4e8 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 54 68 65 tual table. The
1e4e9 72 65 20 75 73 65 64 20 74 6f 0a 2a 2a 20 62 65 re used to.** be
1e4ea 20 61 20 73 65 70 61 72 61 74 65 20 4f 50 5f 56 a separate OP_V
1e4eb 52 6f 77 69 64 20 6f 70 63 6f 64 65 20 66 6f 72 Rowid opcode for
1e4ec 20 75 73 65 20 77 69 74 68 20 76 69 72 74 75 61 use with virtua
1e4ed 6c 20 74 61 62 6c 65 73 2c 20 62 75 74 20 74 68 l tables, but th
1e4ee 69 73 0a 2a 2a 20 6f 6e 65 20 6f 70 63 6f 64 65 is.** one opcode
1e4ef 20 6e 6f 77 20 77 6f 72 6b 73 20 66 6f 72 20 62 now works for b
1e4f0 6f 74 68 20 74 61 62 6c 65 20 74 79 70 65 73 2e oth table types.
1e4f1 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 6f 77 69 .*/.case OP_Rowi
1e4f2 64 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 d: {
1e4f3 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 /* out2-pre
1e4f4 72 65 6c 65 61 73 65 20 2a 2f 0a 23 69 66 20 30 release */.#if 0
1e4f5 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 /* local varia
1e4f6 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 bles moved into
1e4f7 75 2e 62 69 20 2a 2f 0a 20 20 56 64 62 65 43 75 u.bi */. VdbeCu
1e4f8 72 73 6f 72 20 2a 70 43 3b 0a 20 20 69 36 34 20 rsor *pC;. i64
1e4f9 76 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 v;. sqlite3_vta
1e4fa 62 20 2a 70 56 74 61 62 3b 0a 20 20 63 6f 6e 73 b *pVtab;. cons
1e4fb 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 t sqlite3_module
1e4fc 20 2a 70 4d 6f 64 75 6c 65 3b 0a 23 65 6e 64 69 *pModule;.#endi
1e4fd 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 f /* local varia
1e4fe 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 bles moved into
1e4ff 75 2e 62 69 20 2a 2f 0a 0a 20 20 61 73 73 65 72 u.bi */.. asser
1e500 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 t( pOp->p1>=0 &&
1e501 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 pOp->p1<p->nCur
1e502 73 6f 72 20 29 3b 0a 20 20 75 2e 62 69 2e 70 43 sor );. u.bi.pC
1e503 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d = p->apCsr[pOp-
1e504 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 >p1];. assert(
1e505 75 2e 62 69 2e 70 43 21 3d 30 20 29 3b 0a 20 20 u.bi.pC!=0 );.
1e506 61 73 73 65 72 74 28 20 75 2e 62 69 2e 70 43 2d assert( u.bi.pC-
1e507 3e 70 73 65 75 64 6f 54 61 62 6c 65 52 65 67 3d >pseudoTableReg=
1e508 3d 30 20 29 3b 0a 20 20 69 66 28 20 75 2e 62 69 =0 );. if( u.bi
1e509 2e 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 29 7b 0a .pC->nullRow ){.
1e50a 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 68 69 6e /* Do nothin
1e50b 67 20 73 6f 20 74 68 61 74 20 72 65 67 5b 50 32 g so that reg[P2
1e50c 5d 20 72 65 6d 61 69 6e 73 20 4e 55 4c 4c 20 2a ] remains NULL *
1e50d 2f 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d /. break;. }
1e50e 65 6c 73 65 20 69 66 28 20 75 2e 62 69 2e 70 43 else if( u.bi.pC
1e50f 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f ->deferredMoveto
1e510 20 29 7b 0a 20 20 20 20 75 2e 62 69 2e 76 20 3d ){. u.bi.v =
1e511 20 75 2e 62 69 2e 70 43 2d 3e 6d 6f 76 65 74 6f u.bi.pC->moveto
1e512 54 61 72 67 65 74 3b 0a 23 69 66 6e 64 65 66 20 Target;.#ifndef
1e513 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 SQLITE_OMIT_VIRT
1e514 55 41 4c 54 41 42 4c 45 0a 20 20 7d 65 6c 73 65 UALTABLE. }else
1e515 20 69 66 28 20 75 2e 62 69 2e 70 43 2d 3e 70 56 if( u.bi.pC->pV
1e516 74 61 62 43 75 72 73 6f 72 20 29 7b 0a 20 20 20 tabCursor ){.
1e517 20 75 2e 62 69 2e 70 56 74 61 62 20 3d 20 75 2e u.bi.pVtab = u.
1e518 62 69 2e 70 43 2d 3e 70 56 74 61 62 43 75 72 73 bi.pC->pVtabCurs
1e519 6f 72 2d 3e 70 56 74 61 62 3b 0a 20 20 20 20 75 or->pVtab;. u
1e51a 2e 62 69 2e 70 4d 6f 64 75 6c 65 20 3d 20 75 2e .bi.pModule = u.
1e51b 62 69 2e 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c bi.pVtab->pModul
1e51c 65 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 e;. assert( u
1e51d 2e 62 69 2e 70 4d 6f 64 75 6c 65 2d 3e 78 52 6f .bi.pModule->xRo
1e51e 77 69 64 20 29 3b 0a 20 20 20 20 69 66 28 20 73 wid );. if( s
1e51f 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 qlite3SafetyOff(
1e520 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 db) ) goto abort
1e521 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a _due_to_misuse;.
1e522 20 20 20 20 72 63 20 3d 20 75 2e 62 69 2e 70 4d rc = u.bi.pM
1e523 6f 64 75 6c 65 2d 3e 78 52 6f 77 69 64 28 75 2e odule->xRowid(u.
1e524 62 69 2e 70 43 2d 3e 70 56 74 61 62 43 75 72 73 bi.pC->pVtabCurs
1e525 6f 72 2c 20 26 75 2e 62 69 2e 76 29 3b 0a 20 20 or, &u.bi.v);.
1e526 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
1e527 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b db, p->zErrMsg);
1e528 0a 20 20 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 . p->zErrMsg
1e529 3d 20 75 2e 62 69 2e 70 56 74 61 62 2d 3e 7a 45 = u.bi.pVtab->zE
1e52a 72 72 4d 73 67 3b 0a 20 20 20 20 75 2e 62 69 2e rrMsg;. u.bi.
1e52b 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d pVtab->zErrMsg =
1e52c 20 30 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 0;. if( sqli
1e52d 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20 te3SafetyOn(db)
1e52e 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 ) goto abort_due
1e52f 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 23 65 6e 64 _to_misuse;.#end
1e530 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 if /* SQLITE_OMI
1e531 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a T_VIRTUALTABLE *
1e532 2f 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 /. }else{. a
1e533 73 73 65 72 74 28 20 75 2e 62 69 2e 70 43 2d 3e ssert( u.bi.pC->
1e534 70 43 75 72 73 6f 72 21 3d 30 20 29 3b 0a 20 20 pCursor!=0 );.
1e535 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 rc = sqlite3Vd
1e536 62 65 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28 75 beCursorMoveto(u
1e537 2e 62 69 2e 70 43 29 3b 0a 20 20 20 20 69 66 28 .bi.pC);. if(
1e538 20 72 63 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 rc ) goto abort
1e539 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 _due_to_error;.
1e53a 20 20 20 69 66 28 20 75 2e 62 69 2e 70 43 2d 3e if( u.bi.pC->
1e53b 72 6f 77 69 64 49 73 56 61 6c 69 64 20 29 7b 0a rowidIsValid ){.
1e53c 20 20 20 20 20 20 75 2e 62 69 2e 76 20 3d 20 75 u.bi.v = u
1e53d 2e 62 69 2e 70 43 2d 3e 6c 61 73 74 52 6f 77 69 .bi.pC->lastRowi
1e53e 64 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 d;. }else{.
1e53f 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1e540 42 74 72 65 65 4b 65 79 53 69 7a 65 28 75 2e 62 BtreeKeySize(u.b
1e541 69 2e 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 26 i.pC->pCursor, &
1e542 75 2e 62 69 2e 76 29 3b 0a 20 20 20 20 20 20 61 u.bi.v);. a
1e543 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 ssert( rc==SQLIT
1e544 45 5f 4f 4b 20 29 3b 20 20 2f 2a 20 41 6c 77 61 E_OK ); /* Alwa
1e545 79 73 20 73 6f 20 62 65 63 61 75 73 65 20 6f 66 ys so because of
1e546 20 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28 29 20 CursorMoveto()
1e547 61 62 6f 76 65 20 2a 2f 0a 20 20 20 20 7d 0a 20 above */. }.
1e548 20 7d 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d }. pOut->u.i =
1e549 20 75 2e 62 69 2e 76 3b 0a 20 20 4d 65 6d 53 65 u.bi.v;. MemSe
1e54a 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 tTypeFlag(pOut,
1e54b 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 62 72 65 61 MEM_Int);. brea
1e54c 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a k;.}../* Opcode:
1e54d 20 4e 75 6c 6c 52 6f 77 20 50 31 20 2a 20 2a 20 NullRow P1 * *
1e54e 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4d 6f 76 65 20 74 * *.**.** Move t
1e54f 68 65 20 63 75 72 73 6f 72 20 50 31 20 74 6f 20 he cursor P1 to
1e550 61 20 6e 75 6c 6c 20 72 6f 77 2e 20 20 41 6e 79 a null row. Any
1e551 20 4f 50 5f 43 6f 6c 75 6d 6e 20 6f 70 65 72 61 OP_Column opera
1e552 74 69 6f 6e 73 0a 2a 2a 20 74 68 61 74 20 6f 63 tions.** that oc
1e553 63 75 72 20 77 68 69 6c 65 20 74 68 65 20 63 75 cur while the cu
1e554 72 73 6f 72 20 69 73 20 6f 6e 20 74 68 65 20 6e rsor is on the n
1e555 75 6c 6c 20 72 6f 77 20 77 69 6c 6c 20 61 6c 77 ull row will alw
1e556 61 79 73 0a 2a 2a 20 77 72 69 74 65 20 61 20 4e ays.** write a N
1e557 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f ULL..*/.case OP_
1e558 4e 75 6c 6c 52 6f 77 3a 20 7b 0a 23 69 66 20 30 NullRow: {.#if 0
1e559 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 /* local varia
1e55a 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 bles moved into
1e55b 75 2e 62 6a 20 2a 2f 0a 20 20 56 64 62 65 43 75 u.bj */. VdbeCu
1e55c 72 73 6f 72 20 2a 70 43 3b 0a 23 65 6e 64 69 66 rsor *pC;.#endif
1e55d 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1e55e 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1e55f 2e 62 6a 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 .bj */.. assert
1e560 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 ( pOp->p1>=0 &&
1e561 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 pOp->p1<p->nCurs
1e562 6f 72 20 29 3b 0a 20 20 75 2e 62 6a 2e 70 43 20 or );. u.bj.pC
1e563 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e = p->apCsr[pOp->
1e564 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75 p1];. assert( u
1e565 2e 62 6a 2e 70 43 21 3d 30 20 29 3b 0a 20 20 75 .bj.pC!=0 );. u
1e566 2e 62 6a 2e 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 .bj.pC->nullRow
1e567 3d 20 31 3b 0a 20 20 75 2e 62 6a 2e 70 43 2d 3e = 1;. u.bj.pC->
1e568 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20 30 rowidIsValid = 0
1e569 3b 0a 20 20 69 66 28 20 75 2e 62 6a 2e 70 43 2d ;. if( u.bj.pC-
1e56a 3e 70 43 75 72 73 6f 72 20 29 7b 0a 20 20 20 20 >pCursor ){.
1e56b 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 65 61 sqlite3BtreeClea
1e56c 72 43 75 72 73 6f 72 28 75 2e 62 6a 2e 70 43 2d rCursor(u.bj.pC-
1e56d 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 7d 0a 20 >pCursor);. }.
1e56e 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 break;.}../* Op
1e56f 63 6f 64 65 3a 20 4c 61 73 74 20 50 31 20 50 32 code: Last P1 P2
1e570 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 * * *.**.** The
1e571 20 6e 65 78 74 20 75 73 65 20 6f 66 20 74 68 65 next use of the
1e572 20 52 6f 77 69 64 20 6f 72 20 43 6f 6c 75 6d 6e Rowid or Column
1e573 20 6f 72 20 4e 65 78 74 20 69 6e 73 74 72 75 63 or Next instruc
1e574 74 69 6f 6e 20 66 6f 72 20 50 31 20 0a 2a 2a 20 tion for P1 .**
1e575 77 69 6c 6c 20 72 65 66 65 72 20 74 6f 20 74 68 will refer to th
1e576 65 20 6c 61 73 74 20 65 6e 74 72 79 20 69 6e 20 e last entry in
1e577 74 68 65 20 64 61 74 61 62 61 73 65 20 74 61 62 the database tab
1e578 6c 65 20 6f 72 20 69 6e 64 65 78 2e 0a 2a 2a 20 le or index..**
1e579 49 66 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 If the table or
1e57a 69 6e 64 65 78 20 69 73 20 65 6d 70 74 79 20 61 index is empty a
1e57b 6e 64 20 50 32 3e 30 2c 20 74 68 65 6e 20 6a 75 nd P2>0, then ju
1e57c 6d 70 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 74 mp immediately t
1e57d 6f 20 50 32 2e 0a 2a 2a 20 49 66 20 50 32 20 69 o P2..** If P2 i
1e57e 73 20 30 20 6f 72 20 69 66 20 74 68 65 20 74 61 s 0 or if the ta
1e57f 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 69 73 20 ble or index is
1e580 6e 6f 74 20 65 6d 70 74 79 2c 20 66 61 6c 6c 20 not empty, fall
1e581 74 68 72 6f 75 67 68 0a 2a 2a 20 74 6f 20 74 68 through.** to th
1e582 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e 73 74 e following inst
1e583 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 ruction..*/.case
1e584 20 4f 50 5f 4c 61 73 74 3a 20 7b 20 20 20 20 20 OP_Last: {
1e585 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 23 69 /* jump */.#i
1e586 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 f 0 /* local va
1e587 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1e588 74 6f 20 75 2e 62 6b 20 2a 2f 0a 20 20 56 64 62 to u.bk */. Vdb
1e589 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 42 eCursor *pC;. B
1e58a 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a tCursor *pCrsr;.
1e58b 20 20 69 6e 74 20 72 65 73 3b 0a 23 65 6e 64 69 int res;.#endi
1e58c 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 f /* local varia
1e58d 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 bles moved into
1e58e 75 2e 62 6b 20 2a 2f 0a 0a 20 20 61 73 73 65 72 u.bk */.. asser
1e58f 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 t( pOp->p1>=0 &&
1e590 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 pOp->p1<p->nCur
1e591 73 6f 72 20 29 3b 0a 20 20 75 2e 62 6b 2e 70 43 sor );. u.bk.pC
1e592 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d = p->apCsr[pOp-
1e593 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 >p1];. assert(
1e594 75 2e 62 6b 2e 70 43 21 3d 30 20 29 3b 0a 20 20 u.bk.pC!=0 );.
1e595 75 2e 62 6b 2e 70 43 72 73 72 20 3d 20 75 2e 62 u.bk.pCrsr = u.b
1e596 6b 2e 70 43 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 k.pC->pCursor;.
1e597 20 69 66 28 20 75 2e 62 6b 2e 70 43 72 73 72 3d if( u.bk.pCrsr=
1e598 3d 30 20 29 7b 0a 20 20 20 20 75 2e 62 6b 2e 72 =0 ){. u.bk.r
1e599 65 73 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b es = 1;. }else{
1e59a 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
1e59b 33 42 74 72 65 65 4c 61 73 74 28 75 2e 62 6b 2e 3BtreeLast(u.bk.
1e59c 70 43 72 73 72 2c 20 26 75 2e 62 6b 2e 72 65 73 pCrsr, &u.bk.res
1e59d 29 3b 0a 20 20 7d 0a 20 20 75 2e 62 6b 2e 70 43 );. }. u.bk.pC
1e59e 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 28 75 38 29 ->nullRow = (u8)
1e59f 75 2e 62 6b 2e 72 65 73 3b 0a 20 20 75 2e 62 6b u.bk.res;. u.bk
1e5a0 2e 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 .pC->deferredMov
1e5a1 65 74 6f 20 3d 20 30 3b 0a 20 20 75 2e 62 6b 2e eto = 0;. u.bk.
1e5a2 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 pC->rowidIsValid
1e5a3 20 3d 20 30 3b 0a 20 20 75 2e 62 6b 2e 70 43 2d = 0;. u.bk.pC-
1e5a4 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 43 >cacheStatus = C
1e5a5 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 69 66 ACHE_STALE;. if
1e5a6 28 20 70 4f 70 2d 3e 70 32 3e 30 20 26 26 20 75 ( pOp->p2>0 && u
1e5a7 2e 62 6b 2e 72 65 73 20 29 7b 0a 20 20 20 20 70 .bk.res ){. p
1e5a8 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b c = pOp->p2 - 1;
1e5a9 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a . }. break;.}.
1e5aa 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 6f 72 ../* Opcode: Sor
1e5ab 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a t P1 P2 * * *.**
1e5ac 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20 .** This opcode
1e5ad 64 6f 65 73 20 65 78 61 63 74 6c 79 20 74 68 65 does exactly the
1e5ae 20 73 61 6d 65 20 74 68 69 6e 67 20 61 73 20 4f same thing as O
1e5af 50 5f 52 65 77 69 6e 64 20 65 78 63 65 70 74 20 P_Rewind except
1e5b0 74 68 61 74 0a 2a 2a 20 69 74 20 69 6e 63 72 65 that.** it incre
1e5b1 6d 65 6e 74 73 20 61 6e 20 75 6e 64 6f 63 75 6d ments an undocum
1e5b2 65 6e 74 65 64 20 67 6c 6f 62 61 6c 20 76 61 72 ented global var
1e5b3 69 61 62 6c 65 20 75 73 65 64 20 66 6f 72 20 74 iable used for t
1e5b4 65 73 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 53 6f esting..**.** So
1e5b5 72 74 69 6e 67 20 69 73 20 61 63 63 6f 6d 70 6c rting is accompl
1e5b6 69 73 68 65 64 20 62 79 20 77 72 69 74 69 6e 67 ished by writing
1e5b7 20 72 65 63 6f 72 64 73 20 69 6e 74 6f 20 61 20 records into a
1e5b8 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 2c 0a 2a sorting index,.*
1e5b9 2a 20 74 68 65 6e 20 72 65 77 69 6e 64 69 6e 67 * then rewinding
1e5ba 20 74 68 61 74 20 69 6e 64 65 78 20 61 6e 64 20 that index and
1e5bb 70 6c 61 79 69 6e 67 20 69 74 20 62 61 63 6b 20 playing it back
1e5bc 66 72 6f 6d 20 62 65 67 69 6e 6e 69 6e 67 20 74 from beginning t
1e5bd 6f 0a 2a 2a 20 65 6e 64 2e 20 20 57 65 20 75 73 o.** end. We us
1e5be 65 20 74 68 65 20 4f 50 5f 53 6f 72 74 20 6f 70 e the OP_Sort op
1e5bf 63 6f 64 65 20 69 6e 73 74 65 61 64 20 6f 66 20 code instead of
1e5c0 4f 50 5f 52 65 77 69 6e 64 20 74 6f 20 64 6f 20 OP_Rewind to do
1e5c1 74 68 65 0a 2a 2a 20 72 65 77 69 6e 64 69 6e 67 the.** rewinding
1e5c2 20 73 6f 20 74 68 61 74 20 74 68 65 20 67 6c 6f so that the glo
1e5c3 62 61 6c 20 76 61 72 69 61 62 6c 65 20 77 69 6c bal variable wil
1e5c4 6c 20 62 65 20 69 6e 63 72 65 6d 65 6e 74 65 64 l be incremented
1e5c5 20 61 6e 64 0a 2a 2a 20 72 65 67 72 65 73 73 69 and.** regressi
1e5c6 6f 6e 20 74 65 73 74 73 20 63 61 6e 20 64 65 74 on tests can det
1e5c7 65 72 6d 69 6e 65 20 77 68 65 74 68 65 72 20 6f ermine whether o
1e5c8 72 20 6e 6f 74 20 74 68 65 20 6f 70 74 69 6d 69 r not the optimi
1e5c9 7a 65 72 20 69 73 0a 2a 2a 20 63 6f 72 72 65 63 zer is.** correc
1e5ca 74 6c 79 20 6f 70 74 69 6d 69 7a 69 6e 67 20 6f tly optimizing o
1e5cb 75 74 20 73 6f 72 74 73 2e 0a 2a 2f 0a 63 61 73 ut sorts..*/.cas
1e5cc 65 20 4f 50 5f 53 6f 72 74 3a 20 7b 20 20 20 20 e OP_Sort: {
1e5cd 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 23 /* jump */.#
1e5ce 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 ifdef SQLITE_TES
1e5cf 54 0a 20 20 73 71 6c 69 74 65 33 5f 73 6f 72 74 T. sqlite3_sort
1e5d0 5f 63 6f 75 6e 74 2b 2b 3b 0a 20 20 73 71 6c 69 _count++;. sqli
1e5d1 74 65 33 5f 73 65 61 72 63 68 5f 63 6f 75 6e 74 te3_search_count
1e5d2 2d 2d 3b 0a 23 65 6e 64 69 66 0a 20 20 70 2d 3e --;.#endif. p->
1e5d3 61 43 6f 75 6e 74 65 72 5b 53 51 4c 49 54 45 5f aCounter[SQLITE_
1e5d4 53 54 4d 54 53 54 41 54 55 53 5f 53 4f 52 54 2d STMTSTATUS_SORT-
1e5d5 31 5d 2b 2b 3b 0a 20 20 2f 2a 20 46 61 6c 6c 20 1]++;. /* Fall
1e5d6 74 68 72 6f 75 67 68 20 69 6e 74 6f 20 4f 50 5f through into OP_
1e5d7 52 65 77 69 6e 64 20 2a 2f 0a 7d 0a 2f 2a 20 4f Rewind */.}./* O
1e5d8 70 63 6f 64 65 3a 20 52 65 77 69 6e 64 20 50 31 pcode: Rewind P1
1e5d9 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 P2 * * *.**.**
1e5da 54 68 65 20 6e 65 78 74 20 75 73 65 20 6f 66 20 The next use of
1e5db 74 68 65 20 52 6f 77 69 64 20 6f 72 20 43 6f 6c the Rowid or Col
1e5dc 75 6d 6e 20 6f 72 20 4e 65 78 74 20 69 6e 73 74 umn or Next inst
1e5dd 72 75 63 74 69 6f 6e 20 66 6f 72 20 50 31 20 0a ruction for P1 .
1e5de 2a 2a 20 77 69 6c 6c 20 72 65 66 65 72 20 74 6f ** will refer to
1e5df 20 74 68 65 20 66 69 72 73 74 20 65 6e 74 72 79 the first entry
1e5e0 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 in the database
1e5e1 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 2e table or index.
1e5e2 0a 2a 2a 20 49 66 20 74 68 65 20 74 61 62 6c 65 .** If the table
1e5e3 20 6f 72 20 69 6e 64 65 78 20 69 73 20 65 6d 70 or index is emp
1e5e4 74 79 20 61 6e 64 20 50 32 3e 30 2c 20 74 68 65 ty and P2>0, the
1e5e5 6e 20 6a 75 6d 70 20 69 6d 6d 65 64 69 61 74 65 n jump immediate
1e5e6 6c 79 20 74 6f 20 50 32 2e 0a 2a 2a 20 49 66 20 ly to P2..** If
1e5e7 50 32 20 69 73 20 30 20 6f 72 20 69 66 20 74 68 P2 is 0 or if th
1e5e8 65 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 e table or index
1e5e9 20 69 73 20 6e 6f 74 20 65 6d 70 74 79 2c 20 66 is not empty, f
1e5ea 61 6c 6c 20 74 68 72 6f 75 67 68 0a 2a 2a 20 74 all through.** t
1e5eb 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 o the following
1e5ec 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a instruction..*/.
1e5ed 63 61 73 65 20 4f 50 5f 52 65 77 69 6e 64 3a 20 case OP_Rewind:
1e5ee 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 { /* jump
1e5ef 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f */.#if 0 /* lo
1e5f0 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f cal variables mo
1e5f1 76 65 64 20 69 6e 74 6f 20 75 2e 62 6c 20 2a 2f ved into u.bl */
1e5f2 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 . VdbeCursor *p
1e5f3 43 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 C;. BtCursor *p
1e5f4 43 72 73 72 3b 0a 20 20 69 6e 74 20 72 65 73 3b Crsr;. int res;
1e5f5 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c .#endif /* local
1e5f6 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1e5f7 20 69 6e 74 6f 20 75 2e 62 6c 20 2a 2f 0a 0a 20 into u.bl */..
1e5f8 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 assert( pOp->p1
1e5f9 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 >=0 && pOp->p1<p
1e5fa 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 75 ->nCursor );. u
1e5fb 2e 62 6c 2e 70 43 20 3d 20 70 2d 3e 61 70 43 73 .bl.pC = p->apCs
1e5fc 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 r[pOp->p1];. as
1e5fd 73 65 72 74 28 20 75 2e 62 6c 2e 70 43 21 3d 30 sert( u.bl.pC!=0
1e5fe 20 29 3b 0a 20 20 69 66 28 20 28 75 2e 62 6c 2e );. if( (u.bl.
1e5ff 70 43 72 73 72 20 3d 20 75 2e 62 6c 2e 70 43 2d pCrsr = u.bl.pC-
1e600 3e 70 43 75 72 73 6f 72 29 21 3d 30 20 29 7b 0a >pCursor)!=0 ){.
1e601 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1e602 42 74 72 65 65 46 69 72 73 74 28 75 2e 62 6c 2e BtreeFirst(u.bl.
1e603 70 43 72 73 72 2c 20 26 75 2e 62 6c 2e 72 65 73 pCrsr, &u.bl.res
1e604 29 3b 0a 20 20 20 20 75 2e 62 6c 2e 70 43 2d 3e );. u.bl.pC->
1e605 61 74 46 69 72 73 74 20 3d 20 75 2e 62 6c 2e 72 atFirst = u.bl.r
1e606 65 73 3d 3d 30 20 3f 31 3a 30 3b 0a 20 20 20 20 es==0 ?1:0;.
1e607 75 2e 62 6c 2e 70 43 2d 3e 64 65 66 65 72 72 65 u.bl.pC->deferre
1e608 64 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a 20 20 20 dMoveto = 0;.
1e609 20 75 2e 62 6c 2e 70 43 2d 3e 63 61 63 68 65 53 u.bl.pC->cacheS
1e60a 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 tatus = CACHE_ST
1e60b 41 4c 45 3b 0a 20 20 20 20 75 2e 62 6c 2e 70 43 ALE;. u.bl.pC
1e60c 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d ->rowidIsValid =
1e60d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 0;. }else{.
1e60e 20 75 2e 62 6c 2e 72 65 73 20 3d 20 31 3b 0a 20 u.bl.res = 1;.
1e60f 20 7d 0a 20 20 75 2e 62 6c 2e 70 43 2d 3e 6e 75 }. u.bl.pC->nu
1e610 6c 6c 52 6f 77 20 3d 20 28 75 38 29 75 2e 62 6c llRow = (u8)u.bl
1e611 2e 72 65 73 3b 0a 20 20 61 73 73 65 72 74 28 20 .res;. assert(
1e612 70 4f 70 2d 3e 70 32 3e 30 20 26 26 20 70 4f 70 pOp->p2>0 && pOp
1e613 2d 3e 70 32 3c 70 2d 3e 6e 4f 70 20 29 3b 0a 20 ->p2<p->nOp );.
1e614 20 69 66 28 20 75 2e 62 6c 2e 72 65 73 20 29 7b if( u.bl.res ){
1e615 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 . pc = pOp->p
1e616 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 2 - 1;. }. bre
1e617 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 ak;.}../* Opcode
1e618 3a 20 4e 65 78 74 20 50 31 20 50 32 20 2a 20 2a : Next P1 P2 * *
1e619 20 2a 0a 2a 2a 0a 2a 2a 20 41 64 76 61 6e 63 65 *.**.** Advance
1e61a 20 63 75 72 73 6f 72 20 50 31 20 73 6f 20 74 68 cursor P1 so th
1e61b 61 74 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 at it points to
1e61c 74 68 65 20 6e 65 78 74 20 6b 65 79 2f 64 61 74 the next key/dat
1e61d 61 20 70 61 69 72 20 69 6e 20 69 74 73 0a 2a 2a a pair in its.**
1e61e 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 2e table or index.
1e61f 20 20 49 66 20 74 68 65 72 65 20 61 72 65 20 6e If there are n
1e620 6f 20 6d 6f 72 65 20 6b 65 79 2f 76 61 6c 75 65 o more key/value
1e621 20 70 61 69 72 73 20 74 68 65 6e 20 66 61 6c 6c pairs then fall
1e622 20 74 68 72 6f 75 67 68 0a 2a 2a 20 74 6f 20 74 through.** to t
1e623 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e 73 he following ins
1e624 74 72 75 63 74 69 6f 6e 2e 20 20 42 75 74 20 69 truction. But i
1e625 66 20 74 68 65 20 63 75 72 73 6f 72 20 61 64 76 f the cursor adv
1e626 61 6e 63 65 20 77 61 73 20 73 75 63 63 65 73 73 ance was success
1e627 66 75 6c 2c 0a 2a 2a 20 6a 75 6d 70 20 69 6d 6d ful,.** jump imm
1e628 65 64 69 61 74 65 6c 79 20 74 6f 20 50 32 2e 0a ediately to P2..
1e629 2a 2a 0a 2a 2a 20 54 68 65 20 50 31 20 63 75 72 **.** The P1 cur
1e62a 73 6f 72 20 6d 75 73 74 20 62 65 20 66 6f 72 20 sor must be for
1e62b 61 20 72 65 61 6c 20 74 61 62 6c 65 2c 20 6e 6f a real table, no
1e62c 74 20 61 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 t a pseudo-table
1e62d 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f ..**.** See also
1e62e 3a 20 50 72 65 76 0a 2a 2f 0a 2f 2a 20 4f 70 63 : Prev.*/./* Opc
1e62f 6f 64 65 3a 20 50 72 65 76 20 50 31 20 50 32 20 ode: Prev P1 P2
1e630 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 42 61 63 6b * * *.**.** Back
1e631 20 75 70 20 63 75 72 73 6f 72 20 50 31 20 73 6f up cursor P1 so
1e632 20 74 68 61 74 20 69 74 20 70 6f 69 6e 74 73 20 that it points
1e633 74 6f 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 to the previous
1e634 6b 65 79 2f 64 61 74 61 20 70 61 69 72 20 69 6e key/data pair in
1e635 20 69 74 73 0a 2a 2a 20 74 61 62 6c 65 20 6f 72 its.** table or
1e636 20 69 6e 64 65 78 2e 20 20 49 66 20 74 68 65 72 index. If ther
1e637 65 20 69 73 20 6e 6f 20 70 72 65 76 69 6f 75 73 e is no previous
1e638 20 6b 65 79 2f 76 61 6c 75 65 20 70 61 69 72 73 key/value pairs
1e639 20 74 68 65 6e 20 66 61 6c 6c 20 74 68 72 6f 75 then fall throu
1e63a 67 68 0a 2a 2a 20 74 6f 20 74 68 65 20 66 6f 6c gh.** to the fol
1e63b 6c 6f 77 69 6e 67 20 69 6e 73 74 72 75 63 74 69 lowing instructi
1e63c 6f 6e 2e 20 20 42 75 74 20 69 66 20 74 68 65 20 on. But if the
1e63d 63 75 72 73 6f 72 20 62 61 63 6b 75 70 20 77 61 cursor backup wa
1e63e 73 20 73 75 63 63 65 73 73 66 75 6c 2c 0a 2a 2a s successful,.**
1e63f 20 6a 75 6d 70 20 69 6d 6d 65 64 69 61 74 65 6c jump immediatel
1e640 79 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 54 y to P2..**.** T
1e641 68 65 20 50 31 20 63 75 72 73 6f 72 20 6d 75 73 he P1 cursor mus
1e642 74 20 62 65 20 66 6f 72 20 61 20 72 65 61 6c 20 t be for a real
1e643 74 61 62 6c 65 2c 20 6e 6f 74 20 61 20 70 73 65 table, not a pse
1e644 75 64 6f 2d 74 61 62 6c 65 2e 0a 2a 2f 0a 63 61 udo-table..*/.ca
1e645 73 65 20 4f 50 5f 50 72 65 76 3a 20 20 20 20 20 se OP_Prev:
1e646 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a /* jump */.
1e647 63 61 73 65 20 4f 50 5f 4e 65 78 74 3a 20 7b 20 case OP_Next: {
1e648 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a /* jump *
1e649 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 /.#if 0 /* loca
1e64a 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 l variables move
1e64b 64 20 69 6e 74 6f 20 75 2e 62 6d 20 2a 2f 0a 20 d into u.bm */.
1e64c 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b VdbeCursor *pC;
1e64d 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 . BtCursor *pCr
1e64e 73 72 3b 0a 20 20 69 6e 74 20 72 65 73 3b 0a 23 sr;. int res;.#
1e64f 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 endif /* local v
1e650 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 ariables moved i
1e651 6e 74 6f 20 75 2e 62 6d 20 2a 2f 0a 0a 20 20 43 nto u.bm */.. C
1e652 48 45 43 4b 5f 46 4f 52 5f 49 4e 54 45 52 52 55 HECK_FOR_INTERRU
1e653 50 54 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f PT;. assert( pO
1e654 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d p->p1>=0 && pOp-
1e655 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 >p1<p->nCursor )
1e656 3b 0a 20 20 75 2e 62 6d 2e 70 43 20 3d 20 70 2d ;. u.bm.pC = p-
1e657 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b >apCsr[pOp->p1];
1e658 0a 20 20 69 66 28 20 75 2e 62 6d 2e 70 43 3d 3d . if( u.bm.pC==
1e659 30 20 29 7b 0a 20 20 20 20 62 72 65 61 6b 3b 20 0 ){. break;
1e65a 20 2f 2a 20 53 65 65 20 74 69 63 6b 65 74 20 23 /* See ticket #
1e65b 32 32 37 33 20 2a 2f 0a 20 20 7d 0a 20 20 75 2e 2273 */. }. u.
1e65c 62 6d 2e 70 43 72 73 72 20 3d 20 75 2e 62 6d 2e bm.pCrsr = u.bm.
1e65d 70 43 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 69 pC->pCursor;. i
1e65e 66 28 20 75 2e 62 6d 2e 70 43 72 73 72 3d 3d 30 f( u.bm.pCrsr==0
1e65f 20 29 7b 0a 20 20 20 20 75 2e 62 6d 2e 70 43 2d ){. u.bm.pC-
1e660 3e 6e 75 6c 6c 52 6f 77 20 3d 20 31 3b 0a 20 20 >nullRow = 1;.
1e661 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 75 break;. }. u
1e662 2e 62 6d 2e 72 65 73 20 3d 20 31 3b 0a 20 20 61 .bm.res = 1;. a
1e663 73 73 65 72 74 28 20 75 2e 62 6d 2e 70 43 2d 3e ssert( u.bm.pC->
1e664 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 3d 3d deferredMoveto==
1e665 30 20 29 3b 0a 20 20 72 63 20 3d 20 70 4f 70 2d 0 );. rc = pOp-
1e666 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4e 65 78 74 >opcode==OP_Next
1e667 20 3f 20 73 71 6c 69 74 65 33 42 74 72 65 65 4e ? sqlite3BtreeN
1e668 65 78 74 28 75 2e 62 6d 2e 70 43 72 73 72 2c 20 ext(u.bm.pCrsr,
1e669 26 75 2e 62 6d 2e 72 65 73 29 20 3a 0a 20 20 20 &u.bm.res) :.
1e66a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e66b 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 sqlit
1e66c 65 33 42 74 72 65 65 50 72 65 76 69 6f 75 73 28 e3BtreePrevious(
1e66d 75 2e 62 6d 2e 70 43 72 73 72 2c 20 26 75 2e 62 u.bm.pCrsr, &u.b
1e66e 6d 2e 72 65 73 29 3b 0a 20 20 75 2e 62 6d 2e 70 m.res);. u.bm.p
1e66f 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 28 75 38 C->nullRow = (u8
1e670 29 75 2e 62 6d 2e 72 65 73 3b 0a 20 20 75 2e 62 )u.bm.res;. u.b
1e671 6d 2e 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75 m.pC->cacheStatu
1e672 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b s = CACHE_STALE;
1e673 0a 20 20 69 66 28 20 75 2e 62 6d 2e 72 65 73 3d . if( u.bm.res=
1e674 3d 30 20 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 =0 ){. pc = p
1e675 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 Op->p2 - 1;.
1e676 69 66 28 20 70 4f 70 2d 3e 70 35 20 29 20 70 2d if( pOp->p5 ) p-
1e677 3e 61 43 6f 75 6e 74 65 72 5b 70 4f 70 2d 3e 70 >aCounter[pOp->p
1e678 35 2d 31 5d 2b 2b 3b 0a 23 69 66 64 65 66 20 53 5-1]++;.#ifdef S
1e679 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 20 20 73 QLITE_TEST. s
1e67a 71 6c 69 74 65 33 5f 73 65 61 72 63 68 5f 63 6f qlite3_search_co
1e67b 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 unt++;.#endif.
1e67c 7d 0a 20 20 75 2e 62 6d 2e 70 43 2d 3e 72 6f 77 }. u.bm.pC->row
1e67d 69 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 idIsValid = 0;.
1e67e 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 break;.}../* Op
1e67f 63 6f 64 65 3a 20 49 64 78 49 6e 73 65 72 74 20 code: IdxInsert
1e680 50 31 20 50 32 20 50 33 20 2a 20 50 35 0a 2a 2a P1 P2 P3 * P5.**
1e681 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 50 32 20 .** Register P2
1e682 68 6f 6c 64 73 20 61 20 53 51 4c 20 69 6e 64 65 holds a SQL inde
1e683 78 20 6b 65 79 20 6d 61 64 65 20 75 73 69 6e 67 x key made using
1e684 20 74 68 65 0a 2a 2a 20 4d 61 6b 65 52 65 63 6f the.** MakeReco
1e685 72 64 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 2e rd instructions.
1e686 20 20 54 68 69 73 20 6f 70 63 6f 64 65 20 77 72 This opcode wr
1e687 69 74 65 73 20 74 68 61 74 20 6b 65 79 0a 2a 2a ites that key.**
1e688 20 69 6e 74 6f 20 74 68 65 20 69 6e 64 65 78 20 into the index
1e689 50 31 2e 20 20 44 61 74 61 20 66 6f 72 20 74 68 P1. Data for th
1e68a 65 20 65 6e 74 72 79 20 69 73 20 6e 69 6c 2e 0a e entry is nil..
1e68b 2a 2a 0a 2a 2a 20 50 33 20 69 73 20 61 20 66 6c **.** P3 is a fl
1e68c 61 67 20 74 68 61 74 20 70 72 6f 76 69 64 65 73 ag that provides
1e68d 20 61 20 68 69 6e 74 20 74 6f 20 74 68 65 20 62 a hint to the b
1e68e 2d 74 72 65 65 20 6c 61 79 65 72 20 74 68 61 74 -tree layer that
1e68f 20 74 68 69 73 0a 2a 2a 20 69 6e 73 65 72 74 20 this.** insert
1e690 69 73 20 6c 69 6b 65 6c 79 20 74 6f 20 62 65 20 is likely to be
1e691 61 6e 20 61 70 70 65 6e 64 2e 0a 2a 2a 0a 2a 2a an append..**.**
1e692 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f This instructio
1e693 6e 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 66 6f 72 n only works for
1e694 20 69 6e 64 69 63 65 73 2e 20 20 54 68 65 20 65 indices. The e
1e695 71 75 69 76 61 6c 65 6e 74 20 69 6e 73 74 72 75 quivalent instru
1e696 63 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20 74 61 62 ction.** for tab
1e697 6c 65 73 20 69 73 20 4f 50 5f 49 6e 73 65 72 74 les is OP_Insert
1e698 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 64 78 ..*/.case OP_Idx
1e699 49 6e 73 65 72 74 3a 20 7b 20 20 20 20 20 20 20 Insert: {
1e69a 20 2f 2a 20 69 6e 32 20 2a 2f 0a 23 69 66 20 30 /* in2 */.#if 0
1e69b 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 /* local varia
1e69c 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 bles moved into
1e69d 75 2e 62 6e 20 2a 2f 0a 20 20 56 64 62 65 43 75 u.bn */. VdbeCu
1e69e 72 73 6f 72 20 2a 70 43 3b 0a 20 20 42 74 43 75 rsor *pC;. BtCu
1e69f 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 69 rsor *pCrsr;. i
1e6a0 6e 74 20 6e 4b 65 79 3b 0a 20 20 63 6f 6e 73 74 nt nKey;. const
1e6a1 20 63 68 61 72 20 2a 7a 4b 65 79 3b 0a 23 65 6e char *zKey;.#en
1e6a2 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 dif /* local var
1e6a3 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 iables moved int
1e6a4 6f 20 75 2e 62 6e 20 2a 2f 0a 0a 20 20 61 73 73 o u.bn */.. ass
1e6a5 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 ert( pOp->p1>=0
1e6a6 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 && pOp->p1<p->nC
1e6a7 75 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 6e 2e ursor );. u.bn.
1e6a8 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f pC = p->apCsr[pO
1e6a9 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 p->p1];. assert
1e6aa 28 20 75 2e 62 6e 2e 70 43 21 3d 30 20 29 3b 0a ( u.bn.pC!=0 );.
1e6ab 20 20 61 73 73 65 72 74 28 20 70 49 6e 32 2d 3e assert( pIn2->
1e6ac 66 6c 61 67 73 20 26 20 4d 45 4d 5f 42 6c 6f 62 flags & MEM_Blob
1e6ad 20 29 3b 0a 20 20 75 2e 62 6e 2e 70 43 72 73 72 );. u.bn.pCrsr
1e6ae 20 3d 20 75 2e 62 6e 2e 70 43 2d 3e 70 43 75 72 = u.bn.pC->pCur
1e6af 73 6f 72 3b 0a 20 20 69 66 28 20 41 4c 57 41 59 sor;. if( ALWAY
1e6b0 53 28 75 2e 62 6e 2e 70 43 72 73 72 21 3d 30 29 S(u.bn.pCrsr!=0)
1e6b1 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
1e6b2 75 2e 62 6e 2e 70 43 2d 3e 69 73 54 61 62 6c 65 u.bn.pC->isTable
1e6b3 3d 3d 30 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 ==0 );. rc =
1e6b4 45 78 70 61 6e 64 42 6c 6f 62 28 70 49 6e 32 29 ExpandBlob(pIn2)
1e6b5 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 ;. if( rc==SQ
1e6b6 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
1e6b7 20 75 2e 62 6e 2e 6e 4b 65 79 20 3d 20 70 49 6e u.bn.nKey = pIn
1e6b8 32 2d 3e 6e 3b 0a 20 20 20 20 20 20 75 2e 62 6e 2->n;. u.bn
1e6b9 2e 7a 4b 65 79 20 3d 20 70 49 6e 32 2d 3e 7a 3b .zKey = pIn2->z;
1e6ba 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
1e6bb 74 65 33 42 74 72 65 65 49 6e 73 65 72 74 28 75 te3BtreeInsert(u
1e6bc 2e 62 6e 2e 70 43 72 73 72 2c 20 75 2e 62 6e 2e .bn.pCrsr, u.bn.
1e6bd 7a 4b 65 79 2c 20 75 2e 62 6e 2e 6e 4b 65 79 2c zKey, u.bn.nKey,
1e6be 20 22 22 2c 20 30 2c 20 30 2c 20 70 4f 70 2d 3e "", 0, 0, pOp->
1e6bf 70 33 2c 0a 20 20 20 20 20 20 20 20 20 20 28 28 p3,. ((
1e6c0 70 4f 70 2d 3e 70 35 20 26 20 4f 50 46 4c 41 47 pOp->p5 & OPFLAG
1e6c1 5f 55 53 45 53 45 45 4b 52 45 53 55 4c 54 29 20 _USESEEKRESULT)
1e6c2 3f 20 75 2e 62 6e 2e 70 43 2d 3e 73 65 65 6b 52 ? u.bn.pC->seekR
1e6c3 65 73 75 6c 74 20 3a 20 30 29 0a 20 20 20 20 20 esult : 0).
1e6c4 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 );. assert
1e6c5 28 20 75 2e 62 6e 2e 70 43 2d 3e 64 65 66 65 72 ( u.bn.pC->defer
1e6c6 72 65 64 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a redMoveto==0 );.
1e6c7 20 20 20 20 20 20 75 2e 62 6e 2e 70 43 2d 3e 63 u.bn.pC->c
1e6c8 61 63 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 acheStatus = CAC
1e6c9 48 45 5f 53 54 41 4c 45 3b 0a 20 20 20 20 7d 0a HE_STALE;. }.
1e6ca 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a }. break;.}..
1e6cb 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 64 78 44 65 /* Opcode: IdxDe
1e6cc 6c 65 74 65 20 50 31 20 50 32 20 50 33 20 2a 20 lete P1 P2 P3 *
1e6cd 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e 74 *.**.** The cont
1e6ce 65 6e 74 20 6f 66 20 50 33 20 72 65 67 69 73 74 ent of P3 regist
1e6cf 65 72 73 20 73 74 61 72 74 69 6e 67 20 61 74 20 ers starting at
1e6d0 72 65 67 69 73 74 65 72 20 50 32 20 66 6f 72 6d register P2 form
1e6d1 0a 2a 2a 20 61 6e 20 75 6e 70 61 63 6b 65 64 20 .** an unpacked
1e6d2 69 6e 64 65 78 20 6b 65 79 2e 20 54 68 69 73 20 index key. This
1e6d3 6f 70 63 6f 64 65 20 72 65 6d 6f 76 65 73 20 74 opcode removes t
1e6d4 68 61 74 20 65 6e 74 72 79 20 66 72 6f 6d 20 74 hat entry from t
1e6d5 68 65 20 0a 2a 2a 20 69 6e 64 65 78 20 6f 70 65 he .** index ope
1e6d6 6e 65 64 20 62 79 20 63 75 72 73 6f 72 20 50 31 ned by cursor P1
1e6d7 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 64 78 ..*/.case OP_Idx
1e6d8 44 65 6c 65 74 65 3a 20 7b 0a 23 69 66 20 30 20 Delete: {.#if 0
1e6d9 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1e6da 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1e6db 2e 62 6f 20 2a 2f 0a 20 20 56 64 62 65 43 75 72 .bo */. VdbeCur
1e6dc 73 6f 72 20 2a 70 43 3b 0a 20 20 42 74 43 75 72 sor *pC;. BtCur
1e6dd 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 69 6e sor *pCrsr;. in
1e6de 74 20 72 65 73 3b 0a 20 20 55 6e 70 61 63 6b 65 t res;. Unpacke
1e6df 64 52 65 63 6f 72 64 20 72 3b 0a 23 65 6e 64 69 dRecord r;.#endi
1e6e0 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 f /* local varia
1e6e1 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 bles moved into
1e6e2 75 2e 62 6f 20 2a 2f 0a 0a 20 20 61 73 73 65 72 u.bo */.. asser
1e6e3 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 29 3b 0a t( pOp->p3>0 );.
1e6e4 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
1e6e5 32 3e 30 20 26 26 20 70 4f 70 2d 3e 70 32 2b 70 2>0 && pOp->p2+p
1e6e6 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 2b Op->p3<=p->nMem+
1e6e7 31 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 1 );. assert( p
1e6e8 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 Op->p1>=0 && pOp
1e6e9 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 ->p1<p->nCursor
1e6ea 29 3b 0a 20 20 75 2e 62 6f 2e 70 43 20 3d 20 70 );. u.bo.pC = p
1e6eb 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d ->apCsr[pOp->p1]
1e6ec 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 6f ;. assert( u.bo
1e6ed 2e 70 43 21 3d 30 20 29 3b 0a 20 20 75 2e 62 6f .pC!=0 );. u.bo
1e6ee 2e 70 43 72 73 72 20 3d 20 75 2e 62 6f 2e 70 43 .pCrsr = u.bo.pC
1e6ef 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 69 66 28 ->pCursor;. if(
1e6f0 20 41 4c 57 41 59 53 28 75 2e 62 6f 2e 70 43 72 ALWAYS(u.bo.pCr
1e6f1 73 72 21 3d 30 29 20 29 7b 0a 20 20 20 20 75 2e sr!=0) ){. u.
1e6f2 62 6f 2e 72 2e 70 4b 65 79 49 6e 66 6f 20 3d 20 bo.r.pKeyInfo =
1e6f3 75 2e 62 6f 2e 70 43 2d 3e 70 4b 65 79 49 6e 66 u.bo.pC->pKeyInf
1e6f4 6f 3b 0a 20 20 20 20 75 2e 62 6f 2e 72 2e 6e 46 o;. u.bo.r.nF
1e6f5 69 65 6c 64 20 3d 20 28 75 31 36 29 70 4f 70 2d ield = (u16)pOp-
1e6f6 3e 70 33 3b 0a 20 20 20 20 75 2e 62 6f 2e 72 2e >p3;. u.bo.r.
1e6f7 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20 20 20 75 flags = 0;. u
1e6f8 2e 62 6f 2e 72 2e 61 4d 65 6d 20 3d 20 26 70 2d .bo.r.aMem = &p-
1e6f9 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a >aMem[pOp->p2];.
1e6fa 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1e6fb 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 BtreeMovetoUnpac
1e6fc 6b 65 64 28 75 2e 62 6f 2e 70 43 72 73 72 2c 20 ked(u.bo.pCrsr,
1e6fd 26 75 2e 62 6f 2e 72 2c 20 30 2c 20 30 2c 20 26 &u.bo.r, 0, 0, &
1e6fe 75 2e 62 6f 2e 72 65 73 29 3b 0a 20 20 20 20 69 u.bo.res);. i
1e6ff 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
1e700 20 26 26 20 75 2e 62 6f 2e 72 65 73 3d 3d 30 20 && u.bo.res==0
1e701 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 ){. rc = sq
1e702 6c 69 74 65 33 42 74 72 65 65 44 65 6c 65 74 65 lite3BtreeDelete
1e703 28 75 2e 62 6f 2e 70 43 72 73 72 29 3b 0a 20 20 (u.bo.pCrsr);.
1e704 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 }. assert(
1e705 75 2e 62 6f 2e 70 43 2d 3e 64 65 66 65 72 72 65 u.bo.pC->deferre
1e706 64 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a 20 20 dMoveto==0 );.
1e707 20 20 75 2e 62 6f 2e 70 43 2d 3e 63 61 63 68 65 u.bo.pC->cache
1e708 53 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 Status = CACHE_S
1e709 54 41 4c 45 3b 0a 20 20 7d 0a 20 20 62 72 65 61 TALE;. }. brea
1e70a 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a k;.}../* Opcode:
1e70b 20 49 64 78 52 6f 77 69 64 20 50 31 20 50 32 20 IdxRowid P1 P2
1e70c 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 * * *.**.** Writ
1e70d 65 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 e into register
1e70e 50 32 20 61 6e 20 69 6e 74 65 67 65 72 20 77 68 P2 an integer wh
1e70f 69 63 68 20 69 73 20 74 68 65 20 6c 61 73 74 20 ich is the last
1e710 65 6e 74 72 79 20 69 6e 20 74 68 65 20 72 65 63 entry in the rec
1e711 6f 72 64 20 61 74 0a 2a 2a 20 74 68 65 20 65 6e ord at.** the en
1e712 64 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 6b d of the index k
1e713 65 79 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 ey pointed to by
1e714 20 63 75 72 73 6f 72 20 50 31 2e 20 20 54 68 69 cursor P1. Thi
1e715 73 20 69 6e 74 65 67 65 72 20 73 68 6f 75 6c 64 s integer should
1e716 20 62 65 0a 2a 2a 20 74 68 65 20 72 6f 77 69 64 be.** the rowid
1e717 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 65 6e of the table en
1e718 74 72 79 20 74 6f 20 77 68 69 63 68 20 74 68 69 try to which thi
1e719 73 20 69 6e 64 65 78 20 65 6e 74 72 79 20 70 6f s index entry po
1e71a 69 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 ints..**.** See
1e71b 61 6c 73 6f 3a 20 52 6f 77 69 64 2c 20 4d 61 6b also: Rowid, Mak
1e71c 65 52 65 63 6f 72 64 2e 0a 2a 2f 0a 63 61 73 65 eRecord..*/.case
1e71d 20 4f 50 5f 49 64 78 52 6f 77 69 64 3a 20 7b 20 OP_IdxRowid: {
1e71e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1e71f 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 out2-prerelease
1e720 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 */.#if 0 /* loc
1e721 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1e722 65 64 20 69 6e 74 6f 20 75 2e 62 70 20 2a 2f 0a ed into u.bp */.
1e723 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 BtCursor *pCrs
1e724 72 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 r;. VdbeCursor
1e725 2a 70 43 3b 0a 20 20 69 36 34 20 72 6f 77 69 64 *pC;. i64 rowid
1e726 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 ;.#endif /* loca
1e727 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 l variables move
1e728 64 20 69 6e 74 6f 20 75 2e 62 70 20 2a 2f 0a 0a d into u.bp */..
1e729 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
1e72a 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 1>=0 && pOp->p1<
1e72b 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 p->nCursor );.
1e72c 75 2e 62 70 2e 70 43 20 3d 20 70 2d 3e 61 70 43 u.bp.pC = p->apC
1e72d 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 sr[pOp->p1];. a
1e72e 73 73 65 72 74 28 20 75 2e 62 70 2e 70 43 21 3d ssert( u.bp.pC!=
1e72f 30 20 29 3b 0a 20 20 75 2e 62 70 2e 70 43 72 73 0 );. u.bp.pCrs
1e730 72 20 3d 20 75 2e 62 70 2e 70 43 2d 3e 70 43 75 r = u.bp.pC->pCu
1e731 72 73 6f 72 3b 0a 20 20 69 66 28 20 41 4c 57 41 rsor;. if( ALWA
1e732 59 53 28 75 2e 62 70 2e 70 43 72 73 72 21 3d 30 YS(u.bp.pCrsr!=0
1e733 29 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 ) ){. rc = sq
1e734 6c 69 74 65 33 56 64 62 65 43 75 72 73 6f 72 4d lite3VdbeCursorM
1e735 6f 76 65 74 6f 28 75 2e 62 70 2e 70 43 29 3b 0a oveto(u.bp.pC);.
1e736 20 20 20 20 69 66 28 20 4e 45 56 45 52 28 72 63 if( NEVER(rc
1e737 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 ) ) goto abort_d
1e738 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 ue_to_error;.
1e739 20 61 73 73 65 72 74 28 20 75 2e 62 70 2e 70 43 assert( u.bp.pC
1e73a 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f ->deferredMoveto
1e73b 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 ==0 );. asser
1e73c 74 28 20 75 2e 62 70 2e 70 43 2d 3e 69 73 54 61 t( u.bp.pC->isTa
1e73d 62 6c 65 3d 3d 30 20 29 3b 0a 20 20 20 20 69 66 ble==0 );. if
1e73e 28 20 21 75 2e 62 70 2e 70 43 2d 3e 6e 75 6c 6c ( !u.bp.pC->null
1e73f 52 6f 77 20 29 7b 0a 20 20 20 20 20 20 72 63 20 Row ){. rc
1e740 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49 64 78 = sqlite3VdbeIdx
1e741 52 6f 77 69 64 28 64 62 2c 20 75 2e 62 70 2e 70 Rowid(db, u.bp.p
1e742 43 72 73 72 2c 20 26 75 2e 62 70 2e 72 6f 77 69 Crsr, &u.bp.rowi
1e743 64 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 d);. if( rc
1e744 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
1e745 20 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 goto abor
1e746 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a t_due_to_error;.
1e747 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 4d 65 }. Me
1e748 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f 75 mSetTypeFlag(pOu
1e749 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 20 t, MEM_Int);.
1e74a 20 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 pOut->u.i = u
1e74b 2e 62 70 2e 72 6f 77 69 64 3b 0a 20 20 20 20 7d .bp.rowid;. }
1e74c 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a . }. break;.}.
1e74d 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 64 78 47 ./* Opcode: IdxG
1e74e 45 20 50 31 20 50 32 20 50 33 20 50 34 20 50 35 E P1 P2 P3 P4 P5
1e74f 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 34 20 72 65 .**.** The P4 re
1e750 67 69 73 74 65 72 20 76 61 6c 75 65 73 20 62 65 gister values be
1e751 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 50 33 20 ginning with P3
1e752 66 6f 72 6d 20 61 6e 20 75 6e 70 61 63 6b 65 64 form an unpacked
1e753 20 69 6e 64 65 78 20 0a 2a 2a 20 6b 65 79 20 74 index .** key t
1e754 68 61 74 20 6f 6d 69 74 73 20 74 68 65 20 52 4f hat omits the RO
1e755 57 49 44 2e 20 20 43 6f 6d 70 61 72 65 20 74 68 WID. Compare th
1e756 69 73 20 6b 65 79 20 76 61 6c 75 65 20 61 67 61 is key value aga
1e757 69 6e 73 74 20 74 68 65 20 69 6e 64 65 78 20 0a inst the index .
1e758 2a 2a 20 74 68 61 74 20 50 31 20 69 73 20 63 75 ** that P1 is cu
1e759 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 rrently pointing
1e75a 20 74 6f 2c 20 69 67 6e 6f 72 69 6e 67 20 74 68 to, ignoring th
1e75b 65 20 52 4f 57 49 44 20 6f 6e 20 74 68 65 20 50 e ROWID on the P
1e75c 31 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 49 1 index..**.** I
1e75d 66 20 74 68 65 20 50 31 20 69 6e 64 65 78 20 65 f the P1 index e
1e75e 6e 74 72 79 20 69 73 20 67 72 65 61 74 65 72 20 ntry is greater
1e75f 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f than or equal to
1e760 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65 0a 2a the key value.*
1e761 2a 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 50 * then jump to P
1e762 32 2e 20 20 4f 74 68 65 72 77 69 73 65 20 66 61 2. Otherwise fa
1e763 6c 6c 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 ll through to th
1e764 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69 e next instructi
1e765 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 35 20 on..**.** If P5
1e766 69 73 20 6e 6f 6e 2d 7a 65 72 6f 20 74 68 65 6e is non-zero then
1e767 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65 20 69 the key value i
1e768 73 20 69 6e 63 72 65 61 73 65 64 20 62 79 20 61 s increased by a
1e769 6e 20 65 70 73 69 6c 6f 6e 20 0a 2a 2a 20 70 72 n epsilon .** pr
1e76a 69 6f 72 20 74 6f 20 74 68 65 20 63 6f 6d 70 61 ior to the compa
1e76b 72 69 73 6f 6e 2e 20 20 54 68 69 73 20 6d 61 6b rison. This mak
1e76c 65 20 74 68 65 20 6f 70 63 6f 64 65 20 77 6f 72 e the opcode wor
1e76d 6b 20 6c 69 6b 65 20 49 64 78 47 54 20 65 78 63 k like IdxGT exc
1e76e 65 70 74 0a 2a 2a 20 74 68 61 74 20 69 66 20 74 ept.** that if t
1e76f 68 65 20 6b 65 79 20 66 72 6f 6d 20 72 65 67 69 he key from regi
1e770 73 74 65 72 20 50 33 20 69 73 20 61 20 70 72 65 ster P3 is a pre
1e771 66 69 78 20 6f 66 20 74 68 65 20 6b 65 79 20 69 fix of the key i
1e772 6e 20 74 68 65 20 63 75 72 73 6f 72 2c 0a 2a 2a n the cursor,.**
1e773 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 66 the result is f
1e774 61 6c 73 65 20 77 68 65 72 65 61 73 20 69 74 20 alse whereas it
1e775 77 6f 75 6c 64 20 62 65 20 74 72 75 65 20 77 69 would be true wi
1e776 74 68 20 49 64 78 47 54 2e 0a 2a 2f 0a 2f 2a 20 th IdxGT..*/./*
1e777 4f 70 63 6f 64 65 3a 20 49 64 78 4c 54 20 50 31 Opcode: IdxLT P1
1e778 20 50 32 20 50 33 20 2a 20 50 35 0a 2a 2a 0a 2a P2 P3 * P5.**.*
1e779 2a 20 54 68 65 20 50 34 20 72 65 67 69 73 74 65 * The P4 registe
1e77a 72 20 76 61 6c 75 65 73 20 62 65 67 69 6e 6e 69 r values beginni
1e77b 6e 67 20 77 69 74 68 20 50 33 20 66 6f 72 6d 20 ng with P3 form
1e77c 61 6e 20 75 6e 70 61 63 6b 65 64 20 69 6e 64 65 an unpacked inde
1e77d 78 20 0a 2a 2a 20 6b 65 79 20 74 68 61 74 20 6f x .** key that o
1e77e 6d 69 74 73 20 74 68 65 20 52 4f 57 49 44 2e 20 mits the ROWID.
1e77f 20 43 6f 6d 70 61 72 65 20 74 68 69 73 20 6b 65 Compare this ke
1e780 79 20 76 61 6c 75 65 20 61 67 61 69 6e 73 74 20 y value against
1e781 74 68 65 20 69 6e 64 65 78 20 0a 2a 2a 20 74 68 the index .** th
1e782 61 74 20 50 31 20 69 73 20 63 75 72 72 65 6e 74 at P1 is current
1e783 6c 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2c 20 ly pointing to,
1e784 69 67 6e 6f 72 69 6e 67 20 74 68 65 20 52 4f 57 ignoring the ROW
1e785 49 44 20 6f 6e 20 74 68 65 20 50 31 20 69 6e 64 ID on the P1 ind
1e786 65 78 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 ex..**.** If the
1e787 20 50 31 20 69 6e 64 65 78 20 65 6e 74 72 79 20 P1 index entry
1e788 69 73 20 6c 65 73 73 20 74 68 61 6e 20 74 68 65 is less than the
1e789 20 6b 65 79 20 76 61 6c 75 65 20 74 68 65 6e 20 key value then
1e78a 6a 75 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a 20 4f jump to P2..** O
1e78b 74 68 65 72 77 69 73 65 20 66 61 6c 6c 20 74 68 therwise fall th
1e78c 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6e 65 78 rough to the nex
1e78d 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a t instruction..*
1e78e 2a 0a 2a 2a 20 49 66 20 50 35 20 69 73 20 6e 6f *.** If P5 is no
1e78f 6e 2d 7a 65 72 6f 20 74 68 65 6e 20 74 68 65 20 n-zero then the
1e790 6b 65 79 20 76 61 6c 75 65 20 69 73 20 69 6e 63 key value is inc
1e791 72 65 61 73 65 64 20 62 79 20 61 6e 20 65 70 73 reased by an eps
1e792 69 6c 6f 6e 20 70 72 69 6f 72 20 0a 2a 2a 20 74 ilon prior .** t
1e793 6f 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e o the comparison
1e794 2e 20 20 54 68 69 73 20 6d 61 6b 65 73 20 74 68 . This makes th
1e795 65 20 6f 70 63 6f 64 65 20 77 6f 72 6b 20 6c 69 e opcode work li
1e796 6b 65 20 49 64 78 4c 45 2e 0a 2a 2f 0a 63 61 73 ke IdxLE..*/.cas
1e797 65 20 4f 50 5f 49 64 78 4c 54 3a 20 20 20 20 20 e OP_IdxLT:
1e798 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e /* jump, in
1e799 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 64 78 3 */.case OP_Idx
1e79a 47 45 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 GE: { /*
1e79b 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a 23 69 66 jump, in3 */.#if
1e79c 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 0 /* local var
1e79d 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 iables moved int
1e79e 6f 20 75 2e 62 71 20 2a 2f 0a 20 20 56 64 62 65 o u.bq */. Vdbe
1e79f 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 69 6e Cursor *pC;. in
1e7a0 74 20 72 65 73 3b 0a 20 20 55 6e 70 61 63 6b 65 t res;. Unpacke
1e7a1 64 52 65 63 6f 72 64 20 72 3b 0a 23 65 6e 64 69 dRecord r;.#endi
1e7a2 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 f /* local varia
1e7a3 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 bles moved into
1e7a4 75 2e 62 71 20 2a 2f 0a 0a 20 20 61 73 73 65 72 u.bq */.. asser
1e7a5 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 t( pOp->p1>=0 &&
1e7a6 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 pOp->p1<p->nCur
1e7a7 73 6f 72 20 29 3b 0a 20 20 75 2e 62 71 2e 70 43 sor );. u.bq.pC
1e7a8 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d = p->apCsr[pOp-
1e7a9 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 >p1];. assert(
1e7aa 75 2e 62 71 2e 70 43 21 3d 30 20 29 3b 0a 20 20 u.bq.pC!=0 );.
1e7ab 69 66 28 20 41 4c 57 41 59 53 28 75 2e 62 71 2e if( ALWAYS(u.bq.
1e7ac 70 43 2d 3e 70 43 75 72 73 6f 72 21 3d 30 29 20 pC->pCursor!=0)
1e7ad 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 ){. assert( u
1e7ae 2e 62 71 2e 70 43 2d 3e 64 65 66 65 72 72 65 64 .bq.pC->deferred
1e7af 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a 20 20 20 Moveto==0 );.
1e7b0 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 35 assert( pOp->p5
1e7b1 3d 3d 30 20 7c 7c 20 70 4f 70 2d 3e 70 35 3d 3d ==0 || pOp->p5==
1e7b2 31 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 1 );. assert(
1e7b3 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 pOp->p4type==P4
1e7b4 5f 49 4e 54 33 32 20 29 3b 0a 20 20 20 20 75 2e _INT32 );. u.
1e7b5 62 71 2e 72 2e 70 4b 65 79 49 6e 66 6f 20 3d 20 bq.r.pKeyInfo =
1e7b6 75 2e 62 71 2e 70 43 2d 3e 70 4b 65 79 49 6e 66 u.bq.pC->pKeyInf
1e7b7 6f 3b 0a 20 20 20 20 75 2e 62 71 2e 72 2e 6e 46 o;. u.bq.r.nF
1e7b8 69 65 6c 64 20 3d 20 28 75 31 36 29 70 4f 70 2d ield = (u16)pOp-
1e7b9 3e 70 34 2e 69 3b 0a 20 20 20 20 69 66 28 20 70 >p4.i;. if( p
1e7ba 4f 70 2d 3e 70 35 20 29 7b 0a 20 20 20 20 20 20 Op->p5 ){.
1e7bb 75 2e 62 71 2e 72 2e 66 6c 61 67 73 20 3d 20 55 u.bq.r.flags = U
1e7bc 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b 45 59 20 NPACKED_INCRKEY
1e7bd 7c 20 55 4e 50 41 43 4b 45 44 5f 49 47 4e 4f 52 | UNPACKED_IGNOR
1e7be 45 5f 52 4f 57 49 44 3b 0a 20 20 20 20 7d 65 6c E_ROWID;. }el
1e7bf 73 65 7b 0a 20 20 20 20 20 20 75 2e 62 71 2e 72 se{. u.bq.r
1e7c0 2e 66 6c 61 67 73 20 3d 20 55 4e 50 41 43 4b 45 .flags = UNPACKE
1e7c1 44 5f 49 47 4e 4f 52 45 5f 52 4f 57 49 44 3b 0a D_IGNORE_ROWID;.
1e7c2 20 20 20 20 7d 0a 20 20 20 20 75 2e 62 71 2e 72 }. u.bq.r
1e7c3 2e 61 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d 65 6d .aMem = &p->aMem
1e7c4 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20 20 72 [pOp->p3];. r
1e7c5 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49 c = sqlite3VdbeI
1e7c6 64 78 4b 65 79 43 6f 6d 70 61 72 65 28 75 2e 62 dxKeyCompare(u.b
1e7c7 71 2e 70 43 2c 20 26 75 2e 62 71 2e 72 2c 20 26 q.pC, &u.bq.r, &
1e7c8 75 2e 62 71 2e 72 65 73 29 3b 0a 20 20 20 20 69 u.bq.res);. i
1e7c9 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d f( pOp->opcode==
1e7ca 4f 50 5f 49 64 78 4c 54 20 29 7b 0a 20 20 20 20 OP_IdxLT ){.
1e7cb 20 20 75 2e 62 71 2e 72 65 73 20 3d 20 2d 75 2e u.bq.res = -u.
1e7cc 62 71 2e 72 65 73 3b 0a 20 20 20 20 7d 65 6c 73 bq.res;. }els
1e7cd 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 e{. assert(
1e7ce 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 pOp->opcode==OP
1e7cf 5f 49 64 78 47 45 20 29 3b 0a 20 20 20 20 20 20 _IdxGE );.
1e7d0 75 2e 62 71 2e 72 65 73 2b 2b 3b 0a 20 20 20 20 u.bq.res++;.
1e7d1 7d 0a 20 20 20 20 69 66 28 20 75 2e 62 71 2e 72 }. if( u.bq.r
1e7d2 65 73 3e 30 20 29 7b 0a 20 20 20 20 20 20 70 63 es>0 ){. pc
1e7d3 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 20 3b = pOp->p2 - 1 ;
1e7d4 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65 . }. }. bre
1e7d5 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 ak;.}../* Opcode
1e7d6 3a 20 44 65 73 74 72 6f 79 20 50 31 20 50 32 20 : Destroy P1 P2
1e7d7 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 44 65 6c P3 * *.**.** Del
1e7d8 65 74 65 20 61 6e 20 65 6e 74 69 72 65 20 64 61 ete an entire da
1e7d9 74 61 62 61 73 65 20 74 61 62 6c 65 20 6f 72 20 tabase table or
1e7da 69 6e 64 65 78 20 77 68 6f 73 65 20 72 6f 6f 74 index whose root
1e7db 20 70 61 67 65 20 69 6e 20 74 68 65 20 64 61 74 page in the dat
1e7dc 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 20 69 73 abase.** file is
1e7dd 20 67 69 76 65 6e 20 62 79 20 50 31 2e 0a 2a 2a given by P1..**
1e7de 0a 2a 2a 20 54 68 65 20 74 61 62 6c 65 20 62 65 .** The table be
1e7df 69 6e 67 20 64 65 73 74 72 6f 79 65 64 20 69 73 ing destroyed is
1e7e0 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 74 in the main dat
1e7e1 61 62 61 73 65 20 66 69 6c 65 20 69 66 20 50 33 abase file if P3
1e7e2 3d 3d 30 2e 20 20 49 66 0a 2a 2a 20 50 33 3d 3d ==0. If.** P3==
1e7e3 31 20 74 68 65 6e 20 74 68 65 20 74 61 62 6c 65 1 then the table
1e7e4 20 74 6f 20 62 65 20 63 6c 65 61 72 20 69 73 20 to be clear is
1e7e5 69 6e 20 74 68 65 20 61 75 78 69 6c 69 61 72 79 in the auxiliary
1e7e6 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 0a 2a database file.*
1e7e7 2a 20 74 68 61 74 20 69 73 20 75 73 65 64 20 74 * that is used t
1e7e8 6f 20 73 74 6f 72 65 20 74 61 62 6c 65 73 20 63 o store tables c
1e7e9 72 65 61 74 65 20 75 73 69 6e 67 20 43 52 45 41 reate using CREA
1e7ea 54 45 20 54 45 4d 50 4f 52 41 52 59 20 54 41 42 TE TEMPORARY TAB
1e7eb 4c 45 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 41 55 54 LE..**.** If AUT
1e7ec 4f 56 41 43 55 55 4d 20 69 73 20 65 6e 61 62 6c OVACUUM is enabl
1e7ed 65 64 20 74 68 65 6e 20 69 74 20 69 73 20 70 6f ed then it is po
1e7ee 73 73 69 62 6c 65 20 74 68 61 74 20 61 6e 6f 74 ssible that anot
1e7ef 68 65 72 20 72 6f 6f 74 20 70 61 67 65 0a 2a 2a her root page.**
1e7f0 20 6d 69 67 68 74 20 62 65 20 6d 6f 76 65 64 20 might be moved
1e7f1 69 6e 74 6f 20 74 68 65 20 6e 65 77 6c 79 20 64 into the newly d
1e7f2 65 6c 65 74 65 64 20 72 6f 6f 74 20 70 61 67 65 eleted root page
1e7f3 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6b 65 65 in order to kee
1e7f4 70 20 61 6c 6c 0a 2a 2a 20 72 6f 6f 74 20 70 61 p all.** root pa
1e7f5 67 65 73 20 63 6f 6e 74 69 67 75 6f 75 73 20 61 ges contiguous a
1e7f6 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 t the beginning
1e7f7 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 2e of the database.
1e7f8 20 20 54 68 65 20 66 6f 72 6d 65 72 0a 2a 2a 20 The former.**
1e7f9 76 61 6c 75 65 20 6f 66 20 74 68 65 20 72 6f 6f value of the roo
1e7fa 74 20 70 61 67 65 20 74 68 61 74 20 6d 6f 76 65 t page that move
1e7fb 64 20 2d 20 69 74 73 20 76 61 6c 75 65 20 62 65 d - its value be
1e7fc 66 6f 72 65 20 74 68 65 20 6d 6f 76 65 20 6f 63 fore the move oc
1e7fd 63 75 72 72 65 64 20 2d 0a 2a 2a 20 69 73 20 73 curred -.** is s
1e7fe 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74 65 tored in registe
1e7ff 72 20 50 32 2e 20 20 49 66 20 6e 6f 20 70 61 67 r P2. If no pag
1e800 65 20 0a 2a 2a 20 6d 6f 76 65 6d 65 6e 74 20 77 e .** movement w
1e801 61 73 20 72 65 71 75 69 72 65 64 20 28 62 65 63 as required (bec
1e802 61 75 73 65 20 74 68 65 20 74 61 62 6c 65 20 62 ause the table b
1e803 65 69 6e 67 20 64 72 6f 70 70 65 64 20 77 61 73 eing dropped was
1e804 20 61 6c 72 65 61 64 79 20 0a 2a 2a 20 74 68 65 already .** the
1e805 20 6c 61 73 74 20 6f 6e 65 20 69 6e 20 74 68 65 last one in the
1e806 20 64 61 74 61 62 61 73 65 29 20 74 68 65 6e 20 database) then
1e807 61 20 7a 65 72 6f 20 69 73 20 73 74 6f 72 65 64 a zero is stored
1e808 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 2e in register P2.
1e809 0a 2a 2a 20 49 66 20 41 55 54 4f 56 41 43 55 55 .** If AUTOVACUU
1e80a 4d 20 69 73 20 64 69 73 61 62 6c 65 64 20 74 68 M is disabled th
1e80b 65 6e 20 61 20 7a 65 72 6f 20 69 73 20 73 74 6f en a zero is sto
1e80c 72 65 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 red in register
1e80d 50 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c P2..**.** See al
1e80e 73 6f 3a 20 43 6c 65 61 72 0a 2a 2f 0a 63 61 73 so: Clear.*/.cas
1e80f 65 20 4f 50 5f 44 65 73 74 72 6f 79 3a 20 7b 20 e OP_Destroy: {
1e810 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 /* out2-prer
1e811 65 6c 65 61 73 65 20 2a 2f 0a 23 69 66 20 30 20 elease */.#if 0
1e812 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1e813 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1e814 2e 62 72 20 2a 2f 0a 20 20 69 6e 74 20 69 4d 6f .br */. int iMo
1e815 76 65 64 3b 0a 20 20 69 6e 74 20 69 43 6e 74 3b ved;. int iCnt;
1e816 0a 20 20 56 64 62 65 20 2a 70 56 64 62 65 3b 0a . Vdbe *pVdbe;.
1e817 20 20 69 6e 74 20 69 44 62 3b 0a 23 65 6e 64 69 int iDb;.#endi
1e818 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 f /* local varia
1e819 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 bles moved into
1e81a 75 2e 62 72 20 2a 2f 0a 23 69 66 6e 64 65 66 20 u.br */.#ifndef
1e81b 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 SQLITE_OMIT_VIRT
1e81c 55 41 4c 54 41 42 4c 45 0a 20 20 75 2e 62 72 2e UALTABLE. u.br.
1e81d 69 43 6e 74 20 3d 20 30 3b 0a 20 20 66 6f 72 28 iCnt = 0;. for(
1e81e 75 2e 62 72 2e 70 56 64 62 65 3d 64 62 2d 3e 70 u.br.pVdbe=db->p
1e81f 56 64 62 65 3b 20 75 2e 62 72 2e 70 56 64 62 65 Vdbe; u.br.pVdbe
1e820 3b 20 75 2e 62 72 2e 70 56 64 62 65 20 3d 20 75 ; u.br.pVdbe = u
1e821 2e 62 72 2e 70 56 64 62 65 2d 3e 70 4e 65 78 74 .br.pVdbe->pNext
1e822 29 7b 0a 20 20 20 20 69 66 28 20 75 2e 62 72 2e ){. if( u.br.
1e823 70 56 64 62 65 2d 3e 6d 61 67 69 63 3d 3d 56 44 pVdbe->magic==VD
1e824 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 26 26 20 BE_MAGIC_RUN &&
1e825 75 2e 62 72 2e 70 56 64 62 65 2d 3e 69 6e 56 74 u.br.pVdbe->inVt
1e826 61 62 4d 65 74 68 6f 64 3c 32 20 26 26 20 75 2e abMethod<2 && u.
1e827 62 72 2e 70 56 64 62 65 2d 3e 70 63 3e 3d 30 20 br.pVdbe->pc>=0
1e828 29 7b 0a 20 20 20 20 20 20 75 2e 62 72 2e 69 43 ){. u.br.iC
1e829 6e 74 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a nt++;. }. }.
1e82a 23 65 6c 73 65 0a 20 20 75 2e 62 72 2e 69 43 6e #else. u.br.iCn
1e82b 74 20 3d 20 64 62 2d 3e 61 63 74 69 76 65 56 64 t = db->activeVd
1e82c 62 65 43 6e 74 3b 0a 23 65 6e 64 69 66 0a 20 20 beCnt;.#endif.
1e82d 69 66 28 20 75 2e 62 72 2e 69 43 6e 74 3e 31 20 if( u.br.iCnt>1
1e82e 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 ){. rc = SQLI
1e82f 54 45 5f 4c 4f 43 4b 45 44 3b 0a 20 20 20 20 70 TE_LOCKED;. p
1e830 2d 3e 65 72 72 6f 72 41 63 74 69 6f 6e 20 3d 20 ->errorAction =
1e831 4f 45 5f 41 62 6f 72 74 3b 0a 20 20 7d 65 6c 73 OE_Abort;. }els
1e832 65 7b 0a 20 20 20 20 75 2e 62 72 2e 69 44 62 20 e{. u.br.iDb
1e833 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 20 20 61 = pOp->p3;. a
1e834 73 73 65 72 74 28 20 75 2e 62 72 2e 69 43 6e 74 ssert( u.br.iCnt
1e835 3d 3d 31 20 29 3b 0a 20 20 20 20 61 73 73 65 72 ==1 );. asser
1e836 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b t( (p->btreeMask
1e837 20 26 20 28 31 3c 3c 75 2e 62 72 2e 69 44 62 29 & (1<<u.br.iDb)
1e838 29 21 3d 30 20 29 3b 0a 20 20 20 20 72 63 20 3d )!=0 );. rc =
1e839 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 72 6f sqlite3BtreeDro
1e83a 70 54 61 62 6c 65 28 64 62 2d 3e 61 44 62 5b 75 pTable(db->aDb[u
1e83b 2e 62 72 2e 69 44 62 5d 2e 70 42 74 2c 20 70 4f .br.iDb].pBt, pO
1e83c 70 2d 3e 70 31 2c 20 26 75 2e 62 72 2e 69 4d 6f p->p1, &u.br.iMo
1e83d 76 65 64 29 3b 0a 20 20 20 20 4d 65 6d 53 65 74 ved);. MemSet
1e83e 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d TypeFlag(pOut, M
1e83f 45 4d 5f 49 6e 74 29 3b 0a 20 20 20 20 70 4f 75 EM_Int);. pOu
1e840 74 2d 3e 75 2e 69 20 3d 20 75 2e 62 72 2e 69 4d t->u.i = u.br.iM
1e841 6f 76 65 64 3b 0a 23 69 66 6e 64 65 66 20 53 51 oved;.#ifndef SQ
1e842 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 LITE_OMIT_AUTOVA
1e843 43 55 55 4d 0a 20 20 20 20 69 66 28 20 72 63 3d CUUM. if( rc=
1e844 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 75 2e =SQLITE_OK && u.
1e845 62 72 2e 69 4d 6f 76 65 64 21 3d 30 20 29 7b 0a br.iMoved!=0 ){.
1e846 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 6f 6f sqlite3Roo
1e847 74 50 61 67 65 4d 6f 76 65 64 28 26 64 62 2d 3e tPageMoved(&db->
1e848 61 44 62 5b 75 2e 62 72 2e 69 44 62 5d 2c 20 75 aDb[u.br.iDb], u
1e849 2e 62 72 2e 69 4d 6f 76 65 64 2c 20 70 4f 70 2d .br.iMoved, pOp-
1e84a 3e 70 31 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 >p1);. }.#end
1e84b 69 66 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a if. }. break;.
1e84c 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6c }../* Opcode: Cl
1e84d 65 61 72 20 50 31 20 50 32 20 50 33 0a 2a 2a 0a ear P1 P2 P3.**.
1e84e 2a 2a 20 44 65 6c 65 74 65 20 61 6c 6c 20 63 6f ** Delete all co
1e84f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 64 61 ntents of the da
1e850 74 61 62 61 73 65 20 74 61 62 6c 65 20 6f 72 20 tabase table or
1e851 69 6e 64 65 78 20 77 68 6f 73 65 20 72 6f 6f 74 index whose root
1e852 20 70 61 67 65 0a 2a 2a 20 69 6e 20 74 68 65 20 page.** in the
1e853 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 73 database file is
1e854 20 67 69 76 65 6e 20 62 79 20 50 31 2e 20 20 42 given by P1. B
1e855 75 74 2c 20 75 6e 6c 69 6b 65 20 44 65 73 74 72 ut, unlike Destr
1e856 6f 79 2c 20 64 6f 20 6e 6f 74 0a 2a 2a 20 72 65 oy, do not.** re
1e857 6d 6f 76 65 20 74 68 65 20 74 61 62 6c 65 20 6f move the table o
1e858 72 20 69 6e 64 65 78 20 66 72 6f 6d 20 74 68 65 r index from the
1e859 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a database file..
1e85a 2a 2a 0a 2a 2a 20 54 68 65 20 74 61 62 6c 65 20 **.** The table
1e85b 62 65 69 6e 67 20 63 6c 65 61 72 20 69 73 20 69 being clear is i
1e85c 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 n the main datab
1e85d 61 73 65 20 66 69 6c 65 20 69 66 20 50 32 3d 3d ase file if P2==
1e85e 30 2e 20 20 49 66 0a 2a 2a 20 50 32 3d 3d 31 20 0. If.** P2==1
1e85f 74 68 65 6e 20 74 68 65 20 74 61 62 6c 65 20 74 then the table t
1e860 6f 20 62 65 20 63 6c 65 61 72 20 69 73 20 69 6e o be clear is in
1e861 20 74 68 65 20 61 75 78 69 6c 69 61 72 79 20 64 the auxiliary d
1e862 61 74 61 62 61 73 65 20 66 69 6c 65 0a 2a 2a 20 atabase file.**
1e863 74 68 61 74 20 69 73 20 75 73 65 64 20 74 6f 20 that is used to
1e864 73 74 6f 72 65 20 74 61 62 6c 65 73 20 63 72 65 store tables cre
1e865 61 74 65 20 75 73 69 6e 67 20 43 52 45 41 54 45 ate using CREATE
1e866 20 54 45 4d 50 4f 52 41 52 59 20 54 41 42 4c 45 TEMPORARY TABLE
1e867 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 50 ..**.** If the P
1e868 33 20 76 61 6c 75 65 20 69 73 20 6e 6f 6e 2d 7a 3 value is non-z
1e869 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 74 61 ero, then the ta
1e86a 62 6c 65 20 72 65 66 65 72 72 65 64 20 74 6f 20 ble referred to
1e86b 6d 75 73 74 20 62 65 20 61 6e 0a 2a 2a 20 69 6e must be an.** in
1e86c 74 6b 65 79 20 74 61 62 6c 65 20 28 61 6e 20 53 tkey table (an S
1e86d 51 4c 20 74 61 62 6c 65 2c 20 6e 6f 74 20 61 6e QL table, not an
1e86e 20 69 6e 64 65 78 29 2e 20 49 6e 20 74 68 69 73 index). In this
1e86f 20 63 61 73 65 20 74 68 65 20 72 6f 77 20 63 68 case the row ch
1e870 61 6e 67 65 20 0a 2a 2a 20 63 6f 75 6e 74 20 69 ange .** count i
1e871 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 62 79 s incremented by
1e872 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 the number of r
1e873 6f 77 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 ows in the table
1e874 20 62 65 69 6e 67 20 63 6c 65 61 72 65 64 2e 20 being cleared.
1e875 0a 2a 2a 20 49 66 20 50 33 20 69 73 20 67 72 65 .** If P3 is gre
1e876 61 74 65 72 20 74 68 61 6e 20 7a 65 72 6f 2c 20 ater than zero,
1e877 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 73 then the value s
1e878 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74 65 tored in registe
1e879 72 20 50 33 20 69 73 0a 2a 2a 20 61 6c 73 6f 20 r P3 is.** also
1e87a 69 6e 63 72 65 6d 65 6e 74 65 64 20 62 79 20 74 incremented by t
1e87b 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 he number of row
1e87c 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20 62 s in the table b
1e87d 65 69 6e 67 20 63 6c 65 61 72 65 64 2e 0a 2a 2a eing cleared..**
1e87e 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 44 65 .** See also: De
1e87f 73 74 72 6f 79 0a 2a 2f 0a 63 61 73 65 20 4f 50 stroy.*/.case OP
1e880 5f 43 6c 65 61 72 3a 20 7b 0a 23 69 66 20 30 20 _Clear: {.#if 0
1e881 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1e882 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1e883 2e 62 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 68 .bs */. int nCh
1e884 61 6e 67 65 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 ange;.#endif /*
1e885 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 local variables
1e886 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 73 20 moved into u.bs
1e887 2a 2f 0a 0a 20 20 75 2e 62 73 2e 6e 43 68 61 6e */.. u.bs.nChan
1e888 67 65 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 ge = 0;. assert
1e889 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 ( (p->btreeMask
1e88a 26 20 28 31 3c 3c 70 4f 70 2d 3e 70 32 29 29 21 & (1<<pOp->p2))!
1e88b 3d 30 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c =0 );. rc = sql
1e88c 69 74 65 33 42 74 72 65 65 43 6c 65 61 72 54 61 ite3BtreeClearTa
1e88d 62 6c 65 28 0a 20 20 20 20 20 20 64 62 2d 3e 61 ble(. db->a
1e88e 44 62 5b 70 4f 70 2d 3e 70 32 5d 2e 70 42 74 2c Db[pOp->p2].pBt,
1e88f 20 70 4f 70 2d 3e 70 31 2c 20 28 70 4f 70 2d 3e pOp->p1, (pOp->
1e890 70 33 20 3f 20 26 75 2e 62 73 2e 6e 43 68 61 6e p3 ? &u.bs.nChan
1e891 67 65 20 3a 20 30 29 0a 20 20 29 3b 0a 20 20 69 ge : 0). );. i
1e892 66 28 20 70 4f 70 2d 3e 70 33 20 29 7b 0a 20 20 f( pOp->p3 ){.
1e893 20 20 70 2d 3e 6e 43 68 61 6e 67 65 20 2b 3d 20 p->nChange +=
1e894 75 2e 62 73 2e 6e 43 68 61 6e 67 65 3b 0a 20 20 u.bs.nChange;.
1e895 20 20 69 66 28 20 70 4f 70 2d 3e 70 33 3e 30 20 if( pOp->p3>0
1e896 29 7b 0a 20 20 20 20 20 20 70 2d 3e 61 4d 65 6d ){. p->aMem
1e897 5b 70 4f 70 2d 3e 70 33 5d 2e 75 2e 69 20 2b 3d [pOp->p3].u.i +=
1e898 20 75 2e 62 73 2e 6e 43 68 61 6e 67 65 3b 0a 20 u.bs.nChange;.
1e899 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61 6b }. }. break
1e89a 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ;.}../* Opcode:
1e89b 43 72 65 61 74 65 54 61 62 6c 65 20 50 31 20 50 CreateTable P1 P
1e89c 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41 6c 2 * * *.**.** Al
1e89d 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 74 61 62 locate a new tab
1e89e 6c 65 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 64 le in the main d
1e89f 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 66 20 atabase file if
1e8a0 50 31 3d 3d 30 20 6f 72 20 69 6e 20 74 68 65 0a P1==0 or in the.
1e8a1 2a 2a 20 61 75 78 69 6c 69 61 72 79 20 64 61 74 ** auxiliary dat
1e8a2 61 62 61 73 65 20 66 69 6c 65 20 69 66 20 50 31 abase file if P1
1e8a3 3d 3d 31 20 6f 72 20 69 6e 20 61 6e 20 61 74 74 ==1 or in an att
1e8a4 61 63 68 65 64 20 64 61 74 61 62 61 73 65 20 69 ached database i
1e8a5 66 0a 2a 2a 20 50 31 3e 31 2e 20 20 57 72 69 74 f.** P1>1. Writ
1e8a6 65 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 e the root page
1e8a7 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 6e 65 number of the ne
1e8a8 77 20 74 61 62 6c 65 20 69 6e 74 6f 0a 2a 2a 20 w table into.**
1e8a9 72 65 67 69 73 74 65 72 20 50 32 0a 2a 2a 0a 2a register P2.**.*
1e8aa 2a 20 54 68 65 20 64 69 66 66 65 72 65 6e 63 65 * The difference
1e8ab 20 62 65 74 77 65 65 6e 20 61 20 74 61 62 6c 65 between a table
1e8ac 20 61 6e 64 20 61 6e 20 69 6e 64 65 78 20 69 73 and an index is
1e8ad 20 74 68 69 73 3a 20 20 41 20 74 61 62 6c 65 20 this: A table
1e8ae 6d 75 73 74 0a 2a 2a 20 68 61 76 65 20 61 20 34 must.** have a 4
1e8af 2d 62 79 74 65 20 69 6e 74 65 67 65 72 20 6b 65 -byte integer ke
1e8b0 79 20 61 6e 64 20 63 61 6e 20 68 61 76 65 20 61 y and can have a
1e8b1 72 62 69 74 72 61 72 79 20 64 61 74 61 2e 20 20 rbitrary data.
1e8b2 41 6e 20 69 6e 64 65 78 0a 2a 2a 20 68 61 73 20 An index.** has
1e8b3 61 6e 20 61 72 62 69 74 72 61 72 79 20 6b 65 79 an arbitrary key
1e8b4 20 62 75 74 20 6e 6f 20 64 61 74 61 2e 0a 2a 2a but no data..**
1e8b5 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 43 72 .** See also: Cr
1e8b6 65 61 74 65 49 6e 64 65 78 0a 2a 2f 0a 2f 2a 20 eateIndex.*/./*
1e8b7 4f 70 63 6f 64 65 3a 20 43 72 65 61 74 65 49 6e Opcode: CreateIn
1e8b8 64 65 78 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a dex P1 P2 * * *.
1e8b9 2a 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 **.** Allocate a
1e8ba 20 6e 65 77 20 69 6e 64 65 78 20 69 6e 20 74 68 new index in th
1e8bb 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 e main database
1e8bc 66 69 6c 65 20 69 66 20 50 31 3d 3d 30 20 6f 72 file if P1==0 or
1e8bd 20 69 6e 20 74 68 65 0a 2a 2a 20 61 75 78 69 6c in the.** auxil
1e8be 69 61 72 79 20 64 61 74 61 62 61 73 65 20 66 69 iary database fi
1e8bf 6c 65 20 69 66 20 50 31 3d 3d 31 20 6f 72 20 69 le if P1==1 or i
1e8c0 6e 20 61 6e 20 61 74 74 61 63 68 65 64 20 64 61 n an attached da
1e8c1 74 61 62 61 73 65 20 69 66 0a 2a 2a 20 50 31 3e tabase if.** P1>
1e8c2 31 2e 20 20 57 72 69 74 65 20 74 68 65 20 72 6f 1. Write the ro
1e8c3 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f ot page number o
1e8c4 66 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 f the new table
1e8c5 69 6e 74 6f 0a 2a 2a 20 72 65 67 69 73 74 65 72 into.** register
1e8c6 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 64 P2..**.** See d
1e8c7 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 6f 6e 20 ocumentation on
1e8c8 4f 50 5f 43 72 65 61 74 65 54 61 62 6c 65 20 66 OP_CreateTable f
1e8c9 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e or additional in
1e8ca 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 formation..*/.ca
1e8cb 73 65 20 4f 50 5f 43 72 65 61 74 65 49 6e 64 65 se OP_CreateInde
1e8cc 78 3a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a x: /*
1e8cd 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 out2-prerelease
1e8ce 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 72 65 61 */.case OP_Crea
1e8cf 74 65 54 61 62 6c 65 3a 20 7b 20 20 20 20 20 20 teTable: {
1e8d0 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 /* out2-prer
1e8d1 65 6c 65 61 73 65 20 2a 2f 0a 23 69 66 20 30 20 elease */.#if 0
1e8d2 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1e8d3 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1e8d4 2e 62 74 20 2a 2f 0a 20 20 69 6e 74 20 70 67 6e .bt */. int pgn
1e8d5 6f 3b 0a 20 20 69 6e 74 20 66 6c 61 67 73 3b 0a o;. int flags;.
1e8d6 20 20 44 62 20 2a 70 44 62 3b 0a 23 65 6e 64 69 Db *pDb;.#endi
1e8d7 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 f /* local varia
1e8d8 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 bles moved into
1e8d9 75 2e 62 74 20 2a 2f 0a 0a 20 20 75 2e 62 74 2e u.bt */.. u.bt.
1e8da 70 67 6e 6f 20 3d 20 30 3b 0a 20 20 61 73 73 65 pgno = 0;. asse
1e8db 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 rt( pOp->p1>=0 &
1e8dc 26 20 70 4f 70 2d 3e 70 31 3c 64 62 2d 3e 6e 44 & pOp->p1<db->nD
1e8dd 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 b );. assert( (
1e8de 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28 p->btreeMask & (
1e8df 31 3c 3c 70 4f 70 2d 3e 70 31 29 29 21 3d 30 20 1<<pOp->p1))!=0
1e8e0 29 3b 0a 20 20 75 2e 62 74 2e 70 44 62 20 3d 20 );. u.bt.pDb =
1e8e1 26 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 31 &db->aDb[pOp->p1
1e8e2 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 ];. assert( u.b
1e8e3 74 2e 70 44 62 2d 3e 70 42 74 21 3d 30 20 29 3b t.pDb->pBt!=0 );
1e8e4 0a 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f . if( pOp->opco
1e8e5 64 65 3d 3d 4f 50 5f 43 72 65 61 74 65 54 61 62 de==OP_CreateTab
1e8e6 6c 65 20 29 7b 0a 20 20 20 20 2f 2a 20 75 2e 62 le ){. /* u.b
1e8e7 74 2e 66 6c 61 67 73 20 3d 20 42 54 52 45 45 5f t.flags = BTREE_
1e8e8 49 4e 54 4b 45 59 3b 20 2a 2f 0a 20 20 20 20 75 INTKEY; */. u
1e8e9 2e 62 74 2e 66 6c 61 67 73 20 3d 20 42 54 52 45 .bt.flags = BTRE
1e8ea 45 5f 4c 45 41 46 44 41 54 41 7c 42 54 52 45 45 E_LEAFDATA|BTREE
1e8eb 5f 49 4e 54 4b 45 59 3b 0a 20 20 7d 65 6c 73 65 _INTKEY;. }else
1e8ec 7b 0a 20 20 20 20 75 2e 62 74 2e 66 6c 61 67 73 {. u.bt.flags
1e8ed 20 3d 20 42 54 52 45 45 5f 5a 45 52 4f 44 41 54 = BTREE_ZERODAT
1e8ee 41 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73 71 A;. }. rc = sq
1e8ef 6c 69 74 65 33 42 74 72 65 65 43 72 65 61 74 65 lite3BtreeCreate
1e8f0 54 61 62 6c 65 28 75 2e 62 74 2e 70 44 62 2d 3e Table(u.bt.pDb->
1e8f1 70 42 74 2c 20 26 75 2e 62 74 2e 70 67 6e 6f 2c pBt, &u.bt.pgno,
1e8f2 20 75 2e 62 74 2e 66 6c 61 67 73 29 3b 0a 20 20 u.bt.flags);.
1e8f3 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 2e 62 74 pOut->u.i = u.bt
1e8f4 2e 70 67 6e 6f 3b 0a 20 20 4d 65 6d 53 65 74 54 .pgno;. MemSetT
1e8f5 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 ypeFlag(pOut, ME
1e8f6 4d 5f 49 6e 74 29 3b 0a 20 20 62 72 65 61 6b 3b M_Int);. break;
1e8f7 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 50 .}../* Opcode: P
1e8f8 61 72 73 65 53 63 68 65 6d 61 20 50 31 20 50 32 arseSchema P1 P2
1e8f9 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 * P4 *.**.** Re
1e8fa 61 64 20 61 6e 64 20 70 61 72 73 65 20 61 6c 6c ad and parse all
1e8fb 20 65 6e 74 72 69 65 73 20 66 72 6f 6d 20 74 68 entries from th
1e8fc 65 20 53 51 4c 49 54 45 5f 4d 41 53 54 45 52 20 e SQLITE_MASTER
1e8fd 74 61 62 6c 65 20 6f 66 20 64 61 74 61 62 61 73 table of databas
1e8fe 65 20 50 31 0a 2a 2a 20 74 68 61 74 20 6d 61 74 e P1.** that mat
1e8ff 63 68 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 ch the WHERE cla
1e900 75 73 65 20 50 34 2e 20 20 50 32 20 69 73 20 74 use P4. P2 is t
1e901 68 65 20 22 66 6f 72 63 65 22 20 66 6c 61 67 2e he "force" flag.
1e902 20 20 20 41 6c 77 61 79 73 20 64 6f 0a 2a 2a 20 Always do.**
1e903 74 68 65 20 70 61 72 73 69 6e 67 20 69 66 20 50 the parsing if P
1e904 32 20 69 73 20 74 72 75 65 2e 20 20 49 66 20 50 2 is true. If P
1e905 32 20 69 73 20 66 61 6c 73 65 2c 20 74 68 65 6e 2 is false, then
1e906 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 this routine is
1e907 20 61 0a 2a 2a 20 6e 6f 2d 6f 70 20 69 66 20 74 a.** no-op if t
1e908 68 65 20 73 63 68 65 6d 61 20 69 73 20 6e 6f 74 he schema is not
1e909 20 63 75 72 72 65 6e 74 6c 79 20 6c 6f 61 64 65 currently loade
1e90a 64 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 d. In other wor
1e90b 64 73 2c 20 69 66 20 50 32 0a 2a 2a 20 69 73 20 ds, if P2.** is
1e90c 66 61 6c 73 65 2c 20 74 68 65 20 53 51 4c 49 54 false, the SQLIT
1e90d 45 5f 4d 41 53 54 45 52 20 74 61 62 6c 65 20 69 E_MASTER table i
1e90e 73 20 6f 6e 6c 79 20 70 61 72 73 65 64 20 69 66 s only parsed if
1e90f 20 74 68 65 20 72 65 73 74 20 6f 66 20 74 68 65 the rest of the
1e910 0a 2a 2a 20 73 63 68 65 6d 61 20 69 73 20 61 6c .** schema is al
1e911 72 65 61 64 79 20 6c 6f 61 64 65 64 20 69 6e 74 ready loaded int
1e912 6f 20 74 68 65 20 73 79 6d 62 6f 6c 20 74 61 62 o the symbol tab
1e913 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f le..**.** This o
1e914 70 63 6f 64 65 20 69 6e 76 6f 6b 65 73 20 74 68 pcode invokes th
1e915 65 20 70 61 72 73 65 72 20 74 6f 20 63 72 65 61 e parser to crea
1e916 74 65 20 61 20 6e 65 77 20 76 69 72 74 75 61 6c te a new virtual
1e917 20 6d 61 63 68 69 6e 65 2c 0a 2a 2a 20 74 68 65 machine,.** the
1e918 6e 20 72 75 6e 73 20 74 68 65 20 6e 65 77 20 76 n runs the new v
1e919 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e 20 irtual machine.
1e91a 20 49 74 20 69 73 20 74 68 75 73 20 61 20 72 65 It is thus a re
1e91b 2d 65 6e 74 72 61 6e 74 20 6f 70 63 6f 64 65 2e -entrant opcode.
1e91c 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 50 61 72 73 .*/.case OP_Pars
1e91d 65 53 63 68 65 6d 61 3a 20 7b 0a 23 69 66 20 30 eSchema: {.#if 0
1e91e 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 /* local varia
1e91f 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 bles moved into
1e920 75 2e 62 75 20 2a 2f 0a 20 20 69 6e 74 20 69 44 u.bu */. int iD
1e921 62 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 b;. const char
1e922 2a 7a 4d 61 73 74 65 72 3b 0a 20 20 63 68 61 72 *zMaster;. char
1e923 20 2a 7a 53 71 6c 3b 0a 20 20 49 6e 69 74 44 61 *zSql;. InitDa
1e924 74 61 20 69 6e 69 74 44 61 74 61 3b 0a 23 65 6e ta initData;.#en
1e925 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 dif /* local var
1e926 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 iables moved int
1e927 6f 20 75 2e 62 75 20 2a 2f 0a 0a 20 20 75 2e 62 o u.bu */.. u.b
1e928 75 2e 69 44 62 20 3d 20 70 4f 70 2d 3e 70 31 3b u.iDb = pOp->p1;
1e929 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 75 2e . assert( u.bu.
1e92a 69 44 62 3e 3d 30 20 26 26 20 75 2e 62 75 2e 69 iDb>=0 && u.bu.i
1e92b 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 0a 20 Db<db->nDb );..
1e92c 20 2f 2a 20 49 66 20 70 4f 70 2d 3e 70 32 20 69 /* If pOp->p2 i
1e92d 73 20 30 2c 20 74 68 65 6e 20 74 68 69 73 20 6f s 0, then this o
1e92e 70 63 6f 64 65 20 69 73 20 62 65 69 6e 67 20 65 pcode is being e
1e92f 78 65 63 75 74 65 64 20 74 6f 20 72 65 61 64 20 xecuted to read
1e930 61 0a 20 20 2a 2a 20 73 69 6e 67 6c 65 20 72 6f a. ** single ro
1e931 77 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 20 74 w, for example t
1e932 68 65 20 72 6f 77 20 63 6f 72 72 65 73 70 6f 6e he row correspon
1e933 64 69 6e 67 20 74 6f 20 61 20 6e 65 77 20 69 6e ding to a new in
1e934 64 65 78 0a 20 20 2a 2a 20 63 72 65 61 74 65 64 dex. ** created
1e935 20 62 79 20 74 68 69 73 20 56 44 42 45 2c 20 66 by this VDBE, f
1e936 72 6f 6d 20 74 68 65 20 73 71 6c 69 74 65 5f 6d rom the sqlite_m
1e937 61 73 74 65 72 20 74 61 62 6c 65 2e 20 49 74 20 aster table. It
1e938 6f 6e 6c 79 0a 20 20 2a 2a 20 64 6f 65 73 20 74 only. ** does t
1e939 68 69 73 20 69 66 20 74 68 65 20 63 6f 72 72 65 his if the corre
1e93a 73 70 6f 6e 64 69 6e 67 20 69 6e 2d 6d 65 6d 6f sponding in-memo
1e93b 72 79 20 73 63 68 65 6d 61 20 69 73 20 63 75 72 ry schema is cur
1e93c 72 65 6e 74 6c 79 0a 20 20 2a 2a 20 6c 6f 61 64 rently. ** load
1e93d 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 74 ed. Otherwise, t
1e93e 68 65 20 6e 65 77 20 69 6e 64 65 78 20 64 65 66 he new index def
1e93f 69 6e 69 74 69 6f 6e 20 63 61 6e 20 62 65 20 6c inition can be l
1e940 6f 61 64 65 64 20 61 6c 6f 6e 67 0a 20 20 2a 2a oaded along. **
1e941 20 77 69 74 68 20 74 68 65 20 72 65 73 74 20 6f with the rest o
1e942 66 20 74 68 65 20 73 63 68 65 6d 61 20 77 68 65 f the schema whe
1e943 6e 20 69 74 20 69 73 20 72 65 71 75 69 72 65 64 n it is required
1e944 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 6c 74 68 .. **. ** Alth
1e945 6f 75 67 68 20 74 68 65 20 6d 75 74 65 78 20 6f ough the mutex o
1e946 6e 20 74 68 65 20 42 74 53 68 61 72 65 64 20 6f n the BtShared o
1e947 62 6a 65 63 74 20 74 68 61 74 20 63 6f 72 72 65 bject that corre
1e948 73 70 6f 6e 64 73 20 74 6f 0a 20 20 2a 2a 20 64 sponds to. ** d
1e949 61 74 61 62 61 73 65 20 75 2e 62 75 2e 69 44 62 atabase u.bu.iDb
1e94a 20 28 74 68 65 20 64 61 74 61 62 61 73 65 20 63 (the database c
1e94b 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 73 71 ontaining the sq
1e94c 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c lite_master tabl
1e94d 65 0a 20 20 2a 2a 20 72 65 61 64 20 62 79 20 74 e. ** read by t
1e94e 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 29 his instruction)
1e94f 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 68 65 is currently he
1e950 6c 64 2c 20 69 74 20 69 73 20 6e 65 63 65 73 73 ld, it is necess
1e951 61 72 79 20 74 6f 0a 20 20 2a 2a 20 6f 62 74 61 ary to. ** obta
1e952 69 6e 20 74 68 65 20 6d 75 74 65 78 65 73 20 6f in the mutexes o
1e953 6e 20 61 6c 6c 20 61 74 74 61 63 68 65 64 20 64 n all attached d
1e954 61 74 61 62 61 73 65 73 20 62 65 66 6f 72 65 20 atabases before
1e955 63 68 65 63 6b 69 6e 67 20 69 66 0a 20 20 2a 2a checking if. **
1e956 20 74 68 65 20 73 63 68 65 6d 61 20 6f 66 20 75 the schema of u
1e957 2e 62 75 2e 69 44 62 20 69 73 20 6c 6f 61 64 65 .bu.iDb is loade
1e958 64 2e 20 54 68 69 73 20 69 73 20 62 65 63 61 75 d. This is becau
1e959 73 65 2c 20 61 74 20 74 68 65 20 73 74 61 72 74 se, at the start
1e95a 20 6f 66 0a 20 20 2a 2a 20 74 68 65 20 73 71 6c of. ** the sql
1e95b 69 74 65 33 5f 65 78 65 63 28 29 20 63 61 6c 6c ite3_exec() call
1e95c 20 62 65 6c 6f 77 2c 20 53 51 4c 69 74 65 20 77 below, SQLite w
1e95d 69 6c 6c 20 69 6e 76 6f 6b 65 0a 20 20 2a 2a 20 ill invoke. **
1e95e 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 sqlite3BtreeEnte
1e95f 72 41 6c 6c 28 29 2e 20 49 66 20 61 6c 6c 20 6d rAll(). If all m
1e960 75 74 65 78 65 73 20 61 72 65 20 6e 6f 74 20 61 utexes are not a
1e961 6c 72 65 61 64 79 20 68 65 6c 64 2c 20 74 68 65 lready held, the
1e962 0a 20 20 2a 2a 20 75 2e 62 75 2e 69 44 62 20 6d . ** u.bu.iDb m
1e963 75 74 65 78 20 6d 61 79 20 62 65 20 74 65 6d 70 utex may be temp
1e964 6f 72 61 72 69 6c 79 20 72 65 6c 65 61 73 65 64 orarily released
1e965 20 74 6f 20 61 76 6f 69 64 20 64 65 61 64 6c 6f to avoid deadlo
1e966 63 6b 2e 20 49 66 0a 20 20 2a 2a 20 74 68 69 73 ck. If. ** this
1e967 20 68 61 70 70 65 6e 73 2c 20 74 68 65 6e 20 73 happens, then s
1e968 6f 6d 65 20 6f 74 68 65 72 20 74 68 72 65 61 64 ome other thread
1e969 20 6d 61 79 20 64 65 6c 65 74 65 20 74 68 65 20 may delete the
1e96a 69 6e 2d 6d 65 6d 6f 72 79 0a 20 20 2a 2a 20 73 in-memory. ** s
1e96b 63 68 65 6d 61 20 6f 66 20 64 61 74 61 62 61 73 chema of databas
1e96c 65 20 75 2e 62 75 2e 69 44 62 20 62 65 66 6f 72 e u.bu.iDb befor
1e96d 65 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d e the SQL statem
1e96e 65 6e 74 20 72 75 6e 73 2e 20 54 68 65 20 73 63 ent runs. The sc
1e96f 68 65 6d 61 0a 20 20 2a 2a 20 77 69 6c 6c 20 6e hema. ** will n
1e970 6f 74 20 62 65 20 72 65 6c 6f 61 64 65 64 20 62 ot be reloaded b
1e971 65 63 75 61 73 65 20 74 68 65 20 64 62 2d 3e 69 ecuase the db->i
1e972 6e 69 74 2e 62 75 73 79 20 66 6c 61 67 20 69 73 nit.busy flag is
1e973 20 73 65 74 2e 20 54 68 69 73 0a 20 20 2a 2a 20 set. This. **
1e974 63 61 6e 20 72 65 73 75 6c 74 20 69 6e 20 61 20 can result in a
1e975 22 6e 6f 20 73 75 63 68 20 74 61 62 6c 65 3a 20 "no such table:
1e976 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 22 20 6f sqlite_master" o
1e977 72 20 22 6d 61 6c 66 6f 72 6d 65 64 0a 20 20 2a r "malformed. *
1e978 2a 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d * database schem
1e979 61 22 20 65 72 72 6f 72 20 62 65 69 6e 67 20 72 a" error being r
1e97a 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20 75 eturned to the u
1e97b 73 65 72 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 ser.. */. asse
1e97c 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 rt( sqlite3Btree
1e97d 48 6f 6c 64 73 4d 75 74 65 78 28 64 62 2d 3e 61 HoldsMutex(db->a
1e97e 44 62 5b 75 2e 62 75 2e 69 44 62 5d 2e 70 42 74 Db[u.bu.iDb].pBt
1e97f 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 ) );. sqlite3Bt
1e980 72 65 65 45 6e 74 65 72 41 6c 6c 28 64 62 29 3b reeEnterAll(db);
1e981 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 32 20 7c . if( pOp->p2 |
1e982 7c 20 44 62 48 61 73 50 72 6f 70 65 72 74 79 28 | DbHasProperty(
1e983 64 62 2c 20 75 2e 62 75 2e 69 44 62 2c 20 44 42 db, u.bu.iDb, DB
1e984 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64 29 20 29 _SchemaLoaded) )
1e985 7b 0a 20 20 20 20 75 2e 62 75 2e 7a 4d 61 73 74 {. u.bu.zMast
1e986 65 72 20 3d 20 53 43 48 45 4d 41 5f 54 41 42 4c er = SCHEMA_TABL
1e987 45 28 75 2e 62 75 2e 69 44 62 29 3b 0a 20 20 20 E(u.bu.iDb);.
1e988 20 75 2e 62 75 2e 69 6e 69 74 44 61 74 61 2e 64 u.bu.initData.d
1e989 62 20 3d 20 64 62 3b 0a 20 20 20 20 75 2e 62 75 b = db;. u.bu
1e98a 2e 69 6e 69 74 44 61 74 61 2e 69 44 62 20 3d 20 .initData.iDb =
1e98b 70 4f 70 2d 3e 70 31 3b 0a 20 20 20 20 75 2e 62 pOp->p1;. u.b
1e98c 75 2e 69 6e 69 74 44 61 74 61 2e 70 7a 45 72 72 u.initData.pzErr
1e98d 4d 73 67 20 3d 20 26 70 2d 3e 7a 45 72 72 4d 73 Msg = &p->zErrMs
1e98e 67 3b 0a 20 20 20 20 75 2e 62 75 2e 7a 53 71 6c g;. u.bu.zSql
1e98f 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 = sqlite3MPrint
1e990 66 28 64 62 2c 0a 20 20 20 20 20 20 20 22 53 45 f(db,. "SE
1e991 4c 45 43 54 20 6e 61 6d 65 2c 20 72 6f 6f 74 70 LECT name, rootp
1e992 61 67 65 2c 20 73 71 6c 20 46 52 4f 4d 20 27 25 age, sql FROM '%
1e993 71 27 2e 25 73 20 57 48 45 52 45 20 25 73 22 2c q'.%s WHERE %s",
1e994 0a 20 20 20 20 20 20 20 64 62 2d 3e 61 44 62 5b . db->aDb[
1e995 75 2e 62 75 2e 69 44 62 5d 2e 7a 4e 61 6d 65 2c u.bu.iDb].zName,
1e996 20 75 2e 62 75 2e 7a 4d 61 73 74 65 72 2c 20 70 u.bu.zMaster, p
1e997 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 20 20 69 Op->p4.z);. i
1e998 66 28 20 75 2e 62 75 2e 7a 53 71 6c 3d 3d 30 20 f( u.bu.zSql==0
1e999 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 ){. rc = SQ
1e99a 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 LITE_NOMEM;.
1e99b 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 28 76 6f }else{. (vo
1e99c 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 id)sqlite3Safety
1e99d 4f 66 66 28 64 62 29 3b 0a 20 20 20 20 20 20 61 Off(db);. a
1e99e 73 73 65 72 74 28 20 64 62 2d 3e 69 6e 69 74 2e ssert( db->init.
1e99f 62 75 73 79 3d 3d 30 20 29 3b 0a 20 20 20 20 20 busy==0 );.
1e9a0 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 3d db->init.busy =
1e9a1 20 31 3b 0a 20 20 20 20 20 20 75 2e 62 75 2e 69 1;. u.bu.i
1e9a2 6e 69 74 44 61 74 61 2e 72 63 20 3d 20 53 51 4c nitData.rc = SQL
1e9a3 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 61 73 ITE_OK;. as
1e9a4 73 65 72 74 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f sert( !db->mallo
1e9a5 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 20 20 20 cFailed );.
1e9a6 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 rc = sqlite3_ex
1e9a7 65 63 28 64 62 2c 20 75 2e 62 75 2e 7a 53 71 6c ec(db, u.bu.zSql
1e9a8 2c 20 73 71 6c 69 74 65 33 49 6e 69 74 43 61 6c , sqlite3InitCal
1e9a9 6c 62 61 63 6b 2c 20 26 75 2e 62 75 2e 69 6e 69 lback, &u.bu.ini
1e9aa 74 44 61 74 61 2c 20 30 29 3b 0a 20 20 20 20 20 tData, 0);.
1e9ab 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
1e9ac 4f 4b 20 29 20 72 63 20 3d 20 75 2e 62 75 2e 69 OK ) rc = u.bu.i
1e9ad 6e 69 74 44 61 74 61 2e 72 63 3b 0a 20 20 20 20 nitData.rc;.
1e9ae 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
1e9af 64 62 2c 20 75 2e 62 75 2e 7a 53 71 6c 29 3b 0a db, u.bu.zSql);.
1e9b0 20 20 20 20 20 20 64 62 2d 3e 69 6e 69 74 2e 62 db->init.b
1e9b1 75 73 79 20 3d 20 30 3b 0a 20 20 20 20 20 20 28 usy = 0;. (
1e9b2 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 void)sqlite3Safe
1e9b3 74 79 4f 6e 28 64 62 29 3b 0a 20 20 20 20 7d 0a tyOn(db);. }.
1e9b4 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 72 }. sqlite3Btr
1e9b5 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b 0a eeLeaveAll(db);.
1e9b6 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
1e9b7 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 67 6f _NOMEM ){. go
1e9b8 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 to no_mem;. }.
1e9b9 20 62 72 65 61 6b 3b 0a 7d 0a 0a 23 69 66 20 21 break;.}..#if !
1e9ba 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f defined(SQLITE_O
1e9bb 4d 49 54 5f 41 4e 41 4c 59 5a 45 29 0a 2f 2a 20 MIT_ANALYZE)./*
1e9bc 4f 70 63 6f 64 65 3a 20 4c 6f 61 64 41 6e 61 6c Opcode: LoadAnal
1e9bd 79 73 69 73 20 50 31 20 2a 20 2a 20 2a 20 2a 0a ysis P1 * * * *.
1e9be 2a 2a 0a 2a 2a 20 52 65 61 64 20 74 68 65 20 73 **.** Read the s
1e9bf 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c qlite_stat1 tabl
1e9c0 65 20 66 6f 72 20 64 61 74 61 62 61 73 65 20 50 e for database P
1e9c1 31 20 61 6e 64 20 6c 6f 61 64 20 74 68 65 20 63 1 and load the c
1e9c2 6f 6e 74 65 6e 74 0a 2a 2a 20 6f 66 20 74 68 61 ontent.** of tha
1e9c3 74 20 74 61 62 6c 65 20 69 6e 74 6f 20 74 68 65 t table into the
1e9c4 20 69 6e 74 65 72 6e 61 6c 20 69 6e 64 65 78 20 internal index
1e9c5 68 61 73 68 20 74 61 62 6c 65 2e 20 20 54 68 69 hash table. Thi
1e9c6 73 20 77 69 6c 6c 20 63 61 75 73 65 0a 2a 2a 20 s will cause.**
1e9c7 74 68 65 20 61 6e 61 6c 79 73 69 73 20 74 6f 20 the analysis to
1e9c8 62 65 20 75 73 65 64 20 77 68 65 6e 20 70 72 65 be used when pre
1e9c9 70 61 72 69 6e 67 20 61 6c 6c 20 73 75 62 73 65 paring all subse
1e9ca 71 75 65 6e 74 20 71 75 65 72 69 65 73 2e 0a 2a quent queries..*
1e9cb 2f 0a 63 61 73 65 20 4f 50 5f 4c 6f 61 64 41 6e /.case OP_LoadAn
1e9cc 61 6c 79 73 69 73 3a 20 7b 0a 20 20 61 73 73 65 alysis: {. asse
1e9cd 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 rt( pOp->p1>=0 &
1e9ce 26 20 70 4f 70 2d 3e 70 31 3c 64 62 2d 3e 6e 44 & pOp->p1<db->nD
1e9cf 62 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 b );. rc = sqli
1e9d0 74 65 33 41 6e 61 6c 79 73 69 73 4c 6f 61 64 28 te3AnalysisLoad(
1e9d1 64 62 2c 20 70 4f 70 2d 3e 70 31 29 3b 0a 20 20 db, pOp->p1);.
1e9d2 62 72 65 61 6b 3b 20 20 0a 7d 0a 23 65 6e 64 69 break; .}.#endi
1e9d3 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51 f /* !defined(SQ
1e9d4 4c 49 54 45 5f 4f 4d 49 54 5f 41 4e 41 4c 59 5a LITE_OMIT_ANALYZ
1e9d5 45 29 20 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f 64 65 E) */../* Opcode
1e9d6 3a 20 44 72 6f 70 54 61 62 6c 65 20 50 31 20 2a : DropTable P1 *
1e9d7 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 * P4 *.**.** Re
1e9d8 6d 6f 76 65 20 74 68 65 20 69 6e 74 65 72 6e 61 move the interna
1e9d9 6c 20 28 69 6e 2d 6d 65 6d 6f 72 79 29 20 64 61 l (in-memory) da
1e9da 74 61 20 73 74 72 75 63 74 75 72 65 73 20 74 68 ta structures th
1e9db 61 74 20 64 65 73 63 72 69 62 65 0a 2a 2a 20 74 at describe.** t
1e9dc 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 64 20 50 he table named P
1e9dd 34 20 69 6e 20 64 61 74 61 62 61 73 65 20 50 31 4 in database P1
1e9de 2e 20 20 54 68 69 73 20 69 73 20 63 61 6c 6c 65 . This is calle
1e9df 64 20 61 66 74 65 72 20 61 20 74 61 62 6c 65 0a d after a table.
1e9e0 2a 2a 20 69 73 20 64 72 6f 70 70 65 64 20 69 6e ** is dropped in
1e9e1 20 6f 72 64 65 72 20 74 6f 20 6b 65 65 70 20 74 order to keep t
1e9e2 68 65 20 69 6e 74 65 72 6e 61 6c 20 72 65 70 72 he internal repr
1e9e3 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 esentation of th
1e9e4 65 0a 2a 2a 20 73 63 68 65 6d 61 20 63 6f 6e 73 e.** schema cons
1e9e5 69 73 74 65 6e 74 20 77 69 74 68 20 77 68 61 74 istent with what
1e9e6 20 69 73 20 6f 6e 20 64 69 73 6b 2e 0a 2a 2f 0a is on disk..*/.
1e9e7 63 61 73 65 20 4f 50 5f 44 72 6f 70 54 61 62 6c case OP_DropTabl
1e9e8 65 3a 20 7b 0a 20 20 73 71 6c 69 74 65 33 55 6e e: {. sqlite3Un
1e9e9 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 54 61 62 linkAndDeleteTab
1e9ea 6c 65 28 64 62 2c 20 70 4f 70 2d 3e 70 31 2c 20 le(db, pOp->p1,
1e9eb 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 62 72 pOp->p4.z);. br
1e9ec 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1e9ed 65 3a 20 44 72 6f 70 49 6e 64 65 78 20 50 31 20 e: DropIndex P1
1e9ee 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 52 * * P4 *.**.** R
1e9ef 65 6d 6f 76 65 20 74 68 65 20 69 6e 74 65 72 6e emove the intern
1e9f0 61 6c 20 28 69 6e 2d 6d 65 6d 6f 72 79 29 20 64 al (in-memory) d
1e9f1 61 74 61 20 73 74 72 75 63 74 75 72 65 73 20 74 ata structures t
1e9f2 68 61 74 20 64 65 73 63 72 69 62 65 0a 2a 2a 20 hat describe.**
1e9f3 74 68 65 20 69 6e 64 65 78 20 6e 61 6d 65 64 20 the index named
1e9f4 50 34 20 69 6e 20 64 61 74 61 62 61 73 65 20 50 P4 in database P
1e9f5 31 2e 20 20 54 68 69 73 20 69 73 20 63 61 6c 6c 1. This is call
1e9f6 65 64 20 61 66 74 65 72 20 61 6e 20 69 6e 64 65 ed after an inde
1e9f7 78 0a 2a 2a 20 69 73 20 64 72 6f 70 70 65 64 20 x.** is dropped
1e9f8 69 6e 20 6f 72 64 65 72 20 74 6f 20 6b 65 65 70 in order to keep
1e9f9 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 72 65 the internal re
1e9fa 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 presentation of
1e9fb 74 68 65 0a 2a 2a 20 73 63 68 65 6d 61 20 63 6f the.** schema co
1e9fc 6e 73 69 73 74 65 6e 74 20 77 69 74 68 20 77 68 nsistent with wh
1e9fd 61 74 20 69 73 20 6f 6e 20 64 69 73 6b 2e 0a 2a at is on disk..*
1e9fe 2f 0a 63 61 73 65 20 4f 50 5f 44 72 6f 70 49 6e /.case OP_DropIn
1e9ff 64 65 78 3a 20 7b 0a 20 20 73 71 6c 69 74 65 33 dex: {. sqlite3
1ea00 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 49 UnlinkAndDeleteI
1ea01 6e 64 65 78 28 64 62 2c 20 70 4f 70 2d 3e 70 31 ndex(db, pOp->p1
1ea02 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 , pOp->p4.z);.
1ea03 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 break;.}../* Opc
1ea04 6f 64 65 3a 20 44 72 6f 70 54 72 69 67 67 65 72 ode: DropTrigger
1ea05 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a P1 * * P4 *.**.
1ea06 2a 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 69 6e ** Remove the in
1ea07 74 65 72 6e 61 6c 20 28 69 6e 2d 6d 65 6d 6f 72 ternal (in-memor
1ea08 79 29 20 64 61 74 61 20 73 74 72 75 63 74 75 72 y) data structur
1ea09 65 73 20 74 68 61 74 20 64 65 73 63 72 69 62 65 es that describe
1ea0a 0a 2a 2a 20 74 68 65 20 74 72 69 67 67 65 72 20 .** the trigger
1ea0b 6e 61 6d 65 64 20 50 34 20 69 6e 20 64 61 74 61 named P4 in data
1ea0c 62 61 73 65 20 50 31 2e 20 20 54 68 69 73 20 69 base P1. This i
1ea0d 73 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20 61 s called after a
1ea0e 20 74 72 69 67 67 65 72 0a 2a 2a 20 69 73 20 64 trigger.** is d
1ea0f 72 6f 70 70 65 64 20 69 6e 20 6f 72 64 65 72 20 ropped in order
1ea10 74 6f 20 6b 65 65 70 20 74 68 65 20 69 6e 74 65 to keep the inte
1ea11 72 6e 61 6c 20 72 65 70 72 65 73 65 6e 74 61 74 rnal representat
1ea12 69 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 73 63 ion of the.** sc
1ea13 68 65 6d 61 20 63 6f 6e 73 69 73 74 65 6e 74 20 hema consistent
1ea14 77 69 74 68 20 77 68 61 74 20 69 73 20 6f 6e 20 with what is on
1ea15 64 69 73 6b 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 disk..*/.case OP
1ea16 5f 44 72 6f 70 54 72 69 67 67 65 72 3a 20 7b 0a _DropTrigger: {.
1ea17 20 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 sqlite3UnlinkA
1ea18 6e 64 44 65 6c 65 74 65 54 72 69 67 67 65 72 28 ndDeleteTrigger(
1ea19 64 62 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 4f 70 db, pOp->p1, pOp
1ea1a 2d 3e 70 34 2e 7a 29 3b 0a 20 20 62 72 65 61 6b ->p4.z);. break
1ea1b 3b 0a 7d 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 ;.}...#ifndef SQ
1ea1c 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 LITE_OMIT_INTEGR
1ea1d 49 54 59 5f 43 48 45 43 4b 0a 2f 2a 20 4f 70 63 ITY_CHECK./* Opc
1ea1e 6f 64 65 3a 20 49 6e 74 65 67 72 69 74 79 43 6b ode: IntegrityCk
1ea1f 20 50 31 20 50 32 20 50 33 20 2a 20 50 35 0a 2a P1 P2 P3 * P5.*
1ea20 2a 0a 2a 2a 20 44 6f 20 61 6e 20 61 6e 61 6c 79 *.** Do an analy
1ea21 73 69 73 20 6f 66 20 74 68 65 20 63 75 72 72 65 sis of the curre
1ea22 6e 74 6c 79 20 6f 70 65 6e 20 64 61 74 61 62 61 ntly open databa
1ea23 73 65 2e 20 20 53 74 6f 72 65 20 69 6e 0a 2a 2a se. Store in.**
1ea24 20 72 65 67 69 73 74 65 72 20 50 31 20 74 68 65 register P1 the
1ea25 20 74 65 78 74 20 6f 66 20 61 6e 20 65 72 72 6f text of an erro
1ea26 72 20 6d 65 73 73 61 67 65 20 64 65 73 63 72 69 r message descri
1ea27 62 69 6e 67 20 61 6e 79 20 70 72 6f 62 6c 65 6d bing any problem
1ea28 73 2e 0a 2a 2a 20 49 66 20 6e 6f 20 70 72 6f 62 s..** If no prob
1ea29 6c 65 6d 73 20 61 72 65 20 66 6f 75 6e 64 2c 20 lems are found,
1ea2a 73 74 6f 72 65 20 61 20 4e 55 4c 4c 20 69 6e 20 store a NULL in
1ea2b 72 65 67 69 73 74 65 72 20 50 31 2e 0a 2a 2a 0a register P1..**.
1ea2c 2a 2a 20 54 68 65 20 72 65 67 69 73 74 65 72 20 ** The register
1ea2d 50 33 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 P3 contains the
1ea2e 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f maximum number o
1ea2f 66 20 61 6c 6c 6f 77 65 64 20 65 72 72 6f 72 73 f allowed errors
1ea30 2e 0a 2a 2a 20 41 74 20 6d 6f 73 74 20 72 65 67 ..** At most reg
1ea31 28 50 33 29 20 65 72 72 6f 72 73 20 77 69 6c 6c (P3) errors will
1ea32 20 62 65 20 72 65 70 6f 72 74 65 64 2e 0a 2a 2a be reported..**
1ea33 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c In other words,
1ea34 20 74 68 65 20 61 6e 61 6c 79 73 69 73 20 73 74 the analysis st
1ea35 6f 70 73 20 61 73 20 73 6f 6f 6e 20 61 73 20 72 ops as soon as r
1ea36 65 67 28 50 31 29 20 65 72 72 6f 72 73 20 61 72 eg(P1) errors ar
1ea37 65 20 0a 2a 2a 20 73 65 65 6e 2e 20 20 52 65 67 e .** seen. Reg
1ea38 28 50 31 29 20 69 73 20 75 70 64 61 74 65 64 20 (P1) is updated
1ea39 77 69 74 68 20 74 68 65 20 6e 75 6d 62 65 72 20 with the number
1ea3a 6f 66 20 65 72 72 6f 72 73 20 72 65 6d 61 69 6e of errors remain
1ea3b 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 ing..**.** The r
1ea3c 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72 73 oot page numbers
1ea3d 20 6f 66 20 61 6c 6c 20 74 61 62 6c 65 73 20 69 of all tables i
1ea3e 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61 n the database a
1ea3f 72 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20 73 74 re integer.** st
1ea40 6f 72 65 64 20 69 6e 20 72 65 67 28 50 31 29 2c ored in reg(P1),
1ea41 20 72 65 67 28 50 31 2b 31 29 2c 20 72 65 67 28 reg(P1+1), reg(
1ea42 50 31 2b 32 29 2c 20 2e 2e 2e 2e 20 20 54 68 65 P1+2), .... The
1ea43 72 65 20 61 72 65 20 50 32 20 74 61 62 6c 65 73 re are P2 tables
1ea44 0a 2a 2a 20 74 6f 74 61 6c 2e 0a 2a 2a 0a 2a 2a .** total..**.**
1ea45 20 49 66 20 50 35 20 69 73 20 6e 6f 74 20 7a 65 If P5 is not ze
1ea46 72 6f 2c 20 74 68 65 20 63 68 65 63 6b 20 69 73 ro, the check is
1ea47 20 64 6f 6e 65 20 6f 6e 20 74 68 65 20 61 75 78 done on the aux
1ea48 69 6c 69 61 72 79 20 64 61 74 61 62 61 73 65 0a iliary database.
1ea49 2a 2a 20 66 69 6c 65 2c 20 6e 6f 74 20 74 68 65 ** file, not the
1ea4a 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 main database f
1ea4b 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ile..**.** This
1ea4c 6f 70 63 6f 64 65 20 69 73 20 75 73 65 64 20 74 opcode is used t
1ea4d 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 o implement the
1ea4e 69 6e 74 65 67 72 69 74 79 5f 63 68 65 63 6b 20 integrity_check
1ea4f 70 72 61 67 6d 61 2e 0a 2a 2f 0a 63 61 73 65 20 pragma..*/.case
1ea50 4f 50 5f 49 6e 74 65 67 72 69 74 79 43 6b 3a 20 OP_IntegrityCk:
1ea51 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 {.#if 0 /* loca
1ea52 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 l variables move
1ea53 64 20 69 6e 74 6f 20 75 2e 62 76 20 2a 2f 0a 20 d into u.bv */.
1ea54 20 69 6e 74 20 6e 52 6f 6f 74 3b 20 20 20 20 20 int nRoot;
1ea55 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 61 /* Number of ta
1ea56 62 6c 65 73 20 74 6f 20 63 68 65 63 6b 2e 20 20 bles to check.
1ea57 28 4e 75 6d 62 65 72 20 6f 66 20 72 6f 6f 74 20 (Number of root
1ea58 70 61 67 65 73 2e 29 20 2a 2f 0a 20 20 69 6e 74 pages.) */. int
1ea59 20 2a 61 52 6f 6f 74 3b 20 20 20 20 20 2f 2a 20 *aRoot; /*
1ea5a 41 72 72 61 79 20 6f 66 20 72 6f 6f 74 70 61 67 Array of rootpag
1ea5b 65 20 6e 75 6d 62 65 72 73 20 66 6f 72 20 74 61 e numbers for ta
1ea5c 62 6c 65 73 20 74 6f 20 62 65 20 63 68 65 63 6b bles to be check
1ea5d 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6a 3b 20 20 ed */. int j;
1ea5e 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 /* Loop
1ea5f 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 counter */. int
1ea60 20 6e 45 72 72 3b 20 20 20 20 20 20 20 2f 2a 20 nErr; /*
1ea61 4e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73 Number of errors
1ea62 20 72 65 70 6f 72 74 65 64 20 2a 2f 0a 20 20 63 reported */. c
1ea63 68 61 72 20 2a 7a 3b 20 20 20 20 20 20 20 20 2f har *z; /
1ea64 2a 20 54 65 78 74 20 6f 66 20 74 68 65 20 65 72 * Text of the er
1ea65 72 6f 72 20 72 65 70 6f 72 74 20 2a 2f 0a 20 20 ror report */.
1ea66 4d 65 6d 20 2a 70 6e 45 72 72 3b 20 20 20 20 20 Mem *pnErr;
1ea67 2f 2a 20 52 65 67 69 73 74 65 72 20 6b 65 65 70 /* Register keep
1ea68 69 6e 67 20 74 72 61 63 6b 20 6f 66 20 65 72 72 ing track of err
1ea69 6f 72 73 20 72 65 6d 61 69 6e 69 6e 67 20 2a 2f ors remaining */
1ea6a 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c .#endif /* local
1ea6b 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1ea6c 20 69 6e 74 6f 20 75 2e 62 76 20 2a 2f 0a 0a 20 into u.bv */..
1ea6d 20 75 2e 62 76 2e 6e 52 6f 6f 74 20 3d 20 70 4f u.bv.nRoot = pO
1ea6e 70 2d 3e 70 32 3b 0a 20 20 61 73 73 65 72 74 28 p->p2;. assert(
1ea6f 20 75 2e 62 76 2e 6e 52 6f 6f 74 3e 30 20 29 3b u.bv.nRoot>0 );
1ea70 0a 20 20 75 2e 62 76 2e 61 52 6f 6f 74 20 3d 20 . u.bv.aRoot =
1ea71 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 sqlite3DbMallocR
1ea72 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28 69 6e aw(db, sizeof(in
1ea73 74 29 2a 28 75 2e 62 76 2e 6e 52 6f 6f 74 2b 31 t)*(u.bv.nRoot+1
1ea74 29 20 29 3b 0a 20 20 69 66 28 20 75 2e 62 76 2e ) );. if( u.bv.
1ea75 61 52 6f 6f 74 3d 3d 30 20 29 20 67 6f 74 6f 20 aRoot==0 ) goto
1ea76 6e 6f 5f 6d 65 6d 3b 0a 20 20 61 73 73 65 72 74 no_mem;. assert
1ea77 28 20 70 4f 70 2d 3e 70 33 3e 30 20 26 26 20 70 ( pOp->p3>0 && p
1ea78 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 Op->p3<=p->nMem
1ea79 29 3b 0a 20 20 75 2e 62 76 2e 70 6e 45 72 72 20 );. u.bv.pnErr
1ea7a 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e = &p->aMem[pOp->
1ea7b 70 33 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 28 p3];. assert( (
1ea7c 75 2e 62 76 2e 70 6e 45 72 72 2d 3e 66 6c 61 67 u.bv.pnErr->flag
1ea7d 73 20 26 20 4d 45 4d 5f 49 6e 74 29 21 3d 30 20 s & MEM_Int)!=0
1ea7e 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 75 2e );. assert( (u.
1ea7f 62 76 2e 70 6e 45 72 72 2d 3e 66 6c 61 67 73 20 bv.pnErr->flags
1ea80 26 20 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 & (MEM_Str|MEM_B
1ea81 6c 6f 62 29 29 3d 3d 30 20 29 3b 0a 20 20 70 49 lob))==0 );. pI
1ea82 6e 31 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f n1 = &p->aMem[pO
1ea83 70 2d 3e 70 31 5d 3b 0a 20 20 66 6f 72 28 75 2e p->p1];. for(u.
1ea84 62 76 2e 6a 3d 30 3b 20 75 2e 62 76 2e 6a 3c 75 bv.j=0; u.bv.j<u
1ea85 2e 62 76 2e 6e 52 6f 6f 74 3b 20 75 2e 62 76 2e .bv.nRoot; u.bv.
1ea86 6a 2b 2b 29 7b 0a 20 20 20 20 75 2e 62 76 2e 61 j++){. u.bv.a
1ea87 52 6f 6f 74 5b 75 2e 62 76 2e 6a 5d 20 3d 20 28 Root[u.bv.j] = (
1ea88 69 6e 74 29 73 71 6c 69 74 65 33 56 64 62 65 49 int)sqlite3VdbeI
1ea89 6e 74 56 61 6c 75 65 28 26 70 49 6e 31 5b 75 2e ntValue(&pIn1[u.
1ea8a 62 76 2e 6a 5d 29 3b 0a 20 20 7d 0a 20 20 75 2e bv.j]);. }. u.
1ea8b 62 76 2e 61 52 6f 6f 74 5b 75 2e 62 76 2e 6a 5d bv.aRoot[u.bv.j]
1ea8c 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 = 0;. assert(
1ea8d 70 4f 70 2d 3e 70 35 3c 64 62 2d 3e 6e 44 62 20 pOp->p5<db->nDb
1ea8e 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 2d );. assert( (p-
1ea8f 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28 31 3c >btreeMask & (1<
1ea90 3c 70 4f 70 2d 3e 70 35 29 29 21 3d 30 20 29 3b <pOp->p5))!=0 );
1ea91 0a 20 20 75 2e 62 76 2e 7a 20 3d 20 73 71 6c 69 . u.bv.z = sqli
1ea92 74 65 33 42 74 72 65 65 49 6e 74 65 67 72 69 74 te3BtreeIntegrit
1ea93 79 43 68 65 63 6b 28 64 62 2d 3e 61 44 62 5b 70 yCheck(db->aDb[p
1ea94 4f 70 2d 3e 70 35 5d 2e 70 42 74 2c 20 75 2e 62 Op->p5].pBt, u.b
1ea95 76 2e 61 52 6f 6f 74 2c 20 75 2e 62 76 2e 6e 52 v.aRoot, u.bv.nR
1ea96 6f 6f 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 oot,.
1ea97 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ea98 20 20 20 20 20 20 28 69 6e 74 29 75 2e 62 76 2e (int)u.bv.
1ea99 70 6e 45 72 72 2d 3e 75 2e 69 2c 20 26 75 2e 62 pnErr->u.i, &u.b
1ea9a 76 2e 6e 45 72 72 29 3b 0a 20 20 73 71 6c 69 74 v.nErr);. sqlit
1ea9b 65 33 44 62 46 72 65 65 28 64 62 2c 20 75 2e 62 e3DbFree(db, u.b
1ea9c 76 2e 61 52 6f 6f 74 29 3b 0a 20 20 75 2e 62 76 v.aRoot);. u.bv
1ea9d 2e 70 6e 45 72 72 2d 3e 75 2e 69 20 2d 3d 20 75 .pnErr->u.i -= u
1ea9e 2e 62 76 2e 6e 45 72 72 3b 0a 20 20 73 71 6c 69 .bv.nErr;. sqli
1ea9f 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c te3VdbeMemSetNul
1eaa0 6c 28 70 49 6e 31 29 3b 0a 20 20 69 66 28 20 75 l(pIn1);. if( u
1eaa1 2e 62 76 2e 6e 45 72 72 3d 3d 30 20 29 7b 0a 20 .bv.nErr==0 ){.
1eaa2 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 76 2e assert( u.bv.
1eaa3 7a 3d 3d 30 20 29 3b 0a 20 20 7d 65 6c 73 65 20 z==0 );. }else
1eaa4 69 66 28 20 75 2e 62 76 2e 7a 3d 3d 30 20 29 7b if( u.bv.z==0 ){
1eaa5 0a 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d . goto no_mem
1eaa6 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 ;. }else{. s
1eaa7 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 qlite3VdbeMemSet
1eaa8 53 74 72 28 70 49 6e 31 2c 20 75 2e 62 76 2e 7a Str(pIn1, u.bv.z
1eaa9 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 55 54 46 , -1, SQLITE_UTF
1eaaa 38 2c 20 73 71 6c 69 74 65 33 5f 66 72 65 65 29 8, sqlite3_free)
1eaab 3b 0a 20 20 7d 0a 20 20 55 50 44 41 54 45 5f 4d ;. }. UPDATE_M
1eaac 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 49 6e 31 AX_BLOBSIZE(pIn1
1eaad 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 );. sqlite3Vdbe
1eaae 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70 ChangeEncoding(p
1eaaf 49 6e 31 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a In1, encoding);.
1eab0 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 break;.}.#endi
1eab1 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 f /* SQLITE_OMIT
1eab2 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b _INTEGRITY_CHECK
1eab3 20 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 */../* Opcode:
1eab4 52 6f 77 53 65 74 41 64 64 20 50 31 20 50 32 20 RowSetAdd P1 P2
1eab5 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 6e 73 65 * * *.**.** Inse
1eab6 72 74 20 74 68 65 20 69 6e 74 65 67 65 72 20 76 rt the integer v
1eab7 61 6c 75 65 20 68 65 6c 64 20 62 79 20 72 65 67 alue held by reg
1eab8 69 73 74 65 72 20 50 32 20 69 6e 74 6f 20 61 20 ister P2 into a
1eab9 62 6f 6f 6c 65 61 6e 20 69 6e 64 65 78 0a 2a 2a boolean index.**
1eaba 20 68 65 6c 64 20 69 6e 20 72 65 67 69 73 74 65 held in registe
1eabb 72 20 50 31 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 61 r P1..**.** An a
1eabc 73 73 65 72 74 69 6f 6e 20 66 61 69 6c 73 20 69 ssertion fails i
1eabd 66 20 50 32 20 69 73 20 6e 6f 74 20 61 6e 20 69 f P2 is not an i
1eabe 6e 74 65 67 65 72 2e 0a 2a 2f 0a 63 61 73 65 20 nteger..*/.case
1eabf 4f 50 5f 52 6f 77 53 65 74 41 64 64 3a 20 7b 20 OP_RowSetAdd: {
1eac0 20 20 20 20 20 20 2f 2a 20 69 6e 32 20 2a 2f 0a /* in2 */.
1eac1 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 #if 0 /* local
1eac2 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 variables moved
1eac3 69 6e 74 6f 20 75 2e 62 77 20 2a 2f 0a 20 20 4d into u.bw */. M
1eac4 65 6d 20 2a 70 49 64 78 3b 0a 20 20 4d 65 6d 20 em *pIdx;. Mem
1eac5 2a 70 56 61 6c 3b 0a 23 65 6e 64 69 66 20 2f 2a *pVal;.#endif /*
1eac6 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 local variables
1eac7 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 77 moved into u.bw
1eac8 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4f */. assert( pO
1eac9 70 2d 3e 70 31 3e 30 20 26 26 20 70 4f 70 2d 3e p->p1>0 && pOp->
1eaca 70 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 p1<=p->nMem );.
1eacb 20 75 2e 62 77 2e 70 49 64 78 20 3d 20 26 70 2d u.bw.pIdx = &p-
1eacc 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a >aMem[pOp->p1];.
1eacd 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
1eace 32 3e 30 20 26 26 20 70 4f 70 2d 3e 70 32 3c 3d 2>0 && pOp->p2<=
1eacf 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 75 2e 62 p->nMem );. u.b
1ead0 77 2e 70 56 61 6c 20 3d 20 26 70 2d 3e 61 4d 65 w.pVal = &p->aMe
1ead1 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 61 73 m[pOp->p2];. as
1ead2 73 65 72 74 28 20 28 75 2e 62 77 2e 70 56 61 6c sert( (u.bw.pVal
1ead3 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e ->flags & MEM_In
1ead4 74 29 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 28 t)!=0 );. if( (
1ead5 75 2e 62 77 2e 70 49 64 78 2d 3e 66 6c 61 67 73 u.bw.pIdx->flags
1ead6 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d & MEM_RowSet)==
1ead7 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 0 ){. sqlite3
1ead8 56 64 62 65 4d 65 6d 53 65 74 52 6f 77 53 65 74 VdbeMemSetRowSet
1ead9 28 75 2e 62 77 2e 70 49 64 78 29 3b 0a 20 20 20 (u.bw.pIdx);.
1eada 20 69 66 28 20 28 75 2e 62 77 2e 70 49 64 78 2d if( (u.bw.pIdx-
1eadb 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 >flags & MEM_Row
1eadc 53 65 74 29 3d 3d 30 20 29 20 67 6f 74 6f 20 6e Set)==0 ) goto n
1eadd 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 20 73 71 6c o_mem;. }. sql
1eade 69 74 65 33 52 6f 77 53 65 74 49 6e 73 65 72 74 ite3RowSetInsert
1eadf 28 75 2e 62 77 2e 70 49 64 78 2d 3e 75 2e 70 52 (u.bw.pIdx->u.pR
1eae0 6f 77 53 65 74 2c 20 75 2e 62 77 2e 70 56 61 6c owSet, u.bw.pVal
1eae1 2d 3e 75 2e 69 29 3b 0a 20 20 62 72 65 61 6b 3b ->u.i);. break;
1eae2 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 .}../* Opcode: R
1eae3 6f 77 53 65 74 52 65 61 64 20 50 31 20 50 32 20 owSetRead P1 P2
1eae4 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 45 78 74 P3 * *.**.** Ext
1eae5 72 61 63 74 20 74 68 65 20 73 6d 61 6c 6c 65 73 ract the smalles
1eae6 74 20 76 61 6c 75 65 20 66 72 6f 6d 20 62 6f 6f t value from boo
1eae7 6c 65 61 6e 20 69 6e 64 65 78 20 50 31 20 61 6e lean index P1 an
1eae8 64 20 70 75 74 20 74 68 61 74 20 76 61 6c 75 65 d put that value
1eae9 20 69 6e 74 6f 0a 2a 2a 20 72 65 67 69 73 74 65 into.** registe
1eaea 72 20 50 33 2e 20 20 4f 72 2c 20 69 66 20 62 6f r P3. Or, if bo
1eaeb 6f 6c 65 61 6e 20 69 6e 64 65 78 20 50 31 20 69 olean index P1 i
1eaec 73 20 69 6e 69 74 69 61 6c 6c 79 20 65 6d 70 74 s initially empt
1eaed 79 2c 20 6c 65 61 76 65 20 50 33 0a 2a 2a 20 75 y, leave P3.** u
1eaee 6e 63 68 61 6e 67 65 64 20 61 6e 64 20 6a 75 6d nchanged and jum
1eaef 70 20 74 6f 20 69 6e 73 74 72 75 63 74 69 6f 6e p to instruction
1eaf0 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f P2..*/.case OP_
1eaf1 52 6f 77 53 65 74 52 65 61 64 3a 20 7b 20 20 20 RowSetRead: {
1eaf2 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 6f 75 74 /* jump, out
1eaf3 33 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 3 */.#if 0 /* l
1eaf4 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1eaf5 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 78 20 2a oved into u.bx *
1eaf6 2f 0a 20 20 4d 65 6d 20 2a 70 49 64 78 3b 0a 20 /. Mem *pIdx;.
1eaf7 20 69 36 34 20 76 61 6c 3b 0a 23 65 6e 64 69 66 i64 val;.#endif
1eaf8 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1eaf9 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1eafa 2e 62 78 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 .bx */. assert(
1eafb 20 70 4f 70 2d 3e 70 31 3e 30 20 26 26 20 70 4f pOp->p1>0 && pO
1eafc 70 2d 3e 70 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 p->p1<=p->nMem )
1eafd 3b 0a 20 20 43 48 45 43 4b 5f 46 4f 52 5f 49 4e ;. CHECK_FOR_IN
1eafe 54 45 52 52 55 50 54 3b 0a 20 20 75 2e 62 78 2e TERRUPT;. u.bx.
1eaff 70 49 64 78 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b pIdx = &p->aMem[
1eb00 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 70 4f 75 74 pOp->p1];. pOut
1eb01 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d = &p->aMem[pOp-
1eb02 3e 70 33 5d 3b 0a 20 20 69 66 28 20 28 75 2e 62 >p3];. if( (u.b
1eb03 78 2e 70 49 64 78 2d 3e 66 6c 61 67 73 20 26 20 x.pIdx->flags &
1eb04 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 0a 20 MEM_RowSet)==0.
1eb05 20 20 7c 7c 20 73 71 6c 69 74 65 33 52 6f 77 53 || sqlite3RowS
1eb06 65 74 4e 65 78 74 28 75 2e 62 78 2e 70 49 64 78 etNext(u.bx.pIdx
1eb07 2d 3e 75 2e 70 52 6f 77 53 65 74 2c 20 26 75 2e ->u.pRowSet, &u.
1eb08 62 78 2e 76 61 6c 29 3d 3d 30 0a 20 20 29 7b 0a bx.val)==0. ){.
1eb09 20 20 20 20 2f 2a 20 54 68 65 20 62 6f 6f 6c 65 /* The boole
1eb0a 61 6e 20 69 6e 64 65 78 20 69 73 20 65 6d 70 74 an index is empt
1eb0b 79 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 y */. sqlite3
1eb0c 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 75 VdbeMemSetNull(u
1eb0d 2e 62 78 2e 70 49 64 78 29 3b 0a 20 20 20 20 70 .bx.pIdx);. p
1eb0e 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b c = pOp->p2 - 1;
1eb0f 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a . }else{. /*
1eb10 20 41 20 76 61 6c 75 65 20 77 61 73 20 70 75 6c A value was pul
1eb11 6c 65 64 20 66 72 6f 6d 20 74 68 65 20 69 6e 64 led from the ind
1eb12 65 78 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 ex */. assert
1eb13 28 20 70 4f 70 2d 3e 70 33 3e 30 20 26 26 20 70 ( pOp->p3>0 && p
1eb14 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 Op->p3<=p->nMem
1eb15 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 );. sqlite3Vd
1eb16 62 65 4d 65 6d 53 65 74 49 6e 74 36 34 28 70 4f beMemSetInt64(pO
1eb17 75 74 2c 20 75 2e 62 78 2e 76 61 6c 29 3b 0a 20 ut, u.bx.val);.
1eb18 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f }. break;.}../
1eb19 2a 20 4f 70 63 6f 64 65 3a 20 52 6f 77 53 65 74 * Opcode: RowSet
1eb1a 54 65 73 74 20 50 31 20 50 32 20 50 33 20 50 34 Test P1 P2 P3 P4
1eb1b 0a 2a 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 .**.** Register
1eb1c 50 33 20 69 73 20 61 73 73 75 6d 65 64 20 74 6f P3 is assumed to
1eb1d 20 68 6f 6c 64 20 61 20 36 34 2d 62 69 74 20 69 hold a 64-bit i
1eb1e 6e 74 65 67 65 72 20 76 61 6c 75 65 2e 20 49 66 nteger value. If
1eb1f 20 72 65 67 69 73 74 65 72 20 50 31 0a 2a 2a 20 register P1.**
1eb20 63 6f 6e 74 61 69 6e 73 20 61 20 52 6f 77 53 65 contains a RowSe
1eb21 74 20 6f 62 6a 65 63 74 20 61 6e 64 20 74 68 61 t object and tha
1eb22 74 20 52 6f 77 53 65 74 20 6f 62 6a 65 63 74 20 t RowSet object
1eb23 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 74 68 65 20 contains.** the
1eb24 76 61 6c 75 65 20 68 65 6c 64 20 69 6e 20 50 33 value held in P3
1eb25 2c 20 6a 75 6d 70 20 74 6f 20 72 65 67 69 73 74 , jump to regist
1eb26 65 72 20 50 32 2e 20 4f 74 68 65 72 77 69 73 65 er P2. Otherwise
1eb27 2c 20 69 6e 73 65 72 74 20 74 68 65 0a 2a 2a 20 , insert the.**
1eb28 69 6e 74 65 67 65 72 20 69 6e 20 50 33 20 69 6e integer in P3 in
1eb29 74 6f 20 74 68 65 20 52 6f 77 53 65 74 20 61 6e to the RowSet an
1eb2a 64 20 63 6f 6e 74 69 6e 75 65 20 6f 6e 20 74 6f d continue on to
1eb2b 20 74 68 65 0a 2a 2a 20 6e 65 78 74 20 6f 70 63 the.** next opc
1eb2c 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 52 ode..**.** The R
1eb2d 6f 77 53 65 74 20 6f 62 6a 65 63 74 20 69 73 20 owSet object is
1eb2e 6f 70 74 69 6d 69 7a 65 64 20 66 6f 72 20 74 68 optimized for th
1eb2f 65 20 63 61 73 65 20 77 68 65 72 65 20 73 75 63 e case where suc
1eb30 63 65 73 73 69 76 65 20 73 65 74 73 0a 2a 2a 20 cessive sets.**
1eb31 6f 66 20 69 6e 74 65 67 65 72 73 2c 20 77 68 65 of integers, whe
1eb32 72 65 20 65 61 63 68 20 73 65 74 20 63 6f 6e 74 re each set cont
1eb33 61 69 6e 73 20 6e 6f 20 64 75 70 6c 69 63 61 74 ains no duplicat
1eb34 65 73 2e 20 45 61 63 68 20 73 65 74 0a 2a 2a 20 es. Each set.**
1eb35 6f 66 20 76 61 6c 75 65 73 20 69 73 20 69 64 65 of values is ide
1eb36 6e 74 69 66 69 65 64 20 62 79 20 61 20 75 6e 69 ntified by a uni
1eb37 71 75 65 20 50 34 20 76 61 6c 75 65 2e 20 54 68 que P4 value. Th
1eb38 65 20 66 69 72 73 74 20 73 65 74 0a 2a 2a 20 6d e first set.** m
1eb39 75 73 74 20 68 61 76 65 20 50 34 3d 3d 30 2c 20 ust have P4==0,
1eb3a 74 68 65 20 66 69 6e 61 6c 20 73 65 74 20 50 34 the final set P4
1eb3b 3d 2d 31 2e 20 20 50 34 20 6d 75 73 74 20 62 65 =-1. P4 must be
1eb3c 20 65 69 74 68 65 72 20 2d 31 20 6f 72 0a 2a 2a either -1 or.**
1eb3d 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2e 20 20 non-negative.
1eb3e 46 6f 72 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 For non-negative
1eb3f 20 76 61 6c 75 65 73 20 6f 66 20 50 34 20 6f 6e values of P4 on
1eb40 6c 79 20 74 68 65 20 6c 6f 77 65 72 20 34 0a 2a ly the lower 4.*
1eb41 2a 20 62 69 74 73 20 61 72 65 20 73 69 67 6e 69 * bits are signi
1eb42 66 69 63 61 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 ficant..**.** Th
1eb43 69 73 20 61 6c 6c 6f 77 73 20 6f 70 74 69 6d 69 is allows optimi
1eb44 7a 61 74 69 6f 6e 73 3a 20 28 61 29 20 77 68 65 zations: (a) whe
1eb45 6e 20 50 34 3d 3d 30 20 74 68 65 72 65 20 69 73 n P4==0 there is
1eb46 20 6e 6f 20 6e 65 65 64 20 74 6f 20 74 65 73 74 no need to test
1eb47 0a 2a 2a 20 74 68 65 20 72 6f 77 73 65 74 20 6f .** the rowset o
1eb48 62 6a 65 63 74 20 66 6f 72 20 50 33 2c 20 61 73 bject for P3, as
1eb49 20 69 74 20 69 73 20 67 75 61 72 61 6e 74 65 65 it is guarantee
1eb4a 64 20 6e 6f 74 20 74 6f 20 63 6f 6e 74 61 69 6e d not to contain
1eb4b 20 69 74 2c 0a 2a 2a 20 28 62 29 20 77 68 65 6e it,.** (b) when
1eb4c 20 50 34 3d 3d 2d 31 20 74 68 65 72 65 20 69 73 P4==-1 there is
1eb4d 20 6e 6f 20 6e 65 65 64 20 74 6f 20 69 6e 73 65 no need to inse
1eb4e 72 74 20 74 68 65 20 76 61 6c 75 65 2c 20 61 73 rt the value, as
1eb4f 20 69 74 20 77 69 6c 6c 0a 2a 2a 20 6e 65 76 65 it will.** neve
1eb50 72 20 62 65 20 74 65 73 74 65 64 20 66 6f 72 2c r be tested for,
1eb51 20 61 6e 64 20 28 63 29 20 77 68 65 6e 20 61 20 and (c) when a
1eb52 76 61 6c 75 65 20 74 68 61 74 20 69 73 20 70 61 value that is pa
1eb53 72 74 20 6f 66 20 73 65 74 20 58 20 69 73 0a 2a rt of set X is.*
1eb54 2a 20 69 6e 73 65 72 74 65 64 2c 20 74 68 65 72 * inserted, ther
1eb55 65 20 69 73 20 6e 6f 20 6e 65 65 64 20 74 6f 20 e is no need to
1eb56 73 65 61 72 63 68 20 74 6f 20 73 65 65 20 69 66 search to see if
1eb57 20 74 68 65 20 73 61 6d 65 20 76 61 6c 75 65 20 the same value
1eb58 77 61 73 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c was.** previousl
1eb59 79 20 69 6e 73 65 72 74 65 64 20 61 73 20 70 61 y inserted as pa
1eb5a 72 74 20 6f 66 20 73 65 74 20 58 20 28 6f 6e 6c rt of set X (onl
1eb5b 79 20 69 66 20 69 74 20 77 61 73 20 70 72 65 76 y if it was prev
1eb5c 69 6f 75 73 6c 79 0a 2a 2a 20 69 6e 73 65 72 74 iously.** insert
1eb5d 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 73 6f ed as part of so
1eb5e 6d 65 20 6f 74 68 65 72 20 73 65 74 29 2e 0a 2a me other set)..*
1eb5f 2f 0a 63 61 73 65 20 4f 50 5f 52 6f 77 53 65 74 /.case OP_RowSet
1eb60 54 65 73 74 3a 20 7b 20 20 20 20 20 20 20 20 20 Test: {
1eb61 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6a /* j
1eb62 75 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20 2a 2f ump, in1, in3 */
1eb63 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c .#if 0 /* local
1eb64 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1eb65 20 69 6e 74 6f 20 75 2e 62 79 20 2a 2f 0a 20 20 into u.by */.
1eb66 69 6e 74 20 69 53 65 74 3b 0a 20 20 69 6e 74 20 int iSet;. int
1eb67 65 78 69 73 74 73 3b 0a 23 65 6e 64 69 66 20 2f exists;.#endif /
1eb68 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 * local variable
1eb69 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 s moved into u.b
1eb6a 79 20 2a 2f 0a 0a 20 20 75 2e 62 79 2e 69 53 65 y */.. u.by.iSe
1eb6b 74 20 3d 20 70 4f 70 2d 3e 70 34 2e 69 3b 0a 20 t = pOp->p4.i;.
1eb6c 20 61 73 73 65 72 74 28 20 70 49 6e 33 2d 3e 66 assert( pIn3->f
1eb6d 6c 61 67 73 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a lags&MEM_Int );.
1eb6e 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 . /* If there i
1eb6f 73 20 61 6e 79 74 68 69 6e 67 20 6f 74 68 65 72 s anything other
1eb70 20 74 68 61 6e 20 61 20 72 6f 77 73 65 74 20 6f than a rowset o
1eb71 62 6a 65 63 74 20 69 6e 20 6d 65 6d 6f 72 79 20 bject in memory
1eb72 63 65 6c 6c 20 50 31 2c 0a 20 20 2a 2a 20 64 65 cell P1,. ** de
1eb73 6c 65 74 65 20 69 74 20 6e 6f 77 20 61 6e 64 20 lete it now and
1eb74 69 6e 69 74 69 61 6c 69 7a 65 20 50 31 20 77 69 initialize P1 wi
1eb75 74 68 20 61 6e 20 65 6d 70 74 79 20 72 6f 77 73 th an empty rows
1eb76 65 74 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 70 et. */. if( (p
1eb77 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d In1->flags & MEM
1eb78 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 7b 0a 20 _RowSet)==0 ){.
1eb79 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 sqlite3VdbeMe
1eb7a 6d 53 65 74 52 6f 77 53 65 74 28 70 49 6e 31 29 mSetRowSet(pIn1)
1eb7b 3b 0a 20 20 20 20 69 66 28 20 28 70 49 6e 31 2d ;. if( (pIn1-
1eb7c 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 >flags & MEM_Row
1eb7d 53 65 74 29 3d 3d 30 20 29 20 67 6f 74 6f 20 6e Set)==0 ) goto n
1eb7e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 0a 20 20 61 73 o_mem;. }.. as
1eb7f 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 sert( pOp->p4typ
1eb80 65 3d 3d 50 34 5f 49 4e 54 33 32 20 29 3b 0a 20 e==P4_INT32 );.
1eb81 20 61 73 73 65 72 74 28 20 75 2e 62 79 2e 69 53 assert( u.by.iS
1eb82 65 74 3d 3d 2d 31 20 7c 7c 20 75 2e 62 79 2e 69 et==-1 || u.by.i
1eb83 53 65 74 3e 3d 30 20 29 3b 0a 20 20 69 66 28 20 Set>=0 );. if(
1eb84 75 2e 62 79 2e 69 53 65 74 20 29 7b 0a 20 20 20 u.by.iSet ){.
1eb85 20 75 2e 62 79 2e 65 78 69 73 74 73 20 3d 20 73 u.by.exists = s
1eb86 71 6c 69 74 65 33 52 6f 77 53 65 74 54 65 73 74 qlite3RowSetTest
1eb87 28 70 49 6e 31 2d 3e 75 2e 70 52 6f 77 53 65 74 (pIn1->u.pRowSet
1eb88 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
1eb89 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1eb8a 20 28 75 38 29 28 75 2e 62 79 2e 69 53 65 74 3e (u8)(u.by.iSet>
1eb8b 3d 30 20 3f 20 75 2e 62 79 2e 69 53 65 74 20 26 =0 ? u.by.iSet &
1eb8c 20 30 78 66 20 3a 20 30 78 66 66 29 2c 0a 20 20 0xf : 0xff),.
1eb8d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1eb8e 20 20 20 20 20 20 20 20 20 20 20 20 20 70 49 6e pIn
1eb8f 33 2d 3e 75 2e 69 29 3b 0a 20 20 20 20 69 66 28 3->u.i);. if(
1eb90 20 75 2e 62 79 2e 65 78 69 73 74 73 20 29 7b 0a u.by.exists ){.
1eb91 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e pc = pOp->
1eb92 70 32 20 2d 20 31 3b 0a 20 20 20 20 20 20 62 72 p2 - 1;. br
1eb93 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 eak;. }. }.
1eb94 20 69 66 28 20 75 2e 62 79 2e 69 53 65 74 3e 3d if( u.by.iSet>=
1eb95 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 0 ){. sqlite3
1eb96 52 6f 77 53 65 74 49 6e 73 65 72 74 28 70 49 6e RowSetInsert(pIn
1eb97 31 2d 3e 75 2e 70 52 6f 77 53 65 74 2c 20 70 49 1->u.pRowSet, pI
1eb98 6e 33 2d 3e 75 2e 69 29 3b 0a 20 20 7d 0a 20 20 n3->u.i);. }.
1eb99 62 72 65 61 6b 3b 0a 7d 0a 0a 0a 23 69 66 6e 64 break;.}...#ifnd
1eb9a 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 ef SQLITE_OMIT_T
1eb9b 52 49 47 47 45 52 0a 0a 2f 2a 20 4f 70 63 6f 64 RIGGER../* Opcod
1eb9c 65 3a 20 50 72 6f 67 72 61 6d 20 50 31 20 50 32 e: Program P1 P2
1eb9d 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 45 P3 P4 *.**.** E
1eb9e 78 65 63 75 74 65 20 74 68 65 20 74 72 69 67 67 xecute the trigg
1eb9f 65 72 20 70 72 6f 67 72 61 6d 20 70 61 73 73 65 er program passe
1eba0 64 20 61 73 20 50 34 20 28 74 79 70 65 20 50 34 d as P4 (type P4
1eba1 5f 53 55 42 50 52 4f 47 52 41 4d 29 2e 20 0a 2a _SUBPROGRAM). .*
1eba2 2a 0a 2a 2a 20 50 31 20 63 6f 6e 74 61 69 6e 73 *.** P1 contains
1eba3 20 74 68 65 20 61 64 64 72 65 73 73 20 6f 66 20 the address of
1eba4 74 68 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 the memory cell
1eba5 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 that contains th
1eba6 65 20 66 69 72 73 74 20 6d 65 6d 6f 72 79 20 0a e first memory .
1eba7 2a 2a 20 63 65 6c 6c 20 69 6e 20 61 6e 20 61 72 ** cell in an ar
1eba8 72 61 79 20 6f 66 20 76 61 6c 75 65 73 20 75 73 ray of values us
1eba9 65 64 20 61 73 20 61 72 67 75 6d 65 6e 74 73 20 ed as arguments
1ebaa 74 6f 20 74 68 65 20 73 75 62 2d 70 72 6f 67 72 to the sub-progr
1ebab 61 6d 2e 20 50 32 20 0a 2a 2a 20 63 6f 6e 74 61 am. P2 .** conta
1ebac 69 6e 73 20 74 68 65 20 61 64 64 72 65 73 73 20 ins the address
1ebad 74 6f 20 6a 75 6d 70 20 74 6f 20 69 66 20 74 68 to jump to if th
1ebae 65 20 73 75 62 2d 70 72 6f 67 72 61 6d 20 74 68 e sub-program th
1ebaf 72 6f 77 73 20 61 6e 20 49 47 4e 4f 52 45 20 0a rows an IGNORE .
1ebb0 2a 2a 20 65 78 63 65 70 74 69 6f 6e 20 75 73 69 ** exception usi
1ebb1 6e 67 20 74 68 65 20 52 41 49 53 45 28 29 20 66 ng the RAISE() f
1ebb2 75 6e 63 74 69 6f 6e 2e 20 52 65 67 69 73 74 65 unction. Registe
1ebb3 72 20 50 33 20 63 6f 6e 74 61 69 6e 73 20 74 68 r P3 contains th
1ebb4 65 20 61 64 64 72 65 73 73 20 0a 2a 2a 20 6f 66 e address .** of
1ebb5 20 61 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 69 a memory cell i
1ebb6 6e 20 74 68 69 73 20 28 74 68 65 20 70 61 72 65 n this (the pare
1ebb7 6e 74 29 20 56 4d 20 74 68 61 74 20 69 73 20 75 nt) VM that is u
1ebb8 73 65 64 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 sed to allocate
1ebb9 74 68 65 20 0a 2a 2a 20 6d 65 6d 6f 72 79 20 72 the .** memory r
1ebba 65 71 75 69 72 65 64 20 62 79 20 74 68 65 20 73 equired by the s
1ebbb 75 62 2d 76 64 62 65 20 61 74 20 72 75 6e 74 69 ub-vdbe at runti
1ebbc 6d 65 2e 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 me..**.** P4 is
1ebbd 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
1ebbe 20 56 4d 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 VM containing t
1ebbf 68 65 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 he trigger progr
1ebc0 61 6d 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 50 am..*/.case OP_P
1ebc1 72 6f 67 72 61 6d 3a 20 7b 20 20 20 20 20 20 20 rogram: {
1ebc2 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 23 69 66 20 /* jump */.#if
1ebc3 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 0 /* local vari
1ebc4 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1ebc5 20 75 2e 62 7a 20 2a 2f 0a 20 20 69 6e 74 20 6e u.bz */. int n
1ebc6 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 Mem;
1ebc7 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
1ebc8 6d 65 6d 6f 72 79 20 72 65 67 69 73 74 65 72 73 memory registers
1ebc9 20 66 6f 72 20 73 75 62 2d 70 72 6f 67 72 61 6d for sub-program
1ebca 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b */. int nByte;
1ebcb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1ebcc 20 42 79 74 65 73 20 6f 66 20 72 75 6e 74 69 6d Bytes of runtim
1ebcd 65 20 73 70 61 63 65 20 72 65 71 75 69 72 65 64 e space required
1ebce 20 66 6f 72 20 73 75 62 2d 70 72 6f 67 72 61 6d for sub-program
1ebcf 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 52 74 3b 20 */. Mem *pRt;
1ebd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1ebd1 20 52 65 67 69 73 74 65 72 20 74 6f 20 61 6c 6c Register to all
1ebd2 6f 63 61 74 65 20 72 75 6e 74 69 6d 65 20 73 70 ocate runtime sp
1ebd3 61 63 65 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4d ace */. Mem *pM
1ebd4 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 em;
1ebd5 20 2f 2a 20 55 73 65 64 20 74 6f 20 69 74 65 72 /* Used to iter
1ebd6 61 74 65 20 74 68 72 6f 75 67 68 20 6d 65 6d 6f ate through memo
1ebd7 72 79 20 63 65 6c 6c 73 20 2a 2f 0a 20 20 4d 65 ry cells */. Me
1ebd8 6d 20 2a 70 45 6e 64 3b 20 20 20 20 20 20 20 20 m *pEnd;
1ebd9 20 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 6d 65 /* Last me
1ebda 6d 6f 72 79 20 63 65 6c 6c 20 69 6e 20 6e 65 77 mory cell in new
1ebdb 20 61 72 72 61 79 20 2a 2f 0a 20 20 56 64 62 65 array */. Vdbe
1ebdc 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 3b 20 20 Frame *pFrame;
1ebdd 20 20 20 20 2f 2a 20 4e 65 77 20 76 64 62 65 20 /* New vdbe
1ebde 66 72 61 6d 65 20 74 6f 20 65 78 65 63 75 74 65 frame to execute
1ebdf 20 69 6e 20 2a 2f 0a 20 20 53 75 62 50 72 6f 67 in */. SubProg
1ebe0 72 61 6d 20 2a 70 50 72 6f 67 72 61 6d 3b 20 20 ram *pProgram;
1ebe1 20 2f 2a 20 53 75 62 2d 70 72 6f 67 72 61 6d 20 /* Sub-program
1ebe2 74 6f 20 65 78 65 63 75 74 65 20 2a 2f 0a 20 20 to execute */.
1ebe3 76 6f 69 64 20 2a 74 3b 20 20 20 20 20 20 20 20 void *t;
1ebe4 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e /* Token
1ebe5 20 69 64 65 6e 74 69 66 79 69 6e 67 20 74 72 69 identifying tri
1ebe6 67 67 65 72 20 2a 2f 0a 23 65 6e 64 69 66 20 2f gger */.#endif /
1ebe7 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 * local variable
1ebe8 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 s moved into u.b
1ebe9 7a 20 2a 2f 0a 0a 20 20 75 2e 62 7a 2e 70 50 72 z */.. u.bz.pPr
1ebea 6f 67 72 61 6d 20 3d 20 70 4f 70 2d 3e 70 34 2e ogram = pOp->p4.
1ebeb 70 50 72 6f 67 72 61 6d 3b 0a 20 20 75 2e 62 7a pProgram;. u.bz
1ebec 2e 70 52 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b .pRt = &p->aMem[
1ebed 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 61 73 73 65 pOp->p3];. asse
1ebee 72 74 28 20 75 2e 62 7a 2e 70 50 72 6f 67 72 61 rt( u.bz.pProgra
1ebef 6d 2d 3e 6e 4f 70 3e 30 20 29 3b 0a 0a 20 20 2f m->nOp>0 );.. /
1ebf0 2a 20 49 66 20 74 68 65 20 70 35 20 66 6c 61 67 * If the p5 flag
1ebf1 20 69 73 20 63 6c 65 61 72 2c 20 74 68 65 6e 20 is clear, then
1ebf2 72 65 63 75 72 73 69 76 65 20 69 6e 76 6f 63 61 recursive invoca
1ebf3 74 69 6f 6e 20 6f 66 20 74 72 69 67 67 65 72 73 tion of triggers
1ebf4 20 69 73 0a 20 20 2a 2a 20 64 69 73 61 62 6c 65 is. ** disable
1ebf5 64 20 66 6f 72 20 62 61 63 6b 77 61 72 64 73 20 d for backwards
1ebf6 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 28 70 compatibility (p
1ebf7 35 20 69 73 20 73 65 74 20 69 66 20 74 68 69 73 5 is set if this
1ebf8 20 73 75 62 2d 70 72 6f 67 72 61 6d 0a 20 20 2a sub-program. *
1ebf9 2a 20 69 73 20 72 65 61 6c 6c 79 20 61 20 74 72 * is really a tr
1ebfa 69 67 67 65 72 2c 20 6e 6f 74 20 61 20 66 6f 72 igger, not a for
1ebfb 65 69 67 6e 20 6b 65 79 20 61 63 74 69 6f 6e 2c eign key action,
1ebfc 20 61 6e 64 20 74 68 65 20 66 6c 61 67 20 73 65 and the flag se
1ebfd 74 0a 20 20 2a 2a 20 61 6e 64 20 63 6c 65 61 72 t. ** and clear
1ebfe 65 64 20 62 79 20 74 68 65 20 22 50 52 41 47 4d ed by the "PRAGM
1ebff 41 20 72 65 63 75 72 73 69 76 65 5f 74 72 69 67 A recursive_trig
1ec00 67 65 72 73 22 20 63 6f 6d 6d 61 6e 64 20 69 73 gers" command is
1ec01 20 63 6c 65 61 72 29 2e 0a 20 20 2a 2a 0a 20 20 clear).. **.
1ec02 2a 2a 20 49 74 20 69 73 20 72 65 63 75 72 73 69 ** It is recursi
1ec03 76 65 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 ve invocation of
1ec04 20 74 72 69 67 67 65 72 73 2c 20 61 74 20 74 68 triggers, at th
1ec05 65 20 53 51 4c 20 6c 65 76 65 6c 2c 20 74 68 61 e SQL level, tha
1ec06 74 20 69 73 0a 20 20 2a 2a 20 64 69 73 61 62 6c t is. ** disabl
1ec07 65 64 2e 20 49 6e 20 73 6f 6d 65 20 63 61 73 65 ed. In some case
1ec08 73 20 61 20 73 69 6e 67 6c 65 20 74 72 69 67 67 s a single trigg
1ec09 65 72 20 6d 61 79 20 67 65 6e 65 72 61 74 65 20 er may generate
1ec0a 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 0a 20 20 more than one.
1ec0b 2a 2a 20 53 75 62 50 72 6f 67 72 61 6d 20 28 69 ** SubProgram (i
1ec0c 66 20 74 68 65 20 74 72 69 67 67 65 72 20 6d 61 f the trigger ma
1ec0d 79 20 62 65 20 65 78 65 63 75 74 65 64 20 77 69 y be executed wi
1ec0e 74 68 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 th more than one
1ec0f 20 64 69 66 66 65 72 65 6e 74 0a 20 20 2a 2a 20 different. **
1ec10 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 61 6c 67 6f ON CONFLICT algo
1ec11 72 69 74 68 6d 29 2e 20 53 75 62 50 72 6f 67 72 rithm). SubProgr
1ec12 61 6d 20 73 74 72 75 63 74 75 72 65 73 20 61 73 am structures as
1ec13 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 0a sociated with a.
1ec14 20 20 2a 2a 20 73 69 6e 67 6c 65 20 74 72 69 67 ** single trig
1ec15 67 65 72 20 61 6c 6c 20 68 61 76 65 20 74 68 65 ger all have the
1ec16 20 73 61 6d 65 20 76 61 6c 75 65 20 66 6f 72 20 same value for
1ec17 74 68 65 20 53 75 62 50 72 6f 67 72 61 6d 2e 74 the SubProgram.t
1ec18 6f 6b 65 6e 0a 20 20 2a 2a 20 76 61 72 69 61 62 oken. ** variab
1ec19 6c 65 2e 20 20 2a 2f 0a 20 20 69 66 28 20 70 4f le. */. if( pO
1ec1a 70 2d 3e 70 35 20 29 7b 0a 20 20 20 20 75 2e 62 p->p5 ){. u.b
1ec1b 7a 2e 74 20 3d 20 75 2e 62 7a 2e 70 50 72 6f 67 z.t = u.bz.pProg
1ec1c 72 61 6d 2d 3e 74 6f 6b 65 6e 3b 0a 20 20 20 20 ram->token;.
1ec1d 66 6f 72 28 75 2e 62 7a 2e 70 46 72 61 6d 65 3d for(u.bz.pFrame=
1ec1e 70 2d 3e 70 46 72 61 6d 65 3b 20 75 2e 62 7a 2e p->pFrame; u.bz.
1ec1f 70 46 72 61 6d 65 20 26 26 20 75 2e 62 7a 2e 70 pFrame && u.bz.p
1ec20 46 72 61 6d 65 2d 3e 74 6f 6b 65 6e 21 3d 75 2e Frame->token!=u.
1ec21 62 7a 2e 74 3b 20 75 2e 62 7a 2e 70 46 72 61 6d bz.t; u.bz.pFram
1ec22 65 3d 75 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e 70 e=u.bz.pFrame->p
1ec23 50 61 72 65 6e 74 29 3b 0a 20 20 20 20 69 66 28 Parent);. if(
1ec24 20 75 2e 62 7a 2e 70 46 72 61 6d 65 20 29 20 62 u.bz.pFrame ) b
1ec25 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 reak;. }.. if(
1ec26 20 70 2d 3e 6e 46 72 61 6d 65 3e 3d 64 62 2d 3e p->nFrame>=db->
1ec27 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 aLimit[SQLITE_LI
1ec28 4d 49 54 5f 54 52 49 47 47 45 52 5f 44 45 50 54 MIT_TRIGGER_DEPT
1ec29 48 5d 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 H] ){. rc = S
1ec2a 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 QLITE_ERROR;.
1ec2b 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e sqlite3SetStrin
1ec2c 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 g(&p->zErrMsg, d
1ec2d 62 2c 20 22 74 6f 6f 20 6d 61 6e 79 20 6c 65 76 b, "too many lev
1ec2e 65 6c 73 20 6f 66 20 74 72 69 67 67 65 72 20 72 els of trigger r
1ec2f 65 63 75 72 73 69 6f 6e 22 29 3b 0a 20 20 20 20 ecursion");.
1ec30 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a break;. }.. /*
1ec31 20 52 65 67 69 73 74 65 72 20 75 2e 62 7a 2e 70 Register u.bz.p
1ec32 52 74 20 69 73 20 75 73 65 64 20 74 6f 20 73 74 Rt is used to st
1ec33 6f 72 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 72 ore the memory r
1ec34 65 71 75 69 72 65 64 20 74 6f 20 73 61 76 65 20 equired to save
1ec35 74 68 65 20 73 74 61 74 65 0a 20 20 2a 2a 20 6f the state. ** o
1ec36 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 70 72 f the current pr
1ec37 6f 67 72 61 6d 2c 20 61 6e 64 20 74 68 65 20 6d ogram, and the m
1ec38 65 6d 6f 72 79 20 72 65 71 75 69 72 65 64 20 61 emory required a
1ec39 74 20 72 75 6e 74 69 6d 65 20 74 6f 20 65 78 65 t runtime to exe
1ec3a 63 75 74 65 0a 20 20 2a 2a 20 74 68 65 20 74 72 cute. ** the tr
1ec3b 69 67 67 65 72 20 70 72 6f 67 72 61 6d 2e 20 49 igger program. I
1ec3c 66 20 74 68 69 73 20 74 72 69 67 67 65 72 20 68 f this trigger h
1ec3d 61 73 20 62 65 65 6e 20 66 69 72 65 64 20 62 65 as been fired be
1ec3e 66 6f 72 65 2c 20 74 68 65 6e 20 75 2e 62 7a 2e fore, then u.bz.
1ec3f 70 52 74 0a 20 20 2a 2a 20 69 73 20 61 6c 72 65 pRt. ** is alre
1ec40 61 64 79 20 61 6c 6c 6f 63 61 74 65 64 2e 20 4f ady allocated. O
1ec41 74 68 65 72 77 69 73 65 2c 20 69 74 20 6d 75 73 therwise, it mus
1ec42 74 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 t be initialized
1ec43 2e 20 20 2a 2f 0a 20 20 69 66 28 20 28 75 2e 62 . */. if( (u.b
1ec44 7a 2e 70 52 74 2d 3e 66 6c 61 67 73 26 4d 45 4d z.pRt->flags&MEM
1ec45 5f 46 72 61 6d 65 29 3d 3d 30 20 29 7b 0a 20 20 _Frame)==0 ){.
1ec46 20 20 2f 2a 20 53 75 62 50 72 6f 67 72 61 6d 2e /* SubProgram.
1ec47 6e 4d 65 6d 20 69 73 20 73 65 74 20 74 6f 20 74 nMem is set to t
1ec48 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d 65 6d he number of mem
1ec49 6f 72 79 20 63 65 6c 6c 73 20 75 73 65 64 20 62 ory cells used b
1ec4a 79 20 74 68 65 0a 20 20 20 20 2a 2a 20 70 72 6f y the. ** pro
1ec4b 67 72 61 6d 20 73 74 6f 72 65 64 20 69 6e 20 53 gram stored in S
1ec4c 75 62 50 72 6f 67 72 61 6d 2e 61 4f 70 2e 20 41 ubProgram.aOp. A
1ec4d 73 20 77 65 6c 6c 20 61 73 20 74 68 65 73 65 2c s well as these,
1ec4e 20 6f 6e 65 20 6d 65 6d 6f 72 79 0a 20 20 20 20 one memory.
1ec4f 2a 2a 20 63 65 6c 6c 20 69 73 20 72 65 71 75 69 ** cell is requi
1ec50 72 65 64 20 66 6f 72 20 65 61 63 68 20 63 75 72 red for each cur
1ec51 73 6f 72 20 75 73 65 64 20 62 79 20 74 68 65 20 sor used by the
1ec52 70 72 6f 67 72 61 6d 2e 20 53 65 74 20 6c 6f 63 program. Set loc
1ec53 61 6c 0a 20 20 20 20 2a 2a 20 76 61 72 69 61 62 al. ** variab
1ec54 6c 65 20 75 2e 62 7a 2e 6e 4d 65 6d 20 28 61 6e le u.bz.nMem (an
1ec55 64 20 6c 61 74 65 72 2c 20 56 64 62 65 46 72 61 d later, VdbeFra
1ec56 6d 65 2e 6e 43 68 69 6c 64 4d 65 6d 29 20 74 6f me.nChildMem) to
1ec57 20 74 68 69 73 20 76 61 6c 75 65 2e 0a 20 20 20 this value..
1ec58 20 2a 2f 0a 20 20 20 20 75 2e 62 7a 2e 6e 4d 65 */. u.bz.nMe
1ec59 6d 20 3d 20 75 2e 62 7a 2e 70 50 72 6f 67 72 61 m = u.bz.pProgra
1ec5a 6d 2d 3e 6e 4d 65 6d 20 2b 20 75 2e 62 7a 2e 70 m->nMem + u.bz.p
1ec5b 50 72 6f 67 72 61 6d 2d 3e 6e 43 73 72 3b 0a 20 Program->nCsr;.
1ec5c 20 20 20 75 2e 62 7a 2e 6e 42 79 74 65 20 3d 20 u.bz.nByte =
1ec5d 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 56 64 ROUND8(sizeof(Vd
1ec5e 62 65 46 72 61 6d 65 29 29 0a 20 20 20 20 20 20 beFrame)).
1ec5f 20 20 20 20 20 20 20 20 2b 20 75 2e 62 7a 2e 6e + u.bz.n
1ec60 4d 65 6d 20 2a 20 73 69 7a 65 6f 66 28 4d 65 6d Mem * sizeof(Mem
1ec61 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ).
1ec62 2b 20 75 2e 62 7a 2e 70 50 72 6f 67 72 61 6d 2d + u.bz.pProgram-
1ec63 3e 6e 43 73 72 20 2a 20 73 69 7a 65 6f 66 28 56 >nCsr * sizeof(V
1ec64 64 62 65 43 75 72 73 6f 72 20 2a 29 3b 0a 20 20 dbeCursor *);.
1ec65 20 20 75 2e 62 7a 2e 70 46 72 61 6d 65 20 3d 20 u.bz.pFrame =
1ec66 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a sqlite3DbMallocZ
1ec67 65 72 6f 28 64 62 2c 20 75 2e 62 7a 2e 6e 42 79 ero(db, u.bz.nBy
1ec68 74 65 29 3b 0a 20 20 20 20 69 66 28 20 21 75 2e te);. if( !u.
1ec69 62 7a 2e 70 46 72 61 6d 65 20 29 7b 0a 20 20 20 bz.pFrame ){.
1ec6a 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a goto no_mem;.
1ec6b 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 }. sqlite
1ec6c 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 3VdbeMemRelease(
1ec6d 75 2e 62 7a 2e 70 52 74 29 3b 0a 20 20 20 20 75 u.bz.pRt);. u
1ec6e 2e 62 7a 2e 70 52 74 2d 3e 66 6c 61 67 73 20 3d .bz.pRt->flags =
1ec6f 20 4d 45 4d 5f 46 72 61 6d 65 3b 0a 20 20 20 20 MEM_Frame;.
1ec70 75 2e 62 7a 2e 70 52 74 2d 3e 75 2e 70 46 72 61 u.bz.pRt->u.pFra
1ec71 6d 65 20 3d 20 75 2e 62 7a 2e 70 46 72 61 6d 65 me = u.bz.pFrame
1ec72 3b 0a 0a 20 20 20 20 75 2e 62 7a 2e 70 46 72 61 ;.. u.bz.pFra
1ec73 6d 65 2d 3e 76 20 3d 20 70 3b 0a 20 20 20 20 75 me->v = p;. u
1ec74 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e 6e 43 68 69 .bz.pFrame->nChi
1ec75 6c 64 4d 65 6d 20 3d 20 75 2e 62 7a 2e 6e 4d 65 ldMem = u.bz.nMe
1ec76 6d 3b 0a 20 20 20 20 75 2e 62 7a 2e 70 46 72 61 m;. u.bz.pFra
1ec77 6d 65 2d 3e 6e 43 68 69 6c 64 43 73 72 20 3d 20 me->nChildCsr =
1ec78 75 2e 62 7a 2e 70 50 72 6f 67 72 61 6d 2d 3e 6e u.bz.pProgram->n
1ec79 43 73 72 3b 0a 20 20 20 20 75 2e 62 7a 2e 70 46 Csr;. u.bz.pF
1ec7a 72 61 6d 65 2d 3e 70 63 20 3d 20 70 63 3b 0a 20 rame->pc = pc;.
1ec7b 20 20 20 75 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e u.bz.pFrame->
1ec7c 61 4d 65 6d 20 3d 20 70 2d 3e 61 4d 65 6d 3b 0a aMem = p->aMem;.
1ec7d 20 20 20 20 75 2e 62 7a 2e 70 46 72 61 6d 65 2d u.bz.pFrame-
1ec7e 3e 6e 4d 65 6d 20 3d 20 70 2d 3e 6e 4d 65 6d 3b >nMem = p->nMem;
1ec7f 0a 20 20 20 20 75 2e 62 7a 2e 70 46 72 61 6d 65 . u.bz.pFrame
1ec80 2d 3e 61 70 43 73 72 20 3d 20 70 2d 3e 61 70 43 ->apCsr = p->apC
1ec81 73 72 3b 0a 20 20 20 20 75 2e 62 7a 2e 70 46 72 sr;. u.bz.pFr
1ec82 61 6d 65 2d 3e 6e 43 75 72 73 6f 72 20 3d 20 70 ame->nCursor = p
1ec83 2d 3e 6e 43 75 72 73 6f 72 3b 0a 20 20 20 20 75 ->nCursor;. u
1ec84 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e 61 4f 70 20 .bz.pFrame->aOp
1ec85 3d 20 70 2d 3e 61 4f 70 3b 0a 20 20 20 20 75 2e = p->aOp;. u.
1ec86 62 7a 2e 70 46 72 61 6d 65 2d 3e 6e 4f 70 20 3d bz.pFrame->nOp =
1ec87 20 70 2d 3e 6e 4f 70 3b 0a 20 20 20 20 75 2e 62 p->nOp;. u.b
1ec88 7a 2e 70 46 72 61 6d 65 2d 3e 74 6f 6b 65 6e 20 z.pFrame->token
1ec89 3d 20 75 2e 62 7a 2e 70 50 72 6f 67 72 61 6d 2d = u.bz.pProgram-
1ec8a 3e 74 6f 6b 65 6e 3b 0a 0a 20 20 20 20 75 2e 62 >token;.. u.b
1ec8b 7a 2e 70 45 6e 64 20 3d 20 26 56 64 62 65 46 72 z.pEnd = &VdbeFr
1ec8c 61 6d 65 4d 65 6d 28 75 2e 62 7a 2e 70 46 72 61 ameMem(u.bz.pFra
1ec8d 6d 65 29 5b 75 2e 62 7a 2e 70 46 72 61 6d 65 2d me)[u.bz.pFrame-
1ec8e 3e 6e 43 68 69 6c 64 4d 65 6d 5d 3b 0a 20 20 20 >nChildMem];.
1ec8f 20 66 6f 72 28 75 2e 62 7a 2e 70 4d 65 6d 3d 56 for(u.bz.pMem=V
1ec90 64 62 65 46 72 61 6d 65 4d 65 6d 28 75 2e 62 7a dbeFrameMem(u.bz
1ec91 2e 70 46 72 61 6d 65 29 3b 20 75 2e 62 7a 2e 70 .pFrame); u.bz.p
1ec92 4d 65 6d 21 3d 75 2e 62 7a 2e 70 45 6e 64 3b 20 Mem!=u.bz.pEnd;
1ec93 75 2e 62 7a 2e 70 4d 65 6d 2b 2b 29 7b 0a 20 20 u.bz.pMem++){.
1ec94 20 20 20 20 75 2e 62 7a 2e 70 4d 65 6d 2d 3e 66 u.bz.pMem->f
1ec95 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b lags = MEM_Null;
1ec96 0a 20 20 20 20 20 20 75 2e 62 7a 2e 70 4d 65 6d . u.bz.pMem
1ec97 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 20 20 20 7d ->db = db;. }
1ec98 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 2e . }else{. u.
1ec99 62 7a 2e 70 46 72 61 6d 65 20 3d 20 75 2e 62 7a bz.pFrame = u.bz
1ec9a 2e 70 52 74 2d 3e 75 2e 70 46 72 61 6d 65 3b 0a .pRt->u.pFrame;.
1ec9b 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 7a assert( u.bz
1ec9c 2e 70 50 72 6f 67 72 61 6d 2d 3e 6e 4d 65 6d 2b .pProgram->nMem+
1ec9d 75 2e 62 7a 2e 70 50 72 6f 67 72 61 6d 2d 3e 6e u.bz.pProgram->n
1ec9e 43 73 72 3d 3d 75 2e 62 7a 2e 70 46 72 61 6d 65 Csr==u.bz.pFrame
1ec9f 2d 3e 6e 43 68 69 6c 64 4d 65 6d 20 29 3b 0a 20 ->nChildMem );.
1eca0 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 7a 2e assert( u.bz.
1eca1 70 50 72 6f 67 72 61 6d 2d 3e 6e 43 73 72 3d 3d pProgram->nCsr==
1eca2 75 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e 6e 43 68 u.bz.pFrame->nCh
1eca3 69 6c 64 43 73 72 20 29 3b 0a 20 20 20 20 61 73 ildCsr );. as
1eca4 73 65 72 74 28 20 70 63 3d 3d 75 2e 62 7a 2e 70 sert( pc==u.bz.p
1eca5 46 72 61 6d 65 2d 3e 70 63 20 29 3b 0a 20 20 7d Frame->pc );. }
1eca6 0a 0a 20 20 70 2d 3e 6e 46 72 61 6d 65 2b 2b 3b .. p->nFrame++;
1eca7 0a 20 20 75 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e . u.bz.pFrame->
1eca8 70 50 61 72 65 6e 74 20 3d 20 70 2d 3e 70 46 72 pParent = p->pFr
1eca9 61 6d 65 3b 0a 20 20 75 2e 62 7a 2e 70 46 72 61 ame;. u.bz.pFra
1ecaa 6d 65 2d 3e 6c 61 73 74 52 6f 77 69 64 20 3d 20 me->lastRowid =
1ecab 64 62 2d 3e 6c 61 73 74 52 6f 77 69 64 3b 0a 20 db->lastRowid;.
1ecac 20 75 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e 6e 43 u.bz.pFrame->nC
1ecad 68 61 6e 67 65 20 3d 20 70 2d 3e 6e 43 68 61 6e hange = p->nChan
1ecae 67 65 3b 0a 20 20 70 2d 3e 6e 43 68 61 6e 67 65 ge;. p->nChange
1ecaf 20 3d 20 30 3b 0a 20 20 70 2d 3e 70 46 72 61 6d = 0;. p->pFram
1ecb0 65 20 3d 20 75 2e 62 7a 2e 70 46 72 61 6d 65 3b e = u.bz.pFrame;
1ecb1 0a 20 20 70 2d 3e 61 4d 65 6d 20 3d 20 26 56 64 . p->aMem = &Vd
1ecb2 62 65 46 72 61 6d 65 4d 65 6d 28 75 2e 62 7a 2e beFrameMem(u.bz.
1ecb3 70 46 72 61 6d 65 29 5b 2d 31 5d 3b 0a 20 20 70 pFrame)[-1];. p
1ecb4 2d 3e 6e 4d 65 6d 20 3d 20 75 2e 62 7a 2e 70 46 ->nMem = u.bz.pF
1ecb5 72 61 6d 65 2d 3e 6e 43 68 69 6c 64 4d 65 6d 3b rame->nChildMem;
1ecb6 0a 20 20 70 2d 3e 6e 43 75 72 73 6f 72 20 3d 20 . p->nCursor =
1ecb7 28 75 31 36 29 75 2e 62 7a 2e 70 46 72 61 6d 65 (u16)u.bz.pFrame
1ecb8 2d 3e 6e 43 68 69 6c 64 43 73 72 3b 0a 20 20 70 ->nChildCsr;. p
1ecb9 2d 3e 61 70 43 73 72 20 3d 20 28 56 64 62 65 43 ->apCsr = (VdbeC
1ecba 75 72 73 6f 72 20 2a 2a 29 26 70 2d 3e 61 4d 65 ursor **)&p->aMe
1ecbb 6d 5b 70 2d 3e 6e 4d 65 6d 2b 31 5d 3b 0a 20 20 m[p->nMem+1];.
1ecbc 70 2d 3e 61 4f 70 20 3d 20 75 2e 62 7a 2e 70 50 p->aOp = u.bz.pP
1ecbd 72 6f 67 72 61 6d 2d 3e 61 4f 70 3b 0a 20 20 70 rogram->aOp;. p
1ecbe 2d 3e 6e 4f 70 20 3d 20 75 2e 62 7a 2e 70 50 72 ->nOp = u.bz.pPr
1ecbf 6f 67 72 61 6d 2d 3e 6e 4f 70 3b 0a 20 20 70 63 ogram->nOp;. pc
1ecc0 20 3d 20 2d 31 3b 0a 0a 20 20 62 72 65 61 6b 3b = -1;.. break;
1ecc1 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 50 .}../* Opcode: P
1ecc2 61 72 61 6d 20 50 31 20 50 32 20 2a 20 2a 20 2a aram P1 P2 * * *
1ecc3 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f .**.** This opco
1ecc4 64 65 20 69 73 20 6f 6e 6c 79 20 65 76 65 72 20 de is only ever
1ecc5 70 72 65 73 65 6e 74 20 69 6e 20 73 75 62 2d 70 present in sub-p
1ecc6 72 6f 67 72 61 6d 73 20 63 61 6c 6c 65 64 20 76 rograms called v
1ecc7 69 61 20 74 68 65 20 0a 2a 2a 20 4f 50 5f 50 72 ia the .** OP_Pr
1ecc8 6f 67 72 61 6d 20 69 6e 73 74 72 75 63 74 69 6f ogram instructio
1ecc9 6e 2e 20 43 6f 70 79 20 61 20 76 61 6c 75 65 20 n. Copy a value
1ecca 63 75 72 72 65 6e 74 6c 79 20 73 74 6f 72 65 64 currently stored
1eccb 20 69 6e 20 61 20 6d 65 6d 6f 72 79 20 0a 2a 2a in a memory .**
1eccc 20 63 65 6c 6c 20 6f 66 20 74 68 65 20 63 61 6c cell of the cal
1eccd 6c 69 6e 67 20 28 70 61 72 65 6e 74 29 20 66 72 ling (parent) fr
1ecce 61 6d 65 20 74 6f 20 63 65 6c 6c 20 50 32 20 69 ame to cell P2 i
1eccf 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 66 72 n the current fr
1ecd0 61 6d 65 73 20 0a 2a 2a 20 61 64 64 72 65 73 73 ames .** address
1ecd1 20 73 70 61 63 65 2e 20 54 68 69 73 20 69 73 20 space. This is
1ecd2 75 73 65 64 20 62 79 20 74 72 69 67 67 65 72 20 used by trigger
1ecd3 70 72 6f 67 72 61 6d 73 20 74 6f 20 61 63 63 65 programs to acce
1ecd4 73 73 20 74 68 65 20 6e 65 77 2e 2a 20 0a 2a 2a ss the new.* .**
1ecd5 20 61 6e 64 20 6f 6c 64 2e 2a 20 76 61 6c 75 65 and old.* value
1ecd6 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 64 64 s..**.** The add
1ecd7 72 65 73 73 20 6f 66 20 74 68 65 20 63 65 6c 6c ress of the cell
1ecd8 20 69 6e 20 74 68 65 20 70 61 72 65 6e 74 20 66 in the parent f
1ecd9 72 61 6d 65 20 69 73 20 64 65 74 65 72 6d 69 6e rame is determin
1ecda 65 64 20 62 79 20 61 64 64 69 6e 67 0a 2a 2a 20 ed by adding.**
1ecdb 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 the value of the
1ecdc 20 50 31 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 P1 argument to
1ecdd 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 the value of the
1ecde 20 50 31 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 P1 argument to
1ecdf 74 68 65 0a 2a 2a 20 63 61 6c 6c 69 6e 67 20 4f the.** calling O
1ece0 50 5f 50 72 6f 67 72 61 6d 20 69 6e 73 74 72 75 P_Program instru
1ece1 63 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f ction..*/.case O
1ece2 50 5f 50 61 72 61 6d 3a 20 7b 20 20 20 20 20 20 P_Param: {
1ece3 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 /* out2-pre
1ece4 72 65 6c 65 61 73 65 20 2a 2f 0a 23 69 66 20 30 release */.#if 0
1ece5 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 /* local varia
1ece6 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 bles moved into
1ece7 75 2e 63 61 20 2a 2f 0a 20 20 56 64 62 65 46 72 u.ca */. VdbeFr
1ece8 61 6d 65 20 2a 70 46 72 61 6d 65 3b 0a 20 20 4d ame *pFrame;. M
1ece9 65 6d 20 2a 70 49 6e 3b 0a 23 65 6e 64 69 66 20 em *pIn;.#endif
1ecea 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c /* local variabl
1eceb 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e es moved into u.
1ecec 63 61 20 2a 2f 0a 20 20 75 2e 63 61 2e 70 46 72 ca */. u.ca.pFr
1eced 61 6d 65 20 3d 20 70 2d 3e 70 46 72 61 6d 65 3b ame = p->pFrame;
1ecee 0a 20 20 75 2e 63 61 2e 70 49 6e 20 3d 20 26 75 . u.ca.pIn = &u
1ecef 2e 63 61 2e 70 46 72 61 6d 65 2d 3e 61 4d 65 6d .ca.pFrame->aMem
1ecf0 5b 70 4f 70 2d 3e 70 31 20 2b 20 75 2e 63 61 2e [pOp->p1 + u.ca.
1ecf1 70 46 72 61 6d 65 2d 3e 61 4f 70 5b 75 2e 63 61 pFrame->aOp[u.ca
1ecf2 2e 70 46 72 61 6d 65 2d 3e 70 63 5d 2e 70 31 5d .pFrame->pc].p1]
1ecf3 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d ;. sqlite3VdbeM
1ecf4 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28 70 4f emShallowCopy(pO
1ecf5 75 74 2c 20 75 2e 63 61 2e 70 49 6e 2c 20 4d 45 ut, u.ca.pIn, ME
1ecf6 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 62 72 65 61 M_Ephem);. brea
1ecf7 6b 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 k;.}..#endif /*
1ecf8 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1ecf9 4d 49 54 5f 54 52 49 47 47 45 52 20 2a 2f 0a 0a MIT_TRIGGER */..
1ecfa 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1ecfb 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 0a MIT_FOREIGN_KEY.
1ecfc 2f 2a 20 4f 70 63 6f 64 65 3a 20 46 6b 43 6f 75 /* Opcode: FkCou
1ecfd 6e 74 65 72 20 50 31 20 50 32 20 2a 20 2a 20 2a nter P1 P2 * * *
1ecfe 0a 2a 2a 0a 2a 2a 20 49 6e 63 72 65 6d 65 6e 74 .**.** Increment
1ecff 20 61 20 22 63 6f 6e 73 74 72 61 69 6e 74 20 63 a "constraint c
1ed00 6f 75 6e 74 65 72 22 20 62 79 20 50 32 20 28 50 ounter" by P2 (P
1ed01 32 20 6d 61 79 20 62 65 20 6e 65 67 61 74 69 76 2 may be negativ
1ed02 65 20 6f 72 20 70 6f 73 69 74 69 76 65 29 2e 0a e or positive)..
1ed03 2a 2a 20 49 66 20 50 31 20 69 73 20 6e 6f 6e 2d ** If P1 is non-
1ed04 7a 65 72 6f 2c 20 74 68 65 20 64 61 74 61 62 61 zero, the databa
1ed05 73 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 6f se constraint co
1ed06 75 6e 74 65 72 20 69 73 20 69 6e 63 72 65 6d 65 unter is increme
1ed07 6e 74 65 64 20 0a 2a 2a 20 28 64 65 66 65 72 72 nted .** (deferr
1ed08 65 64 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 ed foreign key c
1ed09 6f 6e 73 74 72 61 69 6e 74 73 29 2e 20 4f 74 68 onstraints). Oth
1ed0a 65 72 77 69 73 65 2c 20 69 66 20 50 31 20 69 73 erwise, if P1 is
1ed0b 20 7a 65 72 6f 2c 20 74 68 65 20 0a 2a 2a 20 73 zero, the .** s
1ed0c 74 61 74 65 6d 65 6e 74 20 63 6f 75 6e 74 65 72 tatement counter
1ed0d 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 is incremented
1ed0e 28 69 6d 6d 65 64 69 61 74 65 20 66 6f 72 65 69 (immediate forei
1ed0f 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e gn key constrain
1ed10 74 73 29 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f ts)..*/.case OP_
1ed11 46 6b 43 6f 75 6e 74 65 72 3a 20 7b 0a 20 20 69 FkCounter: {. i
1ed12 66 28 20 70 4f 70 2d 3e 70 31 20 29 7b 0a 20 20 f( pOp->p1 ){.
1ed13 20 20 64 62 2d 3e 6e 44 65 66 65 72 72 65 64 43 db->nDeferredC
1ed14 6f 6e 73 20 2b 3d 20 70 4f 70 2d 3e 70 32 3b 0a ons += pOp->p2;.
1ed15 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 2d 3e }else{. p->
1ed16 6e 46 6b 43 6f 6e 73 74 72 61 69 6e 74 20 2b 3d nFkConstraint +=
1ed17 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 7d 0a 20 20 pOp->p2;. }.
1ed18 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 break;.}../* Opc
1ed19 6f 64 65 3a 20 46 6b 49 66 5a 65 72 6f 20 50 31 ode: FkIfZero P1
1ed1a 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 P2 * * *.**.**
1ed1b 54 68 69 73 20 6f 70 63 6f 64 65 20 74 65 73 74 This opcode test
1ed1c 73 20 69 66 20 61 20 66 6f 72 65 69 67 6e 20 6b s if a foreign k
1ed1d 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 2d 63 6f ey constraint-co
1ed1e 75 6e 74 65 72 20 69 73 20 63 75 72 72 65 6e 74 unter is current
1ed1f 6c 79 20 7a 65 72 6f 2e 0a 2a 2a 20 49 66 20 73 ly zero..** If s
1ed20 6f 2c 20 6a 75 6d 70 20 74 6f 20 69 6e 73 74 72 o, jump to instr
1ed21 75 63 74 69 6f 6e 20 50 32 2e 20 4f 74 68 65 72 uction P2. Other
1ed22 77 69 73 65 2c 20 66 61 6c 6c 20 74 68 72 6f 75 wise, fall throu
1ed23 67 68 20 74 6f 20 74 68 65 20 6e 65 78 74 20 0a gh to the next .
1ed24 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a ** instruction..
1ed25 2a 2a 0a 2a 2a 20 49 66 20 50 31 20 69 73 20 6e **.** If P1 is n
1ed26 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 on-zero, then th
1ed27 65 20 6a 75 6d 70 20 69 73 20 74 61 6b 65 6e 20 e jump is taken
1ed28 69 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 if the database
1ed29 63 6f 6e 73 74 72 61 69 6e 74 2d 63 6f 75 6e 74 constraint-count
1ed2a 65 72 0a 2a 2a 20 69 73 20 7a 65 72 6f 20 28 74 er.** is zero (t
1ed2b 68 65 20 6f 6e 65 20 74 68 61 74 20 63 6f 75 6e he one that coun
1ed2c 74 73 20 64 65 66 65 72 72 65 64 20 63 6f 6e 73 ts deferred cons
1ed2d 74 72 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e traint violation
1ed2e 73 29 2e 20 49 66 20 50 31 20 69 73 0a 2a 2a 20 s). If P1 is.**
1ed2f 7a 65 72 6f 2c 20 74 68 65 20 6a 75 6d 70 20 69 zero, the jump i
1ed30 73 20 74 61 6b 65 6e 20 69 66 20 74 68 65 20 73 s taken if the s
1ed31 74 61 74 65 6d 65 6e 74 20 63 6f 6e 73 74 72 61 tatement constra
1ed32 69 6e 74 2d 63 6f 75 6e 74 65 72 20 69 73 20 7a int-counter is z
1ed33 65 72 6f 0a 2a 2a 20 28 69 6d 6d 65 64 69 61 74 ero.** (immediat
1ed34 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f e foreign key co
1ed35 6e 73 74 72 61 69 6e 74 20 76 69 6f 6c 61 74 69 nstraint violati
1ed36 6f 6e 73 29 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 ons)..*/.case OP
1ed37 5f 46 6b 49 66 5a 65 72 6f 3a 20 7b 20 20 20 20 _FkIfZero: {
1ed38 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a /* jump */.
1ed39 20 20 69 66 28 20 70 4f 70 2d 3e 70 31 20 29 7b if( pOp->p1 ){
1ed3a 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6e 44 65 . if( db->nDe
1ed3b 66 65 72 72 65 64 43 6f 6e 73 3d 3d 30 20 29 20 ferredCons==0 )
1ed3c 70 63 20 3d 20 70 4f 70 2d 3e 70 32 2d 31 3b 0a pc = pOp->p2-1;.
1ed3d 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66 28 }else{. if(
1ed3e 20 70 2d 3e 6e 46 6b 43 6f 6e 73 74 72 61 69 6e p->nFkConstrain
1ed3f 74 3d 3d 30 20 29 20 70 63 20 3d 20 70 4f 70 2d t==0 ) pc = pOp-
1ed40 3e 70 32 2d 31 3b 0a 20 20 7d 0a 20 20 62 72 65 >p2-1;. }. bre
1ed41 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 ak;.}.#endif /*
1ed42 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1ed43 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 20 MIT_FOREIGN_KEY
1ed44 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 */..#ifndef SQLI
1ed45 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52 TE_OMIT_AUTOINCR
1ed46 45 4d 45 4e 54 0a 2f 2a 20 4f 70 63 6f 64 65 3a EMENT./* Opcode:
1ed47 20 4d 65 6d 4d 61 78 20 50 31 20 50 32 20 2a 20 MemMax P1 P2 *
1ed48 2a 20 2a 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 * *.**.** P1 is
1ed49 61 20 72 65 67 69 73 74 65 72 20 69 6e 20 74 68 a register in th
1ed4a 65 20 72 6f 6f 74 20 66 72 61 6d 65 20 6f 66 20 e root frame of
1ed4b 74 68 69 73 20 56 4d 20 28 74 68 65 20 72 6f 6f this VM (the roo
1ed4c 74 20 66 72 61 6d 65 20 69 73 0a 2a 2a 20 64 69 t frame is.** di
1ed4d 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 74 68 65 fferent from the
1ed4e 20 63 75 72 72 65 6e 74 20 66 72 61 6d 65 20 69 current frame i
1ed4f 66 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 f this instructi
1ed50 6f 6e 20 69 73 20 62 65 69 6e 67 20 65 78 65 63 on is being exec
1ed51 75 74 65 64 0a 2a 2a 20 77 69 74 68 69 6e 20 61 uted.** within a
1ed52 20 73 75 62 2d 70 72 6f 67 72 61 6d 29 2e 20 53 sub-program). S
1ed53 65 74 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 et the value of
1ed54 72 65 67 69 73 74 65 72 20 50 31 20 74 6f 20 74 register P1 to t
1ed55 68 65 20 6d 61 78 69 6d 75 6d 20 6f 66 20 0a 2a he maximum of .*
1ed56 2a 20 69 74 73 20 63 75 72 72 65 6e 74 20 76 61 * its current va
1ed57 6c 75 65 20 61 6e 64 20 74 68 65 20 76 61 6c 75 lue and the valu
1ed58 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 e in register P2
1ed59 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 73 ..**.** This ins
1ed5a 74 72 75 63 74 69 6f 6e 20 74 68 72 6f 77 73 20 truction throws
1ed5b 61 6e 20 65 72 72 6f 72 20 69 66 20 74 68 65 20 an error if the
1ed5c 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 69 73 20 6e memory cell is n
1ed5d 6f 74 20 69 6e 69 74 69 61 6c 6c 79 0a 2a 2a 20 ot initially.**
1ed5e 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2f 0a 63 an integer..*/.c
1ed5f 61 73 65 20 4f 50 5f 4d 65 6d 4d 61 78 3a 20 7b ase OP_MemMax: {
1ed60 20 20 20 20 20 20 20 20 2f 2a 20 69 6e 32 20 2a /* in2 *
1ed61 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 /.#if 0 /* loca
1ed62 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 l variables move
1ed63 64 20 69 6e 74 6f 20 75 2e 63 62 20 2a 2f 0a 20 d into u.cb */.
1ed64 20 4d 65 6d 20 2a 70 49 6e 31 3b 0a 20 20 56 64 Mem *pIn1;. Vd
1ed65 62 65 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 3b beFrame *pFrame;
1ed66 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c .#endif /* local
1ed67 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1ed68 20 69 6e 74 6f 20 75 2e 63 62 20 2a 2f 0a 20 20 into u.cb */.
1ed69 69 66 28 20 70 2d 3e 70 46 72 61 6d 65 20 29 7b if( p->pFrame ){
1ed6a 0a 20 20 20 20 66 6f 72 28 75 2e 63 62 2e 70 46 . for(u.cb.pF
1ed6b 72 61 6d 65 3d 70 2d 3e 70 46 72 61 6d 65 3b 20 rame=p->pFrame;
1ed6c 75 2e 63 62 2e 70 46 72 61 6d 65 2d 3e 70 50 61 u.cb.pFrame->pPa
1ed6d 72 65 6e 74 3b 20 75 2e 63 62 2e 70 46 72 61 6d rent; u.cb.pFram
1ed6e 65 3d 75 2e 63 62 2e 70 46 72 61 6d 65 2d 3e 70 e=u.cb.pFrame->p
1ed6f 50 61 72 65 6e 74 29 3b 0a 20 20 20 20 75 2e 63 Parent);. u.c
1ed70 62 2e 70 49 6e 31 20 3d 20 26 75 2e 63 62 2e 70 b.pIn1 = &u.cb.p
1ed71 46 72 61 6d 65 2d 3e 61 4d 65 6d 5b 70 4f 70 2d Frame->aMem[pOp-
1ed72 3e 70 31 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 >p1];. }else{.
1ed73 20 20 20 75 2e 63 62 2e 70 49 6e 31 20 3d 20 26 u.cb.pIn1 = &
1ed74 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d p->aMem[pOp->p1]
1ed75 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 ;. }. sqlite3V
1ed76 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 dbeMemIntegerify
1ed77 28 75 2e 63 62 2e 70 49 6e 31 29 3b 0a 20 20 73 (u.cb.pIn1);. s
1ed78 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 qlite3VdbeMemInt
1ed79 65 67 65 72 69 66 79 28 70 49 6e 32 29 3b 0a 20 egerify(pIn2);.
1ed7a 20 69 66 28 20 75 2e 63 62 2e 70 49 6e 31 2d 3e if( u.cb.pIn1->
1ed7b 75 2e 69 3c 70 49 6e 32 2d 3e 75 2e 69 29 7b 0a u.i<pIn2->u.i){.
1ed7c 20 20 20 20 75 2e 63 62 2e 70 49 6e 31 2d 3e 75 u.cb.pIn1->u
1ed7d 2e 69 20 3d 20 70 49 6e 32 2d 3e 75 2e 69 3b 0a .i = pIn2->u.i;.
1ed7e 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 }. break;.}.#
1ed7f 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
1ed80 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d 45 OMIT_AUTOINCREME
1ed81 4e 54 20 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f 64 65 NT */../* Opcode
1ed82 3a 20 49 66 50 6f 73 20 50 31 20 50 32 20 2a 20 : IfPos P1 P2 *
1ed83 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 * *.**.** If the
1ed84 20 76 61 6c 75 65 20 6f 66 20 72 65 67 69 73 74 value of regist
1ed85 65 72 20 50 31 20 69 73 20 31 20 6f 72 20 67 72 er P1 is 1 or gr
1ed86 65 61 74 65 72 2c 20 6a 75 6d 70 20 74 6f 20 50 eater, jump to P
1ed87 32 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 69 2..**.** It is i
1ed88 6c 6c 65 67 61 6c 20 74 6f 20 75 73 65 20 74 68 llegal to use th
1ed89 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 6f is instruction o
1ed8a 6e 20 61 20 72 65 67 69 73 74 65 72 20 74 68 61 n a register tha
1ed8b 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 63 6f t does.** not co
1ed8c 6e 74 61 69 6e 20 61 6e 20 69 6e 74 65 67 65 72 ntain an integer
1ed8d 2e 20 20 41 6e 20 61 73 73 65 72 74 69 6f 6e 20 . An assertion
1ed8e 66 61 75 6c 74 20 77 69 6c 6c 20 72 65 73 75 6c fault will resul
1ed8f 74 20 69 66 20 79 6f 75 20 74 72 79 2e 0a 2a 2f t if you try..*/
1ed90 0a 63 61 73 65 20 4f 50 5f 49 66 50 6f 73 3a 20 .case OP_IfPos:
1ed91 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 { /* jump
1ed92 2c 20 69 6e 31 20 2a 2f 0a 20 20 61 73 73 65 72 , in1 */. asser
1ed93 74 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 26 4d t( pIn1->flags&M
1ed94 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 69 66 28 20 EM_Int );. if(
1ed95 70 49 6e 31 2d 3e 75 2e 69 3e 30 20 29 7b 0a 20 pIn1->u.i>0 ){.
1ed96 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 pc = pOp->p2
1ed97 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 - 1;. }. brea
1ed98 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a k;.}../* Opcode:
1ed99 20 49 66 4e 65 67 20 50 31 20 50 32 20 2a 20 2a IfNeg P1 P2 * *
1ed9a 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 *.**.** If the
1ed9b 76 61 6c 75 65 20 6f 66 20 72 65 67 69 73 74 65 value of registe
1ed9c 72 20 50 31 20 69 73 20 6c 65 73 73 20 74 68 61 r P1 is less tha
1ed9d 6e 20 7a 65 72 6f 2c 20 6a 75 6d 70 20 74 6f 20 n zero, jump to
1ed9e 50 32 2e 20 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 P2. .**.** It is
1ed9f 20 69 6c 6c 65 67 61 6c 20 74 6f 20 75 73 65 20 illegal to use
1eda0 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e this instruction
1eda1 20 6f 6e 20 61 20 72 65 67 69 73 74 65 72 20 74 on a register t
1eda2 68 61 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 hat does.** not
1eda3 63 6f 6e 74 61 69 6e 20 61 6e 20 69 6e 74 65 67 contain an integ
1eda4 65 72 2e 20 20 41 6e 20 61 73 73 65 72 74 69 6f er. An assertio
1eda5 6e 20 66 61 75 6c 74 20 77 69 6c 6c 20 72 65 73 n fault will res
1eda6 75 6c 74 20 69 66 20 79 6f 75 20 74 72 79 2e 0a ult if you try..
1eda7 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 66 4e 65 67 */.case OP_IfNeg
1eda8 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 : { /* ju
1eda9 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 20 61 73 73 mp, in1 */. ass
1edaa 65 72 74 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 ert( pIn1->flags
1edab 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 69 66 &MEM_Int );. if
1edac 28 20 70 49 6e 31 2d 3e 75 2e 69 3c 30 20 29 7b ( pIn1->u.i<0 ){
1edad 0a 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e . pc = pOp->
1edae 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 p2 - 1;. }. br
1edaf 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1edb0 65 3a 20 49 66 5a 65 72 6f 20 50 31 20 50 32 20 e: IfZero P1 P2
1edb1 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 74 * * *.**.** If t
1edb2 68 65 20 76 61 6c 75 65 20 6f 66 20 72 65 67 69 he value of regi
1edb3 73 74 65 72 20 50 31 20 69 73 20 65 78 61 63 74 ster P1 is exact
1edb4 6c 79 20 30 2c 20 6a 75 6d 70 20 74 6f 20 50 32 ly 0, jump to P2
1edb5 2e 20 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 69 . .**.** It is i
1edb6 6c 6c 65 67 61 6c 20 74 6f 20 75 73 65 20 74 68 llegal to use th
1edb7 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 6f is instruction o
1edb8 6e 20 61 20 72 65 67 69 73 74 65 72 20 74 68 61 n a register tha
1edb9 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 63 6f t does.** not co
1edba 6e 74 61 69 6e 20 61 6e 20 69 6e 74 65 67 65 72 ntain an integer
1edbb 2e 20 20 41 6e 20 61 73 73 65 72 74 69 6f 6e 20 . An assertion
1edbc 66 61 75 6c 74 20 77 69 6c 6c 20 72 65 73 75 6c fault will resul
1edbd 74 20 69 66 20 79 6f 75 20 74 72 79 2e 0a 2a 2f t if you try..*/
1edbe 0a 63 61 73 65 20 4f 50 5f 49 66 5a 65 72 6f 3a .case OP_IfZero:
1edbf 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d { /* jum
1edc0 70 2c 20 69 6e 31 20 2a 2f 0a 20 20 61 73 73 65 p, in1 */. asse
1edc1 72 74 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 26 rt( pIn1->flags&
1edc2 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 69 66 28 MEM_Int );. if(
1edc3 20 70 49 6e 31 2d 3e 75 2e 69 3d 3d 30 20 29 7b pIn1->u.i==0 ){
1edc4 0a 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e . pc = pOp->
1edc5 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 p2 - 1;. }. br
1edc6 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1edc7 65 3a 20 41 67 67 53 74 65 70 20 2a 20 50 32 20 e: AggStep * P2
1edc8 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 45 P3 P4 P5.**.** E
1edc9 78 65 63 75 74 65 20 74 68 65 20 73 74 65 70 20 xecute the step
1edca 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 61 6e 20 function for an
1edcb 61 67 67 72 65 67 61 74 65 2e 20 20 54 68 65 0a aggregate. The.
1edcc 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 68 61 73 20 ** function has
1edcd 50 35 20 61 72 67 75 6d 65 6e 74 73 2e 20 20 20 P5 arguments.
1edce 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 P4 is a pointer
1edcf 74 6f 20 74 68 65 20 46 75 6e 63 44 65 66 0a 2a to the FuncDef.*
1edd0 2a 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74 * structure that
1edd1 20 73 70 65 63 69 66 69 65 73 20 74 68 65 20 66 specifies the f
1edd2 75 6e 63 74 69 6f 6e 2e 20 20 55 73 65 20 72 65 unction. Use re
1edd3 67 69 73 74 65 72 0a 2a 2a 20 50 33 20 61 73 20 gister.** P3 as
1edd4 74 68 65 20 61 63 63 75 6d 75 6c 61 74 6f 72 2e the accumulator.
1edd5 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 35 20 61 72 .**.** The P5 ar
1edd6 67 75 6d 65 6e 74 73 20 61 72 65 20 74 61 6b 65 guments are take
1edd7 6e 20 66 72 6f 6d 20 72 65 67 69 73 74 65 72 20 n from register
1edd8 50 32 20 61 6e 64 20 69 74 73 0a 2a 2a 20 73 75 P2 and its.** su
1edd9 63 63 65 73 73 6f 72 73 2e 0a 2a 2f 0a 63 61 73 ccessors..*/.cas
1edda 65 20 4f 50 5f 41 67 67 53 74 65 70 3a 20 7b 0a e OP_AggStep: {.
1eddb 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 #if 0 /* local
1eddc 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 variables moved
1eddd 69 6e 74 6f 20 75 2e 63 63 20 2a 2f 0a 20 20 69 into u.cc */. i
1edde 6e 74 20 6e 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 nt n;. int i;.
1eddf 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 4d 65 Mem *pMem;. Me
1ede0 6d 20 2a 70 52 65 63 3b 0a 20 20 73 71 6c 69 74 m *pRec;. sqlit
1ede1 65 33 5f 63 6f 6e 74 65 78 74 20 63 74 78 3b 0a e3_context ctx;.
1ede2 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 sqlite3_value
1ede3 2a 2a 61 70 56 61 6c 3b 0a 23 65 6e 64 69 66 20 **apVal;.#endif
1ede4 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c /* local variabl
1ede5 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e es moved into u.
1ede6 63 63 20 2a 2f 0a 0a 20 20 75 2e 63 63 2e 6e 20 cc */.. u.cc.n
1ede7 3d 20 70 4f 70 2d 3e 70 35 3b 0a 20 20 61 73 73 = pOp->p5;. ass
1ede8 65 72 74 28 20 75 2e 63 63 2e 6e 3e 3d 30 20 29 ert( u.cc.n>=0 )
1ede9 3b 0a 20 20 75 2e 63 63 2e 70 52 65 63 20 3d 20 ;. u.cc.pRec =
1edea 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 &p->aMem[pOp->p2
1edeb 5d 3b 0a 20 20 75 2e 63 63 2e 61 70 56 61 6c 20 ];. u.cc.apVal
1edec 3d 20 70 2d 3e 61 70 41 72 67 3b 0a 20 20 61 73 = p->apArg;. as
1eded 73 65 72 74 28 20 75 2e 63 63 2e 61 70 56 61 6c sert( u.cc.apVal
1edee 20 7c 7c 20 75 2e 63 63 2e 6e 3d 3d 30 20 29 3b || u.cc.n==0 );
1edef 0a 20 20 66 6f 72 28 75 2e 63 63 2e 69 3d 30 3b . for(u.cc.i=0;
1edf0 20 75 2e 63 63 2e 69 3c 75 2e 63 63 2e 6e 3b 20 u.cc.i<u.cc.n;
1edf1 75 2e 63 63 2e 69 2b 2b 2c 20 75 2e 63 63 2e 70 u.cc.i++, u.cc.p
1edf2 52 65 63 2b 2b 29 7b 0a 20 20 20 20 75 2e 63 63 Rec++){. u.cc
1edf3 2e 61 70 56 61 6c 5b 75 2e 63 63 2e 69 5d 20 3d .apVal[u.cc.i] =
1edf4 20 75 2e 63 63 2e 70 52 65 63 3b 0a 20 20 20 20 u.cc.pRec;.
1edf5 73 74 6f 72 65 54 79 70 65 49 6e 66 6f 28 75 2e storeTypeInfo(u.
1edf6 63 63 2e 70 52 65 63 2c 20 65 6e 63 6f 64 69 6e cc.pRec, encodin
1edf7 67 29 3b 0a 20 20 7d 0a 20 20 75 2e 63 63 2e 63 g);. }. u.cc.c
1edf8 74 78 2e 70 46 75 6e 63 20 3d 20 70 4f 70 2d 3e tx.pFunc = pOp->
1edf9 70 34 2e 70 46 75 6e 63 3b 0a 20 20 61 73 73 65 p4.pFunc;. asse
1edfa 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 26 26 rt( pOp->p3>0 &&
1edfb 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 pOp->p3<=p->nMe
1edfc 6d 20 29 3b 0a 20 20 75 2e 63 63 2e 63 74 78 2e m );. u.cc.ctx.
1edfd 70 4d 65 6d 20 3d 20 75 2e 63 63 2e 70 4d 65 6d pMem = u.cc.pMem
1edfe 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d = &p->aMem[pOp-
1edff 3e 70 33 5d 3b 0a 20 20 75 2e 63 63 2e 70 4d 65 >p3];. u.cc.pMe
1ee00 6d 2d 3e 6e 2b 2b 3b 0a 20 20 75 2e 63 63 2e 63 m->n++;. u.cc.c
1ee01 74 78 2e 73 2e 66 6c 61 67 73 20 3d 20 4d 45 4d tx.s.flags = MEM
1ee02 5f 4e 75 6c 6c 3b 0a 20 20 75 2e 63 63 2e 63 74 _Null;. u.cc.ct
1ee03 78 2e 73 2e 7a 20 3d 20 30 3b 0a 20 20 75 2e 63 x.s.z = 0;. u.c
1ee04 63 2e 63 74 78 2e 73 2e 7a 4d 61 6c 6c 6f 63 20 c.ctx.s.zMalloc
1ee05 3d 20 30 3b 0a 20 20 75 2e 63 63 2e 63 74 78 2e = 0;. u.cc.ctx.
1ee06 73 2e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 75 2e s.xDel = 0;. u.
1ee07 63 63 2e 63 74 78 2e 73 2e 64 62 20 3d 20 64 62 cc.ctx.s.db = db
1ee08 3b 0a 20 20 75 2e 63 63 2e 63 74 78 2e 69 73 45 ;. u.cc.ctx.isE
1ee09 72 72 6f 72 20 3d 20 30 3b 0a 20 20 75 2e 63 63 rror = 0;. u.cc
1ee0a 2e 63 74 78 2e 70 43 6f 6c 6c 20 3d 20 30 3b 0a .ctx.pColl = 0;.
1ee0b 20 20 69 66 28 20 75 2e 63 63 2e 63 74 78 2e 70 if( u.cc.ctx.p
1ee0c 46 75 6e 63 2d 3e 66 6c 61 67 73 20 26 20 53 51 Func->flags & SQ
1ee0d 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 45 44 43 4f LITE_FUNC_NEEDCO
1ee0e 4c 4c 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 LL ){. assert
1ee0f 28 20 70 4f 70 3e 70 2d 3e 61 4f 70 20 29 3b 0a ( pOp>p->aOp );.
1ee10 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 5b assert( pOp[
1ee11 2d 31 5d 2e 70 34 74 79 70 65 3d 3d 50 34 5f 43 -1].p4type==P4_C
1ee12 4f 4c 4c 53 45 51 20 29 3b 0a 20 20 20 20 61 73 OLLSEQ );. as
1ee13 73 65 72 74 28 20 70 4f 70 5b 2d 31 5d 2e 6f 70 sert( pOp[-1].op
1ee14 63 6f 64 65 3d 3d 4f 50 5f 43 6f 6c 6c 53 65 71 code==OP_CollSeq
1ee15 20 29 3b 0a 20 20 20 20 75 2e 63 63 2e 63 74 78 );. u.cc.ctx
1ee16 2e 70 43 6f 6c 6c 20 3d 20 70 4f 70 5b 2d 31 5d .pColl = pOp[-1]
1ee17 2e 70 34 2e 70 43 6f 6c 6c 3b 0a 20 20 7d 0a 20 .p4.pColl;. }.
1ee18 20 28 75 2e 63 63 2e 63 74 78 2e 70 46 75 6e 63 (u.cc.ctx.pFunc
1ee19 2d 3e 78 53 74 65 70 29 28 26 75 2e 63 63 2e 63 ->xStep)(&u.cc.c
1ee1a 74 78 2c 20 75 2e 63 63 2e 6e 2c 20 75 2e 63 63 tx, u.cc.n, u.cc
1ee1b 2e 61 70 56 61 6c 29 3b 0a 20 20 69 66 28 20 75 .apVal);. if( u
1ee1c 2e 63 63 2e 63 74 78 2e 69 73 45 72 72 6f 72 20 .cc.ctx.isError
1ee1d 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 ){. sqlite3Se
1ee1e 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 tString(&p->zErr
1ee1f 4d 73 67 2c 20 64 62 2c 20 22 25 73 22 2c 20 73 Msg, db, "%s", s
1ee20 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 qlite3_value_tex
1ee21 74 28 26 75 2e 63 63 2e 63 74 78 2e 73 29 29 3b t(&u.cc.ctx.s));
1ee22 0a 20 20 20 20 72 63 20 3d 20 75 2e 63 63 2e 63 . rc = u.cc.c
1ee23 74 78 2e 69 73 45 72 72 6f 72 3b 0a 20 20 7d 0a tx.isError;. }.
1ee24 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1ee25 52 65 6c 65 61 73 65 28 26 75 2e 63 63 2e 63 74 Release(&u.cc.ct
1ee26 78 2e 73 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d x.s);. break;.}
1ee27 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 41 67 67 ../* Opcode: Agg
1ee28 46 69 6e 61 6c 20 50 31 20 50 32 20 2a 20 50 34 Final P1 P2 * P4
1ee29 20 2a 0a 2a 2a 0a 2a 2a 20 45 78 65 63 75 74 65 *.**.** Execute
1ee2a 20 74 68 65 20 66 69 6e 61 6c 69 7a 65 72 20 66 the finalizer f
1ee2b 75 6e 63 74 69 6f 6e 20 66 6f 72 20 61 6e 20 61 unction for an a
1ee2c 67 67 72 65 67 61 74 65 2e 20 20 50 31 20 69 73 ggregate. P1 is
1ee2d 0a 2a 2a 20 74 68 65 20 6d 65 6d 6f 72 79 20 6c .** the memory l
1ee2e 6f 63 61 74 69 6f 6e 20 74 68 61 74 20 69 73 20 ocation that is
1ee2f 74 68 65 20 61 63 63 75 6d 75 6c 61 74 6f 72 20 the accumulator
1ee30 66 6f 72 20 74 68 65 20 61 67 67 72 65 67 61 74 for the aggregat
1ee31 65 2e 0a 2a 2a 0a 2a 2a 20 50 32 20 69 73 20 74 e..**.** P2 is t
1ee32 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 he number of arg
1ee33 75 6d 65 6e 74 73 20 74 68 61 74 20 74 68 65 20 uments that the
1ee34 73 74 65 70 20 66 75 6e 63 74 69 6f 6e 20 74 61 step function ta
1ee35 6b 65 73 20 61 6e 64 0a 2a 2a 20 50 34 20 69 73 kes and.** P4 is
1ee36 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 a pointer to th
1ee37 65 20 46 75 6e 63 44 65 66 20 66 6f 72 20 74 68 e FuncDef for th
1ee38 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 is function. Th
1ee39 65 20 50 32 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 e P2.** argument
1ee3a 20 69 73 20 6e 6f 74 20 75 73 65 64 20 62 79 20 is not used by
1ee3b 74 68 69 73 20 6f 70 63 6f 64 65 2e 20 20 49 74 this opcode. It
1ee3c 20 69 73 20 6f 6e 6c 79 20 74 68 65 72 65 20 74 is only there t
1ee3d 6f 20 64 69 73 61 6d 62 69 67 75 61 74 65 0a 2a o disambiguate.*
1ee3e 2a 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 * functions that
1ee3f 20 63 61 6e 20 74 61 6b 65 20 76 61 72 79 69 6e can take varyin
1ee40 67 20 6e 75 6d 62 65 72 73 20 6f 66 20 61 72 67 g numbers of arg
1ee41 75 6d 65 6e 74 73 2e 20 20 54 68 65 0a 2a 2a 20 uments. The.**
1ee42 50 34 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6f P4 argument is o
1ee43 6e 6c 79 20 6e 65 65 64 65 64 20 66 6f 72 20 74 nly needed for t
1ee44 68 65 20 64 65 67 65 6e 65 72 61 74 65 20 63 61 he degenerate ca
1ee45 73 65 20 77 68 65 72 65 0a 2a 2a 20 74 68 65 20 se where.** the
1ee46 73 74 65 70 20 66 75 6e 63 74 69 6f 6e 20 77 61 step function wa
1ee47 73 20 6e 6f 74 20 70 72 65 76 69 6f 75 73 6c 79 s not previously
1ee48 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 63 61 73 65 called..*/.case
1ee49 20 4f 50 5f 41 67 67 46 69 6e 61 6c 3a 20 7b 0a OP_AggFinal: {.
1ee4a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 #if 0 /* local
1ee4b 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 variables moved
1ee4c 69 6e 74 6f 20 75 2e 63 64 20 2a 2f 0a 20 20 4d into u.cd */. M
1ee4d 65 6d 20 2a 70 4d 65 6d 3b 0a 23 65 6e 64 69 66 em *pMem;.#endif
1ee4e 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1ee4f 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1ee50 2e 63 64 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 .cd */. assert(
1ee51 20 70 4f 70 2d 3e 70 31 3e 30 20 26 26 20 70 4f pOp->p1>0 && pO
1ee52 70 2d 3e 70 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 p->p1<=p->nMem )
1ee53 3b 0a 20 20 75 2e 63 64 2e 70 4d 65 6d 20 3d 20 ;. u.cd.pMem =
1ee54 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 &p->aMem[pOp->p1
1ee55 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 28 75 2e ];. assert( (u.
1ee56 63 64 2e 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 cd.pMem->flags &
1ee57 20 7e 28 4d 45 4d 5f 4e 75 6c 6c 7c 4d 45 4d 5f ~(MEM_Null|MEM_
1ee58 41 67 67 29 29 3d 3d 30 20 29 3b 0a 20 20 72 63 Agg))==0 );. rc
1ee59 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 = sqlite3VdbeMe
1ee5a 6d 46 69 6e 61 6c 69 7a 65 28 75 2e 63 64 2e 70 mFinalize(u.cd.p
1ee5b 4d 65 6d 2c 20 70 4f 70 2d 3e 70 34 2e 70 46 75 Mem, pOp->p4.pFu
1ee5c 6e 63 29 3b 0a 20 20 69 66 28 20 72 63 20 29 7b nc);. if( rc ){
1ee5d 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 . sqlite3SetS
1ee5e 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 tring(&p->zErrMs
1ee5f 67 2c 20 64 62 2c 20 22 25 73 22 2c 20 73 71 6c g, db, "%s", sql
1ee60 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 ite3_value_text(
1ee61 75 2e 63 64 2e 70 4d 65 6d 29 29 3b 0a 20 20 7d u.cd.pMem));. }
1ee62 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 . sqlite3VdbeCh
1ee63 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 75 2e 63 angeEncoding(u.c
1ee64 64 2e 70 4d 65 6d 2c 20 65 6e 63 6f 64 69 6e 67 d.pMem, encoding
1ee65 29 3b 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f );. UPDATE_MAX_
1ee66 42 4c 4f 42 53 49 5a 45 28 75 2e 63 64 2e 70 4d BLOBSIZE(u.cd.pM
1ee67 65 6d 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 em);. if( sqlit
1ee68 65 33 56 64 62 65 4d 65 6d 54 6f 6f 42 69 67 28 e3VdbeMemTooBig(
1ee69 75 2e 63 64 2e 70 4d 65 6d 29 20 29 7b 0a 20 20 u.cd.pMem) ){.
1ee6a 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a goto too_big;.
1ee6b 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a }. break;.}..
1ee6c 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 .#if !defined(SQ
1ee6d 4c 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d LITE_OMIT_VACUUM
1ee6e 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 ) && !defined(SQ
1ee6f 4c 49 54 45 5f 4f 4d 49 54 5f 41 54 54 41 43 48 LITE_OMIT_ATTACH
1ee70 29 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 61 63 )./* Opcode: Vac
1ee71 75 75 6d 20 2a 20 2a 20 2a 20 2a 20 2a 0a 2a 2a uum * * * * *.**
1ee72 0a 2a 2a 20 56 61 63 75 75 6d 20 74 68 65 20 65 .** Vacuum the e
1ee73 6e 74 69 72 65 20 64 61 74 61 62 61 73 65 2e 20 ntire database.
1ee74 20 54 68 69 73 20 6f 70 63 6f 64 65 20 77 69 6c This opcode wil
1ee75 6c 20 63 61 75 73 65 20 6f 74 68 65 72 20 76 69 l cause other vi
1ee76 72 74 75 61 6c 0a 2a 2a 20 6d 61 63 68 69 6e 65 rtual.** machine
1ee77 73 20 74 6f 20 62 65 20 63 72 65 61 74 65 64 20 s to be created
1ee78 61 6e 64 20 72 75 6e 2e 20 20 49 74 20 6d 61 79 and run. It may
1ee79 20 6e 6f 74 20 62 65 20 63 61 6c 6c 65 64 20 66 not be called f
1ee7a 72 6f 6d 20 77 69 74 68 69 6e 0a 2a 2a 20 61 20 rom within.** a
1ee7b 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2f 0a transaction..*/.
1ee7c 63 61 73 65 20 4f 50 5f 56 61 63 75 75 6d 3a 20 case OP_Vacuum:
1ee7d 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 {. if( sqlite3S
1ee7e 61 66 65 74 79 4f 66 66 28 64 62 29 20 29 20 67 afetyOff(db) ) g
1ee7f 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f oto abort_due_to
1ee80 5f 6d 69 73 75 73 65 3b 20 0a 20 20 72 63 20 3d _misuse; . rc =
1ee81 20 73 71 6c 69 74 65 33 52 75 6e 56 61 63 75 75 sqlite3RunVacuu
1ee82 6d 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 m(&p->zErrMsg, d
1ee83 62 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 b);. if( sqlite
1ee84 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29 20 3SafetyOn(db) )
1ee85 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 goto abort_due_t
1ee86 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 62 72 65 61 o_misuse;. brea
1ee87 6b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 k;.}.#endif..#if
1ee88 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 !defined(SQLITE
1ee89 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d _OMIT_AUTOVACUUM
1ee8a 29 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 6e 63 )./* Opcode: Inc
1ee8b 72 56 61 63 75 75 6d 20 50 31 20 50 32 20 2a 20 rVacuum P1 P2 *
1ee8c 2a 20 2a 0a 2a 2a 0a 2a 2a 20 50 65 72 66 6f 72 * *.**.** Perfor
1ee8d 6d 20 61 20 73 69 6e 67 6c 65 20 73 74 65 70 20 m a single step
1ee8e 6f 66 20 74 68 65 20 69 6e 63 72 65 6d 65 6e 74 of the increment
1ee8f 61 6c 20 76 61 63 75 75 6d 20 70 72 6f 63 65 64 al vacuum proced
1ee90 75 72 65 20 6f 6e 0a 2a 2a 20 74 68 65 20 50 31 ure on.** the P1
1ee91 20 64 61 74 61 62 61 73 65 2e 20 49 66 20 74 68 database. If th
1ee92 65 20 76 61 63 75 75 6d 20 68 61 73 20 66 69 6e e vacuum has fin
1ee93 69 73 68 65 64 2c 20 6a 75 6d 70 20 74 6f 20 69 ished, jump to i
1ee94 6e 73 74 72 75 63 74 69 6f 6e 0a 2a 2a 20 50 32 nstruction.** P2
1ee95 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 66 61 6c . Otherwise, fal
1ee96 6c 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 l through to the
1ee97 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f next instructio
1ee98 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 6e n..*/.case OP_In
1ee99 63 72 56 61 63 75 75 6d 3a 20 7b 20 20 20 20 20 crVacuum: {
1ee9a 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 23 69 /* jump */.#i
1ee9b 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 f 0 /* local va
1ee9c 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1ee9d 74 6f 20 75 2e 63 65 20 2a 2f 0a 20 20 42 74 72 to u.ce */. Btr
1ee9e 65 65 20 2a 70 42 74 3b 0a 23 65 6e 64 69 66 20 ee *pBt;.#endif
1ee9f 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c /* local variabl
1eea0 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e es moved into u.
1eea1 63 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 ce */.. assert(
1eea2 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 pOp->p1>=0 && p
1eea3 4f 70 2d 3e 70 31 3c 64 62 2d 3e 6e 44 62 20 29 Op->p1<db->nDb )
1eea4 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e ;. assert( (p->
1eea5 62 74 72 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c btreeMask & (1<<
1eea6 70 4f 70 2d 3e 70 31 29 29 21 3d 30 20 29 3b 0a pOp->p1))!=0 );.
1eea7 20 20 75 2e 63 65 2e 70 42 74 20 3d 20 64 62 2d u.ce.pBt = db-
1eea8 3e 61 44 62 5b 70 4f 70 2d 3e 70 31 5d 2e 70 42 >aDb[pOp->p1].pB
1eea9 74 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 t;. rc = sqlite
1eeaa 33 42 74 72 65 65 49 6e 63 72 56 61 63 75 75 6d 3BtreeIncrVacuum
1eeab 28 75 2e 63 65 2e 70 42 74 29 3b 0a 20 20 69 66 (u.ce.pBt);. if
1eeac 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e ( rc==SQLITE_DON
1eead 45 20 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f E ){. pc = pO
1eeae 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 72 p->p2 - 1;. r
1eeaf 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
1eeb0 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 }. break;.}.#e
1eeb1 6e 64 69 66 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a ndif../* Opcode:
1eeb2 20 45 78 70 69 72 65 20 50 31 20 2a 20 2a 20 2a Expire P1 * * *
1eeb3 20 2a 0a 2a 2a 0a 2a 2a 20 43 61 75 73 65 20 70 *.**.** Cause p
1eeb4 72 65 63 6f 6d 70 69 6c 65 64 20 73 74 61 74 65 recompiled state
1eeb5 6d 65 6e 74 73 20 74 6f 20 62 65 63 6f 6d 65 20 ments to become
1eeb6 65 78 70 69 72 65 64 2e 20 41 6e 20 65 78 70 69 expired. An expi
1eeb7 72 65 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a red statement.**
1eeb8 20 66 61 69 6c 73 20 77 69 74 68 20 61 6e 20 65 fails with an e
1eeb9 72 72 6f 72 20 63 6f 64 65 20 6f 66 20 53 51 4c rror code of SQL
1eeba 49 54 45 5f 53 43 48 45 4d 41 20 69 66 20 69 74 ITE_SCHEMA if it
1eebb 20 69 73 20 65 76 65 72 20 65 78 65 63 75 74 65 is ever execute
1eebc 64 20 0a 2a 2a 20 28 76 69 61 20 73 71 6c 69 74 d .** (via sqlit
1eebd 65 33 5f 73 74 65 70 28 29 29 2e 0a 2a 2a 20 0a e3_step())..** .
1eebe 2a 2a 20 49 66 20 50 31 20 69 73 20 30 2c 20 74 ** If P1 is 0, t
1eebf 68 65 6e 20 61 6c 6c 20 53 51 4c 20 73 74 61 74 hen all SQL stat
1eec0 65 6d 65 6e 74 73 20 62 65 63 6f 6d 65 20 65 78 ements become ex
1eec1 70 69 72 65 64 2e 20 49 66 20 50 31 20 69 73 20 pired. If P1 is
1eec2 6e 6f 6e 2d 7a 65 72 6f 2c 0a 2a 2a 20 74 68 65 non-zero,.** the
1eec3 6e 20 6f 6e 6c 79 20 74 68 65 20 63 75 72 72 65 n only the curre
1eec4 6e 74 6c 79 20 65 78 65 63 75 74 69 6e 67 20 73 ntly executing s
1eec5 74 61 74 65 6d 65 6e 74 20 69 73 20 61 66 66 65 tatement is affe
1eec6 63 74 65 64 2e 20 0a 2a 2f 0a 63 61 73 65 20 4f cted. .*/.case O
1eec7 50 5f 45 78 70 69 72 65 3a 20 7b 0a 20 20 69 66 P_Expire: {. if
1eec8 28 20 21 70 4f 70 2d 3e 70 31 20 29 7b 0a 20 20 ( !pOp->p1 ){.
1eec9 20 20 73 71 6c 69 74 65 33 45 78 70 69 72 65 50 sqlite3ExpireP
1eeca 72 65 70 61 72 65 64 53 74 61 74 65 6d 65 6e 74 reparedStatement
1eecb 73 28 64 62 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a s(db);. }else{.
1eecc 20 20 20 20 70 2d 3e 65 78 70 69 72 65 64 20 3d p->expired =
1eecd 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 1;. }. break;
1eece 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 .}..#ifndef SQLI
1eecf 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 TE_OMIT_SHARED_C
1eed0 41 43 48 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ACHE./* Opcode:
1eed1 54 61 62 6c 65 4c 6f 63 6b 20 50 31 20 50 32 20 TableLock P1 P2
1eed2 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 4f 62 P3 P4 *.**.** Ob
1eed3 74 61 69 6e 20 61 20 6c 6f 63 6b 20 6f 6e 20 61 tain a lock on a
1eed4 20 70 61 72 74 69 63 75 6c 61 72 20 74 61 62 6c particular tabl
1eed5 65 2e 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 e. This instruct
1eed6 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 ion is only used
1eed7 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20 73 68 61 when.** the sha
1eed8 72 65 64 2d 63 61 63 68 65 20 66 65 61 74 75 72 red-cache featur
1eed9 65 20 69 73 20 65 6e 61 62 6c 65 64 2e 20 0a 2a e is enabled. .*
1eeda 2a 0a 2a 2a 20 50 31 20 69 73 20 74 68 65 20 69 *.** P1 is the i
1eedb 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61 74 61 ndex of the data
1eedc 62 61 73 65 20 69 6e 20 73 71 6c 69 74 65 33 2e base in sqlite3.
1eedd 61 44 62 5b 5d 20 6f 66 20 74 68 65 20 64 61 74 aDb[] of the dat
1eede 61 62 61 73 65 0a 2a 2a 20 6f 6e 20 77 68 69 63 abase.** on whic
1eedf 68 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 61 63 h the lock is ac
1eee0 71 75 69 72 65 64 2e 20 20 41 20 72 65 61 64 6c quired. A readl
1eee1 6f 63 6b 20 69 73 20 6f 62 74 61 69 6e 65 64 20 ock is obtained
1eee2 69 66 20 50 33 3d 3d 30 20 6f 72 0a 2a 2a 20 61 if P3==0 or.** a
1eee3 20 77 72 69 74 65 20 6c 6f 63 6b 20 69 66 20 50 write lock if P
1eee4 33 3d 3d 31 2e 0a 2a 2a 0a 2a 2a 20 50 32 20 63 3==1..**.** P2 c
1eee5 6f 6e 74 61 69 6e 73 20 74 68 65 20 72 6f 6f 74 ontains the root
1eee6 2d 70 61 67 65 20 6f 66 20 74 68 65 20 74 61 62 -page of the tab
1eee7 6c 65 20 74 6f 20 6c 6f 63 6b 2e 0a 2a 2a 0a 2a le to lock..**.*
1eee8 2a 20 50 34 20 63 6f 6e 74 61 69 6e 73 20 61 20 * P4 contains a
1eee9 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e pointer to the n
1eeea 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 ame of the table
1eeeb 20 62 65 69 6e 67 20 6c 6f 63 6b 65 64 2e 20 54 being locked. T
1eeec 68 69 73 20 69 73 20 6f 6e 6c 79 0a 2a 2a 20 75 his is only.** u
1eeed 73 65 64 20 74 6f 20 67 65 6e 65 72 61 74 65 20 sed to generate
1eeee 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 an error message
1eeef 20 69 66 20 74 68 65 20 6c 6f 63 6b 20 63 61 6e if the lock can
1eef0 6e 6f 74 20 62 65 20 6f 62 74 61 69 6e 65 64 2e not be obtained.
1eef1 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 54 61 62 6c .*/.case OP_Tabl
1eef2 65 4c 6f 63 6b 3a 20 7b 0a 20 20 75 38 20 69 73 eLock: {. u8 is
1eef3 57 72 69 74 65 4c 6f 63 6b 20 3d 20 28 75 38 29 WriteLock = (u8)
1eef4 70 4f 70 2d 3e 70 33 3b 0a 20 20 69 66 28 20 69 pOp->p3;. if( i
1eef5 73 57 72 69 74 65 4c 6f 63 6b 20 7c 7c 20 30 3d sWriteLock || 0=
1eef6 3d 28 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 =(db->flags&SQLI
1eef7 54 45 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 TE_ReadUncommitt
1eef8 65 64 29 20 29 7b 0a 20 20 20 20 69 6e 74 20 70 ed) ){. int p
1eef9 31 20 3d 20 70 4f 70 2d 3e 70 31 3b 20 0a 20 20 1 = pOp->p1; .
1eefa 20 20 61 73 73 65 72 74 28 20 70 31 3e 3d 30 20 assert( p1>=0
1eefb 26 26 20 70 31 3c 64 62 2d 3e 6e 44 62 20 29 3b && p1<db->nDb );
1eefc 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70 2d . assert( (p-
1eefd 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28 31 3c >btreeMask & (1<
1eefe 3c 70 31 29 29 21 3d 30 20 29 3b 0a 20 20 20 20 <p1))!=0 );.
1eeff 61 73 73 65 72 74 28 20 69 73 57 72 69 74 65 4c assert( isWriteL
1ef00 6f 63 6b 3d 3d 30 20 7c 7c 20 69 73 57 72 69 74 ock==0 || isWrit
1ef01 65 4c 6f 63 6b 3d 3d 31 20 29 3b 0a 20 20 20 20 eLock==1 );.
1ef02 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 rc = sqlite3Btre
1ef03 65 4c 6f 63 6b 54 61 62 6c 65 28 64 62 2d 3e 61 eLockTable(db->a
1ef04 44 62 5b 70 31 5d 2e 70 42 74 2c 20 70 4f 70 2d Db[p1].pBt, pOp-
1ef05 3e 70 32 2c 20 69 73 57 72 69 74 65 4c 6f 63 6b >p2, isWriteLock
1ef06 29 3b 0a 20 20 20 20 69 66 28 20 28 72 63 26 30 );. if( (rc&0
1ef07 78 46 46 29 3d 3d 53 51 4c 49 54 45 5f 4c 4f 43 xFF)==SQLITE_LOC
1ef08 4b 45 44 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e KED ){. con
1ef09 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 70 4f 70 st char *z = pOp
1ef0a 2d 3e 70 34 2e 7a 3b 0a 20 20 20 20 20 20 73 71 ->p4.z;. sq
1ef0b 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 lite3SetString(&
1ef0c 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 p->zErrMsg, db,
1ef0d 22 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 20 "database table
1ef0e 69 73 20 6c 6f 63 6b 65 64 3a 20 25 73 22 2c 20 is locked: %s",
1ef0f 7a 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 z);. }. }.
1ef10 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 break;.}.#endif
1ef11 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 /* SQLITE_OMIT_S
1ef12 48 41 52 45 44 5f 43 41 43 48 45 20 2a 2f 0a 0a HARED_CACHE */..
1ef13 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1ef14 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 MIT_VIRTUALTABLE
1ef15 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 42 65 67 ./* Opcode: VBeg
1ef16 69 6e 20 2a 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a in * * * P4 *.**
1ef17 0a 2a 2a 20 50 34 20 6d 61 79 20 62 65 20 61 20 .** P4 may be a
1ef18 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 73 71 pointer to an sq
1ef19 6c 69 74 65 33 5f 76 74 61 62 20 73 74 72 75 63 lite3_vtab struc
1ef1a 74 75 72 65 2e 20 49 66 20 73 6f 2c 20 63 61 6c ture. If so, cal
1ef1b 6c 20 74 68 65 20 0a 2a 2a 20 78 42 65 67 69 6e l the .** xBegin
1ef1c 20 6d 65 74 68 6f 64 20 66 6f 72 20 74 68 61 74 method for that
1ef1d 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 6c table..**.** Al
1ef1e 73 6f 2c 20 77 68 65 74 68 65 72 20 6f 72 20 6e so, whether or n
1ef1f 6f 74 20 50 34 20 69 73 20 73 65 74 2c 20 63 68 ot P4 is set, ch
1ef20 65 63 6b 20 74 68 61 74 20 74 68 69 73 20 69 73 eck that this is
1ef21 20 6e 6f 74 20 62 65 69 6e 67 20 63 61 6c 6c 65 not being calle
1ef22 64 20 66 72 6f 6d 0a 2a 2a 20 77 69 74 68 69 6e d from.** within
1ef23 20 61 20 63 61 6c 6c 62 61 63 6b 20 74 6f 20 61 a callback to a
1ef24 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 78 virtual table x
1ef25 53 79 6e 63 28 29 20 6d 65 74 68 6f 64 2e 20 49 Sync() method. I
1ef26 66 20 69 74 20 69 73 2c 20 74 68 65 20 65 72 72 f it is, the err
1ef27 6f 72 0a 2a 2a 20 63 6f 64 65 20 77 69 6c 6c 20 or.** code will
1ef28 62 65 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 be set to SQLITE
1ef29 5f 4c 4f 43 4b 45 44 2e 0a 2a 2f 0a 63 61 73 65 _LOCKED..*/.case
1ef2a 20 4f 50 5f 56 42 65 67 69 6e 3a 20 7b 0a 23 69 OP_VBegin: {.#i
1ef2b 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 f 0 /* local va
1ef2c 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1ef2d 74 6f 20 75 2e 63 66 20 2a 2f 0a 20 20 56 54 61 to u.cf */. VTa
1ef2e 62 6c 65 20 2a 70 56 54 61 62 3b 0a 23 65 6e 64 ble *pVTab;.#end
1ef2f 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 if /* 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 66 20 2a 2f 0a 20 20 75 2e 63 66 2e u.cf */. u.cf.
1ef32 70 56 54 61 62 20 3d 20 70 4f 70 2d 3e 70 34 2e pVTab = pOp->p4.
1ef33 70 56 74 61 62 3b 0a 20 20 72 63 20 3d 20 73 71 pVtab;. rc = sq
1ef34 6c 69 74 65 33 56 74 61 62 42 65 67 69 6e 28 64 lite3VtabBegin(d
1ef35 62 2c 20 75 2e 63 66 2e 70 56 54 61 62 29 3b 0a b, u.cf.pVTab);.
1ef36 20 20 69 66 28 20 75 2e 63 66 2e 70 56 54 61 62 if( u.cf.pVTab
1ef37 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 ){. sqlite3D
1ef38 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 bFree(db, p->zEr
1ef39 72 4d 73 67 29 3b 0a 20 20 20 20 70 2d 3e 7a 45 rMsg);. p->zE
1ef3a 72 72 4d 73 67 20 3d 20 75 2e 63 66 2e 70 56 54 rrMsg = u.cf.pVT
1ef3b 61 62 2d 3e 70 56 74 61 62 2d 3e 7a 45 72 72 4d ab->pVtab->zErrM
1ef3c 73 67 3b 0a 20 20 20 20 75 2e 63 66 2e 70 56 54 sg;. u.cf.pVT
1ef3d 61 62 2d 3e 70 56 74 61 62 2d 3e 7a 45 72 72 4d ab->pVtab->zErrM
1ef3e 73 67 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 62 72 sg = 0;. }. br
1ef3f 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a eak;.}.#endif /*
1ef40 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 SQLITE_OMIT_VIR
1ef41 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 TUALTABLE */..#i
1ef42 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
1ef43 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f T_VIRTUALTABLE./
1ef44 2a 20 4f 70 63 6f 64 65 3a 20 56 43 72 65 61 74 * Opcode: VCreat
1ef45 65 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a e P1 * * P4 *.**
1ef46 0a 2a 2a 20 50 34 20 69 73 20 74 68 65 20 6e 61 .** P4 is the na
1ef47 6d 65 20 6f 66 20 61 20 76 69 72 74 75 61 6c 20 me of a virtual
1ef48 74 61 62 6c 65 20 69 6e 20 64 61 74 61 62 61 73 table in databas
1ef49 65 20 50 31 2e 20 43 61 6c 6c 20 74 68 65 20 78 e P1. Call the x
1ef4a 43 72 65 61 74 65 20 6d 65 74 68 6f 64 0a 2a 2a Create method.**
1ef4b 20 66 6f 72 20 74 68 61 74 20 74 61 62 6c 65 2e for that table.
1ef4c 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 43 72 65 .*/.case OP_VCre
1ef4d 61 74 65 3a 20 7b 0a 20 20 72 63 20 3d 20 73 71 ate: {. rc = sq
1ef4e 6c 69 74 65 33 56 74 61 62 43 61 6c 6c 43 72 65 lite3VtabCallCre
1ef4f 61 74 65 28 64 62 2c 20 70 4f 70 2d 3e 70 31 2c ate(db, pOp->p1,
1ef50 20 70 4f 70 2d 3e 70 34 2e 7a 2c 20 26 70 2d 3e pOp->p4.z, &p->
1ef51 7a 45 72 72 4d 73 67 29 3b 0a 20 20 62 72 65 61 zErrMsg);. brea
1ef52 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 k;.}.#endif /* S
1ef53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 QLITE_OMIT_VIRTU
1ef54 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e ALTABLE */..#ifn
1ef55 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1ef56 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 VIRTUALTABLE./*
1ef57 4f 70 63 6f 64 65 3a 20 56 44 65 73 74 72 6f 79 Opcode: VDestroy
1ef58 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a P1 * * P4 *.**.
1ef59 2a 2a 20 50 34 20 69 73 20 74 68 65 20 6e 61 6d ** P4 is the nam
1ef5a 65 20 6f 66 20 61 20 76 69 72 74 75 61 6c 20 74 e of a virtual t
1ef5b 61 62 6c 65 20 69 6e 20 64 61 74 61 62 61 73 65 able in database
1ef5c 20 50 31 2e 20 20 43 61 6c 6c 20 74 68 65 20 78 P1. Call the x
1ef5d 44 65 73 74 72 6f 79 20 6d 65 74 68 6f 64 0a 2a Destroy method.*
1ef5e 2a 20 6f 66 20 74 68 61 74 20 74 61 62 6c 65 2e * of that table.
1ef5f 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 44 65 73 .*/.case OP_VDes
1ef60 74 72 6f 79 3a 20 7b 0a 20 20 70 2d 3e 69 6e 56 troy: {. p->inV
1ef61 74 61 62 4d 65 74 68 6f 64 20 3d 20 32 3b 0a 20 tabMethod = 2;.
1ef62 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 74 61 rc = sqlite3Vta
1ef63 62 43 61 6c 6c 44 65 73 74 72 6f 79 28 64 62 2c bCallDestroy(db,
1ef64 20 70 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70 pOp->p1, pOp->p
1ef65 34 2e 7a 29 3b 0a 20 20 70 2d 3e 69 6e 56 74 61 4.z);. p->inVta
1ef66 62 4d 65 74 68 6f 64 20 3d 20 30 3b 0a 20 20 62 bMethod = 0;. b
1ef67 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f reak;.}.#endif /
1ef68 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 * SQLITE_OMIT_VI
1ef69 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 RTUALTABLE */..#
1ef6a 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1ef6b 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a IT_VIRTUALTABLE.
1ef6c 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 4f 70 65 6e /* Opcode: VOpen
1ef6d 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a P1 * * P4 *.**.
1ef6e 2a 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 ** P4 is a point
1ef6f 65 72 20 74 6f 20 61 20 76 69 72 74 75 61 6c 20 er to a virtual
1ef70 74 61 62 6c 65 20 6f 62 6a 65 63 74 2c 20 61 6e table object, an
1ef71 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 73 74 sqlite3_vtab st
1ef72 72 75 63 74 75 72 65 2e 0a 2a 2a 20 50 31 20 69 ructure..** P1 i
1ef73 73 20 61 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 s a cursor numbe
1ef74 72 2e 20 20 54 68 69 73 20 6f 70 63 6f 64 65 20 r. This opcode
1ef75 6f 70 65 6e 73 20 61 20 63 75 72 73 6f 72 20 74 opens a cursor t
1ef76 6f 20 74 68 65 20 76 69 72 74 75 61 6c 0a 2a 2a o the virtual.**
1ef77 20 74 61 62 6c 65 20 61 6e 64 20 73 74 6f 72 65 table and store
1ef78 73 20 74 68 61 74 20 63 75 72 73 6f 72 20 69 6e s that cursor in
1ef79 20 50 31 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f P1..*/.case OP_
1ef7a 56 4f 70 65 6e 3a 20 7b 0a 23 69 66 20 30 20 20 VOpen: {.#if 0
1ef7b 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c /* local variabl
1ef7c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e es moved into u.
1ef7d 63 67 20 2a 2f 0a 20 20 56 64 62 65 43 75 72 73 cg */. VdbeCurs
1ef7e 6f 72 20 2a 70 43 75 72 3b 0a 20 20 73 71 6c 69 or *pCur;. sqli
1ef7f 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 te3_vtab_cursor
1ef80 2a 70 56 74 61 62 43 75 72 73 6f 72 3b 0a 20 20 *pVtabCursor;.
1ef81 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 sqlite3_vtab *pV
1ef82 74 61 62 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d tab;. sqlite3_m
1ef83 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a odule *pModule;.
1ef84 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 #endif /* local
1ef85 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 variables moved
1ef86 69 6e 74 6f 20 75 2e 63 67 20 2a 2f 0a 0a 20 20 into u.cg */..
1ef87 75 2e 63 67 2e 70 43 75 72 20 3d 20 30 3b 0a 20 u.cg.pCur = 0;.
1ef88 20 75 2e 63 67 2e 70 56 74 61 62 43 75 72 73 6f u.cg.pVtabCurso
1ef89 72 20 3d 20 30 3b 0a 20 20 75 2e 63 67 2e 70 56 r = 0;. u.cg.pV
1ef8a 74 61 62 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 56 tab = pOp->p4.pV
1ef8b 74 61 62 2d 3e 70 56 74 61 62 3b 0a 20 20 75 2e tab->pVtab;. u.
1ef8c 63 67 2e 70 4d 6f 64 75 6c 65 20 3d 20 28 73 71 cg.pModule = (sq
1ef8d 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 29 75 lite3_module *)u
1ef8e 2e 63 67 2e 70 56 74 61 62 2d 3e 70 4d 6f 64 75 .cg.pVtab->pModu
1ef8f 6c 65 3b 0a 20 20 61 73 73 65 72 74 28 75 2e 63 le;. assert(u.c
1ef90 67 2e 70 56 74 61 62 20 26 26 20 75 2e 63 67 2e g.pVtab && u.cg.
1ef91 70 4d 6f 64 75 6c 65 29 3b 0a 20 20 69 66 28 20 pModule);. if(
1ef92 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 sqlite3SafetyOff
1ef93 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 (db) ) goto abor
1ef94 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b t_due_to_misuse;
1ef95 0a 20 20 72 63 20 3d 20 75 2e 63 67 2e 70 4d 6f . rc = u.cg.pMo
1ef96 64 75 6c 65 2d 3e 78 4f 70 65 6e 28 75 2e 63 67 dule->xOpen(u.cg
1ef97 2e 70 56 74 61 62 2c 20 26 75 2e 63 67 2e 70 56 .pVtab, &u.cg.pV
1ef98 74 61 62 43 75 72 73 6f 72 29 3b 0a 20 20 73 71 tabCursor);. sq
1ef99 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 lite3DbFree(db,
1ef9a 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 70 p->zErrMsg);. p
1ef9b 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 75 2e 63 67 ->zErrMsg = u.cg
1ef9c 2e 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 3b .pVtab->zErrMsg;
1ef9d 0a 20 20 75 2e 63 67 2e 70 56 74 61 62 2d 3e 7a . u.cg.pVtab->z
1ef9e 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 69 66 ErrMsg = 0;. if
1ef9f 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f ( sqlite3SafetyO
1efa0 6e 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f n(db) ) goto abo
1efa1 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 rt_due_to_misuse
1efa2 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f ;. if( SQLITE_O
1efa3 4b 3d 3d 72 63 20 29 7b 0a 20 20 20 20 2f 2a 20 K==rc ){. /*
1efa4 49 6e 69 74 69 61 6c 69 7a 65 20 73 71 6c 69 74 Initialize sqlit
1efa5 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 62 e3_vtab_cursor b
1efa6 61 73 65 20 63 6c 61 73 73 20 2a 2f 0a 20 20 20 ase class */.
1efa7 20 75 2e 63 67 2e 70 56 74 61 62 43 75 72 73 6f u.cg.pVtabCurso
1efa8 72 2d 3e 70 56 74 61 62 20 3d 20 75 2e 63 67 2e r->pVtab = u.cg.
1efa9 70 56 74 61 62 3b 0a 0a 20 20 20 20 2f 2a 20 49 pVtab;.. /* I
1efaa 6e 69 74 69 61 6c 69 73 65 20 76 64 62 65 20 63 nitialise vdbe c
1efab 75 72 73 6f 72 20 6f 62 6a 65 63 74 20 2a 2f 0a ursor object */.
1efac 20 20 20 20 75 2e 63 67 2e 70 43 75 72 20 3d 20 u.cg.pCur =
1efad 61 6c 6c 6f 63 61 74 65 43 75 72 73 6f 72 28 70 allocateCursor(p
1efae 2c 20 70 4f 70 2d 3e 70 31 2c 20 30 2c 20 2d 31 , pOp->p1, 0, -1
1efaf 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 75 2e , 0);. if( u.
1efb0 63 67 2e 70 43 75 72 20 29 7b 0a 20 20 20 20 20 cg.pCur ){.
1efb1 20 75 2e 63 67 2e 70 43 75 72 2d 3e 70 56 74 61 u.cg.pCur->pVta
1efb2 62 43 75 72 73 6f 72 20 3d 20 75 2e 63 67 2e 70 bCursor = u.cg.p
1efb3 56 74 61 62 43 75 72 73 6f 72 3b 0a 20 20 20 20 VtabCursor;.
1efb4 20 20 75 2e 63 67 2e 70 43 75 72 2d 3e 70 4d 6f u.cg.pCur->pMo
1efb5 64 75 6c 65 20 3d 20 75 2e 63 67 2e 70 56 74 61 dule = u.cg.pVta
1efb6 62 43 75 72 73 6f 72 2d 3e 70 56 74 61 62 2d 3e bCursor->pVtab->
1efb7 70 4d 6f 64 75 6c 65 3b 0a 20 20 20 20 7d 65 6c pModule;. }el
1efb8 73 65 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61 se{. db->ma
1efb9 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a llocFailed = 1;.
1efba 20 20 20 20 20 20 75 2e 63 67 2e 70 4d 6f 64 75 u.cg.pModu
1efbb 6c 65 2d 3e 78 43 6c 6f 73 65 28 75 2e 63 67 2e le->xClose(u.cg.
1efbc 70 56 74 61 62 43 75 72 73 6f 72 29 3b 0a 20 20 pVtabCursor);.
1efbd 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b }. }. break;
1efbe 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c .}.#endif /* SQL
1efbf 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c ITE_OMIT_VIRTUAL
1efc0 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 TABLE */..#ifnde
1efc1 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 f SQLITE_OMIT_VI
1efc2 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 RTUALTABLE./* Op
1efc3 63 6f 64 65 3a 20 56 46 69 6c 74 65 72 20 50 31 code: VFilter P1
1efc4 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a P2 P3 P4 *.**.*
1efc5 2a 20 50 31 20 69 73 20 61 20 63 75 72 73 6f 72 * P1 is a cursor
1efc6 20 6f 70 65 6e 65 64 20 75 73 69 6e 67 20 56 4f opened using VO
1efc7 70 65 6e 2e 20 20 50 32 20 69 73 20 61 6e 20 61 pen. P2 is an a
1efc8 64 64 72 65 73 73 20 74 6f 20 6a 75 6d 70 20 74 ddress to jump t
1efc9 6f 20 69 66 0a 2a 2a 20 74 68 65 20 66 69 6c 74 o if.** the filt
1efca 65 72 65 64 20 72 65 73 75 6c 74 20 73 65 74 20 ered result set
1efcb 69 73 20 65 6d 70 74 79 2e 0a 2a 2a 0a 2a 2a 20 is empty..**.**
1efcc 50 34 20 69 73 20 65 69 74 68 65 72 20 4e 55 4c P4 is either NUL
1efcd 4c 20 6f 72 20 61 20 73 74 72 69 6e 67 20 74 68 L or a string th
1efce 61 74 20 77 61 73 20 67 65 6e 65 72 61 74 65 64 at was generated
1efcf 20 62 79 20 74 68 65 20 78 42 65 73 74 49 6e 64 by the xBestInd
1efd0 65 78 0a 2a 2a 20 6d 65 74 68 6f 64 20 6f 66 20 ex.** method of
1efd1 74 68 65 20 6d 6f 64 75 6c 65 2e 20 20 54 68 65 the module. The
1efd2 20 69 6e 74 65 72 70 72 65 74 61 74 69 6f 6e 20 interpretation
1efd3 6f 66 20 74 68 65 20 50 34 20 73 74 72 69 6e 67 of the P4 string
1efd4 20 69 73 20 6c 65 66 74 0a 2a 2a 20 74 6f 20 74 is left.** to t
1efd5 68 65 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d he module implem
1efd6 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 entation..**.**
1efd7 54 68 69 73 20 6f 70 63 6f 64 65 20 69 6e 76 6f This opcode invo
1efd8 6b 65 73 20 74 68 65 20 78 46 69 6c 74 65 72 20 kes the xFilter
1efd9 6d 65 74 68 6f 64 20 6f 6e 20 74 68 65 20 76 69 method on the vi
1efda 72 74 75 61 6c 20 74 61 62 6c 65 20 73 70 65 63 rtual table spec
1efdb 69 66 69 65 64 0a 2a 2a 20 62 79 20 50 31 2e 20 ified.** by P1.
1efdc 20 54 68 65 20 69 6e 74 65 67 65 72 20 71 75 65 The integer que
1efdd 72 79 20 70 6c 61 6e 20 70 61 72 61 6d 65 74 65 ry plan paramete
1efde 72 20 74 6f 20 78 46 69 6c 74 65 72 20 69 73 20 r to xFilter is
1efdf 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74 stored in regist
1efe0 65 72 0a 2a 2a 20 50 33 2e 20 52 65 67 69 73 74 er.** P3. Regist
1efe1 65 72 20 50 33 2b 31 20 73 74 6f 72 65 73 20 74 er P3+1 stores t
1efe2 68 65 20 61 72 67 63 20 70 61 72 61 6d 65 74 65 he argc paramete
1efe3 72 20 74 6f 20 62 65 20 70 61 73 73 65 64 20 74 r to be passed t
1efe4 6f 20 74 68 65 0a 2a 2a 20 78 46 69 6c 74 65 72 o the.** xFilter
1efe5 20 6d 65 74 68 6f 64 2e 20 52 65 67 69 73 74 65 method. Registe
1efe6 72 73 20 50 33 2b 32 2e 2e 50 33 2b 31 2b 61 72 rs P3+2..P3+1+ar
1efe7 67 63 20 61 72 65 20 74 68 65 20 61 72 67 63 0a gc are the argc.
1efe8 2a 2a 20 61 64 64 69 74 69 6f 6e 61 6c 20 70 61 ** additional pa
1efe9 72 61 6d 65 74 65 72 73 20 77 68 69 63 68 20 61 rameters which a
1efea 72 65 20 70 61 73 73 65 64 20 74 6f 0a 2a 2a 20 re passed to.**
1efeb 78 46 69 6c 74 65 72 20 61 73 20 61 72 67 76 2e xFilter as argv.
1efec 20 52 65 67 69 73 74 65 72 20 50 33 2b 32 20 62 Register P3+2 b
1efed 65 63 6f 6d 65 73 20 61 72 67 76 5b 30 5d 20 77 ecomes argv[0] w
1efee 68 65 6e 20 70 61 73 73 65 64 20 74 6f 20 78 46 hen passed to xF
1efef 69 6c 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 20 6a ilter..**.** A j
1eff0 75 6d 70 20 69 73 20 6d 61 64 65 20 74 6f 20 50 ump is made to P
1eff1 32 20 69 66 20 74 68 65 20 72 65 73 75 6c 74 20 2 if the result
1eff2 73 65 74 20 61 66 74 65 72 20 66 69 6c 74 65 72 set after filter
1eff3 69 6e 67 20 77 6f 75 6c 64 20 62 65 20 65 6d 70 ing would be emp
1eff4 74 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 ty..*/.case OP_V
1eff5 46 69 6c 74 65 72 3a 20 7b 20 20 20 2f 2a 20 6a Filter: { /* j
1eff6 75 6d 70 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a ump */.#if 0 /*
1eff7 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 local variables
1eff8 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 68 moved into u.ch
1eff9 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 72 67 3b 0a */. int nArg;.
1effa 20 20 69 6e 74 20 69 51 75 65 72 79 3b 0a 20 20 int iQuery;.
1effb 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f const sqlite3_mo
1effc 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20 dule *pModule;.
1effd 20 4d 65 6d 20 2a 70 51 75 65 72 79 3b 0a 20 20 Mem *pQuery;.
1effe 4d 65 6d 20 2a 70 41 72 67 63 3b 0a 20 20 73 71 Mem *pArgc;. sq
1efff 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f lite3_vtab_curso
1f000 72 20 2a 70 56 74 61 62 43 75 72 73 6f 72 3b 0a r *pVtabCursor;.
1f001 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a sqlite3_vtab *
1f002 70 56 74 61 62 3b 0a 20 20 56 64 62 65 43 75 72 pVtab;. VdbeCur
1f003 73 6f 72 20 2a 70 43 75 72 3b 0a 20 20 69 6e 74 sor *pCur;. int
1f004 20 72 65 73 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 res;. int i;.
1f005 20 4d 65 6d 20 2a 2a 61 70 41 72 67 3b 0a 23 65 Mem **apArg;.#e
1f006 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 ndif /* local va
1f007 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1f008 74 6f 20 75 2e 63 68 20 2a 2f 0a 0a 20 20 75 2e to u.ch */.. u.
1f009 63 68 2e 70 51 75 65 72 79 20 3d 20 26 70 2d 3e ch.pQuery = &p->
1f00a 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 aMem[pOp->p3];.
1f00b 20 75 2e 63 68 2e 70 41 72 67 63 20 3d 20 26 75 u.ch.pArgc = &u
1f00c 2e 63 68 2e 70 51 75 65 72 79 5b 31 5d 3b 0a 20 .ch.pQuery[1];.
1f00d 20 75 2e 63 68 2e 70 43 75 72 20 3d 20 70 2d 3e u.ch.pCur = p->
1f00e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a apCsr[pOp->p1];.
1f00f 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 REGISTER_TRACE
1f010 28 70 4f 70 2d 3e 70 33 2c 20 75 2e 63 68 2e 70 (pOp->p3, u.ch.p
1f011 51 75 65 72 79 29 3b 0a 20 20 61 73 73 65 72 74 Query);. assert
1f012 28 20 75 2e 63 68 2e 70 43 75 72 2d 3e 70 56 74 ( u.ch.pCur->pVt
1f013 61 62 43 75 72 73 6f 72 20 29 3b 0a 20 20 75 2e abCursor );. u.
1f014 63 68 2e 70 56 74 61 62 43 75 72 73 6f 72 20 3d ch.pVtabCursor =
1f015 20 75 2e 63 68 2e 70 43 75 72 2d 3e 70 56 74 61 u.ch.pCur->pVta
1f016 62 43 75 72 73 6f 72 3b 0a 20 20 75 2e 63 68 2e bCursor;. u.ch.
1f017 70 56 74 61 62 20 3d 20 75 2e 63 68 2e 70 56 74 pVtab = u.ch.pVt
1f018 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 61 62 3b abCursor->pVtab;
1f019 0a 20 20 75 2e 63 68 2e 70 4d 6f 64 75 6c 65 20 . u.ch.pModule
1f01a 3d 20 75 2e 63 68 2e 70 56 74 61 62 2d 3e 70 4d = u.ch.pVtab->pM
1f01b 6f 64 75 6c 65 3b 0a 0a 20 20 2f 2a 20 47 72 61 odule;.. /* Gra
1f01c 62 20 74 68 65 20 69 6e 64 65 78 20 6e 75 6d 62 b the index numb
1f01d 65 72 20 61 6e 64 20 61 72 67 63 20 70 61 72 61 er and argc para
1f01e 6d 65 74 65 72 73 20 2a 2f 0a 20 20 61 73 73 65 meters */. asse
1f01f 72 74 28 20 28 75 2e 63 68 2e 70 51 75 65 72 79 rt( (u.ch.pQuery
1f020 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 49 6e 74 29 ->flags&MEM_Int)
1f021 21 3d 30 20 26 26 20 75 2e 63 68 2e 70 41 72 67 !=0 && u.ch.pArg
1f022 63 2d 3e 66 6c 61 67 73 3d 3d 4d 45 4d 5f 49 6e c->flags==MEM_In
1f023 74 20 29 3b 0a 20 20 75 2e 63 68 2e 6e 41 72 67 t );. u.ch.nArg
1f024 20 3d 20 28 69 6e 74 29 75 2e 63 68 2e 70 41 72 = (int)u.ch.pAr
1f025 67 63 2d 3e 75 2e 69 3b 0a 20 20 75 2e 63 68 2e gc->u.i;. u.ch.
1f026 69 51 75 65 72 79 20 3d 20 28 69 6e 74 29 75 2e iQuery = (int)u.
1f027 63 68 2e 70 51 75 65 72 79 2d 3e 75 2e 69 3b 0a ch.pQuery->u.i;.
1f028 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65 . /* Invoke the
1f029 20 78 46 69 6c 74 65 72 20 6d 65 74 68 6f 64 20 xFilter method
1f02a 2a 2f 0a 20 20 7b 0a 20 20 20 20 75 2e 63 68 2e */. {. u.ch.
1f02b 72 65 73 20 3d 20 30 3b 0a 20 20 20 20 75 2e 63 res = 0;. u.c
1f02c 68 2e 61 70 41 72 67 20 3d 20 70 2d 3e 61 70 41 h.apArg = p->apA
1f02d 72 67 3b 0a 20 20 20 20 66 6f 72 28 75 2e 63 68 rg;. for(u.ch
1f02e 2e 69 20 3d 20 30 3b 20 75 2e 63 68 2e 69 3c 75 .i = 0; u.ch.i<u
1f02f 2e 63 68 2e 6e 41 72 67 3b 20 75 2e 63 68 2e 69 .ch.nArg; u.ch.i
1f030 2b 2b 29 7b 0a 20 20 20 20 20 20 75 2e 63 68 2e ++){. u.ch.
1f031 61 70 41 72 67 5b 75 2e 63 68 2e 69 5d 20 3d 20 apArg[u.ch.i] =
1f032 26 75 2e 63 68 2e 70 41 72 67 63 5b 75 2e 63 68 &u.ch.pArgc[u.ch
1f033 2e 69 2b 31 5d 3b 0a 20 20 20 20 20 20 73 74 6f .i+1];. sto
1f034 72 65 54 79 70 65 49 6e 66 6f 28 75 2e 63 68 2e reTypeInfo(u.ch.
1f035 61 70 41 72 67 5b 75 2e 63 68 2e 69 5d 2c 20 30 apArg[u.ch.i], 0
1f036 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 );. }.. if
1f037 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f ( sqlite3SafetyO
1f038 66 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 ff(db) ) goto ab
1f039 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 ort_due_to_misus
1f03a 65 3b 0a 20 20 20 20 70 2d 3e 69 6e 56 74 61 62 e;. p->inVtab
1f03b 4d 65 74 68 6f 64 20 3d 20 31 3b 0a 20 20 20 20 Method = 1;.
1f03c 72 63 20 3d 20 75 2e 63 68 2e 70 4d 6f 64 75 6c rc = u.ch.pModul
1f03d 65 2d 3e 78 46 69 6c 74 65 72 28 75 2e 63 68 2e e->xFilter(u.ch.
1f03e 70 56 74 61 62 43 75 72 73 6f 72 2c 20 75 2e 63 pVtabCursor, u.c
1f03f 68 2e 69 51 75 65 72 79 2c 20 70 4f 70 2d 3e 70 h.iQuery, pOp->p
1f040 34 2e 7a 2c 20 75 2e 63 68 2e 6e 41 72 67 2c 20 4.z, u.ch.nArg,
1f041 75 2e 63 68 2e 61 70 41 72 67 29 3b 0a 20 20 20 u.ch.apArg);.
1f042 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64 p->inVtabMethod
1f043 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 = 0;. sqlite
1f044 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 3DbFree(db, p->z
1f045 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 70 2d 3e ErrMsg);. p->
1f046 7a 45 72 72 4d 73 67 20 3d 20 75 2e 63 68 2e 70 zErrMsg = u.ch.p
1f047 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 Vtab->zErrMsg;.
1f048 20 20 20 75 2e 63 68 2e 70 56 74 61 62 2d 3e 7a u.ch.pVtab->z
1f049 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 20 20 ErrMsg = 0;.
1f04a 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
1f04b 4b 20 29 7b 0a 20 20 20 20 20 20 75 2e 63 68 2e K ){. u.ch.
1f04c 72 65 73 20 3d 20 75 2e 63 68 2e 70 4d 6f 64 75 res = u.ch.pModu
1f04d 6c 65 2d 3e 78 45 6f 66 28 75 2e 63 68 2e 70 56 le->xEof(u.ch.pV
1f04e 74 61 62 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 tabCursor);.
1f04f 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 }. if( sqlite
1f050 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29 20 3SafetyOn(db) )
1f051 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 goto abort_due_t
1f052 6f 5f 6d 69 73 75 73 65 3b 0a 0a 20 20 20 20 69 o_misuse;.. i
1f053 66 28 20 75 2e 63 68 2e 72 65 73 20 29 7b 0a 20 f( u.ch.res ){.
1f054 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 pc = pOp->p
1f055 32 20 2d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 2 - 1;. }. }
1f056 0a 20 20 75 2e 63 68 2e 70 43 75 72 2d 3e 6e 75 . u.ch.pCur->nu
1f057 6c 6c 52 6f 77 20 3d 20 30 3b 0a 0a 20 20 62 72 llRow = 0;.. br
1f058 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a eak;.}.#endif /*
1f059 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 SQLITE_OMIT_VIR
1f05a 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 TUALTABLE */..#i
1f05b 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
1f05c 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f T_VIRTUALTABLE./
1f05d 2a 20 4f 70 63 6f 64 65 3a 20 56 43 6f 6c 75 6d * Opcode: VColum
1f05e 6e 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a n P1 P2 P3 * *.*
1f05f 2a 0a 2a 2a 20 53 74 6f 72 65 20 74 68 65 20 76 *.** Store the v
1f060 61 6c 75 65 20 6f 66 20 74 68 65 20 50 32 2d 74 alue of the P2-t
1f061 68 20 63 6f 6c 75 6d 6e 20 6f 66 0a 2a 2a 20 74 h column of.** t
1f062 68 65 20 72 6f 77 20 6f 66 20 74 68 65 20 76 69 he row of the vi
1f063 72 74 75 61 6c 2d 74 61 62 6c 65 20 74 68 61 74 rtual-table that
1f064 20 74 68 65 20 0a 2a 2a 20 50 31 20 63 75 72 73 the .** P1 curs
1f065 6f 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 74 or is pointing t
1f066 6f 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 o into register
1f067 50 33 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 P3..*/.case OP_V
1f068 43 6f 6c 75 6d 6e 3a 20 7b 0a 23 69 66 20 30 20 Column: {.#if 0
1f069 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1f06a 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1f06b 2e 63 69 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 .ci */. sqlite3
1f06c 5f 76 74 61 62 20 2a 70 56 74 61 62 3b 0a 20 20 _vtab *pVtab;.
1f06d 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f const sqlite3_mo
1f06e 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20 dule *pModule;.
1f06f 20 4d 65 6d 20 2a 70 44 65 73 74 3b 0a 20 20 73 Mem *pDest;. s
1f070 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 73 qlite3_context s
1f071 43 6f 6e 74 65 78 74 3b 0a 23 65 6e 64 69 66 20 Context;.#endif
1f072 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c /* local variabl
1f073 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e es moved into u.
1f074 63 69 20 2a 2f 0a 0a 20 20 56 64 62 65 43 75 72 ci */.. VdbeCur
1f075 73 6f 72 20 2a 70 43 75 72 20 3d 20 70 2d 3e 61 sor *pCur = p->a
1f076 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 pCsr[pOp->p1];.
1f077 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 70 assert( pCur->p
1f078 56 74 61 62 43 75 72 73 6f 72 20 29 3b 0a 20 20 VtabCursor );.
1f079 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e assert( pOp->p3>
1f07a 30 20 26 26 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 0 && pOp->p3<=p-
1f07b 3e 6e 4d 65 6d 20 29 3b 0a 20 20 75 2e 63 69 2e >nMem );. u.ci.
1f07c 70 44 65 73 74 20 3d 20 26 70 2d 3e 61 4d 65 6d pDest = &p->aMem
1f07d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 69 66 28 [pOp->p3];. if(
1f07e 20 70 43 75 72 2d 3e 6e 75 6c 6c 52 6f 77 20 29 pCur->nullRow )
1f07f 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 {. sqlite3Vdb
1f080 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 75 2e 63 69 eMemSetNull(u.ci
1f081 2e 70 44 65 73 74 29 3b 0a 20 20 20 20 62 72 65 .pDest);. bre
1f082 61 6b 3b 0a 20 20 7d 0a 20 20 75 2e 63 69 2e 70 ak;. }. u.ci.p
1f083 56 74 61 62 20 3d 20 70 43 75 72 2d 3e 70 56 74 Vtab = pCur->pVt
1f084 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 61 62 3b abCursor->pVtab;
1f085 0a 20 20 75 2e 63 69 2e 70 4d 6f 64 75 6c 65 20 . u.ci.pModule
1f086 3d 20 75 2e 63 69 2e 70 56 74 61 62 2d 3e 70 4d = u.ci.pVtab->pM
1f087 6f 64 75 6c 65 3b 0a 20 20 61 73 73 65 72 74 28 odule;. assert(
1f088 20 75 2e 63 69 2e 70 4d 6f 64 75 6c 65 2d 3e 78 u.ci.pModule->x
1f089 43 6f 6c 75 6d 6e 20 29 3b 0a 20 20 6d 65 6d 73 Column );. mems
1f08a 65 74 28 26 75 2e 63 69 2e 73 43 6f 6e 74 65 78 et(&u.ci.sContex
1f08b 74 2c 20 30 2c 20 73 69 7a 65 6f 66 28 75 2e 63 t, 0, sizeof(u.c
1f08c 69 2e 73 43 6f 6e 74 65 78 74 29 29 3b 0a 0a 20 i.sContext));..
1f08d 20 2f 2a 20 54 68 65 20 6f 75 74 70 75 74 20 63 /* The output c
1f08e 65 6c 6c 20 6d 61 79 20 61 6c 72 65 61 64 79 20 ell may already
1f08f 68 61 76 65 20 61 20 62 75 66 66 65 72 20 61 6c have a buffer al
1f090 6c 6f 63 61 74 65 64 2e 20 4d 6f 76 65 0a 20 20 located. Move.
1f091 2a 2a 20 74 68 65 20 63 75 72 72 65 6e 74 20 63 ** the current c
1f092 6f 6e 74 65 6e 74 73 20 74 6f 20 75 2e 63 69 2e ontents to u.ci.
1f093 73 43 6f 6e 74 65 78 74 2e 73 20 73 6f 20 69 6e sContext.s so in
1f094 20 63 61 73 65 20 74 68 65 20 75 73 65 72 2d 66 case the user-f
1f095 75 6e 63 74 69 6f 6e 0a 20 20 2a 2a 20 63 61 6e unction. ** can
1f096 20 75 73 65 20 74 68 65 20 61 6c 72 65 61 64 79 use the already
1f097 20 61 6c 6c 6f 63 61 74 65 64 20 62 75 66 66 65 allocated buffe
1f098 72 20 69 6e 73 74 65 61 64 20 6f 66 20 61 6c 6c r instead of all
1f099 6f 63 61 74 69 6e 67 20 61 0a 20 20 2a 2a 20 6e ocating a. ** n
1f09a 65 77 20 6f 6e 65 2e 0a 20 20 2a 2f 0a 20 20 73 ew one.. */. s
1f09b 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 qlite3VdbeMemMov
1f09c 65 28 26 75 2e 63 69 2e 73 43 6f 6e 74 65 78 74 e(&u.ci.sContext
1f09d 2e 73 2c 20 75 2e 63 69 2e 70 44 65 73 74 29 3b .s, u.ci.pDest);
1f09e 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 . MemSetTypeFla
1f09f 67 28 26 75 2e 63 69 2e 73 43 6f 6e 74 65 78 74 g(&u.ci.sContext
1f0a0 2e 73 2c 20 4d 45 4d 5f 4e 75 6c 6c 29 3b 0a 0a .s, MEM_Null);..
1f0a1 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 if( sqlite3Saf
1f0a2 65 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f 74 etyOff(db) ) got
1f0a3 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d o abort_due_to_m
1f0a4 69 73 75 73 65 3b 0a 20 20 72 63 20 3d 20 75 2e isuse;. rc = u.
1f0a5 63 69 2e 70 4d 6f 64 75 6c 65 2d 3e 78 43 6f 6c ci.pModule->xCol
1f0a6 75 6d 6e 28 70 43 75 72 2d 3e 70 56 74 61 62 43 umn(pCur->pVtabC
1f0a7 75 72 73 6f 72 2c 20 26 75 2e 63 69 2e 73 43 6f ursor, &u.ci.sCo
1f0a8 6e 74 65 78 74 2c 20 70 4f 70 2d 3e 70 32 29 3b ntext, pOp->p2);
1f0a9 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 . sqlite3DbFree
1f0aa 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 (db, p->zErrMsg)
1f0ab 3b 0a 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d ;. p->zErrMsg =
1f0ac 20 75 2e 63 69 2e 70 56 74 61 62 2d 3e 7a 45 72 u.ci.pVtab->zEr
1f0ad 72 4d 73 67 3b 0a 20 20 75 2e 63 69 2e 70 56 74 rMsg;. u.ci.pVt
1f0ae 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b ab->zErrMsg = 0;
1f0af 0a 20 20 69 66 28 20 75 2e 63 69 2e 73 43 6f 6e . if( u.ci.sCon
1f0b0 74 65 78 74 2e 69 73 45 72 72 6f 72 20 29 7b 0a text.isError ){.
1f0b1 20 20 20 20 72 63 20 3d 20 75 2e 63 69 2e 73 43 rc = u.ci.sC
1f0b2 6f 6e 74 65 78 74 2e 69 73 45 72 72 6f 72 3b 0a ontext.isError;.
1f0b3 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 70 79 20 74 }.. /* Copy t
1f0b4 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 he result of the
1f0b5 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 74 68 65 function to the
1f0b6 20 50 33 20 72 65 67 69 73 74 65 72 2e 20 57 65 P3 register. We
1f0b7 0a 20 20 2a 2a 20 64 6f 20 74 68 69 73 20 72 65 . ** do this re
1f0b8 67 61 72 64 6c 65 73 73 20 6f 66 20 77 68 65 74 gardless of whet
1f0b9 68 65 72 20 6f 72 20 6e 6f 74 20 61 6e 20 65 72 her or not an er
1f0ba 72 6f 72 20 6f 63 63 75 72 72 65 64 20 74 6f 20 ror occurred to
1f0bb 65 6e 73 75 72 65 20 61 6e 79 0a 20 20 2a 2a 20 ensure any. **
1f0bc 64 79 6e 61 6d 69 63 20 61 6c 6c 6f 63 61 74 69 dynamic allocati
1f0bd 6f 6e 20 69 6e 20 75 2e 63 69 2e 73 43 6f 6e 74 on in u.ci.sCont
1f0be 65 78 74 2e 73 20 28 61 20 4d 65 6d 20 73 74 72 ext.s (a Mem str
1f0bf 75 63 74 29 20 69 73 20 20 72 65 6c 65 61 73 65 uct) is release
1f0c0 64 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 d.. */. sqlite
1f0c1 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 3VdbeChangeEncod
1f0c2 69 6e 67 28 26 75 2e 63 69 2e 73 43 6f 6e 74 65 ing(&u.ci.sConte
1f0c3 78 74 2e 73 2c 20 65 6e 63 6f 64 69 6e 67 29 3b xt.s, encoding);
1f0c4 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 . REGISTER_TRAC
1f0c5 45 28 70 4f 70 2d 3e 70 33 2c 20 75 2e 63 69 2e E(pOp->p3, u.ci.
1f0c6 70 44 65 73 74 29 3b 0a 20 20 73 71 6c 69 74 65 pDest);. sqlite
1f0c7 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28 75 2e 63 3VdbeMemMove(u.c
1f0c8 69 2e 70 44 65 73 74 2c 20 26 75 2e 63 69 2e 73 i.pDest, &u.ci.s
1f0c9 43 6f 6e 74 65 78 74 2e 73 29 3b 0a 20 20 55 50 Context.s);. UP
1f0ca 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a DATE_MAX_BLOBSIZ
1f0cb 45 28 75 2e 63 69 2e 70 44 65 73 74 29 3b 0a 0a E(u.ci.pDest);..
1f0cc 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 if( sqlite3Saf
1f0cd 65 74 79 4f 6e 28 64 62 29 20 29 7b 0a 20 20 20 etyOn(db) ){.
1f0ce 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f goto abort_due_
1f0cf 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 7d 0a 20 to_misuse;. }.
1f0d0 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 if( sqlite3Vdbe
1f0d1 4d 65 6d 54 6f 6f 42 69 67 28 75 2e 63 69 2e 70 MemTooBig(u.ci.p
1f0d2 44 65 73 74 29 20 29 7b 0a 20 20 20 20 67 6f 74 Dest) ){. got
1f0d3 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a 20 o too_big;. }.
1f0d4 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 break;.}.#endif
1f0d5 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f /* SQLITE_OMIT_
1f0d6 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a VIRTUALTABLE */.
1f0d7 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1f0d8 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c OMIT_VIRTUALTABL
1f0d9 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 4e 65 E./* Opcode: VNe
1f0da 78 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a xt P1 P2 * * *.*
1f0db 2a 0a 2a 2a 20 41 64 76 61 6e 63 65 20 76 69 72 *.** Advance vir
1f0dc 74 75 61 6c 20 74 61 62 6c 65 20 50 31 20 74 6f tual table P1 to
1f0dd 20 74 68 65 20 6e 65 78 74 20 72 6f 77 20 69 6e the next row in
1f0de 20 69 74 73 20 72 65 73 75 6c 74 20 73 65 74 20 its result set
1f0df 61 6e 64 0a 2a 2a 20 6a 75 6d 70 20 74 6f 20 69 and.** jump to i
1f0e0 6e 73 74 72 75 63 74 69 6f 6e 20 50 32 2e 20 20 nstruction P2.
1f0e1 4f 72 2c 20 69 66 20 74 68 65 20 76 69 72 74 75 Or, if the virtu
1f0e2 61 6c 20 74 61 62 6c 65 20 68 61 73 20 72 65 61 al table has rea
1f0e3 63 68 65 64 0a 2a 2a 20 74 68 65 20 65 6e 64 20 ched.** the end
1f0e4 6f 66 20 69 74 73 20 72 65 73 75 6c 74 20 73 65 of its result se
1f0e5 74 2c 20 74 68 65 6e 20 66 61 6c 6c 20 74 68 72 t, then fall thr
1f0e6 6f 75 67 68 20 74 6f 20 74 68 65 20 6e 65 78 74 ough to the next
1f0e7 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f instruction..*/
1f0e8 0a 63 61 73 65 20 4f 50 5f 56 4e 65 78 74 3a 20 .case OP_VNext:
1f0e9 7b 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 23 { /* jump */.#
1f0ea 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 if 0 /* local v
1f0eb 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 ariables moved i
1f0ec 6e 74 6f 20 75 2e 63 6a 20 2a 2f 0a 20 20 73 71 nto u.cj */. sq
1f0ed 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 lite3_vtab *pVta
1f0ee 62 3b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 b;. const sqlit
1f0ef 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 e3_module *pModu
1f0f0 6c 65 3b 0a 20 20 69 6e 74 20 72 65 73 3b 0a 20 le;. int res;.
1f0f1 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 75 VdbeCursor *pCu
1f0f2 72 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 r;.#endif /* loc
1f0f3 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1f0f4 65 64 20 69 6e 74 6f 20 75 2e 63 6a 20 2a 2f 0a ed into u.cj */.
1f0f5 0a 20 20 75 2e 63 6a 2e 72 65 73 20 3d 20 30 3b . u.cj.res = 0;
1f0f6 0a 20 20 75 2e 63 6a 2e 70 43 75 72 20 3d 20 70 . u.cj.pCur = p
1f0f7 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d ->apCsr[pOp->p1]
1f0f8 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 63 6a ;. assert( u.cj
1f0f9 2e 70 43 75 72 2d 3e 70 56 74 61 62 43 75 72 73 .pCur->pVtabCurs
1f0fa 6f 72 20 29 3b 0a 20 20 69 66 28 20 75 2e 63 6a or );. if( u.cj
1f0fb 2e 70 43 75 72 2d 3e 6e 75 6c 6c 52 6f 77 20 29 .pCur->nullRow )
1f0fc 7b 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d {. break;. }
1f0fd 0a 20 20 75 2e 63 6a 2e 70 56 74 61 62 20 3d 20 . u.cj.pVtab =
1f0fe 75 2e 63 6a 2e 70 43 75 72 2d 3e 70 56 74 61 62 u.cj.pCur->pVtab
1f0ff 43 75 72 73 6f 72 2d 3e 70 56 74 61 62 3b 0a 20 Cursor->pVtab;.
1f100 20 75 2e 63 6a 2e 70 4d 6f 64 75 6c 65 20 3d 20 u.cj.pModule =
1f101 75 2e 63 6a 2e 70 56 74 61 62 2d 3e 70 4d 6f 64 u.cj.pVtab->pMod
1f102 75 6c 65 3b 0a 20 20 61 73 73 65 72 74 28 20 75 ule;. assert( u
1f103 2e 63 6a 2e 70 4d 6f 64 75 6c 65 2d 3e 78 4e 65 .cj.pModule->xNe
1f104 78 74 20 29 3b 0a 0a 20 20 2f 2a 20 49 6e 76 6f xt );.. /* Invo
1f105 6b 65 20 74 68 65 20 78 4e 65 78 74 28 29 20 6d ke the xNext() m
1f106 65 74 68 6f 64 20 6f 66 20 74 68 65 20 6d 6f 64 ethod of the mod
1f107 75 6c 65 2e 20 54 68 65 72 65 20 69 73 20 6e 6f ule. There is no
1f108 20 77 61 79 20 66 6f 72 20 74 68 65 0a 20 20 2a way for the. *
1f109 2a 20 75 6e 64 65 72 6c 79 69 6e 67 20 69 6d 70 * underlying imp
1f10a 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 74 6f 20 72 lementation to r
1f10b 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 69 eturn an error i
1f10c 66 20 6f 6e 65 20 6f 63 63 75 72 73 20 64 75 72 f one occurs dur
1f10d 69 6e 67 0a 20 20 2a 2a 20 78 4e 65 78 74 28 29 ing. ** xNext()
1f10e 2e 20 49 6e 73 74 65 61 64 2c 20 69 66 20 61 6e . Instead, if an
1f10f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 74 error occurs, t
1f110 72 75 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 rue is returned
1f111 28 69 6e 64 69 63 61 74 69 6e 67 20 74 68 61 74 (indicating that
1f112 0a 20 20 2a 2a 20 64 61 74 61 20 69 73 20 61 76 . ** data is av
1f113 61 69 6c 61 62 6c 65 29 20 61 6e 64 20 74 68 65 ailable) and the
1f114 20 65 72 72 6f 72 20 63 6f 64 65 20 72 65 74 75 error code retu
1f115 72 6e 65 64 20 77 68 65 6e 20 78 43 6f 6c 75 6d rned when xColum
1f116 6e 20 6f 72 0a 20 20 2a 2a 20 73 6f 6d 65 20 6f n or. ** some o
1f117 74 68 65 72 20 6d 65 74 68 6f 64 20 69 73 20 6e ther method is n
1f118 65 78 74 20 69 6e 76 6f 6b 65 64 20 6f 6e 20 74 ext invoked on t
1f119 68 65 20 73 61 76 65 20 76 69 72 74 75 61 6c 20 he save virtual
1f11a 74 61 62 6c 65 20 63 75 72 73 6f 72 2e 0a 20 20 table cursor..
1f11b 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 */. if( sqlite3
1f11c 53 61 66 65 74 79 4f 66 66 28 64 62 29 20 29 20 SafetyOff(db) )
1f11d 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 goto abort_due_t
1f11e 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 70 2d 3e 69 o_misuse;. p->i
1f11f 6e 56 74 61 62 4d 65 74 68 6f 64 20 3d 20 31 3b nVtabMethod = 1;
1f120 0a 20 20 72 63 20 3d 20 75 2e 63 6a 2e 70 4d 6f . rc = u.cj.pMo
1f121 64 75 6c 65 2d 3e 78 4e 65 78 74 28 75 2e 63 6a dule->xNext(u.cj
1f122 2e 70 43 75 72 2d 3e 70 56 74 61 62 43 75 72 73 .pCur->pVtabCurs
1f123 6f 72 29 3b 0a 20 20 70 2d 3e 69 6e 56 74 61 62 or);. p->inVtab
1f124 4d 65 74 68 6f 64 20 3d 20 30 3b 0a 20 20 73 71 Method = 0;. sq
1f125 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 lite3DbFree(db,
1f126 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 70 p->zErrMsg);. p
1f127 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 75 2e 63 6a ->zErrMsg = u.cj
1f128 2e 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 3b .pVtab->zErrMsg;
1f129 0a 20 20 75 2e 63 6a 2e 70 56 74 61 62 2d 3e 7a . u.cj.pVtab->z
1f12a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 69 66 ErrMsg = 0;. if
1f12b 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
1f12c 29 7b 0a 20 20 20 20 75 2e 63 6a 2e 72 65 73 20 ){. u.cj.res
1f12d 3d 20 75 2e 63 6a 2e 70 4d 6f 64 75 6c 65 2d 3e = u.cj.pModule->
1f12e 78 45 6f 66 28 75 2e 63 6a 2e 70 43 75 72 2d 3e xEof(u.cj.pCur->
1f12f 70 56 74 61 62 43 75 72 73 6f 72 29 3b 0a 20 20 pVtabCursor);.
1f130 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 }. if( sqlite3S
1f131 61 66 65 74 79 4f 6e 28 64 62 29 20 29 20 67 6f afetyOn(db) ) go
1f132 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f to abort_due_to_
1f133 6d 69 73 75 73 65 3b 0a 0a 20 20 69 66 28 20 21 misuse;.. if( !
1f134 75 2e 63 6a 2e 72 65 73 20 29 7b 0a 20 20 20 20 u.cj.res ){.
1f135 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 64 /* If there is d
1f136 61 74 61 2c 20 6a 75 6d 70 20 74 6f 20 50 32 20 ata, jump to P2
1f137 2a 2f 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d */. pc = pOp-
1f138 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 >p2 - 1;. }. b
1f139 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f reak;.}.#endif /
1f13a 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 * SQLITE_OMIT_VI
1f13b 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 RTUALTABLE */..#
1f13c 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1f13d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a IT_VIRTUALTABLE.
1f13e 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 52 65 6e 61 /* Opcode: VRena
1f13f 6d 65 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a 2a me P1 * * P4 *.*
1f140 2a 0a 2a 2a 20 50 34 20 69 73 20 61 20 70 6f 69 *.** P4 is a poi
1f141 6e 74 65 72 20 74 6f 20 61 20 76 69 72 74 75 61 nter to a virtua
1f142 6c 20 74 61 62 6c 65 20 6f 62 6a 65 63 74 2c 20 l table object,
1f143 61 6e 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 an sqlite3_vtab
1f144 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 54 68 structure..** Th
1f145 69 73 20 6f 70 63 6f 64 65 20 69 6e 76 6f 6b 65 is opcode invoke
1f146 73 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 s the correspond
1f147 69 6e 67 20 78 52 65 6e 61 6d 65 20 6d 65 74 68 ing xRename meth
1f148 6f 64 2e 20 54 68 65 20 76 61 6c 75 65 0a 2a 2a od. The value.**
1f149 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 in register P1
1f14a 69 73 20 70 61 73 73 65 64 20 61 73 20 74 68 65 is passed as the
1f14b 20 7a 4e 61 6d 65 20 61 72 67 75 6d 65 6e 74 20 zName argument
1f14c 74 6f 20 74 68 65 20 78 52 65 6e 61 6d 65 20 6d to the xRename m
1f14d 65 74 68 6f 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f ethod..*/.case O
1f14e 50 5f 56 52 65 6e 61 6d 65 3a 20 7b 0a 23 69 66 P_VRename: {.#if
1f14f 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 0 /* local var
1f150 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 iables moved int
1f151 6f 20 75 2e 63 6b 20 2a 2f 0a 20 20 73 71 6c 69 o u.ck */. sqli
1f152 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b te3_vtab *pVtab;
1f153 0a 20 20 4d 65 6d 20 2a 70 4e 61 6d 65 3b 0a 23 . Mem *pName;.#
1f154 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 endif /* local v
1f155 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 ariables moved i
1f156 6e 74 6f 20 75 2e 63 6b 20 2a 2f 0a 0a 20 20 75 nto u.ck */.. u
1f157 2e 63 6b 2e 70 56 74 61 62 20 3d 20 70 4f 70 2d .ck.pVtab = pOp-
1f158 3e 70 34 2e 70 56 74 61 62 2d 3e 70 56 74 61 62 >p4.pVtab->pVtab
1f159 3b 0a 20 20 75 2e 63 6b 2e 70 4e 61 6d 65 20 3d ;. u.ck.pName =
1f15a 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 &p->aMem[pOp->p
1f15b 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 1];. assert( u.
1f15c 63 6b 2e 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c ck.pVtab->pModul
1f15d 65 2d 3e 78 52 65 6e 61 6d 65 20 29 3b 0a 20 20 e->xRename );.
1f15e 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 REGISTER_TRACE(p
1f15f 4f 70 2d 3e 70 31 2c 20 75 2e 63 6b 2e 70 4e 61 Op->p1, u.ck.pNa
1f160 6d 65 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75 me);. assert( u
1f161 2e 63 6b 2e 70 4e 61 6d 65 2d 3e 66 6c 61 67 73 .ck.pName->flags
1f162 20 26 20 4d 45 4d 5f 53 74 72 20 29 3b 0a 20 20 & MEM_Str );.
1f163 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 if( sqlite3Safet
1f164 79 4f 66 66 28 64 62 29 20 29 20 67 6f 74 6f 20 yOff(db) ) goto
1f165 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 abort_due_to_mis
1f166 75 73 65 3b 0a 20 20 72 63 20 3d 20 75 2e 63 6b use;. rc = u.ck
1f167 2e 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 2d .pVtab->pModule-
1f168 3e 78 52 65 6e 61 6d 65 28 75 2e 63 6b 2e 70 56 >xRename(u.ck.pV
1f169 74 61 62 2c 20 75 2e 63 6b 2e 70 4e 61 6d 65 2d tab, u.ck.pName-
1f16a 3e 7a 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 >z);. sqlite3Db
1f16b 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 Free(db, p->zErr
1f16c 4d 73 67 29 3b 0a 20 20 70 2d 3e 7a 45 72 72 4d Msg);. p->zErrM
1f16d 73 67 20 3d 20 75 2e 63 6b 2e 70 56 74 61 62 2d sg = u.ck.pVtab-
1f16e 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 75 2e 63 6b >zErrMsg;. u.ck
1f16f 2e 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20 .pVtab->zErrMsg
1f170 3d 20 30 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 = 0;. if( sqlit
1f171 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29 e3SafetyOn(db) )
1f172 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f goto abort_due_
1f173 74 6f 5f 6d 69 73 75 73 65 3b 0a 0a 20 20 62 72 to_misuse;.. br
1f174 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 eak;.}.#endif..#
1f175 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1f176 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a IT_VIRTUALTABLE.
1f177 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 55 70 64 61 /* Opcode: VUpda
1f178 74 65 20 50 31 20 50 32 20 50 33 20 50 34 20 2a te P1 P2 P3 P4 *
1f179 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 61 20 70 .**.** P4 is a p
1f17a 6f 69 6e 74 65 72 20 74 6f 20 61 20 76 69 72 74 ointer to a virt
1f17b 75 61 6c 20 74 61 62 6c 65 20 6f 62 6a 65 63 74 ual table object
1f17c 2c 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 74 61 , an sqlite3_vta
1f17d 62 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 b structure..**
1f17e 54 68 69 73 20 6f 70 63 6f 64 65 20 69 6e 76 6f This opcode invo
1f17f 6b 65 73 20 74 68 65 20 63 6f 72 72 65 73 70 6f kes the correspo
1f180 6e 64 69 6e 67 20 78 55 70 64 61 74 65 20 6d 65 nding xUpdate me
1f181 74 68 6f 64 2e 20 50 32 20 76 61 6c 75 65 73 0a thod. P2 values.
1f182 2a 2a 20 61 72 65 20 63 6f 6e 74 69 67 75 6f 75 ** are contiguou
1f183 73 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 73 s memory cells s
1f184 74 61 72 74 69 6e 67 20 61 74 20 50 33 20 74 6f tarting at P3 to
1f185 20 70 61 73 73 20 74 6f 20 74 68 65 20 78 55 70 pass to the xUp
1f186 64 61 74 65 20 0a 2a 2a 20 69 6e 76 6f 63 61 74 date .** invocat
1f187 69 6f 6e 2e 20 54 68 65 20 76 61 6c 75 65 20 69 ion. The value i
1f188 6e 20 72 65 67 69 73 74 65 72 20 28 50 33 2b 50 n register (P3+P
1f189 32 2d 31 29 20 63 6f 72 72 65 73 70 6f 6e 64 73 2-1) corresponds
1f18a 20 74 6f 20 74 68 65 20 0a 2a 2a 20 70 32 74 68 to the .** p2th
1f18b 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 element of the
1f18c 61 72 67 76 20 61 72 72 61 79 20 70 61 73 73 65 argv array passe
1f18d 64 20 74 6f 20 78 55 70 64 61 74 65 2e 0a 2a 2a d to xUpdate..**
1f18e 0a 2a 2a 20 54 68 65 20 78 55 70 64 61 74 65 20 .** The xUpdate
1f18f 6d 65 74 68 6f 64 20 77 69 6c 6c 20 64 6f 20 61 method will do a
1f190 20 44 45 4c 45 54 45 20 6f 72 20 61 6e 20 49 4e DELETE or an IN
1f191 53 45 52 54 20 6f 72 20 62 6f 74 68 2e 0a 2a 2a SERT or both..**
1f192 20 54 68 65 20 61 72 67 76 5b 30 5d 20 65 6c 65 The argv[0] ele
1f193 6d 65 6e 74 20 28 77 68 69 63 68 20 63 6f 72 72 ment (which corr
1f194 65 73 70 6f 6e 64 73 20 74 6f 20 6d 65 6d 6f 72 esponds to memor
1f195 79 20 63 65 6c 6c 20 50 33 29 0a 2a 2a 20 69 73 y cell P3).** is
1f196 20 74 68 65 20 72 6f 77 69 64 20 6f 66 20 61 20 the rowid of a
1f197 72 6f 77 20 74 6f 20 64 65 6c 65 74 65 2e 20 20 row to delete.
1f198 49 66 20 61 72 67 76 5b 30 5d 20 69 73 20 4e 55 If argv[0] is NU
1f199 4c 4c 20 74 68 65 6e 20 6e 6f 20 0a 2a 2a 20 64 LL then no .** d
1f19a 65 6c 65 74 69 6f 6e 20 6f 63 63 75 72 73 2e 20 eletion occurs.
1f19b 20 54 68 65 20 61 72 67 76 5b 31 5d 20 65 6c 65 The argv[1] ele
1f19c 6d 65 6e 74 20 69 73 20 74 68 65 20 72 6f 77 69 ment is the rowi
1f19d 64 20 6f 66 20 74 68 65 20 6e 65 77 20 0a 2a 2a d of the new .**
1f19e 20 72 6f 77 2e 20 20 54 68 69 73 20 63 61 6e 20 row. This can
1f19f 62 65 20 4e 55 4c 4c 20 74 6f 20 68 61 76 65 20 be NULL to have
1f1a0 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c the virtual tabl
1f1a1 65 20 73 65 6c 65 63 74 20 74 68 65 20 6e 65 77 e select the new
1f1a2 20 0a 2a 2a 20 72 6f 77 69 64 20 66 6f 72 20 69 .** rowid for i
1f1a3 74 73 65 6c 66 2e 20 20 54 68 65 20 73 75 62 73 tself. The subs
1f1a4 65 71 75 65 6e 74 20 65 6c 65 6d 65 6e 74 73 20 equent elements
1f1a5 69 6e 20 74 68 65 20 61 72 72 61 79 20 61 72 65 in the array are
1f1a6 20 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 73 20 .** the values
1f1a7 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 of columns in th
1f1a8 65 20 6e 65 77 20 72 6f 77 2e 0a 2a 2a 0a 2a 2a e new row..**.**
1f1a9 20 49 66 20 50 32 3d 3d 31 20 74 68 65 6e 20 6e If P2==1 then n
1f1aa 6f 20 69 6e 73 65 72 74 20 69 73 20 70 65 72 66 o insert is perf
1f1ab 6f 72 6d 65 64 2e 20 20 61 72 67 76 5b 30 5d 20 ormed. argv[0]
1f1ac 69 73 20 74 68 65 20 72 6f 77 69 64 20 6f 66 0a is the rowid of.
1f1ad 2a 2a 20 61 20 72 6f 77 20 74 6f 20 64 65 6c 65 ** a row to dele
1f1ae 74 65 2e 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 te..**.** P1 is
1f1af 61 20 62 6f 6f 6c 65 61 6e 20 66 6c 61 67 2e 20 a boolean flag.
1f1b0 49 66 20 69 74 20 69 73 20 73 65 74 20 74 6f 20 If it is set to
1f1b1 74 72 75 65 20 61 6e 64 20 74 68 65 20 78 55 70 true and the xUp
1f1b2 64 61 74 65 20 63 61 6c 6c 0a 2a 2a 20 69 73 20 date call.** is
1f1b3 73 75 63 63 65 73 73 66 75 6c 2c 20 74 68 65 6e successful, then
1f1b4 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 the value retur
1f1b5 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 6c ned by sqlite3_l
1f1b6 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 ast_insert_rowid
1f1b7 28 29 20 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f () .** is set to
1f1b8 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 the value of th
1f1b9 65 20 72 6f 77 69 64 20 66 6f 72 20 74 68 65 20 e rowid for the
1f1ba 72 6f 77 20 6a 75 73 74 20 69 6e 73 65 72 74 65 row just inserte
1f1bb 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 55 d..*/.case OP_VU
1f1bc 70 64 61 74 65 3a 20 7b 0a 23 69 66 20 30 20 20 pdate: {.#if 0
1f1bd 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c /* local variabl
1f1be 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e es moved into u.
1f1bf 63 6c 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f cl */. sqlite3_
1f1c0 76 74 61 62 20 2a 70 56 74 61 62 3b 0a 20 20 73 vtab *pVtab;. s
1f1c1 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 qlite3_module *p
1f1c2 4d 6f 64 75 6c 65 3b 0a 20 20 69 6e 74 20 6e 41 Module;. int nA
1f1c3 72 67 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 rg;. int i;. s
1f1c4 71 6c 69 74 65 5f 69 6e 74 36 34 20 72 6f 77 69 qlite_int64 rowi
1f1c5 64 3b 0a 20 20 4d 65 6d 20 2a 2a 61 70 41 72 67 d;. Mem **apArg
1f1c6 3b 0a 20 20 4d 65 6d 20 2a 70 58 3b 0a 23 65 6e ;. Mem *pX;.#en
1f1c7 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 dif /* local var
1f1c8 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 iables moved int
1f1c9 6f 20 75 2e 63 6c 20 2a 2f 0a 0a 20 20 75 2e 63 o u.cl */.. u.c
1f1ca 6c 2e 70 56 74 61 62 20 3d 20 70 4f 70 2d 3e 70 l.pVtab = pOp->p
1f1cb 34 2e 70 56 74 61 62 2d 3e 70 56 74 61 62 3b 0a 4.pVtab->pVtab;.
1f1cc 20 20 75 2e 63 6c 2e 70 4d 6f 64 75 6c 65 20 3d u.cl.pModule =
1f1cd 20 28 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 (sqlite3_module
1f1ce 20 2a 29 75 2e 63 6c 2e 70 56 74 61 62 2d 3e 70 *)u.cl.pVtab->p
1f1cf 4d 6f 64 75 6c 65 3b 0a 20 20 75 2e 63 6c 2e 6e Module;. u.cl.n
1f1d0 41 72 67 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 Arg = pOp->p2;.
1f1d1 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 assert( pOp->p4
1f1d2 74 79 70 65 3d 3d 50 34 5f 56 54 41 42 20 29 3b type==P4_VTAB );
1f1d3 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 75 2e . if( ALWAYS(u.
1f1d4 63 6c 2e 70 4d 6f 64 75 6c 65 2d 3e 78 55 70 64 cl.pModule->xUpd
1f1d5 61 74 65 29 20 29 7b 0a 20 20 20 20 75 2e 63 6c ate) ){. u.cl
1f1d6 2e 61 70 41 72 67 20 3d 20 70 2d 3e 61 70 41 72 .apArg = p->apAr
1f1d7 67 3b 0a 20 20 20 20 75 2e 63 6c 2e 70 58 20 3d g;. u.cl.pX =
1f1d8 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 &p->aMem[pOp->p
1f1d9 33 5d 3b 0a 20 20 20 20 66 6f 72 28 75 2e 63 6c 3];. for(u.cl
1f1da 2e 69 3d 30 3b 20 75 2e 63 6c 2e 69 3c 75 2e 63 .i=0; u.cl.i<u.c
1f1db 6c 2e 6e 41 72 67 3b 20 75 2e 63 6c 2e 69 2b 2b l.nArg; u.cl.i++
1f1dc 29 7b 0a 20 20 20 20 20 20 73 74 6f 72 65 54 79 ){. storeTy
1f1dd 70 65 49 6e 66 6f 28 75 2e 63 6c 2e 70 58 2c 20 peInfo(u.cl.pX,
1f1de 30 29 3b 0a 20 20 20 20 20 20 75 2e 63 6c 2e 61 0);. u.cl.a
1f1df 70 41 72 67 5b 75 2e 63 6c 2e 69 5d 20 3d 20 75 pArg[u.cl.i] = u
1f1e0 2e 63 6c 2e 70 58 3b 0a 20 20 20 20 20 20 75 2e .cl.pX;. u.
1f1e1 63 6c 2e 70 58 2b 2b 3b 0a 20 20 20 20 7d 0a 20 cl.pX++;. }.
1f1e2 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 if( sqlite3Sa
1f1e3 66 65 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f fetyOff(db) ) go
1f1e4 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f to abort_due_to_
1f1e5 6d 69 73 75 73 65 3b 0a 20 20 20 20 72 63 20 3d misuse;. rc =
1f1e6 20 75 2e 63 6c 2e 70 4d 6f 64 75 6c 65 2d 3e 78 u.cl.pModule->x
1f1e7 55 70 64 61 74 65 28 75 2e 63 6c 2e 70 56 74 61 Update(u.cl.pVta
1f1e8 62 2c 20 75 2e 63 6c 2e 6e 41 72 67 2c 20 75 2e b, u.cl.nArg, u.
1f1e9 63 6c 2e 61 70 41 72 67 2c 20 26 75 2e 63 6c 2e cl.apArg, &u.cl.
1f1ea 72 6f 77 69 64 29 3b 0a 20 20 20 20 73 71 6c 69 rowid);. sqli
1f1eb 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d te3DbFree(db, p-
1f1ec 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 70 >zErrMsg);. p
1f1ed 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 75 2e 63 6c ->zErrMsg = u.cl
1f1ee 2e 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 3b .pVtab->zErrMsg;
1f1ef 0a 20 20 20 20 75 2e 63 6c 2e 70 56 74 61 62 2d . u.cl.pVtab-
1f1f0 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 >zErrMsg = 0;.
1f1f1 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 if( sqlite3Saf
1f1f2 65 74 79 4f 6e 28 64 62 29 20 29 20 67 6f 74 6f etyOn(db) ) goto
1f1f3 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 abort_due_to_mi
1f1f4 73 75 73 65 3b 0a 20 20 20 20 69 66 28 20 72 63 suse;. if( rc
1f1f5 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 ==SQLITE_OK && p
1f1f6 4f 70 2d 3e 70 31 20 29 7b 0a 20 20 20 20 20 20 Op->p1 ){.
1f1f7 61 73 73 65 72 74 28 20 75 2e 63 6c 2e 6e 41 72 assert( u.cl.nAr
1f1f8 67 3e 31 20 26 26 20 75 2e 63 6c 2e 61 70 41 72 g>1 && u.cl.apAr
1f1f9 67 5b 30 5d 20 26 26 20 28 75 2e 63 6c 2e 61 70 g[0] && (u.cl.ap
1f1fa 41 72 67 5b 30 5d 2d 3e 66 6c 61 67 73 26 4d 45 Arg[0]->flags&ME
1f1fb 4d 5f 4e 75 6c 6c 29 20 29 3b 0a 20 20 20 20 20 M_Null) );.
1f1fc 20 64 62 2d 3e 6c 61 73 74 52 6f 77 69 64 20 3d db->lastRowid =
1f1fd 20 75 2e 63 6c 2e 72 6f 77 69 64 3b 0a 20 20 20 u.cl.rowid;.
1f1fe 20 7d 0a 20 20 20 20 70 2d 3e 6e 43 68 61 6e 67 }. p->nChang
1f1ff 65 2b 2b 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b e++;. }. break
1f200 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 ;.}.#endif /* SQ
1f201 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 LITE_OMIT_VIRTUA
1f202 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64 LTABLE */..#ifnd
1f203 65 66 20 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f ef SQLITE_OMIT_
1f204 50 41 47 45 52 5f 50 52 41 47 4d 41 53 0a 2f 2a PAGER_PRAGMAS./*
1f205 20 4f 70 63 6f 64 65 3a 20 50 61 67 65 63 6f 75 Opcode: Pagecou
1f206 6e 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a nt P1 P2 * * *.*
1f207 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20 63 *.** Write the c
1f208 75 72 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 urrent number of
1f209 20 70 61 67 65 73 20 69 6e 20 64 61 74 61 62 61 pages in databa
1f20a 73 65 20 50 31 20 74 6f 20 6d 65 6d 6f 72 79 20 se P1 to memory
1f20b 63 65 6c 6c 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 cell P2..*/.case
1f20c 20 4f 50 5f 50 61 67 65 63 6f 75 6e 74 3a 20 7b OP_Pagecount: {
1f20d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6f /* o
1f20e 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a ut2-prerelease *
1f20f 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 /.#if 0 /* loca
1f210 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 l variables move
1f211 64 20 69 6e 74 6f 20 75 2e 63 6d 20 2a 2f 0a 20 d into u.cm */.
1f212 20 69 6e 74 20 70 31 3b 0a 20 20 69 6e 74 20 6e int p1;. int n
1f213 50 61 67 65 3b 0a 20 20 50 61 67 65 72 20 2a 70 Page;. Pager *p
1f214 50 61 67 65 72 3b 0a 23 65 6e 64 69 66 20 2f 2a Pager;.#endif /*
1f215 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 local variables
1f216 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 6d moved into u.cm
1f217 20 2a 2f 0a 0a 20 20 75 2e 63 6d 2e 70 31 20 3d */.. u.cm.p1 =
1f218 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 75 2e 63 6d pOp->p1;. u.cm
1f219 2e 70 50 61 67 65 72 20 3d 20 73 71 6c 69 74 65 .pPager = sqlite
1f21a 33 42 74 72 65 65 50 61 67 65 72 28 64 62 2d 3e 3BtreePager(db->
1f21b 61 44 62 5b 75 2e 63 6d 2e 70 31 5d 2e 70 42 74 aDb[u.cm.p1].pBt
1f21c 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 );. rc = sqlite
1f21d 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 3PagerPagecount(
1f21e 75 2e 63 6d 2e 70 50 61 67 65 72 2c 20 26 75 2e u.cm.pPager, &u.
1f21f 63 6d 2e 6e 50 61 67 65 29 3b 0a 20 20 2f 2a 20 cm.nPage);. /*
1f220 4f 50 5f 50 61 67 65 63 6f 75 6e 74 20 69 73 20 OP_Pagecount is
1f221 61 6c 77 61 79 73 20 63 61 6c 6c 65 64 20 66 72 always called fr
1f222 6f 6d 20 77 69 74 68 69 6e 20 61 20 72 65 61 64 om within a read
1f223 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 54 transaction. T
1f224 68 65 0a 20 20 2a 2a 20 70 61 67 65 20 63 6f 75 he. ** page cou
1f225 6e 74 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 nt has already b
1f226 65 65 6e 20 73 75 63 63 65 73 73 66 75 6c 6c 79 een successfully
1f227 20 72 65 61 64 20 61 6e 64 20 63 61 63 68 65 64 read and cached
1f228 2e 20 20 53 6f 20 74 68 65 0a 20 20 2a 2a 20 73 . So the. ** s
1f229 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 63 qlite3PagerPagec
1f22a 6f 75 6e 74 28 29 20 63 61 6c 6c 20 61 62 6f 76 ount() call abov
1f22b 65 20 63 61 6e 6e 6f 74 20 66 61 69 6c 2e 20 2a e cannot fail. *
1f22c 2f 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 72 /. if( ALWAYS(r
1f22d 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 29 7b c==SQLITE_OK) ){
1f22e 0a 20 20 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 . pOut->flags
1f22f 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 = MEM_Int;.
1f230 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 2e 63 6d pOut->u.i = u.cm
1f231 2e 6e 50 61 67 65 3b 0a 20 20 7d 0a 20 20 62 72 .nPage;. }. br
1f232 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 eak;.}.#endif..#
1f233 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1f234 49 54 5f 54 52 41 43 45 0a 2f 2a 20 4f 70 63 6f IT_TRACE./* Opco
1f235 64 65 3a 20 54 72 61 63 65 20 2a 20 2a 20 2a 20 de: Trace * * *
1f236 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 74 72 P4 *.**.** If tr
1f237 61 63 69 6e 67 20 69 73 20 65 6e 61 62 6c 65 64 acing is enabled
1f238 20 28 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 (by the sqlite3
1f239 5f 74 72 61 63 65 28 29 29 20 69 6e 74 65 72 66 _trace()) interf
1f23a 61 63 65 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65 ace, then.** the
1f23b 20 55 54 46 2d 38 20 73 74 72 69 6e 67 20 63 6f UTF-8 string co
1f23c 6e 74 61 69 6e 65 64 20 69 6e 20 50 34 20 69 73 ntained in P4 is
1f23d 20 65 6d 69 74 74 65 64 20 6f 6e 20 74 68 65 20 emitted on the
1f23e 74 72 61 63 65 20 63 61 6c 6c 62 61 63 6b 2e 0a trace callback..
1f23f 2a 2f 0a 63 61 73 65 20 4f 50 5f 54 72 61 63 65 */.case OP_Trace
1f240 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f : {.#if 0 /* lo
1f241 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f cal variables mo
1f242 76 65 64 20 69 6e 74 6f 20 75 2e 63 6e 20 2a 2f ved into u.cn */
1f243 0a 20 20 63 68 61 72 20 2a 7a 54 72 61 63 65 3b . char *zTrace;
1f244 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c .#endif /* local
1f245 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1f246 20 69 6e 74 6f 20 75 2e 63 6e 20 2a 2f 0a 0a 20 into u.cn */..
1f247 20 75 2e 63 6e 2e 7a 54 72 61 63 65 20 3d 20 28 u.cn.zTrace = (
1f248 70 4f 70 2d 3e 70 34 2e 7a 20 3f 20 70 4f 70 2d pOp->p4.z ? pOp-
1f249 3e 70 34 2e 7a 20 3a 20 70 2d 3e 7a 53 71 6c 29 >p4.z : p->zSql)
1f24a 3b 0a 20 20 69 66 28 20 75 2e 63 6e 2e 7a 54 72 ;. if( u.cn.zTr
1f24b 61 63 65 20 29 7b 0a 20 20 20 20 69 66 28 20 64 ace ){. if( d
1f24c 62 2d 3e 78 54 72 61 63 65 20 29 7b 0a 20 20 20 b->xTrace ){.
1f24d 20 20 20 64 62 2d 3e 78 54 72 61 63 65 28 64 62 db->xTrace(db
1f24e 2d 3e 70 54 72 61 63 65 41 72 67 2c 20 75 2e 63 ->pTraceArg, u.c
1f24f 6e 2e 7a 54 72 61 63 65 29 3b 0a 20 20 20 20 7d n.zTrace);. }
1f250 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 .#ifdef SQLITE_D
1f251 45 42 55 47 0a 20 20 20 20 69 66 28 20 28 64 62 EBUG. if( (db
1f252 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 ->flags & SQLITE
1f253 5f 53 71 6c 54 72 61 63 65 29 21 3d 30 20 29 7b _SqlTrace)!=0 ){
1f254 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 65 . sqlite3De
1f255 62 75 67 50 72 69 6e 74 66 28 22 53 51 4c 2d 74 bugPrintf("SQL-t
1f256 72 61 63 65 3a 20 25 73 5c 6e 22 2c 20 75 2e 63 race: %s\n", u.c
1f257 6e 2e 7a 54 72 61 63 65 29 3b 0a 20 20 20 20 7d n.zTrace);. }
1f258 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
1f259 45 5f 44 45 42 55 47 20 2a 2f 0a 20 20 7d 0a 20 E_DEBUG */. }.
1f25a 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 break;.}.#endif
1f25b 0a 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 6f .../* Opcode: No
1f25c 6f 70 20 2a 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a op * * * * *.**.
1f25d 2a 2a 20 44 6f 20 6e 6f 74 68 69 6e 67 2e 20 20 ** Do nothing.
1f25e 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e This instruction
1f25f 20 69 73 20 6f 66 74 65 6e 20 75 73 65 66 75 6c is often useful
1f260 20 61 73 20 61 20 6a 75 6d 70 0a 2a 2a 20 64 65 as a jump.** de
1f261 73 74 69 6e 61 74 69 6f 6e 2e 0a 2a 2f 0a 2f 2a stination..*/./*
1f262 0a 2a 2a 20 54 68 65 20 6d 61 67 69 63 20 45 78 .** The magic Ex
1f263 70 6c 61 69 6e 20 6f 70 63 6f 64 65 20 61 72 65 plain opcode are
1f264 20 6f 6e 6c 79 20 69 6e 73 65 72 74 65 64 20 77 only inserted w
1f265 68 65 6e 20 65 78 70 6c 61 69 6e 3d 3d 32 20 28 hen explain==2 (
1f266 77 68 69 63 68 0a 2a 2a 20 69 73 20 74 6f 20 73 which.** is to s
1f267 61 79 20 77 68 65 6e 20 74 68 65 20 45 58 50 4c ay when the EXPL
1f268 41 49 4e 20 51 55 45 52 59 20 50 4c 41 4e 20 73 AIN QUERY PLAN s
1f269 79 6e 74 61 78 20 69 73 20 75 73 65 64 2e 29 0a yntax is used.).
1f26a 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20 72 ** This opcode r
1f26b 65 63 6f 72 64 73 20 69 6e 66 6f 72 6d 61 74 69 ecords informati
1f26c 6f 6e 20 66 72 6f 6d 20 74 68 65 20 6f 70 74 69 on from the opti
1f26d 6d 69 7a 65 72 2e 20 20 49 74 20 69 73 20 74 68 mizer. It is th
1f26e 65 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20 61 73 e.** the same as
1f26f 20 61 20 6e 6f 2d 6f 70 2e 20 20 54 68 69 73 20 a no-op. This
1f270 6f 70 63 6f 64 65 73 6e 65 76 65 72 20 61 70 70 opcodesnever app
1f271 65 61 72 73 20 69 6e 20 61 20 72 65 61 6c 20 56 ears in a real V
1f272 4d 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f 0a 64 65 M program..*/.de
1f273 66 61 75 6c 74 3a 20 7b 20 20 20 20 20 20 20 20 fault: {
1f274 20 20 2f 2a 20 54 68 69 73 20 69 73 20 72 65 61 /* This is rea
1f275 6c 6c 79 20 4f 50 5f 4e 6f 6f 70 20 61 6e 64 20 lly OP_Noop and
1f276 4f 50 5f 45 78 70 6c 61 69 6e 20 2a 2f 0a 20 20 OP_Explain */.
1f277 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a break;.}../*****
1f278 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f279 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f27a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f27b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f27c 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 ********.** The
1f27d 63 61 73 65 73 20 6f 66 20 74 68 65 20 73 77 69 cases of the swi
1f27e 74 63 68 20 73 74 61 74 65 6d 65 6e 74 20 61 62 tch statement ab
1f27f 6f 76 65 20 74 68 69 73 20 6c 69 6e 65 20 73 68 ove this line sh
1f280 6f 75 6c 64 20 61 6c 6c 20 62 65 20 69 6e 64 65 ould all be inde
1f281 6e 74 65 64 0a 2a 2a 20 62 79 20 36 20 73 70 61 nted.** by 6 spa
1f282 63 65 73 2e 20 20 42 75 74 20 74 68 65 20 6c 65 ces. But the le
1f283 66 74 2d 6d 6f 73 74 20 36 20 73 70 61 63 65 73 ft-most 6 spaces
1f284 20 68 61 76 65 20 62 65 65 6e 20 72 65 6d 6f 76 have been remov
1f285 65 64 20 74 6f 20 69 6d 70 72 6f 76 65 20 74 68 ed to improve th
1f286 65 0a 2a 2a 20 72 65 61 64 61 62 69 6c 69 74 79 e.** readability
1f287 2e 20 20 46 72 6f 6d 20 74 68 69 73 20 70 6f 69 . From this poi
1f288 6e 74 20 6f 6e 20 64 6f 77 6e 2c 20 74 68 65 20 nt on down, the
1f289 6e 6f 72 6d 61 6c 20 69 6e 64 65 6e 74 61 74 69 normal indentati
1f28a 6f 6e 20 72 75 6c 65 73 20 61 72 65 0a 2a 2a 20 on rules are.**
1f28b 72 65 73 74 6f 72 65 64 2e 0a 2a 2a 2a 2a 2a 2a restored..******
1f28c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f28d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f28e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f28f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f290 2a 2a 2a 2a 2a 2a 2a 2f 0a 20 20 20 20 7d 0a 0a *******/. }..
1f291 23 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f 46 #ifdef VDBE_PROF
1f292 49 4c 45 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 ILE. {.
1f293 75 36 34 20 65 6c 61 70 73 65 64 20 3d 20 73 71 u64 elapsed = sq
1f294 6c 69 74 65 33 48 77 74 69 6d 65 28 29 20 2d 20 lite3Hwtime() -
1f295 73 74 61 72 74 3b 0a 20 20 20 20 20 20 70 4f 70 start;. pOp
1f296 2d 3e 63 79 63 6c 65 73 20 2b 3d 20 65 6c 61 70 ->cycles += elap
1f297 73 65 64 3b 0a 20 20 20 20 20 20 70 4f 70 2d 3e sed;. pOp->
1f298 63 6e 74 2b 2b 3b 0a 23 69 66 20 30 0a 20 20 20 cnt++;.#if 0.
1f299 20 20 20 20 20 66 70 72 69 6e 74 66 28 73 74 64 fprintf(std
1f29a 6f 75 74 2c 20 22 25 31 30 6c 6c 75 20 22 2c 20 out, "%10llu ",
1f29b 65 6c 61 70 73 65 64 29 3b 0a 20 20 20 20 20 20 elapsed);.
1f29c 20 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69 sqlite3VdbePri
1f29d 6e 74 4f 70 28 73 74 64 6f 75 74 2c 20 6f 72 69 ntOp(stdout, ori
1f29e 67 50 63 2c 20 26 70 2d 3e 61 4f 70 5b 6f 72 69 gPc, &p->aOp[ori
1f29f 67 50 63 5d 29 3b 0a 23 65 6e 64 69 66 0a 20 20 gPc]);.#endif.
1f2a0 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 }.#endif..
1f2a1 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 /* The following
1f2a2 20 63 6f 64 65 20 61 64 64 73 20 6e 6f 74 68 69 code adds nothi
1f2a3 6e 67 20 74 6f 20 74 68 65 20 61 63 74 75 61 6c ng to the actual
1f2a4 20 66 75 6e 63 74 69 6f 6e 61 6c 69 74 79 0a 20 functionality.
1f2a5 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 70 72 6f ** of the pro
1f2a6 67 72 61 6d 2e 20 20 49 74 20 69 73 20 6f 6e 6c gram. It is onl
1f2a7 79 20 68 65 72 65 20 66 6f 72 20 74 65 73 74 69 y here for testi
1f2a8 6e 67 20 61 6e 64 20 64 65 62 75 67 67 69 6e 67 ng and debugging
1f2a9 2e 0a 20 20 20 20 2a 2a 20 4f 6e 20 74 68 65 20 .. ** On the
1f2aa 6f 74 68 65 72 20 68 61 6e 64 2c 20 69 74 20 64 other hand, it d
1f2ab 6f 65 73 20 62 75 72 6e 20 43 50 55 20 63 79 63 oes burn CPU cyc
1f2ac 6c 65 73 20 65 76 65 72 79 20 74 69 6d 65 20 74 les every time t
1f2ad 68 72 6f 75 67 68 0a 20 20 20 20 2a 2a 20 74 68 hrough. ** th
1f2ae 65 20 65 76 61 6c 75 61 74 6f 72 20 6c 6f 6f 70 e evaluator loop
1f2af 2e 20 20 53 6f 20 77 65 20 63 61 6e 20 6c 65 61 . So we can lea
1f2b0 76 65 20 69 74 20 6f 75 74 20 77 68 65 6e 20 4e ve it out when N
1f2b1 44 45 42 55 47 20 69 73 20 64 65 66 69 6e 65 64 DEBUG is defined
1f2b2 2e 0a 20 20 20 20 2a 2f 0a 23 69 66 6e 64 65 66 .. */.#ifndef
1f2b3 20 4e 44 45 42 55 47 0a 20 20 20 20 61 73 73 65 NDEBUG. asse
1f2b4 72 74 28 20 70 63 3e 3d 2d 31 20 26 26 20 70 63 rt( pc>=-1 && pc
1f2b5 3c 70 2d 3e 6e 4f 70 20 29 3b 0a 0a 23 69 66 64 <p->nOp );..#ifd
1f2b6 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a ef SQLITE_DEBUG.
1f2b7 20 20 20 20 69 66 28 20 70 2d 3e 74 72 61 63 65 if( p->trace
1f2b8 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 72 63 ){. if( rc
1f2b9 21 3d 30 20 29 20 66 70 72 69 6e 74 66 28 70 2d !=0 ) fprintf(p-
1f2ba 3e 74 72 61 63 65 2c 22 72 63 3d 25 64 5c 6e 22 >trace,"rc=%d\n"
1f2bb 2c 72 63 29 3b 0a 20 20 20 20 20 20 69 66 28 20 ,rc);. if(
1f2bc 6f 70 50 72 6f 70 65 72 74 79 20 26 20 4f 50 46 opProperty & OPF
1f2bd 4c 47 5f 4f 55 54 32 5f 50 52 45 52 45 4c 45 41 LG_OUT2_PRERELEA
1f2be 53 45 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 SE ){. re
1f2bf 67 69 73 74 65 72 54 72 61 63 65 28 70 2d 3e 74 gisterTrace(p->t
1f2c0 72 61 63 65 2c 20 70 4f 70 2d 3e 70 32 2c 20 70 race, pOp->p2, p
1f2c1 4f 75 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 Out);. }.
1f2c2 20 20 20 20 69 66 28 20 6f 70 50 72 6f 70 65 72 if( opProper
1f2c3 74 79 20 26 20 4f 50 46 4c 47 5f 4f 55 54 33 20 ty & OPFLG_OUT3
1f2c4 29 7b 0a 20 20 20 20 20 20 20 20 72 65 67 69 73 ){. regis
1f2c5 74 65 72 54 72 61 63 65 28 70 2d 3e 74 72 61 63 terTrace(p->trac
1f2c6 65 2c 20 70 4f 70 2d 3e 70 33 2c 20 70 4f 75 74 e, pOp->p3, pOut
1f2c7 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d );. }. }
1f2c8 0a 23 65 6e 64 69 66 20 20 2f 2a 20 53 51 4c 49 .#endif /* SQLI
1f2c9 54 45 5f 44 45 42 55 47 20 2a 2f 0a 23 65 6e 64 TE_DEBUG */.#end
1f2ca 69 66 20 20 2f 2a 20 4e 44 45 42 55 47 20 2a 2f if /* NDEBUG */
1f2cb 0a 20 20 7d 20 20 2f 2a 20 54 68 65 20 65 6e 64 . } /* The end
1f2cc 20 6f 66 20 74 68 65 20 66 6f 72 28 3b 3b 29 20 of the for(;;)
1f2cd 6c 6f 6f 70 20 74 68 65 20 6c 6f 6f 70 73 20 74 loop the loops t
1f2ce 68 72 6f 75 67 68 20 6f 70 63 6f 64 65 73 20 2a hrough opcodes *
1f2cf 2f 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 72 65 /.. /* If we re
1f2d0 61 63 68 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 ach this point,
1f2d1 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 65 78 it means that ex
1f2d2 65 63 75 74 69 6f 6e 20 69 73 20 66 69 6e 69 73 ecution is finis
1f2d3 68 65 64 20 77 69 74 68 0a 20 20 2a 2a 20 61 6e hed with. ** an
1f2d4 20 65 72 72 6f 72 20 6f 66 20 73 6f 6d 65 20 6b error of some k
1f2d5 69 6e 64 2e 0a 20 20 2a 2f 0a 76 64 62 65 5f 65 ind.. */.vdbe_e
1f2d6 72 72 6f 72 5f 68 61 6c 74 3a 0a 20 20 61 73 73 rror_halt:. ass
1f2d7 65 72 74 28 20 72 63 20 29 3b 0a 20 20 70 2d 3e ert( rc );. p->
1f2d8 72 63 20 3d 20 72 63 3b 0a 20 20 73 71 6c 69 74 rc = rc;. sqlit
1f2d9 65 33 56 64 62 65 48 61 6c 74 28 70 29 3b 0a 20 e3VdbeHalt(p);.
1f2da 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
1f2db 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 20 64 62 IOERR_NOMEM ) db
1f2dc 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d ->mallocFailed =
1f2dd 20 31 3b 0a 20 20 72 63 20 3d 20 53 51 4c 49 54 1;. rc = SQLIT
1f2de 45 5f 45 52 52 4f 52 3b 0a 0a 20 20 2f 2a 20 54 E_ERROR;.. /* T
1f2df 68 69 73 20 69 73 20 74 68 65 20 6f 6e 6c 79 20 his is the only
1f2e0 77 61 79 20 6f 75 74 20 6f 66 20 74 68 69 73 20 way out of this
1f2e1 70 72 6f 63 65 64 75 72 65 2e 20 20 57 65 20 68 procedure. We h
1f2e2 61 76 65 20 74 6f 0a 20 20 2a 2a 20 72 65 6c 65 ave to. ** rele
1f2e3 61 73 65 20 74 68 65 20 6d 75 74 65 78 65 73 20 ase the mutexes
1f2e4 6f 6e 20 62 74 72 65 65 73 20 74 68 61 74 20 77 on btrees that w
1f2e5 65 72 65 20 61 63 71 75 69 72 65 64 20 61 74 20 ere acquired at
1f2e6 74 68 65 0a 20 20 2a 2a 20 74 6f 70 2e 20 2a 2f the. ** top. */
1f2e7 0a 76 64 62 65 5f 72 65 74 75 72 6e 3a 0a 20 20 .vdbe_return:.
1f2e8 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 sqlite3BtreeMute
1f2e9 78 41 72 72 61 79 4c 65 61 76 65 28 26 70 2d 3e xArrayLeave(&p->
1f2ea 61 4d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 aMutex);. retur
1f2eb 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 4a 75 6d 70 n rc;.. /* Jump
1f2ec 20 74 6f 20 68 65 72 65 20 69 66 20 61 20 73 74 to here if a st
1f2ed 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 6c 61 72 ring or blob lar
1f2ee 67 65 72 20 74 68 61 6e 20 53 51 4c 49 54 45 5f ger than SQLITE_
1f2ef 4d 41 58 5f 4c 45 4e 47 54 48 0a 20 20 2a 2a 20 MAX_LENGTH. **
1f2f0 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2e 0a is encountered..
1f2f1 20 20 2a 2f 0a 74 6f 6f 5f 62 69 67 3a 0a 20 20 */.too_big:.
1f2f2 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 sqlite3SetString
1f2f3 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 (&p->zErrMsg, db
1f2f4 2c 20 22 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f , "string or blo
1f2f5 62 20 74 6f 6f 20 62 69 67 22 29 3b 0a 20 20 72 b too big");. r
1f2f6 63 20 3d 20 53 51 4c 49 54 45 5f 54 4f 4f 42 49 c = SQLITE_TOOBI
1f2f7 47 3b 0a 20 20 67 6f 74 6f 20 76 64 62 65 5f 65 G;. goto vdbe_e
1f2f8 72 72 6f 72 5f 68 61 6c 74 3b 0a 0a 20 20 2f 2a rror_halt;.. /*
1f2f9 20 4a 75 6d 70 20 74 6f 20 68 65 72 65 20 69 66 Jump to here if
1f2fa 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c a malloc() fail
1f2fb 73 2e 0a 20 20 2a 2f 0a 6e 6f 5f 6d 65 6d 3a 0a s.. */.no_mem:.
1f2fc 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c db->mallocFail
1f2fd 65 64 20 3d 20 31 3b 0a 20 20 73 71 6c 69 74 65 ed = 1;. sqlite
1f2fe 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 3SetString(&p->z
1f2ff 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 6f 75 74 ErrMsg, db, "out
1f300 20 6f 66 20 6d 65 6d 6f 72 79 22 29 3b 0a 20 20 of memory");.
1f301 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 rc = SQLITE_NOME
1f302 4d 3b 0a 20 20 67 6f 74 6f 20 76 64 62 65 5f 65 M;. goto vdbe_e
1f303 72 72 6f 72 5f 68 61 6c 74 3b 0a 0a 20 20 2f 2a rror_halt;.. /*
1f304 20 4a 75 6d 70 20 74 6f 20 68 65 72 65 20 66 6f Jump to here fo
1f305 72 20 61 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 r an SQLITE_MISU
1f306 53 45 20 65 72 72 6f 72 2e 0a 20 20 2a 2f 0a 61 SE error.. */.a
1f307 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 bort_due_to_misu
1f308 73 65 3a 0a 20 20 72 63 20 3d 20 53 51 4c 49 54 se:. rc = SQLIT
1f309 45 5f 4d 49 53 55 53 45 3b 0a 20 20 2f 2a 20 46 E_MISUSE;. /* F
1f30a 61 6c 6c 20 74 68 72 75 20 69 6e 74 6f 20 61 62 all thru into ab
1f30b 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 ort_due_to_error
1f30c 20 2a 2f 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 74 */.. /* Jump t
1f30d 6f 20 68 65 72 65 20 66 6f 72 20 61 6e 79 20 6f o here for any o
1f30e 74 68 65 72 20 6b 69 6e 64 20 6f 66 20 66 61 74 ther kind of fat
1f30f 61 6c 20 65 72 72 6f 72 2e 20 20 54 68 65 20 22 al error. The "
1f310 72 63 22 20 76 61 72 69 61 62 6c 65 0a 20 20 2a rc" variable. *
1f311 2a 20 73 68 6f 75 6c 64 20 68 6f 6c 64 20 74 68 * should hold th
1f312 65 20 65 72 72 6f 72 20 6e 75 6d 62 65 72 2e 0a e error number..
1f313 20 20 2a 2f 0a 61 62 6f 72 74 5f 64 75 65 5f 74 */.abort_due_t
1f314 6f 5f 65 72 72 6f 72 3a 0a 20 20 61 73 73 65 72 o_error:. asser
1f315 74 28 20 70 2d 3e 7a 45 72 72 4d 73 67 3d 3d 30 t( p->zErrMsg==0
1f316 20 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 );. if( db->ma
1f317 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 63 20 llocFailed ) rc
1f318 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a = SQLITE_NOMEM;.
1f319 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
1f31a 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 7b 0a _IOERR_NOMEM ){.
1f31b 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 sqlite3SetSt
1f31c 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 ring(&p->zErrMsg
1f31d 2c 20 64 62 2c 20 22 25 73 22 2c 20 73 71 6c 69 , db, "%s", sqli
1f31e 74 65 33 45 72 72 53 74 72 28 72 63 29 29 3b 0a te3ErrStr(rc));.
1f31f 20 20 7d 0a 20 20 67 6f 74 6f 20 76 64 62 65 5f }. goto vdbe_
1f320 65 72 72 6f 72 5f 68 61 6c 74 3b 0a 0a 20 20 2f error_halt;.. /
1f321 2a 20 4a 75 6d 70 20 74 6f 20 68 65 72 65 20 69 * Jump to here i
1f322 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e f the sqlite3_in
1f323 74 65 72 72 75 70 74 28 29 20 41 50 49 20 73 65 terrupt() API se
1f324 74 73 20 74 68 65 20 69 6e 74 65 72 72 75 70 74 ts the interrupt
1f325 0a 20 20 2a 2a 20 66 6c 61 67 2e 0a 20 20 2a 2f . ** flag.. */
1f326 0a 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 69 6e .abort_due_to_in
1f327 74 65 72 72 75 70 74 3a 0a 20 20 61 73 73 65 72 terrupt:. asser
1f328 74 28 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65 t( db->u1.isInte
1f329 72 72 75 70 74 65 64 20 29 3b 0a 20 20 72 63 20 rrupted );. rc
1f32a 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 = SQLITE_INTERRU
1f32b 50 54 3b 0a 20 20 70 2d 3e 72 63 20 3d 20 72 63 PT;. p->rc = rc
1f32c 3b 0a 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 ;. sqlite3SetSt
1f32d 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 ring(&p->zErrMsg
1f32e 2c 20 64 62 2c 20 22 25 73 22 2c 20 73 71 6c 69 , db, "%s", sqli
1f32f 74 65 33 45 72 72 53 74 72 28 72 63 29 29 3b 0a te3ErrStr(rc));.
1f330 20 20 67 6f 74 6f 20 76 64 62 65 5f 65 72 72 6f goto vdbe_erro
1f331 72 5f 68 61 6c 74 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a r_halt;.}../****
1f332 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
1f333 66 20 76 64 62 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a f vdbe.c *******
1f334 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f335 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f336 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
1f337 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
1f338 20 66 69 6c 65 20 76 64 62 65 62 6c 6f 62 2e 63 file vdbeblob.c
1f339 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
1f33a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f33b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a *********/./*.**
1f33c 20 32 30 30 37 20 4d 61 79 20 31 0a 2a 2a 0a 2a 2007 May 1.**.*
1f33d 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 * The author dis
1f33e 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 claims copyright
1f33f 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 to this source
1f340 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 code. In place
1f341 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f of.** a legal no
1f342 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 tice, here is a
1f343 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 blessing:.**.**
1f344 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f May you do go
1f345 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e od and not evil.
1f346 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 .** May you f
1f347 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 ind forgiveness
1f348 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 for yourself and
1f349 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e forgive others.
1f34a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 .** May you s
1f34b 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 hare freely, nev
1f34c 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 er taking more t
1f34d 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a han you give..**
1f34e 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
1f34f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f350 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f351 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f352 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a **********.**.**
1f353 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 This file conta
1f354 69 6e 73 20 63 6f 64 65 20 75 73 65 64 20 74 6f ins code used to
1f355 20 69 6d 70 6c 65 6d 65 6e 74 20 69 6e 63 72 65 implement incre
1f356 6d 65 6e 74 61 6c 20 42 4c 4f 42 20 49 2f 4f 2e mental BLOB I/O.
1f357 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76 64 62 65 .**.** $Id: vdbe
1f358 62 6c 6f 62 2e 63 2c 76 20 31 2e 33 35 20 32 30 blob.c,v 1.35 20
1f359 30 39 2f 30 37 2f 30 32 20 30 37 3a 34 37 3a 33 09/07/02 07:47:3
1f35a 33 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 3 danielk1977 Ex
1f35b 70 20 24 0a 2a 2f 0a 0a 0a 23 69 66 6e 64 65 66 p $.*/...#ifndef
1f35c 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 SQLITE_OMIT_INC
1f35d 52 42 4c 4f 42 0a 0a 2f 2a 0a 2a 2a 20 56 61 6c RBLOB../*.** Val
1f35e 69 64 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 2a id sqlite3_blob*
1f35f 20 68 61 6e 64 6c 65 73 20 70 6f 69 6e 74 20 74 handles point t
1f360 6f 20 49 6e 63 72 62 6c 6f 62 20 73 74 72 75 63 o Incrblob struc
1f361 74 75 72 65 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 tures..*/.typede
1f362 66 20 73 74 72 75 63 74 20 49 6e 63 72 62 6c 6f f struct Incrblo
1f363 62 20 49 6e 63 72 62 6c 6f 62 3b 0a 73 74 72 75 b Incrblob;.stru
1f364 63 74 20 49 6e 63 72 62 6c 6f 62 20 7b 0a 20 20 ct Incrblob {.
1f365 69 6e 74 20 66 6c 61 67 73 3b 20 20 20 20 20 20 int flags;
1f366 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 /* Copy
1f367 6f 66 20 22 66 6c 61 67 73 22 20 70 61 73 73 65 of "flags" passe
1f368 64 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 6c 6f d to sqlite3_blo
1f369 62 5f 6f 70 65 6e 28 29 20 2a 2f 0a 20 20 69 6e b_open() */. in
1f36a 74 20 6e 42 79 74 65 3b 20 20 20 20 20 20 20 20 t nByte;
1f36b 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 /* Size of
1f36c 20 6f 70 65 6e 20 62 6c 6f 62 2c 20 69 6e 20 62 open blob, in b
1f36d 79 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20 69 4f ytes */. int iO
1f36e 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 20 20 ffset;
1f36f 20 20 2f 2a 20 42 79 74 65 20 6f 66 66 73 65 74 /* Byte offset
1f370 20 6f 66 20 62 6c 6f 62 20 69 6e 20 63 75 72 73 of blob in curs
1f371 6f 72 20 64 61 74 61 20 2a 2f 0a 20 20 42 74 43 or data */. BtC
1f372 75 72 73 6f 72 20 2a 70 43 73 72 3b 20 20 20 20 ursor *pCsr;
1f373 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 70 /* Cursor p
1f374 6f 69 6e 74 69 6e 67 20 61 74 20 62 6c 6f 62 20 ointing at blob
1f375 72 6f 77 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 row */. sqlite3
1f376 5f 73 74 6d 74 20 2a 70 53 74 6d 74 3b 20 20 20 _stmt *pStmt;
1f377 20 2f 2a 20 53 74 61 74 65 6d 65 6e 74 20 68 6f /* Statement ho
1f378 6c 64 69 6e 67 20 63 75 72 73 6f 72 20 6f 70 65 lding cursor ope
1f379 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a n */. sqlite3 *
1f37a 64 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f db; /
1f37b 2a 20 54 68 65 20 61 73 73 6f 63 69 61 74 65 64 * The associated
1f37c 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 7d 3b 0a database */.};.
1f37d 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 62 6c ./*.** Open a bl
1f37e 6f 62 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 53 51 ob handle..*/.SQ
1f37f 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
1f380 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 0a ite3_blob_open(.
1f381 20 20 73 71 6c 69 74 65 33 2a 20 64 62 2c 20 20 sqlite3* db,
1f382 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
1f383 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
1f384 74 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 tion */. const
1f385 63 68 61 72 20 2a 7a 44 62 2c 20 20 20 20 20 20 char *zDb,
1f386 20 20 2f 2a 20 54 68 65 20 61 74 74 61 63 68 65 /* The attache
1f387 64 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 d database conta
1f388 69 6e 69 6e 67 20 74 68 65 20 62 6c 6f 62 20 2a ining the blob *
1f389 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
1f38a 7a 54 61 62 6c 65 2c 20 20 20 20 20 2f 2a 20 54 zTable, /* T
1f38b 68 65 20 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e he table contain
1f38c 69 6e 67 20 74 68 65 20 62 6c 6f 62 20 2a 2f 0a ing the blob */.
1f38d 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 const char *zC
1f38e 6f 6c 75 6d 6e 2c 20 20 20 20 2f 2a 20 54 68 65 olumn, /* The
1f38f 20 63 6f 6c 75 6d 6e 20 63 6f 6e 74 61 69 6e 69 column containi
1f390 6e 67 20 74 68 65 20 62 6c 6f 62 20 2a 2f 0a 20 ng the blob */.
1f391 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 52 sqlite_int64 iR
1f392 6f 77 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 ow, /* The
1f393 72 6f 77 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 row containing t
1f394 68 65 20 67 6c 6f 62 20 2a 2f 0a 20 20 69 6e 74 he glob */. int
1f395 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 flags,
1f396 20 20 20 20 20 2f 2a 20 54 72 75 65 20 2d 3e 20 /* True ->
1f397 72 65 61 64 2f 77 72 69 74 65 20 61 63 63 65 73 read/write acces
1f398 73 2c 20 66 61 6c 73 65 20 2d 3e 20 72 65 61 64 s, false -> read
1f399 2d 6f 6e 6c 79 20 2a 2f 0a 20 20 73 71 6c 69 74 -only */. sqlit
1f39a 65 33 5f 62 6c 6f 62 20 2a 2a 70 70 42 6c 6f 62 e3_blob **ppBlob
1f39b 20 20 20 2f 2a 20 48 61 6e 64 6c 65 20 66 6f 72 /* Handle for
1f39c 20 61 63 63 65 73 73 69 6e 67 20 74 68 65 20 62 accessing the b
1f39d 6c 6f 62 20 72 65 74 75 72 6e 65 64 20 68 65 72 lob returned her
1f39e 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 6e 41 e */.){. int nA
1f39f 74 74 65 6d 70 74 20 3d 20 30 3b 0a 20 20 69 6e ttempt = 0;. in
1f3a0 74 20 69 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 t iCol;
1f3a1 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f /* Index o
1f3a2 66 20 7a 43 6f 6c 75 6d 6e 20 69 6e 20 72 6f 77 f zColumn in row
1f3a3 2d 72 65 63 6f 72 64 20 2a 2f 0a 0a 20 20 2f 2a -record */.. /*
1f3a4 20 54 68 69 73 20 56 44 42 45 20 70 72 6f 67 72 This VDBE progr
1f3a5 61 6d 20 73 65 65 6b 73 20 61 20 62 74 72 65 65 am seeks a btree
1f3a6 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 69 cursor to the i
1f3a7 64 65 6e 74 69 66 69 65 64 20 0a 20 20 2a 2a 20 dentified . **
1f3a8 64 62 2f 74 61 62 6c 65 2f 72 6f 77 20 65 6e 74 db/table/row ent
1f3a9 72 79 2e 20 54 68 65 20 72 65 61 73 6f 6e 20 66 ry. The reason f
1f3aa 6f 72 20 75 73 69 6e 67 20 61 20 76 64 62 65 20 or using a vdbe
1f3ab 70 72 6f 67 72 61 6d 20 69 6e 73 74 65 61 64 0a program instead.
1f3ac 20 20 2a 2a 20 6f 66 20 77 72 69 74 69 6e 67 20 ** of writing
1f3ad 63 6f 64 65 20 74 6f 20 75 73 65 20 74 68 65 20 code to use the
1f3ae 62 2d 74 72 65 65 20 6c 61 79 65 72 20 64 69 72 b-tree layer dir
1f3af 65 63 74 6c 79 20 69 73 20 74 68 61 74 20 74 68 ectly is that th
1f3b0 65 0a 20 20 2a 2a 20 76 64 62 65 20 70 72 6f 67 e. ** vdbe prog
1f3b1 72 61 6d 20 77 69 6c 6c 20 74 61 6b 65 20 61 64 ram will take ad
1f3b2 76 61 6e 74 61 67 65 20 6f 66 20 74 68 65 20 76 vantage of the v
1f3b3 61 72 69 6f 75 73 20 74 72 61 6e 73 61 63 74 69 arious transacti
1f3b4 6f 6e 2c 0a 20 20 2a 2a 20 6c 6f 63 6b 69 6e 67 on,. ** locking
1f3b5 20 61 6e 64 20 65 72 72 6f 72 20 68 61 6e 64 6c and error handl
1f3b6 69 6e 67 20 69 6e 66 72 61 73 74 72 75 63 74 75 ing infrastructu
1f3b7 72 65 20 62 75 69 6c 74 20 69 6e 74 6f 20 74 68 re built into th
1f3b8 65 20 76 64 62 65 2e 0a 20 20 2a 2a 0a 20 20 2a e vdbe.. **. *
1f3b9 2a 20 41 66 74 65 72 20 73 65 65 6b 69 6e 67 20 * After seeking
1f3ba 74 68 65 20 63 75 72 73 6f 72 2c 20 74 68 65 20 the cursor, the
1f3bb 76 64 62 65 20 65 78 65 63 75 74 65 73 20 61 6e vdbe executes an
1f3bc 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2e 0a 20 OP_ResultRow..
1f3bd 20 2a 2a 20 43 6f 64 65 20 65 78 74 65 72 6e 61 ** Code externa
1f3be 6c 20 74 6f 20 74 68 65 20 56 64 62 65 20 74 68 l to the Vdbe th
1f3bf 65 6e 20 22 62 6f 72 72 6f 77 73 22 20 74 68 65 en "borrows" the
1f3c0 20 62 2d 74 72 65 65 20 63 75 72 73 6f 72 20 61 b-tree cursor a
1f3c1 6e 64 0a 20 20 2a 2a 20 75 73 65 73 20 69 74 20 nd. ** uses it
1f3c2 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 to implement the
1f3c3 20 62 6c 6f 62 5f 72 65 61 64 28 29 2c 20 62 6c blob_read(), bl
1f3c4 6f 62 5f 77 72 69 74 65 28 29 20 61 6e 64 20 0a ob_write() and .
1f3c5 20 20 2a 2a 20 62 6c 6f 62 5f 62 79 74 65 73 28 ** blob_bytes(
1f3c6 29 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 20 20 2a ) functions.. *
1f3c7 2a 0a 20 20 2a 2a 20 54 68 65 20 73 71 6c 69 74 *. ** The sqlit
1f3c8 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28 29 20 e3_blob_close()
1f3c9 66 75 6e 63 74 69 6f 6e 20 66 69 6e 61 6c 69 7a function finaliz
1f3ca 65 73 20 74 68 65 20 76 64 62 65 20 70 72 6f 67 es the vdbe prog
1f3cb 72 61 6d 2c 0a 20 20 2a 2a 20 77 68 69 63 68 20 ram,. ** which
1f3cc 63 6c 6f 73 65 73 20 74 68 65 20 62 2d 74 72 65 closes the b-tre
1f3cd 65 20 63 75 72 73 6f 72 20 61 6e 64 20 28 70 6f e cursor and (po
1f3ce 73 73 69 62 6c 79 29 20 63 6f 6d 6d 69 74 73 20 ssibly) commits
1f3cf 74 68 65 20 0a 20 20 2a 2a 20 74 72 61 6e 73 61 the . ** transa
1f3d0 63 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 73 74 ction.. */. st
1f3d1 61 74 69 63 20 63 6f 6e 73 74 20 56 64 62 65 4f atic const VdbeO
1f3d2 70 4c 69 73 74 20 6f 70 65 6e 42 6c 6f 62 5b 5d pList openBlob[]
1f3d3 20 3d 20 7b 0a 20 20 20 20 7b 4f 50 5f 54 72 61 = {. {OP_Tra
1f3d4 6e 73 61 63 74 69 6f 6e 2c 20 30 2c 20 30 2c 20 nsaction, 0, 0,
1f3d5 30 7d 2c 20 20 20 20 20 2f 2a 20 30 3a 20 53 74 0}, /* 0: St
1f3d6 61 72 74 20 61 20 74 72 61 6e 73 61 63 74 69 6f art a transactio
1f3d7 6e 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 56 65 72 n */. {OP_Ver
1f3d8 69 66 79 43 6f 6f 6b 69 65 2c 20 30 2c 20 30 2c ifyCookie, 0, 0,
1f3d9 20 30 7d 2c 20 20 20 20 2f 2a 20 31 3a 20 43 68 0}, /* 1: Ch
1f3da 65 63 6b 20 74 68 65 20 73 63 68 65 6d 61 20 63 eck the schema c
1f3db 6f 6f 6b 69 65 20 2a 2f 0a 20 20 20 20 7b 4f 50 ookie */. {OP
1f3dc 5f 54 61 62 6c 65 4c 6f 63 6b 2c 20 30 2c 20 30 _TableLock, 0, 0
1f3dd 2c 20 30 7d 2c 20 20 20 20 20 20 20 2f 2a 20 32 , 0}, /* 2
1f3de 3a 20 41 63 71 75 69 72 65 20 61 20 72 65 61 64 : Acquire a read
1f3df 20 6f 72 20 77 72 69 74 65 20 6c 6f 63 6b 20 2a or write lock *
1f3e0 2f 0a 0a 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 /.. /* One of
1f3e1 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 the following t
1f3e2 77 6f 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 wo instructions
1f3e3 69 73 20 72 65 70 6c 61 63 65 64 20 62 79 20 61 is replaced by a
1f3e4 6e 20 4f 50 5f 4e 6f 6f 70 2e 20 2a 2f 0a 20 20 n OP_Noop. */.
1f3e5 20 20 7b 4f 50 5f 4f 70 65 6e 52 65 61 64 2c 20 {OP_OpenRead,
1f3e6 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20 20 20 20 0, 0, 0},
1f3e7 20 2f 2a 20 33 3a 20 4f 70 65 6e 20 63 75 72 73 /* 3: Open curs
1f3e8 6f 72 20 30 20 66 6f 72 20 72 65 61 64 69 6e 67 or 0 for reading
1f3e9 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 4f 70 65 6e */. {OP_Open
1f3ea 57 72 69 74 65 2c 20 30 2c 20 30 2c 20 30 7d 2c Write, 0, 0, 0},
1f3eb 20 20 20 20 20 20 20 2f 2a 20 34 3a 20 4f 70 65 /* 4: Ope
1f3ec 6e 20 63 75 72 73 6f 72 20 30 20 66 6f 72 20 72 n cursor 0 for r
1f3ed 65 61 64 2f 77 72 69 74 65 20 2a 2f 0a 0a 20 20 ead/write */..
1f3ee 20 20 7b 4f 50 5f 56 61 72 69 61 62 6c 65 2c 20 {OP_Variable,
1f3ef 31 2c 20 31 2c 20 31 7d 2c 20 20 20 20 20 20 20 1, 1, 1},
1f3f0 20 2f 2a 20 35 3a 20 50 75 73 68 20 74 68 65 20 /* 5: Push the
1f3f1 72 6f 77 69 64 20 74 6f 20 74 68 65 20 73 74 61 rowid to the sta
1f3f2 63 6b 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 4e 6f ck */. {OP_No
1f3f3 74 45 78 69 73 74 73 2c 20 30 2c 20 39 2c 20 31 tExists, 0, 9, 1
1f3f4 7d 2c 20 20 20 20 20 20 20 2f 2a 20 36 3a 20 53 }, /* 6: S
1f3f5 65 65 6b 20 74 68 65 20 63 75 72 73 6f 72 20 2a eek the cursor *
1f3f6 2f 0a 20 20 20 20 7b 4f 50 5f 43 6f 6c 75 6d 6e /. {OP_Column
1f3f7 2c 20 30 2c 20 30 2c 20 31 7d 2c 20 20 20 20 20 , 0, 0, 1},
1f3f8 20 20 20 20 20 2f 2a 20 37 20 20 2a 2f 0a 20 20 /* 7 */.
1f3f9 20 20 7b 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c {OP_ResultRow,
1f3fa 20 31 2c 20 30 2c 20 30 7d 2c 20 20 20 20 20 20 1, 0, 0},
1f3fb 20 2f 2a 20 38 20 20 2a 2f 0a 20 20 20 20 7b 4f /* 8 */. {O
1f3fc 50 5f 43 6c 6f 73 65 2c 20 30 2c 20 30 2c 20 30 P_Close, 0, 0, 0
1f3fd 7d 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 }, /*
1f3fe 39 20 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 48 61 9 */. {OP_Ha
1f3ff 6c 74 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20 lt, 0, 0, 0},
1f400 20 20 20 20 20 20 20 20 20 2f 2a 20 31 30 20 2a /* 10 *
1f401 2f 0a 20 20 7d 3b 0a 0a 20 20 56 64 62 65 20 2a /. };.. Vdbe *
1f402 76 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 20 v = 0;. int rc
1f403 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 63 = SQLITE_OK;. c
1f404 68 61 72 20 2a 7a 45 72 72 20 3d 20 30 3b 0a 20 har *zErr = 0;.
1f405 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 Table *pTab;.
1f406 50 61 72 73 65 20 2a 70 50 61 72 73 65 3b 0a 0a Parse *pParse;..
1f407 20 20 2a 70 70 42 6c 6f 62 20 3d 20 30 3b 0a 20 *ppBlob = 0;.
1f408 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 sqlite3_mutex_e
1f409 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b nter(db->mutex);
1f40a 0a 20 20 70 50 61 72 73 65 20 3d 20 73 71 6c 69 . pParse = sqli
1f40b 74 65 33 53 74 61 63 6b 41 6c 6c 6f 63 52 61 77 te3StackAllocRaw
1f40c 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 50 61 (db, sizeof(*pPa
1f40d 72 73 65 29 29 3b 0a 20 20 69 66 28 20 70 50 61 rse));. if( pPa
1f40e 72 73 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 rse==0 ){. rc
1f40f 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b = SQLITE_NOMEM;
1f410 0a 20 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f . goto blob_o
1f411 70 65 6e 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 64 pen_out;. }. d
1f412 6f 20 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 o {. memset(p
1f413 50 61 72 73 65 2c 20 30 2c 20 73 69 7a 65 6f 66 Parse, 0, sizeof
1f414 28 50 61 72 73 65 29 29 3b 0a 20 20 20 20 70 50 (Parse));. pP
1f415 61 72 73 65 2d 3e 64 62 20 3d 20 64 62 3b 0a 0a arse->db = db;..
1f416 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 if( sqlite3S
1f417 61 66 65 74 79 4f 6e 28 64 62 29 20 29 7b 0a 20 afetyOn(db) ){.
1f418 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 sqlite3DbFr
1f419 65 65 28 64 62 2c 20 7a 45 72 72 29 3b 0a 20 20 ee(db, zErr);.
1f41a 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 63 6b sqlite3Stack
1f41b 46 72 65 65 28 64 62 2c 20 70 50 61 72 73 65 29 Free(db, pParse)
1f41c 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f ;. sqlite3_
1f41d 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e mutex_leave(db->
1f41e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 72 65 mutex);. re
1f41f 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 turn SQLITE_MISU
1f420 53 45 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73 SE;. }.. s
1f421 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 qlite3BtreeEnter
1f422 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20 70 54 61 All(db);. pTa
1f423 62 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 b = sqlite3Locat
1f424 65 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 30 eTable(pParse, 0
1f425 2c 20 7a 54 61 62 6c 65 2c 20 7a 44 62 29 3b 0a , zTable, zDb);.
1f426 20 20 20 20 69 66 28 20 70 54 61 62 20 26 26 20 if( pTab &&
1f427 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 IsVirtual(pTab)
1f428 29 7b 0a 20 20 20 20 20 20 70 54 61 62 20 3d 20 ){. pTab =
1f429 30 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 0;. sqlite3
1f42a 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c ErrorMsg(pParse,
1f42b 20 22 63 61 6e 6e 6f 74 20 6f 70 65 6e 20 76 69 "cannot open vi
1f42c 72 74 75 61 6c 20 74 61 62 6c 65 3a 20 25 73 22 rtual table: %s"
1f42d 2c 20 7a 54 61 62 6c 65 29 3b 0a 20 20 20 20 7d , zTable);. }
1f42e 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1f42f 4f 4d 49 54 5f 56 49 45 57 0a 20 20 20 20 69 66 OMIT_VIEW. if
1f430 28 20 70 54 61 62 20 26 26 20 70 54 61 62 2d 3e ( pTab && pTab->
1f431 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 20 pSelect ){.
1f432 20 70 54 61 62 20 3d 20 30 3b 0a 20 20 20 20 20 pTab = 0;.
1f433 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 sqlite3ErrorMsg
1f434 28 70 50 61 72 73 65 2c 20 22 63 61 6e 6e 6f 74 (pParse, "cannot
1f435 20 6f 70 65 6e 20 76 69 65 77 3a 20 25 73 22 2c open view: %s",
1f436 20 7a 54 61 62 6c 65 29 3b 0a 20 20 20 20 7d 0a zTable);. }.
1f437 23 65 6e 64 69 66 0a 20 20 20 20 69 66 28 20 21 #endif. if( !
1f438 70 54 61 62 20 29 7b 0a 20 20 20 20 20 20 69 66 pTab ){. if
1f439 28 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 ( pParse->zErrMs
1f43a 67 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c g ){. sql
1f43b 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a ite3DbFree(db, z
1f43c 45 72 72 29 3b 0a 20 20 20 20 20 20 20 20 7a 45 Err);. zE
1f43d 72 72 20 3d 20 70 50 61 72 73 65 2d 3e 7a 45 72 rr = pParse->zEr
1f43e 72 4d 73 67 3b 0a 20 20 20 20 20 20 20 20 70 50 rMsg;. pP
1f43f 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 arse->zErrMsg =
1f440 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 0;. }.
1f441 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 rc = SQLITE_ERR
1f442 4f 52 3b 0a 20 20 20 20 20 20 28 76 6f 69 64 29 OR;. (void)
1f443 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 sqlite3SafetyOff
1f444 28 64 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 (db);. sqli
1f445 74 65 33 42 74 72 65 65 4c 65 61 76 65 41 6c 6c te3BtreeLeaveAll
1f446 28 64 62 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f (db);. goto
1f447 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74 3b 0a blob_open_out;.
1f448 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 4e 6f }.. /* No
1f449 77 20 73 65 61 72 63 68 20 70 54 61 62 20 66 6f w search pTab fo
1f44a 72 20 74 68 65 20 65 78 61 63 74 20 63 6f 6c 75 r the exact colu
1f44b 6d 6e 2e 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 mn. */. for(i
1f44c 43 6f 6c 3d 30 3b 20 69 43 6f 6c 20 3c 20 70 54 Col=0; iCol < pT
1f44d 61 62 2d 3e 6e 43 6f 6c 3b 20 69 43 6f 6c 2b 2b ab->nCol; iCol++
1f44e 29 20 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 ) {. if( sq
1f44f 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 54 61 lite3StrICmp(pTa
1f450 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e b->aCol[iCol].zN
1f451 61 6d 65 2c 20 7a 43 6f 6c 75 6d 6e 29 3d 3d 30 ame, zColumn)==0
1f452 20 29 7b 0a 20 20 20 20 20 20 20 20 62 72 65 61 ){. brea
1f453 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d k;. }. }
1f454 0a 20 20 20 20 69 66 28 20 69 43 6f 6c 3d 3d 70 . if( iCol==p
1f455 54 61 62 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20 Tab->nCol ){.
1f456 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 sqlite3DbFree
1f457 28 64 62 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20 (db, zErr);.
1f458 20 20 7a 45 72 72 20 3d 20 73 71 6c 69 74 65 33 zErr = sqlite3
1f459 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 6e 6f 20 MPrintf(db, "no
1f45a 73 75 63 68 20 63 6f 6c 75 6d 6e 3a 20 5c 22 25 such column: \"%
1f45b 73 5c 22 22 2c 20 7a 43 6f 6c 75 6d 6e 29 3b 0a s\"", zColumn);.
1f45c 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 rc = SQLIT
1f45d 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 28 E_ERROR;. (
1f45e 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 void)sqlite3Safe
1f45f 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 20 20 20 tyOff(db);.
1f460 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 sqlite3BtreeLea
1f461 76 65 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20 20 veAll(db);.
1f462 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f 70 65 6e 5f goto blob_open_
1f463 6f 75 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 out;. }..
1f464 2f 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 /* If the value
1f465 69 73 20 62 65 69 6e 67 20 6f 70 65 6e 65 64 20 is being opened
1f466 66 6f 72 20 77 72 69 74 69 6e 67 2c 20 63 68 65 for writing, che
1f467 63 6b 20 74 68 61 74 20 74 68 65 0a 20 20 20 20 ck that the.
1f468 2a 2a 20 63 6f 6c 75 6d 6e 20 69 73 20 6e 6f 74 ** column is not
1f469 20 69 6e 64 65 78 65 64 2c 20 61 6e 64 20 74 68 indexed, and th
1f46a 61 74 20 69 74 20 69 73 20 6e 6f 74 20 70 61 72 at it is not par
1f46b 74 20 6f 66 20 61 20 66 6f 72 65 69 67 6e 20 6b t of a foreign k
1f46c 65 79 2e 20 0a 20 20 20 20 2a 2a 20 49 74 20 69 ey. . ** It i
1f46d 73 20 61 67 61 69 6e 73 74 20 74 68 65 20 72 75 s against the ru
1f46e 6c 65 73 20 74 6f 20 6f 70 65 6e 20 61 20 63 6f les to open a co
1f46f 6c 75 6d 6e 20 74 6f 20 77 68 69 63 68 20 65 69 lumn to which ei
1f470 74 68 65 72 20 6f 66 20 74 68 65 73 65 0a 20 20 ther of these.
1f471 20 20 2a 2a 20 64 65 73 63 72 69 70 74 69 6f 6e ** description
1f472 73 20 61 70 70 6c 69 65 73 20 66 6f 72 20 77 72 s applies for wr
1f473 69 74 69 6e 67 2e 20 20 2a 2f 0a 20 20 20 20 69 iting. */. i
1f474 66 28 20 66 6c 61 67 73 20 29 7b 0a 20 20 20 20 f( flags ){.
1f475 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 const char *zF
1f476 61 75 6c 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 ault = 0;.
1f477 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a 23 69 66 Index *pIdx;.#if
1f478 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1f479 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 0a 20 20 20 _FOREIGN_KEY.
1f47a 20 20 20 69 66 28 20 64 62 2d 3e 66 6c 61 67 73 if( db->flags
1f47b 26 53 51 4c 49 54 45 5f 46 6f 72 65 69 67 6e 4b &SQLITE_ForeignK
1f47c 65 79 73 20 29 7b 0a 20 20 20 20 20 20 20 20 2f eys ){. /
1f47d 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 65 * Check that the
1f47e 20 63 6f 6c 75 6d 6e 20 69 73 20 6e 6f 74 20 70 column is not p
1f47f 61 72 74 20 6f 66 20 61 6e 20 46 4b 20 63 68 69 art of an FK chi
1f480 6c 64 20 6b 65 79 20 64 65 66 69 6e 69 74 69 6f ld key definitio
1f481 6e 2e 20 49 74 0a 20 20 20 20 20 20 20 20 2a 2a n. It. **
1f482 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 is not necessar
1f483 79 20 74 6f 20 63 68 65 63 6b 20 69 66 20 69 74 y to check if it
1f484 20 69 73 20 70 61 72 74 20 6f 66 20 61 20 70 61 is part of a pa
1f485 72 65 6e 74 20 6b 65 79 2c 20 61 73 20 70 61 72 rent key, as par
1f486 65 6e 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 6b ent. ** k
1f487 65 79 20 63 6f 6c 75 6d 6e 73 20 6d 75 73 74 20 ey columns must
1f488 62 65 20 69 6e 64 65 78 65 64 2e 20 54 68 65 20 be indexed. The
1f489 63 68 65 63 6b 20 62 65 6c 6f 77 20 77 69 6c 6c check below will
1f48a 20 70 69 63 6b 20 75 70 20 74 68 69 73 20 0a 20 pick up this .
1f48b 20 20 20 20 20 20 20 2a 2a 20 63 61 73 65 2e 20 ** case.
1f48c 20 2a 2f 0a 20 20 20 20 20 20 20 20 46 4b 65 79 */. FKey
1f48d 20 2a 70 46 4b 65 79 3b 0a 20 20 20 20 20 20 20 *pFKey;.
1f48e 20 66 6f 72 28 70 46 4b 65 79 3d 70 54 61 62 2d for(pFKey=pTab-
1f48f 3e 70 46 4b 65 79 3b 20 70 46 4b 65 79 3b 20 70 >pFKey; pFKey; p
1f490 46 4b 65 79 3d 70 46 4b 65 79 2d 3e 70 4e 65 78 FKey=pFKey->pNex
1f491 74 46 72 6f 6d 29 7b 0a 20 20 20 20 20 20 20 20 tFrom){.
1f492 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 20 int j;.
1f493 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 46 for(j=0; j<pF
1f494 4b 65 79 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 29 7b Key->nCol; j++){
1f495 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 . if(
1f496 20 70 46 4b 65 79 2d 3e 61 43 6f 6c 5b 6a 5d 2e pFKey->aCol[j].
1f497 69 46 72 6f 6d 3d 3d 69 43 6f 6c 20 29 7b 0a 20 iFrom==iCol ){.
1f498 20 20 20 20 20 20 20 20 20 20 20 20 20 7a 46 61 zFa
1f499 75 6c 74 20 3d 20 22 66 6f 72 65 69 67 6e 20 6b ult = "foreign k
1f49a 65 79 22 3b 0a 20 20 20 20 20 20 20 20 20 20 20 ey";.
1f49b 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 }. }.
1f49c 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
1f49d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 66 6f .#endif. fo
1f49e 72 28 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e r(pIdx=pTab->pIn
1f49f 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d dex; pIdx; pIdx=
1f4a0 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 pIdx->pNext){.
1f4a1 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 int j;.
1f4a2 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c for(j=0; j<
1f4a3 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 6a pIdx->nColumn; j
1f4a4 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 ++){. i
1f4a5 66 28 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d f( pIdx->aiColum
1f4a6 6e 5b 6a 5d 3d 3d 69 43 6f 6c 20 29 7b 0a 20 20 n[j]==iCol ){.
1f4a7 20 20 20 20 20 20 20 20 20 20 7a 46 61 75 6c 74 zFault
1f4a8 20 3d 20 22 69 6e 64 65 78 65 64 22 3b 0a 20 20 = "indexed";.
1f4a9 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1f4aa 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
1f4ab 20 20 69 66 28 20 7a 46 61 75 6c 74 20 29 7b 0a if( zFault ){.
1f4ac 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 sqlite3D
1f4ad 62 46 72 65 65 28 64 62 2c 20 7a 45 72 72 29 3b bFree(db, zErr);
1f4ae 0a 20 20 20 20 20 20 20 20 7a 45 72 72 20 3d 20 . zErr =
1f4af 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 sqlite3MPrintf(d
1f4b0 62 2c 20 22 63 61 6e 6e 6f 74 20 6f 70 65 6e 20 b, "cannot open
1f4b1 25 73 20 63 6f 6c 75 6d 6e 20 66 6f 72 20 77 72 %s column for wr
1f4b2 69 74 69 6e 67 22 2c 20 7a 46 61 75 6c 74 29 3b iting", zFault);
1f4b3 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 . rc = SQ
1f4b4 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 LITE_ERROR;.
1f4b5 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 (void)sqlite
1f4b6 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 3SafetyOff(db);.
1f4b7 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 sqlite3B
1f4b8 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 treeLeaveAll(db)
1f4b9 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 62 ;. goto b
1f4ba 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 lob_open_out;.
1f4bb 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 }. }..
1f4bc 20 76 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 v = sqlite3Vdbe
1f4bd 43 72 65 61 74 65 28 64 62 29 3b 0a 20 20 20 20 Create(db);.
1f4be 69 66 28 20 76 20 29 7b 0a 20 20 20 20 20 20 69 if( v ){. i
1f4bf 6e 74 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 nt iDb = sqlite3
1f4c0 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 SchemaToIndex(db
1f4c1 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 , pTab->pSchema)
1f4c2 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 ;. sqlite3V
1f4c3 64 62 65 41 64 64 4f 70 4c 69 73 74 28 76 2c 20 dbeAddOpList(v,
1f4c4 73 69 7a 65 6f 66 28 6f 70 65 6e 42 6c 6f 62 29 sizeof(openBlob)
1f4c5 2f 73 69 7a 65 6f 66 28 56 64 62 65 4f 70 4c 69 /sizeof(VdbeOpLi
1f4c6 73 74 29 2c 20 6f 70 65 6e 42 6c 6f 62 29 3b 0a st), openBlob);.
1f4c7 20 20 20 20 20 20 66 6c 61 67 73 20 3d 20 21 21 flags = !!
1f4c8 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 flags;
1f4c9 20 20 20 20 20 20 20 2f 2a 20 66 6c 61 67 73 20 /* flags
1f4ca 3d 20 28 66 6c 61 67 73 20 3f 20 31 20 3a 20 30 = (flags ? 1 : 0
1f4cb 29 3b 20 2a 2f 0a 0a 20 20 20 20 20 20 2f 2a 20 ); */.. /*
1f4cc 43 6f 6e 66 69 67 75 72 65 20 74 68 65 20 4f 50 Configure the OP
1f4cd 5f 54 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a _Transaction */.
1f4ce 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
1f4cf 65 43 68 61 6e 67 65 50 31 28 76 2c 20 30 2c 20 eChangeP1(v, 0,
1f4d0 69 44 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 iDb);. sqli
1f4d1 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 32 28 te3VdbeChangeP2(
1f4d2 76 2c 20 30 2c 20 66 6c 61 67 73 29 3b 0a 0a 20 v, 0, flags);..
1f4d3 20 20 20 20 20 2f 2a 20 43 6f 6e 66 69 67 75 72 /* Configur
1f4d4 65 20 74 68 65 20 4f 50 5f 56 65 72 69 66 79 43 e the OP_VerifyC
1f4d5 6f 6f 6b 69 65 20 2a 2f 0a 20 20 20 20 20 20 73 ookie */. s
1f4d6 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 qlite3VdbeChange
1f4d7 50 31 28 76 2c 20 31 2c 20 69 44 62 29 3b 0a 20 P1(v, 1, iDb);.
1f4d8 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1f4d9 43 68 61 6e 67 65 50 32 28 76 2c 20 31 2c 20 70 ChangeP2(v, 1, p
1f4da 54 61 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 73 63 Tab->pSchema->sc
1f4db 68 65 6d 61 5f 63 6f 6f 6b 69 65 29 3b 0a 0a 20 hema_cookie);..
1f4dc 20 20 20 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 /* Make sur
1f4dd 65 20 61 20 6d 75 74 65 78 20 69 73 20 68 65 6c e a mutex is hel
1f4de 64 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 74 d on the table t
1f4df 6f 20 62 65 20 61 63 63 65 73 73 65 64 20 2a 2f o be accessed */
1f4e0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
1f4e1 62 65 55 73 65 73 42 74 72 65 65 28 76 2c 20 69 beUsesBtree(v, i
1f4e2 44 62 29 3b 20 0a 0a 20 20 20 20 20 20 2f 2a 20 Db); .. /*
1f4e3 43 6f 6e 66 69 67 75 72 65 20 74 68 65 20 4f 50 Configure the OP
1f4e4 5f 54 61 62 6c 65 4c 6f 63 6b 20 69 6e 73 74 72 _TableLock instr
1f4e5 75 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 uction */.
1f4e6 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 sqlite3VdbeChang
1f4e7 65 50 31 28 76 2c 20 32 2c 20 69 44 62 29 3b 0a eP1(v, 2, iDb);.
1f4e8 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
1f4e9 65 43 68 61 6e 67 65 50 32 28 76 2c 20 32 2c 20 eChangeP2(v, 2,
1f4ea 70 54 61 62 2d 3e 74 6e 75 6d 29 3b 0a 20 20 20 pTab->tnum);.
1f4eb 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 sqlite3VdbeCh
1f4ec 61 6e 67 65 50 33 28 76 2c 20 32 2c 20 66 6c 61 angeP3(v, 2, fla
1f4ed 67 73 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 gs);. sqlit
1f4ee 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 e3VdbeChangeP4(v
1f4ef 2c 20 32 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 , 2, pTab->zName
1f4f0 2c 20 50 34 5f 54 52 41 4e 53 49 45 4e 54 29 3b , P4_TRANSIENT);
1f4f1 0a 0a 20 20 20 20 20 20 2f 2a 20 52 65 6d 6f 76 .. /* Remov
1f4f2 65 20 65 69 74 68 65 72 20 74 68 65 20 4f 50 5f e either the OP_
1f4f3 4f 70 65 6e 57 72 69 74 65 20 6f 72 20 4f 70 65 OpenWrite or Ope
1f4f4 6e 52 65 61 64 2e 20 53 65 74 20 74 68 65 20 50 nRead. Set the P
1f4f5 32 20 0a 20 20 20 20 20 20 2a 2a 20 70 61 72 61 2 . ** para
1f4f6 6d 65 74 65 72 20 6f 66 20 74 68 65 20 6f 74 68 meter of the oth
1f4f7 65 72 20 74 6f 20 70 54 61 62 2d 3e 74 6e 75 6d er to pTab->tnum
1f4f8 2e 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 . */. sqli
1f4f9 74 65 33 56 64 62 65 43 68 61 6e 67 65 54 6f 4e te3VdbeChangeToN
1f4fa 6f 6f 70 28 76 2c 20 34 20 2d 20 66 6c 61 67 73 oop(v, 4 - flags
1f4fb 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 , 1);. sqli
1f4fc 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 32 28 te3VdbeChangeP2(
1f4fd 76 2c 20 33 20 2b 20 66 6c 61 67 73 2c 20 70 54 v, 3 + flags, pT
1f4fe 61 62 2d 3e 74 6e 75 6d 29 3b 0a 20 20 20 20 20 ab->tnum);.
1f4ff 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e sqlite3VdbeChan
1f500 67 65 50 33 28 76 2c 20 33 20 2b 20 66 6c 61 67 geP3(v, 3 + flag
1f501 73 2c 20 69 44 62 29 3b 0a 0a 20 20 20 20 20 20 s, iDb);..
1f502 2f 2a 20 43 6f 6e 66 69 67 75 72 65 20 74 68 65 /* Configure the
1f503 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d number of colum
1f504 6e 73 2e 20 43 6f 6e 66 69 67 75 72 65 20 74 68 ns. Configure th
1f505 65 20 63 75 72 73 6f 72 20 74 6f 0a 20 20 20 20 e cursor to.
1f506 20 20 2a 2a 20 74 68 69 6e 6b 20 74 68 61 74 20 ** think that
1f507 74 68 65 20 74 61 62 6c 65 20 68 61 73 20 6f 6e the table has on
1f508 65 20 6d 6f 72 65 20 63 6f 6c 75 6d 6e 20 74 68 e more column th
1f509 61 6e 20 69 74 20 72 65 61 6c 6c 79 0a 20 20 20 an it really.
1f50a 20 20 20 2a 2a 20 64 6f 65 73 2e 20 41 6e 20 4f ** does. An O
1f50b 50 5f 43 6f 6c 75 6d 6e 20 74 6f 20 72 65 74 72 P_Column to retr
1f50c 69 65 76 65 20 74 68 69 73 20 69 6d 61 67 69 6e ieve this imagin
1f50d 61 72 79 20 63 6f 6c 75 6d 6e 20 77 69 6c 6c 0a ary column will.
1f50e 20 20 20 20 20 20 2a 2a 20 61 6c 77 61 79 73 20 ** always
1f50f 72 65 74 75 72 6e 20 61 6e 20 53 51 4c 20 4e 55 return an SQL NU
1f510 4c 4c 2e 20 54 68 69 73 20 69 73 20 75 73 65 66 LL. This is usef
1f511 75 6c 20 62 65 63 61 75 73 65 20 69 74 20 6d 65 ul because it me
1f512 61 6e 73 0a 20 20 20 20 20 20 2a 2a 20 77 65 20 ans. ** we
1f513 63 61 6e 20 69 6e 76 6f 6b 65 20 4f 50 5f 43 6f can invoke OP_Co
1f514 6c 75 6d 6e 20 74 6f 20 66 69 6c 6c 20 69 6e 20 lumn to fill in
1f515 74 68 65 20 76 64 62 65 20 63 75 72 73 6f 72 73 the vdbe cursors
1f516 20 74 79 70 65 20 0a 20 20 20 20 20 20 2a 2a 20 type . **
1f517 61 6e 64 20 6f 66 66 73 65 74 20 63 61 63 68 65 and offset cache
1f518 20 77 69 74 68 6f 75 74 20 63 61 75 73 69 6e 67 without causing
1f519 20 61 6e 79 20 49 4f 2e 0a 20 20 20 20 20 20 2a any IO.. *
1f51a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 /. sqlite3V
1f51b 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 33 dbeChangeP4(v, 3
1f51c 2b 66 6c 61 67 73 2c 20 53 51 4c 49 54 45 5f 49 +flags, SQLITE_I
1f51d 4e 54 5f 54 4f 5f 50 54 52 28 70 54 61 62 2d 3e NT_TO_PTR(pTab->
1f51e 6e 43 6f 6c 2b 31 29 2c 50 34 5f 49 4e 54 33 32 nCol+1),P4_INT32
1f51f 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
1f520 56 64 62 65 43 68 61 6e 67 65 50 32 28 76 2c 20 VdbeChangeP2(v,
1f521 37 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 29 3b 0a 7, pTab->nCol);.
1f522 20 20 20 20 20 20 69 66 28 20 21 64 62 2d 3e 6d if( !db->m
1f523 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 allocFailed ){.
1f524 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 sqlite3Vd
1f525 62 65 4d 61 6b 65 52 65 61 64 79 28 76 2c 20 31 beMakeReady(v, 1
1f526 2c 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20 30 29 , 1, 1, 0, 0, 0)
1f527 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
1f528 20 20 20 0a 20 20 20 20 73 71 6c 69 74 65 33 42 . sqlite3B
1f529 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 treeLeaveAll(db)
1f52a 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 ;. rc = sqlit
1f52b 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b e3SafetyOff(db);
1f52c 0a 20 20 20 20 69 66 28 20 4e 45 56 45 52 28 72 . if( NEVER(r
1f52d 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 7c 7c c!=SQLITE_OK) ||
1f52e 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 db->mallocFaile
1f52f 64 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 d ){. goto
1f530 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 blob_open_out;.
1f531 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c 69 74 65 }.. sqlite
1f532 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 28 73 71 3_bind_int64((sq
1f533 6c 69 74 65 33 5f 73 74 6d 74 20 2a 29 76 2c 20 lite3_stmt *)v,
1f534 31 2c 20 69 52 6f 77 29 3b 0a 20 20 20 20 72 63 1, iRow);. rc
1f535 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 = sqlite3_step(
1f536 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 29 (sqlite3_stmt *)
1f537 76 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d v);. if( rc!=
1f538 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 SQLITE_ROW ){.
1f539 20 20 20 20 6e 41 74 74 65 6d 70 74 2b 2b 3b 0a nAttempt++;.
1f53a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
1f53b 65 33 5f 66 69 6e 61 6c 69 7a 65 28 28 73 71 6c e3_finalize((sql
1f53c 69 74 65 33 5f 73 74 6d 74 20 2a 29 76 29 3b 0a ite3_stmt *)v);.
1f53d 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 sqlite3DbF
1f53e 72 65 65 28 64 62 2c 20 7a 45 72 72 29 3b 0a 20 ree(db, zErr);.
1f53f 20 20 20 20 20 7a 45 72 72 20 3d 20 73 71 6c 69 zErr = sqli
1f540 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 73 te3MPrintf(db, s
1f541 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 64 62 qlite3_errmsg(db
1f542 29 29 3b 0a 20 20 20 20 20 20 76 20 3d 20 30 3b ));. v = 0;
1f543 0a 20 20 20 20 7d 0a 20 20 7d 20 77 68 69 6c 65 . }. } while
1f544 28 20 6e 41 74 74 65 6d 70 74 3c 35 20 26 26 20 ( nAttempt<5 &&
1f545 72 63 3d 3d 53 51 4c 49 54 45 5f 53 43 48 45 4d rc==SQLITE_SCHEM
1f546 41 20 29 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d A );.. if( rc==
1f547 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 SQLITE_ROW ){.
1f548 20 20 2f 2a 20 54 68 65 20 72 6f 77 2d 72 65 63 /* The row-rec
1f549 6f 72 64 20 68 61 73 20 62 65 65 6e 20 6f 70 65 ord has been ope
1f54a 6e 65 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 ned successfully
1f54b 2e 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 65 . Check that the
1f54c 0a 20 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 69 . ** column i
1f54d 6e 20 71 75 65 73 74 69 6f 6e 20 63 6f 6e 74 61 n question conta
1f54e 69 6e 73 20 74 65 78 74 20 6f 72 20 61 20 62 6c ins text or a bl
1f54f 6f 62 2e 20 49 66 20 69 74 20 63 6f 6e 74 61 69 ob. If it contai
1f550 6e 73 0a 20 20 20 20 2a 2a 20 74 65 78 74 2c 20 ns. ** text,
1f551 69 74 20 69 73 20 75 70 20 74 6f 20 74 68 65 20 it is up to the
1f552 63 61 6c 6c 65 72 20 74 6f 20 67 65 74 20 74 68 caller to get th
1f553 65 20 65 6e 63 6f 64 69 6e 67 20 72 69 67 68 74 e encoding right
1f554 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 49 6e 63 .. */. Inc
1f555 72 62 6c 6f 62 20 2a 70 42 6c 6f 62 3b 0a 20 20 rblob *pBlob;.
1f556 20 20 75 33 32 20 74 79 70 65 20 3d 20 76 2d 3e u32 type = v->
1f557 61 70 43 73 72 5b 30 5d 2d 3e 61 54 79 70 65 5b apCsr[0]->aType[
1f558 69 43 6f 6c 5d 3b 0a 0a 20 20 20 20 69 66 28 20 iCol];.. if(
1f559 74 79 70 65 3c 31 32 20 29 7b 0a 20 20 20 20 20 type<12 ){.
1f55a 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
1f55b 62 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20 20 20 b, zErr);.
1f55c 7a 45 72 72 20 3d 20 73 71 6c 69 74 65 33 4d 50 zErr = sqlite3MP
1f55d 72 69 6e 74 66 28 64 62 2c 20 22 63 61 6e 6e 6f rintf(db, "canno
1f55e 74 20 6f 70 65 6e 20 76 61 6c 75 65 20 6f 66 20 t open value of
1f55f 74 79 70 65 20 25 73 22 2c 0a 20 20 20 20 20 20 type %s",.
1f560 20 20 20 20 74 79 70 65 3d 3d 30 3f 22 6e 75 6c type==0?"nul
1f561 6c 22 3a 20 74 79 70 65 3d 3d 37 3f 22 72 65 61 l": type==7?"rea
1f562 6c 22 3a 20 22 69 6e 74 65 67 65 72 22 0a 20 20 l": "integer".
1f563 20 20 20 20 29 3b 0a 20 20 20 20 20 20 72 63 20 );. rc
1f564 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a = SQLITE_ERROR;.
1f565 20 20 20 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f goto blob_
1f566 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a open_out;. }.
1f567 20 20 20 20 70 42 6c 6f 62 20 3d 20 28 49 6e 63 pBlob = (Inc
1f568 72 62 6c 6f 62 20 2a 29 73 71 6c 69 74 65 33 44 rblob *)sqlite3D
1f569 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 bMallocZero(db,
1f56a 73 69 7a 65 6f 66 28 49 6e 63 72 62 6c 6f 62 29 sizeof(Incrblob)
1f56b 29 3b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d );. if( db->m
1f56c 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 allocFailed ){.
1f56d 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 sqlite3DbFr
1f56e 65 65 28 64 62 2c 20 70 42 6c 6f 62 29 3b 0a 20 ee(db, pBlob);.
1f56f 20 20 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f goto blob_o
1f570 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 pen_out;. }.
1f571 20 20 20 70 42 6c 6f 62 2d 3e 66 6c 61 67 73 20 pBlob->flags
1f572 3d 20 66 6c 61 67 73 3b 0a 20 20 20 20 70 42 6c = flags;. pBl
1f573 6f 62 2d 3e 70 43 73 72 20 3d 20 20 76 2d 3e 61 ob->pCsr = v->a
1f574 70 43 73 72 5b 30 5d 2d 3e 70 43 75 72 73 6f 72 pCsr[0]->pCursor
1f575 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 ;. sqlite3Btr
1f576 65 65 45 6e 74 65 72 43 75 72 73 6f 72 28 70 42 eeEnterCursor(pB
1f577 6c 6f 62 2d 3e 70 43 73 72 29 3b 0a 20 20 20 20 lob->pCsr);.
1f578 73 71 6c 69 74 65 33 42 74 72 65 65 43 61 63 68 sqlite3BtreeCach
1f579 65 4f 76 65 72 66 6c 6f 77 28 70 42 6c 6f 62 2d eOverflow(pBlob-
1f57a 3e 70 43 73 72 29 3b 0a 20 20 20 20 73 71 6c 69 >pCsr);. sqli
1f57b 74 65 33 42 74 72 65 65 4c 65 61 76 65 43 75 72 te3BtreeLeaveCur
1f57c 73 6f 72 28 70 42 6c 6f 62 2d 3e 70 43 73 72 29 sor(pBlob->pCsr)
1f57d 3b 0a 20 20 20 20 70 42 6c 6f 62 2d 3e 70 53 74 ;. pBlob->pSt
1f57e 6d 74 20 3d 20 28 73 71 6c 69 74 65 33 5f 73 74 mt = (sqlite3_st
1f57f 6d 74 20 2a 29 76 3b 0a 20 20 20 20 70 42 6c 6f mt *)v;. pBlo
1f580 62 2d 3e 69 4f 66 66 73 65 74 20 3d 20 76 2d 3e b->iOffset = v->
1f581 61 70 43 73 72 5b 30 5d 2d 3e 61 4f 66 66 73 65 apCsr[0]->aOffse
1f582 74 5b 69 43 6f 6c 5d 3b 0a 20 20 20 20 70 42 6c t[iCol];. pBl
1f583 6f 62 2d 3e 6e 42 79 74 65 20 3d 20 73 71 6c 69 ob->nByte = sqli
1f584 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 te3VdbeSerialTyp
1f585 65 4c 65 6e 28 74 79 70 65 29 3b 0a 20 20 20 20 eLen(type);.
1f586 70 42 6c 6f 62 2d 3e 64 62 20 3d 20 64 62 3b 0a pBlob->db = db;.
1f587 20 20 20 20 2a 70 70 42 6c 6f 62 20 3d 20 28 73 *ppBlob = (s
1f588 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 29 70 42 qlite3_blob *)pB
1f589 6c 6f 62 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 lob;. rc = SQ
1f58a 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 LITE_OK;. }else
1f58b 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
1f58c 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 OK ){. sqlite
1f58d 33 44 62 46 72 65 65 28 64 62 2c 20 7a 45 72 72 3DbFree(db, zErr
1f58e 29 3b 0a 20 20 20 20 7a 45 72 72 20 3d 20 73 71 );. zErr = sq
1f58f 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c lite3MPrintf(db,
1f590 20 22 6e 6f 20 73 75 63 68 20 72 6f 77 69 64 3a "no such rowid:
1f591 20 25 6c 6c 64 22 2c 20 69 52 6f 77 29 3b 0a 20 %lld", iRow);.
1f592 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 rc = SQLITE_E
1f593 52 52 4f 52 3b 0a 20 20 7d 0a 0a 62 6c 6f 62 5f RROR;. }..blob_
1f594 6f 70 65 6e 5f 6f 75 74 3a 0a 20 20 69 66 28 20 open_out:. if(
1f595 76 20 26 26 20 28 72 63 21 3d 53 51 4c 49 54 45 v && (rc!=SQLITE
1f596 5f 4f 4b 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f _OK || db->mallo
1f597 63 46 61 69 6c 65 64 29 20 29 7b 0a 20 20 20 20 cFailed) ){.
1f598 73 71 6c 69 74 65 33 56 64 62 65 46 69 6e 61 6c sqlite3VdbeFinal
1f599 69 7a 65 28 76 29 3b 0a 20 20 7d 0a 20 20 73 71 ize(v);. }. sq
1f59a 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 72 lite3Error(db, r
1f59b 63 2c 20 7a 45 72 72 29 3b 0a 20 20 73 71 6c 69 c, zErr);. sqli
1f59c 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 45 te3DbFree(db, zE
1f59d 72 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 74 rr);. sqlite3St
1f59e 61 63 6b 46 72 65 65 28 64 62 2c 20 70 50 61 72 ackFree(db, pPar
1f59f 73 65 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 se);. rc = sqli
1f5a0 74 65 33 41 70 69 45 78 69 74 28 64 62 2c 20 72 te3ApiExit(db, r
1f5a1 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 c);. sqlite3_mu
1f5a2 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 tex_leave(db->mu
1f5a3 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 tex);. return r
1f5a4 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 c;.}../*.** Clos
1f5a5 65 20 61 20 62 6c 6f 62 20 68 61 6e 64 6c 65 20 e a blob handle
1f5a6 74 68 61 74 20 77 61 73 20 70 72 65 76 69 6f 75 that was previou
1f5a7 73 6c 79 20 63 72 65 61 74 65 64 20 75 73 69 6e sly created usin
1f5a8 67 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 6c 6f g.** sqlite3_blo
1f5a9 62 5f 6f 70 65 6e 28 29 2e 0a 2a 2f 0a 53 51 4c b_open()..*/.SQL
1f5aa 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
1f5ab 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28 73 te3_blob_close(s
1f5ac 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 70 42 6c qlite3_blob *pBl
1f5ad 6f 62 29 7b 0a 20 20 49 6e 63 72 62 6c 6f 62 20 ob){. Incrblob
1f5ae 2a 70 20 3d 20 28 49 6e 63 72 62 6c 6f 62 20 2a *p = (Incrblob *
1f5af 29 70 42 6c 6f 62 3b 0a 20 20 69 6e 74 20 72 63 )pBlob;. int rc
1f5b0 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b ;. sqlite3 *db;
1f5b1 0a 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 .. if( p ){.
1f5b2 20 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 20 db = p->db;.
1f5b3 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 sqlite3_mutex_e
1f5b4 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b nter(db->mutex);
1f5b5 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
1f5b6 33 5f 66 69 6e 61 6c 69 7a 65 28 70 2d 3e 70 53 3_finalize(p->pS
1f5b7 74 6d 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 tmt);. sqlite
1f5b8 33 44 62 46 72 65 65 28 64 62 2c 20 70 29 3b 0a 3DbFree(db, p);.
1f5b9 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 sqlite3_mute
1f5ba 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 x_leave(db->mute
1f5bb 78 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 x);. }else{.
1f5bc 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
1f5bd 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
1f5be 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 65 72 66 6f ;.}../*.** Perfo
1f5bf 72 6d 20 61 20 72 65 61 64 20 6f 72 20 77 72 69 rm a read or wri
1f5c0 74 65 20 6f 70 65 72 61 74 69 6f 6e 20 6f 6e 20 te operation on
1f5c1 61 20 62 6c 6f 62 0a 2a 2f 0a 73 74 61 74 69 63 a blob.*/.static
1f5c2 20 69 6e 74 20 62 6c 6f 62 52 65 61 64 57 72 69 int blobReadWri
1f5c3 74 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 62 6c te(. sqlite3_bl
1f5c4 6f 62 20 2a 70 42 6c 6f 62 2c 20 0a 20 20 76 6f ob *pBlob, . vo
1f5c5 69 64 20 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c id *z, . int n,
1f5c6 20 0a 20 20 69 6e 74 20 69 4f 66 66 73 65 74 2c . int iOffset,
1f5c7 20 0a 20 20 69 6e 74 20 28 2a 78 43 61 6c 6c 29 . int (*xCall)
1f5c8 28 42 74 43 75 72 73 6f 72 2a 2c 20 75 33 32 2c (BtCursor*, u32,
1f5c9 20 75 33 32 2c 20 76 6f 69 64 2a 29 0a 29 7b 0a u32, void*).){.
1f5ca 20 20 69 6e 74 20 72 63 3b 0a 20 20 49 6e 63 72 int rc;. Incr
1f5cb 62 6c 6f 62 20 2a 70 20 3d 20 28 49 6e 63 72 62 blob *p = (Incrb
1f5cc 6c 6f 62 20 2a 29 70 42 6c 6f 62 3b 0a 20 20 56 lob *)pBlob;. V
1f5cd 64 62 65 20 2a 76 3b 0a 20 20 73 71 6c 69 74 65 dbe *v;. sqlite
1f5ce 33 20 2a 64 62 3b 0a 0a 20 20 69 66 28 20 70 3d 3 *db;.. if( p=
1f5cf 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 =0 ) return SQLI
1f5d0 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 64 62 20 TE_MISUSE;. db
1f5d1 3d 20 70 2d 3e 64 62 3b 0a 20 20 73 71 6c 69 74 = p->db;. sqlit
1f5d2 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 e3_mutex_enter(d
1f5d3 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 76 20 3d b->mutex);. v =
1f5d4 20 28 56 64 62 65 2a 29 70 2d 3e 70 53 74 6d 74 (Vdbe*)p->pStmt
1f5d5 3b 0a 0a 20 20 69 66 28 20 6e 3c 30 20 7c 7c 20 ;.. if( n<0 ||
1f5d6 69 4f 66 66 73 65 74 3c 30 20 7c 7c 20 28 69 4f iOffset<0 || (iO
1f5d7 66 66 73 65 74 2b 6e 29 3e 70 2d 3e 6e 42 79 74 ffset+n)>p->nByt
1f5d8 65 20 29 7b 0a 20 20 20 20 2f 2a 20 52 65 71 75 e ){. /* Requ
1f5d9 65 73 74 20 69 73 20 6f 75 74 20 6f 66 20 72 61 est is out of ra
1f5da 6e 67 65 2e 20 52 65 74 75 72 6e 20 61 20 74 72 nge. Return a tr
1f5db 61 6e 73 69 65 6e 74 20 65 72 72 6f 72 2e 20 2a ansient error. *
1f5dc 2f 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 /. rc = SQLIT
1f5dd 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 73 71 6c E_ERROR;. sql
1f5de 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51 ite3Error(db, SQ
1f5df 4c 49 54 45 5f 45 52 52 4f 52 2c 20 30 29 3b 0a LITE_ERROR, 0);.
1f5e0 20 20 7d 20 65 6c 73 65 20 69 66 28 20 76 3d 3d } else if( v==
1f5e1 30 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 0 ){. /* If t
1f5e2 68 65 72 65 20 69 73 20 6e 6f 20 73 74 61 74 65 here is no state
1f5e3 6d 65 6e 74 20 68 61 6e 64 6c 65 2c 20 74 68 65 ment handle, the
1f5e4 6e 20 74 68 65 20 62 6c 6f 62 2d 68 61 6e 64 6c n the blob-handl
1f5e5 65 20 68 61 73 0a 20 20 20 20 2a 2a 20 61 6c 72 e has. ** alr
1f5e6 65 61 64 79 20 62 65 65 6e 20 69 6e 76 61 6c 69 eady been invali
1f5e7 64 61 74 65 64 2e 20 52 65 74 75 72 6e 20 53 51 dated. Return SQ
1f5e8 4c 49 54 45 5f 41 42 4f 52 54 20 69 6e 20 74 68 LITE_ABORT in th
1f5e9 69 73 20 63 61 73 65 2e 0a 20 20 20 20 2a 2f 0a is case.. */.
1f5ea 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
1f5eb 41 42 4f 52 54 3b 0a 20 20 7d 65 6c 73 65 7b 0a ABORT;. }else{.
1f5ec 20 20 20 20 2f 2a 20 43 61 6c 6c 20 65 69 74 68 /* Call eith
1f5ed 65 72 20 42 74 72 65 65 44 61 74 61 28 29 20 6f er BtreeData() o
1f5ee 72 20 42 74 72 65 65 50 75 74 44 61 74 61 28 29 r BtreePutData()
1f5ef 2e 20 49 66 20 53 51 4c 49 54 45 5f 41 42 4f 52 . If SQLITE_ABOR
1f5f0 54 20 69 73 0a 20 20 20 20 2a 2a 20 72 65 74 75 T is. ** retu
1f5f1 72 6e 65 64 2c 20 63 6c 65 61 6e 2d 75 70 20 74 rned, clean-up t
1f5f2 68 65 20 73 74 61 74 65 6d 65 6e 74 20 68 61 6e he statement han
1f5f3 64 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 dle.. */.
1f5f4 61 73 73 65 72 74 28 20 64 62 20 3d 3d 20 76 2d assert( db == v-
1f5f5 3e 64 62 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 >db );. sqlit
1f5f6 65 33 42 74 72 65 65 45 6e 74 65 72 43 75 72 73 e3BtreeEnterCurs
1f5f7 6f 72 28 70 2d 3e 70 43 73 72 29 3b 0a 20 20 20 or(p->pCsr);.
1f5f8 20 72 63 20 3d 20 78 43 61 6c 6c 28 70 2d 3e 70 rc = xCall(p->p
1f5f9 43 73 72 2c 20 69 4f 66 66 73 65 74 2b 70 2d 3e Csr, iOffset+p->
1f5fa 69 4f 66 66 73 65 74 2c 20 6e 2c 20 7a 29 3b 0a iOffset, n, z);.
1f5fb 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 sqlite3Btree
1f5fc 4c 65 61 76 65 43 75 72 73 6f 72 28 70 2d 3e 70 LeaveCursor(p->p
1f5fd 43 73 72 29 3b 0a 20 20 20 20 69 66 28 20 72 63 Csr);. if( rc
1f5fe 3d 3d 53 51 4c 49 54 45 5f 41 42 4f 52 54 20 29 ==SQLITE_ABORT )
1f5ff 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 {. sqlite3V
1f600 64 62 65 46 69 6e 61 6c 69 7a 65 28 76 29 3b 0a dbeFinalize(v);.
1f601 20 20 20 20 20 20 70 2d 3e 70 53 74 6d 74 20 3d p->pStmt =
1f602 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 0;. }else{.
1f603 20 20 20 20 20 64 62 2d 3e 65 72 72 43 6f 64 65 db->errCode
1f604 20 3d 20 72 63 3b 0a 20 20 20 20 20 20 76 2d 3e = rc;. v->
1f605 72 63 20 3d 20 72 63 3b 0a 20 20 20 20 7d 0a 20 rc = rc;. }.
1f606 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 }. rc = sqlite
1f607 33 41 70 69 45 78 69 74 28 64 62 2c 20 72 63 29 3ApiExit(db, rc)
1f608 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 ;. sqlite3_mute
1f609 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 x_leave(db->mute
1f60a 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b x);. return rc;
1f60b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 64 .}../*.** Read d
1f60c 61 74 61 20 66 72 6f 6d 20 61 20 62 6c 6f 62 20 ata from a blob
1f60d 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 handle..*/.SQLIT
1f60e 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
1f60f 33 5f 62 6c 6f 62 5f 72 65 61 64 28 73 71 6c 69 3_blob_read(sqli
1f610 74 65 33 5f 62 6c 6f 62 20 2a 70 42 6c 6f 62 2c te3_blob *pBlob,
1f611 20 76 6f 69 64 20 2a 7a 2c 20 69 6e 74 20 6e 2c void *z, int n,
1f612 20 69 6e 74 20 69 4f 66 66 73 65 74 29 7b 0a 20 int iOffset){.
1f613 20 72 65 74 75 72 6e 20 62 6c 6f 62 52 65 61 64 return blobRead
1f614 57 72 69 74 65 28 70 42 6c 6f 62 2c 20 7a 2c 20 Write(pBlob, z,
1f615 6e 2c 20 69 4f 66 66 73 65 74 2c 20 73 71 6c 69 n, iOffset, sqli
1f616 74 65 33 42 74 72 65 65 44 61 74 61 29 3b 0a 7d te3BtreeData);.}
1f617 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 64 61 ../*.** Write da
1f618 74 61 20 74 6f 20 61 20 62 6c 6f 62 20 68 61 6e ta to a blob han
1f619 64 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 dle..*/.SQLITE_A
1f61a 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 PI int sqlite3_b
1f61b 6c 6f 62 5f 77 72 69 74 65 28 73 71 6c 69 74 65 lob_write(sqlite
1f61c 33 5f 62 6c 6f 62 20 2a 70 42 6c 6f 62 2c 20 63 3_blob *pBlob, c
1f61d 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 2c 20 69 6e onst void *z, in
1f61e 74 20 6e 2c 20 69 6e 74 20 69 4f 66 66 73 65 74 t n, int iOffset
1f61f 29 7b 0a 20 20 72 65 74 75 72 6e 20 62 6c 6f 62 ){. return blob
1f620 52 65 61 64 57 72 69 74 65 28 70 42 6c 6f 62 2c ReadWrite(pBlob,
1f621 20 28 76 6f 69 64 20 2a 29 7a 2c 20 6e 2c 20 69 (void *)z, n, i
1f622 4f 66 66 73 65 74 2c 20 73 71 6c 69 74 65 33 42 Offset, sqlite3B
1f623 74 72 65 65 50 75 74 44 61 74 61 29 3b 0a 7d 0a treePutData);.}.
1f624 0a 2f 2a 0a 2a 2a 20 51 75 65 72 79 20 61 20 62 ./*.** Query a b
1f625 6c 6f 62 20 68 61 6e 64 6c 65 20 66 6f 72 20 74 lob handle for t
1f626 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 he size of the d
1f627 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 49 ata..**.** The I
1f628 6e 63 72 62 6c 6f 62 2e 6e 42 79 74 65 20 66 69 ncrblob.nByte fi
1f629 65 6c 64 20 69 73 20 66 69 78 65 64 20 66 6f 72 eld is fixed for
1f62a 20 74 68 65 20 6c 69 66 65 74 69 6d 65 20 6f 66 the lifetime of
1f62b 20 74 68 65 20 49 6e 63 72 62 6c 6f 62 0a 2a 2a the Incrblob.**
1f62c 20 73 6f 20 6e 6f 20 6d 75 74 65 78 20 69 73 20 so no mutex is
1f62d 72 65 71 75 69 72 65 64 20 66 6f 72 20 61 63 63 required for acc
1f62e 65 73 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 ess..*/.SQLITE_A
1f62f 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 PI int sqlite3_b
1f630 6c 6f 62 5f 62 79 74 65 73 28 73 71 6c 69 74 65 lob_bytes(sqlite
1f631 33 5f 62 6c 6f 62 20 2a 70 42 6c 6f 62 29 7b 0a 3_blob *pBlob){.
1f632 20 20 49 6e 63 72 62 6c 6f 62 20 2a 70 20 3d 20 Incrblob *p =
1f633 28 49 6e 63 72 62 6c 6f 62 20 2a 29 70 42 6c 6f (Incrblob *)pBlo
1f634 62 3b 0a 20 20 72 65 74 75 72 6e 20 70 20 3f 20 b;. return p ?
1f635 70 2d 3e 6e 42 79 74 65 20 3a 20 30 3b 0a 7d 0a p->nByte : 0;.}.
1f636 0a 23 65 6e 64 69 66 20 2f 2a 20 23 69 66 6e 64 .#endif /* #ifnd
1f637 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 ef SQLITE_OMIT_I
1f638 4e 43 52 42 4c 4f 42 20 2a 2f 0a 0a 2f 2a 2a 2a NCRBLOB */../***
1f639 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
1f63a 6f 66 20 76 64 62 65 62 6c 6f 62 2e 63 20 2a 2a of vdbeblob.c **
1f63b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f63c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f63d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
1f63e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
1f63f 6e 20 66 69 6c 65 20 6a 6f 75 72 6e 61 6c 2e 63 n file journal.c
1f640 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
1f641 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f642 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a **********/./*.*
1f643 2a 20 32 30 30 37 20 41 75 67 75 73 74 20 32 32 * 2007 August 22
1f644 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
1f645 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
1f646 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
1f647 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
1f648 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
1f649 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
1f64a 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
1f64b 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
1f64c 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
1f64d 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
1f64e 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
1f64f 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
1f650 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
1f651 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
1f652 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
1f653 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
1f654 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
1f655 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
1f656 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f657 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f658 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f659 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
1f65a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a 20 **.** @(#) $Id:
1f65b 6a 6f 75 72 6e 61 6c 2e 63 2c 76 20 31 2e 39 20 journal.c,v 1.9
1f65c 32 30 30 39 2f 30 31 2f 32 30 20 31 37 3a 30 36 2009/01/20 17:06
1f65d 3a 32 37 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 :27 danielk1977
1f65e 45 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 64 65 66 Exp $.*/..#ifdef
1f65f 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 SQLITE_ENABLE_A
1f660 54 4f 4d 49 43 5f 57 52 49 54 45 0a 0a 2f 2a 0a TOMIC_WRITE../*.
1f661 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 69 6d 70 ** This file imp
1f662 6c 65 6d 65 6e 74 73 20 61 20 73 70 65 63 69 61 lements a specia
1f663 6c 20 6b 69 6e 64 20 6f 66 20 73 71 6c 69 74 65 l kind of sqlite
1f664 33 5f 66 69 6c 65 20 6f 62 6a 65 63 74 20 75 73 3_file object us
1f665 65 64 0a 2a 2a 20 62 79 20 53 51 4c 69 74 65 20 ed.** by SQLite
1f666 74 6f 20 63 72 65 61 74 65 20 6a 6f 75 72 6e 61 to create journa
1f667 6c 20 66 69 6c 65 73 20 69 66 20 74 68 65 20 61 l files if the a
1f668 74 6f 6d 69 63 2d 77 72 69 74 65 20 6f 70 74 69 tomic-write opti
1f669 6d 69 7a 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 65 mization.** is e
1f66a 6e 61 62 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 nabled..**.** Th
1f66b 65 20 64 69 73 74 69 6e 63 74 69 76 65 20 63 68 e distinctive ch
1f66c 61 72 61 63 74 65 72 69 73 74 69 63 20 6f 66 20 aracteristic of
1f66d 74 68 69 73 20 73 71 6c 69 74 65 33 5f 66 69 6c this sqlite3_fil
1f66e 65 20 69 73 20 74 68 61 74 20 74 68 65 0a 2a 2a e is that the.**
1f66f 20 61 63 74 75 61 6c 20 6f 6e 20 64 69 73 6b 20 actual on disk
1f670 66 69 6c 65 20 69 73 20 63 72 65 61 74 65 64 20 file is created
1f671 6c 61 7a 69 6c 79 2e 20 57 68 65 6e 20 74 68 65 lazily. When the
1f672 20 66 69 6c 65 20 69 73 20 63 72 65 61 74 65 64 file is created
1f673 2c 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 65 72 20 ,.** the caller
1f674 73 70 65 63 69 66 69 65 73 20 61 20 62 75 66 66 specifies a buff
1f675 65 72 20 73 69 7a 65 20 66 6f 72 20 61 6e 20 69 er size for an i
1f676 6e 2d 6d 65 6d 6f 72 79 20 62 75 66 66 65 72 20 n-memory buffer
1f677 74 6f 0a 2a 2a 20 62 65 20 75 73 65 64 20 74 6f to.** be used to
1f678 20 73 65 72 76 69 63 65 20 72 65 61 64 28 29 20 service read()
1f679 61 6e 64 20 77 72 69 74 65 28 29 20 72 65 71 75 and write() requ
1f67a 65 73 74 73 2e 20 54 68 65 20 61 63 74 75 61 6c ests. The actual
1f67b 20 66 69 6c 65 0a 2a 2a 20 6f 6e 20 64 69 73 6b file.** on disk
1f67c 20 69 73 20 6e 6f 74 20 63 72 65 61 74 65 64 20 is not created
1f67d 6f 72 20 70 6f 70 75 6c 61 74 65 64 20 75 6e 74 or populated unt
1f67e 69 6c 20 65 69 74 68 65 72 3a 0a 2a 2a 0a 2a 2a il either:.**.**
1f67f 20 20 20 31 29 20 54 68 65 20 69 6e 2d 6d 65 6d 1) The in-mem
1f680 6f 72 79 20 72 65 70 72 65 73 65 6e 74 61 74 69 ory representati
1f681 6f 6e 20 67 72 6f 77 73 20 74 6f 6f 20 6c 61 72 on grows too lar
1f682 67 65 20 66 6f 72 20 74 68 65 20 61 6c 6c 6f 63 ge for the alloc
1f683 61 74 65 64 20 0a 2a 2a 20 20 20 20 20 20 62 75 ated .** bu
1f684 66 66 65 72 2c 20 6f 72 0a 2a 2a 20 20 20 32 29 ffer, or.** 2)
1f685 20 54 68 65 20 73 71 6c 69 74 65 33 4a 6f 75 72 The sqlite3Jour
1f686 6e 61 6c 43 72 65 61 74 65 28 29 20 66 75 6e 63 nalCreate() func
1f687 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2e 0a tion is called..
1f688 2a 2f 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 41 20 4a 6f */..../*.** A Jo
1f689 75 72 6e 61 6c 46 69 6c 65 20 6f 62 6a 65 63 74 urnalFile object
1f68a 20 69 73 20 61 20 73 75 62 63 6c 61 73 73 20 6f is a subclass o
1f68b 66 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 75 f sqlite3_file u
1f68c 73 65 64 20 62 79 0a 2a 2a 20 61 73 20 61 6e 20 sed by.** as an
1f68d 6f 70 65 6e 20 66 69 6c 65 20 68 61 6e 64 6c 65 open file handle
1f68e 20 66 6f 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c for journal fil
1f68f 65 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4a 6f es..*/.struct Jo
1f690 75 72 6e 61 6c 46 69 6c 65 20 7b 0a 20 20 73 71 urnalFile {. sq
1f691 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 lite3_io_methods
1f692 20 2a 70 4d 65 74 68 6f 64 3b 20 20 20 20 2f 2a *pMethod; /*
1f693 20 49 2f 4f 20 6d 65 74 68 6f 64 73 20 6f 6e 20 I/O methods on
1f694 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 20 2a 2f journal files */
1f695 0a 20 20 69 6e 74 20 6e 42 75 66 3b 20 20 20 20 . int nBuf;
1f696 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f697 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 7a 42 /* Size of zB
1f698 75 66 5b 5d 20 69 6e 20 62 79 74 65 73 20 2a 2f uf[] in bytes */
1f699 0a 20 20 63 68 61 72 20 2a 7a 42 75 66 3b 20 20 . char *zBuf;
1f69a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f69b 20 20 20 2f 2a 20 53 70 61 63 65 20 74 6f 20 62 /* Space to b
1f69c 75 66 66 65 72 20 6a 6f 75 72 6e 61 6c 20 77 72 uffer journal wr
1f69d 69 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20 69 53 ites */. int iS
1f69e 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 ize;
1f69f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6d 6f /* Amo
1f6a0 75 6e 74 20 6f 66 20 7a 42 75 66 5b 5d 20 63 75 unt of zBuf[] cu
1f6a1 72 72 65 6e 74 6c 79 20 75 73 65 64 20 2a 2f 0a rrently used */.
1f6a2 20 20 69 6e 74 20 66 6c 61 67 73 3b 20 20 20 20 int flags;
1f6a3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f6a4 20 20 2f 2a 20 78 4f 70 65 6e 20 66 6c 61 67 73 /* xOpen flags
1f6a5 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 */. sqlite3_vf
1f6a6 73 20 2a 70 56 66 73 3b 20 20 20 20 20 20 20 20 s *pVfs;
1f6a7 20 20 20 20 20 20 2f 2a 20 54 68 65 20 22 72 65 /* The "re
1f6a8 61 6c 22 20 75 6e 64 65 72 6c 79 69 6e 67 20 56 al" underlying V
1f6a9 46 53 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f FS */. sqlite3_
1f6aa 66 69 6c 65 20 2a 70 52 65 61 6c 3b 20 20 20 20 file *pReal;
1f6ab 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 22 /* The "
1f6ac 72 65 61 6c 22 20 75 6e 64 65 72 6c 79 69 6e 67 real" underlying
1f6ad 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 file descriptor
1f6ae 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 */. const char
1f6af 20 2a 7a 4a 6f 75 72 6e 61 6c 3b 20 20 20 20 20 *zJournal;
1f6b0 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 /* Name of
1f6b1 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
1f6b2 65 20 2a 2f 0a 7d 3b 0a 74 79 70 65 64 65 66 20 e */.};.typedef
1f6b3 73 74 72 75 63 74 20 4a 6f 75 72 6e 61 6c 46 69 struct JournalFi
1f6b4 6c 65 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 3b 0a le JournalFile;.
1f6b5 0a 2f 2a 0a 2a 2a 20 49 66 20 69 74 20 64 6f 65 ./*.** If it doe
1f6b6 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 65 78 s not already ex
1f6b7 69 73 74 73 2c 20 63 72 65 61 74 65 20 61 6e 64 ists, create and
1f6b8 20 70 6f 70 75 6c 61 74 65 20 74 68 65 20 6f 6e populate the on
1f6b9 2d 64 69 73 6b 20 66 69 6c 65 20 0a 2a 2a 20 66 -disk file .** f
1f6ba 6f 72 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 70 or JournalFile p
1f6bb 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
1f6bc 63 72 65 61 74 65 46 69 6c 65 28 4a 6f 75 72 6e createFile(Journ
1f6bd 61 6c 46 69 6c 65 20 2a 70 29 7b 0a 20 20 69 6e alFile *p){. in
1f6be 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
1f6bf 3b 0a 20 20 69 66 28 20 21 70 2d 3e 70 52 65 61 ;. if( !p->pRea
1f6c0 6c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 l ){. sqlite3
1f6c1 5f 66 69 6c 65 20 2a 70 52 65 61 6c 20 3d 20 28 _file *pReal = (
1f6c2 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 26 sqlite3_file *)&
1f6c3 70 5b 31 5d 3b 0a 20 20 20 20 72 63 20 3d 20 73 p[1];. rc = s
1f6c4 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 2d 3e qlite3OsOpen(p->
1f6c5 70 56 66 73 2c 20 70 2d 3e 7a 4a 6f 75 72 6e 61 pVfs, p->zJourna
1f6c6 6c 2c 20 70 52 65 61 6c 2c 20 70 2d 3e 66 6c 61 l, pReal, p->fla
1f6c7 67 73 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 gs, 0);. if(
1f6c8 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
1f6c9 0a 20 20 20 20 20 20 70 2d 3e 70 52 65 61 6c 20 . p->pReal
1f6ca 3d 20 70 52 65 61 6c 3b 0a 20 20 20 20 20 20 69 = pReal;. i
1f6cb 66 28 20 70 2d 3e 69 53 69 7a 65 3e 30 20 29 7b f( p->iSize>0 ){
1f6cc 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
1f6cd 70 2d 3e 69 53 69 7a 65 3c 3d 70 2d 3e 6e 42 75 p->iSize<=p->nBu
1f6ce 66 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d f);. rc =
1f6cf 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 sqlite3OsWrite(
1f6d0 70 2d 3e 70 52 65 61 6c 2c 20 70 2d 3e 7a 42 75 p->pReal, p->zBu
1f6d1 66 2c 20 70 2d 3e 69 53 69 7a 65 2c 20 30 29 3b f, p->iSize, 0);
1f6d2 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
1f6d3 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
1f6d4 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 74 }../*.** Close t
1f6d5 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 he file..*/.stat
1f6d6 69 63 20 69 6e 74 20 6a 72 6e 6c 43 6c 6f 73 65 ic int jrnlClose
1f6d7 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 (sqlite3_file *p
1f6d8 4a 66 64 29 7b 0a 20 20 4a 6f 75 72 6e 61 6c 46 Jfd){. JournalF
1f6d9 69 6c 65 20 2a 70 20 3d 20 28 4a 6f 75 72 6e 61 ile *p = (Journa
1f6da 6c 46 69 6c 65 20 2a 29 70 4a 66 64 3b 0a 20 20 lFile *)pJfd;.
1f6db 69 66 28 20 70 2d 3e 70 52 65 61 6c 20 29 7b 0a if( p->pReal ){.
1f6dc 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f sqlite3OsClo
1f6dd 73 65 28 70 2d 3e 70 52 65 61 6c 29 3b 0a 20 20 se(p->pReal);.
1f6de 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 }. sqlite3_free
1f6df 28 70 2d 3e 7a 42 75 66 29 3b 0a 20 20 72 65 74 (p->zBuf);. ret
1f6e0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
1f6e1 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 64 61 74 ../*.** Read dat
1f6e2 61 20 66 72 6f 6d 20 74 68 65 20 66 69 6c 65 2e a from the file.
1f6e3 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6a .*/.static int j
1f6e4 72 6e 6c 52 65 61 64 28 0a 20 20 73 71 6c 69 74 rnlRead(. sqlit
1f6e5 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 20 e3_file *pJfd,
1f6e6 20 20 2f 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c /* The journal
1f6e7 20 66 69 6c 65 20 66 72 6f 6d 20 77 68 69 63 68 file from which
1f6e8 20 74 6f 20 72 65 61 64 20 2a 2f 0a 20 20 76 6f to read */. vo
1f6e9 69 64 20 2a 7a 42 75 66 2c 20 20 20 20 20 20 20 id *zBuf,
1f6ea 20 20 20 20 20 2f 2a 20 50 75 74 20 74 68 65 20 /* Put the
1f6eb 72 65 73 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a results here */.
1f6ec 20 20 69 6e 74 20 69 41 6d 74 2c 20 20 20 20 20 int iAmt,
1f6ed 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
1f6ee 65 72 20 6f 66 20 62 79 74 65 73 20 74 6f 20 72 er of bytes to r
1f6ef 65 61 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f ead */. sqlite_
1f6f0 69 6e 74 36 34 20 69 4f 66 73 74 20 20 20 20 20 int64 iOfst
1f6f1 2f 2a 20 42 65 67 69 6e 20 72 65 61 64 69 6e 67 /* Begin reading
1f6f2 20 61 74 20 74 68 69 73 20 6f 66 66 73 65 74 20 at this offset
1f6f3 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d */.){. int rc =
1f6f4 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4a 6f SQLITE_OK;. Jo
1f6f5 75 72 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 urnalFile *p = (
1f6f6 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a JournalFile *)pJ
1f6f7 66 64 3b 0a 20 20 69 66 28 20 70 2d 3e 70 52 65 fd;. if( p->pRe
1f6f8 61 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 al ){. rc = s
1f6f9 71 6c 69 74 65 33 4f 73 52 65 61 64 28 70 2d 3e qlite3OsRead(p->
1f6fa 70 52 65 61 6c 2c 20 7a 42 75 66 2c 20 69 41 6d pReal, zBuf, iAm
1f6fb 74 2c 20 69 4f 66 73 74 29 3b 0a 20 20 7d 65 6c t, iOfst);. }el
1f6fc 73 65 20 69 66 28 20 28 69 41 6d 74 2b 69 4f 66 se if( (iAmt+iOf
1f6fd 73 74 29 3e 70 2d 3e 69 53 69 7a 65 20 29 7b 0a st)>p->iSize ){.
1f6fe 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
1f6ff 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 IOERR_SHORT_READ
1f700 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6d ;. }else{. m
1f701 65 6d 63 70 79 28 7a 42 75 66 2c 20 26 70 2d 3e emcpy(zBuf, &p->
1f702 7a 42 75 66 5b 69 4f 66 73 74 5d 2c 20 69 41 6d zBuf[iOfst], iAm
1f703 74 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e t);. }. return
1f704 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 rc;.}../*.** Wr
1f705 69 74 65 20 64 61 74 61 20 74 6f 20 74 68 65 20 ite data to the
1f706 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 file..*/.static
1f707 69 6e 74 20 6a 72 6e 6c 57 72 69 74 65 28 0a 20 int jrnlWrite(.
1f708 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 sqlite3_file *p
1f709 4a 66 64 2c 20 20 20 20 2f 2a 20 54 68 65 20 6a Jfd, /* The j
1f70a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 6e 74 6f ournal file into
1f70b 20 77 68 69 63 68 20 74 6f 20 77 72 69 74 65 20 which to write
1f70c 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 */. const void
1f70d 2a 7a 42 75 66 2c 20 20 20 20 20 20 2f 2a 20 54 *zBuf, /* T
1f70e 61 6b 65 20 64 61 74 61 20 74 6f 20 62 65 20 77 ake data to be w
1f70f 72 69 74 74 65 6e 20 66 72 6f 6d 20 68 65 72 65 ritten from here
1f710 20 2a 2f 0a 20 20 69 6e 74 20 69 41 6d 74 2c 20 */. int iAmt,
1f711 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1f712 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 Number of bytes
1f713 74 6f 20 77 72 69 74 65 20 2a 2f 0a 20 20 73 71 to write */. sq
1f714 6c 69 74 65 5f 69 6e 74 36 34 20 69 4f 66 73 74 lite_int64 iOfst
1f715 20 20 20 20 20 2f 2a 20 42 65 67 69 6e 20 77 72 /* Begin wr
1f716 69 74 69 6e 67 20 61 74 20 74 68 69 73 20 6f 66 iting at this of
1f717 66 73 65 74 20 69 6e 74 6f 20 74 68 65 20 66 69 fset into the fi
1f718 6c 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 le */.){. int r
1f719 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
1f71a 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 70 20 JournalFile *p
1f71b 3d 20 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a = (JournalFile *
1f71c 29 70 4a 66 64 3b 0a 20 20 69 66 28 20 21 70 2d )pJfd;. if( !p-
1f71d 3e 70 52 65 61 6c 20 26 26 20 28 69 4f 66 73 74 >pReal && (iOfst
1f71e 2b 69 41 6d 74 29 3e 70 2d 3e 6e 42 75 66 20 29 +iAmt)>p->nBuf )
1f71f 7b 0a 20 20 20 20 72 63 20 3d 20 63 72 65 61 74 {. rc = creat
1f720 65 46 69 6c 65 28 70 29 3b 0a 20 20 7d 0a 20 20 eFile(p);. }.
1f721 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
1f722 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e K ){. if( p->
1f723 70 52 65 61 6c 20 29 7b 0a 20 20 20 20 20 20 72 pReal ){. r
1f724 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 c = sqlite3OsWri
1f725 74 65 28 70 2d 3e 70 52 65 61 6c 2c 20 7a 42 75 te(p->pReal, zBu
1f726 66 2c 20 69 41 6d 74 2c 20 69 4f 66 73 74 29 3b f, iAmt, iOfst);
1f727 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
1f728 20 20 6d 65 6d 63 70 79 28 26 70 2d 3e 7a 42 75 memcpy(&p->zBu
1f729 66 5b 69 4f 66 73 74 5d 2c 20 7a 42 75 66 2c 20 f[iOfst], zBuf,
1f72a 69 41 6d 74 29 3b 0a 20 20 20 20 20 20 69 66 28 iAmt);. if(
1f72b 20 70 2d 3e 69 53 69 7a 65 3c 28 69 4f 66 73 74 p->iSize<(iOfst
1f72c 2b 69 41 6d 74 29 20 29 7b 0a 20 20 20 20 20 20 +iAmt) ){.
1f72d 20 20 70 2d 3e 69 53 69 7a 65 20 3d 20 28 69 4f p->iSize = (iO
1f72e 66 73 74 2b 69 41 6d 74 29 3b 0a 20 20 20 20 20 fst+iAmt);.
1f72f 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 }. }. }. r
1f730 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
1f731 2a 2a 20 54 72 75 6e 63 61 74 65 20 74 68 65 20 ** Truncate the
1f732 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 file..*/.static
1f733 69 6e 74 20 6a 72 6e 6c 54 72 75 6e 63 61 74 65 int jrnlTruncate
1f734 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 (sqlite3_file *p
1f735 4a 66 64 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 Jfd, sqlite_int6
1f736 34 20 73 69 7a 65 29 7b 0a 20 20 69 6e 74 20 72 4 size){. int r
1f737 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
1f738 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 70 20 JournalFile *p
1f739 3d 20 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a = (JournalFile *
1f73a 29 70 4a 66 64 3b 0a 20 20 69 66 28 20 70 2d 3e )pJfd;. if( p->
1f73b 70 52 65 61 6c 20 29 7b 0a 20 20 20 20 72 63 20 pReal ){. rc
1f73c 3d 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 = sqlite3OsTrunc
1f73d 61 74 65 28 70 2d 3e 70 52 65 61 6c 2c 20 73 69 ate(p->pReal, si
1f73e 7a 65 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 ze);. }else if(
1f73f 20 73 69 7a 65 3c 70 2d 3e 69 53 69 7a 65 20 29 size<p->iSize )
1f740 7b 0a 20 20 20 20 70 2d 3e 69 53 69 7a 65 20 3d {. p->iSize =
1f741 20 73 69 7a 65 3b 0a 20 20 7d 0a 20 20 72 65 74 size;. }. ret
1f742 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
1f743 20 53 79 6e 63 20 74 68 65 20 66 69 6c 65 2e 0a Sync the file..
1f744 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6a 72 */.static int jr
1f745 6e 6c 53 79 6e 63 28 73 71 6c 69 74 65 33 5f 66 nlSync(sqlite3_f
1f746 69 6c 65 20 2a 70 4a 66 64 2c 20 69 6e 74 20 66 ile *pJfd, int f
1f747 6c 61 67 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b lags){. int rc;
1f748 0a 20 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a . JournalFile *
1f749 70 20 3d 20 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 p = (JournalFile
1f74a 20 2a 29 70 4a 66 64 3b 0a 20 20 69 66 28 20 70 *)pJfd;. if( p
1f74b 2d 3e 70 52 65 61 6c 20 29 7b 0a 20 20 20 20 72 ->pReal ){. r
1f74c 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 79 6e c = sqlite3OsSyn
1f74d 63 28 70 2d 3e 70 52 65 61 6c 2c 20 66 6c 61 67 c(p->pReal, flag
1f74e 73 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 s);. }else{.
1f74f 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
1f750 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
1f751 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72 79 ;.}../*.** Query
1f752 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 the size of the
1f753 20 66 69 6c 65 20 69 6e 20 62 79 74 65 73 2e 0a file in bytes..
1f754 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6a 72 */.static int jr
1f755 6e 6c 46 69 6c 65 53 69 7a 65 28 73 71 6c 69 74 nlFileSize(sqlit
1f756 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 73 e3_file *pJfd, s
1f757 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70 53 69 qlite_int64 *pSi
1f758 7a 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 ze){. int rc =
1f759 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4a 6f 75 SQLITE_OK;. Jou
1f75a 72 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a rnalFile *p = (J
1f75b 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66 ournalFile *)pJf
1f75c 64 3b 0a 20 20 69 66 28 20 70 2d 3e 70 52 65 61 d;. if( p->pRea
1f75d 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 l ){. rc = sq
1f75e 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 lite3OsFileSize(
1f75f 70 2d 3e 70 52 65 61 6c 2c 20 70 53 69 7a 65 29 p->pReal, pSize)
1f760 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2a ;. }else{. *
1f761 70 53 69 7a 65 20 3d 20 28 73 71 6c 69 74 65 5f pSize = (sqlite_
1f762 69 6e 74 36 34 29 20 70 2d 3e 69 53 69 7a 65 3b int64) p->iSize;
1f763 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
1f764 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 61 62 6c 65 ;.}../*.** Table
1f765 20 6f 66 20 6d 65 74 68 6f 64 73 20 66 6f 72 20 of methods for
1f766 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 73 71 6c 69 JournalFile sqli
1f767 74 65 33 5f 66 69 6c 65 20 6f 62 6a 65 63 74 2e te3_file object.
1f768 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75 63 .*/.static struc
1f769 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 t sqlite3_io_met
1f76a 68 6f 64 73 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 hods JournalFile
1f76b 4d 65 74 68 6f 64 73 20 3d 20 7b 0a 20 20 31 2c Methods = {. 1,
1f76c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1f76d 69 56 65 72 73 69 6f 6e 20 2a 2f 0a 20 20 6a 72 iVersion */. jr
1f76e 6e 6c 43 6c 6f 73 65 2c 20 20 20 20 20 2f 2a 20 nlClose, /*
1f76f 78 43 6c 6f 73 65 20 2a 2f 0a 20 20 6a 72 6e 6c xClose */. jrnl
1f770 52 65 61 64 2c 20 20 20 20 20 20 2f 2a 20 78 52 Read, /* xR
1f771 65 61 64 20 2a 2f 0a 20 20 6a 72 6e 6c 57 72 69 ead */. jrnlWri
1f772 74 65 2c 20 20 20 20 20 2f 2a 20 78 57 72 69 74 te, /* xWrit
1f773 65 20 2a 2f 0a 20 20 6a 72 6e 6c 54 72 75 6e 63 e */. jrnlTrunc
1f774 61 74 65 2c 20 20 2f 2a 20 78 54 72 75 6e 63 61 ate, /* xTrunca
1f775 74 65 20 2a 2f 0a 20 20 6a 72 6e 6c 53 79 6e 63 te */. jrnlSync
1f776 2c 20 20 20 20 20 20 2f 2a 20 78 53 79 6e 63 20 , /* xSync
1f777 2a 2f 0a 20 20 6a 72 6e 6c 46 69 6c 65 53 69 7a */. jrnlFileSiz
1f778 65 2c 20 20 2f 2a 20 78 46 69 6c 65 53 69 7a 65 e, /* xFileSize
1f779 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 20 */. 0,
1f77a 20 20 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 2a 2f /* xLock */
1f77b 0a 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 . 0,
1f77c 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 2a 2f 0a /* xUnlock */.
1f77d 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 0,
1f77e 20 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76 /* xCheckReserv
1f77f 65 64 4c 6f 63 6b 20 2a 2f 0a 20 20 30 2c 20 20 edLock */. 0,
1f780 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 46 /* xF
1f781 69 6c 65 43 6f 6e 74 72 6f 6c 20 2a 2f 0a 20 20 ileControl */.
1f782 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 0, /
1f783 2a 20 78 53 65 63 74 6f 72 53 69 7a 65 20 2a 2f * xSectorSize */
1f784 0a 20 20 30 20 20 20 20 20 20 20 20 20 20 20 20 . 0
1f785 20 20 2f 2a 20 78 44 65 76 69 63 65 43 68 61 72 /* xDeviceChar
1f786 61 63 74 65 72 69 73 74 69 63 73 20 2a 2f 0a 7d acteristics */.}
1f787 3b 0a 0a 2f 2a 20 0a 2a 2a 20 4f 70 65 6e 20 61 ;../* .** Open a
1f788 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 2a journal file..*
1f789 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1f78a 20 69 6e 74 20 73 71 6c 69 74 65 33 4a 6f 75 72 int sqlite3Jour
1f78b 6e 61 6c 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 nalOpen(. sqlit
1f78c 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 e3_vfs *pVfs,
1f78d 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 46 53 /* The VFS
1f78e 20 74 6f 20 75 73 65 20 66 6f 72 20 61 63 74 75 to use for actu
1f78f 61 6c 20 66 69 6c 65 20 49 2f 4f 20 2a 2f 0a 20 al file I/O */.
1f790 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 const char *zNa
1f791 6d 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4e me, /* N
1f792 61 6d 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e ame of the journ
1f793 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 20 73 71 6c al file */. sql
1f794 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c ite3_file *pJfd,
1f795 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 61 6c /* Preal
1f796 6c 6f 63 61 74 65 64 2c 20 62 6c 61 6e 6b 20 66 located, blank f
1f797 69 6c 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 ile handle */.
1f798 69 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 int flags,
1f799 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 /* Op
1f79a 65 6e 69 6e 67 20 66 6c 61 67 73 20 2a 2f 0a 20 ening flags */.
1f79b 20 69 6e 74 20 6e 42 75 66 20 20 20 20 20 20 20 int nBuf
1f79c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 /* B
1f79d 79 74 65 73 20 62 75 66 66 65 72 65 64 20 62 65 ytes buffered be
1f79e 66 6f 72 65 20 6f 70 65 6e 69 6e 67 20 74 68 65 fore opening the
1f79f 20 66 69 6c 65 20 2a 2f 0a 29 7b 0a 20 20 4a 6f file */.){. Jo
1f7a0 75 72 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 urnalFile *p = (
1f7a1 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a JournalFile *)pJ
1f7a2 66 64 3b 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20 fd;. memset(p,
1f7a3 30 2c 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 0, sqlite3Journa
1f7a4 6c 53 69 7a 65 28 70 56 66 73 29 29 3b 0a 20 20 lSize(pVfs));.
1f7a5 69 66 28 20 6e 42 75 66 3e 30 20 29 7b 0a 20 20 if( nBuf>0 ){.
1f7a6 20 20 70 2d 3e 7a 42 75 66 20 3d 20 73 71 6c 69 p->zBuf = sqli
1f7a7 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 6e 42 te3MallocZero(nB
1f7a8 75 66 29 3b 0a 20 20 20 20 69 66 28 20 21 70 2d uf);. if( !p-
1f7a9 3e 7a 42 75 66 20 29 7b 0a 20 20 20 20 20 20 72 >zBuf ){. r
1f7aa 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d eturn SQLITE_NOM
1f7ab 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 EM;. }. }els
1f7ac 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 e{. return sq
1f7ad 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 56 66 73 lite3OsOpen(pVfs
1f7ae 2c 20 7a 4e 61 6d 65 2c 20 70 4a 66 64 2c 20 66 , zName, pJfd, f
1f7af 6c 61 67 73 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 lags, 0);. }.
1f7b0 70 2d 3e 70 4d 65 74 68 6f 64 20 3d 20 26 4a 6f p->pMethod = &Jo
1f7b1 75 72 6e 61 6c 46 69 6c 65 4d 65 74 68 6f 64 73 urnalFileMethods
1f7b2 3b 0a 20 20 70 2d 3e 6e 42 75 66 20 3d 20 6e 42 ;. p->nBuf = nB
1f7b3 75 66 3b 0a 20 20 70 2d 3e 66 6c 61 67 73 20 3d uf;. p->flags =
1f7b4 20 66 6c 61 67 73 3b 0a 20 20 70 2d 3e 7a 4a 6f flags;. p->zJo
1f7b5 75 72 6e 61 6c 20 3d 20 7a 4e 61 6d 65 3b 0a 20 urnal = zName;.
1f7b6 20 70 2d 3e 70 56 66 73 20 3d 20 70 56 66 73 3b p->pVfs = pVfs;
1f7b7 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
1f7b8 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 _OK;.}../*.** If
1f7b9 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 70 20 the argument p
1f7ba 70 6f 69 6e 74 73 20 74 6f 20 61 20 4a 6f 75 72 points to a Jour
1f7bb 6e 61 6c 46 69 6c 65 20 73 74 72 75 63 74 75 72 nalFile structur
1f7bc 65 2c 20 61 6e 64 20 74 68 65 20 75 6e 64 65 72 e, and the under
1f7bd 6c 79 69 6e 67 0a 2a 2a 20 66 69 6c 65 20 68 61 lying.** file ha
1f7be 73 20 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 63 s not yet been c
1f7bf 72 65 61 74 65 64 2c 20 63 72 65 61 74 65 20 69 reated, create i
1f7c0 74 20 6e 6f 77 2e 0a 2a 2f 0a 53 51 4c 49 54 45 t now..*/.SQLITE
1f7c1 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
1f7c2 69 74 65 33 4a 6f 75 72 6e 61 6c 43 72 65 61 74 ite3JournalCreat
1f7c3 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a e(sqlite3_file *
1f7c4 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e 70 4d 65 p){. if( p->pMe
1f7c5 74 68 6f 64 73 21 3d 26 4a 6f 75 72 6e 61 6c 46 thods!=&JournalF
1f7c6 69 6c 65 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 ileMethods ){.
1f7c7 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
1f7c8 4f 4b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e OK;. }. return
1f7c9 20 63 72 65 61 74 65 46 69 6c 65 28 28 4a 6f 75 createFile((Jou
1f7ca 72 6e 61 6c 46 69 6c 65 20 2a 29 70 29 3b 0a 7d rnalFile *)p);.}
1f7cb 0a 0a 2f 2a 20 0a 2a 2a 20 52 65 74 75 72 6e 20 ../* .** Return
1f7cc 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 the number of by
1f7cd 74 65 73 20 72 65 71 75 69 72 65 64 20 74 6f 20 tes required to
1f7ce 73 74 6f 72 65 20 61 20 4a 6f 75 72 6e 61 6c 46 store a JournalF
1f7cf 69 6c 65 20 74 68 61 74 20 75 73 65 73 20 76 66 ile that uses vf
1f7d0 73 0a 2a 2a 20 70 56 66 73 20 74 6f 20 63 72 65 s.** pVfs to cre
1f7d1 61 74 65 20 74 68 65 20 75 6e 64 65 72 6c 79 69 ate the underlyi
1f7d2 6e 67 20 6f 6e 2d 64 69 73 6b 20 66 69 6c 65 73 ng on-disk files
1f7d3 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1f7d4 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4a ATE int sqlite3J
1f7d5 6f 75 72 6e 61 6c 53 69 7a 65 28 73 71 6c 69 74 ournalSize(sqlit
1f7d6 65 33 5f 76 66 73 20 2a 70 56 66 73 29 7b 0a 20 e3_vfs *pVfs){.
1f7d7 20 72 65 74 75 72 6e 20 28 70 56 66 73 2d 3e 73 return (pVfs->s
1f7d8 7a 4f 73 46 69 6c 65 2b 73 69 7a 65 6f 66 28 4a zOsFile+sizeof(J
1f7d9 6f 75 72 6e 61 6c 46 69 6c 65 29 29 3b 0a 7d 0a ournalFile));.}.
1f7da 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a #endif../*******
1f7db 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6a ******* End of j
1f7dc 6f 75 72 6e 61 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a ournal.c *******
1f7dd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f7de 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f7df 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
1f7e0 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 ******* Begin fi
1f7e1 6c 65 20 6d 65 6d 6a 6f 75 72 6e 61 6c 2e 63 20 le memjournal.c
1f7e2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f7e3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f7e4 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 ******/./*.** 20
1f7e5 30 38 20 4f 63 74 6f 62 65 72 20 37 0a 2a 2a 0a 08 October 7.**.
1f7e6 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 ** The author di
1f7e7 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 sclaims copyrigh
1f7e8 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 t to this source
1f7e9 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 code. In place
1f7ea 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e of.** a legal n
1f7eb 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 otice, here is a
1f7ec 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a blessing:.**.**
1f7ed 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 May you do g
1f7ee 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c ood and not evil
1f7ef 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
1f7f0 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 find forgiveness
1f7f1 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e for yourself an
1f7f2 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 d forgive others
1f7f3 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
1f7f4 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 share freely, ne
1f7f5 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 ver taking more
1f7f6 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a than you give..*
1f7f7 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *.**************
1f7f8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f7f9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f7fa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f7fb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a ***********.**.*
1f7fc 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 * This file cont
1f7fd 61 69 6e 73 20 63 6f 64 65 20 75 73 65 20 74 6f ains code use to
1f7fe 20 69 6d 70 6c 65 6d 65 6e 74 20 61 6e 20 69 6e implement an in
1f7ff 2d 6d 65 6d 6f 72 79 20 72 6f 6c 6c 62 61 63 6b -memory rollback
1f800 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 20 54 68 65 journal..** The
1f801 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 6f 6c 6c 62 in-memory rollb
1f802 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 69 73 20 75 ack journal is u
1f803 73 65 64 20 74 6f 20 6a 6f 75 72 6e 61 6c 20 74 sed to journal t
1f804 72 61 6e 73 61 63 74 69 6f 6e 73 20 66 6f 72 0a ransactions for.
1f805 2a 2a 20 22 3a 6d 65 6d 6f 72 79 3a 22 20 64 61 ** ":memory:" da
1f806 74 61 62 61 73 65 73 20 61 6e 64 20 77 68 65 6e tabases and when
1f807 20 74 68 65 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 the journal_mod
1f808 65 3d 4d 45 4d 4f 52 59 20 70 72 61 67 6d 61 20 e=MEMORY pragma
1f809 69 73 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 40 is used..**.** @
1f80a 28 23 29 20 24 49 64 3a 20 6d 65 6d 6a 6f 75 72 (#) $Id: memjour
1f80b 6e 61 6c 2e 63 2c 76 20 31 2e 31 32 20 32 30 30 nal.c,v 1.12 200
1f80c 39 2f 30 35 2f 30 34 20 31 31 3a 34 32 3a 33 30 9/05/04 11:42:30
1f80d 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 danielk1977 Exp
1f80e 20 24 0a 2a 2f 0a 0a 2f 2a 20 46 6f 72 77 61 72 $.*/../* Forwar
1f80f 64 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 d references to
1f810 69 6e 74 65 72 6e 61 6c 20 73 74 72 75 63 74 75 internal structu
1f811 72 65 73 20 2a 2f 0a 74 79 70 65 64 65 66 20 73 res */.typedef s
1f812 74 72 75 63 74 20 4d 65 6d 4a 6f 75 72 6e 61 6c truct MemJournal
1f813 20 4d 65 6d 4a 6f 75 72 6e 61 6c 3b 0a 74 79 70 MemJournal;.typ
1f814 65 64 65 66 20 73 74 72 75 63 74 20 46 69 6c 65 edef struct File
1f815 50 6f 69 6e 74 20 46 69 6c 65 50 6f 69 6e 74 3b Point FilePoint;
1f816 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
1f817 46 69 6c 65 43 68 75 6e 6b 20 46 69 6c 65 43 68 FileChunk FileCh
1f818 75 6e 6b 3b 0a 0a 2f 2a 20 53 70 61 63 65 20 74 unk;../* Space t
1f819 6f 20 68 6f 6c 64 20 74 68 65 20 72 6f 6c 6c 62 o hold the rollb
1f81a 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 69 73 20 61 ack journal is a
1f81b 6c 6c 6f 63 61 74 65 64 20 69 6e 20 69 6e 63 72 llocated in incr
1f81c 65 6d 65 6e 74 73 20 6f 66 0a 2a 2a 20 74 68 69 ements of.** thi
1f81d 73 20 6d 61 6e 79 20 62 79 74 65 73 2e 0a 2a 2a s many bytes..**
1f81e 0a 2a 2a 20 54 68 65 20 73 69 7a 65 20 63 68 6f .** The size cho
1f81f 73 65 6e 20 69 73 20 61 20 6c 69 74 74 6c 65 20 sen is a little
1f820 6c 65 73 73 20 74 68 61 6e 20 61 20 70 6f 77 65 less than a powe
1f821 72 20 6f 66 20 74 77 6f 2e 20 20 54 68 61 74 20 r of two. That
1f822 77 61 79 2c 0a 2a 2a 20 74 68 65 20 46 69 6c 65 way,.** the File
1f823 43 68 75 6e 6b 20 6f 62 6a 65 63 74 20 77 69 6c Chunk object wil
1f824 6c 20 68 61 76 65 20 61 20 73 69 7a 65 20 74 68 l have a size th
1f825 61 74 20 61 6c 6d 6f 73 74 20 65 78 61 63 74 6c at almost exactl
1f826 79 20 66 69 6c 6c 73 0a 2a 2a 20 61 20 70 6f 77 y fills.** a pow
1f827 65 72 2d 6f 66 2d 74 77 6f 20 61 6c 6c 6f 63 61 er-of-two alloca
1f828 74 69 6f 6e 2e 20 20 54 68 69 73 20 6d 69 6d 69 tion. This mimi
1f829 6d 69 7a 65 73 20 77 61 73 74 65 64 20 73 70 61 mizes wasted spa
1f82a 63 65 20 69 6e 20 70 6f 77 65 72 2d 6f 66 2d 74 ce in power-of-t
1f82b 77 6f 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c wo.** memory all
1f82c 6f 63 61 74 6f 72 73 2e 0a 2a 2f 0a 23 64 65 66 ocators..*/.#def
1f82d 69 6e 65 20 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e ine JOURNAL_CHUN
1f82e 4b 53 49 5a 45 20 28 28 69 6e 74 29 28 31 30 32 KSIZE ((int)(102
1f82f 34 2d 73 69 7a 65 6f 66 28 46 69 6c 65 43 68 75 4-sizeof(FileChu
1f830 6e 6b 2a 29 29 29 0a 0a 2f 2a 20 4d 61 63 72 6f nk*)))../* Macro
1f831 20 74 6f 20 66 69 6e 64 20 74 68 65 20 6d 69 6e to find the min
1f832 69 6d 75 6d 20 6f 66 20 74 77 6f 20 6e 75 6d 65 imum of two nume
1f833 72 69 63 20 76 61 6c 75 65 73 2e 0a 2a 2f 0a 23 ric values..*/.#
1f834 69 66 6e 64 65 66 20 4d 49 4e 0a 23 20 64 65 66 ifndef MIN.# def
1f835 69 6e 65 20 4d 49 4e 28 78 2c 79 29 20 28 28 78 ine MIN(x,y) ((x
1f836 29 3c 28 79 29 3f 28 78 29 3a 28 79 29 29 0a 23 )<(y)?(x):(y)).#
1f837 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 endif../*.** The
1f838 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 rollback journa
1f839 6c 20 69 73 20 63 6f 6d 70 6f 73 65 64 20 6f 66 l is composed of
1f83a 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f a linked list o
1f83b 66 20 74 68 65 73 65 20 73 74 72 75 63 74 75 72 f these structur
1f83c 65 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 46 69 es..*/.struct Fi
1f83d 6c 65 43 68 75 6e 6b 20 7b 0a 20 20 46 69 6c 65 leChunk {. File
1f83e 43 68 75 6e 6b 20 2a 70 4e 65 78 74 3b 20 20 20 Chunk *pNext;
1f83f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
1f840 65 78 74 20 63 68 75 6e 6b 20 69 6e 20 74 68 65 ext chunk in the
1f841 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 75 38 journal */. u8
1f842 20 7a 43 68 75 6e 6b 5b 4a 4f 55 52 4e 41 4c 5f zChunk[JOURNAL_
1f843 43 48 55 4e 4b 53 49 5a 45 5d 3b 20 20 20 2f 2a CHUNKSIZE]; /*
1f844 20 43 6f 6e 74 65 6e 74 20 6f 66 20 74 68 69 73 Content of this
1f845 20 63 68 75 6e 6b 20 2a 2f 0a 7d 3b 0a 0a 2f 2a chunk */.};../*
1f846 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 .** An instance
1f847 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74 20 73 of this object s
1f848 65 72 76 65 73 20 61 73 20 61 20 63 75 72 73 6f erves as a curso
1f849 72 20 69 6e 74 6f 20 74 68 65 20 72 6f 6c 6c 62 r into the rollb
1f84a 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 20 ack journal..**
1f84b 54 68 65 20 63 75 72 73 6f 72 20 63 61 6e 20 62 The cursor can b
1f84c 65 20 65 69 74 68 65 72 20 66 6f 72 20 72 65 61 e either for rea
1f84d 64 69 6e 67 20 6f 72 20 77 72 69 74 69 6e 67 2e ding or writing.
1f84e 0a 2a 2f 0a 73 74 72 75 63 74 20 46 69 6c 65 50 .*/.struct FileP
1f84f 6f 69 6e 74 20 7b 0a 20 20 73 71 6c 69 74 65 33 oint {. sqlite3
1f850 5f 69 6e 74 36 34 20 69 4f 66 66 73 65 74 3b 20 _int64 iOffset;
1f851 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 /* Offs
1f852 65 74 20 66 72 6f 6d 20 74 68 65 20 62 65 67 69 et from the begi
1f853 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 66 69 6c nning of the fil
1f854 65 20 2a 2f 0a 20 20 46 69 6c 65 43 68 75 6e 6b e */. FileChunk
1f855 20 2a 70 43 68 75 6e 6b 3b 20 20 20 20 20 20 20 *pChunk;
1f856 20 20 20 20 20 20 20 2f 2a 20 53 70 65 63 69 66 /* Specif
1f857 69 63 20 63 68 75 6e 6b 20 69 6e 74 6f 20 77 68 ic chunk into wh
1f858 69 63 68 20 63 75 72 73 6f 72 20 70 6f 69 6e 74 ich cursor point
1f859 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 s */.};../*.** T
1f85a 68 69 73 20 73 75 62 63 6c 61 73 73 20 69 73 20 his subclass is
1f85b 61 20 73 75 62 63 6c 61 73 73 20 6f 66 20 73 71 a subclass of sq
1f85c 6c 69 74 65 33 5f 66 69 6c 65 2e 20 20 45 61 63 lite3_file. Eac
1f85d 68 20 6f 70 65 6e 20 6d 65 6d 6f 72 79 2d 6a 6f h open memory-jo
1f85e 75 72 6e 61 6c 0a 2a 2a 20 69 73 20 61 6e 20 69 urnal.** is an i
1f85f 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 nstance of this
1f860 63 6c 61 73 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 class..*/.struct
1f861 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20 7b 0a 20 20 MemJournal {.
1f862 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f sqlite3_io_metho
1f863 64 73 20 2a 70 4d 65 74 68 6f 64 3b 20 20 20 20 ds *pMethod;
1f864 2f 2a 20 50 61 72 65 6e 74 20 63 6c 61 73 73 2e /* Parent class.
1f865 20 4d 55 53 54 20 42 45 20 46 49 52 53 54 20 2a MUST BE FIRST *
1f866 2f 0a 20 20 46 69 6c 65 43 68 75 6e 6b 20 2a 70 /. FileChunk *p
1f867 46 69 72 73 74 3b 20 20 20 20 20 20 20 20 20 20 First;
1f868 20 20 20 20 2f 2a 20 48 65 61 64 20 6f 66 20 69 /* Head of i
1f869 6e 2d 6d 65 6d 6f 72 79 20 63 68 75 6e 6b 2d 6c n-memory chunk-l
1f86a 69 73 74 20 2a 2f 0a 20 20 46 69 6c 65 50 6f 69 ist */. FilePoi
1f86b 6e 74 20 65 6e 64 70 6f 69 6e 74 3b 20 20 20 20 nt endpoint;
1f86c 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e /* Poin
1f86d 74 65 72 20 74 6f 20 74 68 65 20 65 6e 64 20 6f ter to the end o
1f86e 66 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20 f the file */.
1f86f 46 69 6c 65 50 6f 69 6e 74 20 72 65 61 64 70 6f FilePoint readpo
1f870 69 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 int;
1f871 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 /* Pointer to th
1f872 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6c 61 73 e end of the las
1f873 74 20 78 52 65 61 64 28 29 20 2a 2f 0a 7d 3b 0a t xRead() */.};.
1f874 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 64 61 74 61 ./*.** Read data
1f875 20 66 72 6f 6d 20 74 68 65 20 69 6e 2d 6d 65 6d from the in-mem
1f876 6f 72 79 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ory journal file
1f877 2e 20 20 54 68 69 73 20 69 73 20 74 68 65 20 69 . This is the i
1f878 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a mplementation.**
1f879 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f of the sqlite3_
1f87a 76 66 73 2e 78 52 65 61 64 20 6d 65 74 68 6f 64 vfs.xRead method
1f87b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
1f87c 6d 65 6d 6a 72 6e 6c 52 65 61 64 28 0a 20 20 73 memjrnlRead(. s
1f87d 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 qlite3_file *pJf
1f87e 64 2c 20 20 20 20 2f 2a 20 54 68 65 20 6a 6f 75 d, /* The jou
1f87f 72 6e 61 6c 20 66 69 6c 65 20 66 72 6f 6d 20 77 rnal file from w
1f880 68 69 63 68 20 74 6f 20 72 65 61 64 20 2a 2f 0a hich to read */.
1f881 20 20 76 6f 69 64 20 2a 7a 42 75 66 2c 20 20 20 void *zBuf,
1f882 20 20 20 20 20 20 20 20 20 2f 2a 20 50 75 74 20 /* Put
1f883 74 68 65 20 72 65 73 75 6c 74 73 20 68 65 72 65 the results here
1f884 20 2a 2f 0a 20 20 69 6e 74 20 69 41 6d 74 2c 20 */. int iAmt,
1f885 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1f886 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 Number of bytes
1f887 74 6f 20 72 65 61 64 20 2a 2f 0a 20 20 73 71 6c to read */. sql
1f888 69 74 65 5f 69 6e 74 36 34 20 69 4f 66 73 74 20 ite_int64 iOfst
1f889 20 20 20 20 2f 2a 20 42 65 67 69 6e 20 72 65 61 /* Begin rea
1f88a 64 69 6e 67 20 61 74 20 74 68 69 73 20 6f 66 66 ding at this off
1f88b 73 65 74 20 2a 2f 0a 29 7b 0a 20 20 4d 65 6d 4a set */.){. MemJ
1f88c 6f 75 72 6e 61 6c 20 2a 70 20 3d 20 28 4d 65 6d ournal *p = (Mem
1f88d 4a 6f 75 72 6e 61 6c 20 2a 29 70 4a 66 64 3b 0a Journal *)pJfd;.
1f88e 20 20 75 38 20 2a 7a 4f 75 74 20 3d 20 7a 42 75 u8 *zOut = zBu
1f88f 66 3b 0a 20 20 69 6e 74 20 6e 52 65 61 64 20 3d f;. int nRead =
1f890 20 69 41 6d 74 3b 0a 20 20 69 6e 74 20 69 43 68 iAmt;. int iCh
1f891 75 6e 6b 4f 66 66 73 65 74 3b 0a 20 20 46 69 6c unkOffset;. Fil
1f892 65 43 68 75 6e 6b 20 2a 70 43 68 75 6e 6b 3b 0a eChunk *pChunk;.
1f893 0a 20 20 2f 2a 20 53 51 4c 69 74 65 20 6e 65 76 . /* SQLite nev
1f894 65 72 20 74 72 69 65 73 20 74 6f 20 72 65 61 64 er tries to read
1f895 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 past the end of
1f896 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 a rollback jour
1f897 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 20 61 73 nal file */. as
1f898 73 65 72 74 28 20 69 4f 66 73 74 2b 69 41 6d 74 sert( iOfst+iAmt
1f899 3c 3d 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e 69 4f <=p->endpoint.iO
1f89a 66 66 73 65 74 20 29 3b 0a 0a 20 20 69 66 28 20 ffset );.. if(
1f89b 70 2d 3e 72 65 61 64 70 6f 69 6e 74 2e 69 4f 66 p->readpoint.iOf
1f89c 66 73 65 74 21 3d 69 4f 66 73 74 20 7c 7c 20 69 fset!=iOfst || i
1f89d 4f 66 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 73 Ofst==0 ){. s
1f89e 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 4f 66 qlite3_int64 iOf
1f89f 66 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28 70 f = 0;. for(p
1f8a0 43 68 75 6e 6b 3d 70 2d 3e 70 46 69 72 73 74 3b Chunk=p->pFirst;
1f8a1 20 0a 20 20 20 20 20 20 20 20 41 4c 57 41 59 53 . ALWAYS
1f8a2 28 70 43 68 75 6e 6b 29 20 26 26 20 28 69 4f 66 (pChunk) && (iOf
1f8a3 66 2b 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 f+JOURNAL_CHUNKS
1f8a4 49 5a 45 29 3c 3d 69 4f 66 73 74 3b 0a 20 20 20 IZE)<=iOfst;.
1f8a5 20 20 20 20 20 70 43 68 75 6e 6b 3d 70 43 68 75 pChunk=pChu
1f8a6 6e 6b 2d 3e 70 4e 65 78 74 0a 20 20 20 20 29 7b nk->pNext. ){
1f8a7 0a 20 20 20 20 20 20 69 4f 66 66 20 2b 3d 20 4a . iOff += J
1f8a8 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 OURNAL_CHUNKSIZE
1f8a9 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b ;. }. }else{
1f8aa 0a 20 20 20 20 70 43 68 75 6e 6b 20 3d 20 70 2d . pChunk = p-
1f8ab 3e 72 65 61 64 70 6f 69 6e 74 2e 70 43 68 75 6e >readpoint.pChun
1f8ac 6b 3b 0a 20 20 7d 0a 0a 20 20 69 43 68 75 6e 6b k;. }.. iChunk
1f8ad 4f 66 66 73 65 74 20 3d 20 28 69 6e 74 29 28 69 Offset = (int)(i
1f8ae 4f 66 73 74 25 4a 4f 55 52 4e 41 4c 5f 43 48 55 Ofst%JOURNAL_CHU
1f8af 4e 4b 53 49 5a 45 29 3b 0a 20 20 64 6f 20 7b 0a NKSIZE);. do {.
1f8b0 20 20 20 20 69 6e 74 20 69 53 70 61 63 65 20 3d int iSpace =
1f8b1 20 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 49 JOURNAL_CHUNKSI
1f8b2 5a 45 20 2d 20 69 43 68 75 6e 6b 4f 66 66 73 65 ZE - iChunkOffse
1f8b3 74 3b 0a 20 20 20 20 69 6e 74 20 6e 43 6f 70 79 t;. int nCopy
1f8b4 20 3d 20 4d 49 4e 28 6e 52 65 61 64 2c 20 28 4a = MIN(nRead, (J
1f8b5 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 OURNAL_CHUNKSIZE
1f8b6 20 2d 20 69 43 68 75 6e 6b 4f 66 66 73 65 74 29 - iChunkOffset)
1f8b7 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 7a 4f );. memcpy(zO
1f8b8 75 74 2c 20 26 70 43 68 75 6e 6b 2d 3e 7a 43 68 ut, &pChunk->zCh
1f8b9 75 6e 6b 5b 69 43 68 75 6e 6b 4f 66 66 73 65 74 unk[iChunkOffset
1f8ba 5d 2c 20 6e 43 6f 70 79 29 3b 0a 20 20 20 20 7a ], nCopy);. z
1f8bb 4f 75 74 20 2b 3d 20 6e 43 6f 70 79 3b 0a 20 20 Out += nCopy;.
1f8bc 20 20 6e 52 65 61 64 20 2d 3d 20 69 53 70 61 63 nRead -= iSpac
1f8bd 65 3b 0a 20 20 20 20 69 43 68 75 6e 6b 4f 66 66 e;. iChunkOff
1f8be 73 65 74 20 3d 20 30 3b 0a 20 20 7d 20 77 68 69 set = 0;. } whi
1f8bf 6c 65 28 20 6e 52 65 61 64 3e 3d 30 20 26 26 20 le( nRead>=0 &&
1f8c0 28 70 43 68 75 6e 6b 3d 70 43 68 75 6e 6b 2d 3e (pChunk=pChunk->
1f8c1 70 4e 65 78 74 29 21 3d 30 20 26 26 20 6e 52 65 pNext)!=0 && nRe
1f8c2 61 64 3e 30 20 29 3b 0a 20 20 70 2d 3e 72 65 61 ad>0 );. p->rea
1f8c3 64 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 20 3d dpoint.iOffset =
1f8c4 20 69 4f 66 73 74 2b 69 41 6d 74 3b 0a 20 20 70 iOfst+iAmt;. p
1f8c5 2d 3e 72 65 61 64 70 6f 69 6e 74 2e 70 43 68 75 ->readpoint.pChu
1f8c6 6e 6b 20 3d 20 70 43 68 75 6e 6b 3b 0a 0a 20 20 nk = pChunk;..
1f8c7 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
1f8c8 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 ;.}../*.** Write
1f8c9 20 64 61 74 61 20 74 6f 20 74 68 65 20 66 69 6c data to the fil
1f8ca 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
1f8cb 20 6d 65 6d 6a 72 6e 6c 57 72 69 74 65 28 0a 20 memjrnlWrite(.
1f8cc 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 sqlite3_file *p
1f8cd 4a 66 64 2c 20 20 20 20 2f 2a 20 54 68 65 20 6a Jfd, /* The j
1f8ce 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 6e 74 6f ournal file into
1f8cf 20 77 68 69 63 68 20 74 6f 20 77 72 69 74 65 20 which to write
1f8d0 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 */. const void
1f8d1 2a 7a 42 75 66 2c 20 20 20 20 20 20 2f 2a 20 54 *zBuf, /* T
1f8d2 61 6b 65 20 64 61 74 61 20 74 6f 20 62 65 20 77 ake data to be w
1f8d3 72 69 74 74 65 6e 20 66 72 6f 6d 20 68 65 72 65 ritten from here
1f8d4 20 2a 2f 0a 20 20 69 6e 74 20 69 41 6d 74 2c 20 */. int iAmt,
1f8d5 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1f8d6 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 Number of bytes
1f8d7 74 6f 20 77 72 69 74 65 20 2a 2f 0a 20 20 73 71 to write */. sq
1f8d8 6c 69 74 65 5f 69 6e 74 36 34 20 69 4f 66 73 74 lite_int64 iOfst
1f8d9 20 20 20 20 20 2f 2a 20 42 65 67 69 6e 20 77 72 /* Begin wr
1f8da 69 74 69 6e 67 20 61 74 20 74 68 69 73 20 6f 66 iting at this of
1f8db 66 73 65 74 20 69 6e 74 6f 20 74 68 65 20 66 69 fset into the fi
1f8dc 6c 65 20 2a 2f 0a 29 7b 0a 20 20 4d 65 6d 4a 6f le */.){. MemJo
1f8dd 75 72 6e 61 6c 20 2a 70 20 3d 20 28 4d 65 6d 4a urnal *p = (MemJ
1f8de 6f 75 72 6e 61 6c 20 2a 29 70 4a 66 64 3b 0a 20 ournal *)pJfd;.
1f8df 20 69 6e 74 20 6e 57 72 69 74 65 20 3d 20 69 41 int nWrite = iA
1f8e0 6d 74 3b 0a 20 20 75 38 20 2a 7a 57 72 69 74 65 mt;. u8 *zWrite
1f8e1 20 3d 20 28 75 38 20 2a 29 7a 42 75 66 3b 0a 0a = (u8 *)zBuf;..
1f8e2 20 20 2f 2a 20 41 6e 20 69 6e 2d 6d 65 6d 6f 72 /* An in-memor
1f8e3 79 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 73 y journal file s
1f8e4 68 6f 75 6c 64 20 6f 6e 6c 79 20 65 76 65 72 20 hould only ever
1f8e5 62 65 20 61 70 70 65 6e 64 65 64 20 74 6f 2e 20 be appended to.
1f8e6 52 61 6e 64 6f 6d 0a 20 20 2a 2a 20 61 63 63 65 Random. ** acce
1f8e7 73 73 20 77 72 69 74 65 73 20 61 72 65 20 6e 6f ss writes are no
1f8e8 74 20 72 65 71 75 69 72 65 64 20 62 79 20 73 71 t required by sq
1f8e9 6c 69 74 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 lite.. */. ass
1f8ea 65 72 74 28 20 69 4f 66 73 74 3d 3d 70 2d 3e 65 ert( iOfst==p->e
1f8eb 6e 64 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 20 ndpoint.iOffset
1f8ec 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 );. UNUSED_PARA
1f8ed 4d 45 54 45 52 28 69 4f 66 73 74 29 3b 0a 0a 20 METER(iOfst);..
1f8ee 20 77 68 69 6c 65 28 20 6e 57 72 69 74 65 3e 30 while( nWrite>0
1f8ef 20 29 7b 0a 20 20 20 20 46 69 6c 65 43 68 75 6e ){. FileChun
1f8f0 6b 20 2a 70 43 68 75 6e 6b 20 3d 20 70 2d 3e 65 k *pChunk = p->e
1f8f1 6e 64 70 6f 69 6e 74 2e 70 43 68 75 6e 6b 3b 0a ndpoint.pChunk;.
1f8f2 20 20 20 20 69 6e 74 20 69 43 68 75 6e 6b 4f 66 int iChunkOf
1f8f3 66 73 65 74 20 3d 20 28 69 6e 74 29 28 70 2d 3e fset = (int)(p->
1f8f4 65 6e 64 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 endpoint.iOffset
1f8f5 25 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 49 %JOURNAL_CHUNKSI
1f8f6 5a 45 29 3b 0a 20 20 20 20 69 6e 74 20 69 53 70 ZE);. int iSp
1f8f7 61 63 65 20 3d 20 4d 49 4e 28 6e 57 72 69 74 65 ace = MIN(nWrite
1f8f8 2c 20 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 , JOURNAL_CHUNKS
1f8f9 49 5a 45 20 2d 20 69 43 68 75 6e 6b 4f 66 66 73 IZE - iChunkOffs
1f8fa 65 74 29 3b 0a 0a 20 20 20 20 69 66 28 20 69 43 et);.. if( iC
1f8fb 68 75 6e 6b 4f 66 66 73 65 74 3d 3d 30 20 29 7b hunkOffset==0 ){
1f8fc 0a 20 20 20 20 20 20 2f 2a 20 4e 65 77 20 63 68 . /* New ch
1f8fd 75 6e 6b 20 69 73 20 72 65 71 75 69 72 65 64 20 unk is required
1f8fe 74 6f 20 65 78 74 65 6e 64 20 74 68 65 20 66 69 to extend the fi
1f8ff 6c 65 2e 20 2a 2f 0a 20 20 20 20 20 20 46 69 6c le. */. Fil
1f900 65 43 68 75 6e 6b 20 2a 70 4e 65 77 20 3d 20 73 eChunk *pNew = s
1f901 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 qlite3_malloc(si
1f902 7a 65 6f 66 28 46 69 6c 65 43 68 75 6e 6b 29 29 zeof(FileChunk))
1f903 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70 4e 65 ;. if( !pNe
1f904 77 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 w ){. ret
1f905 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 urn SQLITE_IOERR
1f906 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 0a _NOMEM;. }.
1f907 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 4e 65 78 pNew->pNex
1f908 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 66 28 t = 0;. if(
1f909 20 70 43 68 75 6e 6b 20 29 7b 0a 20 20 20 20 20 pChunk ){.
1f90a 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 46 assert( p->pF
1f90b 69 72 73 74 20 29 3b 0a 20 20 20 20 20 20 20 20 irst );.
1f90c 70 43 68 75 6e 6b 2d 3e 70 4e 65 78 74 20 3d 20 pChunk->pNext =
1f90d 70 4e 65 77 3b 0a 20 20 20 20 20 20 7d 65 6c 73 pNew;. }els
1f90e 65 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 e{. asser
1f90f 74 28 20 21 70 2d 3e 70 46 69 72 73 74 20 29 3b t( !p->pFirst );
1f910 0a 20 20 20 20 20 20 20 20 70 2d 3e 70 46 69 72 . p->pFir
1f911 73 74 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 20 st = pNew;.
1f912 20 7d 0a 20 20 20 20 20 20 70 2d 3e 65 6e 64 70 }. p->endp
1f913 6f 69 6e 74 2e 70 43 68 75 6e 6b 20 3d 20 70 4e oint.pChunk = pN
1f914 65 77 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 6d ew;. }.. m
1f915 65 6d 63 70 79 28 26 70 2d 3e 65 6e 64 70 6f 69 emcpy(&p->endpoi
1f916 6e 74 2e 70 43 68 75 6e 6b 2d 3e 7a 43 68 75 6e nt.pChunk->zChun
1f917 6b 5b 69 43 68 75 6e 6b 4f 66 66 73 65 74 5d 2c k[iChunkOffset],
1f918 20 7a 57 72 69 74 65 2c 20 69 53 70 61 63 65 29 zWrite, iSpace)
1f919 3b 0a 20 20 20 20 7a 57 72 69 74 65 20 2b 3d 20 ;. zWrite +=
1f91a 69 53 70 61 63 65 3b 0a 20 20 20 20 6e 57 72 69 iSpace;. nWri
1f91b 74 65 20 2d 3d 20 69 53 70 61 63 65 3b 0a 20 20 te -= iSpace;.
1f91c 20 20 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e 69 4f p->endpoint.iO
1f91d 66 66 73 65 74 20 2b 3d 20 69 53 70 61 63 65 3b ffset += iSpace;
1f91e 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 53 . }.. return S
1f91f 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a QLITE_OK;.}../*.
1f920 2a 2a 20 54 72 75 6e 63 61 74 65 20 74 68 65 20 ** Truncate the
1f921 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 file..*/.static
1f922 69 6e 74 20 6d 65 6d 6a 72 6e 6c 54 72 75 6e 63 int memjrnlTrunc
1f923 61 74 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ate(sqlite3_file
1f924 20 2a 70 4a 66 64 2c 20 73 71 6c 69 74 65 5f 69 *pJfd, sqlite_i
1f925 6e 74 36 34 20 73 69 7a 65 29 7b 0a 20 20 4d 65 nt64 size){. Me
1f926 6d 4a 6f 75 72 6e 61 6c 20 2a 70 20 3d 20 28 4d mJournal *p = (M
1f927 65 6d 4a 6f 75 72 6e 61 6c 20 2a 29 70 4a 66 64 emJournal *)pJfd
1f928 3b 0a 20 20 46 69 6c 65 43 68 75 6e 6b 20 2a 70 ;. FileChunk *p
1f929 43 68 75 6e 6b 3b 0a 20 20 61 73 73 65 72 74 28 Chunk;. assert(
1f92a 73 69 7a 65 3d 3d 30 29 3b 0a 20 20 55 4e 55 53 size==0);. UNUS
1f92b 45 44 5f 50 41 52 41 4d 45 54 45 52 28 73 69 7a ED_PARAMETER(siz
1f92c 65 29 3b 0a 20 20 70 43 68 75 6e 6b 20 3d 20 70 e);. pChunk = p
1f92d 2d 3e 70 46 69 72 73 74 3b 0a 20 20 77 68 69 6c ->pFirst;. whil
1f92e 65 28 20 70 43 68 75 6e 6b 20 29 7b 0a 20 20 20 e( pChunk ){.
1f92f 20 46 69 6c 65 43 68 75 6e 6b 20 2a 70 54 6d 70 FileChunk *pTmp
1f930 20 3d 20 70 43 68 75 6e 6b 3b 0a 20 20 20 20 70 = pChunk;. p
1f931 43 68 75 6e 6b 20 3d 20 70 43 68 75 6e 6b 2d 3e Chunk = pChunk->
1f932 70 4e 65 78 74 3b 0a 20 20 20 20 73 71 6c 69 74 pNext;. sqlit
1f933 65 33 5f 66 72 65 65 28 70 54 6d 70 29 3b 0a 20 e3_free(pTmp);.
1f934 20 7d 0a 20 20 73 71 6c 69 74 65 33 4d 65 6d 4a }. sqlite3MemJ
1f935 6f 75 72 6e 61 6c 4f 70 65 6e 28 70 4a 66 64 29 ournalOpen(pJfd)
1f936 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
1f937 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 E_OK;.}../*.** C
1f938 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 2e 0a 2a lose the file..*
1f939 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d /.static int mem
1f93a 6a 72 6e 6c 43 6c 6f 73 65 28 73 71 6c 69 74 65 jrnlClose(sqlite
1f93b 33 5f 66 69 6c 65 20 2a 70 4a 66 64 29 7b 0a 20 3_file *pJfd){.
1f93c 20 6d 65 6d 6a 72 6e 6c 54 72 75 6e 63 61 74 65 memjrnlTruncate
1f93d 28 70 4a 66 64 2c 20 30 29 3b 0a 20 20 72 65 74 (pJfd, 0);. ret
1f93e 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
1f93f 0a 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e 63 20 74 68 .../*.** Sync th
1f940 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 53 79 e file..**.** Sy
1f941 6e 63 69 6e 67 20 61 6e 20 69 6e 2d 6d 65 6d 6f ncing an in-memo
1f942 72 79 20 6a 6f 75 72 6e 61 6c 20 69 73 20 61 20 ry journal is a
1f943 6e 6f 2d 6f 70 2e 20 20 41 6e 64 2c 20 69 6e 20 no-op. And, in
1f944 66 61 63 74 2c 20 74 68 69 73 20 72 6f 75 74 69 fact, this routi
1f945 6e 65 0a 2a 2a 20 69 73 20 6e 65 76 65 72 20 63 ne.** is never c
1f946 61 6c 6c 65 64 20 69 6e 20 61 20 77 6f 72 6b 69 alled in a worki
1f947 6e 67 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ng implementatio
1f948 6e 2e 20 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 n. This impleme
1f949 6e 74 61 74 69 6f 6e 0a 2a 2a 20 65 78 69 73 74 ntation.** exist
1f94a 73 20 70 75 72 65 6c 79 20 61 73 20 61 20 63 6f s purely as a co
1f94b 6e 74 69 6e 67 65 6e 63 79 2c 20 69 6e 20 63 61 ntingency, in ca
1f94c 73 65 20 73 6f 6d 65 20 6d 61 6c 66 75 6e 63 74 se some malfunct
1f94d 69 6f 6e 20 69 6e 20 73 6f 6d 65 20 6f 74 68 65 ion in some othe
1f94e 72 0a 2a 2a 20 70 61 72 74 20 6f 66 20 53 51 4c r.** part of SQL
1f94f 69 74 65 20 63 61 75 73 65 73 20 53 79 6e 63 20 ite causes Sync
1f950 74 6f 20 62 65 20 63 61 6c 6c 65 64 20 62 79 20 to be called by
1f951 6d 69 73 74 61 6b 65 2e 0a 2a 2f 0a 73 74 61 74 mistake..*/.stat
1f952 69 63 20 69 6e 74 20 6d 65 6d 6a 72 6e 6c 53 79 ic int memjrnlSy
1f953 6e 63 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 nc(sqlite3_file
1f954 2a 4e 6f 74 55 73 65 64 2c 20 69 6e 74 20 4e 6f *NotUsed, int No
1f955 74 55 73 65 64 32 29 7b 20 20 20 2f 2a 4e 4f 5f tUsed2){ /*NO_
1f956 54 45 53 54 2a 2f 0a 20 20 55 4e 55 53 45 44 5f TEST*/. UNUSED_
1f957 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 PARAMETER2(NotUs
1f958 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 20 20 ed, NotUsed2);
1f959 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f95a 20 20 20 20 20 20 2f 2a 4e 4f 5f 54 45 53 54 2a /*NO_TEST*
1f95b 2f 0a 20 20 61 73 73 65 72 74 28 20 30 20 29 3b /. assert( 0 );
1f95c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f95d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f95e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f95f 20 2f 2a 4e 4f 5f 54 45 53 54 2a 2f 0a 20 20 72 /*NO_TEST*/. r
1f960 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
1f961 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f962 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f963 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 4e 4f /*NO
1f964 5f 54 45 53 54 2a 2f 0a 7d 20 20 20 20 20 20 20 _TEST*/.}
1f965 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f966 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f967 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f968 20 20 20 20 20 20 20 2f 2a 4e 4f 5f 54 45 53 54 /*NO_TEST
1f969 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72 79 20 */../*.** Query
1f96a 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 the size of the
1f96b 66 69 6c 65 20 69 6e 20 62 79 74 65 73 2e 0a 2a file in bytes..*
1f96c 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d /.static int mem
1f96d 6a 72 6e 6c 46 69 6c 65 53 69 7a 65 28 73 71 6c jrnlFileSize(sql
1f96e 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c ite3_file *pJfd,
1f96f 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70 sqlite_int64 *p
1f970 53 69 7a 65 29 7b 0a 20 20 4d 65 6d 4a 6f 75 72 Size){. MemJour
1f971 6e 61 6c 20 2a 70 20 3d 20 28 4d 65 6d 4a 6f 75 nal *p = (MemJou
1f972 72 6e 61 6c 20 2a 29 70 4a 66 64 3b 0a 20 20 2a rnal *)pJfd;. *
1f973 70 53 69 7a 65 20 3d 20 28 73 71 6c 69 74 65 5f pSize = (sqlite_
1f974 69 6e 74 36 34 29 20 70 2d 3e 65 6e 64 70 6f 69 int64) p->endpoi
1f975 6e 74 2e 69 4f 66 66 73 65 74 3b 0a 20 20 72 65 nt.iOffset;. re
1f976 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
1f977 7d 0a 0a 2f 2a 0a 2a 2a 20 54 61 62 6c 65 20 6f }../*.** Table o
1f978 66 20 6d 65 74 68 6f 64 73 20 66 6f 72 20 4d 65 f methods for Me
1f979 6d 4a 6f 75 72 6e 61 6c 20 73 71 6c 69 74 65 33 mJournal sqlite3
1f97a 5f 66 69 6c 65 20 6f 62 6a 65 63 74 2e 0a 2a 2f _file object..*/
1f97b 0a 73 74 61 74 69 63 20 73 74 72 75 63 74 20 73 .static struct s
1f97c 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 qlite3_io_method
1f97d 73 20 4d 65 6d 4a 6f 75 72 6e 61 6c 4d 65 74 68 s MemJournalMeth
1f97e 6f 64 73 20 3d 20 7b 0a 20 20 31 2c 20 20 20 20 ods = {. 1,
1f97f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 /* i
1f980 56 65 72 73 69 6f 6e 20 2a 2f 0a 20 20 6d 65 6d Version */. mem
1f981 6a 72 6e 6c 43 6c 6f 73 65 2c 20 20 20 20 20 2f jrnlClose, /
1f982 2a 20 78 43 6c 6f 73 65 20 2a 2f 0a 20 20 6d 65 * xClose */. me
1f983 6d 6a 72 6e 6c 52 65 61 64 2c 20 20 20 20 20 20 mjrnlRead,
1f984 2f 2a 20 78 52 65 61 64 20 2a 2f 0a 20 20 6d 65 /* xRead */. me
1f985 6d 6a 72 6e 6c 57 72 69 74 65 2c 20 20 20 20 20 mjrnlWrite,
1f986 2f 2a 20 78 57 72 69 74 65 20 2a 2f 0a 20 20 6d /* xWrite */. m
1f987 65 6d 6a 72 6e 6c 54 72 75 6e 63 61 74 65 2c 20 emjrnlTruncate,
1f988 20 2f 2a 20 78 54 72 75 6e 63 61 74 65 20 2a 2f /* xTruncate */
1f989 0a 20 20 6d 65 6d 6a 72 6e 6c 53 79 6e 63 2c 20 . memjrnlSync,
1f98a 20 20 20 20 20 2f 2a 20 78 53 79 6e 63 20 2a 2f /* xSync */
1f98b 0a 20 20 6d 65 6d 6a 72 6e 6c 46 69 6c 65 53 69 . memjrnlFileSi
1f98c 7a 65 2c 20 20 2f 2a 20 78 46 69 6c 65 53 69 7a ze, /* xFileSiz
1f98d 65 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 e */. 0,
1f98e 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63 /* xLoc
1f98f 6b 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 k */. 0,
1f990 20 20 20 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c /* xUnl
1f991 6f 63 6b 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 ock */. 0,
1f992 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43 /* xC
1f993 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b heckReservedLock
1f994 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 20 */. 0,
1f995 20 20 20 20 20 20 20 20 2f 2a 20 78 46 69 6c 65 /* xFile
1f996 43 6f 6e 74 72 6f 6c 20 2a 2f 0a 20 20 30 2c 20 Control */. 0,
1f997 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1f998 2a 20 78 53 65 63 74 6f 72 53 69 7a 65 20 2a 2f * xSectorSize */
1f999 0a 20 20 30 20 20 20 20 20 20 20 20 20 20 20 20 . 0
1f99a 20 20 20 20 20 2f 2a 20 78 44 65 76 69 63 65 43 /* xDeviceC
1f99b 68 61 72 61 63 74 65 72 69 73 74 69 63 73 20 2a haracteristics *
1f99c 2f 0a 7d 3b 0a 0a 2f 2a 20 0a 2a 2a 20 4f 70 65 /.};../* .** Ope
1f99d 6e 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 n a journal file
1f99e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1f99f 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
1f9a0 4d 65 6d 4a 6f 75 72 6e 61 6c 4f 70 65 6e 28 73 MemJournalOpen(s
1f9a1 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 qlite3_file *pJf
1f9a2 64 29 7b 0a 20 20 4d 65 6d 4a 6f 75 72 6e 61 6c d){. MemJournal
1f9a3 20 2a 70 20 3d 20 28 4d 65 6d 4a 6f 75 72 6e 61 *p = (MemJourna
1f9a4 6c 20 2a 29 70 4a 66 64 3b 0a 20 20 61 73 73 65 l *)pJfd;. asse
1f9a5 72 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41 rt( EIGHT_BYTE_A
1f9a6 4c 49 47 4e 4d 45 4e 54 28 70 29 20 29 3b 0a 20 LIGNMENT(p) );.
1f9a7 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 73 71 memset(p, 0, sq
1f9a8 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 53 lite3MemJournalS
1f9a9 69 7a 65 28 29 29 3b 0a 20 20 70 2d 3e 70 4d 65 ize());. p->pMe
1f9aa 74 68 6f 64 20 3d 20 26 4d 65 6d 4a 6f 75 72 6e thod = &MemJourn
1f9ab 61 6c 4d 65 74 68 6f 64 73 3b 0a 7d 0a 0a 2f 2a alMethods;.}../*
1f9ac 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 .** Return true
1f9ad 69 66 20 74 68 65 20 66 69 6c 65 2d 68 61 6e 64 if the file-hand
1f9ae 6c 65 20 70 61 73 73 65 64 20 61 73 20 61 6e 20 le passed as an
1f9af 61 72 67 75 6d 65 6e 74 20 69 73 20 0a 2a 2a 20 argument is .**
1f9b0 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 an in-memory jou
1f9b1 72 6e 61 6c 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f rnal .*/.SQLITE_
1f9b2 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
1f9b3 74 65 33 49 73 4d 65 6d 4a 6f 75 72 6e 61 6c 28 te3IsMemJournal(
1f9b4 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a sqlite3_file *pJ
1f9b5 66 64 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 4a fd){. return pJ
1f9b6 66 64 2d 3e 70 4d 65 74 68 6f 64 73 3d 3d 26 4d fd->pMethods==&M
1f9b7 65 6d 4a 6f 75 72 6e 61 6c 4d 65 74 68 6f 64 73 emJournalMethods
1f9b8 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 65 74 75 ;.}../* .** Retu
1f9b9 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 rn the number of
1f9ba 20 62 79 74 65 73 20 72 65 71 75 69 72 65 64 20 bytes required
1f9bb 74 6f 20 73 74 6f 72 65 20 61 20 4d 65 6d 4a 6f to store a MemJo
1f9bc 75 72 6e 61 6c 20 74 68 61 74 20 75 73 65 73 20 urnal that uses
1f9bd 76 66 73 0a 2a 2a 20 70 56 66 73 20 74 6f 20 63 vfs.** pVfs to c
1f9be 72 65 61 74 65 20 74 68 65 20 75 6e 64 65 72 6c reate the underl
1f9bf 79 69 6e 67 20 6f 6e 2d 64 69 73 6b 20 66 69 6c ying on-disk fil
1f9c0 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 es..*/.SQLITE_PR
1f9c1 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
1f9c2 33 4d 65 6d 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 3MemJournalSize(
1f9c3 76 6f 69 64 29 7b 0a 20 20 72 65 74 75 72 6e 20 void){. return
1f9c4 73 69 7a 65 6f 66 28 4d 65 6d 4a 6f 75 72 6e 61 sizeof(MemJourna
1f9c5 6c 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a l);.}../********
1f9c6 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 65 ****** End of me
1f9c7 6d 6a 6f 75 72 6e 61 6c 2e 63 20 2a 2a 2a 2a 2a mjournal.c *****
1f9c8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f9c9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f9ca 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
1f9cb 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c ****** Begin fil
1f9cc 65 20 77 61 6c 6b 65 72 2e 63 20 2a 2a 2a 2a 2a e walker.c *****
1f9cd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f9ce 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f9cf 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 *****/./*.** 200
1f9d0 38 20 41 75 67 75 73 74 20 31 36 0a 2a 2a 0a 2a 8 August 16.**.*
1f9d1 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 * The author dis
1f9d2 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 claims copyright
1f9d3 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 to this source
1f9d4 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 code. In place
1f9d5 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f of.** a legal no
1f9d6 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 tice, here is a
1f9d7 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 blessing:.**.**
1f9d8 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f May you do go
1f9d9 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e od and not evil.
1f9da 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 .** May you f
1f9db 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 ind forgiveness
1f9dc 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 for yourself and
1f9dd 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e forgive others.
1f9de 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 .** May you s
1f9df 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 hare freely, nev
1f9e0 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 er taking more t
1f9e1 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a han you give..**
1f9e2 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
1f9e3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f9e4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f9e5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f9e6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 **********.** Th
1f9e7 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 is file contains
1f9e8 20 72 6f 75 74 69 6e 65 73 20 75 73 65 64 20 66 routines used f
1f9e9 6f 72 20 77 61 6c 6b 69 6e 67 20 74 68 65 20 70 or walking the p
1f9ea 61 72 73 65 72 20 74 72 65 65 20 66 6f 72 0a 2a arser tree for.*
1f9eb 2a 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 * an SQL stateme
1f9ec 6e 74 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 77 nt..**.** $Id: w
1f9ed 61 6c 6b 65 72 2e 63 2c 76 20 31 2e 37 20 32 30 alker.c,v 1.7 20
1f9ee 30 39 2f 30 36 2f 31 35 20 32 33 3a 31 35 3a 35 09/06/15 23:15:5
1f9ef 39 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 9 drh Exp $.*/..
1f9f0 0a 2f 2a 0a 2a 2a 20 57 61 6c 6b 20 61 6e 20 65 ./*.** Walk an e
1f9f1 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2e 20 xpression tree.
1f9f2 20 49 6e 76 6f 6b 65 20 74 68 65 20 63 61 6c 6c Invoke the call
1f9f3 62 61 63 6b 20 6f 6e 63 65 20 66 6f 72 20 65 61 back once for ea
1f9f4 63 68 20 6e 6f 64 65 0a 2a 2a 20 6f 66 20 74 68 ch node.** of th
1f9f5 65 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 77 68 e expression, wh
1f9f6 69 6c 65 20 64 65 63 65 6e 64 69 6e 67 2e 20 20 ile decending.
1f9f7 28 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c (In other words,
1f9f8 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a the callback.**
1f9f9 20 69 73 20 69 6e 76 6f 6b 65 64 20 62 65 66 6f is invoked befo
1f9fa 72 65 20 76 69 73 69 74 69 6e 67 20 63 68 69 6c re visiting chil
1f9fb 64 72 65 6e 2e 29 0a 2a 2a 0a 2a 2a 20 54 68 65 dren.).**.** The
1f9fc 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 66 72 return value fr
1f9fd 6f 6d 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 om the callback
1f9fe 73 68 6f 75 6c 64 20 62 65 20 6f 6e 65 20 6f 66 should be one of
1f9ff 20 74 68 65 20 57 52 43 5f 2a 0a 2a 2a 20 63 6f the WRC_*.** co
1fa00 6e 73 74 61 6e 74 73 20 74 6f 20 73 70 65 63 69 nstants to speci
1fa01 66 79 20 68 6f 77 20 74 6f 20 70 72 6f 63 65 65 fy how to procee
1fa02 64 20 77 69 74 68 20 74 68 65 20 77 61 6c 6b 2e d with the walk.
1fa03 0a 2a 2a 0a 2a 2a 20 20 20 20 57 52 43 5f 43 6f .**.** WRC_Co
1fa04 6e 74 69 6e 75 65 20 20 20 20 20 20 43 6f 6e 74 ntinue Cont
1fa05 69 6e 75 65 20 64 65 73 63 65 6e 64 69 6e 67 20 inue descending
1fa06 64 6f 77 6e 20 74 68 65 20 74 72 65 65 2e 0a 2a down the tree..*
1fa07 2a 0a 2a 2a 20 20 20 20 57 52 43 5f 50 72 75 6e *.** WRC_Prun
1fa08 65 20 20 20 20 20 20 20 20 20 44 6f 20 6e 6f 74 e Do not
1fa09 20 64 65 73 63 65 6e 64 20 69 6e 74 6f 20 63 68 descend into ch
1fa0a 69 6c 64 20 6e 6f 64 65 73 2e 20 20 42 75 74 20 ild nodes. But
1fa0b 61 6c 6c 6f 77 0a 2a 2a 20 20 20 20 20 20 20 20 allow.**
1fa0c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68 th
1fa0d 65 20 77 61 6c 6b 20 74 6f 20 63 6f 6e 74 69 6e e walk to contin
1fa0e 75 65 20 77 69 74 68 20 73 69 62 6c 69 6e 67 20 ue with sibling
1fa0f 6e 6f 64 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 nodes..**.**
1fa10 57 52 43 5f 41 62 6f 72 74 20 20 20 20 20 20 20 WRC_Abort
1fa11 20 20 44 6f 20 6e 6f 20 6d 6f 72 65 20 63 61 6c Do no more cal
1fa12 6c 62 61 63 6b 73 2e 20 20 55 6e 77 69 6e 64 20 lbacks. Unwind
1fa13 74 68 65 20 73 74 61 63 6b 20 61 6e 64 0a 2a 2a the stack and.**
1fa14 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fa15 20 20 20 20 20 20 72 65 74 75 72 6e 20 74 68 65 return the
1fa16 20 74 6f 70 2d 6c 65 76 65 6c 20 77 61 6c 6b 20 top-level walk
1fa17 63 61 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 call..**.** The
1fa18 72 65 74 75 72 6e 20 76 61 6c 75 65 20 66 72 6f return value fro
1fa19 6d 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 m this routine i
1fa1a 73 20 57 52 43 5f 41 62 6f 72 74 20 74 6f 20 61 s WRC_Abort to a
1fa1b 62 61 6e 64 6f 6e 20 74 68 65 20 74 72 65 65 20 bandon the tree
1fa1c 77 61 6c 6b 0a 2a 2a 20 61 6e 64 20 57 52 43 5f walk.** and WRC_
1fa1d 43 6f 6e 74 69 6e 75 65 20 74 6f 20 63 6f 6e 74 Continue to cont
1fa1e 69 6e 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f inue..*/.SQLITE_
1fa1f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
1fa20 74 65 33 57 61 6c 6b 45 78 70 72 28 57 61 6c 6b te3WalkExpr(Walk
1fa21 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 45 78 70 er *pWalker, Exp
1fa22 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 69 6e 74 r *pExpr){. int
1fa23 20 72 63 3b 0a 20 20 69 66 28 20 70 45 78 70 72 rc;. if( pExpr
1fa24 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 57 52 43 ==0 ) return WRC
1fa25 5f 43 6f 6e 74 69 6e 75 65 3b 0a 20 20 74 65 73 _Continue;. tes
1fa26 74 63 61 73 65 28 20 45 78 70 72 48 61 73 50 72 tcase( ExprHasPr
1fa27 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 operty(pExpr, EP
1fa28 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29 3b 0a 20 _TokenOnly) );.
1fa29 20 74 65 73 74 63 61 73 65 28 20 45 78 70 72 48 testcase( ExprH
1fa2a 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 asProperty(pExpr
1fa2b 2c 20 45 50 5f 52 65 64 75 63 65 64 29 20 29 3b , EP_Reduced) );
1fa2c 0a 20 20 72 63 20 3d 20 70 57 61 6c 6b 65 72 2d . rc = pWalker-
1fa2d 3e 78 45 78 70 72 43 61 6c 6c 62 61 63 6b 28 70 >xExprCallback(p
1fa2e 57 61 6c 6b 65 72 2c 20 70 45 78 70 72 29 3b 0a Walker, pExpr);.
1fa2f 20 20 69 66 28 20 72 63 3d 3d 57 52 43 5f 43 6f if( rc==WRC_Co
1fa30 6e 74 69 6e 75 65 0a 20 20 20 20 20 20 20 20 20 ntinue.
1fa31 20 20 20 20 20 26 26 20 21 45 78 70 72 48 61 73 && !ExprHas
1fa32 41 6e 79 50 72 6f 70 65 72 74 79 28 70 45 78 70 AnyProperty(pExp
1fa33 72 2c 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 20 r,EP_TokenOnly)
1fa34 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 ){. if( sqlit
1fa35 65 33 57 61 6c 6b 45 78 70 72 28 70 57 61 6c 6b e3WalkExpr(pWalk
1fa36 65 72 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 er, pExpr->pLeft
1fa37 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 ) ) return WRC_A
1fa38 62 6f 72 74 3b 0a 20 20 20 20 69 66 28 20 73 71 bort;. if( sq
1fa39 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 70 57 lite3WalkExpr(pW
1fa3a 61 6c 6b 65 72 2c 20 70 45 78 70 72 2d 3e 70 52 alker, pExpr->pR
1fa3b 69 67 68 74 29 20 29 20 72 65 74 75 72 6e 20 57 ight) ) return W
1fa3c 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 69 66 RC_Abort;. if
1fa3d 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 ( ExprHasPropert
1fa3e 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53 y(pExpr, EP_xIsS
1fa3f 65 6c 65 63 74 29 20 29 7b 0a 20 20 20 20 20 20 elect) ){.
1fa40 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 if( sqlite3WalkS
1fa41 65 6c 65 63 74 28 70 57 61 6c 6b 65 72 2c 20 70 elect(pWalker, p
1fa42 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74 29 Expr->x.pSelect)
1fa43 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 ) return WRC_Ab
1fa44 6f 72 74 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a ort;. }else{.
1fa45 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 if( sqlite
1fa46 33 57 61 6c 6b 45 78 70 72 4c 69 73 74 28 70 57 3WalkExprList(pW
1fa47 61 6c 6b 65 72 2c 20 70 45 78 70 72 2d 3e 78 2e alker, pExpr->x.
1fa48 70 4c 69 73 74 29 20 29 20 72 65 74 75 72 6e 20 pList) ) return
1fa49 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d WRC_Abort;. }
1fa4a 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
1fa4b 20 26 20 57 52 43 5f 41 62 6f 72 74 3b 0a 7d 0a & WRC_Abort;.}.
1fa4c 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 ./*.** Call sqli
1fa4d 74 65 33 57 61 6c 6b 45 78 70 72 28 29 20 66 6f te3WalkExpr() fo
1fa4e 72 20 65 76 65 72 79 20 65 78 70 72 65 73 73 69 r every expressi
1fa4f 6f 6e 20 69 6e 20 6c 69 73 74 20 70 20 6f 72 20 on in list p or
1fa50 75 6e 74 69 6c 0a 2a 2a 20 61 6e 20 61 62 6f 72 until.** an abor
1fa51 74 20 72 65 71 75 65 73 74 20 69 73 20 73 65 65 t request is see
1fa52 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 n..*/.SQLITE_PRI
1fa53 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
1fa54 57 61 6c 6b 45 78 70 72 4c 69 73 74 28 57 61 6c WalkExprList(Wal
1fa55 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 45 78 ker *pWalker, Ex
1fa56 70 72 4c 69 73 74 20 2a 70 29 7b 0a 20 20 69 6e prList *p){. in
1fa57 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20 45 78 t i;. struct Ex
1fa58 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 prList_item *pIt
1fa59 65 6d 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 em;. if( p ){.
1fa5a 20 20 20 66 6f 72 28 69 3d 70 2d 3e 6e 45 78 70 for(i=p->nExp
1fa5b 72 2c 20 70 49 74 65 6d 3d 70 2d 3e 61 3b 20 69 r, pItem=p->a; i
1fa5c 3e 30 3b 20 69 2d 2d 2c 20 70 49 74 65 6d 2b 2b >0; i--, pItem++
1fa5d 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c ){. if( sql
1fa5e 69 74 65 33 57 61 6c 6b 45 78 70 72 28 70 57 61 ite3WalkExpr(pWa
1fa5f 6c 6b 65 72 2c 20 70 49 74 65 6d 2d 3e 70 45 78 lker, pItem->pEx
1fa60 70 72 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 pr) ) return WRC
1fa61 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 _Abort;. }.
1fa62 7d 0a 20 20 72 65 74 75 72 6e 20 57 52 43 5f 43 }. return WRC_C
1fa63 6f 6e 74 69 6e 75 65 3b 0a 7d 0a 0a 2f 2a 0a 2a ontinue;.}../*.*
1fa64 2a 20 57 61 6c 6b 20 61 6c 6c 20 65 78 70 72 65 * Walk all expre
1fa65 73 73 69 6f 6e 73 20 61 73 73 6f 63 69 61 74 65 ssions associate
1fa66 64 20 77 69 74 68 20 53 45 4c 45 43 54 20 73 74 d with SELECT st
1fa67 61 74 65 6d 65 6e 74 20 70 2e 20 20 44 6f 0a 2a atement p. Do.*
1fa68 2a 20 6e 6f 74 20 69 6e 76 6f 6b 65 20 74 68 65 * not invoke the
1fa69 20 53 45 4c 45 43 54 20 63 61 6c 6c 62 61 63 6b SELECT callback
1fa6a 20 6f 6e 20 70 2c 20 62 75 74 20 64 6f 20 28 6f on p, but do (o
1fa6b 66 20 63 6f 75 72 73 65 29 20 69 6e 76 6f 6b 65 f course) invoke
1fa6c 0a 2a 2a 20 61 6e 79 20 65 78 70 72 20 63 61 6c .** any expr cal
1fa6d 6c 62 61 63 6b 73 20 61 6e 64 20 53 45 4c 45 43 lbacks and SELEC
1fa6e 54 20 63 61 6c 6c 62 61 63 6b 73 20 74 68 61 74 T callbacks that
1fa6f 20 63 6f 6d 65 20 66 72 6f 6d 20 73 75 62 71 75 come from subqu
1fa70 65 72 69 65 73 2e 0a 2a 2a 20 52 65 74 75 72 6e eries..** Return
1fa71 20 57 52 43 5f 41 62 6f 72 74 20 6f 72 20 57 52 WRC_Abort or WR
1fa72 43 5f 43 6f 6e 74 69 6e 75 65 2e 0a 2a 2f 0a 53 C_Continue..*/.S
1fa73 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
1fa74 74 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c t sqlite3WalkSel
1fa75 65 63 74 45 78 70 72 28 57 61 6c 6b 65 72 20 2a ectExpr(Walker *
1fa76 70 57 61 6c 6b 65 72 2c 20 53 65 6c 65 63 74 20 pWalker, Select
1fa77 2a 70 29 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 *p){. if( sqlit
1fa78 65 33 57 61 6c 6b 45 78 70 72 4c 69 73 74 28 70 e3WalkExprList(p
1fa79 57 61 6c 6b 65 72 2c 20 70 2d 3e 70 45 4c 69 73 Walker, p->pELis
1fa7a 74 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f t) ) return WRC_
1fa7b 41 62 6f 72 74 3b 0a 20 20 69 66 28 20 73 71 6c Abort;. if( sql
1fa7c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 70 57 61 ite3WalkExpr(pWa
1fa7d 6c 6b 65 72 2c 20 70 2d 3e 70 57 68 65 72 65 29 lker, p->pWhere)
1fa7e 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 ) return WRC_Ab
1fa7f 6f 72 74 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 ort;. if( sqlit
1fa80 65 33 57 61 6c 6b 45 78 70 72 4c 69 73 74 28 70 e3WalkExprList(p
1fa81 57 61 6c 6b 65 72 2c 20 70 2d 3e 70 47 72 6f 75 Walker, p->pGrou
1fa82 70 42 79 29 20 29 20 72 65 74 75 72 6e 20 57 52 pBy) ) return WR
1fa83 43 5f 41 62 6f 72 74 3b 0a 20 20 69 66 28 20 73 C_Abort;. if( s
1fa84 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 70 qlite3WalkExpr(p
1fa85 57 61 6c 6b 65 72 2c 20 70 2d 3e 70 48 61 76 69 Walker, p->pHavi
1fa86 6e 67 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 ng) ) return WRC
1fa87 5f 41 62 6f 72 74 3b 0a 20 20 69 66 28 20 73 71 _Abort;. if( sq
1fa88 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 4c 69 73 lite3WalkExprLis
1fa89 74 28 70 57 61 6c 6b 65 72 2c 20 70 2d 3e 70 4f t(pWalker, p->pO
1fa8a 72 64 65 72 42 79 29 20 29 20 72 65 74 75 72 6e rderBy) ) return
1fa8b 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 69 66 WRC_Abort;. if
1fa8c 28 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 ( sqlite3WalkExp
1fa8d 72 28 70 57 61 6c 6b 65 72 2c 20 70 2d 3e 70 4c r(pWalker, p->pL
1fa8e 69 6d 69 74 29 20 29 20 72 65 74 75 72 6e 20 57 imit) ) return W
1fa8f 52 43 5f 41 62 6f 72 74 3b 0a 20 20 69 66 28 20 RC_Abort;. if(
1fa90 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 sqlite3WalkExpr(
1fa91 70 57 61 6c 6b 65 72 2c 20 70 2d 3e 70 4f 66 66 pWalker, p->pOff
1fa92 73 65 74 29 20 29 20 72 65 74 75 72 6e 20 57 52 set) ) return WR
1fa93 43 5f 41 62 6f 72 74 3b 0a 20 20 72 65 74 75 72 C_Abort;. retur
1fa94 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a n WRC_Continue;.
1fa95 7d 0a 0a 2f 2a 0a 2a 2a 20 57 61 6c 6b 20 74 68 }../*.** Walk th
1fa96 65 20 70 61 72 73 65 20 74 72 65 65 73 20 61 73 e parse trees as
1fa97 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 6c sociated with al
1fa98 6c 20 73 75 62 71 75 65 72 69 65 73 20 69 6e 20 l subqueries in
1fa99 74 68 65 0a 2a 2a 20 46 52 4f 4d 20 63 6c 61 75 the.** FROM clau
1fa9a 73 65 20 6f 66 20 53 45 4c 45 43 54 20 73 74 61 se of SELECT sta
1fa9b 74 65 6d 65 6e 74 20 70 2e 20 20 44 6f 20 6e 6f tement p. Do no
1fa9c 74 20 69 6e 76 6f 6b 65 20 74 68 65 20 73 65 6c t invoke the sel
1fa9d 65 63 74 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 ect.** callback
1fa9e 6f 6e 20 70 2c 20 62 75 74 20 64 6f 20 69 6e 76 on p, but do inv
1fa9f 6f 6b 65 20 69 74 20 6f 6e 20 65 61 63 68 20 46 oke it on each F
1faa0 52 4f 4d 20 63 6c 61 75 73 65 20 73 75 62 71 75 ROM clause subqu
1faa1 65 72 79 0a 2a 2a 20 61 6e 64 20 6f 6e 20 61 6e ery.** and on an
1faa2 79 20 73 75 62 71 75 65 72 69 65 73 20 66 75 72 y subqueries fur
1faa3 74 68 65 72 20 64 6f 77 6e 20 69 6e 20 74 68 65 ther down in the
1faa4 20 74 72 65 65 2e 20 20 52 65 74 75 72 6e 20 0a tree. Return .
1faa5 2a 2a 20 57 52 43 5f 41 62 6f 72 74 20 6f 72 20 ** WRC_Abort or
1faa6 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 2a 2f WRC_Continue;.*/
1faa7 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1faa8 69 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 int sqlite3WalkS
1faa9 65 6c 65 63 74 46 72 6f 6d 28 57 61 6c 6b 65 72 electFrom(Walker
1faaa 20 2a 70 57 61 6c 6b 65 72 2c 20 53 65 6c 65 63 *pWalker, Selec
1faab 74 20 2a 70 29 7b 0a 20 20 53 72 63 4c 69 73 74 t *p){. SrcList
1faac 20 2a 70 53 72 63 3b 0a 20 20 69 6e 74 20 69 3b *pSrc;. int i;
1faad 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 . struct SrcLis
1faae 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 0a t_item *pItem;..
1faaf 20 20 70 53 72 63 20 3d 20 70 2d 3e 70 53 72 63 pSrc = p->pSrc
1fab0 3b 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 ;. if( ALWAYS(p
1fab1 53 72 63 29 20 29 7b 0a 20 20 20 20 66 6f 72 28 Src) ){. for(
1fab2 69 3d 70 53 72 63 2d 3e 6e 53 72 63 2c 20 70 49 i=pSrc->nSrc, pI
1fab3 74 65 6d 3d 70 53 72 63 2d 3e 61 3b 20 69 3e 30 tem=pSrc->a; i>0
1fab4 3b 20 69 2d 2d 2c 20 70 49 74 65 6d 2b 2b 29 7b ; i--, pItem++){
1fab5 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 . if( sqlit
1fab6 65 33 57 61 6c 6b 53 65 6c 65 63 74 28 70 57 61 e3WalkSelect(pWa
1fab7 6c 6b 65 72 2c 20 70 49 74 65 6d 2d 3e 70 53 65 lker, pItem->pSe
1fab8 6c 65 63 74 29 20 29 7b 0a 20 20 20 20 20 20 20 lect) ){.
1fab9 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 return WRC_Abor
1faba 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d t;. }. }
1fabb 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 57 52 . }. return WR
1fabc 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d 20 0a 0a C_Continue;.} ..
1fabd 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 74 /*.** Call sqlit
1fabe 65 33 57 61 6c 6b 45 78 70 72 28 29 20 66 6f 72 e3WalkExpr() for
1fabf 20 65 76 65 72 79 20 65 78 70 72 65 73 73 69 6f every expressio
1fac0 6e 20 69 6e 20 53 65 6c 65 63 74 20 73 74 61 74 n in Select stat
1fac1 65 6d 65 6e 74 20 70 2e 0a 2a 2a 20 49 6e 76 6f ement p..** Invo
1fac2 6b 65 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 ke sqlite3WalkSe
1fac3 6c 65 63 74 28 29 20 66 6f 72 20 73 75 62 71 75 lect() for subqu
1fac4 65 72 69 65 73 20 69 6e 20 74 68 65 20 46 52 4f eries in the FRO
1fac5 4d 20 63 6c 61 75 73 65 20 61 6e 64 0a 2a 2a 20 M clause and.**
1fac6 6f 6e 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 on the compound
1fac7 73 65 6c 65 63 74 20 63 68 61 69 6e 2c 20 70 2d select chain, p-
1fac8 3e 70 50 72 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20 52 >pPrior..**.** R
1fac9 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e eturn WRC_Contin
1faca 75 65 20 75 6e 64 65 72 20 6e 6f 72 6d 61 6c 20 ue under normal
1facb 63 6f 6e 64 69 74 69 6f 6e 73 2e 20 20 52 65 74 conditions. Ret
1facc 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 20 69 66 urn WRC_Abort if
1facd 0a 2a 2a 20 74 68 65 72 65 20 69 73 20 61 6e 20 .** there is an
1face 61 62 6f 72 74 20 72 65 71 75 65 73 74 2e 0a 2a abort request..*
1facf 2a 0a 2a 2a 20 49 66 20 74 68 65 20 57 61 6c 6b *.** If the Walk
1fad0 65 72 20 64 6f 65 73 20 6e 6f 74 20 68 61 76 65 er does not have
1fad1 20 61 6e 20 78 53 65 6c 65 63 74 43 61 6c 6c 62 an xSelectCallb
1fad2 61 63 6b 28 29 20 74 68 65 6e 20 74 68 69 73 20 ack() then this
1fad3 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 73 20 61 20 routine.** is a
1fad4 6e 6f 2d 6f 70 20 72 65 74 75 72 6e 69 6e 67 20 no-op returning
1fad5 57 52 43 5f 43 6f 6e 74 69 6e 75 65 2e 0a 2a 2f WRC_Continue..*/
1fad6 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1fad7 69 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 int sqlite3WalkS
1fad8 65 6c 65 63 74 28 57 61 6c 6b 65 72 20 2a 70 57 elect(Walker *pW
1fad9 61 6c 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a 70 alker, Select *p
1fada 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 ){. int rc;. i
1fadb 66 28 20 70 3d 3d 30 20 7c 7c 20 70 57 61 6c 6b f( p==0 || pWalk
1fadc 65 72 2d 3e 78 53 65 6c 65 63 74 43 61 6c 6c 62 er->xSelectCallb
1fadd 61 63 6b 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 ack==0 ) return
1fade 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 20 20 WRC_Continue;.
1fadf 72 63 20 3d 20 57 52 43 5f 43 6f 6e 74 69 6e 75 rc = WRC_Continu
1fae0 65 3b 0a 20 20 77 68 69 6c 65 28 20 70 20 20 29 e;. while( p )
1fae1 7b 0a 20 20 20 20 72 63 20 3d 20 70 57 61 6c 6b {. rc = pWalk
1fae2 65 72 2d 3e 78 53 65 6c 65 63 74 43 61 6c 6c 62 er->xSelectCallb
1fae3 61 63 6b 28 70 57 61 6c 6b 65 72 2c 20 70 29 3b ack(pWalker, p);
1fae4 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 62 72 . if( rc ) br
1fae5 65 61 6b 3b 0a 20 20 20 20 69 66 28 20 73 71 6c eak;. if( sql
1fae6 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 45 78 ite3WalkSelectEx
1fae7 70 72 28 70 57 61 6c 6b 65 72 2c 20 70 29 20 29 pr(pWalker, p) )
1fae8 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 return WRC_Abor
1fae9 74 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 t;. if( sqlit
1faea 65 33 57 61 6c 6b 53 65 6c 65 63 74 46 72 6f 6d e3WalkSelectFrom
1faeb 28 70 57 61 6c 6b 65 72 2c 20 70 29 20 29 20 72 (pWalker, p) ) r
1faec 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b eturn WRC_Abort;
1faed 0a 20 20 20 20 70 20 3d 20 70 2d 3e 70 50 72 69 . p = p->pPri
1faee 6f 72 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e or;. }. return
1faef 20 72 63 20 26 20 57 52 43 5f 41 62 6f 72 74 3b rc & WRC_Abort;
1faf0 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .}../***********
1faf1 2a 2a 2a 20 45 6e 64 20 6f 66 20 77 61 6c 6b 65 *** End of walke
1faf2 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a r.c ************
1faf3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1faf4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1faf5 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
1faf6 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 72 *** Begin file r
1faf7 65 73 6f 6c 76 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a esolve.c *******
1faf8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1faf9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fafa 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 41 **/./*.** 2008 A
1fafb 75 67 75 73 74 20 31 38 0a 2a 2a 0a 2a 2a 20 54 ugust 18.**.** T
1fafc 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 he author discla
1fafd 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f ims copyright to
1fafe 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 this source cod
1faff 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a e. In place of.
1fb00 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 ** a legal notic
1fb01 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 e, here is a ble
1fb02 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 ssing:.**.**
1fb03 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 May you do good
1fb04 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a and not evil..**
1fb05 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 May you find
1fb06 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 forgiveness for
1fb07 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f yourself and fo
1fb08 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a rgive others..**
1fb09 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 May you shar
1fb0a 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 e freely, never
1fb0b 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e taking more than
1fb0c 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a you give..**.**
1fb0d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fb0e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fb0f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fb10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fb11 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 *******.**.** Th
1fb12 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 is file contains
1fb13 20 72 6f 75 74 69 6e 65 73 20 75 73 65 64 20 66 routines used f
1fb14 6f 72 20 77 61 6c 6b 69 6e 67 20 74 68 65 20 70 or walking the p
1fb15 61 72 73 65 72 20 74 72 65 65 20 61 6e 64 0a 2a arser tree and.*
1fb16 2a 20 72 65 73 6f 6c 76 65 20 61 6c 6c 20 69 64 * resolve all id
1fb17 65 6e 74 69 66 69 65 72 73 20 62 79 20 61 73 73 entifiers by ass
1fb18 6f 63 69 61 74 69 6e 67 20 74 68 65 6d 20 77 69 ociating them wi
1fb19 74 68 20 61 20 70 61 72 74 69 63 75 6c 61 72 0a th a particular.
1fb1a 2a 2a 20 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c ** table and col
1fb1b 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 umn..**.** $Id:
1fb1c 72 65 73 6f 6c 76 65 2e 63 2c 76 20 31 2e 33 30 resolve.c,v 1.30
1fb1d 20 32 30 30 39 2f 30 36 2f 31 35 20 32 33 3a 31 2009/06/15 23:1
1fb1e 35 3a 35 39 20 64 72 68 20 45 78 70 20 24 0a 2a 5:59 drh Exp $.*
1fb1f 2f 0a 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20 74 68 /../*.** Turn th
1fb20 65 20 70 45 78 70 72 20 65 78 70 72 65 73 73 69 e pExpr expressi
1fb21 6f 6e 20 69 6e 74 6f 20 61 6e 20 61 6c 69 61 73 on into an alias
1fb22 20 66 6f 72 20 74 68 65 20 69 43 6f 6c 2d 74 68 for the iCol-th
1fb23 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 0a 2a column of the.*
1fb24 2a 20 72 65 73 75 6c 74 20 73 65 74 20 69 6e 20 * result set in
1fb25 70 45 4c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 pEList..**.** If
1fb26 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 the result set
1fb27 63 6f 6c 75 6d 6e 20 69 73 20 61 20 73 69 6d 70 column is a simp
1fb28 6c 65 20 63 6f 6c 75 6d 6e 20 72 65 66 65 72 65 le column refere
1fb29 6e 63 65 2c 20 74 68 65 6e 20 74 68 69 73 20 72 nce, then this r
1fb2a 6f 75 74 69 6e 65 0a 2a 2a 20 6d 61 6b 65 73 20 outine.** makes
1fb2b 61 6e 20 65 78 61 63 74 20 63 6f 70 79 2e 20 20 an exact copy.
1fb2c 42 75 74 20 66 6f 72 20 61 6e 79 20 6f 74 68 65 But for any othe
1fb2d 72 20 6b 69 6e 64 20 6f 66 20 65 78 70 72 65 73 r kind of expres
1fb2e 73 69 6f 6e 2c 20 74 68 69 73 0a 2a 2a 20 72 6f sion, this.** ro
1fb2f 75 74 69 6e 65 20 6d 61 6b 65 20 61 20 63 6f 70 utine make a cop
1fb30 79 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 y of the result
1fb31 73 65 74 20 63 6f 6c 75 6d 6e 20 61 73 20 74 68 set column as th
1fb32 65 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 e argument to th
1fb33 65 0a 2a 2a 20 54 4b 5f 41 53 20 6f 70 65 72 61 e.** TK_AS opera
1fb34 74 6f 72 2e 20 20 54 68 65 20 54 4b 5f 41 53 20 tor. The TK_AS
1fb35 6f 70 65 72 61 74 6f 72 20 63 61 75 73 65 73 20 operator causes
1fb36 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 the expression t
1fb37 6f 20 62 65 0a 2a 2a 20 65 76 61 6c 75 61 74 65 o be.** evaluate
1fb38 64 20 6a 75 73 74 20 6f 6e 63 65 20 61 6e 64 20 d just once and
1fb39 74 68 65 6e 20 72 65 75 73 65 64 20 66 6f 72 20 then reused for
1fb3a 65 61 63 68 20 61 6c 69 61 73 2e 0a 2a 2a 0a 2a each alias..**.*
1fb3b 2a 20 54 68 65 20 72 65 61 73 6f 6e 20 66 6f 72 * The reason for
1fb3c 20 73 75 70 70 72 65 73 73 69 6e 67 20 74 68 65 suppressing the
1fb3d 20 54 4b 5f 41 53 20 74 65 72 6d 20 77 68 65 6e TK_AS term when
1fb3e 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 the expression
1fb3f 69 73 20 61 20 73 69 6d 70 6c 65 0a 2a 2a 20 63 is a simple.** c
1fb40 6f 6c 75 6d 6e 20 72 65 66 65 72 65 6e 63 65 20 olumn reference
1fb41 69 73 20 73 6f 20 74 68 61 74 20 74 68 65 20 63 is so that the c
1fb42 6f 6c 75 6d 6e 20 72 65 66 65 72 65 6e 63 65 20 olumn reference
1fb43 77 69 6c 6c 20 62 65 20 72 65 63 6f 67 6e 69 7a will be recogniz
1fb44 65 64 20 61 73 0a 2a 2a 20 75 73 61 62 6c 65 20 ed as.** usable
1fb45 62 79 20 69 6e 64 69 63 65 73 20 77 69 74 68 69 by indices withi
1fb46 6e 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 n the WHERE clau
1fb47 73 65 20 70 72 6f 63 65 73 73 69 6e 67 20 6c 6f se processing lo
1fb48 67 69 63 2e 20 0a 2a 2a 0a 2a 2a 20 48 61 63 6b gic. .**.** Hack
1fb49 3a 20 20 54 68 65 20 54 4b 5f 41 53 20 6f 70 65 : The TK_AS ope
1fb4a 72 61 74 6f 72 20 69 73 20 69 6e 68 69 62 69 74 rator is inhibit
1fb4b 65 64 20 69 66 20 7a 54 79 70 65 5b 30 5d 3d 3d ed if zType[0]==
1fb4c 27 47 27 2e 20 20 54 68 69 73 20 6d 65 61 6e 73 'G'. This means
1fb4d 0a 2a 2a 20 74 68 61 74 20 69 6e 20 61 20 47 52 .** that in a GR
1fb4e 4f 55 50 20 42 59 20 63 6c 61 75 73 65 2c 20 74 OUP BY clause, t
1fb4f 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 he expression is
1fb50 20 65 76 61 6c 75 61 74 65 64 20 74 77 69 63 65 evaluated twice
1fb51 2e 20 20 48 65 6e 63 65 3a 0a 2a 2a 0a 2a 2a 20 . Hence:.**.**
1fb52 20 20 20 20 53 45 4c 45 43 54 20 72 61 6e 64 6f SELECT rando
1fb53 6d 28 29 25 35 20 41 53 20 78 2c 20 63 6f 75 6e m()%5 AS x, coun
1fb54 74 28 2a 29 20 46 52 4f 4d 20 74 61 62 20 47 52 t(*) FROM tab GR
1fb55 4f 55 50 20 42 59 20 78 0a 2a 2a 0a 2a 2a 20 49 OUP BY x.**.** I
1fb56 73 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 3a s equivalent to:
1fb57 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 .**.** SELEC
1fb58 54 20 72 61 6e 64 6f 6d 28 29 25 35 20 41 53 20 T random()%5 AS
1fb59 78 2c 20 63 6f 75 6e 74 28 2a 29 20 46 52 4f 4d x, count(*) FROM
1fb5a 20 74 61 62 20 47 52 4f 55 50 20 42 59 20 72 61 tab GROUP BY ra
1fb5b 6e 64 6f 6d 28 29 25 35 0a 2a 2a 0a 2a 2a 20 54 ndom()%5.**.** T
1fb5c 68 65 20 72 65 73 75 6c 74 20 6f 66 20 72 61 6e he result of ran
1fb5d 64 6f 6d 28 29 25 35 20 69 6e 20 74 68 65 20 47 dom()%5 in the G
1fb5e 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20 69 ROUP BY clause i
1fb5f 73 20 70 72 6f 62 61 62 6c 79 20 64 69 66 66 65 s probably diffe
1fb60 72 65 6e 74 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 rent.** from the
1fb61 20 72 65 73 75 6c 74 20 69 6e 20 74 68 65 20 72 result in the r
1fb62 65 73 75 6c 74 2d 73 65 74 2e 20 20 57 65 20 6d esult-set. We m
1fb63 69 67 68 74 20 66 69 78 20 74 68 69 73 20 73 6f ight fix this so
1fb64 6d 65 64 61 79 2e 20 20 4f 72 0a 2a 2a 20 74 68 meday. Or.** th
1fb65 65 6e 20 61 67 61 69 6e 2c 20 77 65 20 6d 69 67 en again, we mig
1fb66 68 74 20 6e 6f 74 2e 2e 2e 0a 2a 2f 0a 73 74 61 ht not....*/.sta
1fb67 74 69 63 20 76 6f 69 64 20 72 65 73 6f 6c 76 65 tic void resolve
1fb68 41 6c 69 61 73 28 0a 20 20 50 61 72 73 65 20 2a Alias(. Parse *
1fb69 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 pParse,
1fb6a 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 /* Parsing conte
1fb6b 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 xt */. ExprList
1fb6c 20 2a 70 45 4c 69 73 74 2c 20 20 20 20 20 20 2f *pEList, /
1fb6d 2a 20 41 20 72 65 73 75 6c 74 20 73 65 74 20 2a * A result set *
1fb6e 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 2c 20 20 20 /. int iCol,
1fb6f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 /* A
1fb70 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 72 65 column in the re
1fb71 73 75 6c 74 20 73 65 74 2e 20 20 30 2e 2e 70 45 sult set. 0..pE
1fb72 4c 69 73 74 2d 3e 6e 45 78 70 72 2d 31 20 2a 2f List->nExpr-1 */
1fb73 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 . Expr *pExpr,
1fb74 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 61 /* Tra
1fb75 6e 73 66 6f 72 6d 20 74 68 69 73 20 69 6e 74 6f nsform this into
1fb76 20 61 6e 20 61 6c 69 61 73 20 74 6f 20 74 68 65 an alias to the
1fb77 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 result set */.
1fb78 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 79 const char *zTy
1fb79 70 65 20 20 20 20 20 20 2f 2a 20 22 47 52 4f 55 pe /* "GROU
1fb7a 50 22 20 6f 72 20 22 4f 52 44 45 52 22 20 6f 72 P" or "ORDER" or
1fb7b 20 22 22 20 2a 2f 0a 29 7b 0a 20 20 45 78 70 72 "" */.){. Expr
1fb7c 20 2a 70 4f 72 69 67 3b 20 20 20 20 20 20 20 20 *pOrig;
1fb7d 20 20 20 2f 2a 20 54 68 65 20 69 43 6f 6c 2d 74 /* The iCol-t
1fb7e 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 h column of the
1fb7f 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20 result set */.
1fb80 45 78 70 72 20 2a 70 44 75 70 3b 20 20 20 20 20 Expr *pDup;
1fb81 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f /* Copy o
1fb82 66 20 70 4f 72 69 67 20 2a 2f 0a 20 20 73 71 6c f pOrig */. sql
1fb83 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 ite3 *db;
1fb84 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 /* The datab
1fb85 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a ase connection *
1fb86 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 69 43 6f /.. assert( iCo
1fb87 6c 3e 3d 30 20 26 26 20 69 43 6f 6c 3c 70 45 4c l>=0 && iCol<pEL
1fb88 69 73 74 2d 3e 6e 45 78 70 72 20 29 3b 0a 20 20 ist->nExpr );.
1fb89 70 4f 72 69 67 20 3d 20 70 45 4c 69 73 74 2d 3e pOrig = pEList->
1fb8a 61 5b 69 43 6f 6c 5d 2e 70 45 78 70 72 3b 0a 20 a[iCol].pExpr;.
1fb8b 20 61 73 73 65 72 74 28 20 70 4f 72 69 67 21 3d assert( pOrig!=
1fb8c 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 0 );. assert( p
1fb8d 4f 72 69 67 2d 3e 66 6c 61 67 73 20 26 20 45 50 Orig->flags & EP
1fb8e 5f 52 65 73 6f 6c 76 65 64 20 29 3b 0a 20 20 64 _Resolved );. d
1fb8f 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a b = pParse->db;.
1fb90 20 20 69 66 28 20 70 4f 72 69 67 2d 3e 6f 70 21 if( pOrig->op!
1fb91 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 26 26 20 7a 54 =TK_COLUMN && zT
1fb92 79 70 65 5b 30 5d 21 3d 27 47 27 20 29 7b 0a 20 ype[0]!='G' ){.
1fb93 20 20 20 70 44 75 70 20 3d 20 73 71 6c 69 74 65 pDup = sqlite
1fb94 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 4f 72 3ExprDup(db, pOr
1fb95 69 67 2c 20 30 29 3b 0a 20 20 20 20 70 44 75 70 ig, 0);. pDup
1fb96 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 = sqlite3PExpr(
1fb97 70 50 61 72 73 65 2c 20 54 4b 5f 41 53 2c 20 70 pParse, TK_AS, p
1fb98 44 75 70 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 Dup, 0, 0);.
1fb99 69 66 28 20 70 44 75 70 3d 3d 30 20 29 20 72 65 if( pDup==0 ) re
1fb9a 74 75 72 6e 3b 0a 20 20 20 20 69 66 28 20 70 45 turn;. if( pE
1fb9b 4c 69 73 74 2d 3e 61 5b 69 43 6f 6c 5d 2e 69 41 List->a[iCol].iA
1fb9c 6c 69 61 73 3d 3d 30 20 29 7b 0a 20 20 20 20 20 lias==0 ){.
1fb9d 20 70 45 4c 69 73 74 2d 3e 61 5b 69 43 6f 6c 5d pEList->a[iCol]
1fb9e 2e 69 41 6c 69 61 73 20 3d 20 28 75 31 36 29 28 .iAlias = (u16)(
1fb9f 2b 2b 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 ++pParse->nAlias
1fba0 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 44 75 );. }. pDu
1fba1 70 2d 3e 69 54 61 62 6c 65 20 3d 20 70 45 4c 69 p->iTable = pELi
1fba2 73 74 2d 3e 61 5b 69 43 6f 6c 5d 2e 69 41 6c 69 st->a[iCol].iAli
1fba3 61 73 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 as;. }else if(
1fba4 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 ExprHasProperty(
1fba5 70 4f 72 69 67 2c 20 45 50 5f 49 6e 74 56 61 6c pOrig, EP_IntVal
1fba6 75 65 29 20 7c 7c 20 70 4f 72 69 67 2d 3e 75 2e ue) || pOrig->u.
1fba7 7a 54 6f 6b 65 6e 3d 3d 30 20 29 7b 0a 20 20 20 zToken==0 ){.
1fba8 20 70 44 75 70 20 3d 20 73 71 6c 69 74 65 33 45 pDup = sqlite3E
1fba9 78 70 72 44 75 70 28 64 62 2c 20 70 4f 72 69 67 xprDup(db, pOrig
1fbaa 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 70 44 , 0);. if( pD
1fbab 75 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a up==0 ) return;.
1fbac 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 68 61 }else{. cha
1fbad 72 20 2a 7a 54 6f 6b 65 6e 20 3d 20 70 4f 72 69 r *zToken = pOri
1fbae 67 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 20 g->u.zToken;.
1fbaf 20 61 73 73 65 72 74 28 20 7a 54 6f 6b 65 6e 21 assert( zToken!
1fbb0 3d 30 20 29 3b 0a 20 20 20 20 70 4f 72 69 67 2d =0 );. pOrig-
1fbb1 3e 75 2e 7a 54 6f 6b 65 6e 20 3d 20 30 3b 0a 20 >u.zToken = 0;.
1fbb2 20 20 20 70 44 75 70 20 3d 20 73 71 6c 69 74 65 pDup = sqlite
1fbb3 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 4f 72 3ExprDup(db, pOr
1fbb4 69 67 2c 20 30 29 3b 0a 20 20 20 20 70 4f 72 69 ig, 0);. pOri
1fbb5 67 2d 3e 75 2e 7a 54 6f 6b 65 6e 20 3d 20 7a 54 g->u.zToken = zT
1fbb6 6f 6b 65 6e 3b 0a 20 20 20 20 69 66 28 20 70 44 oken;. if( pD
1fbb7 75 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a up==0 ) return;.
1fbb8 20 20 20 20 61 73 73 65 72 74 28 20 28 70 44 75 assert( (pDu
1fbb9 70 2d 3e 66 6c 61 67 73 20 26 20 28 45 50 5f 52 p->flags & (EP_R
1fbba 65 64 75 63 65 64 7c 45 50 5f 54 6f 6b 65 6e 4f educed|EP_TokenO
1fbbb 6e 6c 79 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20 nly))==0 );.
1fbbc 70 44 75 70 2d 3e 66 6c 61 67 73 32 20 7c 3d 20 pDup->flags2 |=
1fbbd 45 50 32 5f 4d 61 6c 6c 6f 63 65 64 54 6f 6b 65 EP2_MallocedToke
1fbbe 6e 3b 0a 20 20 20 20 70 44 75 70 2d 3e 75 2e 7a n;. pDup->u.z
1fbbf 54 6f 6b 65 6e 20 3d 20 73 71 6c 69 74 65 33 44 Token = sqlite3D
1fbc0 62 53 74 72 44 75 70 28 64 62 2c 20 7a 54 6f 6b bStrDup(db, zTok
1fbc1 65 6e 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 en);. }. if( p
1fbc2 45 78 70 72 2d 3e 66 6c 61 67 73 20 26 20 45 50 Expr->flags & EP
1fbc3 5f 45 78 70 43 6f 6c 6c 61 74 65 20 29 7b 0a 20 _ExpCollate ){.
1fbc4 20 20 20 70 44 75 70 2d 3e 70 43 6f 6c 6c 20 3d pDup->pColl =
1fbc5 20 70 45 78 70 72 2d 3e 70 43 6f 6c 6c 3b 0a 20 pExpr->pColl;.
1fbc6 20 20 20 70 44 75 70 2d 3e 66 6c 61 67 73 20 7c pDup->flags |
1fbc7 3d 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 3b = EP_ExpCollate;
1fbc8 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 45 78 . }. sqlite3Ex
1fbc9 70 72 43 6c 65 61 72 28 64 62 2c 20 70 45 78 70 prClear(db, pExp
1fbca 72 29 3b 0a 20 20 6d 65 6d 63 70 79 28 70 45 78 r);. memcpy(pEx
1fbcb 70 72 2c 20 70 44 75 70 2c 20 73 69 7a 65 6f 66 pr, pDup, sizeof
1fbcc 28 2a 70 45 78 70 72 29 29 3b 0a 20 20 73 71 6c (*pExpr));. sql
1fbcd 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 ite3DbFree(db, p
1fbce 44 75 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 Dup);.}../*.** G
1fbcf 69 76 65 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 iven the name of
1fbd0 20 61 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 a column of the
1fbd1 20 66 6f 72 6d 20 58 2e 59 2e 5a 20 6f 72 20 59 form X.Y.Z or Y
1fbd2 2e 5a 20 6f 72 20 6a 75 73 74 20 5a 2c 20 6c 6f .Z or just Z, lo
1fbd3 6f 6b 20 75 70 0a 2a 2a 20 74 68 61 74 20 6e 61 ok up.** that na
1fbd4 6d 65 20 69 6e 20 74 68 65 20 73 65 74 20 6f 66 me in the set of
1fbd5 20 73 6f 75 72 63 65 20 74 61 62 6c 65 73 20 69 source tables i
1fbd6 6e 20 70 53 72 63 4c 69 73 74 20 61 6e 64 20 6d n pSrcList and m
1fbd7 61 6b 65 20 74 68 65 20 70 45 78 70 72 20 0a 2a ake the pExpr .*
1fbd8 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 6e 6f 64 * expression nod
1fbd9 65 20 72 65 66 65 72 20 62 61 63 6b 20 74 6f 20 e refer back to
1fbda 74 68 61 74 20 73 6f 75 72 63 65 20 63 6f 6c 75 that source colu
1fbdb 6d 6e 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 mn. The followi
1fbdc 6e 67 20 63 68 61 6e 67 65 73 0a 2a 2a 20 61 72 ng changes.** ar
1fbdd 65 20 6d 61 64 65 20 74 6f 20 70 45 78 70 72 3a e made to pExpr:
1fbde 0a 2a 2a 0a 2a 2a 20 20 20 20 70 45 78 70 72 2d .**.** pExpr-
1fbdf 3e 69 44 62 20 20 20 20 20 20 20 20 20 20 20 53 >iDb S
1fbe0 65 74 20 74 68 65 20 69 6e 64 65 78 20 69 6e 20 et the index in
1fbe1 64 62 2d 3e 61 44 62 5b 5d 20 6f 66 20 74 68 65 db->aDb[] of the
1fbe2 20 64 61 74 61 62 61 73 65 20 58 0a 2a 2a 20 20 database X.**
1fbe3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fbe4 20 20 20 20 20 20 20 28 65 76 65 6e 20 69 66 20 (even if
1fbe5 58 20 69 73 20 69 6d 70 6c 69 65 64 29 2e 0a 2a X is implied)..*
1fbe6 2a 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 * pExpr->iTab
1fbe7 6c 65 20 20 20 20 20 20 20 20 53 65 74 20 74 6f le Set to
1fbe8 20 74 68 65 20 63 75 72 73 6f 72 20 6e 75 6d 62 the cursor numb
1fbe9 65 72 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 er for the table
1fbea 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 20 20 20 obtained.**
1fbeb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fbec 20 20 20 20 20 66 72 6f 6d 20 70 53 72 63 4c 69 from pSrcLi
1fbed 73 74 2e 0a 2a 2a 20 20 20 20 70 45 78 70 72 2d st..** pExpr-
1fbee 3e 70 54 61 62 20 20 20 20 20 20 20 20 20 20 50 >pTab P
1fbef 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 54 61 62 oints to the Tab
1fbf0 6c 65 20 73 74 72 75 63 74 75 72 65 20 6f 66 20 le structure of
1fbf1 58 2e 59 20 28 65 76 65 6e 20 69 66 0a 2a 2a 20 X.Y (even if.**
1fbf2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fbf3 20 20 20 20 20 20 20 20 58 20 61 6e 64 2f 6f 72 X and/or
1fbf4 20 59 20 61 72 65 20 69 6d 70 6c 69 65 64 2e 29 Y are implied.)
1fbf5 0a 2a 2a 20 20 20 20 70 45 78 70 72 2d 3e 69 43 .** pExpr->iC
1fbf6 6f 6c 75 6d 6e 20 20 20 20 20 20 20 53 65 74 20 olumn Set
1fbf7 74 6f 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 75 to the column nu
1fbf8 6d 62 65 72 20 77 69 74 68 69 6e 20 74 68 65 20 mber within the
1fbf9 74 61 62 6c 65 2e 0a 2a 2a 20 20 20 20 70 45 78 table..** pEx
1fbfa 70 72 2d 3e 6f 70 20 20 20 20 20 20 20 20 20 20 pr->op
1fbfb 20 20 53 65 74 20 74 6f 20 54 4b 5f 43 4f 4c 55 Set to TK_COLU
1fbfc 4d 4e 2e 0a 2a 2a 20 20 20 20 70 45 78 70 72 2d MN..** pExpr-
1fbfd 3e 70 4c 65 66 74 20 20 20 20 20 20 20 20 20 41 >pLeft A
1fbfe 6e 79 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 ny expression th
1fbff 69 73 20 70 6f 69 6e 74 73 20 74 6f 20 69 73 20 is points to is
1fc00 64 65 6c 65 74 65 64 0a 2a 2a 20 20 20 20 70 45 deleted.** pE
1fc01 78 70 72 2d 3e 70 52 69 67 68 74 20 20 20 20 20 xpr->pRight
1fc02 20 20 20 41 6e 79 20 65 78 70 72 65 73 73 69 6f Any expressio
1fc03 6e 20 74 68 69 73 20 70 6f 69 6e 74 73 20 74 6f n this points to
1fc04 20 69 73 20 64 65 6c 65 74 65 64 2e 0a 2a 2a 0a is deleted..**.
1fc05 2a 2a 20 54 68 65 20 7a 44 62 20 76 61 72 69 61 ** The zDb varia
1fc06 62 6c 65 20 69 73 20 74 68 65 20 6e 61 6d 65 20 ble is the name
1fc07 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
1fc08 28 74 68 65 20 22 58 22 29 2e 20 20 54 68 69 73 (the "X"). This
1fc09 20 76 61 6c 75 65 20 6d 61 79 20 62 65 0a 2a 2a value may be.**
1fc0a 20 4e 55 4c 4c 20 6d 65 61 6e 69 6e 67 20 74 68 NULL meaning th
1fc0b 61 74 20 6e 61 6d 65 20 69 73 20 6f 66 20 74 68 at name is of th
1fc0c 65 20 66 6f 72 6d 20 59 2e 5a 20 6f 72 20 5a 2e e form Y.Z or Z.
1fc0d 20 20 41 6e 79 20 61 76 61 69 6c 61 62 6c 65 20 Any available
1fc0e 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 61 6e 20 database.** can
1fc0f 62 65 20 75 73 65 64 2e 20 20 54 68 65 20 7a 54 be used. The zT
1fc10 61 62 6c 65 20 76 61 72 69 61 62 6c 65 20 69 73 able variable is
1fc11 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 the name of the
1fc12 20 74 61 62 6c 65 20 28 74 68 65 20 22 59 22 29 table (the "Y")
1fc13 2e 20 20 54 68 69 73 0a 2a 2a 20 76 61 6c 75 65 . This.** value
1fc14 20 63 61 6e 20 62 65 20 4e 55 4c 4c 20 69 66 20 can be NULL if
1fc15 7a 44 62 20 69 73 20 61 6c 73 6f 20 4e 55 4c 4c zDb is also NULL
1fc16 2e 20 20 49 66 20 7a 54 61 62 6c 65 20 69 73 20 . If zTable is
1fc17 4e 55 4c 4c 20 69 74 0a 2a 2a 20 6d 65 61 6e 73 NULL it.** means
1fc18 20 74 68 61 74 20 74 68 65 20 66 6f 72 6d 20 6f that the form o
1fc19 66 20 74 68 65 20 6e 61 6d 65 20 69 73 20 5a 20 f the name is Z
1fc1a 61 6e 64 20 74 68 61 74 20 63 6f 6c 75 6d 6e 73 and that columns
1fc1b 20 66 72 6f 6d 20 61 6e 79 20 74 61 62 6c 65 0a from any table.
1fc1c 2a 2a 20 63 61 6e 20 62 65 20 75 73 65 64 2e 0a ** can be used..
1fc1d 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e 61 6d **.** If the nam
1fc1e 65 20 63 61 6e 6e 6f 74 20 62 65 20 72 65 73 6f e cannot be reso
1fc1f 6c 76 65 64 20 75 6e 61 6d 62 69 67 75 6f 75 73 lved unambiguous
1fc20 6c 79 2c 20 6c 65 61 76 65 20 61 6e 20 65 72 72 ly, leave an err
1fc21 6f 72 20 6d 65 73 73 61 67 65 0a 2a 2a 20 69 6e or message.** in
1fc22 20 70 50 61 72 73 65 20 61 6e 64 20 72 65 74 75 pParse and retu
1fc23 72 6e 20 57 52 43 5f 41 62 6f 72 74 2e 20 20 52 rn WRC_Abort. R
1fc24 65 74 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 20 eturn WRC_Prune
1fc25 6f 6e 20 73 75 63 63 65 73 73 2e 0a 2a 2f 0a 73 on success..*/.s
1fc26 74 61 74 69 63 20 69 6e 74 20 6c 6f 6f 6b 75 70 tatic int lookup
1fc27 4e 61 6d 65 28 0a 20 20 50 61 72 73 65 20 2a 70 Name(. Parse *p
1fc28 50 61 72 73 65 2c 20 20 20 20 20 20 20 2f 2a 20 Parse, /*
1fc29 54 68 65 20 70 61 72 73 69 6e 67 20 63 6f 6e 74 The parsing cont
1fc2a 65 78 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 ext */. const c
1fc2b 68 61 72 20 2a 7a 44 62 2c 20 20 20 20 20 2f 2a har *zDb, /*
1fc2c 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 Name of the dat
1fc2d 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 abase containing
1fc2e 20 74 61 62 6c 65 2c 20 6f 72 20 4e 55 4c 4c 20 table, or NULL
1fc2f 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
1fc30 2a 7a 54 61 62 2c 20 20 20 20 2f 2a 20 4e 61 6d *zTab, /* Nam
1fc31 65 20 6f 66 20 74 61 62 6c 65 20 63 6f 6e 74 61 e of table conta
1fc32 69 6e 69 6e 67 20 63 6f 6c 75 6d 6e 2c 20 6f 72 ining column, or
1fc33 20 4e 55 4c 4c 20 2a 2f 0a 20 20 63 6f 6e 73 74 NULL */. const
1fc34 20 63 68 61 72 20 2a 7a 43 6f 6c 2c 20 20 20 20 char *zCol,
1fc35 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 63 /* Name of the c
1fc36 6f 6c 75 6d 6e 2e 20 2a 2f 0a 20 20 4e 61 6d 65 olumn. */. Name
1fc37 43 6f 6e 74 65 78 74 20 2a 70 4e 43 2c 20 20 20 Context *pNC,
1fc38 20 2f 2a 20 54 68 65 20 6e 61 6d 65 20 63 6f 6e /* The name con
1fc39 74 65 78 74 20 75 73 65 64 20 74 6f 20 72 65 73 text used to res
1fc3a 6f 6c 76 65 20 74 68 65 20 6e 61 6d 65 20 2a 2f olve the name */
1fc3b 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 20 20 . Expr *pExpr
1fc3c 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 6b 65 20 /* Make
1fc3d 74 68 69 73 20 45 58 50 52 20 6e 6f 64 65 20 70 this EXPR node p
1fc3e 6f 69 6e 74 20 74 6f 20 74 68 65 20 73 65 6c 65 oint to the sele
1fc3f 63 74 65 64 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 29 cted column */.)
1fc40 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 20 20 20 {. int i, j;
1fc41 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 /* Loop
1fc42 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20 69 counters */. i
1fc43 6e 74 20 63 6e 74 20 3d 20 30 3b 20 20 20 20 20 nt cnt = 0;
1fc44 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fc45 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 61 /* Number of ma
1fc46 74 63 68 69 6e 67 20 63 6f 6c 75 6d 6e 20 6e 61 tching column na
1fc47 6d 65 73 20 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 mes */. int cnt
1fc48 54 61 62 20 3d 20 30 3b 20 20 20 20 20 20 20 20 Tab = 0;
1fc49 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
1fc4a 6d 62 65 72 20 6f 66 20 6d 61 74 63 68 69 6e 67 mber of matching
1fc4b 20 74 61 62 6c 65 20 6e 61 6d 65 73 20 2a 2f 0a table names */.
1fc4c 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 sqlite3 *db =
1fc4d 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 20 20 20 pParse->db;
1fc4e 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 /* The datab
1fc4f 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a ase connection *
1fc50 2f 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 /. struct SrcLi
1fc51 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 20 st_item *pItem;
1fc52 20 20 20 20 20 20 2f 2a 20 55 73 65 20 66 6f 72 /* Use for
1fc53 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 70 53 looping over pS
1fc54 72 63 4c 69 73 74 20 69 74 65 6d 73 20 2a 2f 0a rcList items */.
1fc55 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 struct SrcList
1fc56 5f 69 74 65 6d 20 2a 70 4d 61 74 63 68 20 3d 20 _item *pMatch =
1fc57 30 3b 20 20 2f 2a 20 54 68 65 20 6d 61 74 63 68 0; /* The match
1fc58 69 6e 67 20 70 53 72 63 4c 69 73 74 20 69 74 65 ing pSrcList ite
1fc59 6d 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 m */. NameConte
1fc5a 78 74 20 2a 70 54 6f 70 4e 43 20 3d 20 70 4e 43 xt *pTopNC = pNC
1fc5b 3b 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 ; /* Firs
1fc5c 74 20 6e 61 6d 65 63 6f 6e 74 65 78 74 20 69 6e t namecontext in
1fc5d 20 74 68 65 20 6c 69 73 74 20 2a 2f 0a 20 20 53 the list */. S
1fc5e 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 20 3d chema *pSchema =
1fc5f 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 0;
1fc60 20 2f 2a 20 53 63 68 65 6d 61 20 6f 66 20 74 68 /* Schema of th
1fc61 65 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a e expression */.
1fc62 20 20 69 6e 74 20 69 73 54 72 69 67 67 65 72 20 int isTrigger
1fc63 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 = 0;.. assert(
1fc64 70 4e 43 20 29 3b 20 20 20 20 20 2f 2a 20 74 68 pNC ); /* th
1fc65 65 20 6e 61 6d 65 20 63 6f 6e 74 65 78 74 20 63 e name context c
1fc66 61 6e 6e 6f 74 20 62 65 20 4e 55 4c 4c 2e 20 2a annot be NULL. *
1fc67 2f 0a 20 20 61 73 73 65 72 74 28 20 7a 43 6f 6c /. assert( zCol
1fc68 20 29 3b 20 20 20 20 2f 2a 20 54 68 65 20 5a 20 ); /* The Z
1fc69 69 6e 20 58 2e 59 2e 5a 20 63 61 6e 6e 6f 74 20 in X.Y.Z cannot
1fc6a 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 61 73 73 be NULL */. ass
1fc6b 65 72 74 28 20 7e 45 78 70 72 48 61 73 41 6e 79 ert( ~ExprHasAny
1fc6c 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 Property(pExpr,
1fc6d 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c 45 50 5f EP_TokenOnly|EP_
1fc6e 52 65 64 75 63 65 64 29 20 29 3b 0a 0a 20 20 2f Reduced) );.. /
1fc6f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 * Initialize the
1fc70 20 6e 6f 64 65 20 74 6f 20 6e 6f 2d 6d 61 74 63 node to no-matc
1fc71 68 20 2a 2f 0a 20 20 70 45 78 70 72 2d 3e 69 54 h */. pExpr->iT
1fc72 61 62 6c 65 20 3d 20 2d 31 3b 0a 20 20 70 45 78 able = -1;. pEx
1fc73 70 72 2d 3e 70 54 61 62 20 3d 20 30 3b 0a 20 20 pr->pTab = 0;.
1fc74 45 78 70 72 53 65 74 49 72 72 65 64 75 63 69 62 ExprSetIrreducib
1fc75 6c 65 28 70 45 78 70 72 29 3b 0a 0a 20 20 2f 2a le(pExpr);.. /*
1fc76 20 53 74 61 72 74 20 61 74 20 74 68 65 20 69 6e Start at the in
1fc77 6e 65 72 2d 6d 6f 73 74 20 63 6f 6e 74 65 78 74 ner-most context
1fc78 20 61 6e 64 20 6d 6f 76 65 20 6f 75 74 77 61 72 and move outwar
1fc79 64 20 75 6e 74 69 6c 20 61 20 6d 61 74 63 68 20 d until a match
1fc7a 69 73 20 66 6f 75 6e 64 20 2a 2f 0a 20 20 77 68 is found */. wh
1fc7b 69 6c 65 28 20 70 4e 43 20 26 26 20 63 6e 74 3d ile( pNC && cnt=
1fc7c 3d 30 20 29 7b 0a 20 20 20 20 45 78 70 72 4c 69 =0 ){. ExprLi
1fc7d 73 74 20 2a 70 45 4c 69 73 74 3b 0a 20 20 20 20 st *pEList;.
1fc7e 53 72 63 4c 69 73 74 20 2a 70 53 72 63 4c 69 73 SrcList *pSrcLis
1fc7f 74 20 3d 20 70 4e 43 2d 3e 70 53 72 63 4c 69 73 t = pNC->pSrcLis
1fc80 74 3b 0a 0a 20 20 20 20 69 66 28 20 70 53 72 63 t;.. if( pSrc
1fc81 4c 69 73 74 20 29 7b 0a 20 20 20 20 20 20 66 6f List ){. fo
1fc82 72 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70 53 72 r(i=0, pItem=pSr
1fc83 63 4c 69 73 74 2d 3e 61 3b 20 69 3c 70 53 72 63 cList->a; i<pSrc
1fc84 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c List->nSrc; i++,
1fc85 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 pItem++){.
1fc86 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a Table *pTab;.
1fc87 20 20 20 20 20 20 20 20 69 6e 74 20 69 44 62 3b int iDb;
1fc88 0a 20 20 20 20 20 20 20 20 43 6f 6c 75 6d 6e 20 . Column
1fc89 2a 70 43 6f 6c 3b 0a 20 20 0a 20 20 20 20 20 20 *pCol;. .
1fc8a 20 20 70 54 61 62 20 3d 20 70 49 74 65 6d 2d 3e pTab = pItem->
1fc8b 70 54 61 62 3b 0a 20 20 20 20 20 20 20 20 61 73 pTab;. as
1fc8c 73 65 72 74 28 20 70 54 61 62 21 3d 30 20 26 26 sert( pTab!=0 &&
1fc8d 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 21 3d 30 20 pTab->zName!=0
1fc8e 29 3b 0a 20 20 20 20 20 20 20 20 69 44 62 20 3d );. iDb =
1fc8f 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f sqlite3SchemaTo
1fc90 49 6e 64 65 78 28 64 62 2c 20 70 54 61 62 2d 3e Index(db, pTab->
1fc91 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 20 20 pSchema);.
1fc92 20 20 61 73 73 65 72 74 28 20 70 54 61 62 2d 3e assert( pTab->
1fc93 6e 43 6f 6c 3e 30 20 29 3b 0a 20 20 20 20 20 20 nCol>0 );.
1fc94 20 20 69 66 28 20 7a 54 61 62 20 29 7b 0a 20 20 if( zTab ){.
1fc95 20 20 20 20 20 20 20 20 69 66 28 20 70 49 74 65 if( pIte
1fc96 6d 2d 3e 7a 41 6c 69 61 73 20 29 7b 0a 20 20 20 m->zAlias ){.
1fc97 20 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a char *z
1fc98 54 61 62 4e 61 6d 65 20 3d 20 70 49 74 65 6d 2d TabName = pItem-
1fc99 3e 7a 41 6c 69 61 73 3b 0a 20 20 20 20 20 20 20 >zAlias;.
1fc9a 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 if( sqlite3
1fc9b 53 74 72 49 43 6d 70 28 7a 54 61 62 4e 61 6d 65 StrICmp(zTabName
1fc9c 2c 20 7a 54 61 62 29 21 3d 30 20 29 20 63 6f 6e , zTab)!=0 ) con
1fc9d 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 20 tinue;.
1fc9e 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
1fc9f 20 20 20 20 63 68 61 72 20 2a 7a 54 61 62 4e 61 char *zTabNa
1fca0 6d 65 20 3d 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 me = pTab->zName
1fca1 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 ;. if
1fca2 28 20 4e 45 56 45 52 28 7a 54 61 62 4e 61 6d 65 ( NEVER(zTabName
1fca3 3d 3d 30 29 20 7c 7c 20 73 71 6c 69 74 65 33 53 ==0) || sqlite3S
1fca4 74 72 49 43 6d 70 28 7a 54 61 62 4e 61 6d 65 2c trICmp(zTabName,
1fca5 20 7a 54 61 62 29 21 3d 30 20 29 7b 0a 20 20 20 zTab)!=0 ){.
1fca6 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 69 conti
1fca7 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 20 20 20 nue;.
1fca8 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 }. i
1fca9 66 28 20 7a 44 62 21 3d 30 20 26 26 20 73 71 6c f( zDb!=0 && sql
1fcaa 69 74 65 33 53 74 72 49 43 6d 70 28 64 62 2d 3e ite3StrICmp(db->
1fcab 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 aDb[iDb].zName,
1fcac 7a 44 62 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 zDb)!=0 ){.
1fcad 20 20 20 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 continu
1fcae 65 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d e;. }
1fcaf 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 . }.
1fcb0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 }. i
1fcb1 66 28 20 30 3d 3d 28 63 6e 74 54 61 62 2b 2b 29 f( 0==(cntTab++)
1fcb2 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 45 ){. pE
1fcb3 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 70 49 xpr->iTable = pI
1fcb4 74 65 6d 2d 3e 69 43 75 72 73 6f 72 3b 0a 20 20 tem->iCursor;.
1fcb5 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 70 pExpr->p
1fcb6 54 61 62 20 3d 20 70 54 61 62 3b 0a 20 20 20 20 Tab = pTab;.
1fcb7 20 20 20 20 20 20 70 53 63 68 65 6d 61 20 3d 20 pSchema =
1fcb8 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 3b 0a 20 pTab->pSchema;.
1fcb9 20 20 20 20 20 20 20 20 20 70 4d 61 74 63 68 20 pMatch
1fcba 3d 20 70 49 74 65 6d 3b 0a 20 20 20 20 20 20 20 = pItem;.
1fcbb 20 7d 0a 20 20 20 20 20 20 20 20 66 6f 72 28 6a }. for(j
1fcbc 3d 30 2c 20 70 43 6f 6c 3d 70 54 61 62 2d 3e 61 =0, pCol=pTab->a
1fcbd 43 6f 6c 3b 20 6a 3c 70 54 61 62 2d 3e 6e 43 6f Col; j<pTab->nCo
1fcbe 6c 3b 20 6a 2b 2b 2c 20 70 43 6f 6c 2b 2b 29 7b l; j++, pCol++){
1fcbf 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 73 . if( s
1fcc0 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 43 qlite3StrICmp(pC
1fcc1 6f 6c 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 29 ol->zName, zCol)
1fcc2 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ==0 ){.
1fcc3 20 20 20 49 64 4c 69 73 74 20 2a 70 55 73 69 6e IdList *pUsin
1fcc4 67 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 63 g;. c
1fcc5 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 nt++;.
1fcc6 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 pExpr->iTable
1fcc7 3d 20 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 = pItem->iCursor
1fcc8 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 45 ;. pE
1fcc9 78 70 72 2d 3e 70 54 61 62 20 3d 20 70 54 61 62 xpr->pTab = pTab
1fcca 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 4d ;. pM
1fccb 61 74 63 68 20 3d 20 70 49 74 65 6d 3b 0a 20 20 atch = pItem;.
1fccc 20 20 20 20 20 20 20 20 20 20 70 53 63 68 65 6d pSchem
1fccd 61 20 3d 20 70 54 61 62 2d 3e 70 53 63 68 65 6d a = pTab->pSchem
1fcce 61 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f a;. /
1fccf 2a 20 53 75 62 73 74 69 74 75 74 65 20 74 68 65 * Substitute the
1fcd0 20 72 6f 77 69 64 20 28 63 6f 6c 75 6d 6e 20 2d rowid (column -
1fcd1 31 29 20 66 6f 72 20 74 68 65 20 49 4e 54 45 47 1) for the INTEG
1fcd2 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20 2a ER PRIMARY KEY *
1fcd3 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 45 /. pE
1fcd4 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 6a xpr->iColumn = j
1fcd5 3d 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20 3f 20 ==pTab->iPKey ?
1fcd6 2d 31 20 3a 20 28 69 31 36 29 6a 3b 0a 20 20 20 -1 : (i16)j;.
1fcd7 20 20 20 20 20 20 20 20 20 69 66 28 20 69 3c 70 if( i<p
1fcd8 53 72 63 4c 69 73 74 2d 3e 6e 53 72 63 2d 31 20 SrcList->nSrc-1
1fcd9 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ){.
1fcda 20 69 66 28 20 70 49 74 65 6d 5b 31 5d 2e 6a 6f if( pItem[1].jo
1fcdb 69 6e 74 79 70 65 20 26 20 4a 54 5f 4e 41 54 55 intype & JT_NATU
1fcdc 52 41 4c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 RAL ){.
1fcdd 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 69 /* If thi
1fcde 73 20 6d 61 74 63 68 20 6f 63 63 75 72 72 65 64 s match occurred
1fcdf 20 69 6e 20 74 68 65 20 6c 65 66 74 20 74 61 62 in the left tab
1fce0 6c 65 20 6f 66 20 61 20 6e 61 74 75 72 61 6c 20 le of a natural
1fce1 6a 6f 69 6e 2c 0a 20 20 20 20 20 20 20 20 20 20 join,.
1fce2 20 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 73 6b ** then sk
1fce3 69 70 20 74 68 65 20 72 69 67 68 74 20 74 61 62 ip the right tab
1fce4 6c 65 20 74 6f 20 61 76 6f 69 64 20 61 20 64 75 le to avoid a du
1fce5 70 6c 69 63 61 74 65 20 6d 61 74 63 68 20 2a 2f plicate match */
1fce6 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1fce7 20 70 49 74 65 6d 2b 2b 3b 0a 20 20 20 20 20 20 pItem++;.
1fce8 20 20 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a 20 i++;.
1fce9 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c }el
1fcea 73 65 20 69 66 28 20 28 70 55 73 69 6e 67 20 3d se if( (pUsing =
1fceb 20 70 49 74 65 6d 5b 31 5d 2e 70 55 73 69 6e 67 pItem[1].pUsing
1fcec 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 )!=0 ){.
1fced 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 /* If th
1fcee 69 73 20 6d 61 74 63 68 20 6f 63 63 75 72 73 20 is match occurs
1fcef 6f 6e 20 61 20 63 6f 6c 75 6d 6e 20 74 68 61 74 on a column that
1fcf0 20 69 73 20 69 6e 20 74 68 65 20 55 53 49 4e 47 is in the USING
1fcf1 20 63 6c 61 75 73 65 0a 20 20 20 20 20 20 20 20 clause.
1fcf2 20 20 20 20 20 20 20 20 2a 2a 20 6f 66 20 61 20 ** of a
1fcf3 6a 6f 69 6e 2c 20 73 6b 69 70 20 74 68 65 20 73 join, skip the s
1fcf4 65 61 72 63 68 20 6f 66 20 74 68 65 20 72 69 67 earch of the rig
1fcf5 68 74 20 74 61 62 6c 65 20 6f 66 20 74 68 65 20 ht table of the
1fcf6 6a 6f 69 6e 0a 20 20 20 20 20 20 20 20 20 20 20 join.
1fcf7 20 20 20 20 20 2a 2a 20 74 6f 20 61 76 6f 69 64 ** to avoid
1fcf8 20 61 20 64 75 70 6c 69 63 61 74 65 20 6d 61 74 a duplicate mat
1fcf9 63 68 20 74 68 65 72 65 2e 20 2a 2f 0a 20 20 20 ch there. */.
1fcfa 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 int
1fcfb 20 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 k;.
1fcfc 20 20 20 20 66 6f 72 28 6b 3d 30 3b 20 6b 3c 70 for(k=0; k<p
1fcfd 55 73 69 6e 67 2d 3e 6e 49 64 3b 20 6b 2b 2b 29 Using->nId; k++)
1fcfe 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {.
1fcff 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 if( sqlite3S
1fd00 74 72 49 43 6d 70 28 70 55 73 69 6e 67 2d 3e 61 trICmp(pUsing->a
1fd01 5b 6b 5d 2e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 29 [k].zName, zCol)
1fd02 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ==0 ){.
1fd03 20 20 20 20 20 20 20 20 20 20 20 70 49 74 65 6d pItem
1fd04 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ++;.
1fd05 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 i++;.
1fd06 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fd07 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 break;.
1fd08 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
1fd09 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 }.
1fd0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 }.
1fd0b 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
1fd0c 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
1fd0d 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
1fd0e 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
1fd0f 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 }..#ifndef SQLI
1fd10 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a TE_OMIT_TRIGGER.
1fd11 20 20 20 20 2f 2a 20 49 66 20 77 65 20 68 61 76 /* If we hav
1fd12 65 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 72 65 e not already re
1fd13 73 6f 6c 76 65 64 20 74 68 65 20 6e 61 6d 65 2c solved the name,
1fd14 20 74 68 65 6e 20 6d 61 79 62 65 20 0a 20 20 20 then maybe .
1fd15 20 2a 2a 20 69 74 20 69 73 20 61 20 6e 65 77 2e ** it is a new.
1fd16 2a 20 6f 72 20 6f 6c 64 2e 2a 20 74 72 69 67 67 * or old.* trigg
1fd17 65 72 20 61 72 67 75 6d 65 6e 74 20 72 65 66 65 er argument refe
1fd18 72 65 6e 63 65 0a 20 20 20 20 2a 2f 0a 20 20 20 rence. */.
1fd19 20 69 66 28 20 7a 44 62 3d 3d 30 20 26 26 20 7a if( zDb==0 && z
1fd1a 54 61 62 21 3d 30 20 26 26 20 63 6e 74 3d 3d 30 Tab!=0 && cnt==0
1fd1b 20 26 26 20 70 50 61 72 73 65 2d 3e 70 54 72 69 && pParse->pTri
1fd1c 67 67 65 72 54 61 62 21 3d 30 20 29 7b 0a 20 20 ggerTab!=0 ){.
1fd1d 20 20 20 20 69 6e 74 20 6f 70 20 3d 20 70 50 61 int op = pPa
1fd1e 72 73 65 2d 3e 65 54 72 69 67 67 65 72 4f 70 3b rse->eTriggerOp;
1fd1f 0a 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70 54 . Table *pT
1fd20 61 62 20 3d 20 30 3b 0a 20 20 20 20 20 20 61 73 ab = 0;. as
1fd21 73 65 72 74 28 20 6f 70 3d 3d 54 4b 5f 44 45 4c sert( op==TK_DEL
1fd22 45 54 45 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 55 50 ETE || op==TK_UP
1fd23 44 41 54 45 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 49 DATE || op==TK_I
1fd24 4e 53 45 52 54 20 29 3b 0a 20 20 20 20 20 20 69 NSERT );. i
1fd25 66 28 20 6f 70 21 3d 54 4b 5f 44 45 4c 45 54 45 f( op!=TK_DELETE
1fd26 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 49 43 && sqlite3StrIC
1fd27 6d 70 28 22 6e 65 77 22 2c 7a 54 61 62 29 20 3d mp("new",zTab) =
1fd28 3d 20 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 = 0 ){. p
1fd29 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 31 Expr->iTable = 1
1fd2a 3b 0a 20 20 20 20 20 20 20 20 70 54 61 62 20 3d ;. pTab =
1fd2b 20 70 50 61 72 73 65 2d 3e 70 54 72 69 67 67 65 pParse->pTrigge
1fd2c 72 54 61 62 3b 0a 20 20 20 20 20 20 7d 65 6c 73 rTab;. }els
1fd2d 65 20 69 66 28 20 6f 70 21 3d 54 4b 5f 49 4e 53 e if( op!=TK_INS
1fd2e 45 52 54 20 26 26 20 73 71 6c 69 74 65 33 53 74 ERT && sqlite3St
1fd2f 72 49 43 6d 70 28 22 6f 6c 64 22 2c 7a 54 61 62 rICmp("old",zTab
1fd30 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 )==0 ){.
1fd31 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 pExpr->iTable =
1fd32 30 3b 0a 20 20 20 20 20 20 20 20 70 54 61 62 20 0;. pTab
1fd33 3d 20 70 50 61 72 73 65 2d 3e 70 54 72 69 67 67 = pParse->pTrigg
1fd34 65 72 54 61 62 3b 0a 20 20 20 20 20 20 7d 0a 0a erTab;. }..
1fd35 20 20 20 20 20 20 69 66 28 20 70 54 61 62 20 29 if( pTab )
1fd36 7b 20 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 { . int i
1fd37 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 70 53 63 Col;. pSc
1fd38 68 65 6d 61 20 3d 20 70 54 61 62 2d 3e 70 53 63 hema = pTab->pSc
1fd39 68 65 6d 61 3b 0a 20 20 20 20 20 20 20 20 63 6e hema;. cn
1fd3a 74 54 61 62 2b 2b 3b 0a 20 20 20 20 20 20 20 20 tTab++;.
1fd3b 69 66 28 20 73 71 6c 69 74 65 33 49 73 52 6f 77 if( sqlite3IsRow
1fd3c 69 64 28 7a 43 6f 6c 29 20 29 7b 0a 20 20 20 20 id(zCol) ){.
1fd3d 20 20 20 20 20 20 69 43 6f 6c 20 3d 20 2d 31 3b iCol = -1;
1fd3e 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a . }else{.
1fd3f 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 69 43 for(iC
1fd40 6f 6c 3d 30 3b 20 69 43 6f 6c 3c 70 54 61 62 2d ol=0; iCol<pTab-
1fd41 3e 6e 43 6f 6c 3b 20 69 43 6f 6c 2b 2b 29 7b 0a >nCol; iCol++){.
1fd42 20 20 20 20 20 20 20 20 20 20 20 20 43 6f 6c 75 Colu
1fd43 6d 6e 20 2a 70 43 6f 6c 20 3d 20 26 70 54 61 62 mn *pCol = &pTab
1fd44 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 3b 0a 20 20 ->aCol[iCol];.
1fd45 20 20 20 20 20 20 20 20 20 20 69 66 28 20 73 71 if( sq
1fd46 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 43 6f lite3StrICmp(pCo
1fd47 6c 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 29 3d l->zName, zCol)=
1fd48 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 =0 ){.
1fd49 20 20 20 20 69 66 28 20 69 43 6f 6c 3d 3d 70 54 if( iCol==pT
1fd4a 61 62 2d 3e 69 50 4b 65 79 20 29 7b 0a 20 20 20 ab->iPKey ){.
1fd4b 20 20 20 20 20 20 20 20 20 20 20 20 20 69 43 6f iCo
1fd4c 6c 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 20 20 l = -1;.
1fd4d 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
1fd4e 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
1fd4f 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
1fd50 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d }. }
1fd51 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 43 6f . if( iCo
1fd52 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 7b 0a l<pTab->nCol ){.
1fd53 20 20 20 20 20 20 20 20 20 20 63 6e 74 2b 2b 3b cnt++;
1fd54 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 69 . if( i
1fd55 43 6f 6c 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 Col<0 ){.
1fd56 20 20 20 20 20 70 45 78 70 72 2d 3e 61 66 66 69 pExpr->affi
1fd57 6e 69 74 79 20 3d 20 53 51 4c 49 54 45 5f 41 46 nity = SQLITE_AF
1fd58 46 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 20 20 F_INTEGER;.
1fd59 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 }else if( p
1fd5a 45 78 70 72 2d 3e 69 54 61 62 6c 65 3d 3d 30 20 Expr->iTable==0
1fd5b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 74 ){. t
1fd5c 65 73 74 63 61 73 65 28 20 69 43 6f 6c 3d 3d 33 estcase( iCol==3
1fd5d 31 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 1 );.
1fd5e 20 74 65 73 74 63 61 73 65 28 20 69 43 6f 6c 3d testcase( iCol=
1fd5f 3d 33 32 20 29 3b 0a 20 20 20 20 20 20 20 20 20 =32 );.
1fd60 20 20 20 70 50 61 72 73 65 2d 3e 6f 6c 64 6d 61 pParse->oldma
1fd61 73 6b 20 7c 3d 20 28 69 43 6f 6c 3e 3d 33 32 20 sk |= (iCol>=32
1fd62 3f 20 30 78 66 66 66 66 66 66 66 66 20 3a 20 28 ? 0xffffffff : (
1fd63 28 28 75 33 32 29 31 29 3c 3c 69 43 6f 6c 29 29 ((u32)1)<<iCol))
1fd64 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
1fd65 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 pExpr->i
1fd66 43 6f 6c 75 6d 6e 20 3d 20 28 69 31 36 29 69 43 Column = (i16)iC
1fd67 6f 6c 3b 0a 20 20 20 20 20 20 20 20 20 20 70 45 ol;. pE
1fd68 78 70 72 2d 3e 70 54 61 62 20 3d 20 70 54 61 62 xpr->pTab = pTab
1fd69 3b 0a 20 20 20 20 20 20 20 20 20 20 69 73 54 72 ;. isTr
1fd6a 69 67 67 65 72 20 3d 20 31 3b 0a 20 20 20 20 20 igger = 1;.
1fd6b 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
1fd6c 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 }.#endif /* !de
1fd6d 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 fined(SQLITE_OMI
1fd6e 54 5f 54 52 49 47 47 45 52 29 20 2a 2f 0a 0a 20 T_TRIGGER) */..
1fd6f 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 50 65 72 /*. ** Per
1fd70 68 61 70 73 20 74 68 65 20 6e 61 6d 65 20 69 73 haps the name is
1fd71 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 a reference to
1fd72 74 68 65 20 52 4f 57 49 44 0a 20 20 20 20 2a 2f the ROWID. */
1fd73 0a 20 20 20 20 69 66 28 20 63 6e 74 3d 3d 30 20 . if( cnt==0
1fd74 26 26 20 63 6e 74 54 61 62 3d 3d 31 20 26 26 20 && cntTab==1 &&
1fd75 73 71 6c 69 74 65 33 49 73 52 6f 77 69 64 28 7a sqlite3IsRowid(z
1fd76 43 6f 6c 29 20 29 7b 0a 20 20 20 20 20 20 63 6e Col) ){. cn
1fd77 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 45 78 t = 1;. pEx
1fd78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 2d 31 pr->iColumn = -1
1fd79 3b 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 61 ;. pExpr->a
1fd7a 66 66 69 6e 69 74 79 20 3d 20 53 51 4c 49 54 45 ffinity = SQLITE
1fd7b 5f 41 46 46 5f 49 4e 54 45 47 45 52 3b 0a 20 20 _AFF_INTEGER;.
1fd7c 20 20 7d 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20 }.. /*.
1fd7d 2a 2a 20 49 66 20 74 68 65 20 69 6e 70 75 74 20 ** If the input
1fd7e 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 5a is of the form Z
1fd7f 20 28 6e 6f 74 20 59 2e 5a 20 6f 72 20 58 2e 59 (not Y.Z or X.Y
1fd80 2e 5a 29 20 74 68 65 6e 20 74 68 65 20 6e 61 6d .Z) then the nam
1fd81 65 20 5a 0a 20 20 20 20 2a 2a 20 6d 69 67 68 74 e Z. ** might
1fd82 20 72 65 66 65 72 20 74 6f 20 61 6e 20 72 65 73 refer to an res
1fd83 75 6c 74 2d 73 65 74 20 61 6c 69 61 73 2e 20 20 ult-set alias.
1fd84 54 68 69 73 20 68 61 70 70 65 6e 73 2c 20 66 6f This happens, fo
1fd85 72 20 65 78 61 6d 70 6c 65 2c 20 77 68 65 6e 0a r example, when.
1fd86 20 20 20 20 2a 2a 20 77 65 20 61 72 65 20 72 65 ** we are re
1fd87 73 6f 6c 76 69 6e 67 20 6e 61 6d 65 73 20 69 6e solving names in
1fd88 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 the WHERE claus
1fd89 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 e of the followi
1fd8a 6e 67 20 63 6f 6d 6d 61 6e 64 3a 0a 20 20 20 20 ng command:.
1fd8b 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 53 45 **. ** SE
1fd8c 4c 45 43 54 20 61 2b 62 20 41 53 20 78 20 46 52 LECT a+b AS x FR
1fd8d 4f 4d 20 74 61 62 6c 65 20 57 48 45 52 45 20 78 OM table WHERE x
1fd8e 3c 31 30 3b 0a 20 20 20 20 2a 2a 0a 20 20 20 20 <10;. **.
1fd8f 2a 2a 20 49 6e 20 63 61 73 65 73 20 6c 69 6b 65 ** In cases like
1fd90 20 74 68 69 73 2c 20 72 65 70 6c 61 63 65 20 70 this, replace p
1fd91 45 78 70 72 20 77 69 74 68 20 61 20 63 6f 70 79 Expr with a copy
1fd92 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69 of the expressi
1fd93 6f 6e 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 66 on that. ** f
1fd94 6f 72 6d 73 20 74 68 65 20 72 65 73 75 6c 74 20 orms the result
1fd95 73 65 74 20 65 6e 74 72 79 20 28 22 61 2b 62 22 set entry ("a+b"
1fd96 20 69 6e 20 74 68 65 20 65 78 61 6d 70 6c 65 29 in the example)
1fd97 20 61 6e 64 20 72 65 74 75 72 6e 20 69 6d 6d 65 and return imme
1fd98 64 69 61 74 65 6c 79 2e 0a 20 20 20 20 2a 2a 20 diately.. **
1fd99 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 65 78 Note that the ex
1fd9a 70 72 65 73 73 69 6f 6e 20 69 6e 20 74 68 65 20 pression in the
1fd9b 72 65 73 75 6c 74 20 73 65 74 20 73 68 6f 75 6c result set shoul
1fd9c 64 20 68 61 76 65 20 61 6c 72 65 61 64 79 20 62 d have already b
1fd9d 65 65 6e 0a 20 20 20 20 2a 2a 20 72 65 73 6f 6c een. ** resol
1fd9e 76 65 64 20 62 79 20 74 68 65 20 74 69 6d 65 20 ved by the time
1fd9f 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 the WHERE clause
1fda0 20 69 73 20 72 65 73 6f 6c 76 65 64 2e 0a 20 20 is resolved..
1fda1 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 63 6e 74 */. if( cnt
1fda2 3d 3d 30 20 26 26 20 28 70 45 4c 69 73 74 20 3d ==0 && (pEList =
1fda3 20 70 4e 43 2d 3e 70 45 4c 69 73 74 29 21 3d 30 pNC->pEList)!=0
1fda4 20 26 26 20 7a 54 61 62 3d 3d 30 20 29 7b 0a 20 && zTab==0 ){.
1fda5 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c for(j=0; j<
1fda6 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 6a pEList->nExpr; j
1fda7 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 63 68 61 ++){. cha
1fda8 72 20 2a 7a 41 73 20 3d 20 70 45 4c 69 73 74 2d r *zAs = pEList-
1fda9 3e 61 5b 6a 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 >a[j].zName;.
1fdaa 20 20 20 20 20 69 66 28 20 7a 41 73 21 3d 30 20 if( zAs!=0
1fdab 26 26 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d && sqlite3StrICm
1fdac 70 28 7a 41 73 2c 20 7a 43 6f 6c 29 3d 3d 30 20 p(zAs, zCol)==0
1fdad 29 7b 0a 20 20 20 20 20 20 20 20 20 20 45 78 70 ){. Exp
1fdae 72 20 2a 70 4f 72 69 67 3b 0a 20 20 20 20 20 20 r *pOrig;.
1fdaf 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70 assert( pExp
1fdb0 72 2d 3e 70 4c 65 66 74 3d 3d 30 20 26 26 20 70 r->pLeft==0 && p
1fdb1 45 78 70 72 2d 3e 70 52 69 67 68 74 3d 3d 30 20 Expr->pRight==0
1fdb2 29 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 );. ass
1fdb3 65 72 74 28 20 70 45 78 70 72 2d 3e 78 2e 70 4c ert( pExpr->x.pL
1fdb4 69 73 74 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 ist==0 );.
1fdb5 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70 assert( pExp
1fdb6 72 2d 3e 78 2e 70 53 65 6c 65 63 74 3d 3d 30 20 r->x.pSelect==0
1fdb7 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4f 72 );. pOr
1fdb8 69 67 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 6a ig = pEList->a[j
1fdb9 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 20 ].pExpr;.
1fdba 20 20 20 69 66 28 20 21 70 4e 43 2d 3e 61 6c 6c if( !pNC->all
1fdbb 6f 77 41 67 67 20 26 26 20 45 78 70 72 48 61 73 owAgg && ExprHas
1fdbc 50 72 6f 70 65 72 74 79 28 70 4f 72 69 67 2c 20 Property(pOrig,
1fdbd 45 50 5f 41 67 67 29 20 29 7b 0a 20 20 20 20 20 EP_Agg) ){.
1fdbe 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 sqlite3Er
1fdbf 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 rorMsg(pParse, "
1fdc0 6d 69 73 75 73 65 20 6f 66 20 61 6c 69 61 73 65 misuse of aliase
1fdc1 64 20 61 67 67 72 65 67 61 74 65 20 25 73 22 2c d aggregate %s",
1fdc2 20 7a 41 73 29 3b 0a 20 20 20 20 20 20 20 20 20 zAs);.
1fdc3 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 return WRC_Ab
1fdc4 6f 72 74 3b 0a 20 20 20 20 20 20 20 20 20 20 7d ort;. }
1fdc5 0a 20 20 20 20 20 20 20 20 20 20 72 65 73 6f 6c . resol
1fdc6 76 65 41 6c 69 61 73 28 70 50 61 72 73 65 2c 20 veAlias(pParse,
1fdc7 70 45 4c 69 73 74 2c 20 6a 2c 20 70 45 78 70 72 pEList, j, pExpr
1fdc8 2c 20 22 22 29 3b 0a 20 20 20 20 20 20 20 20 20 , "");.
1fdc9 20 63 6e 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 cnt = 1;.
1fdca 20 20 20 20 70 4d 61 74 63 68 20 3d 20 30 3b 0a pMatch = 0;.
1fdcb 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 assert
1fdcc 28 20 7a 54 61 62 3d 3d 30 20 26 26 20 7a 44 62 ( zTab==0 && zDb
1fdcd 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 20 ==0 );.
1fdce 20 67 6f 74 6f 20 6c 6f 6f 6b 75 70 6e 61 6d 65 goto lookupname
1fdcf 5f 65 6e 64 3b 0a 20 20 20 20 20 20 20 20 7d 0a _end;. }.
1fdd0 20 20 20 20 20 20 7d 20 0a 20 20 20 20 7d 0a 0a } . }..
1fdd1 20 20 20 20 2f 2a 20 41 64 76 61 6e 63 65 20 74 /* Advance t
1fdd2 6f 20 74 68 65 20 6e 65 78 74 20 6e 61 6d 65 20 o the next name
1fdd3 63 6f 6e 74 65 78 74 2e 20 20 54 68 65 20 6c 6f context. The lo
1fdd4 6f 70 20 77 69 6c 6c 20 65 78 69 74 20 77 68 65 op will exit whe
1fdd5 6e 20 65 69 74 68 65 72 0a 20 20 20 20 2a 2a 20 n either. **
1fdd6 77 65 20 68 61 76 65 20 61 20 6d 61 74 63 68 20 we have a match
1fdd7 28 63 6e 74 3e 30 29 20 6f 72 20 77 68 65 6e 20 (cnt>0) or when
1fdd8 77 65 20 72 75 6e 20 6f 75 74 20 6f 66 20 6e 61 we run out of na
1fdd9 6d 65 20 63 6f 6e 74 65 78 74 73 2e 0a 20 20 20 me contexts..
1fdda 20 2a 2f 0a 20 20 20 20 69 66 28 20 63 6e 74 3d */. if( cnt=
1fddb 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 4e 43 20 =0 ){. pNC
1fddc 3d 20 70 4e 43 2d 3e 70 4e 65 78 74 3b 0a 20 20 = pNC->pNext;.
1fddd 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 }. }.. /*.
1fdde 2a 2a 20 49 66 20 58 20 61 6e 64 20 59 20 61 72 ** If X and Y ar
1fddf 65 20 4e 55 4c 4c 20 28 69 6e 20 6f 74 68 65 72 e NULL (in other
1fde0 20 77 6f 72 64 73 20 69 66 20 6f 6e 6c 79 20 74 words if only t
1fde1 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 5a he column name Z
1fde2 20 69 73 0a 20 20 2a 2a 20 73 75 70 70 6c 69 65 is. ** supplie
1fde3 64 29 20 61 6e 64 20 74 68 65 20 76 61 6c 75 65 d) and the value
1fde4 20 6f 66 20 5a 20 69 73 20 65 6e 63 6c 6f 73 65 of Z is enclose
1fde5 64 20 69 6e 20 64 6f 75 62 6c 65 2d 71 75 6f 74 d in double-quot
1fde6 65 73 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 5a 20 es, then. ** Z
1fde7 69 73 20 61 20 73 74 72 69 6e 67 20 6c 69 74 65 is a string lite
1fde8 72 61 6c 20 69 66 20 69 74 20 64 6f 65 73 6e 27 ral if it doesn'
1fde9 74 20 6d 61 74 63 68 20 61 6e 79 20 63 6f 6c 75 t match any colu
1fdea 6d 6e 20 6e 61 6d 65 73 2e 20 20 49 6e 20 74 68 mn names. In th
1fdeb 61 74 0a 20 20 2a 2a 20 63 61 73 65 2c 20 77 65 at. ** case, we
1fdec 20 6e 65 65 64 20 74 6f 20 72 65 74 75 72 6e 20 need to return
1fded 72 69 67 68 74 20 61 77 61 79 20 61 6e 64 20 6e right away and n
1fdee 6f 74 20 6d 61 6b 65 20 61 6e 79 20 63 68 61 6e ot make any chan
1fdef 67 65 73 20 74 6f 0a 20 20 2a 2a 20 70 45 78 70 ges to. ** pExp
1fdf0 72 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 42 65 63 r.. **. ** Bec
1fdf1 61 75 73 65 20 6e 6f 20 72 65 66 65 72 65 6e 63 ause no referenc
1fdf2 65 20 77 61 73 20 6d 61 64 65 20 74 6f 20 6f 75 e was made to ou
1fdf3 74 65 72 20 63 6f 6e 74 65 78 74 73 2c 20 74 68 ter contexts, th
1fdf4 65 20 70 4e 43 2d 3e 6e 52 65 66 0a 20 20 2a 2a e pNC->nRef. **
1fdf5 20 66 69 65 6c 64 73 20 61 72 65 20 6e 6f 74 20 fields are not
1fdf6 63 68 61 6e 67 65 64 20 69 6e 20 61 6e 79 20 63 changed in any c
1fdf7 6f 6e 74 65 78 74 2e 0a 20 20 2a 2f 0a 20 20 69 ontext.. */. i
1fdf8 66 28 20 63 6e 74 3d 3d 30 20 26 26 20 7a 54 61 f( cnt==0 && zTa
1fdf9 62 3d 3d 30 20 26 26 20 45 78 70 72 48 61 73 50 b==0 && ExprHasP
1fdfa 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 45 50 roperty(pExpr,EP
1fdfb 5f 44 62 6c 51 75 6f 74 65 64 29 20 29 7b 0a 20 _DblQuoted) ){.
1fdfc 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54 pExpr->op = T
1fdfd 4b 5f 53 54 52 49 4e 47 3b 0a 20 20 20 20 70 45 K_STRING;. pE
1fdfe 78 70 72 2d 3e 70 54 61 62 20 3d 20 30 3b 0a 20 xpr->pTab = 0;.
1fdff 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72 return WRC_Pr
1fe00 75 6e 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 une;. }.. /*.
1fe01 20 2a 2a 20 63 6e 74 3d 3d 30 20 6d 65 61 6e 73 ** cnt==0 means
1fe02 20 74 68 65 72 65 20 77 61 73 20 6e 6f 74 20 6d there was not m
1fe03 61 74 63 68 2e 20 20 63 6e 74 3e 31 20 6d 65 61 atch. cnt>1 mea
1fe04 6e 73 20 74 68 65 72 65 20 77 65 72 65 20 74 77 ns there were tw
1fe05 6f 20 6f 72 0a 20 20 2a 2a 20 6d 6f 72 65 20 6d o or. ** more m
1fe06 61 74 63 68 65 73 2e 20 20 45 69 74 68 65 72 20 atches. Either
1fe07 77 61 79 2c 20 77 65 20 68 61 76 65 20 61 6e 20 way, we have an
1fe08 65 72 72 6f 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 error.. */. if
1fe09 28 20 63 6e 74 21 3d 31 20 29 7b 0a 20 20 20 20 ( cnt!=1 ){.
1fe0a 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 45 72 72 const char *zErr
1fe0b 3b 0a 20 20 20 20 7a 45 72 72 20 3d 20 63 6e 74 ;. zErr = cnt
1fe0c 3d 3d 30 20 3f 20 22 6e 6f 20 73 75 63 68 20 63 ==0 ? "no such c
1fe0d 6f 6c 75 6d 6e 22 20 3a 20 22 61 6d 62 69 67 75 olumn" : "ambigu
1fe0e 6f 75 73 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 22 ous column name"
1fe0f 3b 0a 20 20 20 20 69 66 28 20 7a 44 62 20 29 7b ;. if( zDb ){
1fe10 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 . sqlite3Er
1fe11 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 rorMsg(pParse, "
1fe12 25 73 3a 20 25 73 2e 25 73 2e 25 73 22 2c 20 7a %s: %s.%s.%s", z
1fe13 45 72 72 2c 20 7a 44 62 2c 20 7a 54 61 62 2c 20 Err, zDb, zTab,
1fe14 7a 43 6f 6c 29 3b 0a 20 20 20 20 7d 65 6c 73 65 zCol);. }else
1fe15 20 69 66 28 20 7a 54 61 62 20 29 7b 0a 20 20 20 if( zTab ){.
1fe16 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d sqlite3ErrorM
1fe17 73 67 28 70 50 61 72 73 65 2c 20 22 25 73 3a 20 sg(pParse, "%s:
1fe18 25 73 2e 25 73 22 2c 20 7a 45 72 72 2c 20 7a 54 %s.%s", zErr, zT
1fe19 61 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 20 7d ab, zCol);. }
1fe1a 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 else{. sqli
1fe1b 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 te3ErrorMsg(pPar
1fe1c 73 65 2c 20 22 25 73 3a 20 25 73 22 2c 20 7a 45 se, "%s: %s", zE
1fe1d 72 72 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 20 7d rr, zCol);. }
1fe1e 0a 20 20 20 20 70 54 6f 70 4e 43 2d 3e 6e 45 72 . pTopNC->nEr
1fe1f 72 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 r++;. }.. /* I
1fe20 66 20 61 20 63 6f 6c 75 6d 6e 20 66 72 6f 6d 20 f a column from
1fe21 61 20 74 61 62 6c 65 20 69 6e 20 70 53 72 63 4c a table in pSrcL
1fe22 69 73 74 20 69 73 20 72 65 66 65 72 65 6e 63 65 ist is reference
1fe23 64 2c 20 74 68 65 6e 20 72 65 63 6f 72 64 0a 20 d, then record.
1fe24 20 2a 2a 20 74 68 69 73 20 66 61 63 74 20 69 6e ** this fact in
1fe25 20 74 68 65 20 70 53 72 63 4c 69 73 74 2e 61 5b the pSrcList.a[
1fe26 5d 2e 63 6f 6c 55 73 65 64 20 62 69 74 6d 61 73 ].colUsed bitmas
1fe27 6b 2e 20 20 43 6f 6c 75 6d 6e 20 30 20 63 61 75 k. Column 0 cau
1fe28 73 65 73 0a 20 20 2a 2a 20 62 69 74 20 30 20 74 ses. ** bit 0 t
1fe29 6f 20 62 65 20 73 65 74 2e 20 20 43 6f 6c 75 6d o be set. Colum
1fe2a 6e 20 31 20 73 65 74 73 20 62 69 74 20 31 2e 20 n 1 sets bit 1.
1fe2b 20 41 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20 20 And so forth.
1fe2c 49 66 20 74 68 65 0a 20 20 2a 2a 20 63 6f 6c 75 If the. ** colu
1fe2d 6d 6e 20 6e 75 6d 62 65 72 20 69 73 20 67 72 65 mn number is gre
1fe2e 61 74 65 72 20 74 68 61 6e 20 74 68 65 20 6e 75 ater than the nu
1fe2f 6d 62 65 72 20 6f 66 20 62 69 74 73 20 69 6e 20 mber of bits in
1fe30 74 68 65 20 62 69 74 6d 61 73 6b 0a 20 20 2a 2a the bitmask. **
1fe31 20 74 68 65 6e 20 73 65 74 20 74 68 65 20 68 69 then set the hi
1fe32 67 68 2d 6f 72 64 65 72 20 62 69 74 20 6f 66 20 gh-order bit of
1fe33 74 68 65 20 62 69 74 6d 61 73 6b 2e 0a 20 20 2a the bitmask.. *
1fe34 2f 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e 69 /. if( pExpr->i
1fe35 43 6f 6c 75 6d 6e 3e 3d 30 20 26 26 20 70 4d 61 Column>=0 && pMa
1fe36 74 63 68 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e tch!=0 ){. in
1fe37 74 20 6e 20 3d 20 70 45 78 70 72 2d 3e 69 43 6f t n = pExpr->iCo
1fe38 6c 75 6d 6e 3b 0a 20 20 20 20 74 65 73 74 63 61 lumn;. testca
1fe39 73 65 28 20 6e 3d 3d 42 4d 53 2d 31 20 29 3b 0a se( n==BMS-1 );.
1fe3a 20 20 20 20 69 66 28 20 6e 3e 3d 42 4d 53 20 29 if( n>=BMS )
1fe3b 7b 0a 20 20 20 20 20 20 6e 20 3d 20 42 4d 53 2d {. n = BMS-
1fe3c 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 1;. }. ass
1fe3d 65 72 74 28 20 70 4d 61 74 63 68 2d 3e 69 43 75 ert( pMatch->iCu
1fe3e 72 73 6f 72 3d 3d 70 45 78 70 72 2d 3e 69 54 61 rsor==pExpr->iTa
1fe3f 62 6c 65 20 29 3b 0a 20 20 20 20 70 4d 61 74 63 ble );. pMatc
1fe40 68 2d 3e 63 6f 6c 55 73 65 64 20 7c 3d 20 28 28 h->colUsed |= ((
1fe41 42 69 74 6d 61 73 6b 29 31 29 3c 3c 6e 3b 0a 20 Bitmask)1)<<n;.
1fe42 20 7d 0a 0a 20 20 2f 2a 20 43 6c 65 61 6e 20 75 }.. /* Clean u
1fe43 70 20 61 6e 64 20 72 65 74 75 72 6e 0a 20 20 2a p and return. *
1fe44 2f 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 /. sqlite3ExprD
1fe45 65 6c 65 74 65 28 64 62 2c 20 70 45 78 70 72 2d elete(db, pExpr-
1fe46 3e 70 4c 65 66 74 29 3b 0a 20 20 70 45 78 70 72 >pLeft);. pExpr
1fe47 2d 3e 70 4c 65 66 74 20 3d 20 30 3b 0a 20 20 73 ->pLeft = 0;. s
1fe48 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 qlite3ExprDelete
1fe49 28 64 62 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 (db, pExpr->pRig
1fe4a 68 74 29 3b 0a 20 20 70 45 78 70 72 2d 3e 70 52 ht);. pExpr->pR
1fe4b 69 67 68 74 20 3d 20 30 3b 0a 20 20 70 45 78 70 ight = 0;. pExp
1fe4c 72 2d 3e 6f 70 20 3d 20 28 69 73 54 72 69 67 67 r->op = (isTrigg
1fe4d 65 72 20 3f 20 54 4b 5f 54 52 49 47 47 45 52 20 er ? TK_TRIGGER
1fe4e 3a 20 54 4b 5f 43 4f 4c 55 4d 4e 29 3b 0a 6c 6f : TK_COLUMN);.lo
1fe4f 6f 6b 75 70 6e 61 6d 65 5f 65 6e 64 3a 0a 20 20 okupname_end:.
1fe50 69 66 28 20 63 6e 74 3d 3d 31 20 29 7b 0a 20 20 if( cnt==1 ){.
1fe51 20 20 61 73 73 65 72 74 28 20 70 4e 43 21 3d 30 assert( pNC!=0
1fe52 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 41 );. sqlite3A
1fe53 75 74 68 52 65 61 64 28 70 50 61 72 73 65 2c 20 uthRead(pParse,
1fe54 70 45 78 70 72 2c 20 70 53 63 68 65 6d 61 2c 20 pExpr, pSchema,
1fe55 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74 29 3b 0a pNC->pSrcList);.
1fe56 20 20 20 20 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 /* Increment
1fe57 20 74 68 65 20 6e 52 65 66 20 76 61 6c 75 65 20 the nRef value
1fe58 6f 6e 20 61 6c 6c 20 6e 61 6d 65 20 63 6f 6e 74 on all name cont
1fe59 65 78 74 73 20 66 72 6f 6d 20 54 6f 70 4e 43 20 exts from TopNC
1fe5a 75 70 20 74 6f 0a 20 20 20 20 2a 2a 20 74 68 65 up to. ** the
1fe5b 20 70 6f 69 6e 74 20 77 68 65 72 65 20 74 68 65 point where the
1fe5c 20 6e 61 6d 65 20 6d 61 74 63 68 65 64 2e 20 2a name matched. *
1fe5d 2f 0a 20 20 20 20 66 6f 72 28 3b 3b 29 7b 0a 20 /. for(;;){.
1fe5e 20 20 20 20 20 61 73 73 65 72 74 28 20 70 54 6f assert( pTo
1fe5f 70 4e 43 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 pNC!=0 );.
1fe60 70 54 6f 70 4e 43 2d 3e 6e 52 65 66 2b 2b 3b 0a pTopNC->nRef++;.
1fe61 20 20 20 20 20 20 69 66 28 20 70 54 6f 70 4e 43 if( pTopNC
1fe62 3d 3d 70 4e 43 20 29 20 62 72 65 61 6b 3b 0a 20 ==pNC ) break;.
1fe63 20 20 20 20 20 70 54 6f 70 4e 43 20 3d 20 70 54 pTopNC = pT
1fe64 6f 70 4e 43 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 opNC->pNext;.
1fe65 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 57 52 }. return WR
1fe66 43 5f 50 72 75 6e 65 3b 0a 20 20 7d 20 65 6c 73 C_Prune;. } els
1fe67 65 20 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 57 e {. return W
1fe68 52 43 5f 41 62 6f 72 74 3b 0a 20 20 7d 0a 7d 0a RC_Abort;. }.}.
1fe69 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 ./*.** This rout
1fe6a 69 6e 65 20 69 73 20 63 61 6c 6c 62 61 63 6b 20 ine is callback
1fe6b 66 6f 72 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 for sqlite3WalkE
1fe6c 78 70 72 28 29 2e 0a 2a 2a 0a 2a 2a 20 52 65 73 xpr()..**.** Res
1fe6d 6f 6c 76 65 20 73 79 6d 62 6f 6c 69 63 20 6e 61 olve symbolic na
1fe6e 6d 65 73 20 69 6e 74 6f 20 54 4b 5f 43 4f 4c 55 mes into TK_COLU
1fe6f 4d 4e 20 6f 70 65 72 61 74 6f 72 73 20 66 6f 72 MN operators for
1fe70 20 74 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20 the current.**
1fe71 6e 6f 64 65 20 69 6e 20 74 68 65 20 65 78 70 72 node in the expr
1fe72 65 73 73 69 6f 6e 20 74 72 65 65 2e 20 20 52 65 ession tree. Re
1fe73 74 75 72 6e 20 30 20 74 6f 20 63 6f 6e 74 69 6e turn 0 to contin
1fe74 75 65 20 74 68 65 20 73 65 61 72 63 68 20 64 6f ue the search do
1fe75 77 6e 0a 2a 2a 20 74 68 65 20 74 72 65 65 20 6f wn.** the tree o
1fe76 72 20 32 20 74 6f 20 61 62 6f 72 74 20 74 68 65 r 2 to abort the
1fe77 20 74 72 65 65 20 77 61 6c 6b 2e 0a 2a 2a 0a 2a tree walk..**.*
1fe78 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 * This routine a
1fe79 6c 73 6f 20 64 6f 65 73 20 65 72 72 6f 72 20 63 lso does error c
1fe7a 68 65 63 6b 69 6e 67 20 61 6e 64 20 6e 61 6d 65 hecking and name
1fe7b 20 72 65 73 6f 6c 75 74 69 6f 6e 20 66 6f 72 0a resolution for.
1fe7c 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 ** function name
1fe7d 73 2e 20 20 54 68 65 20 6f 70 65 72 61 74 6f 72 s. The operator
1fe7e 20 66 6f 72 20 61 67 67 72 65 67 61 74 65 20 66 for aggregate f
1fe7f 75 6e 63 74 69 6f 6e 73 20 69 73 20 63 68 61 6e unctions is chan
1fe80 67 65 64 0a 2a 2a 20 74 6f 20 54 4b 5f 41 47 47 ged.** to TK_AGG
1fe81 5f 46 55 4e 43 54 49 4f 4e 2e 0a 2a 2f 0a 73 74 _FUNCTION..*/.st
1fe82 61 74 69 63 20 69 6e 74 20 72 65 73 6f 6c 76 65 atic int resolve
1fe83 45 78 70 72 53 74 65 70 28 57 61 6c 6b 65 72 20 ExprStep(Walker
1fe84 2a 70 57 61 6c 6b 65 72 2c 20 45 78 70 72 20 2a *pWalker, Expr *
1fe85 70 45 78 70 72 29 7b 0a 20 20 4e 61 6d 65 43 6f pExpr){. NameCo
1fe86 6e 74 65 78 74 20 2a 70 4e 43 3b 0a 20 20 50 61 ntext *pNC;. Pa
1fe87 72 73 65 20 2a 70 50 61 72 73 65 3b 0a 0a 20 20 rse *pParse;..
1fe88 70 4e 43 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 75 pNC = pWalker->u
1fe89 2e 70 4e 43 3b 0a 20 20 61 73 73 65 72 74 28 20 .pNC;. assert(
1fe8a 70 4e 43 21 3d 30 20 29 3b 0a 20 20 70 50 61 72 pNC!=0 );. pPar
1fe8b 73 65 20 3d 20 70 4e 43 2d 3e 70 50 61 72 73 65 se = pNC->pParse
1fe8c 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72 ;. assert( pPar
1fe8d 73 65 3d 3d 70 57 61 6c 6b 65 72 2d 3e 70 50 61 se==pWalker->pPa
1fe8e 72 73 65 20 29 3b 0a 0a 20 20 69 66 28 20 45 78 rse );.. if( Ex
1fe8f 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 prHasAnyProperty
1fe90 28 70 45 78 70 72 2c 20 45 50 5f 52 65 73 6f 6c (pExpr, EP_Resol
1fe91 76 65 64 29 20 29 20 72 65 74 75 72 6e 20 57 52 ved) ) return WR
1fe92 43 5f 50 72 75 6e 65 3b 0a 20 20 45 78 70 72 53 C_Prune;. ExprS
1fe93 65 74 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 etProperty(pExpr
1fe94 2c 20 45 50 5f 52 65 73 6f 6c 76 65 64 29 3b 0a , EP_Resolved);.
1fe95 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 #ifndef NDEBUG.
1fe96 20 69 66 28 20 70 4e 43 2d 3e 70 53 72 63 4c 69 if( pNC->pSrcLi
1fe97 73 74 20 26 26 20 70 4e 43 2d 3e 70 53 72 63 4c st && pNC->pSrcL
1fe98 69 73 74 2d 3e 6e 41 6c 6c 6f 63 3e 30 20 29 7b ist->nAlloc>0 ){
1fe99 0a 20 20 20 20 53 72 63 4c 69 73 74 20 2a 70 53 . SrcList *pS
1fe9a 72 63 4c 69 73 74 20 3d 20 70 4e 43 2d 3e 70 53 rcList = pNC->pS
1fe9b 72 63 4c 69 73 74 3b 0a 20 20 20 20 69 6e 74 20 rcList;. int
1fe9c 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 i;. for(i=0;
1fe9d 69 3c 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74 2d i<pNC->pSrcList-
1fe9e 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 >nSrc; i++){.
1fe9f 20 20 20 61 73 73 65 72 74 28 20 70 53 72 63 4c assert( pSrcL
1fea0 69 73 74 2d 3e 61 5b 69 5d 2e 69 43 75 72 73 6f ist->a[i].iCurso
1fea1 72 3e 3d 30 20 26 26 20 70 53 72 63 4c 69 73 74 r>=0 && pSrcList
1fea2 2d 3e 61 5b 69 5d 2e 69 43 75 72 73 6f 72 3c 70 ->a[i].iCursor<p
1fea3 50 61 72 73 65 2d 3e 6e 54 61 62 29 3b 0a 20 20 Parse->nTab);.
1fea4 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 }. }.#endif.
1fea5 20 73 77 69 74 63 68 28 20 70 45 78 70 72 2d 3e switch( pExpr->
1fea6 6f 70 20 29 7b 0a 0a 23 69 66 20 64 65 66 69 6e op ){..#if defin
1fea7 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 ed(SQLITE_ENABLE
1fea8 5f 55 50 44 41 54 45 5f 44 45 4c 45 54 45 5f 4c _UPDATE_DELETE_L
1fea9 49 4d 49 54 29 20 26 26 20 21 64 65 66 69 6e 65 IMIT) && !define
1feaa 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 d(SQLITE_OMIT_SU
1feab 42 51 55 45 52 59 29 0a 20 20 20 20 2f 2a 20 54 BQUERY). /* T
1feac 68 65 20 73 70 65 63 69 61 6c 20 6f 70 65 72 61 he special opera
1fead 74 6f 72 20 54 4b 5f 52 4f 57 20 6d 65 61 6e 73 tor TK_ROW means
1feae 20 75 73 65 20 74 68 65 20 72 6f 77 69 64 20 66 use the rowid f
1feaf 6f 72 20 74 68 65 20 66 69 72 73 74 0a 20 20 20 or the first.
1feb0 20 2a 2a 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 ** column in th
1feb1 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e 20 20 e FROM clause.
1feb2 54 68 69 73 20 69 73 20 75 73 65 64 20 62 79 20 This is used by
1feb3 74 68 65 20 4c 49 4d 49 54 20 61 6e 64 20 4f 52 the LIMIT and OR
1feb4 44 45 52 20 42 59 0a 20 20 20 20 2a 2a 20 63 6c DER BY. ** cl
1feb5 61 75 73 65 20 70 72 6f 63 65 73 73 69 6e 67 20 ause processing
1feb6 6f 6e 20 55 50 44 41 54 45 20 61 6e 64 20 44 45 on UPDATE and DE
1feb7 4c 45 54 45 20 73 74 61 74 65 6d 65 6e 74 73 2e LETE statements.
1feb8 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 . */. case
1feb9 20 54 4b 5f 52 4f 57 3a 20 7b 0a 20 20 20 20 20 TK_ROW: {.
1feba 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 4c 69 SrcList *pSrcLi
1febb 73 74 20 3d 20 70 4e 43 2d 3e 70 53 72 63 4c 69 st = pNC->pSrcLi
1febc 73 74 3b 0a 20 20 20 20 20 20 73 74 72 75 63 74 st;. struct
1febd 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 SrcList_item *p
1febe 49 74 65 6d 3b 0a 20 20 20 20 20 20 61 73 73 65 Item;. asse
1febf 72 74 28 20 70 53 72 63 4c 69 73 74 20 26 26 20 rt( pSrcList &&
1fec0 70 53 72 63 4c 69 73 74 2d 3e 6e 53 72 63 3d 3d pSrcList->nSrc==
1fec1 31 20 29 3b 0a 20 20 20 20 20 20 70 49 74 65 6d 1 );. pItem
1fec2 20 3d 20 70 53 72 63 4c 69 73 74 2d 3e 61 3b 20 = pSrcList->a;
1fec3 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 . pExpr->op
1fec4 20 3d 20 54 4b 5f 43 4f 4c 55 4d 4e 3b 0a 20 20 = TK_COLUMN;.
1fec5 20 20 20 20 70 45 78 70 72 2d 3e 70 54 61 62 20 pExpr->pTab
1fec6 3d 20 70 49 74 65 6d 2d 3e 70 54 61 62 3b 0a 20 = pItem->pTab;.
1fec7 20 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 pExpr->iTab
1fec8 6c 65 20 3d 20 70 49 74 65 6d 2d 3e 69 43 75 72 le = pItem->iCur
1fec9 73 6f 72 3b 0a 20 20 20 20 20 20 70 45 78 70 72 sor;. pExpr
1feca 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 2d 31 3b 0a ->iColumn = -1;.
1fecb 20 20 20 20 20 20 70 45 78 70 72 2d 3e 61 66 66 pExpr->aff
1fecc 69 6e 69 74 79 20 3d 20 53 51 4c 49 54 45 5f 41 inity = SQLITE_A
1fecd 46 46 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 20 FF_INTEGER;.
1fece 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 break;. }.#
1fecf 65 6e 64 69 66 20 2f 2a 20 64 65 66 69 6e 65 64 endif /* defined
1fed0 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 55 (SQLITE_ENABLE_U
1fed1 50 44 41 54 45 5f 44 45 4c 45 54 45 5f 4c 49 4d PDATE_DELETE_LIM
1fed2 49 54 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 IT) && !defined(
1fed3 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 SQLITE_OMIT_SUBQ
1fed4 55 45 52 59 29 20 2a 2f 0a 0a 20 20 20 20 2f 2a UERY) */.. /*
1fed5 20 41 20 6c 6f 6e 65 20 69 64 65 6e 74 69 66 69 A lone identifi
1fed6 65 72 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f er is the name o
1fed7 66 20 61 20 63 6f 6c 75 6d 6e 2e 0a 20 20 20 20 f a column..
1fed8 2a 2f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 */. case TK_I
1fed9 44 3a 20 7b 0a 20 20 20 20 20 20 72 65 74 75 72 D: {. retur
1feda 6e 20 6c 6f 6f 6b 75 70 4e 61 6d 65 28 70 50 61 n lookupName(pPa
1fedb 72 73 65 2c 20 30 2c 20 30 2c 20 70 45 78 70 72 rse, 0, 0, pExpr
1fedc 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 20 70 4e 43 2c ->u.zToken, pNC,
1fedd 20 70 45 78 70 72 29 3b 0a 20 20 20 20 7d 0a 20 pExpr);. }.
1fede 20 0a 20 20 20 20 2f 2a 20 41 20 74 61 62 6c 65 . /* A table
1fedf 20 6e 61 6d 65 20 61 6e 64 20 63 6f 6c 75 6d 6e name and column
1fee0 20 6e 61 6d 65 3a 20 20 20 20 20 49 44 2e 49 44 name: ID.ID
1fee1 0a 20 20 20 20 2a 2a 20 4f 72 20 61 20 64 61 74 . ** Or a dat
1fee2 61 62 61 73 65 2c 20 74 61 62 6c 65 20 61 6e 64 abase, table and
1fee3 20 63 6f 6c 75 6d 6e 3a 20 20 49 44 2e 49 44 2e column: ID.ID.
1fee4 49 44 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 ID. */. ca
1fee5 73 65 20 54 4b 5f 44 4f 54 3a 20 7b 0a 20 20 20 se TK_DOT: {.
1fee6 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a const char *z
1fee7 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 63 6f Column;. co
1fee8 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 6c 65 nst char *zTable
1fee9 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 ;. const ch
1feea 61 72 20 2a 7a 44 62 3b 0a 20 20 20 20 20 20 45 ar *zDb;. E
1feeb 78 70 72 20 2a 70 52 69 67 68 74 3b 0a 0a 20 20 xpr *pRight;..
1feec 20 20 20 20 2f 2a 20 69 66 28 20 70 53 72 63 4c /* if( pSrcL
1feed 69 73 74 3d 3d 30 20 29 20 62 72 65 61 6b 3b 20 ist==0 ) break;
1feee 2a 2f 0a 20 20 20 20 20 20 70 52 69 67 68 74 20 */. pRight
1feef 3d 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 3b = pExpr->pRight;
1fef0 0a 20 20 20 20 20 20 69 66 28 20 70 52 69 67 68 . if( pRigh
1fef1 74 2d 3e 6f 70 3d 3d 54 4b 5f 49 44 20 29 7b 0a t->op==TK_ID ){.
1fef2 20 20 20 20 20 20 20 20 7a 44 62 20 3d 20 30 3b zDb = 0;
1fef3 0a 20 20 20 20 20 20 20 20 7a 54 61 62 6c 65 20 . zTable
1fef4 3d 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2d 3e = pExpr->pLeft->
1fef5 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 u.zToken;.
1fef6 20 20 7a 43 6f 6c 75 6d 6e 20 3d 20 70 52 69 67 zColumn = pRig
1fef7 68 74 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 ht->u.zToken;.
1fef8 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1fef9 20 20 20 61 73 73 65 72 74 28 20 70 52 69 67 68 assert( pRigh
1fefa 74 2d 3e 6f 70 3d 3d 54 4b 5f 44 4f 54 20 29 3b t->op==TK_DOT );
1fefb 0a 20 20 20 20 20 20 20 20 7a 44 62 20 3d 20 70 . zDb = p
1fefc 45 78 70 72 2d 3e 70 4c 65 66 74 2d 3e 75 2e 7a Expr->pLeft->u.z
1fefd 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 20 20 7a Token;. z
1fefe 54 61 62 6c 65 20 3d 20 70 52 69 67 68 74 2d 3e Table = pRight->
1feff 70 4c 65 66 74 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b pLeft->u.zToken;
1ff00 0a 20 20 20 20 20 20 20 20 7a 43 6f 6c 75 6d 6e . zColumn
1ff01 20 3d 20 70 52 69 67 68 74 2d 3e 70 52 69 67 68 = pRight->pRigh
1ff02 74 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 20 t->u.zToken;.
1ff03 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72 }. retur
1ff04 6e 20 6c 6f 6f 6b 75 70 4e 61 6d 65 28 70 50 61 n lookupName(pPa
1ff05 72 73 65 2c 20 7a 44 62 2c 20 7a 54 61 62 6c 65 rse, zDb, zTable
1ff06 2c 20 7a 43 6f 6c 75 6d 6e 2c 20 70 4e 43 2c 20 , zColumn, pNC,
1ff07 70 45 78 70 72 29 3b 0a 20 20 20 20 7d 0a 0a 20 pExpr);. }..
1ff08 20 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 66 75 /* Resolve fu
1ff09 6e 63 74 69 6f 6e 20 6e 61 6d 65 73 0a 20 20 20 nction names.
1ff0a 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f */. case TK_
1ff0b 43 4f 4e 53 54 5f 46 55 4e 43 3a 0a 20 20 20 20 CONST_FUNC:.
1ff0c 63 61 73 65 20 54 4b 5f 46 55 4e 43 54 49 4f 4e case TK_FUNCTION
1ff0d 3a 20 7b 0a 20 20 20 20 20 20 45 78 70 72 4c 69 : {. ExprLi
1ff0e 73 74 20 2a 70 4c 69 73 74 20 3d 20 70 45 78 70 st *pList = pExp
1ff0f 72 2d 3e 78 2e 70 4c 69 73 74 3b 20 20 20 20 2f r->x.pList; /
1ff10 2a 20 54 68 65 20 61 72 67 75 6d 65 6e 74 20 6c * The argument l
1ff11 69 73 74 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 ist */. int
1ff12 20 6e 20 3d 20 70 4c 69 73 74 20 3f 20 70 4c 69 n = pList ? pLi
1ff13 73 74 2d 3e 6e 45 78 70 72 20 3a 20 30 3b 20 20 st->nExpr : 0;
1ff14 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 /* Number of a
1ff15 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20 rguments */.
1ff16 20 20 69 6e 74 20 6e 6f 5f 73 75 63 68 5f 66 75 int no_such_fu
1ff17 6e 63 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a nc = 0; /*
1ff18 20 54 72 75 65 20 69 66 20 6e 6f 20 73 75 63 68 True if no such
1ff19 20 66 75 6e 63 74 69 6f 6e 20 65 78 69 73 74 73 function exists
1ff1a 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 77 72 */. int wr
1ff1b 6f 6e 67 5f 6e 75 6d 5f 61 72 67 73 20 3d 20 30 ong_num_args = 0
1ff1c 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 ; /* True if
1ff1d 20 77 72 6f 6e 67 20 6e 75 6d 62 65 72 20 6f 66 wrong number of
1ff1e 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 20 20 arguments */.
1ff1f 20 20 20 20 69 6e 74 20 69 73 5f 61 67 67 20 3d int is_agg =
1ff20 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 0;
1ff21 2f 2a 20 54 72 75 65 20 69 66 20 69 73 20 61 6e /* True if is an
1ff22 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 aggregate funct
1ff23 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 ion */. int
1ff24 20 61 75 74 68 3b 20 20 20 20 20 20 20 20 20 20 auth;
1ff25 20 20 20 20 20 20 20 20 20 2f 2a 20 41 75 74 68 /* Auth
1ff26 6f 72 69 7a 61 74 69 6f 6e 20 74 6f 20 75 73 65 orization to use
1ff27 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f the function */
1ff28 0a 20 20 20 20 20 20 69 6e 74 20 6e 49 64 3b 20 . int nId;
1ff29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ff2a 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
1ff2b 63 68 61 72 61 63 74 65 72 73 20 69 6e 20 66 75 characters in fu
1ff2c 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 nction name */.
1ff2d 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 const char
1ff2e 2a 7a 49 64 3b 20 20 20 20 20 20 20 20 20 20 20 *zId;
1ff2f 20 2f 2a 20 54 68 65 20 66 75 6e 63 74 69 6f 6e /* The function
1ff30 20 6e 61 6d 65 2e 20 2a 2f 0a 20 20 20 20 20 20 name. */.
1ff31 46 75 6e 63 44 65 66 20 2a 70 44 65 66 3b 20 20 FuncDef *pDef;
1ff32 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 /* I
1ff33 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 nformation about
1ff34 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f the function */
1ff35 0a 20 20 20 20 20 20 75 38 20 65 6e 63 20 3d 20 . u8 enc =
1ff36 45 4e 43 28 70 50 61 72 73 65 2d 3e 64 62 29 3b ENC(pParse->db);
1ff37 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 /* The databa
1ff38 73 65 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 0a se encoding */..
1ff39 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
1ff3a 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f pExpr->op==TK_CO
1ff3b 4e 53 54 5f 46 55 4e 43 20 29 3b 0a 20 20 20 20 NST_FUNC );.
1ff3c 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48 assert( !ExprH
1ff3d 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 asProperty(pExpr
1ff3e 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 , EP_xIsSelect)
1ff3f 29 3b 0a 20 20 20 20 20 20 7a 49 64 20 3d 20 70 );. zId = p
1ff40 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a Expr->u.zToken;.
1ff41 20 20 20 20 20 20 6e 49 64 20 3d 20 73 71 6c 69 nId = sqli
1ff42 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 49 64 29 te3Strlen30(zId)
1ff43 3b 0a 20 20 20 20 20 20 70 44 65 66 20 3d 20 73 ;. pDef = s
1ff44 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74 69 qlite3FindFuncti
1ff45 6f 6e 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 7a on(pParse->db, z
1ff46 49 64 2c 20 6e 49 64 2c 20 6e 2c 20 65 6e 63 2c Id, nId, n, enc,
1ff47 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 0);. if( p
1ff48 44 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 Def==0 ){.
1ff49 20 20 70 44 65 66 20 3d 20 73 71 6c 69 74 65 33 pDef = sqlite3
1ff4a 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28 70 50 61 FindFunction(pPa
1ff4b 72 73 65 2d 3e 64 62 2c 20 7a 49 64 2c 20 6e 49 rse->db, zId, nI
1ff4c 64 2c 20 2d 31 2c 20 65 6e 63 2c 20 30 29 3b 0a d, -1, enc, 0);.
1ff4d 20 20 20 20 20 20 20 20 69 66 28 20 70 44 65 66 if( pDef
1ff4e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ==0 ){.
1ff4f 20 6e 6f 5f 73 75 63 68 5f 66 75 6e 63 20 3d 20 no_such_func =
1ff50 31 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 1;. }else
1ff51 7b 0a 20 20 20 20 20 20 20 20 20 20 77 72 6f 6e {. wron
1ff52 67 5f 6e 75 6d 5f 61 72 67 73 20 3d 20 31 3b 0a g_num_args = 1;.
1ff53 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1ff54 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69 }else{. i
1ff55 73 5f 61 67 67 20 3d 20 70 44 65 66 2d 3e 78 46 s_agg = pDef->xF
1ff56 75 6e 63 3d 3d 30 3b 0a 20 20 20 20 20 20 7d 0a unc==0;. }.
1ff57 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1ff58 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f MIT_AUTHORIZATIO
1ff59 4e 0a 20 20 20 20 20 20 69 66 28 20 70 44 65 66 N. if( pDef
1ff5a 20 29 7b 0a 20 20 20 20 20 20 20 20 61 75 74 68 ){. auth
1ff5b 20 3d 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 = sqlite3AuthCh
1ff5c 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 eck(pParse, SQLI
1ff5d 54 45 5f 46 55 4e 43 54 49 4f 4e 2c 20 30 2c 20 TE_FUNCTION, 0,
1ff5e 70 44 65 66 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b pDef->zName, 0);
1ff5f 0a 20 20 20 20 20 20 20 20 69 66 28 20 61 75 74 . if( aut
1ff60 68 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a h!=SQLITE_OK ){.
1ff61 20 20 20 20 20 20 20 20 20 20 69 66 28 20 61 75 if( au
1ff62 74 68 3d 3d 53 51 4c 49 54 45 5f 44 45 4e 59 20 th==SQLITE_DENY
1ff63 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 ){. s
1ff64 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 qlite3ErrorMsg(p
1ff65 50 61 72 73 65 2c 20 22 6e 6f 74 20 61 75 74 68 Parse, "not auth
1ff66 6f 72 69 7a 65 64 20 74 6f 20 75 73 65 20 66 75 orized to use fu
1ff67 6e 63 74 69 6f 6e 3a 20 25 73 22 2c 0a 20 20 20 nction: %s",.
1ff68 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ff69 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ff6a 20 70 44 65 66 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 pDef->zName);.
1ff6b 20 20 20 20 20 20 20 20 20 20 20 70 4e 43 2d 3e pNC->
1ff6c 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20 20 20 nErr++;.
1ff6d 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70 45 }. pE
1ff6e 78 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 4e 55 4c xpr->op = TK_NUL
1ff6f 4c 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 L;. ret
1ff70 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 urn WRC_Prune;.
1ff71 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
1ff72 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 69 66 .#endif. if
1ff73 28 20 69 73 5f 61 67 67 20 26 26 20 21 70 4e 43 ( is_agg && !pNC
1ff74 2d 3e 61 6c 6c 6f 77 41 67 67 20 29 7b 0a 20 20 ->allowAgg ){.
1ff75 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 sqlite3Err
1ff76 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6d orMsg(pParse, "m
1ff77 69 73 75 73 65 20 6f 66 20 61 67 67 72 65 67 61 isuse of aggrega
1ff78 74 65 20 66 75 6e 63 74 69 6f 6e 20 25 2e 2a 73 te function %.*s
1ff79 28 29 22 2c 20 6e 49 64 2c 7a 49 64 29 3b 0a 20 ()", nId,zId);.
1ff7a 20 20 20 20 20 20 20 70 4e 43 2d 3e 6e 45 72 72 pNC->nErr
1ff7b 2b 2b 3b 0a 20 20 20 20 20 20 20 20 69 73 5f 61 ++;. is_a
1ff7c 67 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65 gg = 0;. }e
1ff7d 6c 73 65 20 69 66 28 20 6e 6f 5f 73 75 63 68 5f lse if( no_such_
1ff7e 66 75 6e 63 20 29 7b 0a 20 20 20 20 20 20 20 20 func ){.
1ff7f 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 sqlite3ErrorMsg(
1ff80 70 50 61 72 73 65 2c 20 22 6e 6f 20 73 75 63 68 pParse, "no such
1ff81 20 66 75 6e 63 74 69 6f 6e 3a 20 25 2e 2a 73 22 function: %.*s"
1ff82 2c 20 6e 49 64 2c 20 7a 49 64 29 3b 0a 20 20 20 , nId, zId);.
1ff83 20 20 20 20 20 70 4e 43 2d 3e 6e 45 72 72 2b 2b pNC->nErr++
1ff84 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 ;. }else if
1ff85 28 20 77 72 6f 6e 67 5f 6e 75 6d 5f 61 72 67 73 ( wrong_num_args
1ff86 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 ){. sqli
1ff87 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 te3ErrorMsg(pPar
1ff88 73 65 2c 22 77 72 6f 6e 67 20 6e 75 6d 62 65 72 se,"wrong number
1ff89 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 74 6f of arguments to
1ff8a 20 66 75 6e 63 74 69 6f 6e 20 25 2e 2a 73 28 29 function %.*s()
1ff8b 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ",.
1ff8c 6e 49 64 2c 20 7a 49 64 29 3b 0a 20 20 20 20 20 nId, zId);.
1ff8d 20 20 20 70 4e 43 2d 3e 6e 45 72 72 2b 2b 3b 0a pNC->nErr++;.
1ff8e 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 }. if
1ff8f 28 20 69 73 5f 61 67 67 20 29 7b 0a 20 20 20 20 ( is_agg ){.
1ff90 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20 pExpr->op =
1ff91 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 3b TK_AGG_FUNCTION;
1ff92 0a 20 20 20 20 20 20 20 20 70 4e 43 2d 3e 68 61 . pNC->ha
1ff93 73 41 67 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 sAgg = 1;.
1ff94 7d 0a 20 20 20 20 20 20 69 66 28 20 69 73 5f 61 }. if( is_a
1ff95 67 67 20 29 20 70 4e 43 2d 3e 61 6c 6c 6f 77 41 gg ) pNC->allowA
1ff96 67 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 73 71 gg = 0;. sq
1ff97 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 4c 69 73 lite3WalkExprLis
1ff98 74 28 70 57 61 6c 6b 65 72 2c 20 70 4c 69 73 74 t(pWalker, pList
1ff99 29 3b 0a 20 20 20 20 20 20 69 66 28 20 69 73 5f );. if( is_
1ff9a 61 67 67 20 29 20 70 4e 43 2d 3e 61 6c 6c 6f 77 agg ) pNC->allow
1ff9b 41 67 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 2f Agg = 1;. /
1ff9c 2a 20 46 49 58 20 4d 45 3a 20 20 43 6f 6d 70 75 * FIX ME: Compu
1ff9d 74 65 20 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 te pExpr->affini
1ff9e 74 79 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20 ty based on the
1ff9f 65 78 70 65 63 74 65 64 20 72 65 74 75 72 6e 0a expected return.
1ffa0 20 20 20 20 20 20 2a 2a 20 74 79 70 65 20 6f 66 ** type of
1ffa1 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 0a 20 the function .
1ffa2 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 65 */. re
1ffa3 74 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a turn WRC_Prune;.
1ffa4 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 }.#ifndef SQ
1ffa5 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 LITE_OMIT_SUBQUE
1ffa6 52 59 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 53 RY. case TK_S
1ffa7 45 4c 45 43 54 3a 0a 20 20 20 20 63 61 73 65 20 ELECT:. case
1ffa8 54 4b 5f 45 58 49 53 54 53 3a 20 20 74 65 73 74 TK_EXISTS: test
1ffa9 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d case( pExpr->op=
1ffaa 3d 54 4b 5f 45 58 49 53 54 53 20 29 3b 0a 23 65 =TK_EXISTS );.#e
1ffab 6e 64 69 66 0a 20 20 20 20 63 61 73 65 20 54 4b ndif. case TK
1ffac 5f 49 4e 3a 20 7b 0a 20 20 20 20 20 20 74 65 73 _IN: {. tes
1ffad 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 tcase( pExpr->op
1ffae 3d 3d 54 4b 5f 49 4e 20 29 3b 0a 20 20 20 20 20 ==TK_IN );.
1ffaf 20 69 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 if( ExprHasProp
1ffb0 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78 erty(pExpr, EP_x
1ffb1 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20 20 20 IsSelect) ){.
1ffb2 20 20 20 20 20 69 6e 74 20 6e 52 65 66 20 3d 20 int nRef =
1ffb3 70 4e 43 2d 3e 6e 52 65 66 3b 0a 23 69 66 6e 64 pNC->nRef;.#ifnd
1ffb4 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 ef SQLITE_OMIT_C
1ffb5 48 45 43 4b 0a 20 20 20 20 20 20 20 20 69 66 28 HECK. if(
1ffb6 20 70 4e 43 2d 3e 69 73 43 68 65 63 6b 20 29 7b pNC->isCheck ){
1ffb7 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 . sqlit
1ffb8 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 e3ErrorMsg(pPars
1ffb9 65 2c 22 73 75 62 71 75 65 72 69 65 73 20 70 72 e,"subqueries pr
1ffba 6f 68 69 62 69 74 65 64 20 69 6e 20 43 48 45 43 ohibited in CHEC
1ffbb 4b 20 63 6f 6e 73 74 72 61 69 6e 74 73 22 29 3b K constraints");
1ffbc 0a 20 20 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 . }.#endi
1ffbd 66 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 f. sqlite
1ffbe 33 57 61 6c 6b 53 65 6c 65 63 74 28 70 57 61 6c 3WalkSelect(pWal
1ffbf 6b 65 72 2c 20 70 45 78 70 72 2d 3e 78 2e 70 53 ker, pExpr->x.pS
1ffc0 65 6c 65 63 74 29 3b 0a 20 20 20 20 20 20 20 20 elect);.
1ffc1 61 73 73 65 72 74 28 20 70 4e 43 2d 3e 6e 52 65 assert( pNC->nRe
1ffc2 66 3e 3d 6e 52 65 66 20 29 3b 0a 20 20 20 20 20 f>=nRef );.
1ffc3 20 20 20 69 66 28 20 6e 52 65 66 21 3d 70 4e 43 if( nRef!=pNC
1ffc4 2d 3e 6e 52 65 66 20 29 7b 0a 20 20 20 20 20 20 ->nRef ){.
1ffc5 20 20 20 20 45 78 70 72 53 65 74 50 72 6f 70 65 ExprSetPrope
1ffc6 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 56 61 rty(pExpr, EP_Va
1ffc7 72 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 20 20 rSelect);.
1ffc8 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
1ffc9 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 break;. }.#
1ffca 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1ffcb 49 54 5f 43 48 45 43 4b 0a 20 20 20 20 63 61 73 IT_CHECK. cas
1ffcc 65 20 54 4b 5f 56 41 52 49 41 42 4c 45 3a 20 7b e TK_VARIABLE: {
1ffcd 0a 20 20 20 20 20 20 69 66 28 20 70 4e 43 2d 3e . if( pNC->
1ffce 69 73 43 68 65 63 6b 20 29 7b 0a 20 20 20 20 20 isCheck ){.
1ffcf 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d sqlite3ErrorM
1ffd0 73 67 28 70 50 61 72 73 65 2c 22 70 61 72 61 6d sg(pParse,"param
1ffd1 65 74 65 72 73 20 70 72 6f 68 69 62 69 74 65 64 eters prohibited
1ffd2 20 69 6e 20 43 48 45 43 4b 20 63 6f 6e 73 74 72 in CHECK constr
1ffd3 61 69 6e 74 73 22 29 3b 0a 20 20 20 20 20 20 7d aints");. }
1ffd4 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
1ffd5 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 }.#endif. }.
1ffd6 20 72 65 74 75 72 6e 20 28 70 50 61 72 73 65 2d return (pParse-
1ffd7 3e 6e 45 72 72 20 7c 7c 20 70 50 61 72 73 65 2d >nErr || pParse-
1ffd8 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 >db->mallocFaile
1ffd9 64 29 20 3f 20 57 52 43 5f 41 62 6f 72 74 20 3a d) ? WRC_Abort :
1ffda 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d WRC_Continue;.}
1ffdb 0a 0a 2f 2a 0a 2a 2a 20 70 45 4c 69 73 74 20 69 ../*.** pEList i
1ffdc 73 20 61 20 6c 69 73 74 20 6f 66 20 65 78 70 72 s a list of expr
1ffdd 65 73 73 69 6f 6e 73 20 77 68 69 63 68 20 61 72 essions which ar
1ffde 65 20 72 65 61 6c 6c 79 20 74 68 65 20 72 65 73 e really the res
1ffdf 75 6c 74 20 73 65 74 20 6f 66 20 74 68 65 0a 2a ult set of the.*
1ffe0 2a 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 * a SELECT state
1ffe1 6d 65 6e 74 2e 20 20 70 45 20 69 73 20 61 20 74 ment. pE is a t
1ffe2 65 72 6d 20 69 6e 20 61 6e 20 4f 52 44 45 52 20 erm in an ORDER
1ffe3 42 59 20 6f 72 20 47 52 4f 55 50 20 42 59 20 63 BY or GROUP BY c
1ffe4 6c 61 75 73 65 2e 0a 2a 2a 20 54 68 69 73 20 72 lause..** This r
1ffe5 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20 74 6f outine checks to
1ffe6 20 73 65 65 20 69 66 20 70 45 20 69 73 20 61 20 see if pE is a
1ffe7 73 69 6d 70 6c 65 20 69 64 65 6e 74 69 66 69 65 simple identifie
1ffe8 72 20 77 68 69 63 68 20 63 6f 72 72 65 73 70 6f r which correspo
1ffe9 6e 64 73 0a 2a 2a 20 74 6f 20 74 68 65 20 41 53 nds.** to the AS
1ffea 2d 6e 61 6d 65 20 6f 66 20 6f 6e 65 20 6f 66 20 -name of one of
1ffeb 74 68 65 20 74 65 72 6d 73 20 6f 66 20 74 68 65 the terms of the
1ffec 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 expression list
1ffed 2e 20 20 49 66 20 69 74 20 69 73 2c 0a 2a 2a 20 . If it is,.**
1ffee 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 this routine ret
1ffef 75 72 6e 20 61 6e 20 69 6e 74 65 67 65 72 20 62 urn an integer b
1fff0 65 74 77 65 65 6e 20 31 20 61 6e 64 20 4e 20 77 etween 1 and N w
1fff1 68 65 72 65 20 4e 20 69 73 20 74 68 65 20 6e 75 here N is the nu
1fff2 6d 62 65 72 20 6f 66 0a 2a 2a 20 65 6c 65 6d 65 mber of.** eleme
1fff3 6e 74 73 20 69 6e 20 70 45 4c 69 73 74 2c 20 63 nts in pEList, c
1fff4 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 orresponding to
1fff5 74 68 65 20 6d 61 74 63 68 69 6e 67 20 65 6e 74 the matching ent
1fff6 72 79 2e 20 20 49 66 20 74 68 65 72 65 20 69 73 ry. If there is
1fff7 0a 2a 2a 20 6e 6f 20 6d 61 74 63 68 2c 20 6f 72 .** no match, or
1fff8 20 69 66 20 70 45 20 69 73 20 6e 6f 74 20 61 20 if pE is not a
1fff9 73 69 6d 70 6c 65 20 69 64 65 6e 74 69 66 69 65 simple identifie
1fffa 72 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 r, then this rou
1fffb 74 69 6e 65 0a 2a 2a 20 72 65 74 75 72 6e 20 30 tine.** return 0
1fffc 2e 0a 2a 2a 0a 2a 2a 20 70 45 4c 69 73 74 20 68 ..**.** pEList h
1fffd 61 73 20 62 65 65 6e 20 72 65 73 6f 6c 76 65 64 as been resolved
1fffe 2e 20 20 70 45 20 68 61 73 20 6e 6f 74 2e 0a 2a . pE has not..*
1ffff 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 73 /.static int res
20000 6f 6c 76 65 41 73 4e 61 6d 65 28 0a 20 20 50 61 olveAsName(. Pa
20001 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 rse *pParse,
20002 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 /* Parsing cont
20003 65 78 74 20 66 6f 72 20 65 72 72 6f 72 20 6d 65 ext for error me
20004 73 73 61 67 65 73 20 2a 2f 0a 20 20 45 78 70 72 ssages */. Expr
20005 4c 69 73 74 20 2a 70 45 4c 69 73 74 2c 20 20 2f List *pEList, /
20006 2a 20 4c 69 73 74 20 6f 66 20 65 78 70 72 65 73 * List of expres
20007 73 69 6f 6e 73 20 74 6f 20 73 63 61 6e 20 2a 2f sions to scan */
20008 0a 20 20 45 78 70 72 20 2a 70 45 20 20 20 20 20 . Expr *pE
20009 20 20 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73 /* Express
2000a 69 6f 6e 20 77 65 20 61 72 65 20 74 72 79 69 6e ion we are tryin
2000b 67 20 74 6f 20 6d 61 74 63 68 20 2a 2f 0a 29 7b g to match */.){
2000c 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 . int i;
2000d 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f /* Loop co
2000e 75 6e 74 65 72 20 2a 2f 0a 0a 20 20 55 4e 55 53 unter */.. UNUS
2000f 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 50 61 ED_PARAMETER(pPa
20010 72 73 65 29 3b 0a 0a 20 20 69 66 28 20 70 45 2d rse);.. if( pE-
20011 3e 6f 70 3d 3d 54 4b 5f 49 44 20 29 7b 0a 20 20 >op==TK_ID ){.
20012 20 20 63 68 61 72 20 2a 7a 43 6f 6c 20 3d 20 70 char *zCol = p
20013 45 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 20 E->u.zToken;.
20014 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 45 4c 69 for(i=0; i<pELi
20015 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b st->nExpr; i++){
20016 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 41 73 . char *zAs
20017 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e = pEList->a[i].
20018 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 69 66 28 zName;. if(
20019 20 7a 41 73 21 3d 30 20 26 26 20 73 71 6c 69 74 zAs!=0 && sqlit
2001a 65 33 53 74 72 49 43 6d 70 28 7a 41 73 2c 20 7a e3StrICmp(zAs, z
2001b 43 6f 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 Col)==0 ){.
2001c 20 20 20 72 65 74 75 72 6e 20 69 2b 31 3b 0a 20 return i+1;.
2001d 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d }. }. }
2001e 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a . return 0;.}..
2001f 2f 2a 0a 2a 2a 20 70 45 20 69 73 20 61 20 70 6f /*.** pE is a po
20020 69 6e 74 65 72 20 74 6f 20 61 6e 20 65 78 70 72 inter to an expr
20021 65 73 73 69 6f 6e 20 77 68 69 63 68 20 69 73 20 ession which is
20022 61 20 73 69 6e 67 6c 65 20 74 65 72 6d 20 69 6e a single term in
20023 20 74 68 65 0a 2a 2a 20 4f 52 44 45 52 20 42 59 the.** ORDER BY
20024 20 6f 66 20 61 20 63 6f 6d 70 6f 75 6e 64 20 53 of a compound S
20025 45 4c 45 43 54 2e 20 20 54 68 65 20 65 78 70 72 ELECT. The expr
20026 65 73 73 69 6f 6e 20 68 61 73 20 6e 6f 74 20 62 ession has not b
20027 65 65 6e 0a 2a 2a 20 6e 61 6d 65 20 72 65 73 6f een.** name reso
20028 6c 76 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 74 20 74 lved..**.** At t
20029 68 65 20 70 6f 69 6e 74 20 74 68 69 73 20 72 6f he point this ro
2002a 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c utine is called,
2002b 20 77 65 20 61 6c 72 65 61 64 79 20 6b 6e 6f 77 we already know
2002c 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 4f 52 44 that the.** ORD
2002d 45 52 20 42 59 20 74 65 72 6d 20 69 73 20 6e 6f ER BY term is no
2002e 74 20 61 6e 20 69 6e 74 65 67 65 72 20 69 6e 64 t an integer ind
2002f 65 78 20 69 6e 74 6f 20 74 68 65 20 72 65 73 75 ex into the resu
20030 6c 74 20 73 65 74 2e 20 20 54 68 61 74 0a 2a 2a lt set. That.**
20031 20 63 61 73 65 20 69 73 20 68 61 6e 64 6c 65 64 case is handled
20032 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 by the calling
20033 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 41 routine..**.** A
20034 74 74 65 6d 70 74 20 74 6f 20 6d 61 74 63 68 20 ttempt to match
20035 70 45 20 61 67 61 69 6e 73 74 20 72 65 73 75 6c pE against resul
20036 74 20 73 65 74 20 63 6f 6c 75 6d 6e 73 20 69 6e t set columns in
20037 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74 0a 2a the left-most.*
20038 2a 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 * SELECT stateme
20039 6e 74 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 nt. Return the
2003a 69 6e 64 65 78 20 69 20 6f 66 20 74 68 65 20 6d index i of the m
2003b 61 74 63 68 69 6e 67 20 63 6f 6c 75 6d 6e 2c 0a atching column,.
2003c 2a 2a 20 61 73 20 61 6e 20 69 6e 64 69 63 61 74 ** as an indicat
2003d 69 6f 6e 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 ion to the calle
2003e 72 20 74 68 61 74 20 69 74 20 73 68 6f 75 6c 64 r that it should
2003f 20 73 6f 72 74 20 62 79 20 74 68 65 20 69 2d 74 sort by the i-t
20040 68 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 20 54 68 65 h column..** The
20041 20 6c 65 66 74 2d 6d 6f 73 74 20 63 6f 6c 75 6d left-most colum
20042 6e 20 69 73 20 31 2e 20 20 49 6e 20 6f 74 68 65 n is 1. In othe
20043 72 20 77 6f 72 64 73 2c 20 74 68 65 20 76 61 6c r words, the val
20044 75 65 20 72 65 74 75 72 6e 65 64 20 69 73 20 74 ue returned is t
20045 68 65 0a 2a 2a 20 73 61 6d 65 20 69 6e 74 65 67 he.** same integ
20046 65 72 20 76 61 6c 75 65 20 74 68 61 74 20 77 6f er value that wo
20047 75 6c 64 20 62 65 20 75 73 65 64 20 69 6e 20 74 uld be used in t
20048 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 he SQL statement
20049 20 74 6f 20 69 6e 64 69 63 61 74 65 0a 2a 2a 20 to indicate.**
2004a 74 68 65 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a the column..**.*
2004b 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f * If there is no
2004c 20 6d 61 74 63 68 2c 20 72 65 74 75 72 6e 20 30 match, return 0
2004d 2e 20 20 52 65 74 75 72 6e 20 2d 31 20 69 66 20 . Return -1 if
2004e 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e an error occurs.
2004f 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 .*/.static int r
20050 65 73 6f 6c 76 65 4f 72 64 65 72 42 79 54 65 72 esolveOrderByTer
20051 6d 54 6f 45 78 70 72 4c 69 73 74 28 0a 20 20 50 mToExprList(. P
20052 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 arse *pParse,
20053 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e /* Parsing con
20054 74 65 78 74 20 66 6f 72 20 65 72 72 6f 72 20 6d text for error m
20055 65 73 73 61 67 65 73 20 2a 2f 0a 20 20 53 65 6c essages */. Sel
20056 65 63 74 20 2a 70 53 65 6c 65 63 74 2c 20 20 20 ect *pSelect,
20057 2f 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73 74 /* The SELECT st
20058 61 74 65 6d 65 6e 74 20 77 69 74 68 20 74 68 65 atement with the
20059 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 ORDER BY clause
2005a 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 20 20 */. Expr *pE
2005b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
2005c 73 70 65 63 69 66 69 63 20 4f 52 44 45 52 20 42 specific ORDER B
2005d 59 20 74 65 72 6d 20 2a 2f 0a 29 7b 0a 20 20 69 Y term */.){. i
2005e 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 nt i;
2005f 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 /* Loop counte
20060 72 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 r */. ExprList
20061 2a 70 45 4c 69 73 74 3b 20 20 2f 2a 20 54 68 65 *pEList; /* The
20062 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20 columns of the
20063 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20 result set */.
20064 4e 61 6d 65 43 6f 6e 74 65 78 74 20 6e 63 3b 20 NameContext nc;
20065 20 20 20 2f 2a 20 4e 61 6d 65 20 63 6f 6e 74 65 /* Name conte
20066 78 74 20 66 6f 72 20 72 65 73 6f 6c 76 69 6e 67 xt for resolving
20067 20 70 45 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 pE */.. assert
20068 28 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 49 ( sqlite3ExprIsI
20069 6e 74 65 67 65 72 28 70 45 2c 20 26 69 29 3d 3d nteger(pE, &i)==
2006a 30 20 29 3b 0a 20 20 70 45 4c 69 73 74 20 3d 20 0 );. pEList =
2006b 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 3b pSelect->pEList;
2006c 0a 0a 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 61 .. /* Resolve a
2006d 6c 6c 20 6e 61 6d 65 73 20 69 6e 20 74 68 65 20 ll names in the
2006e 4f 52 44 45 52 20 42 59 20 74 65 72 6d 20 65 78 ORDER BY term ex
2006f 70 72 65 73 73 69 6f 6e 0a 20 20 2a 2f 0a 20 20 pression. */.
20070 6d 65 6d 73 65 74 28 26 6e 63 2c 20 30 2c 20 73 memset(&nc, 0, s
20071 69 7a 65 6f 66 28 6e 63 29 29 3b 0a 20 20 6e 63 izeof(nc));. nc
20072 2e 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 .pParse = pParse
20073 3b 0a 20 20 6e 63 2e 70 53 72 63 4c 69 73 74 20 ;. nc.pSrcList
20074 3d 20 70 53 65 6c 65 63 74 2d 3e 70 53 72 63 3b = pSelect->pSrc;
20075 0a 20 20 6e 63 2e 70 45 4c 69 73 74 20 3d 20 70 . nc.pEList = p
20076 45 4c 69 73 74 3b 0a 20 20 6e 63 2e 61 6c 6c 6f EList;. nc.allo
20077 77 41 67 67 20 3d 20 31 3b 0a 20 20 6e 63 2e 6e wAgg = 1;. nc.n
20078 45 72 72 20 3d 20 30 3b 0a 20 20 69 66 28 20 73 Err = 0;. if( s
20079 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 qlite3ResolveExp
2007a 72 4e 61 6d 65 73 28 26 6e 63 2c 20 70 45 29 20 rNames(&nc, pE)
2007b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 ){. sqlite3Er
2007c 72 6f 72 43 6c 65 61 72 28 70 50 61 72 73 65 29 rorClear(pParse)
2007d 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a ;. return 0;.
2007e 20 20 7d 0a 0a 20 20 2f 2a 20 54 72 79 20 74 6f }.. /* Try to
2007f 20 6d 61 74 63 68 20 74 68 65 20 4f 52 44 45 52 match the ORDER
20080 20 42 59 20 65 78 70 72 65 73 73 69 6f 6e 20 61 BY expression a
20081 67 61 69 6e 73 74 20 61 6e 20 65 78 70 72 65 73 gainst an expres
20082 73 69 6f 6e 0a 20 20 2a 2a 20 69 6e 20 74 68 65 sion. ** in the
20083 20 72 65 73 75 6c 74 20 73 65 74 2e 20 20 52 65 result set. Re
20084 74 75 72 6e 20 61 6e 20 31 2d 62 61 73 65 64 20 turn an 1-based
20085 69 6e 64 65 78 20 6f 66 20 74 68 65 20 6d 61 74 index of the mat
20086 63 68 69 6e 67 0a 20 20 2a 2a 20 72 65 73 75 6c ching. ** resul
20087 74 2d 73 65 74 20 65 6e 74 72 79 2e 0a 20 20 2a t-set entry.. *
20088 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 /. for(i=0; i<p
20089 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b EList->nExpr; i+
2008a 2b 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 +){. if( sqli
2008b 74 65 33 45 78 70 72 43 6f 6d 70 61 72 65 28 70 te3ExprCompare(p
2008c 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 EList->a[i].pExp
2008d 72 2c 20 70 45 29 20 29 7b 0a 20 20 20 20 20 20 r, pE) ){.
2008e 72 65 74 75 72 6e 20 69 2b 31 3b 0a 20 20 20 20 return i+1;.
2008f 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 6e }. }.. /* If n
20090 6f 20 6d 61 74 63 68 2c 20 72 65 74 75 72 6e 20 o match, return
20091 30 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 30 0. */. return 0
20092 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 ;.}../*.** Gener
20093 61 74 65 20 61 6e 20 4f 52 44 45 52 20 42 59 20 ate an ORDER BY
20094 6f 72 20 47 52 4f 55 50 20 42 59 20 74 65 72 6d or GROUP BY term
20095 20 6f 75 74 2d 6f 66 2d 72 61 6e 67 65 20 65 72 out-of-range er
20096 72 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ror..*/.static v
20097 6f 69 64 20 72 65 73 6f 6c 76 65 4f 75 74 4f 66 oid resolveOutOf
20098 52 61 6e 67 65 45 72 72 6f 72 28 0a 20 20 50 61 RangeError(. Pa
20099 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 rse *pParse,
2009a 20 20 20 20 20 2f 2a 20 54 68 65 20 65 72 72 6f /* The erro
2009b 72 20 63 6f 6e 74 65 78 74 20 69 6e 74 6f 20 77 r context into w
2009c 68 69 63 68 20 74 6f 20 77 72 69 74 65 20 74 68 hich to write th
2009d 65 20 65 72 72 6f 72 20 2a 2f 0a 20 20 63 6f 6e e error */. con
2009e 73 74 20 63 68 61 72 20 2a 7a 54 79 70 65 2c 20 st char *zType,
2009f 20 20 20 20 2f 2a 20 22 4f 52 44 45 52 22 20 6f /* "ORDER" o
200a0 72 20 22 47 52 4f 55 50 22 20 2a 2f 0a 20 20 69 r "GROUP" */. i
200a1 6e 74 20 69 2c 20 20 20 20 20 20 20 20 20 20 20 nt i,
200a2 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69 6e 64 /* The ind
200a3 65 78 20 28 31 2d 62 61 73 65 64 29 20 6f 66 20 ex (1-based) of
200a4 74 68 65 20 74 65 72 6d 20 6f 75 74 20 6f 66 20 the term out of
200a5 72 61 6e 67 65 20 2a 2f 0a 20 20 69 6e 74 20 6d range */. int m
200a6 78 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 x
200a7 20 20 2f 2a 20 4c 61 72 67 65 73 74 20 70 65 72 /* Largest per
200a8 6d 69 73 73 69 62 6c 65 20 76 61 6c 75 65 20 6f missible value o
200a9 66 20 69 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 f i */.){. sqli
200aa 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 te3ErrorMsg(pPar
200ab 73 65 2c 20 0a 20 20 20 20 22 25 72 20 25 73 20 se, . "%r %s
200ac 42 59 20 74 65 72 6d 20 6f 75 74 20 6f 66 20 72 BY term out of r
200ad 61 6e 67 65 20 2d 20 73 68 6f 75 6c 64 20 62 65 ange - should be
200ae 20 22 0a 20 20 20 20 22 62 65 74 77 65 65 6e 20 ". "between
200af 31 20 61 6e 64 20 25 64 22 2c 20 69 2c 20 7a 54 1 and %d", i, zT
200b0 79 70 65 2c 20 6d 78 29 3b 0a 7d 0a 0a 2f 2a 0a ype, mx);.}../*.
200b1 2a 2a 20 41 6e 61 6c 79 7a 65 20 74 68 65 20 4f ** Analyze the O
200b2 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 69 RDER BY clause i
200b3 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c n a compound SEL
200b4 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 ECT statement.
200b5 20 4d 6f 64 69 66 79 0a 2a 2a 20 65 61 63 68 20 Modify.** each
200b6 74 65 72 6d 20 6f 66 20 74 68 65 20 4f 52 44 45 term of the ORDE
200b7 52 20 42 59 20 63 6c 61 75 73 65 20 69 73 20 61 R BY clause is a
200b8 20 63 6f 6e 73 74 61 6e 74 20 69 6e 74 65 67 65 constant intege
200b9 72 20 62 65 74 77 65 65 6e 20 31 0a 2a 2a 20 61 r between 1.** a
200ba 6e 64 20 4e 20 77 68 65 72 65 20 4e 20 69 73 20 nd N where N is
200bb 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f the number of co
200bc 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 63 6f 6d lumns in the com
200bd 70 6f 75 6e 64 20 53 45 4c 45 43 54 2e 0a 2a 2a pound SELECT..**
200be 0a 2a 2a 20 4f 52 44 45 52 20 42 59 20 74 65 72 .** ORDER BY ter
200bf 6d 73 20 74 68 61 74 20 61 72 65 20 61 6c 72 65 ms that are alre
200c0 61 64 79 20 61 6e 20 69 6e 74 65 67 65 72 20 62 ady an integer b
200c1 65 74 77 65 65 6e 20 31 20 61 6e 64 20 4e 20 61 etween 1 and N a
200c2 72 65 0a 2a 2a 20 75 6e 6d 6f 64 69 66 69 65 64 re.** unmodified
200c3 2e 20 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d . ORDER BY term
200c4 73 20 74 68 61 74 20 61 72 65 20 69 6e 74 65 67 s that are integ
200c5 65 72 73 20 6f 75 74 73 69 64 65 20 74 68 65 20 ers outside the
200c6 72 61 6e 67 65 20 6f 66 0a 2a 2a 20 31 20 74 68 range of.** 1 th
200c7 72 6f 75 67 68 20 4e 20 67 65 6e 65 72 61 74 65 rough N generate
200c8 20 61 6e 20 65 72 72 6f 72 2e 20 20 4f 52 44 45 an error. ORDE
200c9 52 20 42 59 20 74 65 72 6d 73 20 74 68 61 74 20 R BY terms that
200ca 61 72 65 20 65 78 70 72 65 73 73 69 6f 6e 73 0a are expressions.
200cb 2a 2a 20 61 72 65 20 6d 61 74 63 68 65 64 20 61 ** are matched a
200cc 67 61 69 6e 73 74 20 72 65 73 75 6c 74 20 73 65 gainst result se
200cd 74 20 65 78 70 72 65 73 73 69 6f 6e 73 20 6f 66 t expressions of
200ce 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 compound SELECT
200cf 0a 2a 2a 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 .** beginning wi
200d0 74 68 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74 th the left-most
200d1 20 53 45 4c 45 43 54 20 61 6e 64 20 77 6f 72 6b SELECT and work
200d2 69 6e 67 20 74 6f 77 61 72 64 20 74 68 65 20 72 ing toward the r
200d3 69 67 68 74 2e 0a 2a 2a 20 41 74 20 74 68 65 20 ight..** At the
200d4 66 69 72 73 74 20 6d 61 74 63 68 2c 20 74 68 65 first match, the
200d5 20 4f 52 44 45 52 20 42 59 20 65 78 70 72 65 73 ORDER BY expres
200d6 73 69 6f 6e 20 69 73 20 74 72 61 6e 73 66 6f 72 sion is transfor
200d7 6d 65 64 20 69 6e 74 6f 0a 2a 2a 20 74 68 65 20 med into.** the
200d8 69 6e 74 65 67 65 72 20 63 6f 6c 75 6d 6e 20 6e integer column n
200d9 75 6d 62 65 72 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 umber..**.** Ret
200da 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f urn the number o
200db 66 20 65 72 72 6f 72 73 20 73 65 65 6e 2e 0a 2a f errors seen..*
200dc 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 73 /.static int res
200dd 6f 6c 76 65 43 6f 6d 70 6f 75 6e 64 4f 72 64 65 olveCompoundOrde
200de 72 42 79 28 0a 20 20 50 61 72 73 65 20 2a 70 50 rBy(. Parse *pP
200df 61 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 arse, /*
200e0 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 2e Parsing context.
200e1 20 20 4c 65 61 76 65 20 65 72 72 6f 72 20 6d 65 Leave error me
200e2 73 73 61 67 65 73 20 68 65 72 65 20 2a 2f 0a 20 ssages here */.
200e3 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 Select *pSelect
200e4 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53 45 /* The SE
200e5 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 63 LECT statement c
200e6 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 4f 52 ontaining the OR
200e7 44 45 52 20 42 59 20 2a 2f 0a 29 7b 0a 20 20 69 DER BY */.){. i
200e8 6e 74 20 69 3b 0a 20 20 45 78 70 72 4c 69 73 74 nt i;. ExprList
200e9 20 2a 70 4f 72 64 65 72 42 79 3b 0a 20 20 45 78 *pOrderBy;. Ex
200ea 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 0a prList *pEList;.
200eb 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 sqlite3 *db;.
200ec 20 69 6e 74 20 6d 6f 72 65 54 6f 44 6f 20 3d 20 int moreToDo =
200ed 31 3b 0a 0a 20 20 70 4f 72 64 65 72 42 79 20 3d 1;.. pOrderBy =
200ee 20 70 53 65 6c 65 63 74 2d 3e 70 4f 72 64 65 72 pSelect->pOrder
200ef 42 79 3b 0a 20 20 69 66 28 20 70 4f 72 64 65 72 By;. if( pOrder
200f0 42 79 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 By==0 ) return 0
200f1 3b 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d ;. db = pParse-
200f2 3e 64 62 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f >db;.#if SQLITE_
200f3 4d 41 58 5f 43 4f 4c 55 4d 4e 0a 20 20 69 66 28 MAX_COLUMN. if(
200f4 20 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 pOrderBy->nExpr
200f5 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 >db->aLimit[SQLI
200f6 54 45 5f 4c 49 4d 49 54 5f 43 4f 4c 55 4d 4e 5d TE_LIMIT_COLUMN]
200f7 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 ){. sqlite3E
200f8 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 rrorMsg(pParse,
200f9 22 74 6f 6f 20 6d 61 6e 79 20 74 65 72 6d 73 20 "too many terms
200fa 69 6e 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 in ORDER BY clau
200fb 73 65 22 29 3b 0a 20 20 20 20 72 65 74 75 72 6e se");. return
200fc 20 31 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 1;. }.#endif.
200fd 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4f 72 64 for(i=0; i<pOrd
200fe 65 72 42 79 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b erBy->nExpr; i++
200ff 29 7b 0a 20 20 20 20 70 4f 72 64 65 72 42 79 2d ){. pOrderBy-
20100 3e 61 5b 69 5d 2e 64 6f 6e 65 20 3d 20 30 3b 0a >a[i].done = 0;.
20101 20 20 7d 0a 20 20 70 53 65 6c 65 63 74 2d 3e 70 }. pSelect->p
20102 4e 65 78 74 20 3d 20 30 3b 0a 20 20 77 68 69 6c Next = 0;. whil
20103 65 28 20 70 53 65 6c 65 63 74 2d 3e 70 50 72 69 e( pSelect->pPri
20104 6f 72 20 29 7b 0a 20 20 20 20 70 53 65 6c 65 63 or ){. pSelec
20105 74 2d 3e 70 50 72 69 6f 72 2d 3e 70 4e 65 78 74 t->pPrior->pNext
20106 20 3d 20 70 53 65 6c 65 63 74 3b 0a 20 20 20 20 = pSelect;.
20107 70 53 65 6c 65 63 74 20 3d 20 70 53 65 6c 65 63 pSelect = pSelec
20108 74 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 7d 0a 20 t->pPrior;. }.
20109 20 77 68 69 6c 65 28 20 70 53 65 6c 65 63 74 20 while( pSelect
2010a 26 26 20 6d 6f 72 65 54 6f 44 6f 20 29 7b 0a 20 && moreToDo ){.
2010b 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 struct ExprLi
2010c 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a st_item *pItem;.
2010d 20 20 20 20 6d 6f 72 65 54 6f 44 6f 20 3d 20 30 moreToDo = 0
2010e 3b 0a 20 20 20 20 70 45 4c 69 73 74 20 3d 20 70 ;. pEList = p
2010f 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 3b 0a Select->pEList;.
20110 20 20 20 20 61 73 73 65 72 74 28 20 70 45 4c 69 assert( pELi
20111 73 74 21 3d 30 20 29 3b 0a 20 20 20 20 66 6f 72 st!=0 );. for
20112 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70 4f 72 64 (i=0, pItem=pOrd
20113 65 72 42 79 2d 3e 61 3b 20 69 3c 70 4f 72 64 65 erBy->a; i<pOrde
20114 72 42 79 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c rBy->nExpr; i++,
20115 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 pItem++){.
20116 20 69 6e 74 20 69 43 6f 6c 20 3d 20 2d 31 3b 0a int iCol = -1;.
20117 20 20 20 20 20 20 45 78 70 72 20 2a 70 45 2c 20 Expr *pE,
20118 2a 70 44 75 70 3b 0a 20 20 20 20 20 20 69 66 28 *pDup;. if(
20119 20 70 49 74 65 6d 2d 3e 64 6f 6e 65 20 29 20 63 pItem->done ) c
2011a 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 70 ontinue;. p
2011b 45 20 3d 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 E = pItem->pExpr
2011c 3b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 ;. if( sqli
2011d 74 65 33 45 78 70 72 49 73 49 6e 74 65 67 65 72 te3ExprIsInteger
2011e 28 70 45 2c 20 26 69 43 6f 6c 29 20 29 7b 0a 20 (pE, &iCol) ){.
2011f 20 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c 3c if( iCol<
20120 3d 30 20 7c 7c 20 69 43 6f 6c 3e 70 45 4c 69 73 =0 || iCol>pELis
20121 74 2d 3e 6e 45 78 70 72 20 29 7b 0a 20 20 20 20 t->nExpr ){.
20122 20 20 20 20 20 20 72 65 73 6f 6c 76 65 4f 75 74 resolveOut
20123 4f 66 52 61 6e 67 65 45 72 72 6f 72 28 70 50 61 OfRangeError(pPa
20124 72 73 65 2c 20 22 4f 52 44 45 52 22 2c 20 69 2b rse, "ORDER", i+
20125 31 2c 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 1, pEList->nExpr
20126 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 );. ret
20127 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 20 20 7d urn 1;. }
20128 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
20129 20 20 20 20 20 20 69 43 6f 6c 20 3d 20 72 65 73 iCol = res
2012a 6f 6c 76 65 41 73 4e 61 6d 65 28 70 50 61 72 73 olveAsName(pPars
2012b 65 2c 20 70 45 4c 69 73 74 2c 20 70 45 29 3b 0a e, pEList, pE);.
2012c 20 20 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c if( iCol
2012d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ==0 ){.
2012e 20 70 44 75 70 20 3d 20 73 71 6c 69 74 65 33 45 pDup = sqlite3E
2012f 78 70 72 44 75 70 28 64 62 2c 20 70 45 2c 20 30 xprDup(db, pE, 0
20130 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 );. if(
20131 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c !db->mallocFail
20132 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 ed ){.
20133 20 20 61 73 73 65 72 74 28 70 44 75 70 29 3b 0a assert(pDup);.
20134 20 20 20 20 20 20 20 20 20 20 20 20 69 43 6f 6c iCol
20135 20 3d 20 72 65 73 6f 6c 76 65 4f 72 64 65 72 42 = resolveOrderB
20136 79 54 65 72 6d 54 6f 45 78 70 72 4c 69 73 74 28 yTermToExprList(
20137 70 50 61 72 73 65 2c 20 70 53 65 6c 65 63 74 2c pParse, pSelect,
20138 20 70 44 75 70 29 3b 0a 20 20 20 20 20 20 20 20 pDup);.
20139 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 73 71 }. sq
2013a 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 lite3ExprDelete(
2013b 64 62 2c 20 70 44 75 70 29 3b 0a 20 20 20 20 20 db, pDup);.
2013c 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
2013d 20 20 20 69 66 28 20 69 43 6f 6c 3e 30 20 29 7b if( iCol>0 ){
2013e 0a 20 20 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 . CollSeq
2013f 20 2a 70 43 6f 6c 6c 20 3d 20 70 45 2d 3e 70 43 *pColl = pE->pC
20140 6f 6c 6c 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 oll;. int
20141 20 66 6c 61 67 73 20 3d 20 70 45 2d 3e 66 6c 61 flags = pE->fla
20142 67 73 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61 gs & EP_ExpColla
20143 74 65 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 te;. sqli
20144 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 te3ExprDelete(db
20145 2c 20 70 45 29 3b 0a 20 20 20 20 20 20 20 20 70 , pE);. p
20146 49 74 65 6d 2d 3e 70 45 78 70 72 20 3d 20 70 45 Item->pExpr = pE
20147 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 28 64 = sqlite3Expr(d
20148 62 2c 20 54 4b 5f 49 4e 54 45 47 45 52 2c 20 30 b, TK_INTEGER, 0
20149 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 );. if( p
2014a 45 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 31 3b E==0 ) return 1;
2014b 0a 20 20 20 20 20 20 20 20 70 45 2d 3e 70 43 6f . pE->pCo
2014c 6c 6c 20 3d 20 70 43 6f 6c 6c 3b 0a 20 20 20 20 ll = pColl;.
2014d 20 20 20 20 70 45 2d 3e 66 6c 61 67 73 20 7c 3d pE->flags |=
2014e 20 45 50 5f 49 6e 74 56 61 6c 75 65 20 7c 20 66 EP_IntValue | f
2014f 6c 61 67 73 3b 0a 20 20 20 20 20 20 20 20 70 45 lags;. pE
20150 2d 3e 75 2e 69 56 61 6c 75 65 20 3d 20 69 43 6f ->u.iValue = iCo
20151 6c 3b 0a 20 20 20 20 20 20 20 20 70 49 74 65 6d l;. pItem
20152 2d 3e 69 43 6f 6c 20 3d 20 28 75 31 36 29 69 43 ->iCol = (u16)iC
20153 6f 6c 3b 0a 20 20 20 20 20 20 20 20 70 49 74 65 ol;. pIte
20154 6d 2d 3e 64 6f 6e 65 20 3d 20 31 3b 0a 20 20 20 m->done = 1;.
20155 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
20156 20 20 6d 6f 72 65 54 6f 44 6f 20 3d 20 31 3b 0a moreToDo = 1;.
20157 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
20158 20 20 70 53 65 6c 65 63 74 20 3d 20 70 53 65 6c pSelect = pSel
20159 65 63 74 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a ect->pNext;. }.
2015a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4f 72 for(i=0; i<pOr
2015b 64 65 72 42 79 2d 3e 6e 45 78 70 72 3b 20 69 2b derBy->nExpr; i+
2015c 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 4f 72 64 +){. if( pOrd
2015d 65 72 42 79 2d 3e 61 5b 69 5d 2e 64 6f 6e 65 3d erBy->a[i].done=
2015e 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 =0 ){. sqli
2015f 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 te3ErrorMsg(pPar
20160 73 65 2c 20 22 25 72 20 4f 52 44 45 52 20 42 59 se, "%r ORDER BY
20161 20 74 65 72 6d 20 64 6f 65 73 20 6e 6f 74 20 6d term does not m
20162 61 74 63 68 20 61 6e 79 20 22 0a 20 20 20 20 20 atch any ".
20163 20 20 20 20 20 20 20 22 63 6f 6c 75 6d 6e 20 69 "column i
20164 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 n the result set
20165 22 2c 20 69 2b 31 29 3b 0a 20 20 20 20 20 20 72 ", i+1);. r
20166 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 eturn 1;. }.
20167 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d }. return 0;.}
20168 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 65 76 ../*.** Check ev
20169 65 72 79 20 74 65 72 6d 20 69 6e 20 74 68 65 20 ery term in the
2016a 4f 52 44 45 52 20 42 59 20 6f 72 20 47 52 4f 55 ORDER BY or GROU
2016b 50 20 42 59 20 63 6c 61 75 73 65 20 70 4f 72 64 P BY clause pOrd
2016c 65 72 42 79 20 6f 66 0a 2a 2a 20 74 68 65 20 53 erBy of.** the S
2016d 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 ELECT statement
2016e 70 53 65 6c 65 63 74 2e 20 20 49 66 20 61 6e 79 pSelect. If any
2016f 20 74 65 72 6d 20 69 73 20 72 65 66 65 72 65 6e term is referen
20170 63 65 20 74 6f 20 61 0a 2a 2a 20 72 65 73 75 6c ce to a.** resul
20171 74 20 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e t set expression
20172 20 28 61 73 20 64 65 74 65 72 6d 69 6e 65 64 20 (as determined
20173 62 79 20 74 68 65 20 45 78 70 72 4c 69 73 74 2e by the ExprList.
20174 61 2e 69 43 6f 6c 20 66 69 65 6c 64 29 0a 2a 2a a.iCol field).**
20175 20 74 68 65 6e 20 63 6f 6e 76 65 72 74 20 74 68 then convert th
20176 61 74 20 74 65 72 6d 20 69 6e 74 6f 20 61 20 63 at term into a c
20177 6f 70 79 20 6f 66 20 74 68 65 20 63 6f 72 72 65 opy of the corre
20178 73 70 6f 6e 64 69 6e 67 20 72 65 73 75 6c 74 20 sponding result
20179 73 65 74 0a 2a 2a 20 63 6f 6c 75 6d 6e 2e 0a 2a set.** column..*
2017a 2a 0a 2a 2a 20 49 66 20 61 6e 79 20 65 72 72 6f *.** If any erro
2017b 72 73 20 61 72 65 20 64 65 74 65 63 74 65 64 2c rs are detected,
2017c 20 61 64 64 20 61 6e 20 65 72 72 6f 72 20 6d 65 add an error me
2017d 73 73 61 67 65 20 74 6f 20 70 50 61 72 73 65 20 ssage to pParse
2017e 61 6e 64 0a 2a 2a 20 72 65 74 75 72 6e 20 6e 6f and.** return no
2017f 6e 2d 7a 65 72 6f 2e 20 20 52 65 74 75 72 6e 20 n-zero. Return
20180 7a 65 72 6f 20 69 66 20 6e 6f 20 65 72 72 6f 72 zero if no error
20181 73 20 61 72 65 20 73 65 65 6e 2e 0a 2a 2f 0a 53 s are seen..*/.S
20182 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
20183 74 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 t sqlite3Resolve
20184 4f 72 64 65 72 47 72 6f 75 70 42 79 28 0a 20 20 OrderGroupBy(.
20185 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 Parse *pParse,
20186 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 /* Parsing
20187 20 63 6f 6e 74 65 78 74 2e 20 20 4c 65 61 76 65 context. Leave
20188 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 error messages
20189 68 65 72 65 20 2a 2f 0a 20 20 53 65 6c 65 63 74 here */. Select
2018a 20 2a 70 53 65 6c 65 63 74 2c 20 20 20 20 20 20 *pSelect,
2018b 2f 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73 74 /* The SELECT st
2018c 61 74 65 6d 65 6e 74 20 63 6f 6e 74 61 69 6e 69 atement containi
2018d 6e 67 20 74 68 65 20 63 6c 61 75 73 65 20 2a 2f ng the clause */
2018e 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 . ExprList *pOr
2018f 64 65 72 42 79 2c 20 20 20 2f 2a 20 54 68 65 20 derBy, /* The
20190 4f 52 44 45 52 20 42 59 20 6f 72 20 47 52 4f 55 ORDER BY or GROU
20191 50 20 42 59 20 63 6c 61 75 73 65 20 74 6f 20 62 P BY clause to b
20192 65 20 70 72 6f 63 65 73 73 65 64 20 2a 2f 0a 20 e processed */.
20193 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 79 const char *zTy
20194 70 65 20 20 20 20 20 2f 2a 20 22 4f 52 44 45 52 pe /* "ORDER
20195 22 20 6f 72 20 22 47 52 4f 55 50 22 20 2a 2f 0a " or "GROUP" */.
20196 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 71 ){. int i;. sq
20197 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 lite3 *db = pPar
20198 73 65 2d 3e 64 62 3b 0a 20 20 45 78 70 72 4c 69 se->db;. ExprLi
20199 73 74 20 2a 70 45 4c 69 73 74 3b 0a 20 20 73 74 st *pEList;. st
2019a 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 ruct ExprList_it
2019b 65 6d 20 2a 70 49 74 65 6d 3b 0a 0a 20 20 69 66 em *pItem;.. if
2019c 28 20 70 4f 72 64 65 72 42 79 3d 3d 30 20 7c 7c ( pOrderBy==0 ||
2019d 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c pParse->db->mal
2019e 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 65 74 75 locFailed ) retu
2019f 72 6e 20 30 3b 0a 23 69 66 20 53 51 4c 49 54 45 rn 0;.#if SQLITE
201a0 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 0a 20 20 69 66 _MAX_COLUMN. if
201a1 28 20 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 ( pOrderBy->nExp
201a2 72 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c r>db->aLimit[SQL
201a3 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4c 55 4d 4e ITE_LIMIT_COLUMN
201a4 5d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 ] ){. sqlite3
201a5 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c ErrorMsg(pParse,
201a6 20 22 74 6f 6f 20 6d 61 6e 79 20 74 65 72 6d 73 "too many terms
201a7 20 69 6e 20 25 73 20 42 59 20 63 6c 61 75 73 65 in %s BY clause
201a8 22 2c 20 7a 54 79 70 65 29 3b 0a 20 20 20 20 72 ", zType);. r
201a9 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 23 65 6e eturn 1;. }.#en
201aa 64 69 66 0a 20 20 70 45 4c 69 73 74 20 3d 20 70 dif. pEList = p
201ab 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 3b 0a Select->pEList;.
201ac 20 20 61 73 73 65 72 74 28 20 70 45 4c 69 73 74 assert( pEList
201ad 21 3d 30 20 29 3b 20 20 2f 2a 20 73 71 6c 69 74 !=0 ); /* sqlit
201ae 65 33 53 65 6c 65 63 74 4e 65 77 28 29 20 67 75 e3SelectNew() gu
201af 61 72 61 6e 74 65 65 73 20 74 68 69 73 20 2a 2f arantees this */
201b0 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 49 74 65 . for(i=0, pIte
201b1 6d 3d 70 4f 72 64 65 72 42 79 2d 3e 61 3b 20 69 m=pOrderBy->a; i
201b2 3c 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 <pOrderBy->nExpr
201b3 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b ; i++, pItem++){
201b4 0a 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e . if( pItem->
201b5 69 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 69 66 iCol ){. if
201b6 28 20 70 49 74 65 6d 2d 3e 69 43 6f 6c 3e 70 45 ( pItem->iCol>pE
201b7 4c 69 73 74 2d 3e 6e 45 78 70 72 20 29 7b 0a 20 List->nExpr ){.
201b8 20 20 20 20 20 20 20 72 65 73 6f 6c 76 65 4f 75 resolveOu
201b9 74 4f 66 52 61 6e 67 65 45 72 72 6f 72 28 70 50 tOfRangeError(pP
201ba 61 72 73 65 2c 20 7a 54 79 70 65 2c 20 69 2b 31 arse, zType, i+1
201bb 2c 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 29 , pEList->nExpr)
201bc 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e ;. return
201bd 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 1;. }.
201be 20 20 72 65 73 6f 6c 76 65 41 6c 69 61 73 28 70 resolveAlias(p
201bf 50 61 72 73 65 2c 20 70 45 4c 69 73 74 2c 20 70 Parse, pEList, p
201c0 49 74 65 6d 2d 3e 69 43 6f 6c 2d 31 2c 20 70 49 Item->iCol-1, pI
201c1 74 65 6d 2d 3e 70 45 78 70 72 2c 20 7a 54 79 70 tem->pExpr, zTyp
201c2 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 e);. }. }.
201c3 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a return 0;.}../*.
201c4 2a 2a 20 70 4f 72 64 65 72 42 79 20 69 73 20 61 ** pOrderBy is a
201c5 6e 20 4f 52 44 45 52 20 42 59 20 6f 72 20 47 52 n ORDER BY or GR
201c6 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20 69 6e OUP BY clause in
201c7 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e SELECT statemen
201c8 74 20 70 53 65 6c 65 63 74 2e 0a 2a 2a 20 54 68 t pSelect..** Th
201c9 65 20 4e 61 6d 65 20 63 6f 6e 74 65 78 74 20 6f e Name context o
201ca 66 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61 f the SELECT sta
201cb 74 65 6d 65 6e 74 20 69 73 20 70 4e 43 2e 20 20 tement is pNC.
201cc 7a 54 79 70 65 20 69 73 20 65 69 74 68 65 72 0a zType is either.
201cd 2a 2a 20 22 4f 52 44 45 52 22 20 6f 72 20 22 47 ** "ORDER" or "G
201ce 52 4f 55 50 22 20 64 65 70 65 6e 64 69 6e 67 20 ROUP" depending
201cf 6f 6e 20 77 68 69 63 68 20 74 79 70 65 20 6f 66 on which type of
201d0 20 63 6c 61 75 73 65 20 70 4f 72 64 65 72 42 79 clause pOrderBy
201d1 20 69 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 is..**.** This
201d2 72 6f 75 74 69 6e 65 20 72 65 73 6f 6c 76 65 73 routine resolves
201d3 20 65 61 63 68 20 74 65 72 6d 20 6f 66 20 74 68 each term of th
201d4 65 20 63 6c 61 75 73 65 20 69 6e 74 6f 20 61 6e e clause into an
201d5 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a 20 expression..**
201d6 49 66 20 74 68 65 20 6f 72 64 65 72 2d 62 79 20 If the order-by
201d7 74 65 72 6d 20 69 73 20 61 6e 20 69 6e 74 65 67 term is an integ
201d8 65 72 20 49 20 62 65 74 77 65 65 6e 20 31 20 61 er I between 1 a
201d9 6e 64 20 4e 20 28 77 68 65 72 65 20 4e 20 69 73 nd N (where N is
201da 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f the.** number o
201db 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 f columns in the
201dc 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20 74 result set of t
201dd 68 65 20 53 45 4c 45 43 54 29 20 74 68 65 6e 20 he SELECT) then
201de 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a the expression.*
201df 2a 20 69 6e 20 74 68 65 20 72 65 73 6f 6c 75 74 * in the resolut
201e0 69 6f 6e 20 69 73 20 61 20 63 6f 70 79 20 6f 66 ion is a copy of
201e1 20 74 68 65 20 49 2d 74 68 20 72 65 73 75 6c 74 the I-th result
201e2 2d 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e 2e -set expression.
201e3 20 20 49 66 0a 2a 2a 20 74 68 65 20 6f 72 64 65 If.** the orde
201e4 72 2d 62 79 20 74 65 72 6d 20 69 73 20 61 6e 20 r-by term is an
201e5 69 64 65 6e 74 69 66 79 20 74 68 61 74 20 63 6f identify that co
201e6 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 74 68 65 rresponds to the
201e7 20 41 53 2d 6e 61 6d 65 20 6f 66 0a 2a 2a 20 61 AS-name of.** a
201e8 20 72 65 73 75 6c 74 2d 73 65 74 20 65 78 70 72 result-set expr
201e9 65 73 73 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 ession, then the
201ea 20 74 65 72 6d 20 72 65 73 6f 6c 76 65 73 20 74 term resolves t
201eb 6f 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 0a o a copy of the.
201ec 2a 2a 20 72 65 73 75 6c 74 2d 73 65 74 20 65 78 ** result-set ex
201ed 70 72 65 73 73 69 6f 6e 2e 20 20 4f 74 68 65 72 pression. Other
201ee 77 69 73 65 2c 20 74 68 65 20 65 78 70 72 65 73 wise, the expres
201ef 73 69 6f 6e 20 69 73 20 72 65 73 6f 6c 76 65 64 sion is resolved
201f0 20 69 6e 0a 2a 2a 20 74 68 65 20 75 73 75 61 6c in.** the usual
201f1 20 77 61 79 20 2d 20 75 73 69 6e 67 20 73 71 6c way - using sql
201f2 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e ite3ResolveExprN
201f3 61 6d 65 73 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 ames()..**.** Th
201f4 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 is routine retur
201f5 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 ns the number of
201f6 20 65 72 72 6f 72 73 2e 20 20 49 66 20 65 72 72 errors. If err
201f7 6f 72 73 20 6f 63 63 75 72 2c 20 74 68 65 6e 0a ors occur, then.
201f8 2a 2a 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74 ** an appropriat
201f9 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 e error message
201fa 6d 69 67 68 74 20 62 65 20 6c 65 66 74 20 69 6e might be left in
201fb 20 70 50 61 72 73 65 2e 20 20 28 4f 4f 4d 20 65 pParse. (OOM e
201fc 72 72 6f 72 73 0a 2a 2a 20 65 78 63 65 70 74 65 rrors.** excepte
201fd 64 2e 29 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e d.).*/.static in
201fe 74 20 72 65 73 6f 6c 76 65 4f 72 64 65 72 47 72 t resolveOrderGr
201ff 6f 75 70 42 79 28 0a 20 20 4e 61 6d 65 43 6f 6e oupBy(. NameCon
20200 74 65 78 74 20 2a 70 4e 43 2c 20 20 20 20 20 2f text *pNC, /
20201 2a 20 54 68 65 20 6e 61 6d 65 20 63 6f 6e 74 65 * The name conte
20202 78 74 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 xt of the SELECT
20203 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 statement */.
20204 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 2c Select *pSelect,
20205 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53 45 4c /* The SEL
20206 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 68 6f ECT statement ho
20207 6c 64 69 6e 67 20 70 4f 72 64 65 72 42 79 20 2a lding pOrderBy *
20208 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f /. ExprList *pO
20209 72 64 65 72 42 79 2c 20 20 20 2f 2a 20 41 6e 20 rderBy, /* An
2020a 4f 52 44 45 52 20 42 59 20 6f 72 20 47 52 4f 55 ORDER BY or GROU
2020b 50 20 42 59 20 63 6c 61 75 73 65 20 74 6f 20 72 P BY clause to r
2020c 65 73 6f 6c 76 65 20 2a 2f 0a 20 20 63 6f 6e 73 esolve */. cons
2020d 74 20 63 68 61 72 20 2a 7a 54 79 70 65 20 20 20 t char *zType
2020e 20 20 2f 2a 20 45 69 74 68 65 72 20 22 4f 52 44 /* Either "ORD
2020f 45 52 22 20 6f 72 20 22 47 52 4f 55 50 22 2c 20 ER" or "GROUP",
20210 61 73 20 61 70 70 72 6f 70 72 69 61 74 65 20 2a as appropriate *
20211 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 /.){. int i;
20212 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20213 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f /* Loop co
20214 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 69 unter */. int i
20215 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 Col;
20216 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c /* Col
20217 75 6d 6e 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 umn number */.
20218 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f struct ExprList_
20219 69 74 65 6d 20 2a 70 49 74 65 6d 3b 20 20 20 2f item *pItem; /
2021a 2a 20 41 20 74 65 72 6d 20 6f 66 20 74 68 65 20 * A term of the
2021b 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 ORDER BY clause
2021c 2a 2f 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 */. Parse *pPar
2021d 73 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 se;
2021e 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 /* Parsing c
2021f 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 ontext */. int
20220 6e 52 65 73 75 6c 74 3b 20 20 20 20 20 20 20 20 nResult;
20221 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
20222 6d 62 65 72 20 6f 66 20 74 65 72 6d 73 20 69 6e mber of terms in
20223 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 the result set
20224 2a 2f 0a 0a 20 20 69 66 28 20 70 4f 72 64 65 72 */.. if( pOrder
20225 42 79 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 By==0 ) return 0
20226 3b 0a 20 20 6e 52 65 73 75 6c 74 20 3d 20 70 53 ;. nResult = pS
20227 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 2d 3e 6e elect->pEList->n
20228 45 78 70 72 3b 0a 20 20 70 50 61 72 73 65 20 3d Expr;. pParse =
20229 20 70 4e 43 2d 3e 70 50 61 72 73 65 3b 0a 20 20 pNC->pParse;.
2022a 66 6f 72 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70 for(i=0, pItem=p
2022b 4f 72 64 65 72 42 79 2d 3e 61 3b 20 69 3c 70 4f OrderBy->a; i<pO
2022c 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 3b 20 69 rderBy->nExpr; i
2022d 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 ++, pItem++){.
2022e 20 20 45 78 70 72 20 2a 70 45 20 3d 20 70 49 74 Expr *pE = pIt
2022f 65 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20 69 em->pExpr;. i
20230 43 6f 6c 20 3d 20 72 65 73 6f 6c 76 65 41 73 4e Col = resolveAsN
20231 61 6d 65 28 70 50 61 72 73 65 2c 20 70 53 65 6c ame(pParse, pSel
20232 65 63 74 2d 3e 70 45 4c 69 73 74 2c 20 70 45 29 ect->pEList, pE)
20233 3b 0a 20 20 20 20 69 66 28 20 69 43 6f 6c 3e 30 ;. if( iCol>0
20234 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 ){. /* If
20235 61 6e 20 41 53 2d 6e 61 6d 65 20 6d 61 74 63 68 an AS-name match
20236 20 69 73 20 66 6f 75 6e 64 2c 20 6d 61 72 6b 20 is found, mark
20237 74 68 69 73 20 4f 52 44 45 52 20 42 59 20 63 6f this ORDER BY co
20238 6c 75 6d 6e 20 61 73 20 62 65 69 6e 67 0a 20 20 lumn as being.
20239 20 20 20 20 2a 2a 20 61 20 63 6f 70 79 20 6f 66 ** a copy of
2023a 20 74 68 65 20 69 43 6f 6c 2d 74 68 20 72 65 73 the iCol-th res
2023b 75 6c 74 2d 73 65 74 20 63 6f 6c 75 6d 6e 2e 20 ult-set column.
2023c 20 54 68 65 20 73 75 62 73 65 71 75 65 6e 74 20 The subsequent
2023d 63 61 6c 6c 20 74 6f 0a 20 20 20 20 20 20 2a 2a call to. **
2023e 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 4f sqlite3ResolveO
2023f 72 64 65 72 47 72 6f 75 70 42 79 28 29 20 77 69 rderGroupBy() wi
20240 6c 6c 20 63 6f 6e 76 65 72 74 20 74 68 65 20 65 ll convert the e
20241 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 61 0a 20 xpression to a.
20242 20 20 20 20 20 2a 2a 20 63 6f 70 79 20 6f 66 20 ** copy of
20243 74 68 65 20 69 43 6f 6c 2d 74 68 20 72 65 73 75 the iCol-th resu
20244 6c 74 2d 73 65 74 20 65 78 70 72 65 73 73 69 6f lt-set expressio
20245 6e 2e 20 2a 2f 0a 20 20 20 20 20 20 70 49 74 65 n. */. pIte
20246 6d 2d 3e 69 43 6f 6c 20 3d 20 28 75 31 36 29 69 m->iCol = (u16)i
20247 43 6f 6c 3b 0a 20 20 20 20 20 20 63 6f 6e 74 69 Col;. conti
20248 6e 75 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 nue;. }. i
20249 66 28 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 f( sqlite3ExprIs
2024a 49 6e 74 65 67 65 72 28 70 45 2c 20 26 69 43 6f Integer(pE, &iCo
2024b 6c 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 l) ){. /* T
2024c 68 65 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d he ORDER BY term
2024d 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 63 is an integer c
2024e 6f 6e 73 74 61 6e 74 2e 20 20 41 67 61 69 6e 2c onstant. Again,
2024f 20 73 65 74 20 74 68 65 20 63 6f 6c 75 6d 6e 0a set the column.
20250 20 20 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20 ** number
20251 73 6f 20 74 68 61 74 20 73 71 6c 69 74 65 33 52 so that sqlite3R
20252 65 73 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 70 esolveOrderGroup
20253 42 79 28 29 20 77 69 6c 6c 20 63 6f 6e 76 65 72 By() will conver
20254 74 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 6f t the. ** o
20255 72 64 65 72 2d 62 79 20 74 65 72 6d 20 74 6f 20 rder-by term to
20256 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 72 65 a copy of the re
20257 73 75 6c 74 2d 73 65 74 20 65 78 70 72 65 73 73 sult-set express
20258 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 ion */. if(
20259 20 69 43 6f 6c 3c 31 20 29 7b 0a 20 20 20 20 20 iCol<1 ){.
2025a 20 20 20 72 65 73 6f 6c 76 65 4f 75 74 4f 66 52 resolveOutOfR
2025b 61 6e 67 65 45 72 72 6f 72 28 70 50 61 72 73 65 angeError(pParse
2025c 2c 20 7a 54 79 70 65 2c 20 69 2b 31 2c 20 6e 52 , zType, i+1, nR
2025d 65 73 75 6c 74 29 3b 0a 20 20 20 20 20 20 20 20 esult);.
2025e 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 return 1;.
2025f 7d 0a 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 69 }. pItem->i
20260 43 6f 6c 20 3d 20 28 75 31 36 29 69 43 6f 6c 3b Col = (u16)iCol;
20261 0a 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b . continue;
20262 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 4f . }.. /* O
20263 74 68 65 72 77 69 73 65 2c 20 74 72 65 61 74 20 therwise, treat
20264 74 68 65 20 4f 52 44 45 52 20 42 59 20 74 65 72 the ORDER BY ter
20265 6d 20 61 73 20 61 6e 20 6f 72 64 69 6e 61 72 79 m as an ordinary
20266 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 expression */.
20267 20 20 20 70 49 74 65 6d 2d 3e 69 43 6f 6c 20 3d pItem->iCol =
20268 20 30 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 0;. if( sqli
20269 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 te3ResolveExprNa
2026a 6d 65 73 28 70 4e 43 2c 20 70 45 29 20 29 7b 0a mes(pNC, pE) ){.
2026b 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a return 1;.
2026c 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
2026d 72 6e 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 rn sqlite3Resolv
2026e 65 4f 72 64 65 72 47 72 6f 75 70 42 79 28 70 50 eOrderGroupBy(pP
2026f 61 72 73 65 2c 20 70 53 65 6c 65 63 74 2c 20 70 arse, pSelect, p
20270 4f 72 64 65 72 42 79 2c 20 7a 54 79 70 65 29 3b OrderBy, zType);
20271 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 6f 6c 76 .}../*.** Resolv
20272 65 20 6e 61 6d 65 73 20 69 6e 20 74 68 65 20 53 e names in the S
20273 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 ELECT statement
20274 70 20 61 6e 64 20 61 6c 6c 20 6f 66 20 69 74 73 p and all of its
20275 20 64 65 73 63 65 6e 64 65 6e 74 73 2e 0a 2a 2f descendents..*/
20276 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 73 6f .static int reso
20277 6c 76 65 53 65 6c 65 63 74 53 74 65 70 28 57 61 lveSelectStep(Wa
20278 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 53 lker *pWalker, S
20279 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20 4e 61 6d elect *p){. Nam
2027a 65 43 6f 6e 74 65 78 74 20 2a 70 4f 75 74 65 72 eContext *pOuter
2027b 4e 43 3b 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20 NC; /* Context
2027c 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 that contains th
2027d 69 73 20 53 45 4c 45 43 54 20 2a 2f 0a 20 20 4e is SELECT */. N
2027e 61 6d 65 43 6f 6e 74 65 78 74 20 73 4e 43 3b 20 ameContext sNC;
2027f 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 63 /* Name c
20280 6f 6e 74 65 78 74 20 6f 66 20 74 68 69 73 20 53 ontext of this S
20281 45 4c 45 43 54 20 2a 2f 0a 20 20 69 6e 74 20 69 ELECT */. int i
20282 73 43 6f 6d 70 6f 75 6e 64 3b 20 20 20 20 20 20 sCompound;
20283 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 70 20 /* True if p
20284 69 73 20 61 20 63 6f 6d 70 6f 75 6e 64 20 73 65 is a compound se
20285 6c 65 63 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 lect */. int nC
20286 6f 6d 70 6f 75 6e 64 3b 20 20 20 20 20 20 20 20 ompound;
20287 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 /* Number of c
20288 6f 6d 70 6f 75 6e 64 20 74 65 72 6d 73 20 70 72 ompound terms pr
20289 6f 63 65 73 73 65 64 20 73 6f 20 66 61 72 20 2a ocessed so far *
2028a 2f 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 /. Parse *pPars
2028b 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 e; /* P
2028c 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a arsing context *
2028d 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 /. ExprList *pE
2028e 4c 69 73 74 3b 20 20 20 20 20 20 20 2f 2a 20 52 List; /* R
2028f 65 73 75 6c 74 20 73 65 74 20 65 78 70 72 65 73 esult set expres
20290 73 69 6f 6e 20 6c 69 73 74 20 2a 2f 0a 20 20 69 sion list */. i
20291 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 nt i;
20292 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 /* Loop c
20293 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 45 78 70 72 ounter */. Expr
20294 4c 69 73 74 20 2a 70 47 72 6f 75 70 42 79 3b 20 List *pGroupBy;
20295 20 20 20 20 2f 2a 20 54 68 65 20 47 52 4f 55 50 /* The GROUP
20296 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 BY clause */.
20297 53 65 6c 65 63 74 20 2a 70 4c 65 66 74 6d 6f 73 Select *pLeftmos
20298 74 3b 20 20 20 20 20 20 2f 2a 20 4c 65 66 74 2d t; /* Left-
20299 6d 6f 73 74 20 6f 66 20 53 45 4c 45 43 54 20 6f most of SELECT o
2029a 66 20 61 20 63 6f 6d 70 6f 75 6e 64 20 2a 2f 0a f a compound */.
2029b 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 sqlite3 *db;
2029c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 /* Dat
2029d 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
2029e 20 2a 2f 0a 20 20 0a 0a 20 20 61 73 73 65 72 74 */. .. assert
2029f 28 20 70 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 ( p!=0 );. if(
202a0 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46 p->selFlags & SF
202a1 5f 52 65 73 6f 6c 76 65 64 20 29 7b 0a 20 20 20 _Resolved ){.
202a2 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72 75 6e return WRC_Prun
202a3 65 3b 0a 20 20 7d 0a 20 20 70 4f 75 74 65 72 4e e;. }. pOuterN
202a4 43 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 70 C = pWalker->u.p
202a5 4e 43 3b 0a 20 20 70 50 61 72 73 65 20 3d 20 70 NC;. pParse = p
202a6 57 61 6c 6b 65 72 2d 3e 70 50 61 72 73 65 3b 0a Walker->pParse;.
202a7 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 db = pParse->d
202a8 62 3b 0a 0a 20 20 2f 2a 20 4e 6f 72 6d 61 6c 6c b;.. /* Normall
202a9 79 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 45 y sqlite3SelectE
202aa 78 70 61 6e 64 28 29 20 77 69 6c 6c 20 62 65 20 xpand() will be
202ab 63 61 6c 6c 65 64 20 66 69 72 73 74 20 61 6e 64 called first and
202ac 20 77 69 6c 6c 20 68 61 76 65 0a 20 20 2a 2a 20 will have. **
202ad 61 6c 72 65 61 64 79 20 65 78 70 61 6e 64 65 64 already expanded
202ae 20 74 68 69 73 20 53 45 4c 45 43 54 2e 20 20 48 this SELECT. H
202af 6f 77 65 76 65 72 2c 20 69 66 20 74 68 69 73 20 owever, if this
202b0 69 73 20 61 20 73 75 62 71 75 65 72 79 20 77 69 is a subquery wi
202b1 74 68 69 6e 0a 20 20 2a 2a 20 61 6e 20 65 78 70 thin. ** an exp
202b2 72 65 73 73 69 6f 6e 2c 20 73 71 6c 69 74 65 33 ression, sqlite3
202b3 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 ResolveExprNames
202b4 28 29 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c 65 () will be calle
202b5 64 20 77 69 74 68 6f 75 74 20 61 0a 20 20 2a 2a d without a. **
202b6 20 70 72 69 6f 72 20 63 61 6c 6c 20 74 6f 20 73 prior call to s
202b7 71 6c 69 74 65 33 53 65 6c 65 63 74 45 78 70 61 qlite3SelectExpa
202b8 6e 64 28 29 2e 20 20 57 68 65 6e 20 74 68 61 74 nd(). When that
202b9 20 68 61 70 70 65 6e 73 2c 20 6c 65 74 0a 20 20 happens, let.
202ba 2a 2a 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 ** sqlite3Select
202bb 50 72 65 70 28 29 20 64 6f 20 61 6c 6c 20 6f 66 Prep() do all of
202bc 20 74 68 65 20 70 72 6f 63 65 73 73 69 6e 67 20 the processing
202bd 66 6f 72 20 74 68 69 73 20 53 45 4c 45 43 54 2e for this SELECT.
202be 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 53 65 6c . ** sqlite3Sel
202bf 65 63 74 50 72 65 70 28 29 20 77 69 6c 6c 20 69 ectPrep() will i
202c0 6e 76 6f 6b 65 20 62 6f 74 68 20 73 71 6c 69 74 nvoke both sqlit
202c1 65 33 53 65 6c 65 63 74 45 78 70 61 6e 64 28 29 e3SelectExpand()
202c2 20 61 6e 64 0a 20 20 2a 2a 20 74 68 69 73 20 72 and. ** this r
202c3 6f 75 74 69 6e 65 20 69 6e 20 74 68 65 20 63 6f outine in the co
202c4 72 72 65 63 74 20 6f 72 64 65 72 2e 0a 20 20 2a rrect order.. *
202c5 2f 0a 20 20 69 66 28 20 28 70 2d 3e 73 65 6c 46 /. if( (p->selF
202c6 6c 61 67 73 20 26 20 53 46 5f 45 78 70 61 6e 64 lags & SF_Expand
202c7 65 64 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 ed)==0 ){. sq
202c8 6c 69 74 65 33 53 65 6c 65 63 74 50 72 65 70 28 lite3SelectPrep(
202c9 70 50 61 72 73 65 2c 20 70 2c 20 70 4f 75 74 65 pParse, p, pOute
202ca 72 4e 43 29 3b 0a 20 20 20 20 72 65 74 75 72 6e rNC);. return
202cb 20 28 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c (pParse->nErr |
202cc 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c | db->mallocFail
202cd 65 64 29 20 3f 20 57 52 43 5f 41 62 6f 72 74 20 ed) ? WRC_Abort
202ce 3a 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20 7d : WRC_Prune;. }
202cf 0a 0a 20 20 69 73 43 6f 6d 70 6f 75 6e 64 20 3d .. isCompound =
202d0 20 70 2d 3e 70 50 72 69 6f 72 21 3d 30 3b 0a 20 p->pPrior!=0;.
202d1 20 6e 43 6f 6d 70 6f 75 6e 64 20 3d 20 30 3b 0a nCompound = 0;.
202d2 20 20 70 4c 65 66 74 6d 6f 73 74 20 3d 20 70 3b pLeftmost = p;
202d3 0a 20 20 77 68 69 6c 65 28 20 70 20 29 7b 0a 20 . while( p ){.
202d4 20 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 73 assert( (p->s
202d5 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 45 78 70 elFlags & SF_Exp
202d6 61 6e 64 65 64 29 21 3d 30 20 29 3b 0a 20 20 20 anded)!=0 );.
202d7 20 61 73 73 65 72 74 28 20 28 70 2d 3e 73 65 6c assert( (p->sel
202d8 46 6c 61 67 73 20 26 20 53 46 5f 52 65 73 6f 6c Flags & SF_Resol
202d9 76 65 64 29 3d 3d 30 20 29 3b 0a 20 20 20 20 70 ved)==0 );. p
202da 2d 3e 73 65 6c 46 6c 61 67 73 20 7c 3d 20 53 46 ->selFlags |= SF
202db 5f 52 65 73 6f 6c 76 65 64 3b 0a 0a 20 20 20 20 _Resolved;..
202dc 2f 2a 20 52 65 73 6f 6c 76 65 20 74 68 65 20 65 /* Resolve the e
202dd 78 70 72 65 73 73 69 6f 6e 73 20 69 6e 20 74 68 xpressions in th
202de 65 20 4c 49 4d 49 54 20 61 6e 64 20 4f 46 46 53 e LIMIT and OFFS
202df 45 54 20 63 6c 61 75 73 65 73 2e 20 54 68 65 73 ET clauses. Thes
202e0 65 0a 20 20 20 20 2a 2a 20 61 72 65 20 6e 6f 74 e. ** are not
202e1 20 61 6c 6c 6f 77 65 64 20 74 6f 20 72 65 66 65 allowed to refe
202e2 72 20 74 6f 20 61 6e 79 20 6e 61 6d 65 73 2c 20 r to any names,
202e3 73 6f 20 70 61 73 73 20 61 6e 20 65 6d 70 74 79 so pass an empty
202e4 20 4e 61 6d 65 43 6f 6e 74 65 78 74 2e 0a 20 20 NameContext..
202e5 20 20 2a 2f 0a 20 20 20 20 6d 65 6d 73 65 74 28 */. memset(
202e6 26 73 4e 43 2c 20 30 2c 20 73 69 7a 65 6f 66 28 &sNC, 0, sizeof(
202e7 73 4e 43 29 29 3b 0a 20 20 20 20 73 4e 43 2e 70 sNC));. sNC.p
202e8 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a Parse = pParse;.
202e9 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 if( sqlite3R
202ea 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 esolveExprNames(
202eb 26 73 4e 43 2c 20 70 2d 3e 70 4c 69 6d 69 74 29 &sNC, p->pLimit)
202ec 20 7c 7c 0a 20 20 20 20 20 20 20 20 73 71 6c 69 ||. sqli
202ed 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 te3ResolveExprNa
202ee 6d 65 73 28 26 73 4e 43 2c 20 70 2d 3e 70 4f 66 mes(&sNC, p->pOf
202ef 66 73 65 74 29 20 29 7b 0a 20 20 20 20 20 20 72 fset) ){. r
202f0 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b eturn WRC_Abort;
202f1 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a . }. . /*
202f2 20 53 65 74 20 75 70 20 74 68 65 20 6c 6f 63 61 Set up the loca
202f3 6c 20 6e 61 6d 65 2d 63 6f 6e 74 65 78 74 20 74 l name-context t
202f4 6f 20 70 61 73 73 20 74 6f 20 73 71 6c 69 74 65 o pass to sqlite
202f5 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 3ResolveExprName
202f6 73 28 29 20 74 6f 0a 20 20 20 20 2a 2a 20 72 65 s() to. ** re
202f7 73 6f 6c 76 65 20 74 68 65 20 72 65 73 75 6c 74 solve the result
202f8 2d 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e 20 -set expression
202f9 6c 69 73 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 list.. */.
202fa 20 73 4e 43 2e 61 6c 6c 6f 77 41 67 67 20 3d 20 sNC.allowAgg =
202fb 31 3b 0a 20 20 20 20 73 4e 43 2e 70 53 72 63 4c 1;. sNC.pSrcL
202fc 69 73 74 20 3d 20 70 2d 3e 70 53 72 63 3b 0a 20 ist = p->pSrc;.
202fd 20 20 20 73 4e 43 2e 70 4e 65 78 74 20 3d 20 70 sNC.pNext = p
202fe 4f 75 74 65 72 4e 43 3b 0a 20 20 0a 20 20 20 20 OuterNC;. .
202ff 2f 2a 20 52 65 73 6f 6c 76 65 20 6e 61 6d 65 73 /* Resolve names
20300 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 in the result s
20301 65 74 2e 20 2a 2f 0a 20 20 20 20 70 45 4c 69 73 et. */. pELis
20302 74 20 3d 20 70 2d 3e 70 45 4c 69 73 74 3b 0a 20 t = p->pEList;.
20303 20 20 20 61 73 73 65 72 74 28 20 70 45 4c 69 73 assert( pELis
20304 74 21 3d 30 20 29 3b 0a 20 20 20 20 66 6f 72 28 t!=0 );. for(
20305 69 3d 30 3b 20 69 3c 70 45 4c 69 73 74 2d 3e 6e i=0; i<pEList->n
20306 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 Expr; i++){.
20307 20 20 45 78 70 72 20 2a 70 58 20 3d 20 70 45 4c Expr *pX = pEL
20308 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b ist->a[i].pExpr;
20309 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 . if( sqlit
2030a 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d e3ResolveExprNam
2030b 65 73 28 26 73 4e 43 2c 20 70 58 29 20 29 7b 0a es(&sNC, pX) ){.
2030c 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 return W
2030d 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 20 20 RC_Abort;.
2030e 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f }. }. . /
2030f 2a 20 52 65 63 75 72 73 69 76 65 6c 79 20 72 65 * Recursively re
20310 73 6f 6c 76 65 20 6e 61 6d 65 73 20 69 6e 20 61 solve names in a
20311 6c 6c 20 73 75 62 71 75 65 72 69 65 73 0a 20 20 ll subqueries.
20312 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30 */. for(i=0
20313 3b 20 69 3c 70 2d 3e 70 53 72 63 2d 3e 6e 53 72 ; i<p->pSrc->nSr
20314 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 c; i++){. s
20315 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 truct SrcList_it
20316 65 6d 20 2a 70 49 74 65 6d 20 3d 20 26 70 2d 3e em *pItem = &p->
20317 70 53 72 63 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20 pSrc->a[i];.
20318 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 70 53 65 if( pItem->pSe
20319 6c 65 63 74 20 29 7b 0a 20 20 20 20 20 20 20 20 lect ){.
2031a 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 61 76 const char *zSav
2031b 65 64 43 6f 6e 74 65 78 74 20 3d 20 70 50 61 72 edContext = pPar
2031c 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74 se->zAuthContext
2031d 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 49 ;. if( pI
2031e 74 65 6d 2d 3e 7a 4e 61 6d 65 20 29 20 70 50 61 tem->zName ) pPa
2031f 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 rse->zAuthContex
20320 74 20 3d 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 t = pItem->zName
20321 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 ;. sqlite
20322 33 52 65 73 6f 6c 76 65 53 65 6c 65 63 74 4e 61 3ResolveSelectNa
20323 6d 65 73 28 70 50 61 72 73 65 2c 20 70 49 74 65 mes(pParse, pIte
20324 6d 2d 3e 70 53 65 6c 65 63 74 2c 20 70 4f 75 74 m->pSelect, pOut
20325 65 72 4e 43 29 3b 0a 20 20 20 20 20 20 20 20 70 erNC);. p
20326 50 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 Parse->zAuthCont
20327 65 78 74 20 3d 20 7a 53 61 76 65 64 43 6f 6e 74 ext = zSavedCont
20328 65 78 74 3b 0a 20 20 20 20 20 20 20 20 69 66 28 ext;. if(
20329 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c pParse->nErr ||
2032a 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 db->mallocFaile
2032b 64 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 d ) return WRC_A
2032c 62 6f 72 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 bort;. }.
2032d 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 49 66 }. . /* If
2032e 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 61 67 there are no ag
2032f 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e gregate function
20330 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 2d s in the result-
20331 73 65 74 2c 20 61 6e 64 20 6e 6f 20 47 52 4f 55 set, and no GROU
20332 50 20 42 59 20 0a 20 20 20 20 2a 2a 20 65 78 70 P BY . ** exp
20333 72 65 73 73 69 6f 6e 2c 20 64 6f 20 6e 6f 74 20 ression, do not
20334 61 6c 6c 6f 77 20 61 67 67 72 65 67 61 74 65 73 allow aggregates
20335 20 69 6e 20 61 6e 79 20 6f 66 20 74 68 65 20 6f in any of the o
20336 74 68 65 72 20 65 78 70 72 65 73 73 69 6f 6e 73 ther expressions
20337 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 .. */. ass
20338 65 72 74 28 20 28 70 2d 3e 73 65 6c 46 6c 61 67 ert( (p->selFlag
20339 73 20 26 20 53 46 5f 41 67 67 72 65 67 61 74 65 s & SF_Aggregate
2033a 29 3d 3d 30 20 29 3b 0a 20 20 20 20 70 47 72 6f )==0 );. pGro
2033b 75 70 42 79 20 3d 20 70 2d 3e 70 47 72 6f 75 70 upBy = p->pGroup
2033c 42 79 3b 0a 20 20 20 20 69 66 28 20 70 47 72 6f By;. if( pGro
2033d 75 70 42 79 20 7c 7c 20 73 4e 43 2e 68 61 73 41 upBy || sNC.hasA
2033e 67 67 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 73 gg ){. p->s
2033f 65 6c 46 6c 61 67 73 20 7c 3d 20 53 46 5f 41 67 elFlags |= SF_Ag
20340 67 72 65 67 61 74 65 3b 0a 20 20 20 20 7d 65 6c gregate;. }el
20341 73 65 7b 0a 20 20 20 20 20 20 73 4e 43 2e 61 6c se{. sNC.al
20342 6c 6f 77 41 67 67 20 3d 20 30 3b 0a 20 20 20 20 lowAgg = 0;.
20343 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 49 66 20 61 }. . /* If a
20344 20 48 41 56 49 4e 47 20 63 6c 61 75 73 65 20 69 HAVING clause i
20345 73 20 70 72 65 73 65 6e 74 2c 20 74 68 65 6e 20 s present, then
20346 74 68 65 72 65 20 6d 75 73 74 20 62 65 20 61 20 there must be a
20347 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 2e GROUP BY clause.
20348 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 . */. if(
20349 70 2d 3e 70 48 61 76 69 6e 67 20 26 26 20 21 70 p->pHaving && !p
2034a 47 72 6f 75 70 42 79 20 29 7b 0a 20 20 20 20 20 GroupBy ){.
2034b 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 sqlite3ErrorMsg
2034c 28 70 50 61 72 73 65 2c 20 22 61 20 47 52 4f 55 (pParse, "a GROU
2034d 50 20 42 59 20 63 6c 61 75 73 65 20 69 73 20 72 P BY clause is r
2034e 65 71 75 69 72 65 64 20 62 65 66 6f 72 65 20 48 equired before H
2034f 41 56 49 4e 47 22 29 3b 0a 20 20 20 20 20 20 72 AVING");. r
20350 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b eturn WRC_Abort;
20351 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a . }. . /*
20352 20 41 64 64 20 74 68 65 20 65 78 70 72 65 73 73 Add the express
20353 69 6f 6e 20 6c 69 73 74 20 74 6f 20 74 68 65 20 ion list to the
20354 6e 61 6d 65 2d 63 6f 6e 74 65 78 74 20 62 65 66 name-context bef
20355 6f 72 65 20 70 61 72 73 69 6e 67 20 74 68 65 0a ore parsing the.
20356 20 20 20 20 2a 2a 20 6f 74 68 65 72 20 65 78 70 ** other exp
20357 72 65 73 73 69 6f 6e 73 20 69 6e 20 74 68 65 20 ressions in the
20358 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 SELECT statement
20359 2e 20 54 68 69 73 20 69 73 20 73 6f 20 74 68 61 . This is so tha
2035a 74 0a 20 20 20 20 2a 2a 20 65 78 70 72 65 73 73 t. ** express
2035b 69 6f 6e 73 20 69 6e 20 74 68 65 20 57 48 45 52 ions in the WHER
2035c 45 20 63 6c 61 75 73 65 20 28 65 74 63 2e 29 20 E clause (etc.)
2035d 63 61 6e 20 72 65 66 65 72 20 74 6f 20 65 78 70 can refer to exp
2035e 72 65 73 73 69 6f 6e 73 20 62 79 0a 20 20 20 20 ressions by.
2035f 2a 2a 20 61 6c 69 61 73 65 73 20 69 6e 20 74 68 ** aliases in th
20360 65 20 72 65 73 75 6c 74 20 73 65 74 2e 0a 20 20 e result set..
20361 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 4d 69 6e 6f **. ** Mino
20362 72 20 70 6f 69 6e 74 3a 20 49 66 20 74 68 69 73 r point: If this
20363 20 69 73 20 74 68 65 20 63 61 73 65 2c 20 74 68 is the case, th
20364 65 6e 20 74 68 65 20 65 78 70 72 65 73 73 69 6f en the expressio
20365 6e 20 77 69 6c 6c 20 62 65 0a 20 20 20 20 2a 2a n will be. **
20366 20 72 65 2d 65 76 61 6c 75 61 74 65 64 20 66 6f re-evaluated fo
20367 72 20 65 61 63 68 20 72 65 66 65 72 65 6e 63 65 r each reference
20368 20 74 6f 20 69 74 2e 0a 20 20 20 20 2a 2f 0a 20 to it.. */.
20369 20 20 20 73 4e 43 2e 70 45 4c 69 73 74 20 3d 20 sNC.pEList =
2036a 70 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 20 20 69 p->pEList;. i
2036b 66 28 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 f( sqlite3Resolv
2036c 65 45 78 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c eExprNames(&sNC,
2036d 20 70 2d 3e 70 57 68 65 72 65 29 20 7c 7c 0a 20 p->pWhere) ||.
2036e 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 73 sqlite3Res
2036f 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 26 73 olveExprNames(&s
20370 4e 43 2c 20 70 2d 3e 70 48 61 76 69 6e 67 29 0a NC, p->pHaving).
20371 20 20 20 20 29 7b 0a 20 20 20 20 20 20 72 65 74 ){. ret
20372 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 urn WRC_Abort;.
20373 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 54 68 65 }.. /* The
20374 20 4f 52 44 45 52 20 42 59 20 61 6e 64 20 47 52 ORDER BY and GR
20375 4f 55 50 20 42 59 20 63 6c 61 75 73 65 73 20 6d OUP BY clauses m
20376 61 79 20 6e 6f 74 20 72 65 66 65 72 20 74 6f 20 ay not refer to
20377 74 65 72 6d 73 20 69 6e 0a 20 20 20 20 2a 2a 20 terms in. **
20378 6f 75 74 65 72 20 71 75 65 72 69 65 73 20 0a 20 outer queries .
20379 20 20 20 2a 2f 0a 20 20 20 20 73 4e 43 2e 70 4e */. sNC.pN
2037a 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 73 4e 43 ext = 0;. sNC
2037b 2e 61 6c 6c 6f 77 41 67 67 20 3d 20 31 3b 0a 0a .allowAgg = 1;..
2037c 20 20 20 20 2f 2a 20 50 72 6f 63 65 73 73 20 74 /* Process t
2037d 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 he ORDER BY clau
2037e 73 65 20 66 6f 72 20 73 69 6e 67 6c 65 74 6f 6e se for singleton
2037f 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e SELECT statemen
20380 74 73 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20 4f ts.. ** The O
20381 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 66 RDER BY clause f
20382 6f 72 20 63 6f 6d 70 6f 75 6e 64 73 20 53 45 4c or compounds SEL
20383 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 20 69 ECT statements i
20384 73 20 68 61 6e 64 6c 65 64 0a 20 20 20 20 2a 2a s handled. **
20385 20 62 65 6c 6f 77 2c 20 61 66 74 65 72 20 61 6c below, after al
20386 6c 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 2d l of the result-
20387 73 65 74 73 20 66 6f 72 20 61 6c 6c 20 6f 66 20 sets for all of
20388 74 68 65 20 65 6c 65 6d 65 6e 74 73 20 6f 66 0a the elements of.
20389 20 20 20 20 2a 2a 20 74 68 65 20 63 6f 6d 70 6f ** the compo
2038a 75 6e 64 20 68 61 76 65 20 62 65 65 6e 20 72 65 und have been re
2038b 73 6f 6c 76 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 solved.. */.
2038c 20 20 20 69 66 28 20 21 69 73 43 6f 6d 70 6f 75 if( !isCompou
2038d 6e 64 20 26 26 20 72 65 73 6f 6c 76 65 4f 72 64 nd && resolveOrd
2038e 65 72 47 72 6f 75 70 42 79 28 26 73 4e 43 2c 20 erGroupBy(&sNC,
2038f 70 2c 20 70 2d 3e 70 4f 72 64 65 72 42 79 2c 20 p, p->pOrderBy,
20390 22 4f 52 44 45 52 22 29 20 29 7b 0a 20 20 20 20 "ORDER") ){.
20391 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f return WRC_Abo
20392 72 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 rt;. }. if
20393 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c ( db->mallocFail
20394 65 64 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 ed ){. retu
20395 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 rn WRC_Abort;.
20396 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 52 65 }. . /* Re
20397 73 6f 6c 76 65 20 74 68 65 20 47 52 4f 55 50 20 solve the GROUP
20398 42 59 20 63 6c 61 75 73 65 2e 20 20 41 74 20 74 BY clause. At t
20399 68 65 20 73 61 6d 65 20 74 69 6d 65 2c 20 6d 61 he same time, ma
2039a 6b 65 20 73 75 72 65 20 0a 20 20 20 20 2a 2a 20 ke sure . **
2039b 74 68 65 20 47 52 4f 55 50 20 42 59 20 63 6c 61 the GROUP BY cla
2039c 75 73 65 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6e use does not con
2039d 74 61 69 6e 20 61 67 67 72 65 67 61 74 65 20 66 tain aggregate f
2039e 75 6e 63 74 69 6f 6e 73 2e 0a 20 20 20 20 2a 2f unctions.. */
2039f 0a 20 20 20 20 69 66 28 20 70 47 72 6f 75 70 42 . if( pGroupB
203a0 79 20 29 7b 0a 20 20 20 20 20 20 73 74 72 75 63 y ){. struc
203a1 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 t ExprList_item
203a2 2a 70 49 74 65 6d 3b 0a 20 20 20 20 0a 20 20 20 *pItem;. .
203a3 20 20 20 69 66 28 20 72 65 73 6f 6c 76 65 4f 72 if( resolveOr
203a4 64 65 72 47 72 6f 75 70 42 79 28 26 73 4e 43 2c derGroupBy(&sNC,
203a5 20 70 2c 20 70 47 72 6f 75 70 42 79 2c 20 22 47 p, pGroupBy, "G
203a6 52 4f 55 50 22 29 20 7c 7c 20 64 62 2d 3e 6d 61 ROUP") || db->ma
203a7 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 llocFailed ){.
203a8 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 return WRC
203a9 5f 41 62 6f 72 74 3b 0a 20 20 20 20 20 20 7d 0a _Abort;. }.
203aa 20 20 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 70 for(i=0, p
203ab 49 74 65 6d 3d 70 47 72 6f 75 70 42 79 2d 3e 61 Item=pGroupBy->a
203ac 3b 20 69 3c 70 47 72 6f 75 70 42 79 2d 3e 6e 45 ; i<pGroupBy->nE
203ad 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b xpr; i++, pItem+
203ae 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 +){. if(
203af 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 ExprHasProperty(
203b0 70 49 74 65 6d 2d 3e 70 45 78 70 72 2c 20 45 50 pItem->pExpr, EP
203b1 5f 41 67 67 29 20 29 7b 0a 20 20 20 20 20 20 20 _Agg) ){.
203b2 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d sqlite3ErrorM
203b3 73 67 28 70 50 61 72 73 65 2c 20 22 61 67 67 72 sg(pParse, "aggr
203b4 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 egate functions
203b5 61 72 65 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 are not allowed
203b6 69 6e 20 22 0a 20 20 20 20 20 20 20 20 20 20 20 in ".
203b7 20 20 20 22 74 68 65 20 47 52 4f 55 50 20 42 59 "the GROUP BY
203b8 20 63 6c 61 75 73 65 22 29 3b 0a 20 20 20 20 20 clause");.
203b9 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f return WRC_
203ba 41 62 6f 72 74 3b 0a 20 20 20 20 20 20 20 20 7d Abort;. }
203bb 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a . }. }..
203bc 20 20 20 20 2f 2a 20 41 64 76 61 6e 63 65 20 74 /* Advance t
203bd 6f 20 74 68 65 20 6e 65 78 74 20 74 65 72 6d 20 o the next term
203be 6f 66 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 0a of the compound.
203bf 20 20 20 20 2a 2f 0a 20 20 20 20 70 20 3d 20 70 */. p = p
203c0 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 20 20 6e 43 ->pPrior;. nC
203c1 6f 6d 70 6f 75 6e 64 2b 2b 3b 0a 20 20 7d 0a 0a ompound++;. }..
203c2 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 74 68 65 /* Resolve the
203c3 20 4f 52 44 45 52 20 42 59 20 6f 6e 20 61 20 63 ORDER BY on a c
203c4 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 20 61 ompound SELECT a
203c5 66 74 65 72 20 61 6c 6c 20 74 65 72 6d 73 20 6f fter all terms o
203c6 66 0a 20 20 2a 2a 20 74 68 65 20 63 6f 6d 70 6f f. ** the compo
203c7 75 6e 64 20 68 61 76 65 20 62 65 65 6e 20 72 65 und have been re
203c8 73 6f 6c 76 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 solved.. */. i
203c9 66 28 20 69 73 43 6f 6d 70 6f 75 6e 64 20 26 26 f( isCompound &&
203ca 20 72 65 73 6f 6c 76 65 43 6f 6d 70 6f 75 6e 64 resolveCompound
203cb 4f 72 64 65 72 42 79 28 70 50 61 72 73 65 2c 20 OrderBy(pParse,
203cc 70 4c 65 66 74 6d 6f 73 74 29 20 29 7b 0a 20 20 pLeftmost) ){.
203cd 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f return WRC_Abo
203ce 72 74 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 rt;. }.. retur
203cf 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 7d 0a 0a n WRC_Prune;.}..
203d0 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 /*.** This routi
203d1 6e 65 20 77 61 6c 6b 73 20 61 6e 20 65 78 70 72 ne walks an expr
203d2 65 73 73 69 6f 6e 20 74 72 65 65 20 61 6e 64 20 ession tree and
203d3 72 65 73 6f 6c 76 65 73 20 72 65 66 65 72 65 6e resolves referen
203d4 63 65 73 20 74 6f 0a 2a 2a 20 74 61 62 6c 65 20 ces to.** table
203d5 63 6f 6c 75 6d 6e 73 20 61 6e 64 20 72 65 73 75 columns and resu
203d6 6c 74 2d 73 65 74 20 63 6f 6c 75 6d 6e 73 2e 20 lt-set columns.
203d7 20 41 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d At the same tim
203d8 65 2c 20 64 6f 20 65 72 72 6f 72 0a 2a 2a 20 63 e, do error.** c
203d9 68 65 63 6b 69 6e 67 20 6f 6e 20 66 75 6e 63 74 hecking on funct
203da 69 6f 6e 20 75 73 61 67 65 20 61 6e 64 20 73 65 ion usage and se
203db 74 20 61 20 66 6c 61 67 20 69 66 20 61 6e 79 20 t a flag if any
203dc 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 aggregate functi
203dd 6f 6e 73 0a 2a 2a 20 61 72 65 20 73 65 65 6e 2e ons.** are seen.
203de 0a 2a 2a 0a 2a 2a 20 54 6f 20 72 65 73 6f 6c 76 .**.** To resolv
203df 65 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 73 20 e table columns
203e0 72 65 66 65 72 65 6e 63 65 73 20 77 65 20 6c 6f references we lo
203e1 6f 6b 20 66 6f 72 20 6e 6f 64 65 73 20 28 6f 72 ok for nodes (or
203e2 20 73 75 62 74 72 65 65 73 29 20 6f 66 20 74 68 subtrees) of th
203e3 65 20 0a 2a 2a 20 66 6f 72 6d 20 58 2e 59 2e 5a e .** form X.Y.Z
203e4 20 6f 72 20 59 2e 5a 20 6f 72 20 6a 75 73 74 20 or Y.Z or just
203e5 5a 20 77 68 65 72 65 0a 2a 2a 0a 2a 2a 20 20 20 Z where.**.**
203e6 20 20 20 58 3a 20 20 20 54 68 65 20 6e 61 6d 65 X: The name
203e7 20 6f 66 20 61 20 64 61 74 61 62 61 73 65 2e 20 of a database.
203e8 20 45 78 3a 20 20 22 6d 61 69 6e 22 20 6f 72 20 Ex: "main" or
203e9 22 74 65 6d 70 22 20 6f 72 0a 2a 2a 20 20 20 20 "temp" or.**
203ea 20 20 20 20 20 20 20 74 68 65 20 73 79 6d 62 6f the symbo
203eb 6c 69 63 20 6e 61 6d 65 20 61 73 73 69 67 6e 65 lic name assigne
203ec 64 20 74 6f 20 61 6e 20 41 54 54 41 43 48 2d 65 d to an ATTACH-e
203ed 64 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a d database..**.*
203ee 2a 20 20 20 20 20 20 59 3a 20 20 20 54 68 65 20 * Y: The
203ef 6e 61 6d 65 20 6f 66 20 61 20 74 61 62 6c 65 20 name of a table
203f0 69 6e 20 61 20 46 52 4f 4d 20 63 6c 61 75 73 65 in a FROM clause
203f1 2e 20 20 4f 72 20 69 6e 20 61 20 74 72 69 67 67 . Or in a trigg
203f2 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 er.**
203f3 6f 6e 65 20 6f 66 20 74 68 65 20 73 70 65 63 69 one of the speci
203f4 61 6c 20 6e 61 6d 65 73 20 22 6f 6c 64 22 20 6f al names "old" o
203f5 72 20 22 6e 65 77 22 2e 0a 2a 2a 0a 2a 2a 20 20 r "new"..**.**
203f6 20 20 20 20 5a 3a 20 20 20 54 68 65 20 6e 61 6d Z: The nam
203f7 65 20 6f 66 20 61 20 63 6f 6c 75 6d 6e 20 69 6e e of a column in
203f8 20 74 61 62 6c 65 20 59 2e 0a 2a 2a 0a 2a 2a 20 table Y..**.**
203f9 54 68 65 20 6e 6f 64 65 20 61 74 20 74 68 65 20 The node at the
203fa 72 6f 6f 74 20 6f 66 20 74 68 65 20 73 75 62 74 root of the subt
203fb 72 65 65 20 69 73 20 6d 6f 64 69 66 69 65 64 20 ree is modified
203fc 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a as follows:.**.*
203fd 2a 20 20 20 20 45 78 70 72 2e 6f 70 20 20 20 20 * Expr.op
203fe 20 20 20 20 43 68 61 6e 67 65 64 20 74 6f 20 54 Changed to T
203ff 4b 5f 43 4f 4c 55 4d 4e 0a 2a 2a 20 20 20 20 45 K_COLUMN.** E
20400 78 70 72 2e 70 54 61 62 20 20 20 20 20 20 50 6f xpr.pTab Po
20401 69 6e 74 73 20 74 6f 20 74 68 65 20 54 61 62 6c ints to the Tabl
20402 65 20 6f 62 6a 65 63 74 20 66 6f 72 20 58 2e 59 e object for X.Y
20403 0a 2a 2a 20 20 20 20 45 78 70 72 2e 69 43 6f 6c .** Expr.iCol
20404 75 6d 6e 20 20 20 54 68 65 20 63 6f 6c 75 6d 6e umn The column
20405 20 69 6e 64 65 78 20 69 6e 20 58 2e 59 2e 20 20 index in X.Y.
20406 2d 31 20 66 6f 72 20 74 68 65 20 72 6f 77 69 64 -1 for the rowid
20407 2e 0a 2a 2a 20 20 20 20 45 78 70 72 2e 69 54 61 ..** Expr.iTa
20408 62 6c 65 20 20 20 20 54 68 65 20 56 44 42 45 20 ble The VDBE
20409 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 66 6f cursor number fo
2040a 72 20 58 2e 59 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 r X.Y.**.**.** T
2040b 6f 20 72 65 73 6f 6c 76 65 20 72 65 73 75 6c 74 o resolve result
2040c 2d 73 65 74 20 72 65 66 65 72 65 6e 63 65 73 2c -set references,
2040d 20 6c 6f 6f 6b 20 66 6f 72 20 65 78 70 72 65 73 look for expres
2040e 73 69 6f 6e 20 6e 6f 64 65 73 20 6f 66 20 74 68 sion nodes of th
2040f 65 0a 2a 2a 20 66 6f 72 6d 20 5a 20 28 77 69 74 e.** form Z (wit
20410 68 20 6e 6f 20 58 20 61 6e 64 20 59 20 70 72 65 h no X and Y pre
20411 66 69 78 29 20 77 68 65 72 65 20 74 68 65 20 5a fix) where the Z
20412 20 6d 61 74 63 68 65 73 20 74 68 65 20 72 69 67 matches the rig
20413 68 74 2d 68 61 6e 64 0a 2a 2a 20 73 69 7a 65 20 ht-hand.** size
20414 6f 66 20 61 6e 20 41 53 20 63 6c 61 75 73 65 20 of an AS clause
20415 69 6e 20 74 68 65 20 72 65 73 75 6c 74 2d 73 65 in the result-se
20416 74 20 6f 66 20 61 20 53 45 4c 45 43 54 2e 20 20 t of a SELECT.
20417 54 68 65 20 5a 20 65 78 70 72 65 73 73 69 6f 6e The Z expression
20418 0a 2a 2a 20 69 73 20 72 65 70 6c 61 63 65 64 20 .** is replaced
20419 62 79 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 by a copy of the
2041a 20 6c 65 66 74 2d 68 61 6e 64 20 73 69 64 65 20 left-hand side
2041b 6f 66 20 74 68 65 20 72 65 73 75 6c 74 2d 73 65 of the result-se
2041c 74 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a t expression..**
2041d 20 54 61 62 6c 65 2d 6e 61 6d 65 20 61 6e 64 20 Table-name and
2041e 66 75 6e 63 74 69 6f 6e 20 72 65 73 6f 6c 75 74 function resolut
2041f 69 6f 6e 20 6f 63 63 75 72 73 20 6f 6e 20 74 68 ion occurs on th
20420 65 20 73 75 62 73 74 69 74 75 74 65 64 20 65 78 e substituted ex
20421 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 74 72 65 65 pression.** tree
20422 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 . For example,
20423 69 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 53 in:.**.** S
20424 45 4c 45 43 54 20 61 2b 62 20 41 53 20 78 2c 20 ELECT a+b AS x,
20425 63 2b 64 20 41 53 20 79 20 46 52 4f 4d 20 74 31 c+d AS y FROM t1
20426 20 4f 52 44 45 52 20 42 59 20 78 3b 0a 2a 2a 0a ORDER BY x;.**.
20427 2a 2a 20 54 68 65 20 22 78 22 20 74 65 72 6d 20 ** The "x" term
20428 6f 66 20 74 68 65 20 6f 72 64 65 72 20 62 79 20 of the order by
20429 69 73 20 72 65 70 6c 61 63 65 64 20 62 79 20 22 is replaced by "
2042a 61 2b 62 22 20 74 6f 20 72 65 6e 64 65 72 3a 0a a+b" to render:.
2042b 2a 2a 0a 2a 2a 20 20 20 20 20 20 53 45 4c 45 43 **.** SELEC
2042c 54 20 61 2b 62 20 41 53 20 78 2c 20 63 2b 64 20 T a+b AS x, c+d
2042d 41 53 20 79 20 46 52 4f 4d 20 74 31 20 4f 52 44 AS y FROM t1 ORD
2042e 45 52 20 42 59 20 61 2b 62 3b 0a 2a 2a 0a 2a 2a ER BY a+b;.**.**
2042f 20 46 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 73 20 Function calls
20430 61 72 65 20 63 68 65 63 6b 65 64 20 74 6f 20 6d are checked to m
20431 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 74 68 ake sure that th
20432 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 0a 2a e function is .*
20433 2a 20 64 65 66 69 6e 65 64 20 61 6e 64 20 74 68 * defined and th
20434 61 74 20 74 68 65 20 63 6f 72 72 65 63 74 20 6e at the correct n
20435 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e umber of argumen
20436 74 73 20 61 72 65 20 73 70 65 63 69 66 69 65 64 ts are specified
20437 2e 0a 2a 2a 20 49 66 20 74 68 65 20 66 75 6e 63 ..** If the func
20438 74 69 6f 6e 20 69 73 20 61 6e 20 61 67 67 72 65 tion is an aggre
20439 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 2c 20 74 gate function, t
2043a 68 65 6e 20 74 68 65 20 70 4e 43 2d 3e 68 61 73 hen the pNC->has
2043b 41 67 67 20 69 73 0a 2a 2a 20 73 65 74 20 61 6e Agg is.** set an
2043c 64 20 74 68 65 20 6f 70 63 6f 64 65 20 69 73 20 d the opcode is
2043d 63 68 61 6e 67 65 64 20 66 72 6f 6d 20 54 4b 5f changed from TK_
2043e 46 55 4e 43 54 49 4f 4e 20 74 6f 20 54 4b 5f 41 FUNCTION to TK_A
2043f 47 47 5f 46 55 4e 43 54 49 4f 4e 2e 0a 2a 2a 20 GG_FUNCTION..**
20440 49 66 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e If an expression
20441 20 63 6f 6e 74 61 69 6e 73 20 61 67 67 72 65 67 contains aggreg
20442 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 ate functions th
20443 65 6e 20 74 68 65 20 45 50 5f 41 67 67 0a 2a 2a en the EP_Agg.**
20444 20 70 72 6f 70 65 72 74 79 20 6f 6e 20 74 68 65 property on the
20445 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 73 expression is s
20446 65 74 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 72 72 et..**.** An err
20447 6f 72 20 6d 65 73 73 61 67 65 20 69 73 20 6c 65 or message is le
20448 66 74 20 69 6e 20 70 50 61 72 73 65 20 69 66 20 ft in pParse if
20449 61 6e 79 74 68 69 6e 67 20 69 73 20 61 6d 69 73 anything is amis
2044a 73 2e 20 20 54 68 65 20 6e 75 6d 62 65 72 0a 2a s. The number.*
2044b 2a 20 69 66 20 65 72 72 6f 72 73 20 69 73 20 72 * if errors is r
2044c 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 eturned..*/.SQLI
2044d 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
2044e 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 qlite3ResolveExp
2044f 72 4e 61 6d 65 73 28 20 0a 20 20 4e 61 6d 65 43 rNames( . NameC
20450 6f 6e 74 65 78 74 20 2a 70 4e 43 2c 20 20 20 20 ontext *pNC,
20451 20 20 20 2f 2a 20 4e 61 6d 65 73 70 61 63 65 20 /* Namespace
20452 74 6f 20 72 65 73 6f 6c 76 65 20 65 78 70 72 65 to resolve expre
20453 73 73 69 6f 6e 73 20 69 6e 2e 20 2a 2f 0a 20 20 ssions in. */.
20454 45 78 70 72 20 2a 70 45 78 70 72 20 20 20 20 20 Expr *pExpr
20455 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65 /* The e
20456 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 62 65 20 xpression to be
20457 61 6e 61 6c 79 7a 65 64 2e 20 2a 2f 0a 29 7b 0a analyzed. */.){.
20458 20 20 69 6e 74 20 73 61 76 65 64 48 61 73 41 67 int savedHasAg
20459 67 3b 0a 20 20 57 61 6c 6b 65 72 20 77 3b 0a 0a g;. Walker w;..
2045a 20 20 69 66 28 20 70 45 78 70 72 3d 3d 30 20 29 if( pExpr==0 )
2045b 20 72 65 74 75 72 6e 20 30 3b 0a 23 69 66 20 53 return 0;.#if S
2045c 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 QLITE_MAX_EXPR_D
2045d 45 50 54 48 3e 30 0a 20 20 7b 0a 20 20 20 20 50 EPTH>0. {. P
2045e 61 72 73 65 20 2a 70 50 61 72 73 65 20 3d 20 70 arse *pParse = p
2045f 4e 43 2d 3e 70 50 61 72 73 65 3b 0a 20 20 20 20 NC->pParse;.
20460 69 66 28 20 73 71 6c 69 74 65 33 45 78 70 72 43 if( sqlite3ExprC
20461 68 65 63 6b 48 65 69 67 68 74 28 70 50 61 72 73 heckHeight(pPars
20462 65 2c 20 70 45 78 70 72 2d 3e 6e 48 65 69 67 68 e, pExpr->nHeigh
20463 74 2b 70 4e 43 2d 3e 70 50 61 72 73 65 2d 3e 6e t+pNC->pParse->n
20464 48 65 69 67 68 74 29 20 29 7b 0a 20 20 20 20 20 Height) ){.
20465 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d return 1;. }
20466 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 48 65 . pParse->nHe
20467 69 67 68 74 20 2b 3d 20 70 45 78 70 72 2d 3e 6e ight += pExpr->n
20468 48 65 69 67 68 74 3b 0a 20 20 7d 0a 23 65 6e 64 Height;. }.#end
20469 69 66 0a 20 20 73 61 76 65 64 48 61 73 41 67 67 if. savedHasAgg
2046a 20 3d 20 70 4e 43 2d 3e 68 61 73 41 67 67 3b 0a = pNC->hasAgg;.
2046b 20 20 70 4e 43 2d 3e 68 61 73 41 67 67 20 3d 20 pNC->hasAgg =
2046c 30 3b 0a 20 20 77 2e 78 45 78 70 72 43 61 6c 6c 0;. w.xExprCall
2046d 62 61 63 6b 20 3d 20 72 65 73 6f 6c 76 65 45 78 back = resolveEx
2046e 70 72 53 74 65 70 3b 0a 20 20 77 2e 78 53 65 6c prStep;. w.xSel
2046f 65 63 74 43 61 6c 6c 62 61 63 6b 20 3d 20 72 65 ectCallback = re
20470 73 6f 6c 76 65 53 65 6c 65 63 74 53 74 65 70 3b solveSelectStep;
20471 0a 20 20 77 2e 70 50 61 72 73 65 20 3d 20 70 4e . w.pParse = pN
20472 43 2d 3e 70 50 61 72 73 65 3b 0a 20 20 77 2e 75 C->pParse;. w.u
20473 2e 70 4e 43 20 3d 20 70 4e 43 3b 0a 20 20 73 71 .pNC = pNC;. sq
20474 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 26 77 lite3WalkExpr(&w
20475 2c 20 70 45 78 70 72 29 3b 0a 23 69 66 20 53 51 , pExpr);.#if SQ
20476 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45 LITE_MAX_EXPR_DE
20477 50 54 48 3e 30 0a 20 20 70 4e 43 2d 3e 70 50 61 PTH>0. pNC->pPa
20478 72 73 65 2d 3e 6e 48 65 69 67 68 74 20 2d 3d 20 rse->nHeight -=
20479 70 45 78 70 72 2d 3e 6e 48 65 69 67 68 74 3b 0a pExpr->nHeight;.
2047a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 70 4e 43 #endif. if( pNC
2047b 2d 3e 6e 45 72 72 3e 30 20 7c 7c 20 77 2e 70 50 ->nErr>0 || w.pP
2047c 61 72 73 65 2d 3e 6e 45 72 72 3e 30 20 29 7b 0a arse->nErr>0 ){.
2047d 20 20 20 20 45 78 70 72 53 65 74 50 72 6f 70 65 ExprSetPrope
2047e 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 45 72 rty(pExpr, EP_Er
2047f 72 6f 72 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 ror);. }. if(
20480 70 4e 43 2d 3e 68 61 73 41 67 67 20 29 7b 0a 20 pNC->hasAgg ){.
20481 20 20 20 45 78 70 72 53 65 74 50 72 6f 70 65 72 ExprSetProper
20482 74 79 28 70 45 78 70 72 2c 20 45 50 5f 41 67 67 ty(pExpr, EP_Agg
20483 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73 );. }else if( s
20484 61 76 65 64 48 61 73 41 67 67 20 29 7b 0a 20 20 avedHasAgg ){.
20485 20 20 70 4e 43 2d 3e 68 61 73 41 67 67 20 3d 20 pNC->hasAgg =
20486 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 1;. }. return
20487 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 ExprHasProperty(
20488 70 45 78 70 72 2c 20 45 50 5f 45 72 72 6f 72 29 pExpr, EP_Error)
20489 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 6f ;.}.../*.** Reso
2048a 6c 76 65 20 61 6c 6c 20 6e 61 6d 65 73 20 69 6e lve all names in
2048b 20 61 6c 6c 20 65 78 70 72 65 73 73 69 6f 6e 73 all expressions
2048c 20 6f 66 20 61 20 53 45 4c 45 43 54 20 61 6e 64 of a SELECT and
2048d 20 69 6e 20 61 6c 6c 0a 2a 2a 20 64 65 63 65 6e in all.** decen
2048e 64 65 6e 74 73 20 6f 66 20 74 68 65 20 53 45 4c dents of the SEL
2048f 45 43 54 2c 20 69 6e 63 6c 75 64 69 6e 67 20 63 ECT, including c
20490 6f 6d 70 6f 75 6e 64 73 20 6f 66 66 20 6f 66 20 ompounds off of
20491 70 2d 3e 70 50 72 69 6f 72 2c 0a 2a 2a 20 73 75 p->pPrior,.** su
20492 62 71 75 65 72 69 65 73 20 69 6e 20 65 78 70 72 bqueries in expr
20493 65 73 73 69 6f 6e 73 2c 20 61 6e 64 20 73 75 62 essions, and sub
20494 71 75 65 72 69 65 73 20 75 73 65 64 20 61 73 20 queries used as
20495 46 52 4f 4d 20 63 6c 61 75 73 65 0a 2a 2a 20 74 FROM clause.** t
20496 65 72 6d 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 erms..**.** See
20497 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 sqlite3ResolveEx
20498 70 72 4e 61 6d 65 73 28 29 20 66 6f 72 20 61 20 prNames() for a
20499 64 65 73 63 72 69 70 74 69 6f 6e 20 6f 66 20 74 description of t
2049a 68 65 20 6b 69 6e 64 73 20 6f 66 0a 2a 2a 20 74 he kinds of.** t
2049b 72 61 6e 73 66 6f 72 6d 61 74 69 6f 6e 73 20 74 ransformations t
2049c 68 61 74 20 6f 63 63 75 72 2e 0a 2a 2a 0a 2a 2a hat occur..**.**
2049d 20 41 6c 6c 20 53 45 4c 45 43 54 20 73 74 61 74 All SELECT stat
2049e 65 6d 65 6e 74 73 20 73 68 6f 75 6c 64 20 68 61 ements should ha
2049f 76 65 20 62 65 65 6e 20 65 78 70 61 6e 64 65 64 ve been expanded
204a0 20 75 73 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 using.** sqlite
204a1 33 53 65 6c 65 63 74 45 78 70 61 6e 64 28 29 20 3SelectExpand()
204a2 70 72 69 6f 72 20 74 6f 20 69 6e 76 6f 6b 69 6e prior to invokin
204a3 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a g this routine..
204a4 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
204a5 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65 E void sqlite3Re
204a6 73 6f 6c 76 65 53 65 6c 65 63 74 4e 61 6d 65 73 solveSelectNames
204a7 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 (. Parse *pPars
204a8 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 e, /* Th
204a9 65 20 70 61 72 73 65 72 20 63 6f 6e 74 65 78 74 e parser context
204aa 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 2c */. Select *p,
204ab 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
204ac 54 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 The SELECT state
204ad 6d 65 6e 74 20 62 65 69 6e 67 20 63 6f 64 65 64 ment being coded
204ae 2e 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 . */. NameConte
204af 78 74 20 2a 70 4f 75 74 65 72 4e 43 20 20 2f 2a xt *pOuterNC /*
204b0 20 4e 61 6d 65 20 63 6f 6e 74 65 78 74 20 66 6f Name context fo
204b1 72 20 70 61 72 65 6e 74 20 53 45 4c 45 43 54 20 r parent SELECT
204b2 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 29 7b 0a statement */.){.
204b3 20 20 57 61 6c 6b 65 72 20 77 3b 0a 0a 20 20 61 Walker w;.. a
204b4 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20 ssert( p!=0 );.
204b5 20 77 2e 78 45 78 70 72 43 61 6c 6c 62 61 63 6b w.xExprCallback
204b6 20 3d 20 72 65 73 6f 6c 76 65 45 78 70 72 53 74 = resolveExprSt
204b7 65 70 3b 0a 20 20 77 2e 78 53 65 6c 65 63 74 43 ep;. w.xSelectC
204b8 61 6c 6c 62 61 63 6b 20 3d 20 72 65 73 6f 6c 76 allback = resolv
204b9 65 53 65 6c 65 63 74 53 74 65 70 3b 0a 20 20 77 eSelectStep;. w
204ba 2e 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 .pParse = pParse
204bb 3b 0a 20 20 77 2e 75 2e 70 4e 43 20 3d 20 70 4f ;. w.u.pNC = pO
204bc 75 74 65 72 4e 43 3b 0a 20 20 73 71 6c 69 74 65 uterNC;. sqlite
204bd 33 57 61 6c 6b 53 65 6c 65 63 74 28 26 77 2c 20 3WalkSelect(&w,
204be 70 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a p);.}../********
204bf 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 72 65 ****** End of re
204c0 73 6f 6c 76 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a solve.c ********
204c1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
204c2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
204c3 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
204c4 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c ****** Begin fil
204c5 65 20 65 78 70 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a e expr.c *******
204c6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
204c7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
204c8 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 *****/./*.** 200
204c9 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 1 September 15.*
204ca 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 *.** The author
204cb 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 disclaims copyri
204cc 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 ght to this sour
204cd 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 ce code. In pla
204ce 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c ce of.** a legal
204cf 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 notice, here is
204d0 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a a blessing:.**.
204d1 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f ** May you do
204d2 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 good and not ev
204d3 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f il..** May yo
204d4 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 u find forgivene
204d5 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 ss for yourself
204d6 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 and forgive othe
204d7 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f rs..** May yo
204d8 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 u share freely,
204d9 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 never taking mor
204da 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e e than you give.
204db 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .**.************
204dc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
204dd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
204de 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
204df 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
204e0 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 This file conta
204e1 69 6e 73 20 72 6f 75 74 69 6e 65 73 20 75 73 65 ins routines use
204e2 64 20 66 6f 72 20 61 6e 61 6c 79 7a 69 6e 67 20 d for analyzing
204e3 65 78 70 72 65 73 73 69 6f 6e 73 20 61 6e 64 0a expressions and.
204e4 2a 2a 20 66 6f 72 20 67 65 6e 65 72 61 74 69 6e ** for generatin
204e5 67 20 56 44 42 45 20 63 6f 64 65 20 74 68 61 74 g VDBE code that
204e6 20 65 76 61 6c 75 61 74 65 73 20 65 78 70 72 65 evaluates expre
204e7 73 73 69 6f 6e 73 20 69 6e 20 53 51 4c 69 74 65 ssions in SQLite
204e8 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 ..*/../*.** Retu
204e9 72 6e 20 74 68 65 20 27 61 66 66 69 6e 69 74 79 rn the 'affinity
204ea 27 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 ' of the express
204eb 69 6f 6e 20 70 45 78 70 72 20 69 66 20 61 6e 79 ion pExpr if any
204ec 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 45 78 70 72 ..**.** If pExpr
204ed 20 69 73 20 61 20 63 6f 6c 75 6d 6e 2c 20 61 20 is a column, a
204ee 72 65 66 65 72 65 6e 63 65 20 74 6f 20 61 20 63 reference to a c
204ef 6f 6c 75 6d 6e 20 76 69 61 20 61 6e 20 27 41 53 olumn via an 'AS
204f0 27 20 61 6c 69 61 73 2c 0a 2a 2a 20 6f 72 20 61 ' alias,.** or a
204f1 20 73 75 62 2d 73 65 6c 65 63 74 20 77 69 74 68 sub-select with
204f2 20 61 20 63 6f 6c 75 6d 6e 20 61 73 20 74 68 65 a column as the
204f3 20 72 65 74 75 72 6e 20 76 61 6c 75 65 2c 20 74 return value, t
204f4 68 65 6e 20 74 68 65 20 0a 2a 2a 20 61 66 66 69 hen the .** affi
204f5 6e 69 74 79 20 6f 66 20 74 68 61 74 20 63 6f 6c nity of that col
204f6 75 6d 6e 20 69 73 20 72 65 74 75 72 6e 65 64 2e umn is returned.
204f7 20 4f 74 68 65 72 77 69 73 65 2c 20 30 78 30 30 Otherwise, 0x00
204f8 20 69 73 20 72 65 74 75 72 6e 65 64 2c 0a 2a 2a is returned,.**
204f9 20 69 6e 64 69 63 61 74 69 6e 67 20 6e 6f 20 61 indicating no a
204fa 66 66 69 6e 69 74 79 20 66 6f 72 20 74 68 65 20 ffinity for the
204fb 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a 0a 2a expression..**.*
204fc 2a 20 69 2e 65 2e 20 74 68 65 20 57 48 45 52 45 * i.e. the WHERE
204fd 20 63 6c 61 75 73 65 20 65 78 70 72 65 73 73 73 clause expresss
204fe 69 6f 6e 73 20 69 6e 20 74 68 65 20 66 6f 6c 6c ions in the foll
204ff 6f 77 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 73 owing statements
20500 20 61 6c 6c 0a 2a 2a 20 68 61 76 65 20 61 6e 20 all.** have an
20501 61 66 66 69 6e 69 74 79 3a 0a 2a 2a 0a 2a 2a 20 affinity:.**.**
20502 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 31 28 CREATE TABLE t1(
20503 61 29 3b 0a 2a 2a 20 53 45 4c 45 43 54 20 2a 20 a);.** SELECT *
20504 46 52 4f 4d 20 74 31 20 57 48 45 52 45 20 61 3b FROM t1 WHERE a;
20505 0a 2a 2a 20 53 45 4c 45 43 54 20 61 20 41 53 20 .** SELECT a AS
20506 62 20 46 52 4f 4d 20 74 31 20 57 48 45 52 45 20 b FROM t1 WHERE
20507 62 3b 0a 2a 2a 20 53 45 4c 45 43 54 20 2a 20 46 b;.** SELECT * F
20508 52 4f 4d 20 74 31 20 57 48 45 52 45 20 28 73 65 ROM t1 WHERE (se
20509 6c 65 63 74 20 61 20 66 72 6f 6d 20 74 31 29 3b lect a from t1);
2050a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
2050b 54 45 20 63 68 61 72 20 73 71 6c 69 74 65 33 45 TE char sqlite3E
2050c 78 70 72 41 66 66 69 6e 69 74 79 28 45 78 70 72 xprAffinity(Expr
2050d 20 2a 70 45 78 70 72 29 7b 0a 20 20 69 6e 74 20 *pExpr){. int
2050e 6f 70 20 3d 20 70 45 78 70 72 2d 3e 6f 70 3b 0a op = pExpr->op;.
2050f 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 53 45 4c if( op==TK_SEL
20510 45 43 54 20 29 7b 0a 20 20 20 20 61 73 73 65 72 ECT ){. asser
20511 74 28 20 70 45 78 70 72 2d 3e 66 6c 61 67 73 26 t( pExpr->flags&
20512 45 50 5f 78 49 73 53 65 6c 65 63 74 20 29 3b 0a EP_xIsSelect );.
20513 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 return sqlit
20514 65 33 45 78 70 72 41 66 66 69 6e 69 74 79 28 70 e3ExprAffinity(p
20515 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74 2d Expr->x.pSelect-
20516 3e 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 >pEList->a[0].pE
20517 78 70 72 29 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 xpr);. }.#ifnde
20518 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 41 f SQLITE_OMIT_CA
20519 53 54 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f ST. if( op==TK_
2051a 43 41 53 54 20 29 7b 0a 20 20 20 20 61 73 73 65 CAST ){. asse
2051b 72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 rt( !ExprHasProp
2051c 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 49 erty(pExpr, EP_I
2051d 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20 20 20 20 ntValue) );.
2051e 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 41 66 return sqlite3Af
2051f 66 69 6e 69 74 79 54 79 70 65 28 70 45 78 70 72 finityType(pExpr
20520 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0a 20 20 7d ->u.zToken);. }
20521 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 28 6f .#endif. if( (o
20522 70 3d 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e p==TK_AGG_COLUMN
20523 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d || op==TK_COLUM
20524 4e 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 52 45 47 49 N || op==TK_REGI
20525 53 54 45 52 29 20 0a 20 20 20 26 26 20 70 45 78 STER) . && pEx
20526 70 72 2d 3e 70 54 61 62 21 3d 30 0a 20 20 29 7b pr->pTab!=0. ){
20527 0a 20 20 20 20 2f 2a 20 6f 70 3d 3d 54 4b 5f 52 . /* op==TK_R
20528 45 47 49 53 54 45 52 20 26 26 20 70 45 78 70 72 EGISTER && pExpr
20529 2d 3e 70 54 61 62 21 3d 30 20 68 61 70 70 65 6e ->pTab!=0 happen
2052a 73 20 77 68 65 6e 20 70 45 78 70 72 20 77 61 73 s when pExpr was
2052b 20 6f 72 69 67 69 6e 61 6c 6c 79 0a 20 20 20 20 originally.
2052c 2a 2a 20 61 20 54 4b 5f 43 4f 4c 55 4d 4e 20 62 ** a TK_COLUMN b
2052d 75 74 20 77 61 73 20 70 72 65 76 69 6f 75 73 6c ut was previousl
2052e 79 20 65 76 61 6c 75 61 74 65 64 20 61 6e 64 20 y evaluated and
2052f 63 61 63 68 65 64 20 69 6e 20 61 20 72 65 67 69 cached in a regi
20530 73 74 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 ster */. int
20531 6a 20 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 j = pExpr->iColu
20532 6d 6e 3b 0a 20 20 20 20 69 66 28 20 6a 3c 30 20 mn;. if( j<0
20533 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f ) return SQLITE_
20534 41 46 46 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 AFF_INTEGER;.
20535 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e assert( pExpr->
20536 70 54 61 62 20 26 26 20 6a 3c 70 45 78 70 72 2d pTab && j<pExpr-
20537 3e 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 3b 0a 20 >pTab->nCol );.
20538 20 20 20 72 65 74 75 72 6e 20 70 45 78 70 72 2d return pExpr-
20539 3e 70 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d 2e 61 >pTab->aCol[j].a
2053a 66 66 69 6e 69 74 79 3b 0a 20 20 7d 0a 20 20 72 ffinity;. }. r
2053b 65 74 75 72 6e 20 70 45 78 70 72 2d 3e 61 66 66 eturn pExpr->aff
2053c 69 6e 69 74 79 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 inity;.}../*.**
2053d 53 65 74 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e Set the collatin
2053e 67 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20 65 g sequence for e
2053f 78 70 72 65 73 73 69 6f 6e 20 70 45 78 70 72 20 xpression pExpr
20540 74 6f 20 62 65 20 74 68 65 20 63 6f 6c 6c 61 74 to be the collat
20541 69 6e 67 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20 ing.** sequence
20542 6e 61 6d 65 64 20 62 79 20 70 54 6f 6b 65 6e 2e named by pToken.
20543 20 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e Return a poin
20544 74 65 72 20 74 6f 20 74 68 65 20 72 65 76 69 73 ter to the revis
20545 65 64 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a ed expression..*
20546 2a 20 54 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 * The collating
20547 73 65 71 75 65 6e 63 65 20 69 73 20 6d 61 72 6b sequence is mark
20548 65 64 20 61 73 20 22 65 78 70 6c 69 63 69 74 22 ed as "explicit"
20549 20 75 73 69 6e 67 20 74 68 65 20 45 50 5f 45 78 using the EP_Ex
2054a 70 43 6f 6c 6c 61 74 65 0a 2a 2a 20 66 6c 61 67 pCollate.** flag
2054b 2e 20 20 41 6e 20 65 78 70 6c 69 63 69 74 20 63 . An explicit c
2054c 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 ollating sequenc
2054d 65 20 77 69 6c 6c 20 6f 76 65 72 72 69 64 65 20 e will override
2054e 69 6d 70 6c 69 63 69 74 0a 2a 2a 20 63 6f 6c 6c implicit.** coll
2054f 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 73 2e ating sequences.
20550 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
20551 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 TE Expr *sqlite3
20552 45 78 70 72 53 65 74 43 6f 6c 6c 28 50 61 72 73 ExprSetColl(Pars
20553 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 e *pParse, Expr
20554 2a 70 45 78 70 72 2c 20 54 6f 6b 65 6e 20 2a 70 *pExpr, Token *p
20555 43 6f 6c 6c 4e 61 6d 65 29 7b 0a 20 20 63 68 61 CollName){. cha
20556 72 20 2a 7a 43 6f 6c 6c 20 3d 20 30 3b 20 20 20 r *zColl = 0;
20557 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 71 75 /* Dequ
20558 6f 74 65 64 20 6e 61 6d 65 20 6f 66 20 63 6f 6c oted name of col
20559 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 lation sequence
2055a 2a 2f 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 */. CollSeq *pC
2055b 6f 6c 6c 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a oll;. sqlite3 *
2055c 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b db = pParse->db;
2055d 0a 20 20 7a 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 . zColl = sqlit
2055e 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 e3NameFromToken(
2055f 64 62 2c 20 70 43 6f 6c 6c 4e 61 6d 65 29 3b 0a db, pCollName);.
20560 20 20 69 66 28 20 70 45 78 70 72 20 26 26 20 7a if( pExpr && z
20561 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 70 43 6f 6c Coll ){. pCol
20562 6c 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 l = sqlite3Locat
20563 65 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c eCollSeq(pParse,
20564 20 7a 43 6f 6c 6c 29 3b 0a 20 20 20 20 69 66 28 zColl);. if(
20565 20 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 pColl ){.
20566 70 45 78 70 72 2d 3e 70 43 6f 6c 6c 20 3d 20 70 pExpr->pColl = p
20567 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 70 45 78 70 Coll;. pExp
20568 72 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f 45 r->flags |= EP_E
20569 78 70 43 6f 6c 6c 61 74 65 3b 0a 20 20 20 20 7d xpCollate;. }
2056a 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 62 . }. sqlite3Db
2056b 46 72 65 65 28 64 62 2c 20 7a 43 6f 6c 6c 29 3b Free(db, zColl);
2056c 0a 20 20 72 65 74 75 72 6e 20 70 45 78 70 72 3b . return pExpr;
2056d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
2056e 20 74 68 65 20 64 65 66 61 75 6c 74 20 63 6f 6c the default col
2056f 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 lation sequence
20570 66 6f 72 20 74 68 65 20 65 78 70 72 65 73 73 69 for the expressi
20571 6f 6e 20 70 45 78 70 72 2e 20 49 66 0a 2a 2a 20 on pExpr. If.**
20572 74 68 65 72 65 20 69 73 20 6e 6f 20 64 65 66 61 there is no defa
20573 75 6c 74 20 63 6f 6c 6c 61 74 69 6f 6e 20 74 79 ult collation ty
20574 70 65 2c 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2f pe, return 0..*/
20575 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
20576 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 CollSeq *sqlite3
20577 45 78 70 72 43 6f 6c 6c 53 65 71 28 50 61 72 73 ExprCollSeq(Pars
20578 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 e *pParse, Expr
20579 2a 70 45 78 70 72 29 7b 0a 20 20 43 6f 6c 6c 53 *pExpr){. CollS
2057a 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 30 3b 0a 20 eq *pColl = 0;.
2057b 20 45 78 70 72 20 2a 70 20 3d 20 70 45 78 70 72 Expr *p = pExpr
2057c 3b 0a 20 20 77 68 69 6c 65 28 20 41 4c 57 41 59 ;. while( ALWAY
2057d 53 28 70 29 20 29 7b 0a 20 20 20 20 69 6e 74 20 S(p) ){. int
2057e 6f 70 3b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 op;. pColl =
2057f 70 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20 20 69 66 p->pColl;. if
20580 28 20 70 43 6f 6c 6c 20 29 20 62 72 65 61 6b 3b ( pColl ) break;
20581 0a 20 20 20 20 6f 70 20 3d 20 70 2d 3e 6f 70 3b . op = p->op;
20582 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 54 61 62 . if( p->pTab
20583 21 3d 30 20 26 26 20 28 0a 20 20 20 20 20 20 20 !=0 && (.
20584 20 6f 70 3d 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 op==TK_AGG_COLU
20585 4d 4e 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 43 4f 4c MN || op==TK_COL
20586 55 4d 4e 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 52 45 UMN || op==TK_RE
20587 47 49 53 54 45 52 20 7c 7c 20 6f 70 3d 3d 54 4b GISTER || op==TK
20588 5f 54 52 49 47 47 45 52 0a 20 20 20 20 29 29 7b _TRIGGER. )){
20589 0a 20 20 20 20 20 20 2f 2a 20 6f 70 3d 3d 54 4b . /* op==TK
2058a 5f 52 45 47 49 53 54 45 52 20 26 26 20 70 2d 3e _REGISTER && p->
2058b 70 54 61 62 21 3d 30 20 68 61 70 70 65 6e 73 20 pTab!=0 happens
2058c 77 68 65 6e 20 70 45 78 70 72 20 77 61 73 20 6f when pExpr was o
2058d 72 69 67 69 6e 61 6c 6c 79 0a 20 20 20 20 20 20 riginally.
2058e 2a 2a 20 61 20 54 4b 5f 43 4f 4c 55 4d 4e 20 62 ** a TK_COLUMN b
2058f 75 74 20 77 61 73 20 70 72 65 76 69 6f 75 73 6c ut was previousl
20590 79 20 65 76 61 6c 75 61 74 65 64 20 61 6e 64 20 y evaluated and
20591 63 61 63 68 65 64 20 69 6e 20 61 20 72 65 67 69 cached in a regi
20592 73 74 65 72 20 2a 2f 0a 20 20 20 20 20 20 63 6f ster */. co
20593 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c 6c 3b nst char *zColl;
20594 0a 20 20 20 20 20 20 69 6e 74 20 6a 20 3d 20 70 . int j = p
20595 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 ->iColumn;.
20596 20 69 66 28 20 6a 3e 3d 30 20 29 7b 0a 20 20 20 if( j>=0 ){.
20597 20 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 sqlite3 *db
20598 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 = pParse->db;.
20599 20 20 20 20 20 20 20 7a 43 6f 6c 6c 20 3d 20 70 zColl = p
2059a 2d 3e 70 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d 2e ->pTab->aCol[j].
2059b 7a 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 20 20 70 zColl;. p
2059c 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 46 69 Coll = sqlite3Fi
2059d 6e 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20 45 4e ndCollSeq(db, EN
2059e 43 28 64 62 29 2c 20 7a 43 6f 6c 6c 2c 20 30 29 C(db), zColl, 0)
2059f 3b 0a 20 20 20 20 20 20 20 20 70 45 78 70 72 2d ;. pExpr-
205a0 3e 70 43 6f 6c 6c 20 3d 20 70 43 6f 6c 6c 3b 0a >pColl = pColl;.
205a1 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 }. br
205a2 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 eak;. }. i
205a3 66 28 20 6f 70 21 3d 54 4b 5f 43 41 53 54 20 26 f( op!=TK_CAST &
205a4 26 20 6f 70 21 3d 54 4b 5f 55 50 4c 55 53 20 29 & op!=TK_UPLUS )
205a5 7b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 {. break;.
205a6 20 20 20 7d 0a 20 20 20 20 70 20 3d 20 70 2d 3e }. p = p->
205a7 70 4c 65 66 74 3b 0a 20 20 7d 0a 20 20 69 66 28 pLeft;. }. if(
205a8 20 73 71 6c 69 74 65 33 43 68 65 63 6b 43 6f 6c sqlite3CheckCol
205a9 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 43 6f lSeq(pParse, pCo
205aa 6c 6c 29 20 29 7b 20 0a 20 20 20 20 70 43 6f 6c ll) ){ . pCol
205ab 6c 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 l = 0;. }. ret
205ac 75 72 6e 20 70 43 6f 6c 6c 3b 0a 7d 0a 0a 2f 2a urn pColl;.}../*
205ad 0a 2a 2a 20 70 45 78 70 72 20 69 73 20 61 6e 20 .** pExpr is an
205ae 6f 70 65 72 61 6e 64 20 6f 66 20 61 20 63 6f 6d operand of a com
205af 70 61 72 69 73 6f 6e 20 6f 70 65 72 61 74 6f 72 parison operator
205b0 2e 20 20 61 66 66 32 20 69 73 20 74 68 65 0a 2a . aff2 is the.*
205b1 2a 20 74 79 70 65 20 61 66 66 69 6e 69 74 79 20 * type affinity
205b2 6f 66 20 74 68 65 20 6f 74 68 65 72 20 6f 70 65 of the other ope
205b3 72 61 6e 64 2e 20 20 54 68 69 73 20 72 6f 75 74 rand. This rout
205b4 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65 0a ine returns the.
205b5 2a 2a 20 74 79 70 65 20 61 66 66 69 6e 69 74 79 ** type affinity
205b6 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 that should be
205b7 75 73 65 64 20 66 6f 72 20 74 68 65 20 63 6f 6d used for the com
205b8 70 61 72 69 73 6f 6e 20 6f 70 65 72 61 74 6f 72 parison operator
205b9 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
205ba 41 54 45 20 63 68 61 72 20 73 71 6c 69 74 65 33 ATE char sqlite3
205bb 43 6f 6d 70 61 72 65 41 66 66 69 6e 69 74 79 28 CompareAffinity(
205bc 45 78 70 72 20 2a 70 45 78 70 72 2c 20 63 68 61 Expr *pExpr, cha
205bd 72 20 61 66 66 32 29 7b 0a 20 20 63 68 61 72 20 r aff2){. char
205be 61 66 66 31 20 3d 20 73 71 6c 69 74 65 33 45 78 aff1 = sqlite3Ex
205bf 70 72 41 66 66 69 6e 69 74 79 28 70 45 78 70 72 prAffinity(pExpr
205c0 29 3b 0a 20 20 69 66 28 20 61 66 66 31 20 26 26 );. if( aff1 &&
205c1 20 61 66 66 32 20 29 7b 0a 20 20 20 20 2f 2a 20 aff2 ){. /*
205c2 42 6f 74 68 20 73 69 64 65 73 20 6f 66 20 74 68 Both sides of th
205c3 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 61 72 65 e comparison are
205c4 20 63 6f 6c 75 6d 6e 73 2e 20 49 66 20 6f 6e 65 columns. If one
205c5 20 68 61 73 20 6e 75 6d 65 72 69 63 0a 20 20 20 has numeric.
205c6 20 2a 2a 20 61 66 66 69 6e 69 74 79 2c 20 75 73 ** affinity, us
205c7 65 20 74 68 61 74 2e 20 4f 74 68 65 72 77 69 73 e that. Otherwis
205c8 65 20 75 73 65 20 6e 6f 20 61 66 66 69 6e 69 74 e use no affinit
205c9 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 y.. */. if
205ca 28 20 73 71 6c 69 74 65 33 49 73 4e 75 6d 65 72 ( sqlite3IsNumer
205cb 69 63 41 66 66 69 6e 69 74 79 28 61 66 66 31 29 icAffinity(aff1)
205cc 20 7c 7c 20 73 71 6c 69 74 65 33 49 73 4e 75 6d || sqlite3IsNum
205cd 65 72 69 63 41 66 66 69 6e 69 74 79 28 61 66 66 ericAffinity(aff
205ce 32 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 2) ){. retu
205cf 72 6e 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 rn SQLITE_AFF_NU
205d0 4d 45 52 49 43 3b 0a 20 20 20 20 7d 65 6c 73 65 MERIC;. }else
205d1 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 {. return S
205d2 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b 0a QLITE_AFF_NONE;.
205d3 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 }. }else if
205d4 28 20 21 61 66 66 31 20 26 26 20 21 61 66 66 32 ( !aff1 && !aff2
205d5 20 29 7b 0a 20 20 20 20 2f 2a 20 4e 65 69 74 68 ){. /* Neith
205d6 65 72 20 73 69 64 65 20 6f 66 20 74 68 65 20 63 er side of the c
205d7 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 61 20 63 omparison is a c
205d8 6f 6c 75 6d 6e 2e 20 20 43 6f 6d 70 61 72 65 20 olumn. Compare
205d9 74 68 65 0a 20 20 20 20 2a 2a 20 72 65 73 75 6c the. ** resul
205da 74 73 20 64 69 72 65 63 74 6c 79 2e 0a 20 20 20 ts directly..
205db 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 53 */. return S
205dc 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b 0a QLITE_AFF_NONE;.
205dd 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 }else{. /*
205de 4f 6e 65 20 73 69 64 65 20 69 73 20 61 20 63 6f One side is a co
205df 6c 75 6d 6e 2c 20 74 68 65 20 6f 74 68 65 72 20 lumn, the other
205e0 69 73 20 6e 6f 74 2e 20 55 73 65 20 74 68 65 20 is not. Use the
205e1 63 6f 6c 75 6d 6e 73 20 61 66 66 69 6e 69 74 79 columns affinity
205e2 2e 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 . */. assert(
205e3 20 61 66 66 31 3d 3d 30 20 7c 7c 20 61 66 66 32 aff1==0 || aff2
205e4 3d 3d 30 20 29 3b 0a 20 20 20 20 72 65 74 75 72 ==0 );. retur
205e5 6e 20 28 61 66 66 31 20 2b 20 61 66 66 32 29 3b n (aff1 + aff2);
205e6 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 45 . }.}../*.** pE
205e7 78 70 72 20 69 73 20 61 20 63 6f 6d 70 61 72 69 xpr is a compari
205e8 73 6f 6e 20 6f 70 65 72 61 74 6f 72 2e 20 20 52 son operator. R
205e9 65 74 75 72 6e 20 74 68 65 20 74 79 70 65 20 61 eturn the type a
205ea 66 66 69 6e 69 74 79 20 74 68 61 74 20 73 68 6f ffinity that sho
205eb 75 6c 64 0a 2a 2a 20 62 65 20 61 70 70 6c 69 65 uld.** be applie
205ec 64 20 74 6f 20 62 6f 74 68 20 6f 70 65 72 61 6e d to both operan
205ed 64 73 20 70 72 69 6f 72 20 74 6f 20 64 6f 69 6e ds prior to doin
205ee 67 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e g the comparison
205ef 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72 ..*/.static char
205f0 20 63 6f 6d 70 61 72 69 73 6f 6e 41 66 66 69 6e comparisonAffin
205f1 69 74 79 28 45 78 70 72 20 2a 70 45 78 70 72 29 ity(Expr *pExpr)
205f2 7b 0a 20 20 63 68 61 72 20 61 66 66 3b 0a 20 20 {. char aff;.
205f3 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 6f assert( pExpr->o
205f4 70 3d 3d 54 4b 5f 45 51 20 7c 7c 20 70 45 78 70 p==TK_EQ || pExp
205f5 72 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e 20 7c 7c 20 r->op==TK_IN ||
205f6 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 4c 54 pExpr->op==TK_LT
205f7 20 7c 7c 0a 20 20 20 20 20 20 20 20 20 20 70 45 ||. pE
205f8 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 47 54 20 7c xpr->op==TK_GT |
205f9 7c 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f | pExpr->op==TK_
205fa 47 45 20 7c 7c 20 70 45 78 70 72 2d 3e 6f 70 3d GE || pExpr->op=
205fb 3d 54 4b 5f 4c 45 20 7c 7c 0a 20 20 20 20 20 20 =TK_LE ||.
205fc 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 pExpr->op==T
205fd 4b 5f 4e 45 20 7c 7c 20 70 45 78 70 72 2d 3e 6f K_NE || pExpr->o
205fe 70 3d 3d 54 4b 5f 49 53 20 7c 7c 20 70 45 78 70 p==TK_IS || pExp
205ff 72 2d 3e 6f 70 3d 3d 54 4b 5f 49 53 4e 4f 54 20 r->op==TK_ISNOT
20600 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45 78 );. assert( pEx
20601 70 72 2d 3e 70 4c 65 66 74 20 29 3b 0a 20 20 61 pr->pLeft );. a
20602 66 66 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 ff = sqlite3Expr
20603 41 66 66 69 6e 69 74 79 28 70 45 78 70 72 2d 3e Affinity(pExpr->
20604 70 4c 65 66 74 29 3b 0a 20 20 69 66 28 20 70 45 pLeft);. if( pE
20605 78 70 72 2d 3e 70 52 69 67 68 74 20 29 7b 0a 20 xpr->pRight ){.
20606 20 20 20 61 66 66 20 3d 20 73 71 6c 69 74 65 33 aff = sqlite3
20607 43 6f 6d 70 61 72 65 41 66 66 69 6e 69 74 79 28 CompareAffinity(
20608 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 61 pExpr->pRight, a
20609 66 66 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 ff);. }else if(
2060a 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 ExprHasProperty
2060b 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65 (pExpr, EP_xIsSe
2060c 6c 65 63 74 29 20 29 7b 0a 20 20 20 20 61 66 66 lect) ){. aff
2060d 20 3d 20 73 71 6c 69 74 65 33 43 6f 6d 70 61 72 = sqlite3Compar
2060e 65 41 66 66 69 6e 69 74 79 28 70 45 78 70 72 2d eAffinity(pExpr-
2060f 3e 78 2e 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 >x.pSelect->pELi
20610 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2c 20 st->a[0].pExpr,
20611 61 66 66 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 aff);. }else if
20612 28 20 21 61 66 66 20 29 7b 0a 20 20 20 20 61 66 ( !aff ){. af
20613 66 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e f = SQLITE_AFF_N
20614 4f 4e 45 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 ONE;. }. retur
20615 6e 20 61 66 66 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 n aff;.}../*.**
20616 70 45 78 70 72 20 69 73 20 61 20 63 6f 6d 70 61 pExpr is a compa
20617 72 69 73 6f 6e 20 65 78 70 72 65 73 73 69 6f 6e rison expression
20618 2c 20 65 67 2e 20 27 3d 27 2c 20 27 3c 27 2c 20 , eg. '=', '<',
20619 49 4e 28 2e 2e 2e 29 20 65 74 63 2e 0a 2a 2a 20 IN(...) etc..**
2061a 69 64 78 5f 61 66 66 69 6e 69 74 79 20 69 73 20 idx_affinity is
2061b 74 68 65 20 61 66 66 69 6e 69 74 79 20 6f 66 20 the affinity of
2061c 61 6e 20 69 6e 64 65 78 65 64 20 63 6f 6c 75 6d an indexed colum
2061d 6e 2e 20 52 65 74 75 72 6e 20 74 72 75 65 0a 2a n. Return true.*
2061e 2a 20 69 66 20 74 68 65 20 69 6e 64 65 78 20 77 * if the index w
2061f 69 74 68 20 61 66 66 69 6e 69 74 79 20 69 64 78 ith affinity idx
20620 5f 61 66 66 69 6e 69 74 79 20 6d 61 79 20 62 65 _affinity may be
20621 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 used to impleme
20622 6e 74 0a 2a 2a 20 74 68 65 20 63 6f 6d 70 61 72 nt.** the compar
20623 69 73 6f 6e 20 69 6e 20 70 45 78 70 72 2e 0a 2a ison in pExpr..*
20624 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
20625 20 69 6e 74 20 73 71 6c 69 74 65 33 49 6e 64 65 int sqlite3Inde
20626 78 41 66 66 69 6e 69 74 79 4f 6b 28 45 78 70 72 xAffinityOk(Expr
20627 20 2a 70 45 78 70 72 2c 20 63 68 61 72 20 69 64 *pExpr, char id
20628 78 5f 61 66 66 69 6e 69 74 79 29 7b 0a 20 20 63 x_affinity){. c
20629 68 61 72 20 61 66 66 20 3d 20 63 6f 6d 70 61 72 har aff = compar
2062a 69 73 6f 6e 41 66 66 69 6e 69 74 79 28 70 45 78 isonAffinity(pEx
2062b 70 72 29 3b 0a 20 20 73 77 69 74 63 68 28 20 61 pr);. switch( a
2062c 66 66 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 ff ){. case S
2062d 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3a 0a QLITE_AFF_NONE:.
2062e 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a return 1;.
2062f 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f case SQLITE_
20630 41 46 46 5f 54 45 58 54 3a 0a 20 20 20 20 20 20 AFF_TEXT:.
20631 72 65 74 75 72 6e 20 69 64 78 5f 61 66 66 69 6e return idx_affin
20632 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f ity==SQLITE_AFF_
20633 54 45 58 54 3b 0a 20 20 20 20 64 65 66 61 75 6c TEXT;. defaul
20634 74 3a 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 t:. return
20635 73 71 6c 69 74 65 33 49 73 4e 75 6d 65 72 69 63 sqlite3IsNumeric
20636 41 66 66 69 6e 69 74 79 28 69 64 78 5f 61 66 66 Affinity(idx_aff
20637 69 6e 69 74 79 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f inity);. }.}../
20638 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
20639 50 35 20 76 61 6c 75 65 20 74 68 61 74 20 73 68 P5 value that sh
2063a 6f 75 6c 64 20 62 65 20 75 73 65 64 20 66 6f 72 ould be used for
2063b 20 61 20 62 69 6e 61 72 79 20 63 6f 6d 70 61 72 a binary compar
2063c 69 73 6f 6e 0a 2a 2a 20 6f 70 63 6f 64 65 20 28 ison.** opcode (
2063d 4f 50 5f 45 71 2c 20 4f 50 5f 47 65 20 65 74 63 OP_Eq, OP_Ge etc
2063e 2e 29 20 75 73 65 64 20 74 6f 20 63 6f 6d 70 61 .) used to compa
2063f 72 65 20 70 45 78 70 72 31 20 61 6e 64 20 70 45 re pExpr1 and pE
20640 78 70 72 32 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 xpr2..*/.static
20641 75 38 20 62 69 6e 61 72 79 43 6f 6d 70 61 72 65 u8 binaryCompare
20642 50 35 28 45 78 70 72 20 2a 70 45 78 70 72 31 2c P5(Expr *pExpr1,
20643 20 45 78 70 72 20 2a 70 45 78 70 72 32 2c 20 69 Expr *pExpr2, i
20644 6e 74 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 7b 0a nt jumpIfNull){.
20645 20 20 75 38 20 61 66 66 20 3d 20 28 63 68 61 72 u8 aff = (char
20646 29 73 71 6c 69 74 65 33 45 78 70 72 41 66 66 69 )sqlite3ExprAffi
20647 6e 69 74 79 28 70 45 78 70 72 32 29 3b 0a 20 20 nity(pExpr2);.
20648 61 66 66 20 3d 20 28 75 38 29 73 71 6c 69 74 65 aff = (u8)sqlite
20649 33 43 6f 6d 70 61 72 65 41 66 66 69 6e 69 74 79 3CompareAffinity
2064a 28 70 45 78 70 72 31 2c 20 61 66 66 29 20 7c 20 (pExpr1, aff) |
2064b 28 75 38 29 6a 75 6d 70 49 66 4e 75 6c 6c 3b 0a (u8)jumpIfNull;.
2064c 20 20 72 65 74 75 72 6e 20 61 66 66 3b 0a 7d 0a return aff;.}.
2064d 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 ./*.** Return a
2064e 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 63 pointer to the c
2064f 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 ollation sequenc
20650 65 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 e that should be
20651 20 75 73 65 64 20 62 79 0a 2a 2a 20 61 20 62 69 used by.** a bi
20652 6e 61 72 79 20 63 6f 6d 70 61 72 69 73 6f 6e 20 nary comparison
20653 6f 70 65 72 61 74 6f 72 20 63 6f 6d 70 61 72 69 operator compari
20654 6e 67 20 70 4c 65 66 74 20 61 6e 64 20 70 52 69 ng pLeft and pRi
20655 67 68 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 ght..**.** If th
20656 65 20 6c 65 66 74 20 68 61 6e 64 20 65 78 70 72 e left hand expr
20657 65 73 73 69 6f 6e 20 68 61 73 20 61 20 63 6f 6c ession has a col
20658 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 lating sequence
20659 74 79 70 65 2c 20 74 68 65 6e 20 69 74 20 69 73 type, then it is
2065a 0a 2a 2a 20 75 73 65 64 2e 20 4f 74 68 65 72 77 .** used. Otherw
2065b 69 73 65 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f ise the collatio
2065c 6e 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20 74 n sequence for t
2065d 68 65 20 72 69 67 68 74 20 68 61 6e 64 20 65 78 he right hand ex
2065e 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 69 73 20 75 pression.** is u
2065f 73 65 64 2c 20 6f 72 20 74 68 65 20 64 65 66 61 sed, or the defa
20660 75 6c 74 20 28 42 49 4e 41 52 59 29 20 69 66 20 ult (BINARY) if
20661 6e 65 69 74 68 65 72 20 65 78 70 72 65 73 73 69 neither expressi
20662 6f 6e 20 68 61 73 20 61 20 63 6f 6c 6c 61 74 69 on has a collati
20663 6e 67 0a 2a 2a 20 74 79 70 65 2e 0a 2a 2a 0a 2a ng.** type..**.*
20664 2a 20 41 72 67 75 6d 65 6e 74 20 70 52 69 67 68 * Argument pRigh
20665 74 20 28 62 75 74 20 6e 6f 74 20 70 4c 65 66 74 t (but not pLeft
20666 29 20 6d 61 79 20 62 65 20 61 20 6e 75 6c 6c 20 ) may be a null
20667 70 6f 69 6e 74 65 72 2e 20 49 6e 20 74 68 69 73 pointer. In this
20668 20 63 61 73 65 2c 0a 2a 2a 20 69 74 20 69 73 20 case,.** it is
20669 6e 6f 74 20 63 6f 6e 73 69 64 65 72 65 64 2e 0a not considered..
2066a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
2066b 45 20 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 E CollSeq *sqlit
2066c 65 33 42 69 6e 61 72 79 43 6f 6d 70 61 72 65 43 e3BinaryCompareC
2066d 6f 6c 6c 53 65 71 28 0a 20 20 50 61 72 73 65 20 ollSeq(. Parse
2066e 2a 70 50 61 72 73 65 2c 20 0a 20 20 45 78 70 72 *pParse, . Expr
2066f 20 2a 70 4c 65 66 74 2c 20 0a 20 20 45 78 70 72 *pLeft, . Expr
20670 20 2a 70 52 69 67 68 74 0a 29 7b 0a 20 20 43 6f *pRight.){. Co
20671 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20 20 llSeq *pColl;.
20672 61 73 73 65 72 74 28 20 70 4c 65 66 74 20 29 3b assert( pLeft );
20673 0a 20 20 69 66 28 20 70 4c 65 66 74 2d 3e 66 6c . if( pLeft->fl
20674 61 67 73 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c ags & EP_ExpColl
20675 61 74 65 20 29 7b 0a 20 20 20 20 61 73 73 65 72 ate ){. asser
20676 74 28 20 70 4c 65 66 74 2d 3e 70 43 6f 6c 6c 20 t( pLeft->pColl
20677 29 3b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 70 );. pColl = p
20678 4c 65 66 74 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 7d Left->pColl;. }
20679 65 6c 73 65 20 69 66 28 20 70 52 69 67 68 74 20 else if( pRight
2067a 26 26 20 70 52 69 67 68 74 2d 3e 66 6c 61 67 73 && pRight->flags
2067b 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 & EP_ExpCollate
2067c 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
2067d 70 52 69 67 68 74 2d 3e 70 43 6f 6c 6c 20 29 3b pRight->pColl );
2067e 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 70 52 69 . pColl = pRi
2067f 67 68 74 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 7d 65 ght->pColl;. }e
20680 6c 73 65 7b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d lse{. pColl =
20681 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c sqlite3ExprColl
20682 53 65 71 28 70 50 61 72 73 65 2c 20 70 4c 65 66 Seq(pParse, pLef
20683 74 29 3b 0a 20 20 20 20 69 66 28 20 21 70 43 6f t);. if( !pCo
20684 6c 6c 20 29 7b 0a 20 20 20 20 20 20 70 43 6f 6c ll ){. pCol
20685 6c 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 l = sqlite3ExprC
20686 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 ollSeq(pParse, p
20687 52 69 67 68 74 29 3b 0a 20 20 20 20 7d 0a 20 20 Right);. }.
20688 7d 0a 20 20 72 65 74 75 72 6e 20 70 43 6f 6c 6c }. return pColl
20689 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 ;.}../*.** Gener
2068a 61 74 65 20 74 68 65 20 6f 70 65 72 61 6e 64 73 ate the operands
2068b 20 66 6f 72 20 61 20 63 6f 6d 70 61 72 69 73 6f for a compariso
2068c 6e 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 42 65 n operation. Be
2068d 66 6f 72 65 0a 2a 2a 20 67 65 6e 65 72 61 74 69 fore.** generati
2068e 6e 67 20 74 68 65 20 63 6f 64 65 20 66 6f 72 20 ng the code for
2068f 65 61 63 68 20 6f 70 65 72 61 6e 64 2c 20 73 65 each operand, se
20690 74 20 74 68 65 20 45 50 5f 41 6e 79 41 66 66 0a t the EP_AnyAff.
20691 2a 2a 20 66 6c 61 67 20 6f 6e 20 74 68 65 20 65 ** flag on the e
20692 78 70 72 65 73 73 69 6f 6e 20 73 6f 20 74 68 61 xpression so tha
20693 74 20 69 74 20 77 69 6c 6c 20 62 65 20 61 62 6c t it will be abl
20694 65 20 74 6f 20 75 73 65 64 20 61 0a 2a 2a 20 63 e to used a.** c
20695 61 63 68 65 64 20 63 6f 6c 75 6d 6e 20 76 61 6c ached column val
20696 75 65 20 74 68 61 74 20 68 61 73 20 70 72 65 76 ue that has prev
20697 69 6f 75 73 6c 79 20 75 6e 64 65 72 67 6f 6e 65 iously undergone
20698 20 61 6e 0a 2a 2a 20 61 66 66 69 6e 69 74 79 20 an.** affinity
20699 63 68 61 6e 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 change..*/.stati
2069a 63 20 76 6f 69 64 20 63 6f 64 65 43 6f 6d 70 61 c void codeCompa
2069b 72 65 4f 70 65 72 61 6e 64 73 28 0a 20 20 50 61 reOperands(. Pa
2069c 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 rse *pParse,
2069d 2f 2a 20 50 61 72 73 69 6e 67 20 61 6e 64 20 63 /* Parsing and c
2069e 6f 64 65 20 67 65 6e 65 72 61 74 69 6e 67 20 63 ode generating c
2069f 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 ontext */. Expr
206a0 20 2a 70 4c 65 66 74 2c 20 20 20 20 20 20 2f 2a *pLeft, /*
206a1 20 54 68 65 20 6c 65 66 74 20 6f 70 65 72 61 6e The left operan
206a2 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 52 65 67 d */. int *pReg
206a3 4c 65 66 74 2c 20 20 20 20 2f 2a 20 52 65 67 69 Left, /* Regi
206a4 73 74 65 72 20 77 68 65 72 65 20 6c 65 66 74 20 ster where left
206a5 6f 70 65 72 61 6e 64 20 69 73 20 73 74 6f 72 65 operand is store
206a6 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 46 72 65 d */. int *pFre
206a7 65 4c 65 66 74 2c 20 20 20 2f 2a 20 46 72 65 65 eLeft, /* Free
206a8 20 74 68 69 73 20 72 65 67 69 73 74 65 72 20 77 this register w
206a9 68 65 6e 20 64 6f 6e 65 20 2a 2f 0a 20 20 45 78 hen done */. Ex
206aa 70 72 20 2a 70 52 69 67 68 74 2c 20 20 20 20 20 pr *pRight,
206ab 2f 2a 20 54 68 65 20 72 69 67 68 74 20 6f 70 65 /* The right ope
206ac 72 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 rand */. int *p
206ad 52 65 67 52 69 67 68 74 2c 20 20 20 2f 2a 20 52 RegRight, /* R
206ae 65 67 69 73 74 65 72 20 77 68 65 72 65 20 72 69 egister where ri
206af 67 68 74 20 6f 70 65 72 61 6e 64 20 69 73 20 73 ght operand is s
206b0 74 6f 72 65 64 20 2a 2f 0a 20 20 69 6e 74 20 2a tored */. int *
206b1 70 46 72 65 65 52 69 67 68 74 20 20 20 2f 2a 20 pFreeRight /*
206b2 57 72 69 74 65 20 74 65 6d 70 20 72 65 67 69 73 Write temp regis
206b3 74 65 72 20 66 6f 72 20 72 69 67 68 74 20 6f 70 ter for right op
206b4 65 72 61 6e 64 20 74 68 65 72 65 20 2a 2f 0a 29 erand there */.)
206b5 7b 0a 20 20 77 68 69 6c 65 28 20 70 4c 65 66 74 {. while( pLeft
206b6 2d 3e 6f 70 3d 3d 54 4b 5f 55 50 4c 55 53 20 29 ->op==TK_UPLUS )
206b7 20 70 4c 65 66 74 20 3d 20 70 4c 65 66 74 2d 3e pLeft = pLeft->
206b8 70 4c 65 66 74 3b 0a 20 20 70 4c 65 66 74 2d 3e pLeft;. pLeft->
206b9 66 6c 61 67 73 20 7c 3d 20 45 50 5f 41 6e 79 41 flags |= EP_AnyA
206ba 66 66 3b 0a 20 20 2a 70 52 65 67 4c 65 66 74 20 ff;. *pRegLeft
206bb 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 = sqlite3ExprCod
206bc 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70 4c eTemp(pParse, pL
206bd 65 66 74 2c 20 70 46 72 65 65 4c 65 66 74 29 3b eft, pFreeLeft);
206be 0a 20 20 77 68 69 6c 65 28 20 70 52 69 67 68 74 . while( pRight
206bf 2d 3e 6f 70 3d 3d 54 4b 5f 55 50 4c 55 53 20 29 ->op==TK_UPLUS )
206c0 20 70 52 69 67 68 74 20 3d 20 70 52 69 67 68 74 pRight = pRight
206c1 2d 3e 70 4c 65 66 74 3b 0a 20 20 70 52 69 67 68 ->pLeft;. pRigh
206c2 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f 41 t->flags |= EP_A
206c3 6e 79 41 66 66 3b 0a 20 20 2a 70 52 65 67 52 69 nyAff;. *pRegRi
206c4 67 68 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 ght = sqlite3Exp
206c5 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65 rCodeTemp(pParse
206c6 2c 20 70 52 69 67 68 74 2c 20 70 46 72 65 65 52 , pRight, pFreeR
206c7 69 67 68 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 ight);.}../*.**
206c8 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f Generate code fo
206c9 72 20 61 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f r a comparison o
206ca 70 65 72 61 74 6f 72 2e 0a 2a 2f 0a 73 74 61 74 perator..*/.stat
206cb 69 63 20 69 6e 74 20 63 6f 64 65 43 6f 6d 70 61 ic int codeCompa
206cc 72 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 re(. Parse *pPa
206cd 72 73 65 2c 20 20 20 20 2f 2a 20 54 68 65 20 70 rse, /* The p
206ce 61 72 73 69 6e 67 20 28 61 6e 64 20 63 6f 64 65 arsing (and code
206cf 20 67 65 6e 65 72 61 74 69 6e 67 29 20 63 6f 6e generating) con
206d0 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a text */. Expr *
206d1 70 4c 65 66 74 2c 20 20 20 20 20 20 2f 2a 20 54 pLeft, /* T
206d2 68 65 20 6c 65 66 74 20 6f 70 65 72 61 6e 64 20 he left operand
206d3 2a 2f 0a 20 20 45 78 70 72 20 2a 70 52 69 67 68 */. Expr *pRigh
206d4 74 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 72 69 t, /* The ri
206d5 67 68 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 ght operand */.
206d6 20 69 6e 74 20 6f 70 63 6f 64 65 2c 20 20 20 20 int opcode,
206d7 20 20 20 2f 2a 20 54 68 65 20 63 6f 6d 70 61 72 /* The compar
206d8 69 73 6f 6e 20 6f 70 63 6f 64 65 20 2a 2f 0a 20 ison opcode */.
206d9 20 69 6e 74 20 69 6e 31 2c 20 69 6e 74 20 69 6e int in1, int in
206da 32 2c 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68 2, /* Register h
206db 6f 6c 64 69 6e 67 20 6f 70 65 72 61 6e 64 73 20 olding operands
206dc 2a 2f 0a 20 20 69 6e 74 20 64 65 73 74 2c 20 20 */. int dest,
206dd 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68 /* Jump h
206de 65 72 65 20 69 66 20 74 72 75 65 2e 20 20 2a 2f ere if true. */
206df 0a 20 20 69 6e 74 20 6a 75 6d 70 49 66 4e 75 6c . int jumpIfNul
206e0 6c 20 20 20 20 2f 2a 20 49 66 20 74 72 75 65 2c l /* If true,
206e1 20 6a 75 6d 70 20 69 66 20 65 69 74 68 65 72 20 jump if either
206e2 6f 70 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c 20 operand is NULL
206e3 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 70 35 3b 0a */.){. int p5;.
206e4 20 20 69 6e 74 20 61 64 64 72 3b 0a 20 20 43 6f int addr;. Co
206e5 6c 6c 53 65 71 20 2a 70 34 3b 0a 0a 20 20 70 34 llSeq *p4;.. p4
206e6 20 3d 20 73 71 6c 69 74 65 33 42 69 6e 61 72 79 = sqlite3Binary
206e7 43 6f 6d 70 61 72 65 43 6f 6c 6c 53 65 71 28 70 CompareCollSeq(p
206e8 50 61 72 73 65 2c 20 70 4c 65 66 74 2c 20 70 52 Parse, pLeft, pR
206e9 69 67 68 74 29 3b 0a 20 20 70 35 20 3d 20 62 69 ight);. p5 = bi
206ea 6e 61 72 79 43 6f 6d 70 61 72 65 50 35 28 70 4c naryCompareP5(pL
206eb 65 66 74 2c 20 70 52 69 67 68 74 2c 20 6a 75 6d eft, pRight, jum
206ec 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 61 64 64 72 pIfNull);. addr
206ed 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 = sqlite3VdbeAd
206ee 64 4f 70 34 28 70 50 61 72 73 65 2d 3e 70 56 64 dOp4(pParse->pVd
206ef 62 65 2c 20 6f 70 63 6f 64 65 2c 20 69 6e 32 2c be, opcode, in2,
206f0 20 64 65 73 74 2c 20 69 6e 31 2c 0a 20 20 20 20 dest, in1,.
206f1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
206f2 20 20 20 20 20 20 20 28 76 6f 69 64 2a 29 70 34 (void*)p4
206f3 2c 20 50 34 5f 43 4f 4c 4c 53 45 51 29 3b 0a 20 , P4_COLLSEQ);.
206f4 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e sqlite3VdbeChan
206f5 67 65 50 35 28 70 50 61 72 73 65 2d 3e 70 56 64 geP5(pParse->pVd
206f6 62 65 2c 20 28 75 38 29 70 35 29 3b 0a 20 20 69 be, (u8)p5);. i
206f7 66 28 20 28 70 35 20 26 20 53 51 4c 49 54 45 5f f( (p5 & SQLITE_
206f8 41 46 46 5f 4d 41 53 4b 29 21 3d 53 51 4c 49 54 AFF_MASK)!=SQLIT
206f9 45 5f 41 46 46 5f 4e 4f 4e 45 20 29 7b 0a 20 20 E_AFF_NONE ){.
206fa 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 sqlite3ExprCac
206fb 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65 heAffinityChange
206fc 28 70 50 61 72 73 65 2c 20 69 6e 31 2c 20 31 29 (pParse, in1, 1)
206fd 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 ;. sqlite3Exp
206fe 72 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 rCacheAffinityCh
206ff 61 6e 67 65 28 70 50 61 72 73 65 2c 20 69 6e 32 ange(pParse, in2
20700 2c 20 31 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 , 1);. }. retu
20701 72 6e 20 61 64 64 72 3b 0a 7d 0a 0a 23 69 66 20 rn addr;.}..#if
20702 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f SQLITE_MAX_EXPR_
20703 44 45 50 54 48 3e 30 0a 2f 2a 0a 2a 2a 20 43 68 DEPTH>0./*.** Ch
20704 65 63 6b 20 74 68 61 74 20 61 72 67 75 6d 65 6e eck that argumen
20705 74 20 6e 48 65 69 67 68 74 20 69 73 20 6c 65 73 t nHeight is les
20706 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 s than or equal
20707 74 6f 20 74 68 65 20 6d 61 78 69 6d 75 6d 0a 2a to the maximum.*
20708 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 64 65 70 * expression dep
20709 74 68 20 61 6c 6c 6f 77 65 64 2e 20 49 66 20 69 th allowed. If i
2070a 74 20 69 73 20 6e 6f 74 2c 20 6c 65 61 76 65 20 t is not, leave
2070b 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 an error message
2070c 20 69 6e 0a 2a 2a 20 70 50 61 72 73 65 2e 0a 2a in.** pParse..*
2070d 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
2070e 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 int sqlite3Expr
2070f 43 68 65 63 6b 48 65 69 67 68 74 28 50 61 72 73 CheckHeight(Pars
20710 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 6e e *pParse, int n
20711 48 65 69 67 68 74 29 7b 0a 20 20 69 6e 74 20 72 Height){. int r
20712 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
20713 20 69 6e 74 20 6d 78 48 65 69 67 68 74 20 3d 20 int mxHeight =
20714 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61 4c 69 6d pParse->db->aLim
20715 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f it[SQLITE_LIMIT_
20716 45 58 50 52 5f 44 45 50 54 48 5d 3b 0a 20 20 69 EXPR_DEPTH];. i
20717 66 28 20 6e 48 65 69 67 68 74 3e 6d 78 48 65 69 f( nHeight>mxHei
20718 67 68 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 ght ){. sqlit
20719 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 e3ErrorMsg(pPars
2071a 65 2c 20 0a 20 20 20 20 20 20 20 22 45 78 70 72 e, . "Expr
2071b 65 73 73 69 6f 6e 20 74 72 65 65 20 69 73 20 74 ession tree is t
2071c 6f 6f 20 6c 61 72 67 65 20 28 6d 61 78 69 6d 75 oo large (maximu
2071d 6d 20 64 65 70 74 68 20 25 64 29 22 2c 20 6d 78 m depth %d)", mx
2071e 48 65 69 67 68 74 0a 20 20 20 20 29 3b 0a 20 20 Height. );.
2071f 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 rc = SQLITE_ER
20720 52 4f 52 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 ROR;. }. retur
20721 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 54 68 65 20 n rc;.}../* The
20722 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 72 65 65 20 following three
20723 66 75 6e 63 74 69 6f 6e 73 2c 20 68 65 69 67 68 functions, heigh
20724 74 4f 66 45 78 70 72 28 29 2c 20 68 65 69 67 68 tOfExpr(), heigh
20725 74 4f 66 45 78 70 72 4c 69 73 74 28 29 0a 2a 2a tOfExprList().**
20726 20 61 6e 64 20 68 65 69 67 68 74 4f 66 53 65 6c and heightOfSel
20727 65 63 74 28 29 2c 20 61 72 65 20 75 73 65 64 20 ect(), are used
20728 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 to determine the
20729 20 6d 61 78 69 6d 75 6d 20 68 65 69 67 68 74 0a maximum height.
2072a 2a 2a 20 6f 66 20 61 6e 79 20 65 78 70 72 65 73 ** of any expres
2072b 73 69 6f 6e 20 74 72 65 65 20 72 65 66 65 72 65 sion tree refere
2072c 6e 63 65 64 20 62 79 20 74 68 65 20 73 74 72 75 nced by the stru
2072d 63 74 75 72 65 20 70 61 73 73 65 64 20 61 73 20 cture passed as
2072e 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 61 72 67 the.** first arg
2072f 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 ument..**.** If
20730 74 68 69 73 20 6d 61 78 69 6d 75 6d 20 68 65 69 this maximum hei
20731 67 68 74 20 69 73 20 67 72 65 61 74 65 72 20 74 ght is greater t
20732 68 61 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 han the current
20733 76 61 6c 75 65 20 70 6f 69 6e 74 65 64 0a 2a 2a value pointed.**
20734 20 74 6f 20 62 79 20 70 6e 48 65 69 67 68 74 2c to by pnHeight,
20735 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 the second para
20736 6d 65 74 65 72 2c 20 74 68 65 6e 20 73 65 74 20 meter, then set
20737 2a 70 6e 48 65 69 67 68 74 20 74 6f 20 74 68 61 *pnHeight to tha
20738 74 0a 2a 2a 20 76 61 6c 75 65 2e 0a 2a 2f 0a 73 t.** value..*/.s
20739 74 61 74 69 63 20 76 6f 69 64 20 68 65 69 67 68 tatic void heigh
2073a 74 4f 66 45 78 70 72 28 45 78 70 72 20 2a 70 2c tOfExpr(Expr *p,
2073b 20 69 6e 74 20 2a 70 6e 48 65 69 67 68 74 29 7b int *pnHeight){
2073c 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 . if( p ){.
2073d 69 66 28 20 70 2d 3e 6e 48 65 69 67 68 74 3e 2a if( p->nHeight>*
2073e 70 6e 48 65 69 67 68 74 20 29 7b 0a 20 20 20 20 pnHeight ){.
2073f 20 20 2a 70 6e 48 65 69 67 68 74 20 3d 20 70 2d *pnHeight = p-
20740 3e 6e 48 65 69 67 68 74 3b 0a 20 20 20 20 7d 0a >nHeight;. }.
20741 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 }.}.static voi
20742 64 20 68 65 69 67 68 74 4f 66 45 78 70 72 4c 69 d heightOfExprLi
20743 73 74 28 45 78 70 72 4c 69 73 74 20 2a 70 2c 20 st(ExprList *p,
20744 69 6e 74 20 2a 70 6e 48 65 69 67 68 74 29 7b 0a int *pnHeight){.
20745 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 69 if( p ){. i
20746 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d nt i;. for(i=
20747 30 3b 20 69 3c 70 2d 3e 6e 45 78 70 72 3b 20 69 0; i<p->nExpr; i
20748 2b 2b 29 7b 0a 20 20 20 20 20 20 68 65 69 67 68 ++){. heigh
20749 74 4f 66 45 78 70 72 28 70 2d 3e 61 5b 69 5d 2e tOfExpr(p->a[i].
2074a 70 45 78 70 72 2c 20 70 6e 48 65 69 67 68 74 29 pExpr, pnHeight)
2074b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 73 74 ;. }. }.}.st
2074c 61 74 69 63 20 76 6f 69 64 20 68 65 69 67 68 74 atic void height
2074d 4f 66 53 65 6c 65 63 74 28 53 65 6c 65 63 74 20 OfSelect(Select
2074e 2a 70 2c 20 69 6e 74 20 2a 70 6e 48 65 69 67 68 *p, int *pnHeigh
2074f 74 29 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 t){. if( p ){.
20750 20 20 20 68 65 69 67 68 74 4f 66 45 78 70 72 28 heightOfExpr(
20751 70 2d 3e 70 57 68 65 72 65 2c 20 70 6e 48 65 69 p->pWhere, pnHei
20752 67 68 74 29 3b 0a 20 20 20 20 68 65 69 67 68 74 ght);. height
20753 4f 66 45 78 70 72 28 70 2d 3e 70 48 61 76 69 6e OfExpr(p->pHavin
20754 67 2c 20 70 6e 48 65 69 67 68 74 29 3b 0a 20 20 g, pnHeight);.
20755 20 20 68 65 69 67 68 74 4f 66 45 78 70 72 28 70 heightOfExpr(p
20756 2d 3e 70 4c 69 6d 69 74 2c 20 70 6e 48 65 69 67 ->pLimit, pnHeig
20757 68 74 29 3b 0a 20 20 20 20 68 65 69 67 68 74 4f ht);. heightO
20758 66 45 78 70 72 28 70 2d 3e 70 4f 66 66 73 65 74 fExpr(p->pOffset
20759 2c 20 70 6e 48 65 69 67 68 74 29 3b 0a 20 20 20 , pnHeight);.
2075a 20 68 65 69 67 68 74 4f 66 45 78 70 72 4c 69 73 heightOfExprLis
2075b 74 28 70 2d 3e 70 45 4c 69 73 74 2c 20 70 6e 48 t(p->pEList, pnH
2075c 65 69 67 68 74 29 3b 0a 20 20 20 20 68 65 69 67 eight);. heig
2075d 68 74 4f 66 45 78 70 72 4c 69 73 74 28 70 2d 3e htOfExprList(p->
2075e 70 47 72 6f 75 70 42 79 2c 20 70 6e 48 65 69 67 pGroupBy, pnHeig
2075f 68 74 29 3b 0a 20 20 20 20 68 65 69 67 68 74 4f ht);. heightO
20760 66 45 78 70 72 4c 69 73 74 28 70 2d 3e 70 4f 72 fExprList(p->pOr
20761 64 65 72 42 79 2c 20 70 6e 48 65 69 67 68 74 29 derBy, pnHeight)
20762 3b 0a 20 20 20 20 68 65 69 67 68 74 4f 66 53 65 ;. heightOfSe
20763 6c 65 63 74 28 70 2d 3e 70 50 72 69 6f 72 2c 20 lect(p->pPrior,
20764 70 6e 48 65 69 67 68 74 29 3b 0a 20 20 7d 0a 7d pnHeight);. }.}
20765 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 ../*.** Set the
20766 45 78 70 72 2e 6e 48 65 69 67 68 74 20 76 61 72 Expr.nHeight var
20767 69 61 62 6c 65 20 69 6e 20 74 68 65 20 73 74 72 iable in the str
20768 75 63 74 75 72 65 20 70 61 73 73 65 64 20 61 73 ucture passed as
20769 20 61 6e 20 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 an .** argument
2076a 2e 20 41 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 . An expression
2076b 77 69 74 68 20 6e 6f 20 63 68 69 6c 64 72 65 6e with no children
2076c 2c 20 45 78 70 72 2e 70 4c 69 73 74 20 6f 72 20 , Expr.pList or
2076d 0a 2a 2a 20 45 78 70 72 2e 70 53 65 6c 65 63 74 .** Expr.pSelect
2076e 20 6d 65 6d 62 65 72 20 68 61 73 20 61 20 68 65 member has a he
2076f 69 67 68 74 20 6f 66 20 31 2e 20 41 6e 79 20 6f ight of 1. Any o
20770 74 68 65 72 20 65 78 70 72 65 73 73 69 6f 6e 0a ther expression.
20771 2a 2a 20 68 61 73 20 61 20 68 65 69 67 68 74 20 ** has a height
20772 65 71 75 61 6c 20 74 6f 20 74 68 65 20 6d 61 78 equal to the max
20773 69 6d 75 6d 20 68 65 69 67 68 74 20 6f 66 20 61 imum height of a
20774 6e 79 20 6f 74 68 65 72 20 0a 2a 2a 20 72 65 66 ny other .** ref
20775 65 72 65 6e 63 65 64 20 45 78 70 72 20 70 6c 75 erenced Expr plu
20776 73 20 6f 6e 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 s one..*/.static
20777 20 76 6f 69 64 20 65 78 70 72 53 65 74 48 65 69 void exprSetHei
20778 67 68 74 28 45 78 70 72 20 2a 70 29 7b 0a 20 20 ght(Expr *p){.
20779 69 6e 74 20 6e 48 65 69 67 68 74 20 3d 20 30 3b int nHeight = 0;
2077a 0a 20 20 68 65 69 67 68 74 4f 66 45 78 70 72 28 . heightOfExpr(
2077b 70 2d 3e 70 4c 65 66 74 2c 20 26 6e 48 65 69 67 p->pLeft, &nHeig
2077c 68 74 29 3b 0a 20 20 68 65 69 67 68 74 4f 66 45 ht);. heightOfE
2077d 78 70 72 28 70 2d 3e 70 52 69 67 68 74 2c 20 26 xpr(p->pRight, &
2077e 6e 48 65 69 67 68 74 29 3b 0a 20 20 69 66 28 20 nHeight);. if(
2077f 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 ExprHasProperty(
20780 70 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 p, EP_xIsSelect)
20781 20 29 7b 0a 20 20 20 20 68 65 69 67 68 74 4f 66 ){. heightOf
20782 53 65 6c 65 63 74 28 70 2d 3e 78 2e 70 53 65 6c Select(p->x.pSel
20783 65 63 74 2c 20 26 6e 48 65 69 67 68 74 29 3b 0a ect, &nHeight);.
20784 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 68 65 69 }else{. hei
20785 67 68 74 4f 66 45 78 70 72 4c 69 73 74 28 70 2d ghtOfExprList(p-
20786 3e 78 2e 70 4c 69 73 74 2c 20 26 6e 48 65 69 67 >x.pList, &nHeig
20787 68 74 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e 6e 48 ht);. }. p->nH
20788 65 69 67 68 74 20 3d 20 6e 48 65 69 67 68 74 20 eight = nHeight
20789 2b 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 + 1;.}../*.** Se
2078a 74 20 74 68 65 20 45 78 70 72 2e 6e 48 65 69 67 t the Expr.nHeig
2078b 68 74 20 76 61 72 69 61 62 6c 65 20 75 73 69 6e ht variable usin
2078c 67 20 74 68 65 20 65 78 70 72 53 65 74 48 65 69 g the exprSetHei
2078d 67 68 74 28 29 20 66 75 6e 63 74 69 6f 6e 2e 20 ght() function.
2078e 49 66 0a 2a 2a 20 74 68 65 20 68 65 69 67 68 74 If.** the height
2078f 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e is greater than
20790 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 61 6c 6c the maximum all
20791 6f 77 65 64 20 65 78 70 72 65 73 73 69 6f 6e 20 owed expression
20792 64 65 70 74 68 2c 0a 2a 2a 20 6c 65 61 76 65 20 depth,.** leave
20793 61 6e 20 65 72 72 6f 72 20 69 6e 20 70 50 61 72 an error in pPar
20794 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 se..*/.SQLITE_PR
20795 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
20796 65 33 45 78 70 72 53 65 74 48 65 69 67 68 74 28 e3ExprSetHeight(
20797 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 Parse *pParse, E
20798 78 70 72 20 2a 70 29 7b 0a 20 20 65 78 70 72 53 xpr *p){. exprS
20799 65 74 48 65 69 67 68 74 28 70 29 3b 0a 20 20 73 etHeight(p);. s
2079a 71 6c 69 74 65 33 45 78 70 72 43 68 65 63 6b 48 qlite3ExprCheckH
2079b 65 69 67 68 74 28 70 50 61 72 73 65 2c 20 70 2d eight(pParse, p-
2079c 3e 6e 48 65 69 67 68 74 29 3b 0a 7d 0a 0a 2f 2a >nHeight);.}../*
2079d 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6d .** Return the m
2079e 61 78 69 6d 75 6d 20 68 65 69 67 68 74 20 6f 66 aximum height of
2079f 20 61 6e 79 20 65 78 70 72 65 73 73 69 6f 6e 20 any expression
207a0 74 72 65 65 20 72 65 66 65 72 65 6e 63 65 64 0a tree referenced.
207a1 2a 2a 20 62 79 20 74 68 65 20 73 65 6c 65 63 74 ** by the select
207a2 20 73 74 61 74 65 6d 65 6e 74 20 70 61 73 73 65 statement passe
207a3 64 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 d as an argument
207a4 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
207a5 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 ATE int sqlite3S
207a6 65 6c 65 63 74 45 78 70 72 48 65 69 67 68 74 28 electExprHeight(
207a7 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20 69 6e Select *p){. in
207a8 74 20 6e 48 65 69 67 68 74 20 3d 20 30 3b 0a 20 t nHeight = 0;.
207a9 20 68 65 69 67 68 74 4f 66 53 65 6c 65 63 74 28 heightOfSelect(
207aa 70 2c 20 26 6e 48 65 69 67 68 74 29 3b 0a 20 20 p, &nHeight);.
207ab 72 65 74 75 72 6e 20 6e 48 65 69 67 68 74 3b 0a return nHeight;.
207ac 7d 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e }.#else. #defin
207ad 65 20 65 78 70 72 53 65 74 48 65 69 67 68 74 28 e exprSetHeight(
207ae 79 29 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c y).#endif /* SQL
207af 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50 ITE_MAX_EXPR_DEP
207b0 54 48 3e 30 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 TH>0 */../*.** T
207b1 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74 his routine is t
207b2 68 65 20 63 6f 72 65 20 61 6c 6c 6f 63 61 74 6f he core allocato
207b3 72 20 66 6f 72 20 45 78 70 72 20 6e 6f 64 65 73 r for Expr nodes
207b4 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6e 73 74 72 75 63 ..**.** Construc
207b5 74 20 61 20 6e 65 77 20 65 78 70 72 65 73 73 69 t a new expressi
207b6 6f 6e 20 6e 6f 64 65 20 61 6e 64 20 72 65 74 75 on node and retu
207b7 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 rn a pointer to
207b8 69 74 2e 20 20 4d 65 6d 6f 72 79 0a 2a 2a 20 66 it. Memory.** f
207b9 6f 72 20 74 68 69 73 20 6e 6f 64 65 20 61 6e 64 or this node and
207ba 20 66 6f 72 20 74 68 65 20 70 54 6f 6b 65 6e 20 for the pToken
207bb 61 72 67 75 6d 65 6e 74 20 69 73 20 61 20 73 69 argument is a si
207bc 6e 67 6c 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a ngle allocation.
207bd 2a 2a 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d ** obtained from
207be 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 sqlite3DbMalloc
207bf 28 29 2e 20 20 54 68 65 20 63 61 6c 6c 69 6e 67 (). The calling
207c0 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 69 73 20 function.** is
207c1 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 responsible for
207c2 6d 61 6b 69 6e 67 20 73 75 72 65 20 74 68 65 20 making sure the
207c3 6e 6f 64 65 20 65 76 65 6e 74 75 61 6c 6c 79 20 node eventually
207c4 67 65 74 73 20 66 72 65 65 64 2e 0a 2a 2a 0a 2a gets freed..**.*
207c5 2a 20 49 66 20 64 65 71 75 6f 74 65 20 69 73 20 * If dequote is
207c6 74 72 75 65 2c 20 74 68 65 6e 20 74 68 65 20 74 true, then the t
207c7 6f 6b 65 6e 20 28 69 66 20 69 74 20 65 78 69 73 oken (if it exis
207c8 74 73 29 20 69 73 20 64 65 71 75 6f 74 65 64 2e ts) is dequoted.
207c9 0a 2a 2a 20 49 66 20 64 65 71 75 6f 74 65 20 69 .** If dequote i
207ca 73 20 66 61 6c 73 65 2c 20 6e 6f 20 64 65 71 75 s false, no dequ
207cb 6f 74 69 6e 67 20 69 73 20 70 65 72 66 6f 72 6d oting is perform
207cc 61 6e 63 65 2e 20 20 54 68 65 20 64 65 51 75 6f ance. The deQuo
207cd 74 65 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 te.** parameter
207ce 69 73 20 69 67 6e 6f 72 65 64 20 69 66 20 70 54 is ignored if pT
207cf 6f 6b 65 6e 20 69 73 20 4e 55 4c 4c 20 6f 72 20 oken is NULL or
207d0 69 66 20 74 68 65 20 74 6f 6b 65 6e 20 64 6f 65 if the token doe
207d1 73 20 6e 6f 74 0a 2a 2a 20 61 70 70 65 61 72 20 s not.** appear
207d2 74 6f 20 62 65 20 71 75 6f 74 65 64 2e 20 20 49 to be quoted. I
207d3 66 20 74 68 65 20 71 75 6f 74 65 73 20 77 65 72 f the quotes wer
207d4 65 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 2e e of the form ".
207d5 2e 2e 22 20 28 64 6f 75 62 6c 65 2d 71 75 6f 74 .." (double-quot
207d6 65 73 29 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 es).** then the
207d7 45 50 5f 44 62 6c 51 75 6f 74 65 64 20 66 6c 61 EP_DblQuoted fla
207d8 67 20 69 73 20 73 65 74 20 6f 6e 20 74 68 65 20 g is set on the
207d9 65 78 70 72 65 73 73 69 6f 6e 20 6e 6f 64 65 2e expression node.
207da 0a 2a 2a 0a 2a 2a 20 53 70 65 63 69 61 6c 20 63 .**.** Special c
207db 61 73 65 3a 20 20 49 66 20 6f 70 3d 3d 54 4b 5f ase: If op==TK_
207dc 49 4e 54 45 47 45 52 20 61 6e 64 20 70 54 6f 6b INTEGER and pTok
207dd 65 6e 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 73 en points to a s
207de 74 72 69 6e 67 20 74 68 61 74 0a 2a 2a 20 63 61 tring that.** ca
207df 6e 20 62 65 20 74 72 61 6e 73 6c 61 74 65 64 20 n be translated
207e0 69 6e 74 6f 20 61 20 33 32 2d 62 69 74 20 69 6e into a 32-bit in
207e1 74 65 67 65 72 2c 20 74 68 65 6e 20 74 68 65 20 teger, then the
207e2 74 6f 6b 65 6e 20 69 73 20 6e 6f 74 0a 2a 2a 20 token is not.**
207e3 73 74 6f 72 65 64 20 69 6e 20 75 2e 7a 54 6f 6b stored in u.zTok
207e4 65 6e 2e 20 20 49 6e 73 74 65 61 64 2c 20 74 68 en. Instead, th
207e5 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 73 e integer values
207e6 20 69 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20 69 is written.** i
207e7 6e 74 6f 20 75 2e 69 56 61 6c 75 65 20 61 6e 64 nto u.iValue and
207e8 20 74 68 65 20 45 50 5f 49 6e 74 56 61 6c 75 65 the EP_IntValue
207e9 20 66 6c 61 67 20 69 73 20 73 65 74 2e 20 20 4e flag is set. N
207ea 6f 20 65 78 74 72 61 20 73 74 6f 72 61 67 65 0a o extra storage.
207eb 2a 2a 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 ** is allocated
207ec 74 6f 20 68 6f 6c 64 20 74 68 65 20 69 6e 74 65 to hold the inte
207ed 67 65 72 20 74 65 78 74 20 61 6e 64 20 74 68 65 ger text and the
207ee 20 64 65 71 75 6f 74 65 20 66 6c 61 67 20 69 73 dequote flag is
207ef 20 69 67 6e 6f 72 65 64 2e 0a 2a 2f 0a 53 51 4c ignored..*/.SQL
207f0 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 ITE_PRIVATE Expr
207f1 20 2a 73 71 6c 69 74 65 33 45 78 70 72 41 6c 6c *sqlite3ExprAll
207f2 6f 63 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 oc(. sqlite3 *d
207f3 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a b, /*
207f4 20 48 61 6e 64 6c 65 20 66 6f 72 20 73 71 6c 69 Handle for sqli
207f5 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 te3DbMallocZero(
207f6 29 20 28 6d 61 79 20 62 65 20 6e 75 6c 6c 29 20 ) (may be null)
207f7 2a 2f 0a 20 20 69 6e 74 20 6f 70 2c 20 20 20 20 */. int op,
207f8 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
207f9 45 78 70 72 65 73 73 69 6f 6e 20 6f 70 63 6f 64 Expression opcod
207fa 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 54 6f 6b e */. const Tok
207fb 65 6e 20 2a 70 54 6f 6b 65 6e 2c 20 20 20 20 2f en *pToken, /
207fc 2a 20 54 6f 6b 65 6e 20 61 72 67 75 6d 65 6e 74 * Token argument
207fd 2e 20 20 4d 69 67 68 74 20 62 65 20 4e 55 4c 4c . Might be NULL
207fe 20 2a 2f 0a 20 20 69 6e 74 20 64 65 71 75 6f 74 */. int dequot
207ff 65 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a e /*
20800 20 54 72 75 65 20 74 6f 20 64 65 71 75 6f 74 65 True to dequote
20801 20 2a 2f 0a 29 7b 0a 20 20 45 78 70 72 20 2a 70 */.){. Expr *p
20802 4e 65 77 3b 0a 20 20 69 6e 74 20 6e 45 78 74 72 New;. int nExtr
20803 61 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 56 61 a = 0;. int iVa
20804 6c 75 65 20 3d 20 30 3b 0a 0a 20 20 69 66 28 20 lue = 0;.. if(
20805 70 54 6f 6b 65 6e 20 29 7b 0a 20 20 20 20 69 66 pToken ){. if
20806 28 20 6f 70 21 3d 54 4b 5f 49 4e 54 45 47 45 52 ( op!=TK_INTEGER
20807 20 7c 7c 20 70 54 6f 6b 65 6e 2d 3e 7a 3d 3d 30 || pToken->z==0
20808 0a 20 20 20 20 20 20 20 20 20 20 7c 7c 20 73 71 . || sq
20809 6c 69 74 65 33 47 65 74 49 6e 74 33 32 28 70 54 lite3GetInt32(pT
2080a 6f 6b 65 6e 2d 3e 7a 2c 20 26 69 56 61 6c 75 65 oken->z, &iValue
2080b 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 6e 45 )==0 ){. nE
2080c 78 74 72 61 20 3d 20 70 54 6f 6b 65 6e 2d 3e 6e xtra = pToken->n
2080d 2b 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 +1;. }. }.
2080e 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 pNew = sqlite3Db
2080f 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 MallocZero(db, s
20810 69 7a 65 6f 66 28 45 78 70 72 29 2b 6e 45 78 74 izeof(Expr)+nExt
20811 72 61 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 20 ra);. if( pNew
20812 29 7b 0a 20 20 20 20 70 4e 65 77 2d 3e 6f 70 20 ){. pNew->op
20813 3d 20 28 75 38 29 6f 70 3b 0a 20 20 20 20 70 4e = (u8)op;. pN
20814 65 77 2d 3e 69 41 67 67 20 3d 20 2d 31 3b 0a 20 ew->iAgg = -1;.
20815 20 20 20 69 66 28 20 70 54 6f 6b 65 6e 20 29 7b if( pToken ){
20816 0a 20 20 20 20 20 20 69 66 28 20 6e 45 78 74 72 . if( nExtr
20817 61 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 a==0 ){.
20818 70 4e 65 77 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 pNew->flags |= E
20819 50 5f 49 6e 74 56 61 6c 75 65 3b 0a 20 20 20 20 P_IntValue;.
2081a 20 20 20 20 70 4e 65 77 2d 3e 75 2e 69 56 61 6c pNew->u.iVal
2081b 75 65 20 3d 20 69 56 61 6c 75 65 3b 0a 20 20 20 ue = iValue;.
2081c 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
2081d 20 20 69 6e 74 20 63 3b 0a 20 20 20 20 20 20 20 int c;.
2081e 20 70 4e 65 77 2d 3e 75 2e 7a 54 6f 6b 65 6e 20 pNew->u.zToken
2081f 3d 20 28 63 68 61 72 2a 29 26 70 4e 65 77 5b 31 = (char*)&pNew[1
20820 5d 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 ];. memcp
20821 79 28 70 4e 65 77 2d 3e 75 2e 7a 54 6f 6b 65 6e y(pNew->u.zToken
20822 2c 20 70 54 6f 6b 65 6e 2d 3e 7a 2c 20 70 54 6f , pToken->z, pTo
20823 6b 65 6e 2d 3e 6e 29 3b 0a 20 20 20 20 20 20 20 ken->n);.
20824 20 70 4e 65 77 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b pNew->u.zToken[
20825 70 54 6f 6b 65 6e 2d 3e 6e 5d 20 3d 20 30 3b 0a pToken->n] = 0;.
20826 20 20 20 20 20 20 20 20 69 66 28 20 64 65 71 75 if( dequ
20827 6f 74 65 20 26 26 20 6e 45 78 74 72 61 3e 3d 33 ote && nExtra>=3
20828 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 26 . &
20829 26 20 28 28 63 20 3d 20 70 54 6f 6b 65 6e 2d 3e & ((c = pToken->
2082a 7a 5b 30 5d 29 3d 3d 27 5c 27 27 20 7c 7c 20 63 z[0])=='\'' || c
2082b 3d 3d 27 22 27 20 7c 7c 20 63 3d 3d 27 5b 27 20 =='"' || c=='['
2082c 7c 7c 20 63 3d 3d 27 60 27 29 20 29 7b 0a 20 20 || c=='`') ){.
2082d 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 sqlite3D
2082e 65 71 75 6f 74 65 28 70 4e 65 77 2d 3e 75 2e 7a equote(pNew->u.z
2082f 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20 20 20 Token);.
20830 20 20 69 66 28 20 63 3d 3d 27 22 27 20 29 20 70 if( c=='"' ) p
20831 4e 65 77 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 New->flags |= EP
20832 5f 44 62 6c 51 75 6f 74 65 64 3b 0a 20 20 20 20 _DblQuoted;.
20833 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 }. }.
20834 20 20 7d 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d }.#if SQLITE_M
20835 41 58 5f 45 58 50 52 5f 44 45 50 54 48 3e 30 0a AX_EXPR_DEPTH>0.
20836 20 20 20 20 70 4e 65 77 2d 3e 6e 48 65 69 67 68 pNew->nHeigh
20837 74 20 3d 20 31 3b 0a 23 65 6e 64 69 66 20 20 0a t = 1;.#endif .
20838 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 }. return pNe
20839 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f w;.}../*.** Allo
2083a 63 61 74 65 20 61 20 6e 65 77 20 65 78 70 72 65 cate a new expre
2083b 73 73 69 6f 6e 20 6e 6f 64 65 20 66 72 6f 6d 20 ssion node from
2083c 61 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 a zero-terminate
2083d 64 20 74 6f 6b 65 6e 20 74 68 61 74 20 68 61 73 d token that has
2083e 0a 2a 2a 20 61 6c 72 65 61 64 79 20 62 65 65 6e .** already been
2083f 20 64 65 71 75 6f 74 65 64 2e 0a 2a 2f 0a 53 51 dequoted..*/.SQ
20840 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 LITE_PRIVATE Exp
20841 72 20 2a 73 71 6c 69 74 65 33 45 78 70 72 28 0a r *sqlite3Expr(.
20842 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 sqlite3 *db,
20843 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 6e /* Han
20844 64 6c 65 20 66 6f 72 20 73 71 6c 69 74 65 33 44 dle for sqlite3D
20845 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 29 20 28 6d bMallocZero() (m
20846 61 79 20 62 65 20 6e 75 6c 6c 29 20 2a 2f 0a 20 ay be null) */.
20847 20 69 6e 74 20 6f 70 2c 20 20 20 20 20 20 20 20 int op,
20848 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70 72 /* Expr
20849 65 73 73 69 6f 6e 20 6f 70 63 6f 64 65 20 2a 2f ession opcode */
2084a 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
2084b 54 6f 6b 65 6e 20 20 20 20 20 20 2f 2a 20 54 6f Token /* To
2084c 6b 65 6e 20 61 72 67 75 6d 65 6e 74 2e 20 20 4d ken argument. M
2084d 69 67 68 74 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a ight be NULL */.
2084e 29 7b 0a 20 20 54 6f 6b 65 6e 20 78 3b 0a 20 20 ){. Token x;.
2084f 78 2e 7a 20 3d 20 7a 54 6f 6b 65 6e 3b 0a 20 20 x.z = zToken;.
20850 78 2e 6e 20 3d 20 7a 54 6f 6b 65 6e 20 3f 20 73 x.n = zToken ? s
20851 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a qlite3Strlen30(z
20852 54 6f 6b 65 6e 29 20 3a 20 30 3b 0a 20 20 72 65 Token) : 0;. re
20853 74 75 72 6e 20 73 71 6c 69 74 65 33 45 78 70 72 turn sqlite3Expr
20854 41 6c 6c 6f 63 28 64 62 2c 20 6f 70 2c 20 26 78 Alloc(db, op, &x
20855 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 , 0);.}../*.** A
20856 74 74 61 63 68 20 73 75 62 74 72 65 65 73 20 70 ttach subtrees p
20857 4c 65 66 74 20 61 6e 64 20 70 52 69 67 68 74 20 Left and pRight
20858 74 6f 20 74 68 65 20 45 78 70 72 20 6e 6f 64 65 to the Expr node
20859 20 70 52 6f 6f 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 pRoot..**.** If
2085a 20 70 52 6f 6f 74 3d 3d 4e 55 4c 4c 20 74 68 61 pRoot==NULL tha
2085b 74 20 6d 65 61 6e 73 20 74 68 61 74 20 61 20 6d t means that a m
2085c 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
2085d 20 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72 error has occur
2085e 72 65 64 2e 0a 2a 2a 20 49 6e 20 74 68 61 74 20 red..** In that
2085f 63 61 73 65 2c 20 64 65 6c 65 74 65 20 74 68 65 case, delete the
20860 20 73 75 62 74 72 65 65 73 20 70 4c 65 66 74 20 subtrees pLeft
20861 61 6e 64 20 70 52 69 67 68 74 2e 0a 2a 2f 0a 53 and pRight..*/.S
20862 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
20863 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 41 74 id sqlite3ExprAt
20864 74 61 63 68 53 75 62 74 72 65 65 73 28 0a 20 20 tachSubtrees(.
20865 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 45 sqlite3 *db,. E
20866 78 70 72 20 2a 70 52 6f 6f 74 2c 0a 20 20 45 78 xpr *pRoot,. Ex
20867 70 72 20 2a 70 4c 65 66 74 2c 0a 20 20 45 78 70 pr *pLeft,. Exp
20868 72 20 2a 70 52 69 67 68 74 0a 29 7b 0a 20 20 69 r *pRight.){. i
20869 66 28 20 70 52 6f 6f 74 3d 3d 30 20 29 7b 0a 20 f( pRoot==0 ){.
2086a 20 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6d assert( db->m
2086b 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 allocFailed );.
2086c 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 sqlite3ExprDe
2086d 6c 65 74 65 28 64 62 2c 20 70 4c 65 66 74 29 3b lete(db, pLeft);
2086e 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 . sqlite3Expr
2086f 44 65 6c 65 74 65 28 64 62 2c 20 70 52 69 67 68 Delete(db, pRigh
20870 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 t);. }else{.
20871 20 69 66 28 20 70 52 69 67 68 74 20 29 7b 0a 20 if( pRight ){.
20872 20 20 20 20 20 70 52 6f 6f 74 2d 3e 70 52 69 67 pRoot->pRig
20873 68 74 20 3d 20 70 52 69 67 68 74 3b 0a 20 20 20 ht = pRight;.
20874 20 20 20 69 66 28 20 70 52 69 67 68 74 2d 3e 66 if( pRight->f
20875 6c 61 67 73 20 26 20 45 50 5f 45 78 70 43 6f 6c lags & EP_ExpCol
20876 6c 61 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20 late ){.
20877 70 52 6f 6f 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 pRoot->flags |=
20878 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 3b 0a 20 EP_ExpCollate;.
20879 20 20 20 20 20 20 20 70 52 6f 6f 74 2d 3e 70 43 pRoot->pC
2087a 6f 6c 6c 20 3d 20 70 52 69 67 68 74 2d 3e 70 43 oll = pRight->pC
2087b 6f 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 oll;. }.
2087c 20 7d 0a 20 20 20 20 69 66 28 20 70 4c 65 66 74 }. if( pLeft
2087d 20 29 7b 0a 20 20 20 20 20 20 70 52 6f 6f 74 2d ){. pRoot-
2087e 3e 70 4c 65 66 74 20 3d 20 70 4c 65 66 74 3b 0a >pLeft = pLeft;.
2087f 20 20 20 20 20 20 69 66 28 20 70 4c 65 66 74 2d if( pLeft-
20880 3e 66 6c 61 67 73 20 26 20 45 50 5f 45 78 70 43 >flags & EP_ExpC
20881 6f 6c 6c 61 74 65 20 29 7b 0a 20 20 20 20 20 20 ollate ){.
20882 20 20 70 52 6f 6f 74 2d 3e 66 6c 61 67 73 20 7c pRoot->flags |
20883 3d 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 3b = EP_ExpCollate;
20884 0a 20 20 20 20 20 20 20 20 70 52 6f 6f 74 2d 3e . pRoot->
20885 70 43 6f 6c 6c 20 3d 20 70 4c 65 66 74 2d 3e 70 pColl = pLeft->p
20886 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 Coll;. }.
20887 20 20 7d 0a 20 20 20 20 65 78 70 72 53 65 74 48 }. exprSetH
20888 65 69 67 68 74 28 70 52 6f 6f 74 29 3b 0a 20 20 eight(pRoot);.
20889 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 }.}../*.** Alloc
2088a 61 74 65 20 61 20 45 78 70 72 20 6e 6f 64 65 20 ate a Expr node
2088b 77 68 69 63 68 20 6a 6f 69 6e 73 20 61 73 20 6d which joins as m
2088c 61 6e 79 20 61 73 20 74 77 6f 20 73 75 62 74 72 any as two subtr
2088d 65 65 73 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 65 20 6f ees..**.** One o
2088e 72 20 62 6f 74 68 20 6f 66 20 74 68 65 20 73 75 r both of the su
2088f 62 74 72 65 65 73 20 63 61 6e 20 62 65 20 4e 55 btrees can be NU
20890 4c 4c 2e 20 20 52 65 74 75 72 6e 20 61 20 70 6f LL. Return a po
20891 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 65 77 inter to the new
20892 0a 2a 2a 20 45 78 70 72 20 6e 6f 64 65 2e 20 20 .** Expr node.
20893 4f 72 2c 20 69 66 20 61 6e 20 4f 4f 4d 20 65 72 Or, if an OOM er
20894 72 6f 72 20 6f 63 63 75 72 73 2c 20 73 65 74 20 ror occurs, set
20895 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c pParse->db->mall
20896 6f 63 46 61 69 6c 65 64 2c 0a 2a 2a 20 66 72 65 ocFailed,.** fre
20897 65 20 74 68 65 20 73 75 62 74 72 65 65 73 20 61 e the subtrees a
20898 6e 64 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a nd return NULL..
20899 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
2089a 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 50 E Expr *sqlite3P
2089b 45 78 70 72 28 0a 20 20 50 61 72 73 65 20 2a 70 Expr(. Parse *p
2089c 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 Parse,
2089d 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 /* Parsing conte
2089e 78 74 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 2c 20 xt */. int op,
2089f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
208a0 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 6f 70 /* Expression op
208a1 63 6f 64 65 20 2a 2f 0a 20 20 45 78 70 72 20 2a code */. Expr *
208a2 70 4c 65 66 74 2c 20 20 20 20 20 20 20 20 20 20 pLeft,
208a3 20 20 2f 2a 20 4c 65 66 74 20 6f 70 65 72 61 6e /* Left operan
208a4 64 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 52 69 d */. Expr *pRi
208a5 67 68 74 2c 20 20 20 20 20 20 20 20 20 20 20 2f ght, /
208a6 2a 20 52 69 67 68 74 20 6f 70 65 72 61 6e 64 20 * Right operand
208a7 2a 2f 0a 20 20 63 6f 6e 73 74 20 54 6f 6b 65 6e */. const Token
208a8 20 2a 70 54 6f 6b 65 6e 20 20 20 20 20 2f 2a 20 *pToken /*
208a9 41 72 67 75 6d 65 6e 74 20 74 6f 6b 65 6e 20 2a Argument token *
208aa 2f 0a 29 7b 0a 20 20 45 78 70 72 20 2a 70 20 3d /.){. Expr *p =
208ab 20 73 71 6c 69 74 65 33 45 78 70 72 41 6c 6c 6f sqlite3ExprAllo
208ac 63 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 6f 70 c(pParse->db, op
208ad 2c 20 70 54 6f 6b 65 6e 2c 20 31 29 3b 0a 20 20 , pToken, 1);.
208ae 73 71 6c 69 74 65 33 45 78 70 72 41 74 74 61 63 sqlite3ExprAttac
208af 68 53 75 62 74 72 65 65 73 28 70 50 61 72 73 65 hSubtrees(pParse
208b0 2d 3e 64 62 2c 20 70 2c 20 70 4c 65 66 74 2c 20 ->db, p, pLeft,
208b1 70 52 69 67 68 74 29 3b 0a 20 20 72 65 74 75 72 pRight);. retur
208b2 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4a 6f n p;.}../*.** Jo
208b3 69 6e 20 74 77 6f 20 65 78 70 72 65 73 73 69 6f in two expressio
208b4 6e 73 20 75 73 69 6e 67 20 61 6e 20 41 4e 44 20 ns using an AND
208b5 6f 70 65 72 61 74 6f 72 2e 20 20 49 66 20 65 69 operator. If ei
208b6 74 68 65 72 20 65 78 70 72 65 73 73 69 6f 6e 20 ther expression
208b7 69 73 0a 2a 2a 20 4e 55 4c 4c 2c 20 74 68 65 6e is.** NULL, then
208b8 20 6a 75 73 74 20 72 65 74 75 72 6e 20 74 68 65 just return the
208b9 20 6f 74 68 65 72 20 65 78 70 72 65 73 73 69 6f other expressio
208ba 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 n..*/.SQLITE_PRI
208bb 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 VATE Expr *sqlit
208bc 65 33 45 78 70 72 41 6e 64 28 73 71 6c 69 74 65 e3ExprAnd(sqlite
208bd 33 20 2a 64 62 2c 20 45 78 70 72 20 2a 70 4c 65 3 *db, Expr *pLe
208be 66 74 2c 20 45 78 70 72 20 2a 70 52 69 67 68 74 ft, Expr *pRight
208bf 29 7b 0a 20 20 69 66 28 20 70 4c 65 66 74 3d 3d ){. if( pLeft==
208c0 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 0 ){. return
208c1 70 52 69 67 68 74 3b 0a 20 20 7d 65 6c 73 65 20 pRight;. }else
208c2 69 66 28 20 70 52 69 67 68 74 3d 3d 30 20 29 7b if( pRight==0 ){
208c3 0a 20 20 20 20 72 65 74 75 72 6e 20 70 4c 65 66 . return pLef
208c4 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 t;. }else{.
208c5 45 78 70 72 20 2a 70 4e 65 77 20 3d 20 73 71 6c Expr *pNew = sql
208c6 69 74 65 33 45 78 70 72 41 6c 6c 6f 63 28 64 62 ite3ExprAlloc(db
208c7 2c 20 54 4b 5f 41 4e 44 2c 20 30 2c 20 30 29 3b , TK_AND, 0, 0);
208c8 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 . sqlite3Expr
208c9 41 74 74 61 63 68 53 75 62 74 72 65 65 73 28 64 AttachSubtrees(d
208ca 62 2c 20 70 4e 65 77 2c 20 70 4c 65 66 74 2c 20 b, pNew, pLeft,
208cb 70 52 69 67 68 74 29 3b 0a 20 20 20 20 72 65 74 pRight);. ret
208cc 75 72 6e 20 70 4e 65 77 3b 0a 20 20 7d 0a 7d 0a urn pNew;. }.}.
208cd 0a 2f 2a 0a 2a 2a 20 43 6f 6e 73 74 72 75 63 74 ./*.** Construct
208ce 20 61 20 6e 65 77 20 65 78 70 72 65 73 73 69 6f a new expressio
208cf 6e 20 6e 6f 64 65 20 66 6f 72 20 61 20 66 75 6e n node for a fun
208d0 63 74 69 6f 6e 20 77 69 74 68 20 6d 75 6c 74 69 ction with multi
208d1 70 6c 65 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 73 ple.** arguments
208d2 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
208d3 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 ATE Expr *sqlite
208d4 33 45 78 70 72 46 75 6e 63 74 69 6f 6e 28 50 61 3ExprFunction(Pa
208d5 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 rse *pParse, Exp
208d6 72 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 54 6f rList *pList, To
208d7 6b 65 6e 20 2a 70 54 6f 6b 65 6e 29 7b 0a 20 20 ken *pToken){.
208d8 45 78 70 72 20 2a 70 4e 65 77 3b 0a 20 20 73 71 Expr *pNew;. sq
208d9 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 lite3 *db = pPar
208da 73 65 2d 3e 64 62 3b 0a 20 20 61 73 73 65 72 74 se->db;. assert
208db 28 20 70 54 6f 6b 65 6e 20 29 3b 0a 20 20 70 4e ( pToken );. pN
208dc 65 77 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 ew = sqlite3Expr
208dd 41 6c 6c 6f 63 28 64 62 2c 20 54 4b 5f 46 55 4e Alloc(db, TK_FUN
208de 43 54 49 4f 4e 2c 20 70 54 6f 6b 65 6e 2c 20 31 CTION, pToken, 1
208df 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 );. if( pNew==0
208e0 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 ){. sqlite3E
208e1 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 xprListDelete(db
208e2 2c 20 70 4c 69 73 74 29 3b 20 2f 2a 20 41 76 6f , pList); /* Avo
208e3 69 64 20 6d 65 6d 6f 72 79 20 6c 65 61 6b 20 77 id memory leak w
208e4 68 65 6e 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 hen malloc fails
208e5 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 30 */. return 0
208e6 3b 0a 20 20 7d 0a 20 20 70 4e 65 77 2d 3e 78 2e ;. }. pNew->x.
208e7 70 4c 69 73 74 20 3d 20 70 4c 69 73 74 3b 0a 20 pList = pList;.
208e8 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61 assert( !ExprHa
208e9 73 50 72 6f 70 65 72 74 79 28 70 4e 65 77 2c 20 sProperty(pNew,
208ea 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 3b EP_xIsSelect) );
208eb 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 53 65 . sqlite3ExprSe
208ec 74 48 65 69 67 68 74 28 70 50 61 72 73 65 2c 20 tHeight(pParse,
208ed 70 4e 65 77 29 3b 0a 20 20 72 65 74 75 72 6e 20 pNew);. return
208ee 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 pNew;.}../*.** A
208ef 73 73 69 67 6e 20 61 20 76 61 72 69 61 62 6c 65 ssign a variable
208f0 20 6e 75 6d 62 65 72 20 74 6f 20 61 6e 20 65 78 number to an ex
208f1 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20 65 6e pression that en
208f2 63 6f 64 65 73 20 61 20 77 69 6c 64 63 61 72 64 codes a wildcard
208f3 0a 2a 2a 20 69 6e 20 74 68 65 20 6f 72 69 67 69 .** in the origi
208f4 6e 61 6c 20 53 51 4c 20 73 74 61 74 65 6d 65 6e nal SQL statemen
208f5 74 2e 20 20 0a 2a 2a 0a 2a 2a 20 57 69 6c 64 63 t. .**.** Wildc
208f6 61 72 64 73 20 63 6f 6e 73 69 73 74 69 6e 67 20 ards consisting
208f7 6f 66 20 61 20 73 69 6e 67 6c 65 20 22 3f 22 20 of a single "?"
208f8 61 72 65 20 61 73 73 69 67 6e 65 64 20 74 68 65 are assigned the
208f9 20 6e 65 78 74 20 73 65 71 75 65 6e 74 69 61 6c next sequential
208fa 0a 2a 2a 20 76 61 72 69 61 62 6c 65 20 6e 75 6d .** variable num
208fb 62 65 72 2e 0a 2a 2a 0a 2a 2a 20 57 69 6c 64 63 ber..**.** Wildc
208fc 61 72 64 73 20 6f 66 20 74 68 65 20 66 6f 72 6d ards of the form
208fd 20 22 3f 6e 6e 6e 22 20 61 72 65 20 61 73 73 69 "?nnn" are assi
208fe 67 6e 65 64 20 74 68 65 20 6e 75 6d 62 65 72 20 gned the number
208ff 22 6e 6e 6e 22 2e 20 20 57 65 20 6d 61 6b 65 0a "nnn". We make.
20900 2a 2a 20 73 75 72 65 20 22 6e 6e 6e 22 20 69 73 ** sure "nnn" is
20901 20 6e 6f 74 20 74 6f 6f 20 62 65 20 74 6f 20 61 not too be to a
20902 76 6f 69 64 20 61 20 64 65 6e 69 61 6c 20 6f 66 void a denial of
20903 20 73 65 72 76 69 63 65 20 61 74 74 61 63 6b 20 service attack
20904 77 68 65 6e 0a 2a 2a 20 74 68 65 20 53 51 4c 20 when.** the SQL
20905 73 74 61 74 65 6d 65 6e 74 20 63 6f 6d 65 73 20 statement comes
20906 66 72 6f 6d 20 61 6e 20 65 78 74 65 72 6e 61 6c from an external
20907 20 73 6f 75 72 63 65 2e 0a 2a 2a 0a 2a 2a 20 57 source..**.** W
20908 69 6c 64 63 61 72 64 73 20 6f 66 20 74 68 65 20 ildcards of the
20909 66 6f 72 6d 20 22 3a 61 61 61 22 2c 20 22 40 61 form ":aaa", "@a
2090a 61 61 22 2c 20 6f 72 20 22 24 61 61 61 22 20 61 aa", or "$aaa" a
2090b 72 65 20 61 73 73 69 67 6e 65 64 20 74 68 65 20 re assigned the
2090c 73 61 6d 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 61 same number.** a
2090d 73 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 69 s the previous i
2090e 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 73 nstance of the s
2090f 61 6d 65 20 77 69 6c 64 63 61 72 64 2e 20 20 4f ame wildcard. O
20910 72 20 69 66 20 74 68 69 73 20 69 73 20 74 68 65 r if this is the
20911 20 66 69 72 73 74 0a 2a 2a 20 69 6e 73 74 61 6e first.** instan
20912 63 65 20 6f 66 20 74 68 65 20 77 69 6c 64 63 61 ce of the wildca
20913 72 64 2c 20 74 68 65 20 6e 65 78 74 20 73 65 71 rd, the next seq
20914 75 65 6e 69 61 6c 20 76 61 72 69 61 62 6c 65 20 uenial variable
20915 6e 75 6d 62 65 72 20 69 73 0a 2a 2a 20 61 73 73 number is.** ass
20916 69 67 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 igned..*/.SQLITE
20917 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
20918 6c 69 74 65 33 45 78 70 72 41 73 73 69 67 6e 56 lite3ExprAssignV
20919 61 72 4e 75 6d 62 65 72 28 50 61 72 73 65 20 2a arNumber(Parse *
2091a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70 45 pParse, Expr *pE
2091b 78 70 72 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 xpr){. sqlite3
2091c 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 *db = pParse->db
2091d 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a ;. const char *
2091e 7a 3b 0a 0a 20 20 69 66 28 20 70 45 78 70 72 3d z;.. if( pExpr=
2091f 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 =0 ) return;. a
20920 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73 41 ssert( !ExprHasA
20921 6e 79 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 nyProperty(pExpr
20922 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 7c 45 50 , EP_IntValue|EP
20923 5f 52 65 64 75 63 65 64 7c 45 50 5f 54 6f 6b 65 _Reduced|EP_Toke
20924 6e 4f 6e 6c 79 29 20 29 3b 0a 20 20 7a 20 3d 20 nOnly) );. z =
20925 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b pExpr->u.zToken;
20926 0a 20 20 61 73 73 65 72 74 28 20 7a 21 3d 30 20 . assert( z!=0
20927 29 3b 0a 20 20 61 73 73 65 72 74 28 20 7a 5b 30 );. assert( z[0
20928 5d 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 7a 5b ]!=0 );. if( z[
20929 31 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 1]==0 ){. /*
2092a 57 69 6c 64 63 61 72 64 20 6f 66 20 74 68 65 20 Wildcard of the
2092b 66 6f 72 6d 20 22 3f 22 2e 20 20 41 73 73 69 67 form "?". Assig
2092c 6e 20 74 68 65 20 6e 65 78 74 20 76 61 72 69 61 n the next varia
2092d 62 6c 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 ble number */.
2092e 20 20 61 73 73 65 72 74 28 20 7a 5b 30 5d 3d 3d assert( z[0]==
2092f 27 3f 27 20 29 3b 0a 20 20 20 20 70 45 78 70 72 '?' );. pExpr
20930 2d 3e 69 54 61 62 6c 65 20 3d 20 2b 2b 70 50 61 ->iTable = ++pPa
20931 72 73 65 2d 3e 6e 56 61 72 3b 0a 20 20 7d 65 6c rse->nVar;. }el
20932 73 65 20 69 66 28 20 7a 5b 30 5d 3d 3d 27 3f 27 se if( z[0]=='?'
20933 20 29 7b 0a 20 20 20 20 2f 2a 20 57 69 6c 64 63 ){. /* Wildc
20934 61 72 64 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 ard of the form
20935 22 3f 6e 6e 6e 22 2e 20 20 43 6f 6e 76 65 72 74 "?nnn". Convert
20936 20 22 6e 6e 6e 22 20 74 6f 20 61 6e 20 69 6e 74 "nnn" to an int
20937 65 67 65 72 20 61 6e 64 0a 20 20 20 20 2a 2a 20 eger and. **
20938 75 73 65 20 69 74 20 61 73 20 74 68 65 20 76 61 use it as the va
20939 72 69 61 62 6c 65 20 6e 75 6d 62 65 72 20 2a 2f riable number */
2093a 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 . int i;.
2093b 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 pExpr->iTable =
2093c 69 20 3d 20 61 74 6f 69 28 28 63 68 61 72 2a 29 i = atoi((char*)
2093d 26 7a 5b 31 5d 29 3b 0a 20 20 20 20 74 65 73 74 &z[1]);. test
2093e 63 61 73 65 28 20 69 3d 3d 30 20 29 3b 0a 20 20 case( i==0 );.
2093f 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31 testcase( i==1
20940 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 );. testcase
20941 28 20 69 3d 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b ( i==db->aLimit[
20942 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41 52 SQLITE_LIMIT_VAR
20943 49 41 42 4c 45 5f 4e 55 4d 42 45 52 5d 2d 31 20 IABLE_NUMBER]-1
20944 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 );. testcase(
20945 20 69 3d 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 i==db->aLimit[S
20946 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41 52 49 QLITE_LIMIT_VARI
20947 41 42 4c 45 5f 4e 55 4d 42 45 52 5d 20 29 3b 0a ABLE_NUMBER] );.
20948 20 20 20 20 69 66 28 20 69 3c 31 20 7c 7c 20 69 if( i<1 || i
20949 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 >db->aLimit[SQLI
2094a 54 45 5f 4c 49 4d 49 54 5f 56 41 52 49 41 42 4c TE_LIMIT_VARIABL
2094b 45 5f 4e 55 4d 42 45 52 5d 20 29 7b 0a 20 20 20 E_NUMBER] ){.
2094c 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d sqlite3ErrorM
2094d 73 67 28 70 50 61 72 73 65 2c 20 22 76 61 72 69 sg(pParse, "vari
2094e 61 62 6c 65 20 6e 75 6d 62 65 72 20 6d 75 73 74 able number must
2094f 20 62 65 20 62 65 74 77 65 65 6e 20 3f 31 20 61 be between ?1 a
20950 6e 64 20 3f 25 64 22 2c 0a 20 20 20 20 20 20 20 nd ?%d",.
20951 20 20 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 db->aLimit[SQ
20952 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41 52 49 41 LITE_LIMIT_VARIA
20953 42 4c 45 5f 4e 55 4d 42 45 52 5d 29 3b 0a 20 20 BLE_NUMBER]);.
20954 20 20 7d 0a 20 20 20 20 69 66 28 20 69 3e 70 50 }. if( i>pP
20955 61 72 73 65 2d 3e 6e 56 61 72 20 29 7b 0a 20 20 arse->nVar ){.
20956 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 pParse->nVar
20957 20 3d 20 69 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 = i;. }. }e
20958 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 57 69 6c 64 lse{. /* Wild
20959 63 61 72 64 73 20 6c 69 6b 65 20 22 3a 61 61 61 cards like ":aaa
2095a 22 2c 20 22 24 61 61 61 22 20 6f 72 20 22 40 61 ", "$aaa" or "@a
2095b 61 61 22 2e 20 20 52 65 75 73 65 20 74 68 65 20 aa". Reuse the
2095c 73 61 6d 65 20 76 61 72 69 61 62 6c 65 0a 20 20 same variable.
2095d 20 20 2a 2a 20 6e 75 6d 62 65 72 20 61 73 20 74 ** number as t
2095e 68 65 20 70 72 69 6f 72 20 61 70 70 65 61 72 61 he prior appeara
2095f 6e 63 65 20 6f 66 20 74 68 65 20 73 61 6d 65 20 nce of the same
20960 6e 61 6d 65 2c 20 6f 72 20 69 66 20 74 68 65 20 name, or if the
20961 6e 61 6d 65 0a 20 20 20 20 2a 2a 20 68 61 73 20 name. ** has
20962 6e 65 76 65 72 20 61 70 70 65 61 72 65 64 20 62 never appeared b
20963 65 66 6f 72 65 2c 20 72 65 75 73 65 20 74 68 65 efore, reuse the
20964 20 73 61 6d 65 20 76 61 72 69 61 62 6c 65 20 6e same variable n
20965 75 6d 62 65 72 0a 20 20 20 20 2a 2f 0a 20 20 20 umber. */.
20966 20 69 6e 74 20 69 3b 0a 20 20 20 20 75 33 32 20 int i;. u32
20967 6e 3b 0a 20 20 20 20 6e 20 3d 20 73 71 6c 69 74 n;. n = sqlit
20968 65 33 53 74 72 6c 65 6e 33 30 28 7a 29 3b 0a 20 e3Strlen30(z);.
20969 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50 for(i=0; i<pP
2096a 61 72 73 65 2d 3e 6e 56 61 72 45 78 70 72 3b 20 arse->nVarExpr;
2096b 69 2b 2b 29 7b 0a 20 20 20 20 20 20 45 78 70 72 i++){. Expr
2096c 20 2a 70 45 20 3d 20 70 50 61 72 73 65 2d 3e 61 *pE = pParse->a
2096d 70 56 61 72 45 78 70 72 5b 69 5d 3b 0a 20 20 20 pVarExpr[i];.
2096e 20 20 20 61 73 73 65 72 74 28 20 70 45 21 3d 30 assert( pE!=0
2096f 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6d 65 );. if( me
20970 6d 63 6d 70 28 70 45 2d 3e 75 2e 7a 54 6f 6b 65 mcmp(pE->u.zToke
20971 6e 2c 20 7a 2c 20 6e 29 3d 3d 30 20 26 26 20 70 n, z, n)==0 && p
20972 45 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 6e 5d 3d 3d E->u.zToken[n]==
20973 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 45 78 0 ){. pEx
20974 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 70 45 2d pr->iTable = pE-
20975 3e 69 54 61 62 6c 65 3b 0a 20 20 20 20 20 20 20 >iTable;.
20976 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a break;. }.
20977 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69 3e }. if( i>
20978 3d 70 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 70 =pParse->nVarExp
20979 72 20 29 7b 0a 20 20 20 20 20 20 70 45 78 70 72 r ){. pExpr
2097a 2d 3e 69 54 61 62 6c 65 20 3d 20 2b 2b 70 50 61 ->iTable = ++pPa
2097b 72 73 65 2d 3e 6e 56 61 72 3b 0a 20 20 20 20 20 rse->nVar;.
2097c 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 56 61 if( pParse->nVa
2097d 72 45 78 70 72 3e 3d 70 50 61 72 73 65 2d 3e 6e rExpr>=pParse->n
2097e 56 61 72 45 78 70 72 41 6c 6c 6f 63 2d 31 20 29 VarExprAlloc-1 )
2097f 7b 0a 20 20 20 20 20 20 20 20 70 50 61 72 73 65 {. pParse
20980 2d 3e 6e 56 61 72 45 78 70 72 41 6c 6c 6f 63 20 ->nVarExprAlloc
20981 2b 3d 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 45 += pParse->nVarE
20982 78 70 72 41 6c 6c 6f 63 20 2b 20 31 30 3b 0a 20 xprAlloc + 10;.
20983 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 61 pParse->a
20984 70 56 61 72 45 78 70 72 20 3d 0a 20 20 20 20 20 pVarExpr =.
20985 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 sqlite3Db
20986 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 0a 20 ReallocOrFree(.
20987 20 20 20 20 20 20 20 20 20 20 20 20 20 64 62 2c db,
20988 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 . p
20989 50 61 72 73 65 2d 3e 61 70 56 61 72 45 78 70 72 Parse->apVarExpr
2098a 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
2098b 70 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 70 72 pParse->nVarExpr
2098c 41 6c 6c 6f 63 2a 73 69 7a 65 6f 66 28 70 50 61 Alloc*sizeof(pPa
2098d 72 73 65 2d 3e 61 70 56 61 72 45 78 70 72 5b 30 rse->apVarExpr[0
2098e 5d 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 29 ]). )
2098f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
20990 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 if( !db->mallocF
20991 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 20 ailed ){.
20992 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65 2d assert( pParse-
20993 3e 61 70 56 61 72 45 78 70 72 21 3d 30 20 29 3b >apVarExpr!=0 );
20994 0a 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d . pParse-
20995 3e 61 70 56 61 72 45 78 70 72 5b 70 50 61 72 73 >apVarExpr[pPars
20996 65 2d 3e 6e 56 61 72 45 78 70 72 2b 2b 5d 20 3d e->nVarExpr++] =
20997 20 70 45 78 70 72 3b 0a 20 20 20 20 20 20 7d 0a pExpr;. }.
20998 20 20 20 20 7d 0a 20 20 7d 20 0a 20 20 69 66 28 }. } . if(
20999 20 21 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 26 !pParse->nErr &
2099a 26 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 3e 64 & pParse->nVar>d
2099b 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 b->aLimit[SQLITE
2099c 5f 4c 49 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f _LIMIT_VARIABLE_
2099d 4e 55 4d 42 45 52 5d 20 29 7b 0a 20 20 20 20 73 NUMBER] ){. s
2099e 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 qlite3ErrorMsg(p
2099f 50 61 72 73 65 2c 20 22 74 6f 6f 20 6d 61 6e 79 Parse, "too many
209a0 20 53 51 4c 20 76 61 72 69 61 62 6c 65 73 22 29 SQL variables")
209a1 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 ;. }.}../*.** C
209a2 6c 65 61 72 20 61 6e 20 65 78 70 72 65 73 73 69 lear an expressi
209a3 6f 6e 20 73 74 72 75 63 74 75 72 65 20 77 69 74 on structure wit
209a4 68 6f 75 74 20 64 65 6c 65 74 69 6e 67 20 74 68 hout deleting th
209a5 65 20 73 74 72 75 63 74 75 72 65 20 69 74 73 65 e structure itse
209a6 6c 66 2e 0a 2a 2a 20 53 75 62 73 74 72 75 63 74 lf..** Substruct
209a7 75 72 65 20 69 73 20 64 65 6c 65 74 65 64 2e 0a ure is deleted..
209a8 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
209a9 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 E void sqlite3Ex
209aa 70 72 43 6c 65 61 72 28 73 71 6c 69 74 65 33 20 prClear(sqlite3
209ab 2a 64 62 2c 20 45 78 70 72 20 2a 70 29 7b 0a 20 *db, Expr *p){.
209ac 20 61 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b assert( p!=0 );
209ad 0a 20 20 69 66 28 20 21 45 78 70 72 48 61 73 41 . if( !ExprHasA
209ae 6e 79 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50 nyProperty(p, EP
209af 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29 7b 0a 20 _TokenOnly) ){.
209b0 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 sqlite3ExprDe
209b1 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 4c 65 66 lete(db, p->pLef
209b2 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 t);. sqlite3E
209b3 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 2d xprDelete(db, p-
209b4 3e 70 52 69 67 68 74 29 3b 0a 20 20 20 20 69 66 >pRight);. if
209b5 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 ( !ExprHasProper
209b6 74 79 28 70 2c 20 45 50 5f 52 65 64 75 63 65 64 ty(p, EP_Reduced
209b7 29 20 26 26 20 28 70 2d 3e 66 6c 61 67 73 32 20 ) && (p->flags2
209b8 26 20 45 50 32 5f 4d 61 6c 6c 6f 63 65 64 54 6f & EP2_MallocedTo
209b9 6b 65 6e 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 ken)!=0 ){.
209ba 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
209bb 62 2c 20 70 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 3b b, p->u.zToken);
209bc 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 45 . }. if( E
209bd 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 xprHasProperty(p
209be 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 , EP_xIsSelect)
209bf 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
209c0 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c SelectDelete(db,
209c1 20 70 2d 3e 78 2e 70 53 65 6c 65 63 74 29 3b 0a p->x.pSelect);.
209c2 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
209c3 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 sqlite3ExprList
209c4 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 78 2e Delete(db, p->x.
209c5 70 4c 69 73 74 29 3b 0a 20 20 20 20 7d 0a 20 20 pList);. }.
209c6 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 75 72 }.}../*.** Recur
209c7 73 69 76 65 6c 79 20 64 65 6c 65 74 65 20 61 6e sively delete an
209c8 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 expression tree
209c9 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
209ca 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
209cb 45 78 70 72 44 65 6c 65 74 65 28 73 71 6c 69 74 ExprDelete(sqlit
209cc 65 33 20 2a 64 62 2c 20 45 78 70 72 20 2a 70 29 e3 *db, Expr *p)
209cd 7b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 {. if( p==0 ) r
209ce 65 74 75 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33 eturn;. sqlite3
209cf 45 78 70 72 43 6c 65 61 72 28 64 62 2c 20 70 29 ExprClear(db, p)
209d0 3b 0a 20 20 69 66 28 20 21 45 78 70 72 48 61 73 ;. if( !ExprHas
209d1 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 53 Property(p, EP_S
209d2 74 61 74 69 63 29 20 29 7b 0a 20 20 20 20 73 71 tatic) ){. sq
209d3 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 lite3DbFree(db,
209d4 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a p);. }.}../*.**
209d5 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 Return the numb
209d6 65 72 20 6f 66 20 62 79 74 65 73 20 61 6c 6c 6f er of bytes allo
209d7 63 61 74 65 64 20 66 6f 72 20 74 68 65 20 65 78 cated for the ex
209d8 70 72 65 73 73 69 6f 6e 20 73 74 72 75 63 74 75 pression structu
209d9 72 65 20 0a 2a 2a 20 70 61 73 73 65 64 20 61 73 re .** passed as
209da 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d the first argum
209db 65 6e 74 2e 20 54 68 69 73 20 69 73 20 61 6c 77 ent. This is alw
209dc 61 79 73 20 6f 6e 65 20 6f 66 20 45 58 50 52 5f ays one of EXPR_
209dd 46 55 4c 4c 53 49 5a 45 2c 0a 2a 2a 20 45 58 50 FULLSIZE,.** EXP
209de 52 5f 52 45 44 55 43 45 44 53 49 5a 45 20 6f 72 R_REDUCEDSIZE or
209df 20 45 58 50 52 5f 54 4f 4b 45 4e 4f 4e 4c 59 53 EXPR_TOKENONLYS
209e0 49 5a 45 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 IZE..*/.static i
209e1 6e 74 20 65 78 70 72 53 74 72 75 63 74 53 69 7a nt exprStructSiz
209e2 65 28 45 78 70 72 20 2a 70 29 7b 0a 20 20 69 66 e(Expr *p){. if
209e3 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 ( ExprHasPropert
209e4 79 28 70 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c y(p, EP_TokenOnl
209e5 79 29 20 29 20 72 65 74 75 72 6e 20 45 58 50 52 y) ) return EXPR
209e6 5f 54 4f 4b 45 4e 4f 4e 4c 59 53 49 5a 45 3b 0a _TOKENONLYSIZE;.
209e7 20 20 69 66 28 20 45 78 70 72 48 61 73 50 72 6f if( ExprHasPro
209e8 70 65 72 74 79 28 70 2c 20 45 50 5f 52 65 64 75 perty(p, EP_Redu
209e9 63 65 64 29 20 29 20 72 65 74 75 72 6e 20 45 58 ced) ) return EX
209ea 50 52 5f 52 45 44 55 43 45 44 53 49 5a 45 3b 0a PR_REDUCEDSIZE;.
209eb 20 20 72 65 74 75 72 6e 20 45 58 50 52 5f 46 55 return EXPR_FU
209ec 4c 4c 53 49 5a 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a LLSIZE;.}../*.**
209ed 20 54 68 65 20 64 75 70 65 64 45 78 70 72 2a 53 The dupedExpr*S
209ee 69 7a 65 28 29 20 72 6f 75 74 69 6e 65 73 20 65 ize() routines e
209ef 61 63 68 20 72 65 74 75 72 6e 20 74 68 65 20 6e ach return the n
209f0 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 72 umber of bytes r
209f1 65 71 75 69 72 65 64 0a 2a 2a 20 74 6f 20 73 74 equired.** to st
209f2 6f 72 65 20 61 20 63 6f 70 79 20 6f 66 20 61 6e ore a copy of an
209f3 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 20 65 expression or e
209f4 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2e 20 xpression tree.
209f5 20 54 68 65 79 20 64 69 66 66 65 72 20 69 6e 0a They differ in.
209f6 2a 2a 20 68 6f 77 20 6d 75 63 68 20 6f 66 20 74 ** how much of t
209f7 68 65 20 74 72 65 65 20 69 73 20 6d 65 61 73 75 he tree is measu
209f8 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 64 red..**.** d
209f9 75 70 65 64 45 78 70 72 53 74 72 75 63 74 53 69 upedExprStructSi
209fa 7a 65 28 29 20 20 20 20 20 53 69 7a 65 20 6f 66 ze() Size of
209fb 20 6f 6e 6c 79 20 74 68 65 20 45 78 70 72 20 73 only the Expr s
209fc 74 72 75 63 74 75 72 65 20 0a 2a 2a 20 20 20 20 tructure .**
209fd 20 64 75 70 65 64 45 78 70 72 4e 6f 64 65 53 69 dupedExprNodeSi
209fe 7a 65 28 29 20 20 20 20 20 20 20 53 69 7a 65 20 ze() Size
209ff 6f 66 20 45 78 70 72 20 2b 20 73 70 61 63 65 20 of Expr + space
20a00 66 6f 72 20 74 6f 6b 65 6e 0a 2a 2a 20 20 20 20 for token.**
20a01 20 64 75 70 65 64 45 78 70 72 53 69 7a 65 28 29 dupedExprSize()
20a02 20 20 20 20 20 20 20 20 20 20 20 45 78 70 72 20 Expr
20a03 2b 20 74 6f 6b 65 6e 20 2b 20 73 75 62 74 72 65 + token + subtre
20a04 65 20 63 6f 6d 70 6f 6e 65 6e 74 73 0a 2a 2a 0a e components.**.
20a05 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20a06 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20a07 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20a08 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20a09 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a ***********.**.*
20a0a 2a 20 54 68 65 20 64 75 70 65 64 45 78 70 72 53 * The dupedExprS
20a0b 74 72 75 63 74 53 69 7a 65 28 29 20 66 75 6e 63 tructSize() func
20a0c 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 77 6f tion returns two
20a0d 20 76 61 6c 75 65 73 20 4f 52 2d 65 64 20 74 6f values OR-ed to
20a0e 67 65 74 68 65 72 3a 20 20 0a 2a 2a 20 28 31 29 gether: .** (1)
20a0f 20 74 68 65 20 73 70 61 63 65 20 72 65 71 75 69 the space requi
20a10 72 65 64 20 66 6f 72 20 61 20 63 6f 70 79 20 6f red for a copy o
20a11 66 20 74 68 65 20 45 78 70 72 20 73 74 72 75 63 f the Expr struc
20a12 74 75 72 65 20 6f 6e 6c 79 20 61 6e 64 20 0a 2a ture only and .*
20a13 2a 20 28 32 29 20 74 68 65 20 45 50 5f 78 78 78 * (2) the EP_xxx
20a14 20 66 6c 61 67 73 20 74 68 61 74 20 69 6e 64 69 flags that indi
20a15 63 61 74 65 20 77 68 61 74 20 74 68 65 20 73 74 cate what the st
20a16 72 75 63 74 75 72 65 20 73 69 7a 65 20 73 68 6f ructure size sho
20a17 75 6c 64 20 62 65 2e 0a 2a 2a 20 54 68 65 20 72 uld be..** The r
20a18 65 74 75 72 6e 20 76 61 6c 75 65 73 20 69 73 20 eturn values is
20a19 61 6c 77 61 79 73 20 6f 6e 65 20 6f 66 3a 0a 2a always one of:.*
20a1a 2a 0a 2a 2a 20 20 20 20 20 20 45 58 50 52 5f 46 *.** EXPR_F
20a1b 55 4c 4c 53 49 5a 45 0a 2a 2a 20 20 20 20 20 20 ULLSIZE.**
20a1c 45 58 50 52 5f 52 45 44 55 43 45 44 53 49 5a 45 EXPR_REDUCEDSIZE
20a1d 20 20 20 7c 20 45 50 5f 52 65 64 75 63 65 64 0a | EP_Reduced.
20a1e 2a 2a 20 20 20 20 20 20 45 58 50 52 5f 54 4f 4b ** EXPR_TOK
20a1f 45 4e 4f 4e 4c 59 53 49 5a 45 20 7c 20 45 50 5f ENONLYSIZE | EP_
20a20 54 6f 6b 65 6e 4f 6e 6c 79 0a 2a 2a 0a 2a 2a 20 TokenOnly.**.**
20a21 54 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 The size of the
20a22 73 74 72 75 63 74 75 72 65 20 63 61 6e 20 62 65 structure can be
20a23 20 66 6f 75 6e 64 20 62 79 20 6d 61 73 6b 69 6e found by maskin
20a24 67 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c g the return val
20a25 75 65 0a 2a 2a 20 6f 66 20 74 68 69 73 20 72 6f ue.** of this ro
20a26 75 74 69 6e 65 20 77 69 74 68 20 30 78 66 66 66 utine with 0xfff
20a27 2e 20 20 54 68 65 20 66 6c 61 67 73 20 63 61 6e . The flags can
20a28 20 62 65 20 66 6f 75 6e 64 20 62 79 20 6d 61 73 be found by mas
20a29 6b 69 6e 67 20 74 68 65 0a 2a 2a 20 72 65 74 75 king the.** retu
20a2a 72 6e 20 76 61 6c 75 65 20 77 69 74 68 20 45 50 rn value with EP
20a2b 5f 52 65 64 75 63 65 64 7c 45 50 5f 54 6f 6b 65 _Reduced|EP_Toke
20a2c 6e 4f 6e 6c 79 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 nOnly..**.** Not
20a2d 65 20 74 68 61 74 20 77 69 74 68 20 66 6c 61 67 e that with flag
20a2e 73 3d 3d 45 58 50 52 44 55 50 5f 52 45 44 55 43 s==EXPRDUP_REDUC
20a2f 45 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 73 E, this routines
20a30 20 77 6f 72 6b 73 20 6f 6e 20 66 75 6c 6c 2d 73 works on full-s
20a31 69 7a 65 0a 2a 2a 20 28 75 6e 72 65 64 75 63 65 ize.** (unreduce
20a32 64 29 20 45 78 70 72 20 6f 62 6a 65 63 74 73 20 d) Expr objects
20a33 61 73 20 74 68 65 79 20 6f 72 20 6f 72 69 67 69 as they or origi
20a34 6e 61 6c 6c 79 20 63 6f 6e 73 74 72 75 63 74 65 nally constructe
20a35 64 20 62 79 20 74 68 65 20 70 61 72 73 65 72 2e d by the parser.
20a36 0a 2a 2a 20 44 75 72 69 6e 67 20 65 78 70 72 65 .** During expre
20a37 73 73 69 6f 6e 20 61 6e 61 6c 79 73 69 73 2c 20 ssion analysis,
20a38 65 78 74 72 61 20 69 6e 66 6f 72 6d 61 74 69 6f extra informatio
20a39 6e 20 69 73 20 63 6f 6d 70 75 74 65 64 20 61 6e n is computed an
20a3a 64 20 6d 6f 76 65 64 20 69 6e 74 6f 0a 2a 2a 20 d moved into.**
20a3b 6c 61 74 65 72 20 70 61 72 74 73 20 6f 66 20 74 later parts of t
20a3c 65 68 20 45 78 70 72 20 6f 62 6a 65 63 74 20 61 eh Expr object a
20a3d 6e 64 20 74 68 61 74 20 65 78 74 72 61 20 69 6e nd that extra in
20a3e 66 6f 72 6d 61 74 69 6f 6e 20 6d 69 67 68 74 20 formation might
20a3f 67 65 74 20 63 68 6f 70 70 65 64 0a 2a 2a 20 6f get chopped.** o
20a40 66 66 20 69 66 20 74 68 65 20 65 78 70 72 65 73 ff if the expres
20a41 73 69 6f 6e 20 69 73 20 72 65 64 75 63 65 64 2e sion is reduced.
20a42 20 20 4e 6f 74 65 20 61 6c 73 6f 20 74 68 61 74 Note also that
20a43 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 77 6f 72 it does not wor
20a44 6b 20 74 6f 0a 2a 2a 20 6d 61 6b 65 20 61 20 45 k to.** make a E
20a45 58 50 52 44 55 50 5f 52 45 44 55 43 45 20 63 6f XPRDUP_REDUCE co
20a46 70 79 20 6f 66 20 61 20 72 65 64 75 63 65 64 20 py of a reduced
20a47 65 78 70 72 65 73 73 69 6f 6e 2e 20 20 49 74 20 expression. It
20a48 69 73 20 6f 6e 6c 79 20 6c 65 67 61 6c 0a 2a 2a is only legal.**
20a49 20 74 6f 20 72 65 64 75 63 65 20 61 20 70 72 69 to reduce a pri
20a4a 73 74 69 6e 65 20 65 78 70 72 65 73 73 69 6f 6e stine expression
20a4b 20 74 72 65 65 20 66 72 6f 6d 20 74 68 65 20 70 tree from the p
20a4c 61 72 73 65 72 2e 20 20 54 68 65 20 69 6d 70 6c arser. The impl
20a4d 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 6f 66 ementation.** of
20a4e 20 64 75 70 65 64 45 78 70 72 53 74 72 75 63 74 dupedExprStruct
20a4f 53 69 7a 65 28 29 20 63 6f 6e 74 61 69 6e 20 6d Size() contain m
20a50 75 6c 74 69 70 6c 65 20 61 73 73 65 72 74 28 29 ultiple assert()
20a51 20 73 74 61 74 65 6d 65 6e 74 73 20 74 68 61 74 statements that
20a52 20 61 74 74 65 6d 70 74 0a 2a 2a 20 74 6f 20 65 attempt.** to e
20a53 6e 66 6f 72 63 65 20 74 68 69 73 20 63 6f 6e 73 nforce this cons
20a54 74 72 61 69 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 traint..*/.stati
20a55 63 20 69 6e 74 20 64 75 70 65 64 45 78 70 72 53 c int dupedExprS
20a56 74 72 75 63 74 53 69 7a 65 28 45 78 70 72 20 2a tructSize(Expr *
20a57 70 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 p, int flags){.
20a58 20 69 6e 74 20 6e 53 69 7a 65 3b 0a 20 20 61 73 int nSize;. as
20a59 73 65 72 74 28 20 66 6c 61 67 73 3d 3d 45 58 50 sert( flags==EXP
20a5a 52 44 55 50 5f 52 45 44 55 43 45 20 7c 7c 20 66 RDUP_REDUCE || f
20a5b 6c 61 67 73 3d 3d 30 20 29 3b 20 2f 2a 20 4f 6e lags==0 ); /* On
20a5c 6c 79 20 6f 6e 65 20 66 6c 61 67 20 76 61 6c 75 ly one flag valu
20a5d 65 20 61 6c 6c 6f 77 65 64 20 2a 2f 0a 20 20 69 e allowed */. i
20a5e 66 28 20 30 3d 3d 28 66 6c 61 67 73 26 45 58 50 f( 0==(flags&EXP
20a5f 52 44 55 50 5f 52 45 44 55 43 45 29 20 29 7b 0a RDUP_REDUCE) ){.
20a60 20 20 20 20 6e 53 69 7a 65 20 3d 20 45 58 50 52 nSize = EXPR
20a61 5f 46 55 4c 4c 53 49 5a 45 3b 0a 20 20 7d 65 6c _FULLSIZE;. }el
20a62 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 se{. assert(
20a63 21 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 !ExprHasAnyPrope
20a64 72 74 79 28 70 2c 20 45 50 5f 54 6f 6b 65 6e 4f rty(p, EP_TokenO
20a65 6e 6c 79 7c 45 50 5f 52 65 64 75 63 65 64 29 20 nly|EP_Reduced)
20a66 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 );. assert( !
20a67 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 ExprHasProperty(
20a68 70 2c 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e 29 20 p, EP_FromJoin)
20a69 29 3b 20 0a 20 20 20 20 61 73 73 65 72 74 28 20 ); . assert(
20a6a 28 70 2d 3e 66 6c 61 67 73 32 20 26 20 45 50 32 (p->flags2 & EP2
20a6b 5f 4d 61 6c 6c 6f 63 65 64 54 6f 6b 65 6e 29 3d _MallocedToken)=
20a6c 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 =0 );. assert
20a6d 28 20 28 70 2d 3e 66 6c 61 67 73 32 20 26 20 45 ( (p->flags2 & E
20a6e 50 32 5f 49 72 72 65 64 75 63 69 62 6c 65 29 3d P2_Irreducible)=
20a6f 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 70 2d =0 );. if( p-
20a70 3e 70 4c 65 66 74 20 7c 7c 20 70 2d 3e 70 52 69 >pLeft || p->pRi
20a71 67 68 74 20 7c 7c 20 70 2d 3e 70 43 6f 6c 6c 20 ght || p->pColl
20a72 7c 7c 20 70 2d 3e 78 2e 70 4c 69 73 74 20 29 7b || p->x.pList ){
20a73 0a 20 20 20 20 20 20 6e 53 69 7a 65 20 3d 20 45 . nSize = E
20a74 58 50 52 5f 52 45 44 55 43 45 44 53 49 5a 45 20 XPR_REDUCEDSIZE
20a75 7c 20 45 50 5f 52 65 64 75 63 65 64 3b 0a 20 20 | EP_Reduced;.
20a76 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e }else{. n
20a77 53 69 7a 65 20 3d 20 45 58 50 52 5f 54 4f 4b 45 Size = EXPR_TOKE
20a78 4e 4f 4e 4c 59 53 49 5a 45 20 7c 20 45 50 5f 54 NONLYSIZE | EP_T
20a79 6f 6b 65 6e 4f 6e 6c 79 3b 0a 20 20 20 20 7d 0a okenOnly;. }.
20a7a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e 53 69 }. return nSi
20a7b 7a 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 ze;.}../*.** Thi
20a7c 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 s function retur
20a7d 6e 73 20 74 68 65 20 73 70 61 63 65 20 69 6e 20 ns the space in
20a7e 62 79 74 65 73 20 72 65 71 75 69 72 65 64 20 74 bytes required t
20a7f 6f 20 73 74 6f 72 65 20 74 68 65 20 63 6f 70 79 o store the copy
20a80 20 0a 2a 2a 20 6f 66 20 74 68 65 20 45 78 70 72 .** of the Expr
20a81 20 73 74 72 75 63 74 75 72 65 20 61 6e 64 20 61 structure and a
20a82 20 63 6f 70 79 20 6f 66 20 74 68 65 20 45 78 70 copy of the Exp
20a83 72 2e 75 2e 7a 54 6f 6b 65 6e 20 73 74 72 69 6e r.u.zToken strin
20a84 67 20 28 69 66 20 74 68 61 74 0a 2a 2a 20 73 74 g (if that.** st
20a85 72 69 6e 67 20 69 73 20 64 65 66 69 6e 65 64 2e ring is defined.
20a86 29 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ).*/.static int
20a87 64 75 70 65 64 45 78 70 72 4e 6f 64 65 53 69 7a dupedExprNodeSiz
20a88 65 28 45 78 70 72 20 2a 70 2c 20 69 6e 74 20 66 e(Expr *p, int f
20a89 6c 61 67 73 29 7b 0a 20 20 69 6e 74 20 6e 42 79 lags){. int nBy
20a8a 74 65 20 3d 20 64 75 70 65 64 45 78 70 72 53 74 te = dupedExprSt
20a8b 72 75 63 74 53 69 7a 65 28 70 2c 20 66 6c 61 67 ructSize(p, flag
20a8c 73 29 20 26 20 30 78 66 66 66 3b 0a 20 20 69 66 s) & 0xfff;. if
20a8d 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 ( !ExprHasProper
20a8e 74 79 28 70 2c 20 45 50 5f 49 6e 74 56 61 6c 75 ty(p, EP_IntValu
20a8f 65 29 20 26 26 20 70 2d 3e 75 2e 7a 54 6f 6b 65 e) && p->u.zToke
20a90 6e 20 29 7b 0a 20 20 20 20 6e 42 79 74 65 20 2b n ){. nByte +
20a91 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 = sqlite3Strlen3
20a92 30 28 70 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 2b 31 0(p->u.zToken)+1
20a93 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 52 ;. }. return R
20a94 4f 55 4e 44 38 28 6e 42 79 74 65 29 3b 0a 7d 0a OUND8(nByte);.}.
20a95 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ./*.** Return th
20a96 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 e number of byte
20a97 73 20 72 65 71 75 69 72 65 64 20 74 6f 20 63 72 s required to cr
20a98 65 61 74 65 20 61 20 64 75 70 6c 69 63 61 74 65 eate a duplicate
20a99 20 6f 66 20 74 68 65 20 0a 2a 2a 20 65 78 70 72 of the .** expr
20a9a 65 73 73 69 6f 6e 20 70 61 73 73 65 64 20 61 73 ession passed as
20a9b 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d the first argum
20a9c 65 6e 74 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 ent. The second
20a9d 61 72 67 75 6d 65 6e 74 20 69 73 20 61 0a 2a 2a argument is a.**
20a9e 20 6d 61 73 6b 20 63 6f 6e 74 61 69 6e 69 6e 67 mask containing
20a9f 20 45 58 50 52 44 55 50 5f 58 58 58 20 66 6c 61 EXPRDUP_XXX fla
20aa0 67 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 76 61 gs..**.** The va
20aa1 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 6e 63 lue returned inc
20aa2 6c 75 64 65 73 20 73 70 61 63 65 20 74 6f 20 63 ludes space to c
20aa3 72 65 61 74 65 20 61 20 63 6f 70 79 20 6f 66 20 reate a copy of
20aa4 74 68 65 20 45 78 70 72 20 73 74 72 75 63 74 0a the Expr struct.
20aa5 2a 2a 20 69 74 73 65 6c 66 20 61 6e 64 20 74 68 ** itself and th
20aa6 65 20 62 75 66 66 65 72 20 72 65 66 65 72 72 65 e buffer referre
20aa7 64 20 74 6f 20 62 79 20 45 78 70 72 2e 75 2e 7a d to by Expr.u.z
20aa8 54 6f 6b 65 6e 2c 20 69 66 20 61 6e 79 2e 0a 2a Token, if any..*
20aa9 2a 0a 2a 2a 20 49 66 20 74 68 65 20 45 58 50 52 *.** If the EXPR
20aaa 44 55 50 5f 52 45 44 55 43 45 20 66 6c 61 67 20 DUP_REDUCE flag
20aab 69 73 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65 is set, then the
20aac 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 6e return value in
20aad 63 6c 75 64 65 73 20 0a 2a 2a 20 73 70 61 63 65 cludes .** space
20aae 20 74 6f 20 64 75 70 6c 69 63 61 74 65 20 61 6c to duplicate al
20aaf 6c 20 45 78 70 72 20 6e 6f 64 65 73 20 69 6e 20 l Expr nodes in
20ab0 74 68 65 20 74 72 65 65 20 66 6f 72 6d 65 64 20 the tree formed
20ab1 62 79 20 45 78 70 72 2e 70 4c 65 66 74 20 0a 2a by Expr.pLeft .*
20ab2 2a 20 61 6e 64 20 45 78 70 72 2e 70 52 69 67 68 * and Expr.pRigh
20ab3 74 20 76 61 72 69 61 62 6c 65 73 20 28 62 75 74 t variables (but
20ab4 20 6e 6f 74 20 66 6f 72 20 61 6e 79 20 73 74 72 not for any str
20ab5 75 63 74 75 72 65 73 20 70 6f 69 6e 74 65 64 20 uctures pointed
20ab6 74 6f 20 6f 72 20 0a 2a 2a 20 64 65 73 63 65 6e to or .** descen
20ab7 64 65 64 20 66 72 6f 6d 20 74 68 65 20 45 78 70 ded from the Exp
20ab8 72 2e 78 2e 70 4c 69 73 74 20 6f 72 20 45 78 70 r.x.pList or Exp
20ab9 72 2e 78 2e 70 53 65 6c 65 63 74 20 76 61 72 69 r.x.pSelect vari
20aba 61 62 6c 65 73 29 2e 0a 2a 2f 0a 73 74 61 74 69 ables)..*/.stati
20abb 63 20 69 6e 74 20 64 75 70 65 64 45 78 70 72 53 c int dupedExprS
20abc 69 7a 65 28 45 78 70 72 20 2a 70 2c 20 69 6e 74 ize(Expr *p, int
20abd 20 66 6c 61 67 73 29 7b 0a 20 20 69 6e 74 20 6e flags){. int n
20abe 42 79 74 65 20 3d 20 30 3b 0a 20 20 69 66 28 20 Byte = 0;. if(
20abf 70 20 29 7b 0a 20 20 20 20 6e 42 79 74 65 20 3d p ){. nByte =
20ac0 20 64 75 70 65 64 45 78 70 72 4e 6f 64 65 53 69 dupedExprNodeSi
20ac1 7a 65 28 70 2c 20 66 6c 61 67 73 29 3b 0a 20 20 ze(p, flags);.
20ac2 20 20 69 66 28 20 66 6c 61 67 73 26 45 58 50 52 if( flags&EXPR
20ac3 44 55 50 5f 52 45 44 55 43 45 20 29 7b 0a 20 20 DUP_REDUCE ){.
20ac4 20 20 20 20 6e 42 79 74 65 20 2b 3d 20 64 75 70 nByte += dup
20ac5 65 64 45 78 70 72 53 69 7a 65 28 70 2d 3e 70 4c edExprSize(p->pL
20ac6 65 66 74 2c 20 66 6c 61 67 73 29 20 2b 20 64 75 eft, flags) + du
20ac7 70 65 64 45 78 70 72 53 69 7a 65 28 70 2d 3e 70 pedExprSize(p->p
20ac8 52 69 67 68 74 2c 20 66 6c 61 67 73 29 3b 0a 20 Right, flags);.
20ac9 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
20aca 6e 20 6e 42 79 74 65 3b 0a 7d 0a 0a 2f 2a 0a 2a n nByte;.}../*.*
20acb 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
20acc 69 73 20 73 69 6d 69 6c 61 72 20 74 6f 20 73 71 is similar to sq
20acd 6c 69 74 65 33 45 78 70 72 44 75 70 28 29 2c 20 lite3ExprDup(),
20ace 65 78 63 65 70 74 20 74 68 61 74 20 69 66 20 70 except that if p
20acf 7a 42 75 66 66 65 72 20 0a 2a 2a 20 69 73 20 6e zBuffer .** is n
20ad0 6f 74 20 4e 55 4c 4c 20 74 68 65 6e 20 2a 70 7a ot NULL then *pz
20ad1 42 75 66 66 65 72 20 69 73 20 61 73 73 75 6d 65 Buffer is assume
20ad2 64 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 61 20 d to point to a
20ad3 62 75 66 66 65 72 20 6c 61 72 67 65 20 65 6e 6f buffer large eno
20ad4 75 67 68 20 0a 2a 2a 20 74 6f 20 73 74 6f 72 65 ugh .** to store
20ad5 20 74 68 65 20 63 6f 70 79 20 6f 66 20 65 78 70 the copy of exp
20ad6 72 65 73 73 69 6f 6e 20 70 2c 20 74 68 65 20 63 ression p, the c
20ad7 6f 70 69 65 73 20 6f 66 20 70 2d 3e 75 2e 7a 54 opies of p->u.zT
20ad8 6f 6b 65 6e 0a 2a 2a 20 28 69 66 20 61 70 70 6c oken.** (if appl
20ad9 69 63 61 62 6c 65 29 2c 20 61 6e 64 20 74 68 65 icable), and the
20ada 20 63 6f 70 69 65 73 20 6f 66 20 74 68 65 20 70 copies of the p
20adb 2d 3e 70 4c 65 66 74 20 61 6e 64 20 70 2d 3e 70 ->pLeft and p->p
20adc 52 69 67 68 74 20 65 78 70 72 65 73 73 69 6f 6e Right expression
20add 73 2c 0a 2a 2a 20 69 66 20 61 6e 79 2e 20 42 65 s,.** if any. Be
20ade 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2c 20 fore returning,
20adf 2a 70 7a 42 75 66 66 65 72 20 69 73 20 73 65 74 *pzBuffer is set
20ae0 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 62 79 to the first by
20ae1 74 65 20 70 61 73 73 65 64 20 74 68 65 0a 2a 2a te passed the.**
20ae2 20 70 6f 72 74 69 6f 6e 20 6f 66 20 74 68 65 20 portion of the
20ae3 62 75 66 66 65 72 20 63 6f 70 69 65 64 20 69 6e buffer copied in
20ae4 74 6f 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 to by this funct
20ae5 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 45 ion..*/.static E
20ae6 78 70 72 20 2a 65 78 70 72 44 75 70 28 73 71 6c xpr *exprDup(sql
20ae7 69 74 65 33 20 2a 64 62 2c 20 45 78 70 72 20 2a ite3 *db, Expr *
20ae8 70 2c 20 69 6e 74 20 66 6c 61 67 73 2c 20 75 38 p, int flags, u8
20ae9 20 2a 2a 70 7a 42 75 66 66 65 72 29 7b 0a 20 20 **pzBuffer){.
20aea 45 78 70 72 20 2a 70 4e 65 77 20 3d 20 30 3b 20 Expr *pNew = 0;
20aeb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20aec 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f /* Value to
20aed 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20 69 66 28 return */. if(
20aee 20 70 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 p ){. const
20aef 69 6e 74 20 69 73 52 65 64 75 63 65 64 20 3d 20 int isReduced =
20af0 28 66 6c 61 67 73 26 45 58 50 52 44 55 50 5f 52 (flags&EXPRDUP_R
20af1 45 44 55 43 45 29 3b 0a 20 20 20 20 75 38 20 2a EDUCE);. u8 *
20af2 7a 41 6c 6c 6f 63 3b 0a 20 20 20 20 75 33 32 20 zAlloc;. u32
20af3 73 74 61 74 69 63 46 6c 61 67 20 3d 20 30 3b 0a staticFlag = 0;.
20af4 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 7a 42 . assert( pzB
20af5 75 66 66 65 72 3d 3d 30 20 7c 7c 20 69 73 52 65 uffer==0 || isRe
20af6 64 75 63 65 64 20 29 3b 0a 0a 20 20 20 20 2f 2a duced );.. /*
20af7 20 46 69 67 75 72 65 20 6f 75 74 20 77 68 65 72 Figure out wher
20af8 65 20 74 6f 20 77 72 69 74 65 20 74 68 65 20 6e e to write the n
20af9 65 77 20 45 78 70 72 20 73 74 72 75 63 74 75 72 ew Expr structur
20afa 65 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 7a e. */. if( pz
20afb 42 75 66 66 65 72 20 29 7b 0a 20 20 20 20 20 20 Buffer ){.
20afc 7a 41 6c 6c 6f 63 20 3d 20 2a 70 7a 42 75 66 66 zAlloc = *pzBuff
20afd 65 72 3b 0a 20 20 20 20 20 20 73 74 61 74 69 63 er;. static
20afe 46 6c 61 67 20 3d 20 45 50 5f 53 74 61 74 69 63 Flag = EP_Static
20aff 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
20b00 20 20 20 7a 41 6c 6c 6f 63 20 3d 20 73 71 6c 69 zAlloc = sqli
20b01 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 te3DbMallocRaw(d
20b02 62 2c 20 64 75 70 65 64 45 78 70 72 53 69 7a 65 b, dupedExprSize
20b03 28 70 2c 20 66 6c 61 67 73 29 29 3b 0a 20 20 20 (p, flags));.
20b04 20 7d 0a 20 20 20 20 70 4e 65 77 20 3d 20 28 45 }. pNew = (E
20b05 78 70 72 20 2a 29 7a 41 6c 6c 6f 63 3b 0a 0a 20 xpr *)zAlloc;..
20b06 20 20 20 69 66 28 20 70 4e 65 77 20 29 7b 0a 20 if( pNew ){.
20b07 20 20 20 20 20 2f 2a 20 53 65 74 20 6e 4e 65 77 /* Set nNew
20b08 53 69 7a 65 20 74 6f 20 74 68 65 20 73 69 7a 65 Size to the size
20b09 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 74 allocated for t
20b0a 68 65 20 73 74 72 75 63 74 75 72 65 20 70 6f 69 he structure poi
20b0b 6e 74 65 64 20 74 6f 0a 20 20 20 20 20 20 2a 2a nted to. **
20b0c 20 62 79 20 70 4e 65 77 2e 20 54 68 69 73 20 69 by pNew. This i
20b0d 73 20 65 69 74 68 65 72 20 45 58 50 52 5f 46 55 s either EXPR_FU
20b0e 4c 4c 53 49 5a 45 2c 20 45 58 50 52 5f 52 45 44 LLSIZE, EXPR_RED
20b0f 55 43 45 44 53 49 5a 45 20 6f 72 0a 20 20 20 20 UCEDSIZE or.
20b10 20 20 2a 2a 20 45 58 50 52 5f 54 4f 4b 45 4e 4f ** EXPR_TOKENO
20b11 4e 4c 59 53 49 5a 45 2e 20 6e 54 6f 6b 65 6e 20 NLYSIZE. nToken
20b12 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 6e 75 is set to the nu
20b13 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 63 6f mber of bytes co
20b14 6e 73 75 6d 65 64 0a 20 20 20 20 20 20 2a 2a 20 nsumed. **
20b15 62 79 20 74 68 65 20 63 6f 70 79 20 6f 66 20 74 by the copy of t
20b16 68 65 20 70 2d 3e 75 2e 7a 54 6f 6b 65 6e 20 73 he p->u.zToken s
20b17 74 72 69 6e 67 20 28 69 66 20 61 6e 79 29 2e 0a tring (if any)..
20b18 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 63 */. c
20b19 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 6e 53 onst unsigned nS
20b1a 74 72 75 63 74 53 69 7a 65 20 3d 20 64 75 70 65 tructSize = dupe
20b1b 64 45 78 70 72 53 74 72 75 63 74 53 69 7a 65 28 dExprStructSize(
20b1c 70 2c 20 66 6c 61 67 73 29 3b 0a 20 20 20 20 20 p, flags);.
20b1d 20 63 6f 6e 73 74 20 69 6e 74 20 6e 4e 65 77 53 const int nNewS
20b1e 69 7a 65 20 3d 20 6e 53 74 72 75 63 74 53 69 7a ize = nStructSiz
20b1f 65 20 26 20 30 78 66 66 66 3b 0a 20 20 20 20 20 e & 0xfff;.
20b20 20 69 6e 74 20 6e 54 6f 6b 65 6e 3b 0a 20 20 20 int nToken;.
20b21 20 20 20 69 66 28 20 21 45 78 70 72 48 61 73 50 if( !ExprHasP
20b22 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 49 6e roperty(p, EP_In
20b23 74 56 61 6c 75 65 29 20 26 26 20 70 2d 3e 75 2e tValue) && p->u.
20b24 7a 54 6f 6b 65 6e 20 29 7b 0a 20 20 20 20 20 20 zToken ){.
20b25 20 20 6e 54 6f 6b 65 6e 20 3d 20 73 71 6c 69 74 nToken = sqlit
20b26 65 33 53 74 72 6c 65 6e 33 30 28 70 2d 3e 75 2e e3Strlen30(p->u.
20b27 7a 54 6f 6b 65 6e 29 20 2b 20 31 3b 0a 20 20 20 zToken) + 1;.
20b28 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
20b29 20 20 6e 54 6f 6b 65 6e 20 3d 20 30 3b 0a 20 20 nToken = 0;.
20b2a 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 }. if(
20b2b 69 73 52 65 64 75 63 65 64 20 29 7b 0a 20 20 20 isReduced ){.
20b2c 20 20 20 20 20 61 73 73 65 72 74 28 20 45 78 70 assert( Exp
20b2d 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 2c 20 rHasProperty(p,
20b2e 45 50 5f 52 65 64 75 63 65 64 29 3d 3d 30 20 29 EP_Reduced)==0 )
20b2f 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 ;. memcpy
20b30 28 7a 41 6c 6c 6f 63 2c 20 70 2c 20 6e 4e 65 77 (zAlloc, p, nNew
20b31 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 7d 65 6c Size);. }el
20b32 73 65 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 se{. int
20b33 6e 53 69 7a 65 20 3d 20 65 78 70 72 53 74 72 75 nSize = exprStru
20b34 63 74 53 69 7a 65 28 70 29 3b 0a 20 20 20 20 20 ctSize(p);.
20b35 20 20 20 6d 65 6d 63 70 79 28 7a 41 6c 6c 6f 63 memcpy(zAlloc
20b36 2c 20 70 2c 20 6e 53 69 7a 65 29 3b 0a 20 20 20 , p, nSize);.
20b37 20 20 20 20 20 6d 65 6d 73 65 74 28 26 7a 41 6c memset(&zAl
20b38 6c 6f 63 5b 6e 53 69 7a 65 5d 2c 20 30 2c 20 45 loc[nSize], 0, E
20b39 58 50 52 5f 46 55 4c 4c 53 49 5a 45 2d 6e 53 69 XPR_FULLSIZE-nSi
20b3a 7a 65 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 ze);. }..
20b3b 20 20 20 20 2f 2a 20 53 65 74 20 74 68 65 20 45 /* Set the E
20b3c 50 5f 52 65 64 75 63 65 64 2c 20 45 50 5f 54 6f P_Reduced, EP_To
20b3d 6b 65 6e 4f 6e 6c 79 2c 20 61 6e 64 20 45 50 5f kenOnly, and EP_
20b3e 53 74 61 74 69 63 20 66 6c 61 67 73 20 61 70 70 Static flags app
20b3f 72 6f 70 72 69 61 74 65 6c 79 2e 20 2a 2f 0a 20 ropriately. */.
20b40 20 20 20 20 20 70 4e 65 77 2d 3e 66 6c 61 67 73 pNew->flags
20b41 20 26 3d 20 7e 28 45 50 5f 52 65 64 75 63 65 64 &= ~(EP_Reduced
20b42 7c 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c 45 50 |EP_TokenOnly|EP
20b43 5f 53 74 61 74 69 63 29 3b 0a 20 20 20 20 20 20 _Static);.
20b44 70 4e 65 77 2d 3e 66 6c 61 67 73 20 7c 3d 20 6e pNew->flags |= n
20b45 53 74 72 75 63 74 53 69 7a 65 20 26 20 28 45 50 StructSize & (EP
20b46 5f 52 65 64 75 63 65 64 7c 45 50 5f 54 6f 6b 65 _Reduced|EP_Toke
20b47 6e 4f 6e 6c 79 29 3b 0a 20 20 20 20 20 20 70 4e nOnly);. pN
20b48 65 77 2d 3e 66 6c 61 67 73 20 7c 3d 20 73 74 61 ew->flags |= sta
20b49 74 69 63 46 6c 61 67 3b 0a 0a 20 20 20 20 20 20 ticFlag;..
20b4a 2f 2a 20 43 6f 70 79 20 74 68 65 20 70 2d 3e 75 /* Copy the p->u
20b4b 2e 7a 54 6f 6b 65 6e 20 73 74 72 69 6e 67 2c 20 .zToken string,
20b4c 69 66 20 61 6e 79 2e 20 2a 2f 0a 20 20 20 20 20 if any. */.
20b4d 20 69 66 28 20 6e 54 6f 6b 65 6e 20 29 7b 0a 20 if( nToken ){.
20b4e 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a 54 6f char *zTo
20b4f 6b 65 6e 20 3d 20 70 4e 65 77 2d 3e 75 2e 7a 54 ken = pNew->u.zT
20b50 6f 6b 65 6e 20 3d 20 28 63 68 61 72 2a 29 26 7a oken = (char*)&z
20b51 41 6c 6c 6f 63 5b 6e 4e 65 77 53 69 7a 65 5d 3b Alloc[nNewSize];
20b52 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 . memcpy(
20b53 7a 54 6f 6b 65 6e 2c 20 70 2d 3e 75 2e 7a 54 6f zToken, p->u.zTo
20b54 6b 65 6e 2c 20 6e 54 6f 6b 65 6e 29 3b 0a 20 20 ken, nToken);.
20b55 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 69 66 28 }.. if(
20b56 20 30 3d 3d 28 28 70 2d 3e 66 6c 61 67 73 7c 70 0==((p->flags|p
20b57 4e 65 77 2d 3e 66 6c 61 67 73 29 20 26 20 45 50 New->flags) & EP
20b58 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29 7b 0a 20 _TokenOnly) ){.
20b59 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 6c 20 69 /* Fill i
20b5a 6e 20 74 68 65 20 70 4e 65 77 2d 3e 78 2e 70 53 n the pNew->x.pS
20b5b 65 6c 65 63 74 20 6f 72 20 70 4e 65 77 2d 3e 78 elect or pNew->x
20b5c 2e 70 4c 69 73 74 20 6d 65 6d 62 65 72 2e 20 2a .pList member. *
20b5d 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 45 78 /. if( Ex
20b5e 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 2c prHasProperty(p,
20b5f 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 EP_xIsSelect) )
20b60 7b 0a 20 20 20 20 20 20 20 20 20 20 70 4e 65 77 {. pNew
20b61 2d 3e 78 2e 70 53 65 6c 65 63 74 20 3d 20 73 71 ->x.pSelect = sq
20b62 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70 28 64 lite3SelectDup(d
20b63 62 2c 20 70 2d 3e 78 2e 70 53 65 6c 65 63 74 2c b, p->x.pSelect,
20b64 20 69 73 52 65 64 75 63 65 64 29 3b 0a 20 20 20 isReduced);.
20b65 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
20b66 20 20 20 20 20 20 70 4e 65 77 2d 3e 78 2e 70 4c pNew->x.pL
20b67 69 73 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 ist = sqlite3Exp
20b68 72 4c 69 73 74 44 75 70 28 64 62 2c 20 70 2d 3e rListDup(db, p->
20b69 78 2e 70 4c 69 73 74 2c 20 69 73 52 65 64 75 63 x.pList, isReduc
20b6a 65 64 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 ed);. }.
20b6b 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a }.. /*
20b6c 20 46 69 6c 6c 20 69 6e 20 70 4e 65 77 2d 3e 70 Fill in pNew->p
20b6d 4c 65 66 74 20 61 6e 64 20 70 4e 65 77 2d 3e 70 Left and pNew->p
20b6e 52 69 67 68 74 2e 20 2a 2f 0a 20 20 20 20 20 20 Right. */.
20b6f 69 66 28 20 45 78 70 72 48 61 73 41 6e 79 50 72 if( ExprHasAnyPr
20b70 6f 70 65 72 74 79 28 70 4e 65 77 2c 20 45 50 5f operty(pNew, EP_
20b71 52 65 64 75 63 65 64 7c 45 50 5f 54 6f 6b 65 6e Reduced|EP_Token
20b72 4f 6e 6c 79 29 20 29 7b 0a 20 20 20 20 20 20 20 Only) ){.
20b73 20 7a 41 6c 6c 6f 63 20 2b 3d 20 64 75 70 65 64 zAlloc += duped
20b74 45 78 70 72 4e 6f 64 65 53 69 7a 65 28 70 2c 20 ExprNodeSize(p,
20b75 66 6c 61 67 73 29 3b 0a 20 20 20 20 20 20 20 20 flags);.
20b76 69 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 if( ExprHasPrope
20b77 72 74 79 28 70 4e 65 77 2c 20 45 50 5f 52 65 64 rty(pNew, EP_Red
20b78 75 63 65 64 29 20 29 7b 0a 20 20 20 20 20 20 20 uced) ){.
20b79 20 20 20 70 4e 65 77 2d 3e 70 4c 65 66 74 20 3d pNew->pLeft =
20b7a 20 65 78 70 72 44 75 70 28 64 62 2c 20 70 2d 3e exprDup(db, p->
20b7b 70 4c 65 66 74 2c 20 45 58 50 52 44 55 50 5f 52 pLeft, EXPRDUP_R
20b7c 45 44 55 43 45 2c 20 26 7a 41 6c 6c 6f 63 29 3b EDUCE, &zAlloc);
20b7d 0a 20 20 20 20 20 20 20 20 20 20 70 4e 65 77 2d . pNew-
20b7e 3e 70 52 69 67 68 74 20 3d 20 65 78 70 72 44 75 >pRight = exprDu
20b7f 70 28 64 62 2c 20 70 2d 3e 70 52 69 67 68 74 2c p(db, p->pRight,
20b80 20 45 58 50 52 44 55 50 5f 52 45 44 55 43 45 2c EXPRDUP_REDUCE,
20b81 20 26 7a 41 6c 6c 6f 63 29 3b 0a 20 20 20 20 20 &zAlloc);.
20b82 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 }. if(
20b83 20 70 7a 42 75 66 66 65 72 20 29 7b 0a 20 20 20 pzBuffer ){.
20b84 20 20 20 20 20 20 20 2a 70 7a 42 75 66 66 65 72 *pzBuffer
20b85 20 3d 20 7a 41 6c 6c 6f 63 3b 0a 20 20 20 20 20 = zAlloc;.
20b86 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 }. }else
20b87 7b 0a 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e {. pNew->
20b88 66 6c 61 67 73 32 20 3d 20 30 3b 0a 20 20 20 20 flags2 = 0;.
20b89 20 20 20 20 69 66 28 20 21 45 78 70 72 48 61 73 if( !ExprHas
20b8a 41 6e 79 50 72 6f 70 65 72 74 79 28 70 2c 20 45 AnyProperty(p, E
20b8b 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29 7b 0a P_TokenOnly) ){.
20b8c 20 20 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e pNew->
20b8d 70 4c 65 66 74 20 3d 20 73 71 6c 69 74 65 33 45 pLeft = sqlite3E
20b8e 78 70 72 44 75 70 28 64 62 2c 20 70 2d 3e 70 4c xprDup(db, p->pL
20b8f 65 66 74 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 eft, 0);.
20b90 20 20 20 70 4e 65 77 2d 3e 70 52 69 67 68 74 20 pNew->pRight
20b91 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 = sqlite3ExprDup
20b92 28 64 62 2c 20 70 2d 3e 70 52 69 67 68 74 2c 20 (db, p->pRight,
20b93 30 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 0);. }.
20b94 20 20 20 20 7d 0a 0a 20 20 20 20 7d 0a 20 20 7d }.. }. }
20b95 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a . return pNew;.
20b96 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c }../*.** The fol
20b97 6c 6f 77 69 6e 67 20 67 72 6f 75 70 20 6f 66 20 lowing group of
20b98 72 6f 75 74 69 6e 65 73 20 6d 61 6b 65 20 64 65 routines make de
20b99 65 70 20 63 6f 70 69 65 73 20 6f 66 20 65 78 70 ep copies of exp
20b9a 72 65 73 73 69 6f 6e 73 2c 0a 2a 2a 20 65 78 70 ressions,.** exp
20b9b 72 65 73 73 69 6f 6e 20 6c 69 73 74 73 2c 20 49 ression lists, I
20b9c 44 20 6c 69 73 74 73 2c 20 61 6e 64 20 73 65 6c D lists, and sel
20b9d 65 63 74 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 ect statements.
20b9e 20 54 68 65 20 63 6f 70 69 65 73 20 63 61 6e 0a The copies can.
20b9f 2a 2a 20 62 65 20 64 65 6c 65 74 65 64 20 28 62 ** be deleted (b
20ba0 79 20 62 65 69 6e 67 20 70 61 73 73 65 64 20 74 y being passed t
20ba1 6f 20 74 68 65 69 72 20 72 65 73 70 65 63 74 69 o their respecti
20ba2 76 65 20 2e 2e 2e 44 65 6c 65 74 65 28 29 20 72 ve ...Delete() r
20ba3 6f 75 74 69 6e 65 73 29 0a 2a 2a 20 77 69 74 68 outines).** with
20ba4 6f 75 74 20 65 66 66 65 63 74 69 6e 67 20 74 68 out effecting th
20ba5 65 20 6f 72 69 67 69 6e 61 6c 73 2e 0a 2a 2a 0a e originals..**.
20ba6 2a 2a 20 54 68 65 20 65 78 70 72 65 73 73 69 6f ** The expressio
20ba7 6e 20 6c 69 73 74 2c 20 49 44 2c 20 61 6e 64 20 n list, ID, and
20ba8 73 6f 75 72 63 65 20 6c 69 73 74 73 20 72 65 74 source lists ret
20ba9 75 72 6e 20 62 79 20 73 71 6c 69 74 65 33 45 78 urn by sqlite3Ex
20baa 70 72 4c 69 73 74 44 75 70 28 29 2c 0a 2a 2a 20 prListDup(),.**
20bab 73 71 6c 69 74 65 33 49 64 4c 69 73 74 44 75 70 sqlite3IdListDup
20bac 28 29 2c 20 61 6e 64 20 73 71 6c 69 74 65 33 53 (), and sqlite3S
20bad 72 63 4c 69 73 74 44 75 70 28 29 20 63 61 6e 20 rcListDup() can
20bae 6e 6f 74 20 62 65 20 66 75 72 74 68 65 72 20 65 not be further e
20baf 78 70 61 6e 64 65 64 20 0a 2a 2a 20 62 79 20 73 xpanded .** by s
20bb0 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 ubsequent calls
20bb1 74 6f 20 73 71 6c 69 74 65 2a 4c 69 73 74 41 70 to sqlite*ListAp
20bb2 70 65 6e 64 28 29 20 72 6f 75 74 69 6e 65 73 2e pend() routines.
20bb3 0a 2a 2a 0a 2a 2a 20 41 6e 79 20 74 61 62 6c 65 .**.** Any table
20bb4 73 20 74 68 61 74 20 74 68 65 20 53 72 63 4c 69 s that the SrcLi
20bb5 73 74 20 6d 69 67 68 74 20 70 6f 69 6e 74 20 74 st might point t
20bb6 6f 20 61 72 65 20 6e 6f 74 20 64 75 70 6c 69 63 o are not duplic
20bb7 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ated..**.** The
20bb8 66 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72 20 flags parameter
20bb9 63 6f 6e 74 61 69 6e 73 20 61 20 63 6f 6d 62 69 contains a combi
20bba 6e 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 45 58 nation of the EX
20bbb 50 52 44 55 50 5f 58 58 58 20 66 6c 61 67 73 2e PRDUP_XXX flags.
20bbc 0a 2a 2a 20 49 66 20 74 68 65 20 45 58 50 52 44 .** If the EXPRD
20bbd 55 50 5f 52 45 44 55 43 45 20 66 6c 61 67 20 69 UP_REDUCE flag i
20bbe 73 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20 s set, then the
20bbf 73 74 72 75 63 74 75 72 65 20 72 65 74 75 72 6e structure return
20bc0 65 64 20 69 73 20 61 0a 2a 2a 20 74 72 75 6e 63 ed is a.** trunc
20bc1 61 74 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 ated version of
20bc2 74 68 65 20 75 73 75 61 6c 20 45 78 70 72 20 73 the usual Expr s
20bc3 74 72 75 63 74 75 72 65 20 74 68 61 74 20 77 69 tructure that wi
20bc4 6c 6c 20 62 65 20 73 74 6f 72 65 64 20 61 73 0a ll be stored as.
20bc5 2a 2a 20 70 61 72 74 20 6f 66 20 74 68 65 20 69 ** part of the i
20bc6 6e 2d 6d 65 6d 6f 72 79 20 72 65 70 72 65 73 65 n-memory represe
20bc7 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 64 ntation of the d
20bc8 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 2e 0a atabase schema..
20bc9 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
20bca 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45 E Expr *sqlite3E
20bcb 78 70 72 44 75 70 28 73 71 6c 69 74 65 33 20 2a xprDup(sqlite3 *
20bcc 64 62 2c 20 45 78 70 72 20 2a 70 2c 20 69 6e 74 db, Expr *p, int
20bcd 20 66 6c 61 67 73 29 7b 0a 20 20 72 65 74 75 72 flags){. retur
20bce 6e 20 65 78 70 72 44 75 70 28 64 62 2c 20 70 2c n exprDup(db, p,
20bcf 20 66 6c 61 67 73 2c 20 30 29 3b 0a 7d 0a 53 51 flags, 0);.}.SQ
20bd0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 LITE_PRIVATE Exp
20bd1 72 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 45 78 rList *sqlite3Ex
20bd2 70 72 4c 69 73 74 44 75 70 28 73 71 6c 69 74 65 prListDup(sqlite
20bd3 33 20 2a 64 62 2c 20 45 78 70 72 4c 69 73 74 20 3 *db, ExprList
20bd4 2a 70 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a *p, int flags){.
20bd5 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4e 65 77 ExprList *pNew
20bd6 3b 0a 20 20 73 74 72 75 63 74 20 45 78 70 72 4c ;. struct ExprL
20bd7 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 2c ist_item *pItem,
20bd8 20 2a 70 4f 6c 64 49 74 65 6d 3b 0a 20 20 69 6e *pOldItem;. in
20bd9 74 20 69 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 t i;. if( p==0
20bda 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 4e ) return 0;. pN
20bdb 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 ew = sqlite3DbMa
20bdc 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65 llocRaw(db, size
20bdd 6f 66 28 2a 70 4e 65 77 29 20 29 3b 0a 20 20 69 of(*pNew) );. i
20bde 66 28 20 70 4e 65 77 3d 3d 30 20 29 20 72 65 74 f( pNew==0 ) ret
20bdf 75 72 6e 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 69 urn 0;. pNew->i
20be0 45 43 75 72 73 6f 72 20 3d 20 30 3b 0a 20 20 70 ECursor = 0;. p
20be1 4e 65 77 2d 3e 6e 45 78 70 72 20 3d 20 70 4e 65 New->nExpr = pNe
20be2 77 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 70 2d 3e 6e w->nAlloc = p->n
20be3 45 78 70 72 3b 0a 20 20 70 4e 65 77 2d 3e 61 20 Expr;. pNew->a
20be4 3d 20 70 49 74 65 6d 20 3d 20 73 71 6c 69 74 65 = pItem = sqlite
20be5 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 3DbMallocRaw(db,
20be6 20 20 70 2d 3e 6e 45 78 70 72 2a 73 69 7a 65 6f p->nExpr*sizeo
20be7 66 28 70 2d 3e 61 5b 30 5d 29 20 29 3b 0a 20 20 f(p->a[0]) );.
20be8 69 66 28 20 70 49 74 65 6d 3d 3d 30 20 29 7b 0a if( pItem==0 ){.
20be9 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
20bea 65 28 64 62 2c 20 70 4e 65 77 29 3b 0a 20 20 20 e(db, pNew);.
20beb 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 20 0a return 0;. } .
20bec 20 20 70 4f 6c 64 49 74 65 6d 20 3d 20 70 2d 3e pOldItem = p->
20bed 61 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c a;. for(i=0; i<
20bee 70 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 70 p->nExpr; i++, p
20bef 49 74 65 6d 2b 2b 2c 20 70 4f 6c 64 49 74 65 6d Item++, pOldItem
20bf0 2b 2b 29 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 ++){. Expr *p
20bf1 4f 6c 64 45 78 70 72 20 3d 20 70 4f 6c 64 49 74 OldExpr = pOldIt
20bf2 65 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20 70 em->pExpr;. p
20bf3 49 74 65 6d 2d 3e 70 45 78 70 72 20 3d 20 73 71 Item->pExpr = sq
20bf4 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c lite3ExprDup(db,
20bf5 20 70 4f 6c 64 45 78 70 72 2c 20 66 6c 61 67 73 pOldExpr, flags
20bf6 29 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e 7a 4e );. pItem->zN
20bf7 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 44 62 53 ame = sqlite3DbS
20bf8 74 72 44 75 70 28 64 62 2c 20 70 4f 6c 64 49 74 trDup(db, pOldIt
20bf9 65 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 em->zName);.
20bfa 70 49 74 65 6d 2d 3e 7a 53 70 61 6e 20 3d 20 73 pItem->zSpan = s
20bfb 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64 qlite3DbStrDup(d
20bfc 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 7a 53 70 b, pOldItem->zSp
20bfd 61 6e 29 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e an);. pItem->
20bfe 73 6f 72 74 4f 72 64 65 72 20 3d 20 70 4f 6c 64 sortOrder = pOld
20bff 49 74 65 6d 2d 3e 73 6f 72 74 4f 72 64 65 72 3b Item->sortOrder;
20c00 0a 20 20 20 20 70 49 74 65 6d 2d 3e 64 6f 6e 65 . pItem->done
20c01 20 3d 20 30 3b 0a 20 20 20 20 70 49 74 65 6d 2d = 0;. pItem-
20c02 3e 69 43 6f 6c 20 3d 20 70 4f 6c 64 49 74 65 6d >iCol = pOldItem
20c03 2d 3e 69 43 6f 6c 3b 0a 20 20 20 20 70 49 74 65 ->iCol;. pIte
20c04 6d 2d 3e 69 41 6c 69 61 73 20 3d 20 70 4f 6c 64 m->iAlias = pOld
20c05 49 74 65 6d 2d 3e 69 41 6c 69 61 73 3b 0a 20 20 Item->iAlias;.
20c06 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b }. return pNew;
20c07 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 63 75 72 .}../*.** If cur
20c08 73 6f 72 73 2c 20 74 72 69 67 67 65 72 73 2c 20 sors, triggers,
20c09 76 69 65 77 73 20 61 6e 64 20 73 75 62 71 75 65 views and subque
20c0a 72 69 65 73 20 61 72 65 20 61 6c 6c 20 6f 6d 69 ries are all omi
20c0b 74 74 65 64 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 tted from.** the
20c0c 20 62 75 69 6c 64 2c 20 74 68 65 6e 20 6e 6f 6e build, then non
20c0d 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 e of the followi
20c0e 6e 67 20 72 6f 75 74 69 6e 65 73 2c 20 65 78 63 ng routines, exc
20c0f 65 70 74 20 66 6f 72 20 0a 2a 2a 20 73 71 6c 69 ept for .** sqli
20c10 74 65 33 53 65 6c 65 63 74 44 75 70 28 29 2c 20 te3SelectDup(),
20c11 63 61 6e 20 62 65 20 63 61 6c 6c 65 64 2e 20 73 can be called. s
20c12 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70 28 qlite3SelectDup(
20c13 29 20 69 73 20 73 6f 6d 65 74 69 6d 65 73 0a 2a ) is sometimes.*
20c14 2a 20 63 61 6c 6c 65 64 20 77 69 74 68 20 61 20 * called with a
20c15 4e 55 4c 4c 20 61 72 67 75 6d 65 6e 74 2e 0a 2a NULL argument..*
20c16 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 /.#if !defined(S
20c17 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 QLITE_OMIT_VIEW)
20c18 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c || !defined(SQL
20c19 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 ITE_OMIT_TRIGGER
20c1a 29 20 5c 0a 20 7c 7c 20 21 64 65 66 69 6e 65 64 ) \. || !defined
20c1b 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 (SQLITE_OMIT_SUB
20c1c 51 55 45 52 59 29 0a 53 51 4c 49 54 45 5f 50 52 QUERY).SQLITE_PR
20c1d 49 56 41 54 45 20 53 72 63 4c 69 73 74 20 2a 73 IVATE SrcList *s
20c1e 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 75 70 qlite3SrcListDup
20c1f 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 53 72 (sqlite3 *db, Sr
20c20 63 4c 69 73 74 20 2a 70 2c 20 69 6e 74 20 66 6c cList *p, int fl
20c21 61 67 73 29 7b 0a 20 20 53 72 63 4c 69 73 74 20 ags){. SrcList
20c22 2a 70 4e 65 77 3b 0a 20 20 69 6e 74 20 69 3b 0a *pNew;. int i;.
20c23 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20 69 int nByte;. i
20c24 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e f( p==0 ) return
20c25 20 30 3b 0a 20 20 6e 42 79 74 65 20 3d 20 73 69 0;. nByte = si
20c26 7a 65 6f 66 28 2a 70 29 20 2b 20 28 70 2d 3e 6e zeof(*p) + (p->n
20c27 53 72 63 3e 30 20 3f 20 73 69 7a 65 6f 66 28 70 Src>0 ? sizeof(p
20c28 2d 3e 61 5b 30 5d 29 20 2a 20 28 70 2d 3e 6e 53 ->a[0]) * (p->nS
20c29 72 63 2d 31 29 20 3a 20 30 29 3b 0a 20 20 70 4e rc-1) : 0);. pN
20c2a 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 ew = sqlite3DbMa
20c2b 6c 6c 6f 63 52 61 77 28 64 62 2c 20 6e 42 79 74 llocRaw(db, nByt
20c2c 65 20 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d e );. if( pNew=
20c2d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 =0 ) return 0;.
20c2e 20 70 4e 65 77 2d 3e 6e 53 72 63 20 3d 20 70 4e pNew->nSrc = pN
20c2f 65 77 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 70 2d 3e ew->nAlloc = p->
20c30 6e 53 72 63 3b 0a 20 20 66 6f 72 28 69 3d 30 3b nSrc;. for(i=0;
20c31 20 69 3c 70 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29 i<p->nSrc; i++)
20c32 7b 0a 20 20 20 20 73 74 72 75 63 74 20 53 72 63 {. struct Src
20c33 4c 69 73 74 5f 69 74 65 6d 20 2a 70 4e 65 77 49 List_item *pNewI
20c34 74 65 6d 20 3d 20 26 70 4e 65 77 2d 3e 61 5b 69 tem = &pNew->a[i
20c35 5d 3b 0a 20 20 20 20 73 74 72 75 63 74 20 53 72 ];. struct Sr
20c36 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 4f 6c 64 cList_item *pOld
20c37 49 74 65 6d 20 3d 20 26 70 2d 3e 61 5b 69 5d 3b Item = &p->a[i];
20c38 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 . Table *pTab
20c39 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e ;. pNewItem->
20c3a 7a 44 61 74 61 62 61 73 65 20 3d 20 73 71 6c 69 zDatabase = sqli
20c3b 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 te3DbStrDup(db,
20c3c 70 4f 6c 64 49 74 65 6d 2d 3e 7a 44 61 74 61 62 pOldItem->zDatab
20c3d 61 73 65 29 3b 0a 20 20 20 20 70 4e 65 77 49 74 ase);. pNewIt
20c3e 65 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 em->zName = sqli
20c3f 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 te3DbStrDup(db,
20c40 70 4f 6c 64 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29 pOldItem->zName)
20c41 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e ;. pNewItem->
20c42 7a 41 6c 69 61 73 20 3d 20 73 71 6c 69 74 65 33 zAlias = sqlite3
20c43 44 62 53 74 72 44 75 70 28 64 62 2c 20 70 4f 6c DbStrDup(db, pOl
20c44 64 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 29 3b 0a dItem->zAlias);.
20c45 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 6a 6f pNewItem->jo
20c46 69 6e 74 79 70 65 20 3d 20 70 4f 6c 64 49 74 65 intype = pOldIte
20c47 6d 2d 3e 6a 6f 69 6e 74 79 70 65 3b 0a 20 20 20 m->jointype;.
20c48 20 70 4e 65 77 49 74 65 6d 2d 3e 69 43 75 72 73 pNewItem->iCurs
20c49 6f 72 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 69 or = pOldItem->i
20c4a 43 75 72 73 6f 72 3b 0a 20 20 20 20 70 4e 65 77 Cursor;. pNew
20c4b 49 74 65 6d 2d 3e 69 73 50 6f 70 75 6c 61 74 65 Item->isPopulate
20c4c 64 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 69 73 d = pOldItem->is
20c4d 50 6f 70 75 6c 61 74 65 64 3b 0a 20 20 20 20 70 Populated;. p
20c4e 4e 65 77 49 74 65 6d 2d 3e 7a 49 6e 64 65 78 20 NewItem->zIndex
20c4f 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 = sqlite3DbStrDu
20c50 70 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e p(db, pOldItem->
20c51 7a 49 6e 64 65 78 29 3b 0a 20 20 20 20 70 4e 65 zIndex);. pNe
20c52 77 49 74 65 6d 2d 3e 6e 6f 74 49 6e 64 65 78 65 wItem->notIndexe
20c53 64 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 6e 6f d = pOldItem->no
20c54 74 49 6e 64 65 78 65 64 3b 0a 20 20 20 20 70 4e tIndexed;. pN
20c55 65 77 49 74 65 6d 2d 3e 70 49 6e 64 65 78 20 3d ewItem->pIndex =
20c56 20 70 4f 6c 64 49 74 65 6d 2d 3e 70 49 6e 64 65 pOldItem->pInde
20c57 78 3b 0a 20 20 20 20 70 54 61 62 20 3d 20 70 4e x;. pTab = pN
20c58 65 77 49 74 65 6d 2d 3e 70 54 61 62 20 3d 20 70 ewItem->pTab = p
20c59 4f 6c 64 49 74 65 6d 2d 3e 70 54 61 62 3b 0a 20 OldItem->pTab;.
20c5a 20 20 20 69 66 28 20 70 54 61 62 20 29 7b 0a 20 if( pTab ){.
20c5b 20 20 20 20 20 70 54 61 62 2d 3e 6e 52 65 66 2b pTab->nRef+
20c5c 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4e 65 +;. }. pNe
20c5d 77 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 20 3d wItem->pSelect =
20c5e 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75 sqlite3SelectDu
20c5f 70 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e p(db, pOldItem->
20c60 70 53 65 6c 65 63 74 2c 20 66 6c 61 67 73 29 3b pSelect, flags);
20c61 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 70 . pNewItem->p
20c62 4f 6e 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 On = sqlite3Expr
20c63 44 75 70 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d Dup(db, pOldItem
20c64 2d 3e 70 4f 6e 2c 20 66 6c 61 67 73 29 3b 0a 20 ->pOn, flags);.
20c65 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 70 55 73 pNewItem->pUs
20c66 69 6e 67 20 3d 20 73 71 6c 69 74 65 33 49 64 4c ing = sqlite3IdL
20c67 69 73 74 44 75 70 28 64 62 2c 20 70 4f 6c 64 49 istDup(db, pOldI
20c68 74 65 6d 2d 3e 70 55 73 69 6e 67 29 3b 0a 20 20 tem->pUsing);.
20c69 20 20 70 4e 65 77 49 74 65 6d 2d 3e 63 6f 6c 55 pNewItem->colU
20c6a 73 65 64 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e sed = pOldItem->
20c6b 63 6f 6c 55 73 65 64 3b 0a 20 20 7d 0a 20 20 72 colUsed;. }. r
20c6c 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 53 51 eturn pNew;.}.SQ
20c6d 4c 49 54 45 5f 50 52 49 56 41 54 45 20 49 64 4c LITE_PRIVATE IdL
20c6e 69 73 74 20 2a 73 71 6c 69 74 65 33 49 64 4c 69 ist *sqlite3IdLi
20c6f 73 74 44 75 70 28 73 71 6c 69 74 65 33 20 2a 64 stDup(sqlite3 *d
20c70 62 2c 20 49 64 4c 69 73 74 20 2a 70 29 7b 0a 20 b, IdList *p){.
20c71 20 49 64 4c 69 73 74 20 2a 70 4e 65 77 3b 0a 20 IdList *pNew;.
20c72 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 70 3d int i;. if( p=
20c73 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 =0 ) return 0;.
20c74 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 pNew = sqlite3D
20c75 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73 bMallocRaw(db, s
20c76 69 7a 65 6f 66 28 2a 70 4e 65 77 29 20 29 3b 0a izeof(*pNew) );.
20c77 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 20 if( pNew==0 )
20c78 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 4e 65 77 return 0;. pNew
20c79 2d 3e 6e 49 64 20 3d 20 70 4e 65 77 2d 3e 6e 41 ->nId = pNew->nA
20c7a 6c 6c 6f 63 20 3d 20 70 2d 3e 6e 49 64 3b 0a 20 lloc = p->nId;.
20c7b 20 70 4e 65 77 2d 3e 61 20 3d 20 73 71 6c 69 74 pNew->a = sqlit
20c7c 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 e3DbMallocRaw(db
20c7d 2c 20 70 2d 3e 6e 49 64 2a 73 69 7a 65 6f 66 28 , p->nId*sizeof(
20c7e 70 2d 3e 61 5b 30 5d 29 20 29 3b 0a 20 20 69 66 p->a[0]) );. if
20c7f 28 20 70 4e 65 77 2d 3e 61 3d 3d 30 20 29 7b 0a ( pNew->a==0 ){.
20c80 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
20c81 65 28 64 62 2c 20 70 4e 65 77 29 3b 0a 20 20 20 e(db, pNew);.
20c82 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 return 0;. }.
20c83 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e for(i=0; i<p->n
20c84 49 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 74 Id; i++){. st
20c85 72 75 63 74 20 49 64 4c 69 73 74 5f 69 74 65 6d ruct IdList_item
20c86 20 2a 70 4e 65 77 49 74 65 6d 20 3d 20 26 70 4e *pNewItem = &pN
20c87 65 77 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20 73 74 ew->a[i];. st
20c88 72 75 63 74 20 49 64 4c 69 73 74 5f 69 74 65 6d ruct IdList_item
20c89 20 2a 70 4f 6c 64 49 74 65 6d 20 3d 20 26 70 2d *pOldItem = &p-
20c8a 3e 61 5b 69 5d 3b 0a 20 20 20 20 70 4e 65 77 49 >a[i];. pNewI
20c8b 74 65 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c tem->zName = sql
20c8c 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c ite3DbStrDup(db,
20c8d 20 70 4f 6c 64 49 74 65 6d 2d 3e 7a 4e 61 6d 65 pOldItem->zName
20c8e 29 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d );. pNewItem-
20c8f 3e 69 64 78 20 3d 20 70 4f 6c 64 49 74 65 6d 2d >idx = pOldItem-
20c90 3e 69 64 78 3b 0a 20 20 7d 0a 20 20 72 65 74 75 >idx;. }. retu
20c91 72 6e 20 70 4e 65 77 3b 0a 7d 0a 53 51 4c 49 54 rn pNew;.}.SQLIT
20c92 45 5f 50 52 49 56 41 54 45 20 53 65 6c 65 63 74 E_PRIVATE Select
20c93 20 2a 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 *sqlite3SelectD
20c94 75 70 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 up(sqlite3 *db,
20c95 53 65 6c 65 63 74 20 2a 70 2c 20 69 6e 74 20 66 Select *p, int f
20c96 6c 61 67 73 29 7b 0a 20 20 53 65 6c 65 63 74 20 lags){. Select
20c97 2a 70 4e 65 77 3b 0a 20 20 69 66 28 20 70 3d 3d *pNew;. if( p==
20c98 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 0 ) return 0;.
20c99 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 pNew = sqlite3Db
20c9a 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 MallocRaw(db, si
20c9b 7a 65 6f 66 28 2a 70 29 20 29 3b 0a 20 20 69 66 zeof(*p) );. if
20c9c 28 20 70 4e 65 77 3d 3d 30 20 29 20 72 65 74 75 ( pNew==0 ) retu
20c9d 72 6e 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 70 45 rn 0;. pNew->pE
20c9e 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33 45 78 List = sqlite3Ex
20c9f 70 72 4c 69 73 74 44 75 70 28 64 62 2c 20 70 2d prListDup(db, p-
20ca0 3e 70 45 4c 69 73 74 2c 20 66 6c 61 67 73 29 3b >pEList, flags);
20ca1 0a 20 20 70 4e 65 77 2d 3e 70 53 72 63 20 3d 20 . pNew->pSrc =
20ca2 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 75 sqlite3SrcListDu
20ca3 70 28 64 62 2c 20 70 2d 3e 70 53 72 63 2c 20 66 p(db, p->pSrc, f
20ca4 6c 61 67 73 29 3b 0a 20 20 70 4e 65 77 2d 3e 70 lags);. pNew->p
20ca5 57 68 65 72 65 20 3d 20 73 71 6c 69 74 65 33 45 Where = sqlite3E
20ca6 78 70 72 44 75 70 28 64 62 2c 20 70 2d 3e 70 57 xprDup(db, p->pW
20ca7 68 65 72 65 2c 20 66 6c 61 67 73 29 3b 0a 20 20 here, flags);.
20ca8 70 4e 65 77 2d 3e 70 47 72 6f 75 70 42 79 20 3d pNew->pGroupBy =
20ca9 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 sqlite3ExprList
20caa 44 75 70 28 64 62 2c 20 70 2d 3e 70 47 72 6f 75 Dup(db, p->pGrou
20cab 70 42 79 2c 20 66 6c 61 67 73 29 3b 0a 20 20 70 pBy, flags);. p
20cac 4e 65 77 2d 3e 70 48 61 76 69 6e 67 20 3d 20 73 New->pHaving = s
20cad 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 qlite3ExprDup(db
20cae 2c 20 70 2d 3e 70 48 61 76 69 6e 67 2c 20 66 6c , p->pHaving, fl
20caf 61 67 73 29 3b 0a 20 20 70 4e 65 77 2d 3e 70 4f ags);. pNew->pO
20cb0 72 64 65 72 42 79 20 3d 20 73 71 6c 69 74 65 33 rderBy = sqlite3
20cb1 45 78 70 72 4c 69 73 74 44 75 70 28 64 62 2c 20 ExprListDup(db,
20cb2 70 2d 3e 70 4f 72 64 65 72 42 79 2c 20 66 6c 61 p->pOrderBy, fla
20cb3 67 73 29 3b 0a 20 20 70 4e 65 77 2d 3e 6f 70 20 gs);. pNew->op
20cb4 3d 20 70 2d 3e 6f 70 3b 0a 20 20 70 4e 65 77 2d = p->op;. pNew-
20cb5 3e 70 50 72 69 6f 72 20 3d 20 73 71 6c 69 74 65 >pPrior = sqlite
20cb6 33 53 65 6c 65 63 74 44 75 70 28 64 62 2c 20 70 3SelectDup(db, p
20cb7 2d 3e 70 50 72 69 6f 72 2c 20 66 6c 61 67 73 29 ->pPrior, flags)
20cb8 3b 0a 20 20 70 4e 65 77 2d 3e 70 4c 69 6d 69 74 ;. pNew->pLimit
20cb9 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 = sqlite3ExprDu
20cba 70 28 64 62 2c 20 70 2d 3e 70 4c 69 6d 69 74 2c p(db, p->pLimit,
20cbb 20 66 6c 61 67 73 29 3b 0a 20 20 70 4e 65 77 2d flags);. pNew-
20cbc 3e 70 4f 66 66 73 65 74 20 3d 20 73 71 6c 69 74 >pOffset = sqlit
20cbd 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 2d e3ExprDup(db, p-
20cbe 3e 70 4f 66 66 73 65 74 2c 20 66 6c 61 67 73 29 >pOffset, flags)
20cbf 3b 0a 20 20 70 4e 65 77 2d 3e 69 4c 69 6d 69 74 ;. pNew->iLimit
20cc0 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 69 4f = 0;. pNew->iO
20cc1 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 70 4e 65 ffset = 0;. pNe
20cc2 77 2d 3e 73 65 6c 46 6c 61 67 73 20 3d 20 70 2d w->selFlags = p-
20cc3 3e 73 65 6c 46 6c 61 67 73 20 26 20 7e 53 46 5f >selFlags & ~SF_
20cc4 55 73 65 73 45 70 68 65 6d 65 72 61 6c 3b 0a 20 UsesEphemeral;.
20cc5 20 70 4e 65 77 2d 3e 70 52 69 67 68 74 6d 6f 73 pNew->pRightmos
20cc6 74 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 61 t = 0;. pNew->a
20cc7 64 64 72 4f 70 65 6e 45 70 68 6d 5b 30 5d 20 3d ddrOpenEphm[0] =
20cc8 20 2d 31 3b 0a 20 20 70 4e 65 77 2d 3e 61 64 64 -1;. pNew->add
20cc9 72 4f 70 65 6e 45 70 68 6d 5b 31 5d 20 3d 20 2d rOpenEphm[1] = -
20cca 31 3b 0a 20 20 70 4e 65 77 2d 3e 61 64 64 72 4f 1;. pNew->addrO
20ccb 70 65 6e 45 70 68 6d 5b 32 5d 20 3d 20 2d 31 3b penEphm[2] = -1;
20ccc 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a . return pNew;.
20ccd 7d 0a 23 65 6c 73 65 0a 53 51 4c 49 54 45 5f 50 }.#else.SQLITE_P
20cce 52 49 56 41 54 45 20 53 65 6c 65 63 74 20 2a 73 RIVATE Select *s
20ccf 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70 28 qlite3SelectDup(
20cd0 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 53 65 6c sqlite3 *db, Sel
20cd1 65 63 74 20 2a 70 2c 20 69 6e 74 20 66 6c 61 67 ect *p, int flag
20cd2 73 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 3d s){. assert( p=
20cd3 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 30 =0 );. return 0
20cd4 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a ;.}.#endif.../*.
20cd5 2a 2a 20 41 64 64 20 61 20 6e 65 77 20 65 6c 65 ** Add a new ele
20cd6 6d 65 6e 74 20 74 6f 20 74 68 65 20 65 6e 64 20 ment to the end
20cd7 6f 66 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e of an expression
20cd8 20 6c 69 73 74 2e 20 20 49 66 20 70 4c 69 73 74 list. If pList
20cd9 20 69 73 0a 2a 2a 20 69 6e 69 74 69 61 6c 6c 79 is.** initially
20cda 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 63 72 65 61 NULL, then crea
20cdb 74 65 20 61 20 6e 65 77 20 65 78 70 72 65 73 73 te a new express
20cdc 69 6f 6e 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 ion list..**.**
20cdd 49 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f If a memory allo
20cde 63 61 74 69 6f 6e 20 65 72 72 6f 72 20 6f 63 63 cation error occ
20cdf 75 72 73 2c 20 74 68 65 20 65 6e 74 69 72 65 20 urs, the entire
20ce0 6c 69 73 74 20 69 73 20 66 72 65 65 64 20 61 6e list is freed an
20ce1 64 0a 2a 2a 20 4e 55 4c 4c 20 69 73 20 72 65 74 d.** NULL is ret
20ce2 75 72 6e 65 64 2e 20 20 49 66 20 6e 6f 6e 2d 4e urned. If non-N
20ce3 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 2c ULL is returned,
20ce4 20 74 68 65 6e 20 69 74 20 69 73 20 67 75 61 72 then it is guar
20ce5 61 6e 74 65 65 64 0a 2a 2a 20 74 68 61 74 20 74 anteed.** that t
20ce6 68 65 20 6e 65 77 20 65 6e 74 72 79 20 77 61 73 he new entry was
20ce7 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 61 70 successfully ap
20ce8 70 65 6e 64 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 pended..*/.SQLIT
20ce9 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 4c 69 E_PRIVATE ExprLi
20cea 73 74 20 2a 73 71 6c 69 74 65 33 45 78 70 72 4c st *sqlite3ExprL
20ceb 69 73 74 41 70 70 65 6e 64 28 0a 20 20 50 61 72 istAppend(. Par
20cec 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 se *pParse,
20ced 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 /* Parsing
20cee 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 context */. Exp
20cef 72 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 20 20 rList *pList,
20cf0 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 74 6f 20 /* List to
20cf1 77 68 69 63 68 20 74 6f 20 61 70 70 65 6e 64 2e which to append.
20cf2 20 4d 69 67 68 74 20 62 65 20 4e 55 4c 4c 20 2a Might be NULL *
20cf3 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 20 /. Expr *pExpr
20cf4 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 /* E
20cf5 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 62 65 20 xpression to be
20cf6 61 70 70 65 6e 64 65 64 2e 20 4d 69 67 68 74 20 appended. Might
20cf7 62 65 20 4e 55 4c 4c 20 2a 2f 0a 29 7b 0a 20 20 be NULL */.){.
20cf8 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 sqlite3 *db = pP
20cf9 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20 arse->db;. if(
20cfa 70 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 pList==0 ){.
20cfb 70 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33 44 pList = sqlite3D
20cfc 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 bMallocZero(db,
20cfd 73 69 7a 65 6f 66 28 45 78 70 72 4c 69 73 74 29 sizeof(ExprList)
20cfe 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4c 69 73 );. if( pLis
20cff 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 67 6f t==0 ){. go
20d00 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 7d to no_mem;. }
20d01 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 69 . assert( pLi
20d02 73 74 2d 3e 6e 41 6c 6c 6f 63 3d 3d 30 20 29 3b st->nAlloc==0 );
20d03 0a 20 20 7d 0a 20 20 69 66 28 20 70 4c 69 73 74 . }. if( pList
20d04 2d 3e 6e 41 6c 6c 6f 63 3c 3d 70 4c 69 73 74 2d ->nAlloc<=pList-
20d05 3e 6e 45 78 70 72 20 29 7b 0a 20 20 20 20 73 74 >nExpr ){. st
20d06 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 ruct ExprList_it
20d07 65 6d 20 2a 61 3b 0a 20 20 20 20 69 6e 74 20 6e em *a;. int n
20d08 20 3d 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 = pList->nAlloc
20d09 2a 32 20 2b 20 34 3b 0a 20 20 20 20 61 20 3d 20 *2 + 4;. a =
20d0a 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 sqlite3DbRealloc
20d0b 28 64 62 2c 20 70 4c 69 73 74 2d 3e 61 2c 20 6e (db, pList->a, n
20d0c 2a 73 69 7a 65 6f 66 28 70 4c 69 73 74 2d 3e 61 *sizeof(pList->a
20d0d 5b 30 5d 29 29 3b 0a 20 20 20 20 69 66 28 20 61 [0]));. if( a
20d0e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 ==0 ){. got
20d0f 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 7d 0a o no_mem;. }.
20d10 20 20 20 20 70 4c 69 73 74 2d 3e 61 20 3d 20 61 pList->a = a
20d11 3b 0a 20 20 20 20 70 4c 69 73 74 2d 3e 6e 41 6c ;. pList->nAl
20d12 6c 6f 63 20 3d 20 73 71 6c 69 74 65 33 44 62 4d loc = sqlite3DbM
20d13 61 6c 6c 6f 63 53 69 7a 65 28 64 62 2c 20 61 29 allocSize(db, a)
20d14 2f 73 69 7a 65 6f 66 28 61 5b 30 5d 29 3b 0a 20 /sizeof(a[0]);.
20d15 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 4c 69 }. assert( pLi
20d16 73 74 2d 3e 61 21 3d 30 20 29 3b 0a 20 20 69 66 st->a!=0 );. if
20d17 28 20 31 20 29 7b 0a 20 20 20 20 73 74 72 75 63 ( 1 ){. struc
20d18 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 t ExprList_item
20d19 2a 70 49 74 65 6d 20 3d 20 26 70 4c 69 73 74 2d *pItem = &pList-
20d1a 3e 61 5b 70 4c 69 73 74 2d 3e 6e 45 78 70 72 2b >a[pList->nExpr+
20d1b 2b 5d 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 +];. memset(p
20d1c 49 74 65 6d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 Item, 0, sizeof(
20d1d 2a 70 49 74 65 6d 29 29 3b 0a 20 20 20 20 70 49 *pItem));. pI
20d1e 74 65 6d 2d 3e 70 45 78 70 72 20 3d 20 70 45 78 tem->pExpr = pEx
20d1f 70 72 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e pr;. }. return
20d20 20 70 4c 69 73 74 3b 0a 0a 6e 6f 5f 6d 65 6d 3a pList;..no_mem:
20d21 20 20 20 20 20 0a 20 20 2f 2a 20 41 76 6f 69 64 . /* Avoid
20d22 20 6c 65 61 6b 69 6e 67 20 6d 65 6d 6f 72 79 20 leaking memory
20d23 69 66 20 6d 61 6c 6c 6f 63 20 68 61 73 20 66 61 if malloc has fa
20d24 69 6c 65 64 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 iled. */. sqlit
20d25 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c e3ExprDelete(db,
20d26 20 70 45 78 70 72 29 3b 0a 20 20 73 71 6c 69 74 pExpr);. sqlit
20d27 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 e3ExprListDelete
20d28 28 64 62 2c 20 70 4c 69 73 74 29 3b 0a 20 20 72 (db, pList);. r
20d29 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a eturn 0;.}../*.*
20d2a 2a 20 53 65 74 20 74 68 65 20 45 78 70 72 4c 69 * Set the ExprLi
20d2b 73 74 2e 61 5b 5d 2e 7a 4e 61 6d 65 20 65 6c 65 st.a[].zName ele
20d2c 6d 65 6e 74 20 6f 66 20 74 68 65 20 6d 6f 73 74 ment of the most
20d2d 20 72 65 63 65 6e 74 6c 79 20 61 64 64 65 64 20 recently added
20d2e 69 74 65 6d 0a 2a 2a 20 6f 6e 20 74 68 65 20 65 item.** on the e
20d2f 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 2e 0a xpression list..
20d30 2a 2a 0a 2a 2a 20 70 4c 69 73 74 20 6d 69 67 68 **.** pList migh
20d31 74 20 62 65 20 4e 55 4c 4c 20 66 6f 6c 6c 6f 77 t be NULL follow
20d32 69 6e 67 20 61 6e 20 4f 4f 4d 20 65 72 72 6f 72 ing an OOM error
20d33 2e 20 20 42 75 74 20 70 4e 61 6d 65 20 73 68 6f . But pName sho
20d34 75 6c 64 20 6e 65 76 65 72 20 62 65 0a 2a 2a 20 uld never be.**
20d35 4e 55 4c 4c 2e 20 20 49 66 20 61 20 6d 65 6d 6f NULL. If a memo
20d36 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 ry allocation fa
20d37 69 6c 73 2c 20 74 68 65 20 70 50 61 72 73 65 2d ils, the pParse-
20d38 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 >db->mallocFaile
20d39 64 20 66 6c 61 67 0a 2a 2a 20 69 73 20 73 65 74 d flag.** is set
20d3a 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
20d3b 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
20d3c 45 78 70 72 4c 69 73 74 53 65 74 4e 61 6d 65 28 ExprListSetName(
20d3d 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 . Parse *pParse
20d3e 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 , /* Pa
20d3f 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f rsing context */
20d40 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 . ExprList *pLi
20d41 73 74 2c 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 st, /* Li
20d42 73 74 20 74 6f 20 77 68 69 63 68 20 74 6f 20 61 st to which to a
20d43 64 64 20 74 68 65 20 73 70 61 6e 2e 20 2a 2f 0a dd the span. */.
20d44 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 2c 20 Token *pName,
20d45 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d /* Nam
20d46 65 20 74 6f 20 62 65 20 61 64 64 65 64 20 2a 2f e to be added */
20d47 0a 20 20 69 6e 74 20 64 65 71 75 6f 74 65 20 20 . int dequote
20d48 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 /* Tr
20d49 75 65 20 74 6f 20 63 61 75 73 65 20 74 68 65 20 ue to cause the
20d4a 6e 61 6d 65 20 74 6f 20 62 65 20 64 65 71 75 6f name to be dequo
20d4b 74 65 64 20 2a 2f 0a 29 7b 0a 20 20 61 73 73 65 ted */.){. asse
20d4c 72 74 28 20 70 4c 69 73 74 21 3d 30 20 7c 7c 20 rt( pList!=0 ||
20d4d 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c pParse->db->mall
20d4e 6f 63 46 61 69 6c 65 64 21 3d 30 20 29 3b 0a 20 ocFailed!=0 );.
20d4f 20 69 66 28 20 70 4c 69 73 74 20 29 7b 0a 20 20 if( pList ){.
20d50 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 struct ExprLis
20d51 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 t_item *pItem;.
20d52 20 20 20 61 73 73 65 72 74 28 20 70 4c 69 73 74 assert( pList
20d53 2d 3e 6e 45 78 70 72 3e 30 20 29 3b 0a 20 20 20 ->nExpr>0 );.
20d54 20 70 49 74 65 6d 20 3d 20 26 70 4c 69 73 74 2d pItem = &pList-
20d55 3e 61 5b 70 4c 69 73 74 2d 3e 6e 45 78 70 72 2d >a[pList->nExpr-
20d56 31 5d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 1];. assert(
20d57 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 3d 3d 30 20 pItem->zName==0
20d58 29 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e 7a 4e );. pItem->zN
20d59 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 44 62 53 ame = sqlite3DbS
20d5a 74 72 4e 44 75 70 28 70 50 61 72 73 65 2d 3e 64 trNDup(pParse->d
20d5b 62 2c 20 70 4e 61 6d 65 2d 3e 7a 2c 20 70 4e 61 b, pName->z, pNa
20d5c 6d 65 2d 3e 6e 29 3b 0a 20 20 20 20 69 66 28 20 me->n);. if(
20d5d 64 65 71 75 6f 74 65 20 26 26 20 70 49 74 65 6d dequote && pItem
20d5e 2d 3e 7a 4e 61 6d 65 20 29 20 73 71 6c 69 74 65 ->zName ) sqlite
20d5f 33 44 65 71 75 6f 74 65 28 70 49 74 65 6d 2d 3e 3Dequote(pItem->
20d60 7a 4e 61 6d 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f zName);. }.}../
20d61 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 45 78 70 *.** Set the Exp
20d62 72 4c 69 73 74 2e 61 5b 5d 2e 7a 53 70 61 6e 20 rList.a[].zSpan
20d63 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 6d element of the m
20d64 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 61 64 64 ost recently add
20d65 65 64 20 69 74 65 6d 0a 2a 2a 20 6f 6e 20 74 68 ed item.** on th
20d66 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 e expression lis
20d67 74 2e 0a 2a 2a 0a 2a 2a 20 70 4c 69 73 74 20 6d t..**.** pList m
20d68 69 67 68 74 20 62 65 20 4e 55 4c 4c 20 66 6f 6c ight be NULL fol
20d69 6c 6f 77 69 6e 67 20 61 6e 20 4f 4f 4d 20 65 72 lowing an OOM er
20d6a 72 6f 72 2e 20 20 42 75 74 20 70 53 70 61 6e 20 ror. But pSpan
20d6b 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 62 65 0a should never be.
20d6c 2a 2a 20 4e 55 4c 4c 2e 20 20 49 66 20 61 20 6d ** NULL. If a m
20d6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
20d6e 20 66 61 69 6c 73 2c 20 74 68 65 20 70 50 61 72 fails, the pPar
20d6f 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 se->db->mallocFa
20d70 69 6c 65 64 20 66 6c 61 67 0a 2a 2a 20 69 73 20 iled flag.** is
20d71 73 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 set..*/.SQLITE_P
20d72 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
20d73 74 65 33 45 78 70 72 4c 69 73 74 53 65 74 53 70 te3ExprListSetSp
20d74 61 6e 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 an(. Parse *pPa
20d75 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a rse, /*
20d76 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 Parsing context
20d77 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a */. ExprList *
20d78 70 4c 69 73 74 2c 20 20 20 20 20 20 20 20 2f 2a pList, /*
20d79 20 4c 69 73 74 20 74 6f 20 77 68 69 63 68 20 74 List to which t
20d7a 6f 20 61 64 64 20 74 68 65 20 73 70 61 6e 2e 20 o add the span.
20d7b 2a 2f 0a 20 20 45 78 70 72 53 70 61 6e 20 2a 70 */. ExprSpan *p
20d7c 53 70 61 6e 20 20 20 20 20 20 20 20 20 2f 2a 20 Span /*
20d7d 54 68 65 20 73 70 61 6e 20 74 6f 20 62 65 20 61 The span to be a
20d7e 64 64 65 64 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c dded */.){. sql
20d7f 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 ite3 *db = pPars
20d80 65 2d 3e 64 62 3b 0a 20 20 61 73 73 65 72 74 28 e->db;. assert(
20d81 20 70 4c 69 73 74 21 3d 30 20 7c 7c 20 64 62 2d pList!=0 || db-
20d82 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 21 3d 30 >mallocFailed!=0
20d83 20 29 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 20 );. if( pList
20d84 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 45 78 ){. struct Ex
20d85 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 prList_item *pIt
20d86 65 6d 20 3d 20 26 70 4c 69 73 74 2d 3e 61 5b 70 em = &pList->a[p
20d87 4c 69 73 74 2d 3e 6e 45 78 70 72 2d 31 5d 3b 0a List->nExpr-1];.
20d88 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 69 73 assert( pLis
20d89 74 2d 3e 6e 45 78 70 72 3e 30 20 29 3b 0a 20 20 t->nExpr>0 );.
20d8a 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6d 61 assert( db->ma
20d8b 6c 6c 6f 63 46 61 69 6c 65 64 20 7c 7c 20 70 49 llocFailed || pI
20d8c 74 65 6d 2d 3e 70 45 78 70 72 3d 3d 70 53 70 61 tem->pExpr==pSpa
20d8d 6e 2d 3e 70 45 78 70 72 20 29 3b 0a 20 20 20 20 n->pExpr );.
20d8e 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
20d8f 2c 20 70 49 74 65 6d 2d 3e 7a 53 70 61 6e 29 3b , pItem->zSpan);
20d90 0a 20 20 20 20 70 49 74 65 6d 2d 3e 7a 53 70 61 . pItem->zSpa
20d91 6e 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 n = sqlite3DbStr
20d92 4e 44 75 70 28 64 62 2c 20 28 63 68 61 72 2a 29 NDup(db, (char*)
20d93 70 53 70 61 6e 2d 3e 7a 53 74 61 72 74 2c 0a 20 pSpan->zStart,.
20d94 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20d95 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20d96 20 20 20 28 69 6e 74 29 28 70 53 70 61 6e 2d 3e (int)(pSpan->
20d97 7a 45 6e 64 20 2d 20 70 53 70 61 6e 2d 3e 7a 53 zEnd - pSpan->zS
20d98 74 61 72 74 29 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f tart));. }.}../
20d99 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65 78 70 72 *.** If the expr
20d9a 65 73 73 69 6f 6e 20 6c 69 73 74 20 70 45 4c 69 ession list pELi
20d9b 73 74 20 63 6f 6e 74 61 69 6e 73 20 6d 6f 72 65 st contains more
20d9c 20 74 68 61 6e 20 69 4c 69 6d 69 74 20 65 6c 65 than iLimit ele
20d9d 6d 65 6e 74 73 2c 0a 2a 2a 20 6c 65 61 76 65 20 ments,.** leave
20d9e 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 an error message
20d9f 20 69 6e 20 70 50 61 72 73 65 2e 0a 2a 2f 0a 53 in pParse..*/.S
20da0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
20da1 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 id sqlite3ExprLi
20da2 73 74 43 68 65 63 6b 4c 65 6e 67 74 68 28 0a 20 stCheckLength(.
20da3 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 0a Parse *pParse,.
20da4 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 ExprList *pELi
20da5 73 74 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 st,. const char
20da6 20 2a 7a 4f 62 6a 65 63 74 0a 29 7b 0a 20 20 69 *zObject.){. i
20da7 6e 74 20 6d 78 20 3d 20 70 50 61 72 73 65 2d 3e nt mx = pParse->
20da8 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 db->aLimit[SQLIT
20da9 45 5f 4c 49 4d 49 54 5f 43 4f 4c 55 4d 4e 5d 3b E_LIMIT_COLUMN];
20daa 0a 20 20 74 65 73 74 63 61 73 65 28 20 70 45 4c . testcase( pEL
20dab 69 73 74 20 26 26 20 70 45 4c 69 73 74 2d 3e 6e ist && pEList->n
20dac 45 78 70 72 3d 3d 6d 78 20 29 3b 0a 20 20 74 65 Expr==mx );. te
20dad 73 74 63 61 73 65 28 20 70 45 4c 69 73 74 20 26 stcase( pEList &
20dae 26 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3d & pEList->nExpr=
20daf 3d 6d 78 2b 31 20 29 3b 0a 20 20 69 66 28 20 70 =mx+1 );. if( p
20db0 45 4c 69 73 74 20 26 26 20 70 45 4c 69 73 74 2d EList && pEList-
20db1 3e 6e 45 78 70 72 3e 6d 78 20 29 7b 0a 20 20 20 >nExpr>mx ){.
20db2 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 sqlite3ErrorMsg
20db3 28 70 50 61 72 73 65 2c 20 22 74 6f 6f 20 6d 61 (pParse, "too ma
20db4 6e 79 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 25 73 ny columns in %s
20db5 22 2c 20 7a 4f 62 6a 65 63 74 29 3b 0a 20 20 7d ", zObject);. }
20db6 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 .}../*.** Delete
20db7 20 61 6e 20 65 6e 74 69 72 65 20 65 78 70 72 65 an entire expre
20db8 73 73 69 6f 6e 20 6c 69 73 74 2e 0a 2a 2f 0a 53 ssion list..*/.S
20db9 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
20dba 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 id sqlite3ExprLi
20dbb 73 74 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33 stDelete(sqlite3
20dbc 20 2a 64 62 2c 20 45 78 70 72 4c 69 73 74 20 2a *db, ExprList *
20dbd 70 4c 69 73 74 29 7b 0a 20 20 69 6e 74 20 69 3b pList){. int i;
20dbe 0a 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 . struct ExprLi
20dbf 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a st_item *pItem;.
20dc0 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 if( pList==0 )
20dc1 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65 72 return;. asser
20dc2 74 28 20 70 4c 69 73 74 2d 3e 61 21 3d 30 20 7c t( pList->a!=0 |
20dc3 7c 20 28 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3d | (pList->nExpr=
20dc4 3d 30 20 26 26 20 70 4c 69 73 74 2d 3e 6e 41 6c =0 && pList->nAl
20dc5 6c 6f 63 3d 3d 30 29 20 29 3b 0a 20 20 61 73 73 loc==0) );. ass
20dc6 65 72 74 28 20 70 4c 69 73 74 2d 3e 6e 45 78 70 ert( pList->nExp
20dc7 72 3c 3d 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 r<=pList->nAlloc
20dc8 20 29 3b 0a 20 20 66 6f 72 28 70 49 74 65 6d 3d );. for(pItem=
20dc9 70 4c 69 73 74 2d 3e 61 2c 20 69 3d 30 3b 20 69 pList->a, i=0; i
20dca 3c 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 <pList->nExpr; i
20dcb 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 ++, pItem++){.
20dcc 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c sqlite3ExprDel
20dcd 65 74 65 28 64 62 2c 20 70 49 74 65 6d 2d 3e 70 ete(db, pItem->p
20dce 45 78 70 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 Expr);. sqlit
20dcf 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 49 74 e3DbFree(db, pIt
20dd0 65 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 em->zName);.
20dd1 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
20dd2 2c 20 70 49 74 65 6d 2d 3e 7a 53 70 61 6e 29 3b , pItem->zSpan);
20dd3 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 62 . }. sqlite3Db
20dd4 46 72 65 65 28 64 62 2c 20 70 4c 69 73 74 2d 3e Free(db, pList->
20dd5 61 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 a);. sqlite3DbF
20dd6 72 65 65 28 64 62 2c 20 70 4c 69 73 74 29 3b 0a ree(db, pList);.
20dd7 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 72 }../*.** These r
20dd8 6f 75 74 69 6e 65 73 20 61 72 65 20 57 61 6c 6b outines are Walk
20dd9 65 72 20 63 61 6c 6c 62 61 63 6b 73 2e 20 20 57 er callbacks. W
20dda 61 6c 6b 65 72 2e 75 2e 70 69 20 69 73 20 61 20 alker.u.pi is a
20ddb 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 61 6e pointer.** to an
20ddc 20 69 6e 74 65 67 65 72 2e 20 20 54 68 65 73 65 integer. These
20ddd 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 63 68 routines are ch
20dde 65 63 6b 69 6e 67 20 61 6e 20 65 78 70 72 65 73 ecking an expres
20ddf 73 69 6f 6e 20 74 6f 20 73 65 65 0a 2a 2a 20 69 sion to see.** i
20de0 66 20 69 74 20 69 73 20 61 20 63 6f 6e 73 74 61 f it is a consta
20de1 6e 74 2e 20 20 53 65 74 20 2a 57 61 6c 6b 65 72 nt. Set *Walker
20de2 2e 75 2e 70 69 20 74 6f 20 30 20 69 66 20 74 68 .u.pi to 0 if th
20de3 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 0a e expression is.
20de4 2a 2a 20 6e 6f 74 20 63 6f 6e 73 74 61 6e 74 2e ** not constant.
20de5 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 63 61 6c .**.** These cal
20de6 6c 62 61 63 6b 20 72 6f 75 74 69 6e 65 73 20 61 lback routines a
20de7 72 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 re used to imple
20de8 6d 65 6e 74 20 74 68 65 20 66 6f 6c 6c 6f 77 69 ment the followi
20de9 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 73 71 ng:.**.** sq
20dea 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74 lite3ExprIsConst
20deb 61 6e 74 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c ant().** sql
20dec 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61 ite3ExprIsConsta
20ded 6e 74 4e 6f 74 4a 6f 69 6e 28 29 0a 2a 2a 20 20 ntNotJoin().**
20dee 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 sqlite3ExprIs
20def 43 6f 6e 73 74 61 6e 74 4f 72 46 75 6e 63 74 69 ConstantOrFuncti
20df0 6f 6e 28 29 0a 2a 2a 0a 2a 2f 0a 73 74 61 74 69 on().**.*/.stati
20df1 63 20 69 6e 74 20 65 78 70 72 4e 6f 64 65 49 73 c int exprNodeIs
20df2 43 6f 6e 73 74 61 6e 74 28 57 61 6c 6b 65 72 20 Constant(Walker
20df3 2a 70 57 61 6c 6b 65 72 2c 20 45 78 70 72 20 2a *pWalker, Expr *
20df4 70 45 78 70 72 29 7b 0a 0a 20 20 2f 2a 20 49 66 pExpr){.. /* If
20df5 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 69 20 69 73 pWalker->u.i is
20df6 20 33 20 74 68 65 6e 20 61 6e 79 20 74 65 72 6d 3 then any term
20df7 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69 of the expressi
20df8 6f 6e 20 74 68 61 74 20 63 6f 6d 65 73 20 66 72 on that comes fr
20df9 6f 6d 0a 20 20 2a 2a 20 74 68 65 20 4f 4e 20 6f om. ** the ON o
20dfa 72 20 55 53 49 4e 47 20 63 6c 61 75 73 65 73 20 r USING clauses
20dfb 6f 66 20 61 20 6a 6f 69 6e 20 64 69 73 71 75 61 of a join disqua
20dfc 6c 69 66 69 65 73 20 74 68 65 20 65 78 70 72 65 lifies the expre
20dfd 73 73 69 6f 6e 0a 20 20 2a 2a 20 66 72 6f 6d 20 ssion. ** from
20dfe 62 65 69 6e 67 20 63 6f 6e 73 69 64 65 72 65 64 being considered
20dff 20 63 6f 6e 73 74 61 6e 74 2e 20 2a 2f 0a 20 20 constant. */.
20e00 69 66 28 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 69 if( pWalker->u.i
20e01 3d 3d 33 20 26 26 20 45 78 70 72 48 61 73 41 6e ==3 && ExprHasAn
20e02 79 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c yProperty(pExpr,
20e03 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e 29 20 29 7b EP_FromJoin) ){
20e04 0a 20 20 20 20 70 57 61 6c 6b 65 72 2d 3e 75 2e . pWalker->u.
20e05 69 20 3d 20 30 3b 0a 20 20 20 20 72 65 74 75 72 i = 0;. retur
20e06 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 7d n WRC_Abort;. }
20e07 0a 0a 20 20 73 77 69 74 63 68 28 20 70 45 78 70 .. switch( pExp
20e08 72 2d 3e 6f 70 20 29 7b 0a 20 20 20 20 2f 2a 20 r->op ){. /*
20e09 43 6f 6e 73 69 64 65 72 20 66 75 6e 63 74 69 6f Consider functio
20e0a 6e 73 20 74 6f 20 62 65 20 63 6f 6e 73 74 61 6e ns to be constan
20e0b 74 20 69 66 20 61 6c 6c 20 74 68 65 69 72 20 61 t if all their a
20e0c 72 67 75 6d 65 6e 74 73 20 61 72 65 20 63 6f 6e rguments are con
20e0d 73 74 61 6e 74 0a 20 20 20 20 2a 2a 20 61 6e 64 stant. ** and
20e0e 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 69 3d 3d 32 pWalker->u.i==2
20e0f 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f */. case TK_
20e10 46 55 4e 43 54 49 4f 4e 3a 0a 20 20 20 20 20 20 FUNCTION:.
20e11 69 66 28 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 69 if( pWalker->u.i
20e12 3d 3d 32 20 29 20 72 65 74 75 72 6e 20 30 3b 0a ==2 ) return 0;.
20e13 20 20 20 20 20 20 2f 2a 20 46 61 6c 6c 20 74 68 /* Fall th
20e14 72 6f 75 67 68 20 2a 2f 0a 20 20 20 20 63 61 73 rough */. cas
20e15 65 20 54 4b 5f 49 44 3a 0a 20 20 20 20 63 61 73 e TK_ID:. cas
20e16 65 20 54 4b 5f 43 4f 4c 55 4d 4e 3a 0a 20 20 20 e TK_COLUMN:.
20e17 20 63 61 73 65 20 54 4b 5f 41 47 47 5f 46 55 4e case TK_AGG_FUN
20e18 43 54 49 4f 4e 3a 0a 20 20 20 20 63 61 73 65 20 CTION:. case
20e19 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 3a 0a 20 TK_AGG_COLUMN:.
20e1a 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 testcase( p
20e1b 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 49 44 20 Expr->op==TK_ID
20e1c 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 );. testcas
20e1d 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b e( pExpr->op==TK
20e1e 5f 43 4f 4c 55 4d 4e 20 29 3b 0a 20 20 20 20 20 _COLUMN );.
20e1f 20 74 65 73 74 63 61 73 65 28 20 70 45 78 70 72 testcase( pExpr
20e20 2d 3e 6f 70 3d 3d 54 4b 5f 41 47 47 5f 46 55 4e ->op==TK_AGG_FUN
20e21 43 54 49 4f 4e 20 29 3b 0a 20 20 20 20 20 20 74 CTION );. t
20e22 65 73 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e estcase( pExpr->
20e23 6f 70 3d 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d op==TK_AGG_COLUM
20e24 4e 20 29 3b 0a 20 20 20 20 20 20 70 57 61 6c 6b N );. pWalk
20e25 65 72 2d 3e 75 2e 69 20 3d 20 30 3b 0a 20 20 20 er->u.i = 0;.
20e26 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 return WRC_Ab
20e27 6f 72 74 3b 0a 20 20 20 20 64 65 66 61 75 6c 74 ort;. default
20e28 3a 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 :. testcase
20e29 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f ( pExpr->op==TK_
20e2a 53 45 4c 45 43 54 20 29 3b 20 2f 2a 20 73 65 6c SELECT ); /* sel
20e2b 65 63 74 4e 6f 64 65 49 73 43 6f 6e 73 74 61 6e ectNodeIsConstan
20e2c 74 20 77 69 6c 6c 20 64 69 73 61 6c 6c 6f 77 20 t will disallow
20e2d 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 */. testcas
20e2e 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b e( pExpr->op==TK
20e2f 5f 45 58 49 53 54 53 20 29 3b 20 2f 2a 20 73 65 _EXISTS ); /* se
20e30 6c 65 63 74 4e 6f 64 65 49 73 43 6f 6e 73 74 61 lectNodeIsConsta
20e31 6e 74 20 77 69 6c 6c 20 64 69 73 61 6c 6c 6f 77 nt will disallow
20e32 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 72 6e */. return
20e33 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 20 WRC_Continue;.
20e34 20 7d 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 }.}.static int
20e35 73 65 6c 65 63 74 4e 6f 64 65 49 73 43 6f 6e 73 selectNodeIsCons
20e36 74 61 6e 74 28 57 61 6c 6b 65 72 20 2a 70 57 61 tant(Walker *pWa
20e37 6c 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a 4e 6f lker, Select *No
20e38 74 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 tUsed){. UNUSED
20e39 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 _PARAMETER(NotUs
20e3a 65 64 29 3b 0a 20 20 70 57 61 6c 6b 65 72 2d 3e ed);. pWalker->
20e3b 75 2e 69 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 u.i = 0;. retur
20e3c 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 7d 0a 73 n WRC_Abort;.}.s
20e3d 74 61 74 69 63 20 69 6e 74 20 65 78 70 72 49 73 tatic int exprIs
20e3e 43 6f 6e 73 74 28 45 78 70 72 20 2a 70 2c 20 69 Const(Expr *p, i
20e3f 6e 74 20 69 6e 69 74 46 6c 61 67 29 7b 0a 20 20 nt initFlag){.
20e40 57 61 6c 6b 65 72 20 77 3b 0a 20 20 77 2e 75 2e Walker w;. w.u.
20e41 69 20 3d 20 69 6e 69 74 46 6c 61 67 3b 0a 20 20 i = initFlag;.
20e42 77 2e 78 45 78 70 72 43 61 6c 6c 62 61 63 6b 20 w.xExprCallback
20e43 3d 20 65 78 70 72 4e 6f 64 65 49 73 43 6f 6e 73 = exprNodeIsCons
20e44 74 61 6e 74 3b 0a 20 20 77 2e 78 53 65 6c 65 63 tant;. w.xSelec
20e45 74 43 61 6c 6c 62 61 63 6b 20 3d 20 73 65 6c 65 tCallback = sele
20e46 63 74 4e 6f 64 65 49 73 43 6f 6e 73 74 61 6e 74 ctNodeIsConstant
20e47 3b 0a 20 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 ;. sqlite3WalkE
20e48 78 70 72 28 26 77 2c 20 70 29 3b 0a 20 20 72 65 xpr(&w, p);. re
20e49 74 75 72 6e 20 77 2e 75 2e 69 3b 0a 7d 0a 0a 2f turn w.u.i;.}../
20e4a 2a 0a 2a 2a 20 57 61 6c 6b 20 61 6e 20 65 78 70 *.** Walk an exp
20e4b 72 65 73 73 69 6f 6e 20 74 72 65 65 2e 20 20 52 ression tree. R
20e4c 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 20 65 eturn 1 if the e
20e4d 78 70 72 65 73 73 69 6f 6e 20 69 73 20 63 6f 6e xpression is con
20e4e 73 74 61 6e 74 0a 2a 2a 20 61 6e 64 20 30 20 69 stant.** and 0 i
20e4f 66 20 69 74 20 69 6e 76 6f 6c 76 65 73 20 76 61 f it involves va
20e50 72 69 61 62 6c 65 73 20 6f 72 20 66 75 6e 63 74 riables or funct
20e51 69 6f 6e 20 63 61 6c 6c 73 2e 0a 2a 2a 0a 2a 2a ion calls..**.**
20e52 20 46 6f 72 20 74 68 65 20 70 75 72 70 6f 73 65 For the purpose
20e53 73 20 6f 66 20 74 68 69 73 20 66 75 6e 63 74 69 s of this functi
20e54 6f 6e 2c 20 61 20 64 6f 75 62 6c 65 2d 71 75 6f on, a double-quo
20e55 74 65 64 20 73 74 72 69 6e 67 20 28 65 78 3a 20 ted string (ex:
20e56 22 61 62 63 22 29 0a 2a 2a 20 69 73 20 63 6f 6e "abc").** is con
20e57 73 69 64 65 72 65 64 20 61 20 76 61 72 69 61 62 sidered a variab
20e58 6c 65 20 62 75 74 20 61 20 73 69 6e 67 6c 65 2d le but a single-
20e59 71 75 6f 74 65 64 20 73 74 72 69 6e 67 20 28 65 quoted string (e
20e5a 78 3a 20 27 61 62 63 27 29 20 69 73 0a 2a 2a 20 x: 'abc') is.**
20e5b 61 20 63 6f 6e 73 74 61 6e 74 2e 0a 2a 2f 0a 53 a constant..*/.S
20e5c 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
20e5d 74 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 43 t sqlite3ExprIsC
20e5e 6f 6e 73 74 61 6e 74 28 45 78 70 72 20 2a 70 29 onstant(Expr *p)
20e5f 7b 0a 20 20 72 65 74 75 72 6e 20 65 78 70 72 49 {. return exprI
20e60 73 43 6f 6e 73 74 28 70 2c 20 31 29 3b 0a 7d 0a sConst(p, 1);.}.
20e61 0a 2f 2a 0a 2a 2a 20 57 61 6c 6b 20 61 6e 20 65 ./*.** Walk an e
20e62 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2e 20 xpression tree.
20e63 20 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 Return 1 if the
20e64 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 63 expression is c
20e65 6f 6e 73 74 61 6e 74 0a 2a 2a 20 74 68 61 74 20 onstant.** that
20e66 64 6f 65 73 20 6e 6f 20 6f 72 69 67 69 6e 61 74 does no originat
20e67 65 20 66 72 6f 6d 20 74 68 65 20 4f 4e 20 6f 72 e from the ON or
20e68 20 55 53 49 4e 47 20 63 6c 61 75 73 65 73 20 6f USING clauses o
20e69 66 20 61 20 6a 6f 69 6e 2e 0a 2a 2a 20 52 65 74 f a join..** Ret
20e6a 75 72 6e 20 30 20 69 66 20 69 74 20 69 6e 76 6f urn 0 if it invo
20e6b 6c 76 65 73 20 76 61 72 69 61 62 6c 65 73 20 6f lves variables o
20e6c 72 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 73 r function calls
20e6d 20 6f 72 20 74 65 72 6d 73 20 66 72 6f 6d 0a 2a or terms from.*
20e6e 2a 20 61 6e 20 4f 4e 20 6f 72 20 55 53 49 4e 47 * an ON or USING
20e6f 20 63 6c 61 75 73 65 2e 0a 2a 2f 0a 53 51 4c 49 clause..*/.SQLI
20e70 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
20e71 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 qlite3ExprIsCons
20e72 74 61 6e 74 4e 6f 74 4a 6f 69 6e 28 45 78 70 72 tantNotJoin(Expr
20e73 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 65 *p){. return e
20e74 78 70 72 49 73 43 6f 6e 73 74 28 70 2c 20 33 29 xprIsConst(p, 3)
20e75 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 61 6c 6b 20 ;.}../*.** Walk
20e76 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 an expression tr
20e77 65 65 2e 20 20 52 65 74 75 72 6e 20 31 20 69 66 ee. Return 1 if
20e78 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 the expression
20e79 69 73 20 63 6f 6e 73 74 61 6e 74 0a 2a 2a 20 6f is constant.** o
20e7a 72 20 61 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c r a function cal
20e7b 6c 20 77 69 74 68 20 63 6f 6e 73 74 61 6e 74 20 l with constant
20e7c 61 72 67 75 6d 65 6e 74 73 2e 20 20 52 65 74 75 arguments. Retu
20e7d 72 6e 20 61 6e 64 20 30 20 69 66 20 74 68 65 72 rn and 0 if ther
20e7e 65 0a 2a 2a 20 61 72 65 20 61 6e 79 20 76 61 72 e.** are any var
20e7f 69 61 62 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 46 6f iables..**.** Fo
20e80 72 20 74 68 65 20 70 75 72 70 6f 73 65 73 20 6f r the purposes o
20e81 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c f this function,
20e82 20 61 20 64 6f 75 62 6c 65 2d 71 75 6f 74 65 64 a double-quoted
20e83 20 73 74 72 69 6e 67 20 28 65 78 3a 20 22 61 62 string (ex: "ab
20e84 63 22 29 0a 2a 2a 20 69 73 20 63 6f 6e 73 69 64 c").** is consid
20e85 65 72 65 64 20 61 20 76 61 72 69 61 62 6c 65 20 ered a variable
20e86 62 75 74 20 61 20 73 69 6e 67 6c 65 2d 71 75 6f but a single-quo
20e87 74 65 64 20 73 74 72 69 6e 67 20 28 65 78 3a 20 ted string (ex:
20e88 27 61 62 63 27 29 20 69 73 0a 2a 2a 20 61 20 63 'abc') is.** a c
20e89 6f 6e 73 74 61 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 onstant..*/.SQLI
20e8a 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
20e8b 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 qlite3ExprIsCons
20e8c 74 61 6e 74 4f 72 46 75 6e 63 74 69 6f 6e 28 45 tantOrFunction(E
20e8d 78 70 72 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 xpr *p){. retur
20e8e 6e 20 65 78 70 72 49 73 43 6f 6e 73 74 28 70 2c n exprIsConst(p,
20e8f 20 32 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 2);.}../*.** If
20e90 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 the expression
20e91 70 20 63 6f 64 65 73 20 61 20 63 6f 6e 73 74 61 p codes a consta
20e92 6e 74 20 69 6e 74 65 67 65 72 20 74 68 61 74 20 nt integer that
20e93 69 73 20 73 6d 61 6c 6c 20 65 6e 6f 75 67 68 0a is small enough.
20e94 2a 2a 20 74 6f 20 66 69 74 20 69 6e 20 61 20 33 ** to fit in a 3
20e95 32 2d 62 69 74 20 69 6e 74 65 67 65 72 2c 20 72 2-bit integer, r
20e96 65 74 75 72 6e 20 31 20 61 6e 64 20 70 75 74 20 eturn 1 and put
20e97 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 the value of the
20e98 20 69 6e 74 65 67 65 72 0a 2a 2a 20 69 6e 20 2a integer.** in *
20e99 70 56 61 6c 75 65 2e 20 20 49 66 20 74 68 65 20 pValue. If the
20e9a 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 6e 6f expression is no
20e9b 74 20 61 6e 20 69 6e 74 65 67 65 72 20 6f 72 20 t an integer or
20e9c 69 66 20 69 74 20 69 73 20 74 6f 6f 20 62 69 67 if it is too big
20e9d 0a 2a 2a 20 74 6f 20 66 69 74 20 69 6e 20 61 20 .** to fit in a
20e9e 73 69 67 6e 65 64 20 33 32 2d 62 69 74 20 69 6e signed 32-bit in
20e9f 74 65 67 65 72 2c 20 72 65 74 75 72 6e 20 30 20 teger, return 0
20ea0 61 6e 64 20 6c 65 61 76 65 20 2a 70 56 61 6c 75 and leave *pValu
20ea1 65 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a e unchanged..*/.
20ea2 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
20ea3 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 nt sqlite3ExprIs
20ea4 49 6e 74 65 67 65 72 28 45 78 70 72 20 2a 70 2c Integer(Expr *p,
20ea5 20 69 6e 74 20 2a 70 56 61 6c 75 65 29 7b 0a 20 int *pValue){.
20ea6 20 69 6e 74 20 72 63 20 3d 20 30 3b 0a 20 20 69 int rc = 0;. i
20ea7 66 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 45 50 f( p->flags & EP
20ea8 5f 49 6e 74 56 61 6c 75 65 20 29 7b 0a 20 20 20 _IntValue ){.
20ea9 20 2a 70 56 61 6c 75 65 20 3d 20 70 2d 3e 75 2e *pValue = p->u.
20eaa 69 56 61 6c 75 65 3b 0a 20 20 20 20 72 65 74 75 iValue;. retu
20eab 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 73 77 69 74 rn 1;. }. swit
20eac 63 68 28 20 70 2d 3e 6f 70 20 29 7b 0a 20 20 20 ch( p->op ){.
20ead 20 63 61 73 65 20 54 4b 5f 49 4e 54 45 47 45 52 case TK_INTEGER
20eae 3a 20 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 : {. rc = s
20eaf 71 6c 69 74 65 33 47 65 74 49 6e 74 33 32 28 70 qlite3GetInt32(p
20eb0 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 20 70 56 61 6c ->u.zToken, pVal
20eb1 75 65 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 ue);. asser
20eb2 74 28 20 72 63 3d 3d 30 20 29 3b 0a 20 20 20 20 t( rc==0 );.
20eb3 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
20eb4 20 20 20 63 61 73 65 20 54 4b 5f 55 50 4c 55 53 case TK_UPLUS
20eb5 3a 20 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 : {. rc = s
20eb6 71 6c 69 74 65 33 45 78 70 72 49 73 49 6e 74 65 qlite3ExprIsInte
20eb7 67 65 72 28 70 2d 3e 70 4c 65 66 74 2c 20 70 56 ger(p->pLeft, pV
20eb8 61 6c 75 65 29 3b 0a 20 20 20 20 20 20 62 72 65 alue);. bre
20eb9 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 ak;. }. ca
20eba 73 65 20 54 4b 5f 55 4d 49 4e 55 53 3a 20 7b 0a se TK_UMINUS: {.
20ebb 20 20 20 20 20 20 69 6e 74 20 76 3b 0a 20 20 20 int v;.
20ebc 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 45 78 if( sqlite3Ex
20ebd 70 72 49 73 49 6e 74 65 67 65 72 28 70 2d 3e 70 prIsInteger(p->p
20ebe 4c 65 66 74 2c 20 26 76 29 20 29 7b 0a 20 20 20 Left, &v) ){.
20ebf 20 20 20 20 20 2a 70 56 61 6c 75 65 20 3d 20 2d *pValue = -
20ec0 76 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 v;. rc =
20ec1 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 1;. }.
20ec2 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
20ec3 20 20 64 65 66 61 75 6c 74 3a 20 62 72 65 61 6b default: break
20ec4 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 20 29 ;. }. if( rc )
20ec5 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 45 78 {. assert( Ex
20ec6 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 prHasAnyProperty
20ec7 28 70 2c 20 45 50 5f 52 65 64 75 63 65 64 7c 45 (p, EP_Reduced|E
20ec8 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 0a 20 20 20 P_TokenOnly).
20ec9 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 28 || (
20eca 70 2d 3e 66 6c 61 67 73 32 20 26 20 45 50 32 5f p->flags2 & EP2_
20ecb 4d 61 6c 6c 6f 63 65 64 54 6f 6b 65 6e 29 3d 3d MallocedToken)==
20ecc 30 20 29 3b 0a 20 20 20 20 70 2d 3e 6f 70 20 3d 0 );. p->op =
20ecd 20 54 4b 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 TK_INTEGER;.
20ece 20 70 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f p->flags |= EP_
20ecf 49 6e 74 56 61 6c 75 65 3b 0a 20 20 20 20 70 2d IntValue;. p-
20ed0 3e 75 2e 69 56 61 6c 75 65 20 3d 20 2a 70 56 61 >u.iValue = *pVa
20ed1 6c 75 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 lue;. }. retur
20ed2 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 n rc;.}../*.** R
20ed3 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 74 68 eturn TRUE if th
20ed4 65 20 67 69 76 65 6e 20 73 74 72 69 6e 67 20 69 e given string i
20ed5 73 20 61 20 72 6f 77 2d 69 64 20 63 6f 6c 75 6d s a row-id colum
20ed6 6e 20 6e 61 6d 65 2e 0a 2a 2f 0a 53 51 4c 49 54 n name..*/.SQLIT
20ed7 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
20ed8 6c 69 74 65 33 49 73 52 6f 77 69 64 28 63 6f 6e lite3IsRowid(con
20ed9 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 69 st char *z){. i
20eda 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d f( sqlite3StrICm
20edb 70 28 7a 2c 20 22 5f 52 4f 57 49 44 5f 22 29 3d p(z, "_ROWID_")=
20edc 3d 30 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 =0 ) return 1;.
20edd 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 if( sqlite3StrI
20ede 43 6d 70 28 7a 2c 20 22 52 4f 57 49 44 22 29 3d Cmp(z, "ROWID")=
20edf 3d 30 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 =0 ) return 1;.
20ee0 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 if( sqlite3StrI
20ee1 43 6d 70 28 7a 2c 20 22 4f 49 44 22 29 3d 3d 30 Cmp(z, "OID")==0
20ee2 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 72 ) return 1;. r
20ee3 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a eturn 0;.}../*.*
20ee4 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 * Return true if
20ee5 20 77 65 20 61 72 65 20 61 62 6c 65 20 74 6f 20 we are able to
20ee6 74 68 65 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 the IN operator
20ee7 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 6f 6e 20 optimization on
20ee8 61 0a 2a 2a 20 71 75 65 72 79 20 6f 66 20 74 68 a.** query of th
20ee9 65 20 66 6f 72 6d 0a 2a 2a 0a 2a 2a 20 20 20 20 e form.**.**
20eea 20 20 20 78 20 49 4e 20 28 53 45 4c 45 43 54 20 x IN (SELECT
20eeb 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 57 68 65 72 65 ...).**.** Where
20eec 20 74 68 65 20 53 45 4c 45 43 54 2e 2e 2e 20 63 the SELECT... c
20eed 6c 61 75 73 65 20 69 73 20 61 73 20 73 70 65 63 lause is as spec
20eee 69 66 69 65 64 20 62 79 20 74 68 65 20 70 61 72 ified by the par
20eef 61 6d 65 74 65 72 20 74 6f 20 74 68 69 73 0a 2a ameter to this.*
20ef0 2a 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a * routine..**.**
20ef1 20 54 68 65 20 53 65 6c 65 63 74 20 6f 62 6a 65 The Select obje
20ef2 63 74 20 70 61 73 73 65 64 20 69 6e 20 68 61 73 ct passed in has
20ef3 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 70 72 already been pr
20ef4 65 70 72 6f 63 65 73 73 65 64 20 61 6e 64 20 6e eprocessed and n
20ef5 6f 0a 2a 2a 20 65 72 72 6f 72 73 20 68 61 76 65 o.** errors have
20ef6 20 62 65 65 6e 20 66 6f 75 6e 64 2e 0a 2a 2f 0a been found..*/.
20ef7 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
20ef8 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 73 74 61 MIT_SUBQUERY.sta
20ef9 74 69 63 20 69 6e 74 20 69 73 43 61 6e 64 69 64 tic int isCandid
20efa 61 74 65 46 6f 72 49 6e 4f 70 74 28 53 65 6c 65 ateForInOpt(Sele
20efb 63 74 20 2a 70 29 7b 0a 20 20 53 72 63 4c 69 73 ct *p){. SrcLis
20efc 74 20 2a 70 53 72 63 3b 0a 20 20 45 78 70 72 4c t *pSrc;. ExprL
20efd 69 73 74 20 2a 70 45 4c 69 73 74 3b 0a 20 20 54 ist *pEList;. T
20efe 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 69 66 able *pTab;. if
20eff 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 ( p==0 ) return
20f00 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0;
20f01 20 20 20 20 20 2f 2a 20 72 69 67 68 74 2d 68 61 /* right-ha
20f02 6e 64 20 73 69 64 65 20 6f 66 20 49 4e 20 69 73 nd side of IN is
20f03 20 53 45 4c 45 43 54 20 2a 2f 0a 20 20 69 66 28 SELECT */. if(
20f04 20 70 2d 3e 70 50 72 69 6f 72 20 29 20 72 65 74 p->pPrior ) ret
20f05 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20 urn 0;
20f06 20 20 20 20 2f 2a 20 4e 6f 74 20 61 20 63 6f 6d /* Not a com
20f07 70 6f 75 6e 64 20 53 45 4c 45 43 54 20 2a 2f 0a pound SELECT */.
20f08 20 20 69 66 28 20 70 2d 3e 73 65 6c 46 6c 61 67 if( p->selFlag
20f09 73 20 26 20 28 53 46 5f 44 69 73 74 69 6e 63 74 s & (SF_Distinct
20f0a 7c 53 46 5f 41 67 67 72 65 67 61 74 65 29 20 29 |SF_Aggregate) )
20f0b 7b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 {. testcase(
20f0c 28 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 28 (p->selFlags & (
20f0d 53 46 5f 44 69 73 74 69 6e 63 74 7c 53 46 5f 41 SF_Distinct|SF_A
20f0e 67 67 72 65 67 61 74 65 29 29 3d 3d 53 46 5f 44 ggregate))==SF_D
20f0f 69 73 74 69 6e 63 74 20 29 3b 0a 20 20 20 20 74 istinct );. t
20f10 65 73 74 63 61 73 65 28 20 28 70 2d 3e 73 65 6c estcase( (p->sel
20f11 46 6c 61 67 73 20 26 20 28 53 46 5f 44 69 73 74 Flags & (SF_Dist
20f12 69 6e 63 74 7c 53 46 5f 41 67 67 72 65 67 61 74 inct|SF_Aggregat
20f13 65 29 29 3d 3d 53 46 5f 41 67 67 72 65 67 61 74 e))==SF_Aggregat
20f14 65 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 e );. return
20f15 30 3b 20 2f 2a 20 4e 6f 20 44 49 53 54 49 4e 43 0; /* No DISTINC
20f16 54 20 6b 65 79 77 6f 72 64 20 61 6e 64 20 6e 6f T keyword and no
20f17 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 aggregate funct
20f18 69 6f 6e 73 20 2a 2f 0a 20 20 7d 0a 20 20 61 73 ions */. }. as
20f19 73 65 72 74 28 20 70 2d 3e 70 47 72 6f 75 70 42 sert( p->pGroupB
20f1a 79 3d 3d 30 20 29 3b 20 20 20 20 20 20 20 20 20 y==0 );
20f1b 20 20 20 20 20 2f 2a 20 48 61 73 20 6e 6f 20 47 /* Has no G
20f1c 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20 2a ROUP BY clause *
20f1d 2f 0a 20 20 69 66 28 20 70 2d 3e 70 4c 69 6d 69 /. if( p->pLimi
20f1e 74 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 t ) return 0;
20f1f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 /* Ha
20f20 73 20 6e 6f 20 4c 49 4d 49 54 20 63 6c 61 75 73 s no LIMIT claus
20f21 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 e */. assert( p
20f22 2d 3e 70 4f 66 66 73 65 74 3d 3d 30 20 29 3b 20 ->pOffset==0 );
20f23 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
20f24 20 4e 6f 20 4c 49 4d 49 54 20 6d 65 61 6e 73 20 No LIMIT means
20f25 6e 6f 20 4f 46 46 53 45 54 20 2a 2f 0a 20 20 69 no OFFSET */. i
20f26 66 28 20 70 2d 3e 70 57 68 65 72 65 20 29 20 72 f( p->pWhere ) r
20f27 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20 eturn 0;
20f28 20 20 20 20 20 20 2f 2a 20 48 61 73 20 6e 6f 20 /* Has no
20f29 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a WHERE clause */.
20f2a 20 20 70 53 72 63 20 3d 20 70 2d 3e 70 53 72 63 pSrc = p->pSrc
20f2b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 53 72 63 ;. assert( pSrc
20f2c 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 53 72 !=0 );. if( pSr
20f2d 63 2d 3e 6e 53 72 63 21 3d 31 20 29 20 72 65 74 c->nSrc!=1 ) ret
20f2e 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20 urn 0;
20f2f 2f 2a 20 53 69 6e 67 6c 65 20 74 65 72 6d 20 69 /* Single term i
20f30 6e 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f n FROM clause */
20f31 0a 20 20 69 66 28 20 70 53 72 63 2d 3e 61 5b 30 . if( pSrc->a[0
20f32 5d 2e 70 53 65 6c 65 63 74 20 29 20 72 65 74 75 ].pSelect ) retu
20f33 72 6e 20 30 3b 20 20 20 20 20 2f 2a 20 46 52 4f rn 0; /* FRO
20f34 4d 20 69 73 20 6e 6f 74 20 61 20 73 75 62 71 75 M is not a subqu
20f35 65 72 79 20 6f 72 20 76 69 65 77 20 2a 2f 0a 20 ery or view */.
20f36 20 70 54 61 62 20 3d 20 70 53 72 63 2d 3e 61 5b pTab = pSrc->a[
20f37 30 5d 2e 70 54 61 62 3b 0a 20 20 69 66 28 20 4e 0].pTab;. if( N
20f38 45 56 45 52 28 70 54 61 62 3d 3d 30 29 20 29 20 EVER(pTab==0) )
20f39 72 65 74 75 72 6e 20 30 3b 0a 20 20 61 73 73 65 return 0;. asse
20f3a 72 74 28 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 rt( pTab->pSelec
20f3b 74 3d 3d 30 20 29 3b 20 20 20 20 20 20 20 20 20 t==0 );
20f3c 20 20 20 2f 2a 20 46 52 4f 4d 20 63 6c 61 75 73 /* FROM claus
20f3d 65 20 69 73 20 6e 6f 74 20 61 20 76 69 65 77 20 e is not a view
20f3e 2a 2f 0a 20 20 69 66 28 20 49 73 56 69 72 74 75 */. if( IsVirtu
20f3f 61 6c 28 70 54 61 62 29 20 29 20 72 65 74 75 72 al(pTab) ) retur
20f40 6e 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 46 n 0; /* F
20f41 52 4f 4d 20 63 6c 61 75 73 65 20 6e 6f 74 20 61 ROM clause not a
20f42 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 2a virtual table *
20f43 2f 0a 20 20 70 45 4c 69 73 74 20 3d 20 70 2d 3e /. pEList = p->
20f44 70 45 4c 69 73 74 3b 0a 20 20 69 66 28 20 70 45 pEList;. if( pE
20f45 4c 69 73 74 2d 3e 6e 45 78 70 72 21 3d 31 20 29 List->nExpr!=1 )
20f46 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20 return 0;
20f47 20 2f 2a 20 4f 6e 65 20 63 6f 6c 75 6d 6e 20 69 /* One column i
20f48 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 n the result set
20f49 20 2a 2f 0a 20 20 69 66 28 20 70 45 4c 69 73 74 */. if( pEList
20f4a 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2d 3e 6f 70 ->a[0].pExpr->op
20f4b 21 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 20 72 65 !=TK_COLUMN ) re
20f4c 74 75 72 6e 20 30 3b 20 2f 2a 20 52 65 73 75 6c turn 0; /* Resul
20f4d 74 20 69 73 20 61 20 63 6f 6c 75 6d 6e 20 2a 2f t is a column */
20f4e 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 . return 1;.}.#
20f4f 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
20f50 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 20 2a 2f OMIT_SUBQUERY */
20f51 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e ../*.** This fun
20f52 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 62 79 ction is used by
20f53 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 the implementat
20f54 69 6f 6e 20 6f 66 20 74 68 65 20 49 4e 20 28 2e ion of the IN (.
20f55 2e 2e 29 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2a ..) operator..**
20f56 20 49 74 27 73 20 6a 6f 62 20 69 73 20 74 6f 20 It's job is to
20f57 66 69 6e 64 20 6f 72 20 63 72 65 61 74 65 20 61 find or create a
20f58 20 62 2d 74 72 65 65 20 73 74 72 75 63 74 75 72 b-tree structur
20f59 65 20 74 68 61 74 20 6d 61 79 20 62 65 20 75 73 e that may be us
20f5a 65 64 0a 2a 2a 20 65 69 74 68 65 72 20 74 6f 20 ed.** either to
20f5b 74 65 73 74 20 66 6f 72 20 6d 65 6d 62 65 72 73 test for members
20f5c 68 69 70 20 6f 66 20 74 68 65 20 28 2e 2e 2e 29 hip of the (...)
20f5d 20 73 65 74 20 6f 72 20 74 6f 20 69 74 65 72 61 set or to itera
20f5e 74 65 20 74 68 72 6f 75 67 68 0a 2a 2a 20 69 74 te through.** it
20f5f 73 20 6d 65 6d 62 65 72 73 2c 20 73 6b 69 70 70 s members, skipp
20f60 69 6e 67 20 64 75 70 6c 69 63 61 74 65 73 2e 0a ing duplicates..
20f61 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 64 65 78 20 **.** The index
20f62 6f 66 20 74 68 65 20 63 75 72 73 6f 72 20 6f 70 of the cursor op
20f63 65 6e 65 64 20 6f 6e 20 74 68 65 20 62 2d 74 72 ened on the b-tr
20f64 65 65 20 28 64 61 74 61 62 61 73 65 20 74 61 62 ee (database tab
20f65 6c 65 2c 20 64 61 74 61 62 61 73 65 20 69 6e 64 le, database ind
20f66 65 78 20 0a 2a 2a 20 6f 72 20 65 70 68 65 72 6d ex .** or epherm
20f67 61 6c 20 74 61 62 6c 65 29 20 69 73 20 73 74 6f al table) is sto
20f68 72 65 64 20 69 6e 20 70 58 2d 3e 69 54 61 62 6c red in pX->iTabl
20f69 65 20 62 65 66 6f 72 65 20 74 68 69 73 20 66 75 e before this fu
20f6a 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 2e 0a nction returns..
20f6b 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 ** The returned
20f6c 76 61 6c 75 65 20 6f 66 20 74 68 69 73 20 66 75 value of this fu
20f6d 6e 63 74 69 6f 6e 20 69 6e 64 69 63 61 74 65 73 nction indicates
20f6e 20 74 68 65 20 62 2d 74 72 65 65 20 74 79 70 65 the b-tree type
20f6f 2c 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a , as follows:.**
20f70 0a 2a 2a 20 20 20 49 4e 5f 49 4e 44 45 58 5f 52 .** IN_INDEX_R
20f71 4f 57 49 44 20 2d 20 54 68 65 20 63 75 72 73 6f OWID - The curso
20f72 72 20 77 61 73 20 6f 70 65 6e 65 64 20 6f 6e 20 r was opened on
20f73 61 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 a database table
20f74 2e 0a 2a 2a 20 20 20 49 4e 5f 49 4e 44 45 58 5f ..** IN_INDEX_
20f75 49 4e 44 45 58 20 2d 20 54 68 65 20 63 75 72 73 INDEX - The curs
20f76 6f 72 20 77 61 73 20 6f 70 65 6e 65 64 20 6f 6e or was opened on
20f77 20 61 20 64 61 74 61 62 61 73 65 20 69 6e 64 65 a database inde
20f78 78 2e 0a 2a 2a 20 20 20 49 4e 5f 49 4e 44 45 58 x..** IN_INDEX
20f79 5f 45 50 48 20 2d 20 20 20 54 68 65 20 63 75 72 _EPH - The cur
20f7a 73 6f 72 20 77 61 73 20 6f 70 65 6e 65 64 20 6f sor was opened o
20f7b 6e 20 61 20 73 70 65 63 69 61 6c 6c 79 20 63 72 n a specially cr
20f7c 65 61 74 65 64 20 61 6e 64 0a 2a 2a 20 20 20 20 eated and.**
20f7d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20f7e 70 6f 70 75 6c 61 74 65 64 20 65 70 68 65 72 65 populated ephere
20f7f 6d 61 6c 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a mal table..**.**
20f80 20 41 6e 20 65 78 69 73 74 69 6e 67 20 62 2d 74 An existing b-t
20f81 72 65 65 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 ree may only be
20f82 75 73 65 64 20 69 66 20 74 68 65 20 53 45 4c 45 used if the SELE
20f83 43 54 20 69 73 20 6f 66 20 74 68 65 20 73 69 6d CT is of the sim
20f84 70 6c 65 0a 2a 2a 20 66 6f 72 6d 3a 0a 2a 2a 0a ple.** form:.**.
20f85 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20 3c 63 ** SELECT <c
20f86 6f 6c 75 6d 6e 3e 20 46 52 4f 4d 20 3c 74 61 62 olumn> FROM <tab
20f87 6c 65 3e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 le>.**.** If the
20f88 20 70 72 4e 6f 74 46 6f 75 6e 64 20 70 61 72 61 prNotFound para
20f89 6d 65 74 65 72 20 69 73 20 30 2c 20 74 68 65 6e meter is 0, then
20f8a 20 74 68 65 20 62 2d 74 72 65 65 20 77 69 6c 6c the b-tree will
20f8b 20 62 65 20 75 73 65 64 20 74 6f 20 69 74 65 72 be used to iter
20f8c 61 74 65 0a 2a 2a 20 74 68 72 6f 75 67 68 20 74 ate.** through t
20f8d 68 65 20 73 65 74 20 6d 65 6d 62 65 72 73 2c 20 he set members,
20f8e 73 6b 69 70 70 69 6e 67 20 61 6e 79 20 64 75 70 skipping any dup
20f8f 6c 69 63 61 74 65 73 2e 20 49 6e 20 74 68 69 73 licates. In this
20f90 20 63 61 73 65 20 61 6e 0a 2a 2a 20 65 70 68 65 case an.** ephe
20f91 72 65 6d 61 6c 20 74 61 62 6c 65 20 6d 75 73 74 remal table must
20f92 20 62 65 20 75 73 65 64 20 75 6e 6c 65 73 73 20 be used unless
20f93 74 68 65 20 73 65 6c 65 63 74 65 64 20 3c 63 6f the selected <co
20f94 6c 75 6d 6e 3e 20 69 73 20 67 75 61 72 61 6e 74 lumn> is guarant
20f95 65 65 64 0a 2a 2a 20 74 6f 20 62 65 20 75 6e 69 eed.** to be uni
20f96 71 75 65 20 2d 20 65 69 74 68 65 72 20 62 65 63 que - either bec
20f97 61 75 73 65 20 69 74 20 69 73 20 61 6e 20 49 4e ause it is an IN
20f98 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 TEGER PRIMARY KE
20f99 59 20 6f 72 20 69 74 0a 2a 2a 20 68 61 73 20 61 Y or it.** has a
20f9a 20 55 4e 49 51 55 45 20 63 6f 6e 73 74 72 61 69 UNIQUE constrai
20f9b 6e 74 20 6f 72 20 55 4e 49 51 55 45 20 69 6e 64 nt or UNIQUE ind
20f9c 65 78 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 ex..**.** If the
20f9d 20 70 72 4e 6f 74 46 6f 75 6e 64 20 70 61 72 61 prNotFound para
20f9e 6d 65 74 65 72 20 69 73 20 6e 6f 74 20 30 2c 20 meter is not 0,
20f9f 74 68 65 6e 20 74 68 65 20 62 2d 74 72 65 65 20 then the b-tree
20fa0 77 69 6c 6c 20 62 65 20 75 73 65 64 20 0a 2a 2a will be used .**
20fa1 20 66 6f 72 20 66 61 73 74 20 73 65 74 20 6d 65 for fast set me
20fa2 6d 62 65 72 73 68 69 70 20 74 65 73 74 73 2e 20 mbership tests.
20fa3 49 6e 20 74 68 69 73 20 63 61 73 65 20 61 6e 20 In this case an
20fa4 65 70 68 65 72 65 6d 61 6c 20 74 61 62 6c 65 20 epheremal table
20fa5 6d 75 73 74 20 0a 2a 2a 20 62 65 20 75 73 65 64 must .** be used
20fa6 20 75 6e 6c 65 73 73 20 3c 63 6f 6c 75 6d 6e 3e unless <column>
20fa7 20 69 73 20 61 6e 20 49 4e 54 45 47 45 52 20 50 is an INTEGER P
20fa8 52 49 4d 41 52 59 20 4b 45 59 20 6f 72 20 61 6e RIMARY KEY or an
20fa9 20 69 6e 64 65 78 20 63 61 6e 20 0a 2a 2a 20 62 index can .** b
20faa 65 20 66 6f 75 6e 64 20 77 69 74 68 20 3c 63 6f e found with <co
20fab 6c 75 6d 6e 3e 20 61 73 20 69 74 73 20 6c 65 66 lumn> as its lef
20fac 74 2d 6d 6f 73 74 20 63 6f 6c 75 6d 6e 2e 0a 2a t-most column..*
20fad 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 62 2d *.** When the b-
20fae 74 72 65 65 20 69 73 20 62 65 69 6e 67 20 75 73 tree is being us
20faf 65 64 20 66 6f 72 20 6d 65 6d 62 65 72 73 68 69 ed for membershi
20fb0 70 20 74 65 73 74 73 2c 20 74 68 65 20 63 61 6c p tests, the cal
20fb1 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a ling function.**
20fb2 20 6e 65 65 64 73 20 74 6f 20 6b 6e 6f 77 20 77 needs to know w
20fb3 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 hether or not th
20fb4 65 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 e structure cont
20fb5 61 69 6e 73 20 61 6e 20 53 51 4c 20 4e 55 4c 4c ains an SQL NULL
20fb6 20 0a 2a 2a 20 76 61 6c 75 65 20 69 6e 20 6f 72 .** value in or
20fb7 64 65 72 20 74 6f 20 63 6f 72 72 65 63 74 6c 79 der to correctly
20fb8 20 65 76 61 6c 75 61 74 65 20 65 78 70 72 65 73 evaluate expres
20fb9 73 69 6f 6e 73 20 6c 69 6b 65 20 22 58 20 49 4e sions like "X IN
20fba 20 28 59 2c 20 5a 29 22 2e 0a 2a 2a 20 49 66 20 (Y, Z)"..** If
20fbb 74 68 65 72 65 20 69 73 20 61 20 63 68 61 6e 63 there is a chanc
20fbc 65 20 74 68 61 74 20 74 68 65 20 62 2d 74 72 65 e that the b-tre
20fbd 65 20 6d 69 67 68 74 20 63 6f 6e 74 61 69 6e 20 e might contain
20fbe 61 20 4e 55 4c 4c 20 76 61 6c 75 65 20 61 74 0a a NULL value at.
20fbf 2a 2a 20 72 75 6e 74 69 6d 65 2c 20 74 68 65 6e ** runtime, then
20fc0 20 61 20 72 65 67 69 73 74 65 72 20 69 73 20 61 a register is a
20fc1 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 74 68 65 llocated and the
20fc2 20 72 65 67 69 73 74 65 72 20 6e 75 6d 62 65 72 register number
20fc3 20 77 72 69 74 74 65 6e 0a 2a 2a 20 74 6f 20 2a written.** to *
20fc4 70 72 4e 6f 74 46 6f 75 6e 64 2e 20 49 66 20 74 prNotFound. If t
20fc5 68 65 72 65 20 69 73 20 6e 6f 20 63 68 61 6e 63 here is no chanc
20fc6 65 20 74 68 61 74 20 74 68 65 20 62 2d 74 72 65 e that the b-tre
20fc7 65 20 63 6f 6e 74 61 69 6e 73 20 61 0a 2a 2a 20 e contains a.**
20fc8 4e 55 4c 4c 20 76 61 6c 75 65 2c 20 74 68 65 6e NULL value, then
20fc9 20 2a 70 72 4e 6f 74 46 6f 75 6e 64 20 69 73 20 *prNotFound is
20fca 6c 65 66 74 20 75 6e 63 68 61 6e 67 65 64 2e 0a left unchanged..
20fcb 2a 2a 0a 2a 2a 20 49 66 20 61 20 72 65 67 69 73 **.** If a regis
20fcc 74 65 72 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 ter is allocated
20fcd 20 61 6e 64 20 69 74 73 20 6c 6f 63 61 74 69 6f and its locatio
20fce 6e 20 73 74 6f 72 65 64 20 69 6e 20 2a 70 72 4e n stored in *prN
20fcf 6f 74 46 6f 75 6e 64 2c 20 74 68 65 6e 0a 2a 2a otFound, then.**
20fd0 20 69 74 73 20 69 6e 69 74 69 61 6c 20 76 61 6c its initial val
20fd1 75 65 20 69 73 20 4e 55 4c 4c 2e 20 49 66 20 74 ue is NULL. If t
20fd2 68 65 20 62 2d 74 72 65 65 20 64 6f 65 73 20 6e he b-tree does n
20fd3 6f 74 20 72 65 6d 61 69 6e 20 63 6f 6e 73 74 61 ot remain consta
20fd4 6e 74 0a 2a 2a 20 66 6f 72 20 74 68 65 20 64 75 nt.** for the du
20fd5 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 71 75 ration of the qu
20fd6 65 72 79 20 28 69 2e 65 2e 20 74 68 65 20 53 45 ery (i.e. the SE
20fd7 4c 45 43 54 20 74 68 61 74 20 67 65 6e 65 72 61 LECT that genera
20fd8 74 65 73 20 74 68 65 20 62 2d 74 72 65 65 0a 2a tes the b-tree.*
20fd9 2a 20 69 73 20 61 20 63 6f 72 72 65 6c 61 74 65 * is a correlate
20fda 64 20 73 75 62 71 75 65 72 79 29 20 74 68 65 6e d subquery) then
20fdb 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 the value of th
20fdc 65 20 61 6c 6c 6f 63 61 74 65 64 20 72 65 67 69 e allocated regi
20fdd 73 74 65 72 20 69 73 0a 2a 2a 20 72 65 73 65 74 ster is.** reset
20fde 20 74 6f 20 4e 55 4c 4c 20 65 61 63 68 20 74 69 to NULL each ti
20fdf 6d 65 20 74 68 65 20 62 2d 74 72 65 65 20 69 73 me the b-tree is
20fe0 20 72 65 70 6f 70 75 6c 61 74 65 64 2e 20 54 68 repopulated. Th
20fe1 69 73 20 61 6c 6c 6f 77 73 20 74 68 65 0a 2a 2a is allows the.**
20fe2 20 63 61 6c 6c 65 72 20 74 6f 20 75 73 65 20 76 caller to use v
20fe3 64 62 65 20 63 6f 64 65 20 65 71 75 69 76 61 6c dbe code equival
20fe4 65 6e 74 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f ent to the follo
20fe5 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 69 66 wing:.**.** if
20fe6 28 20 72 65 67 69 73 74 65 72 3d 3d 4e 55 4c 4c ( register==NULL
20fe7 20 29 7b 0a 2a 2a 20 20 20 20 20 68 61 73 5f 6e ){.** has_n
20fe8 75 6c 6c 20 3d 20 3c 74 65 73 74 20 69 66 20 64 ull = <test if d
20fe9 61 74 61 20 73 74 72 75 63 74 75 72 65 20 63 6f ata structure co
20fea 6e 74 61 69 6e 73 20 6e 75 6c 6c 3e 0a 2a 2a 20 ntains null>.**
20feb 20 20 20 20 72 65 67 69 73 74 65 72 20 3d 20 31 register = 1
20fec 0a 2a 2a 20 20 20 7d 0a 2a 2a 0a 2a 2a 20 69 6e .** }.**.** in
20fed 20 6f 72 64 65 72 20 74 6f 20 61 76 6f 69 64 20 order to avoid
20fee 72 75 6e 6e 69 6e 67 20 74 68 65 20 3c 74 65 73 running the <tes
20fef 74 20 69 66 20 64 61 74 61 20 73 74 72 75 63 74 t if data struct
20ff0 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 6e 75 6c ure contains nul
20ff1 6c 3e 0a 2a 2a 20 74 65 73 74 20 6d 6f 72 65 20 l>.** test more
20ff2 6f 66 74 65 6e 20 74 68 61 6e 20 69 73 20 6e 65 often than is ne
20ff3 63 65 73 73 61 72 79 2e 0a 2a 2f 0a 23 69 66 6e cessary..*/.#ifn
20ff4 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
20ff5 53 55 42 51 55 45 52 59 0a 53 51 4c 49 54 45 5f SUBQUERY.SQLITE_
20ff6 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
20ff7 74 65 33 46 69 6e 64 49 6e 49 6e 64 65 78 28 50 te3FindInIndex(P
20ff8 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 arse *pParse, Ex
20ff9 70 72 20 2a 70 58 2c 20 69 6e 74 20 2a 70 72 4e pr *pX, int *prN
20ffa 6f 74 46 6f 75 6e 64 29 7b 0a 20 20 53 65 6c 65 otFound){. Sele
20ffb 63 74 20 2a 70 3b 20 20 20 20 20 20 20 20 20 20 ct *p;
20ffc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20ffd 20 20 2f 2a 20 53 45 4c 45 43 54 20 74 6f 20 74 /* SELECT to t
20ffe 68 65 20 72 69 67 68 74 20 6f 66 20 49 4e 20 6f he right of IN o
20fff 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20 69 6e 74 perator */. int
21000 20 65 54 79 70 65 20 3d 20 30 3b 20 20 20 20 20 eType = 0;
21001 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21002 20 20 20 2f 2a 20 54 79 70 65 20 6f 66 20 52 48 /* Type of RH
21003 53 20 74 61 62 6c 65 2e 20 49 4e 5f 49 4e 44 45 S table. IN_INDE
21004 58 5f 2a 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 X_* */. int iTa
21005 62 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 b = pParse->nTab
21006 2b 2b 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f ++; /
21007 2a 20 43 75 72 73 6f 72 20 6f 66 20 74 68 65 20 * Cursor of the
21008 52 48 53 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 RHS table */. i
21009 6e 74 20 6d 75 73 74 42 65 55 6e 69 71 75 65 20 nt mustBeUnique
2100a 3d 20 28 70 72 4e 6f 74 46 6f 75 6e 64 3d 3d 30 = (prNotFound==0
2100b 29 3b 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 ); /* True if
2100c 52 48 53 20 6d 75 73 74 20 62 65 20 75 6e 69 71 RHS must be uniq
2100d 75 65 20 2a 2f 0a 0a 20 20 2f 2a 20 43 68 65 63 ue */.. /* Chec
2100e 6b 20 74 6f 20 73 65 65 20 69 66 20 61 6e 20 65 k to see if an e
2100f 78 69 73 74 69 6e 67 20 74 61 62 6c 65 20 6f 72 xisting table or
21010 20 69 6e 64 65 78 20 63 61 6e 20 62 65 20 75 73 index can be us
21011 65 64 20 74 6f 0a 20 20 2a 2a 20 73 61 74 69 73 ed to. ** satis
21012 66 79 20 74 68 65 20 71 75 65 72 79 2e 20 20 54 fy the query. T
21013 68 69 73 20 69 73 20 70 72 65 66 65 72 61 62 6c his is preferabl
21014 65 20 74 6f 20 67 65 6e 65 72 61 74 69 6e 67 20 e to generating
21015 61 20 6e 65 77 20 0a 20 20 2a 2a 20 65 70 68 65 a new . ** ephe
21016 6d 65 72 61 6c 20 74 61 62 6c 65 2e 0a 20 20 2a meral table.. *
21017 2f 0a 20 20 70 20 3d 20 28 45 78 70 72 48 61 73 /. p = (ExprHas
21018 50 72 6f 70 65 72 74 79 28 70 58 2c 20 45 50 5f Property(pX, EP_
21019 78 49 73 53 65 6c 65 63 74 29 20 3f 20 70 58 2d xIsSelect) ? pX-
2101a 3e 78 2e 70 53 65 6c 65 63 74 20 3a 20 30 29 3b >x.pSelect : 0);
2101b 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 50 . if( ALWAYS(pP
2101c 61 72 73 65 2d 3e 6e 45 72 72 3d 3d 30 29 20 26 arse->nErr==0) &
2101d 26 20 69 73 43 61 6e 64 69 64 61 74 65 46 6f 72 & isCandidateFor
2101e 49 6e 4f 70 74 28 70 29 20 29 7b 0a 20 20 20 20 InOpt(p) ){.
2101f 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 sqlite3 *db = pP
21020 61 72 73 65 2d 3e 64 62 3b 20 20 20 20 20 20 20 arse->db;
21021 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 /* Databa
21022 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f se connection */
21023 0a 20 20 20 20 45 78 70 72 20 2a 70 45 78 70 72 . Expr *pExpr
21024 20 3d 20 70 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b = p->pEList->a[
21025 30 5d 2e 70 45 78 70 72 3b 20 20 20 2f 2a 20 45 0].pExpr; /* E
21026 78 70 72 65 73 73 69 6f 6e 20 3c 63 6f 6c 75 6d xpression <colum
21027 6e 3e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 43 n> */. int iC
21028 6f 6c 20 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c ol = pExpr->iCol
21029 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 umn;
2102a 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 6f 6c /* Index of col
2102b 75 6d 6e 20 3c 63 6f 6c 75 6d 6e 3e 20 2a 2f 0a umn <column> */.
2102c 20 20 20 20 56 64 62 65 20 2a 76 20 3d 20 73 71 Vdbe *v = sq
2102d 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61 lite3GetVdbe(pPa
2102e 72 73 65 29 3b 20 20 20 20 20 20 2f 2a 20 56 69 rse); /* Vi
2102f 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 62 65 rtual machine be
21030 69 6e 67 20 63 6f 64 65 64 20 2a 2f 0a 20 20 20 ing coded */.
21031 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 Table *pTab = p
21032 2d 3e 70 53 72 63 2d 3e 61 5b 30 5d 2e 70 54 61 ->pSrc->a[0].pTa
21033 62 3b 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 b; /* Table
21034 20 3c 74 61 62 6c 65 3e 2e 20 2a 2f 0a 20 20 20 <table>. */.
21035 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 int iDb;
21036 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21037 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 /* Datab
21038 61 73 65 20 69 64 78 20 66 6f 72 20 70 54 61 62 ase idx for pTab
21039 20 2a 2f 0a 20 20 20 0a 20 20 20 20 2f 2a 20 43 */. . /* C
2103a 6f 64 65 20 61 6e 20 4f 50 5f 56 65 72 69 66 79 ode an OP_Verify
2103b 43 6f 6f 6b 69 65 20 61 6e 64 20 4f 50 5f 54 61 Cookie and OP_Ta
2103c 62 6c 65 4c 6f 63 6b 20 66 6f 72 20 3c 74 61 62 bleLock for <tab
2103d 6c 65 3e 2e 20 2a 2f 0a 20 20 20 20 69 44 62 20 le>. */. iDb
2103e 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 = sqlite3SchemaT
2103f 6f 49 6e 64 65 78 28 64 62 2c 20 70 54 61 62 2d oIndex(db, pTab-
21040 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 73 >pSchema);. s
21041 71 6c 69 74 65 33 43 6f 64 65 56 65 72 69 66 79 qlite3CodeVerify
21042 53 63 68 65 6d 61 28 70 50 61 72 73 65 2c 20 69 Schema(pParse, i
21043 44 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 Db);. sqlite3
21044 54 61 62 6c 65 4c 6f 63 6b 28 70 50 61 72 73 65 TableLock(pParse
21045 2c 20 69 44 62 2c 20 70 54 61 62 2d 3e 74 6e 75 , iDb, pTab->tnu
21046 6d 2c 20 30 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d m, 0, pTab->zNam
21047 65 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 69 73 e);.. /* This
21048 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6f 6e 6c function is onl
21049 79 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 74 77 y called from tw
2104a 6f 20 70 6c 61 63 65 73 2e 20 49 6e 20 62 6f 74 o places. In bot
2104b 68 20 63 61 73 65 73 20 74 68 65 20 76 64 62 65 h cases the vdbe
2104c 0a 20 20 20 20 2a 2a 20 68 61 73 20 61 6c 72 65 . ** has alre
2104d 61 64 79 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 ady been allocat
2104e 65 64 2e 20 53 6f 20 61 73 73 75 6d 65 20 73 71 ed. So assume sq
2104f 6c 69 74 65 33 47 65 74 56 64 62 65 28 29 20 69 lite3GetVdbe() i
21050 73 20 61 6c 77 61 79 73 0a 20 20 20 20 2a 2a 20 s always. **
21051 73 75 63 63 65 73 73 66 75 6c 20 68 65 72 65 2e successful here.
21052 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 . */. asse
21053 72 74 28 76 29 3b 0a 20 20 20 20 69 66 28 20 69 rt(v);. if( i
21054 43 6f 6c 3c 30 20 29 7b 0a 20 20 20 20 20 20 69 Col<0 ){. i
21055 6e 74 20 69 4d 65 6d 20 3d 20 2b 2b 70 50 61 72 nt iMem = ++pPar
21056 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 se->nMem;.
21057 69 6e 74 20 69 41 64 64 72 3b 0a 0a 20 20 20 20 int iAddr;..
21058 20 20 69 41 64 64 72 20 3d 20 73 71 6c 69 74 65 iAddr = sqlite
21059 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 3VdbeAddOp1(v, O
2105a 50 5f 49 66 2c 20 69 4d 65 6d 29 3b 0a 20 20 20 P_If, iMem);.
2105b 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
2105c 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 dOp2(v, OP_Integ
2105d 65 72 2c 20 31 2c 20 69 4d 65 6d 29 3b 0a 0a 20 er, 1, iMem);..
2105e 20 20 20 20 20 73 71 6c 69 74 65 33 4f 70 65 6e sqlite3Open
2105f 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 69 54 Table(pParse, iT
21060 61 62 2c 20 69 44 62 2c 20 70 54 61 62 2c 20 4f ab, iDb, pTab, O
21061 50 5f 4f 70 65 6e 52 65 61 64 29 3b 0a 20 20 20 P_OpenRead);.
21062 20 20 20 65 54 79 70 65 20 3d 20 49 4e 5f 49 4e eType = IN_IN
21063 44 45 58 5f 52 4f 57 49 44 3b 0a 0a 20 20 20 20 DEX_ROWID;..
21064 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d sqlite3VdbeJum
21065 70 48 65 72 65 28 76 2c 20 69 41 64 64 72 29 3b pHere(v, iAddr);
21066 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
21067 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 20 20 Index *pIdx;
21068 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21069 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74 /* Iterat
2106a 6f 72 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 0a or variable */..
2106b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6c /* The col
2106c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 lation sequence
2106d 75 73 65 64 20 62 79 20 74 68 65 20 63 6f 6d 70 used by the comp
2106e 61 72 69 73 6f 6e 2e 20 49 66 20 61 6e 20 69 6e arison. If an in
2106f 64 65 78 20 69 73 20 74 6f 0a 20 20 20 20 20 20 dex is to.
21070 2a 2a 20 62 65 20 75 73 65 64 20 69 6e 20 70 6c ** be used in pl
21071 61 63 65 20 6f 66 20 61 20 74 65 6d 70 2d 74 61 ace of a temp-ta
21072 62 6c 65 2c 20 69 74 20 6d 75 73 74 20 62 65 20 ble, it must be
21073 6f 72 64 65 72 65 64 20 61 63 63 6f 72 64 69 6e ordered accordin
21074 67 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 74 68 g. ** to th
21075 69 73 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 is collation seq
21076 75 65 6e 63 65 2e 20 20 2a 2f 0a 20 20 20 20 20 uence. */.
21077 20 43 6f 6c 6c 53 65 71 20 2a 70 52 65 71 20 3d CollSeq *pReq =
21078 20 73 71 6c 69 74 65 33 42 69 6e 61 72 79 43 6f sqlite3BinaryCo
21079 6d 70 61 72 65 43 6f 6c 6c 53 65 71 28 70 50 61 mpareCollSeq(pPa
2107a 72 73 65 2c 20 70 58 2d 3e 70 4c 65 66 74 2c 20 rse, pX->pLeft,
2107b 70 45 78 70 72 29 3b 0a 0a 20 20 20 20 20 20 2f pExpr);.. /
2107c 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 65 * Check that the
2107d 20 61 66 66 69 6e 69 74 79 20 74 68 61 74 20 77 affinity that w
2107e 69 6c 6c 20 62 65 20 75 73 65 64 20 74 6f 20 70 ill be used to p
2107f 65 72 66 6f 72 6d 20 74 68 65 20 0a 20 20 20 20 erform the .
21080 20 20 2a 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 20 ** comparison
21081 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 is the same as t
21082 68 65 20 61 66 66 69 6e 69 74 79 20 6f 66 20 74 he affinity of t
21083 68 65 20 63 6f 6c 75 6d 6e 2e 20 49 66 0a 20 20 he column. If.
21084 20 20 20 20 2a 2a 20 69 74 20 69 73 20 6e 6f 74 ** it is not
21085 2c 20 69 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 , it is not poss
21086 69 62 6c 65 20 74 6f 20 75 73 65 20 61 6e 79 20 ible to use any
21087 69 6e 64 65 78 2e 0a 20 20 20 20 20 20 2a 2f 0a index.. */.
21088 20 20 20 20 20 20 63 68 61 72 20 61 66 66 20 3d char aff =
21089 20 63 6f 6d 70 61 72 69 73 6f 6e 41 66 66 69 6e comparisonAffin
2108a 69 74 79 28 70 58 29 3b 0a 20 20 20 20 20 20 69 ity(pX);. i
2108b 6e 74 20 61 66 66 69 6e 69 74 79 5f 6f 6b 20 3d nt affinity_ok =
2108c 20 28 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f (pTab->aCol[iCo
2108d 6c 5d 2e 61 66 66 69 6e 69 74 79 3d 3d 61 66 66 l].affinity==aff
2108e 7c 7c 61 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46 ||aff==SQLITE_AF
2108f 46 5f 4e 4f 4e 45 29 3b 0a 0a 20 20 20 20 20 20 F_NONE);..
21090 66 6f 72 28 70 49 64 78 3d 70 54 61 62 2d 3e 70 for(pIdx=pTab->p
21091 49 6e 64 65 78 3b 20 70 49 64 78 20 26 26 20 65 Index; pIdx && e
21092 54 79 70 65 3d 3d 30 20 26 26 20 61 66 66 69 6e Type==0 && affin
21093 69 74 79 5f 6f 6b 3b 20 70 49 64 78 3d 70 49 64 ity_ok; pIdx=pId
21094 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 x->pNext){.
21095 20 20 20 69 66 28 20 28 70 49 64 78 2d 3e 61 69 if( (pIdx->ai
21096 43 6f 6c 75 6d 6e 5b 30 5d 3d 3d 69 43 6f 6c 29 Column[0]==iCol)
21097 0a 20 20 20 20 20 20 20 20 20 26 26 20 73 71 6c . && sql
21098 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28 ite3FindCollSeq(
21099 64 62 2c 20 45 4e 43 28 64 62 29 2c 20 70 49 64 db, ENC(db), pId
2109a 78 2d 3e 61 7a 43 6f 6c 6c 5b 30 5d 2c 20 30 29 x->azColl[0], 0)
2109b 3d 3d 70 52 65 71 0a 20 20 20 20 20 20 20 20 20 ==pReq.
2109c 26 26 20 28 21 6d 75 73 74 42 65 55 6e 69 71 75 && (!mustBeUniqu
2109d 65 20 7c 7c 20 28 70 49 64 78 2d 3e 6e 43 6f 6c e || (pIdx->nCol
2109e 75 6d 6e 3d 3d 31 20 26 26 20 70 49 64 78 2d 3e umn==1 && pIdx->
2109f 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e 65 onError!=OE_None
210a0 29 29 0a 20 20 20 20 20 20 20 20 29 7b 0a 20 20 )). ){.
210a1 20 20 20 20 20 20 20 20 69 6e 74 20 69 4d 65 6d int iMem
210a2 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 = ++pParse->nMe
210a3 6d 3b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 m;. int
210a4 20 69 41 64 64 72 3b 0a 20 20 20 20 20 20 20 20 iAddr;.
210a5 20 20 63 68 61 72 20 2a 70 4b 65 79 3b 0a 20 20 char *pKey;.
210a6 0a 20 20 20 20 20 20 20 20 20 20 70 4b 65 79 20 . pKey
210a7 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 = (char *)sqlite
210a8 33 49 6e 64 65 78 4b 65 79 69 6e 66 6f 28 70 50 3IndexKeyinfo(pP
210a9 61 72 73 65 2c 20 70 49 64 78 29 3b 0a 20 20 20 arse, pIdx);.
210aa 20 20 20 20 20 20 20 69 41 64 64 72 20 3d 20 73 iAddr = s
210ab 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 qlite3VdbeAddOp1
210ac 28 76 2c 20 4f 50 5f 49 66 2c 20 69 4d 65 6d 29 (v, OP_If, iMem)
210ad 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 ;. sqli
210ae 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c te3VdbeAddOp2(v,
210af 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 OP_Integer, 1,
210b0 69 4d 65 6d 29 3b 0a 20 20 0a 20 20 20 20 20 20 iMem);. .
210b1 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
210b2 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4f 70 65 6e ddOp4(v, OP_Open
210b3 52 65 61 64 2c 20 69 54 61 62 2c 20 70 49 64 78 Read, iTab, pIdx
210b4 2d 3e 74 6e 75 6d 2c 20 69 44 62 2c 0a 20 20 20 ->tnum, iDb,.
210b5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
210b6 20 20 20 20 20 20 20 20 20 20 20 20 70 4b 65 79 pKey
210b7 2c 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 ,P4_KEYINFO_HAND
210b8 4f 46 46 29 3b 0a 20 20 20 20 20 20 20 20 20 20 OFF);.
210b9 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 VdbeComment((v,
210ba 22 25 73 22 2c 20 70 49 64 78 2d 3e 7a 4e 61 6d "%s", pIdx->zNam
210bb 65 29 29 3b 0a 20 20 20 20 20 20 20 20 20 20 65 e));. e
210bc 54 79 70 65 20 3d 20 49 4e 5f 49 4e 44 45 58 5f Type = IN_INDEX_
210bd 49 4e 44 45 58 3b 0a 0a 20 20 20 20 20 20 20 20 INDEX;..
210be 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d sqlite3VdbeJum
210bf 70 48 65 72 65 28 76 2c 20 69 41 64 64 72 29 3b pHere(v, iAddr);
210c0 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 . if( p
210c1 72 4e 6f 74 46 6f 75 6e 64 20 26 26 20 21 70 54 rNotFound && !pT
210c2 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 6e ab->aCol[iCol].n
210c3 6f 74 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 20 20 otNull ){.
210c4 20 20 20 20 20 20 2a 70 72 4e 6f 74 46 6f 75 6e *prNotFoun
210c5 64 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d d = ++pParse->nM
210c6 65 6d 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a em;. }.
210c7 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
210c8 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 }. }. }.. i
210c9 66 28 20 65 54 79 70 65 3d 3d 30 20 29 7b 0a 20 f( eType==0 ){.
210ca 20 20 20 2f 2a 20 43 6f 75 6c 64 20 6e 6f 74 20 /* Could not
210cb 66 6f 75 6e 64 20 61 6e 20 65 78 69 73 74 69 6e found an existin
210cc 67 20 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 g able or index
210cd 74 6f 20 75 73 65 20 61 73 20 74 68 65 20 52 48 to use as the RH
210ce 53 20 62 2d 74 72 65 65 2e 0a 20 20 20 20 2a 2a S b-tree.. **
210cf 20 57 65 20 77 69 6c 6c 20 68 61 76 65 20 74 6f We will have to
210d0 20 67 65 6e 65 72 61 74 65 20 61 6e 20 65 70 68 generate an eph
210d1 65 6d 65 72 61 6c 20 74 61 62 6c 65 20 74 6f 20 emeral table to
210d2 64 6f 20 74 68 65 20 6a 6f 62 2e 0a 20 20 20 20 do the job..
210d3 2a 2f 0a 20 20 20 20 69 6e 74 20 72 4d 61 79 48 */. int rMayH
210d4 61 76 65 4e 75 6c 6c 20 3d 20 30 3b 0a 20 20 20 aveNull = 0;.
210d5 20 65 54 79 70 65 20 3d 20 49 4e 5f 49 4e 44 45 eType = IN_INDE
210d6 58 5f 45 50 48 3b 0a 20 20 20 20 69 66 28 20 70 X_EPH;. if( p
210d7 72 4e 6f 74 46 6f 75 6e 64 20 29 7b 0a 20 20 20 rNotFound ){.
210d8 20 20 20 2a 70 72 4e 6f 74 46 6f 75 6e 64 20 3d *prNotFound =
210d9 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 20 3d 20 rMayHaveNull =
210da 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a ++pParse->nMem;.
210db 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 58 }else if( pX
210dc 2d 3e 70 4c 65 66 74 2d 3e 69 43 6f 6c 75 6d 6e ->pLeft->iColumn
210dd 3c 30 20 26 26 20 21 45 78 70 72 48 61 73 41 6e <0 && !ExprHasAn
210de 79 50 72 6f 70 65 72 74 79 28 70 58 2c 20 45 50 yProperty(pX, EP
210df 5f 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20 _xIsSelect) ){.
210e0 20 20 20 20 20 65 54 79 70 65 20 3d 20 49 4e 5f eType = IN_
210e1 49 4e 44 45 58 5f 52 4f 57 49 44 3b 0a 20 20 20 INDEX_ROWID;.
210e2 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6f }. sqlite3Co
210e3 64 65 53 75 62 73 65 6c 65 63 74 28 70 50 61 72 deSubselect(pPar
210e4 73 65 2c 20 70 58 2c 20 72 4d 61 79 48 61 76 65 se, pX, rMayHave
210e5 4e 75 6c 6c 2c 20 65 54 79 70 65 3d 3d 49 4e 5f Null, eType==IN_
210e6 49 4e 44 45 58 5f 52 4f 57 49 44 29 3b 0a 20 20 INDEX_ROWID);.
210e7 7d 65 6c 73 65 7b 0a 20 20 20 20 70 58 2d 3e 69 }else{. pX->i
210e8 54 61 62 6c 65 20 3d 20 69 54 61 62 3b 0a 20 20 Table = iTab;.
210e9 7d 0a 20 20 72 65 74 75 72 6e 20 65 54 79 70 65 }. return eType
210ea 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a ;.}.#endif../*.*
210eb 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 * Generate code
210ec 66 6f 72 20 73 63 61 6c 61 72 20 73 75 62 71 75 for scalar subqu
210ed 65 72 69 65 73 20 75 73 65 64 20 61 73 20 61 6e eries used as an
210ee 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 61 expression.** a
210ef 6e 64 20 49 4e 20 6f 70 65 72 61 74 6f 72 73 2e nd IN operators.
210f0 20 20 45 78 61 6d 70 6c 65 73 3a 0a 2a 2a 0a 2a Examples:.**.*
210f1 2a 20 20 20 20 20 28 53 45 4c 45 43 54 20 61 20 * (SELECT a
210f2 46 52 4f 4d 20 62 29 20 20 20 20 20 20 20 20 20 FROM b)
210f3 20 2d 2d 20 73 75 62 71 75 65 72 79 0a 2a 2a 20 -- subquery.**
210f4 20 20 20 20 45 58 49 53 54 53 20 28 53 45 4c 45 EXISTS (SELE
210f5 43 54 20 61 20 46 52 4f 4d 20 62 29 20 20 20 2d CT a FROM b) -
210f6 2d 20 45 58 49 53 54 53 20 73 75 62 71 75 65 72 - EXISTS subquer
210f7 79 0a 2a 2a 20 20 20 20 20 78 20 49 4e 20 28 34 y.** x IN (4
210f8 2c 35 2c 31 31 29 20 20 20 20 20 20 20 20 20 20 ,5,11)
210f9 20 20 20 20 2d 2d 20 49 4e 20 6f 70 65 72 61 74 -- IN operat
210fa 6f 72 20 77 69 74 68 20 6c 69 73 74 20 6f 6e 20 or with list on
210fb 72 69 67 68 74 2d 68 61 6e 64 20 73 69 64 65 0a right-hand side.
210fc 2a 2a 20 20 20 20 20 78 20 49 4e 20 28 53 45 4c ** x IN (SEL
210fd 45 43 54 20 61 20 46 52 4f 4d 20 62 29 20 20 20 ECT a FROM b)
210fe 20 20 2d 2d 20 49 4e 20 6f 70 65 72 61 74 6f 72 -- IN operator
210ff 20 77 69 74 68 20 73 75 62 71 75 65 72 79 20 6f with subquery o
21100 6e 20 74 68 65 20 72 69 67 68 74 0a 2a 2a 0a 2a n the right.**.*
21101 2a 20 54 68 65 20 70 45 78 70 72 20 70 61 72 61 * The pExpr para
21102 6d 65 74 65 72 20 64 65 73 63 72 69 62 65 73 20 meter describes
21103 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 the expression t
21104 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 hat contains the
21105 20 49 4e 0a 2a 2a 20 6f 70 65 72 61 74 6f 72 20 IN.** operator
21106 6f 72 20 73 75 62 71 75 65 72 79 2e 0a 2a 2a 0a or subquery..**.
21107 2a 2a 20 49 66 20 70 61 72 61 6d 65 74 65 72 20 ** If parameter
21108 69 73 52 6f 77 69 64 20 69 73 20 6e 6f 6e 2d 7a isRowid is non-z
21109 65 72 6f 2c 20 74 68 65 6e 20 65 78 70 72 65 73 ero, then expres
2110a 73 69 6f 6e 20 70 45 78 70 72 20 69 73 20 67 75 sion pExpr is gu
2110b 61 72 61 6e 74 65 65 64 0a 2a 2a 20 74 6f 20 62 aranteed.** to b
2110c 65 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 3c e of the form "<
2110d 72 6f 77 69 64 3e 20 49 4e 20 28 3f 2c 20 3f 2c rowid> IN (?, ?,
2110e 20 3f 29 22 2c 20 77 68 65 72 65 20 3c 72 6f 77 ?)", where <row
2110f 69 64 3e 20 69 73 20 61 20 72 65 66 65 72 65 6e id> is a referen
21110 63 65 0a 2a 2a 20 74 6f 20 73 6f 6d 65 20 69 6e ce.** to some in
21111 74 65 67 65 72 20 6b 65 79 20 63 6f 6c 75 6d 6e teger key column
21112 20 6f 66 20 61 20 74 61 62 6c 65 20 42 2d 54 72 of a table B-Tr
21113 65 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 ee. In this case
21114 2c 20 75 73 65 20 61 6e 0a 2a 2a 20 69 6e 74 6b , use an.** intk
21115 65 79 20 42 2d 54 72 65 65 20 74 6f 20 73 74 6f ey B-Tree to sto
21116 72 65 20 74 68 65 20 73 65 74 20 6f 66 20 49 4e re the set of IN
21117 28 2e 2e 2e 29 20 76 61 6c 75 65 73 20 69 6e 73 (...) values ins
21118 74 65 61 64 20 6f 66 20 74 68 65 20 75 73 75 61 tead of the usua
21119 6c 0a 2a 2a 20 28 73 6c 6f 77 65 72 29 20 76 61 l.** (slower) va
2111a 72 69 61 62 6c 65 20 6c 65 6e 67 74 68 20 6b 65 riable length ke
2111b 79 73 20 42 2d 54 72 65 65 2e 0a 2a 2a 0a 2a 2a ys B-Tree..**.**
2111c 20 49 66 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c If rMayHaveNull
2111d 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 is non-zero, th
2111e 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 at means that th
2111f 65 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 61 e operation is a
21120 6e 20 49 4e 0a 2a 2a 20 28 6e 6f 74 20 61 20 53 n IN.** (not a S
21121 45 4c 45 43 54 20 6f 72 20 45 58 49 53 54 53 29 ELECT or EXISTS)
21122 20 61 6e 64 20 74 68 61 74 20 74 68 65 20 52 48 and that the RH
21123 53 20 6d 69 67 68 74 20 63 6f 6e 74 61 69 6e 73 S might contains
21124 20 4e 55 4c 4c 73 2e 0a 2a 2a 20 46 75 72 74 68 NULLs..** Furth
21125 65 72 6d 6f 72 65 2c 20 74 68 65 20 49 4e 20 69 ermore, the IN i
21126 73 20 69 6e 20 61 20 57 48 45 52 45 20 63 6c 61 s in a WHERE cla
21127 75 73 65 20 61 6e 64 20 74 68 61 74 20 77 65 20 use and that we
21128 72 65 61 6c 6c 79 20 77 61 6e 74 0a 2a 2a 20 74 really want.** t
21129 6f 20 69 74 65 72 61 74 65 20 6f 76 65 72 20 74 o iterate over t
2112a 68 65 20 52 48 53 20 6f 66 20 74 68 65 20 49 4e he RHS of the IN
2112b 20 6f 70 65 72 61 74 6f 72 20 69 6e 20 6f 72 64 operator in ord
2112c 65 72 20 74 6f 20 71 75 69 63 6b 6c 79 20 6c 6f er to quickly lo
2112d 63 61 74 65 0a 2a 2a 20 61 6c 6c 20 63 6f 72 72 cate.** all corr
2112e 65 73 70 6f 6e 64 69 6e 67 20 4c 48 53 20 65 6c esponding LHS el
2112f 65 6d 65 6e 74 73 2e 20 20 41 6c 6c 20 74 68 69 ements. All thi
21130 73 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 69 s routine does i
21131 73 20 69 6e 69 74 69 61 6c 69 7a 65 0a 2a 2a 20 s initialize.**
21132 74 68 65 20 72 65 67 69 73 74 65 72 20 67 69 76 the register giv
21133 65 6e 20 62 79 20 72 4d 61 79 48 61 76 65 4e 75 en by rMayHaveNu
21134 6c 6c 20 74 6f 20 4e 55 4c 4c 2e 20 20 43 61 6c ll to NULL. Cal
21135 6c 69 6e 67 20 72 6f 75 74 69 6e 65 73 20 77 69 ling routines wi
21136 6c 6c 20 74 61 6b 65 0a 2a 2a 20 63 61 72 65 20 ll take.** care
21137 6f 66 20 63 68 61 6e 67 69 6e 67 20 74 68 69 73 of changing this
21138 20 72 65 67 69 73 74 65 72 20 76 61 6c 75 65 20 register value
21139 74 6f 20 6e 6f 6e 2d 4e 55 4c 4c 20 69 66 20 74 to non-NULL if t
2113a 68 65 20 52 48 53 20 69 73 20 4e 55 4c 4c 2d 66 he RHS is NULL-f
2113b 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 72 4d ree..**.** If rM
2113c 61 79 48 61 76 65 4e 75 6c 6c 20 69 73 20 7a 65 ayHaveNull is ze
2113d 72 6f 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74 ro, that means t
2113e 68 61 74 20 74 68 65 20 73 75 62 71 75 65 72 79 hat the subquery
2113f 20 69 73 20 62 65 69 6e 67 20 75 73 65 64 0a 2a is being used.*
21140 2a 20 66 6f 72 20 6d 65 6d 62 65 72 73 68 69 70 * for membership
21141 20 74 65 73 74 69 6e 67 20 6f 6e 6c 79 2e 20 20 testing only.
21142 54 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 There is no need
21143 20 74 6f 20 69 6e 69 74 69 61 6c 69 7a 65 20 61 to initialize a
21144 6e 79 0a 2a 2a 20 72 65 67 69 73 74 65 72 73 20 ny.** registers
21145 74 6f 20 69 6e 64 69 63 61 74 65 20 74 68 65 20 to indicate the
21146 70 72 65 73 65 6e 73 65 20 6f 72 20 61 62 73 65 presense or abse
21147 6e 63 65 20 6f 66 20 4e 55 4c 4c 73 20 6f 6e 20 nce of NULLs on
21148 74 68 65 20 52 48 53 2e 0a 2a 2f 0a 23 69 66 6e the RHS..*/.#ifn
21149 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
2114a 53 55 42 51 55 45 52 59 0a 53 51 4c 49 54 45 5f SUBQUERY.SQLITE_
2114b 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
2114c 69 74 65 33 43 6f 64 65 53 75 62 73 65 6c 65 63 ite3CodeSubselec
2114d 74 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 t(. Parse *pPar
2114e 73 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 se, /*
2114f 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 Parsing context
21150 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 */. Expr *pExpr
21151 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 , /*
21152 54 68 65 20 49 4e 2c 20 53 45 4c 45 43 54 2c 20 The IN, SELECT,
21153 6f 72 20 45 58 49 53 54 53 20 6f 70 65 72 61 74 or EXISTS operat
21154 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 72 4d 61 79 or */. int rMay
21155 48 61 76 65 4e 75 6c 6c 2c 20 20 20 20 20 20 20 HaveNull,
21156 2f 2a 20 52 65 67 69 73 74 65 72 20 74 68 61 74 /* Register that
21157 20 72 65 63 6f 72 64 73 20 77 68 65 74 68 65 72 records whether
21158 20 4e 55 4c 4c 73 20 65 78 69 73 74 20 69 6e 20 NULLs exist in
21159 52 48 53 20 2a 2f 0a 20 20 69 6e 74 20 69 73 52 RHS */. int isR
2115a 6f 77 69 64 20 20 20 20 20 20 20 20 20 20 20 20 owid
2115b 20 2f 2a 20 49 66 20 74 72 75 65 2c 20 4c 48 53 /* If true, LHS
2115c 20 6f 66 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 of IN operator
2115d 69 73 20 61 20 72 6f 77 69 64 20 2a 2f 0a 29 7b is a rowid */.){
2115e 0a 20 20 69 6e 74 20 74 65 73 74 41 64 64 72 20 . int testAddr
2115f 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 = 0;
21160 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e /* On
21161 65 2d 74 69 6d 65 20 74 65 73 74 20 61 64 64 72 e-time test addr
21162 65 73 73 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 ess */. Vdbe *v
21163 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 = sqlite3GetVdb
21164 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 e(pParse);. if(
21165 20 4e 45 56 45 52 28 76 3d 3d 30 29 20 29 20 72 NEVER(v==0) ) r
21166 65 74 75 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33 eturn;. sqlite3
21167 45 78 70 72 43 61 63 68 65 50 75 73 68 28 70 50 ExprCachePush(pP
21168 61 72 73 65 29 3b 0a 0a 20 20 2f 2a 20 54 68 69 arse);.. /* Thi
21169 73 20 63 6f 64 65 20 6d 75 73 74 20 62 65 20 72 s code must be r
2116a 75 6e 20 69 6e 20 69 74 73 20 65 6e 74 69 72 65 un in its entire
2116b 74 79 20 65 76 65 72 79 20 74 69 6d 65 20 69 74 ty every time it
2116c 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 0a is encountered.
2116d 20 20 2a 2a 20 69 66 20 61 6e 79 20 6f 66 20 74 ** if any of t
2116e 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 he following is
2116f 74 72 75 65 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 true:. **. **
21170 20 20 20 2a 20 20 54 68 65 20 72 69 67 68 74 2d * The right-
21171 68 61 6e 64 20 73 69 64 65 20 69 73 20 61 20 63 hand side is a c
21172 6f 72 72 65 6c 61 74 65 64 20 73 75 62 71 75 65 orrelated subque
21173 72 79 0a 20 20 2a 2a 20 20 20 20 2a 20 20 54 68 ry. ** * Th
21174 65 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69 64 e right-hand sid
21175 65 20 69 73 20 61 6e 20 65 78 70 72 65 73 73 69 e is an expressi
21176 6f 6e 20 6c 69 73 74 20 63 6f 6e 74 61 69 6e 69 on list containi
21177 6e 67 20 76 61 72 69 61 62 6c 65 73 0a 20 20 2a ng variables. *
21178 2a 20 20 20 20 2a 20 20 57 65 20 61 72 65 20 69 * * We are i
21179 6e 73 69 64 65 20 61 20 74 72 69 67 67 65 72 0a nside a trigger.
2117a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 61 6c 6c **. ** If all
2117b 20 6f 66 20 74 68 65 20 61 62 6f 76 65 20 61 72 of the above ar
2117c 65 20 66 61 6c 73 65 2c 20 74 68 65 6e 20 77 65 e false, then we
2117d 20 63 61 6e 20 72 75 6e 20 74 68 69 73 20 63 6f can run this co
2117e 64 65 20 6a 75 73 74 20 6f 6e 63 65 0a 20 20 2a de just once. *
2117f 2a 20 73 61 76 65 20 74 68 65 20 72 65 73 75 6c * save the resul
21180 74 73 2c 20 61 6e 64 20 72 65 75 73 65 20 74 68 ts, and reuse th
21181 65 20 73 61 6d 65 20 72 65 73 75 6c 74 20 6f 6e e same result on
21182 20 73 75 62 73 65 71 75 65 6e 74 20 69 6e 76 6f subsequent invo
21183 63 61 74 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 cations.. */.
21184 69 66 28 20 21 45 78 70 72 48 61 73 41 6e 79 50 if( !ExprHasAnyP
21185 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 roperty(pExpr, E
21186 50 5f 56 61 72 53 65 6c 65 63 74 29 20 26 26 20 P_VarSelect) &&
21187 21 70 50 61 72 73 65 2d 3e 70 54 72 69 67 67 65 !pParse->pTrigge
21188 72 54 61 62 20 29 7b 0a 20 20 20 20 69 6e 74 20 rTab ){. int
21189 6d 65 6d 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e mem = ++pParse->
2118a 6e 4d 65 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65 nMem;. sqlite
2118b 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 3VdbeAddOp1(v, O
2118c 50 5f 49 66 2c 20 6d 65 6d 29 3b 0a 20 20 20 20 P_If, mem);.
2118d 74 65 73 74 41 64 64 72 20 3d 20 73 71 6c 69 74 testAddr = sqlit
2118e 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
2118f 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 6d OP_Integer, 1, m
21190 65 6d 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 em);. assert(
21191 20 74 65 73 74 41 64 64 72 3e 30 20 7c 7c 20 70 testAddr>0 || p
21192 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f Parse->db->mallo
21193 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 7d 0a 0a cFailed );. }..
21194 20 20 73 77 69 74 63 68 28 20 70 45 78 70 72 2d switch( pExpr-
21195 3e 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20 >op ){. case
21196 54 4b 5f 49 4e 3a 20 7b 0a 20 20 20 20 20 20 63 TK_IN: {. c
21197 68 61 72 20 61 66 66 69 6e 69 74 79 3b 0a 20 20 har affinity;.
21198 20 20 20 20 4b 65 79 49 6e 66 6f 20 6b 65 79 49 KeyInfo keyI
21199 6e 66 6f 3b 0a 20 20 20 20 20 20 69 6e 74 20 61 nfo;. int a
2119a 64 64 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 41 ddr; /* A
2119b 64 64 72 65 73 73 20 6f 66 20 4f 50 5f 4f 70 65 ddress of OP_Ope
2119c 6e 45 70 68 65 6d 65 72 61 6c 20 69 6e 73 74 72 nEphemeral instr
2119d 75 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 uction */.
2119e 45 78 70 72 20 2a 70 4c 65 66 74 20 3d 20 70 45 Expr *pLeft = pE
2119f 78 70 72 2d 3e 70 4c 65 66 74 3b 0a 0a 20 20 20 xpr->pLeft;..
211a0 20 20 20 69 66 28 20 72 4d 61 79 48 61 76 65 4e if( rMayHaveN
211a1 75 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 73 ull ){. s
211a2 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 qlite3VdbeAddOp2
211a3 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 (v, OP_Null, 0,
211a4 72 4d 61 79 48 61 76 65 4e 75 6c 6c 29 3b 0a 20 rMayHaveNull);.
211a5 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 61 66 }.. af
211a6 66 69 6e 69 74 79 20 3d 20 73 71 6c 69 74 65 33 finity = sqlite3
211a7 45 78 70 72 41 66 66 69 6e 69 74 79 28 70 4c 65 ExprAffinity(pLe
211a8 66 74 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 57 ft);.. /* W
211a9 68 65 74 68 65 72 20 74 68 69 73 20 69 73 20 61 hether this is a
211aa 6e 20 27 78 20 49 4e 28 53 45 4c 45 43 54 2e 2e n 'x IN(SELECT..
211ab 2e 29 27 20 6f 72 20 61 6e 20 27 78 20 49 4e 28 .)' or an 'x IN(
211ac 3c 65 78 70 72 6c 69 73 74 3e 29 27 0a 20 20 20 <exprlist>)'.
211ad 20 20 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e ** expression
211ae 20 69 74 20 69 73 20 68 61 6e 64 6c 65 64 20 74 it is handled t
211af 68 65 20 73 61 6d 65 20 77 61 79 2e 20 41 20 76 he same way. A v
211b0 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 73 20 irtual table is
211b1 0a 20 20 20 20 20 20 2a 2a 20 66 69 6c 6c 65 64 . ** filled
211b2 20 77 69 74 68 20 73 69 6e 67 6c 65 2d 66 69 65 with single-fie
211b3 6c 64 20 69 6e 64 65 78 20 6b 65 79 73 20 72 65 ld index keys re
211b4 70 72 65 73 65 6e 74 69 6e 67 20 74 68 65 20 72 presenting the r
211b5 65 73 75 6c 74 73 0a 20 20 20 20 20 20 2a 2a 20 esults. **
211b6 66 72 6f 6d 20 74 68 65 20 53 45 4c 45 43 54 20 from the SELECT
211b7 6f 72 20 74 68 65 20 3c 65 78 70 72 6c 69 73 74 or the <exprlist
211b8 3e 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 >.. **.
211b9 20 20 2a 2a 20 49 66 20 74 68 65 20 27 78 27 20 ** If the 'x'
211ba 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 20 expression is a
211bb 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 2c 20 6f 72 column value, or
211bc 20 74 68 65 20 53 45 4c 45 43 54 2e 2e 2e 0a 20 the SELECT....
211bd 20 20 20 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e ** statemen
211be 74 20 72 65 74 75 72 6e 73 20 61 20 63 6f 6c 75 t returns a colu
211bf 6d 6e 20 76 61 6c 75 65 2c 20 74 68 65 6e 20 74 mn value, then t
211c0 68 65 20 61 66 66 69 6e 69 74 79 20 6f 66 20 74 he affinity of t
211c1 68 61 74 0a 20 20 20 20 20 20 2a 2a 20 63 6f 6c hat. ** col
211c2 75 6d 6e 20 69 73 20 75 73 65 64 20 74 6f 20 62 umn is used to b
211c3 75 69 6c 64 20 74 68 65 20 69 6e 64 65 78 20 6b uild the index k
211c4 65 79 73 2e 20 49 66 20 62 6f 74 68 20 27 78 27 eys. If both 'x'
211c5 20 61 6e 64 20 74 68 65 0a 20 20 20 20 20 20 2a and the. *
211c6 2a 20 53 45 4c 45 43 54 2e 2e 2e 20 73 74 61 74 * SELECT... stat
211c7 65 6d 65 6e 74 20 61 72 65 20 63 6f 6c 75 6d 6e ement are column
211c8 73 2c 20 74 68 65 6e 20 6e 75 6d 65 72 69 63 20 s, then numeric
211c9 61 66 66 69 6e 69 74 79 20 69 73 20 75 73 65 64 affinity is used
211ca 0a 20 20 20 20 20 20 2a 2a 20 69 66 20 65 69 74 . ** if eit
211cb 68 65 72 20 63 6f 6c 75 6d 6e 20 68 61 73 20 4e her column has N
211cc 55 4d 45 52 49 43 20 6f 72 20 49 4e 54 45 47 45 UMERIC or INTEGE
211cd 52 20 61 66 66 69 6e 69 74 79 2e 20 49 66 20 6e R affinity. If n
211ce 65 69 74 68 65 72 0a 20 20 20 20 20 20 2a 2a 20 either. **
211cf 27 78 27 20 6e 6f 72 20 74 68 65 20 53 45 4c 45 'x' nor the SELE
211d0 43 54 2e 2e 2e 20 73 74 61 74 65 6d 65 6e 74 20 CT... statement
211d1 61 72 65 20 63 6f 6c 75 6d 6e 73 2c 20 74 68 65 are columns, the
211d2 6e 20 6e 75 6d 65 72 69 63 20 61 66 66 69 6e 69 n numeric affini
211d3 74 79 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 75 ty. ** is u
211d4 73 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 sed.. */.
211d5 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c pExpr->iTabl
211d6 65 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 e = pParse->nTab
211d7 2b 2b 3b 0a 20 20 20 20 20 20 61 64 64 72 20 3d ++;. addr =
211d8 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
211d9 70 32 28 76 2c 20 4f 50 5f 4f 70 65 6e 45 70 68 p2(v, OP_OpenEph
211da 65 6d 65 72 61 6c 2c 20 70 45 78 70 72 2d 3e 69 emeral, pExpr->i
211db 54 61 62 6c 65 2c 20 21 69 73 52 6f 77 69 64 29 Table, !isRowid)
211dc 3b 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26 ;. memset(&
211dd 6b 65 79 49 6e 66 6f 2c 20 30 2c 20 73 69 7a 65 keyInfo, 0, size
211de 6f 66 28 6b 65 79 49 6e 66 6f 29 29 3b 0a 20 20 of(keyInfo));.
211df 20 20 20 20 6b 65 79 49 6e 66 6f 2e 6e 46 69 65 keyInfo.nFie
211e0 6c 64 20 3d 20 31 3b 0a 0a 20 20 20 20 20 20 69 ld = 1;.. i
211e1 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 f( ExprHasProper
211e2 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73 ty(pExpr, EP_xIs
211e3 53 65 6c 65 63 74 29 20 29 7b 0a 20 20 20 20 20 Select) ){.
211e4 20 20 20 2f 2a 20 43 61 73 65 20 31 3a 20 20 20 /* Case 1:
211e5 20 20 65 78 70 72 20 49 4e 20 28 53 45 4c 45 43 expr IN (SELEC
211e6 54 20 2e 2e 2e 29 0a 20 20 20 20 20 20 20 20 2a T ...). *
211e7 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 47 65 6e *. ** Gen
211e8 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 77 72 erate code to wr
211e9 69 74 65 20 74 68 65 20 72 65 73 75 6c 74 73 20 ite the results
211ea 6f 66 20 74 68 65 20 73 65 6c 65 63 74 20 69 6e of the select in
211eb 74 6f 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 to the temporary
211ec 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 61 62 6c . ** tabl
211ed 65 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 e allocated and
211ee 6f 70 65 6e 65 64 20 61 62 6f 76 65 2e 0a 20 20 opened above..
211ef 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 */.
211f0 20 53 65 6c 65 63 74 44 65 73 74 20 64 65 73 74 SelectDest dest
211f1 3b 0a 20 20 20 20 20 20 20 20 45 78 70 72 4c 69 ;. ExprLi
211f2 73 74 20 2a 70 45 4c 69 73 74 3b 0a 0a 20 20 20 st *pEList;..
211f3 20 20 20 20 20 61 73 73 65 72 74 28 20 21 69 73 assert( !is
211f4 52 6f 77 69 64 20 29 3b 0a 20 20 20 20 20 20 20 Rowid );.
211f5 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 sqlite3SelectDe
211f6 73 74 49 6e 69 74 28 26 64 65 73 74 2c 20 53 52 stInit(&dest, SR
211f7 54 5f 53 65 74 2c 20 70 45 78 70 72 2d 3e 69 54 T_Set, pExpr->iT
211f8 61 62 6c 65 29 3b 0a 20 20 20 20 20 20 20 20 64 able);. d
211f9 65 73 74 2e 61 66 66 69 6e 69 74 79 20 3d 20 28 est.affinity = (
211fa 75 38 29 61 66 66 69 6e 69 74 79 3b 0a 20 20 20 u8)affinity;.
211fb 20 20 20 20 20 61 73 73 65 72 74 28 20 28 70 45 assert( (pE
211fc 78 70 72 2d 3e 69 54 61 62 6c 65 26 30 78 30 30 xpr->iTable&0x00
211fd 30 30 46 46 46 46 29 3d 3d 70 45 78 70 72 2d 3e 00FFFF)==pExpr->
211fe 69 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 20 20 iTable );.
211ff 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 65 6c if( sqlite3Sel
21200 65 63 74 28 70 50 61 72 73 65 2c 20 70 45 78 70 ect(pParse, pExp
21201 72 2d 3e 78 2e 70 53 65 6c 65 63 74 2c 20 26 64 r->x.pSelect, &d
21202 65 73 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 est) ){.
21203 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 return;.
21204 20 20 7d 0a 20 20 20 20 20 20 20 20 70 45 4c 69 }. pELi
21205 73 74 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 53 st = pExpr->x.pS
21206 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 3b 0a 20 elect->pEList;.
21207 20 20 20 20 20 20 20 69 66 28 20 41 4c 57 41 59 if( ALWAY
21208 53 28 70 45 4c 69 73 74 21 3d 30 20 26 26 20 70 S(pEList!=0 && p
21209 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3e 30 29 20 EList->nExpr>0)
2120a 29 7b 20 0a 20 20 20 20 20 20 20 20 20 20 6b 65 ){ . ke
2120b 79 49 6e 66 6f 2e 61 43 6f 6c 6c 5b 30 5d 20 3d yInfo.aColl[0] =
2120c 20 73 71 6c 69 74 65 33 42 69 6e 61 72 79 43 6f sqlite3BinaryCo
2120d 6d 70 61 72 65 43 6f 6c 6c 53 65 71 28 70 50 61 mpareCollSeq(pPa
2120e 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 rse, pExpr->pLef
2120f 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 t,.
21210 20 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 pEList->a[0].pE
21211 78 70 72 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a xpr);. }.
21212 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 }else if(
21213 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 21 3d pExpr->x.pList!=
21214 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 0 ){. /*
21215 43 61 73 65 20 32 3a 20 20 20 20 20 65 78 70 72 Case 2: expr
21216 20 49 4e 20 28 65 78 70 72 6c 69 73 74 29 0a 20 IN (exprlist).
21217 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 **.
21218 20 20 2a 2a 20 46 6f 72 20 65 61 63 68 20 65 78 ** For each ex
21219 70 72 65 73 73 69 6f 6e 2c 20 62 75 69 6c 64 20 pression, build
2121a 61 6e 20 69 6e 64 65 78 20 6b 65 79 20 66 72 6f an index key fro
2121b 6d 20 74 68 65 20 65 76 61 6c 75 61 74 69 6f 6e m the evaluation
2121c 20 61 6e 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 and. **
2121d 73 74 6f 72 65 20 69 74 20 69 6e 20 74 68 65 20 store it in the
2121e 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 2e temporary table.
2121f 20 49 66 20 3c 65 78 70 72 3e 20 69 73 20 61 20 If <expr> is a
21220 63 6f 6c 75 6d 6e 2c 20 74 68 65 6e 20 75 73 65 column, then use
21221 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 61 74 . ** that
21222 20 63 6f 6c 75 6d 6e 73 20 61 66 66 69 6e 69 74 columns affinit
21223 79 20 77 68 65 6e 20 62 75 69 6c 64 69 6e 67 20 y when building
21224 69 6e 64 65 78 20 6b 65 79 73 2e 20 49 66 20 3c index keys. If <
21225 65 78 70 72 3e 20 69 73 20 6e 6f 74 0a 20 20 20 expr> is not.
21226 20 20 20 20 20 2a 2a 20 61 20 63 6f 6c 75 6d 6e ** a column
21227 2c 20 75 73 65 20 6e 75 6d 65 72 69 63 20 61 66 , use numeric af
21228 66 69 6e 69 74 79 2e 0a 20 20 20 20 20 20 20 20 finity..
21229 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 */. int i
2122a 3b 0a 20 20 20 20 20 20 20 20 45 78 70 72 4c 69 ;. ExprLi
2122b 73 74 20 2a 70 4c 69 73 74 20 3d 20 70 45 78 70 st *pList = pExp
2122c 72 2d 3e 78 2e 70 4c 69 73 74 3b 0a 20 20 20 20 r->x.pList;.
2122d 20 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c struct ExprL
2122e 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b ist_item *pItem;
2122f 0a 20 20 20 20 20 20 20 20 69 6e 74 20 72 31 2c . int r1,
21230 20 72 32 2c 20 72 33 3b 0a 0a 20 20 20 20 20 20 r2, r3;..
21231 20 20 69 66 28 20 21 61 66 66 69 6e 69 74 79 20 if( !affinity
21232 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61 66 66 ){. aff
21233 69 6e 69 74 79 20 3d 20 53 51 4c 49 54 45 5f 41 inity = SQLITE_A
21234 46 46 5f 4e 4f 4e 45 3b 0a 20 20 20 20 20 20 20 FF_NONE;.
21235 20 7d 0a 20 20 20 20 20 20 20 20 6b 65 79 49 6e }. keyIn
21236 66 6f 2e 61 43 6f 6c 6c 5b 30 5d 20 3d 20 73 71 fo.aColl[0] = sq
21237 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 71 lite3ExprCollSeq
21238 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e (pParse, pExpr->
21239 70 4c 65 66 74 29 3b 0a 0a 20 20 20 20 20 20 20 pLeft);..
2123a 20 2f 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68 /* Loop through
2123b 20 65 61 63 68 20 65 78 70 72 65 73 73 69 6f 6e each expression
2123c 20 69 6e 20 3c 65 78 70 72 6c 69 73 74 3e 2e 20 in <exprlist>.
2123d 2a 2f 0a 20 20 20 20 20 20 20 20 72 31 20 3d 20 */. r1 =
2123e 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 sqlite3GetTempRe
2123f 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 g(pParse);.
21240 20 20 20 72 32 20 3d 20 73 71 6c 69 74 65 33 47 r2 = sqlite3G
21241 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 etTempReg(pParse
21242 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 );. sqlit
21243 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
21244 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 32 29 3b OP_Null, 0, r2);
21245 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 70 . for(i=p
21246 4c 69 73 74 2d 3e 6e 45 78 70 72 2c 20 70 49 74 List->nExpr, pIt
21247 65 6d 3d 70 4c 69 73 74 2d 3e 61 3b 20 69 3e 30 em=pList->a; i>0
21248 3b 20 69 2d 2d 2c 20 70 49 74 65 6d 2b 2b 29 7b ; i--, pItem++){
21249 0a 20 20 20 20 20 20 20 20 20 20 45 78 70 72 20 . Expr
2124a 2a 70 45 32 20 3d 20 70 49 74 65 6d 2d 3e 70 45 *pE2 = pItem->pE
2124b 78 70 72 3b 0a 0a 20 20 20 20 20 20 20 20 20 20 xpr;..
2124c 2f 2a 20 49 66 20 74 68 65 20 65 78 70 72 65 73 /* If the expres
2124d 73 69 6f 6e 20 69 73 20 6e 6f 74 20 63 6f 6e 73 sion is not cons
2124e 74 61 6e 74 20 74 68 65 6e 20 77 65 20 77 69 6c tant then we wil
2124f 6c 20 6e 65 65 64 20 74 6f 0a 20 20 20 20 20 20 l need to.
21250 20 20 20 20 2a 2a 20 64 69 73 61 62 6c 65 20 74 ** disable t
21251 68 65 20 74 65 73 74 20 74 68 61 74 20 77 61 73 he test that was
21252 20 67 65 6e 65 72 61 74 65 64 20 61 62 6f 76 65 generated above
21253 20 74 68 61 74 20 6d 61 6b 65 73 20 73 75 72 65 that makes sure
21254 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68 . ** th
21255 69 73 20 63 6f 64 65 20 6f 6e 6c 79 20 65 78 65 is code only exe
21256 63 75 74 65 73 20 6f 6e 63 65 2e 20 20 42 65 63 cutes once. Bec
21257 61 75 73 65 20 66 6f 72 20 61 20 6e 6f 6e 2d 63 ause for a non-c
21258 6f 6e 73 74 61 6e 74 0a 20 20 20 20 20 20 20 20 onstant.
21259 20 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 ** expression
2125a 77 65 20 6e 65 65 64 20 74 6f 20 72 65 72 75 6e we need to rerun
2125b 20 74 68 69 73 20 63 6f 64 65 20 65 61 63 68 20 this code each
2125c 74 69 6d 65 2e 0a 20 20 20 20 20 20 20 20 20 20 time..
2125d 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 */. if(
2125e 20 74 65 73 74 41 64 64 72 20 26 26 20 21 73 71 testAddr && !sq
2125f 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74 lite3ExprIsConst
21260 61 6e 74 28 70 45 32 29 20 29 7b 0a 20 20 20 20 ant(pE2) ){.
21261 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
21262 64 62 65 43 68 61 6e 67 65 54 6f 4e 6f 6f 70 28 dbeChangeToNoop(
21263 76 2c 20 74 65 73 74 41 64 64 72 2d 31 2c 20 32 v, testAddr-1, 2
21264 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 74 );. t
21265 65 73 74 41 64 64 72 20 3d 20 30 3b 0a 20 20 20 estAddr = 0;.
21266 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 }..
21267 20 20 20 20 2f 2a 20 45 76 61 6c 75 61 74 65 20 /* Evaluate
21268 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 61 the expression a
21269 6e 64 20 69 6e 73 65 72 74 20 69 74 20 69 6e 74 nd insert it int
2126a 6f 20 74 68 65 20 74 65 6d 70 20 74 61 62 6c 65 o the temp table
2126b 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 72 33 */. r3
2126c 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f = sqlite3ExprCo
2126d 64 65 54 61 72 67 65 74 28 70 50 61 72 73 65 2c deTarget(pParse,
2126e 20 70 45 32 2c 20 72 31 29 3b 0a 20 20 20 20 20 pE2, r1);.
2126f 20 20 20 20 20 69 66 28 20 69 73 52 6f 77 69 64 if( isRowid
21270 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
21271 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
21272 32 28 76 2c 20 4f 50 5f 4d 75 73 74 42 65 49 6e 2(v, OP_MustBeIn
21273 74 2c 20 72 33 2c 20 73 71 6c 69 74 65 33 56 64 t, r3, sqlite3Vd
21274 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 beCurrentAddr(v)
21275 2b 32 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 +2);.
21276 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
21277 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c p3(v, OP_Insert,
21278 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 2c 20 pExpr->iTable,
21279 72 32 2c 20 72 33 29 3b 0a 20 20 20 20 20 20 20 r2, r3);.
2127a 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
2127b 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
2127c 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4d 61 eAddOp4(v, OP_Ma
2127d 6b 65 52 65 63 6f 72 64 2c 20 72 33 2c 20 31 2c keRecord, r3, 1,
2127e 20 72 32 2c 20 26 61 66 66 69 6e 69 74 79 2c 20 r2, &affinity,
2127f 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 1);.
21280 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 sqlite3ExprCache
21281 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70 AffinityChange(p
21282 50 61 72 73 65 2c 20 72 33 2c 20 31 29 3b 0a 20 Parse, r3, 1);.
21283 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 sqlit
21284 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
21285 4f 50 5f 49 64 78 49 6e 73 65 72 74 2c 20 70 45 OP_IdxInsert, pE
21286 78 70 72 2d 3e 69 54 61 62 6c 65 2c 20 72 32 29 xpr->iTable, r2)
21287 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
21288 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
21289 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 sqlite3ReleaseTe
2128a 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 31 mpReg(pParse, r1
2128b 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 );. sqlit
2128c 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 e3ReleaseTempReg
2128d 28 70 50 61 72 73 65 2c 20 72 32 29 3b 0a 20 20 (pParse, r2);.
2128e 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 }. if(
2128f 21 69 73 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 !isRowid ){.
21290 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 sqlite3VdbeC
21291 68 61 6e 67 65 50 34 28 76 2c 20 61 64 64 72 2c hangeP4(v, addr,
21292 20 28 76 6f 69 64 20 2a 29 26 6b 65 79 49 6e 66 (void *)&keyInf
21293 6f 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 29 3b 0a o, P4_KEYINFO);.
21294 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 }. br
21295 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 eak;. }..
21296 63 61 73 65 20 54 4b 5f 45 58 49 53 54 53 3a 0a case TK_EXISTS:.
21297 20 20 20 20 63 61 73 65 20 54 4b 5f 53 45 4c 45 case TK_SELE
21298 43 54 3a 0a 20 20 20 20 64 65 66 61 75 6c 74 3a CT:. default:
21299 20 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 {. /* If t
2129a 68 69 73 20 68 61 73 20 74 6f 20 62 65 20 61 20 his has to be a
2129b 73 63 61 6c 61 72 20 53 45 4c 45 43 54 2e 20 20 scalar SELECT.
2129c 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f Generate code to
2129d 20 70 75 74 20 74 68 65 0a 20 20 20 20 20 20 2a put the. *
2129e 2a 20 76 61 6c 75 65 20 6f 66 20 74 68 69 73 20 * value of this
2129f 73 65 6c 65 63 74 20 69 6e 20 61 20 6d 65 6d 6f select in a memo
212a0 72 79 20 63 65 6c 6c 20 61 6e 64 20 72 65 63 6f ry cell and reco
212a1 72 64 20 74 68 65 20 6e 75 6d 62 65 72 0a 20 20 rd the number.
212a2 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 6d 65 ** of the me
212a3 6d 6f 72 79 20 63 65 6c 6c 20 69 6e 20 69 43 6f mory cell in iCo
212a4 6c 75 6d 6e 2e 20 20 49 66 20 74 68 69 73 20 69 lumn. If this i
212a5 73 20 61 6e 20 45 58 49 53 54 53 2c 20 77 72 69 s an EXISTS, wri
212a6 74 65 0a 20 20 20 20 20 20 2a 2a 20 61 6e 20 69 te. ** an i
212a7 6e 74 65 67 65 72 20 30 20 28 6e 6f 74 20 65 78 nteger 0 (not ex
212a8 69 73 74 73 29 20 6f 72 20 31 20 28 65 78 69 73 ists) or 1 (exis
212a9 74 73 29 20 69 6e 74 6f 20 61 20 6d 65 6d 6f 72 ts) into a memor
212aa 79 20 63 65 6c 6c 0a 20 20 20 20 20 20 2a 2a 20 y cell. **
212ab 61 6e 64 20 72 65 63 6f 72 64 20 74 68 61 74 20 and record that
212ac 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 69 6e 20 69 memory cell in i
212ad 43 6f 6c 75 6d 6e 2e 0a 20 20 20 20 20 20 2a 2f Column.. */
212ae 0a 20 20 20 20 20 20 73 74 61 74 69 63 20 63 6f . static co
212af 6e 73 74 20 54 6f 6b 65 6e 20 6f 6e 65 20 3d 20 nst Token one =
212b0 7b 20 22 31 22 2c 20 31 20 7d 3b 20 20 2f 2a 20 { "1", 1 }; /*
212b1 54 6f 6b 65 6e 20 66 6f 72 20 6c 69 74 65 72 61 Token for litera
212b2 6c 20 76 61 6c 75 65 20 31 20 2a 2f 0a 20 20 20 l value 1 */.
212b3 20 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 3b Select *pSel;
212b4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
212b5 20 20 20 20 20 20 20 20 20 2f 2a 20 53 45 4c 45 /* SELE
212b6 43 54 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 CT statement to
212b7 65 6e 63 6f 64 65 20 2a 2f 0a 20 20 20 20 20 20 encode */.
212b8 53 65 6c 65 63 74 44 65 73 74 20 64 65 73 74 3b SelectDest dest;
212b9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
212ba 20 20 20 20 20 20 2f 2a 20 48 6f 77 20 74 6f 20 /* How to
212bb 64 65 61 6c 20 77 69 74 68 20 53 45 4c 45 43 74 deal with SELECt
212bc 20 72 65 73 75 6c 74 20 2a 2f 0a 0a 20 20 20 20 result */..
212bd 20 20 74 65 73 74 63 61 73 65 28 20 70 45 78 70 testcase( pExp
212be 72 2d 3e 6f 70 3d 3d 54 4b 5f 45 58 49 53 54 53 r->op==TK_EXISTS
212bf 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 );. testca
212c0 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 se( pExpr->op==T
212c1 4b 5f 53 45 4c 45 43 54 20 29 3b 0a 20 20 20 20 K_SELECT );.
212c2 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d assert( pExpr-
212c3 3e 6f 70 3d 3d 54 4b 5f 45 58 49 53 54 53 20 7c >op==TK_EXISTS |
212c4 7c 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f | pExpr->op==TK_
212c5 53 45 4c 45 43 54 20 29 3b 0a 0a 20 20 20 20 20 SELECT );..
212c6 20 61 73 73 65 72 74 28 20 45 78 70 72 48 61 73 assert( ExprHas
212c7 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 Property(pExpr,
212c8 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 3b EP_xIsSelect) );
212c9 0a 20 20 20 20 20 20 70 53 65 6c 20 3d 20 70 45 . pSel = pE
212ca 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74 3b 0a xpr->x.pSelect;.
212cb 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c sqlite3Sel
212cc 65 63 74 44 65 73 74 49 6e 69 74 28 26 64 65 73 ectDestInit(&des
212cd 74 2c 20 30 2c 20 2b 2b 70 50 61 72 73 65 2d 3e t, 0, ++pParse->
212ce 6e 4d 65 6d 29 3b 0a 20 20 20 20 20 20 69 66 28 nMem);. if(
212cf 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 53 pExpr->op==TK_S
212d0 45 4c 45 43 54 20 29 7b 0a 20 20 20 20 20 20 20 ELECT ){.
212d1 20 64 65 73 74 2e 65 44 65 73 74 20 3d 20 53 52 dest.eDest = SR
212d2 54 5f 4d 65 6d 3b 0a 20 20 20 20 20 20 20 20 73 T_Mem;. s
212d3 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 qlite3VdbeAddOp2
212d4 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 (v, OP_Null, 0,
212d5 64 65 73 74 2e 69 50 61 72 6d 29 3b 0a 20 20 20 dest.iParm);.
212d6 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 VdbeComment
212d7 28 28 76 2c 20 22 49 6e 69 74 20 73 75 62 71 75 ((v, "Init subqu
212d8 65 72 79 20 72 65 73 75 6c 74 22 29 29 3b 0a 20 ery result"));.
212d9 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
212da 20 20 20 20 64 65 73 74 2e 65 44 65 73 74 20 3d dest.eDest =
212db 20 53 52 54 5f 45 78 69 73 74 73 3b 0a 20 20 20 SRT_Exists;.
212dc 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
212dd 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 AddOp2(v, OP_Int
212de 65 67 65 72 2c 20 30 2c 20 64 65 73 74 2e 69 50 eger, 0, dest.iP
212df 61 72 6d 29 3b 0a 20 20 20 20 20 20 20 20 56 64 arm);. Vd
212e0 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 49 beComment((v, "I
212e1 6e 69 74 20 45 58 49 53 54 53 20 72 65 73 75 6c nit EXISTS resul
212e2 74 22 29 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 t"));. }.
212e3 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 sqlite3ExprD
212e4 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62 elete(pParse->db
212e5 2c 20 70 53 65 6c 2d 3e 70 4c 69 6d 69 74 29 3b , pSel->pLimit);
212e6 0a 20 20 20 20 20 20 70 53 65 6c 2d 3e 70 4c 69 . pSel->pLi
212e7 6d 69 74 20 3d 20 73 71 6c 69 74 65 33 50 45 78 mit = sqlite3PEx
212e8 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 4e pr(pParse, TK_IN
212e9 54 45 47 45 52 2c 20 30 2c 20 30 2c 20 26 6f 6e TEGER, 0, 0, &on
212ea 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 73 71 e);. if( sq
212eb 6c 69 74 65 33 53 65 6c 65 63 74 28 70 50 61 72 lite3Select(pPar
212ec 73 65 2c 20 70 53 65 6c 2c 20 26 64 65 73 74 29 se, pSel, &dest)
212ed 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 ){. retu
212ee 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 rn;. }.
212ef 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e pExpr->iColumn
212f0 20 3d 20 28 69 31 36 29 64 65 73 74 2e 69 50 61 = (i16)dest.iPa
212f1 72 6d 3b 0a 20 20 20 20 20 20 45 78 70 72 53 65 rm;. ExprSe
212f2 74 49 72 72 65 64 75 63 69 62 6c 65 28 70 45 78 tIrreducible(pEx
212f3 70 72 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b pr);. break
212f4 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 ;. }. }.. i
212f5 66 28 20 74 65 73 74 41 64 64 72 20 29 7b 0a 20 f( testAddr ){.
212f6 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 sqlite3VdbeJu
212f7 6d 70 48 65 72 65 28 76 2c 20 74 65 73 74 41 64 mpHere(v, testAd
212f8 64 72 2d 31 29 3b 0a 20 20 7d 0a 20 20 73 71 6c dr-1);. }. sql
212f9 69 74 65 33 45 78 70 72 43 61 63 68 65 50 6f 70 ite3ExprCachePop
212fa 28 70 50 61 72 73 65 2c 20 31 29 3b 0a 0a 20 20 (pParse, 1);..
212fb 72 65 74 75 72 6e 3b 0a 7d 0a 23 65 6e 64 69 66 return;.}.#endif
212fc 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f /* SQLITE_OMIT_
212fd 53 55 42 51 55 45 52 59 20 2a 2f 0a 0a 2f 2a 0a SUBQUERY */../*.
212fe 2a 2a 20 44 75 70 6c 69 63 61 74 65 20 61 6e 20 ** Duplicate an
212ff 38 2d 62 79 74 65 20 76 61 6c 75 65 0a 2a 2f 0a 8-byte value.*/.
21300 73 74 61 74 69 63 20 63 68 61 72 20 2a 64 75 70 static char *dup
21301 38 62 79 74 65 73 28 56 64 62 65 20 2a 76 2c 20 8bytes(Vdbe *v,
21302 63 6f 6e 73 74 20 63 68 61 72 20 2a 69 6e 29 7b const char *in){
21303 0a 20 20 63 68 61 72 20 2a 6f 75 74 20 3d 20 73 . char *out = s
21304 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 qlite3DbMallocRa
21305 77 28 73 71 6c 69 74 65 33 56 64 62 65 44 62 28 w(sqlite3VdbeDb(
21306 76 29 2c 20 38 29 3b 0a 20 20 69 66 28 20 6f 75 v), 8);. if( ou
21307 74 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 t ){. memcpy(
21308 6f 75 74 2c 20 69 6e 2c 20 38 29 3b 0a 20 20 7d out, in, 8);. }
21309 0a 20 20 72 65 74 75 72 6e 20 6f 75 74 3b 0a 7d . return out;.}
2130a 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 ../*.** Generate
2130b 20 61 6e 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 an instruction
2130c 74 68 61 74 20 77 69 6c 6c 20 70 75 74 20 74 68 that will put th
2130d 65 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 e floating point
2130e 0a 2a 2a 20 76 61 6c 75 65 20 64 65 73 63 72 69 .** value descri
2130f 62 65 64 20 62 79 20 7a 5b 30 2e 2e 6e 2d 31 5d bed by z[0..n-1]
21310 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 69 into register i
21311 4d 65 6d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 7a Mem..**.** The z
21312 5b 5d 20 73 74 72 69 6e 67 20 77 69 6c 6c 20 70 [] string will p
21313 72 6f 62 61 62 6c 79 20 6e 6f 74 20 62 65 20 7a robably not be z
21314 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 2e 20 ero-terminated.
21315 20 42 75 74 20 74 68 65 20 0a 2a 2a 20 7a 5b 6e But the .** z[n
21316 5d 20 63 68 61 72 61 63 74 65 72 20 69 73 20 67 ] character is g
21317 75 61 72 61 6e 74 65 65 64 20 74 6f 20 62 65 20 uaranteed to be
21318 73 6f 6d 65 74 68 69 6e 67 20 74 68 61 74 20 64 something that d
21319 6f 65 73 20 6e 6f 74 20 6c 6f 6f 6b 0a 2a 2a 20 oes not look.**
2131a 6c 69 6b 65 20 74 68 65 20 63 6f 6e 74 69 6e 75 like the continu
2131b 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6e 75 6d ation of the num
2131c 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ber..*/.static v
2131d 6f 69 64 20 63 6f 64 65 52 65 61 6c 28 56 64 62 oid codeReal(Vdb
2131e 65 20 2a 76 2c 20 63 6f 6e 73 74 20 63 68 61 72 e *v, const char
2131f 20 2a 7a 2c 20 69 6e 74 20 6e 65 67 61 74 65 46 *z, int negateF
21320 6c 61 67 2c 20 69 6e 74 20 69 4d 65 6d 29 7b 0a lag, int iMem){.
21321 20 20 69 66 28 20 41 4c 57 41 59 53 28 7a 21 3d if( ALWAYS(z!=
21322 30 29 20 29 7b 0a 20 20 20 20 64 6f 75 62 6c 65 0) ){. double
21323 20 76 61 6c 75 65 3b 0a 20 20 20 20 63 68 61 72 value;. char
21324 20 2a 7a 56 3b 0a 20 20 20 20 73 71 6c 69 74 65 *zV;. sqlite
21325 33 41 74 6f 46 28 7a 2c 20 26 76 61 6c 75 65 29 3AtoF(z, &value)
21326 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 73 ;. assert( !s
21327 71 6c 69 74 65 33 49 73 4e 61 4e 28 76 61 6c 75 qlite3IsNaN(valu
21328 65 29 20 29 3b 20 2f 2a 20 54 68 65 20 6e 65 77 e) ); /* The new
21329 20 41 74 6f 46 20 6e 65 76 65 72 20 72 65 74 75 AtoF never retu
2132a 72 6e 73 20 4e 61 4e 20 2a 2f 0a 20 20 20 20 69 rns NaN */. i
2132b 66 28 20 6e 65 67 61 74 65 46 6c 61 67 20 29 20 f( negateFlag )
2132c 76 61 6c 75 65 20 3d 20 2d 76 61 6c 75 65 3b 0a value = -value;.
2132d 20 20 20 20 7a 56 20 3d 20 64 75 70 38 62 79 74 zV = dup8byt
2132e 65 73 28 76 2c 20 28 63 68 61 72 2a 29 26 76 61 es(v, (char*)&va
2132f 6c 75 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 lue);. sqlite
21330 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 3VdbeAddOp4(v, O
21331 50 5f 52 65 61 6c 2c 20 30 2c 20 69 4d 65 6d 2c P_Real, 0, iMem,
21332 20 30 2c 20 7a 56 2c 20 50 34 5f 52 45 41 4c 29 0, zV, P4_REAL)
21333 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 ;. }.}.../*.**
21334 47 65 6e 65 72 61 74 65 20 61 6e 20 69 6e 73 74 Generate an inst
21335 72 75 63 74 69 6f 6e 20 74 68 61 74 20 77 69 6c ruction that wil
21336 6c 20 70 75 74 20 74 68 65 20 69 6e 74 65 67 65 l put the intege
21337 72 20 64 65 73 63 72 69 62 65 20 62 79 0a 2a 2a r describe by.**
21338 20 74 65 78 74 20 7a 5b 30 2e 2e 6e 2d 31 5d 20 text z[0..n-1]
21339 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 69 4d into register iM
2133a 65 6d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 7a 5b em..**.** The z[
2133b 5d 20 73 74 72 69 6e 67 20 77 69 6c 6c 20 70 72 ] string will pr
2133c 6f 62 61 62 6c 79 20 6e 6f 74 20 62 65 20 7a 65 obably not be ze
2133d 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 2e 20 20 ro-terminated.
2133e 42 75 74 20 74 68 65 20 0a 2a 2a 20 7a 5b 6e 5d But the .** z[n]
2133f 20 63 68 61 72 61 63 74 65 72 20 69 73 20 67 75 character is gu
21340 61 72 61 6e 74 65 65 64 20 74 6f 20 62 65 20 73 aranteed to be s
21341 6f 6d 65 74 68 69 6e 67 20 74 68 61 74 20 64 6f omething that do
21342 65 73 20 6e 6f 74 20 6c 6f 6f 6b 0a 2a 2a 20 6c es not look.** l
21343 69 6b 65 20 74 68 65 20 63 6f 6e 74 69 6e 75 61 ike the continua
21344 74 69 6f 6e 20 6f 66 20 74 68 65 20 6e 75 6d 62 tion of the numb
21345 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f er..*/.static vo
21346 69 64 20 63 6f 64 65 49 6e 74 65 67 65 72 28 56 id codeInteger(V
21347 64 62 65 20 2a 76 2c 20 45 78 70 72 20 2a 70 45 dbe *v, Expr *pE
21348 78 70 72 2c 20 69 6e 74 20 6e 65 67 46 6c 61 67 xpr, int negFlag
21349 2c 20 69 6e 74 20 69 4d 65 6d 29 7b 0a 20 20 69 , int iMem){. i
2134a 66 28 20 70 45 78 70 72 2d 3e 66 6c 61 67 73 20 f( pExpr->flags
2134b 26 20 45 50 5f 49 6e 74 56 61 6c 75 65 20 29 7b & EP_IntValue ){
2134c 0a 20 20 20 20 69 6e 74 20 69 20 3d 20 70 45 78 . int i = pEx
2134d 70 72 2d 3e 75 2e 69 56 61 6c 75 65 3b 0a 20 20 pr->u.iValue;.
2134e 20 20 69 66 28 20 6e 65 67 46 6c 61 67 20 29 20 if( negFlag )
2134f 69 20 3d 20 2d 69 3b 0a 20 20 20 20 73 71 6c 69 i = -i;. sqli
21350 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c te3VdbeAddOp2(v,
21351 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 69 2c 20 OP_Integer, i,
21352 69 4d 65 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a iMem);. }else{.
21353 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a const char *
21354 7a 20 3d 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f z = pExpr->u.zTo
21355 6b 65 6e 3b 0a 20 20 20 20 61 73 73 65 72 74 28 ken;. assert(
21356 20 7a 21 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 z!=0 );. if(
21357 20 73 71 6c 69 74 65 33 46 69 74 73 49 6e 36 34 sqlite3FitsIn64
21358 42 69 74 73 28 7a 2c 20 6e 65 67 46 6c 61 67 29 Bits(z, negFlag)
21359 20 29 7b 0a 20 20 20 20 20 20 69 36 34 20 76 61 ){. i64 va
2135a 6c 75 65 3b 0a 20 20 20 20 20 20 63 68 61 72 20 lue;. char
2135b 2a 7a 56 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 *zV;. sqlit
2135c 65 33 41 74 6f 69 36 34 28 7a 2c 20 26 76 61 6c e3Atoi64(z, &val
2135d 75 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6e ue);. if( n
2135e 65 67 46 6c 61 67 20 29 20 76 61 6c 75 65 20 3d egFlag ) value =
2135f 20 2d 76 61 6c 75 65 3b 0a 20 20 20 20 20 20 7a -value;. z
21360 56 20 3d 20 64 75 70 38 62 79 74 65 73 28 76 2c V = dup8bytes(v,
21361 20 28 63 68 61 72 2a 29 26 76 61 6c 75 65 29 3b (char*)&value);
21362 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
21363 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 49 beAddOp4(v, OP_I
21364 6e 74 36 34 2c 20 30 2c 20 69 4d 65 6d 2c 20 30 nt64, 0, iMem, 0
21365 2c 20 7a 56 2c 20 50 34 5f 49 4e 54 36 34 29 3b , zV, P4_INT64);
21366 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
21367 20 20 63 6f 64 65 52 65 61 6c 28 76 2c 20 7a 2c codeReal(v, z,
21368 20 6e 65 67 46 6c 61 67 2c 20 69 4d 65 6d 29 3b negFlag, iMem);
21369 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a . }. }.}../*
2136a 0a 2a 2a 20 43 6c 65 61 72 20 61 20 63 61 63 68 .** Clear a cach
2136b 65 20 65 6e 74 72 79 2e 0a 2a 2f 0a 73 74 61 74 e entry..*/.stat
2136c 69 63 20 76 6f 69 64 20 63 61 63 68 65 45 6e 74 ic void cacheEnt
2136d 72 79 43 6c 65 61 72 28 50 61 72 73 65 20 2a 70 ryClear(Parse *p
2136e 50 61 72 73 65 2c 20 73 74 72 75 63 74 20 79 43 Parse, struct yC
2136f 6f 6c 43 61 63 68 65 20 2a 70 29 7b 0a 20 20 69 olCache *p){. i
21370 66 28 20 70 2d 3e 74 65 6d 70 52 65 67 20 29 7b f( p->tempReg ){
21371 0a 20 20 20 20 69 66 28 20 70 50 61 72 73 65 2d . if( pParse-
21372 3e 6e 54 65 6d 70 52 65 67 3c 41 72 72 61 79 53 >nTempReg<ArrayS
21373 69 7a 65 28 70 50 61 72 73 65 2d 3e 61 54 65 6d ize(pParse->aTem
21374 70 52 65 67 29 20 29 7b 0a 20 20 20 20 20 20 70 pReg) ){. p
21375 50 61 72 73 65 2d 3e 61 54 65 6d 70 52 65 67 5b Parse->aTempReg[
21376 70 50 61 72 73 65 2d 3e 6e 54 65 6d 70 52 65 67 pParse->nTempReg
21377 2b 2b 5d 20 3d 20 70 2d 3e 69 52 65 67 3b 0a 20 ++] = p->iReg;.
21378 20 20 20 7d 0a 20 20 20 20 70 2d 3e 74 65 6d 70 }. p->temp
21379 52 65 67 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a Reg = 0;. }.}..
2137a 0a 2f 2a 0a 2a 2a 20 52 65 63 6f 72 64 20 69 6e ./*.** Record in
2137b 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63 61 63 68 the column cach
2137c 65 20 74 68 61 74 20 61 20 70 61 72 74 69 63 75 e that a particu
2137d 6c 61 72 20 63 6f 6c 75 6d 6e 20 66 72 6f 6d 20 lar column from
2137e 61 0a 2a 2a 20 70 61 72 74 69 63 75 6c 61 72 20 a.** particular
2137f 74 61 62 6c 65 20 69 73 20 73 74 6f 72 65 64 20 table is stored
21380 69 6e 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 in a particular
21381 72 65 67 69 73 74 65 72 2e 0a 2a 2f 0a 53 51 4c register..*/.SQL
21382 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
21383 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 sqlite3ExprCach
21384 65 53 74 6f 72 65 28 50 61 72 73 65 20 2a 70 50 eStore(Parse *pP
21385 61 72 73 65 2c 20 69 6e 74 20 69 54 61 62 2c 20 arse, int iTab,
21386 69 6e 74 20 69 43 6f 6c 2c 20 69 6e 74 20 69 52 int iCol, int iR
21387 65 67 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 eg){. int i;.
21388 69 6e 74 20 6d 69 6e 4c 72 75 3b 0a 20 20 69 6e int minLru;. in
21389 74 20 69 64 78 4c 72 75 3b 0a 20 20 73 74 72 75 t idxLru;. stru
2138a 63 74 20 79 43 6f 6c 43 61 63 68 65 20 2a 70 3b ct yColCache *p;
2138b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 52 65 67 .. assert( iReg
2138c 3e 30 20 29 3b 20 20 2f 2a 20 52 65 67 69 73 74 >0 ); /* Regist
2138d 65 72 20 6e 75 6d 62 65 72 73 20 61 72 65 20 61 er numbers are a
2138e 6c 77 61 79 73 20 70 6f 73 69 74 69 76 65 20 2a lways positive *
2138f 2f 0a 20 20 61 73 73 65 72 74 28 20 69 43 6f 6c /. assert( iCol
21390 3e 3d 2d 31 20 26 26 20 69 43 6f 6c 3c 33 32 37 >=-1 && iCol<327
21391 36 38 20 29 3b 20 20 2f 2a 20 46 69 6e 69 74 65 68 ); /* Finite
21392 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 73 20 column numbers
21393 2a 2f 0a 0a 20 20 2f 2a 20 46 69 72 73 74 20 72 */.. /* First r
21394 65 70 6c 61 63 65 20 61 6e 79 20 65 78 69 73 74 eplace any exist
21395 69 6e 67 20 65 6e 74 72 79 20 2a 2f 0a 20 20 66 ing entry */. f
21396 6f 72 28 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 or(i=0, p=pParse
21397 2d 3e 61 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 ->aColCache; i<S
21398 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 QLITE_N_COLCACHE
21399 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 ; i++, p++){.
2139a 20 69 66 28 20 70 2d 3e 69 52 65 67 20 26 26 20 if( p->iReg &&
2139b 70 2d 3e 69 54 61 62 6c 65 3d 3d 69 54 61 62 20 p->iTable==iTab
2139c 26 26 20 70 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 69 && p->iColumn==i
2139d 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 63 61 63 Col ){. cac
2139e 68 65 45 6e 74 72 79 43 6c 65 61 72 28 70 50 61 heEntryClear(pPa
2139f 72 73 65 2c 20 70 29 3b 0a 20 20 20 20 20 20 70 rse, p);. p
213a0 2d 3e 69 4c 65 76 65 6c 20 3d 20 70 50 61 72 73 ->iLevel = pPars
213a1 65 2d 3e 69 43 61 63 68 65 4c 65 76 65 6c 3b 0a e->iCacheLevel;.
213a2 20 20 20 20 20 20 70 2d 3e 69 52 65 67 20 3d 20 p->iReg =
213a3 69 52 65 67 3b 0a 20 20 20 20 20 20 70 2d 3e 61 iReg;. p->a
213a4 66 66 43 68 61 6e 67 65 20 3d 20 30 3b 0a 20 20 ffChange = 0;.
213a5 20 20 20 20 70 2d 3e 6c 72 75 20 3d 20 70 50 61 p->lru = pPa
213a6 72 73 65 2d 3e 69 43 61 63 68 65 43 6e 74 2b 2b rse->iCacheCnt++
213a7 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a ;. return;.
213a8 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 }. }.. /*
213a9 46 69 6e 64 20 61 6e 20 65 6d 70 74 79 20 73 6c Find an empty sl
213aa 6f 74 20 61 6e 64 20 72 65 70 6c 61 63 65 20 69 ot and replace i
213ab 74 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 2c 20 t */. for(i=0,
213ac 70 3d 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 p=pParse->aColCa
213ad 63 68 65 3b 20 69 3c 53 51 4c 49 54 45 5f 4e 5f che; i<SQLITE_N_
213ae 43 4f 4c 43 41 43 48 45 3b 20 69 2b 2b 2c 20 70 COLCACHE; i++, p
213af 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e ++){. if( p->
213b0 69 52 65 67 3d 3d 30 20 29 7b 0a 20 20 20 20 20 iReg==0 ){.
213b1 20 70 2d 3e 69 4c 65 76 65 6c 20 3d 20 70 50 61 p->iLevel = pPa
213b2 72 73 65 2d 3e 69 43 61 63 68 65 4c 65 76 65 6c rse->iCacheLevel
213b3 3b 0a 20 20 20 20 20 20 70 2d 3e 69 54 61 62 6c ;. p->iTabl
213b4 65 20 3d 20 69 54 61 62 3b 0a 20 20 20 20 20 20 e = iTab;.
213b5 70 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 69 43 6f p->iColumn = iCo
213b6 6c 3b 0a 20 20 20 20 20 20 70 2d 3e 69 52 65 67 l;. p->iReg
213b7 20 3d 20 69 52 65 67 3b 0a 20 20 20 20 20 20 70 = iReg;. p
213b8 2d 3e 61 66 66 43 68 61 6e 67 65 20 3d 20 30 3b ->affChange = 0;
213b9 0a 20 20 20 20 20 20 70 2d 3e 74 65 6d 70 52 65 . p->tempRe
213ba 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e g = 0;. p->
213bb 6c 72 75 20 3d 20 70 50 61 72 73 65 2d 3e 69 43 lru = pParse->iC
213bc 61 63 68 65 43 6e 74 2b 2b 3b 0a 20 20 20 20 20 acheCnt++;.
213bd 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 return;. }.
213be 20 7d 0a 0a 20 20 2f 2a 20 52 65 70 6c 61 63 65 }.. /* Replace
213bf 20 74 68 65 20 6c 61 73 74 20 72 65 63 65 6e 74 the last recent
213c0 6c 79 20 75 73 65 64 20 2a 2f 0a 20 20 6d 69 6e ly used */. min
213c1 4c 72 75 20 3d 20 30 78 37 66 66 66 66 66 66 66 Lru = 0x7fffffff
213c2 3b 0a 20 20 69 64 78 4c 72 75 20 3d 20 2d 31 3b ;. idxLru = -1;
213c3 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 3d 70 50 . for(i=0, p=pP
213c4 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 3b arse->aColCache;
213c5 20 69 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 i<SQLITE_N_COLC
213c6 41 43 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b ACHE; i++, p++){
213c7 0a 20 20 20 20 69 66 28 20 70 2d 3e 6c 72 75 3c . if( p->lru<
213c8 6d 69 6e 4c 72 75 20 29 7b 0a 20 20 20 20 20 20 minLru ){.
213c9 69 64 78 4c 72 75 20 3d 20 69 3b 0a 20 20 20 20 idxLru = i;.
213ca 20 20 6d 69 6e 4c 72 75 20 3d 20 70 2d 3e 6c 72 minLru = p->lr
213cb 75 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 u;. }. }. i
213cc 66 28 20 41 4c 57 41 59 53 28 69 64 78 4c 72 75 f( ALWAYS(idxLru
213cd 3e 3d 30 29 20 29 7b 0a 20 20 20 20 70 20 3d 20 >=0) ){. p =
213ce 26 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63 &pParse->aColCac
213cf 68 65 5b 69 64 78 4c 72 75 5d 3b 0a 20 20 20 20 he[idxLru];.
213d0 70 2d 3e 69 4c 65 76 65 6c 20 3d 20 70 50 61 72 p->iLevel = pPar
213d1 73 65 2d 3e 69 43 61 63 68 65 4c 65 76 65 6c 3b se->iCacheLevel;
213d2 0a 20 20 20 20 70 2d 3e 69 54 61 62 6c 65 20 3d . p->iTable =
213d3 20 69 54 61 62 3b 0a 20 20 20 20 70 2d 3e 69 43 iTab;. p->iC
213d4 6f 6c 75 6d 6e 20 3d 20 69 43 6f 6c 3b 0a 20 20 olumn = iCol;.
213d5 20 20 70 2d 3e 69 52 65 67 20 3d 20 69 52 65 67 p->iReg = iReg
213d6 3b 0a 20 20 20 20 70 2d 3e 61 66 66 43 68 61 6e ;. p->affChan
213d7 67 65 20 3d 20 30 3b 0a 20 20 20 20 70 2d 3e 74 ge = 0;. p->t
213d8 65 6d 70 52 65 67 20 3d 20 30 3b 0a 20 20 20 20 empReg = 0;.
213d9 70 2d 3e 6c 72 75 20 3d 20 70 50 61 72 73 65 2d p->lru = pParse-
213da 3e 69 43 61 63 68 65 43 6e 74 2b 2b 3b 0a 20 20 >iCacheCnt++;.
213db 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 7d 0a return;. }.}.
213dc 0a 2f 2a 0a 2a 2a 20 49 6e 64 69 63 61 74 65 20 ./*.** Indicate
213dd 74 68 61 74 20 61 20 72 65 67 69 73 74 65 72 20 that a register
213de 69 73 20 62 65 69 6e 67 20 6f 76 65 72 77 72 69 is being overwri
213df 74 74 65 6e 2e 20 20 50 75 72 67 65 20 74 68 65 tten. Purge the
213e0 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 66 72 6f register.** fro
213e1 6d 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63 61 63 m the column cac
213e2 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 he..*/.SQLITE_PR
213e3 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
213e4 65 33 45 78 70 72 43 61 63 68 65 52 65 6d 6f 76 e3ExprCacheRemov
213e5 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c e(Parse *pParse,
213e6 20 69 6e 74 20 69 52 65 67 29 7b 0a 20 20 69 6e int iReg){. in
213e7 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20 79 43 t i;. struct yC
213e8 6f 6c 43 61 63 68 65 20 2a 70 3b 0a 20 20 66 6f olCache *p;. fo
213e9 72 28 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d r(i=0, p=pParse-
213ea 3e 61 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 >aColCache; i<SQ
213eb 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b LITE_N_COLCACHE;
213ec 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 i++, p++){.
213ed 69 66 28 20 70 2d 3e 69 52 65 67 3d 3d 69 52 65 if( p->iReg==iRe
213ee 67 20 29 7b 0a 20 20 20 20 20 20 63 61 63 68 65 g ){. cache
213ef 45 6e 74 72 79 43 6c 65 61 72 28 70 50 61 72 73 EntryClear(pPars
213f0 65 2c 20 70 29 3b 0a 20 20 20 20 20 20 70 2d 3e e, p);. p->
213f1 69 52 65 67 20 3d 20 30 3b 0a 20 20 20 20 7d 0a iReg = 0;. }.
213f2 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d }.}../*.** Rem
213f3 65 6d 62 65 72 20 74 68 65 20 63 75 72 72 65 6e ember the curren
213f4 74 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 20 63 t column cache c
213f5 6f 6e 74 65 78 74 2e 20 20 41 6e 79 20 6e 65 77 ontext. Any new
213f6 20 65 6e 74 72 69 65 73 20 61 64 64 65 64 0a 2a entries added.*
213f7 2a 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 63 * added to the c
213f8 6f 6c 75 6d 6e 20 63 61 63 68 65 20 61 66 74 65 olumn cache afte
213f9 72 20 74 68 69 73 20 63 61 6c 6c 20 61 72 65 20 r this call are
213fa 72 65 6d 6f 76 65 64 20 77 68 65 6e 20 74 68 65 removed when the
213fb 0a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e .** correspondin
213fc 67 20 70 6f 70 20 6f 63 63 75 72 73 2e 0a 2a 2f g pop occurs..*/
213fd 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
213fe 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 void sqlite3Expr
213ff 43 61 63 68 65 50 75 73 68 28 50 61 72 73 65 20 CachePush(Parse
21400 2a 70 50 61 72 73 65 29 7b 0a 20 20 70 50 61 72 *pParse){. pPar
21401 73 65 2d 3e 69 43 61 63 68 65 4c 65 76 65 6c 2b se->iCacheLevel+
21402 2b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f +;.}../*.** Remo
21403 76 65 20 66 72 6f 6d 20 74 68 65 20 63 6f 6c 75 ve from the colu
21404 6d 6e 20 63 61 63 68 65 20 61 6e 79 20 65 6e 74 mn cache any ent
21405 72 69 65 73 20 74 68 61 74 20 77 65 72 65 20 61 ries that were a
21406 64 64 65 64 20 73 69 6e 63 65 20 74 68 65 0a 2a dded since the.*
21407 2a 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 4e * the previous N
21408 20 50 75 73 68 20 6f 70 65 72 61 74 69 6f 6e 73 Push operations
21409 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 . In other word
2140a 73 2c 20 72 65 73 74 6f 72 65 20 74 68 65 20 63 s, restore the c
2140b 61 63 68 65 0a 2a 2a 20 74 6f 20 74 68 65 20 73 ache.** to the s
2140c 74 61 74 65 20 69 74 20 77 61 73 20 69 6e 20 4e tate it was in N
2140d 20 50 75 73 68 65 73 20 61 67 6f 2e 0a 2a 2f 0a Pushes ago..*/.
2140e 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
2140f 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 oid sqlite3ExprC
21410 61 63 68 65 50 6f 70 28 50 61 72 73 65 20 2a 70 achePop(Parse *p
21411 50 61 72 73 65 2c 20 69 6e 74 20 4e 29 7b 0a 20 Parse, int N){.
21412 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 int i;. struct
21413 20 79 43 6f 6c 43 61 63 68 65 20 2a 70 3b 0a 20 yColCache *p;.
21414 20 61 73 73 65 72 74 28 20 4e 3e 30 20 29 3b 0a assert( N>0 );.
21415 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65 assert( pParse
21416 2d 3e 69 43 61 63 68 65 4c 65 76 65 6c 3e 3d 4e ->iCacheLevel>=N
21417 20 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 69 43 );. pParse->iC
21418 61 63 68 65 4c 65 76 65 6c 20 2d 3d 20 4e 3b 0a acheLevel -= N;.
21419 20 20 66 6f 72 28 69 3d 30 2c 20 70 3d 70 50 61 for(i=0, p=pPa
2141a 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 3b 20 rse->aColCache;
2141b 69 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 i<SQLITE_N_COLCA
2141c 43 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a CHE; i++, p++){.
2141d 20 20 20 20 69 66 28 20 70 2d 3e 69 52 65 67 20 if( p->iReg
2141e 26 26 20 70 2d 3e 69 4c 65 76 65 6c 3e 70 50 61 && p->iLevel>pPa
2141f 72 73 65 2d 3e 69 43 61 63 68 65 4c 65 76 65 6c rse->iCacheLevel
21420 20 29 7b 0a 20 20 20 20 20 20 63 61 63 68 65 45 ){. cacheE
21421 6e 74 72 79 43 6c 65 61 72 28 70 50 61 72 73 65 ntryClear(pParse
21422 2c 20 70 29 3b 0a 20 20 20 20 20 20 70 2d 3e 69 , p);. p->i
21423 52 65 67 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 Reg = 0;. }.
21424 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e }.}../*.** When
21425 20 61 20 63 61 63 68 65 64 20 63 6f 6c 75 6d 6e a cached column
21426 20 69 73 20 72 65 75 73 65 64 2c 20 6d 61 6b 65 is reused, make
21427 20 73 75 72 65 20 74 68 61 74 20 69 74 73 20 72 sure that its r
21428 65 67 69 73 74 65 72 20 69 73 0a 2a 2a 20 6e 6f egister is.** no
21429 20 6c 6f 6e 67 65 72 20 61 76 61 69 6c 61 62 6c longer availabl
2142a 65 20 61 73 20 61 20 74 65 6d 70 20 72 65 67 69 e as a temp regi
2142b 73 74 65 72 2e 20 20 74 69 63 6b 65 74 20 23 33 ster. ticket #3
2142c 38 37 39 3a 20 20 74 68 61 74 20 73 61 6d 65 0a 879: that same.
2142d 2a 2a 20 72 65 67 69 73 74 65 72 20 6d 69 67 68 ** register migh
2142e 74 20 62 65 20 69 6e 20 74 68 65 20 63 61 63 68 t be in the cach
2142f 65 20 69 6e 20 6d 75 6c 74 69 70 6c 65 20 70 6c e in multiple pl
21430 61 63 65 73 2c 20 73 6f 20 62 65 20 73 75 72 65 aces, so be sure
21431 20 74 6f 0a 2a 2a 20 67 65 74 20 74 68 65 6d 20 to.** get them
21432 61 6c 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 all..*/.static v
21433 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 oid sqlite3ExprC
21434 61 63 68 65 50 69 6e 52 65 67 69 73 74 65 72 28 achePinRegister(
21435 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 Parse *pParse, i
21436 6e 74 20 69 52 65 67 29 7b 0a 20 20 69 6e 74 20 nt iReg){. int
21437 69 3b 0a 20 20 73 74 72 75 63 74 20 79 43 6f 6c i;. struct yCol
21438 43 61 63 68 65 20 2a 70 3b 0a 20 20 66 6f 72 28 Cache *p;. for(
21439 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61 i=0, p=pParse->a
2143a 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c 49 ColCache; i<SQLI
2143b 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69 TE_N_COLCACHE; i
2143c 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69 66 ++, p++){. if
2143d 28 20 70 2d 3e 69 52 65 67 3d 3d 69 52 65 67 20 ( p->iReg==iReg
2143e 29 7b 0a 20 20 20 20 20 20 70 2d 3e 74 65 6d 70 ){. p->temp
2143f 52 65 67 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 Reg = 0;. }.
21440 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 }.}../*.** Gene
21441 72 61 74 65 20 63 6f 64 65 20 74 68 61 74 20 77 rate code that w
21442 69 6c 6c 20 65 78 74 72 61 63 74 20 74 68 65 20 ill extract the
21443 69 43 6f 6c 75 6d 6e 2d 74 68 20 63 6f 6c 75 6d iColumn-th colum
21444 6e 20 66 72 6f 6d 0a 2a 2a 20 74 61 62 6c 65 20 n from.** table
21445 70 54 61 62 20 61 6e 64 20 73 74 6f 72 65 20 74 pTab and store t
21446 68 65 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 20 he column value
21447 69 6e 20 61 20 72 65 67 69 73 74 65 72 2e 20 20 in a register.
21448 41 6e 20 65 66 66 6f 72 74 0a 2a 2a 20 69 73 20 An effort.** is
21449 6d 61 64 65 20 74 6f 20 73 74 6f 72 65 20 74 68 made to store th
2144a 65 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 20 69 e column value i
2144b 6e 20 72 65 67 69 73 74 65 72 20 69 52 65 67 2c n register iReg,
2144c 20 62 75 74 20 74 68 69 73 20 69 73 0a 2a 2a 20 but this is.**
2144d 6e 6f 74 20 67 75 61 72 61 6e 74 65 65 64 2e 20 not guaranteed.
2144e 20 54 68 65 20 6c 6f 63 61 74 69 6f 6e 20 6f 66 The location of
2144f 20 74 68 65 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 the column valu
21450 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a e is returned..*
21451 2a 0a 2a 2a 20 54 68 65 72 65 20 6d 75 73 74 20 *.** There must
21452 62 65 20 61 6e 20 6f 70 65 6e 20 63 75 72 73 6f be an open curso
21453 72 20 74 6f 20 70 54 61 62 20 69 6e 20 69 54 61 r to pTab in iTa
21454 62 6c 65 20 77 68 65 6e 20 74 68 69 73 20 72 6f ble when this ro
21455 75 74 69 6e 65 0a 2a 2a 20 69 73 20 63 61 6c 6c utine.** is call
21456 65 64 2e 20 20 49 66 20 69 43 6f 6c 75 6d 6e 3c ed. If iColumn<
21457 30 20 74 68 65 6e 20 63 6f 64 65 20 69 73 20 67 0 then code is g
21458 65 6e 65 72 61 74 65 64 20 74 68 61 74 20 65 78 enerated that ex
21459 74 72 61 63 74 73 20 74 68 65 20 72 6f 77 69 64 tracts the rowid
2145a 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
2145b 74 69 6e 65 20 6d 69 67 68 74 20 61 74 74 65 6d tine might attem
2145c 70 74 20 74 6f 20 72 65 75 73 65 20 74 68 65 20 pt to reuse the
2145d 76 61 6c 75 65 20 6f 66 20 74 68 65 20 63 6f 6c value of the col
2145e 75 6d 6e 20 74 68 61 74 0a 2a 2a 20 68 61 73 20 umn that.** has
2145f 61 6c 72 65 61 64 79 20 62 65 65 6e 20 6c 6f 61 already been loa
21460 64 65 64 20 69 6e 74 6f 20 61 20 72 65 67 69 73 ded into a regis
21461 74 65 72 2e 20 20 54 68 65 20 76 61 6c 75 65 20 ter. The value
21462 77 69 6c 6c 20 61 6c 77 61 79 73 0a 2a 2a 20 62 will always.** b
21463 65 20 75 73 65 64 20 69 66 20 69 74 20 68 61 73 e used if it has
21464 20 6e 6f 74 20 75 6e 64 65 72 67 6f 6e 65 20 61 not undergone a
21465 6e 79 20 61 66 66 69 6e 69 74 79 20 63 68 61 6e ny affinity chan
21466 67 65 73 2e 20 20 42 75 74 20 69 66 0a 2a 2a 20 ges. But if.**
21467 61 6e 20 61 66 66 69 6e 69 74 79 20 63 68 61 6e an affinity chan
21468 67 65 20 68 61 73 20 6f 63 63 75 72 72 65 64 2c ge has occurred,
21469 20 74 68 65 6e 20 74 68 65 20 63 61 63 68 65 64 then the cached
2146a 20 76 61 6c 75 65 20 77 69 6c 6c 20 6f 6e 6c 79 value will only
2146b 20 62 65 0a 2a 2a 20 75 73 65 64 20 69 66 20 61 be.** used if a
2146c 6c 6c 6f 77 41 66 66 43 68 6e 67 20 69 73 20 74 llowAffChng is t
2146d 72 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 rue..*/.SQLITE_P
2146e 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
2146f 65 33 45 78 70 72 43 6f 64 65 47 65 74 43 6f 6c e3ExprCodeGetCol
21470 75 6d 6e 28 0a 20 20 50 61 72 73 65 20 2a 70 50 umn(. Parse *pP
21471 61 72 73 65 2c 20 20 20 2f 2a 20 50 61 72 73 69 arse, /* Parsi
21472 6e 67 20 61 6e 64 20 63 6f 64 65 20 67 65 6e 65 ng and code gene
21473 72 61 74 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a rating context *
21474 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 2c /. Table *pTab,
21475 20 20 20 20 20 2f 2a 20 44 65 73 63 72 69 70 74 /* Descript
21476 69 6f 6e 20 6f 66 20 74 68 65 20 74 61 62 6c 65 ion of the table
21477 20 77 65 20 61 72 65 20 72 65 61 64 69 6e 67 20 we are reading
21478 66 72 6f 6d 20 2a 2f 0a 20 20 69 6e 74 20 69 43 from */. int iC
21479 6f 6c 75 6d 6e 2c 20 20 20 20 20 2f 2a 20 49 6e olumn, /* In
2147a 64 65 78 20 6f 66 20 74 68 65 20 74 61 62 6c 65 dex of the table
2147b 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 69 6e 74 column */. int
2147c 20 69 54 61 62 6c 65 2c 20 20 20 20 20 20 2f 2a iTable, /*
2147d 20 54 68 65 20 63 75 72 73 6f 72 20 70 6f 69 6e The cursor poin
2147e 74 69 6e 67 20 74 6f 20 74 68 65 20 74 61 62 6c ting to the tabl
2147f 65 20 2a 2f 0a 20 20 69 6e 74 20 69 52 65 67 2c e */. int iReg,
21480 20 20 20 20 20 20 20 20 2f 2a 20 53 74 6f 72 65 /* Store
21481 20 72 65 73 75 6c 74 73 20 68 65 72 65 20 2a 2f results here */
21482 0a 20 20 69 6e 74 20 61 6c 6c 6f 77 41 66 66 43 . int allowAffC
21483 68 6e 67 20 2f 2a 20 54 72 75 65 20 69 66 20 70 hng /* True if p
21484 72 69 6f 72 20 61 66 66 69 6e 69 74 79 20 63 68 rior affinity ch
21485 61 6e 67 65 73 20 61 72 65 20 4f 4b 20 2a 2f 0a anges are OK */.
21486 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 ){. Vdbe *v = p
21487 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 Parse->pVdbe;.
21488 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20 int i;. struct
21489 79 43 6f 6c 43 61 63 68 65 20 2a 70 3b 0a 0a 20 yColCache *p;..
2148a 20 66 6f 72 28 69 3d 30 2c 20 70 3d 70 50 61 72 for(i=0, p=pPar
2148b 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 3b 20 69 se->aColCache; i
2148c 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 <SQLITE_N_COLCAC
2148d 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 HE; i++, p++){.
2148e 20 20 20 69 66 28 20 70 2d 3e 69 52 65 67 3e 30 if( p->iReg>0
2148f 20 26 26 20 70 2d 3e 69 54 61 62 6c 65 3d 3d 69 && p->iTable==i
21490 54 61 62 6c 65 20 26 26 20 70 2d 3e 69 43 6f 6c Table && p->iCol
21491 75 6d 6e 3d 3d 69 43 6f 6c 75 6d 6e 0a 20 20 20 umn==iColumn.
21492 20 20 20 20 20 20 20 20 26 26 20 28 21 70 2d 3e && (!p->
21493 61 66 66 43 68 61 6e 67 65 20 7c 7c 20 61 6c 6c affChange || all
21494 6f 77 41 66 66 43 68 6e 67 29 20 29 7b 0a 20 20 owAffChng) ){.
21495 20 20 20 20 70 2d 3e 6c 72 75 20 3d 20 70 50 61 p->lru = pPa
21496 72 73 65 2d 3e 69 43 61 63 68 65 43 6e 74 2b 2b rse->iCacheCnt++
21497 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 ;. sqlite3E
21498 78 70 72 43 61 63 68 65 50 69 6e 52 65 67 69 73 xprCachePinRegis
21499 74 65 72 28 70 50 61 72 73 65 2c 20 70 2d 3e 69 ter(pParse, p->i
2149a 52 65 67 29 3b 0a 20 20 20 20 20 20 72 65 74 75 Reg);. retu
2149b 72 6e 20 70 2d 3e 69 52 65 67 3b 0a 20 20 20 20 rn p->iReg;.
2149c 7d 0a 20 20 7d 20 20 0a 20 20 61 73 73 65 72 74 }. } . assert
2149d 28 20 76 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 ( v!=0 );. if(
2149e 69 43 6f 6c 75 6d 6e 3c 30 20 29 7b 0a 20 20 20 iColumn<0 ){.
2149f 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
214a0 70 32 28 76 2c 20 4f 50 5f 52 6f 77 69 64 2c 20 p2(v, OP_Rowid,
214a1 69 54 61 62 6c 65 2c 20 69 52 65 67 29 3b 0a 20 iTable, iReg);.
214a2 20 7d 65 6c 73 65 20 69 66 28 20 41 4c 57 41 59 }else if( ALWAY
214a3 53 28 70 54 61 62 21 3d 30 29 20 29 7b 0a 20 20 S(pTab!=0) ){.
214a4 20 20 69 6e 74 20 6f 70 20 3d 20 49 73 56 69 72 int op = IsVir
214a5 74 75 61 6c 28 70 54 61 62 29 20 3f 20 4f 50 5f tual(pTab) ? OP_
214a6 56 43 6f 6c 75 6d 6e 20 3a 20 4f 50 5f 43 6f 6c VColumn : OP_Col
214a7 75 6d 6e 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 umn;. sqlite3
214a8 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 6f 70 VdbeAddOp3(v, op
214a9 2c 20 69 54 61 62 6c 65 2c 20 69 43 6f 6c 75 6d , iTable, iColum
214aa 6e 2c 20 69 52 65 67 29 3b 0a 20 20 20 20 73 71 n, iReg);. sq
214ab 6c 69 74 65 33 43 6f 6c 75 6d 6e 44 65 66 61 75 lite3ColumnDefau
214ac 6c 74 28 76 2c 20 70 54 61 62 2c 20 69 43 6f 6c lt(v, pTab, iCol
214ad 75 6d 6e 2c 20 69 52 65 67 29 3b 0a 20 20 7d 0a umn, iReg);. }.
214ae 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 sqlite3ExprCac
214af 68 65 53 74 6f 72 65 28 70 50 61 72 73 65 2c 20 heStore(pParse,
214b0 69 54 61 62 6c 65 2c 20 69 43 6f 6c 75 6d 6e 2c iTable, iColumn,
214b1 20 69 52 65 67 29 3b 0a 20 20 72 65 74 75 72 6e iReg);. return
214b2 20 69 52 65 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 iReg;.}../*.**
214b3 43 6c 65 61 72 20 61 6c 6c 20 63 6f 6c 75 6d 6e Clear all column
214b4 20 63 61 63 68 65 20 65 6e 74 72 69 65 73 2e 0a cache entries..
214b5 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
214b6 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 E void sqlite3Ex
214b7 70 72 43 61 63 68 65 43 6c 65 61 72 28 50 61 72 prCacheClear(Par
214b8 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 69 se *pParse){. i
214b9 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20 79 nt i;. struct y
214ba 43 6f 6c 43 61 63 68 65 20 2a 70 3b 0a 0a 20 20 ColCache *p;..
214bb 66 6f 72 28 69 3d 30 2c 20 70 3d 70 50 61 72 73 for(i=0, p=pPars
214bc 65 2d 3e 61 43 6f 6c 43 61 63 68 65 3b 20 69 3c e->aColCache; i<
214bd 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 SQLITE_N_COLCACH
214be 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 E; i++, p++){.
214bf 20 20 69 66 28 20 70 2d 3e 69 52 65 67 20 29 7b if( p->iReg ){
214c0 0a 20 20 20 20 20 20 63 61 63 68 65 45 6e 74 72 . cacheEntr
214c1 79 43 6c 65 61 72 28 70 50 61 72 73 65 2c 20 70 yClear(pParse, p
214c2 29 3b 0a 20 20 20 20 20 20 70 2d 3e 69 52 65 67 );. p->iReg
214c3 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a = 0;. }. }.
214c4 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 6f 72 64 20 }../*.** Record
214c5 74 68 65 20 66 61 63 74 20 74 68 61 74 20 61 6e the fact that an
214c6 20 61 66 66 69 6e 69 74 79 20 63 68 61 6e 67 65 affinity change
214c7 20 68 61 73 20 6f 63 63 75 72 72 65 64 20 6f 6e has occurred on
214c8 20 69 43 6f 75 6e 74 0a 2a 2a 20 72 65 67 69 73 iCount.** regis
214c9 74 65 72 73 20 73 74 61 72 74 69 6e 67 20 77 69 ters starting wi
214ca 74 68 20 69 53 74 61 72 74 2e 0a 2a 2f 0a 53 51 th iStart..*/.SQ
214cb 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
214cc 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 d sqlite3ExprCac
214cd 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65 heAffinityChange
214ce 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 (Parse *pParse,
214cf 69 6e 74 20 69 53 74 61 72 74 2c 20 69 6e 74 20 int iStart, int
214d0 69 43 6f 75 6e 74 29 7b 0a 20 20 69 6e 74 20 69 iCount){. int i
214d1 45 6e 64 20 3d 20 69 53 74 61 72 74 20 2b 20 69 End = iStart + i
214d2 43 6f 75 6e 74 20 2d 20 31 3b 0a 20 20 69 6e 74 Count - 1;. int
214d3 20 69 3b 0a 20 20 73 74 72 75 63 74 20 79 43 6f i;. struct yCo
214d4 6c 43 61 63 68 65 20 2a 70 3b 0a 20 20 66 6f 72 lCache *p;. for
214d5 28 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e (i=0, p=pParse->
214d6 61 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c aColCache; i<SQL
214d7 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 ITE_N_COLCACHE;
214d8 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69 i++, p++){. i
214d9 6e 74 20 72 20 3d 20 70 2d 3e 69 52 65 67 3b 0a nt r = p->iReg;.
214da 20 20 20 20 69 66 28 20 72 3e 3d 69 53 74 61 72 if( r>=iStar
214db 74 20 26 26 20 72 3c 3d 69 45 6e 64 20 29 7b 0a t && r<=iEnd ){.
214dc 20 20 20 20 20 20 70 2d 3e 61 66 66 43 68 61 6e p->affChan
214dd 67 65 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 ge = 1;. }.
214de 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 }.}../*.** Gener
214df 61 74 65 20 63 6f 64 65 20 74 6f 20 6d 6f 76 65 ate code to move
214e0 20 63 6f 6e 74 65 6e 74 20 66 72 6f 6d 20 72 65 content from re
214e1 67 69 73 74 65 72 73 20 69 46 72 6f 6d 2e 2e 2e gisters iFrom...
214e2 69 46 72 6f 6d 2b 6e 52 65 67 2d 31 0a 2a 2a 20 iFrom+nReg-1.**
214e3 6f 76 65 72 20 74 6f 20 69 54 6f 2e 2e 69 54 6f over to iTo..iTo
214e4 2b 6e 52 65 67 2d 31 2e 20 4b 65 65 70 20 74 68 +nReg-1. Keep th
214e5 65 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 20 75 e column cache u
214e6 70 2d 74 6f 2d 64 61 74 65 2e 0a 2a 2f 0a 53 51 p-to-date..*/.SQ
214e7 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
214e8 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 d sqlite3ExprCod
214e9 65 4d 6f 76 65 28 50 61 72 73 65 20 2a 70 50 61 eMove(Parse *pPa
214ea 72 73 65 2c 20 69 6e 74 20 69 46 72 6f 6d 2c 20 rse, int iFrom,
214eb 69 6e 74 20 69 54 6f 2c 20 69 6e 74 20 6e 52 65 int iTo, int nRe
214ec 67 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 g){. int i;. s
214ed 74 72 75 63 74 20 79 43 6f 6c 43 61 63 68 65 20 truct yColCache
214ee 2a 70 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28 *p;. if( NEVER(
214ef 69 46 72 6f 6d 3d 3d 69 54 6f 29 20 29 20 72 65 iFrom==iTo) ) re
214f0 74 75 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33 56 turn;. sqlite3V
214f1 64 62 65 41 64 64 4f 70 33 28 70 50 61 72 73 65 dbeAddOp3(pParse
214f2 2d 3e 70 56 64 62 65 2c 20 4f 50 5f 4d 6f 76 65 ->pVdbe, OP_Move
214f3 2c 20 69 46 72 6f 6d 2c 20 69 54 6f 2c 20 6e 52 , iFrom, iTo, nR
214f4 65 67 29 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 eg);. for(i=0,
214f5 70 3d 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 p=pParse->aColCa
214f6 63 68 65 3b 20 69 3c 53 51 4c 49 54 45 5f 4e 5f che; i<SQLITE_N_
214f7 43 4f 4c 43 41 43 48 45 3b 20 69 2b 2b 2c 20 70 COLCACHE; i++, p
214f8 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 78 20 3d ++){. int x =
214f9 20 70 2d 3e 69 52 65 67 3b 0a 20 20 20 20 69 66 p->iReg;. if
214fa 28 20 78 3e 3d 69 46 72 6f 6d 20 26 26 20 78 3c ( x>=iFrom && x<
214fb 69 46 72 6f 6d 2b 6e 52 65 67 20 29 7b 0a 20 20 iFrom+nReg ){.
214fc 20 20 20 20 70 2d 3e 69 52 65 67 20 2b 3d 20 69 p->iReg += i
214fd 54 6f 2d 69 46 72 6f 6d 3b 0a 20 20 20 20 7d 0a To-iFrom;. }.
214fe 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e }.}../*.** Gen
214ff 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 63 6f erate code to co
21500 70 79 20 63 6f 6e 74 65 6e 74 20 66 72 6f 6d 20 py content from
21501 72 65 67 69 73 74 65 72 73 20 69 46 72 6f 6d 2e registers iFrom.
21502 2e 2e 69 46 72 6f 6d 2b 6e 52 65 67 2d 31 0a 2a ..iFrom+nReg-1.*
21503 2a 20 6f 76 65 72 20 74 6f 20 69 54 6f 2e 2e 69 * over to iTo..i
21504 54 6f 2b 6e 52 65 67 2d 31 2e 0a 2a 2f 0a 53 51 To+nReg-1..*/.SQ
21505 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
21506 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 d sqlite3ExprCod
21507 65 43 6f 70 79 28 50 61 72 73 65 20 2a 70 50 61 eCopy(Parse *pPa
21508 72 73 65 2c 20 69 6e 74 20 69 46 72 6f 6d 2c 20 rse, int iFrom,
21509 69 6e 74 20 69 54 6f 2c 20 69 6e 74 20 6e 52 65 int iTo, int nRe
2150a 67 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 g){. int i;. i
2150b 66 28 20 4e 45 56 45 52 28 69 46 72 6f 6d 3d 3d f( NEVER(iFrom==
2150c 69 54 6f 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 iTo) ) return;.
2150d 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 52 65 67 for(i=0; i<nReg
2150e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 ; i++){. sqli
2150f 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 70 50 te3VdbeAddOp2(pP
21510 61 72 73 65 2d 3e 70 56 64 62 65 2c 20 4f 50 5f arse->pVdbe, OP_
21511 43 6f 70 79 2c 20 69 46 72 6f 6d 2b 69 2c 20 69 Copy, iFrom+i, i
21512 54 6f 2b 69 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a To+i);. }.}../*
21513 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 .** Return true
21514 69 66 20 61 6e 79 20 72 65 67 69 73 74 65 72 20 if any register
21515 69 6e 20 74 68 65 20 72 61 6e 67 65 20 69 46 72 in the range iFr
21516 6f 6d 2e 2e 69 54 6f 20 28 69 6e 63 6c 75 73 69 om..iTo (inclusi
21517 76 65 29 0a 2a 2a 20 69 73 20 75 73 65 64 20 61 ve).** is used a
21518 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 63 6f s part of the co
21519 6c 75 6d 6e 20 63 61 63 68 65 2e 0a 2a 2f 0a 73 lumn cache..*/.s
2151a 74 61 74 69 63 20 69 6e 74 20 75 73 65 64 41 73 tatic int usedAs
2151b 43 6f 6c 75 6d 6e 43 61 63 68 65 28 50 61 72 73 ColumnCache(Pars
2151c 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 e *pParse, int i
2151d 46 72 6f 6d 2c 20 69 6e 74 20 69 54 6f 29 7b 0a From, int iTo){.
2151e 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 int i;. struc
2151f 74 20 79 43 6f 6c 43 61 63 68 65 20 2a 70 3b 0a t yColCache *p;.
21520 20 20 66 6f 72 28 69 3d 30 2c 20 70 3d 70 50 61 for(i=0, p=pPa
21521 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 3b 20 rse->aColCache;
21522 69 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 i<SQLITE_N_COLCA
21523 43 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a CHE; i++, p++){.
21524 20 20 20 20 69 6e 74 20 72 20 3d 20 70 2d 3e 69 int r = p->i
21525 52 65 67 3b 0a 20 20 20 20 69 66 28 20 72 3e 3d Reg;. if( r>=
21526 69 46 72 6f 6d 20 26 26 20 72 3c 3d 69 54 6f 20 iFrom && r<=iTo
21527 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a ) return 1;. }.
21528 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f return 0;.}../
21529 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c 61 73 74 *.** If the last
2152a 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 63 6f 64 instruction cod
2152b 65 64 20 69 73 20 61 6e 20 65 70 68 65 6d 65 72 ed is an ephemer
2152c 61 6c 20 63 6f 70 79 20 6f 66 20 61 6e 79 20 6f al copy of any o
2152d 66 0a 2a 2a 20 74 68 65 20 72 65 67 69 73 74 65 f.** the registe
2152e 72 73 20 69 6e 20 74 68 65 20 6e 52 65 67 20 72 rs in the nReg r
2152f 65 67 69 73 74 65 72 73 20 62 65 67 69 6e 6e 69 egisters beginni
21530 6e 67 20 77 69 74 68 20 69 52 65 67 2c 20 74 68 ng with iReg, th
21531 65 6e 0a 2a 2a 20 63 6f 6e 76 65 72 74 20 74 68 en.** convert th
21532 65 20 6c 61 73 74 20 69 6e 73 74 72 75 63 74 69 e last instructi
21533 6f 6e 20 66 72 6f 6d 20 4f 50 5f 53 43 6f 70 79 on from OP_SCopy
21534 20 74 6f 20 4f 50 5f 43 6f 70 79 2e 0a 2a 2f 0a to OP_Copy..*/.
21535 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
21536 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 48 oid sqlite3ExprH
21537 61 72 64 43 6f 70 79 28 50 61 72 73 65 20 2a 70 ardCopy(Parse *p
21538 50 61 72 73 65 2c 20 69 6e 74 20 69 52 65 67 2c Parse, int iReg,
21539 20 69 6e 74 20 6e 52 65 67 29 7b 0a 20 20 56 64 int nReg){. Vd
2153a 62 65 4f 70 20 2a 70 4f 70 3b 0a 20 20 56 64 62 beOp *pOp;. Vdb
2153b 65 20 2a 76 3b 0a 0a 20 20 61 73 73 65 72 74 28 e *v;.. assert(
2153c 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c pParse->db->mal
2153d 6c 6f 63 46 61 69 6c 65 64 3d 3d 30 20 29 3b 0a locFailed==0 );.
2153e 20 20 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 v = pParse->pV
2153f 64 62 65 3b 0a 20 20 61 73 73 65 72 74 28 20 76 dbe;. assert( v
21540 21 3d 30 20 29 3b 0a 20 20 70 4f 70 20 3d 20 73 !=0 );. pOp = s
21541 71 6c 69 74 65 33 56 64 62 65 47 65 74 4f 70 28 qlite3VdbeGetOp(
21542 76 2c 20 2d 31 29 3b 0a 20 20 61 73 73 65 72 74 v, -1);. assert
21543 28 20 70 4f 70 21 3d 30 20 29 3b 0a 20 20 69 66 ( pOp!=0 );. if
21544 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f ( pOp->opcode==O
21545 50 5f 53 43 6f 70 79 20 26 26 20 70 4f 70 2d 3e P_SCopy && pOp->
21546 70 31 3e 3d 69 52 65 67 20 26 26 20 70 4f 70 2d p1>=iReg && pOp-
21547 3e 70 31 3c 69 52 65 67 2b 6e 52 65 67 20 29 7b >p1<iReg+nReg ){
21548 0a 20 20 20 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 . pOp->opcode
21549 20 3d 20 4f 50 5f 43 6f 70 79 3b 0a 20 20 7d 0a = OP_Copy;. }.
2154a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 }../*.** Generat
2154b 65 20 63 6f 64 65 20 74 6f 20 73 74 6f 72 65 20 e code to store
2154c 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 the value of the
2154d 20 69 41 6c 69 61 73 2d 74 68 20 61 6c 69 61 73 iAlias-th alias
2154e 20 69 6e 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 in register.**
2154f 74 61 72 67 65 74 2e 20 20 54 68 65 20 66 69 72 target. The fir
21550 73 74 20 74 69 6d 65 20 74 68 69 73 20 69 73 20 st time this is
21551 63 61 6c 6c 65 64 2c 20 70 45 78 70 72 20 69 73 called, pExpr is
21552 20 65 76 61 6c 75 61 74 65 64 20 74 6f 20 63 6f evaluated to co
21553 6d 70 75 74 65 0a 2a 2a 20 74 68 65 20 76 61 6c mpute.** the val
21554 75 65 20 6f 66 20 74 68 65 20 61 6c 69 61 73 2e ue of the alias.
21555 20 20 54 68 65 20 76 61 6c 75 65 20 69 73 20 73 The value is s
21556 74 6f 72 65 64 20 69 6e 20 61 6e 20 61 75 78 69 tored in an auxi
21557 6c 69 61 72 79 20 72 65 67 69 73 74 65 72 0a 2a liary register.*
21558 2a 20 61 6e 64 20 74 68 65 20 6e 75 6d 62 65 72 * and the number
21559 20 6f 66 20 74 68 61 74 20 72 65 67 69 73 74 65 of that registe
2155a 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 r is returned.
2155b 4f 6e 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 On subsequent ca
2155c 6c 6c 73 2c 0a 2a 2a 20 74 68 65 20 72 65 67 69 lls,.** the regi
2155d 73 74 65 72 20 6e 75 6d 62 65 72 20 69 73 20 72 ster number is r
2155e 65 74 75 72 6e 65 64 20 77 69 74 68 6f 75 74 20 eturned without
2155f 67 65 6e 65 72 61 74 69 6e 67 20 61 6e 79 20 63 generating any c
21560 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 ode..**.** Note
21561 74 68 61 74 20 69 6e 20 6f 72 64 65 72 20 66 6f that in order fo
21562 72 20 74 68 69 73 20 74 6f 20 77 6f 72 6b 2c 20 r this to work,
21563 63 6f 64 65 20 6d 75 73 74 20 62 65 20 67 65 6e code must be gen
21564 65 72 61 74 65 64 20 69 6e 20 74 68 65 0a 2a 2a erated in the.**
21565 20 73 61 6d 65 20 6f 72 64 65 72 20 74 68 61 74 same order that
21566 20 69 74 20 69 73 20 65 78 65 63 75 74 65 64 2e it is executed.
21567 0a 2a 2a 0a 2a 2a 20 41 6c 69 61 73 65 73 20 61 .**.** Aliases a
21568 72 65 20 6e 75 6d 62 65 72 65 64 20 73 74 61 72 re numbered star
21569 74 69 6e 67 20 77 69 74 68 20 31 2e 20 20 53 6f ting with 1. So
2156a 20 69 41 6c 69 61 73 20 69 73 20 69 6e 20 74 68 iAlias is in th
2156b 65 20 72 61 6e 67 65 0a 2a 2a 20 6f 66 20 31 20 e range.** of 1
2156c 74 6f 20 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 to pParse->nAlia
2156d 73 20 69 6e 63 6c 75 73 69 76 65 2e 20 20 0a 2a s inclusive. .*
2156e 2a 0a 2a 2a 20 70 50 61 72 73 65 2d 3e 61 41 6c *.** pParse->aAl
2156f 69 61 73 5b 69 41 6c 69 61 73 2d 31 5d 20 72 65 ias[iAlias-1] re
21570 63 6f 72 64 73 20 74 68 65 20 72 65 67 69 73 74 cords the regist
21571 65 72 20 6e 75 6d 62 65 72 20 77 68 65 72 65 20 er number where
21572 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 20 the value.** of
21573 74 68 65 20 69 41 6c 69 61 73 2d 74 68 20 61 6c the iAlias-th al
21574 69 61 73 20 69 73 20 73 74 6f 72 65 64 2e 20 20 ias is stored.
21575 49 66 20 7a 65 72 6f 2c 20 74 68 61 74 20 6d 65 If zero, that me
21576 61 6e 73 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 ans that the.**
21577 61 6c 69 61 73 20 68 61 73 20 6e 6f 74 20 79 65 alias has not ye
21578 74 20 62 65 65 6e 20 63 6f 6d 70 75 74 65 64 2e t been computed.
21579 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 .*/.static int c
2157a 6f 64 65 41 6c 69 61 73 28 50 61 72 73 65 20 2a odeAlias(Parse *
2157b 70 50 61 72 73 65 2c 20 69 6e 74 20 69 41 6c 69 pParse, int iAli
2157c 61 73 2c 20 45 78 70 72 20 2a 70 45 78 70 72 2c as, Expr *pExpr,
2157d 20 69 6e 74 20 74 61 72 67 65 74 29 7b 0a 23 69 int target){.#i
2157e 66 20 30 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 f 0. sqlite3 *d
2157f 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a b = pParse->db;.
21580 20 20 69 6e 74 20 69 52 65 67 3b 0a 20 20 69 66 int iReg;. if
21581 28 20 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 ( pParse->nAlias
21582 41 6c 6c 6f 63 3c 70 50 61 72 73 65 2d 3e 6e 41 Alloc<pParse->nA
21583 6c 69 61 73 20 29 7b 0a 20 20 20 20 70 50 61 72 lias ){. pPar
21584 73 65 2d 3e 61 41 6c 69 61 73 20 3d 20 73 71 6c se->aAlias = sql
21585 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46 ite3DbReallocOrF
21586 72 65 65 28 64 62 2c 20 70 50 61 72 73 65 2d 3e ree(db, pParse->
21587 61 41 6c 69 61 73 2c 0a 20 20 20 20 20 20 20 20 aAlias,.
21588 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21589 20 20 20 20 20 20 20 20 20 73 69 7a 65 6f 66 28 sizeof(
2158a 70 50 61 72 73 65 2d 3e 61 41 6c 69 61 73 5b 30 pParse->aAlias[0
2158b 5d 29 2a 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 ])*pParse->nAlia
2158c 73 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 s );. testcas
2158d 65 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 e( db->mallocFai
2158e 6c 65 64 20 26 26 20 70 50 61 72 73 65 2d 3e 6e led && pParse->n
2158f 41 6c 69 61 73 41 6c 6c 6f 63 3e 30 20 29 3b 0a AliasAlloc>0 );.
21590 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c if( db->mall
21591 6f 63 46 61 69 6c 65 64 20 29 20 72 65 74 75 72 ocFailed ) retur
21592 6e 20 30 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 n 0;. memset(
21593 26 70 50 61 72 73 65 2d 3e 61 41 6c 69 61 73 5b &pParse->aAlias[
21594 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 41 6c pParse->nAliasAl
21595 6c 6f 63 5d 2c 20 30 2c 0a 20 20 20 20 20 20 20 loc], 0,.
21596 20 20 20 20 28 70 50 61 72 73 65 2d 3e 6e 41 6c (pParse->nAl
21597 69 61 73 2d 70 50 61 72 73 65 2d 3e 6e 41 6c 69 ias-pParse->nAli
21598 61 73 41 6c 6c 6f 63 29 2a 73 69 7a 65 6f 66 28 asAlloc)*sizeof(
21599 70 50 61 72 73 65 2d 3e 61 41 6c 69 61 73 5b 30 pParse->aAlias[0
2159a 5d 29 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d ]));. pParse-
2159b 3e 6e 41 6c 69 61 73 41 6c 6c 6f 63 20 3d 20 70 >nAliasAlloc = p
2159c 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 3b 0a 20 Parse->nAlias;.
2159d 20 7d 0a 20 20 61 73 73 65 72 74 28 20 69 41 6c }. assert( iAl
2159e 69 61 73 3e 30 20 26 26 20 69 41 6c 69 61 73 3c ias>0 && iAlias<
2159f 3d 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 20 =pParse->nAlias
215a0 29 3b 0a 20 20 69 52 65 67 20 3d 20 70 50 61 72 );. iReg = pPar
215a1 73 65 2d 3e 61 41 6c 69 61 73 5b 69 41 6c 69 61 se->aAlias[iAlia
215a2 73 2d 31 5d 3b 0a 20 20 69 66 28 20 69 52 65 67 s-1];. if( iReg
215a3 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 70 ==0 ){. if( p
215a4 50 61 72 73 65 2d 3e 69 43 61 63 68 65 4c 65 76 Parse->iCacheLev
215a5 65 6c 3e 30 20 29 7b 0a 20 20 20 20 20 20 69 52 el>0 ){. iR
215a6 65 67 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 eg = sqlite3Expr
215a7 43 6f 64 65 54 61 72 67 65 74 28 70 50 61 72 73 CodeTarget(pPars
215a8 65 2c 20 70 45 78 70 72 2c 20 74 61 72 67 65 74 e, pExpr, target
215a9 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 );. }else{.
215aa 20 20 20 20 69 52 65 67 20 3d 20 2b 2b 70 50 61 iReg = ++pPa
215ab 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 rse->nMem;.
215ac 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 sqlite3ExprCode
215ad 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 (pParse, pExpr,
215ae 69 52 65 67 29 3b 0a 20 20 20 20 20 20 70 50 61 iReg);. pPa
215af 72 73 65 2d 3e 61 41 6c 69 61 73 5b 69 41 6c 69 rse->aAlias[iAli
215b0 61 73 2d 31 5d 20 3d 20 69 52 65 67 3b 0a 20 20 as-1] = iReg;.
215b1 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e }. }. return
215b2 20 69 52 65 67 3b 0a 23 65 6c 73 65 0a 20 20 55 iReg;.#else. U
215b3 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
215b4 69 41 6c 69 61 73 29 3b 0a 20 20 72 65 74 75 72 iAlias);. retur
215b5 6e 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 n sqlite3ExprCod
215b6 65 54 61 72 67 65 74 28 70 50 61 72 73 65 2c 20 eTarget(pParse,
215b7 70 45 78 70 72 2c 20 74 61 72 67 65 74 29 3b 0a pExpr, target);.
215b8 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 #endif.}../*.**
215b9 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 69 6e Generate code in
215ba 74 6f 20 74 68 65 20 63 75 72 72 65 6e 74 20 56 to the current V
215bb 64 62 65 20 74 6f 20 65 76 61 6c 75 61 74 65 20 dbe to evaluate
215bc 74 68 65 20 67 69 76 65 6e 0a 2a 2a 20 65 78 70 the given.** exp
215bd 72 65 73 73 69 6f 6e 2e 20 20 41 74 74 65 6d 70 ression. Attemp
215be 74 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 72 t to store the r
215bf 65 73 75 6c 74 73 20 69 6e 20 72 65 67 69 73 74 esults in regist
215c0 65 72 20 22 74 61 72 67 65 74 22 2e 0a 2a 2a 20 er "target"..**
215c1 52 65 74 75 72 6e 20 74 68 65 20 72 65 67 69 73 Return the regis
215c2 74 65 72 20 77 68 65 72 65 20 72 65 73 75 6c 74 ter where result
215c3 73 20 61 72 65 20 73 74 6f 72 65 64 2e 0a 2a 2a s are stored..**
215c4 0a 2a 2a 20 57 69 74 68 20 74 68 69 73 20 72 6f .** With this ro
215c5 75 74 69 6e 65 2c 20 74 68 65 72 65 20 69 73 20 utine, there is
215c6 6e 6f 20 67 75 61 72 61 6e 74 65 65 20 74 68 61 no guarantee tha
215c7 74 20 72 65 73 75 6c 74 73 20 77 69 6c 6c 0a 2a t results will.*
215c8 2a 20 62 65 20 73 74 6f 72 65 64 20 69 6e 20 74 * be stored in t
215c9 61 72 67 65 74 2e 20 20 54 68 65 20 72 65 73 75 arget. The resu
215ca 6c 74 20 6d 69 67 68 74 20 62 65 20 73 74 6f 72 lt might be stor
215cb 65 64 20 69 6e 20 73 6f 6d 65 20 6f 74 68 65 72 ed in some other
215cc 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 69 66 20 .** register if
215cd 69 74 20 69 73 20 63 6f 6e 76 65 6e 69 65 6e 74 it is convenient
215ce 20 74 6f 20 64 6f 20 73 6f 2e 20 20 54 68 65 20 to do so. The
215cf 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e calling function
215d0 0a 2a 2a 20 6d 75 73 74 20 63 68 65 63 6b 20 74 .** must check t
215d1 68 65 20 72 65 74 75 72 6e 20 63 6f 64 65 20 61 he return code a
215d2 6e 64 20 6d 6f 76 65 20 74 68 65 20 72 65 73 75 nd move the resu
215d3 6c 74 73 20 74 6f 20 74 68 65 20 64 65 73 69 72 lts to the desir
215d4 65 64 0a 2a 2a 20 72 65 67 69 73 74 65 72 2e 0a ed.** register..
215d5 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
215d6 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 E int sqlite3Exp
215d7 72 43 6f 64 65 54 61 72 67 65 74 28 50 61 72 73 rCodeTarget(Pars
215d8 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 e *pParse, Expr
215d9 2a 70 45 78 70 72 2c 20 69 6e 74 20 74 61 72 67 *pExpr, int targ
215da 65 74 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d et){. Vdbe *v =
215db 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 20 pParse->pVdbe;
215dc 20 2f 2a 20 54 68 65 20 56 4d 20 75 6e 64 65 72 /* The VM under
215dd 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f construction */
215de 0a 20 20 69 6e 74 20 6f 70 3b 20 20 20 20 20 20 . int op;
215df 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
215e0 54 68 65 20 6f 70 63 6f 64 65 20 62 65 69 6e 67 The opcode being
215e1 20 63 6f 64 65 64 20 2a 2f 0a 20 20 69 6e 74 20 coded */. int
215e2 69 6e 52 65 67 20 3d 20 74 61 72 67 65 74 3b 20 inReg = target;
215e3 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 73 /* Results
215e4 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 stored in regis
215e5 74 65 72 20 69 6e 52 65 67 20 2a 2f 0a 20 20 69 ter inReg */. i
215e6 6e 74 20 72 65 67 46 72 65 65 31 20 3d 20 30 3b nt regFree1 = 0;
215e7 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 6e /* If n
215e8 6f 6e 2d 7a 65 72 6f 20 66 72 65 65 20 74 68 69 on-zero free thi
215e9 73 20 74 65 6d 70 6f 72 61 72 79 20 72 65 67 69 s temporary regi
215ea 73 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 72 65 ster */. int re
215eb 67 46 72 65 65 32 20 3d 20 30 3b 20 20 20 20 20 gFree2 = 0;
215ec 20 20 20 20 2f 2a 20 49 66 20 6e 6f 6e 2d 7a 65 /* If non-ze
215ed 72 6f 20 66 72 65 65 20 74 68 69 73 20 74 65 6d ro free this tem
215ee 70 6f 72 61 72 79 20 72 65 67 69 73 74 65 72 20 porary register
215ef 2a 2f 0a 20 20 69 6e 74 20 72 31 2c 20 72 32 2c */. int r1, r2,
215f0 20 72 33 2c 20 72 34 3b 20 20 20 20 20 20 20 2f r3, r4; /
215f1 2a 20 56 61 72 69 6f 75 73 20 72 65 67 69 73 74 * Various regist
215f2 65 72 20 6e 75 6d 62 65 72 73 20 2a 2f 0a 20 20 er numbers */.
215f3 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 sqlite3 *db = pP
215f4 61 72 73 65 2d 3e 64 62 3b 20 2f 2a 20 54 68 65 arse->db; /* The
215f5 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
215f6 74 69 6f 6e 20 2a 2f 0a 0a 20 20 61 73 73 65 72 tion */.. asser
215f7 74 28 20 74 61 72 67 65 74 3e 30 20 26 26 20 74 t( target>0 && t
215f8 61 72 67 65 74 3c 3d 70 50 61 72 73 65 2d 3e 6e arget<=pParse->n
215f9 4d 65 6d 20 29 3b 0a 20 20 69 66 28 20 76 3d 3d Mem );. if( v==
215fa 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 0 ){. assert(
215fb 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c pParse->db->mal
215fc 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 20 locFailed );.
215fd 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a return 0;. }..
215fe 20 20 69 66 28 20 70 45 78 70 72 3d 3d 30 20 29 if( pExpr==0 )
215ff 7b 0a 20 20 20 20 6f 70 20 3d 20 54 4b 5f 4e 55 {. op = TK_NU
21600 4c 4c 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 LL;. }else{.
21601 20 6f 70 20 3d 20 70 45 78 70 72 2d 3e 6f 70 3b op = pExpr->op;
21602 0a 20 20 7d 0a 20 20 73 77 69 74 63 68 28 20 6f . }. switch( o
21603 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20 54 4b p ){. case TK
21604 5f 41 47 47 5f 43 4f 4c 55 4d 4e 3a 20 7b 0a 20 _AGG_COLUMN: {.
21605 20 20 20 20 20 41 67 67 49 6e 66 6f 20 2a 70 41 AggInfo *pA
21606 67 67 49 6e 66 6f 20 3d 20 70 45 78 70 72 2d 3e ggInfo = pExpr->
21607 70 41 67 67 49 6e 66 6f 3b 0a 20 20 20 20 20 20 pAggInfo;.
21608 73 74 72 75 63 74 20 41 67 67 49 6e 66 6f 5f 63 struct AggInfo_c
21609 6f 6c 20 2a 70 43 6f 6c 20 3d 20 26 70 41 67 67 ol *pCol = &pAgg
2160a 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 70 45 78 70 72 Info->aCol[pExpr
2160b 2d 3e 69 41 67 67 5d 3b 0a 20 20 20 20 20 20 69 ->iAgg];. i
2160c 66 28 20 21 70 41 67 67 49 6e 66 6f 2d 3e 64 69 f( !pAggInfo->di
2160d 72 65 63 74 4d 6f 64 65 20 29 7b 0a 20 20 20 20 rectMode ){.
2160e 20 20 20 20 61 73 73 65 72 74 28 20 70 43 6f 6c assert( pCol
2160f 2d 3e 69 4d 65 6d 3e 30 20 29 3b 0a 20 20 20 20 ->iMem>0 );.
21610 20 20 20 20 69 6e 52 65 67 20 3d 20 70 43 6f 6c inReg = pCol
21611 2d 3e 69 4d 65 6d 3b 0a 20 20 20 20 20 20 20 20 ->iMem;.
21612 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 65 6c break;. }el
21613 73 65 20 69 66 28 20 70 41 67 67 49 6e 66 6f 2d se if( pAggInfo-
21614 3e 75 73 65 53 6f 72 74 69 6e 67 49 64 78 20 29 >useSortingIdx )
21615 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
21616 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 3VdbeAddOp3(v, O
21617 50 5f 43 6f 6c 75 6d 6e 2c 20 70 41 67 67 49 6e P_Column, pAggIn
21618 66 6f 2d 3e 73 6f 72 74 69 6e 67 49 64 78 2c 0a fo->sortingIdx,.
21619 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2161a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 43 pC
2161b 6f 6c 2d 3e 69 53 6f 72 74 65 72 43 6f 6c 75 6d ol->iSorterColum
2161c 6e 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 n, target);.
2161d 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
2161e 20 7d 0a 20 20 20 20 20 20 2f 2a 20 4f 74 68 65 }. /* Othe
2161f 72 77 69 73 65 2c 20 66 61 6c 6c 20 74 68 72 75 rwise, fall thru
21620 20 69 6e 74 6f 20 74 68 65 20 54 4b 5f 43 4f 4c into the TK_COL
21621 55 4d 4e 20 63 61 73 65 20 2a 2f 0a 20 20 20 20 UMN case */.
21622 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 43 4f }. case TK_CO
21623 4c 55 4d 4e 3a 20 7b 0a 20 20 20 20 20 20 69 66 LUMN: {. if
21624 28 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 3c ( pExpr->iTable<
21625 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 0 ){. /*
21626 54 68 69 73 20 6f 6e 6c 79 20 68 61 70 70 65 6e This only happen
21627 73 20 77 68 65 6e 20 63 6f 64 69 6e 67 20 63 68 s when coding ch
21628 65 63 6b 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 eck constraints
21629 2a 2f 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 */. asser
2162a 74 28 20 70 50 61 72 73 65 2d 3e 63 6b 42 61 73 t( pParse->ckBas
2162b 65 3e 30 20 29 3b 0a 20 20 20 20 20 20 20 20 69 e>0 );. i
2162c 6e 52 65 67 20 3d 20 70 45 78 70 72 2d 3e 69 43 nReg = pExpr->iC
2162d 6f 6c 75 6d 6e 20 2b 20 70 50 61 72 73 65 2d 3e olumn + pParse->
2162e 63 6b 42 61 73 65 3b 0a 20 20 20 20 20 20 7d 65 ckBase;. }e
2162f 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 74 65 73 lse{. tes
21630 74 63 61 73 65 28 20 28 70 45 78 70 72 2d 3e 66 tcase( (pExpr->f
21631 6c 61 67 73 20 26 20 45 50 5f 41 6e 79 41 66 66 lags & EP_AnyAff
21632 29 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 )!=0 );.
21633 69 6e 52 65 67 20 3d 20 73 71 6c 69 74 65 33 45 inReg = sqlite3E
21634 78 70 72 43 6f 64 65 47 65 74 43 6f 6c 75 6d 6e xprCodeGetColumn
21635 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e (pParse, pExpr->
21636 70 54 61 62 2c 0a 20 20 20 20 20 20 20 20 20 20 pTab,.
21637 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21638 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 43 pExpr->iC
21639 6f 6c 75 6d 6e 2c 20 70 45 78 70 72 2d 3e 69 54 olumn, pExpr->iT
2163a 61 62 6c 65 2c 20 74 61 72 67 65 74 2c 0a 20 20 able, target,.
2163b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2163c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 p
2163d 45 78 70 72 2d 3e 66 6c 61 67 73 20 26 20 45 50 Expr->flags & EP
2163e 5f 41 6e 79 41 66 66 29 3b 0a 20 20 20 20 20 20 _AnyAff);.
2163f 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 }. break;.
21640 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b }. case TK
21641 5f 49 4e 54 45 47 45 52 3a 20 7b 0a 20 20 20 20 _INTEGER: {.
21642 20 20 63 6f 64 65 49 6e 74 65 67 65 72 28 76 2c codeInteger(v,
21643 20 70 45 78 70 72 2c 20 30 2c 20 74 61 72 67 65 pExpr, 0, targe
21644 74 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b t);. break;
21645 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 . }. case
21646 54 4b 5f 46 4c 4f 41 54 3a 20 7b 0a 20 20 20 20 TK_FLOAT: {.
21647 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48 assert( !ExprH
21648 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 asProperty(pExpr
21649 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 29 , EP_IntValue) )
2164a 3b 0a 20 20 20 20 20 20 63 6f 64 65 52 65 61 6c ;. codeReal
2164b 28 76 2c 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f (v, pExpr->u.zTo
2164c 6b 65 6e 2c 20 30 2c 20 74 61 72 67 65 74 29 3b ken, 0, target);
2164d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
2164e 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f }. case TK_
2164f 53 54 52 49 4e 47 3a 20 7b 0a 20 20 20 20 20 20 STRING: {.
21650 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73 assert( !ExprHas
21651 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 Property(pExpr,
21652 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 29 3b 0a EP_IntValue) );.
21653 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
21654 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 eAddOp4(v, OP_St
21655 72 69 6e 67 38 2c 20 30 2c 20 74 61 72 67 65 74 ring8, 0, target
21656 2c 20 30 2c 20 70 45 78 70 72 2d 3e 75 2e 7a 54 , 0, pExpr->u.zT
21657 6f 6b 65 6e 2c 20 30 29 3b 0a 20 20 20 20 20 20 oken, 0);.
21658 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 break;. }.
21659 20 63 61 73 65 20 54 4b 5f 4e 55 4c 4c 3a 20 7b case TK_NULL: {
2165a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
2165b 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e beAddOp2(v, OP_N
2165c 75 6c 6c 2c 20 30 2c 20 74 61 72 67 65 74 29 3b ull, 0, target);
2165d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
2165e 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 }.#ifndef SQLI
2165f 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 TE_OMIT_BLOB_LIT
21660 45 52 41 4c 0a 20 20 20 20 63 61 73 65 20 54 4b ERAL. case TK
21661 5f 42 4c 4f 42 3a 20 7b 0a 20 20 20 20 20 20 69 _BLOB: {. i
21662 6e 74 20 6e 3b 0a 20 20 20 20 20 20 63 6f 6e 73 nt n;. cons
21663 74 20 63 68 61 72 20 2a 7a 3b 0a 20 20 20 20 20 t char *z;.
21664 20 63 68 61 72 20 2a 7a 42 6c 6f 62 3b 0a 20 20 char *zBlob;.
21665 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70 assert( !Exp
21666 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 rHasProperty(pEx
21667 70 72 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 pr, EP_IntValue)
21668 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 );. assert
21669 28 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 ( pExpr->u.zToke
2166a 6e 5b 30 5d 3d 3d 27 78 27 20 7c 7c 20 70 45 78 n[0]=='x' || pEx
2166b 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 30 5d 3d pr->u.zToken[0]=
2166c 3d 27 58 27 20 29 3b 0a 20 20 20 20 20 20 61 73 ='X' );. as
2166d 73 65 72 74 28 20 70 45 78 70 72 2d 3e 75 2e 7a sert( pExpr->u.z
2166e 54 6f 6b 65 6e 5b 31 5d 3d 3d 27 5c 27 27 20 29 Token[1]=='\'' )
2166f 3b 0a 20 20 20 20 20 20 7a 20 3d 20 26 70 45 78 ;. z = &pEx
21670 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 32 5d 3b pr->u.zToken[2];
21671 0a 20 20 20 20 20 20 6e 20 3d 20 73 71 6c 69 74 . n = sqlit
21672 65 33 53 74 72 6c 65 6e 33 30 28 7a 29 20 2d 20 e3Strlen30(z) -
21673 31 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 1;. assert(
21674 20 7a 5b 6e 5d 3d 3d 27 5c 27 27 20 29 3b 0a 20 z[n]=='\'' );.
21675 20 20 20 20 20 7a 42 6c 6f 62 20 3d 20 73 71 6c zBlob = sql
21676 69 74 65 33 48 65 78 54 6f 42 6c 6f 62 28 73 71 ite3HexToBlob(sq
21677 6c 69 74 65 33 56 64 62 65 44 62 28 76 29 2c 20 lite3VdbeDb(v),
21678 7a 2c 20 6e 29 3b 0a 20 20 20 20 20 20 73 71 6c z, n);. sql
21679 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 ite3VdbeAddOp4(v
2167a 2c 20 4f 50 5f 42 6c 6f 62 2c 20 6e 2f 32 2c 20 , OP_Blob, n/2,
2167b 74 61 72 67 65 74 2c 20 30 2c 20 7a 42 6c 6f 62 target, 0, zBlob
2167c 2c 20 50 34 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 , P4_DYNAMIC);.
2167d 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
2167e 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 63 61 73 }.#endif. cas
2167f 65 20 54 4b 5f 56 41 52 49 41 42 4c 45 3a 20 7b e TK_VARIABLE: {
21680 0a 20 20 20 20 20 20 56 64 62 65 4f 70 20 2a 70 . VdbeOp *p
21681 4f 70 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 Op;. assert
21682 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 ( !ExprHasProper
21683 74 79 28 70 45 78 70 72 2c 20 45 50 5f 49 6e 74 ty(pExpr, EP_Int
21684 56 61 6c 75 65 29 20 29 3b 0a 20 20 20 20 20 20 Value) );.
21685 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 75 assert( pExpr->u
21686 2e 7a 54 6f 6b 65 6e 21 3d 30 20 29 3b 0a 20 20 .zToken!=0 );.
21687 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70 assert( pExp
21688 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 30 5d 21 3d r->u.zToken[0]!=
21689 30 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 0 );. if( p
2168a 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 31 Expr->u.zToken[1
2168b 5d 3d 3d 30 0a 20 20 20 20 20 20 20 20 20 26 26 ]==0. &&
2168c 20 28 70 4f 70 20 3d 20 73 71 6c 69 74 65 33 56 (pOp = sqlite3V
2168d 64 62 65 47 65 74 4f 70 28 76 2c 20 2d 31 29 29 dbeGetOp(v, -1))
2168e 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56 61 72 ->opcode==OP_Var
2168f 69 61 62 6c 65 0a 20 20 20 20 20 20 20 20 20 26 iable. &
21690 26 20 70 4f 70 2d 3e 70 31 2b 70 4f 70 2d 3e 70 & pOp->p1+pOp->p
21691 33 3d 3d 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 3==pExpr->iTable
21692 0a 20 20 20 20 20 20 20 20 20 26 26 20 70 4f 70 . && pOp
21693 2d 3e 70 32 2b 70 4f 70 2d 3e 70 33 3d 3d 74 61 ->p2+pOp->p3==ta
21694 72 67 65 74 0a 20 20 20 20 20 20 20 20 20 26 26 rget. &&
21695 20 70 4f 70 2d 3e 70 34 2e 7a 3d 3d 30 0a 20 20 pOp->p4.z==0.
21696 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 2f ){. /
21697 2a 20 49 66 20 74 68 65 20 70 72 65 76 69 6f 75 * If the previou
21698 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 77 61 s instruction wa
21699 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 s a copy of the
2169a 70 72 65 76 69 6f 75 73 20 75 6e 6e 61 6d 65 64 previous unnamed
2169b 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 61 72 61 . ** para
2169c 6d 65 74 65 72 20 69 6e 74 6f 20 74 68 65 20 70 meter into the p
2169d 72 65 76 69 6f 75 73 20 72 65 67 69 73 74 65 72 revious register
2169e 2c 20 74 68 65 6e 20 73 69 6d 70 6c 79 20 69 6e , then simply in
2169f 63 72 65 6d 65 6e 74 20 74 68 65 0a 20 20 20 20 crement the.
216a0 20 20 20 20 2a 2a 20 72 65 70 65 61 74 20 63 6f ** repeat co
216a1 75 6e 74 20 6f 6e 20 74 68 65 20 70 72 69 6f 72 unt on the prior
216a2 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 72 61 74 instruction rat
216a3 68 65 72 20 74 68 61 6e 20 6d 61 6b 69 6e 67 20 her than making
216a4 61 20 6e 65 77 0a 20 20 20 20 20 20 20 20 2a 2a a new. **
216a5 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 20 20 instruction..
216a6 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 */.
216a7 20 70 4f 70 2d 3e 70 33 2b 2b 3b 0a 20 20 20 20 pOp->p3++;.
216a8 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
216a9 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
216aa 70 33 28 76 2c 20 4f 50 5f 56 61 72 69 61 62 6c p3(v, OP_Variabl
216ab 65 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 e, pExpr->iTable
216ac 2c 20 74 61 72 67 65 74 2c 20 31 29 3b 0a 20 20 , target, 1);.
216ad 20 20 20 20 20 20 69 66 28 20 70 45 78 70 72 2d if( pExpr-
216ae 3e 75 2e 7a 54 6f 6b 65 6e 5b 31 5d 21 3d 30 20 >u.zToken[1]!=0
216af 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c ){. sql
216b0 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 ite3VdbeChangeP4
216b1 28 76 2c 20 2d 31 2c 20 70 45 78 70 72 2d 3e 75 (v, -1, pExpr->u
216b2 2e 7a 54 6f 6b 65 6e 2c 20 30 29 3b 0a 20 20 20 .zToken, 0);.
216b3 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 }. }.
216b4 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
216b5 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 52 45 }. case TK_RE
216b6 47 49 53 54 45 52 3a 20 7b 0a 20 20 20 20 20 20 GISTER: {.
216b7 69 6e 52 65 67 20 3d 20 70 45 78 70 72 2d 3e 69 inReg = pExpr->i
216b8 54 61 62 6c 65 3b 0a 20 20 20 20 20 20 62 72 65 Table;. bre
216b9 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 ak;. }. ca
216ba 73 65 20 54 4b 5f 41 53 3a 20 7b 0a 20 20 20 20 se TK_AS: {.
216bb 20 20 69 6e 52 65 67 20 3d 20 63 6f 64 65 41 6c inReg = codeAl
216bc 69 61 73 28 70 50 61 72 73 65 2c 20 70 45 78 70 ias(pParse, pExp
216bd 72 2d 3e 69 54 61 62 6c 65 2c 20 70 45 78 70 72 r->iTable, pExpr
216be 2d 3e 70 4c 65 66 74 2c 20 74 61 72 67 65 74 29 ->pLeft, target)
216bf 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
216c0 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c }.#ifndef SQL
216c1 49 54 45 5f 4f 4d 49 54 5f 43 41 53 54 0a 20 20 ITE_OMIT_CAST.
216c2 20 20 63 61 73 65 20 54 4b 5f 43 41 53 54 3a 20 case TK_CAST:
216c3 7b 0a 20 20 20 20 20 20 2f 2a 20 45 78 70 72 65 {. /* Expre
216c4 73 73 69 6f 6e 73 20 6f 66 20 74 68 65 20 66 6f ssions of the fo
216c5 72 6d 3a 20 20 20 43 41 53 54 28 70 4c 65 66 74 rm: CAST(pLeft
216c6 20 41 53 20 74 6f 6b 65 6e 29 20 2a 2f 0a 20 20 AS token) */.
216c7 20 20 20 20 69 6e 74 20 61 66 66 2c 20 74 6f 5f int aff, to_
216c8 6f 70 3b 0a 20 20 20 20 20 20 69 6e 52 65 67 20 op;. inReg
216c9 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 = sqlite3ExprCod
216ca 65 54 61 72 67 65 74 28 70 50 61 72 73 65 2c 20 eTarget(pParse,
216cb 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 74 61 pExpr->pLeft, ta
216cc 72 67 65 74 29 3b 0a 20 20 20 20 20 20 61 73 73 rget);. ass
216cd 65 72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f ert( !ExprHasPro
216ce 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f perty(pExpr, EP_
216cf 49 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20 20 20 IntValue) );.
216d0 20 20 20 61 66 66 20 3d 20 73 71 6c 69 74 65 33 aff = sqlite3
216d1 41 66 66 69 6e 69 74 79 54 79 70 65 28 70 45 78 AffinityType(pEx
216d2 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0a 20 pr->u.zToken);.
216d3 20 20 20 20 20 74 6f 5f 6f 70 20 3d 20 61 66 66 to_op = aff
216d4 20 2d 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 - SQLITE_AFF_TE
216d5 58 54 20 2b 20 4f 50 5f 54 6f 54 65 78 74 3b 0a XT + OP_ToText;.
216d6 20 20 20 20 20 20 61 73 73 65 72 74 28 20 74 6f assert( to
216d7 5f 6f 70 3d 3d 4f 50 5f 54 6f 54 65 78 74 20 20 _op==OP_ToText
216d8 20 20 7c 7c 20 61 66 66 21 3d 53 51 4c 49 54 45 || aff!=SQLITE
216d9 5f 41 46 46 5f 54 45 58 54 20 20 20 20 29 3b 0a _AFF_TEXT );.
216da 20 20 20 20 20 20 61 73 73 65 72 74 28 20 74 6f assert( to
216db 5f 6f 70 3d 3d 4f 50 5f 54 6f 42 6c 6f 62 20 20 _op==OP_ToBlob
216dc 20 20 7c 7c 20 61 66 66 21 3d 53 51 4c 49 54 45 || aff!=SQLITE
216dd 5f 41 46 46 5f 4e 4f 4e 45 20 20 20 20 29 3b 0a _AFF_NONE );.
216de 20 20 20 20 20 20 61 73 73 65 72 74 28 20 74 6f assert( to
216df 5f 6f 70 3d 3d 4f 50 5f 54 6f 4e 75 6d 65 72 69 _op==OP_ToNumeri
216e0 63 20 7c 7c 20 61 66 66 21 3d 53 51 4c 49 54 45 c || aff!=SQLITE
216e1 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20 29 3b 0a _AFF_NUMERIC );.
216e2 20 20 20 20 20 20 61 73 73 65 72 74 28 20 74 6f assert( to
216e3 5f 6f 70 3d 3d 4f 50 5f 54 6f 49 6e 74 20 20 20 _op==OP_ToInt
216e4 20 20 7c 7c 20 61 66 66 21 3d 53 51 4c 49 54 45 || aff!=SQLITE
216e5 5f 41 46 46 5f 49 4e 54 45 47 45 52 20 29 3b 0a _AFF_INTEGER );.
216e6 20 20 20 20 20 20 61 73 73 65 72 74 28 20 74 6f assert( to
216e7 5f 6f 70 3d 3d 4f 50 5f 54 6f 52 65 61 6c 20 20 _op==OP_ToReal
216e8 20 20 7c 7c 20 61 66 66 21 3d 53 51 4c 49 54 45 || aff!=SQLITE
216e9 5f 41 46 46 5f 52 45 41 4c 20 20 20 20 29 3b 0a _AFF_REAL );.
216ea 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
216eb 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 54 65 78 74 to_op==OP_ToText
216ec 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 );. testca
216ed 73 65 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f se( to_op==OP_To
216ee 42 6c 6f 62 20 29 3b 0a 20 20 20 20 20 20 74 65 Blob );. te
216ef 73 74 63 61 73 65 28 20 74 6f 5f 6f 70 3d 3d 4f stcase( to_op==O
216f0 50 5f 54 6f 4e 75 6d 65 72 69 63 20 29 3b 0a 20 P_ToNumeric );.
216f1 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 74 testcase( t
216f2 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 49 6e 74 20 29 o_op==OP_ToInt )
216f3 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
216f4 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 52 65 ( to_op==OP_ToRe
216f5 61 6c 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 al );. if(
216f6 69 6e 52 65 67 21 3d 74 61 72 67 65 74 20 29 7b inReg!=target ){
216f7 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
216f8 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 VdbeAddOp2(v, OP
216f9 5f 53 43 6f 70 79 2c 20 69 6e 52 65 67 2c 20 74 _SCopy, inReg, t
216fa 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20 arget);.
216fb 69 6e 52 65 67 20 3d 20 74 61 72 67 65 74 3b 0a inReg = target;.
216fc 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 }. sq
216fd 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 lite3VdbeAddOp1(
216fe 76 2c 20 74 6f 5f 6f 70 2c 20 69 6e 52 65 67 29 v, to_op, inReg)
216ff 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
21700 28 20 75 73 65 64 41 73 43 6f 6c 75 6d 6e 43 61 ( usedAsColumnCa
21701 63 68 65 28 70 50 61 72 73 65 2c 20 69 6e 52 65 che(pParse, inRe
21702 67 2c 20 69 6e 52 65 67 29 20 29 3b 0a 20 20 20 g, inReg) );.
21703 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 sqlite3ExprCa
21704 63 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67 cheAffinityChang
21705 65 28 70 50 61 72 73 65 2c 20 69 6e 52 65 67 2c e(pParse, inReg,
21706 20 31 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 1);. break
21707 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f ;. }.#endif /
21708 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 41 * SQLITE_OMIT_CA
21709 53 54 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 54 ST */. case T
2170a 4b 5f 4c 54 3a 0a 20 20 20 20 63 61 73 65 20 54 K_LT:. case T
2170b 4b 5f 4c 45 3a 0a 20 20 20 20 63 61 73 65 20 54 K_LE:. case T
2170c 4b 5f 47 54 3a 0a 20 20 20 20 63 61 73 65 20 54 K_GT:. case T
2170d 4b 5f 47 45 3a 0a 20 20 20 20 63 61 73 65 20 54 K_GE:. case T
2170e 4b 5f 4e 45 3a 0a 20 20 20 20 63 61 73 65 20 54 K_NE:. case T
2170f 4b 5f 45 51 3a 20 7b 0a 20 20 20 20 20 20 61 73 K_EQ: {. as
21710 73 65 72 74 28 20 54 4b 5f 4c 54 3d 3d 4f 50 5f sert( TK_LT==OP_
21711 4c 74 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 Lt );. asse
21712 72 74 28 20 54 4b 5f 4c 45 3d 3d 4f 50 5f 4c 65 rt( TK_LE==OP_Le
21713 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 );. assert
21714 28 20 54 4b 5f 47 54 3d 3d 4f 50 5f 47 74 20 29 ( TK_GT==OP_Gt )
21715 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
21716 54 4b 5f 47 45 3d 3d 4f 50 5f 47 65 20 29 3b 0a TK_GE==OP_Ge );.
21717 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b assert( TK
21718 5f 45 51 3d 3d 4f 50 5f 45 71 20 29 3b 0a 20 20 _EQ==OP_Eq );.
21719 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4e assert( TK_N
2171a 45 3d 3d 4f 50 5f 4e 65 20 29 3b 0a 20 20 20 20 E==OP_Ne );.
2171b 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d testcase( op==
2171c 54 4b 5f 4c 54 20 29 3b 0a 20 20 20 20 20 20 74 TK_LT );. t
2171d 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f estcase( op==TK_
2171e 4c 45 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 LE );. test
2171f 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 47 54 20 case( op==TK_GT
21720 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 );. testcas
21721 65 28 20 6f 70 3d 3d 54 4b 5f 47 45 20 29 3b 0a e( op==TK_GE );.
21722 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
21723 6f 70 3d 3d 54 4b 5f 45 51 20 29 3b 0a 20 20 20 op==TK_EQ );.
21724 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d testcase( op=
21725 3d 54 4b 5f 4e 45 20 29 3b 0a 20 20 20 20 20 20 =TK_NE );.
21726 63 6f 64 65 43 6f 6d 70 61 72 65 4f 70 65 72 61 codeCompareOpera
21727 6e 64 73 28 70 50 61 72 73 65 2c 20 70 45 78 70 nds(pParse, pExp
21728 72 2d 3e 70 4c 65 66 74 2c 20 26 72 31 2c 20 26 r->pLeft, &r1, &
21729 72 65 67 46 72 65 65 31 2c 0a 20 20 20 20 20 20 regFree1,.
2172a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2172b 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78 70 pExp
2172c 72 2d 3e 70 52 69 67 68 74 2c 20 26 72 32 2c 20 r->pRight, &r2,
2172d 26 72 65 67 46 72 65 65 32 29 3b 0a 20 20 20 20 ®Free2);.
2172e 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 28 70 50 codeCompare(pP
2172f 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 arse, pExpr->pLe
21730 66 74 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 ft, pExpr->pRigh
21731 74 2c 20 6f 70 2c 0a 20 20 20 20 20 20 20 20 20 t, op,.
21732 20 20 20 20 20 20 20 20 20 72 31 2c 20 72 32 2c r1, r2,
21733 20 69 6e 52 65 67 2c 20 53 51 4c 49 54 45 5f 53 inReg, SQLITE_S
21734 54 4f 52 45 50 32 29 3b 0a 20 20 20 20 20 20 74 TOREP2);. t
21735 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65 estcase( regFree
21736 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 74 65 1==0 );. te
21737 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65 32 stcase( regFree2
21738 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65 ==0 );. bre
21739 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 ak;. }. ca
2173a 73 65 20 54 4b 5f 49 53 3a 0a 20 20 20 20 63 61 se TK_IS:. ca
2173b 73 65 20 54 4b 5f 49 53 4e 4f 54 3a 20 7b 0a 20 se TK_ISNOT: {.
2173c 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f testcase( o
2173d 70 3d 3d 54 4b 5f 49 53 20 29 3b 0a 20 20 20 20 p==TK_IS );.
2173e 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d testcase( op==
2173f 54 4b 5f 49 53 4e 4f 54 20 29 3b 0a 20 20 20 20 TK_ISNOT );.
21740 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 4f 70 65 codeCompareOpe
21741 72 61 6e 64 73 28 70 50 61 72 73 65 2c 20 70 45 rands(pParse, pE
21742 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 31 2c xpr->pLeft, &r1,
21743 20 26 72 65 67 46 72 65 65 31 2c 0a 20 20 20 20 ®Free1,.
21744 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21745 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 45 pE
21746 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 26 72 32 xpr->pRight, &r2
21747 2c 20 26 72 65 67 46 72 65 65 32 29 3b 0a 20 20 , ®Free2);.
21748 20 20 20 20 6f 70 20 3d 20 28 6f 70 3d 3d 54 4b op = (op==TK
21749 5f 49 53 29 20 3f 20 54 4b 5f 45 51 20 3a 20 54 _IS) ? TK_EQ : T
2174a 4b 5f 4e 45 3b 0a 20 20 20 20 20 20 63 6f 64 65 K_NE;. code
2174b 43 6f 6d 70 61 72 65 28 70 50 61 72 73 65 2c 20 Compare(pParse,
2174c 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 70 45 pExpr->pLeft, pE
2174d 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 6f 70 2c xpr->pRight, op,
2174e 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
2174f 20 20 20 72 31 2c 20 72 32 2c 20 69 6e 52 65 67 r1, r2, inReg
21750 2c 20 53 51 4c 49 54 45 5f 53 54 4f 52 45 50 32 , SQLITE_STOREP2
21751 20 7c 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 45 51 | SQLITE_NULLEQ
21752 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 );. testcas
21753 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29 e( regFree1==0 )
21754 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
21755 28 20 72 65 67 46 72 65 65 32 3d 3d 30 20 29 3b ( regFree2==0 );
21756 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
21757 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f }. case TK_
21758 41 4e 44 3a 0a 20 20 20 20 63 61 73 65 20 54 4b AND:. case TK
21759 5f 4f 52 3a 0a 20 20 20 20 63 61 73 65 20 54 4b _OR:. case TK
2175a 5f 50 4c 55 53 3a 0a 20 20 20 20 63 61 73 65 20 _PLUS:. case
2175b 54 4b 5f 53 54 41 52 3a 0a 20 20 20 20 63 61 73 TK_STAR:. cas
2175c 65 20 54 4b 5f 4d 49 4e 55 53 3a 0a 20 20 20 20 e TK_MINUS:.
2175d 63 61 73 65 20 54 4b 5f 52 45 4d 3a 0a 20 20 20 case TK_REM:.
2175e 20 63 61 73 65 20 54 4b 5f 42 49 54 41 4e 44 3a case TK_BITAND:
2175f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 42 49 54 . case TK_BIT
21760 4f 52 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f OR:. case TK_
21761 53 4c 41 53 48 3a 0a 20 20 20 20 63 61 73 65 20 SLASH:. case
21762 54 4b 5f 4c 53 48 49 46 54 3a 0a 20 20 20 20 63 TK_LSHIFT:. c
21763 61 73 65 20 54 4b 5f 52 53 48 49 46 54 3a 20 0a ase TK_RSHIFT: .
21764 20 20 20 20 63 61 73 65 20 54 4b 5f 43 4f 4e 43 case TK_CONC
21765 41 54 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65 AT: {. asse
21766 72 74 28 20 54 4b 5f 41 4e 44 3d 3d 4f 50 5f 41 rt( TK_AND==OP_A
21767 6e 64 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 nd );. asse
21768 72 74 28 20 54 4b 5f 4f 52 3d 3d 4f 50 5f 4f 72 rt( TK_OR==OP_Or
21769 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 );. assert
2176a 28 20 54 4b 5f 50 4c 55 53 3d 3d 4f 50 5f 41 64 ( TK_PLUS==OP_Ad
2176b 64 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 d );. asser
2176c 74 28 20 54 4b 5f 4d 49 4e 55 53 3d 3d 4f 50 5f t( TK_MINUS==OP_
2176d 53 75 62 74 72 61 63 74 20 29 3b 0a 20 20 20 20 Subtract );.
2176e 20 20 61 73 73 65 72 74 28 20 54 4b 5f 52 45 4d assert( TK_REM
2176f 3d 3d 4f 50 5f 52 65 6d 61 69 6e 64 65 72 20 29 ==OP_Remainder )
21770 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
21771 54 4b 5f 42 49 54 41 4e 44 3d 3d 4f 50 5f 42 69 TK_BITAND==OP_Bi
21772 74 41 6e 64 20 29 3b 0a 20 20 20 20 20 20 61 73 tAnd );. as
21773 73 65 72 74 28 20 54 4b 5f 42 49 54 4f 52 3d 3d sert( TK_BITOR==
21774 4f 50 5f 42 69 74 4f 72 20 29 3b 0a 20 20 20 20 OP_BitOr );.
21775 20 20 61 73 73 65 72 74 28 20 54 4b 5f 53 4c 41 assert( TK_SLA
21776 53 48 3d 3d 4f 50 5f 44 69 76 69 64 65 20 29 3b SH==OP_Divide );
21777 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 . assert( T
21778 4b 5f 4c 53 48 49 46 54 3d 3d 4f 50 5f 53 68 69 K_LSHIFT==OP_Shi
21779 66 74 4c 65 66 74 20 29 3b 0a 20 20 20 20 20 20 ftLeft );.
2177a 61 73 73 65 72 74 28 20 54 4b 5f 52 53 48 49 46 assert( TK_RSHIF
2177b 54 3d 3d 4f 50 5f 53 68 69 66 74 52 69 67 68 74 T==OP_ShiftRight
2177c 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 );. assert
2177d 28 20 54 4b 5f 43 4f 4e 43 41 54 3d 3d 4f 50 5f ( TK_CONCAT==OP_
2177e 43 6f 6e 63 61 74 20 29 3b 0a 20 20 20 20 20 20 Concat );.
2177f 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b testcase( op==TK
21780 5f 41 4e 44 20 29 3b 0a 20 20 20 20 20 20 74 65 _AND );. te
21781 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4f stcase( op==TK_O
21782 52 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 R );. testc
21783 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 50 4c 55 53 ase( op==TK_PLUS
21784 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 );. testca
21785 73 65 28 20 6f 70 3d 3d 54 4b 5f 4d 49 4e 55 53 se( op==TK_MINUS
21786 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 );. testca
21787 73 65 28 20 6f 70 3d 3d 54 4b 5f 52 45 4d 20 29 se( op==TK_REM )
21788 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
21789 28 20 6f 70 3d 3d 54 4b 5f 42 49 54 41 4e 44 20 ( op==TK_BITAND
2178a 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 );. testcas
2178b 65 28 20 6f 70 3d 3d 54 4b 5f 42 49 54 4f 52 20 e( op==TK_BITOR
2178c 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 );. testcas
2178d 65 28 20 6f 70 3d 3d 54 4b 5f 53 4c 41 53 48 20 e( op==TK_SLASH
2178e 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 );. testcas
2178f 65 28 20 6f 70 3d 3d 54 4b 5f 4c 53 48 49 46 54 e( op==TK_LSHIFT
21790 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 );. testca
21791 73 65 28 20 6f 70 3d 3d 54 4b 5f 52 53 48 49 46 se( op==TK_RSHIF
21792 54 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 T );. testc
21793 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 43 4f 4e 43 ase( op==TK_CONC
21794 41 54 20 29 3b 0a 20 20 20 20 20 20 72 31 20 3d AT );. r1 =
21795 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 sqlite3ExprCode
21796 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70 45 78 Temp(pParse, pEx
21797 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 65 67 46 pr->pLeft, ®F
21798 72 65 65 31 29 3b 0a 20 20 20 20 20 20 72 32 20 ree1);. r2
21799 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 = sqlite3ExprCod
2179a 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70 45 eTemp(pParse, pE
2179b 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 26 72 65 xpr->pRight, &re
2179c 67 46 72 65 65 32 29 3b 0a 20 20 20 20 20 20 73 gFree2);. s
2179d 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 qlite3VdbeAddOp3
2179e 28 76 2c 20 6f 70 2c 20 72 32 2c 20 72 31 2c 20 (v, op, r2, r1,
2179f 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 74 target);. t
217a0 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65 estcase( regFree
217a1 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 74 65 1==0 );. te
217a2 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65 32 stcase( regFree2
217a3 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65 ==0 );. bre
217a4 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 ak;. }. ca
217a5 73 65 20 54 4b 5f 55 4d 49 4e 55 53 3a 20 7b 0a se TK_UMINUS: {.
217a6 20 20 20 20 20 20 45 78 70 72 20 2a 70 4c 65 66 Expr *pLef
217a7 74 20 3d 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 t = pExpr->pLeft
217a8 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
217a9 70 4c 65 66 74 20 29 3b 0a 20 20 20 20 20 20 69 pLeft );. i
217aa 66 28 20 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b f( pLeft->op==TK
217ab 5f 46 4c 4f 41 54 20 29 7b 0a 20 20 20 20 20 20 _FLOAT ){.
217ac 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48 assert( !ExprH
217ad 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 asProperty(pExpr
217ae 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 29 , EP_IntValue) )
217af 3b 0a 20 20 20 20 20 20 20 20 63 6f 64 65 52 65 ;. codeRe
217b0 61 6c 28 76 2c 20 70 4c 65 66 74 2d 3e 75 2e 7a al(v, pLeft->u.z
217b1 54 6f 6b 65 6e 2c 20 31 2c 20 74 61 72 67 65 74 Token, 1, target
217b2 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 );. }else i
217b3 66 28 20 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b f( pLeft->op==TK
217b4 5f 49 4e 54 45 47 45 52 20 29 7b 0a 20 20 20 20 _INTEGER ){.
217b5 20 20 20 20 63 6f 64 65 49 6e 74 65 67 65 72 28 codeInteger(
217b6 76 2c 20 70 4c 65 66 74 2c 20 31 2c 20 74 61 72 v, pLeft, 1, tar
217b7 67 65 74 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 get);. }els
217b8 65 7b 0a 20 20 20 20 20 20 20 20 72 65 67 46 72 e{. regFr
217b9 65 65 31 20 3d 20 72 31 20 3d 20 73 71 6c 69 74 ee1 = r1 = sqlit
217ba 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 e3GetTempReg(pPa
217bb 72 73 65 29 3b 0a 20 20 20 20 20 20 20 20 73 71 rse);. sq
217bc 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 lite3VdbeAddOp2(
217bd 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 v, OP_Integer, 0
217be 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 20 20 72 , r1);. r
217bf 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 2 = sqlite3ExprC
217c0 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20 odeTemp(pParse,
217c1 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 pExpr->pLeft, &r
217c2 65 67 46 72 65 65 32 29 3b 0a 20 20 20 20 20 20 egFree2);.
217c3 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
217c4 4f 70 33 28 76 2c 20 4f 50 5f 53 75 62 74 72 61 Op3(v, OP_Subtra
217c5 63 74 2c 20 72 32 2c 20 72 31 2c 20 74 61 72 67 ct, r2, r1, targ
217c6 65 74 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73 et);. tes
217c7 74 63 61 73 65 28 20 72 65 67 46 72 65 65 32 3d tcase( regFree2=
217c8 3d 30 20 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 =0 );. }.
217c9 20 20 20 20 69 6e 52 65 67 20 3d 20 74 61 72 67 inReg = targ
217ca 65 74 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b et;. break;
217cb 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 . }. case
217cc 54 4b 5f 42 49 54 4e 4f 54 3a 0a 20 20 20 20 63 TK_BITNOT:. c
217cd 61 73 65 20 54 4b 5f 4e 4f 54 3a 20 7b 0a 20 20 ase TK_NOT: {.
217ce 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 42 assert( TK_B
217cf 49 54 4e 4f 54 3d 3d 4f 50 5f 42 69 74 4e 6f 74 ITNOT==OP_BitNot
217d0 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 );. assert
217d1 28 20 54 4b 5f 4e 4f 54 3d 3d 4f 50 5f 4e 6f 74 ( TK_NOT==OP_Not
217d2 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 );. testca
217d3 73 65 28 20 6f 70 3d 3d 54 4b 5f 42 49 54 4e 4f se( op==TK_BITNO
217d4 54 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 T );. testc
217d5 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4e 4f 54 20 ase( op==TK_NOT
217d6 29 3b 0a 20 20 20 20 20 20 72 31 20 3d 20 73 71 );. r1 = sq
217d7 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d lite3ExprCodeTem
217d8 70 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d p(pParse, pExpr-
217d9 3e 70 4c 65 66 74 2c 20 26 72 65 67 46 72 65 65 >pLeft, ®Free
217da 31 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 1);. testca
217db 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20 se( regFree1==0
217dc 29 3b 0a 20 20 20 20 20 20 69 6e 52 65 67 20 3d );. inReg =
217dd 20 74 61 72 67 65 74 3b 0a 20 20 20 20 20 20 73 target;. s
217de 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 qlite3VdbeAddOp2
217df 28 76 2c 20 6f 70 2c 20 72 31 2c 20 69 6e 52 65 (v, op, r1, inRe
217e0 67 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b g);. break;
217e1 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 . }. case
217e2 54 4b 5f 49 53 4e 55 4c 4c 3a 0a 20 20 20 20 63 TK_ISNULL:. c
217e3 61 73 65 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 3a 20 ase TK_NOTNULL:
217e4 7b 0a 20 20 20 20 20 20 69 6e 74 20 61 64 64 72 {. int addr
217e5 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
217e6 54 4b 5f 49 53 4e 55 4c 4c 3d 3d 4f 50 5f 49 73 TK_ISNULL==OP_Is
217e7 4e 75 6c 6c 20 29 3b 0a 20 20 20 20 20 20 61 73 Null );. as
217e8 73 65 72 74 28 20 54 4b 5f 4e 4f 54 4e 55 4c 4c sert( TK_NOTNULL
217e9 3d 3d 4f 50 5f 4e 6f 74 4e 75 6c 6c 20 29 3b 0a ==OP_NotNull );.
217ea 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
217eb 6f 70 3d 3d 54 4b 5f 49 53 4e 55 4c 4c 20 29 3b op==TK_ISNULL );
217ec 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
217ed 20 6f 70 3d 3d 54 4b 5f 4e 4f 54 4e 55 4c 4c 20 op==TK_NOTNULL
217ee 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
217ef 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 VdbeAddOp2(v, OP
217f0 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 74 61 72 _Integer, 1, tar
217f1 67 65 74 29 3b 0a 20 20 20 20 20 20 72 31 20 3d get);. r1 =
217f2 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 sqlite3ExprCode
217f3 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70 45 78 Temp(pParse, pEx
217f4 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 65 67 46 pr->pLeft, ®F
217f5 72 65 65 31 29 3b 0a 20 20 20 20 20 20 74 65 73 ree1);. tes
217f6 74 63 61 73 65 28 20 72 65 67 46 72 65 65 31 3d tcase( regFree1=
217f7 3d 30 20 29 3b 0a 20 20 20 20 20 20 61 64 64 72 =0 );. addr
217f8 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 = sqlite3VdbeAd
217f9 64 4f 70 31 28 76 2c 20 6f 70 2c 20 72 31 29 3b dOp1(v, op, r1);
217fa 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
217fb 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 beAddOp2(v, OP_A
217fc 64 64 49 6d 6d 2c 20 74 61 72 67 65 74 2c 20 2d ddImm, target, -
217fd 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 1);. sqlite
217fe 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 3VdbeJumpHere(v,
217ff 20 61 64 64 72 29 3b 0a 20 20 20 20 20 20 62 72 addr);. br
21800 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 eak;. }. c
21801 61 73 65 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 ase TK_AGG_FUNCT
21802 49 4f 4e 3a 20 7b 0a 20 20 20 20 20 20 41 67 67 ION: {. Agg
21803 49 6e 66 6f 20 2a 70 49 6e 66 6f 20 3d 20 70 45 Info *pInfo = pE
21804 78 70 72 2d 3e 70 41 67 67 49 6e 66 6f 3b 0a 20 xpr->pAggInfo;.
21805 20 20 20 20 20 69 66 28 20 70 49 6e 66 6f 3d 3d if( pInfo==
21806 30 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 0 ){. ass
21807 65 72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f ert( !ExprHasPro
21808 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f perty(pExpr, EP_
21809 49 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20 20 20 IntValue) );.
2180a 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f sqlite3Erro
2180b 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6d 69 rMsg(pParse, "mi
2180c 73 75 73 65 20 6f 66 20 61 67 67 72 65 67 61 74 suse of aggregat
2180d 65 3a 20 25 73 28 29 22 2c 20 70 45 78 70 72 2d e: %s()", pExpr-
2180e 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 >u.zToken);.
2180f 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
21810 20 69 6e 52 65 67 20 3d 20 70 49 6e 66 6f 2d 3e inReg = pInfo->
21811 61 46 75 6e 63 5b 70 45 78 70 72 2d 3e 69 41 67 aFunc[pExpr->iAg
21812 67 5d 2e 69 4d 65 6d 3b 0a 20 20 20 20 20 20 7d g].iMem;. }
21813 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
21814 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f }. case TK_
21815 43 4f 4e 53 54 5f 46 55 4e 43 3a 0a 20 20 20 20 CONST_FUNC:.
21816 63 61 73 65 20 54 4b 5f 46 55 4e 43 54 49 4f 4e case TK_FUNCTION
21817 3a 20 7b 0a 20 20 20 20 20 20 45 78 70 72 4c 69 : {. ExprLi
21818 73 74 20 2a 70 46 61 72 67 3b 20 20 20 20 20 20 st *pFarg;
21819 20 2f 2a 20 4c 69 73 74 20 6f 66 20 66 75 6e 63 /* List of func
2181a 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 73 20 2a tion arguments *
2181b 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 46 61 72 /. int nFar
2181c 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f g; /
2181d 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 75 6e 63 * Number of func
2181e 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 73 20 2a tion arguments *
2181f 2f 0a 20 20 20 20 20 20 46 75 6e 63 44 65 66 20 /. FuncDef
21820 2a 70 44 65 66 3b 20 20 20 20 20 20 20 20 20 2f *pDef; /
21821 2a 20 54 68 65 20 66 75 6e 63 74 69 6f 6e 20 64 * The function d
21822 65 66 69 6e 69 74 69 6f 6e 20 6f 62 6a 65 63 74 efinition object
21823 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 49 */. int nI
21824 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 d;
21825 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 74 68 /* Length of th
21826 65 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 e function name
21827 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 20 20 in bytes */.
21828 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49 const char *zI
21829 64 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 d; /* The
2182a 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f function name */
2182b 0a 20 20 20 20 20 20 69 6e 74 20 63 6f 6e 73 74 . int const
2182c 4d 61 73 6b 20 3d 20 30 3b 20 20 20 20 20 2f 2a Mask = 0; /*
2182d 20 4d 61 73 6b 20 6f 66 20 66 75 6e 63 74 69 6f Mask of functio
2182e 6e 20 61 72 67 75 6d 65 6e 74 73 20 74 68 61 74 n arguments that
2182f 20 61 72 65 20 63 6f 6e 73 74 61 6e 74 20 2a 2f are constant */
21830 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 20 20 20 . int i;
21831 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
21832 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f Loop counter */
21833 0a 20 20 20 20 20 20 75 38 20 65 6e 63 20 3d 20 . u8 enc =
21834 45 4e 43 28 64 62 29 3b 20 20 20 20 20 20 2f 2a ENC(db); /*
21835 20 54 68 65 20 74 65 78 74 20 65 6e 63 6f 64 69 The text encodi
21836 6e 67 20 75 73 65 64 20 62 79 20 74 68 69 73 20 ng used by this
21837 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 20 20 database */.
21838 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c CollSeq *pColl
21839 20 3d 20 30 3b 20 20 20 20 2f 2a 20 41 20 63 6f = 0; /* A co
2183a 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 llating sequence
2183b 20 2a 2f 0a 0a 20 20 20 20 20 20 61 73 73 65 72 */.. asser
2183c 74 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 t( !ExprHasPrope
2183d 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49 rty(pExpr, EP_xI
2183e 73 53 65 6c 65 63 74 29 20 29 3b 0a 20 20 20 20 sSelect) );.
2183f 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d testcase( op==
21840 54 4b 5f 43 4f 4e 53 54 5f 46 55 4e 43 20 29 3b TK_CONST_FUNC );
21841 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
21842 20 6f 70 3d 3d 54 4b 5f 46 55 4e 43 54 49 4f 4e op==TK_FUNCTION
21843 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 45 78 );. if( Ex
21844 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 prHasAnyProperty
21845 28 70 45 78 70 72 2c 20 45 50 5f 54 6f 6b 65 6e (pExpr, EP_Token
21846 4f 6e 6c 79 29 20 29 7b 0a 20 20 20 20 20 20 20 Only) ){.
21847 20 70 46 61 72 67 20 3d 20 30 3b 0a 20 20 20 20 pFarg = 0;.
21848 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
21849 20 70 46 61 72 67 20 3d 20 70 45 78 70 72 2d 3e pFarg = pExpr->
2184a 78 2e 70 4c 69 73 74 3b 0a 20 20 20 20 20 20 7d x.pList;. }
2184b 0a 20 20 20 20 20 20 6e 46 61 72 67 20 3d 20 70 . nFarg = p
2184c 46 61 72 67 20 3f 20 70 46 61 72 67 2d 3e 6e 45 Farg ? pFarg->nE
2184d 78 70 72 20 3a 20 30 3b 0a 20 20 20 20 20 20 61 xpr : 0;. a
2184e 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73 50 ssert( !ExprHasP
2184f 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 roperty(pExpr, E
21850 50 5f 49 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20 P_IntValue) );.
21851 20 20 20 20 20 7a 49 64 20 3d 20 70 45 78 70 72 zId = pExpr
21852 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20 ->u.zToken;.
21853 20 20 6e 49 64 20 3d 20 73 71 6c 69 74 65 33 53 nId = sqlite3S
21854 74 72 6c 65 6e 33 30 28 7a 49 64 29 3b 0a 20 20 trlen30(zId);.
21855 20 20 20 20 70 44 65 66 20 3d 20 73 71 6c 69 74 pDef = sqlit
21856 65 33 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28 64 e3FindFunction(d
21857 62 2c 20 7a 49 64 2c 20 6e 49 64 2c 20 6e 46 61 b, zId, nId, nFa
21858 72 67 2c 20 65 6e 63 2c 20 30 29 3b 0a 20 20 20 rg, enc, 0);.
21859 20 20 20 69 66 28 20 70 44 65 66 3d 3d 30 20 29 if( pDef==0 )
2185a 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
2185b 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 3ErrorMsg(pParse
2185c 2c 20 22 75 6e 6b 6e 6f 77 6e 20 66 75 6e 63 74 , "unknown funct
2185d 69 6f 6e 3a 20 25 2e 2a 73 28 29 22 2c 20 6e 49 ion: %.*s()", nI
2185e 64 2c 20 7a 49 64 29 3b 0a 20 20 20 20 20 20 20 d, zId);.
2185f 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a break;. }.
21860 20 20 20 20 20 20 69 66 28 20 70 46 61 72 67 20 if( pFarg
21861 29 7b 0a 20 20 20 20 20 20 20 20 72 31 20 3d 20 ){. r1 =
21862 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 61 sqlite3GetTempRa
21863 6e 67 65 28 70 50 61 72 73 65 2c 20 6e 46 61 72 nge(pParse, nFar
21864 67 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 g);. sqli
21865 74 65 33 45 78 70 72 43 61 63 68 65 50 75 73 68 te3ExprCachePush
21866 28 70 50 61 72 73 65 29 3b 20 20 20 20 20 2f 2a (pParse); /*
21867 20 54 69 63 6b 65 74 20 32 65 61 32 34 32 35 64 Ticket 2ea2425d
21868 33 34 62 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 34be */.
21869 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 45 sqlite3ExprCodeE
2186a 78 70 72 4c 69 73 74 28 70 50 61 72 73 65 2c 20 xprList(pParse,
2186b 70 46 61 72 67 2c 20 72 31 2c 20 31 29 3b 0a 20 pFarg, r1, 1);.
2186c 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 sqlite3Ex
2186d 70 72 43 61 63 68 65 50 6f 70 28 70 50 61 72 73 prCachePop(pPars
2186e 65 2c 20 31 29 3b 20 20 20 2f 2a 20 54 69 63 6b e, 1); /* Tick
2186f 65 74 20 32 65 61 32 34 32 35 64 33 34 62 65 20 et 2ea2425d34be
21870 2a 2f 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a */. }else{.
21871 20 20 20 20 20 20 20 20 72 31 20 3d 20 30 3b 0a r1 = 0;.
21872 20 20 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 }.#ifndef
21873 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 SQLITE_OMIT_VIRT
21874 55 41 4c 54 41 42 4c 45 0a 20 20 20 20 20 20 2f UALTABLE. /
21875 2a 20 50 6f 73 73 69 62 6c 79 20 6f 76 65 72 6c * Possibly overl
21876 6f 61 64 20 74 68 65 20 66 75 6e 63 74 69 6f 6e oad the function
21877 20 69 66 20 74 68 65 20 66 69 72 73 74 20 61 72 if the first ar
21878 67 75 6d 65 6e 74 20 69 73 0a 20 20 20 20 20 20 gument is.
21879 2a 2a 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 ** a virtual tab
2187a 6c 65 20 63 6f 6c 75 6d 6e 2e 0a 20 20 20 20 20 le column..
2187b 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 46 6f 72 **. ** For
2187c 20 69 6e 66 69 78 20 66 75 6e 63 74 69 6f 6e 73 infix functions
2187d 20 28 4c 49 4b 45 2c 20 47 4c 4f 42 2c 20 52 45 (LIKE, GLOB, RE
2187e 47 45 58 50 2c 20 61 6e 64 20 4d 41 54 43 48 29 GEXP, and MATCH)
2187f 20 75 73 65 20 74 68 65 0a 20 20 20 20 20 20 2a use the. *
21880 2a 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e * second argumen
21881 74 2c 20 6e 6f 74 20 74 68 65 20 66 69 72 73 74 t, not the first
21882 2c 20 61 73 20 74 68 65 20 61 72 67 75 6d 65 6e , as the argumen
21883 74 20 74 6f 20 74 65 73 74 20 74 6f 0a 20 20 20 t to test to.
21884 20 20 20 2a 2a 20 73 65 65 20 69 66 20 69 74 20 ** see if it
21885 69 73 20 61 20 63 6f 6c 75 6d 6e 20 69 6e 20 61 is a column in a
21886 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 20 virtual table.
21887 20 54 68 69 73 20 69 73 20 64 6f 6e 65 20 62 65 This is done be
21888 63 61 75 73 65 0a 20 20 20 20 20 20 2a 2a 20 74 cause. ** t
21889 68 65 20 6c 65 66 74 20 6f 70 65 72 61 6e 64 20 he left operand
2188a 6f 66 20 69 6e 66 69 78 20 66 75 6e 63 74 69 6f of infix functio
2188b 6e 73 20 28 74 68 65 20 6f 70 65 72 61 6e 64 20 ns (the operand
2188c 77 65 20 77 61 6e 74 20 74 6f 0a 20 20 20 20 20 we want to.
2188d 20 2a 2a 20 63 6f 6e 74 72 6f 6c 20 6f 76 65 72 ** control over
2188e 6c 6f 61 64 69 6e 67 29 20 65 6e 64 73 20 75 70 loading) ends up
2188f 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61 as the second a
21890 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 0a 20 rgument to the.
21891 20 20 20 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e ** function
21892 2e 20 20 54 68 65 20 65 78 70 72 65 73 73 69 6f . The expressio
21893 6e 20 22 41 20 67 6c 6f 62 20 42 22 20 69 73 20 n "A glob B" is
21894 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 20 0a 20 equivalent to .
21895 20 20 20 20 20 2a 2a 20 22 67 6c 6f 62 28 42 2c ** "glob(B,
21896 41 29 2e 20 20 57 65 20 77 61 6e 74 20 74 6f 20 A). We want to
21897 75 73 65 20 74 68 65 20 41 20 69 6e 20 22 41 20 use the A in "A
21898 67 6c 6f 62 20 42 22 20 74 6f 20 74 65 73 74 0a glob B" to test.
21899 20 20 20 20 20 20 2a 2a 20 66 6f 72 20 66 75 6e ** for fun
2189a 63 74 69 6f 6e 20 6f 76 65 72 6c 6f 61 64 69 6e ction overloadin
2189b 67 2e 20 20 42 75 74 20 77 65 20 75 73 65 20 74 g. But we use t
2189c 68 65 20 42 20 74 65 72 6d 20 69 6e 20 22 67 6c he B term in "gl
2189d 6f 62 28 42 2c 41 29 22 2e 0a 20 20 20 20 20 20 ob(B,A)"..
2189e 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 6e 46 61 */. if( nFa
2189f 72 67 3e 3d 32 20 26 26 20 28 70 45 78 70 72 2d rg>=2 && (pExpr-
218a0 3e 66 6c 61 67 73 20 26 20 45 50 5f 49 6e 66 69 >flags & EP_Infi
218a1 78 46 75 6e 63 29 20 29 7b 0a 20 20 20 20 20 20 xFunc) ){.
218a2 20 20 70 44 65 66 20 3d 20 73 71 6c 69 74 65 33 pDef = sqlite3
218a3 56 74 61 62 4f 76 65 72 6c 6f 61 64 46 75 6e 63 VtabOverloadFunc
218a4 74 69 6f 6e 28 64 62 2c 20 70 44 65 66 2c 20 6e tion(db, pDef, n
218a5 46 61 72 67 2c 20 70 46 61 72 67 2d 3e 61 5b 31 Farg, pFarg->a[1
218a6 5d 2e 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20 ].pExpr);.
218a7 7d 65 6c 73 65 20 69 66 28 20 6e 46 61 72 67 3e }else if( nFarg>
218a8 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 44 65 0 ){. pDe
218a9 66 20 3d 20 73 71 6c 69 74 65 33 56 74 61 62 4f f = sqlite3VtabO
218aa 76 65 72 6c 6f 61 64 46 75 6e 63 74 69 6f 6e 28 verloadFunction(
218ab 64 62 2c 20 70 44 65 66 2c 20 6e 46 61 72 67 2c db, pDef, nFarg,
218ac 20 70 46 61 72 67 2d 3e 61 5b 30 5d 2e 70 45 78 pFarg->a[0].pEx
218ad 70 72 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e pr);. }.#en
218ae 64 69 66 0a 20 20 20 20 20 20 66 6f 72 28 69 3d dif. for(i=
218af 30 3b 20 69 3c 6e 46 61 72 67 3b 20 69 2b 2b 29 0; i<nFarg; i++)
218b0 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 3c {. if( i<
218b1 33 32 20 26 26 20 73 71 6c 69 74 65 33 45 78 70 32 && sqlite3Exp
218b2 72 49 73 43 6f 6e 73 74 61 6e 74 28 70 46 61 72 rIsConstant(pFar
218b3 67 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 29 20 29 g->a[i].pExpr) )
218b4 7b 0a 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 {. cons
218b5 74 4d 61 73 6b 20 7c 3d 20 28 31 3c 3c 69 29 3b tMask |= (1<<i);
218b6 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
218b7 20 20 20 69 66 28 20 28 70 44 65 66 2d 3e 66 6c if( (pDef->fl
218b8 61 67 73 20 26 20 53 51 4c 49 54 45 5f 46 55 4e ags & SQLITE_FUN
218b9 43 5f 4e 45 45 44 43 4f 4c 4c 29 21 3d 30 20 26 C_NEEDCOLL)!=0 &
218ba 26 20 21 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 & !pColl ){.
218bb 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71 pColl = sq
218bc 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 71 lite3ExprCollSeq
218bd 28 70 50 61 72 73 65 2c 20 70 46 61 72 67 2d 3e (pParse, pFarg->
218be 61 5b 69 5d 2e 70 45 78 70 72 29 3b 0a 20 20 20 a[i].pExpr);.
218bf 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 }. }.
218c0 20 20 20 20 20 69 66 28 20 70 44 65 66 2d 3e 66 if( pDef->f
218c1 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 46 55 lags & SQLITE_FU
218c2 4e 43 5f 4e 45 45 44 43 4f 4c 4c 20 29 7b 0a 20 NC_NEEDCOLL ){.
218c3 20 20 20 20 20 20 20 69 66 28 20 21 70 43 6f 6c if( !pCol
218c4 6c 20 29 20 70 43 6f 6c 6c 20 3d 20 64 62 2d 3e l ) pColl = db->
218c5 70 44 66 6c 74 43 6f 6c 6c 3b 20 0a 20 20 20 20 pDfltColl; .
218c6 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
218c7 64 64 4f 70 34 28 76 2c 20 4f 50 5f 43 6f 6c 6c ddOp4(v, OP_Coll
218c8 53 65 71 2c 20 30 2c 20 30 2c 20 30 2c 20 28 63 Seq, 0, 0, 0, (c
218c9 68 61 72 20 2a 29 70 43 6f 6c 6c 2c 20 50 34 5f har *)pColl, P4_
218ca 43 4f 4c 4c 53 45 51 29 3b 0a 20 20 20 20 20 20 COLLSEQ);.
218cb 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 }. sqlite3V
218cc 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f dbeAddOp4(v, OP_
218cd 46 75 6e 63 74 69 6f 6e 2c 20 63 6f 6e 73 74 4d Function, constM
218ce 61 73 6b 2c 20 72 31 2c 20 74 61 72 67 65 74 2c ask, r1, target,
218cf 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
218d0 20 20 20 20 20 20 20 20 20 28 63 68 61 72 2a 29 (char*)
218d1 70 44 65 66 2c 20 50 34 5f 46 55 4e 43 44 45 46 pDef, P4_FUNCDEF
218d2 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
218d3 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 VdbeChangeP5(v,
218d4 28 75 38 29 6e 46 61 72 67 29 3b 0a 20 20 20 20 (u8)nFarg);.
218d5 20 20 69 66 28 20 6e 46 61 72 67 20 29 7b 0a 20 if( nFarg ){.
218d6 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 sqlite3Re
218d7 6c 65 61 73 65 54 65 6d 70 52 61 6e 67 65 28 70 leaseTempRange(p
218d8 50 61 72 73 65 2c 20 72 31 2c 20 6e 46 61 72 67 Parse, r1, nFarg
218d9 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
218da 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 sqlite3ExprCach
218db 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 eAffinityChange(
218dc 70 50 61 72 73 65 2c 20 72 31 2c 20 6e 46 61 72 pParse, r1, nFar
218dd 67 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b g);. break;
218de 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 . }.#ifndef S
218df 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 QLITE_OMIT_SUBQU
218e0 45 52 59 0a 20 20 20 20 63 61 73 65 20 54 4b 5f ERY. case TK_
218e1 45 58 49 53 54 53 3a 0a 20 20 20 20 63 61 73 65 EXISTS:. case
218e2 20 54 4b 5f 53 45 4c 45 43 54 3a 20 7b 0a 20 20 TK_SELECT: {.
218e3 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 testcase( op
218e4 3d 3d 54 4b 5f 45 58 49 53 54 53 20 29 3b 0a 20 ==TK_EXISTS );.
218e5 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f testcase( o
218e6 70 3d 3d 54 4b 5f 53 45 4c 45 43 54 20 29 3b 0a p==TK_SELECT );.
218e7 20 20 20 20 20 20 73 71 6c 69 74 65 33 43 6f 64 sqlite3Cod
218e8 65 53 75 62 73 65 6c 65 63 74 28 70 50 61 72 73 eSubselect(pPars
218e9 65 2c 20 70 45 78 70 72 2c 20 30 2c 20 30 29 3b e, pExpr, 0, 0);
218ea 0a 20 20 20 20 20 20 69 6e 52 65 67 20 3d 20 70 . inReg = p
218eb 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 Expr->iColumn;.
218ec 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
218ed 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 4e }. case TK_IN
218ee 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 4e : {. int rN
218ef 6f 74 46 6f 75 6e 64 20 3d 20 30 3b 0a 20 20 20 otFound = 0;.
218f0 20 20 20 69 6e 74 20 72 4d 61 79 48 61 76 65 4e int rMayHaveN
218f1 75 6c 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 ull = 0;. i
218f2 6e 74 20 6a 32 2c 20 6a 33 2c 20 6a 34 2c 20 6a nt j2, j3, j4, j
218f3 35 3b 0a 20 20 20 20 20 20 63 68 61 72 20 61 66 5;. char af
218f4 66 69 6e 69 74 79 3b 0a 20 20 20 20 20 20 69 6e finity;. in
218f5 74 20 65 54 79 70 65 3b 0a 0a 20 20 20 20 20 20 t eType;..
218f6 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 VdbeNoopComment(
218f7 28 76 2c 20 22 62 65 67 69 6e 20 49 4e 20 65 78 (v, "begin IN ex
218f8 70 72 20 72 25 64 22 2c 20 74 61 72 67 65 74 29 pr r%d", target)
218f9 29 3b 0a 20 20 20 20 20 20 65 54 79 70 65 20 3d );. eType =
218fa 20 73 71 6c 69 74 65 33 46 69 6e 64 49 6e 49 6e sqlite3FindInIn
218fb 64 65 78 28 70 50 61 72 73 65 2c 20 70 45 78 70 dex(pParse, pExp
218fc 72 2c 20 26 72 4d 61 79 48 61 76 65 4e 75 6c 6c r, &rMayHaveNull
218fd 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 4d 61 );. if( rMa
218fe 79 48 61 76 65 4e 75 6c 6c 20 29 7b 0a 20 20 20 yHaveNull ){.
218ff 20 20 20 20 20 72 4e 6f 74 46 6f 75 6e 64 20 3d rNotFound =
21900 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b ++pParse->nMem;
21901 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 . }..
21902 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20 74 68 /* Figure out th
21903 65 20 61 66 66 69 6e 69 74 79 20 74 6f 20 75 73 e affinity to us
21904 65 20 74 6f 20 63 72 65 61 74 65 20 61 20 6b 65 e to create a ke
21905 79 20 66 72 6f 6d 20 74 68 65 20 72 65 73 75 6c y from the resul
21906 74 73 0a 20 20 20 20 20 20 2a 2a 20 6f 66 20 74 ts. ** of t
21907 68 65 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 61 he expression. a
21908 66 66 69 6e 69 74 79 53 74 72 20 73 74 6f 72 65 ffinityStr store
21909 73 20 61 20 73 74 61 74 69 63 20 73 74 72 69 6e s a static strin
2190a 67 20 73 75 69 74 61 62 6c 65 20 66 6f 72 0a 20 g suitable for.
2190b 20 20 20 20 20 2a 2a 20 50 34 20 6f 66 20 4f 50 ** P4 of OP
2190c 5f 4d 61 6b 65 52 65 63 6f 72 64 2e 0a 20 20 20 _MakeRecord..
2190d 20 20 20 2a 2f 0a 20 20 20 20 20 20 61 66 66 69 */. affi
2190e 6e 69 74 79 20 3d 20 63 6f 6d 70 61 72 69 73 6f nity = compariso
2190f 6e 41 66 66 69 6e 69 74 79 28 70 45 78 70 72 29 nAffinity(pExpr)
21910 3b 0a 0a 0a 20 20 20 20 20 20 2f 2a 20 43 6f 64 ;... /* Cod
21911 65 20 74 68 65 20 3c 65 78 70 72 3e 20 66 72 6f e the <expr> fro
21912 6d 20 22 3c 65 78 70 72 3e 20 49 4e 20 28 2e 2e m "<expr> IN (..
21913 2e 29 22 2e 20 54 68 65 20 74 65 6d 70 6f 72 61 .)". The tempora
21914 72 79 20 74 61 62 6c 65 0a 20 20 20 20 20 20 2a ry table. *
21915 2a 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 * pExpr->iTable
21916 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 76 61 6c contains the val
21917 75 65 73 20 74 68 61 74 20 6d 61 6b 65 20 75 70 ues that make up
21918 20 74 68 65 20 28 2e 2e 2e 29 20 73 65 74 2e 0a the (...) set..
21919 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 */. s
2191a 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50 qlite3ExprCacheP
2191b 75 73 68 28 70 50 61 72 73 65 29 3b 0a 20 20 20 ush(pParse);.
2191c 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f sqlite3ExprCo
2191d 64 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 de(pParse, pExpr
2191e 2d 3e 70 4c 65 66 74 2c 20 74 61 72 67 65 74 29 ->pLeft, target)
2191f 3b 0a 20 20 20 20 20 20 6a 32 20 3d 20 73 71 6c ;. j2 = sql
21920 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 ite3VdbeAddOp1(v
21921 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 74 61 72 , OP_IsNull, tar
21922 67 65 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 get);. if(
21923 65 54 79 70 65 3d 3d 49 4e 5f 49 4e 44 45 58 5f eType==IN_INDEX_
21924 52 4f 57 49 44 20 29 7b 0a 20 20 20 20 20 20 20 ROWID ){.
21925 20 6a 33 20 3d 20 73 71 6c 69 74 65 33 56 64 62 j3 = sqlite3Vdb
21926 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4d 75 eAddOp1(v, OP_Mu
21927 73 74 42 65 49 6e 74 2c 20 74 61 72 67 65 74 29 stBeInt, target)
21928 3b 0a 20 20 20 20 20 20 20 20 6a 34 20 3d 20 73 ;. j4 = s
21929 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 qlite3VdbeAddOp3
2192a 28 76 2c 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 (v, OP_NotExists
2192b 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 2c , pExpr->iTable,
2192c 20 30 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 0, target);.
2192d 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
2192e 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 AddOp2(v, OP_Int
2192f 65 67 65 72 2c 20 31 2c 20 74 61 72 67 65 74 29 eger, 1, target)
21930 3b 0a 20 20 20 20 20 20 20 20 6a 35 20 3d 20 73 ;. j5 = s
21931 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 30 qlite3VdbeAddOp0
21932 28 76 2c 20 4f 50 5f 47 6f 74 6f 29 3b 0a 20 20 (v, OP_Goto);.
21933 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
21934 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 33 29 eJumpHere(v, j3)
21935 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 ;. sqlite
21936 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 3VdbeJumpHere(v,
21937 20 6a 34 29 3b 0a 20 20 20 20 20 20 20 20 73 71 j4);. sq
21938 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 lite3VdbeAddOp2(
21939 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 v, OP_Integer, 0
2193a 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 , target);.
2193b 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
2193c 72 32 20 3d 20 72 65 67 46 72 65 65 32 20 3d 20 r2 = regFree2 =
2193d 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 sqlite3GetTempRe
2193e 67 28 70 50 61 72 73 65 29 3b 0a 0a 20 20 20 20 g(pParse);..
2193f 20 20 20 20 2f 2a 20 43 72 65 61 74 65 20 61 20 /* Create a
21940 72 65 63 6f 72 64 20 61 6e 64 20 74 65 73 74 20 record and test
21941 66 6f 72 20 73 65 74 20 6d 65 6d 62 65 72 73 68 for set membersh
21942 69 70 2e 20 49 66 20 74 68 65 20 73 65 74 20 63 ip. If the set c
21943 6f 6e 74 61 69 6e 73 0a 20 20 20 20 20 20 20 20 ontains.
21944 2a 2a 20 74 68 65 20 76 61 6c 75 65 2c 20 74 68 ** the value, th
21945 65 6e 20 6a 75 6d 70 20 74 6f 20 74 68 65 20 65 en jump to the e
21946 6e 64 20 6f 66 20 74 68 65 20 74 65 73 74 20 63 nd of the test c
21947 6f 64 65 2e 20 54 68 65 20 74 61 72 67 65 74 0a ode. The target.
21948 20 20 20 20 20 20 20 20 2a 2a 20 72 65 67 69 73 ** regis
21949 74 65 72 20 73 74 69 6c 6c 20 63 6f 6e 74 61 69 ter still contai
2194a 6e 73 20 74 68 65 20 74 72 75 65 20 28 31 29 20 ns the true (1)
2194b 76 61 6c 75 65 20 77 72 69 74 74 65 6e 20 74 6f value written to
2194c 20 69 74 20 65 61 72 6c 69 65 72 2e 0a 20 20 20 it earlier..
2194d 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 */.
2194e 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
2194f 34 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 4(v, OP_MakeReco
21950 72 64 2c 20 74 61 72 67 65 74 2c 20 31 2c 20 72 rd, target, 1, r
21951 32 2c 20 26 61 66 66 69 6e 69 74 79 2c 20 31 29 2, &affinity, 1)
21952 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 ;. sqlite
21953 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 3VdbeAddOp2(v, O
21954 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 74 61 P_Integer, 1, ta
21955 72 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20 6a rget);. j
21956 35 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 5 = sqlite3VdbeA
21957 64 64 4f 70 33 28 76 2c 20 4f 50 5f 46 6f 75 6e ddOp3(v, OP_Foun
21958 64 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 d, pExpr->iTable
21959 2c 20 30 2c 20 72 32 29 3b 0a 0a 20 20 20 20 20 , 0, r2);..
2195a 20 20 20 2f 2a 20 49 66 20 74 68 65 20 73 65 74 /* If the set
2195b 20 6d 65 6d 62 65 72 73 68 69 70 20 74 65 73 74 membership test
2195c 20 66 61 69 6c 73 2c 20 74 68 65 6e 20 74 68 65 fails, then the
2195d 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 20 0a result of the .
2195e 20 20 20 20 20 20 20 20 2a 2a 20 22 78 20 49 4e ** "x IN
2195f 20 28 2e 2e 2e 29 22 20 65 78 70 72 65 73 73 69 (...)" expressi
21960 6f 6e 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 on must be eithe
21961 72 20 30 20 6f 72 20 4e 55 4c 4c 2e 20 49 66 20 r 0 or NULL. If
21962 74 68 65 20 73 65 74 0a 20 20 20 20 20 20 20 20 the set.
21963 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 20 4e ** contains no N
21964 55 4c 4c 20 76 61 6c 75 65 73 2c 20 74 68 65 6e ULL values, then
21965 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 30 the result is 0
21966 2e 20 49 66 20 74 68 65 20 73 65 74 20 0a 20 20 . If the set .
21967 20 20 20 20 20 20 2a 2a 20 63 6f 6e 74 61 69 6e ** contain
21968 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 4e 55 s one or more NU
21969 4c 4c 20 76 61 6c 75 65 73 2c 20 74 68 65 6e 20 LL values, then
2196a 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68 the result of th
2196b 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 65 78 70 e. ** exp
2196c 72 65 73 73 69 6f 6e 20 69 73 20 61 6c 73 6f 20 ression is also
2196d 4e 55 4c 4c 2e 0a 20 20 20 20 20 20 20 20 2a 2f NULL.. */
2196e 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 4e 6f . if( rNo
2196f 74 46 6f 75 6e 64 3d 3d 30 20 29 7b 0a 20 20 20 tFound==0 ){.
21970 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 62 /* This b
21971 72 61 6e 63 68 20 72 75 6e 73 20 69 66 20 69 74 ranch runs if it
21972 20 69 73 20 6b 6e 6f 77 6e 20 61 74 20 63 6f 6d is known at com
21973 70 69 6c 65 20 74 69 6d 65 20 28 6e 6f 77 29 20 pile time (now)
21974 74 68 61 74 20 0a 20 20 20 20 20 20 20 20 20 20 that .
21975 2a 2a 20 74 68 65 20 73 65 74 20 63 6f 6e 74 61 ** the set conta
21976 69 6e 73 20 6e 6f 20 4e 55 4c 4c 20 76 61 6c 75 ins no NULL valu
21977 65 73 2e 20 54 68 69 73 20 68 61 70 70 65 6e 73 es. This happens
21978 20 61 73 20 74 68 65 20 72 65 73 75 6c 74 0a 20 as the result.
21979 20 20 20 20 20 20 20 20 20 2a 2a 20 6f 66 20 61 ** of a
2197a 20 22 4e 4f 54 20 4e 55 4c 4c 22 20 63 6f 6e 73 "NOT NULL" cons
2197b 74 72 61 69 6e 74 20 69 6e 20 74 68 65 20 64 61 traint in the da
2197c 74 61 62 61 73 65 20 73 63 68 65 6d 61 2e 20 4e tabase schema. N
2197d 6f 20 6e 65 65 64 0a 20 20 20 20 20 20 20 20 20 o need.
2197e 20 2a 2a 20 74 6f 20 74 65 73 74 20 74 68 65 20 ** to test the
2197f 64 61 74 61 20 73 74 72 75 63 74 75 72 65 20 61 data structure a
21980 74 20 72 75 6e 74 69 6d 65 20 69 6e 20 74 68 69 t runtime in thi
21981 73 20 63 61 73 65 2e 0a 20 20 20 20 20 20 20 20 s case..
21982 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 73 */. s
21983 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 qlite3VdbeAddOp2
21984 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 (v, OP_Integer,
21985 30 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 0, target);.
21986 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
21987 20 20 20 20 20 2f 2a 20 54 68 69 73 20 62 6c 6f /* This blo
21988 63 6b 20 70 6f 70 75 6c 61 74 65 73 20 74 68 65 ck populates the
21989 20 72 4e 6f 74 46 6f 75 6e 64 20 72 65 67 69 73 rNotFound regis
2198a 74 65 72 20 77 69 74 68 20 65 69 74 68 65 72 20 ter with either
2198b 4e 55 4c 4c 0a 20 20 20 20 20 20 20 20 20 20 2a NULL. *
2198c 2a 20 6f 72 20 30 20 28 61 6e 20 69 6e 74 65 67 * or 0 (an integ
2198d 65 72 20 76 61 6c 75 65 29 2e 20 49 66 20 74 68 er value). If th
2198e 65 20 64 61 74 61 20 73 74 72 75 63 74 75 72 65 e data structure
2198f 20 63 6f 6e 74 61 69 6e 73 20 6f 6e 65 0a 20 20 contains one.
21990 20 20 20 20 20 20 20 20 2a 2a 20 6f 72 20 6d 6f ** or mo
21991 72 65 20 4e 55 4c 4c 73 2c 20 74 68 65 6e 20 73 re NULLs, then s
21992 65 74 20 72 4e 6f 74 46 6f 75 6e 64 20 74 6f 20 et rNotFound to
21993 4e 55 4c 4c 2e 20 4f 74 68 65 72 77 69 73 65 2c NULL. Otherwise,
21994 20 73 65 74 20 69 74 0a 20 20 20 20 20 20 20 20 set it.
21995 20 20 2a 2a 20 74 6f 20 30 2e 20 49 66 20 72 65 ** to 0. If re
21996 67 69 73 74 65 72 20 72 4d 61 79 48 61 76 65 4e gister rMayHaveN
21997 75 6c 6c 20 69 73 20 61 6c 72 65 61 64 79 20 73 ull is already s
21998 65 74 20 74 6f 20 73 6f 6d 65 20 76 61 6c 75 65 et to some value
21999 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6f 74 . ** ot
2199a 68 65 72 20 74 68 61 6e 20 4e 55 4c 4c 2c 20 74 her than NULL, t
2199b 68 65 6e 20 74 68 65 20 74 65 73 74 20 68 61 73 hen the test has
2199c 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 72 75 already been ru
2199d 6e 20 61 6e 64 20 0a 20 20 20 20 20 20 20 20 20 n and .
2199e 20 2a 2a 20 72 4e 6f 74 46 6f 75 6e 64 20 69 73 ** rNotFound is
2199f 20 61 6c 72 65 61 64 79 20 70 6f 70 75 6c 61 74 already populat
219a0 65 64 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f ed.. */
219a1 0a 20 20 20 20 20 20 20 20 20 20 73 74 61 74 69 . stati
219a2 63 20 63 6f 6e 73 74 20 63 68 61 72 20 6e 75 6c c const char nul
219a3 6c 52 65 63 6f 72 64 5b 5d 20 3d 20 7b 20 30 78 lRecord[] = { 0x
219a4 30 32 2c 20 30 78 30 30 20 7d 3b 0a 20 20 20 20 02, 0x00 };.
219a5 20 20 20 20 20 20 6a 33 20 3d 20 73 71 6c 69 74 j3 = sqlit
219a6 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 e3VdbeAddOp1(v,
219a7 4f 50 5f 4e 6f 74 4e 75 6c 6c 2c 20 72 4d 61 79 OP_NotNull, rMay
219a8 48 61 76 65 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 HaveNull);.
219a9 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
219aa 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c AddOp2(v, OP_Nul
219ab 6c 2c 20 30 2c 20 72 4e 6f 74 46 6f 75 6e 64 29 l, 0, rNotFound)
219ac 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 ;. sqli
219ad 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c te3VdbeAddOp4(v,
219ae 20 4f 50 5f 42 6c 6f 62 2c 20 32 2c 20 72 4d 61 OP_Blob, 2, rMa
219af 79 48 61 76 65 4e 75 6c 6c 2c 20 30 2c 20 0a 20 yHaveNull, 0, .
219b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
219b1 20 20 20 20 20 20 20 20 20 20 20 20 6e 75 6c 6c null
219b2 52 65 63 6f 72 64 2c 20 50 34 5f 53 54 41 54 49 Record, P4_STATI
219b3 43 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6a 34 C);. j4
219b4 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 = sqlite3VdbeAd
219b5 64 4f 70 33 28 76 2c 20 4f 50 5f 46 6f 75 6e 64 dOp3(v, OP_Found
219b6 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 2c , pExpr->iTable,
219b7 20 30 2c 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 0, rMayHaveNull
219b8 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c );. sql
219b9 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 ite3VdbeAddOp2(v
219ba 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c , OP_Integer, 0,
219bb 20 72 4e 6f 74 46 6f 75 6e 64 29 3b 0a 20 20 20 rNotFound);.
219bc 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 sqlite3Vd
219bd 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 34 beJumpHere(v, j4
219be 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c );. sql
219bf 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 ite3VdbeJumpHere
219c0 28 76 2c 20 6a 33 29 3b 0a 0a 20 20 20 20 20 20 (v, j3);..
219c1 20 20 20 20 2f 2a 20 43 6f 70 79 20 74 68 65 20 /* Copy the
219c2 76 61 6c 75 65 20 6f 66 20 72 65 67 69 73 74 65 value of registe
219c3 72 20 72 4e 6f 74 46 6f 75 6e 64 20 28 77 68 69 r rNotFound (whi
219c4 63 68 20 69 73 20 65 69 74 68 65 72 20 4e 55 4c ch is either NUL
219c5 4c 20 6f 72 20 30 29 0a 20 20 20 20 20 20 20 20 L or 0).
219c6 20 20 2a 2a 20 69 6e 74 6f 20 74 68 65 20 74 61 ** into the ta
219c7 72 67 65 74 20 72 65 67 69 73 74 65 72 2e 20 54 rget register. T
219c8 68 69 73 20 77 69 6c 6c 20 62 65 20 74 68 65 20 his will be the
219c9 72 65 73 75 6c 74 20 6f 66 20 74 68 65 0a 20 20 result of the.
219ca 20 20 20 20 20 20 20 20 2a 2a 20 65 78 70 72 65 ** expre
219cb 73 73 69 6f 6e 2e 0a 20 20 20 20 20 20 20 20 20 ssion..
219cc 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 73 71 */. sq
219cd 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 lite3VdbeAddOp2(
219ce 76 2c 20 4f 50 5f 43 6f 70 79 2c 20 72 4e 6f 74 v, OP_Copy, rNot
219cf 46 6f 75 6e 64 2c 20 74 61 72 67 65 74 29 3b 0a Found, target);.
219d0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
219d1 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 }. sqlite3V
219d2 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a dbeJumpHere(v, j
219d3 32 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 2);. sqlite
219d4 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 3VdbeJumpHere(v,
219d5 20 6a 35 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 j5);. sqli
219d6 74 65 33 45 78 70 72 43 61 63 68 65 50 6f 70 28 te3ExprCachePop(
219d7 70 50 61 72 73 65 2c 20 31 29 3b 0a 20 20 20 20 pParse, 1);.
219d8 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 VdbeComment((v
219d9 2c 20 22 65 6e 64 20 49 4e 20 65 78 70 72 20 72 , "end IN expr r
219da 25 64 22 2c 20 74 61 72 67 65 74 29 29 3b 0a 20 %d", target));.
219db 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
219dc 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 2f 2a 0a }.#endif. /*.
219dd 20 20 20 20 2a 2a 20 20 20 20 78 20 42 45 54 57 ** x BETW
219de 45 45 4e 20 79 20 41 4e 44 20 7a 0a 20 20 20 20 EEN y AND z.
219df 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 69 73 20 69 **. ** This i
219e0 73 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 0a s equivalent to.
219e1 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 **. **
219e2 20 78 3e 3d 79 20 41 4e 44 20 78 3c 3d 7a 0a 20 x>=y AND x<=z.
219e3 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 58 20 69 **. ** X i
219e4 73 20 73 74 6f 72 65 64 20 69 6e 20 70 45 78 70 s stored in pExp
219e5 72 2d 3e 70 4c 65 66 74 2e 0a 20 20 20 20 2a 2a r->pLeft.. **
219e6 20 59 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 Y is stored in
219e7 70 45 78 70 72 2d 3e 70 4c 69 73 74 2d 3e 61 5b pExpr->pList->a[
219e8 30 5d 2e 70 45 78 70 72 2e 0a 20 20 20 20 2a 2a 0].pExpr.. **
219e9 20 5a 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 Z is stored in
219ea 70 45 78 70 72 2d 3e 70 4c 69 73 74 2d 3e 61 5b pExpr->pList->a[
219eb 31 5d 2e 70 45 78 70 72 2e 0a 20 20 20 20 2a 2f 1].pExpr.. */
219ec 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 42 45 54 . case TK_BET
219ed 57 45 45 4e 3a 20 7b 0a 20 20 20 20 20 20 45 78 WEEN: {. Ex
219ee 70 72 20 2a 70 4c 65 66 74 20 3d 20 70 45 78 70 pr *pLeft = pExp
219ef 72 2d 3e 70 4c 65 66 74 3b 0a 20 20 20 20 20 20 r->pLeft;.
219f0 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f struct ExprList_
219f1 69 74 65 6d 20 2a 70 4c 49 74 65 6d 20 3d 20 70 item *pLItem = p
219f2 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2d 3e 61 Expr->x.pList->a
219f3 3b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 52 ;. Expr *pR
219f4 69 67 68 74 20 3d 20 70 4c 49 74 65 6d 2d 3e 70 ight = pLItem->p
219f5 45 78 70 72 3b 0a 0a 20 20 20 20 20 20 63 6f 64 Expr;.. cod
219f6 65 43 6f 6d 70 61 72 65 4f 70 65 72 61 6e 64 73 eCompareOperands
219f7 28 70 50 61 72 73 65 2c 20 70 4c 65 66 74 2c 20 (pParse, pLeft,
219f8 26 72 31 2c 20 26 72 65 67 46 72 65 65 31 2c 0a &r1, ®Free1,.
219f9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
219fa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
219fb 20 20 70 52 69 67 68 74 2c 20 26 72 32 2c 20 26 pRight, &r2, &
219fc 72 65 67 46 72 65 65 32 29 3b 0a 20 20 20 20 20 regFree2);.
219fd 20 74 65 73 74 63 61 73 65 28 20 72 65 67 46 72 testcase( regFr
219fe 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 ee1==0 );.
219ff 74 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65 testcase( regFre
21a00 65 32 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 72 e2==0 );. r
21a01 33 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 3 = sqlite3GetTe
21a02 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 mpReg(pParse);.
21a03 20 20 20 20 20 72 34 20 3d 20 73 71 6c 69 74 65 r4 = sqlite
21a04 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 3GetTempReg(pPar
21a05 73 65 29 3b 0a 20 20 20 20 20 20 63 6f 64 65 43 se);. codeC
21a06 6f 6d 70 61 72 65 28 70 50 61 72 73 65 2c 20 70 ompare(pParse, p
21a07 4c 65 66 74 2c 20 70 52 69 67 68 74 2c 20 4f 50 Left, pRight, OP
21a08 5f 47 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 _Ge,.
21a09 20 20 20 20 20 20 20 72 31 2c 20 72 32 2c 20 72 r1, r2, r
21a0a 33 2c 20 53 51 4c 49 54 45 5f 53 54 4f 52 45 50 3, SQLITE_STOREP
21a0b 32 29 3b 0a 20 20 20 20 20 20 70 4c 49 74 65 6d 2);. pLItem
21a0c 2b 2b 3b 0a 20 20 20 20 20 20 70 52 69 67 68 74 ++;. pRight
21a0d 20 3d 20 70 4c 49 74 65 6d 2d 3e 70 45 78 70 72 = pLItem->pExpr
21a0e 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 ;. sqlite3R
21a0f 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 eleaseTempReg(pP
21a10 61 72 73 65 2c 20 72 65 67 46 72 65 65 32 29 3b arse, regFree2);
21a11 0a 20 20 20 20 20 20 72 32 20 3d 20 73 71 6c 69 . r2 = sqli
21a12 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 te3ExprCodeTemp(
21a13 70 50 61 72 73 65 2c 20 70 52 69 67 68 74 2c 20 pParse, pRight,
21a14 26 72 65 67 46 72 65 65 32 29 3b 0a 20 20 20 20 ®Free2);.
21a15 20 20 74 65 73 74 63 61 73 65 28 20 72 65 67 46 testcase( regF
21a16 72 65 65 32 3d 3d 30 20 29 3b 0a 20 20 20 20 20 ree2==0 );.
21a17 20 63 6f 64 65 43 6f 6d 70 61 72 65 28 70 50 61 codeCompare(pPa
21a18 72 73 65 2c 20 70 4c 65 66 74 2c 20 70 52 69 67 rse, pLeft, pRig
21a19 68 74 2c 20 4f 50 5f 4c 65 2c 20 72 31 2c 20 72 ht, OP_Le, r1, r
21a1a 32 2c 20 72 34 2c 20 53 51 4c 49 54 45 5f 53 54 2, r4, SQLITE_ST
21a1b 4f 52 45 50 32 29 3b 0a 20 20 20 20 20 20 73 71 OREP2);. sq
21a1c 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 lite3VdbeAddOp3(
21a1d 76 2c 20 4f 50 5f 41 6e 64 2c 20 72 33 2c 20 72 v, OP_And, r3, r
21a1e 34 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 4, target);.
21a1f 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 sqlite3Release
21a20 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 TempReg(pParse,
21a21 72 33 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 r3);. sqlit
21a22 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 e3ReleaseTempReg
21a23 28 70 50 61 72 73 65 2c 20 72 34 29 3b 0a 20 20 (pParse, r4);.
21a24 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
21a25 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 55 50 4c . case TK_UPL
21a26 55 53 3a 20 7b 0a 20 20 20 20 20 20 69 6e 52 65 US: {. inRe
21a27 67 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 g = sqlite3ExprC
21a28 6f 64 65 54 61 72 67 65 74 28 70 50 61 72 73 65 odeTarget(pParse
21a29 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 , pExpr->pLeft,
21a2a 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 62 target);. b
21a2b 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 reak;. }..
21a2c 20 63 61 73 65 20 54 4b 5f 54 52 49 47 47 45 52 case TK_TRIGGER
21a2d 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 : {. /* If
21a2e 74 68 65 20 6f 70 63 6f 64 65 20 69 73 20 54 4b the opcode is TK
21a2f 5f 54 52 49 47 47 45 52 2c 20 74 68 65 6e 20 74 _TRIGGER, then t
21a30 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 he expression is
21a31 20 61 20 72 65 66 65 72 65 6e 63 65 0a 20 20 20 a reference.
21a32 20 20 20 2a 2a 20 74 6f 20 61 20 63 6f 6c 75 6d ** to a colum
21a33 6e 20 69 6e 20 74 68 65 20 6e 65 77 2e 2a 20 6f n in the new.* o
21a34 72 20 6f 6c 64 2e 2a 20 70 73 65 75 64 6f 2d 74 r old.* pseudo-t
21a35 61 62 6c 65 73 20 61 76 61 69 6c 61 62 6c 65 20 ables available
21a36 74 6f 0a 20 20 20 20 20 20 2a 2a 20 74 72 69 67 to. ** trig
21a37 67 65 72 20 70 72 6f 67 72 61 6d 73 2e 20 49 6e ger programs. In
21a38 20 74 68 69 73 20 63 61 73 65 20 45 78 70 72 2e this case Expr.
21a39 69 54 61 62 6c 65 20 69 73 20 73 65 74 20 74 6f iTable is set to
21a3a 20 31 20 66 6f 72 20 74 68 65 0a 20 20 20 20 20 1 for the.
21a3b 20 2a 2a 20 6e 65 77 2e 2a 20 70 73 65 75 64 6f ** new.* pseudo
21a3c 2d 74 61 62 6c 65 2c 20 6f 72 20 30 20 66 6f 72 -table, or 0 for
21a3d 20 74 68 65 20 6f 6c 64 2e 2a 20 70 73 65 75 64 the old.* pseud
21a3e 6f 2d 74 61 62 6c 65 2e 20 45 78 70 72 2e 69 43 o-table. Expr.iC
21a3f 6f 6c 75 6d 6e 0a 20 20 20 20 20 20 2a 2a 20 69 olumn. ** i
21a40 73 20 73 65 74 20 74 6f 20 74 68 65 20 63 6f 6c s set to the col
21a41 75 6d 6e 20 6f 66 20 74 68 65 20 70 73 65 75 64 umn of the pseud
21a42 6f 2d 74 61 62 6c 65 20 74 6f 20 72 65 61 64 2c o-table to read,
21a43 20 6f 72 20 74 6f 20 2d 31 20 74 6f 0a 20 20 20 or to -1 to.
21a44 20 20 20 2a 2a 20 72 65 61 64 20 74 68 65 20 72 ** read the r
21a45 6f 77 69 64 20 66 69 65 6c 64 2e 0a 20 20 20 20 owid field..
21a46 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 54 68 **. ** Th
21a47 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 e expression is
21a48 69 6d 70 6c 65 6d 65 6e 74 65 64 20 75 73 69 6e implemented usin
21a49 67 20 61 6e 20 4f 50 5f 50 61 72 61 6d 20 6f 70 g an OP_Param op
21a4a 63 6f 64 65 2e 20 54 68 65 20 70 31 0a 20 20 20 code. The p1.
21a4b 20 20 20 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 ** parameter
21a4c 69 73 20 73 65 74 20 74 6f 20 30 20 66 6f 72 20 is set to 0 for
21a4d 61 6e 20 6f 6c 64 2e 72 6f 77 69 64 20 72 65 66 an old.rowid ref
21a4e 65 72 65 6e 63 65 2c 20 6f 72 20 74 6f 20 28 69 erence, or to (i
21a4f 2b 31 29 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 +1). ** to
21a50 72 65 66 65 72 65 6e 63 65 20 61 6e 6f 74 68 65 reference anothe
21a51 72 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 r column of the
21a52 6f 6c 64 2e 2a 20 70 73 65 75 64 6f 2d 74 61 62 old.* pseudo-tab
21a53 6c 65 2c 20 77 68 65 72 65 20 0a 20 20 20 20 20 le, where .
21a54 20 2a 2a 20 69 20 69 73 20 74 68 65 20 69 6e 64 ** i is the ind
21a55 65 78 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e ex of the column
21a56 2e 20 46 6f 72 20 61 20 6e 65 77 2e 72 6f 77 69 . For a new.rowi
21a57 64 20 72 65 66 65 72 65 6e 63 65 2c 20 70 31 20 d reference, p1
21a58 69 73 0a 20 20 20 20 20 20 2a 2a 20 73 65 74 20 is. ** set
21a59 74 6f 20 28 6e 2b 31 29 2c 20 77 68 65 72 65 20 to (n+1), where
21a5a 6e 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 n is the number
21a5b 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 65 61 of columns in ea
21a5c 63 68 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 2e ch pseudo-table.
21a5d 0a 20 20 20 20 20 20 2a 2a 20 46 6f 72 20 61 20 . ** For a
21a5e 72 65 66 65 72 65 6e 63 65 20 74 6f 20 61 6e 79 reference to any
21a5f 20 6f 74 68 65 72 20 63 6f 6c 75 6d 6e 20 69 6e other column in
21a60 20 74 68 65 20 6e 65 77 2e 2a 20 70 73 65 75 64 the new.* pseud
21a61 6f 2d 74 61 62 6c 65 2c 20 70 31 0a 20 20 20 20 o-table, p1.
21a62 20 20 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 28 ** is set to (
21a63 6e 2b 32 2b 69 29 2c 20 77 68 65 72 65 20 6e 20 n+2+i), where n
21a64 61 6e 64 20 69 20 61 72 65 20 61 73 20 64 65 66 and i are as def
21a65 69 6e 65 64 20 70 72 65 76 69 6f 75 73 6c 79 2e ined previously.
21a66 20 46 6f 72 0a 20 20 20 20 20 20 2a 2a 20 65 78 For. ** ex
21a67 61 6d 70 6c 65 2c 20 69 66 20 74 68 65 20 74 61 ample, if the ta
21a68 62 6c 65 20 6f 6e 20 77 68 69 63 68 20 74 72 69 ble on which tri
21a69 67 67 65 72 73 20 61 72 65 20 62 65 69 6e 67 20 ggers are being
21a6a 66 69 72 65 64 20 69 73 0a 20 20 20 20 20 20 2a fired is. *
21a6b 2a 20 64 65 63 6c 61 72 65 64 20 61 73 3a 0a 20 * declared as:.
21a6c 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a **. **
21a6d 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 CREATE TABLE
21a6e 74 31 28 61 2c 20 62 29 3b 0a 20 20 20 20 20 20 t1(a, b);.
21a6f 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 54 68 65 6e **. ** Then
21a70 20 70 31 20 69 73 20 69 6e 74 65 72 70 72 65 74 p1 is interpret
21a71 65 64 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 20 ed as follows:.
21a72 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a **. **
21a73 20 20 20 70 31 3d 3d 30 20 20 20 2d 3e 20 20 20 p1==0 ->
21a74 20 6f 6c 64 2e 72 6f 77 69 64 20 20 20 20 20 70 old.rowid p
21a75 31 3d 3d 33 20 20 20 2d 3e 20 20 20 20 6e 65 77 1==3 -> new
21a76 2e 72 6f 77 69 64 0a 20 20 20 20 20 20 2a 2a 20 .rowid. **
21a77 20 20 70 31 3d 3d 31 20 20 20 2d 3e 20 20 20 20 p1==1 ->
21a78 6f 6c 64 2e 61 20 20 20 20 20 20 20 20 20 70 31 old.a p1
21a79 3d 3d 34 20 20 20 2d 3e 20 20 20 20 6e 65 77 2e ==4 -> new.
21a7a 61 0a 20 20 20 20 20 20 2a 2a 20 20 20 70 31 3d a. ** p1=
21a7b 3d 32 20 20 20 2d 3e 20 20 20 20 6f 6c 64 2e 62 =2 -> old.b
21a7c 20 20 20 20 20 20 20 20 20 70 31 3d 3d 35 20 20 p1==5
21a7d 20 2d 3e 20 20 20 20 6e 65 77 2e 62 20 20 20 20 -> new.b
21a7e 20 20 20 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 . */.
21a7f 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d Table *pTab =
21a80 20 70 45 78 70 72 2d 3e 70 54 61 62 3b 0a 20 20 pExpr->pTab;.
21a81 20 20 20 20 69 6e 74 20 70 31 20 3d 20 70 45 78 int p1 = pEx
21a82 70 72 2d 3e 69 54 61 62 6c 65 20 2a 20 28 70 54 pr->iTable * (pT
21a83 61 62 2d 3e 6e 43 6f 6c 2b 31 29 20 2b 20 31 20 ab->nCol+1) + 1
21a84 2b 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e + pExpr->iColumn
21a85 3b 0a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 ;.. assert(
21a86 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 3d 3d pExpr->iTable==
21a87 30 20 7c 7c 20 70 45 78 70 72 2d 3e 69 54 61 62 0 || pExpr->iTab
21a88 6c 65 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 61 le==1 );. a
21a89 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 69 43 ssert( pExpr->iC
21a8a 6f 6c 75 6d 6e 3e 3d 2d 31 20 26 26 20 70 45 78 olumn>=-1 && pEx
21a8b 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3c 70 54 61 62 pr->iColumn<pTab
21a8c 2d 3e 6e 43 6f 6c 20 29 3b 0a 20 20 20 20 20 20 ->nCol );.
21a8d 61 73 73 65 72 74 28 20 70 54 61 62 2d 3e 69 50 assert( pTab->iP
21a8e 4b 65 79 3c 30 20 7c 7c 20 70 45 78 70 72 2d 3e Key<0 || pExpr->
21a8f 69 43 6f 6c 75 6d 6e 21 3d 70 54 61 62 2d 3e 69 iColumn!=pTab->i
21a90 50 4b 65 79 20 29 3b 0a 20 20 20 20 20 20 61 73 PKey );. as
21a91 73 65 72 74 28 20 70 31 3e 3d 30 20 26 26 20 70 sert( p1>=0 && p
21a92 31 3c 28 70 54 61 62 2d 3e 6e 43 6f 6c 2a 32 2b 1<(pTab->nCol*2+
21a93 32 29 20 29 3b 0a 0a 20 20 20 20 20 20 73 71 6c 2) );.. sql
21a94 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 ite3VdbeAddOp2(v
21a95 2c 20 4f 50 5f 50 61 72 61 6d 2c 20 70 31 2c 20 , OP_Param, p1,
21a96 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 56 target);. V
21a97 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 dbeComment((v, "
21a98 25 73 2e 25 73 20 2d 3e 20 24 25 64 22 2c 0a 20 %s.%s -> $%d",.
21a99 20 20 20 20 20 20 20 28 70 45 78 70 72 2d 3e 69 (pExpr->i
21a9a 54 61 62 6c 65 20 3f 20 22 6e 65 77 22 20 3a 20 Table ? "new" :
21a9b 22 6f 6c 64 22 29 2c 0a 20 20 20 20 20 20 20 20 "old"),.
21a9c 28 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3c (pExpr->iColumn<
21a9d 30 20 3f 20 22 72 6f 77 69 64 22 20 3a 20 70 45 0 ? "rowid" : pE
21a9e 78 70 72 2d 3e 70 54 61 62 2d 3e 61 43 6f 6c 5b xpr->pTab->aCol[
21a9f 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 5d 2e pExpr->iColumn].
21aa0 7a 4e 61 6d 65 29 2c 0a 20 20 20 20 20 20 20 20 zName),.
21aa1 74 61 72 67 65 74 0a 20 20 20 20 20 20 29 29 3b target. ));
21aa2 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 .. /* If th
21aa3 65 20 63 6f 6c 75 6d 6e 20 68 61 73 20 52 45 41 e column has REA
21aa4 4c 20 61 66 66 69 6e 69 74 79 2c 20 69 74 20 6d L affinity, it m
21aa5 61 79 20 63 75 72 72 65 6e 74 6c 79 20 62 65 20 ay currently be
21aa6 73 74 6f 72 65 64 20 61 73 20 61 6e 0a 20 20 20 stored as an.
21aa7 20 20 20 2a 2a 20 69 6e 74 65 67 65 72 2e 20 55 ** integer. U
21aa8 73 65 20 4f 50 5f 52 65 61 6c 41 66 66 69 6e 69 se OP_RealAffini
21aa9 74 79 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 ty to make sure
21aaa 69 74 20 69 73 20 72 65 61 6c 6c 79 20 72 65 61 it is really rea
21aab 6c 2e 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 l. */. if(
21aac 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3e pExpr->iColumn>
21aad 3d 30 20 0a 20 20 20 20 20 20 20 26 26 20 70 54 =0 . && pT
21aae 61 62 2d 3e 61 43 6f 6c 5b 70 45 78 70 72 2d 3e ab->aCol[pExpr->
21aaf 69 43 6f 6c 75 6d 6e 5d 2e 61 66 66 69 6e 69 74 iColumn].affinit
21ab0 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 y==SQLITE_AFF_RE
21ab1 41 4c 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20 AL. ){.
21ab2 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
21ab3 64 64 4f 70 31 28 76 2c 20 4f 50 5f 52 65 61 6c ddOp1(v, OP_Real
21ab4 41 66 66 69 6e 69 74 79 2c 20 74 61 72 67 65 74 Affinity, target
21ab5 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
21ab6 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 0a break;. }...
21ab7 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 46 6f /*. ** Fo
21ab8 72 6d 20 41 3a 0a 20 20 20 20 2a 2a 20 20 20 43 rm A:. ** C
21ab9 41 53 45 20 78 20 57 48 45 4e 20 65 31 20 54 48 ASE x WHEN e1 TH
21aba 45 4e 20 72 31 20 57 48 45 4e 20 65 32 20 54 48 EN r1 WHEN e2 TH
21abb 45 4e 20 72 32 20 2e 2e 2e 20 57 48 45 4e 20 65 EN r2 ... WHEN e
21abc 4e 20 54 48 45 4e 20 72 4e 20 45 4c 53 45 20 79 N THEN rN ELSE y
21abd 20 45 4e 44 0a 20 20 20 20 2a 2a 0a 20 20 20 20 END. **.
21abe 2a 2a 20 46 6f 72 6d 20 42 3a 0a 20 20 20 20 2a ** Form B:. *
21abf 2a 20 20 20 43 41 53 45 20 57 48 45 4e 20 65 31 * CASE WHEN e1
21ac0 20 54 48 45 4e 20 72 31 20 57 48 45 4e 20 65 32 THEN r1 WHEN e2
21ac1 20 54 48 45 4e 20 72 32 20 2e 2e 2e 20 57 48 45 THEN r2 ... WHE
21ac2 4e 20 65 4e 20 54 48 45 4e 20 72 4e 20 45 4c 53 N eN THEN rN ELS
21ac3 45 20 79 20 45 4e 44 0a 20 20 20 20 2a 2a 0a 20 E y END. **.
21ac4 20 20 20 2a 2a 20 46 6f 72 6d 20 41 20 69 73 20 ** Form A is
21ac5 63 61 6e 20 62 65 20 74 72 61 6e 73 66 6f 72 6d can be transform
21ac6 65 64 20 69 6e 74 6f 20 74 68 65 20 65 71 75 69 ed into the equi
21ac7 76 61 6c 65 6e 74 20 66 6f 72 6d 20 42 20 61 73 valent form B as
21ac8 20 66 6f 6c 6c 6f 77 73 3a 0a 20 20 20 20 2a 2a follows:. **
21ac9 20 20 20 43 41 53 45 20 57 48 45 4e 20 78 3d 65 CASE WHEN x=e
21aca 31 20 54 48 45 4e 20 72 31 20 57 48 45 4e 20 78 1 THEN r1 WHEN x
21acb 3d 65 32 20 54 48 45 4e 20 72 32 20 2e 2e 2e 0a =e2 THEN r2 ....
21acc 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 57 48 ** WH
21acd 45 4e 20 78 3d 65 4e 20 54 48 45 4e 20 72 4e 20 EN x=eN THEN rN
21ace 45 4c 53 45 20 79 20 45 4e 44 0a 20 20 20 20 2a ELSE y END. *
21acf 2a 0a 20 20 20 20 2a 2a 20 58 20 28 69 66 20 69 *. ** X (if i
21ad0 74 20 65 78 69 73 74 73 29 20 69 73 20 69 6e 20 t exists) is in
21ad1 70 45 78 70 72 2d 3e 70 4c 65 66 74 2e 0a 20 20 pExpr->pLeft..
21ad2 20 20 2a 2a 20 59 20 69 73 20 69 6e 20 70 45 78 ** Y is in pEx
21ad3 70 72 2d 3e 70 52 69 67 68 74 2e 20 20 54 68 65 pr->pRight. The
21ad4 20 59 20 69 73 20 61 6c 73 6f 20 6f 70 74 69 6f Y is also optio
21ad5 6e 61 6c 2e 20 20 49 66 20 74 68 65 72 65 20 69 nal. If there i
21ad6 73 20 6e 6f 0a 20 20 20 20 2a 2a 20 45 4c 53 45 s no. ** ELSE
21ad7 20 63 6c 61 75 73 65 20 61 6e 64 20 6e 6f 20 6f clause and no o
21ad8 74 68 65 72 20 74 65 72 6d 20 6d 61 74 63 68 65 ther term matche
21ad9 73 2c 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 s, then the resu
21ada 6c 74 20 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a lt of the. **
21adb 20 65 78 70 72 73 73 69 6f 6e 20 69 73 20 4e 55 exprssion is NU
21adc 4c 4c 2e 0a 20 20 20 20 2a 2a 20 45 69 20 69 73 LL.. ** Ei is
21add 20 69 6e 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 in pExpr->pList
21ade 2d 3e 61 5b 69 2a 32 5d 20 61 6e 64 20 52 69 20 ->a[i*2] and Ri
21adf 69 73 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 2d is pExpr->pList-
21ae0 3e 61 5b 69 2a 32 2b 31 5d 2e 0a 20 20 20 20 2a >a[i*2+1].. *
21ae1 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 72 65 73 *. ** The res
21ae2 75 6c 74 20 6f 66 20 74 68 65 20 65 78 70 72 65 ult of the expre
21ae3 73 73 69 6f 6e 20 69 73 20 74 68 65 20 52 69 20 ssion is the Ri
21ae4 66 6f 72 20 74 68 65 20 66 69 72 73 74 20 6d 61 for the first ma
21ae5 74 63 68 69 6e 67 20 45 69 2c 0a 20 20 20 20 2a tching Ei,. *
21ae6 2a 20 6f 72 20 69 66 20 74 68 65 72 65 20 69 73 * or if there is
21ae7 20 6e 6f 20 6d 61 74 63 68 69 6e 67 20 45 69 2c no matching Ei,
21ae8 20 74 68 65 20 45 4c 53 45 20 74 65 72 6d 20 59 the ELSE term Y
21ae9 2c 20 6f 72 20 69 66 20 74 68 65 72 65 20 69 73 , or if there is
21aea 0a 20 20 20 20 2a 2a 20 6e 6f 20 45 4c 53 45 20 . ** no ELSE
21aeb 74 65 72 6d 2c 20 4e 55 4c 4c 2e 0a 20 20 20 20 term, NULL..
21aec 2a 2f 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 */. default:
21aed 61 73 73 65 72 74 28 20 6f 70 3d 3d 54 4b 5f 43 assert( op==TK_C
21aee 41 53 45 20 29 3b 20 7b 0a 20 20 20 20 20 20 69 ASE ); {. i
21aef 6e 74 20 65 6e 64 4c 61 62 65 6c 3b 20 20 20 20 nt endLabel;
21af0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21af1 20 2f 2a 20 47 4f 54 4f 20 6c 61 62 65 6c 20 66 /* GOTO label f
21af2 6f 72 20 65 6e 64 20 6f 66 20 43 41 53 45 20 73 or end of CASE s
21af3 74 6d 74 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 tmt */. int
21af4 20 6e 65 78 74 43 61 73 65 3b 20 20 20 20 20 20 nextCase;
21af5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
21af6 2a 20 47 4f 54 4f 20 6c 61 62 65 6c 20 66 6f 72 * GOTO label for
21af7 20 6e 65 78 74 20 57 48 45 4e 20 63 6c 61 75 73 next WHEN claus
21af8 65 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e e */. int n
21af9 45 78 70 72 3b 20 20 20 20 20 20 20 20 20 20 20 Expr;
21afa 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
21afb 32 78 20 6e 75 6d 62 65 72 20 6f 66 20 57 48 45 2x number of WHE
21afc 4e 20 74 65 72 6d 73 20 2a 2f 0a 20 20 20 20 20 N terms */.
21afd 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 int i;
21afe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21aff 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 /* Loop count
21b00 65 72 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 72 er */. Expr
21b01 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 20 20 20 List *pEList;
21b02 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
21b03 20 4c 69 73 74 20 6f 66 20 57 48 45 4e 20 74 65 List of WHEN te
21b04 72 6d 73 20 2a 2f 0a 20 20 20 20 20 20 73 74 72 rms */. str
21b05 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 uct ExprList_ite
21b06 6d 20 2a 61 4c 69 73 74 65 6c 65 6d 3b 20 20 2f m *aListelem; /
21b07 2a 20 41 72 72 61 79 20 6f 66 20 57 48 45 4e 20 * Array of WHEN
21b08 74 65 72 6d 73 20 2a 2f 0a 20 20 20 20 20 20 45 terms */. E
21b09 78 70 72 20 6f 70 43 6f 6d 70 61 72 65 3b 20 20 xpr opCompare;
21b0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21b0b 20 2f 2a 20 54 68 65 20 58 3d 3d 45 69 20 65 78 /* The X==Ei ex
21b0c 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 pression */.
21b0d 20 20 45 78 70 72 20 63 61 63 68 65 58 3b 20 20 Expr cacheX;
21b0e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21b0f 20 20 20 20 2f 2a 20 43 61 63 68 65 64 20 65 78 /* Cached ex
21b10 70 72 65 73 73 69 6f 6e 20 58 20 2a 2f 0a 20 20 pression X */.
21b11 20 20 20 20 45 78 70 72 20 2a 70 58 3b 20 20 20 Expr *pX;
21b12 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21b13 20 20 20 20 20 20 2f 2a 20 54 68 65 20 58 20 65 /* The X e
21b14 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 20 xpression */.
21b15 20 20 20 45 78 70 72 20 2a 70 54 65 73 74 20 3d Expr *pTest =
21b16 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 0;
21b17 20 20 20 20 20 2f 2a 20 58 3d 3d 45 69 20 28 66 /* X==Ei (f
21b18 6f 72 6d 20 41 29 20 6f 72 20 6a 75 73 74 20 45 orm A) or just E
21b19 69 20 28 66 6f 72 6d 20 42 29 20 2a 2f 0a 20 20 i (form B) */.
21b1a 20 20 20 20 56 56 41 5f 4f 4e 4c 59 28 20 69 6e VVA_ONLY( in
21b1b 74 20 69 43 61 63 68 65 4c 65 76 65 6c 20 3d 20 t iCacheLevel =
21b1c 70 50 61 72 73 65 2d 3e 69 43 61 63 68 65 4c 65 pParse->iCacheLe
21b1d 76 65 6c 3b 20 29 0a 0a 20 20 20 20 20 20 61 73 vel; ).. as
21b1e 73 65 72 74 28 20 21 45 78 70 72 48 61 73 50 72 sert( !ExprHasPr
21b1f 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 operty(pExpr, EP
21b20 5f 78 49 73 53 65 6c 65 63 74 29 20 26 26 20 70 _xIsSelect) && p
21b21 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 20 29 3b Expr->x.pList );
21b22 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 28 70 . assert((p
21b23 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2d 3e 6e Expr->x.pList->n
21b24 45 78 70 72 20 25 20 32 29 20 3d 3d 20 30 29 3b Expr % 2) == 0);
21b25 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 70 45 . assert(pE
21b26 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2d 3e 6e 45 xpr->x.pList->nE
21b27 78 70 72 20 3e 20 30 29 3b 0a 20 20 20 20 20 20 xpr > 0);.
21b28 70 45 4c 69 73 74 20 3d 20 70 45 78 70 72 2d 3e pEList = pExpr->
21b29 78 2e 70 4c 69 73 74 3b 0a 20 20 20 20 20 20 61 x.pList;. a
21b2a 4c 69 73 74 65 6c 65 6d 20 3d 20 70 45 4c 69 73 Listelem = pELis
21b2b 74 2d 3e 61 3b 0a 20 20 20 20 20 20 6e 45 78 70 t->a;. nExp
21b2c 72 20 3d 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 r = pEList->nExp
21b2d 72 3b 0a 20 20 20 20 20 20 65 6e 64 4c 61 62 65 r;. endLabe
21b2e 6c 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d l = sqlite3VdbeM
21b2f 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 akeLabel(v);.
21b30 20 20 20 69 66 28 20 28 70 58 20 3d 20 70 45 78 if( (pX = pEx
21b31 70 72 2d 3e 70 4c 65 66 74 29 21 3d 30 20 29 7b pr->pLeft)!=0 ){
21b32 0a 20 20 20 20 20 20 20 20 63 61 63 68 65 58 20 . cacheX
21b33 3d 20 2a 70 58 3b 0a 20 20 20 20 20 20 20 20 74 = *pX;. t
21b34 65 73 74 63 61 73 65 28 20 70 58 2d 3e 6f 70 3d estcase( pX->op=
21b35 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 3b 0a 20 20 =TK_COLUMN );.
21b36 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
21b37 70 58 2d 3e 6f 70 3d 3d 54 4b 5f 52 45 47 49 53 pX->op==TK_REGIS
21b38 54 45 52 20 29 3b 0a 20 20 20 20 20 20 20 20 63 TER );. c
21b39 61 63 68 65 58 2e 69 54 61 62 6c 65 20 3d 20 73 acheX.iTable = s
21b3a 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 65 qlite3ExprCodeTe
21b3b 6d 70 28 70 50 61 72 73 65 2c 20 70 58 2c 20 26 mp(pParse, pX, &
21b3c 72 65 67 46 72 65 65 31 29 3b 0a 20 20 20 20 20 regFree1);.
21b3d 20 20 20 74 65 73 74 63 61 73 65 28 20 72 65 67 testcase( reg
21b3e 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20 Free1==0 );.
21b3f 20 20 20 20 63 61 63 68 65 58 2e 6f 70 20 3d 20 cacheX.op =
21b40 54 4b 5f 52 45 47 49 53 54 45 52 3b 0a 20 20 20 TK_REGISTER;.
21b41 20 20 20 20 20 6f 70 43 6f 6d 70 61 72 65 2e 6f opCompare.o
21b42 70 20 3d 20 54 4b 5f 45 51 3b 0a 20 20 20 20 20 p = TK_EQ;.
21b43 20 20 20 6f 70 43 6f 6d 70 61 72 65 2e 70 4c 65 opCompare.pLe
21b44 66 74 20 3d 20 26 63 61 63 68 65 58 3b 0a 20 20 ft = &cacheX;.
21b45 20 20 20 20 20 20 70 54 65 73 74 20 3d 20 26 6f pTest = &o
21b46 70 43 6f 6d 70 61 72 65 3b 0a 20 20 20 20 20 20 pCompare;.
21b47 7d 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b }. for(i=0;
21b48 20 69 3c 6e 45 78 70 72 3b 20 69 3d 69 2b 32 29 i<nExpr; i=i+2)
21b49 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
21b4a 33 45 78 70 72 43 61 63 68 65 50 75 73 68 28 70 3ExprCachePush(p
21b4b 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 20 20 Parse);.
21b4c 69 66 28 20 70 58 20 29 7b 0a 20 20 20 20 20 20 if( pX ){.
21b4d 20 20 20 20 61 73 73 65 72 74 28 20 70 54 65 73 assert( pTes
21b4e 74 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 t!=0 );.
21b4f 20 20 6f 70 43 6f 6d 70 61 72 65 2e 70 52 69 67 opCompare.pRig
21b50 68 74 20 3d 20 61 4c 69 73 74 65 6c 65 6d 5b 69 ht = aListelem[i
21b51 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 20 ].pExpr;.
21b52 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
21b53 20 20 70 54 65 73 74 20 3d 20 61 4c 69 73 74 65 pTest = aListe
21b54 6c 65 6d 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 lem[i].pExpr;.
21b55 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
21b56 6e 65 78 74 43 61 73 65 20 3d 20 73 71 6c 69 74 nextCase = sqlit
21b57 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 e3VdbeMakeLabel(
21b58 76 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 v);. test
21b59 63 61 73 65 28 20 70 54 65 73 74 2d 3e 6f 70 3d case( pTest->op=
21b5a 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 3b 0a 20 20 =TK_COLUMN );.
21b5b 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 sqlite3Exp
21b5c 72 49 66 46 61 6c 73 65 28 70 50 61 72 73 65 2c rIfFalse(pParse,
21b5d 20 70 54 65 73 74 2c 20 6e 65 78 74 43 61 73 65 pTest, nextCase
21b5e 2c 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e , SQLITE_JUMPIFN
21b5f 55 4c 4c 29 3b 0a 20 20 20 20 20 20 20 20 74 65 ULL);. te
21b60 73 74 63 61 73 65 28 20 61 4c 69 73 74 65 6c 65 stcase( aListele
21b61 6d 5b 69 2b 31 5d 2e 70 45 78 70 72 2d 3e 6f 70 m[i+1].pExpr->op
21b62 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 3b 0a 20 ==TK_COLUMN );.
21b63 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 testcase(
21b64 20 61 4c 69 73 74 65 6c 65 6d 5b 69 2b 31 5d 2e aListelem[i+1].
21b65 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 52 45 pExpr->op==TK_RE
21b66 47 49 53 54 45 52 20 29 3b 0a 20 20 20 20 20 20 GISTER );.
21b67 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 sqlite3ExprCod
21b68 65 28 70 50 61 72 73 65 2c 20 61 4c 69 73 74 65 e(pParse, aListe
21b69 6c 65 6d 5b 69 2b 31 5d 2e 70 45 78 70 72 2c 20 lem[i+1].pExpr,
21b6a 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 20 target);.
21b6b 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
21b6c 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 p2(v, OP_Goto, 0
21b6d 2c 20 65 6e 64 4c 61 62 65 6c 29 3b 0a 20 20 20 , endLabel);.
21b6e 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 sqlite3Expr
21b6f 43 61 63 68 65 50 6f 70 28 70 50 61 72 73 65 2c CachePop(pParse,
21b70 20 31 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 1);. sql
21b71 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c ite3VdbeResolveL
21b72 61 62 65 6c 28 76 2c 20 6e 65 78 74 43 61 73 65 abel(v, nextCase
21b73 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
21b74 20 69 66 28 20 70 45 78 70 72 2d 3e 70 52 69 67 if( pExpr->pRig
21b75 68 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 ht ){. sq
21b76 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50 75 lite3ExprCachePu
21b77 73 68 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 sh(pParse);.
21b78 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 sqlite3ExprC
21b79 6f 64 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 ode(pParse, pExp
21b7a 72 2d 3e 70 52 69 67 68 74 2c 20 74 61 72 67 65 r->pRight, targe
21b7b 74 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 t);. sqli
21b7c 74 65 33 45 78 70 72 43 61 63 68 65 50 6f 70 28 te3ExprCachePop(
21b7d 70 50 61 72 73 65 2c 20 31 29 3b 0a 20 20 20 20 pParse, 1);.
21b7e 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
21b7f 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
21b80 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 p2(v, OP_Null, 0
21b81 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 , target);.
21b82 20 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 }. assert(
21b83 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 db->mallocFaile
21b84 64 20 7c 7c 20 70 50 61 72 73 65 2d 3e 6e 45 72 d || pParse->nEr
21b85 72 3e 30 20 0a 20 20 20 20 20 20 20 20 20 20 20 r>0 .
21b86 7c 7c 20 70 50 61 72 73 65 2d 3e 69 43 61 63 68 || pParse->iCach
21b87 65 4c 65 76 65 6c 3d 3d 69 43 61 63 68 65 4c 65 eLevel==iCacheLe
21b88 76 65 6c 20 29 3b 0a 20 20 20 20 20 20 73 71 6c vel );. sql
21b89 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c ite3VdbeResolveL
21b8a 61 62 65 6c 28 76 2c 20 65 6e 64 4c 61 62 65 6c abel(v, endLabel
21b8b 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a );. break;.
21b8c 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 }.#ifndef SQ
21b8d 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 LITE_OMIT_TRIGGE
21b8e 52 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 52 41 R. case TK_RA
21b8f 49 53 45 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 ISE: {. ass
21b90 65 72 74 28 20 70 45 78 70 72 2d 3e 61 66 66 69 ert( pExpr->affi
21b91 6e 69 74 79 3d 3d 4f 45 5f 52 6f 6c 6c 62 61 63 nity==OE_Rollbac
21b92 6b 20 0a 20 20 20 20 20 20 20 20 20 20 20 7c 7c k . ||
21b93 20 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 79 pExpr->affinity
21b94 3d 3d 4f 45 5f 41 62 6f 72 74 0a 20 20 20 20 20 ==OE_Abort.
21b95 20 20 20 20 20 20 7c 7c 20 70 45 78 70 72 2d 3e || pExpr->
21b96 61 66 66 69 6e 69 74 79 3d 3d 4f 45 5f 46 61 69 affinity==OE_Fai
21b97 6c 0a 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 l. ||
21b98 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 79 3d pExpr->affinity=
21b99 3d 4f 45 5f 49 67 6e 6f 72 65 0a 20 20 20 20 20 =OE_Ignore.
21b9a 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70 );. if( !p
21b9b 50 61 72 73 65 2d 3e 70 54 72 69 67 67 65 72 54 Parse->pTriggerT
21b9c 61 62 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 ab ){. sq
21b9d 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 lite3ErrorMsg(pP
21b9e 61 72 73 65 2c 0a 20 20 20 20 20 20 20 20 20 20 arse,.
21b9f 20 20 20 20 20 20 20 20 20 20 20 20 20 22 52 41 "RA
21ba0 49 53 45 28 29 20 6d 61 79 20 6f 6e 6c 79 20 62 ISE() may only b
21ba1 65 20 75 73 65 64 20 77 69 74 68 69 6e 20 61 20 e used within a
21ba2 74 72 69 67 67 65 72 2d 70 72 6f 67 72 61 6d 22 trigger-program"
21ba3 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 );. retur
21ba4 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 n 0;. }.
21ba5 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 61 66 if( pExpr->af
21ba6 66 69 6e 69 74 79 3d 3d 4f 45 5f 41 62 6f 72 74 finity==OE_Abort
21ba7 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 ){. sqli
21ba8 74 65 33 4d 61 79 41 62 6f 72 74 28 70 50 61 72 te3MayAbort(pPar
21ba9 73 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 se);. }.
21baa 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72 assert( !Expr
21bab 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 HasProperty(pExp
21bac 72 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 r, EP_IntValue)
21bad 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 45 78 );. if( pEx
21bae 70 72 2d 3e 61 66 66 69 6e 69 74 79 3d 3d 4f 45 pr->affinity==OE
21baf 5f 49 67 6e 6f 72 65 20 29 7b 0a 20 20 20 20 20 _Ignore ){.
21bb0 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
21bb1 64 4f 70 34 28 0a 20 20 20 20 20 20 20 20 20 20 dOp4(.
21bb2 20 20 76 2c 20 4f 50 5f 48 61 6c 74 2c 20 53 51 v, OP_Halt, SQ
21bb3 4c 49 54 45 5f 4f 4b 2c 20 4f 45 5f 49 67 6e 6f LITE_OK, OE_Igno
21bb4 72 65 2c 20 30 2c 20 70 45 78 70 72 2d 3e 75 2e re, 0, pExpr->u.
21bb5 7a 54 6f 6b 65 6e 2c 30 29 3b 0a 20 20 20 20 20 zToken,0);.
21bb6 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
21bb7 73 71 6c 69 74 65 33 48 61 6c 74 43 6f 6e 73 74 sqlite3HaltConst
21bb8 72 61 69 6e 74 28 70 50 61 72 73 65 2c 20 70 45 raint(pParse, pE
21bb9 78 70 72 2d 3e 61 66 66 69 6e 69 74 79 2c 20 70 xpr->affinity, p
21bba 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 20 Expr->u.zToken,
21bbb 30 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 0);. }..
21bbc 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
21bbd 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 73 71 6c #endif. }. sql
21bbe 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 ite3ReleaseTempR
21bbf 65 67 28 70 50 61 72 73 65 2c 20 72 65 67 46 72 eg(pParse, regFr
21bc0 65 65 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 52 ee1);. sqlite3R
21bc1 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 eleaseTempReg(pP
21bc2 61 72 73 65 2c 20 72 65 67 46 72 65 65 32 29 3b arse, regFree2);
21bc3 0a 20 20 72 65 74 75 72 6e 20 69 6e 52 65 67 3b . return inReg;
21bc4 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 .}../*.** Genera
21bc5 74 65 20 63 6f 64 65 20 74 6f 20 65 76 61 6c 75 te code to evalu
21bc6 61 74 65 20 61 6e 20 65 78 70 72 65 73 73 69 6f ate an expressio
21bc7 6e 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20 n and store the
21bc8 72 65 73 75 6c 74 73 0a 2a 2a 20 69 6e 74 6f 20 results.** into
21bc9 61 20 72 65 67 69 73 74 65 72 2e 20 20 52 65 74 a register. Ret
21bca 75 72 6e 20 74 68 65 20 72 65 67 69 73 74 65 72 urn the register
21bcb 20 6e 75 6d 62 65 72 20 77 68 65 72 65 20 74 68 number where th
21bcc 65 20 72 65 73 75 6c 74 73 0a 2a 2a 20 61 72 65 e results.** are
21bcd 20 73 74 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 stored..**.** I
21bce 66 20 74 68 65 20 72 65 67 69 73 74 65 72 20 69 f the register i
21bcf 73 20 61 20 74 65 6d 70 6f 72 61 72 79 20 72 65 s a temporary re
21bd0 67 69 73 74 65 72 20 74 68 61 74 20 63 61 6e 20 gister that can
21bd1 62 65 20 64 65 61 6c 6c 6f 63 61 74 65 64 2c 0a be deallocated,.
21bd2 2a 2a 20 74 68 65 6e 20 77 72 69 74 65 20 69 74 ** then write it
21bd3 73 20 6e 75 6d 62 65 72 20 69 6e 74 6f 20 2a 70 s number into *p
21bd4 52 65 67 2e 20 20 49 66 20 74 68 65 20 72 65 73 Reg. If the res
21bd5 75 6c 74 20 72 65 67 69 73 74 65 72 20 69 73 20 ult register is
21bd6 6e 6f 74 0a 2a 2a 20 61 20 74 65 6d 70 6f 72 61 not.** a tempora
21bd7 72 79 2c 20 74 68 65 6e 20 73 65 74 20 2a 70 52 ry, then set *pR
21bd8 65 67 20 74 6f 20 7a 65 72 6f 2e 0a 2a 2f 0a 53 eg to zero..*/.S
21bd9 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
21bda 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 t sqlite3ExprCod
21bdb 65 54 65 6d 70 28 50 61 72 73 65 20 2a 70 50 61 eTemp(Parse *pPa
21bdc 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72 rse, Expr *pExpr
21bdd 2c 20 69 6e 74 20 2a 70 52 65 67 29 7b 0a 20 20 , int *pReg){.
21bde 69 6e 74 20 72 31 20 3d 20 73 71 6c 69 74 65 33 int r1 = sqlite3
21bdf 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 GetTempReg(pPars
21be0 65 29 3b 0a 20 20 69 6e 74 20 72 32 20 3d 20 73 e);. int r2 = s
21be1 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 61 qlite3ExprCodeTa
21be2 72 67 65 74 28 70 50 61 72 73 65 2c 20 70 45 78 rget(pParse, pEx
21be3 70 72 2c 20 72 31 29 3b 0a 20 20 69 66 28 20 72 pr, r1);. if( r
21be4 32 3d 3d 72 31 20 29 7b 0a 20 20 20 20 2a 70 52 2==r1 ){. *pR
21be5 65 67 20 3d 20 72 31 3b 0a 20 20 7d 65 6c 73 65 eg = r1;. }else
21be6 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c {. sqlite3Rel
21be7 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 easeTempReg(pPar
21be8 73 65 2c 20 72 31 29 3b 0a 20 20 20 20 2a 70 52 se, r1);. *pR
21be9 65 67 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 eg = 0;. }. re
21bea 74 75 72 6e 20 72 32 3b 0a 7d 0a 0a 2f 2a 0a 2a turn r2;.}../*.*
21beb 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 * Generate code
21bec 74 68 61 74 20 77 69 6c 6c 20 65 76 61 6c 75 61 that will evalua
21bed 74 65 20 65 78 70 72 65 73 73 69 6f 6e 20 70 45 te expression pE
21bee 78 70 72 20 61 6e 64 20 73 74 6f 72 65 20 74 68 xpr and store th
21bef 65 0a 2a 2a 20 72 65 73 75 6c 74 73 20 69 6e 20 e.** results in
21bf0 72 65 67 69 73 74 65 72 20 74 61 72 67 65 74 2e register target.
21bf1 20 20 54 68 65 20 72 65 73 75 6c 74 73 20 61 72 The results ar
21bf2 65 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f 20 e guaranteed to
21bf3 61 70 70 65 61 72 0a 2a 2a 20 69 6e 20 72 65 67 appear.** in reg
21bf4 69 73 74 65 72 20 74 61 72 67 65 74 2e 0a 2a 2f ister target..*/
21bf5 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
21bf6 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 int sqlite3ExprC
21bf7 6f 64 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 ode(Parse *pPars
21bf8 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 e, Expr *pExpr,
21bf9 69 6e 74 20 74 61 72 67 65 74 29 7b 0a 20 20 69 int target){. i
21bfa 6e 74 20 69 6e 52 65 67 3b 0a 0a 20 20 61 73 73 nt inReg;.. ass
21bfb 65 72 74 28 20 74 61 72 67 65 74 3e 30 20 26 26 ert( target>0 &&
21bfc 20 74 61 72 67 65 74 3c 3d 70 50 61 72 73 65 2d target<=pParse-
21bfd 3e 6e 4d 65 6d 20 29 3b 0a 20 20 69 6e 52 65 67 >nMem );. inReg
21bfe 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f = sqlite3ExprCo
21bff 64 65 54 61 72 67 65 74 28 70 50 61 72 73 65 2c deTarget(pParse,
21c00 20 70 45 78 70 72 2c 20 74 61 72 67 65 74 29 3b pExpr, target);
21c01 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73 . assert( pPars
21c02 65 2d 3e 70 56 64 62 65 20 7c 7c 20 70 50 61 72 e->pVdbe || pPar
21c03 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 se->db->mallocFa
21c04 69 6c 65 64 20 29 3b 0a 20 20 69 66 28 20 69 6e iled );. if( in
21c05 52 65 67 21 3d 74 61 72 67 65 74 20 26 26 20 70 Reg!=target && p
21c06 50 61 72 73 65 2d 3e 70 56 64 62 65 20 29 7b 0a Parse->pVdbe ){.
21c07 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
21c08 64 64 4f 70 32 28 70 50 61 72 73 65 2d 3e 70 56 ddOp2(pParse->pV
21c09 64 62 65 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 69 dbe, OP_SCopy, i
21c0a 6e 52 65 67 2c 20 74 61 72 67 65 74 29 3b 0a 20 nReg, target);.
21c0b 20 7d 0a 20 20 72 65 74 75 72 6e 20 74 61 72 67 }. return targ
21c0c 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e et;.}../*.** Gen
21c0d 65 72 61 74 65 20 63 6f 64 65 20 74 68 61 74 20 erate code that
21c0e 65 76 61 6c 75 74 65 73 20 74 68 65 20 67 69 76 evalutes the giv
21c0f 65 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 61 6e en expression an
21c10 64 20 70 75 74 73 20 74 68 65 20 72 65 73 75 6c d puts the resul
21c11 74 0a 2a 2a 20 69 6e 20 72 65 67 69 73 74 65 72 t.** in register
21c12 20 74 61 72 67 65 74 2e 0a 2a 2a 0a 2a 2a 20 41 target..**.** A
21c13 6c 73 6f 20 6d 61 6b 65 20 61 20 63 6f 70 79 20 lso make a copy
21c14 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f of the expressio
21c15 6e 20 72 65 73 75 6c 74 73 20 69 6e 74 6f 20 61 n results into a
21c16 6e 6f 74 68 65 72 20 22 63 61 63 68 65 22 20 72 nother "cache" r
21c17 65 67 69 73 74 65 72 0a 2a 2a 20 61 6e 64 20 6d egister.** and m
21c18 6f 64 69 66 79 20 74 68 65 20 65 78 70 72 65 73 odify the expres
21c19 73 69 6f 6e 20 73 6f 20 74 68 61 74 20 74 68 65 sion so that the
21c1a 20 6e 65 78 74 20 74 69 6d 65 20 69 74 20 69 73 next time it is
21c1b 20 65 76 61 6c 75 61 74 65 64 2c 0a 2a 2a 20 74 evaluated,.** t
21c1c 68 65 20 72 65 73 75 6c 74 20 69 73 20 61 20 63 he result is a c
21c1d 6f 70 79 20 6f 66 20 74 68 65 20 63 61 63 68 65 opy of the cache
21c1e 20 72 65 67 69 73 74 65 72 2e 0a 2a 2a 0a 2a 2a register..**.**
21c1f 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 This routine is
21c20 20 75 73 65 64 20 66 6f 72 20 65 78 70 72 65 73 used for expres
21c21 73 69 6f 6e 73 20 74 68 61 74 20 61 72 65 20 75 sions that are u
21c22 73 65 64 20 6d 75 6c 74 69 70 6c 65 20 0a 2a 2a sed multiple .**
21c23 20 74 69 6d 65 73 2e 20 20 54 68 65 79 20 61 72 times. They ar
21c24 65 20 65 76 61 6c 75 61 74 65 64 20 6f 6e 63 65 e evaluated once
21c25 20 61 6e 64 20 74 68 65 20 72 65 73 75 6c 74 73 and the results
21c26 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69 of the expressi
21c27 6f 6e 0a 2a 2a 20 61 72 65 20 72 65 75 73 65 64 on.** are reused
21c28 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
21c29 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 ATE int sqlite3E
21c2a 78 70 72 43 6f 64 65 41 6e 64 43 61 63 68 65 28 xprCodeAndCache(
21c2b 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 Parse *pParse, E
21c2c 78 70 72 20 2a 70 45 78 70 72 2c 20 69 6e 74 20 xpr *pExpr, int
21c2d 74 61 72 67 65 74 29 7b 0a 20 20 56 64 62 65 20 target){. Vdbe
21c2e 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 *v = pParse->pVd
21c2f 62 65 3b 0a 20 20 69 6e 74 20 69 6e 52 65 67 3b be;. int inReg;
21c30 0a 20 20 69 6e 52 65 67 20 3d 20 73 71 6c 69 74 . inReg = sqlit
21c31 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 e3ExprCode(pPars
21c32 65 2c 20 70 45 78 70 72 2c 20 74 61 72 67 65 74 e, pExpr, target
21c33 29 3b 0a 20 20 61 73 73 65 72 74 28 20 74 61 72 );. assert( tar
21c34 67 65 74 3e 30 20 29 3b 0a 20 20 2f 2a 20 54 68 get>0 );. /* Th
21c35 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 is routine is ca
21c36 6c 6c 65 64 20 66 6f 72 20 74 65 72 6d 73 20 74 lled for terms t
21c37 6f 20 49 4e 53 45 52 54 20 6f 72 20 55 50 44 41 o INSERT or UPDA
21c38 54 45 2e 20 20 41 6e 64 20 74 68 65 20 6f 6e 6c TE. And the onl
21c39 79 0a 20 20 2a 2a 20 6f 74 68 65 72 20 70 6c 61 y. ** other pla
21c3a 63 65 20 77 68 65 72 65 20 65 78 70 72 65 73 73 ce where express
21c3b 69 6f 6e 73 20 63 61 6e 20 62 65 20 63 6f 6e 76 ions can be conv
21c3c 65 72 74 65 64 20 69 6e 74 6f 20 54 4b 5f 52 45 erted into TK_RE
21c3d 47 49 53 54 45 52 20 69 73 0a 20 20 2a 2a 20 69 GISTER is. ** i
21c3e 6e 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 70 n WHERE clause p
21c3f 72 6f 63 65 73 73 69 6e 67 2e 20 20 53 6f 20 61 rocessing. So a
21c40 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6d 70 6c s currently impl
21c41 65 6d 65 6e 74 65 64 2c 20 74 68 65 72 65 20 69 emented, there i
21c42 73 0a 20 20 2a 2a 20 6e 6f 20 77 61 79 20 66 6f s. ** no way fo
21c43 72 20 61 20 54 4b 5f 52 45 47 49 53 54 45 52 20 r a TK_REGISTER
21c44 74 6f 20 65 78 69 73 74 20 68 65 72 65 2e 20 20 to exist here.
21c45 42 75 74 20 69 74 20 73 65 65 6d 73 20 70 72 75 But it seems pru
21c46 64 65 6e 74 20 74 6f 0a 20 20 2a 2a 20 6b 65 65 dent to. ** kee
21c47 70 20 74 68 65 20 41 4c 57 41 59 53 28 29 20 69 p the ALWAYS() i
21c48 6e 20 63 61 73 65 20 74 68 65 20 63 6f 6e 64 69 n case the condi
21c49 74 69 6f 6e 73 20 61 62 6f 76 65 20 63 68 61 6e tions above chan
21c4a 67 65 20 77 69 74 68 20 66 75 74 75 72 65 0a 20 ge with future.
21c4b 20 2a 2a 20 6d 6f 64 69 66 69 63 61 74 69 6f 6e ** modification
21c4c 73 20 6f 72 20 65 6e 68 61 6e 63 65 6d 65 6e 74 s or enhancement
21c4d 73 2e 20 2a 2f 0a 20 20 69 66 28 20 41 4c 57 41 s. */. if( ALWA
21c4e 59 53 28 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b YS(pExpr->op!=TK
21c4f 5f 52 45 47 49 53 54 45 52 29 20 29 7b 20 20 0a _REGISTER) ){ .
21c50 20 20 20 20 69 6e 74 20 69 4d 65 6d 3b 0a 20 20 int iMem;.
21c51 20 20 69 4d 65 6d 20 3d 20 2b 2b 70 50 61 72 73 iMem = ++pPars
21c52 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73 71 6c e->nMem;. sql
21c53 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 ite3VdbeAddOp2(v
21c54 2c 20 4f 50 5f 43 6f 70 79 2c 20 69 6e 52 65 67 , OP_Copy, inReg
21c55 2c 20 69 4d 65 6d 29 3b 0a 20 20 20 20 70 45 78 , iMem);. pEx
21c56 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 69 4d 65 pr->iTable = iMe
21c57 6d 3b 0a 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 m;. pExpr->op
21c58 20 3d 20 54 4b 5f 52 45 47 49 53 54 45 52 3b 0a = TK_REGISTER;.
21c59 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 69 6e 52 }. return inR
21c5a 65 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 eg;.}../*.** Ret
21c5b 75 72 6e 20 54 52 55 45 20 69 66 20 70 45 78 70 urn TRUE if pExp
21c5c 72 20 69 73 20 61 6e 20 63 6f 6e 73 74 61 6e 74 r is an constant
21c5d 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74 expression that
21c5e 20 69 73 20 61 70 70 72 6f 70 72 69 61 74 65 0a is appropriate.
21c5f 2a 2a 20 66 6f 72 20 66 61 63 74 6f 72 69 6e 67 ** for factoring
21c60 20 6f 75 74 20 6f 66 20 61 20 6c 6f 6f 70 2e 20 out of a loop.
21c61 20 41 70 70 72 6f 70 72 69 61 74 65 20 65 78 70 Appropriate exp
21c62 72 65 73 73 69 6f 6e 73 20 61 72 65 3a 0a 2a 2a ressions are:.**
21c63 0a 2a 2a 20 20 20 20 2a 20 20 41 6e 79 20 65 78 .** * Any ex
21c64 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20 65 76 pression that ev
21c65 61 6c 75 61 74 65 73 20 74 6f 20 74 77 6f 20 6f aluates to two o
21c66 72 20 6d 6f 72 65 20 6f 70 63 6f 64 65 73 2e 0a r more opcodes..
21c67 2a 2a 0a 2a 2a 20 20 20 20 2a 20 20 41 6e 79 20 **.** * Any
21c68 4f 50 5f 49 6e 74 65 67 65 72 2c 20 4f 50 5f 52 OP_Integer, OP_R
21c69 65 61 6c 2c 20 4f 50 5f 53 74 72 69 6e 67 2c 20 eal, OP_String,
21c6a 4f 50 5f 42 6c 6f 62 2c 20 4f 50 5f 4e 75 6c 6c OP_Blob, OP_Null
21c6b 2c 20 0a 2a 2a 20 20 20 20 20 20 20 6f 72 20 4f , .** or O
21c6c 50 5f 56 61 72 69 61 62 6c 65 20 74 68 61 74 20 P_Variable that
21c6d 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f does not need to
21c6e 20 62 65 20 70 6c 61 63 65 64 20 69 6e 20 61 20 be placed in a
21c6f 0a 2a 2a 20 20 20 20 20 20 20 73 70 65 63 69 66 .** specif
21c70 69 63 20 72 65 67 69 73 74 65 72 2e 0a 2a 2a 0a ic register..**.
21c71 2a 2a 20 54 68 65 72 65 20 69 73 20 6e 6f 20 70 ** There is no p
21c72 6f 69 6e 74 20 69 6e 20 66 61 63 74 6f 72 69 6e oint in factorin
21c73 67 20 6f 75 74 20 73 69 6e 67 6c 65 2d 69 6e 73 g out single-ins
21c74 74 72 75 63 74 69 6f 6e 20 63 6f 6e 73 74 61 6e truction constan
21c75 74 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 73 t.** expressions
21c76 20 74 68 61 74 20 6e 65 65 64 20 74 6f 20 62 65 that need to be
21c77 20 70 6c 61 63 65 64 20 69 6e 20 61 20 70 61 72 placed in a par
21c78 74 69 63 75 6c 61 72 20 72 65 67 69 73 74 65 72 ticular register
21c79 2e 20 20 0a 2a 2a 20 57 65 20 63 6f 75 6c 64 20 . .** We could
21c7a 66 61 63 74 6f 72 20 74 68 65 6d 20 6f 75 74 2c factor them out,
21c7b 20 62 75 74 20 74 68 65 6e 20 77 65 20 77 6f 75 but then we wou
21c7c 6c 64 20 65 6e 64 20 75 70 20 61 64 64 69 6e 67 ld end up adding
21c7d 20 61 6e 0a 2a 2a 20 4f 50 5f 53 43 6f 70 79 20 an.** OP_SCopy
21c7e 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 6f 20 6d instruction to m
21c7f 6f 76 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e ove the value in
21c80 74 6f 20 74 68 65 20 63 6f 72 72 65 63 74 20 72 to the correct r
21c81 65 67 69 73 74 65 72 0a 2a 2a 20 6c 61 74 65 72 egister.** later
21c82 2e 20 20 57 65 20 6d 69 67 68 74 20 61 73 20 77 . We might as w
21c83 65 6c 6c 20 6a 75 73 74 20 75 73 65 20 74 68 65 ell just use the
21c84 20 6f 72 69 67 69 6e 61 6c 20 69 6e 73 74 72 75 original instru
21c85 63 74 69 6f 6e 20 61 6e 64 0a 2a 2a 20 61 76 6f ction and.** avo
21c86 69 64 20 74 68 65 20 4f 50 5f 53 43 6f 70 79 2e id the OP_SCopy.
21c87 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69 .*/.static int i
21c88 73 41 70 70 72 6f 70 72 69 61 74 65 46 6f 72 46 sAppropriateForF
21c89 61 63 74 6f 72 69 6e 67 28 45 78 70 72 20 2a 70 actoring(Expr *p
21c8a 29 7b 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 ){. if( !sqlite
21c8b 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 4e 3ExprIsConstantN
21c8c 6f 74 4a 6f 69 6e 28 70 29 20 29 7b 0a 20 20 20 otJoin(p) ){.
21c8d 20 72 65 74 75 72 6e 20 30 3b 20 20 2f 2a 20 4f return 0; /* O
21c8e 6e 6c 79 20 63 6f 6e 73 74 61 6e 74 20 65 78 70 nly constant exp
21c8f 72 65 73 73 69 6f 6e 73 20 61 72 65 20 61 70 70 ressions are app
21c90 72 6f 70 72 69 61 74 65 20 66 6f 72 20 66 61 63 ropriate for fac
21c91 74 6f 72 69 6e 67 20 2a 2f 0a 20 20 7d 0a 20 20 toring */. }.
21c92 69 66 28 20 28 70 2d 3e 66 6c 61 67 73 20 26 20 if( (p->flags &
21c93 45 50 5f 46 69 78 65 64 44 65 73 74 29 3d 3d 30 EP_FixedDest)==0
21c94 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 ){. return 1
21c95 3b 20 20 2f 2a 20 41 6e 79 20 63 6f 6e 73 74 61 ; /* Any consta
21c96 6e 74 20 77 69 74 68 6f 75 74 20 61 20 66 69 78 nt without a fix
21c97 65 64 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 69 ed destination i
21c98 73 20 61 70 70 72 6f 70 72 69 61 74 65 20 2a 2f s appropriate */
21c99 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28 20 70 2d . }. while( p-
21c9a 3e 6f 70 3d 3d 54 4b 5f 55 50 4c 55 53 20 29 20 >op==TK_UPLUS )
21c9b 70 20 3d 20 70 2d 3e 70 4c 65 66 74 3b 0a 20 20 p = p->pLeft;.
21c9c 73 77 69 74 63 68 28 20 70 2d 3e 6f 70 20 29 7b switch( p->op ){
21c9d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
21c9e 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 OMIT_BLOB_LITERA
21c9f 4c 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 42 4c L. case TK_BL
21ca0 4f 42 3a 0a 23 65 6e 64 69 66 0a 20 20 20 20 63 OB:.#endif. c
21ca1 61 73 65 20 54 4b 5f 56 41 52 49 41 42 4c 45 3a ase TK_VARIABLE:
21ca2 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 4e 54 . case TK_INT
21ca3 45 47 45 52 3a 0a 20 20 20 20 63 61 73 65 20 54 EGER:. case T
21ca4 4b 5f 46 4c 4f 41 54 3a 0a 20 20 20 20 63 61 73 K_FLOAT:. cas
21ca5 65 20 54 4b 5f 4e 55 4c 4c 3a 0a 20 20 20 20 63 e TK_NULL:. c
21ca6 61 73 65 20 54 4b 5f 53 54 52 49 4e 47 3a 20 7b ase TK_STRING: {
21ca7 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
21ca8 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 42 4c 4f 42 20 p->op==TK_BLOB
21ca9 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 );. testcas
21caa 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 56 41 52 e( p->op==TK_VAR
21cab 49 41 42 4c 45 20 29 3b 0a 20 20 20 20 20 20 74 IABLE );. t
21cac 65 73 74 63 61 73 65 28 20 70 2d 3e 6f 70 3d 3d estcase( p->op==
21cad 54 4b 5f 49 4e 54 45 47 45 52 20 29 3b 0a 20 20 TK_INTEGER );.
21cae 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 2d testcase( p-
21caf 3e 6f 70 3d 3d 54 4b 5f 46 4c 4f 41 54 20 29 3b >op==TK_FLOAT );
21cb0 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
21cb1 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 4e 55 4c 4c 20 p->op==TK_NULL
21cb2 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 );. testcas
21cb3 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 53 54 52 e( p->op==TK_STR
21cb4 49 4e 47 20 29 3b 0a 20 20 20 20 20 20 2f 2a 20 ING );. /*
21cb5 53 69 6e 67 6c 65 2d 69 6e 73 74 72 75 63 74 69 Single-instructi
21cb6 6f 6e 20 63 6f 6e 73 74 61 6e 74 73 20 77 69 74 on constants wit
21cb7 68 20 61 20 66 69 78 65 64 20 64 65 73 74 69 6e h a fixed destin
21cb8 61 74 69 6f 6e 20 61 72 65 0a 20 20 20 20 20 20 ation are.
21cb9 2a 2a 20 62 65 74 74 65 72 20 64 6f 6e 65 20 69 ** better done i
21cba 6e 2d 6c 69 6e 65 2e 20 20 49 66 20 77 65 20 66 n-line. If we f
21cbb 61 63 74 6f 72 20 74 68 65 6d 2c 20 74 68 65 79 actor them, they
21cbc 20 77 69 6c 6c 20 6a 75 73 74 20 65 6e 64 0a 20 will just end.
21cbd 20 20 20 20 20 2a 2a 20 75 70 20 67 65 6e 65 72 ** up gener
21cbe 61 74 69 6e 67 20 61 6e 20 4f 50 5f 53 43 6f 70 ating an OP_SCop
21cbf 79 20 74 6f 20 6d 6f 76 65 20 74 68 65 20 76 61 y to move the va
21cc0 6c 75 65 20 74 6f 20 74 68 65 20 64 65 73 74 69 lue to the desti
21cc1 6e 61 74 69 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 nation. **
21cc2 72 65 67 69 73 74 65 72 2e 20 2a 2f 0a 20 20 20 register. */.
21cc3 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 return 0;.
21cc4 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 55 }. case TK_U
21cc5 4d 49 4e 55 53 3a 20 7b 0a 20 20 20 20 20 20 69 MINUS: {. i
21cc6 66 28 20 70 2d 3e 70 4c 65 66 74 2d 3e 6f 70 3d f( p->pLeft->op=
21cc7 3d 54 4b 5f 46 4c 4f 41 54 20 7c 7c 20 70 2d 3e =TK_FLOAT || p->
21cc8 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e pLeft->op==TK_IN
21cc9 54 45 47 45 52 20 29 7b 0a 20 20 20 20 20 20 20 TEGER ){.
21cca 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 return 0;.
21ccb 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a }. break;.
21ccc 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c }. defaul
21ccd 74 3a 20 7b 0a 20 20 20 20 20 20 62 72 65 61 6b t: {. break
21cce 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 ;. }. }. re
21ccf 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a turn 1;.}../*.**
21cd0 20 49 66 20 70 45 78 70 72 20 69 73 20 61 20 63 If pExpr is a c
21cd1 6f 6e 73 74 61 6e 74 20 65 78 70 72 65 73 73 69 onstant expressi
21cd2 6f 6e 20 74 68 61 74 20 69 73 20 61 70 70 72 6f on that is appro
21cd3 70 72 69 61 74 65 20 66 6f 72 0a 2a 2a 20 66 61 priate for.** fa
21cd4 63 74 6f 72 69 6e 67 20 6f 75 74 20 6f 66 20 61 ctoring out of a
21cd5 20 6c 6f 6f 70 2c 20 74 68 65 6e 20 65 76 61 6c loop, then eval
21cd6 75 61 74 65 20 74 68 65 20 65 78 70 72 65 73 73 uate the express
21cd7 69 6f 6e 0a 2a 2a 20 69 6e 74 6f 20 61 20 72 65 ion.** into a re
21cd8 67 69 73 74 65 72 20 61 6e 64 20 63 6f 6e 76 65 gister and conve
21cd9 72 74 20 74 68 65 20 65 78 70 72 65 73 73 69 6f rt the expressio
21cda 6e 20 69 6e 74 6f 20 61 20 54 4b 5f 52 45 47 49 n into a TK_REGI
21cdb 53 54 45 52 0a 2a 2a 20 65 78 70 72 65 73 73 69 STER.** expressi
21cdc 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e on..*/.static in
21cdd 74 20 65 76 61 6c 43 6f 6e 73 74 45 78 70 72 28 t evalConstExpr(
21cde 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c Walker *pWalker,
21cdf 20 45 78 70 72 20 2a 70 45 78 70 72 29 7b 0a 20 Expr *pExpr){.
21ce0 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 20 3d Parse *pParse =
21ce1 20 70 57 61 6c 6b 65 72 2d 3e 70 50 61 72 73 65 pWalker->pParse
21ce2 3b 0a 20 20 73 77 69 74 63 68 28 20 70 45 78 70 ;. switch( pExp
21ce3 72 2d 3e 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 r->op ){. cas
21ce4 65 20 54 4b 5f 52 45 47 49 53 54 45 52 3a 20 7b e TK_REGISTER: {
21ce5 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 . return WR
21ce6 43 5f 50 72 75 6e 65 3b 0a 20 20 20 20 7d 0a 20 C_Prune;. }.
21ce7 20 20 20 63 61 73 65 20 54 4b 5f 46 55 4e 43 54 case TK_FUNCT
21ce8 49 4f 4e 3a 0a 20 20 20 20 63 61 73 65 20 54 4b ION:. case TK
21ce9 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 3a 0a 20 _AGG_FUNCTION:.
21cea 20 20 20 63 61 73 65 20 54 4b 5f 43 4f 4e 53 54 case TK_CONST
21ceb 5f 46 55 4e 43 3a 20 7b 0a 20 20 20 20 20 20 2f _FUNC: {. /
21cec 2a 20 54 68 65 20 61 72 67 75 6d 65 6e 74 73 20 * The arguments
21ced 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 20 68 61 to a function ha
21cee 76 65 20 61 20 66 69 78 65 64 20 64 65 73 74 69 ve a fixed desti
21cef 6e 61 74 69 6f 6e 2e 0a 20 20 20 20 20 20 2a 2a nation.. **
21cf0 20 4d 61 72 6b 20 74 68 65 6d 20 74 68 69 73 20 Mark them this
21cf1 77 61 79 20 74 6f 20 61 76 6f 69 64 20 67 65 6e way to avoid gen
21cf2 65 72 61 74 65 64 20 75 6e 6e 65 65 64 65 64 20 erated unneeded
21cf3 4f 50 5f 53 43 6f 70 79 0a 20 20 20 20 20 20 2a OP_SCopy. *
21cf4 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 2e 20 * instructions.
21cf5 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
21cf6 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 20 ExprList *pList
21cf7 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 = pExpr->x.pList
21cf8 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
21cf9 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 !ExprHasProperty
21cfa 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65 (pExpr, EP_xIsSe
21cfb 6c 65 63 74 29 20 29 3b 0a 20 20 20 20 20 20 69 lect) );. i
21cfc 66 28 20 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 f( pList ){.
21cfd 20 20 20 20 69 6e 74 20 69 20 3d 20 70 4c 69 73 int i = pLis
21cfe 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20 20 20 t->nExpr;.
21cff 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 struct ExprLis
21d00 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d 20 t_item *pItem =
21d01 70 4c 69 73 74 2d 3e 61 3b 0a 20 20 20 20 20 20 pList->a;.
21d02 20 20 66 6f 72 28 3b 20 69 3e 30 3b 20 69 2d 2d for(; i>0; i--
21d03 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 , pItem++){.
21d04 20 20 20 20 20 20 69 66 28 20 41 4c 57 41 59 53 if( ALWAYS
21d05 28 70 49 74 65 6d 2d 3e 70 45 78 70 72 29 20 29 (pItem->pExpr) )
21d06 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 2d 3e 66 pItem->pExpr->f
21d07 6c 61 67 73 20 7c 3d 20 45 50 5f 46 69 78 65 64 lags |= EP_Fixed
21d08 44 65 73 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a Dest;. }.
21d09 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 }. br
21d0a 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 eak;. }. }.
21d0b 20 69 66 28 20 69 73 41 70 70 72 6f 70 72 69 61 if( isAppropria
21d0c 74 65 46 6f 72 46 61 63 74 6f 72 69 6e 67 28 70 teForFactoring(p
21d0d 45 78 70 72 29 20 29 7b 0a 20 20 20 20 69 6e 74 Expr) ){. int
21d0e 20 72 31 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e r1 = ++pParse->
21d0f 6e 4d 65 6d 3b 0a 20 20 20 20 69 6e 74 20 72 32 nMem;. int r2
21d10 3b 0a 20 20 20 20 72 32 20 3d 20 73 71 6c 69 74 ;. r2 = sqlit
21d11 65 33 45 78 70 72 43 6f 64 65 54 61 72 67 65 74 e3ExprCodeTarget
21d12 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 (pParse, pExpr,
21d13 72 31 29 3b 0a 20 20 20 20 69 66 28 20 4e 45 56 r1);. if( NEV
21d14 45 52 28 72 31 21 3d 72 32 29 20 29 20 73 71 6c ER(r1!=r2) ) sql
21d15 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 ite3ReleaseTempR
21d16 65 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b 0a eg(pParse, r1);.
21d17 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 32 20 3d pExpr->op2 =
21d18 20 70 45 78 70 72 2d 3e 6f 70 3b 0a 20 20 20 20 pExpr->op;.
21d19 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 52 pExpr->op = TK_R
21d1a 45 47 49 53 54 45 52 3b 0a 20 20 20 20 70 45 78 EGISTER;. pEx
21d1b 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 72 32 3b pr->iTable = r2;
21d1c 0a 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f . return WRC_
21d1d 50 72 75 6e 65 3b 0a 20 20 7d 0a 20 20 72 65 74 Prune;. }. ret
21d1e 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 urn WRC_Continue
21d1f 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 65 65 76 ;.}../*.** Preev
21d20 61 6c 75 61 74 65 20 63 6f 6e 73 74 61 6e 74 20 aluate constant
21d21 73 75 62 65 78 70 72 65 73 73 69 6f 6e 73 20 77 subexpressions w
21d22 69 74 68 69 6e 20 70 45 78 70 72 20 61 6e 64 20 ithin pExpr and
21d23 73 74 6f 72 65 20 74 68 65 0a 2a 2a 20 72 65 73 store the.** res
21d24 75 6c 74 73 20 69 6e 20 72 65 67 69 73 74 65 72 ults in register
21d25 73 2e 20 20 4d 6f 64 69 66 79 20 70 45 78 70 72 s. Modify pExpr
21d26 20 73 6f 20 74 68 61 74 20 74 68 65 20 63 6f 6e so that the con
21d27 73 74 61 6e 74 20 73 75 62 65 78 70 72 65 73 69 stant subexpresi
21d28 6f 6e 73 0a 2a 2a 20 61 72 65 20 54 4b 5f 52 45 ons.** are TK_RE
21d29 47 49 53 54 45 52 20 6f 70 63 6f 64 65 73 20 74 GISTER opcodes t
21d2a 68 61 74 20 72 65 66 65 72 20 74 6f 20 74 68 65 hat refer to the
21d2b 20 70 72 65 63 6f 6d 70 75 74 65 64 20 76 61 6c precomputed val
21d2c 75 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ues..*/.SQLITE_P
21d2d 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
21d2e 74 65 33 45 78 70 72 43 6f 64 65 43 6f 6e 73 74 te3ExprCodeConst
21d2f 61 6e 74 73 28 50 61 72 73 65 20 2a 70 50 61 72 ants(Parse *pPar
21d30 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72 29 se, Expr *pExpr)
21d31 7b 0a 20 20 57 61 6c 6b 65 72 20 77 3b 0a 20 20 {. Walker w;.
21d32 77 2e 78 45 78 70 72 43 61 6c 6c 62 61 63 6b 20 w.xExprCallback
21d33 3d 20 65 76 61 6c 43 6f 6e 73 74 45 78 70 72 3b = evalConstExpr;
21d34 0a 20 20 77 2e 78 53 65 6c 65 63 74 43 61 6c 6c . w.xSelectCall
21d35 62 61 63 6b 20 3d 20 30 3b 0a 20 20 77 2e 70 50 back = 0;. w.pP
21d36 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 arse = pParse;.
21d37 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 sqlite3WalkExpr
21d38 28 26 77 2c 20 70 45 78 70 72 29 3b 0a 7d 0a 0a (&w, pExpr);.}..
21d39 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 ./*.** Generate
21d3a 63 6f 64 65 20 74 68 61 74 20 70 75 73 68 65 73 code that pushes
21d3b 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 65 76 the value of ev
21d3c 65 72 79 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 ery element of t
21d3d 68 65 20 67 69 76 65 6e 0a 2a 2a 20 65 78 70 72 he given.** expr
21d3e 65 73 73 69 6f 6e 20 6c 69 73 74 20 69 6e 74 6f ession list into
21d3f 20 61 20 73 65 71 75 65 6e 63 65 20 6f 66 20 72 a sequence of r
21d40 65 67 69 73 74 65 72 73 20 62 65 67 69 6e 6e 69 egisters beginni
21d41 6e 67 20 61 74 20 74 61 72 67 65 74 2e 0a 2a 2a ng at target..**
21d42 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e .** Return the n
21d43 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 umber of element
21d44 73 20 65 76 61 6c 75 61 74 65 64 2e 0a 2a 2f 0a s evaluated..*/.
21d45 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
21d46 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f nt sqlite3ExprCo
21d47 64 65 45 78 70 72 4c 69 73 74 28 0a 20 20 50 61 deExprList(. Pa
21d48 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 rse *pParse,
21d49 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 /* Parsing cont
21d4a 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 ext */. ExprLis
21d4b 74 20 2a 70 4c 69 73 74 2c 20 20 20 2f 2a 20 54 t *pList, /* T
21d4c 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 he expression li
21d4d 73 74 20 74 6f 20 62 65 20 63 6f 64 65 64 20 2a st to be coded *
21d4e 2f 0a 20 20 69 6e 74 20 74 61 72 67 65 74 2c 20 /. int target,
21d4f 20 20 20 20 20 20 20 2f 2a 20 57 68 65 72 65 20 /* Where
21d50 74 6f 20 77 72 69 74 65 20 72 65 73 75 6c 74 73 to write results
21d51 20 2a 2f 0a 20 20 69 6e 74 20 64 6f 48 61 72 64 */. int doHard
21d52 43 6f 70 79 20 20 20 20 20 2f 2a 20 4d 61 6b 65 Copy /* Make
21d53 20 61 20 68 61 72 64 20 63 6f 70 79 20 6f 66 20 a hard copy of
21d54 65 76 65 72 79 20 65 6c 65 6d 65 6e 74 20 2a 2f every element */
21d55 0a 29 7b 0a 20 20 73 74 72 75 63 74 20 45 78 70 .){. struct Exp
21d56 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 rList_item *pIte
21d57 6d 3b 0a 20 20 69 6e 74 20 69 2c 20 6e 3b 0a 20 m;. int i, n;.
21d58 20 61 73 73 65 72 74 28 20 70 4c 69 73 74 21 3d assert( pList!=
21d59 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 74 0 );. assert( t
21d5a 61 72 67 65 74 3e 30 20 29 3b 0a 20 20 6e 20 3d arget>0 );. n =
21d5b 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 pList->nExpr;.
21d5c 20 66 6f 72 28 70 49 74 65 6d 3d 70 4c 69 73 74 for(pItem=pList
21d5d 2d 3e 61 2c 20 69 3d 30 3b 20 69 3c 6e 3b 20 69 ->a, i=0; i<n; i
21d5e 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 ++, pItem++){.
21d5f 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 69 41 6c if( pItem->iAl
21d60 69 61 73 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 ias ){. int
21d61 20 69 52 65 67 20 3d 20 63 6f 64 65 41 6c 69 61 iReg = codeAlia
21d62 73 28 70 50 61 72 73 65 2c 20 70 49 74 65 6d 2d s(pParse, pItem-
21d63 3e 69 41 6c 69 61 73 2c 20 70 49 74 65 6d 2d 3e >iAlias, pItem->
21d64 70 45 78 70 72 2c 20 74 61 72 67 65 74 2b 69 29 pExpr, target+i)
21d65 3b 0a 20 20 20 20 20 20 56 64 62 65 20 2a 76 20 ;. Vdbe *v
21d66 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 = sqlite3GetVdbe
21d67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 (pParse);.
21d68 69 66 28 20 69 52 65 67 21 3d 74 61 72 67 65 74 if( iReg!=target
21d69 2b 69 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 +i ){. sq
21d6a 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 lite3VdbeAddOp2(
21d6b 76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 69 52 65 v, OP_SCopy, iRe
21d6c 67 2c 20 74 61 72 67 65 74 2b 69 29 3b 0a 20 20 g, target+i);.
21d6d 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b }. }else{
21d6e 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 . sqlite3Ex
21d6f 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 prCode(pParse, p
21d70 49 74 65 6d 2d 3e 70 45 78 70 72 2c 20 74 61 72 Item->pExpr, tar
21d71 67 65 74 2b 69 29 3b 0a 20 20 20 20 7d 0a 20 20 get+i);. }.
21d72 20 20 69 66 28 20 64 6f 48 61 72 64 43 6f 70 79 if( doHardCopy
21d73 20 26 26 20 21 70 50 61 72 73 65 2d 3e 64 62 2d && !pParse->db-
21d74 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b >mallocFailed ){
21d75 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 . sqlite3Ex
21d76 70 72 48 61 72 64 43 6f 70 79 28 70 50 61 72 73 prHardCopy(pPars
21d77 65 2c 20 74 61 72 67 65 74 2c 20 6e 29 3b 0a 20 e, target, n);.
21d78 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
21d79 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 n n;.}../*.** Ge
21d7a 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20 nerate code for
21d7b 61 20 62 6f 6f 6c 65 61 6e 20 65 78 70 72 65 73 a boolean expres
21d7c 73 69 6f 6e 20 73 75 63 68 20 74 68 61 74 20 61 sion such that a
21d7d 20 6a 75 6d 70 20 69 73 20 6d 61 64 65 0a 2a 2a jump is made.**
21d7e 20 74 6f 20 74 68 65 20 6c 61 62 65 6c 20 22 64 to the label "d
21d7f 65 73 74 22 20 69 66 20 74 68 65 20 65 78 70 72 est" if the expr
21d80 65 73 73 69 6f 6e 20 69 73 20 74 72 75 65 20 62 ession is true b
21d81 75 74 20 65 78 65 63 75 74 69 6f 6e 0a 2a 2a 20 ut execution.**
21d82 63 6f 6e 74 69 6e 75 65 73 20 73 74 72 61 69 67 continues straig
21d83 68 74 20 74 68 72 75 20 69 66 20 74 68 65 20 65 ht thru if the e
21d84 78 70 72 65 73 73 69 6f 6e 20 69 73 20 66 61 6c xpression is fal
21d85 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 se..**.** If the
21d86 20 65 78 70 72 65 73 73 69 6f 6e 20 65 76 61 6c expression eval
21d87 75 61 74 65 73 20 74 6f 20 4e 55 4c 4c 20 28 6e uates to NULL (n
21d88 65 69 74 68 65 72 20 74 72 75 65 20 6e 6f 72 20 either true nor
21d89 66 61 6c 73 65 29 2c 20 74 68 65 6e 0a 2a 2a 20 false), then.**
21d8a 74 61 6b 65 20 74 68 65 20 6a 75 6d 70 20 69 66 take the jump if
21d8b 20 74 68 65 20 6a 75 6d 70 49 66 4e 75 6c 6c 20 the jumpIfNull
21d8c 66 6c 61 67 20 69 73 20 53 51 4c 49 54 45 5f 4a flag is SQLITE_J
21d8d 55 4d 50 49 46 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a UMPIFNULL..**.**
21d8e 20 54 68 69 73 20 63 6f 64 65 20 64 65 70 65 6e This code depen
21d8f 64 73 20 6f 6e 20 74 68 65 20 66 61 63 74 20 74 ds on the fact t
21d90 68 61 74 20 63 65 72 74 61 69 6e 20 74 6f 6b 65 hat certain toke
21d91 6e 20 76 61 6c 75 65 73 20 28 65 78 3a 20 54 4b n values (ex: TK
21d92 5f 45 51 29 0a 2a 2a 20 61 72 65 20 74 68 65 20 _EQ).** are the
21d93 73 61 6d 65 20 61 73 20 6f 70 63 6f 64 65 20 76 same as opcode v
21d94 61 6c 75 65 73 20 28 65 78 3a 20 4f 50 5f 45 71 alues (ex: OP_Eq
21d95 29 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 ) that implement
21d96 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 the correspondi
21d97 6e 67 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 2e ng.** operation.
21d98 20 20 53 70 65 63 69 61 6c 20 63 6f 6d 6d 65 6e Special commen
21d99 74 73 20 69 6e 20 76 64 62 65 2e 63 20 61 6e 64 ts in vdbe.c and
21d9a 20 74 68 65 20 6d 6b 6f 70 63 6f 64 65 68 2e 61 the mkopcodeh.a
21d9b 77 6b 20 73 63 72 69 70 74 20 69 6e 0a 2a 2a 20 wk script in.**
21d9c 74 68 65 20 6d 61 6b 65 20 70 72 6f 63 65 73 73 the make process
21d9d 20 63 61 75 73 65 20 74 68 65 73 65 20 76 61 6c cause these val
21d9e 75 65 73 20 74 6f 20 61 6c 69 67 6e 2e 20 20 41 ues to align. A
21d9f 73 73 65 72 74 28 29 73 20 69 6e 20 74 68 65 20 ssert()s in the
21da0 63 6f 64 65 0a 2a 2a 20 62 65 6c 6f 77 20 76 65 code.** below ve
21da1 72 69 66 79 20 74 68 61 74 20 74 68 65 20 6e 75 rify that the nu
21da2 6d 62 65 72 73 20 61 72 65 20 61 6c 69 67 6e 65 mbers are aligne
21da3 64 20 63 6f 72 72 65 63 74 6c 79 2e 0a 2a 2f 0a d correctly..*/.
21da4 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
21da5 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 49 oid sqlite3ExprI
21da6 66 54 72 75 65 28 50 61 72 73 65 20 2a 70 50 61 fTrue(Parse *pPa
21da7 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72 rse, Expr *pExpr
21da8 2c 20 69 6e 74 20 64 65 73 74 2c 20 69 6e 74 20 , int dest, int
21da9 6a 75 6d 70 49 66 4e 75 6c 6c 29 7b 0a 20 20 56 jumpIfNull){. V
21daa 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d dbe *v = pParse-
21dab 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 6f 70 >pVdbe;. int op
21dac 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 65 67 46 = 0;. int regF
21dad 72 65 65 31 20 3d 20 30 3b 0a 20 20 69 6e 74 20 ree1 = 0;. int
21dae 72 65 67 46 72 65 65 32 20 3d 20 30 3b 0a 20 20 regFree2 = 0;.
21daf 69 6e 74 20 72 31 2c 20 72 32 3b 0a 0a 20 20 61 int r1, r2;.. a
21db0 73 73 65 72 74 28 20 6a 75 6d 70 49 66 4e 75 6c ssert( jumpIfNul
21db1 6c 3d 3d 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 l==SQLITE_JUMPIF
21db2 4e 55 4c 4c 20 7c 7c 20 6a 75 6d 70 49 66 4e 75 NULL || jumpIfNu
21db3 6c 6c 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 4e ll==0 );. if( N
21db4 45 56 45 52 28 76 3d 3d 30 29 20 29 20 20 20 20 EVER(v==0) )
21db5 20 72 65 74 75 72 6e 3b 20 20 2f 2a 20 45 78 69 return; /* Exi
21db6 73 74 61 6e 63 65 20 6f 66 20 56 44 42 45 20 63 stance of VDBE c
21db7 68 65 63 6b 65 64 20 62 79 20 63 61 6c 6c 65 72 hecked by caller
21db8 20 2a 2f 0a 20 20 69 66 28 20 4e 45 56 45 52 28 */. if( NEVER(
21db9 70 45 78 70 72 3d 3d 30 29 20 29 20 72 65 74 75 pExpr==0) ) retu
21dba 72 6e 3b 20 20 2f 2a 20 4e 6f 20 77 61 79 20 74 rn; /* No way t
21dbb 68 69 73 20 63 61 6e 20 68 61 70 70 65 6e 20 2a his can happen *
21dbc 2f 0a 20 20 6f 70 20 3d 20 70 45 78 70 72 2d 3e /. op = pExpr->
21dbd 6f 70 3b 0a 20 20 73 77 69 74 63 68 28 20 6f 70 op;. switch( op
21dbe 20 29 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f ){. case TK_
21dbf 41 4e 44 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 AND: {. int
21dc0 20 64 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62 d2 = sqlite3Vdb
21dc1 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 eMakeLabel(v);.
21dc2 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6a testcase( j
21dc3 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a umpIfNull==0 );.
21dc4 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 sqlite3Exp
21dc5 72 43 61 63 68 65 50 75 73 68 28 70 50 61 72 73 rCachePush(pPars
21dc6 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 e);. sqlite
21dc7 33 45 78 70 72 49 66 46 61 6c 73 65 28 70 50 61 3ExprIfFalse(pPa
21dc8 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 rse, pExpr->pLef
21dc9 74 2c 20 64 32 2c 6a 75 6d 70 49 66 4e 75 6c 6c t, d2,jumpIfNull
21dca 5e 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 ^SQLITE_JUMPIFNU
21dcb 4c 4c 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 LL);. sqlit
21dcc 65 33 45 78 70 72 49 66 54 72 75 65 28 70 50 61 e3ExprIfTrue(pPa
21dcd 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 rse, pExpr->pRig
21dce 68 74 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 ht, dest, jumpIf
21dcf 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 73 71 6c Null);. sql
21dd0 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c ite3VdbeResolveL
21dd1 61 62 65 6c 28 76 2c 20 64 32 29 3b 0a 20 20 20 abel(v, d2);.
21dd2 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 sqlite3ExprCa
21dd3 63 68 65 50 6f 70 28 70 50 61 72 73 65 2c 20 31 chePop(pParse, 1
21dd4 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a );. break;.
21dd5 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 }. case T
21dd6 4b 5f 4f 52 3a 20 7b 0a 20 20 20 20 20 20 74 65 K_OR: {. te
21dd7 73 74 63 61 73 65 28 20 6a 75 6d 70 49 66 4e 75 stcase( jumpIfNu
21dd8 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 73 ll==0 );. s
21dd9 71 6c 69 74 65 33 45 78 70 72 49 66 54 72 75 65 qlite3ExprIfTrue
21dda 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e (pParse, pExpr->
21ddb 70 4c 65 66 74 2c 20 64 65 73 74 2c 20 6a 75 6d pLeft, dest, jum
21ddc 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 pIfNull);.
21ddd 73 71 6c 69 74 65 33 45 78 70 72 49 66 54 72 75 sqlite3ExprIfTru
21dde 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d e(pParse, pExpr-
21ddf 3e 70 52 69 67 68 74 2c 20 64 65 73 74 2c 20 6a >pRight, dest, j
21de0 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 umpIfNull);.
21de1 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
21de2 20 20 20 63 61 73 65 20 54 4b 5f 4e 4f 54 3a 20 case TK_NOT:
21de3 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 {. testcase
21de4 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 ( jumpIfNull==0
21de5 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
21de6 45 78 70 72 49 66 46 61 6c 73 65 28 70 50 61 72 ExprIfFalse(pPar
21de7 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 se, pExpr->pLeft
21de8 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 , dest, jumpIfNu
21de9 6c 6c 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b ll);. break
21dea 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 ;. }. case
21deb 20 54 4b 5f 4c 54 3a 0a 20 20 20 20 63 61 73 65 TK_LT:. case
21dec 20 54 4b 5f 4c 45 3a 0a 20 20 20 20 63 61 73 65 TK_LE:. case
21ded 20 54 4b 5f 47 54 3a 0a 20 20 20 20 63 61 73 65 TK_GT:. case
21dee 20 54 4b 5f 47 45 3a 0a 20 20 20 20 63 61 73 65 TK_GE:. case
21def 20 54 4b 5f 4e 45 3a 0a 20 20 20 20 63 61 73 65 TK_NE:. case
21df0 20 54 4b 5f 45 51 3a 20 7b 0a 20 20 20 20 20 20 TK_EQ: {.
21df1 61 73 73 65 72 74 28 20 54 4b 5f 4c 54 3d 3d 4f assert( TK_LT==O
21df2 50 5f 4c 74 20 29 3b 0a 20 20 20 20 20 20 61 73 P_Lt );. as
21df3 73 65 72 74 28 20 54 4b 5f 4c 45 3d 3d 4f 50 5f sert( TK_LE==OP_
21df4 4c 65 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 Le );. asse
21df5 72 74 28 20 54 4b 5f 47 54 3d 3d 4f 50 5f 47 74 rt( TK_GT==OP_Gt
21df6 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 );. assert
21df7 28 20 54 4b 5f 47 45 3d 3d 4f 50 5f 47 65 20 29 ( TK_GE==OP_Ge )
21df8 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
21df9 54 4b 5f 45 51 3d 3d 4f 50 5f 45 71 20 29 3b 0a TK_EQ==OP_Eq );.
21dfa 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b assert( TK
21dfb 5f 4e 45 3d 3d 4f 50 5f 4e 65 20 29 3b 0a 20 20 _NE==OP_Ne );.
21dfc 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 testcase( op
21dfd 3d 3d 54 4b 5f 4c 54 20 29 3b 0a 20 20 20 20 20 ==TK_LT );.
21dfe 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 testcase( op==T
21dff 4b 5f 4c 45 20 29 3b 0a 20 20 20 20 20 20 74 65 K_LE );. te
21e00 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 47 stcase( op==TK_G
21e01 54 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 T );. testc
21e02 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 47 45 20 29 ase( op==TK_GE )
21e03 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
21e04 28 20 6f 70 3d 3d 54 4b 5f 45 51 20 29 3b 0a 20 ( op==TK_EQ );.
21e05 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f testcase( o
21e06 70 3d 3d 54 4b 5f 4e 45 20 29 3b 0a 20 20 20 20 p==TK_NE );.
21e07 20 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d 70 testcase( jump
21e08 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 IfNull==0 );.
21e09 20 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 4f 70 codeCompareOp
21e0a 65 72 61 6e 64 73 28 70 50 61 72 73 65 2c 20 70 erands(pParse, p
21e0b 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 31 Expr->pLeft, &r1
21e0c 2c 20 26 72 65 67 46 72 65 65 31 2c 0a 20 20 20 , ®Free1,.
21e0d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21e0e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 p
21e0f 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 26 72 Expr->pRight, &r
21e10 32 2c 20 26 72 65 67 46 72 65 65 32 29 3b 0a 20 2, ®Free2);.
21e11 20 20 20 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 codeCompare
21e12 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e (pParse, pExpr->
21e13 70 4c 65 66 74 2c 20 70 45 78 70 72 2d 3e 70 52 pLeft, pExpr->pR
21e14 69 67 68 74 2c 20 6f 70 2c 0a 20 20 20 20 20 20 ight, op,.
21e15 20 20 20 20 20 20 20 20 20 20 20 20 72 31 2c 20 r1,
21e16 72 32 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 r2, dest, jumpIf
21e17 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 74 65 73 Null);. tes
21e18 74 63 61 73 65 28 20 72 65 67 46 72 65 65 31 3d tcase( regFree1=
21e19 3d 30 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 =0 );. test
21e1a 63 61 73 65 28 20 72 65 67 46 72 65 65 32 3d 3d case( regFree2==
21e1b 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 0 );. break
21e1c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 ;. }. case
21e1d 20 54 4b 5f 49 53 3a 0a 20 20 20 20 63 61 73 65 TK_IS:. case
21e1e 20 54 4b 5f 49 53 4e 4f 54 3a 20 7b 0a 20 20 20 TK_ISNOT: {.
21e1f 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d testcase( op=
21e20 3d 54 4b 5f 49 53 20 29 3b 0a 20 20 20 20 20 20 =TK_IS );.
21e21 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b testcase( op==TK
21e22 5f 49 53 4e 4f 54 20 29 3b 0a 20 20 20 20 20 20 _ISNOT );.
21e23 63 6f 64 65 43 6f 6d 70 61 72 65 4f 70 65 72 61 codeCompareOpera
21e24 6e 64 73 28 70 50 61 72 73 65 2c 20 70 45 78 70 nds(pParse, pExp
21e25 72 2d 3e 70 4c 65 66 74 2c 20 26 72 31 2c 20 26 r->pLeft, &r1, &
21e26 72 65 67 46 72 65 65 31 2c 0a 20 20 20 20 20 20 regFree1,.
21e27 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21e28 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78 70 pExp
21e29 72 2d 3e 70 52 69 67 68 74 2c 20 26 72 32 2c 20 r->pRight, &r2,
21e2a 26 72 65 67 46 72 65 65 32 29 3b 0a 20 20 20 20 ®Free2);.
21e2b 20 20 6f 70 20 3d 20 28 6f 70 3d 3d 54 4b 5f 49 op = (op==TK_I
21e2c 53 29 20 3f 20 54 4b 5f 45 51 20 3a 20 54 4b 5f S) ? TK_EQ : TK_
21e2d 4e 45 3b 0a 20 20 20 20 20 20 63 6f 64 65 43 6f NE;. codeCo
21e2e 6d 70 61 72 65 28 70 50 61 72 73 65 2c 20 70 45 mpare(pParse, pE
21e2f 78 70 72 2d 3e 70 4c 65 66 74 2c 20 70 45 78 70 xpr->pLeft, pExp
21e30 72 2d 3e 70 52 69 67 68 74 2c 20 6f 70 2c 0a 20 r->pRight, op,.
21e31 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21e32 20 72 31 2c 20 72 32 2c 20 64 65 73 74 2c 20 53 r1, r2, dest, S
21e33 51 4c 49 54 45 5f 4e 55 4c 4c 45 51 29 3b 0a 20 QLITE_NULLEQ);.
21e34 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72 testcase( r
21e35 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 egFree1==0 );.
21e36 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72 65 testcase( re
21e37 67 46 72 65 65 32 3d 3d 30 20 29 3b 0a 20 20 20 gFree2==0 );.
21e38 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
21e39 20 20 20 20 63 61 73 65 20 54 4b 5f 49 53 4e 55 case TK_ISNU
21e3a 4c 4c 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f LL:. case TK_
21e3b 4e 4f 54 4e 55 4c 4c 3a 20 7b 0a 20 20 20 20 20 NOTNULL: {.
21e3c 20 61 73 73 65 72 74 28 20 54 4b 5f 49 53 4e 55 assert( TK_ISNU
21e3d 4c 4c 3d 3d 4f 50 5f 49 73 4e 75 6c 6c 20 29 3b LL==OP_IsNull );
21e3e 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 . assert( T
21e3f 4b 5f 4e 4f 54 4e 55 4c 4c 3d 3d 4f 50 5f 4e 6f K_NOTNULL==OP_No
21e40 74 4e 75 6c 6c 20 29 3b 0a 20 20 20 20 20 20 74 tNull );. t
21e41 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f estcase( op==TK_
21e42 49 53 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 20 20 ISNULL );.
21e43 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b testcase( op==TK
21e44 5f 4e 4f 54 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 _NOTNULL );.
21e45 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 45 78 r1 = sqlite3Ex
21e46 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73 prCodeTemp(pPars
21e47 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c e, pExpr->pLeft,
21e48 20 26 72 65 67 46 72 65 65 31 29 3b 0a 20 20 20 ®Free1);.
21e49 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
21e4a 64 4f 70 32 28 76 2c 20 6f 70 2c 20 72 31 2c 20 dOp2(v, op, r1,
21e4b 64 65 73 74 29 3b 0a 20 20 20 20 20 20 74 65 73 dest);. tes
21e4c 74 63 61 73 65 28 20 72 65 67 46 72 65 65 31 3d tcase( regFree1=
21e4d 3d 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 =0 );. brea
21e4e 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 k;. }. cas
21e4f 65 20 54 4b 5f 42 45 54 57 45 45 4e 3a 20 7b 0a e TK_BETWEEN: {.
21e50 20 20 20 20 20 20 2f 2a 20 20 20 20 78 20 42 45 /* x BE
21e51 54 57 45 45 4e 20 79 20 41 4e 44 20 7a 0a 20 20 TWEEN y AND z.
21e52 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 **. **
21e53 49 73 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f Is equivalent to
21e54 20 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 . **.
21e55 20 2a 2a 20 20 20 20 78 3e 3d 79 20 41 4e 44 20 ** x>=y AND
21e56 78 3c 3d 7a 0a 20 20 20 20 20 20 2a 2a 0a 20 20 x<=z. **.
21e57 20 20 20 20 2a 2a 20 43 6f 64 65 20 69 74 20 61 ** Code it a
21e58 73 20 73 75 63 68 2c 20 74 61 6b 69 6e 67 20 63 s such, taking c
21e59 61 72 65 20 74 6f 20 64 6f 20 74 68 65 20 63 6f are to do the co
21e5a 6d 6d 6f 6e 20 73 75 62 65 78 70 72 65 73 73 69 mmon subexpressi
21e5b 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 65 6c 65 6d on. ** elem
21e5c 65 6e 74 61 74 69 6f 6e 20 6f 66 20 78 2e 0a 20 entation of x..
21e5d 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 45 78 */. Ex
21e5e 70 72 20 65 78 70 72 41 6e 64 3b 0a 20 20 20 20 pr exprAnd;.
21e5f 20 20 45 78 70 72 20 63 6f 6d 70 4c 65 66 74 3b Expr compLeft;
21e60 0a 20 20 20 20 20 20 45 78 70 72 20 63 6f 6d 70 . Expr comp
21e61 52 69 67 68 74 3b 0a 20 20 20 20 20 20 45 78 70 Right;. Exp
21e62 72 20 65 78 70 72 58 3b 0a 0a 20 20 20 20 20 20 r exprX;..
21e63 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73 assert( !ExprHas
21e64 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 Property(pExpr,
21e65 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 3b EP_xIsSelect) );
21e66 0a 20 20 20 20 20 20 65 78 70 72 58 20 3d 20 2a . exprX = *
21e67 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b 0a 20 20 pExpr->pLeft;.
21e68 20 20 20 20 65 78 70 72 41 6e 64 2e 6f 70 20 3d exprAnd.op =
21e69 20 54 4b 5f 41 4e 44 3b 0a 20 20 20 20 20 20 65 TK_AND;. e
21e6a 78 70 72 41 6e 64 2e 70 4c 65 66 74 20 3d 20 26 xprAnd.pLeft = &
21e6b 63 6f 6d 70 4c 65 66 74 3b 0a 20 20 20 20 20 20 compLeft;.
21e6c 65 78 70 72 41 6e 64 2e 70 52 69 67 68 74 20 3d exprAnd.pRight =
21e6d 20 26 63 6f 6d 70 52 69 67 68 74 3b 0a 20 20 20 &compRight;.
21e6e 20 20 20 63 6f 6d 70 4c 65 66 74 2e 6f 70 20 3d compLeft.op =
21e6f 20 54 4b 5f 47 45 3b 0a 20 20 20 20 20 20 63 6f TK_GE;. co
21e70 6d 70 4c 65 66 74 2e 70 4c 65 66 74 20 3d 20 26 mpLeft.pLeft = &
21e71 65 78 70 72 58 3b 0a 20 20 20 20 20 20 63 6f 6d exprX;. com
21e72 70 4c 65 66 74 2e 70 52 69 67 68 74 20 3d 20 70 pLeft.pRight = p
21e73 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2d 3e 61 Expr->x.pList->a
21e74 5b 30 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 [0].pExpr;.
21e75 20 63 6f 6d 70 52 69 67 68 74 2e 6f 70 20 3d 20 compRight.op =
21e76 54 4b 5f 4c 45 3b 0a 20 20 20 20 20 20 63 6f 6d TK_LE;. com
21e77 70 52 69 67 68 74 2e 70 4c 65 66 74 20 3d 20 26 pRight.pLeft = &
21e78 65 78 70 72 58 3b 0a 20 20 20 20 20 20 63 6f 6d exprX;. com
21e79 70 52 69 67 68 74 2e 70 52 69 67 68 74 20 3d 20 pRight.pRight =
21e7a 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2d 3e pExpr->x.pList->
21e7b 61 5b 31 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 a[1].pExpr;.
21e7c 20 20 65 78 70 72 58 2e 69 54 61 62 6c 65 20 3d exprX.iTable =
21e7d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 sqlite3ExprCode
21e7e 54 65 6d 70 28 70 50 61 72 73 65 2c 20 26 65 78 Temp(pParse, &ex
21e7f 70 72 58 2c 20 26 72 65 67 46 72 65 65 31 29 3b prX, ®Free1);
21e80 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
21e81 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a regFree1==0 );.
21e82 20 20 20 20 20 20 65 78 70 72 58 2e 6f 70 20 3d exprX.op =
21e83 20 54 4b 5f 52 45 47 49 53 54 45 52 3b 0a 20 20 TK_REGISTER;.
21e84 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6a 75 testcase( ju
21e85 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 mpIfNull==0 );.
21e86 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 sqlite3Expr
21e87 49 66 54 72 75 65 28 70 50 61 72 73 65 2c 20 26 IfTrue(pParse, &